Home | History | Annotate | Line # | Download | only in netinet6
ip6_flow.c revision 1.28.2.3
      1 /*	$NetBSD: ip6_flow.c,v 1.28.2.3 2017/01/07 08:56:51 pgoyette 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.28.2.3 2017/01/07 08:56:51 pgoyette Exp $");
     42 
     43 #ifdef _KERNEL_OPT
     44 #include "opt_net_mpsafe.h"
     45 #endif
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/malloc.h>
     50 #include <sys/mbuf.h>
     51 #include <sys/domain.h>
     52 #include <sys/protosw.h>
     53 #include <sys/socket.h>
     54 #include <sys/socketvar.h>
     55 #include <sys/time.h>
     56 #include <sys/kernel.h>
     57 #include <sys/pool.h>
     58 #include <sys/sysctl.h>
     59 #include <sys/workqueue.h>
     60 #include <sys/atomic.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 #include <netinet6/ip6_private.h>
     73 
     74 /*
     75  * IPv6 Fast Forward caches/hashes flows from one source to destination.
     76  *
     77  * Upon a successful forward IPv6FF caches and hashes details such as the
     78  * route, source and destination. Once another packet is received matching
     79  * the source and destination the packet is forwarded straight onto if_output
     80  * using the cached details.
     81  *
     82  * Example:
     83  * ether/fddi_input -> ip6flow_fastforward -> if_output
     84  */
     85 
     86 static struct pool ip6flow_pool;
     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 /*
     98  * ip6_flow.c internal lock.
     99  * If we use softnet_lock, it would cause recursive lock.
    100  *
    101  * This is a tentative workaround.
    102  * We should make it scalable somehow in the future.
    103  */
    104 static kmutex_t ip6flow_lock;
    105 static struct ip6flowhead *ip6flowtable = NULL;
    106 static struct ip6flowhead ip6flowlist;
    107 static int ip6flow_inuse;
    108 
    109 static void ip6flow_slowtimo_work(struct work *, void *);
    110 static struct workqueue	*ip6flow_slowtimo_wq;
    111 static struct work	ip6flow_slowtimo_wk;
    112 
    113 static int sysctl_net_inet6_ip6_hashsize(SYSCTLFN_PROTO);
    114 static int sysctl_net_inet6_ip6_maxflows(SYSCTLFN_PROTO);
    115 static void ip6flow_sysctl_init(struct sysctllog **);
    116 
    117 /*
    118  * Insert an ip6flow into the list.
    119  */
    120 #define	IP6FLOW_INSERT(bucket, ip6f) \
    121 do { \
    122 	LIST_INSERT_HEAD((bucket), (ip6f), ip6f_hash); \
    123 	LIST_INSERT_HEAD(&ip6flowlist, (ip6f), ip6f_list); \
    124 } while (/*CONSTCOND*/ 0)
    125 
    126 /*
    127  * Remove an ip6flow from the list.
    128  */
    129 #define	IP6FLOW_REMOVE(ip6f) \
    130 do { \
    131 	LIST_REMOVE((ip6f), ip6f_hash); \
    132 	LIST_REMOVE((ip6f), ip6f_list); \
    133 } while (/*CONSTCOND*/ 0)
    134 
    135 #ifndef IP6FLOW_DEFAULT
    136 #define	IP6FLOW_DEFAULT		256
    137 #endif
    138 
    139 int ip6_maxflows = IP6FLOW_DEFAULT;
    140 int ip6_hashsize = IP6FLOW_DEFAULT_HASHSIZE;
    141 
    142 /*
    143  * Calculate hash table position.
    144  */
    145 static size_t
    146 ip6flow_hash(const struct ip6_hdr *ip6)
    147 {
    148 	size_t hash;
    149 	uint32_t dst_sum, src_sum;
    150 	size_t idx;
    151 
    152 	src_sum = ip6->ip6_src.s6_addr32[0] + ip6->ip6_src.s6_addr32[1]
    153 	    + ip6->ip6_src.s6_addr32[2] + ip6->ip6_src.s6_addr32[3];
    154 	dst_sum = ip6->ip6_dst.s6_addr32[0] + ip6->ip6_dst.s6_addr32[1]
    155 	    + ip6->ip6_dst.s6_addr32[2] + ip6->ip6_dst.s6_addr32[3];
    156 
    157 	hash = ip6->ip6_flow;
    158 
    159 	for (idx = 0; idx < 32; idx += IP6FLOW_HASHBITS)
    160 		hash += (dst_sum >> (32 - idx)) + (src_sum >> idx);
    161 
    162 	return hash & (ip6_hashsize-1);
    163 }
    164 
    165 /*
    166  * Check to see if a flow already exists - if so return it.
    167  */
    168 static struct ip6flow *
    169 ip6flow_lookup(const struct ip6_hdr *ip6)
    170 {
    171 	size_t hash;
    172 	struct ip6flow *ip6f;
    173 
    174 	KASSERT(mutex_owned(&ip6flow_lock));
    175 
    176 	hash = ip6flow_hash(ip6);
    177 
    178 	LIST_FOREACH(ip6f, &ip6flowtable[hash], ip6f_hash) {
    179 		if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6f->ip6f_dst)
    180 		    && IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6f->ip6f_src)
    181 		    && ip6f->ip6f_flow == ip6->ip6_flow) {
    182 		    	/* A cached flow has been found. */
    183 			return ip6f;
    184 		}
    185 	}
    186 
    187 	return NULL;
    188 }
    189 
    190 void
    191 ip6flow_poolinit(void)
    192 {
    193 
    194 	pool_init(&ip6flow_pool, sizeof(struct ip6flow), 0, 0, 0, "ip6flowpl",
    195 			NULL, IPL_NET);
    196 }
    197 
    198 /*
    199  * Allocate memory and initialise lists. This function is called
    200  * from ip6_init and called there after to resize the hash table.
    201  * If a newly sized table cannot be malloc'ed we just continue
    202  * to use the old one.
    203  */
    204 static int
    205 ip6flow_init_locked(int table_size)
    206 {
    207 	struct ip6flowhead *new_table;
    208 	size_t i;
    209 
    210 	KASSERT(mutex_owned(&ip6flow_lock));
    211 
    212 	new_table = (struct ip6flowhead *)malloc(sizeof(struct ip6flowhead) *
    213 	    table_size, M_RTABLE, M_NOWAIT);
    214 
    215 	if (new_table == NULL)
    216 		return 1;
    217 
    218 	if (ip6flowtable != NULL)
    219 		free(ip6flowtable, M_RTABLE);
    220 
    221 	ip6flowtable = new_table;
    222 	ip6_hashsize = table_size;
    223 
    224 	LIST_INIT(&ip6flowlist);
    225 	for (i = 0; i < ip6_hashsize; i++)
    226 		LIST_INIT(&ip6flowtable[i]);
    227 
    228 	return 0;
    229 }
    230 
    231 int
    232 ip6flow_init(int table_size)
    233 {
    234 	int ret, error;
    235 
    236 	error = workqueue_create(&ip6flow_slowtimo_wq, "ip6flow_slowtimo",
    237 	    ip6flow_slowtimo_work, NULL, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
    238 	if (error != 0)
    239 		panic("%s: workqueue_create failed (%d)\n", __func__, error);
    240 
    241 	mutex_init(&ip6flow_lock, MUTEX_DEFAULT, IPL_NONE);
    242 
    243 	mutex_enter(&ip6flow_lock);
    244 	ret = ip6flow_init_locked(table_size);
    245 	mutex_exit(&ip6flow_lock);
    246 	ip6flow_sysctl_init(NULL);
    247 
    248 	return ret;
    249 }
    250 
    251 /*
    252  * IPv6 Fast Forward routine. Attempt to forward the packet -
    253  * if any problems are found return to the main IPv6 input
    254  * routine to deal with.
    255  */
    256 int
    257 ip6flow_fastforward(struct mbuf **mp)
    258 {
    259 	struct ip6flow *ip6f;
    260 	struct ip6_hdr *ip6;
    261 	struct rtentry *rt = NULL;
    262 	struct mbuf *m;
    263 	const struct sockaddr *dst;
    264 	int error;
    265 	int ret = 0;
    266 
    267 	mutex_enter(&ip6flow_lock);
    268 
    269 	/*
    270 	 * Are we forwarding packets and have flows?
    271 	 */
    272 	if (!ip6_forwarding || ip6flow_inuse == 0)
    273 		goto out;
    274 
    275 	m = *mp;
    276 	/*
    277 	 * At least size of IPv6 Header?
    278 	 */
    279 	if (m->m_len < sizeof(struct ip6_hdr))
    280 		goto out;
    281 	/*
    282 	 * Was packet received as a link-level multicast or broadcast?
    283 	 * If so, don't try to fast forward.
    284 	 */
    285 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0)
    286 		goto out;
    287 
    288 	if (IP6_HDR_ALIGNED_P(mtod(m, const void *)) == 0) {
    289 		if ((m = m_copyup(m, sizeof(struct ip6_hdr),
    290 				(max_linkhdr + 3) & ~3)) == NULL) {
    291 			goto out;
    292 		}
    293 		*mp = m;
    294 	} else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
    295 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
    296 			goto out;
    297 		}
    298 		*mp = m;
    299 	}
    300 
    301 	ip6 = mtod(m, struct ip6_hdr *);
    302 
    303 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
    304 		/* Bad version. */
    305 		goto out;
    306 	}
    307 
    308 	/*
    309 	 * If we have a hop-by-hop extension we must process it.
    310 	 * We just leave this up to ip6_input to deal with.
    311 	 */
    312 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS)
    313 		goto out;
    314 
    315 	/*
    316 	 * Attempt to find a flow.
    317 	 */
    318 	if ((ip6f = ip6flow_lookup(ip6)) == NULL) {
    319 		/* No flow found. */
    320 		goto out;
    321 	}
    322 
    323 	/*
    324 	 * Route and interface still up?
    325 	 */
    326 	if ((rt = rtcache_validate(&ip6f->ip6f_ro)) == NULL ||
    327 	    (rt->rt_ifp->if_flags & IFF_UP) == 0 ||
    328 	    (rt->rt_flags & RTF_BLACKHOLE) != 0)
    329 		goto out_unref;
    330 
    331 	/*
    332 	 * Packet size greater than MTU?
    333 	 */
    334 	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
    335 		/* Return to main IPv6 input function. */
    336 		goto out_unref;
    337 	}
    338 
    339 	/*
    340 	 * Clear any in-bound checksum flags for this packet.
    341 	 */
    342 	m->m_pkthdr.csum_flags = 0;
    343 
    344 	if (ip6->ip6_hlim <= IPV6_HLIMDEC)
    345 		goto out_unref;
    346 
    347 	/* Decrement hop limit (same as TTL) */
    348 	ip6->ip6_hlim -= IPV6_HLIMDEC;
    349 
    350 	if (rt->rt_flags & RTF_GATEWAY)
    351 		dst = rt->rt_gateway;
    352 	else
    353 		dst = rtcache_getdst(&ip6f->ip6f_ro);
    354 
    355 	PRT_SLOW_ARM(ip6f->ip6f_timer, IP6FLOW_TIMER);
    356 
    357 	ip6f->ip6f_uses++;
    358 
    359 	/* Send on its way - straight to the interface output routine. */
    360 	if ((error = if_output_lock(rt->rt_ifp, rt->rt_ifp, m, dst, rt)) != 0) {
    361 		ip6f->ip6f_dropped++;
    362 	} else {
    363 		ip6f->ip6f_forwarded++;
    364 	}
    365 	ret = 1;
    366 out_unref:
    367 	rtcache_unref(rt, &ip6f->ip6f_ro);
    368 out:
    369 	mutex_exit(&ip6flow_lock);
    370 	return ret;
    371 }
    372 
    373 /*
    374  * Add the IPv6 flow statistics to the main IPv6 statistics.
    375  */
    376 static void
    377 ip6flow_addstats_rt(struct rtentry *rt, struct ip6flow *ip6f)
    378 {
    379 	uint64_t *ip6s;
    380 
    381 	if (rt != NULL)
    382 		rt->rt_use += ip6f->ip6f_uses;
    383 	ip6s = IP6_STAT_GETREF();
    384 	ip6s[IP6_STAT_FASTFORWARDFLOWS] = ip6flow_inuse;
    385 	ip6s[IP6_STAT_CANTFORWARD] += ip6f->ip6f_dropped;
    386 	ip6s[IP6_STAT_ODROPPED] += ip6f->ip6f_dropped;
    387 	ip6s[IP6_STAT_TOTAL] += ip6f->ip6f_uses;
    388 	ip6s[IP6_STAT_FORWARD] += ip6f->ip6f_forwarded;
    389 	ip6s[IP6_STAT_FASTFORWARD] += ip6f->ip6f_forwarded;
    390 	IP6_STAT_PUTREF();
    391 }
    392 
    393 static void
    394 ip6flow_addstats(struct ip6flow *ip6f)
    395 {
    396 	struct rtentry *rt;
    397 
    398 	rt = rtcache_validate(&ip6f->ip6f_ro);
    399 	ip6flow_addstats_rt(rt, ip6f);
    400 	rtcache_unref(rt, &ip6f->ip6f_ro);
    401 }
    402 
    403 /*
    404  * Add statistics and free the flow.
    405  */
    406 static void
    407 ip6flow_free(struct ip6flow *ip6f)
    408 {
    409 
    410 	KASSERT(mutex_owned(&ip6flow_lock));
    411 
    412 	/*
    413 	 * Remove the flow from the hash table (at elevated IPL).
    414 	 * Once it's off the list, we can deal with it at normal
    415 	 * network IPL.
    416 	 */
    417 	IP6FLOW_REMOVE(ip6f);
    418 
    419 	ip6flow_inuse--;
    420 	ip6flow_addstats(ip6f);
    421 	rtcache_free(&ip6f->ip6f_ro);
    422 	pool_put(&ip6flow_pool, ip6f);
    423 }
    424 
    425 static struct ip6flow *
    426 ip6flow_reap_locked(int just_one)
    427 {
    428 
    429 	KASSERT(mutex_owned(&ip6flow_lock));
    430 
    431 	while (just_one || ip6flow_inuse > ip6_maxflows) {
    432 		struct ip6flow *ip6f, *maybe_ip6f = NULL;
    433 
    434 	/*
    435 	 * This case is used in slow path(sysctl).
    436 	 * At first, remove invalid rtcache ip6flow, and then remove TAILQ_LAST
    437 	 * ip6flow if it is ensured least recently used by comparing last_uses.
    438 	 */
    439 	while (ip6flow_inuse > ip6_maxflows) {
    440 		struct ip6flow *maybe_ip6f = TAILQ_LAST(&ip6flowlist, ip6flowhead);
    441 
    442 		TAILQ_FOREACH(ip6f, &ip6flowlist, ip6f_list) {
    443 			struct rtentry *rt;
    444 			/*
    445 			 * If this no longer points to a valid route -
    446 			 * reclaim it.
    447 			 */
    448 			if ((rt = rtcache_validate(&ip6f->ip6f_ro)) == NULL)
    449 				goto done;
    450 			rtcache_unref(rt, &ip6f->ip6f_ro);
    451 			/*
    452 			 * choose the one that's been least recently
    453 			 * used or has had the least uses in the
    454 			 * last 1.5 intervals.
    455 			 */
    456 			if (maybe_ip6f == NULL ||
    457 			    ip6f->ip6f_timer < maybe_ip6f->ip6f_timer ||
    458 			    (ip6f->ip6f_timer == maybe_ip6f->ip6f_timer &&
    459 			     ip6f->ip6f_last_uses + ip6f->ip6f_uses <
    460 			         maybe_ip6f->ip6f_last_uses +
    461 			         maybe_ip6f->ip6f_uses))
    462 				maybe_ip6f = ip6f;
    463 			ip6f = LIST_NEXT(ip6f, ip6f_list);
    464 		}
    465 		ip6f = maybe_ip6f;
    466 	    done:
    467 		/*
    468 		 * Remove the entry from the flow table
    469 		 */
    470 		IP6FLOW_REMOVE(ip6f);
    471 
    472 		rtcache_free(&ip6f->ip6f_ro);
    473 		if (just_one) {
    474 			ip6flow_addstats(ip6f);
    475 			return ip6f;
    476 		}
    477 		ip6flow_inuse--;
    478 		ip6flow_addstats(ip6f);
    479 		pool_put(&ip6flow_pool, ip6f);
    480 	}
    481 	return NULL;
    482 }
    483 
    484 /*
    485  * Reap one or more flows - ip6flow_reap may remove
    486  * multiple flows if net.inet6.ip6.maxflows is reduced.
    487  */
    488 struct ip6flow *
    489 ip6flow_reap(int just_one)
    490 {
    491 	struct ip6flow *ip6f;
    492 
    493 	mutex_enter(&ip6flow_lock);
    494 	ip6f = ip6flow_reap_locked(just_one);
    495 	mutex_exit(&ip6flow_lock);
    496 	return ip6f;
    497 }
    498 
    499 static unsigned int ip6flow_work_enqueued = 0;
    500 
    501 void
    502 ip6flow_slowtimo_work(struct work *wk, void *arg)
    503 {
    504 	struct ip6flow *ip6f, *next_ip6f;
    505 
    506 	/* We can allow enqueuing another work at this point */
    507 	atomic_swap_uint(&ip6flow_work_enqueued, 0);
    508 
    509 #ifndef NET_MPSAFE
    510 	mutex_enter(softnet_lock);
    511 	KERNEL_LOCK(1, NULL);
    512 #endif
    513 	mutex_enter(&ip6flow_lock);
    514 
    515 	for (ip6f = TAILQ_FIRST(&ip6flowlist); ip6f != NULL; ip6f = next_ip6f) {
    516 		struct rtentry *rt = NULL;
    517 		next_ip6f = TAILQ_NEXT(ip6f, ip6f_list);
    518 		if (PRT_SLOW_ISEXPIRED(ip6f->ip6f_timer) ||
    519 		    (rt = rtcache_validate(&ip6f->ip6f_ro)) == NULL) {
    520 			ip6flow_free(ip6f);
    521 		} else {
    522 			ip6f->ip6f_last_uses = ip6f->ip6f_uses;
    523 			ip6flow_addstats_rt(rt, ip6f);
    524 			ip6f->ip6f_uses = 0;
    525 			ip6f->ip6f_dropped = 0;
    526 			ip6f->ip6f_forwarded = 0;
    527 		}
    528 		rtcache_unref(rt, &ip6f->ip6f_ro);
    529 	}
    530 
    531 	mutex_exit(&ip6flow_lock);
    532 #ifndef NET_MPSAFE
    533 	KERNEL_UNLOCK_ONE(NULL);
    534 	mutex_exit(softnet_lock);
    535 #endif
    536 }
    537 
    538 void
    539 ip6flow_slowtimo(void)
    540 {
    541 
    542 	/* Avoid enqueuing another work when one is already enqueued */
    543 	if (atomic_swap_uint(&ip6flow_work_enqueued, 1) == 1)
    544 		return;
    545 
    546 	workqueue_enqueue(ip6flow_slowtimo_wq, &ip6flow_slowtimo_wk, NULL);
    547 }
    548 
    549 /*
    550  * We have successfully forwarded a packet using the normal
    551  * IPv6 stack. Now create/update a flow.
    552  */
    553 void
    554 ip6flow_create(struct route *ro, struct mbuf *m)
    555 {
    556 	const struct ip6_hdr *ip6;
    557 	struct ip6flow *ip6f;
    558 	size_t hash;
    559 
    560 	ip6 = mtod(m, const struct ip6_hdr *);
    561 
    562 #ifndef NET_MPSAFE
    563 	KERNEL_LOCK(1, NULL);
    564 #endif
    565 	mutex_enter(&ip6flow_lock);
    566 
    567 	/*
    568 	 * If IPv6 Fast Forward is disabled, don't create a flow.
    569 	 * It can be disabled by setting net.inet6.ip6.maxflows to 0.
    570 	 *
    571 	 * Don't create a flow for ICMPv6 messages.
    572 	 */
    573 	if (ip6_maxflows == 0 || ip6->ip6_nxt == IPPROTO_IPV6_ICMP)
    574 		goto out;
    575 
    576 	/*
    577 	 * See if an existing flow exists.  If so:
    578 	 *	- Remove the flow
    579 	 *	- Add flow statistics
    580 	 *	- Free the route
    581 	 *	- Reset statistics
    582 	 *
    583 	 * If a flow doesn't exist allocate a new one if
    584 	 * ip6_maxflows hasn't reached its limit. If it has
    585 	 * been reached, reap some flows.
    586 	 */
    587 	ip6f = ip6flow_lookup(ip6);
    588 	if (ip6f == NULL) {
    589 		if (ip6flow_inuse >= ip6_maxflows) {
    590 			ip6f = ip6flow_reap_locked(1);
    591 		} else {
    592 			ip6f = pool_get(&ip6flow_pool, PR_NOWAIT);
    593 			if (ip6f == NULL)
    594 				goto out;
    595 			ip6flow_inuse++;
    596 		}
    597 		memset(ip6f, 0, sizeof(*ip6f));
    598 	} else {
    599 		IP6FLOW_REMOVE(ip6f);
    600 
    601 		ip6flow_addstats(ip6f);
    602 		rtcache_free(&ip6f->ip6f_ro);
    603 		ip6f->ip6f_uses = 0;
    604 		ip6f->ip6f_last_uses = 0;
    605 		ip6f->ip6f_dropped = 0;
    606 		ip6f->ip6f_forwarded = 0;
    607 	}
    608 
    609 	/*
    610 	 * Fill in the updated/new details.
    611 	 */
    612 	rtcache_copy(&ip6f->ip6f_ro, ro);
    613 	ip6f->ip6f_dst = ip6->ip6_dst;
    614 	ip6f->ip6f_src = ip6->ip6_src;
    615 	ip6f->ip6f_flow = ip6->ip6_flow;
    616 	PRT_SLOW_ARM(ip6f->ip6f_timer, IP6FLOW_TIMER);
    617 
    618 	/*
    619 	 * Insert into the approriate bucket of the flow table.
    620 	 */
    621 	hash = ip6flow_hash(ip6);
    622 	IP6FLOW_INSERT(&ip6flowtable[hash], ip6f);
    623 
    624  out:
    625 	mutex_exit(&ip6flow_lock);
    626 #ifndef NET_MPSAFE
    627 	KERNEL_UNLOCK_ONE(NULL);
    628 #endif
    629 }
    630 
    631 /*
    632  * Invalidate/remove all flows - if new_size is positive we
    633  * resize the hash table.
    634  */
    635 int
    636 ip6flow_invalidate_all(int new_size)
    637 {
    638 	struct ip6flow *ip6f, *next_ip6f;
    639 	int error;
    640 
    641 	error = 0;
    642 
    643 	mutex_enter(&ip6flow_lock);
    644 
    645 	for (ip6f = LIST_FIRST(&ip6flowlist); ip6f != NULL; ip6f = next_ip6f) {
    646 		next_ip6f = LIST_NEXT(ip6f, ip6f_list);
    647 		ip6flow_free(ip6f);
    648 	}
    649 
    650 	if (new_size)
    651 		error = ip6flow_init_locked(new_size);
    652 
    653 	mutex_exit(&ip6flow_lock);
    654 
    655 	return error;
    656 }
    657 
    658 /*
    659  * sysctl helper routine for net.inet.ip6.maxflows. Since
    660  * we could reduce this value, call ip6flow_reap();
    661  */
    662 static int
    663 sysctl_net_inet6_ip6_maxflows(SYSCTLFN_ARGS)
    664 {
    665 	int error;
    666 
    667 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
    668 	if (error || newp == NULL)
    669 		return (error);
    670 
    671 #ifndef NET_MPSAFE
    672 	mutex_enter(softnet_lock);
    673 	KERNEL_LOCK(1, NULL);
    674 #endif
    675 
    676 	ip6flow_reap(0);
    677 
    678 #ifndef NET_MPSAFE
    679 	KERNEL_UNLOCK_ONE(NULL);
    680 	mutex_exit(softnet_lock);
    681 #endif
    682 
    683 	return (0);
    684 }
    685 
    686 static int
    687 sysctl_net_inet6_ip6_hashsize(SYSCTLFN_ARGS)
    688 {
    689 	int error, tmp;
    690 	struct sysctlnode node;
    691 
    692 	node = *rnode;
    693 	tmp = ip6_hashsize;
    694 	node.sysctl_data = &tmp;
    695 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    696 	if (error || newp == NULL)
    697 		return (error);
    698 
    699 	if ((tmp & (tmp - 1)) == 0 && tmp != 0) {
    700 		/*
    701 		 * Can only fail due to malloc()
    702 		 */
    703 #ifndef NET_MPSAFE
    704 		mutex_enter(softnet_lock);
    705 		KERNEL_LOCK(1, NULL);
    706 #endif
    707 		error = ip6flow_invalidate_all(tmp);
    708 #ifndef NET_MPSAFE
    709 		KERNEL_UNLOCK_ONE(NULL);
    710 		mutex_exit(softnet_lock);
    711 #endif
    712 	} else {
    713 		/*
    714 		 * EINVAL if not a power of 2
    715 		 */
    716 		error = EINVAL;
    717 	}
    718 
    719 	return error;
    720 }
    721 
    722 static void
    723 ip6flow_sysctl_init(struct sysctllog **clog)
    724 {
    725 
    726 	sysctl_createv(clog, 0, NULL, NULL,
    727 		       CTLFLAG_PERMANENT,
    728 		       CTLTYPE_NODE, "inet6",
    729 		       SYSCTL_DESCR("PF_INET6 related settings"),
    730 		       NULL, 0, NULL, 0,
    731 		       CTL_NET, PF_INET6, CTL_EOL);
    732 	sysctl_createv(clog, 0, NULL, NULL,
    733 		       CTLFLAG_PERMANENT,
    734 		       CTLTYPE_NODE, "ip6",
    735 		       SYSCTL_DESCR("IPv6 related settings"),
    736 		       NULL, 0, NULL, 0,
    737 		       CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_EOL);
    738 
    739 	sysctl_createv(clog, 0, NULL, NULL,
    740 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    741 			CTLTYPE_INT, "maxflows",
    742 			SYSCTL_DESCR("Number of flows for fast forwarding (IPv6)"),
    743 			sysctl_net_inet6_ip6_maxflows, 0, &ip6_maxflows, 0,
    744 			CTL_NET, PF_INET6, IPPROTO_IPV6,
    745 			CTL_CREATE, CTL_EOL);
    746 	sysctl_createv(clog, 0, NULL, NULL,
    747 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    748 			CTLTYPE_INT, "hashsize",
    749 			SYSCTL_DESCR("Size of hash table for fast forwarding (IPv6)"),
    750 			sysctl_net_inet6_ip6_hashsize, 0, &ip6_hashsize, 0,
    751 			CTL_NET, PF_INET6, IPPROTO_IPV6,
    752 			CTL_CREATE, CTL_EOL);
    753 }
    754