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