ip_fastfwd.c revision 149369
1/*- 2 * Copyright (c) 2003 Andre Oppermann, Internet Business Solutions AG 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: head/sys/netinet/ip_fastfwd.c 149369 2005-08-22 12:06:26Z andre $ 30 */ 31 32/* 33 * ip_fastforward gets its speed from processing the forwarded packet to 34 * completion (if_output on the other side) without any queues or netisr's. 35 * The receiving interface DMAs the packet into memory, the upper half of 36 * driver calls ip_fastforward, we do our routing table lookup and directly 37 * send it off to the outgoing interface, which DMAs the packet to the 38 * network card. The only part of the packet we touch with the CPU is the 39 * IP header (unless there are complex firewall rules touching other parts 40 * of the packet, but that is up to you). We are essentially limited by bus 41 * bandwidth and how fast the network card/driver can set up receives and 42 * transmits. 43 * 44 * We handle basic errors, IP header errors, checksum errors, 45 * destination unreachable, fragmentation and fragmentation needed and 46 * report them via ICMP to the sender. 47 * 48 * Else if something is not pure IPv4 unicast forwarding we fall back to 49 * the normal ip_input processing path. We should only be called from 50 * interfaces connected to the outside world. 51 * 52 * Firewalling is fully supported including divert, ipfw fwd and ipfilter 53 * ipnat and address rewrite. 54 * 55 * IPSEC is not supported if this host is a tunnel broker. IPSEC is 56 * supported for connections to/from local host. 57 * 58 * We try to do the least expensive (in CPU ops) checks and operations 59 * first to catch junk with as little overhead as possible. 60 * 61 * We take full advantage of hardware support for IP checksum and 62 * fragmentation offloading. 63 * 64 * We don't do ICMP redirect in the fast forwarding path. I have had my own 65 * cases where two core routers with Zebra routing suite would send millions 66 * ICMP redirects to connected hosts if the destination router was not the 67 * default gateway. In one case it was filling the routing table of a host 68 * with approximately 300.000 cloned redirect entries until it ran out of 69 * kernel memory. However the networking code proved very robust and it didn't 70 * crash or fail in other ways. 71 */ 72 73/* 74 * Many thanks to Matt Thomas of NetBSD for basic structure of ip_flow.c which 75 * is being followed here. 76 */ 77 78#include "opt_ipfw.h" 79#include "opt_ipstealth.h" 80 81#include <sys/param.h> 82#include <sys/systm.h> 83#include <sys/kernel.h> 84#include <sys/malloc.h> 85#include <sys/mbuf.h> 86#include <sys/protosw.h> 87#include <sys/socket.h> 88#include <sys/sysctl.h> 89 90#include <net/pfil.h> 91#include <net/if.h> 92#include <net/if_types.h> 93#include <net/if_var.h> 94#include <net/if_dl.h> 95#include <net/route.h> 96 97#include <netinet/in.h> 98#include <netinet/in_systm.h> 99#include <netinet/in_var.h> 100#include <netinet/ip.h> 101#include <netinet/ip_var.h> 102#include <netinet/ip_icmp.h> 103 104#include <machine/in_cksum.h> 105 106static int ipfastforward_active = 0; 107SYSCTL_INT(_net_inet_ip, OID_AUTO, fastforwarding, CTLFLAG_RW, 108 &ipfastforward_active, 0, "Enable fast IP forwarding"); 109 110static struct sockaddr_in * 111ip_findroute(struct route *ro, struct in_addr dest, struct mbuf *m) 112{ 113 struct sockaddr_in *dst; 114 struct rtentry *rt; 115 116 /* 117 * Find route to destination. 118 */ 119 bzero(ro, sizeof(*ro)); 120 dst = (struct sockaddr_in *)&ro->ro_dst; 121 dst->sin_family = AF_INET; 122 dst->sin_len = sizeof(*dst); 123 dst->sin_addr.s_addr = dest.s_addr; 124 rtalloc_ign(ro, RTF_CLONING); 125 126 /* 127 * Route there and interface still up? 128 */ 129 rt = ro->ro_rt; 130 if (rt && (rt->rt_flags & RTF_UP) && 131 (rt->rt_ifp->if_flags & IFF_UP) && 132 (rt->rt_ifp->if_drv_flags & IFF_DRV_RUNNING)) { 133 if (rt->rt_flags & RTF_GATEWAY) 134 dst = (struct sockaddr_in *)rt->rt_gateway; 135 } else { 136 ipstat.ips_noroute++; 137 ipstat.ips_cantforward++; 138 if (rt) 139 RTFREE(rt); 140 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 141 return NULL; 142 } 143 return dst; 144} 145 146/* 147 * Try to forward a packet based on the destination address. 148 * This is a fast path optimized for the plain forwarding case. 149 * If the packet is handled (and consumed) here then we return 1; 150 * otherwise 0 is returned and the packet should be delivered 151 * to ip_input for full processing. 152 */ 153int 154ip_fastforward(struct mbuf *m) 155{ 156 struct ip *ip; 157 struct mbuf *m0 = NULL; 158 struct route ro; 159 struct sockaddr_in *dst = NULL; 160 struct ifnet *ifp; 161 struct in_addr odest, dest; 162 u_short sum, ip_len; 163 int error = 0; 164 int hlen, mtu; 165#ifdef IPFIREWALL_FORWARD 166 struct m_tag *fwd_tag; 167#endif 168 169 /* 170 * Are we active and forwarding packets? 171 */ 172 if (!ipfastforward_active || !ipforwarding) 173 return 0; 174 175 M_ASSERTVALID(m); 176 M_ASSERTPKTHDR(m); 177 178 ro.ro_rt = NULL; 179 180 /* 181 * Step 1: check for packet drop conditions (and sanity checks) 182 */ 183 184 /* 185 * Is entire packet big enough? 186 */ 187 if (m->m_pkthdr.len < sizeof(struct ip)) { 188 ipstat.ips_tooshort++; 189 goto drop; 190 } 191 192 /* 193 * Is first mbuf large enough for ip header and is header present? 194 */ 195 if (m->m_len < sizeof (struct ip) && 196 (m = m_pullup(m, sizeof (struct ip))) == NULL) { 197 ipstat.ips_toosmall++; 198 return 1; /* mbuf already free'd */ 199 } 200 201 ip = mtod(m, struct ip *); 202 203 /* 204 * Is it IPv4? 205 */ 206 if (ip->ip_v != IPVERSION) { 207 ipstat.ips_badvers++; 208 goto drop; 209 } 210 211 /* 212 * Is IP header length correct and is it in first mbuf? 213 */ 214 hlen = ip->ip_hl << 2; 215 if (hlen < sizeof(struct ip)) { /* minimum header length */ 216 ipstat.ips_badlen++; 217 goto drop; 218 } 219 if (hlen > m->m_len) { 220 if ((m = m_pullup(m, hlen)) == 0) { 221 ipstat.ips_badhlen++; 222 return 1; 223 } 224 ip = mtod(m, struct ip *); 225 } 226 227 /* 228 * Checksum correct? 229 */ 230 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) 231 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 232 else { 233 if (hlen == sizeof(struct ip)) 234 sum = in_cksum_hdr(ip); 235 else 236 sum = in_cksum(m, hlen); 237 } 238 if (sum) { 239 ipstat.ips_badsum++; 240 goto drop; 241 } 242 243 /* 244 * Remember that we have checked the IP header and found it valid. 245 */ 246 m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID); 247 248 ip_len = ntohs(ip->ip_len); 249 250 /* 251 * Is IP length longer than packet we have got? 252 */ 253 if (m->m_pkthdr.len < ip_len) { 254 ipstat.ips_tooshort++; 255 goto drop; 256 } 257 258 /* 259 * Is packet longer than IP header tells us? If yes, truncate packet. 260 */ 261 if (m->m_pkthdr.len > ip_len) { 262 if (m->m_len == m->m_pkthdr.len) { 263 m->m_len = ip_len; 264 m->m_pkthdr.len = ip_len; 265 } else 266 m_adj(m, ip_len - m->m_pkthdr.len); 267 } 268 269 /* 270 * Is packet from or to 127/8? 271 */ 272 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 273 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 274 ipstat.ips_badaddr++; 275 goto drop; 276 } 277 278#ifdef ALTQ 279 /* 280 * Is packet dropped by traffic conditioner? 281 */ 282 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 283 return 1; 284#endif 285 286 /* 287 * Step 2: fallback conditions to normal ip_input path processing 288 */ 289 290 /* 291 * Only IP packets without options 292 */ 293 if (ip->ip_hl != (sizeof(struct ip) >> 2)) { 294 if (ip_doopts == 1) 295 return 0; 296 else if (ip_doopts == 2) { 297 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB, 298 0, 0); 299 return 1; 300 } 301 /* else ignore IP options and continue */ 302 } 303 304 /* 305 * Only unicast IP, not from loopback, no L2 or IP broadcast, 306 * no multicast, no INADDR_ANY 307 * 308 * XXX: Probably some of these checks could be direct drop 309 * conditions. However it is not clear whether there are some 310 * hacks or obscure behaviours which make it neccessary to 311 * let ip_input handle it. We play safe here and let ip_input 312 * deal with it until it is proven that we can directly drop it. 313 */ 314 if ((m->m_flags & (M_BCAST|M_MCAST)) || 315 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) || 316 ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST || 317 ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST || 318 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 319 IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 320 ip->ip_src.s_addr == INADDR_ANY || 321 ip->ip_dst.s_addr == INADDR_ANY ) 322 return 0; 323 324 /* 325 * Is it for a local address on this host? 326 */ 327 if (in_localip(ip->ip_dst)) 328 return 0; 329 330 ipstat.ips_total++; 331 332 /* 333 * Step 3: incoming packet firewall processing 334 */ 335 336 /* 337 * Convert to host representation 338 */ 339 ip->ip_len = ntohs(ip->ip_len); 340 ip->ip_off = ntohs(ip->ip_off); 341 342 odest.s_addr = dest.s_addr = ip->ip_dst.s_addr; 343 344 /* 345 * Run through list of ipfilter hooks for input packets 346 */ 347 if (inet_pfil_hook.ph_busy_count == -1) 348 goto passin; 349 350 if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL) || 351 m == NULL) 352 return 1; 353 354 M_ASSERTVALID(m); 355 M_ASSERTPKTHDR(m); 356 357 ip = mtod(m, struct ip *); /* m may have changed by pfil hook */ 358 dest.s_addr = ip->ip_dst.s_addr; 359 360 /* 361 * Destination address changed? 362 */ 363 if (odest.s_addr != dest.s_addr) { 364 /* 365 * Is it now for a local address on this host? 366 */ 367 if (in_localip(dest)) 368 goto forwardlocal; 369 /* 370 * Go on with new destination address 371 */ 372 } 373#ifdef IPFIREWALL_FORWARD 374 if (m->m_flags & M_FASTFWD_OURS) { 375 /* 376 * ipfw changed it for a local address on this host. 377 */ 378 goto forwardlocal; 379 } 380#endif /* IPFIREWALL_FORWARD */ 381 382passin: 383 /* 384 * Step 4: decrement TTL and look up route 385 */ 386 387 /* 388 * Check TTL 389 */ 390#ifdef IPSTEALTH 391 if (!ipstealth) { 392#endif 393 if (ip->ip_ttl <= IPTTLDEC) { 394 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0); 395 return 1; 396 } 397 398 /* 399 * Decrement the TTL and incrementally change the IP header checksum. 400 * Don't bother doing this with hw checksum offloading, it's faster 401 * doing it right here. 402 */ 403 ip->ip_ttl -= IPTTLDEC; 404 if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8)) 405 ip->ip_sum -= ~htons(IPTTLDEC << 8); 406 else 407 ip->ip_sum += htons(IPTTLDEC << 8); 408#ifdef IPSTEALTH 409 } 410#endif 411 412 /* 413 * Find route to destination. 414 */ 415 if ((dst = ip_findroute(&ro, dest, m)) == NULL) 416 return 1; /* icmp unreach already sent */ 417 ifp = ro.ro_rt->rt_ifp; 418 419 /* 420 * Immediately drop blackholed traffic. 421 */ 422 if (ro.ro_rt->rt_flags & RTF_BLACKHOLE) 423 goto drop; 424 425 /* 426 * Step 5: outgoing firewall packet processing 427 */ 428 429 /* 430 * Run through list of hooks for output packets. 431 */ 432 if (inet_pfil_hook.ph_busy_count == -1) 433 goto passout; 434 435 if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, NULL) || m == NULL) { 436 goto consumed; 437 } 438 439 M_ASSERTVALID(m); 440 M_ASSERTPKTHDR(m); 441 442 ip = mtod(m, struct ip *); 443 dest.s_addr = ip->ip_dst.s_addr; 444 445 /* 446 * Destination address changed? 447 */ 448#ifndef IPFIREWALL_FORWARD 449 if (odest.s_addr != dest.s_addr) { 450#else 451 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 452 if (odest.s_addr != dest.s_addr || fwd_tag != NULL) { 453#endif /* IPFIREWALL_FORWARD */ 454 /* 455 * Is it now for a local address on this host? 456 */ 457#ifndef IPFIREWALL_FORWARD 458 if (in_localip(dest)) { 459#else 460 if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) { 461#endif /* IPFIREWALL_FORWARD */ 462forwardlocal: 463 /* 464 * Return packet for processing by ip_input(). 465 * Keep host byte order as expected at ip_input's 466 * "ours"-label. 467 */ 468 m->m_flags |= M_FASTFWD_OURS; 469 if (ro.ro_rt) 470 RTFREE(ro.ro_rt); 471 return 0; 472 } 473 /* 474 * Redo route lookup with new destination address 475 */ 476#ifdef IPFIREWALL_FORWARD 477 if (fwd_tag) { 478 if (!in_localip(ip->ip_src) && !in_localaddr(ip->ip_dst)) 479 dest.s_addr = ((struct sockaddr_in *)(fwd_tag+1))->sin_addr.s_addr; 480 m_tag_delete(m, fwd_tag); 481 } 482#endif /* IPFIREWALL_FORWARD */ 483 RTFREE(ro.ro_rt); 484 if ((dst = ip_findroute(&ro, dest, m)) == NULL) 485 return 1; /* icmp unreach already sent */ 486 ifp = ro.ro_rt->rt_ifp; 487 } 488 489passout: 490 /* 491 * Step 6: send off the packet 492 */ 493 494 /* 495 * Check if route is dampned (when ARP is unable to resolve) 496 */ 497 if ((ro.ro_rt->rt_flags & RTF_REJECT) && 498 ro.ro_rt->rt_rmx.rmx_expire >= time_second) { 499 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 500 goto consumed; 501 } 502 503#ifndef ALTQ 504 /* 505 * Check if there is enough space in the interface queue 506 */ 507 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 508 ifp->if_snd.ifq_maxlen) { 509 ipstat.ips_odropped++; 510 /* would send source quench here but that is depreciated */ 511 goto drop; 512 } 513#endif 514 515 /* 516 * Check if media link state of interface is not down 517 */ 518 if (ifp->if_link_state == LINK_STATE_DOWN) { 519 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 520 goto consumed; 521 } 522 523 /* 524 * Check if packet fits MTU or if hardware will fragment for us 525 */ 526 if (ro.ro_rt->rt_rmx.rmx_mtu) 527 mtu = min(ro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu); 528 else 529 mtu = ifp->if_mtu; 530 531 if (ip->ip_len <= mtu || 532 (ifp->if_hwassist & CSUM_FRAGMENT && (ip->ip_off & IP_DF) == 0)) { 533 /* 534 * Restore packet header fields to original values 535 */ 536 ip->ip_len = htons(ip->ip_len); 537 ip->ip_off = htons(ip->ip_off); 538 /* 539 * Send off the packet via outgoing interface 540 */ 541 error = (*ifp->if_output)(ifp, m, 542 (struct sockaddr *)dst, ro.ro_rt); 543 } else { 544 /* 545 * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery 546 */ 547 if (ip->ip_off & IP_DF) { 548 ipstat.ips_cantfrag++; 549 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 550 0, mtu); 551 goto consumed; 552 } else { 553 /* 554 * We have to fragment the packet 555 */ 556 m->m_pkthdr.csum_flags |= CSUM_IP; 557 /* 558 * ip_fragment expects ip_len and ip_off in host byte 559 * order but returns all packets in network byte order 560 */ 561 if (ip_fragment(ip, &m, mtu, ifp->if_hwassist, 562 (~ifp->if_hwassist & CSUM_DELAY_IP))) { 563 goto drop; 564 } 565 KASSERT(m != NULL, ("null mbuf and no error")); 566 /* 567 * Send off the fragments via outgoing interface 568 */ 569 error = 0; 570 do { 571 m0 = m->m_nextpkt; 572 m->m_nextpkt = NULL; 573 574 error = (*ifp->if_output)(ifp, m, 575 (struct sockaddr *)dst, ro.ro_rt); 576 if (error) 577 break; 578 } while ((m = m0) != NULL); 579 if (error) { 580 /* Reclaim remaining fragments */ 581 for (m = m0; m; m = m0) { 582 m0 = m->m_nextpkt; 583 m_freem(m); 584 } 585 } else 586 ipstat.ips_fragmented++; 587 } 588 } 589 590 if (error != 0) 591 ipstat.ips_odropped++; 592 else { 593 ro.ro_rt->rt_rmx.rmx_pksent++; 594 ipstat.ips_forward++; 595 ipstat.ips_fastforward++; 596 } 597consumed: 598 RTFREE(ro.ro_rt); 599 return 1; 600drop: 601 if (m) 602 m_freem(m); 603 if (ro.ro_rt) 604 RTFREE(ro.ro_rt); 605 return 1; 606} 607