Home | History | Annotate | Line # | Download | only in netinet
ip_flow.c revision 1.65.2.3
      1  1.65.2.3     skrll /*	$NetBSD: ip_flow.c,v 1.65.2.3 2016/10/05 20:56:09 skrll 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.65.2.3     skrll __KERNEL_RCSID(0, "$NetBSD: ip_flow.c,v 1.65.2.3 2016/10/05 20:56:09 skrll 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.65.2.3     skrll #include <sys/workqueue.h>
     49  1.65.2.3     skrll #include <sys/atomic.h>
     50       1.1      matt 
     51       1.1      matt #include <net/if.h>
     52       1.1      matt #include <net/if_dl.h>
     53       1.1      matt #include <net/route.h>
     54       1.1      matt #include <net/pfil.h>
     55       1.1      matt 
     56       1.1      matt #include <netinet/in.h>
     57       1.1      matt #include <netinet/in_systm.h>
     58       1.1      matt #include <netinet/ip.h>
     59       1.1      matt #include <netinet/in_pcb.h>
     60       1.1      matt #include <netinet/in_var.h>
     61       1.1      matt #include <netinet/ip_var.h>
     62      1.54   thorpej #include <netinet/ip_private.h>
     63       1.1      matt 
     64      1.44  liamjfoy /*
     65      1.44  liamjfoy  * Similar code is very well commented in netinet6/ip6_flow.c
     66  1.65.2.3     skrll  */
     67      1.44  liamjfoy 
     68      1.53   thorpej #define	IPFLOW_HASHBITS		6	/* should not be a multiple of 8 */
     69      1.53   thorpej 
     70      1.57     pooka static struct pool ipflow_pool;
     71       1.7   thorpej 
     72  1.65.2.3     skrll TAILQ_HEAD(ipflowhead, ipflow);
     73       1.5   thorpej 
     74       1.1      matt #define	IPFLOW_TIMER		(5 * PR_SLOWHZ)
     75      1.43  liamjfoy #define	IPFLOW_DEFAULT_HASHSIZE	(1 << IPFLOW_HASHBITS)
     76       1.5   thorpej 
     77  1.65.2.2     skrll /*
     78  1.65.2.2     skrll  * ip_flow.c internal lock.
     79  1.65.2.2     skrll  * If we use softnet_lock, it would cause recursive lock.
     80  1.65.2.2     skrll  *
     81  1.65.2.2     skrll  * This is a tentative workaround.
     82  1.65.2.2     skrll  * We should make it scalable somehow in the future.
     83  1.65.2.2     skrll  */
     84  1.65.2.2     skrll static kmutex_t ipflow_lock;
     85      1.43  liamjfoy static struct ipflowhead *ipflowtable = NULL;
     86       1.5   thorpej static struct ipflowhead ipflowlist;
     87       1.1      matt static int ipflow_inuse;
     88       1.5   thorpej 
     89  1.65.2.3     skrll #define	IPFLOW_INSERT(hashidx, ipf) \
     90       1.5   thorpej do { \
     91  1.65.2.3     skrll 	(ipf)->ipf_hashidx = (hashidx); \
     92  1.65.2.3     skrll 	TAILQ_INSERT_HEAD(&ipflowtable[(hashidx)], (ipf), ipf_hash); \
     93  1.65.2.3     skrll 	TAILQ_INSERT_HEAD(&ipflowlist, (ipf), ipf_list); \
     94      1.26     perry } while (/*CONSTCOND*/ 0)
     95       1.5   thorpej 
     96  1.65.2.3     skrll #define	IPFLOW_REMOVE(hashidx, ipf) \
     97       1.5   thorpej do { \
     98  1.65.2.3     skrll 	TAILQ_REMOVE(&ipflowtable[(hashidx)], (ipf), ipf_hash); \
     99  1.65.2.3     skrll 	TAILQ_REMOVE(&ipflowlist, (ipf), ipf_list); \
    100      1.26     perry } while (/*CONSTCOND*/ 0)
    101       1.5   thorpej 
    102       1.3      matt #ifndef IPFLOW_MAX
    103       1.1      matt #define	IPFLOW_MAX		256
    104       1.3      matt #endif
    105      1.64     rmind static int ip_maxflows = IPFLOW_MAX;
    106      1.64     rmind static int ip_hashsize = IPFLOW_DEFAULT_HASHSIZE;
    107      1.64     rmind 
    108  1.65.2.2     skrll static struct ipflow *ipflow_reap(bool);
    109      1.64     rmind static void ipflow_sysctl_init(struct sysctllog **);
    110       1.1      matt 
    111  1.65.2.3     skrll static void ipflow_slowtimo_work(struct work *, void *);
    112  1.65.2.3     skrll static struct workqueue	*ipflow_slowtimo_wq;
    113  1.65.2.3     skrll static struct work	ipflow_slowtimo_wk;
    114  1.65.2.3     skrll 
    115  1.65.2.3     skrll static size_t
    116      1.51    dyoung ipflow_hash(const struct ip *ip)
    117       1.1      matt {
    118      1.45  liamjfoy 	size_t hash = ip->ip_tos;
    119      1.45  liamjfoy 	size_t idx;
    120      1.45  liamjfoy 
    121      1.45  liamjfoy 	for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS) {
    122      1.45  liamjfoy 		hash += (ip->ip_dst.s_addr >> (32 - idx)) +
    123      1.45  liamjfoy 		    (ip->ip_src.s_addr >> idx);
    124      1.45  liamjfoy 	}
    125      1.45  liamjfoy 
    126      1.43  liamjfoy 	return hash & (ip_hashsize-1);
    127       1.1      matt }
    128       1.1      matt 
    129       1.1      matt static struct ipflow *
    130      1.51    dyoung ipflow_lookup(const struct ip *ip)
    131       1.1      matt {
    132      1.45  liamjfoy 	size_t hash;
    133       1.1      matt 	struct ipflow *ipf;
    134       1.1      matt 
    135  1.65.2.2     skrll 	KASSERT(mutex_owned(&ipflow_lock));
    136  1.65.2.2     skrll 
    137      1.45  liamjfoy 	hash = ipflow_hash(ip);
    138       1.1      matt 
    139  1.65.2.3     skrll 	TAILQ_FOREACH(ipf, &ipflowtable[hash], ipf_hash) {
    140       1.1      matt 		if (ip->ip_dst.s_addr == ipf->ipf_dst.s_addr
    141       1.1      matt 		    && ip->ip_src.s_addr == ipf->ipf_src.s_addr
    142       1.1      matt 		    && ip->ip_tos == ipf->ipf_tos)
    143       1.1      matt 			break;
    144       1.1      matt 	}
    145       1.1      matt 	return ipf;
    146       1.1      matt }
    147       1.1      matt 
    148      1.57     pooka void
    149      1.58    cegger ipflow_poolinit(void)
    150      1.57     pooka {
    151      1.57     pooka 
    152      1.57     pooka 	pool_init(&ipflow_pool, sizeof(struct ipflow), 0, 0, 0, "ipflowpl",
    153      1.57     pooka 	    NULL, IPL_NET);
    154      1.57     pooka }
    155      1.57     pooka 
    156      1.64     rmind static int
    157      1.64     rmind ipflow_reinit(int table_size)
    158       1.7   thorpej {
    159      1.43  liamjfoy 	struct ipflowhead *new_table;
    160      1.45  liamjfoy 	size_t i;
    161       1.7   thorpej 
    162  1.65.2.2     skrll 	KASSERT(mutex_owned(&ipflow_lock));
    163  1.65.2.2     skrll 
    164      1.43  liamjfoy 	new_table = (struct ipflowhead *)malloc(sizeof(struct ipflowhead) *
    165      1.43  liamjfoy 	    table_size, M_RTABLE, M_NOWAIT);
    166      1.43  liamjfoy 
    167      1.43  liamjfoy 	if (new_table == NULL)
    168      1.43  liamjfoy 		return 1;
    169      1.43  liamjfoy 
    170      1.43  liamjfoy 	if (ipflowtable != NULL)
    171      1.43  liamjfoy 		free(ipflowtable, M_RTABLE);
    172      1.43  liamjfoy 
    173      1.43  liamjfoy 	ipflowtable = new_table;
    174      1.43  liamjfoy 	ip_hashsize = table_size;
    175      1.43  liamjfoy 
    176  1.65.2.3     skrll 	TAILQ_INIT(&ipflowlist);
    177      1.43  liamjfoy 	for (i = 0; i < ip_hashsize; i++)
    178  1.65.2.3     skrll 		TAILQ_INIT(&ipflowtable[i]);
    179      1.43  liamjfoy 
    180      1.43  liamjfoy 	return 0;
    181       1.7   thorpej }
    182       1.7   thorpej 
    183      1.64     rmind void
    184      1.64     rmind ipflow_init(void)
    185      1.64     rmind {
    186  1.65.2.3     skrll 	int error;
    187  1.65.2.3     skrll 
    188  1.65.2.3     skrll 	error = workqueue_create(&ipflow_slowtimo_wq, "ipflow_slowtimo",
    189  1.65.2.3     skrll 	    ipflow_slowtimo_work, NULL, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
    190  1.65.2.3     skrll 	if (error != 0)
    191  1.65.2.3     skrll 		panic("%s: workqueue_create failed (%d)\n", __func__, error);
    192  1.65.2.2     skrll 
    193  1.65.2.2     skrll 	mutex_init(&ipflow_lock, MUTEX_DEFAULT, IPL_NONE);
    194  1.65.2.2     skrll 
    195  1.65.2.2     skrll 	mutex_enter(&ipflow_lock);
    196      1.64     rmind 	(void)ipflow_reinit(ip_hashsize);
    197  1.65.2.2     skrll 	mutex_exit(&ipflow_lock);
    198      1.64     rmind 	ipflow_sysctl_init(NULL);
    199      1.64     rmind }
    200      1.64     rmind 
    201       1.1      matt int
    202      1.29     perry ipflow_fastforward(struct mbuf *m)
    203       1.1      matt {
    204      1.51    dyoung 	struct ip *ip;
    205      1.51    dyoung 	struct ip ip_store;
    206       1.1      matt 	struct ipflow *ipf;
    207       1.1      matt 	struct rtentry *rt;
    208      1.40    dyoung 	const struct sockaddr *dst;
    209       1.1      matt 	int error;
    210       1.6  sommerfe 	int iplen;
    211  1.65.2.2     skrll 	struct ifnet *ifp;
    212  1.65.2.2     skrll 	int s;
    213  1.65.2.2     skrll 	int ret = 0;
    214       1.1      matt 
    215  1.65.2.2     skrll 	mutex_enter(&ipflow_lock);
    216       1.1      matt 	/*
    217       1.1      matt 	 * Are we forwarding packets?  Big enough for an IP packet?
    218       1.1      matt 	 */
    219       1.3      matt 	if (!ipforwarding || ipflow_inuse == 0 || m->m_len < sizeof(struct ip))
    220  1.65.2.2     skrll 		goto out;
    221      1.14  sommerfe 
    222      1.14  sommerfe 	/*
    223      1.19       wiz 	 * Was packet received as a link-level multicast or broadcast?
    224      1.14  sommerfe 	 * If so, don't try to fast forward..
    225      1.14  sommerfe 	 */
    226      1.14  sommerfe 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0)
    227  1.65.2.2     skrll 		goto out;
    228      1.24    itojun 
    229       1.1      matt 	/*
    230       1.1      matt 	 * IP header with no option and valid version and length
    231       1.1      matt 	 */
    232      1.51    dyoung 	if (IP_HDR_ALIGNED_P(mtod(m, const void *)))
    233      1.25   thorpej 		ip = mtod(m, struct ip *);
    234      1.25   thorpej 	else {
    235      1.51    dyoung 		memcpy(&ip_store, mtod(m, const void *), sizeof(ip_store));
    236      1.25   thorpej 		ip = &ip_store;
    237      1.25   thorpej 	}
    238       1.6  sommerfe 	iplen = ntohs(ip->ip_len);
    239       1.5   thorpej 	if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2) ||
    240      1.13     proff 	    iplen < sizeof(struct ip) || iplen > m->m_pkthdr.len)
    241  1.65.2.2     skrll 		goto out;
    242       1.1      matt 	/*
    243       1.1      matt 	 * Find a flow.
    244       1.1      matt 	 */
    245       1.1      matt 	if ((ipf = ipflow_lookup(ip)) == NULL)
    246  1.65.2.2     skrll 		goto out;
    247       1.1      matt 
    248  1.65.2.2     skrll 	ifp = m_get_rcvif(m, &s);
    249       1.1      matt 	/*
    250      1.18   thorpej 	 * Verify the IP header checksum.
    251       1.2   thorpej 	 */
    252      1.18   thorpej 	switch (m->m_pkthdr.csum_flags &
    253  1.65.2.2     skrll 		((ifp->if_csum_flags_rx & M_CSUM_IPv4) |
    254      1.18   thorpej 		 M_CSUM_IPv4_BAD)) {
    255      1.18   thorpej 	case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
    256  1.65.2.2     skrll 		m_put_rcvif(ifp, &s);
    257  1.65.2.2     skrll 		goto out;
    258      1.18   thorpej 
    259      1.18   thorpej 	case M_CSUM_IPv4:
    260      1.18   thorpej 		/* Checksum was okay. */
    261      1.18   thorpej 		break;
    262      1.18   thorpej 
    263      1.18   thorpej 	default:
    264      1.18   thorpej 		/* Must compute it ourselves. */
    265  1.65.2.2     skrll 		if (in_cksum(m, sizeof(struct ip)) != 0) {
    266  1.65.2.2     skrll 			m_put_rcvif(ifp, &s);
    267  1.65.2.2     skrll 			goto out;
    268  1.65.2.2     skrll 		}
    269      1.18   thorpej 		break;
    270      1.18   thorpej 	}
    271  1.65.2.2     skrll 	m_put_rcvif(ifp, &s);
    272       1.2   thorpej 
    273       1.2   thorpej 	/*
    274       1.1      matt 	 * Route and interface still up?
    275       1.1      matt 	 */
    276      1.50    dyoung 	if ((rt = rtcache_validate(&ipf->ipf_ro)) == NULL ||
    277  1.65.2.1     skrll 	    (rt->rt_ifp->if_flags & IFF_UP) == 0 ||
    278  1.65.2.1     skrll 	    (rt->rt_flags & (RTF_BLACKHOLE | RTF_BROADCAST)) != 0)
    279  1.65.2.2     skrll 		goto out;
    280       1.1      matt 
    281       1.1      matt 	/*
    282       1.1      matt 	 * Packet size OK?  TTL?
    283       1.1      matt 	 */
    284       1.1      matt 	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
    285  1.65.2.2     skrll 		goto out;
    286       1.1      matt 
    287       1.1      matt 	/*
    288      1.18   thorpej 	 * Clear any in-bound checksum flags for this packet.
    289      1.18   thorpej 	 */
    290      1.18   thorpej 	m->m_pkthdr.csum_flags = 0;
    291      1.18   thorpej 
    292      1.18   thorpej 	/*
    293       1.1      matt 	 * Everything checks out and so we can forward this packet.
    294       1.1      matt 	 * Modify the TTL and incrementally change the checksum.
    295      1.24    itojun 	 *
    296       1.9   mycroft 	 * This method of adding the checksum works on either endian CPU.
    297       1.9   mycroft 	 * If htons() is inlined, all the arithmetic is folded; otherwise
    298      1.32     perry 	 * the htons()s are combined by CSE due to the const attribute.
    299      1.18   thorpej 	 *
    300      1.18   thorpej 	 * Don't bother using HW checksumming here -- the incremental
    301      1.18   thorpej 	 * update is pretty fast.
    302       1.1      matt 	 */
    303       1.1      matt 	ip->ip_ttl -= IPTTLDEC;
    304      1.12     itohy 	if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
    305      1.11   mycroft 		ip->ip_sum -= ~htons(IPTTLDEC << 8);
    306       1.8   mycroft 	else
    307       1.2   thorpej 		ip->ip_sum += htons(IPTTLDEC << 8);
    308      1.25   thorpej 
    309      1.25   thorpej 	/*
    310      1.25   thorpej 	 * Done modifying the header; copy it back, if necessary.
    311      1.51    dyoung 	 *
    312      1.51    dyoung 	 * XXX Use m_copyback_cow(9) here? --dyoung
    313      1.25   thorpej 	 */
    314      1.41  christos 	if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0)
    315      1.41  christos 		memcpy(mtod(m, void *), &ip_store, sizeof(ip_store));
    316       1.6  sommerfe 
    317       1.6  sommerfe 	/*
    318      1.24    itojun 	 * Trim the packet in case it's too long..
    319       1.6  sommerfe 	 */
    320       1.6  sommerfe 	if (m->m_pkthdr.len > iplen) {
    321       1.6  sommerfe 		if (m->m_len == m->m_pkthdr.len) {
    322       1.6  sommerfe 			m->m_len = iplen;
    323       1.6  sommerfe 			m->m_pkthdr.len = iplen;
    324       1.6  sommerfe 		} else
    325       1.6  sommerfe 			m_adj(m, iplen - m->m_pkthdr.len);
    326       1.2   thorpej 	}
    327       1.1      matt 
    328       1.1      matt 	/*
    329      1.65       snj 	 * Send the packet on its way.  All we can get back is ENOBUFS
    330       1.1      matt 	 */
    331       1.1      matt 	ipf->ipf_uses++;
    332  1.65.2.3     skrll 
    333  1.65.2.3     skrll #if 0
    334  1.65.2.3     skrll 	/*
    335  1.65.2.3     skrll 	 * Sorting list is too heavy for fast path(packet processing path).
    336  1.65.2.3     skrll 	 * It degrades about 10% performance. So, we does not sort ipflowtable,
    337  1.65.2.3     skrll 	 * and then we use FIFO cache replacement instead fo LRU.
    338  1.65.2.3     skrll 	 */
    339  1.65.2.3     skrll 	/* move to head (LRU) for ipflowlist. ipflowtable ooes not care LRU. */
    340  1.65.2.3     skrll 	TAILQ_REMOVE(&ipflowlist, ipf, ipf_list);
    341  1.65.2.3     skrll 	TAILQ_INSERT_HEAD(&ipflowlist, ipf, ipf_list);
    342  1.65.2.3     skrll #endif
    343  1.65.2.3     skrll 
    344       1.5   thorpej 	PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
    345      1.16   thorpej 
    346      1.16   thorpej 	if (rt->rt_flags & RTF_GATEWAY)
    347      1.16   thorpej 		dst = rt->rt_gateway;
    348      1.16   thorpej 	else
    349      1.40    dyoung 		dst = rtcache_getdst(&ipf->ipf_ro);
    350      1.16   thorpej 
    351  1.65.2.2     skrll 	if ((error = if_output_lock(rt->rt_ifp, rt->rt_ifp, m, dst, rt)) != 0) {
    352       1.1      matt 		if (error == ENOBUFS)
    353       1.1      matt 			ipf->ipf_dropped++;
    354       1.1      matt 		else
    355       1.1      matt 			ipf->ipf_errors++;
    356       1.1      matt 	}
    357  1.65.2.2     skrll 	ret = 1;
    358  1.65.2.2     skrll  out:
    359  1.65.2.2     skrll 	mutex_exit(&ipflow_lock);
    360  1.65.2.2     skrll 	return ret;
    361       1.1      matt }
    362  1.65.2.3     skrll 
    363       1.1      matt static void
    364      1.29     perry ipflow_addstats(struct ipflow *ipf)
    365       1.1      matt {
    366      1.49    dyoung 	struct rtentry *rt;
    367      1.54   thorpej 	uint64_t *ips;
    368      1.49    dyoung 
    369      1.50    dyoung 	if ((rt = rtcache_validate(&ipf->ipf_ro)) != NULL)
    370      1.49    dyoung 		rt->rt_use += ipf->ipf_uses;
    371  1.65.2.3     skrll 
    372      1.54   thorpej 	ips = IP_STAT_GETREF();
    373      1.54   thorpej 	ips[IP_STAT_CANTFORWARD] += ipf->ipf_errors + ipf->ipf_dropped;
    374      1.54   thorpej 	ips[IP_STAT_TOTAL] += ipf->ipf_uses;
    375      1.54   thorpej 	ips[IP_STAT_FORWARD] += ipf->ipf_uses;
    376      1.54   thorpej 	ips[IP_STAT_FASTFORWARD] += ipf->ipf_uses;
    377      1.54   thorpej 	IP_STAT_PUTREF();
    378       1.1      matt }
    379       1.1      matt 
    380       1.1      matt static void
    381      1.29     perry ipflow_free(struct ipflow *ipf)
    382       1.1      matt {
    383  1.65.2.2     skrll 
    384  1.65.2.2     skrll 	KASSERT(mutex_owned(&ipflow_lock));
    385  1.65.2.2     skrll 
    386       1.1      matt 	/*
    387       1.1      matt 	 * Remove the flow from the hash table (at elevated IPL).
    388       1.1      matt 	 * Once it's off the list, we can deal with it at normal
    389       1.1      matt 	 * network IPL.
    390       1.1      matt 	 */
    391  1.65.2.3     skrll 	IPFLOW_REMOVE(ipf->ipf_hashidx, ipf);
    392  1.65.2.2     skrll 
    393       1.1      matt 	ipflow_addstats(ipf);
    394      1.38     joerg 	rtcache_free(&ipf->ipf_ro);
    395       1.1      matt 	ipflow_inuse--;
    396       1.7   thorpej 	pool_put(&ipflow_pool, ipf);
    397       1.1      matt }
    398       1.1      matt 
    399  1.65.2.2     skrll static struct ipflow *
    400      1.53   thorpej ipflow_reap(bool just_one)
    401       1.1      matt {
    402  1.65.2.3     skrll 	struct ipflow *ipf;
    403  1.65.2.2     skrll 
    404  1.65.2.2     skrll 	KASSERT(mutex_owned(&ipflow_lock));
    405  1.65.2.2     skrll 
    406  1.65.2.3     skrll 	/*
    407  1.65.2.3     skrll 	 * This case must remove one ipflow. Furthermore, this case is used in
    408  1.65.2.3     skrll 	 * fast path(packet processing path). So, simply remove TAILQ_LAST one.
    409  1.65.2.3     skrll 	 */
    410  1.65.2.3     skrll 	if (just_one) {
    411  1.65.2.3     skrll 		ipf = TAILQ_LAST(&ipflowlist, ipflowhead);
    412  1.65.2.3     skrll 		KASSERT(ipf != NULL);
    413  1.65.2.3     skrll 
    414  1.65.2.3     skrll 		IPFLOW_REMOVE(ipf->ipf_hashidx, ipf);
    415  1.65.2.3     skrll 
    416  1.65.2.3     skrll 		ipflow_addstats(ipf);
    417  1.65.2.3     skrll 		rtcache_free(&ipf->ipf_ro);
    418  1.65.2.3     skrll 		return ipf;
    419  1.65.2.3     skrll 	}
    420  1.65.2.3     skrll 
    421  1.65.2.3     skrll 	/*
    422  1.65.2.3     skrll 	 * This case is used in slow path(sysctl).
    423  1.65.2.3     skrll 	 * At first, remove invalid rtcache ipflow, and then remove TAILQ_LAST
    424  1.65.2.3     skrll 	 * ipflow if it is ensured least recently used by comparing last_uses.
    425  1.65.2.3     skrll 	 */
    426  1.65.2.3     skrll 	while (ipflow_inuse > ip_maxflows) {
    427  1.65.2.3     skrll 		struct ipflow *maybe_ipf = TAILQ_LAST(&ipflowlist, ipflowhead);
    428       1.3      matt 
    429  1.65.2.3     skrll 		TAILQ_FOREACH(ipf, &ipflowlist, ipf_list) {
    430       1.5   thorpej 			/*
    431       1.5   thorpej 			 * If this no longer points to a valid route
    432       1.5   thorpej 			 * reclaim it.
    433       1.5   thorpej 			 */
    434      1.50    dyoung 			if (rtcache_validate(&ipf->ipf_ro) == NULL)
    435       1.5   thorpej 				goto done;
    436       1.5   thorpej 			/*
    437       1.5   thorpej 			 * choose the one that's been least recently
    438       1.5   thorpej 			 * used or has had the least uses in the
    439       1.5   thorpej 			 * last 1.5 intervals.
    440       1.5   thorpej 			 */
    441  1.65.2.3     skrll 			if (ipf->ipf_timer < maybe_ipf->ipf_timer
    442  1.65.2.3     skrll 			    || ((ipf->ipf_timer == maybe_ipf->ipf_timer)
    443  1.65.2.3     skrll 				&& (ipf->ipf_last_uses + ipf->ipf_uses
    444  1.65.2.3     skrll 				    < maybe_ipf->ipf_last_uses + maybe_ipf->ipf_uses)))
    445       1.5   thorpej 				maybe_ipf = ipf;
    446       1.1      matt 		}
    447       1.3      matt 		ipf = maybe_ipf;
    448       1.3      matt 	    done:
    449       1.3      matt 		/*
    450       1.3      matt 		 * Remove the entry from the flow table.
    451       1.3      matt 		 */
    452  1.65.2.3     skrll 		IPFLOW_REMOVE(ipf->ipf_hashidx, ipf);
    453  1.65.2.2     skrll 
    454       1.3      matt 		ipflow_addstats(ipf);
    455      1.38     joerg 		rtcache_free(&ipf->ipf_ro);
    456       1.7   thorpej 		pool_put(&ipflow_pool, ipf);
    457       1.3      matt 		ipflow_inuse--;
    458       1.1      matt 	}
    459       1.3      matt 	return NULL;
    460       1.1      matt }
    461       1.1      matt 
    462  1.65.2.3     skrll static unsigned int ipflow_work_enqueued = 0;
    463  1.65.2.3     skrll 
    464  1.65.2.3     skrll static void
    465  1.65.2.3     skrll ipflow_slowtimo_work(struct work *wk, void *arg)
    466       1.1      matt {
    467      1.49    dyoung 	struct rtentry *rt;
    468       1.5   thorpej 	struct ipflow *ipf, *next_ipf;
    469      1.54   thorpej 	uint64_t *ips;
    470       1.2   thorpej 
    471  1.65.2.3     skrll 	/* We can allow enqueuing another work at this point */
    472  1.65.2.3     skrll 	atomic_swap_uint(&ipflow_work_enqueued, 0);
    473  1.65.2.3     skrll 
    474      1.55        ad 	mutex_enter(softnet_lock);
    475  1.65.2.2     skrll 	mutex_enter(&ipflow_lock);
    476      1.55        ad 	KERNEL_LOCK(1, NULL);
    477  1.65.2.3     skrll 	for (ipf = TAILQ_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
    478  1.65.2.3     skrll 		next_ipf = TAILQ_NEXT(ipf, ipf_list);
    479      1.37    dyoung 		if (PRT_SLOW_ISEXPIRED(ipf->ipf_timer) ||
    480      1.50    dyoung 		    (rt = rtcache_validate(&ipf->ipf_ro)) == NULL) {
    481       1.5   thorpej 			ipflow_free(ipf);
    482       1.5   thorpej 		} else {
    483       1.5   thorpej 			ipf->ipf_last_uses = ipf->ipf_uses;
    484      1.49    dyoung 			rt->rt_use += ipf->ipf_uses;
    485      1.54   thorpej 			ips = IP_STAT_GETREF();
    486      1.54   thorpej 			ips[IP_STAT_TOTAL] += ipf->ipf_uses;
    487      1.54   thorpej 			ips[IP_STAT_FORWARD] += ipf->ipf_uses;
    488      1.54   thorpej 			ips[IP_STAT_FASTFORWARD] += ipf->ipf_uses;
    489      1.54   thorpej 			IP_STAT_PUTREF();
    490       1.5   thorpej 			ipf->ipf_uses = 0;
    491       1.1      matt 		}
    492       1.1      matt 	}
    493      1.55        ad 	KERNEL_UNLOCK_ONE(NULL);
    494  1.65.2.2     skrll 	mutex_exit(&ipflow_lock);
    495      1.55        ad 	mutex_exit(softnet_lock);
    496       1.1      matt }
    497       1.1      matt 
    498       1.1      matt void
    499  1.65.2.3     skrll ipflow_slowtimo(void)
    500  1.65.2.3     skrll {
    501  1.65.2.3     skrll 
    502  1.65.2.3     skrll 	/* Avoid enqueuing another work when one is already enqueued */
    503  1.65.2.3     skrll 	if (atomic_swap_uint(&ipflow_work_enqueued, 1) == 1)
    504  1.65.2.3     skrll 		return;
    505  1.65.2.3     skrll 
    506  1.65.2.3     skrll 	workqueue_enqueue(ipflow_slowtimo_wq, &ipflow_slowtimo_wk, NULL);
    507  1.65.2.3     skrll }
    508  1.65.2.3     skrll 
    509  1.65.2.3     skrll void
    510      1.29     perry ipflow_create(const struct route *ro, struct mbuf *m)
    511       1.1      matt {
    512      1.51    dyoung 	const struct ip *const ip = mtod(m, const struct ip *);
    513       1.1      matt 	struct ipflow *ipf;
    514      1.45  liamjfoy 	size_t hash;
    515  1.65.2.2     skrll 
    516  1.65.2.2     skrll 	mutex_enter(&ipflow_lock);
    517       1.1      matt 
    518       1.1      matt 	/*
    519       1.1      matt 	 * Don't create cache entries for ICMP messages.
    520       1.1      matt 	 */
    521  1.65.2.2     skrll 	if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP) {
    522  1.65.2.2     skrll 		mutex_exit(&ipflow_lock);
    523       1.1      matt 		return;
    524  1.65.2.2     skrll 	}
    525      1.63     pooka 
    526      1.63     pooka 	KERNEL_LOCK(1, NULL);
    527      1.63     pooka 
    528       1.1      matt 	/*
    529      1.65       snj 	 * See if an existing flow struct exists.  If so remove it from its
    530       1.1      matt 	 * list and free the old route.  If not, try to malloc a new one
    531       1.1      matt 	 * (if we aren't at our limit).
    532       1.1      matt 	 */
    533       1.1      matt 	ipf = ipflow_lookup(ip);
    534       1.1      matt 	if (ipf == NULL) {
    535       1.3      matt 		if (ipflow_inuse >= ip_maxflows) {
    536      1.53   thorpej 			ipf = ipflow_reap(true);
    537       1.1      matt 		} else {
    538       1.7   thorpej 			ipf = pool_get(&ipflow_pool, PR_NOWAIT);
    539       1.1      matt 			if (ipf == NULL)
    540      1.63     pooka 				goto out;
    541       1.1      matt 			ipflow_inuse++;
    542       1.1      matt 		}
    543      1.39    dyoung 		memset(ipf, 0, sizeof(*ipf));
    544       1.1      matt 	} else {
    545  1.65.2.3     skrll 		IPFLOW_REMOVE(ipf->ipf_hashidx, ipf);
    546  1.65.2.2     skrll 
    547       1.1      matt 		ipflow_addstats(ipf);
    548      1.38     joerg 		rtcache_free(&ipf->ipf_ro);
    549       1.1      matt 		ipf->ipf_uses = ipf->ipf_last_uses = 0;
    550       1.1      matt 		ipf->ipf_errors = ipf->ipf_dropped = 0;
    551       1.1      matt 	}
    552       1.1      matt 
    553       1.1      matt 	/*
    554       1.1      matt 	 * Fill in the updated information.
    555       1.1      matt 	 */
    556      1.46    dyoung 	rtcache_copy(&ipf->ipf_ro, ro);
    557       1.1      matt 	ipf->ipf_dst = ip->ip_dst;
    558       1.1      matt 	ipf->ipf_src = ip->ip_src;
    559       1.1      matt 	ipf->ipf_tos = ip->ip_tos;
    560       1.5   thorpej 	PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
    561      1.60  liamjfoy 
    562       1.1      matt 	/*
    563       1.1      matt 	 * Insert into the approriate bucket of the flow table.
    564       1.1      matt 	 */
    565      1.45  liamjfoy 	hash = ipflow_hash(ip);
    566  1.65.2.3     skrll 	IPFLOW_INSERT(hash, ipf);
    567      1.63     pooka 
    568      1.63     pooka  out:
    569      1.63     pooka 	KERNEL_UNLOCK_ONE(NULL);
    570  1.65.2.2     skrll 	mutex_exit(&ipflow_lock);
    571      1.27       scw }
    572      1.27       scw 
    573      1.43  liamjfoy int
    574      1.43  liamjfoy ipflow_invalidate_all(int new_size)
    575      1.27       scw {
    576      1.27       scw 	struct ipflow *ipf, *next_ipf;
    577  1.65.2.2     skrll 	int error;
    578      1.27       scw 
    579      1.43  liamjfoy 	error = 0;
    580  1.65.2.2     skrll 
    581  1.65.2.2     skrll 	mutex_enter(&ipflow_lock);
    582  1.65.2.2     skrll 
    583  1.65.2.3     skrll 	for (ipf = TAILQ_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
    584  1.65.2.3     skrll 		next_ipf = TAILQ_NEXT(ipf, ipf_list);
    585      1.27       scw 		ipflow_free(ipf);
    586      1.27       scw 	}
    587      1.43  liamjfoy 
    588      1.43  liamjfoy 	if (new_size)
    589      1.64     rmind 		error = ipflow_reinit(new_size);
    590  1.65.2.2     skrll 
    591  1.65.2.2     skrll 	mutex_exit(&ipflow_lock);
    592      1.43  liamjfoy 
    593      1.43  liamjfoy 	return error;
    594       1.1      matt }
    595      1.64     rmind 
    596      1.64     rmind /*
    597      1.64     rmind  * sysctl helper routine for net.inet.ip.maxflows.
    598      1.64     rmind  */
    599      1.64     rmind static int
    600      1.64     rmind sysctl_net_inet_ip_maxflows(SYSCTLFN_ARGS)
    601      1.64     rmind {
    602      1.64     rmind 	int error;
    603      1.64     rmind 
    604      1.64     rmind 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
    605      1.64     rmind 	if (error || newp == NULL)
    606      1.64     rmind 		return (error);
    607      1.64     rmind 
    608      1.64     rmind 	mutex_enter(softnet_lock);
    609  1.65.2.2     skrll 	mutex_enter(&ipflow_lock);
    610      1.64     rmind 	KERNEL_LOCK(1, NULL);
    611      1.64     rmind 
    612      1.64     rmind 	ipflow_reap(false);
    613      1.64     rmind 
    614      1.64     rmind 	KERNEL_UNLOCK_ONE(NULL);
    615  1.65.2.2     skrll 	mutex_exit(&ipflow_lock);
    616      1.64     rmind 	mutex_exit(softnet_lock);
    617      1.64     rmind 
    618      1.64     rmind 	return (0);
    619      1.64     rmind }
    620      1.64     rmind 
    621      1.64     rmind static int
    622      1.64     rmind sysctl_net_inet_ip_hashsize(SYSCTLFN_ARGS)
    623      1.64     rmind {
    624      1.64     rmind 	int error, tmp;
    625      1.64     rmind 	struct sysctlnode node;
    626      1.64     rmind 
    627      1.64     rmind 	node = *rnode;
    628      1.64     rmind 	tmp = ip_hashsize;
    629      1.64     rmind 	node.sysctl_data = &tmp;
    630      1.64     rmind 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    631      1.64     rmind 	if (error || newp == NULL)
    632      1.64     rmind 		return (error);
    633      1.64     rmind 
    634      1.64     rmind 	if ((tmp & (tmp - 1)) == 0 && tmp != 0) {
    635      1.64     rmind 		/*
    636      1.64     rmind 		 * Can only fail due to malloc()
    637      1.64     rmind 		 */
    638      1.64     rmind 		mutex_enter(softnet_lock);
    639      1.64     rmind 		KERNEL_LOCK(1, NULL);
    640      1.64     rmind 
    641      1.64     rmind 		error = ipflow_invalidate_all(tmp);
    642      1.64     rmind 
    643      1.64     rmind 		KERNEL_UNLOCK_ONE(NULL);
    644      1.64     rmind 		mutex_exit(softnet_lock);
    645      1.64     rmind 
    646      1.64     rmind 	} else {
    647      1.64     rmind 		/*
    648      1.64     rmind 		 * EINVAL if not a power of 2
    649      1.64     rmind 	         */
    650      1.64     rmind 		error = EINVAL;
    651      1.64     rmind 	}
    652      1.64     rmind 
    653      1.64     rmind 	return error;
    654      1.64     rmind }
    655      1.64     rmind 
    656      1.64     rmind static void
    657      1.64     rmind ipflow_sysctl_init(struct sysctllog **clog)
    658      1.64     rmind {
    659      1.64     rmind 	sysctl_createv(clog, 0, NULL, NULL,
    660      1.64     rmind 		       CTLFLAG_PERMANENT,
    661      1.64     rmind 		       CTLTYPE_NODE, "inet",
    662      1.64     rmind 		       SYSCTL_DESCR("PF_INET related settings"),
    663      1.64     rmind 		       NULL, 0, NULL, 0,
    664      1.64     rmind 		       CTL_NET, PF_INET, CTL_EOL);
    665      1.64     rmind 	sysctl_createv(clog, 0, NULL, NULL,
    666      1.64     rmind 		       CTLFLAG_PERMANENT,
    667      1.64     rmind 		       CTLTYPE_NODE, "ip",
    668      1.64     rmind 		       SYSCTL_DESCR("IPv4 related settings"),
    669      1.64     rmind 		       NULL, 0, NULL, 0,
    670      1.64     rmind 		       CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
    671      1.64     rmind 
    672      1.64     rmind 	sysctl_createv(clog, 0, NULL, NULL,
    673      1.64     rmind 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    674      1.64     rmind 		       CTLTYPE_INT, "maxflows",
    675      1.64     rmind 		       SYSCTL_DESCR("Number of flows for fast forwarding"),
    676      1.64     rmind 		       sysctl_net_inet_ip_maxflows, 0, &ip_maxflows, 0,
    677      1.64     rmind 		       CTL_NET, PF_INET, IPPROTO_IP,
    678      1.64     rmind 		       IPCTL_MAXFLOWS, CTL_EOL);
    679      1.64     rmind 	sysctl_createv(clog, 0, NULL, NULL,
    680      1.64     rmind 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    681      1.64     rmind 			CTLTYPE_INT, "hashsize",
    682      1.64     rmind 			SYSCTL_DESCR("Size of hash table for fast forwarding (IPv4)"),
    683      1.64     rmind 			sysctl_net_inet_ip_hashsize, 0, &ip_hashsize, 0,
    684      1.64     rmind 			CTL_NET, PF_INET, IPPROTO_IP,
    685      1.64     rmind 			CTL_CREATE, CTL_EOL);
    686      1.64     rmind }
    687