ip_fastfwd.c revision 149369
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 * $FreeBSD: head/sys/netinet/ip_fastfwd.c 149369 2005-08-22 12:06:26Z andre $ 30122702Sandre */ 31122702Sandre 32122702Sandre/* 33122702Sandre * ip_fastforward gets its speed from processing the forwarded packet to 34122702Sandre * completion (if_output on the other side) without any queues or netisr's. 35122702Sandre * The receiving interface DMAs the packet into memory, the upper half of 36122702Sandre * driver calls ip_fastforward, we do our routing table lookup and directly 37148324Skeramida * send it off to the outgoing interface, which DMAs the packet to the 38122702Sandre * network card. The only part of the packet we touch with the CPU is the 39122759Sandre * IP header (unless there are complex firewall rules touching other parts 40122759Sandre * of the packet, but that is up to you). We are essentially limited by bus 41122759Sandre * bandwidth and how fast the network card/driver can set up receives and 42122759Sandre * transmits. 43122702Sandre * 44148324Skeramida * We handle basic errors, IP header errors, checksum errors, 45122702Sandre * destination unreachable, fragmentation and fragmentation needed and 46148324Skeramida * report them via ICMP to the sender. 47122702Sandre * 48122702Sandre * Else if something is not pure IPv4 unicast forwarding we fall back to 49122702Sandre * the normal ip_input processing path. We should only be called from 50122702Sandre * interfaces connected to the outside world. 51122702Sandre * 52122702Sandre * Firewalling is fully supported including divert, ipfw fwd and ipfilter 53122702Sandre * ipnat and address rewrite. 54122702Sandre * 55122702Sandre * IPSEC is not supported if this host is a tunnel broker. IPSEC is 56122702Sandre * supported for connections to/from local host. 57122702Sandre * 58122702Sandre * We try to do the least expensive (in CPU ops) checks and operations 59122702Sandre * first to catch junk with as little overhead as possible. 60122702Sandre * 61148324Skeramida * We take full advantage of hardware support for IP checksum and 62122702Sandre * fragmentation offloading. 63122702Sandre * 64122702Sandre * We don't do ICMP redirect in the fast forwarding path. I have had my own 65122702Sandre * cases where two core routers with Zebra routing suite would send millions 66148324Skeramida * ICMP redirects to connected hosts if the destination router was not the 67148324Skeramida * default gateway. In one case it was filling the routing table of a host 68148324Skeramida * with approximately 300.000 cloned redirect entries until it ran out of 69148324Skeramida * kernel memory. However the networking code proved very robust and it didn't 70148324Skeramida * crash or fail in other ways. 71122702Sandre */ 72122702Sandre 73122702Sandre/* 74122702Sandre * Many thanks to Matt Thomas of NetBSD for basic structure of ip_flow.c which 75122702Sandre * is being followed here. 76122702Sandre */ 77122702Sandre 78122702Sandre#include "opt_ipfw.h" 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> 87122702Sandre#include <sys/socket.h> 88122702Sandre#include <sys/sysctl.h> 89122702Sandre 90122702Sandre#include <net/pfil.h> 91122702Sandre#include <net/if.h> 92122702Sandre#include <net/if_types.h> 93122702Sandre#include <net/if_var.h> 94122702Sandre#include <net/if_dl.h> 95122702Sandre#include <net/route.h> 96122702Sandre 97122702Sandre#include <netinet/in.h> 98122702Sandre#include <netinet/in_systm.h> 99122702Sandre#include <netinet/in_var.h> 100122702Sandre#include <netinet/ip.h> 101122702Sandre#include <netinet/ip_var.h> 102122702Sandre#include <netinet/ip_icmp.h> 103122702Sandre 104122702Sandre#include <machine/in_cksum.h> 105122702Sandre 106122702Sandrestatic int ipfastforward_active = 0; 107122702SandreSYSCTL_INT(_net_inet_ip, OID_AUTO, fastforwarding, CTLFLAG_RW, 108122702Sandre &ipfastforward_active, 0, "Enable fast IP forwarding"); 109122702Sandre 110128872Sandrestatic struct sockaddr_in * 111133497Sandreip_findroute(struct route *ro, struct in_addr dest, struct mbuf *m) 112128872Sandre{ 113128872Sandre struct sockaddr_in *dst; 114128872Sandre struct rtentry *rt; 115128872Sandre 116128872Sandre /* 117128872Sandre * Find route to destination. 118128872Sandre */ 119128872Sandre bzero(ro, sizeof(*ro)); 120128872Sandre dst = (struct sockaddr_in *)&ro->ro_dst; 121128872Sandre dst->sin_family = AF_INET; 122128872Sandre dst->sin_len = sizeof(*dst); 123133497Sandre dst->sin_addr.s_addr = dest.s_addr; 124128872Sandre rtalloc_ign(ro, RTF_CLONING); 125128872Sandre 126128872Sandre /* 127128872Sandre * Route there and interface still up? 128128872Sandre */ 129128872Sandre rt = ro->ro_rt; 130128872Sandre if (rt && (rt->rt_flags & RTF_UP) && 131128872Sandre (rt->rt_ifp->if_flags & IFF_UP) && 132148887Srwatson (rt->rt_ifp->if_drv_flags & IFF_DRV_RUNNING)) { 133128872Sandre if (rt->rt_flags & RTF_GATEWAY) 134128872Sandre dst = (struct sockaddr_in *)rt->rt_gateway; 135128872Sandre } else { 136128872Sandre ipstat.ips_noroute++; 137128872Sandre ipstat.ips_cantforward++; 138128872Sandre if (rt) 139128872Sandre RTFREE(rt); 140145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 141128872Sandre return NULL; 142128872Sandre } 143128872Sandre return dst; 144128872Sandre} 145128872Sandre 146122702Sandre/* 147122702Sandre * Try to forward a packet based on the destination address. 148122702Sandre * This is a fast path optimized for the plain forwarding case. 149122702Sandre * If the packet is handled (and consumed) here then we return 1; 150122702Sandre * otherwise 0 is returned and the packet should be delivered 151122702Sandre * to ip_input for full processing. 152122702Sandre */ 153122702Sandreint 154122702Sandreip_fastforward(struct mbuf *m) 155122702Sandre{ 156122702Sandre struct ip *ip; 157122702Sandre struct mbuf *m0 = NULL; 158122702Sandre struct route ro; 159122702Sandre struct sockaddr_in *dst = NULL; 160128872Sandre struct ifnet *ifp; 161133497Sandre struct in_addr odest, dest; 162128872Sandre u_short sum, ip_len; 163122702Sandre int error = 0; 164133920Sandre int hlen, mtu; 165133920Sandre#ifdef IPFIREWALL_FORWARD 166133920Sandre struct m_tag *fwd_tag; 167133920Sandre#endif 168122702Sandre 169122702Sandre /* 170122702Sandre * Are we active and forwarding packets? 171122702Sandre */ 172122702Sandre if (!ipfastforward_active || !ipforwarding) 173122702Sandre return 0; 174122702Sandre 175122702Sandre M_ASSERTVALID(m); 176122702Sandre M_ASSERTPKTHDR(m); 177122702Sandre 178128872Sandre ro.ro_rt = NULL; 179128872Sandre 180122702Sandre /* 181122702Sandre * Step 1: check for packet drop conditions (and sanity checks) 182122702Sandre */ 183122702Sandre 184122702Sandre /* 185122702Sandre * Is entire packet big enough? 186122702Sandre */ 187122702Sandre if (m->m_pkthdr.len < sizeof(struct ip)) { 188122702Sandre ipstat.ips_tooshort++; 189122702Sandre goto drop; 190122702Sandre } 191122702Sandre 192122702Sandre /* 193122702Sandre * Is first mbuf large enough for ip header and is header present? 194122702Sandre */ 195122702Sandre if (m->m_len < sizeof (struct ip) && 196137302Sandre (m = m_pullup(m, sizeof (struct ip))) == NULL) { 197122702Sandre ipstat.ips_toosmall++; 198137302Sandre return 1; /* mbuf already free'd */ 199122702Sandre } 200122702Sandre 201122702Sandre ip = mtod(m, struct ip *); 202122702Sandre 203122702Sandre /* 204122702Sandre * Is it IPv4? 205122702Sandre */ 206122702Sandre if (ip->ip_v != IPVERSION) { 207122702Sandre ipstat.ips_badvers++; 208122702Sandre goto drop; 209122702Sandre } 210122702Sandre 211122702Sandre /* 212122702Sandre * Is IP header length correct and is it in first mbuf? 213122702Sandre */ 214122702Sandre hlen = ip->ip_hl << 2; 215122702Sandre if (hlen < sizeof(struct ip)) { /* minimum header length */ 216122702Sandre ipstat.ips_badlen++; 217122702Sandre goto drop; 218122702Sandre } 219122702Sandre if (hlen > m->m_len) { 220122702Sandre if ((m = m_pullup(m, hlen)) == 0) { 221122702Sandre ipstat.ips_badhlen++; 222137450Sandre return 1; 223122702Sandre } 224122702Sandre ip = mtod(m, struct ip *); 225122702Sandre } 226122702Sandre 227122702Sandre /* 228122702Sandre * Checksum correct? 229122702Sandre */ 230122702Sandre if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) 231122702Sandre sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 232122702Sandre else { 233122702Sandre if (hlen == sizeof(struct ip)) 234122702Sandre sum = in_cksum_hdr(ip); 235122702Sandre else 236122702Sandre sum = in_cksum(m, hlen); 237122702Sandre } 238122702Sandre if (sum) { 239122702Sandre ipstat.ips_badsum++; 240122702Sandre goto drop; 241122702Sandre } 242136690Sandre 243136690Sandre /* 244148324Skeramida * Remember that we have checked the IP header and found it valid. 245136690Sandre */ 246122702Sandre m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID); 247122702Sandre 248128872Sandre ip_len = ntohs(ip->ip_len); 249122702Sandre 250122702Sandre /* 251122702Sandre * Is IP length longer than packet we have got? 252122702Sandre */ 253128872Sandre if (m->m_pkthdr.len < ip_len) { 254122702Sandre ipstat.ips_tooshort++; 255122702Sandre goto drop; 256122702Sandre } 257122702Sandre 258122702Sandre /* 259122702Sandre * Is packet longer than IP header tells us? If yes, truncate packet. 260122702Sandre */ 261128872Sandre if (m->m_pkthdr.len > ip_len) { 262122702Sandre if (m->m_len == m->m_pkthdr.len) { 263128872Sandre m->m_len = ip_len; 264128872Sandre m->m_pkthdr.len = ip_len; 265122702Sandre } else 266128872Sandre m_adj(m, ip_len - m->m_pkthdr.len); 267122702Sandre } 268122702Sandre 269122702Sandre /* 270122702Sandre * Is packet from or to 127/8? 271122702Sandre */ 272122702Sandre if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 273122702Sandre (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 274122702Sandre ipstat.ips_badaddr++; 275122702Sandre goto drop; 276122702Sandre } 277122702Sandre 278133480Sandre#ifdef ALTQ 279122702Sandre /* 280133480Sandre * Is packet dropped by traffic conditioner? 281133480Sandre */ 282133480Sandre if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 283133480Sandre return 1; 284133480Sandre#endif 285133480Sandre 286133480Sandre /* 287122702Sandre * Step 2: fallback conditions to normal ip_input path processing 288122702Sandre */ 289122702Sandre 290122702Sandre /* 291122702Sandre * Only IP packets without options 292122702Sandre */ 293129017Sandre if (ip->ip_hl != (sizeof(struct ip) >> 2)) { 294129017Sandre if (ip_doopts == 1) 295129017Sandre return 0; 296129017Sandre else if (ip_doopts == 2) { 297129017Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB, 298145863Sandre 0, 0); 299129017Sandre return 1; 300129017Sandre } 301129017Sandre /* else ignore IP options and continue */ 302129017Sandre } 303122702Sandre 304122702Sandre /* 305122702Sandre * Only unicast IP, not from loopback, no L2 or IP broadcast, 306122702Sandre * no multicast, no INADDR_ANY 307122702Sandre * 308122702Sandre * XXX: Probably some of these checks could be direct drop 309122702Sandre * conditions. However it is not clear whether there are some 310122702Sandre * hacks or obscure behaviours which make it neccessary to 311122702Sandre * let ip_input handle it. We play safe here and let ip_input 312122702Sandre * deal with it until it is proven that we can directly drop it. 313122702Sandre */ 314149369Sandre if ((m->m_flags & (M_BCAST|M_MCAST)) || 315149369Sandre (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) || 316122702Sandre ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST || 317122702Sandre ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST || 318122702Sandre IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 319122702Sandre IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 320149369Sandre ip->ip_src.s_addr == INADDR_ANY || 321122702Sandre ip->ip_dst.s_addr == INADDR_ANY ) 322128872Sandre return 0; 323122702Sandre 324122702Sandre /* 325122702Sandre * Is it for a local address on this host? 326122702Sandre */ 327133497Sandre if (in_localip(ip->ip_dst)) 328133497Sandre return 0; 329122702Sandre 330122702Sandre ipstat.ips_total++; 331122702Sandre 332122702Sandre /* 333122702Sandre * Step 3: incoming packet firewall processing 334122702Sandre */ 335122702Sandre 336128872Sandre /* 337128872Sandre * Convert to host representation 338128872Sandre */ 339128872Sandre ip->ip_len = ntohs(ip->ip_len); 340128872Sandre ip->ip_off = ntohs(ip->ip_off); 341128872Sandre 342133497Sandre odest.s_addr = dest.s_addr = ip->ip_dst.s_addr; 343134383Sandre 344122702Sandre /* 345122702Sandre * Run through list of ipfilter hooks for input packets 346122702Sandre */ 347134383Sandre if (inet_pfil_hook.ph_busy_count == -1) 348134383Sandre goto passin; 349134383Sandre 350135920Smlaier if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL) || 351122702Sandre m == NULL) 352122702Sandre return 1; 353122702Sandre 354122702Sandre M_ASSERTVALID(m); 355122702Sandre M_ASSERTPKTHDR(m); 356122702Sandre 357122702Sandre ip = mtod(m, struct ip *); /* m may have changed by pfil hook */ 358133497Sandre dest.s_addr = ip->ip_dst.s_addr; 359122702Sandre 360122702Sandre /* 361122702Sandre * Destination address changed? 362122702Sandre */ 363133497Sandre if (odest.s_addr != dest.s_addr) { 364122702Sandre /* 365122702Sandre * Is it now for a local address on this host? 366122702Sandre */ 367133497Sandre if (in_localip(dest)) 368133497Sandre goto forwardlocal; 369122702Sandre /* 370122702Sandre * Go on with new destination address 371122702Sandre */ 372122702Sandre } 373133920Sandre#ifdef IPFIREWALL_FORWARD 374133920Sandre if (m->m_flags & M_FASTFWD_OURS) { 375133920Sandre /* 376133920Sandre * ipfw changed it for a local address on this host. 377133920Sandre */ 378133920Sandre goto forwardlocal; 379133920Sandre } 380133920Sandre#endif /* IPFIREWALL_FORWARD */ 381122702Sandre 382134383Sandrepassin: 383122702Sandre /* 384122702Sandre * Step 4: decrement TTL and look up route 385122702Sandre */ 386122702Sandre 387122702Sandre /* 388122702Sandre * Check TTL 389122702Sandre */ 390122702Sandre#ifdef IPSTEALTH 391122702Sandre if (!ipstealth) { 392122702Sandre#endif 393122702Sandre if (ip->ip_ttl <= IPTTLDEC) { 394145863Sandre icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0); 395122702Sandre return 1; 396122702Sandre } 397122702Sandre 398122702Sandre /* 399136690Sandre * Decrement the TTL and incrementally change the IP header checksum. 400136690Sandre * Don't bother doing this with hw checksum offloading, it's faster 401136690Sandre * doing it right here. 402122702Sandre */ 403122702Sandre ip->ip_ttl -= IPTTLDEC; 404122702Sandre if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8)) 405122702Sandre ip->ip_sum -= ~htons(IPTTLDEC << 8); 406122702Sandre else 407122702Sandre ip->ip_sum += htons(IPTTLDEC << 8); 408122702Sandre#ifdef IPSTEALTH 409122702Sandre } 410122702Sandre#endif 411122702Sandre 412122702Sandre /* 413122702Sandre * Find route to destination. 414122702Sandre */ 415128872Sandre if ((dst = ip_findroute(&ro, dest, m)) == NULL) 416128872Sandre return 1; /* icmp unreach already sent */ 417128872Sandre ifp = ro.ro_rt->rt_ifp; 418122702Sandre 419122702Sandre /* 420137179Sbms * Immediately drop blackholed traffic. 421137179Sbms */ 422137179Sbms if (ro.ro_rt->rt_flags & RTF_BLACKHOLE) 423137179Sbms goto drop; 424137179Sbms 425137179Sbms /* 426122702Sandre * Step 5: outgoing firewall packet processing 427122702Sandre */ 428122702Sandre 429122702Sandre /* 430122702Sandre * Run through list of hooks for output packets. 431122702Sandre */ 432134383Sandre if (inet_pfil_hook.ph_busy_count == -1) 433134383Sandre goto passout; 434134383Sandre 435135920Smlaier if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, NULL) || m == NULL) { 436128872Sandre goto consumed; 437122702Sandre } 438122702Sandre 439122702Sandre M_ASSERTVALID(m); 440122702Sandre M_ASSERTPKTHDR(m); 441122702Sandre 442122702Sandre ip = mtod(m, struct ip *); 443133497Sandre dest.s_addr = ip->ip_dst.s_addr; 444122702Sandre 445122702Sandre /* 446122702Sandre * Destination address changed? 447122702Sandre */ 448133920Sandre#ifndef IPFIREWALL_FORWARD 449133497Sandre if (odest.s_addr != dest.s_addr) { 450133920Sandre#else 451133920Sandre fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 452133920Sandre if (odest.s_addr != dest.s_addr || fwd_tag != NULL) { 453133920Sandre#endif /* IPFIREWALL_FORWARD */ 454122702Sandre /* 455122702Sandre * Is it now for a local address on this host? 456122702Sandre */ 457133920Sandre#ifndef IPFIREWALL_FORWARD 458133497Sandre if (in_localip(dest)) { 459133920Sandre#else 460136690Sandre if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) { 461133920Sandre#endif /* IPFIREWALL_FORWARD */ 462122702Sandreforwardlocal: 463133497Sandre /* 464135158Sandre * Return packet for processing by ip_input(). 465135158Sandre * Keep host byte order as expected at ip_input's 466135158Sandre * "ours"-label. 467133497Sandre */ 468135158Sandre m->m_flags |= M_FASTFWD_OURS; 469133497Sandre if (ro.ro_rt) 470133497Sandre RTFREE(ro.ro_rt); 471133497Sandre return 0; 472122702Sandre } 473122702Sandre /* 474122702Sandre * Redo route lookup with new destination address 475122702Sandre */ 476133920Sandre#ifdef IPFIREWALL_FORWARD 477133920Sandre if (fwd_tag) { 478133920Sandre if (!in_localip(ip->ip_src) && !in_localaddr(ip->ip_dst)) 479133920Sandre dest.s_addr = ((struct sockaddr_in *)(fwd_tag+1))->sin_addr.s_addr; 480133920Sandre m_tag_delete(m, fwd_tag); 481133920Sandre } 482133920Sandre#endif /* IPFIREWALL_FORWARD */ 483122702Sandre RTFREE(ro.ro_rt); 484128872Sandre if ((dst = ip_findroute(&ro, dest, m)) == NULL) 485128872Sandre return 1; /* icmp unreach already sent */ 486128872Sandre ifp = ro.ro_rt->rt_ifp; 487122702Sandre } 488122702Sandre 489134383Sandrepassout: 490122702Sandre /* 491122702Sandre * Step 6: send off the packet 492122702Sandre */ 493122702Sandre 494122702Sandre /* 495128872Sandre * Check if route is dampned (when ARP is unable to resolve) 496128872Sandre */ 497128872Sandre if ((ro.ro_rt->rt_flags & RTF_REJECT) && 498128872Sandre ro.ro_rt->rt_rmx.rmx_expire >= time_second) { 499145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 500128872Sandre goto consumed; 501128872Sandre } 502128872Sandre 503133480Sandre#ifndef ALTQ 504128872Sandre /* 505128872Sandre * Check if there is enough space in the interface queue 506128872Sandre */ 507128872Sandre if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 508128872Sandre ifp->if_snd.ifq_maxlen) { 509128872Sandre ipstat.ips_odropped++; 510128872Sandre /* would send source quench here but that is depreciated */ 511128872Sandre goto drop; 512128872Sandre } 513133480Sandre#endif 514128872Sandre 515128872Sandre /* 516128872Sandre * Check if media link state of interface is not down 517128872Sandre */ 518128872Sandre if (ifp->if_link_state == LINK_STATE_DOWN) { 519145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 520128872Sandre goto consumed; 521128872Sandre } 522128872Sandre 523128872Sandre /* 524148324Skeramida * Check if packet fits MTU or if hardware will fragment for us 525122702Sandre */ 526122702Sandre if (ro.ro_rt->rt_rmx.rmx_mtu) 527122702Sandre mtu = min(ro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu); 528122702Sandre else 529122702Sandre mtu = ifp->if_mtu; 530122702Sandre 531122702Sandre if (ip->ip_len <= mtu || 532122702Sandre (ifp->if_hwassist & CSUM_FRAGMENT && (ip->ip_off & IP_DF) == 0)) { 533122702Sandre /* 534122702Sandre * Restore packet header fields to original values 535122702Sandre */ 536122702Sandre ip->ip_len = htons(ip->ip_len); 537122702Sandre ip->ip_off = htons(ip->ip_off); 538122702Sandre /* 539122702Sandre * Send off the packet via outgoing interface 540122702Sandre */ 541122702Sandre error = (*ifp->if_output)(ifp, m, 542122702Sandre (struct sockaddr *)dst, ro.ro_rt); 543122702Sandre } else { 544122702Sandre /* 545128872Sandre * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery 546122702Sandre */ 547122702Sandre if (ip->ip_off & IP_DF) { 548128872Sandre ipstat.ips_cantfrag++; 549122702Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 550145863Sandre 0, mtu); 551128872Sandre goto consumed; 552122702Sandre } else { 553122702Sandre /* 554148324Skeramida * We have to fragment the packet 555122702Sandre */ 556122702Sandre m->m_pkthdr.csum_flags |= CSUM_IP; 557128872Sandre /* 558128872Sandre * ip_fragment expects ip_len and ip_off in host byte 559128872Sandre * order but returns all packets in network byte order 560128872Sandre */ 561122702Sandre if (ip_fragment(ip, &m, mtu, ifp->if_hwassist, 562122702Sandre (~ifp->if_hwassist & CSUM_DELAY_IP))) { 563122702Sandre goto drop; 564122702Sandre } 565122702Sandre KASSERT(m != NULL, ("null mbuf and no error")); 566122702Sandre /* 567122702Sandre * Send off the fragments via outgoing interface 568122702Sandre */ 569122702Sandre error = 0; 570122702Sandre do { 571122702Sandre m0 = m->m_nextpkt; 572122702Sandre m->m_nextpkt = NULL; 573122702Sandre 574122702Sandre error = (*ifp->if_output)(ifp, m, 575122702Sandre (struct sockaddr *)dst, ro.ro_rt); 576122702Sandre if (error) 577122702Sandre break; 578122702Sandre } while ((m = m0) != NULL); 579122702Sandre if (error) { 580122702Sandre /* Reclaim remaining fragments */ 581144301Sglebius for (m = m0; m; m = m0) { 582122702Sandre m0 = m->m_nextpkt; 583122702Sandre m_freem(m); 584122702Sandre } 585122702Sandre } else 586122702Sandre ipstat.ips_fragmented++; 587122702Sandre } 588122702Sandre } 589122702Sandre 590122702Sandre if (error != 0) 591122702Sandre ipstat.ips_odropped++; 592122702Sandre else { 593122702Sandre ro.ro_rt->rt_rmx.rmx_pksent++; 594122702Sandre ipstat.ips_forward++; 595122702Sandre ipstat.ips_fastforward++; 596122702Sandre } 597128872Sandreconsumed: 598122702Sandre RTFREE(ro.ro_rt); 599122702Sandre return 1; 600122702Sandredrop: 601122702Sandre if (m) 602122702Sandre m_freem(m); 603128872Sandre if (ro.ro_rt) 604128872Sandre RTFREE(ro.ro_rt); 605122702Sandre return 1; 606122702Sandre} 607