xref: /freebsd/sys/netinet/ip_input.c (revision 9d3ddf438402c8e711dd639be6052d51cb45b680)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
324b421e2dSMike Silbersack #include <sys/cdefs.h>
334b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
344b421e2dSMike Silbersack 
350ac40133SBrian Somers #include "opt_bootp.h"
3674a9466cSGary Palmer #include "opt_ipfw.h"
3727108a15SDag-Erling Smørgrav #include "opt_ipstealth.h"
386a800098SYoshinobu Inoue #include "opt_ipsec.h"
3933553d6eSBjoern A. Zeeb #include "opt_route.h"
4074a9466cSGary Palmer 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
44b715f178SLuigi Rizzo #include <sys/malloc.h>
45df8bae1dSRodney W. Grimes #include <sys/domain.h>
46df8bae1dSRodney W. Grimes #include <sys/protosw.h>
47df8bae1dSRodney W. Grimes #include <sys/socket.h>
48df8bae1dSRodney W. Grimes #include <sys/time.h>
49df8bae1dSRodney W. Grimes #include <sys/kernel.h>
50385195c0SMarko Zec #include <sys/lock.h>
51385195c0SMarko Zec #include <sys/rwlock.h>
5257f60867SMark Johnston #include <sys/sdt.h>
531025071fSGarrett Wollman #include <sys/syslog.h>
54b5e8ce9fSBruce Evans #include <sys/sysctl.h>
55df8bae1dSRodney W. Grimes 
56c85540ddSAndrey A. Chernov #include <net/pfil.h>
57df8bae1dSRodney W. Grimes #include <net/if.h>
589494d596SBrooks Davis #include <net/if_types.h>
59d314ad7bSJulian Elischer #include <net/if_var.h>
6082c23ebaSBill Fenner #include <net/if_dl.h>
61df8bae1dSRodney W. Grimes #include <net/route.h>
62748e0b0aSGarrett Wollman #include <net/netisr.h>
634b79449eSBjoern A. Zeeb #include <net/vnet.h>
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes #include <netinet/in.h>
6657f60867SMark Johnston #include <netinet/in_kdtrace.h>
67df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
68b5e8ce9fSBruce Evans #include <netinet/in_var.h>
69df8bae1dSRodney W. Grimes #include <netinet/ip.h>
70df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
71df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
72eddfbb76SRobert Watson #include <netinet/ip_fw.h>
73df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
74ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
7558938916SGarrett Wollman #include <machine/in_cksum.h>
76a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
77b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
781dfcf0d2SAndre Oppermann #include <netinet/ip_ipsec.h>
79b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
80df8bae1dSRodney W. Grimes 
81f0068c4aSGarrett Wollman #include <sys/socketvar.h>
826ddbf1e2SGary Palmer 
83aed55708SRobert Watson #include <security/mac/mac_framework.h>
84aed55708SRobert Watson 
85d2035ffbSEd Maste #ifdef CTASSERT
86d2035ffbSEd Maste CTASSERT(sizeof(struct ip) == 20);
87d2035ffbSEd Maste #endif
88d2035ffbSEd Maste 
89f89d4c3aSAndre Oppermann struct	rwlock in_ifaddr_lock;
9064aeca7bSRobert Watson RW_SYSINIT(in_ifaddr_lock, &in_ifaddr_lock, "in_ifaddr_lock");
91f0068c4aSGarrett Wollman 
9282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, rsvp_on);
9382cea7e6SBjoern A. Zeeb 
9482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ipforwarding);
95eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
96eddfbb76SRobert Watson     &VNET_NAME(ipforwarding), 0,
978b615593SMarko Zec     "Enable IP forwarding between interfaces");
980312fbe9SPoul-Henning Kamp 
993e288e62SDimitry Andric static VNET_DEFINE(int, ipsendredirects) = 1;	/* XXX */
10082cea7e6SBjoern A. Zeeb #define	V_ipsendredirects	VNET(ipsendredirects)
101eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
102eddfbb76SRobert Watson     &VNET_NAME(ipsendredirects), 0,
1038b615593SMarko Zec     "Enable sending IP redirects");
1040312fbe9SPoul-Henning Kamp 
1053e288e62SDimitry Andric static VNET_DEFINE(int, ip_keepfaith);
10682cea7e6SBjoern A. Zeeb #define	V_ip_keepfaith		VNET(ip_keepfaith)
107eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
108eddfbb76SRobert Watson     &VNET_NAME(ip_keepfaith), 0,
1096a800098SYoshinobu Inoue     "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
1106a800098SYoshinobu Inoue 
1113e288e62SDimitry Andric static VNET_DEFINE(int, ip_sendsourcequench);
11282cea7e6SBjoern A. Zeeb #define	V_ip_sendsourcequench	VNET(ip_sendsourcequench)
113eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW,
114eddfbb76SRobert Watson     &VNET_NAME(ip_sendsourcequench), 0,
115df285b3dSMike Silbersack     "Enable the transmission of source quench packets");
116df285b3dSMike Silbersack 
11782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip_do_randomid);
118eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW,
119eddfbb76SRobert Watson     &VNET_NAME(ip_do_randomid), 0,
120eddfbb76SRobert Watson     "Assign random ip_id values");
1211f44b0a1SDavid Malone 
122823db0e9SDon Lewis /*
123823db0e9SDon Lewis  * XXX - Setting ip_checkinterface mostly implements the receive side of
124823db0e9SDon Lewis  * the Strong ES model described in RFC 1122, but since the routing table
125a8f12100SDon Lewis  * and transmit implementation do not implement the Strong ES model,
126823db0e9SDon Lewis  * setting this to 1 results in an odd hybrid.
1273f67c834SDon Lewis  *
128a8f12100SDon Lewis  * XXX - ip_checkinterface currently must be disabled if you use ipnat
129a8f12100SDon Lewis  * to translate the destination address to another local interface.
1303f67c834SDon Lewis  *
1313f67c834SDon Lewis  * XXX - ip_checkinterface must be disabled if you add IP aliases
1323f67c834SDon Lewis  * to the loopback interface instead of the interface where the
1333f67c834SDon Lewis  * packets for those addresses are received.
134823db0e9SDon Lewis  */
1353e288e62SDimitry Andric static VNET_DEFINE(int, ip_checkinterface);
13682cea7e6SBjoern A. Zeeb #define	V_ip_checkinterface	VNET(ip_checkinterface)
137eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
138eddfbb76SRobert Watson     &VNET_NAME(ip_checkinterface), 0,
1398b615593SMarko Zec     "Verify packet arrives on correct interface");
140b3e95d4eSJonathan Lemon 
1410b4b0b0fSJulian Elischer VNET_DEFINE(struct pfil_head, inet_pfil_hook);	/* Packet filter hooks */
142df8bae1dSRodney W. Grimes 
143d4b5cae4SRobert Watson static struct netisr_handler ip_nh = {
144d4b5cae4SRobert Watson 	.nh_name = "ip",
145d4b5cae4SRobert Watson 	.nh_handler = ip_input,
146d4b5cae4SRobert Watson 	.nh_proto = NETISR_IP,
147d4b5cae4SRobert Watson 	.nh_policy = NETISR_POLICY_FLOW,
148d4b5cae4SRobert Watson };
149ca925d9cSJonathan Lemon 
150df8bae1dSRodney W. Grimes extern	struct domain inetdomain;
151f0ffb944SJulian Elischer extern	struct protosw inetsw[];
152df8bae1dSRodney W. Grimes u_char	ip_protox[IPPROTO_MAX];
15382cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead);  /* first inet address */
15482cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); /* inet addr hash table  */
15582cea7e6SBjoern A. Zeeb VNET_DEFINE(u_long, in_ifaddrhmask);		/* mask for hash table */
156ca925d9cSJonathan Lemon 
1573e288e62SDimitry Andric static VNET_DEFINE(uma_zone_t, ipq_zone);
1583e288e62SDimitry Andric static VNET_DEFINE(TAILQ_HEAD(ipqhead, ipq), ipq[IPREASS_NHASH]);
159f89d4c3aSAndre Oppermann static struct mtx ipqlock;
1602fad1e93SSam Leffler 
16182cea7e6SBjoern A. Zeeb #define	V_ipq_zone		VNET(ipq_zone)
16282cea7e6SBjoern A. Zeeb #define	V_ipq			VNET(ipq)
16382cea7e6SBjoern A. Zeeb 
1642fad1e93SSam Leffler #define	IPQ_LOCK()	mtx_lock(&ipqlock)
1652fad1e93SSam Leffler #define	IPQ_UNLOCK()	mtx_unlock(&ipqlock)
166888c2a3cSSam Leffler #define	IPQ_LOCK_INIT()	mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
167888c2a3cSSam Leffler #define	IPQ_LOCK_ASSERT()	mtx_assert(&ipqlock, MA_OWNED)
168f23b4c91SGarrett Wollman 
169d248c7d7SRobert Watson static void	maxnipq_update(void);
1704f590175SPaul Saab static void	ipq_zone_change(void *);
1719802380eSBjoern A. Zeeb static void	ip_drain_locked(void);
172d248c7d7SRobert Watson 
1733e288e62SDimitry Andric static VNET_DEFINE(int, maxnipq);  /* Administrative limit on # reass queues. */
1743e288e62SDimitry Andric static VNET_DEFINE(int, nipq);			/* Total # of reass queues */
17582cea7e6SBjoern A. Zeeb #define	V_maxnipq		VNET(maxnipq)
17682cea7e6SBjoern A. Zeeb #define	V_nipq			VNET(nipq)
177eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_RD,
178eddfbb76SRobert Watson     &VNET_NAME(nipq), 0,
1798b615593SMarko Zec     "Current number of IPv4 fragment reassembly queue entries");
180d248c7d7SRobert Watson 
1813e288e62SDimitry Andric static VNET_DEFINE(int, maxfragsperpacket);
18282cea7e6SBjoern A. Zeeb #define	V_maxfragsperpacket	VNET(maxfragsperpacket)
183eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW,
184eddfbb76SRobert Watson     &VNET_NAME(maxfragsperpacket), 0,
185d248c7d7SRobert Watson     "Maximum number of IPv4 fragments allowed per packet");
186d248c7d7SRobert Watson 
1870312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU
1880312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
1893d177f46SBill Fumerola     &ip_mtu, 0, "Default MTU");
1900312fbe9SPoul-Henning Kamp #endif
1910312fbe9SPoul-Henning Kamp 
1921b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
19382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ipstealth);
194eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
195eddfbb76SRobert Watson     &VNET_NAME(ipstealth), 0,
196eddfbb76SRobert Watson     "IP stealth mode, no TTL decrementation on forwarding");
1971b968362SDag-Erling Smørgrav #endif
198eddfbb76SRobert Watson 
1994d77a549SAlfred Perlstein static void	ip_freef(struct ipqhead *, struct ipq *);
2008948e4baSArchie Cobbs 
201315e3e38SRobert Watson /*
2025da0521fSAndrey V. Elsukov  * IP statistics are stored in the "array" of counter(9)s.
2035923c293SGleb Smirnoff  */
2045da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct ipstat, ipstat);
2055da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(ipstat);
2065da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_ip, IPCTL_STATS, stats, struct ipstat, ipstat,
2075da0521fSAndrey V. Elsukov     "IP statistics (struct ipstat, netinet/ip_var.h)");
2085923c293SGleb Smirnoff 
2095923c293SGleb Smirnoff #ifdef VIMAGE
2105da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(ipstat);
2115923c293SGleb Smirnoff #endif /* VIMAGE */
2125923c293SGleb Smirnoff 
2135923c293SGleb Smirnoff /*
214315e3e38SRobert Watson  * Kernel module interface for updating ipstat.  The argument is an index
2155923c293SGleb Smirnoff  * into ipstat treated as an array.
216315e3e38SRobert Watson  */
217315e3e38SRobert Watson void
218315e3e38SRobert Watson kmod_ipstat_inc(int statnum)
219315e3e38SRobert Watson {
220315e3e38SRobert Watson 
2215da0521fSAndrey V. Elsukov 	counter_u64_add(VNET(ipstat)[statnum], 1);
222315e3e38SRobert Watson }
223315e3e38SRobert Watson 
224315e3e38SRobert Watson void
225315e3e38SRobert Watson kmod_ipstat_dec(int statnum)
226315e3e38SRobert Watson {
227315e3e38SRobert Watson 
2285da0521fSAndrey V. Elsukov 	counter_u64_add(VNET(ipstat)[statnum], -1);
229315e3e38SRobert Watson }
230315e3e38SRobert Watson 
231d4b5cae4SRobert Watson static int
232d4b5cae4SRobert Watson sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
233d4b5cae4SRobert Watson {
234d4b5cae4SRobert Watson 	int error, qlimit;
235d4b5cae4SRobert Watson 
236d4b5cae4SRobert Watson 	netisr_getqlimit(&ip_nh, &qlimit);
237d4b5cae4SRobert Watson 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
238d4b5cae4SRobert Watson 	if (error || !req->newptr)
239d4b5cae4SRobert Watson 		return (error);
240d4b5cae4SRobert Watson 	if (qlimit < 1)
241d4b5cae4SRobert Watson 		return (EINVAL);
242d4b5cae4SRobert Watson 	return (netisr_setqlimit(&ip_nh, qlimit));
243d4b5cae4SRobert Watson }
244d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen,
245d4b5cae4SRobert Watson     CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_queue_maxlen, "I",
246d4b5cae4SRobert Watson     "Maximum size of the IP input queue");
247d4b5cae4SRobert Watson 
248d4b5cae4SRobert Watson static int
249d4b5cae4SRobert Watson sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS)
250d4b5cae4SRobert Watson {
251d4b5cae4SRobert Watson 	u_int64_t qdrops_long;
252d4b5cae4SRobert Watson 	int error, qdrops;
253d4b5cae4SRobert Watson 
254d4b5cae4SRobert Watson 	netisr_getqdrops(&ip_nh, &qdrops_long);
255d4b5cae4SRobert Watson 	qdrops = qdrops_long;
256d4b5cae4SRobert Watson 	error = sysctl_handle_int(oidp, &qdrops, 0, req);
257d4b5cae4SRobert Watson 	if (error || !req->newptr)
258d4b5cae4SRobert Watson 		return (error);
259d4b5cae4SRobert Watson 	if (qdrops != 0)
260d4b5cae4SRobert Watson 		return (EINVAL);
261d4b5cae4SRobert Watson 	netisr_clearqdrops(&ip_nh);
262d4b5cae4SRobert Watson 	return (0);
263d4b5cae4SRobert Watson }
264d4b5cae4SRobert Watson 
265d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops,
266d4b5cae4SRobert Watson     CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_queue_drops, "I",
267d4b5cae4SRobert Watson     "Number of packets dropped from the IP input queue");
268d4b5cae4SRobert Watson 
269df8bae1dSRodney W. Grimes /*
270df8bae1dSRodney W. Grimes  * IP initialization: fill in IP protocol switch table.
271df8bae1dSRodney W. Grimes  * All protocols not implemented in kernel go to raw IP protocol handler.
272df8bae1dSRodney W. Grimes  */
273df8bae1dSRodney W. Grimes void
274f2565d68SRobert Watson ip_init(void)
275df8bae1dSRodney W. Grimes {
276f2565d68SRobert Watson 	struct protosw *pr;
277f2565d68SRobert Watson 	int i;
278df8bae1dSRodney W. Grimes 
279a511354aSRobert Watson 	V_ip_id = time_second & 0xffff;
280a511354aSRobert Watson 
281603724d3SBjoern A. Zeeb 	TAILQ_INIT(&V_in_ifaddrhead);
282603724d3SBjoern A. Zeeb 	V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
2831ed81b73SMarko Zec 
2841ed81b73SMarko Zec 	/* Initialize IP reassembly queue. */
2851ed81b73SMarko Zec 	for (i = 0; i < IPREASS_NHASH; i++)
2861ed81b73SMarko Zec 		TAILQ_INIT(&V_ipq[i]);
2871ed81b73SMarko Zec 	V_maxnipq = nmbclusters / 32;
2881ed81b73SMarko Zec 	V_maxfragsperpacket = 16;
2891ed81b73SMarko Zec 	V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
2901ed81b73SMarko Zec 	    NULL, UMA_ALIGN_PTR, 0);
2911ed81b73SMarko Zec 	maxnipq_update();
2921ed81b73SMarko Zec 
2930b4b0b0fSJulian Elischer 	/* Initialize packet filter hooks. */
2940b4b0b0fSJulian Elischer 	V_inet_pfil_hook.ph_type = PFIL_TYPE_AF;
2950b4b0b0fSJulian Elischer 	V_inet_pfil_hook.ph_af = AF_INET;
2960b4b0b0fSJulian Elischer 	if ((i = pfil_head_register(&V_inet_pfil_hook)) != 0)
2970b4b0b0fSJulian Elischer 		printf("%s: WARNING: unable to register pfil hook, "
2980b4b0b0fSJulian Elischer 			"error %d\n", __func__, i);
2990b4b0b0fSJulian Elischer 
3001ed81b73SMarko Zec 	/* Skip initialization of globals for non-default instances. */
3011ed81b73SMarko Zec 	if (!IS_DEFAULT_VNET(curvnet))
3021ed81b73SMarko Zec 		return;
3031ed81b73SMarko Zec 
304f0ffb944SJulian Elischer 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
30502410549SRobert Watson 	if (pr == NULL)
306db09bef3SAndre Oppermann 		panic("ip_init: PF_INET not found");
307db09bef3SAndre Oppermann 
308db09bef3SAndre Oppermann 	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
309df8bae1dSRodney W. Grimes 	for (i = 0; i < IPPROTO_MAX; i++)
310df8bae1dSRodney W. Grimes 		ip_protox[i] = pr - inetsw;
311db09bef3SAndre Oppermann 	/*
312db09bef3SAndre Oppermann 	 * Cycle through IP protocols and put them into the appropriate place
313db09bef3SAndre Oppermann 	 * in ip_protox[].
314db09bef3SAndre Oppermann 	 */
315f0ffb944SJulian Elischer 	for (pr = inetdomain.dom_protosw;
316f0ffb944SJulian Elischer 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
317df8bae1dSRodney W. Grimes 		if (pr->pr_domain->dom_family == PF_INET &&
318db09bef3SAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
319db09bef3SAndre Oppermann 			/* Be careful to only index valid IP protocols. */
320db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX)
321df8bae1dSRodney W. Grimes 				ip_protox[pr->pr_protocol] = pr - inetsw;
322db09bef3SAndre Oppermann 		}
323194a213eSAndrey A. Chernov 
3244f590175SPaul Saab 	EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
3254f590175SPaul Saab 		NULL, EVENTHANDLER_PRI_ANY);
3265f311da2SMike Silbersack 
327db09bef3SAndre Oppermann 	/* Initialize various other remaining things. */
3281ed81b73SMarko Zec 	IPQ_LOCK_INIT();
329d4b5cae4SRobert Watson 	netisr_register(&ip_nh);
330df8bae1dSRodney W. Grimes }
331df8bae1dSRodney W. Grimes 
3329802380eSBjoern A. Zeeb #ifdef VIMAGE
3339802380eSBjoern A. Zeeb void
3349802380eSBjoern A. Zeeb ip_destroy(void)
3359802380eSBjoern A. Zeeb {
3364d3dfd45SMikolaj Golub 	int i;
3374d3dfd45SMikolaj Golub 
3384d3dfd45SMikolaj Golub 	if ((i = pfil_head_unregister(&V_inet_pfil_hook)) != 0)
3394d3dfd45SMikolaj Golub 		printf("%s: WARNING: unable to unregister pfil hook, "
3404d3dfd45SMikolaj Golub 		    "error %d\n", __func__, i);
3419802380eSBjoern A. Zeeb 
3429802380eSBjoern A. Zeeb 	/* Cleanup in_ifaddr hash table; should be empty. */
3439802380eSBjoern A. Zeeb 	hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask);
3449802380eSBjoern A. Zeeb 
3459802380eSBjoern A. Zeeb 	IPQ_LOCK();
3469802380eSBjoern A. Zeeb 	ip_drain_locked();
3479802380eSBjoern A. Zeeb 	IPQ_UNLOCK();
3489802380eSBjoern A. Zeeb 
3499802380eSBjoern A. Zeeb 	uma_zdestroy(V_ipq_zone);
3509802380eSBjoern A. Zeeb }
3519802380eSBjoern A. Zeeb #endif
3529802380eSBjoern A. Zeeb 
3534d2e3692SLuigi Rizzo /*
354df8bae1dSRodney W. Grimes  * Ip input routine.  Checksum and byte swap header.  If fragmented
355df8bae1dSRodney W. Grimes  * try to reassemble.  Process options.  Pass to next level.
356df8bae1dSRodney W. Grimes  */
357c67b1d17SGarrett Wollman void
358c67b1d17SGarrett Wollman ip_input(struct mbuf *m)
359df8bae1dSRodney W. Grimes {
3609188b4a1SAndre Oppermann 	struct ip *ip = NULL;
3615da9f8faSJosef Karthauser 	struct in_ifaddr *ia = NULL;
362ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
3630aade26eSRobert Watson 	struct ifnet *ifp;
3649b932e9eSAndre Oppermann 	int    checkif, hlen = 0;
36521d172a3SGleb Smirnoff 	uint16_t sum, ip_len;
36602c1c707SAndre Oppermann 	int dchg = 0;				/* dest changed after fw */
367f51f805fSSam Leffler 	struct in_addr odst;			/* original dst address */
368b715f178SLuigi Rizzo 
369fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
370db40007dSAndrew R. Reiter 
371ac9d7e26SMax Laier 	if (m->m_flags & M_FASTFWD_OURS) {
37276ff6dcfSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
37376ff6dcfSAndre Oppermann 		/* Set up some basics that will be used later. */
3742b25acc1SLuigi Rizzo 		ip = mtod(m, struct ip *);
37553be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
3768f134647SGleb Smirnoff 		ip_len = ntohs(ip->ip_len);
3779b932e9eSAndre Oppermann 		goto ours;
3782b25acc1SLuigi Rizzo 	}
3792b25acc1SLuigi Rizzo 
38086425c62SRobert Watson 	IPSTAT_INC(ips_total);
38158938916SGarrett Wollman 
38258938916SGarrett Wollman 	if (m->m_pkthdr.len < sizeof(struct ip))
38358938916SGarrett Wollman 		goto tooshort;
38458938916SGarrett Wollman 
385df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct ip) &&
3860b17fba7SAndre Oppermann 	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
38786425c62SRobert Watson 		IPSTAT_INC(ips_toosmall);
388c67b1d17SGarrett Wollman 		return;
389df8bae1dSRodney W. Grimes 	}
390df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
39158938916SGarrett Wollman 
39253be11f6SPoul-Henning Kamp 	if (ip->ip_v != IPVERSION) {
39386425c62SRobert Watson 		IPSTAT_INC(ips_badvers);
394df8bae1dSRodney W. Grimes 		goto bad;
395df8bae1dSRodney W. Grimes 	}
39658938916SGarrett Wollman 
39753be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
398df8bae1dSRodney W. Grimes 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
39986425c62SRobert Watson 		IPSTAT_INC(ips_badhlen);
400df8bae1dSRodney W. Grimes 		goto bad;
401df8bae1dSRodney W. Grimes 	}
402df8bae1dSRodney W. Grimes 	if (hlen > m->m_len) {
4030b17fba7SAndre Oppermann 		if ((m = m_pullup(m, hlen)) == NULL) {
40486425c62SRobert Watson 			IPSTAT_INC(ips_badhlen);
405c67b1d17SGarrett Wollman 			return;
406df8bae1dSRodney W. Grimes 		}
407df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
408df8bae1dSRodney W. Grimes 	}
40933841545SHajimu UMEMOTO 
41057f60867SMark Johnston 	IP_PROBE(receive, NULL, NULL, ip, m->m_pkthdr.rcvif, ip, NULL);
41157f60867SMark Johnston 
41233841545SHajimu UMEMOTO 	/* 127/8 must not appear on wire - RFC1122 */
4130aade26eSRobert Watson 	ifp = m->m_pkthdr.rcvif;
41433841545SHajimu UMEMOTO 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
41533841545SHajimu UMEMOTO 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
4160aade26eSRobert Watson 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
41786425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
41833841545SHajimu UMEMOTO 			goto bad;
41933841545SHajimu UMEMOTO 		}
42033841545SHajimu UMEMOTO 	}
42133841545SHajimu UMEMOTO 
422db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
423db4f9cc7SJonathan Lemon 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
424db4f9cc7SJonathan Lemon 	} else {
42558938916SGarrett Wollman 		if (hlen == sizeof(struct ip)) {
42647c861ecSBrian Somers 			sum = in_cksum_hdr(ip);
42758938916SGarrett Wollman 		} else {
42847c861ecSBrian Somers 			sum = in_cksum(m, hlen);
42958938916SGarrett Wollman 		}
430db4f9cc7SJonathan Lemon 	}
43147c861ecSBrian Somers 	if (sum) {
43286425c62SRobert Watson 		IPSTAT_INC(ips_badsum);
433df8bae1dSRodney W. Grimes 		goto bad;
434df8bae1dSRodney W. Grimes 	}
435df8bae1dSRodney W. Grimes 
43602b199f1SMax Laier #ifdef ALTQ
43702b199f1SMax Laier 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
43802b199f1SMax Laier 		/* packet is dropped by traffic conditioner */
43902b199f1SMax Laier 		return;
44002b199f1SMax Laier #endif
44102b199f1SMax Laier 
44221d172a3SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
44321d172a3SGleb Smirnoff 	if (ip_len < hlen) {
44486425c62SRobert Watson 		IPSTAT_INC(ips_badlen);
445df8bae1dSRodney W. Grimes 		goto bad;
446df8bae1dSRodney W. Grimes 	}
447df8bae1dSRodney W. Grimes 
448df8bae1dSRodney W. Grimes 	/*
449df8bae1dSRodney W. Grimes 	 * Check that the amount of data in the buffers
450df8bae1dSRodney W. Grimes 	 * is as at least much as the IP header would have us expect.
451df8bae1dSRodney W. Grimes 	 * Trim mbufs if longer than we expect.
452df8bae1dSRodney W. Grimes 	 * Drop packet if shorter than we expect.
453df8bae1dSRodney W. Grimes 	 */
45421d172a3SGleb Smirnoff 	if (m->m_pkthdr.len < ip_len) {
45558938916SGarrett Wollman tooshort:
45686425c62SRobert Watson 		IPSTAT_INC(ips_tooshort);
457df8bae1dSRodney W. Grimes 		goto bad;
458df8bae1dSRodney W. Grimes 	}
45921d172a3SGleb Smirnoff 	if (m->m_pkthdr.len > ip_len) {
460df8bae1dSRodney W. Grimes 		if (m->m_len == m->m_pkthdr.len) {
46121d172a3SGleb Smirnoff 			m->m_len = ip_len;
46221d172a3SGleb Smirnoff 			m->m_pkthdr.len = ip_len;
463df8bae1dSRodney W. Grimes 		} else
46421d172a3SGleb Smirnoff 			m_adj(m, ip_len - m->m_pkthdr.len);
465df8bae1dSRodney W. Grimes 	}
466b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
46714dd6717SSam Leffler 	/*
468ffe8cd7bSBjoern A. Zeeb 	 * Bypass packet filtering for packets previously handled by IPsec.
46914dd6717SSam Leffler 	 */
470cc977adcSBjoern A. Zeeb 	if (ip_ipsec_filtertunnel(m))
471c21fd232SAndre Oppermann 		goto passin;
472b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
4733f67c834SDon Lewis 
474c4ac87eaSDarren Reed 	/*
475134ea224SSam Leffler 	 * Run through list of hooks for input packets.
476f51f805fSSam Leffler 	 *
477f51f805fSSam Leffler 	 * NB: Beware of the destination address changing (e.g.
478f51f805fSSam Leffler 	 *     by NAT rewriting).  When this happens, tell
479f51f805fSSam Leffler 	 *     ip_forward to do the right thing.
480c4ac87eaSDarren Reed 	 */
481c21fd232SAndre Oppermann 
482c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
4830b4b0b0fSJulian Elischer 	if (!PFIL_HOOKED(&V_inet_pfil_hook))
484c21fd232SAndre Oppermann 		goto passin;
485c21fd232SAndre Oppermann 
486f51f805fSSam Leffler 	odst = ip->ip_dst;
4870b4b0b0fSJulian Elischer 	if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0)
488beec8214SDarren Reed 		return;
489134ea224SSam Leffler 	if (m == NULL)			/* consumed by filter */
490c4ac87eaSDarren Reed 		return;
4919b932e9eSAndre Oppermann 
492c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
49302c1c707SAndre Oppermann 	dchg = (odst.s_addr != ip->ip_dst.s_addr);
4940aade26eSRobert Watson 	ifp = m->m_pkthdr.rcvif;
4959b932e9eSAndre Oppermann 
4969b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
4979b932e9eSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
4989b932e9eSAndre Oppermann 		goto ours;
4999b932e9eSAndre Oppermann 	}
500ffdbf9daSAndrey V. Elsukov 	if (m->m_flags & M_IP_NEXTHOP) {
501ffdbf9daSAndrey V. Elsukov 		dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL);
502ffdbf9daSAndrey V. Elsukov 		if (dchg != 0) {
503099dd043SAndre Oppermann 			/*
504ffdbf9daSAndrey V. Elsukov 			 * Directly ship the packet on.  This allows
505ffdbf9daSAndrey V. Elsukov 			 * forwarding packets originally destined to us
506ffdbf9daSAndrey V. Elsukov 			 * to some other directly connected host.
507099dd043SAndre Oppermann 			 */
508ffdbf9daSAndrey V. Elsukov 			ip_forward(m, 1);
509099dd043SAndre Oppermann 			return;
510099dd043SAndre Oppermann 		}
511ffdbf9daSAndrey V. Elsukov 	}
512c21fd232SAndre Oppermann passin:
51321d172a3SGleb Smirnoff 
51421d172a3SGleb Smirnoff 	/*
515df8bae1dSRodney W. Grimes 	 * Process options and, if not destined for us,
516df8bae1dSRodney W. Grimes 	 * ship it on.  ip_dooptions returns 1 when an
517df8bae1dSRodney W. Grimes 	 * error was detected (causing an icmp message
518df8bae1dSRodney W. Grimes 	 * to be sent and the original packet to be freed).
519df8bae1dSRodney W. Grimes 	 */
5209b932e9eSAndre Oppermann 	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
521c67b1d17SGarrett Wollman 		return;
522df8bae1dSRodney W. Grimes 
523f0068c4aSGarrett Wollman         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
524f0068c4aSGarrett Wollman          * matter if it is destined to another node, or whether it is
525f0068c4aSGarrett Wollman          * a multicast one, RSVP wants it! and prevents it from being forwarded
526f0068c4aSGarrett Wollman          * anywhere else. Also checks if the rsvp daemon is running before
527f0068c4aSGarrett Wollman 	 * grabbing the packet.
528f0068c4aSGarrett Wollman          */
529603724d3SBjoern A. Zeeb 	if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP)
530f0068c4aSGarrett Wollman 		goto ours;
531f0068c4aSGarrett Wollman 
532df8bae1dSRodney W. Grimes 	/*
533df8bae1dSRodney W. Grimes 	 * Check our list of addresses, to see if the packet is for us.
534cc766e04SGarrett Wollman 	 * If we don't have any addresses, assume any unicast packet
535cc766e04SGarrett Wollman 	 * we receive might be for us (and let the upper layers deal
536cc766e04SGarrett Wollman 	 * with it).
537df8bae1dSRodney W. Grimes 	 */
538603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
539cc766e04SGarrett Wollman 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
540cc766e04SGarrett Wollman 		goto ours;
541cc766e04SGarrett Wollman 
5427538a9a0SJonathan Lemon 	/*
543823db0e9SDon Lewis 	 * Enable a consistency check between the destination address
544823db0e9SDon Lewis 	 * and the arrival interface for a unicast packet (the RFC 1122
545823db0e9SDon Lewis 	 * strong ES model) if IP forwarding is disabled and the packet
546e15ae1b2SDon Lewis 	 * is not locally generated and the packet is not subject to
547e15ae1b2SDon Lewis 	 * 'ipfw fwd'.
5483f67c834SDon Lewis 	 *
5493f67c834SDon Lewis 	 * XXX - Checking also should be disabled if the destination
5503f67c834SDon Lewis 	 * address is ipnat'ed to a different interface.
5513f67c834SDon Lewis 	 *
552a8f12100SDon Lewis 	 * XXX - Checking is incompatible with IP aliases added
5533f67c834SDon Lewis 	 * to the loopback interface instead of the interface where
5543f67c834SDon Lewis 	 * the packets are received.
555a9771948SGleb Smirnoff 	 *
556a9771948SGleb Smirnoff 	 * XXX - This is the case for carp vhost IPs as well so we
557a9771948SGleb Smirnoff 	 * insert a workaround. If the packet got here, we already
558a9771948SGleb Smirnoff 	 * checked with carp_iamatch() and carp_forus().
559823db0e9SDon Lewis 	 */
560603724d3SBjoern A. Zeeb 	checkif = V_ip_checkinterface && (V_ipforwarding == 0) &&
5610aade26eSRobert Watson 	    ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) &&
56254bfbd51SWill Andrews 	    ifp->if_carp == NULL && (dchg == 0);
563823db0e9SDon Lewis 
564ca925d9cSJonathan Lemon 	/*
565ca925d9cSJonathan Lemon 	 * Check for exact addresses in the hash bucket.
566ca925d9cSJonathan Lemon 	 */
5672d9cfabaSRobert Watson 	/* IN_IFADDR_RLOCK(); */
5689b932e9eSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
569f9e354dfSJulian Elischer 		/*
570823db0e9SDon Lewis 		 * If the address matches, verify that the packet
571823db0e9SDon Lewis 		 * arrived via the correct interface if checking is
572823db0e9SDon Lewis 		 * enabled.
573f9e354dfSJulian Elischer 		 */
5749b932e9eSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
5758c0fec80SRobert Watson 		    (!checkif || ia->ia_ifp == ifp)) {
5767caf4ab7SGleb Smirnoff 			counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
5777caf4ab7SGleb Smirnoff 			counter_u64_add(ia->ia_ifa.ifa_ibytes,
5787caf4ab7SGleb Smirnoff 			    m->m_pkthdr.len);
5792d9cfabaSRobert Watson 			/* IN_IFADDR_RUNLOCK(); */
580ed1ff184SJulian Elischer 			goto ours;
581ca925d9cSJonathan Lemon 		}
5828c0fec80SRobert Watson 	}
5832d9cfabaSRobert Watson 	/* IN_IFADDR_RUNLOCK(); */
5842d9cfabaSRobert Watson 
585823db0e9SDon Lewis 	/*
586ca925d9cSJonathan Lemon 	 * Check for broadcast addresses.
587ca925d9cSJonathan Lemon 	 *
588ca925d9cSJonathan Lemon 	 * Only accept broadcast packets that arrive via the matching
589ca925d9cSJonathan Lemon 	 * interface.  Reception of forwarded directed broadcasts would
590ca925d9cSJonathan Lemon 	 * be handled via ip_forward() and ether_output() with the loopback
591ca925d9cSJonathan Lemon 	 * into the stack for SIMPLEX interfaces handled by ether_output().
592823db0e9SDon Lewis 	 */
5930aade26eSRobert Watson 	if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
594137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
5950aade26eSRobert Watson 	        TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
596ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
597ca925d9cSJonathan Lemon 				continue;
598ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
599df8bae1dSRodney W. Grimes 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
6000aade26eSRobert Watson 			    ip->ip_dst.s_addr) {
6017caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
6027caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_ibytes,
6037caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len);
604137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
605df8bae1dSRodney W. Grimes 				goto ours;
6060aade26eSRobert Watson 			}
6070ac40133SBrian Somers #ifdef BOOTP_COMPAT
6080aade26eSRobert Watson 			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) {
6097caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
6107caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_ibytes,
6117caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len);
612137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
613ca925d9cSJonathan Lemon 				goto ours;
6140aade26eSRobert Watson 			}
6150ac40133SBrian Somers #endif
616df8bae1dSRodney W. Grimes 		}
617137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
61819e5b0a7SRobert Watson 		ia = NULL;
619df8bae1dSRodney W. Grimes 	}
620f8429ca2SBruce M Simpson 	/* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
621f8429ca2SBruce M Simpson 	if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
62286425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
623f8429ca2SBruce M Simpson 		m_freem(m);
624f8429ca2SBruce M Simpson 		return;
625f8429ca2SBruce M Simpson 	}
626df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
627603724d3SBjoern A. Zeeb 		if (V_ip_mrouter) {
628df8bae1dSRodney W. Grimes 			/*
629df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, all
630df8bae1dSRodney W. Grimes 			 * incoming multicast packets are passed to the
631df8bae1dSRodney W. Grimes 			 * kernel-level multicast forwarding function.
632df8bae1dSRodney W. Grimes 			 * The packet is returned (relatively) intact; if
633df8bae1dSRodney W. Grimes 			 * ip_mforward() returns a non-zero value, the packet
634df8bae1dSRodney W. Grimes 			 * must be discarded, else it may be accepted below.
635df8bae1dSRodney W. Grimes 			 */
6360aade26eSRobert Watson 			if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
63786425c62SRobert Watson 				IPSTAT_INC(ips_cantforward);
638df8bae1dSRodney W. Grimes 				m_freem(m);
639c67b1d17SGarrett Wollman 				return;
640df8bae1dSRodney W. Grimes 			}
641df8bae1dSRodney W. Grimes 
642df8bae1dSRodney W. Grimes 			/*
64311612afaSDima Dorfman 			 * The process-level routing daemon needs to receive
644df8bae1dSRodney W. Grimes 			 * all multicast IGMP packets, whether or not this
645df8bae1dSRodney W. Grimes 			 * host belongs to their destination groups.
646df8bae1dSRodney W. Grimes 			 */
647df8bae1dSRodney W. Grimes 			if (ip->ip_p == IPPROTO_IGMP)
648df8bae1dSRodney W. Grimes 				goto ours;
64986425c62SRobert Watson 			IPSTAT_INC(ips_forward);
650df8bae1dSRodney W. Grimes 		}
651df8bae1dSRodney W. Grimes 		/*
652d10910e6SBruce M Simpson 		 * Assume the packet is for us, to avoid prematurely taking
653d10910e6SBruce M Simpson 		 * a lock on the in_multi hash. Protocols must perform
654d10910e6SBruce M Simpson 		 * their own filtering and update statistics accordingly.
655df8bae1dSRodney W. Grimes 		 */
656df8bae1dSRodney W. Grimes 		goto ours;
657df8bae1dSRodney W. Grimes 	}
658df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
659df8bae1dSRodney W. Grimes 		goto ours;
660df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == INADDR_ANY)
661df8bae1dSRodney W. Grimes 		goto ours;
662df8bae1dSRodney W. Grimes 
6636a800098SYoshinobu Inoue 	/*
6646a800098SYoshinobu Inoue 	 * FAITH(Firewall Aided Internet Translator)
6656a800098SYoshinobu Inoue 	 */
6660aade26eSRobert Watson 	if (ifp && ifp->if_type == IFT_FAITH) {
667603724d3SBjoern A. Zeeb 		if (V_ip_keepfaith) {
6686a800098SYoshinobu Inoue 			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
6696a800098SYoshinobu Inoue 				goto ours;
6706a800098SYoshinobu Inoue 		}
6716a800098SYoshinobu Inoue 		m_freem(m);
6726a800098SYoshinobu Inoue 		return;
6736a800098SYoshinobu Inoue 	}
6749494d596SBrooks Davis 
675df8bae1dSRodney W. Grimes 	/*
676df8bae1dSRodney W. Grimes 	 * Not for us; forward if possible and desirable.
677df8bae1dSRodney W. Grimes 	 */
678603724d3SBjoern A. Zeeb 	if (V_ipforwarding == 0) {
67986425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
680df8bae1dSRodney W. Grimes 		m_freem(m);
681546f251bSChris D. Faulhaber 	} else {
682b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
6831dfcf0d2SAndre Oppermann 		if (ip_ipsec_fwd(m))
684546f251bSChris D. Faulhaber 			goto bad;
685b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
6869b932e9eSAndre Oppermann 		ip_forward(m, dchg);
687546f251bSChris D. Faulhaber 	}
688c67b1d17SGarrett Wollman 	return;
689df8bae1dSRodney W. Grimes 
690df8bae1dSRodney W. Grimes ours:
691d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH
692d0ebc0d2SYaroslav Tykhiy 	/*
693d0ebc0d2SYaroslav Tykhiy 	 * IPSTEALTH: Process non-routing options only
694d0ebc0d2SYaroslav Tykhiy 	 * if the packet is destined for us.
695d0ebc0d2SYaroslav Tykhiy 	 */
6967caf4ab7SGleb Smirnoff 	if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1))
697d0ebc0d2SYaroslav Tykhiy 		return;
698d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */
699d0ebc0d2SYaroslav Tykhiy 
70063f8d699SJordan K. Hubbard 	/*
701b6ea1aa5SRuslan Ermilov 	 * Attempt reassembly; if it succeeds, proceed.
702ac9d7e26SMax Laier 	 * ip_reass() will return a different mbuf.
703df8bae1dSRodney W. Grimes 	 */
7048f134647SGleb Smirnoff 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
705aa69c612SGleb Smirnoff 		/* XXXGL: shouldn't we save & set m_flags? */
706f0cada84SAndre Oppermann 		m = ip_reass(m);
707f0cada84SAndre Oppermann 		if (m == NULL)
708c67b1d17SGarrett Wollman 			return;
7096a800098SYoshinobu Inoue 		ip = mtod(m, struct ip *);
7107e2df452SRuslan Ermilov 		/* Get the header length of the reassembled packet */
71153be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
712f0cada84SAndre Oppermann 	}
713f0cada84SAndre Oppermann 
714b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
71533841545SHajimu UMEMOTO 	/*
71633841545SHajimu UMEMOTO 	 * enforce IPsec policy checking if we are seeing last header.
71733841545SHajimu UMEMOTO 	 * note that we do not visit this with protocols with pcb layer
71833841545SHajimu UMEMOTO 	 * code - like udp/tcp/raw ip.
71933841545SHajimu UMEMOTO 	 */
7201dfcf0d2SAndre Oppermann 	if (ip_ipsec_input(m))
72133841545SHajimu UMEMOTO 		goto bad;
722b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
72333841545SHajimu UMEMOTO 
724df8bae1dSRodney W. Grimes 	/*
725df8bae1dSRodney W. Grimes 	 * Switch out to protocol's input routine.
726df8bae1dSRodney W. Grimes 	 */
72786425c62SRobert Watson 	IPSTAT_INC(ips_delivered);
7289b932e9eSAndre Oppermann 
7298f5a8818SKevin Lo 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(&m, &hlen, ip->ip_p);
730c67b1d17SGarrett Wollman 	return;
731df8bae1dSRodney W. Grimes bad:
732df8bae1dSRodney W. Grimes 	m_freem(m);
733c67b1d17SGarrett Wollman }
734c67b1d17SGarrett Wollman 
735c67b1d17SGarrett Wollman /*
736d248c7d7SRobert Watson  * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
737d248c7d7SRobert Watson  * max has slightly different semantics than the sysctl, for historical
738d248c7d7SRobert Watson  * reasons.
739d248c7d7SRobert Watson  */
740d248c7d7SRobert Watson static void
741d248c7d7SRobert Watson maxnipq_update(void)
742d248c7d7SRobert Watson {
743d248c7d7SRobert Watson 
744d248c7d7SRobert Watson 	/*
745d248c7d7SRobert Watson 	 * -1 for unlimited allocation.
746d248c7d7SRobert Watson 	 */
747603724d3SBjoern A. Zeeb 	if (V_maxnipq < 0)
748603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 0);
749d248c7d7SRobert Watson 	/*
750d248c7d7SRobert Watson 	 * Positive number for specific bound.
751d248c7d7SRobert Watson 	 */
752603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0)
753603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, V_maxnipq);
754d248c7d7SRobert Watson 	/*
755d248c7d7SRobert Watson 	 * Zero specifies no further fragment queue allocation -- set the
756d248c7d7SRobert Watson 	 * bound very low, but rely on implementation elsewhere to actually
757d248c7d7SRobert Watson 	 * prevent allocation and reclaim current queues.
758d248c7d7SRobert Watson 	 */
759603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0)
760603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 1);
761d248c7d7SRobert Watson }
762d248c7d7SRobert Watson 
7634f590175SPaul Saab static void
7644f590175SPaul Saab ipq_zone_change(void *tag)
7654f590175SPaul Saab {
7664f590175SPaul Saab 
767603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) {
768603724d3SBjoern A. Zeeb 		V_maxnipq = nmbclusters / 32;
7694f590175SPaul Saab 		maxnipq_update();
7704f590175SPaul Saab 	}
7714f590175SPaul Saab }
7724f590175SPaul Saab 
773d248c7d7SRobert Watson static int
774d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
775d248c7d7SRobert Watson {
776d248c7d7SRobert Watson 	int error, i;
777d248c7d7SRobert Watson 
778603724d3SBjoern A. Zeeb 	i = V_maxnipq;
779d248c7d7SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
780d248c7d7SRobert Watson 	if (error || !req->newptr)
781d248c7d7SRobert Watson 		return (error);
782d248c7d7SRobert Watson 
783d248c7d7SRobert Watson 	/*
784d248c7d7SRobert Watson 	 * XXXRW: Might be a good idea to sanity check the argument and place
785d248c7d7SRobert Watson 	 * an extreme upper bound.
786d248c7d7SRobert Watson 	 */
787d248c7d7SRobert Watson 	if (i < -1)
788d248c7d7SRobert Watson 		return (EINVAL);
789603724d3SBjoern A. Zeeb 	V_maxnipq = i;
790d248c7d7SRobert Watson 	maxnipq_update();
791d248c7d7SRobert Watson 	return (0);
792d248c7d7SRobert Watson }
793d248c7d7SRobert Watson 
794d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
795d248c7d7SRobert Watson     NULL, 0, sysctl_maxnipq, "I",
796d248c7d7SRobert Watson     "Maximum number of IPv4 fragment reassembly queue entries");
797d248c7d7SRobert Watson 
798aa69c612SGleb Smirnoff #define	M_IP_FRAG	M_PROTO9
799aa69c612SGleb Smirnoff 
800d248c7d7SRobert Watson /*
8018948e4baSArchie Cobbs  * Take incoming datagram fragment and try to reassemble it into
802f0cada84SAndre Oppermann  * whole datagram.  If the argument is the first fragment or one
803f0cada84SAndre Oppermann  * in between the function will return NULL and store the mbuf
804f0cada84SAndre Oppermann  * in the fragment chain.  If the argument is the last fragment
805f0cada84SAndre Oppermann  * the packet will be reassembled and the pointer to the new
806f0cada84SAndre Oppermann  * mbuf returned for further processing.  Only m_tags attached
807f0cada84SAndre Oppermann  * to the first packet/fragment are preserved.
808f0cada84SAndre Oppermann  * The IP header is *NOT* adjusted out of iplen.
809df8bae1dSRodney W. Grimes  */
810f0cada84SAndre Oppermann struct mbuf *
811f0cada84SAndre Oppermann ip_reass(struct mbuf *m)
812df8bae1dSRodney W. Grimes {
813f0cada84SAndre Oppermann 	struct ip *ip;
814f0cada84SAndre Oppermann 	struct mbuf *p, *q, *nq, *t;
815f0cada84SAndre Oppermann 	struct ipq *fp = NULL;
816f0cada84SAndre Oppermann 	struct ipqhead *head;
817f0cada84SAndre Oppermann 	int i, hlen, next;
81859dfcba4SHajimu UMEMOTO 	u_int8_t ecn, ecn0;
819f0cada84SAndre Oppermann 	u_short hash;
820df8bae1dSRodney W. Grimes 
821800af1fbSMaxim Konovalov 	/* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
822603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0 || V_maxfragsperpacket == 0) {
82386425c62SRobert Watson 		IPSTAT_INC(ips_fragments);
82486425c62SRobert Watson 		IPSTAT_INC(ips_fragdropped);
8259d804f81SAndre Oppermann 		m_freem(m);
8269d804f81SAndre Oppermann 		return (NULL);
827f0cada84SAndre Oppermann 	}
8282fad1e93SSam Leffler 
829f0cada84SAndre Oppermann 	ip = mtod(m, struct ip *);
830f0cada84SAndre Oppermann 	hlen = ip->ip_hl << 2;
831f0cada84SAndre Oppermann 
832f0cada84SAndre Oppermann 	hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
833603724d3SBjoern A. Zeeb 	head = &V_ipq[hash];
834f0cada84SAndre Oppermann 	IPQ_LOCK();
835f0cada84SAndre Oppermann 
836f0cada84SAndre Oppermann 	/*
837f0cada84SAndre Oppermann 	 * Look for queue of fragments
838f0cada84SAndre Oppermann 	 * of this datagram.
839f0cada84SAndre Oppermann 	 */
840f0cada84SAndre Oppermann 	TAILQ_FOREACH(fp, head, ipq_list)
841f0cada84SAndre Oppermann 		if (ip->ip_id == fp->ipq_id &&
842f0cada84SAndre Oppermann 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
843f0cada84SAndre Oppermann 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
844f0cada84SAndre Oppermann #ifdef MAC
84530d239bcSRobert Watson 		    mac_ipq_match(m, fp) &&
846f0cada84SAndre Oppermann #endif
847f0cada84SAndre Oppermann 		    ip->ip_p == fp->ipq_p)
848f0cada84SAndre Oppermann 			goto found;
849f0cada84SAndre Oppermann 
850f0cada84SAndre Oppermann 	fp = NULL;
851f0cada84SAndre Oppermann 
852f0cada84SAndre Oppermann 	/*
853d248c7d7SRobert Watson 	 * Attempt to trim the number of allocated fragment queues if it
854d248c7d7SRobert Watson 	 * exceeds the administrative limit.
855f0cada84SAndre Oppermann 	 */
856603724d3SBjoern A. Zeeb 	if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) {
857f0cada84SAndre Oppermann 		/*
858f0cada84SAndre Oppermann 		 * drop something from the tail of the current queue
859f0cada84SAndre Oppermann 		 * before proceeding further
860f0cada84SAndre Oppermann 		 */
861f0cada84SAndre Oppermann 		struct ipq *q = TAILQ_LAST(head, ipqhead);
862f0cada84SAndre Oppermann 		if (q == NULL) {   /* gak */
863f0cada84SAndre Oppermann 			for (i = 0; i < IPREASS_NHASH; i++) {
864603724d3SBjoern A. Zeeb 				struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead);
865f0cada84SAndre Oppermann 				if (r) {
86686425c62SRobert Watson 					IPSTAT_ADD(ips_fragtimeout,
86786425c62SRobert Watson 					    r->ipq_nfrags);
868603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], r);
869f0cada84SAndre Oppermann 					break;
870f0cada84SAndre Oppermann 				}
871f0cada84SAndre Oppermann 			}
872f0cada84SAndre Oppermann 		} else {
87386425c62SRobert Watson 			IPSTAT_ADD(ips_fragtimeout, q->ipq_nfrags);
874f0cada84SAndre Oppermann 			ip_freef(head, q);
875f0cada84SAndre Oppermann 		}
876f0cada84SAndre Oppermann 	}
877f0cada84SAndre Oppermann 
878f0cada84SAndre Oppermann found:
879f0cada84SAndre Oppermann 	/*
880f0cada84SAndre Oppermann 	 * Adjust ip_len to not reflect header,
881f0cada84SAndre Oppermann 	 * convert offset of this to bytes.
882f0cada84SAndre Oppermann 	 */
8838f134647SGleb Smirnoff 	ip->ip_len = htons(ntohs(ip->ip_len) - hlen);
8848f134647SGleb Smirnoff 	if (ip->ip_off & htons(IP_MF)) {
885f0cada84SAndre Oppermann 		/*
886f0cada84SAndre Oppermann 		 * Make sure that fragments have a data length
887f0cada84SAndre Oppermann 		 * that's a non-zero multiple of 8 bytes.
888f0cada84SAndre Oppermann 		 */
889b6fcf6f9SGleb Smirnoff 		if (ip->ip_len == htons(0) || (ntohs(ip->ip_len) & 0x7) != 0) {
89086425c62SRobert Watson 			IPSTAT_INC(ips_toosmall); /* XXX */
891f0cada84SAndre Oppermann 			goto dropfrag;
892f0cada84SAndre Oppermann 		}
893b09dc7e3SAndre Oppermann 		m->m_flags |= M_IP_FRAG;
894f0cada84SAndre Oppermann 	} else
895b09dc7e3SAndre Oppermann 		m->m_flags &= ~M_IP_FRAG;
8968f134647SGleb Smirnoff 	ip->ip_off = htons(ntohs(ip->ip_off) << 3);
897f0cada84SAndre Oppermann 
898f0cada84SAndre Oppermann 	/*
899f0cada84SAndre Oppermann 	 * Attempt reassembly; if it succeeds, proceed.
900f0cada84SAndre Oppermann 	 * ip_reass() will return a different mbuf.
901f0cada84SAndre Oppermann 	 */
90286425c62SRobert Watson 	IPSTAT_INC(ips_fragments);
9031b4381afSAndre Oppermann 	m->m_pkthdr.PH_loc.ptr = ip;
904f0cada84SAndre Oppermann 
905f0cada84SAndre Oppermann 	/* Previous ip_reass() started here. */
906df8bae1dSRodney W. Grimes 	/*
907df8bae1dSRodney W. Grimes 	 * Presence of header sizes in mbufs
908df8bae1dSRodney W. Grimes 	 * would confuse code below.
909df8bae1dSRodney W. Grimes 	 */
910df8bae1dSRodney W. Grimes 	m->m_data += hlen;
911df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
912df8bae1dSRodney W. Grimes 
913df8bae1dSRodney W. Grimes 	/*
914df8bae1dSRodney W. Grimes 	 * If first fragment to arrive, create a reassembly queue.
915df8bae1dSRodney W. Grimes 	 */
916042bbfa3SRobert Watson 	if (fp == NULL) {
917603724d3SBjoern A. Zeeb 		fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
918d248c7d7SRobert Watson 		if (fp == NULL)
919df8bae1dSRodney W. Grimes 			goto dropfrag;
92036b0360bSRobert Watson #ifdef MAC
92130d239bcSRobert Watson 		if (mac_ipq_init(fp, M_NOWAIT) != 0) {
922603724d3SBjoern A. Zeeb 			uma_zfree(V_ipq_zone, fp);
9231d7d0bfeSPawel Jakub Dawidek 			fp = NULL;
9245e7ce478SRobert Watson 			goto dropfrag;
9255e7ce478SRobert Watson 		}
92630d239bcSRobert Watson 		mac_ipq_create(m, fp);
92736b0360bSRobert Watson #endif
928462b86feSPoul-Henning Kamp 		TAILQ_INSERT_HEAD(head, fp, ipq_list);
929603724d3SBjoern A. Zeeb 		V_nipq++;
930375386e2SMike Silbersack 		fp->ipq_nfrags = 1;
931df8bae1dSRodney W. Grimes 		fp->ipq_ttl = IPFRAGTTL;
932df8bae1dSRodney W. Grimes 		fp->ipq_p = ip->ip_p;
933df8bae1dSRodney W. Grimes 		fp->ipq_id = ip->ip_id;
9346effc713SDoug Rabson 		fp->ipq_src = ip->ip_src;
9356effc713SDoug Rabson 		fp->ipq_dst = ip->ip_dst;
936af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
937af38c68cSLuigi Rizzo 		m->m_nextpkt = NULL;
938800af1fbSMaxim Konovalov 		goto done;
93936b0360bSRobert Watson 	} else {
940375386e2SMike Silbersack 		fp->ipq_nfrags++;
94136b0360bSRobert Watson #ifdef MAC
94230d239bcSRobert Watson 		mac_ipq_update(m, fp);
94336b0360bSRobert Watson #endif
944df8bae1dSRodney W. Grimes 	}
945df8bae1dSRodney W. Grimes 
9461b4381afSAndre Oppermann #define GETIP(m)	((struct ip*)((m)->m_pkthdr.PH_loc.ptr))
9476effc713SDoug Rabson 
948df8bae1dSRodney W. Grimes 	/*
94959dfcba4SHajimu UMEMOTO 	 * Handle ECN by comparing this segment with the first one;
95059dfcba4SHajimu UMEMOTO 	 * if CE is set, do not lose CE.
95159dfcba4SHajimu UMEMOTO 	 * drop if CE and not-ECT are mixed for the same packet.
95259dfcba4SHajimu UMEMOTO 	 */
95359dfcba4SHajimu UMEMOTO 	ecn = ip->ip_tos & IPTOS_ECN_MASK;
95459dfcba4SHajimu UMEMOTO 	ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
95559dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_CE) {
95659dfcba4SHajimu UMEMOTO 		if (ecn0 == IPTOS_ECN_NOTECT)
95759dfcba4SHajimu UMEMOTO 			goto dropfrag;
95859dfcba4SHajimu UMEMOTO 		if (ecn0 != IPTOS_ECN_CE)
95959dfcba4SHajimu UMEMOTO 			GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
96059dfcba4SHajimu UMEMOTO 	}
96159dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
96259dfcba4SHajimu UMEMOTO 		goto dropfrag;
96359dfcba4SHajimu UMEMOTO 
96459dfcba4SHajimu UMEMOTO 	/*
965df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
966df8bae1dSRodney W. Grimes 	 */
9676effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
9688f134647SGleb Smirnoff 		if (ntohs(GETIP(q)->ip_off) > ntohs(ip->ip_off))
969df8bae1dSRodney W. Grimes 			break;
970df8bae1dSRodney W. Grimes 
971df8bae1dSRodney W. Grimes 	/*
972df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
973df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
974af38c68cSLuigi Rizzo 	 * segment.  If it provides all of our data, drop us, otherwise
975af38c68cSLuigi Rizzo 	 * stick new segment in the proper place.
976db4f9cc7SJonathan Lemon 	 *
9776bccea7cSRebecca Cran 	 * If some of the data is dropped from the preceding
978db4f9cc7SJonathan Lemon 	 * segment, then it's checksum is invalidated.
979df8bae1dSRodney W. Grimes 	 */
9806effc713SDoug Rabson 	if (p) {
9818f134647SGleb Smirnoff 		i = ntohs(GETIP(p)->ip_off) + ntohs(GETIP(p)->ip_len) -
9828f134647SGleb Smirnoff 		    ntohs(ip->ip_off);
983df8bae1dSRodney W. Grimes 		if (i > 0) {
9848f134647SGleb Smirnoff 			if (i >= ntohs(ip->ip_len))
985df8bae1dSRodney W. Grimes 				goto dropfrag;
9866a800098SYoshinobu Inoue 			m_adj(m, i);
987db4f9cc7SJonathan Lemon 			m->m_pkthdr.csum_flags = 0;
9888f134647SGleb Smirnoff 			ip->ip_off = htons(ntohs(ip->ip_off) + i);
9898f134647SGleb Smirnoff 			ip->ip_len = htons(ntohs(ip->ip_len) - i);
990df8bae1dSRodney W. Grimes 		}
991af38c68cSLuigi Rizzo 		m->m_nextpkt = p->m_nextpkt;
992af38c68cSLuigi Rizzo 		p->m_nextpkt = m;
993af38c68cSLuigi Rizzo 	} else {
994af38c68cSLuigi Rizzo 		m->m_nextpkt = fp->ipq_frags;
995af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
996df8bae1dSRodney W. Grimes 	}
997df8bae1dSRodney W. Grimes 
998df8bae1dSRodney W. Grimes 	/*
999df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
1000df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
1001df8bae1dSRodney W. Grimes 	 */
10028f134647SGleb Smirnoff 	for (; q != NULL && ntohs(ip->ip_off) + ntohs(ip->ip_len) >
10038f134647SGleb Smirnoff 	    ntohs(GETIP(q)->ip_off); q = nq) {
10048f134647SGleb Smirnoff 		i = (ntohs(ip->ip_off) + ntohs(ip->ip_len)) -
10058f134647SGleb Smirnoff 		    ntohs(GETIP(q)->ip_off);
10068f134647SGleb Smirnoff 		if (i < ntohs(GETIP(q)->ip_len)) {
10078f134647SGleb Smirnoff 			GETIP(q)->ip_len = htons(ntohs(GETIP(q)->ip_len) - i);
10088f134647SGleb Smirnoff 			GETIP(q)->ip_off = htons(ntohs(GETIP(q)->ip_off) + i);
10096effc713SDoug Rabson 			m_adj(q, i);
1010db4f9cc7SJonathan Lemon 			q->m_pkthdr.csum_flags = 0;
1011df8bae1dSRodney W. Grimes 			break;
1012df8bae1dSRodney W. Grimes 		}
10136effc713SDoug Rabson 		nq = q->m_nextpkt;
1014af38c68cSLuigi Rizzo 		m->m_nextpkt = nq;
101586425c62SRobert Watson 		IPSTAT_INC(ips_fragdropped);
1016375386e2SMike Silbersack 		fp->ipq_nfrags--;
10176effc713SDoug Rabson 		m_freem(q);
1018df8bae1dSRodney W. Grimes 	}
1019df8bae1dSRodney W. Grimes 
1020df8bae1dSRodney W. Grimes 	/*
1021375386e2SMike Silbersack 	 * Check for complete reassembly and perform frag per packet
1022375386e2SMike Silbersack 	 * limiting.
1023375386e2SMike Silbersack 	 *
1024375386e2SMike Silbersack 	 * Frag limiting is performed here so that the nth frag has
1025375386e2SMike Silbersack 	 * a chance to complete the packet before we drop the packet.
1026375386e2SMike Silbersack 	 * As a result, n+1 frags are actually allowed per packet, but
1027375386e2SMike Silbersack 	 * only n will ever be stored. (n = maxfragsperpacket.)
1028375386e2SMike Silbersack 	 *
1029df8bae1dSRodney W. Grimes 	 */
10306effc713SDoug Rabson 	next = 0;
10316effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
10328f134647SGleb Smirnoff 		if (ntohs(GETIP(q)->ip_off) != next) {
1033603724d3SBjoern A. Zeeb 			if (fp->ipq_nfrags > V_maxfragsperpacket) {
103486425c62SRobert Watson 				IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1035375386e2SMike Silbersack 				ip_freef(head, fp);
103699e8617dSMaxim Konovalov 			}
1037f0cada84SAndre Oppermann 			goto done;
1038375386e2SMike Silbersack 		}
10398f134647SGleb Smirnoff 		next += ntohs(GETIP(q)->ip_len);
10406effc713SDoug Rabson 	}
10416effc713SDoug Rabson 	/* Make sure the last packet didn't have the IP_MF flag */
1042b09dc7e3SAndre Oppermann 	if (p->m_flags & M_IP_FRAG) {
1043603724d3SBjoern A. Zeeb 		if (fp->ipq_nfrags > V_maxfragsperpacket) {
104486425c62SRobert Watson 			IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1045375386e2SMike Silbersack 			ip_freef(head, fp);
104699e8617dSMaxim Konovalov 		}
1047f0cada84SAndre Oppermann 		goto done;
1048375386e2SMike Silbersack 	}
1049df8bae1dSRodney W. Grimes 
1050df8bae1dSRodney W. Grimes 	/*
1051430d30d8SBill Fenner 	 * Reassembly is complete.  Make sure the packet is a sane size.
1052430d30d8SBill Fenner 	 */
10536effc713SDoug Rabson 	q = fp->ipq_frags;
10546effc713SDoug Rabson 	ip = GETIP(q);
105553be11f6SPoul-Henning Kamp 	if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
105686425c62SRobert Watson 		IPSTAT_INC(ips_toolong);
105786425c62SRobert Watson 		IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1058462b86feSPoul-Henning Kamp 		ip_freef(head, fp);
1059f0cada84SAndre Oppermann 		goto done;
1060430d30d8SBill Fenner 	}
1061430d30d8SBill Fenner 
1062430d30d8SBill Fenner 	/*
1063430d30d8SBill Fenner 	 * Concatenate fragments.
1064df8bae1dSRodney W. Grimes 	 */
10656effc713SDoug Rabson 	m = q;
1066df8bae1dSRodney W. Grimes 	t = m->m_next;
106702410549SRobert Watson 	m->m_next = NULL;
1068df8bae1dSRodney W. Grimes 	m_cat(m, t);
10696effc713SDoug Rabson 	nq = q->m_nextpkt;
107002410549SRobert Watson 	q->m_nextpkt = NULL;
10716effc713SDoug Rabson 	for (q = nq; q != NULL; q = nq) {
10726effc713SDoug Rabson 		nq = q->m_nextpkt;
1073945aa40dSDoug Rabson 		q->m_nextpkt = NULL;
1074db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1075db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1076a8db1d93SJonathan Lemon 		m_cat(m, q);
1077df8bae1dSRodney W. Grimes 	}
10786edb555dSOleg Bulyzhin 	/*
10796edb555dSOleg Bulyzhin 	 * In order to do checksumming faster we do 'end-around carry' here
10806edb555dSOleg Bulyzhin 	 * (and not in for{} loop), though it implies we are not going to
10816edb555dSOleg Bulyzhin 	 * reassemble more than 64k fragments.
10826edb555dSOleg Bulyzhin 	 */
1083c732cd1aSPyun YongHyeon 	while (m->m_pkthdr.csum_data & 0xffff0000)
1084c732cd1aSPyun YongHyeon 		m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
1085c732cd1aSPyun YongHyeon 		    (m->m_pkthdr.csum_data >> 16);
108636b0360bSRobert Watson #ifdef MAC
108730d239bcSRobert Watson 	mac_ipq_reassemble(fp, m);
108830d239bcSRobert Watson 	mac_ipq_destroy(fp);
108936b0360bSRobert Watson #endif
1090df8bae1dSRodney W. Grimes 
1091df8bae1dSRodney W. Grimes 	/*
1092f0cada84SAndre Oppermann 	 * Create header for new ip packet by modifying header of first
1093f0cada84SAndre Oppermann 	 * packet;  dequeue and discard fragment reassembly header.
1094df8bae1dSRodney W. Grimes 	 * Make header visible.
1095df8bae1dSRodney W. Grimes 	 */
10968f134647SGleb Smirnoff 	ip->ip_len = htons((ip->ip_hl << 2) + next);
10976effc713SDoug Rabson 	ip->ip_src = fp->ipq_src;
10986effc713SDoug Rabson 	ip->ip_dst = fp->ipq_dst;
1099462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(head, fp, ipq_list);
1100603724d3SBjoern A. Zeeb 	V_nipq--;
1101603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
110253be11f6SPoul-Henning Kamp 	m->m_len += (ip->ip_hl << 2);
110353be11f6SPoul-Henning Kamp 	m->m_data -= (ip->ip_hl << 2);
1104df8bae1dSRodney W. Grimes 	/* some debugging cruft by sklower, below, will go away soon */
1105a5554bf0SPoul-Henning Kamp 	if (m->m_flags & M_PKTHDR)	/* XXX this should be done elsewhere */
1106a5554bf0SPoul-Henning Kamp 		m_fixhdr(m);
110786425c62SRobert Watson 	IPSTAT_INC(ips_reassembled);
1108f0cada84SAndre Oppermann 	IPQ_UNLOCK();
11096a800098SYoshinobu Inoue 	return (m);
1110df8bae1dSRodney W. Grimes 
1111df8bae1dSRodney W. Grimes dropfrag:
111286425c62SRobert Watson 	IPSTAT_INC(ips_fragdropped);
1113042bbfa3SRobert Watson 	if (fp != NULL)
1114375386e2SMike Silbersack 		fp->ipq_nfrags--;
1115df8bae1dSRodney W. Grimes 	m_freem(m);
1116f0cada84SAndre Oppermann done:
1117f0cada84SAndre Oppermann 	IPQ_UNLOCK();
1118f0cada84SAndre Oppermann 	return (NULL);
11196effc713SDoug Rabson 
11206effc713SDoug Rabson #undef GETIP
1121df8bae1dSRodney W. Grimes }
1122df8bae1dSRodney W. Grimes 
1123df8bae1dSRodney W. Grimes /*
1124df8bae1dSRodney W. Grimes  * Free a fragment reassembly header and all
1125df8bae1dSRodney W. Grimes  * associated datagrams.
1126df8bae1dSRodney W. Grimes  */
11270312fbe9SPoul-Henning Kamp static void
1128f2565d68SRobert Watson ip_freef(struct ipqhead *fhp, struct ipq *fp)
1129df8bae1dSRodney W. Grimes {
1130f2565d68SRobert Watson 	struct mbuf *q;
1131df8bae1dSRodney W. Grimes 
11322fad1e93SSam Leffler 	IPQ_LOCK_ASSERT();
11332fad1e93SSam Leffler 
11346effc713SDoug Rabson 	while (fp->ipq_frags) {
11356effc713SDoug Rabson 		q = fp->ipq_frags;
11366effc713SDoug Rabson 		fp->ipq_frags = q->m_nextpkt;
11376effc713SDoug Rabson 		m_freem(q);
1138df8bae1dSRodney W. Grimes 	}
1139462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(fhp, fp, ipq_list);
1140603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
1141603724d3SBjoern A. Zeeb 	V_nipq--;
1142df8bae1dSRodney W. Grimes }
1143df8bae1dSRodney W. Grimes 
1144df8bae1dSRodney W. Grimes /*
1145df8bae1dSRodney W. Grimes  * IP timer processing;
1146df8bae1dSRodney W. Grimes  * if a timer expires on a reassembly
1147df8bae1dSRodney W. Grimes  * queue, discard it.
1148df8bae1dSRodney W. Grimes  */
1149df8bae1dSRodney W. Grimes void
1150f2565d68SRobert Watson ip_slowtimo(void)
1151df8bae1dSRodney W. Grimes {
11528b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1153f2565d68SRobert Watson 	struct ipq *fp;
1154194a213eSAndrey A. Chernov 	int i;
1155df8bae1dSRodney W. Grimes 
11565ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
11572fad1e93SSam Leffler 	IPQ_LOCK();
11588b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
11598b615593SMarko Zec 		CURVNET_SET(vnet_iter);
1160194a213eSAndrey A. Chernov 		for (i = 0; i < IPREASS_NHASH; i++) {
1161603724d3SBjoern A. Zeeb 			for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) {
1162462b86feSPoul-Henning Kamp 				struct ipq *fpp;
1163462b86feSPoul-Henning Kamp 
1164462b86feSPoul-Henning Kamp 				fpp = fp;
1165462b86feSPoul-Henning Kamp 				fp = TAILQ_NEXT(fp, ipq_list);
1166462b86feSPoul-Henning Kamp 				if(--fpp->ipq_ttl == 0) {
116786425c62SRobert Watson 					IPSTAT_ADD(ips_fragtimeout,
116886425c62SRobert Watson 					    fpp->ipq_nfrags);
1169603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], fpp);
1170df8bae1dSRodney W. Grimes 				}
1171df8bae1dSRodney W. Grimes 			}
1172194a213eSAndrey A. Chernov 		}
1173690a6055SJesper Skriver 		/*
1174690a6055SJesper Skriver 		 * If we are over the maximum number of fragments
1175690a6055SJesper Skriver 		 * (due to the limit being lowered), drain off
1176690a6055SJesper Skriver 		 * enough to get down to the new limit.
1177690a6055SJesper Skriver 		 */
1178603724d3SBjoern A. Zeeb 		if (V_maxnipq >= 0 && V_nipq > V_maxnipq) {
1179690a6055SJesper Skriver 			for (i = 0; i < IPREASS_NHASH; i++) {
11808b615593SMarko Zec 				while (V_nipq > V_maxnipq &&
11818b615593SMarko Zec 				    !TAILQ_EMPTY(&V_ipq[i])) {
118286425c62SRobert Watson 					IPSTAT_ADD(ips_fragdropped,
118386425c62SRobert Watson 					    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
11848b615593SMarko Zec 					ip_freef(&V_ipq[i],
11858b615593SMarko Zec 					    TAILQ_FIRST(&V_ipq[i]));
1186690a6055SJesper Skriver 				}
1187690a6055SJesper Skriver 			}
1188690a6055SJesper Skriver 		}
11898b615593SMarko Zec 		CURVNET_RESTORE();
11908b615593SMarko Zec 	}
11912fad1e93SSam Leffler 	IPQ_UNLOCK();
11925ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
1193df8bae1dSRodney W. Grimes }
1194df8bae1dSRodney W. Grimes 
1195df8bae1dSRodney W. Grimes /*
1196df8bae1dSRodney W. Grimes  * Drain off all datagram fragments.
1197df8bae1dSRodney W. Grimes  */
11989802380eSBjoern A. Zeeb static void
11999802380eSBjoern A. Zeeb ip_drain_locked(void)
1200df8bae1dSRodney W. Grimes {
1201194a213eSAndrey A. Chernov 	int     i;
1202ce29ab3aSGarrett Wollman 
12039802380eSBjoern A. Zeeb 	IPQ_LOCK_ASSERT();
12049802380eSBjoern A. Zeeb 
1205194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1206603724d3SBjoern A. Zeeb 		while(!TAILQ_EMPTY(&V_ipq[i])) {
120786425c62SRobert Watson 			IPSTAT_ADD(ips_fragdropped,
120886425c62SRobert Watson 			    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
1209603724d3SBjoern A. Zeeb 			ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1210194a213eSAndrey A. Chernov 		}
1211194a213eSAndrey A. Chernov 	}
12129802380eSBjoern A. Zeeb }
12139802380eSBjoern A. Zeeb 
12149802380eSBjoern A. Zeeb void
12159802380eSBjoern A. Zeeb ip_drain(void)
12169802380eSBjoern A. Zeeb {
12179802380eSBjoern A. Zeeb 	VNET_ITERATOR_DECL(vnet_iter);
12189802380eSBjoern A. Zeeb 
12199802380eSBjoern A. Zeeb 	VNET_LIST_RLOCK_NOSLEEP();
12209802380eSBjoern A. Zeeb 	IPQ_LOCK();
12219802380eSBjoern A. Zeeb 	VNET_FOREACH(vnet_iter) {
12229802380eSBjoern A. Zeeb 		CURVNET_SET(vnet_iter);
12239802380eSBjoern A. Zeeb 		ip_drain_locked();
12248b615593SMarko Zec 		CURVNET_RESTORE();
12258b615593SMarko Zec 	}
12262fad1e93SSam Leffler 	IPQ_UNLOCK();
12275ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
1228ce29ab3aSGarrett Wollman 	in_rtqdrain();
1229df8bae1dSRodney W. Grimes }
1230df8bae1dSRodney W. Grimes 
1231df8bae1dSRodney W. Grimes /*
1232de38924dSAndre Oppermann  * The protocol to be inserted into ip_protox[] must be already registered
1233de38924dSAndre Oppermann  * in inetsw[], either statically or through pf_proto_register().
1234de38924dSAndre Oppermann  */
1235de38924dSAndre Oppermann int
12361b48d245SBjoern A. Zeeb ipproto_register(short ipproto)
1237de38924dSAndre Oppermann {
1238de38924dSAndre Oppermann 	struct protosw *pr;
1239de38924dSAndre Oppermann 
1240de38924dSAndre Oppermann 	/* Sanity checks. */
12411b48d245SBjoern A. Zeeb 	if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
1242de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1243de38924dSAndre Oppermann 
1244de38924dSAndre Oppermann 	/*
1245de38924dSAndre Oppermann 	 * The protocol slot must not be occupied by another protocol
1246de38924dSAndre Oppermann 	 * already.  An index pointing to IPPROTO_RAW is unused.
1247de38924dSAndre Oppermann 	 */
1248de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1249de38924dSAndre Oppermann 	if (pr == NULL)
1250de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1251de38924dSAndre Oppermann 	if (ip_protox[ipproto] != pr - inetsw)	/* IPPROTO_RAW */
1252de38924dSAndre Oppermann 		return (EEXIST);
1253de38924dSAndre Oppermann 
1254de38924dSAndre Oppermann 	/* Find the protocol position in inetsw[] and set the index. */
1255de38924dSAndre Oppermann 	for (pr = inetdomain.dom_protosw;
1256de38924dSAndre Oppermann 	     pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1257de38924dSAndre Oppermann 		if (pr->pr_domain->dom_family == PF_INET &&
1258de38924dSAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol == ipproto) {
1259de38924dSAndre Oppermann 			ip_protox[pr->pr_protocol] = pr - inetsw;
1260de38924dSAndre Oppermann 			return (0);
1261de38924dSAndre Oppermann 		}
1262de38924dSAndre Oppermann 	}
1263de38924dSAndre Oppermann 	return (EPROTONOSUPPORT);
1264de38924dSAndre Oppermann }
1265de38924dSAndre Oppermann 
1266de38924dSAndre Oppermann int
12671b48d245SBjoern A. Zeeb ipproto_unregister(short ipproto)
1268de38924dSAndre Oppermann {
1269de38924dSAndre Oppermann 	struct protosw *pr;
1270de38924dSAndre Oppermann 
1271de38924dSAndre Oppermann 	/* Sanity checks. */
12721b48d245SBjoern A. Zeeb 	if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
1273de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1274de38924dSAndre Oppermann 
1275de38924dSAndre Oppermann 	/* Check if the protocol was indeed registered. */
1276de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1277de38924dSAndre Oppermann 	if (pr == NULL)
1278de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1279de38924dSAndre Oppermann 	if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
1280de38924dSAndre Oppermann 		return (ENOENT);
1281de38924dSAndre Oppermann 
1282de38924dSAndre Oppermann 	/* Reset the protocol slot to IPPROTO_RAW. */
1283de38924dSAndre Oppermann 	ip_protox[ipproto] = pr - inetsw;
1284de38924dSAndre Oppermann 	return (0);
1285de38924dSAndre Oppermann }
1286de38924dSAndre Oppermann 
1287df8bae1dSRodney W. Grimes /*
12888c0fec80SRobert Watson  * Given address of next destination (final or next hop), return (referenced)
12898c0fec80SRobert Watson  * internet address info of interface to be used to get there.
1290df8bae1dSRodney W. Grimes  */
1291bd714208SRuslan Ermilov struct in_ifaddr *
12928b07e49aSJulian Elischer ip_rtaddr(struct in_addr dst, u_int fibnum)
1293df8bae1dSRodney W. Grimes {
129497d8d152SAndre Oppermann 	struct route sro;
129502c1c707SAndre Oppermann 	struct sockaddr_in *sin;
129619e5b0a7SRobert Watson 	struct in_ifaddr *ia;
1297df8bae1dSRodney W. Grimes 
12980cfbbe3bSAndre Oppermann 	bzero(&sro, sizeof(sro));
129997d8d152SAndre Oppermann 	sin = (struct sockaddr_in *)&sro.ro_dst;
1300df8bae1dSRodney W. Grimes 	sin->sin_family = AF_INET;
1301df8bae1dSRodney W. Grimes 	sin->sin_len = sizeof(*sin);
1302df8bae1dSRodney W. Grimes 	sin->sin_addr = dst;
13036e6b3f7cSQing Li 	in_rtalloc_ign(&sro, 0, fibnum);
1304df8bae1dSRodney W. Grimes 
130597d8d152SAndre Oppermann 	if (sro.ro_rt == NULL)
130602410549SRobert Watson 		return (NULL);
130702c1c707SAndre Oppermann 
130819e5b0a7SRobert Watson 	ia = ifatoia(sro.ro_rt->rt_ifa);
130919e5b0a7SRobert Watson 	ifa_ref(&ia->ia_ifa);
131097d8d152SAndre Oppermann 	RTFREE(sro.ro_rt);
131119e5b0a7SRobert Watson 	return (ia);
1312df8bae1dSRodney W. Grimes }
1313df8bae1dSRodney W. Grimes 
1314df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = {
1315df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1316df8bae1dSRodney W. Grimes 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1317df8bae1dSRodney W. Grimes 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1318df8bae1dSRodney W. Grimes 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1319fcaf9f91SMike Silbersack 	0,		0,		EHOSTUNREACH,	0,
13203b8123b7SJesper Skriver 	ENOPROTOOPT,	ECONNREFUSED
1321df8bae1dSRodney W. Grimes };
1322df8bae1dSRodney W. Grimes 
1323df8bae1dSRodney W. Grimes /*
1324df8bae1dSRodney W. Grimes  * Forward a packet.  If some error occurs return the sender
1325df8bae1dSRodney W. Grimes  * an icmp packet.  Note we can't always generate a meaningful
1326df8bae1dSRodney W. Grimes  * icmp message because icmp doesn't have a large enough repertoire
1327df8bae1dSRodney W. Grimes  * of codes and types.
1328df8bae1dSRodney W. Grimes  *
1329df8bae1dSRodney W. Grimes  * If not forwarding, just drop the packet.  This could be confusing
1330df8bae1dSRodney W. Grimes  * if ipforwarding was zero but some routing protocol was advancing
1331df8bae1dSRodney W. Grimes  * us as a gateway to somewhere.  However, we must let the routing
1332df8bae1dSRodney W. Grimes  * protocol deal with that.
1333df8bae1dSRodney W. Grimes  *
1334df8bae1dSRodney W. Grimes  * The srcrt parameter indicates whether the packet is being forwarded
1335df8bae1dSRodney W. Grimes  * via a source route.
1336df8bae1dSRodney W. Grimes  */
13379b932e9eSAndre Oppermann void
13389b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt)
1339df8bae1dSRodney W. Grimes {
13402b25acc1SLuigi Rizzo 	struct ip *ip = mtod(m, struct ip *);
1341efbad259SEdward Tomasz Napierala 	struct in_ifaddr *ia;
1342df8bae1dSRodney W. Grimes 	struct mbuf *mcopy;
13439b932e9eSAndre Oppermann 	struct in_addr dest;
1344b835b6feSBjoern A. Zeeb 	struct route ro;
1345c773494eSAndre Oppermann 	int error, type = 0, code = 0, mtu = 0;
13463efc3014SJulian Elischer 
13479b932e9eSAndre Oppermann 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
134886425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
1349df8bae1dSRodney W. Grimes 		m_freem(m);
1350df8bae1dSRodney W. Grimes 		return;
1351df8bae1dSRodney W. Grimes 	}
13521b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
1353603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
13541b968362SDag-Erling Smørgrav #endif
1355df8bae1dSRodney W. Grimes 		if (ip->ip_ttl <= IPTTLDEC) {
13561b968362SDag-Erling Smørgrav 			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
135702c1c707SAndre Oppermann 			    0, 0);
1358df8bae1dSRodney W. Grimes 			return;
1359df8bae1dSRodney W. Grimes 		}
13601b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
13611b968362SDag-Erling Smørgrav 	}
13621b968362SDag-Erling Smørgrav #endif
1363df8bae1dSRodney W. Grimes 
13648b07e49aSJulian Elischer 	ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
1365efbad259SEdward Tomasz Napierala #ifndef IPSEC
1366efbad259SEdward Tomasz Napierala 	/*
1367efbad259SEdward Tomasz Napierala 	 * 'ia' may be NULL if there is no route for this destination.
1368efbad259SEdward Tomasz Napierala 	 * In case of IPsec, Don't discard it just yet, but pass it to
1369efbad259SEdward Tomasz Napierala 	 * ip_output in case of outgoing IPsec policy.
1370efbad259SEdward Tomasz Napierala 	 */
1371d23d475fSGuido van Rooij 	if (!srcrt && ia == NULL) {
137202c1c707SAndre Oppermann 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1373df8bae1dSRodney W. Grimes 		return;
137402c1c707SAndre Oppermann 	}
1375efbad259SEdward Tomasz Napierala #endif
1376df8bae1dSRodney W. Grimes 
1377df8bae1dSRodney W. Grimes 	/*
1378bfef7ed4SIan Dowse 	 * Save the IP header and at most 8 bytes of the payload,
1379bfef7ed4SIan Dowse 	 * in case we need to generate an ICMP message to the src.
1380bfef7ed4SIan Dowse 	 *
13814d2e3692SLuigi Rizzo 	 * XXX this can be optimized a lot by saving the data in a local
13824d2e3692SLuigi Rizzo 	 * buffer on the stack (72 bytes at most), and only allocating the
13834d2e3692SLuigi Rizzo 	 * mbuf if really necessary. The vast majority of the packets
13844d2e3692SLuigi Rizzo 	 * are forwarded without having to send an ICMP back (either
13854d2e3692SLuigi Rizzo 	 * because unnecessary, or because rate limited), so we are
13864d2e3692SLuigi Rizzo 	 * really we are wasting a lot of work here.
13874d2e3692SLuigi Rizzo 	 *
1388bfef7ed4SIan Dowse 	 * We don't use m_copy() because it might return a reference
1389bfef7ed4SIan Dowse 	 * to a shared cluster. Both this function and ip_output()
1390bfef7ed4SIan Dowse 	 * assume exclusive access to the IP header in `m', so any
1391bfef7ed4SIan Dowse 	 * data in a cluster may change before we reach icmp_error().
1392df8bae1dSRodney W. Grimes 	 */
1393dc4ad05eSGleb Smirnoff 	mcopy = m_gethdr(M_NOWAIT, m->m_type);
1394eb1b1807SGleb Smirnoff 	if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) {
13959967cafcSSam Leffler 		/*
13969967cafcSSam Leffler 		 * It's probably ok if the pkthdr dup fails (because
13979967cafcSSam Leffler 		 * the deep copy of the tag chain failed), but for now
13989967cafcSSam Leffler 		 * be conservative and just discard the copy since
13999967cafcSSam Leffler 		 * code below may some day want the tags.
14009967cafcSSam Leffler 		 */
14019967cafcSSam Leffler 		m_free(mcopy);
14029967cafcSSam Leffler 		mcopy = NULL;
14039967cafcSSam Leffler 	}
1404bfef7ed4SIan Dowse 	if (mcopy != NULL) {
14058f134647SGleb Smirnoff 		mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy));
1406e6b0a570SBruce M Simpson 		mcopy->m_pkthdr.len = mcopy->m_len;
1407bfef7ed4SIan Dowse 		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1408bfef7ed4SIan Dowse 	}
140904287599SRuslan Ermilov 
141004287599SRuslan Ermilov #ifdef IPSTEALTH
1411603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
141204287599SRuslan Ermilov #endif
141304287599SRuslan Ermilov 		ip->ip_ttl -= IPTTLDEC;
141404287599SRuslan Ermilov #ifdef IPSTEALTH
141504287599SRuslan Ermilov 	}
141604287599SRuslan Ermilov #endif
1417df8bae1dSRodney W. Grimes 
1418df8bae1dSRodney W. Grimes 	/*
1419df8bae1dSRodney W. Grimes 	 * If forwarding packet using same interface that it came in on,
1420df8bae1dSRodney W. Grimes 	 * perhaps should send a redirect to sender to shortcut a hop.
1421df8bae1dSRodney W. Grimes 	 * Only send redirect if source is sending directly to us,
1422df8bae1dSRodney W. Grimes 	 * and if packet was not source routed (or has any options).
1423df8bae1dSRodney W. Grimes 	 * Also, don't send redirect if forwarding using a default route
1424df8bae1dSRodney W. Grimes 	 * or a route modified by a redirect.
1425df8bae1dSRodney W. Grimes 	 */
14269b932e9eSAndre Oppermann 	dest.s_addr = 0;
1427efbad259SEdward Tomasz Napierala 	if (!srcrt && V_ipsendredirects &&
1428efbad259SEdward Tomasz Napierala 	    ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) {
142902c1c707SAndre Oppermann 		struct sockaddr_in *sin;
143002c1c707SAndre Oppermann 		struct rtentry *rt;
143102c1c707SAndre Oppermann 
14320cfbbe3bSAndre Oppermann 		bzero(&ro, sizeof(ro));
143302c1c707SAndre Oppermann 		sin = (struct sockaddr_in *)&ro.ro_dst;
143402c1c707SAndre Oppermann 		sin->sin_family = AF_INET;
143502c1c707SAndre Oppermann 		sin->sin_len = sizeof(*sin);
14369b932e9eSAndre Oppermann 		sin->sin_addr = ip->ip_dst;
14376e6b3f7cSQing Li 		in_rtalloc_ign(&ro, 0, M_GETFIB(m));
143802c1c707SAndre Oppermann 
143902c1c707SAndre Oppermann 		rt = ro.ro_rt;
144002c1c707SAndre Oppermann 
144102c1c707SAndre Oppermann 		if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
14429b932e9eSAndre Oppermann 		    satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1443df8bae1dSRodney W. Grimes #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1444df8bae1dSRodney W. Grimes 			u_long src = ntohl(ip->ip_src.s_addr);
1445df8bae1dSRodney W. Grimes 
1446df8bae1dSRodney W. Grimes 			if (RTA(rt) &&
1447df8bae1dSRodney W. Grimes 			    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1448df8bae1dSRodney W. Grimes 				if (rt->rt_flags & RTF_GATEWAY)
14499b932e9eSAndre Oppermann 					dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1450df8bae1dSRodney W. Grimes 				else
14519b932e9eSAndre Oppermann 					dest.s_addr = ip->ip_dst.s_addr;
1452df8bae1dSRodney W. Grimes 				/* Router requirements says to only send host redirects */
1453df8bae1dSRodney W. Grimes 				type = ICMP_REDIRECT;
1454df8bae1dSRodney W. Grimes 				code = ICMP_REDIRECT_HOST;
1455df8bae1dSRodney W. Grimes 			}
1456df8bae1dSRodney W. Grimes 		}
145702c1c707SAndre Oppermann 		if (rt)
145802c1c707SAndre Oppermann 			RTFREE(rt);
145902c1c707SAndre Oppermann 	}
1460df8bae1dSRodney W. Grimes 
1461b835b6feSBjoern A. Zeeb 	/*
1462b835b6feSBjoern A. Zeeb 	 * Try to cache the route MTU from ip_output so we can consider it for
1463b835b6feSBjoern A. Zeeb 	 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191.
1464b835b6feSBjoern A. Zeeb 	 */
1465b835b6feSBjoern A. Zeeb 	bzero(&ro, sizeof(ro));
1466b835b6feSBjoern A. Zeeb 
1467b835b6feSBjoern A. Zeeb 	error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1468b835b6feSBjoern A. Zeeb 
1469b835b6feSBjoern A. Zeeb 	if (error == EMSGSIZE && ro.ro_rt)
1470e3a7aa6fSGleb Smirnoff 		mtu = ro.ro_rt->rt_mtu;
1471bf984051SGleb Smirnoff 	RO_RTFREE(&ro);
1472b835b6feSBjoern A. Zeeb 
1473df8bae1dSRodney W. Grimes 	if (error)
147486425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
1475df8bae1dSRodney W. Grimes 	else {
147686425c62SRobert Watson 		IPSTAT_INC(ips_forward);
1477df8bae1dSRodney W. Grimes 		if (type)
147886425c62SRobert Watson 			IPSTAT_INC(ips_redirectsent);
1479df8bae1dSRodney W. Grimes 		else {
14809188b4a1SAndre Oppermann 			if (mcopy)
1481df8bae1dSRodney W. Grimes 				m_freem(mcopy);
14828c0fec80SRobert Watson 			if (ia != NULL)
14838c0fec80SRobert Watson 				ifa_free(&ia->ia_ifa);
1484df8bae1dSRodney W. Grimes 			return;
1485df8bae1dSRodney W. Grimes 		}
1486df8bae1dSRodney W. Grimes 	}
14878c0fec80SRobert Watson 	if (mcopy == NULL) {
14888c0fec80SRobert Watson 		if (ia != NULL)
14898c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
1490df8bae1dSRodney W. Grimes 		return;
14918c0fec80SRobert Watson 	}
1492df8bae1dSRodney W. Grimes 
1493df8bae1dSRodney W. Grimes 	switch (error) {
1494df8bae1dSRodney W. Grimes 
1495df8bae1dSRodney W. Grimes 	case 0:				/* forwarded, but need redirect */
1496df8bae1dSRodney W. Grimes 		/* type, code set above */
1497df8bae1dSRodney W. Grimes 		break;
1498df8bae1dSRodney W. Grimes 
1499efbad259SEdward Tomasz Napierala 	case ENETUNREACH:
1500df8bae1dSRodney W. Grimes 	case EHOSTUNREACH:
1501df8bae1dSRodney W. Grimes 	case ENETDOWN:
1502df8bae1dSRodney W. Grimes 	case EHOSTDOWN:
1503df8bae1dSRodney W. Grimes 	default:
1504df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1505df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_HOST;
1506df8bae1dSRodney W. Grimes 		break;
1507df8bae1dSRodney W. Grimes 
1508df8bae1dSRodney W. Grimes 	case EMSGSIZE:
1509df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1510df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_NEEDFRAG;
15111dfcf0d2SAndre Oppermann 
1512b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
1513b835b6feSBjoern A. Zeeb 		/*
1514b835b6feSBjoern A. Zeeb 		 * If IPsec is configured for this path,
1515b835b6feSBjoern A. Zeeb 		 * override any possibly mtu value set by ip_output.
1516b835b6feSBjoern A. Zeeb 		 */
15171c044382SBjoern A. Zeeb 		mtu = ip_ipsec_mtu(mcopy, mtu);
1518b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
15199b932e9eSAndre Oppermann 		/*
1520b835b6feSBjoern A. Zeeb 		 * If the MTU was set before make sure we are below the
1521b835b6feSBjoern A. Zeeb 		 * interface MTU.
1522ab48768bSAndre Oppermann 		 * If the MTU wasn't set before use the interface mtu or
1523ab48768bSAndre Oppermann 		 * fall back to the next smaller mtu step compared to the
1524ab48768bSAndre Oppermann 		 * current packet size.
15259b932e9eSAndre Oppermann 		 */
1526b835b6feSBjoern A. Zeeb 		if (mtu != 0) {
1527b835b6feSBjoern A. Zeeb 			if (ia != NULL)
1528b835b6feSBjoern A. Zeeb 				mtu = min(mtu, ia->ia_ifp->if_mtu);
1529b835b6feSBjoern A. Zeeb 		} else {
1530ab48768bSAndre Oppermann 			if (ia != NULL)
1531c773494eSAndre Oppermann 				mtu = ia->ia_ifp->if_mtu;
1532ab48768bSAndre Oppermann 			else
15338f134647SGleb Smirnoff 				mtu = ip_next_mtu(ntohs(ip->ip_len), 0);
1534ab48768bSAndre Oppermann 		}
153586425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
1536df8bae1dSRodney W. Grimes 		break;
1537df8bae1dSRodney W. Grimes 
1538df8bae1dSRodney W. Grimes 	case ENOBUFS:
1539df285b3dSMike Silbersack 		/*
1540df285b3dSMike Silbersack 		 * A router should not generate ICMP_SOURCEQUENCH as
1541df285b3dSMike Silbersack 		 * required in RFC1812 Requirements for IP Version 4 Routers.
1542df285b3dSMike Silbersack 		 * Source quench could be a big problem under DoS attacks,
1543df285b3dSMike Silbersack 		 * or if the underlying interface is rate-limited.
1544df285b3dSMike Silbersack 		 * Those who need source quench packets may re-enable them
1545df285b3dSMike Silbersack 		 * via the net.inet.ip.sendsourcequench sysctl.
1546df285b3dSMike Silbersack 		 */
1547603724d3SBjoern A. Zeeb 		if (V_ip_sendsourcequench == 0) {
1548df285b3dSMike Silbersack 			m_freem(mcopy);
15498c0fec80SRobert Watson 			if (ia != NULL)
15508c0fec80SRobert Watson 				ifa_free(&ia->ia_ifa);
1551df285b3dSMike Silbersack 			return;
1552df285b3dSMike Silbersack 		} else {
1553df8bae1dSRodney W. Grimes 			type = ICMP_SOURCEQUENCH;
1554df8bae1dSRodney W. Grimes 			code = 0;
1555df285b3dSMike Silbersack 		}
1556df8bae1dSRodney W. Grimes 		break;
15573a06e3e0SRuslan Ermilov 
15583a06e3e0SRuslan Ermilov 	case EACCES:			/* ipfw denied packet */
15593a06e3e0SRuslan Ermilov 		m_freem(mcopy);
15608c0fec80SRobert Watson 		if (ia != NULL)
15618c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
15623a06e3e0SRuslan Ermilov 		return;
1563df8bae1dSRodney W. Grimes 	}
15648c0fec80SRobert Watson 	if (ia != NULL)
15658c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
1566c773494eSAndre Oppermann 	icmp_error(mcopy, type, code, dest.s_addr, mtu);
1567df8bae1dSRodney W. Grimes }
1568df8bae1dSRodney W. Grimes 
156982c23ebaSBill Fenner void
1570f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1571f2565d68SRobert Watson     struct mbuf *m)
157282c23ebaSBill Fenner {
15738b615593SMarko Zec 
1574be8a62e8SPoul-Henning Kamp 	if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1575be8a62e8SPoul-Henning Kamp 		struct bintime bt;
1576be8a62e8SPoul-Henning Kamp 
1577be8a62e8SPoul-Henning Kamp 		bintime(&bt);
1578be8a62e8SPoul-Henning Kamp 		if (inp->inp_socket->so_options & SO_BINTIME) {
1579be8a62e8SPoul-Henning Kamp 			*mp = sbcreatecontrol((caddr_t)&bt, sizeof(bt),
1580be8a62e8SPoul-Henning Kamp 			    SCM_BINTIME, SOL_SOCKET);
1581be8a62e8SPoul-Henning Kamp 			if (*mp)
1582be8a62e8SPoul-Henning Kamp 				mp = &(*mp)->m_next;
1583be8a62e8SPoul-Henning Kamp 		}
158482c23ebaSBill Fenner 		if (inp->inp_socket->so_options & SO_TIMESTAMP) {
158582c23ebaSBill Fenner 			struct timeval tv;
158682c23ebaSBill Fenner 
1587be8a62e8SPoul-Henning Kamp 			bintime2timeval(&bt, &tv);
158882c23ebaSBill Fenner 			*mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv),
158982c23ebaSBill Fenner 			    SCM_TIMESTAMP, SOL_SOCKET);
159082c23ebaSBill Fenner 			if (*mp)
159182c23ebaSBill Fenner 				mp = &(*mp)->m_next;
15924cc20ab1SSeigo Tanimura 		}
1593be8a62e8SPoul-Henning Kamp 	}
159482c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVDSTADDR) {
159582c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t)&ip->ip_dst,
159682c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
159782c23ebaSBill Fenner 		if (*mp)
159882c23ebaSBill Fenner 			mp = &(*mp)->m_next;
159982c23ebaSBill Fenner 	}
16004957466bSMatthew N. Dodd 	if (inp->inp_flags & INP_RECVTTL) {
16014957466bSMatthew N. Dodd 		*mp = sbcreatecontrol((caddr_t)&ip->ip_ttl,
16024957466bSMatthew N. Dodd 		    sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
16034957466bSMatthew N. Dodd 		if (*mp)
16044957466bSMatthew N. Dodd 			mp = &(*mp)->m_next;
16054957466bSMatthew N. Dodd 	}
160682c23ebaSBill Fenner #ifdef notyet
160782c23ebaSBill Fenner 	/* XXX
160882c23ebaSBill Fenner 	 * Moving these out of udp_input() made them even more broken
160982c23ebaSBill Fenner 	 * than they already were.
161082c23ebaSBill Fenner 	 */
161182c23ebaSBill Fenner 	/* options were tossed already */
161282c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVOPTS) {
161382c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t)opts_deleted_above,
161482c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
161582c23ebaSBill Fenner 		if (*mp)
161682c23ebaSBill Fenner 			mp = &(*mp)->m_next;
161782c23ebaSBill Fenner 	}
161882c23ebaSBill Fenner 	/* ip_srcroute doesn't do what we want here, need to fix */
161982c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVRETOPTS) {
1620e0982661SAndre Oppermann 		*mp = sbcreatecontrol((caddr_t)ip_srcroute(m),
162182c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
162282c23ebaSBill Fenner 		if (*mp)
162382c23ebaSBill Fenner 			mp = &(*mp)->m_next;
162482c23ebaSBill Fenner 	}
162582c23ebaSBill Fenner #endif
162682c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVIF) {
1627d314ad7bSJulian Elischer 		struct ifnet *ifp;
1628d314ad7bSJulian Elischer 		struct sdlbuf {
162982c23ebaSBill Fenner 			struct sockaddr_dl sdl;
1630d314ad7bSJulian Elischer 			u_char	pad[32];
1631d314ad7bSJulian Elischer 		} sdlbuf;
1632d314ad7bSJulian Elischer 		struct sockaddr_dl *sdp;
1633d314ad7bSJulian Elischer 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
163482c23ebaSBill Fenner 
163546f2df9cSSergey Kandaurov 		if ((ifp = m->m_pkthdr.rcvif) &&
163646f2df9cSSergey Kandaurov 		    ifp->if_index && ifp->if_index <= V_if_index) {
16374a0d6638SRuslan Ermilov 			sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1638d314ad7bSJulian Elischer 			/*
1639d314ad7bSJulian Elischer 			 * Change our mind and don't try copy.
1640d314ad7bSJulian Elischer 			 */
164146f2df9cSSergey Kandaurov 			if (sdp->sdl_family != AF_LINK ||
164246f2df9cSSergey Kandaurov 			    sdp->sdl_len > sizeof(sdlbuf)) {
1643d314ad7bSJulian Elischer 				goto makedummy;
1644d314ad7bSJulian Elischer 			}
1645d314ad7bSJulian Elischer 			bcopy(sdp, sdl2, sdp->sdl_len);
1646d314ad7bSJulian Elischer 		} else {
1647d314ad7bSJulian Elischer makedummy:
164846f2df9cSSergey Kandaurov 			sdl2->sdl_len =
164946f2df9cSSergey Kandaurov 			    offsetof(struct sockaddr_dl, sdl_data[0]);
1650d314ad7bSJulian Elischer 			sdl2->sdl_family = AF_LINK;
1651d314ad7bSJulian Elischer 			sdl2->sdl_index = 0;
1652d314ad7bSJulian Elischer 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1653d314ad7bSJulian Elischer 		}
1654d314ad7bSJulian Elischer 		*mp = sbcreatecontrol((caddr_t)sdl2, sdl2->sdl_len,
165582c23ebaSBill Fenner 		    IP_RECVIF, IPPROTO_IP);
165682c23ebaSBill Fenner 		if (*mp)
165782c23ebaSBill Fenner 			mp = &(*mp)->m_next;
165882c23ebaSBill Fenner 	}
16593cca425bSMichael Tuexen 	if (inp->inp_flags & INP_RECVTOS) {
16603cca425bSMichael Tuexen 		*mp = sbcreatecontrol((caddr_t)&ip->ip_tos,
16613cca425bSMichael Tuexen 		    sizeof(u_char), IP_RECVTOS, IPPROTO_IP);
16623cca425bSMichael Tuexen 		if (*mp)
16633cca425bSMichael Tuexen 			mp = &(*mp)->m_next;
16643cca425bSMichael Tuexen 	}
1665*9d3ddf43SAdrian Chadd 
1666*9d3ddf43SAdrian Chadd 	if (inp->inp_flags2 & INP_RECVFLOWID) {
1667*9d3ddf43SAdrian Chadd 		uint32_t flowid, flow_type;
1668*9d3ddf43SAdrian Chadd 
1669*9d3ddf43SAdrian Chadd 		flowid = m->m_pkthdr.flowid;
1670*9d3ddf43SAdrian Chadd 		flow_type = M_HASHTYPE_GET(m);
1671*9d3ddf43SAdrian Chadd 
1672*9d3ddf43SAdrian Chadd 		/*
1673*9d3ddf43SAdrian Chadd 		 * XXX should handle the failure of one or the
1674*9d3ddf43SAdrian Chadd 		 * other - don't populate both?
1675*9d3ddf43SAdrian Chadd 		 */
1676*9d3ddf43SAdrian Chadd 		*mp = sbcreatecontrol((caddr_t) &flowid,
1677*9d3ddf43SAdrian Chadd 		    sizeof(uint32_t), IP_FLOWID, IPPROTO_IP);
1678*9d3ddf43SAdrian Chadd 		if (*mp)
1679*9d3ddf43SAdrian Chadd 			mp = &(*mp)->m_next;
1680*9d3ddf43SAdrian Chadd 		*mp = sbcreatecontrol((caddr_t) &flow_type,
1681*9d3ddf43SAdrian Chadd 		    sizeof(uint32_t), IP_FLOWTYPE, IPPROTO_IP);
1682*9d3ddf43SAdrian Chadd 		if (*mp)
1683*9d3ddf43SAdrian Chadd 			mp = &(*mp)->m_next;
1684*9d3ddf43SAdrian Chadd 	}
1685*9d3ddf43SAdrian Chadd 
1686*9d3ddf43SAdrian Chadd #ifdef	RSS
1687*9d3ddf43SAdrian Chadd 	if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
1688*9d3ddf43SAdrian Chadd 		uint32_t flowid, flow_type;
1689*9d3ddf43SAdrian Chadd 		uint32_t rss_bucketid;
1690*9d3ddf43SAdrian Chadd 
1691*9d3ddf43SAdrian Chadd 		flowid = m->m_pkthdr.flowid;
1692*9d3ddf43SAdrian Chadd 		flow_type = M_HASHTYPE_GET(m);
1693*9d3ddf43SAdrian Chadd 
1694*9d3ddf43SAdrian Chadd 		if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
1695*9d3ddf43SAdrian Chadd 			*mp = sbcreatecontrol((caddr_t) &rss_bucketid,
1696*9d3ddf43SAdrian Chadd 			   sizeof(uint32_t), IP_RSSBUCKETID, IPPROTO_IP);
1697*9d3ddf43SAdrian Chadd 			if (*mp)
1698*9d3ddf43SAdrian Chadd 				mp = &(*mp)->m_next;
1699*9d3ddf43SAdrian Chadd 		}
1700*9d3ddf43SAdrian Chadd 	}
1701*9d3ddf43SAdrian Chadd #endif
170282c23ebaSBill Fenner }
170382c23ebaSBill Fenner 
17044d2e3692SLuigi Rizzo /*
170530916a2dSRobert Watson  * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
170630916a2dSRobert Watson  * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
170730916a2dSRobert Watson  * locking.  This code remains in ip_input.c as ip_mroute.c is optionally
170830916a2dSRobert Watson  * compiled.
17094d2e3692SLuigi Rizzo  */
17103e288e62SDimitry Andric static VNET_DEFINE(int, ip_rsvp_on);
171182cea7e6SBjoern A. Zeeb VNET_DEFINE(struct socket *, ip_rsvpd);
171282cea7e6SBjoern A. Zeeb 
171382cea7e6SBjoern A. Zeeb #define	V_ip_rsvp_on		VNET(ip_rsvp_on)
171482cea7e6SBjoern A. Zeeb 
1715df8bae1dSRodney W. Grimes int
1716f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so)
1717f0068c4aSGarrett Wollman {
17188b615593SMarko Zec 
1719f0068c4aSGarrett Wollman 	if (so->so_type != SOCK_RAW ||
1720f0068c4aSGarrett Wollman 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1721f0068c4aSGarrett Wollman 		return EOPNOTSUPP;
1722f0068c4aSGarrett Wollman 
1723603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL)
1724f0068c4aSGarrett Wollman 		return EADDRINUSE;
1725f0068c4aSGarrett Wollman 
1726603724d3SBjoern A. Zeeb 	V_ip_rsvpd = so;
17271c5de19aSGarrett Wollman 	/*
17281c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-increment
17291c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
17301c5de19aSGarrett Wollman 	 */
1731603724d3SBjoern A. Zeeb 	if (!V_ip_rsvp_on) {
1732603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 1;
1733603724d3SBjoern A. Zeeb 		V_rsvp_on++;
17341c5de19aSGarrett Wollman 	}
1735f0068c4aSGarrett Wollman 
1736f0068c4aSGarrett Wollman 	return 0;
1737f0068c4aSGarrett Wollman }
1738f0068c4aSGarrett Wollman 
1739f0068c4aSGarrett Wollman int
1740f0068c4aSGarrett Wollman ip_rsvp_done(void)
1741f0068c4aSGarrett Wollman {
17428b615593SMarko Zec 
1743603724d3SBjoern A. Zeeb 	V_ip_rsvpd = NULL;
17441c5de19aSGarrett Wollman 	/*
17451c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-decrement
17461c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
17471c5de19aSGarrett Wollman 	 */
1748603724d3SBjoern A. Zeeb 	if (V_ip_rsvp_on) {
1749603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 0;
1750603724d3SBjoern A. Zeeb 		V_rsvp_on--;
17511c5de19aSGarrett Wollman 	}
1752f0068c4aSGarrett Wollman 	return 0;
1753f0068c4aSGarrett Wollman }
1754bbb4330bSLuigi Rizzo 
17558f5a8818SKevin Lo int
17568f5a8818SKevin Lo rsvp_input(struct mbuf **mp, int *offp, int proto)
1757bbb4330bSLuigi Rizzo {
17588f5a8818SKevin Lo 	struct mbuf *m;
17598f5a8818SKevin Lo 
17608f5a8818SKevin Lo 	m = *mp;
17618f5a8818SKevin Lo 	*mp = NULL;
17628b615593SMarko Zec 
1763bbb4330bSLuigi Rizzo 	if (rsvp_input_p) { /* call the real one if loaded */
17648f5a8818SKevin Lo 		*mp = m;
17658f5a8818SKevin Lo 		rsvp_input_p(mp, offp, proto);
17668f5a8818SKevin Lo 		return (IPPROTO_DONE);
1767bbb4330bSLuigi Rizzo 	}
1768bbb4330bSLuigi Rizzo 
1769bbb4330bSLuigi Rizzo 	/* Can still get packets with rsvp_on = 0 if there is a local member
1770bbb4330bSLuigi Rizzo 	 * of the group to which the RSVP packet is addressed.  But in this
1771bbb4330bSLuigi Rizzo 	 * case we want to throw the packet away.
1772bbb4330bSLuigi Rizzo 	 */
1773bbb4330bSLuigi Rizzo 
1774603724d3SBjoern A. Zeeb 	if (!V_rsvp_on) {
1775bbb4330bSLuigi Rizzo 		m_freem(m);
17768f5a8818SKevin Lo 		return (IPPROTO_DONE);
1777bbb4330bSLuigi Rizzo 	}
1778bbb4330bSLuigi Rizzo 
1779603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL) {
17808f5a8818SKevin Lo 		*mp = m;
17818f5a8818SKevin Lo 		rip_input(mp, offp, proto);
17828f5a8818SKevin Lo 		return (IPPROTO_DONE);
1783bbb4330bSLuigi Rizzo 	}
1784bbb4330bSLuigi Rizzo 	/* Drop the packet */
1785bbb4330bSLuigi Rizzo 	m_freem(m);
17868f5a8818SKevin Lo 	return (IPPROTO_DONE);
1787bbb4330bSLuigi Rizzo }
1788