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/11/sys/netinet/ip_fastfwd.c 338611 2018-09-12 08:46:49Z eugen $"); 78172467Ssilby 79122702Sandre#include "opt_ipstealth.h" 80122702Sandre 81122702Sandre#include <sys/param.h> 82122702Sandre#include <sys/systm.h> 83122702Sandre#include <sys/kernel.h> 84122702Sandre#include <sys/malloc.h> 85122702Sandre#include <sys/mbuf.h> 86122702Sandre#include <sys/protosw.h> 87254889Smarkj#include <sys/sdt.h> 88122702Sandre#include <sys/socket.h> 89122702Sandre#include <sys/sysctl.h> 90122702Sandre 91122702Sandre#include <net/pfil.h> 92122702Sandre#include <net/if.h> 93122702Sandre#include <net/if_types.h> 94122702Sandre#include <net/if_var.h> 95122702Sandre#include <net/if_dl.h> 96122702Sandre#include <net/route.h> 97196019Srwatson#include <net/vnet.h> 98122702Sandre 99122702Sandre#include <netinet/in.h> 100310771Sae#include <netinet/in_fib.h> 101254889Smarkj#include <netinet/in_kdtrace.h> 102122702Sandre#include <netinet/in_systm.h> 103122702Sandre#include <netinet/in_var.h> 104122702Sandre#include <netinet/ip.h> 105122702Sandre#include <netinet/ip_var.h> 106122702Sandre#include <netinet/ip_icmp.h> 107152592Sandre#include <netinet/ip_options.h> 108122702Sandre 109122702Sandre#include <machine/in_cksum.h> 110122702Sandre 111310771Saestatic int 112310771Saeip_findroute(struct nhop4_basic *pnh, struct in_addr dest, struct mbuf *m) 113128872Sandre{ 114128872Sandre 115310771Sae bzero(pnh, sizeof(*pnh)); 116310771Sae if (fib4_lookup_nh_basic(M_GETFIB(m), dest, 0, 0, pnh) != 0) { 117310771Sae IPSTAT_INC(ips_noroute); 118310771Sae IPSTAT_INC(ips_cantforward); 119310771Sae icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 120310771Sae return (EHOSTUNREACH); 121310771Sae } 122128872Sandre /* 123310771Sae * Drop blackholed traffic and directed broadcasts. 124128872Sandre */ 125310771Sae if ((pnh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST)) != 0) { 126310771Sae IPSTAT_INC(ips_cantforward); 127310771Sae m_freem(m); 128310771Sae return (EHOSTUNREACH); 129310771Sae } 130128872Sandre 131310771Sae if (pnh->nh_flags & NHF_REJECT) { 132190951Srwatson IPSTAT_INC(ips_cantforward); 133145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 134310771Sae return (EHOSTUNREACH); 135128872Sandre } 136310771Sae 137310771Sae return (0); 138128872Sandre} 139128872Sandre 140122702Sandre/* 141122702Sandre * Try to forward a packet based on the destination address. 142122702Sandre * This is a fast path optimized for the plain forwarding case. 143196881Spjd * If the packet is handled (and consumed) here then we return NULL; 144196881Spjd * otherwise mbuf is returned and the packet should be delivered 145122702Sandre * to ip_input for full processing. 146122702Sandre */ 147154518Sandrestruct mbuf * 148290383Sgnnip_tryforward(struct mbuf *m) 149122702Sandre{ 150122702Sandre struct ip *ip; 151122702Sandre struct mbuf *m0 = NULL; 152310771Sae struct nhop4_basic nh; 153310771Sae struct sockaddr_in dst; 154338611Seugen struct in_addr dest, odest, rtdest; 155290383Sgnn uint16_t ip_len, ip_off; 156122702Sandre int error = 0; 157242079Sae struct m_tag *fwd_tag = NULL; 158122702Sandre 159122702Sandre /* 160122702Sandre * Are we active and forwarding packets? 161122702Sandre */ 162122702Sandre 163122702Sandre M_ASSERTVALID(m); 164122702Sandre M_ASSERTPKTHDR(m); 165122702Sandre 166133480Sandre#ifdef ALTQ 167122702Sandre /* 168133480Sandre * Is packet dropped by traffic conditioner? 169133480Sandre */ 170133480Sandre if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 171154518Sandre goto drop; 172133480Sandre#endif 173133480Sandre 174133480Sandre /* 175290383Sgnn * Only IP packets without options 176122702Sandre */ 177290383Sgnn ip = mtod(m, struct ip *); 178122702Sandre 179129017Sandre if (ip->ip_hl != (sizeof(struct ip) >> 2)) { 180271610Shrs if (V_ip_doopts == 1) 181154518Sandre return m; 182271610Shrs else if (V_ip_doopts == 2) { 183129017Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB, 184145863Sandre 0, 0); 185154518Sandre return NULL; /* mbuf already free'd */ 186129017Sandre } 187129017Sandre /* else ignore IP options and continue */ 188129017Sandre } 189122702Sandre 190122702Sandre /* 191122702Sandre * Only unicast IP, not from loopback, no L2 or IP broadcast, 192122702Sandre * no multicast, no INADDR_ANY 193122702Sandre * 194122702Sandre * XXX: Probably some of these checks could be direct drop 195122702Sandre * conditions. However it is not clear whether there are some 196298995Spfg * hacks or obscure behaviours which make it necessary to 197122702Sandre * let ip_input handle it. We play safe here and let ip_input 198122702Sandre * deal with it until it is proven that we can directly drop it. 199122702Sandre */ 200149369Sandre if ((m->m_flags & (M_BCAST|M_MCAST)) || 201149369Sandre (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) || 202122702Sandre ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST || 203122702Sandre ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST || 204122702Sandre IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 205122702Sandre IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 206166452Sbms IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) || 207166452Sbms IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) || 208149369Sandre ip->ip_src.s_addr == INADDR_ANY || 209122702Sandre ip->ip_dst.s_addr == INADDR_ANY ) 210154518Sandre return m; 211122702Sandre 212122702Sandre /* 213122702Sandre * Is it for a local address on this host? 214122702Sandre */ 215133497Sandre if (in_localip(ip->ip_dst)) 216154518Sandre return m; 217122702Sandre 218190951Srwatson IPSTAT_INC(ips_total); 219122702Sandre 220122702Sandre /* 221122702Sandre * Step 3: incoming packet firewall processing 222122702Sandre */ 223122702Sandre 224133497Sandre odest.s_addr = dest.s_addr = ip->ip_dst.s_addr; 225134383Sandre 226122702Sandre /* 227122702Sandre * Run through list of ipfilter hooks for input packets 228122702Sandre */ 229197952Sjulian if (!PFIL_HOOKED(&V_inet_pfil_hook)) 230134383Sandre goto passin; 231134383Sandre 232197952Sjulian if (pfil_run_hooks( 233332513Skp &V_inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, 0, NULL) || 234122702Sandre m == NULL) 235154518Sandre goto drop; 236122702Sandre 237122702Sandre M_ASSERTVALID(m); 238122702Sandre M_ASSERTPKTHDR(m); 239122702Sandre 240122702Sandre ip = mtod(m, struct ip *); /* m may have changed by pfil hook */ 241133497Sandre dest.s_addr = ip->ip_dst.s_addr; 242122702Sandre 243122702Sandre /* 244122702Sandre * Destination address changed? 245122702Sandre */ 246133497Sandre if (odest.s_addr != dest.s_addr) { 247122702Sandre /* 248122702Sandre * Is it now for a local address on this host? 249122702Sandre */ 250133497Sandre if (in_localip(dest)) 251133497Sandre goto forwardlocal; 252122702Sandre /* 253122702Sandre * Go on with new destination address 254122702Sandre */ 255122702Sandre } 256242079Sae 257133920Sandre if (m->m_flags & M_FASTFWD_OURS) { 258133920Sandre /* 259133920Sandre * ipfw changed it for a local address on this host. 260133920Sandre */ 261133920Sandre goto forwardlocal; 262133920Sandre } 263122702Sandre 264134383Sandrepassin: 265122702Sandre /* 266122702Sandre * Step 4: decrement TTL and look up route 267122702Sandre */ 268122702Sandre 269122702Sandre /* 270122702Sandre * Check TTL 271122702Sandre */ 272122702Sandre#ifdef IPSTEALTH 273181803Sbz if (!V_ipstealth) { 274122702Sandre#endif 275122702Sandre if (ip->ip_ttl <= IPTTLDEC) { 276145863Sandre icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0); 277154518Sandre return NULL; /* mbuf already free'd */ 278122702Sandre } 279122702Sandre 280122702Sandre /* 281136690Sandre * Decrement the TTL and incrementally change the IP header checksum. 282136690Sandre * Don't bother doing this with hw checksum offloading, it's faster 283136690Sandre * doing it right here. 284122702Sandre */ 285122702Sandre ip->ip_ttl -= IPTTLDEC; 286122702Sandre if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8)) 287122702Sandre ip->ip_sum -= ~htons(IPTTLDEC << 8); 288122702Sandre else 289122702Sandre ip->ip_sum += htons(IPTTLDEC << 8); 290122702Sandre#ifdef IPSTEALTH 291122702Sandre } 292122702Sandre#endif 293122702Sandre 294122702Sandre /* 295338611Seugen * Next hop forced by pfil(9) hook? 296338611Seugen */ 297338611Seugen if ((m->m_flags & M_IP_NEXTHOP) && 298338611Seugen ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) { 299338611Seugen /* 300338611Seugen * Now we will find route to forced destination. 301338611Seugen */ 302338611Seugen dest.s_addr = ((struct sockaddr_in *) 303338611Seugen (fwd_tag + 1))->sin_addr.s_addr; 304338611Seugen m_tag_delete(m, fwd_tag); 305338611Seugen m->m_flags &= ~M_IP_NEXTHOP; 306338611Seugen } 307338611Seugen 308338611Seugen /* 309122702Sandre * Find route to destination. 310122702Sandre */ 311310771Sae if (ip_findroute(&nh, dest, m) != 0) 312310771Sae return (NULL); /* icmp unreach already sent */ 313122702Sandre 314122702Sandre /* 315338611Seugen * Avoid second route lookup by caching destination. 316338611Seugen */ 317338611Seugen rtdest.s_addr = dest.s_addr; 318338611Seugen 319338611Seugen /* 320122702Sandre * Step 5: outgoing firewall packet processing 321122702Sandre */ 322197952Sjulian if (!PFIL_HOOKED(&V_inet_pfil_hook)) 323134383Sandre goto passout; 324134383Sandre 325332513Skp if (pfil_run_hooks(&V_inet_pfil_hook, &m, nh.nh_ifp, PFIL_OUT, PFIL_FWD, 326332513Skp NULL) || m == NULL) { 327154518Sandre goto drop; 328122702Sandre } 329122702Sandre 330122702Sandre M_ASSERTVALID(m); 331122702Sandre M_ASSERTPKTHDR(m); 332122702Sandre 333122702Sandre ip = mtod(m, struct ip *); 334133497Sandre dest.s_addr = ip->ip_dst.s_addr; 335122702Sandre 336122702Sandre /* 337122702Sandre * Destination address changed? 338122702Sandre */ 339242463Sae if (m->m_flags & M_IP_NEXTHOP) 340242079Sae fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 341338611Seugen else 342338611Seugen fwd_tag = NULL; 343133920Sandre if (odest.s_addr != dest.s_addr || fwd_tag != NULL) { 344122702Sandre /* 345122702Sandre * Is it now for a local address on this host? 346122702Sandre */ 347136690Sandre if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) { 348122702Sandreforwardlocal: 349133497Sandre /* 350135158Sandre * Return packet for processing by ip_input(). 351133497Sandre */ 352135158Sandre m->m_flags |= M_FASTFWD_OURS; 353310771Sae return (m); 354122702Sandre } 355122702Sandre /* 356122702Sandre * Redo route lookup with new destination address 357122702Sandre */ 358133920Sandre if (fwd_tag) { 359161380Sjulian dest.s_addr = ((struct sockaddr_in *) 360157833Sglebius (fwd_tag + 1))->sin_addr.s_addr; 361133920Sandre m_tag_delete(m, fwd_tag); 362242463Sae m->m_flags &= ~M_IP_NEXTHOP; 363133920Sandre } 364338611Seugen if (dest.s_addr != rtdest.s_addr && 365338611Seugen ip_findroute(&nh, dest, m) != 0) 366310771Sae return (NULL); /* icmp unreach already sent */ 367122702Sandre } 368122702Sandre 369134383Sandrepassout: 370122702Sandre /* 371122702Sandre * Step 6: send off the packet 372122702Sandre */ 373241245Sglebius ip_len = ntohs(ip->ip_len); 374241245Sglebius ip_off = ntohs(ip->ip_off); 375122702Sandre 376310771Sae bzero(&dst, sizeof(dst)); 377310771Sae dst.sin_family = AF_INET; 378310771Sae dst.sin_len = sizeof(dst); 379310771Sae dst.sin_addr = nh.nh_addr; 380128872Sandre 381128872Sandre /* 382148324Skeramida * Check if packet fits MTU or if hardware will fragment for us 383122702Sandre */ 384310771Sae if (ip_len <= nh.nh_mtu) { 385122702Sandre /* 386254523Sandre * Avoid confusing lower layers. 387254523Sandre */ 388254523Sandre m_clrprotoflags(m); 389254523Sandre /* 390122702Sandre * Send off the packet via outgoing interface 391122702Sandre */ 392310771Sae IP_PROBE(send, NULL, NULL, ip, nh.nh_ifp, ip, NULL); 393310771Sae error = (*nh.nh_ifp->if_output)(nh.nh_ifp, m, 394310771Sae (struct sockaddr *)&dst, NULL); 395122702Sandre } else { 396122702Sandre /* 397128872Sandre * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery 398122702Sandre */ 399241245Sglebius if (ip_off & IP_DF) { 400190951Srwatson IPSTAT_INC(ips_cantfrag); 401122702Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 402310771Sae 0, nh.nh_mtu); 403128872Sandre goto consumed; 404122702Sandre } else { 405122702Sandre /* 406148324Skeramida * We have to fragment the packet 407122702Sandre */ 408122702Sandre m->m_pkthdr.csum_flags |= CSUM_IP; 409310771Sae if (ip_fragment(ip, &m, nh.nh_mtu, 410310771Sae nh.nh_ifp->if_hwassist) != 0) 411122702Sandre goto drop; 412122702Sandre KASSERT(m != NULL, ("null mbuf and no error")); 413122702Sandre /* 414122702Sandre * Send off the fragments via outgoing interface 415122702Sandre */ 416122702Sandre error = 0; 417122702Sandre do { 418122702Sandre m0 = m->m_nextpkt; 419122702Sandre m->m_nextpkt = NULL; 420254523Sandre /* 421254523Sandre * Avoid confusing lower layers. 422254523Sandre */ 423254523Sandre m_clrprotoflags(m); 424122702Sandre 425311682Sae IP_PROBE(send, NULL, NULL, 426311682Sae mtod(m, struct ip *), nh.nh_ifp, 427311682Sae mtod(m, struct ip *), NULL); 428310771Sae /* XXX: we can use cached route here */ 429310771Sae error = (*nh.nh_ifp->if_output)(nh.nh_ifp, m, 430310771Sae (struct sockaddr *)&dst, NULL); 431122702Sandre if (error) 432122702Sandre break; 433122702Sandre } while ((m = m0) != NULL); 434122702Sandre if (error) { 435122702Sandre /* Reclaim remaining fragments */ 436144301Sglebius for (m = m0; m; m = m0) { 437122702Sandre m0 = m->m_nextpkt; 438122702Sandre m_freem(m); 439122702Sandre } 440122702Sandre } else 441190951Srwatson IPSTAT_INC(ips_fragmented); 442122702Sandre } 443122702Sandre } 444122702Sandre 445122702Sandre if (error != 0) 446190951Srwatson IPSTAT_INC(ips_odropped); 447122702Sandre else { 448190951Srwatson IPSTAT_INC(ips_forward); 449190951Srwatson IPSTAT_INC(ips_fastforward); 450122702Sandre } 451128872Sandreconsumed: 452154518Sandre return NULL; 453122702Sandredrop: 454122702Sandre if (m) 455122702Sandre m_freem(m); 456154518Sandre return NULL; 457122702Sandre} 458