Home | History | Annotate | Line # | Download | only in netinet
ip_flow.c revision 1.55.2.1
      1  1.55.2.1      yamt /*	$NetBSD: ip_flow.c,v 1.55.2.1 2008/05/16 02:25:41 yamt Exp $	*/
      2       1.1      matt 
      3       1.1      matt /*-
      4       1.1      matt  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5       1.1      matt  * All rights reserved.
      6       1.1      matt  *
      7       1.1      matt  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1      matt  * by the 3am Software Foundry ("3am").  It was developed by Matt Thomas.
      9       1.1      matt  *
     10       1.1      matt  * Redistribution and use in source and binary forms, with or without
     11       1.1      matt  * modification, are permitted provided that the following conditions
     12       1.1      matt  * are met:
     13       1.1      matt  * 1. Redistributions of source code must retain the above copyright
     14       1.1      matt  *    notice, this list of conditions and the following disclaimer.
     15       1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     17       1.1      matt  *    documentation and/or other materials provided with the distribution.
     18       1.1      matt  *
     19       1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1      matt  */
     31      1.22     lukem 
     32      1.22     lukem #include <sys/cdefs.h>
     33  1.55.2.1      yamt __KERNEL_RCSID(0, "$NetBSD: ip_flow.c,v 1.55.2.1 2008/05/16 02:25:41 yamt Exp $");
     34       1.1      matt 
     35       1.1      matt #include <sys/param.h>
     36       1.1      matt #include <sys/systm.h>
     37       1.1      matt #include <sys/malloc.h>
     38       1.1      matt #include <sys/mbuf.h>
     39       1.1      matt #include <sys/domain.h>
     40       1.1      matt #include <sys/protosw.h>
     41       1.1      matt #include <sys/socket.h>
     42       1.1      matt #include <sys/socketvar.h>
     43       1.1      matt #include <sys/errno.h>
     44       1.1      matt #include <sys/time.h>
     45       1.1      matt #include <sys/kernel.h>
     46       1.7   thorpej #include <sys/pool.h>
     47       1.1      matt #include <sys/sysctl.h>
     48       1.1      matt 
     49       1.1      matt #include <net/if.h>
     50       1.1      matt #include <net/if_dl.h>
     51       1.1      matt #include <net/route.h>
     52       1.1      matt #include <net/pfil.h>
     53       1.1      matt 
     54       1.1      matt #include <netinet/in.h>
     55       1.1      matt #include <netinet/in_systm.h>
     56       1.1      matt #include <netinet/ip.h>
     57       1.1      matt #include <netinet/in_pcb.h>
     58       1.1      matt #include <netinet/in_var.h>
     59       1.1      matt #include <netinet/ip_var.h>
     60      1.54   thorpej #include <netinet/ip_private.h>
     61       1.1      matt 
     62      1.44  liamjfoy /*
     63      1.44  liamjfoy  * Similar code is very well commented in netinet6/ip6_flow.c
     64      1.44  liamjfoy  */
     65      1.44  liamjfoy 
     66      1.53   thorpej struct ipflow {
     67      1.53   thorpej 	LIST_ENTRY(ipflow) ipf_list;	/* next in active list */
     68      1.53   thorpej 	LIST_ENTRY(ipflow) ipf_hash;	/* next ipflow in bucket */
     69      1.53   thorpej 	struct in_addr ipf_dst;		/* destination address */
     70      1.53   thorpej 	struct in_addr ipf_src;		/* source address */
     71      1.53   thorpej 	uint8_t ipf_tos;		/* type-of-service */
     72      1.53   thorpej 	struct route ipf_ro;		/* associated route entry */
     73      1.53   thorpej 	u_long ipf_uses;		/* number of uses in this period */
     74      1.53   thorpej 	u_long ipf_last_uses;		/* number of uses in last period */
     75      1.53   thorpej 	u_long ipf_dropped;		/* ENOBUFS retured by if_output */
     76      1.53   thorpej 	u_long ipf_errors;		/* other errors returned by if_output */
     77      1.53   thorpej 	u_int ipf_timer;		/* lifetime timer */
     78      1.53   thorpej 	time_t ipf_start;		/* creation time */
     79      1.53   thorpej };
     80      1.53   thorpej 
     81      1.53   thorpej #define	IPFLOW_HASHBITS		6	/* should not be a multiple of 8 */
     82      1.53   thorpej 
     83      1.42        ad POOL_INIT(ipflow_pool, sizeof(struct ipflow), 0, 0, 0, "ipflowpl", NULL,
     84      1.42        ad     IPL_NET);
     85       1.7   thorpej 
     86       1.5   thorpej LIST_HEAD(ipflowhead, ipflow);
     87       1.5   thorpej 
     88       1.1      matt #define	IPFLOW_TIMER		(5 * PR_SLOWHZ)
     89      1.43  liamjfoy #define	IPFLOW_DEFAULT_HASHSIZE	(1 << IPFLOW_HASHBITS)
     90       1.5   thorpej 
     91      1.43  liamjfoy static struct ipflowhead *ipflowtable = NULL;
     92       1.5   thorpej static struct ipflowhead ipflowlist;
     93       1.1      matt static int ipflow_inuse;
     94       1.5   thorpej 
     95       1.5   thorpej #define	IPFLOW_INSERT(bucket, ipf) \
     96       1.5   thorpej do { \
     97       1.5   thorpej 	LIST_INSERT_HEAD((bucket), (ipf), ipf_hash); \
     98       1.5   thorpej 	LIST_INSERT_HEAD(&ipflowlist, (ipf), ipf_list); \
     99      1.26     perry } while (/*CONSTCOND*/ 0)
    100       1.5   thorpej 
    101       1.5   thorpej #define	IPFLOW_REMOVE(ipf) \
    102       1.5   thorpej do { \
    103       1.5   thorpej 	LIST_REMOVE((ipf), ipf_hash); \
    104       1.5   thorpej 	LIST_REMOVE((ipf), ipf_list); \
    105      1.26     perry } while (/*CONSTCOND*/ 0)
    106       1.5   thorpej 
    107       1.3      matt #ifndef IPFLOW_MAX
    108       1.1      matt #define	IPFLOW_MAX		256
    109       1.3      matt #endif
    110       1.3      matt int ip_maxflows = IPFLOW_MAX;
    111      1.43  liamjfoy int ip_hashsize = IPFLOW_DEFAULT_HASHSIZE;
    112       1.1      matt 
    113      1.45  liamjfoy static size_t
    114      1.51    dyoung ipflow_hash(const struct ip *ip)
    115       1.1      matt {
    116      1.45  liamjfoy 	size_t hash = ip->ip_tos;
    117      1.45  liamjfoy 	size_t idx;
    118      1.45  liamjfoy 
    119      1.45  liamjfoy 	for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS) {
    120      1.45  liamjfoy 		hash += (ip->ip_dst.s_addr >> (32 - idx)) +
    121      1.45  liamjfoy 		    (ip->ip_src.s_addr >> idx);
    122      1.45  liamjfoy 	}
    123      1.45  liamjfoy 
    124      1.43  liamjfoy 	return hash & (ip_hashsize-1);
    125       1.1      matt }
    126       1.1      matt 
    127       1.1      matt static struct ipflow *
    128      1.51    dyoung ipflow_lookup(const struct ip *ip)
    129       1.1      matt {
    130      1.45  liamjfoy 	size_t hash;
    131       1.1      matt 	struct ipflow *ipf;
    132       1.1      matt 
    133      1.45  liamjfoy 	hash = ipflow_hash(ip);
    134       1.1      matt 
    135      1.30  christos 	LIST_FOREACH(ipf, &ipflowtable[hash], ipf_hash) {
    136       1.1      matt 		if (ip->ip_dst.s_addr == ipf->ipf_dst.s_addr
    137       1.1      matt 		    && ip->ip_src.s_addr == ipf->ipf_src.s_addr
    138       1.1      matt 		    && ip->ip_tos == ipf->ipf_tos)
    139       1.1      matt 			break;
    140       1.1      matt 	}
    141       1.1      matt 	return ipf;
    142       1.1      matt }
    143       1.1      matt 
    144      1.43  liamjfoy int
    145      1.43  liamjfoy ipflow_init(int table_size)
    146       1.7   thorpej {
    147      1.43  liamjfoy 	struct ipflowhead *new_table;
    148      1.45  liamjfoy 	size_t i;
    149       1.7   thorpej 
    150      1.43  liamjfoy 	new_table = (struct ipflowhead *)malloc(sizeof(struct ipflowhead) *
    151      1.43  liamjfoy 	    table_size, M_RTABLE, M_NOWAIT);
    152      1.43  liamjfoy 
    153      1.43  liamjfoy 	if (new_table == NULL)
    154      1.43  liamjfoy 		return 1;
    155      1.43  liamjfoy 
    156      1.43  liamjfoy 	if (ipflowtable != NULL)
    157      1.43  liamjfoy 		free(ipflowtable, M_RTABLE);
    158      1.43  liamjfoy 
    159      1.43  liamjfoy 	ipflowtable = new_table;
    160      1.43  liamjfoy 	ip_hashsize = table_size;
    161      1.43  liamjfoy 
    162       1.7   thorpej 	LIST_INIT(&ipflowlist);
    163      1.43  liamjfoy 	for (i = 0; i < ip_hashsize; i++)
    164       1.7   thorpej 		LIST_INIT(&ipflowtable[i]);
    165      1.43  liamjfoy 
    166      1.43  liamjfoy 	return 0;
    167       1.7   thorpej }
    168       1.7   thorpej 
    169       1.1      matt int
    170      1.29     perry ipflow_fastforward(struct mbuf *m)
    171       1.1      matt {
    172      1.51    dyoung 	struct ip *ip;
    173      1.51    dyoung 	struct ip ip_store;
    174       1.1      matt 	struct ipflow *ipf;
    175       1.1      matt 	struct rtentry *rt;
    176      1.40    dyoung 	const struct sockaddr *dst;
    177       1.1      matt 	int error;
    178       1.6  sommerfe 	int iplen;
    179       1.1      matt 
    180       1.1      matt 	/*
    181       1.1      matt 	 * Are we forwarding packets?  Big enough for an IP packet?
    182       1.1      matt 	 */
    183       1.3      matt 	if (!ipforwarding || ipflow_inuse == 0 || m->m_len < sizeof(struct ip))
    184       1.1      matt 		return 0;
    185      1.14  sommerfe 
    186      1.14  sommerfe 	/*
    187      1.19       wiz 	 * Was packet received as a link-level multicast or broadcast?
    188      1.14  sommerfe 	 * If so, don't try to fast forward..
    189      1.14  sommerfe 	 */
    190      1.14  sommerfe 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0)
    191      1.14  sommerfe 		return 0;
    192      1.24    itojun 
    193       1.1      matt 	/*
    194       1.1      matt 	 * IP header with no option and valid version and length
    195       1.1      matt 	 */
    196      1.51    dyoung 	if (IP_HDR_ALIGNED_P(mtod(m, const void *)))
    197      1.25   thorpej 		ip = mtod(m, struct ip *);
    198      1.25   thorpej 	else {
    199      1.51    dyoung 		memcpy(&ip_store, mtod(m, const void *), sizeof(ip_store));
    200      1.25   thorpej 		ip = &ip_store;
    201      1.25   thorpej 	}
    202       1.6  sommerfe 	iplen = ntohs(ip->ip_len);
    203       1.5   thorpej 	if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2) ||
    204      1.13     proff 	    iplen < sizeof(struct ip) || iplen > m->m_pkthdr.len)
    205       1.1      matt 		return 0;
    206       1.1      matt 	/*
    207       1.1      matt 	 * Find a flow.
    208       1.1      matt 	 */
    209       1.1      matt 	if ((ipf = ipflow_lookup(ip)) == NULL)
    210       1.1      matt 		return 0;
    211       1.1      matt 
    212       1.1      matt 	/*
    213      1.18   thorpej 	 * Verify the IP header checksum.
    214       1.2   thorpej 	 */
    215      1.18   thorpej 	switch (m->m_pkthdr.csum_flags &
    216      1.20   thorpej 		((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
    217      1.18   thorpej 		 M_CSUM_IPv4_BAD)) {
    218      1.18   thorpej 	case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
    219      1.18   thorpej 		return (0);
    220      1.18   thorpej 
    221      1.18   thorpej 	case M_CSUM_IPv4:
    222      1.18   thorpej 		/* Checksum was okay. */
    223      1.18   thorpej 		break;
    224      1.18   thorpej 
    225      1.18   thorpej 	default:
    226      1.18   thorpej 		/* Must compute it ourselves. */
    227      1.18   thorpej 		if (in_cksum(m, sizeof(struct ip)) != 0)
    228      1.18   thorpej 			return (0);
    229      1.18   thorpej 		break;
    230      1.18   thorpej 	}
    231       1.2   thorpej 
    232       1.2   thorpej 	/*
    233       1.1      matt 	 * Route and interface still up?
    234       1.1      matt 	 */
    235      1.50    dyoung 	if ((rt = rtcache_validate(&ipf->ipf_ro)) == NULL ||
    236      1.48    dyoung 	    (rt->rt_ifp->if_flags & IFF_UP) == 0)
    237       1.1      matt 		return 0;
    238       1.1      matt 
    239       1.1      matt 	/*
    240       1.1      matt 	 * Packet size OK?  TTL?
    241       1.1      matt 	 */
    242       1.1      matt 	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
    243       1.1      matt 		return 0;
    244       1.1      matt 
    245       1.1      matt 	/*
    246      1.18   thorpej 	 * Clear any in-bound checksum flags for this packet.
    247      1.18   thorpej 	 */
    248      1.18   thorpej 	m->m_pkthdr.csum_flags = 0;
    249      1.18   thorpej 
    250      1.18   thorpej 	/*
    251       1.1      matt 	 * Everything checks out and so we can forward this packet.
    252       1.1      matt 	 * Modify the TTL and incrementally change the checksum.
    253      1.24    itojun 	 *
    254       1.9   mycroft 	 * This method of adding the checksum works on either endian CPU.
    255       1.9   mycroft 	 * If htons() is inlined, all the arithmetic is folded; otherwise
    256      1.32     perry 	 * the htons()s are combined by CSE due to the const attribute.
    257      1.18   thorpej 	 *
    258      1.18   thorpej 	 * Don't bother using HW checksumming here -- the incremental
    259      1.18   thorpej 	 * update is pretty fast.
    260       1.1      matt 	 */
    261       1.1      matt 	ip->ip_ttl -= IPTTLDEC;
    262      1.12     itohy 	if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
    263      1.11   mycroft 		ip->ip_sum -= ~htons(IPTTLDEC << 8);
    264       1.8   mycroft 	else
    265       1.2   thorpej 		ip->ip_sum += htons(IPTTLDEC << 8);
    266      1.25   thorpej 
    267      1.25   thorpej 	/*
    268      1.25   thorpej 	 * Done modifying the header; copy it back, if necessary.
    269      1.51    dyoung 	 *
    270      1.51    dyoung 	 * XXX Use m_copyback_cow(9) here? --dyoung
    271      1.25   thorpej 	 */
    272      1.41  christos 	if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0)
    273      1.41  christos 		memcpy(mtod(m, void *), &ip_store, sizeof(ip_store));
    274       1.6  sommerfe 
    275       1.6  sommerfe 	/*
    276      1.24    itojun 	 * Trim the packet in case it's too long..
    277       1.6  sommerfe 	 */
    278       1.6  sommerfe 	if (m->m_pkthdr.len > iplen) {
    279       1.6  sommerfe 		if (m->m_len == m->m_pkthdr.len) {
    280       1.6  sommerfe 			m->m_len = iplen;
    281       1.6  sommerfe 			m->m_pkthdr.len = iplen;
    282       1.6  sommerfe 		} else
    283       1.6  sommerfe 			m_adj(m, iplen - m->m_pkthdr.len);
    284       1.2   thorpej 	}
    285       1.1      matt 
    286       1.1      matt 	/*
    287       1.1      matt 	 * Send the packet on it's way.  All we can get back is ENOBUFS
    288       1.1      matt 	 */
    289       1.1      matt 	ipf->ipf_uses++;
    290       1.5   thorpej 	PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
    291      1.16   thorpej 
    292      1.16   thorpej 	if (rt->rt_flags & RTF_GATEWAY)
    293      1.16   thorpej 		dst = rt->rt_gateway;
    294      1.16   thorpej 	else
    295      1.40    dyoung 		dst = rtcache_getdst(&ipf->ipf_ro);
    296      1.16   thorpej 
    297      1.16   thorpej 	if ((error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m, dst, rt)) != 0) {
    298       1.1      matt 		if (error == ENOBUFS)
    299       1.1      matt 			ipf->ipf_dropped++;
    300       1.1      matt 		else
    301       1.1      matt 			ipf->ipf_errors++;
    302       1.1      matt 	}
    303       1.1      matt 	return 1;
    304       1.1      matt }
    305       1.1      matt 
    306       1.1      matt static void
    308       1.1      matt ipflow_addstats(struct ipflow *ipf)
    309      1.49    dyoung {
    310      1.54   thorpej 	struct rtentry *rt;
    311      1.49    dyoung 	uint64_t *ips;
    312      1.50    dyoung 
    313      1.49    dyoung 	if ((rt = rtcache_validate(&ipf->ipf_ro)) != NULL)
    314      1.54   thorpej 		rt->rt_use += ipf->ipf_uses;
    315      1.54   thorpej 
    316      1.54   thorpej 	ips = IP_STAT_GETREF();
    317      1.54   thorpej 	ips[IP_STAT_CANTFORWARD] += ipf->ipf_errors + ipf->ipf_dropped;
    318      1.54   thorpej 	ips[IP_STAT_TOTAL] += ipf->ipf_uses;
    319      1.54   thorpej 	ips[IP_STAT_FORWARD] += ipf->ipf_uses;
    320      1.54   thorpej 	ips[IP_STAT_FASTFORWARD] += ipf->ipf_uses;
    321       1.1      matt 	IP_STAT_PUTREF();
    322       1.1      matt }
    323       1.1      matt 
    324      1.29     perry static void
    325       1.1      matt ipflow_free(struct ipflow *ipf)
    326       1.1      matt {
    327       1.1      matt 	int s;
    328       1.1      matt 	/*
    329       1.1      matt 	 * Remove the flow from the hash table (at elevated IPL).
    330       1.1      matt 	 * Once it's off the list, we can deal with it at normal
    331       1.1      matt 	 * network IPL.
    332      1.17   thorpej 	 */
    333       1.5   thorpej 	s = splnet();
    334       1.1      matt 	IPFLOW_REMOVE(ipf);
    335       1.1      matt 	splx(s);
    336      1.38     joerg 	ipflow_addstats(ipf);
    337       1.1      matt 	rtcache_free(&ipf->ipf_ro);
    338      1.35       tls 	ipflow_inuse--;
    339       1.7   thorpej 	s = splnet();
    340      1.35       tls 	pool_put(&ipflow_pool, ipf);
    341       1.1      matt 	splx(s);
    342       1.1      matt }
    343      1.53   thorpej 
    344      1.53   thorpej static struct ipflow *
    345       1.1      matt ipflow_reap(bool just_one)
    346       1.3      matt {
    347       1.3      matt 	while (just_one || ipflow_inuse > ip_maxflows) {
    348       1.3      matt 		struct ipflow *ipf, *maybe_ipf = NULL;
    349       1.3      matt 		int s;
    350       1.5   thorpej 
    351       1.5   thorpej 		ipf = LIST_FIRST(&ipflowlist);
    352       1.5   thorpej 		while (ipf != NULL) {
    353       1.5   thorpej 			/*
    354       1.5   thorpej 			 * If this no longer points to a valid route
    355       1.5   thorpej 			 * reclaim it.
    356      1.50    dyoung 			 */
    357       1.5   thorpej 			if (rtcache_validate(&ipf->ipf_ro) == NULL)
    358       1.5   thorpej 				goto done;
    359       1.5   thorpej 			/*
    360       1.5   thorpej 			 * choose the one that's been least recently
    361       1.5   thorpej 			 * used or has had the least uses in the
    362       1.5   thorpej 			 * last 1.5 intervals.
    363       1.5   thorpej 			 */
    364       1.5   thorpej 			if (maybe_ipf == NULL ||
    365       1.5   thorpej 			    ipf->ipf_timer < maybe_ipf->ipf_timer ||
    366       1.5   thorpej 			    (ipf->ipf_timer == maybe_ipf->ipf_timer &&
    367       1.5   thorpej 			     ipf->ipf_last_uses + ipf->ipf_uses <
    368       1.5   thorpej 			         maybe_ipf->ipf_last_uses +
    369       1.5   thorpej 			         maybe_ipf->ipf_uses))
    370       1.5   thorpej 				maybe_ipf = ipf;
    371       1.1      matt 			ipf = LIST_NEXT(ipf, ipf_list);
    372       1.3      matt 		}
    373       1.3      matt 		ipf = maybe_ipf;
    374       1.3      matt 	    done:
    375       1.3      matt 		/*
    376       1.3      matt 		 * Remove the entry from the flow table.
    377      1.17   thorpej 		 */
    378       1.5   thorpej 		s = splnet();
    379       1.3      matt 		IPFLOW_REMOVE(ipf);
    380       1.3      matt 		splx(s);
    381      1.38     joerg 		ipflow_addstats(ipf);
    382       1.3      matt 		rtcache_free(&ipf->ipf_ro);
    383       1.3      matt 		if (just_one)
    384       1.7   thorpej 			return ipf;
    385       1.3      matt 		pool_put(&ipflow_pool, ipf);
    386       1.1      matt 		ipflow_inuse--;
    387       1.3      matt 	}
    388       1.1      matt 	return NULL;
    389       1.1      matt }
    390       1.1      matt 
    391      1.53   thorpej void
    392      1.53   thorpej ipflow_prune(void)
    393      1.53   thorpej {
    394      1.53   thorpej 
    395      1.53   thorpej 	(void) ipflow_reap(false);
    396      1.53   thorpej }
    397      1.53   thorpej 
    398      1.29     perry void
    399       1.1      matt ipflow_slowtimo(void)
    400      1.49    dyoung {
    401       1.5   thorpej 	struct rtentry *rt;
    402      1.54   thorpej 	struct ipflow *ipf, *next_ipf;
    403       1.2   thorpej 	uint64_t *ips;
    404      1.55        ad 
    405      1.55        ad 	mutex_enter(softnet_lock);
    406      1.30  christos 	KERNEL_LOCK(1, NULL);
    407       1.5   thorpej 	for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
    408      1.37    dyoung 		next_ipf = LIST_NEXT(ipf, ipf_list);
    409      1.50    dyoung 		if (PRT_SLOW_ISEXPIRED(ipf->ipf_timer) ||
    410       1.5   thorpej 		    (rt = rtcache_validate(&ipf->ipf_ro)) == NULL) {
    411       1.5   thorpej 			ipflow_free(ipf);
    412       1.5   thorpej 		} else {
    413      1.49    dyoung 			ipf->ipf_last_uses = ipf->ipf_uses;
    414      1.54   thorpej 			rt->rt_use += ipf->ipf_uses;
    415      1.54   thorpej 			ips = IP_STAT_GETREF();
    416      1.54   thorpej 			ips[IP_STAT_TOTAL] += ipf->ipf_uses;
    417      1.54   thorpej 			ips[IP_STAT_FORWARD] += ipf->ipf_uses;
    418      1.54   thorpej 			ips[IP_STAT_FASTFORWARD] += ipf->ipf_uses;
    419       1.5   thorpej 			IP_STAT_PUTREF();
    420       1.1      matt 			ipf->ipf_uses = 0;
    421       1.1      matt 		}
    422      1.55        ad 	}
    423      1.55        ad 	KERNEL_UNLOCK_ONE(NULL);
    424       1.1      matt 	mutex_exit(softnet_lock);
    425       1.1      matt }
    426       1.1      matt 
    427      1.29     perry void
    428       1.1      matt ipflow_create(const struct route *ro, struct mbuf *m)
    429      1.51    dyoung {
    430       1.1      matt 	const struct ip *const ip = mtod(m, const struct ip *);
    431      1.45  liamjfoy 	struct ipflow *ipf;
    432       1.1      matt 	size_t hash;
    433       1.1      matt 	int s;
    434       1.1      matt 
    435       1.1      matt 	/*
    436       1.1      matt 	 * Don't create cache entries for ICMP messages.
    437       1.3      matt 	 */
    438       1.1      matt 	if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP)
    439       1.1      matt 		return;
    440       1.1      matt 	/*
    441       1.1      matt 	 * See if an existing flow struct exists.  If so remove it from it's
    442       1.1      matt 	 * list and free the old route.  If not, try to malloc a new one
    443       1.1      matt 	 * (if we aren't at our limit).
    444       1.1      matt 	 */
    445       1.1      matt 	ipf = ipflow_lookup(ip);
    446       1.3      matt 	if (ipf == NULL) {
    447      1.53   thorpej 		if (ipflow_inuse >= ip_maxflows) {
    448       1.1      matt 			ipf = ipflow_reap(true);
    449      1.36       mrg 		} else {
    450       1.7   thorpej 			s = splnet();
    451      1.35       tls 			ipf = pool_get(&ipflow_pool, PR_NOWAIT);
    452       1.1      matt 			splx(s);
    453       1.1      matt 			if (ipf == NULL)
    454       1.1      matt 				return;
    455       1.1      matt 			ipflow_inuse++;
    456      1.39    dyoung 		}
    457       1.1      matt 		memset(ipf, 0, sizeof(*ipf));
    458      1.17   thorpej 	} else {
    459       1.5   thorpej 		s = splnet();
    460       1.1      matt 		IPFLOW_REMOVE(ipf);
    461       1.1      matt 		splx(s);
    462      1.38     joerg 		ipflow_addstats(ipf);
    463       1.1      matt 		rtcache_free(&ipf->ipf_ro);
    464       1.1      matt 		ipf->ipf_uses = ipf->ipf_last_uses = 0;
    465       1.1      matt 		ipf->ipf_errors = ipf->ipf_dropped = 0;
    466       1.1      matt 	}
    467       1.1      matt 
    468       1.1      matt 	/*
    469       1.1      matt 	 * Fill in the updated information.
    470      1.46    dyoung 	 */
    471       1.1      matt 	rtcache_copy(&ipf->ipf_ro, ro);
    472       1.1      matt 	ipf->ipf_dst = ip->ip_dst;
    473       1.1      matt 	ipf->ipf_src = ip->ip_src;
    474       1.5   thorpej 	ipf->ipf_tos = ip->ip_tos;
    475      1.33    kardel 	PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
    476       1.1      matt 	ipf->ipf_start = time_uptime;
    477       1.1      matt 	/*
    478       1.1      matt 	 * Insert into the approriate bucket of the flow table.
    479      1.45  liamjfoy 	 */
    480      1.17   thorpej 	hash = ipflow_hash(ip);
    481       1.5   thorpej 	s = splnet();
    482      1.27       scw 	IPFLOW_INSERT(&ipflowtable[hash], ipf);
    483      1.27       scw 	splx(s);
    484      1.27       scw }
    485      1.43  liamjfoy 
    486      1.43  liamjfoy int
    487      1.27       scw ipflow_invalidate_all(int new_size)
    488      1.27       scw {
    489      1.43  liamjfoy 	struct ipflow *ipf, *next_ipf;
    490      1.27       scw 	int s, error;
    491      1.43  liamjfoy 
    492      1.27       scw 	error = 0;
    493      1.27       scw 	s = splnet();
    494      1.27       scw 	for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
    495      1.27       scw 		next_ipf = LIST_NEXT(ipf, ipf_list);
    496      1.27       scw 		ipflow_free(ipf);
    497      1.43  liamjfoy 	}
    498      1.43  liamjfoy 
    499      1.43  liamjfoy 	if (new_size)
    500       1.1      matt 		error = ipflow_init(new_size);
    501      1.43  liamjfoy 	splx(s);
    502      1.43  liamjfoy 
    503       1.1      matt 	return error;
    504                     }
    505