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
kmod_ipstat_inc(int statnum)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
kmod_ipstat_dec(int statnum)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
sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)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
sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS)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
sysctl_netinet_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)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
sysctl_netinet_intr_direct_queue_drops(SYSCTL_HANDLER_ARGS)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
ip_vnet_init(void * arg __unused)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
ip_init(const void * unused __unused)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
ip_destroy(void * unused __unused)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
ip_direct_input(struct mbuf * m)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
ip_input(struct mbuf * m)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:
63940faf878SMark Johnston /*
64040faf878SMark Johnston * The unspecified address can appear only as a src address - RFC1122.
64140faf878SMark Johnston *
64240faf878SMark Johnston * The check is deferred to here to give firewalls a chance to block
64340faf878SMark Johnston * (and log) such packets. ip_tryforward() will not process such
64440faf878SMark Johnston * packets.
64540faf878SMark Johnston */
64640faf878SMark Johnston if (__predict_false(ntohl(ip->ip_dst.s_addr) == INADDR_ANY)) {
64740faf878SMark Johnston IPSTAT_INC(ips_badaddr);
64840faf878SMark Johnston goto bad;
64940faf878SMark 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 }
792532106f7SGleb Smirnoff if (in_broadcast(ip->ip_dst))
793df8bae1dSRodney W. Grimes goto ours;
79403b0505bSZhenlei Huang /* RFC 3927 2.7: Do not forward packets to or from IN_LINKLOCAL. */
7953d846e48SZhenlei Huang if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
7963d846e48SZhenlei Huang IN_LINKLOCAL(ntohl(ip->ip_src.s_addr))) {
7973d846e48SZhenlei Huang IPSTAT_INC(ips_cantforward);
7983d846e48SZhenlei Huang m_freem(m);
7993d846e48SZhenlei Huang return;
8003d846e48SZhenlei Huang }
801df8bae1dSRodney W. Grimes
8026a800098SYoshinobu Inoue /*
803df8bae1dSRodney W. Grimes * Not for us; forward if possible and desirable.
804df8bae1dSRodney W. Grimes */
805603724d3SBjoern A. Zeeb if (V_ipforwarding == 0) {
80686425c62SRobert Watson IPSTAT_INC(ips_cantforward);
807df8bae1dSRodney W. Grimes m_freem(m);
808546f251bSChris D. Faulhaber } else {
8099b932e9eSAndre Oppermann ip_forward(m, dchg);
810546f251bSChris D. Faulhaber }
811c67b1d17SGarrett Wollman return;
812df8bae1dSRodney W. Grimes
813df8bae1dSRodney W. Grimes ours:
814d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH
815d0ebc0d2SYaroslav Tykhiy /*
816d0ebc0d2SYaroslav Tykhiy * IPSTEALTH: Process non-routing options only
817d0ebc0d2SYaroslav Tykhiy * if the packet is destined for us.
818d0ebc0d2SYaroslav Tykhiy */
8197caf4ab7SGleb Smirnoff if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1))
820d0ebc0d2SYaroslav Tykhiy return;
821d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */
822d0ebc0d2SYaroslav Tykhiy
82363f8d699SJordan K. Hubbard /*
8245ab15157SDoug Rabson * We are going to ship the packet to the local protocol stack. Call the
8255ab15157SDoug Rabson * filter again for this 'output' action, allowing redirect-like rules
8265ab15157SDoug Rabson * to adjust the source address.
8275ab15157SDoug Rabson */
8285ab15157SDoug Rabson if (PFIL_HOOKED_OUT(V_inet_local_pfil_head)) {
8295ab15157SDoug Rabson if (pfil_mbuf_out(V_inet_local_pfil_head, &m, V_loif, NULL) !=
8305ab15157SDoug Rabson PFIL_PASS)
8315ab15157SDoug Rabson return;
8325ab15157SDoug Rabson ip = mtod(m, struct ip *);
8335ab15157SDoug Rabson }
8345ab15157SDoug Rabson
8355ab15157SDoug Rabson /*
836b6ea1aa5SRuslan Ermilov * Attempt reassembly; if it succeeds, proceed.
837ac9d7e26SMax Laier * ip_reass() will return a different mbuf.
838df8bae1dSRodney W. Grimes */
8398f134647SGleb Smirnoff if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
840aa69c612SGleb Smirnoff /* XXXGL: shouldn't we save & set m_flags? */
841f0cada84SAndre Oppermann m = ip_reass(m);
842f0cada84SAndre Oppermann if (m == NULL)
843c67b1d17SGarrett Wollman return;
8446a800098SYoshinobu Inoue ip = mtod(m, struct ip *);
8457e2df452SRuslan Ermilov /* Get the header length of the reassembled packet */
84653be11f6SPoul-Henning Kamp hlen = ip->ip_hl << 2;
847f0cada84SAndre Oppermann }
848f0cada84SAndre Oppermann
849fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
850fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) {
851fcf59617SAndrey V. Elsukov if (IPSEC_INPUT(ipv4, m, hlen, ip->ip_p) != 0)
852fcf59617SAndrey V. Elsukov return;
853fcf59617SAndrey V. Elsukov }
854b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
85533841545SHajimu UMEMOTO
856df8bae1dSRodney W. Grimes /*
857df8bae1dSRodney W. Grimes * Switch out to protocol's input routine.
858df8bae1dSRodney W. Grimes */
85986425c62SRobert Watson IPSTAT_INC(ips_delivered);
8609b932e9eSAndre Oppermann
86178b1fc05SGleb Smirnoff ip_protox[ip->ip_p](&m, &hlen, ip->ip_p);
862c67b1d17SGarrett Wollman return;
863df8bae1dSRodney W. Grimes bad:
864df8bae1dSRodney W. Grimes m_freem(m);
865c67b1d17SGarrett Wollman }
866c67b1d17SGarrett Wollman
867de38924dSAndre Oppermann int
ipproto_register(uint8_t proto,ipproto_input_t input,ipproto_ctlinput_t ctl)86878b1fc05SGleb Smirnoff ipproto_register(uint8_t proto, ipproto_input_t input, ipproto_ctlinput_t ctl)
869de38924dSAndre Oppermann {
870de38924dSAndre Oppermann
87178b1fc05SGleb Smirnoff MPASS(proto > 0);
872de38924dSAndre Oppermann
873de38924dSAndre Oppermann /*
874de38924dSAndre Oppermann * The protocol slot must not be occupied by another protocol
87578b1fc05SGleb Smirnoff * already. An index pointing to rip_input() is unused.
876de38924dSAndre Oppermann */
87778b1fc05SGleb Smirnoff if (ip_protox[proto] == rip_input) {
87878b1fc05SGleb Smirnoff ip_protox[proto] = input;
87978b1fc05SGleb Smirnoff ip_ctlprotox[proto] = ctl;
880de38924dSAndre Oppermann return (0);
88178b1fc05SGleb Smirnoff } else
88278b1fc05SGleb Smirnoff return (EEXIST);
883de38924dSAndre Oppermann }
884de38924dSAndre Oppermann
885de38924dSAndre Oppermann int
ipproto_unregister(uint8_t proto)88678b1fc05SGleb Smirnoff ipproto_unregister(uint8_t proto)
887de38924dSAndre Oppermann {
888de38924dSAndre Oppermann
88978b1fc05SGleb Smirnoff MPASS(proto > 0);
890de38924dSAndre Oppermann
89178b1fc05SGleb Smirnoff if (ip_protox[proto] != rip_input) {
89278b1fc05SGleb Smirnoff ip_protox[proto] = rip_input;
89378b1fc05SGleb Smirnoff ip_ctlprotox[proto] = rip_ctlinput;
894de38924dSAndre Oppermann return (0);
89578b1fc05SGleb Smirnoff } else
89678b1fc05SGleb Smirnoff return (ENOENT);
897de38924dSAndre Oppermann }
898de38924dSAndre Oppermann
899df8bae1dSRodney W. Grimes /*
900df8bae1dSRodney W. Grimes * Forward a packet. If some error occurs return the sender
901df8bae1dSRodney W. Grimes * an icmp packet. Note we can't always generate a meaningful
902df8bae1dSRodney W. Grimes * icmp message because icmp doesn't have a large enough repertoire
903df8bae1dSRodney W. Grimes * of codes and types.
904df8bae1dSRodney W. Grimes *
905df8bae1dSRodney W. Grimes * If not forwarding, just drop the packet. This could be confusing
906df8bae1dSRodney W. Grimes * if ipforwarding was zero but some routing protocol was advancing
907df8bae1dSRodney W. Grimes * us as a gateway to somewhere. However, we must let the routing
908df8bae1dSRodney W. Grimes * protocol deal with that.
909df8bae1dSRodney W. Grimes *
910df8bae1dSRodney W. Grimes * The srcrt parameter indicates whether the packet is being forwarded
911df8bae1dSRodney W. Grimes * via a source route.
912df8bae1dSRodney W. Grimes */
9139b932e9eSAndre Oppermann void
ip_forward(struct mbuf * m,int srcrt)9149b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt)
915df8bae1dSRodney W. Grimes {
9162b25acc1SLuigi Rizzo struct ip *ip = mtod(m, struct ip *);
917efbad259SEdward Tomasz Napierala struct in_ifaddr *ia;
918df8bae1dSRodney W. Grimes struct mbuf *mcopy;
919d14122b0SErmal Luçi struct sockaddr_in *sin;
9209b932e9eSAndre Oppermann struct in_addr dest;
921b835b6feSBjoern A. Zeeb struct route ro;
9224043ee3cSAlexander V. Chernikov uint32_t flowid;
923c773494eSAndre Oppermann int error, type = 0, code = 0, mtu = 0;
9243efc3014SJulian Elischer
925b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT();
926b8a6e03fSGleb Smirnoff
927*3ae7c763SZhenlei Huang if (m->m_flags & (M_BCAST|M_MCAST) || !in_canforward(ip->ip_dst)) {
92886425c62SRobert Watson IPSTAT_INC(ips_cantforward);
929df8bae1dSRodney W. Grimes m_freem(m);
930df8bae1dSRodney W. Grimes return;
931df8bae1dSRodney W. Grimes }
932fcf59617SAndrey V. Elsukov if (
933fcf59617SAndrey V. Elsukov #ifdef IPSTEALTH
934fcf59617SAndrey V. Elsukov V_ipstealth == 0 &&
935fcf59617SAndrey V. Elsukov #endif
936fcf59617SAndrey V. Elsukov ip->ip_ttl <= IPTTLDEC) {
937fcf59617SAndrey V. Elsukov icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0);
9388922ddbeSAndrey V. Elsukov return;
9398922ddbeSAndrey V. Elsukov }
940df8bae1dSRodney W. Grimes
941d14122b0SErmal Luçi bzero(&ro, sizeof(ro));
942d14122b0SErmal Luçi sin = (struct sockaddr_in *)&ro.ro_dst;
943d14122b0SErmal Luçi sin->sin_family = AF_INET;
944d14122b0SErmal Luçi sin->sin_len = sizeof(*sin);
945d14122b0SErmal Luçi sin->sin_addr = ip->ip_dst;
9464043ee3cSAlexander V. Chernikov flowid = m->m_pkthdr.flowid;
9474043ee3cSAlexander V. Chernikov ro.ro_nh = fib4_lookup(M_GETFIB(m), ip->ip_dst, 0, NHR_REF, flowid);
948983066f0SAlexander V. Chernikov if (ro.ro_nh != NULL) {
949e27970aeSKristof Provost if (ro.ro_nh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST)) {
950e27970aeSKristof Provost IPSTAT_INC(ips_cantforward);
951e27970aeSKristof Provost m_freem(m);
952e27970aeSKristof Provost NH_FREE(ro.ro_nh);
953e27970aeSKristof Provost return;
954e27970aeSKristof Provost }
955e27970aeSKristof Provost if (ro.ro_nh->nh_flags & NHF_REJECT) {
956e27970aeSKristof Provost IPSTAT_INC(ips_cantforward);
957e27970aeSKristof Provost NH_FREE(ro.ro_nh);
958e27970aeSKristof Provost icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
959e27970aeSKristof Provost return;
960e27970aeSKristof Provost }
961983066f0SAlexander V. Chernikov ia = ifatoia(ro.ro_nh->nh_ifa);
96256844a62SErmal Luçi } else
96356844a62SErmal Luçi ia = NULL;
964df8bae1dSRodney W. Grimes /*
965bfef7ed4SIan Dowse * Save the IP header and at most 8 bytes of the payload,
966bfef7ed4SIan Dowse * in case we need to generate an ICMP message to the src.
967bfef7ed4SIan Dowse *
9684d2e3692SLuigi Rizzo * XXX this can be optimized a lot by saving the data in a local
9694d2e3692SLuigi Rizzo * buffer on the stack (72 bytes at most), and only allocating the
9704d2e3692SLuigi Rizzo * mbuf if really necessary. The vast majority of the packets
9714d2e3692SLuigi Rizzo * are forwarded without having to send an ICMP back (either
9724d2e3692SLuigi Rizzo * because unnecessary, or because rate limited), so we are
9734d2e3692SLuigi Rizzo * really we are wasting a lot of work here.
9744d2e3692SLuigi Rizzo *
975c3bef61eSKevin Lo * We don't use m_copym() because it might return a reference
976bfef7ed4SIan Dowse * to a shared cluster. Both this function and ip_output()
977bfef7ed4SIan Dowse * assume exclusive access to the IP header in `m', so any
978bfef7ed4SIan Dowse * data in a cluster may change before we reach icmp_error().
979df8bae1dSRodney W. Grimes */
980dc4ad05eSGleb Smirnoff mcopy = m_gethdr(M_NOWAIT, m->m_type);
981eb1b1807SGleb Smirnoff if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) {
9829967cafcSSam Leffler /*
9839967cafcSSam Leffler * It's probably ok if the pkthdr dup fails (because
9849967cafcSSam Leffler * the deep copy of the tag chain failed), but for now
9859967cafcSSam Leffler * be conservative and just discard the copy since
9869967cafcSSam Leffler * code below may some day want the tags.
9879967cafcSSam Leffler */
9889967cafcSSam Leffler m_free(mcopy);
9899967cafcSSam Leffler mcopy = NULL;
9909967cafcSSam Leffler }
991bfef7ed4SIan Dowse if (mcopy != NULL) {
9928f134647SGleb Smirnoff mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy));
993e6b0a570SBruce M Simpson mcopy->m_pkthdr.len = mcopy->m_len;
994bfef7ed4SIan Dowse m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
995bfef7ed4SIan Dowse }
99604287599SRuslan Ermilov #ifdef IPSTEALTH
997fcf59617SAndrey V. Elsukov if (V_ipstealth == 0)
99804287599SRuslan Ermilov #endif
99904287599SRuslan Ermilov ip->ip_ttl -= IPTTLDEC;
1000fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1001fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) {
1002fcf59617SAndrey V. Elsukov if ((error = IPSEC_FORWARD(ipv4, m)) != 0) {
1003fcf59617SAndrey V. Elsukov /* mbuf consumed by IPsec */
1004d16a2e47SMark Johnston RO_NHFREE(&ro);
1005fcf59617SAndrey V. Elsukov m_freem(mcopy);
1006fcf59617SAndrey V. Elsukov if (error != EINPROGRESS)
1007fcf59617SAndrey V. Elsukov IPSTAT_INC(ips_cantforward);
1008b8a6e03fSGleb Smirnoff return;
100904287599SRuslan Ermilov }
1010fcf59617SAndrey V. Elsukov /* No IPsec processing required */
1011fcf59617SAndrey V. Elsukov }
1012fcf59617SAndrey V. Elsukov #endif /* IPSEC */
1013df8bae1dSRodney W. Grimes /*
1014df8bae1dSRodney W. Grimes * If forwarding packet using same interface that it came in on,
1015df8bae1dSRodney W. Grimes * perhaps should send a redirect to sender to shortcut a hop.
1016df8bae1dSRodney W. Grimes * Only send redirect if source is sending directly to us,
1017df8bae1dSRodney W. Grimes * and if packet was not source routed (or has any options).
1018df8bae1dSRodney W. Grimes * Also, don't send redirect if forwarding using a default route
1019df8bae1dSRodney W. Grimes * or a route modified by a redirect.
1020df8bae1dSRodney W. Grimes */
10219b932e9eSAndre Oppermann dest.s_addr = 0;
1022efbad259SEdward Tomasz Napierala if (!srcrt && V_ipsendredirects &&
1023efbad259SEdward Tomasz Napierala ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) {
1024983066f0SAlexander V. Chernikov struct nhop_object *nh;
102502c1c707SAndre Oppermann
1026983066f0SAlexander V. Chernikov nh = ro.ro_nh;
102702c1c707SAndre Oppermann
1028983066f0SAlexander V. Chernikov if (nh != NULL && ((nh->nh_flags & (NHF_REDIRECT|NHF_DEFAULT)) == 0)) {
1029983066f0SAlexander V. Chernikov struct in_ifaddr *nh_ia = (struct in_ifaddr *)(nh->nh_ifa);
1030df8bae1dSRodney W. Grimes u_long src = ntohl(ip->ip_src.s_addr);
1031df8bae1dSRodney W. Grimes
1032983066f0SAlexander V. Chernikov if (nh_ia != NULL &&
1033983066f0SAlexander V. Chernikov (src & nh_ia->ia_subnetmask) == nh_ia->ia_subnet) {
1034df8bae1dSRodney W. Grimes /* Router requirements says to only send host redirects */
1035df8bae1dSRodney W. Grimes type = ICMP_REDIRECT;
1036df8bae1dSRodney W. Grimes code = ICMP_REDIRECT_HOST;
103762e1a437SZhenlei Huang if (nh->nh_flags & NHF_GATEWAY) {
103862e1a437SZhenlei Huang if (nh->gw_sa.sa_family == AF_INET)
103962e1a437SZhenlei Huang dest.s_addr = nh->gw4_sa.sin_addr.s_addr;
104062e1a437SZhenlei Huang else /* Do not redirect in case gw is AF_INET6 */
104162e1a437SZhenlei Huang type = 0;
104262e1a437SZhenlei Huang } else
104362e1a437SZhenlei Huang dest.s_addr = ip->ip_dst.s_addr;
1044df8bae1dSRodney W. Grimes }
1045df8bae1dSRodney W. Grimes }
104602c1c707SAndre Oppermann }
1047df8bae1dSRodney W. Grimes
1048b835b6feSBjoern A. Zeeb error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1049b835b6feSBjoern A. Zeeb
1050983066f0SAlexander V. Chernikov if (error == EMSGSIZE && ro.ro_nh)
1051983066f0SAlexander V. Chernikov mtu = ro.ro_nh->nh_mtu;
1052983066f0SAlexander V. Chernikov RO_NHFREE(&ro);
1053b835b6feSBjoern A. Zeeb
1054df8bae1dSRodney W. Grimes if (error)
105586425c62SRobert Watson IPSTAT_INC(ips_cantforward);
1056df8bae1dSRodney W. Grimes else {
105786425c62SRobert Watson IPSTAT_INC(ips_forward);
1058df8bae1dSRodney W. Grimes if (type)
105986425c62SRobert Watson IPSTAT_INC(ips_redirectsent);
1060df8bae1dSRodney W. Grimes else {
10619188b4a1SAndre Oppermann if (mcopy)
1062df8bae1dSRodney W. Grimes m_freem(mcopy);
1063b8a6e03fSGleb Smirnoff return;
1064df8bae1dSRodney W. Grimes }
1065df8bae1dSRodney W. Grimes }
10664f6c66ccSMatt Macy if (mcopy == NULL)
1067b8a6e03fSGleb Smirnoff return;
10684f6c66ccSMatt Macy
1069df8bae1dSRodney W. Grimes switch (error) {
1070df8bae1dSRodney W. Grimes case 0: /* forwarded, but need redirect */
1071df8bae1dSRodney W. Grimes /* type, code set above */
1072df8bae1dSRodney W. Grimes break;
1073df8bae1dSRodney W. Grimes
1074efbad259SEdward Tomasz Napierala case ENETUNREACH:
1075df8bae1dSRodney W. Grimes case EHOSTUNREACH:
1076df8bae1dSRodney W. Grimes case ENETDOWN:
1077df8bae1dSRodney W. Grimes case EHOSTDOWN:
1078df8bae1dSRodney W. Grimes default:
1079df8bae1dSRodney W. Grimes type = ICMP_UNREACH;
1080df8bae1dSRodney W. Grimes code = ICMP_UNREACH_HOST;
1081df8bae1dSRodney W. Grimes break;
1082df8bae1dSRodney W. Grimes
1083df8bae1dSRodney W. Grimes case EMSGSIZE:
1084df8bae1dSRodney W. Grimes type = ICMP_UNREACH;
1085df8bae1dSRodney W. Grimes code = ICMP_UNREACH_NEEDFRAG;
10869b932e9eSAndre Oppermann /*
1087b835b6feSBjoern A. Zeeb * If the MTU was set before make sure we are below the
1088b835b6feSBjoern A. Zeeb * interface MTU.
1089ab48768bSAndre Oppermann * If the MTU wasn't set before use the interface mtu or
1090ab48768bSAndre Oppermann * fall back to the next smaller mtu step compared to the
1091ab48768bSAndre Oppermann * current packet size.
10929b932e9eSAndre Oppermann */
1093b835b6feSBjoern A. Zeeb if (mtu != 0) {
1094b835b6feSBjoern A. Zeeb if (ia != NULL)
1095b835b6feSBjoern A. Zeeb mtu = min(mtu, ia->ia_ifp->if_mtu);
1096b835b6feSBjoern A. Zeeb } else {
1097ab48768bSAndre Oppermann if (ia != NULL)
1098c773494eSAndre Oppermann mtu = ia->ia_ifp->if_mtu;
1099ab48768bSAndre Oppermann else
11008f134647SGleb Smirnoff mtu = ip_next_mtu(ntohs(ip->ip_len), 0);
1101ab48768bSAndre Oppermann }
110286425c62SRobert Watson IPSTAT_INC(ips_cantfrag);
1103df8bae1dSRodney W. Grimes break;
1104df8bae1dSRodney W. Grimes
1105df8bae1dSRodney W. Grimes case ENOBUFS:
11063a06e3e0SRuslan Ermilov case EACCES: /* ipfw denied packet */
11073a06e3e0SRuslan Ermilov m_freem(mcopy);
1108b8a6e03fSGleb Smirnoff return;
1109df8bae1dSRodney W. Grimes }
1110c773494eSAndre Oppermann icmp_error(mcopy, type, code, dest.s_addr, mtu);
1111df8bae1dSRodney W. Grimes }
1112df8bae1dSRodney W. Grimes
1113339efd75SMaxim Sobolev #define CHECK_SO_CT(sp, ct) \
1114339efd75SMaxim Sobolev (((sp->so_options & SO_TIMESTAMP) && (sp->so_ts_clock == ct)) ? 1 : 0)
1115339efd75SMaxim Sobolev
111682c23ebaSBill Fenner void
ip_savecontrol(struct inpcb * inp,struct mbuf ** mp,struct ip * ip,struct mbuf * m)1117f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1118f2565d68SRobert Watson struct mbuf *m)
111982c23ebaSBill Fenner {
112006193f0bSKonstantin Belousov bool stamped;
11218b615593SMarko Zec
112206193f0bSKonstantin Belousov stamped = false;
1123339efd75SMaxim Sobolev if ((inp->inp_socket->so_options & SO_BINTIME) ||
1124339efd75SMaxim Sobolev CHECK_SO_CT(inp->inp_socket, SO_TS_BINTIME)) {
112506193f0bSKonstantin Belousov struct bintime boottimebin, bt;
112606193f0bSKonstantin Belousov struct timespec ts1;
1127be8a62e8SPoul-Henning Kamp
112806193f0bSKonstantin Belousov if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
112906193f0bSKonstantin Belousov M_TSTMP)) {
113006193f0bSKonstantin Belousov mbuf_tstmp2timespec(m, &ts1);
113106193f0bSKonstantin Belousov timespec2bintime(&ts1, &bt);
113206193f0bSKonstantin Belousov getboottimebin(&boottimebin);
113306193f0bSKonstantin Belousov bintime_add(&bt, &boottimebin);
113406193f0bSKonstantin Belousov } else {
1135be8a62e8SPoul-Henning Kamp bintime(&bt);
113606193f0bSKonstantin Belousov }
1137b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&bt, sizeof(bt), SCM_BINTIME,
1138b46667c6SGleb Smirnoff SOL_SOCKET, M_NOWAIT);
113906193f0bSKonstantin Belousov if (*mp != NULL) {
1140be8a62e8SPoul-Henning Kamp mp = &(*mp)->m_next;
114106193f0bSKonstantin Belousov stamped = true;
114206193f0bSKonstantin Belousov }
1143be8a62e8SPoul-Henning Kamp }
1144339efd75SMaxim Sobolev if (CHECK_SO_CT(inp->inp_socket, SO_TS_REALTIME_MICRO)) {
114506193f0bSKonstantin Belousov struct bintime boottimebin, bt1;
1146c012cfe6SEd Maste struct timespec ts1;
114782c23ebaSBill Fenner struct timeval tv;
114882c23ebaSBill Fenner
114906193f0bSKonstantin Belousov if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
115006193f0bSKonstantin Belousov M_TSTMP)) {
115106193f0bSKonstantin Belousov mbuf_tstmp2timespec(m, &ts1);
115206193f0bSKonstantin Belousov timespec2bintime(&ts1, &bt1);
115306193f0bSKonstantin Belousov getboottimebin(&boottimebin);
115406193f0bSKonstantin Belousov bintime_add(&bt1, &boottimebin);
115506193f0bSKonstantin Belousov bintime2timeval(&bt1, &tv);
115606193f0bSKonstantin Belousov } else {
1157339efd75SMaxim Sobolev microtime(&tv);
115806193f0bSKonstantin Belousov }
1159b46667c6SGleb Smirnoff *mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv), SCM_TIMESTAMP,
1160b46667c6SGleb Smirnoff SOL_SOCKET, M_NOWAIT);
116106193f0bSKonstantin Belousov if (*mp != NULL) {
116282c23ebaSBill Fenner mp = &(*mp)->m_next;
116306193f0bSKonstantin Belousov stamped = true;
116406193f0bSKonstantin Belousov }
1165339efd75SMaxim Sobolev } else if (CHECK_SO_CT(inp->inp_socket, SO_TS_REALTIME)) {
116606193f0bSKonstantin Belousov struct bintime boottimebin;
116706193f0bSKonstantin Belousov struct timespec ts, ts1;
1168339efd75SMaxim Sobolev
116906193f0bSKonstantin Belousov if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
117006193f0bSKonstantin Belousov M_TSTMP)) {
117106193f0bSKonstantin Belousov mbuf_tstmp2timespec(m, &ts);
117206193f0bSKonstantin Belousov getboottimebin(&boottimebin);
117306193f0bSKonstantin Belousov bintime2timespec(&boottimebin, &ts1);
11746040822cSAlan Somers timespecadd(&ts, &ts1, &ts);
117506193f0bSKonstantin Belousov } else {
1176339efd75SMaxim Sobolev nanotime(&ts);
117706193f0bSKonstantin Belousov }
1178b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&ts, sizeof(ts), SCM_REALTIME,
1179b46667c6SGleb Smirnoff SOL_SOCKET, M_NOWAIT);
118006193f0bSKonstantin Belousov if (*mp != NULL) {
1181339efd75SMaxim Sobolev mp = &(*mp)->m_next;
118206193f0bSKonstantin Belousov stamped = true;
118306193f0bSKonstantin Belousov }
1184339efd75SMaxim Sobolev } else if (CHECK_SO_CT(inp->inp_socket, SO_TS_MONOTONIC)) {
1185339efd75SMaxim Sobolev struct timespec ts;
1186339efd75SMaxim Sobolev
118706193f0bSKonstantin Belousov if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
118806193f0bSKonstantin Belousov M_TSTMP))
118906193f0bSKonstantin Belousov mbuf_tstmp2timespec(m, &ts);
119006193f0bSKonstantin Belousov else
1191339efd75SMaxim Sobolev nanouptime(&ts);
1192b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&ts, sizeof(ts), SCM_MONOTONIC,
1193b46667c6SGleb Smirnoff SOL_SOCKET, M_NOWAIT);
119406193f0bSKonstantin Belousov if (*mp != NULL) {
119506193f0bSKonstantin Belousov mp = &(*mp)->m_next;
119606193f0bSKonstantin Belousov stamped = true;
119706193f0bSKonstantin Belousov }
119806193f0bSKonstantin Belousov }
119906193f0bSKonstantin Belousov if (stamped && (m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
120006193f0bSKonstantin Belousov M_TSTMP)) {
120106193f0bSKonstantin Belousov struct sock_timestamp_info sti;
120206193f0bSKonstantin Belousov
120306193f0bSKonstantin Belousov bzero(&sti, sizeof(sti));
120406193f0bSKonstantin Belousov sti.st_info_flags = ST_INFO_HW;
120506193f0bSKonstantin Belousov if ((m->m_flags & M_TSTMP_HPREC) != 0)
120606193f0bSKonstantin Belousov sti.st_info_flags |= ST_INFO_HW_HPREC;
1207b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&sti, sizeof(sti), SCM_TIME_INFO,
1208b46667c6SGleb Smirnoff SOL_SOCKET, M_NOWAIT);
120906193f0bSKonstantin Belousov if (*mp != NULL)
1210339efd75SMaxim Sobolev mp = &(*mp)->m_next;
1211be8a62e8SPoul-Henning Kamp }
121282c23ebaSBill Fenner if (inp->inp_flags & INP_RECVDSTADDR) {
1213b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&ip->ip_dst, sizeof(struct in_addr),
1214b46667c6SGleb Smirnoff IP_RECVDSTADDR, IPPROTO_IP, M_NOWAIT);
121582c23ebaSBill Fenner if (*mp)
121682c23ebaSBill Fenner mp = &(*mp)->m_next;
121782c23ebaSBill Fenner }
12184957466bSMatthew N. Dodd if (inp->inp_flags & INP_RECVTTL) {
1219b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&ip->ip_ttl, sizeof(u_char), IP_RECVTTL,
1220b46667c6SGleb Smirnoff IPPROTO_IP, M_NOWAIT);
12214957466bSMatthew N. Dodd if (*mp)
12224957466bSMatthew N. Dodd mp = &(*mp)->m_next;
12234957466bSMatthew N. Dodd }
122482c23ebaSBill Fenner #ifdef notyet
122582c23ebaSBill Fenner /* XXX
122682c23ebaSBill Fenner * Moving these out of udp_input() made them even more broken
122782c23ebaSBill Fenner * than they already were.
122882c23ebaSBill Fenner */
122982c23ebaSBill Fenner /* options were tossed already */
123082c23ebaSBill Fenner if (inp->inp_flags & INP_RECVOPTS) {
1231b46667c6SGleb Smirnoff *mp = sbcreatecontrol(opts_deleted_above,
1232b46667c6SGleb Smirnoff sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP, M_NOWAIT);
123382c23ebaSBill Fenner if (*mp)
123482c23ebaSBill Fenner mp = &(*mp)->m_next;
123582c23ebaSBill Fenner }
123682c23ebaSBill Fenner /* ip_srcroute doesn't do what we want here, need to fix */
123782c23ebaSBill Fenner if (inp->inp_flags & INP_RECVRETOPTS) {
1238b46667c6SGleb Smirnoff *mp = sbcreatecontrol(ip_srcroute(m), sizeof(struct in_addr),
1239b46667c6SGleb Smirnoff IP_RECVRETOPTS, IPPROTO_IP, M_NOWAIT);
124082c23ebaSBill Fenner if (*mp)
124182c23ebaSBill Fenner mp = &(*mp)->m_next;
124282c23ebaSBill Fenner }
124382c23ebaSBill Fenner #endif
124482c23ebaSBill Fenner if (inp->inp_flags & INP_RECVIF) {
1245d314ad7bSJulian Elischer struct ifnet *ifp;
1246d314ad7bSJulian Elischer struct sdlbuf {
124782c23ebaSBill Fenner struct sockaddr_dl sdl;
1248d314ad7bSJulian Elischer u_char pad[32];
1249d314ad7bSJulian Elischer } sdlbuf;
1250d314ad7bSJulian Elischer struct sockaddr_dl *sdp;
1251d314ad7bSJulian Elischer struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
125282c23ebaSBill Fenner
1253db0ac6deSCy Schubert if ((ifp = m->m_pkthdr.rcvif)) {
12544a0d6638SRuslan Ermilov sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1255d314ad7bSJulian Elischer /*
1256d314ad7bSJulian Elischer * Change our mind and don't try copy.
1257d314ad7bSJulian Elischer */
125846f2df9cSSergey Kandaurov if (sdp->sdl_family != AF_LINK ||
125946f2df9cSSergey Kandaurov sdp->sdl_len > sizeof(sdlbuf)) {
1260d314ad7bSJulian Elischer goto makedummy;
1261d314ad7bSJulian Elischer }
1262d314ad7bSJulian Elischer bcopy(sdp, sdl2, sdp->sdl_len);
1263d314ad7bSJulian Elischer } else {
1264d314ad7bSJulian Elischer makedummy:
126546f2df9cSSergey Kandaurov sdl2->sdl_len =
126646f2df9cSSergey Kandaurov offsetof(struct sockaddr_dl, sdl_data[0]);
1267d314ad7bSJulian Elischer sdl2->sdl_family = AF_LINK;
1268d314ad7bSJulian Elischer sdl2->sdl_index = 0;
1269d314ad7bSJulian Elischer sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1270d314ad7bSJulian Elischer }
1271b46667c6SGleb Smirnoff *mp = sbcreatecontrol(sdl2, sdl2->sdl_len, IP_RECVIF,
1272b46667c6SGleb Smirnoff IPPROTO_IP, M_NOWAIT);
127382c23ebaSBill Fenner if (*mp)
127482c23ebaSBill Fenner mp = &(*mp)->m_next;
127582c23ebaSBill Fenner }
12763cca425bSMichael Tuexen if (inp->inp_flags & INP_RECVTOS) {
1277b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&ip->ip_tos, sizeof(u_char), IP_RECVTOS,
1278b46667c6SGleb Smirnoff IPPROTO_IP, M_NOWAIT);
12793cca425bSMichael Tuexen if (*mp)
12803cca425bSMichael Tuexen mp = &(*mp)->m_next;
12813cca425bSMichael Tuexen }
12829d3ddf43SAdrian Chadd
12839d3ddf43SAdrian Chadd if (inp->inp_flags2 & INP_RECVFLOWID) {
12849d3ddf43SAdrian Chadd uint32_t flowid, flow_type;
12859d3ddf43SAdrian Chadd
12869d3ddf43SAdrian Chadd flowid = m->m_pkthdr.flowid;
12879d3ddf43SAdrian Chadd flow_type = M_HASHTYPE_GET(m);
12889d3ddf43SAdrian Chadd
12899d3ddf43SAdrian Chadd /*
12909d3ddf43SAdrian Chadd * XXX should handle the failure of one or the
12919d3ddf43SAdrian Chadd * other - don't populate both?
12929d3ddf43SAdrian Chadd */
1293b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&flowid, sizeof(uint32_t), IP_FLOWID,
1294b46667c6SGleb Smirnoff IPPROTO_IP, M_NOWAIT);
12959d3ddf43SAdrian Chadd if (*mp)
12969d3ddf43SAdrian Chadd mp = &(*mp)->m_next;
1297b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&flow_type, sizeof(uint32_t),
1298b46667c6SGleb Smirnoff IP_FLOWTYPE, IPPROTO_IP, M_NOWAIT);
12999d3ddf43SAdrian Chadd if (*mp)
13009d3ddf43SAdrian Chadd mp = &(*mp)->m_next;
13019d3ddf43SAdrian Chadd }
13029d3ddf43SAdrian Chadd
13039d3ddf43SAdrian Chadd #ifdef RSS
13049d3ddf43SAdrian Chadd if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
13059d3ddf43SAdrian Chadd uint32_t flowid, flow_type;
13069d3ddf43SAdrian Chadd uint32_t rss_bucketid;
13079d3ddf43SAdrian Chadd
13089d3ddf43SAdrian Chadd flowid = m->m_pkthdr.flowid;
13099d3ddf43SAdrian Chadd flow_type = M_HASHTYPE_GET(m);
13109d3ddf43SAdrian Chadd
13119d3ddf43SAdrian Chadd if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
1312b46667c6SGleb Smirnoff *mp = sbcreatecontrol(&rss_bucketid, sizeof(uint32_t),
1313b46667c6SGleb Smirnoff IP_RSSBUCKETID, IPPROTO_IP, M_NOWAIT);
13149d3ddf43SAdrian Chadd if (*mp)
13159d3ddf43SAdrian Chadd mp = &(*mp)->m_next;
13169d3ddf43SAdrian Chadd }
13179d3ddf43SAdrian Chadd }
13189d3ddf43SAdrian Chadd #endif
131982c23ebaSBill Fenner }
132082c23ebaSBill Fenner
13214d2e3692SLuigi Rizzo /*
132230916a2dSRobert Watson * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
132330916a2dSRobert Watson * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
132430916a2dSRobert Watson * locking. This code remains in ip_input.c as ip_mroute.c is optionally
132530916a2dSRobert Watson * compiled.
13264d2e3692SLuigi Rizzo */
13275f901c92SAndrew Turner VNET_DEFINE_STATIC(int, ip_rsvp_on);
132882cea7e6SBjoern A. Zeeb VNET_DEFINE(struct socket *, ip_rsvpd);
132982cea7e6SBjoern A. Zeeb
133082cea7e6SBjoern A. Zeeb #define V_ip_rsvp_on VNET(ip_rsvp_on)
133182cea7e6SBjoern A. Zeeb
1332df8bae1dSRodney W. Grimes int
ip_rsvp_init(struct socket * so)1333f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so)
1334f0068c4aSGarrett Wollman {
13358b615593SMarko Zec
1336603724d3SBjoern A. Zeeb if (V_ip_rsvpd != NULL)
1337f0068c4aSGarrett Wollman return EADDRINUSE;
1338f0068c4aSGarrett Wollman
1339603724d3SBjoern A. Zeeb V_ip_rsvpd = so;
13401c5de19aSGarrett Wollman /*
13411c5de19aSGarrett Wollman * This may seem silly, but we need to be sure we don't over-increment
13421c5de19aSGarrett Wollman * the RSVP counter, in case something slips up.
13431c5de19aSGarrett Wollman */
1344603724d3SBjoern A. Zeeb if (!V_ip_rsvp_on) {
1345603724d3SBjoern A. Zeeb V_ip_rsvp_on = 1;
1346603724d3SBjoern A. Zeeb V_rsvp_on++;
13471c5de19aSGarrett Wollman }
1348f0068c4aSGarrett Wollman
1349f0068c4aSGarrett Wollman return 0;
1350f0068c4aSGarrett Wollman }
1351f0068c4aSGarrett Wollman
1352f0068c4aSGarrett Wollman int
ip_rsvp_done(void)1353f0068c4aSGarrett Wollman ip_rsvp_done(void)
1354f0068c4aSGarrett Wollman {
13558b615593SMarko Zec
1356603724d3SBjoern A. Zeeb V_ip_rsvpd = NULL;
13571c5de19aSGarrett Wollman /*
13581c5de19aSGarrett Wollman * This may seem silly, but we need to be sure we don't over-decrement
13591c5de19aSGarrett Wollman * the RSVP counter, in case something slips up.
13601c5de19aSGarrett Wollman */
1361603724d3SBjoern A. Zeeb if (V_ip_rsvp_on) {
1362603724d3SBjoern A. Zeeb V_ip_rsvp_on = 0;
1363603724d3SBjoern A. Zeeb V_rsvp_on--;
13641c5de19aSGarrett Wollman }
1365f0068c4aSGarrett Wollman return 0;
1366f0068c4aSGarrett Wollman }
1367bbb4330bSLuigi Rizzo
13688f5a8818SKevin Lo int
rsvp_input(struct mbuf ** mp,int * offp,int proto)13698f5a8818SKevin Lo rsvp_input(struct mbuf **mp, int *offp, int proto)
1370bbb4330bSLuigi Rizzo {
13718f5a8818SKevin Lo struct mbuf *m;
13728f5a8818SKevin Lo
13738f5a8818SKevin Lo m = *mp;
13748f5a8818SKevin Lo *mp = NULL;
13758b615593SMarko Zec
1376bbb4330bSLuigi Rizzo if (rsvp_input_p) { /* call the real one if loaded */
13778f5a8818SKevin Lo *mp = m;
13788f5a8818SKevin Lo rsvp_input_p(mp, offp, proto);
13798f5a8818SKevin Lo return (IPPROTO_DONE);
1380bbb4330bSLuigi Rizzo }
1381bbb4330bSLuigi Rizzo
1382bbb4330bSLuigi Rizzo /* Can still get packets with rsvp_on = 0 if there is a local member
1383bbb4330bSLuigi Rizzo * of the group to which the RSVP packet is addressed. But in this
1384bbb4330bSLuigi Rizzo * case we want to throw the packet away.
1385bbb4330bSLuigi Rizzo */
1386bbb4330bSLuigi Rizzo
1387603724d3SBjoern A. Zeeb if (!V_rsvp_on) {
1388bbb4330bSLuigi Rizzo m_freem(m);
13898f5a8818SKevin Lo return (IPPROTO_DONE);
1390bbb4330bSLuigi Rizzo }
1391bbb4330bSLuigi Rizzo
1392603724d3SBjoern A. Zeeb if (V_ip_rsvpd != NULL) {
13938f5a8818SKevin Lo *mp = m;
13948f5a8818SKevin Lo rip_input(mp, offp, proto);
13958f5a8818SKevin Lo return (IPPROTO_DONE);
1396bbb4330bSLuigi Rizzo }
1397bbb4330bSLuigi Rizzo /* Drop the packet */
1398bbb4330bSLuigi Rizzo m_freem(m);
13998f5a8818SKevin Lo return (IPPROTO_DONE);
1400bbb4330bSLuigi Rizzo }
1401