ip_flow.c revision 1.73 1 1.73 ozaki /* $NetBSD: ip_flow.c,v 1.73 2016/07/11 07:37:00 ozaki-r Exp $ */
2 1.1 matt
3 1.1 matt /*-
4 1.1 matt * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.1 matt * by the 3am Software Foundry ("3am"). It was developed by Matt Thomas.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt *
19 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
30 1.1 matt */
31 1.22 lukem
32 1.22 lukem #include <sys/cdefs.h>
33 1.73 ozaki __KERNEL_RCSID(0, "$NetBSD: ip_flow.c,v 1.73 2016/07/11 07:37:00 ozaki-r Exp $");
34 1.1 matt
35 1.1 matt #include <sys/param.h>
36 1.1 matt #include <sys/systm.h>
37 1.1 matt #include <sys/malloc.h>
38 1.1 matt #include <sys/mbuf.h>
39 1.1 matt #include <sys/domain.h>
40 1.1 matt #include <sys/protosw.h>
41 1.1 matt #include <sys/socket.h>
42 1.1 matt #include <sys/socketvar.h>
43 1.1 matt #include <sys/errno.h>
44 1.1 matt #include <sys/time.h>
45 1.1 matt #include <sys/kernel.h>
46 1.7 thorpej #include <sys/pool.h>
47 1.1 matt #include <sys/sysctl.h>
48 1.73 ozaki #include <sys/workqueue.h>
49 1.1 matt
50 1.1 matt #include <net/if.h>
51 1.1 matt #include <net/if_dl.h>
52 1.1 matt #include <net/route.h>
53 1.1 matt #include <net/pfil.h>
54 1.1 matt
55 1.1 matt #include <netinet/in.h>
56 1.1 matt #include <netinet/in_systm.h>
57 1.1 matt #include <netinet/ip.h>
58 1.1 matt #include <netinet/in_pcb.h>
59 1.1 matt #include <netinet/in_var.h>
60 1.1 matt #include <netinet/ip_var.h>
61 1.54 thorpej #include <netinet/ip_private.h>
62 1.1 matt
63 1.44 liamjfoy /*
64 1.44 liamjfoy * Similar code is very well commented in netinet6/ip6_flow.c
65 1.44 liamjfoy */
66 1.44 liamjfoy
67 1.53 thorpej #define IPFLOW_HASHBITS 6 /* should not be a multiple of 8 */
68 1.53 thorpej
69 1.57 pooka static struct pool ipflow_pool;
70 1.7 thorpej
71 1.5 thorpej LIST_HEAD(ipflowhead, ipflow);
72 1.5 thorpej
73 1.1 matt #define IPFLOW_TIMER (5 * PR_SLOWHZ)
74 1.43 liamjfoy #define IPFLOW_DEFAULT_HASHSIZE (1 << IPFLOW_HASHBITS)
75 1.5 thorpej
76 1.70 knakahar /*
77 1.70 knakahar * ip_flow.c internal lock.
78 1.70 knakahar * If we use softnet_lock, it would cause recursive lock.
79 1.70 knakahar *
80 1.70 knakahar * This is a tentative workaround.
81 1.70 knakahar * We should make it scalable somehow in the future.
82 1.70 knakahar */
83 1.70 knakahar static kmutex_t ipflow_lock;
84 1.43 liamjfoy static struct ipflowhead *ipflowtable = NULL;
85 1.5 thorpej static struct ipflowhead ipflowlist;
86 1.1 matt static int ipflow_inuse;
87 1.5 thorpej
88 1.5 thorpej #define IPFLOW_INSERT(bucket, ipf) \
89 1.5 thorpej do { \
90 1.5 thorpej LIST_INSERT_HEAD((bucket), (ipf), ipf_hash); \
91 1.5 thorpej LIST_INSERT_HEAD(&ipflowlist, (ipf), ipf_list); \
92 1.26 perry } while (/*CONSTCOND*/ 0)
93 1.5 thorpej
94 1.5 thorpej #define IPFLOW_REMOVE(ipf) \
95 1.5 thorpej do { \
96 1.5 thorpej LIST_REMOVE((ipf), ipf_hash); \
97 1.5 thorpej LIST_REMOVE((ipf), ipf_list); \
98 1.26 perry } while (/*CONSTCOND*/ 0)
99 1.5 thorpej
100 1.3 matt #ifndef IPFLOW_MAX
101 1.1 matt #define IPFLOW_MAX 256
102 1.3 matt #endif
103 1.64 rmind static int ip_maxflows = IPFLOW_MAX;
104 1.64 rmind static int ip_hashsize = IPFLOW_DEFAULT_HASHSIZE;
105 1.64 rmind
106 1.69 knakahar static struct ipflow *ipflow_reap(bool);
107 1.64 rmind static void ipflow_sysctl_init(struct sysctllog **);
108 1.1 matt
109 1.73 ozaki static void ipflow_slowtimo_work(struct work *, void *);
110 1.73 ozaki static struct workqueue *ipflow_slowtimo_wq;
111 1.73 ozaki static struct work ipflow_slowtimo_wk;
112 1.73 ozaki
113 1.45 liamjfoy static size_t
114 1.51 dyoung ipflow_hash(const struct ip *ip)
115 1.1 matt {
116 1.45 liamjfoy size_t hash = ip->ip_tos;
117 1.45 liamjfoy size_t idx;
118 1.45 liamjfoy
119 1.45 liamjfoy for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS) {
120 1.45 liamjfoy hash += (ip->ip_dst.s_addr >> (32 - idx)) +
121 1.45 liamjfoy (ip->ip_src.s_addr >> idx);
122 1.45 liamjfoy }
123 1.45 liamjfoy
124 1.43 liamjfoy return hash & (ip_hashsize-1);
125 1.1 matt }
126 1.1 matt
127 1.1 matt static struct ipflow *
128 1.51 dyoung ipflow_lookup(const struct ip *ip)
129 1.1 matt {
130 1.45 liamjfoy size_t hash;
131 1.1 matt struct ipflow *ipf;
132 1.1 matt
133 1.70 knakahar KASSERT(mutex_owned(&ipflow_lock));
134 1.70 knakahar
135 1.45 liamjfoy hash = ipflow_hash(ip);
136 1.1 matt
137 1.30 christos LIST_FOREACH(ipf, &ipflowtable[hash], ipf_hash) {
138 1.1 matt if (ip->ip_dst.s_addr == ipf->ipf_dst.s_addr
139 1.1 matt && ip->ip_src.s_addr == ipf->ipf_src.s_addr
140 1.1 matt && ip->ip_tos == ipf->ipf_tos)
141 1.1 matt break;
142 1.1 matt }
143 1.1 matt return ipf;
144 1.1 matt }
145 1.1 matt
146 1.57 pooka void
147 1.58 cegger ipflow_poolinit(void)
148 1.57 pooka {
149 1.57 pooka
150 1.57 pooka pool_init(&ipflow_pool, sizeof(struct ipflow), 0, 0, 0, "ipflowpl",
151 1.57 pooka NULL, IPL_NET);
152 1.57 pooka }
153 1.57 pooka
154 1.64 rmind static int
155 1.64 rmind ipflow_reinit(int table_size)
156 1.7 thorpej {
157 1.43 liamjfoy struct ipflowhead *new_table;
158 1.45 liamjfoy size_t i;
159 1.7 thorpej
160 1.70 knakahar KASSERT(mutex_owned(&ipflow_lock));
161 1.70 knakahar
162 1.43 liamjfoy new_table = (struct ipflowhead *)malloc(sizeof(struct ipflowhead) *
163 1.43 liamjfoy table_size, M_RTABLE, M_NOWAIT);
164 1.43 liamjfoy
165 1.43 liamjfoy if (new_table == NULL)
166 1.43 liamjfoy return 1;
167 1.43 liamjfoy
168 1.43 liamjfoy if (ipflowtable != NULL)
169 1.43 liamjfoy free(ipflowtable, M_RTABLE);
170 1.43 liamjfoy
171 1.43 liamjfoy ipflowtable = new_table;
172 1.43 liamjfoy ip_hashsize = table_size;
173 1.43 liamjfoy
174 1.7 thorpej LIST_INIT(&ipflowlist);
175 1.43 liamjfoy for (i = 0; i < ip_hashsize; i++)
176 1.7 thorpej LIST_INIT(&ipflowtable[i]);
177 1.43 liamjfoy
178 1.43 liamjfoy return 0;
179 1.7 thorpej }
180 1.7 thorpej
181 1.64 rmind void
182 1.64 rmind ipflow_init(void)
183 1.64 rmind {
184 1.73 ozaki int error;
185 1.73 ozaki
186 1.73 ozaki error = workqueue_create(&ipflow_slowtimo_wq, "ipflow_slowtimo",
187 1.73 ozaki ipflow_slowtimo_work, NULL, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
188 1.73 ozaki if (error != 0)
189 1.73 ozaki panic("%s: workqueue_create failed (%d)\n", __func__, error);
190 1.70 knakahar
191 1.70 knakahar mutex_init(&ipflow_lock, MUTEX_DEFAULT, IPL_NONE);
192 1.70 knakahar
193 1.70 knakahar mutex_enter(&ipflow_lock);
194 1.64 rmind (void)ipflow_reinit(ip_hashsize);
195 1.70 knakahar mutex_exit(&ipflow_lock);
196 1.64 rmind ipflow_sysctl_init(NULL);
197 1.64 rmind }
198 1.64 rmind
199 1.1 matt int
200 1.29 perry ipflow_fastforward(struct mbuf *m)
201 1.1 matt {
202 1.51 dyoung struct ip *ip;
203 1.51 dyoung struct ip ip_store;
204 1.1 matt struct ipflow *ipf;
205 1.1 matt struct rtentry *rt;
206 1.40 dyoung const struct sockaddr *dst;
207 1.1 matt int error;
208 1.6 sommerfe int iplen;
209 1.67 ozaki struct ifnet *ifp;
210 1.67 ozaki int s;
211 1.70 knakahar int ret = 0;
212 1.1 matt
213 1.70 knakahar mutex_enter(&ipflow_lock);
214 1.1 matt /*
215 1.1 matt * Are we forwarding packets? Big enough for an IP packet?
216 1.1 matt */
217 1.3 matt if (!ipforwarding || ipflow_inuse == 0 || m->m_len < sizeof(struct ip))
218 1.70 knakahar goto out;
219 1.14 sommerfe
220 1.14 sommerfe /*
221 1.19 wiz * Was packet received as a link-level multicast or broadcast?
222 1.14 sommerfe * If so, don't try to fast forward..
223 1.14 sommerfe */
224 1.14 sommerfe if ((m->m_flags & (M_BCAST|M_MCAST)) != 0)
225 1.70 knakahar goto out;
226 1.24 itojun
227 1.1 matt /*
228 1.1 matt * IP header with no option and valid version and length
229 1.1 matt */
230 1.51 dyoung if (IP_HDR_ALIGNED_P(mtod(m, const void *)))
231 1.25 thorpej ip = mtod(m, struct ip *);
232 1.25 thorpej else {
233 1.51 dyoung memcpy(&ip_store, mtod(m, const void *), sizeof(ip_store));
234 1.25 thorpej ip = &ip_store;
235 1.25 thorpej }
236 1.6 sommerfe iplen = ntohs(ip->ip_len);
237 1.5 thorpej if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2) ||
238 1.13 proff iplen < sizeof(struct ip) || iplen > m->m_pkthdr.len)
239 1.70 knakahar goto out;
240 1.1 matt /*
241 1.1 matt * Find a flow.
242 1.1 matt */
243 1.1 matt if ((ipf = ipflow_lookup(ip)) == NULL)
244 1.70 knakahar goto out;
245 1.1 matt
246 1.67 ozaki ifp = m_get_rcvif(m, &s);
247 1.1 matt /*
248 1.18 thorpej * Verify the IP header checksum.
249 1.2 thorpej */
250 1.18 thorpej switch (m->m_pkthdr.csum_flags &
251 1.67 ozaki ((ifp->if_csum_flags_rx & M_CSUM_IPv4) |
252 1.18 thorpej M_CSUM_IPv4_BAD)) {
253 1.18 thorpej case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
254 1.67 ozaki m_put_rcvif(ifp, &s);
255 1.70 knakahar goto out;
256 1.18 thorpej
257 1.18 thorpej case M_CSUM_IPv4:
258 1.18 thorpej /* Checksum was okay. */
259 1.18 thorpej break;
260 1.18 thorpej
261 1.18 thorpej default:
262 1.18 thorpej /* Must compute it ourselves. */
263 1.67 ozaki if (in_cksum(m, sizeof(struct ip)) != 0) {
264 1.67 ozaki m_put_rcvif(ifp, &s);
265 1.70 knakahar goto out;
266 1.67 ozaki }
267 1.18 thorpej break;
268 1.18 thorpej }
269 1.67 ozaki m_put_rcvif(ifp, &s);
270 1.2 thorpej
271 1.2 thorpej /*
272 1.1 matt * Route and interface still up?
273 1.1 matt */
274 1.50 dyoung if ((rt = rtcache_validate(&ipf->ipf_ro)) == NULL ||
275 1.66 roy (rt->rt_ifp->if_flags & IFF_UP) == 0 ||
276 1.66 roy (rt->rt_flags & (RTF_BLACKHOLE | RTF_BROADCAST)) != 0)
277 1.70 knakahar goto out;
278 1.1 matt
279 1.1 matt /*
280 1.1 matt * Packet size OK? TTL?
281 1.1 matt */
282 1.1 matt if (m->m_pkthdr.len > rt->rt_ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
283 1.70 knakahar goto out;
284 1.1 matt
285 1.1 matt /*
286 1.18 thorpej * Clear any in-bound checksum flags for this packet.
287 1.18 thorpej */
288 1.18 thorpej m->m_pkthdr.csum_flags = 0;
289 1.18 thorpej
290 1.18 thorpej /*
291 1.1 matt * Everything checks out and so we can forward this packet.
292 1.1 matt * Modify the TTL and incrementally change the checksum.
293 1.24 itojun *
294 1.9 mycroft * This method of adding the checksum works on either endian CPU.
295 1.9 mycroft * If htons() is inlined, all the arithmetic is folded; otherwise
296 1.32 perry * the htons()s are combined by CSE due to the const attribute.
297 1.18 thorpej *
298 1.18 thorpej * Don't bother using HW checksumming here -- the incremental
299 1.18 thorpej * update is pretty fast.
300 1.1 matt */
301 1.1 matt ip->ip_ttl -= IPTTLDEC;
302 1.12 itohy if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
303 1.11 mycroft ip->ip_sum -= ~htons(IPTTLDEC << 8);
304 1.8 mycroft else
305 1.2 thorpej ip->ip_sum += htons(IPTTLDEC << 8);
306 1.25 thorpej
307 1.25 thorpej /*
308 1.25 thorpej * Done modifying the header; copy it back, if necessary.
309 1.51 dyoung *
310 1.51 dyoung * XXX Use m_copyback_cow(9) here? --dyoung
311 1.25 thorpej */
312 1.41 christos if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0)
313 1.41 christos memcpy(mtod(m, void *), &ip_store, sizeof(ip_store));
314 1.6 sommerfe
315 1.6 sommerfe /*
316 1.24 itojun * Trim the packet in case it's too long..
317 1.6 sommerfe */
318 1.6 sommerfe if (m->m_pkthdr.len > iplen) {
319 1.6 sommerfe if (m->m_len == m->m_pkthdr.len) {
320 1.6 sommerfe m->m_len = iplen;
321 1.6 sommerfe m->m_pkthdr.len = iplen;
322 1.6 sommerfe } else
323 1.6 sommerfe m_adj(m, iplen - m->m_pkthdr.len);
324 1.2 thorpej }
325 1.1 matt
326 1.1 matt /*
327 1.65 snj * Send the packet on its way. All we can get back is ENOBUFS
328 1.1 matt */
329 1.1 matt ipf->ipf_uses++;
330 1.5 thorpej PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
331 1.16 thorpej
332 1.16 thorpej if (rt->rt_flags & RTF_GATEWAY)
333 1.16 thorpej dst = rt->rt_gateway;
334 1.16 thorpej else
335 1.40 dyoung dst = rtcache_getdst(&ipf->ipf_ro);
336 1.16 thorpej
337 1.72 knakahar if ((error = if_output_lock(rt->rt_ifp, rt->rt_ifp, m, dst, rt)) != 0) {
338 1.1 matt if (error == ENOBUFS)
339 1.1 matt ipf->ipf_dropped++;
340 1.1 matt else
341 1.1 matt ipf->ipf_errors++;
342 1.1 matt }
343 1.70 knakahar ret = 1;
344 1.70 knakahar out:
345 1.70 knakahar mutex_exit(&ipflow_lock);
346 1.70 knakahar return ret;
347 1.1 matt }
348 1.1 matt
349 1.1 matt static void
351 1.1 matt ipflow_addstats(struct ipflow *ipf)
352 1.49 dyoung {
353 1.54 thorpej struct rtentry *rt;
354 1.49 dyoung uint64_t *ips;
355 1.50 dyoung
356 1.49 dyoung if ((rt = rtcache_validate(&ipf->ipf_ro)) != NULL)
357 1.54 thorpej rt->rt_use += ipf->ipf_uses;
358 1.54 thorpej
359 1.54 thorpej ips = IP_STAT_GETREF();
360 1.54 thorpej ips[IP_STAT_CANTFORWARD] += ipf->ipf_errors + ipf->ipf_dropped;
361 1.54 thorpej ips[IP_STAT_TOTAL] += ipf->ipf_uses;
362 1.54 thorpej ips[IP_STAT_FORWARD] += ipf->ipf_uses;
363 1.54 thorpej ips[IP_STAT_FASTFORWARD] += ipf->ipf_uses;
364 1.1 matt IP_STAT_PUTREF();
365 1.1 matt }
366 1.1 matt
367 1.29 perry static void
368 1.1 matt ipflow_free(struct ipflow *ipf)
369 1.70 knakahar {
370 1.70 knakahar
371 1.70 knakahar KASSERT(mutex_owned(&ipflow_lock));
372 1.1 matt
373 1.1 matt /*
374 1.1 matt * Remove the flow from the hash table (at elevated IPL).
375 1.1 matt * Once it's off the list, we can deal with it at normal
376 1.1 matt * network IPL.
377 1.5 thorpej */
378 1.71 knakahar IPFLOW_REMOVE(ipf);
379 1.1 matt
380 1.38 joerg ipflow_addstats(ipf);
381 1.1 matt rtcache_free(&ipf->ipf_ro);
382 1.7 thorpej ipflow_inuse--;
383 1.1 matt pool_put(&ipflow_pool, ipf);
384 1.1 matt }
385 1.69 knakahar
386 1.53 thorpej static struct ipflow *
387 1.1 matt ipflow_reap(bool just_one)
388 1.70 knakahar {
389 1.70 knakahar
390 1.70 knakahar KASSERT(mutex_owned(&ipflow_lock));
391 1.3 matt
392 1.3 matt while (just_one || ipflow_inuse > ip_maxflows) {
393 1.3 matt struct ipflow *ipf, *maybe_ipf = NULL;
394 1.5 thorpej
395 1.5 thorpej ipf = LIST_FIRST(&ipflowlist);
396 1.5 thorpej while (ipf != NULL) {
397 1.5 thorpej /*
398 1.5 thorpej * If this no longer points to a valid route
399 1.5 thorpej * reclaim it.
400 1.50 dyoung */
401 1.5 thorpej if (rtcache_validate(&ipf->ipf_ro) == NULL)
402 1.5 thorpej goto done;
403 1.5 thorpej /*
404 1.5 thorpej * choose the one that's been least recently
405 1.5 thorpej * used or has had the least uses in the
406 1.5 thorpej * last 1.5 intervals.
407 1.5 thorpej */
408 1.5 thorpej if (maybe_ipf == NULL ||
409 1.5 thorpej ipf->ipf_timer < maybe_ipf->ipf_timer ||
410 1.5 thorpej (ipf->ipf_timer == maybe_ipf->ipf_timer &&
411 1.5 thorpej ipf->ipf_last_uses + ipf->ipf_uses <
412 1.5 thorpej maybe_ipf->ipf_last_uses +
413 1.5 thorpej maybe_ipf->ipf_uses))
414 1.5 thorpej maybe_ipf = ipf;
415 1.1 matt ipf = LIST_NEXT(ipf, ipf_list);
416 1.3 matt }
417 1.3 matt ipf = maybe_ipf;
418 1.3 matt done:
419 1.3 matt /*
420 1.3 matt * Remove the entry from the flow table.
421 1.5 thorpej */
422 1.71 knakahar IPFLOW_REMOVE(ipf);
423 1.3 matt
424 1.38 joerg ipflow_addstats(ipf);
425 1.3 matt rtcache_free(&ipf->ipf_ro);
426 1.3 matt if (just_one)
427 1.7 thorpej return ipf;
428 1.3 matt pool_put(&ipflow_pool, ipf);
429 1.1 matt ipflow_inuse--;
430 1.3 matt }
431 1.1 matt return NULL;
432 1.1 matt }
433 1.73 ozaki
434 1.73 ozaki static bool ipflow_work_enqueued = false;
435 1.73 ozaki
436 1.73 ozaki static void
437 1.1 matt ipflow_slowtimo_work(struct work *wk, void *arg)
438 1.49 dyoung {
439 1.5 thorpej struct rtentry *rt;
440 1.54 thorpej struct ipflow *ipf, *next_ipf;
441 1.2 thorpej uint64_t *ips;
442 1.55 ad
443 1.70 knakahar mutex_enter(softnet_lock);
444 1.55 ad mutex_enter(&ipflow_lock);
445 1.30 christos KERNEL_LOCK(1, NULL);
446 1.5 thorpej for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
447 1.37 dyoung next_ipf = LIST_NEXT(ipf, ipf_list);
448 1.50 dyoung if (PRT_SLOW_ISEXPIRED(ipf->ipf_timer) ||
449 1.5 thorpej (rt = rtcache_validate(&ipf->ipf_ro)) == NULL) {
450 1.5 thorpej ipflow_free(ipf);
451 1.5 thorpej } else {
452 1.49 dyoung ipf->ipf_last_uses = ipf->ipf_uses;
453 1.54 thorpej rt->rt_use += ipf->ipf_uses;
454 1.54 thorpej ips = IP_STAT_GETREF();
455 1.54 thorpej ips[IP_STAT_TOTAL] += ipf->ipf_uses;
456 1.54 thorpej ips[IP_STAT_FORWARD] += ipf->ipf_uses;
457 1.54 thorpej ips[IP_STAT_FASTFORWARD] += ipf->ipf_uses;
458 1.5 thorpej IP_STAT_PUTREF();
459 1.1 matt ipf->ipf_uses = 0;
460 1.1 matt }
461 1.73 ozaki }
462 1.55 ad ipflow_work_enqueued = false;
463 1.70 knakahar KERNEL_UNLOCK_ONE(NULL);
464 1.55 ad mutex_exit(&ipflow_lock);
465 1.1 matt mutex_exit(softnet_lock);
466 1.1 matt }
467 1.1 matt
468 1.73 ozaki void
469 1.73 ozaki ipflow_slowtimo(void)
470 1.73 ozaki {
471 1.73 ozaki
472 1.73 ozaki /* Avoid enqueuing another work when one is already enqueued */
473 1.73 ozaki mutex_enter(&ipflow_lock);
474 1.73 ozaki if (ipflow_work_enqueued) {
475 1.73 ozaki mutex_exit(&ipflow_lock);
476 1.73 ozaki return;
477 1.73 ozaki }
478 1.73 ozaki ipflow_work_enqueued = true;
479 1.73 ozaki mutex_exit(&ipflow_lock);
480 1.73 ozaki
481 1.73 ozaki workqueue_enqueue(ipflow_slowtimo_wq, &ipflow_slowtimo_wk, NULL);
482 1.73 ozaki }
483 1.73 ozaki
484 1.29 perry void
485 1.1 matt ipflow_create(const struct route *ro, struct mbuf *m)
486 1.51 dyoung {
487 1.1 matt const struct ip *const ip = mtod(m, const struct ip *);
488 1.45 liamjfoy struct ipflow *ipf;
489 1.1 matt size_t hash;
490 1.70 knakahar
491 1.70 knakahar mutex_enter(&ipflow_lock);
492 1.1 matt
493 1.1 matt /*
494 1.1 matt * Don't create cache entries for ICMP messages.
495 1.70 knakahar */
496 1.70 knakahar if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP) {
497 1.1 matt mutex_exit(&ipflow_lock);
498 1.70 knakahar return;
499 1.63 pooka }
500 1.63 pooka
501 1.63 pooka KERNEL_LOCK(1, NULL);
502 1.1 matt
503 1.65 snj /*
504 1.1 matt * See if an existing flow struct exists. If so remove it from its
505 1.1 matt * list and free the old route. If not, try to malloc a new one
506 1.1 matt * (if we aren't at our limit).
507 1.1 matt */
508 1.1 matt ipf = ipflow_lookup(ip);
509 1.3 matt if (ipf == NULL) {
510 1.53 thorpej if (ipflow_inuse >= ip_maxflows) {
511 1.1 matt ipf = ipflow_reap(true);
512 1.7 thorpej } else {
513 1.1 matt ipf = pool_get(&ipflow_pool, PR_NOWAIT);
514 1.63 pooka if (ipf == NULL)
515 1.1 matt goto out;
516 1.1 matt ipflow_inuse++;
517 1.39 dyoung }
518 1.1 matt memset(ipf, 0, sizeof(*ipf));
519 1.5 thorpej } else {
520 1.71 knakahar IPFLOW_REMOVE(ipf);
521 1.1 matt
522 1.38 joerg ipflow_addstats(ipf);
523 1.1 matt rtcache_free(&ipf->ipf_ro);
524 1.1 matt ipf->ipf_uses = ipf->ipf_last_uses = 0;
525 1.1 matt ipf->ipf_errors = ipf->ipf_dropped = 0;
526 1.1 matt }
527 1.1 matt
528 1.1 matt /*
529 1.1 matt * Fill in the updated information.
530 1.46 dyoung */
531 1.1 matt rtcache_copy(&ipf->ipf_ro, ro);
532 1.1 matt ipf->ipf_dst = ip->ip_dst;
533 1.1 matt ipf->ipf_src = ip->ip_src;
534 1.5 thorpej ipf->ipf_tos = ip->ip_tos;
535 1.60 liamjfoy PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
536 1.1 matt
537 1.1 matt /*
538 1.1 matt * Insert into the approriate bucket of the flow table.
539 1.45 liamjfoy */
540 1.5 thorpej hash = ipflow_hash(ip);
541 1.63 pooka IPFLOW_INSERT(&ipflowtable[hash], ipf);
542 1.63 pooka
543 1.63 pooka out:
544 1.70 knakahar KERNEL_UNLOCK_ONE(NULL);
545 1.27 scw mutex_exit(&ipflow_lock);
546 1.27 scw }
547 1.43 liamjfoy
548 1.43 liamjfoy int
549 1.27 scw ipflow_invalidate_all(int new_size)
550 1.27 scw {
551 1.71 knakahar struct ipflow *ipf, *next_ipf;
552 1.27 scw int error;
553 1.43 liamjfoy
554 1.70 knakahar error = 0;
555 1.70 knakahar
556 1.70 knakahar mutex_enter(&ipflow_lock);
557 1.27 scw
558 1.27 scw for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
559 1.27 scw next_ipf = LIST_NEXT(ipf, ipf_list);
560 1.27 scw ipflow_free(ipf);
561 1.43 liamjfoy }
562 1.43 liamjfoy
563 1.64 rmind if (new_size)
564 1.43 liamjfoy error = ipflow_reinit(new_size);
565 1.70 knakahar
566 1.70 knakahar mutex_exit(&ipflow_lock);
567 1.43 liamjfoy
568 1.1 matt return error;
569 1.64 rmind }
570 1.64 rmind
571 1.64 rmind #ifdef GATEWAY
572 1.64 rmind /*
573 1.64 rmind * sysctl helper routine for net.inet.ip.maxflows.
574 1.64 rmind */
575 1.64 rmind static int
576 1.64 rmind sysctl_net_inet_ip_maxflows(SYSCTLFN_ARGS)
577 1.64 rmind {
578 1.64 rmind int error;
579 1.64 rmind
580 1.64 rmind error = sysctl_lookup(SYSCTLFN_CALL(rnode));
581 1.64 rmind if (error || newp == NULL)
582 1.64 rmind return (error);
583 1.64 rmind
584 1.70 knakahar mutex_enter(softnet_lock);
585 1.64 rmind mutex_enter(&ipflow_lock);
586 1.64 rmind KERNEL_LOCK(1, NULL);
587 1.64 rmind
588 1.64 rmind ipflow_reap(false);
589 1.64 rmind
590 1.70 knakahar KERNEL_UNLOCK_ONE(NULL);
591 1.64 rmind mutex_exit(&ipflow_lock);
592 1.64 rmind mutex_exit(softnet_lock);
593 1.64 rmind
594 1.64 rmind return (0);
595 1.64 rmind }
596 1.64 rmind
597 1.64 rmind static int
598 1.64 rmind sysctl_net_inet_ip_hashsize(SYSCTLFN_ARGS)
599 1.64 rmind {
600 1.64 rmind int error, tmp;
601 1.64 rmind struct sysctlnode node;
602 1.64 rmind
603 1.64 rmind node = *rnode;
604 1.64 rmind tmp = ip_hashsize;
605 1.64 rmind node.sysctl_data = &tmp;
606 1.64 rmind error = sysctl_lookup(SYSCTLFN_CALL(&node));
607 1.64 rmind if (error || newp == NULL)
608 1.64 rmind return (error);
609 1.64 rmind
610 1.64 rmind if ((tmp & (tmp - 1)) == 0 && tmp != 0) {
611 1.64 rmind /*
612 1.64 rmind * Can only fail due to malloc()
613 1.64 rmind */
614 1.64 rmind mutex_enter(softnet_lock);
615 1.64 rmind KERNEL_LOCK(1, NULL);
616 1.64 rmind
617 1.64 rmind error = ipflow_invalidate_all(tmp);
618 1.64 rmind
619 1.64 rmind KERNEL_UNLOCK_ONE(NULL);
620 1.64 rmind mutex_exit(softnet_lock);
621 1.64 rmind
622 1.64 rmind } else {
623 1.64 rmind /*
624 1.64 rmind * EINVAL if not a power of 2
625 1.64 rmind */
626 1.64 rmind error = EINVAL;
627 1.64 rmind }
628 1.64 rmind
629 1.64 rmind return error;
630 1.64 rmind }
631 1.64 rmind #endif /* GATEWAY */
632 1.64 rmind
633 1.64 rmind static void
634 1.64 rmind ipflow_sysctl_init(struct sysctllog **clog)
635 1.64 rmind {
636 1.64 rmind sysctl_createv(clog, 0, NULL, NULL,
637 1.64 rmind CTLFLAG_PERMANENT,
638 1.64 rmind CTLTYPE_NODE, "inet",
639 1.64 rmind SYSCTL_DESCR("PF_INET related settings"),
640 1.64 rmind NULL, 0, NULL, 0,
641 1.64 rmind CTL_NET, PF_INET, CTL_EOL);
642 1.64 rmind sysctl_createv(clog, 0, NULL, NULL,
643 1.64 rmind CTLFLAG_PERMANENT,
644 1.64 rmind CTLTYPE_NODE, "ip",
645 1.64 rmind SYSCTL_DESCR("IPv4 related settings"),
646 1.64 rmind NULL, 0, NULL, 0,
647 1.64 rmind CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
648 1.64 rmind
649 1.64 rmind #ifdef GATEWAY
650 1.64 rmind sysctl_createv(clog, 0, NULL, NULL,
651 1.64 rmind CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
652 1.64 rmind CTLTYPE_INT, "maxflows",
653 1.64 rmind SYSCTL_DESCR("Number of flows for fast forwarding"),
654 1.64 rmind sysctl_net_inet_ip_maxflows, 0, &ip_maxflows, 0,
655 1.64 rmind CTL_NET, PF_INET, IPPROTO_IP,
656 1.64 rmind IPCTL_MAXFLOWS, CTL_EOL);
657 1.64 rmind sysctl_createv(clog, 0, NULL, NULL,
658 1.64 rmind CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
659 1.64 rmind CTLTYPE_INT, "hashsize",
660 1.64 rmind SYSCTL_DESCR("Size of hash table for fast forwarding (IPv4)"),
661 1.64 rmind sysctl_net_inet_ip_hashsize, 0, &ip_hashsize, 0,
662 1.64 rmind CTL_NET, PF_INET, IPPROTO_IP,
663 1.64 rmind CTL_CREATE, CTL_EOL);
664 1.64 rmind #endif /* GATEWAY */
665 }
666