ip_flow.c revision 1.68 1 /* $NetBSD: ip_flow.c,v 1.68 2016/06/13 08:04:44 knakahara 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.68 2016/06/13 08:04:44 knakahara 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 pool_put(&ipflow_pool, ipf);
351 }
352
353 struct ipflow *
354 ipflow_reap(bool just_one)
355 {
356 while (just_one || ipflow_inuse > ip_maxflows) {
357 struct ipflow *ipf, *maybe_ipf = NULL;
358 int s;
359
360 ipf = LIST_FIRST(&ipflowlist);
361 while (ipf != NULL) {
362 /*
363 * If this no longer points to a valid route
364 * reclaim it.
365 */
366 if (rtcache_validate(&ipf->ipf_ro) == NULL)
367 goto done;
368 /*
369 * choose the one that's been least recently
370 * used or has had the least uses in the
371 * last 1.5 intervals.
372 */
373 if (maybe_ipf == NULL ||
374 ipf->ipf_timer < maybe_ipf->ipf_timer ||
375 (ipf->ipf_timer == maybe_ipf->ipf_timer &&
376 ipf->ipf_last_uses + ipf->ipf_uses <
377 maybe_ipf->ipf_last_uses +
378 maybe_ipf->ipf_uses))
379 maybe_ipf = ipf;
380 ipf = LIST_NEXT(ipf, ipf_list);
381 }
382 ipf = maybe_ipf;
383 done:
384 /*
385 * Remove the entry from the flow table.
386 */
387 s = splnet();
388 IPFLOW_REMOVE(ipf);
389 splx(s);
390 ipflow_addstats(ipf);
391 rtcache_free(&ipf->ipf_ro);
392 if (just_one)
393 return ipf;
394 pool_put(&ipflow_pool, ipf);
395 ipflow_inuse--;
396 }
397 return NULL;
398 }
399
400 void
401 ipflow_slowtimo(void)
402 {
403 struct rtentry *rt;
404 struct ipflow *ipf, *next_ipf;
405 uint64_t *ips;
406
407 mutex_enter(softnet_lock);
408 KERNEL_LOCK(1, NULL);
409 for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
410 next_ipf = LIST_NEXT(ipf, ipf_list);
411 if (PRT_SLOW_ISEXPIRED(ipf->ipf_timer) ||
412 (rt = rtcache_validate(&ipf->ipf_ro)) == NULL) {
413 ipflow_free(ipf);
414 } else {
415 ipf->ipf_last_uses = ipf->ipf_uses;
416 rt->rt_use += ipf->ipf_uses;
417 ips = IP_STAT_GETREF();
418 ips[IP_STAT_TOTAL] += ipf->ipf_uses;
419 ips[IP_STAT_FORWARD] += ipf->ipf_uses;
420 ips[IP_STAT_FASTFORWARD] += ipf->ipf_uses;
421 IP_STAT_PUTREF();
422 ipf->ipf_uses = 0;
423 }
424 }
425 KERNEL_UNLOCK_ONE(NULL);
426 mutex_exit(softnet_lock);
427 }
428
429 void
430 ipflow_create(const struct route *ro, struct mbuf *m)
431 {
432 const struct ip *const ip = mtod(m, const struct ip *);
433 struct ipflow *ipf;
434 size_t hash;
435 int s;
436
437 /*
438 * Don't create cache entries for ICMP messages.
439 */
440 if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP)
441 return;
442
443 KERNEL_LOCK(1, NULL);
444
445 /*
446 * See if an existing flow struct exists. If so remove it from its
447 * list and free the old route. If not, try to malloc a new one
448 * (if we aren't at our limit).
449 */
450 ipf = ipflow_lookup(ip);
451 if (ipf == NULL) {
452 if (ipflow_inuse >= ip_maxflows) {
453 ipf = ipflow_reap(true);
454 } else {
455 ipf = pool_get(&ipflow_pool, PR_NOWAIT);
456 if (ipf == NULL)
457 goto out;
458 ipflow_inuse++;
459 }
460 memset(ipf, 0, sizeof(*ipf));
461 } else {
462 s = splnet();
463 IPFLOW_REMOVE(ipf);
464 splx(s);
465 ipflow_addstats(ipf);
466 rtcache_free(&ipf->ipf_ro);
467 ipf->ipf_uses = ipf->ipf_last_uses = 0;
468 ipf->ipf_errors = ipf->ipf_dropped = 0;
469 }
470
471 /*
472 * Fill in the updated information.
473 */
474 rtcache_copy(&ipf->ipf_ro, ro);
475 ipf->ipf_dst = ip->ip_dst;
476 ipf->ipf_src = ip->ip_src;
477 ipf->ipf_tos = ip->ip_tos;
478 PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
479
480 /*
481 * Insert into the approriate bucket of the flow table.
482 */
483 hash = ipflow_hash(ip);
484 s = splnet();
485 IPFLOW_INSERT(&ipflowtable[hash], ipf);
486 splx(s);
487
488 out:
489 KERNEL_UNLOCK_ONE(NULL);
490 }
491
492 int
493 ipflow_invalidate_all(int new_size)
494 {
495 struct ipflow *ipf, *next_ipf;
496 int s, error;
497
498 error = 0;
499 s = splnet();
500 for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
501 next_ipf = LIST_NEXT(ipf, ipf_list);
502 ipflow_free(ipf);
503 }
504
505 if (new_size)
506 error = ipflow_reinit(new_size);
507 splx(s);
508
509 return error;
510 }
511
512 #ifdef GATEWAY
513 /*
514 * sysctl helper routine for net.inet.ip.maxflows.
515 */
516 static int
517 sysctl_net_inet_ip_maxflows(SYSCTLFN_ARGS)
518 {
519 int error;
520
521 error = sysctl_lookup(SYSCTLFN_CALL(rnode));
522 if (error || newp == NULL)
523 return (error);
524
525 mutex_enter(softnet_lock);
526 KERNEL_LOCK(1, NULL);
527
528 ipflow_reap(false);
529
530 KERNEL_UNLOCK_ONE(NULL);
531 mutex_exit(softnet_lock);
532
533 return (0);
534 }
535
536 static int
537 sysctl_net_inet_ip_hashsize(SYSCTLFN_ARGS)
538 {
539 int error, tmp;
540 struct sysctlnode node;
541
542 node = *rnode;
543 tmp = ip_hashsize;
544 node.sysctl_data = &tmp;
545 error = sysctl_lookup(SYSCTLFN_CALL(&node));
546 if (error || newp == NULL)
547 return (error);
548
549 if ((tmp & (tmp - 1)) == 0 && tmp != 0) {
550 /*
551 * Can only fail due to malloc()
552 */
553 mutex_enter(softnet_lock);
554 KERNEL_LOCK(1, NULL);
555
556 error = ipflow_invalidate_all(tmp);
557
558 KERNEL_UNLOCK_ONE(NULL);
559 mutex_exit(softnet_lock);
560
561 } else {
562 /*
563 * EINVAL if not a power of 2
564 */
565 error = EINVAL;
566 }
567
568 return error;
569 }
570 #endif /* GATEWAY */
571
572 static void
573 ipflow_sysctl_init(struct sysctllog **clog)
574 {
575 sysctl_createv(clog, 0, NULL, NULL,
576 CTLFLAG_PERMANENT,
577 CTLTYPE_NODE, "inet",
578 SYSCTL_DESCR("PF_INET related settings"),
579 NULL, 0, NULL, 0,
580 CTL_NET, PF_INET, CTL_EOL);
581 sysctl_createv(clog, 0, NULL, NULL,
582 CTLFLAG_PERMANENT,
583 CTLTYPE_NODE, "ip",
584 SYSCTL_DESCR("IPv4 related settings"),
585 NULL, 0, NULL, 0,
586 CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
587
588 #ifdef GATEWAY
589 sysctl_createv(clog, 0, NULL, NULL,
590 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
591 CTLTYPE_INT, "maxflows",
592 SYSCTL_DESCR("Number of flows for fast forwarding"),
593 sysctl_net_inet_ip_maxflows, 0, &ip_maxflows, 0,
594 CTL_NET, PF_INET, IPPROTO_IP,
595 IPCTL_MAXFLOWS, CTL_EOL);
596 sysctl_createv(clog, 0, NULL, NULL,
597 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
598 CTLTYPE_INT, "hashsize",
599 SYSCTL_DESCR("Size of hash table for fast forwarding (IPv4)"),
600 sysctl_net_inet_ip_hashsize, 0, &ip_hashsize, 0,
601 CTL_NET, PF_INET, IPPROTO_IP,
602 CTL_CREATE, CTL_EOL);
603 #endif /* GATEWAY */
604 }
605