xref: /freebsd/sys/netinet/ip_input.c (revision 4d3dfd450a42dd28864f910f526685f4784f4ddf)
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"
3957f60867SMark Johnston #include "opt_kdtrace.h"
4033553d6eSBjoern A. Zeeb #include "opt_route.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>
6565111ec7SKip Macy #include <net/flowtable.h>
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes #include <netinet/in.h>
6857f60867SMark Johnston #include <netinet/in_kdtrace.h>
69df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
70b5e8ce9fSBruce Evans #include <netinet/in_var.h>
71df8bae1dSRodney W. Grimes #include <netinet/ip.h>
72df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
73df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
74eddfbb76SRobert Watson #include <netinet/ip_fw.h>
75df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
76ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
7758938916SGarrett Wollman #include <machine/in_cksum.h>
78a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
79b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
801dfcf0d2SAndre Oppermann #include <netinet/ip_ipsec.h>
81b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
82df8bae1dSRodney W. Grimes 
83f0068c4aSGarrett Wollman #include <sys/socketvar.h>
846ddbf1e2SGary Palmer 
85aed55708SRobert Watson #include <security/mac/mac_framework.h>
86aed55708SRobert Watson 
87d2035ffbSEd Maste #ifdef CTASSERT
88d2035ffbSEd Maste CTASSERT(sizeof(struct ip) == 20);
89d2035ffbSEd Maste #endif
90d2035ffbSEd Maste 
91f89d4c3aSAndre Oppermann struct	rwlock in_ifaddr_lock;
9264aeca7bSRobert Watson RW_SYSINIT(in_ifaddr_lock, &in_ifaddr_lock, "in_ifaddr_lock");
93f0068c4aSGarrett Wollman 
9482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, rsvp_on);
9582cea7e6SBjoern A. Zeeb 
9682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ipforwarding);
97eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
98eddfbb76SRobert Watson     &VNET_NAME(ipforwarding), 0,
998b615593SMarko Zec     "Enable IP forwarding between interfaces");
1000312fbe9SPoul-Henning Kamp 
1013e288e62SDimitry Andric static VNET_DEFINE(int, ipsendredirects) = 1;	/* XXX */
10282cea7e6SBjoern A. Zeeb #define	V_ipsendredirects	VNET(ipsendredirects)
103eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
104eddfbb76SRobert Watson     &VNET_NAME(ipsendredirects), 0,
1058b615593SMarko Zec     "Enable sending IP redirects");
1060312fbe9SPoul-Henning Kamp 
1073e288e62SDimitry Andric static VNET_DEFINE(int, ip_keepfaith);
10882cea7e6SBjoern A. Zeeb #define	V_ip_keepfaith		VNET(ip_keepfaith)
109eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
110eddfbb76SRobert Watson     &VNET_NAME(ip_keepfaith), 0,
1116a800098SYoshinobu Inoue     "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
1126a800098SYoshinobu Inoue 
1133e288e62SDimitry Andric static VNET_DEFINE(int, ip_sendsourcequench);
11482cea7e6SBjoern A. Zeeb #define	V_ip_sendsourcequench	VNET(ip_sendsourcequench)
115eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW,
116eddfbb76SRobert Watson     &VNET_NAME(ip_sendsourcequench), 0,
117df285b3dSMike Silbersack     "Enable the transmission of source quench packets");
118df285b3dSMike Silbersack 
11982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip_do_randomid);
120eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW,
121eddfbb76SRobert Watson     &VNET_NAME(ip_do_randomid), 0,
122eddfbb76SRobert Watson     "Assign random ip_id values");
1231f44b0a1SDavid Malone 
124823db0e9SDon Lewis /*
125823db0e9SDon Lewis  * XXX - Setting ip_checkinterface mostly implements the receive side of
126823db0e9SDon Lewis  * the Strong ES model described in RFC 1122, but since the routing table
127a8f12100SDon Lewis  * and transmit implementation do not implement the Strong ES model,
128823db0e9SDon Lewis  * setting this to 1 results in an odd hybrid.
1293f67c834SDon Lewis  *
130a8f12100SDon Lewis  * XXX - ip_checkinterface currently must be disabled if you use ipnat
131a8f12100SDon Lewis  * to translate the destination address to another local interface.
1323f67c834SDon Lewis  *
1333f67c834SDon Lewis  * XXX - ip_checkinterface must be disabled if you add IP aliases
1343f67c834SDon Lewis  * to the loopback interface instead of the interface where the
1353f67c834SDon Lewis  * packets for those addresses are received.
136823db0e9SDon Lewis  */
1373e288e62SDimitry Andric static VNET_DEFINE(int, ip_checkinterface);
13882cea7e6SBjoern A. Zeeb #define	V_ip_checkinterface	VNET(ip_checkinterface)
139eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
140eddfbb76SRobert Watson     &VNET_NAME(ip_checkinterface), 0,
1418b615593SMarko Zec     "Verify packet arrives on correct interface");
142b3e95d4eSJonathan Lemon 
1430b4b0b0fSJulian Elischer VNET_DEFINE(struct pfil_head, inet_pfil_hook);	/* Packet filter hooks */
144df8bae1dSRodney W. Grimes 
145d4b5cae4SRobert Watson static struct netisr_handler ip_nh = {
146d4b5cae4SRobert Watson 	.nh_name = "ip",
147d4b5cae4SRobert Watson 	.nh_handler = ip_input,
148d4b5cae4SRobert Watson 	.nh_proto = NETISR_IP,
149d4b5cae4SRobert Watson 	.nh_policy = NETISR_POLICY_FLOW,
150d4b5cae4SRobert Watson };
151ca925d9cSJonathan Lemon 
152df8bae1dSRodney W. Grimes extern	struct domain inetdomain;
153f0ffb944SJulian Elischer extern	struct protosw inetsw[];
154df8bae1dSRodney W. Grimes u_char	ip_protox[IPPROTO_MAX];
15582cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead);  /* first inet address */
15682cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); /* inet addr hash table  */
15782cea7e6SBjoern A. Zeeb VNET_DEFINE(u_long, in_ifaddrhmask);		/* mask for hash table */
158ca925d9cSJonathan Lemon 
1593e288e62SDimitry Andric static VNET_DEFINE(uma_zone_t, ipq_zone);
1603e288e62SDimitry Andric static VNET_DEFINE(TAILQ_HEAD(ipqhead, ipq), ipq[IPREASS_NHASH]);
161f89d4c3aSAndre Oppermann static struct mtx ipqlock;
1622fad1e93SSam Leffler 
16382cea7e6SBjoern A. Zeeb #define	V_ipq_zone		VNET(ipq_zone)
16482cea7e6SBjoern A. Zeeb #define	V_ipq			VNET(ipq)
16582cea7e6SBjoern A. Zeeb 
1662fad1e93SSam Leffler #define	IPQ_LOCK()	mtx_lock(&ipqlock)
1672fad1e93SSam Leffler #define	IPQ_UNLOCK()	mtx_unlock(&ipqlock)
168888c2a3cSSam Leffler #define	IPQ_LOCK_INIT()	mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
169888c2a3cSSam Leffler #define	IPQ_LOCK_ASSERT()	mtx_assert(&ipqlock, MA_OWNED)
170f23b4c91SGarrett Wollman 
171d248c7d7SRobert Watson static void	maxnipq_update(void);
1724f590175SPaul Saab static void	ipq_zone_change(void *);
1739802380eSBjoern A. Zeeb static void	ip_drain_locked(void);
174d248c7d7SRobert Watson 
1753e288e62SDimitry Andric static VNET_DEFINE(int, maxnipq);  /* Administrative limit on # reass queues. */
1763e288e62SDimitry Andric static VNET_DEFINE(int, nipq);			/* Total # of reass queues */
17782cea7e6SBjoern A. Zeeb #define	V_maxnipq		VNET(maxnipq)
17882cea7e6SBjoern A. Zeeb #define	V_nipq			VNET(nipq)
179eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_RD,
180eddfbb76SRobert Watson     &VNET_NAME(nipq), 0,
1818b615593SMarko Zec     "Current number of IPv4 fragment reassembly queue entries");
182d248c7d7SRobert Watson 
1833e288e62SDimitry Andric static VNET_DEFINE(int, maxfragsperpacket);
18482cea7e6SBjoern A. Zeeb #define	V_maxfragsperpacket	VNET(maxfragsperpacket)
185eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW,
186eddfbb76SRobert Watson     &VNET_NAME(maxfragsperpacket), 0,
187d248c7d7SRobert Watson     "Maximum number of IPv4 fragments allowed per packet");
188d248c7d7SRobert Watson 
1890312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU
1900312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
1913d177f46SBill Fumerola     &ip_mtu, 0, "Default MTU");
1920312fbe9SPoul-Henning Kamp #endif
1930312fbe9SPoul-Henning Kamp 
1941b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
19582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ipstealth);
196eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
197eddfbb76SRobert Watson     &VNET_NAME(ipstealth), 0,
198eddfbb76SRobert Watson     "IP stealth mode, no TTL decrementation on forwarding");
1991b968362SDag-Erling Smørgrav #endif
200eddfbb76SRobert Watson 
20153be8fcaSBjoern A. Zeeb #ifdef FLOWTABLE
2023e288e62SDimitry Andric static VNET_DEFINE(int, ip_output_flowtable_size) = 2048;
203eddfbb76SRobert Watson VNET_DEFINE(struct flowtable *, ip_ft);
2041e77c105SRobert Watson #define	V_ip_output_flowtable_size	VNET(ip_output_flowtable_size)
205eddfbb76SRobert Watson 
206eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, output_flowtable_size, CTLFLAG_RDTUN,
207eddfbb76SRobert Watson     &VNET_NAME(ip_output_flowtable_size), 2048,
20865111ec7SKip Macy     "number of entries in the per-cpu output flow caches");
20953be8fcaSBjoern A. Zeeb #endif
21053be8fcaSBjoern A. Zeeb 
2114d77a549SAlfred Perlstein static void	ip_freef(struct ipqhead *, struct ipq *);
2128948e4baSArchie Cobbs 
213315e3e38SRobert Watson /*
2145da0521fSAndrey V. Elsukov  * IP statistics are stored in the "array" of counter(9)s.
2155923c293SGleb Smirnoff  */
2165da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct ipstat, ipstat);
2175da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(ipstat);
2185da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_ip, IPCTL_STATS, stats, struct ipstat, ipstat,
2195da0521fSAndrey V. Elsukov     "IP statistics (struct ipstat, netinet/ip_var.h)");
2205923c293SGleb Smirnoff 
2215923c293SGleb Smirnoff #ifdef VIMAGE
2225da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(ipstat);
2235923c293SGleb Smirnoff #endif /* VIMAGE */
2245923c293SGleb Smirnoff 
2255923c293SGleb Smirnoff /*
226315e3e38SRobert Watson  * Kernel module interface for updating ipstat.  The argument is an index
2275923c293SGleb Smirnoff  * into ipstat treated as an array.
228315e3e38SRobert Watson  */
229315e3e38SRobert Watson void
230315e3e38SRobert Watson kmod_ipstat_inc(int statnum)
231315e3e38SRobert Watson {
232315e3e38SRobert Watson 
2335da0521fSAndrey V. Elsukov 	counter_u64_add(VNET(ipstat)[statnum], 1);
234315e3e38SRobert Watson }
235315e3e38SRobert Watson 
236315e3e38SRobert Watson void
237315e3e38SRobert Watson kmod_ipstat_dec(int statnum)
238315e3e38SRobert Watson {
239315e3e38SRobert Watson 
2405da0521fSAndrey V. Elsukov 	counter_u64_add(VNET(ipstat)[statnum], -1);
241315e3e38SRobert Watson }
242315e3e38SRobert Watson 
243d4b5cae4SRobert Watson static int
244d4b5cae4SRobert Watson sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
245d4b5cae4SRobert Watson {
246d4b5cae4SRobert Watson 	int error, qlimit;
247d4b5cae4SRobert Watson 
248d4b5cae4SRobert Watson 	netisr_getqlimit(&ip_nh, &qlimit);
249d4b5cae4SRobert Watson 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
250d4b5cae4SRobert Watson 	if (error || !req->newptr)
251d4b5cae4SRobert Watson 		return (error);
252d4b5cae4SRobert Watson 	if (qlimit < 1)
253d4b5cae4SRobert Watson 		return (EINVAL);
254d4b5cae4SRobert Watson 	return (netisr_setqlimit(&ip_nh, qlimit));
255d4b5cae4SRobert Watson }
256d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen,
257d4b5cae4SRobert Watson     CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_queue_maxlen, "I",
258d4b5cae4SRobert Watson     "Maximum size of the IP input queue");
259d4b5cae4SRobert Watson 
260d4b5cae4SRobert Watson static int
261d4b5cae4SRobert Watson sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS)
262d4b5cae4SRobert Watson {
263d4b5cae4SRobert Watson 	u_int64_t qdrops_long;
264d4b5cae4SRobert Watson 	int error, qdrops;
265d4b5cae4SRobert Watson 
266d4b5cae4SRobert Watson 	netisr_getqdrops(&ip_nh, &qdrops_long);
267d4b5cae4SRobert Watson 	qdrops = qdrops_long;
268d4b5cae4SRobert Watson 	error = sysctl_handle_int(oidp, &qdrops, 0, req);
269d4b5cae4SRobert Watson 	if (error || !req->newptr)
270d4b5cae4SRobert Watson 		return (error);
271d4b5cae4SRobert Watson 	if (qdrops != 0)
272d4b5cae4SRobert Watson 		return (EINVAL);
273d4b5cae4SRobert Watson 	netisr_clearqdrops(&ip_nh);
274d4b5cae4SRobert Watson 	return (0);
275d4b5cae4SRobert Watson }
276d4b5cae4SRobert Watson 
277d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops,
278d4b5cae4SRobert Watson     CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_queue_drops, "I",
279d4b5cae4SRobert Watson     "Number of packets dropped from the IP input queue");
280d4b5cae4SRobert Watson 
281df8bae1dSRodney W. Grimes /*
282df8bae1dSRodney W. Grimes  * IP initialization: fill in IP protocol switch table.
283df8bae1dSRodney W. Grimes  * All protocols not implemented in kernel go to raw IP protocol handler.
284df8bae1dSRodney W. Grimes  */
285df8bae1dSRodney W. Grimes void
286f2565d68SRobert Watson ip_init(void)
287df8bae1dSRodney W. Grimes {
288f2565d68SRobert Watson 	struct protosw *pr;
289f2565d68SRobert Watson 	int i;
290df8bae1dSRodney W. Grimes 
291a511354aSRobert Watson 	V_ip_id = time_second & 0xffff;
292a511354aSRobert Watson 
293603724d3SBjoern A. Zeeb 	TAILQ_INIT(&V_in_ifaddrhead);
294603724d3SBjoern A. Zeeb 	V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
2951ed81b73SMarko Zec 
2961ed81b73SMarko Zec 	/* Initialize IP reassembly queue. */
2971ed81b73SMarko Zec 	for (i = 0; i < IPREASS_NHASH; i++)
2981ed81b73SMarko Zec 		TAILQ_INIT(&V_ipq[i]);
2991ed81b73SMarko Zec 	V_maxnipq = nmbclusters / 32;
3001ed81b73SMarko Zec 	V_maxfragsperpacket = 16;
3011ed81b73SMarko Zec 	V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
3021ed81b73SMarko Zec 	    NULL, UMA_ALIGN_PTR, 0);
3031ed81b73SMarko Zec 	maxnipq_update();
3041ed81b73SMarko Zec 
3050b4b0b0fSJulian Elischer 	/* Initialize packet filter hooks. */
3060b4b0b0fSJulian Elischer 	V_inet_pfil_hook.ph_type = PFIL_TYPE_AF;
3070b4b0b0fSJulian Elischer 	V_inet_pfil_hook.ph_af = AF_INET;
3080b4b0b0fSJulian Elischer 	if ((i = pfil_head_register(&V_inet_pfil_hook)) != 0)
3090b4b0b0fSJulian Elischer 		printf("%s: WARNING: unable to register pfil hook, "
3100b4b0b0fSJulian Elischer 			"error %d\n", __func__, i);
3110b4b0b0fSJulian Elischer 
312fa057b15SMarko Zec #ifdef FLOWTABLE
3133059584eSKip Macy 	if (TUNABLE_INT_FETCH("net.inet.ip.output_flowtable_size",
3143059584eSKip Macy 		&V_ip_output_flowtable_size)) {
3153059584eSKip Macy 		if (V_ip_output_flowtable_size < 256)
3163059584eSKip Macy 			V_ip_output_flowtable_size = 256;
3173059584eSKip Macy 		if (!powerof2(V_ip_output_flowtable_size)) {
3183059584eSKip Macy 			printf("flowtable must be power of 2 size\n");
3193059584eSKip Macy 			V_ip_output_flowtable_size = 2048;
3203059584eSKip Macy 		}
3213059584eSKip Macy 	} else {
3223059584eSKip Macy 		/*
3233059584eSKip Macy 		 * round up to the next power of 2
3243059584eSKip Macy 		 */
3253059584eSKip Macy 		V_ip_output_flowtable_size = 1 << fls((1024 + maxusers * 64)-1);
3263059584eSKip Macy 	}
327d4121a02SKip Macy 	V_ip_ft = flowtable_alloc("ipv4", V_ip_output_flowtable_size, FL_PCPU);
328fa057b15SMarko Zec #endif
329fa057b15SMarko Zec 
3301ed81b73SMarko Zec 	/* Skip initialization of globals for non-default instances. */
3311ed81b73SMarko Zec 	if (!IS_DEFAULT_VNET(curvnet))
3321ed81b73SMarko Zec 		return;
3331ed81b73SMarko Zec 
334f0ffb944SJulian Elischer 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
33502410549SRobert Watson 	if (pr == NULL)
336db09bef3SAndre Oppermann 		panic("ip_init: PF_INET not found");
337db09bef3SAndre Oppermann 
338db09bef3SAndre Oppermann 	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
339df8bae1dSRodney W. Grimes 	for (i = 0; i < IPPROTO_MAX; i++)
340df8bae1dSRodney W. Grimes 		ip_protox[i] = pr - inetsw;
341db09bef3SAndre Oppermann 	/*
342db09bef3SAndre Oppermann 	 * Cycle through IP protocols and put them into the appropriate place
343db09bef3SAndre Oppermann 	 * in ip_protox[].
344db09bef3SAndre Oppermann 	 */
345f0ffb944SJulian Elischer 	for (pr = inetdomain.dom_protosw;
346f0ffb944SJulian Elischer 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
347df8bae1dSRodney W. Grimes 		if (pr->pr_domain->dom_family == PF_INET &&
348db09bef3SAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
349db09bef3SAndre Oppermann 			/* Be careful to only index valid IP protocols. */
350db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX)
351df8bae1dSRodney W. Grimes 				ip_protox[pr->pr_protocol] = pr - inetsw;
352db09bef3SAndre Oppermann 		}
353194a213eSAndrey A. Chernov 
3544f590175SPaul Saab 	EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
3554f590175SPaul Saab 		NULL, EVENTHANDLER_PRI_ANY);
3565f311da2SMike Silbersack 
357db09bef3SAndre Oppermann 	/* Initialize various other remaining things. */
3581ed81b73SMarko Zec 	IPQ_LOCK_INIT();
359d4b5cae4SRobert Watson 	netisr_register(&ip_nh);
360df8bae1dSRodney W. Grimes }
361df8bae1dSRodney W. Grimes 
3629802380eSBjoern A. Zeeb #ifdef VIMAGE
3639802380eSBjoern A. Zeeb void
3649802380eSBjoern A. Zeeb ip_destroy(void)
3659802380eSBjoern A. Zeeb {
366*4d3dfd45SMikolaj Golub 	int i;
367*4d3dfd45SMikolaj Golub 
368*4d3dfd45SMikolaj Golub 	if ((i = pfil_head_unregister(&V_inet_pfil_hook)) != 0)
369*4d3dfd45SMikolaj Golub 		printf("%s: WARNING: unable to unregister pfil hook, "
370*4d3dfd45SMikolaj Golub 		    "error %d\n", __func__, i);
3719802380eSBjoern A. Zeeb 
3729802380eSBjoern A. Zeeb 	/* Cleanup in_ifaddr hash table; should be empty. */
3739802380eSBjoern A. Zeeb 	hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask);
3749802380eSBjoern A. Zeeb 
3759802380eSBjoern A. Zeeb 	IPQ_LOCK();
3769802380eSBjoern A. Zeeb 	ip_drain_locked();
3779802380eSBjoern A. Zeeb 	IPQ_UNLOCK();
3789802380eSBjoern A. Zeeb 
3799802380eSBjoern A. Zeeb 	uma_zdestroy(V_ipq_zone);
3809802380eSBjoern A. Zeeb }
3819802380eSBjoern A. Zeeb #endif
3829802380eSBjoern A. Zeeb 
3834d2e3692SLuigi Rizzo /*
384df8bae1dSRodney W. Grimes  * Ip input routine.  Checksum and byte swap header.  If fragmented
385df8bae1dSRodney W. Grimes  * try to reassemble.  Process options.  Pass to next level.
386df8bae1dSRodney W. Grimes  */
387c67b1d17SGarrett Wollman void
388c67b1d17SGarrett Wollman ip_input(struct mbuf *m)
389df8bae1dSRodney W. Grimes {
3909188b4a1SAndre Oppermann 	struct ip *ip = NULL;
3915da9f8faSJosef Karthauser 	struct in_ifaddr *ia = NULL;
392ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
3930aade26eSRobert Watson 	struct ifnet *ifp;
3949b932e9eSAndre Oppermann 	int    checkif, hlen = 0;
39521d172a3SGleb Smirnoff 	uint16_t sum, ip_len;
39602c1c707SAndre Oppermann 	int dchg = 0;				/* dest changed after fw */
397f51f805fSSam Leffler 	struct in_addr odst;			/* original dst address */
398b715f178SLuigi Rizzo 
399fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
400db40007dSAndrew R. Reiter 
401ac9d7e26SMax Laier 	if (m->m_flags & M_FASTFWD_OURS) {
40276ff6dcfSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
40376ff6dcfSAndre Oppermann 		/* Set up some basics that will be used later. */
4042b25acc1SLuigi Rizzo 		ip = mtod(m, struct ip *);
40553be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
4068f134647SGleb Smirnoff 		ip_len = ntohs(ip->ip_len);
4079b932e9eSAndre Oppermann 		goto ours;
4082b25acc1SLuigi Rizzo 	}
4092b25acc1SLuigi Rizzo 
41086425c62SRobert Watson 	IPSTAT_INC(ips_total);
41158938916SGarrett Wollman 
41258938916SGarrett Wollman 	if (m->m_pkthdr.len < sizeof(struct ip))
41358938916SGarrett Wollman 		goto tooshort;
41458938916SGarrett Wollman 
415df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct ip) &&
4160b17fba7SAndre Oppermann 	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
41786425c62SRobert Watson 		IPSTAT_INC(ips_toosmall);
418c67b1d17SGarrett Wollman 		return;
419df8bae1dSRodney W. Grimes 	}
420df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
42158938916SGarrett Wollman 
42253be11f6SPoul-Henning Kamp 	if (ip->ip_v != IPVERSION) {
42386425c62SRobert Watson 		IPSTAT_INC(ips_badvers);
424df8bae1dSRodney W. Grimes 		goto bad;
425df8bae1dSRodney W. Grimes 	}
42658938916SGarrett Wollman 
42753be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
428df8bae1dSRodney W. Grimes 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
42986425c62SRobert Watson 		IPSTAT_INC(ips_badhlen);
430df8bae1dSRodney W. Grimes 		goto bad;
431df8bae1dSRodney W. Grimes 	}
432df8bae1dSRodney W. Grimes 	if (hlen > m->m_len) {
4330b17fba7SAndre Oppermann 		if ((m = m_pullup(m, hlen)) == NULL) {
43486425c62SRobert Watson 			IPSTAT_INC(ips_badhlen);
435c67b1d17SGarrett Wollman 			return;
436df8bae1dSRodney W. Grimes 		}
437df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
438df8bae1dSRodney W. Grimes 	}
43933841545SHajimu UMEMOTO 
44057f60867SMark Johnston 	IP_PROBE(receive, NULL, NULL, ip, m->m_pkthdr.rcvif, ip, NULL);
44157f60867SMark Johnston 
44233841545SHajimu UMEMOTO 	/* 127/8 must not appear on wire - RFC1122 */
4430aade26eSRobert Watson 	ifp = m->m_pkthdr.rcvif;
44433841545SHajimu UMEMOTO 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
44533841545SHajimu UMEMOTO 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
4460aade26eSRobert Watson 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
44786425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
44833841545SHajimu UMEMOTO 			goto bad;
44933841545SHajimu UMEMOTO 		}
45033841545SHajimu UMEMOTO 	}
45133841545SHajimu UMEMOTO 
452db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
453db4f9cc7SJonathan Lemon 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
454db4f9cc7SJonathan Lemon 	} else {
45558938916SGarrett Wollman 		if (hlen == sizeof(struct ip)) {
45647c861ecSBrian Somers 			sum = in_cksum_hdr(ip);
45758938916SGarrett Wollman 		} else {
45847c861ecSBrian Somers 			sum = in_cksum(m, hlen);
45958938916SGarrett Wollman 		}
460db4f9cc7SJonathan Lemon 	}
46147c861ecSBrian Somers 	if (sum) {
46286425c62SRobert Watson 		IPSTAT_INC(ips_badsum);
463df8bae1dSRodney W. Grimes 		goto bad;
464df8bae1dSRodney W. Grimes 	}
465df8bae1dSRodney W. Grimes 
46602b199f1SMax Laier #ifdef ALTQ
46702b199f1SMax Laier 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
46802b199f1SMax Laier 		/* packet is dropped by traffic conditioner */
46902b199f1SMax Laier 		return;
47002b199f1SMax Laier #endif
47102b199f1SMax Laier 
47221d172a3SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
47321d172a3SGleb Smirnoff 	if (ip_len < hlen) {
47486425c62SRobert Watson 		IPSTAT_INC(ips_badlen);
475df8bae1dSRodney W. Grimes 		goto bad;
476df8bae1dSRodney W. Grimes 	}
477df8bae1dSRodney W. Grimes 
478df8bae1dSRodney W. Grimes 	/*
479df8bae1dSRodney W. Grimes 	 * Check that the amount of data in the buffers
480df8bae1dSRodney W. Grimes 	 * is as at least much as the IP header would have us expect.
481df8bae1dSRodney W. Grimes 	 * Trim mbufs if longer than we expect.
482df8bae1dSRodney W. Grimes 	 * Drop packet if shorter than we expect.
483df8bae1dSRodney W. Grimes 	 */
48421d172a3SGleb Smirnoff 	if (m->m_pkthdr.len < ip_len) {
48558938916SGarrett Wollman tooshort:
48686425c62SRobert Watson 		IPSTAT_INC(ips_tooshort);
487df8bae1dSRodney W. Grimes 		goto bad;
488df8bae1dSRodney W. Grimes 	}
48921d172a3SGleb Smirnoff 	if (m->m_pkthdr.len > ip_len) {
490df8bae1dSRodney W. Grimes 		if (m->m_len == m->m_pkthdr.len) {
49121d172a3SGleb Smirnoff 			m->m_len = ip_len;
49221d172a3SGleb Smirnoff 			m->m_pkthdr.len = ip_len;
493df8bae1dSRodney W. Grimes 		} else
49421d172a3SGleb Smirnoff 			m_adj(m, ip_len - m->m_pkthdr.len);
495df8bae1dSRodney W. Grimes 	}
496b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
49714dd6717SSam Leffler 	/*
498ffe8cd7bSBjoern A. Zeeb 	 * Bypass packet filtering for packets previously handled by IPsec.
49914dd6717SSam Leffler 	 */
500cc977adcSBjoern A. Zeeb 	if (ip_ipsec_filtertunnel(m))
501c21fd232SAndre Oppermann 		goto passin;
502b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
5033f67c834SDon Lewis 
504c4ac87eaSDarren Reed 	/*
505134ea224SSam Leffler 	 * Run through list of hooks for input packets.
506f51f805fSSam Leffler 	 *
507f51f805fSSam Leffler 	 * NB: Beware of the destination address changing (e.g.
508f51f805fSSam Leffler 	 *     by NAT rewriting).  When this happens, tell
509f51f805fSSam Leffler 	 *     ip_forward to do the right thing.
510c4ac87eaSDarren Reed 	 */
511c21fd232SAndre Oppermann 
512c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
5130b4b0b0fSJulian Elischer 	if (!PFIL_HOOKED(&V_inet_pfil_hook))
514c21fd232SAndre Oppermann 		goto passin;
515c21fd232SAndre Oppermann 
516f51f805fSSam Leffler 	odst = ip->ip_dst;
5170b4b0b0fSJulian Elischer 	if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0)
518beec8214SDarren Reed 		return;
519134ea224SSam Leffler 	if (m == NULL)			/* consumed by filter */
520c4ac87eaSDarren Reed 		return;
5219b932e9eSAndre Oppermann 
522c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
52302c1c707SAndre Oppermann 	dchg = (odst.s_addr != ip->ip_dst.s_addr);
5240aade26eSRobert Watson 	ifp = m->m_pkthdr.rcvif;
5259b932e9eSAndre Oppermann 
5269b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
5279b932e9eSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
5289b932e9eSAndre Oppermann 		goto ours;
5299b932e9eSAndre Oppermann 	}
530ffdbf9daSAndrey V. Elsukov 	if (m->m_flags & M_IP_NEXTHOP) {
531ffdbf9daSAndrey V. Elsukov 		dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL);
532ffdbf9daSAndrey V. Elsukov 		if (dchg != 0) {
533099dd043SAndre Oppermann 			/*
534ffdbf9daSAndrey V. Elsukov 			 * Directly ship the packet on.  This allows
535ffdbf9daSAndrey V. Elsukov 			 * forwarding packets originally destined to us
536ffdbf9daSAndrey V. Elsukov 			 * to some other directly connected host.
537099dd043SAndre Oppermann 			 */
538ffdbf9daSAndrey V. Elsukov 			ip_forward(m, 1);
539099dd043SAndre Oppermann 			return;
540099dd043SAndre Oppermann 		}
541ffdbf9daSAndrey V. Elsukov 	}
542c21fd232SAndre Oppermann passin:
54321d172a3SGleb Smirnoff 
54421d172a3SGleb Smirnoff 	/*
545df8bae1dSRodney W. Grimes 	 * Process options and, if not destined for us,
546df8bae1dSRodney W. Grimes 	 * ship it on.  ip_dooptions returns 1 when an
547df8bae1dSRodney W. Grimes 	 * error was detected (causing an icmp message
548df8bae1dSRodney W. Grimes 	 * to be sent and the original packet to be freed).
549df8bae1dSRodney W. Grimes 	 */
5509b932e9eSAndre Oppermann 	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
551c67b1d17SGarrett Wollman 		return;
552df8bae1dSRodney W. Grimes 
553f0068c4aSGarrett Wollman         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
554f0068c4aSGarrett Wollman          * matter if it is destined to another node, or whether it is
555f0068c4aSGarrett Wollman          * a multicast one, RSVP wants it! and prevents it from being forwarded
556f0068c4aSGarrett Wollman          * anywhere else. Also checks if the rsvp daemon is running before
557f0068c4aSGarrett Wollman 	 * grabbing the packet.
558f0068c4aSGarrett Wollman          */
559603724d3SBjoern A. Zeeb 	if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP)
560f0068c4aSGarrett Wollman 		goto ours;
561f0068c4aSGarrett Wollman 
562df8bae1dSRodney W. Grimes 	/*
563df8bae1dSRodney W. Grimes 	 * Check our list of addresses, to see if the packet is for us.
564cc766e04SGarrett Wollman 	 * If we don't have any addresses, assume any unicast packet
565cc766e04SGarrett Wollman 	 * we receive might be for us (and let the upper layers deal
566cc766e04SGarrett Wollman 	 * with it).
567df8bae1dSRodney W. Grimes 	 */
568603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
569cc766e04SGarrett Wollman 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
570cc766e04SGarrett Wollman 		goto ours;
571cc766e04SGarrett Wollman 
5727538a9a0SJonathan Lemon 	/*
573823db0e9SDon Lewis 	 * Enable a consistency check between the destination address
574823db0e9SDon Lewis 	 * and the arrival interface for a unicast packet (the RFC 1122
575823db0e9SDon Lewis 	 * strong ES model) if IP forwarding is disabled and the packet
576e15ae1b2SDon Lewis 	 * is not locally generated and the packet is not subject to
577e15ae1b2SDon Lewis 	 * 'ipfw fwd'.
5783f67c834SDon Lewis 	 *
5793f67c834SDon Lewis 	 * XXX - Checking also should be disabled if the destination
5803f67c834SDon Lewis 	 * address is ipnat'ed to a different interface.
5813f67c834SDon Lewis 	 *
582a8f12100SDon Lewis 	 * XXX - Checking is incompatible with IP aliases added
5833f67c834SDon Lewis 	 * to the loopback interface instead of the interface where
5843f67c834SDon Lewis 	 * the packets are received.
585a9771948SGleb Smirnoff 	 *
586a9771948SGleb Smirnoff 	 * XXX - This is the case for carp vhost IPs as well so we
587a9771948SGleb Smirnoff 	 * insert a workaround. If the packet got here, we already
588a9771948SGleb Smirnoff 	 * checked with carp_iamatch() and carp_forus().
589823db0e9SDon Lewis 	 */
590603724d3SBjoern A. Zeeb 	checkif = V_ip_checkinterface && (V_ipforwarding == 0) &&
5910aade26eSRobert Watson 	    ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) &&
59254bfbd51SWill Andrews 	    ifp->if_carp == NULL && (dchg == 0);
593823db0e9SDon Lewis 
594ca925d9cSJonathan Lemon 	/*
595ca925d9cSJonathan Lemon 	 * Check for exact addresses in the hash bucket.
596ca925d9cSJonathan Lemon 	 */
5972d9cfabaSRobert Watson 	/* IN_IFADDR_RLOCK(); */
5989b932e9eSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
599f9e354dfSJulian Elischer 		/*
600823db0e9SDon Lewis 		 * If the address matches, verify that the packet
601823db0e9SDon Lewis 		 * arrived via the correct interface if checking is
602823db0e9SDon Lewis 		 * enabled.
603f9e354dfSJulian Elischer 		 */
6049b932e9eSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
6058c0fec80SRobert Watson 		    (!checkif || ia->ia_ifp == ifp)) {
6068c0fec80SRobert Watson 			ifa_ref(&ia->ia_ifa);
6072d9cfabaSRobert Watson 			/* IN_IFADDR_RUNLOCK(); */
608ed1ff184SJulian Elischer 			goto ours;
609ca925d9cSJonathan Lemon 		}
6108c0fec80SRobert Watson 	}
6112d9cfabaSRobert Watson 	/* IN_IFADDR_RUNLOCK(); */
6122d9cfabaSRobert Watson 
613823db0e9SDon Lewis 	/*
614ca925d9cSJonathan Lemon 	 * Check for broadcast addresses.
615ca925d9cSJonathan Lemon 	 *
616ca925d9cSJonathan Lemon 	 * Only accept broadcast packets that arrive via the matching
617ca925d9cSJonathan Lemon 	 * interface.  Reception of forwarded directed broadcasts would
618ca925d9cSJonathan Lemon 	 * be handled via ip_forward() and ether_output() with the loopback
619ca925d9cSJonathan Lemon 	 * into the stack for SIMPLEX interfaces handled by ether_output().
620823db0e9SDon Lewis 	 */
6210aade26eSRobert Watson 	if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
622137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
6230aade26eSRobert Watson 	        TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
624ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
625ca925d9cSJonathan Lemon 				continue;
626ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
627df8bae1dSRodney W. Grimes 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
6280aade26eSRobert Watson 			    ip->ip_dst.s_addr) {
6298c0fec80SRobert Watson 				ifa_ref(ifa);
630137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
631df8bae1dSRodney W. Grimes 				goto ours;
6320aade26eSRobert Watson 			}
6330ac40133SBrian Somers #ifdef BOOTP_COMPAT
6340aade26eSRobert Watson 			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) {
6358c0fec80SRobert Watson 				ifa_ref(ifa);
636137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
637ca925d9cSJonathan Lemon 				goto ours;
6380aade26eSRobert Watson 			}
6390ac40133SBrian Somers #endif
640df8bae1dSRodney W. Grimes 		}
641137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
64219e5b0a7SRobert Watson 		ia = NULL;
643df8bae1dSRodney W. Grimes 	}
644f8429ca2SBruce M Simpson 	/* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
645f8429ca2SBruce M Simpson 	if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
64686425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
647f8429ca2SBruce M Simpson 		m_freem(m);
648f8429ca2SBruce M Simpson 		return;
649f8429ca2SBruce M Simpson 	}
650df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
651603724d3SBjoern A. Zeeb 		if (V_ip_mrouter) {
652df8bae1dSRodney W. Grimes 			/*
653df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, all
654df8bae1dSRodney W. Grimes 			 * incoming multicast packets are passed to the
655df8bae1dSRodney W. Grimes 			 * kernel-level multicast forwarding function.
656df8bae1dSRodney W. Grimes 			 * The packet is returned (relatively) intact; if
657df8bae1dSRodney W. Grimes 			 * ip_mforward() returns a non-zero value, the packet
658df8bae1dSRodney W. Grimes 			 * must be discarded, else it may be accepted below.
659df8bae1dSRodney W. Grimes 			 */
6600aade26eSRobert Watson 			if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
66186425c62SRobert Watson 				IPSTAT_INC(ips_cantforward);
662df8bae1dSRodney W. Grimes 				m_freem(m);
663c67b1d17SGarrett Wollman 				return;
664df8bae1dSRodney W. Grimes 			}
665df8bae1dSRodney W. Grimes 
666df8bae1dSRodney W. Grimes 			/*
66711612afaSDima Dorfman 			 * The process-level routing daemon needs to receive
668df8bae1dSRodney W. Grimes 			 * all multicast IGMP packets, whether or not this
669df8bae1dSRodney W. Grimes 			 * host belongs to their destination groups.
670df8bae1dSRodney W. Grimes 			 */
671df8bae1dSRodney W. Grimes 			if (ip->ip_p == IPPROTO_IGMP)
672df8bae1dSRodney W. Grimes 				goto ours;
67386425c62SRobert Watson 			IPSTAT_INC(ips_forward);
674df8bae1dSRodney W. Grimes 		}
675df8bae1dSRodney W. Grimes 		/*
676d10910e6SBruce M Simpson 		 * Assume the packet is for us, to avoid prematurely taking
677d10910e6SBruce M Simpson 		 * a lock on the in_multi hash. Protocols must perform
678d10910e6SBruce M Simpson 		 * their own filtering and update statistics accordingly.
679df8bae1dSRodney W. Grimes 		 */
680df8bae1dSRodney W. Grimes 		goto ours;
681df8bae1dSRodney W. Grimes 	}
682df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
683df8bae1dSRodney W. Grimes 		goto ours;
684df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == INADDR_ANY)
685df8bae1dSRodney W. Grimes 		goto ours;
686df8bae1dSRodney W. Grimes 
6876a800098SYoshinobu Inoue 	/*
6886a800098SYoshinobu Inoue 	 * FAITH(Firewall Aided Internet Translator)
6896a800098SYoshinobu Inoue 	 */
6900aade26eSRobert Watson 	if (ifp && ifp->if_type == IFT_FAITH) {
691603724d3SBjoern A. Zeeb 		if (V_ip_keepfaith) {
6926a800098SYoshinobu Inoue 			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
6936a800098SYoshinobu Inoue 				goto ours;
6946a800098SYoshinobu Inoue 		}
6956a800098SYoshinobu Inoue 		m_freem(m);
6966a800098SYoshinobu Inoue 		return;
6976a800098SYoshinobu Inoue 	}
6989494d596SBrooks Davis 
699df8bae1dSRodney W. Grimes 	/*
700df8bae1dSRodney W. Grimes 	 * Not for us; forward if possible and desirable.
701df8bae1dSRodney W. Grimes 	 */
702603724d3SBjoern A. Zeeb 	if (V_ipforwarding == 0) {
70386425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
704df8bae1dSRodney W. Grimes 		m_freem(m);
705546f251bSChris D. Faulhaber 	} else {
706b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
7071dfcf0d2SAndre Oppermann 		if (ip_ipsec_fwd(m))
708546f251bSChris D. Faulhaber 			goto bad;
709b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
7109b932e9eSAndre Oppermann 		ip_forward(m, dchg);
711546f251bSChris D. Faulhaber 	}
712c67b1d17SGarrett Wollman 	return;
713df8bae1dSRodney W. Grimes 
714df8bae1dSRodney W. Grimes ours:
715d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH
716d0ebc0d2SYaroslav Tykhiy 	/*
717d0ebc0d2SYaroslav Tykhiy 	 * IPSTEALTH: Process non-routing options only
718d0ebc0d2SYaroslav Tykhiy 	 * if the packet is destined for us.
719d0ebc0d2SYaroslav Tykhiy 	 */
72019e5b0a7SRobert Watson 	if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1)) {
72119e5b0a7SRobert Watson 		if (ia != NULL)
72219e5b0a7SRobert Watson 			ifa_free(&ia->ia_ifa);
723d0ebc0d2SYaroslav Tykhiy 		return;
72419e5b0a7SRobert Watson 	}
725d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */
726d0ebc0d2SYaroslav Tykhiy 
7275da9f8faSJosef Karthauser 	/* Count the packet in the ip address stats */
7285da9f8faSJosef Karthauser 	if (ia != NULL) {
7295da9f8faSJosef Karthauser 		ia->ia_ifa.if_ipackets++;
7305da9f8faSJosef Karthauser 		ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
7318c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
7325da9f8faSJosef Karthauser 	}
733100ba1a6SJordan K. Hubbard 
73463f8d699SJordan K. Hubbard 	/*
735b6ea1aa5SRuslan Ermilov 	 * Attempt reassembly; if it succeeds, proceed.
736ac9d7e26SMax Laier 	 * ip_reass() will return a different mbuf.
737df8bae1dSRodney W. Grimes 	 */
7388f134647SGleb Smirnoff 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
739f0cada84SAndre Oppermann 		m = ip_reass(m);
740f0cada84SAndre Oppermann 		if (m == NULL)
741c67b1d17SGarrett Wollman 			return;
7426a800098SYoshinobu Inoue 		ip = mtod(m, struct ip *);
7437e2df452SRuslan Ermilov 		/* Get the header length of the reassembled packet */
74453be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
745f0cada84SAndre Oppermann 	}
746f0cada84SAndre Oppermann 
747b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
74833841545SHajimu UMEMOTO 	/*
74933841545SHajimu UMEMOTO 	 * enforce IPsec policy checking if we are seeing last header.
75033841545SHajimu UMEMOTO 	 * note that we do not visit this with protocols with pcb layer
75133841545SHajimu UMEMOTO 	 * code - like udp/tcp/raw ip.
75233841545SHajimu UMEMOTO 	 */
7531dfcf0d2SAndre Oppermann 	if (ip_ipsec_input(m))
75433841545SHajimu UMEMOTO 		goto bad;
755b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
75633841545SHajimu UMEMOTO 
757df8bae1dSRodney W. Grimes 	/*
758df8bae1dSRodney W. Grimes 	 * Switch out to protocol's input routine.
759df8bae1dSRodney W. Grimes 	 */
76086425c62SRobert Watson 	IPSTAT_INC(ips_delivered);
7619b932e9eSAndre Oppermann 
7622b25acc1SLuigi Rizzo 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
763c67b1d17SGarrett Wollman 	return;
764df8bae1dSRodney W. Grimes bad:
765df8bae1dSRodney W. Grimes 	m_freem(m);
766c67b1d17SGarrett Wollman }
767c67b1d17SGarrett Wollman 
768c67b1d17SGarrett Wollman /*
769d248c7d7SRobert Watson  * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
770d248c7d7SRobert Watson  * max has slightly different semantics than the sysctl, for historical
771d248c7d7SRobert Watson  * reasons.
772d248c7d7SRobert Watson  */
773d248c7d7SRobert Watson static void
774d248c7d7SRobert Watson maxnipq_update(void)
775d248c7d7SRobert Watson {
776d248c7d7SRobert Watson 
777d248c7d7SRobert Watson 	/*
778d248c7d7SRobert Watson 	 * -1 for unlimited allocation.
779d248c7d7SRobert Watson 	 */
780603724d3SBjoern A. Zeeb 	if (V_maxnipq < 0)
781603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 0);
782d248c7d7SRobert Watson 	/*
783d248c7d7SRobert Watson 	 * Positive number for specific bound.
784d248c7d7SRobert Watson 	 */
785603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0)
786603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, V_maxnipq);
787d248c7d7SRobert Watson 	/*
788d248c7d7SRobert Watson 	 * Zero specifies no further fragment queue allocation -- set the
789d248c7d7SRobert Watson 	 * bound very low, but rely on implementation elsewhere to actually
790d248c7d7SRobert Watson 	 * prevent allocation and reclaim current queues.
791d248c7d7SRobert Watson 	 */
792603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0)
793603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 1);
794d248c7d7SRobert Watson }
795d248c7d7SRobert Watson 
7964f590175SPaul Saab static void
7974f590175SPaul Saab ipq_zone_change(void *tag)
7984f590175SPaul Saab {
7994f590175SPaul Saab 
800603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) {
801603724d3SBjoern A. Zeeb 		V_maxnipq = nmbclusters / 32;
8024f590175SPaul Saab 		maxnipq_update();
8034f590175SPaul Saab 	}
8044f590175SPaul Saab }
8054f590175SPaul Saab 
806d248c7d7SRobert Watson static int
807d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
808d248c7d7SRobert Watson {
809d248c7d7SRobert Watson 	int error, i;
810d248c7d7SRobert Watson 
811603724d3SBjoern A. Zeeb 	i = V_maxnipq;
812d248c7d7SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
813d248c7d7SRobert Watson 	if (error || !req->newptr)
814d248c7d7SRobert Watson 		return (error);
815d248c7d7SRobert Watson 
816d248c7d7SRobert Watson 	/*
817d248c7d7SRobert Watson 	 * XXXRW: Might be a good idea to sanity check the argument and place
818d248c7d7SRobert Watson 	 * an extreme upper bound.
819d248c7d7SRobert Watson 	 */
820d248c7d7SRobert Watson 	if (i < -1)
821d248c7d7SRobert Watson 		return (EINVAL);
822603724d3SBjoern A. Zeeb 	V_maxnipq = i;
823d248c7d7SRobert Watson 	maxnipq_update();
824d248c7d7SRobert Watson 	return (0);
825d248c7d7SRobert Watson }
826d248c7d7SRobert Watson 
827d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
828d248c7d7SRobert Watson     NULL, 0, sysctl_maxnipq, "I",
829d248c7d7SRobert Watson     "Maximum number of IPv4 fragment reassembly queue entries");
830d248c7d7SRobert Watson 
831d248c7d7SRobert Watson /*
8328948e4baSArchie Cobbs  * Take incoming datagram fragment and try to reassemble it into
833f0cada84SAndre Oppermann  * whole datagram.  If the argument is the first fragment or one
834f0cada84SAndre Oppermann  * in between the function will return NULL and store the mbuf
835f0cada84SAndre Oppermann  * in the fragment chain.  If the argument is the last fragment
836f0cada84SAndre Oppermann  * the packet will be reassembled and the pointer to the new
837f0cada84SAndre Oppermann  * mbuf returned for further processing.  Only m_tags attached
838f0cada84SAndre Oppermann  * to the first packet/fragment are preserved.
839f0cada84SAndre Oppermann  * The IP header is *NOT* adjusted out of iplen.
840df8bae1dSRodney W. Grimes  */
841f0cada84SAndre Oppermann struct mbuf *
842f0cada84SAndre Oppermann ip_reass(struct mbuf *m)
843df8bae1dSRodney W. Grimes {
844f0cada84SAndre Oppermann 	struct ip *ip;
845f0cada84SAndre Oppermann 	struct mbuf *p, *q, *nq, *t;
846f0cada84SAndre Oppermann 	struct ipq *fp = NULL;
847f0cada84SAndre Oppermann 	struct ipqhead *head;
848f0cada84SAndre Oppermann 	int i, hlen, next;
84959dfcba4SHajimu UMEMOTO 	u_int8_t ecn, ecn0;
850f0cada84SAndre Oppermann 	u_short hash;
851df8bae1dSRodney W. Grimes 
852800af1fbSMaxim Konovalov 	/* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
853603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0 || V_maxfragsperpacket == 0) {
85486425c62SRobert Watson 		IPSTAT_INC(ips_fragments);
85586425c62SRobert Watson 		IPSTAT_INC(ips_fragdropped);
8569d804f81SAndre Oppermann 		m_freem(m);
8579d804f81SAndre Oppermann 		return (NULL);
858f0cada84SAndre Oppermann 	}
8592fad1e93SSam Leffler 
860f0cada84SAndre Oppermann 	ip = mtod(m, struct ip *);
861f0cada84SAndre Oppermann 	hlen = ip->ip_hl << 2;
862f0cada84SAndre Oppermann 
863f0cada84SAndre Oppermann 	hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
864603724d3SBjoern A. Zeeb 	head = &V_ipq[hash];
865f0cada84SAndre Oppermann 	IPQ_LOCK();
866f0cada84SAndre Oppermann 
867f0cada84SAndre Oppermann 	/*
868f0cada84SAndre Oppermann 	 * Look for queue of fragments
869f0cada84SAndre Oppermann 	 * of this datagram.
870f0cada84SAndre Oppermann 	 */
871f0cada84SAndre Oppermann 	TAILQ_FOREACH(fp, head, ipq_list)
872f0cada84SAndre Oppermann 		if (ip->ip_id == fp->ipq_id &&
873f0cada84SAndre Oppermann 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
874f0cada84SAndre Oppermann 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
875f0cada84SAndre Oppermann #ifdef MAC
87630d239bcSRobert Watson 		    mac_ipq_match(m, fp) &&
877f0cada84SAndre Oppermann #endif
878f0cada84SAndre Oppermann 		    ip->ip_p == fp->ipq_p)
879f0cada84SAndre Oppermann 			goto found;
880f0cada84SAndre Oppermann 
881f0cada84SAndre Oppermann 	fp = NULL;
882f0cada84SAndre Oppermann 
883f0cada84SAndre Oppermann 	/*
884d248c7d7SRobert Watson 	 * Attempt to trim the number of allocated fragment queues if it
885d248c7d7SRobert Watson 	 * exceeds the administrative limit.
886f0cada84SAndre Oppermann 	 */
887603724d3SBjoern A. Zeeb 	if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) {
888f0cada84SAndre Oppermann 		/*
889f0cada84SAndre Oppermann 		 * drop something from the tail of the current queue
890f0cada84SAndre Oppermann 		 * before proceeding further
891f0cada84SAndre Oppermann 		 */
892f0cada84SAndre Oppermann 		struct ipq *q = TAILQ_LAST(head, ipqhead);
893f0cada84SAndre Oppermann 		if (q == NULL) {   /* gak */
894f0cada84SAndre Oppermann 			for (i = 0; i < IPREASS_NHASH; i++) {
895603724d3SBjoern A. Zeeb 				struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead);
896f0cada84SAndre Oppermann 				if (r) {
89786425c62SRobert Watson 					IPSTAT_ADD(ips_fragtimeout,
89886425c62SRobert Watson 					    r->ipq_nfrags);
899603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], r);
900f0cada84SAndre Oppermann 					break;
901f0cada84SAndre Oppermann 				}
902f0cada84SAndre Oppermann 			}
903f0cada84SAndre Oppermann 		} else {
90486425c62SRobert Watson 			IPSTAT_ADD(ips_fragtimeout, q->ipq_nfrags);
905f0cada84SAndre Oppermann 			ip_freef(head, q);
906f0cada84SAndre Oppermann 		}
907f0cada84SAndre Oppermann 	}
908f0cada84SAndre Oppermann 
909f0cada84SAndre Oppermann found:
910f0cada84SAndre Oppermann 	/*
911f0cada84SAndre Oppermann 	 * Adjust ip_len to not reflect header,
912f0cada84SAndre Oppermann 	 * convert offset of this to bytes.
913f0cada84SAndre Oppermann 	 */
9148f134647SGleb Smirnoff 	ip->ip_len = htons(ntohs(ip->ip_len) - hlen);
9158f134647SGleb Smirnoff 	if (ip->ip_off & htons(IP_MF)) {
916f0cada84SAndre Oppermann 		/*
917f0cada84SAndre Oppermann 		 * Make sure that fragments have a data length
918f0cada84SAndre Oppermann 		 * that's a non-zero multiple of 8 bytes.
919f0cada84SAndre Oppermann 		 */
920b6fcf6f9SGleb Smirnoff 		if (ip->ip_len == htons(0) || (ntohs(ip->ip_len) & 0x7) != 0) {
92186425c62SRobert Watson 			IPSTAT_INC(ips_toosmall); /* XXX */
922f0cada84SAndre Oppermann 			goto dropfrag;
923f0cada84SAndre Oppermann 		}
924b09dc7e3SAndre Oppermann 		m->m_flags |= M_IP_FRAG;
925f0cada84SAndre Oppermann 	} else
926b09dc7e3SAndre Oppermann 		m->m_flags &= ~M_IP_FRAG;
9278f134647SGleb Smirnoff 	ip->ip_off = htons(ntohs(ip->ip_off) << 3);
928f0cada84SAndre Oppermann 
929f0cada84SAndre Oppermann 	/*
930f0cada84SAndre Oppermann 	 * Attempt reassembly; if it succeeds, proceed.
931f0cada84SAndre Oppermann 	 * ip_reass() will return a different mbuf.
932f0cada84SAndre Oppermann 	 */
93386425c62SRobert Watson 	IPSTAT_INC(ips_fragments);
9341b4381afSAndre Oppermann 	m->m_pkthdr.PH_loc.ptr = ip;
935f0cada84SAndre Oppermann 
936f0cada84SAndre Oppermann 	/* Previous ip_reass() started here. */
937df8bae1dSRodney W. Grimes 	/*
938df8bae1dSRodney W. Grimes 	 * Presence of header sizes in mbufs
939df8bae1dSRodney W. Grimes 	 * would confuse code below.
940df8bae1dSRodney W. Grimes 	 */
941df8bae1dSRodney W. Grimes 	m->m_data += hlen;
942df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
943df8bae1dSRodney W. Grimes 
944df8bae1dSRodney W. Grimes 	/*
945df8bae1dSRodney W. Grimes 	 * If first fragment to arrive, create a reassembly queue.
946df8bae1dSRodney W. Grimes 	 */
947042bbfa3SRobert Watson 	if (fp == NULL) {
948603724d3SBjoern A. Zeeb 		fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
949d248c7d7SRobert Watson 		if (fp == NULL)
950df8bae1dSRodney W. Grimes 			goto dropfrag;
95136b0360bSRobert Watson #ifdef MAC
95230d239bcSRobert Watson 		if (mac_ipq_init(fp, M_NOWAIT) != 0) {
953603724d3SBjoern A. Zeeb 			uma_zfree(V_ipq_zone, fp);
9541d7d0bfeSPawel Jakub Dawidek 			fp = NULL;
9555e7ce478SRobert Watson 			goto dropfrag;
9565e7ce478SRobert Watson 		}
95730d239bcSRobert Watson 		mac_ipq_create(m, fp);
95836b0360bSRobert Watson #endif
959462b86feSPoul-Henning Kamp 		TAILQ_INSERT_HEAD(head, fp, ipq_list);
960603724d3SBjoern A. Zeeb 		V_nipq++;
961375386e2SMike Silbersack 		fp->ipq_nfrags = 1;
962df8bae1dSRodney W. Grimes 		fp->ipq_ttl = IPFRAGTTL;
963df8bae1dSRodney W. Grimes 		fp->ipq_p = ip->ip_p;
964df8bae1dSRodney W. Grimes 		fp->ipq_id = ip->ip_id;
9656effc713SDoug Rabson 		fp->ipq_src = ip->ip_src;
9666effc713SDoug Rabson 		fp->ipq_dst = ip->ip_dst;
967af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
968af38c68cSLuigi Rizzo 		m->m_nextpkt = NULL;
969800af1fbSMaxim Konovalov 		goto done;
97036b0360bSRobert Watson 	} else {
971375386e2SMike Silbersack 		fp->ipq_nfrags++;
97236b0360bSRobert Watson #ifdef MAC
97330d239bcSRobert Watson 		mac_ipq_update(m, fp);
97436b0360bSRobert Watson #endif
975df8bae1dSRodney W. Grimes 	}
976df8bae1dSRodney W. Grimes 
9771b4381afSAndre Oppermann #define GETIP(m)	((struct ip*)((m)->m_pkthdr.PH_loc.ptr))
9786effc713SDoug Rabson 
979df8bae1dSRodney W. Grimes 	/*
98059dfcba4SHajimu UMEMOTO 	 * Handle ECN by comparing this segment with the first one;
98159dfcba4SHajimu UMEMOTO 	 * if CE is set, do not lose CE.
98259dfcba4SHajimu UMEMOTO 	 * drop if CE and not-ECT are mixed for the same packet.
98359dfcba4SHajimu UMEMOTO 	 */
98459dfcba4SHajimu UMEMOTO 	ecn = ip->ip_tos & IPTOS_ECN_MASK;
98559dfcba4SHajimu UMEMOTO 	ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
98659dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_CE) {
98759dfcba4SHajimu UMEMOTO 		if (ecn0 == IPTOS_ECN_NOTECT)
98859dfcba4SHajimu UMEMOTO 			goto dropfrag;
98959dfcba4SHajimu UMEMOTO 		if (ecn0 != IPTOS_ECN_CE)
99059dfcba4SHajimu UMEMOTO 			GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
99159dfcba4SHajimu UMEMOTO 	}
99259dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
99359dfcba4SHajimu UMEMOTO 		goto dropfrag;
99459dfcba4SHajimu UMEMOTO 
99559dfcba4SHajimu UMEMOTO 	/*
996df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
997df8bae1dSRodney W. Grimes 	 */
9986effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
9998f134647SGleb Smirnoff 		if (ntohs(GETIP(q)->ip_off) > ntohs(ip->ip_off))
1000df8bae1dSRodney W. Grimes 			break;
1001df8bae1dSRodney W. Grimes 
1002df8bae1dSRodney W. Grimes 	/*
1003df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
1004df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
1005af38c68cSLuigi Rizzo 	 * segment.  If it provides all of our data, drop us, otherwise
1006af38c68cSLuigi Rizzo 	 * stick new segment in the proper place.
1007db4f9cc7SJonathan Lemon 	 *
10086bccea7cSRebecca Cran 	 * If some of the data is dropped from the preceding
1009db4f9cc7SJonathan Lemon 	 * segment, then it's checksum is invalidated.
1010df8bae1dSRodney W. Grimes 	 */
10116effc713SDoug Rabson 	if (p) {
10128f134647SGleb Smirnoff 		i = ntohs(GETIP(p)->ip_off) + ntohs(GETIP(p)->ip_len) -
10138f134647SGleb Smirnoff 		    ntohs(ip->ip_off);
1014df8bae1dSRodney W. Grimes 		if (i > 0) {
10158f134647SGleb Smirnoff 			if (i >= ntohs(ip->ip_len))
1016df8bae1dSRodney W. Grimes 				goto dropfrag;
10176a800098SYoshinobu Inoue 			m_adj(m, i);
1018db4f9cc7SJonathan Lemon 			m->m_pkthdr.csum_flags = 0;
10198f134647SGleb Smirnoff 			ip->ip_off = htons(ntohs(ip->ip_off) + i);
10208f134647SGleb Smirnoff 			ip->ip_len = htons(ntohs(ip->ip_len) - i);
1021df8bae1dSRodney W. Grimes 		}
1022af38c68cSLuigi Rizzo 		m->m_nextpkt = p->m_nextpkt;
1023af38c68cSLuigi Rizzo 		p->m_nextpkt = m;
1024af38c68cSLuigi Rizzo 	} else {
1025af38c68cSLuigi Rizzo 		m->m_nextpkt = fp->ipq_frags;
1026af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
1027df8bae1dSRodney W. Grimes 	}
1028df8bae1dSRodney W. Grimes 
1029df8bae1dSRodney W. Grimes 	/*
1030df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
1031df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
1032df8bae1dSRodney W. Grimes 	 */
10338f134647SGleb Smirnoff 	for (; q != NULL && ntohs(ip->ip_off) + ntohs(ip->ip_len) >
10348f134647SGleb Smirnoff 	    ntohs(GETIP(q)->ip_off); q = nq) {
10358f134647SGleb Smirnoff 		i = (ntohs(ip->ip_off) + ntohs(ip->ip_len)) -
10368f134647SGleb Smirnoff 		    ntohs(GETIP(q)->ip_off);
10378f134647SGleb Smirnoff 		if (i < ntohs(GETIP(q)->ip_len)) {
10388f134647SGleb Smirnoff 			GETIP(q)->ip_len = htons(ntohs(GETIP(q)->ip_len) - i);
10398f134647SGleb Smirnoff 			GETIP(q)->ip_off = htons(ntohs(GETIP(q)->ip_off) + i);
10406effc713SDoug Rabson 			m_adj(q, i);
1041db4f9cc7SJonathan Lemon 			q->m_pkthdr.csum_flags = 0;
1042df8bae1dSRodney W. Grimes 			break;
1043df8bae1dSRodney W. Grimes 		}
10446effc713SDoug Rabson 		nq = q->m_nextpkt;
1045af38c68cSLuigi Rizzo 		m->m_nextpkt = nq;
104686425c62SRobert Watson 		IPSTAT_INC(ips_fragdropped);
1047375386e2SMike Silbersack 		fp->ipq_nfrags--;
10486effc713SDoug Rabson 		m_freem(q);
1049df8bae1dSRodney W. Grimes 	}
1050df8bae1dSRodney W. Grimes 
1051df8bae1dSRodney W. Grimes 	/*
1052375386e2SMike Silbersack 	 * Check for complete reassembly and perform frag per packet
1053375386e2SMike Silbersack 	 * limiting.
1054375386e2SMike Silbersack 	 *
1055375386e2SMike Silbersack 	 * Frag limiting is performed here so that the nth frag has
1056375386e2SMike Silbersack 	 * a chance to complete the packet before we drop the packet.
1057375386e2SMike Silbersack 	 * As a result, n+1 frags are actually allowed per packet, but
1058375386e2SMike Silbersack 	 * only n will ever be stored. (n = maxfragsperpacket.)
1059375386e2SMike Silbersack 	 *
1060df8bae1dSRodney W. Grimes 	 */
10616effc713SDoug Rabson 	next = 0;
10626effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
10638f134647SGleb Smirnoff 		if (ntohs(GETIP(q)->ip_off) != next) {
1064603724d3SBjoern A. Zeeb 			if (fp->ipq_nfrags > V_maxfragsperpacket) {
106586425c62SRobert Watson 				IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1066375386e2SMike Silbersack 				ip_freef(head, fp);
106799e8617dSMaxim Konovalov 			}
1068f0cada84SAndre Oppermann 			goto done;
1069375386e2SMike Silbersack 		}
10708f134647SGleb Smirnoff 		next += ntohs(GETIP(q)->ip_len);
10716effc713SDoug Rabson 	}
10726effc713SDoug Rabson 	/* Make sure the last packet didn't have the IP_MF flag */
1073b09dc7e3SAndre Oppermann 	if (p->m_flags & M_IP_FRAG) {
1074603724d3SBjoern A. Zeeb 		if (fp->ipq_nfrags > V_maxfragsperpacket) {
107586425c62SRobert Watson 			IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1076375386e2SMike Silbersack 			ip_freef(head, fp);
107799e8617dSMaxim Konovalov 		}
1078f0cada84SAndre Oppermann 		goto done;
1079375386e2SMike Silbersack 	}
1080df8bae1dSRodney W. Grimes 
1081df8bae1dSRodney W. Grimes 	/*
1082430d30d8SBill Fenner 	 * Reassembly is complete.  Make sure the packet is a sane size.
1083430d30d8SBill Fenner 	 */
10846effc713SDoug Rabson 	q = fp->ipq_frags;
10856effc713SDoug Rabson 	ip = GETIP(q);
108653be11f6SPoul-Henning Kamp 	if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
108786425c62SRobert Watson 		IPSTAT_INC(ips_toolong);
108886425c62SRobert Watson 		IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1089462b86feSPoul-Henning Kamp 		ip_freef(head, fp);
1090f0cada84SAndre Oppermann 		goto done;
1091430d30d8SBill Fenner 	}
1092430d30d8SBill Fenner 
1093430d30d8SBill Fenner 	/*
1094430d30d8SBill Fenner 	 * Concatenate fragments.
1095df8bae1dSRodney W. Grimes 	 */
10966effc713SDoug Rabson 	m = q;
1097df8bae1dSRodney W. Grimes 	t = m->m_next;
109802410549SRobert Watson 	m->m_next = NULL;
1099df8bae1dSRodney W. Grimes 	m_cat(m, t);
11006effc713SDoug Rabson 	nq = q->m_nextpkt;
110102410549SRobert Watson 	q->m_nextpkt = NULL;
11026effc713SDoug Rabson 	for (q = nq; q != NULL; q = nq) {
11036effc713SDoug Rabson 		nq = q->m_nextpkt;
1104945aa40dSDoug Rabson 		q->m_nextpkt = NULL;
1105db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1106db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1107a8db1d93SJonathan Lemon 		m_cat(m, q);
1108df8bae1dSRodney W. Grimes 	}
11096edb555dSOleg Bulyzhin 	/*
11106edb555dSOleg Bulyzhin 	 * In order to do checksumming faster we do 'end-around carry' here
11116edb555dSOleg Bulyzhin 	 * (and not in for{} loop), though it implies we are not going to
11126edb555dSOleg Bulyzhin 	 * reassemble more than 64k fragments.
11136edb555dSOleg Bulyzhin 	 */
11146edb555dSOleg Bulyzhin 	m->m_pkthdr.csum_data =
11156edb555dSOleg Bulyzhin 	    (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16);
111636b0360bSRobert Watson #ifdef MAC
111730d239bcSRobert Watson 	mac_ipq_reassemble(fp, m);
111830d239bcSRobert Watson 	mac_ipq_destroy(fp);
111936b0360bSRobert Watson #endif
1120df8bae1dSRodney W. Grimes 
1121df8bae1dSRodney W. Grimes 	/*
1122f0cada84SAndre Oppermann 	 * Create header for new ip packet by modifying header of first
1123f0cada84SAndre Oppermann 	 * packet;  dequeue and discard fragment reassembly header.
1124df8bae1dSRodney W. Grimes 	 * Make header visible.
1125df8bae1dSRodney W. Grimes 	 */
11268f134647SGleb Smirnoff 	ip->ip_len = htons((ip->ip_hl << 2) + next);
11276effc713SDoug Rabson 	ip->ip_src = fp->ipq_src;
11286effc713SDoug Rabson 	ip->ip_dst = fp->ipq_dst;
1129462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(head, fp, ipq_list);
1130603724d3SBjoern A. Zeeb 	V_nipq--;
1131603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
113253be11f6SPoul-Henning Kamp 	m->m_len += (ip->ip_hl << 2);
113353be11f6SPoul-Henning Kamp 	m->m_data -= (ip->ip_hl << 2);
1134df8bae1dSRodney W. Grimes 	/* some debugging cruft by sklower, below, will go away soon */
1135a5554bf0SPoul-Henning Kamp 	if (m->m_flags & M_PKTHDR)	/* XXX this should be done elsewhere */
1136a5554bf0SPoul-Henning Kamp 		m_fixhdr(m);
113786425c62SRobert Watson 	IPSTAT_INC(ips_reassembled);
1138f0cada84SAndre Oppermann 	IPQ_UNLOCK();
11396a800098SYoshinobu Inoue 	return (m);
1140df8bae1dSRodney W. Grimes 
1141df8bae1dSRodney W. Grimes dropfrag:
114286425c62SRobert Watson 	IPSTAT_INC(ips_fragdropped);
1143042bbfa3SRobert Watson 	if (fp != NULL)
1144375386e2SMike Silbersack 		fp->ipq_nfrags--;
1145df8bae1dSRodney W. Grimes 	m_freem(m);
1146f0cada84SAndre Oppermann done:
1147f0cada84SAndre Oppermann 	IPQ_UNLOCK();
1148f0cada84SAndre Oppermann 	return (NULL);
11496effc713SDoug Rabson 
11506effc713SDoug Rabson #undef GETIP
1151df8bae1dSRodney W. Grimes }
1152df8bae1dSRodney W. Grimes 
1153df8bae1dSRodney W. Grimes /*
1154df8bae1dSRodney W. Grimes  * Free a fragment reassembly header and all
1155df8bae1dSRodney W. Grimes  * associated datagrams.
1156df8bae1dSRodney W. Grimes  */
11570312fbe9SPoul-Henning Kamp static void
1158f2565d68SRobert Watson ip_freef(struct ipqhead *fhp, struct ipq *fp)
1159df8bae1dSRodney W. Grimes {
1160f2565d68SRobert Watson 	struct mbuf *q;
1161df8bae1dSRodney W. Grimes 
11622fad1e93SSam Leffler 	IPQ_LOCK_ASSERT();
11632fad1e93SSam Leffler 
11646effc713SDoug Rabson 	while (fp->ipq_frags) {
11656effc713SDoug Rabson 		q = fp->ipq_frags;
11666effc713SDoug Rabson 		fp->ipq_frags = q->m_nextpkt;
11676effc713SDoug Rabson 		m_freem(q);
1168df8bae1dSRodney W. Grimes 	}
1169462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(fhp, fp, ipq_list);
1170603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
1171603724d3SBjoern A. Zeeb 	V_nipq--;
1172df8bae1dSRodney W. Grimes }
1173df8bae1dSRodney W. Grimes 
1174df8bae1dSRodney W. Grimes /*
1175df8bae1dSRodney W. Grimes  * IP timer processing;
1176df8bae1dSRodney W. Grimes  * if a timer expires on a reassembly
1177df8bae1dSRodney W. Grimes  * queue, discard it.
1178df8bae1dSRodney W. Grimes  */
1179df8bae1dSRodney W. Grimes void
1180f2565d68SRobert Watson ip_slowtimo(void)
1181df8bae1dSRodney W. Grimes {
11828b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1183f2565d68SRobert Watson 	struct ipq *fp;
1184194a213eSAndrey A. Chernov 	int i;
1185df8bae1dSRodney W. Grimes 
11865ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
11872fad1e93SSam Leffler 	IPQ_LOCK();
11888b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
11898b615593SMarko Zec 		CURVNET_SET(vnet_iter);
1190194a213eSAndrey A. Chernov 		for (i = 0; i < IPREASS_NHASH; i++) {
1191603724d3SBjoern A. Zeeb 			for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) {
1192462b86feSPoul-Henning Kamp 				struct ipq *fpp;
1193462b86feSPoul-Henning Kamp 
1194462b86feSPoul-Henning Kamp 				fpp = fp;
1195462b86feSPoul-Henning Kamp 				fp = TAILQ_NEXT(fp, ipq_list);
1196462b86feSPoul-Henning Kamp 				if(--fpp->ipq_ttl == 0) {
119786425c62SRobert Watson 					IPSTAT_ADD(ips_fragtimeout,
119886425c62SRobert Watson 					    fpp->ipq_nfrags);
1199603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], fpp);
1200df8bae1dSRodney W. Grimes 				}
1201df8bae1dSRodney W. Grimes 			}
1202194a213eSAndrey A. Chernov 		}
1203690a6055SJesper Skriver 		/*
1204690a6055SJesper Skriver 		 * If we are over the maximum number of fragments
1205690a6055SJesper Skriver 		 * (due to the limit being lowered), drain off
1206690a6055SJesper Skriver 		 * enough to get down to the new limit.
1207690a6055SJesper Skriver 		 */
1208603724d3SBjoern A. Zeeb 		if (V_maxnipq >= 0 && V_nipq > V_maxnipq) {
1209690a6055SJesper Skriver 			for (i = 0; i < IPREASS_NHASH; i++) {
12108b615593SMarko Zec 				while (V_nipq > V_maxnipq &&
12118b615593SMarko Zec 				    !TAILQ_EMPTY(&V_ipq[i])) {
121286425c62SRobert Watson 					IPSTAT_ADD(ips_fragdropped,
121386425c62SRobert Watson 					    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
12148b615593SMarko Zec 					ip_freef(&V_ipq[i],
12158b615593SMarko Zec 					    TAILQ_FIRST(&V_ipq[i]));
1216690a6055SJesper Skriver 				}
1217690a6055SJesper Skriver 			}
1218690a6055SJesper Skriver 		}
12198b615593SMarko Zec 		CURVNET_RESTORE();
12208b615593SMarko Zec 	}
12212fad1e93SSam Leffler 	IPQ_UNLOCK();
12225ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
1223df8bae1dSRodney W. Grimes }
1224df8bae1dSRodney W. Grimes 
1225df8bae1dSRodney W. Grimes /*
1226df8bae1dSRodney W. Grimes  * Drain off all datagram fragments.
1227df8bae1dSRodney W. Grimes  */
12289802380eSBjoern A. Zeeb static void
12299802380eSBjoern A. Zeeb ip_drain_locked(void)
1230df8bae1dSRodney W. Grimes {
1231194a213eSAndrey A. Chernov 	int     i;
1232ce29ab3aSGarrett Wollman 
12339802380eSBjoern A. Zeeb 	IPQ_LOCK_ASSERT();
12349802380eSBjoern A. Zeeb 
1235194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1236603724d3SBjoern A. Zeeb 		while(!TAILQ_EMPTY(&V_ipq[i])) {
123786425c62SRobert Watson 			IPSTAT_ADD(ips_fragdropped,
123886425c62SRobert Watson 			    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
1239603724d3SBjoern A. Zeeb 			ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1240194a213eSAndrey A. Chernov 		}
1241194a213eSAndrey A. Chernov 	}
12429802380eSBjoern A. Zeeb }
12439802380eSBjoern A. Zeeb 
12449802380eSBjoern A. Zeeb void
12459802380eSBjoern A. Zeeb ip_drain(void)
12469802380eSBjoern A. Zeeb {
12479802380eSBjoern A. Zeeb 	VNET_ITERATOR_DECL(vnet_iter);
12489802380eSBjoern A. Zeeb 
12499802380eSBjoern A. Zeeb 	VNET_LIST_RLOCK_NOSLEEP();
12509802380eSBjoern A. Zeeb 	IPQ_LOCK();
12519802380eSBjoern A. Zeeb 	VNET_FOREACH(vnet_iter) {
12529802380eSBjoern A. Zeeb 		CURVNET_SET(vnet_iter);
12539802380eSBjoern A. Zeeb 		ip_drain_locked();
12548b615593SMarko Zec 		CURVNET_RESTORE();
12558b615593SMarko Zec 	}
12562fad1e93SSam Leffler 	IPQ_UNLOCK();
12575ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
1258ce29ab3aSGarrett Wollman 	in_rtqdrain();
1259df8bae1dSRodney W. Grimes }
1260df8bae1dSRodney W. Grimes 
1261df8bae1dSRodney W. Grimes /*
1262de38924dSAndre Oppermann  * The protocol to be inserted into ip_protox[] must be already registered
1263de38924dSAndre Oppermann  * in inetsw[], either statically or through pf_proto_register().
1264de38924dSAndre Oppermann  */
1265de38924dSAndre Oppermann int
12661b48d245SBjoern A. Zeeb ipproto_register(short ipproto)
1267de38924dSAndre Oppermann {
1268de38924dSAndre Oppermann 	struct protosw *pr;
1269de38924dSAndre Oppermann 
1270de38924dSAndre Oppermann 	/* Sanity checks. */
12711b48d245SBjoern A. Zeeb 	if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
1272de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1273de38924dSAndre Oppermann 
1274de38924dSAndre Oppermann 	/*
1275de38924dSAndre Oppermann 	 * The protocol slot must not be occupied by another protocol
1276de38924dSAndre Oppermann 	 * already.  An index pointing to IPPROTO_RAW is unused.
1277de38924dSAndre Oppermann 	 */
1278de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1279de38924dSAndre Oppermann 	if (pr == NULL)
1280de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1281de38924dSAndre Oppermann 	if (ip_protox[ipproto] != pr - inetsw)	/* IPPROTO_RAW */
1282de38924dSAndre Oppermann 		return (EEXIST);
1283de38924dSAndre Oppermann 
1284de38924dSAndre Oppermann 	/* Find the protocol position in inetsw[] and set the index. */
1285de38924dSAndre Oppermann 	for (pr = inetdomain.dom_protosw;
1286de38924dSAndre Oppermann 	     pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1287de38924dSAndre Oppermann 		if (pr->pr_domain->dom_family == PF_INET &&
1288de38924dSAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol == ipproto) {
1289de38924dSAndre Oppermann 			ip_protox[pr->pr_protocol] = pr - inetsw;
1290de38924dSAndre Oppermann 			return (0);
1291de38924dSAndre Oppermann 		}
1292de38924dSAndre Oppermann 	}
1293de38924dSAndre Oppermann 	return (EPROTONOSUPPORT);
1294de38924dSAndre Oppermann }
1295de38924dSAndre Oppermann 
1296de38924dSAndre Oppermann int
12971b48d245SBjoern A. Zeeb ipproto_unregister(short ipproto)
1298de38924dSAndre Oppermann {
1299de38924dSAndre Oppermann 	struct protosw *pr;
1300de38924dSAndre Oppermann 
1301de38924dSAndre Oppermann 	/* Sanity checks. */
13021b48d245SBjoern A. Zeeb 	if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
1303de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1304de38924dSAndre Oppermann 
1305de38924dSAndre Oppermann 	/* Check if the protocol was indeed registered. */
1306de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1307de38924dSAndre Oppermann 	if (pr == NULL)
1308de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1309de38924dSAndre Oppermann 	if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
1310de38924dSAndre Oppermann 		return (ENOENT);
1311de38924dSAndre Oppermann 
1312de38924dSAndre Oppermann 	/* Reset the protocol slot to IPPROTO_RAW. */
1313de38924dSAndre Oppermann 	ip_protox[ipproto] = pr - inetsw;
1314de38924dSAndre Oppermann 	return (0);
1315de38924dSAndre Oppermann }
1316de38924dSAndre Oppermann 
1317df8bae1dSRodney W. Grimes /*
13188c0fec80SRobert Watson  * Given address of next destination (final or next hop), return (referenced)
13198c0fec80SRobert Watson  * internet address info of interface to be used to get there.
1320df8bae1dSRodney W. Grimes  */
1321bd714208SRuslan Ermilov struct in_ifaddr *
13228b07e49aSJulian Elischer ip_rtaddr(struct in_addr dst, u_int fibnum)
1323df8bae1dSRodney W. Grimes {
132497d8d152SAndre Oppermann 	struct route sro;
132502c1c707SAndre Oppermann 	struct sockaddr_in *sin;
132619e5b0a7SRobert Watson 	struct in_ifaddr *ia;
1327df8bae1dSRodney W. Grimes 
13280cfbbe3bSAndre Oppermann 	bzero(&sro, sizeof(sro));
132997d8d152SAndre Oppermann 	sin = (struct sockaddr_in *)&sro.ro_dst;
1330df8bae1dSRodney W. Grimes 	sin->sin_family = AF_INET;
1331df8bae1dSRodney W. Grimes 	sin->sin_len = sizeof(*sin);
1332df8bae1dSRodney W. Grimes 	sin->sin_addr = dst;
13336e6b3f7cSQing Li 	in_rtalloc_ign(&sro, 0, fibnum);
1334df8bae1dSRodney W. Grimes 
133597d8d152SAndre Oppermann 	if (sro.ro_rt == NULL)
133602410549SRobert Watson 		return (NULL);
133702c1c707SAndre Oppermann 
133819e5b0a7SRobert Watson 	ia = ifatoia(sro.ro_rt->rt_ifa);
133919e5b0a7SRobert Watson 	ifa_ref(&ia->ia_ifa);
134097d8d152SAndre Oppermann 	RTFREE(sro.ro_rt);
134119e5b0a7SRobert Watson 	return (ia);
1342df8bae1dSRodney W. Grimes }
1343df8bae1dSRodney W. Grimes 
1344df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = {
1345df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1346df8bae1dSRodney W. Grimes 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1347df8bae1dSRodney W. Grimes 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1348df8bae1dSRodney W. Grimes 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1349fcaf9f91SMike Silbersack 	0,		0,		EHOSTUNREACH,	0,
13503b8123b7SJesper Skriver 	ENOPROTOOPT,	ECONNREFUSED
1351df8bae1dSRodney W. Grimes };
1352df8bae1dSRodney W. Grimes 
1353df8bae1dSRodney W. Grimes /*
1354df8bae1dSRodney W. Grimes  * Forward a packet.  If some error occurs return the sender
1355df8bae1dSRodney W. Grimes  * an icmp packet.  Note we can't always generate a meaningful
1356df8bae1dSRodney W. Grimes  * icmp message because icmp doesn't have a large enough repertoire
1357df8bae1dSRodney W. Grimes  * of codes and types.
1358df8bae1dSRodney W. Grimes  *
1359df8bae1dSRodney W. Grimes  * If not forwarding, just drop the packet.  This could be confusing
1360df8bae1dSRodney W. Grimes  * if ipforwarding was zero but some routing protocol was advancing
1361df8bae1dSRodney W. Grimes  * us as a gateway to somewhere.  However, we must let the routing
1362df8bae1dSRodney W. Grimes  * protocol deal with that.
1363df8bae1dSRodney W. Grimes  *
1364df8bae1dSRodney W. Grimes  * The srcrt parameter indicates whether the packet is being forwarded
1365df8bae1dSRodney W. Grimes  * via a source route.
1366df8bae1dSRodney W. Grimes  */
13679b932e9eSAndre Oppermann void
13689b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt)
1369df8bae1dSRodney W. Grimes {
13702b25acc1SLuigi Rizzo 	struct ip *ip = mtod(m, struct ip *);
1371efbad259SEdward Tomasz Napierala 	struct in_ifaddr *ia;
1372df8bae1dSRodney W. Grimes 	struct mbuf *mcopy;
13739b932e9eSAndre Oppermann 	struct in_addr dest;
1374b835b6feSBjoern A. Zeeb 	struct route ro;
1375c773494eSAndre Oppermann 	int error, type = 0, code = 0, mtu = 0;
13763efc3014SJulian Elischer 
13779b932e9eSAndre Oppermann 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
137886425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
1379df8bae1dSRodney W. Grimes 		m_freem(m);
1380df8bae1dSRodney W. Grimes 		return;
1381df8bae1dSRodney W. Grimes 	}
13821b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
1383603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
13841b968362SDag-Erling Smørgrav #endif
1385df8bae1dSRodney W. Grimes 		if (ip->ip_ttl <= IPTTLDEC) {
13861b968362SDag-Erling Smørgrav 			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
138702c1c707SAndre Oppermann 			    0, 0);
1388df8bae1dSRodney W. Grimes 			return;
1389df8bae1dSRodney W. Grimes 		}
13901b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
13911b968362SDag-Erling Smørgrav 	}
13921b968362SDag-Erling Smørgrav #endif
1393df8bae1dSRodney W. Grimes 
13948b07e49aSJulian Elischer 	ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
1395efbad259SEdward Tomasz Napierala #ifndef IPSEC
1396efbad259SEdward Tomasz Napierala 	/*
1397efbad259SEdward Tomasz Napierala 	 * 'ia' may be NULL if there is no route for this destination.
1398efbad259SEdward Tomasz Napierala 	 * In case of IPsec, Don't discard it just yet, but pass it to
1399efbad259SEdward Tomasz Napierala 	 * ip_output in case of outgoing IPsec policy.
1400efbad259SEdward Tomasz Napierala 	 */
1401d23d475fSGuido van Rooij 	if (!srcrt && ia == NULL) {
140202c1c707SAndre Oppermann 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1403df8bae1dSRodney W. Grimes 		return;
140402c1c707SAndre Oppermann 	}
1405efbad259SEdward Tomasz Napierala #endif
1406df8bae1dSRodney W. Grimes 
1407df8bae1dSRodney W. Grimes 	/*
1408bfef7ed4SIan Dowse 	 * Save the IP header and at most 8 bytes of the payload,
1409bfef7ed4SIan Dowse 	 * in case we need to generate an ICMP message to the src.
1410bfef7ed4SIan Dowse 	 *
14114d2e3692SLuigi Rizzo 	 * XXX this can be optimized a lot by saving the data in a local
14124d2e3692SLuigi Rizzo 	 * buffer on the stack (72 bytes at most), and only allocating the
14134d2e3692SLuigi Rizzo 	 * mbuf if really necessary. The vast majority of the packets
14144d2e3692SLuigi Rizzo 	 * are forwarded without having to send an ICMP back (either
14154d2e3692SLuigi Rizzo 	 * because unnecessary, or because rate limited), so we are
14164d2e3692SLuigi Rizzo 	 * really we are wasting a lot of work here.
14174d2e3692SLuigi Rizzo 	 *
1418bfef7ed4SIan Dowse 	 * We don't use m_copy() because it might return a reference
1419bfef7ed4SIan Dowse 	 * to a shared cluster. Both this function and ip_output()
1420bfef7ed4SIan Dowse 	 * assume exclusive access to the IP header in `m', so any
1421bfef7ed4SIan Dowse 	 * data in a cluster may change before we reach icmp_error().
1422df8bae1dSRodney W. Grimes 	 */
1423dc4ad05eSGleb Smirnoff 	mcopy = m_gethdr(M_NOWAIT, m->m_type);
1424eb1b1807SGleb Smirnoff 	if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) {
14259967cafcSSam Leffler 		/*
14269967cafcSSam Leffler 		 * It's probably ok if the pkthdr dup fails (because
14279967cafcSSam Leffler 		 * the deep copy of the tag chain failed), but for now
14289967cafcSSam Leffler 		 * be conservative and just discard the copy since
14299967cafcSSam Leffler 		 * code below may some day want the tags.
14309967cafcSSam Leffler 		 */
14319967cafcSSam Leffler 		m_free(mcopy);
14329967cafcSSam Leffler 		mcopy = NULL;
14339967cafcSSam Leffler 	}
1434bfef7ed4SIan Dowse 	if (mcopy != NULL) {
14358f134647SGleb Smirnoff 		mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy));
1436e6b0a570SBruce M Simpson 		mcopy->m_pkthdr.len = mcopy->m_len;
1437bfef7ed4SIan Dowse 		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1438bfef7ed4SIan Dowse 	}
143904287599SRuslan Ermilov 
144004287599SRuslan Ermilov #ifdef IPSTEALTH
1441603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
144204287599SRuslan Ermilov #endif
144304287599SRuslan Ermilov 		ip->ip_ttl -= IPTTLDEC;
144404287599SRuslan Ermilov #ifdef IPSTEALTH
144504287599SRuslan Ermilov 	}
144604287599SRuslan Ermilov #endif
1447df8bae1dSRodney W. Grimes 
1448df8bae1dSRodney W. Grimes 	/*
1449df8bae1dSRodney W. Grimes 	 * If forwarding packet using same interface that it came in on,
1450df8bae1dSRodney W. Grimes 	 * perhaps should send a redirect to sender to shortcut a hop.
1451df8bae1dSRodney W. Grimes 	 * Only send redirect if source is sending directly to us,
1452df8bae1dSRodney W. Grimes 	 * and if packet was not source routed (or has any options).
1453df8bae1dSRodney W. Grimes 	 * Also, don't send redirect if forwarding using a default route
1454df8bae1dSRodney W. Grimes 	 * or a route modified by a redirect.
1455df8bae1dSRodney W. Grimes 	 */
14569b932e9eSAndre Oppermann 	dest.s_addr = 0;
1457efbad259SEdward Tomasz Napierala 	if (!srcrt && V_ipsendredirects &&
1458efbad259SEdward Tomasz Napierala 	    ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) {
145902c1c707SAndre Oppermann 		struct sockaddr_in *sin;
146002c1c707SAndre Oppermann 		struct rtentry *rt;
146102c1c707SAndre Oppermann 
14620cfbbe3bSAndre Oppermann 		bzero(&ro, sizeof(ro));
146302c1c707SAndre Oppermann 		sin = (struct sockaddr_in *)&ro.ro_dst;
146402c1c707SAndre Oppermann 		sin->sin_family = AF_INET;
146502c1c707SAndre Oppermann 		sin->sin_len = sizeof(*sin);
14669b932e9eSAndre Oppermann 		sin->sin_addr = ip->ip_dst;
14676e6b3f7cSQing Li 		in_rtalloc_ign(&ro, 0, M_GETFIB(m));
146802c1c707SAndre Oppermann 
146902c1c707SAndre Oppermann 		rt = ro.ro_rt;
147002c1c707SAndre Oppermann 
147102c1c707SAndre Oppermann 		if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
14729b932e9eSAndre Oppermann 		    satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1473df8bae1dSRodney W. Grimes #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1474df8bae1dSRodney W. Grimes 			u_long src = ntohl(ip->ip_src.s_addr);
1475df8bae1dSRodney W. Grimes 
1476df8bae1dSRodney W. Grimes 			if (RTA(rt) &&
1477df8bae1dSRodney W. Grimes 			    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1478df8bae1dSRodney W. Grimes 				if (rt->rt_flags & RTF_GATEWAY)
14799b932e9eSAndre Oppermann 					dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1480df8bae1dSRodney W. Grimes 				else
14819b932e9eSAndre Oppermann 					dest.s_addr = ip->ip_dst.s_addr;
1482df8bae1dSRodney W. Grimes 				/* Router requirements says to only send host redirects */
1483df8bae1dSRodney W. Grimes 				type = ICMP_REDIRECT;
1484df8bae1dSRodney W. Grimes 				code = ICMP_REDIRECT_HOST;
1485df8bae1dSRodney W. Grimes 			}
1486df8bae1dSRodney W. Grimes 		}
148702c1c707SAndre Oppermann 		if (rt)
148802c1c707SAndre Oppermann 			RTFREE(rt);
148902c1c707SAndre Oppermann 	}
1490df8bae1dSRodney W. Grimes 
1491b835b6feSBjoern A. Zeeb 	/*
1492b835b6feSBjoern A. Zeeb 	 * Try to cache the route MTU from ip_output so we can consider it for
1493b835b6feSBjoern A. Zeeb 	 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191.
1494b835b6feSBjoern A. Zeeb 	 */
1495b835b6feSBjoern A. Zeeb 	bzero(&ro, sizeof(ro));
1496b835b6feSBjoern A. Zeeb 
1497b835b6feSBjoern A. Zeeb 	error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1498b835b6feSBjoern A. Zeeb 
1499b835b6feSBjoern A. Zeeb 	if (error == EMSGSIZE && ro.ro_rt)
1500b835b6feSBjoern A. Zeeb 		mtu = ro.ro_rt->rt_rmx.rmx_mtu;
1501bf984051SGleb Smirnoff 	RO_RTFREE(&ro);
1502b835b6feSBjoern A. Zeeb 
1503df8bae1dSRodney W. Grimes 	if (error)
150486425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
1505df8bae1dSRodney W. Grimes 	else {
150686425c62SRobert Watson 		IPSTAT_INC(ips_forward);
1507df8bae1dSRodney W. Grimes 		if (type)
150886425c62SRobert Watson 			IPSTAT_INC(ips_redirectsent);
1509df8bae1dSRodney W. Grimes 		else {
15109188b4a1SAndre Oppermann 			if (mcopy)
1511df8bae1dSRodney W. Grimes 				m_freem(mcopy);
15128c0fec80SRobert Watson 			if (ia != NULL)
15138c0fec80SRobert Watson 				ifa_free(&ia->ia_ifa);
1514df8bae1dSRodney W. Grimes 			return;
1515df8bae1dSRodney W. Grimes 		}
1516df8bae1dSRodney W. Grimes 	}
15178c0fec80SRobert Watson 	if (mcopy == NULL) {
15188c0fec80SRobert Watson 		if (ia != NULL)
15198c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
1520df8bae1dSRodney W. Grimes 		return;
15218c0fec80SRobert Watson 	}
1522df8bae1dSRodney W. Grimes 
1523df8bae1dSRodney W. Grimes 	switch (error) {
1524df8bae1dSRodney W. Grimes 
1525df8bae1dSRodney W. Grimes 	case 0:				/* forwarded, but need redirect */
1526df8bae1dSRodney W. Grimes 		/* type, code set above */
1527df8bae1dSRodney W. Grimes 		break;
1528df8bae1dSRodney W. Grimes 
1529efbad259SEdward Tomasz Napierala 	case ENETUNREACH:
1530df8bae1dSRodney W. Grimes 	case EHOSTUNREACH:
1531df8bae1dSRodney W. Grimes 	case ENETDOWN:
1532df8bae1dSRodney W. Grimes 	case EHOSTDOWN:
1533df8bae1dSRodney W. Grimes 	default:
1534df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1535df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_HOST;
1536df8bae1dSRodney W. Grimes 		break;
1537df8bae1dSRodney W. Grimes 
1538df8bae1dSRodney W. Grimes 	case EMSGSIZE:
1539df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1540df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_NEEDFRAG;
15411dfcf0d2SAndre Oppermann 
1542b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
1543b835b6feSBjoern A. Zeeb 		/*
1544b835b6feSBjoern A. Zeeb 		 * If IPsec is configured for this path,
1545b835b6feSBjoern A. Zeeb 		 * override any possibly mtu value set by ip_output.
1546b835b6feSBjoern A. Zeeb 		 */
15471c044382SBjoern A. Zeeb 		mtu = ip_ipsec_mtu(mcopy, mtu);
1548b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
15499b932e9eSAndre Oppermann 		/*
1550b835b6feSBjoern A. Zeeb 		 * If the MTU was set before make sure we are below the
1551b835b6feSBjoern A. Zeeb 		 * interface MTU.
1552ab48768bSAndre Oppermann 		 * If the MTU wasn't set before use the interface mtu or
1553ab48768bSAndre Oppermann 		 * fall back to the next smaller mtu step compared to the
1554ab48768bSAndre Oppermann 		 * current packet size.
15559b932e9eSAndre Oppermann 		 */
1556b835b6feSBjoern A. Zeeb 		if (mtu != 0) {
1557b835b6feSBjoern A. Zeeb 			if (ia != NULL)
1558b835b6feSBjoern A. Zeeb 				mtu = min(mtu, ia->ia_ifp->if_mtu);
1559b835b6feSBjoern A. Zeeb 		} else {
1560ab48768bSAndre Oppermann 			if (ia != NULL)
1561c773494eSAndre Oppermann 				mtu = ia->ia_ifp->if_mtu;
1562ab48768bSAndre Oppermann 			else
15638f134647SGleb Smirnoff 				mtu = ip_next_mtu(ntohs(ip->ip_len), 0);
1564ab48768bSAndre Oppermann 		}
156586425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
1566df8bae1dSRodney W. Grimes 		break;
1567df8bae1dSRodney W. Grimes 
1568df8bae1dSRodney W. Grimes 	case ENOBUFS:
1569df285b3dSMike Silbersack 		/*
1570df285b3dSMike Silbersack 		 * A router should not generate ICMP_SOURCEQUENCH as
1571df285b3dSMike Silbersack 		 * required in RFC1812 Requirements for IP Version 4 Routers.
1572df285b3dSMike Silbersack 		 * Source quench could be a big problem under DoS attacks,
1573df285b3dSMike Silbersack 		 * or if the underlying interface is rate-limited.
1574df285b3dSMike Silbersack 		 * Those who need source quench packets may re-enable them
1575df285b3dSMike Silbersack 		 * via the net.inet.ip.sendsourcequench sysctl.
1576df285b3dSMike Silbersack 		 */
1577603724d3SBjoern A. Zeeb 		if (V_ip_sendsourcequench == 0) {
1578df285b3dSMike Silbersack 			m_freem(mcopy);
15798c0fec80SRobert Watson 			if (ia != NULL)
15808c0fec80SRobert Watson 				ifa_free(&ia->ia_ifa);
1581df285b3dSMike Silbersack 			return;
1582df285b3dSMike Silbersack 		} else {
1583df8bae1dSRodney W. Grimes 			type = ICMP_SOURCEQUENCH;
1584df8bae1dSRodney W. Grimes 			code = 0;
1585df285b3dSMike Silbersack 		}
1586df8bae1dSRodney W. Grimes 		break;
15873a06e3e0SRuslan Ermilov 
15883a06e3e0SRuslan Ermilov 	case EACCES:			/* ipfw denied packet */
15893a06e3e0SRuslan Ermilov 		m_freem(mcopy);
15908c0fec80SRobert Watson 		if (ia != NULL)
15918c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
15923a06e3e0SRuslan Ermilov 		return;
1593df8bae1dSRodney W. Grimes 	}
15948c0fec80SRobert Watson 	if (ia != NULL)
15958c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
1596c773494eSAndre Oppermann 	icmp_error(mcopy, type, code, dest.s_addr, mtu);
1597df8bae1dSRodney W. Grimes }
1598df8bae1dSRodney W. Grimes 
159982c23ebaSBill Fenner void
1600f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1601f2565d68SRobert Watson     struct mbuf *m)
160282c23ebaSBill Fenner {
16038b615593SMarko Zec 
1604be8a62e8SPoul-Henning Kamp 	if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1605be8a62e8SPoul-Henning Kamp 		struct bintime bt;
1606be8a62e8SPoul-Henning Kamp 
1607be8a62e8SPoul-Henning Kamp 		bintime(&bt);
1608be8a62e8SPoul-Henning Kamp 		if (inp->inp_socket->so_options & SO_BINTIME) {
1609be8a62e8SPoul-Henning Kamp 			*mp = sbcreatecontrol((caddr_t)&bt, sizeof(bt),
1610be8a62e8SPoul-Henning Kamp 			    SCM_BINTIME, SOL_SOCKET);
1611be8a62e8SPoul-Henning Kamp 			if (*mp)
1612be8a62e8SPoul-Henning Kamp 				mp = &(*mp)->m_next;
1613be8a62e8SPoul-Henning Kamp 		}
161482c23ebaSBill Fenner 		if (inp->inp_socket->so_options & SO_TIMESTAMP) {
161582c23ebaSBill Fenner 			struct timeval tv;
161682c23ebaSBill Fenner 
1617be8a62e8SPoul-Henning Kamp 			bintime2timeval(&bt, &tv);
161882c23ebaSBill Fenner 			*mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv),
161982c23ebaSBill Fenner 			    SCM_TIMESTAMP, SOL_SOCKET);
162082c23ebaSBill Fenner 			if (*mp)
162182c23ebaSBill Fenner 				mp = &(*mp)->m_next;
16224cc20ab1SSeigo Tanimura 		}
1623be8a62e8SPoul-Henning Kamp 	}
162482c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVDSTADDR) {
162582c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t)&ip->ip_dst,
162682c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
162782c23ebaSBill Fenner 		if (*mp)
162882c23ebaSBill Fenner 			mp = &(*mp)->m_next;
162982c23ebaSBill Fenner 	}
16304957466bSMatthew N. Dodd 	if (inp->inp_flags & INP_RECVTTL) {
16314957466bSMatthew N. Dodd 		*mp = sbcreatecontrol((caddr_t)&ip->ip_ttl,
16324957466bSMatthew N. Dodd 		    sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
16334957466bSMatthew N. Dodd 		if (*mp)
16344957466bSMatthew N. Dodd 			mp = &(*mp)->m_next;
16354957466bSMatthew N. Dodd 	}
163682c23ebaSBill Fenner #ifdef notyet
163782c23ebaSBill Fenner 	/* XXX
163882c23ebaSBill Fenner 	 * Moving these out of udp_input() made them even more broken
163982c23ebaSBill Fenner 	 * than they already were.
164082c23ebaSBill Fenner 	 */
164182c23ebaSBill Fenner 	/* options were tossed already */
164282c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVOPTS) {
164382c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t)opts_deleted_above,
164482c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
164582c23ebaSBill Fenner 		if (*mp)
164682c23ebaSBill Fenner 			mp = &(*mp)->m_next;
164782c23ebaSBill Fenner 	}
164882c23ebaSBill Fenner 	/* ip_srcroute doesn't do what we want here, need to fix */
164982c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVRETOPTS) {
1650e0982661SAndre Oppermann 		*mp = sbcreatecontrol((caddr_t)ip_srcroute(m),
165182c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
165282c23ebaSBill Fenner 		if (*mp)
165382c23ebaSBill Fenner 			mp = &(*mp)->m_next;
165482c23ebaSBill Fenner 	}
165582c23ebaSBill Fenner #endif
165682c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVIF) {
1657d314ad7bSJulian Elischer 		struct ifnet *ifp;
1658d314ad7bSJulian Elischer 		struct sdlbuf {
165982c23ebaSBill Fenner 			struct sockaddr_dl sdl;
1660d314ad7bSJulian Elischer 			u_char	pad[32];
1661d314ad7bSJulian Elischer 		} sdlbuf;
1662d314ad7bSJulian Elischer 		struct sockaddr_dl *sdp;
1663d314ad7bSJulian Elischer 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
166482c23ebaSBill Fenner 
166546f2df9cSSergey Kandaurov 		if ((ifp = m->m_pkthdr.rcvif) &&
166646f2df9cSSergey Kandaurov 		    ifp->if_index && ifp->if_index <= V_if_index) {
16674a0d6638SRuslan Ermilov 			sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1668d314ad7bSJulian Elischer 			/*
1669d314ad7bSJulian Elischer 			 * Change our mind and don't try copy.
1670d314ad7bSJulian Elischer 			 */
167146f2df9cSSergey Kandaurov 			if (sdp->sdl_family != AF_LINK ||
167246f2df9cSSergey Kandaurov 			    sdp->sdl_len > sizeof(sdlbuf)) {
1673d314ad7bSJulian Elischer 				goto makedummy;
1674d314ad7bSJulian Elischer 			}
1675d314ad7bSJulian Elischer 			bcopy(sdp, sdl2, sdp->sdl_len);
1676d314ad7bSJulian Elischer 		} else {
1677d314ad7bSJulian Elischer makedummy:
167846f2df9cSSergey Kandaurov 			sdl2->sdl_len =
167946f2df9cSSergey Kandaurov 			    offsetof(struct sockaddr_dl, sdl_data[0]);
1680d314ad7bSJulian Elischer 			sdl2->sdl_family = AF_LINK;
1681d314ad7bSJulian Elischer 			sdl2->sdl_index = 0;
1682d314ad7bSJulian Elischer 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1683d314ad7bSJulian Elischer 		}
1684d314ad7bSJulian Elischer 		*mp = sbcreatecontrol((caddr_t)sdl2, sdl2->sdl_len,
168582c23ebaSBill Fenner 		    IP_RECVIF, IPPROTO_IP);
168682c23ebaSBill Fenner 		if (*mp)
168782c23ebaSBill Fenner 			mp = &(*mp)->m_next;
168882c23ebaSBill Fenner 	}
16893cca425bSMichael Tuexen 	if (inp->inp_flags & INP_RECVTOS) {
16903cca425bSMichael Tuexen 		*mp = sbcreatecontrol((caddr_t)&ip->ip_tos,
16913cca425bSMichael Tuexen 		    sizeof(u_char), IP_RECVTOS, IPPROTO_IP);
16923cca425bSMichael Tuexen 		if (*mp)
16933cca425bSMichael Tuexen 			mp = &(*mp)->m_next;
16943cca425bSMichael Tuexen 	}
169582c23ebaSBill Fenner }
169682c23ebaSBill Fenner 
16974d2e3692SLuigi Rizzo /*
169830916a2dSRobert Watson  * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
169930916a2dSRobert Watson  * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
170030916a2dSRobert Watson  * locking.  This code remains in ip_input.c as ip_mroute.c is optionally
170130916a2dSRobert Watson  * compiled.
17024d2e3692SLuigi Rizzo  */
17033e288e62SDimitry Andric static VNET_DEFINE(int, ip_rsvp_on);
170482cea7e6SBjoern A. Zeeb VNET_DEFINE(struct socket *, ip_rsvpd);
170582cea7e6SBjoern A. Zeeb 
170682cea7e6SBjoern A. Zeeb #define	V_ip_rsvp_on		VNET(ip_rsvp_on)
170782cea7e6SBjoern A. Zeeb 
1708df8bae1dSRodney W. Grimes int
1709f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so)
1710f0068c4aSGarrett Wollman {
17118b615593SMarko Zec 
1712f0068c4aSGarrett Wollman 	if (so->so_type != SOCK_RAW ||
1713f0068c4aSGarrett Wollman 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1714f0068c4aSGarrett Wollman 		return EOPNOTSUPP;
1715f0068c4aSGarrett Wollman 
1716603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL)
1717f0068c4aSGarrett Wollman 		return EADDRINUSE;
1718f0068c4aSGarrett Wollman 
1719603724d3SBjoern A. Zeeb 	V_ip_rsvpd = so;
17201c5de19aSGarrett Wollman 	/*
17211c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-increment
17221c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
17231c5de19aSGarrett Wollman 	 */
1724603724d3SBjoern A. Zeeb 	if (!V_ip_rsvp_on) {
1725603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 1;
1726603724d3SBjoern A. Zeeb 		V_rsvp_on++;
17271c5de19aSGarrett Wollman 	}
1728f0068c4aSGarrett Wollman 
1729f0068c4aSGarrett Wollman 	return 0;
1730f0068c4aSGarrett Wollman }
1731f0068c4aSGarrett Wollman 
1732f0068c4aSGarrett Wollman int
1733f0068c4aSGarrett Wollman ip_rsvp_done(void)
1734f0068c4aSGarrett Wollman {
17358b615593SMarko Zec 
1736603724d3SBjoern A. Zeeb 	V_ip_rsvpd = NULL;
17371c5de19aSGarrett Wollman 	/*
17381c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-decrement
17391c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
17401c5de19aSGarrett Wollman 	 */
1741603724d3SBjoern A. Zeeb 	if (V_ip_rsvp_on) {
1742603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 0;
1743603724d3SBjoern A. Zeeb 		V_rsvp_on--;
17441c5de19aSGarrett Wollman 	}
1745f0068c4aSGarrett Wollman 	return 0;
1746f0068c4aSGarrett Wollman }
1747bbb4330bSLuigi Rizzo 
1748bbb4330bSLuigi Rizzo void
1749bbb4330bSLuigi Rizzo rsvp_input(struct mbuf *m, int off)	/* XXX must fixup manually */
1750bbb4330bSLuigi Rizzo {
17518b615593SMarko Zec 
1752bbb4330bSLuigi Rizzo 	if (rsvp_input_p) { /* call the real one if loaded */
1753bbb4330bSLuigi Rizzo 		rsvp_input_p(m, off);
1754bbb4330bSLuigi Rizzo 		return;
1755bbb4330bSLuigi Rizzo 	}
1756bbb4330bSLuigi Rizzo 
1757bbb4330bSLuigi Rizzo 	/* Can still get packets with rsvp_on = 0 if there is a local member
1758bbb4330bSLuigi Rizzo 	 * of the group to which the RSVP packet is addressed.  But in this
1759bbb4330bSLuigi Rizzo 	 * case we want to throw the packet away.
1760bbb4330bSLuigi Rizzo 	 */
1761bbb4330bSLuigi Rizzo 
1762603724d3SBjoern A. Zeeb 	if (!V_rsvp_on) {
1763bbb4330bSLuigi Rizzo 		m_freem(m);
1764bbb4330bSLuigi Rizzo 		return;
1765bbb4330bSLuigi Rizzo 	}
1766bbb4330bSLuigi Rizzo 
1767603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL) {
1768bbb4330bSLuigi Rizzo 		rip_input(m, off);
1769bbb4330bSLuigi Rizzo 		return;
1770bbb4330bSLuigi Rizzo 	}
1771bbb4330bSLuigi Rizzo 	/* Drop the packet */
1772bbb4330bSLuigi Rizzo 	m_freem(m);
1773bbb4330bSLuigi Rizzo }
1774