xref: /freebsd/sys/netinet/ip_input.c (revision 8922ddbe40e98fae2ddbeebb10ea64b4da8e03bc)
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"
40b8bc95cdSAdrian 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 */
81b8bc95cdSAdrian 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);
976df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_VNET | 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)
1036df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_VNET | CTLFLAG_RW,
104eddfbb76SRobert Watson     &VNET_NAME(ipsendredirects), 0,
1058b615593SMarko Zec     "Enable sending IP redirects");
1060312fbe9SPoul-Henning Kamp 
10782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip_do_randomid);
1086df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_VNET | CTLFLAG_RW,
109eddfbb76SRobert Watson     &VNET_NAME(ip_do_randomid), 0,
110eddfbb76SRobert Watson     "Assign random ip_id values");
1111f44b0a1SDavid Malone 
112823db0e9SDon Lewis /*
113823db0e9SDon Lewis  * XXX - Setting ip_checkinterface mostly implements the receive side of
114823db0e9SDon Lewis  * the Strong ES model described in RFC 1122, but since the routing table
115a8f12100SDon Lewis  * and transmit implementation do not implement the Strong ES model,
116823db0e9SDon Lewis  * setting this to 1 results in an odd hybrid.
1173f67c834SDon Lewis  *
118a8f12100SDon Lewis  * XXX - ip_checkinterface currently must be disabled if you use ipnat
119a8f12100SDon Lewis  * to translate the destination address to another local interface.
1203f67c834SDon Lewis  *
1213f67c834SDon Lewis  * XXX - ip_checkinterface must be disabled if you add IP aliases
1223f67c834SDon Lewis  * to the loopback interface instead of the interface where the
1233f67c834SDon Lewis  * packets for those addresses are received.
124823db0e9SDon Lewis  */
1253e288e62SDimitry Andric static VNET_DEFINE(int, ip_checkinterface);
12682cea7e6SBjoern A. Zeeb #define	V_ip_checkinterface	VNET(ip_checkinterface)
1276df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_VNET | CTLFLAG_RW,
128eddfbb76SRobert Watson     &VNET_NAME(ip_checkinterface), 0,
1298b615593SMarko Zec     "Verify packet arrives on correct interface");
130b3e95d4eSJonathan Lemon 
1310b4b0b0fSJulian Elischer VNET_DEFINE(struct pfil_head, inet_pfil_hook);	/* Packet filter hooks */
132df8bae1dSRodney W. Grimes 
133d4b5cae4SRobert Watson static struct netisr_handler ip_nh = {
134d4b5cae4SRobert Watson 	.nh_name = "ip",
135d4b5cae4SRobert Watson 	.nh_handler = ip_input,
136d4b5cae4SRobert Watson 	.nh_proto = NETISR_IP,
137b8bc95cdSAdrian Chadd #ifdef	RSS
138b8bc95cdSAdrian Chadd 	.nh_m2cpuid = rss_soft_m2cpuid,
139b8bc95cdSAdrian Chadd 	.nh_policy = NETISR_POLICY_CPU,
140b8bc95cdSAdrian Chadd 	.nh_dispatch = NETISR_DISPATCH_HYBRID,
141b8bc95cdSAdrian Chadd #else
142d4b5cae4SRobert Watson 	.nh_policy = NETISR_POLICY_FLOW,
143b8bc95cdSAdrian Chadd #endif
144d4b5cae4SRobert Watson };
145ca925d9cSJonathan Lemon 
146b8bc95cdSAdrian Chadd #ifdef	RSS
147b8bc95cdSAdrian Chadd /*
148b8bc95cdSAdrian Chadd  * Directly dispatched frames are currently assumed
149b8bc95cdSAdrian Chadd  * to have a flowid already calculated.
150b8bc95cdSAdrian Chadd  *
151b8bc95cdSAdrian Chadd  * It should likely have something that assert it
152b8bc95cdSAdrian Chadd  * actually has valid flow details.
153b8bc95cdSAdrian Chadd  */
154b8bc95cdSAdrian Chadd static struct netisr_handler ip_direct_nh = {
155b8bc95cdSAdrian Chadd 	.nh_name = "ip_direct",
156b8bc95cdSAdrian Chadd 	.nh_handler = ip_direct_input,
157b8bc95cdSAdrian Chadd 	.nh_proto = NETISR_IP_DIRECT,
158b8bc95cdSAdrian Chadd 	.nh_m2cpuid = rss_m2cpuid,
159b8bc95cdSAdrian Chadd 	.nh_policy = NETISR_POLICY_CPU,
160b8bc95cdSAdrian Chadd 	.nh_dispatch = NETISR_DISPATCH_HYBRID,
161b8bc95cdSAdrian Chadd };
162b8bc95cdSAdrian Chadd #endif
163b8bc95cdSAdrian Chadd 
164df8bae1dSRodney W. Grimes extern	struct domain inetdomain;
165f0ffb944SJulian Elischer extern	struct protosw inetsw[];
166df8bae1dSRodney W. Grimes u_char	ip_protox[IPPROTO_MAX];
16782cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead);  /* first inet address */
16882cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); /* inet addr hash table  */
16982cea7e6SBjoern A. Zeeb VNET_DEFINE(u_long, in_ifaddrhmask);		/* mask for hash table */
170ca925d9cSJonathan Lemon 
1713e288e62SDimitry Andric static VNET_DEFINE(uma_zone_t, ipq_zone);
1723e288e62SDimitry Andric static VNET_DEFINE(TAILQ_HEAD(ipqhead, ipq), ipq[IPREASS_NHASH]);
173f89d4c3aSAndre Oppermann static struct mtx ipqlock;
1742fad1e93SSam Leffler 
17582cea7e6SBjoern A. Zeeb #define	V_ipq_zone		VNET(ipq_zone)
17682cea7e6SBjoern A. Zeeb #define	V_ipq			VNET(ipq)
17782cea7e6SBjoern A. Zeeb 
1782fad1e93SSam Leffler #define	IPQ_LOCK()	mtx_lock(&ipqlock)
1792fad1e93SSam Leffler #define	IPQ_UNLOCK()	mtx_unlock(&ipqlock)
180888c2a3cSSam Leffler #define	IPQ_LOCK_INIT()	mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
181888c2a3cSSam Leffler #define	IPQ_LOCK_ASSERT()	mtx_assert(&ipqlock, MA_OWNED)
182f23b4c91SGarrett Wollman 
183d248c7d7SRobert Watson static void	maxnipq_update(void);
1844f590175SPaul Saab static void	ipq_zone_change(void *);
1859802380eSBjoern A. Zeeb static void	ip_drain_locked(void);
186d248c7d7SRobert Watson 
1873e288e62SDimitry Andric static VNET_DEFINE(int, maxnipq);  /* Administrative limit on # reass queues. */
1883e288e62SDimitry Andric static VNET_DEFINE(int, nipq);			/* Total # of reass queues */
18982cea7e6SBjoern A. Zeeb #define	V_maxnipq		VNET(maxnipq)
19082cea7e6SBjoern A. Zeeb #define	V_nipq			VNET(nipq)
1916df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_VNET | CTLFLAG_RD,
192eddfbb76SRobert Watson     &VNET_NAME(nipq), 0,
1938b615593SMarko Zec     "Current number of IPv4 fragment reassembly queue entries");
194d248c7d7SRobert Watson 
1953e288e62SDimitry Andric static VNET_DEFINE(int, maxfragsperpacket);
19682cea7e6SBjoern A. Zeeb #define	V_maxfragsperpacket	VNET(maxfragsperpacket)
1976df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_VNET | CTLFLAG_RW,
198eddfbb76SRobert Watson     &VNET_NAME(maxfragsperpacket), 0,
199d248c7d7SRobert Watson     "Maximum number of IPv4 fragments allowed per packet");
200d248c7d7SRobert Watson 
2010312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU
2020312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
2033d177f46SBill Fumerola     &ip_mtu, 0, "Default MTU");
2040312fbe9SPoul-Henning Kamp #endif
2050312fbe9SPoul-Henning Kamp 
2061b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
20782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ipstealth);
2086df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_VNET | CTLFLAG_RW,
209eddfbb76SRobert Watson     &VNET_NAME(ipstealth), 0,
210eddfbb76SRobert Watson     "IP stealth mode, no TTL decrementation on forwarding");
2111b968362SDag-Erling Smørgrav #endif
212eddfbb76SRobert Watson 
2134d77a549SAlfred Perlstein static void	ip_freef(struct ipqhead *, struct ipq *);
2148948e4baSArchie Cobbs 
215315e3e38SRobert Watson /*
2165da0521fSAndrey V. Elsukov  * IP statistics are stored in the "array" of counter(9)s.
2175923c293SGleb Smirnoff  */
2185da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct ipstat, ipstat);
2195da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(ipstat);
2205da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_ip, IPCTL_STATS, stats, struct ipstat, ipstat,
2215da0521fSAndrey V. Elsukov     "IP statistics (struct ipstat, netinet/ip_var.h)");
2225923c293SGleb Smirnoff 
2235923c293SGleb Smirnoff #ifdef VIMAGE
2245da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(ipstat);
2255923c293SGleb Smirnoff #endif /* VIMAGE */
2265923c293SGleb Smirnoff 
2275923c293SGleb Smirnoff /*
228315e3e38SRobert Watson  * Kernel module interface for updating ipstat.  The argument is an index
2295923c293SGleb Smirnoff  * into ipstat treated as an array.
230315e3e38SRobert Watson  */
231315e3e38SRobert Watson void
232315e3e38SRobert Watson kmod_ipstat_inc(int statnum)
233315e3e38SRobert Watson {
234315e3e38SRobert Watson 
2355da0521fSAndrey V. Elsukov 	counter_u64_add(VNET(ipstat)[statnum], 1);
236315e3e38SRobert Watson }
237315e3e38SRobert Watson 
238315e3e38SRobert Watson void
239315e3e38SRobert Watson kmod_ipstat_dec(int statnum)
240315e3e38SRobert Watson {
241315e3e38SRobert Watson 
2425da0521fSAndrey V. Elsukov 	counter_u64_add(VNET(ipstat)[statnum], -1);
243315e3e38SRobert Watson }
244315e3e38SRobert Watson 
245d4b5cae4SRobert Watson static int
246d4b5cae4SRobert Watson sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
247d4b5cae4SRobert Watson {
248d4b5cae4SRobert Watson 	int error, qlimit;
249d4b5cae4SRobert Watson 
250d4b5cae4SRobert Watson 	netisr_getqlimit(&ip_nh, &qlimit);
251d4b5cae4SRobert Watson 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
252d4b5cae4SRobert Watson 	if (error || !req->newptr)
253d4b5cae4SRobert Watson 		return (error);
254d4b5cae4SRobert Watson 	if (qlimit < 1)
255d4b5cae4SRobert Watson 		return (EINVAL);
256d4b5cae4SRobert Watson 	return (netisr_setqlimit(&ip_nh, qlimit));
257d4b5cae4SRobert Watson }
258d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen,
259d4b5cae4SRobert Watson     CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_queue_maxlen, "I",
260d4b5cae4SRobert Watson     "Maximum size of the IP input queue");
261d4b5cae4SRobert Watson 
262d4b5cae4SRobert Watson static int
263d4b5cae4SRobert Watson sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS)
264d4b5cae4SRobert Watson {
265d4b5cae4SRobert Watson 	u_int64_t qdrops_long;
266d4b5cae4SRobert Watson 	int error, qdrops;
267d4b5cae4SRobert Watson 
268d4b5cae4SRobert Watson 	netisr_getqdrops(&ip_nh, &qdrops_long);
269d4b5cae4SRobert Watson 	qdrops = qdrops_long;
270d4b5cae4SRobert Watson 	error = sysctl_handle_int(oidp, &qdrops, 0, req);
271d4b5cae4SRobert Watson 	if (error || !req->newptr)
272d4b5cae4SRobert Watson 		return (error);
273d4b5cae4SRobert Watson 	if (qdrops != 0)
274d4b5cae4SRobert Watson 		return (EINVAL);
275d4b5cae4SRobert Watson 	netisr_clearqdrops(&ip_nh);
276d4b5cae4SRobert Watson 	return (0);
277d4b5cae4SRobert Watson }
278d4b5cae4SRobert Watson 
279d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops,
280d4b5cae4SRobert Watson     CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_queue_drops, "I",
281d4b5cae4SRobert Watson     "Number of packets dropped from the IP input queue");
282d4b5cae4SRobert Watson 
283b8bc95cdSAdrian Chadd #ifdef	RSS
284b8bc95cdSAdrian Chadd static int
285b8bc95cdSAdrian Chadd sysctl_netinet_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
286b8bc95cdSAdrian Chadd {
287b8bc95cdSAdrian Chadd 	int error, qlimit;
288b8bc95cdSAdrian Chadd 
289b8bc95cdSAdrian Chadd 	netisr_getqlimit(&ip_direct_nh, &qlimit);
290b8bc95cdSAdrian Chadd 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
291b8bc95cdSAdrian Chadd 	if (error || !req->newptr)
292b8bc95cdSAdrian Chadd 		return (error);
293b8bc95cdSAdrian Chadd 	if (qlimit < 1)
294b8bc95cdSAdrian Chadd 		return (EINVAL);
295b8bc95cdSAdrian Chadd 	return (netisr_setqlimit(&ip_direct_nh, qlimit));
296b8bc95cdSAdrian Chadd }
297b8bc95cdSAdrian Chadd SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_direct_queue_maxlen,
298b8bc95cdSAdrian Chadd     CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_direct_queue_maxlen, "I",
299b8bc95cdSAdrian Chadd     "Maximum size of the IP direct input queue");
300b8bc95cdSAdrian Chadd 
301b8bc95cdSAdrian Chadd static int
302b8bc95cdSAdrian Chadd sysctl_netinet_intr_direct_queue_drops(SYSCTL_HANDLER_ARGS)
303b8bc95cdSAdrian Chadd {
304b8bc95cdSAdrian Chadd 	u_int64_t qdrops_long;
305b8bc95cdSAdrian Chadd 	int error, qdrops;
306b8bc95cdSAdrian Chadd 
307b8bc95cdSAdrian Chadd 	netisr_getqdrops(&ip_direct_nh, &qdrops_long);
308b8bc95cdSAdrian Chadd 	qdrops = qdrops_long;
309b8bc95cdSAdrian Chadd 	error = sysctl_handle_int(oidp, &qdrops, 0, req);
310b8bc95cdSAdrian Chadd 	if (error || !req->newptr)
311b8bc95cdSAdrian Chadd 		return (error);
312b8bc95cdSAdrian Chadd 	if (qdrops != 0)
313b8bc95cdSAdrian Chadd 		return (EINVAL);
314b8bc95cdSAdrian Chadd 	netisr_clearqdrops(&ip_direct_nh);
315b8bc95cdSAdrian Chadd 	return (0);
316b8bc95cdSAdrian Chadd }
317b8bc95cdSAdrian Chadd 
318b8bc95cdSAdrian Chadd SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_direct_queue_drops,
319b8bc95cdSAdrian Chadd     CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_direct_queue_drops, "I",
320b8bc95cdSAdrian Chadd     "Number of packets dropped from the IP direct input queue");
321b8bc95cdSAdrian Chadd #endif	/* RSS */
322b8bc95cdSAdrian Chadd 
323df8bae1dSRodney W. Grimes /*
324df8bae1dSRodney W. Grimes  * IP initialization: fill in IP protocol switch table.
325df8bae1dSRodney W. Grimes  * All protocols not implemented in kernel go to raw IP protocol handler.
326df8bae1dSRodney W. Grimes  */
327df8bae1dSRodney W. Grimes void
328f2565d68SRobert Watson ip_init(void)
329df8bae1dSRodney W. Grimes {
330f2565d68SRobert Watson 	struct protosw *pr;
331f2565d68SRobert Watson 	int i;
332df8bae1dSRodney W. Grimes 
333a511354aSRobert Watson 	V_ip_id = time_second & 0xffff;
334a511354aSRobert Watson 
335603724d3SBjoern A. Zeeb 	TAILQ_INIT(&V_in_ifaddrhead);
336603724d3SBjoern A. Zeeb 	V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
3371ed81b73SMarko Zec 
3381ed81b73SMarko Zec 	/* Initialize IP reassembly queue. */
3391ed81b73SMarko Zec 	for (i = 0; i < IPREASS_NHASH; i++)
3401ed81b73SMarko Zec 		TAILQ_INIT(&V_ipq[i]);
3411ed81b73SMarko Zec 	V_maxnipq = nmbclusters / 32;
3421ed81b73SMarko Zec 	V_maxfragsperpacket = 16;
3431ed81b73SMarko Zec 	V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
3441ed81b73SMarko Zec 	    NULL, UMA_ALIGN_PTR, 0);
3451ed81b73SMarko Zec 	maxnipq_update();
3461ed81b73SMarko Zec 
3470b4b0b0fSJulian Elischer 	/* Initialize packet filter hooks. */
3480b4b0b0fSJulian Elischer 	V_inet_pfil_hook.ph_type = PFIL_TYPE_AF;
3490b4b0b0fSJulian Elischer 	V_inet_pfil_hook.ph_af = AF_INET;
3500b4b0b0fSJulian Elischer 	if ((i = pfil_head_register(&V_inet_pfil_hook)) != 0)
3510b4b0b0fSJulian Elischer 		printf("%s: WARNING: unable to register pfil hook, "
3520b4b0b0fSJulian Elischer 			"error %d\n", __func__, i);
3530b4b0b0fSJulian Elischer 
3541ed81b73SMarko Zec 	/* Skip initialization of globals for non-default instances. */
3551ed81b73SMarko Zec 	if (!IS_DEFAULT_VNET(curvnet))
3561ed81b73SMarko Zec 		return;
3571ed81b73SMarko Zec 
358f0ffb944SJulian Elischer 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
35902410549SRobert Watson 	if (pr == NULL)
360db09bef3SAndre Oppermann 		panic("ip_init: PF_INET not found");
361db09bef3SAndre Oppermann 
362db09bef3SAndre Oppermann 	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
363df8bae1dSRodney W. Grimes 	for (i = 0; i < IPPROTO_MAX; i++)
364df8bae1dSRodney W. Grimes 		ip_protox[i] = pr - inetsw;
365db09bef3SAndre Oppermann 	/*
366db09bef3SAndre Oppermann 	 * Cycle through IP protocols and put them into the appropriate place
367db09bef3SAndre Oppermann 	 * in ip_protox[].
368db09bef3SAndre Oppermann 	 */
369f0ffb944SJulian Elischer 	for (pr = inetdomain.dom_protosw;
370f0ffb944SJulian Elischer 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
371df8bae1dSRodney W. Grimes 		if (pr->pr_domain->dom_family == PF_INET &&
372db09bef3SAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
373db09bef3SAndre Oppermann 			/* Be careful to only index valid IP protocols. */
374db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX)
375df8bae1dSRodney W. Grimes 				ip_protox[pr->pr_protocol] = pr - inetsw;
376db09bef3SAndre Oppermann 		}
377194a213eSAndrey A. Chernov 
3784f590175SPaul Saab 	EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
3794f590175SPaul Saab 		NULL, EVENTHANDLER_PRI_ANY);
3805f311da2SMike Silbersack 
381db09bef3SAndre Oppermann 	/* Initialize various other remaining things. */
3821ed81b73SMarko Zec 	IPQ_LOCK_INIT();
383d4b5cae4SRobert Watson 	netisr_register(&ip_nh);
384b8bc95cdSAdrian Chadd #ifdef	RSS
385b8bc95cdSAdrian Chadd 	netisr_register(&ip_direct_nh);
386b8bc95cdSAdrian Chadd #endif
387df8bae1dSRodney W. Grimes }
388df8bae1dSRodney W. Grimes 
3899802380eSBjoern A. Zeeb #ifdef VIMAGE
3909802380eSBjoern A. Zeeb void
3919802380eSBjoern A. Zeeb ip_destroy(void)
3929802380eSBjoern A. Zeeb {
3934d3dfd45SMikolaj Golub 	int i;
3944d3dfd45SMikolaj Golub 
3954d3dfd45SMikolaj Golub 	if ((i = pfil_head_unregister(&V_inet_pfil_hook)) != 0)
3964d3dfd45SMikolaj Golub 		printf("%s: WARNING: unable to unregister pfil hook, "
3974d3dfd45SMikolaj Golub 		    "error %d\n", __func__, i);
3989802380eSBjoern A. Zeeb 
3999802380eSBjoern A. Zeeb 	/* Cleanup in_ifaddr hash table; should be empty. */
4009802380eSBjoern A. Zeeb 	hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask);
4019802380eSBjoern A. Zeeb 
4029802380eSBjoern A. Zeeb 	IPQ_LOCK();
4039802380eSBjoern A. Zeeb 	ip_drain_locked();
4049802380eSBjoern A. Zeeb 	IPQ_UNLOCK();
4059802380eSBjoern A. Zeeb 
4069802380eSBjoern A. Zeeb 	uma_zdestroy(V_ipq_zone);
4079802380eSBjoern A. Zeeb }
4089802380eSBjoern A. Zeeb #endif
4099802380eSBjoern A. Zeeb 
410b8bc95cdSAdrian Chadd #ifdef	RSS
411b8bc95cdSAdrian Chadd /*
412b8bc95cdSAdrian Chadd  * IP direct input routine.
413b8bc95cdSAdrian Chadd  *
414b8bc95cdSAdrian Chadd  * This is called when reinjecting completed fragments where
415b8bc95cdSAdrian Chadd  * all of the previous checking and book-keeping has been done.
416b8bc95cdSAdrian Chadd  */
417b8bc95cdSAdrian Chadd void
418b8bc95cdSAdrian Chadd ip_direct_input(struct mbuf *m)
419b8bc95cdSAdrian Chadd {
420b8bc95cdSAdrian Chadd 	struct ip *ip;
421b8bc95cdSAdrian Chadd 	int hlen;
422b8bc95cdSAdrian Chadd 
423b8bc95cdSAdrian Chadd 	ip = mtod(m, struct ip *);
424b8bc95cdSAdrian Chadd 	hlen = ip->ip_hl << 2;
425b8bc95cdSAdrian Chadd 
426b8bc95cdSAdrian Chadd 	IPSTAT_INC(ips_delivered);
427b8bc95cdSAdrian Chadd 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(&m, &hlen, ip->ip_p);
428b8bc95cdSAdrian Chadd 	return;
429b8bc95cdSAdrian Chadd }
430b8bc95cdSAdrian Chadd #endif
431b8bc95cdSAdrian Chadd 
4324d2e3692SLuigi Rizzo /*
433df8bae1dSRodney W. Grimes  * Ip input routine.  Checksum and byte swap header.  If fragmented
434df8bae1dSRodney W. Grimes  * try to reassemble.  Process options.  Pass to next level.
435df8bae1dSRodney W. Grimes  */
436c67b1d17SGarrett Wollman void
437c67b1d17SGarrett Wollman ip_input(struct mbuf *m)
438df8bae1dSRodney W. Grimes {
4399188b4a1SAndre Oppermann 	struct ip *ip = NULL;
4405da9f8faSJosef Karthauser 	struct in_ifaddr *ia = NULL;
441ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
4420aade26eSRobert Watson 	struct ifnet *ifp;
4439b932e9eSAndre Oppermann 	int    checkif, hlen = 0;
44421d172a3SGleb Smirnoff 	uint16_t sum, ip_len;
44502c1c707SAndre Oppermann 	int dchg = 0;				/* dest changed after fw */
446f51f805fSSam Leffler 	struct in_addr odst;			/* original dst address */
447b715f178SLuigi Rizzo 
448fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
449db40007dSAndrew R. Reiter 
450ac9d7e26SMax Laier 	if (m->m_flags & M_FASTFWD_OURS) {
45176ff6dcfSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
45276ff6dcfSAndre Oppermann 		/* Set up some basics that will be used later. */
4532b25acc1SLuigi Rizzo 		ip = mtod(m, struct ip *);
45453be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
4558f134647SGleb Smirnoff 		ip_len = ntohs(ip->ip_len);
4569b932e9eSAndre Oppermann 		goto ours;
4572b25acc1SLuigi Rizzo 	}
4582b25acc1SLuigi Rizzo 
45986425c62SRobert Watson 	IPSTAT_INC(ips_total);
46058938916SGarrett Wollman 
46158938916SGarrett Wollman 	if (m->m_pkthdr.len < sizeof(struct ip))
46258938916SGarrett Wollman 		goto tooshort;
46358938916SGarrett Wollman 
464df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct ip) &&
4650b17fba7SAndre Oppermann 	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
46686425c62SRobert Watson 		IPSTAT_INC(ips_toosmall);
467c67b1d17SGarrett Wollman 		return;
468df8bae1dSRodney W. Grimes 	}
469df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
47058938916SGarrett Wollman 
47153be11f6SPoul-Henning Kamp 	if (ip->ip_v != IPVERSION) {
47286425c62SRobert Watson 		IPSTAT_INC(ips_badvers);
473df8bae1dSRodney W. Grimes 		goto bad;
474df8bae1dSRodney W. Grimes 	}
47558938916SGarrett Wollman 
47653be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
477df8bae1dSRodney W. Grimes 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
47886425c62SRobert Watson 		IPSTAT_INC(ips_badhlen);
479df8bae1dSRodney W. Grimes 		goto bad;
480df8bae1dSRodney W. Grimes 	}
481df8bae1dSRodney W. Grimes 	if (hlen > m->m_len) {
4820b17fba7SAndre Oppermann 		if ((m = m_pullup(m, hlen)) == NULL) {
48386425c62SRobert Watson 			IPSTAT_INC(ips_badhlen);
484c67b1d17SGarrett Wollman 			return;
485df8bae1dSRodney W. Grimes 		}
486df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
487df8bae1dSRodney W. Grimes 	}
48833841545SHajimu UMEMOTO 
48957f60867SMark Johnston 	IP_PROBE(receive, NULL, NULL, ip, m->m_pkthdr.rcvif, ip, NULL);
49057f60867SMark Johnston 
49133841545SHajimu UMEMOTO 	/* 127/8 must not appear on wire - RFC1122 */
4920aade26eSRobert Watson 	ifp = m->m_pkthdr.rcvif;
49333841545SHajimu UMEMOTO 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
49433841545SHajimu UMEMOTO 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
4950aade26eSRobert Watson 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
49686425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
49733841545SHajimu UMEMOTO 			goto bad;
49833841545SHajimu UMEMOTO 		}
49933841545SHajimu UMEMOTO 	}
50033841545SHajimu UMEMOTO 
501db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
502db4f9cc7SJonathan Lemon 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
503db4f9cc7SJonathan Lemon 	} else {
50458938916SGarrett Wollman 		if (hlen == sizeof(struct ip)) {
50547c861ecSBrian Somers 			sum = in_cksum_hdr(ip);
50658938916SGarrett Wollman 		} else {
50747c861ecSBrian Somers 			sum = in_cksum(m, hlen);
50858938916SGarrett Wollman 		}
509db4f9cc7SJonathan Lemon 	}
51047c861ecSBrian Somers 	if (sum) {
51186425c62SRobert Watson 		IPSTAT_INC(ips_badsum);
512df8bae1dSRodney W. Grimes 		goto bad;
513df8bae1dSRodney W. Grimes 	}
514df8bae1dSRodney W. Grimes 
51502b199f1SMax Laier #ifdef ALTQ
51602b199f1SMax Laier 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
51702b199f1SMax Laier 		/* packet is dropped by traffic conditioner */
51802b199f1SMax Laier 		return;
51902b199f1SMax Laier #endif
52002b199f1SMax Laier 
52121d172a3SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
52221d172a3SGleb Smirnoff 	if (ip_len < hlen) {
52386425c62SRobert Watson 		IPSTAT_INC(ips_badlen);
524df8bae1dSRodney W. Grimes 		goto bad;
525df8bae1dSRodney W. Grimes 	}
526df8bae1dSRodney W. Grimes 
527df8bae1dSRodney W. Grimes 	/*
528df8bae1dSRodney W. Grimes 	 * Check that the amount of data in the buffers
529df8bae1dSRodney W. Grimes 	 * is as at least much as the IP header would have us expect.
530df8bae1dSRodney W. Grimes 	 * Trim mbufs if longer than we expect.
531df8bae1dSRodney W. Grimes 	 * Drop packet if shorter than we expect.
532df8bae1dSRodney W. Grimes 	 */
53321d172a3SGleb Smirnoff 	if (m->m_pkthdr.len < ip_len) {
53458938916SGarrett Wollman tooshort:
53586425c62SRobert Watson 		IPSTAT_INC(ips_tooshort);
536df8bae1dSRodney W. Grimes 		goto bad;
537df8bae1dSRodney W. Grimes 	}
53821d172a3SGleb Smirnoff 	if (m->m_pkthdr.len > ip_len) {
539df8bae1dSRodney W. Grimes 		if (m->m_len == m->m_pkthdr.len) {
54021d172a3SGleb Smirnoff 			m->m_len = ip_len;
54121d172a3SGleb Smirnoff 			m->m_pkthdr.len = ip_len;
542df8bae1dSRodney W. Grimes 		} else
54321d172a3SGleb Smirnoff 			m_adj(m, ip_len - m->m_pkthdr.len);
544df8bae1dSRodney W. Grimes 	}
545b8bc95cdSAdrian Chadd 
546b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
54714dd6717SSam Leffler 	/*
548ffe8cd7bSBjoern A. Zeeb 	 * Bypass packet filtering for packets previously handled by IPsec.
54914dd6717SSam Leffler 	 */
550cc977adcSBjoern A. Zeeb 	if (ip_ipsec_filtertunnel(m))
551c21fd232SAndre Oppermann 		goto passin;
552b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
5533f67c834SDon Lewis 
554c4ac87eaSDarren Reed 	/*
555134ea224SSam Leffler 	 * Run through list of hooks for input packets.
556f51f805fSSam Leffler 	 *
557f51f805fSSam Leffler 	 * NB: Beware of the destination address changing (e.g.
558f51f805fSSam Leffler 	 *     by NAT rewriting).  When this happens, tell
559f51f805fSSam Leffler 	 *     ip_forward to do the right thing.
560c4ac87eaSDarren Reed 	 */
561c21fd232SAndre Oppermann 
562c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
5630b4b0b0fSJulian Elischer 	if (!PFIL_HOOKED(&V_inet_pfil_hook))
564c21fd232SAndre Oppermann 		goto passin;
565c21fd232SAndre Oppermann 
566f51f805fSSam Leffler 	odst = ip->ip_dst;
5670b4b0b0fSJulian Elischer 	if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0)
568beec8214SDarren Reed 		return;
569134ea224SSam Leffler 	if (m == NULL)			/* consumed by filter */
570c4ac87eaSDarren Reed 		return;
5719b932e9eSAndre Oppermann 
572c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
57302c1c707SAndre Oppermann 	dchg = (odst.s_addr != ip->ip_dst.s_addr);
5740aade26eSRobert Watson 	ifp = m->m_pkthdr.rcvif;
5759b932e9eSAndre Oppermann 
5769b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
5779b932e9eSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
5789b932e9eSAndre Oppermann 		goto ours;
5799b932e9eSAndre Oppermann 	}
580ffdbf9daSAndrey V. Elsukov 	if (m->m_flags & M_IP_NEXTHOP) {
581ffdbf9daSAndrey V. Elsukov 		dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL);
582ffdbf9daSAndrey V. Elsukov 		if (dchg != 0) {
583099dd043SAndre Oppermann 			/*
584ffdbf9daSAndrey V. Elsukov 			 * Directly ship the packet on.  This allows
585ffdbf9daSAndrey V. Elsukov 			 * forwarding packets originally destined to us
586ffdbf9daSAndrey V. Elsukov 			 * to some other directly connected host.
587099dd043SAndre Oppermann 			 */
588ffdbf9daSAndrey V. Elsukov 			ip_forward(m, 1);
589099dd043SAndre Oppermann 			return;
590099dd043SAndre Oppermann 		}
591ffdbf9daSAndrey V. Elsukov 	}
592c21fd232SAndre Oppermann passin:
59321d172a3SGleb Smirnoff 
59421d172a3SGleb Smirnoff 	/*
595df8bae1dSRodney W. Grimes 	 * Process options and, if not destined for us,
596df8bae1dSRodney W. Grimes 	 * ship it on.  ip_dooptions returns 1 when an
597df8bae1dSRodney W. Grimes 	 * error was detected (causing an icmp message
598df8bae1dSRodney W. Grimes 	 * to be sent and the original packet to be freed).
599df8bae1dSRodney W. Grimes 	 */
6009b932e9eSAndre Oppermann 	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
601c67b1d17SGarrett Wollman 		return;
602df8bae1dSRodney W. Grimes 
603f0068c4aSGarrett Wollman         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
604f0068c4aSGarrett Wollman          * matter if it is destined to another node, or whether it is
605f0068c4aSGarrett Wollman          * a multicast one, RSVP wants it! and prevents it from being forwarded
606f0068c4aSGarrett Wollman          * anywhere else. Also checks if the rsvp daemon is running before
607f0068c4aSGarrett Wollman 	 * grabbing the packet.
608f0068c4aSGarrett Wollman          */
609603724d3SBjoern A. Zeeb 	if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP)
610f0068c4aSGarrett Wollman 		goto ours;
611f0068c4aSGarrett Wollman 
612df8bae1dSRodney W. Grimes 	/*
613df8bae1dSRodney W. Grimes 	 * Check our list of addresses, to see if the packet is for us.
614cc766e04SGarrett Wollman 	 * If we don't have any addresses, assume any unicast packet
615cc766e04SGarrett Wollman 	 * we receive might be for us (and let the upper layers deal
616cc766e04SGarrett Wollman 	 * with it).
617df8bae1dSRodney W. Grimes 	 */
618603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
619cc766e04SGarrett Wollman 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
620cc766e04SGarrett Wollman 		goto ours;
621cc766e04SGarrett Wollman 
6227538a9a0SJonathan Lemon 	/*
623823db0e9SDon Lewis 	 * Enable a consistency check between the destination address
624823db0e9SDon Lewis 	 * and the arrival interface for a unicast packet (the RFC 1122
625823db0e9SDon Lewis 	 * strong ES model) if IP forwarding is disabled and the packet
626e15ae1b2SDon Lewis 	 * is not locally generated and the packet is not subject to
627e15ae1b2SDon Lewis 	 * 'ipfw fwd'.
6283f67c834SDon Lewis 	 *
6293f67c834SDon Lewis 	 * XXX - Checking also should be disabled if the destination
6303f67c834SDon Lewis 	 * address is ipnat'ed to a different interface.
6313f67c834SDon Lewis 	 *
632a8f12100SDon Lewis 	 * XXX - Checking is incompatible with IP aliases added
6333f67c834SDon Lewis 	 * to the loopback interface instead of the interface where
6343f67c834SDon Lewis 	 * the packets are received.
635a9771948SGleb Smirnoff 	 *
636a9771948SGleb Smirnoff 	 * XXX - This is the case for carp vhost IPs as well so we
637a9771948SGleb Smirnoff 	 * insert a workaround. If the packet got here, we already
638a9771948SGleb Smirnoff 	 * checked with carp_iamatch() and carp_forus().
639823db0e9SDon Lewis 	 */
640603724d3SBjoern A. Zeeb 	checkif = V_ip_checkinterface && (V_ipforwarding == 0) &&
6410aade26eSRobert Watson 	    ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) &&
64254bfbd51SWill Andrews 	    ifp->if_carp == NULL && (dchg == 0);
643823db0e9SDon Lewis 
644ca925d9cSJonathan Lemon 	/*
645ca925d9cSJonathan Lemon 	 * Check for exact addresses in the hash bucket.
646ca925d9cSJonathan Lemon 	 */
6472d9cfabaSRobert Watson 	/* IN_IFADDR_RLOCK(); */
6489b932e9eSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
649f9e354dfSJulian Elischer 		/*
650823db0e9SDon Lewis 		 * If the address matches, verify that the packet
651823db0e9SDon Lewis 		 * arrived via the correct interface if checking is
652823db0e9SDon Lewis 		 * enabled.
653f9e354dfSJulian Elischer 		 */
6549b932e9eSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
6558c0fec80SRobert Watson 		    (!checkif || ia->ia_ifp == ifp)) {
6567caf4ab7SGleb Smirnoff 			counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
6577caf4ab7SGleb Smirnoff 			counter_u64_add(ia->ia_ifa.ifa_ibytes,
6587caf4ab7SGleb Smirnoff 			    m->m_pkthdr.len);
6592d9cfabaSRobert Watson 			/* IN_IFADDR_RUNLOCK(); */
660ed1ff184SJulian Elischer 			goto ours;
661ca925d9cSJonathan Lemon 		}
6628c0fec80SRobert Watson 	}
6632d9cfabaSRobert Watson 	/* IN_IFADDR_RUNLOCK(); */
6642d9cfabaSRobert Watson 
665823db0e9SDon Lewis 	/*
666ca925d9cSJonathan Lemon 	 * Check for broadcast addresses.
667ca925d9cSJonathan Lemon 	 *
668ca925d9cSJonathan Lemon 	 * Only accept broadcast packets that arrive via the matching
669ca925d9cSJonathan Lemon 	 * interface.  Reception of forwarded directed broadcasts would
670ca925d9cSJonathan Lemon 	 * be handled via ip_forward() and ether_output() with the loopback
671ca925d9cSJonathan Lemon 	 * into the stack for SIMPLEX interfaces handled by ether_output().
672823db0e9SDon Lewis 	 */
6730aade26eSRobert Watson 	if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
674137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
6750aade26eSRobert Watson 	        TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
676ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
677ca925d9cSJonathan Lemon 				continue;
678ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
679df8bae1dSRodney W. Grimes 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
6800aade26eSRobert Watson 			    ip->ip_dst.s_addr) {
6817caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
6827caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_ibytes,
6837caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len);
684137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
685df8bae1dSRodney W. Grimes 				goto ours;
6860aade26eSRobert Watson 			}
6870ac40133SBrian Somers #ifdef BOOTP_COMPAT
6880aade26eSRobert Watson 			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) {
6897caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
6907caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_ibytes,
6917caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len);
692137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
693ca925d9cSJonathan Lemon 				goto ours;
6940aade26eSRobert Watson 			}
6950ac40133SBrian Somers #endif
696df8bae1dSRodney W. Grimes 		}
697137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
69819e5b0a7SRobert Watson 		ia = NULL;
699df8bae1dSRodney W. Grimes 	}
700f8429ca2SBruce M Simpson 	/* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
701f8429ca2SBruce M Simpson 	if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
70286425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
703f8429ca2SBruce M Simpson 		m_freem(m);
704f8429ca2SBruce M Simpson 		return;
705f8429ca2SBruce M Simpson 	}
706df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
707603724d3SBjoern A. Zeeb 		if (V_ip_mrouter) {
708df8bae1dSRodney W. Grimes 			/*
709df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, all
710df8bae1dSRodney W. Grimes 			 * incoming multicast packets are passed to the
711df8bae1dSRodney W. Grimes 			 * kernel-level multicast forwarding function.
712df8bae1dSRodney W. Grimes 			 * The packet is returned (relatively) intact; if
713df8bae1dSRodney W. Grimes 			 * ip_mforward() returns a non-zero value, the packet
714df8bae1dSRodney W. Grimes 			 * must be discarded, else it may be accepted below.
715df8bae1dSRodney W. Grimes 			 */
7160aade26eSRobert Watson 			if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
71786425c62SRobert Watson 				IPSTAT_INC(ips_cantforward);
718df8bae1dSRodney W. Grimes 				m_freem(m);
719c67b1d17SGarrett Wollman 				return;
720df8bae1dSRodney W. Grimes 			}
721df8bae1dSRodney W. Grimes 
722df8bae1dSRodney W. Grimes 			/*
72311612afaSDima Dorfman 			 * The process-level routing daemon needs to receive
724df8bae1dSRodney W. Grimes 			 * all multicast IGMP packets, whether or not this
725df8bae1dSRodney W. Grimes 			 * host belongs to their destination groups.
726df8bae1dSRodney W. Grimes 			 */
727df8bae1dSRodney W. Grimes 			if (ip->ip_p == IPPROTO_IGMP)
728df8bae1dSRodney W. Grimes 				goto ours;
72986425c62SRobert Watson 			IPSTAT_INC(ips_forward);
730df8bae1dSRodney W. Grimes 		}
731df8bae1dSRodney W. Grimes 		/*
732d10910e6SBruce M Simpson 		 * Assume the packet is for us, to avoid prematurely taking
733d10910e6SBruce M Simpson 		 * a lock on the in_multi hash. Protocols must perform
734d10910e6SBruce M Simpson 		 * their own filtering and update statistics accordingly.
735df8bae1dSRodney W. Grimes 		 */
736df8bae1dSRodney W. Grimes 		goto ours;
737df8bae1dSRodney W. Grimes 	}
738df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
739df8bae1dSRodney W. Grimes 		goto ours;
740df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == INADDR_ANY)
741df8bae1dSRodney W. Grimes 		goto ours;
742df8bae1dSRodney W. Grimes 
7436a800098SYoshinobu Inoue 	/*
744df8bae1dSRodney W. Grimes 	 * Not for us; forward if possible and desirable.
745df8bae1dSRodney W. Grimes 	 */
746603724d3SBjoern A. Zeeb 	if (V_ipforwarding == 0) {
74786425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
748df8bae1dSRodney W. Grimes 		m_freem(m);
749546f251bSChris D. Faulhaber 	} else {
7509b932e9eSAndre Oppermann 		ip_forward(m, dchg);
751546f251bSChris D. Faulhaber 	}
752c67b1d17SGarrett Wollman 	return;
753df8bae1dSRodney W. Grimes 
754df8bae1dSRodney W. Grimes ours:
755d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH
756d0ebc0d2SYaroslav Tykhiy 	/*
757d0ebc0d2SYaroslav Tykhiy 	 * IPSTEALTH: Process non-routing options only
758d0ebc0d2SYaroslav Tykhiy 	 * if the packet is destined for us.
759d0ebc0d2SYaroslav Tykhiy 	 */
7607caf4ab7SGleb Smirnoff 	if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1))
761d0ebc0d2SYaroslav Tykhiy 		return;
762d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */
763d0ebc0d2SYaroslav Tykhiy 
76463f8d699SJordan K. Hubbard 	/*
765b6ea1aa5SRuslan Ermilov 	 * Attempt reassembly; if it succeeds, proceed.
766ac9d7e26SMax Laier 	 * ip_reass() will return a different mbuf.
767df8bae1dSRodney W. Grimes 	 */
7688f134647SGleb Smirnoff 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
769aa69c612SGleb Smirnoff 		/* XXXGL: shouldn't we save & set m_flags? */
770f0cada84SAndre Oppermann 		m = ip_reass(m);
771f0cada84SAndre Oppermann 		if (m == NULL)
772c67b1d17SGarrett Wollman 			return;
7736a800098SYoshinobu Inoue 		ip = mtod(m, struct ip *);
7747e2df452SRuslan Ermilov 		/* Get the header length of the reassembled packet */
77553be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
776f0cada84SAndre Oppermann 	}
777f0cada84SAndre Oppermann 
778b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
77933841545SHajimu UMEMOTO 	/*
78033841545SHajimu UMEMOTO 	 * enforce IPsec policy checking if we are seeing last header.
78133841545SHajimu UMEMOTO 	 * note that we do not visit this with protocols with pcb layer
78233841545SHajimu UMEMOTO 	 * code - like udp/tcp/raw ip.
78333841545SHajimu UMEMOTO 	 */
784e58320f1SAndrey V. Elsukov 	if (ip_ipsec_input(m, ip->ip_p) != 0)
78533841545SHajimu UMEMOTO 		goto bad;
786b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
78733841545SHajimu UMEMOTO 
788df8bae1dSRodney W. Grimes 	/*
789df8bae1dSRodney W. Grimes 	 * Switch out to protocol's input routine.
790df8bae1dSRodney W. Grimes 	 */
79186425c62SRobert Watson 	IPSTAT_INC(ips_delivered);
7929b932e9eSAndre Oppermann 
7938f5a8818SKevin Lo 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(&m, &hlen, ip->ip_p);
794c67b1d17SGarrett Wollman 	return;
795df8bae1dSRodney W. Grimes bad:
796df8bae1dSRodney W. Grimes 	m_freem(m);
797c67b1d17SGarrett Wollman }
798c67b1d17SGarrett Wollman 
799c67b1d17SGarrett Wollman /*
800d248c7d7SRobert Watson  * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
801d248c7d7SRobert Watson  * max has slightly different semantics than the sysctl, for historical
802d248c7d7SRobert Watson  * reasons.
803d248c7d7SRobert Watson  */
804d248c7d7SRobert Watson static void
805d248c7d7SRobert Watson maxnipq_update(void)
806d248c7d7SRobert Watson {
807d248c7d7SRobert Watson 
808d248c7d7SRobert Watson 	/*
809d248c7d7SRobert Watson 	 * -1 for unlimited allocation.
810d248c7d7SRobert Watson 	 */
811603724d3SBjoern A. Zeeb 	if (V_maxnipq < 0)
812603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 0);
813d248c7d7SRobert Watson 	/*
814d248c7d7SRobert Watson 	 * Positive number for specific bound.
815d248c7d7SRobert Watson 	 */
816603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0)
817603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, V_maxnipq);
818d248c7d7SRobert Watson 	/*
819d248c7d7SRobert Watson 	 * Zero specifies no further fragment queue allocation -- set the
820d248c7d7SRobert Watson 	 * bound very low, but rely on implementation elsewhere to actually
821d248c7d7SRobert Watson 	 * prevent allocation and reclaim current queues.
822d248c7d7SRobert Watson 	 */
823603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0)
824603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 1);
825d248c7d7SRobert Watson }
826d248c7d7SRobert Watson 
8274f590175SPaul Saab static void
8284f590175SPaul Saab ipq_zone_change(void *tag)
8294f590175SPaul Saab {
8304f590175SPaul Saab 
831603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) {
832603724d3SBjoern A. Zeeb 		V_maxnipq = nmbclusters / 32;
8334f590175SPaul Saab 		maxnipq_update();
8344f590175SPaul Saab 	}
8354f590175SPaul Saab }
8364f590175SPaul Saab 
837d248c7d7SRobert Watson static int
838d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
839d248c7d7SRobert Watson {
840d248c7d7SRobert Watson 	int error, i;
841d248c7d7SRobert Watson 
842603724d3SBjoern A. Zeeb 	i = V_maxnipq;
843d248c7d7SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
844d248c7d7SRobert Watson 	if (error || !req->newptr)
845d248c7d7SRobert Watson 		return (error);
846d248c7d7SRobert Watson 
847d248c7d7SRobert Watson 	/*
848d248c7d7SRobert Watson 	 * XXXRW: Might be a good idea to sanity check the argument and place
849d248c7d7SRobert Watson 	 * an extreme upper bound.
850d248c7d7SRobert Watson 	 */
851d248c7d7SRobert Watson 	if (i < -1)
852d248c7d7SRobert Watson 		return (EINVAL);
853603724d3SBjoern A. Zeeb 	V_maxnipq = i;
854d248c7d7SRobert Watson 	maxnipq_update();
855d248c7d7SRobert Watson 	return (0);
856d248c7d7SRobert Watson }
857d248c7d7SRobert Watson 
858d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
859d248c7d7SRobert Watson     NULL, 0, sysctl_maxnipq, "I",
860d248c7d7SRobert Watson     "Maximum number of IPv4 fragment reassembly queue entries");
861d248c7d7SRobert Watson 
862aa69c612SGleb Smirnoff #define	M_IP_FRAG	M_PROTO9
863aa69c612SGleb Smirnoff 
864d248c7d7SRobert Watson /*
8658948e4baSArchie Cobbs  * Take incoming datagram fragment and try to reassemble it into
866f0cada84SAndre Oppermann  * whole datagram.  If the argument is the first fragment or one
867f0cada84SAndre Oppermann  * in between the function will return NULL and store the mbuf
868f0cada84SAndre Oppermann  * in the fragment chain.  If the argument is the last fragment
869f0cada84SAndre Oppermann  * the packet will be reassembled and the pointer to the new
870f0cada84SAndre Oppermann  * mbuf returned for further processing.  Only m_tags attached
871f0cada84SAndre Oppermann  * to the first packet/fragment are preserved.
872f0cada84SAndre Oppermann  * The IP header is *NOT* adjusted out of iplen.
873df8bae1dSRodney W. Grimes  */
874f0cada84SAndre Oppermann struct mbuf *
875f0cada84SAndre Oppermann ip_reass(struct mbuf *m)
876df8bae1dSRodney W. Grimes {
877f0cada84SAndre Oppermann 	struct ip *ip;
878f0cada84SAndre Oppermann 	struct mbuf *p, *q, *nq, *t;
879f0cada84SAndre Oppermann 	struct ipq *fp = NULL;
880f0cada84SAndre Oppermann 	struct ipqhead *head;
881f0cada84SAndre Oppermann 	int i, hlen, next;
88259dfcba4SHajimu UMEMOTO 	u_int8_t ecn, ecn0;
883f0cada84SAndre Oppermann 	u_short hash;
884b8bc95cdSAdrian Chadd #ifdef	RSS
885b8bc95cdSAdrian Chadd 	uint32_t rss_hash, rss_type;
886b8bc95cdSAdrian Chadd #endif
887df8bae1dSRodney W. Grimes 
888800af1fbSMaxim Konovalov 	/* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
889603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0 || V_maxfragsperpacket == 0) {
89086425c62SRobert Watson 		IPSTAT_INC(ips_fragments);
89186425c62SRobert Watson 		IPSTAT_INC(ips_fragdropped);
8929d804f81SAndre Oppermann 		m_freem(m);
8939d804f81SAndre Oppermann 		return (NULL);
894f0cada84SAndre Oppermann 	}
8952fad1e93SSam Leffler 
896f0cada84SAndre Oppermann 	ip = mtod(m, struct ip *);
897f0cada84SAndre Oppermann 	hlen = ip->ip_hl << 2;
898f0cada84SAndre Oppermann 
899f0cada84SAndre Oppermann 	hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
900603724d3SBjoern A. Zeeb 	head = &V_ipq[hash];
901f0cada84SAndre Oppermann 	IPQ_LOCK();
902f0cada84SAndre Oppermann 
903f0cada84SAndre Oppermann 	/*
904f0cada84SAndre Oppermann 	 * Look for queue of fragments
905f0cada84SAndre Oppermann 	 * of this datagram.
906f0cada84SAndre Oppermann 	 */
907f0cada84SAndre Oppermann 	TAILQ_FOREACH(fp, head, ipq_list)
908f0cada84SAndre Oppermann 		if (ip->ip_id == fp->ipq_id &&
909f0cada84SAndre Oppermann 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
910f0cada84SAndre Oppermann 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
911f0cada84SAndre Oppermann #ifdef MAC
91230d239bcSRobert Watson 		    mac_ipq_match(m, fp) &&
913f0cada84SAndre Oppermann #endif
914f0cada84SAndre Oppermann 		    ip->ip_p == fp->ipq_p)
915f0cada84SAndre Oppermann 			goto found;
916f0cada84SAndre Oppermann 
917f0cada84SAndre Oppermann 	fp = NULL;
918f0cada84SAndre Oppermann 
919f0cada84SAndre Oppermann 	/*
920d248c7d7SRobert Watson 	 * Attempt to trim the number of allocated fragment queues if it
921d248c7d7SRobert Watson 	 * exceeds the administrative limit.
922f0cada84SAndre Oppermann 	 */
923603724d3SBjoern A. Zeeb 	if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) {
924f0cada84SAndre Oppermann 		/*
925f0cada84SAndre Oppermann 		 * drop something from the tail of the current queue
926f0cada84SAndre Oppermann 		 * before proceeding further
927f0cada84SAndre Oppermann 		 */
928f0cada84SAndre Oppermann 		struct ipq *q = TAILQ_LAST(head, ipqhead);
929f0cada84SAndre Oppermann 		if (q == NULL) {   /* gak */
930f0cada84SAndre Oppermann 			for (i = 0; i < IPREASS_NHASH; i++) {
931603724d3SBjoern A. Zeeb 				struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead);
932f0cada84SAndre Oppermann 				if (r) {
93386425c62SRobert Watson 					IPSTAT_ADD(ips_fragtimeout,
93486425c62SRobert Watson 					    r->ipq_nfrags);
935603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], r);
936f0cada84SAndre Oppermann 					break;
937f0cada84SAndre Oppermann 				}
938f0cada84SAndre Oppermann 			}
939f0cada84SAndre Oppermann 		} else {
94086425c62SRobert Watson 			IPSTAT_ADD(ips_fragtimeout, q->ipq_nfrags);
941f0cada84SAndre Oppermann 			ip_freef(head, q);
942f0cada84SAndre Oppermann 		}
943f0cada84SAndre Oppermann 	}
944f0cada84SAndre Oppermann 
945f0cada84SAndre Oppermann found:
946f0cada84SAndre Oppermann 	/*
947f0cada84SAndre Oppermann 	 * Adjust ip_len to not reflect header,
948f0cada84SAndre Oppermann 	 * convert offset of this to bytes.
949f0cada84SAndre Oppermann 	 */
9508f134647SGleb Smirnoff 	ip->ip_len = htons(ntohs(ip->ip_len) - hlen);
9518f134647SGleb Smirnoff 	if (ip->ip_off & htons(IP_MF)) {
952f0cada84SAndre Oppermann 		/*
953f0cada84SAndre Oppermann 		 * Make sure that fragments have a data length
954f0cada84SAndre Oppermann 		 * that's a non-zero multiple of 8 bytes.
955f0cada84SAndre Oppermann 		 */
956b6fcf6f9SGleb Smirnoff 		if (ip->ip_len == htons(0) || (ntohs(ip->ip_len) & 0x7) != 0) {
95786425c62SRobert Watson 			IPSTAT_INC(ips_toosmall); /* XXX */
958f0cada84SAndre Oppermann 			goto dropfrag;
959f0cada84SAndre Oppermann 		}
960b09dc7e3SAndre Oppermann 		m->m_flags |= M_IP_FRAG;
961f0cada84SAndre Oppermann 	} else
962b09dc7e3SAndre Oppermann 		m->m_flags &= ~M_IP_FRAG;
9638f134647SGleb Smirnoff 	ip->ip_off = htons(ntohs(ip->ip_off) << 3);
964f0cada84SAndre Oppermann 
965f0cada84SAndre Oppermann 	/*
966f0cada84SAndre Oppermann 	 * Attempt reassembly; if it succeeds, proceed.
967f0cada84SAndre Oppermann 	 * ip_reass() will return a different mbuf.
968f0cada84SAndre Oppermann 	 */
96986425c62SRobert Watson 	IPSTAT_INC(ips_fragments);
9701b4381afSAndre Oppermann 	m->m_pkthdr.PH_loc.ptr = ip;
971f0cada84SAndre Oppermann 
972f0cada84SAndre Oppermann 	/* Previous ip_reass() started here. */
973df8bae1dSRodney W. Grimes 	/*
974df8bae1dSRodney W. Grimes 	 * Presence of header sizes in mbufs
975df8bae1dSRodney W. Grimes 	 * would confuse code below.
976df8bae1dSRodney W. Grimes 	 */
977df8bae1dSRodney W. Grimes 	m->m_data += hlen;
978df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
979df8bae1dSRodney W. Grimes 
980df8bae1dSRodney W. Grimes 	/*
981df8bae1dSRodney W. Grimes 	 * If first fragment to arrive, create a reassembly queue.
982df8bae1dSRodney W. Grimes 	 */
983042bbfa3SRobert Watson 	if (fp == NULL) {
984603724d3SBjoern A. Zeeb 		fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
985d248c7d7SRobert Watson 		if (fp == NULL)
986df8bae1dSRodney W. Grimes 			goto dropfrag;
98736b0360bSRobert Watson #ifdef MAC
98830d239bcSRobert Watson 		if (mac_ipq_init(fp, M_NOWAIT) != 0) {
989603724d3SBjoern A. Zeeb 			uma_zfree(V_ipq_zone, fp);
9901d7d0bfeSPawel Jakub Dawidek 			fp = NULL;
9915e7ce478SRobert Watson 			goto dropfrag;
9925e7ce478SRobert Watson 		}
99330d239bcSRobert Watson 		mac_ipq_create(m, fp);
99436b0360bSRobert Watson #endif
995462b86feSPoul-Henning Kamp 		TAILQ_INSERT_HEAD(head, fp, ipq_list);
996603724d3SBjoern A. Zeeb 		V_nipq++;
997375386e2SMike Silbersack 		fp->ipq_nfrags = 1;
998df8bae1dSRodney W. Grimes 		fp->ipq_ttl = IPFRAGTTL;
999df8bae1dSRodney W. Grimes 		fp->ipq_p = ip->ip_p;
1000df8bae1dSRodney W. Grimes 		fp->ipq_id = ip->ip_id;
10016effc713SDoug Rabson 		fp->ipq_src = ip->ip_src;
10026effc713SDoug Rabson 		fp->ipq_dst = ip->ip_dst;
1003af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
1004af38c68cSLuigi Rizzo 		m->m_nextpkt = NULL;
1005800af1fbSMaxim Konovalov 		goto done;
100636b0360bSRobert Watson 	} else {
1007375386e2SMike Silbersack 		fp->ipq_nfrags++;
100836b0360bSRobert Watson #ifdef MAC
100930d239bcSRobert Watson 		mac_ipq_update(m, fp);
101036b0360bSRobert Watson #endif
1011df8bae1dSRodney W. Grimes 	}
1012df8bae1dSRodney W. Grimes 
10131b4381afSAndre Oppermann #define GETIP(m)	((struct ip*)((m)->m_pkthdr.PH_loc.ptr))
10146effc713SDoug Rabson 
1015df8bae1dSRodney W. Grimes 	/*
101659dfcba4SHajimu UMEMOTO 	 * Handle ECN by comparing this segment with the first one;
101759dfcba4SHajimu UMEMOTO 	 * if CE is set, do not lose CE.
101859dfcba4SHajimu UMEMOTO 	 * drop if CE and not-ECT are mixed for the same packet.
101959dfcba4SHajimu UMEMOTO 	 */
102059dfcba4SHajimu UMEMOTO 	ecn = ip->ip_tos & IPTOS_ECN_MASK;
102159dfcba4SHajimu UMEMOTO 	ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
102259dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_CE) {
102359dfcba4SHajimu UMEMOTO 		if (ecn0 == IPTOS_ECN_NOTECT)
102459dfcba4SHajimu UMEMOTO 			goto dropfrag;
102559dfcba4SHajimu UMEMOTO 		if (ecn0 != IPTOS_ECN_CE)
102659dfcba4SHajimu UMEMOTO 			GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
102759dfcba4SHajimu UMEMOTO 	}
102859dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
102959dfcba4SHajimu UMEMOTO 		goto dropfrag;
103059dfcba4SHajimu UMEMOTO 
103159dfcba4SHajimu UMEMOTO 	/*
1032df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
1033df8bae1dSRodney W. Grimes 	 */
10346effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
10358f134647SGleb Smirnoff 		if (ntohs(GETIP(q)->ip_off) > ntohs(ip->ip_off))
1036df8bae1dSRodney W. Grimes 			break;
1037df8bae1dSRodney W. Grimes 
1038df8bae1dSRodney W. Grimes 	/*
1039df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
1040df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
1041af38c68cSLuigi Rizzo 	 * segment.  If it provides all of our data, drop us, otherwise
1042af38c68cSLuigi Rizzo 	 * stick new segment in the proper place.
1043db4f9cc7SJonathan Lemon 	 *
10446bccea7cSRebecca Cran 	 * If some of the data is dropped from the preceding
1045db4f9cc7SJonathan Lemon 	 * segment, then it's checksum is invalidated.
1046df8bae1dSRodney W. Grimes 	 */
10476effc713SDoug Rabson 	if (p) {
10488f134647SGleb Smirnoff 		i = ntohs(GETIP(p)->ip_off) + ntohs(GETIP(p)->ip_len) -
10498f134647SGleb Smirnoff 		    ntohs(ip->ip_off);
1050df8bae1dSRodney W. Grimes 		if (i > 0) {
10518f134647SGleb Smirnoff 			if (i >= ntohs(ip->ip_len))
1052df8bae1dSRodney W. Grimes 				goto dropfrag;
10536a800098SYoshinobu Inoue 			m_adj(m, i);
1054db4f9cc7SJonathan Lemon 			m->m_pkthdr.csum_flags = 0;
10558f134647SGleb Smirnoff 			ip->ip_off = htons(ntohs(ip->ip_off) + i);
10568f134647SGleb Smirnoff 			ip->ip_len = htons(ntohs(ip->ip_len) - i);
1057df8bae1dSRodney W. Grimes 		}
1058af38c68cSLuigi Rizzo 		m->m_nextpkt = p->m_nextpkt;
1059af38c68cSLuigi Rizzo 		p->m_nextpkt = m;
1060af38c68cSLuigi Rizzo 	} else {
1061af38c68cSLuigi Rizzo 		m->m_nextpkt = fp->ipq_frags;
1062af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
1063df8bae1dSRodney W. Grimes 	}
1064df8bae1dSRodney W. Grimes 
1065df8bae1dSRodney W. Grimes 	/*
1066df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
1067df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
1068df8bae1dSRodney W. Grimes 	 */
10698f134647SGleb Smirnoff 	for (; q != NULL && ntohs(ip->ip_off) + ntohs(ip->ip_len) >
10708f134647SGleb Smirnoff 	    ntohs(GETIP(q)->ip_off); q = nq) {
10718f134647SGleb Smirnoff 		i = (ntohs(ip->ip_off) + ntohs(ip->ip_len)) -
10728f134647SGleb Smirnoff 		    ntohs(GETIP(q)->ip_off);
10738f134647SGleb Smirnoff 		if (i < ntohs(GETIP(q)->ip_len)) {
10748f134647SGleb Smirnoff 			GETIP(q)->ip_len = htons(ntohs(GETIP(q)->ip_len) - i);
10758f134647SGleb Smirnoff 			GETIP(q)->ip_off = htons(ntohs(GETIP(q)->ip_off) + i);
10766effc713SDoug Rabson 			m_adj(q, i);
1077db4f9cc7SJonathan Lemon 			q->m_pkthdr.csum_flags = 0;
1078df8bae1dSRodney W. Grimes 			break;
1079df8bae1dSRodney W. Grimes 		}
10806effc713SDoug Rabson 		nq = q->m_nextpkt;
1081af38c68cSLuigi Rizzo 		m->m_nextpkt = nq;
108286425c62SRobert Watson 		IPSTAT_INC(ips_fragdropped);
1083375386e2SMike Silbersack 		fp->ipq_nfrags--;
10846effc713SDoug Rabson 		m_freem(q);
1085df8bae1dSRodney W. Grimes 	}
1086df8bae1dSRodney W. Grimes 
1087df8bae1dSRodney W. Grimes 	/*
1088375386e2SMike Silbersack 	 * Check for complete reassembly and perform frag per packet
1089375386e2SMike Silbersack 	 * limiting.
1090375386e2SMike Silbersack 	 *
1091375386e2SMike Silbersack 	 * Frag limiting is performed here so that the nth frag has
1092375386e2SMike Silbersack 	 * a chance to complete the packet before we drop the packet.
1093375386e2SMike Silbersack 	 * As a result, n+1 frags are actually allowed per packet, but
1094375386e2SMike Silbersack 	 * only n will ever be stored. (n = maxfragsperpacket.)
1095375386e2SMike Silbersack 	 *
1096df8bae1dSRodney W. Grimes 	 */
10976effc713SDoug Rabson 	next = 0;
10986effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
10998f134647SGleb Smirnoff 		if (ntohs(GETIP(q)->ip_off) != next) {
1100603724d3SBjoern A. Zeeb 			if (fp->ipq_nfrags > V_maxfragsperpacket) {
110186425c62SRobert Watson 				IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1102375386e2SMike Silbersack 				ip_freef(head, fp);
110399e8617dSMaxim Konovalov 			}
1104f0cada84SAndre Oppermann 			goto done;
1105375386e2SMike Silbersack 		}
11068f134647SGleb Smirnoff 		next += ntohs(GETIP(q)->ip_len);
11076effc713SDoug Rabson 	}
11086effc713SDoug Rabson 	/* Make sure the last packet didn't have the IP_MF flag */
1109b09dc7e3SAndre Oppermann 	if (p->m_flags & M_IP_FRAG) {
1110603724d3SBjoern A. Zeeb 		if (fp->ipq_nfrags > V_maxfragsperpacket) {
111186425c62SRobert Watson 			IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1112375386e2SMike Silbersack 			ip_freef(head, fp);
111399e8617dSMaxim Konovalov 		}
1114f0cada84SAndre Oppermann 		goto done;
1115375386e2SMike Silbersack 	}
1116df8bae1dSRodney W. Grimes 
1117df8bae1dSRodney W. Grimes 	/*
1118430d30d8SBill Fenner 	 * Reassembly is complete.  Make sure the packet is a sane size.
1119430d30d8SBill Fenner 	 */
11206effc713SDoug Rabson 	q = fp->ipq_frags;
11216effc713SDoug Rabson 	ip = GETIP(q);
112253be11f6SPoul-Henning Kamp 	if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
112386425c62SRobert Watson 		IPSTAT_INC(ips_toolong);
112486425c62SRobert Watson 		IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1125462b86feSPoul-Henning Kamp 		ip_freef(head, fp);
1126f0cada84SAndre Oppermann 		goto done;
1127430d30d8SBill Fenner 	}
1128430d30d8SBill Fenner 
1129430d30d8SBill Fenner 	/*
1130430d30d8SBill Fenner 	 * Concatenate fragments.
1131df8bae1dSRodney W. Grimes 	 */
11326effc713SDoug Rabson 	m = q;
1133df8bae1dSRodney W. Grimes 	t = m->m_next;
113402410549SRobert Watson 	m->m_next = NULL;
1135df8bae1dSRodney W. Grimes 	m_cat(m, t);
11366effc713SDoug Rabson 	nq = q->m_nextpkt;
113702410549SRobert Watson 	q->m_nextpkt = NULL;
11386effc713SDoug Rabson 	for (q = nq; q != NULL; q = nq) {
11396effc713SDoug Rabson 		nq = q->m_nextpkt;
1140945aa40dSDoug Rabson 		q->m_nextpkt = NULL;
1141db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1142db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1143a8db1d93SJonathan Lemon 		m_cat(m, q);
1144df8bae1dSRodney W. Grimes 	}
11456edb555dSOleg Bulyzhin 	/*
11466edb555dSOleg Bulyzhin 	 * In order to do checksumming faster we do 'end-around carry' here
11476edb555dSOleg Bulyzhin 	 * (and not in for{} loop), though it implies we are not going to
11486edb555dSOleg Bulyzhin 	 * reassemble more than 64k fragments.
11496edb555dSOleg Bulyzhin 	 */
1150c732cd1aSPyun YongHyeon 	while (m->m_pkthdr.csum_data & 0xffff0000)
1151c732cd1aSPyun YongHyeon 		m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
1152c732cd1aSPyun YongHyeon 		    (m->m_pkthdr.csum_data >> 16);
115336b0360bSRobert Watson #ifdef MAC
115430d239bcSRobert Watson 	mac_ipq_reassemble(fp, m);
115530d239bcSRobert Watson 	mac_ipq_destroy(fp);
115636b0360bSRobert Watson #endif
1157df8bae1dSRodney W. Grimes 
1158df8bae1dSRodney W. Grimes 	/*
1159f0cada84SAndre Oppermann 	 * Create header for new ip packet by modifying header of first
1160f0cada84SAndre Oppermann 	 * packet;  dequeue and discard fragment reassembly header.
1161df8bae1dSRodney W. Grimes 	 * Make header visible.
1162df8bae1dSRodney W. Grimes 	 */
11638f134647SGleb Smirnoff 	ip->ip_len = htons((ip->ip_hl << 2) + next);
11646effc713SDoug Rabson 	ip->ip_src = fp->ipq_src;
11656effc713SDoug Rabson 	ip->ip_dst = fp->ipq_dst;
1166462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(head, fp, ipq_list);
1167603724d3SBjoern A. Zeeb 	V_nipq--;
1168603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
116953be11f6SPoul-Henning Kamp 	m->m_len += (ip->ip_hl << 2);
117053be11f6SPoul-Henning Kamp 	m->m_data -= (ip->ip_hl << 2);
1171df8bae1dSRodney W. Grimes 	/* some debugging cruft by sklower, below, will go away soon */
1172a5554bf0SPoul-Henning Kamp 	if (m->m_flags & M_PKTHDR)	/* XXX this should be done elsewhere */
1173a5554bf0SPoul-Henning Kamp 		m_fixhdr(m);
117486425c62SRobert Watson 	IPSTAT_INC(ips_reassembled);
1175f0cada84SAndre Oppermann 	IPQ_UNLOCK();
1176b8bc95cdSAdrian Chadd 
1177b8bc95cdSAdrian Chadd #ifdef	RSS
1178b8bc95cdSAdrian Chadd 	/*
1179b8bc95cdSAdrian Chadd 	 * Query the RSS layer for the flowid / flowtype for the
1180b8bc95cdSAdrian Chadd 	 * mbuf payload.
1181b8bc95cdSAdrian Chadd 	 *
1182b8bc95cdSAdrian Chadd 	 * For now, just assume we have to calculate a new one.
1183b8bc95cdSAdrian Chadd 	 * Later on we should check to see if the assigned flowid matches
1184b8bc95cdSAdrian Chadd 	 * what RSS wants for the given IP protocol and if so, just keep it.
1185b8bc95cdSAdrian Chadd 	 *
1186b8bc95cdSAdrian Chadd 	 * We then queue into the relevant netisr so it can be dispatched
1187b8bc95cdSAdrian Chadd 	 * to the correct CPU.
1188b8bc95cdSAdrian Chadd 	 *
1189b8bc95cdSAdrian Chadd 	 * Note - this may return 1, which means the flowid in the mbuf
1190b8bc95cdSAdrian Chadd 	 * is correct for the configured RSS hash types and can be used.
1191b8bc95cdSAdrian Chadd 	 */
1192b8bc95cdSAdrian Chadd 	if (rss_mbuf_software_hash_v4(m, 0, &rss_hash, &rss_type) == 0) {
1193b8bc95cdSAdrian Chadd 		m->m_pkthdr.flowid = rss_hash;
1194b8bc95cdSAdrian Chadd 		M_HASHTYPE_SET(m, rss_type);
1195b8bc95cdSAdrian Chadd 	}
1196b8bc95cdSAdrian Chadd 
1197b8bc95cdSAdrian Chadd 	/*
1198b8bc95cdSAdrian Chadd 	 * Queue/dispatch for reprocessing.
1199b8bc95cdSAdrian Chadd 	 *
1200b8bc95cdSAdrian Chadd 	 * Note: this is much slower than just handling the frame in the
1201b8bc95cdSAdrian Chadd 	 * current receive context.  It's likely worth investigating
1202b8bc95cdSAdrian Chadd 	 * why this is.
1203b8bc95cdSAdrian Chadd 	 */
1204b8bc95cdSAdrian Chadd 	netisr_dispatch(NETISR_IP_DIRECT, m);
1205b8bc95cdSAdrian Chadd 	return (NULL);
1206b8bc95cdSAdrian Chadd #endif
1207b8bc95cdSAdrian Chadd 
1208b8bc95cdSAdrian Chadd 	/* Handle in-line */
12096a800098SYoshinobu Inoue 	return (m);
1210df8bae1dSRodney W. Grimes 
1211df8bae1dSRodney W. Grimes dropfrag:
121286425c62SRobert Watson 	IPSTAT_INC(ips_fragdropped);
1213042bbfa3SRobert Watson 	if (fp != NULL)
1214375386e2SMike Silbersack 		fp->ipq_nfrags--;
1215df8bae1dSRodney W. Grimes 	m_freem(m);
1216f0cada84SAndre Oppermann done:
1217f0cada84SAndre Oppermann 	IPQ_UNLOCK();
1218f0cada84SAndre Oppermann 	return (NULL);
12196effc713SDoug Rabson 
12206effc713SDoug Rabson #undef GETIP
1221df8bae1dSRodney W. Grimes }
1222df8bae1dSRodney W. Grimes 
1223df8bae1dSRodney W. Grimes /*
1224df8bae1dSRodney W. Grimes  * Free a fragment reassembly header and all
1225df8bae1dSRodney W. Grimes  * associated datagrams.
1226df8bae1dSRodney W. Grimes  */
12270312fbe9SPoul-Henning Kamp static void
1228f2565d68SRobert Watson ip_freef(struct ipqhead *fhp, struct ipq *fp)
1229df8bae1dSRodney W. Grimes {
1230f2565d68SRobert Watson 	struct mbuf *q;
1231df8bae1dSRodney W. Grimes 
12322fad1e93SSam Leffler 	IPQ_LOCK_ASSERT();
12332fad1e93SSam Leffler 
12346effc713SDoug Rabson 	while (fp->ipq_frags) {
12356effc713SDoug Rabson 		q = fp->ipq_frags;
12366effc713SDoug Rabson 		fp->ipq_frags = q->m_nextpkt;
12376effc713SDoug Rabson 		m_freem(q);
1238df8bae1dSRodney W. Grimes 	}
1239462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(fhp, fp, ipq_list);
1240603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
1241603724d3SBjoern A. Zeeb 	V_nipq--;
1242df8bae1dSRodney W. Grimes }
1243df8bae1dSRodney W. Grimes 
1244df8bae1dSRodney W. Grimes /*
1245df8bae1dSRodney W. Grimes  * IP timer processing;
1246df8bae1dSRodney W. Grimes  * if a timer expires on a reassembly
1247df8bae1dSRodney W. Grimes  * queue, discard it.
1248df8bae1dSRodney W. Grimes  */
1249df8bae1dSRodney W. Grimes void
1250f2565d68SRobert Watson ip_slowtimo(void)
1251df8bae1dSRodney W. Grimes {
12528b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1253f2565d68SRobert Watson 	struct ipq *fp;
1254194a213eSAndrey A. Chernov 	int i;
1255df8bae1dSRodney W. Grimes 
12565ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
12572fad1e93SSam Leffler 	IPQ_LOCK();
12588b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
12598b615593SMarko Zec 		CURVNET_SET(vnet_iter);
1260194a213eSAndrey A. Chernov 		for (i = 0; i < IPREASS_NHASH; i++) {
1261603724d3SBjoern A. Zeeb 			for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) {
1262462b86feSPoul-Henning Kamp 				struct ipq *fpp;
1263462b86feSPoul-Henning Kamp 
1264462b86feSPoul-Henning Kamp 				fpp = fp;
1265462b86feSPoul-Henning Kamp 				fp = TAILQ_NEXT(fp, ipq_list);
1266462b86feSPoul-Henning Kamp 				if(--fpp->ipq_ttl == 0) {
126786425c62SRobert Watson 					IPSTAT_ADD(ips_fragtimeout,
126886425c62SRobert Watson 					    fpp->ipq_nfrags);
1269603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], fpp);
1270df8bae1dSRodney W. Grimes 				}
1271df8bae1dSRodney W. Grimes 			}
1272194a213eSAndrey A. Chernov 		}
1273690a6055SJesper Skriver 		/*
1274690a6055SJesper Skriver 		 * If we are over the maximum number of fragments
1275690a6055SJesper Skriver 		 * (due to the limit being lowered), drain off
1276690a6055SJesper Skriver 		 * enough to get down to the new limit.
1277690a6055SJesper Skriver 		 */
1278603724d3SBjoern A. Zeeb 		if (V_maxnipq >= 0 && V_nipq > V_maxnipq) {
1279690a6055SJesper Skriver 			for (i = 0; i < IPREASS_NHASH; i++) {
12808b615593SMarko Zec 				while (V_nipq > V_maxnipq &&
12818b615593SMarko Zec 				    !TAILQ_EMPTY(&V_ipq[i])) {
128286425c62SRobert Watson 					IPSTAT_ADD(ips_fragdropped,
128386425c62SRobert Watson 					    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
12848b615593SMarko Zec 					ip_freef(&V_ipq[i],
12858b615593SMarko Zec 					    TAILQ_FIRST(&V_ipq[i]));
1286690a6055SJesper Skriver 				}
1287690a6055SJesper Skriver 			}
1288690a6055SJesper Skriver 		}
12898b615593SMarko Zec 		CURVNET_RESTORE();
12908b615593SMarko Zec 	}
12912fad1e93SSam Leffler 	IPQ_UNLOCK();
12925ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
1293df8bae1dSRodney W. Grimes }
1294df8bae1dSRodney W. Grimes 
1295df8bae1dSRodney W. Grimes /*
1296df8bae1dSRodney W. Grimes  * Drain off all datagram fragments.
1297df8bae1dSRodney W. Grimes  */
12989802380eSBjoern A. Zeeb static void
12999802380eSBjoern A. Zeeb ip_drain_locked(void)
1300df8bae1dSRodney W. Grimes {
1301194a213eSAndrey A. Chernov 	int     i;
1302ce29ab3aSGarrett Wollman 
13039802380eSBjoern A. Zeeb 	IPQ_LOCK_ASSERT();
13049802380eSBjoern A. Zeeb 
1305194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1306603724d3SBjoern A. Zeeb 		while(!TAILQ_EMPTY(&V_ipq[i])) {
130786425c62SRobert Watson 			IPSTAT_ADD(ips_fragdropped,
130886425c62SRobert Watson 			    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
1309603724d3SBjoern A. Zeeb 			ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1310194a213eSAndrey A. Chernov 		}
1311194a213eSAndrey A. Chernov 	}
13129802380eSBjoern A. Zeeb }
13139802380eSBjoern A. Zeeb 
13149802380eSBjoern A. Zeeb void
13159802380eSBjoern A. Zeeb ip_drain(void)
13169802380eSBjoern A. Zeeb {
13179802380eSBjoern A. Zeeb 	VNET_ITERATOR_DECL(vnet_iter);
13189802380eSBjoern A. Zeeb 
13199802380eSBjoern A. Zeeb 	VNET_LIST_RLOCK_NOSLEEP();
13209802380eSBjoern A. Zeeb 	IPQ_LOCK();
13219802380eSBjoern A. Zeeb 	VNET_FOREACH(vnet_iter) {
13229802380eSBjoern A. Zeeb 		CURVNET_SET(vnet_iter);
13239802380eSBjoern A. Zeeb 		ip_drain_locked();
13248b615593SMarko Zec 		CURVNET_RESTORE();
13258b615593SMarko Zec 	}
13262fad1e93SSam Leffler 	IPQ_UNLOCK();
13275ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
1328df8bae1dSRodney W. Grimes }
1329df8bae1dSRodney W. Grimes 
1330df8bae1dSRodney W. Grimes /*
1331de38924dSAndre Oppermann  * The protocol to be inserted into ip_protox[] must be already registered
1332de38924dSAndre Oppermann  * in inetsw[], either statically or through pf_proto_register().
1333de38924dSAndre Oppermann  */
1334de38924dSAndre Oppermann int
13351b48d245SBjoern A. Zeeb ipproto_register(short ipproto)
1336de38924dSAndre Oppermann {
1337de38924dSAndre Oppermann 	struct protosw *pr;
1338de38924dSAndre Oppermann 
1339de38924dSAndre Oppermann 	/* Sanity checks. */
13401b48d245SBjoern A. Zeeb 	if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
1341de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1342de38924dSAndre Oppermann 
1343de38924dSAndre Oppermann 	/*
1344de38924dSAndre Oppermann 	 * The protocol slot must not be occupied by another protocol
1345de38924dSAndre Oppermann 	 * already.  An index pointing to IPPROTO_RAW is unused.
1346de38924dSAndre Oppermann 	 */
1347de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1348de38924dSAndre Oppermann 	if (pr == NULL)
1349de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1350de38924dSAndre Oppermann 	if (ip_protox[ipproto] != pr - inetsw)	/* IPPROTO_RAW */
1351de38924dSAndre Oppermann 		return (EEXIST);
1352de38924dSAndre Oppermann 
1353de38924dSAndre Oppermann 	/* Find the protocol position in inetsw[] and set the index. */
1354de38924dSAndre Oppermann 	for (pr = inetdomain.dom_protosw;
1355de38924dSAndre Oppermann 	     pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1356de38924dSAndre Oppermann 		if (pr->pr_domain->dom_family == PF_INET &&
1357de38924dSAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol == ipproto) {
1358de38924dSAndre Oppermann 			ip_protox[pr->pr_protocol] = pr - inetsw;
1359de38924dSAndre Oppermann 			return (0);
1360de38924dSAndre Oppermann 		}
1361de38924dSAndre Oppermann 	}
1362de38924dSAndre Oppermann 	return (EPROTONOSUPPORT);
1363de38924dSAndre Oppermann }
1364de38924dSAndre Oppermann 
1365de38924dSAndre Oppermann int
13661b48d245SBjoern A. Zeeb ipproto_unregister(short ipproto)
1367de38924dSAndre Oppermann {
1368de38924dSAndre Oppermann 	struct protosw *pr;
1369de38924dSAndre Oppermann 
1370de38924dSAndre Oppermann 	/* Sanity checks. */
13711b48d245SBjoern A. Zeeb 	if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
1372de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1373de38924dSAndre Oppermann 
1374de38924dSAndre Oppermann 	/* Check if the protocol was indeed registered. */
1375de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1376de38924dSAndre Oppermann 	if (pr == NULL)
1377de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1378de38924dSAndre Oppermann 	if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
1379de38924dSAndre Oppermann 		return (ENOENT);
1380de38924dSAndre Oppermann 
1381de38924dSAndre Oppermann 	/* Reset the protocol slot to IPPROTO_RAW. */
1382de38924dSAndre Oppermann 	ip_protox[ipproto] = pr - inetsw;
1383de38924dSAndre Oppermann 	return (0);
1384de38924dSAndre Oppermann }
1385de38924dSAndre Oppermann 
1386df8bae1dSRodney W. Grimes /*
13878c0fec80SRobert Watson  * Given address of next destination (final or next hop), return (referenced)
13888c0fec80SRobert Watson  * internet address info of interface to be used to get there.
1389df8bae1dSRodney W. Grimes  */
1390bd714208SRuslan Ermilov struct in_ifaddr *
13918b07e49aSJulian Elischer ip_rtaddr(struct in_addr dst, u_int fibnum)
1392df8bae1dSRodney W. Grimes {
139397d8d152SAndre Oppermann 	struct route sro;
139402c1c707SAndre Oppermann 	struct sockaddr_in *sin;
139519e5b0a7SRobert Watson 	struct in_ifaddr *ia;
1396df8bae1dSRodney W. Grimes 
13970cfbbe3bSAndre Oppermann 	bzero(&sro, sizeof(sro));
139897d8d152SAndre Oppermann 	sin = (struct sockaddr_in *)&sro.ro_dst;
1399df8bae1dSRodney W. Grimes 	sin->sin_family = AF_INET;
1400df8bae1dSRodney W. Grimes 	sin->sin_len = sizeof(*sin);
1401df8bae1dSRodney W. Grimes 	sin->sin_addr = dst;
14026e6b3f7cSQing Li 	in_rtalloc_ign(&sro, 0, fibnum);
1403df8bae1dSRodney W. Grimes 
140497d8d152SAndre Oppermann 	if (sro.ro_rt == NULL)
140502410549SRobert Watson 		return (NULL);
140602c1c707SAndre Oppermann 
140719e5b0a7SRobert Watson 	ia = ifatoia(sro.ro_rt->rt_ifa);
140819e5b0a7SRobert Watson 	ifa_ref(&ia->ia_ifa);
140997d8d152SAndre Oppermann 	RTFREE(sro.ro_rt);
141019e5b0a7SRobert Watson 	return (ia);
1411df8bae1dSRodney W. Grimes }
1412df8bae1dSRodney W. Grimes 
1413df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = {
1414df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1415df8bae1dSRodney W. Grimes 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1416df8bae1dSRodney W. Grimes 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1417df8bae1dSRodney W. Grimes 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1418fcaf9f91SMike Silbersack 	0,		0,		EHOSTUNREACH,	0,
14193b8123b7SJesper Skriver 	ENOPROTOOPT,	ECONNREFUSED
1420df8bae1dSRodney W. Grimes };
1421df8bae1dSRodney W. Grimes 
1422df8bae1dSRodney W. Grimes /*
1423df8bae1dSRodney W. Grimes  * Forward a packet.  If some error occurs return the sender
1424df8bae1dSRodney W. Grimes  * an icmp packet.  Note we can't always generate a meaningful
1425df8bae1dSRodney W. Grimes  * icmp message because icmp doesn't have a large enough repertoire
1426df8bae1dSRodney W. Grimes  * of codes and types.
1427df8bae1dSRodney W. Grimes  *
1428df8bae1dSRodney W. Grimes  * If not forwarding, just drop the packet.  This could be confusing
1429df8bae1dSRodney W. Grimes  * if ipforwarding was zero but some routing protocol was advancing
1430df8bae1dSRodney W. Grimes  * us as a gateway to somewhere.  However, we must let the routing
1431df8bae1dSRodney W. Grimes  * protocol deal with that.
1432df8bae1dSRodney W. Grimes  *
1433df8bae1dSRodney W. Grimes  * The srcrt parameter indicates whether the packet is being forwarded
1434df8bae1dSRodney W. Grimes  * via a source route.
1435df8bae1dSRodney W. Grimes  */
14369b932e9eSAndre Oppermann void
14379b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt)
1438df8bae1dSRodney W. Grimes {
14392b25acc1SLuigi Rizzo 	struct ip *ip = mtod(m, struct ip *);
1440efbad259SEdward Tomasz Napierala 	struct in_ifaddr *ia;
1441df8bae1dSRodney W. Grimes 	struct mbuf *mcopy;
14429b932e9eSAndre Oppermann 	struct in_addr dest;
1443b835b6feSBjoern A. Zeeb 	struct route ro;
1444c773494eSAndre Oppermann 	int error, type = 0, code = 0, mtu = 0;
14453efc3014SJulian Elischer 
14469b932e9eSAndre Oppermann 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
144786425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
1448df8bae1dSRodney W. Grimes 		m_freem(m);
1449df8bae1dSRodney W. Grimes 		return;
1450df8bae1dSRodney W. Grimes 	}
1451*8922ddbeSAndrey V. Elsukov #ifdef IPSEC
1452*8922ddbeSAndrey V. Elsukov 	if (ip_ipsec_fwd(m) != 0) {
1453*8922ddbeSAndrey V. Elsukov 		IPSTAT_INC(ips_cantforward);
1454*8922ddbeSAndrey V. Elsukov 		m_freem(m);
1455*8922ddbeSAndrey V. Elsukov 		return;
1456*8922ddbeSAndrey V. Elsukov 	}
1457*8922ddbeSAndrey V. Elsukov #endif /* IPSEC */
14581b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
1459603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
14601b968362SDag-Erling Smørgrav #endif
1461df8bae1dSRodney W. Grimes 		if (ip->ip_ttl <= IPTTLDEC) {
14621b968362SDag-Erling Smørgrav 			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
146302c1c707SAndre Oppermann 			    0, 0);
1464df8bae1dSRodney W. Grimes 			return;
1465df8bae1dSRodney W. Grimes 		}
14661b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
14671b968362SDag-Erling Smørgrav 	}
14681b968362SDag-Erling Smørgrav #endif
1469df8bae1dSRodney W. Grimes 
14708b07e49aSJulian Elischer 	ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
1471efbad259SEdward Tomasz Napierala #ifndef IPSEC
1472efbad259SEdward Tomasz Napierala 	/*
1473efbad259SEdward Tomasz Napierala 	 * 'ia' may be NULL if there is no route for this destination.
1474efbad259SEdward Tomasz Napierala 	 * In case of IPsec, Don't discard it just yet, but pass it to
1475efbad259SEdward Tomasz Napierala 	 * ip_output in case of outgoing IPsec policy.
1476efbad259SEdward Tomasz Napierala 	 */
1477d23d475fSGuido van Rooij 	if (!srcrt && ia == NULL) {
147802c1c707SAndre Oppermann 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1479df8bae1dSRodney W. Grimes 		return;
148002c1c707SAndre Oppermann 	}
1481efbad259SEdward Tomasz Napierala #endif
1482df8bae1dSRodney W. Grimes 
1483df8bae1dSRodney W. Grimes 	/*
1484bfef7ed4SIan Dowse 	 * Save the IP header and at most 8 bytes of the payload,
1485bfef7ed4SIan Dowse 	 * in case we need to generate an ICMP message to the src.
1486bfef7ed4SIan Dowse 	 *
14874d2e3692SLuigi Rizzo 	 * XXX this can be optimized a lot by saving the data in a local
14884d2e3692SLuigi Rizzo 	 * buffer on the stack (72 bytes at most), and only allocating the
14894d2e3692SLuigi Rizzo 	 * mbuf if really necessary. The vast majority of the packets
14904d2e3692SLuigi Rizzo 	 * are forwarded without having to send an ICMP back (either
14914d2e3692SLuigi Rizzo 	 * because unnecessary, or because rate limited), so we are
14924d2e3692SLuigi Rizzo 	 * really we are wasting a lot of work here.
14934d2e3692SLuigi Rizzo 	 *
1494bfef7ed4SIan Dowse 	 * We don't use m_copy() because it might return a reference
1495bfef7ed4SIan Dowse 	 * to a shared cluster. Both this function and ip_output()
1496bfef7ed4SIan Dowse 	 * assume exclusive access to the IP header in `m', so any
1497bfef7ed4SIan Dowse 	 * data in a cluster may change before we reach icmp_error().
1498df8bae1dSRodney W. Grimes 	 */
1499dc4ad05eSGleb Smirnoff 	mcopy = m_gethdr(M_NOWAIT, m->m_type);
1500eb1b1807SGleb Smirnoff 	if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) {
15019967cafcSSam Leffler 		/*
15029967cafcSSam Leffler 		 * It's probably ok if the pkthdr dup fails (because
15039967cafcSSam Leffler 		 * the deep copy of the tag chain failed), but for now
15049967cafcSSam Leffler 		 * be conservative and just discard the copy since
15059967cafcSSam Leffler 		 * code below may some day want the tags.
15069967cafcSSam Leffler 		 */
15079967cafcSSam Leffler 		m_free(mcopy);
15089967cafcSSam Leffler 		mcopy = NULL;
15099967cafcSSam Leffler 	}
1510bfef7ed4SIan Dowse 	if (mcopy != NULL) {
15118f134647SGleb Smirnoff 		mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy));
1512e6b0a570SBruce M Simpson 		mcopy->m_pkthdr.len = mcopy->m_len;
1513bfef7ed4SIan Dowse 		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1514bfef7ed4SIan Dowse 	}
151504287599SRuslan Ermilov 
151604287599SRuslan Ermilov #ifdef IPSTEALTH
1517603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
151804287599SRuslan Ermilov #endif
151904287599SRuslan Ermilov 		ip->ip_ttl -= IPTTLDEC;
152004287599SRuslan Ermilov #ifdef IPSTEALTH
152104287599SRuslan Ermilov 	}
152204287599SRuslan Ermilov #endif
1523df8bae1dSRodney W. Grimes 
1524df8bae1dSRodney W. Grimes 	/*
1525df8bae1dSRodney W. Grimes 	 * If forwarding packet using same interface that it came in on,
1526df8bae1dSRodney W. Grimes 	 * perhaps should send a redirect to sender to shortcut a hop.
1527df8bae1dSRodney W. Grimes 	 * Only send redirect if source is sending directly to us,
1528df8bae1dSRodney W. Grimes 	 * and if packet was not source routed (or has any options).
1529df8bae1dSRodney W. Grimes 	 * Also, don't send redirect if forwarding using a default route
1530df8bae1dSRodney W. Grimes 	 * or a route modified by a redirect.
1531df8bae1dSRodney W. Grimes 	 */
15329b932e9eSAndre Oppermann 	dest.s_addr = 0;
1533efbad259SEdward Tomasz Napierala 	if (!srcrt && V_ipsendredirects &&
1534efbad259SEdward Tomasz Napierala 	    ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) {
153502c1c707SAndre Oppermann 		struct sockaddr_in *sin;
153602c1c707SAndre Oppermann 		struct rtentry *rt;
153702c1c707SAndre Oppermann 
15380cfbbe3bSAndre Oppermann 		bzero(&ro, sizeof(ro));
153902c1c707SAndre Oppermann 		sin = (struct sockaddr_in *)&ro.ro_dst;
154002c1c707SAndre Oppermann 		sin->sin_family = AF_INET;
154102c1c707SAndre Oppermann 		sin->sin_len = sizeof(*sin);
15429b932e9eSAndre Oppermann 		sin->sin_addr = ip->ip_dst;
15436e6b3f7cSQing Li 		in_rtalloc_ign(&ro, 0, M_GETFIB(m));
154402c1c707SAndre Oppermann 
154502c1c707SAndre Oppermann 		rt = ro.ro_rt;
154602c1c707SAndre Oppermann 
154702c1c707SAndre Oppermann 		if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
15489b932e9eSAndre Oppermann 		    satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1549df8bae1dSRodney W. Grimes #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1550df8bae1dSRodney W. Grimes 			u_long src = ntohl(ip->ip_src.s_addr);
1551df8bae1dSRodney W. Grimes 
1552df8bae1dSRodney W. Grimes 			if (RTA(rt) &&
1553df8bae1dSRodney W. Grimes 			    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1554df8bae1dSRodney W. Grimes 				if (rt->rt_flags & RTF_GATEWAY)
15559b932e9eSAndre Oppermann 					dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1556df8bae1dSRodney W. Grimes 				else
15579b932e9eSAndre Oppermann 					dest.s_addr = ip->ip_dst.s_addr;
1558df8bae1dSRodney W. Grimes 				/* Router requirements says to only send host redirects */
1559df8bae1dSRodney W. Grimes 				type = ICMP_REDIRECT;
1560df8bae1dSRodney W. Grimes 				code = ICMP_REDIRECT_HOST;
1561df8bae1dSRodney W. Grimes 			}
1562df8bae1dSRodney W. Grimes 		}
156302c1c707SAndre Oppermann 		if (rt)
156402c1c707SAndre Oppermann 			RTFREE(rt);
156502c1c707SAndre Oppermann 	}
1566df8bae1dSRodney W. Grimes 
1567b835b6feSBjoern A. Zeeb 	/*
1568b835b6feSBjoern A. Zeeb 	 * Try to cache the route MTU from ip_output so we can consider it for
1569b835b6feSBjoern A. Zeeb 	 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191.
1570b835b6feSBjoern A. Zeeb 	 */
1571b835b6feSBjoern A. Zeeb 	bzero(&ro, sizeof(ro));
1572b835b6feSBjoern A. Zeeb 
1573b835b6feSBjoern A. Zeeb 	error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1574b835b6feSBjoern A. Zeeb 
1575b835b6feSBjoern A. Zeeb 	if (error == EMSGSIZE && ro.ro_rt)
1576e3a7aa6fSGleb Smirnoff 		mtu = ro.ro_rt->rt_mtu;
1577bf984051SGleb Smirnoff 	RO_RTFREE(&ro);
1578b835b6feSBjoern A. Zeeb 
1579df8bae1dSRodney W. Grimes 	if (error)
158086425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
1581df8bae1dSRodney W. Grimes 	else {
158286425c62SRobert Watson 		IPSTAT_INC(ips_forward);
1583df8bae1dSRodney W. Grimes 		if (type)
158486425c62SRobert Watson 			IPSTAT_INC(ips_redirectsent);
1585df8bae1dSRodney W. Grimes 		else {
15869188b4a1SAndre Oppermann 			if (mcopy)
1587df8bae1dSRodney W. Grimes 				m_freem(mcopy);
15888c0fec80SRobert Watson 			if (ia != NULL)
15898c0fec80SRobert Watson 				ifa_free(&ia->ia_ifa);
1590df8bae1dSRodney W. Grimes 			return;
1591df8bae1dSRodney W. Grimes 		}
1592df8bae1dSRodney W. Grimes 	}
15938c0fec80SRobert Watson 	if (mcopy == NULL) {
15948c0fec80SRobert Watson 		if (ia != NULL)
15958c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
1596df8bae1dSRodney W. Grimes 		return;
15978c0fec80SRobert Watson 	}
1598df8bae1dSRodney W. Grimes 
1599df8bae1dSRodney W. Grimes 	switch (error) {
1600df8bae1dSRodney W. Grimes 
1601df8bae1dSRodney W. Grimes 	case 0:				/* forwarded, but need redirect */
1602df8bae1dSRodney W. Grimes 		/* type, code set above */
1603df8bae1dSRodney W. Grimes 		break;
1604df8bae1dSRodney W. Grimes 
1605efbad259SEdward Tomasz Napierala 	case ENETUNREACH:
1606df8bae1dSRodney W. Grimes 	case EHOSTUNREACH:
1607df8bae1dSRodney W. Grimes 	case ENETDOWN:
1608df8bae1dSRodney W. Grimes 	case EHOSTDOWN:
1609df8bae1dSRodney W. Grimes 	default:
1610df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1611df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_HOST;
1612df8bae1dSRodney W. Grimes 		break;
1613df8bae1dSRodney W. Grimes 
1614df8bae1dSRodney W. Grimes 	case EMSGSIZE:
1615df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1616df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_NEEDFRAG;
16171dfcf0d2SAndre Oppermann 
1618b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
1619b835b6feSBjoern A. Zeeb 		/*
1620b835b6feSBjoern A. Zeeb 		 * If IPsec is configured for this path,
1621b835b6feSBjoern A. Zeeb 		 * override any possibly mtu value set by ip_output.
1622b835b6feSBjoern A. Zeeb 		 */
16231c044382SBjoern A. Zeeb 		mtu = ip_ipsec_mtu(mcopy, mtu);
1624b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
16259b932e9eSAndre Oppermann 		/*
1626b835b6feSBjoern A. Zeeb 		 * If the MTU was set before make sure we are below the
1627b835b6feSBjoern A. Zeeb 		 * interface MTU.
1628ab48768bSAndre Oppermann 		 * If the MTU wasn't set before use the interface mtu or
1629ab48768bSAndre Oppermann 		 * fall back to the next smaller mtu step compared to the
1630ab48768bSAndre Oppermann 		 * current packet size.
16319b932e9eSAndre Oppermann 		 */
1632b835b6feSBjoern A. Zeeb 		if (mtu != 0) {
1633b835b6feSBjoern A. Zeeb 			if (ia != NULL)
1634b835b6feSBjoern A. Zeeb 				mtu = min(mtu, ia->ia_ifp->if_mtu);
1635b835b6feSBjoern A. Zeeb 		} else {
1636ab48768bSAndre Oppermann 			if (ia != NULL)
1637c773494eSAndre Oppermann 				mtu = ia->ia_ifp->if_mtu;
1638ab48768bSAndre Oppermann 			else
16398f134647SGleb Smirnoff 				mtu = ip_next_mtu(ntohs(ip->ip_len), 0);
1640ab48768bSAndre Oppermann 		}
164186425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
1642df8bae1dSRodney W. Grimes 		break;
1643df8bae1dSRodney W. Grimes 
1644df8bae1dSRodney W. Grimes 	case ENOBUFS:
16453a06e3e0SRuslan Ermilov 	case EACCES:			/* ipfw denied packet */
16463a06e3e0SRuslan Ermilov 		m_freem(mcopy);
16478c0fec80SRobert Watson 		if (ia != NULL)
16488c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
16493a06e3e0SRuslan Ermilov 		return;
1650df8bae1dSRodney W. Grimes 	}
16518c0fec80SRobert Watson 	if (ia != NULL)
16528c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
1653c773494eSAndre Oppermann 	icmp_error(mcopy, type, code, dest.s_addr, mtu);
1654df8bae1dSRodney W. Grimes }
1655df8bae1dSRodney W. Grimes 
165682c23ebaSBill Fenner void
1657f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1658f2565d68SRobert Watson     struct mbuf *m)
165982c23ebaSBill Fenner {
16608b615593SMarko Zec 
1661be8a62e8SPoul-Henning Kamp 	if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1662be8a62e8SPoul-Henning Kamp 		struct bintime bt;
1663be8a62e8SPoul-Henning Kamp 
1664be8a62e8SPoul-Henning Kamp 		bintime(&bt);
1665be8a62e8SPoul-Henning Kamp 		if (inp->inp_socket->so_options & SO_BINTIME) {
1666be8a62e8SPoul-Henning Kamp 			*mp = sbcreatecontrol((caddr_t)&bt, sizeof(bt),
1667be8a62e8SPoul-Henning Kamp 			    SCM_BINTIME, SOL_SOCKET);
1668be8a62e8SPoul-Henning Kamp 			if (*mp)
1669be8a62e8SPoul-Henning Kamp 				mp = &(*mp)->m_next;
1670be8a62e8SPoul-Henning Kamp 		}
167182c23ebaSBill Fenner 		if (inp->inp_socket->so_options & SO_TIMESTAMP) {
167282c23ebaSBill Fenner 			struct timeval tv;
167382c23ebaSBill Fenner 
1674be8a62e8SPoul-Henning Kamp 			bintime2timeval(&bt, &tv);
167582c23ebaSBill Fenner 			*mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv),
167682c23ebaSBill Fenner 			    SCM_TIMESTAMP, SOL_SOCKET);
167782c23ebaSBill Fenner 			if (*mp)
167882c23ebaSBill Fenner 				mp = &(*mp)->m_next;
16794cc20ab1SSeigo Tanimura 		}
1680be8a62e8SPoul-Henning Kamp 	}
168182c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVDSTADDR) {
168282c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t)&ip->ip_dst,
168382c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
168482c23ebaSBill Fenner 		if (*mp)
168582c23ebaSBill Fenner 			mp = &(*mp)->m_next;
168682c23ebaSBill Fenner 	}
16874957466bSMatthew N. Dodd 	if (inp->inp_flags & INP_RECVTTL) {
16884957466bSMatthew N. Dodd 		*mp = sbcreatecontrol((caddr_t)&ip->ip_ttl,
16894957466bSMatthew N. Dodd 		    sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
16904957466bSMatthew N. Dodd 		if (*mp)
16914957466bSMatthew N. Dodd 			mp = &(*mp)->m_next;
16924957466bSMatthew N. Dodd 	}
169382c23ebaSBill Fenner #ifdef notyet
169482c23ebaSBill Fenner 	/* XXX
169582c23ebaSBill Fenner 	 * Moving these out of udp_input() made them even more broken
169682c23ebaSBill Fenner 	 * than they already were.
169782c23ebaSBill Fenner 	 */
169882c23ebaSBill Fenner 	/* options were tossed already */
169982c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVOPTS) {
170082c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t)opts_deleted_above,
170182c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
170282c23ebaSBill Fenner 		if (*mp)
170382c23ebaSBill Fenner 			mp = &(*mp)->m_next;
170482c23ebaSBill Fenner 	}
170582c23ebaSBill Fenner 	/* ip_srcroute doesn't do what we want here, need to fix */
170682c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVRETOPTS) {
1707e0982661SAndre Oppermann 		*mp = sbcreatecontrol((caddr_t)ip_srcroute(m),
170882c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
170982c23ebaSBill Fenner 		if (*mp)
171082c23ebaSBill Fenner 			mp = &(*mp)->m_next;
171182c23ebaSBill Fenner 	}
171282c23ebaSBill Fenner #endif
171382c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVIF) {
1714d314ad7bSJulian Elischer 		struct ifnet *ifp;
1715d314ad7bSJulian Elischer 		struct sdlbuf {
171682c23ebaSBill Fenner 			struct sockaddr_dl sdl;
1717d314ad7bSJulian Elischer 			u_char	pad[32];
1718d314ad7bSJulian Elischer 		} sdlbuf;
1719d314ad7bSJulian Elischer 		struct sockaddr_dl *sdp;
1720d314ad7bSJulian Elischer 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
172182c23ebaSBill Fenner 
172246f2df9cSSergey Kandaurov 		if ((ifp = m->m_pkthdr.rcvif) &&
172346f2df9cSSergey Kandaurov 		    ifp->if_index && ifp->if_index <= V_if_index) {
17244a0d6638SRuslan Ermilov 			sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1725d314ad7bSJulian Elischer 			/*
1726d314ad7bSJulian Elischer 			 * Change our mind and don't try copy.
1727d314ad7bSJulian Elischer 			 */
172846f2df9cSSergey Kandaurov 			if (sdp->sdl_family != AF_LINK ||
172946f2df9cSSergey Kandaurov 			    sdp->sdl_len > sizeof(sdlbuf)) {
1730d314ad7bSJulian Elischer 				goto makedummy;
1731d314ad7bSJulian Elischer 			}
1732d314ad7bSJulian Elischer 			bcopy(sdp, sdl2, sdp->sdl_len);
1733d314ad7bSJulian Elischer 		} else {
1734d314ad7bSJulian Elischer makedummy:
173546f2df9cSSergey Kandaurov 			sdl2->sdl_len =
173646f2df9cSSergey Kandaurov 			    offsetof(struct sockaddr_dl, sdl_data[0]);
1737d314ad7bSJulian Elischer 			sdl2->sdl_family = AF_LINK;
1738d314ad7bSJulian Elischer 			sdl2->sdl_index = 0;
1739d314ad7bSJulian Elischer 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1740d314ad7bSJulian Elischer 		}
1741d314ad7bSJulian Elischer 		*mp = sbcreatecontrol((caddr_t)sdl2, sdl2->sdl_len,
174282c23ebaSBill Fenner 		    IP_RECVIF, IPPROTO_IP);
174382c23ebaSBill Fenner 		if (*mp)
174482c23ebaSBill Fenner 			mp = &(*mp)->m_next;
174582c23ebaSBill Fenner 	}
17463cca425bSMichael Tuexen 	if (inp->inp_flags & INP_RECVTOS) {
17473cca425bSMichael Tuexen 		*mp = sbcreatecontrol((caddr_t)&ip->ip_tos,
17483cca425bSMichael Tuexen 		    sizeof(u_char), IP_RECVTOS, IPPROTO_IP);
17493cca425bSMichael Tuexen 		if (*mp)
17503cca425bSMichael Tuexen 			mp = &(*mp)->m_next;
17513cca425bSMichael Tuexen 	}
17529d3ddf43SAdrian Chadd 
17539d3ddf43SAdrian Chadd 	if (inp->inp_flags2 & INP_RECVFLOWID) {
17549d3ddf43SAdrian Chadd 		uint32_t flowid, flow_type;
17559d3ddf43SAdrian Chadd 
17569d3ddf43SAdrian Chadd 		flowid = m->m_pkthdr.flowid;
17579d3ddf43SAdrian Chadd 		flow_type = M_HASHTYPE_GET(m);
17589d3ddf43SAdrian Chadd 
17599d3ddf43SAdrian Chadd 		/*
17609d3ddf43SAdrian Chadd 		 * XXX should handle the failure of one or the
17619d3ddf43SAdrian Chadd 		 * other - don't populate both?
17629d3ddf43SAdrian Chadd 		 */
17639d3ddf43SAdrian Chadd 		*mp = sbcreatecontrol((caddr_t) &flowid,
17649d3ddf43SAdrian Chadd 		    sizeof(uint32_t), IP_FLOWID, IPPROTO_IP);
17659d3ddf43SAdrian Chadd 		if (*mp)
17669d3ddf43SAdrian Chadd 			mp = &(*mp)->m_next;
17679d3ddf43SAdrian Chadd 		*mp = sbcreatecontrol((caddr_t) &flow_type,
17689d3ddf43SAdrian Chadd 		    sizeof(uint32_t), IP_FLOWTYPE, IPPROTO_IP);
17699d3ddf43SAdrian Chadd 		if (*mp)
17709d3ddf43SAdrian Chadd 			mp = &(*mp)->m_next;
17719d3ddf43SAdrian Chadd 	}
17729d3ddf43SAdrian Chadd 
17739d3ddf43SAdrian Chadd #ifdef	RSS
17749d3ddf43SAdrian Chadd 	if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
17759d3ddf43SAdrian Chadd 		uint32_t flowid, flow_type;
17769d3ddf43SAdrian Chadd 		uint32_t rss_bucketid;
17779d3ddf43SAdrian Chadd 
17789d3ddf43SAdrian Chadd 		flowid = m->m_pkthdr.flowid;
17799d3ddf43SAdrian Chadd 		flow_type = M_HASHTYPE_GET(m);
17809d3ddf43SAdrian Chadd 
17819d3ddf43SAdrian Chadd 		if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
17829d3ddf43SAdrian Chadd 			*mp = sbcreatecontrol((caddr_t) &rss_bucketid,
17839d3ddf43SAdrian Chadd 			   sizeof(uint32_t), IP_RSSBUCKETID, IPPROTO_IP);
17849d3ddf43SAdrian Chadd 			if (*mp)
17859d3ddf43SAdrian Chadd 				mp = &(*mp)->m_next;
17869d3ddf43SAdrian Chadd 		}
17879d3ddf43SAdrian Chadd 	}
17889d3ddf43SAdrian Chadd #endif
178982c23ebaSBill Fenner }
179082c23ebaSBill Fenner 
17914d2e3692SLuigi Rizzo /*
179230916a2dSRobert Watson  * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
179330916a2dSRobert Watson  * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
179430916a2dSRobert Watson  * locking.  This code remains in ip_input.c as ip_mroute.c is optionally
179530916a2dSRobert Watson  * compiled.
17964d2e3692SLuigi Rizzo  */
17973e288e62SDimitry Andric static VNET_DEFINE(int, ip_rsvp_on);
179882cea7e6SBjoern A. Zeeb VNET_DEFINE(struct socket *, ip_rsvpd);
179982cea7e6SBjoern A. Zeeb 
180082cea7e6SBjoern A. Zeeb #define	V_ip_rsvp_on		VNET(ip_rsvp_on)
180182cea7e6SBjoern A. Zeeb 
1802df8bae1dSRodney W. Grimes int
1803f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so)
1804f0068c4aSGarrett Wollman {
18058b615593SMarko Zec 
1806f0068c4aSGarrett Wollman 	if (so->so_type != SOCK_RAW ||
1807f0068c4aSGarrett Wollman 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1808f0068c4aSGarrett Wollman 		return EOPNOTSUPP;
1809f0068c4aSGarrett Wollman 
1810603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL)
1811f0068c4aSGarrett Wollman 		return EADDRINUSE;
1812f0068c4aSGarrett Wollman 
1813603724d3SBjoern A. Zeeb 	V_ip_rsvpd = so;
18141c5de19aSGarrett Wollman 	/*
18151c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-increment
18161c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
18171c5de19aSGarrett Wollman 	 */
1818603724d3SBjoern A. Zeeb 	if (!V_ip_rsvp_on) {
1819603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 1;
1820603724d3SBjoern A. Zeeb 		V_rsvp_on++;
18211c5de19aSGarrett Wollman 	}
1822f0068c4aSGarrett Wollman 
1823f0068c4aSGarrett Wollman 	return 0;
1824f0068c4aSGarrett Wollman }
1825f0068c4aSGarrett Wollman 
1826f0068c4aSGarrett Wollman int
1827f0068c4aSGarrett Wollman ip_rsvp_done(void)
1828f0068c4aSGarrett Wollman {
18298b615593SMarko Zec 
1830603724d3SBjoern A. Zeeb 	V_ip_rsvpd = NULL;
18311c5de19aSGarrett Wollman 	/*
18321c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-decrement
18331c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
18341c5de19aSGarrett Wollman 	 */
1835603724d3SBjoern A. Zeeb 	if (V_ip_rsvp_on) {
1836603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 0;
1837603724d3SBjoern A. Zeeb 		V_rsvp_on--;
18381c5de19aSGarrett Wollman 	}
1839f0068c4aSGarrett Wollman 	return 0;
1840f0068c4aSGarrett Wollman }
1841bbb4330bSLuigi Rizzo 
18428f5a8818SKevin Lo int
18438f5a8818SKevin Lo rsvp_input(struct mbuf **mp, int *offp, int proto)
1844bbb4330bSLuigi Rizzo {
18458f5a8818SKevin Lo 	struct mbuf *m;
18468f5a8818SKevin Lo 
18478f5a8818SKevin Lo 	m = *mp;
18488f5a8818SKevin Lo 	*mp = NULL;
18498b615593SMarko Zec 
1850bbb4330bSLuigi Rizzo 	if (rsvp_input_p) { /* call the real one if loaded */
18518f5a8818SKevin Lo 		*mp = m;
18528f5a8818SKevin Lo 		rsvp_input_p(mp, offp, proto);
18538f5a8818SKevin Lo 		return (IPPROTO_DONE);
1854bbb4330bSLuigi Rizzo 	}
1855bbb4330bSLuigi Rizzo 
1856bbb4330bSLuigi Rizzo 	/* Can still get packets with rsvp_on = 0 if there is a local member
1857bbb4330bSLuigi Rizzo 	 * of the group to which the RSVP packet is addressed.  But in this
1858bbb4330bSLuigi Rizzo 	 * case we want to throw the packet away.
1859bbb4330bSLuigi Rizzo 	 */
1860bbb4330bSLuigi Rizzo 
1861603724d3SBjoern A. Zeeb 	if (!V_rsvp_on) {
1862bbb4330bSLuigi Rizzo 		m_freem(m);
18638f5a8818SKevin Lo 		return (IPPROTO_DONE);
1864bbb4330bSLuigi Rizzo 	}
1865bbb4330bSLuigi Rizzo 
1866603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL) {
18678f5a8818SKevin Lo 		*mp = m;
18688f5a8818SKevin Lo 		rip_input(mp, offp, proto);
18698f5a8818SKevin Lo 		return (IPPROTO_DONE);
1870bbb4330bSLuigi Rizzo 	}
1871bbb4330bSLuigi Rizzo 	/* Drop the packet */
1872bbb4330bSLuigi Rizzo 	m_freem(m);
18738f5a8818SKevin Lo 	return (IPPROTO_DONE);
1874bbb4330bSLuigi Rizzo }
1875