Home | History | Annotate | Line # | Download | only in netinet
ip_flow.c revision 1.16
      1 /*	$NetBSD: ip_flow.c,v 1.16 2000/06/30 19:43:53 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 <uvm/uvm_extern.h>
     54 
     55 #include <sys/sysctl.h>
     56 
     57 #include <net/if.h>
     58 #include <net/if_dl.h>
     59 #include <net/route.h>
     60 #include <net/pfil.h>
     61 
     62 #include <netinet/in.h>
     63 #include <netinet/in_systm.h>
     64 #include <netinet/ip.h>
     65 #include <netinet/in_pcb.h>
     66 #include <netinet/in_var.h>
     67 #include <netinet/ip_var.h>
     68 
     69 struct pool ipflow_pool;
     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 (0)
     85 
     86 #define	IPFLOW_REMOVE(ipf) \
     87 do { \
     88 	LIST_REMOVE((ipf), ipf_hash); \
     89 	LIST_REMOVE((ipf), ipf_list); \
     90 } while (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(
     99 	struct in_addr dst,
    100 	struct in_addr src,
    101 	unsigned tos)
    102 {
    103 	unsigned hash = tos;
    104 	int idx;
    105 	for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS)
    106 		hash += (dst.s_addr >> (32 - idx)) + (src.s_addr >> idx);
    107 	return hash & (IPFLOW_HASHSIZE-1);
    108 }
    109 
    110 static struct ipflow *
    111 ipflow_lookup(
    112 	const struct ip *ip)
    113 {
    114 	unsigned hash;
    115 	struct ipflow *ipf;
    116 
    117 	hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
    118 
    119 	ipf = LIST_FIRST(&ipflowtable[hash]);
    120 	while (ipf != NULL) {
    121 		if (ip->ip_dst.s_addr == ipf->ipf_dst.s_addr
    122 		    && ip->ip_src.s_addr == ipf->ipf_src.s_addr
    123 		    && ip->ip_tos == ipf->ipf_tos)
    124 			break;
    125 		ipf = LIST_NEXT(ipf, ipf_hash);
    126 	}
    127 	return ipf;
    128 }
    129 
    130 void
    131 ipflow_init()
    132 {
    133 	int i;
    134 
    135 	pool_init(&ipflow_pool, sizeof(struct ipflow), 0, 0, 0, "ipflowpl",
    136 	    0, NULL, NULL, M_IPFLOW);
    137 
    138 	LIST_INIT(&ipflowlist);
    139 	for (i = 0; i < IPFLOW_HASHSIZE; i++)
    140 		LIST_INIT(&ipflowtable[i]);
    141 }
    142 
    143 int
    144 ipflow_fastforward(
    145 	struct mbuf *m)
    146 {
    147 	struct ip *ip;
    148 	struct ipflow *ipf;
    149 	struct rtentry *rt;
    150 	struct sockaddr *dst;
    151 	int error;
    152 	int iplen;
    153 
    154 	/*
    155 	 * Are we forwarding packets?  Big enough for an IP packet?
    156 	 */
    157 	if (!ipforwarding || ipflow_inuse == 0 || m->m_len < sizeof(struct ip))
    158 		return 0;
    159 
    160 	/*
    161 	 * Was packet recieved as a link-level multicast or broadcast?
    162 	 * If so, don't try to fast forward..
    163 	 */
    164 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0)
    165 		return 0;
    166 
    167 	/*
    168 	 * IP header with no option and valid version and length
    169 	 */
    170 	ip = mtod(m, struct ip *);
    171 	iplen = ntohs(ip->ip_len);
    172 	if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2) ||
    173 	    iplen < sizeof(struct ip) || iplen > m->m_pkthdr.len)
    174 		return 0;
    175 	/*
    176 	 * Find a flow.
    177 	 */
    178 	if ((ipf = ipflow_lookup(ip)) == NULL)
    179 		return 0;
    180 
    181 	/*
    182 	 * Veryify the IP header checksum.
    183 	 */
    184 	if (in_cksum(m, sizeof(struct ip)) != 0)
    185 		return 0;
    186 
    187 	/*
    188 	 * Route and interface still up?
    189 	 */
    190 	rt = ipf->ipf_ro.ro_rt;
    191 	if ((rt->rt_flags & RTF_UP) == 0 ||
    192 	    (rt->rt_ifp->if_flags & IFF_UP) == 0)
    193 		return 0;
    194 
    195 	/*
    196 	 * Packet size OK?  TTL?
    197 	 */
    198 	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
    199 		return 0;
    200 
    201 	/*
    202 	 * Everything checks out and so we can forward this packet.
    203 	 * Modify the TTL and incrementally change the checksum.
    204 	 *
    205 	 * This method of adding the checksum works on either endian CPU.
    206 	 * If htons() is inlined, all the arithmetic is folded; otherwise
    207 	 * the htons()s are combined by CSE due to the __const__ attribute.
    208 	 */
    209 	ip->ip_ttl -= IPTTLDEC;
    210 	if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
    211 		ip->ip_sum -= ~htons(IPTTLDEC << 8);
    212 	else
    213 		ip->ip_sum += htons(IPTTLDEC << 8);
    214 
    215 	/*
    216 	 * Trim the packet in case it's too long..
    217 	 */
    218 	if (m->m_pkthdr.len > iplen) {
    219 		if (m->m_len == m->m_pkthdr.len) {
    220 			m->m_len = iplen;
    221 			m->m_pkthdr.len = iplen;
    222 		} else
    223 			m_adj(m, iplen - m->m_pkthdr.len);
    224 	}
    225 
    226 	/*
    227 	 * Send the packet on it's way.  All we can get back is ENOBUFS
    228 	 */
    229 	ipf->ipf_uses++;
    230 	PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
    231 
    232 	if (rt->rt_flags & RTF_GATEWAY)
    233 		dst = rt->rt_gateway;
    234 	else
    235 		dst = &ipf->ipf_ro.ro_dst;
    236 
    237 	if ((error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m, dst, rt)) != 0) {
    238 		if (error == ENOBUFS)
    239 			ipf->ipf_dropped++;
    240 		else
    241 			ipf->ipf_errors++;
    242 	}
    243 	return 1;
    244 }
    245 
    246 static void
    248 ipflow_addstats(
    249 	struct ipflow *ipf)
    250 {
    251 	ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
    252 	ipstat.ips_cantforward += ipf->ipf_errors + ipf->ipf_dropped;
    253 	ipstat.ips_forward += ipf->ipf_uses;
    254 	ipstat.ips_fastforward += ipf->ipf_uses;
    255 }
    256 
    257 static void
    258 ipflow_free(
    259 	struct ipflow *ipf)
    260 {
    261 	int s;
    262 	/*
    263 	 * Remove the flow from the hash table (at elevated IPL).
    264 	 * Once it's off the list, we can deal with it at normal
    265 	 * network IPL.
    266 	 */
    267 	s = splimp();
    268 	IPFLOW_REMOVE(ipf);
    269 	splx(s);
    270 	ipflow_addstats(ipf);
    271 	RTFREE(ipf->ipf_ro.ro_rt);
    272 	ipflow_inuse--;
    273 	pool_put(&ipflow_pool, ipf);
    274 }
    275 
    276 struct ipflow *
    277 ipflow_reap(
    278 	int just_one)
    279 {
    280 	while (just_one || ipflow_inuse > ip_maxflows) {
    281 		struct ipflow *ipf, *maybe_ipf = NULL;
    282 		int s;
    283 
    284 		ipf = LIST_FIRST(&ipflowlist);
    285 		while (ipf != NULL) {
    286 			/*
    287 			 * If this no longer points to a valid route
    288 			 * reclaim it.
    289 			 */
    290 			if ((ipf->ipf_ro.ro_rt->rt_flags & RTF_UP) == 0)
    291 				goto done;
    292 			/*
    293 			 * choose the one that's been least recently
    294 			 * used or has had the least uses in the
    295 			 * last 1.5 intervals.
    296 			 */
    297 			if (maybe_ipf == NULL ||
    298 			    ipf->ipf_timer < maybe_ipf->ipf_timer ||
    299 			    (ipf->ipf_timer == maybe_ipf->ipf_timer &&
    300 			     ipf->ipf_last_uses + ipf->ipf_uses <
    301 			         maybe_ipf->ipf_last_uses +
    302 			         maybe_ipf->ipf_uses))
    303 				maybe_ipf = ipf;
    304 			ipf = LIST_NEXT(ipf, ipf_list);
    305 		}
    306 		ipf = maybe_ipf;
    307 	    done:
    308 		/*
    309 		 * Remove the entry from the flow table.
    310 		 */
    311 		s = splimp();
    312 		IPFLOW_REMOVE(ipf);
    313 		splx(s);
    314 		ipflow_addstats(ipf);
    315 		RTFREE(ipf->ipf_ro.ro_rt);
    316 		if (just_one)
    317 			return ipf;
    318 		pool_put(&ipflow_pool, ipf);
    319 		ipflow_inuse--;
    320 	}
    321 	return NULL;
    322 }
    323 
    324 void
    325 ipflow_slowtimo(
    326 	void)
    327 {
    328 	struct ipflow *ipf, *next_ipf;
    329 
    330 	ipf = LIST_FIRST(&ipflowlist);
    331 	while (ipf != NULL) {
    332 		next_ipf = LIST_NEXT(ipf, ipf_list);
    333 		if (PRT_SLOW_ISEXPIRED(ipf->ipf_timer)) {
    334 			ipflow_free(ipf);
    335 		} else {
    336 			ipf->ipf_last_uses = ipf->ipf_uses;
    337 			ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
    338 			ipstat.ips_forward += ipf->ipf_uses;
    339 			ipstat.ips_fastforward += ipf->ipf_uses;
    340 			ipf->ipf_uses = 0;
    341 		}
    342 		ipf = next_ipf;
    343 	}
    344 }
    345 
    346 void
    347 ipflow_create(
    348 	const struct route *ro,
    349 	struct mbuf *m)
    350 {
    351 	const struct ip *const ip = mtod(m, struct ip *);
    352 	struct ipflow *ipf;
    353 	unsigned hash;
    354 	int s;
    355 
    356 	/*
    357 	 * Don't create cache entries for ICMP messages.
    358 	 */
    359 	if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP)
    360 		return;
    361 	/*
    362 	 * See if an existing flow struct exists.  If so remove it from it's
    363 	 * list and free the old route.  If not, try to malloc a new one
    364 	 * (if we aren't at our limit).
    365 	 */
    366 	ipf = ipflow_lookup(ip);
    367 	if (ipf == NULL) {
    368 		if (ipflow_inuse >= ip_maxflows) {
    369 			ipf = ipflow_reap(1);
    370 		} else {
    371 			ipf = pool_get(&ipflow_pool, PR_NOWAIT);
    372 			if (ipf == NULL)
    373 				return;
    374 			ipflow_inuse++;
    375 		}
    376 		bzero((caddr_t) ipf, sizeof(*ipf));
    377 	} else {
    378 		s = splimp();
    379 		IPFLOW_REMOVE(ipf);
    380 		splx(s);
    381 		ipflow_addstats(ipf);
    382 		RTFREE(ipf->ipf_ro.ro_rt);
    383 		ipf->ipf_uses = ipf->ipf_last_uses = 0;
    384 		ipf->ipf_errors = ipf->ipf_dropped = 0;
    385 	}
    386 
    387 	/*
    388 	 * Fill in the updated information.
    389 	 */
    390 	ipf->ipf_ro = *ro;
    391 	ro->ro_rt->rt_refcnt++;
    392 	ipf->ipf_dst = ip->ip_dst;
    393 	ipf->ipf_src = ip->ip_src;
    394 	ipf->ipf_tos = ip->ip_tos;
    395 	PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
    396 	ipf->ipf_start = time.tv_sec;
    397 	/*
    398 	 * Insert into the approriate bucket of the flow table.
    399 	 */
    400 	hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
    401 	s = splimp();
    402 	IPFLOW_INSERT(&ipflowtable[hash], ipf);
    403 	splx(s);
    404 }
    405