xref: /freebsd/sys/netinet/ip_input.c (revision b8bc95cd49446231025df4b1e66b254dff00805f)
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"
40*b8bc95cdSAdrian Chadd #include "opt_rss.h"
4174a9466cSGary Palmer 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
44df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
45b715f178SLuigi Rizzo #include <sys/malloc.h>
46df8bae1dSRodney W. Grimes #include <sys/domain.h>
47df8bae1dSRodney W. Grimes #include <sys/protosw.h>
48df8bae1dSRodney W. Grimes #include <sys/socket.h>
49df8bae1dSRodney W. Grimes #include <sys/time.h>
50df8bae1dSRodney W. Grimes #include <sys/kernel.h>
51385195c0SMarko Zec #include <sys/lock.h>
52385195c0SMarko Zec #include <sys/rwlock.h>
5357f60867SMark Johnston #include <sys/sdt.h>
541025071fSGarrett Wollman #include <sys/syslog.h>
55b5e8ce9fSBruce Evans #include <sys/sysctl.h>
56df8bae1dSRodney W. Grimes 
57c85540ddSAndrey A. Chernov #include <net/pfil.h>
58df8bae1dSRodney W. Grimes #include <net/if.h>
599494d596SBrooks Davis #include <net/if_types.h>
60d314ad7bSJulian Elischer #include <net/if_var.h>
6182c23ebaSBill Fenner #include <net/if_dl.h>
62df8bae1dSRodney W. Grimes #include <net/route.h>
63748e0b0aSGarrett Wollman #include <net/netisr.h>
644b79449eSBjoern A. Zeeb #include <net/vnet.h>
65df8bae1dSRodney W. Grimes 
66df8bae1dSRodney W. Grimes #include <netinet/in.h>
6757f60867SMark Johnston #include <netinet/in_kdtrace.h>
68df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
69b5e8ce9fSBruce Evans #include <netinet/in_var.h>
70df8bae1dSRodney W. Grimes #include <netinet/ip.h>
71df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
72df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
73eddfbb76SRobert Watson #include <netinet/ip_fw.h>
74df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
75ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
7658938916SGarrett Wollman #include <machine/in_cksum.h>
77a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
78b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
791dfcf0d2SAndre Oppermann #include <netinet/ip_ipsec.h>
80b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
81*b8bc95cdSAdrian Chadd #include <netinet/in_rss.h>
82df8bae1dSRodney W. Grimes 
83f0068c4aSGarrett Wollman #include <sys/socketvar.h>
846ddbf1e2SGary Palmer 
85aed55708SRobert Watson #include <security/mac/mac_framework.h>
86aed55708SRobert Watson 
87d2035ffbSEd Maste #ifdef CTASSERT
88d2035ffbSEd Maste CTASSERT(sizeof(struct ip) == 20);
89d2035ffbSEd Maste #endif
90d2035ffbSEd Maste 
91f89d4c3aSAndre Oppermann struct	rwlock in_ifaddr_lock;
9264aeca7bSRobert Watson RW_SYSINIT(in_ifaddr_lock, &in_ifaddr_lock, "in_ifaddr_lock");
93f0068c4aSGarrett Wollman 
9482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, rsvp_on);
9582cea7e6SBjoern A. Zeeb 
9682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ipforwarding);
97eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
98eddfbb76SRobert Watson     &VNET_NAME(ipforwarding), 0,
998b615593SMarko Zec     "Enable IP forwarding between interfaces");
1000312fbe9SPoul-Henning Kamp 
1013e288e62SDimitry Andric static VNET_DEFINE(int, ipsendredirects) = 1;	/* XXX */
10282cea7e6SBjoern A. Zeeb #define	V_ipsendredirects	VNET(ipsendredirects)
103eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
104eddfbb76SRobert Watson     &VNET_NAME(ipsendredirects), 0,
1058b615593SMarko Zec     "Enable sending IP redirects");
1060312fbe9SPoul-Henning Kamp 
1073e288e62SDimitry Andric static VNET_DEFINE(int, ip_keepfaith);
10882cea7e6SBjoern A. Zeeb #define	V_ip_keepfaith		VNET(ip_keepfaith)
109eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
110eddfbb76SRobert Watson     &VNET_NAME(ip_keepfaith), 0,
1116a800098SYoshinobu Inoue     "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
1126a800098SYoshinobu Inoue 
1133e288e62SDimitry Andric static VNET_DEFINE(int, ip_sendsourcequench);
11482cea7e6SBjoern A. Zeeb #define	V_ip_sendsourcequench	VNET(ip_sendsourcequench)
115eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW,
116eddfbb76SRobert Watson     &VNET_NAME(ip_sendsourcequench), 0,
117df285b3dSMike Silbersack     "Enable the transmission of source quench packets");
118df285b3dSMike Silbersack 
11982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip_do_randomid);
120eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW,
121eddfbb76SRobert Watson     &VNET_NAME(ip_do_randomid), 0,
122eddfbb76SRobert Watson     "Assign random ip_id values");
1231f44b0a1SDavid Malone 
124823db0e9SDon Lewis /*
125823db0e9SDon Lewis  * XXX - Setting ip_checkinterface mostly implements the receive side of
126823db0e9SDon Lewis  * the Strong ES model described in RFC 1122, but since the routing table
127a8f12100SDon Lewis  * and transmit implementation do not implement the Strong ES model,
128823db0e9SDon Lewis  * setting this to 1 results in an odd hybrid.
1293f67c834SDon Lewis  *
130a8f12100SDon Lewis  * XXX - ip_checkinterface currently must be disabled if you use ipnat
131a8f12100SDon Lewis  * to translate the destination address to another local interface.
1323f67c834SDon Lewis  *
1333f67c834SDon Lewis  * XXX - ip_checkinterface must be disabled if you add IP aliases
1343f67c834SDon Lewis  * to the loopback interface instead of the interface where the
1353f67c834SDon Lewis  * packets for those addresses are received.
136823db0e9SDon Lewis  */
1373e288e62SDimitry Andric static VNET_DEFINE(int, ip_checkinterface);
13882cea7e6SBjoern A. Zeeb #define	V_ip_checkinterface	VNET(ip_checkinterface)
139eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
140eddfbb76SRobert Watson     &VNET_NAME(ip_checkinterface), 0,
1418b615593SMarko Zec     "Verify packet arrives on correct interface");
142b3e95d4eSJonathan Lemon 
1430b4b0b0fSJulian Elischer VNET_DEFINE(struct pfil_head, inet_pfil_hook);	/* Packet filter hooks */
144df8bae1dSRodney W. Grimes 
145d4b5cae4SRobert Watson static struct netisr_handler ip_nh = {
146d4b5cae4SRobert Watson 	.nh_name = "ip",
147d4b5cae4SRobert Watson 	.nh_handler = ip_input,
148d4b5cae4SRobert Watson 	.nh_proto = NETISR_IP,
149*b8bc95cdSAdrian Chadd #ifdef	RSS
150*b8bc95cdSAdrian Chadd 	.nh_m2cpuid = rss_soft_m2cpuid,
151*b8bc95cdSAdrian Chadd 	.nh_policy = NETISR_POLICY_CPU,
152*b8bc95cdSAdrian Chadd 	.nh_dispatch = NETISR_DISPATCH_HYBRID,
153*b8bc95cdSAdrian Chadd #else
154d4b5cae4SRobert Watson 	.nh_policy = NETISR_POLICY_FLOW,
155*b8bc95cdSAdrian Chadd #endif
156d4b5cae4SRobert Watson };
157ca925d9cSJonathan Lemon 
158*b8bc95cdSAdrian Chadd #ifdef	RSS
159*b8bc95cdSAdrian Chadd /*
160*b8bc95cdSAdrian Chadd  * Directly dispatched frames are currently assumed
161*b8bc95cdSAdrian Chadd  * to have a flowid already calculated.
162*b8bc95cdSAdrian Chadd  *
163*b8bc95cdSAdrian Chadd  * It should likely have something that assert it
164*b8bc95cdSAdrian Chadd  * actually has valid flow details.
165*b8bc95cdSAdrian Chadd  */
166*b8bc95cdSAdrian Chadd static struct netisr_handler ip_direct_nh = {
167*b8bc95cdSAdrian Chadd 	.nh_name = "ip_direct",
168*b8bc95cdSAdrian Chadd 	.nh_handler = ip_direct_input,
169*b8bc95cdSAdrian Chadd 	.nh_proto = NETISR_IP_DIRECT,
170*b8bc95cdSAdrian Chadd 	.nh_m2cpuid = rss_m2cpuid,
171*b8bc95cdSAdrian Chadd 	.nh_policy = NETISR_POLICY_CPU,
172*b8bc95cdSAdrian Chadd 	.nh_dispatch = NETISR_DISPATCH_HYBRID,
173*b8bc95cdSAdrian Chadd };
174*b8bc95cdSAdrian Chadd #endif
175*b8bc95cdSAdrian Chadd 
176df8bae1dSRodney W. Grimes extern	struct domain inetdomain;
177f0ffb944SJulian Elischer extern	struct protosw inetsw[];
178df8bae1dSRodney W. Grimes u_char	ip_protox[IPPROTO_MAX];
17982cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead);  /* first inet address */
18082cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); /* inet addr hash table  */
18182cea7e6SBjoern A. Zeeb VNET_DEFINE(u_long, in_ifaddrhmask);		/* mask for hash table */
182ca925d9cSJonathan Lemon 
1833e288e62SDimitry Andric static VNET_DEFINE(uma_zone_t, ipq_zone);
1843e288e62SDimitry Andric static VNET_DEFINE(TAILQ_HEAD(ipqhead, ipq), ipq[IPREASS_NHASH]);
185f89d4c3aSAndre Oppermann static struct mtx ipqlock;
1862fad1e93SSam Leffler 
18782cea7e6SBjoern A. Zeeb #define	V_ipq_zone		VNET(ipq_zone)
18882cea7e6SBjoern A. Zeeb #define	V_ipq			VNET(ipq)
18982cea7e6SBjoern A. Zeeb 
1902fad1e93SSam Leffler #define	IPQ_LOCK()	mtx_lock(&ipqlock)
1912fad1e93SSam Leffler #define	IPQ_UNLOCK()	mtx_unlock(&ipqlock)
192888c2a3cSSam Leffler #define	IPQ_LOCK_INIT()	mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
193888c2a3cSSam Leffler #define	IPQ_LOCK_ASSERT()	mtx_assert(&ipqlock, MA_OWNED)
194f23b4c91SGarrett Wollman 
195d248c7d7SRobert Watson static void	maxnipq_update(void);
1964f590175SPaul Saab static void	ipq_zone_change(void *);
1979802380eSBjoern A. Zeeb static void	ip_drain_locked(void);
198d248c7d7SRobert Watson 
1993e288e62SDimitry Andric static VNET_DEFINE(int, maxnipq);  /* Administrative limit on # reass queues. */
2003e288e62SDimitry Andric static VNET_DEFINE(int, nipq);			/* Total # of reass queues */
20182cea7e6SBjoern A. Zeeb #define	V_maxnipq		VNET(maxnipq)
20282cea7e6SBjoern A. Zeeb #define	V_nipq			VNET(nipq)
203eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_RD,
204eddfbb76SRobert Watson     &VNET_NAME(nipq), 0,
2058b615593SMarko Zec     "Current number of IPv4 fragment reassembly queue entries");
206d248c7d7SRobert Watson 
2073e288e62SDimitry Andric static VNET_DEFINE(int, maxfragsperpacket);
20882cea7e6SBjoern A. Zeeb #define	V_maxfragsperpacket	VNET(maxfragsperpacket)
209eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW,
210eddfbb76SRobert Watson     &VNET_NAME(maxfragsperpacket), 0,
211d248c7d7SRobert Watson     "Maximum number of IPv4 fragments allowed per packet");
212d248c7d7SRobert Watson 
2130312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU
2140312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
2153d177f46SBill Fumerola     &ip_mtu, 0, "Default MTU");
2160312fbe9SPoul-Henning Kamp #endif
2170312fbe9SPoul-Henning Kamp 
2181b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
21982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ipstealth);
220eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
221eddfbb76SRobert Watson     &VNET_NAME(ipstealth), 0,
222eddfbb76SRobert Watson     "IP stealth mode, no TTL decrementation on forwarding");
2231b968362SDag-Erling Smørgrav #endif
224eddfbb76SRobert Watson 
2254d77a549SAlfred Perlstein static void	ip_freef(struct ipqhead *, struct ipq *);
2268948e4baSArchie Cobbs 
227315e3e38SRobert Watson /*
2285da0521fSAndrey V. Elsukov  * IP statistics are stored in the "array" of counter(9)s.
2295923c293SGleb Smirnoff  */
2305da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct ipstat, ipstat);
2315da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(ipstat);
2325da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_ip, IPCTL_STATS, stats, struct ipstat, ipstat,
2335da0521fSAndrey V. Elsukov     "IP statistics (struct ipstat, netinet/ip_var.h)");
2345923c293SGleb Smirnoff 
2355923c293SGleb Smirnoff #ifdef VIMAGE
2365da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(ipstat);
2375923c293SGleb Smirnoff #endif /* VIMAGE */
2385923c293SGleb Smirnoff 
2395923c293SGleb Smirnoff /*
240315e3e38SRobert Watson  * Kernel module interface for updating ipstat.  The argument is an index
2415923c293SGleb Smirnoff  * into ipstat treated as an array.
242315e3e38SRobert Watson  */
243315e3e38SRobert Watson void
244315e3e38SRobert Watson kmod_ipstat_inc(int statnum)
245315e3e38SRobert Watson {
246315e3e38SRobert Watson 
2475da0521fSAndrey V. Elsukov 	counter_u64_add(VNET(ipstat)[statnum], 1);
248315e3e38SRobert Watson }
249315e3e38SRobert Watson 
250315e3e38SRobert Watson void
251315e3e38SRobert Watson kmod_ipstat_dec(int statnum)
252315e3e38SRobert Watson {
253315e3e38SRobert Watson 
2545da0521fSAndrey V. Elsukov 	counter_u64_add(VNET(ipstat)[statnum], -1);
255315e3e38SRobert Watson }
256315e3e38SRobert Watson 
257d4b5cae4SRobert Watson static int
258d4b5cae4SRobert Watson sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
259d4b5cae4SRobert Watson {
260d4b5cae4SRobert Watson 	int error, qlimit;
261d4b5cae4SRobert Watson 
262d4b5cae4SRobert Watson 	netisr_getqlimit(&ip_nh, &qlimit);
263d4b5cae4SRobert Watson 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
264d4b5cae4SRobert Watson 	if (error || !req->newptr)
265d4b5cae4SRobert Watson 		return (error);
266d4b5cae4SRobert Watson 	if (qlimit < 1)
267d4b5cae4SRobert Watson 		return (EINVAL);
268d4b5cae4SRobert Watson 	return (netisr_setqlimit(&ip_nh, qlimit));
269d4b5cae4SRobert Watson }
270d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen,
271d4b5cae4SRobert Watson     CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_queue_maxlen, "I",
272d4b5cae4SRobert Watson     "Maximum size of the IP input queue");
273d4b5cae4SRobert Watson 
274d4b5cae4SRobert Watson static int
275d4b5cae4SRobert Watson sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS)
276d4b5cae4SRobert Watson {
277d4b5cae4SRobert Watson 	u_int64_t qdrops_long;
278d4b5cae4SRobert Watson 	int error, qdrops;
279d4b5cae4SRobert Watson 
280d4b5cae4SRobert Watson 	netisr_getqdrops(&ip_nh, &qdrops_long);
281d4b5cae4SRobert Watson 	qdrops = qdrops_long;
282d4b5cae4SRobert Watson 	error = sysctl_handle_int(oidp, &qdrops, 0, req);
283d4b5cae4SRobert Watson 	if (error || !req->newptr)
284d4b5cae4SRobert Watson 		return (error);
285d4b5cae4SRobert Watson 	if (qdrops != 0)
286d4b5cae4SRobert Watson 		return (EINVAL);
287d4b5cae4SRobert Watson 	netisr_clearqdrops(&ip_nh);
288d4b5cae4SRobert Watson 	return (0);
289d4b5cae4SRobert Watson }
290d4b5cae4SRobert Watson 
291d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops,
292d4b5cae4SRobert Watson     CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_queue_drops, "I",
293d4b5cae4SRobert Watson     "Number of packets dropped from the IP input queue");
294d4b5cae4SRobert Watson 
295*b8bc95cdSAdrian Chadd #ifdef	RSS
296*b8bc95cdSAdrian Chadd static int
297*b8bc95cdSAdrian Chadd sysctl_netinet_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
298*b8bc95cdSAdrian Chadd {
299*b8bc95cdSAdrian Chadd 	int error, qlimit;
300*b8bc95cdSAdrian Chadd 
301*b8bc95cdSAdrian Chadd 	netisr_getqlimit(&ip_direct_nh, &qlimit);
302*b8bc95cdSAdrian Chadd 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
303*b8bc95cdSAdrian Chadd 	if (error || !req->newptr)
304*b8bc95cdSAdrian Chadd 		return (error);
305*b8bc95cdSAdrian Chadd 	if (qlimit < 1)
306*b8bc95cdSAdrian Chadd 		return (EINVAL);
307*b8bc95cdSAdrian Chadd 	return (netisr_setqlimit(&ip_direct_nh, qlimit));
308*b8bc95cdSAdrian Chadd }
309*b8bc95cdSAdrian Chadd SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_direct_queue_maxlen,
310*b8bc95cdSAdrian Chadd     CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_direct_queue_maxlen, "I",
311*b8bc95cdSAdrian Chadd     "Maximum size of the IP direct input queue");
312*b8bc95cdSAdrian Chadd 
313*b8bc95cdSAdrian Chadd static int
314*b8bc95cdSAdrian Chadd sysctl_netinet_intr_direct_queue_drops(SYSCTL_HANDLER_ARGS)
315*b8bc95cdSAdrian Chadd {
316*b8bc95cdSAdrian Chadd 	u_int64_t qdrops_long;
317*b8bc95cdSAdrian Chadd 	int error, qdrops;
318*b8bc95cdSAdrian Chadd 
319*b8bc95cdSAdrian Chadd 	netisr_getqdrops(&ip_direct_nh, &qdrops_long);
320*b8bc95cdSAdrian Chadd 	qdrops = qdrops_long;
321*b8bc95cdSAdrian Chadd 	error = sysctl_handle_int(oidp, &qdrops, 0, req);
322*b8bc95cdSAdrian Chadd 	if (error || !req->newptr)
323*b8bc95cdSAdrian Chadd 		return (error);
324*b8bc95cdSAdrian Chadd 	if (qdrops != 0)
325*b8bc95cdSAdrian Chadd 		return (EINVAL);
326*b8bc95cdSAdrian Chadd 	netisr_clearqdrops(&ip_direct_nh);
327*b8bc95cdSAdrian Chadd 	return (0);
328*b8bc95cdSAdrian Chadd }
329*b8bc95cdSAdrian Chadd 
330*b8bc95cdSAdrian Chadd SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_direct_queue_drops,
331*b8bc95cdSAdrian Chadd     CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_direct_queue_drops, "I",
332*b8bc95cdSAdrian Chadd     "Number of packets dropped from the IP direct input queue");
333*b8bc95cdSAdrian Chadd #endif	/* RSS */
334*b8bc95cdSAdrian Chadd 
335df8bae1dSRodney W. Grimes /*
336df8bae1dSRodney W. Grimes  * IP initialization: fill in IP protocol switch table.
337df8bae1dSRodney W. Grimes  * All protocols not implemented in kernel go to raw IP protocol handler.
338df8bae1dSRodney W. Grimes  */
339df8bae1dSRodney W. Grimes void
340f2565d68SRobert Watson ip_init(void)
341df8bae1dSRodney W. Grimes {
342f2565d68SRobert Watson 	struct protosw *pr;
343f2565d68SRobert Watson 	int i;
344df8bae1dSRodney W. Grimes 
345a511354aSRobert Watson 	V_ip_id = time_second & 0xffff;
346a511354aSRobert Watson 
347603724d3SBjoern A. Zeeb 	TAILQ_INIT(&V_in_ifaddrhead);
348603724d3SBjoern A. Zeeb 	V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
3491ed81b73SMarko Zec 
3501ed81b73SMarko Zec 	/* Initialize IP reassembly queue. */
3511ed81b73SMarko Zec 	for (i = 0; i < IPREASS_NHASH; i++)
3521ed81b73SMarko Zec 		TAILQ_INIT(&V_ipq[i]);
3531ed81b73SMarko Zec 	V_maxnipq = nmbclusters / 32;
3541ed81b73SMarko Zec 	V_maxfragsperpacket = 16;
3551ed81b73SMarko Zec 	V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
3561ed81b73SMarko Zec 	    NULL, UMA_ALIGN_PTR, 0);
3571ed81b73SMarko Zec 	maxnipq_update();
3581ed81b73SMarko Zec 
3590b4b0b0fSJulian Elischer 	/* Initialize packet filter hooks. */
3600b4b0b0fSJulian Elischer 	V_inet_pfil_hook.ph_type = PFIL_TYPE_AF;
3610b4b0b0fSJulian Elischer 	V_inet_pfil_hook.ph_af = AF_INET;
3620b4b0b0fSJulian Elischer 	if ((i = pfil_head_register(&V_inet_pfil_hook)) != 0)
3630b4b0b0fSJulian Elischer 		printf("%s: WARNING: unable to register pfil hook, "
3640b4b0b0fSJulian Elischer 			"error %d\n", __func__, i);
3650b4b0b0fSJulian Elischer 
3661ed81b73SMarko Zec 	/* Skip initialization of globals for non-default instances. */
3671ed81b73SMarko Zec 	if (!IS_DEFAULT_VNET(curvnet))
3681ed81b73SMarko Zec 		return;
3691ed81b73SMarko Zec 
370f0ffb944SJulian Elischer 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
37102410549SRobert Watson 	if (pr == NULL)
372db09bef3SAndre Oppermann 		panic("ip_init: PF_INET not found");
373db09bef3SAndre Oppermann 
374db09bef3SAndre Oppermann 	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
375df8bae1dSRodney W. Grimes 	for (i = 0; i < IPPROTO_MAX; i++)
376df8bae1dSRodney W. Grimes 		ip_protox[i] = pr - inetsw;
377db09bef3SAndre Oppermann 	/*
378db09bef3SAndre Oppermann 	 * Cycle through IP protocols and put them into the appropriate place
379db09bef3SAndre Oppermann 	 * in ip_protox[].
380db09bef3SAndre Oppermann 	 */
381f0ffb944SJulian Elischer 	for (pr = inetdomain.dom_protosw;
382f0ffb944SJulian Elischer 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
383df8bae1dSRodney W. Grimes 		if (pr->pr_domain->dom_family == PF_INET &&
384db09bef3SAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
385db09bef3SAndre Oppermann 			/* Be careful to only index valid IP protocols. */
386db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX)
387df8bae1dSRodney W. Grimes 				ip_protox[pr->pr_protocol] = pr - inetsw;
388db09bef3SAndre Oppermann 		}
389194a213eSAndrey A. Chernov 
3904f590175SPaul Saab 	EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
3914f590175SPaul Saab 		NULL, EVENTHANDLER_PRI_ANY);
3925f311da2SMike Silbersack 
393db09bef3SAndre Oppermann 	/* Initialize various other remaining things. */
3941ed81b73SMarko Zec 	IPQ_LOCK_INIT();
395d4b5cae4SRobert Watson 	netisr_register(&ip_nh);
396*b8bc95cdSAdrian Chadd #ifdef	RSS
397*b8bc95cdSAdrian Chadd 	netisr_register(&ip_direct_nh);
398*b8bc95cdSAdrian Chadd #endif
399df8bae1dSRodney W. Grimes }
400df8bae1dSRodney W. Grimes 
4019802380eSBjoern A. Zeeb #ifdef VIMAGE
4029802380eSBjoern A. Zeeb void
4039802380eSBjoern A. Zeeb ip_destroy(void)
4049802380eSBjoern A. Zeeb {
4054d3dfd45SMikolaj Golub 	int i;
4064d3dfd45SMikolaj Golub 
4074d3dfd45SMikolaj Golub 	if ((i = pfil_head_unregister(&V_inet_pfil_hook)) != 0)
4084d3dfd45SMikolaj Golub 		printf("%s: WARNING: unable to unregister pfil hook, "
4094d3dfd45SMikolaj Golub 		    "error %d\n", __func__, i);
4109802380eSBjoern A. Zeeb 
4119802380eSBjoern A. Zeeb 	/* Cleanup in_ifaddr hash table; should be empty. */
4129802380eSBjoern A. Zeeb 	hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask);
4139802380eSBjoern A. Zeeb 
4149802380eSBjoern A. Zeeb 	IPQ_LOCK();
4159802380eSBjoern A. Zeeb 	ip_drain_locked();
4169802380eSBjoern A. Zeeb 	IPQ_UNLOCK();
4179802380eSBjoern A. Zeeb 
4189802380eSBjoern A. Zeeb 	uma_zdestroy(V_ipq_zone);
4199802380eSBjoern A. Zeeb }
4209802380eSBjoern A. Zeeb #endif
4219802380eSBjoern A. Zeeb 
422*b8bc95cdSAdrian Chadd #ifdef	RSS
423*b8bc95cdSAdrian Chadd /*
424*b8bc95cdSAdrian Chadd  * IP direct input routine.
425*b8bc95cdSAdrian Chadd  *
426*b8bc95cdSAdrian Chadd  * This is called when reinjecting completed fragments where
427*b8bc95cdSAdrian Chadd  * all of the previous checking and book-keeping has been done.
428*b8bc95cdSAdrian Chadd  */
429*b8bc95cdSAdrian Chadd void
430*b8bc95cdSAdrian Chadd ip_direct_input(struct mbuf *m)
431*b8bc95cdSAdrian Chadd {
432*b8bc95cdSAdrian Chadd 	struct ip *ip;
433*b8bc95cdSAdrian Chadd 	int hlen;
434*b8bc95cdSAdrian Chadd 
435*b8bc95cdSAdrian Chadd 	ip = mtod(m, struct ip *);
436*b8bc95cdSAdrian Chadd 	hlen = ip->ip_hl << 2;
437*b8bc95cdSAdrian Chadd 
438*b8bc95cdSAdrian Chadd 	IPSTAT_INC(ips_delivered);
439*b8bc95cdSAdrian Chadd 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(&m, &hlen, ip->ip_p);
440*b8bc95cdSAdrian Chadd 	return;
441*b8bc95cdSAdrian Chadd }
442*b8bc95cdSAdrian Chadd #endif
443*b8bc95cdSAdrian Chadd 
4444d2e3692SLuigi Rizzo /*
445df8bae1dSRodney W. Grimes  * Ip input routine.  Checksum and byte swap header.  If fragmented
446df8bae1dSRodney W. Grimes  * try to reassemble.  Process options.  Pass to next level.
447df8bae1dSRodney W. Grimes  */
448c67b1d17SGarrett Wollman void
449c67b1d17SGarrett Wollman ip_input(struct mbuf *m)
450df8bae1dSRodney W. Grimes {
4519188b4a1SAndre Oppermann 	struct ip *ip = NULL;
4525da9f8faSJosef Karthauser 	struct in_ifaddr *ia = NULL;
453ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
4540aade26eSRobert Watson 	struct ifnet *ifp;
4559b932e9eSAndre Oppermann 	int    checkif, hlen = 0;
45621d172a3SGleb Smirnoff 	uint16_t sum, ip_len;
45702c1c707SAndre Oppermann 	int dchg = 0;				/* dest changed after fw */
458f51f805fSSam Leffler 	struct in_addr odst;			/* original dst address */
459b715f178SLuigi Rizzo 
460fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
461db40007dSAndrew R. Reiter 
462ac9d7e26SMax Laier 	if (m->m_flags & M_FASTFWD_OURS) {
46376ff6dcfSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
46476ff6dcfSAndre Oppermann 		/* Set up some basics that will be used later. */
4652b25acc1SLuigi Rizzo 		ip = mtod(m, struct ip *);
46653be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
4678f134647SGleb Smirnoff 		ip_len = ntohs(ip->ip_len);
4689b932e9eSAndre Oppermann 		goto ours;
4692b25acc1SLuigi Rizzo 	}
4702b25acc1SLuigi Rizzo 
47186425c62SRobert Watson 	IPSTAT_INC(ips_total);
47258938916SGarrett Wollman 
47358938916SGarrett Wollman 	if (m->m_pkthdr.len < sizeof(struct ip))
47458938916SGarrett Wollman 		goto tooshort;
47558938916SGarrett Wollman 
476df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct ip) &&
4770b17fba7SAndre Oppermann 	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
47886425c62SRobert Watson 		IPSTAT_INC(ips_toosmall);
479c67b1d17SGarrett Wollman 		return;
480df8bae1dSRodney W. Grimes 	}
481df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
48258938916SGarrett Wollman 
48353be11f6SPoul-Henning Kamp 	if (ip->ip_v != IPVERSION) {
48486425c62SRobert Watson 		IPSTAT_INC(ips_badvers);
485df8bae1dSRodney W. Grimes 		goto bad;
486df8bae1dSRodney W. Grimes 	}
48758938916SGarrett Wollman 
48853be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
489df8bae1dSRodney W. Grimes 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
49086425c62SRobert Watson 		IPSTAT_INC(ips_badhlen);
491df8bae1dSRodney W. Grimes 		goto bad;
492df8bae1dSRodney W. Grimes 	}
493df8bae1dSRodney W. Grimes 	if (hlen > m->m_len) {
4940b17fba7SAndre Oppermann 		if ((m = m_pullup(m, hlen)) == NULL) {
49586425c62SRobert Watson 			IPSTAT_INC(ips_badhlen);
496c67b1d17SGarrett Wollman 			return;
497df8bae1dSRodney W. Grimes 		}
498df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
499df8bae1dSRodney W. Grimes 	}
50033841545SHajimu UMEMOTO 
50157f60867SMark Johnston 	IP_PROBE(receive, NULL, NULL, ip, m->m_pkthdr.rcvif, ip, NULL);
50257f60867SMark Johnston 
50333841545SHajimu UMEMOTO 	/* 127/8 must not appear on wire - RFC1122 */
5040aade26eSRobert Watson 	ifp = m->m_pkthdr.rcvif;
50533841545SHajimu UMEMOTO 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
50633841545SHajimu UMEMOTO 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
5070aade26eSRobert Watson 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
50886425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
50933841545SHajimu UMEMOTO 			goto bad;
51033841545SHajimu UMEMOTO 		}
51133841545SHajimu UMEMOTO 	}
51233841545SHajimu UMEMOTO 
513db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
514db4f9cc7SJonathan Lemon 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
515db4f9cc7SJonathan Lemon 	} else {
51658938916SGarrett Wollman 		if (hlen == sizeof(struct ip)) {
51747c861ecSBrian Somers 			sum = in_cksum_hdr(ip);
51858938916SGarrett Wollman 		} else {
51947c861ecSBrian Somers 			sum = in_cksum(m, hlen);
52058938916SGarrett Wollman 		}
521db4f9cc7SJonathan Lemon 	}
52247c861ecSBrian Somers 	if (sum) {
52386425c62SRobert Watson 		IPSTAT_INC(ips_badsum);
524df8bae1dSRodney W. Grimes 		goto bad;
525df8bae1dSRodney W. Grimes 	}
526df8bae1dSRodney W. Grimes 
52702b199f1SMax Laier #ifdef ALTQ
52802b199f1SMax Laier 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
52902b199f1SMax Laier 		/* packet is dropped by traffic conditioner */
53002b199f1SMax Laier 		return;
53102b199f1SMax Laier #endif
53202b199f1SMax Laier 
53321d172a3SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
53421d172a3SGleb Smirnoff 	if (ip_len < hlen) {
53586425c62SRobert Watson 		IPSTAT_INC(ips_badlen);
536df8bae1dSRodney W. Grimes 		goto bad;
537df8bae1dSRodney W. Grimes 	}
538df8bae1dSRodney W. Grimes 
539df8bae1dSRodney W. Grimes 	/*
540df8bae1dSRodney W. Grimes 	 * Check that the amount of data in the buffers
541df8bae1dSRodney W. Grimes 	 * is as at least much as the IP header would have us expect.
542df8bae1dSRodney W. Grimes 	 * Trim mbufs if longer than we expect.
543df8bae1dSRodney W. Grimes 	 * Drop packet if shorter than we expect.
544df8bae1dSRodney W. Grimes 	 */
54521d172a3SGleb Smirnoff 	if (m->m_pkthdr.len < ip_len) {
54658938916SGarrett Wollman tooshort:
54786425c62SRobert Watson 		IPSTAT_INC(ips_tooshort);
548df8bae1dSRodney W. Grimes 		goto bad;
549df8bae1dSRodney W. Grimes 	}
55021d172a3SGleb Smirnoff 	if (m->m_pkthdr.len > ip_len) {
551df8bae1dSRodney W. Grimes 		if (m->m_len == m->m_pkthdr.len) {
55221d172a3SGleb Smirnoff 			m->m_len = ip_len;
55321d172a3SGleb Smirnoff 			m->m_pkthdr.len = ip_len;
554df8bae1dSRodney W. Grimes 		} else
55521d172a3SGleb Smirnoff 			m_adj(m, ip_len - m->m_pkthdr.len);
556df8bae1dSRodney W. Grimes 	}
557*b8bc95cdSAdrian Chadd 
558b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
55914dd6717SSam Leffler 	/*
560ffe8cd7bSBjoern A. Zeeb 	 * Bypass packet filtering for packets previously handled by IPsec.
56114dd6717SSam Leffler 	 */
562cc977adcSBjoern A. Zeeb 	if (ip_ipsec_filtertunnel(m))
563c21fd232SAndre Oppermann 		goto passin;
564b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
5653f67c834SDon Lewis 
566c4ac87eaSDarren Reed 	/*
567134ea224SSam Leffler 	 * Run through list of hooks for input packets.
568f51f805fSSam Leffler 	 *
569f51f805fSSam Leffler 	 * NB: Beware of the destination address changing (e.g.
570f51f805fSSam Leffler 	 *     by NAT rewriting).  When this happens, tell
571f51f805fSSam Leffler 	 *     ip_forward to do the right thing.
572c4ac87eaSDarren Reed 	 */
573c21fd232SAndre Oppermann 
574c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
5750b4b0b0fSJulian Elischer 	if (!PFIL_HOOKED(&V_inet_pfil_hook))
576c21fd232SAndre Oppermann 		goto passin;
577c21fd232SAndre Oppermann 
578f51f805fSSam Leffler 	odst = ip->ip_dst;
5790b4b0b0fSJulian Elischer 	if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0)
580beec8214SDarren Reed 		return;
581134ea224SSam Leffler 	if (m == NULL)			/* consumed by filter */
582c4ac87eaSDarren Reed 		return;
5839b932e9eSAndre Oppermann 
584c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
58502c1c707SAndre Oppermann 	dchg = (odst.s_addr != ip->ip_dst.s_addr);
5860aade26eSRobert Watson 	ifp = m->m_pkthdr.rcvif;
5879b932e9eSAndre Oppermann 
5889b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
5899b932e9eSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
5909b932e9eSAndre Oppermann 		goto ours;
5919b932e9eSAndre Oppermann 	}
592ffdbf9daSAndrey V. Elsukov 	if (m->m_flags & M_IP_NEXTHOP) {
593ffdbf9daSAndrey V. Elsukov 		dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL);
594ffdbf9daSAndrey V. Elsukov 		if (dchg != 0) {
595099dd043SAndre Oppermann 			/*
596ffdbf9daSAndrey V. Elsukov 			 * Directly ship the packet on.  This allows
597ffdbf9daSAndrey V. Elsukov 			 * forwarding packets originally destined to us
598ffdbf9daSAndrey V. Elsukov 			 * to some other directly connected host.
599099dd043SAndre Oppermann 			 */
600ffdbf9daSAndrey V. Elsukov 			ip_forward(m, 1);
601099dd043SAndre Oppermann 			return;
602099dd043SAndre Oppermann 		}
603ffdbf9daSAndrey V. Elsukov 	}
604c21fd232SAndre Oppermann passin:
60521d172a3SGleb Smirnoff 
60621d172a3SGleb Smirnoff 	/*
607df8bae1dSRodney W. Grimes 	 * Process options and, if not destined for us,
608df8bae1dSRodney W. Grimes 	 * ship it on.  ip_dooptions returns 1 when an
609df8bae1dSRodney W. Grimes 	 * error was detected (causing an icmp message
610df8bae1dSRodney W. Grimes 	 * to be sent and the original packet to be freed).
611df8bae1dSRodney W. Grimes 	 */
6129b932e9eSAndre Oppermann 	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
613c67b1d17SGarrett Wollman 		return;
614df8bae1dSRodney W. Grimes 
615f0068c4aSGarrett Wollman         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
616f0068c4aSGarrett Wollman          * matter if it is destined to another node, or whether it is
617f0068c4aSGarrett Wollman          * a multicast one, RSVP wants it! and prevents it from being forwarded
618f0068c4aSGarrett Wollman          * anywhere else. Also checks if the rsvp daemon is running before
619f0068c4aSGarrett Wollman 	 * grabbing the packet.
620f0068c4aSGarrett Wollman          */
621603724d3SBjoern A. Zeeb 	if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP)
622f0068c4aSGarrett Wollman 		goto ours;
623f0068c4aSGarrett Wollman 
624df8bae1dSRodney W. Grimes 	/*
625df8bae1dSRodney W. Grimes 	 * Check our list of addresses, to see if the packet is for us.
626cc766e04SGarrett Wollman 	 * If we don't have any addresses, assume any unicast packet
627cc766e04SGarrett Wollman 	 * we receive might be for us (and let the upper layers deal
628cc766e04SGarrett Wollman 	 * with it).
629df8bae1dSRodney W. Grimes 	 */
630603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
631cc766e04SGarrett Wollman 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
632cc766e04SGarrett Wollman 		goto ours;
633cc766e04SGarrett Wollman 
6347538a9a0SJonathan Lemon 	/*
635823db0e9SDon Lewis 	 * Enable a consistency check between the destination address
636823db0e9SDon Lewis 	 * and the arrival interface for a unicast packet (the RFC 1122
637823db0e9SDon Lewis 	 * strong ES model) if IP forwarding is disabled and the packet
638e15ae1b2SDon Lewis 	 * is not locally generated and the packet is not subject to
639e15ae1b2SDon Lewis 	 * 'ipfw fwd'.
6403f67c834SDon Lewis 	 *
6413f67c834SDon Lewis 	 * XXX - Checking also should be disabled if the destination
6423f67c834SDon Lewis 	 * address is ipnat'ed to a different interface.
6433f67c834SDon Lewis 	 *
644a8f12100SDon Lewis 	 * XXX - Checking is incompatible with IP aliases added
6453f67c834SDon Lewis 	 * to the loopback interface instead of the interface where
6463f67c834SDon Lewis 	 * the packets are received.
647a9771948SGleb Smirnoff 	 *
648a9771948SGleb Smirnoff 	 * XXX - This is the case for carp vhost IPs as well so we
649a9771948SGleb Smirnoff 	 * insert a workaround. If the packet got here, we already
650a9771948SGleb Smirnoff 	 * checked with carp_iamatch() and carp_forus().
651823db0e9SDon Lewis 	 */
652603724d3SBjoern A. Zeeb 	checkif = V_ip_checkinterface && (V_ipforwarding == 0) &&
6530aade26eSRobert Watson 	    ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) &&
65454bfbd51SWill Andrews 	    ifp->if_carp == NULL && (dchg == 0);
655823db0e9SDon Lewis 
656ca925d9cSJonathan Lemon 	/*
657ca925d9cSJonathan Lemon 	 * Check for exact addresses in the hash bucket.
658ca925d9cSJonathan Lemon 	 */
6592d9cfabaSRobert Watson 	/* IN_IFADDR_RLOCK(); */
6609b932e9eSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
661f9e354dfSJulian Elischer 		/*
662823db0e9SDon Lewis 		 * If the address matches, verify that the packet
663823db0e9SDon Lewis 		 * arrived via the correct interface if checking is
664823db0e9SDon Lewis 		 * enabled.
665f9e354dfSJulian Elischer 		 */
6669b932e9eSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
6678c0fec80SRobert Watson 		    (!checkif || ia->ia_ifp == ifp)) {
6687caf4ab7SGleb Smirnoff 			counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
6697caf4ab7SGleb Smirnoff 			counter_u64_add(ia->ia_ifa.ifa_ibytes,
6707caf4ab7SGleb Smirnoff 			    m->m_pkthdr.len);
6712d9cfabaSRobert Watson 			/* IN_IFADDR_RUNLOCK(); */
672ed1ff184SJulian Elischer 			goto ours;
673ca925d9cSJonathan Lemon 		}
6748c0fec80SRobert Watson 	}
6752d9cfabaSRobert Watson 	/* IN_IFADDR_RUNLOCK(); */
6762d9cfabaSRobert Watson 
677823db0e9SDon Lewis 	/*
678ca925d9cSJonathan Lemon 	 * Check for broadcast addresses.
679ca925d9cSJonathan Lemon 	 *
680ca925d9cSJonathan Lemon 	 * Only accept broadcast packets that arrive via the matching
681ca925d9cSJonathan Lemon 	 * interface.  Reception of forwarded directed broadcasts would
682ca925d9cSJonathan Lemon 	 * be handled via ip_forward() and ether_output() with the loopback
683ca925d9cSJonathan Lemon 	 * into the stack for SIMPLEX interfaces handled by ether_output().
684823db0e9SDon Lewis 	 */
6850aade26eSRobert Watson 	if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
686137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
6870aade26eSRobert Watson 	        TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
688ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
689ca925d9cSJonathan Lemon 				continue;
690ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
691df8bae1dSRodney W. Grimes 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
6920aade26eSRobert Watson 			    ip->ip_dst.s_addr) {
6937caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
6947caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_ibytes,
6957caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len);
696137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
697df8bae1dSRodney W. Grimes 				goto ours;
6980aade26eSRobert Watson 			}
6990ac40133SBrian Somers #ifdef BOOTP_COMPAT
7000aade26eSRobert Watson 			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) {
7017caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
7027caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_ibytes,
7037caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len);
704137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
705ca925d9cSJonathan Lemon 				goto ours;
7060aade26eSRobert Watson 			}
7070ac40133SBrian Somers #endif
708df8bae1dSRodney W. Grimes 		}
709137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
71019e5b0a7SRobert Watson 		ia = NULL;
711df8bae1dSRodney W. Grimes 	}
712f8429ca2SBruce M Simpson 	/* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
713f8429ca2SBruce M Simpson 	if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
71486425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
715f8429ca2SBruce M Simpson 		m_freem(m);
716f8429ca2SBruce M Simpson 		return;
717f8429ca2SBruce M Simpson 	}
718df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
719603724d3SBjoern A. Zeeb 		if (V_ip_mrouter) {
720df8bae1dSRodney W. Grimes 			/*
721df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, all
722df8bae1dSRodney W. Grimes 			 * incoming multicast packets are passed to the
723df8bae1dSRodney W. Grimes 			 * kernel-level multicast forwarding function.
724df8bae1dSRodney W. Grimes 			 * The packet is returned (relatively) intact; if
725df8bae1dSRodney W. Grimes 			 * ip_mforward() returns a non-zero value, the packet
726df8bae1dSRodney W. Grimes 			 * must be discarded, else it may be accepted below.
727df8bae1dSRodney W. Grimes 			 */
7280aade26eSRobert Watson 			if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
72986425c62SRobert Watson 				IPSTAT_INC(ips_cantforward);
730df8bae1dSRodney W. Grimes 				m_freem(m);
731c67b1d17SGarrett Wollman 				return;
732df8bae1dSRodney W. Grimes 			}
733df8bae1dSRodney W. Grimes 
734df8bae1dSRodney W. Grimes 			/*
73511612afaSDima Dorfman 			 * The process-level routing daemon needs to receive
736df8bae1dSRodney W. Grimes 			 * all multicast IGMP packets, whether or not this
737df8bae1dSRodney W. Grimes 			 * host belongs to their destination groups.
738df8bae1dSRodney W. Grimes 			 */
739df8bae1dSRodney W. Grimes 			if (ip->ip_p == IPPROTO_IGMP)
740df8bae1dSRodney W. Grimes 				goto ours;
74186425c62SRobert Watson 			IPSTAT_INC(ips_forward);
742df8bae1dSRodney W. Grimes 		}
743df8bae1dSRodney W. Grimes 		/*
744d10910e6SBruce M Simpson 		 * Assume the packet is for us, to avoid prematurely taking
745d10910e6SBruce M Simpson 		 * a lock on the in_multi hash. Protocols must perform
746d10910e6SBruce M Simpson 		 * their own filtering and update statistics accordingly.
747df8bae1dSRodney W. Grimes 		 */
748df8bae1dSRodney W. Grimes 		goto ours;
749df8bae1dSRodney W. Grimes 	}
750df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
751df8bae1dSRodney W. Grimes 		goto ours;
752df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == INADDR_ANY)
753df8bae1dSRodney W. Grimes 		goto ours;
754df8bae1dSRodney W. Grimes 
7556a800098SYoshinobu Inoue 	/*
7566a800098SYoshinobu Inoue 	 * FAITH(Firewall Aided Internet Translator)
7576a800098SYoshinobu Inoue 	 */
7580aade26eSRobert Watson 	if (ifp && ifp->if_type == IFT_FAITH) {
759603724d3SBjoern A. Zeeb 		if (V_ip_keepfaith) {
7606a800098SYoshinobu Inoue 			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
7616a800098SYoshinobu Inoue 				goto ours;
7626a800098SYoshinobu Inoue 		}
7636a800098SYoshinobu Inoue 		m_freem(m);
7646a800098SYoshinobu Inoue 		return;
7656a800098SYoshinobu Inoue 	}
7669494d596SBrooks Davis 
767df8bae1dSRodney W. Grimes 	/*
768df8bae1dSRodney W. Grimes 	 * Not for us; forward if possible and desirable.
769df8bae1dSRodney W. Grimes 	 */
770603724d3SBjoern A. Zeeb 	if (V_ipforwarding == 0) {
77186425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
772df8bae1dSRodney W. Grimes 		m_freem(m);
773546f251bSChris D. Faulhaber 	} else {
774b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
7751dfcf0d2SAndre Oppermann 		if (ip_ipsec_fwd(m))
776546f251bSChris D. Faulhaber 			goto bad;
777b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
7789b932e9eSAndre Oppermann 		ip_forward(m, dchg);
779546f251bSChris D. Faulhaber 	}
780c67b1d17SGarrett Wollman 	return;
781df8bae1dSRodney W. Grimes 
782df8bae1dSRodney W. Grimes ours:
783d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH
784d0ebc0d2SYaroslav Tykhiy 	/*
785d0ebc0d2SYaroslav Tykhiy 	 * IPSTEALTH: Process non-routing options only
786d0ebc0d2SYaroslav Tykhiy 	 * if the packet is destined for us.
787d0ebc0d2SYaroslav Tykhiy 	 */
7887caf4ab7SGleb Smirnoff 	if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1))
789d0ebc0d2SYaroslav Tykhiy 		return;
790d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */
791d0ebc0d2SYaroslav Tykhiy 
79263f8d699SJordan K. Hubbard 	/*
793b6ea1aa5SRuslan Ermilov 	 * Attempt reassembly; if it succeeds, proceed.
794ac9d7e26SMax Laier 	 * ip_reass() will return a different mbuf.
795df8bae1dSRodney W. Grimes 	 */
7968f134647SGleb Smirnoff 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
797aa69c612SGleb Smirnoff 		/* XXXGL: shouldn't we save & set m_flags? */
798f0cada84SAndre Oppermann 		m = ip_reass(m);
799f0cada84SAndre Oppermann 		if (m == NULL)
800c67b1d17SGarrett Wollman 			return;
8016a800098SYoshinobu Inoue 		ip = mtod(m, struct ip *);
8027e2df452SRuslan Ermilov 		/* Get the header length of the reassembled packet */
80353be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
804f0cada84SAndre Oppermann 	}
805f0cada84SAndre Oppermann 
806b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
80733841545SHajimu UMEMOTO 	/*
80833841545SHajimu UMEMOTO 	 * enforce IPsec policy checking if we are seeing last header.
80933841545SHajimu UMEMOTO 	 * note that we do not visit this with protocols with pcb layer
81033841545SHajimu UMEMOTO 	 * code - like udp/tcp/raw ip.
81133841545SHajimu UMEMOTO 	 */
8121dfcf0d2SAndre Oppermann 	if (ip_ipsec_input(m))
81333841545SHajimu UMEMOTO 		goto bad;
814b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
81533841545SHajimu UMEMOTO 
816df8bae1dSRodney W. Grimes 	/*
817df8bae1dSRodney W. Grimes 	 * Switch out to protocol's input routine.
818df8bae1dSRodney W. Grimes 	 */
81986425c62SRobert Watson 	IPSTAT_INC(ips_delivered);
8209b932e9eSAndre Oppermann 
8218f5a8818SKevin Lo 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(&m, &hlen, ip->ip_p);
822c67b1d17SGarrett Wollman 	return;
823df8bae1dSRodney W. Grimes bad:
824df8bae1dSRodney W. Grimes 	m_freem(m);
825c67b1d17SGarrett Wollman }
826c67b1d17SGarrett Wollman 
827c67b1d17SGarrett Wollman /*
828d248c7d7SRobert Watson  * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
829d248c7d7SRobert Watson  * max has slightly different semantics than the sysctl, for historical
830d248c7d7SRobert Watson  * reasons.
831d248c7d7SRobert Watson  */
832d248c7d7SRobert Watson static void
833d248c7d7SRobert Watson maxnipq_update(void)
834d248c7d7SRobert Watson {
835d248c7d7SRobert Watson 
836d248c7d7SRobert Watson 	/*
837d248c7d7SRobert Watson 	 * -1 for unlimited allocation.
838d248c7d7SRobert Watson 	 */
839603724d3SBjoern A. Zeeb 	if (V_maxnipq < 0)
840603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 0);
841d248c7d7SRobert Watson 	/*
842d248c7d7SRobert Watson 	 * Positive number for specific bound.
843d248c7d7SRobert Watson 	 */
844603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0)
845603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, V_maxnipq);
846d248c7d7SRobert Watson 	/*
847d248c7d7SRobert Watson 	 * Zero specifies no further fragment queue allocation -- set the
848d248c7d7SRobert Watson 	 * bound very low, but rely on implementation elsewhere to actually
849d248c7d7SRobert Watson 	 * prevent allocation and reclaim current queues.
850d248c7d7SRobert Watson 	 */
851603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0)
852603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 1);
853d248c7d7SRobert Watson }
854d248c7d7SRobert Watson 
8554f590175SPaul Saab static void
8564f590175SPaul Saab ipq_zone_change(void *tag)
8574f590175SPaul Saab {
8584f590175SPaul Saab 
859603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) {
860603724d3SBjoern A. Zeeb 		V_maxnipq = nmbclusters / 32;
8614f590175SPaul Saab 		maxnipq_update();
8624f590175SPaul Saab 	}
8634f590175SPaul Saab }
8644f590175SPaul Saab 
865d248c7d7SRobert Watson static int
866d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
867d248c7d7SRobert Watson {
868d248c7d7SRobert Watson 	int error, i;
869d248c7d7SRobert Watson 
870603724d3SBjoern A. Zeeb 	i = V_maxnipq;
871d248c7d7SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
872d248c7d7SRobert Watson 	if (error || !req->newptr)
873d248c7d7SRobert Watson 		return (error);
874d248c7d7SRobert Watson 
875d248c7d7SRobert Watson 	/*
876d248c7d7SRobert Watson 	 * XXXRW: Might be a good idea to sanity check the argument and place
877d248c7d7SRobert Watson 	 * an extreme upper bound.
878d248c7d7SRobert Watson 	 */
879d248c7d7SRobert Watson 	if (i < -1)
880d248c7d7SRobert Watson 		return (EINVAL);
881603724d3SBjoern A. Zeeb 	V_maxnipq = i;
882d248c7d7SRobert Watson 	maxnipq_update();
883d248c7d7SRobert Watson 	return (0);
884d248c7d7SRobert Watson }
885d248c7d7SRobert Watson 
886d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
887d248c7d7SRobert Watson     NULL, 0, sysctl_maxnipq, "I",
888d248c7d7SRobert Watson     "Maximum number of IPv4 fragment reassembly queue entries");
889d248c7d7SRobert Watson 
890aa69c612SGleb Smirnoff #define	M_IP_FRAG	M_PROTO9
891aa69c612SGleb Smirnoff 
892d248c7d7SRobert Watson /*
8938948e4baSArchie Cobbs  * Take incoming datagram fragment and try to reassemble it into
894f0cada84SAndre Oppermann  * whole datagram.  If the argument is the first fragment or one
895f0cada84SAndre Oppermann  * in between the function will return NULL and store the mbuf
896f0cada84SAndre Oppermann  * in the fragment chain.  If the argument is the last fragment
897f0cada84SAndre Oppermann  * the packet will be reassembled and the pointer to the new
898f0cada84SAndre Oppermann  * mbuf returned for further processing.  Only m_tags attached
899f0cada84SAndre Oppermann  * to the first packet/fragment are preserved.
900f0cada84SAndre Oppermann  * The IP header is *NOT* adjusted out of iplen.
901df8bae1dSRodney W. Grimes  */
902f0cada84SAndre Oppermann struct mbuf *
903f0cada84SAndre Oppermann ip_reass(struct mbuf *m)
904df8bae1dSRodney W. Grimes {
905f0cada84SAndre Oppermann 	struct ip *ip;
906f0cada84SAndre Oppermann 	struct mbuf *p, *q, *nq, *t;
907f0cada84SAndre Oppermann 	struct ipq *fp = NULL;
908f0cada84SAndre Oppermann 	struct ipqhead *head;
909f0cada84SAndre Oppermann 	int i, hlen, next;
91059dfcba4SHajimu UMEMOTO 	u_int8_t ecn, ecn0;
911f0cada84SAndre Oppermann 	u_short hash;
912*b8bc95cdSAdrian Chadd #ifdef	RSS
913*b8bc95cdSAdrian Chadd 	uint32_t rss_hash, rss_type;
914*b8bc95cdSAdrian Chadd #endif
915df8bae1dSRodney W. Grimes 
916800af1fbSMaxim Konovalov 	/* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
917603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0 || V_maxfragsperpacket == 0) {
91886425c62SRobert Watson 		IPSTAT_INC(ips_fragments);
91986425c62SRobert Watson 		IPSTAT_INC(ips_fragdropped);
9209d804f81SAndre Oppermann 		m_freem(m);
9219d804f81SAndre Oppermann 		return (NULL);
922f0cada84SAndre Oppermann 	}
9232fad1e93SSam Leffler 
924f0cada84SAndre Oppermann 	ip = mtod(m, struct ip *);
925f0cada84SAndre Oppermann 	hlen = ip->ip_hl << 2;
926f0cada84SAndre Oppermann 
927f0cada84SAndre Oppermann 	hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
928603724d3SBjoern A. Zeeb 	head = &V_ipq[hash];
929f0cada84SAndre Oppermann 	IPQ_LOCK();
930f0cada84SAndre Oppermann 
931f0cada84SAndre Oppermann 	/*
932f0cada84SAndre Oppermann 	 * Look for queue of fragments
933f0cada84SAndre Oppermann 	 * of this datagram.
934f0cada84SAndre Oppermann 	 */
935f0cada84SAndre Oppermann 	TAILQ_FOREACH(fp, head, ipq_list)
936f0cada84SAndre Oppermann 		if (ip->ip_id == fp->ipq_id &&
937f0cada84SAndre Oppermann 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
938f0cada84SAndre Oppermann 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
939f0cada84SAndre Oppermann #ifdef MAC
94030d239bcSRobert Watson 		    mac_ipq_match(m, fp) &&
941f0cada84SAndre Oppermann #endif
942f0cada84SAndre Oppermann 		    ip->ip_p == fp->ipq_p)
943f0cada84SAndre Oppermann 			goto found;
944f0cada84SAndre Oppermann 
945f0cada84SAndre Oppermann 	fp = NULL;
946f0cada84SAndre Oppermann 
947f0cada84SAndre Oppermann 	/*
948d248c7d7SRobert Watson 	 * Attempt to trim the number of allocated fragment queues if it
949d248c7d7SRobert Watson 	 * exceeds the administrative limit.
950f0cada84SAndre Oppermann 	 */
951603724d3SBjoern A. Zeeb 	if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) {
952f0cada84SAndre Oppermann 		/*
953f0cada84SAndre Oppermann 		 * drop something from the tail of the current queue
954f0cada84SAndre Oppermann 		 * before proceeding further
955f0cada84SAndre Oppermann 		 */
956f0cada84SAndre Oppermann 		struct ipq *q = TAILQ_LAST(head, ipqhead);
957f0cada84SAndre Oppermann 		if (q == NULL) {   /* gak */
958f0cada84SAndre Oppermann 			for (i = 0; i < IPREASS_NHASH; i++) {
959603724d3SBjoern A. Zeeb 				struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead);
960f0cada84SAndre Oppermann 				if (r) {
96186425c62SRobert Watson 					IPSTAT_ADD(ips_fragtimeout,
96286425c62SRobert Watson 					    r->ipq_nfrags);
963603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], r);
964f0cada84SAndre Oppermann 					break;
965f0cada84SAndre Oppermann 				}
966f0cada84SAndre Oppermann 			}
967f0cada84SAndre Oppermann 		} else {
96886425c62SRobert Watson 			IPSTAT_ADD(ips_fragtimeout, q->ipq_nfrags);
969f0cada84SAndre Oppermann 			ip_freef(head, q);
970f0cada84SAndre Oppermann 		}
971f0cada84SAndre Oppermann 	}
972f0cada84SAndre Oppermann 
973f0cada84SAndre Oppermann found:
974f0cada84SAndre Oppermann 	/*
975f0cada84SAndre Oppermann 	 * Adjust ip_len to not reflect header,
976f0cada84SAndre Oppermann 	 * convert offset of this to bytes.
977f0cada84SAndre Oppermann 	 */
9788f134647SGleb Smirnoff 	ip->ip_len = htons(ntohs(ip->ip_len) - hlen);
9798f134647SGleb Smirnoff 	if (ip->ip_off & htons(IP_MF)) {
980f0cada84SAndre Oppermann 		/*
981f0cada84SAndre Oppermann 		 * Make sure that fragments have a data length
982f0cada84SAndre Oppermann 		 * that's a non-zero multiple of 8 bytes.
983f0cada84SAndre Oppermann 		 */
984b6fcf6f9SGleb Smirnoff 		if (ip->ip_len == htons(0) || (ntohs(ip->ip_len) & 0x7) != 0) {
98586425c62SRobert Watson 			IPSTAT_INC(ips_toosmall); /* XXX */
986f0cada84SAndre Oppermann 			goto dropfrag;
987f0cada84SAndre Oppermann 		}
988b09dc7e3SAndre Oppermann 		m->m_flags |= M_IP_FRAG;
989f0cada84SAndre Oppermann 	} else
990b09dc7e3SAndre Oppermann 		m->m_flags &= ~M_IP_FRAG;
9918f134647SGleb Smirnoff 	ip->ip_off = htons(ntohs(ip->ip_off) << 3);
992f0cada84SAndre Oppermann 
993f0cada84SAndre Oppermann 	/*
994f0cada84SAndre Oppermann 	 * Attempt reassembly; if it succeeds, proceed.
995f0cada84SAndre Oppermann 	 * ip_reass() will return a different mbuf.
996f0cada84SAndre Oppermann 	 */
99786425c62SRobert Watson 	IPSTAT_INC(ips_fragments);
9981b4381afSAndre Oppermann 	m->m_pkthdr.PH_loc.ptr = ip;
999f0cada84SAndre Oppermann 
1000f0cada84SAndre Oppermann 	/* Previous ip_reass() started here. */
1001df8bae1dSRodney W. Grimes 	/*
1002df8bae1dSRodney W. Grimes 	 * Presence of header sizes in mbufs
1003df8bae1dSRodney W. Grimes 	 * would confuse code below.
1004df8bae1dSRodney W. Grimes 	 */
1005df8bae1dSRodney W. Grimes 	m->m_data += hlen;
1006df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
1007df8bae1dSRodney W. Grimes 
1008df8bae1dSRodney W. Grimes 	/*
1009df8bae1dSRodney W. Grimes 	 * If first fragment to arrive, create a reassembly queue.
1010df8bae1dSRodney W. Grimes 	 */
1011042bbfa3SRobert Watson 	if (fp == NULL) {
1012603724d3SBjoern A. Zeeb 		fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
1013d248c7d7SRobert Watson 		if (fp == NULL)
1014df8bae1dSRodney W. Grimes 			goto dropfrag;
101536b0360bSRobert Watson #ifdef MAC
101630d239bcSRobert Watson 		if (mac_ipq_init(fp, M_NOWAIT) != 0) {
1017603724d3SBjoern A. Zeeb 			uma_zfree(V_ipq_zone, fp);
10181d7d0bfeSPawel Jakub Dawidek 			fp = NULL;
10195e7ce478SRobert Watson 			goto dropfrag;
10205e7ce478SRobert Watson 		}
102130d239bcSRobert Watson 		mac_ipq_create(m, fp);
102236b0360bSRobert Watson #endif
1023462b86feSPoul-Henning Kamp 		TAILQ_INSERT_HEAD(head, fp, ipq_list);
1024603724d3SBjoern A. Zeeb 		V_nipq++;
1025375386e2SMike Silbersack 		fp->ipq_nfrags = 1;
1026df8bae1dSRodney W. Grimes 		fp->ipq_ttl = IPFRAGTTL;
1027df8bae1dSRodney W. Grimes 		fp->ipq_p = ip->ip_p;
1028df8bae1dSRodney W. Grimes 		fp->ipq_id = ip->ip_id;
10296effc713SDoug Rabson 		fp->ipq_src = ip->ip_src;
10306effc713SDoug Rabson 		fp->ipq_dst = ip->ip_dst;
1031af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
1032af38c68cSLuigi Rizzo 		m->m_nextpkt = NULL;
1033800af1fbSMaxim Konovalov 		goto done;
103436b0360bSRobert Watson 	} else {
1035375386e2SMike Silbersack 		fp->ipq_nfrags++;
103636b0360bSRobert Watson #ifdef MAC
103730d239bcSRobert Watson 		mac_ipq_update(m, fp);
103836b0360bSRobert Watson #endif
1039df8bae1dSRodney W. Grimes 	}
1040df8bae1dSRodney W. Grimes 
10411b4381afSAndre Oppermann #define GETIP(m)	((struct ip*)((m)->m_pkthdr.PH_loc.ptr))
10426effc713SDoug Rabson 
1043df8bae1dSRodney W. Grimes 	/*
104459dfcba4SHajimu UMEMOTO 	 * Handle ECN by comparing this segment with the first one;
104559dfcba4SHajimu UMEMOTO 	 * if CE is set, do not lose CE.
104659dfcba4SHajimu UMEMOTO 	 * drop if CE and not-ECT are mixed for the same packet.
104759dfcba4SHajimu UMEMOTO 	 */
104859dfcba4SHajimu UMEMOTO 	ecn = ip->ip_tos & IPTOS_ECN_MASK;
104959dfcba4SHajimu UMEMOTO 	ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
105059dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_CE) {
105159dfcba4SHajimu UMEMOTO 		if (ecn0 == IPTOS_ECN_NOTECT)
105259dfcba4SHajimu UMEMOTO 			goto dropfrag;
105359dfcba4SHajimu UMEMOTO 		if (ecn0 != IPTOS_ECN_CE)
105459dfcba4SHajimu UMEMOTO 			GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
105559dfcba4SHajimu UMEMOTO 	}
105659dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
105759dfcba4SHajimu UMEMOTO 		goto dropfrag;
105859dfcba4SHajimu UMEMOTO 
105959dfcba4SHajimu UMEMOTO 	/*
1060df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
1061df8bae1dSRodney W. Grimes 	 */
10626effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
10638f134647SGleb Smirnoff 		if (ntohs(GETIP(q)->ip_off) > ntohs(ip->ip_off))
1064df8bae1dSRodney W. Grimes 			break;
1065df8bae1dSRodney W. Grimes 
1066df8bae1dSRodney W. Grimes 	/*
1067df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
1068df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
1069af38c68cSLuigi Rizzo 	 * segment.  If it provides all of our data, drop us, otherwise
1070af38c68cSLuigi Rizzo 	 * stick new segment in the proper place.
1071db4f9cc7SJonathan Lemon 	 *
10726bccea7cSRebecca Cran 	 * If some of the data is dropped from the preceding
1073db4f9cc7SJonathan Lemon 	 * segment, then it's checksum is invalidated.
1074df8bae1dSRodney W. Grimes 	 */
10756effc713SDoug Rabson 	if (p) {
10768f134647SGleb Smirnoff 		i = ntohs(GETIP(p)->ip_off) + ntohs(GETIP(p)->ip_len) -
10778f134647SGleb Smirnoff 		    ntohs(ip->ip_off);
1078df8bae1dSRodney W. Grimes 		if (i > 0) {
10798f134647SGleb Smirnoff 			if (i >= ntohs(ip->ip_len))
1080df8bae1dSRodney W. Grimes 				goto dropfrag;
10816a800098SYoshinobu Inoue 			m_adj(m, i);
1082db4f9cc7SJonathan Lemon 			m->m_pkthdr.csum_flags = 0;
10838f134647SGleb Smirnoff 			ip->ip_off = htons(ntohs(ip->ip_off) + i);
10848f134647SGleb Smirnoff 			ip->ip_len = htons(ntohs(ip->ip_len) - i);
1085df8bae1dSRodney W. Grimes 		}
1086af38c68cSLuigi Rizzo 		m->m_nextpkt = p->m_nextpkt;
1087af38c68cSLuigi Rizzo 		p->m_nextpkt = m;
1088af38c68cSLuigi Rizzo 	} else {
1089af38c68cSLuigi Rizzo 		m->m_nextpkt = fp->ipq_frags;
1090af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
1091df8bae1dSRodney W. Grimes 	}
1092df8bae1dSRodney W. Grimes 
1093df8bae1dSRodney W. Grimes 	/*
1094df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
1095df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
1096df8bae1dSRodney W. Grimes 	 */
10978f134647SGleb Smirnoff 	for (; q != NULL && ntohs(ip->ip_off) + ntohs(ip->ip_len) >
10988f134647SGleb Smirnoff 	    ntohs(GETIP(q)->ip_off); q = nq) {
10998f134647SGleb Smirnoff 		i = (ntohs(ip->ip_off) + ntohs(ip->ip_len)) -
11008f134647SGleb Smirnoff 		    ntohs(GETIP(q)->ip_off);
11018f134647SGleb Smirnoff 		if (i < ntohs(GETIP(q)->ip_len)) {
11028f134647SGleb Smirnoff 			GETIP(q)->ip_len = htons(ntohs(GETIP(q)->ip_len) - i);
11038f134647SGleb Smirnoff 			GETIP(q)->ip_off = htons(ntohs(GETIP(q)->ip_off) + i);
11046effc713SDoug Rabson 			m_adj(q, i);
1105db4f9cc7SJonathan Lemon 			q->m_pkthdr.csum_flags = 0;
1106df8bae1dSRodney W. Grimes 			break;
1107df8bae1dSRodney W. Grimes 		}
11086effc713SDoug Rabson 		nq = q->m_nextpkt;
1109af38c68cSLuigi Rizzo 		m->m_nextpkt = nq;
111086425c62SRobert Watson 		IPSTAT_INC(ips_fragdropped);
1111375386e2SMike Silbersack 		fp->ipq_nfrags--;
11126effc713SDoug Rabson 		m_freem(q);
1113df8bae1dSRodney W. Grimes 	}
1114df8bae1dSRodney W. Grimes 
1115df8bae1dSRodney W. Grimes 	/*
1116375386e2SMike Silbersack 	 * Check for complete reassembly and perform frag per packet
1117375386e2SMike Silbersack 	 * limiting.
1118375386e2SMike Silbersack 	 *
1119375386e2SMike Silbersack 	 * Frag limiting is performed here so that the nth frag has
1120375386e2SMike Silbersack 	 * a chance to complete the packet before we drop the packet.
1121375386e2SMike Silbersack 	 * As a result, n+1 frags are actually allowed per packet, but
1122375386e2SMike Silbersack 	 * only n will ever be stored. (n = maxfragsperpacket.)
1123375386e2SMike Silbersack 	 *
1124df8bae1dSRodney W. Grimes 	 */
11256effc713SDoug Rabson 	next = 0;
11266effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
11278f134647SGleb Smirnoff 		if (ntohs(GETIP(q)->ip_off) != next) {
1128603724d3SBjoern A. Zeeb 			if (fp->ipq_nfrags > V_maxfragsperpacket) {
112986425c62SRobert Watson 				IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1130375386e2SMike Silbersack 				ip_freef(head, fp);
113199e8617dSMaxim Konovalov 			}
1132f0cada84SAndre Oppermann 			goto done;
1133375386e2SMike Silbersack 		}
11348f134647SGleb Smirnoff 		next += ntohs(GETIP(q)->ip_len);
11356effc713SDoug Rabson 	}
11366effc713SDoug Rabson 	/* Make sure the last packet didn't have the IP_MF flag */
1137b09dc7e3SAndre Oppermann 	if (p->m_flags & M_IP_FRAG) {
1138603724d3SBjoern A. Zeeb 		if (fp->ipq_nfrags > V_maxfragsperpacket) {
113986425c62SRobert Watson 			IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1140375386e2SMike Silbersack 			ip_freef(head, fp);
114199e8617dSMaxim Konovalov 		}
1142f0cada84SAndre Oppermann 		goto done;
1143375386e2SMike Silbersack 	}
1144df8bae1dSRodney W. Grimes 
1145df8bae1dSRodney W. Grimes 	/*
1146430d30d8SBill Fenner 	 * Reassembly is complete.  Make sure the packet is a sane size.
1147430d30d8SBill Fenner 	 */
11486effc713SDoug Rabson 	q = fp->ipq_frags;
11496effc713SDoug Rabson 	ip = GETIP(q);
115053be11f6SPoul-Henning Kamp 	if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
115186425c62SRobert Watson 		IPSTAT_INC(ips_toolong);
115286425c62SRobert Watson 		IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1153462b86feSPoul-Henning Kamp 		ip_freef(head, fp);
1154f0cada84SAndre Oppermann 		goto done;
1155430d30d8SBill Fenner 	}
1156430d30d8SBill Fenner 
1157430d30d8SBill Fenner 	/*
1158430d30d8SBill Fenner 	 * Concatenate fragments.
1159df8bae1dSRodney W. Grimes 	 */
11606effc713SDoug Rabson 	m = q;
1161df8bae1dSRodney W. Grimes 	t = m->m_next;
116202410549SRobert Watson 	m->m_next = NULL;
1163df8bae1dSRodney W. Grimes 	m_cat(m, t);
11646effc713SDoug Rabson 	nq = q->m_nextpkt;
116502410549SRobert Watson 	q->m_nextpkt = NULL;
11666effc713SDoug Rabson 	for (q = nq; q != NULL; q = nq) {
11676effc713SDoug Rabson 		nq = q->m_nextpkt;
1168945aa40dSDoug Rabson 		q->m_nextpkt = NULL;
1169db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1170db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1171a8db1d93SJonathan Lemon 		m_cat(m, q);
1172df8bae1dSRodney W. Grimes 	}
11736edb555dSOleg Bulyzhin 	/*
11746edb555dSOleg Bulyzhin 	 * In order to do checksumming faster we do 'end-around carry' here
11756edb555dSOleg Bulyzhin 	 * (and not in for{} loop), though it implies we are not going to
11766edb555dSOleg Bulyzhin 	 * reassemble more than 64k fragments.
11776edb555dSOleg Bulyzhin 	 */
1178c732cd1aSPyun YongHyeon 	while (m->m_pkthdr.csum_data & 0xffff0000)
1179c732cd1aSPyun YongHyeon 		m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
1180c732cd1aSPyun YongHyeon 		    (m->m_pkthdr.csum_data >> 16);
118136b0360bSRobert Watson #ifdef MAC
118230d239bcSRobert Watson 	mac_ipq_reassemble(fp, m);
118330d239bcSRobert Watson 	mac_ipq_destroy(fp);
118436b0360bSRobert Watson #endif
1185df8bae1dSRodney W. Grimes 
1186df8bae1dSRodney W. Grimes 	/*
1187f0cada84SAndre Oppermann 	 * Create header for new ip packet by modifying header of first
1188f0cada84SAndre Oppermann 	 * packet;  dequeue and discard fragment reassembly header.
1189df8bae1dSRodney W. Grimes 	 * Make header visible.
1190df8bae1dSRodney W. Grimes 	 */
11918f134647SGleb Smirnoff 	ip->ip_len = htons((ip->ip_hl << 2) + next);
11926effc713SDoug Rabson 	ip->ip_src = fp->ipq_src;
11936effc713SDoug Rabson 	ip->ip_dst = fp->ipq_dst;
1194462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(head, fp, ipq_list);
1195603724d3SBjoern A. Zeeb 	V_nipq--;
1196603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
119753be11f6SPoul-Henning Kamp 	m->m_len += (ip->ip_hl << 2);
119853be11f6SPoul-Henning Kamp 	m->m_data -= (ip->ip_hl << 2);
1199df8bae1dSRodney W. Grimes 	/* some debugging cruft by sklower, below, will go away soon */
1200a5554bf0SPoul-Henning Kamp 	if (m->m_flags & M_PKTHDR)	/* XXX this should be done elsewhere */
1201a5554bf0SPoul-Henning Kamp 		m_fixhdr(m);
120286425c62SRobert Watson 	IPSTAT_INC(ips_reassembled);
1203f0cada84SAndre Oppermann 	IPQ_UNLOCK();
1204*b8bc95cdSAdrian Chadd 
1205*b8bc95cdSAdrian Chadd #ifdef	RSS
1206*b8bc95cdSAdrian Chadd 	/*
1207*b8bc95cdSAdrian Chadd 	 * Query the RSS layer for the flowid / flowtype for the
1208*b8bc95cdSAdrian Chadd 	 * mbuf payload.
1209*b8bc95cdSAdrian Chadd 	 *
1210*b8bc95cdSAdrian Chadd 	 * For now, just assume we have to calculate a new one.
1211*b8bc95cdSAdrian Chadd 	 * Later on we should check to see if the assigned flowid matches
1212*b8bc95cdSAdrian Chadd 	 * what RSS wants for the given IP protocol and if so, just keep it.
1213*b8bc95cdSAdrian Chadd 	 *
1214*b8bc95cdSAdrian Chadd 	 * We then queue into the relevant netisr so it can be dispatched
1215*b8bc95cdSAdrian Chadd 	 * to the correct CPU.
1216*b8bc95cdSAdrian Chadd 	 *
1217*b8bc95cdSAdrian Chadd 	 * Note - this may return 1, which means the flowid in the mbuf
1218*b8bc95cdSAdrian Chadd 	 * is correct for the configured RSS hash types and can be used.
1219*b8bc95cdSAdrian Chadd 	 */
1220*b8bc95cdSAdrian Chadd 	if (rss_mbuf_software_hash_v4(m, 0, &rss_hash, &rss_type) == 0) {
1221*b8bc95cdSAdrian Chadd 		m->m_pkthdr.flowid = rss_hash;
1222*b8bc95cdSAdrian Chadd 		M_HASHTYPE_SET(m, rss_type);
1223*b8bc95cdSAdrian Chadd 		m->m_flags |= M_FLOWID;
1224*b8bc95cdSAdrian Chadd 	}
1225*b8bc95cdSAdrian Chadd #endif
1226*b8bc95cdSAdrian Chadd 
1227*b8bc95cdSAdrian Chadd #ifdef	RSS
1228*b8bc95cdSAdrian Chadd 	/*
1229*b8bc95cdSAdrian Chadd 	 * Queue/dispatch for reprocessing.
1230*b8bc95cdSAdrian Chadd 	 *
1231*b8bc95cdSAdrian Chadd 	 * Note: this is much slower than just handling the frame in the
1232*b8bc95cdSAdrian Chadd 	 * current receive context.  It's likely worth investigating
1233*b8bc95cdSAdrian Chadd 	 * why this is.
1234*b8bc95cdSAdrian Chadd 	 */
1235*b8bc95cdSAdrian Chadd 	netisr_dispatch(NETISR_IP_DIRECT, m);
1236*b8bc95cdSAdrian Chadd 	return (NULL);
1237*b8bc95cdSAdrian Chadd #endif
1238*b8bc95cdSAdrian Chadd 
1239*b8bc95cdSAdrian Chadd 	/* Handle in-line */
12406a800098SYoshinobu Inoue 	return (m);
1241df8bae1dSRodney W. Grimes 
1242df8bae1dSRodney W. Grimes dropfrag:
124386425c62SRobert Watson 	IPSTAT_INC(ips_fragdropped);
1244042bbfa3SRobert Watson 	if (fp != NULL)
1245375386e2SMike Silbersack 		fp->ipq_nfrags--;
1246df8bae1dSRodney W. Grimes 	m_freem(m);
1247f0cada84SAndre Oppermann done:
1248f0cada84SAndre Oppermann 	IPQ_UNLOCK();
1249f0cada84SAndre Oppermann 	return (NULL);
12506effc713SDoug Rabson 
12516effc713SDoug Rabson #undef GETIP
1252df8bae1dSRodney W. Grimes }
1253df8bae1dSRodney W. Grimes 
1254df8bae1dSRodney W. Grimes /*
1255df8bae1dSRodney W. Grimes  * Free a fragment reassembly header and all
1256df8bae1dSRodney W. Grimes  * associated datagrams.
1257df8bae1dSRodney W. Grimes  */
12580312fbe9SPoul-Henning Kamp static void
1259f2565d68SRobert Watson ip_freef(struct ipqhead *fhp, struct ipq *fp)
1260df8bae1dSRodney W. Grimes {
1261f2565d68SRobert Watson 	struct mbuf *q;
1262df8bae1dSRodney W. Grimes 
12632fad1e93SSam Leffler 	IPQ_LOCK_ASSERT();
12642fad1e93SSam Leffler 
12656effc713SDoug Rabson 	while (fp->ipq_frags) {
12666effc713SDoug Rabson 		q = fp->ipq_frags;
12676effc713SDoug Rabson 		fp->ipq_frags = q->m_nextpkt;
12686effc713SDoug Rabson 		m_freem(q);
1269df8bae1dSRodney W. Grimes 	}
1270462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(fhp, fp, ipq_list);
1271603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
1272603724d3SBjoern A. Zeeb 	V_nipq--;
1273df8bae1dSRodney W. Grimes }
1274df8bae1dSRodney W. Grimes 
1275df8bae1dSRodney W. Grimes /*
1276df8bae1dSRodney W. Grimes  * IP timer processing;
1277df8bae1dSRodney W. Grimes  * if a timer expires on a reassembly
1278df8bae1dSRodney W. Grimes  * queue, discard it.
1279df8bae1dSRodney W. Grimes  */
1280df8bae1dSRodney W. Grimes void
1281f2565d68SRobert Watson ip_slowtimo(void)
1282df8bae1dSRodney W. Grimes {
12838b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1284f2565d68SRobert Watson 	struct ipq *fp;
1285194a213eSAndrey A. Chernov 	int i;
1286df8bae1dSRodney W. Grimes 
12875ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
12882fad1e93SSam Leffler 	IPQ_LOCK();
12898b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
12908b615593SMarko Zec 		CURVNET_SET(vnet_iter);
1291194a213eSAndrey A. Chernov 		for (i = 0; i < IPREASS_NHASH; i++) {
1292603724d3SBjoern A. Zeeb 			for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) {
1293462b86feSPoul-Henning Kamp 				struct ipq *fpp;
1294462b86feSPoul-Henning Kamp 
1295462b86feSPoul-Henning Kamp 				fpp = fp;
1296462b86feSPoul-Henning Kamp 				fp = TAILQ_NEXT(fp, ipq_list);
1297462b86feSPoul-Henning Kamp 				if(--fpp->ipq_ttl == 0) {
129886425c62SRobert Watson 					IPSTAT_ADD(ips_fragtimeout,
129986425c62SRobert Watson 					    fpp->ipq_nfrags);
1300603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], fpp);
1301df8bae1dSRodney W. Grimes 				}
1302df8bae1dSRodney W. Grimes 			}
1303194a213eSAndrey A. Chernov 		}
1304690a6055SJesper Skriver 		/*
1305690a6055SJesper Skriver 		 * If we are over the maximum number of fragments
1306690a6055SJesper Skriver 		 * (due to the limit being lowered), drain off
1307690a6055SJesper Skriver 		 * enough to get down to the new limit.
1308690a6055SJesper Skriver 		 */
1309603724d3SBjoern A. Zeeb 		if (V_maxnipq >= 0 && V_nipq > V_maxnipq) {
1310690a6055SJesper Skriver 			for (i = 0; i < IPREASS_NHASH; i++) {
13118b615593SMarko Zec 				while (V_nipq > V_maxnipq &&
13128b615593SMarko Zec 				    !TAILQ_EMPTY(&V_ipq[i])) {
131386425c62SRobert Watson 					IPSTAT_ADD(ips_fragdropped,
131486425c62SRobert Watson 					    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
13158b615593SMarko Zec 					ip_freef(&V_ipq[i],
13168b615593SMarko Zec 					    TAILQ_FIRST(&V_ipq[i]));
1317690a6055SJesper Skriver 				}
1318690a6055SJesper Skriver 			}
1319690a6055SJesper Skriver 		}
13208b615593SMarko Zec 		CURVNET_RESTORE();
13218b615593SMarko Zec 	}
13222fad1e93SSam Leffler 	IPQ_UNLOCK();
13235ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
1324df8bae1dSRodney W. Grimes }
1325df8bae1dSRodney W. Grimes 
1326df8bae1dSRodney W. Grimes /*
1327df8bae1dSRodney W. Grimes  * Drain off all datagram fragments.
1328df8bae1dSRodney W. Grimes  */
13299802380eSBjoern A. Zeeb static void
13309802380eSBjoern A. Zeeb ip_drain_locked(void)
1331df8bae1dSRodney W. Grimes {
1332194a213eSAndrey A. Chernov 	int     i;
1333ce29ab3aSGarrett Wollman 
13349802380eSBjoern A. Zeeb 	IPQ_LOCK_ASSERT();
13359802380eSBjoern A. Zeeb 
1336194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1337603724d3SBjoern A. Zeeb 		while(!TAILQ_EMPTY(&V_ipq[i])) {
133886425c62SRobert Watson 			IPSTAT_ADD(ips_fragdropped,
133986425c62SRobert Watson 			    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
1340603724d3SBjoern A. Zeeb 			ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1341194a213eSAndrey A. Chernov 		}
1342194a213eSAndrey A. Chernov 	}
13439802380eSBjoern A. Zeeb }
13449802380eSBjoern A. Zeeb 
13459802380eSBjoern A. Zeeb void
13469802380eSBjoern A. Zeeb ip_drain(void)
13479802380eSBjoern A. Zeeb {
13489802380eSBjoern A. Zeeb 	VNET_ITERATOR_DECL(vnet_iter);
13499802380eSBjoern A. Zeeb 
13509802380eSBjoern A. Zeeb 	VNET_LIST_RLOCK_NOSLEEP();
13519802380eSBjoern A. Zeeb 	IPQ_LOCK();
13529802380eSBjoern A. Zeeb 	VNET_FOREACH(vnet_iter) {
13539802380eSBjoern A. Zeeb 		CURVNET_SET(vnet_iter);
13549802380eSBjoern A. Zeeb 		ip_drain_locked();
13558b615593SMarko Zec 		CURVNET_RESTORE();
13568b615593SMarko Zec 	}
13572fad1e93SSam Leffler 	IPQ_UNLOCK();
13585ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
1359ce29ab3aSGarrett Wollman 	in_rtqdrain();
1360df8bae1dSRodney W. Grimes }
1361df8bae1dSRodney W. Grimes 
1362df8bae1dSRodney W. Grimes /*
1363de38924dSAndre Oppermann  * The protocol to be inserted into ip_protox[] must be already registered
1364de38924dSAndre Oppermann  * in inetsw[], either statically or through pf_proto_register().
1365de38924dSAndre Oppermann  */
1366de38924dSAndre Oppermann int
13671b48d245SBjoern A. Zeeb ipproto_register(short ipproto)
1368de38924dSAndre Oppermann {
1369de38924dSAndre Oppermann 	struct protosw *pr;
1370de38924dSAndre Oppermann 
1371de38924dSAndre Oppermann 	/* Sanity checks. */
13721b48d245SBjoern A. Zeeb 	if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
1373de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1374de38924dSAndre Oppermann 
1375de38924dSAndre Oppermann 	/*
1376de38924dSAndre Oppermann 	 * The protocol slot must not be occupied by another protocol
1377de38924dSAndre Oppermann 	 * already.  An index pointing to IPPROTO_RAW is unused.
1378de38924dSAndre Oppermann 	 */
1379de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1380de38924dSAndre Oppermann 	if (pr == NULL)
1381de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1382de38924dSAndre Oppermann 	if (ip_protox[ipproto] != pr - inetsw)	/* IPPROTO_RAW */
1383de38924dSAndre Oppermann 		return (EEXIST);
1384de38924dSAndre Oppermann 
1385de38924dSAndre Oppermann 	/* Find the protocol position in inetsw[] and set the index. */
1386de38924dSAndre Oppermann 	for (pr = inetdomain.dom_protosw;
1387de38924dSAndre Oppermann 	     pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1388de38924dSAndre Oppermann 		if (pr->pr_domain->dom_family == PF_INET &&
1389de38924dSAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol == ipproto) {
1390de38924dSAndre Oppermann 			ip_protox[pr->pr_protocol] = pr - inetsw;
1391de38924dSAndre Oppermann 			return (0);
1392de38924dSAndre Oppermann 		}
1393de38924dSAndre Oppermann 	}
1394de38924dSAndre Oppermann 	return (EPROTONOSUPPORT);
1395de38924dSAndre Oppermann }
1396de38924dSAndre Oppermann 
1397de38924dSAndre Oppermann int
13981b48d245SBjoern A. Zeeb ipproto_unregister(short ipproto)
1399de38924dSAndre Oppermann {
1400de38924dSAndre Oppermann 	struct protosw *pr;
1401de38924dSAndre Oppermann 
1402de38924dSAndre Oppermann 	/* Sanity checks. */
14031b48d245SBjoern A. Zeeb 	if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
1404de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1405de38924dSAndre Oppermann 
1406de38924dSAndre Oppermann 	/* Check if the protocol was indeed registered. */
1407de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1408de38924dSAndre Oppermann 	if (pr == NULL)
1409de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1410de38924dSAndre Oppermann 	if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
1411de38924dSAndre Oppermann 		return (ENOENT);
1412de38924dSAndre Oppermann 
1413de38924dSAndre Oppermann 	/* Reset the protocol slot to IPPROTO_RAW. */
1414de38924dSAndre Oppermann 	ip_protox[ipproto] = pr - inetsw;
1415de38924dSAndre Oppermann 	return (0);
1416de38924dSAndre Oppermann }
1417de38924dSAndre Oppermann 
1418df8bae1dSRodney W. Grimes /*
14198c0fec80SRobert Watson  * Given address of next destination (final or next hop), return (referenced)
14208c0fec80SRobert Watson  * internet address info of interface to be used to get there.
1421df8bae1dSRodney W. Grimes  */
1422bd714208SRuslan Ermilov struct in_ifaddr *
14238b07e49aSJulian Elischer ip_rtaddr(struct in_addr dst, u_int fibnum)
1424df8bae1dSRodney W. Grimes {
142597d8d152SAndre Oppermann 	struct route sro;
142602c1c707SAndre Oppermann 	struct sockaddr_in *sin;
142719e5b0a7SRobert Watson 	struct in_ifaddr *ia;
1428df8bae1dSRodney W. Grimes 
14290cfbbe3bSAndre Oppermann 	bzero(&sro, sizeof(sro));
143097d8d152SAndre Oppermann 	sin = (struct sockaddr_in *)&sro.ro_dst;
1431df8bae1dSRodney W. Grimes 	sin->sin_family = AF_INET;
1432df8bae1dSRodney W. Grimes 	sin->sin_len = sizeof(*sin);
1433df8bae1dSRodney W. Grimes 	sin->sin_addr = dst;
14346e6b3f7cSQing Li 	in_rtalloc_ign(&sro, 0, fibnum);
1435df8bae1dSRodney W. Grimes 
143697d8d152SAndre Oppermann 	if (sro.ro_rt == NULL)
143702410549SRobert Watson 		return (NULL);
143802c1c707SAndre Oppermann 
143919e5b0a7SRobert Watson 	ia = ifatoia(sro.ro_rt->rt_ifa);
144019e5b0a7SRobert Watson 	ifa_ref(&ia->ia_ifa);
144197d8d152SAndre Oppermann 	RTFREE(sro.ro_rt);
144219e5b0a7SRobert Watson 	return (ia);
1443df8bae1dSRodney W. Grimes }
1444df8bae1dSRodney W. Grimes 
1445df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = {
1446df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1447df8bae1dSRodney W. Grimes 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1448df8bae1dSRodney W. Grimes 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1449df8bae1dSRodney W. Grimes 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1450fcaf9f91SMike Silbersack 	0,		0,		EHOSTUNREACH,	0,
14513b8123b7SJesper Skriver 	ENOPROTOOPT,	ECONNREFUSED
1452df8bae1dSRodney W. Grimes };
1453df8bae1dSRodney W. Grimes 
1454df8bae1dSRodney W. Grimes /*
1455df8bae1dSRodney W. Grimes  * Forward a packet.  If some error occurs return the sender
1456df8bae1dSRodney W. Grimes  * an icmp packet.  Note we can't always generate a meaningful
1457df8bae1dSRodney W. Grimes  * icmp message because icmp doesn't have a large enough repertoire
1458df8bae1dSRodney W. Grimes  * of codes and types.
1459df8bae1dSRodney W. Grimes  *
1460df8bae1dSRodney W. Grimes  * If not forwarding, just drop the packet.  This could be confusing
1461df8bae1dSRodney W. Grimes  * if ipforwarding was zero but some routing protocol was advancing
1462df8bae1dSRodney W. Grimes  * us as a gateway to somewhere.  However, we must let the routing
1463df8bae1dSRodney W. Grimes  * protocol deal with that.
1464df8bae1dSRodney W. Grimes  *
1465df8bae1dSRodney W. Grimes  * The srcrt parameter indicates whether the packet is being forwarded
1466df8bae1dSRodney W. Grimes  * via a source route.
1467df8bae1dSRodney W. Grimes  */
14689b932e9eSAndre Oppermann void
14699b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt)
1470df8bae1dSRodney W. Grimes {
14712b25acc1SLuigi Rizzo 	struct ip *ip = mtod(m, struct ip *);
1472efbad259SEdward Tomasz Napierala 	struct in_ifaddr *ia;
1473df8bae1dSRodney W. Grimes 	struct mbuf *mcopy;
14749b932e9eSAndre Oppermann 	struct in_addr dest;
1475b835b6feSBjoern A. Zeeb 	struct route ro;
1476c773494eSAndre Oppermann 	int error, type = 0, code = 0, mtu = 0;
14773efc3014SJulian Elischer 
14789b932e9eSAndre Oppermann 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
147986425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
1480df8bae1dSRodney W. Grimes 		m_freem(m);
1481df8bae1dSRodney W. Grimes 		return;
1482df8bae1dSRodney W. Grimes 	}
14831b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
1484603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
14851b968362SDag-Erling Smørgrav #endif
1486df8bae1dSRodney W. Grimes 		if (ip->ip_ttl <= IPTTLDEC) {
14871b968362SDag-Erling Smørgrav 			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
148802c1c707SAndre Oppermann 			    0, 0);
1489df8bae1dSRodney W. Grimes 			return;
1490df8bae1dSRodney W. Grimes 		}
14911b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
14921b968362SDag-Erling Smørgrav 	}
14931b968362SDag-Erling Smørgrav #endif
1494df8bae1dSRodney W. Grimes 
14958b07e49aSJulian Elischer 	ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
1496efbad259SEdward Tomasz Napierala #ifndef IPSEC
1497efbad259SEdward Tomasz Napierala 	/*
1498efbad259SEdward Tomasz Napierala 	 * 'ia' may be NULL if there is no route for this destination.
1499efbad259SEdward Tomasz Napierala 	 * In case of IPsec, Don't discard it just yet, but pass it to
1500efbad259SEdward Tomasz Napierala 	 * ip_output in case of outgoing IPsec policy.
1501efbad259SEdward Tomasz Napierala 	 */
1502d23d475fSGuido van Rooij 	if (!srcrt && ia == NULL) {
150302c1c707SAndre Oppermann 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1504df8bae1dSRodney W. Grimes 		return;
150502c1c707SAndre Oppermann 	}
1506efbad259SEdward Tomasz Napierala #endif
1507df8bae1dSRodney W. Grimes 
1508df8bae1dSRodney W. Grimes 	/*
1509bfef7ed4SIan Dowse 	 * Save the IP header and at most 8 bytes of the payload,
1510bfef7ed4SIan Dowse 	 * in case we need to generate an ICMP message to the src.
1511bfef7ed4SIan Dowse 	 *
15124d2e3692SLuigi Rizzo 	 * XXX this can be optimized a lot by saving the data in a local
15134d2e3692SLuigi Rizzo 	 * buffer on the stack (72 bytes at most), and only allocating the
15144d2e3692SLuigi Rizzo 	 * mbuf if really necessary. The vast majority of the packets
15154d2e3692SLuigi Rizzo 	 * are forwarded without having to send an ICMP back (either
15164d2e3692SLuigi Rizzo 	 * because unnecessary, or because rate limited), so we are
15174d2e3692SLuigi Rizzo 	 * really we are wasting a lot of work here.
15184d2e3692SLuigi Rizzo 	 *
1519bfef7ed4SIan Dowse 	 * We don't use m_copy() because it might return a reference
1520bfef7ed4SIan Dowse 	 * to a shared cluster. Both this function and ip_output()
1521bfef7ed4SIan Dowse 	 * assume exclusive access to the IP header in `m', so any
1522bfef7ed4SIan Dowse 	 * data in a cluster may change before we reach icmp_error().
1523df8bae1dSRodney W. Grimes 	 */
1524dc4ad05eSGleb Smirnoff 	mcopy = m_gethdr(M_NOWAIT, m->m_type);
1525eb1b1807SGleb Smirnoff 	if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) {
15269967cafcSSam Leffler 		/*
15279967cafcSSam Leffler 		 * It's probably ok if the pkthdr dup fails (because
15289967cafcSSam Leffler 		 * the deep copy of the tag chain failed), but for now
15299967cafcSSam Leffler 		 * be conservative and just discard the copy since
15309967cafcSSam Leffler 		 * code below may some day want the tags.
15319967cafcSSam Leffler 		 */
15329967cafcSSam Leffler 		m_free(mcopy);
15339967cafcSSam Leffler 		mcopy = NULL;
15349967cafcSSam Leffler 	}
1535bfef7ed4SIan Dowse 	if (mcopy != NULL) {
15368f134647SGleb Smirnoff 		mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy));
1537e6b0a570SBruce M Simpson 		mcopy->m_pkthdr.len = mcopy->m_len;
1538bfef7ed4SIan Dowse 		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1539bfef7ed4SIan Dowse 	}
154004287599SRuslan Ermilov 
154104287599SRuslan Ermilov #ifdef IPSTEALTH
1542603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
154304287599SRuslan Ermilov #endif
154404287599SRuslan Ermilov 		ip->ip_ttl -= IPTTLDEC;
154504287599SRuslan Ermilov #ifdef IPSTEALTH
154604287599SRuslan Ermilov 	}
154704287599SRuslan Ermilov #endif
1548df8bae1dSRodney W. Grimes 
1549df8bae1dSRodney W. Grimes 	/*
1550df8bae1dSRodney W. Grimes 	 * If forwarding packet using same interface that it came in on,
1551df8bae1dSRodney W. Grimes 	 * perhaps should send a redirect to sender to shortcut a hop.
1552df8bae1dSRodney W. Grimes 	 * Only send redirect if source is sending directly to us,
1553df8bae1dSRodney W. Grimes 	 * and if packet was not source routed (or has any options).
1554df8bae1dSRodney W. Grimes 	 * Also, don't send redirect if forwarding using a default route
1555df8bae1dSRodney W. Grimes 	 * or a route modified by a redirect.
1556df8bae1dSRodney W. Grimes 	 */
15579b932e9eSAndre Oppermann 	dest.s_addr = 0;
1558efbad259SEdward Tomasz Napierala 	if (!srcrt && V_ipsendredirects &&
1559efbad259SEdward Tomasz Napierala 	    ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) {
156002c1c707SAndre Oppermann 		struct sockaddr_in *sin;
156102c1c707SAndre Oppermann 		struct rtentry *rt;
156202c1c707SAndre Oppermann 
15630cfbbe3bSAndre Oppermann 		bzero(&ro, sizeof(ro));
156402c1c707SAndre Oppermann 		sin = (struct sockaddr_in *)&ro.ro_dst;
156502c1c707SAndre Oppermann 		sin->sin_family = AF_INET;
156602c1c707SAndre Oppermann 		sin->sin_len = sizeof(*sin);
15679b932e9eSAndre Oppermann 		sin->sin_addr = ip->ip_dst;
15686e6b3f7cSQing Li 		in_rtalloc_ign(&ro, 0, M_GETFIB(m));
156902c1c707SAndre Oppermann 
157002c1c707SAndre Oppermann 		rt = ro.ro_rt;
157102c1c707SAndre Oppermann 
157202c1c707SAndre Oppermann 		if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
15739b932e9eSAndre Oppermann 		    satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1574df8bae1dSRodney W. Grimes #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1575df8bae1dSRodney W. Grimes 			u_long src = ntohl(ip->ip_src.s_addr);
1576df8bae1dSRodney W. Grimes 
1577df8bae1dSRodney W. Grimes 			if (RTA(rt) &&
1578df8bae1dSRodney W. Grimes 			    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1579df8bae1dSRodney W. Grimes 				if (rt->rt_flags & RTF_GATEWAY)
15809b932e9eSAndre Oppermann 					dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1581df8bae1dSRodney W. Grimes 				else
15829b932e9eSAndre Oppermann 					dest.s_addr = ip->ip_dst.s_addr;
1583df8bae1dSRodney W. Grimes 				/* Router requirements says to only send host redirects */
1584df8bae1dSRodney W. Grimes 				type = ICMP_REDIRECT;
1585df8bae1dSRodney W. Grimes 				code = ICMP_REDIRECT_HOST;
1586df8bae1dSRodney W. Grimes 			}
1587df8bae1dSRodney W. Grimes 		}
158802c1c707SAndre Oppermann 		if (rt)
158902c1c707SAndre Oppermann 			RTFREE(rt);
159002c1c707SAndre Oppermann 	}
1591df8bae1dSRodney W. Grimes 
1592b835b6feSBjoern A. Zeeb 	/*
1593b835b6feSBjoern A. Zeeb 	 * Try to cache the route MTU from ip_output so we can consider it for
1594b835b6feSBjoern A. Zeeb 	 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191.
1595b835b6feSBjoern A. Zeeb 	 */
1596b835b6feSBjoern A. Zeeb 	bzero(&ro, sizeof(ro));
1597b835b6feSBjoern A. Zeeb 
1598b835b6feSBjoern A. Zeeb 	error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1599b835b6feSBjoern A. Zeeb 
1600b835b6feSBjoern A. Zeeb 	if (error == EMSGSIZE && ro.ro_rt)
1601e3a7aa6fSGleb Smirnoff 		mtu = ro.ro_rt->rt_mtu;
1602bf984051SGleb Smirnoff 	RO_RTFREE(&ro);
1603b835b6feSBjoern A. Zeeb 
1604df8bae1dSRodney W. Grimes 	if (error)
160586425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
1606df8bae1dSRodney W. Grimes 	else {
160786425c62SRobert Watson 		IPSTAT_INC(ips_forward);
1608df8bae1dSRodney W. Grimes 		if (type)
160986425c62SRobert Watson 			IPSTAT_INC(ips_redirectsent);
1610df8bae1dSRodney W. Grimes 		else {
16119188b4a1SAndre Oppermann 			if (mcopy)
1612df8bae1dSRodney W. Grimes 				m_freem(mcopy);
16138c0fec80SRobert Watson 			if (ia != NULL)
16148c0fec80SRobert Watson 				ifa_free(&ia->ia_ifa);
1615df8bae1dSRodney W. Grimes 			return;
1616df8bae1dSRodney W. Grimes 		}
1617df8bae1dSRodney W. Grimes 	}
16188c0fec80SRobert Watson 	if (mcopy == NULL) {
16198c0fec80SRobert Watson 		if (ia != NULL)
16208c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
1621df8bae1dSRodney W. Grimes 		return;
16228c0fec80SRobert Watson 	}
1623df8bae1dSRodney W. Grimes 
1624df8bae1dSRodney W. Grimes 	switch (error) {
1625df8bae1dSRodney W. Grimes 
1626df8bae1dSRodney W. Grimes 	case 0:				/* forwarded, but need redirect */
1627df8bae1dSRodney W. Grimes 		/* type, code set above */
1628df8bae1dSRodney W. Grimes 		break;
1629df8bae1dSRodney W. Grimes 
1630efbad259SEdward Tomasz Napierala 	case ENETUNREACH:
1631df8bae1dSRodney W. Grimes 	case EHOSTUNREACH:
1632df8bae1dSRodney W. Grimes 	case ENETDOWN:
1633df8bae1dSRodney W. Grimes 	case EHOSTDOWN:
1634df8bae1dSRodney W. Grimes 	default:
1635df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1636df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_HOST;
1637df8bae1dSRodney W. Grimes 		break;
1638df8bae1dSRodney W. Grimes 
1639df8bae1dSRodney W. Grimes 	case EMSGSIZE:
1640df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1641df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_NEEDFRAG;
16421dfcf0d2SAndre Oppermann 
1643b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
1644b835b6feSBjoern A. Zeeb 		/*
1645b835b6feSBjoern A. Zeeb 		 * If IPsec is configured for this path,
1646b835b6feSBjoern A. Zeeb 		 * override any possibly mtu value set by ip_output.
1647b835b6feSBjoern A. Zeeb 		 */
16481c044382SBjoern A. Zeeb 		mtu = ip_ipsec_mtu(mcopy, mtu);
1649b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
16509b932e9eSAndre Oppermann 		/*
1651b835b6feSBjoern A. Zeeb 		 * If the MTU was set before make sure we are below the
1652b835b6feSBjoern A. Zeeb 		 * interface MTU.
1653ab48768bSAndre Oppermann 		 * If the MTU wasn't set before use the interface mtu or
1654ab48768bSAndre Oppermann 		 * fall back to the next smaller mtu step compared to the
1655ab48768bSAndre Oppermann 		 * current packet size.
16569b932e9eSAndre Oppermann 		 */
1657b835b6feSBjoern A. Zeeb 		if (mtu != 0) {
1658b835b6feSBjoern A. Zeeb 			if (ia != NULL)
1659b835b6feSBjoern A. Zeeb 				mtu = min(mtu, ia->ia_ifp->if_mtu);
1660b835b6feSBjoern A. Zeeb 		} else {
1661ab48768bSAndre Oppermann 			if (ia != NULL)
1662c773494eSAndre Oppermann 				mtu = ia->ia_ifp->if_mtu;
1663ab48768bSAndre Oppermann 			else
16648f134647SGleb Smirnoff 				mtu = ip_next_mtu(ntohs(ip->ip_len), 0);
1665ab48768bSAndre Oppermann 		}
166686425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
1667df8bae1dSRodney W. Grimes 		break;
1668df8bae1dSRodney W. Grimes 
1669df8bae1dSRodney W. Grimes 	case ENOBUFS:
1670df285b3dSMike Silbersack 		/*
1671df285b3dSMike Silbersack 		 * A router should not generate ICMP_SOURCEQUENCH as
1672df285b3dSMike Silbersack 		 * required in RFC1812 Requirements for IP Version 4 Routers.
1673df285b3dSMike Silbersack 		 * Source quench could be a big problem under DoS attacks,
1674df285b3dSMike Silbersack 		 * or if the underlying interface is rate-limited.
1675df285b3dSMike Silbersack 		 * Those who need source quench packets may re-enable them
1676df285b3dSMike Silbersack 		 * via the net.inet.ip.sendsourcequench sysctl.
1677df285b3dSMike Silbersack 		 */
1678603724d3SBjoern A. Zeeb 		if (V_ip_sendsourcequench == 0) {
1679df285b3dSMike Silbersack 			m_freem(mcopy);
16808c0fec80SRobert Watson 			if (ia != NULL)
16818c0fec80SRobert Watson 				ifa_free(&ia->ia_ifa);
1682df285b3dSMike Silbersack 			return;
1683df285b3dSMike Silbersack 		} else {
1684df8bae1dSRodney W. Grimes 			type = ICMP_SOURCEQUENCH;
1685df8bae1dSRodney W. Grimes 			code = 0;
1686df285b3dSMike Silbersack 		}
1687df8bae1dSRodney W. Grimes 		break;
16883a06e3e0SRuslan Ermilov 
16893a06e3e0SRuslan Ermilov 	case EACCES:			/* ipfw denied packet */
16903a06e3e0SRuslan Ermilov 		m_freem(mcopy);
16918c0fec80SRobert Watson 		if (ia != NULL)
16928c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
16933a06e3e0SRuslan Ermilov 		return;
1694df8bae1dSRodney W. Grimes 	}
16958c0fec80SRobert Watson 	if (ia != NULL)
16968c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
1697c773494eSAndre Oppermann 	icmp_error(mcopy, type, code, dest.s_addr, mtu);
1698df8bae1dSRodney W. Grimes }
1699df8bae1dSRodney W. Grimes 
170082c23ebaSBill Fenner void
1701f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1702f2565d68SRobert Watson     struct mbuf *m)
170382c23ebaSBill Fenner {
17048b615593SMarko Zec 
1705be8a62e8SPoul-Henning Kamp 	if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1706be8a62e8SPoul-Henning Kamp 		struct bintime bt;
1707be8a62e8SPoul-Henning Kamp 
1708be8a62e8SPoul-Henning Kamp 		bintime(&bt);
1709be8a62e8SPoul-Henning Kamp 		if (inp->inp_socket->so_options & SO_BINTIME) {
1710be8a62e8SPoul-Henning Kamp 			*mp = sbcreatecontrol((caddr_t)&bt, sizeof(bt),
1711be8a62e8SPoul-Henning Kamp 			    SCM_BINTIME, SOL_SOCKET);
1712be8a62e8SPoul-Henning Kamp 			if (*mp)
1713be8a62e8SPoul-Henning Kamp 				mp = &(*mp)->m_next;
1714be8a62e8SPoul-Henning Kamp 		}
171582c23ebaSBill Fenner 		if (inp->inp_socket->so_options & SO_TIMESTAMP) {
171682c23ebaSBill Fenner 			struct timeval tv;
171782c23ebaSBill Fenner 
1718be8a62e8SPoul-Henning Kamp 			bintime2timeval(&bt, &tv);
171982c23ebaSBill Fenner 			*mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv),
172082c23ebaSBill Fenner 			    SCM_TIMESTAMP, SOL_SOCKET);
172182c23ebaSBill Fenner 			if (*mp)
172282c23ebaSBill Fenner 				mp = &(*mp)->m_next;
17234cc20ab1SSeigo Tanimura 		}
1724be8a62e8SPoul-Henning Kamp 	}
172582c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVDSTADDR) {
172682c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t)&ip->ip_dst,
172782c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
172882c23ebaSBill Fenner 		if (*mp)
172982c23ebaSBill Fenner 			mp = &(*mp)->m_next;
173082c23ebaSBill Fenner 	}
17314957466bSMatthew N. Dodd 	if (inp->inp_flags & INP_RECVTTL) {
17324957466bSMatthew N. Dodd 		*mp = sbcreatecontrol((caddr_t)&ip->ip_ttl,
17334957466bSMatthew N. Dodd 		    sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
17344957466bSMatthew N. Dodd 		if (*mp)
17354957466bSMatthew N. Dodd 			mp = &(*mp)->m_next;
17364957466bSMatthew N. Dodd 	}
173782c23ebaSBill Fenner #ifdef notyet
173882c23ebaSBill Fenner 	/* XXX
173982c23ebaSBill Fenner 	 * Moving these out of udp_input() made them even more broken
174082c23ebaSBill Fenner 	 * than they already were.
174182c23ebaSBill Fenner 	 */
174282c23ebaSBill Fenner 	/* options were tossed already */
174382c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVOPTS) {
174482c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t)opts_deleted_above,
174582c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
174682c23ebaSBill Fenner 		if (*mp)
174782c23ebaSBill Fenner 			mp = &(*mp)->m_next;
174882c23ebaSBill Fenner 	}
174982c23ebaSBill Fenner 	/* ip_srcroute doesn't do what we want here, need to fix */
175082c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVRETOPTS) {
1751e0982661SAndre Oppermann 		*mp = sbcreatecontrol((caddr_t)ip_srcroute(m),
175282c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
175382c23ebaSBill Fenner 		if (*mp)
175482c23ebaSBill Fenner 			mp = &(*mp)->m_next;
175582c23ebaSBill Fenner 	}
175682c23ebaSBill Fenner #endif
175782c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVIF) {
1758d314ad7bSJulian Elischer 		struct ifnet *ifp;
1759d314ad7bSJulian Elischer 		struct sdlbuf {
176082c23ebaSBill Fenner 			struct sockaddr_dl sdl;
1761d314ad7bSJulian Elischer 			u_char	pad[32];
1762d314ad7bSJulian Elischer 		} sdlbuf;
1763d314ad7bSJulian Elischer 		struct sockaddr_dl *sdp;
1764d314ad7bSJulian Elischer 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
176582c23ebaSBill Fenner 
176646f2df9cSSergey Kandaurov 		if ((ifp = m->m_pkthdr.rcvif) &&
176746f2df9cSSergey Kandaurov 		    ifp->if_index && ifp->if_index <= V_if_index) {
17684a0d6638SRuslan Ermilov 			sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1769d314ad7bSJulian Elischer 			/*
1770d314ad7bSJulian Elischer 			 * Change our mind and don't try copy.
1771d314ad7bSJulian Elischer 			 */
177246f2df9cSSergey Kandaurov 			if (sdp->sdl_family != AF_LINK ||
177346f2df9cSSergey Kandaurov 			    sdp->sdl_len > sizeof(sdlbuf)) {
1774d314ad7bSJulian Elischer 				goto makedummy;
1775d314ad7bSJulian Elischer 			}
1776d314ad7bSJulian Elischer 			bcopy(sdp, sdl2, sdp->sdl_len);
1777d314ad7bSJulian Elischer 		} else {
1778d314ad7bSJulian Elischer makedummy:
177946f2df9cSSergey Kandaurov 			sdl2->sdl_len =
178046f2df9cSSergey Kandaurov 			    offsetof(struct sockaddr_dl, sdl_data[0]);
1781d314ad7bSJulian Elischer 			sdl2->sdl_family = AF_LINK;
1782d314ad7bSJulian Elischer 			sdl2->sdl_index = 0;
1783d314ad7bSJulian Elischer 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1784d314ad7bSJulian Elischer 		}
1785d314ad7bSJulian Elischer 		*mp = sbcreatecontrol((caddr_t)sdl2, sdl2->sdl_len,
178682c23ebaSBill Fenner 		    IP_RECVIF, IPPROTO_IP);
178782c23ebaSBill Fenner 		if (*mp)
178882c23ebaSBill Fenner 			mp = &(*mp)->m_next;
178982c23ebaSBill Fenner 	}
17903cca425bSMichael Tuexen 	if (inp->inp_flags & INP_RECVTOS) {
17913cca425bSMichael Tuexen 		*mp = sbcreatecontrol((caddr_t)&ip->ip_tos,
17923cca425bSMichael Tuexen 		    sizeof(u_char), IP_RECVTOS, IPPROTO_IP);
17933cca425bSMichael Tuexen 		if (*mp)
17943cca425bSMichael Tuexen 			mp = &(*mp)->m_next;
17953cca425bSMichael Tuexen 	}
17969d3ddf43SAdrian Chadd 
17979d3ddf43SAdrian Chadd 	if (inp->inp_flags2 & INP_RECVFLOWID) {
17989d3ddf43SAdrian Chadd 		uint32_t flowid, flow_type;
17999d3ddf43SAdrian Chadd 
18009d3ddf43SAdrian Chadd 		flowid = m->m_pkthdr.flowid;
18019d3ddf43SAdrian Chadd 		flow_type = M_HASHTYPE_GET(m);
18029d3ddf43SAdrian Chadd 
18039d3ddf43SAdrian Chadd 		/*
18049d3ddf43SAdrian Chadd 		 * XXX should handle the failure of one or the
18059d3ddf43SAdrian Chadd 		 * other - don't populate both?
18069d3ddf43SAdrian Chadd 		 */
18079d3ddf43SAdrian Chadd 		*mp = sbcreatecontrol((caddr_t) &flowid,
18089d3ddf43SAdrian Chadd 		    sizeof(uint32_t), IP_FLOWID, IPPROTO_IP);
18099d3ddf43SAdrian Chadd 		if (*mp)
18109d3ddf43SAdrian Chadd 			mp = &(*mp)->m_next;
18119d3ddf43SAdrian Chadd 		*mp = sbcreatecontrol((caddr_t) &flow_type,
18129d3ddf43SAdrian Chadd 		    sizeof(uint32_t), IP_FLOWTYPE, IPPROTO_IP);
18139d3ddf43SAdrian Chadd 		if (*mp)
18149d3ddf43SAdrian Chadd 			mp = &(*mp)->m_next;
18159d3ddf43SAdrian Chadd 	}
18169d3ddf43SAdrian Chadd 
18179d3ddf43SAdrian Chadd #ifdef	RSS
18189d3ddf43SAdrian Chadd 	if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
18199d3ddf43SAdrian Chadd 		uint32_t flowid, flow_type;
18209d3ddf43SAdrian Chadd 		uint32_t rss_bucketid;
18219d3ddf43SAdrian Chadd 
18229d3ddf43SAdrian Chadd 		flowid = m->m_pkthdr.flowid;
18239d3ddf43SAdrian Chadd 		flow_type = M_HASHTYPE_GET(m);
18249d3ddf43SAdrian Chadd 
18259d3ddf43SAdrian Chadd 		if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
18269d3ddf43SAdrian Chadd 			*mp = sbcreatecontrol((caddr_t) &rss_bucketid,
18279d3ddf43SAdrian Chadd 			   sizeof(uint32_t), IP_RSSBUCKETID, IPPROTO_IP);
18289d3ddf43SAdrian Chadd 			if (*mp)
18299d3ddf43SAdrian Chadd 				mp = &(*mp)->m_next;
18309d3ddf43SAdrian Chadd 		}
18319d3ddf43SAdrian Chadd 	}
18329d3ddf43SAdrian Chadd #endif
183382c23ebaSBill Fenner }
183482c23ebaSBill Fenner 
18354d2e3692SLuigi Rizzo /*
183630916a2dSRobert Watson  * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
183730916a2dSRobert Watson  * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
183830916a2dSRobert Watson  * locking.  This code remains in ip_input.c as ip_mroute.c is optionally
183930916a2dSRobert Watson  * compiled.
18404d2e3692SLuigi Rizzo  */
18413e288e62SDimitry Andric static VNET_DEFINE(int, ip_rsvp_on);
184282cea7e6SBjoern A. Zeeb VNET_DEFINE(struct socket *, ip_rsvpd);
184382cea7e6SBjoern A. Zeeb 
184482cea7e6SBjoern A. Zeeb #define	V_ip_rsvp_on		VNET(ip_rsvp_on)
184582cea7e6SBjoern A. Zeeb 
1846df8bae1dSRodney W. Grimes int
1847f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so)
1848f0068c4aSGarrett Wollman {
18498b615593SMarko Zec 
1850f0068c4aSGarrett Wollman 	if (so->so_type != SOCK_RAW ||
1851f0068c4aSGarrett Wollman 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1852f0068c4aSGarrett Wollman 		return EOPNOTSUPP;
1853f0068c4aSGarrett Wollman 
1854603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL)
1855f0068c4aSGarrett Wollman 		return EADDRINUSE;
1856f0068c4aSGarrett Wollman 
1857603724d3SBjoern A. Zeeb 	V_ip_rsvpd = so;
18581c5de19aSGarrett Wollman 	/*
18591c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-increment
18601c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
18611c5de19aSGarrett Wollman 	 */
1862603724d3SBjoern A. Zeeb 	if (!V_ip_rsvp_on) {
1863603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 1;
1864603724d3SBjoern A. Zeeb 		V_rsvp_on++;
18651c5de19aSGarrett Wollman 	}
1866f0068c4aSGarrett Wollman 
1867f0068c4aSGarrett Wollman 	return 0;
1868f0068c4aSGarrett Wollman }
1869f0068c4aSGarrett Wollman 
1870f0068c4aSGarrett Wollman int
1871f0068c4aSGarrett Wollman ip_rsvp_done(void)
1872f0068c4aSGarrett Wollman {
18738b615593SMarko Zec 
1874603724d3SBjoern A. Zeeb 	V_ip_rsvpd = NULL;
18751c5de19aSGarrett Wollman 	/*
18761c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-decrement
18771c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
18781c5de19aSGarrett Wollman 	 */
1879603724d3SBjoern A. Zeeb 	if (V_ip_rsvp_on) {
1880603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 0;
1881603724d3SBjoern A. Zeeb 		V_rsvp_on--;
18821c5de19aSGarrett Wollman 	}
1883f0068c4aSGarrett Wollman 	return 0;
1884f0068c4aSGarrett Wollman }
1885bbb4330bSLuigi Rizzo 
18868f5a8818SKevin Lo int
18878f5a8818SKevin Lo rsvp_input(struct mbuf **mp, int *offp, int proto)
1888bbb4330bSLuigi Rizzo {
18898f5a8818SKevin Lo 	struct mbuf *m;
18908f5a8818SKevin Lo 
18918f5a8818SKevin Lo 	m = *mp;
18928f5a8818SKevin Lo 	*mp = NULL;
18938b615593SMarko Zec 
1894bbb4330bSLuigi Rizzo 	if (rsvp_input_p) { /* call the real one if loaded */
18958f5a8818SKevin Lo 		*mp = m;
18968f5a8818SKevin Lo 		rsvp_input_p(mp, offp, proto);
18978f5a8818SKevin Lo 		return (IPPROTO_DONE);
1898bbb4330bSLuigi Rizzo 	}
1899bbb4330bSLuigi Rizzo 
1900bbb4330bSLuigi Rizzo 	/* Can still get packets with rsvp_on = 0 if there is a local member
1901bbb4330bSLuigi Rizzo 	 * of the group to which the RSVP packet is addressed.  But in this
1902bbb4330bSLuigi Rizzo 	 * case we want to throw the packet away.
1903bbb4330bSLuigi Rizzo 	 */
1904bbb4330bSLuigi Rizzo 
1905603724d3SBjoern A. Zeeb 	if (!V_rsvp_on) {
1906bbb4330bSLuigi Rizzo 		m_freem(m);
19078f5a8818SKevin Lo 		return (IPPROTO_DONE);
1908bbb4330bSLuigi Rizzo 	}
1909bbb4330bSLuigi Rizzo 
1910603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL) {
19118f5a8818SKevin Lo 		*mp = m;
19128f5a8818SKevin Lo 		rip_input(mp, offp, proto);
19138f5a8818SKevin Lo 		return (IPPROTO_DONE);
1914bbb4330bSLuigi Rizzo 	}
1915bbb4330bSLuigi Rizzo 	/* Drop the packet */
1916bbb4330bSLuigi Rizzo 	m_freem(m);
19178f5a8818SKevin Lo 	return (IPPROTO_DONE);
1918bbb4330bSLuigi Rizzo }
1919