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