ip_fastfwd.c revision 271006
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 271006 2014-09-03 08:30:18Z glebius $"); 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 111215701Sdimstatic VNET_DEFINE(int, ipfastforward_active); 112195727Srwatson#define V_ipfastforward_active VNET(ipfastforward_active) 113122702Sandre 114195699SrwatsonSYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, fastforwarding, CTLFLAG_RW, 115195699Srwatson &VNET_NAME(ipfastforward_active), 0, "Enable fast IP forwarding"); 116195699Srwatson 117128872Sandrestatic struct sockaddr_in * 118133497Sandreip_findroute(struct route *ro, struct in_addr dest, struct mbuf *m) 119128872Sandre{ 120128872Sandre struct sockaddr_in *dst; 121128872Sandre struct rtentry *rt; 122128872Sandre 123128872Sandre /* 124128872Sandre * Find route to destination. 125128872Sandre */ 126128872Sandre bzero(ro, sizeof(*ro)); 127128872Sandre dst = (struct sockaddr_in *)&ro->ro_dst; 128128872Sandre dst->sin_family = AF_INET; 129128872Sandre dst->sin_len = sizeof(*dst); 130133497Sandre dst->sin_addr.s_addr = dest.s_addr; 131186119Sqingli in_rtalloc_ign(ro, 0, M_GETFIB(m)); 132128872Sandre 133128872Sandre /* 134128872Sandre * Route there and interface still up? 135128872Sandre */ 136128872Sandre rt = ro->ro_rt; 137128872Sandre if (rt && (rt->rt_flags & RTF_UP) && 138128872Sandre (rt->rt_ifp->if_flags & IFF_UP) && 139148887Srwatson (rt->rt_ifp->if_drv_flags & IFF_DRV_RUNNING)) { 140128872Sandre if (rt->rt_flags & RTF_GATEWAY) 141128872Sandre dst = (struct sockaddr_in *)rt->rt_gateway; 142128872Sandre } else { 143190951Srwatson IPSTAT_INC(ips_noroute); 144190951Srwatson IPSTAT_INC(ips_cantforward); 145128872Sandre if (rt) 146128872Sandre RTFREE(rt); 147145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 148128872Sandre return NULL; 149128872Sandre } 150128872Sandre return dst; 151128872Sandre} 152128872Sandre 153122702Sandre/* 154122702Sandre * Try to forward a packet based on the destination address. 155122702Sandre * This is a fast path optimized for the plain forwarding case. 156196881Spjd * If the packet is handled (and consumed) here then we return NULL; 157196881Spjd * otherwise mbuf is returned and the packet should be delivered 158122702Sandre * to ip_input for full processing. 159122702Sandre */ 160154518Sandrestruct mbuf * 161122702Sandreip_fastforward(struct mbuf *m) 162122702Sandre{ 163122702Sandre struct ip *ip; 164122702Sandre struct mbuf *m0 = NULL; 165122702Sandre struct route ro; 166122702Sandre struct sockaddr_in *dst = NULL; 167128872Sandre struct ifnet *ifp; 168133497Sandre struct in_addr odest, dest; 169241245Sglebius uint16_t sum, ip_len, ip_off; 170122702Sandre int error = 0; 171133920Sandre int hlen, mtu; 172242079Sae struct m_tag *fwd_tag = NULL; 173122702Sandre 174122702Sandre /* 175122702Sandre * Are we active and forwarding packets? 176122702Sandre */ 177181803Sbz if (!V_ipfastforward_active || !V_ipforwarding) 178154518Sandre return m; 179122702Sandre 180122702Sandre M_ASSERTVALID(m); 181122702Sandre M_ASSERTPKTHDR(m); 182122702Sandre 183191148Skmacy bzero(&ro, sizeof(ro)); 184128872Sandre 185122702Sandre /* 186122702Sandre * Step 1: check for packet drop conditions (and sanity checks) 187122702Sandre */ 188122702Sandre 189122702Sandre /* 190122702Sandre * Is entire packet big enough? 191122702Sandre */ 192122702Sandre if (m->m_pkthdr.len < sizeof(struct ip)) { 193190951Srwatson IPSTAT_INC(ips_tooshort); 194122702Sandre goto drop; 195122702Sandre } 196122702Sandre 197122702Sandre /* 198122702Sandre * Is first mbuf large enough for ip header and is header present? 199122702Sandre */ 200122702Sandre if (m->m_len < sizeof (struct ip) && 201137302Sandre (m = m_pullup(m, sizeof (struct ip))) == NULL) { 202190951Srwatson IPSTAT_INC(ips_toosmall); 203154518Sandre return NULL; /* mbuf already free'd */ 204122702Sandre } 205122702Sandre 206122702Sandre ip = mtod(m, struct ip *); 207122702Sandre 208122702Sandre /* 209122702Sandre * Is it IPv4? 210122702Sandre */ 211122702Sandre if (ip->ip_v != IPVERSION) { 212190951Srwatson IPSTAT_INC(ips_badvers); 213122702Sandre goto drop; 214122702Sandre } 215122702Sandre 216122702Sandre /* 217122702Sandre * Is IP header length correct and is it in first mbuf? 218122702Sandre */ 219122702Sandre hlen = ip->ip_hl << 2; 220122702Sandre if (hlen < sizeof(struct ip)) { /* minimum header length */ 221216192Sbz IPSTAT_INC(ips_badhlen); 222122702Sandre goto drop; 223122702Sandre } 224122702Sandre if (hlen > m->m_len) { 225154518Sandre if ((m = m_pullup(m, hlen)) == NULL) { 226190951Srwatson IPSTAT_INC(ips_badhlen); 227154518Sandre return NULL; /* mbuf already free'd */ 228122702Sandre } 229122702Sandre ip = mtod(m, struct ip *); 230122702Sandre } 231122702Sandre 232122702Sandre /* 233122702Sandre * Checksum correct? 234122702Sandre */ 235122702Sandre if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) 236122702Sandre sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 237122702Sandre else { 238122702Sandre if (hlen == sizeof(struct ip)) 239122702Sandre sum = in_cksum_hdr(ip); 240122702Sandre else 241122702Sandre sum = in_cksum(m, hlen); 242122702Sandre } 243122702Sandre if (sum) { 244190951Srwatson IPSTAT_INC(ips_badsum); 245122702Sandre goto drop; 246122702Sandre } 247136690Sandre 248136690Sandre /* 249148324Skeramida * Remember that we have checked the IP header and found it valid. 250136690Sandre */ 251122702Sandre m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID); 252122702Sandre 253128872Sandre ip_len = ntohs(ip->ip_len); 254122702Sandre 255122702Sandre /* 256122702Sandre * Is IP length longer than packet we have got? 257122702Sandre */ 258128872Sandre if (m->m_pkthdr.len < ip_len) { 259190951Srwatson IPSTAT_INC(ips_tooshort); 260122702Sandre goto drop; 261122702Sandre } 262122702Sandre 263122702Sandre /* 264122702Sandre * Is packet longer than IP header tells us? If yes, truncate packet. 265122702Sandre */ 266128872Sandre if (m->m_pkthdr.len > ip_len) { 267122702Sandre if (m->m_len == m->m_pkthdr.len) { 268128872Sandre m->m_len = ip_len; 269128872Sandre m->m_pkthdr.len = ip_len; 270122702Sandre } else 271128872Sandre m_adj(m, ip_len - m->m_pkthdr.len); 272122702Sandre } 273122702Sandre 274122702Sandre /* 275122702Sandre * Is packet from or to 127/8? 276122702Sandre */ 277122702Sandre if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 278122702Sandre (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 279190951Srwatson IPSTAT_INC(ips_badaddr); 280122702Sandre goto drop; 281122702Sandre } 282122702Sandre 283133480Sandre#ifdef ALTQ 284122702Sandre /* 285133480Sandre * Is packet dropped by traffic conditioner? 286133480Sandre */ 287133480Sandre if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 288154518Sandre goto drop; 289133480Sandre#endif 290133480Sandre 291133480Sandre /* 292122702Sandre * Step 2: fallback conditions to normal ip_input path processing 293122702Sandre */ 294122702Sandre 295122702Sandre /* 296122702Sandre * Only IP packets without options 297122702Sandre */ 298129017Sandre if (ip->ip_hl != (sizeof(struct ip) >> 2)) { 299129017Sandre if (ip_doopts == 1) 300154518Sandre return m; 301129017Sandre else if (ip_doopts == 2) { 302129017Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB, 303145863Sandre 0, 0); 304154518Sandre return NULL; /* mbuf already free'd */ 305129017Sandre } 306129017Sandre /* else ignore IP options and continue */ 307129017Sandre } 308122702Sandre 309122702Sandre /* 310122702Sandre * Only unicast IP, not from loopback, no L2 or IP broadcast, 311122702Sandre * no multicast, no INADDR_ANY 312122702Sandre * 313122702Sandre * XXX: Probably some of these checks could be direct drop 314122702Sandre * conditions. However it is not clear whether there are some 315122702Sandre * hacks or obscure behaviours which make it neccessary to 316122702Sandre * let ip_input handle it. We play safe here and let ip_input 317122702Sandre * deal with it until it is proven that we can directly drop it. 318122702Sandre */ 319149369Sandre if ((m->m_flags & (M_BCAST|M_MCAST)) || 320149369Sandre (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) || 321122702Sandre ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST || 322122702Sandre ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST || 323122702Sandre IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 324122702Sandre IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 325166452Sbms IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) || 326166452Sbms IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) || 327149369Sandre ip->ip_src.s_addr == INADDR_ANY || 328122702Sandre ip->ip_dst.s_addr == INADDR_ANY ) 329154518Sandre return m; 330122702Sandre 331122702Sandre /* 332122702Sandre * Is it for a local address on this host? 333122702Sandre */ 334133497Sandre if (in_localip(ip->ip_dst)) 335154518Sandre return m; 336122702Sandre 337190951Srwatson IPSTAT_INC(ips_total); 338122702Sandre 339122702Sandre /* 340122702Sandre * Step 3: incoming packet firewall processing 341122702Sandre */ 342122702Sandre 343133497Sandre odest.s_addr = dest.s_addr = ip->ip_dst.s_addr; 344134383Sandre 345122702Sandre /* 346122702Sandre * Run through list of ipfilter hooks for input packets 347122702Sandre */ 348197952Sjulian if (!PFIL_HOOKED(&V_inet_pfil_hook)) 349134383Sandre goto passin; 350134383Sandre 351197952Sjulian if (pfil_run_hooks( 352197952Sjulian &V_inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL) || 353122702Sandre m == NULL) 354154518Sandre goto drop; 355122702Sandre 356122702Sandre M_ASSERTVALID(m); 357122702Sandre M_ASSERTPKTHDR(m); 358122702Sandre 359122702Sandre ip = mtod(m, struct ip *); /* m may have changed by pfil hook */ 360133497Sandre dest.s_addr = ip->ip_dst.s_addr; 361122702Sandre 362122702Sandre /* 363122702Sandre * Destination address changed? 364122702Sandre */ 365133497Sandre if (odest.s_addr != dest.s_addr) { 366122702Sandre /* 367122702Sandre * Is it now for a local address on this host? 368122702Sandre */ 369133497Sandre if (in_localip(dest)) 370133497Sandre goto forwardlocal; 371122702Sandre /* 372122702Sandre * Go on with new destination address 373122702Sandre */ 374122702Sandre } 375242079Sae 376133920Sandre if (m->m_flags & M_FASTFWD_OURS) { 377133920Sandre /* 378133920Sandre * ipfw changed it for a local address on this host. 379133920Sandre */ 380133920Sandre goto forwardlocal; 381133920Sandre } 382122702Sandre 383134383Sandrepassin: 384122702Sandre /* 385122702Sandre * Step 4: decrement TTL and look up route 386122702Sandre */ 387122702Sandre 388122702Sandre /* 389122702Sandre * Check TTL 390122702Sandre */ 391122702Sandre#ifdef IPSTEALTH 392181803Sbz if (!V_ipstealth) { 393122702Sandre#endif 394122702Sandre if (ip->ip_ttl <= IPTTLDEC) { 395145863Sandre icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0); 396154518Sandre return NULL; /* mbuf already free'd */ 397122702Sandre } 398122702Sandre 399122702Sandre /* 400136690Sandre * Decrement the TTL and incrementally change the IP header checksum. 401136690Sandre * Don't bother doing this with hw checksum offloading, it's faster 402136690Sandre * doing it right here. 403122702Sandre */ 404122702Sandre ip->ip_ttl -= IPTTLDEC; 405122702Sandre if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8)) 406122702Sandre ip->ip_sum -= ~htons(IPTTLDEC << 8); 407122702Sandre else 408122702Sandre ip->ip_sum += htons(IPTTLDEC << 8); 409122702Sandre#ifdef IPSTEALTH 410122702Sandre } 411122702Sandre#endif 412122702Sandre 413122702Sandre /* 414122702Sandre * Find route to destination. 415122702Sandre */ 416128872Sandre if ((dst = ip_findroute(&ro, dest, m)) == NULL) 417154518Sandre return NULL; /* icmp unreach already sent */ 418128872Sandre ifp = ro.ro_rt->rt_ifp; 419122702Sandre 420122702Sandre /* 421166507Sbms * Immediately drop blackholed traffic, and directed broadcasts 422166507Sbms * for either the all-ones or all-zero subnet addresses on 423166507Sbms * locally attached networks. 424137179Sbms */ 425166507Sbms if ((ro.ro_rt->rt_flags & (RTF_BLACKHOLE|RTF_BROADCAST)) != 0) 426137179Sbms goto drop; 427137179Sbms 428137179Sbms /* 429122702Sandre * Step 5: outgoing firewall packet processing 430122702Sandre */ 431122702Sandre 432122702Sandre /* 433122702Sandre * Run through list of hooks for output packets. 434122702Sandre */ 435197952Sjulian if (!PFIL_HOOKED(&V_inet_pfil_hook)) 436134383Sandre goto passout; 437134383Sandre 438197952Sjulian if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_OUT, NULL) || m == NULL) { 439154518Sandre goto drop; 440122702Sandre } 441122702Sandre 442122702Sandre M_ASSERTVALID(m); 443122702Sandre M_ASSERTPKTHDR(m); 444122702Sandre 445122702Sandre ip = mtod(m, struct ip *); 446133497Sandre dest.s_addr = ip->ip_dst.s_addr; 447122702Sandre 448122702Sandre /* 449122702Sandre * Destination address changed? 450122702Sandre */ 451242463Sae if (m->m_flags & M_IP_NEXTHOP) 452242079Sae fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 453133920Sandre if (odest.s_addr != dest.s_addr || fwd_tag != NULL) { 454122702Sandre /* 455122702Sandre * Is it now for a local address on this host? 456122702Sandre */ 457136690Sandre if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) { 458122702Sandreforwardlocal: 459133497Sandre /* 460135158Sandre * Return packet for processing by ip_input(). 461133497Sandre */ 462135158Sandre m->m_flags |= M_FASTFWD_OURS; 463133497Sandre if (ro.ro_rt) 464133497Sandre RTFREE(ro.ro_rt); 465154518Sandre return m; 466122702Sandre } 467122702Sandre /* 468122702Sandre * Redo route lookup with new destination address 469122702Sandre */ 470133920Sandre if (fwd_tag) { 471161380Sjulian dest.s_addr = ((struct sockaddr_in *) 472157833Sglebius (fwd_tag + 1))->sin_addr.s_addr; 473133920Sandre m_tag_delete(m, fwd_tag); 474242463Sae m->m_flags &= ~M_IP_NEXTHOP; 475133920Sandre } 476122702Sandre RTFREE(ro.ro_rt); 477128872Sandre if ((dst = ip_findroute(&ro, dest, m)) == NULL) 478154518Sandre return NULL; /* icmp unreach already sent */ 479128872Sandre ifp = ro.ro_rt->rt_ifp; 480122702Sandre } 481122702Sandre 482134383Sandrepassout: 483122702Sandre /* 484122702Sandre * Step 6: send off the packet 485122702Sandre */ 486241245Sglebius ip_len = ntohs(ip->ip_len); 487241245Sglebius ip_off = ntohs(ip->ip_off); 488122702Sandre 489122702Sandre /* 490128872Sandre * Check if route is dampned (when ARP is unable to resolve) 491128872Sandre */ 492128872Sandre if ((ro.ro_rt->rt_flags & RTF_REJECT) && 493262763Sglebius (ro.ro_rt->rt_expire == 0 || time_uptime < ro.ro_rt->rt_expire)) { 494145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 495128872Sandre goto consumed; 496128872Sandre } 497128872Sandre 498133480Sandre#ifndef ALTQ 499128872Sandre /* 500128872Sandre * Check if there is enough space in the interface queue 501128872Sandre */ 502241245Sglebius if ((ifp->if_snd.ifq_len + ip_len / ifp->if_mtu + 1) >= 503128872Sandre ifp->if_snd.ifq_maxlen) { 504190951Srwatson IPSTAT_INC(ips_odropped); 505128872Sandre /* would send source quench here but that is depreciated */ 506128872Sandre goto drop; 507128872Sandre } 508133480Sandre#endif 509128872Sandre 510128872Sandre /* 511128872Sandre * Check if media link state of interface is not down 512128872Sandre */ 513128872Sandre if (ifp->if_link_state == LINK_STATE_DOWN) { 514145863Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 515128872Sandre goto consumed; 516128872Sandre } 517128872Sandre 518128872Sandre /* 519148324Skeramida * Check if packet fits MTU or if hardware will fragment for us 520122702Sandre */ 521262763Sglebius if (ro.ro_rt->rt_mtu) 522262763Sglebius mtu = min(ro.ro_rt->rt_mtu, ifp->if_mtu); 523122702Sandre else 524122702Sandre mtu = ifp->if_mtu; 525122702Sandre 526271006Sglebius if (ip_len <= mtu) { 527122702Sandre /* 528254523Sandre * Avoid confusing lower layers. 529254523Sandre */ 530254523Sandre m_clrprotoflags(m); 531254523Sandre /* 532122702Sandre * Send off the packet via outgoing interface 533122702Sandre */ 534254889Smarkj IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 535122702Sandre error = (*ifp->if_output)(ifp, m, 536191148Skmacy (struct sockaddr *)dst, &ro); 537122702Sandre } else { 538122702Sandre /* 539128872Sandre * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery 540122702Sandre */ 541241245Sglebius if (ip_off & IP_DF) { 542190951Srwatson IPSTAT_INC(ips_cantfrag); 543122702Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 544145863Sandre 0, mtu); 545128872Sandre goto consumed; 546122702Sandre } else { 547122702Sandre /* 548148324Skeramida * We have to fragment the packet 549122702Sandre */ 550122702Sandre m->m_pkthdr.csum_flags |= CSUM_IP; 551242161Sglebius if (ip_fragment(ip, &m, mtu, ifp->if_hwassist)) 552122702Sandre goto drop; 553122702Sandre KASSERT(m != NULL, ("null mbuf and no error")); 554122702Sandre /* 555122702Sandre * Send off the fragments via outgoing interface 556122702Sandre */ 557122702Sandre error = 0; 558122702Sandre do { 559122702Sandre m0 = m->m_nextpkt; 560122702Sandre m->m_nextpkt = NULL; 561254523Sandre /* 562254523Sandre * Avoid confusing lower layers. 563254523Sandre */ 564254523Sandre m_clrprotoflags(m); 565122702Sandre 566254889Smarkj IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 567122702Sandre error = (*ifp->if_output)(ifp, m, 568191148Skmacy (struct sockaddr *)dst, &ro); 569122702Sandre if (error) 570122702Sandre break; 571122702Sandre } while ((m = m0) != NULL); 572122702Sandre if (error) { 573122702Sandre /* Reclaim remaining fragments */ 574144301Sglebius for (m = m0; m; m = m0) { 575122702Sandre m0 = m->m_nextpkt; 576122702Sandre m_freem(m); 577122702Sandre } 578122702Sandre } else 579190951Srwatson IPSTAT_INC(ips_fragmented); 580122702Sandre } 581122702Sandre } 582122702Sandre 583122702Sandre if (error != 0) 584190951Srwatson IPSTAT_INC(ips_odropped); 585122702Sandre else { 586262763Sglebius counter_u64_add(ro.ro_rt->rt_pksent, 1); 587190951Srwatson IPSTAT_INC(ips_forward); 588190951Srwatson IPSTAT_INC(ips_fastforward); 589122702Sandre } 590128872Sandreconsumed: 591122702Sandre RTFREE(ro.ro_rt); 592154518Sandre return NULL; 593122702Sandredrop: 594122702Sandre if (m) 595122702Sandre m_freem(m); 596128872Sandre if (ro.ro_rt) 597128872Sandre RTFREE(ro.ro_rt); 598154518Sandre return NULL; 599122702Sandre} 600