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