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$"); 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 112295896Sgnnstatic VNET_DEFINE(int, ipfastforward_active); 113295896Sgnn#define V_ipfastforward_active VNET(ipfastforward_active) 114295896Sgnn 115295896SgnnSYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, fastforwarding, CTLFLAG_RW, 116295896Sgnn &VNET_NAME(ipfastforward_active), 0, "Enable fast IP forwarding"); 117295896Sgnn 118128872Sandrestatic struct sockaddr_in * 119133497Sandreip_findroute(struct route *ro, struct in_addr dest, struct mbuf *m) 120128872Sandre{ 121128872Sandre struct sockaddr_in *dst; 122128872Sandre struct rtentry *rt; 123128872Sandre 124128872Sandre /* 125128872Sandre * Find route to destination. 126128872Sandre */ 127128872Sandre bzero(ro, sizeof(*ro)); 128128872Sandre dst = (struct sockaddr_in *)&ro->ro_dst; 129128872Sandre dst->sin_family = AF_INET; 130128872Sandre dst->sin_len = sizeof(*dst); 131133497Sandre dst->sin_addr.s_addr = dest.s_addr; 132186119Sqingli in_rtalloc_ign(ro, 0, M_GETFIB(m)); 133128872Sandre 134128872Sandre /* 135128872Sandre * Route there and interface still up? 136128872Sandre */ 137128872Sandre rt = ro->ro_rt; 138128872Sandre if (rt && (rt->rt_flags & RTF_UP) && 139128872Sandre (rt->rt_ifp->if_flags & IFF_UP) && 140148887Srwatson (rt->rt_ifp->if_drv_flags & IFF_DRV_RUNNING)) { 141128872Sandre if (rt->rt_flags & RTF_GATEWAY) 142128872Sandre dst = (struct sockaddr_in *)rt->rt_gateway; 143128872Sandre } else { 144190951Srwatson IPSTAT_INC(ips_noroute); 145190951Srwatson IPSTAT_INC(ips_cantforward); 146128872Sandre if (rt) 147128872Sandre RTFREE(rt); 148145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 149128872Sandre return NULL; 150128872Sandre } 151128872Sandre return dst; 152128872Sandre} 153128872Sandre 154122702Sandre/* 155122702Sandre * Try to forward a packet based on the destination address. 156122702Sandre * This is a fast path optimized for the plain forwarding case. 157196881Spjd * If the packet is handled (and consumed) here then we return NULL; 158196881Spjd * otherwise mbuf is returned and the packet should be delivered 159122702Sandre * to ip_input for full processing. 160122702Sandre */ 161154518Sandrestruct mbuf * 162295896Sgnnip_fastforward(struct mbuf *m) 163122702Sandre{ 164122702Sandre struct ip *ip; 165122702Sandre struct mbuf *m0 = NULL; 166122702Sandre struct route ro; 167122702Sandre struct sockaddr_in *dst = NULL; 168128872Sandre struct ifnet *ifp; 169133497Sandre struct in_addr odest, dest; 170295896Sgnn uint16_t sum, ip_len, ip_off; 171122702Sandre int error = 0; 172295896Sgnn int hlen, mtu; 173242079Sae struct m_tag *fwd_tag = NULL; 174122702Sandre 175122702Sandre /* 176122702Sandre * Are we active and forwarding packets? 177122702Sandre */ 178295896Sgnn if (!V_ipfastforward_active || !V_ipforwarding) 179295896Sgnn return m; 180122702Sandre 181122702Sandre M_ASSERTVALID(m); 182122702Sandre M_ASSERTPKTHDR(m); 183122702Sandre 184191148Skmacy bzero(&ro, sizeof(ro)); 185128872Sandre 186295896Sgnn /* 187295896Sgnn * Step 1: check for packet drop conditions (and sanity checks) 188295896Sgnn */ 189122702Sandre 190295896Sgnn /* 191295896Sgnn * Is entire packet big enough? 192295896Sgnn */ 193295896Sgnn if (m->m_pkthdr.len < sizeof(struct ip)) { 194295896Sgnn IPSTAT_INC(ips_tooshort); 195295896Sgnn goto drop; 196295896Sgnn } 197295896Sgnn 198295896Sgnn /* 199295896Sgnn * Is first mbuf large enough for ip header and is header present? 200295896Sgnn */ 201295896Sgnn if (m->m_len < sizeof (struct ip) && 202295896Sgnn (m = m_pullup(m, sizeof (struct ip))) == NULL) { 203295896Sgnn IPSTAT_INC(ips_toosmall); 204295896Sgnn return NULL; /* mbuf already free'd */ 205295896Sgnn } 206295896Sgnn 207295896Sgnn ip = mtod(m, struct ip *); 208295896Sgnn 209295896Sgnn /* 210295896Sgnn * Is it IPv4? 211295896Sgnn */ 212295896Sgnn if (ip->ip_v != IPVERSION) { 213295896Sgnn IPSTAT_INC(ips_badvers); 214295896Sgnn goto drop; 215295896Sgnn } 216295896Sgnn 217295896Sgnn /* 218295896Sgnn * Is IP header length correct and is it in first mbuf? 219295896Sgnn */ 220295896Sgnn hlen = ip->ip_hl << 2; 221295896Sgnn if (hlen < sizeof(struct ip)) { /* minimum header length */ 222295896Sgnn IPSTAT_INC(ips_badhlen); 223295896Sgnn goto drop; 224295896Sgnn } 225295896Sgnn if (hlen > m->m_len) { 226295896Sgnn if ((m = m_pullup(m, hlen)) == NULL) { 227295896Sgnn IPSTAT_INC(ips_badhlen); 228295896Sgnn return NULL; /* mbuf already free'd */ 229295896Sgnn } 230295896Sgnn ip = mtod(m, struct ip *); 231295896Sgnn } 232295896Sgnn 233295896Sgnn /* 234295896Sgnn * Checksum correct? 235295896Sgnn */ 236295896Sgnn if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) 237295896Sgnn sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 238295896Sgnn else { 239295896Sgnn if (hlen == sizeof(struct ip)) 240295896Sgnn sum = in_cksum_hdr(ip); 241295896Sgnn else 242295896Sgnn sum = in_cksum(m, hlen); 243295896Sgnn } 244295896Sgnn if (sum) { 245295896Sgnn IPSTAT_INC(ips_badsum); 246295896Sgnn goto drop; 247295896Sgnn } 248295896Sgnn 249295896Sgnn /* 250295896Sgnn * Remember that we have checked the IP header and found it valid. 251295896Sgnn */ 252295896Sgnn m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID); 253295896Sgnn 254295896Sgnn ip_len = ntohs(ip->ip_len); 255295896Sgnn 256295896Sgnn /* 257295896Sgnn * Is IP length longer than packet we have got? 258295896Sgnn */ 259295896Sgnn if (m->m_pkthdr.len < ip_len) { 260295896Sgnn IPSTAT_INC(ips_tooshort); 261295896Sgnn goto drop; 262295896Sgnn } 263295896Sgnn 264295896Sgnn /* 265295896Sgnn * Is packet longer than IP header tells us? If yes, truncate packet. 266295896Sgnn */ 267295896Sgnn if (m->m_pkthdr.len > ip_len) { 268295896Sgnn if (m->m_len == m->m_pkthdr.len) { 269295896Sgnn m->m_len = ip_len; 270295896Sgnn m->m_pkthdr.len = ip_len; 271295896Sgnn } else 272295896Sgnn m_adj(m, ip_len - m->m_pkthdr.len); 273295896Sgnn } 274295896Sgnn 275295896Sgnn /* 276295896Sgnn * Is packet from or to 127/8? 277295896Sgnn */ 278295896Sgnn if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 279295896Sgnn (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 280295896Sgnn IPSTAT_INC(ips_badaddr); 281295896Sgnn goto drop; 282295896Sgnn } 283295896Sgnn 284133480Sandre#ifdef ALTQ 285122702Sandre /* 286133480Sandre * Is packet dropped by traffic conditioner? 287133480Sandre */ 288133480Sandre if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 289154518Sandre goto drop; 290133480Sandre#endif 291133480Sandre 292133480Sandre /* 293295896Sgnn * Step 2: fallback conditions to normal ip_input path processing 294295896Sgnn */ 295295896Sgnn 296295896Sgnn /* 297295285Sgnn * Only IP packets without options 298122702Sandre */ 299129017Sandre if (ip->ip_hl != (sizeof(struct ip) >> 2)) { 300272868Shrs if (V_ip_doopts == 1) 301154518Sandre return m; 302272868Shrs else if (V_ip_doopts == 2) { 303129017Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB, 304145863Sandre 0, 0); 305154518Sandre return NULL; /* mbuf already free'd */ 306129017Sandre } 307129017Sandre /* else ignore IP options and continue */ 308129017Sandre } 309122702Sandre 310122702Sandre /* 311122702Sandre * Only unicast IP, not from loopback, no L2 or IP broadcast, 312122702Sandre * no multicast, no INADDR_ANY 313122702Sandre * 314122702Sandre * XXX: Probably some of these checks could be direct drop 315122702Sandre * conditions. However it is not clear whether there are some 316122702Sandre * hacks or obscure behaviours which make it neccessary to 317122702Sandre * let ip_input handle it. We play safe here and let ip_input 318122702Sandre * deal with it until it is proven that we can directly drop it. 319122702Sandre */ 320149369Sandre if ((m->m_flags & (M_BCAST|M_MCAST)) || 321149369Sandre (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) || 322122702Sandre ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST || 323122702Sandre ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST || 324122702Sandre IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 325122702Sandre IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 326166452Sbms IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) || 327166452Sbms IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) || 328149369Sandre ip->ip_src.s_addr == INADDR_ANY || 329122702Sandre ip->ip_dst.s_addr == INADDR_ANY ) 330154518Sandre return m; 331122702Sandre 332122702Sandre /* 333122702Sandre * Is it for a local address on this host? 334122702Sandre */ 335133497Sandre if (in_localip(ip->ip_dst)) 336154518Sandre return m; 337122702Sandre 338190951Srwatson IPSTAT_INC(ips_total); 339122702Sandre 340122702Sandre /* 341122702Sandre * Step 3: incoming packet firewall processing 342122702Sandre */ 343122702Sandre 344133497Sandre odest.s_addr = dest.s_addr = ip->ip_dst.s_addr; 345134383Sandre 346122702Sandre /* 347122702Sandre * Run through list of ipfilter hooks for input packets 348122702Sandre */ 349197952Sjulian if (!PFIL_HOOKED(&V_inet_pfil_hook)) 350134383Sandre goto passin; 351134383Sandre 352197952Sjulian if (pfil_run_hooks( 353197952Sjulian &V_inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL) || 354122702Sandre m == NULL) 355154518Sandre goto drop; 356122702Sandre 357122702Sandre M_ASSERTVALID(m); 358122702Sandre M_ASSERTPKTHDR(m); 359122702Sandre 360122702Sandre ip = mtod(m, struct ip *); /* m may have changed by pfil hook */ 361133497Sandre dest.s_addr = ip->ip_dst.s_addr; 362122702Sandre 363122702Sandre /* 364122702Sandre * Destination address changed? 365122702Sandre */ 366133497Sandre if (odest.s_addr != dest.s_addr) { 367122702Sandre /* 368122702Sandre * Is it now for a local address on this host? 369122702Sandre */ 370133497Sandre if (in_localip(dest)) 371133497Sandre goto forwardlocal; 372122702Sandre /* 373122702Sandre * Go on with new destination address 374122702Sandre */ 375122702Sandre } 376242079Sae 377133920Sandre if (m->m_flags & M_FASTFWD_OURS) { 378133920Sandre /* 379133920Sandre * ipfw changed it for a local address on this host. 380133920Sandre */ 381133920Sandre goto forwardlocal; 382133920Sandre } 383122702Sandre 384134383Sandrepassin: 385122702Sandre /* 386122702Sandre * Step 4: decrement TTL and look up route 387122702Sandre */ 388122702Sandre 389122702Sandre /* 390122702Sandre * Check TTL 391122702Sandre */ 392122702Sandre#ifdef IPSTEALTH 393181803Sbz if (!V_ipstealth) { 394122702Sandre#endif 395122702Sandre if (ip->ip_ttl <= IPTTLDEC) { 396145863Sandre icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0); 397154518Sandre return NULL; /* mbuf already free'd */ 398122702Sandre } 399122702Sandre 400122702Sandre /* 401136690Sandre * Decrement the TTL and incrementally change the IP header checksum. 402136690Sandre * Don't bother doing this with hw checksum offloading, it's faster 403136690Sandre * doing it right here. 404122702Sandre */ 405122702Sandre ip->ip_ttl -= IPTTLDEC; 406122702Sandre if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8)) 407122702Sandre ip->ip_sum -= ~htons(IPTTLDEC << 8); 408122702Sandre else 409122702Sandre ip->ip_sum += htons(IPTTLDEC << 8); 410122702Sandre#ifdef IPSTEALTH 411122702Sandre } 412122702Sandre#endif 413122702Sandre 414122702Sandre /* 415122702Sandre * Find route to destination. 416122702Sandre */ 417128872Sandre if ((dst = ip_findroute(&ro, dest, m)) == NULL) 418154518Sandre return NULL; /* icmp unreach already sent */ 419128872Sandre ifp = ro.ro_rt->rt_ifp; 420122702Sandre 421122702Sandre /* 422166507Sbms * Immediately drop blackholed traffic, and directed broadcasts 423166507Sbms * for either the all-ones or all-zero subnet addresses on 424166507Sbms * locally attached networks. 425137179Sbms */ 426166507Sbms if ((ro.ro_rt->rt_flags & (RTF_BLACKHOLE|RTF_BROADCAST)) != 0) 427137179Sbms goto drop; 428137179Sbms 429137179Sbms /* 430122702Sandre * Step 5: outgoing firewall packet processing 431122702Sandre */ 432122702Sandre 433122702Sandre /* 434122702Sandre * Run through list of hooks for output packets. 435122702Sandre */ 436197952Sjulian if (!PFIL_HOOKED(&V_inet_pfil_hook)) 437134383Sandre goto passout; 438134383Sandre 439197952Sjulian if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_OUT, NULL) || m == NULL) { 440154518Sandre goto drop; 441122702Sandre } 442122702Sandre 443122702Sandre M_ASSERTVALID(m); 444122702Sandre M_ASSERTPKTHDR(m); 445122702Sandre 446122702Sandre ip = mtod(m, struct ip *); 447133497Sandre dest.s_addr = ip->ip_dst.s_addr; 448122702Sandre 449122702Sandre /* 450122702Sandre * Destination address changed? 451122702Sandre */ 452242463Sae if (m->m_flags & M_IP_NEXTHOP) 453242079Sae fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 454133920Sandre if (odest.s_addr != dest.s_addr || fwd_tag != NULL) { 455122702Sandre /* 456122702Sandre * Is it now for a local address on this host? 457122702Sandre */ 458136690Sandre if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) { 459122702Sandreforwardlocal: 460133497Sandre /* 461135158Sandre * Return packet for processing by ip_input(). 462133497Sandre */ 463135158Sandre m->m_flags |= M_FASTFWD_OURS; 464133497Sandre if (ro.ro_rt) 465133497Sandre RTFREE(ro.ro_rt); 466154518Sandre return m; 467122702Sandre } 468122702Sandre /* 469122702Sandre * Redo route lookup with new destination address 470122702Sandre */ 471133920Sandre if (fwd_tag) { 472161380Sjulian dest.s_addr = ((struct sockaddr_in *) 473157833Sglebius (fwd_tag + 1))->sin_addr.s_addr; 474133920Sandre m_tag_delete(m, fwd_tag); 475242463Sae m->m_flags &= ~M_IP_NEXTHOP; 476133920Sandre } 477122702Sandre RTFREE(ro.ro_rt); 478128872Sandre if ((dst = ip_findroute(&ro, dest, m)) == NULL) 479154518Sandre return NULL; /* icmp unreach already sent */ 480128872Sandre ifp = ro.ro_rt->rt_ifp; 481122702Sandre } 482122702Sandre 483134383Sandrepassout: 484122702Sandre /* 485122702Sandre * Step 6: send off the packet 486122702Sandre */ 487241245Sglebius ip_len = ntohs(ip->ip_len); 488241245Sglebius ip_off = ntohs(ip->ip_off); 489122702Sandre 490122702Sandre /* 491128872Sandre * Check if route is dampned (when ARP is unable to resolve) 492128872Sandre */ 493128872Sandre if ((ro.ro_rt->rt_flags & RTF_REJECT) && 494263478Sglebius (ro.ro_rt->rt_expire == 0 || time_uptime < ro.ro_rt->rt_expire)) { 495145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 496128872Sandre goto consumed; 497128872Sandre } 498128872Sandre 499133480Sandre#ifndef ALTQ 500128872Sandre /* 501128872Sandre * Check if there is enough space in the interface queue 502128872Sandre */ 503241245Sglebius if ((ifp->if_snd.ifq_len + ip_len / ifp->if_mtu + 1) >= 504128872Sandre ifp->if_snd.ifq_maxlen) { 505190951Srwatson IPSTAT_INC(ips_odropped); 506128872Sandre /* would send source quench here but that is depreciated */ 507128872Sandre goto drop; 508128872Sandre } 509133480Sandre#endif 510128872Sandre 511128872Sandre /* 512128872Sandre * Check if media link state of interface is not down 513128872Sandre */ 514128872Sandre if (ifp->if_link_state == LINK_STATE_DOWN) { 515145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 516128872Sandre goto consumed; 517128872Sandre } 518128872Sandre 519128872Sandre /* 520148324Skeramida * Check if packet fits MTU or if hardware will fragment for us 521122702Sandre */ 522263478Sglebius if (ro.ro_rt->rt_mtu) 523263478Sglebius mtu = min(ro.ro_rt->rt_mtu, ifp->if_mtu); 524122702Sandre else 525122702Sandre mtu = ifp->if_mtu; 526122702Sandre 527241245Sglebius if (ip_len <= mtu || 528241245Sglebius (ifp->if_hwassist & CSUM_FRAGMENT && (ip_off & IP_DF) == 0)) { 529122702Sandre /* 530254523Sandre * Avoid confusing lower layers. 531254523Sandre */ 532254523Sandre m_clrprotoflags(m); 533254523Sandre /* 534122702Sandre * Send off the packet via outgoing interface 535122702Sandre */ 536254889Smarkj IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 537122702Sandre error = (*ifp->if_output)(ifp, m, 538191148Skmacy (struct sockaddr *)dst, &ro); 539122702Sandre } else { 540122702Sandre /* 541128872Sandre * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery 542122702Sandre */ 543241245Sglebius if (ip_off & IP_DF) { 544190951Srwatson IPSTAT_INC(ips_cantfrag); 545122702Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 546145863Sandre 0, mtu); 547128872Sandre goto consumed; 548122702Sandre } else { 549122702Sandre /* 550148324Skeramida * We have to fragment the packet 551122702Sandre */ 552122702Sandre m->m_pkthdr.csum_flags |= CSUM_IP; 553242161Sglebius if (ip_fragment(ip, &m, mtu, ifp->if_hwassist)) 554122702Sandre goto drop; 555122702Sandre KASSERT(m != NULL, ("null mbuf and no error")); 556122702Sandre /* 557122702Sandre * Send off the fragments via outgoing interface 558122702Sandre */ 559122702Sandre error = 0; 560122702Sandre do { 561122702Sandre m0 = m->m_nextpkt; 562122702Sandre m->m_nextpkt = NULL; 563254523Sandre /* 564254523Sandre * Avoid confusing lower layers. 565254523Sandre */ 566254523Sandre m_clrprotoflags(m); 567122702Sandre 568254889Smarkj IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 569122702Sandre error = (*ifp->if_output)(ifp, m, 570191148Skmacy (struct sockaddr *)dst, &ro); 571122702Sandre if (error) 572122702Sandre break; 573122702Sandre } while ((m = m0) != NULL); 574122702Sandre if (error) { 575122702Sandre /* Reclaim remaining fragments */ 576144301Sglebius for (m = m0; m; m = m0) { 577122702Sandre m0 = m->m_nextpkt; 578122702Sandre m_freem(m); 579122702Sandre } 580122702Sandre } else 581190951Srwatson IPSTAT_INC(ips_fragmented); 582122702Sandre } 583122702Sandre } 584122702Sandre 585122702Sandre if (error != 0) 586190951Srwatson IPSTAT_INC(ips_odropped); 587122702Sandre else { 588263478Sglebius counter_u64_add(ro.ro_rt->rt_pksent, 1); 589190951Srwatson IPSTAT_INC(ips_forward); 590190951Srwatson IPSTAT_INC(ips_fastforward); 591122702Sandre } 592128872Sandreconsumed: 593122702Sandre RTFREE(ro.ro_rt); 594154518Sandre return NULL; 595122702Sandredrop: 596122702Sandre if (m) 597122702Sandre m_freem(m); 598128872Sandre if (ro.ro_rt) 599128872Sandre RTFREE(ro.ro_rt); 600154518Sandre return NULL; 601122702Sandre} 602