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