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