ip_flow.c revision 1.62 1 /* $NetBSD: ip_flow.c,v 1.62 2014/03/19 10:54:20 liamjfoy 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.62 2014/03/19 10:54:20 liamjfoy 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 int ip_maxflows = IPFLOW_MAX;
95 int ip_hashsize = IPFLOW_DEFAULT_HASHSIZE;
96
97 static size_t
98 ipflow_hash(const struct ip *ip)
99 {
100 size_t hash = ip->ip_tos;
101 size_t idx;
102
103 for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS) {
104 hash += (ip->ip_dst.s_addr >> (32 - idx)) +
105 (ip->ip_src.s_addr >> idx);
106 }
107
108 return hash & (ip_hashsize-1);
109 }
110
111 static struct ipflow *
112 ipflow_lookup(const struct ip *ip)
113 {
114 size_t hash;
115 struct ipflow *ipf;
116
117 hash = ipflow_hash(ip);
118
119 LIST_FOREACH(ipf, &ipflowtable[hash], ipf_hash) {
120 if (ip->ip_dst.s_addr == ipf->ipf_dst.s_addr
121 && ip->ip_src.s_addr == ipf->ipf_src.s_addr
122 && ip->ip_tos == ipf->ipf_tos)
123 break;
124 }
125 return ipf;
126 }
127
128 void
129 ipflow_poolinit(void)
130 {
131
132 pool_init(&ipflow_pool, sizeof(struct ipflow), 0, 0, 0, "ipflowpl",
133 NULL, IPL_NET);
134 }
135
136 int
137 ipflow_init(int table_size)
138 {
139 struct ipflowhead *new_table;
140 size_t i;
141
142 new_table = (struct ipflowhead *)malloc(sizeof(struct ipflowhead) *
143 table_size, M_RTABLE, M_NOWAIT);
144
145 if (new_table == NULL)
146 return 1;
147
148 if (ipflowtable != NULL)
149 free(ipflowtable, M_RTABLE);
150
151 ipflowtable = new_table;
152 ip_hashsize = table_size;
153
154 LIST_INIT(&ipflowlist);
155 for (i = 0; i < ip_hashsize; i++)
156 LIST_INIT(&ipflowtable[i]);
157
158 return 0;
159 }
160
161 int
162 ipflow_fastforward(struct mbuf *m)
163 {
164 struct ip *ip;
165 struct ip ip_store;
166 struct ipflow *ipf;
167 struct rtentry *rt;
168 const struct sockaddr *dst;
169 int error;
170 int iplen;
171
172 /*
173 * Are we forwarding packets? Big enough for an IP packet?
174 */
175 if (!ipforwarding || ipflow_inuse == 0 || m->m_len < sizeof(struct ip))
176 return 0;
177
178 /*
179 * Was packet received as a link-level multicast or broadcast?
180 * If so, don't try to fast forward..
181 */
182 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0)
183 return 0;
184
185 /*
186 * IP header with no option and valid version and length
187 */
188 if (IP_HDR_ALIGNED_P(mtod(m, const void *)))
189 ip = mtod(m, struct ip *);
190 else {
191 memcpy(&ip_store, mtod(m, const void *), sizeof(ip_store));
192 ip = &ip_store;
193 }
194 iplen = ntohs(ip->ip_len);
195 if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2) ||
196 iplen < sizeof(struct ip) || iplen > m->m_pkthdr.len)
197 return 0;
198 /*
199 * Find a flow.
200 */
201 if ((ipf = ipflow_lookup(ip)) == NULL)
202 return 0;
203
204 /*
205 * Verify the IP header checksum.
206 */
207 switch (m->m_pkthdr.csum_flags &
208 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
209 M_CSUM_IPv4_BAD)) {
210 case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
211 return (0);
212
213 case M_CSUM_IPv4:
214 /* Checksum was okay. */
215 break;
216
217 default:
218 /* Must compute it ourselves. */
219 if (in_cksum(m, sizeof(struct ip)) != 0)
220 return (0);
221 break;
222 }
223
224 /*
225 * Route and interface still up?
226 */
227 if ((rt = rtcache_validate(&ipf->ipf_ro)) == NULL ||
228 (rt->rt_ifp->if_flags & IFF_UP) == 0)
229 return 0;
230
231 /*
232 * Packet size OK? TTL?
233 */
234 if (m->m_pkthdr.len > rt->rt_ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
235 return 0;
236
237 /*
238 * Clear any in-bound checksum flags for this packet.
239 */
240 m->m_pkthdr.csum_flags = 0;
241
242 /*
243 * Everything checks out and so we can forward this packet.
244 * Modify the TTL and incrementally change the checksum.
245 *
246 * This method of adding the checksum works on either endian CPU.
247 * If htons() is inlined, all the arithmetic is folded; otherwise
248 * the htons()s are combined by CSE due to the const attribute.
249 *
250 * Don't bother using HW checksumming here -- the incremental
251 * update is pretty fast.
252 */
253 ip->ip_ttl -= IPTTLDEC;
254 if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
255 ip->ip_sum -= ~htons(IPTTLDEC << 8);
256 else
257 ip->ip_sum += htons(IPTTLDEC << 8);
258
259 /*
260 * Done modifying the header; copy it back, if necessary.
261 *
262 * XXX Use m_copyback_cow(9) here? --dyoung
263 */
264 if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0)
265 memcpy(mtod(m, void *), &ip_store, sizeof(ip_store));
266
267 /*
268 * Trim the packet in case it's too long..
269 */
270 if (m->m_pkthdr.len > iplen) {
271 if (m->m_len == m->m_pkthdr.len) {
272 m->m_len = iplen;
273 m->m_pkthdr.len = iplen;
274 } else
275 m_adj(m, iplen - m->m_pkthdr.len);
276 }
277
278 /*
279 * Send the packet on it's way. All we can get back is ENOBUFS
280 */
281 ipf->ipf_uses++;
282 PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
283
284 if (rt->rt_flags & RTF_GATEWAY)
285 dst = rt->rt_gateway;
286 else
287 dst = rtcache_getdst(&ipf->ipf_ro);
288
289 KERNEL_LOCK(1, NULL);
290 if ((error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m, dst, rt)) != 0) {
291 if (error == ENOBUFS)
292 ipf->ipf_dropped++;
293 else
294 ipf->ipf_errors++;
295 }
296 KERNEL_UNLOCK_ONE(NULL);
297 return 1;
298 }
299
300 static void
302 ipflow_addstats(struct ipflow *ipf)
303 {
304 struct rtentry *rt;
305 uint64_t *ips;
306
307 if ((rt = rtcache_validate(&ipf->ipf_ro)) != NULL)
308 rt->rt_use += ipf->ipf_uses;
309
310 ips = IP_STAT_GETREF();
311 ips[IP_STAT_CANTFORWARD] += ipf->ipf_errors + ipf->ipf_dropped;
312 ips[IP_STAT_TOTAL] += ipf->ipf_uses;
313 ips[IP_STAT_FORWARD] += ipf->ipf_uses;
314 ips[IP_STAT_FASTFORWARD] += ipf->ipf_uses;
315 IP_STAT_PUTREF();
316 }
317
318 static void
319 ipflow_free(struct ipflow *ipf)
320 {
321 int s;
322 /*
323 * Remove the flow from the hash table (at elevated IPL).
324 * Once it's off the list, we can deal with it at normal
325 * network IPL.
326 */
327 s = splnet();
328 IPFLOW_REMOVE(ipf);
329 splx(s);
330 ipflow_addstats(ipf);
331 rtcache_free(&ipf->ipf_ro);
332 ipflow_inuse--;
333 s = splnet();
334 pool_put(&ipflow_pool, ipf);
335 splx(s);
336 }
337
338 struct ipflow *
339 ipflow_reap(bool just_one)
340 {
341 while (just_one || ipflow_inuse > ip_maxflows) {
342 struct ipflow *ipf, *maybe_ipf = NULL;
343 int s;
344
345 ipf = LIST_FIRST(&ipflowlist);
346 while (ipf != NULL) {
347 /*
348 * If this no longer points to a valid route
349 * reclaim it.
350 */
351 if (rtcache_validate(&ipf->ipf_ro) == NULL)
352 goto done;
353 /*
354 * choose the one that's been least recently
355 * used or has had the least uses in the
356 * last 1.5 intervals.
357 */
358 if (maybe_ipf == NULL ||
359 ipf->ipf_timer < maybe_ipf->ipf_timer ||
360 (ipf->ipf_timer == maybe_ipf->ipf_timer &&
361 ipf->ipf_last_uses + ipf->ipf_uses <
362 maybe_ipf->ipf_last_uses +
363 maybe_ipf->ipf_uses))
364 maybe_ipf = ipf;
365 ipf = LIST_NEXT(ipf, ipf_list);
366 }
367 ipf = maybe_ipf;
368 done:
369 /*
370 * Remove the entry from the flow table.
371 */
372 s = splnet();
373 IPFLOW_REMOVE(ipf);
374 splx(s);
375 ipflow_addstats(ipf);
376 rtcache_free(&ipf->ipf_ro);
377 if (just_one)
378 return ipf;
379 pool_put(&ipflow_pool, ipf);
380 ipflow_inuse--;
381 }
382 return NULL;
383 }
384
385 void
386 ipflow_slowtimo(void)
387 {
388 struct rtentry *rt;
389 struct ipflow *ipf, *next_ipf;
390 uint64_t *ips;
391
392 mutex_enter(softnet_lock);
393 KERNEL_LOCK(1, NULL);
394 for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
395 next_ipf = LIST_NEXT(ipf, ipf_list);
396 if (PRT_SLOW_ISEXPIRED(ipf->ipf_timer) ||
397 (rt = rtcache_validate(&ipf->ipf_ro)) == NULL) {
398 ipflow_free(ipf);
399 } else {
400 ipf->ipf_last_uses = ipf->ipf_uses;
401 rt->rt_use += ipf->ipf_uses;
402 ips = IP_STAT_GETREF();
403 ips[IP_STAT_TOTAL] += ipf->ipf_uses;
404 ips[IP_STAT_FORWARD] += ipf->ipf_uses;
405 ips[IP_STAT_FASTFORWARD] += ipf->ipf_uses;
406 IP_STAT_PUTREF();
407 ipf->ipf_uses = 0;
408 }
409 }
410 KERNEL_UNLOCK_ONE(NULL);
411 mutex_exit(softnet_lock);
412 }
413
414 void
415 ipflow_create(const struct route *ro, struct mbuf *m)
416 {
417 const struct ip *const ip = mtod(m, const struct ip *);
418 struct ipflow *ipf;
419 size_t hash;
420 int s;
421
422 /*
423 * Don't create cache entries for ICMP messages.
424 */
425 if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP)
426 return;
427 /*
428 * See if an existing flow struct exists. If so remove it from it's
429 * list and free the old route. If not, try to malloc a new one
430 * (if we aren't at our limit).
431 */
432 ipf = ipflow_lookup(ip);
433 if (ipf == NULL) {
434 if (ipflow_inuse >= ip_maxflows) {
435 ipf = ipflow_reap(true);
436 } else {
437 s = splnet();
438 ipf = pool_get(&ipflow_pool, PR_NOWAIT);
439 splx(s);
440 if (ipf == NULL)
441 return;
442 ipflow_inuse++;
443 }
444 memset(ipf, 0, sizeof(*ipf));
445 } else {
446 s = splnet();
447 IPFLOW_REMOVE(ipf);
448 splx(s);
449 ipflow_addstats(ipf);
450 rtcache_free(&ipf->ipf_ro);
451 ipf->ipf_uses = ipf->ipf_last_uses = 0;
452 ipf->ipf_errors = ipf->ipf_dropped = 0;
453 }
454
455 /*
456 * Fill in the updated information.
457 */
458 rtcache_copy(&ipf->ipf_ro, ro);
459 ipf->ipf_dst = ip->ip_dst;
460 ipf->ipf_src = ip->ip_src;
461 ipf->ipf_tos = ip->ip_tos;
462 PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
463
464 /*
465 * Insert into the approriate bucket of the flow table.
466 */
467 hash = ipflow_hash(ip);
468 s = splnet();
469 IPFLOW_INSERT(&ipflowtable[hash], ipf);
470 splx(s);
471 }
472
473 int
474 ipflow_invalidate_all(int new_size)
475 {
476 struct ipflow *ipf, *next_ipf;
477 int s, error;
478
479 error = 0;
480 s = splnet();
481 for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
482 next_ipf = LIST_NEXT(ipf, ipf_list);
483 ipflow_free(ipf);
484 }
485
486 if (new_size)
487 error = ipflow_init(new_size);
488 splx(s);
489
490 return error;
491 }
492