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" 40b8bc95cdSAdrian Chadd #include "opt_rss.h" 4174a9466cSGary Palmer 42df8bae1dSRodney W. Grimes #include <sys/param.h> 43df8bae1dSRodney W. Grimes #include <sys/systm.h> 44df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 45b715f178SLuigi Rizzo #include <sys/malloc.h> 46df8bae1dSRodney W. Grimes #include <sys/domain.h> 47df8bae1dSRodney W. Grimes #include <sys/protosw.h> 48df8bae1dSRodney W. Grimes #include <sys/socket.h> 49df8bae1dSRodney W. Grimes #include <sys/time.h> 50df8bae1dSRodney W. Grimes #include <sys/kernel.h> 51385195c0SMarko Zec #include <sys/lock.h> 52385195c0SMarko Zec #include <sys/rwlock.h> 5357f60867SMark Johnston #include <sys/sdt.h> 541025071fSGarrett Wollman #include <sys/syslog.h> 55b5e8ce9fSBruce Evans #include <sys/sysctl.h> 56df8bae1dSRodney W. Grimes 57c85540ddSAndrey A. Chernov #include <net/pfil.h> 58df8bae1dSRodney W. Grimes #include <net/if.h> 599494d596SBrooks Davis #include <net/if_types.h> 60d314ad7bSJulian Elischer #include <net/if_var.h> 6182c23ebaSBill Fenner #include <net/if_dl.h> 62df8bae1dSRodney W. Grimes #include <net/route.h> 63748e0b0aSGarrett Wollman #include <net/netisr.h> 64b2bdc62aSAdrian Chadd #include <net/rss_config.h> 654b79449eSBjoern A. Zeeb #include <net/vnet.h> 66df8bae1dSRodney W. Grimes 67df8bae1dSRodney W. Grimes #include <netinet/in.h> 6857f60867SMark Johnston #include <netinet/in_kdtrace.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> 74eddfbb76SRobert Watson #include <netinet/ip_fw.h> 75df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h> 76ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 7758938916SGarrett Wollman #include <machine/in_cksum.h> 78a9771948SGleb Smirnoff #include <netinet/ip_carp.h> 79b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 801dfcf0d2SAndre Oppermann #include <netinet/ip_ipsec.h> 81b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 82b8bc95cdSAdrian Chadd #include <netinet/in_rss.h> 83df8bae1dSRodney W. Grimes 84f0068c4aSGarrett Wollman #include <sys/socketvar.h> 856ddbf1e2SGary Palmer 86aed55708SRobert Watson #include <security/mac/mac_framework.h> 87aed55708SRobert Watson 88d2035ffbSEd Maste #ifdef CTASSERT 89d2035ffbSEd Maste CTASSERT(sizeof(struct ip) == 20); 90d2035ffbSEd Maste #endif 91d2035ffbSEd Maste 92f89d4c3aSAndre Oppermann struct rwlock in_ifaddr_lock; 9364aeca7bSRobert Watson RW_SYSINIT(in_ifaddr_lock, &in_ifaddr_lock, "in_ifaddr_lock"); 94f0068c4aSGarrett Wollman 9582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, rsvp_on); 9682cea7e6SBjoern A. Zeeb 9782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ipforwarding); 986df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_VNET | CTLFLAG_RW, 99eddfbb76SRobert Watson &VNET_NAME(ipforwarding), 0, 1008b615593SMarko Zec "Enable IP forwarding between interfaces"); 1010312fbe9SPoul-Henning Kamp 1023e288e62SDimitry Andric static VNET_DEFINE(int, ipsendredirects) = 1; /* XXX */ 10382cea7e6SBjoern A. Zeeb #define V_ipsendredirects VNET(ipsendredirects) 1046df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_VNET | CTLFLAG_RW, 105eddfbb76SRobert Watson &VNET_NAME(ipsendredirects), 0, 1068b615593SMarko Zec "Enable sending IP redirects"); 1070312fbe9SPoul-Henning Kamp 108823db0e9SDon Lewis /* 109823db0e9SDon Lewis * XXX - Setting ip_checkinterface mostly implements the receive side of 110823db0e9SDon Lewis * the Strong ES model described in RFC 1122, but since the routing table 111a8f12100SDon Lewis * and transmit implementation do not implement the Strong ES model, 112823db0e9SDon Lewis * setting this to 1 results in an odd hybrid. 1133f67c834SDon Lewis * 114a8f12100SDon Lewis * XXX - ip_checkinterface currently must be disabled if you use ipnat 115a8f12100SDon Lewis * to translate the destination address to another local interface. 1163f67c834SDon Lewis * 1173f67c834SDon Lewis * XXX - ip_checkinterface must be disabled if you add IP aliases 1183f67c834SDon Lewis * to the loopback interface instead of the interface where the 1193f67c834SDon Lewis * packets for those addresses are received. 120823db0e9SDon Lewis */ 1213e288e62SDimitry Andric static VNET_DEFINE(int, ip_checkinterface); 12282cea7e6SBjoern A. Zeeb #define V_ip_checkinterface VNET(ip_checkinterface) 1236df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_VNET | CTLFLAG_RW, 124eddfbb76SRobert Watson &VNET_NAME(ip_checkinterface), 0, 1258b615593SMarko Zec "Verify packet arrives on correct interface"); 126b3e95d4eSJonathan Lemon 1270b4b0b0fSJulian Elischer VNET_DEFINE(struct pfil_head, inet_pfil_hook); /* Packet filter hooks */ 128df8bae1dSRodney W. Grimes 129d4b5cae4SRobert Watson static struct netisr_handler ip_nh = { 130d4b5cae4SRobert Watson .nh_name = "ip", 131d4b5cae4SRobert Watson .nh_handler = ip_input, 132d4b5cae4SRobert Watson .nh_proto = NETISR_IP, 133b8bc95cdSAdrian Chadd #ifdef RSS 134b8bc95cdSAdrian Chadd .nh_m2cpuid = rss_soft_m2cpuid, 135b8bc95cdSAdrian Chadd .nh_policy = NETISR_POLICY_CPU, 136b8bc95cdSAdrian Chadd .nh_dispatch = NETISR_DISPATCH_HYBRID, 137b8bc95cdSAdrian Chadd #else 138d4b5cae4SRobert Watson .nh_policy = NETISR_POLICY_FLOW, 139b8bc95cdSAdrian Chadd #endif 140d4b5cae4SRobert Watson }; 141ca925d9cSJonathan Lemon 142b8bc95cdSAdrian Chadd #ifdef RSS 143b8bc95cdSAdrian Chadd /* 144b8bc95cdSAdrian Chadd * Directly dispatched frames are currently assumed 145b8bc95cdSAdrian Chadd * to have a flowid already calculated. 146b8bc95cdSAdrian Chadd * 147b8bc95cdSAdrian Chadd * It should likely have something that assert it 148b8bc95cdSAdrian Chadd * actually has valid flow details. 149b8bc95cdSAdrian Chadd */ 150b8bc95cdSAdrian Chadd static struct netisr_handler ip_direct_nh = { 151b8bc95cdSAdrian Chadd .nh_name = "ip_direct", 152b8bc95cdSAdrian Chadd .nh_handler = ip_direct_input, 153b8bc95cdSAdrian Chadd .nh_proto = NETISR_IP_DIRECT, 154b8bc95cdSAdrian Chadd .nh_m2cpuid = rss_m2cpuid, 155b8bc95cdSAdrian Chadd .nh_policy = NETISR_POLICY_CPU, 156b8bc95cdSAdrian Chadd .nh_dispatch = NETISR_DISPATCH_HYBRID, 157b8bc95cdSAdrian Chadd }; 158b8bc95cdSAdrian Chadd #endif 159b8bc95cdSAdrian Chadd 160df8bae1dSRodney W. Grimes extern struct domain inetdomain; 161f0ffb944SJulian Elischer extern struct protosw inetsw[]; 162df8bae1dSRodney W. Grimes u_char ip_protox[IPPROTO_MAX]; 16382cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead); /* first inet address */ 16482cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); /* inet addr hash table */ 16582cea7e6SBjoern A. Zeeb VNET_DEFINE(u_long, in_ifaddrhmask); /* mask for hash table */ 166ca925d9cSJonathan Lemon 1673e288e62SDimitry Andric static VNET_DEFINE(uma_zone_t, ipq_zone); 16882cea7e6SBjoern A. Zeeb #define V_ipq_zone VNET(ipq_zone) 169*e3c2c634SGleb Smirnoff struct ipqbucket { 170*e3c2c634SGleb Smirnoff TAILQ_HEAD(ipqhead, ipq) head; 171*e3c2c634SGleb Smirnoff struct mtx lock; 172*e3c2c634SGleb Smirnoff }; 173*e3c2c634SGleb Smirnoff static VNET_DEFINE(struct ipqbucket, ipq[IPREASS_NHASH]); 17482cea7e6SBjoern A. Zeeb #define V_ipq VNET(ipq) 175*e3c2c634SGleb Smirnoff #define IPQ_LOCK(i) mtx_lock(&V_ipq[i].lock) 176*e3c2c634SGleb Smirnoff #define IPQ_UNLOCK(i) mtx_unlock(&V_ipq[i].lock) 177f23b4c91SGarrett Wollman 178d248c7d7SRobert Watson static void maxnipq_update(void); 1794f590175SPaul Saab static void ipq_zone_change(void *); 1803de5805bSGleb Smirnoff static void ip_drain_vnet(void); 18155c28800SGleb Smirnoff static void ipq_free(struct ipqhead *, struct ipq *); 18255c28800SGleb Smirnoff 18355c28800SGleb Smirnoff static inline void 18455c28800SGleb Smirnoff ipq_timeout(struct ipqhead *head, struct ipq *fp) 18555c28800SGleb Smirnoff { 18655c28800SGleb Smirnoff 18755c28800SGleb Smirnoff IPSTAT_ADD(ips_fragtimeout, fp->ipq_nfrags); 18855c28800SGleb Smirnoff ipq_free(head, fp); 18955c28800SGleb Smirnoff } 19055c28800SGleb Smirnoff 19155c28800SGleb Smirnoff static inline void 19255c28800SGleb Smirnoff ipq_drop(struct ipqhead *head, struct ipq *fp) 19355c28800SGleb Smirnoff { 19455c28800SGleb Smirnoff 19555c28800SGleb Smirnoff IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags); 19655c28800SGleb Smirnoff ipq_free(head, fp); 19755c28800SGleb Smirnoff } 198d248c7d7SRobert Watson 1993e288e62SDimitry Andric static VNET_DEFINE(int, maxnipq); /* Administrative limit on # reass queues. */ 2003e288e62SDimitry Andric static VNET_DEFINE(int, nipq); /* Total # of reass queues */ 20182cea7e6SBjoern A. Zeeb #define V_maxnipq VNET(maxnipq) 20282cea7e6SBjoern A. Zeeb #define V_nipq VNET(nipq) 2036df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_VNET | CTLFLAG_RD, 204eddfbb76SRobert Watson &VNET_NAME(nipq), 0, 2058b615593SMarko Zec "Current number of IPv4 fragment reassembly queue entries"); 206d248c7d7SRobert Watson 2073e288e62SDimitry Andric static VNET_DEFINE(int, maxfragsperpacket); 20882cea7e6SBjoern A. Zeeb #define V_maxfragsperpacket VNET(maxfragsperpacket) 2096df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_VNET | CTLFLAG_RW, 210eddfbb76SRobert Watson &VNET_NAME(maxfragsperpacket), 0, 211d248c7d7SRobert Watson "Maximum number of IPv4 fragments allowed per packet"); 212d248c7d7SRobert Watson 2130312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU 2140312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, 2153d177f46SBill Fumerola &ip_mtu, 0, "Default MTU"); 2160312fbe9SPoul-Henning Kamp #endif 2170312fbe9SPoul-Henning Kamp 2181b968362SDag-Erling Smørgrav #ifdef IPSTEALTH 21982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ipstealth); 2206df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_VNET | CTLFLAG_RW, 221eddfbb76SRobert Watson &VNET_NAME(ipstealth), 0, 222eddfbb76SRobert Watson "IP stealth mode, no TTL decrementation on forwarding"); 2231b968362SDag-Erling Smørgrav #endif 224eddfbb76SRobert Watson 225315e3e38SRobert Watson /* 2265da0521fSAndrey V. Elsukov * IP statistics are stored in the "array" of counter(9)s. 2275923c293SGleb Smirnoff */ 2285da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct ipstat, ipstat); 2295da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(ipstat); 2305da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_ip, IPCTL_STATS, stats, struct ipstat, ipstat, 2315da0521fSAndrey V. Elsukov "IP statistics (struct ipstat, netinet/ip_var.h)"); 2325923c293SGleb Smirnoff 2335923c293SGleb Smirnoff #ifdef VIMAGE 2345da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(ipstat); 2355923c293SGleb Smirnoff #endif /* VIMAGE */ 2365923c293SGleb Smirnoff 2375923c293SGleb Smirnoff /* 238315e3e38SRobert Watson * Kernel module interface for updating ipstat. The argument is an index 2395923c293SGleb Smirnoff * into ipstat treated as an array. 240315e3e38SRobert Watson */ 241315e3e38SRobert Watson void 242315e3e38SRobert Watson kmod_ipstat_inc(int statnum) 243315e3e38SRobert Watson { 244315e3e38SRobert Watson 2455da0521fSAndrey V. Elsukov counter_u64_add(VNET(ipstat)[statnum], 1); 246315e3e38SRobert Watson } 247315e3e38SRobert Watson 248315e3e38SRobert Watson void 249315e3e38SRobert Watson kmod_ipstat_dec(int statnum) 250315e3e38SRobert Watson { 251315e3e38SRobert Watson 2525da0521fSAndrey V. Elsukov counter_u64_add(VNET(ipstat)[statnum], -1); 253315e3e38SRobert Watson } 254315e3e38SRobert Watson 255d4b5cae4SRobert Watson static int 256d4b5cae4SRobert Watson sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS) 257d4b5cae4SRobert Watson { 258d4b5cae4SRobert Watson int error, qlimit; 259d4b5cae4SRobert Watson 260d4b5cae4SRobert Watson netisr_getqlimit(&ip_nh, &qlimit); 261d4b5cae4SRobert Watson error = sysctl_handle_int(oidp, &qlimit, 0, req); 262d4b5cae4SRobert Watson if (error || !req->newptr) 263d4b5cae4SRobert Watson return (error); 264d4b5cae4SRobert Watson if (qlimit < 1) 265d4b5cae4SRobert Watson return (EINVAL); 266d4b5cae4SRobert Watson return (netisr_setqlimit(&ip_nh, qlimit)); 267d4b5cae4SRobert Watson } 268d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, 269d4b5cae4SRobert Watson CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_queue_maxlen, "I", 270d4b5cae4SRobert Watson "Maximum size of the IP input queue"); 271d4b5cae4SRobert Watson 272d4b5cae4SRobert Watson static int 273d4b5cae4SRobert Watson sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS) 274d4b5cae4SRobert Watson { 275d4b5cae4SRobert Watson u_int64_t qdrops_long; 276d4b5cae4SRobert Watson int error, qdrops; 277d4b5cae4SRobert Watson 278d4b5cae4SRobert Watson netisr_getqdrops(&ip_nh, &qdrops_long); 279d4b5cae4SRobert Watson qdrops = qdrops_long; 280d4b5cae4SRobert Watson error = sysctl_handle_int(oidp, &qdrops, 0, req); 281d4b5cae4SRobert Watson if (error || !req->newptr) 282d4b5cae4SRobert Watson return (error); 283d4b5cae4SRobert Watson if (qdrops != 0) 284d4b5cae4SRobert Watson return (EINVAL); 285d4b5cae4SRobert Watson netisr_clearqdrops(&ip_nh); 286d4b5cae4SRobert Watson return (0); 287d4b5cae4SRobert Watson } 288d4b5cae4SRobert Watson 289d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, 290d4b5cae4SRobert Watson CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_queue_drops, "I", 291d4b5cae4SRobert Watson "Number of packets dropped from the IP input queue"); 292d4b5cae4SRobert Watson 293b8bc95cdSAdrian Chadd #ifdef RSS 294b8bc95cdSAdrian Chadd static int 295b8bc95cdSAdrian Chadd sysctl_netinet_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS) 296b8bc95cdSAdrian Chadd { 297b8bc95cdSAdrian Chadd int error, qlimit; 298b8bc95cdSAdrian Chadd 299b8bc95cdSAdrian Chadd netisr_getqlimit(&ip_direct_nh, &qlimit); 300b8bc95cdSAdrian Chadd error = sysctl_handle_int(oidp, &qlimit, 0, req); 301b8bc95cdSAdrian Chadd if (error || !req->newptr) 302b8bc95cdSAdrian Chadd return (error); 303b8bc95cdSAdrian Chadd if (qlimit < 1) 304b8bc95cdSAdrian Chadd return (EINVAL); 305b8bc95cdSAdrian Chadd return (netisr_setqlimit(&ip_direct_nh, qlimit)); 306b8bc95cdSAdrian Chadd } 307b8bc95cdSAdrian Chadd SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_direct_queue_maxlen, 308b8bc95cdSAdrian Chadd CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_direct_queue_maxlen, "I", 309b8bc95cdSAdrian Chadd "Maximum size of the IP direct input queue"); 310b8bc95cdSAdrian Chadd 311b8bc95cdSAdrian Chadd static int 312b8bc95cdSAdrian Chadd sysctl_netinet_intr_direct_queue_drops(SYSCTL_HANDLER_ARGS) 313b8bc95cdSAdrian Chadd { 314b8bc95cdSAdrian Chadd u_int64_t qdrops_long; 315b8bc95cdSAdrian Chadd int error, qdrops; 316b8bc95cdSAdrian Chadd 317b8bc95cdSAdrian Chadd netisr_getqdrops(&ip_direct_nh, &qdrops_long); 318b8bc95cdSAdrian Chadd qdrops = qdrops_long; 319b8bc95cdSAdrian Chadd error = sysctl_handle_int(oidp, &qdrops, 0, req); 320b8bc95cdSAdrian Chadd if (error || !req->newptr) 321b8bc95cdSAdrian Chadd return (error); 322b8bc95cdSAdrian Chadd if (qdrops != 0) 323b8bc95cdSAdrian Chadd return (EINVAL); 324b8bc95cdSAdrian Chadd netisr_clearqdrops(&ip_direct_nh); 325b8bc95cdSAdrian Chadd return (0); 326b8bc95cdSAdrian Chadd } 327b8bc95cdSAdrian Chadd 328b8bc95cdSAdrian Chadd SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_direct_queue_drops, 329b8bc95cdSAdrian Chadd CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_direct_queue_drops, "I", 330b8bc95cdSAdrian Chadd "Number of packets dropped from the IP direct input queue"); 331b8bc95cdSAdrian Chadd #endif /* RSS */ 332b8bc95cdSAdrian Chadd 333df8bae1dSRodney W. Grimes /* 334df8bae1dSRodney W. Grimes * IP initialization: fill in IP protocol switch table. 335df8bae1dSRodney W. Grimes * All protocols not implemented in kernel go to raw IP protocol handler. 336df8bae1dSRodney W. Grimes */ 337df8bae1dSRodney W. Grimes void 338f2565d68SRobert Watson ip_init(void) 339df8bae1dSRodney W. Grimes { 340f2565d68SRobert Watson struct protosw *pr; 341f2565d68SRobert Watson int i; 342df8bae1dSRodney W. Grimes 343603724d3SBjoern A. Zeeb TAILQ_INIT(&V_in_ifaddrhead); 344603724d3SBjoern A. Zeeb V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask); 3451ed81b73SMarko Zec 3461ed81b73SMarko Zec /* Initialize IP reassembly queue. */ 347*e3c2c634SGleb Smirnoff for (i = 0; i < IPREASS_NHASH; i++) { 348*e3c2c634SGleb Smirnoff TAILQ_INIT(&V_ipq[i].head); 349*e3c2c634SGleb Smirnoff mtx_init(&V_ipq[i].lock, "IP reassembly", NULL, MTX_DEF); 350*e3c2c634SGleb Smirnoff } 3511ed81b73SMarko Zec V_maxnipq = nmbclusters / 32; 3521ed81b73SMarko Zec V_maxfragsperpacket = 16; 3531ed81b73SMarko Zec V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL, 3541ed81b73SMarko Zec NULL, UMA_ALIGN_PTR, 0); 3551ed81b73SMarko Zec maxnipq_update(); 3561ed81b73SMarko Zec 3570b4b0b0fSJulian Elischer /* Initialize packet filter hooks. */ 3580b4b0b0fSJulian Elischer V_inet_pfil_hook.ph_type = PFIL_TYPE_AF; 3590b4b0b0fSJulian Elischer V_inet_pfil_hook.ph_af = AF_INET; 3600b4b0b0fSJulian Elischer if ((i = pfil_head_register(&V_inet_pfil_hook)) != 0) 3610b4b0b0fSJulian Elischer printf("%s: WARNING: unable to register pfil hook, " 3620b4b0b0fSJulian Elischer "error %d\n", __func__, i); 3630b4b0b0fSJulian Elischer 3641ed81b73SMarko Zec /* Skip initialization of globals for non-default instances. */ 3651ed81b73SMarko Zec if (!IS_DEFAULT_VNET(curvnet)) 3661ed81b73SMarko Zec return; 3671ed81b73SMarko Zec 368f0ffb944SJulian Elischer pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 36902410549SRobert Watson if (pr == NULL) 370db09bef3SAndre Oppermann panic("ip_init: PF_INET not found"); 371db09bef3SAndre Oppermann 372db09bef3SAndre Oppermann /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */ 373df8bae1dSRodney W. Grimes for (i = 0; i < IPPROTO_MAX; i++) 374df8bae1dSRodney W. Grimes ip_protox[i] = pr - inetsw; 375db09bef3SAndre Oppermann /* 376db09bef3SAndre Oppermann * Cycle through IP protocols and put them into the appropriate place 377db09bef3SAndre Oppermann * in ip_protox[]. 378db09bef3SAndre Oppermann */ 379f0ffb944SJulian Elischer for (pr = inetdomain.dom_protosw; 380f0ffb944SJulian Elischer pr < inetdomain.dom_protoswNPROTOSW; pr++) 381df8bae1dSRodney W. Grimes if (pr->pr_domain->dom_family == PF_INET && 382db09bef3SAndre Oppermann pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) { 383db09bef3SAndre Oppermann /* Be careful to only index valid IP protocols. */ 384db77984cSSam Leffler if (pr->pr_protocol < IPPROTO_MAX) 385df8bae1dSRodney W. Grimes ip_protox[pr->pr_protocol] = pr - inetsw; 386db09bef3SAndre Oppermann } 387194a213eSAndrey A. Chernov 3884f590175SPaul Saab EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change, 3894f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 3905f311da2SMike Silbersack 391d4b5cae4SRobert Watson netisr_register(&ip_nh); 392b8bc95cdSAdrian Chadd #ifdef RSS 393b8bc95cdSAdrian Chadd netisr_register(&ip_direct_nh); 394b8bc95cdSAdrian Chadd #endif 395df8bae1dSRodney W. Grimes } 396df8bae1dSRodney W. Grimes 3979802380eSBjoern A. Zeeb #ifdef VIMAGE 3989802380eSBjoern A. Zeeb void 3999802380eSBjoern A. Zeeb ip_destroy(void) 4009802380eSBjoern A. Zeeb { 4014d3dfd45SMikolaj Golub int i; 4024d3dfd45SMikolaj Golub 4034d3dfd45SMikolaj Golub if ((i = pfil_head_unregister(&V_inet_pfil_hook)) != 0) 4044d3dfd45SMikolaj Golub printf("%s: WARNING: unable to unregister pfil hook, " 4054d3dfd45SMikolaj Golub "error %d\n", __func__, i); 4069802380eSBjoern A. Zeeb 4079802380eSBjoern A. Zeeb /* Cleanup in_ifaddr hash table; should be empty. */ 4089802380eSBjoern A. Zeeb hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask); 4099802380eSBjoern A. Zeeb 410*e3c2c634SGleb Smirnoff /* Destroy IP reassembly queue. */ 4113de5805bSGleb Smirnoff ip_drain_vnet(); 4129802380eSBjoern A. Zeeb uma_zdestroy(V_ipq_zone); 413*e3c2c634SGleb Smirnoff for (i = 0; i < IPREASS_NHASH; i++) 414*e3c2c634SGleb Smirnoff mtx_destroy(&V_ipq[i].lock); 4159802380eSBjoern A. Zeeb } 4169802380eSBjoern A. Zeeb #endif 4179802380eSBjoern A. Zeeb 418b8bc95cdSAdrian Chadd #ifdef RSS 419b8bc95cdSAdrian Chadd /* 420b8bc95cdSAdrian Chadd * IP direct input routine. 421b8bc95cdSAdrian Chadd * 422b8bc95cdSAdrian Chadd * This is called when reinjecting completed fragments where 423b8bc95cdSAdrian Chadd * all of the previous checking and book-keeping has been done. 424b8bc95cdSAdrian Chadd */ 425b8bc95cdSAdrian Chadd void 426b8bc95cdSAdrian Chadd ip_direct_input(struct mbuf *m) 427b8bc95cdSAdrian Chadd { 428b8bc95cdSAdrian Chadd struct ip *ip; 429b8bc95cdSAdrian Chadd int hlen; 430b8bc95cdSAdrian Chadd 431b8bc95cdSAdrian Chadd ip = mtod(m, struct ip *); 432b8bc95cdSAdrian Chadd hlen = ip->ip_hl << 2; 433b8bc95cdSAdrian Chadd 434b8bc95cdSAdrian Chadd IPSTAT_INC(ips_delivered); 435b8bc95cdSAdrian Chadd (*inetsw[ip_protox[ip->ip_p]].pr_input)(&m, &hlen, ip->ip_p); 436b8bc95cdSAdrian Chadd return; 437b8bc95cdSAdrian Chadd } 438b8bc95cdSAdrian Chadd #endif 439b8bc95cdSAdrian Chadd 4404d2e3692SLuigi Rizzo /* 441df8bae1dSRodney W. Grimes * Ip input routine. Checksum and byte swap header. If fragmented 442df8bae1dSRodney W. Grimes * try to reassemble. Process options. Pass to next level. 443df8bae1dSRodney W. Grimes */ 444c67b1d17SGarrett Wollman void 445c67b1d17SGarrett Wollman ip_input(struct mbuf *m) 446df8bae1dSRodney W. Grimes { 4479188b4a1SAndre Oppermann struct ip *ip = NULL; 4485da9f8faSJosef Karthauser struct in_ifaddr *ia = NULL; 449ca925d9cSJonathan Lemon struct ifaddr *ifa; 4500aade26eSRobert Watson struct ifnet *ifp; 4519b932e9eSAndre Oppermann int checkif, hlen = 0; 45221d172a3SGleb Smirnoff uint16_t sum, ip_len; 45302c1c707SAndre Oppermann int dchg = 0; /* dest changed after fw */ 454f51f805fSSam Leffler struct in_addr odst; /* original dst address */ 455b715f178SLuigi Rizzo 456fe584538SDag-Erling Smørgrav M_ASSERTPKTHDR(m); 457db40007dSAndrew R. Reiter 458ac9d7e26SMax Laier if (m->m_flags & M_FASTFWD_OURS) { 45976ff6dcfSAndre Oppermann m->m_flags &= ~M_FASTFWD_OURS; 46076ff6dcfSAndre Oppermann /* Set up some basics that will be used later. */ 4612b25acc1SLuigi Rizzo ip = mtod(m, struct ip *); 46253be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 4638f134647SGleb Smirnoff ip_len = ntohs(ip->ip_len); 4649b932e9eSAndre Oppermann goto ours; 4652b25acc1SLuigi Rizzo } 4662b25acc1SLuigi Rizzo 46786425c62SRobert Watson IPSTAT_INC(ips_total); 46858938916SGarrett Wollman 46958938916SGarrett Wollman if (m->m_pkthdr.len < sizeof(struct ip)) 47058938916SGarrett Wollman goto tooshort; 47158938916SGarrett Wollman 472df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct ip) && 4730b17fba7SAndre Oppermann (m = m_pullup(m, sizeof (struct ip))) == NULL) { 47486425c62SRobert Watson IPSTAT_INC(ips_toosmall); 475c67b1d17SGarrett Wollman return; 476df8bae1dSRodney W. Grimes } 477df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 47858938916SGarrett Wollman 47953be11f6SPoul-Henning Kamp if (ip->ip_v != IPVERSION) { 48086425c62SRobert Watson IPSTAT_INC(ips_badvers); 481df8bae1dSRodney W. Grimes goto bad; 482df8bae1dSRodney W. Grimes } 48358938916SGarrett Wollman 48453be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 485df8bae1dSRodney W. Grimes if (hlen < sizeof(struct ip)) { /* minimum header length */ 48686425c62SRobert Watson IPSTAT_INC(ips_badhlen); 487df8bae1dSRodney W. Grimes goto bad; 488df8bae1dSRodney W. Grimes } 489df8bae1dSRodney W. Grimes if (hlen > m->m_len) { 4900b17fba7SAndre Oppermann if ((m = m_pullup(m, hlen)) == NULL) { 49186425c62SRobert Watson IPSTAT_INC(ips_badhlen); 492c67b1d17SGarrett Wollman return; 493df8bae1dSRodney W. Grimes } 494df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 495df8bae1dSRodney W. Grimes } 49633841545SHajimu UMEMOTO 49757f60867SMark Johnston IP_PROBE(receive, NULL, NULL, ip, m->m_pkthdr.rcvif, ip, NULL); 49857f60867SMark Johnston 49933841545SHajimu UMEMOTO /* 127/8 must not appear on wire - RFC1122 */ 5000aade26eSRobert Watson ifp = m->m_pkthdr.rcvif; 50133841545SHajimu UMEMOTO if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 50233841545SHajimu UMEMOTO (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 5030aade26eSRobert Watson if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 50486425c62SRobert Watson IPSTAT_INC(ips_badaddr); 50533841545SHajimu UMEMOTO goto bad; 50633841545SHajimu UMEMOTO } 50733841545SHajimu UMEMOTO } 50833841545SHajimu UMEMOTO 509db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 510db4f9cc7SJonathan Lemon sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 511db4f9cc7SJonathan Lemon } else { 51258938916SGarrett Wollman if (hlen == sizeof(struct ip)) { 51347c861ecSBrian Somers sum = in_cksum_hdr(ip); 51458938916SGarrett Wollman } else { 51547c861ecSBrian Somers sum = in_cksum(m, hlen); 51658938916SGarrett Wollman } 517db4f9cc7SJonathan Lemon } 51847c861ecSBrian Somers if (sum) { 51986425c62SRobert Watson IPSTAT_INC(ips_badsum); 520df8bae1dSRodney W. Grimes goto bad; 521df8bae1dSRodney W. Grimes } 522df8bae1dSRodney W. Grimes 52302b199f1SMax Laier #ifdef ALTQ 52402b199f1SMax Laier if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 52502b199f1SMax Laier /* packet is dropped by traffic conditioner */ 52602b199f1SMax Laier return; 52702b199f1SMax Laier #endif 52802b199f1SMax Laier 52921d172a3SGleb Smirnoff ip_len = ntohs(ip->ip_len); 53021d172a3SGleb Smirnoff if (ip_len < hlen) { 53186425c62SRobert Watson IPSTAT_INC(ips_badlen); 532df8bae1dSRodney W. Grimes goto bad; 533df8bae1dSRodney W. Grimes } 534df8bae1dSRodney W. Grimes 535df8bae1dSRodney W. Grimes /* 536df8bae1dSRodney W. Grimes * Check that the amount of data in the buffers 537df8bae1dSRodney W. Grimes * is as at least much as the IP header would have us expect. 538df8bae1dSRodney W. Grimes * Trim mbufs if longer than we expect. 539df8bae1dSRodney W. Grimes * Drop packet if shorter than we expect. 540df8bae1dSRodney W. Grimes */ 54121d172a3SGleb Smirnoff if (m->m_pkthdr.len < ip_len) { 54258938916SGarrett Wollman tooshort: 54386425c62SRobert Watson IPSTAT_INC(ips_tooshort); 544df8bae1dSRodney W. Grimes goto bad; 545df8bae1dSRodney W. Grimes } 54621d172a3SGleb Smirnoff if (m->m_pkthdr.len > ip_len) { 547df8bae1dSRodney W. Grimes if (m->m_len == m->m_pkthdr.len) { 54821d172a3SGleb Smirnoff m->m_len = ip_len; 54921d172a3SGleb Smirnoff m->m_pkthdr.len = ip_len; 550df8bae1dSRodney W. Grimes } else 55121d172a3SGleb Smirnoff m_adj(m, ip_len - m->m_pkthdr.len); 552df8bae1dSRodney W. Grimes } 553b8bc95cdSAdrian Chadd 554b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 55514dd6717SSam Leffler /* 556ffe8cd7bSBjoern A. Zeeb * Bypass packet filtering for packets previously handled by IPsec. 55714dd6717SSam Leffler */ 558cc977adcSBjoern A. Zeeb if (ip_ipsec_filtertunnel(m)) 559c21fd232SAndre Oppermann goto passin; 560b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 5613f67c834SDon Lewis 562c4ac87eaSDarren Reed /* 563134ea224SSam Leffler * Run through list of hooks for input packets. 564f51f805fSSam Leffler * 565f51f805fSSam Leffler * NB: Beware of the destination address changing (e.g. 566f51f805fSSam Leffler * by NAT rewriting). When this happens, tell 567f51f805fSSam Leffler * ip_forward to do the right thing. 568c4ac87eaSDarren Reed */ 569c21fd232SAndre Oppermann 570c21fd232SAndre Oppermann /* Jump over all PFIL processing if hooks are not active. */ 5710b4b0b0fSJulian Elischer if (!PFIL_HOOKED(&V_inet_pfil_hook)) 572c21fd232SAndre Oppermann goto passin; 573c21fd232SAndre Oppermann 574f51f805fSSam Leffler odst = ip->ip_dst; 5750b4b0b0fSJulian Elischer if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0) 576beec8214SDarren Reed return; 577134ea224SSam Leffler if (m == NULL) /* consumed by filter */ 578c4ac87eaSDarren Reed return; 5799b932e9eSAndre Oppermann 580c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 58102c1c707SAndre Oppermann dchg = (odst.s_addr != ip->ip_dst.s_addr); 5820aade26eSRobert Watson ifp = m->m_pkthdr.rcvif; 5839b932e9eSAndre Oppermann 5849b932e9eSAndre Oppermann if (m->m_flags & M_FASTFWD_OURS) { 5859b932e9eSAndre Oppermann m->m_flags &= ~M_FASTFWD_OURS; 5869b932e9eSAndre Oppermann goto ours; 5879b932e9eSAndre Oppermann } 588ffdbf9daSAndrey V. Elsukov if (m->m_flags & M_IP_NEXTHOP) { 589ffdbf9daSAndrey V. Elsukov dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL); 590ffdbf9daSAndrey V. Elsukov if (dchg != 0) { 591099dd043SAndre Oppermann /* 592ffdbf9daSAndrey V. Elsukov * Directly ship the packet on. This allows 593ffdbf9daSAndrey V. Elsukov * forwarding packets originally destined to us 594ffdbf9daSAndrey V. Elsukov * to some other directly connected host. 595099dd043SAndre Oppermann */ 596ffdbf9daSAndrey V. Elsukov ip_forward(m, 1); 597099dd043SAndre Oppermann return; 598099dd043SAndre Oppermann } 599ffdbf9daSAndrey V. Elsukov } 600c21fd232SAndre Oppermann passin: 60121d172a3SGleb Smirnoff 60221d172a3SGleb Smirnoff /* 603df8bae1dSRodney W. Grimes * Process options and, if not destined for us, 604df8bae1dSRodney W. Grimes * ship it on. ip_dooptions returns 1 when an 605df8bae1dSRodney W. Grimes * error was detected (causing an icmp message 606df8bae1dSRodney W. Grimes * to be sent and the original packet to be freed). 607df8bae1dSRodney W. Grimes */ 6089b932e9eSAndre Oppermann if (hlen > sizeof (struct ip) && ip_dooptions(m, 0)) 609c67b1d17SGarrett Wollman return; 610df8bae1dSRodney W. Grimes 611f0068c4aSGarrett Wollman /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 612f0068c4aSGarrett Wollman * matter if it is destined to another node, or whether it is 613f0068c4aSGarrett Wollman * a multicast one, RSVP wants it! and prevents it from being forwarded 614f0068c4aSGarrett Wollman * anywhere else. Also checks if the rsvp daemon is running before 615f0068c4aSGarrett Wollman * grabbing the packet. 616f0068c4aSGarrett Wollman */ 617603724d3SBjoern A. Zeeb if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP) 618f0068c4aSGarrett Wollman goto ours; 619f0068c4aSGarrett Wollman 620df8bae1dSRodney W. Grimes /* 621df8bae1dSRodney W. Grimes * Check our list of addresses, to see if the packet is for us. 622cc766e04SGarrett Wollman * If we don't have any addresses, assume any unicast packet 623cc766e04SGarrett Wollman * we receive might be for us (and let the upper layers deal 624cc766e04SGarrett Wollman * with it). 625df8bae1dSRodney W. Grimes */ 626603724d3SBjoern A. Zeeb if (TAILQ_EMPTY(&V_in_ifaddrhead) && 627cc766e04SGarrett Wollman (m->m_flags & (M_MCAST|M_BCAST)) == 0) 628cc766e04SGarrett Wollman goto ours; 629cc766e04SGarrett Wollman 6307538a9a0SJonathan Lemon /* 631823db0e9SDon Lewis * Enable a consistency check between the destination address 632823db0e9SDon Lewis * and the arrival interface for a unicast packet (the RFC 1122 633823db0e9SDon Lewis * strong ES model) if IP forwarding is disabled and the packet 634e15ae1b2SDon Lewis * is not locally generated and the packet is not subject to 635e15ae1b2SDon Lewis * 'ipfw fwd'. 6363f67c834SDon Lewis * 6373f67c834SDon Lewis * XXX - Checking also should be disabled if the destination 6383f67c834SDon Lewis * address is ipnat'ed to a different interface. 6393f67c834SDon Lewis * 640a8f12100SDon Lewis * XXX - Checking is incompatible with IP aliases added 6413f67c834SDon Lewis * to the loopback interface instead of the interface where 6423f67c834SDon Lewis * the packets are received. 643a9771948SGleb Smirnoff * 644a9771948SGleb Smirnoff * XXX - This is the case for carp vhost IPs as well so we 645a9771948SGleb Smirnoff * insert a workaround. If the packet got here, we already 646a9771948SGleb Smirnoff * checked with carp_iamatch() and carp_forus(). 647823db0e9SDon Lewis */ 648603724d3SBjoern A. Zeeb checkif = V_ip_checkinterface && (V_ipforwarding == 0) && 6490aade26eSRobert Watson ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) && 65054bfbd51SWill Andrews ifp->if_carp == NULL && (dchg == 0); 651823db0e9SDon Lewis 652ca925d9cSJonathan Lemon /* 653ca925d9cSJonathan Lemon * Check for exact addresses in the hash bucket. 654ca925d9cSJonathan Lemon */ 6552d9cfabaSRobert Watson /* IN_IFADDR_RLOCK(); */ 6569b932e9eSAndre Oppermann LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 657f9e354dfSJulian Elischer /* 658823db0e9SDon Lewis * If the address matches, verify that the packet 659823db0e9SDon Lewis * arrived via the correct interface if checking is 660823db0e9SDon Lewis * enabled. 661f9e354dfSJulian Elischer */ 6629b932e9eSAndre Oppermann if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr && 6638c0fec80SRobert Watson (!checkif || ia->ia_ifp == ifp)) { 6647caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_ipackets, 1); 6657caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_ibytes, 6667caf4ab7SGleb Smirnoff m->m_pkthdr.len); 6672d9cfabaSRobert Watson /* IN_IFADDR_RUNLOCK(); */ 668ed1ff184SJulian Elischer goto ours; 669ca925d9cSJonathan Lemon } 6708c0fec80SRobert Watson } 6712d9cfabaSRobert Watson /* IN_IFADDR_RUNLOCK(); */ 6722d9cfabaSRobert Watson 673823db0e9SDon Lewis /* 674ca925d9cSJonathan Lemon * Check for broadcast addresses. 675ca925d9cSJonathan Lemon * 676ca925d9cSJonathan Lemon * Only accept broadcast packets that arrive via the matching 677ca925d9cSJonathan Lemon * interface. Reception of forwarded directed broadcasts would 678ca925d9cSJonathan Lemon * be handled via ip_forward() and ether_output() with the loopback 679ca925d9cSJonathan Lemon * into the stack for SIMPLEX interfaces handled by ether_output(). 680823db0e9SDon Lewis */ 6810aade26eSRobert Watson if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) { 682137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 6830aade26eSRobert Watson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 684ca925d9cSJonathan Lemon if (ifa->ifa_addr->sa_family != AF_INET) 685ca925d9cSJonathan Lemon continue; 686ca925d9cSJonathan Lemon ia = ifatoia(ifa); 687df8bae1dSRodney W. Grimes if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 6880aade26eSRobert Watson ip->ip_dst.s_addr) { 6897caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_ipackets, 1); 6907caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_ibytes, 6917caf4ab7SGleb Smirnoff m->m_pkthdr.len); 692137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 693df8bae1dSRodney W. Grimes goto ours; 6940aade26eSRobert Watson } 6950ac40133SBrian Somers #ifdef BOOTP_COMPAT 6960aade26eSRobert Watson if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) { 6977caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_ipackets, 1); 6987caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_ibytes, 6997caf4ab7SGleb Smirnoff m->m_pkthdr.len); 700137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 701ca925d9cSJonathan Lemon goto ours; 7020aade26eSRobert Watson } 7030ac40133SBrian Somers #endif 704df8bae1dSRodney W. Grimes } 705137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 70619e5b0a7SRobert Watson ia = NULL; 707df8bae1dSRodney W. Grimes } 708f8429ca2SBruce M Simpson /* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */ 709f8429ca2SBruce M Simpson if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) { 71086425c62SRobert Watson IPSTAT_INC(ips_cantforward); 711f8429ca2SBruce M Simpson m_freem(m); 712f8429ca2SBruce M Simpson return; 713f8429ca2SBruce M Simpson } 714df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 715603724d3SBjoern A. Zeeb if (V_ip_mrouter) { 716df8bae1dSRodney W. Grimes /* 717df8bae1dSRodney W. Grimes * If we are acting as a multicast router, all 718df8bae1dSRodney W. Grimes * incoming multicast packets are passed to the 719df8bae1dSRodney W. Grimes * kernel-level multicast forwarding function. 720df8bae1dSRodney W. Grimes * The packet is returned (relatively) intact; if 721df8bae1dSRodney W. Grimes * ip_mforward() returns a non-zero value, the packet 722df8bae1dSRodney W. Grimes * must be discarded, else it may be accepted below. 723df8bae1dSRodney W. Grimes */ 7240aade26eSRobert Watson if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) { 72586425c62SRobert Watson IPSTAT_INC(ips_cantforward); 726df8bae1dSRodney W. Grimes m_freem(m); 727c67b1d17SGarrett Wollman return; 728df8bae1dSRodney W. Grimes } 729df8bae1dSRodney W. Grimes 730df8bae1dSRodney W. Grimes /* 73111612afaSDima Dorfman * The process-level routing daemon needs to receive 732df8bae1dSRodney W. Grimes * all multicast IGMP packets, whether or not this 733df8bae1dSRodney W. Grimes * host belongs to their destination groups. 734df8bae1dSRodney W. Grimes */ 735df8bae1dSRodney W. Grimes if (ip->ip_p == IPPROTO_IGMP) 736df8bae1dSRodney W. Grimes goto ours; 73786425c62SRobert Watson IPSTAT_INC(ips_forward); 738df8bae1dSRodney W. Grimes } 739df8bae1dSRodney W. Grimes /* 740d10910e6SBruce M Simpson * Assume the packet is for us, to avoid prematurely taking 741d10910e6SBruce M Simpson * a lock on the in_multi hash. Protocols must perform 742d10910e6SBruce M Simpson * their own filtering and update statistics accordingly. 743df8bae1dSRodney W. Grimes */ 744df8bae1dSRodney W. Grimes goto ours; 745df8bae1dSRodney W. Grimes } 746df8bae1dSRodney W. Grimes if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 747df8bae1dSRodney W. Grimes goto ours; 748df8bae1dSRodney W. Grimes if (ip->ip_dst.s_addr == INADDR_ANY) 749df8bae1dSRodney W. Grimes goto ours; 750df8bae1dSRodney W. Grimes 7516a800098SYoshinobu Inoue /* 752df8bae1dSRodney W. Grimes * Not for us; forward if possible and desirable. 753df8bae1dSRodney W. Grimes */ 754603724d3SBjoern A. Zeeb if (V_ipforwarding == 0) { 75586425c62SRobert Watson IPSTAT_INC(ips_cantforward); 756df8bae1dSRodney W. Grimes m_freem(m); 757546f251bSChris D. Faulhaber } else { 7589b932e9eSAndre Oppermann ip_forward(m, dchg); 759546f251bSChris D. Faulhaber } 760c67b1d17SGarrett Wollman return; 761df8bae1dSRodney W. Grimes 762df8bae1dSRodney W. Grimes ours: 763d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH 764d0ebc0d2SYaroslav Tykhiy /* 765d0ebc0d2SYaroslav Tykhiy * IPSTEALTH: Process non-routing options only 766d0ebc0d2SYaroslav Tykhiy * if the packet is destined for us. 767d0ebc0d2SYaroslav Tykhiy */ 7687caf4ab7SGleb Smirnoff if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1)) 769d0ebc0d2SYaroslav Tykhiy return; 770d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */ 771d0ebc0d2SYaroslav Tykhiy 77263f8d699SJordan K. Hubbard /* 773b6ea1aa5SRuslan Ermilov * Attempt reassembly; if it succeeds, proceed. 774ac9d7e26SMax Laier * ip_reass() will return a different mbuf. 775df8bae1dSRodney W. Grimes */ 7768f134647SGleb Smirnoff if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) { 777aa69c612SGleb Smirnoff /* XXXGL: shouldn't we save & set m_flags? */ 778f0cada84SAndre Oppermann m = ip_reass(m); 779f0cada84SAndre Oppermann if (m == NULL) 780c67b1d17SGarrett Wollman return; 7816a800098SYoshinobu Inoue ip = mtod(m, struct ip *); 7827e2df452SRuslan Ermilov /* Get the header length of the reassembled packet */ 78353be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 784f0cada84SAndre Oppermann } 785f0cada84SAndre Oppermann 786b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 78733841545SHajimu UMEMOTO /* 78833841545SHajimu UMEMOTO * enforce IPsec policy checking if we are seeing last header. 78933841545SHajimu UMEMOTO * note that we do not visit this with protocols with pcb layer 79033841545SHajimu UMEMOTO * code - like udp/tcp/raw ip. 79133841545SHajimu UMEMOTO */ 792e58320f1SAndrey V. Elsukov if (ip_ipsec_input(m, ip->ip_p) != 0) 79333841545SHajimu UMEMOTO goto bad; 794b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 79533841545SHajimu UMEMOTO 796df8bae1dSRodney W. Grimes /* 797df8bae1dSRodney W. Grimes * Switch out to protocol's input routine. 798df8bae1dSRodney W. Grimes */ 79986425c62SRobert Watson IPSTAT_INC(ips_delivered); 8009b932e9eSAndre Oppermann 8018f5a8818SKevin Lo (*inetsw[ip_protox[ip->ip_p]].pr_input)(&m, &hlen, ip->ip_p); 802c67b1d17SGarrett Wollman return; 803df8bae1dSRodney W. Grimes bad: 804df8bae1dSRodney W. Grimes m_freem(m); 805c67b1d17SGarrett Wollman } 806c67b1d17SGarrett Wollman 807c67b1d17SGarrett Wollman /* 808d248c7d7SRobert Watson * After maxnipq has been updated, propagate the change to UMA. The UMA zone 809d248c7d7SRobert Watson * max has slightly different semantics than the sysctl, for historical 810d248c7d7SRobert Watson * reasons. 811d248c7d7SRobert Watson */ 812d248c7d7SRobert Watson static void 813d248c7d7SRobert Watson maxnipq_update(void) 814d248c7d7SRobert Watson { 815d248c7d7SRobert Watson 816d248c7d7SRobert Watson /* 817d248c7d7SRobert Watson * -1 for unlimited allocation. 818d248c7d7SRobert Watson */ 819603724d3SBjoern A. Zeeb if (V_maxnipq < 0) 820603724d3SBjoern A. Zeeb uma_zone_set_max(V_ipq_zone, 0); 821d248c7d7SRobert Watson /* 822d248c7d7SRobert Watson * Positive number for specific bound. 823d248c7d7SRobert Watson */ 824603724d3SBjoern A. Zeeb if (V_maxnipq > 0) 825603724d3SBjoern A. Zeeb uma_zone_set_max(V_ipq_zone, V_maxnipq); 826d248c7d7SRobert Watson /* 8271c0b48c7SGleb Smirnoff * Zero specifies no further fragment queue allocation. 828d248c7d7SRobert Watson */ 8291c0b48c7SGleb Smirnoff if (V_maxnipq == 0) { 830603724d3SBjoern A. Zeeb uma_zone_set_max(V_ipq_zone, 1); 8311c0b48c7SGleb Smirnoff ip_drain_vnet(); 8321c0b48c7SGleb Smirnoff } 833d248c7d7SRobert Watson } 834d248c7d7SRobert Watson 8354f590175SPaul Saab static void 8364f590175SPaul Saab ipq_zone_change(void *tag) 8374f590175SPaul Saab { 8384f590175SPaul Saab 839603724d3SBjoern A. Zeeb if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) { 840603724d3SBjoern A. Zeeb V_maxnipq = nmbclusters / 32; 8414f590175SPaul Saab maxnipq_update(); 8424f590175SPaul Saab } 8434f590175SPaul Saab } 8444f590175SPaul Saab 845d248c7d7SRobert Watson static int 846d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS) 847d248c7d7SRobert Watson { 848d248c7d7SRobert Watson int error, i; 849d248c7d7SRobert Watson 850603724d3SBjoern A. Zeeb i = V_maxnipq; 851d248c7d7SRobert Watson error = sysctl_handle_int(oidp, &i, 0, req); 852d248c7d7SRobert Watson if (error || !req->newptr) 853d248c7d7SRobert Watson return (error); 854d248c7d7SRobert Watson 855d248c7d7SRobert Watson /* 856d248c7d7SRobert Watson * XXXRW: Might be a good idea to sanity check the argument and place 857d248c7d7SRobert Watson * an extreme upper bound. 858d248c7d7SRobert Watson */ 859d248c7d7SRobert Watson if (i < -1) 860d248c7d7SRobert Watson return (EINVAL); 861603724d3SBjoern A. Zeeb V_maxnipq = i; 862d248c7d7SRobert Watson maxnipq_update(); 863d248c7d7SRobert Watson return (0); 864d248c7d7SRobert Watson } 865d248c7d7SRobert Watson 866d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW, 867d248c7d7SRobert Watson NULL, 0, sysctl_maxnipq, "I", 868d248c7d7SRobert Watson "Maximum number of IPv4 fragment reassembly queue entries"); 869d248c7d7SRobert Watson 870aa69c612SGleb Smirnoff #define M_IP_FRAG M_PROTO9 871aa69c612SGleb Smirnoff 872d248c7d7SRobert Watson /* 873f59e59d5SAdrian Chadd * Attempt to purge something from the reassembly queue to make 874f59e59d5SAdrian Chadd * room. 875f59e59d5SAdrian Chadd * 876f59e59d5SAdrian Chadd * Must be called without any IPQ locks held, as it will attempt 877f59e59d5SAdrian Chadd * to lock each in turn. 878f59e59d5SAdrian Chadd * 879f59e59d5SAdrian Chadd * 'skip_bucket' is the bucket with which to skip over, or -1 to 880f59e59d5SAdrian Chadd * not skip over anything. 881f59e59d5SAdrian Chadd * 882f59e59d5SAdrian Chadd * Returns the bucket being freed, or -1 for no action. 883f59e59d5SAdrian Chadd */ 884f59e59d5SAdrian Chadd static int 885f59e59d5SAdrian Chadd ip_reass_purge_element(int skip_bucket) 886f59e59d5SAdrian Chadd { 887f59e59d5SAdrian Chadd int i; 888f59e59d5SAdrian Chadd struct ipq *r; 889f59e59d5SAdrian Chadd 890f59e59d5SAdrian Chadd for (i = 0; i < IPREASS_NHASH; i++) { 891f59e59d5SAdrian Chadd if (skip_bucket > -1 && i == skip_bucket) 892f59e59d5SAdrian Chadd continue; 893f59e59d5SAdrian Chadd IPQ_LOCK(i); 894*e3c2c634SGleb Smirnoff r = TAILQ_LAST(&V_ipq[i].head, ipqhead); 895f59e59d5SAdrian Chadd if (r) { 896*e3c2c634SGleb Smirnoff ipq_timeout(&V_ipq[i].head, r); 897f59e59d5SAdrian Chadd IPQ_UNLOCK(i); 898f59e59d5SAdrian Chadd return (i); 899f59e59d5SAdrian Chadd } 900f59e59d5SAdrian Chadd IPQ_UNLOCK(i); 901f59e59d5SAdrian Chadd } 902f59e59d5SAdrian Chadd return (-1); 903f59e59d5SAdrian Chadd } 904f59e59d5SAdrian Chadd 905f59e59d5SAdrian Chadd /* 9068948e4baSArchie Cobbs * Take incoming datagram fragment and try to reassemble it into 907f0cada84SAndre Oppermann * whole datagram. If the argument is the first fragment or one 908f0cada84SAndre Oppermann * in between the function will return NULL and store the mbuf 909f0cada84SAndre Oppermann * in the fragment chain. If the argument is the last fragment 910f0cada84SAndre Oppermann * the packet will be reassembled and the pointer to the new 911f0cada84SAndre Oppermann * mbuf returned for further processing. Only m_tags attached 912f0cada84SAndre Oppermann * to the first packet/fragment are preserved. 913f0cada84SAndre Oppermann * The IP header is *NOT* adjusted out of iplen. 914df8bae1dSRodney W. Grimes */ 915f0cada84SAndre Oppermann struct mbuf * 916f0cada84SAndre Oppermann ip_reass(struct mbuf *m) 917df8bae1dSRodney W. Grimes { 918f0cada84SAndre Oppermann struct ip *ip; 919f0cada84SAndre Oppermann struct mbuf *p, *q, *nq, *t; 920f0cada84SAndre Oppermann struct ipq *fp = NULL; 921f0cada84SAndre Oppermann struct ipqhead *head; 922f0cada84SAndre Oppermann int i, hlen, next; 92359dfcba4SHajimu UMEMOTO u_int8_t ecn, ecn0; 924f0cada84SAndre Oppermann u_short hash; 925b8bc95cdSAdrian Chadd #ifdef RSS 926b8bc95cdSAdrian Chadd uint32_t rss_hash, rss_type; 927b8bc95cdSAdrian Chadd #endif 928f59e59d5SAdrian Chadd int do_purge = 0; 929df8bae1dSRodney W. Grimes 930800af1fbSMaxim Konovalov /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */ 931603724d3SBjoern A. Zeeb if (V_maxnipq == 0 || V_maxfragsperpacket == 0) { 93286425c62SRobert Watson IPSTAT_INC(ips_fragments); 93386425c62SRobert Watson IPSTAT_INC(ips_fragdropped); 9349d804f81SAndre Oppermann m_freem(m); 9359d804f81SAndre Oppermann return (NULL); 936f0cada84SAndre Oppermann } 9372fad1e93SSam Leffler 938f0cada84SAndre Oppermann ip = mtod(m, struct ip *); 939f0cada84SAndre Oppermann hlen = ip->ip_hl << 2; 940f0cada84SAndre Oppermann 941f0cada84SAndre Oppermann hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 942*e3c2c634SGleb Smirnoff head = &V_ipq[hash].head; 943f59e59d5SAdrian Chadd IPQ_LOCK(hash); 944f0cada84SAndre Oppermann 945f0cada84SAndre Oppermann /* 946f0cada84SAndre Oppermann * Look for queue of fragments 947f0cada84SAndre Oppermann * of this datagram. 948f0cada84SAndre Oppermann */ 949f0cada84SAndre Oppermann TAILQ_FOREACH(fp, head, ipq_list) 950f0cada84SAndre Oppermann if (ip->ip_id == fp->ipq_id && 951f0cada84SAndre Oppermann ip->ip_src.s_addr == fp->ipq_src.s_addr && 952f0cada84SAndre Oppermann ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 953f0cada84SAndre Oppermann #ifdef MAC 95430d239bcSRobert Watson mac_ipq_match(m, fp) && 955f0cada84SAndre Oppermann #endif 956f0cada84SAndre Oppermann ip->ip_p == fp->ipq_p) 957f0cada84SAndre Oppermann goto found; 958f0cada84SAndre Oppermann 959f0cada84SAndre Oppermann fp = NULL; 960f0cada84SAndre Oppermann 961f0cada84SAndre Oppermann /* 962d248c7d7SRobert Watson * Attempt to trim the number of allocated fragment queues if it 963d248c7d7SRobert Watson * exceeds the administrative limit. 964f0cada84SAndre Oppermann */ 965603724d3SBjoern A. Zeeb if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) { 966f0cada84SAndre Oppermann /* 967f0cada84SAndre Oppermann * drop something from the tail of the current queue 968f0cada84SAndre Oppermann * before proceeding further 969f0cada84SAndre Oppermann */ 970f0cada84SAndre Oppermann struct ipq *q = TAILQ_LAST(head, ipqhead); 971f0cada84SAndre Oppermann if (q == NULL) { /* gak */ 972f59e59d5SAdrian Chadd /* 973f59e59d5SAdrian Chadd * Defer doing this until later; when the 974f59e59d5SAdrian Chadd * lock is no longer held. 975f59e59d5SAdrian Chadd */ 976f59e59d5SAdrian Chadd do_purge = 1; 97755c28800SGleb Smirnoff } else 97855c28800SGleb Smirnoff ipq_timeout(head, q); 979f0cada84SAndre Oppermann } 980f0cada84SAndre Oppermann 981f0cada84SAndre Oppermann found: 982f0cada84SAndre Oppermann /* 983f0cada84SAndre Oppermann * Adjust ip_len to not reflect header, 984f0cada84SAndre Oppermann * convert offset of this to bytes. 985f0cada84SAndre Oppermann */ 9868f134647SGleb Smirnoff ip->ip_len = htons(ntohs(ip->ip_len) - hlen); 9878f134647SGleb Smirnoff if (ip->ip_off & htons(IP_MF)) { 988f0cada84SAndre Oppermann /* 989f0cada84SAndre Oppermann * Make sure that fragments have a data length 990f0cada84SAndre Oppermann * that's a non-zero multiple of 8 bytes. 991f0cada84SAndre Oppermann */ 992b6fcf6f9SGleb Smirnoff if (ip->ip_len == htons(0) || (ntohs(ip->ip_len) & 0x7) != 0) { 99386425c62SRobert Watson IPSTAT_INC(ips_toosmall); /* XXX */ 994f0cada84SAndre Oppermann goto dropfrag; 995f0cada84SAndre Oppermann } 996b09dc7e3SAndre Oppermann m->m_flags |= M_IP_FRAG; 997f0cada84SAndre Oppermann } else 998b09dc7e3SAndre Oppermann m->m_flags &= ~M_IP_FRAG; 9998f134647SGleb Smirnoff ip->ip_off = htons(ntohs(ip->ip_off) << 3); 1000f0cada84SAndre Oppermann 1001f0cada84SAndre Oppermann /* 1002f0cada84SAndre Oppermann * Attempt reassembly; if it succeeds, proceed. 1003f0cada84SAndre Oppermann * ip_reass() will return a different mbuf. 1004f0cada84SAndre Oppermann */ 100586425c62SRobert Watson IPSTAT_INC(ips_fragments); 10061b4381afSAndre Oppermann m->m_pkthdr.PH_loc.ptr = ip; 1007f0cada84SAndre Oppermann 1008f0cada84SAndre Oppermann /* Previous ip_reass() started here. */ 1009df8bae1dSRodney W. Grimes /* 1010df8bae1dSRodney W. Grimes * Presence of header sizes in mbufs 1011df8bae1dSRodney W. Grimes * would confuse code below. 1012df8bae1dSRodney W. Grimes */ 1013df8bae1dSRodney W. Grimes m->m_data += hlen; 1014df8bae1dSRodney W. Grimes m->m_len -= hlen; 1015df8bae1dSRodney W. Grimes 1016df8bae1dSRodney W. Grimes /* 1017df8bae1dSRodney W. Grimes * If first fragment to arrive, create a reassembly queue. 1018df8bae1dSRodney W. Grimes */ 1019042bbfa3SRobert Watson if (fp == NULL) { 1020603724d3SBjoern A. Zeeb fp = uma_zalloc(V_ipq_zone, M_NOWAIT); 1021d248c7d7SRobert Watson if (fp == NULL) 1022df8bae1dSRodney W. Grimes goto dropfrag; 102336b0360bSRobert Watson #ifdef MAC 102430d239bcSRobert Watson if (mac_ipq_init(fp, M_NOWAIT) != 0) { 1025603724d3SBjoern A. Zeeb uma_zfree(V_ipq_zone, fp); 10261d7d0bfeSPawel Jakub Dawidek fp = NULL; 10275e7ce478SRobert Watson goto dropfrag; 10285e7ce478SRobert Watson } 102930d239bcSRobert Watson mac_ipq_create(m, fp); 103036b0360bSRobert Watson #endif 1031462b86feSPoul-Henning Kamp TAILQ_INSERT_HEAD(head, fp, ipq_list); 1032603724d3SBjoern A. Zeeb V_nipq++; 1033375386e2SMike Silbersack fp->ipq_nfrags = 1; 1034df8bae1dSRodney W. Grimes fp->ipq_ttl = IPFRAGTTL; 1035df8bae1dSRodney W. Grimes fp->ipq_p = ip->ip_p; 1036df8bae1dSRodney W. Grimes fp->ipq_id = ip->ip_id; 10376effc713SDoug Rabson fp->ipq_src = ip->ip_src; 10386effc713SDoug Rabson fp->ipq_dst = ip->ip_dst; 1039af38c68cSLuigi Rizzo fp->ipq_frags = m; 1040af38c68cSLuigi Rizzo m->m_nextpkt = NULL; 1041800af1fbSMaxim Konovalov goto done; 104236b0360bSRobert Watson } else { 1043375386e2SMike Silbersack fp->ipq_nfrags++; 104436b0360bSRobert Watson #ifdef MAC 104530d239bcSRobert Watson mac_ipq_update(m, fp); 104636b0360bSRobert Watson #endif 1047df8bae1dSRodney W. Grimes } 1048df8bae1dSRodney W. Grimes 10491b4381afSAndre Oppermann #define GETIP(m) ((struct ip*)((m)->m_pkthdr.PH_loc.ptr)) 10506effc713SDoug Rabson 1051df8bae1dSRodney W. Grimes /* 105259dfcba4SHajimu UMEMOTO * Handle ECN by comparing this segment with the first one; 105359dfcba4SHajimu UMEMOTO * if CE is set, do not lose CE. 105459dfcba4SHajimu UMEMOTO * drop if CE and not-ECT are mixed for the same packet. 105559dfcba4SHajimu UMEMOTO */ 105659dfcba4SHajimu UMEMOTO ecn = ip->ip_tos & IPTOS_ECN_MASK; 105759dfcba4SHajimu UMEMOTO ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK; 105859dfcba4SHajimu UMEMOTO if (ecn == IPTOS_ECN_CE) { 105959dfcba4SHajimu UMEMOTO if (ecn0 == IPTOS_ECN_NOTECT) 106059dfcba4SHajimu UMEMOTO goto dropfrag; 106159dfcba4SHajimu UMEMOTO if (ecn0 != IPTOS_ECN_CE) 106259dfcba4SHajimu UMEMOTO GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE; 106359dfcba4SHajimu UMEMOTO } 106459dfcba4SHajimu UMEMOTO if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) 106559dfcba4SHajimu UMEMOTO goto dropfrag; 106659dfcba4SHajimu UMEMOTO 106759dfcba4SHajimu UMEMOTO /* 1068df8bae1dSRodney W. Grimes * Find a segment which begins after this one does. 1069df8bae1dSRodney W. Grimes */ 10706effc713SDoug Rabson for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 10718f134647SGleb Smirnoff if (ntohs(GETIP(q)->ip_off) > ntohs(ip->ip_off)) 1072df8bae1dSRodney W. Grimes break; 1073df8bae1dSRodney W. Grimes 1074df8bae1dSRodney W. Grimes /* 1075df8bae1dSRodney W. Grimes * If there is a preceding segment, it may provide some of 1076df8bae1dSRodney W. Grimes * our data already. If so, drop the data from the incoming 1077af38c68cSLuigi Rizzo * segment. If it provides all of our data, drop us, otherwise 1078af38c68cSLuigi Rizzo * stick new segment in the proper place. 1079db4f9cc7SJonathan Lemon * 10806bccea7cSRebecca Cran * If some of the data is dropped from the preceding 1081db4f9cc7SJonathan Lemon * segment, then it's checksum is invalidated. 1082df8bae1dSRodney W. Grimes */ 10836effc713SDoug Rabson if (p) { 10848f134647SGleb Smirnoff i = ntohs(GETIP(p)->ip_off) + ntohs(GETIP(p)->ip_len) - 10858f134647SGleb Smirnoff ntohs(ip->ip_off); 1086df8bae1dSRodney W. Grimes if (i > 0) { 10878f134647SGleb Smirnoff if (i >= ntohs(ip->ip_len)) 1088df8bae1dSRodney W. Grimes goto dropfrag; 10896a800098SYoshinobu Inoue m_adj(m, i); 1090db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = 0; 10918f134647SGleb Smirnoff ip->ip_off = htons(ntohs(ip->ip_off) + i); 10928f134647SGleb Smirnoff ip->ip_len = htons(ntohs(ip->ip_len) - i); 1093df8bae1dSRodney W. Grimes } 1094af38c68cSLuigi Rizzo m->m_nextpkt = p->m_nextpkt; 1095af38c68cSLuigi Rizzo p->m_nextpkt = m; 1096af38c68cSLuigi Rizzo } else { 1097af38c68cSLuigi Rizzo m->m_nextpkt = fp->ipq_frags; 1098af38c68cSLuigi Rizzo fp->ipq_frags = m; 1099df8bae1dSRodney W. Grimes } 1100df8bae1dSRodney W. Grimes 1101df8bae1dSRodney W. Grimes /* 1102df8bae1dSRodney W. Grimes * While we overlap succeeding segments trim them or, 1103df8bae1dSRodney W. Grimes * if they are completely covered, dequeue them. 1104df8bae1dSRodney W. Grimes */ 11058f134647SGleb Smirnoff for (; q != NULL && ntohs(ip->ip_off) + ntohs(ip->ip_len) > 11068f134647SGleb Smirnoff ntohs(GETIP(q)->ip_off); q = nq) { 11078f134647SGleb Smirnoff i = (ntohs(ip->ip_off) + ntohs(ip->ip_len)) - 11088f134647SGleb Smirnoff ntohs(GETIP(q)->ip_off); 11098f134647SGleb Smirnoff if (i < ntohs(GETIP(q)->ip_len)) { 11108f134647SGleb Smirnoff GETIP(q)->ip_len = htons(ntohs(GETIP(q)->ip_len) - i); 11118f134647SGleb Smirnoff GETIP(q)->ip_off = htons(ntohs(GETIP(q)->ip_off) + i); 11126effc713SDoug Rabson m_adj(q, i); 1113db4f9cc7SJonathan Lemon q->m_pkthdr.csum_flags = 0; 1114df8bae1dSRodney W. Grimes break; 1115df8bae1dSRodney W. Grimes } 11166effc713SDoug Rabson nq = q->m_nextpkt; 1117af38c68cSLuigi Rizzo m->m_nextpkt = nq; 111886425c62SRobert Watson IPSTAT_INC(ips_fragdropped); 1119375386e2SMike Silbersack fp->ipq_nfrags--; 11206effc713SDoug Rabson m_freem(q); 1121df8bae1dSRodney W. Grimes } 1122df8bae1dSRodney W. Grimes 1123df8bae1dSRodney W. Grimes /* 1124375386e2SMike Silbersack * Check for complete reassembly and perform frag per packet 1125375386e2SMike Silbersack * limiting. 1126375386e2SMike Silbersack * 1127375386e2SMike Silbersack * Frag limiting is performed here so that the nth frag has 1128375386e2SMike Silbersack * a chance to complete the packet before we drop the packet. 1129375386e2SMike Silbersack * As a result, n+1 frags are actually allowed per packet, but 1130375386e2SMike Silbersack * only n will ever be stored. (n = maxfragsperpacket.) 1131375386e2SMike Silbersack * 1132df8bae1dSRodney W. Grimes */ 11336effc713SDoug Rabson next = 0; 11346effc713SDoug Rabson for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 11358f134647SGleb Smirnoff if (ntohs(GETIP(q)->ip_off) != next) { 113655c28800SGleb Smirnoff if (fp->ipq_nfrags > V_maxfragsperpacket) 113755c28800SGleb Smirnoff ipq_drop(head, fp); 1138f0cada84SAndre Oppermann goto done; 1139375386e2SMike Silbersack } 11408f134647SGleb Smirnoff next += ntohs(GETIP(q)->ip_len); 11416effc713SDoug Rabson } 11426effc713SDoug Rabson /* Make sure the last packet didn't have the IP_MF flag */ 1143b09dc7e3SAndre Oppermann if (p->m_flags & M_IP_FRAG) { 114455c28800SGleb Smirnoff if (fp->ipq_nfrags > V_maxfragsperpacket) 114555c28800SGleb Smirnoff ipq_drop(head, fp); 1146f0cada84SAndre Oppermann goto done; 1147375386e2SMike Silbersack } 1148df8bae1dSRodney W. Grimes 1149df8bae1dSRodney W. Grimes /* 1150430d30d8SBill Fenner * Reassembly is complete. Make sure the packet is a sane size. 1151430d30d8SBill Fenner */ 11526effc713SDoug Rabson q = fp->ipq_frags; 11536effc713SDoug Rabson ip = GETIP(q); 115453be11f6SPoul-Henning Kamp if (next + (ip->ip_hl << 2) > IP_MAXPACKET) { 115586425c62SRobert Watson IPSTAT_INC(ips_toolong); 115655c28800SGleb Smirnoff ipq_drop(head, fp); 1157f0cada84SAndre Oppermann goto done; 1158430d30d8SBill Fenner } 1159430d30d8SBill Fenner 1160430d30d8SBill Fenner /* 1161430d30d8SBill Fenner * Concatenate fragments. 1162df8bae1dSRodney W. Grimes */ 11636effc713SDoug Rabson m = q; 1164df8bae1dSRodney W. Grimes t = m->m_next; 116502410549SRobert Watson m->m_next = NULL; 1166df8bae1dSRodney W. Grimes m_cat(m, t); 11676effc713SDoug Rabson nq = q->m_nextpkt; 116802410549SRobert Watson q->m_nextpkt = NULL; 11696effc713SDoug Rabson for (q = nq; q != NULL; q = nq) { 11706effc713SDoug Rabson nq = q->m_nextpkt; 1171945aa40dSDoug Rabson q->m_nextpkt = NULL; 1172db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; 1173db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; 1174a8db1d93SJonathan Lemon m_cat(m, q); 1175df8bae1dSRodney W. Grimes } 11766edb555dSOleg Bulyzhin /* 11776edb555dSOleg Bulyzhin * In order to do checksumming faster we do 'end-around carry' here 11786edb555dSOleg Bulyzhin * (and not in for{} loop), though it implies we are not going to 11796edb555dSOleg Bulyzhin * reassemble more than 64k fragments. 11806edb555dSOleg Bulyzhin */ 1181c732cd1aSPyun YongHyeon while (m->m_pkthdr.csum_data & 0xffff0000) 1182c732cd1aSPyun YongHyeon m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) + 1183c732cd1aSPyun YongHyeon (m->m_pkthdr.csum_data >> 16); 118436b0360bSRobert Watson #ifdef MAC 118530d239bcSRobert Watson mac_ipq_reassemble(fp, m); 118630d239bcSRobert Watson mac_ipq_destroy(fp); 118736b0360bSRobert Watson #endif 1188df8bae1dSRodney W. Grimes 1189df8bae1dSRodney W. Grimes /* 1190f0cada84SAndre Oppermann * Create header for new ip packet by modifying header of first 1191f0cada84SAndre Oppermann * packet; dequeue and discard fragment reassembly header. 1192df8bae1dSRodney W. Grimes * Make header visible. 1193df8bae1dSRodney W. Grimes */ 11948f134647SGleb Smirnoff ip->ip_len = htons((ip->ip_hl << 2) + next); 11956effc713SDoug Rabson ip->ip_src = fp->ipq_src; 11966effc713SDoug Rabson ip->ip_dst = fp->ipq_dst; 1197462b86feSPoul-Henning Kamp TAILQ_REMOVE(head, fp, ipq_list); 1198603724d3SBjoern A. Zeeb V_nipq--; 1199603724d3SBjoern A. Zeeb uma_zfree(V_ipq_zone, fp); 120053be11f6SPoul-Henning Kamp m->m_len += (ip->ip_hl << 2); 120153be11f6SPoul-Henning Kamp m->m_data -= (ip->ip_hl << 2); 1202df8bae1dSRodney W. Grimes /* some debugging cruft by sklower, below, will go away soon */ 1203a5554bf0SPoul-Henning Kamp if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */ 1204a5554bf0SPoul-Henning Kamp m_fixhdr(m); 120586425c62SRobert Watson IPSTAT_INC(ips_reassembled); 1206f59e59d5SAdrian Chadd IPQ_UNLOCK(hash); 1207f59e59d5SAdrian Chadd 1208f59e59d5SAdrian Chadd /* 1209f59e59d5SAdrian Chadd * Do the delayed purge to keep fragment counts under 1210f59e59d5SAdrian Chadd * the configured maximum. 1211f59e59d5SAdrian Chadd * 1212f59e59d5SAdrian Chadd * This is delayed so that it's not done with another IPQ bucket 1213f59e59d5SAdrian Chadd * lock held. 1214f59e59d5SAdrian Chadd * 1215f59e59d5SAdrian Chadd * Note that we pass in the bucket to /skip/ over, not 1216f59e59d5SAdrian Chadd * the bucket to /purge/. 1217f59e59d5SAdrian Chadd */ 1218f59e59d5SAdrian Chadd if (do_purge) 1219f59e59d5SAdrian Chadd ip_reass_purge_element(hash); 1220b8bc95cdSAdrian Chadd 1221b8bc95cdSAdrian Chadd #ifdef RSS 1222b8bc95cdSAdrian Chadd /* 1223b8bc95cdSAdrian Chadd * Query the RSS layer for the flowid / flowtype for the 1224b8bc95cdSAdrian Chadd * mbuf payload. 1225b8bc95cdSAdrian Chadd * 1226b8bc95cdSAdrian Chadd * For now, just assume we have to calculate a new one. 1227b8bc95cdSAdrian Chadd * Later on we should check to see if the assigned flowid matches 1228b8bc95cdSAdrian Chadd * what RSS wants for the given IP protocol and if so, just keep it. 1229b8bc95cdSAdrian Chadd * 1230b8bc95cdSAdrian Chadd * We then queue into the relevant netisr so it can be dispatched 1231b8bc95cdSAdrian Chadd * to the correct CPU. 1232b8bc95cdSAdrian Chadd * 1233b8bc95cdSAdrian Chadd * Note - this may return 1, which means the flowid in the mbuf 1234b8bc95cdSAdrian Chadd * is correct for the configured RSS hash types and can be used. 1235b8bc95cdSAdrian Chadd */ 1236b8bc95cdSAdrian Chadd if (rss_mbuf_software_hash_v4(m, 0, &rss_hash, &rss_type) == 0) { 1237b8bc95cdSAdrian Chadd m->m_pkthdr.flowid = rss_hash; 1238b8bc95cdSAdrian Chadd M_HASHTYPE_SET(m, rss_type); 1239b8bc95cdSAdrian Chadd } 1240b8bc95cdSAdrian Chadd 1241b8bc95cdSAdrian Chadd /* 1242b8bc95cdSAdrian Chadd * Queue/dispatch for reprocessing. 1243b8bc95cdSAdrian Chadd * 1244b8bc95cdSAdrian Chadd * Note: this is much slower than just handling the frame in the 1245b8bc95cdSAdrian Chadd * current receive context. It's likely worth investigating 1246b8bc95cdSAdrian Chadd * why this is. 1247b8bc95cdSAdrian Chadd */ 1248b8bc95cdSAdrian Chadd netisr_dispatch(NETISR_IP_DIRECT, m); 1249b8bc95cdSAdrian Chadd return (NULL); 1250b8bc95cdSAdrian Chadd #endif 1251b8bc95cdSAdrian Chadd 1252b8bc95cdSAdrian Chadd /* Handle in-line */ 12536a800098SYoshinobu Inoue return (m); 1254df8bae1dSRodney W. Grimes 1255df8bae1dSRodney W. Grimes dropfrag: 125686425c62SRobert Watson IPSTAT_INC(ips_fragdropped); 1257042bbfa3SRobert Watson if (fp != NULL) 1258375386e2SMike Silbersack fp->ipq_nfrags--; 1259df8bae1dSRodney W. Grimes m_freem(m); 1260f0cada84SAndre Oppermann done: 1261f59e59d5SAdrian Chadd IPQ_UNLOCK(hash); 1262f0cada84SAndre Oppermann return (NULL); 12636effc713SDoug Rabson 12646effc713SDoug Rabson #undef GETIP 1265df8bae1dSRodney W. Grimes } 1266df8bae1dSRodney W. Grimes 1267df8bae1dSRodney W. Grimes /* 1268df8bae1dSRodney W. Grimes * Free a fragment reassembly header and all 1269df8bae1dSRodney W. Grimes * associated datagrams. 1270df8bae1dSRodney W. Grimes */ 12710312fbe9SPoul-Henning Kamp static void 127255c28800SGleb Smirnoff ipq_free(struct ipqhead *fhp, struct ipq *fp) 1273df8bae1dSRodney W. Grimes { 1274f2565d68SRobert Watson struct mbuf *q; 1275df8bae1dSRodney W. Grimes 12766effc713SDoug Rabson while (fp->ipq_frags) { 12776effc713SDoug Rabson q = fp->ipq_frags; 12786effc713SDoug Rabson fp->ipq_frags = q->m_nextpkt; 12796effc713SDoug Rabson m_freem(q); 1280df8bae1dSRodney W. Grimes } 1281462b86feSPoul-Henning Kamp TAILQ_REMOVE(fhp, fp, ipq_list); 1282603724d3SBjoern A. Zeeb uma_zfree(V_ipq_zone, fp); 1283603724d3SBjoern A. Zeeb V_nipq--; 1284df8bae1dSRodney W. Grimes } 1285df8bae1dSRodney W. Grimes 1286df8bae1dSRodney W. Grimes /* 1287df8bae1dSRodney W. Grimes * IP timer processing; 1288df8bae1dSRodney W. Grimes * if a timer expires on a reassembly 1289df8bae1dSRodney W. Grimes * queue, discard it. 1290df8bae1dSRodney W. Grimes */ 1291df8bae1dSRodney W. Grimes void 1292f2565d68SRobert Watson ip_slowtimo(void) 1293df8bae1dSRodney W. Grimes { 12948b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 129571c70e13SGleb Smirnoff struct ipq *fp, *tmp; 1296194a213eSAndrey A. Chernov int i; 1297df8bae1dSRodney W. Grimes 12985ee847d3SRobert Watson VNET_LIST_RLOCK_NOSLEEP(); 12998b615593SMarko Zec VNET_FOREACH(vnet_iter) { 13008b615593SMarko Zec CURVNET_SET(vnet_iter); 1301194a213eSAndrey A. Chernov for (i = 0; i < IPREASS_NHASH; i++) { 1302f59e59d5SAdrian Chadd IPQ_LOCK(i); 1303*e3c2c634SGleb Smirnoff TAILQ_FOREACH_SAFE(fp, &V_ipq[i].head, ipq_list, tmp) 130471c70e13SGleb Smirnoff if (--fp->ipq_ttl == 0) 1305*e3c2c634SGleb Smirnoff ipq_timeout(&V_ipq[i].head, fp); 1306f59e59d5SAdrian Chadd IPQ_UNLOCK(i); 1307194a213eSAndrey A. Chernov } 1308690a6055SJesper Skriver /* 1309690a6055SJesper Skriver * If we are over the maximum number of fragments 1310690a6055SJesper Skriver * (due to the limit being lowered), drain off 1311690a6055SJesper Skriver * enough to get down to the new limit. 1312690a6055SJesper Skriver */ 1313603724d3SBjoern A. Zeeb if (V_maxnipq >= 0 && V_nipq > V_maxnipq) { 1314690a6055SJesper Skriver for (i = 0; i < IPREASS_NHASH; i++) { 1315f59e59d5SAdrian Chadd IPQ_LOCK(i); 13168b615593SMarko Zec while (V_nipq > V_maxnipq && 1317*e3c2c634SGleb Smirnoff !TAILQ_EMPTY(&V_ipq[i].head)) 1318*e3c2c634SGleb Smirnoff ipq_drop(&V_ipq[i].head, 1319*e3c2c634SGleb Smirnoff TAILQ_FIRST(&V_ipq[i].head)); 1320f59e59d5SAdrian Chadd IPQ_UNLOCK(i); 1321690a6055SJesper Skriver } 1322690a6055SJesper Skriver } 13238b615593SMarko Zec CURVNET_RESTORE(); 13248b615593SMarko Zec } 13255ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 1326df8bae1dSRodney W. Grimes } 1327df8bae1dSRodney W. Grimes 1328df8bae1dSRodney W. Grimes /* 1329df8bae1dSRodney W. Grimes * Drain off all datagram fragments. 1330df8bae1dSRodney W. Grimes */ 13319802380eSBjoern A. Zeeb static void 13323de5805bSGleb Smirnoff ip_drain_vnet(void) 1333df8bae1dSRodney W. Grimes { 1334194a213eSAndrey A. Chernov int i; 1335ce29ab3aSGarrett Wollman 1336194a213eSAndrey A. Chernov for (i = 0; i < IPREASS_NHASH; i++) { 1337f59e59d5SAdrian Chadd IPQ_LOCK(i); 1338*e3c2c634SGleb Smirnoff while(!TAILQ_EMPTY(&V_ipq[i].head)) 1339*e3c2c634SGleb Smirnoff ipq_drop(&V_ipq[i].head, TAILQ_FIRST(&V_ipq[i].head)); 1340f59e59d5SAdrian Chadd IPQ_UNLOCK(i); 1341194a213eSAndrey A. Chernov } 13429802380eSBjoern A. Zeeb } 13439802380eSBjoern A. Zeeb 13449802380eSBjoern A. Zeeb void 13459802380eSBjoern A. Zeeb ip_drain(void) 13469802380eSBjoern A. Zeeb { 13479802380eSBjoern A. Zeeb VNET_ITERATOR_DECL(vnet_iter); 13489802380eSBjoern A. Zeeb 13499802380eSBjoern A. Zeeb VNET_LIST_RLOCK_NOSLEEP(); 13509802380eSBjoern A. Zeeb VNET_FOREACH(vnet_iter) { 13519802380eSBjoern A. Zeeb CURVNET_SET(vnet_iter); 13523de5805bSGleb Smirnoff ip_drain_vnet(); 13538b615593SMarko Zec CURVNET_RESTORE(); 13548b615593SMarko Zec } 13555ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 1356df8bae1dSRodney W. Grimes } 1357df8bae1dSRodney W. Grimes 1358df8bae1dSRodney W. Grimes /* 1359de38924dSAndre Oppermann * The protocol to be inserted into ip_protox[] must be already registered 1360de38924dSAndre Oppermann * in inetsw[], either statically or through pf_proto_register(). 1361de38924dSAndre Oppermann */ 1362de38924dSAndre Oppermann int 13631b48d245SBjoern A. Zeeb ipproto_register(short ipproto) 1364de38924dSAndre Oppermann { 1365de38924dSAndre Oppermann struct protosw *pr; 1366de38924dSAndre Oppermann 1367de38924dSAndre Oppermann /* Sanity checks. */ 13681b48d245SBjoern A. Zeeb if (ipproto <= 0 || ipproto >= IPPROTO_MAX) 1369de38924dSAndre Oppermann return (EPROTONOSUPPORT); 1370de38924dSAndre Oppermann 1371de38924dSAndre Oppermann /* 1372de38924dSAndre Oppermann * The protocol slot must not be occupied by another protocol 1373de38924dSAndre Oppermann * already. An index pointing to IPPROTO_RAW is unused. 1374de38924dSAndre Oppermann */ 1375de38924dSAndre Oppermann pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 1376de38924dSAndre Oppermann if (pr == NULL) 1377de38924dSAndre Oppermann return (EPFNOSUPPORT); 1378de38924dSAndre Oppermann if (ip_protox[ipproto] != pr - inetsw) /* IPPROTO_RAW */ 1379de38924dSAndre Oppermann return (EEXIST); 1380de38924dSAndre Oppermann 1381de38924dSAndre Oppermann /* Find the protocol position in inetsw[] and set the index. */ 1382de38924dSAndre Oppermann for (pr = inetdomain.dom_protosw; 1383de38924dSAndre Oppermann pr < inetdomain.dom_protoswNPROTOSW; pr++) { 1384de38924dSAndre Oppermann if (pr->pr_domain->dom_family == PF_INET && 1385de38924dSAndre Oppermann pr->pr_protocol && pr->pr_protocol == ipproto) { 1386de38924dSAndre Oppermann ip_protox[pr->pr_protocol] = pr - inetsw; 1387de38924dSAndre Oppermann return (0); 1388de38924dSAndre Oppermann } 1389de38924dSAndre Oppermann } 1390de38924dSAndre Oppermann return (EPROTONOSUPPORT); 1391de38924dSAndre Oppermann } 1392de38924dSAndre Oppermann 1393de38924dSAndre Oppermann int 13941b48d245SBjoern A. Zeeb ipproto_unregister(short ipproto) 1395de38924dSAndre Oppermann { 1396de38924dSAndre Oppermann struct protosw *pr; 1397de38924dSAndre Oppermann 1398de38924dSAndre Oppermann /* Sanity checks. */ 13991b48d245SBjoern A. Zeeb if (ipproto <= 0 || ipproto >= IPPROTO_MAX) 1400de38924dSAndre Oppermann return (EPROTONOSUPPORT); 1401de38924dSAndre Oppermann 1402de38924dSAndre Oppermann /* Check if the protocol was indeed registered. */ 1403de38924dSAndre Oppermann pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 1404de38924dSAndre Oppermann if (pr == NULL) 1405de38924dSAndre Oppermann return (EPFNOSUPPORT); 1406de38924dSAndre Oppermann if (ip_protox[ipproto] == pr - inetsw) /* IPPROTO_RAW */ 1407de38924dSAndre Oppermann return (ENOENT); 1408de38924dSAndre Oppermann 1409de38924dSAndre Oppermann /* Reset the protocol slot to IPPROTO_RAW. */ 1410de38924dSAndre Oppermann ip_protox[ipproto] = pr - inetsw; 1411de38924dSAndre Oppermann return (0); 1412de38924dSAndre Oppermann } 1413de38924dSAndre Oppermann 1414df8bae1dSRodney W. Grimes /* 14158c0fec80SRobert Watson * Given address of next destination (final or next hop), return (referenced) 14168c0fec80SRobert Watson * internet address info of interface to be used to get there. 1417df8bae1dSRodney W. Grimes */ 1418bd714208SRuslan Ermilov struct in_ifaddr * 14198b07e49aSJulian Elischer ip_rtaddr(struct in_addr dst, u_int fibnum) 1420df8bae1dSRodney W. Grimes { 142197d8d152SAndre Oppermann struct route sro; 142202c1c707SAndre Oppermann struct sockaddr_in *sin; 142319e5b0a7SRobert Watson struct in_ifaddr *ia; 1424df8bae1dSRodney W. Grimes 14250cfbbe3bSAndre Oppermann bzero(&sro, sizeof(sro)); 142697d8d152SAndre Oppermann sin = (struct sockaddr_in *)&sro.ro_dst; 1427df8bae1dSRodney W. Grimes sin->sin_family = AF_INET; 1428df8bae1dSRodney W. Grimes sin->sin_len = sizeof(*sin); 1429df8bae1dSRodney W. Grimes sin->sin_addr = dst; 14306e6b3f7cSQing Li in_rtalloc_ign(&sro, 0, fibnum); 1431df8bae1dSRodney W. Grimes 143297d8d152SAndre Oppermann if (sro.ro_rt == NULL) 143302410549SRobert Watson return (NULL); 143402c1c707SAndre Oppermann 143519e5b0a7SRobert Watson ia = ifatoia(sro.ro_rt->rt_ifa); 143619e5b0a7SRobert Watson ifa_ref(&ia->ia_ifa); 143797d8d152SAndre Oppermann RTFREE(sro.ro_rt); 143819e5b0a7SRobert Watson return (ia); 1439df8bae1dSRodney W. Grimes } 1440df8bae1dSRodney W. Grimes 1441df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = { 1442df8bae1dSRodney W. Grimes 0, 0, 0, 0, 1443df8bae1dSRodney W. Grimes 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1444df8bae1dSRodney W. Grimes EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1445df8bae1dSRodney W. Grimes EMSGSIZE, EHOSTUNREACH, 0, 0, 1446fcaf9f91SMike Silbersack 0, 0, EHOSTUNREACH, 0, 14473b8123b7SJesper Skriver ENOPROTOOPT, ECONNREFUSED 1448df8bae1dSRodney W. Grimes }; 1449df8bae1dSRodney W. Grimes 1450df8bae1dSRodney W. Grimes /* 1451df8bae1dSRodney W. Grimes * Forward a packet. If some error occurs return the sender 1452df8bae1dSRodney W. Grimes * an icmp packet. Note we can't always generate a meaningful 1453df8bae1dSRodney W. Grimes * icmp message because icmp doesn't have a large enough repertoire 1454df8bae1dSRodney W. Grimes * of codes and types. 1455df8bae1dSRodney W. Grimes * 1456df8bae1dSRodney W. Grimes * If not forwarding, just drop the packet. This could be confusing 1457df8bae1dSRodney W. Grimes * if ipforwarding was zero but some routing protocol was advancing 1458df8bae1dSRodney W. Grimes * us as a gateway to somewhere. However, we must let the routing 1459df8bae1dSRodney W. Grimes * protocol deal with that. 1460df8bae1dSRodney W. Grimes * 1461df8bae1dSRodney W. Grimes * The srcrt parameter indicates whether the packet is being forwarded 1462df8bae1dSRodney W. Grimes * via a source route. 1463df8bae1dSRodney W. Grimes */ 14649b932e9eSAndre Oppermann void 14659b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt) 1466df8bae1dSRodney W. Grimes { 14672b25acc1SLuigi Rizzo struct ip *ip = mtod(m, struct ip *); 1468efbad259SEdward Tomasz Napierala struct in_ifaddr *ia; 1469df8bae1dSRodney W. Grimes struct mbuf *mcopy; 14709b932e9eSAndre Oppermann struct in_addr dest; 1471b835b6feSBjoern A. Zeeb struct route ro; 1472c773494eSAndre Oppermann int error, type = 0, code = 0, mtu = 0; 14733efc3014SJulian Elischer 14749b932e9eSAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 147586425c62SRobert Watson IPSTAT_INC(ips_cantforward); 1476df8bae1dSRodney W. Grimes m_freem(m); 1477df8bae1dSRodney W. Grimes return; 1478df8bae1dSRodney W. Grimes } 14798922ddbeSAndrey V. Elsukov #ifdef IPSEC 14808922ddbeSAndrey V. Elsukov if (ip_ipsec_fwd(m) != 0) { 14818922ddbeSAndrey V. Elsukov IPSTAT_INC(ips_cantforward); 14828922ddbeSAndrey V. Elsukov m_freem(m); 14838922ddbeSAndrey V. Elsukov return; 14848922ddbeSAndrey V. Elsukov } 14858922ddbeSAndrey V. Elsukov #endif /* IPSEC */ 14861b968362SDag-Erling Smørgrav #ifdef IPSTEALTH 1487603724d3SBjoern A. Zeeb if (!V_ipstealth) { 14881b968362SDag-Erling Smørgrav #endif 1489df8bae1dSRodney W. Grimes if (ip->ip_ttl <= IPTTLDEC) { 14901b968362SDag-Erling Smørgrav icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 149102c1c707SAndre Oppermann 0, 0); 1492df8bae1dSRodney W. Grimes return; 1493df8bae1dSRodney W. Grimes } 14941b968362SDag-Erling Smørgrav #ifdef IPSTEALTH 14951b968362SDag-Erling Smørgrav } 14961b968362SDag-Erling Smørgrav #endif 1497df8bae1dSRodney W. Grimes 14988b07e49aSJulian Elischer ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m)); 1499efbad259SEdward Tomasz Napierala #ifndef IPSEC 1500efbad259SEdward Tomasz Napierala /* 1501efbad259SEdward Tomasz Napierala * 'ia' may be NULL if there is no route for this destination. 1502efbad259SEdward Tomasz Napierala * In case of IPsec, Don't discard it just yet, but pass it to 1503efbad259SEdward Tomasz Napierala * ip_output in case of outgoing IPsec policy. 1504efbad259SEdward Tomasz Napierala */ 1505d23d475fSGuido van Rooij if (!srcrt && ia == NULL) { 150602c1c707SAndre Oppermann icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 1507df8bae1dSRodney W. Grimes return; 150802c1c707SAndre Oppermann } 1509efbad259SEdward Tomasz Napierala #endif 1510df8bae1dSRodney W. Grimes 1511df8bae1dSRodney W. Grimes /* 1512bfef7ed4SIan Dowse * Save the IP header and at most 8 bytes of the payload, 1513bfef7ed4SIan Dowse * in case we need to generate an ICMP message to the src. 1514bfef7ed4SIan Dowse * 15154d2e3692SLuigi Rizzo * XXX this can be optimized a lot by saving the data in a local 15164d2e3692SLuigi Rizzo * buffer on the stack (72 bytes at most), and only allocating the 15174d2e3692SLuigi Rizzo * mbuf if really necessary. The vast majority of the packets 15184d2e3692SLuigi Rizzo * are forwarded without having to send an ICMP back (either 15194d2e3692SLuigi Rizzo * because unnecessary, or because rate limited), so we are 15204d2e3692SLuigi Rizzo * really we are wasting a lot of work here. 15214d2e3692SLuigi Rizzo * 1522bfef7ed4SIan Dowse * We don't use m_copy() because it might return a reference 1523bfef7ed4SIan Dowse * to a shared cluster. Both this function and ip_output() 1524bfef7ed4SIan Dowse * assume exclusive access to the IP header in `m', so any 1525bfef7ed4SIan Dowse * data in a cluster may change before we reach icmp_error(). 1526df8bae1dSRodney W. Grimes */ 1527dc4ad05eSGleb Smirnoff mcopy = m_gethdr(M_NOWAIT, m->m_type); 1528eb1b1807SGleb Smirnoff if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) { 15299967cafcSSam Leffler /* 15309967cafcSSam Leffler * It's probably ok if the pkthdr dup fails (because 15319967cafcSSam Leffler * the deep copy of the tag chain failed), but for now 15329967cafcSSam Leffler * be conservative and just discard the copy since 15339967cafcSSam Leffler * code below may some day want the tags. 15349967cafcSSam Leffler */ 15359967cafcSSam Leffler m_free(mcopy); 15369967cafcSSam Leffler mcopy = NULL; 15379967cafcSSam Leffler } 1538bfef7ed4SIan Dowse if (mcopy != NULL) { 15398f134647SGleb Smirnoff mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy)); 1540e6b0a570SBruce M Simpson mcopy->m_pkthdr.len = mcopy->m_len; 1541bfef7ed4SIan Dowse m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t)); 1542bfef7ed4SIan Dowse } 154304287599SRuslan Ermilov 154404287599SRuslan Ermilov #ifdef IPSTEALTH 1545603724d3SBjoern A. Zeeb if (!V_ipstealth) { 154604287599SRuslan Ermilov #endif 154704287599SRuslan Ermilov ip->ip_ttl -= IPTTLDEC; 154804287599SRuslan Ermilov #ifdef IPSTEALTH 154904287599SRuslan Ermilov } 155004287599SRuslan Ermilov #endif 1551df8bae1dSRodney W. Grimes 1552df8bae1dSRodney W. Grimes /* 1553df8bae1dSRodney W. Grimes * If forwarding packet using same interface that it came in on, 1554df8bae1dSRodney W. Grimes * perhaps should send a redirect to sender to shortcut a hop. 1555df8bae1dSRodney W. Grimes * Only send redirect if source is sending directly to us, 1556df8bae1dSRodney W. Grimes * and if packet was not source routed (or has any options). 1557df8bae1dSRodney W. Grimes * Also, don't send redirect if forwarding using a default route 1558df8bae1dSRodney W. Grimes * or a route modified by a redirect. 1559df8bae1dSRodney W. Grimes */ 15609b932e9eSAndre Oppermann dest.s_addr = 0; 1561efbad259SEdward Tomasz Napierala if (!srcrt && V_ipsendredirects && 1562efbad259SEdward Tomasz Napierala ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) { 156302c1c707SAndre Oppermann struct sockaddr_in *sin; 156402c1c707SAndre Oppermann struct rtentry *rt; 156502c1c707SAndre Oppermann 15660cfbbe3bSAndre Oppermann bzero(&ro, sizeof(ro)); 156702c1c707SAndre Oppermann sin = (struct sockaddr_in *)&ro.ro_dst; 156802c1c707SAndre Oppermann sin->sin_family = AF_INET; 156902c1c707SAndre Oppermann sin->sin_len = sizeof(*sin); 15709b932e9eSAndre Oppermann sin->sin_addr = ip->ip_dst; 15716e6b3f7cSQing Li in_rtalloc_ign(&ro, 0, M_GETFIB(m)); 157202c1c707SAndre Oppermann 157302c1c707SAndre Oppermann rt = ro.ro_rt; 157402c1c707SAndre Oppermann 157502c1c707SAndre Oppermann if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 15769b932e9eSAndre Oppermann satosin(rt_key(rt))->sin_addr.s_addr != 0) { 1577df8bae1dSRodney W. Grimes #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1578df8bae1dSRodney W. Grimes u_long src = ntohl(ip->ip_src.s_addr); 1579df8bae1dSRodney W. Grimes 1580df8bae1dSRodney W. Grimes if (RTA(rt) && 1581df8bae1dSRodney W. Grimes (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1582df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_GATEWAY) 15839b932e9eSAndre Oppermann dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr; 1584df8bae1dSRodney W. Grimes else 15859b932e9eSAndre Oppermann dest.s_addr = ip->ip_dst.s_addr; 1586df8bae1dSRodney W. Grimes /* Router requirements says to only send host redirects */ 1587df8bae1dSRodney W. Grimes type = ICMP_REDIRECT; 1588df8bae1dSRodney W. Grimes code = ICMP_REDIRECT_HOST; 1589df8bae1dSRodney W. Grimes } 1590df8bae1dSRodney W. Grimes } 159102c1c707SAndre Oppermann if (rt) 159202c1c707SAndre Oppermann RTFREE(rt); 159302c1c707SAndre Oppermann } 1594df8bae1dSRodney W. Grimes 1595b835b6feSBjoern A. Zeeb /* 1596b835b6feSBjoern A. Zeeb * Try to cache the route MTU from ip_output so we can consider it for 1597b835b6feSBjoern A. Zeeb * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191. 1598b835b6feSBjoern A. Zeeb */ 1599b835b6feSBjoern A. Zeeb bzero(&ro, sizeof(ro)); 1600b835b6feSBjoern A. Zeeb 1601b835b6feSBjoern A. Zeeb error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL); 1602b835b6feSBjoern A. Zeeb 1603b835b6feSBjoern A. Zeeb if (error == EMSGSIZE && ro.ro_rt) 1604e3a7aa6fSGleb Smirnoff mtu = ro.ro_rt->rt_mtu; 1605bf984051SGleb Smirnoff RO_RTFREE(&ro); 1606b835b6feSBjoern A. Zeeb 1607df8bae1dSRodney W. Grimes if (error) 160886425c62SRobert Watson IPSTAT_INC(ips_cantforward); 1609df8bae1dSRodney W. Grimes else { 161086425c62SRobert Watson IPSTAT_INC(ips_forward); 1611df8bae1dSRodney W. Grimes if (type) 161286425c62SRobert Watson IPSTAT_INC(ips_redirectsent); 1613df8bae1dSRodney W. Grimes else { 16149188b4a1SAndre Oppermann if (mcopy) 1615df8bae1dSRodney W. Grimes m_freem(mcopy); 16168c0fec80SRobert Watson if (ia != NULL) 16178c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 1618df8bae1dSRodney W. Grimes return; 1619df8bae1dSRodney W. Grimes } 1620df8bae1dSRodney W. Grimes } 16218c0fec80SRobert Watson if (mcopy == NULL) { 16228c0fec80SRobert Watson if (ia != NULL) 16238c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 1624df8bae1dSRodney W. Grimes return; 16258c0fec80SRobert Watson } 1626df8bae1dSRodney W. Grimes 1627df8bae1dSRodney W. Grimes switch (error) { 1628df8bae1dSRodney W. Grimes 1629df8bae1dSRodney W. Grimes case 0: /* forwarded, but need redirect */ 1630df8bae1dSRodney W. Grimes /* type, code set above */ 1631df8bae1dSRodney W. Grimes break; 1632df8bae1dSRodney W. Grimes 1633efbad259SEdward Tomasz Napierala case ENETUNREACH: 1634df8bae1dSRodney W. Grimes case EHOSTUNREACH: 1635df8bae1dSRodney W. Grimes case ENETDOWN: 1636df8bae1dSRodney W. Grimes case EHOSTDOWN: 1637df8bae1dSRodney W. Grimes default: 1638df8bae1dSRodney W. Grimes type = ICMP_UNREACH; 1639df8bae1dSRodney W. Grimes code = ICMP_UNREACH_HOST; 1640df8bae1dSRodney W. Grimes break; 1641df8bae1dSRodney W. Grimes 1642df8bae1dSRodney W. Grimes case EMSGSIZE: 1643df8bae1dSRodney W. Grimes type = ICMP_UNREACH; 1644df8bae1dSRodney W. Grimes code = ICMP_UNREACH_NEEDFRAG; 16451dfcf0d2SAndre Oppermann 1646b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 1647b835b6feSBjoern A. Zeeb /* 1648b835b6feSBjoern A. Zeeb * If IPsec is configured for this path, 1649b835b6feSBjoern A. Zeeb * override any possibly mtu value set by ip_output. 1650b835b6feSBjoern A. Zeeb */ 16511c044382SBjoern A. Zeeb mtu = ip_ipsec_mtu(mcopy, mtu); 1652b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 16539b932e9eSAndre Oppermann /* 1654b835b6feSBjoern A. Zeeb * If the MTU was set before make sure we are below the 1655b835b6feSBjoern A. Zeeb * interface MTU. 1656ab48768bSAndre Oppermann * If the MTU wasn't set before use the interface mtu or 1657ab48768bSAndre Oppermann * fall back to the next smaller mtu step compared to the 1658ab48768bSAndre Oppermann * current packet size. 16599b932e9eSAndre Oppermann */ 1660b835b6feSBjoern A. Zeeb if (mtu != 0) { 1661b835b6feSBjoern A. Zeeb if (ia != NULL) 1662b835b6feSBjoern A. Zeeb mtu = min(mtu, ia->ia_ifp->if_mtu); 1663b835b6feSBjoern A. Zeeb } else { 1664ab48768bSAndre Oppermann if (ia != NULL) 1665c773494eSAndre Oppermann mtu = ia->ia_ifp->if_mtu; 1666ab48768bSAndre Oppermann else 16678f134647SGleb Smirnoff mtu = ip_next_mtu(ntohs(ip->ip_len), 0); 1668ab48768bSAndre Oppermann } 166986425c62SRobert Watson IPSTAT_INC(ips_cantfrag); 1670df8bae1dSRodney W. Grimes break; 1671df8bae1dSRodney W. Grimes 1672df8bae1dSRodney W. Grimes case ENOBUFS: 16733a06e3e0SRuslan Ermilov case EACCES: /* ipfw denied packet */ 16743a06e3e0SRuslan Ermilov m_freem(mcopy); 16758c0fec80SRobert Watson if (ia != NULL) 16768c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 16773a06e3e0SRuslan Ermilov return; 1678df8bae1dSRodney W. Grimes } 16798c0fec80SRobert Watson if (ia != NULL) 16808c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 1681c773494eSAndre Oppermann icmp_error(mcopy, type, code, dest.s_addr, mtu); 1682df8bae1dSRodney W. Grimes } 1683df8bae1dSRodney W. Grimes 168482c23ebaSBill Fenner void 1685f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip, 1686f2565d68SRobert Watson struct mbuf *m) 168782c23ebaSBill Fenner { 16888b615593SMarko Zec 1689be8a62e8SPoul-Henning Kamp if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) { 1690be8a62e8SPoul-Henning Kamp struct bintime bt; 1691be8a62e8SPoul-Henning Kamp 1692be8a62e8SPoul-Henning Kamp bintime(&bt); 1693be8a62e8SPoul-Henning Kamp if (inp->inp_socket->so_options & SO_BINTIME) { 1694be8a62e8SPoul-Henning Kamp *mp = sbcreatecontrol((caddr_t)&bt, sizeof(bt), 1695be8a62e8SPoul-Henning Kamp SCM_BINTIME, SOL_SOCKET); 1696be8a62e8SPoul-Henning Kamp if (*mp) 1697be8a62e8SPoul-Henning Kamp mp = &(*mp)->m_next; 1698be8a62e8SPoul-Henning Kamp } 169982c23ebaSBill Fenner if (inp->inp_socket->so_options & SO_TIMESTAMP) { 170082c23ebaSBill Fenner struct timeval tv; 170182c23ebaSBill Fenner 1702be8a62e8SPoul-Henning Kamp bintime2timeval(&bt, &tv); 170382c23ebaSBill Fenner *mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv), 170482c23ebaSBill Fenner SCM_TIMESTAMP, SOL_SOCKET); 170582c23ebaSBill Fenner if (*mp) 170682c23ebaSBill Fenner mp = &(*mp)->m_next; 17074cc20ab1SSeigo Tanimura } 1708be8a62e8SPoul-Henning Kamp } 170982c23ebaSBill Fenner if (inp->inp_flags & INP_RECVDSTADDR) { 171082c23ebaSBill Fenner *mp = sbcreatecontrol((caddr_t)&ip->ip_dst, 171182c23ebaSBill Fenner sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 171282c23ebaSBill Fenner if (*mp) 171382c23ebaSBill Fenner mp = &(*mp)->m_next; 171482c23ebaSBill Fenner } 17154957466bSMatthew N. Dodd if (inp->inp_flags & INP_RECVTTL) { 17164957466bSMatthew N. Dodd *mp = sbcreatecontrol((caddr_t)&ip->ip_ttl, 17174957466bSMatthew N. Dodd sizeof(u_char), IP_RECVTTL, IPPROTO_IP); 17184957466bSMatthew N. Dodd if (*mp) 17194957466bSMatthew N. Dodd mp = &(*mp)->m_next; 17204957466bSMatthew N. Dodd } 172182c23ebaSBill Fenner #ifdef notyet 172282c23ebaSBill Fenner /* XXX 172382c23ebaSBill Fenner * Moving these out of udp_input() made them even more broken 172482c23ebaSBill Fenner * than they already were. 172582c23ebaSBill Fenner */ 172682c23ebaSBill Fenner /* options were tossed already */ 172782c23ebaSBill Fenner if (inp->inp_flags & INP_RECVOPTS) { 172882c23ebaSBill Fenner *mp = sbcreatecontrol((caddr_t)opts_deleted_above, 172982c23ebaSBill Fenner sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 173082c23ebaSBill Fenner if (*mp) 173182c23ebaSBill Fenner mp = &(*mp)->m_next; 173282c23ebaSBill Fenner } 173382c23ebaSBill Fenner /* ip_srcroute doesn't do what we want here, need to fix */ 173482c23ebaSBill Fenner if (inp->inp_flags & INP_RECVRETOPTS) { 1735e0982661SAndre Oppermann *mp = sbcreatecontrol((caddr_t)ip_srcroute(m), 173682c23ebaSBill Fenner sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 173782c23ebaSBill Fenner if (*mp) 173882c23ebaSBill Fenner mp = &(*mp)->m_next; 173982c23ebaSBill Fenner } 174082c23ebaSBill Fenner #endif 174182c23ebaSBill Fenner if (inp->inp_flags & INP_RECVIF) { 1742d314ad7bSJulian Elischer struct ifnet *ifp; 1743d314ad7bSJulian Elischer struct sdlbuf { 174482c23ebaSBill Fenner struct sockaddr_dl sdl; 1745d314ad7bSJulian Elischer u_char pad[32]; 1746d314ad7bSJulian Elischer } sdlbuf; 1747d314ad7bSJulian Elischer struct sockaddr_dl *sdp; 1748d314ad7bSJulian Elischer struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 174982c23ebaSBill Fenner 175046f2df9cSSergey Kandaurov if ((ifp = m->m_pkthdr.rcvif) && 175146f2df9cSSergey Kandaurov ifp->if_index && ifp->if_index <= V_if_index) { 17524a0d6638SRuslan Ermilov sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr; 1753d314ad7bSJulian Elischer /* 1754d314ad7bSJulian Elischer * Change our mind and don't try copy. 1755d314ad7bSJulian Elischer */ 175646f2df9cSSergey Kandaurov if (sdp->sdl_family != AF_LINK || 175746f2df9cSSergey Kandaurov sdp->sdl_len > sizeof(sdlbuf)) { 1758d314ad7bSJulian Elischer goto makedummy; 1759d314ad7bSJulian Elischer } 1760d314ad7bSJulian Elischer bcopy(sdp, sdl2, sdp->sdl_len); 1761d314ad7bSJulian Elischer } else { 1762d314ad7bSJulian Elischer makedummy: 176346f2df9cSSergey Kandaurov sdl2->sdl_len = 176446f2df9cSSergey Kandaurov offsetof(struct sockaddr_dl, sdl_data[0]); 1765d314ad7bSJulian Elischer sdl2->sdl_family = AF_LINK; 1766d314ad7bSJulian Elischer sdl2->sdl_index = 0; 1767d314ad7bSJulian Elischer sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 1768d314ad7bSJulian Elischer } 1769d314ad7bSJulian Elischer *mp = sbcreatecontrol((caddr_t)sdl2, sdl2->sdl_len, 177082c23ebaSBill Fenner IP_RECVIF, IPPROTO_IP); 177182c23ebaSBill Fenner if (*mp) 177282c23ebaSBill Fenner mp = &(*mp)->m_next; 177382c23ebaSBill Fenner } 17743cca425bSMichael Tuexen if (inp->inp_flags & INP_RECVTOS) { 17753cca425bSMichael Tuexen *mp = sbcreatecontrol((caddr_t)&ip->ip_tos, 17763cca425bSMichael Tuexen sizeof(u_char), IP_RECVTOS, IPPROTO_IP); 17773cca425bSMichael Tuexen if (*mp) 17783cca425bSMichael Tuexen mp = &(*mp)->m_next; 17793cca425bSMichael Tuexen } 17809d3ddf43SAdrian Chadd 17819d3ddf43SAdrian Chadd if (inp->inp_flags2 & INP_RECVFLOWID) { 17829d3ddf43SAdrian Chadd uint32_t flowid, flow_type; 17839d3ddf43SAdrian Chadd 17849d3ddf43SAdrian Chadd flowid = m->m_pkthdr.flowid; 17859d3ddf43SAdrian Chadd flow_type = M_HASHTYPE_GET(m); 17869d3ddf43SAdrian Chadd 17879d3ddf43SAdrian Chadd /* 17889d3ddf43SAdrian Chadd * XXX should handle the failure of one or the 17899d3ddf43SAdrian Chadd * other - don't populate both? 17909d3ddf43SAdrian Chadd */ 17919d3ddf43SAdrian Chadd *mp = sbcreatecontrol((caddr_t) &flowid, 17929d3ddf43SAdrian Chadd sizeof(uint32_t), IP_FLOWID, IPPROTO_IP); 17939d3ddf43SAdrian Chadd if (*mp) 17949d3ddf43SAdrian Chadd mp = &(*mp)->m_next; 17959d3ddf43SAdrian Chadd *mp = sbcreatecontrol((caddr_t) &flow_type, 17969d3ddf43SAdrian Chadd sizeof(uint32_t), IP_FLOWTYPE, IPPROTO_IP); 17979d3ddf43SAdrian Chadd if (*mp) 17989d3ddf43SAdrian Chadd mp = &(*mp)->m_next; 17999d3ddf43SAdrian Chadd } 18009d3ddf43SAdrian Chadd 18019d3ddf43SAdrian Chadd #ifdef RSS 18029d3ddf43SAdrian Chadd if (inp->inp_flags2 & INP_RECVRSSBUCKETID) { 18039d3ddf43SAdrian Chadd uint32_t flowid, flow_type; 18049d3ddf43SAdrian Chadd uint32_t rss_bucketid; 18059d3ddf43SAdrian Chadd 18069d3ddf43SAdrian Chadd flowid = m->m_pkthdr.flowid; 18079d3ddf43SAdrian Chadd flow_type = M_HASHTYPE_GET(m); 18089d3ddf43SAdrian Chadd 18099d3ddf43SAdrian Chadd if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) { 18109d3ddf43SAdrian Chadd *mp = sbcreatecontrol((caddr_t) &rss_bucketid, 18119d3ddf43SAdrian Chadd sizeof(uint32_t), IP_RSSBUCKETID, IPPROTO_IP); 18129d3ddf43SAdrian Chadd if (*mp) 18139d3ddf43SAdrian Chadd mp = &(*mp)->m_next; 18149d3ddf43SAdrian Chadd } 18159d3ddf43SAdrian Chadd } 18169d3ddf43SAdrian Chadd #endif 181782c23ebaSBill Fenner } 181882c23ebaSBill Fenner 18194d2e3692SLuigi Rizzo /* 182030916a2dSRobert Watson * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the 182130916a2dSRobert Watson * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on 182230916a2dSRobert Watson * locking. This code remains in ip_input.c as ip_mroute.c is optionally 182330916a2dSRobert Watson * compiled. 18244d2e3692SLuigi Rizzo */ 18253e288e62SDimitry Andric static VNET_DEFINE(int, ip_rsvp_on); 182682cea7e6SBjoern A. Zeeb VNET_DEFINE(struct socket *, ip_rsvpd); 182782cea7e6SBjoern A. Zeeb 182882cea7e6SBjoern A. Zeeb #define V_ip_rsvp_on VNET(ip_rsvp_on) 182982cea7e6SBjoern A. Zeeb 1830df8bae1dSRodney W. Grimes int 1831f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so) 1832f0068c4aSGarrett Wollman { 18338b615593SMarko Zec 1834f0068c4aSGarrett Wollman if (so->so_type != SOCK_RAW || 1835f0068c4aSGarrett Wollman so->so_proto->pr_protocol != IPPROTO_RSVP) 1836f0068c4aSGarrett Wollman return EOPNOTSUPP; 1837f0068c4aSGarrett Wollman 1838603724d3SBjoern A. Zeeb if (V_ip_rsvpd != NULL) 1839f0068c4aSGarrett Wollman return EADDRINUSE; 1840f0068c4aSGarrett Wollman 1841603724d3SBjoern A. Zeeb V_ip_rsvpd = so; 18421c5de19aSGarrett Wollman /* 18431c5de19aSGarrett Wollman * This may seem silly, but we need to be sure we don't over-increment 18441c5de19aSGarrett Wollman * the RSVP counter, in case something slips up. 18451c5de19aSGarrett Wollman */ 1846603724d3SBjoern A. Zeeb if (!V_ip_rsvp_on) { 1847603724d3SBjoern A. Zeeb V_ip_rsvp_on = 1; 1848603724d3SBjoern A. Zeeb V_rsvp_on++; 18491c5de19aSGarrett Wollman } 1850f0068c4aSGarrett Wollman 1851f0068c4aSGarrett Wollman return 0; 1852f0068c4aSGarrett Wollman } 1853f0068c4aSGarrett Wollman 1854f0068c4aSGarrett Wollman int 1855f0068c4aSGarrett Wollman ip_rsvp_done(void) 1856f0068c4aSGarrett Wollman { 18578b615593SMarko Zec 1858603724d3SBjoern A. Zeeb V_ip_rsvpd = NULL; 18591c5de19aSGarrett Wollman /* 18601c5de19aSGarrett Wollman * This may seem silly, but we need to be sure we don't over-decrement 18611c5de19aSGarrett Wollman * the RSVP counter, in case something slips up. 18621c5de19aSGarrett Wollman */ 1863603724d3SBjoern A. Zeeb if (V_ip_rsvp_on) { 1864603724d3SBjoern A. Zeeb V_ip_rsvp_on = 0; 1865603724d3SBjoern A. Zeeb V_rsvp_on--; 18661c5de19aSGarrett Wollman } 1867f0068c4aSGarrett Wollman return 0; 1868f0068c4aSGarrett Wollman } 1869bbb4330bSLuigi Rizzo 18708f5a8818SKevin Lo int 18718f5a8818SKevin Lo rsvp_input(struct mbuf **mp, int *offp, int proto) 1872bbb4330bSLuigi Rizzo { 18738f5a8818SKevin Lo struct mbuf *m; 18748f5a8818SKevin Lo 18758f5a8818SKevin Lo m = *mp; 18768f5a8818SKevin Lo *mp = NULL; 18778b615593SMarko Zec 1878bbb4330bSLuigi Rizzo if (rsvp_input_p) { /* call the real one if loaded */ 18798f5a8818SKevin Lo *mp = m; 18808f5a8818SKevin Lo rsvp_input_p(mp, offp, proto); 18818f5a8818SKevin Lo return (IPPROTO_DONE); 1882bbb4330bSLuigi Rizzo } 1883bbb4330bSLuigi Rizzo 1884bbb4330bSLuigi Rizzo /* Can still get packets with rsvp_on = 0 if there is a local member 1885bbb4330bSLuigi Rizzo * of the group to which the RSVP packet is addressed. But in this 1886bbb4330bSLuigi Rizzo * case we want to throw the packet away. 1887bbb4330bSLuigi Rizzo */ 1888bbb4330bSLuigi Rizzo 1889603724d3SBjoern A. Zeeb if (!V_rsvp_on) { 1890bbb4330bSLuigi Rizzo m_freem(m); 18918f5a8818SKevin Lo return (IPPROTO_DONE); 1892bbb4330bSLuigi Rizzo } 1893bbb4330bSLuigi Rizzo 1894603724d3SBjoern A. Zeeb if (V_ip_rsvpd != NULL) { 18958f5a8818SKevin Lo *mp = m; 18968f5a8818SKevin Lo rip_input(mp, offp, proto); 18978f5a8818SKevin Lo return (IPPROTO_DONE); 1898bbb4330bSLuigi Rizzo } 1899bbb4330bSLuigi Rizzo /* Drop the packet */ 1900bbb4330bSLuigi Rizzo m_freem(m); 19018f5a8818SKevin Lo return (IPPROTO_DONE); 1902bbb4330bSLuigi Rizzo } 1903