Home | History | Annotate | Line # | Download | only in netinet
ip_flow.c revision 1.40
      1 /*	$NetBSD: ip_flow.c,v 1.40 2007/02/17 22:34:11 dyoung 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/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: ip_flow.c,v 1.40 2007/02/17 22:34:11 dyoung Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/malloc.h>
     45 #include <sys/mbuf.h>
     46 #include <sys/domain.h>
     47 #include <sys/protosw.h>
     48 #include <sys/socket.h>
     49 #include <sys/socketvar.h>
     50 #include <sys/errno.h>
     51 #include <sys/time.h>
     52 #include <sys/kernel.h>
     53 #include <sys/pool.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_route.h>
     66 #include <netinet/in_var.h>
     67 #include <netinet/ip_var.h>
     68 
     69 POOL_INIT(ipflow_pool, sizeof(struct ipflow), 0, 0, 0, "ipflowpl", NULL);
     70 
     71 LIST_HEAD(ipflowhead, ipflow);
     72 
     73 #define	IPFLOW_TIMER		(5 * PR_SLOWHZ)
     74 #define	IPFLOW_HASHSIZE		(1 << IPFLOW_HASHBITS)
     75 
     76 static struct ipflowhead ipflowtable[IPFLOW_HASHSIZE];
     77 static struct ipflowhead ipflowlist;
     78 static int ipflow_inuse;
     79 
     80 #define	IPFLOW_INSERT(bucket, ipf) \
     81 do { \
     82 	LIST_INSERT_HEAD((bucket), (ipf), ipf_hash); \
     83 	LIST_INSERT_HEAD(&ipflowlist, (ipf), ipf_list); \
     84 } while (/*CONSTCOND*/ 0)
     85 
     86 #define	IPFLOW_REMOVE(ipf) \
     87 do { \
     88 	LIST_REMOVE((ipf), ipf_hash); \
     89 	LIST_REMOVE((ipf), ipf_list); \
     90 } while (/*CONSTCOND*/ 0)
     91 
     92 #ifndef IPFLOW_MAX
     93 #define	IPFLOW_MAX		256
     94 #endif
     95 int ip_maxflows = IPFLOW_MAX;
     96 
     97 static unsigned
     98 ipflow_hash(struct in_addr dst,	struct in_addr src, unsigned tos)
     99 {
    100 	unsigned hash = tos;
    101 	int idx;
    102 	for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS)
    103 		hash += (dst.s_addr >> (32 - idx)) + (src.s_addr >> idx);
    104 	return hash & (IPFLOW_HASHSIZE-1);
    105 }
    106 
    107 static struct ipflow *
    108 ipflow_lookup(const struct ip *ip)
    109 {
    110 	unsigned hash;
    111 	struct ipflow *ipf;
    112 
    113 	hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
    114 
    115 	LIST_FOREACH(ipf, &ipflowtable[hash], ipf_hash) {
    116 		if (ip->ip_dst.s_addr == ipf->ipf_dst.s_addr
    117 		    && ip->ip_src.s_addr == ipf->ipf_src.s_addr
    118 		    && ip->ip_tos == ipf->ipf_tos)
    119 			break;
    120 	}
    121 	return ipf;
    122 }
    123 
    124 void
    125 ipflow_init(void)
    126 {
    127 	int i;
    128 
    129 	LIST_INIT(&ipflowlist);
    130 	for (i = 0; i < IPFLOW_HASHSIZE; i++)
    131 		LIST_INIT(&ipflowtable[i]);
    132 }
    133 
    134 int
    135 ipflow_fastforward(struct mbuf *m)
    136 {
    137 	struct ip *ip, ip_store;
    138 	struct ipflow *ipf;
    139 	struct rtentry *rt;
    140 	const struct sockaddr *dst;
    141 	int error;
    142 	int iplen;
    143 
    144 	/*
    145 	 * Are we forwarding packets?  Big enough for an IP packet?
    146 	 */
    147 	if (!ipforwarding || ipflow_inuse == 0 || m->m_len < sizeof(struct ip))
    148 		return 0;
    149 
    150 	/*
    151 	 * Was packet received as a link-level multicast or broadcast?
    152 	 * If so, don't try to fast forward..
    153 	 */
    154 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0)
    155 		return 0;
    156 
    157 	/*
    158 	 * IP header with no option and valid version and length
    159 	 */
    160 	if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)))
    161 		ip = mtod(m, struct ip *);
    162 	else {
    163 		memcpy(&ip_store, mtod(m, caddr_t), sizeof(ip_store));
    164 		ip = &ip_store;
    165 	}
    166 	iplen = ntohs(ip->ip_len);
    167 	if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2) ||
    168 	    iplen < sizeof(struct ip) || iplen > m->m_pkthdr.len)
    169 		return 0;
    170 	/*
    171 	 * Find a flow.
    172 	 */
    173 	if ((ipf = ipflow_lookup(ip)) == NULL)
    174 		return 0;
    175 
    176 	/*
    177 	 * Verify the IP header checksum.
    178 	 */
    179 	switch (m->m_pkthdr.csum_flags &
    180 		((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
    181 		 M_CSUM_IPv4_BAD)) {
    182 	case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
    183 		return (0);
    184 
    185 	case M_CSUM_IPv4:
    186 		/* Checksum was okay. */
    187 		break;
    188 
    189 	default:
    190 		/* Must compute it ourselves. */
    191 		if (in_cksum(m, sizeof(struct ip)) != 0)
    192 			return (0);
    193 		break;
    194 	}
    195 
    196 	/*
    197 	 * Route and interface still up?
    198 	 */
    199 	rtcache_check(&ipf->ipf_ro);
    200 	rt = ipf->ipf_ro.ro_rt;
    201 	if (rt == NULL || (rt->rt_ifp->if_flags & IFF_UP) == 0)
    202 		return 0;
    203 
    204 	/*
    205 	 * Packet size OK?  TTL?
    206 	 */
    207 	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
    208 		return 0;
    209 
    210 	/*
    211 	 * Clear any in-bound checksum flags for this packet.
    212 	 */
    213 	m->m_pkthdr.csum_flags = 0;
    214 
    215 	/*
    216 	 * Everything checks out and so we can forward this packet.
    217 	 * Modify the TTL and incrementally change the checksum.
    218 	 *
    219 	 * This method of adding the checksum works on either endian CPU.
    220 	 * If htons() is inlined, all the arithmetic is folded; otherwise
    221 	 * the htons()s are combined by CSE due to the const attribute.
    222 	 *
    223 	 * Don't bother using HW checksumming here -- the incremental
    224 	 * update is pretty fast.
    225 	 */
    226 	ip->ip_ttl -= IPTTLDEC;
    227 	if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
    228 		ip->ip_sum -= ~htons(IPTTLDEC << 8);
    229 	else
    230 		ip->ip_sum += htons(IPTTLDEC << 8);
    231 
    232 	/*
    233 	 * Done modifying the header; copy it back, if necessary.
    234 	 */
    235 	if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0)
    236 		memcpy(mtod(m, caddr_t), &ip_store, sizeof(ip_store));
    237 
    238 	/*
    239 	 * Trim the packet in case it's too long..
    240 	 */
    241 	if (m->m_pkthdr.len > iplen) {
    242 		if (m->m_len == m->m_pkthdr.len) {
    243 			m->m_len = iplen;
    244 			m->m_pkthdr.len = iplen;
    245 		} else
    246 			m_adj(m, iplen - m->m_pkthdr.len);
    247 	}
    248 
    249 	/*
    250 	 * Send the packet on it's way.  All we can get back is ENOBUFS
    251 	 */
    252 	ipf->ipf_uses++;
    253 	PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
    254 
    255 	if (rt->rt_flags & RTF_GATEWAY)
    256 		dst = rt->rt_gateway;
    257 	else
    258 		dst = rtcache_getdst(&ipf->ipf_ro);
    259 
    260 	if ((error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m, dst, rt)) != 0) {
    261 		if (error == ENOBUFS)
    262 			ipf->ipf_dropped++;
    263 		else
    264 			ipf->ipf_errors++;
    265 	}
    266 	return 1;
    267 }
    268 
    269 static void
    271 ipflow_addstats(struct ipflow *ipf)
    272 {
    273 	rtcache_check(&ipf->ipf_ro);
    274 	if (ipf->ipf_ro.ro_rt != NULL)
    275 		ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
    276 	ipstat.ips_cantforward += ipf->ipf_errors + ipf->ipf_dropped;
    277 	ipstat.ips_total += ipf->ipf_uses;
    278 	ipstat.ips_forward += ipf->ipf_uses;
    279 	ipstat.ips_fastforward += ipf->ipf_uses;
    280 }
    281 
    282 static void
    283 ipflow_free(struct ipflow *ipf)
    284 {
    285 	int s;
    286 	/*
    287 	 * Remove the flow from the hash table (at elevated IPL).
    288 	 * Once it's off the list, we can deal with it at normal
    289 	 * network IPL.
    290 	 */
    291 	s = splnet();
    292 	IPFLOW_REMOVE(ipf);
    293 	splx(s);
    294 	ipflow_addstats(ipf);
    295 	rtcache_free(&ipf->ipf_ro);
    296 	ipflow_inuse--;
    297 	s = splnet();
    298 	pool_put(&ipflow_pool, ipf);
    299 	splx(s);
    300 }
    301 
    302 struct ipflow *
    303 ipflow_reap(int just_one)
    304 {
    305 	while (just_one || ipflow_inuse > ip_maxflows) {
    306 		struct ipflow *ipf, *maybe_ipf = NULL;
    307 		int s;
    308 
    309 		ipf = LIST_FIRST(&ipflowlist);
    310 		while (ipf != NULL) {
    311 			/*
    312 			 * If this no longer points to a valid route
    313 			 * reclaim it.
    314 			 */
    315 			rtcache_check(&ipf->ipf_ro);
    316 			if (ipf->ipf_ro.ro_rt == NULL)
    317 				goto done;
    318 			/*
    319 			 * choose the one that's been least recently
    320 			 * used or has had the least uses in the
    321 			 * last 1.5 intervals.
    322 			 */
    323 			if (maybe_ipf == NULL ||
    324 			    ipf->ipf_timer < maybe_ipf->ipf_timer ||
    325 			    (ipf->ipf_timer == maybe_ipf->ipf_timer &&
    326 			     ipf->ipf_last_uses + ipf->ipf_uses <
    327 			         maybe_ipf->ipf_last_uses +
    328 			         maybe_ipf->ipf_uses))
    329 				maybe_ipf = ipf;
    330 			ipf = LIST_NEXT(ipf, ipf_list);
    331 		}
    332 		ipf = maybe_ipf;
    333 	    done:
    334 		/*
    335 		 * Remove the entry from the flow table.
    336 		 */
    337 		s = splnet();
    338 		IPFLOW_REMOVE(ipf);
    339 		splx(s);
    340 		ipflow_addstats(ipf);
    341 		rtcache_free(&ipf->ipf_ro);
    342 		if (just_one)
    343 			return ipf;
    344 		pool_put(&ipflow_pool, ipf);
    345 		ipflow_inuse--;
    346 	}
    347 	return NULL;
    348 }
    349 
    350 void
    351 ipflow_slowtimo(void)
    352 {
    353 	struct ipflow *ipf, *next_ipf;
    354 
    355 	for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
    356 		next_ipf = LIST_NEXT(ipf, ipf_list);
    357 		rtcache_check(&ipf->ipf_ro);
    358 		if (PRT_SLOW_ISEXPIRED(ipf->ipf_timer) ||
    359 		    ipf->ipf_ro.ro_rt == NULL) {
    360 			ipflow_free(ipf);
    361 		} else {
    362 			ipf->ipf_last_uses = ipf->ipf_uses;
    363 			ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
    364 			ipstat.ips_total += ipf->ipf_uses;
    365 			ipstat.ips_forward += ipf->ipf_uses;
    366 			ipstat.ips_fastforward += ipf->ipf_uses;
    367 			ipf->ipf_uses = 0;
    368 		}
    369 	}
    370 }
    371 
    372 void
    373 ipflow_create(const struct route *ro, struct mbuf *m)
    374 {
    375 	const struct ip *const ip = mtod(m, struct ip *);
    376 	struct ipflow *ipf;
    377 	unsigned hash;
    378 	int s;
    379 
    380 	/*
    381 	 * Don't create cache entries for ICMP messages.
    382 	 */
    383 	if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP)
    384 		return;
    385 	/*
    386 	 * See if an existing flow struct exists.  If so remove it from it's
    387 	 * list and free the old route.  If not, try to malloc a new one
    388 	 * (if we aren't at our limit).
    389 	 */
    390 	ipf = ipflow_lookup(ip);
    391 	if (ipf == NULL) {
    392 		if (ipflow_inuse >= ip_maxflows) {
    393 			ipf = ipflow_reap(1);
    394 		} else {
    395 			s = splnet();
    396 			ipf = pool_get(&ipflow_pool, PR_NOWAIT);
    397 			splx(s);
    398 			if (ipf == NULL)
    399 				return;
    400 			ipflow_inuse++;
    401 		}
    402 		memset(ipf, 0, sizeof(*ipf));
    403 	} else {
    404 		s = splnet();
    405 		IPFLOW_REMOVE(ipf);
    406 		splx(s);
    407 		ipflow_addstats(ipf);
    408 		rtcache_free(&ipf->ipf_ro);
    409 		ipf->ipf_uses = ipf->ipf_last_uses = 0;
    410 		ipf->ipf_errors = ipf->ipf_dropped = 0;
    411 	}
    412 
    413 	/*
    414 	 * Fill in the updated information.
    415 	 */
    416 	rtcache_copy(&ipf->ipf_ro, ro, sizeof(ipf->ipf_ro));
    417 	ipf->ipf_dst = ip->ip_dst;
    418 	ipf->ipf_src = ip->ip_src;
    419 	ipf->ipf_tos = ip->ip_tos;
    420 	PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
    421 	ipf->ipf_start = time_uptime;
    422 	/*
    423 	 * Insert into the approriate bucket of the flow table.
    424 	 */
    425 	hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
    426 	s = splnet();
    427 	IPFLOW_INSERT(&ipflowtable[hash], ipf);
    428 	splx(s);
    429 }
    430 
    431 void
    432 ipflow_invalidate_all(void)
    433 {
    434 	struct ipflow *ipf, *next_ipf;
    435 	int s;
    436 
    437 	s = splnet();
    438 	for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
    439 		next_ipf = LIST_NEXT(ipf, ipf_list);
    440 		ipflow_free(ipf);
    441 	}
    442 	splx(s);
    443 }
    444