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; 24544e33a07SMarko Zec V_ipforwarding = 0; 24644e33a07SMarko Zec V_ipstealth = 0; 24744e33a07SMarko Zec V_nipq = 0; /* Total # of reass queues */ 24844e33a07SMarko Zec 24944e33a07SMarko Zec V_ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */ 25044e33a07SMarko Zec V_ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */ 25144e33a07SMarko Zec V_ipport_firstauto = IPPORT_EPHEMERALFIRST; /* 10000 */ 25244e33a07SMarko Zec V_ipport_lastauto = IPPORT_EPHEMERALLAST; /* 65535 */ 25344e33a07SMarko Zec V_ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */ 25444e33a07SMarko Zec V_ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */ 25544e33a07SMarko Zec V_ipport_reservedhigh = IPPORT_RESERVED - 1; /* 1023 */ 25644e33a07SMarko Zec V_ipport_reservedlow = 0; 25744e33a07SMarko Zec V_ipport_randomized = 1; /* user controlled via sysctl */ 25844e33a07SMarko Zec V_ipport_randomcps = 10; /* user controlled via sysctl */ 25944e33a07SMarko Zec V_ipport_randomtime = 45; /* user controlled via sysctl */ 26044e33a07SMarko Zec V_ipport_stoprandom = 0; /* toggled by ipport_tick */ 26144e33a07SMarko Zec 262385195c0SMarko Zec V_fw_one_pass = 1; 263385195c0SMarko Zec 26444e33a07SMarko Zec #ifdef NOTYET 26544e33a07SMarko Zec /* XXX global static but not instantiated in this file */ 26644e33a07SMarko Zec V_ipfastforward_active = 0; 26744e33a07SMarko Zec V_subnetsarelocal = 0; 26844e33a07SMarko Zec V_sameprefixcarponly = 0; 26944e33a07SMarko Zec #endif 27044e33a07SMarko Zec 271603724d3SBjoern A. Zeeb TAILQ_INIT(&V_in_ifaddrhead); 272603724d3SBjoern A. Zeeb V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask); 273f0ffb944SJulian Elischer pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 27402410549SRobert Watson if (pr == NULL) 275db09bef3SAndre Oppermann panic("ip_init: PF_INET not found"); 276db09bef3SAndre Oppermann 277db09bef3SAndre Oppermann /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */ 278df8bae1dSRodney W. Grimes for (i = 0; i < IPPROTO_MAX; i++) 279df8bae1dSRodney W. Grimes ip_protox[i] = pr - inetsw; 280db09bef3SAndre Oppermann /* 281db09bef3SAndre Oppermann * Cycle through IP protocols and put them into the appropriate place 282db09bef3SAndre Oppermann * in ip_protox[]. 283db09bef3SAndre Oppermann */ 284f0ffb944SJulian Elischer for (pr = inetdomain.dom_protosw; 285f0ffb944SJulian Elischer pr < inetdomain.dom_protoswNPROTOSW; pr++) 286df8bae1dSRodney W. Grimes if (pr->pr_domain->dom_family == PF_INET && 287db09bef3SAndre Oppermann pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) { 288db09bef3SAndre Oppermann /* Be careful to only index valid IP protocols. */ 289db77984cSSam Leffler if (pr->pr_protocol < IPPROTO_MAX) 290df8bae1dSRodney W. Grimes ip_protox[pr->pr_protocol] = pr - inetsw; 291db09bef3SAndre Oppermann } 292194a213eSAndrey A. Chernov 293c21fd232SAndre Oppermann /* Initialize packet filter hooks. */ 294134ea224SSam Leffler inet_pfil_hook.ph_type = PFIL_TYPE_AF; 295134ea224SSam Leffler inet_pfil_hook.ph_af = AF_INET; 296134ea224SSam Leffler if ((i = pfil_head_register(&inet_pfil_hook)) != 0) 297134ea224SSam Leffler printf("%s: WARNING: unable to register pfil hook, " 298134ea224SSam Leffler "error %d\n", __func__, i); 299134ea224SSam Leffler 300db09bef3SAndre Oppermann /* Initialize IP reassembly queue. */ 3012fad1e93SSam Leffler IPQ_LOCK_INIT(); 302194a213eSAndrey A. Chernov for (i = 0; i < IPREASS_NHASH; i++) 303603724d3SBjoern A. Zeeb TAILQ_INIT(&V_ipq[i]); 304603724d3SBjoern A. Zeeb V_maxnipq = nmbclusters / 32; 305603724d3SBjoern A. Zeeb V_maxfragsperpacket = 16; 306603724d3SBjoern A. Zeeb V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL, 307d248c7d7SRobert Watson NULL, UMA_ALIGN_PTR, 0); 308d248c7d7SRobert Watson maxnipq_update(); 309194a213eSAndrey A. Chernov 3105f311da2SMike Silbersack /* Start ipport_tick. */ 3115f311da2SMike Silbersack callout_init(&ipport_tick_callout, CALLOUT_MPSAFE); 3125f311da2SMike Silbersack ipport_tick(NULL); 3135f311da2SMike Silbersack EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 3145f311da2SMike Silbersack SHUTDOWN_PRI_DEFAULT); 3154f590175SPaul Saab EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change, 3164f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 3175f311da2SMike Silbersack 318db09bef3SAndre Oppermann /* Initialize various other remaining things. */ 319b53c8130SJulian Elischer V_ip_id = time_second & 0xffff; 320df8bae1dSRodney W. Grimes ipintrq.ifq_maxlen = ipqmaxlen; 3216008862bSJohn Baldwin mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF); 32259dd72d0SRobert Watson netisr_register(NETISR_IP, ip_input, &ipintrq, 0); 323df8bae1dSRodney W. Grimes } 324df8bae1dSRodney W. Grimes 325f2565d68SRobert Watson void 326f2565d68SRobert Watson ip_fini(void *xtp) 3275f311da2SMike Silbersack { 328f2565d68SRobert Watson 3295f311da2SMike Silbersack callout_stop(&ipport_tick_callout); 3305f311da2SMike Silbersack } 3315f311da2SMike Silbersack 3324d2e3692SLuigi Rizzo /* 333df8bae1dSRodney W. Grimes * Ip input routine. Checksum and byte swap header. If fragmented 334df8bae1dSRodney W. Grimes * try to reassemble. Process options. Pass to next level. 335df8bae1dSRodney W. Grimes */ 336c67b1d17SGarrett Wollman void 337c67b1d17SGarrett Wollman ip_input(struct mbuf *m) 338df8bae1dSRodney W. Grimes { 3398b615593SMarko Zec INIT_VNET_INET(curvnet); 3409188b4a1SAndre Oppermann struct ip *ip = NULL; 3415da9f8faSJosef Karthauser struct in_ifaddr *ia = NULL; 342ca925d9cSJonathan Lemon struct ifaddr *ifa; 3439b932e9eSAndre Oppermann int checkif, hlen = 0; 34447c861ecSBrian Somers u_short sum; 34502c1c707SAndre Oppermann int dchg = 0; /* dest changed after fw */ 346f51f805fSSam Leffler struct in_addr odst; /* original dst address */ 347b715f178SLuigi Rizzo 348fe584538SDag-Erling Smørgrav M_ASSERTPKTHDR(m); 349db40007dSAndrew R. Reiter 350ac9d7e26SMax Laier if (m->m_flags & M_FASTFWD_OURS) { 3519b932e9eSAndre Oppermann /* 35276ff6dcfSAndre Oppermann * Firewall or NAT changed destination to local. 35376ff6dcfSAndre Oppermann * We expect ip_len and ip_off to be in host byte order. 3549b932e9eSAndre Oppermann */ 35576ff6dcfSAndre Oppermann m->m_flags &= ~M_FASTFWD_OURS; 35676ff6dcfSAndre Oppermann /* Set up some basics that will be used later. */ 3572b25acc1SLuigi Rizzo ip = mtod(m, struct ip *); 35853be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 3599b932e9eSAndre Oppermann goto ours; 3602b25acc1SLuigi Rizzo } 3612b25acc1SLuigi Rizzo 362603724d3SBjoern A. Zeeb V_ipstat.ips_total++; 36358938916SGarrett Wollman 36458938916SGarrett Wollman if (m->m_pkthdr.len < sizeof(struct ip)) 36558938916SGarrett Wollman goto tooshort; 36658938916SGarrett Wollman 367df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct ip) && 3680b17fba7SAndre Oppermann (m = m_pullup(m, sizeof (struct ip))) == NULL) { 369603724d3SBjoern A. Zeeb V_ipstat.ips_toosmall++; 370c67b1d17SGarrett Wollman return; 371df8bae1dSRodney W. Grimes } 372df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 37358938916SGarrett Wollman 37453be11f6SPoul-Henning Kamp if (ip->ip_v != IPVERSION) { 375603724d3SBjoern A. Zeeb V_ipstat.ips_badvers++; 376df8bae1dSRodney W. Grimes goto bad; 377df8bae1dSRodney W. Grimes } 37858938916SGarrett Wollman 37953be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 380df8bae1dSRodney W. Grimes if (hlen < sizeof(struct ip)) { /* minimum header length */ 381603724d3SBjoern A. Zeeb V_ipstat.ips_badhlen++; 382df8bae1dSRodney W. Grimes goto bad; 383df8bae1dSRodney W. Grimes } 384df8bae1dSRodney W. Grimes if (hlen > m->m_len) { 3850b17fba7SAndre Oppermann if ((m = m_pullup(m, hlen)) == NULL) { 386603724d3SBjoern A. Zeeb V_ipstat.ips_badhlen++; 387c67b1d17SGarrett Wollman return; 388df8bae1dSRodney W. Grimes } 389df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 390df8bae1dSRodney W. Grimes } 39133841545SHajimu UMEMOTO 39233841545SHajimu UMEMOTO /* 127/8 must not appear on wire - RFC1122 */ 39333841545SHajimu UMEMOTO if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 39433841545SHajimu UMEMOTO (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 39533841545SHajimu UMEMOTO if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 396603724d3SBjoern A. Zeeb V_ipstat.ips_badaddr++; 39733841545SHajimu UMEMOTO goto bad; 39833841545SHajimu UMEMOTO } 39933841545SHajimu UMEMOTO } 40033841545SHajimu UMEMOTO 401db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 402db4f9cc7SJonathan Lemon sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 403db4f9cc7SJonathan Lemon } else { 40458938916SGarrett Wollman if (hlen == sizeof(struct ip)) { 40547c861ecSBrian Somers sum = in_cksum_hdr(ip); 40658938916SGarrett Wollman } else { 40747c861ecSBrian Somers sum = in_cksum(m, hlen); 40858938916SGarrett Wollman } 409db4f9cc7SJonathan Lemon } 41047c861ecSBrian Somers if (sum) { 411603724d3SBjoern A. Zeeb V_ipstat.ips_badsum++; 412df8bae1dSRodney W. Grimes goto bad; 413df8bae1dSRodney W. Grimes } 414df8bae1dSRodney W. Grimes 41502b199f1SMax Laier #ifdef ALTQ 41602b199f1SMax Laier if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 41702b199f1SMax Laier /* packet is dropped by traffic conditioner */ 41802b199f1SMax Laier return; 41902b199f1SMax Laier #endif 42002b199f1SMax Laier 421df8bae1dSRodney W. Grimes /* 422df8bae1dSRodney W. Grimes * Convert fields to host representation. 423df8bae1dSRodney W. Grimes */ 424fd8e4ebcSMike Barcroft ip->ip_len = ntohs(ip->ip_len); 425df8bae1dSRodney W. Grimes if (ip->ip_len < hlen) { 426603724d3SBjoern A. Zeeb V_ipstat.ips_badlen++; 427df8bae1dSRodney W. Grimes goto bad; 428df8bae1dSRodney W. Grimes } 429fd8e4ebcSMike Barcroft ip->ip_off = ntohs(ip->ip_off); 430df8bae1dSRodney W. Grimes 431df8bae1dSRodney W. Grimes /* 432df8bae1dSRodney W. Grimes * Check that the amount of data in the buffers 433df8bae1dSRodney W. Grimes * is as at least much as the IP header would have us expect. 434df8bae1dSRodney W. Grimes * Trim mbufs if longer than we expect. 435df8bae1dSRodney W. Grimes * Drop packet if shorter than we expect. 436df8bae1dSRodney W. Grimes */ 437df8bae1dSRodney W. Grimes if (m->m_pkthdr.len < ip->ip_len) { 43858938916SGarrett Wollman tooshort: 439603724d3SBjoern A. Zeeb V_ipstat.ips_tooshort++; 440df8bae1dSRodney W. Grimes goto bad; 441df8bae1dSRodney W. Grimes } 442df8bae1dSRodney W. Grimes if (m->m_pkthdr.len > ip->ip_len) { 443df8bae1dSRodney W. Grimes if (m->m_len == m->m_pkthdr.len) { 444df8bae1dSRodney W. Grimes m->m_len = ip->ip_len; 445df8bae1dSRodney W. Grimes m->m_pkthdr.len = ip->ip_len; 446df8bae1dSRodney W. Grimes } else 447df8bae1dSRodney W. Grimes m_adj(m, ip->ip_len - m->m_pkthdr.len); 448df8bae1dSRodney W. Grimes } 449b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 45014dd6717SSam Leffler /* 45114dd6717SSam Leffler * Bypass packet filtering for packets from a tunnel (gif). 45214dd6717SSam Leffler */ 453cc977adcSBjoern A. Zeeb if (ip_ipsec_filtertunnel(m)) 454c21fd232SAndre Oppermann goto passin; 455b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 4563f67c834SDon Lewis 457c4ac87eaSDarren Reed /* 458134ea224SSam Leffler * Run through list of hooks for input packets. 459f51f805fSSam Leffler * 460f51f805fSSam Leffler * NB: Beware of the destination address changing (e.g. 461f51f805fSSam Leffler * by NAT rewriting). When this happens, tell 462f51f805fSSam Leffler * ip_forward to do the right thing. 463c4ac87eaSDarren Reed */ 464c21fd232SAndre Oppermann 465c21fd232SAndre Oppermann /* Jump over all PFIL processing if hooks are not active. */ 466604afec4SChristian S.J. Peron if (!PFIL_HOOKED(&inet_pfil_hook)) 467c21fd232SAndre Oppermann goto passin; 468c21fd232SAndre Oppermann 469f51f805fSSam Leffler odst = ip->ip_dst; 470134ea224SSam Leffler if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, 471d6a8d588SMax Laier PFIL_IN, NULL) != 0) 472beec8214SDarren Reed return; 473134ea224SSam Leffler if (m == NULL) /* consumed by filter */ 474c4ac87eaSDarren Reed return; 4759b932e9eSAndre Oppermann 476c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 47702c1c707SAndre Oppermann dchg = (odst.s_addr != ip->ip_dst.s_addr); 4789b932e9eSAndre Oppermann 4799b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 4809b932e9eSAndre Oppermann if (m->m_flags & M_FASTFWD_OURS) { 4819b932e9eSAndre Oppermann m->m_flags &= ~M_FASTFWD_OURS; 4829b932e9eSAndre Oppermann goto ours; 4839b932e9eSAndre Oppermann } 484099dd043SAndre Oppermann if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) { 485099dd043SAndre Oppermann /* 486099dd043SAndre Oppermann * Directly ship on the packet. This allows to forward packets 487099dd043SAndre Oppermann * that were destined for us to some other directly connected 488099dd043SAndre Oppermann * host. 489099dd043SAndre Oppermann */ 490099dd043SAndre Oppermann ip_forward(m, dchg); 491099dd043SAndre Oppermann return; 492099dd043SAndre Oppermann } 4939b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 4949b932e9eSAndre Oppermann 495c21fd232SAndre Oppermann passin: 496df8bae1dSRodney W. Grimes /* 497df8bae1dSRodney W. Grimes * Process options and, if not destined for us, 498df8bae1dSRodney W. Grimes * ship it on. ip_dooptions returns 1 when an 499df8bae1dSRodney W. Grimes * error was detected (causing an icmp message 500df8bae1dSRodney W. Grimes * to be sent and the original packet to be freed). 501df8bae1dSRodney W. Grimes */ 5029b932e9eSAndre Oppermann if (hlen > sizeof (struct ip) && ip_dooptions(m, 0)) 503c67b1d17SGarrett Wollman return; 504df8bae1dSRodney W. Grimes 505f0068c4aSGarrett Wollman /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 506f0068c4aSGarrett Wollman * matter if it is destined to another node, or whether it is 507f0068c4aSGarrett Wollman * a multicast one, RSVP wants it! and prevents it from being forwarded 508f0068c4aSGarrett Wollman * anywhere else. Also checks if the rsvp daemon is running before 509f0068c4aSGarrett Wollman * grabbing the packet. 510f0068c4aSGarrett Wollman */ 511603724d3SBjoern A. Zeeb if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP) 512f0068c4aSGarrett Wollman goto ours; 513f0068c4aSGarrett Wollman 514df8bae1dSRodney W. Grimes /* 515df8bae1dSRodney W. Grimes * Check our list of addresses, to see if the packet is for us. 516cc766e04SGarrett Wollman * If we don't have any addresses, assume any unicast packet 517cc766e04SGarrett Wollman * we receive might be for us (and let the upper layers deal 518cc766e04SGarrett Wollman * with it). 519df8bae1dSRodney W. Grimes */ 520603724d3SBjoern A. Zeeb if (TAILQ_EMPTY(&V_in_ifaddrhead) && 521cc766e04SGarrett Wollman (m->m_flags & (M_MCAST|M_BCAST)) == 0) 522cc766e04SGarrett Wollman goto ours; 523cc766e04SGarrett Wollman 5247538a9a0SJonathan Lemon /* 525823db0e9SDon Lewis * Enable a consistency check between the destination address 526823db0e9SDon Lewis * and the arrival interface for a unicast packet (the RFC 1122 527823db0e9SDon Lewis * strong ES model) if IP forwarding is disabled and the packet 528e15ae1b2SDon Lewis * is not locally generated and the packet is not subject to 529e15ae1b2SDon Lewis * 'ipfw fwd'. 5303f67c834SDon Lewis * 5313f67c834SDon Lewis * XXX - Checking also should be disabled if the destination 5323f67c834SDon Lewis * address is ipnat'ed to a different interface. 5333f67c834SDon Lewis * 534a8f12100SDon Lewis * XXX - Checking is incompatible with IP aliases added 5353f67c834SDon Lewis * to the loopback interface instead of the interface where 5363f67c834SDon Lewis * the packets are received. 537a9771948SGleb Smirnoff * 538a9771948SGleb Smirnoff * XXX - This is the case for carp vhost IPs as well so we 539a9771948SGleb Smirnoff * insert a workaround. If the packet got here, we already 540a9771948SGleb Smirnoff * checked with carp_iamatch() and carp_forus(). 541823db0e9SDon Lewis */ 542603724d3SBjoern A. Zeeb checkif = V_ip_checkinterface && (V_ipforwarding == 0) && 5439494d596SBrooks Davis m->m_pkthdr.rcvif != NULL && 544e15ae1b2SDon Lewis ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) && 545a9771948SGleb Smirnoff #ifdef DEV_CARP 546a9771948SGleb Smirnoff !m->m_pkthdr.rcvif->if_carp && 547a9771948SGleb Smirnoff #endif 5489b932e9eSAndre Oppermann (dchg == 0); 549823db0e9SDon Lewis 550ca925d9cSJonathan Lemon /* 551ca925d9cSJonathan Lemon * Check for exact addresses in the hash bucket. 552ca925d9cSJonathan Lemon */ 5539b932e9eSAndre Oppermann LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 554f9e354dfSJulian Elischer /* 555823db0e9SDon Lewis * If the address matches, verify that the packet 556823db0e9SDon Lewis * arrived via the correct interface if checking is 557823db0e9SDon Lewis * enabled. 558f9e354dfSJulian Elischer */ 5599b932e9eSAndre Oppermann if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr && 560823db0e9SDon Lewis (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif)) 561ed1ff184SJulian Elischer goto ours; 562ca925d9cSJonathan Lemon } 563823db0e9SDon Lewis /* 564ca925d9cSJonathan Lemon * Check for broadcast addresses. 565ca925d9cSJonathan Lemon * 566ca925d9cSJonathan Lemon * Only accept broadcast packets that arrive via the matching 567ca925d9cSJonathan Lemon * interface. Reception of forwarded directed broadcasts would 568ca925d9cSJonathan Lemon * be handled via ip_forward() and ether_output() with the loopback 569ca925d9cSJonathan Lemon * into the stack for SIMPLEX interfaces handled by ether_output(). 570823db0e9SDon Lewis */ 5714f450ff9SBruce M Simpson if (m->m_pkthdr.rcvif != NULL && 5724f450ff9SBruce M Simpson m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 573ca925d9cSJonathan Lemon TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 574ca925d9cSJonathan Lemon if (ifa->ifa_addr->sa_family != AF_INET) 575ca925d9cSJonathan Lemon continue; 576ca925d9cSJonathan Lemon ia = ifatoia(ifa); 577df8bae1dSRodney W. Grimes if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 5789b932e9eSAndre Oppermann ip->ip_dst.s_addr) 579df8bae1dSRodney W. Grimes goto ours; 5809b932e9eSAndre Oppermann if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr) 581df8bae1dSRodney W. Grimes goto ours; 5820ac40133SBrian Somers #ifdef BOOTP_COMPAT 583ca925d9cSJonathan Lemon if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) 584ca925d9cSJonathan Lemon goto ours; 5850ac40133SBrian Somers #endif 586df8bae1dSRodney W. Grimes } 587df8bae1dSRodney W. Grimes } 588f8429ca2SBruce M Simpson /* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */ 589f8429ca2SBruce M Simpson if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) { 590603724d3SBjoern A. Zeeb V_ipstat.ips_cantforward++; 591f8429ca2SBruce M Simpson m_freem(m); 592f8429ca2SBruce M Simpson return; 593f8429ca2SBruce M Simpson } 594df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 595603724d3SBjoern A. Zeeb if (V_ip_mrouter) { 596df8bae1dSRodney W. Grimes /* 597df8bae1dSRodney W. Grimes * If we are acting as a multicast router, all 598df8bae1dSRodney W. Grimes * incoming multicast packets are passed to the 599df8bae1dSRodney W. Grimes * kernel-level multicast forwarding function. 600df8bae1dSRodney W. Grimes * The packet is returned (relatively) intact; if 601df8bae1dSRodney W. Grimes * ip_mforward() returns a non-zero value, the packet 602df8bae1dSRodney W. Grimes * must be discarded, else it may be accepted below. 603df8bae1dSRodney W. Grimes */ 604bbb4330bSLuigi Rizzo if (ip_mforward && 605bbb4330bSLuigi Rizzo ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) { 606603724d3SBjoern A. Zeeb V_ipstat.ips_cantforward++; 607df8bae1dSRodney W. Grimes m_freem(m); 608c67b1d17SGarrett Wollman return; 609df8bae1dSRodney W. Grimes } 610df8bae1dSRodney W. Grimes 611df8bae1dSRodney W. Grimes /* 61211612afaSDima Dorfman * The process-level routing daemon needs to receive 613df8bae1dSRodney W. Grimes * all multicast IGMP packets, whether or not this 614df8bae1dSRodney W. Grimes * host belongs to their destination groups. 615df8bae1dSRodney W. Grimes */ 616df8bae1dSRodney W. Grimes if (ip->ip_p == IPPROTO_IGMP) 617df8bae1dSRodney W. Grimes goto ours; 618603724d3SBjoern A. Zeeb V_ipstat.ips_forward++; 619df8bae1dSRodney W. Grimes } 620df8bae1dSRodney W. Grimes /* 621d10910e6SBruce M Simpson * Assume the packet is for us, to avoid prematurely taking 622d10910e6SBruce M Simpson * a lock on the in_multi hash. Protocols must perform 623d10910e6SBruce M Simpson * their own filtering and update statistics accordingly. 624df8bae1dSRodney W. Grimes */ 625df8bae1dSRodney W. Grimes goto ours; 626df8bae1dSRodney W. Grimes } 627df8bae1dSRodney W. Grimes if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 628df8bae1dSRodney W. Grimes goto ours; 629df8bae1dSRodney W. Grimes if (ip->ip_dst.s_addr == INADDR_ANY) 630df8bae1dSRodney W. Grimes goto ours; 631df8bae1dSRodney W. Grimes 6326a800098SYoshinobu Inoue /* 6336a800098SYoshinobu Inoue * FAITH(Firewall Aided Internet Translator) 6346a800098SYoshinobu Inoue */ 6356a800098SYoshinobu Inoue if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 636603724d3SBjoern A. Zeeb if (V_ip_keepfaith) { 6376a800098SYoshinobu Inoue if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) 6386a800098SYoshinobu Inoue goto ours; 6396a800098SYoshinobu Inoue } 6406a800098SYoshinobu Inoue m_freem(m); 6416a800098SYoshinobu Inoue return; 6426a800098SYoshinobu Inoue } 6439494d596SBrooks Davis 644df8bae1dSRodney W. Grimes /* 645df8bae1dSRodney W. Grimes * Not for us; forward if possible and desirable. 646df8bae1dSRodney W. Grimes */ 647603724d3SBjoern A. Zeeb if (V_ipforwarding == 0) { 648603724d3SBjoern A. Zeeb V_ipstat.ips_cantforward++; 649df8bae1dSRodney W. Grimes m_freem(m); 650546f251bSChris D. Faulhaber } else { 651b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 6521dfcf0d2SAndre Oppermann if (ip_ipsec_fwd(m)) 653546f251bSChris D. Faulhaber goto bad; 654b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 6559b932e9eSAndre Oppermann ip_forward(m, dchg); 656546f251bSChris D. Faulhaber } 657c67b1d17SGarrett Wollman return; 658df8bae1dSRodney W. Grimes 659df8bae1dSRodney W. Grimes ours: 660d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH 661d0ebc0d2SYaroslav Tykhiy /* 662d0ebc0d2SYaroslav Tykhiy * IPSTEALTH: Process non-routing options only 663d0ebc0d2SYaroslav Tykhiy * if the packet is destined for us. 664d0ebc0d2SYaroslav Tykhiy */ 665603724d3SBjoern A. Zeeb if (V_ipstealth && hlen > sizeof (struct ip) && 6669b932e9eSAndre Oppermann ip_dooptions(m, 1)) 667d0ebc0d2SYaroslav Tykhiy return; 668d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */ 669d0ebc0d2SYaroslav Tykhiy 6705da9f8faSJosef Karthauser /* Count the packet in the ip address stats */ 6715da9f8faSJosef Karthauser if (ia != NULL) { 6725da9f8faSJosef Karthauser ia->ia_ifa.if_ipackets++; 6735da9f8faSJosef Karthauser ia->ia_ifa.if_ibytes += m->m_pkthdr.len; 6745da9f8faSJosef Karthauser } 675100ba1a6SJordan K. Hubbard 67663f8d699SJordan K. Hubbard /* 677b6ea1aa5SRuslan Ermilov * Attempt reassembly; if it succeeds, proceed. 678ac9d7e26SMax Laier * ip_reass() will return a different mbuf. 679df8bae1dSRodney W. Grimes */ 680f0cada84SAndre Oppermann if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 681f0cada84SAndre Oppermann m = ip_reass(m); 682f0cada84SAndre Oppermann if (m == NULL) 683c67b1d17SGarrett Wollman return; 6846a800098SYoshinobu Inoue ip = mtod(m, struct ip *); 6857e2df452SRuslan Ermilov /* Get the header length of the reassembled packet */ 68653be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 687f0cada84SAndre Oppermann } 688f0cada84SAndre Oppermann 689f0cada84SAndre Oppermann /* 690f0cada84SAndre Oppermann * Further protocols expect the packet length to be w/o the 691f0cada84SAndre Oppermann * IP header. 692f0cada84SAndre Oppermann */ 693df8bae1dSRodney W. Grimes ip->ip_len -= hlen; 694df8bae1dSRodney W. Grimes 695b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 69633841545SHajimu UMEMOTO /* 69733841545SHajimu UMEMOTO * enforce IPsec policy checking if we are seeing last header. 69833841545SHajimu UMEMOTO * note that we do not visit this with protocols with pcb layer 69933841545SHajimu UMEMOTO * code - like udp/tcp/raw ip. 70033841545SHajimu UMEMOTO */ 7011dfcf0d2SAndre Oppermann if (ip_ipsec_input(m)) 70233841545SHajimu UMEMOTO goto bad; 703b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 70433841545SHajimu UMEMOTO 705df8bae1dSRodney W. Grimes /* 706df8bae1dSRodney W. Grimes * Switch out to protocol's input routine. 707df8bae1dSRodney W. Grimes */ 708603724d3SBjoern A. Zeeb V_ipstat.ips_delivered++; 7099b932e9eSAndre Oppermann 7102b25acc1SLuigi Rizzo (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 711c67b1d17SGarrett Wollman return; 712df8bae1dSRodney W. Grimes bad: 713df8bae1dSRodney W. Grimes m_freem(m); 714c67b1d17SGarrett Wollman } 715c67b1d17SGarrett Wollman 716c67b1d17SGarrett Wollman /* 717d248c7d7SRobert Watson * After maxnipq has been updated, propagate the change to UMA. The UMA zone 718d248c7d7SRobert Watson * max has slightly different semantics than the sysctl, for historical 719d248c7d7SRobert Watson * reasons. 720d248c7d7SRobert Watson */ 721d248c7d7SRobert Watson static void 722d248c7d7SRobert Watson maxnipq_update(void) 723d248c7d7SRobert Watson { 7248b615593SMarko Zec INIT_VNET_INET(curvnet); 725d248c7d7SRobert Watson 726d248c7d7SRobert Watson /* 727d248c7d7SRobert Watson * -1 for unlimited allocation. 728d248c7d7SRobert Watson */ 729603724d3SBjoern A. Zeeb if (V_maxnipq < 0) 730603724d3SBjoern A. Zeeb uma_zone_set_max(V_ipq_zone, 0); 731d248c7d7SRobert Watson /* 732d248c7d7SRobert Watson * Positive number for specific bound. 733d248c7d7SRobert Watson */ 734603724d3SBjoern A. Zeeb if (V_maxnipq > 0) 735603724d3SBjoern A. Zeeb uma_zone_set_max(V_ipq_zone, V_maxnipq); 736d248c7d7SRobert Watson /* 737d248c7d7SRobert Watson * Zero specifies no further fragment queue allocation -- set the 738d248c7d7SRobert Watson * bound very low, but rely on implementation elsewhere to actually 739d248c7d7SRobert Watson * prevent allocation and reclaim current queues. 740d248c7d7SRobert Watson */ 741603724d3SBjoern A. Zeeb if (V_maxnipq == 0) 742603724d3SBjoern A. Zeeb uma_zone_set_max(V_ipq_zone, 1); 743d248c7d7SRobert Watson } 744d248c7d7SRobert Watson 7454f590175SPaul Saab static void 7464f590175SPaul Saab ipq_zone_change(void *tag) 7474f590175SPaul Saab { 7488b615593SMarko Zec INIT_VNET_INET(curvnet); 7494f590175SPaul Saab 750603724d3SBjoern A. Zeeb if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) { 751603724d3SBjoern A. Zeeb V_maxnipq = nmbclusters / 32; 7524f590175SPaul Saab maxnipq_update(); 7534f590175SPaul Saab } 7544f590175SPaul Saab } 7554f590175SPaul Saab 756d248c7d7SRobert Watson static int 757d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS) 758d248c7d7SRobert Watson { 7598b615593SMarko Zec INIT_VNET_INET(curvnet); 760d248c7d7SRobert Watson int error, i; 761d248c7d7SRobert Watson 762603724d3SBjoern A. Zeeb i = V_maxnipq; 763d248c7d7SRobert Watson error = sysctl_handle_int(oidp, &i, 0, req); 764d248c7d7SRobert Watson if (error || !req->newptr) 765d248c7d7SRobert Watson return (error); 766d248c7d7SRobert Watson 767d248c7d7SRobert Watson /* 768d248c7d7SRobert Watson * XXXRW: Might be a good idea to sanity check the argument and place 769d248c7d7SRobert Watson * an extreme upper bound. 770d248c7d7SRobert Watson */ 771d248c7d7SRobert Watson if (i < -1) 772d248c7d7SRobert Watson return (EINVAL); 773603724d3SBjoern A. Zeeb V_maxnipq = i; 774d248c7d7SRobert Watson maxnipq_update(); 775d248c7d7SRobert Watson return (0); 776d248c7d7SRobert Watson } 777d248c7d7SRobert Watson 778d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW, 779d248c7d7SRobert Watson NULL, 0, sysctl_maxnipq, "I", 780d248c7d7SRobert Watson "Maximum number of IPv4 fragment reassembly queue entries"); 781d248c7d7SRobert Watson 782d248c7d7SRobert Watson /* 7838948e4baSArchie Cobbs * Take incoming datagram fragment and try to reassemble it into 784f0cada84SAndre Oppermann * whole datagram. If the argument is the first fragment or one 785f0cada84SAndre Oppermann * in between the function will return NULL and store the mbuf 786f0cada84SAndre Oppermann * in the fragment chain. If the argument is the last fragment 787f0cada84SAndre Oppermann * the packet will be reassembled and the pointer to the new 788f0cada84SAndre Oppermann * mbuf returned for further processing. Only m_tags attached 789f0cada84SAndre Oppermann * to the first packet/fragment are preserved. 790f0cada84SAndre Oppermann * The IP header is *NOT* adjusted out of iplen. 791df8bae1dSRodney W. Grimes */ 792f0cada84SAndre Oppermann struct mbuf * 793f0cada84SAndre Oppermann ip_reass(struct mbuf *m) 794df8bae1dSRodney W. Grimes { 7958b615593SMarko Zec INIT_VNET_INET(curvnet); 796f0cada84SAndre Oppermann struct ip *ip; 797f0cada84SAndre Oppermann struct mbuf *p, *q, *nq, *t; 798f0cada84SAndre Oppermann struct ipq *fp = NULL; 799f0cada84SAndre Oppermann struct ipqhead *head; 800f0cada84SAndre Oppermann int i, hlen, next; 80159dfcba4SHajimu UMEMOTO u_int8_t ecn, ecn0; 802f0cada84SAndre Oppermann u_short hash; 803df8bae1dSRodney W. Grimes 804800af1fbSMaxim Konovalov /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */ 805603724d3SBjoern A. Zeeb if (V_maxnipq == 0 || V_maxfragsperpacket == 0) { 806603724d3SBjoern A. Zeeb V_ipstat.ips_fragments++; 807603724d3SBjoern A. Zeeb V_ipstat.ips_fragdropped++; 8089d804f81SAndre Oppermann m_freem(m); 8099d804f81SAndre Oppermann return (NULL); 810f0cada84SAndre Oppermann } 8112fad1e93SSam Leffler 812f0cada84SAndre Oppermann ip = mtod(m, struct ip *); 813f0cada84SAndre Oppermann hlen = ip->ip_hl << 2; 814f0cada84SAndre Oppermann 815f0cada84SAndre Oppermann hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 816603724d3SBjoern A. Zeeb head = &V_ipq[hash]; 817f0cada84SAndre Oppermann IPQ_LOCK(); 818f0cada84SAndre Oppermann 819f0cada84SAndre Oppermann /* 820f0cada84SAndre Oppermann * Look for queue of fragments 821f0cada84SAndre Oppermann * of this datagram. 822f0cada84SAndre Oppermann */ 823f0cada84SAndre Oppermann TAILQ_FOREACH(fp, head, ipq_list) 824f0cada84SAndre Oppermann if (ip->ip_id == fp->ipq_id && 825f0cada84SAndre Oppermann ip->ip_src.s_addr == fp->ipq_src.s_addr && 826f0cada84SAndre Oppermann ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 827f0cada84SAndre Oppermann #ifdef MAC 82830d239bcSRobert Watson mac_ipq_match(m, fp) && 829f0cada84SAndre Oppermann #endif 830f0cada84SAndre Oppermann ip->ip_p == fp->ipq_p) 831f0cada84SAndre Oppermann goto found; 832f0cada84SAndre Oppermann 833f0cada84SAndre Oppermann fp = NULL; 834f0cada84SAndre Oppermann 835f0cada84SAndre Oppermann /* 836d248c7d7SRobert Watson * Attempt to trim the number of allocated fragment queues if it 837d248c7d7SRobert Watson * exceeds the administrative limit. 838f0cada84SAndre Oppermann */ 839603724d3SBjoern A. Zeeb if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) { 840f0cada84SAndre Oppermann /* 841f0cada84SAndre Oppermann * drop something from the tail of the current queue 842f0cada84SAndre Oppermann * before proceeding further 843f0cada84SAndre Oppermann */ 844f0cada84SAndre Oppermann struct ipq *q = TAILQ_LAST(head, ipqhead); 845f0cada84SAndre Oppermann if (q == NULL) { /* gak */ 846f0cada84SAndre Oppermann for (i = 0; i < IPREASS_NHASH; i++) { 847603724d3SBjoern A. Zeeb struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead); 848f0cada84SAndre Oppermann if (r) { 849ac957cd2SJulian Elischer V_ipstat.ips_fragtimeout += 850ac957cd2SJulian Elischer r->ipq_nfrags; 851603724d3SBjoern A. Zeeb ip_freef(&V_ipq[i], r); 852f0cada84SAndre Oppermann break; 853f0cada84SAndre Oppermann } 854f0cada84SAndre Oppermann } 855f0cada84SAndre Oppermann } else { 856603724d3SBjoern A. Zeeb V_ipstat.ips_fragtimeout += q->ipq_nfrags; 857f0cada84SAndre Oppermann ip_freef(head, q); 858f0cada84SAndre Oppermann } 859f0cada84SAndre Oppermann } 860f0cada84SAndre Oppermann 861f0cada84SAndre Oppermann found: 862f0cada84SAndre Oppermann /* 863f0cada84SAndre Oppermann * Adjust ip_len to not reflect header, 864f0cada84SAndre Oppermann * convert offset of this to bytes. 865f0cada84SAndre Oppermann */ 866f0cada84SAndre Oppermann ip->ip_len -= hlen; 867f0cada84SAndre Oppermann if (ip->ip_off & IP_MF) { 868f0cada84SAndre Oppermann /* 869f0cada84SAndre Oppermann * Make sure that fragments have a data length 870f0cada84SAndre Oppermann * that's a non-zero multiple of 8 bytes. 871f0cada84SAndre Oppermann */ 872f0cada84SAndre Oppermann if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { 873603724d3SBjoern A. Zeeb V_ipstat.ips_toosmall++; /* XXX */ 874f0cada84SAndre Oppermann goto dropfrag; 875f0cada84SAndre Oppermann } 876f0cada84SAndre Oppermann m->m_flags |= M_FRAG; 877f0cada84SAndre Oppermann } else 878f0cada84SAndre Oppermann m->m_flags &= ~M_FRAG; 879f0cada84SAndre Oppermann ip->ip_off <<= 3; 880f0cada84SAndre Oppermann 881f0cada84SAndre Oppermann 882f0cada84SAndre Oppermann /* 883f0cada84SAndre Oppermann * Attempt reassembly; if it succeeds, proceed. 884f0cada84SAndre Oppermann * ip_reass() will return a different mbuf. 885f0cada84SAndre Oppermann */ 886603724d3SBjoern A. Zeeb V_ipstat.ips_fragments++; 887f0cada84SAndre Oppermann m->m_pkthdr.header = ip; 888f0cada84SAndre Oppermann 889f0cada84SAndre Oppermann /* Previous ip_reass() started here. */ 890df8bae1dSRodney W. Grimes /* 891df8bae1dSRodney W. Grimes * Presence of header sizes in mbufs 892df8bae1dSRodney W. Grimes * would confuse code below. 893df8bae1dSRodney W. Grimes */ 894df8bae1dSRodney W. Grimes m->m_data += hlen; 895df8bae1dSRodney W. Grimes m->m_len -= hlen; 896df8bae1dSRodney W. Grimes 897df8bae1dSRodney W. Grimes /* 898df8bae1dSRodney W. Grimes * If first fragment to arrive, create a reassembly queue. 899df8bae1dSRodney W. Grimes */ 900042bbfa3SRobert Watson if (fp == NULL) { 901603724d3SBjoern A. Zeeb fp = uma_zalloc(V_ipq_zone, M_NOWAIT); 902d248c7d7SRobert Watson if (fp == NULL) 903df8bae1dSRodney W. Grimes goto dropfrag; 90436b0360bSRobert Watson #ifdef MAC 90530d239bcSRobert Watson if (mac_ipq_init(fp, M_NOWAIT) != 0) { 906603724d3SBjoern A. Zeeb uma_zfree(V_ipq_zone, fp); 9071d7d0bfeSPawel Jakub Dawidek fp = NULL; 9085e7ce478SRobert Watson goto dropfrag; 9095e7ce478SRobert Watson } 91030d239bcSRobert Watson mac_ipq_create(m, fp); 91136b0360bSRobert Watson #endif 912462b86feSPoul-Henning Kamp TAILQ_INSERT_HEAD(head, fp, ipq_list); 913603724d3SBjoern A. Zeeb V_nipq++; 914375386e2SMike Silbersack fp->ipq_nfrags = 1; 915df8bae1dSRodney W. Grimes fp->ipq_ttl = IPFRAGTTL; 916df8bae1dSRodney W. Grimes fp->ipq_p = ip->ip_p; 917df8bae1dSRodney W. Grimes fp->ipq_id = ip->ip_id; 9186effc713SDoug Rabson fp->ipq_src = ip->ip_src; 9196effc713SDoug Rabson fp->ipq_dst = ip->ip_dst; 920af38c68cSLuigi Rizzo fp->ipq_frags = m; 921af38c68cSLuigi Rizzo m->m_nextpkt = NULL; 922800af1fbSMaxim Konovalov goto done; 92336b0360bSRobert Watson } else { 924375386e2SMike Silbersack fp->ipq_nfrags++; 92536b0360bSRobert Watson #ifdef MAC 92630d239bcSRobert Watson mac_ipq_update(m, fp); 92736b0360bSRobert Watson #endif 928df8bae1dSRodney W. Grimes } 929df8bae1dSRodney W. Grimes 9306effc713SDoug Rabson #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) 9316effc713SDoug Rabson 932df8bae1dSRodney W. Grimes /* 93359dfcba4SHajimu UMEMOTO * Handle ECN by comparing this segment with the first one; 93459dfcba4SHajimu UMEMOTO * if CE is set, do not lose CE. 93559dfcba4SHajimu UMEMOTO * drop if CE and not-ECT are mixed for the same packet. 93659dfcba4SHajimu UMEMOTO */ 93759dfcba4SHajimu UMEMOTO ecn = ip->ip_tos & IPTOS_ECN_MASK; 93859dfcba4SHajimu UMEMOTO ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK; 93959dfcba4SHajimu UMEMOTO if (ecn == IPTOS_ECN_CE) { 94059dfcba4SHajimu UMEMOTO if (ecn0 == IPTOS_ECN_NOTECT) 94159dfcba4SHajimu UMEMOTO goto dropfrag; 94259dfcba4SHajimu UMEMOTO if (ecn0 != IPTOS_ECN_CE) 94359dfcba4SHajimu UMEMOTO GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE; 94459dfcba4SHajimu UMEMOTO } 94559dfcba4SHajimu UMEMOTO if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) 94659dfcba4SHajimu UMEMOTO goto dropfrag; 94759dfcba4SHajimu UMEMOTO 94859dfcba4SHajimu UMEMOTO /* 949df8bae1dSRodney W. Grimes * Find a segment which begins after this one does. 950df8bae1dSRodney W. Grimes */ 9516effc713SDoug Rabson for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 9526effc713SDoug Rabson if (GETIP(q)->ip_off > ip->ip_off) 953df8bae1dSRodney W. Grimes break; 954df8bae1dSRodney W. Grimes 955df8bae1dSRodney W. Grimes /* 956df8bae1dSRodney W. Grimes * If there is a preceding segment, it may provide some of 957df8bae1dSRodney W. Grimes * our data already. If so, drop the data from the incoming 958af38c68cSLuigi Rizzo * segment. If it provides all of our data, drop us, otherwise 959af38c68cSLuigi Rizzo * stick new segment in the proper place. 960db4f9cc7SJonathan Lemon * 961db4f9cc7SJonathan Lemon * If some of the data is dropped from the the preceding 962db4f9cc7SJonathan Lemon * segment, then it's checksum is invalidated. 963df8bae1dSRodney W. Grimes */ 9646effc713SDoug Rabson if (p) { 9656effc713SDoug Rabson i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; 966df8bae1dSRodney W. Grimes if (i > 0) { 967df8bae1dSRodney W. Grimes if (i >= ip->ip_len) 968df8bae1dSRodney W. Grimes goto dropfrag; 9696a800098SYoshinobu Inoue m_adj(m, i); 970db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = 0; 971df8bae1dSRodney W. Grimes ip->ip_off += i; 972df8bae1dSRodney W. Grimes ip->ip_len -= i; 973df8bae1dSRodney W. Grimes } 974af38c68cSLuigi Rizzo m->m_nextpkt = p->m_nextpkt; 975af38c68cSLuigi Rizzo p->m_nextpkt = m; 976af38c68cSLuigi Rizzo } else { 977af38c68cSLuigi Rizzo m->m_nextpkt = fp->ipq_frags; 978af38c68cSLuigi Rizzo fp->ipq_frags = m; 979df8bae1dSRodney W. Grimes } 980df8bae1dSRodney W. Grimes 981df8bae1dSRodney W. Grimes /* 982df8bae1dSRodney W. Grimes * While we overlap succeeding segments trim them or, 983df8bae1dSRodney W. Grimes * if they are completely covered, dequeue them. 984df8bae1dSRodney W. Grimes */ 9856effc713SDoug Rabson for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; 986af38c68cSLuigi Rizzo q = nq) { 987b36f5b37SMaxim Konovalov i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off; 9886effc713SDoug Rabson if (i < GETIP(q)->ip_len) { 9896effc713SDoug Rabson GETIP(q)->ip_len -= i; 9906effc713SDoug Rabson GETIP(q)->ip_off += i; 9916effc713SDoug Rabson m_adj(q, i); 992db4f9cc7SJonathan Lemon q->m_pkthdr.csum_flags = 0; 993df8bae1dSRodney W. Grimes break; 994df8bae1dSRodney W. Grimes } 9956effc713SDoug Rabson nq = q->m_nextpkt; 996af38c68cSLuigi Rizzo m->m_nextpkt = nq; 997603724d3SBjoern A. Zeeb V_ipstat.ips_fragdropped++; 998375386e2SMike Silbersack fp->ipq_nfrags--; 9996effc713SDoug Rabson m_freem(q); 1000df8bae1dSRodney W. Grimes } 1001df8bae1dSRodney W. Grimes 1002df8bae1dSRodney W. Grimes /* 1003375386e2SMike Silbersack * Check for complete reassembly and perform frag per packet 1004375386e2SMike Silbersack * limiting. 1005375386e2SMike Silbersack * 1006375386e2SMike Silbersack * Frag limiting is performed here so that the nth frag has 1007375386e2SMike Silbersack * a chance to complete the packet before we drop the packet. 1008375386e2SMike Silbersack * As a result, n+1 frags are actually allowed per packet, but 1009375386e2SMike Silbersack * only n will ever be stored. (n = maxfragsperpacket.) 1010375386e2SMike Silbersack * 1011df8bae1dSRodney W. Grimes */ 10126effc713SDoug Rabson next = 0; 10136effc713SDoug Rabson for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 1014375386e2SMike Silbersack if (GETIP(q)->ip_off != next) { 1015603724d3SBjoern A. Zeeb if (fp->ipq_nfrags > V_maxfragsperpacket) { 1016603724d3SBjoern A. Zeeb V_ipstat.ips_fragdropped += fp->ipq_nfrags; 1017375386e2SMike Silbersack ip_freef(head, fp); 101899e8617dSMaxim Konovalov } 1019f0cada84SAndre Oppermann goto done; 1020375386e2SMike Silbersack } 10216effc713SDoug Rabson next += GETIP(q)->ip_len; 10226effc713SDoug Rabson } 10236effc713SDoug Rabson /* Make sure the last packet didn't have the IP_MF flag */ 1024375386e2SMike Silbersack if (p->m_flags & M_FRAG) { 1025603724d3SBjoern A. Zeeb if (fp->ipq_nfrags > V_maxfragsperpacket) { 1026603724d3SBjoern A. Zeeb V_ipstat.ips_fragdropped += fp->ipq_nfrags; 1027375386e2SMike Silbersack ip_freef(head, fp); 102899e8617dSMaxim Konovalov } 1029f0cada84SAndre Oppermann goto done; 1030375386e2SMike Silbersack } 1031df8bae1dSRodney W. Grimes 1032df8bae1dSRodney W. Grimes /* 1033430d30d8SBill Fenner * Reassembly is complete. Make sure the packet is a sane size. 1034430d30d8SBill Fenner */ 10356effc713SDoug Rabson q = fp->ipq_frags; 10366effc713SDoug Rabson ip = GETIP(q); 103753be11f6SPoul-Henning Kamp if (next + (ip->ip_hl << 2) > IP_MAXPACKET) { 1038603724d3SBjoern A. Zeeb V_ipstat.ips_toolong++; 1039603724d3SBjoern A. Zeeb V_ipstat.ips_fragdropped += fp->ipq_nfrags; 1040462b86feSPoul-Henning Kamp ip_freef(head, fp); 1041f0cada84SAndre Oppermann goto done; 1042430d30d8SBill Fenner } 1043430d30d8SBill Fenner 1044430d30d8SBill Fenner /* 1045430d30d8SBill Fenner * Concatenate fragments. 1046df8bae1dSRodney W. Grimes */ 10476effc713SDoug Rabson m = q; 1048df8bae1dSRodney W. Grimes t = m->m_next; 104902410549SRobert Watson m->m_next = NULL; 1050df8bae1dSRodney W. Grimes m_cat(m, t); 10516effc713SDoug Rabson nq = q->m_nextpkt; 105202410549SRobert Watson q->m_nextpkt = NULL; 10536effc713SDoug Rabson for (q = nq; q != NULL; q = nq) { 10546effc713SDoug Rabson nq = q->m_nextpkt; 1055945aa40dSDoug Rabson q->m_nextpkt = NULL; 1056db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; 1057db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; 1058a8db1d93SJonathan Lemon m_cat(m, q); 1059df8bae1dSRodney W. Grimes } 10606edb555dSOleg Bulyzhin /* 10616edb555dSOleg Bulyzhin * In order to do checksumming faster we do 'end-around carry' here 10626edb555dSOleg Bulyzhin * (and not in for{} loop), though it implies we are not going to 10636edb555dSOleg Bulyzhin * reassemble more than 64k fragments. 10646edb555dSOleg Bulyzhin */ 10656edb555dSOleg Bulyzhin m->m_pkthdr.csum_data = 10666edb555dSOleg Bulyzhin (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16); 106736b0360bSRobert Watson #ifdef MAC 106830d239bcSRobert Watson mac_ipq_reassemble(fp, m); 106930d239bcSRobert Watson mac_ipq_destroy(fp); 107036b0360bSRobert Watson #endif 1071df8bae1dSRodney W. Grimes 1072df8bae1dSRodney W. Grimes /* 1073f0cada84SAndre Oppermann * Create header for new ip packet by modifying header of first 1074f0cada84SAndre Oppermann * packet; dequeue and discard fragment reassembly header. 1075df8bae1dSRodney W. Grimes * Make header visible. 1076df8bae1dSRodney W. Grimes */ 1077f0cada84SAndre Oppermann ip->ip_len = (ip->ip_hl << 2) + next; 10786effc713SDoug Rabson ip->ip_src = fp->ipq_src; 10796effc713SDoug Rabson ip->ip_dst = fp->ipq_dst; 1080462b86feSPoul-Henning Kamp TAILQ_REMOVE(head, fp, ipq_list); 1081603724d3SBjoern A. Zeeb V_nipq--; 1082603724d3SBjoern A. Zeeb uma_zfree(V_ipq_zone, fp); 108353be11f6SPoul-Henning Kamp m->m_len += (ip->ip_hl << 2); 108453be11f6SPoul-Henning Kamp m->m_data -= (ip->ip_hl << 2); 1085df8bae1dSRodney W. Grimes /* some debugging cruft by sklower, below, will go away soon */ 1086a5554bf0SPoul-Henning Kamp if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */ 1087a5554bf0SPoul-Henning Kamp m_fixhdr(m); 1088603724d3SBjoern A. Zeeb V_ipstat.ips_reassembled++; 1089f0cada84SAndre Oppermann IPQ_UNLOCK(); 10906a800098SYoshinobu Inoue return (m); 1091df8bae1dSRodney W. Grimes 1092df8bae1dSRodney W. Grimes dropfrag: 1093603724d3SBjoern A. Zeeb V_ipstat.ips_fragdropped++; 1094042bbfa3SRobert Watson if (fp != NULL) 1095375386e2SMike Silbersack fp->ipq_nfrags--; 1096df8bae1dSRodney W. Grimes m_freem(m); 1097f0cada84SAndre Oppermann done: 1098f0cada84SAndre Oppermann IPQ_UNLOCK(); 1099f0cada84SAndre Oppermann return (NULL); 11006effc713SDoug Rabson 11016effc713SDoug Rabson #undef GETIP 1102df8bae1dSRodney W. Grimes } 1103df8bae1dSRodney W. Grimes 1104df8bae1dSRodney W. Grimes /* 1105df8bae1dSRodney W. Grimes * Free a fragment reassembly header and all 1106df8bae1dSRodney W. Grimes * associated datagrams. 1107df8bae1dSRodney W. Grimes */ 11080312fbe9SPoul-Henning Kamp static void 1109f2565d68SRobert Watson ip_freef(struct ipqhead *fhp, struct ipq *fp) 1110df8bae1dSRodney W. Grimes { 11118b615593SMarko Zec INIT_VNET_INET(curvnet); 1112f2565d68SRobert Watson struct mbuf *q; 1113df8bae1dSRodney W. Grimes 11142fad1e93SSam Leffler IPQ_LOCK_ASSERT(); 11152fad1e93SSam Leffler 11166effc713SDoug Rabson while (fp->ipq_frags) { 11176effc713SDoug Rabson q = fp->ipq_frags; 11186effc713SDoug Rabson fp->ipq_frags = q->m_nextpkt; 11196effc713SDoug Rabson m_freem(q); 1120df8bae1dSRodney W. Grimes } 1121462b86feSPoul-Henning Kamp TAILQ_REMOVE(fhp, fp, ipq_list); 1122603724d3SBjoern A. Zeeb uma_zfree(V_ipq_zone, fp); 1123603724d3SBjoern A. Zeeb V_nipq--; 1124df8bae1dSRodney W. Grimes } 1125df8bae1dSRodney W. Grimes 1126df8bae1dSRodney W. Grimes /* 1127df8bae1dSRodney W. Grimes * IP timer processing; 1128df8bae1dSRodney W. Grimes * if a timer expires on a reassembly 1129df8bae1dSRodney W. Grimes * queue, discard it. 1130df8bae1dSRodney W. Grimes */ 1131df8bae1dSRodney W. Grimes void 1132f2565d68SRobert Watson ip_slowtimo(void) 1133df8bae1dSRodney W. Grimes { 11348b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 1135f2565d68SRobert Watson struct ipq *fp; 1136194a213eSAndrey A. Chernov int i; 1137df8bae1dSRodney W. Grimes 11382fad1e93SSam Leffler IPQ_LOCK(); 11398b615593SMarko Zec VNET_LIST_RLOCK(); 11408b615593SMarko Zec VNET_FOREACH(vnet_iter) { 11418b615593SMarko Zec CURVNET_SET(vnet_iter); 11428b615593SMarko Zec INIT_VNET_INET(vnet_iter); 1143194a213eSAndrey A. Chernov for (i = 0; i < IPREASS_NHASH; i++) { 1144603724d3SBjoern A. Zeeb for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) { 1145462b86feSPoul-Henning Kamp struct ipq *fpp; 1146462b86feSPoul-Henning Kamp 1147462b86feSPoul-Henning Kamp fpp = fp; 1148462b86feSPoul-Henning Kamp fp = TAILQ_NEXT(fp, ipq_list); 1149462b86feSPoul-Henning Kamp if(--fpp->ipq_ttl == 0) { 11508b615593SMarko Zec V_ipstat.ips_fragtimeout += 11518b615593SMarko Zec fpp->ipq_nfrags; 1152603724d3SBjoern A. Zeeb ip_freef(&V_ipq[i], fpp); 1153df8bae1dSRodney W. Grimes } 1154df8bae1dSRodney W. Grimes } 1155194a213eSAndrey A. Chernov } 1156690a6055SJesper Skriver /* 1157690a6055SJesper Skriver * If we are over the maximum number of fragments 1158690a6055SJesper Skriver * (due to the limit being lowered), drain off 1159690a6055SJesper Skriver * enough to get down to the new limit. 1160690a6055SJesper Skriver */ 1161603724d3SBjoern A. Zeeb if (V_maxnipq >= 0 && V_nipq > V_maxnipq) { 1162690a6055SJesper Skriver for (i = 0; i < IPREASS_NHASH; i++) { 11638b615593SMarko Zec while (V_nipq > V_maxnipq && 11648b615593SMarko Zec !TAILQ_EMPTY(&V_ipq[i])) { 1165603724d3SBjoern A. Zeeb V_ipstat.ips_fragdropped += 1166603724d3SBjoern A. Zeeb TAILQ_FIRST(&V_ipq[i])->ipq_nfrags; 11678b615593SMarko Zec ip_freef(&V_ipq[i], 11688b615593SMarko Zec TAILQ_FIRST(&V_ipq[i])); 1169690a6055SJesper Skriver } 1170690a6055SJesper Skriver } 1171690a6055SJesper Skriver } 11728b615593SMarko Zec CURVNET_RESTORE(); 11738b615593SMarko Zec } 11748b615593SMarko Zec VNET_LIST_RUNLOCK(); 11752fad1e93SSam Leffler IPQ_UNLOCK(); 1176df8bae1dSRodney W. Grimes } 1177df8bae1dSRodney W. Grimes 1178df8bae1dSRodney W. Grimes /* 1179df8bae1dSRodney W. Grimes * Drain off all datagram fragments. 1180df8bae1dSRodney W. Grimes */ 1181df8bae1dSRodney W. Grimes void 1182f2565d68SRobert Watson ip_drain(void) 1183df8bae1dSRodney W. Grimes { 11848b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 1185194a213eSAndrey A. Chernov int i; 1186ce29ab3aSGarrett Wollman 11872fad1e93SSam Leffler IPQ_LOCK(); 11888b615593SMarko Zec VNET_LIST_RLOCK(); 11898b615593SMarko Zec VNET_FOREACH(vnet_iter) { 11908b615593SMarko Zec CURVNET_SET(vnet_iter); 11918b615593SMarko Zec INIT_VNET_INET(vnet_iter); 1192194a213eSAndrey A. Chernov for (i = 0; i < IPREASS_NHASH; i++) { 1193603724d3SBjoern A. Zeeb while(!TAILQ_EMPTY(&V_ipq[i])) { 1194603724d3SBjoern A. Zeeb V_ipstat.ips_fragdropped += 1195603724d3SBjoern A. Zeeb TAILQ_FIRST(&V_ipq[i])->ipq_nfrags; 1196603724d3SBjoern A. Zeeb ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i])); 1197194a213eSAndrey A. Chernov } 1198194a213eSAndrey A. Chernov } 11998b615593SMarko Zec CURVNET_RESTORE(); 12008b615593SMarko Zec } 12018b615593SMarko Zec VNET_LIST_RUNLOCK(); 12022fad1e93SSam Leffler IPQ_UNLOCK(); 1203ce29ab3aSGarrett Wollman in_rtqdrain(); 1204df8bae1dSRodney W. Grimes } 1205df8bae1dSRodney W. Grimes 1206df8bae1dSRodney W. Grimes /* 1207de38924dSAndre Oppermann * The protocol to be inserted into ip_protox[] must be already registered 1208de38924dSAndre Oppermann * in inetsw[], either statically or through pf_proto_register(). 1209de38924dSAndre Oppermann */ 1210de38924dSAndre Oppermann int 1211de38924dSAndre Oppermann ipproto_register(u_char ipproto) 1212de38924dSAndre Oppermann { 1213de38924dSAndre Oppermann struct protosw *pr; 1214de38924dSAndre Oppermann 1215de38924dSAndre Oppermann /* Sanity checks. */ 1216de38924dSAndre Oppermann if (ipproto == 0) 1217de38924dSAndre Oppermann return (EPROTONOSUPPORT); 1218de38924dSAndre Oppermann 1219de38924dSAndre Oppermann /* 1220de38924dSAndre Oppermann * The protocol slot must not be occupied by another protocol 1221de38924dSAndre Oppermann * already. An index pointing to IPPROTO_RAW is unused. 1222de38924dSAndre Oppermann */ 1223de38924dSAndre Oppermann pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 1224de38924dSAndre Oppermann if (pr == NULL) 1225de38924dSAndre Oppermann return (EPFNOSUPPORT); 1226de38924dSAndre Oppermann if (ip_protox[ipproto] != pr - inetsw) /* IPPROTO_RAW */ 1227de38924dSAndre Oppermann return (EEXIST); 1228de38924dSAndre Oppermann 1229de38924dSAndre Oppermann /* Find the protocol position in inetsw[] and set the index. */ 1230de38924dSAndre Oppermann for (pr = inetdomain.dom_protosw; 1231de38924dSAndre Oppermann pr < inetdomain.dom_protoswNPROTOSW; pr++) { 1232de38924dSAndre Oppermann if (pr->pr_domain->dom_family == PF_INET && 1233de38924dSAndre Oppermann pr->pr_protocol && pr->pr_protocol == ipproto) { 1234de38924dSAndre Oppermann /* Be careful to only index valid IP protocols. */ 1235db77984cSSam Leffler if (pr->pr_protocol < IPPROTO_MAX) { 1236de38924dSAndre Oppermann ip_protox[pr->pr_protocol] = pr - inetsw; 1237de38924dSAndre Oppermann return (0); 1238de38924dSAndre Oppermann } else 1239de38924dSAndre Oppermann return (EINVAL); 1240de38924dSAndre Oppermann } 1241de38924dSAndre Oppermann } 1242de38924dSAndre Oppermann return (EPROTONOSUPPORT); 1243de38924dSAndre Oppermann } 1244de38924dSAndre Oppermann 1245de38924dSAndre Oppermann int 1246de38924dSAndre Oppermann ipproto_unregister(u_char ipproto) 1247de38924dSAndre Oppermann { 1248de38924dSAndre Oppermann struct protosw *pr; 1249de38924dSAndre Oppermann 1250de38924dSAndre Oppermann /* Sanity checks. */ 1251de38924dSAndre Oppermann if (ipproto == 0) 1252de38924dSAndre Oppermann return (EPROTONOSUPPORT); 1253de38924dSAndre Oppermann 1254de38924dSAndre Oppermann /* Check if the protocol was indeed registered. */ 1255de38924dSAndre Oppermann pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 1256de38924dSAndre Oppermann if (pr == NULL) 1257de38924dSAndre Oppermann return (EPFNOSUPPORT); 1258de38924dSAndre Oppermann if (ip_protox[ipproto] == pr - inetsw) /* IPPROTO_RAW */ 1259de38924dSAndre Oppermann return (ENOENT); 1260de38924dSAndre Oppermann 1261de38924dSAndre Oppermann /* Reset the protocol slot to IPPROTO_RAW. */ 1262de38924dSAndre Oppermann ip_protox[ipproto] = pr - inetsw; 1263de38924dSAndre Oppermann return (0); 1264de38924dSAndre Oppermann } 1265de38924dSAndre Oppermann 1266df8bae1dSRodney W. Grimes /* 1267df8bae1dSRodney W. Grimes * Given address of next destination (final or next hop), 1268df8bae1dSRodney W. Grimes * return internet address info of interface to be used to get there. 1269df8bae1dSRodney W. Grimes */ 1270bd714208SRuslan Ermilov struct in_ifaddr * 12718b07e49aSJulian Elischer ip_rtaddr(struct in_addr dst, u_int fibnum) 1272df8bae1dSRodney W. Grimes { 127397d8d152SAndre Oppermann struct route sro; 127402c1c707SAndre Oppermann struct sockaddr_in *sin; 127502c1c707SAndre Oppermann struct in_ifaddr *ifa; 1276df8bae1dSRodney W. Grimes 12770cfbbe3bSAndre Oppermann bzero(&sro, sizeof(sro)); 127897d8d152SAndre Oppermann sin = (struct sockaddr_in *)&sro.ro_dst; 1279df8bae1dSRodney W. Grimes sin->sin_family = AF_INET; 1280df8bae1dSRodney W. Grimes sin->sin_len = sizeof(*sin); 1281df8bae1dSRodney W. Grimes sin->sin_addr = dst; 12826e6b3f7cSQing Li in_rtalloc_ign(&sro, 0, fibnum); 1283df8bae1dSRodney W. Grimes 128497d8d152SAndre Oppermann if (sro.ro_rt == NULL) 128502410549SRobert Watson return (NULL); 128602c1c707SAndre Oppermann 128797d8d152SAndre Oppermann ifa = ifatoia(sro.ro_rt->rt_ifa); 128897d8d152SAndre Oppermann RTFREE(sro.ro_rt); 128902410549SRobert Watson return (ifa); 1290df8bae1dSRodney W. Grimes } 1291df8bae1dSRodney W. Grimes 1292df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = { 1293df8bae1dSRodney W. Grimes 0, 0, 0, 0, 1294df8bae1dSRodney W. Grimes 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1295df8bae1dSRodney W. Grimes EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1296df8bae1dSRodney W. Grimes EMSGSIZE, EHOSTUNREACH, 0, 0, 1297fcaf9f91SMike Silbersack 0, 0, EHOSTUNREACH, 0, 12983b8123b7SJesper Skriver ENOPROTOOPT, ECONNREFUSED 1299df8bae1dSRodney W. Grimes }; 1300df8bae1dSRodney W. Grimes 1301df8bae1dSRodney W. Grimes /* 1302df8bae1dSRodney W. Grimes * Forward a packet. If some error occurs return the sender 1303df8bae1dSRodney W. Grimes * an icmp packet. Note we can't always generate a meaningful 1304df8bae1dSRodney W. Grimes * icmp message because icmp doesn't have a large enough repertoire 1305df8bae1dSRodney W. Grimes * of codes and types. 1306df8bae1dSRodney W. Grimes * 1307df8bae1dSRodney W. Grimes * If not forwarding, just drop the packet. This could be confusing 1308df8bae1dSRodney W. Grimes * if ipforwarding was zero but some routing protocol was advancing 1309df8bae1dSRodney W. Grimes * us as a gateway to somewhere. However, we must let the routing 1310df8bae1dSRodney W. Grimes * protocol deal with that. 1311df8bae1dSRodney W. Grimes * 1312df8bae1dSRodney W. Grimes * The srcrt parameter indicates whether the packet is being forwarded 1313df8bae1dSRodney W. Grimes * via a source route. 1314df8bae1dSRodney W. Grimes */ 13159b932e9eSAndre Oppermann void 13169b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt) 1317df8bae1dSRodney W. Grimes { 13188b615593SMarko Zec INIT_VNET_INET(curvnet); 13192b25acc1SLuigi Rizzo struct ip *ip = mtod(m, struct ip *); 13209b932e9eSAndre Oppermann struct in_ifaddr *ia = NULL; 1321df8bae1dSRodney W. Grimes struct mbuf *mcopy; 13229b932e9eSAndre Oppermann struct in_addr dest; 1323b835b6feSBjoern A. Zeeb struct route ro; 1324c773494eSAndre Oppermann int error, type = 0, code = 0, mtu = 0; 13253efc3014SJulian Elischer 13269b932e9eSAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1327603724d3SBjoern A. Zeeb V_ipstat.ips_cantforward++; 1328df8bae1dSRodney W. Grimes m_freem(m); 1329df8bae1dSRodney W. Grimes return; 1330df8bae1dSRodney W. Grimes } 13311b968362SDag-Erling Smørgrav #ifdef IPSTEALTH 1332603724d3SBjoern A. Zeeb if (!V_ipstealth) { 13331b968362SDag-Erling Smørgrav #endif 1334df8bae1dSRodney W. Grimes if (ip->ip_ttl <= IPTTLDEC) { 13351b968362SDag-Erling Smørgrav icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 133602c1c707SAndre Oppermann 0, 0); 1337df8bae1dSRodney W. Grimes return; 1338df8bae1dSRodney W. Grimes } 13391b968362SDag-Erling Smørgrav #ifdef IPSTEALTH 13401b968362SDag-Erling Smørgrav } 13411b968362SDag-Erling Smørgrav #endif 1342df8bae1dSRodney W. Grimes 13438b07e49aSJulian Elischer ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m)); 1344d23d475fSGuido van Rooij if (!srcrt && ia == NULL) { 134502c1c707SAndre Oppermann icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 1346df8bae1dSRodney W. Grimes return; 134702c1c707SAndre Oppermann } 1348df8bae1dSRodney W. Grimes 1349df8bae1dSRodney W. Grimes /* 1350bfef7ed4SIan Dowse * Save the IP header and at most 8 bytes of the payload, 1351bfef7ed4SIan Dowse * in case we need to generate an ICMP message to the src. 1352bfef7ed4SIan Dowse * 13534d2e3692SLuigi Rizzo * XXX this can be optimized a lot by saving the data in a local 13544d2e3692SLuigi Rizzo * buffer on the stack (72 bytes at most), and only allocating the 13554d2e3692SLuigi Rizzo * mbuf if really necessary. The vast majority of the packets 13564d2e3692SLuigi Rizzo * are forwarded without having to send an ICMP back (either 13574d2e3692SLuigi Rizzo * because unnecessary, or because rate limited), so we are 13584d2e3692SLuigi Rizzo * really we are wasting a lot of work here. 13594d2e3692SLuigi Rizzo * 1360bfef7ed4SIan Dowse * We don't use m_copy() because it might return a reference 1361bfef7ed4SIan Dowse * to a shared cluster. Both this function and ip_output() 1362bfef7ed4SIan Dowse * assume exclusive access to the IP header in `m', so any 1363bfef7ed4SIan Dowse * data in a cluster may change before we reach icmp_error(). 1364df8bae1dSRodney W. Grimes */ 1365780b2f69SAndre Oppermann MGETHDR(mcopy, M_DONTWAIT, m->m_type); 1366a163d034SWarner Losh if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) { 13679967cafcSSam Leffler /* 13689967cafcSSam Leffler * It's probably ok if the pkthdr dup fails (because 13699967cafcSSam Leffler * the deep copy of the tag chain failed), but for now 13709967cafcSSam Leffler * be conservative and just discard the copy since 13719967cafcSSam Leffler * code below may some day want the tags. 13729967cafcSSam Leffler */ 13739967cafcSSam Leffler m_free(mcopy); 13749967cafcSSam Leffler mcopy = NULL; 13759967cafcSSam Leffler } 1376bfef7ed4SIan Dowse if (mcopy != NULL) { 1377780b2f69SAndre Oppermann mcopy->m_len = min(ip->ip_len, M_TRAILINGSPACE(mcopy)); 1378e6b0a570SBruce M Simpson mcopy->m_pkthdr.len = mcopy->m_len; 1379bfef7ed4SIan Dowse m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t)); 1380bfef7ed4SIan Dowse } 138104287599SRuslan Ermilov 138204287599SRuslan Ermilov #ifdef IPSTEALTH 1383603724d3SBjoern A. Zeeb if (!V_ipstealth) { 138404287599SRuslan Ermilov #endif 138504287599SRuslan Ermilov ip->ip_ttl -= IPTTLDEC; 138604287599SRuslan Ermilov #ifdef IPSTEALTH 138704287599SRuslan Ermilov } 138804287599SRuslan Ermilov #endif 1389df8bae1dSRodney W. Grimes 1390df8bae1dSRodney W. Grimes /* 1391df8bae1dSRodney W. Grimes * If forwarding packet using same interface that it came in on, 1392df8bae1dSRodney W. Grimes * perhaps should send a redirect to sender to shortcut a hop. 1393df8bae1dSRodney W. Grimes * Only send redirect if source is sending directly to us, 1394df8bae1dSRodney W. Grimes * and if packet was not source routed (or has any options). 1395df8bae1dSRodney W. Grimes * Also, don't send redirect if forwarding using a default route 1396df8bae1dSRodney W. Grimes * or a route modified by a redirect. 1397df8bae1dSRodney W. Grimes */ 13989b932e9eSAndre Oppermann dest.s_addr = 0; 1399603724d3SBjoern A. Zeeb if (!srcrt && V_ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) { 140002c1c707SAndre Oppermann struct sockaddr_in *sin; 140102c1c707SAndre Oppermann struct rtentry *rt; 140202c1c707SAndre Oppermann 14030cfbbe3bSAndre Oppermann bzero(&ro, sizeof(ro)); 140402c1c707SAndre Oppermann sin = (struct sockaddr_in *)&ro.ro_dst; 140502c1c707SAndre Oppermann sin->sin_family = AF_INET; 140602c1c707SAndre Oppermann sin->sin_len = sizeof(*sin); 14079b932e9eSAndre Oppermann sin->sin_addr = ip->ip_dst; 14086e6b3f7cSQing Li in_rtalloc_ign(&ro, 0, M_GETFIB(m)); 140902c1c707SAndre Oppermann 141002c1c707SAndre Oppermann rt = ro.ro_rt; 141102c1c707SAndre Oppermann 141202c1c707SAndre Oppermann if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 14139b932e9eSAndre Oppermann satosin(rt_key(rt))->sin_addr.s_addr != 0) { 1414df8bae1dSRodney W. Grimes #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1415df8bae1dSRodney W. Grimes u_long src = ntohl(ip->ip_src.s_addr); 1416df8bae1dSRodney W. Grimes 1417df8bae1dSRodney W. Grimes if (RTA(rt) && 1418df8bae1dSRodney W. Grimes (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1419df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_GATEWAY) 14209b932e9eSAndre Oppermann dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr; 1421df8bae1dSRodney W. Grimes else 14229b932e9eSAndre Oppermann dest.s_addr = ip->ip_dst.s_addr; 1423df8bae1dSRodney W. Grimes /* Router requirements says to only send host redirects */ 1424df8bae1dSRodney W. Grimes type = ICMP_REDIRECT; 1425df8bae1dSRodney W. Grimes code = ICMP_REDIRECT_HOST; 1426df8bae1dSRodney W. Grimes } 1427df8bae1dSRodney W. Grimes } 142802c1c707SAndre Oppermann if (rt) 142902c1c707SAndre Oppermann RTFREE(rt); 143002c1c707SAndre Oppermann } 1431df8bae1dSRodney W. Grimes 1432b835b6feSBjoern A. Zeeb /* 1433b835b6feSBjoern A. Zeeb * Try to cache the route MTU from ip_output so we can consider it for 1434b835b6feSBjoern A. Zeeb * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191. 1435b835b6feSBjoern A. Zeeb */ 1436b835b6feSBjoern A. Zeeb bzero(&ro, sizeof(ro)); 1437b835b6feSBjoern A. Zeeb 1438b835b6feSBjoern A. Zeeb error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL); 1439b835b6feSBjoern A. Zeeb 1440b835b6feSBjoern A. Zeeb if (error == EMSGSIZE && ro.ro_rt) 1441b835b6feSBjoern A. Zeeb mtu = ro.ro_rt->rt_rmx.rmx_mtu; 1442b835b6feSBjoern A. Zeeb if (ro.ro_rt) 1443b835b6feSBjoern A. Zeeb RTFREE(ro.ro_rt); 1444b835b6feSBjoern A. Zeeb 1445df8bae1dSRodney W. Grimes if (error) 1446603724d3SBjoern A. Zeeb V_ipstat.ips_cantforward++; 1447df8bae1dSRodney W. Grimes else { 1448603724d3SBjoern A. Zeeb V_ipstat.ips_forward++; 1449df8bae1dSRodney W. Grimes if (type) 1450603724d3SBjoern A. Zeeb V_ipstat.ips_redirectsent++; 1451df8bae1dSRodney W. Grimes else { 14529188b4a1SAndre Oppermann if (mcopy) 1453df8bae1dSRodney W. Grimes m_freem(mcopy); 1454df8bae1dSRodney W. Grimes return; 1455df8bae1dSRodney W. Grimes } 1456df8bae1dSRodney W. Grimes } 1457df8bae1dSRodney W. Grimes if (mcopy == NULL) 1458df8bae1dSRodney W. Grimes return; 1459df8bae1dSRodney W. Grimes 1460df8bae1dSRodney W. Grimes switch (error) { 1461df8bae1dSRodney W. Grimes 1462df8bae1dSRodney W. Grimes case 0: /* forwarded, but need redirect */ 1463df8bae1dSRodney W. Grimes /* type, code set above */ 1464df8bae1dSRodney W. Grimes break; 1465df8bae1dSRodney W. Grimes 1466df8bae1dSRodney W. Grimes case ENETUNREACH: /* shouldn't happen, checked above */ 1467df8bae1dSRodney W. Grimes case EHOSTUNREACH: 1468df8bae1dSRodney W. Grimes case ENETDOWN: 1469df8bae1dSRodney W. Grimes case EHOSTDOWN: 1470df8bae1dSRodney W. Grimes default: 1471df8bae1dSRodney W. Grimes type = ICMP_UNREACH; 1472df8bae1dSRodney W. Grimes code = ICMP_UNREACH_HOST; 1473df8bae1dSRodney W. Grimes break; 1474df8bae1dSRodney W. Grimes 1475df8bae1dSRodney W. Grimes case EMSGSIZE: 1476df8bae1dSRodney W. Grimes type = ICMP_UNREACH; 1477df8bae1dSRodney W. Grimes code = ICMP_UNREACH_NEEDFRAG; 14781dfcf0d2SAndre Oppermann 1479b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 1480b835b6feSBjoern A. Zeeb /* 1481b835b6feSBjoern A. Zeeb * If IPsec is configured for this path, 1482b835b6feSBjoern A. Zeeb * override any possibly mtu value set by ip_output. 1483b835b6feSBjoern A. Zeeb */ 1484b835b6feSBjoern A. Zeeb mtu = ip_ipsec_mtu(m, mtu); 1485b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 14869b932e9eSAndre Oppermann /* 1487b835b6feSBjoern A. Zeeb * If the MTU was set before make sure we are below the 1488b835b6feSBjoern A. Zeeb * interface MTU. 1489ab48768bSAndre Oppermann * If the MTU wasn't set before use the interface mtu or 1490ab48768bSAndre Oppermann * fall back to the next smaller mtu step compared to the 1491ab48768bSAndre Oppermann * current packet size. 14929b932e9eSAndre Oppermann */ 1493b835b6feSBjoern A. Zeeb if (mtu != 0) { 1494b835b6feSBjoern A. Zeeb if (ia != NULL) 1495b835b6feSBjoern A. Zeeb mtu = min(mtu, ia->ia_ifp->if_mtu); 1496b835b6feSBjoern A. Zeeb } else { 1497ab48768bSAndre Oppermann if (ia != NULL) 1498c773494eSAndre Oppermann mtu = ia->ia_ifp->if_mtu; 1499ab48768bSAndre Oppermann else 1500ab48768bSAndre Oppermann mtu = ip_next_mtu(ip->ip_len, 0); 1501ab48768bSAndre Oppermann } 1502603724d3SBjoern A. Zeeb V_ipstat.ips_cantfrag++; 1503df8bae1dSRodney W. Grimes break; 1504df8bae1dSRodney W. Grimes 1505df8bae1dSRodney W. Grimes case ENOBUFS: 1506df285b3dSMike Silbersack /* 1507df285b3dSMike Silbersack * A router should not generate ICMP_SOURCEQUENCH as 1508df285b3dSMike Silbersack * required in RFC1812 Requirements for IP Version 4 Routers. 1509df285b3dSMike Silbersack * Source quench could be a big problem under DoS attacks, 1510df285b3dSMike Silbersack * or if the underlying interface is rate-limited. 1511df285b3dSMike Silbersack * Those who need source quench packets may re-enable them 1512df285b3dSMike Silbersack * via the net.inet.ip.sendsourcequench sysctl. 1513df285b3dSMike Silbersack */ 1514603724d3SBjoern A. Zeeb if (V_ip_sendsourcequench == 0) { 1515df285b3dSMike Silbersack m_freem(mcopy); 1516df285b3dSMike Silbersack return; 1517df285b3dSMike Silbersack } else { 1518df8bae1dSRodney W. Grimes type = ICMP_SOURCEQUENCH; 1519df8bae1dSRodney W. Grimes code = 0; 1520df285b3dSMike Silbersack } 1521df8bae1dSRodney W. Grimes break; 15223a06e3e0SRuslan Ermilov 15233a06e3e0SRuslan Ermilov case EACCES: /* ipfw denied packet */ 15243a06e3e0SRuslan Ermilov m_freem(mcopy); 15253a06e3e0SRuslan Ermilov return; 1526df8bae1dSRodney W. Grimes } 1527c773494eSAndre Oppermann icmp_error(mcopy, type, code, dest.s_addr, mtu); 1528df8bae1dSRodney W. Grimes } 1529df8bae1dSRodney W. Grimes 153082c23ebaSBill Fenner void 1531f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip, 1532f2565d68SRobert Watson struct mbuf *m) 153382c23ebaSBill Fenner { 15348b615593SMarko Zec INIT_VNET_NET(inp->inp_vnet); 15358b615593SMarko Zec 1536be8a62e8SPoul-Henning Kamp if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) { 1537be8a62e8SPoul-Henning Kamp struct bintime bt; 1538be8a62e8SPoul-Henning Kamp 1539be8a62e8SPoul-Henning Kamp bintime(&bt); 1540be8a62e8SPoul-Henning Kamp if (inp->inp_socket->so_options & SO_BINTIME) { 1541be8a62e8SPoul-Henning Kamp *mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt), 1542be8a62e8SPoul-Henning Kamp SCM_BINTIME, SOL_SOCKET); 1543be8a62e8SPoul-Henning Kamp if (*mp) 1544be8a62e8SPoul-Henning Kamp mp = &(*mp)->m_next; 1545be8a62e8SPoul-Henning Kamp } 154682c23ebaSBill Fenner if (inp->inp_socket->so_options & SO_TIMESTAMP) { 154782c23ebaSBill Fenner struct timeval tv; 154882c23ebaSBill Fenner 1549be8a62e8SPoul-Henning Kamp bintime2timeval(&bt, &tv); 155082c23ebaSBill Fenner *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 155182c23ebaSBill Fenner SCM_TIMESTAMP, SOL_SOCKET); 155282c23ebaSBill Fenner if (*mp) 155382c23ebaSBill Fenner mp = &(*mp)->m_next; 15544cc20ab1SSeigo Tanimura } 1555be8a62e8SPoul-Henning Kamp } 155682c23ebaSBill Fenner if (inp->inp_flags & INP_RECVDSTADDR) { 155782c23ebaSBill Fenner *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 155882c23ebaSBill Fenner sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 155982c23ebaSBill Fenner if (*mp) 156082c23ebaSBill Fenner mp = &(*mp)->m_next; 156182c23ebaSBill Fenner } 15624957466bSMatthew N. Dodd if (inp->inp_flags & INP_RECVTTL) { 15634957466bSMatthew N. Dodd *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl, 15644957466bSMatthew N. Dodd sizeof(u_char), IP_RECVTTL, IPPROTO_IP); 15654957466bSMatthew N. Dodd if (*mp) 15664957466bSMatthew N. Dodd mp = &(*mp)->m_next; 15674957466bSMatthew N. Dodd } 156882c23ebaSBill Fenner #ifdef notyet 156982c23ebaSBill Fenner /* XXX 157082c23ebaSBill Fenner * Moving these out of udp_input() made them even more broken 157182c23ebaSBill Fenner * than they already were. 157282c23ebaSBill Fenner */ 157382c23ebaSBill Fenner /* options were tossed already */ 157482c23ebaSBill Fenner if (inp->inp_flags & INP_RECVOPTS) { 157582c23ebaSBill Fenner *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 157682c23ebaSBill Fenner sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 157782c23ebaSBill Fenner if (*mp) 157882c23ebaSBill Fenner mp = &(*mp)->m_next; 157982c23ebaSBill Fenner } 158082c23ebaSBill Fenner /* ip_srcroute doesn't do what we want here, need to fix */ 158182c23ebaSBill Fenner if (inp->inp_flags & INP_RECVRETOPTS) { 1582e0982661SAndre Oppermann *mp = sbcreatecontrol((caddr_t) ip_srcroute(m), 158382c23ebaSBill Fenner sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 158482c23ebaSBill Fenner if (*mp) 158582c23ebaSBill Fenner mp = &(*mp)->m_next; 158682c23ebaSBill Fenner } 158782c23ebaSBill Fenner #endif 158882c23ebaSBill Fenner if (inp->inp_flags & INP_RECVIF) { 1589d314ad7bSJulian Elischer struct ifnet *ifp; 1590d314ad7bSJulian Elischer struct sdlbuf { 159182c23ebaSBill Fenner struct sockaddr_dl sdl; 1592d314ad7bSJulian Elischer u_char pad[32]; 1593d314ad7bSJulian Elischer } sdlbuf; 1594d314ad7bSJulian Elischer struct sockaddr_dl *sdp; 1595d314ad7bSJulian Elischer struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 159682c23ebaSBill Fenner 1597d314ad7bSJulian Elischer if (((ifp = m->m_pkthdr.rcvif)) 1598603724d3SBjoern A. Zeeb && ( ifp->if_index && (ifp->if_index <= V_if_index))) { 15994a0d6638SRuslan Ermilov sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr; 1600d314ad7bSJulian Elischer /* 1601d314ad7bSJulian Elischer * Change our mind and don't try copy. 1602d314ad7bSJulian Elischer */ 1603d314ad7bSJulian Elischer if ((sdp->sdl_family != AF_LINK) 1604d314ad7bSJulian Elischer || (sdp->sdl_len > sizeof(sdlbuf))) { 1605d314ad7bSJulian Elischer goto makedummy; 1606d314ad7bSJulian Elischer } 1607d314ad7bSJulian Elischer bcopy(sdp, sdl2, sdp->sdl_len); 1608d314ad7bSJulian Elischer } else { 1609d314ad7bSJulian Elischer makedummy: 1610d314ad7bSJulian Elischer sdl2->sdl_len 1611d314ad7bSJulian Elischer = offsetof(struct sockaddr_dl, sdl_data[0]); 1612d314ad7bSJulian Elischer sdl2->sdl_family = AF_LINK; 1613d314ad7bSJulian Elischer sdl2->sdl_index = 0; 1614d314ad7bSJulian Elischer sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 1615d314ad7bSJulian Elischer } 1616d314ad7bSJulian Elischer *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, 161782c23ebaSBill Fenner IP_RECVIF, IPPROTO_IP); 161882c23ebaSBill Fenner if (*mp) 161982c23ebaSBill Fenner mp = &(*mp)->m_next; 162082c23ebaSBill Fenner } 162182c23ebaSBill Fenner } 162282c23ebaSBill Fenner 16234d2e3692SLuigi Rizzo /* 162430916a2dSRobert Watson * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the 162530916a2dSRobert Watson * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on 162630916a2dSRobert Watson * locking. This code remains in ip_input.c as ip_mroute.c is optionally 162730916a2dSRobert Watson * compiled. 16284d2e3692SLuigi Rizzo */ 1629df8bae1dSRodney W. Grimes int 1630f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so) 1631f0068c4aSGarrett Wollman { 16328b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 16338b615593SMarko Zec 1634f0068c4aSGarrett Wollman if (so->so_type != SOCK_RAW || 1635f0068c4aSGarrett Wollman so->so_proto->pr_protocol != IPPROTO_RSVP) 1636f0068c4aSGarrett Wollman return EOPNOTSUPP; 1637f0068c4aSGarrett Wollman 1638603724d3SBjoern A. Zeeb if (V_ip_rsvpd != NULL) 1639f0068c4aSGarrett Wollman return EADDRINUSE; 1640f0068c4aSGarrett Wollman 1641603724d3SBjoern A. Zeeb V_ip_rsvpd = so; 16421c5de19aSGarrett Wollman /* 16431c5de19aSGarrett Wollman * This may seem silly, but we need to be sure we don't over-increment 16441c5de19aSGarrett Wollman * the RSVP counter, in case something slips up. 16451c5de19aSGarrett Wollman */ 1646603724d3SBjoern A. Zeeb if (!V_ip_rsvp_on) { 1647603724d3SBjoern A. Zeeb V_ip_rsvp_on = 1; 1648603724d3SBjoern A. Zeeb V_rsvp_on++; 16491c5de19aSGarrett Wollman } 1650f0068c4aSGarrett Wollman 1651f0068c4aSGarrett Wollman return 0; 1652f0068c4aSGarrett Wollman } 1653f0068c4aSGarrett Wollman 1654f0068c4aSGarrett Wollman int 1655f0068c4aSGarrett Wollman ip_rsvp_done(void) 1656f0068c4aSGarrett Wollman { 16578b615593SMarko Zec INIT_VNET_INET(curvnet); 16588b615593SMarko Zec 1659603724d3SBjoern A. Zeeb V_ip_rsvpd = NULL; 16601c5de19aSGarrett Wollman /* 16611c5de19aSGarrett Wollman * This may seem silly, but we need to be sure we don't over-decrement 16621c5de19aSGarrett Wollman * the RSVP counter, in case something slips up. 16631c5de19aSGarrett Wollman */ 1664603724d3SBjoern A. Zeeb if (V_ip_rsvp_on) { 1665603724d3SBjoern A. Zeeb V_ip_rsvp_on = 0; 1666603724d3SBjoern A. Zeeb V_rsvp_on--; 16671c5de19aSGarrett Wollman } 1668f0068c4aSGarrett Wollman return 0; 1669f0068c4aSGarrett Wollman } 1670bbb4330bSLuigi Rizzo 1671bbb4330bSLuigi Rizzo void 1672bbb4330bSLuigi Rizzo rsvp_input(struct mbuf *m, int off) /* XXX must fixup manually */ 1673bbb4330bSLuigi Rizzo { 16748b615593SMarko Zec INIT_VNET_INET(curvnet); 16758b615593SMarko Zec 1676bbb4330bSLuigi Rizzo if (rsvp_input_p) { /* call the real one if loaded */ 1677bbb4330bSLuigi Rizzo rsvp_input_p(m, off); 1678bbb4330bSLuigi Rizzo return; 1679bbb4330bSLuigi Rizzo } 1680bbb4330bSLuigi Rizzo 1681bbb4330bSLuigi Rizzo /* Can still get packets with rsvp_on = 0 if there is a local member 1682bbb4330bSLuigi Rizzo * of the group to which the RSVP packet is addressed. But in this 1683bbb4330bSLuigi Rizzo * case we want to throw the packet away. 1684bbb4330bSLuigi Rizzo */ 1685bbb4330bSLuigi Rizzo 1686603724d3SBjoern A. Zeeb if (!V_rsvp_on) { 1687bbb4330bSLuigi Rizzo m_freem(m); 1688bbb4330bSLuigi Rizzo return; 1689bbb4330bSLuigi Rizzo } 1690bbb4330bSLuigi Rizzo 1691603724d3SBjoern A. Zeeb if (V_ip_rsvpd != NULL) { 1692bbb4330bSLuigi Rizzo rip_input(m, off); 1693bbb4330bSLuigi Rizzo return; 1694bbb4330bSLuigi Rizzo } 1695bbb4330bSLuigi Rizzo /* Drop the packet */ 1696bbb4330bSLuigi Rizzo m_freem(m); 1697bbb4330bSLuigi Rizzo } 1698