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