ip_fastfwd.c revision 295285
1139823Simp/*- 2122702Sandre * Copyright (c) 2003 Andre Oppermann, Internet Business Solutions AG 3122702Sandre * All rights reserved. 4122702Sandre * 5122702Sandre * Redistribution and use in source and binary forms, with or without 6122702Sandre * modification, are permitted provided that the following conditions 7122702Sandre * are met: 8122702Sandre * 1. Redistributions of source code must retain the above copyright 9122702Sandre * notice, this list of conditions and the following disclaimer. 10122702Sandre * 2. Redistributions in binary form must reproduce the above copyright 11122702Sandre * notice, this list of conditions and the following disclaimer in the 12122702Sandre * documentation and/or other materials provided with the distribution. 13122702Sandre * 3. The name of the author may not be used to endorse or promote 14122702Sandre * products derived from this software without specific prior written 15122702Sandre * permission. 16122702Sandre * 17122702Sandre * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18122702Sandre * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19122702Sandre * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20122702Sandre * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21122702Sandre * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22122702Sandre * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23122702Sandre * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24122702Sandre * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25122702Sandre * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26122702Sandre * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27122702Sandre * SUCH DAMAGE. 28122702Sandre */ 29122702Sandre 30122702Sandre/* 31122702Sandre * ip_fastforward gets its speed from processing the forwarded packet to 32122702Sandre * completion (if_output on the other side) without any queues or netisr's. 33122702Sandre * The receiving interface DMAs the packet into memory, the upper half of 34122702Sandre * driver calls ip_fastforward, we do our routing table lookup and directly 35148324Skeramida * send it off to the outgoing interface, which DMAs the packet to the 36122702Sandre * network card. The only part of the packet we touch with the CPU is the 37122759Sandre * IP header (unless there are complex firewall rules touching other parts 38122759Sandre * of the packet, but that is up to you). We are essentially limited by bus 39122759Sandre * bandwidth and how fast the network card/driver can set up receives and 40122759Sandre * transmits. 41122702Sandre * 42148324Skeramida * We handle basic errors, IP header errors, checksum errors, 43122702Sandre * destination unreachable, fragmentation and fragmentation needed and 44148324Skeramida * report them via ICMP to the sender. 45122702Sandre * 46122702Sandre * Else if something is not pure IPv4 unicast forwarding we fall back to 47122702Sandre * the normal ip_input processing path. We should only be called from 48122702Sandre * interfaces connected to the outside world. 49122702Sandre * 50122702Sandre * Firewalling is fully supported including divert, ipfw fwd and ipfilter 51122702Sandre * ipnat and address rewrite. 52122702Sandre * 53122702Sandre * IPSEC is not supported if this host is a tunnel broker. IPSEC is 54122702Sandre * supported for connections to/from local host. 55122702Sandre * 56122702Sandre * We try to do the least expensive (in CPU ops) checks and operations 57122702Sandre * first to catch junk with as little overhead as possible. 58122702Sandre * 59148324Skeramida * We take full advantage of hardware support for IP checksum and 60122702Sandre * fragmentation offloading. 61122702Sandre * 62122702Sandre * We don't do ICMP redirect in the fast forwarding path. I have had my own 63122702Sandre * cases where two core routers with Zebra routing suite would send millions 64148324Skeramida * ICMP redirects to connected hosts if the destination router was not the 65148324Skeramida * default gateway. In one case it was filling the routing table of a host 66148324Skeramida * with approximately 300.000 cloned redirect entries until it ran out of 67148324Skeramida * kernel memory. However the networking code proved very robust and it didn't 68148324Skeramida * crash or fail in other ways. 69122702Sandre */ 70122702Sandre 71122702Sandre/* 72122702Sandre * Many thanks to Matt Thomas of NetBSD for basic structure of ip_flow.c which 73122702Sandre * is being followed here. 74122702Sandre */ 75122702Sandre 76172467Ssilby#include <sys/cdefs.h> 77172467Ssilby__FBSDID("$FreeBSD: stable/10/sys/netinet/ip_fastfwd.c 295285 2016-02-04 22:53:12Z gnn $"); 78172467Ssilby 79122702Sandre#include "opt_ipfw.h" 80122702Sandre#include "opt_ipstealth.h" 81254889Smarkj#include "opt_kdtrace.h" 82122702Sandre 83122702Sandre#include <sys/param.h> 84122702Sandre#include <sys/systm.h> 85122702Sandre#include <sys/kernel.h> 86122702Sandre#include <sys/malloc.h> 87122702Sandre#include <sys/mbuf.h> 88122702Sandre#include <sys/protosw.h> 89254889Smarkj#include <sys/sdt.h> 90122702Sandre#include <sys/socket.h> 91122702Sandre#include <sys/sysctl.h> 92122702Sandre 93122702Sandre#include <net/pfil.h> 94122702Sandre#include <net/if.h> 95122702Sandre#include <net/if_types.h> 96122702Sandre#include <net/if_var.h> 97122702Sandre#include <net/if_dl.h> 98122702Sandre#include <net/route.h> 99196019Srwatson#include <net/vnet.h> 100122702Sandre 101122702Sandre#include <netinet/in.h> 102254889Smarkj#include <netinet/in_kdtrace.h> 103122702Sandre#include <netinet/in_systm.h> 104122702Sandre#include <netinet/in_var.h> 105122702Sandre#include <netinet/ip.h> 106122702Sandre#include <netinet/ip_var.h> 107122702Sandre#include <netinet/ip_icmp.h> 108152592Sandre#include <netinet/ip_options.h> 109122702Sandre 110122702Sandre#include <machine/in_cksum.h> 111122702Sandre 112128872Sandrestatic struct sockaddr_in * 113133497Sandreip_findroute(struct route *ro, struct in_addr dest, struct mbuf *m) 114128872Sandre{ 115128872Sandre struct sockaddr_in *dst; 116128872Sandre struct rtentry *rt; 117128872Sandre 118128872Sandre /* 119128872Sandre * Find route to destination. 120128872Sandre */ 121128872Sandre bzero(ro, sizeof(*ro)); 122128872Sandre dst = (struct sockaddr_in *)&ro->ro_dst; 123128872Sandre dst->sin_family = AF_INET; 124128872Sandre dst->sin_len = sizeof(*dst); 125133497Sandre dst->sin_addr.s_addr = dest.s_addr; 126186119Sqingli in_rtalloc_ign(ro, 0, M_GETFIB(m)); 127128872Sandre 128128872Sandre /* 129128872Sandre * Route there and interface still up? 130128872Sandre */ 131128872Sandre rt = ro->ro_rt; 132128872Sandre if (rt && (rt->rt_flags & RTF_UP) && 133128872Sandre (rt->rt_ifp->if_flags & IFF_UP) && 134148887Srwatson (rt->rt_ifp->if_drv_flags & IFF_DRV_RUNNING)) { 135128872Sandre if (rt->rt_flags & RTF_GATEWAY) 136128872Sandre dst = (struct sockaddr_in *)rt->rt_gateway; 137128872Sandre } else { 138190951Srwatson IPSTAT_INC(ips_noroute); 139190951Srwatson IPSTAT_INC(ips_cantforward); 140128872Sandre if (rt) 141128872Sandre RTFREE(rt); 142145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 143128872Sandre return NULL; 144128872Sandre } 145128872Sandre return dst; 146128872Sandre} 147128872Sandre 148122702Sandre/* 149122702Sandre * Try to forward a packet based on the destination address. 150122702Sandre * This is a fast path optimized for the plain forwarding case. 151196881Spjd * If the packet is handled (and consumed) here then we return NULL; 152196881Spjd * otherwise mbuf is returned and the packet should be delivered 153122702Sandre * to ip_input for full processing. 154122702Sandre */ 155154518Sandrestruct mbuf * 156295285Sgnnip_tryforward(struct mbuf *m) 157122702Sandre{ 158122702Sandre struct ip *ip; 159122702Sandre struct mbuf *m0 = NULL; 160122702Sandre struct route ro; 161122702Sandre struct sockaddr_in *dst = NULL; 162128872Sandre struct ifnet *ifp; 163133497Sandre struct in_addr odest, dest; 164295285Sgnn uint16_t ip_len, ip_off; 165122702Sandre int error = 0; 166295285Sgnn int mtu; 167242079Sae struct m_tag *fwd_tag = NULL; 168122702Sandre 169122702Sandre /* 170122702Sandre * Are we active and forwarding packets? 171122702Sandre */ 172122702Sandre 173122702Sandre M_ASSERTVALID(m); 174122702Sandre M_ASSERTPKTHDR(m); 175122702Sandre 176191148Skmacy bzero(&ro, sizeof(ro)); 177128872Sandre 178122702Sandre 179133480Sandre#ifdef ALTQ 180122702Sandre /* 181133480Sandre * Is packet dropped by traffic conditioner? 182133480Sandre */ 183133480Sandre if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 184154518Sandre goto drop; 185133480Sandre#endif 186133480Sandre 187133480Sandre /* 188295285Sgnn * Only IP packets without options 189122702Sandre */ 190295285Sgnn ip = mtod(m, struct ip *); 191122702Sandre 192129017Sandre if (ip->ip_hl != (sizeof(struct ip) >> 2)) { 193272868Shrs if (V_ip_doopts == 1) 194154518Sandre return m; 195272868Shrs else if (V_ip_doopts == 2) { 196129017Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB, 197145863Sandre 0, 0); 198154518Sandre return NULL; /* mbuf already free'd */ 199129017Sandre } 200129017Sandre /* else ignore IP options and continue */ 201129017Sandre } 202122702Sandre 203122702Sandre /* 204122702Sandre * Only unicast IP, not from loopback, no L2 or IP broadcast, 205122702Sandre * no multicast, no INADDR_ANY 206122702Sandre * 207122702Sandre * XXX: Probably some of these checks could be direct drop 208122702Sandre * conditions. However it is not clear whether there are some 209122702Sandre * hacks or obscure behaviours which make it neccessary to 210122702Sandre * let ip_input handle it. We play safe here and let ip_input 211122702Sandre * deal with it until it is proven that we can directly drop it. 212122702Sandre */ 213149369Sandre if ((m->m_flags & (M_BCAST|M_MCAST)) || 214149369Sandre (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) || 215122702Sandre ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST || 216122702Sandre ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST || 217122702Sandre IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 218122702Sandre IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 219166452Sbms IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) || 220166452Sbms IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) || 221149369Sandre ip->ip_src.s_addr == INADDR_ANY || 222122702Sandre ip->ip_dst.s_addr == INADDR_ANY ) 223154518Sandre return m; 224122702Sandre 225122702Sandre /* 226122702Sandre * Is it for a local address on this host? 227122702Sandre */ 228133497Sandre if (in_localip(ip->ip_dst)) 229154518Sandre return m; 230122702Sandre 231190951Srwatson IPSTAT_INC(ips_total); 232122702Sandre 233122702Sandre /* 234122702Sandre * Step 3: incoming packet firewall processing 235122702Sandre */ 236122702Sandre 237133497Sandre odest.s_addr = dest.s_addr = ip->ip_dst.s_addr; 238134383Sandre 239122702Sandre /* 240122702Sandre * Run through list of ipfilter hooks for input packets 241122702Sandre */ 242197952Sjulian if (!PFIL_HOOKED(&V_inet_pfil_hook)) 243134383Sandre goto passin; 244134383Sandre 245197952Sjulian if (pfil_run_hooks( 246197952Sjulian &V_inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL) || 247122702Sandre m == NULL) 248154518Sandre goto drop; 249122702Sandre 250122702Sandre M_ASSERTVALID(m); 251122702Sandre M_ASSERTPKTHDR(m); 252122702Sandre 253122702Sandre ip = mtod(m, struct ip *); /* m may have changed by pfil hook */ 254133497Sandre dest.s_addr = ip->ip_dst.s_addr; 255122702Sandre 256122702Sandre /* 257122702Sandre * Destination address changed? 258122702Sandre */ 259133497Sandre if (odest.s_addr != dest.s_addr) { 260122702Sandre /* 261122702Sandre * Is it now for a local address on this host? 262122702Sandre */ 263133497Sandre if (in_localip(dest)) 264133497Sandre goto forwardlocal; 265122702Sandre /* 266122702Sandre * Go on with new destination address 267122702Sandre */ 268122702Sandre } 269242079Sae 270133920Sandre if (m->m_flags & M_FASTFWD_OURS) { 271133920Sandre /* 272133920Sandre * ipfw changed it for a local address on this host. 273133920Sandre */ 274133920Sandre goto forwardlocal; 275133920Sandre } 276122702Sandre 277134383Sandrepassin: 278122702Sandre /* 279122702Sandre * Step 4: decrement TTL and look up route 280122702Sandre */ 281122702Sandre 282122702Sandre /* 283122702Sandre * Check TTL 284122702Sandre */ 285122702Sandre#ifdef IPSTEALTH 286181803Sbz if (!V_ipstealth) { 287122702Sandre#endif 288122702Sandre if (ip->ip_ttl <= IPTTLDEC) { 289145863Sandre icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0); 290154518Sandre return NULL; /* mbuf already free'd */ 291122702Sandre } 292122702Sandre 293122702Sandre /* 294136690Sandre * Decrement the TTL and incrementally change the IP header checksum. 295136690Sandre * Don't bother doing this with hw checksum offloading, it's faster 296136690Sandre * doing it right here. 297122702Sandre */ 298122702Sandre ip->ip_ttl -= IPTTLDEC; 299122702Sandre if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8)) 300122702Sandre ip->ip_sum -= ~htons(IPTTLDEC << 8); 301122702Sandre else 302122702Sandre ip->ip_sum += htons(IPTTLDEC << 8); 303122702Sandre#ifdef IPSTEALTH 304122702Sandre } 305122702Sandre#endif 306122702Sandre 307122702Sandre /* 308122702Sandre * Find route to destination. 309122702Sandre */ 310128872Sandre if ((dst = ip_findroute(&ro, dest, m)) == NULL) 311154518Sandre return NULL; /* icmp unreach already sent */ 312128872Sandre ifp = ro.ro_rt->rt_ifp; 313122702Sandre 314122702Sandre /* 315166507Sbms * Immediately drop blackholed traffic, and directed broadcasts 316166507Sbms * for either the all-ones or all-zero subnet addresses on 317166507Sbms * locally attached networks. 318137179Sbms */ 319166507Sbms if ((ro.ro_rt->rt_flags & (RTF_BLACKHOLE|RTF_BROADCAST)) != 0) 320137179Sbms goto drop; 321137179Sbms 322137179Sbms /* 323122702Sandre * Step 5: outgoing firewall packet processing 324122702Sandre */ 325122702Sandre 326122702Sandre /* 327122702Sandre * Run through list of hooks for output packets. 328122702Sandre */ 329197952Sjulian if (!PFIL_HOOKED(&V_inet_pfil_hook)) 330134383Sandre goto passout; 331134383Sandre 332197952Sjulian if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_OUT, NULL) || m == NULL) { 333154518Sandre goto drop; 334122702Sandre } 335122702Sandre 336122702Sandre M_ASSERTVALID(m); 337122702Sandre M_ASSERTPKTHDR(m); 338122702Sandre 339122702Sandre ip = mtod(m, struct ip *); 340133497Sandre dest.s_addr = ip->ip_dst.s_addr; 341122702Sandre 342122702Sandre /* 343122702Sandre * Destination address changed? 344122702Sandre */ 345242463Sae if (m->m_flags & M_IP_NEXTHOP) 346242079Sae fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 347133920Sandre if (odest.s_addr != dest.s_addr || fwd_tag != NULL) { 348122702Sandre /* 349122702Sandre * Is it now for a local address on this host? 350122702Sandre */ 351136690Sandre if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) { 352122702Sandreforwardlocal: 353133497Sandre /* 354135158Sandre * Return packet for processing by ip_input(). 355133497Sandre */ 356135158Sandre m->m_flags |= M_FASTFWD_OURS; 357133497Sandre if (ro.ro_rt) 358133497Sandre RTFREE(ro.ro_rt); 359154518Sandre return m; 360122702Sandre } 361122702Sandre /* 362122702Sandre * Redo route lookup with new destination address 363122702Sandre */ 364133920Sandre if (fwd_tag) { 365161380Sjulian dest.s_addr = ((struct sockaddr_in *) 366157833Sglebius (fwd_tag + 1))->sin_addr.s_addr; 367133920Sandre m_tag_delete(m, fwd_tag); 368242463Sae m->m_flags &= ~M_IP_NEXTHOP; 369133920Sandre } 370122702Sandre RTFREE(ro.ro_rt); 371128872Sandre if ((dst = ip_findroute(&ro, dest, m)) == NULL) 372154518Sandre return NULL; /* icmp unreach already sent */ 373128872Sandre ifp = ro.ro_rt->rt_ifp; 374122702Sandre } 375122702Sandre 376134383Sandrepassout: 377122702Sandre /* 378122702Sandre * Step 6: send off the packet 379122702Sandre */ 380241245Sglebius ip_len = ntohs(ip->ip_len); 381241245Sglebius ip_off = ntohs(ip->ip_off); 382122702Sandre 383122702Sandre /* 384128872Sandre * Check if route is dampned (when ARP is unable to resolve) 385128872Sandre */ 386128872Sandre if ((ro.ro_rt->rt_flags & RTF_REJECT) && 387263478Sglebius (ro.ro_rt->rt_expire == 0 || time_uptime < ro.ro_rt->rt_expire)) { 388145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 389128872Sandre goto consumed; 390128872Sandre } 391128872Sandre 392133480Sandre#ifndef ALTQ 393128872Sandre /* 394128872Sandre * Check if there is enough space in the interface queue 395128872Sandre */ 396241245Sglebius if ((ifp->if_snd.ifq_len + ip_len / ifp->if_mtu + 1) >= 397128872Sandre ifp->if_snd.ifq_maxlen) { 398190951Srwatson IPSTAT_INC(ips_odropped); 399128872Sandre /* would send source quench here but that is depreciated */ 400128872Sandre goto drop; 401128872Sandre } 402133480Sandre#endif 403128872Sandre 404128872Sandre /* 405128872Sandre * Check if media link state of interface is not down 406128872Sandre */ 407128872Sandre if (ifp->if_link_state == LINK_STATE_DOWN) { 408145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 409128872Sandre goto consumed; 410128872Sandre } 411128872Sandre 412128872Sandre /* 413148324Skeramida * Check if packet fits MTU or if hardware will fragment for us 414122702Sandre */ 415263478Sglebius if (ro.ro_rt->rt_mtu) 416263478Sglebius mtu = min(ro.ro_rt->rt_mtu, ifp->if_mtu); 417122702Sandre else 418122702Sandre mtu = ifp->if_mtu; 419122702Sandre 420241245Sglebius if (ip_len <= mtu || 421241245Sglebius (ifp->if_hwassist & CSUM_FRAGMENT && (ip_off & IP_DF) == 0)) { 422122702Sandre /* 423254523Sandre * Avoid confusing lower layers. 424254523Sandre */ 425254523Sandre m_clrprotoflags(m); 426254523Sandre /* 427122702Sandre * Send off the packet via outgoing interface 428122702Sandre */ 429254889Smarkj IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 430122702Sandre error = (*ifp->if_output)(ifp, m, 431191148Skmacy (struct sockaddr *)dst, &ro); 432122702Sandre } else { 433122702Sandre /* 434128872Sandre * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery 435122702Sandre */ 436241245Sglebius if (ip_off & IP_DF) { 437190951Srwatson IPSTAT_INC(ips_cantfrag); 438122702Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 439145863Sandre 0, mtu); 440128872Sandre goto consumed; 441122702Sandre } else { 442122702Sandre /* 443148324Skeramida * We have to fragment the packet 444122702Sandre */ 445122702Sandre m->m_pkthdr.csum_flags |= CSUM_IP; 446242161Sglebius if (ip_fragment(ip, &m, mtu, ifp->if_hwassist)) 447122702Sandre goto drop; 448122702Sandre KASSERT(m != NULL, ("null mbuf and no error")); 449122702Sandre /* 450122702Sandre * Send off the fragments via outgoing interface 451122702Sandre */ 452122702Sandre error = 0; 453122702Sandre do { 454122702Sandre m0 = m->m_nextpkt; 455122702Sandre m->m_nextpkt = NULL; 456254523Sandre /* 457254523Sandre * Avoid confusing lower layers. 458254523Sandre */ 459254523Sandre m_clrprotoflags(m); 460122702Sandre 461254889Smarkj IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 462122702Sandre error = (*ifp->if_output)(ifp, m, 463191148Skmacy (struct sockaddr *)dst, &ro); 464122702Sandre if (error) 465122702Sandre break; 466122702Sandre } while ((m = m0) != NULL); 467122702Sandre if (error) { 468122702Sandre /* Reclaim remaining fragments */ 469144301Sglebius for (m = m0; m; m = m0) { 470122702Sandre m0 = m->m_nextpkt; 471122702Sandre m_freem(m); 472122702Sandre } 473122702Sandre } else 474190951Srwatson IPSTAT_INC(ips_fragmented); 475122702Sandre } 476122702Sandre } 477122702Sandre 478122702Sandre if (error != 0) 479190951Srwatson IPSTAT_INC(ips_odropped); 480122702Sandre else { 481263478Sglebius counter_u64_add(ro.ro_rt->rt_pksent, 1); 482190951Srwatson IPSTAT_INC(ips_forward); 483190951Srwatson IPSTAT_INC(ips_fastforward); 484122702Sandre } 485128872Sandreconsumed: 486122702Sandre RTFREE(ro.ro_rt); 487154518Sandre return NULL; 488122702Sandredrop: 489122702Sandre if (m) 490122702Sandre m_freem(m); 491128872Sandre if (ro.ro_rt) 492128872Sandre RTFREE(ro.ro_rt); 493154518Sandre return NULL; 494122702Sandre} 495