ip_fastfwd.c revision 133480
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 133480 2004-08-11 10:42:59Z 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 * 119128872Sandreip_findroute(struct route *ro, in_addr_t 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); 131128872Sandre dst->sin_addr.s_addr = dest; 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; 176122702Sandre in_addr_t 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 */ 333122702Sandre LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 334122702Sandre if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 335128872Sandre return 0; 336122702Sandre } 337122702Sandre 338122702Sandre /* 339122702Sandre * Or is it for a local IP broadcast address on this host? 340122702Sandre */ 341122702Sandre if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 342122702Sandre TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 343122702Sandre if (ifa->ifa_addr->sa_family != AF_INET) 344122702Sandre continue; 345122702Sandre ia = ifatoia(ifa); 346122702Sandre if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr) 347128872Sandre return 0; 348122702Sandre if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 349122702Sandre ip->ip_dst.s_addr) 350128872Sandre return 0; 351122702Sandre } 352122702Sandre } 353122702Sandre ipstat.ips_total++; 354122702Sandre 355122702Sandre /* 356122702Sandre * Step 3: incoming packet firewall processing 357122702Sandre */ 358122702Sandre 359128872Sandre /* 360128872Sandre * Convert to host representation 361128872Sandre */ 362128872Sandre ip->ip_len = ntohs(ip->ip_len); 363128872Sandre ip->ip_off = ntohs(ip->ip_off); 364128872Sandre 365122702Sandre odest = dest = ip->ip_dst.s_addr; 366122702Sandre#ifdef PFIL_HOOKS 367122702Sandre /* 368122702Sandre * Run through list of ipfilter hooks for input packets 369122702Sandre */ 370122702Sandre if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN) || 371122702Sandre m == NULL) 372122702Sandre return 1; 373122702Sandre 374122702Sandre M_ASSERTVALID(m); 375122702Sandre M_ASSERTPKTHDR(m); 376122702Sandre 377122702Sandre ip = mtod(m, struct ip *); /* m may have changed by pfil hook */ 378122702Sandre dest = ip->ip_dst.s_addr; 379122702Sandre#endif 380122702Sandre 381122702Sandre /* 382122702Sandre * Run through ipfw for input packets 383122702Sandre */ 384122702Sandre if (fw_enable && IPFW_LOADED) { 385122702Sandre bzero(&args, sizeof(args)); 386122702Sandre args.m = m; 387122702Sandre 388122702Sandre ipfw = ip_fw_chk_ptr(&args); 389122702Sandre m = args.m; 390122702Sandre 391122702Sandre M_ASSERTVALID(m); 392122702Sandre M_ASSERTPKTHDR(m); 393122702Sandre 394122702Sandre /* 395122702Sandre * Packet denied, drop it 396122702Sandre */ 397122702Sandre if ((ipfw & IP_FW_PORT_DENY_FLAG) || m == NULL) 398122702Sandre goto drop; 399122702Sandre /* 400122702Sandre * Send packet to the appropriate pipe 401122702Sandre */ 402122702Sandre if (DUMMYNET_LOADED && (ipfw & IP_FW_PORT_DYNT_FLAG) != 0) { 403122702Sandre ip_dn_io_ptr(m, ipfw & 0xffff, DN_TO_IP_IN, &args); 404122702Sandre return 1; 405122702Sandre } 406122702Sandre#ifdef IPDIVERT 407122702Sandre /* 408122702Sandre * Divert packet 409122702Sandre */ 410122702Sandre if (ipfw != 0 && (ipfw & IP_FW_PORT_DYNT_FLAG) == 0) { 411122702Sandre /* 412122702Sandre * See if this is a fragment 413122702Sandre */ 414126239Smlaier if (ip->ip_off & (IP_MF | IP_OFFMASK)) 415122702Sandre goto droptoours; 416122702Sandre /* 417122702Sandre * Tee packet 418122702Sandre */ 419122702Sandre if ((ipfw & IP_FW_PORT_TEE_FLAG) != 0) 420126239Smlaier clone = divert_clone(m); 421122702Sandre else 422126239Smlaier clone = m; 423126239Smlaier if (clone == NULL) 424122702Sandre goto passin; 425122702Sandre 426122702Sandre /* 427122702Sandre * Delayed checksums are not compatible 428122702Sandre */ 429126239Smlaier if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 430126239Smlaier in_delayed_cksum(m); 431126239Smlaier m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 432122702Sandre } 433122702Sandre /* 434122702Sandre * Restore packet header fields to original values 435122702Sandre */ 436126239Smlaier tip = mtod(m, struct ip *); 437122702Sandre tip->ip_len = htons(tip->ip_len); 438122702Sandre tip->ip_off = htons(tip->ip_off); 439122702Sandre /* 440122702Sandre * Deliver packet to divert input routine 441122702Sandre */ 442126239Smlaier divert_packet(m, 0); 443122702Sandre /* 444122702Sandre * If this was not tee, we are done 445122702Sandre */ 446126239Smlaier m = clone; 447122702Sandre if ((ipfw & IP_FW_PORT_TEE_FLAG) == 0) 448122702Sandre return 1; 449122702Sandre /* Continue if it was tee */ 450122702Sandre goto passin; 451122702Sandre } 452122702Sandre#endif 453122702Sandre if (ipfw == 0 && args.next_hop != NULL) { 454122702Sandre dest = args.next_hop->sin_addr.s_addr; 455122702Sandre goto passin; 456122702Sandre } 457122702Sandre /* 458122702Sandre * Let through or not? 459122702Sandre */ 460122702Sandre if (ipfw != 0) 461122702Sandre goto drop; 462122702Sandre } 463122702Sandrepassin: 464122702Sandre ip = mtod(m, struct ip *); /* if m changed during fw processing */ 465122702Sandre 466122702Sandre /* 467122702Sandre * Destination address changed? 468122702Sandre */ 469122702Sandre if (odest != dest) { 470122702Sandre /* 471122702Sandre * Is it now for a local address on this host? 472122702Sandre */ 473122702Sandre LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 474122702Sandre if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 475122702Sandre goto forwardlocal; 476122702Sandre } 477122702Sandre /* 478122702Sandre * Go on with new destination address 479122702Sandre */ 480122702Sandre } 481122702Sandre 482122702Sandre /* 483122702Sandre * Step 4: decrement TTL and look up route 484122702Sandre */ 485122702Sandre 486122702Sandre /* 487122702Sandre * Check TTL 488122702Sandre */ 489122702Sandre#ifdef IPSTEALTH 490122702Sandre if (!ipstealth) { 491122702Sandre#endif 492122702Sandre if (ip->ip_ttl <= IPTTLDEC) { 493123740Speter icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, NULL); 494122702Sandre return 1; 495122702Sandre } 496122702Sandre 497122702Sandre /* 498122702Sandre * Decrement the TTL and incrementally change the checksum. 499122702Sandre * Don't bother doing this with hw checksum offloading. 500122702Sandre */ 501122702Sandre ip->ip_ttl -= IPTTLDEC; 502122702Sandre if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8)) 503122702Sandre ip->ip_sum -= ~htons(IPTTLDEC << 8); 504122702Sandre else 505122702Sandre ip->ip_sum += htons(IPTTLDEC << 8); 506122702Sandre#ifdef IPSTEALTH 507122702Sandre } 508122702Sandre#endif 509122702Sandre 510122702Sandre /* 511122702Sandre * Find route to destination. 512122702Sandre */ 513128872Sandre if ((dst = ip_findroute(&ro, dest, m)) == NULL) 514128872Sandre return 1; /* icmp unreach already sent */ 515128872Sandre ifp = ro.ro_rt->rt_ifp; 516122702Sandre 517122702Sandre /* 518122702Sandre * Step 5: outgoing firewall packet processing 519122702Sandre */ 520122702Sandre 521122702Sandre#ifdef PFIL_HOOKS 522122702Sandre /* 523122702Sandre * Run through list of hooks for output packets. 524122702Sandre */ 525122702Sandre if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT) || m == NULL) { 526128872Sandre goto consumed; 527122702Sandre } 528122702Sandre 529122702Sandre M_ASSERTVALID(m); 530122702Sandre M_ASSERTPKTHDR(m); 531122702Sandre 532122702Sandre ip = mtod(m, struct ip *); 533122702Sandre dest = ip->ip_dst.s_addr; 534122702Sandre#endif 535122702Sandre if (fw_enable && IPFW_LOADED && !args.next_hop) { 536122759Sandre bzero(&args, sizeof(args)); 537122702Sandre args.m = m; 538122702Sandre args.oif = ifp; 539122702Sandre 540122702Sandre ipfw = ip_fw_chk_ptr(&args); 541122702Sandre m = args.m; 542122702Sandre 543122702Sandre M_ASSERTVALID(m); 544122702Sandre M_ASSERTPKTHDR(m); 545122702Sandre 546128872Sandre if ((ipfw & IP_FW_PORT_DENY_FLAG) || m == NULL) 547122702Sandre goto drop; 548128872Sandre 549122702Sandre if (DUMMYNET_LOADED && (ipfw & IP_FW_PORT_DYNT_FLAG) != 0) { 550122702Sandre /* 551122702Sandre * XXX note: if the ifp or rt entry are deleted 552122702Sandre * while a pkt is in dummynet, we are in trouble! 553122702Sandre */ 554122702Sandre args.ro = &ro; /* dummynet does not save it */ 555122702Sandre args.dst = dst; 556122702Sandre 557122702Sandre ip_dn_io_ptr(m, ipfw & 0xffff, DN_TO_IP_OUT, &args); 558128872Sandre goto consumed; 559122702Sandre } 560122702Sandre#ifdef IPDIVERT 561122702Sandre if (ipfw != 0 && (ipfw & IP_FW_PORT_DYNT_FLAG) == 0) { 562122702Sandre /* 563122702Sandre * See if this is a fragment 564122702Sandre */ 565126239Smlaier if (ip->ip_off & (IP_MF | IP_OFFMASK)) 566122702Sandre goto droptoours; 567122702Sandre /* 568122702Sandre * Tee packet 569122702Sandre */ 570122702Sandre if ((ipfw & IP_FW_PORT_TEE_FLAG) != 0) 571126239Smlaier clone = divert_clone(m); 572122702Sandre else 573126239Smlaier clone = m; 574126239Smlaier if (clone == NULL) 575122702Sandre goto passout; 576122702Sandre 577122702Sandre /* 578122702Sandre * Delayed checksums are not compatible with divert 579122702Sandre */ 580126239Smlaier if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 581126239Smlaier in_delayed_cksum(m); 582126239Smlaier m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 583122702Sandre } 584122702Sandre /* 585122702Sandre * Restore packet header fields to original values 586122702Sandre */ 587126239Smlaier tip = mtod(m, struct ip *); 588122702Sandre tip->ip_len = htons(tip->ip_len); 589122702Sandre tip->ip_off = htons(tip->ip_off); 590122702Sandre /* 591122702Sandre * Deliver packet to divert input routine 592122702Sandre */ 593126239Smlaier divert_packet(m, 0); 594122702Sandre /* 595122702Sandre * If this was not tee, we are done 596122702Sandre */ 597126239Smlaier m = clone; 598122702Sandre if ((ipfw & IP_FW_PORT_TEE_FLAG) == 0) { 599128872Sandre goto consumed; 600122702Sandre } 601122702Sandre /* Continue if it was tee */ 602122702Sandre goto passout; 603122702Sandre } 604122702Sandre#endif 605122702Sandre if (ipfw == 0 && args.next_hop != NULL) { 606122702Sandre dest = args.next_hop->sin_addr.s_addr; 607122702Sandre goto passout; 608122702Sandre } 609122702Sandre /* 610122702Sandre * Let through or not? 611122702Sandre */ 612122702Sandre if (ipfw != 0) 613122702Sandre goto drop; 614122702Sandre } 615122702Sandrepassout: 616122702Sandre ip = mtod(m, struct ip *); 617122702Sandre 618122702Sandre /* 619122702Sandre * Destination address changed? 620122702Sandre */ 621122702Sandre if (odest != dest) { 622122702Sandre /* 623122702Sandre * Is it now for a local address on this host? 624122702Sandre */ 625122702Sandre LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 626122702Sandre if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) { 627122702Sandreforwardlocal: 628122702Sandre if (args.next_hop) { 629126239Smlaier struct m_tag *mtag = m_tag_get( 630126239Smlaier PACKET_TAG_IPFORWARD, 631126239Smlaier sizeof(struct sockaddr_in *), 632126239Smlaier M_NOWAIT); 633126239Smlaier if (mtag == NULL) { 634122702Sandre goto drop; 635122702Sandre } 636126239Smlaier *(struct sockaddr_in **)(mtag+1) = 637126239Smlaier args.next_hop; 638126239Smlaier m_tag_prepend(m, mtag); 639122702Sandre } 640122702Sandre#ifdef IPDIVERT 641122702Sandredroptoours: /* Used for DIVERT */ 642122702Sandre#endif 643126239Smlaier /* for ip_input */ 644126239Smlaier m->m_flags |= M_FASTFWD_OURS; 645122702Sandre 646122702Sandre /* ip still points to the real packet */ 647122702Sandre ip->ip_len = htons(ip->ip_len); 648122702Sandre ip->ip_off = htons(ip->ip_off); 649122702Sandre 650122702Sandre /* 651122702Sandre * Return packet for processing by ip_input 652122702Sandre */ 653122702Sandre if (ro.ro_rt) 654122702Sandre RTFREE(ro.ro_rt); 655122702Sandre return 0; 656122702Sandre } 657122702Sandre } 658122702Sandre /* 659122702Sandre * Redo route lookup with new destination address 660122702Sandre */ 661122702Sandre RTFREE(ro.ro_rt); 662128872Sandre if ((dst = ip_findroute(&ro, dest, m)) == NULL) 663128872Sandre return 1; /* icmp unreach already sent */ 664128872Sandre ifp = ro.ro_rt->rt_ifp; 665122702Sandre } 666122702Sandre 667122702Sandre /* 668122702Sandre * Step 6: send off the packet 669122702Sandre */ 670122702Sandre 671122702Sandre /* 672128872Sandre * Check if route is dampned (when ARP is unable to resolve) 673128872Sandre */ 674128872Sandre if ((ro.ro_rt->rt_flags & RTF_REJECT) && 675128872Sandre ro.ro_rt->rt_rmx.rmx_expire >= time_second) { 676128872Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, NULL); 677128872Sandre goto consumed; 678128872Sandre } 679128872Sandre 680133480Sandre#ifndef ALTQ 681128872Sandre /* 682128872Sandre * Check if there is enough space in the interface queue 683128872Sandre */ 684128872Sandre if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 685128872Sandre ifp->if_snd.ifq_maxlen) { 686128872Sandre ipstat.ips_odropped++; 687128872Sandre /* would send source quench here but that is depreciated */ 688128872Sandre goto drop; 689128872Sandre } 690133480Sandre#endif 691128872Sandre 692128872Sandre /* 693128872Sandre * Check if media link state of interface is not down 694128872Sandre */ 695128872Sandre if (ifp->if_link_state == LINK_STATE_DOWN) { 696128872Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, NULL); 697128872Sandre goto consumed; 698128872Sandre } 699128872Sandre 700128872Sandre /* 701122702Sandre * Check if packet fits MTU or if hardware will fragement for us 702122702Sandre */ 703122702Sandre if (ro.ro_rt->rt_rmx.rmx_mtu) 704122702Sandre mtu = min(ro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu); 705122702Sandre else 706122702Sandre mtu = ifp->if_mtu; 707122702Sandre 708122702Sandre if (ip->ip_len <= mtu || 709122702Sandre (ifp->if_hwassist & CSUM_FRAGMENT && (ip->ip_off & IP_DF) == 0)) { 710122702Sandre /* 711122702Sandre * Restore packet header fields to original values 712122702Sandre */ 713122702Sandre ip->ip_len = htons(ip->ip_len); 714122702Sandre ip->ip_off = htons(ip->ip_off); 715122702Sandre /* 716122702Sandre * Send off the packet via outgoing interface 717122702Sandre */ 718122702Sandre error = (*ifp->if_output)(ifp, m, 719122702Sandre (struct sockaddr *)dst, ro.ro_rt); 720122702Sandre } else { 721122702Sandre /* 722128872Sandre * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery 723122702Sandre */ 724122702Sandre if (ip->ip_off & IP_DF) { 725128872Sandre ipstat.ips_cantfrag++; 726122702Sandre icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 727123740Speter 0, ifp); 728128872Sandre goto consumed; 729122702Sandre } else { 730122702Sandre /* 731122702Sandre * We have to fragement the packet 732122702Sandre */ 733122702Sandre m->m_pkthdr.csum_flags |= CSUM_IP; 734128872Sandre /* 735128872Sandre * ip_fragment expects ip_len and ip_off in host byte 736128872Sandre * order but returns all packets in network byte order 737128872Sandre */ 738122702Sandre if (ip_fragment(ip, &m, mtu, ifp->if_hwassist, 739122702Sandre (~ifp->if_hwassist & CSUM_DELAY_IP))) { 740122702Sandre goto drop; 741122702Sandre } 742122702Sandre KASSERT(m != NULL, ("null mbuf and no error")); 743122702Sandre /* 744122702Sandre * Send off the fragments via outgoing interface 745122702Sandre */ 746122702Sandre error = 0; 747122702Sandre do { 748122702Sandre m0 = m->m_nextpkt; 749122702Sandre m->m_nextpkt = NULL; 750122702Sandre 751122702Sandre error = (*ifp->if_output)(ifp, m, 752122702Sandre (struct sockaddr *)dst, ro.ro_rt); 753122702Sandre if (error) 754122702Sandre break; 755122702Sandre } while ((m = m0) != NULL); 756122702Sandre if (error) { 757122702Sandre /* Reclaim remaining fragments */ 758122702Sandre for (; m; m = m0) { 759122702Sandre m0 = m->m_nextpkt; 760122702Sandre m->m_nextpkt = NULL; 761122702Sandre m_freem(m); 762122702Sandre } 763122702Sandre } else 764122702Sandre ipstat.ips_fragmented++; 765122702Sandre } 766122702Sandre } 767122702Sandre 768122702Sandre if (error != 0) 769122702Sandre ipstat.ips_odropped++; 770122702Sandre else { 771122702Sandre ro.ro_rt->rt_rmx.rmx_pksent++; 772122702Sandre ipstat.ips_forward++; 773122702Sandre ipstat.ips_fastforward++; 774122702Sandre } 775128872Sandreconsumed: 776122702Sandre RTFREE(ro.ro_rt); 777122702Sandre return 1; 778122702Sandredrop: 779122702Sandre if (m) 780122702Sandre m_freem(m); 781128872Sandre if (ro.ro_rt) 782128872Sandre RTFREE(ro.ro_rt); 783122702Sandre return 1; 784122702Sandre} 785