1c398230bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 324b421e2dSMike Silbersack #include <sys/cdefs.h> 330ac40133SBrian Somers #include "opt_bootp.h" 34126f8248SMichael Tuexen #include "opt_inet.h" 3527108a15SDag-Erling Smørgrav #include "opt_ipstealth.h" 366a800098SYoshinobu Inoue #include "opt_ipsec.h" 3733553d6eSBjoern A. Zeeb #include "opt_route.h" 38b8bc95cdSAdrian Chadd #include "opt_rss.h" 3978b1fc05SGleb Smirnoff #include "opt_sctp.h" 4074a9466cSGary Palmer 41df8bae1dSRodney W. Grimes #include <sys/param.h> 42df8bae1dSRodney W. Grimes #include <sys/systm.h> 43ef91a976SAndrey V. Elsukov #include <sys/hhook.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> 52cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h> 53385195c0SMarko Zec #include <sys/rwlock.h> 5457f60867SMark Johnston #include <sys/sdt.h> 551025071fSGarrett Wollman #include <sys/syslog.h> 56b5e8ce9fSBruce Evans #include <sys/sysctl.h> 57df8bae1dSRodney W. Grimes 58df8bae1dSRodney W. Grimes #include <net/if.h> 599494d596SBrooks Davis #include <net/if_types.h> 60d314ad7bSJulian Elischer #include <net/if_var.h> 6182c23ebaSBill Fenner #include <net/if_dl.h> 623d0d5b21SJustin Hibbits #include <net/if_private.h> 63b252313fSGleb Smirnoff #include <net/pfil.h> 64df8bae1dSRodney W. Grimes #include <net/route.h> 65983066f0SAlexander V. Chernikov #include <net/route/nhop.h> 66748e0b0aSGarrett Wollman #include <net/netisr.h> 67b2bdc62aSAdrian Chadd #include <net/rss_config.h> 684b79449eSBjoern A. Zeeb #include <net/vnet.h> 69df8bae1dSRodney W. Grimes 70df8bae1dSRodney W. Grimes #include <netinet/in.h> 7157f60867SMark Johnston #include <netinet/in_kdtrace.h> 72df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 73b5e8ce9fSBruce Evans #include <netinet/in_var.h> 74df8bae1dSRodney W. Grimes #include <netinet/ip.h> 75983066f0SAlexander V. Chernikov #include <netinet/in_fib.h> 76df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 77df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 7878b1fc05SGleb Smirnoff #include <netinet/ip_encap.h> 79eddfbb76SRobert Watson #include <netinet/ip_fw.h> 80df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h> 8178b1fc05SGleb Smirnoff #include <netinet/igmp_var.h> 82ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 8358938916SGarrett Wollman #include <machine/in_cksum.h> 84a9771948SGleb Smirnoff #include <netinet/ip_carp.h> 85b8bc95cdSAdrian Chadd #include <netinet/in_rss.h> 8678b1fc05SGleb Smirnoff #ifdef SCTP 8778b1fc05SGleb Smirnoff #include <netinet/sctp_var.h> 8878b1fc05SGleb Smirnoff #endif 89df8bae1dSRodney W. Grimes 90fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 91fcf59617SAndrey V. Elsukov 92f0068c4aSGarrett Wollman #include <sys/socketvar.h> 936ddbf1e2SGary Palmer 94aed55708SRobert Watson #include <security/mac/mac_framework.h> 95aed55708SRobert Watson 96d2035ffbSEd Maste #ifdef CTASSERT 97d2035ffbSEd Maste CTASSERT(sizeof(struct ip) == 20); 98d2035ffbSEd Maste #endif 99d2035ffbSEd Maste 1001dbefcc0SGleb Smirnoff /* IP reassembly functions are defined in ip_reass.c. */ 101843b0e57SXin LI extern void ipreass_init(void); 102aea0cd04SGleb Smirnoff extern void ipreass_vnet_init(void); 1031dbefcc0SGleb Smirnoff #ifdef VIMAGE 104843b0e57SXin LI extern void ipreass_destroy(void); 1051dbefcc0SGleb Smirnoff #endif 1061dbefcc0SGleb Smirnoff 10782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, rsvp_on); 10882cea7e6SBjoern A. Zeeb 10982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ipforwarding); 1106df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_VNET | CTLFLAG_RW, 111eddfbb76SRobert Watson &VNET_NAME(ipforwarding), 0, 1128b615593SMarko Zec "Enable IP forwarding between interfaces"); 1130312fbe9SPoul-Henning Kamp 1148ad114c0SGeorge V. Neville-Neil /* 1158ad114c0SGeorge V. Neville-Neil * Respond with an ICMP host redirect when we forward a packet out of 1168ad114c0SGeorge V. Neville-Neil * the same interface on which it was received. See RFC 792. 1178ad114c0SGeorge V. Neville-Neil */ 1188ad114c0SGeorge V. Neville-Neil VNET_DEFINE(int, ipsendredirects) = 1; 1196df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_VNET | CTLFLAG_RW, 120eddfbb76SRobert Watson &VNET_NAME(ipsendredirects), 0, 1218b615593SMarko Zec "Enable sending IP redirects"); 1220312fbe9SPoul-Henning Kamp 12394df3271SGleb Smirnoff VNET_DEFINE_STATIC(bool, ip_strong_es) = false; 12494df3271SGleb Smirnoff #define V_ip_strong_es VNET(ip_strong_es) 12594df3271SGleb Smirnoff SYSCTL_BOOL(_net_inet_ip, OID_AUTO, rfc1122_strong_es, 12694df3271SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_strong_es), false, 12794df3271SGleb Smirnoff "Packet's IP destination address must match address on arrival interface"); 128b3e95d4eSJonathan Lemon 1292ce85919SGleb Smirnoff VNET_DEFINE_STATIC(bool, ip_sav) = true; 1302ce85919SGleb Smirnoff #define V_ip_sav VNET(ip_sav) 1312ce85919SGleb Smirnoff SYSCTL_BOOL(_net_inet_ip, OID_AUTO, source_address_validation, 1322ce85919SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_sav), true, 1332ce85919SGleb Smirnoff "Drop incoming packets with source address that is a local address"); 1342ce85919SGleb Smirnoff 1355ab15157SDoug Rabson /* Packet filter hooks */ 1365ab15157SDoug Rabson VNET_DEFINE(pfil_head_t, inet_pfil_head); 1375ab15157SDoug Rabson VNET_DEFINE(pfil_head_t, inet_local_pfil_head); 138df8bae1dSRodney W. Grimes 139d4b5cae4SRobert Watson static struct netisr_handler ip_nh = { 140d4b5cae4SRobert Watson .nh_name = "ip", 141d4b5cae4SRobert Watson .nh_handler = ip_input, 142d4b5cae4SRobert Watson .nh_proto = NETISR_IP, 143b8bc95cdSAdrian Chadd #ifdef RSS 1442527ccadSAdrian Chadd .nh_m2cpuid = rss_soft_m2cpuid_v4, 145b8bc95cdSAdrian Chadd .nh_policy = NETISR_POLICY_CPU, 146b8bc95cdSAdrian Chadd .nh_dispatch = NETISR_DISPATCH_HYBRID, 147b8bc95cdSAdrian Chadd #else 148d4b5cae4SRobert Watson .nh_policy = NETISR_POLICY_FLOW, 149b8bc95cdSAdrian Chadd #endif 150d4b5cae4SRobert Watson }; 151ca925d9cSJonathan Lemon 152b8bc95cdSAdrian Chadd #ifdef RSS 153b8bc95cdSAdrian Chadd /* 154b8bc95cdSAdrian Chadd * Directly dispatched frames are currently assumed 155b8bc95cdSAdrian Chadd * to have a flowid already calculated. 156b8bc95cdSAdrian Chadd * 157b8bc95cdSAdrian Chadd * It should likely have something that assert it 158b8bc95cdSAdrian Chadd * actually has valid flow details. 159b8bc95cdSAdrian Chadd */ 160b8bc95cdSAdrian Chadd static struct netisr_handler ip_direct_nh = { 161b8bc95cdSAdrian Chadd .nh_name = "ip_direct", 162b8bc95cdSAdrian Chadd .nh_handler = ip_direct_input, 163b8bc95cdSAdrian Chadd .nh_proto = NETISR_IP_DIRECT, 164499baf0aSAdrian Chadd .nh_m2cpuid = rss_soft_m2cpuid_v4, 165b8bc95cdSAdrian Chadd .nh_policy = NETISR_POLICY_CPU, 166b8bc95cdSAdrian Chadd .nh_dispatch = NETISR_DISPATCH_HYBRID, 167b8bc95cdSAdrian Chadd }; 168b8bc95cdSAdrian Chadd #endif 169b8bc95cdSAdrian Chadd 17078b1fc05SGleb Smirnoff ipproto_input_t *ip_protox[IPPROTO_MAX] = { 17178b1fc05SGleb Smirnoff [0 ... IPPROTO_MAX - 1] = rip_input }; 17278b1fc05SGleb Smirnoff ipproto_ctlinput_t *ip_ctlprotox[IPPROTO_MAX] = { 17378b1fc05SGleb Smirnoff [0 ... IPPROTO_MAX - 1] = rip_ctlinput }; 17478b1fc05SGleb Smirnoff 17582cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead); /* first inet address */ 17682cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); /* inet addr hash table */ 17782cea7e6SBjoern A. Zeeb VNET_DEFINE(u_long, in_ifaddrhmask); /* mask for hash table */ 178ca925d9cSJonathan Lemon 179c8ee75f2SGleb Smirnoff /* Make sure it is safe to use hashinit(9) on CK_LIST. */ 180c8ee75f2SGleb Smirnoff CTASSERT(sizeof(struct in_ifaddrhashhead) == sizeof(LIST_HEAD(, in_addr))); 181c8ee75f2SGleb Smirnoff 1820312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU 1830312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, 1843d177f46SBill Fumerola &ip_mtu, 0, "Default MTU"); 1850312fbe9SPoul-Henning Kamp #endif 1860312fbe9SPoul-Henning Kamp 1871b968362SDag-Erling Smørgrav #ifdef IPSTEALTH 18882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ipstealth); 1896df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_VNET | CTLFLAG_RW, 190eddfbb76SRobert Watson &VNET_NAME(ipstealth), 0, 191eddfbb76SRobert Watson "IP stealth mode, no TTL decrementation on forwarding"); 1921b968362SDag-Erling Smørgrav #endif 193eddfbb76SRobert Watson 194315e3e38SRobert Watson /* 1955da0521fSAndrey V. Elsukov * IP statistics are stored in the "array" of counter(9)s. 1965923c293SGleb Smirnoff */ 1975da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct ipstat, ipstat); 1985da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(ipstat); 1995da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_ip, IPCTL_STATS, stats, struct ipstat, ipstat, 2005da0521fSAndrey V. Elsukov "IP statistics (struct ipstat, netinet/ip_var.h)"); 2015923c293SGleb Smirnoff 2025923c293SGleb Smirnoff #ifdef VIMAGE 2035da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(ipstat); 2045923c293SGleb Smirnoff #endif /* VIMAGE */ 2055923c293SGleb Smirnoff 2065923c293SGleb Smirnoff /* 207315e3e38SRobert Watson * Kernel module interface for updating ipstat. The argument is an index 2085923c293SGleb Smirnoff * into ipstat treated as an array. 209315e3e38SRobert Watson */ 210315e3e38SRobert Watson void 211315e3e38SRobert Watson kmod_ipstat_inc(int statnum) 212315e3e38SRobert Watson { 213315e3e38SRobert Watson 2145da0521fSAndrey V. Elsukov counter_u64_add(VNET(ipstat)[statnum], 1); 215315e3e38SRobert Watson } 216315e3e38SRobert Watson 217315e3e38SRobert Watson void 218315e3e38SRobert Watson kmod_ipstat_dec(int statnum) 219315e3e38SRobert Watson { 220315e3e38SRobert Watson 2215da0521fSAndrey V. Elsukov counter_u64_add(VNET(ipstat)[statnum], -1); 222315e3e38SRobert Watson } 223315e3e38SRobert Watson 224d4b5cae4SRobert Watson static int 225d4b5cae4SRobert Watson sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS) 226d4b5cae4SRobert Watson { 227d4b5cae4SRobert Watson int error, qlimit; 228d4b5cae4SRobert Watson 229d4b5cae4SRobert Watson netisr_getqlimit(&ip_nh, &qlimit); 230d4b5cae4SRobert Watson error = sysctl_handle_int(oidp, &qlimit, 0, req); 231d4b5cae4SRobert Watson if (error || !req->newptr) 232d4b5cae4SRobert Watson return (error); 233d4b5cae4SRobert Watson if (qlimit < 1) 234d4b5cae4SRobert Watson return (EINVAL); 235d4b5cae4SRobert Watson return (netisr_setqlimit(&ip_nh, qlimit)); 236d4b5cae4SRobert Watson } 237d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, 2387029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0, 2397029da5cSPawel Biernacki sysctl_netinet_intr_queue_maxlen, "I", 240d4b5cae4SRobert Watson "Maximum size of the IP input queue"); 241d4b5cae4SRobert Watson 242d4b5cae4SRobert Watson static int 243d4b5cae4SRobert Watson sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS) 244d4b5cae4SRobert Watson { 245d4b5cae4SRobert Watson u_int64_t qdrops_long; 246d4b5cae4SRobert Watson int error, qdrops; 247d4b5cae4SRobert Watson 248d4b5cae4SRobert Watson netisr_getqdrops(&ip_nh, &qdrops_long); 249d4b5cae4SRobert Watson qdrops = qdrops_long; 250d4b5cae4SRobert Watson error = sysctl_handle_int(oidp, &qdrops, 0, req); 251d4b5cae4SRobert Watson if (error || !req->newptr) 252d4b5cae4SRobert Watson return (error); 253d4b5cae4SRobert Watson if (qdrops != 0) 254d4b5cae4SRobert Watson return (EINVAL); 255d4b5cae4SRobert Watson netisr_clearqdrops(&ip_nh); 256d4b5cae4SRobert Watson return (0); 257d4b5cae4SRobert Watson } 258d4b5cae4SRobert Watson 259d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, 2607029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 2617029da5cSPawel Biernacki 0, 0, sysctl_netinet_intr_queue_drops, "I", 262d4b5cae4SRobert Watson "Number of packets dropped from the IP input queue"); 263d4b5cae4SRobert Watson 264b8bc95cdSAdrian Chadd #ifdef RSS 265b8bc95cdSAdrian Chadd static int 266b8bc95cdSAdrian Chadd sysctl_netinet_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS) 267b8bc95cdSAdrian Chadd { 268b8bc95cdSAdrian Chadd int error, qlimit; 269b8bc95cdSAdrian Chadd 270b8bc95cdSAdrian Chadd netisr_getqlimit(&ip_direct_nh, &qlimit); 271b8bc95cdSAdrian Chadd error = sysctl_handle_int(oidp, &qlimit, 0, req); 272b8bc95cdSAdrian Chadd if (error || !req->newptr) 273b8bc95cdSAdrian Chadd return (error); 274b8bc95cdSAdrian Chadd if (qlimit < 1) 275b8bc95cdSAdrian Chadd return (EINVAL); 276b8bc95cdSAdrian Chadd return (netisr_setqlimit(&ip_direct_nh, qlimit)); 277b8bc95cdSAdrian Chadd } 2787faa0d21SAndrey V. Elsukov SYSCTL_PROC(_net_inet_ip, IPCTL_INTRDQMAXLEN, intr_direct_queue_maxlen, 2797029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 2807029da5cSPawel Biernacki 0, 0, sysctl_netinet_intr_direct_queue_maxlen, 2817faa0d21SAndrey V. Elsukov "I", "Maximum size of the IP direct input queue"); 282b8bc95cdSAdrian Chadd 283b8bc95cdSAdrian Chadd static int 284b8bc95cdSAdrian Chadd sysctl_netinet_intr_direct_queue_drops(SYSCTL_HANDLER_ARGS) 285b8bc95cdSAdrian Chadd { 286b8bc95cdSAdrian Chadd u_int64_t qdrops_long; 287b8bc95cdSAdrian Chadd int error, qdrops; 288b8bc95cdSAdrian Chadd 289b8bc95cdSAdrian Chadd netisr_getqdrops(&ip_direct_nh, &qdrops_long); 290b8bc95cdSAdrian Chadd qdrops = qdrops_long; 291b8bc95cdSAdrian Chadd error = sysctl_handle_int(oidp, &qdrops, 0, req); 292b8bc95cdSAdrian Chadd if (error || !req->newptr) 293b8bc95cdSAdrian Chadd return (error); 294b8bc95cdSAdrian Chadd if (qdrops != 0) 295b8bc95cdSAdrian Chadd return (EINVAL); 296b8bc95cdSAdrian Chadd netisr_clearqdrops(&ip_direct_nh); 297b8bc95cdSAdrian Chadd return (0); 298b8bc95cdSAdrian Chadd } 299b8bc95cdSAdrian Chadd 3007faa0d21SAndrey V. Elsukov SYSCTL_PROC(_net_inet_ip, IPCTL_INTRDQDROPS, intr_direct_queue_drops, 3017029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0, 3027029da5cSPawel Biernacki sysctl_netinet_intr_direct_queue_drops, "I", 303b8bc95cdSAdrian Chadd "Number of packets dropped from the IP direct input queue"); 304b8bc95cdSAdrian Chadd #endif /* RSS */ 305b8bc95cdSAdrian Chadd 306df8bae1dSRodney W. Grimes /* 307df8bae1dSRodney W. Grimes * IP initialization: fill in IP protocol switch table. 308df8bae1dSRodney W. Grimes * All protocols not implemented in kernel go to raw IP protocol handler. 309df8bae1dSRodney W. Grimes */ 31089128ff3SGleb Smirnoff static void 31189128ff3SGleb Smirnoff ip_vnet_init(void *arg __unused) 312df8bae1dSRodney W. Grimes { 313b252313fSGleb Smirnoff struct pfil_head_args args; 314df8bae1dSRodney W. Grimes 315d7c5a620SMatt Macy CK_STAILQ_INIT(&V_in_ifaddrhead); 316603724d3SBjoern A. Zeeb V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask); 3171ed81b73SMarko Zec 3181ed81b73SMarko Zec /* Initialize IP reassembly queue. */ 319aea0cd04SGleb Smirnoff ipreass_vnet_init(); 3201ed81b73SMarko Zec 3210b4b0b0fSJulian Elischer /* Initialize packet filter hooks. */ 322b252313fSGleb Smirnoff args.pa_version = PFIL_VERSION; 323b252313fSGleb Smirnoff args.pa_flags = PFIL_IN | PFIL_OUT; 324b252313fSGleb Smirnoff args.pa_type = PFIL_TYPE_IP4; 325b252313fSGleb Smirnoff args.pa_headname = PFIL_INET_NAME; 326b252313fSGleb Smirnoff V_inet_pfil_head = pfil_head_register(&args); 3270b4b0b0fSJulian Elischer 3285ab15157SDoug Rabson args.pa_flags = PFIL_OUT; 3295ab15157SDoug Rabson args.pa_headname = PFIL_INET_LOCAL_NAME; 3305ab15157SDoug Rabson V_inet_local_pfil_head = pfil_head_register(&args); 3315ab15157SDoug Rabson 332ef91a976SAndrey V. Elsukov if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET, 333ef91a976SAndrey V. Elsukov &V_ipsec_hhh_in[HHOOK_IPSEC_INET], 334ef91a976SAndrey V. Elsukov HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0) 335ef91a976SAndrey V. Elsukov printf("%s: WARNING: unable to register input helper hook\n", 336ef91a976SAndrey V. Elsukov __func__); 337ef91a976SAndrey V. Elsukov if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET, 338ef91a976SAndrey V. Elsukov &V_ipsec_hhh_out[HHOOK_IPSEC_INET], 339ef91a976SAndrey V. Elsukov HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0) 340ef91a976SAndrey V. Elsukov printf("%s: WARNING: unable to register output helper hook\n", 341ef91a976SAndrey V. Elsukov __func__); 342ef91a976SAndrey V. Elsukov 343484149deSBjoern A. Zeeb #ifdef VIMAGE 344484149deSBjoern A. Zeeb netisr_register_vnet(&ip_nh); 345484149deSBjoern A. Zeeb #ifdef RSS 346484149deSBjoern A. Zeeb netisr_register_vnet(&ip_direct_nh); 347484149deSBjoern A. Zeeb #endif 348484149deSBjoern A. Zeeb #endif 34989128ff3SGleb Smirnoff } 35089128ff3SGleb Smirnoff VNET_SYSINIT(ip_vnet_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, 35189128ff3SGleb Smirnoff ip_vnet_init, NULL); 35289128ff3SGleb Smirnoff 35389128ff3SGleb Smirnoff static void 35489128ff3SGleb Smirnoff ip_init(const void *unused __unused) 35589128ff3SGleb Smirnoff { 3561ed81b73SMarko Zec 357aea0cd04SGleb Smirnoff ipreass_init(); 358aea0cd04SGleb Smirnoff 359db09bef3SAndre Oppermann /* 36078b1fc05SGleb Smirnoff * Register statically compiled protocols, that are unlikely to 36178b1fc05SGleb Smirnoff * ever become dynamic. 362db09bef3SAndre Oppermann */ 36378b1fc05SGleb Smirnoff IPPROTO_REGISTER(IPPROTO_ICMP, icmp_input, NULL); 36478b1fc05SGleb Smirnoff IPPROTO_REGISTER(IPPROTO_IGMP, igmp_input, NULL); 36578b1fc05SGleb Smirnoff IPPROTO_REGISTER(IPPROTO_RSVP, rsvp_input, NULL); 36678b1fc05SGleb Smirnoff IPPROTO_REGISTER(IPPROTO_IPV4, encap4_input, NULL); 36778b1fc05SGleb Smirnoff IPPROTO_REGISTER(IPPROTO_MOBILE, encap4_input, NULL); 36878b1fc05SGleb Smirnoff IPPROTO_REGISTER(IPPROTO_ETHERIP, encap4_input, NULL); 36978b1fc05SGleb Smirnoff IPPROTO_REGISTER(IPPROTO_GRE, encap4_input, NULL); 37078b1fc05SGleb Smirnoff IPPROTO_REGISTER(IPPROTO_IPV6, encap4_input, NULL); 37178b1fc05SGleb Smirnoff IPPROTO_REGISTER(IPPROTO_PIM, encap4_input, NULL); 37278b1fc05SGleb Smirnoff #ifdef SCTP /* XXX: has a loadable & static version */ 37378b1fc05SGleb Smirnoff IPPROTO_REGISTER(IPPROTO_SCTP, sctp_input, sctp_ctlinput); 37478b1fc05SGleb Smirnoff #endif 375194a213eSAndrey A. Chernov 376d4b5cae4SRobert Watson netisr_register(&ip_nh); 377b8bc95cdSAdrian Chadd #ifdef RSS 378b8bc95cdSAdrian Chadd netisr_register(&ip_direct_nh); 379b8bc95cdSAdrian Chadd #endif 380df8bae1dSRodney W. Grimes } 38189128ff3SGleb Smirnoff SYSINIT(ip_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip_init, NULL); 382df8bae1dSRodney W. Grimes 3839802380eSBjoern A. Zeeb #ifdef VIMAGE 3843f58662dSBjoern A. Zeeb static void 3853f58662dSBjoern A. Zeeb ip_destroy(void *unused __unused) 3869802380eSBjoern A. Zeeb { 387ef91a976SAndrey V. Elsukov int error; 3884d3dfd45SMikolaj Golub 389484149deSBjoern A. Zeeb #ifdef RSS 390484149deSBjoern A. Zeeb netisr_unregister_vnet(&ip_direct_nh); 391484149deSBjoern A. Zeeb #endif 392484149deSBjoern A. Zeeb netisr_unregister_vnet(&ip_nh); 393484149deSBjoern A. Zeeb 394b252313fSGleb Smirnoff pfil_head_unregister(V_inet_pfil_head); 395ef91a976SAndrey V. Elsukov error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET]); 396ef91a976SAndrey V. Elsukov if (error != 0) { 397ef91a976SAndrey V. Elsukov printf("%s: WARNING: unable to deregister input helper hook " 398ef91a976SAndrey V. Elsukov "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET: " 399ef91a976SAndrey V. Elsukov "error %d returned\n", __func__, error); 400ef91a976SAndrey V. Elsukov } 401ef91a976SAndrey V. Elsukov error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET]); 402ef91a976SAndrey V. Elsukov if (error != 0) { 403ef91a976SAndrey V. Elsukov printf("%s: WARNING: unable to deregister output helper hook " 404ef91a976SAndrey V. Elsukov "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET: " 405ef91a976SAndrey V. Elsukov "error %d returned\n", __func__, error); 406ef91a976SAndrey V. Elsukov } 40789856f7eSBjoern A. Zeeb 40889856f7eSBjoern A. Zeeb /* Remove the IPv4 addresses from all interfaces. */ 40989856f7eSBjoern A. Zeeb in_ifscrub_all(); 41089856f7eSBjoern A. Zeeb 41189856f7eSBjoern A. Zeeb /* Make sure the IPv4 routes are gone as well. */ 412b1d63265SAlexander V. Chernikov rib_flush_routes_family(AF_INET); 4139802380eSBjoern A. Zeeb 414e3c2c634SGleb Smirnoff /* Destroy IP reassembly queue. */ 4151dbefcc0SGleb Smirnoff ipreass_destroy(); 41689856f7eSBjoern A. Zeeb 41789856f7eSBjoern A. Zeeb /* Cleanup in_ifaddr hash table; should be empty. */ 41889856f7eSBjoern A. Zeeb hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask); 4199802380eSBjoern A. Zeeb } 4203f58662dSBjoern A. Zeeb 4213f58662dSBjoern A. Zeeb VNET_SYSUNINIT(ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip_destroy, NULL); 4229802380eSBjoern A. Zeeb #endif 4239802380eSBjoern A. Zeeb 424b8bc95cdSAdrian Chadd #ifdef RSS 425b8bc95cdSAdrian Chadd /* 426b8bc95cdSAdrian Chadd * IP direct input routine. 427b8bc95cdSAdrian Chadd * 428b8bc95cdSAdrian Chadd * This is called when reinjecting completed fragments where 429b8bc95cdSAdrian Chadd * all of the previous checking and book-keeping has been done. 430b8bc95cdSAdrian Chadd */ 431b8bc95cdSAdrian Chadd void 432b8bc95cdSAdrian Chadd ip_direct_input(struct mbuf *m) 433b8bc95cdSAdrian Chadd { 434b8bc95cdSAdrian Chadd struct ip *ip; 435b8bc95cdSAdrian Chadd int hlen; 436b8bc95cdSAdrian Chadd 437b8bc95cdSAdrian Chadd ip = mtod(m, struct ip *); 438b8bc95cdSAdrian Chadd hlen = ip->ip_hl << 2; 439b8bc95cdSAdrian Chadd 440fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 441fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) { 442fcf59617SAndrey V. Elsukov if (IPSEC_INPUT(ipv4, m, hlen, ip->ip_p) != 0) 443fcf59617SAndrey V. Elsukov return; 444fcf59617SAndrey V. Elsukov } 445fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 446b8bc95cdSAdrian Chadd IPSTAT_INC(ips_delivered); 44778b1fc05SGleb Smirnoff ip_protox[ip->ip_p](&m, &hlen, ip->ip_p); 448b8bc95cdSAdrian Chadd } 449b8bc95cdSAdrian Chadd #endif 450b8bc95cdSAdrian Chadd 4514d2e3692SLuigi Rizzo /* 452df8bae1dSRodney W. Grimes * Ip input routine. Checksum and byte swap header. If fragmented 453df8bae1dSRodney W. Grimes * try to reassemble. Process options. Pass to next level. 454df8bae1dSRodney W. Grimes */ 455c67b1d17SGarrett Wollman void 456c67b1d17SGarrett Wollman ip_input(struct mbuf *m) 457df8bae1dSRodney W. Grimes { 4589188b4a1SAndre Oppermann struct ip *ip = NULL; 4595da9f8faSJosef Karthauser struct in_ifaddr *ia = NULL; 460ca925d9cSJonathan Lemon struct ifaddr *ifa; 4610aade26eSRobert Watson struct ifnet *ifp; 46294df3271SGleb Smirnoff int hlen = 0; 46321d172a3SGleb Smirnoff uint16_t sum, ip_len; 46402c1c707SAndre Oppermann int dchg = 0; /* dest changed after fw */ 465f51f805fSSam Leffler struct in_addr odst; /* original dst address */ 46694df3271SGleb Smirnoff bool strong_es; 467b715f178SLuigi Rizzo 468fe584538SDag-Erling Smørgrav M_ASSERTPKTHDR(m); 469b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 470db40007dSAndrew R. Reiter 471ac9d7e26SMax Laier if (m->m_flags & M_FASTFWD_OURS) { 47276ff6dcfSAndre Oppermann m->m_flags &= ~M_FASTFWD_OURS; 47376ff6dcfSAndre Oppermann /* Set up some basics that will be used later. */ 4742b25acc1SLuigi Rizzo ip = mtod(m, struct ip *); 47553be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 4768f134647SGleb Smirnoff ip_len = ntohs(ip->ip_len); 4779b932e9eSAndre Oppermann goto ours; 4782b25acc1SLuigi Rizzo } 4792b25acc1SLuigi Rizzo 48086425c62SRobert Watson IPSTAT_INC(ips_total); 48158938916SGarrett Wollman 4820359e7a5SMateusz Guzik if (__predict_false(m->m_pkthdr.len < sizeof(struct ip))) 48358938916SGarrett Wollman goto tooshort; 48458938916SGarrett Wollman 4850359e7a5SMateusz Guzik if (m->m_len < sizeof(struct ip)) { 4860359e7a5SMateusz Guzik m = m_pullup(m, sizeof(struct ip)); 4870359e7a5SMateusz Guzik if (__predict_false(m == NULL)) { 48886425c62SRobert Watson IPSTAT_INC(ips_toosmall); 489c67b1d17SGarrett Wollman return; 490df8bae1dSRodney W. Grimes } 4910359e7a5SMateusz Guzik } 492df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 49358938916SGarrett Wollman 4940359e7a5SMateusz Guzik if (__predict_false(ip->ip_v != IPVERSION)) { 49586425c62SRobert Watson IPSTAT_INC(ips_badvers); 496df8bae1dSRodney W. Grimes goto bad; 497df8bae1dSRodney W. Grimes } 49858938916SGarrett Wollman 49953be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 5000359e7a5SMateusz Guzik if (__predict_false(hlen < sizeof(struct ip))) { /* minimum header length */ 50186425c62SRobert Watson IPSTAT_INC(ips_badhlen); 502df8bae1dSRodney W. Grimes goto bad; 503df8bae1dSRodney W. Grimes } 504df8bae1dSRodney W. Grimes if (hlen > m->m_len) { 5050359e7a5SMateusz Guzik m = m_pullup(m, hlen); 5060359e7a5SMateusz Guzik if (__predict_false(m == NULL)) { 50786425c62SRobert Watson IPSTAT_INC(ips_badhlen); 508c67b1d17SGarrett Wollman return; 509df8bae1dSRodney W. Grimes } 510df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 511df8bae1dSRodney W. Grimes } 51233841545SHajimu UMEMOTO 51357f60867SMark Johnston IP_PROBE(receive, NULL, NULL, ip, m->m_pkthdr.rcvif, ip, NULL); 51457f60867SMark Johnston 5156c1c6ae5SRodney W. Grimes /* IN_LOOPBACK must not appear on the wire - RFC1122 */ 5160aade26eSRobert Watson ifp = m->m_pkthdr.rcvif; 5176c1c6ae5SRodney W. Grimes if (IN_LOOPBACK(ntohl(ip->ip_dst.s_addr)) || 5186c1c6ae5SRodney W. Grimes IN_LOOPBACK(ntohl(ip->ip_src.s_addr))) { 5190aade26eSRobert Watson if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 52086425c62SRobert Watson IPSTAT_INC(ips_badaddr); 52133841545SHajimu UMEMOTO goto bad; 52233841545SHajimu UMEMOTO } 52333841545SHajimu UMEMOTO } 52433841545SHajimu UMEMOTO 525db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 526db4f9cc7SJonathan Lemon sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 527db4f9cc7SJonathan Lemon } else { 52858938916SGarrett Wollman if (hlen == sizeof(struct ip)) { 52947c861ecSBrian Somers sum = in_cksum_hdr(ip); 53058938916SGarrett Wollman } else { 53147c861ecSBrian Somers sum = in_cksum(m, hlen); 53258938916SGarrett Wollman } 533db4f9cc7SJonathan Lemon } 5340359e7a5SMateusz Guzik if (__predict_false(sum)) { 53586425c62SRobert Watson IPSTAT_INC(ips_badsum); 536df8bae1dSRodney W. Grimes goto bad; 537df8bae1dSRodney W. Grimes } 538df8bae1dSRodney W. Grimes 53921d172a3SGleb Smirnoff ip_len = ntohs(ip->ip_len); 5400359e7a5SMateusz Guzik if (__predict_false(ip_len < hlen)) { 54186425c62SRobert Watson IPSTAT_INC(ips_badlen); 542df8bae1dSRodney W. Grimes goto bad; 543df8bae1dSRodney W. Grimes } 544df8bae1dSRodney W. Grimes 545df8bae1dSRodney W. Grimes /* 546df8bae1dSRodney W. Grimes * Check that the amount of data in the buffers 547df8bae1dSRodney W. Grimes * is as at least much as the IP header would have us expect. 548df8bae1dSRodney W. Grimes * Trim mbufs if longer than we expect. 549df8bae1dSRodney W. Grimes * Drop packet if shorter than we expect. 550df8bae1dSRodney W. Grimes */ 5510359e7a5SMateusz Guzik if (__predict_false(m->m_pkthdr.len < ip_len)) { 55258938916SGarrett Wollman tooshort: 55386425c62SRobert Watson IPSTAT_INC(ips_tooshort); 554df8bae1dSRodney W. Grimes goto bad; 555df8bae1dSRodney W. Grimes } 55621d172a3SGleb Smirnoff if (m->m_pkthdr.len > ip_len) { 557df8bae1dSRodney W. Grimes if (m->m_len == m->m_pkthdr.len) { 55821d172a3SGleb Smirnoff m->m_len = ip_len; 55921d172a3SGleb Smirnoff m->m_pkthdr.len = ip_len; 560df8bae1dSRodney W. Grimes } else 56121d172a3SGleb Smirnoff m_adj(m, ip_len - m->m_pkthdr.len); 562df8bae1dSRodney W. Grimes } 563b8bc95cdSAdrian Chadd 564ad9f4d6aSAndrey V. Elsukov /* 565ad9f4d6aSAndrey V. Elsukov * Try to forward the packet, but if we fail continue. 566f389439fSBjoern A. Zeeb * ip_tryforward() may generate redirects these days. 567f389439fSBjoern A. Zeeb * XXX the logic below falling through to normal processing 568f389439fSBjoern A. Zeeb * if redirects are required should be revisited as well. 569ad9f4d6aSAndrey V. Elsukov * ip_tryforward() does inbound and outbound packet firewall 570ad9f4d6aSAndrey V. Elsukov * processing. If firewall has decided that destination becomes 571ad9f4d6aSAndrey V. Elsukov * our local address, it sets M_FASTFWD_OURS flag. In this 572ad9f4d6aSAndrey V. Elsukov * case skip another inbound firewall processing and update 573ad9f4d6aSAndrey V. Elsukov * ip pointer. 574ad9f4d6aSAndrey V. Elsukov */ 5758ad114c0SGeorge V. Neville-Neil if (V_ipforwarding != 0 576fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 577fcf59617SAndrey V. Elsukov && (!IPSEC_ENABLED(ipv4) || 578fcf59617SAndrey V. Elsukov IPSEC_CAPS(ipv4, m, IPSEC_CAP_OPERABLE) == 0) 579ad9f4d6aSAndrey V. Elsukov #endif 580ad9f4d6aSAndrey V. Elsukov ) { 581f389439fSBjoern A. Zeeb /* 582f389439fSBjoern A. Zeeb * ip_dooptions() was run so we can ignore the source route (or 583f389439fSBjoern A. Zeeb * any IP options case) case for redirects in ip_tryforward(). 584f389439fSBjoern A. Zeeb */ 585ad9f4d6aSAndrey V. Elsukov if ((m = ip_tryforward(m)) == NULL) 58633872124SGeorge V. Neville-Neil return; 587ad9f4d6aSAndrey V. Elsukov if (m->m_flags & M_FASTFWD_OURS) { 588ad9f4d6aSAndrey V. Elsukov m->m_flags &= ~M_FASTFWD_OURS; 589ad9f4d6aSAndrey V. Elsukov ip = mtod(m, struct ip *); 590ad9f4d6aSAndrey V. Elsukov goto ours; 591ad9f4d6aSAndrey V. Elsukov } 592ad9f4d6aSAndrey V. Elsukov } 593fcf59617SAndrey V. Elsukov 594fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 59514dd6717SSam Leffler /* 596ffe8cd7bSBjoern A. Zeeb * Bypass packet filtering for packets previously handled by IPsec. 59714dd6717SSam Leffler */ 598fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4) && 599fcf59617SAndrey V. Elsukov IPSEC_CAPS(ipv4, m, IPSEC_CAP_BYPASS_FILTER) != 0) 600c21fd232SAndre Oppermann goto passin; 601ad9f4d6aSAndrey V. Elsukov #endif 602fcf59617SAndrey V. Elsukov 603c4ac87eaSDarren Reed /* 604134ea224SSam Leffler * Run through list of hooks for input packets. 605f51f805fSSam Leffler * 606f51f805fSSam Leffler * NB: Beware of the destination address changing (e.g. 607f51f805fSSam Leffler * by NAT rewriting). When this happens, tell 608f51f805fSSam Leffler * ip_forward to do the right thing. 609c4ac87eaSDarren Reed */ 610c21fd232SAndre Oppermann 611c21fd232SAndre Oppermann /* Jump over all PFIL processing if hooks are not active. */ 612b252313fSGleb Smirnoff if (!PFIL_HOOKED_IN(V_inet_pfil_head)) 613c21fd232SAndre Oppermann goto passin; 614c21fd232SAndre Oppermann 615f51f805fSSam Leffler odst = ip->ip_dst; 616dda6376bSMateusz Guzik if (pfil_mbuf_in(V_inet_pfil_head, &m, ifp, NULL) != 617b252313fSGleb Smirnoff PFIL_PASS) 618beec8214SDarren Reed return; 6199b932e9eSAndre Oppermann 620c4ac87eaSDarren Reed ip = mtod(m, struct ip *); 62102c1c707SAndre Oppermann dchg = (odst.s_addr != ip->ip_dst.s_addr); 6229b932e9eSAndre Oppermann 6239b932e9eSAndre Oppermann if (m->m_flags & M_FASTFWD_OURS) { 6249b932e9eSAndre Oppermann m->m_flags &= ~M_FASTFWD_OURS; 6259b932e9eSAndre Oppermann goto ours; 6269b932e9eSAndre Oppermann } 627ffdbf9daSAndrey V. Elsukov if (m->m_flags & M_IP_NEXTHOP) { 628de89d74bSLuiz Otavio O Souza if (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) { 629099dd043SAndre Oppermann /* 630ffdbf9daSAndrey V. Elsukov * Directly ship the packet on. This allows 631ffdbf9daSAndrey V. Elsukov * forwarding packets originally destined to us 632ffdbf9daSAndrey V. Elsukov * to some other directly connected host. 633099dd043SAndre Oppermann */ 634ffdbf9daSAndrey V. Elsukov ip_forward(m, 1); 635099dd043SAndre Oppermann return; 636099dd043SAndre Oppermann } 637ffdbf9daSAndrey V. Elsukov } 638c21fd232SAndre Oppermann passin: 639*40faf878SMark Johnston /* 640*40faf878SMark Johnston * The unspecified address can appear only as a src address - RFC1122. 641*40faf878SMark Johnston * 642*40faf878SMark Johnston * The check is deferred to here to give firewalls a chance to block 643*40faf878SMark Johnston * (and log) such packets. ip_tryforward() will not process such 644*40faf878SMark Johnston * packets. 645*40faf878SMark Johnston */ 646*40faf878SMark Johnston if (__predict_false(ntohl(ip->ip_dst.s_addr) == INADDR_ANY)) { 647*40faf878SMark Johnston IPSTAT_INC(ips_badaddr); 648*40faf878SMark Johnston goto bad; 649*40faf878SMark Johnston } 65021d172a3SGleb Smirnoff 65121d172a3SGleb Smirnoff /* 652df8bae1dSRodney W. Grimes * Process options and, if not destined for us, 653df8bae1dSRodney W. Grimes * ship it on. ip_dooptions returns 1 when an 654df8bae1dSRodney W. Grimes * error was detected (causing an icmp message 655df8bae1dSRodney W. Grimes * to be sent and the original packet to be freed). 656df8bae1dSRodney W. Grimes */ 6579b932e9eSAndre Oppermann if (hlen > sizeof (struct ip) && ip_dooptions(m, 0)) 658c67b1d17SGarrett Wollman return; 659df8bae1dSRodney W. Grimes 660f0068c4aSGarrett Wollman /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 661f0068c4aSGarrett Wollman * matter if it is destined to another node, or whether it is 662f0068c4aSGarrett Wollman * a multicast one, RSVP wants it! and prevents it from being forwarded 663f0068c4aSGarrett Wollman * anywhere else. Also checks if the rsvp daemon is running before 664f0068c4aSGarrett Wollman * grabbing the packet. 665f0068c4aSGarrett Wollman */ 6660359e7a5SMateusz Guzik if (ip->ip_p == IPPROTO_RSVP && V_rsvp_on) 667f0068c4aSGarrett Wollman goto ours; 668f0068c4aSGarrett Wollman 669df8bae1dSRodney W. Grimes /* 670df8bae1dSRodney W. Grimes * Check our list of addresses, to see if the packet is for us. 671cc766e04SGarrett Wollman * If we don't have any addresses, assume any unicast packet 672cc766e04SGarrett Wollman * we receive might be for us (and let the upper layers deal 673cc766e04SGarrett Wollman * with it). 674df8bae1dSRodney W. Grimes */ 675d7c5a620SMatt Macy if (CK_STAILQ_EMPTY(&V_in_ifaddrhead) && 676cc766e04SGarrett Wollman (m->m_flags & (M_MCAST|M_BCAST)) == 0) 677cc766e04SGarrett Wollman goto ours; 678cc766e04SGarrett Wollman 6797538a9a0SJonathan Lemon /* 680823db0e9SDon Lewis * Enable a consistency check between the destination address 681823db0e9SDon Lewis * and the arrival interface for a unicast packet (the RFC 1122 68294df3271SGleb Smirnoff * strong ES model) with a list of additional predicates: 68394df3271SGleb Smirnoff * - if IP forwarding is disabled 68494df3271SGleb Smirnoff * - the packet is not locally generated 68594df3271SGleb Smirnoff * - the packet is not subject to 'ipfw fwd' 68694df3271SGleb Smirnoff * - Interface is not running CARP. If the packet got here, we already 68794df3271SGleb Smirnoff * checked it with carp_iamatch() and carp_forus(). 688823db0e9SDon Lewis */ 68994df3271SGleb Smirnoff strong_es = V_ip_strong_es && (V_ipforwarding == 0) && 69081674f12SGleb Smirnoff ((ifp->if_flags & IFF_LOOPBACK) == 0) && 69154bfbd51SWill Andrews ifp->if_carp == NULL && (dchg == 0); 692823db0e9SDon Lewis 693ca925d9cSJonathan Lemon /* 694ca925d9cSJonathan Lemon * Check for exact addresses in the hash bucket. 695ca925d9cSJonathan Lemon */ 696c8ee75f2SGleb Smirnoff CK_LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 69794df3271SGleb Smirnoff if (IA_SIN(ia)->sin_addr.s_addr != ip->ip_dst.s_addr) 69894df3271SGleb Smirnoff continue; 69994df3271SGleb Smirnoff 700f9e354dfSJulian Elischer /* 70194df3271SGleb Smirnoff * net.inet.ip.rfc1122_strong_es: the address matches, verify 70294df3271SGleb Smirnoff * that the packet arrived via the correct interface. 703f9e354dfSJulian Elischer */ 70494df3271SGleb Smirnoff if (__predict_false(strong_es && ia->ia_ifp != ifp)) { 70594df3271SGleb Smirnoff IPSTAT_INC(ips_badaddr); 70694df3271SGleb Smirnoff goto bad; 707ca925d9cSJonathan Lemon } 70894df3271SGleb Smirnoff 7092ce85919SGleb Smirnoff /* 7102ce85919SGleb Smirnoff * net.inet.ip.source_address_validation: drop incoming 7112ce85919SGleb Smirnoff * packets that pretend to be ours. 7122ce85919SGleb Smirnoff */ 7132ce85919SGleb Smirnoff if (V_ip_sav && !(ifp->if_flags & IFF_LOOPBACK) && 7142ce85919SGleb Smirnoff __predict_false(in_localip_fib(ip->ip_src, ifp->if_fib))) { 7152ce85919SGleb Smirnoff IPSTAT_INC(ips_badaddr); 7162ce85919SGleb Smirnoff goto bad; 7172ce85919SGleb Smirnoff } 7182ce85919SGleb Smirnoff 71994df3271SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_ipackets, 1); 72094df3271SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_ibytes, m->m_pkthdr.len); 72194df3271SGleb Smirnoff goto ours; 7228c0fec80SRobert Watson } 7232d9cfabaSRobert Watson 724823db0e9SDon Lewis /* 725ca925d9cSJonathan Lemon * Check for broadcast addresses. 726ca925d9cSJonathan Lemon * 727ca925d9cSJonathan Lemon * Only accept broadcast packets that arrive via the matching 728ca925d9cSJonathan Lemon * interface. Reception of forwarded directed broadcasts would 729ca925d9cSJonathan Lemon * be handled via ip_forward() and ether_output() with the loopback 730ca925d9cSJonathan Lemon * into the stack for SIMPLEX interfaces handled by ether_output(). 731823db0e9SDon Lewis */ 73281674f12SGleb Smirnoff if (ifp->if_flags & IFF_BROADCAST) { 733d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 734ca925d9cSJonathan Lemon if (ifa->ifa_addr->sa_family != AF_INET) 735ca925d9cSJonathan Lemon continue; 736ca925d9cSJonathan Lemon ia = ifatoia(ifa); 737df8bae1dSRodney W. Grimes if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 7380aade26eSRobert Watson ip->ip_dst.s_addr) { 7397caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_ipackets, 1); 7407caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_ibytes, 7417caf4ab7SGleb Smirnoff m->m_pkthdr.len); 742df8bae1dSRodney W. Grimes goto ours; 7430aade26eSRobert Watson } 7440ac40133SBrian Somers #ifdef BOOTP_COMPAT 7450aade26eSRobert Watson if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) { 7467caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_ipackets, 1); 7477caf4ab7SGleb Smirnoff counter_u64_add(ia->ia_ifa.ifa_ibytes, 7487caf4ab7SGleb Smirnoff m->m_pkthdr.len); 749ca925d9cSJonathan Lemon goto ours; 7500aade26eSRobert Watson } 7510ac40133SBrian Somers #endif 752df8bae1dSRodney W. Grimes } 75319e5b0a7SRobert Watson ia = NULL; 754df8bae1dSRodney W. Grimes } 755df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 75603b0505bSZhenlei Huang /* 75703b0505bSZhenlei Huang * RFC 3927 2.7: Do not forward multicast packets from 75803b0505bSZhenlei Huang * IN_LINKLOCAL. 75903b0505bSZhenlei Huang */ 7603d846e48SZhenlei Huang if (V_ip_mrouter && !IN_LINKLOCAL(ntohl(ip->ip_src.s_addr))) { 761df8bae1dSRodney W. Grimes /* 762df8bae1dSRodney W. Grimes * If we are acting as a multicast router, all 763df8bae1dSRodney W. Grimes * incoming multicast packets are passed to the 764df8bae1dSRodney W. Grimes * kernel-level multicast forwarding function. 765df8bae1dSRodney W. Grimes * The packet is returned (relatively) intact; if 766df8bae1dSRodney W. Grimes * ip_mforward() returns a non-zero value, the packet 767df8bae1dSRodney W. Grimes * must be discarded, else it may be accepted below. 768df8bae1dSRodney W. Grimes */ 7690aade26eSRobert Watson if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) { 77086425c62SRobert Watson IPSTAT_INC(ips_cantforward); 771df8bae1dSRodney W. Grimes m_freem(m); 772c67b1d17SGarrett Wollman return; 773df8bae1dSRodney W. Grimes } 774df8bae1dSRodney W. Grimes 775df8bae1dSRodney W. Grimes /* 77611612afaSDima Dorfman * The process-level routing daemon needs to receive 777df8bae1dSRodney W. Grimes * all multicast IGMP packets, whether or not this 778df8bae1dSRodney W. Grimes * host belongs to their destination groups. 779df8bae1dSRodney W. Grimes */ 78065634ae7SWojciech Macek if (ip->ip_p == IPPROTO_IGMP) { 781df8bae1dSRodney W. Grimes goto ours; 78265634ae7SWojciech Macek } 78386425c62SRobert Watson IPSTAT_INC(ips_forward); 784df8bae1dSRodney W. Grimes } 785df8bae1dSRodney W. Grimes /* 786d10910e6SBruce M Simpson * Assume the packet is for us, to avoid prematurely taking 787d10910e6SBruce M Simpson * a lock on the in_multi hash. Protocols must perform 788d10910e6SBruce M Simpson * their own filtering and update statistics accordingly. 789df8bae1dSRodney W. Grimes */ 790df8bae1dSRodney W. Grimes goto ours; 791df8bae1dSRodney W. Grimes } 792df8bae1dSRodney W. Grimes if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 793df8bae1dSRodney W. Grimes goto ours; 794df8bae1dSRodney W. Grimes if (ip->ip_dst.s_addr == INADDR_ANY) 795df8bae1dSRodney W. Grimes goto ours; 79603b0505bSZhenlei Huang /* RFC 3927 2.7: Do not forward packets to or from IN_LINKLOCAL. */ 7973d846e48SZhenlei Huang if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) || 7983d846e48SZhenlei Huang IN_LINKLOCAL(ntohl(ip->ip_src.s_addr))) { 7993d846e48SZhenlei Huang IPSTAT_INC(ips_cantforward); 8003d846e48SZhenlei Huang m_freem(m); 8013d846e48SZhenlei Huang return; 8023d846e48SZhenlei Huang } 803df8bae1dSRodney W. Grimes 8046a800098SYoshinobu Inoue /* 805df8bae1dSRodney W. Grimes * Not for us; forward if possible and desirable. 806df8bae1dSRodney W. Grimes */ 807603724d3SBjoern A. Zeeb if (V_ipforwarding == 0) { 80886425c62SRobert Watson IPSTAT_INC(ips_cantforward); 809df8bae1dSRodney W. Grimes m_freem(m); 810546f251bSChris D. Faulhaber } else { 8119b932e9eSAndre Oppermann ip_forward(m, dchg); 812546f251bSChris D. Faulhaber } 813c67b1d17SGarrett Wollman return; 814df8bae1dSRodney W. Grimes 815df8bae1dSRodney W. Grimes ours: 816d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH 817d0ebc0d2SYaroslav Tykhiy /* 818d0ebc0d2SYaroslav Tykhiy * IPSTEALTH: Process non-routing options only 819d0ebc0d2SYaroslav Tykhiy * if the packet is destined for us. 820d0ebc0d2SYaroslav Tykhiy */ 8217caf4ab7SGleb Smirnoff if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1)) 822d0ebc0d2SYaroslav Tykhiy return; 823d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */ 824d0ebc0d2SYaroslav Tykhiy 82563f8d699SJordan K. Hubbard /* 8265ab15157SDoug Rabson * We are going to ship the packet to the local protocol stack. Call the 8275ab15157SDoug Rabson * filter again for this 'output' action, allowing redirect-like rules 8285ab15157SDoug Rabson * to adjust the source address. 8295ab15157SDoug Rabson */ 8305ab15157SDoug Rabson if (PFIL_HOOKED_OUT(V_inet_local_pfil_head)) { 8315ab15157SDoug Rabson if (pfil_mbuf_out(V_inet_local_pfil_head, &m, V_loif, NULL) != 8325ab15157SDoug Rabson PFIL_PASS) 8335ab15157SDoug Rabson return; 8345ab15157SDoug Rabson ip = mtod(m, struct ip *); 8355ab15157SDoug Rabson } 8365ab15157SDoug Rabson 8375ab15157SDoug Rabson /* 838b6ea1aa5SRuslan Ermilov * Attempt reassembly; if it succeeds, proceed. 839ac9d7e26SMax Laier * ip_reass() will return a different mbuf. 840df8bae1dSRodney W. Grimes */ 8418f134647SGleb Smirnoff if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) { 842aa69c612SGleb Smirnoff /* XXXGL: shouldn't we save & set m_flags? */ 843f0cada84SAndre Oppermann m = ip_reass(m); 844f0cada84SAndre Oppermann if (m == NULL) 845c67b1d17SGarrett Wollman return; 8466a800098SYoshinobu Inoue ip = mtod(m, struct ip *); 8477e2df452SRuslan Ermilov /* Get the header length of the reassembled packet */ 84853be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2; 849f0cada84SAndre Oppermann } 850f0cada84SAndre Oppermann 851fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 852fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) { 853fcf59617SAndrey V. Elsukov if (IPSEC_INPUT(ipv4, m, hlen, ip->ip_p) != 0) 854fcf59617SAndrey V. Elsukov return; 855fcf59617SAndrey V. Elsukov } 856b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 85733841545SHajimu UMEMOTO 858df8bae1dSRodney W. Grimes /* 859df8bae1dSRodney W. Grimes * Switch out to protocol's input routine. 860df8bae1dSRodney W. Grimes */ 86186425c62SRobert Watson IPSTAT_INC(ips_delivered); 8629b932e9eSAndre Oppermann 86378b1fc05SGleb Smirnoff ip_protox[ip->ip_p](&m, &hlen, ip->ip_p); 864c67b1d17SGarrett Wollman return; 865df8bae1dSRodney W. Grimes bad: 866df8bae1dSRodney W. Grimes m_freem(m); 867c67b1d17SGarrett Wollman } 868c67b1d17SGarrett Wollman 869de38924dSAndre Oppermann int 87078b1fc05SGleb Smirnoff ipproto_register(uint8_t proto, ipproto_input_t input, ipproto_ctlinput_t ctl) 871de38924dSAndre Oppermann { 872de38924dSAndre Oppermann 87378b1fc05SGleb Smirnoff MPASS(proto > 0); 874de38924dSAndre Oppermann 875de38924dSAndre Oppermann /* 876de38924dSAndre Oppermann * The protocol slot must not be occupied by another protocol 87778b1fc05SGleb Smirnoff * already. An index pointing to rip_input() is unused. 878de38924dSAndre Oppermann */ 87978b1fc05SGleb Smirnoff if (ip_protox[proto] == rip_input) { 88078b1fc05SGleb Smirnoff ip_protox[proto] = input; 88178b1fc05SGleb Smirnoff ip_ctlprotox[proto] = ctl; 882de38924dSAndre Oppermann return (0); 88378b1fc05SGleb Smirnoff } else 88478b1fc05SGleb Smirnoff return (EEXIST); 885de38924dSAndre Oppermann } 886de38924dSAndre Oppermann 887de38924dSAndre Oppermann int 88878b1fc05SGleb Smirnoff ipproto_unregister(uint8_t proto) 889de38924dSAndre Oppermann { 890de38924dSAndre Oppermann 89178b1fc05SGleb Smirnoff MPASS(proto > 0); 892de38924dSAndre Oppermann 89378b1fc05SGleb Smirnoff if (ip_protox[proto] != rip_input) { 89478b1fc05SGleb Smirnoff ip_protox[proto] = rip_input; 89578b1fc05SGleb Smirnoff ip_ctlprotox[proto] = rip_ctlinput; 896de38924dSAndre Oppermann return (0); 89778b1fc05SGleb Smirnoff } else 89878b1fc05SGleb Smirnoff return (ENOENT); 899de38924dSAndre Oppermann } 900de38924dSAndre Oppermann 901df8bae1dSRodney W. Grimes /* 902df8bae1dSRodney W. Grimes * Forward a packet. If some error occurs return the sender 903df8bae1dSRodney W. Grimes * an icmp packet. Note we can't always generate a meaningful 904df8bae1dSRodney W. Grimes * icmp message because icmp doesn't have a large enough repertoire 905df8bae1dSRodney W. Grimes * of codes and types. 906df8bae1dSRodney W. Grimes * 907df8bae1dSRodney W. Grimes * If not forwarding, just drop the packet. This could be confusing 908df8bae1dSRodney W. Grimes * if ipforwarding was zero but some routing protocol was advancing 909df8bae1dSRodney W. Grimes * us as a gateway to somewhere. However, we must let the routing 910df8bae1dSRodney W. Grimes * protocol deal with that. 911df8bae1dSRodney W. Grimes * 912df8bae1dSRodney W. Grimes * The srcrt parameter indicates whether the packet is being forwarded 913df8bae1dSRodney W. Grimes * via a source route. 914df8bae1dSRodney W. Grimes */ 9159b932e9eSAndre Oppermann void 9169b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt) 917df8bae1dSRodney W. Grimes { 9182b25acc1SLuigi Rizzo struct ip *ip = mtod(m, struct ip *); 919efbad259SEdward Tomasz Napierala struct in_ifaddr *ia; 920df8bae1dSRodney W. Grimes struct mbuf *mcopy; 921d14122b0SErmal Luçi struct sockaddr_in *sin; 9229b932e9eSAndre Oppermann struct in_addr dest; 923b835b6feSBjoern A. Zeeb struct route ro; 9244043ee3cSAlexander V. Chernikov uint32_t flowid; 925c773494eSAndre Oppermann int error, type = 0, code = 0, mtu = 0; 9263efc3014SJulian Elischer 927b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 928b8a6e03fSGleb Smirnoff 9299b932e9eSAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 93086425c62SRobert Watson IPSTAT_INC(ips_cantforward); 931df8bae1dSRodney W. Grimes m_freem(m); 932df8bae1dSRodney W. Grimes return; 933df8bae1dSRodney W. Grimes } 934fcf59617SAndrey V. Elsukov if ( 935fcf59617SAndrey V. Elsukov #ifdef IPSTEALTH 936fcf59617SAndrey V. Elsukov V_ipstealth == 0 && 937fcf59617SAndrey V. Elsukov #endif 938fcf59617SAndrey V. Elsukov ip->ip_ttl <= IPTTLDEC) { 939fcf59617SAndrey V. Elsukov icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0); 9408922ddbeSAndrey V. Elsukov return; 9418922ddbeSAndrey V. Elsukov } 942df8bae1dSRodney W. Grimes 943d14122b0SErmal Luçi bzero(&ro, sizeof(ro)); 944d14122b0SErmal Luçi sin = (struct sockaddr_in *)&ro.ro_dst; 945d14122b0SErmal Luçi sin->sin_family = AF_INET; 946d14122b0SErmal Luçi sin->sin_len = sizeof(*sin); 947d14122b0SErmal Luçi sin->sin_addr = ip->ip_dst; 9484043ee3cSAlexander V. Chernikov flowid = m->m_pkthdr.flowid; 9494043ee3cSAlexander V. Chernikov ro.ro_nh = fib4_lookup(M_GETFIB(m), ip->ip_dst, 0, NHR_REF, flowid); 950983066f0SAlexander V. Chernikov if (ro.ro_nh != NULL) { 951e27970aeSKristof Provost if (ro.ro_nh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST)) { 952e27970aeSKristof Provost IPSTAT_INC(ips_cantforward); 953e27970aeSKristof Provost m_freem(m); 954e27970aeSKristof Provost NH_FREE(ro.ro_nh); 955e27970aeSKristof Provost return; 956e27970aeSKristof Provost } 957e27970aeSKristof Provost if (ro.ro_nh->nh_flags & NHF_REJECT) { 958e27970aeSKristof Provost IPSTAT_INC(ips_cantforward); 959e27970aeSKristof Provost NH_FREE(ro.ro_nh); 960e27970aeSKristof Provost icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 961e27970aeSKristof Provost return; 962e27970aeSKristof Provost } 963983066f0SAlexander V. Chernikov ia = ifatoia(ro.ro_nh->nh_ifa); 96456844a62SErmal Luçi } else 96556844a62SErmal Luçi ia = NULL; 966df8bae1dSRodney W. Grimes /* 967bfef7ed4SIan Dowse * Save the IP header and at most 8 bytes of the payload, 968bfef7ed4SIan Dowse * in case we need to generate an ICMP message to the src. 969bfef7ed4SIan Dowse * 9704d2e3692SLuigi Rizzo * XXX this can be optimized a lot by saving the data in a local 9714d2e3692SLuigi Rizzo * buffer on the stack (72 bytes at most), and only allocating the 9724d2e3692SLuigi Rizzo * mbuf if really necessary. The vast majority of the packets 9734d2e3692SLuigi Rizzo * are forwarded without having to send an ICMP back (either 9744d2e3692SLuigi Rizzo * because unnecessary, or because rate limited), so we are 9754d2e3692SLuigi Rizzo * really we are wasting a lot of work here. 9764d2e3692SLuigi Rizzo * 977c3bef61eSKevin Lo * We don't use m_copym() because it might return a reference 978bfef7ed4SIan Dowse * to a shared cluster. Both this function and ip_output() 979bfef7ed4SIan Dowse * assume exclusive access to the IP header in `m', so any 980bfef7ed4SIan Dowse * data in a cluster may change before we reach icmp_error(). 981df8bae1dSRodney W. Grimes */ 982dc4ad05eSGleb Smirnoff mcopy = m_gethdr(M_NOWAIT, m->m_type); 983eb1b1807SGleb Smirnoff if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) { 9849967cafcSSam Leffler /* 9859967cafcSSam Leffler * It's probably ok if the pkthdr dup fails (because 9869967cafcSSam Leffler * the deep copy of the tag chain failed), but for now 9879967cafcSSam Leffler * be conservative and just discard the copy since 9889967cafcSSam Leffler * code below may some day want the tags. 9899967cafcSSam Leffler */ 9909967cafcSSam Leffler m_free(mcopy); 9919967cafcSSam Leffler mcopy = NULL; 9929967cafcSSam Leffler } 993bfef7ed4SIan Dowse if (mcopy != NULL) { 9948f134647SGleb Smirnoff mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy)); 995e6b0a570SBruce M Simpson mcopy->m_pkthdr.len = mcopy->m_len; 996bfef7ed4SIan Dowse m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t)); 997bfef7ed4SIan Dowse } 99804287599SRuslan Ermilov #ifdef IPSTEALTH 999fcf59617SAndrey V. Elsukov if (V_ipstealth == 0) 100004287599SRuslan Ermilov #endif 100104287599SRuslan Ermilov ip->ip_ttl -= IPTTLDEC; 1002fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1003fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) { 1004fcf59617SAndrey V. Elsukov if ((error = IPSEC_FORWARD(ipv4, m)) != 0) { 1005fcf59617SAndrey V. Elsukov /* mbuf consumed by IPsec */ 1006d16a2e47SMark Johnston RO_NHFREE(&ro); 1007fcf59617SAndrey V. Elsukov m_freem(mcopy); 1008fcf59617SAndrey V. Elsukov if (error != EINPROGRESS) 1009fcf59617SAndrey V. Elsukov IPSTAT_INC(ips_cantforward); 1010b8a6e03fSGleb Smirnoff return; 101104287599SRuslan Ermilov } 1012fcf59617SAndrey V. Elsukov /* No IPsec processing required */ 1013fcf59617SAndrey V. Elsukov } 1014fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 1015df8bae1dSRodney W. Grimes /* 1016df8bae1dSRodney W. Grimes * If forwarding packet using same interface that it came in on, 1017df8bae1dSRodney W. Grimes * perhaps should send a redirect to sender to shortcut a hop. 1018df8bae1dSRodney W. Grimes * Only send redirect if source is sending directly to us, 1019df8bae1dSRodney W. Grimes * and if packet was not source routed (or has any options). 1020df8bae1dSRodney W. Grimes * Also, don't send redirect if forwarding using a default route 1021df8bae1dSRodney W. Grimes * or a route modified by a redirect. 1022df8bae1dSRodney W. Grimes */ 10239b932e9eSAndre Oppermann dest.s_addr = 0; 1024efbad259SEdward Tomasz Napierala if (!srcrt && V_ipsendredirects && 1025efbad259SEdward Tomasz Napierala ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) { 1026983066f0SAlexander V. Chernikov struct nhop_object *nh; 102702c1c707SAndre Oppermann 1028983066f0SAlexander V. Chernikov nh = ro.ro_nh; 102902c1c707SAndre Oppermann 1030983066f0SAlexander V. Chernikov if (nh != NULL && ((nh->nh_flags & (NHF_REDIRECT|NHF_DEFAULT)) == 0)) { 1031983066f0SAlexander V. Chernikov struct in_ifaddr *nh_ia = (struct in_ifaddr *)(nh->nh_ifa); 1032df8bae1dSRodney W. Grimes u_long src = ntohl(ip->ip_src.s_addr); 1033df8bae1dSRodney W. Grimes 1034983066f0SAlexander V. Chernikov if (nh_ia != NULL && 1035983066f0SAlexander V. Chernikov (src & nh_ia->ia_subnetmask) == nh_ia->ia_subnet) { 1036df8bae1dSRodney W. Grimes /* Router requirements says to only send host redirects */ 1037df8bae1dSRodney W. Grimes type = ICMP_REDIRECT; 1038df8bae1dSRodney W. Grimes code = ICMP_REDIRECT_HOST; 103962e1a437SZhenlei Huang if (nh->nh_flags & NHF_GATEWAY) { 104062e1a437SZhenlei Huang if (nh->gw_sa.sa_family == AF_INET) 104162e1a437SZhenlei Huang dest.s_addr = nh->gw4_sa.sin_addr.s_addr; 104262e1a437SZhenlei Huang else /* Do not redirect in case gw is AF_INET6 */ 104362e1a437SZhenlei Huang type = 0; 104462e1a437SZhenlei Huang } else 104562e1a437SZhenlei Huang dest.s_addr = ip->ip_dst.s_addr; 1046df8bae1dSRodney W. Grimes } 1047df8bae1dSRodney W. Grimes } 104802c1c707SAndre Oppermann } 1049df8bae1dSRodney W. Grimes 1050b835b6feSBjoern A. Zeeb error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL); 1051b835b6feSBjoern A. Zeeb 1052983066f0SAlexander V. Chernikov if (error == EMSGSIZE && ro.ro_nh) 1053983066f0SAlexander V. Chernikov mtu = ro.ro_nh->nh_mtu; 1054983066f0SAlexander V. Chernikov RO_NHFREE(&ro); 1055b835b6feSBjoern A. Zeeb 1056df8bae1dSRodney W. Grimes if (error) 105786425c62SRobert Watson IPSTAT_INC(ips_cantforward); 1058df8bae1dSRodney W. Grimes else { 105986425c62SRobert Watson IPSTAT_INC(ips_forward); 1060df8bae1dSRodney W. Grimes if (type) 106186425c62SRobert Watson IPSTAT_INC(ips_redirectsent); 1062df8bae1dSRodney W. Grimes else { 10639188b4a1SAndre Oppermann if (mcopy) 1064df8bae1dSRodney W. Grimes m_freem(mcopy); 1065b8a6e03fSGleb Smirnoff return; 1066df8bae1dSRodney W. Grimes } 1067df8bae1dSRodney W. Grimes } 10684f6c66ccSMatt Macy if (mcopy == NULL) 1069b8a6e03fSGleb Smirnoff return; 10704f6c66ccSMatt Macy 1071df8bae1dSRodney W. Grimes switch (error) { 1072df8bae1dSRodney W. Grimes case 0: /* forwarded, but need redirect */ 1073df8bae1dSRodney W. Grimes /* type, code set above */ 1074df8bae1dSRodney W. Grimes break; 1075df8bae1dSRodney W. Grimes 1076efbad259SEdward Tomasz Napierala case ENETUNREACH: 1077df8bae1dSRodney W. Grimes case EHOSTUNREACH: 1078df8bae1dSRodney W. Grimes case ENETDOWN: 1079df8bae1dSRodney W. Grimes case EHOSTDOWN: 1080df8bae1dSRodney W. Grimes default: 1081df8bae1dSRodney W. Grimes type = ICMP_UNREACH; 1082df8bae1dSRodney W. Grimes code = ICMP_UNREACH_HOST; 1083df8bae1dSRodney W. Grimes break; 1084df8bae1dSRodney W. Grimes 1085df8bae1dSRodney W. Grimes case EMSGSIZE: 1086df8bae1dSRodney W. Grimes type = ICMP_UNREACH; 1087df8bae1dSRodney W. Grimes code = ICMP_UNREACH_NEEDFRAG; 10889b932e9eSAndre Oppermann /* 1089b835b6feSBjoern A. Zeeb * If the MTU was set before make sure we are below the 1090b835b6feSBjoern A. Zeeb * interface MTU. 1091ab48768bSAndre Oppermann * If the MTU wasn't set before use the interface mtu or 1092ab48768bSAndre Oppermann * fall back to the next smaller mtu step compared to the 1093ab48768bSAndre Oppermann * current packet size. 10949b932e9eSAndre Oppermann */ 1095b835b6feSBjoern A. Zeeb if (mtu != 0) { 1096b835b6feSBjoern A. Zeeb if (ia != NULL) 1097b835b6feSBjoern A. Zeeb mtu = min(mtu, ia->ia_ifp->if_mtu); 1098b835b6feSBjoern A. Zeeb } else { 1099ab48768bSAndre Oppermann if (ia != NULL) 1100c773494eSAndre Oppermann mtu = ia->ia_ifp->if_mtu; 1101ab48768bSAndre Oppermann else 11028f134647SGleb Smirnoff mtu = ip_next_mtu(ntohs(ip->ip_len), 0); 1103ab48768bSAndre Oppermann } 110486425c62SRobert Watson IPSTAT_INC(ips_cantfrag); 1105df8bae1dSRodney W. Grimes break; 1106df8bae1dSRodney W. Grimes 1107df8bae1dSRodney W. Grimes case ENOBUFS: 11083a06e3e0SRuslan Ermilov case EACCES: /* ipfw denied packet */ 11093a06e3e0SRuslan Ermilov m_freem(mcopy); 1110b8a6e03fSGleb Smirnoff return; 1111df8bae1dSRodney W. Grimes } 1112c773494eSAndre Oppermann icmp_error(mcopy, type, code, dest.s_addr, mtu); 1113df8bae1dSRodney W. Grimes } 1114df8bae1dSRodney W. Grimes 1115339efd75SMaxim Sobolev #define CHECK_SO_CT(sp, ct) \ 1116339efd75SMaxim Sobolev (((sp->so_options & SO_TIMESTAMP) && (sp->so_ts_clock == ct)) ? 1 : 0) 1117339efd75SMaxim Sobolev 111882c23ebaSBill Fenner void 1119f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip, 1120f2565d68SRobert Watson struct mbuf *m) 112182c23ebaSBill Fenner { 112206193f0bSKonstantin Belousov bool stamped; 11238b615593SMarko Zec 112406193f0bSKonstantin Belousov stamped = false; 1125339efd75SMaxim Sobolev if ((inp->inp_socket->so_options & SO_BINTIME) || 1126339efd75SMaxim Sobolev CHECK_SO_CT(inp->inp_socket, SO_TS_BINTIME)) { 112706193f0bSKonstantin Belousov struct bintime boottimebin, bt; 112806193f0bSKonstantin Belousov struct timespec ts1; 1129be8a62e8SPoul-Henning Kamp 113006193f0bSKonstantin Belousov if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR | 113106193f0bSKonstantin Belousov M_TSTMP)) { 113206193f0bSKonstantin Belousov mbuf_tstmp2timespec(m, &ts1); 113306193f0bSKonstantin Belousov timespec2bintime(&ts1, &bt); 113406193f0bSKonstantin Belousov getboottimebin(&boottimebin); 113506193f0bSKonstantin Belousov bintime_add(&bt, &boottimebin); 113606193f0bSKonstantin Belousov } else { 1137be8a62e8SPoul-Henning Kamp bintime(&bt); 113806193f0bSKonstantin Belousov } 1139b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&bt, sizeof(bt), SCM_BINTIME, 1140b46667c6SGleb Smirnoff SOL_SOCKET, M_NOWAIT); 114106193f0bSKonstantin Belousov if (*mp != NULL) { 1142be8a62e8SPoul-Henning Kamp mp = &(*mp)->m_next; 114306193f0bSKonstantin Belousov stamped = true; 114406193f0bSKonstantin Belousov } 1145be8a62e8SPoul-Henning Kamp } 1146339efd75SMaxim Sobolev if (CHECK_SO_CT(inp->inp_socket, SO_TS_REALTIME_MICRO)) { 114706193f0bSKonstantin Belousov struct bintime boottimebin, bt1; 1148c012cfe6SEd Maste struct timespec ts1; 114982c23ebaSBill Fenner struct timeval tv; 115082c23ebaSBill Fenner 115106193f0bSKonstantin Belousov if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR | 115206193f0bSKonstantin Belousov M_TSTMP)) { 115306193f0bSKonstantin Belousov mbuf_tstmp2timespec(m, &ts1); 115406193f0bSKonstantin Belousov timespec2bintime(&ts1, &bt1); 115506193f0bSKonstantin Belousov getboottimebin(&boottimebin); 115606193f0bSKonstantin Belousov bintime_add(&bt1, &boottimebin); 115706193f0bSKonstantin Belousov bintime2timeval(&bt1, &tv); 115806193f0bSKonstantin Belousov } else { 1159339efd75SMaxim Sobolev microtime(&tv); 116006193f0bSKonstantin Belousov } 1161b46667c6SGleb Smirnoff *mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv), SCM_TIMESTAMP, 1162b46667c6SGleb Smirnoff SOL_SOCKET, M_NOWAIT); 116306193f0bSKonstantin Belousov if (*mp != NULL) { 116482c23ebaSBill Fenner mp = &(*mp)->m_next; 116506193f0bSKonstantin Belousov stamped = true; 116606193f0bSKonstantin Belousov } 1167339efd75SMaxim Sobolev } else if (CHECK_SO_CT(inp->inp_socket, SO_TS_REALTIME)) { 116806193f0bSKonstantin Belousov struct bintime boottimebin; 116906193f0bSKonstantin Belousov struct timespec ts, ts1; 1170339efd75SMaxim Sobolev 117106193f0bSKonstantin Belousov if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR | 117206193f0bSKonstantin Belousov M_TSTMP)) { 117306193f0bSKonstantin Belousov mbuf_tstmp2timespec(m, &ts); 117406193f0bSKonstantin Belousov getboottimebin(&boottimebin); 117506193f0bSKonstantin Belousov bintime2timespec(&boottimebin, &ts1); 11766040822cSAlan Somers timespecadd(&ts, &ts1, &ts); 117706193f0bSKonstantin Belousov } else { 1178339efd75SMaxim Sobolev nanotime(&ts); 117906193f0bSKonstantin Belousov } 1180b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&ts, sizeof(ts), SCM_REALTIME, 1181b46667c6SGleb Smirnoff SOL_SOCKET, M_NOWAIT); 118206193f0bSKonstantin Belousov if (*mp != NULL) { 1183339efd75SMaxim Sobolev mp = &(*mp)->m_next; 118406193f0bSKonstantin Belousov stamped = true; 118506193f0bSKonstantin Belousov } 1186339efd75SMaxim Sobolev } else if (CHECK_SO_CT(inp->inp_socket, SO_TS_MONOTONIC)) { 1187339efd75SMaxim Sobolev struct timespec ts; 1188339efd75SMaxim Sobolev 118906193f0bSKonstantin Belousov if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR | 119006193f0bSKonstantin Belousov M_TSTMP)) 119106193f0bSKonstantin Belousov mbuf_tstmp2timespec(m, &ts); 119206193f0bSKonstantin Belousov else 1193339efd75SMaxim Sobolev nanouptime(&ts); 1194b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&ts, sizeof(ts), SCM_MONOTONIC, 1195b46667c6SGleb Smirnoff SOL_SOCKET, M_NOWAIT); 119606193f0bSKonstantin Belousov if (*mp != NULL) { 119706193f0bSKonstantin Belousov mp = &(*mp)->m_next; 119806193f0bSKonstantin Belousov stamped = true; 119906193f0bSKonstantin Belousov } 120006193f0bSKonstantin Belousov } 120106193f0bSKonstantin Belousov if (stamped && (m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR | 120206193f0bSKonstantin Belousov M_TSTMP)) { 120306193f0bSKonstantin Belousov struct sock_timestamp_info sti; 120406193f0bSKonstantin Belousov 120506193f0bSKonstantin Belousov bzero(&sti, sizeof(sti)); 120606193f0bSKonstantin Belousov sti.st_info_flags = ST_INFO_HW; 120706193f0bSKonstantin Belousov if ((m->m_flags & M_TSTMP_HPREC) != 0) 120806193f0bSKonstantin Belousov sti.st_info_flags |= ST_INFO_HW_HPREC; 1209b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&sti, sizeof(sti), SCM_TIME_INFO, 1210b46667c6SGleb Smirnoff SOL_SOCKET, M_NOWAIT); 121106193f0bSKonstantin Belousov if (*mp != NULL) 1212339efd75SMaxim Sobolev mp = &(*mp)->m_next; 1213be8a62e8SPoul-Henning Kamp } 121482c23ebaSBill Fenner if (inp->inp_flags & INP_RECVDSTADDR) { 1215b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&ip->ip_dst, sizeof(struct in_addr), 1216b46667c6SGleb Smirnoff IP_RECVDSTADDR, IPPROTO_IP, M_NOWAIT); 121782c23ebaSBill Fenner if (*mp) 121882c23ebaSBill Fenner mp = &(*mp)->m_next; 121982c23ebaSBill Fenner } 12204957466bSMatthew N. Dodd if (inp->inp_flags & INP_RECVTTL) { 1221b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&ip->ip_ttl, sizeof(u_char), IP_RECVTTL, 1222b46667c6SGleb Smirnoff IPPROTO_IP, M_NOWAIT); 12234957466bSMatthew N. Dodd if (*mp) 12244957466bSMatthew N. Dodd mp = &(*mp)->m_next; 12254957466bSMatthew N. Dodd } 122682c23ebaSBill Fenner #ifdef notyet 122782c23ebaSBill Fenner /* XXX 122882c23ebaSBill Fenner * Moving these out of udp_input() made them even more broken 122982c23ebaSBill Fenner * than they already were. 123082c23ebaSBill Fenner */ 123182c23ebaSBill Fenner /* options were tossed already */ 123282c23ebaSBill Fenner if (inp->inp_flags & INP_RECVOPTS) { 1233b46667c6SGleb Smirnoff *mp = sbcreatecontrol(opts_deleted_above, 1234b46667c6SGleb Smirnoff sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP, M_NOWAIT); 123582c23ebaSBill Fenner if (*mp) 123682c23ebaSBill Fenner mp = &(*mp)->m_next; 123782c23ebaSBill Fenner } 123882c23ebaSBill Fenner /* ip_srcroute doesn't do what we want here, need to fix */ 123982c23ebaSBill Fenner if (inp->inp_flags & INP_RECVRETOPTS) { 1240b46667c6SGleb Smirnoff *mp = sbcreatecontrol(ip_srcroute(m), sizeof(struct in_addr), 1241b46667c6SGleb Smirnoff IP_RECVRETOPTS, IPPROTO_IP, M_NOWAIT); 124282c23ebaSBill Fenner if (*mp) 124382c23ebaSBill Fenner mp = &(*mp)->m_next; 124482c23ebaSBill Fenner } 124582c23ebaSBill Fenner #endif 124682c23ebaSBill Fenner if (inp->inp_flags & INP_RECVIF) { 1247d314ad7bSJulian Elischer struct ifnet *ifp; 1248d314ad7bSJulian Elischer struct sdlbuf { 124982c23ebaSBill Fenner struct sockaddr_dl sdl; 1250d314ad7bSJulian Elischer u_char pad[32]; 1251d314ad7bSJulian Elischer } sdlbuf; 1252d314ad7bSJulian Elischer struct sockaddr_dl *sdp; 1253d314ad7bSJulian Elischer struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 125482c23ebaSBill Fenner 1255db0ac6deSCy Schubert if ((ifp = m->m_pkthdr.rcvif)) { 12564a0d6638SRuslan Ermilov sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr; 1257d314ad7bSJulian Elischer /* 1258d314ad7bSJulian Elischer * Change our mind and don't try copy. 1259d314ad7bSJulian Elischer */ 126046f2df9cSSergey Kandaurov if (sdp->sdl_family != AF_LINK || 126146f2df9cSSergey Kandaurov sdp->sdl_len > sizeof(sdlbuf)) { 1262d314ad7bSJulian Elischer goto makedummy; 1263d314ad7bSJulian Elischer } 1264d314ad7bSJulian Elischer bcopy(sdp, sdl2, sdp->sdl_len); 1265d314ad7bSJulian Elischer } else { 1266d314ad7bSJulian Elischer makedummy: 126746f2df9cSSergey Kandaurov sdl2->sdl_len = 126846f2df9cSSergey Kandaurov offsetof(struct sockaddr_dl, sdl_data[0]); 1269d314ad7bSJulian Elischer sdl2->sdl_family = AF_LINK; 1270d314ad7bSJulian Elischer sdl2->sdl_index = 0; 1271d314ad7bSJulian Elischer sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 1272d314ad7bSJulian Elischer } 1273b46667c6SGleb Smirnoff *mp = sbcreatecontrol(sdl2, sdl2->sdl_len, IP_RECVIF, 1274b46667c6SGleb Smirnoff IPPROTO_IP, M_NOWAIT); 127582c23ebaSBill Fenner if (*mp) 127682c23ebaSBill Fenner mp = &(*mp)->m_next; 127782c23ebaSBill Fenner } 12783cca425bSMichael Tuexen if (inp->inp_flags & INP_RECVTOS) { 1279b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&ip->ip_tos, sizeof(u_char), IP_RECVTOS, 1280b46667c6SGleb Smirnoff IPPROTO_IP, M_NOWAIT); 12813cca425bSMichael Tuexen if (*mp) 12823cca425bSMichael Tuexen mp = &(*mp)->m_next; 12833cca425bSMichael Tuexen } 12849d3ddf43SAdrian Chadd 12859d3ddf43SAdrian Chadd if (inp->inp_flags2 & INP_RECVFLOWID) { 12869d3ddf43SAdrian Chadd uint32_t flowid, flow_type; 12879d3ddf43SAdrian Chadd 12889d3ddf43SAdrian Chadd flowid = m->m_pkthdr.flowid; 12899d3ddf43SAdrian Chadd flow_type = M_HASHTYPE_GET(m); 12909d3ddf43SAdrian Chadd 12919d3ddf43SAdrian Chadd /* 12929d3ddf43SAdrian Chadd * XXX should handle the failure of one or the 12939d3ddf43SAdrian Chadd * other - don't populate both? 12949d3ddf43SAdrian Chadd */ 1295b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&flowid, sizeof(uint32_t), IP_FLOWID, 1296b46667c6SGleb Smirnoff IPPROTO_IP, M_NOWAIT); 12979d3ddf43SAdrian Chadd if (*mp) 12989d3ddf43SAdrian Chadd mp = &(*mp)->m_next; 1299b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&flow_type, sizeof(uint32_t), 1300b46667c6SGleb Smirnoff IP_FLOWTYPE, IPPROTO_IP, M_NOWAIT); 13019d3ddf43SAdrian Chadd if (*mp) 13029d3ddf43SAdrian Chadd mp = &(*mp)->m_next; 13039d3ddf43SAdrian Chadd } 13049d3ddf43SAdrian Chadd 13059d3ddf43SAdrian Chadd #ifdef RSS 13069d3ddf43SAdrian Chadd if (inp->inp_flags2 & INP_RECVRSSBUCKETID) { 13079d3ddf43SAdrian Chadd uint32_t flowid, flow_type; 13089d3ddf43SAdrian Chadd uint32_t rss_bucketid; 13099d3ddf43SAdrian Chadd 13109d3ddf43SAdrian Chadd flowid = m->m_pkthdr.flowid; 13119d3ddf43SAdrian Chadd flow_type = M_HASHTYPE_GET(m); 13129d3ddf43SAdrian Chadd 13139d3ddf43SAdrian Chadd if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) { 1314b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&rss_bucketid, sizeof(uint32_t), 1315b46667c6SGleb Smirnoff IP_RSSBUCKETID, IPPROTO_IP, M_NOWAIT); 13169d3ddf43SAdrian Chadd if (*mp) 13179d3ddf43SAdrian Chadd mp = &(*mp)->m_next; 13189d3ddf43SAdrian Chadd } 13199d3ddf43SAdrian Chadd } 13209d3ddf43SAdrian Chadd #endif 132182c23ebaSBill Fenner } 132282c23ebaSBill Fenner 13234d2e3692SLuigi Rizzo /* 132430916a2dSRobert Watson * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the 132530916a2dSRobert Watson * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on 132630916a2dSRobert Watson * locking. This code remains in ip_input.c as ip_mroute.c is optionally 132730916a2dSRobert Watson * compiled. 13284d2e3692SLuigi Rizzo */ 13295f901c92SAndrew Turner VNET_DEFINE_STATIC(int, ip_rsvp_on); 133082cea7e6SBjoern A. Zeeb VNET_DEFINE(struct socket *, ip_rsvpd); 133182cea7e6SBjoern A. Zeeb 133282cea7e6SBjoern A. Zeeb #define V_ip_rsvp_on VNET(ip_rsvp_on) 133382cea7e6SBjoern A. Zeeb 1334df8bae1dSRodney W. Grimes int 1335f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so) 1336f0068c4aSGarrett Wollman { 13378b615593SMarko Zec 1338603724d3SBjoern A. Zeeb if (V_ip_rsvpd != NULL) 1339f0068c4aSGarrett Wollman return EADDRINUSE; 1340f0068c4aSGarrett Wollman 1341603724d3SBjoern A. Zeeb V_ip_rsvpd = so; 13421c5de19aSGarrett Wollman /* 13431c5de19aSGarrett Wollman * This may seem silly, but we need to be sure we don't over-increment 13441c5de19aSGarrett Wollman * the RSVP counter, in case something slips up. 13451c5de19aSGarrett Wollman */ 1346603724d3SBjoern A. Zeeb if (!V_ip_rsvp_on) { 1347603724d3SBjoern A. Zeeb V_ip_rsvp_on = 1; 1348603724d3SBjoern A. Zeeb V_rsvp_on++; 13491c5de19aSGarrett Wollman } 1350f0068c4aSGarrett Wollman 1351f0068c4aSGarrett Wollman return 0; 1352f0068c4aSGarrett Wollman } 1353f0068c4aSGarrett Wollman 1354f0068c4aSGarrett Wollman int 1355f0068c4aSGarrett Wollman ip_rsvp_done(void) 1356f0068c4aSGarrett Wollman { 13578b615593SMarko Zec 1358603724d3SBjoern A. Zeeb V_ip_rsvpd = NULL; 13591c5de19aSGarrett Wollman /* 13601c5de19aSGarrett Wollman * This may seem silly, but we need to be sure we don't over-decrement 13611c5de19aSGarrett Wollman * the RSVP counter, in case something slips up. 13621c5de19aSGarrett Wollman */ 1363603724d3SBjoern A. Zeeb if (V_ip_rsvp_on) { 1364603724d3SBjoern A. Zeeb V_ip_rsvp_on = 0; 1365603724d3SBjoern A. Zeeb V_rsvp_on--; 13661c5de19aSGarrett Wollman } 1367f0068c4aSGarrett Wollman return 0; 1368f0068c4aSGarrett Wollman } 1369bbb4330bSLuigi Rizzo 13708f5a8818SKevin Lo int 13718f5a8818SKevin Lo rsvp_input(struct mbuf **mp, int *offp, int proto) 1372bbb4330bSLuigi Rizzo { 13738f5a8818SKevin Lo struct mbuf *m; 13748f5a8818SKevin Lo 13758f5a8818SKevin Lo m = *mp; 13768f5a8818SKevin Lo *mp = NULL; 13778b615593SMarko Zec 1378bbb4330bSLuigi Rizzo if (rsvp_input_p) { /* call the real one if loaded */ 13798f5a8818SKevin Lo *mp = m; 13808f5a8818SKevin Lo rsvp_input_p(mp, offp, proto); 13818f5a8818SKevin Lo return (IPPROTO_DONE); 1382bbb4330bSLuigi Rizzo } 1383bbb4330bSLuigi Rizzo 1384bbb4330bSLuigi Rizzo /* Can still get packets with rsvp_on = 0 if there is a local member 1385bbb4330bSLuigi Rizzo * of the group to which the RSVP packet is addressed. But in this 1386bbb4330bSLuigi Rizzo * case we want to throw the packet away. 1387bbb4330bSLuigi Rizzo */ 1388bbb4330bSLuigi Rizzo 1389603724d3SBjoern A. Zeeb if (!V_rsvp_on) { 1390bbb4330bSLuigi Rizzo m_freem(m); 13918f5a8818SKevin Lo return (IPPROTO_DONE); 1392bbb4330bSLuigi Rizzo } 1393bbb4330bSLuigi Rizzo 1394603724d3SBjoern A. Zeeb if (V_ip_rsvpd != NULL) { 13958f5a8818SKevin Lo *mp = m; 13968f5a8818SKevin Lo rip_input(mp, offp, proto); 13978f5a8818SKevin Lo return (IPPROTO_DONE); 1398bbb4330bSLuigi Rizzo } 1399bbb4330bSLuigi Rizzo /* Drop the packet */ 1400bbb4330bSLuigi Rizzo m_freem(m); 14018f5a8818SKevin Lo return (IPPROTO_DONE); 1402bbb4330bSLuigi Rizzo } 1403