ip_fastfwd.c revision 133497
1122702Sandre/* 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 133497 2004-08-11 12:32:10Z 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 37122702Sandre * 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 * 44122702Sandre * We handle basic errors, ip header errors, checksum errors, 45122702Sandre * destination unreachable, fragmentation and fragmentation needed and 46122702Sandre * 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 * 61122702Sandre * 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 66122702Sandre * ICMP redirects to connected hosts if the router to dest was not the default 67122702Sandre * gateway. In one case it was filling the routing table of a host with close 68122702Sandre * 300'000 cloned redirect entries until it ran out of kernel memory. However 69122702Sandre * the networking code proved very robust and it didn't crash or went ill 70122702Sandre * otherwise. 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_ipdn.h" 80122702Sandre#include "opt_ipdivert.h" 81122702Sandre#include "opt_ipfilter.h" 82122702Sandre#include "opt_ipstealth.h" 83122702Sandre#include "opt_pfil_hooks.h" 84122702Sandre 85122702Sandre#include <sys/param.h> 86122702Sandre#include <sys/systm.h> 87122702Sandre#include <sys/kernel.h> 88122702Sandre#include <sys/malloc.h> 89122702Sandre#include <sys/mbuf.h> 90122702Sandre#include <sys/protosw.h> 91122702Sandre#include <sys/socket.h> 92122702Sandre#include <sys/sysctl.h> 93122702Sandre 94122702Sandre#include <net/pfil.h> 95122702Sandre#include <net/if.h> 96122702Sandre#include <net/if_types.h> 97122702Sandre#include <net/if_var.h> 98122702Sandre#include <net/if_dl.h> 99122702Sandre#include <net/route.h> 100122702Sandre 101122702Sandre#include <netinet/in.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> 107122702Sandre 108122702Sandre#include <machine/in_cksum.h> 109122702Sandre 110122702Sandre#include <netinet/ip_fw.h> 111126239Smlaier#include <netinet/ip_divert.h> 112122702Sandre#include <netinet/ip_dummynet.h> 113122702Sandre 114122702Sandrestatic int ipfastforward_active = 0; 115122702SandreSYSCTL_INT(_net_inet_ip, OID_AUTO, fastforwarding, CTLFLAG_RW, 116122702Sandre &ipfastforward_active, 0, "Enable fast IP forwarding"); 117122702Sandre 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; 132128872Sandre rtalloc_ign(ro, RTF_CLONING); 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) && 140128872Sandre (rt->rt_ifp->if_flags & IFF_RUNNING)) { 141128872Sandre if (rt->rt_flags & RTF_GATEWAY) 142128872Sandre dst = (struct sockaddr_in *)rt->rt_gateway; 143128872Sandre } else { 144128872Sandre ipstat.ips_noroute++; 145128872Sandre ipstat.ips_cantforward++; 146128872Sandre if (rt) 147128872Sandre RTFREE(rt); 148128872Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, NULL); 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. 157122702Sandre * If the packet is handled (and consumed) here then we return 1; 158122702Sandre * otherwise 0 is returned and the packet should be delivered 159122702Sandre * to ip_input for full processing. 160122702Sandre */ 161122702Sandreint 162122702Sandreip_fastforward(struct mbuf *m) 163122702Sandre{ 164122702Sandre struct ip *ip; 165122702Sandre struct mbuf *m0 = NULL; 166122702Sandre#ifdef IPDIVERT 167122702Sandre struct ip *tip; 168126239Smlaier struct mbuf *clone = NULL; 169122702Sandre#endif 170122702Sandre struct route ro; 171122702Sandre struct sockaddr_in *dst = NULL; 172122702Sandre struct in_ifaddr *ia = NULL; 173122702Sandre struct ifaddr *ifa = NULL; 174128872Sandre struct ifnet *ifp; 175122702Sandre struct ip_fw_args args; 176133497Sandre struct in_addr odest, dest; 177128872Sandre u_short sum, ip_len; 178122702Sandre int error = 0; 179122702Sandre int hlen, ipfw, mtu; 180122702Sandre 181122702Sandre /* 182122702Sandre * Are we active and forwarding packets? 183122702Sandre */ 184122702Sandre if (!ipfastforward_active || !ipforwarding) 185122702Sandre return 0; 186122702Sandre 187122702Sandre M_ASSERTVALID(m); 188122702Sandre M_ASSERTPKTHDR(m); 189122702Sandre 190128872Sandre ro.ro_rt = NULL; 191128872Sandre 192122702Sandre /* 193122702Sandre * Step 1: check for packet drop conditions (and sanity checks) 194122702Sandre */ 195122702Sandre 196122702Sandre /* 197122702Sandre * Is entire packet big enough? 198122702Sandre */ 199122702Sandre if (m->m_pkthdr.len < sizeof(struct ip)) { 200122702Sandre ipstat.ips_tooshort++; 201122702Sandre goto drop; 202122702Sandre } 203122702Sandre 204122702Sandre /* 205122702Sandre * Is first mbuf large enough for ip header and is header present? 206122702Sandre */ 207122702Sandre if (m->m_len < sizeof (struct ip) && 208122702Sandre (m = m_pullup(m, sizeof (struct ip))) == 0) { 209122702Sandre ipstat.ips_toosmall++; 210122702Sandre goto drop; 211122702Sandre } 212122702Sandre 213122702Sandre ip = mtod(m, struct ip *); 214122702Sandre 215122702Sandre /* 216122702Sandre * Is it IPv4? 217122702Sandre */ 218122702Sandre if (ip->ip_v != IPVERSION) { 219122702Sandre ipstat.ips_badvers++; 220122702Sandre goto drop; 221122702Sandre } 222122702Sandre 223122702Sandre /* 224122702Sandre * Is IP header length correct and is it in first mbuf? 225122702Sandre */ 226122702Sandre hlen = ip->ip_hl << 2; 227122702Sandre if (hlen < sizeof(struct ip)) { /* minimum header length */ 228122702Sandre ipstat.ips_badlen++; 229122702Sandre goto drop; 230122702Sandre } 231122702Sandre if (hlen > m->m_len) { 232122702Sandre if ((m = m_pullup(m, hlen)) == 0) { 233122702Sandre ipstat.ips_badhlen++; 234122702Sandre goto drop; 235122702Sandre } 236122702Sandre ip = mtod(m, struct ip *); 237122702Sandre } 238122702Sandre 239122702Sandre /* 240122702Sandre * Checksum correct? 241122702Sandre */ 242122702Sandre if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) 243122702Sandre sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 244122702Sandre else { 245122702Sandre if (hlen == sizeof(struct ip)) 246122702Sandre sum = in_cksum_hdr(ip); 247122702Sandre else 248122702Sandre sum = in_cksum(m, hlen); 249122702Sandre } 250122702Sandre if (sum) { 251122702Sandre ipstat.ips_badsum++; 252122702Sandre goto drop; 253122702Sandre } 254122702Sandre m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID); 255122702Sandre 256128872Sandre ip_len = ntohs(ip->ip_len); 257122702Sandre 258122702Sandre /* 259122702Sandre * Is IP length longer than packet we have got? 260122702Sandre */ 261128872Sandre if (m->m_pkthdr.len < ip_len) { 262122702Sandre ipstat.ips_tooshort++; 263122702Sandre goto drop; 264122702Sandre } 265122702Sandre 266122702Sandre /* 267122702Sandre * Is packet longer than IP header tells us? If yes, truncate packet. 268122702Sandre */ 269128872Sandre if (m->m_pkthdr.len > ip_len) { 270122702Sandre if (m->m_len == m->m_pkthdr.len) { 271128872Sandre m->m_len = ip_len; 272128872Sandre m->m_pkthdr.len = ip_len; 273122702Sandre } else 274128872Sandre m_adj(m, ip_len - m->m_pkthdr.len); 275122702Sandre } 276122702Sandre 277122702Sandre /* 278122702Sandre * Is packet from or to 127/8? 279122702Sandre */ 280122702Sandre if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 281122702Sandre (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 282122702Sandre ipstat.ips_badaddr++; 283122702Sandre goto drop; 284122702Sandre } 285122702Sandre 286133480Sandre#ifdef ALTQ 287122702Sandre /* 288133480Sandre * Is packet dropped by traffic conditioner? 289133480Sandre */ 290133480Sandre if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 291133480Sandre return 1; 292133480Sandre#endif 293133480Sandre 294133480Sandre /* 295122702Sandre * Step 2: fallback conditions to normal ip_input path processing 296122702Sandre */ 297122702Sandre 298122702Sandre /* 299122702Sandre * Only IP packets without options 300122702Sandre */ 301129017Sandre if (ip->ip_hl != (sizeof(struct ip) >> 2)) { 302129017Sandre if (ip_doopts == 1) 303129017Sandre return 0; 304129017Sandre else if (ip_doopts == 2) { 305129017Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB, 306129017Sandre 0, NULL); 307129017Sandre return 1; 308129017Sandre } 309129017Sandre /* else ignore IP options and continue */ 310129017Sandre } 311122702Sandre 312122702Sandre /* 313122702Sandre * Only unicast IP, not from loopback, no L2 or IP broadcast, 314122702Sandre * no multicast, no INADDR_ANY 315122702Sandre * 316122702Sandre * XXX: Probably some of these checks could be direct drop 317122702Sandre * conditions. However it is not clear whether there are some 318122702Sandre * hacks or obscure behaviours which make it neccessary to 319122702Sandre * let ip_input handle it. We play safe here and let ip_input 320122702Sandre * deal with it until it is proven that we can directly drop it. 321122702Sandre */ 322122702Sandre if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) || 323122702Sandre ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST || 324122702Sandre ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST || 325122702Sandre IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 326122702Sandre IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 327122702Sandre ip->ip_dst.s_addr == INADDR_ANY ) 328128872Sandre return 0; 329122702Sandre 330122702Sandre /* 331122702Sandre * Is it for a local address on this host? 332122702Sandre */ 333133497Sandre if (in_localip(ip->ip_dst)) 334133497Sandre return 0; 335122702Sandre 336122702Sandre /* 337122702Sandre * Or is it for a local IP broadcast address on this host? 338122702Sandre */ 339133482Sandre if ((m->m_flags & M_BCAST) && 340133482Sandre (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST)) { 341122702Sandre TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 342122702Sandre if (ifa->ifa_addr->sa_family != AF_INET) 343122702Sandre continue; 344122702Sandre ia = ifatoia(ifa); 345122702Sandre if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr) 346128872Sandre return 0; 347122702Sandre if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 348122702Sandre ip->ip_dst.s_addr) 349128872Sandre return 0; 350122702Sandre } 351122702Sandre } 352122702Sandre ipstat.ips_total++; 353122702Sandre 354122702Sandre /* 355122702Sandre * Step 3: incoming packet firewall processing 356122702Sandre */ 357122702Sandre 358128872Sandre /* 359128872Sandre * Convert to host representation 360128872Sandre */ 361128872Sandre ip->ip_len = ntohs(ip->ip_len); 362128872Sandre ip->ip_off = ntohs(ip->ip_off); 363128872Sandre 364133497Sandre odest.s_addr = dest.s_addr = ip->ip_dst.s_addr; 365122702Sandre#ifdef PFIL_HOOKS 366122702Sandre /* 367122702Sandre * Run through list of ipfilter hooks for input packets 368122702Sandre */ 369122702Sandre if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN) || 370122702Sandre m == NULL) 371122702Sandre return 1; 372122702Sandre 373122702Sandre M_ASSERTVALID(m); 374122702Sandre M_ASSERTPKTHDR(m); 375122702Sandre 376122702Sandre ip = mtod(m, struct ip *); /* m may have changed by pfil hook */ 377133497Sandre dest.s_addr = ip->ip_dst.s_addr; 378122702Sandre#endif 379122702Sandre 380122702Sandre /* 381122702Sandre * Run through ipfw for input packets 382122702Sandre */ 383122702Sandre if (fw_enable && IPFW_LOADED) { 384122702Sandre bzero(&args, sizeof(args)); 385122702Sandre args.m = m; 386122702Sandre 387122702Sandre ipfw = ip_fw_chk_ptr(&args); 388122702Sandre m = args.m; 389122702Sandre 390122702Sandre M_ASSERTVALID(m); 391122702Sandre M_ASSERTPKTHDR(m); 392122702Sandre 393122702Sandre /* 394122702Sandre * Packet denied, drop it 395122702Sandre */ 396122702Sandre if ((ipfw & IP_FW_PORT_DENY_FLAG) || m == NULL) 397122702Sandre goto drop; 398122702Sandre /* 399122702Sandre * Send packet to the appropriate pipe 400122702Sandre */ 401122702Sandre if (DUMMYNET_LOADED && (ipfw & IP_FW_PORT_DYNT_FLAG) != 0) { 402122702Sandre ip_dn_io_ptr(m, ipfw & 0xffff, DN_TO_IP_IN, &args); 403122702Sandre return 1; 404122702Sandre } 405122702Sandre#ifdef IPDIVERT 406122702Sandre /* 407122702Sandre * Divert packet 408122702Sandre */ 409122702Sandre if (ipfw != 0 && (ipfw & IP_FW_PORT_DYNT_FLAG) == 0) { 410122702Sandre /* 411122702Sandre * See if this is a fragment 412122702Sandre */ 413126239Smlaier if (ip->ip_off & (IP_MF | IP_OFFMASK)) 414122702Sandre goto droptoours; 415122702Sandre /* 416122702Sandre * Tee packet 417122702Sandre */ 418122702Sandre if ((ipfw & IP_FW_PORT_TEE_FLAG) != 0) 419126239Smlaier clone = divert_clone(m); 420122702Sandre else 421126239Smlaier clone = m; 422126239Smlaier if (clone == NULL) 423122702Sandre goto passin; 424122702Sandre 425122702Sandre /* 426122702Sandre * Delayed checksums are not compatible 427122702Sandre */ 428126239Smlaier if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 429126239Smlaier in_delayed_cksum(m); 430126239Smlaier m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 431122702Sandre } 432122702Sandre /* 433122702Sandre * Restore packet header fields to original values 434122702Sandre */ 435126239Smlaier tip = mtod(m, struct ip *); 436122702Sandre tip->ip_len = htons(tip->ip_len); 437122702Sandre tip->ip_off = htons(tip->ip_off); 438122702Sandre /* 439122702Sandre * Deliver packet to divert input routine 440122702Sandre */ 441126239Smlaier divert_packet(m, 0); 442122702Sandre /* 443122702Sandre * If this was not tee, we are done 444122702Sandre */ 445126239Smlaier m = clone; 446122702Sandre if ((ipfw & IP_FW_PORT_TEE_FLAG) == 0) 447122702Sandre return 1; 448122702Sandre /* Continue if it was tee */ 449122702Sandre goto passin; 450122702Sandre } 451122702Sandre#endif 452122702Sandre if (ipfw == 0 && args.next_hop != NULL) { 453133497Sandre dest.s_addr = args.next_hop->sin_addr.s_addr; 454122702Sandre goto passin; 455122702Sandre } 456122702Sandre /* 457122702Sandre * Let through or not? 458122702Sandre */ 459122702Sandre if (ipfw != 0) 460122702Sandre goto drop; 461122702Sandre } 462122702Sandrepassin: 463122702Sandre ip = mtod(m, struct ip *); /* if m changed during fw processing */ 464122702Sandre 465122702Sandre /* 466122702Sandre * Destination address changed? 467122702Sandre */ 468133497Sandre if (odest.s_addr != dest.s_addr) { 469122702Sandre /* 470122702Sandre * Is it now for a local address on this host? 471122702Sandre */ 472133497Sandre if (in_localip(dest)) 473133497Sandre goto forwardlocal; 474122702Sandre /* 475122702Sandre * Go on with new destination address 476122702Sandre */ 477122702Sandre } 478122702Sandre 479122702Sandre /* 480122702Sandre * Step 4: decrement TTL and look up route 481122702Sandre */ 482122702Sandre 483122702Sandre /* 484122702Sandre * Check TTL 485122702Sandre */ 486122702Sandre#ifdef IPSTEALTH 487122702Sandre if (!ipstealth) { 488122702Sandre#endif 489122702Sandre if (ip->ip_ttl <= IPTTLDEC) { 490123740Speter icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, NULL); 491122702Sandre return 1; 492122702Sandre } 493122702Sandre 494122702Sandre /* 495122702Sandre * Decrement the TTL and incrementally change the checksum. 496122702Sandre * Don't bother doing this with hw checksum offloading. 497122702Sandre */ 498122702Sandre ip->ip_ttl -= IPTTLDEC; 499122702Sandre if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8)) 500122702Sandre ip->ip_sum -= ~htons(IPTTLDEC << 8); 501122702Sandre else 502122702Sandre ip->ip_sum += htons(IPTTLDEC << 8); 503122702Sandre#ifdef IPSTEALTH 504122702Sandre } 505122702Sandre#endif 506122702Sandre 507122702Sandre /* 508122702Sandre * Find route to destination. 509122702Sandre */ 510128872Sandre if ((dst = ip_findroute(&ro, dest, m)) == NULL) 511128872Sandre return 1; /* icmp unreach already sent */ 512128872Sandre ifp = ro.ro_rt->rt_ifp; 513122702Sandre 514122702Sandre /* 515122702Sandre * Step 5: outgoing firewall packet processing 516122702Sandre */ 517122702Sandre 518122702Sandre#ifdef PFIL_HOOKS 519122702Sandre /* 520122702Sandre * Run through list of hooks for output packets. 521122702Sandre */ 522122702Sandre if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT) || m == NULL) { 523128872Sandre goto consumed; 524122702Sandre } 525122702Sandre 526122702Sandre M_ASSERTVALID(m); 527122702Sandre M_ASSERTPKTHDR(m); 528122702Sandre 529122702Sandre ip = mtod(m, struct ip *); 530133497Sandre dest.s_addr = ip->ip_dst.s_addr; 531122702Sandre#endif 532122702Sandre if (fw_enable && IPFW_LOADED && !args.next_hop) { 533122759Sandre bzero(&args, sizeof(args)); 534122702Sandre args.m = m; 535122702Sandre args.oif = ifp; 536122702Sandre 537122702Sandre ipfw = ip_fw_chk_ptr(&args); 538122702Sandre m = args.m; 539122702Sandre 540122702Sandre M_ASSERTVALID(m); 541122702Sandre M_ASSERTPKTHDR(m); 542122702Sandre 543128872Sandre if ((ipfw & IP_FW_PORT_DENY_FLAG) || m == NULL) 544122702Sandre goto drop; 545128872Sandre 546122702Sandre if (DUMMYNET_LOADED && (ipfw & IP_FW_PORT_DYNT_FLAG) != 0) { 547122702Sandre /* 548122702Sandre * XXX note: if the ifp or rt entry are deleted 549122702Sandre * while a pkt is in dummynet, we are in trouble! 550122702Sandre */ 551122702Sandre args.ro = &ro; /* dummynet does not save it */ 552122702Sandre args.dst = dst; 553122702Sandre 554122702Sandre ip_dn_io_ptr(m, ipfw & 0xffff, DN_TO_IP_OUT, &args); 555128872Sandre goto consumed; 556122702Sandre } 557122702Sandre#ifdef IPDIVERT 558122702Sandre if (ipfw != 0 && (ipfw & IP_FW_PORT_DYNT_FLAG) == 0) { 559122702Sandre /* 560122702Sandre * See if this is a fragment 561122702Sandre */ 562126239Smlaier if (ip->ip_off & (IP_MF | IP_OFFMASK)) 563122702Sandre goto droptoours; 564122702Sandre /* 565122702Sandre * Tee packet 566122702Sandre */ 567122702Sandre if ((ipfw & IP_FW_PORT_TEE_FLAG) != 0) 568126239Smlaier clone = divert_clone(m); 569122702Sandre else 570126239Smlaier clone = m; 571126239Smlaier if (clone == NULL) 572122702Sandre goto passout; 573122702Sandre 574122702Sandre /* 575122702Sandre * Delayed checksums are not compatible with divert 576122702Sandre */ 577126239Smlaier if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 578126239Smlaier in_delayed_cksum(m); 579126239Smlaier m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 580122702Sandre } 581122702Sandre /* 582122702Sandre * Restore packet header fields to original values 583122702Sandre */ 584126239Smlaier tip = mtod(m, struct ip *); 585122702Sandre tip->ip_len = htons(tip->ip_len); 586122702Sandre tip->ip_off = htons(tip->ip_off); 587122702Sandre /* 588122702Sandre * Deliver packet to divert input routine 589122702Sandre */ 590126239Smlaier divert_packet(m, 0); 591122702Sandre /* 592122702Sandre * If this was not tee, we are done 593122702Sandre */ 594126239Smlaier m = clone; 595122702Sandre if ((ipfw & IP_FW_PORT_TEE_FLAG) == 0) { 596128872Sandre goto consumed; 597122702Sandre } 598122702Sandre /* Continue if it was tee */ 599122702Sandre goto passout; 600122702Sandre } 601122702Sandre#endif 602122702Sandre if (ipfw == 0 && args.next_hop != NULL) { 603133497Sandre dest.s_addr = args.next_hop->sin_addr.s_addr; 604122702Sandre goto passout; 605122702Sandre } 606122702Sandre /* 607122702Sandre * Let through or not? 608122702Sandre */ 609122702Sandre if (ipfw != 0) 610122702Sandre goto drop; 611122702Sandre } 612122702Sandrepassout: 613122702Sandre ip = mtod(m, struct ip *); 614122702Sandre 615122702Sandre /* 616122702Sandre * Destination address changed? 617122702Sandre */ 618133497Sandre if (odest.s_addr != dest.s_addr) { 619122702Sandre /* 620122702Sandre * Is it now for a local address on this host? 621122702Sandre */ 622133497Sandre if (in_localip(dest)) { 623122702Sandreforwardlocal: 624133497Sandre if (args.next_hop) { 625133497Sandre struct m_tag *mtag = m_tag_get( 626133497Sandre PACKET_TAG_IPFORWARD, 627133497Sandre sizeof(struct sockaddr_in *), 628133497Sandre M_NOWAIT); 629133497Sandre if (mtag == NULL) { 630133497Sandre goto drop; 631122702Sandre } 632133497Sandre *(struct sockaddr_in **)(mtag+1) = 633133497Sandre args.next_hop; 634133497Sandre m_tag_prepend(m, mtag); 635133497Sandre } 636122702Sandre#ifdef IPDIVERT 637122702Sandredroptoours: /* Used for DIVERT */ 638122702Sandre#endif 639133497Sandre /* for ip_input */ 640133497Sandre m->m_flags |= M_FASTFWD_OURS; 641122702Sandre 642133497Sandre /* ip still points to the real packet */ 643133497Sandre ip->ip_len = htons(ip->ip_len); 644133497Sandre ip->ip_off = htons(ip->ip_off); 645122702Sandre 646133497Sandre /* 647133497Sandre * Return packet for processing by ip_input 648133497Sandre */ 649133497Sandre if (ro.ro_rt) 650133497Sandre RTFREE(ro.ro_rt); 651133497Sandre return 0; 652122702Sandre } 653122702Sandre /* 654122702Sandre * Redo route lookup with new destination address 655122702Sandre */ 656122702Sandre RTFREE(ro.ro_rt); 657128872Sandre if ((dst = ip_findroute(&ro, dest, m)) == NULL) 658128872Sandre return 1; /* icmp unreach already sent */ 659128872Sandre ifp = ro.ro_rt->rt_ifp; 660122702Sandre } 661122702Sandre 662122702Sandre /* 663122702Sandre * Step 6: send off the packet 664122702Sandre */ 665122702Sandre 666122702Sandre /* 667128872Sandre * Check if route is dampned (when ARP is unable to resolve) 668128872Sandre */ 669128872Sandre if ((ro.ro_rt->rt_flags & RTF_REJECT) && 670128872Sandre ro.ro_rt->rt_rmx.rmx_expire >= time_second) { 671128872Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, NULL); 672128872Sandre goto consumed; 673128872Sandre } 674128872Sandre 675133480Sandre#ifndef ALTQ 676128872Sandre /* 677128872Sandre * Check if there is enough space in the interface queue 678128872Sandre */ 679128872Sandre if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 680128872Sandre ifp->if_snd.ifq_maxlen) { 681128872Sandre ipstat.ips_odropped++; 682128872Sandre /* would send source quench here but that is depreciated */ 683128872Sandre goto drop; 684128872Sandre } 685133480Sandre#endif 686128872Sandre 687128872Sandre /* 688128872Sandre * Check if media link state of interface is not down 689128872Sandre */ 690128872Sandre if (ifp->if_link_state == LINK_STATE_DOWN) { 691128872Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, NULL); 692128872Sandre goto consumed; 693128872Sandre } 694128872Sandre 695128872Sandre /* 696122702Sandre * Check if packet fits MTU or if hardware will fragement for us 697122702Sandre */ 698122702Sandre if (ro.ro_rt->rt_rmx.rmx_mtu) 699122702Sandre mtu = min(ro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu); 700122702Sandre else 701122702Sandre mtu = ifp->if_mtu; 702122702Sandre 703122702Sandre if (ip->ip_len <= mtu || 704122702Sandre (ifp->if_hwassist & CSUM_FRAGMENT && (ip->ip_off & IP_DF) == 0)) { 705122702Sandre /* 706122702Sandre * Restore packet header fields to original values 707122702Sandre */ 708122702Sandre ip->ip_len = htons(ip->ip_len); 709122702Sandre ip->ip_off = htons(ip->ip_off); 710122702Sandre /* 711122702Sandre * Send off the packet via outgoing interface 712122702Sandre */ 713122702Sandre error = (*ifp->if_output)(ifp, m, 714122702Sandre (struct sockaddr *)dst, ro.ro_rt); 715122702Sandre } else { 716122702Sandre /* 717128872Sandre * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery 718122702Sandre */ 719122702Sandre if (ip->ip_off & IP_DF) { 720128872Sandre ipstat.ips_cantfrag++; 721122702Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 722123740Speter 0, ifp); 723128872Sandre goto consumed; 724122702Sandre } else { 725122702Sandre /* 726122702Sandre * We have to fragement the packet 727122702Sandre */ 728122702Sandre m->m_pkthdr.csum_flags |= CSUM_IP; 729128872Sandre /* 730128872Sandre * ip_fragment expects ip_len and ip_off in host byte 731128872Sandre * order but returns all packets in network byte order 732128872Sandre */ 733122702Sandre if (ip_fragment(ip, &m, mtu, ifp->if_hwassist, 734122702Sandre (~ifp->if_hwassist & CSUM_DELAY_IP))) { 735122702Sandre goto drop; 736122702Sandre } 737122702Sandre KASSERT(m != NULL, ("null mbuf and no error")); 738122702Sandre /* 739122702Sandre * Send off the fragments via outgoing interface 740122702Sandre */ 741122702Sandre error = 0; 742122702Sandre do { 743122702Sandre m0 = m->m_nextpkt; 744122702Sandre m->m_nextpkt = NULL; 745122702Sandre 746122702Sandre error = (*ifp->if_output)(ifp, m, 747122702Sandre (struct sockaddr *)dst, ro.ro_rt); 748122702Sandre if (error) 749122702Sandre break; 750122702Sandre } while ((m = m0) != NULL); 751122702Sandre if (error) { 752122702Sandre /* Reclaim remaining fragments */ 753122702Sandre for (; m; m = m0) { 754122702Sandre m0 = m->m_nextpkt; 755122702Sandre m->m_nextpkt = NULL; 756122702Sandre m_freem(m); 757122702Sandre } 758122702Sandre } else 759122702Sandre ipstat.ips_fragmented++; 760122702Sandre } 761122702Sandre } 762122702Sandre 763122702Sandre if (error != 0) 764122702Sandre ipstat.ips_odropped++; 765122702Sandre else { 766122702Sandre ro.ro_rt->rt_rmx.rmx_pksent++; 767122702Sandre ipstat.ips_forward++; 768122702Sandre ipstat.ips_fastforward++; 769122702Sandre } 770128872Sandreconsumed: 771122702Sandre RTFREE(ro.ro_rt); 772122702Sandre return 1; 773122702Sandredrop: 774122702Sandre if (m) 775122702Sandre m_freem(m); 776128872Sandre if (ro.ro_rt) 777128872Sandre RTFREE(ro.ro_rt); 778122702Sandre return 1; 779122702Sandre} 780