xref: /freebsd/sys/netinet/ip_input.c (revision ffe8cd7b104ecc8969ec36c065629f1b70c3b3f5)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
324b421e2dSMike Silbersack #include <sys/cdefs.h>
334b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
344b421e2dSMike Silbersack 
350ac40133SBrian Somers #include "opt_bootp.h"
3674a9466cSGary Palmer #include "opt_ipfw.h"
3727108a15SDag-Erling Smørgrav #include "opt_ipstealth.h"
386a800098SYoshinobu Inoue #include "opt_ipsec.h"
3933553d6eSBjoern A. Zeeb #include "opt_route.h"
4074a9466cSGary Palmer 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
44b715f178SLuigi Rizzo #include <sys/malloc.h>
45df8bae1dSRodney W. Grimes #include <sys/domain.h>
46df8bae1dSRodney W. Grimes #include <sys/protosw.h>
47df8bae1dSRodney W. Grimes #include <sys/socket.h>
48df8bae1dSRodney W. Grimes #include <sys/time.h>
49df8bae1dSRodney W. Grimes #include <sys/kernel.h>
50385195c0SMarko Zec #include <sys/lock.h>
51385195c0SMarko Zec #include <sys/rwlock.h>
521025071fSGarrett Wollman #include <sys/syslog.h>
53b5e8ce9fSBruce Evans #include <sys/sysctl.h>
54df8bae1dSRodney W. Grimes 
55c85540ddSAndrey A. Chernov #include <net/pfil.h>
56df8bae1dSRodney W. Grimes #include <net/if.h>
579494d596SBrooks Davis #include <net/if_types.h>
58d314ad7bSJulian Elischer #include <net/if_var.h>
5982c23ebaSBill Fenner #include <net/if_dl.h>
60df8bae1dSRodney W. Grimes #include <net/route.h>
61748e0b0aSGarrett Wollman #include <net/netisr.h>
624b79449eSBjoern A. Zeeb #include <net/vnet.h>
6365111ec7SKip Macy #include <net/flowtable.h>
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes #include <netinet/in.h>
66df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
67b5e8ce9fSBruce Evans #include <netinet/in_var.h>
68df8bae1dSRodney W. Grimes #include <netinet/ip.h>
69df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
70df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
71eddfbb76SRobert Watson #include <netinet/ip_fw.h>
72df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
73ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
7458938916SGarrett Wollman #include <machine/in_cksum.h>
75a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
76b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
771dfcf0d2SAndre Oppermann #include <netinet/ip_ipsec.h>
78b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
79df8bae1dSRodney W. Grimes 
80f0068c4aSGarrett Wollman #include <sys/socketvar.h>
816ddbf1e2SGary Palmer 
82aed55708SRobert Watson #include <security/mac/mac_framework.h>
83aed55708SRobert Watson 
84d2035ffbSEd Maste #ifdef CTASSERT
85d2035ffbSEd Maste CTASSERT(sizeof(struct ip) == 20);
86d2035ffbSEd Maste #endif
87d2035ffbSEd Maste 
882d9cfabaSRobert Watson struct	rwlock in_ifaddr_lock;
8964aeca7bSRobert Watson RW_SYSINIT(in_ifaddr_lock, &in_ifaddr_lock, "in_ifaddr_lock");
90f0068c4aSGarrett Wollman 
9182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, rsvp_on);
9282cea7e6SBjoern A. Zeeb 
9382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ipforwarding);
94eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
95eddfbb76SRobert Watson     &VNET_NAME(ipforwarding), 0,
968b615593SMarko Zec     "Enable IP forwarding between interfaces");
970312fbe9SPoul-Henning Kamp 
983e288e62SDimitry Andric static VNET_DEFINE(int, ipsendredirects) = 1;	/* XXX */
9982cea7e6SBjoern A. Zeeb #define	V_ipsendredirects	VNET(ipsendredirects)
100eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
101eddfbb76SRobert Watson     &VNET_NAME(ipsendredirects), 0,
1028b615593SMarko Zec     "Enable sending IP redirects");
1030312fbe9SPoul-Henning Kamp 
1043e288e62SDimitry Andric static VNET_DEFINE(int, ip_keepfaith);
10582cea7e6SBjoern A. Zeeb #define	V_ip_keepfaith		VNET(ip_keepfaith)
106eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
107eddfbb76SRobert Watson     &VNET_NAME(ip_keepfaith), 0,
1086a800098SYoshinobu Inoue     "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
1096a800098SYoshinobu Inoue 
1103e288e62SDimitry Andric static VNET_DEFINE(int, ip_sendsourcequench);
11182cea7e6SBjoern A. Zeeb #define	V_ip_sendsourcequench	VNET(ip_sendsourcequench)
112eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW,
113eddfbb76SRobert Watson     &VNET_NAME(ip_sendsourcequench), 0,
114df285b3dSMike Silbersack     "Enable the transmission of source quench packets");
115df285b3dSMike Silbersack 
11682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip_do_randomid);
117eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW,
118eddfbb76SRobert Watson     &VNET_NAME(ip_do_randomid), 0,
119eddfbb76SRobert Watson     "Assign random ip_id values");
1201f44b0a1SDavid Malone 
121823db0e9SDon Lewis /*
122823db0e9SDon Lewis  * XXX - Setting ip_checkinterface mostly implements the receive side of
123823db0e9SDon Lewis  * the Strong ES model described in RFC 1122, but since the routing table
124a8f12100SDon Lewis  * and transmit implementation do not implement the Strong ES model,
125823db0e9SDon Lewis  * setting this to 1 results in an odd hybrid.
1263f67c834SDon Lewis  *
127a8f12100SDon Lewis  * XXX - ip_checkinterface currently must be disabled if you use ipnat
128a8f12100SDon Lewis  * to translate the destination address to another local interface.
1293f67c834SDon Lewis  *
1303f67c834SDon Lewis  * XXX - ip_checkinterface must be disabled if you add IP aliases
1313f67c834SDon Lewis  * to the loopback interface instead of the interface where the
1323f67c834SDon Lewis  * packets for those addresses are received.
133823db0e9SDon Lewis  */
1343e288e62SDimitry Andric static VNET_DEFINE(int, ip_checkinterface);
13582cea7e6SBjoern A. Zeeb #define	V_ip_checkinterface	VNET(ip_checkinterface)
136eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
137eddfbb76SRobert Watson     &VNET_NAME(ip_checkinterface), 0,
1388b615593SMarko Zec     "Verify packet arrives on correct interface");
139b3e95d4eSJonathan Lemon 
1400b4b0b0fSJulian Elischer VNET_DEFINE(struct pfil_head, inet_pfil_hook);	/* Packet filter hooks */
141df8bae1dSRodney W. Grimes 
142d4b5cae4SRobert Watson static struct netisr_handler ip_nh = {
143d4b5cae4SRobert Watson 	.nh_name = "ip",
144d4b5cae4SRobert Watson 	.nh_handler = ip_input,
145d4b5cae4SRobert Watson 	.nh_proto = NETISR_IP,
146d4b5cae4SRobert Watson 	.nh_policy = NETISR_POLICY_FLOW,
147d4b5cae4SRobert Watson };
148ca925d9cSJonathan Lemon 
149df8bae1dSRodney W. Grimes extern	struct domain inetdomain;
150f0ffb944SJulian Elischer extern	struct protosw inetsw[];
151df8bae1dSRodney W. Grimes u_char	ip_protox[IPPROTO_MAX];
15282cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead);  /* first inet address */
15382cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); /* inet addr hash table  */
15482cea7e6SBjoern A. Zeeb VNET_DEFINE(u_long, in_ifaddrhmask);		/* mask for hash table */
155ca925d9cSJonathan Lemon 
15682cea7e6SBjoern A. Zeeb VNET_DEFINE(struct ipstat, ipstat);
157eddfbb76SRobert Watson SYSCTL_VNET_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
158eddfbb76SRobert Watson     &VNET_NAME(ipstat), ipstat,
159eddfbb76SRobert Watson     "IP statistics (struct ipstat, netinet/ip_var.h)");
160df8bae1dSRodney W. Grimes 
1613e288e62SDimitry Andric static VNET_DEFINE(uma_zone_t, ipq_zone);
1623e288e62SDimitry Andric static VNET_DEFINE(TAILQ_HEAD(ipqhead, ipq), ipq[IPREASS_NHASH]);
163dfa60d93SRobert Watson static struct mtx ipqlock;
1642fad1e93SSam Leffler 
16582cea7e6SBjoern A. Zeeb #define	V_ipq_zone		VNET(ipq_zone)
16682cea7e6SBjoern A. Zeeb #define	V_ipq			VNET(ipq)
16782cea7e6SBjoern A. Zeeb 
1682fad1e93SSam Leffler #define	IPQ_LOCK()	mtx_lock(&ipqlock)
1692fad1e93SSam Leffler #define	IPQ_UNLOCK()	mtx_unlock(&ipqlock)
170888c2a3cSSam Leffler #define	IPQ_LOCK_INIT()	mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
171888c2a3cSSam Leffler #define	IPQ_LOCK_ASSERT()	mtx_assert(&ipqlock, MA_OWNED)
172f23b4c91SGarrett Wollman 
173d248c7d7SRobert Watson static void	maxnipq_update(void);
1744f590175SPaul Saab static void	ipq_zone_change(void *);
1759802380eSBjoern A. Zeeb static void	ip_drain_locked(void);
176d248c7d7SRobert Watson 
1773e288e62SDimitry Andric static VNET_DEFINE(int, maxnipq);  /* Administrative limit on # reass queues. */
1783e288e62SDimitry Andric static VNET_DEFINE(int, nipq);			/* Total # of reass queues */
17982cea7e6SBjoern A. Zeeb #define	V_maxnipq		VNET(maxnipq)
18082cea7e6SBjoern A. Zeeb #define	V_nipq			VNET(nipq)
181eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_RD,
182eddfbb76SRobert Watson     &VNET_NAME(nipq), 0,
1838b615593SMarko Zec     "Current number of IPv4 fragment reassembly queue entries");
184d248c7d7SRobert Watson 
1853e288e62SDimitry Andric static VNET_DEFINE(int, maxfragsperpacket);
18682cea7e6SBjoern A. Zeeb #define	V_maxfragsperpacket	VNET(maxfragsperpacket)
187eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW,
188eddfbb76SRobert Watson     &VNET_NAME(maxfragsperpacket), 0,
189d248c7d7SRobert Watson     "Maximum number of IPv4 fragments allowed per packet");
190d248c7d7SRobert Watson 
1910312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU
1920312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
1933d177f46SBill Fumerola     &ip_mtu, 0, "Default MTU");
1940312fbe9SPoul-Henning Kamp #endif
1950312fbe9SPoul-Henning Kamp 
1961b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
19782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ipstealth);
198eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
199eddfbb76SRobert Watson     &VNET_NAME(ipstealth), 0,
200eddfbb76SRobert Watson     "IP stealth mode, no TTL decrementation on forwarding");
2011b968362SDag-Erling Smørgrav #endif
202eddfbb76SRobert Watson 
20353be8fcaSBjoern A. Zeeb #ifdef FLOWTABLE
2043e288e62SDimitry Andric static VNET_DEFINE(int, ip_output_flowtable_size) = 2048;
205eddfbb76SRobert Watson VNET_DEFINE(struct flowtable *, ip_ft);
2061e77c105SRobert Watson #define	V_ip_output_flowtable_size	VNET(ip_output_flowtable_size)
207eddfbb76SRobert Watson 
208eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, output_flowtable_size, CTLFLAG_RDTUN,
209eddfbb76SRobert Watson     &VNET_NAME(ip_output_flowtable_size), 2048,
21065111ec7SKip Macy     "number of entries in the per-cpu output flow caches");
21153be8fcaSBjoern A. Zeeb #endif
21253be8fcaSBjoern A. Zeeb 
2134d77a549SAlfred Perlstein static void	ip_freef(struct ipqhead *, struct ipq *);
2148948e4baSArchie Cobbs 
215315e3e38SRobert Watson /*
216315e3e38SRobert Watson  * Kernel module interface for updating ipstat.  The argument is an index
217315e3e38SRobert Watson  * into ipstat treated as an array of u_long.  While this encodes the general
218315e3e38SRobert Watson  * layout of ipstat into the caller, it doesn't encode its location, so that
219315e3e38SRobert Watson  * future changes to add, for example, per-CPU stats support won't cause
220315e3e38SRobert Watson  * binary compatibility problems for kernel modules.
221315e3e38SRobert Watson  */
222315e3e38SRobert Watson void
223315e3e38SRobert Watson kmod_ipstat_inc(int statnum)
224315e3e38SRobert Watson {
225315e3e38SRobert Watson 
226315e3e38SRobert Watson 	(*((u_long *)&V_ipstat + statnum))++;
227315e3e38SRobert Watson }
228315e3e38SRobert Watson 
229315e3e38SRobert Watson void
230315e3e38SRobert Watson kmod_ipstat_dec(int statnum)
231315e3e38SRobert Watson {
232315e3e38SRobert Watson 
233315e3e38SRobert Watson 	(*((u_long *)&V_ipstat + statnum))--;
234315e3e38SRobert Watson }
235315e3e38SRobert Watson 
236d4b5cae4SRobert Watson static int
237d4b5cae4SRobert Watson sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
238d4b5cae4SRobert Watson {
239d4b5cae4SRobert Watson 	int error, qlimit;
240d4b5cae4SRobert Watson 
241d4b5cae4SRobert Watson 	netisr_getqlimit(&ip_nh, &qlimit);
242d4b5cae4SRobert Watson 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
243d4b5cae4SRobert Watson 	if (error || !req->newptr)
244d4b5cae4SRobert Watson 		return (error);
245d4b5cae4SRobert Watson 	if (qlimit < 1)
246d4b5cae4SRobert Watson 		return (EINVAL);
247d4b5cae4SRobert Watson 	return (netisr_setqlimit(&ip_nh, qlimit));
248d4b5cae4SRobert Watson }
249d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen,
250d4b5cae4SRobert Watson     CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_queue_maxlen, "I",
251d4b5cae4SRobert Watson     "Maximum size of the IP input queue");
252d4b5cae4SRobert Watson 
253d4b5cae4SRobert Watson static int
254d4b5cae4SRobert Watson sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS)
255d4b5cae4SRobert Watson {
256d4b5cae4SRobert Watson 	u_int64_t qdrops_long;
257d4b5cae4SRobert Watson 	int error, qdrops;
258d4b5cae4SRobert Watson 
259d4b5cae4SRobert Watson 	netisr_getqdrops(&ip_nh, &qdrops_long);
260d4b5cae4SRobert Watson 	qdrops = qdrops_long;
261d4b5cae4SRobert Watson 	error = sysctl_handle_int(oidp, &qdrops, 0, req);
262d4b5cae4SRobert Watson 	if (error || !req->newptr)
263d4b5cae4SRobert Watson 		return (error);
264d4b5cae4SRobert Watson 	if (qdrops != 0)
265d4b5cae4SRobert Watson 		return (EINVAL);
266d4b5cae4SRobert Watson 	netisr_clearqdrops(&ip_nh);
267d4b5cae4SRobert Watson 	return (0);
268d4b5cae4SRobert Watson }
269d4b5cae4SRobert Watson 
270d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops,
271d4b5cae4SRobert Watson     CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_queue_drops, "I",
272d4b5cae4SRobert Watson     "Number of packets dropped from the IP input queue");
273d4b5cae4SRobert Watson 
274df8bae1dSRodney W. Grimes /*
275df8bae1dSRodney W. Grimes  * IP initialization: fill in IP protocol switch table.
276df8bae1dSRodney W. Grimes  * All protocols not implemented in kernel go to raw IP protocol handler.
277df8bae1dSRodney W. Grimes  */
278df8bae1dSRodney W. Grimes void
279f2565d68SRobert Watson ip_init(void)
280df8bae1dSRodney W. Grimes {
281f2565d68SRobert Watson 	struct protosw *pr;
282f2565d68SRobert Watson 	int i;
283df8bae1dSRodney W. Grimes 
284a511354aSRobert Watson 	V_ip_id = time_second & 0xffff;
285a511354aSRobert Watson 
286603724d3SBjoern A. Zeeb 	TAILQ_INIT(&V_in_ifaddrhead);
287603724d3SBjoern A. Zeeb 	V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
2881ed81b73SMarko Zec 
2891ed81b73SMarko Zec 	/* Initialize IP reassembly queue. */
2901ed81b73SMarko Zec 	for (i = 0; i < IPREASS_NHASH; i++)
2911ed81b73SMarko Zec 		TAILQ_INIT(&V_ipq[i]);
2921ed81b73SMarko Zec 	V_maxnipq = nmbclusters / 32;
2931ed81b73SMarko Zec 	V_maxfragsperpacket = 16;
2941ed81b73SMarko Zec 	V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
2951ed81b73SMarko Zec 	    NULL, UMA_ALIGN_PTR, 0);
2961ed81b73SMarko Zec 	maxnipq_update();
2971ed81b73SMarko Zec 
2980b4b0b0fSJulian Elischer 	/* Initialize packet filter hooks. */
2990b4b0b0fSJulian Elischer 	V_inet_pfil_hook.ph_type = PFIL_TYPE_AF;
3000b4b0b0fSJulian Elischer 	V_inet_pfil_hook.ph_af = AF_INET;
3010b4b0b0fSJulian Elischer 	if ((i = pfil_head_register(&V_inet_pfil_hook)) != 0)
3020b4b0b0fSJulian Elischer 		printf("%s: WARNING: unable to register pfil hook, "
3030b4b0b0fSJulian Elischer 			"error %d\n", __func__, i);
3040b4b0b0fSJulian Elischer 
305fa057b15SMarko Zec #ifdef FLOWTABLE
3063059584eSKip Macy 	if (TUNABLE_INT_FETCH("net.inet.ip.output_flowtable_size",
3073059584eSKip Macy 		&V_ip_output_flowtable_size)) {
3083059584eSKip Macy 		if (V_ip_output_flowtable_size < 256)
3093059584eSKip Macy 			V_ip_output_flowtable_size = 256;
3103059584eSKip Macy 		if (!powerof2(V_ip_output_flowtable_size)) {
3113059584eSKip Macy 			printf("flowtable must be power of 2 size\n");
3123059584eSKip Macy 			V_ip_output_flowtable_size = 2048;
3133059584eSKip Macy 		}
3143059584eSKip Macy 	} else {
3153059584eSKip Macy 		/*
3163059584eSKip Macy 		 * round up to the next power of 2
3173059584eSKip Macy 		 */
3183059584eSKip Macy 		V_ip_output_flowtable_size = 1 << fls((1024 + maxusers * 64)-1);
3193059584eSKip Macy 	}
320d4121a02SKip Macy 	V_ip_ft = flowtable_alloc("ipv4", V_ip_output_flowtable_size, FL_PCPU);
321fa057b15SMarko Zec #endif
322fa057b15SMarko Zec 
3231ed81b73SMarko Zec 	/* Skip initialization of globals for non-default instances. */
3241ed81b73SMarko Zec 	if (!IS_DEFAULT_VNET(curvnet))
3251ed81b73SMarko Zec 		return;
3261ed81b73SMarko Zec 
327f0ffb944SJulian Elischer 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
32802410549SRobert Watson 	if (pr == NULL)
329db09bef3SAndre Oppermann 		panic("ip_init: PF_INET not found");
330db09bef3SAndre Oppermann 
331db09bef3SAndre Oppermann 	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
332df8bae1dSRodney W. Grimes 	for (i = 0; i < IPPROTO_MAX; i++)
333df8bae1dSRodney W. Grimes 		ip_protox[i] = pr - inetsw;
334db09bef3SAndre Oppermann 	/*
335db09bef3SAndre Oppermann 	 * Cycle through IP protocols and put them into the appropriate place
336db09bef3SAndre Oppermann 	 * in ip_protox[].
337db09bef3SAndre Oppermann 	 */
338f0ffb944SJulian Elischer 	for (pr = inetdomain.dom_protosw;
339f0ffb944SJulian Elischer 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
340df8bae1dSRodney W. Grimes 		if (pr->pr_domain->dom_family == PF_INET &&
341db09bef3SAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
342db09bef3SAndre Oppermann 			/* Be careful to only index valid IP protocols. */
343db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX)
344df8bae1dSRodney W. Grimes 				ip_protox[pr->pr_protocol] = pr - inetsw;
345db09bef3SAndre Oppermann 		}
346194a213eSAndrey A. Chernov 
3474f590175SPaul Saab 	EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
3484f590175SPaul Saab 		NULL, EVENTHANDLER_PRI_ANY);
3495f311da2SMike Silbersack 
350db09bef3SAndre Oppermann 	/* Initialize various other remaining things. */
3511ed81b73SMarko Zec 	IPQ_LOCK_INIT();
352d4b5cae4SRobert Watson 	netisr_register(&ip_nh);
353df8bae1dSRodney W. Grimes }
354df8bae1dSRodney W. Grimes 
3559802380eSBjoern A. Zeeb #ifdef VIMAGE
3569802380eSBjoern A. Zeeb void
3579802380eSBjoern A. Zeeb ip_destroy(void)
3589802380eSBjoern A. Zeeb {
3599802380eSBjoern A. Zeeb 
3609802380eSBjoern A. Zeeb 	/* Cleanup in_ifaddr hash table; should be empty. */
3619802380eSBjoern A. Zeeb 	hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask);
3629802380eSBjoern A. Zeeb 
3639802380eSBjoern A. Zeeb 	IPQ_LOCK();
3649802380eSBjoern A. Zeeb 	ip_drain_locked();
3659802380eSBjoern A. Zeeb 	IPQ_UNLOCK();
3669802380eSBjoern A. Zeeb 
3679802380eSBjoern A. Zeeb 	uma_zdestroy(V_ipq_zone);
3689802380eSBjoern A. Zeeb }
3699802380eSBjoern A. Zeeb #endif
3709802380eSBjoern A. Zeeb 
3714d2e3692SLuigi Rizzo /*
372df8bae1dSRodney W. Grimes  * Ip input routine.  Checksum and byte swap header.  If fragmented
373df8bae1dSRodney W. Grimes  * try to reassemble.  Process options.  Pass to next level.
374df8bae1dSRodney W. Grimes  */
375c67b1d17SGarrett Wollman void
376c67b1d17SGarrett Wollman ip_input(struct mbuf *m)
377df8bae1dSRodney W. Grimes {
3789188b4a1SAndre Oppermann 	struct ip *ip = NULL;
3795da9f8faSJosef Karthauser 	struct in_ifaddr *ia = NULL;
380ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
3810aade26eSRobert Watson 	struct ifnet *ifp;
3829b932e9eSAndre Oppermann 	int    checkif, hlen = 0;
38347c861ecSBrian Somers 	u_short sum;
38402c1c707SAndre Oppermann 	int dchg = 0;				/* dest changed after fw */
385f51f805fSSam Leffler 	struct in_addr odst;			/* original dst address */
386b715f178SLuigi Rizzo 
387fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
388db40007dSAndrew R. Reiter 
389ac9d7e26SMax Laier 	if (m->m_flags & M_FASTFWD_OURS) {
3909b932e9eSAndre Oppermann 		/*
39176ff6dcfSAndre Oppermann 		 * Firewall or NAT changed destination to local.
39276ff6dcfSAndre Oppermann 		 * We expect ip_len and ip_off to be in host byte order.
3939b932e9eSAndre Oppermann 		 */
39476ff6dcfSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
39576ff6dcfSAndre Oppermann 		/* Set up some basics that will be used later. */
3962b25acc1SLuigi Rizzo 		ip = mtod(m, struct ip *);
39753be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
3989b932e9eSAndre Oppermann 		goto ours;
3992b25acc1SLuigi Rizzo 	}
4002b25acc1SLuigi Rizzo 
40186425c62SRobert Watson 	IPSTAT_INC(ips_total);
40258938916SGarrett Wollman 
40358938916SGarrett Wollman 	if (m->m_pkthdr.len < sizeof(struct ip))
40458938916SGarrett Wollman 		goto tooshort;
40558938916SGarrett Wollman 
406df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct ip) &&
4070b17fba7SAndre Oppermann 	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
40886425c62SRobert Watson 		IPSTAT_INC(ips_toosmall);
409c67b1d17SGarrett Wollman 		return;
410df8bae1dSRodney W. Grimes 	}
411df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
41258938916SGarrett Wollman 
41353be11f6SPoul-Henning Kamp 	if (ip->ip_v != IPVERSION) {
41486425c62SRobert Watson 		IPSTAT_INC(ips_badvers);
415df8bae1dSRodney W. Grimes 		goto bad;
416df8bae1dSRodney W. Grimes 	}
41758938916SGarrett Wollman 
41853be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
419df8bae1dSRodney W. Grimes 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
42086425c62SRobert Watson 		IPSTAT_INC(ips_badhlen);
421df8bae1dSRodney W. Grimes 		goto bad;
422df8bae1dSRodney W. Grimes 	}
423df8bae1dSRodney W. Grimes 	if (hlen > m->m_len) {
4240b17fba7SAndre Oppermann 		if ((m = m_pullup(m, hlen)) == NULL) {
42586425c62SRobert Watson 			IPSTAT_INC(ips_badhlen);
426c67b1d17SGarrett Wollman 			return;
427df8bae1dSRodney W. Grimes 		}
428df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
429df8bae1dSRodney W. Grimes 	}
43033841545SHajimu UMEMOTO 
43133841545SHajimu UMEMOTO 	/* 127/8 must not appear on wire - RFC1122 */
4320aade26eSRobert Watson 	ifp = m->m_pkthdr.rcvif;
43333841545SHajimu UMEMOTO 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
43433841545SHajimu UMEMOTO 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
4350aade26eSRobert Watson 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
43686425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
43733841545SHajimu UMEMOTO 			goto bad;
43833841545SHajimu UMEMOTO 		}
43933841545SHajimu UMEMOTO 	}
44033841545SHajimu UMEMOTO 
441db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
442db4f9cc7SJonathan Lemon 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
443db4f9cc7SJonathan Lemon 	} else {
44458938916SGarrett Wollman 		if (hlen == sizeof(struct ip)) {
44547c861ecSBrian Somers 			sum = in_cksum_hdr(ip);
44658938916SGarrett Wollman 		} else {
44747c861ecSBrian Somers 			sum = in_cksum(m, hlen);
44858938916SGarrett Wollman 		}
449db4f9cc7SJonathan Lemon 	}
45047c861ecSBrian Somers 	if (sum) {
45186425c62SRobert Watson 		IPSTAT_INC(ips_badsum);
452df8bae1dSRodney W. Grimes 		goto bad;
453df8bae1dSRodney W. Grimes 	}
454df8bae1dSRodney W. Grimes 
45502b199f1SMax Laier #ifdef ALTQ
45602b199f1SMax Laier 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
45702b199f1SMax Laier 		/* packet is dropped by traffic conditioner */
45802b199f1SMax Laier 		return;
45902b199f1SMax Laier #endif
46002b199f1SMax Laier 
461df8bae1dSRodney W. Grimes 	/*
462df8bae1dSRodney W. Grimes 	 * Convert fields to host representation.
463df8bae1dSRodney W. Grimes 	 */
464fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
465df8bae1dSRodney W. Grimes 	if (ip->ip_len < hlen) {
46686425c62SRobert Watson 		IPSTAT_INC(ips_badlen);
467df8bae1dSRodney W. Grimes 		goto bad;
468df8bae1dSRodney W. Grimes 	}
469fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
470df8bae1dSRodney W. Grimes 
471df8bae1dSRodney W. Grimes 	/*
472df8bae1dSRodney W. Grimes 	 * Check that the amount of data in the buffers
473df8bae1dSRodney W. Grimes 	 * is as at least much as the IP header would have us expect.
474df8bae1dSRodney W. Grimes 	 * Trim mbufs if longer than we expect.
475df8bae1dSRodney W. Grimes 	 * Drop packet if shorter than we expect.
476df8bae1dSRodney W. Grimes 	 */
477df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len < ip->ip_len) {
47858938916SGarrett Wollman tooshort:
47986425c62SRobert Watson 		IPSTAT_INC(ips_tooshort);
480df8bae1dSRodney W. Grimes 		goto bad;
481df8bae1dSRodney W. Grimes 	}
482df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len > ip->ip_len) {
483df8bae1dSRodney W. Grimes 		if (m->m_len == m->m_pkthdr.len) {
484df8bae1dSRodney W. Grimes 			m->m_len = ip->ip_len;
485df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = ip->ip_len;
486df8bae1dSRodney W. Grimes 		} else
487df8bae1dSRodney W. Grimes 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
488df8bae1dSRodney W. Grimes 	}
489b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
49014dd6717SSam Leffler 	/*
491*ffe8cd7bSBjoern A. Zeeb 	 * Bypass packet filtering for packets previously handled by IPsec.
49214dd6717SSam Leffler 	 */
493cc977adcSBjoern A. Zeeb 	if (ip_ipsec_filtertunnel(m))
494c21fd232SAndre Oppermann 		goto passin;
495b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
4963f67c834SDon Lewis 
497c4ac87eaSDarren Reed 	/*
498134ea224SSam Leffler 	 * Run through list of hooks for input packets.
499f51f805fSSam Leffler 	 *
500f51f805fSSam Leffler 	 * NB: Beware of the destination address changing (e.g.
501f51f805fSSam Leffler 	 *     by NAT rewriting).  When this happens, tell
502f51f805fSSam Leffler 	 *     ip_forward to do the right thing.
503c4ac87eaSDarren Reed 	 */
504c21fd232SAndre Oppermann 
505c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
5060b4b0b0fSJulian Elischer 	if (!PFIL_HOOKED(&V_inet_pfil_hook))
507c21fd232SAndre Oppermann 		goto passin;
508c21fd232SAndre Oppermann 
509f51f805fSSam Leffler 	odst = ip->ip_dst;
5100b4b0b0fSJulian Elischer 	if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0)
511beec8214SDarren Reed 		return;
512134ea224SSam Leffler 	if (m == NULL)			/* consumed by filter */
513c4ac87eaSDarren Reed 		return;
5149b932e9eSAndre Oppermann 
515c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
51602c1c707SAndre Oppermann 	dchg = (odst.s_addr != ip->ip_dst.s_addr);
5170aade26eSRobert Watson 	ifp = m->m_pkthdr.rcvif;
5189b932e9eSAndre Oppermann 
5199b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
5209b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
5219b932e9eSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
5229b932e9eSAndre Oppermann 		goto ours;
5239b932e9eSAndre Oppermann 	}
524099dd043SAndre Oppermann 	if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) {
525099dd043SAndre Oppermann 		/*
5260d3d0d74SRobert Watson 		 * Directly ship the packet on.  This allows forwarding
52799b96cf9SRobert Watson 		 * packets originally destined to us to some other directly
5286426657eSRobert Watson 		 * connected host.
529099dd043SAndre Oppermann 		 */
530099dd043SAndre Oppermann 		ip_forward(m, dchg);
531099dd043SAndre Oppermann 		return;
532099dd043SAndre Oppermann 	}
5339b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
5349b932e9eSAndre Oppermann 
535c21fd232SAndre Oppermann passin:
536df8bae1dSRodney W. Grimes 	/*
537df8bae1dSRodney W. Grimes 	 * Process options and, if not destined for us,
538df8bae1dSRodney W. Grimes 	 * ship it on.  ip_dooptions returns 1 when an
539df8bae1dSRodney W. Grimes 	 * error was detected (causing an icmp message
540df8bae1dSRodney W. Grimes 	 * to be sent and the original packet to be freed).
541df8bae1dSRodney W. Grimes 	 */
5429b932e9eSAndre Oppermann 	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
543c67b1d17SGarrett Wollman 		return;
544df8bae1dSRodney W. Grimes 
545f0068c4aSGarrett Wollman         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
546f0068c4aSGarrett Wollman          * matter if it is destined to another node, or whether it is
547f0068c4aSGarrett Wollman          * a multicast one, RSVP wants it! and prevents it from being forwarded
548f0068c4aSGarrett Wollman          * anywhere else. Also checks if the rsvp daemon is running before
549f0068c4aSGarrett Wollman 	 * grabbing the packet.
550f0068c4aSGarrett Wollman          */
551603724d3SBjoern A. Zeeb 	if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP)
552f0068c4aSGarrett Wollman 		goto ours;
553f0068c4aSGarrett Wollman 
554df8bae1dSRodney W. Grimes 	/*
555df8bae1dSRodney W. Grimes 	 * Check our list of addresses, to see if the packet is for us.
556cc766e04SGarrett Wollman 	 * If we don't have any addresses, assume any unicast packet
557cc766e04SGarrett Wollman 	 * we receive might be for us (and let the upper layers deal
558cc766e04SGarrett Wollman 	 * with it).
559df8bae1dSRodney W. Grimes 	 */
560603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
561cc766e04SGarrett Wollman 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
562cc766e04SGarrett Wollman 		goto ours;
563cc766e04SGarrett Wollman 
5647538a9a0SJonathan Lemon 	/*
565823db0e9SDon Lewis 	 * Enable a consistency check between the destination address
566823db0e9SDon Lewis 	 * and the arrival interface for a unicast packet (the RFC 1122
567823db0e9SDon Lewis 	 * strong ES model) if IP forwarding is disabled and the packet
568e15ae1b2SDon Lewis 	 * is not locally generated and the packet is not subject to
569e15ae1b2SDon Lewis 	 * 'ipfw fwd'.
5703f67c834SDon Lewis 	 *
5713f67c834SDon Lewis 	 * XXX - Checking also should be disabled if the destination
5723f67c834SDon Lewis 	 * address is ipnat'ed to a different interface.
5733f67c834SDon Lewis 	 *
574a8f12100SDon Lewis 	 * XXX - Checking is incompatible with IP aliases added
5753f67c834SDon Lewis 	 * to the loopback interface instead of the interface where
5763f67c834SDon Lewis 	 * the packets are received.
577a9771948SGleb Smirnoff 	 *
578a9771948SGleb Smirnoff 	 * XXX - This is the case for carp vhost IPs as well so we
579a9771948SGleb Smirnoff 	 * insert a workaround. If the packet got here, we already
580a9771948SGleb Smirnoff 	 * checked with carp_iamatch() and carp_forus().
581823db0e9SDon Lewis 	 */
582603724d3SBjoern A. Zeeb 	checkif = V_ip_checkinterface && (V_ipforwarding == 0) &&
5830aade26eSRobert Watson 	    ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) &&
58454bfbd51SWill Andrews 	    ifp->if_carp == NULL && (dchg == 0);
585823db0e9SDon Lewis 
586ca925d9cSJonathan Lemon 	/*
587ca925d9cSJonathan Lemon 	 * Check for exact addresses in the hash bucket.
588ca925d9cSJonathan Lemon 	 */
5892d9cfabaSRobert Watson 	/* IN_IFADDR_RLOCK(); */
5909b932e9eSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
591f9e354dfSJulian Elischer 		/*
592823db0e9SDon Lewis 		 * If the address matches, verify that the packet
593823db0e9SDon Lewis 		 * arrived via the correct interface if checking is
594823db0e9SDon Lewis 		 * enabled.
595f9e354dfSJulian Elischer 		 */
5969b932e9eSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
5978c0fec80SRobert Watson 		    (!checkif || ia->ia_ifp == ifp)) {
5988c0fec80SRobert Watson 			ifa_ref(&ia->ia_ifa);
5992d9cfabaSRobert Watson 			/* IN_IFADDR_RUNLOCK(); */
600ed1ff184SJulian Elischer 			goto ours;
601ca925d9cSJonathan Lemon 		}
6028c0fec80SRobert Watson 	}
6032d9cfabaSRobert Watson 	/* IN_IFADDR_RUNLOCK(); */
6042d9cfabaSRobert Watson 
605823db0e9SDon Lewis 	/*
606ca925d9cSJonathan Lemon 	 * Check for broadcast addresses.
607ca925d9cSJonathan Lemon 	 *
608ca925d9cSJonathan Lemon 	 * Only accept broadcast packets that arrive via the matching
609ca925d9cSJonathan Lemon 	 * interface.  Reception of forwarded directed broadcasts would
610ca925d9cSJonathan Lemon 	 * be handled via ip_forward() and ether_output() with the loopback
611ca925d9cSJonathan Lemon 	 * into the stack for SIMPLEX interfaces handled by ether_output().
612823db0e9SDon Lewis 	 */
6130aade26eSRobert Watson 	if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
6140aade26eSRobert Watson 		IF_ADDR_LOCK(ifp);
6150aade26eSRobert Watson 	        TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
616ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
617ca925d9cSJonathan Lemon 				continue;
618ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
619df8bae1dSRodney W. Grimes 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
6200aade26eSRobert Watson 			    ip->ip_dst.s_addr) {
6218c0fec80SRobert Watson 				ifa_ref(ifa);
6220aade26eSRobert Watson 				IF_ADDR_UNLOCK(ifp);
623df8bae1dSRodney W. Grimes 				goto ours;
6240aade26eSRobert Watson 			}
6250aade26eSRobert Watson 			if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr) {
6268c0fec80SRobert Watson 				ifa_ref(ifa);
6270aade26eSRobert Watson 				IF_ADDR_UNLOCK(ifp);
628df8bae1dSRodney W. Grimes 				goto ours;
6290aade26eSRobert Watson 			}
6300ac40133SBrian Somers #ifdef BOOTP_COMPAT
6310aade26eSRobert Watson 			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) {
6328c0fec80SRobert Watson 				ifa_ref(ifa);
6330aade26eSRobert Watson 				IF_ADDR_UNLOCK(ifp);
634ca925d9cSJonathan Lemon 				goto ours;
6350aade26eSRobert Watson 			}
6360ac40133SBrian Somers #endif
637df8bae1dSRodney W. Grimes 		}
6380aade26eSRobert Watson 		IF_ADDR_UNLOCK(ifp);
63919e5b0a7SRobert Watson 		ia = NULL;
640df8bae1dSRodney W. Grimes 	}
641f8429ca2SBruce M Simpson 	/* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
642f8429ca2SBruce M Simpson 	if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
64386425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
644f8429ca2SBruce M Simpson 		m_freem(m);
645f8429ca2SBruce M Simpson 		return;
646f8429ca2SBruce M Simpson 	}
647df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
648603724d3SBjoern A. Zeeb 		if (V_ip_mrouter) {
649df8bae1dSRodney W. Grimes 			/*
650df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, all
651df8bae1dSRodney W. Grimes 			 * incoming multicast packets are passed to the
652df8bae1dSRodney W. Grimes 			 * kernel-level multicast forwarding function.
653df8bae1dSRodney W. Grimes 			 * The packet is returned (relatively) intact; if
654df8bae1dSRodney W. Grimes 			 * ip_mforward() returns a non-zero value, the packet
655df8bae1dSRodney W. Grimes 			 * must be discarded, else it may be accepted below.
656df8bae1dSRodney W. Grimes 			 */
6570aade26eSRobert Watson 			if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
65886425c62SRobert Watson 				IPSTAT_INC(ips_cantforward);
659df8bae1dSRodney W. Grimes 				m_freem(m);
660c67b1d17SGarrett Wollman 				return;
661df8bae1dSRodney W. Grimes 			}
662df8bae1dSRodney W. Grimes 
663df8bae1dSRodney W. Grimes 			/*
66411612afaSDima Dorfman 			 * The process-level routing daemon needs to receive
665df8bae1dSRodney W. Grimes 			 * all multicast IGMP packets, whether or not this
666df8bae1dSRodney W. Grimes 			 * host belongs to their destination groups.
667df8bae1dSRodney W. Grimes 			 */
668df8bae1dSRodney W. Grimes 			if (ip->ip_p == IPPROTO_IGMP)
669df8bae1dSRodney W. Grimes 				goto ours;
67086425c62SRobert Watson 			IPSTAT_INC(ips_forward);
671df8bae1dSRodney W. Grimes 		}
672df8bae1dSRodney W. Grimes 		/*
673d10910e6SBruce M Simpson 		 * Assume the packet is for us, to avoid prematurely taking
674d10910e6SBruce M Simpson 		 * a lock on the in_multi hash. Protocols must perform
675d10910e6SBruce M Simpson 		 * their own filtering and update statistics accordingly.
676df8bae1dSRodney W. Grimes 		 */
677df8bae1dSRodney W. Grimes 		goto ours;
678df8bae1dSRodney W. Grimes 	}
679df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
680df8bae1dSRodney W. Grimes 		goto ours;
681df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == INADDR_ANY)
682df8bae1dSRodney W. Grimes 		goto ours;
683df8bae1dSRodney W. Grimes 
6846a800098SYoshinobu Inoue 	/*
6856a800098SYoshinobu Inoue 	 * FAITH(Firewall Aided Internet Translator)
6866a800098SYoshinobu Inoue 	 */
6870aade26eSRobert Watson 	if (ifp && ifp->if_type == IFT_FAITH) {
688603724d3SBjoern A. Zeeb 		if (V_ip_keepfaith) {
6896a800098SYoshinobu Inoue 			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
6906a800098SYoshinobu Inoue 				goto ours;
6916a800098SYoshinobu Inoue 		}
6926a800098SYoshinobu Inoue 		m_freem(m);
6936a800098SYoshinobu Inoue 		return;
6946a800098SYoshinobu Inoue 	}
6959494d596SBrooks Davis 
696df8bae1dSRodney W. Grimes 	/*
697df8bae1dSRodney W. Grimes 	 * Not for us; forward if possible and desirable.
698df8bae1dSRodney W. Grimes 	 */
699603724d3SBjoern A. Zeeb 	if (V_ipforwarding == 0) {
70086425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
701df8bae1dSRodney W. Grimes 		m_freem(m);
702546f251bSChris D. Faulhaber 	} else {
703b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
7041dfcf0d2SAndre Oppermann 		if (ip_ipsec_fwd(m))
705546f251bSChris D. Faulhaber 			goto bad;
706b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
7079b932e9eSAndre Oppermann 		ip_forward(m, dchg);
708546f251bSChris D. Faulhaber 	}
709c67b1d17SGarrett Wollman 	return;
710df8bae1dSRodney W. Grimes 
711df8bae1dSRodney W. Grimes ours:
712d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH
713d0ebc0d2SYaroslav Tykhiy 	/*
714d0ebc0d2SYaroslav Tykhiy 	 * IPSTEALTH: Process non-routing options only
715d0ebc0d2SYaroslav Tykhiy 	 * if the packet is destined for us.
716d0ebc0d2SYaroslav Tykhiy 	 */
71719e5b0a7SRobert Watson 	if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1)) {
71819e5b0a7SRobert Watson 		if (ia != NULL)
71919e5b0a7SRobert Watson 			ifa_free(&ia->ia_ifa);
720d0ebc0d2SYaroslav Tykhiy 		return;
72119e5b0a7SRobert Watson 	}
722d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */
723d0ebc0d2SYaroslav Tykhiy 
7245da9f8faSJosef Karthauser 	/* Count the packet in the ip address stats */
7255da9f8faSJosef Karthauser 	if (ia != NULL) {
7265da9f8faSJosef Karthauser 		ia->ia_ifa.if_ipackets++;
7275da9f8faSJosef Karthauser 		ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
7288c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
7295da9f8faSJosef Karthauser 	}
730100ba1a6SJordan K. Hubbard 
73163f8d699SJordan K. Hubbard 	/*
732b6ea1aa5SRuslan Ermilov 	 * Attempt reassembly; if it succeeds, proceed.
733ac9d7e26SMax Laier 	 * ip_reass() will return a different mbuf.
734df8bae1dSRodney W. Grimes 	 */
735f0cada84SAndre Oppermann 	if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
736f0cada84SAndre Oppermann 		m = ip_reass(m);
737f0cada84SAndre Oppermann 		if (m == NULL)
738c67b1d17SGarrett Wollman 			return;
7396a800098SYoshinobu Inoue 		ip = mtod(m, struct ip *);
7407e2df452SRuslan Ermilov 		/* Get the header length of the reassembled packet */
74153be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
742f0cada84SAndre Oppermann 	}
743f0cada84SAndre Oppermann 
744f0cada84SAndre Oppermann 	/*
745f0cada84SAndre Oppermann 	 * Further protocols expect the packet length to be w/o the
746f0cada84SAndre Oppermann 	 * IP header.
747f0cada84SAndre Oppermann 	 */
748df8bae1dSRodney W. Grimes 	ip->ip_len -= hlen;
749df8bae1dSRodney W. Grimes 
750b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
75133841545SHajimu UMEMOTO 	/*
75233841545SHajimu UMEMOTO 	 * enforce IPsec policy checking if we are seeing last header.
75333841545SHajimu UMEMOTO 	 * note that we do not visit this with protocols with pcb layer
75433841545SHajimu UMEMOTO 	 * code - like udp/tcp/raw ip.
75533841545SHajimu UMEMOTO 	 */
7561dfcf0d2SAndre Oppermann 	if (ip_ipsec_input(m))
75733841545SHajimu UMEMOTO 		goto bad;
758b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
75933841545SHajimu UMEMOTO 
760df8bae1dSRodney W. Grimes 	/*
761df8bae1dSRodney W. Grimes 	 * Switch out to protocol's input routine.
762df8bae1dSRodney W. Grimes 	 */
76386425c62SRobert Watson 	IPSTAT_INC(ips_delivered);
7649b932e9eSAndre Oppermann 
7652b25acc1SLuigi Rizzo 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
766c67b1d17SGarrett Wollman 	return;
767df8bae1dSRodney W. Grimes bad:
768df8bae1dSRodney W. Grimes 	m_freem(m);
769c67b1d17SGarrett Wollman }
770c67b1d17SGarrett Wollman 
771c67b1d17SGarrett Wollman /*
772d248c7d7SRobert Watson  * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
773d248c7d7SRobert Watson  * max has slightly different semantics than the sysctl, for historical
774d248c7d7SRobert Watson  * reasons.
775d248c7d7SRobert Watson  */
776d248c7d7SRobert Watson static void
777d248c7d7SRobert Watson maxnipq_update(void)
778d248c7d7SRobert Watson {
779d248c7d7SRobert Watson 
780d248c7d7SRobert Watson 	/*
781d248c7d7SRobert Watson 	 * -1 for unlimited allocation.
782d248c7d7SRobert Watson 	 */
783603724d3SBjoern A. Zeeb 	if (V_maxnipq < 0)
784603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 0);
785d248c7d7SRobert Watson 	/*
786d248c7d7SRobert Watson 	 * Positive number for specific bound.
787d248c7d7SRobert Watson 	 */
788603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0)
789603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, V_maxnipq);
790d248c7d7SRobert Watson 	/*
791d248c7d7SRobert Watson 	 * Zero specifies no further fragment queue allocation -- set the
792d248c7d7SRobert Watson 	 * bound very low, but rely on implementation elsewhere to actually
793d248c7d7SRobert Watson 	 * prevent allocation and reclaim current queues.
794d248c7d7SRobert Watson 	 */
795603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0)
796603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 1);
797d248c7d7SRobert Watson }
798d248c7d7SRobert Watson 
7994f590175SPaul Saab static void
8004f590175SPaul Saab ipq_zone_change(void *tag)
8014f590175SPaul Saab {
8024f590175SPaul Saab 
803603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) {
804603724d3SBjoern A. Zeeb 		V_maxnipq = nmbclusters / 32;
8054f590175SPaul Saab 		maxnipq_update();
8064f590175SPaul Saab 	}
8074f590175SPaul Saab }
8084f590175SPaul Saab 
809d248c7d7SRobert Watson static int
810d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
811d248c7d7SRobert Watson {
812d248c7d7SRobert Watson 	int error, i;
813d248c7d7SRobert Watson 
814603724d3SBjoern A. Zeeb 	i = V_maxnipq;
815d248c7d7SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
816d248c7d7SRobert Watson 	if (error || !req->newptr)
817d248c7d7SRobert Watson 		return (error);
818d248c7d7SRobert Watson 
819d248c7d7SRobert Watson 	/*
820d248c7d7SRobert Watson 	 * XXXRW: Might be a good idea to sanity check the argument and place
821d248c7d7SRobert Watson 	 * an extreme upper bound.
822d248c7d7SRobert Watson 	 */
823d248c7d7SRobert Watson 	if (i < -1)
824d248c7d7SRobert Watson 		return (EINVAL);
825603724d3SBjoern A. Zeeb 	V_maxnipq = i;
826d248c7d7SRobert Watson 	maxnipq_update();
827d248c7d7SRobert Watson 	return (0);
828d248c7d7SRobert Watson }
829d248c7d7SRobert Watson 
830d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
831d248c7d7SRobert Watson     NULL, 0, sysctl_maxnipq, "I",
832d248c7d7SRobert Watson     "Maximum number of IPv4 fragment reassembly queue entries");
833d248c7d7SRobert Watson 
834d248c7d7SRobert Watson /*
8358948e4baSArchie Cobbs  * Take incoming datagram fragment and try to reassemble it into
836f0cada84SAndre Oppermann  * whole datagram.  If the argument is the first fragment or one
837f0cada84SAndre Oppermann  * in between the function will return NULL and store the mbuf
838f0cada84SAndre Oppermann  * in the fragment chain.  If the argument is the last fragment
839f0cada84SAndre Oppermann  * the packet will be reassembled and the pointer to the new
840f0cada84SAndre Oppermann  * mbuf returned for further processing.  Only m_tags attached
841f0cada84SAndre Oppermann  * to the first packet/fragment are preserved.
842f0cada84SAndre Oppermann  * The IP header is *NOT* adjusted out of iplen.
843df8bae1dSRodney W. Grimes  */
844f0cada84SAndre Oppermann struct mbuf *
845f0cada84SAndre Oppermann ip_reass(struct mbuf *m)
846df8bae1dSRodney W. Grimes {
847f0cada84SAndre Oppermann 	struct ip *ip;
848f0cada84SAndre Oppermann 	struct mbuf *p, *q, *nq, *t;
849f0cada84SAndre Oppermann 	struct ipq *fp = NULL;
850f0cada84SAndre Oppermann 	struct ipqhead *head;
851f0cada84SAndre Oppermann 	int i, hlen, next;
85259dfcba4SHajimu UMEMOTO 	u_int8_t ecn, ecn0;
853f0cada84SAndre Oppermann 	u_short hash;
854df8bae1dSRodney W. Grimes 
855800af1fbSMaxim Konovalov 	/* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
856603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0 || V_maxfragsperpacket == 0) {
85786425c62SRobert Watson 		IPSTAT_INC(ips_fragments);
85886425c62SRobert Watson 		IPSTAT_INC(ips_fragdropped);
8599d804f81SAndre Oppermann 		m_freem(m);
8609d804f81SAndre Oppermann 		return (NULL);
861f0cada84SAndre Oppermann 	}
8622fad1e93SSam Leffler 
863f0cada84SAndre Oppermann 	ip = mtod(m, struct ip *);
864f0cada84SAndre Oppermann 	hlen = ip->ip_hl << 2;
865f0cada84SAndre Oppermann 
866f0cada84SAndre Oppermann 	hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
867603724d3SBjoern A. Zeeb 	head = &V_ipq[hash];
868f0cada84SAndre Oppermann 	IPQ_LOCK();
869f0cada84SAndre Oppermann 
870f0cada84SAndre Oppermann 	/*
871f0cada84SAndre Oppermann 	 * Look for queue of fragments
872f0cada84SAndre Oppermann 	 * of this datagram.
873f0cada84SAndre Oppermann 	 */
874f0cada84SAndre Oppermann 	TAILQ_FOREACH(fp, head, ipq_list)
875f0cada84SAndre Oppermann 		if (ip->ip_id == fp->ipq_id &&
876f0cada84SAndre Oppermann 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
877f0cada84SAndre Oppermann 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
878f0cada84SAndre Oppermann #ifdef MAC
87930d239bcSRobert Watson 		    mac_ipq_match(m, fp) &&
880f0cada84SAndre Oppermann #endif
881f0cada84SAndre Oppermann 		    ip->ip_p == fp->ipq_p)
882f0cada84SAndre Oppermann 			goto found;
883f0cada84SAndre Oppermann 
884f0cada84SAndre Oppermann 	fp = NULL;
885f0cada84SAndre Oppermann 
886f0cada84SAndre Oppermann 	/*
887d248c7d7SRobert Watson 	 * Attempt to trim the number of allocated fragment queues if it
888d248c7d7SRobert Watson 	 * exceeds the administrative limit.
889f0cada84SAndre Oppermann 	 */
890603724d3SBjoern A. Zeeb 	if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) {
891f0cada84SAndre Oppermann 		/*
892f0cada84SAndre Oppermann 		 * drop something from the tail of the current queue
893f0cada84SAndre Oppermann 		 * before proceeding further
894f0cada84SAndre Oppermann 		 */
895f0cada84SAndre Oppermann 		struct ipq *q = TAILQ_LAST(head, ipqhead);
896f0cada84SAndre Oppermann 		if (q == NULL) {   /* gak */
897f0cada84SAndre Oppermann 			for (i = 0; i < IPREASS_NHASH; i++) {
898603724d3SBjoern A. Zeeb 				struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead);
899f0cada84SAndre Oppermann 				if (r) {
90086425c62SRobert Watson 					IPSTAT_ADD(ips_fragtimeout,
90186425c62SRobert Watson 					    r->ipq_nfrags);
902603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], r);
903f0cada84SAndre Oppermann 					break;
904f0cada84SAndre Oppermann 				}
905f0cada84SAndre Oppermann 			}
906f0cada84SAndre Oppermann 		} else {
90786425c62SRobert Watson 			IPSTAT_ADD(ips_fragtimeout, q->ipq_nfrags);
908f0cada84SAndre Oppermann 			ip_freef(head, q);
909f0cada84SAndre Oppermann 		}
910f0cada84SAndre Oppermann 	}
911f0cada84SAndre Oppermann 
912f0cada84SAndre Oppermann found:
913f0cada84SAndre Oppermann 	/*
914f0cada84SAndre Oppermann 	 * Adjust ip_len to not reflect header,
915f0cada84SAndre Oppermann 	 * convert offset of this to bytes.
916f0cada84SAndre Oppermann 	 */
917f0cada84SAndre Oppermann 	ip->ip_len -= hlen;
918f0cada84SAndre Oppermann 	if (ip->ip_off & IP_MF) {
919f0cada84SAndre Oppermann 		/*
920f0cada84SAndre Oppermann 		 * Make sure that fragments have a data length
921f0cada84SAndre Oppermann 		 * that's a non-zero multiple of 8 bytes.
922f0cada84SAndre Oppermann 		 */
923f0cada84SAndre Oppermann 		if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
92486425c62SRobert Watson 			IPSTAT_INC(ips_toosmall); /* XXX */
925f0cada84SAndre Oppermann 			goto dropfrag;
926f0cada84SAndre Oppermann 		}
927f0cada84SAndre Oppermann 		m->m_flags |= M_FRAG;
928f0cada84SAndre Oppermann 	} else
929f0cada84SAndre Oppermann 		m->m_flags &= ~M_FRAG;
930f0cada84SAndre Oppermann 	ip->ip_off <<= 3;
931f0cada84SAndre Oppermann 
932f0cada84SAndre Oppermann 
933f0cada84SAndre Oppermann 	/*
934f0cada84SAndre Oppermann 	 * Attempt reassembly; if it succeeds, proceed.
935f0cada84SAndre Oppermann 	 * ip_reass() will return a different mbuf.
936f0cada84SAndre Oppermann 	 */
93786425c62SRobert Watson 	IPSTAT_INC(ips_fragments);
938f0cada84SAndre Oppermann 	m->m_pkthdr.header = ip;
939f0cada84SAndre Oppermann 
940f0cada84SAndre Oppermann 	/* Previous ip_reass() started here. */
941df8bae1dSRodney W. Grimes 	/*
942df8bae1dSRodney W. Grimes 	 * Presence of header sizes in mbufs
943df8bae1dSRodney W. Grimes 	 * would confuse code below.
944df8bae1dSRodney W. Grimes 	 */
945df8bae1dSRodney W. Grimes 	m->m_data += hlen;
946df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
947df8bae1dSRodney W. Grimes 
948df8bae1dSRodney W. Grimes 	/*
949df8bae1dSRodney W. Grimes 	 * If first fragment to arrive, create a reassembly queue.
950df8bae1dSRodney W. Grimes 	 */
951042bbfa3SRobert Watson 	if (fp == NULL) {
952603724d3SBjoern A. Zeeb 		fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
953d248c7d7SRobert Watson 		if (fp == NULL)
954df8bae1dSRodney W. Grimes 			goto dropfrag;
95536b0360bSRobert Watson #ifdef MAC
95630d239bcSRobert Watson 		if (mac_ipq_init(fp, M_NOWAIT) != 0) {
957603724d3SBjoern A. Zeeb 			uma_zfree(V_ipq_zone, fp);
9581d7d0bfeSPawel Jakub Dawidek 			fp = NULL;
9595e7ce478SRobert Watson 			goto dropfrag;
9605e7ce478SRobert Watson 		}
96130d239bcSRobert Watson 		mac_ipq_create(m, fp);
96236b0360bSRobert Watson #endif
963462b86feSPoul-Henning Kamp 		TAILQ_INSERT_HEAD(head, fp, ipq_list);
964603724d3SBjoern A. Zeeb 		V_nipq++;
965375386e2SMike Silbersack 		fp->ipq_nfrags = 1;
966df8bae1dSRodney W. Grimes 		fp->ipq_ttl = IPFRAGTTL;
967df8bae1dSRodney W. Grimes 		fp->ipq_p = ip->ip_p;
968df8bae1dSRodney W. Grimes 		fp->ipq_id = ip->ip_id;
9696effc713SDoug Rabson 		fp->ipq_src = ip->ip_src;
9706effc713SDoug Rabson 		fp->ipq_dst = ip->ip_dst;
971af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
972af38c68cSLuigi Rizzo 		m->m_nextpkt = NULL;
973800af1fbSMaxim Konovalov 		goto done;
97436b0360bSRobert Watson 	} else {
975375386e2SMike Silbersack 		fp->ipq_nfrags++;
97636b0360bSRobert Watson #ifdef MAC
97730d239bcSRobert Watson 		mac_ipq_update(m, fp);
97836b0360bSRobert Watson #endif
979df8bae1dSRodney W. Grimes 	}
980df8bae1dSRodney W. Grimes 
9816effc713SDoug Rabson #define GETIP(m)	((struct ip*)((m)->m_pkthdr.header))
9826effc713SDoug Rabson 
983df8bae1dSRodney W. Grimes 	/*
98459dfcba4SHajimu UMEMOTO 	 * Handle ECN by comparing this segment with the first one;
98559dfcba4SHajimu UMEMOTO 	 * if CE is set, do not lose CE.
98659dfcba4SHajimu UMEMOTO 	 * drop if CE and not-ECT are mixed for the same packet.
98759dfcba4SHajimu UMEMOTO 	 */
98859dfcba4SHajimu UMEMOTO 	ecn = ip->ip_tos & IPTOS_ECN_MASK;
98959dfcba4SHajimu UMEMOTO 	ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
99059dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_CE) {
99159dfcba4SHajimu UMEMOTO 		if (ecn0 == IPTOS_ECN_NOTECT)
99259dfcba4SHajimu UMEMOTO 			goto dropfrag;
99359dfcba4SHajimu UMEMOTO 		if (ecn0 != IPTOS_ECN_CE)
99459dfcba4SHajimu UMEMOTO 			GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
99559dfcba4SHajimu UMEMOTO 	}
99659dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
99759dfcba4SHajimu UMEMOTO 		goto dropfrag;
99859dfcba4SHajimu UMEMOTO 
99959dfcba4SHajimu UMEMOTO 	/*
1000df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
1001df8bae1dSRodney W. Grimes 	 */
10026effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
10036effc713SDoug Rabson 		if (GETIP(q)->ip_off > ip->ip_off)
1004df8bae1dSRodney W. Grimes 			break;
1005df8bae1dSRodney W. Grimes 
1006df8bae1dSRodney W. Grimes 	/*
1007df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
1008df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
1009af38c68cSLuigi Rizzo 	 * segment.  If it provides all of our data, drop us, otherwise
1010af38c68cSLuigi Rizzo 	 * stick new segment in the proper place.
1011db4f9cc7SJonathan Lemon 	 *
10126bccea7cSRebecca Cran 	 * If some of the data is dropped from the preceding
1013db4f9cc7SJonathan Lemon 	 * segment, then it's checksum is invalidated.
1014df8bae1dSRodney W. Grimes 	 */
10156effc713SDoug Rabson 	if (p) {
10166effc713SDoug Rabson 		i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
1017df8bae1dSRodney W. Grimes 		if (i > 0) {
1018df8bae1dSRodney W. Grimes 			if (i >= ip->ip_len)
1019df8bae1dSRodney W. Grimes 				goto dropfrag;
10206a800098SYoshinobu Inoue 			m_adj(m, i);
1021db4f9cc7SJonathan Lemon 			m->m_pkthdr.csum_flags = 0;
1022df8bae1dSRodney W. Grimes 			ip->ip_off += i;
1023df8bae1dSRodney W. Grimes 			ip->ip_len -= i;
1024df8bae1dSRodney W. Grimes 		}
1025af38c68cSLuigi Rizzo 		m->m_nextpkt = p->m_nextpkt;
1026af38c68cSLuigi Rizzo 		p->m_nextpkt = m;
1027af38c68cSLuigi Rizzo 	} else {
1028af38c68cSLuigi Rizzo 		m->m_nextpkt = fp->ipq_frags;
1029af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
1030df8bae1dSRodney W. Grimes 	}
1031df8bae1dSRodney W. Grimes 
1032df8bae1dSRodney W. Grimes 	/*
1033df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
1034df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
1035df8bae1dSRodney W. Grimes 	 */
10366effc713SDoug Rabson 	for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
1037af38c68cSLuigi Rizzo 	     q = nq) {
1038b36f5b37SMaxim Konovalov 		i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
10396effc713SDoug Rabson 		if (i < GETIP(q)->ip_len) {
10406effc713SDoug Rabson 			GETIP(q)->ip_len -= i;
10416effc713SDoug Rabson 			GETIP(q)->ip_off += i;
10426effc713SDoug Rabson 			m_adj(q, i);
1043db4f9cc7SJonathan Lemon 			q->m_pkthdr.csum_flags = 0;
1044df8bae1dSRodney W. Grimes 			break;
1045df8bae1dSRodney W. Grimes 		}
10466effc713SDoug Rabson 		nq = q->m_nextpkt;
1047af38c68cSLuigi Rizzo 		m->m_nextpkt = nq;
104886425c62SRobert Watson 		IPSTAT_INC(ips_fragdropped);
1049375386e2SMike Silbersack 		fp->ipq_nfrags--;
10506effc713SDoug Rabson 		m_freem(q);
1051df8bae1dSRodney W. Grimes 	}
1052df8bae1dSRodney W. Grimes 
1053df8bae1dSRodney W. Grimes 	/*
1054375386e2SMike Silbersack 	 * Check for complete reassembly and perform frag per packet
1055375386e2SMike Silbersack 	 * limiting.
1056375386e2SMike Silbersack 	 *
1057375386e2SMike Silbersack 	 * Frag limiting is performed here so that the nth frag has
1058375386e2SMike Silbersack 	 * a chance to complete the packet before we drop the packet.
1059375386e2SMike Silbersack 	 * As a result, n+1 frags are actually allowed per packet, but
1060375386e2SMike Silbersack 	 * only n will ever be stored. (n = maxfragsperpacket.)
1061375386e2SMike Silbersack 	 *
1062df8bae1dSRodney W. Grimes 	 */
10636effc713SDoug Rabson 	next = 0;
10646effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
1065375386e2SMike Silbersack 		if (GETIP(q)->ip_off != next) {
1066603724d3SBjoern A. Zeeb 			if (fp->ipq_nfrags > V_maxfragsperpacket) {
106786425c62SRobert Watson 				IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1068375386e2SMike Silbersack 				ip_freef(head, fp);
106999e8617dSMaxim Konovalov 			}
1070f0cada84SAndre Oppermann 			goto done;
1071375386e2SMike Silbersack 		}
10726effc713SDoug Rabson 		next += GETIP(q)->ip_len;
10736effc713SDoug Rabson 	}
10746effc713SDoug Rabson 	/* Make sure the last packet didn't have the IP_MF flag */
1075375386e2SMike Silbersack 	if (p->m_flags & M_FRAG) {
1076603724d3SBjoern A. Zeeb 		if (fp->ipq_nfrags > V_maxfragsperpacket) {
107786425c62SRobert Watson 			IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1078375386e2SMike Silbersack 			ip_freef(head, fp);
107999e8617dSMaxim Konovalov 		}
1080f0cada84SAndre Oppermann 		goto done;
1081375386e2SMike Silbersack 	}
1082df8bae1dSRodney W. Grimes 
1083df8bae1dSRodney W. Grimes 	/*
1084430d30d8SBill Fenner 	 * Reassembly is complete.  Make sure the packet is a sane size.
1085430d30d8SBill Fenner 	 */
10866effc713SDoug Rabson 	q = fp->ipq_frags;
10876effc713SDoug Rabson 	ip = GETIP(q);
108853be11f6SPoul-Henning Kamp 	if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
108986425c62SRobert Watson 		IPSTAT_INC(ips_toolong);
109086425c62SRobert Watson 		IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1091462b86feSPoul-Henning Kamp 		ip_freef(head, fp);
1092f0cada84SAndre Oppermann 		goto done;
1093430d30d8SBill Fenner 	}
1094430d30d8SBill Fenner 
1095430d30d8SBill Fenner 	/*
1096430d30d8SBill Fenner 	 * Concatenate fragments.
1097df8bae1dSRodney W. Grimes 	 */
10986effc713SDoug Rabson 	m = q;
1099df8bae1dSRodney W. Grimes 	t = m->m_next;
110002410549SRobert Watson 	m->m_next = NULL;
1101df8bae1dSRodney W. Grimes 	m_cat(m, t);
11026effc713SDoug Rabson 	nq = q->m_nextpkt;
110302410549SRobert Watson 	q->m_nextpkt = NULL;
11046effc713SDoug Rabson 	for (q = nq; q != NULL; q = nq) {
11056effc713SDoug Rabson 		nq = q->m_nextpkt;
1106945aa40dSDoug Rabson 		q->m_nextpkt = NULL;
1107db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1108db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1109a8db1d93SJonathan Lemon 		m_cat(m, q);
1110df8bae1dSRodney W. Grimes 	}
11116edb555dSOleg Bulyzhin 	/*
11126edb555dSOleg Bulyzhin 	 * In order to do checksumming faster we do 'end-around carry' here
11136edb555dSOleg Bulyzhin 	 * (and not in for{} loop), though it implies we are not going to
11146edb555dSOleg Bulyzhin 	 * reassemble more than 64k fragments.
11156edb555dSOleg Bulyzhin 	 */
11166edb555dSOleg Bulyzhin 	m->m_pkthdr.csum_data =
11176edb555dSOleg Bulyzhin 	    (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16);
111836b0360bSRobert Watson #ifdef MAC
111930d239bcSRobert Watson 	mac_ipq_reassemble(fp, m);
112030d239bcSRobert Watson 	mac_ipq_destroy(fp);
112136b0360bSRobert Watson #endif
1122df8bae1dSRodney W. Grimes 
1123df8bae1dSRodney W. Grimes 	/*
1124f0cada84SAndre Oppermann 	 * Create header for new ip packet by modifying header of first
1125f0cada84SAndre Oppermann 	 * packet;  dequeue and discard fragment reassembly header.
1126df8bae1dSRodney W. Grimes 	 * Make header visible.
1127df8bae1dSRodney W. Grimes 	 */
1128f0cada84SAndre Oppermann 	ip->ip_len = (ip->ip_hl << 2) + next;
11296effc713SDoug Rabson 	ip->ip_src = fp->ipq_src;
11306effc713SDoug Rabson 	ip->ip_dst = fp->ipq_dst;
1131462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(head, fp, ipq_list);
1132603724d3SBjoern A. Zeeb 	V_nipq--;
1133603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
113453be11f6SPoul-Henning Kamp 	m->m_len += (ip->ip_hl << 2);
113553be11f6SPoul-Henning Kamp 	m->m_data -= (ip->ip_hl << 2);
1136df8bae1dSRodney W. Grimes 	/* some debugging cruft by sklower, below, will go away soon */
1137a5554bf0SPoul-Henning Kamp 	if (m->m_flags & M_PKTHDR)	/* XXX this should be done elsewhere */
1138a5554bf0SPoul-Henning Kamp 		m_fixhdr(m);
113986425c62SRobert Watson 	IPSTAT_INC(ips_reassembled);
1140f0cada84SAndre Oppermann 	IPQ_UNLOCK();
11416a800098SYoshinobu Inoue 	return (m);
1142df8bae1dSRodney W. Grimes 
1143df8bae1dSRodney W. Grimes dropfrag:
114486425c62SRobert Watson 	IPSTAT_INC(ips_fragdropped);
1145042bbfa3SRobert Watson 	if (fp != NULL)
1146375386e2SMike Silbersack 		fp->ipq_nfrags--;
1147df8bae1dSRodney W. Grimes 	m_freem(m);
1148f0cada84SAndre Oppermann done:
1149f0cada84SAndre Oppermann 	IPQ_UNLOCK();
1150f0cada84SAndre Oppermann 	return (NULL);
11516effc713SDoug Rabson 
11526effc713SDoug Rabson #undef GETIP
1153df8bae1dSRodney W. Grimes }
1154df8bae1dSRodney W. Grimes 
1155df8bae1dSRodney W. Grimes /*
1156df8bae1dSRodney W. Grimes  * Free a fragment reassembly header and all
1157df8bae1dSRodney W. Grimes  * associated datagrams.
1158df8bae1dSRodney W. Grimes  */
11590312fbe9SPoul-Henning Kamp static void
1160f2565d68SRobert Watson ip_freef(struct ipqhead *fhp, struct ipq *fp)
1161df8bae1dSRodney W. Grimes {
1162f2565d68SRobert Watson 	struct mbuf *q;
1163df8bae1dSRodney W. Grimes 
11642fad1e93SSam Leffler 	IPQ_LOCK_ASSERT();
11652fad1e93SSam Leffler 
11666effc713SDoug Rabson 	while (fp->ipq_frags) {
11676effc713SDoug Rabson 		q = fp->ipq_frags;
11686effc713SDoug Rabson 		fp->ipq_frags = q->m_nextpkt;
11696effc713SDoug Rabson 		m_freem(q);
1170df8bae1dSRodney W. Grimes 	}
1171462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(fhp, fp, ipq_list);
1172603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
1173603724d3SBjoern A. Zeeb 	V_nipq--;
1174df8bae1dSRodney W. Grimes }
1175df8bae1dSRodney W. Grimes 
1176df8bae1dSRodney W. Grimes /*
1177df8bae1dSRodney W. Grimes  * IP timer processing;
1178df8bae1dSRodney W. Grimes  * if a timer expires on a reassembly
1179df8bae1dSRodney W. Grimes  * queue, discard it.
1180df8bae1dSRodney W. Grimes  */
1181df8bae1dSRodney W. Grimes void
1182f2565d68SRobert Watson ip_slowtimo(void)
1183df8bae1dSRodney W. Grimes {
11848b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1185f2565d68SRobert Watson 	struct ipq *fp;
1186194a213eSAndrey A. Chernov 	int i;
1187df8bae1dSRodney W. Grimes 
11885ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
11892fad1e93SSam Leffler 	IPQ_LOCK();
11908b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
11918b615593SMarko Zec 		CURVNET_SET(vnet_iter);
1192194a213eSAndrey A. Chernov 		for (i = 0; i < IPREASS_NHASH; i++) {
1193603724d3SBjoern A. Zeeb 			for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) {
1194462b86feSPoul-Henning Kamp 				struct ipq *fpp;
1195462b86feSPoul-Henning Kamp 
1196462b86feSPoul-Henning Kamp 				fpp = fp;
1197462b86feSPoul-Henning Kamp 				fp = TAILQ_NEXT(fp, ipq_list);
1198462b86feSPoul-Henning Kamp 				if(--fpp->ipq_ttl == 0) {
119986425c62SRobert Watson 					IPSTAT_ADD(ips_fragtimeout,
120086425c62SRobert Watson 					    fpp->ipq_nfrags);
1201603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], fpp);
1202df8bae1dSRodney W. Grimes 				}
1203df8bae1dSRodney W. Grimes 			}
1204194a213eSAndrey A. Chernov 		}
1205690a6055SJesper Skriver 		/*
1206690a6055SJesper Skriver 		 * If we are over the maximum number of fragments
1207690a6055SJesper Skriver 		 * (due to the limit being lowered), drain off
1208690a6055SJesper Skriver 		 * enough to get down to the new limit.
1209690a6055SJesper Skriver 		 */
1210603724d3SBjoern A. Zeeb 		if (V_maxnipq >= 0 && V_nipq > V_maxnipq) {
1211690a6055SJesper Skriver 			for (i = 0; i < IPREASS_NHASH; i++) {
12128b615593SMarko Zec 				while (V_nipq > V_maxnipq &&
12138b615593SMarko Zec 				    !TAILQ_EMPTY(&V_ipq[i])) {
121486425c62SRobert Watson 					IPSTAT_ADD(ips_fragdropped,
121586425c62SRobert Watson 					    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
12168b615593SMarko Zec 					ip_freef(&V_ipq[i],
12178b615593SMarko Zec 					    TAILQ_FIRST(&V_ipq[i]));
1218690a6055SJesper Skriver 				}
1219690a6055SJesper Skriver 			}
1220690a6055SJesper Skriver 		}
12218b615593SMarko Zec 		CURVNET_RESTORE();
12228b615593SMarko Zec 	}
12232fad1e93SSam Leffler 	IPQ_UNLOCK();
12245ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
1225df8bae1dSRodney W. Grimes }
1226df8bae1dSRodney W. Grimes 
1227df8bae1dSRodney W. Grimes /*
1228df8bae1dSRodney W. Grimes  * Drain off all datagram fragments.
1229df8bae1dSRodney W. Grimes  */
12309802380eSBjoern A. Zeeb static void
12319802380eSBjoern A. Zeeb ip_drain_locked(void)
1232df8bae1dSRodney W. Grimes {
1233194a213eSAndrey A. Chernov 	int     i;
1234ce29ab3aSGarrett Wollman 
12359802380eSBjoern A. Zeeb 	IPQ_LOCK_ASSERT();
12369802380eSBjoern A. Zeeb 
1237194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1238603724d3SBjoern A. Zeeb 		while(!TAILQ_EMPTY(&V_ipq[i])) {
123986425c62SRobert Watson 			IPSTAT_ADD(ips_fragdropped,
124086425c62SRobert Watson 			    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
1241603724d3SBjoern A. Zeeb 			ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1242194a213eSAndrey A. Chernov 		}
1243194a213eSAndrey A. Chernov 	}
12449802380eSBjoern A. Zeeb }
12459802380eSBjoern A. Zeeb 
12469802380eSBjoern A. Zeeb void
12479802380eSBjoern A. Zeeb ip_drain(void)
12489802380eSBjoern A. Zeeb {
12499802380eSBjoern A. Zeeb 	VNET_ITERATOR_DECL(vnet_iter);
12509802380eSBjoern A. Zeeb 
12519802380eSBjoern A. Zeeb 	VNET_LIST_RLOCK_NOSLEEP();
12529802380eSBjoern A. Zeeb 	IPQ_LOCK();
12539802380eSBjoern A. Zeeb 	VNET_FOREACH(vnet_iter) {
12549802380eSBjoern A. Zeeb 		CURVNET_SET(vnet_iter);
12559802380eSBjoern A. Zeeb 		ip_drain_locked();
12568b615593SMarko Zec 		CURVNET_RESTORE();
12578b615593SMarko Zec 	}
12582fad1e93SSam Leffler 	IPQ_UNLOCK();
12595ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
1260ce29ab3aSGarrett Wollman 	in_rtqdrain();
1261df8bae1dSRodney W. Grimes }
1262df8bae1dSRodney W. Grimes 
1263df8bae1dSRodney W. Grimes /*
1264de38924dSAndre Oppermann  * The protocol to be inserted into ip_protox[] must be already registered
1265de38924dSAndre Oppermann  * in inetsw[], either statically or through pf_proto_register().
1266de38924dSAndre Oppermann  */
1267de38924dSAndre Oppermann int
12681b48d245SBjoern A. Zeeb ipproto_register(short ipproto)
1269de38924dSAndre Oppermann {
1270de38924dSAndre Oppermann 	struct protosw *pr;
1271de38924dSAndre Oppermann 
1272de38924dSAndre Oppermann 	/* Sanity checks. */
12731b48d245SBjoern A. Zeeb 	if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
1274de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1275de38924dSAndre Oppermann 
1276de38924dSAndre Oppermann 	/*
1277de38924dSAndre Oppermann 	 * The protocol slot must not be occupied by another protocol
1278de38924dSAndre Oppermann 	 * already.  An index pointing to IPPROTO_RAW is unused.
1279de38924dSAndre Oppermann 	 */
1280de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1281de38924dSAndre Oppermann 	if (pr == NULL)
1282de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1283de38924dSAndre Oppermann 	if (ip_protox[ipproto] != pr - inetsw)	/* IPPROTO_RAW */
1284de38924dSAndre Oppermann 		return (EEXIST);
1285de38924dSAndre Oppermann 
1286de38924dSAndre Oppermann 	/* Find the protocol position in inetsw[] and set the index. */
1287de38924dSAndre Oppermann 	for (pr = inetdomain.dom_protosw;
1288de38924dSAndre Oppermann 	     pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1289de38924dSAndre Oppermann 		if (pr->pr_domain->dom_family == PF_INET &&
1290de38924dSAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol == ipproto) {
1291de38924dSAndre Oppermann 			ip_protox[pr->pr_protocol] = pr - inetsw;
1292de38924dSAndre Oppermann 			return (0);
1293de38924dSAndre Oppermann 		}
1294de38924dSAndre Oppermann 	}
1295de38924dSAndre Oppermann 	return (EPROTONOSUPPORT);
1296de38924dSAndre Oppermann }
1297de38924dSAndre Oppermann 
1298de38924dSAndre Oppermann int
12991b48d245SBjoern A. Zeeb ipproto_unregister(short ipproto)
1300de38924dSAndre Oppermann {
1301de38924dSAndre Oppermann 	struct protosw *pr;
1302de38924dSAndre Oppermann 
1303de38924dSAndre Oppermann 	/* Sanity checks. */
13041b48d245SBjoern A. Zeeb 	if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
1305de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1306de38924dSAndre Oppermann 
1307de38924dSAndre Oppermann 	/* Check if the protocol was indeed registered. */
1308de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1309de38924dSAndre Oppermann 	if (pr == NULL)
1310de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1311de38924dSAndre Oppermann 	if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
1312de38924dSAndre Oppermann 		return (ENOENT);
1313de38924dSAndre Oppermann 
1314de38924dSAndre Oppermann 	/* Reset the protocol slot to IPPROTO_RAW. */
1315de38924dSAndre Oppermann 	ip_protox[ipproto] = pr - inetsw;
1316de38924dSAndre Oppermann 	return (0);
1317de38924dSAndre Oppermann }
1318de38924dSAndre Oppermann 
1319df8bae1dSRodney W. Grimes /*
13208c0fec80SRobert Watson  * Given address of next destination (final or next hop), return (referenced)
13218c0fec80SRobert Watson  * internet address info of interface to be used to get there.
1322df8bae1dSRodney W. Grimes  */
1323bd714208SRuslan Ermilov struct in_ifaddr *
13248b07e49aSJulian Elischer ip_rtaddr(struct in_addr dst, u_int fibnum)
1325df8bae1dSRodney W. Grimes {
132697d8d152SAndre Oppermann 	struct route sro;
132702c1c707SAndre Oppermann 	struct sockaddr_in *sin;
132819e5b0a7SRobert Watson 	struct in_ifaddr *ia;
1329df8bae1dSRodney W. Grimes 
13300cfbbe3bSAndre Oppermann 	bzero(&sro, sizeof(sro));
133197d8d152SAndre Oppermann 	sin = (struct sockaddr_in *)&sro.ro_dst;
1332df8bae1dSRodney W. Grimes 	sin->sin_family = AF_INET;
1333df8bae1dSRodney W. Grimes 	sin->sin_len = sizeof(*sin);
1334df8bae1dSRodney W. Grimes 	sin->sin_addr = dst;
13356e6b3f7cSQing Li 	in_rtalloc_ign(&sro, 0, fibnum);
1336df8bae1dSRodney W. Grimes 
133797d8d152SAndre Oppermann 	if (sro.ro_rt == NULL)
133802410549SRobert Watson 		return (NULL);
133902c1c707SAndre Oppermann 
134019e5b0a7SRobert Watson 	ia = ifatoia(sro.ro_rt->rt_ifa);
134119e5b0a7SRobert Watson 	ifa_ref(&ia->ia_ifa);
134297d8d152SAndre Oppermann 	RTFREE(sro.ro_rt);
134319e5b0a7SRobert Watson 	return (ia);
1344df8bae1dSRodney W. Grimes }
1345df8bae1dSRodney W. Grimes 
1346df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = {
1347df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1348df8bae1dSRodney W. Grimes 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1349df8bae1dSRodney W. Grimes 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1350df8bae1dSRodney W. Grimes 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1351fcaf9f91SMike Silbersack 	0,		0,		EHOSTUNREACH,	0,
13523b8123b7SJesper Skriver 	ENOPROTOOPT,	ECONNREFUSED
1353df8bae1dSRodney W. Grimes };
1354df8bae1dSRodney W. Grimes 
1355df8bae1dSRodney W. Grimes /*
1356df8bae1dSRodney W. Grimes  * Forward a packet.  If some error occurs return the sender
1357df8bae1dSRodney W. Grimes  * an icmp packet.  Note we can't always generate a meaningful
1358df8bae1dSRodney W. Grimes  * icmp message because icmp doesn't have a large enough repertoire
1359df8bae1dSRodney W. Grimes  * of codes and types.
1360df8bae1dSRodney W. Grimes  *
1361df8bae1dSRodney W. Grimes  * If not forwarding, just drop the packet.  This could be confusing
1362df8bae1dSRodney W. Grimes  * if ipforwarding was zero but some routing protocol was advancing
1363df8bae1dSRodney W. Grimes  * us as a gateway to somewhere.  However, we must let the routing
1364df8bae1dSRodney W. Grimes  * protocol deal with that.
1365df8bae1dSRodney W. Grimes  *
1366df8bae1dSRodney W. Grimes  * The srcrt parameter indicates whether the packet is being forwarded
1367df8bae1dSRodney W. Grimes  * via a source route.
1368df8bae1dSRodney W. Grimes  */
13699b932e9eSAndre Oppermann void
13709b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt)
1371df8bae1dSRodney W. Grimes {
13722b25acc1SLuigi Rizzo 	struct ip *ip = mtod(m, struct ip *);
1373efbad259SEdward Tomasz Napierala 	struct in_ifaddr *ia;
1374df8bae1dSRodney W. Grimes 	struct mbuf *mcopy;
13759b932e9eSAndre Oppermann 	struct in_addr dest;
1376b835b6feSBjoern A. Zeeb 	struct route ro;
1377c773494eSAndre Oppermann 	int error, type = 0, code = 0, mtu = 0;
13783efc3014SJulian Elischer 
13799b932e9eSAndre Oppermann 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
138086425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
1381df8bae1dSRodney W. Grimes 		m_freem(m);
1382df8bae1dSRodney W. Grimes 		return;
1383df8bae1dSRodney W. Grimes 	}
13841b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
1385603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
13861b968362SDag-Erling Smørgrav #endif
1387df8bae1dSRodney W. Grimes 		if (ip->ip_ttl <= IPTTLDEC) {
13881b968362SDag-Erling Smørgrav 			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
138902c1c707SAndre Oppermann 			    0, 0);
1390df8bae1dSRodney W. Grimes 			return;
1391df8bae1dSRodney W. Grimes 		}
13921b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
13931b968362SDag-Erling Smørgrav 	}
13941b968362SDag-Erling Smørgrav #endif
1395df8bae1dSRodney W. Grimes 
13968b07e49aSJulian Elischer 	ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
1397efbad259SEdward Tomasz Napierala #ifndef IPSEC
1398efbad259SEdward Tomasz Napierala 	/*
1399efbad259SEdward Tomasz Napierala 	 * 'ia' may be NULL if there is no route for this destination.
1400efbad259SEdward Tomasz Napierala 	 * In case of IPsec, Don't discard it just yet, but pass it to
1401efbad259SEdward Tomasz Napierala 	 * ip_output in case of outgoing IPsec policy.
1402efbad259SEdward Tomasz Napierala 	 */
1403d23d475fSGuido van Rooij 	if (!srcrt && ia == NULL) {
140402c1c707SAndre Oppermann 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1405df8bae1dSRodney W. Grimes 		return;
140602c1c707SAndre Oppermann 	}
1407efbad259SEdward Tomasz Napierala #endif
1408df8bae1dSRodney W. Grimes 
1409df8bae1dSRodney W. Grimes 	/*
1410bfef7ed4SIan Dowse 	 * Save the IP header and at most 8 bytes of the payload,
1411bfef7ed4SIan Dowse 	 * in case we need to generate an ICMP message to the src.
1412bfef7ed4SIan Dowse 	 *
14134d2e3692SLuigi Rizzo 	 * XXX this can be optimized a lot by saving the data in a local
14144d2e3692SLuigi Rizzo 	 * buffer on the stack (72 bytes at most), and only allocating the
14154d2e3692SLuigi Rizzo 	 * mbuf if really necessary. The vast majority of the packets
14164d2e3692SLuigi Rizzo 	 * are forwarded without having to send an ICMP back (either
14174d2e3692SLuigi Rizzo 	 * because unnecessary, or because rate limited), so we are
14184d2e3692SLuigi Rizzo 	 * really we are wasting a lot of work here.
14194d2e3692SLuigi Rizzo 	 *
1420bfef7ed4SIan Dowse 	 * We don't use m_copy() because it might return a reference
1421bfef7ed4SIan Dowse 	 * to a shared cluster. Both this function and ip_output()
1422bfef7ed4SIan Dowse 	 * assume exclusive access to the IP header in `m', so any
1423bfef7ed4SIan Dowse 	 * data in a cluster may change before we reach icmp_error().
1424df8bae1dSRodney W. Grimes 	 */
1425780b2f69SAndre Oppermann 	MGETHDR(mcopy, M_DONTWAIT, m->m_type);
1426a163d034SWarner Losh 	if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) {
14279967cafcSSam Leffler 		/*
14289967cafcSSam Leffler 		 * It's probably ok if the pkthdr dup fails (because
14299967cafcSSam Leffler 		 * the deep copy of the tag chain failed), but for now
14309967cafcSSam Leffler 		 * be conservative and just discard the copy since
14319967cafcSSam Leffler 		 * code below may some day want the tags.
14329967cafcSSam Leffler 		 */
14339967cafcSSam Leffler 		m_free(mcopy);
14349967cafcSSam Leffler 		mcopy = NULL;
14359967cafcSSam Leffler 	}
1436bfef7ed4SIan Dowse 	if (mcopy != NULL) {
1437780b2f69SAndre Oppermann 		mcopy->m_len = min(ip->ip_len, M_TRAILINGSPACE(mcopy));
1438e6b0a570SBruce M Simpson 		mcopy->m_pkthdr.len = mcopy->m_len;
1439bfef7ed4SIan Dowse 		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1440bfef7ed4SIan Dowse 	}
144104287599SRuslan Ermilov 
144204287599SRuslan Ermilov #ifdef IPSTEALTH
1443603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
144404287599SRuslan Ermilov #endif
144504287599SRuslan Ermilov 		ip->ip_ttl -= IPTTLDEC;
144604287599SRuslan Ermilov #ifdef IPSTEALTH
144704287599SRuslan Ermilov 	}
144804287599SRuslan Ermilov #endif
1449df8bae1dSRodney W. Grimes 
1450df8bae1dSRodney W. Grimes 	/*
1451df8bae1dSRodney W. Grimes 	 * If forwarding packet using same interface that it came in on,
1452df8bae1dSRodney W. Grimes 	 * perhaps should send a redirect to sender to shortcut a hop.
1453df8bae1dSRodney W. Grimes 	 * Only send redirect if source is sending directly to us,
1454df8bae1dSRodney W. Grimes 	 * and if packet was not source routed (or has any options).
1455df8bae1dSRodney W. Grimes 	 * Also, don't send redirect if forwarding using a default route
1456df8bae1dSRodney W. Grimes 	 * or a route modified by a redirect.
1457df8bae1dSRodney W. Grimes 	 */
14589b932e9eSAndre Oppermann 	dest.s_addr = 0;
1459efbad259SEdward Tomasz Napierala 	if (!srcrt && V_ipsendredirects &&
1460efbad259SEdward Tomasz Napierala 	    ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) {
146102c1c707SAndre Oppermann 		struct sockaddr_in *sin;
146202c1c707SAndre Oppermann 		struct rtentry *rt;
146302c1c707SAndre Oppermann 
14640cfbbe3bSAndre Oppermann 		bzero(&ro, sizeof(ro));
146502c1c707SAndre Oppermann 		sin = (struct sockaddr_in *)&ro.ro_dst;
146602c1c707SAndre Oppermann 		sin->sin_family = AF_INET;
146702c1c707SAndre Oppermann 		sin->sin_len = sizeof(*sin);
14689b932e9eSAndre Oppermann 		sin->sin_addr = ip->ip_dst;
14696e6b3f7cSQing Li 		in_rtalloc_ign(&ro, 0, M_GETFIB(m));
147002c1c707SAndre Oppermann 
147102c1c707SAndre Oppermann 		rt = ro.ro_rt;
147202c1c707SAndre Oppermann 
147302c1c707SAndre Oppermann 		if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
14749b932e9eSAndre Oppermann 		    satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1475df8bae1dSRodney W. Grimes #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1476df8bae1dSRodney W. Grimes 			u_long src = ntohl(ip->ip_src.s_addr);
1477df8bae1dSRodney W. Grimes 
1478df8bae1dSRodney W. Grimes 			if (RTA(rt) &&
1479df8bae1dSRodney W. Grimes 			    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1480df8bae1dSRodney W. Grimes 				if (rt->rt_flags & RTF_GATEWAY)
14819b932e9eSAndre Oppermann 					dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1482df8bae1dSRodney W. Grimes 				else
14839b932e9eSAndre Oppermann 					dest.s_addr = ip->ip_dst.s_addr;
1484df8bae1dSRodney W. Grimes 				/* Router requirements says to only send host redirects */
1485df8bae1dSRodney W. Grimes 				type = ICMP_REDIRECT;
1486df8bae1dSRodney W. Grimes 				code = ICMP_REDIRECT_HOST;
1487df8bae1dSRodney W. Grimes 			}
1488df8bae1dSRodney W. Grimes 		}
148902c1c707SAndre Oppermann 		if (rt)
149002c1c707SAndre Oppermann 			RTFREE(rt);
149102c1c707SAndre Oppermann 	}
1492df8bae1dSRodney W. Grimes 
1493b835b6feSBjoern A. Zeeb 	/*
1494b835b6feSBjoern A. Zeeb 	 * Try to cache the route MTU from ip_output so we can consider it for
1495b835b6feSBjoern A. Zeeb 	 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191.
1496b835b6feSBjoern A. Zeeb 	 */
1497b835b6feSBjoern A. Zeeb 	bzero(&ro, sizeof(ro));
1498b835b6feSBjoern A. Zeeb 
1499b835b6feSBjoern A. Zeeb 	error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1500b835b6feSBjoern A. Zeeb 
1501b835b6feSBjoern A. Zeeb 	if (error == EMSGSIZE && ro.ro_rt)
1502b835b6feSBjoern A. Zeeb 		mtu = ro.ro_rt->rt_rmx.rmx_mtu;
1503b835b6feSBjoern A. Zeeb 	if (ro.ro_rt)
1504b835b6feSBjoern A. Zeeb 		RTFREE(ro.ro_rt);
1505b835b6feSBjoern A. Zeeb 
1506df8bae1dSRodney W. Grimes 	if (error)
150786425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
1508df8bae1dSRodney W. Grimes 	else {
150986425c62SRobert Watson 		IPSTAT_INC(ips_forward);
1510df8bae1dSRodney W. Grimes 		if (type)
151186425c62SRobert Watson 			IPSTAT_INC(ips_redirectsent);
1512df8bae1dSRodney W. Grimes 		else {
15139188b4a1SAndre Oppermann 			if (mcopy)
1514df8bae1dSRodney W. Grimes 				m_freem(mcopy);
15158c0fec80SRobert Watson 			if (ia != NULL)
15168c0fec80SRobert Watson 				ifa_free(&ia->ia_ifa);
1517df8bae1dSRodney W. Grimes 			return;
1518df8bae1dSRodney W. Grimes 		}
1519df8bae1dSRodney W. Grimes 	}
15208c0fec80SRobert Watson 	if (mcopy == NULL) {
15218c0fec80SRobert Watson 		if (ia != NULL)
15228c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
1523df8bae1dSRodney W. Grimes 		return;
15248c0fec80SRobert Watson 	}
1525df8bae1dSRodney W. Grimes 
1526df8bae1dSRodney W. Grimes 	switch (error) {
1527df8bae1dSRodney W. Grimes 
1528df8bae1dSRodney W. Grimes 	case 0:				/* forwarded, but need redirect */
1529df8bae1dSRodney W. Grimes 		/* type, code set above */
1530df8bae1dSRodney W. Grimes 		break;
1531df8bae1dSRodney W. Grimes 
1532efbad259SEdward Tomasz Napierala 	case ENETUNREACH:
1533df8bae1dSRodney W. Grimes 	case EHOSTUNREACH:
1534df8bae1dSRodney W. Grimes 	case ENETDOWN:
1535df8bae1dSRodney W. Grimes 	case EHOSTDOWN:
1536df8bae1dSRodney W. Grimes 	default:
1537df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1538df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_HOST;
1539df8bae1dSRodney W. Grimes 		break;
1540df8bae1dSRodney W. Grimes 
1541df8bae1dSRodney W. Grimes 	case EMSGSIZE:
1542df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1543df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_NEEDFRAG;
15441dfcf0d2SAndre Oppermann 
1545b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
1546b835b6feSBjoern A. Zeeb 		/*
1547b835b6feSBjoern A. Zeeb 		 * If IPsec is configured for this path,
1548b835b6feSBjoern A. Zeeb 		 * override any possibly mtu value set by ip_output.
1549b835b6feSBjoern A. Zeeb 		 */
15501c044382SBjoern A. Zeeb 		mtu = ip_ipsec_mtu(mcopy, mtu);
1551b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
15529b932e9eSAndre Oppermann 		/*
1553b835b6feSBjoern A. Zeeb 		 * If the MTU was set before make sure we are below the
1554b835b6feSBjoern A. Zeeb 		 * interface MTU.
1555ab48768bSAndre Oppermann 		 * If the MTU wasn't set before use the interface mtu or
1556ab48768bSAndre Oppermann 		 * fall back to the next smaller mtu step compared to the
1557ab48768bSAndre Oppermann 		 * current packet size.
15589b932e9eSAndre Oppermann 		 */
1559b835b6feSBjoern A. Zeeb 		if (mtu != 0) {
1560b835b6feSBjoern A. Zeeb 			if (ia != NULL)
1561b835b6feSBjoern A. Zeeb 				mtu = min(mtu, ia->ia_ifp->if_mtu);
1562b835b6feSBjoern A. Zeeb 		} else {
1563ab48768bSAndre Oppermann 			if (ia != NULL)
1564c773494eSAndre Oppermann 				mtu = ia->ia_ifp->if_mtu;
1565ab48768bSAndre Oppermann 			else
1566ab48768bSAndre Oppermann 				mtu = ip_next_mtu(ip->ip_len, 0);
1567ab48768bSAndre Oppermann 		}
156886425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
1569df8bae1dSRodney W. Grimes 		break;
1570df8bae1dSRodney W. Grimes 
1571df8bae1dSRodney W. Grimes 	case ENOBUFS:
1572df285b3dSMike Silbersack 		/*
1573df285b3dSMike Silbersack 		 * A router should not generate ICMP_SOURCEQUENCH as
1574df285b3dSMike Silbersack 		 * required in RFC1812 Requirements for IP Version 4 Routers.
1575df285b3dSMike Silbersack 		 * Source quench could be a big problem under DoS attacks,
1576df285b3dSMike Silbersack 		 * or if the underlying interface is rate-limited.
1577df285b3dSMike Silbersack 		 * Those who need source quench packets may re-enable them
1578df285b3dSMike Silbersack 		 * via the net.inet.ip.sendsourcequench sysctl.
1579df285b3dSMike Silbersack 		 */
1580603724d3SBjoern A. Zeeb 		if (V_ip_sendsourcequench == 0) {
1581df285b3dSMike Silbersack 			m_freem(mcopy);
15828c0fec80SRobert Watson 			if (ia != NULL)
15838c0fec80SRobert Watson 				ifa_free(&ia->ia_ifa);
1584df285b3dSMike Silbersack 			return;
1585df285b3dSMike Silbersack 		} else {
1586df8bae1dSRodney W. Grimes 			type = ICMP_SOURCEQUENCH;
1587df8bae1dSRodney W. Grimes 			code = 0;
1588df285b3dSMike Silbersack 		}
1589df8bae1dSRodney W. Grimes 		break;
15903a06e3e0SRuslan Ermilov 
15913a06e3e0SRuslan Ermilov 	case EACCES:			/* ipfw denied packet */
15923a06e3e0SRuslan Ermilov 		m_freem(mcopy);
15938c0fec80SRobert Watson 		if (ia != NULL)
15948c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
15953a06e3e0SRuslan Ermilov 		return;
1596df8bae1dSRodney W. Grimes 	}
15978c0fec80SRobert Watson 	if (ia != NULL)
15988c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
1599c773494eSAndre Oppermann 	icmp_error(mcopy, type, code, dest.s_addr, mtu);
1600df8bae1dSRodney W. Grimes }
1601df8bae1dSRodney W. Grimes 
160282c23ebaSBill Fenner void
1603f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1604f2565d68SRobert Watson     struct mbuf *m)
160582c23ebaSBill Fenner {
16068b615593SMarko Zec 
1607be8a62e8SPoul-Henning Kamp 	if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1608be8a62e8SPoul-Henning Kamp 		struct bintime bt;
1609be8a62e8SPoul-Henning Kamp 
1610be8a62e8SPoul-Henning Kamp 		bintime(&bt);
1611be8a62e8SPoul-Henning Kamp 		if (inp->inp_socket->so_options & SO_BINTIME) {
1612be8a62e8SPoul-Henning Kamp 			*mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt),
1613be8a62e8SPoul-Henning Kamp 			SCM_BINTIME, SOL_SOCKET);
1614be8a62e8SPoul-Henning Kamp 			if (*mp)
1615be8a62e8SPoul-Henning Kamp 				mp = &(*mp)->m_next;
1616be8a62e8SPoul-Henning Kamp 		}
161782c23ebaSBill Fenner 		if (inp->inp_socket->so_options & SO_TIMESTAMP) {
161882c23ebaSBill Fenner 			struct timeval tv;
161982c23ebaSBill Fenner 
1620be8a62e8SPoul-Henning Kamp 			bintime2timeval(&bt, &tv);
162182c23ebaSBill Fenner 			*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
162282c23ebaSBill Fenner 				SCM_TIMESTAMP, SOL_SOCKET);
162382c23ebaSBill Fenner 			if (*mp)
162482c23ebaSBill Fenner 				mp = &(*mp)->m_next;
16254cc20ab1SSeigo Tanimura 		}
1626be8a62e8SPoul-Henning Kamp 	}
162782c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVDSTADDR) {
162882c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
162982c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
163082c23ebaSBill Fenner 		if (*mp)
163182c23ebaSBill Fenner 			mp = &(*mp)->m_next;
163282c23ebaSBill Fenner 	}
16334957466bSMatthew N. Dodd 	if (inp->inp_flags & INP_RECVTTL) {
16344957466bSMatthew N. Dodd 		*mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
16354957466bSMatthew N. Dodd 		    sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
16364957466bSMatthew N. Dodd 		if (*mp)
16374957466bSMatthew N. Dodd 			mp = &(*mp)->m_next;
16384957466bSMatthew N. Dodd 	}
163982c23ebaSBill Fenner #ifdef notyet
164082c23ebaSBill Fenner 	/* XXX
164182c23ebaSBill Fenner 	 * Moving these out of udp_input() made them even more broken
164282c23ebaSBill Fenner 	 * than they already were.
164382c23ebaSBill Fenner 	 */
164482c23ebaSBill Fenner 	/* options were tossed already */
164582c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVOPTS) {
164682c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
164782c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
164882c23ebaSBill Fenner 		if (*mp)
164982c23ebaSBill Fenner 			mp = &(*mp)->m_next;
165082c23ebaSBill Fenner 	}
165182c23ebaSBill Fenner 	/* ip_srcroute doesn't do what we want here, need to fix */
165282c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVRETOPTS) {
1653e0982661SAndre Oppermann 		*mp = sbcreatecontrol((caddr_t) ip_srcroute(m),
165482c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
165582c23ebaSBill Fenner 		if (*mp)
165682c23ebaSBill Fenner 			mp = &(*mp)->m_next;
165782c23ebaSBill Fenner 	}
165882c23ebaSBill Fenner #endif
165982c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVIF) {
1660d314ad7bSJulian Elischer 		struct ifnet *ifp;
1661d314ad7bSJulian Elischer 		struct sdlbuf {
166282c23ebaSBill Fenner 			struct sockaddr_dl sdl;
1663d314ad7bSJulian Elischer 			u_char	pad[32];
1664d314ad7bSJulian Elischer 		} sdlbuf;
1665d314ad7bSJulian Elischer 		struct sockaddr_dl *sdp;
1666d314ad7bSJulian Elischer 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
166782c23ebaSBill Fenner 
1668d314ad7bSJulian Elischer 		if (((ifp = m->m_pkthdr.rcvif))
1669603724d3SBjoern A. Zeeb 		&& ( ifp->if_index && (ifp->if_index <= V_if_index))) {
16704a0d6638SRuslan Ermilov 			sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1671d314ad7bSJulian Elischer 			/*
1672d314ad7bSJulian Elischer 			 * Change our mind and don't try copy.
1673d314ad7bSJulian Elischer 			 */
1674d314ad7bSJulian Elischer 			if ((sdp->sdl_family != AF_LINK)
1675d314ad7bSJulian Elischer 			|| (sdp->sdl_len > sizeof(sdlbuf))) {
1676d314ad7bSJulian Elischer 				goto makedummy;
1677d314ad7bSJulian Elischer 			}
1678d314ad7bSJulian Elischer 			bcopy(sdp, sdl2, sdp->sdl_len);
1679d314ad7bSJulian Elischer 		} else {
1680d314ad7bSJulian Elischer makedummy:
1681d314ad7bSJulian Elischer 			sdl2->sdl_len
1682d314ad7bSJulian Elischer 				= offsetof(struct sockaddr_dl, sdl_data[0]);
1683d314ad7bSJulian Elischer 			sdl2->sdl_family = AF_LINK;
1684d314ad7bSJulian Elischer 			sdl2->sdl_index = 0;
1685d314ad7bSJulian Elischer 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1686d314ad7bSJulian Elischer 		}
1687d314ad7bSJulian Elischer 		*mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
168882c23ebaSBill Fenner 			IP_RECVIF, IPPROTO_IP);
168982c23ebaSBill Fenner 		if (*mp)
169082c23ebaSBill Fenner 			mp = &(*mp)->m_next;
169182c23ebaSBill Fenner 	}
169282c23ebaSBill Fenner }
169382c23ebaSBill Fenner 
16944d2e3692SLuigi Rizzo /*
169530916a2dSRobert Watson  * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
169630916a2dSRobert Watson  * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
169730916a2dSRobert Watson  * locking.  This code remains in ip_input.c as ip_mroute.c is optionally
169830916a2dSRobert Watson  * compiled.
16994d2e3692SLuigi Rizzo  */
17003e288e62SDimitry Andric static VNET_DEFINE(int, ip_rsvp_on);
170182cea7e6SBjoern A. Zeeb VNET_DEFINE(struct socket *, ip_rsvpd);
170282cea7e6SBjoern A. Zeeb 
170382cea7e6SBjoern A. Zeeb #define	V_ip_rsvp_on		VNET(ip_rsvp_on)
170482cea7e6SBjoern A. Zeeb 
1705df8bae1dSRodney W. Grimes int
1706f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so)
1707f0068c4aSGarrett Wollman {
17088b615593SMarko Zec 
1709f0068c4aSGarrett Wollman 	if (so->so_type != SOCK_RAW ||
1710f0068c4aSGarrett Wollman 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1711f0068c4aSGarrett Wollman 		return EOPNOTSUPP;
1712f0068c4aSGarrett Wollman 
1713603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL)
1714f0068c4aSGarrett Wollman 		return EADDRINUSE;
1715f0068c4aSGarrett Wollman 
1716603724d3SBjoern A. Zeeb 	V_ip_rsvpd = so;
17171c5de19aSGarrett Wollman 	/*
17181c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-increment
17191c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
17201c5de19aSGarrett Wollman 	 */
1721603724d3SBjoern A. Zeeb 	if (!V_ip_rsvp_on) {
1722603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 1;
1723603724d3SBjoern A. Zeeb 		V_rsvp_on++;
17241c5de19aSGarrett Wollman 	}
1725f0068c4aSGarrett Wollman 
1726f0068c4aSGarrett Wollman 	return 0;
1727f0068c4aSGarrett Wollman }
1728f0068c4aSGarrett Wollman 
1729f0068c4aSGarrett Wollman int
1730f0068c4aSGarrett Wollman ip_rsvp_done(void)
1731f0068c4aSGarrett Wollman {
17328b615593SMarko Zec 
1733603724d3SBjoern A. Zeeb 	V_ip_rsvpd = NULL;
17341c5de19aSGarrett Wollman 	/*
17351c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-decrement
17361c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
17371c5de19aSGarrett Wollman 	 */
1738603724d3SBjoern A. Zeeb 	if (V_ip_rsvp_on) {
1739603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 0;
1740603724d3SBjoern A. Zeeb 		V_rsvp_on--;
17411c5de19aSGarrett Wollman 	}
1742f0068c4aSGarrett Wollman 	return 0;
1743f0068c4aSGarrett Wollman }
1744bbb4330bSLuigi Rizzo 
1745bbb4330bSLuigi Rizzo void
1746bbb4330bSLuigi Rizzo rsvp_input(struct mbuf *m, int off)	/* XXX must fixup manually */
1747bbb4330bSLuigi Rizzo {
17488b615593SMarko Zec 
1749bbb4330bSLuigi Rizzo 	if (rsvp_input_p) { /* call the real one if loaded */
1750bbb4330bSLuigi Rizzo 		rsvp_input_p(m, off);
1751bbb4330bSLuigi Rizzo 		return;
1752bbb4330bSLuigi Rizzo 	}
1753bbb4330bSLuigi Rizzo 
1754bbb4330bSLuigi Rizzo 	/* Can still get packets with rsvp_on = 0 if there is a local member
1755bbb4330bSLuigi Rizzo 	 * of the group to which the RSVP packet is addressed.  But in this
1756bbb4330bSLuigi Rizzo 	 * case we want to throw the packet away.
1757bbb4330bSLuigi Rizzo 	 */
1758bbb4330bSLuigi Rizzo 
1759603724d3SBjoern A. Zeeb 	if (!V_rsvp_on) {
1760bbb4330bSLuigi Rizzo 		m_freem(m);
1761bbb4330bSLuigi Rizzo 		return;
1762bbb4330bSLuigi Rizzo 	}
1763bbb4330bSLuigi Rizzo 
1764603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL) {
1765bbb4330bSLuigi Rizzo 		rip_input(m, off);
1766bbb4330bSLuigi Rizzo 		return;
1767bbb4330bSLuigi Rizzo 	}
1768bbb4330bSLuigi Rizzo 	/* Drop the packet */
1769bbb4330bSLuigi Rizzo 	m_freem(m);
1770bbb4330bSLuigi Rizzo }
1771