ip_flow.c revision 1.67 1 /* $NetBSD: ip_flow.c,v 1.67 2016/06/10 13:31:44 ozaki-r 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.67 2016/06/10 13:31:44 ozaki-r 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 struct ifnet *ifp;
181 int s;
182
183 /*
184 * Are we forwarding packets? Big enough for an IP packet?
185 */
186 if (!ipforwarding || ipflow_inuse == 0 || m->m_len < sizeof(struct ip))
187 return 0;
188
189 /*
190 * Was packet received as a link-level multicast or broadcast?
191 * If so, don't try to fast forward..
192 */
193 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0)
194 return 0;
195
196 /*
197 * IP header with no option and valid version and length
198 */
199 if (IP_HDR_ALIGNED_P(mtod(m, const void *)))
200 ip = mtod(m, struct ip *);
201 else {
202 memcpy(&ip_store, mtod(m, const void *), sizeof(ip_store));
203 ip = &ip_store;
204 }
205 iplen = ntohs(ip->ip_len);
206 if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2) ||
207 iplen < sizeof(struct ip) || iplen > m->m_pkthdr.len)
208 return 0;
209 /*
210 * Find a flow.
211 */
212 if ((ipf = ipflow_lookup(ip)) == NULL)
213 return 0;
214
215 ifp = m_get_rcvif(m, &s);
216 /*
217 * Verify the IP header checksum.
218 */
219 switch (m->m_pkthdr.csum_flags &
220 ((ifp->if_csum_flags_rx & M_CSUM_IPv4) |
221 M_CSUM_IPv4_BAD)) {
222 case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
223 m_put_rcvif(ifp, &s);
224 return 0;
225
226 case M_CSUM_IPv4:
227 /* Checksum was okay. */
228 break;
229
230 default:
231 /* Must compute it ourselves. */
232 if (in_cksum(m, sizeof(struct ip)) != 0) {
233 m_put_rcvif(ifp, &s);
234 return 0;
235 }
236 break;
237 }
238 m_put_rcvif(ifp, &s);
239
240 /*
241 * Route and interface still up?
242 */
243 if ((rt = rtcache_validate(&ipf->ipf_ro)) == NULL ||
244 (rt->rt_ifp->if_flags & IFF_UP) == 0 ||
245 (rt->rt_flags & (RTF_BLACKHOLE | RTF_BROADCAST)) != 0)
246 return 0;
247
248 /*
249 * Packet size OK? TTL?
250 */
251 if (m->m_pkthdr.len > rt->rt_ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
252 return 0;
253
254 /*
255 * Clear any in-bound checksum flags for this packet.
256 */
257 m->m_pkthdr.csum_flags = 0;
258
259 /*
260 * Everything checks out and so we can forward this packet.
261 * Modify the TTL and incrementally change the checksum.
262 *
263 * This method of adding the checksum works on either endian CPU.
264 * If htons() is inlined, all the arithmetic is folded; otherwise
265 * the htons()s are combined by CSE due to the const attribute.
266 *
267 * Don't bother using HW checksumming here -- the incremental
268 * update is pretty fast.
269 */
270 ip->ip_ttl -= IPTTLDEC;
271 if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
272 ip->ip_sum -= ~htons(IPTTLDEC << 8);
273 else
274 ip->ip_sum += htons(IPTTLDEC << 8);
275
276 /*
277 * Done modifying the header; copy it back, if necessary.
278 *
279 * XXX Use m_copyback_cow(9) here? --dyoung
280 */
281 if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0)
282 memcpy(mtod(m, void *), &ip_store, sizeof(ip_store));
283
284 /*
285 * Trim the packet in case it's too long..
286 */
287 if (m->m_pkthdr.len > iplen) {
288 if (m->m_len == m->m_pkthdr.len) {
289 m->m_len = iplen;
290 m->m_pkthdr.len = iplen;
291 } else
292 m_adj(m, iplen - m->m_pkthdr.len);
293 }
294
295 /*
296 * Send the packet on its way. All we can get back is ENOBUFS
297 */
298 ipf->ipf_uses++;
299 PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
300
301 if (rt->rt_flags & RTF_GATEWAY)
302 dst = rt->rt_gateway;
303 else
304 dst = rtcache_getdst(&ipf->ipf_ro);
305
306 KERNEL_LOCK(1, NULL);
307 if ((error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m, dst, rt)) != 0) {
308 if (error == ENOBUFS)
309 ipf->ipf_dropped++;
310 else
311 ipf->ipf_errors++;
312 }
313 KERNEL_UNLOCK_ONE(NULL);
314 return 1;
315 }
316
317 static void
319 ipflow_addstats(struct ipflow *ipf)
320 {
321 struct rtentry *rt;
322 uint64_t *ips;
323
324 if ((rt = rtcache_validate(&ipf->ipf_ro)) != NULL)
325 rt->rt_use += ipf->ipf_uses;
326
327 ips = IP_STAT_GETREF();
328 ips[IP_STAT_CANTFORWARD] += ipf->ipf_errors + ipf->ipf_dropped;
329 ips[IP_STAT_TOTAL] += ipf->ipf_uses;
330 ips[IP_STAT_FORWARD] += ipf->ipf_uses;
331 ips[IP_STAT_FASTFORWARD] += ipf->ipf_uses;
332 IP_STAT_PUTREF();
333 }
334
335 static void
336 ipflow_free(struct ipflow *ipf)
337 {
338 int s;
339 /*
340 * Remove the flow from the hash table (at elevated IPL).
341 * Once it's off the list, we can deal with it at normal
342 * network IPL.
343 */
344 s = splnet();
345 IPFLOW_REMOVE(ipf);
346 splx(s);
347 ipflow_addstats(ipf);
348 rtcache_free(&ipf->ipf_ro);
349 ipflow_inuse--;
350 s = splnet();
351 pool_put(&ipflow_pool, ipf);
352 splx(s);
353 }
354
355 struct ipflow *
356 ipflow_reap(bool just_one)
357 {
358 while (just_one || ipflow_inuse > ip_maxflows) {
359 struct ipflow *ipf, *maybe_ipf = NULL;
360 int s;
361
362 ipf = LIST_FIRST(&ipflowlist);
363 while (ipf != NULL) {
364 /*
365 * If this no longer points to a valid route
366 * reclaim it.
367 */
368 if (rtcache_validate(&ipf->ipf_ro) == NULL)
369 goto done;
370 /*
371 * choose the one that's been least recently
372 * used or has had the least uses in the
373 * last 1.5 intervals.
374 */
375 if (maybe_ipf == NULL ||
376 ipf->ipf_timer < maybe_ipf->ipf_timer ||
377 (ipf->ipf_timer == maybe_ipf->ipf_timer &&
378 ipf->ipf_last_uses + ipf->ipf_uses <
379 maybe_ipf->ipf_last_uses +
380 maybe_ipf->ipf_uses))
381 maybe_ipf = ipf;
382 ipf = LIST_NEXT(ipf, ipf_list);
383 }
384 ipf = maybe_ipf;
385 done:
386 /*
387 * Remove the entry from the flow table.
388 */
389 s = splnet();
390 IPFLOW_REMOVE(ipf);
391 splx(s);
392 ipflow_addstats(ipf);
393 rtcache_free(&ipf->ipf_ro);
394 if (just_one)
395 return ipf;
396 pool_put(&ipflow_pool, ipf);
397 ipflow_inuse--;
398 }
399 return NULL;
400 }
401
402 void
403 ipflow_slowtimo(void)
404 {
405 struct rtentry *rt;
406 struct ipflow *ipf, *next_ipf;
407 uint64_t *ips;
408
409 mutex_enter(softnet_lock);
410 KERNEL_LOCK(1, NULL);
411 for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
412 next_ipf = LIST_NEXT(ipf, ipf_list);
413 if (PRT_SLOW_ISEXPIRED(ipf->ipf_timer) ||
414 (rt = rtcache_validate(&ipf->ipf_ro)) == NULL) {
415 ipflow_free(ipf);
416 } else {
417 ipf->ipf_last_uses = ipf->ipf_uses;
418 rt->rt_use += ipf->ipf_uses;
419 ips = IP_STAT_GETREF();
420 ips[IP_STAT_TOTAL] += ipf->ipf_uses;
421 ips[IP_STAT_FORWARD] += ipf->ipf_uses;
422 ips[IP_STAT_FASTFORWARD] += ipf->ipf_uses;
423 IP_STAT_PUTREF();
424 ipf->ipf_uses = 0;
425 }
426 }
427 KERNEL_UNLOCK_ONE(NULL);
428 mutex_exit(softnet_lock);
429 }
430
431 void
432 ipflow_create(const struct route *ro, struct mbuf *m)
433 {
434 const struct ip *const ip = mtod(m, const struct ip *);
435 struct ipflow *ipf;
436 size_t hash;
437 int s;
438
439 /*
440 * Don't create cache entries for ICMP messages.
441 */
442 if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP)
443 return;
444
445 KERNEL_LOCK(1, NULL);
446
447 /*
448 * See if an existing flow struct exists. If so remove it from its
449 * list and free the old route. If not, try to malloc a new one
450 * (if we aren't at our limit).
451 */
452 ipf = ipflow_lookup(ip);
453 if (ipf == NULL) {
454 if (ipflow_inuse >= ip_maxflows) {
455 ipf = ipflow_reap(true);
456 } else {
457 s = splnet();
458 ipf = pool_get(&ipflow_pool, PR_NOWAIT);
459 splx(s);
460 if (ipf == NULL)
461 goto out;
462 ipflow_inuse++;
463 }
464 memset(ipf, 0, sizeof(*ipf));
465 } else {
466 s = splnet();
467 IPFLOW_REMOVE(ipf);
468 splx(s);
469 ipflow_addstats(ipf);
470 rtcache_free(&ipf->ipf_ro);
471 ipf->ipf_uses = ipf->ipf_last_uses = 0;
472 ipf->ipf_errors = ipf->ipf_dropped = 0;
473 }
474
475 /*
476 * Fill in the updated information.
477 */
478 rtcache_copy(&ipf->ipf_ro, ro);
479 ipf->ipf_dst = ip->ip_dst;
480 ipf->ipf_src = ip->ip_src;
481 ipf->ipf_tos = ip->ip_tos;
482 PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
483
484 /*
485 * Insert into the approriate bucket of the flow table.
486 */
487 hash = ipflow_hash(ip);
488 s = splnet();
489 IPFLOW_INSERT(&ipflowtable[hash], ipf);
490 splx(s);
491
492 out:
493 KERNEL_UNLOCK_ONE(NULL);
494 }
495
496 int
497 ipflow_invalidate_all(int new_size)
498 {
499 struct ipflow *ipf, *next_ipf;
500 int s, error;
501
502 error = 0;
503 s = splnet();
504 for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
505 next_ipf = LIST_NEXT(ipf, ipf_list);
506 ipflow_free(ipf);
507 }
508
509 if (new_size)
510 error = ipflow_reinit(new_size);
511 splx(s);
512
513 return error;
514 }
515
516 #ifdef GATEWAY
517 /*
518 * sysctl helper routine for net.inet.ip.maxflows.
519 */
520 static int
521 sysctl_net_inet_ip_maxflows(SYSCTLFN_ARGS)
522 {
523 int error;
524
525 error = sysctl_lookup(SYSCTLFN_CALL(rnode));
526 if (error || newp == NULL)
527 return (error);
528
529 mutex_enter(softnet_lock);
530 KERNEL_LOCK(1, NULL);
531
532 ipflow_reap(false);
533
534 KERNEL_UNLOCK_ONE(NULL);
535 mutex_exit(softnet_lock);
536
537 return (0);
538 }
539
540 static int
541 sysctl_net_inet_ip_hashsize(SYSCTLFN_ARGS)
542 {
543 int error, tmp;
544 struct sysctlnode node;
545
546 node = *rnode;
547 tmp = ip_hashsize;
548 node.sysctl_data = &tmp;
549 error = sysctl_lookup(SYSCTLFN_CALL(&node));
550 if (error || newp == NULL)
551 return (error);
552
553 if ((tmp & (tmp - 1)) == 0 && tmp != 0) {
554 /*
555 * Can only fail due to malloc()
556 */
557 mutex_enter(softnet_lock);
558 KERNEL_LOCK(1, NULL);
559
560 error = ipflow_invalidate_all(tmp);
561
562 KERNEL_UNLOCK_ONE(NULL);
563 mutex_exit(softnet_lock);
564
565 } else {
566 /*
567 * EINVAL if not a power of 2
568 */
569 error = EINVAL;
570 }
571
572 return error;
573 }
574 #endif /* GATEWAY */
575
576 static void
577 ipflow_sysctl_init(struct sysctllog **clog)
578 {
579 sysctl_createv(clog, 0, NULL, NULL,
580 CTLFLAG_PERMANENT,
581 CTLTYPE_NODE, "inet",
582 SYSCTL_DESCR("PF_INET related settings"),
583 NULL, 0, NULL, 0,
584 CTL_NET, PF_INET, CTL_EOL);
585 sysctl_createv(clog, 0, NULL, NULL,
586 CTLFLAG_PERMANENT,
587 CTLTYPE_NODE, "ip",
588 SYSCTL_DESCR("IPv4 related settings"),
589 NULL, 0, NULL, 0,
590 CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
591
592 #ifdef GATEWAY
593 sysctl_createv(clog, 0, NULL, NULL,
594 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
595 CTLTYPE_INT, "maxflows",
596 SYSCTL_DESCR("Number of flows for fast forwarding"),
597 sysctl_net_inet_ip_maxflows, 0, &ip_maxflows, 0,
598 CTL_NET, PF_INET, IPPROTO_IP,
599 IPCTL_MAXFLOWS, CTL_EOL);
600 sysctl_createv(clog, 0, NULL, NULL,
601 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
602 CTLTYPE_INT, "hashsize",
603 SYSCTL_DESCR("Size of hash table for fast forwarding (IPv4)"),
604 sysctl_net_inet_ip_hashsize, 0, &ip_hashsize, 0,
605 CTL_NET, PF_INET, IPPROTO_IP,
606 CTL_CREATE, CTL_EOL);
607 #endif /* GATEWAY */
608 }
609