1c398230bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 324b421e2dSMike Silbersack #include <sys/cdefs.h> 334b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 344b421e2dSMike Silbersack 350ac40133SBrian Somers #include "opt_bootp.h" 3674a9466cSGary Palmer #include "opt_ipfw.h" 3727108a15SDag-Erling Smørgrav #include "opt_ipstealth.h" 386a800098SYoshinobu Inoue #include "opt_ipsec.h" 3933553d6eSBjoern A. Zeeb #include "opt_route.h" 4036b0360bSRobert Watson #include "opt_mac.h" 41a9771948SGleb Smirnoff #include "opt_carp.h" 4274a9466cSGary Palmer 43df8bae1dSRodney W. Grimes #include <sys/param.h> 44df8bae1dSRodney W. Grimes #include <sys/systm.h> 455f311da2SMike Silbersack #include <sys/callout.h> 46df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 47b715f178SLuigi Rizzo #include <sys/malloc.h> 48df8bae1dSRodney W. Grimes #include <sys/domain.h> 49df8bae1dSRodney W. Grimes #include <sys/protosw.h> 50df8bae1dSRodney W. Grimes #include <sys/socket.h> 51df8bae1dSRodney W. Grimes #include <sys/time.h> 52df8bae1dSRodney W. Grimes #include <sys/kernel.h> 53385195c0SMarko Zec #include <sys/lock.h> 54385195c0SMarko Zec #include <sys/rwlock.h> 551025071fSGarrett Wollman #include <sys/syslog.h> 56b5e8ce9fSBruce Evans #include <sys/sysctl.h> 57603724d3SBjoern A. Zeeb #include <sys/vimage.h> 58df8bae1dSRodney W. Grimes 59c85540ddSAndrey A. Chernov #include <net/pfil.h> 60df8bae1dSRodney W. Grimes #include <net/if.h> 619494d596SBrooks Davis #include <net/if_types.h> 62d314ad7bSJulian Elischer #include <net/if_var.h> 6382c23ebaSBill Fenner #include <net/if_dl.h> 64df8bae1dSRodney W. Grimes #include <net/route.h> 65748e0b0aSGarrett Wollman #include <net/netisr.h> 664b79449eSBjoern A. Zeeb #include <net/vnet.h> 67df8bae1dSRodney W. Grimes 68df8bae1dSRodney W. Grimes #include <netinet/in.h> 69df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 70b5e8ce9fSBruce Evans #include <netinet/in_var.h> 71df8bae1dSRodney W. Grimes #include <netinet/ip.h> 72df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 73df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 74df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h> 75ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 7658938916SGarrett Wollman #include <machine/in_cksum.h> 774b79449eSBjoern A. Zeeb #include <netinet/vinet.h> 78a9771948SGleb Smirnoff #ifdef DEV_CARP 79a9771948SGleb Smirnoff #include <netinet/ip_carp.h> 80a9771948SGleb Smirnoff #endif 81b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 821dfcf0d2SAndre Oppermann #include <netinet/ip_ipsec.h> 83b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 84df8bae1dSRodney W. Grimes 85f0068c4aSGarrett Wollman #include <sys/socketvar.h> 866ddbf1e2SGary Palmer 87010b65f5SJulian Elischer /* XXX: Temporary until ipfw_ether and ipfw_bridge are converted. */ 88010b65f5SJulian Elischer #include <netinet/ip_fw.h> 89010b65f5SJulian Elischer #include <netinet/ip_dummynet.h> 90010b65f5SJulian Elischer 91aed55708SRobert Watson #include <security/mac/mac_framework.h> 92aed55708SRobert Watson 93d2035ffbSEd Maste #ifdef CTASSERT 94d2035ffbSEd Maste CTASSERT(sizeof(struct ip) == 20); 95d2035ffbSEd Maste #endif 96d2035ffbSEd Maste 97385195c0SMarko Zec #ifndef VIMAGE 98385195c0SMarko Zec #ifndef VIMAGE_GLOBALS 99385195c0SMarko Zec struct vnet_inet vnet_inet_0; 100385195c0SMarko Zec #endif 101385195c0SMarko Zec #endif 102385195c0SMarko Zec 10344e33a07SMarko Zec #ifdef VIMAGE_GLOBALS 10444e33a07SMarko Zec static int ipsendredirects; 10544e33a07SMarko Zec static int ip_checkinterface; 10644e33a07SMarko Zec static int ip_keepfaith; 10744e33a07SMarko Zec static int ip_sendsourcequench; 10844e33a07SMarko Zec int ip_defttl; 10944e33a07SMarko Zec int ip_do_randomid; 11044e33a07SMarko Zec int ipforwarding; 11144e33a07SMarko Zec struct in_ifaddrhead in_ifaddrhead; /* first inet address */ 11244e33a07SMarko Zec struct in_ifaddrhashhead *in_ifaddrhashtbl; /* inet addr hash table */ 11344e33a07SMarko Zec u_long in_ifaddrhmask; /* mask for hash table */ 11444e33a07SMarko Zec struct ipstat ipstat; 11544e33a07SMarko Zec static int ip_rsvp_on; 11644e33a07SMarko Zec struct socket *ip_rsvpd; 11744e33a07SMarko Zec int rsvp_on; 118f02493cbSMarko Zec static struct ipqhead ipq[IPREASS_NHASH]; 11944e33a07SMarko Zec static int maxnipq; /* Administrative limit on # reass queues. */ 12044e33a07SMarko Zec static int maxfragsperpacket; 12144e33a07SMarko Zec int ipstealth; 12244e33a07SMarko Zec static int nipq; /* Total # of reass queues */ 12344e33a07SMarko Zec #endif 124f0068c4aSGarrett Wollman 1258b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_FORWARDING, 1268b615593SMarko Zec forwarding, CTLFLAG_RW, ipforwarding, 0, 1278b615593SMarko Zec "Enable IP forwarding between interfaces"); 1280312fbe9SPoul-Henning Kamp 1298b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_SENDREDIRECTS, 1308b615593SMarko Zec redirect, CTLFLAG_RW, ipsendredirects, 0, 1318b615593SMarko Zec "Enable sending IP redirects"); 1320312fbe9SPoul-Henning Kamp 1338b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_DEFTTL, 1348b615593SMarko Zec ttl, CTLFLAG_RW, ip_defttl, 0, "Maximum TTL on IP packets"); 1350312fbe9SPoul-Henning Kamp 1368b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_KEEPFAITH, 1378b615593SMarko Zec keepfaith, CTLFLAG_RW, ip_keepfaith, 0, 1386a800098SYoshinobu Inoue "Enable packet capture for FAITH IPv4->IPv6 translater daemon"); 1396a800098SYoshinobu Inoue 1408b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, 1418b615593SMarko Zec sendsourcequench, CTLFLAG_RW, ip_sendsourcequench, 0, 142df285b3dSMike Silbersack "Enable the transmission of source quench packets"); 143df285b3dSMike Silbersack 1448b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, random_id, 1458b615593SMarko Zec CTLFLAG_RW, ip_do_randomid, 0, "Assign random ip_id values"); 1461f44b0a1SDavid Malone 147823db0e9SDon Lewis /* 148823db0e9SDon Lewis * XXX - Setting ip_checkinterface mostly implements the receive side of 149823db0e9SDon Lewis * the Strong ES model described in RFC 1122, but since the routing table 150a8f12100SDon Lewis * and transmit implementation do not implement the Strong ES model, 151823db0e9SDon Lewis * setting this to 1 results in an odd hybrid. 1523f67c834SDon Lewis * 153a8f12100SDon Lewis * XXX - ip_checkinterface currently must be disabled if you use ipnat 154a8f12100SDon Lewis * to translate the destination address to another local interface. 1553f67c834SDon Lewis * 1563f67c834SDon Lewis * XXX - ip_checkinterface must be disabled if you add IP aliases 1573f67c834SDon Lewis * to the loopback interface instead of the interface where the 1583f67c834SDon Lewis * packets for those addresses are received. 159823db0e9SDon Lewis */ 1608b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, 1618b615593SMarko Zec check_interface, CTLFLAG_RW, ip_checkinterface, 0, 1628b615593SMarko Zec "Verify packet arrives on correct interface"); 163b3e95d4eSJonathan Lemon 164c21fd232SAndre Oppermann struct pfil_head inet_pfil_hook; /* Packet filter hooks */ 165df8bae1dSRodney W. Grimes 1661cafed39SJonathan Lemon static struct ifqueue ipintrq; 167ca925d9cSJonathan Lemon static int ipqmaxlen = IFQ_MAXLEN; 168ca925d9cSJonathan Lemon 169df8bae1dSRodney W. Grimes extern struct domain inetdomain; 170f0ffb944SJulian Elischer extern struct protosw inetsw[]; 171df8bae1dSRodney W. Grimes u_char ip_protox[IPPROTO_MAX]; 172ca925d9cSJonathan Lemon 173afed1375SDavid Greenman SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW, 1743d177f46SBill Fumerola &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue"); 1750312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD, 1766489fe65SAndre Oppermann &ipintrq.ifq_drops, 0, 1776489fe65SAndre Oppermann "Number of packets dropped from the IP input queue"); 178df8bae1dSRodney W. Grimes 1798b615593SMarko Zec SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW, 1808b615593SMarko Zec ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)"); 181194a213eSAndrey A. Chernov 182385195c0SMarko Zec #ifdef VIMAGE_GLOBALS 183d248c7d7SRobert Watson static uma_zone_t ipq_zone; 184385195c0SMarko Zec #endif 185dfa60d93SRobert Watson static struct mtx ipqlock; 1862fad1e93SSam Leffler 1872fad1e93SSam Leffler #define IPQ_LOCK() mtx_lock(&ipqlock) 1882fad1e93SSam Leffler #define IPQ_UNLOCK() mtx_unlock(&ipqlock) 189888c2a3cSSam Leffler #define IPQ_LOCK_INIT() mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF) 190888c2a3cSSam Leffler #define IPQ_LOCK_ASSERT() mtx_assert(&ipqlock, MA_OWNED) 191f23b4c91SGarrett Wollman 192d248c7d7SRobert Watson static void maxnipq_update(void); 1934f590175SPaul Saab static void ipq_zone_change(void *); 194d248c7d7SRobert Watson 1958b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, fragpackets, 1968b615593SMarko Zec CTLFLAG_RD, nipq, 0, 1978b615593SMarko Zec "Current number of IPv4 fragment reassembly queue entries"); 198d248c7d7SRobert Watson 1998b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, maxfragsperpacket, 2008b615593SMarko Zec CTLFLAG_RW, maxfragsperpacket, 0, 201d248c7d7SRobert Watson "Maximum number of IPv4 fragments allowed per packet"); 202d248c7d7SRobert Watson 203d248c7d7SRobert Watson struct callout ipport_tick_callout; 204d248c7d7SRobert Watson 2050312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU 2060312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, 2073d177f46SBill Fumerola &ip_mtu, 0, "Default MTU"); 2080312fbe9SPoul-Henning Kamp #endif 2090312fbe9SPoul-Henning Kamp 2101b968362SDag-Erling Smørgrav #ifdef IPSTEALTH 2118b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW, 2128b615593SMarko Zec ipstealth, 0, "IP stealth mode, no TTL decrementation on forwarding"); 2131b968362SDag-Erling Smørgrav #endif 2141b968362SDag-Erling Smørgrav 215010b65f5SJulian Elischer /* 216010b65f5SJulian Elischer * ipfw_ether and ipfw_bridge hooks. 217010b65f5SJulian Elischer * XXX: Temporary until those are converted to pfil_hooks as well. 218010b65f5SJulian Elischer */ 219010b65f5SJulian Elischer ip_fw_chk_t *ip_fw_chk_ptr = NULL; 220010b65f5SJulian Elischer ip_dn_io_t *ip_dn_io_ptr = NULL; 221385195c0SMarko Zec #ifdef VIMAGE_GLOBALS 222385195c0SMarko Zec int fw_one_pass; 223385195c0SMarko Zec #endif 224010b65f5SJulian Elischer 2254d77a549SAlfred Perlstein static void ip_freef(struct ipqhead *, struct ipq *); 2268948e4baSArchie Cobbs 227df8bae1dSRodney W. Grimes /* 228df8bae1dSRodney W. Grimes * IP initialization: fill in IP protocol switch table. 229df8bae1dSRodney W. Grimes * All protocols not implemented in kernel go to raw IP protocol handler. 230df8bae1dSRodney W. Grimes */ 231df8bae1dSRodney W. Grimes void 232f2565d68SRobert Watson ip_init(void) 233df8bae1dSRodney W. Grimes { 2348b615593SMarko Zec INIT_VNET_INET(curvnet); 235f2565d68SRobert Watson struct protosw *pr; 236f2565d68SRobert Watson int i; 237df8bae1dSRodney W. Grimes 23844e33a07SMarko Zec V_ipsendredirects = 1; /* XXX */ 23944e33a07SMarko Zec V_ip_checkinterface = 0; 24044e33a07SMarko Zec V_ip_keepfaith = 0; 24144e33a07SMarko Zec V_ip_sendsourcequench = 0; 24244e33a07SMarko Zec V_rsvp_on = 0; 24344e33a07SMarko Zec V_ip_defttl = IPDEFTTL; 24444e33a07SMarko Zec V_ip_do_randomid = 0; 2451ed81b73SMarko Zec V_ip_id = time_second & 0xffff; 24644e33a07SMarko Zec V_ipforwarding = 0; 24744e33a07SMarko Zec V_ipstealth = 0; 24844e33a07SMarko Zec V_nipq = 0; /* Total # of reass queues */ 24944e33a07SMarko Zec 25044e33a07SMarko Zec V_ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */ 25144e33a07SMarko Zec V_ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */ 25244e33a07SMarko Zec V_ipport_firstauto = IPPORT_EPHEMERALFIRST; /* 10000 */ 25344e33a07SMarko Zec V_ipport_lastauto = IPPORT_EPHEMERALLAST; /* 65535 */ 25444e33a07SMarko Zec V_ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */ 25544e33a07SMarko Zec V_ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */ 25644e33a07SMarko Zec V_ipport_reservedhigh = IPPORT_RESERVED - 1; /* 1023 */ 25744e33a07SMarko Zec V_ipport_reservedlow = 0; 25844e33a07SMarko Zec V_ipport_randomized = 1; /* user controlled via sysctl */ 25944e33a07SMarko Zec V_ipport_randomcps = 10; /* user controlled via sysctl */ 26044e33a07SMarko Zec V_ipport_randomtime = 45; /* user controlled via sysctl */ 26144e33a07SMarko Zec V_ipport_stoprandom = 0; /* toggled by ipport_tick */ 26244e33a07SMarko Zec 263385195c0SMarko Zec V_fw_one_pass = 1; 264385195c0SMarko Zec 26544e33a07SMarko Zec #ifdef NOTYET 26644e33a07SMarko Zec /* XXX global static but not instantiated in this file */ 26744e33a07SMarko Zec V_ipfastforward_active = 0; 26844e33a07SMarko Zec V_subnetsarelocal = 0; 26944e33a07SMarko Zec V_sameprefixcarponly = 0; 27044e33a07SMarko Zec #endif 27144e33a07SMarko Zec 272603724d3SBjoern A. Zeeb TAILQ_INIT(&V_in_ifaddrhead); 273603724d3SBjoern A. Zeeb V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask); 2741ed81b73SMarko Zec 2751ed81b73SMarko Zec /* Initialize IP reassembly queue. */ 2761ed81b73SMarko Zec for (i = 0; i < IPREASS_NHASH; i++) 2771ed81b73SMarko Zec TAILQ_INIT(&V_ipq[i]); 2781ed81b73SMarko Zec V_maxnipq = nmbclusters / 32; 2791ed81b73SMarko Zec V_maxfragsperpacket = 16; 2801ed81b73SMarko Zec V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL, 2811ed81b73SMarko Zec NULL, UMA_ALIGN_PTR, 0); 2821ed81b73SMarko Zec maxnipq_update(); 2831ed81b73SMarko Zec 2841ed81b73SMarko Zec /* Skip initialization of globals for non-default instances. */ 2851ed81b73SMarko Zec if (!IS_DEFAULT_VNET(curvnet)) 2861ed81b73SMarko Zec return; 2871ed81b73SMarko Zec 288f0ffb944SJulian Elischer pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 28902410549SRobert Watson if (pr == NULL) 290db09bef3SAndre Oppermann panic("ip_init: PF_INET not found"); 291db09bef3SAndre Oppermann 292db09bef3SAndre Oppermann /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */ 293df8bae1dSRodney W. Grimes for (i = 0; i < IPPROTO_MAX; i++) 294df8bae1dSRodney W. Grimes ip_protox[i] = pr - inetsw; 295db09bef3SAndre Oppermann /* 296db09bef3SAndre Oppermann * Cycle through IP protocols and put them into the appropriate place 297db09bef3SAndre Oppermann * in ip_protox[]. 298db09bef3SAndre Oppermann */ 299f0ffb944SJulian Elischer for (pr = inetdomain.dom_protosw; 300f0ffb944SJulian Elischer pr < inetdomain.dom_protoswNPROTOSW; pr++) 301df8bae1dSRodney W. Grimes if (pr->pr_domain->dom_family == PF_INET && 302db09bef3SAndre Oppermann pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) { 303db09bef3SAndre Oppermann /* Be careful to only index valid IP protocols. */ 304db77984cSSam Leffler if (pr->pr_protocol < IPPROTO_MAX) 305df8bae1dSRodney W. Grimes ip_protox[pr->pr_protocol] = pr - inetsw; 306db09bef3SAndre Oppermann } 307194a213eSAndrey A. Chernov 308c21fd232SAndre Oppermann /* Initialize packet filter hooks. */ 309134ea224SSam Leffler inet_pfil_hook.ph_type = PFIL_TYPE_AF; 310134ea224SSam Leffler inet_pfil_hook.ph_af = AF_INET; 311134ea224SSam Leffler if ((i = pfil_head_register(&inet_pfil_hook)) != 0) 312134ea224SSam Leffler printf("%s: WARNING: unable to register pfil hook, " 313134ea224SSam Leffler "error %d\n", __func__, i); 314134ea224SSam Leffler 3155f311da2SMike Silbersack /* Start ipport_tick. */ 3165f311da2SMike Silbersack callout_init(&ipport_tick_callout, CALLOUT_MPSAFE); 3175f311da2SMike Silbersack ipport_tick(NULL); 3185f311da2SMike Silbersack EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 3195f311da2SMike Silbersack SHUTDOWN_PRI_DEFAULT); 3204f590175SPaul Saab EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change, 3214f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 3225f311da2SMike Silbersack 323db09bef3SAndre Oppermann /* Initialize various other remaining things. */ 3241ed81b73SMarko Zec IPQ_LOCK_INIT(); 325df8bae1dSRodney W. Grimes ipintrq.ifq_maxlen = ipqmaxlen; 3266008862bSJohn Baldwin mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF); 32759dd72d0SRobert Watson netisr_register(NETISR_IP, ip_input, &ipintrq, 0); 328df8bae1dSRodney W. Grimes } 329df8bae1dSRodney W. Grimes 330f2565d68SRobert Watson void 331f2565d68SRobert Watson ip_fini(void *xtp) 3325f311da2SMike Silbersack { 333f2565d68SRobert Watson 3345f311da2SMike Silbersack callout_stop(&ipport_tick_callout); 3355f311da2SMike Silbersack } 3365f311da2SMike Silbersack 3374d2e3692SLuigi Rizzo /* 338df8bae1dSRodney W. Grimes * Ip input routine. Checksum and byte swap header. If fragmented 339df8bae1dSRodney W. Grimes * try to reassemble. Process options. Pass to next level. 340df8bae1dSRodney W. Grimes */ 341c67b1d17SGarrett Wollman void 342c67b1d17SGarrett Wollman ip_input(struct mbuf *m) 343df8bae1dSRodney W. Grimes { 3448b615593SMarko Zec INIT_VNET_INET(curvnet); 3459188b4a1SAndre Oppermann struct ip *ip = NULL; 3465da9f8faSJosef Karthauser struct in_ifaddr *ia = NULL; 347ca925d9cSJonathan Lemon struct ifaddr *ifa; 3489b932e9eSAndre Oppermann int checkif, hlen = 0; 34947c861ecSBrian Somers u_short sum; 35002c1c707SAndre Oppermann int dchg = 0; /* dest changed after fw */ 351f51f805fSSam Leffler struct in_addr odst; /* original dst address */ 352b715f178SLuigi Rizzo 353fe584538SDag-Erling Smørgrav M_ASSERTPKTHDR(m); 354db40007dSAndrew R. Reiter 355ac9d7e26SMax Laier if (m->m_flags & M_FASTFWD_OURS) { 3569b932e9eSAndre Oppermann /* 35776ff6dcfSAndre Oppermann * Firewall or NAT changed destination to local. 35876ff6dcfSAndre Oppermann * We expect ip_len and ip_off to be in host byte order. 3599b932e9eSAndre Oppermann */ 36076ff6dcfSAndre Oppermann m->m_flags &= ~M_FASTFWD_OURS; 36176ff6dcfSAndre Oppermann /* Set up some basics that will be used later. */ 3622b25acc1SLuigi Rizzo ip = mtod(m, struct ip *); 36353be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 3649b932e9eSAndre Oppermann goto ours; 3652b25acc1SLuigi Rizzo } 3662b25acc1SLuigi Rizzo 367603724d3SBjoern A. Zeeb V_ipstat.ips_total++; 36858938916SGarrett Wollman 36958938916SGarrett Wollman if (m->m_pkthdr.len < sizeof(struct ip)) 37058938916SGarrett Wollman goto tooshort; 37158938916SGarrett Wollman 372df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct ip) && 3730b17fba7SAndre Oppermann (m = m_pullup(m, sizeof (struct ip))) == NULL) { 374603724d3SBjoern A. Zeeb V_ipstat.ips_toosmall++; 375c67b1d17SGarrett Wollman return; 376df8bae1dSRodney W. Grimes } 377df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 37858938916SGarrett Wollman 37953be11f6SPoul-Henning Kamp if (ip->ip_v != IPVERSION) { 380603724d3SBjoern A. Zeeb V_ipstat.ips_badvers++; 381df8bae1dSRodney W. Grimes goto bad; 382df8bae1dSRodney W. Grimes } 38358938916SGarrett Wollman 38453be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 385df8bae1dSRodney W. Grimes if (hlen < sizeof(struct ip)) { /* minimum header length */ 386603724d3SBjoern A. Zeeb V_ipstat.ips_badhlen++; 387df8bae1dSRodney W. Grimes goto bad; 388df8bae1dSRodney W. Grimes } 389df8bae1dSRodney W. Grimes if (hlen > m->m_len) { 3900b17fba7SAndre Oppermann if ((m = m_pullup(m, hlen)) == NULL) { 391603724d3SBjoern A. Zeeb V_ipstat.ips_badhlen++; 392c67b1d17SGarrett Wollman return; 393df8bae1dSRodney W. Grimes } 394df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 395df8bae1dSRodney W. Grimes } 39633841545SHajimu UMEMOTO 39733841545SHajimu UMEMOTO /* 127/8 must not appear on wire - RFC1122 */ 39833841545SHajimu UMEMOTO if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 39933841545SHajimu UMEMOTO (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 40033841545SHajimu UMEMOTO if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 401603724d3SBjoern A. Zeeb V_ipstat.ips_badaddr++; 40233841545SHajimu UMEMOTO goto bad; 40333841545SHajimu UMEMOTO } 40433841545SHajimu UMEMOTO } 40533841545SHajimu UMEMOTO 406db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 407db4f9cc7SJonathan Lemon sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 408db4f9cc7SJonathan Lemon } else { 40958938916SGarrett Wollman if (hlen == sizeof(struct ip)) { 41047c861ecSBrian Somers sum = in_cksum_hdr(ip); 41158938916SGarrett Wollman } else { 41247c861ecSBrian Somers sum = in_cksum(m, hlen); 41358938916SGarrett Wollman } 414db4f9cc7SJonathan Lemon } 41547c861ecSBrian Somers if (sum) { 416603724d3SBjoern A. Zeeb V_ipstat.ips_badsum++; 417df8bae1dSRodney W. Grimes goto bad; 418df8bae1dSRodney W. Grimes } 419df8bae1dSRodney W. Grimes 42002b199f1SMax Laier #ifdef ALTQ 42102b199f1SMax Laier if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 42202b199f1SMax Laier /* packet is dropped by traffic conditioner */ 42302b199f1SMax Laier return; 42402b199f1SMax Laier #endif 42502b199f1SMax Laier 426df8bae1dSRodney W. Grimes /* 427df8bae1dSRodney W. Grimes * Convert fields to host representation. 428df8bae1dSRodney W. Grimes */ 429fd8e4ebcSMike Barcroft ip->ip_len = ntohs(ip->ip_len); 430df8bae1dSRodney W. Grimes if (ip->ip_len < hlen) { 431603724d3SBjoern A. Zeeb V_ipstat.ips_badlen++; 432df8bae1dSRodney W. Grimes goto bad; 433df8bae1dSRodney W. Grimes } 434fd8e4ebcSMike Barcroft ip->ip_off = ntohs(ip->ip_off); 435df8bae1dSRodney W. Grimes 436df8bae1dSRodney W. Grimes /* 437df8bae1dSRodney W. Grimes * Check that the amount of data in the buffers 438df8bae1dSRodney W. Grimes * is as at least much as the IP header would have us expect. 439df8bae1dSRodney W. Grimes * Trim mbufs if longer than we expect. 440df8bae1dSRodney W. Grimes * Drop packet if shorter than we expect. 441df8bae1dSRodney W. Grimes */ 442df8bae1dSRodney W. Grimes if (m->m_pkthdr.len < ip->ip_len) { 44358938916SGarrett Wollman tooshort: 444603724d3SBjoern A. Zeeb V_ipstat.ips_tooshort++; 445df8bae1dSRodney W. Grimes goto bad; 446df8bae1dSRodney W. Grimes } 447df8bae1dSRodney W. Grimes if (m->m_pkthdr.len > ip->ip_len) { 448df8bae1dSRodney W. Grimes if (m->m_len == m->m_pkthdr.len) { 449df8bae1dSRodney W. Grimes m->m_len = ip->ip_len; 450df8bae1dSRodney W. Grimes m->m_pkthdr.len = ip->ip_len; 451df8bae1dSRodney W. Grimes } else 452df8bae1dSRodney W. Grimes m_adj(m, ip->ip_len - m->m_pkthdr.len); 453df8bae1dSRodney W. Grimes } 454b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 45514dd6717SSam Leffler /* 45614dd6717SSam Leffler * Bypass packet filtering for packets from a tunnel (gif). 45714dd6717SSam Leffler */ 458cc977adcSBjoern A. Zeeb if (ip_ipsec_filtertunnel(m)) 459c21fd232SAndre Oppermann goto passin; 460b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 4613f67c834SDon Lewis 462c4ac87eaSDarren Reed /* 463134ea224SSam Leffler * Run through list of hooks for input packets. 464f51f805fSSam Leffler * 465f51f805fSSam Leffler * NB: Beware of the destination address changing (e.g. 466f51f805fSSam Leffler * by NAT rewriting). When this happens, tell 467f51f805fSSam Leffler * ip_forward to do the right thing. 468c4ac87eaSDarren Reed */ 469c21fd232SAndre Oppermann 470c21fd232SAndre Oppermann /* Jump over all PFIL processing if hooks are not active. */ 471604afec4SChristian S.J. Peron if (!PFIL_HOOKED(&inet_pfil_hook)) 472c21fd232SAndre Oppermann goto passin; 473c21fd232SAndre Oppermann 474f51f805fSSam Leffler odst = ip->ip_dst; 475134ea224SSam Leffler if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, 476d6a8d588SMax Laier PFIL_IN, NULL) != 0) 477beec8214SDarren Reed return; 478134ea224SSam Leffler if (m == NULL) /* consumed by filter */ 479c4ac87eaSDarren Reed return; 4809b932e9eSAndre Oppermann 481c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 48202c1c707SAndre Oppermann dchg = (odst.s_addr != ip->ip_dst.s_addr); 4839b932e9eSAndre Oppermann 4849b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 4859b932e9eSAndre Oppermann if (m->m_flags & M_FASTFWD_OURS) { 4869b932e9eSAndre Oppermann m->m_flags &= ~M_FASTFWD_OURS; 4879b932e9eSAndre Oppermann goto ours; 4889b932e9eSAndre Oppermann } 489099dd043SAndre Oppermann if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) { 490099dd043SAndre Oppermann /* 491099dd043SAndre Oppermann * Directly ship on the packet. This allows to forward packets 492099dd043SAndre Oppermann * that were destined for us to some other directly connected 493099dd043SAndre Oppermann * host. 494099dd043SAndre Oppermann */ 495099dd043SAndre Oppermann ip_forward(m, dchg); 496099dd043SAndre Oppermann return; 497099dd043SAndre Oppermann } 4989b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 4999b932e9eSAndre Oppermann 500c21fd232SAndre Oppermann passin: 501df8bae1dSRodney W. Grimes /* 502df8bae1dSRodney W. Grimes * Process options and, if not destined for us, 503df8bae1dSRodney W. Grimes * ship it on. ip_dooptions returns 1 when an 504df8bae1dSRodney W. Grimes * error was detected (causing an icmp message 505df8bae1dSRodney W. Grimes * to be sent and the original packet to be freed). 506df8bae1dSRodney W. Grimes */ 5079b932e9eSAndre Oppermann if (hlen > sizeof (struct ip) && ip_dooptions(m, 0)) 508c67b1d17SGarrett Wollman return; 509df8bae1dSRodney W. Grimes 510f0068c4aSGarrett Wollman /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 511f0068c4aSGarrett Wollman * matter if it is destined to another node, or whether it is 512f0068c4aSGarrett Wollman * a multicast one, RSVP wants it! and prevents it from being forwarded 513f0068c4aSGarrett Wollman * anywhere else. Also checks if the rsvp daemon is running before 514f0068c4aSGarrett Wollman * grabbing the packet. 515f0068c4aSGarrett Wollman */ 516603724d3SBjoern A. Zeeb if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP) 517f0068c4aSGarrett Wollman goto ours; 518f0068c4aSGarrett Wollman 519df8bae1dSRodney W. Grimes /* 520df8bae1dSRodney W. Grimes * Check our list of addresses, to see if the packet is for us. 521cc766e04SGarrett Wollman * If we don't have any addresses, assume any unicast packet 522cc766e04SGarrett Wollman * we receive might be for us (and let the upper layers deal 523cc766e04SGarrett Wollman * with it). 524df8bae1dSRodney W. Grimes */ 525603724d3SBjoern A. Zeeb if (TAILQ_EMPTY(&V_in_ifaddrhead) && 526cc766e04SGarrett Wollman (m->m_flags & (M_MCAST|M_BCAST)) == 0) 527cc766e04SGarrett Wollman goto ours; 528cc766e04SGarrett Wollman 5297538a9a0SJonathan Lemon /* 530823db0e9SDon Lewis * Enable a consistency check between the destination address 531823db0e9SDon Lewis * and the arrival interface for a unicast packet (the RFC 1122 532823db0e9SDon Lewis * strong ES model) if IP forwarding is disabled and the packet 533e15ae1b2SDon Lewis * is not locally generated and the packet is not subject to 534e15ae1b2SDon Lewis * 'ipfw fwd'. 5353f67c834SDon Lewis * 5363f67c834SDon Lewis * XXX - Checking also should be disabled if the destination 5373f67c834SDon Lewis * address is ipnat'ed to a different interface. 5383f67c834SDon Lewis * 539a8f12100SDon Lewis * XXX - Checking is incompatible with IP aliases added 5403f67c834SDon Lewis * to the loopback interface instead of the interface where 5413f67c834SDon Lewis * the packets are received. 542a9771948SGleb Smirnoff * 543a9771948SGleb Smirnoff * XXX - This is the case for carp vhost IPs as well so we 544a9771948SGleb Smirnoff * insert a workaround. If the packet got here, we already 545a9771948SGleb Smirnoff * checked with carp_iamatch() and carp_forus(). 546823db0e9SDon Lewis */ 547603724d3SBjoern A. Zeeb checkif = V_ip_checkinterface && (V_ipforwarding == 0) && 5489494d596SBrooks Davis m->m_pkthdr.rcvif != NULL && 549e15ae1b2SDon Lewis ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) && 550a9771948SGleb Smirnoff #ifdef DEV_CARP 551a9771948SGleb Smirnoff !m->m_pkthdr.rcvif->if_carp && 552a9771948SGleb Smirnoff #endif 5539b932e9eSAndre Oppermann (dchg == 0); 554823db0e9SDon Lewis 555ca925d9cSJonathan Lemon /* 556ca925d9cSJonathan Lemon * Check for exact addresses in the hash bucket. 557ca925d9cSJonathan Lemon */ 5589b932e9eSAndre Oppermann LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 559f9e354dfSJulian Elischer /* 560823db0e9SDon Lewis * If the address matches, verify that the packet 561823db0e9SDon Lewis * arrived via the correct interface if checking is 562823db0e9SDon Lewis * enabled. 563f9e354dfSJulian Elischer */ 5649b932e9eSAndre Oppermann if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr && 565823db0e9SDon Lewis (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif)) 566ed1ff184SJulian Elischer goto ours; 567ca925d9cSJonathan Lemon } 568823db0e9SDon Lewis /* 569ca925d9cSJonathan Lemon * Check for broadcast addresses. 570ca925d9cSJonathan Lemon * 571ca925d9cSJonathan Lemon * Only accept broadcast packets that arrive via the matching 572ca925d9cSJonathan Lemon * interface. Reception of forwarded directed broadcasts would 573ca925d9cSJonathan Lemon * be handled via ip_forward() and ether_output() with the loopback 574ca925d9cSJonathan Lemon * into the stack for SIMPLEX interfaces handled by ether_output(). 575823db0e9SDon Lewis */ 5764f450ff9SBruce M Simpson if (m->m_pkthdr.rcvif != NULL && 5774f450ff9SBruce M Simpson m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 578ca925d9cSJonathan Lemon TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 579ca925d9cSJonathan Lemon if (ifa->ifa_addr->sa_family != AF_INET) 580ca925d9cSJonathan Lemon continue; 581ca925d9cSJonathan Lemon ia = ifatoia(ifa); 582df8bae1dSRodney W. Grimes if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 5839b932e9eSAndre Oppermann ip->ip_dst.s_addr) 584df8bae1dSRodney W. Grimes goto ours; 5859b932e9eSAndre Oppermann if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr) 586df8bae1dSRodney W. Grimes goto ours; 5870ac40133SBrian Somers #ifdef BOOTP_COMPAT 588ca925d9cSJonathan Lemon if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) 589ca925d9cSJonathan Lemon goto ours; 5900ac40133SBrian Somers #endif 591df8bae1dSRodney W. Grimes } 592df8bae1dSRodney W. Grimes } 593f8429ca2SBruce M Simpson /* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */ 594f8429ca2SBruce M Simpson if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) { 595603724d3SBjoern A. Zeeb V_ipstat.ips_cantforward++; 596f8429ca2SBruce M Simpson m_freem(m); 597f8429ca2SBruce M Simpson return; 598f8429ca2SBruce M Simpson } 599df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 600603724d3SBjoern A. Zeeb if (V_ip_mrouter) { 601df8bae1dSRodney W. Grimes /* 602df8bae1dSRodney W. Grimes * If we are acting as a multicast router, all 603df8bae1dSRodney W. Grimes * incoming multicast packets are passed to the 604df8bae1dSRodney W. Grimes * kernel-level multicast forwarding function. 605df8bae1dSRodney W. Grimes * The packet is returned (relatively) intact; if 606df8bae1dSRodney W. Grimes * ip_mforward() returns a non-zero value, the packet 607df8bae1dSRodney W. Grimes * must be discarded, else it may be accepted below. 608df8bae1dSRodney W. Grimes */ 609bbb4330bSLuigi Rizzo if (ip_mforward && 610bbb4330bSLuigi Rizzo ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) { 611603724d3SBjoern A. Zeeb V_ipstat.ips_cantforward++; 612df8bae1dSRodney W. Grimes m_freem(m); 613c67b1d17SGarrett Wollman return; 614df8bae1dSRodney W. Grimes } 615df8bae1dSRodney W. Grimes 616df8bae1dSRodney W. Grimes /* 61711612afaSDima Dorfman * The process-level routing daemon needs to receive 618df8bae1dSRodney W. Grimes * all multicast IGMP packets, whether or not this 619df8bae1dSRodney W. Grimes * host belongs to their destination groups. 620df8bae1dSRodney W. Grimes */ 621df8bae1dSRodney W. Grimes if (ip->ip_p == IPPROTO_IGMP) 622df8bae1dSRodney W. Grimes goto ours; 623603724d3SBjoern A. Zeeb V_ipstat.ips_forward++; 624df8bae1dSRodney W. Grimes } 625df8bae1dSRodney W. Grimes /* 626d10910e6SBruce M Simpson * Assume the packet is for us, to avoid prematurely taking 627d10910e6SBruce M Simpson * a lock on the in_multi hash. Protocols must perform 628d10910e6SBruce M Simpson * their own filtering and update statistics accordingly. 629df8bae1dSRodney W. Grimes */ 630df8bae1dSRodney W. Grimes goto ours; 631df8bae1dSRodney W. Grimes } 632df8bae1dSRodney W. Grimes if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 633df8bae1dSRodney W. Grimes goto ours; 634df8bae1dSRodney W. Grimes if (ip->ip_dst.s_addr == INADDR_ANY) 635df8bae1dSRodney W. Grimes goto ours; 636df8bae1dSRodney W. Grimes 6376a800098SYoshinobu Inoue /* 6386a800098SYoshinobu Inoue * FAITH(Firewall Aided Internet Translator) 6396a800098SYoshinobu Inoue */ 6406a800098SYoshinobu Inoue if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 641603724d3SBjoern A. Zeeb if (V_ip_keepfaith) { 6426a800098SYoshinobu Inoue if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) 6436a800098SYoshinobu Inoue goto ours; 6446a800098SYoshinobu Inoue } 6456a800098SYoshinobu Inoue m_freem(m); 6466a800098SYoshinobu Inoue return; 6476a800098SYoshinobu Inoue } 6489494d596SBrooks Davis 649df8bae1dSRodney W. Grimes /* 650df8bae1dSRodney W. Grimes * Not for us; forward if possible and desirable. 651df8bae1dSRodney W. Grimes */ 652603724d3SBjoern A. Zeeb if (V_ipforwarding == 0) { 653603724d3SBjoern A. Zeeb V_ipstat.ips_cantforward++; 654df8bae1dSRodney W. Grimes m_freem(m); 655546f251bSChris D. Faulhaber } else { 656b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 6571dfcf0d2SAndre Oppermann if (ip_ipsec_fwd(m)) 658546f251bSChris D. Faulhaber goto bad; 659b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 6609b932e9eSAndre Oppermann ip_forward(m, dchg); 661546f251bSChris D. Faulhaber } 662c67b1d17SGarrett Wollman return; 663df8bae1dSRodney W. Grimes 664df8bae1dSRodney W. Grimes ours: 665d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH 666d0ebc0d2SYaroslav Tykhiy /* 667d0ebc0d2SYaroslav Tykhiy * IPSTEALTH: Process non-routing options only 668d0ebc0d2SYaroslav Tykhiy * if the packet is destined for us. 669d0ebc0d2SYaroslav Tykhiy */ 670603724d3SBjoern A. Zeeb if (V_ipstealth && hlen > sizeof (struct ip) && 6719b932e9eSAndre Oppermann ip_dooptions(m, 1)) 672d0ebc0d2SYaroslav Tykhiy return; 673d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */ 674d0ebc0d2SYaroslav Tykhiy 6755da9f8faSJosef Karthauser /* Count the packet in the ip address stats */ 6765da9f8faSJosef Karthauser if (ia != NULL) { 6775da9f8faSJosef Karthauser ia->ia_ifa.if_ipackets++; 6785da9f8faSJosef Karthauser ia->ia_ifa.if_ibytes += m->m_pkthdr.len; 6795da9f8faSJosef Karthauser } 680100ba1a6SJordan K. Hubbard 68163f8d699SJordan K. Hubbard /* 682b6ea1aa5SRuslan Ermilov * Attempt reassembly; if it succeeds, proceed. 683ac9d7e26SMax Laier * ip_reass() will return a different mbuf. 684df8bae1dSRodney W. Grimes */ 685f0cada84SAndre Oppermann if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 686f0cada84SAndre Oppermann m = ip_reass(m); 687f0cada84SAndre Oppermann if (m == NULL) 688c67b1d17SGarrett Wollman return; 6896a800098SYoshinobu Inoue ip = mtod(m, struct ip *); 6907e2df452SRuslan Ermilov /* Get the header length of the reassembled packet */ 69153be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 692f0cada84SAndre Oppermann } 693f0cada84SAndre Oppermann 694f0cada84SAndre Oppermann /* 695f0cada84SAndre Oppermann * Further protocols expect the packet length to be w/o the 696f0cada84SAndre Oppermann * IP header. 697f0cada84SAndre Oppermann */ 698df8bae1dSRodney W. Grimes ip->ip_len -= hlen; 699df8bae1dSRodney W. Grimes 700b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 70133841545SHajimu UMEMOTO /* 70233841545SHajimu UMEMOTO * enforce IPsec policy checking if we are seeing last header. 70333841545SHajimu UMEMOTO * note that we do not visit this with protocols with pcb layer 70433841545SHajimu UMEMOTO * code - like udp/tcp/raw ip. 70533841545SHajimu UMEMOTO */ 7061dfcf0d2SAndre Oppermann if (ip_ipsec_input(m)) 70733841545SHajimu UMEMOTO goto bad; 708b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 70933841545SHajimu UMEMOTO 710df8bae1dSRodney W. Grimes /* 711df8bae1dSRodney W. Grimes * Switch out to protocol's input routine. 712df8bae1dSRodney W. Grimes */ 713603724d3SBjoern A. Zeeb V_ipstat.ips_delivered++; 7149b932e9eSAndre Oppermann 7152b25acc1SLuigi Rizzo (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 716c67b1d17SGarrett Wollman return; 717df8bae1dSRodney W. Grimes bad: 718df8bae1dSRodney W. Grimes m_freem(m); 719c67b1d17SGarrett Wollman } 720c67b1d17SGarrett Wollman 721c67b1d17SGarrett Wollman /* 722d248c7d7SRobert Watson * After maxnipq has been updated, propagate the change to UMA. The UMA zone 723d248c7d7SRobert Watson * max has slightly different semantics than the sysctl, for historical 724d248c7d7SRobert Watson * reasons. 725d248c7d7SRobert Watson */ 726d248c7d7SRobert Watson static void 727d248c7d7SRobert Watson maxnipq_update(void) 728d248c7d7SRobert Watson { 7298b615593SMarko Zec INIT_VNET_INET(curvnet); 730d248c7d7SRobert Watson 731d248c7d7SRobert Watson /* 732d248c7d7SRobert Watson * -1 for unlimited allocation. 733d248c7d7SRobert Watson */ 734603724d3SBjoern A. Zeeb if (V_maxnipq < 0) 735603724d3SBjoern A. Zeeb uma_zone_set_max(V_ipq_zone, 0); 736d248c7d7SRobert Watson /* 737d248c7d7SRobert Watson * Positive number for specific bound. 738d248c7d7SRobert Watson */ 739603724d3SBjoern A. Zeeb if (V_maxnipq > 0) 740603724d3SBjoern A. Zeeb uma_zone_set_max(V_ipq_zone, V_maxnipq); 741d248c7d7SRobert Watson /* 742d248c7d7SRobert Watson * Zero specifies no further fragment queue allocation -- set the 743d248c7d7SRobert Watson * bound very low, but rely on implementation elsewhere to actually 744d248c7d7SRobert Watson * prevent allocation and reclaim current queues. 745d248c7d7SRobert Watson */ 746603724d3SBjoern A. Zeeb if (V_maxnipq == 0) 747603724d3SBjoern A. Zeeb uma_zone_set_max(V_ipq_zone, 1); 748d248c7d7SRobert Watson } 749d248c7d7SRobert Watson 7504f590175SPaul Saab static void 7514f590175SPaul Saab ipq_zone_change(void *tag) 7524f590175SPaul Saab { 7538b615593SMarko Zec INIT_VNET_INET(curvnet); 7544f590175SPaul Saab 755603724d3SBjoern A. Zeeb if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) { 756603724d3SBjoern A. Zeeb V_maxnipq = nmbclusters / 32; 7574f590175SPaul Saab maxnipq_update(); 7584f590175SPaul Saab } 7594f590175SPaul Saab } 7604f590175SPaul Saab 761d248c7d7SRobert Watson static int 762d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS) 763d248c7d7SRobert Watson { 7648b615593SMarko Zec INIT_VNET_INET(curvnet); 765d248c7d7SRobert Watson int error, i; 766d248c7d7SRobert Watson 767603724d3SBjoern A. Zeeb i = V_maxnipq; 768d248c7d7SRobert Watson error = sysctl_handle_int(oidp, &i, 0, req); 769d248c7d7SRobert Watson if (error || !req->newptr) 770d248c7d7SRobert Watson return (error); 771d248c7d7SRobert Watson 772d248c7d7SRobert Watson /* 773d248c7d7SRobert Watson * XXXRW: Might be a good idea to sanity check the argument and place 774d248c7d7SRobert Watson * an extreme upper bound. 775d248c7d7SRobert Watson */ 776d248c7d7SRobert Watson if (i < -1) 777d248c7d7SRobert Watson return (EINVAL); 778603724d3SBjoern A. Zeeb V_maxnipq = i; 779d248c7d7SRobert Watson maxnipq_update(); 780d248c7d7SRobert Watson return (0); 781d248c7d7SRobert Watson } 782d248c7d7SRobert Watson 783d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW, 784d248c7d7SRobert Watson NULL, 0, sysctl_maxnipq, "I", 785d248c7d7SRobert Watson "Maximum number of IPv4 fragment reassembly queue entries"); 786d248c7d7SRobert Watson 787d248c7d7SRobert Watson /* 7888948e4baSArchie Cobbs * Take incoming datagram fragment and try to reassemble it into 789f0cada84SAndre Oppermann * whole datagram. If the argument is the first fragment or one 790f0cada84SAndre Oppermann * in between the function will return NULL and store the mbuf 791f0cada84SAndre Oppermann * in the fragment chain. If the argument is the last fragment 792f0cada84SAndre Oppermann * the packet will be reassembled and the pointer to the new 793f0cada84SAndre Oppermann * mbuf returned for further processing. Only m_tags attached 794f0cada84SAndre Oppermann * to the first packet/fragment are preserved. 795f0cada84SAndre Oppermann * The IP header is *NOT* adjusted out of iplen. 796df8bae1dSRodney W. Grimes */ 797f0cada84SAndre Oppermann struct mbuf * 798f0cada84SAndre Oppermann ip_reass(struct mbuf *m) 799df8bae1dSRodney W. Grimes { 8008b615593SMarko Zec INIT_VNET_INET(curvnet); 801f0cada84SAndre Oppermann struct ip *ip; 802f0cada84SAndre Oppermann struct mbuf *p, *q, *nq, *t; 803f0cada84SAndre Oppermann struct ipq *fp = NULL; 804f0cada84SAndre Oppermann struct ipqhead *head; 805f0cada84SAndre Oppermann int i, hlen, next; 80659dfcba4SHajimu UMEMOTO u_int8_t ecn, ecn0; 807f0cada84SAndre Oppermann u_short hash; 808df8bae1dSRodney W. Grimes 809800af1fbSMaxim Konovalov /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */ 810603724d3SBjoern A. Zeeb if (V_maxnipq == 0 || V_maxfragsperpacket == 0) { 811603724d3SBjoern A. Zeeb V_ipstat.ips_fragments++; 812603724d3SBjoern A. Zeeb V_ipstat.ips_fragdropped++; 8139d804f81SAndre Oppermann m_freem(m); 8149d804f81SAndre Oppermann return (NULL); 815f0cada84SAndre Oppermann } 8162fad1e93SSam Leffler 817f0cada84SAndre Oppermann ip = mtod(m, struct ip *); 818f0cada84SAndre Oppermann hlen = ip->ip_hl << 2; 819f0cada84SAndre Oppermann 820f0cada84SAndre Oppermann hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 821603724d3SBjoern A. Zeeb head = &V_ipq[hash]; 822f0cada84SAndre Oppermann IPQ_LOCK(); 823f0cada84SAndre Oppermann 824f0cada84SAndre Oppermann /* 825f0cada84SAndre Oppermann * Look for queue of fragments 826f0cada84SAndre Oppermann * of this datagram. 827f0cada84SAndre Oppermann */ 828f0cada84SAndre Oppermann TAILQ_FOREACH(fp, head, ipq_list) 829f0cada84SAndre Oppermann if (ip->ip_id == fp->ipq_id && 830f0cada84SAndre Oppermann ip->ip_src.s_addr == fp->ipq_src.s_addr && 831f0cada84SAndre Oppermann ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 832f0cada84SAndre Oppermann #ifdef MAC 83330d239bcSRobert Watson mac_ipq_match(m, fp) && 834f0cada84SAndre Oppermann #endif 835f0cada84SAndre Oppermann ip->ip_p == fp->ipq_p) 836f0cada84SAndre Oppermann goto found; 837f0cada84SAndre Oppermann 838f0cada84SAndre Oppermann fp = NULL; 839f0cada84SAndre Oppermann 840f0cada84SAndre Oppermann /* 841d248c7d7SRobert Watson * Attempt to trim the number of allocated fragment queues if it 842d248c7d7SRobert Watson * exceeds the administrative limit. 843f0cada84SAndre Oppermann */ 844603724d3SBjoern A. Zeeb if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) { 845f0cada84SAndre Oppermann /* 846f0cada84SAndre Oppermann * drop something from the tail of the current queue 847f0cada84SAndre Oppermann * before proceeding further 848f0cada84SAndre Oppermann */ 849f0cada84SAndre Oppermann struct ipq *q = TAILQ_LAST(head, ipqhead); 850f0cada84SAndre Oppermann if (q == NULL) { /* gak */ 851f0cada84SAndre Oppermann for (i = 0; i < IPREASS_NHASH; i++) { 852603724d3SBjoern A. Zeeb struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead); 853f0cada84SAndre Oppermann if (r) { 854ac957cd2SJulian Elischer V_ipstat.ips_fragtimeout += 855ac957cd2SJulian Elischer r->ipq_nfrags; 856603724d3SBjoern A. Zeeb ip_freef(&V_ipq[i], r); 857f0cada84SAndre Oppermann break; 858f0cada84SAndre Oppermann } 859f0cada84SAndre Oppermann } 860f0cada84SAndre Oppermann } else { 861603724d3SBjoern A. Zeeb V_ipstat.ips_fragtimeout += q->ipq_nfrags; 862f0cada84SAndre Oppermann ip_freef(head, q); 863f0cada84SAndre Oppermann } 864f0cada84SAndre Oppermann } 865f0cada84SAndre Oppermann 866f0cada84SAndre Oppermann found: 867f0cada84SAndre Oppermann /* 868f0cada84SAndre Oppermann * Adjust ip_len to not reflect header, 869f0cada84SAndre Oppermann * convert offset of this to bytes. 870f0cada84SAndre Oppermann */ 871f0cada84SAndre Oppermann ip->ip_len -= hlen; 872f0cada84SAndre Oppermann if (ip->ip_off & IP_MF) { 873f0cada84SAndre Oppermann /* 874f0cada84SAndre Oppermann * Make sure that fragments have a data length 875f0cada84SAndre Oppermann * that's a non-zero multiple of 8 bytes. 876f0cada84SAndre Oppermann */ 877f0cada84SAndre Oppermann if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { 878603724d3SBjoern A. Zeeb V_ipstat.ips_toosmall++; /* XXX */ 879f0cada84SAndre Oppermann goto dropfrag; 880f0cada84SAndre Oppermann } 881f0cada84SAndre Oppermann m->m_flags |= M_FRAG; 882f0cada84SAndre Oppermann } else 883f0cada84SAndre Oppermann m->m_flags &= ~M_FRAG; 884f0cada84SAndre Oppermann ip->ip_off <<= 3; 885f0cada84SAndre Oppermann 886f0cada84SAndre Oppermann 887f0cada84SAndre Oppermann /* 888f0cada84SAndre Oppermann * Attempt reassembly; if it succeeds, proceed. 889f0cada84SAndre Oppermann * ip_reass() will return a different mbuf. 890f0cada84SAndre Oppermann */ 891603724d3SBjoern A. Zeeb V_ipstat.ips_fragments++; 892f0cada84SAndre Oppermann m->m_pkthdr.header = ip; 893f0cada84SAndre Oppermann 894f0cada84SAndre Oppermann /* Previous ip_reass() started here. */ 895df8bae1dSRodney W. Grimes /* 896df8bae1dSRodney W. Grimes * Presence of header sizes in mbufs 897df8bae1dSRodney W. Grimes * would confuse code below. 898df8bae1dSRodney W. Grimes */ 899df8bae1dSRodney W. Grimes m->m_data += hlen; 900df8bae1dSRodney W. Grimes m->m_len -= hlen; 901df8bae1dSRodney W. Grimes 902df8bae1dSRodney W. Grimes /* 903df8bae1dSRodney W. Grimes * If first fragment to arrive, create a reassembly queue. 904df8bae1dSRodney W. Grimes */ 905042bbfa3SRobert Watson if (fp == NULL) { 906603724d3SBjoern A. Zeeb fp = uma_zalloc(V_ipq_zone, M_NOWAIT); 907d248c7d7SRobert Watson if (fp == NULL) 908df8bae1dSRodney W. Grimes goto dropfrag; 90936b0360bSRobert Watson #ifdef MAC 91030d239bcSRobert Watson if (mac_ipq_init(fp, M_NOWAIT) != 0) { 911603724d3SBjoern A. Zeeb uma_zfree(V_ipq_zone, fp); 9121d7d0bfeSPawel Jakub Dawidek fp = NULL; 9135e7ce478SRobert Watson goto dropfrag; 9145e7ce478SRobert Watson } 91530d239bcSRobert Watson mac_ipq_create(m, fp); 91636b0360bSRobert Watson #endif 917462b86feSPoul-Henning Kamp TAILQ_INSERT_HEAD(head, fp, ipq_list); 918603724d3SBjoern A. Zeeb V_nipq++; 919375386e2SMike Silbersack fp->ipq_nfrags = 1; 920df8bae1dSRodney W. Grimes fp->ipq_ttl = IPFRAGTTL; 921df8bae1dSRodney W. Grimes fp->ipq_p = ip->ip_p; 922df8bae1dSRodney W. Grimes fp->ipq_id = ip->ip_id; 9236effc713SDoug Rabson fp->ipq_src = ip->ip_src; 9246effc713SDoug Rabson fp->ipq_dst = ip->ip_dst; 925af38c68cSLuigi Rizzo fp->ipq_frags = m; 926af38c68cSLuigi Rizzo m->m_nextpkt = NULL; 927800af1fbSMaxim Konovalov goto done; 92836b0360bSRobert Watson } else { 929375386e2SMike Silbersack fp->ipq_nfrags++; 93036b0360bSRobert Watson #ifdef MAC 93130d239bcSRobert Watson mac_ipq_update(m, fp); 93236b0360bSRobert Watson #endif 933df8bae1dSRodney W. Grimes } 934df8bae1dSRodney W. Grimes 9356effc713SDoug Rabson #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) 9366effc713SDoug Rabson 937df8bae1dSRodney W. Grimes /* 93859dfcba4SHajimu UMEMOTO * Handle ECN by comparing this segment with the first one; 93959dfcba4SHajimu UMEMOTO * if CE is set, do not lose CE. 94059dfcba4SHajimu UMEMOTO * drop if CE and not-ECT are mixed for the same packet. 94159dfcba4SHajimu UMEMOTO */ 94259dfcba4SHajimu UMEMOTO ecn = ip->ip_tos & IPTOS_ECN_MASK; 94359dfcba4SHajimu UMEMOTO ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK; 94459dfcba4SHajimu UMEMOTO if (ecn == IPTOS_ECN_CE) { 94559dfcba4SHajimu UMEMOTO if (ecn0 == IPTOS_ECN_NOTECT) 94659dfcba4SHajimu UMEMOTO goto dropfrag; 94759dfcba4SHajimu UMEMOTO if (ecn0 != IPTOS_ECN_CE) 94859dfcba4SHajimu UMEMOTO GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE; 94959dfcba4SHajimu UMEMOTO } 95059dfcba4SHajimu UMEMOTO if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) 95159dfcba4SHajimu UMEMOTO goto dropfrag; 95259dfcba4SHajimu UMEMOTO 95359dfcba4SHajimu UMEMOTO /* 954df8bae1dSRodney W. Grimes * Find a segment which begins after this one does. 955df8bae1dSRodney W. Grimes */ 9566effc713SDoug Rabson for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 9576effc713SDoug Rabson if (GETIP(q)->ip_off > ip->ip_off) 958df8bae1dSRodney W. Grimes break; 959df8bae1dSRodney W. Grimes 960df8bae1dSRodney W. Grimes /* 961df8bae1dSRodney W. Grimes * If there is a preceding segment, it may provide some of 962df8bae1dSRodney W. Grimes * our data already. If so, drop the data from the incoming 963af38c68cSLuigi Rizzo * segment. If it provides all of our data, drop us, otherwise 964af38c68cSLuigi Rizzo * stick new segment in the proper place. 965db4f9cc7SJonathan Lemon * 966db4f9cc7SJonathan Lemon * If some of the data is dropped from the the preceding 967db4f9cc7SJonathan Lemon * segment, then it's checksum is invalidated. 968df8bae1dSRodney W. Grimes */ 9696effc713SDoug Rabson if (p) { 9706effc713SDoug Rabson i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; 971df8bae1dSRodney W. Grimes if (i > 0) { 972df8bae1dSRodney W. Grimes if (i >= ip->ip_len) 973df8bae1dSRodney W. Grimes goto dropfrag; 9746a800098SYoshinobu Inoue m_adj(m, i); 975db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = 0; 976df8bae1dSRodney W. Grimes ip->ip_off += i; 977df8bae1dSRodney W. Grimes ip->ip_len -= i; 978df8bae1dSRodney W. Grimes } 979af38c68cSLuigi Rizzo m->m_nextpkt = p->m_nextpkt; 980af38c68cSLuigi Rizzo p->m_nextpkt = m; 981af38c68cSLuigi Rizzo } else { 982af38c68cSLuigi Rizzo m->m_nextpkt = fp->ipq_frags; 983af38c68cSLuigi Rizzo fp->ipq_frags = m; 984df8bae1dSRodney W. Grimes } 985df8bae1dSRodney W. Grimes 986df8bae1dSRodney W. Grimes /* 987df8bae1dSRodney W. Grimes * While we overlap succeeding segments trim them or, 988df8bae1dSRodney W. Grimes * if they are completely covered, dequeue them. 989df8bae1dSRodney W. Grimes */ 9906effc713SDoug Rabson for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; 991af38c68cSLuigi Rizzo q = nq) { 992b36f5b37SMaxim Konovalov i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off; 9936effc713SDoug Rabson if (i < GETIP(q)->ip_len) { 9946effc713SDoug Rabson GETIP(q)->ip_len -= i; 9956effc713SDoug Rabson GETIP(q)->ip_off += i; 9966effc713SDoug Rabson m_adj(q, i); 997db4f9cc7SJonathan Lemon q->m_pkthdr.csum_flags = 0; 998df8bae1dSRodney W. Grimes break; 999df8bae1dSRodney W. Grimes } 10006effc713SDoug Rabson nq = q->m_nextpkt; 1001af38c68cSLuigi Rizzo m->m_nextpkt = nq; 1002603724d3SBjoern A. Zeeb V_ipstat.ips_fragdropped++; 1003375386e2SMike Silbersack fp->ipq_nfrags--; 10046effc713SDoug Rabson m_freem(q); 1005df8bae1dSRodney W. Grimes } 1006df8bae1dSRodney W. Grimes 1007df8bae1dSRodney W. Grimes /* 1008375386e2SMike Silbersack * Check for complete reassembly and perform frag per packet 1009375386e2SMike Silbersack * limiting. 1010375386e2SMike Silbersack * 1011375386e2SMike Silbersack * Frag limiting is performed here so that the nth frag has 1012375386e2SMike Silbersack * a chance to complete the packet before we drop the packet. 1013375386e2SMike Silbersack * As a result, n+1 frags are actually allowed per packet, but 1014375386e2SMike Silbersack * only n will ever be stored. (n = maxfragsperpacket.) 1015375386e2SMike Silbersack * 1016df8bae1dSRodney W. Grimes */ 10176effc713SDoug Rabson next = 0; 10186effc713SDoug Rabson for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 1019375386e2SMike Silbersack if (GETIP(q)->ip_off != next) { 1020603724d3SBjoern A. Zeeb if (fp->ipq_nfrags > V_maxfragsperpacket) { 1021603724d3SBjoern A. Zeeb V_ipstat.ips_fragdropped += fp->ipq_nfrags; 1022375386e2SMike Silbersack ip_freef(head, fp); 102399e8617dSMaxim Konovalov } 1024f0cada84SAndre Oppermann goto done; 1025375386e2SMike Silbersack } 10266effc713SDoug Rabson next += GETIP(q)->ip_len; 10276effc713SDoug Rabson } 10286effc713SDoug Rabson /* Make sure the last packet didn't have the IP_MF flag */ 1029375386e2SMike Silbersack if (p->m_flags & M_FRAG) { 1030603724d3SBjoern A. Zeeb if (fp->ipq_nfrags > V_maxfragsperpacket) { 1031603724d3SBjoern A. Zeeb V_ipstat.ips_fragdropped += fp->ipq_nfrags; 1032375386e2SMike Silbersack ip_freef(head, fp); 103399e8617dSMaxim Konovalov } 1034f0cada84SAndre Oppermann goto done; 1035375386e2SMike Silbersack } 1036df8bae1dSRodney W. Grimes 1037df8bae1dSRodney W. Grimes /* 1038430d30d8SBill Fenner * Reassembly is complete. Make sure the packet is a sane size. 1039430d30d8SBill Fenner */ 10406effc713SDoug Rabson q = fp->ipq_frags; 10416effc713SDoug Rabson ip = GETIP(q); 104253be11f6SPoul-Henning Kamp if (next + (ip->ip_hl << 2) > IP_MAXPACKET) { 1043603724d3SBjoern A. Zeeb V_ipstat.ips_toolong++; 1044603724d3SBjoern A. Zeeb V_ipstat.ips_fragdropped += fp->ipq_nfrags; 1045462b86feSPoul-Henning Kamp ip_freef(head, fp); 1046f0cada84SAndre Oppermann goto done; 1047430d30d8SBill Fenner } 1048430d30d8SBill Fenner 1049430d30d8SBill Fenner /* 1050430d30d8SBill Fenner * Concatenate fragments. 1051df8bae1dSRodney W. Grimes */ 10526effc713SDoug Rabson m = q; 1053df8bae1dSRodney W. Grimes t = m->m_next; 105402410549SRobert Watson m->m_next = NULL; 1055df8bae1dSRodney W. Grimes m_cat(m, t); 10566effc713SDoug Rabson nq = q->m_nextpkt; 105702410549SRobert Watson q->m_nextpkt = NULL; 10586effc713SDoug Rabson for (q = nq; q != NULL; q = nq) { 10596effc713SDoug Rabson nq = q->m_nextpkt; 1060945aa40dSDoug Rabson q->m_nextpkt = NULL; 1061db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; 1062db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; 1063a8db1d93SJonathan Lemon m_cat(m, q); 1064df8bae1dSRodney W. Grimes } 10656edb555dSOleg Bulyzhin /* 10666edb555dSOleg Bulyzhin * In order to do checksumming faster we do 'end-around carry' here 10676edb555dSOleg Bulyzhin * (and not in for{} loop), though it implies we are not going to 10686edb555dSOleg Bulyzhin * reassemble more than 64k fragments. 10696edb555dSOleg Bulyzhin */ 10706edb555dSOleg Bulyzhin m->m_pkthdr.csum_data = 10716edb555dSOleg Bulyzhin (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16); 107236b0360bSRobert Watson #ifdef MAC 107330d239bcSRobert Watson mac_ipq_reassemble(fp, m); 107430d239bcSRobert Watson mac_ipq_destroy(fp); 107536b0360bSRobert Watson #endif 1076df8bae1dSRodney W. Grimes 1077df8bae1dSRodney W. Grimes /* 1078f0cada84SAndre Oppermann * Create header for new ip packet by modifying header of first 1079f0cada84SAndre Oppermann * packet; dequeue and discard fragment reassembly header. 1080df8bae1dSRodney W. Grimes * Make header visible. 1081df8bae1dSRodney W. Grimes */ 1082f0cada84SAndre Oppermann ip->ip_len = (ip->ip_hl << 2) + next; 10836effc713SDoug Rabson ip->ip_src = fp->ipq_src; 10846effc713SDoug Rabson ip->ip_dst = fp->ipq_dst; 1085462b86feSPoul-Henning Kamp TAILQ_REMOVE(head, fp, ipq_list); 1086603724d3SBjoern A. Zeeb V_nipq--; 1087603724d3SBjoern A. Zeeb uma_zfree(V_ipq_zone, fp); 108853be11f6SPoul-Henning Kamp m->m_len += (ip->ip_hl << 2); 108953be11f6SPoul-Henning Kamp m->m_data -= (ip->ip_hl << 2); 1090df8bae1dSRodney W. Grimes /* some debugging cruft by sklower, below, will go away soon */ 1091a5554bf0SPoul-Henning Kamp if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */ 1092a5554bf0SPoul-Henning Kamp m_fixhdr(m); 1093603724d3SBjoern A. Zeeb V_ipstat.ips_reassembled++; 1094f0cada84SAndre Oppermann IPQ_UNLOCK(); 10956a800098SYoshinobu Inoue return (m); 1096df8bae1dSRodney W. Grimes 1097df8bae1dSRodney W. Grimes dropfrag: 1098603724d3SBjoern A. Zeeb V_ipstat.ips_fragdropped++; 1099042bbfa3SRobert Watson if (fp != NULL) 1100375386e2SMike Silbersack fp->ipq_nfrags--; 1101df8bae1dSRodney W. Grimes m_freem(m); 1102f0cada84SAndre Oppermann done: 1103f0cada84SAndre Oppermann IPQ_UNLOCK(); 1104f0cada84SAndre Oppermann return (NULL); 11056effc713SDoug Rabson 11066effc713SDoug Rabson #undef GETIP 1107df8bae1dSRodney W. Grimes } 1108df8bae1dSRodney W. Grimes 1109df8bae1dSRodney W. Grimes /* 1110df8bae1dSRodney W. Grimes * Free a fragment reassembly header and all 1111df8bae1dSRodney W. Grimes * associated datagrams. 1112df8bae1dSRodney W. Grimes */ 11130312fbe9SPoul-Henning Kamp static void 1114f2565d68SRobert Watson ip_freef(struct ipqhead *fhp, struct ipq *fp) 1115df8bae1dSRodney W. Grimes { 11168b615593SMarko Zec INIT_VNET_INET(curvnet); 1117f2565d68SRobert Watson struct mbuf *q; 1118df8bae1dSRodney W. Grimes 11192fad1e93SSam Leffler IPQ_LOCK_ASSERT(); 11202fad1e93SSam Leffler 11216effc713SDoug Rabson while (fp->ipq_frags) { 11226effc713SDoug Rabson q = fp->ipq_frags; 11236effc713SDoug Rabson fp->ipq_frags = q->m_nextpkt; 11246effc713SDoug Rabson m_freem(q); 1125df8bae1dSRodney W. Grimes } 1126462b86feSPoul-Henning Kamp TAILQ_REMOVE(fhp, fp, ipq_list); 1127603724d3SBjoern A. Zeeb uma_zfree(V_ipq_zone, fp); 1128603724d3SBjoern A. Zeeb V_nipq--; 1129df8bae1dSRodney W. Grimes } 1130df8bae1dSRodney W. Grimes 1131df8bae1dSRodney W. Grimes /* 1132df8bae1dSRodney W. Grimes * IP timer processing; 1133df8bae1dSRodney W. Grimes * if a timer expires on a reassembly 1134df8bae1dSRodney W. Grimes * queue, discard it. 1135df8bae1dSRodney W. Grimes */ 1136df8bae1dSRodney W. Grimes void 1137f2565d68SRobert Watson ip_slowtimo(void) 1138df8bae1dSRodney W. Grimes { 11398b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 1140f2565d68SRobert Watson struct ipq *fp; 1141194a213eSAndrey A. Chernov int i; 1142df8bae1dSRodney W. Grimes 11432fad1e93SSam Leffler IPQ_LOCK(); 11448b615593SMarko Zec VNET_LIST_RLOCK(); 11458b615593SMarko Zec VNET_FOREACH(vnet_iter) { 11468b615593SMarko Zec CURVNET_SET(vnet_iter); 11478b615593SMarko Zec INIT_VNET_INET(vnet_iter); 1148194a213eSAndrey A. Chernov for (i = 0; i < IPREASS_NHASH; i++) { 1149603724d3SBjoern A. Zeeb for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) { 1150462b86feSPoul-Henning Kamp struct ipq *fpp; 1151462b86feSPoul-Henning Kamp 1152462b86feSPoul-Henning Kamp fpp = fp; 1153462b86feSPoul-Henning Kamp fp = TAILQ_NEXT(fp, ipq_list); 1154462b86feSPoul-Henning Kamp if(--fpp->ipq_ttl == 0) { 11558b615593SMarko Zec V_ipstat.ips_fragtimeout += 11568b615593SMarko Zec fpp->ipq_nfrags; 1157603724d3SBjoern A. Zeeb ip_freef(&V_ipq[i], fpp); 1158df8bae1dSRodney W. Grimes } 1159df8bae1dSRodney W. Grimes } 1160194a213eSAndrey A. Chernov } 1161690a6055SJesper Skriver /* 1162690a6055SJesper Skriver * If we are over the maximum number of fragments 1163690a6055SJesper Skriver * (due to the limit being lowered), drain off 1164690a6055SJesper Skriver * enough to get down to the new limit. 1165690a6055SJesper Skriver */ 1166603724d3SBjoern A. Zeeb if (V_maxnipq >= 0 && V_nipq > V_maxnipq) { 1167690a6055SJesper Skriver for (i = 0; i < IPREASS_NHASH; i++) { 11688b615593SMarko Zec while (V_nipq > V_maxnipq && 11698b615593SMarko Zec !TAILQ_EMPTY(&V_ipq[i])) { 1170603724d3SBjoern A. Zeeb V_ipstat.ips_fragdropped += 1171603724d3SBjoern A. Zeeb TAILQ_FIRST(&V_ipq[i])->ipq_nfrags; 11728b615593SMarko Zec ip_freef(&V_ipq[i], 11738b615593SMarko Zec TAILQ_FIRST(&V_ipq[i])); 1174690a6055SJesper Skriver } 1175690a6055SJesper Skriver } 1176690a6055SJesper Skriver } 11778b615593SMarko Zec CURVNET_RESTORE(); 11788b615593SMarko Zec } 11798b615593SMarko Zec VNET_LIST_RUNLOCK(); 11802fad1e93SSam Leffler IPQ_UNLOCK(); 1181df8bae1dSRodney W. Grimes } 1182df8bae1dSRodney W. Grimes 1183df8bae1dSRodney W. Grimes /* 1184df8bae1dSRodney W. Grimes * Drain off all datagram fragments. 1185df8bae1dSRodney W. Grimes */ 1186df8bae1dSRodney W. Grimes void 1187f2565d68SRobert Watson ip_drain(void) 1188df8bae1dSRodney W. Grimes { 11898b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 1190194a213eSAndrey A. Chernov int i; 1191ce29ab3aSGarrett Wollman 11922fad1e93SSam Leffler IPQ_LOCK(); 11938b615593SMarko Zec VNET_LIST_RLOCK(); 11948b615593SMarko Zec VNET_FOREACH(vnet_iter) { 11958b615593SMarko Zec CURVNET_SET(vnet_iter); 11968b615593SMarko Zec INIT_VNET_INET(vnet_iter); 1197194a213eSAndrey A. Chernov for (i = 0; i < IPREASS_NHASH; i++) { 1198603724d3SBjoern A. Zeeb while(!TAILQ_EMPTY(&V_ipq[i])) { 1199603724d3SBjoern A. Zeeb V_ipstat.ips_fragdropped += 1200603724d3SBjoern A. Zeeb TAILQ_FIRST(&V_ipq[i])->ipq_nfrags; 1201603724d3SBjoern A. Zeeb ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i])); 1202194a213eSAndrey A. Chernov } 1203194a213eSAndrey A. Chernov } 12048b615593SMarko Zec CURVNET_RESTORE(); 12058b615593SMarko Zec } 12068b615593SMarko Zec VNET_LIST_RUNLOCK(); 12072fad1e93SSam Leffler IPQ_UNLOCK(); 1208ce29ab3aSGarrett Wollman in_rtqdrain(); 1209df8bae1dSRodney W. Grimes } 1210df8bae1dSRodney W. Grimes 1211df8bae1dSRodney W. Grimes /* 1212de38924dSAndre Oppermann * The protocol to be inserted into ip_protox[] must be already registered 1213de38924dSAndre Oppermann * in inetsw[], either statically or through pf_proto_register(). 1214de38924dSAndre Oppermann */ 1215de38924dSAndre Oppermann int 1216de38924dSAndre Oppermann ipproto_register(u_char ipproto) 1217de38924dSAndre Oppermann { 1218de38924dSAndre Oppermann struct protosw *pr; 1219de38924dSAndre Oppermann 1220de38924dSAndre Oppermann /* Sanity checks. */ 1221de38924dSAndre Oppermann if (ipproto == 0) 1222de38924dSAndre Oppermann return (EPROTONOSUPPORT); 1223de38924dSAndre Oppermann 1224de38924dSAndre Oppermann /* 1225de38924dSAndre Oppermann * The protocol slot must not be occupied by another protocol 1226de38924dSAndre Oppermann * already. An index pointing to IPPROTO_RAW is unused. 1227de38924dSAndre Oppermann */ 1228de38924dSAndre Oppermann pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 1229de38924dSAndre Oppermann if (pr == NULL) 1230de38924dSAndre Oppermann return (EPFNOSUPPORT); 1231de38924dSAndre Oppermann if (ip_protox[ipproto] != pr - inetsw) /* IPPROTO_RAW */ 1232de38924dSAndre Oppermann return (EEXIST); 1233de38924dSAndre Oppermann 1234de38924dSAndre Oppermann /* Find the protocol position in inetsw[] and set the index. */ 1235de38924dSAndre Oppermann for (pr = inetdomain.dom_protosw; 1236de38924dSAndre Oppermann pr < inetdomain.dom_protoswNPROTOSW; pr++) { 1237de38924dSAndre Oppermann if (pr->pr_domain->dom_family == PF_INET && 1238de38924dSAndre Oppermann pr->pr_protocol && pr->pr_protocol == ipproto) { 1239de38924dSAndre Oppermann /* Be careful to only index valid IP protocols. */ 1240db77984cSSam Leffler if (pr->pr_protocol < IPPROTO_MAX) { 1241de38924dSAndre Oppermann ip_protox[pr->pr_protocol] = pr - inetsw; 1242de38924dSAndre Oppermann return (0); 1243de38924dSAndre Oppermann } else 1244de38924dSAndre Oppermann return (EINVAL); 1245de38924dSAndre Oppermann } 1246de38924dSAndre Oppermann } 1247de38924dSAndre Oppermann return (EPROTONOSUPPORT); 1248de38924dSAndre Oppermann } 1249de38924dSAndre Oppermann 1250de38924dSAndre Oppermann int 1251de38924dSAndre Oppermann ipproto_unregister(u_char ipproto) 1252de38924dSAndre Oppermann { 1253de38924dSAndre Oppermann struct protosw *pr; 1254de38924dSAndre Oppermann 1255de38924dSAndre Oppermann /* Sanity checks. */ 1256de38924dSAndre Oppermann if (ipproto == 0) 1257de38924dSAndre Oppermann return (EPROTONOSUPPORT); 1258de38924dSAndre Oppermann 1259de38924dSAndre Oppermann /* Check if the protocol was indeed registered. */ 1260de38924dSAndre Oppermann pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 1261de38924dSAndre Oppermann if (pr == NULL) 1262de38924dSAndre Oppermann return (EPFNOSUPPORT); 1263de38924dSAndre Oppermann if (ip_protox[ipproto] == pr - inetsw) /* IPPROTO_RAW */ 1264de38924dSAndre Oppermann return (ENOENT); 1265de38924dSAndre Oppermann 1266de38924dSAndre Oppermann /* Reset the protocol slot to IPPROTO_RAW. */ 1267de38924dSAndre Oppermann ip_protox[ipproto] = pr - inetsw; 1268de38924dSAndre Oppermann return (0); 1269de38924dSAndre Oppermann } 1270de38924dSAndre Oppermann 1271df8bae1dSRodney W. Grimes /* 1272df8bae1dSRodney W. Grimes * Given address of next destination (final or next hop), 1273df8bae1dSRodney W. Grimes * return internet address info of interface to be used to get there. 1274df8bae1dSRodney W. Grimes */ 1275bd714208SRuslan Ermilov struct in_ifaddr * 12768b07e49aSJulian Elischer ip_rtaddr(struct in_addr dst, u_int fibnum) 1277df8bae1dSRodney W. Grimes { 127897d8d152SAndre Oppermann struct route sro; 127902c1c707SAndre Oppermann struct sockaddr_in *sin; 128002c1c707SAndre Oppermann struct in_ifaddr *ifa; 1281df8bae1dSRodney W. Grimes 12820cfbbe3bSAndre Oppermann bzero(&sro, sizeof(sro)); 128397d8d152SAndre Oppermann sin = (struct sockaddr_in *)&sro.ro_dst; 1284df8bae1dSRodney W. Grimes sin->sin_family = AF_INET; 1285df8bae1dSRodney W. Grimes sin->sin_len = sizeof(*sin); 1286df8bae1dSRodney W. Grimes sin->sin_addr = dst; 12876e6b3f7cSQing Li in_rtalloc_ign(&sro, 0, fibnum); 1288df8bae1dSRodney W. Grimes 128997d8d152SAndre Oppermann if (sro.ro_rt == NULL) 129002410549SRobert Watson return (NULL); 129102c1c707SAndre Oppermann 129297d8d152SAndre Oppermann ifa = ifatoia(sro.ro_rt->rt_ifa); 129397d8d152SAndre Oppermann RTFREE(sro.ro_rt); 129402410549SRobert Watson return (ifa); 1295df8bae1dSRodney W. Grimes } 1296df8bae1dSRodney W. Grimes 1297df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = { 1298df8bae1dSRodney W. Grimes 0, 0, 0, 0, 1299df8bae1dSRodney W. Grimes 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1300df8bae1dSRodney W. Grimes EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1301df8bae1dSRodney W. Grimes EMSGSIZE, EHOSTUNREACH, 0, 0, 1302fcaf9f91SMike Silbersack 0, 0, EHOSTUNREACH, 0, 13033b8123b7SJesper Skriver ENOPROTOOPT, ECONNREFUSED 1304df8bae1dSRodney W. Grimes }; 1305df8bae1dSRodney W. Grimes 1306df8bae1dSRodney W. Grimes /* 1307df8bae1dSRodney W. Grimes * Forward a packet. If some error occurs return the sender 1308df8bae1dSRodney W. Grimes * an icmp packet. Note we can't always generate a meaningful 1309df8bae1dSRodney W. Grimes * icmp message because icmp doesn't have a large enough repertoire 1310df8bae1dSRodney W. Grimes * of codes and types. 1311df8bae1dSRodney W. Grimes * 1312df8bae1dSRodney W. Grimes * If not forwarding, just drop the packet. This could be confusing 1313df8bae1dSRodney W. Grimes * if ipforwarding was zero but some routing protocol was advancing 1314df8bae1dSRodney W. Grimes * us as a gateway to somewhere. However, we must let the routing 1315df8bae1dSRodney W. Grimes * protocol deal with that. 1316df8bae1dSRodney W. Grimes * 1317df8bae1dSRodney W. Grimes * The srcrt parameter indicates whether the packet is being forwarded 1318df8bae1dSRodney W. Grimes * via a source route. 1319df8bae1dSRodney W. Grimes */ 13209b932e9eSAndre Oppermann void 13219b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt) 1322df8bae1dSRodney W. Grimes { 13238b615593SMarko Zec INIT_VNET_INET(curvnet); 13242b25acc1SLuigi Rizzo struct ip *ip = mtod(m, struct ip *); 13259b932e9eSAndre Oppermann struct in_ifaddr *ia = NULL; 1326df8bae1dSRodney W. Grimes struct mbuf *mcopy; 13279b932e9eSAndre Oppermann struct in_addr dest; 1328b835b6feSBjoern A. Zeeb struct route ro; 1329c773494eSAndre Oppermann int error, type = 0, code = 0, mtu = 0; 13303efc3014SJulian Elischer 13319b932e9eSAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1332603724d3SBjoern A. Zeeb V_ipstat.ips_cantforward++; 1333df8bae1dSRodney W. Grimes m_freem(m); 1334df8bae1dSRodney W. Grimes return; 1335df8bae1dSRodney W. Grimes } 13361b968362SDag-Erling Smørgrav #ifdef IPSTEALTH 1337603724d3SBjoern A. Zeeb if (!V_ipstealth) { 13381b968362SDag-Erling Smørgrav #endif 1339df8bae1dSRodney W. Grimes if (ip->ip_ttl <= IPTTLDEC) { 13401b968362SDag-Erling Smørgrav icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 134102c1c707SAndre Oppermann 0, 0); 1342df8bae1dSRodney W. Grimes return; 1343df8bae1dSRodney W. Grimes } 13441b968362SDag-Erling Smørgrav #ifdef IPSTEALTH 13451b968362SDag-Erling Smørgrav } 13461b968362SDag-Erling Smørgrav #endif 1347df8bae1dSRodney W. Grimes 13488b07e49aSJulian Elischer ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m)); 1349d23d475fSGuido van Rooij if (!srcrt && ia == NULL) { 135002c1c707SAndre Oppermann icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 1351df8bae1dSRodney W. Grimes return; 135202c1c707SAndre Oppermann } 1353df8bae1dSRodney W. Grimes 1354df8bae1dSRodney W. Grimes /* 1355bfef7ed4SIan Dowse * Save the IP header and at most 8 bytes of the payload, 1356bfef7ed4SIan Dowse * in case we need to generate an ICMP message to the src. 1357bfef7ed4SIan Dowse * 13584d2e3692SLuigi Rizzo * XXX this can be optimized a lot by saving the data in a local 13594d2e3692SLuigi Rizzo * buffer on the stack (72 bytes at most), and only allocating the 13604d2e3692SLuigi Rizzo * mbuf if really necessary. The vast majority of the packets 13614d2e3692SLuigi Rizzo * are forwarded without having to send an ICMP back (either 13624d2e3692SLuigi Rizzo * because unnecessary, or because rate limited), so we are 13634d2e3692SLuigi Rizzo * really we are wasting a lot of work here. 13644d2e3692SLuigi Rizzo * 1365bfef7ed4SIan Dowse * We don't use m_copy() because it might return a reference 1366bfef7ed4SIan Dowse * to a shared cluster. Both this function and ip_output() 1367bfef7ed4SIan Dowse * assume exclusive access to the IP header in `m', so any 1368bfef7ed4SIan Dowse * data in a cluster may change before we reach icmp_error(). 1369df8bae1dSRodney W. Grimes */ 1370780b2f69SAndre Oppermann MGETHDR(mcopy, M_DONTWAIT, m->m_type); 1371a163d034SWarner Losh if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) { 13729967cafcSSam Leffler /* 13739967cafcSSam Leffler * It's probably ok if the pkthdr dup fails (because 13749967cafcSSam Leffler * the deep copy of the tag chain failed), but for now 13759967cafcSSam Leffler * be conservative and just discard the copy since 13769967cafcSSam Leffler * code below may some day want the tags. 13779967cafcSSam Leffler */ 13789967cafcSSam Leffler m_free(mcopy); 13799967cafcSSam Leffler mcopy = NULL; 13809967cafcSSam Leffler } 1381bfef7ed4SIan Dowse if (mcopy != NULL) { 1382780b2f69SAndre Oppermann mcopy->m_len = min(ip->ip_len, M_TRAILINGSPACE(mcopy)); 1383e6b0a570SBruce M Simpson mcopy->m_pkthdr.len = mcopy->m_len; 1384bfef7ed4SIan Dowse m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t)); 1385bfef7ed4SIan Dowse } 138604287599SRuslan Ermilov 138704287599SRuslan Ermilov #ifdef IPSTEALTH 1388603724d3SBjoern A. Zeeb if (!V_ipstealth) { 138904287599SRuslan Ermilov #endif 139004287599SRuslan Ermilov ip->ip_ttl -= IPTTLDEC; 139104287599SRuslan Ermilov #ifdef IPSTEALTH 139204287599SRuslan Ermilov } 139304287599SRuslan Ermilov #endif 1394df8bae1dSRodney W. Grimes 1395df8bae1dSRodney W. Grimes /* 1396df8bae1dSRodney W. Grimes * If forwarding packet using same interface that it came in on, 1397df8bae1dSRodney W. Grimes * perhaps should send a redirect to sender to shortcut a hop. 1398df8bae1dSRodney W. Grimes * Only send redirect if source is sending directly to us, 1399df8bae1dSRodney W. Grimes * and if packet was not source routed (or has any options). 1400df8bae1dSRodney W. Grimes * Also, don't send redirect if forwarding using a default route 1401df8bae1dSRodney W. Grimes * or a route modified by a redirect. 1402df8bae1dSRodney W. Grimes */ 14039b932e9eSAndre Oppermann dest.s_addr = 0; 1404603724d3SBjoern A. Zeeb if (!srcrt && V_ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) { 140502c1c707SAndre Oppermann struct sockaddr_in *sin; 140602c1c707SAndre Oppermann struct rtentry *rt; 140702c1c707SAndre Oppermann 14080cfbbe3bSAndre Oppermann bzero(&ro, sizeof(ro)); 140902c1c707SAndre Oppermann sin = (struct sockaddr_in *)&ro.ro_dst; 141002c1c707SAndre Oppermann sin->sin_family = AF_INET; 141102c1c707SAndre Oppermann sin->sin_len = sizeof(*sin); 14129b932e9eSAndre Oppermann sin->sin_addr = ip->ip_dst; 14136e6b3f7cSQing Li in_rtalloc_ign(&ro, 0, M_GETFIB(m)); 141402c1c707SAndre Oppermann 141502c1c707SAndre Oppermann rt = ro.ro_rt; 141602c1c707SAndre Oppermann 141702c1c707SAndre Oppermann if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 14189b932e9eSAndre Oppermann satosin(rt_key(rt))->sin_addr.s_addr != 0) { 1419df8bae1dSRodney W. Grimes #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1420df8bae1dSRodney W. Grimes u_long src = ntohl(ip->ip_src.s_addr); 1421df8bae1dSRodney W. Grimes 1422df8bae1dSRodney W. Grimes if (RTA(rt) && 1423df8bae1dSRodney W. Grimes (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1424df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_GATEWAY) 14259b932e9eSAndre Oppermann dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr; 1426df8bae1dSRodney W. Grimes else 14279b932e9eSAndre Oppermann dest.s_addr = ip->ip_dst.s_addr; 1428df8bae1dSRodney W. Grimes /* Router requirements says to only send host redirects */ 1429df8bae1dSRodney W. Grimes type = ICMP_REDIRECT; 1430df8bae1dSRodney W. Grimes code = ICMP_REDIRECT_HOST; 1431df8bae1dSRodney W. Grimes } 1432df8bae1dSRodney W. Grimes } 143302c1c707SAndre Oppermann if (rt) 143402c1c707SAndre Oppermann RTFREE(rt); 143502c1c707SAndre Oppermann } 1436df8bae1dSRodney W. Grimes 1437b835b6feSBjoern A. Zeeb /* 1438b835b6feSBjoern A. Zeeb * Try to cache the route MTU from ip_output so we can consider it for 1439b835b6feSBjoern A. Zeeb * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191. 1440b835b6feSBjoern A. Zeeb */ 1441b835b6feSBjoern A. Zeeb bzero(&ro, sizeof(ro)); 1442b835b6feSBjoern A. Zeeb 1443b835b6feSBjoern A. Zeeb error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL); 1444b835b6feSBjoern A. Zeeb 1445b835b6feSBjoern A. Zeeb if (error == EMSGSIZE && ro.ro_rt) 1446b835b6feSBjoern A. Zeeb mtu = ro.ro_rt->rt_rmx.rmx_mtu; 1447b835b6feSBjoern A. Zeeb if (ro.ro_rt) 1448b835b6feSBjoern A. Zeeb RTFREE(ro.ro_rt); 1449b835b6feSBjoern A. Zeeb 1450df8bae1dSRodney W. Grimes if (error) 1451603724d3SBjoern A. Zeeb V_ipstat.ips_cantforward++; 1452df8bae1dSRodney W. Grimes else { 1453603724d3SBjoern A. Zeeb V_ipstat.ips_forward++; 1454df8bae1dSRodney W. Grimes if (type) 1455603724d3SBjoern A. Zeeb V_ipstat.ips_redirectsent++; 1456df8bae1dSRodney W. Grimes else { 14579188b4a1SAndre Oppermann if (mcopy) 1458df8bae1dSRodney W. Grimes m_freem(mcopy); 1459df8bae1dSRodney W. Grimes return; 1460df8bae1dSRodney W. Grimes } 1461df8bae1dSRodney W. Grimes } 1462df8bae1dSRodney W. Grimes if (mcopy == NULL) 1463df8bae1dSRodney W. Grimes return; 1464df8bae1dSRodney W. Grimes 1465df8bae1dSRodney W. Grimes switch (error) { 1466df8bae1dSRodney W. Grimes 1467df8bae1dSRodney W. Grimes case 0: /* forwarded, but need redirect */ 1468df8bae1dSRodney W. Grimes /* type, code set above */ 1469df8bae1dSRodney W. Grimes break; 1470df8bae1dSRodney W. Grimes 1471df8bae1dSRodney W. Grimes case ENETUNREACH: /* shouldn't happen, checked above */ 1472df8bae1dSRodney W. Grimes case EHOSTUNREACH: 1473df8bae1dSRodney W. Grimes case ENETDOWN: 1474df8bae1dSRodney W. Grimes case EHOSTDOWN: 1475df8bae1dSRodney W. Grimes default: 1476df8bae1dSRodney W. Grimes type = ICMP_UNREACH; 1477df8bae1dSRodney W. Grimes code = ICMP_UNREACH_HOST; 1478df8bae1dSRodney W. Grimes break; 1479df8bae1dSRodney W. Grimes 1480df8bae1dSRodney W. Grimes case EMSGSIZE: 1481df8bae1dSRodney W. Grimes type = ICMP_UNREACH; 1482df8bae1dSRodney W. Grimes code = ICMP_UNREACH_NEEDFRAG; 14831dfcf0d2SAndre Oppermann 1484b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 1485b835b6feSBjoern A. Zeeb /* 1486b835b6feSBjoern A. Zeeb * If IPsec is configured for this path, 1487b835b6feSBjoern A. Zeeb * override any possibly mtu value set by ip_output. 1488b835b6feSBjoern A. Zeeb */ 1489b835b6feSBjoern A. Zeeb mtu = ip_ipsec_mtu(m, mtu); 1490b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 14919b932e9eSAndre Oppermann /* 1492b835b6feSBjoern A. Zeeb * If the MTU was set before make sure we are below the 1493b835b6feSBjoern A. Zeeb * interface MTU. 1494ab48768bSAndre Oppermann * If the MTU wasn't set before use the interface mtu or 1495ab48768bSAndre Oppermann * fall back to the next smaller mtu step compared to the 1496ab48768bSAndre Oppermann * current packet size. 14979b932e9eSAndre Oppermann */ 1498b835b6feSBjoern A. Zeeb if (mtu != 0) { 1499b835b6feSBjoern A. Zeeb if (ia != NULL) 1500b835b6feSBjoern A. Zeeb mtu = min(mtu, ia->ia_ifp->if_mtu); 1501b835b6feSBjoern A. Zeeb } else { 1502ab48768bSAndre Oppermann if (ia != NULL) 1503c773494eSAndre Oppermann mtu = ia->ia_ifp->if_mtu; 1504ab48768bSAndre Oppermann else 1505ab48768bSAndre Oppermann mtu = ip_next_mtu(ip->ip_len, 0); 1506ab48768bSAndre Oppermann } 1507603724d3SBjoern A. Zeeb V_ipstat.ips_cantfrag++; 1508df8bae1dSRodney W. Grimes break; 1509df8bae1dSRodney W. Grimes 1510df8bae1dSRodney W. Grimes case ENOBUFS: 1511df285b3dSMike Silbersack /* 1512df285b3dSMike Silbersack * A router should not generate ICMP_SOURCEQUENCH as 1513df285b3dSMike Silbersack * required in RFC1812 Requirements for IP Version 4 Routers. 1514df285b3dSMike Silbersack * Source quench could be a big problem under DoS attacks, 1515df285b3dSMike Silbersack * or if the underlying interface is rate-limited. 1516df285b3dSMike Silbersack * Those who need source quench packets may re-enable them 1517df285b3dSMike Silbersack * via the net.inet.ip.sendsourcequench sysctl. 1518df285b3dSMike Silbersack */ 1519603724d3SBjoern A. Zeeb if (V_ip_sendsourcequench == 0) { 1520df285b3dSMike Silbersack m_freem(mcopy); 1521df285b3dSMike Silbersack return; 1522df285b3dSMike Silbersack } else { 1523df8bae1dSRodney W. Grimes type = ICMP_SOURCEQUENCH; 1524df8bae1dSRodney W. Grimes code = 0; 1525df285b3dSMike Silbersack } 1526df8bae1dSRodney W. Grimes break; 15273a06e3e0SRuslan Ermilov 15283a06e3e0SRuslan Ermilov case EACCES: /* ipfw denied packet */ 15293a06e3e0SRuslan Ermilov m_freem(mcopy); 15303a06e3e0SRuslan Ermilov return; 1531df8bae1dSRodney W. Grimes } 1532c773494eSAndre Oppermann icmp_error(mcopy, type, code, dest.s_addr, mtu); 1533df8bae1dSRodney W. Grimes } 1534df8bae1dSRodney W. Grimes 153582c23ebaSBill Fenner void 1536f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip, 1537f2565d68SRobert Watson struct mbuf *m) 153882c23ebaSBill Fenner { 15398b615593SMarko Zec INIT_VNET_NET(inp->inp_vnet); 15408b615593SMarko Zec 1541be8a62e8SPoul-Henning Kamp if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) { 1542be8a62e8SPoul-Henning Kamp struct bintime bt; 1543be8a62e8SPoul-Henning Kamp 1544be8a62e8SPoul-Henning Kamp bintime(&bt); 1545be8a62e8SPoul-Henning Kamp if (inp->inp_socket->so_options & SO_BINTIME) { 1546be8a62e8SPoul-Henning Kamp *mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt), 1547be8a62e8SPoul-Henning Kamp SCM_BINTIME, SOL_SOCKET); 1548be8a62e8SPoul-Henning Kamp if (*mp) 1549be8a62e8SPoul-Henning Kamp mp = &(*mp)->m_next; 1550be8a62e8SPoul-Henning Kamp } 155182c23ebaSBill Fenner if (inp->inp_socket->so_options & SO_TIMESTAMP) { 155282c23ebaSBill Fenner struct timeval tv; 155382c23ebaSBill Fenner 1554be8a62e8SPoul-Henning Kamp bintime2timeval(&bt, &tv); 155582c23ebaSBill Fenner *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 155682c23ebaSBill Fenner SCM_TIMESTAMP, SOL_SOCKET); 155782c23ebaSBill Fenner if (*mp) 155882c23ebaSBill Fenner mp = &(*mp)->m_next; 15594cc20ab1SSeigo Tanimura } 1560be8a62e8SPoul-Henning Kamp } 156182c23ebaSBill Fenner if (inp->inp_flags & INP_RECVDSTADDR) { 156282c23ebaSBill Fenner *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 156382c23ebaSBill Fenner sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 156482c23ebaSBill Fenner if (*mp) 156582c23ebaSBill Fenner mp = &(*mp)->m_next; 156682c23ebaSBill Fenner } 15674957466bSMatthew N. Dodd if (inp->inp_flags & INP_RECVTTL) { 15684957466bSMatthew N. Dodd *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl, 15694957466bSMatthew N. Dodd sizeof(u_char), IP_RECVTTL, IPPROTO_IP); 15704957466bSMatthew N. Dodd if (*mp) 15714957466bSMatthew N. Dodd mp = &(*mp)->m_next; 15724957466bSMatthew N. Dodd } 157382c23ebaSBill Fenner #ifdef notyet 157482c23ebaSBill Fenner /* XXX 157582c23ebaSBill Fenner * Moving these out of udp_input() made them even more broken 157682c23ebaSBill Fenner * than they already were. 157782c23ebaSBill Fenner */ 157882c23ebaSBill Fenner /* options were tossed already */ 157982c23ebaSBill Fenner if (inp->inp_flags & INP_RECVOPTS) { 158082c23ebaSBill Fenner *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 158182c23ebaSBill Fenner sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 158282c23ebaSBill Fenner if (*mp) 158382c23ebaSBill Fenner mp = &(*mp)->m_next; 158482c23ebaSBill Fenner } 158582c23ebaSBill Fenner /* ip_srcroute doesn't do what we want here, need to fix */ 158682c23ebaSBill Fenner if (inp->inp_flags & INP_RECVRETOPTS) { 1587e0982661SAndre Oppermann *mp = sbcreatecontrol((caddr_t) ip_srcroute(m), 158882c23ebaSBill Fenner sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 158982c23ebaSBill Fenner if (*mp) 159082c23ebaSBill Fenner mp = &(*mp)->m_next; 159182c23ebaSBill Fenner } 159282c23ebaSBill Fenner #endif 159382c23ebaSBill Fenner if (inp->inp_flags & INP_RECVIF) { 1594d314ad7bSJulian Elischer struct ifnet *ifp; 1595d314ad7bSJulian Elischer struct sdlbuf { 159682c23ebaSBill Fenner struct sockaddr_dl sdl; 1597d314ad7bSJulian Elischer u_char pad[32]; 1598d314ad7bSJulian Elischer } sdlbuf; 1599d314ad7bSJulian Elischer struct sockaddr_dl *sdp; 1600d314ad7bSJulian Elischer struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 160182c23ebaSBill Fenner 1602d314ad7bSJulian Elischer if (((ifp = m->m_pkthdr.rcvif)) 1603603724d3SBjoern A. Zeeb && ( ifp->if_index && (ifp->if_index <= V_if_index))) { 16044a0d6638SRuslan Ermilov sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr; 1605d314ad7bSJulian Elischer /* 1606d314ad7bSJulian Elischer * Change our mind and don't try copy. 1607d314ad7bSJulian Elischer */ 1608d314ad7bSJulian Elischer if ((sdp->sdl_family != AF_LINK) 1609d314ad7bSJulian Elischer || (sdp->sdl_len > sizeof(sdlbuf))) { 1610d314ad7bSJulian Elischer goto makedummy; 1611d314ad7bSJulian Elischer } 1612d314ad7bSJulian Elischer bcopy(sdp, sdl2, sdp->sdl_len); 1613d314ad7bSJulian Elischer } else { 1614d314ad7bSJulian Elischer makedummy: 1615d314ad7bSJulian Elischer sdl2->sdl_len 1616d314ad7bSJulian Elischer = offsetof(struct sockaddr_dl, sdl_data[0]); 1617d314ad7bSJulian Elischer sdl2->sdl_family = AF_LINK; 1618d314ad7bSJulian Elischer sdl2->sdl_index = 0; 1619d314ad7bSJulian Elischer sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 1620d314ad7bSJulian Elischer } 1621d314ad7bSJulian Elischer *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, 162282c23ebaSBill Fenner IP_RECVIF, IPPROTO_IP); 162382c23ebaSBill Fenner if (*mp) 162482c23ebaSBill Fenner mp = &(*mp)->m_next; 162582c23ebaSBill Fenner } 162682c23ebaSBill Fenner } 162782c23ebaSBill Fenner 16284d2e3692SLuigi Rizzo /* 162930916a2dSRobert Watson * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the 163030916a2dSRobert Watson * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on 163130916a2dSRobert Watson * locking. This code remains in ip_input.c as ip_mroute.c is optionally 163230916a2dSRobert Watson * compiled. 16334d2e3692SLuigi Rizzo */ 1634df8bae1dSRodney W. Grimes int 1635f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so) 1636f0068c4aSGarrett Wollman { 16378b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 16388b615593SMarko Zec 1639f0068c4aSGarrett Wollman if (so->so_type != SOCK_RAW || 1640f0068c4aSGarrett Wollman so->so_proto->pr_protocol != IPPROTO_RSVP) 1641f0068c4aSGarrett Wollman return EOPNOTSUPP; 1642f0068c4aSGarrett Wollman 1643603724d3SBjoern A. Zeeb if (V_ip_rsvpd != NULL) 1644f0068c4aSGarrett Wollman return EADDRINUSE; 1645f0068c4aSGarrett Wollman 1646603724d3SBjoern A. Zeeb V_ip_rsvpd = so; 16471c5de19aSGarrett Wollman /* 16481c5de19aSGarrett Wollman * This may seem silly, but we need to be sure we don't over-increment 16491c5de19aSGarrett Wollman * the RSVP counter, in case something slips up. 16501c5de19aSGarrett Wollman */ 1651603724d3SBjoern A. Zeeb if (!V_ip_rsvp_on) { 1652603724d3SBjoern A. Zeeb V_ip_rsvp_on = 1; 1653603724d3SBjoern A. Zeeb V_rsvp_on++; 16541c5de19aSGarrett Wollman } 1655f0068c4aSGarrett Wollman 1656f0068c4aSGarrett Wollman return 0; 1657f0068c4aSGarrett Wollman } 1658f0068c4aSGarrett Wollman 1659f0068c4aSGarrett Wollman int 1660f0068c4aSGarrett Wollman ip_rsvp_done(void) 1661f0068c4aSGarrett Wollman { 16628b615593SMarko Zec INIT_VNET_INET(curvnet); 16638b615593SMarko Zec 1664603724d3SBjoern A. Zeeb V_ip_rsvpd = NULL; 16651c5de19aSGarrett Wollman /* 16661c5de19aSGarrett Wollman * This may seem silly, but we need to be sure we don't over-decrement 16671c5de19aSGarrett Wollman * the RSVP counter, in case something slips up. 16681c5de19aSGarrett Wollman */ 1669603724d3SBjoern A. Zeeb if (V_ip_rsvp_on) { 1670603724d3SBjoern A. Zeeb V_ip_rsvp_on = 0; 1671603724d3SBjoern A. Zeeb V_rsvp_on--; 16721c5de19aSGarrett Wollman } 1673f0068c4aSGarrett Wollman return 0; 1674f0068c4aSGarrett Wollman } 1675bbb4330bSLuigi Rizzo 1676bbb4330bSLuigi Rizzo void 1677bbb4330bSLuigi Rizzo rsvp_input(struct mbuf *m, int off) /* XXX must fixup manually */ 1678bbb4330bSLuigi Rizzo { 16798b615593SMarko Zec INIT_VNET_INET(curvnet); 16808b615593SMarko Zec 1681bbb4330bSLuigi Rizzo if (rsvp_input_p) { /* call the real one if loaded */ 1682bbb4330bSLuigi Rizzo rsvp_input_p(m, off); 1683bbb4330bSLuigi Rizzo return; 1684bbb4330bSLuigi Rizzo } 1685bbb4330bSLuigi Rizzo 1686bbb4330bSLuigi Rizzo /* Can still get packets with rsvp_on = 0 if there is a local member 1687bbb4330bSLuigi Rizzo * of the group to which the RSVP packet is addressed. But in this 1688bbb4330bSLuigi Rizzo * case we want to throw the packet away. 1689bbb4330bSLuigi Rizzo */ 1690bbb4330bSLuigi Rizzo 1691603724d3SBjoern A. Zeeb if (!V_rsvp_on) { 1692bbb4330bSLuigi Rizzo m_freem(m); 1693bbb4330bSLuigi Rizzo return; 1694bbb4330bSLuigi Rizzo } 1695bbb4330bSLuigi Rizzo 1696603724d3SBjoern A. Zeeb if (V_ip_rsvpd != NULL) { 1697bbb4330bSLuigi Rizzo rip_input(m, off); 1698bbb4330bSLuigi Rizzo return; 1699bbb4330bSLuigi Rizzo } 1700bbb4330bSLuigi Rizzo /* Drop the packet */ 1701bbb4330bSLuigi Rizzo m_freem(m); 1702bbb4330bSLuigi Rizzo } 1703