xref: /netbsd-src/sys/netinet6/ip6_flow.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: ip6_flow.c,v 1.9 2007/08/20 19:42:34 dyoung Exp $	*/
2 
3 /*-
4  * Copyright (c) 2007 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 Liam J. Foy
9  * <liamjfoy@netbsd.org> and Matt Thomas <matt@netbsd.org>.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  *
39  * IPv6 version was developed by Liam J. Foy. Original source existed in IPv4
40  * format developed by Matt Thomas. Thanks to Joerg Sonnenberger, Matt
41  * Thomas and Christos Zoulas.
42  *
43  * Thanks to Liverpool John Moores University, especially Dr. David Llewellyn-Jones
44  * for providing resources (to test) and Professor Madjid Merabti.
45  */
46 
47 #include <sys/cdefs.h>
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/domain.h>
54 #include <sys/protosw.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/time.h>
58 #include <sys/kernel.h>
59 #include <sys/pool.h>
60 #include <sys/sysctl.h>
61 
62 #include <net/if.h>
63 #include <net/if_dl.h>
64 #include <net/route.h>
65 #include <net/pfil.h>
66 
67 #include <netinet/in.h>
68 #include <netinet6/in6_var.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip6.h>
71 #include <netinet6/ip6_var.h>
72 
73 /*
74  * IPv6 Fast Forward caches/hashes flows from one source to destination.
75  *
76  * Upon a successful forward IPv6FF caches and hashes details such as the
77  * route, source and destination. Once another packet is received matching
78  * the source and destination the packet is forwarded straight onto if_output
79  * using the cached details.
80  *
81  * Example:
82  * ether/fddi_input -> ip6flow_fastfoward -> if_output
83  */
84 
85 POOL_INIT(ip6flow_pool, sizeof(struct ip6flow), 0, 0, 0, "ip6flowpl", NULL,
86     IPL_NET);
87 
88 LIST_HEAD(ip6flowhead, ip6flow);
89 
90 /*
91  * We could use IPv4 defines (IPFLOW_HASHBITS) but we'll
92  * use our own (possibly for future expansion).
93  */
94 #define	IP6FLOW_TIMER		(5 * PR_SLOWHZ)
95 #define	IP6FLOW_DEFAULT_HASHSIZE	(1 << IP6FLOW_HASHBITS)
96 
97 static struct ip6flowhead *ip6flowtable = NULL;
98 static struct ip6flowhead ip6flowlist;
99 static int ip6flow_inuse;
100 
101 /*
102  * Insert an ip6flow into the list.
103  */
104 #define	IP6FLOW_INSERT(bucket, ip6f) \
105 do { \
106 	LIST_INSERT_HEAD((bucket), (ip6f), ip6f_hash); \
107 	LIST_INSERT_HEAD(&ip6flowlist, (ip6f), ip6f_list); \
108 } while (/*CONSTCOND*/ 0)
109 
110 /*
111  * Remove an ip6flow from the list.
112  */
113 #define	IP6FLOW_REMOVE(ip6f) \
114 do { \
115 	LIST_REMOVE((ip6f), ip6f_hash); \
116 	LIST_REMOVE((ip6f), ip6f_list); \
117 } while (/*CONSTCOND*/ 0)
118 
119 #ifndef IP6FLOW_DEFAULT
120 #define	IP6FLOW_DEFAULT		256
121 #endif
122 
123 int ip6_maxflows = IP6FLOW_DEFAULT;
124 int ip6_hashsize = IP6FLOW_DEFAULT_HASHSIZE;
125 
126 /*
127  * Calculate hash table position.
128  */
129 static size_t
130 ip6flow_hash(struct ip6_hdr *ip6)
131 {
132 	size_t hash;
133 	uint32_t dst_sum, src_sum;
134 	size_t idx;
135 
136 	src_sum = ip6->ip6_src.s6_addr32[0] + ip6->ip6_src.s6_addr32[1]
137 	    + ip6->ip6_src.s6_addr32[2] + ip6->ip6_src.s6_addr32[3];
138 	dst_sum = ip6->ip6_dst.s6_addr32[0] + ip6->ip6_dst.s6_addr32[1]
139 	    + ip6->ip6_dst.s6_addr32[2] + ip6->ip6_dst.s6_addr32[3];
140 
141 	hash = ip6->ip6_flow;
142 
143 	for (idx = 0; idx < 32; idx += IP6FLOW_HASHBITS)
144 		hash += (dst_sum >> (32 - idx)) + (src_sum >> idx);
145 
146 	return hash & (ip6_hashsize-1);
147 }
148 
149 /*
150  * Check to see if a flow already exists - if so return it.
151  */
152 static struct ip6flow *
153 ip6flow_lookup(struct ip6_hdr *ip6)
154 {
155 	size_t hash;
156 	struct ip6flow *ip6f;
157 
158 	hash = ip6flow_hash(ip6);
159 
160 	LIST_FOREACH(ip6f, &ip6flowtable[hash], ip6f_hash) {
161 		if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6f->ip6f_dst)
162 		    && IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6f->ip6f_src)
163 		    && ip6f->ip6f_flow == ip6->ip6_flow) {
164 		    	/* A cached flow has been found. */
165 			return ip6f;
166 		}
167 	}
168 
169 	return NULL;
170 }
171 
172 /*
173  * Allocate memory and initialise lists. This function is called
174  * from ip6_init and called there after to resize the hash table.
175  * If a newly sized table cannot be malloc'ed we just continue
176  * to use the old one.
177  */
178 int
179 ip6flow_init(int table_size)
180 {
181 	struct ip6flowhead *new_table;
182 	size_t i;
183 
184 	new_table = (struct ip6flowhead *)malloc(sizeof(struct ip6flowhead) *
185 	    table_size, M_RTABLE, M_NOWAIT);
186 
187 	if (new_table == NULL)
188 		return 1;
189 
190 	if (ip6flowtable != NULL)
191 		free(ip6flowtable, M_RTABLE);
192 
193 	ip6flowtable = new_table;
194 	ip6_hashsize = table_size;
195 
196 	LIST_INIT(&ip6flowlist);
197 	for (i = 0; i < ip6_hashsize; i++)
198 		LIST_INIT(&ip6flowtable[i]);
199 
200 	return 0;
201 }
202 
203 /*
204  * IPv6 Fast Forward routine. Attempt to forward the packet -
205  * if any problems are found return to the main IPv6 input
206  * routine to deal with.
207  */
208 int
209 ip6flow_fastforward(struct mbuf *m)
210 {
211 	struct ip6flow *ip6f;
212 	struct ip6_hdr *ip6;
213 	struct rtentry *rt;
214 	const struct sockaddr *dst;
215 	int error;
216 
217 	/*
218 	 * Are we forwarding packets and have flows?
219 	 */
220 	if (!ip6_forwarding || ip6flow_inuse == 0)
221 		return 0;
222 
223 	/*
224 	 * At least size of IPv6 Header?
225 	 */
226 	if (m->m_len < sizeof(struct ip6_hdr))
227 		return 0;
228 	/*
229 	 * Was packet received as a link-level multicast or broadcast?
230 	 * If so, don't try to fast forward.
231 	 */
232 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0)
233 		return 0;
234 
235 	if (IP6_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
236 		if ((m = m_copyup(m, sizeof(struct ip6_hdr),
237 				(max_linkhdr + 3) & ~3)) == NULL) {
238 			return 0;
239 		}
240 	} else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
241 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
242 			return 0;
243 		}
244 	}
245 
246 	ip6 = mtod(m, struct ip6_hdr *);
247 
248 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
249 		/* Bad version. */
250 		return 0;
251 	}
252 
253 	/*
254 	 * If we have a hop-by-hop extension we must process it.
255 	 * We just leave this up to ip6_input to deal with.
256 	 */
257 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS)
258 		return 0;
259 
260 	/*
261 	 * Attempt to find a flow.
262 	 */
263 	if ((ip6f = ip6flow_lookup(ip6)) == NULL) {
264 		/* No flow found. */
265 		return 0;
266 	}
267 
268 	/*
269 	 * Route and interface still up?
270 	 */
271 	if (rtcache_down(&ip6f->ip6f_ro) ||
272 	    (rt = ip6f->ip6f_ro.ro_rt) == NULL ||
273 	    (rt->rt_ifp->if_flags & IFF_UP) == 0) {
274 	    	/* Route or interface is down */
275 		return 0;
276 	}
277 
278 	/*
279 	 * Packet size greater than MTU?
280 	 */
281 	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
282 		/* Return to main IPv6 input function. */
283 		return 0;
284 	}
285 
286 	if (ip6->ip6_hlim <= IPV6_HLIMDEC)
287 		return 0;
288 
289 	/* Decrement hop limit (same as TTL) */
290 	ip6->ip6_hlim -= IPV6_HLIMDEC;
291 
292 	if (rt->rt_flags & RTF_GATEWAY)
293 		dst = rt->rt_gateway;
294 	else
295 		dst = rtcache_getdst(&ip6f->ip6f_ro);
296 
297 	PRT_SLOW_ARM(ip6f->ip6f_timer, IP6FLOW_TIMER);
298 
299 	ip6f->ip6f_uses++;
300 
301 	/* Send on its way - straight to the interface output routine. */
302 	if ((error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m, dst, rt)) != 0) {
303 		ip6f->ip6f_dropped++;
304 	} else {
305 		ip6f->ip6f_forwarded++;
306 	}
307 
308 	return 1;
309 }
310 
311 /*
312  * Add the IPv6 flow statistics to the main IPv6 statistics.
313  */
314 static void
315 ip6flow_addstats(struct ip6flow *ip6f)
316 {
317 	if (!rtcache_down(&ip6f->ip6f_ro) && ip6f->ip6f_ro.ro_rt != NULL)
318 		ip6f->ip6f_ro.ro_rt->rt_use += ip6f->ip6f_uses;
319 	ip6stat.ip6s_fastforwardflows = ip6flow_inuse;
320 	ip6stat.ip6s_cantforward += ip6f->ip6f_dropped;
321 	ip6stat.ip6s_odropped += ip6f->ip6f_dropped;
322 	ip6stat.ip6s_total += ip6f->ip6f_uses;
323 	ip6stat.ip6s_forward += ip6f->ip6f_forwarded;
324 	ip6stat.ip6s_fastforward += ip6f->ip6f_forwarded;
325 }
326 
327 /*
328  * Add statistics and free the flow.
329  */
330 static void
331 ip6flow_free(struct ip6flow *ip6f)
332 {
333 	int s;
334 
335 	/*
336 	 * Remove the flow from the hash table (at elevated IPL).
337 	 * Once it's off the list, we can deal with it at normal
338 	 * network IPL.
339 	 */
340 	s = splnet();
341 	IP6FLOW_REMOVE(ip6f);
342 	splx(s);
343 	ip6flow_inuse--;
344 	ip6flow_addstats(ip6f);
345 	rtcache_free(&ip6f->ip6f_ro);
346 	pool_put(&ip6flow_pool, ip6f);
347 }
348 
349 /*
350  * Reap one or more flows - ip6flow_reap may remove
351  * multiple flows if net.inet6.ip6.maxflows is reduced.
352  */
353 struct ip6flow *
354 ip6flow_reap(int just_one)
355 {
356 	while (just_one || ip6flow_inuse > ip6_maxflows) {
357 		struct ip6flow *ip6f, *maybe_ip6f = NULL;
358 		int s;
359 
360 		ip6f = LIST_FIRST(&ip6flowlist);
361 		while (ip6f != NULL) {
362 			/*
363 			 * If this no longer points to a valid route -
364 			 * reclaim it.
365 			 */
366 			if (rtcache_down(&ip6f->ip6f_ro) ||
367 			    ip6f->ip6f_ro.ro_rt == NULL)
368 				goto done;
369 			/*
370 			 * choose the one that's been least recently
371 			 * used or has had the least uses in the
372 			 * last 1.5 intervals.
373 			 */
374 			if (maybe_ip6f == NULL ||
375 			    ip6f->ip6f_timer < maybe_ip6f->ip6f_timer ||
376 			    (ip6f->ip6f_timer == maybe_ip6f->ip6f_timer &&
377 			     ip6f->ip6f_last_uses + ip6f->ip6f_uses <
378 			         maybe_ip6f->ip6f_last_uses +
379 			         maybe_ip6f->ip6f_uses))
380 				maybe_ip6f = ip6f;
381 			ip6f = LIST_NEXT(ip6f, ip6f_list);
382 		}
383 		ip6f = maybe_ip6f;
384 	    done:
385 		/*
386 		 * Remove the entry from the flow table
387 		 */
388 		s = splnet();
389 		IP6FLOW_REMOVE(ip6f);
390 		splx(s);
391 		rtcache_free(&ip6f->ip6f_ro);
392 		if (just_one) {
393 			ip6flow_addstats(ip6f);
394 			return ip6f;
395 		}
396 		ip6flow_inuse--;
397 		ip6flow_addstats(ip6f);
398 		pool_put(&ip6flow_pool, ip6f);
399 	}
400 	return NULL;
401 }
402 
403 void
404 ip6flow_slowtimo(void)
405 {
406 	struct ip6flow *ip6f, *next_ip6f;
407 
408 	for (ip6f = LIST_FIRST(&ip6flowlist); ip6f != NULL; ip6f = next_ip6f) {
409 		next_ip6f = LIST_NEXT(ip6f, ip6f_list);
410 		if (PRT_SLOW_ISEXPIRED(ip6f->ip6f_timer) ||
411 		    rtcache_down(&ip6f->ip6f_ro) ||
412 		    ip6f->ip6f_ro.ro_rt == NULL) {
413 			ip6flow_free(ip6f);
414 		} else {
415 			ip6f->ip6f_last_uses = ip6f->ip6f_uses;
416 			ip6flow_addstats(ip6f);
417 			ip6f->ip6f_uses = 0;
418 			ip6f->ip6f_dropped = 0;
419 			ip6f->ip6f_forwarded = 0;
420 		}
421 	}
422 }
423 
424 /*
425  * We have successfully forwarded a packet using the normal
426  * IPv6 stack. Now create/update a flow.
427  */
428 void
429 ip6flow_create(const struct route *ro, struct mbuf *m)
430 {
431 	struct ip6_hdr *ip6;
432 	struct ip6flow *ip6f;
433 	size_t hash;
434 	int s;
435 
436 	ip6 = mtod(m, struct ip6_hdr *);
437 
438 	/*
439 	 * If IPv6 Fast Forward is disabled, don't create a flow.
440 	 * It can be disabled by setting net.inet6.ip6.maxflows to 0.
441 	 *
442 	 * Don't create a flow for ICMPv6 messages.
443 	 */
444 	if (ip6_maxflows == 0 || ip6->ip6_nxt == IPPROTO_IPV6_ICMP)
445 		return;
446 
447 	/*
448 	 * See if an existing flow exists.  If so:
449 	 *	- Remove the flow
450 	 *	- Add flow statistics
451 	 *	- Free the route
452 	 *	- Reset statistics
453 	 *
454 	 * If a flow doesn't exist allocate a new one if
455 	 * ip6_maxflows hasn't reached its limit. If it has
456 	 * been reached, reap some flows.
457 	 */
458 	ip6f = ip6flow_lookup(ip6);
459 	if (ip6f == NULL) {
460 		if (ip6flow_inuse >= ip6_maxflows) {
461 			ip6f = ip6flow_reap(1);
462 		} else {
463 			ip6f = pool_get(&ip6flow_pool, PR_NOWAIT);
464 			if (ip6f == NULL)
465 				return;
466 			ip6flow_inuse++;
467 		}
468 		memset(ip6f, 0, sizeof(*ip6f));
469 	} else {
470 		s = splnet();
471 		IP6FLOW_REMOVE(ip6f);
472 		splx(s);
473 		ip6flow_addstats(ip6f);
474 		rtcache_free(&ip6f->ip6f_ro);
475 		ip6f->ip6f_uses = 0;
476 		ip6f->ip6f_last_uses = 0;
477 		ip6f->ip6f_dropped = 0;
478 		ip6f->ip6f_forwarded = 0;
479 	}
480 
481 	/*
482 	 * Fill in the updated/new details.
483 	 */
484 	rtcache_copy(&ip6f->ip6f_ro, ro);
485 	ip6f->ip6f_dst = ip6->ip6_dst;
486 	ip6f->ip6f_src = ip6->ip6_src;
487 	ip6f->ip6f_flow = ip6->ip6_flow;
488 	PRT_SLOW_ARM(ip6f->ip6f_timer, IP6FLOW_TIMER);
489 	ip6f->ip6f_start = time_uptime;
490 
491 	/*
492 	 * Insert into the approriate bucket of the flow table.
493 	 */
494 	hash = ip6flow_hash(ip6);
495 	s = splnet();
496 	IP6FLOW_INSERT(&ip6flowtable[hash], ip6f);
497 	splx(s);
498 }
499 
500 /*
501  * Invalidate/remove all flows - if new_size is positive we
502  * resize the hash table.
503  */
504 int
505 ip6flow_invalidate_all(int new_size)
506 {
507 	struct ip6flow *ip6f, *next_ip6f;
508 	int s, error;
509 
510 	error = 0;
511 	s = splnet();
512 	for (ip6f = LIST_FIRST(&ip6flowlist); ip6f != NULL; ip6f = next_ip6f) {
513 		next_ip6f = LIST_NEXT(ip6f, ip6f_list);
514 		ip6flow_free(ip6f);
515 	}
516 
517 	if (new_size)
518 		error = ip6flow_init(new_size);
519 	splx(s);
520 
521 	return error;
522 }
523