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" 4074a9466cSGary Palmer 41df8bae1dSRodney W. Grimes #include <sys/param.h> 42df8bae1dSRodney W. Grimes #include <sys/systm.h> 435f311da2SMike Silbersack #include <sys/callout.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> 531025071fSGarrett Wollman #include <sys/syslog.h> 54b5e8ce9fSBruce Evans #include <sys/sysctl.h> 55df8bae1dSRodney W. Grimes 56c85540ddSAndrey A. Chernov #include <net/pfil.h> 57df8bae1dSRodney W. Grimes #include <net/if.h> 589494d596SBrooks Davis #include <net/if_types.h> 59d314ad7bSJulian Elischer #include <net/if_var.h> 6082c23ebaSBill Fenner #include <net/if_dl.h> 61df8bae1dSRodney W. Grimes #include <net/route.h> 62748e0b0aSGarrett Wollman #include <net/netisr.h> 634b79449eSBjoern A. Zeeb #include <net/vnet.h> 6465111ec7SKip Macy #include <net/flowtable.h> 65df8bae1dSRodney W. Grimes 66df8bae1dSRodney W. Grimes #include <netinet/in.h> 67df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 68b5e8ce9fSBruce Evans #include <netinet/in_var.h> 69df8bae1dSRodney W. Grimes #include <netinet/ip.h> 70df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 71df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 72eddfbb76SRobert Watson #include <netinet/ip_fw.h> 73df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h> 74ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 7558938916SGarrett Wollman #include <machine/in_cksum.h> 76a9771948SGleb Smirnoff #include <netinet/ip_carp.h> 77b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 781dfcf0d2SAndre Oppermann #include <netinet/ip_ipsec.h> 79b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 80df8bae1dSRodney W. Grimes 81f0068c4aSGarrett Wollman #include <sys/socketvar.h> 826ddbf1e2SGary Palmer 83aed55708SRobert Watson #include <security/mac/mac_framework.h> 84aed55708SRobert Watson 85d2035ffbSEd Maste #ifdef CTASSERT 86d2035ffbSEd Maste CTASSERT(sizeof(struct ip) == 20); 87d2035ffbSEd Maste #endif 88d2035ffbSEd Maste 892d9cfabaSRobert Watson struct rwlock in_ifaddr_lock; 9064aeca7bSRobert Watson RW_SYSINIT(in_ifaddr_lock, &in_ifaddr_lock, "in_ifaddr_lock"); 91f0068c4aSGarrett Wollman 9282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, rsvp_on); 9382cea7e6SBjoern A. Zeeb 9482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ipforwarding); 95eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW, 96eddfbb76SRobert Watson &VNET_NAME(ipforwarding), 0, 978b615593SMarko Zec "Enable IP forwarding between interfaces"); 980312fbe9SPoul-Henning Kamp 9982cea7e6SBjoern A. Zeeb static VNET_DEFINE(int, ipsendredirects) = 1; /* XXX */ 10082cea7e6SBjoern A. Zeeb #define V_ipsendredirects VNET(ipsendredirects) 101eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW, 102eddfbb76SRobert Watson &VNET_NAME(ipsendredirects), 0, 1038b615593SMarko Zec "Enable sending IP redirects"); 1040312fbe9SPoul-Henning Kamp 10582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip_defttl) = IPDEFTTL; 106eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW, 107eddfbb76SRobert Watson &VNET_NAME(ip_defttl), 0, 108eddfbb76SRobert Watson "Maximum TTL on IP packets"); 1090312fbe9SPoul-Henning Kamp 11082cea7e6SBjoern A. Zeeb static VNET_DEFINE(int, ip_keepfaith); 11182cea7e6SBjoern A. Zeeb #define V_ip_keepfaith VNET(ip_keepfaith) 112eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW, 113eddfbb76SRobert Watson &VNET_NAME(ip_keepfaith), 0, 1146a800098SYoshinobu Inoue "Enable packet capture for FAITH IPv4->IPv6 translater daemon"); 1156a800098SYoshinobu Inoue 11682cea7e6SBjoern A. Zeeb static VNET_DEFINE(int, ip_sendsourcequench); 11782cea7e6SBjoern A. Zeeb #define V_ip_sendsourcequench VNET(ip_sendsourcequench) 118eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW, 119eddfbb76SRobert Watson &VNET_NAME(ip_sendsourcequench), 0, 120df285b3dSMike Silbersack "Enable the transmission of source quench packets"); 121df285b3dSMike Silbersack 12282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip_do_randomid); 123eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW, 124eddfbb76SRobert Watson &VNET_NAME(ip_do_randomid), 0, 125eddfbb76SRobert Watson "Assign random ip_id values"); 1261f44b0a1SDavid Malone 127823db0e9SDon Lewis /* 128823db0e9SDon Lewis * XXX - Setting ip_checkinterface mostly implements the receive side of 129823db0e9SDon Lewis * the Strong ES model described in RFC 1122, but since the routing table 130a8f12100SDon Lewis * and transmit implementation do not implement the Strong ES model, 131823db0e9SDon Lewis * setting this to 1 results in an odd hybrid. 1323f67c834SDon Lewis * 133a8f12100SDon Lewis * XXX - ip_checkinterface currently must be disabled if you use ipnat 134a8f12100SDon Lewis * to translate the destination address to another local interface. 1353f67c834SDon Lewis * 1363f67c834SDon Lewis * XXX - ip_checkinterface must be disabled if you add IP aliases 1373f67c834SDon Lewis * to the loopback interface instead of the interface where the 1383f67c834SDon Lewis * packets for those addresses are received. 139823db0e9SDon Lewis */ 14082cea7e6SBjoern A. Zeeb static VNET_DEFINE(int, ip_checkinterface); 14182cea7e6SBjoern A. Zeeb #define V_ip_checkinterface VNET(ip_checkinterface) 142eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW, 143eddfbb76SRobert Watson &VNET_NAME(ip_checkinterface), 0, 1448b615593SMarko Zec "Verify packet arrives on correct interface"); 145b3e95d4eSJonathan Lemon 1460b4b0b0fSJulian Elischer VNET_DEFINE(struct pfil_head, inet_pfil_hook); /* Packet filter hooks */ 147df8bae1dSRodney W. Grimes 148d4b5cae4SRobert Watson static struct netisr_handler ip_nh = { 149d4b5cae4SRobert Watson .nh_name = "ip", 150d4b5cae4SRobert Watson .nh_handler = ip_input, 151d4b5cae4SRobert Watson .nh_proto = NETISR_IP, 152d4b5cae4SRobert Watson .nh_policy = NETISR_POLICY_FLOW, 153d4b5cae4SRobert Watson }; 154ca925d9cSJonathan Lemon 155df8bae1dSRodney W. Grimes extern struct domain inetdomain; 156f0ffb944SJulian Elischer extern struct protosw inetsw[]; 157df8bae1dSRodney W. Grimes u_char ip_protox[IPPROTO_MAX]; 15882cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead); /* first inet address */ 15982cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); /* inet addr hash table */ 16082cea7e6SBjoern A. Zeeb VNET_DEFINE(u_long, in_ifaddrhmask); /* mask for hash table */ 161ca925d9cSJonathan Lemon 16282cea7e6SBjoern A. Zeeb VNET_DEFINE(struct ipstat, ipstat); 163eddfbb76SRobert Watson SYSCTL_VNET_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW, 164eddfbb76SRobert Watson &VNET_NAME(ipstat), ipstat, 165eddfbb76SRobert Watson "IP statistics (struct ipstat, netinet/ip_var.h)"); 166df8bae1dSRodney W. Grimes 167eddfbb76SRobert Watson static VNET_DEFINE(uma_zone_t, ipq_zone); 16882cea7e6SBjoern A. Zeeb static VNET_DEFINE(TAILQ_HEAD(ipqhead, ipq), ipq[IPREASS_NHASH]); 169dfa60d93SRobert Watson static struct mtx ipqlock; 1702fad1e93SSam Leffler 17182cea7e6SBjoern A. Zeeb #define V_ipq_zone VNET(ipq_zone) 17282cea7e6SBjoern A. Zeeb #define V_ipq VNET(ipq) 17382cea7e6SBjoern A. Zeeb 1742fad1e93SSam Leffler #define IPQ_LOCK() mtx_lock(&ipqlock) 1752fad1e93SSam Leffler #define IPQ_UNLOCK() mtx_unlock(&ipqlock) 176888c2a3cSSam Leffler #define IPQ_LOCK_INIT() mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF) 177888c2a3cSSam Leffler #define IPQ_LOCK_ASSERT() mtx_assert(&ipqlock, MA_OWNED) 178f23b4c91SGarrett Wollman 179d248c7d7SRobert Watson static void maxnipq_update(void); 1804f590175SPaul Saab static void ipq_zone_change(void *); 1819802380eSBjoern A. Zeeb static void ip_drain_locked(void); 182d248c7d7SRobert Watson 18382cea7e6SBjoern A. Zeeb static VNET_DEFINE(int, maxnipq); /* Administrative limit on # reass queues. */ 18482cea7e6SBjoern A. Zeeb static VNET_DEFINE(int, nipq); /* Total # of reass queues */ 18582cea7e6SBjoern A. Zeeb #define V_maxnipq VNET(maxnipq) 18682cea7e6SBjoern A. Zeeb #define V_nipq VNET(nipq) 187eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_RD, 188eddfbb76SRobert Watson &VNET_NAME(nipq), 0, 1898b615593SMarko Zec "Current number of IPv4 fragment reassembly queue entries"); 190d248c7d7SRobert Watson 19182cea7e6SBjoern A. Zeeb static VNET_DEFINE(int, maxfragsperpacket); 19282cea7e6SBjoern A. Zeeb #define V_maxfragsperpacket VNET(maxfragsperpacket) 193eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW, 194eddfbb76SRobert Watson &VNET_NAME(maxfragsperpacket), 0, 195d248c7d7SRobert Watson "Maximum number of IPv4 fragments allowed per packet"); 196d248c7d7SRobert Watson 197d248c7d7SRobert Watson struct callout ipport_tick_callout; 198d248c7d7SRobert Watson 1990312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU 2000312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, 2013d177f46SBill Fumerola &ip_mtu, 0, "Default MTU"); 2020312fbe9SPoul-Henning Kamp #endif 2030312fbe9SPoul-Henning Kamp 2041b968362SDag-Erling Smørgrav #ifdef IPSTEALTH 20582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ipstealth); 206eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW, 207eddfbb76SRobert Watson &VNET_NAME(ipstealth), 0, 208eddfbb76SRobert Watson "IP stealth mode, no TTL decrementation on forwarding"); 2091b968362SDag-Erling Smørgrav #endif 210eddfbb76SRobert Watson 21153be8fcaSBjoern A. Zeeb #ifdef FLOWTABLE 212eddfbb76SRobert Watson static VNET_DEFINE(int, ip_output_flowtable_size) = 2048; 213eddfbb76SRobert Watson VNET_DEFINE(struct flowtable *, ip_ft); 2141e77c105SRobert Watson #define V_ip_output_flowtable_size VNET(ip_output_flowtable_size) 215eddfbb76SRobert Watson 216eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, output_flowtable_size, CTLFLAG_RDTUN, 217eddfbb76SRobert Watson &VNET_NAME(ip_output_flowtable_size), 2048, 21865111ec7SKip Macy "number of entries in the per-cpu output flow caches"); 21953be8fcaSBjoern A. Zeeb #endif 22053be8fcaSBjoern A. Zeeb 221eddfbb76SRobert Watson VNET_DEFINE(int, fw_one_pass) = 1; 222010b65f5SJulian Elischer 2234d77a549SAlfred Perlstein static void ip_freef(struct ipqhead *, struct ipq *); 2248948e4baSArchie Cobbs 225315e3e38SRobert Watson /* 226315e3e38SRobert Watson * Kernel module interface for updating ipstat. The argument is an index 227315e3e38SRobert Watson * into ipstat treated as an array of u_long. While this encodes the general 228315e3e38SRobert Watson * layout of ipstat into the caller, it doesn't encode its location, so that 229315e3e38SRobert Watson * future changes to add, for example, per-CPU stats support won't cause 230315e3e38SRobert Watson * binary compatibility problems for kernel modules. 231315e3e38SRobert Watson */ 232315e3e38SRobert Watson void 233315e3e38SRobert Watson kmod_ipstat_inc(int statnum) 234315e3e38SRobert Watson { 235315e3e38SRobert Watson 236315e3e38SRobert Watson (*((u_long *)&V_ipstat + statnum))++; 237315e3e38SRobert Watson } 238315e3e38SRobert Watson 239315e3e38SRobert Watson void 240315e3e38SRobert Watson kmod_ipstat_dec(int statnum) 241315e3e38SRobert Watson { 242315e3e38SRobert Watson 243315e3e38SRobert Watson (*((u_long *)&V_ipstat + statnum))--; 244315e3e38SRobert Watson } 245315e3e38SRobert Watson 246d4b5cae4SRobert Watson static int 247d4b5cae4SRobert Watson sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS) 248d4b5cae4SRobert Watson { 249d4b5cae4SRobert Watson int error, qlimit; 250d4b5cae4SRobert Watson 251d4b5cae4SRobert Watson netisr_getqlimit(&ip_nh, &qlimit); 252d4b5cae4SRobert Watson error = sysctl_handle_int(oidp, &qlimit, 0, req); 253d4b5cae4SRobert Watson if (error || !req->newptr) 254d4b5cae4SRobert Watson return (error); 255d4b5cae4SRobert Watson if (qlimit < 1) 256d4b5cae4SRobert Watson return (EINVAL); 257d4b5cae4SRobert Watson return (netisr_setqlimit(&ip_nh, qlimit)); 258d4b5cae4SRobert Watson } 259d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, 260d4b5cae4SRobert Watson CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_queue_maxlen, "I", 261d4b5cae4SRobert Watson "Maximum size of the IP input queue"); 262d4b5cae4SRobert Watson 263d4b5cae4SRobert Watson static int 264d4b5cae4SRobert Watson sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS) 265d4b5cae4SRobert Watson { 266d4b5cae4SRobert Watson u_int64_t qdrops_long; 267d4b5cae4SRobert Watson int error, qdrops; 268d4b5cae4SRobert Watson 269d4b5cae4SRobert Watson netisr_getqdrops(&ip_nh, &qdrops_long); 270d4b5cae4SRobert Watson qdrops = qdrops_long; 271d4b5cae4SRobert Watson error = sysctl_handle_int(oidp, &qdrops, 0, req); 272d4b5cae4SRobert Watson if (error || !req->newptr) 273d4b5cae4SRobert Watson return (error); 274d4b5cae4SRobert Watson if (qdrops != 0) 275d4b5cae4SRobert Watson return (EINVAL); 276d4b5cae4SRobert Watson netisr_clearqdrops(&ip_nh); 277d4b5cae4SRobert Watson return (0); 278d4b5cae4SRobert Watson } 279d4b5cae4SRobert Watson 280d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, 281d4b5cae4SRobert Watson CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_queue_drops, "I", 282d4b5cae4SRobert Watson "Number of packets dropped from the IP input queue"); 283d4b5cae4SRobert Watson 284df8bae1dSRodney W. Grimes /* 285df8bae1dSRodney W. Grimes * IP initialization: fill in IP protocol switch table. 286df8bae1dSRodney W. Grimes * All protocols not implemented in kernel go to raw IP protocol handler. 287df8bae1dSRodney W. Grimes */ 288df8bae1dSRodney W. Grimes void 289f2565d68SRobert Watson ip_init(void) 290df8bae1dSRodney W. Grimes { 291f2565d68SRobert Watson struct protosw *pr; 292f2565d68SRobert Watson int i; 293df8bae1dSRodney W. Grimes 294a511354aSRobert Watson V_ip_id = time_second & 0xffff; 295a511354aSRobert Watson 296603724d3SBjoern A. Zeeb TAILQ_INIT(&V_in_ifaddrhead); 297603724d3SBjoern A. Zeeb V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask); 2981ed81b73SMarko Zec 2991ed81b73SMarko Zec /* Initialize IP reassembly queue. */ 3001ed81b73SMarko Zec for (i = 0; i < IPREASS_NHASH; i++) 3011ed81b73SMarko Zec TAILQ_INIT(&V_ipq[i]); 3021ed81b73SMarko Zec V_maxnipq = nmbclusters / 32; 3031ed81b73SMarko Zec V_maxfragsperpacket = 16; 3041ed81b73SMarko Zec V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL, 3051ed81b73SMarko Zec NULL, UMA_ALIGN_PTR, 0); 3061ed81b73SMarko Zec maxnipq_update(); 3071ed81b73SMarko Zec 3080b4b0b0fSJulian Elischer /* Initialize packet filter hooks. */ 3090b4b0b0fSJulian Elischer V_inet_pfil_hook.ph_type = PFIL_TYPE_AF; 3100b4b0b0fSJulian Elischer V_inet_pfil_hook.ph_af = AF_INET; 3110b4b0b0fSJulian Elischer if ((i = pfil_head_register(&V_inet_pfil_hook)) != 0) 3120b4b0b0fSJulian Elischer printf("%s: WARNING: unable to register pfil hook, " 3130b4b0b0fSJulian Elischer "error %d\n", __func__, i); 3140b4b0b0fSJulian Elischer 315fa057b15SMarko Zec #ifdef FLOWTABLE 3163059584eSKip Macy if (TUNABLE_INT_FETCH("net.inet.ip.output_flowtable_size", 3173059584eSKip Macy &V_ip_output_flowtable_size)) { 3183059584eSKip Macy if (V_ip_output_flowtable_size < 256) 3193059584eSKip Macy V_ip_output_flowtable_size = 256; 3203059584eSKip Macy if (!powerof2(V_ip_output_flowtable_size)) { 3213059584eSKip Macy printf("flowtable must be power of 2 size\n"); 3223059584eSKip Macy V_ip_output_flowtable_size = 2048; 3233059584eSKip Macy } 3243059584eSKip Macy } else { 3253059584eSKip Macy /* 3263059584eSKip Macy * round up to the next power of 2 3273059584eSKip Macy */ 3283059584eSKip Macy V_ip_output_flowtable_size = 1 << fls((1024 + maxusers * 64)-1); 3293059584eSKip Macy } 330d4121a02SKip Macy V_ip_ft = flowtable_alloc("ipv4", V_ip_output_flowtable_size, FL_PCPU); 331fa057b15SMarko Zec #endif 332fa057b15SMarko Zec 3331ed81b73SMarko Zec /* Skip initialization of globals for non-default instances. */ 3341ed81b73SMarko Zec if (!IS_DEFAULT_VNET(curvnet)) 3351ed81b73SMarko Zec return; 3361ed81b73SMarko Zec 337f0ffb944SJulian Elischer pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 33802410549SRobert Watson if (pr == NULL) 339db09bef3SAndre Oppermann panic("ip_init: PF_INET not found"); 340db09bef3SAndre Oppermann 341db09bef3SAndre Oppermann /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */ 342df8bae1dSRodney W. Grimes for (i = 0; i < IPPROTO_MAX; i++) 343df8bae1dSRodney W. Grimes ip_protox[i] = pr - inetsw; 344db09bef3SAndre Oppermann /* 345db09bef3SAndre Oppermann * Cycle through IP protocols and put them into the appropriate place 346db09bef3SAndre Oppermann * in ip_protox[]. 347db09bef3SAndre Oppermann */ 348f0ffb944SJulian Elischer for (pr = inetdomain.dom_protosw; 349f0ffb944SJulian Elischer pr < inetdomain.dom_protoswNPROTOSW; pr++) 350df8bae1dSRodney W. Grimes if (pr->pr_domain->dom_family == PF_INET && 351db09bef3SAndre Oppermann pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) { 352db09bef3SAndre Oppermann /* Be careful to only index valid IP protocols. */ 353db77984cSSam Leffler if (pr->pr_protocol < IPPROTO_MAX) 354df8bae1dSRodney W. Grimes ip_protox[pr->pr_protocol] = pr - inetsw; 355db09bef3SAndre Oppermann } 356194a213eSAndrey A. Chernov 3575f311da2SMike Silbersack /* Start ipport_tick. */ 3585f311da2SMike Silbersack callout_init(&ipport_tick_callout, CALLOUT_MPSAFE); 35921ca7b57SMarko Zec callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL); 3605f311da2SMike Silbersack EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 3615f311da2SMike Silbersack SHUTDOWN_PRI_DEFAULT); 3624f590175SPaul Saab EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change, 3634f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 3645f311da2SMike Silbersack 365db09bef3SAndre Oppermann /* Initialize various other remaining things. */ 3661ed81b73SMarko Zec IPQ_LOCK_INIT(); 367d4b5cae4SRobert Watson netisr_register(&ip_nh); 368df8bae1dSRodney W. Grimes } 369df8bae1dSRodney W. Grimes 3709802380eSBjoern A. Zeeb #ifdef VIMAGE 3719802380eSBjoern A. Zeeb void 3729802380eSBjoern A. Zeeb ip_destroy(void) 3739802380eSBjoern A. Zeeb { 3749802380eSBjoern A. Zeeb 3759802380eSBjoern A. Zeeb /* Cleanup in_ifaddr hash table; should be empty. */ 3769802380eSBjoern A. Zeeb hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask); 3779802380eSBjoern A. Zeeb 3789802380eSBjoern A. Zeeb IPQ_LOCK(); 3799802380eSBjoern A. Zeeb ip_drain_locked(); 3809802380eSBjoern A. Zeeb IPQ_UNLOCK(); 3819802380eSBjoern A. Zeeb 3829802380eSBjoern A. Zeeb uma_zdestroy(V_ipq_zone); 3839802380eSBjoern A. Zeeb } 3849802380eSBjoern A. Zeeb #endif 3859802380eSBjoern A. Zeeb 386f2565d68SRobert Watson void 387f2565d68SRobert Watson ip_fini(void *xtp) 3885f311da2SMike Silbersack { 389f2565d68SRobert Watson 3905f311da2SMike Silbersack callout_stop(&ipport_tick_callout); 3915f311da2SMike Silbersack } 3925f311da2SMike Silbersack 3934d2e3692SLuigi Rizzo /* 394df8bae1dSRodney W. Grimes * Ip input routine. Checksum and byte swap header. If fragmented 395df8bae1dSRodney W. Grimes * try to reassemble. Process options. Pass to next level. 396df8bae1dSRodney W. Grimes */ 397c67b1d17SGarrett Wollman void 398c67b1d17SGarrett Wollman ip_input(struct mbuf *m) 399df8bae1dSRodney W. Grimes { 4009188b4a1SAndre Oppermann struct ip *ip = NULL; 4015da9f8faSJosef Karthauser struct in_ifaddr *ia = NULL; 402ca925d9cSJonathan Lemon struct ifaddr *ifa; 4030aade26eSRobert Watson struct ifnet *ifp; 4049b932e9eSAndre Oppermann int checkif, hlen = 0; 40547c861ecSBrian Somers u_short sum; 40602c1c707SAndre Oppermann int dchg = 0; /* dest changed after fw */ 407f51f805fSSam Leffler struct in_addr odst; /* original dst address */ 408b715f178SLuigi Rizzo 409fe584538SDag-Erling Smørgrav M_ASSERTPKTHDR(m); 410db40007dSAndrew R. Reiter 411ac9d7e26SMax Laier if (m->m_flags & M_FASTFWD_OURS) { 4129b932e9eSAndre Oppermann /* 41376ff6dcfSAndre Oppermann * Firewall or NAT changed destination to local. 41476ff6dcfSAndre Oppermann * We expect ip_len and ip_off to be in host byte order. 4159b932e9eSAndre Oppermann */ 41676ff6dcfSAndre Oppermann m->m_flags &= ~M_FASTFWD_OURS; 41776ff6dcfSAndre Oppermann /* Set up some basics that will be used later. */ 4182b25acc1SLuigi Rizzo ip = mtod(m, struct ip *); 41953be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 4209b932e9eSAndre Oppermann goto ours; 4212b25acc1SLuigi Rizzo } 4222b25acc1SLuigi Rizzo 42386425c62SRobert Watson IPSTAT_INC(ips_total); 42458938916SGarrett Wollman 42558938916SGarrett Wollman if (m->m_pkthdr.len < sizeof(struct ip)) 42658938916SGarrett Wollman goto tooshort; 42758938916SGarrett Wollman 428df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct ip) && 4290b17fba7SAndre Oppermann (m = m_pullup(m, sizeof (struct ip))) == NULL) { 43086425c62SRobert Watson IPSTAT_INC(ips_toosmall); 431c67b1d17SGarrett Wollman return; 432df8bae1dSRodney W. Grimes } 433df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 43458938916SGarrett Wollman 43553be11f6SPoul-Henning Kamp if (ip->ip_v != IPVERSION) { 43686425c62SRobert Watson IPSTAT_INC(ips_badvers); 437df8bae1dSRodney W. Grimes goto bad; 438df8bae1dSRodney W. Grimes } 43958938916SGarrett Wollman 44053be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 441df8bae1dSRodney W. Grimes if (hlen < sizeof(struct ip)) { /* minimum header length */ 44286425c62SRobert Watson IPSTAT_INC(ips_badhlen); 443df8bae1dSRodney W. Grimes goto bad; 444df8bae1dSRodney W. Grimes } 445df8bae1dSRodney W. Grimes if (hlen > m->m_len) { 4460b17fba7SAndre Oppermann if ((m = m_pullup(m, hlen)) == NULL) { 44786425c62SRobert Watson IPSTAT_INC(ips_badhlen); 448c67b1d17SGarrett Wollman return; 449df8bae1dSRodney W. Grimes } 450df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 451df8bae1dSRodney W. Grimes } 45233841545SHajimu UMEMOTO 45333841545SHajimu UMEMOTO /* 127/8 must not appear on wire - RFC1122 */ 4540aade26eSRobert Watson ifp = m->m_pkthdr.rcvif; 45533841545SHajimu UMEMOTO if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 45633841545SHajimu UMEMOTO (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 4570aade26eSRobert Watson if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 45886425c62SRobert Watson IPSTAT_INC(ips_badaddr); 45933841545SHajimu UMEMOTO goto bad; 46033841545SHajimu UMEMOTO } 46133841545SHajimu UMEMOTO } 46233841545SHajimu UMEMOTO 463db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 464db4f9cc7SJonathan Lemon sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 465db4f9cc7SJonathan Lemon } else { 46658938916SGarrett Wollman if (hlen == sizeof(struct ip)) { 46747c861ecSBrian Somers sum = in_cksum_hdr(ip); 46858938916SGarrett Wollman } else { 46947c861ecSBrian Somers sum = in_cksum(m, hlen); 47058938916SGarrett Wollman } 471db4f9cc7SJonathan Lemon } 47247c861ecSBrian Somers if (sum) { 47386425c62SRobert Watson IPSTAT_INC(ips_badsum); 474df8bae1dSRodney W. Grimes goto bad; 475df8bae1dSRodney W. Grimes } 476df8bae1dSRodney W. Grimes 47702b199f1SMax Laier #ifdef ALTQ 47802b199f1SMax Laier if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 47902b199f1SMax Laier /* packet is dropped by traffic conditioner */ 48002b199f1SMax Laier return; 48102b199f1SMax Laier #endif 48202b199f1SMax Laier 483df8bae1dSRodney W. Grimes /* 484df8bae1dSRodney W. Grimes * Convert fields to host representation. 485df8bae1dSRodney W. Grimes */ 486fd8e4ebcSMike Barcroft ip->ip_len = ntohs(ip->ip_len); 487df8bae1dSRodney W. Grimes if (ip->ip_len < hlen) { 48886425c62SRobert Watson IPSTAT_INC(ips_badlen); 489df8bae1dSRodney W. Grimes goto bad; 490df8bae1dSRodney W. Grimes } 491fd8e4ebcSMike Barcroft ip->ip_off = ntohs(ip->ip_off); 492df8bae1dSRodney W. Grimes 493df8bae1dSRodney W. Grimes /* 494df8bae1dSRodney W. Grimes * Check that the amount of data in the buffers 495df8bae1dSRodney W. Grimes * is as at least much as the IP header would have us expect. 496df8bae1dSRodney W. Grimes * Trim mbufs if longer than we expect. 497df8bae1dSRodney W. Grimes * Drop packet if shorter than we expect. 498df8bae1dSRodney W. Grimes */ 499df8bae1dSRodney W. Grimes if (m->m_pkthdr.len < ip->ip_len) { 50058938916SGarrett Wollman tooshort: 50186425c62SRobert Watson IPSTAT_INC(ips_tooshort); 502df8bae1dSRodney W. Grimes goto bad; 503df8bae1dSRodney W. Grimes } 504df8bae1dSRodney W. Grimes if (m->m_pkthdr.len > ip->ip_len) { 505df8bae1dSRodney W. Grimes if (m->m_len == m->m_pkthdr.len) { 506df8bae1dSRodney W. Grimes m->m_len = ip->ip_len; 507df8bae1dSRodney W. Grimes m->m_pkthdr.len = ip->ip_len; 508df8bae1dSRodney W. Grimes } else 509df8bae1dSRodney W. Grimes m_adj(m, ip->ip_len - m->m_pkthdr.len); 510df8bae1dSRodney W. Grimes } 511b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 51214dd6717SSam Leffler /* 51314dd6717SSam Leffler * Bypass packet filtering for packets from a tunnel (gif). 51414dd6717SSam Leffler */ 515cc977adcSBjoern A. Zeeb if (ip_ipsec_filtertunnel(m)) 516c21fd232SAndre Oppermann goto passin; 517b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 5183f67c834SDon Lewis 519c4ac87eaSDarren Reed /* 520134ea224SSam Leffler * Run through list of hooks for input packets. 521f51f805fSSam Leffler * 522f51f805fSSam Leffler * NB: Beware of the destination address changing (e.g. 523f51f805fSSam Leffler * by NAT rewriting). When this happens, tell 524f51f805fSSam Leffler * ip_forward to do the right thing. 525c4ac87eaSDarren Reed */ 526c21fd232SAndre Oppermann 527c21fd232SAndre Oppermann /* Jump over all PFIL processing if hooks are not active. */ 5280b4b0b0fSJulian Elischer if (!PFIL_HOOKED(&V_inet_pfil_hook)) 529c21fd232SAndre Oppermann goto passin; 530c21fd232SAndre Oppermann 531f51f805fSSam Leffler odst = ip->ip_dst; 5320b4b0b0fSJulian Elischer if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0) 533beec8214SDarren Reed return; 534134ea224SSam Leffler if (m == NULL) /* consumed by filter */ 535c4ac87eaSDarren Reed return; 5369b932e9eSAndre Oppermann 537c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 53802c1c707SAndre Oppermann dchg = (odst.s_addr != ip->ip_dst.s_addr); 5390aade26eSRobert Watson ifp = m->m_pkthdr.rcvif; 5409b932e9eSAndre Oppermann 5419b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 5429b932e9eSAndre Oppermann if (m->m_flags & M_FASTFWD_OURS) { 5439b932e9eSAndre Oppermann m->m_flags &= ~M_FASTFWD_OURS; 5449b932e9eSAndre Oppermann goto ours; 5459b932e9eSAndre Oppermann } 546099dd043SAndre Oppermann if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) { 547099dd043SAndre Oppermann /* 5480d3d0d74SRobert Watson * Directly ship the packet on. This allows forwarding 54999b96cf9SRobert Watson * packets originally destined to us to some other directly 5506426657eSRobert Watson * connected host. 551099dd043SAndre Oppermann */ 552099dd043SAndre Oppermann ip_forward(m, dchg); 553099dd043SAndre Oppermann return; 554099dd043SAndre Oppermann } 5559b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 5569b932e9eSAndre Oppermann 557c21fd232SAndre Oppermann passin: 558df8bae1dSRodney W. Grimes /* 559df8bae1dSRodney W. Grimes * Process options and, if not destined for us, 560df8bae1dSRodney W. Grimes * ship it on. ip_dooptions returns 1 when an 561df8bae1dSRodney W. Grimes * error was detected (causing an icmp message 562df8bae1dSRodney W. Grimes * to be sent and the original packet to be freed). 563df8bae1dSRodney W. Grimes */ 5649b932e9eSAndre Oppermann if (hlen > sizeof (struct ip) && ip_dooptions(m, 0)) 565c67b1d17SGarrett Wollman return; 566df8bae1dSRodney W. Grimes 567f0068c4aSGarrett Wollman /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 568f0068c4aSGarrett Wollman * matter if it is destined to another node, or whether it is 569f0068c4aSGarrett Wollman * a multicast one, RSVP wants it! and prevents it from being forwarded 570f0068c4aSGarrett Wollman * anywhere else. Also checks if the rsvp daemon is running before 571f0068c4aSGarrett Wollman * grabbing the packet. 572f0068c4aSGarrett Wollman */ 573603724d3SBjoern A. Zeeb if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP) 574f0068c4aSGarrett Wollman goto ours; 575f0068c4aSGarrett Wollman 576df8bae1dSRodney W. Grimes /* 577df8bae1dSRodney W. Grimes * Check our list of addresses, to see if the packet is for us. 578cc766e04SGarrett Wollman * If we don't have any addresses, assume any unicast packet 579cc766e04SGarrett Wollman * we receive might be for us (and let the upper layers deal 580cc766e04SGarrett Wollman * with it). 581df8bae1dSRodney W. Grimes */ 582603724d3SBjoern A. Zeeb if (TAILQ_EMPTY(&V_in_ifaddrhead) && 583cc766e04SGarrett Wollman (m->m_flags & (M_MCAST|M_BCAST)) == 0) 584cc766e04SGarrett Wollman goto ours; 585cc766e04SGarrett Wollman 5867538a9a0SJonathan Lemon /* 587823db0e9SDon Lewis * Enable a consistency check between the destination address 588823db0e9SDon Lewis * and the arrival interface for a unicast packet (the RFC 1122 589823db0e9SDon Lewis * strong ES model) if IP forwarding is disabled and the packet 590e15ae1b2SDon Lewis * is not locally generated and the packet is not subject to 591e15ae1b2SDon Lewis * 'ipfw fwd'. 5923f67c834SDon Lewis * 5933f67c834SDon Lewis * XXX - Checking also should be disabled if the destination 5943f67c834SDon Lewis * address is ipnat'ed to a different interface. 5953f67c834SDon Lewis * 596a8f12100SDon Lewis * XXX - Checking is incompatible with IP aliases added 5973f67c834SDon Lewis * to the loopback interface instead of the interface where 5983f67c834SDon Lewis * the packets are received. 599a9771948SGleb Smirnoff * 600a9771948SGleb Smirnoff * XXX - This is the case for carp vhost IPs as well so we 601a9771948SGleb Smirnoff * insert a workaround. If the packet got here, we already 602a9771948SGleb Smirnoff * checked with carp_iamatch() and carp_forus(). 603823db0e9SDon Lewis */ 604603724d3SBjoern A. Zeeb checkif = V_ip_checkinterface && (V_ipforwarding == 0) && 6050aade26eSRobert Watson ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) && 606*54bfbd51SWill Andrews ifp->if_carp == NULL && (dchg == 0); 607823db0e9SDon Lewis 608ca925d9cSJonathan Lemon /* 609ca925d9cSJonathan Lemon * Check for exact addresses in the hash bucket. 610ca925d9cSJonathan Lemon */ 6112d9cfabaSRobert Watson /* IN_IFADDR_RLOCK(); */ 6129b932e9eSAndre Oppermann LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 613f9e354dfSJulian Elischer /* 614823db0e9SDon Lewis * If the address matches, verify that the packet 615823db0e9SDon Lewis * arrived via the correct interface if checking is 616823db0e9SDon Lewis * enabled. 617f9e354dfSJulian Elischer */ 6189b932e9eSAndre Oppermann if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr && 6198c0fec80SRobert Watson (!checkif || ia->ia_ifp == ifp)) { 6208c0fec80SRobert Watson ifa_ref(&ia->ia_ifa); 6212d9cfabaSRobert Watson /* IN_IFADDR_RUNLOCK(); */ 622ed1ff184SJulian Elischer goto ours; 623ca925d9cSJonathan Lemon } 6248c0fec80SRobert Watson } 6252d9cfabaSRobert Watson /* IN_IFADDR_RUNLOCK(); */ 6262d9cfabaSRobert Watson 627823db0e9SDon Lewis /* 628ca925d9cSJonathan Lemon * Check for broadcast addresses. 629ca925d9cSJonathan Lemon * 630ca925d9cSJonathan Lemon * Only accept broadcast packets that arrive via the matching 631ca925d9cSJonathan Lemon * interface. Reception of forwarded directed broadcasts would 632ca925d9cSJonathan Lemon * be handled via ip_forward() and ether_output() with the loopback 633ca925d9cSJonathan Lemon * into the stack for SIMPLEX interfaces handled by ether_output(). 634823db0e9SDon Lewis */ 6350aade26eSRobert Watson if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) { 6360aade26eSRobert Watson IF_ADDR_LOCK(ifp); 6370aade26eSRobert Watson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 638ca925d9cSJonathan Lemon if (ifa->ifa_addr->sa_family != AF_INET) 639ca925d9cSJonathan Lemon continue; 640ca925d9cSJonathan Lemon ia = ifatoia(ifa); 641df8bae1dSRodney W. Grimes if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 6420aade26eSRobert Watson ip->ip_dst.s_addr) { 6438c0fec80SRobert Watson ifa_ref(ifa); 6440aade26eSRobert Watson IF_ADDR_UNLOCK(ifp); 645df8bae1dSRodney W. Grimes goto ours; 6460aade26eSRobert Watson } 6470aade26eSRobert Watson if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr) { 6488c0fec80SRobert Watson ifa_ref(ifa); 6490aade26eSRobert Watson IF_ADDR_UNLOCK(ifp); 650df8bae1dSRodney W. Grimes goto ours; 6510aade26eSRobert Watson } 6520ac40133SBrian Somers #ifdef BOOTP_COMPAT 6530aade26eSRobert Watson if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) { 6548c0fec80SRobert Watson ifa_ref(ifa); 6550aade26eSRobert Watson IF_ADDR_UNLOCK(ifp); 656ca925d9cSJonathan Lemon goto ours; 6570aade26eSRobert Watson } 6580ac40133SBrian Somers #endif 659df8bae1dSRodney W. Grimes } 6600aade26eSRobert Watson IF_ADDR_UNLOCK(ifp); 66119e5b0a7SRobert Watson ia = NULL; 662df8bae1dSRodney W. Grimes } 663f8429ca2SBruce M Simpson /* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */ 664f8429ca2SBruce M Simpson if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) { 66586425c62SRobert Watson IPSTAT_INC(ips_cantforward); 666f8429ca2SBruce M Simpson m_freem(m); 667f8429ca2SBruce M Simpson return; 668f8429ca2SBruce M Simpson } 669df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 670603724d3SBjoern A. Zeeb if (V_ip_mrouter) { 671df8bae1dSRodney W. Grimes /* 672df8bae1dSRodney W. Grimes * If we are acting as a multicast router, all 673df8bae1dSRodney W. Grimes * incoming multicast packets are passed to the 674df8bae1dSRodney W. Grimes * kernel-level multicast forwarding function. 675df8bae1dSRodney W. Grimes * The packet is returned (relatively) intact; if 676df8bae1dSRodney W. Grimes * ip_mforward() returns a non-zero value, the packet 677df8bae1dSRodney W. Grimes * must be discarded, else it may be accepted below. 678df8bae1dSRodney W. Grimes */ 6790aade26eSRobert Watson if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) { 68086425c62SRobert Watson IPSTAT_INC(ips_cantforward); 681df8bae1dSRodney W. Grimes m_freem(m); 682c67b1d17SGarrett Wollman return; 683df8bae1dSRodney W. Grimes } 684df8bae1dSRodney W. Grimes 685df8bae1dSRodney W. Grimes /* 68611612afaSDima Dorfman * The process-level routing daemon needs to receive 687df8bae1dSRodney W. Grimes * all multicast IGMP packets, whether or not this 688df8bae1dSRodney W. Grimes * host belongs to their destination groups. 689df8bae1dSRodney W. Grimes */ 690df8bae1dSRodney W. Grimes if (ip->ip_p == IPPROTO_IGMP) 691df8bae1dSRodney W. Grimes goto ours; 69286425c62SRobert Watson IPSTAT_INC(ips_forward); 693df8bae1dSRodney W. Grimes } 694df8bae1dSRodney W. Grimes /* 695d10910e6SBruce M Simpson * Assume the packet is for us, to avoid prematurely taking 696d10910e6SBruce M Simpson * a lock on the in_multi hash. Protocols must perform 697d10910e6SBruce M Simpson * their own filtering and update statistics accordingly. 698df8bae1dSRodney W. Grimes */ 699df8bae1dSRodney W. Grimes goto ours; 700df8bae1dSRodney W. Grimes } 701df8bae1dSRodney W. Grimes if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 702df8bae1dSRodney W. Grimes goto ours; 703df8bae1dSRodney W. Grimes if (ip->ip_dst.s_addr == INADDR_ANY) 704df8bae1dSRodney W. Grimes goto ours; 705df8bae1dSRodney W. Grimes 7066a800098SYoshinobu Inoue /* 7076a800098SYoshinobu Inoue * FAITH(Firewall Aided Internet Translator) 7086a800098SYoshinobu Inoue */ 7090aade26eSRobert Watson if (ifp && ifp->if_type == IFT_FAITH) { 710603724d3SBjoern A. Zeeb if (V_ip_keepfaith) { 7116a800098SYoshinobu Inoue if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) 7126a800098SYoshinobu Inoue goto ours; 7136a800098SYoshinobu Inoue } 7146a800098SYoshinobu Inoue m_freem(m); 7156a800098SYoshinobu Inoue return; 7166a800098SYoshinobu Inoue } 7179494d596SBrooks Davis 718df8bae1dSRodney W. Grimes /* 719df8bae1dSRodney W. Grimes * Not for us; forward if possible and desirable. 720df8bae1dSRodney W. Grimes */ 721603724d3SBjoern A. Zeeb if (V_ipforwarding == 0) { 72286425c62SRobert Watson IPSTAT_INC(ips_cantforward); 723df8bae1dSRodney W. Grimes m_freem(m); 724546f251bSChris D. Faulhaber } else { 725b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 7261dfcf0d2SAndre Oppermann if (ip_ipsec_fwd(m)) 727546f251bSChris D. Faulhaber goto bad; 728b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 7299b932e9eSAndre Oppermann ip_forward(m, dchg); 730546f251bSChris D. Faulhaber } 731c67b1d17SGarrett Wollman return; 732df8bae1dSRodney W. Grimes 733df8bae1dSRodney W. Grimes ours: 734d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH 735d0ebc0d2SYaroslav Tykhiy /* 736d0ebc0d2SYaroslav Tykhiy * IPSTEALTH: Process non-routing options only 737d0ebc0d2SYaroslav Tykhiy * if the packet is destined for us. 738d0ebc0d2SYaroslav Tykhiy */ 73919e5b0a7SRobert Watson if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1)) { 74019e5b0a7SRobert Watson if (ia != NULL) 74119e5b0a7SRobert Watson ifa_free(&ia->ia_ifa); 742d0ebc0d2SYaroslav Tykhiy return; 74319e5b0a7SRobert Watson } 744d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */ 745d0ebc0d2SYaroslav Tykhiy 7465da9f8faSJosef Karthauser /* Count the packet in the ip address stats */ 7475da9f8faSJosef Karthauser if (ia != NULL) { 7485da9f8faSJosef Karthauser ia->ia_ifa.if_ipackets++; 7495da9f8faSJosef Karthauser ia->ia_ifa.if_ibytes += m->m_pkthdr.len; 7508c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 7515da9f8faSJosef Karthauser } 752100ba1a6SJordan K. Hubbard 75363f8d699SJordan K. Hubbard /* 754b6ea1aa5SRuslan Ermilov * Attempt reassembly; if it succeeds, proceed. 755ac9d7e26SMax Laier * ip_reass() will return a different mbuf. 756df8bae1dSRodney W. Grimes */ 757f0cada84SAndre Oppermann if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 758f0cada84SAndre Oppermann m = ip_reass(m); 759f0cada84SAndre Oppermann if (m == NULL) 760c67b1d17SGarrett Wollman return; 7616a800098SYoshinobu Inoue ip = mtod(m, struct ip *); 7627e2df452SRuslan Ermilov /* Get the header length of the reassembled packet */ 76353be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 764f0cada84SAndre Oppermann } 765f0cada84SAndre Oppermann 766f0cada84SAndre Oppermann /* 767f0cada84SAndre Oppermann * Further protocols expect the packet length to be w/o the 768f0cada84SAndre Oppermann * IP header. 769f0cada84SAndre Oppermann */ 770df8bae1dSRodney W. Grimes ip->ip_len -= hlen; 771df8bae1dSRodney W. Grimes 772b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 77333841545SHajimu UMEMOTO /* 77433841545SHajimu UMEMOTO * enforce IPsec policy checking if we are seeing last header. 77533841545SHajimu UMEMOTO * note that we do not visit this with protocols with pcb layer 77633841545SHajimu UMEMOTO * code - like udp/tcp/raw ip. 77733841545SHajimu UMEMOTO */ 7781dfcf0d2SAndre Oppermann if (ip_ipsec_input(m)) 77933841545SHajimu UMEMOTO goto bad; 780b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 78133841545SHajimu UMEMOTO 782df8bae1dSRodney W. Grimes /* 783df8bae1dSRodney W. Grimes * Switch out to protocol's input routine. 784df8bae1dSRodney W. Grimes */ 78586425c62SRobert Watson IPSTAT_INC(ips_delivered); 7869b932e9eSAndre Oppermann 7872b25acc1SLuigi Rizzo (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 788c67b1d17SGarrett Wollman return; 789df8bae1dSRodney W. Grimes bad: 790df8bae1dSRodney W. Grimes m_freem(m); 791c67b1d17SGarrett Wollman } 792c67b1d17SGarrett Wollman 793c67b1d17SGarrett Wollman /* 794d248c7d7SRobert Watson * After maxnipq has been updated, propagate the change to UMA. The UMA zone 795d248c7d7SRobert Watson * max has slightly different semantics than the sysctl, for historical 796d248c7d7SRobert Watson * reasons. 797d248c7d7SRobert Watson */ 798d248c7d7SRobert Watson static void 799d248c7d7SRobert Watson maxnipq_update(void) 800d248c7d7SRobert Watson { 801d248c7d7SRobert Watson 802d248c7d7SRobert Watson /* 803d248c7d7SRobert Watson * -1 for unlimited allocation. 804d248c7d7SRobert Watson */ 805603724d3SBjoern A. Zeeb if (V_maxnipq < 0) 806603724d3SBjoern A. Zeeb uma_zone_set_max(V_ipq_zone, 0); 807d248c7d7SRobert Watson /* 808d248c7d7SRobert Watson * Positive number for specific bound. 809d248c7d7SRobert Watson */ 810603724d3SBjoern A. Zeeb if (V_maxnipq > 0) 811603724d3SBjoern A. Zeeb uma_zone_set_max(V_ipq_zone, V_maxnipq); 812d248c7d7SRobert Watson /* 813d248c7d7SRobert Watson * Zero specifies no further fragment queue allocation -- set the 814d248c7d7SRobert Watson * bound very low, but rely on implementation elsewhere to actually 815d248c7d7SRobert Watson * prevent allocation and reclaim current queues. 816d248c7d7SRobert Watson */ 817603724d3SBjoern A. Zeeb if (V_maxnipq == 0) 818603724d3SBjoern A. Zeeb uma_zone_set_max(V_ipq_zone, 1); 819d248c7d7SRobert Watson } 820d248c7d7SRobert Watson 8214f590175SPaul Saab static void 8224f590175SPaul Saab ipq_zone_change(void *tag) 8234f590175SPaul Saab { 8244f590175SPaul Saab 825603724d3SBjoern A. Zeeb if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) { 826603724d3SBjoern A. Zeeb V_maxnipq = nmbclusters / 32; 8274f590175SPaul Saab maxnipq_update(); 8284f590175SPaul Saab } 8294f590175SPaul Saab } 8304f590175SPaul Saab 831d248c7d7SRobert Watson static int 832d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS) 833d248c7d7SRobert Watson { 834d248c7d7SRobert Watson int error, i; 835d248c7d7SRobert Watson 836603724d3SBjoern A. Zeeb i = V_maxnipq; 837d248c7d7SRobert Watson error = sysctl_handle_int(oidp, &i, 0, req); 838d248c7d7SRobert Watson if (error || !req->newptr) 839d248c7d7SRobert Watson return (error); 840d248c7d7SRobert Watson 841d248c7d7SRobert Watson /* 842d248c7d7SRobert Watson * XXXRW: Might be a good idea to sanity check the argument and place 843d248c7d7SRobert Watson * an extreme upper bound. 844d248c7d7SRobert Watson */ 845d248c7d7SRobert Watson if (i < -1) 846d248c7d7SRobert Watson return (EINVAL); 847603724d3SBjoern A. Zeeb V_maxnipq = i; 848d248c7d7SRobert Watson maxnipq_update(); 849d248c7d7SRobert Watson return (0); 850d248c7d7SRobert Watson } 851d248c7d7SRobert Watson 852d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW, 853d248c7d7SRobert Watson NULL, 0, sysctl_maxnipq, "I", 854d248c7d7SRobert Watson "Maximum number of IPv4 fragment reassembly queue entries"); 855d248c7d7SRobert Watson 856d248c7d7SRobert Watson /* 8578948e4baSArchie Cobbs * Take incoming datagram fragment and try to reassemble it into 858f0cada84SAndre Oppermann * whole datagram. If the argument is the first fragment or one 859f0cada84SAndre Oppermann * in between the function will return NULL and store the mbuf 860f0cada84SAndre Oppermann * in the fragment chain. If the argument is the last fragment 861f0cada84SAndre Oppermann * the packet will be reassembled and the pointer to the new 862f0cada84SAndre Oppermann * mbuf returned for further processing. Only m_tags attached 863f0cada84SAndre Oppermann * to the first packet/fragment are preserved. 864f0cada84SAndre Oppermann * The IP header is *NOT* adjusted out of iplen. 865df8bae1dSRodney W. Grimes */ 866f0cada84SAndre Oppermann struct mbuf * 867f0cada84SAndre Oppermann ip_reass(struct mbuf *m) 868df8bae1dSRodney W. Grimes { 869f0cada84SAndre Oppermann struct ip *ip; 870f0cada84SAndre Oppermann struct mbuf *p, *q, *nq, *t; 871f0cada84SAndre Oppermann struct ipq *fp = NULL; 872f0cada84SAndre Oppermann struct ipqhead *head; 873f0cada84SAndre Oppermann int i, hlen, next; 87459dfcba4SHajimu UMEMOTO u_int8_t ecn, ecn0; 875f0cada84SAndre Oppermann u_short hash; 876df8bae1dSRodney W. Grimes 877800af1fbSMaxim Konovalov /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */ 878603724d3SBjoern A. Zeeb if (V_maxnipq == 0 || V_maxfragsperpacket == 0) { 87986425c62SRobert Watson IPSTAT_INC(ips_fragments); 88086425c62SRobert Watson IPSTAT_INC(ips_fragdropped); 8819d804f81SAndre Oppermann m_freem(m); 8829d804f81SAndre Oppermann return (NULL); 883f0cada84SAndre Oppermann } 8842fad1e93SSam Leffler 885f0cada84SAndre Oppermann ip = mtod(m, struct ip *); 886f0cada84SAndre Oppermann hlen = ip->ip_hl << 2; 887f0cada84SAndre Oppermann 888f0cada84SAndre Oppermann hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 889603724d3SBjoern A. Zeeb head = &V_ipq[hash]; 890f0cada84SAndre Oppermann IPQ_LOCK(); 891f0cada84SAndre Oppermann 892f0cada84SAndre Oppermann /* 893f0cada84SAndre Oppermann * Look for queue of fragments 894f0cada84SAndre Oppermann * of this datagram. 895f0cada84SAndre Oppermann */ 896f0cada84SAndre Oppermann TAILQ_FOREACH(fp, head, ipq_list) 897f0cada84SAndre Oppermann if (ip->ip_id == fp->ipq_id && 898f0cada84SAndre Oppermann ip->ip_src.s_addr == fp->ipq_src.s_addr && 899f0cada84SAndre Oppermann ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 900f0cada84SAndre Oppermann #ifdef MAC 90130d239bcSRobert Watson mac_ipq_match(m, fp) && 902f0cada84SAndre Oppermann #endif 903f0cada84SAndre Oppermann ip->ip_p == fp->ipq_p) 904f0cada84SAndre Oppermann goto found; 905f0cada84SAndre Oppermann 906f0cada84SAndre Oppermann fp = NULL; 907f0cada84SAndre Oppermann 908f0cada84SAndre Oppermann /* 909d248c7d7SRobert Watson * Attempt to trim the number of allocated fragment queues if it 910d248c7d7SRobert Watson * exceeds the administrative limit. 911f0cada84SAndre Oppermann */ 912603724d3SBjoern A. Zeeb if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) { 913f0cada84SAndre Oppermann /* 914f0cada84SAndre Oppermann * drop something from the tail of the current queue 915f0cada84SAndre Oppermann * before proceeding further 916f0cada84SAndre Oppermann */ 917f0cada84SAndre Oppermann struct ipq *q = TAILQ_LAST(head, ipqhead); 918f0cada84SAndre Oppermann if (q == NULL) { /* gak */ 919f0cada84SAndre Oppermann for (i = 0; i < IPREASS_NHASH; i++) { 920603724d3SBjoern A. Zeeb struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead); 921f0cada84SAndre Oppermann if (r) { 92286425c62SRobert Watson IPSTAT_ADD(ips_fragtimeout, 92386425c62SRobert Watson r->ipq_nfrags); 924603724d3SBjoern A. Zeeb ip_freef(&V_ipq[i], r); 925f0cada84SAndre Oppermann break; 926f0cada84SAndre Oppermann } 927f0cada84SAndre Oppermann } 928f0cada84SAndre Oppermann } else { 92986425c62SRobert Watson IPSTAT_ADD(ips_fragtimeout, q->ipq_nfrags); 930f0cada84SAndre Oppermann ip_freef(head, q); 931f0cada84SAndre Oppermann } 932f0cada84SAndre Oppermann } 933f0cada84SAndre Oppermann 934f0cada84SAndre Oppermann found: 935f0cada84SAndre Oppermann /* 936f0cada84SAndre Oppermann * Adjust ip_len to not reflect header, 937f0cada84SAndre Oppermann * convert offset of this to bytes. 938f0cada84SAndre Oppermann */ 939f0cada84SAndre Oppermann ip->ip_len -= hlen; 940f0cada84SAndre Oppermann if (ip->ip_off & IP_MF) { 941f0cada84SAndre Oppermann /* 942f0cada84SAndre Oppermann * Make sure that fragments have a data length 943f0cada84SAndre Oppermann * that's a non-zero multiple of 8 bytes. 944f0cada84SAndre Oppermann */ 945f0cada84SAndre Oppermann if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { 94686425c62SRobert Watson IPSTAT_INC(ips_toosmall); /* XXX */ 947f0cada84SAndre Oppermann goto dropfrag; 948f0cada84SAndre Oppermann } 949f0cada84SAndre Oppermann m->m_flags |= M_FRAG; 950f0cada84SAndre Oppermann } else 951f0cada84SAndre Oppermann m->m_flags &= ~M_FRAG; 952f0cada84SAndre Oppermann ip->ip_off <<= 3; 953f0cada84SAndre Oppermann 954f0cada84SAndre Oppermann 955f0cada84SAndre Oppermann /* 956f0cada84SAndre Oppermann * Attempt reassembly; if it succeeds, proceed. 957f0cada84SAndre Oppermann * ip_reass() will return a different mbuf. 958f0cada84SAndre Oppermann */ 95986425c62SRobert Watson IPSTAT_INC(ips_fragments); 960f0cada84SAndre Oppermann m->m_pkthdr.header = ip; 961f0cada84SAndre Oppermann 962f0cada84SAndre Oppermann /* Previous ip_reass() started here. */ 963df8bae1dSRodney W. Grimes /* 964df8bae1dSRodney W. Grimes * Presence of header sizes in mbufs 965df8bae1dSRodney W. Grimes * would confuse code below. 966df8bae1dSRodney W. Grimes */ 967df8bae1dSRodney W. Grimes m->m_data += hlen; 968df8bae1dSRodney W. Grimes m->m_len -= hlen; 969df8bae1dSRodney W. Grimes 970df8bae1dSRodney W. Grimes /* 971df8bae1dSRodney W. Grimes * If first fragment to arrive, create a reassembly queue. 972df8bae1dSRodney W. Grimes */ 973042bbfa3SRobert Watson if (fp == NULL) { 974603724d3SBjoern A. Zeeb fp = uma_zalloc(V_ipq_zone, M_NOWAIT); 975d248c7d7SRobert Watson if (fp == NULL) 976df8bae1dSRodney W. Grimes goto dropfrag; 97736b0360bSRobert Watson #ifdef MAC 97830d239bcSRobert Watson if (mac_ipq_init(fp, M_NOWAIT) != 0) { 979603724d3SBjoern A. Zeeb uma_zfree(V_ipq_zone, fp); 9801d7d0bfeSPawel Jakub Dawidek fp = NULL; 9815e7ce478SRobert Watson goto dropfrag; 9825e7ce478SRobert Watson } 98330d239bcSRobert Watson mac_ipq_create(m, fp); 98436b0360bSRobert Watson #endif 985462b86feSPoul-Henning Kamp TAILQ_INSERT_HEAD(head, fp, ipq_list); 986603724d3SBjoern A. Zeeb V_nipq++; 987375386e2SMike Silbersack fp->ipq_nfrags = 1; 988df8bae1dSRodney W. Grimes fp->ipq_ttl = IPFRAGTTL; 989df8bae1dSRodney W. Grimes fp->ipq_p = ip->ip_p; 990df8bae1dSRodney W. Grimes fp->ipq_id = ip->ip_id; 9916effc713SDoug Rabson fp->ipq_src = ip->ip_src; 9926effc713SDoug Rabson fp->ipq_dst = ip->ip_dst; 993af38c68cSLuigi Rizzo fp->ipq_frags = m; 994af38c68cSLuigi Rizzo m->m_nextpkt = NULL; 995800af1fbSMaxim Konovalov goto done; 99636b0360bSRobert Watson } else { 997375386e2SMike Silbersack fp->ipq_nfrags++; 99836b0360bSRobert Watson #ifdef MAC 99930d239bcSRobert Watson mac_ipq_update(m, fp); 100036b0360bSRobert Watson #endif 1001df8bae1dSRodney W. Grimes } 1002df8bae1dSRodney W. Grimes 10036effc713SDoug Rabson #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) 10046effc713SDoug Rabson 1005df8bae1dSRodney W. Grimes /* 100659dfcba4SHajimu UMEMOTO * Handle ECN by comparing this segment with the first one; 100759dfcba4SHajimu UMEMOTO * if CE is set, do not lose CE. 100859dfcba4SHajimu UMEMOTO * drop if CE and not-ECT are mixed for the same packet. 100959dfcba4SHajimu UMEMOTO */ 101059dfcba4SHajimu UMEMOTO ecn = ip->ip_tos & IPTOS_ECN_MASK; 101159dfcba4SHajimu UMEMOTO ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK; 101259dfcba4SHajimu UMEMOTO if (ecn == IPTOS_ECN_CE) { 101359dfcba4SHajimu UMEMOTO if (ecn0 == IPTOS_ECN_NOTECT) 101459dfcba4SHajimu UMEMOTO goto dropfrag; 101559dfcba4SHajimu UMEMOTO if (ecn0 != IPTOS_ECN_CE) 101659dfcba4SHajimu UMEMOTO GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE; 101759dfcba4SHajimu UMEMOTO } 101859dfcba4SHajimu UMEMOTO if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) 101959dfcba4SHajimu UMEMOTO goto dropfrag; 102059dfcba4SHajimu UMEMOTO 102159dfcba4SHajimu UMEMOTO /* 1022df8bae1dSRodney W. Grimes * Find a segment which begins after this one does. 1023df8bae1dSRodney W. Grimes */ 10246effc713SDoug Rabson for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 10256effc713SDoug Rabson if (GETIP(q)->ip_off > ip->ip_off) 1026df8bae1dSRodney W. Grimes break; 1027df8bae1dSRodney W. Grimes 1028df8bae1dSRodney W. Grimes /* 1029df8bae1dSRodney W. Grimes * If there is a preceding segment, it may provide some of 1030df8bae1dSRodney W. Grimes * our data already. If so, drop the data from the incoming 1031af38c68cSLuigi Rizzo * segment. If it provides all of our data, drop us, otherwise 1032af38c68cSLuigi Rizzo * stick new segment in the proper place. 1033db4f9cc7SJonathan Lemon * 1034db4f9cc7SJonathan Lemon * If some of the data is dropped from the the preceding 1035db4f9cc7SJonathan Lemon * segment, then it's checksum is invalidated. 1036df8bae1dSRodney W. Grimes */ 10376effc713SDoug Rabson if (p) { 10386effc713SDoug Rabson i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; 1039df8bae1dSRodney W. Grimes if (i > 0) { 1040df8bae1dSRodney W. Grimes if (i >= ip->ip_len) 1041df8bae1dSRodney W. Grimes goto dropfrag; 10426a800098SYoshinobu Inoue m_adj(m, i); 1043db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = 0; 1044df8bae1dSRodney W. Grimes ip->ip_off += i; 1045df8bae1dSRodney W. Grimes ip->ip_len -= i; 1046df8bae1dSRodney W. Grimes } 1047af38c68cSLuigi Rizzo m->m_nextpkt = p->m_nextpkt; 1048af38c68cSLuigi Rizzo p->m_nextpkt = m; 1049af38c68cSLuigi Rizzo } else { 1050af38c68cSLuigi Rizzo m->m_nextpkt = fp->ipq_frags; 1051af38c68cSLuigi Rizzo fp->ipq_frags = m; 1052df8bae1dSRodney W. Grimes } 1053df8bae1dSRodney W. Grimes 1054df8bae1dSRodney W. Grimes /* 1055df8bae1dSRodney W. Grimes * While we overlap succeeding segments trim them or, 1056df8bae1dSRodney W. Grimes * if they are completely covered, dequeue them. 1057df8bae1dSRodney W. Grimes */ 10586effc713SDoug Rabson for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; 1059af38c68cSLuigi Rizzo q = nq) { 1060b36f5b37SMaxim Konovalov i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off; 10616effc713SDoug Rabson if (i < GETIP(q)->ip_len) { 10626effc713SDoug Rabson GETIP(q)->ip_len -= i; 10636effc713SDoug Rabson GETIP(q)->ip_off += i; 10646effc713SDoug Rabson m_adj(q, i); 1065db4f9cc7SJonathan Lemon q->m_pkthdr.csum_flags = 0; 1066df8bae1dSRodney W. Grimes break; 1067df8bae1dSRodney W. Grimes } 10686effc713SDoug Rabson nq = q->m_nextpkt; 1069af38c68cSLuigi Rizzo m->m_nextpkt = nq; 107086425c62SRobert Watson IPSTAT_INC(ips_fragdropped); 1071375386e2SMike Silbersack fp->ipq_nfrags--; 10726effc713SDoug Rabson m_freem(q); 1073df8bae1dSRodney W. Grimes } 1074df8bae1dSRodney W. Grimes 1075df8bae1dSRodney W. Grimes /* 1076375386e2SMike Silbersack * Check for complete reassembly and perform frag per packet 1077375386e2SMike Silbersack * limiting. 1078375386e2SMike Silbersack * 1079375386e2SMike Silbersack * Frag limiting is performed here so that the nth frag has 1080375386e2SMike Silbersack * a chance to complete the packet before we drop the packet. 1081375386e2SMike Silbersack * As a result, n+1 frags are actually allowed per packet, but 1082375386e2SMike Silbersack * only n will ever be stored. (n = maxfragsperpacket.) 1083375386e2SMike Silbersack * 1084df8bae1dSRodney W. Grimes */ 10856effc713SDoug Rabson next = 0; 10866effc713SDoug Rabson for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 1087375386e2SMike Silbersack if (GETIP(q)->ip_off != next) { 1088603724d3SBjoern A. Zeeb if (fp->ipq_nfrags > V_maxfragsperpacket) { 108986425c62SRobert Watson IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags); 1090375386e2SMike Silbersack ip_freef(head, fp); 109199e8617dSMaxim Konovalov } 1092f0cada84SAndre Oppermann goto done; 1093375386e2SMike Silbersack } 10946effc713SDoug Rabson next += GETIP(q)->ip_len; 10956effc713SDoug Rabson } 10966effc713SDoug Rabson /* Make sure the last packet didn't have the IP_MF flag */ 1097375386e2SMike Silbersack if (p->m_flags & M_FRAG) { 1098603724d3SBjoern A. Zeeb if (fp->ipq_nfrags > V_maxfragsperpacket) { 109986425c62SRobert Watson IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags); 1100375386e2SMike Silbersack ip_freef(head, fp); 110199e8617dSMaxim Konovalov } 1102f0cada84SAndre Oppermann goto done; 1103375386e2SMike Silbersack } 1104df8bae1dSRodney W. Grimes 1105df8bae1dSRodney W. Grimes /* 1106430d30d8SBill Fenner * Reassembly is complete. Make sure the packet is a sane size. 1107430d30d8SBill Fenner */ 11086effc713SDoug Rabson q = fp->ipq_frags; 11096effc713SDoug Rabson ip = GETIP(q); 111053be11f6SPoul-Henning Kamp if (next + (ip->ip_hl << 2) > IP_MAXPACKET) { 111186425c62SRobert Watson IPSTAT_INC(ips_toolong); 111286425c62SRobert Watson IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags); 1113462b86feSPoul-Henning Kamp ip_freef(head, fp); 1114f0cada84SAndre Oppermann goto done; 1115430d30d8SBill Fenner } 1116430d30d8SBill Fenner 1117430d30d8SBill Fenner /* 1118430d30d8SBill Fenner * Concatenate fragments. 1119df8bae1dSRodney W. Grimes */ 11206effc713SDoug Rabson m = q; 1121df8bae1dSRodney W. Grimes t = m->m_next; 112202410549SRobert Watson m->m_next = NULL; 1123df8bae1dSRodney W. Grimes m_cat(m, t); 11246effc713SDoug Rabson nq = q->m_nextpkt; 112502410549SRobert Watson q->m_nextpkt = NULL; 11266effc713SDoug Rabson for (q = nq; q != NULL; q = nq) { 11276effc713SDoug Rabson nq = q->m_nextpkt; 1128945aa40dSDoug Rabson q->m_nextpkt = NULL; 1129db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; 1130db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; 1131a8db1d93SJonathan Lemon m_cat(m, q); 1132df8bae1dSRodney W. Grimes } 11336edb555dSOleg Bulyzhin /* 11346edb555dSOleg Bulyzhin * In order to do checksumming faster we do 'end-around carry' here 11356edb555dSOleg Bulyzhin * (and not in for{} loop), though it implies we are not going to 11366edb555dSOleg Bulyzhin * reassemble more than 64k fragments. 11376edb555dSOleg Bulyzhin */ 11386edb555dSOleg Bulyzhin m->m_pkthdr.csum_data = 11396edb555dSOleg Bulyzhin (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16); 114036b0360bSRobert Watson #ifdef MAC 114130d239bcSRobert Watson mac_ipq_reassemble(fp, m); 114230d239bcSRobert Watson mac_ipq_destroy(fp); 114336b0360bSRobert Watson #endif 1144df8bae1dSRodney W. Grimes 1145df8bae1dSRodney W. Grimes /* 1146f0cada84SAndre Oppermann * Create header for new ip packet by modifying header of first 1147f0cada84SAndre Oppermann * packet; dequeue and discard fragment reassembly header. 1148df8bae1dSRodney W. Grimes * Make header visible. 1149df8bae1dSRodney W. Grimes */ 1150f0cada84SAndre Oppermann ip->ip_len = (ip->ip_hl << 2) + next; 11516effc713SDoug Rabson ip->ip_src = fp->ipq_src; 11526effc713SDoug Rabson ip->ip_dst = fp->ipq_dst; 1153462b86feSPoul-Henning Kamp TAILQ_REMOVE(head, fp, ipq_list); 1154603724d3SBjoern A. Zeeb V_nipq--; 1155603724d3SBjoern A. Zeeb uma_zfree(V_ipq_zone, fp); 115653be11f6SPoul-Henning Kamp m->m_len += (ip->ip_hl << 2); 115753be11f6SPoul-Henning Kamp m->m_data -= (ip->ip_hl << 2); 1158df8bae1dSRodney W. Grimes /* some debugging cruft by sklower, below, will go away soon */ 1159a5554bf0SPoul-Henning Kamp if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */ 1160a5554bf0SPoul-Henning Kamp m_fixhdr(m); 116186425c62SRobert Watson IPSTAT_INC(ips_reassembled); 1162f0cada84SAndre Oppermann IPQ_UNLOCK(); 11636a800098SYoshinobu Inoue return (m); 1164df8bae1dSRodney W. Grimes 1165df8bae1dSRodney W. Grimes dropfrag: 116686425c62SRobert Watson IPSTAT_INC(ips_fragdropped); 1167042bbfa3SRobert Watson if (fp != NULL) 1168375386e2SMike Silbersack fp->ipq_nfrags--; 1169df8bae1dSRodney W. Grimes m_freem(m); 1170f0cada84SAndre Oppermann done: 1171f0cada84SAndre Oppermann IPQ_UNLOCK(); 1172f0cada84SAndre Oppermann return (NULL); 11736effc713SDoug Rabson 11746effc713SDoug Rabson #undef GETIP 1175df8bae1dSRodney W. Grimes } 1176df8bae1dSRodney W. Grimes 1177df8bae1dSRodney W. Grimes /* 1178df8bae1dSRodney W. Grimes * Free a fragment reassembly header and all 1179df8bae1dSRodney W. Grimes * associated datagrams. 1180df8bae1dSRodney W. Grimes */ 11810312fbe9SPoul-Henning Kamp static void 1182f2565d68SRobert Watson ip_freef(struct ipqhead *fhp, struct ipq *fp) 1183df8bae1dSRodney W. Grimes { 1184f2565d68SRobert Watson struct mbuf *q; 1185df8bae1dSRodney W. Grimes 11862fad1e93SSam Leffler IPQ_LOCK_ASSERT(); 11872fad1e93SSam Leffler 11886effc713SDoug Rabson while (fp->ipq_frags) { 11896effc713SDoug Rabson q = fp->ipq_frags; 11906effc713SDoug Rabson fp->ipq_frags = q->m_nextpkt; 11916effc713SDoug Rabson m_freem(q); 1192df8bae1dSRodney W. Grimes } 1193462b86feSPoul-Henning Kamp TAILQ_REMOVE(fhp, fp, ipq_list); 1194603724d3SBjoern A. Zeeb uma_zfree(V_ipq_zone, fp); 1195603724d3SBjoern A. Zeeb V_nipq--; 1196df8bae1dSRodney W. Grimes } 1197df8bae1dSRodney W. Grimes 1198df8bae1dSRodney W. Grimes /* 1199df8bae1dSRodney W. Grimes * IP timer processing; 1200df8bae1dSRodney W. Grimes * if a timer expires on a reassembly 1201df8bae1dSRodney W. Grimes * queue, discard it. 1202df8bae1dSRodney W. Grimes */ 1203df8bae1dSRodney W. Grimes void 1204f2565d68SRobert Watson ip_slowtimo(void) 1205df8bae1dSRodney W. Grimes { 12068b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 1207f2565d68SRobert Watson struct ipq *fp; 1208194a213eSAndrey A. Chernov int i; 1209df8bae1dSRodney W. Grimes 12105ee847d3SRobert Watson VNET_LIST_RLOCK_NOSLEEP(); 12112fad1e93SSam Leffler IPQ_LOCK(); 12128b615593SMarko Zec VNET_FOREACH(vnet_iter) { 12138b615593SMarko Zec CURVNET_SET(vnet_iter); 1214194a213eSAndrey A. Chernov for (i = 0; i < IPREASS_NHASH; i++) { 1215603724d3SBjoern A. Zeeb for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) { 1216462b86feSPoul-Henning Kamp struct ipq *fpp; 1217462b86feSPoul-Henning Kamp 1218462b86feSPoul-Henning Kamp fpp = fp; 1219462b86feSPoul-Henning Kamp fp = TAILQ_NEXT(fp, ipq_list); 1220462b86feSPoul-Henning Kamp if(--fpp->ipq_ttl == 0) { 122186425c62SRobert Watson IPSTAT_ADD(ips_fragtimeout, 122286425c62SRobert Watson fpp->ipq_nfrags); 1223603724d3SBjoern A. Zeeb ip_freef(&V_ipq[i], fpp); 1224df8bae1dSRodney W. Grimes } 1225df8bae1dSRodney W. Grimes } 1226194a213eSAndrey A. Chernov } 1227690a6055SJesper Skriver /* 1228690a6055SJesper Skriver * If we are over the maximum number of fragments 1229690a6055SJesper Skriver * (due to the limit being lowered), drain off 1230690a6055SJesper Skriver * enough to get down to the new limit. 1231690a6055SJesper Skriver */ 1232603724d3SBjoern A. Zeeb if (V_maxnipq >= 0 && V_nipq > V_maxnipq) { 1233690a6055SJesper Skriver for (i = 0; i < IPREASS_NHASH; i++) { 12348b615593SMarko Zec while (V_nipq > V_maxnipq && 12358b615593SMarko Zec !TAILQ_EMPTY(&V_ipq[i])) { 123686425c62SRobert Watson IPSTAT_ADD(ips_fragdropped, 123786425c62SRobert Watson TAILQ_FIRST(&V_ipq[i])->ipq_nfrags); 12388b615593SMarko Zec ip_freef(&V_ipq[i], 12398b615593SMarko Zec TAILQ_FIRST(&V_ipq[i])); 1240690a6055SJesper Skriver } 1241690a6055SJesper Skriver } 1242690a6055SJesper Skriver } 12438b615593SMarko Zec CURVNET_RESTORE(); 12448b615593SMarko Zec } 12452fad1e93SSam Leffler IPQ_UNLOCK(); 12465ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 1247df8bae1dSRodney W. Grimes } 1248df8bae1dSRodney W. Grimes 1249df8bae1dSRodney W. Grimes /* 1250df8bae1dSRodney W. Grimes * Drain off all datagram fragments. 1251df8bae1dSRodney W. Grimes */ 12529802380eSBjoern A. Zeeb static void 12539802380eSBjoern A. Zeeb ip_drain_locked(void) 1254df8bae1dSRodney W. Grimes { 1255194a213eSAndrey A. Chernov int i; 1256ce29ab3aSGarrett Wollman 12579802380eSBjoern A. Zeeb IPQ_LOCK_ASSERT(); 12589802380eSBjoern A. Zeeb 1259194a213eSAndrey A. Chernov for (i = 0; i < IPREASS_NHASH; i++) { 1260603724d3SBjoern A. Zeeb while(!TAILQ_EMPTY(&V_ipq[i])) { 126186425c62SRobert Watson IPSTAT_ADD(ips_fragdropped, 126286425c62SRobert Watson TAILQ_FIRST(&V_ipq[i])->ipq_nfrags); 1263603724d3SBjoern A. Zeeb ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i])); 1264194a213eSAndrey A. Chernov } 1265194a213eSAndrey A. Chernov } 12669802380eSBjoern A. Zeeb } 12679802380eSBjoern A. Zeeb 12689802380eSBjoern A. Zeeb void 12699802380eSBjoern A. Zeeb ip_drain(void) 12709802380eSBjoern A. Zeeb { 12719802380eSBjoern A. Zeeb VNET_ITERATOR_DECL(vnet_iter); 12729802380eSBjoern A. Zeeb 12739802380eSBjoern A. Zeeb VNET_LIST_RLOCK_NOSLEEP(); 12749802380eSBjoern A. Zeeb IPQ_LOCK(); 12759802380eSBjoern A. Zeeb VNET_FOREACH(vnet_iter) { 12769802380eSBjoern A. Zeeb CURVNET_SET(vnet_iter); 12779802380eSBjoern A. Zeeb ip_drain_locked(); 12788b615593SMarko Zec CURVNET_RESTORE(); 12798b615593SMarko Zec } 12802fad1e93SSam Leffler IPQ_UNLOCK(); 12815ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 1282ce29ab3aSGarrett Wollman in_rtqdrain(); 1283df8bae1dSRodney W. Grimes } 1284df8bae1dSRodney W. Grimes 1285df8bae1dSRodney W. Grimes /* 1286de38924dSAndre Oppermann * The protocol to be inserted into ip_protox[] must be already registered 1287de38924dSAndre Oppermann * in inetsw[], either statically or through pf_proto_register(). 1288de38924dSAndre Oppermann */ 1289de38924dSAndre Oppermann int 1290de38924dSAndre Oppermann ipproto_register(u_char ipproto) 1291de38924dSAndre Oppermann { 1292de38924dSAndre Oppermann struct protosw *pr; 1293de38924dSAndre Oppermann 1294de38924dSAndre Oppermann /* Sanity checks. */ 1295de38924dSAndre Oppermann if (ipproto == 0) 1296de38924dSAndre Oppermann return (EPROTONOSUPPORT); 1297de38924dSAndre Oppermann 1298de38924dSAndre Oppermann /* 1299de38924dSAndre Oppermann * The protocol slot must not be occupied by another protocol 1300de38924dSAndre Oppermann * already. An index pointing to IPPROTO_RAW is unused. 1301de38924dSAndre Oppermann */ 1302de38924dSAndre Oppermann pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 1303de38924dSAndre Oppermann if (pr == NULL) 1304de38924dSAndre Oppermann return (EPFNOSUPPORT); 1305de38924dSAndre Oppermann if (ip_protox[ipproto] != pr - inetsw) /* IPPROTO_RAW */ 1306de38924dSAndre Oppermann return (EEXIST); 1307de38924dSAndre Oppermann 1308de38924dSAndre Oppermann /* Find the protocol position in inetsw[] and set the index. */ 1309de38924dSAndre Oppermann for (pr = inetdomain.dom_protosw; 1310de38924dSAndre Oppermann pr < inetdomain.dom_protoswNPROTOSW; pr++) { 1311de38924dSAndre Oppermann if (pr->pr_domain->dom_family == PF_INET && 1312de38924dSAndre Oppermann pr->pr_protocol && pr->pr_protocol == ipproto) { 1313de38924dSAndre Oppermann /* Be careful to only index valid IP protocols. */ 1314db77984cSSam Leffler if (pr->pr_protocol < IPPROTO_MAX) { 1315de38924dSAndre Oppermann ip_protox[pr->pr_protocol] = pr - inetsw; 1316de38924dSAndre Oppermann return (0); 1317de38924dSAndre Oppermann } else 1318de38924dSAndre Oppermann return (EINVAL); 1319de38924dSAndre Oppermann } 1320de38924dSAndre Oppermann } 1321de38924dSAndre Oppermann return (EPROTONOSUPPORT); 1322de38924dSAndre Oppermann } 1323de38924dSAndre Oppermann 1324de38924dSAndre Oppermann int 1325de38924dSAndre Oppermann ipproto_unregister(u_char ipproto) 1326de38924dSAndre Oppermann { 1327de38924dSAndre Oppermann struct protosw *pr; 1328de38924dSAndre Oppermann 1329de38924dSAndre Oppermann /* Sanity checks. */ 1330de38924dSAndre Oppermann if (ipproto == 0) 1331de38924dSAndre Oppermann return (EPROTONOSUPPORT); 1332de38924dSAndre Oppermann 1333de38924dSAndre Oppermann /* Check if the protocol was indeed registered. */ 1334de38924dSAndre Oppermann pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 1335de38924dSAndre Oppermann if (pr == NULL) 1336de38924dSAndre Oppermann return (EPFNOSUPPORT); 1337de38924dSAndre Oppermann if (ip_protox[ipproto] == pr - inetsw) /* IPPROTO_RAW */ 1338de38924dSAndre Oppermann return (ENOENT); 1339de38924dSAndre Oppermann 1340de38924dSAndre Oppermann /* Reset the protocol slot to IPPROTO_RAW. */ 1341de38924dSAndre Oppermann ip_protox[ipproto] = pr - inetsw; 1342de38924dSAndre Oppermann return (0); 1343de38924dSAndre Oppermann } 1344de38924dSAndre Oppermann 1345df8bae1dSRodney W. Grimes /* 13468c0fec80SRobert Watson * Given address of next destination (final or next hop), return (referenced) 13478c0fec80SRobert Watson * internet address info of interface to be used to get there. 1348df8bae1dSRodney W. Grimes */ 1349bd714208SRuslan Ermilov struct in_ifaddr * 13508b07e49aSJulian Elischer ip_rtaddr(struct in_addr dst, u_int fibnum) 1351df8bae1dSRodney W. Grimes { 135297d8d152SAndre Oppermann struct route sro; 135302c1c707SAndre Oppermann struct sockaddr_in *sin; 135419e5b0a7SRobert Watson struct in_ifaddr *ia; 1355df8bae1dSRodney W. Grimes 13560cfbbe3bSAndre Oppermann bzero(&sro, sizeof(sro)); 135797d8d152SAndre Oppermann sin = (struct sockaddr_in *)&sro.ro_dst; 1358df8bae1dSRodney W. Grimes sin->sin_family = AF_INET; 1359df8bae1dSRodney W. Grimes sin->sin_len = sizeof(*sin); 1360df8bae1dSRodney W. Grimes sin->sin_addr = dst; 13616e6b3f7cSQing Li in_rtalloc_ign(&sro, 0, fibnum); 1362df8bae1dSRodney W. Grimes 136397d8d152SAndre Oppermann if (sro.ro_rt == NULL) 136402410549SRobert Watson return (NULL); 136502c1c707SAndre Oppermann 136619e5b0a7SRobert Watson ia = ifatoia(sro.ro_rt->rt_ifa); 136719e5b0a7SRobert Watson ifa_ref(&ia->ia_ifa); 136897d8d152SAndre Oppermann RTFREE(sro.ro_rt); 136919e5b0a7SRobert Watson return (ia); 1370df8bae1dSRodney W. Grimes } 1371df8bae1dSRodney W. Grimes 1372df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = { 1373df8bae1dSRodney W. Grimes 0, 0, 0, 0, 1374df8bae1dSRodney W. Grimes 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1375df8bae1dSRodney W. Grimes EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1376df8bae1dSRodney W. Grimes EMSGSIZE, EHOSTUNREACH, 0, 0, 1377fcaf9f91SMike Silbersack 0, 0, EHOSTUNREACH, 0, 13783b8123b7SJesper Skriver ENOPROTOOPT, ECONNREFUSED 1379df8bae1dSRodney W. Grimes }; 1380df8bae1dSRodney W. Grimes 1381df8bae1dSRodney W. Grimes /* 1382df8bae1dSRodney W. Grimes * Forward a packet. If some error occurs return the sender 1383df8bae1dSRodney W. Grimes * an icmp packet. Note we can't always generate a meaningful 1384df8bae1dSRodney W. Grimes * icmp message because icmp doesn't have a large enough repertoire 1385df8bae1dSRodney W. Grimes * of codes and types. 1386df8bae1dSRodney W. Grimes * 1387df8bae1dSRodney W. Grimes * If not forwarding, just drop the packet. This could be confusing 1388df8bae1dSRodney W. Grimes * if ipforwarding was zero but some routing protocol was advancing 1389df8bae1dSRodney W. Grimes * us as a gateway to somewhere. However, we must let the routing 1390df8bae1dSRodney W. Grimes * protocol deal with that. 1391df8bae1dSRodney W. Grimes * 1392df8bae1dSRodney W. Grimes * The srcrt parameter indicates whether the packet is being forwarded 1393df8bae1dSRodney W. Grimes * via a source route. 1394df8bae1dSRodney W. Grimes */ 13959b932e9eSAndre Oppermann void 13969b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt) 1397df8bae1dSRodney W. Grimes { 13982b25acc1SLuigi Rizzo struct ip *ip = mtod(m, struct ip *); 1399efbad259SEdward Tomasz Napierala struct in_ifaddr *ia; 1400df8bae1dSRodney W. Grimes struct mbuf *mcopy; 14019b932e9eSAndre Oppermann struct in_addr dest; 1402b835b6feSBjoern A. Zeeb struct route ro; 1403c773494eSAndre Oppermann int error, type = 0, code = 0, mtu = 0; 14043efc3014SJulian Elischer 14059b932e9eSAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 140686425c62SRobert Watson IPSTAT_INC(ips_cantforward); 1407df8bae1dSRodney W. Grimes m_freem(m); 1408df8bae1dSRodney W. Grimes return; 1409df8bae1dSRodney W. Grimes } 14101b968362SDag-Erling Smørgrav #ifdef IPSTEALTH 1411603724d3SBjoern A. Zeeb if (!V_ipstealth) { 14121b968362SDag-Erling Smørgrav #endif 1413df8bae1dSRodney W. Grimes if (ip->ip_ttl <= IPTTLDEC) { 14141b968362SDag-Erling Smørgrav icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 141502c1c707SAndre Oppermann 0, 0); 1416df8bae1dSRodney W. Grimes return; 1417df8bae1dSRodney W. Grimes } 14181b968362SDag-Erling Smørgrav #ifdef IPSTEALTH 14191b968362SDag-Erling Smørgrav } 14201b968362SDag-Erling Smørgrav #endif 1421df8bae1dSRodney W. Grimes 14228b07e49aSJulian Elischer ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m)); 1423efbad259SEdward Tomasz Napierala #ifndef IPSEC 1424efbad259SEdward Tomasz Napierala /* 1425efbad259SEdward Tomasz Napierala * 'ia' may be NULL if there is no route for this destination. 1426efbad259SEdward Tomasz Napierala * In case of IPsec, Don't discard it just yet, but pass it to 1427efbad259SEdward Tomasz Napierala * ip_output in case of outgoing IPsec policy. 1428efbad259SEdward Tomasz Napierala */ 1429d23d475fSGuido van Rooij if (!srcrt && ia == NULL) { 143002c1c707SAndre Oppermann icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 1431df8bae1dSRodney W. Grimes return; 143202c1c707SAndre Oppermann } 1433efbad259SEdward Tomasz Napierala #endif 1434df8bae1dSRodney W. Grimes 1435df8bae1dSRodney W. Grimes /* 1436bfef7ed4SIan Dowse * Save the IP header and at most 8 bytes of the payload, 1437bfef7ed4SIan Dowse * in case we need to generate an ICMP message to the src. 1438bfef7ed4SIan Dowse * 14394d2e3692SLuigi Rizzo * XXX this can be optimized a lot by saving the data in a local 14404d2e3692SLuigi Rizzo * buffer on the stack (72 bytes at most), and only allocating the 14414d2e3692SLuigi Rizzo * mbuf if really necessary. The vast majority of the packets 14424d2e3692SLuigi Rizzo * are forwarded without having to send an ICMP back (either 14434d2e3692SLuigi Rizzo * because unnecessary, or because rate limited), so we are 14444d2e3692SLuigi Rizzo * really we are wasting a lot of work here. 14454d2e3692SLuigi Rizzo * 1446bfef7ed4SIan Dowse * We don't use m_copy() because it might return a reference 1447bfef7ed4SIan Dowse * to a shared cluster. Both this function and ip_output() 1448bfef7ed4SIan Dowse * assume exclusive access to the IP header in `m', so any 1449bfef7ed4SIan Dowse * data in a cluster may change before we reach icmp_error(). 1450df8bae1dSRodney W. Grimes */ 1451780b2f69SAndre Oppermann MGETHDR(mcopy, M_DONTWAIT, m->m_type); 1452a163d034SWarner Losh if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) { 14539967cafcSSam Leffler /* 14549967cafcSSam Leffler * It's probably ok if the pkthdr dup fails (because 14559967cafcSSam Leffler * the deep copy of the tag chain failed), but for now 14569967cafcSSam Leffler * be conservative and just discard the copy since 14579967cafcSSam Leffler * code below may some day want the tags. 14589967cafcSSam Leffler */ 14599967cafcSSam Leffler m_free(mcopy); 14609967cafcSSam Leffler mcopy = NULL; 14619967cafcSSam Leffler } 1462bfef7ed4SIan Dowse if (mcopy != NULL) { 1463780b2f69SAndre Oppermann mcopy->m_len = min(ip->ip_len, M_TRAILINGSPACE(mcopy)); 1464e6b0a570SBruce M Simpson mcopy->m_pkthdr.len = mcopy->m_len; 1465bfef7ed4SIan Dowse m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t)); 1466bfef7ed4SIan Dowse } 146704287599SRuslan Ermilov 146804287599SRuslan Ermilov #ifdef IPSTEALTH 1469603724d3SBjoern A. Zeeb if (!V_ipstealth) { 147004287599SRuslan Ermilov #endif 147104287599SRuslan Ermilov ip->ip_ttl -= IPTTLDEC; 147204287599SRuslan Ermilov #ifdef IPSTEALTH 147304287599SRuslan Ermilov } 147404287599SRuslan Ermilov #endif 1475df8bae1dSRodney W. Grimes 1476df8bae1dSRodney W. Grimes /* 1477df8bae1dSRodney W. Grimes * If forwarding packet using same interface that it came in on, 1478df8bae1dSRodney W. Grimes * perhaps should send a redirect to sender to shortcut a hop. 1479df8bae1dSRodney W. Grimes * Only send redirect if source is sending directly to us, 1480df8bae1dSRodney W. Grimes * and if packet was not source routed (or has any options). 1481df8bae1dSRodney W. Grimes * Also, don't send redirect if forwarding using a default route 1482df8bae1dSRodney W. Grimes * or a route modified by a redirect. 1483df8bae1dSRodney W. Grimes */ 14849b932e9eSAndre Oppermann dest.s_addr = 0; 1485efbad259SEdward Tomasz Napierala if (!srcrt && V_ipsendredirects && 1486efbad259SEdward Tomasz Napierala ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) { 148702c1c707SAndre Oppermann struct sockaddr_in *sin; 148802c1c707SAndre Oppermann struct rtentry *rt; 148902c1c707SAndre Oppermann 14900cfbbe3bSAndre Oppermann bzero(&ro, sizeof(ro)); 149102c1c707SAndre Oppermann sin = (struct sockaddr_in *)&ro.ro_dst; 149202c1c707SAndre Oppermann sin->sin_family = AF_INET; 149302c1c707SAndre Oppermann sin->sin_len = sizeof(*sin); 14949b932e9eSAndre Oppermann sin->sin_addr = ip->ip_dst; 14956e6b3f7cSQing Li in_rtalloc_ign(&ro, 0, M_GETFIB(m)); 149602c1c707SAndre Oppermann 149702c1c707SAndre Oppermann rt = ro.ro_rt; 149802c1c707SAndre Oppermann 149902c1c707SAndre Oppermann if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 15009b932e9eSAndre Oppermann satosin(rt_key(rt))->sin_addr.s_addr != 0) { 1501df8bae1dSRodney W. Grimes #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1502df8bae1dSRodney W. Grimes u_long src = ntohl(ip->ip_src.s_addr); 1503df8bae1dSRodney W. Grimes 1504df8bae1dSRodney W. Grimes if (RTA(rt) && 1505df8bae1dSRodney W. Grimes (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1506df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_GATEWAY) 15079b932e9eSAndre Oppermann dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr; 1508df8bae1dSRodney W. Grimes else 15099b932e9eSAndre Oppermann dest.s_addr = ip->ip_dst.s_addr; 1510df8bae1dSRodney W. Grimes /* Router requirements says to only send host redirects */ 1511df8bae1dSRodney W. Grimes type = ICMP_REDIRECT; 1512df8bae1dSRodney W. Grimes code = ICMP_REDIRECT_HOST; 1513df8bae1dSRodney W. Grimes } 1514df8bae1dSRodney W. Grimes } 151502c1c707SAndre Oppermann if (rt) 151602c1c707SAndre Oppermann RTFREE(rt); 151702c1c707SAndre Oppermann } 1518df8bae1dSRodney W. Grimes 1519b835b6feSBjoern A. Zeeb /* 1520b835b6feSBjoern A. Zeeb * Try to cache the route MTU from ip_output so we can consider it for 1521b835b6feSBjoern A. Zeeb * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191. 1522b835b6feSBjoern A. Zeeb */ 1523b835b6feSBjoern A. Zeeb bzero(&ro, sizeof(ro)); 1524b835b6feSBjoern A. Zeeb 1525b835b6feSBjoern A. Zeeb error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL); 1526b835b6feSBjoern A. Zeeb 1527b835b6feSBjoern A. Zeeb if (error == EMSGSIZE && ro.ro_rt) 1528b835b6feSBjoern A. Zeeb mtu = ro.ro_rt->rt_rmx.rmx_mtu; 1529b835b6feSBjoern A. Zeeb if (ro.ro_rt) 1530b835b6feSBjoern A. Zeeb RTFREE(ro.ro_rt); 1531b835b6feSBjoern A. Zeeb 1532df8bae1dSRodney W. Grimes if (error) 153386425c62SRobert Watson IPSTAT_INC(ips_cantforward); 1534df8bae1dSRodney W. Grimes else { 153586425c62SRobert Watson IPSTAT_INC(ips_forward); 1536df8bae1dSRodney W. Grimes if (type) 153786425c62SRobert Watson IPSTAT_INC(ips_redirectsent); 1538df8bae1dSRodney W. Grimes else { 15399188b4a1SAndre Oppermann if (mcopy) 1540df8bae1dSRodney W. Grimes m_freem(mcopy); 15418c0fec80SRobert Watson if (ia != NULL) 15428c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 1543df8bae1dSRodney W. Grimes return; 1544df8bae1dSRodney W. Grimes } 1545df8bae1dSRodney W. Grimes } 15468c0fec80SRobert Watson if (mcopy == NULL) { 15478c0fec80SRobert Watson if (ia != NULL) 15488c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 1549df8bae1dSRodney W. Grimes return; 15508c0fec80SRobert Watson } 1551df8bae1dSRodney W. Grimes 1552df8bae1dSRodney W. Grimes switch (error) { 1553df8bae1dSRodney W. Grimes 1554df8bae1dSRodney W. Grimes case 0: /* forwarded, but need redirect */ 1555df8bae1dSRodney W. Grimes /* type, code set above */ 1556df8bae1dSRodney W. Grimes break; 1557df8bae1dSRodney W. Grimes 1558efbad259SEdward Tomasz Napierala case ENETUNREACH: 1559df8bae1dSRodney W. Grimes case EHOSTUNREACH: 1560df8bae1dSRodney W. Grimes case ENETDOWN: 1561df8bae1dSRodney W. Grimes case EHOSTDOWN: 1562df8bae1dSRodney W. Grimes default: 1563df8bae1dSRodney W. Grimes type = ICMP_UNREACH; 1564df8bae1dSRodney W. Grimes code = ICMP_UNREACH_HOST; 1565df8bae1dSRodney W. Grimes break; 1566df8bae1dSRodney W. Grimes 1567df8bae1dSRodney W. Grimes case EMSGSIZE: 1568df8bae1dSRodney W. Grimes type = ICMP_UNREACH; 1569df8bae1dSRodney W. Grimes code = ICMP_UNREACH_NEEDFRAG; 15701dfcf0d2SAndre Oppermann 1571b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 1572b835b6feSBjoern A. Zeeb /* 1573b835b6feSBjoern A. Zeeb * If IPsec is configured for this path, 1574b835b6feSBjoern A. Zeeb * override any possibly mtu value set by ip_output. 1575b835b6feSBjoern A. Zeeb */ 15761c044382SBjoern A. Zeeb mtu = ip_ipsec_mtu(mcopy, mtu); 1577b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 15789b932e9eSAndre Oppermann /* 1579b835b6feSBjoern A. Zeeb * If the MTU was set before make sure we are below the 1580b835b6feSBjoern A. Zeeb * interface MTU. 1581ab48768bSAndre Oppermann * If the MTU wasn't set before use the interface mtu or 1582ab48768bSAndre Oppermann * fall back to the next smaller mtu step compared to the 1583ab48768bSAndre Oppermann * current packet size. 15849b932e9eSAndre Oppermann */ 1585b835b6feSBjoern A. Zeeb if (mtu != 0) { 1586b835b6feSBjoern A. Zeeb if (ia != NULL) 1587b835b6feSBjoern A. Zeeb mtu = min(mtu, ia->ia_ifp->if_mtu); 1588b835b6feSBjoern A. Zeeb } else { 1589ab48768bSAndre Oppermann if (ia != NULL) 1590c773494eSAndre Oppermann mtu = ia->ia_ifp->if_mtu; 1591ab48768bSAndre Oppermann else 1592ab48768bSAndre Oppermann mtu = ip_next_mtu(ip->ip_len, 0); 1593ab48768bSAndre Oppermann } 159486425c62SRobert Watson IPSTAT_INC(ips_cantfrag); 1595df8bae1dSRodney W. Grimes break; 1596df8bae1dSRodney W. Grimes 1597df8bae1dSRodney W. Grimes case ENOBUFS: 1598df285b3dSMike Silbersack /* 1599df285b3dSMike Silbersack * A router should not generate ICMP_SOURCEQUENCH as 1600df285b3dSMike Silbersack * required in RFC1812 Requirements for IP Version 4 Routers. 1601df285b3dSMike Silbersack * Source quench could be a big problem under DoS attacks, 1602df285b3dSMike Silbersack * or if the underlying interface is rate-limited. 1603df285b3dSMike Silbersack * Those who need source quench packets may re-enable them 1604df285b3dSMike Silbersack * via the net.inet.ip.sendsourcequench sysctl. 1605df285b3dSMike Silbersack */ 1606603724d3SBjoern A. Zeeb if (V_ip_sendsourcequench == 0) { 1607df285b3dSMike Silbersack m_freem(mcopy); 16088c0fec80SRobert Watson if (ia != NULL) 16098c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 1610df285b3dSMike Silbersack return; 1611df285b3dSMike Silbersack } else { 1612df8bae1dSRodney W. Grimes type = ICMP_SOURCEQUENCH; 1613df8bae1dSRodney W. Grimes code = 0; 1614df285b3dSMike Silbersack } 1615df8bae1dSRodney W. Grimes break; 16163a06e3e0SRuslan Ermilov 16173a06e3e0SRuslan Ermilov case EACCES: /* ipfw denied packet */ 16183a06e3e0SRuslan Ermilov m_freem(mcopy); 16198c0fec80SRobert Watson if (ia != NULL) 16208c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 16213a06e3e0SRuslan Ermilov return; 1622df8bae1dSRodney W. Grimes } 16238c0fec80SRobert Watson if (ia != NULL) 16248c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 1625c773494eSAndre Oppermann icmp_error(mcopy, type, code, dest.s_addr, mtu); 1626df8bae1dSRodney W. Grimes } 1627df8bae1dSRodney W. Grimes 162882c23ebaSBill Fenner void 1629f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip, 1630f2565d68SRobert Watson struct mbuf *m) 163182c23ebaSBill Fenner { 16328b615593SMarko Zec 1633be8a62e8SPoul-Henning Kamp if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) { 1634be8a62e8SPoul-Henning Kamp struct bintime bt; 1635be8a62e8SPoul-Henning Kamp 1636be8a62e8SPoul-Henning Kamp bintime(&bt); 1637be8a62e8SPoul-Henning Kamp if (inp->inp_socket->so_options & SO_BINTIME) { 1638be8a62e8SPoul-Henning Kamp *mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt), 1639be8a62e8SPoul-Henning Kamp SCM_BINTIME, SOL_SOCKET); 1640be8a62e8SPoul-Henning Kamp if (*mp) 1641be8a62e8SPoul-Henning Kamp mp = &(*mp)->m_next; 1642be8a62e8SPoul-Henning Kamp } 164382c23ebaSBill Fenner if (inp->inp_socket->so_options & SO_TIMESTAMP) { 164482c23ebaSBill Fenner struct timeval tv; 164582c23ebaSBill Fenner 1646be8a62e8SPoul-Henning Kamp bintime2timeval(&bt, &tv); 164782c23ebaSBill Fenner *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 164882c23ebaSBill Fenner SCM_TIMESTAMP, SOL_SOCKET); 164982c23ebaSBill Fenner if (*mp) 165082c23ebaSBill Fenner mp = &(*mp)->m_next; 16514cc20ab1SSeigo Tanimura } 1652be8a62e8SPoul-Henning Kamp } 165382c23ebaSBill Fenner if (inp->inp_flags & INP_RECVDSTADDR) { 165482c23ebaSBill Fenner *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 165582c23ebaSBill Fenner sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 165682c23ebaSBill Fenner if (*mp) 165782c23ebaSBill Fenner mp = &(*mp)->m_next; 165882c23ebaSBill Fenner } 16594957466bSMatthew N. Dodd if (inp->inp_flags & INP_RECVTTL) { 16604957466bSMatthew N. Dodd *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl, 16614957466bSMatthew N. Dodd sizeof(u_char), IP_RECVTTL, IPPROTO_IP); 16624957466bSMatthew N. Dodd if (*mp) 16634957466bSMatthew N. Dodd mp = &(*mp)->m_next; 16644957466bSMatthew N. Dodd } 166582c23ebaSBill Fenner #ifdef notyet 166682c23ebaSBill Fenner /* XXX 166782c23ebaSBill Fenner * Moving these out of udp_input() made them even more broken 166882c23ebaSBill Fenner * than they already were. 166982c23ebaSBill Fenner */ 167082c23ebaSBill Fenner /* options were tossed already */ 167182c23ebaSBill Fenner if (inp->inp_flags & INP_RECVOPTS) { 167282c23ebaSBill Fenner *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 167382c23ebaSBill Fenner sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 167482c23ebaSBill Fenner if (*mp) 167582c23ebaSBill Fenner mp = &(*mp)->m_next; 167682c23ebaSBill Fenner } 167782c23ebaSBill Fenner /* ip_srcroute doesn't do what we want here, need to fix */ 167882c23ebaSBill Fenner if (inp->inp_flags & INP_RECVRETOPTS) { 1679e0982661SAndre Oppermann *mp = sbcreatecontrol((caddr_t) ip_srcroute(m), 168082c23ebaSBill Fenner sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 168182c23ebaSBill Fenner if (*mp) 168282c23ebaSBill Fenner mp = &(*mp)->m_next; 168382c23ebaSBill Fenner } 168482c23ebaSBill Fenner #endif 168582c23ebaSBill Fenner if (inp->inp_flags & INP_RECVIF) { 1686d314ad7bSJulian Elischer struct ifnet *ifp; 1687d314ad7bSJulian Elischer struct sdlbuf { 168882c23ebaSBill Fenner struct sockaddr_dl sdl; 1689d314ad7bSJulian Elischer u_char pad[32]; 1690d314ad7bSJulian Elischer } sdlbuf; 1691d314ad7bSJulian Elischer struct sockaddr_dl *sdp; 1692d314ad7bSJulian Elischer struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 169382c23ebaSBill Fenner 1694d314ad7bSJulian Elischer if (((ifp = m->m_pkthdr.rcvif)) 1695603724d3SBjoern A. Zeeb && ( ifp->if_index && (ifp->if_index <= V_if_index))) { 16964a0d6638SRuslan Ermilov sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr; 1697d314ad7bSJulian Elischer /* 1698d314ad7bSJulian Elischer * Change our mind and don't try copy. 1699d314ad7bSJulian Elischer */ 1700d314ad7bSJulian Elischer if ((sdp->sdl_family != AF_LINK) 1701d314ad7bSJulian Elischer || (sdp->sdl_len > sizeof(sdlbuf))) { 1702d314ad7bSJulian Elischer goto makedummy; 1703d314ad7bSJulian Elischer } 1704d314ad7bSJulian Elischer bcopy(sdp, sdl2, sdp->sdl_len); 1705d314ad7bSJulian Elischer } else { 1706d314ad7bSJulian Elischer makedummy: 1707d314ad7bSJulian Elischer sdl2->sdl_len 1708d314ad7bSJulian Elischer = offsetof(struct sockaddr_dl, sdl_data[0]); 1709d314ad7bSJulian Elischer sdl2->sdl_family = AF_LINK; 1710d314ad7bSJulian Elischer sdl2->sdl_index = 0; 1711d314ad7bSJulian Elischer sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 1712d314ad7bSJulian Elischer } 1713d314ad7bSJulian Elischer *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, 171482c23ebaSBill Fenner IP_RECVIF, IPPROTO_IP); 171582c23ebaSBill Fenner if (*mp) 171682c23ebaSBill Fenner mp = &(*mp)->m_next; 171782c23ebaSBill Fenner } 171882c23ebaSBill Fenner } 171982c23ebaSBill Fenner 17204d2e3692SLuigi Rizzo /* 172130916a2dSRobert Watson * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the 172230916a2dSRobert Watson * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on 172330916a2dSRobert Watson * locking. This code remains in ip_input.c as ip_mroute.c is optionally 172430916a2dSRobert Watson * compiled. 17254d2e3692SLuigi Rizzo */ 172682cea7e6SBjoern A. Zeeb static VNET_DEFINE(int, ip_rsvp_on); 172782cea7e6SBjoern A. Zeeb VNET_DEFINE(struct socket *, ip_rsvpd); 172882cea7e6SBjoern A. Zeeb 172982cea7e6SBjoern A. Zeeb #define V_ip_rsvp_on VNET(ip_rsvp_on) 173082cea7e6SBjoern A. Zeeb 1731df8bae1dSRodney W. Grimes int 1732f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so) 1733f0068c4aSGarrett Wollman { 17348b615593SMarko Zec 1735f0068c4aSGarrett Wollman if (so->so_type != SOCK_RAW || 1736f0068c4aSGarrett Wollman so->so_proto->pr_protocol != IPPROTO_RSVP) 1737f0068c4aSGarrett Wollman return EOPNOTSUPP; 1738f0068c4aSGarrett Wollman 1739603724d3SBjoern A. Zeeb if (V_ip_rsvpd != NULL) 1740f0068c4aSGarrett Wollman return EADDRINUSE; 1741f0068c4aSGarrett Wollman 1742603724d3SBjoern A. Zeeb V_ip_rsvpd = so; 17431c5de19aSGarrett Wollman /* 17441c5de19aSGarrett Wollman * This may seem silly, but we need to be sure we don't over-increment 17451c5de19aSGarrett Wollman * the RSVP counter, in case something slips up. 17461c5de19aSGarrett Wollman */ 1747603724d3SBjoern A. Zeeb if (!V_ip_rsvp_on) { 1748603724d3SBjoern A. Zeeb V_ip_rsvp_on = 1; 1749603724d3SBjoern A. Zeeb V_rsvp_on++; 17501c5de19aSGarrett Wollman } 1751f0068c4aSGarrett Wollman 1752f0068c4aSGarrett Wollman return 0; 1753f0068c4aSGarrett Wollman } 1754f0068c4aSGarrett Wollman 1755f0068c4aSGarrett Wollman int 1756f0068c4aSGarrett Wollman ip_rsvp_done(void) 1757f0068c4aSGarrett Wollman { 17588b615593SMarko Zec 1759603724d3SBjoern A. Zeeb V_ip_rsvpd = NULL; 17601c5de19aSGarrett Wollman /* 17611c5de19aSGarrett Wollman * This may seem silly, but we need to be sure we don't over-decrement 17621c5de19aSGarrett Wollman * the RSVP counter, in case something slips up. 17631c5de19aSGarrett Wollman */ 1764603724d3SBjoern A. Zeeb if (V_ip_rsvp_on) { 1765603724d3SBjoern A. Zeeb V_ip_rsvp_on = 0; 1766603724d3SBjoern A. Zeeb V_rsvp_on--; 17671c5de19aSGarrett Wollman } 1768f0068c4aSGarrett Wollman return 0; 1769f0068c4aSGarrett Wollman } 1770bbb4330bSLuigi Rizzo 1771bbb4330bSLuigi Rizzo void 1772bbb4330bSLuigi Rizzo rsvp_input(struct mbuf *m, int off) /* XXX must fixup manually */ 1773bbb4330bSLuigi Rizzo { 17748b615593SMarko Zec 1775bbb4330bSLuigi Rizzo if (rsvp_input_p) { /* call the real one if loaded */ 1776bbb4330bSLuigi Rizzo rsvp_input_p(m, off); 1777bbb4330bSLuigi Rizzo return; 1778bbb4330bSLuigi Rizzo } 1779bbb4330bSLuigi Rizzo 1780bbb4330bSLuigi Rizzo /* Can still get packets with rsvp_on = 0 if there is a local member 1781bbb4330bSLuigi Rizzo * of the group to which the RSVP packet is addressed. But in this 1782bbb4330bSLuigi Rizzo * case we want to throw the packet away. 1783bbb4330bSLuigi Rizzo */ 1784bbb4330bSLuigi Rizzo 1785603724d3SBjoern A. Zeeb if (!V_rsvp_on) { 1786bbb4330bSLuigi Rizzo m_freem(m); 1787bbb4330bSLuigi Rizzo return; 1788bbb4330bSLuigi Rizzo } 1789bbb4330bSLuigi Rizzo 1790603724d3SBjoern A. Zeeb if (V_ip_rsvpd != NULL) { 1791bbb4330bSLuigi Rizzo rip_input(m, off); 1792bbb4330bSLuigi Rizzo return; 1793bbb4330bSLuigi Rizzo } 1794bbb4330bSLuigi Rizzo /* Drop the packet */ 1795bbb4330bSLuigi Rizzo m_freem(m); 1796bbb4330bSLuigi Rizzo } 1797