ip_fastfwd.c revision 172467
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 172467 2007-10-07 20:44:24Z silby $"); 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> 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> 97122702Sandre 98122702Sandre#include <netinet/in.h> 99122702Sandre#include <netinet/in_systm.h> 100122702Sandre#include <netinet/in_var.h> 101122702Sandre#include <netinet/ip.h> 102122702Sandre#include <netinet/ip_var.h> 103122702Sandre#include <netinet/ip_icmp.h> 104152592Sandre#include <netinet/ip_options.h> 105122702Sandre 106122702Sandre#include <machine/in_cksum.h> 107122702Sandre 108122702Sandrestatic int ipfastforward_active = 0; 109122702SandreSYSCTL_INT(_net_inet_ip, OID_AUTO, fastforwarding, CTLFLAG_RW, 110122702Sandre &ipfastforward_active, 0, "Enable fast IP forwarding"); 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; 126128872Sandre rtalloc_ign(ro, RTF_CLONING); 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 { 138128872Sandre ipstat.ips_noroute++; 139128872Sandre ipstat.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. 151122702Sandre * If the packet is handled (and consumed) here then we return 1; 152122702Sandre * otherwise 0 is returned and the packet should be delivered 153122702Sandre * to ip_input for full processing. 154122702Sandre */ 155154518Sandrestruct mbuf * 156122702Sandreip_fastforward(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; 164128872Sandre u_short sum, ip_len; 165122702Sandre int error = 0; 166133920Sandre int hlen, mtu; 167133920Sandre#ifdef IPFIREWALL_FORWARD 168133920Sandre struct m_tag *fwd_tag; 169133920Sandre#endif 170122702Sandre 171122702Sandre /* 172122702Sandre * Are we active and forwarding packets? 173122702Sandre */ 174122702Sandre if (!ipfastforward_active || !ipforwarding) 175154518Sandre return m; 176122702Sandre 177122702Sandre M_ASSERTVALID(m); 178122702Sandre M_ASSERTPKTHDR(m); 179122702Sandre 180128872Sandre ro.ro_rt = NULL; 181128872Sandre 182122702Sandre /* 183122702Sandre * Step 1: check for packet drop conditions (and sanity checks) 184122702Sandre */ 185122702Sandre 186122702Sandre /* 187122702Sandre * Is entire packet big enough? 188122702Sandre */ 189122702Sandre if (m->m_pkthdr.len < sizeof(struct ip)) { 190122702Sandre ipstat.ips_tooshort++; 191122702Sandre goto drop; 192122702Sandre } 193122702Sandre 194122702Sandre /* 195122702Sandre * Is first mbuf large enough for ip header and is header present? 196122702Sandre */ 197122702Sandre if (m->m_len < sizeof (struct ip) && 198137302Sandre (m = m_pullup(m, sizeof (struct ip))) == NULL) { 199122702Sandre ipstat.ips_toosmall++; 200154518Sandre return NULL; /* mbuf already free'd */ 201122702Sandre } 202122702Sandre 203122702Sandre ip = mtod(m, struct ip *); 204122702Sandre 205122702Sandre /* 206122702Sandre * Is it IPv4? 207122702Sandre */ 208122702Sandre if (ip->ip_v != IPVERSION) { 209122702Sandre ipstat.ips_badvers++; 210122702Sandre goto drop; 211122702Sandre } 212122702Sandre 213122702Sandre /* 214122702Sandre * Is IP header length correct and is it in first mbuf? 215122702Sandre */ 216122702Sandre hlen = ip->ip_hl << 2; 217122702Sandre if (hlen < sizeof(struct ip)) { /* minimum header length */ 218122702Sandre ipstat.ips_badlen++; 219122702Sandre goto drop; 220122702Sandre } 221122702Sandre if (hlen > m->m_len) { 222154518Sandre if ((m = m_pullup(m, hlen)) == NULL) { 223122702Sandre ipstat.ips_badhlen++; 224154518Sandre return NULL; /* mbuf already free'd */ 225122702Sandre } 226122702Sandre ip = mtod(m, struct ip *); 227122702Sandre } 228122702Sandre 229122702Sandre /* 230122702Sandre * Checksum correct? 231122702Sandre */ 232122702Sandre if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) 233122702Sandre sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 234122702Sandre else { 235122702Sandre if (hlen == sizeof(struct ip)) 236122702Sandre sum = in_cksum_hdr(ip); 237122702Sandre else 238122702Sandre sum = in_cksum(m, hlen); 239122702Sandre } 240122702Sandre if (sum) { 241122702Sandre ipstat.ips_badsum++; 242122702Sandre goto drop; 243122702Sandre } 244136690Sandre 245136690Sandre /* 246148324Skeramida * Remember that we have checked the IP header and found it valid. 247136690Sandre */ 248122702Sandre m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID); 249122702Sandre 250128872Sandre ip_len = ntohs(ip->ip_len); 251122702Sandre 252122702Sandre /* 253122702Sandre * Is IP length longer than packet we have got? 254122702Sandre */ 255128872Sandre if (m->m_pkthdr.len < ip_len) { 256122702Sandre ipstat.ips_tooshort++; 257122702Sandre goto drop; 258122702Sandre } 259122702Sandre 260122702Sandre /* 261122702Sandre * Is packet longer than IP header tells us? If yes, truncate packet. 262122702Sandre */ 263128872Sandre if (m->m_pkthdr.len > ip_len) { 264122702Sandre if (m->m_len == m->m_pkthdr.len) { 265128872Sandre m->m_len = ip_len; 266128872Sandre m->m_pkthdr.len = ip_len; 267122702Sandre } else 268128872Sandre m_adj(m, ip_len - m->m_pkthdr.len); 269122702Sandre } 270122702Sandre 271122702Sandre /* 272122702Sandre * Is packet from or to 127/8? 273122702Sandre */ 274122702Sandre if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 275122702Sandre (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 276122702Sandre ipstat.ips_badaddr++; 277122702Sandre goto drop; 278122702Sandre } 279122702Sandre 280133480Sandre#ifdef ALTQ 281122702Sandre /* 282133480Sandre * Is packet dropped by traffic conditioner? 283133480Sandre */ 284133480Sandre if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 285154518Sandre goto drop; 286133480Sandre#endif 287133480Sandre 288133480Sandre /* 289122702Sandre * Step 2: fallback conditions to normal ip_input path processing 290122702Sandre */ 291122702Sandre 292122702Sandre /* 293122702Sandre * Only IP packets without options 294122702Sandre */ 295129017Sandre if (ip->ip_hl != (sizeof(struct ip) >> 2)) { 296129017Sandre if (ip_doopts == 1) 297154518Sandre return m; 298129017Sandre else if (ip_doopts == 2) { 299129017Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB, 300145863Sandre 0, 0); 301154518Sandre return NULL; /* mbuf already free'd */ 302129017Sandre } 303129017Sandre /* else ignore IP options and continue */ 304129017Sandre } 305122702Sandre 306122702Sandre /* 307122702Sandre * Only unicast IP, not from loopback, no L2 or IP broadcast, 308122702Sandre * no multicast, no INADDR_ANY 309122702Sandre * 310122702Sandre * XXX: Probably some of these checks could be direct drop 311122702Sandre * conditions. However it is not clear whether there are some 312122702Sandre * hacks or obscure behaviours which make it neccessary to 313122702Sandre * let ip_input handle it. We play safe here and let ip_input 314122702Sandre * deal with it until it is proven that we can directly drop it. 315122702Sandre */ 316149369Sandre if ((m->m_flags & (M_BCAST|M_MCAST)) || 317149369Sandre (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) || 318122702Sandre ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST || 319122702Sandre ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST || 320122702Sandre IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 321122702Sandre IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 322166452Sbms IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) || 323166452Sbms IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) || 324149369Sandre ip->ip_src.s_addr == INADDR_ANY || 325122702Sandre ip->ip_dst.s_addr == INADDR_ANY ) 326154518Sandre return m; 327122702Sandre 328122702Sandre /* 329122702Sandre * Is it for a local address on this host? 330122702Sandre */ 331133497Sandre if (in_localip(ip->ip_dst)) 332154518Sandre return m; 333122702Sandre 334122702Sandre ipstat.ips_total++; 335122702Sandre 336122702Sandre /* 337122702Sandre * Step 3: incoming packet firewall processing 338122702Sandre */ 339122702Sandre 340128872Sandre /* 341128872Sandre * Convert to host representation 342128872Sandre */ 343128872Sandre ip->ip_len = ntohs(ip->ip_len); 344128872Sandre ip->ip_off = ntohs(ip->ip_off); 345128872Sandre 346133497Sandre odest.s_addr = dest.s_addr = ip->ip_dst.s_addr; 347134383Sandre 348122702Sandre /* 349122702Sandre * Run through list of ipfilter hooks for input packets 350122702Sandre */ 351155201Scsjp if (!PFIL_HOOKED(&inet_pfil_hook)) 352134383Sandre goto passin; 353134383Sandre 354135920Smlaier if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL) || 355122702Sandre m == NULL) 356154518Sandre goto drop; 357122702Sandre 358122702Sandre M_ASSERTVALID(m); 359122702Sandre M_ASSERTPKTHDR(m); 360122702Sandre 361122702Sandre ip = mtod(m, struct ip *); /* m may have changed by pfil hook */ 362133497Sandre dest.s_addr = ip->ip_dst.s_addr; 363122702Sandre 364122702Sandre /* 365122702Sandre * Destination address changed? 366122702Sandre */ 367133497Sandre if (odest.s_addr != dest.s_addr) { 368122702Sandre /* 369122702Sandre * Is it now for a local address on this host? 370122702Sandre */ 371133497Sandre if (in_localip(dest)) 372133497Sandre goto forwardlocal; 373122702Sandre /* 374122702Sandre * Go on with new destination address 375122702Sandre */ 376122702Sandre } 377133920Sandre#ifdef IPFIREWALL_FORWARD 378133920Sandre if (m->m_flags & M_FASTFWD_OURS) { 379133920Sandre /* 380133920Sandre * ipfw changed it for a local address on this host. 381133920Sandre */ 382133920Sandre goto forwardlocal; 383133920Sandre } 384133920Sandre#endif /* IPFIREWALL_FORWARD */ 385122702Sandre 386134383Sandrepassin: 387122702Sandre /* 388122702Sandre * Step 4: decrement TTL and look up route 389122702Sandre */ 390122702Sandre 391122702Sandre /* 392122702Sandre * Check TTL 393122702Sandre */ 394122702Sandre#ifdef IPSTEALTH 395122702Sandre if (!ipstealth) { 396122702Sandre#endif 397122702Sandre if (ip->ip_ttl <= IPTTLDEC) { 398145863Sandre icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0); 399154518Sandre return NULL; /* mbuf already free'd */ 400122702Sandre } 401122702Sandre 402122702Sandre /* 403136690Sandre * Decrement the TTL and incrementally change the IP header checksum. 404136690Sandre * Don't bother doing this with hw checksum offloading, it's faster 405136690Sandre * doing it right here. 406122702Sandre */ 407122702Sandre ip->ip_ttl -= IPTTLDEC; 408122702Sandre if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8)) 409122702Sandre ip->ip_sum -= ~htons(IPTTLDEC << 8); 410122702Sandre else 411122702Sandre ip->ip_sum += htons(IPTTLDEC << 8); 412122702Sandre#ifdef IPSTEALTH 413122702Sandre } 414122702Sandre#endif 415122702Sandre 416122702Sandre /* 417122702Sandre * Find route to destination. 418122702Sandre */ 419128872Sandre if ((dst = ip_findroute(&ro, dest, m)) == NULL) 420154518Sandre return NULL; /* icmp unreach already sent */ 421128872Sandre ifp = ro.ro_rt->rt_ifp; 422122702Sandre 423122702Sandre /* 424166507Sbms * Immediately drop blackholed traffic, and directed broadcasts 425166507Sbms * for either the all-ones or all-zero subnet addresses on 426166507Sbms * locally attached networks. 427137179Sbms */ 428166507Sbms if ((ro.ro_rt->rt_flags & (RTF_BLACKHOLE|RTF_BROADCAST)) != 0) 429137179Sbms goto drop; 430137179Sbms 431137179Sbms /* 432122702Sandre * Step 5: outgoing firewall packet processing 433122702Sandre */ 434122702Sandre 435122702Sandre /* 436122702Sandre * Run through list of hooks for output packets. 437122702Sandre */ 438155201Scsjp if (!PFIL_HOOKED(&inet_pfil_hook)) 439134383Sandre goto passout; 440134383Sandre 441135920Smlaier if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, NULL) || m == NULL) { 442154518Sandre goto drop; 443122702Sandre } 444122702Sandre 445122702Sandre M_ASSERTVALID(m); 446122702Sandre M_ASSERTPKTHDR(m); 447122702Sandre 448122702Sandre ip = mtod(m, struct ip *); 449133497Sandre dest.s_addr = ip->ip_dst.s_addr; 450122702Sandre 451122702Sandre /* 452122702Sandre * Destination address changed? 453122702Sandre */ 454133920Sandre#ifndef IPFIREWALL_FORWARD 455133497Sandre if (odest.s_addr != dest.s_addr) { 456133920Sandre#else 457133920Sandre fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 458133920Sandre if (odest.s_addr != dest.s_addr || fwd_tag != NULL) { 459133920Sandre#endif /* IPFIREWALL_FORWARD */ 460122702Sandre /* 461122702Sandre * Is it now for a local address on this host? 462122702Sandre */ 463133920Sandre#ifndef IPFIREWALL_FORWARD 464133497Sandre if (in_localip(dest)) { 465133920Sandre#else 466136690Sandre if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) { 467133920Sandre#endif /* IPFIREWALL_FORWARD */ 468122702Sandreforwardlocal: 469133497Sandre /* 470135158Sandre * Return packet for processing by ip_input(). 471135158Sandre * Keep host byte order as expected at ip_input's 472135158Sandre * "ours"-label. 473133497Sandre */ 474135158Sandre m->m_flags |= M_FASTFWD_OURS; 475133497Sandre if (ro.ro_rt) 476133497Sandre RTFREE(ro.ro_rt); 477154518Sandre return m; 478122702Sandre } 479122702Sandre /* 480122702Sandre * Redo route lookup with new destination address 481122702Sandre */ 482133920Sandre#ifdef IPFIREWALL_FORWARD 483133920Sandre if (fwd_tag) { 484161380Sjulian dest.s_addr = ((struct sockaddr_in *) 485157833Sglebius (fwd_tag + 1))->sin_addr.s_addr; 486133920Sandre m_tag_delete(m, fwd_tag); 487133920Sandre } 488133920Sandre#endif /* IPFIREWALL_FORWARD */ 489122702Sandre RTFREE(ro.ro_rt); 490128872Sandre if ((dst = ip_findroute(&ro, dest, m)) == NULL) 491154518Sandre return NULL; /* icmp unreach already sent */ 492128872Sandre ifp = ro.ro_rt->rt_ifp; 493122702Sandre } 494122702Sandre 495134383Sandrepassout: 496122702Sandre /* 497122702Sandre * Step 6: send off the packet 498122702Sandre */ 499122702Sandre 500122702Sandre /* 501128872Sandre * Check if route is dampned (when ARP is unable to resolve) 502128872Sandre */ 503128872Sandre if ((ro.ro_rt->rt_flags & RTF_REJECT) && 504167682Sbms (ro.ro_rt->rt_rmx.rmx_expire == 0 || 505167682Sbms time_uptime < ro.ro_rt->rt_rmx.rmx_expire)) { 506145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 507128872Sandre goto consumed; 508128872Sandre } 509128872Sandre 510133480Sandre#ifndef ALTQ 511128872Sandre /* 512128872Sandre * Check if there is enough space in the interface queue 513128872Sandre */ 514128872Sandre if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 515128872Sandre ifp->if_snd.ifq_maxlen) { 516128872Sandre ipstat.ips_odropped++; 517128872Sandre /* would send source quench here but that is depreciated */ 518128872Sandre goto drop; 519128872Sandre } 520133480Sandre#endif 521128872Sandre 522128872Sandre /* 523128872Sandre * Check if media link state of interface is not down 524128872Sandre */ 525128872Sandre if (ifp->if_link_state == LINK_STATE_DOWN) { 526145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 527128872Sandre goto consumed; 528128872Sandre } 529128872Sandre 530128872Sandre /* 531148324Skeramida * Check if packet fits MTU or if hardware will fragment for us 532122702Sandre */ 533122702Sandre if (ro.ro_rt->rt_rmx.rmx_mtu) 534122702Sandre mtu = min(ro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu); 535122702Sandre else 536122702Sandre mtu = ifp->if_mtu; 537122702Sandre 538122702Sandre if (ip->ip_len <= mtu || 539122702Sandre (ifp->if_hwassist & CSUM_FRAGMENT && (ip->ip_off & IP_DF) == 0)) { 540122702Sandre /* 541122702Sandre * Restore packet header fields to original values 542122702Sandre */ 543122702Sandre ip->ip_len = htons(ip->ip_len); 544122702Sandre ip->ip_off = htons(ip->ip_off); 545122702Sandre /* 546122702Sandre * Send off the packet via outgoing interface 547122702Sandre */ 548122702Sandre error = (*ifp->if_output)(ifp, m, 549122702Sandre (struct sockaddr *)dst, ro.ro_rt); 550122702Sandre } else { 551122702Sandre /* 552128872Sandre * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery 553122702Sandre */ 554122702Sandre if (ip->ip_off & IP_DF) { 555128872Sandre ipstat.ips_cantfrag++; 556122702Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 557145863Sandre 0, mtu); 558128872Sandre goto consumed; 559122702Sandre } else { 560122702Sandre /* 561148324Skeramida * We have to fragment the packet 562122702Sandre */ 563122702Sandre m->m_pkthdr.csum_flags |= CSUM_IP; 564128872Sandre /* 565128872Sandre * ip_fragment expects ip_len and ip_off in host byte 566128872Sandre * order but returns all packets in network byte order 567128872Sandre */ 568122702Sandre if (ip_fragment(ip, &m, mtu, ifp->if_hwassist, 569122702Sandre (~ifp->if_hwassist & CSUM_DELAY_IP))) { 570122702Sandre goto drop; 571122702Sandre } 572122702Sandre KASSERT(m != NULL, ("null mbuf and no error")); 573122702Sandre /* 574122702Sandre * Send off the fragments via outgoing interface 575122702Sandre */ 576122702Sandre error = 0; 577122702Sandre do { 578122702Sandre m0 = m->m_nextpkt; 579122702Sandre m->m_nextpkt = NULL; 580122702Sandre 581122702Sandre error = (*ifp->if_output)(ifp, m, 582122702Sandre (struct sockaddr *)dst, ro.ro_rt); 583122702Sandre if (error) 584122702Sandre break; 585122702Sandre } while ((m = m0) != NULL); 586122702Sandre if (error) { 587122702Sandre /* Reclaim remaining fragments */ 588144301Sglebius for (m = m0; m; m = m0) { 589122702Sandre m0 = m->m_nextpkt; 590122702Sandre m_freem(m); 591122702Sandre } 592122702Sandre } else 593122702Sandre ipstat.ips_fragmented++; 594122702Sandre } 595122702Sandre } 596122702Sandre 597122702Sandre if (error != 0) 598122702Sandre ipstat.ips_odropped++; 599122702Sandre else { 600122702Sandre ro.ro_rt->rt_rmx.rmx_pksent++; 601122702Sandre ipstat.ips_forward++; 602122702Sandre ipstat.ips_fastforward++; 603122702Sandre } 604128872Sandreconsumed: 605122702Sandre RTFREE(ro.ro_rt); 606154518Sandre return NULL; 607122702Sandredrop: 608122702Sandre if (m) 609122702Sandre m_freem(m); 610128872Sandre if (ro.ro_rt) 611128872Sandre RTFREE(ro.ro_rt); 612154518Sandre return NULL; 613122702Sandre} 614