Home | History | Annotate | Line # | Download | only in netinet
ip_flow.c revision 1.7
      1 /*	$NetBSD: ip_flow.c,v 1.7 1998/10/08 01:41:45 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by the 3am Software Foundry ("3am").  It was developed by Matt Thomas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/malloc.h>
     42 #include <sys/mbuf.h>
     43 #include <sys/domain.h>
     44 #include <sys/protosw.h>
     45 #include <sys/socket.h>
     46 #include <sys/socketvar.h>
     47 #include <sys/errno.h>
     48 #include <sys/time.h>
     49 #include <sys/kernel.h>
     50 #include <sys/proc.h>
     51 #include <sys/pool.h>
     52 
     53 #include <vm/vm.h>
     54 #include <sys/sysctl.h>
     55 
     56 #include <net/if.h>
     57 #include <net/if_dl.h>
     58 #include <net/route.h>
     59 #include <net/pfil.h>
     60 
     61 #include <netinet/in.h>
     62 #include <netinet/in_systm.h>
     63 #include <netinet/ip.h>
     64 #include <netinet/in_pcb.h>
     65 #include <netinet/in_var.h>
     66 #include <netinet/ip_var.h>
     67 
     68 struct pool ipflow_pool;
     69 
     70 LIST_HEAD(ipflowhead, ipflow);
     71 
     72 #define	IPFLOW_TIMER		(5 * PR_SLOWHZ)
     73 #define	IPFLOW_HASHSIZE		(1 << IPFLOW_HASHBITS)
     74 
     75 static struct ipflowhead ipflowtable[IPFLOW_HASHSIZE];
     76 static struct ipflowhead ipflowlist;
     77 static int ipflow_inuse;
     78 
     79 #define	IPFLOW_INSERT(bucket, ipf) \
     80 do { \
     81 	LIST_INSERT_HEAD((bucket), (ipf), ipf_hash); \
     82 	LIST_INSERT_HEAD(&ipflowlist, (ipf), ipf_list); \
     83 } while (0)
     84 
     85 #define	IPFLOW_REMOVE(ipf) \
     86 do { \
     87 	LIST_REMOVE((ipf), ipf_hash); \
     88 	LIST_REMOVE((ipf), ipf_list); \
     89 } while (0)
     90 
     91 #ifndef IPFLOW_MAX
     92 #define	IPFLOW_MAX		256
     93 #endif
     94 int ip_maxflows = IPFLOW_MAX;
     95 
     96 static unsigned
     97 ipflow_hash(
     98 	struct in_addr dst,
     99 	struct in_addr src,
    100 	unsigned tos)
    101 {
    102 	unsigned hash = tos;
    103 	int idx;
    104 	for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS)
    105 		hash += (dst.s_addr >> (32 - idx)) + (src.s_addr >> idx);
    106 	return hash & (IPFLOW_HASHSIZE-1);
    107 }
    108 
    109 static struct ipflow *
    110 ipflow_lookup(
    111 	const struct ip *ip)
    112 {
    113 	unsigned hash;
    114 	struct ipflow *ipf;
    115 
    116 	hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
    117 
    118 	ipf = LIST_FIRST(&ipflowtable[hash]);
    119 	while (ipf != NULL) {
    120 		if (ip->ip_dst.s_addr == ipf->ipf_dst.s_addr
    121 		    && ip->ip_src.s_addr == ipf->ipf_src.s_addr
    122 		    && ip->ip_tos == ipf->ipf_tos)
    123 			break;
    124 		ipf = LIST_NEXT(ipf, ipf_hash);
    125 	}
    126 	return ipf;
    127 }
    128 
    129 void
    130 ipflow_init()
    131 {
    132 	int i;
    133 
    134 	pool_init(&ipflow_pool, sizeof(struct ipflow), 0, 0, 0, "ipflowpl",
    135 	    0, NULL, NULL, M_IPFLOW);
    136 
    137 	LIST_INIT(&ipflowlist);
    138 	for (i = 0; i < IPFLOW_HASHSIZE; i++)
    139 		LIST_INIT(&ipflowtable[i]);
    140 }
    141 
    142 int
    143 ipflow_fastforward(
    144 	struct mbuf *m)
    145 {
    146 	struct ip *ip;
    147 	struct ipflow *ipf;
    148 	struct rtentry *rt;
    149 	int error;
    150 	int iplen;
    151 
    152 	/*
    153 	 * Are we forwarding packets?  Big enough for an IP packet?
    154 	 */
    155 	if (!ipforwarding || ipflow_inuse == 0 || m->m_len < sizeof(struct ip))
    156 		return 0;
    157 	/*
    158 	 * IP header with no option and valid version and length
    159 	 */
    160 	ip = mtod(m, struct ip *);
    161 	iplen = ntohs(ip->ip_len);
    162 	if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2) ||
    163 	    iplen > m->m_pkthdr.len)
    164 		return 0;
    165 	/*
    166 	 * Find a flow.
    167 	 */
    168 	if ((ipf = ipflow_lookup(ip)) == NULL)
    169 		return 0;
    170 
    171 	/*
    172 	 * Veryify the IP header checksum.
    173 	 */
    174 	if (in_cksum(m, sizeof(struct ip)) != 0)
    175 		return 0;
    176 
    177 	/*
    178 	 * Route and interface still up?
    179 	 */
    180 	rt = ipf->ipf_ro.ro_rt;
    181 	if ((rt->rt_flags & RTF_UP) == 0 ||
    182 	    (rt->rt_ifp->if_flags & IFF_UP) == 0)
    183 		return 0;
    184 
    185 	/*
    186 	 * Packet size OK?  TTL?
    187 	 */
    188 	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
    189 		return 0;
    190 
    191 	/*
    192 	 * Everything checks out and so we can forward this packet.
    193 	 * Modify the TTL and incrementally change the checksum.
    194 	 * On little endian machine, the TTL is in LSB position
    195 	 * (so we can simply add) while on big-endian it's in the
    196 	 * MSB position (so we have to do two calculation; the first
    197 	 * is the add and second is to wrap the results into 17 bits,
    198 	 * 16 bits and a carry).
    199 	 */
    200 	ip->ip_ttl -= IPTTLDEC;
    201 	if (ip->ip_sum >= htons(0xffff - (IPTTLDEC << 8))) {
    202 		ip->ip_sum += htons(IPTTLDEC << 8) + 1;
    203 	} else {
    204 		ip->ip_sum += htons(IPTTLDEC << 8);
    205 	}
    206 
    207 	/*
    208 	 * Trim the packet in case it's too long..
    209 	 */
    210 	if (m->m_pkthdr.len > iplen) {
    211 		if (m->m_len == m->m_pkthdr.len) {
    212 			m->m_len = iplen;
    213 			m->m_pkthdr.len = iplen;
    214 		} else
    215 			m_adj(m, iplen - m->m_pkthdr.len);
    216 	}
    217 
    218 	/*
    219 	 * Send the packet on it's way.  All we can get back is ENOBUFS
    220 	 */
    221 	ipf->ipf_uses++;
    222 	PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
    223 	if ((error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m,
    224 	    &ipf->ipf_ro.ro_dst, rt)) != 0) {
    225 		if (error == ENOBUFS)
    226 			ipf->ipf_dropped++;
    227 		else
    228 			ipf->ipf_errors++;
    229 	}
    230 	return 1;
    231 }
    232 
    233 static void
    235 ipflow_addstats(
    236 	struct ipflow *ipf)
    237 {
    238 	ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
    239 	ipstat.ips_cantforward += ipf->ipf_errors + ipf->ipf_dropped;
    240 	ipstat.ips_forward += ipf->ipf_uses;
    241 	ipstat.ips_fastforward += ipf->ipf_uses;
    242 }
    243 
    244 static void
    245 ipflow_free(
    246 	struct ipflow *ipf)
    247 {
    248 	int s;
    249 	/*
    250 	 * Remove the flow from the hash table (at elevated IPL).
    251 	 * Once it's off the list, we can deal with it at normal
    252 	 * network IPL.
    253 	 */
    254 	s = splimp();
    255 	IPFLOW_REMOVE(ipf);
    256 	splx(s);
    257 	ipflow_addstats(ipf);
    258 	RTFREE(ipf->ipf_ro.ro_rt);
    259 	ipflow_inuse--;
    260 	pool_put(&ipflow_pool, ipf);
    261 }
    262 
    263 struct ipflow *
    264 ipflow_reap(
    265 	int just_one)
    266 {
    267 	while (just_one || ipflow_inuse > ip_maxflows) {
    268 		struct ipflow *ipf, *maybe_ipf = NULL;
    269 		int s;
    270 
    271 		ipf = LIST_FIRST(&ipflowlist);
    272 		while (ipf != NULL) {
    273 			/*
    274 			 * If this no longer points to a valid route
    275 			 * reclaim it.
    276 			 */
    277 			if ((ipf->ipf_ro.ro_rt->rt_flags & RTF_UP) == 0)
    278 				goto done;
    279 			/*
    280 			 * choose the one that's been least recently
    281 			 * used or has had the least uses in the
    282 			 * last 1.5 intervals.
    283 			 */
    284 			if (maybe_ipf == NULL ||
    285 			    ipf->ipf_timer < maybe_ipf->ipf_timer ||
    286 			    (ipf->ipf_timer == maybe_ipf->ipf_timer &&
    287 			     ipf->ipf_last_uses + ipf->ipf_uses <
    288 			         maybe_ipf->ipf_last_uses +
    289 			         maybe_ipf->ipf_uses))
    290 				maybe_ipf = ipf;
    291 			ipf = LIST_NEXT(ipf, ipf_list);
    292 		}
    293 		ipf = maybe_ipf;
    294 	    done:
    295 		/*
    296 		 * Remove the entry from the flow table.
    297 		 */
    298 		s = splimp();
    299 		IPFLOW_REMOVE(ipf);
    300 		splx(s);
    301 		ipflow_addstats(ipf);
    302 		RTFREE(ipf->ipf_ro.ro_rt);
    303 		if (just_one)
    304 			return ipf;
    305 		pool_put(&ipflow_pool, ipf);
    306 		ipflow_inuse--;
    307 	}
    308 	return NULL;
    309 }
    310 
    311 void
    312 ipflow_slowtimo(
    313 	void)
    314 {
    315 	struct ipflow *ipf, *next_ipf;
    316 
    317 	ipf = LIST_FIRST(&ipflowlist);
    318 	while (ipf != NULL) {
    319 		next_ipf = LIST_NEXT(ipf, ipf_list);
    320 		if (PRT_SLOW_ISEXPIRED(ipf->ipf_timer)) {
    321 			ipflow_free(ipf);
    322 		} else {
    323 			ipf->ipf_last_uses = ipf->ipf_uses;
    324 			ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
    325 			ipstat.ips_forward += ipf->ipf_uses;
    326 			ipstat.ips_fastforward += ipf->ipf_uses;
    327 			ipf->ipf_uses = 0;
    328 		}
    329 		ipf = next_ipf;
    330 	}
    331 }
    332 
    333 void
    334 ipflow_create(
    335 	const struct route *ro,
    336 	struct mbuf *m)
    337 {
    338 	const struct ip *const ip = mtod(m, struct ip *);
    339 	struct ipflow *ipf;
    340 	unsigned hash;
    341 	int s;
    342 
    343 	/*
    344 	 * Don't create cache entries for ICMP messages.
    345 	 */
    346 	if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP)
    347 		return;
    348 	/*
    349 	 * See if an existing flow struct exists.  If so remove it from it's
    350 	 * list and free the old route.  If not, try to malloc a new one
    351 	 * (if we aren't at our limit).
    352 	 */
    353 	ipf = ipflow_lookup(ip);
    354 	if (ipf == NULL) {
    355 		if (ipflow_inuse >= ip_maxflows) {
    356 			ipf = ipflow_reap(1);
    357 		} else {
    358 			ipf = pool_get(&ipflow_pool, PR_NOWAIT);
    359 			if (ipf == NULL)
    360 				return;
    361 			ipflow_inuse++;
    362 		}
    363 		bzero((caddr_t) ipf, sizeof(*ipf));
    364 	} else {
    365 		s = splimp();
    366 		IPFLOW_REMOVE(ipf);
    367 		splx(s);
    368 		ipflow_addstats(ipf);
    369 		RTFREE(ipf->ipf_ro.ro_rt);
    370 		ipf->ipf_uses = ipf->ipf_last_uses = 0;
    371 		ipf->ipf_errors = ipf->ipf_dropped = 0;
    372 	}
    373 
    374 	/*
    375 	 * Fill in the updated information.
    376 	 */
    377 	ipf->ipf_ro = *ro;
    378 	ro->ro_rt->rt_refcnt++;
    379 	ipf->ipf_dst = ip->ip_dst;
    380 	ipf->ipf_src = ip->ip_src;
    381 	ipf->ipf_tos = ip->ip_tos;
    382 	PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
    383 	ipf->ipf_start = time.tv_sec;
    384 	/*
    385 	 * Insert into the approriate bucket of the flow table.
    386 	 */
    387 	hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
    388 	s = splimp();
    389 	IPFLOW_INSERT(&ipflowtable[hash], ipf);
    390 	splx(s);
    391 }
    392