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