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