ip_fastfwd.c revision 298995
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: head/sys/netinet/ip_fastfwd.c 298995 2016-05-03 18:05:43Z pfg $"); 78172467Ssilby 79122702Sandre#include "opt_ipfw.h" 80122702Sandre#include "opt_ipstealth.h" 81122702Sandre 82122702Sandre#include <sys/param.h> 83122702Sandre#include <sys/systm.h> 84122702Sandre#include <sys/kernel.h> 85122702Sandre#include <sys/malloc.h> 86122702Sandre#include <sys/mbuf.h> 87122702Sandre#include <sys/protosw.h> 88254889Smarkj#include <sys/sdt.h> 89122702Sandre#include <sys/socket.h> 90122702Sandre#include <sys/sysctl.h> 91122702Sandre 92122702Sandre#include <net/pfil.h> 93122702Sandre#include <net/if.h> 94122702Sandre#include <net/if_types.h> 95122702Sandre#include <net/if_var.h> 96122702Sandre#include <net/if_dl.h> 97122702Sandre#include <net/route.h> 98196019Srwatson#include <net/vnet.h> 99122702Sandre 100122702Sandre#include <netinet/in.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 111128872Sandrestatic struct sockaddr_in * 112133497Sandreip_findroute(struct route *ro, struct in_addr dest, struct mbuf *m) 113128872Sandre{ 114128872Sandre struct sockaddr_in *dst; 115128872Sandre struct rtentry *rt; 116128872Sandre 117128872Sandre /* 118128872Sandre * Find route to destination. 119128872Sandre */ 120128872Sandre bzero(ro, sizeof(*ro)); 121128872Sandre dst = (struct sockaddr_in *)&ro->ro_dst; 122128872Sandre dst->sin_family = AF_INET; 123128872Sandre dst->sin_len = sizeof(*dst); 124133497Sandre dst->sin_addr.s_addr = dest.s_addr; 125186119Sqingli in_rtalloc_ign(ro, 0, M_GETFIB(m)); 126128872Sandre 127128872Sandre /* 128128872Sandre * Route there and interface still up? 129128872Sandre */ 130128872Sandre rt = ro->ro_rt; 131128872Sandre if (rt && (rt->rt_flags & RTF_UP) && 132128872Sandre (rt->rt_ifp->if_flags & IFF_UP) && 133148887Srwatson (rt->rt_ifp->if_drv_flags & IFF_DRV_RUNNING)) { 134128872Sandre if (rt->rt_flags & RTF_GATEWAY) 135128872Sandre dst = (struct sockaddr_in *)rt->rt_gateway; 136128872Sandre } else { 137190951Srwatson IPSTAT_INC(ips_noroute); 138190951Srwatson IPSTAT_INC(ips_cantforward); 139128872Sandre if (rt) 140128872Sandre RTFREE(rt); 141145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 142128872Sandre return NULL; 143128872Sandre } 144128872Sandre return dst; 145128872Sandre} 146128872Sandre 147122702Sandre/* 148122702Sandre * Try to forward a packet based on the destination address. 149122702Sandre * This is a fast path optimized for the plain forwarding case. 150196881Spjd * If the packet is handled (and consumed) here then we return NULL; 151196881Spjd * otherwise mbuf is returned and the packet should be delivered 152122702Sandre * to ip_input for full processing. 153122702Sandre */ 154154518Sandrestruct mbuf * 155290383Sgnnip_tryforward(struct mbuf *m) 156122702Sandre{ 157122702Sandre struct ip *ip; 158122702Sandre struct mbuf *m0 = NULL; 159122702Sandre struct route ro; 160122702Sandre struct sockaddr_in *dst = NULL; 161128872Sandre struct ifnet *ifp; 162133497Sandre struct in_addr odest, dest; 163290383Sgnn uint16_t ip_len, ip_off; 164122702Sandre int error = 0; 165290383Sgnn int mtu; 166242079Sae struct m_tag *fwd_tag = NULL; 167122702Sandre 168122702Sandre /* 169122702Sandre * Are we active and forwarding packets? 170122702Sandre */ 171122702Sandre 172122702Sandre M_ASSERTVALID(m); 173122702Sandre M_ASSERTPKTHDR(m); 174122702Sandre 175191148Skmacy bzero(&ro, sizeof(ro)); 176128872Sandre 177122702Sandre 178133480Sandre#ifdef ALTQ 179122702Sandre /* 180133480Sandre * Is packet dropped by traffic conditioner? 181133480Sandre */ 182133480Sandre if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 183154518Sandre goto drop; 184133480Sandre#endif 185133480Sandre 186133480Sandre /* 187290383Sgnn * Only IP packets without options 188122702Sandre */ 189290383Sgnn ip = mtod(m, struct ip *); 190122702Sandre 191129017Sandre if (ip->ip_hl != (sizeof(struct ip) >> 2)) { 192271610Shrs if (V_ip_doopts == 1) 193154518Sandre return m; 194271610Shrs else if (V_ip_doopts == 2) { 195129017Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB, 196145863Sandre 0, 0); 197154518Sandre return NULL; /* mbuf already free'd */ 198129017Sandre } 199129017Sandre /* else ignore IP options and continue */ 200129017Sandre } 201122702Sandre 202122702Sandre /* 203122702Sandre * Only unicast IP, not from loopback, no L2 or IP broadcast, 204122702Sandre * no multicast, no INADDR_ANY 205122702Sandre * 206122702Sandre * XXX: Probably some of these checks could be direct drop 207122702Sandre * conditions. However it is not clear whether there are some 208298995Spfg * hacks or obscure behaviours which make it necessary to 209122702Sandre * let ip_input handle it. We play safe here and let ip_input 210122702Sandre * deal with it until it is proven that we can directly drop it. 211122702Sandre */ 212149369Sandre if ((m->m_flags & (M_BCAST|M_MCAST)) || 213149369Sandre (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) || 214122702Sandre ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST || 215122702Sandre ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST || 216122702Sandre IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 217122702Sandre IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 218166452Sbms IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) || 219166452Sbms IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) || 220149369Sandre ip->ip_src.s_addr == INADDR_ANY || 221122702Sandre ip->ip_dst.s_addr == INADDR_ANY ) 222154518Sandre return m; 223122702Sandre 224122702Sandre /* 225122702Sandre * Is it for a local address on this host? 226122702Sandre */ 227133497Sandre if (in_localip(ip->ip_dst)) 228154518Sandre return m; 229122702Sandre 230190951Srwatson IPSTAT_INC(ips_total); 231122702Sandre 232122702Sandre /* 233122702Sandre * Step 3: incoming packet firewall processing 234122702Sandre */ 235122702Sandre 236133497Sandre odest.s_addr = dest.s_addr = ip->ip_dst.s_addr; 237134383Sandre 238122702Sandre /* 239122702Sandre * Run through list of ipfilter hooks for input packets 240122702Sandre */ 241197952Sjulian if (!PFIL_HOOKED(&V_inet_pfil_hook)) 242134383Sandre goto passin; 243134383Sandre 244197952Sjulian if (pfil_run_hooks( 245197952Sjulian &V_inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL) || 246122702Sandre m == NULL) 247154518Sandre goto drop; 248122702Sandre 249122702Sandre M_ASSERTVALID(m); 250122702Sandre M_ASSERTPKTHDR(m); 251122702Sandre 252122702Sandre ip = mtod(m, struct ip *); /* m may have changed by pfil hook */ 253133497Sandre dest.s_addr = ip->ip_dst.s_addr; 254122702Sandre 255122702Sandre /* 256122702Sandre * Destination address changed? 257122702Sandre */ 258133497Sandre if (odest.s_addr != dest.s_addr) { 259122702Sandre /* 260122702Sandre * Is it now for a local address on this host? 261122702Sandre */ 262133497Sandre if (in_localip(dest)) 263133497Sandre goto forwardlocal; 264122702Sandre /* 265122702Sandre * Go on with new destination address 266122702Sandre */ 267122702Sandre } 268242079Sae 269133920Sandre if (m->m_flags & M_FASTFWD_OURS) { 270133920Sandre /* 271133920Sandre * ipfw changed it for a local address on this host. 272133920Sandre */ 273133920Sandre goto forwardlocal; 274133920Sandre } 275122702Sandre 276134383Sandrepassin: 277122702Sandre /* 278122702Sandre * Step 4: decrement TTL and look up route 279122702Sandre */ 280122702Sandre 281122702Sandre /* 282122702Sandre * Check TTL 283122702Sandre */ 284122702Sandre#ifdef IPSTEALTH 285181803Sbz if (!V_ipstealth) { 286122702Sandre#endif 287122702Sandre if (ip->ip_ttl <= IPTTLDEC) { 288145863Sandre icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0); 289154518Sandre return NULL; /* mbuf already free'd */ 290122702Sandre } 291122702Sandre 292122702Sandre /* 293136690Sandre * Decrement the TTL and incrementally change the IP header checksum. 294136690Sandre * Don't bother doing this with hw checksum offloading, it's faster 295136690Sandre * doing it right here. 296122702Sandre */ 297122702Sandre ip->ip_ttl -= IPTTLDEC; 298122702Sandre if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8)) 299122702Sandre ip->ip_sum -= ~htons(IPTTLDEC << 8); 300122702Sandre else 301122702Sandre ip->ip_sum += htons(IPTTLDEC << 8); 302122702Sandre#ifdef IPSTEALTH 303122702Sandre } 304122702Sandre#endif 305122702Sandre 306122702Sandre /* 307122702Sandre * Find route to destination. 308122702Sandre */ 309128872Sandre if ((dst = ip_findroute(&ro, dest, m)) == NULL) 310154518Sandre return NULL; /* icmp unreach already sent */ 311128872Sandre ifp = ro.ro_rt->rt_ifp; 312122702Sandre 313122702Sandre /* 314166507Sbms * Immediately drop blackholed traffic, and directed broadcasts 315166507Sbms * for either the all-ones or all-zero subnet addresses on 316166507Sbms * locally attached networks. 317137179Sbms */ 318166507Sbms if ((ro.ro_rt->rt_flags & (RTF_BLACKHOLE|RTF_BROADCAST)) != 0) 319137179Sbms goto drop; 320137179Sbms 321137179Sbms /* 322122702Sandre * Step 5: outgoing firewall packet processing 323122702Sandre */ 324122702Sandre 325122702Sandre /* 326122702Sandre * Run through list of hooks for output packets. 327122702Sandre */ 328197952Sjulian if (!PFIL_HOOKED(&V_inet_pfil_hook)) 329134383Sandre goto passout; 330134383Sandre 331197952Sjulian if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_OUT, NULL) || m == NULL) { 332154518Sandre goto drop; 333122702Sandre } 334122702Sandre 335122702Sandre M_ASSERTVALID(m); 336122702Sandre M_ASSERTPKTHDR(m); 337122702Sandre 338122702Sandre ip = mtod(m, struct ip *); 339133497Sandre dest.s_addr = ip->ip_dst.s_addr; 340122702Sandre 341122702Sandre /* 342122702Sandre * Destination address changed? 343122702Sandre */ 344242463Sae if (m->m_flags & M_IP_NEXTHOP) 345242079Sae fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 346133920Sandre if (odest.s_addr != dest.s_addr || fwd_tag != NULL) { 347122702Sandre /* 348122702Sandre * Is it now for a local address on this host? 349122702Sandre */ 350136690Sandre if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) { 351122702Sandreforwardlocal: 352133497Sandre /* 353135158Sandre * Return packet for processing by ip_input(). 354133497Sandre */ 355135158Sandre m->m_flags |= M_FASTFWD_OURS; 356133497Sandre if (ro.ro_rt) 357133497Sandre RTFREE(ro.ro_rt); 358154518Sandre return m; 359122702Sandre } 360122702Sandre /* 361122702Sandre * Redo route lookup with new destination address 362122702Sandre */ 363133920Sandre if (fwd_tag) { 364161380Sjulian dest.s_addr = ((struct sockaddr_in *) 365157833Sglebius (fwd_tag + 1))->sin_addr.s_addr; 366133920Sandre m_tag_delete(m, fwd_tag); 367242463Sae m->m_flags &= ~M_IP_NEXTHOP; 368133920Sandre } 369122702Sandre RTFREE(ro.ro_rt); 370128872Sandre if ((dst = ip_findroute(&ro, dest, m)) == NULL) 371154518Sandre return NULL; /* icmp unreach already sent */ 372128872Sandre ifp = ro.ro_rt->rt_ifp; 373122702Sandre } 374122702Sandre 375134383Sandrepassout: 376122702Sandre /* 377122702Sandre * Step 6: send off the packet 378122702Sandre */ 379241245Sglebius ip_len = ntohs(ip->ip_len); 380241245Sglebius ip_off = ntohs(ip->ip_off); 381122702Sandre 382122702Sandre /* 383128872Sandre * Check if route is dampned (when ARP is unable to resolve) 384128872Sandre */ 385128872Sandre if ((ro.ro_rt->rt_flags & RTF_REJECT) && 386262763Sglebius (ro.ro_rt->rt_expire == 0 || time_uptime < ro.ro_rt->rt_expire)) { 387145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 388128872Sandre goto consumed; 389128872Sandre } 390128872Sandre 391128872Sandre /* 392128872Sandre * Check if media link state of interface is not down 393128872Sandre */ 394128872Sandre if (ifp->if_link_state == LINK_STATE_DOWN) { 395145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 396128872Sandre goto consumed; 397128872Sandre } 398128872Sandre 399128872Sandre /* 400148324Skeramida * Check if packet fits MTU or if hardware will fragment for us 401122702Sandre */ 402262763Sglebius if (ro.ro_rt->rt_mtu) 403262763Sglebius mtu = min(ro.ro_rt->rt_mtu, ifp->if_mtu); 404122702Sandre else 405122702Sandre mtu = ifp->if_mtu; 406122702Sandre 407271006Sglebius if (ip_len <= mtu) { 408122702Sandre /* 409254523Sandre * Avoid confusing lower layers. 410254523Sandre */ 411254523Sandre m_clrprotoflags(m); 412254523Sandre /* 413122702Sandre * Send off the packet via outgoing interface 414122702Sandre */ 415254889Smarkj IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 416122702Sandre error = (*ifp->if_output)(ifp, m, 417191148Skmacy (struct sockaddr *)dst, &ro); 418122702Sandre } else { 419122702Sandre /* 420128872Sandre * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery 421122702Sandre */ 422241245Sglebius if (ip_off & IP_DF) { 423190951Srwatson IPSTAT_INC(ips_cantfrag); 424122702Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 425145863Sandre 0, mtu); 426128872Sandre goto consumed; 427122702Sandre } else { 428122702Sandre /* 429148324Skeramida * We have to fragment the packet 430122702Sandre */ 431122702Sandre m->m_pkthdr.csum_flags |= CSUM_IP; 432242161Sglebius if (ip_fragment(ip, &m, mtu, ifp->if_hwassist)) 433122702Sandre goto drop; 434122702Sandre KASSERT(m != NULL, ("null mbuf and no error")); 435122702Sandre /* 436122702Sandre * Send off the fragments via outgoing interface 437122702Sandre */ 438122702Sandre error = 0; 439122702Sandre do { 440122702Sandre m0 = m->m_nextpkt; 441122702Sandre m->m_nextpkt = NULL; 442254523Sandre /* 443254523Sandre * Avoid confusing lower layers. 444254523Sandre */ 445254523Sandre m_clrprotoflags(m); 446122702Sandre 447254889Smarkj IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 448122702Sandre error = (*ifp->if_output)(ifp, m, 449191148Skmacy (struct sockaddr *)dst, &ro); 450122702Sandre if (error) 451122702Sandre break; 452122702Sandre } while ((m = m0) != NULL); 453122702Sandre if (error) { 454122702Sandre /* Reclaim remaining fragments */ 455144301Sglebius for (m = m0; m; m = m0) { 456122702Sandre m0 = m->m_nextpkt; 457122702Sandre m_freem(m); 458122702Sandre } 459122702Sandre } else 460190951Srwatson IPSTAT_INC(ips_fragmented); 461122702Sandre } 462122702Sandre } 463122702Sandre 464122702Sandre if (error != 0) 465190951Srwatson IPSTAT_INC(ips_odropped); 466122702Sandre else { 467262763Sglebius counter_u64_add(ro.ro_rt->rt_pksent, 1); 468190951Srwatson IPSTAT_INC(ips_forward); 469190951Srwatson IPSTAT_INC(ips_fastforward); 470122702Sandre } 471128872Sandreconsumed: 472122702Sandre RTFREE(ro.ro_rt); 473154518Sandre return NULL; 474122702Sandredrop: 475122702Sandre if (m) 476122702Sandre m_freem(m); 477128872Sandre if (ro.ro_rt) 478128872Sandre RTFREE(ro.ro_rt); 479154518Sandre return NULL; 480122702Sandre} 481