xref: /freebsd/sys/netinet/ip_input.c (revision 86425c62a0067a5c61d3c87ab5f3a489e3c4fd25)
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"
4036b0360bSRobert Watson #include "opt_mac.h"
41a9771948SGleb Smirnoff #include "opt_carp.h"
4274a9466cSGary Palmer 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
44df8bae1dSRodney W. Grimes #include <sys/systm.h>
455f311da2SMike Silbersack #include <sys/callout.h>
46df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
47b715f178SLuigi Rizzo #include <sys/malloc.h>
48df8bae1dSRodney W. Grimes #include <sys/domain.h>
49df8bae1dSRodney W. Grimes #include <sys/protosw.h>
50df8bae1dSRodney W. Grimes #include <sys/socket.h>
51df8bae1dSRodney W. Grimes #include <sys/time.h>
52df8bae1dSRodney W. Grimes #include <sys/kernel.h>
53385195c0SMarko Zec #include <sys/lock.h>
54385195c0SMarko Zec #include <sys/rwlock.h>
551025071fSGarrett Wollman #include <sys/syslog.h>
56b5e8ce9fSBruce Evans #include <sys/sysctl.h>
57603724d3SBjoern A. Zeeb #include <sys/vimage.h>
58df8bae1dSRodney W. Grimes 
59c85540ddSAndrey A. Chernov #include <net/pfil.h>
60df8bae1dSRodney W. Grimes #include <net/if.h>
619494d596SBrooks Davis #include <net/if_types.h>
62d314ad7bSJulian Elischer #include <net/if_var.h>
6382c23ebaSBill Fenner #include <net/if_dl.h>
64df8bae1dSRodney W. Grimes #include <net/route.h>
65748e0b0aSGarrett Wollman #include <net/netisr.h>
664b79449eSBjoern A. Zeeb #include <net/vnet.h>
67df8bae1dSRodney W. Grimes 
68df8bae1dSRodney W. Grimes #include <netinet/in.h>
69df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
70b5e8ce9fSBruce Evans #include <netinet/in_var.h>
71df8bae1dSRodney W. Grimes #include <netinet/ip.h>
72df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
73df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
74df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
75ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
7658938916SGarrett Wollman #include <machine/in_cksum.h>
774b79449eSBjoern A. Zeeb #include <netinet/vinet.h>
78a9771948SGleb Smirnoff #ifdef DEV_CARP
79a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
80a9771948SGleb Smirnoff #endif
81b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
821dfcf0d2SAndre Oppermann #include <netinet/ip_ipsec.h>
83b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
84df8bae1dSRodney W. Grimes 
85f0068c4aSGarrett Wollman #include <sys/socketvar.h>
866ddbf1e2SGary Palmer 
87010b65f5SJulian Elischer /* XXX: Temporary until ipfw_ether and ipfw_bridge are converted. */
88010b65f5SJulian Elischer #include <netinet/ip_fw.h>
89010b65f5SJulian Elischer #include <netinet/ip_dummynet.h>
90010b65f5SJulian Elischer 
91aed55708SRobert Watson #include <security/mac/mac_framework.h>
92aed55708SRobert Watson 
93d2035ffbSEd Maste #ifdef CTASSERT
94d2035ffbSEd Maste CTASSERT(sizeof(struct ip) == 20);
95d2035ffbSEd Maste #endif
96d2035ffbSEd Maste 
97385195c0SMarko Zec #ifndef VIMAGE
98385195c0SMarko Zec #ifndef VIMAGE_GLOBALS
99385195c0SMarko Zec struct vnet_inet vnet_inet_0;
100385195c0SMarko Zec #endif
101385195c0SMarko Zec #endif
102385195c0SMarko Zec 
10344e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
10444e33a07SMarko Zec static int	ipsendredirects;
10544e33a07SMarko Zec static int	ip_checkinterface;
10644e33a07SMarko Zec static int	ip_keepfaith;
10744e33a07SMarko Zec static int	ip_sendsourcequench;
10844e33a07SMarko Zec int	ip_defttl;
10944e33a07SMarko Zec int	ip_do_randomid;
11044e33a07SMarko Zec int	ipforwarding;
11144e33a07SMarko Zec struct	in_ifaddrhead in_ifaddrhead; 		/* first inet address */
11244e33a07SMarko Zec struct	in_ifaddrhashhead *in_ifaddrhashtbl;	/* inet addr hash table  */
11344e33a07SMarko Zec u_long 	in_ifaddrhmask;				/* mask for hash table */
11444e33a07SMarko Zec struct ipstat ipstat;
11544e33a07SMarko Zec static int ip_rsvp_on;
11644e33a07SMarko Zec struct socket *ip_rsvpd;
11744e33a07SMarko Zec int	rsvp_on;
118f02493cbSMarko Zec static struct ipqhead ipq[IPREASS_NHASH];
11944e33a07SMarko Zec static int	maxnipq;	/* Administrative limit on # reass queues. */
12044e33a07SMarko Zec static int	maxfragsperpacket;
12144e33a07SMarko Zec int	ipstealth;
12244e33a07SMarko Zec static int	nipq;	/* Total # of reass queues */
12344e33a07SMarko Zec #endif
124f0068c4aSGarrett Wollman 
1258b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_FORWARDING,
1268b615593SMarko Zec     forwarding, CTLFLAG_RW, ipforwarding, 0,
1278b615593SMarko Zec     "Enable IP forwarding between interfaces");
1280312fbe9SPoul-Henning Kamp 
1298b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_SENDREDIRECTS,
1308b615593SMarko Zec     redirect, CTLFLAG_RW, ipsendredirects, 0,
1318b615593SMarko Zec     "Enable sending IP redirects");
1320312fbe9SPoul-Henning Kamp 
1338b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_DEFTTL,
1348b615593SMarko Zec     ttl, CTLFLAG_RW, ip_defttl, 0, "Maximum TTL on IP packets");
1350312fbe9SPoul-Henning Kamp 
1368b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_KEEPFAITH,
1378b615593SMarko Zec     keepfaith, CTLFLAG_RW, ip_keepfaith,	0,
1386a800098SYoshinobu Inoue     "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
1396a800098SYoshinobu Inoue 
1408b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO,
1418b615593SMarko Zec     sendsourcequench, CTLFLAG_RW, ip_sendsourcequench, 0,
142df285b3dSMike Silbersack     "Enable the transmission of source quench packets");
143df285b3dSMike Silbersack 
1448b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, random_id,
1458b615593SMarko Zec     CTLFLAG_RW, ip_do_randomid, 0, "Assign random ip_id values");
1461f44b0a1SDavid Malone 
147823db0e9SDon Lewis /*
148823db0e9SDon Lewis  * XXX - Setting ip_checkinterface mostly implements the receive side of
149823db0e9SDon Lewis  * the Strong ES model described in RFC 1122, but since the routing table
150a8f12100SDon Lewis  * and transmit implementation do not implement the Strong ES model,
151823db0e9SDon Lewis  * setting this to 1 results in an odd hybrid.
1523f67c834SDon Lewis  *
153a8f12100SDon Lewis  * XXX - ip_checkinterface currently must be disabled if you use ipnat
154a8f12100SDon Lewis  * to translate the destination address to another local interface.
1553f67c834SDon Lewis  *
1563f67c834SDon Lewis  * XXX - ip_checkinterface must be disabled if you add IP aliases
1573f67c834SDon Lewis  * to the loopback interface instead of the interface where the
1583f67c834SDon Lewis  * packets for those addresses are received.
159823db0e9SDon Lewis  */
1608b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO,
1618b615593SMarko Zec     check_interface, CTLFLAG_RW, ip_checkinterface, 0,
1628b615593SMarko Zec     "Verify packet arrives on correct interface");
163b3e95d4eSJonathan Lemon 
164c21fd232SAndre Oppermann struct pfil_head inet_pfil_hook;	/* Packet filter hooks */
165df8bae1dSRodney W. Grimes 
1661cafed39SJonathan Lemon static struct	ifqueue ipintrq;
167ca925d9cSJonathan Lemon static int	ipqmaxlen = IFQ_MAXLEN;
168ca925d9cSJonathan Lemon 
169df8bae1dSRodney W. Grimes extern	struct domain inetdomain;
170f0ffb944SJulian Elischer extern	struct protosw inetsw[];
171df8bae1dSRodney W. Grimes u_char	ip_protox[IPPROTO_MAX];
172ca925d9cSJonathan Lemon 
173afed1375SDavid Greenman SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
1743d177f46SBill Fumerola     &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
1750312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
1766489fe65SAndre Oppermann     &ipintrq.ifq_drops, 0,
1776489fe65SAndre Oppermann     "Number of packets dropped from the IP input queue");
178df8bae1dSRodney W. Grimes 
1798b615593SMarko Zec SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
1808b615593SMarko Zec     ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
181194a213eSAndrey A. Chernov 
182385195c0SMarko Zec #ifdef VIMAGE_GLOBALS
183d248c7d7SRobert Watson static uma_zone_t ipq_zone;
184385195c0SMarko Zec #endif
185dfa60d93SRobert Watson static struct mtx ipqlock;
1862fad1e93SSam Leffler 
1872fad1e93SSam Leffler #define	IPQ_LOCK()	mtx_lock(&ipqlock)
1882fad1e93SSam Leffler #define	IPQ_UNLOCK()	mtx_unlock(&ipqlock)
189888c2a3cSSam Leffler #define	IPQ_LOCK_INIT()	mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
190888c2a3cSSam Leffler #define	IPQ_LOCK_ASSERT()	mtx_assert(&ipqlock, MA_OWNED)
191f23b4c91SGarrett Wollman 
192d248c7d7SRobert Watson static void	maxnipq_update(void);
1934f590175SPaul Saab static void	ipq_zone_change(void *);
194d248c7d7SRobert Watson 
1958b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, fragpackets,
1968b615593SMarko Zec     CTLFLAG_RD, nipq, 0,
1978b615593SMarko Zec     "Current number of IPv4 fragment reassembly queue entries");
198d248c7d7SRobert Watson 
1998b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, maxfragsperpacket,
2008b615593SMarko Zec     CTLFLAG_RW, maxfragsperpacket, 0,
201d248c7d7SRobert Watson     "Maximum number of IPv4 fragments allowed per packet");
202d248c7d7SRobert Watson 
203d248c7d7SRobert Watson struct callout	ipport_tick_callout;
204d248c7d7SRobert Watson 
2050312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU
2060312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
2073d177f46SBill Fumerola     &ip_mtu, 0, "Default MTU");
2080312fbe9SPoul-Henning Kamp #endif
2090312fbe9SPoul-Henning Kamp 
2101b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
2118b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
2128b615593SMarko Zec     ipstealth, 0, "IP stealth mode, no TTL decrementation on forwarding");
2131b968362SDag-Erling Smørgrav #endif
2141b968362SDag-Erling Smørgrav 
215010b65f5SJulian Elischer /*
216010b65f5SJulian Elischer  * ipfw_ether and ipfw_bridge hooks.
217010b65f5SJulian Elischer  * XXX: Temporary until those are converted to pfil_hooks as well.
218010b65f5SJulian Elischer  */
219010b65f5SJulian Elischer ip_fw_chk_t *ip_fw_chk_ptr = NULL;
220010b65f5SJulian Elischer ip_dn_io_t *ip_dn_io_ptr = NULL;
221385195c0SMarko Zec #ifdef VIMAGE_GLOBALS
222385195c0SMarko Zec int fw_one_pass;
223385195c0SMarko Zec #endif
224010b65f5SJulian Elischer 
2254d77a549SAlfred Perlstein static void	ip_freef(struct ipqhead *, struct ipq *);
2268948e4baSArchie Cobbs 
227bfe1aba4SMarko Zec #ifndef VIMAGE_GLOBALS
228bfe1aba4SMarko Zec static void vnet_inet_register(void);
229bfe1aba4SMarko Zec 
230bfe1aba4SMarko Zec static const vnet_modinfo_t vnet_inet_modinfo = {
231bfe1aba4SMarko Zec 	.vmi_id		= VNET_MOD_INET,
232bfe1aba4SMarko Zec 	.vmi_name	= "inet",
233bfe1aba4SMarko Zec };
234bfe1aba4SMarko Zec 
235bfe1aba4SMarko Zec static void vnet_inet_register()
236bfe1aba4SMarko Zec {
237bfe1aba4SMarko Zec 
238bfe1aba4SMarko Zec 	vnet_mod_register(&vnet_inet_modinfo);
239bfe1aba4SMarko Zec }
240bfe1aba4SMarko Zec 
241bfe1aba4SMarko Zec SYSINIT(inet, SI_SUB_PROTO_BEGIN, SI_ORDER_FIRST, vnet_inet_register, 0);
242bfe1aba4SMarko Zec #endif
243bfe1aba4SMarko Zec 
244df8bae1dSRodney W. Grimes /*
245df8bae1dSRodney W. Grimes  * IP initialization: fill in IP protocol switch table.
246df8bae1dSRodney W. Grimes  * All protocols not implemented in kernel go to raw IP protocol handler.
247df8bae1dSRodney W. Grimes  */
248df8bae1dSRodney W. Grimes void
249f2565d68SRobert Watson ip_init(void)
250df8bae1dSRodney W. Grimes {
2518b615593SMarko Zec 	INIT_VNET_INET(curvnet);
252f2565d68SRobert Watson 	struct protosw *pr;
253f2565d68SRobert Watson 	int i;
254df8bae1dSRodney W. Grimes 
25544e33a07SMarko Zec 	V_ipsendredirects = 1; /* XXX */
25644e33a07SMarko Zec 	V_ip_checkinterface = 0;
25744e33a07SMarko Zec 	V_ip_keepfaith = 0;
25844e33a07SMarko Zec 	V_ip_sendsourcequench = 0;
25944e33a07SMarko Zec 	V_rsvp_on = 0;
26044e33a07SMarko Zec 	V_ip_defttl = IPDEFTTL;
26144e33a07SMarko Zec 	V_ip_do_randomid = 0;
2621ed81b73SMarko Zec 	V_ip_id = time_second & 0xffff;
26344e33a07SMarko Zec 	V_ipforwarding = 0;
26444e33a07SMarko Zec 	V_ipstealth = 0;
26544e33a07SMarko Zec 	V_nipq = 0;	/* Total # of reass queues */
26644e33a07SMarko Zec 
26744e33a07SMarko Zec 	V_ipport_lowfirstauto = IPPORT_RESERVED - 1;	/* 1023 */
26844e33a07SMarko Zec 	V_ipport_lowlastauto = IPPORT_RESERVEDSTART;	/* 600 */
26944e33a07SMarko Zec 	V_ipport_firstauto = IPPORT_EPHEMERALFIRST;	/* 10000 */
27044e33a07SMarko Zec 	V_ipport_lastauto = IPPORT_EPHEMERALLAST;	/* 65535 */
27144e33a07SMarko Zec 	V_ipport_hifirstauto = IPPORT_HIFIRSTAUTO;	/* 49152 */
27244e33a07SMarko Zec 	V_ipport_hilastauto = IPPORT_HILASTAUTO;	/* 65535 */
27344e33a07SMarko Zec 	V_ipport_reservedhigh = IPPORT_RESERVED - 1;	/* 1023 */
27444e33a07SMarko Zec 	V_ipport_reservedlow = 0;
27544e33a07SMarko Zec 	V_ipport_randomized = 1;	/* user controlled via sysctl */
27644e33a07SMarko Zec 	V_ipport_randomcps = 10;	/* user controlled via sysctl */
27744e33a07SMarko Zec 	V_ipport_randomtime = 45;	/* user controlled via sysctl */
27844e33a07SMarko Zec 	V_ipport_stoprandom = 0;	/* toggled by ipport_tick */
27944e33a07SMarko Zec 
280385195c0SMarko Zec 	V_fw_one_pass = 1;
281385195c0SMarko Zec 
28244e33a07SMarko Zec #ifdef NOTYET
28344e33a07SMarko Zec 	/* XXX global static but not instantiated in this file */
28444e33a07SMarko Zec 	V_ipfastforward_active = 0;
28544e33a07SMarko Zec 	V_subnetsarelocal = 0;
28644e33a07SMarko Zec 	V_sameprefixcarponly = 0;
28744e33a07SMarko Zec #endif
28844e33a07SMarko Zec 
289603724d3SBjoern A. Zeeb 	TAILQ_INIT(&V_in_ifaddrhead);
290603724d3SBjoern A. Zeeb 	V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
2911ed81b73SMarko Zec 
2921ed81b73SMarko Zec 	/* Initialize IP reassembly queue. */
2931ed81b73SMarko Zec 	for (i = 0; i < IPREASS_NHASH; i++)
2941ed81b73SMarko Zec 		TAILQ_INIT(&V_ipq[i]);
2951ed81b73SMarko Zec 	V_maxnipq = nmbclusters / 32;
2961ed81b73SMarko Zec 	V_maxfragsperpacket = 16;
2971ed81b73SMarko Zec 	V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
2981ed81b73SMarko Zec 	    NULL, UMA_ALIGN_PTR, 0);
2991ed81b73SMarko Zec 	maxnipq_update();
3001ed81b73SMarko Zec 
3011ed81b73SMarko Zec 	/* Skip initialization of globals for non-default instances. */
3021ed81b73SMarko Zec 	if (!IS_DEFAULT_VNET(curvnet))
3031ed81b73SMarko Zec 		return;
3041ed81b73SMarko Zec 
305f0ffb944SJulian Elischer 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
30602410549SRobert Watson 	if (pr == NULL)
307db09bef3SAndre Oppermann 		panic("ip_init: PF_INET not found");
308db09bef3SAndre Oppermann 
309db09bef3SAndre Oppermann 	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
310df8bae1dSRodney W. Grimes 	for (i = 0; i < IPPROTO_MAX; i++)
311df8bae1dSRodney W. Grimes 		ip_protox[i] = pr - inetsw;
312db09bef3SAndre Oppermann 	/*
313db09bef3SAndre Oppermann 	 * Cycle through IP protocols and put them into the appropriate place
314db09bef3SAndre Oppermann 	 * in ip_protox[].
315db09bef3SAndre Oppermann 	 */
316f0ffb944SJulian Elischer 	for (pr = inetdomain.dom_protosw;
317f0ffb944SJulian Elischer 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
318df8bae1dSRodney W. Grimes 		if (pr->pr_domain->dom_family == PF_INET &&
319db09bef3SAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
320db09bef3SAndre Oppermann 			/* Be careful to only index valid IP protocols. */
321db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX)
322df8bae1dSRodney W. Grimes 				ip_protox[pr->pr_protocol] = pr - inetsw;
323db09bef3SAndre Oppermann 		}
324194a213eSAndrey A. Chernov 
325c21fd232SAndre Oppermann 	/* Initialize packet filter hooks. */
326134ea224SSam Leffler 	inet_pfil_hook.ph_type = PFIL_TYPE_AF;
327134ea224SSam Leffler 	inet_pfil_hook.ph_af = AF_INET;
328134ea224SSam Leffler 	if ((i = pfil_head_register(&inet_pfil_hook)) != 0)
329134ea224SSam Leffler 		printf("%s: WARNING: unable to register pfil hook, "
330134ea224SSam Leffler 			"error %d\n", __func__, i);
331134ea224SSam Leffler 
3325f311da2SMike Silbersack 	/* Start ipport_tick. */
3335f311da2SMike Silbersack 	callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
3345f311da2SMike Silbersack 	ipport_tick(NULL);
3355f311da2SMike Silbersack 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
3365f311da2SMike Silbersack 		SHUTDOWN_PRI_DEFAULT);
3374f590175SPaul Saab 	EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
3384f590175SPaul Saab 		NULL, EVENTHANDLER_PRI_ANY);
3395f311da2SMike Silbersack 
340db09bef3SAndre Oppermann 	/* Initialize various other remaining things. */
3411ed81b73SMarko Zec 	IPQ_LOCK_INIT();
342df8bae1dSRodney W. Grimes 	ipintrq.ifq_maxlen = ipqmaxlen;
3436008862bSJohn Baldwin 	mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF);
34459dd72d0SRobert Watson 	netisr_register(NETISR_IP, ip_input, &ipintrq, 0);
345df8bae1dSRodney W. Grimes }
346df8bae1dSRodney W. Grimes 
347f2565d68SRobert Watson void
348f2565d68SRobert Watson ip_fini(void *xtp)
3495f311da2SMike Silbersack {
350f2565d68SRobert Watson 
3515f311da2SMike Silbersack 	callout_stop(&ipport_tick_callout);
3525f311da2SMike Silbersack }
3535f311da2SMike Silbersack 
3544d2e3692SLuigi Rizzo /*
355df8bae1dSRodney W. Grimes  * Ip input routine.  Checksum and byte swap header.  If fragmented
356df8bae1dSRodney W. Grimes  * try to reassemble.  Process options.  Pass to next level.
357df8bae1dSRodney W. Grimes  */
358c67b1d17SGarrett Wollman void
359c67b1d17SGarrett Wollman ip_input(struct mbuf *m)
360df8bae1dSRodney W. Grimes {
3618b615593SMarko Zec 	INIT_VNET_INET(curvnet);
3629188b4a1SAndre Oppermann 	struct ip *ip = NULL;
3635da9f8faSJosef Karthauser 	struct in_ifaddr *ia = NULL;
364ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
3659b932e9eSAndre Oppermann 	int    checkif, hlen = 0;
36647c861ecSBrian Somers 	u_short sum;
36702c1c707SAndre Oppermann 	int dchg = 0;				/* dest changed after fw */
368f51f805fSSam Leffler 	struct in_addr odst;			/* original dst address */
369b715f178SLuigi Rizzo 
370fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
371db40007dSAndrew R. Reiter 
372ac9d7e26SMax Laier 	if (m->m_flags & M_FASTFWD_OURS) {
3739b932e9eSAndre Oppermann 		/*
37476ff6dcfSAndre Oppermann 		 * Firewall or NAT changed destination to local.
37576ff6dcfSAndre Oppermann 		 * We expect ip_len and ip_off to be in host byte order.
3769b932e9eSAndre Oppermann 		 */
37776ff6dcfSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
37876ff6dcfSAndre Oppermann 		/* Set up some basics that will be used later. */
3792b25acc1SLuigi Rizzo 		ip = mtod(m, struct ip *);
38053be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
3819b932e9eSAndre Oppermann 		goto ours;
3822b25acc1SLuigi Rizzo 	}
3832b25acc1SLuigi Rizzo 
38486425c62SRobert Watson 	IPSTAT_INC(ips_total);
38558938916SGarrett Wollman 
38658938916SGarrett Wollman 	if (m->m_pkthdr.len < sizeof(struct ip))
38758938916SGarrett Wollman 		goto tooshort;
38858938916SGarrett Wollman 
389df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct ip) &&
3900b17fba7SAndre Oppermann 	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
39186425c62SRobert Watson 		IPSTAT_INC(ips_toosmall);
392c67b1d17SGarrett Wollman 		return;
393df8bae1dSRodney W. Grimes 	}
394df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
39558938916SGarrett Wollman 
39653be11f6SPoul-Henning Kamp 	if (ip->ip_v != IPVERSION) {
39786425c62SRobert Watson 		IPSTAT_INC(ips_badvers);
398df8bae1dSRodney W. Grimes 		goto bad;
399df8bae1dSRodney W. Grimes 	}
40058938916SGarrett Wollman 
40153be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
402df8bae1dSRodney W. Grimes 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
40386425c62SRobert Watson 		IPSTAT_INC(ips_badhlen);
404df8bae1dSRodney W. Grimes 		goto bad;
405df8bae1dSRodney W. Grimes 	}
406df8bae1dSRodney W. Grimes 	if (hlen > m->m_len) {
4070b17fba7SAndre Oppermann 		if ((m = m_pullup(m, hlen)) == NULL) {
40886425c62SRobert Watson 			IPSTAT_INC(ips_badhlen);
409c67b1d17SGarrett Wollman 			return;
410df8bae1dSRodney W. Grimes 		}
411df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
412df8bae1dSRodney W. Grimes 	}
41333841545SHajimu UMEMOTO 
41433841545SHajimu UMEMOTO 	/* 127/8 must not appear on wire - RFC1122 */
41533841545SHajimu UMEMOTO 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
41633841545SHajimu UMEMOTO 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
41733841545SHajimu UMEMOTO 		if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
41886425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
41933841545SHajimu UMEMOTO 			goto bad;
42033841545SHajimu UMEMOTO 		}
42133841545SHajimu UMEMOTO 	}
42233841545SHajimu UMEMOTO 
423db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
424db4f9cc7SJonathan Lemon 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
425db4f9cc7SJonathan Lemon 	} else {
42658938916SGarrett Wollman 		if (hlen == sizeof(struct ip)) {
42747c861ecSBrian Somers 			sum = in_cksum_hdr(ip);
42858938916SGarrett Wollman 		} else {
42947c861ecSBrian Somers 			sum = in_cksum(m, hlen);
43058938916SGarrett Wollman 		}
431db4f9cc7SJonathan Lemon 	}
43247c861ecSBrian Somers 	if (sum) {
43386425c62SRobert Watson 		IPSTAT_INC(ips_badsum);
434df8bae1dSRodney W. Grimes 		goto bad;
435df8bae1dSRodney W. Grimes 	}
436df8bae1dSRodney W. Grimes 
43702b199f1SMax Laier #ifdef ALTQ
43802b199f1SMax Laier 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
43902b199f1SMax Laier 		/* packet is dropped by traffic conditioner */
44002b199f1SMax Laier 		return;
44102b199f1SMax Laier #endif
44202b199f1SMax Laier 
443df8bae1dSRodney W. Grimes 	/*
444df8bae1dSRodney W. Grimes 	 * Convert fields to host representation.
445df8bae1dSRodney W. Grimes 	 */
446fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
447df8bae1dSRodney W. Grimes 	if (ip->ip_len < hlen) {
44886425c62SRobert Watson 		IPSTAT_INC(ips_badlen);
449df8bae1dSRodney W. Grimes 		goto bad;
450df8bae1dSRodney W. Grimes 	}
451fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
452df8bae1dSRodney W. Grimes 
453df8bae1dSRodney W. Grimes 	/*
454df8bae1dSRodney W. Grimes 	 * Check that the amount of data in the buffers
455df8bae1dSRodney W. Grimes 	 * is as at least much as the IP header would have us expect.
456df8bae1dSRodney W. Grimes 	 * Trim mbufs if longer than we expect.
457df8bae1dSRodney W. Grimes 	 * Drop packet if shorter than we expect.
458df8bae1dSRodney W. Grimes 	 */
459df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len < ip->ip_len) {
46058938916SGarrett Wollman tooshort:
46186425c62SRobert Watson 		IPSTAT_INC(ips_tooshort);
462df8bae1dSRodney W. Grimes 		goto bad;
463df8bae1dSRodney W. Grimes 	}
464df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len > ip->ip_len) {
465df8bae1dSRodney W. Grimes 		if (m->m_len == m->m_pkthdr.len) {
466df8bae1dSRodney W. Grimes 			m->m_len = ip->ip_len;
467df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = ip->ip_len;
468df8bae1dSRodney W. Grimes 		} else
469df8bae1dSRodney W. Grimes 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
470df8bae1dSRodney W. Grimes 	}
471b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
47214dd6717SSam Leffler 	/*
47314dd6717SSam Leffler 	 * Bypass packet filtering for packets from a tunnel (gif).
47414dd6717SSam Leffler 	 */
475cc977adcSBjoern A. Zeeb 	if (ip_ipsec_filtertunnel(m))
476c21fd232SAndre Oppermann 		goto passin;
477b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
4783f67c834SDon Lewis 
479c4ac87eaSDarren Reed 	/*
480134ea224SSam Leffler 	 * Run through list of hooks for input packets.
481f51f805fSSam Leffler 	 *
482f51f805fSSam Leffler 	 * NB: Beware of the destination address changing (e.g.
483f51f805fSSam Leffler 	 *     by NAT rewriting).  When this happens, tell
484f51f805fSSam Leffler 	 *     ip_forward to do the right thing.
485c4ac87eaSDarren Reed 	 */
486c21fd232SAndre Oppermann 
487c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
488604afec4SChristian S.J. Peron 	if (!PFIL_HOOKED(&inet_pfil_hook))
489c21fd232SAndre Oppermann 		goto passin;
490c21fd232SAndre Oppermann 
491f51f805fSSam Leffler 	odst = ip->ip_dst;
492134ea224SSam Leffler 	if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
493d6a8d588SMax Laier 	    PFIL_IN, NULL) != 0)
494beec8214SDarren Reed 		return;
495134ea224SSam Leffler 	if (m == NULL)			/* consumed by filter */
496c4ac87eaSDarren Reed 		return;
4979b932e9eSAndre Oppermann 
498c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
49902c1c707SAndre Oppermann 	dchg = (odst.s_addr != ip->ip_dst.s_addr);
5009b932e9eSAndre Oppermann 
5019b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
5029b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
5039b932e9eSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
5049b932e9eSAndre Oppermann 		goto ours;
5059b932e9eSAndre Oppermann 	}
506099dd043SAndre Oppermann 	if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) {
507099dd043SAndre Oppermann 		/*
508099dd043SAndre Oppermann 		 * Directly ship on the packet.  This allows to forward packets
509099dd043SAndre Oppermann 		 * that were destined for us to some other directly connected
510099dd043SAndre Oppermann 		 * host.
511099dd043SAndre Oppermann 		 */
512099dd043SAndre Oppermann 		ip_forward(m, dchg);
513099dd043SAndre Oppermann 		return;
514099dd043SAndre Oppermann 	}
5159b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
5169b932e9eSAndre Oppermann 
517c21fd232SAndre Oppermann passin:
518df8bae1dSRodney W. Grimes 	/*
519df8bae1dSRodney W. Grimes 	 * Process options and, if not destined for us,
520df8bae1dSRodney W. Grimes 	 * ship it on.  ip_dooptions returns 1 when an
521df8bae1dSRodney W. Grimes 	 * error was detected (causing an icmp message
522df8bae1dSRodney W. Grimes 	 * to be sent and the original packet to be freed).
523df8bae1dSRodney W. Grimes 	 */
5249b932e9eSAndre Oppermann 	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
525c67b1d17SGarrett Wollman 		return;
526df8bae1dSRodney W. Grimes 
527f0068c4aSGarrett Wollman         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
528f0068c4aSGarrett Wollman          * matter if it is destined to another node, or whether it is
529f0068c4aSGarrett Wollman          * a multicast one, RSVP wants it! and prevents it from being forwarded
530f0068c4aSGarrett Wollman          * anywhere else. Also checks if the rsvp daemon is running before
531f0068c4aSGarrett Wollman 	 * grabbing the packet.
532f0068c4aSGarrett Wollman          */
533603724d3SBjoern A. Zeeb 	if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP)
534f0068c4aSGarrett Wollman 		goto ours;
535f0068c4aSGarrett Wollman 
536df8bae1dSRodney W. Grimes 	/*
537df8bae1dSRodney W. Grimes 	 * Check our list of addresses, to see if the packet is for us.
538cc766e04SGarrett Wollman 	 * If we don't have any addresses, assume any unicast packet
539cc766e04SGarrett Wollman 	 * we receive might be for us (and let the upper layers deal
540cc766e04SGarrett Wollman 	 * with it).
541df8bae1dSRodney W. Grimes 	 */
542603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
543cc766e04SGarrett Wollman 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
544cc766e04SGarrett Wollman 		goto ours;
545cc766e04SGarrett Wollman 
5467538a9a0SJonathan Lemon 	/*
547823db0e9SDon Lewis 	 * Enable a consistency check between the destination address
548823db0e9SDon Lewis 	 * and the arrival interface for a unicast packet (the RFC 1122
549823db0e9SDon Lewis 	 * strong ES model) if IP forwarding is disabled and the packet
550e15ae1b2SDon Lewis 	 * is not locally generated and the packet is not subject to
551e15ae1b2SDon Lewis 	 * 'ipfw fwd'.
5523f67c834SDon Lewis 	 *
5533f67c834SDon Lewis 	 * XXX - Checking also should be disabled if the destination
5543f67c834SDon Lewis 	 * address is ipnat'ed to a different interface.
5553f67c834SDon Lewis 	 *
556a8f12100SDon Lewis 	 * XXX - Checking is incompatible with IP aliases added
5573f67c834SDon Lewis 	 * to the loopback interface instead of the interface where
5583f67c834SDon Lewis 	 * the packets are received.
559a9771948SGleb Smirnoff 	 *
560a9771948SGleb Smirnoff 	 * XXX - This is the case for carp vhost IPs as well so we
561a9771948SGleb Smirnoff 	 * insert a workaround. If the packet got here, we already
562a9771948SGleb Smirnoff 	 * checked with carp_iamatch() and carp_forus().
563823db0e9SDon Lewis 	 */
564603724d3SBjoern A. Zeeb 	checkif = V_ip_checkinterface && (V_ipforwarding == 0) &&
5659494d596SBrooks Davis 	    m->m_pkthdr.rcvif != NULL &&
566e15ae1b2SDon Lewis 	    ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
567a9771948SGleb Smirnoff #ifdef DEV_CARP
568a9771948SGleb Smirnoff 	    !m->m_pkthdr.rcvif->if_carp &&
569a9771948SGleb Smirnoff #endif
5709b932e9eSAndre Oppermann 	    (dchg == 0);
571823db0e9SDon Lewis 
572ca925d9cSJonathan Lemon 	/*
573ca925d9cSJonathan Lemon 	 * Check for exact addresses in the hash bucket.
574ca925d9cSJonathan Lemon 	 */
5759b932e9eSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
576f9e354dfSJulian Elischer 		/*
577823db0e9SDon Lewis 		 * If the address matches, verify that the packet
578823db0e9SDon Lewis 		 * arrived via the correct interface if checking is
579823db0e9SDon Lewis 		 * enabled.
580f9e354dfSJulian Elischer 		 */
5819b932e9eSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
582823db0e9SDon Lewis 		    (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
583ed1ff184SJulian Elischer 			goto ours;
584ca925d9cSJonathan Lemon 	}
585823db0e9SDon Lewis 	/*
586ca925d9cSJonathan Lemon 	 * Check for broadcast addresses.
587ca925d9cSJonathan Lemon 	 *
588ca925d9cSJonathan Lemon 	 * Only accept broadcast packets that arrive via the matching
589ca925d9cSJonathan Lemon 	 * interface.  Reception of forwarded directed broadcasts would
590ca925d9cSJonathan Lemon 	 * be handled via ip_forward() and ether_output() with the loopback
591ca925d9cSJonathan Lemon 	 * into the stack for SIMPLEX interfaces handled by ether_output().
592823db0e9SDon Lewis 	 */
5934f450ff9SBruce M Simpson 	if (m->m_pkthdr.rcvif != NULL &&
5944f450ff9SBruce M Simpson 	    m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
595ca925d9cSJonathan Lemon 	        TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
596ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
597ca925d9cSJonathan Lemon 				continue;
598ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
599df8bae1dSRodney W. Grimes 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
6009b932e9eSAndre Oppermann 			    ip->ip_dst.s_addr)
601df8bae1dSRodney W. Grimes 				goto ours;
6029b932e9eSAndre Oppermann 			if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr)
603df8bae1dSRodney W. Grimes 				goto ours;
6040ac40133SBrian Somers #ifdef BOOTP_COMPAT
605ca925d9cSJonathan Lemon 			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
606ca925d9cSJonathan Lemon 				goto ours;
6070ac40133SBrian Somers #endif
608df8bae1dSRodney W. Grimes 		}
609df8bae1dSRodney W. Grimes 	}
610f8429ca2SBruce M Simpson 	/* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
611f8429ca2SBruce M Simpson 	if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
61286425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
613f8429ca2SBruce M Simpson 		m_freem(m);
614f8429ca2SBruce M Simpson 		return;
615f8429ca2SBruce M Simpson 	}
616df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
617603724d3SBjoern A. Zeeb 		if (V_ip_mrouter) {
618df8bae1dSRodney W. Grimes 			/*
619df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, all
620df8bae1dSRodney W. Grimes 			 * incoming multicast packets are passed to the
621df8bae1dSRodney W. Grimes 			 * kernel-level multicast forwarding function.
622df8bae1dSRodney W. Grimes 			 * The packet is returned (relatively) intact; if
623df8bae1dSRodney W. Grimes 			 * ip_mforward() returns a non-zero value, the packet
624df8bae1dSRodney W. Grimes 			 * must be discarded, else it may be accepted below.
625df8bae1dSRodney W. Grimes 			 */
626bbb4330bSLuigi Rizzo 			if (ip_mforward &&
627bbb4330bSLuigi Rizzo 			    ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
62886425c62SRobert Watson 				IPSTAT_INC(ips_cantforward);
629df8bae1dSRodney W. Grimes 				m_freem(m);
630c67b1d17SGarrett Wollman 				return;
631df8bae1dSRodney W. Grimes 			}
632df8bae1dSRodney W. Grimes 
633df8bae1dSRodney W. Grimes 			/*
63411612afaSDima Dorfman 			 * The process-level routing daemon needs to receive
635df8bae1dSRodney W. Grimes 			 * all multicast IGMP packets, whether or not this
636df8bae1dSRodney W. Grimes 			 * host belongs to their destination groups.
637df8bae1dSRodney W. Grimes 			 */
638df8bae1dSRodney W. Grimes 			if (ip->ip_p == IPPROTO_IGMP)
639df8bae1dSRodney W. Grimes 				goto ours;
64086425c62SRobert Watson 			IPSTAT_INC(ips_forward);
641df8bae1dSRodney W. Grimes 		}
642df8bae1dSRodney W. Grimes 		/*
643d10910e6SBruce M Simpson 		 * Assume the packet is for us, to avoid prematurely taking
644d10910e6SBruce M Simpson 		 * a lock on the in_multi hash. Protocols must perform
645d10910e6SBruce M Simpson 		 * their own filtering and update statistics accordingly.
646df8bae1dSRodney W. Grimes 		 */
647df8bae1dSRodney W. Grimes 		goto ours;
648df8bae1dSRodney W. Grimes 	}
649df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
650df8bae1dSRodney W. Grimes 		goto ours;
651df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == INADDR_ANY)
652df8bae1dSRodney W. Grimes 		goto ours;
653df8bae1dSRodney W. Grimes 
6546a800098SYoshinobu Inoue 	/*
6556a800098SYoshinobu Inoue 	 * FAITH(Firewall Aided Internet Translator)
6566a800098SYoshinobu Inoue 	 */
6576a800098SYoshinobu Inoue 	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
658603724d3SBjoern A. Zeeb 		if (V_ip_keepfaith) {
6596a800098SYoshinobu Inoue 			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
6606a800098SYoshinobu Inoue 				goto ours;
6616a800098SYoshinobu Inoue 		}
6626a800098SYoshinobu Inoue 		m_freem(m);
6636a800098SYoshinobu Inoue 		return;
6646a800098SYoshinobu Inoue 	}
6659494d596SBrooks Davis 
666df8bae1dSRodney W. Grimes 	/*
667df8bae1dSRodney W. Grimes 	 * Not for us; forward if possible and desirable.
668df8bae1dSRodney W. Grimes 	 */
669603724d3SBjoern A. Zeeb 	if (V_ipforwarding == 0) {
67086425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
671df8bae1dSRodney W. Grimes 		m_freem(m);
672546f251bSChris D. Faulhaber 	} else {
673b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
6741dfcf0d2SAndre Oppermann 		if (ip_ipsec_fwd(m))
675546f251bSChris D. Faulhaber 			goto bad;
676b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
6779b932e9eSAndre Oppermann 		ip_forward(m, dchg);
678546f251bSChris D. Faulhaber 	}
679c67b1d17SGarrett Wollman 	return;
680df8bae1dSRodney W. Grimes 
681df8bae1dSRodney W. Grimes ours:
682d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH
683d0ebc0d2SYaroslav Tykhiy 	/*
684d0ebc0d2SYaroslav Tykhiy 	 * IPSTEALTH: Process non-routing options only
685d0ebc0d2SYaroslav Tykhiy 	 * if the packet is destined for us.
686d0ebc0d2SYaroslav Tykhiy 	 */
687603724d3SBjoern A. Zeeb 	if (V_ipstealth && hlen > sizeof (struct ip) &&
6889b932e9eSAndre Oppermann 	    ip_dooptions(m, 1))
689d0ebc0d2SYaroslav Tykhiy 		return;
690d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */
691d0ebc0d2SYaroslav Tykhiy 
6925da9f8faSJosef Karthauser 	/* Count the packet in the ip address stats */
6935da9f8faSJosef Karthauser 	if (ia != NULL) {
6945da9f8faSJosef Karthauser 		ia->ia_ifa.if_ipackets++;
6955da9f8faSJosef Karthauser 		ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
6965da9f8faSJosef Karthauser 	}
697100ba1a6SJordan K. Hubbard 
69863f8d699SJordan K. Hubbard 	/*
699b6ea1aa5SRuslan Ermilov 	 * Attempt reassembly; if it succeeds, proceed.
700ac9d7e26SMax Laier 	 * ip_reass() will return a different mbuf.
701df8bae1dSRodney W. Grimes 	 */
702f0cada84SAndre Oppermann 	if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
703f0cada84SAndre Oppermann 		m = ip_reass(m);
704f0cada84SAndre Oppermann 		if (m == NULL)
705c67b1d17SGarrett Wollman 			return;
7066a800098SYoshinobu Inoue 		ip = mtod(m, struct ip *);
7077e2df452SRuslan Ermilov 		/* Get the header length of the reassembled packet */
70853be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
709f0cada84SAndre Oppermann 	}
710f0cada84SAndre Oppermann 
711f0cada84SAndre Oppermann 	/*
712f0cada84SAndre Oppermann 	 * Further protocols expect the packet length to be w/o the
713f0cada84SAndre Oppermann 	 * IP header.
714f0cada84SAndre Oppermann 	 */
715df8bae1dSRodney W. Grimes 	ip->ip_len -= hlen;
716df8bae1dSRodney W. Grimes 
717b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
71833841545SHajimu UMEMOTO 	/*
71933841545SHajimu UMEMOTO 	 * enforce IPsec policy checking if we are seeing last header.
72033841545SHajimu UMEMOTO 	 * note that we do not visit this with protocols with pcb layer
72133841545SHajimu UMEMOTO 	 * code - like udp/tcp/raw ip.
72233841545SHajimu UMEMOTO 	 */
7231dfcf0d2SAndre Oppermann 	if (ip_ipsec_input(m))
72433841545SHajimu UMEMOTO 		goto bad;
725b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
72633841545SHajimu UMEMOTO 
727df8bae1dSRodney W. Grimes 	/*
728df8bae1dSRodney W. Grimes 	 * Switch out to protocol's input routine.
729df8bae1dSRodney W. Grimes 	 */
73086425c62SRobert Watson 	IPSTAT_INC(ips_delivered);
7319b932e9eSAndre Oppermann 
7322b25acc1SLuigi Rizzo 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
733c67b1d17SGarrett Wollman 	return;
734df8bae1dSRodney W. Grimes bad:
735df8bae1dSRodney W. Grimes 	m_freem(m);
736c67b1d17SGarrett Wollman }
737c67b1d17SGarrett Wollman 
738c67b1d17SGarrett Wollman /*
739d248c7d7SRobert Watson  * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
740d248c7d7SRobert Watson  * max has slightly different semantics than the sysctl, for historical
741d248c7d7SRobert Watson  * reasons.
742d248c7d7SRobert Watson  */
743d248c7d7SRobert Watson static void
744d248c7d7SRobert Watson maxnipq_update(void)
745d248c7d7SRobert Watson {
7468b615593SMarko Zec 	INIT_VNET_INET(curvnet);
747d248c7d7SRobert Watson 
748d248c7d7SRobert Watson 	/*
749d248c7d7SRobert Watson 	 * -1 for unlimited allocation.
750d248c7d7SRobert Watson 	 */
751603724d3SBjoern A. Zeeb 	if (V_maxnipq < 0)
752603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 0);
753d248c7d7SRobert Watson 	/*
754d248c7d7SRobert Watson 	 * Positive number for specific bound.
755d248c7d7SRobert Watson 	 */
756603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0)
757603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, V_maxnipq);
758d248c7d7SRobert Watson 	/*
759d248c7d7SRobert Watson 	 * Zero specifies no further fragment queue allocation -- set the
760d248c7d7SRobert Watson 	 * bound very low, but rely on implementation elsewhere to actually
761d248c7d7SRobert Watson 	 * prevent allocation and reclaim current queues.
762d248c7d7SRobert Watson 	 */
763603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0)
764603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 1);
765d248c7d7SRobert Watson }
766d248c7d7SRobert Watson 
7674f590175SPaul Saab static void
7684f590175SPaul Saab ipq_zone_change(void *tag)
7694f590175SPaul Saab {
7708b615593SMarko Zec 	INIT_VNET_INET(curvnet);
7714f590175SPaul Saab 
772603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) {
773603724d3SBjoern A. Zeeb 		V_maxnipq = nmbclusters / 32;
7744f590175SPaul Saab 		maxnipq_update();
7754f590175SPaul Saab 	}
7764f590175SPaul Saab }
7774f590175SPaul Saab 
778d248c7d7SRobert Watson static int
779d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
780d248c7d7SRobert Watson {
7818b615593SMarko Zec 	INIT_VNET_INET(curvnet);
782d248c7d7SRobert Watson 	int error, i;
783d248c7d7SRobert Watson 
784603724d3SBjoern A. Zeeb 	i = V_maxnipq;
785d248c7d7SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
786d248c7d7SRobert Watson 	if (error || !req->newptr)
787d248c7d7SRobert Watson 		return (error);
788d248c7d7SRobert Watson 
789d248c7d7SRobert Watson 	/*
790d248c7d7SRobert Watson 	 * XXXRW: Might be a good idea to sanity check the argument and place
791d248c7d7SRobert Watson 	 * an extreme upper bound.
792d248c7d7SRobert Watson 	 */
793d248c7d7SRobert Watson 	if (i < -1)
794d248c7d7SRobert Watson 		return (EINVAL);
795603724d3SBjoern A. Zeeb 	V_maxnipq = i;
796d248c7d7SRobert Watson 	maxnipq_update();
797d248c7d7SRobert Watson 	return (0);
798d248c7d7SRobert Watson }
799d248c7d7SRobert Watson 
800d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
801d248c7d7SRobert Watson     NULL, 0, sysctl_maxnipq, "I",
802d248c7d7SRobert Watson     "Maximum number of IPv4 fragment reassembly queue entries");
803d248c7d7SRobert Watson 
804d248c7d7SRobert Watson /*
8058948e4baSArchie Cobbs  * Take incoming datagram fragment and try to reassemble it into
806f0cada84SAndre Oppermann  * whole datagram.  If the argument is the first fragment or one
807f0cada84SAndre Oppermann  * in between the function will return NULL and store the mbuf
808f0cada84SAndre Oppermann  * in the fragment chain.  If the argument is the last fragment
809f0cada84SAndre Oppermann  * the packet will be reassembled and the pointer to the new
810f0cada84SAndre Oppermann  * mbuf returned for further processing.  Only m_tags attached
811f0cada84SAndre Oppermann  * to the first packet/fragment are preserved.
812f0cada84SAndre Oppermann  * The IP header is *NOT* adjusted out of iplen.
813df8bae1dSRodney W. Grimes  */
814f0cada84SAndre Oppermann struct mbuf *
815f0cada84SAndre Oppermann ip_reass(struct mbuf *m)
816df8bae1dSRodney W. Grimes {
8178b615593SMarko Zec 	INIT_VNET_INET(curvnet);
818f0cada84SAndre Oppermann 	struct ip *ip;
819f0cada84SAndre Oppermann 	struct mbuf *p, *q, *nq, *t;
820f0cada84SAndre Oppermann 	struct ipq *fp = NULL;
821f0cada84SAndre Oppermann 	struct ipqhead *head;
822f0cada84SAndre Oppermann 	int i, hlen, next;
82359dfcba4SHajimu UMEMOTO 	u_int8_t ecn, ecn0;
824f0cada84SAndre Oppermann 	u_short hash;
825df8bae1dSRodney W. Grimes 
826800af1fbSMaxim Konovalov 	/* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
827603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0 || V_maxfragsperpacket == 0) {
82886425c62SRobert Watson 		IPSTAT_INC(ips_fragments);
82986425c62SRobert Watson 		IPSTAT_INC(ips_fragdropped);
8309d804f81SAndre Oppermann 		m_freem(m);
8319d804f81SAndre Oppermann 		return (NULL);
832f0cada84SAndre Oppermann 	}
8332fad1e93SSam Leffler 
834f0cada84SAndre Oppermann 	ip = mtod(m, struct ip *);
835f0cada84SAndre Oppermann 	hlen = ip->ip_hl << 2;
836f0cada84SAndre Oppermann 
837f0cada84SAndre Oppermann 	hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
838603724d3SBjoern A. Zeeb 	head = &V_ipq[hash];
839f0cada84SAndre Oppermann 	IPQ_LOCK();
840f0cada84SAndre Oppermann 
841f0cada84SAndre Oppermann 	/*
842f0cada84SAndre Oppermann 	 * Look for queue of fragments
843f0cada84SAndre Oppermann 	 * of this datagram.
844f0cada84SAndre Oppermann 	 */
845f0cada84SAndre Oppermann 	TAILQ_FOREACH(fp, head, ipq_list)
846f0cada84SAndre Oppermann 		if (ip->ip_id == fp->ipq_id &&
847f0cada84SAndre Oppermann 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
848f0cada84SAndre Oppermann 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
849f0cada84SAndre Oppermann #ifdef MAC
85030d239bcSRobert Watson 		    mac_ipq_match(m, fp) &&
851f0cada84SAndre Oppermann #endif
852f0cada84SAndre Oppermann 		    ip->ip_p == fp->ipq_p)
853f0cada84SAndre Oppermann 			goto found;
854f0cada84SAndre Oppermann 
855f0cada84SAndre Oppermann 	fp = NULL;
856f0cada84SAndre Oppermann 
857f0cada84SAndre Oppermann 	/*
858d248c7d7SRobert Watson 	 * Attempt to trim the number of allocated fragment queues if it
859d248c7d7SRobert Watson 	 * exceeds the administrative limit.
860f0cada84SAndre Oppermann 	 */
861603724d3SBjoern A. Zeeb 	if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) {
862f0cada84SAndre Oppermann 		/*
863f0cada84SAndre Oppermann 		 * drop something from the tail of the current queue
864f0cada84SAndre Oppermann 		 * before proceeding further
865f0cada84SAndre Oppermann 		 */
866f0cada84SAndre Oppermann 		struct ipq *q = TAILQ_LAST(head, ipqhead);
867f0cada84SAndre Oppermann 		if (q == NULL) {   /* gak */
868f0cada84SAndre Oppermann 			for (i = 0; i < IPREASS_NHASH; i++) {
869603724d3SBjoern A. Zeeb 				struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead);
870f0cada84SAndre Oppermann 				if (r) {
87186425c62SRobert Watson 					IPSTAT_ADD(ips_fragtimeout,
87286425c62SRobert Watson 					    r->ipq_nfrags);
873603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], r);
874f0cada84SAndre Oppermann 					break;
875f0cada84SAndre Oppermann 				}
876f0cada84SAndre Oppermann 			}
877f0cada84SAndre Oppermann 		} else {
87886425c62SRobert Watson 			IPSTAT_ADD(ips_fragtimeout, q->ipq_nfrags);
879f0cada84SAndre Oppermann 			ip_freef(head, q);
880f0cada84SAndre Oppermann 		}
881f0cada84SAndre Oppermann 	}
882f0cada84SAndre Oppermann 
883f0cada84SAndre Oppermann found:
884f0cada84SAndre Oppermann 	/*
885f0cada84SAndre Oppermann 	 * Adjust ip_len to not reflect header,
886f0cada84SAndre Oppermann 	 * convert offset of this to bytes.
887f0cada84SAndre Oppermann 	 */
888f0cada84SAndre Oppermann 	ip->ip_len -= hlen;
889f0cada84SAndre Oppermann 	if (ip->ip_off & IP_MF) {
890f0cada84SAndre Oppermann 		/*
891f0cada84SAndre Oppermann 		 * Make sure that fragments have a data length
892f0cada84SAndre Oppermann 		 * that's a non-zero multiple of 8 bytes.
893f0cada84SAndre Oppermann 		 */
894f0cada84SAndre Oppermann 		if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
89586425c62SRobert Watson 			IPSTAT_INC(ips_toosmall); /* XXX */
896f0cada84SAndre Oppermann 			goto dropfrag;
897f0cada84SAndre Oppermann 		}
898f0cada84SAndre Oppermann 		m->m_flags |= M_FRAG;
899f0cada84SAndre Oppermann 	} else
900f0cada84SAndre Oppermann 		m->m_flags &= ~M_FRAG;
901f0cada84SAndre Oppermann 	ip->ip_off <<= 3;
902f0cada84SAndre Oppermann 
903f0cada84SAndre Oppermann 
904f0cada84SAndre Oppermann 	/*
905f0cada84SAndre Oppermann 	 * Attempt reassembly; if it succeeds, proceed.
906f0cada84SAndre Oppermann 	 * ip_reass() will return a different mbuf.
907f0cada84SAndre Oppermann 	 */
90886425c62SRobert Watson 	IPSTAT_INC(ips_fragments);
909f0cada84SAndre Oppermann 	m->m_pkthdr.header = ip;
910f0cada84SAndre Oppermann 
911f0cada84SAndre Oppermann 	/* Previous ip_reass() started here. */
912df8bae1dSRodney W. Grimes 	/*
913df8bae1dSRodney W. Grimes 	 * Presence of header sizes in mbufs
914df8bae1dSRodney W. Grimes 	 * would confuse code below.
915df8bae1dSRodney W. Grimes 	 */
916df8bae1dSRodney W. Grimes 	m->m_data += hlen;
917df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
918df8bae1dSRodney W. Grimes 
919df8bae1dSRodney W. Grimes 	/*
920df8bae1dSRodney W. Grimes 	 * If first fragment to arrive, create a reassembly queue.
921df8bae1dSRodney W. Grimes 	 */
922042bbfa3SRobert Watson 	if (fp == NULL) {
923603724d3SBjoern A. Zeeb 		fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
924d248c7d7SRobert Watson 		if (fp == NULL)
925df8bae1dSRodney W. Grimes 			goto dropfrag;
92636b0360bSRobert Watson #ifdef MAC
92730d239bcSRobert Watson 		if (mac_ipq_init(fp, M_NOWAIT) != 0) {
928603724d3SBjoern A. Zeeb 			uma_zfree(V_ipq_zone, fp);
9291d7d0bfeSPawel Jakub Dawidek 			fp = NULL;
9305e7ce478SRobert Watson 			goto dropfrag;
9315e7ce478SRobert Watson 		}
93230d239bcSRobert Watson 		mac_ipq_create(m, fp);
93336b0360bSRobert Watson #endif
934462b86feSPoul-Henning Kamp 		TAILQ_INSERT_HEAD(head, fp, ipq_list);
935603724d3SBjoern A. Zeeb 		V_nipq++;
936375386e2SMike Silbersack 		fp->ipq_nfrags = 1;
937df8bae1dSRodney W. Grimes 		fp->ipq_ttl = IPFRAGTTL;
938df8bae1dSRodney W. Grimes 		fp->ipq_p = ip->ip_p;
939df8bae1dSRodney W. Grimes 		fp->ipq_id = ip->ip_id;
9406effc713SDoug Rabson 		fp->ipq_src = ip->ip_src;
9416effc713SDoug Rabson 		fp->ipq_dst = ip->ip_dst;
942af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
943af38c68cSLuigi Rizzo 		m->m_nextpkt = NULL;
944800af1fbSMaxim Konovalov 		goto done;
94536b0360bSRobert Watson 	} else {
946375386e2SMike Silbersack 		fp->ipq_nfrags++;
94736b0360bSRobert Watson #ifdef MAC
94830d239bcSRobert Watson 		mac_ipq_update(m, fp);
94936b0360bSRobert Watson #endif
950df8bae1dSRodney W. Grimes 	}
951df8bae1dSRodney W. Grimes 
9526effc713SDoug Rabson #define GETIP(m)	((struct ip*)((m)->m_pkthdr.header))
9536effc713SDoug Rabson 
954df8bae1dSRodney W. Grimes 	/*
95559dfcba4SHajimu UMEMOTO 	 * Handle ECN by comparing this segment with the first one;
95659dfcba4SHajimu UMEMOTO 	 * if CE is set, do not lose CE.
95759dfcba4SHajimu UMEMOTO 	 * drop if CE and not-ECT are mixed for the same packet.
95859dfcba4SHajimu UMEMOTO 	 */
95959dfcba4SHajimu UMEMOTO 	ecn = ip->ip_tos & IPTOS_ECN_MASK;
96059dfcba4SHajimu UMEMOTO 	ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
96159dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_CE) {
96259dfcba4SHajimu UMEMOTO 		if (ecn0 == IPTOS_ECN_NOTECT)
96359dfcba4SHajimu UMEMOTO 			goto dropfrag;
96459dfcba4SHajimu UMEMOTO 		if (ecn0 != IPTOS_ECN_CE)
96559dfcba4SHajimu UMEMOTO 			GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
96659dfcba4SHajimu UMEMOTO 	}
96759dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
96859dfcba4SHajimu UMEMOTO 		goto dropfrag;
96959dfcba4SHajimu UMEMOTO 
97059dfcba4SHajimu UMEMOTO 	/*
971df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
972df8bae1dSRodney W. Grimes 	 */
9736effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
9746effc713SDoug Rabson 		if (GETIP(q)->ip_off > ip->ip_off)
975df8bae1dSRodney W. Grimes 			break;
976df8bae1dSRodney W. Grimes 
977df8bae1dSRodney W. Grimes 	/*
978df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
979df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
980af38c68cSLuigi Rizzo 	 * segment.  If it provides all of our data, drop us, otherwise
981af38c68cSLuigi Rizzo 	 * stick new segment in the proper place.
982db4f9cc7SJonathan Lemon 	 *
983db4f9cc7SJonathan Lemon 	 * If some of the data is dropped from the the preceding
984db4f9cc7SJonathan Lemon 	 * segment, then it's checksum is invalidated.
985df8bae1dSRodney W. Grimes 	 */
9866effc713SDoug Rabson 	if (p) {
9876effc713SDoug Rabson 		i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
988df8bae1dSRodney W. Grimes 		if (i > 0) {
989df8bae1dSRodney W. Grimes 			if (i >= ip->ip_len)
990df8bae1dSRodney W. Grimes 				goto dropfrag;
9916a800098SYoshinobu Inoue 			m_adj(m, i);
992db4f9cc7SJonathan Lemon 			m->m_pkthdr.csum_flags = 0;
993df8bae1dSRodney W. Grimes 			ip->ip_off += i;
994df8bae1dSRodney W. Grimes 			ip->ip_len -= i;
995df8bae1dSRodney W. Grimes 		}
996af38c68cSLuigi Rizzo 		m->m_nextpkt = p->m_nextpkt;
997af38c68cSLuigi Rizzo 		p->m_nextpkt = m;
998af38c68cSLuigi Rizzo 	} else {
999af38c68cSLuigi Rizzo 		m->m_nextpkt = fp->ipq_frags;
1000af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
1001df8bae1dSRodney W. Grimes 	}
1002df8bae1dSRodney W. Grimes 
1003df8bae1dSRodney W. Grimes 	/*
1004df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
1005df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
1006df8bae1dSRodney W. Grimes 	 */
10076effc713SDoug Rabson 	for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
1008af38c68cSLuigi Rizzo 	     q = nq) {
1009b36f5b37SMaxim Konovalov 		i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
10106effc713SDoug Rabson 		if (i < GETIP(q)->ip_len) {
10116effc713SDoug Rabson 			GETIP(q)->ip_len -= i;
10126effc713SDoug Rabson 			GETIP(q)->ip_off += i;
10136effc713SDoug Rabson 			m_adj(q, i);
1014db4f9cc7SJonathan Lemon 			q->m_pkthdr.csum_flags = 0;
1015df8bae1dSRodney W. Grimes 			break;
1016df8bae1dSRodney W. Grimes 		}
10176effc713SDoug Rabson 		nq = q->m_nextpkt;
1018af38c68cSLuigi Rizzo 		m->m_nextpkt = nq;
101986425c62SRobert Watson 		IPSTAT_INC(ips_fragdropped);
1020375386e2SMike Silbersack 		fp->ipq_nfrags--;
10216effc713SDoug Rabson 		m_freem(q);
1022df8bae1dSRodney W. Grimes 	}
1023df8bae1dSRodney W. Grimes 
1024df8bae1dSRodney W. Grimes 	/*
1025375386e2SMike Silbersack 	 * Check for complete reassembly and perform frag per packet
1026375386e2SMike Silbersack 	 * limiting.
1027375386e2SMike Silbersack 	 *
1028375386e2SMike Silbersack 	 * Frag limiting is performed here so that the nth frag has
1029375386e2SMike Silbersack 	 * a chance to complete the packet before we drop the packet.
1030375386e2SMike Silbersack 	 * As a result, n+1 frags are actually allowed per packet, but
1031375386e2SMike Silbersack 	 * only n will ever be stored. (n = maxfragsperpacket.)
1032375386e2SMike Silbersack 	 *
1033df8bae1dSRodney W. Grimes 	 */
10346effc713SDoug Rabson 	next = 0;
10356effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
1036375386e2SMike Silbersack 		if (GETIP(q)->ip_off != next) {
1037603724d3SBjoern A. Zeeb 			if (fp->ipq_nfrags > V_maxfragsperpacket) {
103886425c62SRobert Watson 				IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1039375386e2SMike Silbersack 				ip_freef(head, fp);
104099e8617dSMaxim Konovalov 			}
1041f0cada84SAndre Oppermann 			goto done;
1042375386e2SMike Silbersack 		}
10436effc713SDoug Rabson 		next += GETIP(q)->ip_len;
10446effc713SDoug Rabson 	}
10456effc713SDoug Rabson 	/* Make sure the last packet didn't have the IP_MF flag */
1046375386e2SMike Silbersack 	if (p->m_flags & M_FRAG) {
1047603724d3SBjoern A. Zeeb 		if (fp->ipq_nfrags > V_maxfragsperpacket) {
104886425c62SRobert Watson 			IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1049375386e2SMike Silbersack 			ip_freef(head, fp);
105099e8617dSMaxim Konovalov 		}
1051f0cada84SAndre Oppermann 		goto done;
1052375386e2SMike Silbersack 	}
1053df8bae1dSRodney W. Grimes 
1054df8bae1dSRodney W. Grimes 	/*
1055430d30d8SBill Fenner 	 * Reassembly is complete.  Make sure the packet is a sane size.
1056430d30d8SBill Fenner 	 */
10576effc713SDoug Rabson 	q = fp->ipq_frags;
10586effc713SDoug Rabson 	ip = GETIP(q);
105953be11f6SPoul-Henning Kamp 	if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
106086425c62SRobert Watson 		IPSTAT_INC(ips_toolong);
106186425c62SRobert Watson 		IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1062462b86feSPoul-Henning Kamp 		ip_freef(head, fp);
1063f0cada84SAndre Oppermann 		goto done;
1064430d30d8SBill Fenner 	}
1065430d30d8SBill Fenner 
1066430d30d8SBill Fenner 	/*
1067430d30d8SBill Fenner 	 * Concatenate fragments.
1068df8bae1dSRodney W. Grimes 	 */
10696effc713SDoug Rabson 	m = q;
1070df8bae1dSRodney W. Grimes 	t = m->m_next;
107102410549SRobert Watson 	m->m_next = NULL;
1072df8bae1dSRodney W. Grimes 	m_cat(m, t);
10736effc713SDoug Rabson 	nq = q->m_nextpkt;
107402410549SRobert Watson 	q->m_nextpkt = NULL;
10756effc713SDoug Rabson 	for (q = nq; q != NULL; q = nq) {
10766effc713SDoug Rabson 		nq = q->m_nextpkt;
1077945aa40dSDoug Rabson 		q->m_nextpkt = NULL;
1078db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1079db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1080a8db1d93SJonathan Lemon 		m_cat(m, q);
1081df8bae1dSRodney W. Grimes 	}
10826edb555dSOleg Bulyzhin 	/*
10836edb555dSOleg Bulyzhin 	 * In order to do checksumming faster we do 'end-around carry' here
10846edb555dSOleg Bulyzhin 	 * (and not in for{} loop), though it implies we are not going to
10856edb555dSOleg Bulyzhin 	 * reassemble more than 64k fragments.
10866edb555dSOleg Bulyzhin 	 */
10876edb555dSOleg Bulyzhin 	m->m_pkthdr.csum_data =
10886edb555dSOleg Bulyzhin 	    (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16);
108936b0360bSRobert Watson #ifdef MAC
109030d239bcSRobert Watson 	mac_ipq_reassemble(fp, m);
109130d239bcSRobert Watson 	mac_ipq_destroy(fp);
109236b0360bSRobert Watson #endif
1093df8bae1dSRodney W. Grimes 
1094df8bae1dSRodney W. Grimes 	/*
1095f0cada84SAndre Oppermann 	 * Create header for new ip packet by modifying header of first
1096f0cada84SAndre Oppermann 	 * packet;  dequeue and discard fragment reassembly header.
1097df8bae1dSRodney W. Grimes 	 * Make header visible.
1098df8bae1dSRodney W. Grimes 	 */
1099f0cada84SAndre Oppermann 	ip->ip_len = (ip->ip_hl << 2) + next;
11006effc713SDoug Rabson 	ip->ip_src = fp->ipq_src;
11016effc713SDoug Rabson 	ip->ip_dst = fp->ipq_dst;
1102462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(head, fp, ipq_list);
1103603724d3SBjoern A. Zeeb 	V_nipq--;
1104603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
110553be11f6SPoul-Henning Kamp 	m->m_len += (ip->ip_hl << 2);
110653be11f6SPoul-Henning Kamp 	m->m_data -= (ip->ip_hl << 2);
1107df8bae1dSRodney W. Grimes 	/* some debugging cruft by sklower, below, will go away soon */
1108a5554bf0SPoul-Henning Kamp 	if (m->m_flags & M_PKTHDR)	/* XXX this should be done elsewhere */
1109a5554bf0SPoul-Henning Kamp 		m_fixhdr(m);
111086425c62SRobert Watson 	IPSTAT_INC(ips_reassembled);
1111f0cada84SAndre Oppermann 	IPQ_UNLOCK();
11126a800098SYoshinobu Inoue 	return (m);
1113df8bae1dSRodney W. Grimes 
1114df8bae1dSRodney W. Grimes dropfrag:
111586425c62SRobert Watson 	IPSTAT_INC(ips_fragdropped);
1116042bbfa3SRobert Watson 	if (fp != NULL)
1117375386e2SMike Silbersack 		fp->ipq_nfrags--;
1118df8bae1dSRodney W. Grimes 	m_freem(m);
1119f0cada84SAndre Oppermann done:
1120f0cada84SAndre Oppermann 	IPQ_UNLOCK();
1121f0cada84SAndre Oppermann 	return (NULL);
11226effc713SDoug Rabson 
11236effc713SDoug Rabson #undef GETIP
1124df8bae1dSRodney W. Grimes }
1125df8bae1dSRodney W. Grimes 
1126df8bae1dSRodney W. Grimes /*
1127df8bae1dSRodney W. Grimes  * Free a fragment reassembly header and all
1128df8bae1dSRodney W. Grimes  * associated datagrams.
1129df8bae1dSRodney W. Grimes  */
11300312fbe9SPoul-Henning Kamp static void
1131f2565d68SRobert Watson ip_freef(struct ipqhead *fhp, struct ipq *fp)
1132df8bae1dSRodney W. Grimes {
11338b615593SMarko Zec 	INIT_VNET_INET(curvnet);
1134f2565d68SRobert Watson 	struct mbuf *q;
1135df8bae1dSRodney W. Grimes 
11362fad1e93SSam Leffler 	IPQ_LOCK_ASSERT();
11372fad1e93SSam Leffler 
11386effc713SDoug Rabson 	while (fp->ipq_frags) {
11396effc713SDoug Rabson 		q = fp->ipq_frags;
11406effc713SDoug Rabson 		fp->ipq_frags = q->m_nextpkt;
11416effc713SDoug Rabson 		m_freem(q);
1142df8bae1dSRodney W. Grimes 	}
1143462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(fhp, fp, ipq_list);
1144603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
1145603724d3SBjoern A. Zeeb 	V_nipq--;
1146df8bae1dSRodney W. Grimes }
1147df8bae1dSRodney W. Grimes 
1148df8bae1dSRodney W. Grimes /*
1149df8bae1dSRodney W. Grimes  * IP timer processing;
1150df8bae1dSRodney W. Grimes  * if a timer expires on a reassembly
1151df8bae1dSRodney W. Grimes  * queue, discard it.
1152df8bae1dSRodney W. Grimes  */
1153df8bae1dSRodney W. Grimes void
1154f2565d68SRobert Watson ip_slowtimo(void)
1155df8bae1dSRodney W. Grimes {
11568b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1157f2565d68SRobert Watson 	struct ipq *fp;
1158194a213eSAndrey A. Chernov 	int i;
1159df8bae1dSRodney W. Grimes 
11602fad1e93SSam Leffler 	IPQ_LOCK();
11618b615593SMarko Zec 	VNET_LIST_RLOCK();
11628b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
11638b615593SMarko Zec 		CURVNET_SET(vnet_iter);
11648b615593SMarko Zec 		INIT_VNET_INET(vnet_iter);
1165194a213eSAndrey A. Chernov 		for (i = 0; i < IPREASS_NHASH; i++) {
1166603724d3SBjoern A. Zeeb 			for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) {
1167462b86feSPoul-Henning Kamp 				struct ipq *fpp;
1168462b86feSPoul-Henning Kamp 
1169462b86feSPoul-Henning Kamp 				fpp = fp;
1170462b86feSPoul-Henning Kamp 				fp = TAILQ_NEXT(fp, ipq_list);
1171462b86feSPoul-Henning Kamp 				if(--fpp->ipq_ttl == 0) {
117286425c62SRobert Watson 					IPSTAT_ADD(ips_fragtimeout,
117386425c62SRobert Watson 					    fpp->ipq_nfrags);
1174603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], fpp);
1175df8bae1dSRodney W. Grimes 				}
1176df8bae1dSRodney W. Grimes 			}
1177194a213eSAndrey A. Chernov 		}
1178690a6055SJesper Skriver 		/*
1179690a6055SJesper Skriver 		 * If we are over the maximum number of fragments
1180690a6055SJesper Skriver 		 * (due to the limit being lowered), drain off
1181690a6055SJesper Skriver 		 * enough to get down to the new limit.
1182690a6055SJesper Skriver 		 */
1183603724d3SBjoern A. Zeeb 		if (V_maxnipq >= 0 && V_nipq > V_maxnipq) {
1184690a6055SJesper Skriver 			for (i = 0; i < IPREASS_NHASH; i++) {
11858b615593SMarko Zec 				while (V_nipq > V_maxnipq &&
11868b615593SMarko Zec 				    !TAILQ_EMPTY(&V_ipq[i])) {
118786425c62SRobert Watson 					IPSTAT_ADD(ips_fragdropped,
118886425c62SRobert Watson 					    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
11898b615593SMarko Zec 					ip_freef(&V_ipq[i],
11908b615593SMarko Zec 					    TAILQ_FIRST(&V_ipq[i]));
1191690a6055SJesper Skriver 				}
1192690a6055SJesper Skriver 			}
1193690a6055SJesper Skriver 		}
11948b615593SMarko Zec 		CURVNET_RESTORE();
11958b615593SMarko Zec 	}
11968b615593SMarko Zec 	VNET_LIST_RUNLOCK();
11972fad1e93SSam Leffler 	IPQ_UNLOCK();
1198df8bae1dSRodney W. Grimes }
1199df8bae1dSRodney W. Grimes 
1200df8bae1dSRodney W. Grimes /*
1201df8bae1dSRodney W. Grimes  * Drain off all datagram fragments.
1202df8bae1dSRodney W. Grimes  */
1203df8bae1dSRodney W. Grimes void
1204f2565d68SRobert Watson ip_drain(void)
1205df8bae1dSRodney W. Grimes {
12068b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1207194a213eSAndrey A. Chernov 	int     i;
1208ce29ab3aSGarrett Wollman 
12092fad1e93SSam Leffler 	IPQ_LOCK();
12108b615593SMarko Zec 	VNET_LIST_RLOCK();
12118b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
12128b615593SMarko Zec 		CURVNET_SET(vnet_iter);
12138b615593SMarko Zec 		INIT_VNET_INET(vnet_iter);
1214194a213eSAndrey A. Chernov 		for (i = 0; i < IPREASS_NHASH; i++) {
1215603724d3SBjoern A. Zeeb 			while(!TAILQ_EMPTY(&V_ipq[i])) {
121686425c62SRobert Watson 				IPSTAT_ADD(ips_fragdropped,
121786425c62SRobert Watson 				    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
1218603724d3SBjoern A. Zeeb 				ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1219194a213eSAndrey A. Chernov 			}
1220194a213eSAndrey A. Chernov 		}
12218b615593SMarko Zec 		CURVNET_RESTORE();
12228b615593SMarko Zec 	}
12238b615593SMarko Zec 	VNET_LIST_RUNLOCK();
12242fad1e93SSam Leffler 	IPQ_UNLOCK();
1225ce29ab3aSGarrett Wollman 	in_rtqdrain();
1226df8bae1dSRodney W. Grimes }
1227df8bae1dSRodney W. Grimes 
1228df8bae1dSRodney W. Grimes /*
1229de38924dSAndre Oppermann  * The protocol to be inserted into ip_protox[] must be already registered
1230de38924dSAndre Oppermann  * in inetsw[], either statically or through pf_proto_register().
1231de38924dSAndre Oppermann  */
1232de38924dSAndre Oppermann int
1233de38924dSAndre Oppermann ipproto_register(u_char ipproto)
1234de38924dSAndre Oppermann {
1235de38924dSAndre Oppermann 	struct protosw *pr;
1236de38924dSAndre Oppermann 
1237de38924dSAndre Oppermann 	/* Sanity checks. */
1238de38924dSAndre Oppermann 	if (ipproto == 0)
1239de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1240de38924dSAndre Oppermann 
1241de38924dSAndre Oppermann 	/*
1242de38924dSAndre Oppermann 	 * The protocol slot must not be occupied by another protocol
1243de38924dSAndre Oppermann 	 * already.  An index pointing to IPPROTO_RAW is unused.
1244de38924dSAndre Oppermann 	 */
1245de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1246de38924dSAndre Oppermann 	if (pr == NULL)
1247de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1248de38924dSAndre Oppermann 	if (ip_protox[ipproto] != pr - inetsw)	/* IPPROTO_RAW */
1249de38924dSAndre Oppermann 		return (EEXIST);
1250de38924dSAndre Oppermann 
1251de38924dSAndre Oppermann 	/* Find the protocol position in inetsw[] and set the index. */
1252de38924dSAndre Oppermann 	for (pr = inetdomain.dom_protosw;
1253de38924dSAndre Oppermann 	     pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1254de38924dSAndre Oppermann 		if (pr->pr_domain->dom_family == PF_INET &&
1255de38924dSAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol == ipproto) {
1256de38924dSAndre Oppermann 			/* Be careful to only index valid IP protocols. */
1257db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX) {
1258de38924dSAndre Oppermann 				ip_protox[pr->pr_protocol] = pr - inetsw;
1259de38924dSAndre Oppermann 				return (0);
1260de38924dSAndre Oppermann 			} else
1261de38924dSAndre Oppermann 				return (EINVAL);
1262de38924dSAndre Oppermann 		}
1263de38924dSAndre Oppermann 	}
1264de38924dSAndre Oppermann 	return (EPROTONOSUPPORT);
1265de38924dSAndre Oppermann }
1266de38924dSAndre Oppermann 
1267de38924dSAndre Oppermann int
1268de38924dSAndre Oppermann ipproto_unregister(u_char ipproto)
1269de38924dSAndre Oppermann {
1270de38924dSAndre Oppermann 	struct protosw *pr;
1271de38924dSAndre Oppermann 
1272de38924dSAndre Oppermann 	/* Sanity checks. */
1273de38924dSAndre Oppermann 	if (ipproto == 0)
1274de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1275de38924dSAndre Oppermann 
1276de38924dSAndre Oppermann 	/* Check if the protocol was indeed registered. */
1277de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1278de38924dSAndre Oppermann 	if (pr == NULL)
1279de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1280de38924dSAndre Oppermann 	if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
1281de38924dSAndre Oppermann 		return (ENOENT);
1282de38924dSAndre Oppermann 
1283de38924dSAndre Oppermann 	/* Reset the protocol slot to IPPROTO_RAW. */
1284de38924dSAndre Oppermann 	ip_protox[ipproto] = pr - inetsw;
1285de38924dSAndre Oppermann 	return (0);
1286de38924dSAndre Oppermann }
1287de38924dSAndre Oppermann 
1288df8bae1dSRodney W. Grimes /*
1289df8bae1dSRodney W. Grimes  * Given address of next destination (final or next hop),
1290df8bae1dSRodney W. Grimes  * return internet address info of interface to be used to get there.
1291df8bae1dSRodney W. Grimes  */
1292bd714208SRuslan Ermilov struct in_ifaddr *
12938b07e49aSJulian Elischer ip_rtaddr(struct in_addr dst, u_int fibnum)
1294df8bae1dSRodney W. Grimes {
129597d8d152SAndre Oppermann 	struct route sro;
129602c1c707SAndre Oppermann 	struct sockaddr_in *sin;
129702c1c707SAndre Oppermann 	struct in_ifaddr *ifa;
1298df8bae1dSRodney W. Grimes 
12990cfbbe3bSAndre Oppermann 	bzero(&sro, sizeof(sro));
130097d8d152SAndre Oppermann 	sin = (struct sockaddr_in *)&sro.ro_dst;
1301df8bae1dSRodney W. Grimes 	sin->sin_family = AF_INET;
1302df8bae1dSRodney W. Grimes 	sin->sin_len = sizeof(*sin);
1303df8bae1dSRodney W. Grimes 	sin->sin_addr = dst;
13046e6b3f7cSQing Li 	in_rtalloc_ign(&sro, 0, fibnum);
1305df8bae1dSRodney W. Grimes 
130697d8d152SAndre Oppermann 	if (sro.ro_rt == NULL)
130702410549SRobert Watson 		return (NULL);
130802c1c707SAndre Oppermann 
130997d8d152SAndre Oppermann 	ifa = ifatoia(sro.ro_rt->rt_ifa);
131097d8d152SAndre Oppermann 	RTFREE(sro.ro_rt);
131102410549SRobert Watson 	return (ifa);
1312df8bae1dSRodney W. Grimes }
1313df8bae1dSRodney W. Grimes 
1314df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = {
1315df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1316df8bae1dSRodney W. Grimes 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1317df8bae1dSRodney W. Grimes 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1318df8bae1dSRodney W. Grimes 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1319fcaf9f91SMike Silbersack 	0,		0,		EHOSTUNREACH,	0,
13203b8123b7SJesper Skriver 	ENOPROTOOPT,	ECONNREFUSED
1321df8bae1dSRodney W. Grimes };
1322df8bae1dSRodney W. Grimes 
1323df8bae1dSRodney W. Grimes /*
1324df8bae1dSRodney W. Grimes  * Forward a packet.  If some error occurs return the sender
1325df8bae1dSRodney W. Grimes  * an icmp packet.  Note we can't always generate a meaningful
1326df8bae1dSRodney W. Grimes  * icmp message because icmp doesn't have a large enough repertoire
1327df8bae1dSRodney W. Grimes  * of codes and types.
1328df8bae1dSRodney W. Grimes  *
1329df8bae1dSRodney W. Grimes  * If not forwarding, just drop the packet.  This could be confusing
1330df8bae1dSRodney W. Grimes  * if ipforwarding was zero but some routing protocol was advancing
1331df8bae1dSRodney W. Grimes  * us as a gateway to somewhere.  However, we must let the routing
1332df8bae1dSRodney W. Grimes  * protocol deal with that.
1333df8bae1dSRodney W. Grimes  *
1334df8bae1dSRodney W. Grimes  * The srcrt parameter indicates whether the packet is being forwarded
1335df8bae1dSRodney W. Grimes  * via a source route.
1336df8bae1dSRodney W. Grimes  */
13379b932e9eSAndre Oppermann void
13389b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt)
1339df8bae1dSRodney W. Grimes {
13408b615593SMarko Zec 	INIT_VNET_INET(curvnet);
13412b25acc1SLuigi Rizzo 	struct ip *ip = mtod(m, struct ip *);
13429b932e9eSAndre Oppermann 	struct in_ifaddr *ia = NULL;
1343df8bae1dSRodney W. Grimes 	struct mbuf *mcopy;
13449b932e9eSAndre Oppermann 	struct in_addr dest;
1345b835b6feSBjoern A. Zeeb 	struct route ro;
1346c773494eSAndre Oppermann 	int error, type = 0, code = 0, mtu = 0;
13473efc3014SJulian Elischer 
13489b932e9eSAndre Oppermann 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
134986425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
1350df8bae1dSRodney W. Grimes 		m_freem(m);
1351df8bae1dSRodney W. Grimes 		return;
1352df8bae1dSRodney W. Grimes 	}
13531b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
1354603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
13551b968362SDag-Erling Smørgrav #endif
1356df8bae1dSRodney W. Grimes 		if (ip->ip_ttl <= IPTTLDEC) {
13571b968362SDag-Erling Smørgrav 			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
135802c1c707SAndre Oppermann 			    0, 0);
1359df8bae1dSRodney W. Grimes 			return;
1360df8bae1dSRodney W. Grimes 		}
13611b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
13621b968362SDag-Erling Smørgrav 	}
13631b968362SDag-Erling Smørgrav #endif
1364df8bae1dSRodney W. Grimes 
13658b07e49aSJulian Elischer 	ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
1366d23d475fSGuido van Rooij 	if (!srcrt && ia == NULL) {
136702c1c707SAndre Oppermann 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1368df8bae1dSRodney W. Grimes 		return;
136902c1c707SAndre Oppermann 	}
1370df8bae1dSRodney W. Grimes 
1371df8bae1dSRodney W. Grimes 	/*
1372bfef7ed4SIan Dowse 	 * Save the IP header and at most 8 bytes of the payload,
1373bfef7ed4SIan Dowse 	 * in case we need to generate an ICMP message to the src.
1374bfef7ed4SIan Dowse 	 *
13754d2e3692SLuigi Rizzo 	 * XXX this can be optimized a lot by saving the data in a local
13764d2e3692SLuigi Rizzo 	 * buffer on the stack (72 bytes at most), and only allocating the
13774d2e3692SLuigi Rizzo 	 * mbuf if really necessary. The vast majority of the packets
13784d2e3692SLuigi Rizzo 	 * are forwarded without having to send an ICMP back (either
13794d2e3692SLuigi Rizzo 	 * because unnecessary, or because rate limited), so we are
13804d2e3692SLuigi Rizzo 	 * really we are wasting a lot of work here.
13814d2e3692SLuigi Rizzo 	 *
1382bfef7ed4SIan Dowse 	 * We don't use m_copy() because it might return a reference
1383bfef7ed4SIan Dowse 	 * to a shared cluster. Both this function and ip_output()
1384bfef7ed4SIan Dowse 	 * assume exclusive access to the IP header in `m', so any
1385bfef7ed4SIan Dowse 	 * data in a cluster may change before we reach icmp_error().
1386df8bae1dSRodney W. Grimes 	 */
1387780b2f69SAndre Oppermann 	MGETHDR(mcopy, M_DONTWAIT, m->m_type);
1388a163d034SWarner Losh 	if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) {
13899967cafcSSam Leffler 		/*
13909967cafcSSam Leffler 		 * It's probably ok if the pkthdr dup fails (because
13919967cafcSSam Leffler 		 * the deep copy of the tag chain failed), but for now
13929967cafcSSam Leffler 		 * be conservative and just discard the copy since
13939967cafcSSam Leffler 		 * code below may some day want the tags.
13949967cafcSSam Leffler 		 */
13959967cafcSSam Leffler 		m_free(mcopy);
13969967cafcSSam Leffler 		mcopy = NULL;
13979967cafcSSam Leffler 	}
1398bfef7ed4SIan Dowse 	if (mcopy != NULL) {
1399780b2f69SAndre Oppermann 		mcopy->m_len = min(ip->ip_len, M_TRAILINGSPACE(mcopy));
1400e6b0a570SBruce M Simpson 		mcopy->m_pkthdr.len = mcopy->m_len;
1401bfef7ed4SIan Dowse 		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1402bfef7ed4SIan Dowse 	}
140304287599SRuslan Ermilov 
140404287599SRuslan Ermilov #ifdef IPSTEALTH
1405603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
140604287599SRuslan Ermilov #endif
140704287599SRuslan Ermilov 		ip->ip_ttl -= IPTTLDEC;
140804287599SRuslan Ermilov #ifdef IPSTEALTH
140904287599SRuslan Ermilov 	}
141004287599SRuslan Ermilov #endif
1411df8bae1dSRodney W. Grimes 
1412df8bae1dSRodney W. Grimes 	/*
1413df8bae1dSRodney W. Grimes 	 * If forwarding packet using same interface that it came in on,
1414df8bae1dSRodney W. Grimes 	 * perhaps should send a redirect to sender to shortcut a hop.
1415df8bae1dSRodney W. Grimes 	 * Only send redirect if source is sending directly to us,
1416df8bae1dSRodney W. Grimes 	 * and if packet was not source routed (or has any options).
1417df8bae1dSRodney W. Grimes 	 * Also, don't send redirect if forwarding using a default route
1418df8bae1dSRodney W. Grimes 	 * or a route modified by a redirect.
1419df8bae1dSRodney W. Grimes 	 */
14209b932e9eSAndre Oppermann 	dest.s_addr = 0;
1421603724d3SBjoern A. Zeeb 	if (!srcrt && V_ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) {
142202c1c707SAndre Oppermann 		struct sockaddr_in *sin;
142302c1c707SAndre Oppermann 		struct rtentry *rt;
142402c1c707SAndre Oppermann 
14250cfbbe3bSAndre Oppermann 		bzero(&ro, sizeof(ro));
142602c1c707SAndre Oppermann 		sin = (struct sockaddr_in *)&ro.ro_dst;
142702c1c707SAndre Oppermann 		sin->sin_family = AF_INET;
142802c1c707SAndre Oppermann 		sin->sin_len = sizeof(*sin);
14299b932e9eSAndre Oppermann 		sin->sin_addr = ip->ip_dst;
14306e6b3f7cSQing Li 		in_rtalloc_ign(&ro, 0, M_GETFIB(m));
143102c1c707SAndre Oppermann 
143202c1c707SAndre Oppermann 		rt = ro.ro_rt;
143302c1c707SAndre Oppermann 
143402c1c707SAndre Oppermann 		if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
14359b932e9eSAndre Oppermann 		    satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1436df8bae1dSRodney W. Grimes #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1437df8bae1dSRodney W. Grimes 			u_long src = ntohl(ip->ip_src.s_addr);
1438df8bae1dSRodney W. Grimes 
1439df8bae1dSRodney W. Grimes 			if (RTA(rt) &&
1440df8bae1dSRodney W. Grimes 			    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1441df8bae1dSRodney W. Grimes 				if (rt->rt_flags & RTF_GATEWAY)
14429b932e9eSAndre Oppermann 					dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1443df8bae1dSRodney W. Grimes 				else
14449b932e9eSAndre Oppermann 					dest.s_addr = ip->ip_dst.s_addr;
1445df8bae1dSRodney W. Grimes 				/* Router requirements says to only send host redirects */
1446df8bae1dSRodney W. Grimes 				type = ICMP_REDIRECT;
1447df8bae1dSRodney W. Grimes 				code = ICMP_REDIRECT_HOST;
1448df8bae1dSRodney W. Grimes 			}
1449df8bae1dSRodney W. Grimes 		}
145002c1c707SAndre Oppermann 		if (rt)
145102c1c707SAndre Oppermann 			RTFREE(rt);
145202c1c707SAndre Oppermann 	}
1453df8bae1dSRodney W. Grimes 
1454b835b6feSBjoern A. Zeeb 	/*
1455b835b6feSBjoern A. Zeeb 	 * Try to cache the route MTU from ip_output so we can consider it for
1456b835b6feSBjoern A. Zeeb 	 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191.
1457b835b6feSBjoern A. Zeeb 	 */
1458b835b6feSBjoern A. Zeeb 	bzero(&ro, sizeof(ro));
1459b835b6feSBjoern A. Zeeb 
1460b835b6feSBjoern A. Zeeb 	error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1461b835b6feSBjoern A. Zeeb 
1462b835b6feSBjoern A. Zeeb 	if (error == EMSGSIZE && ro.ro_rt)
1463b835b6feSBjoern A. Zeeb 		mtu = ro.ro_rt->rt_rmx.rmx_mtu;
1464b835b6feSBjoern A. Zeeb 	if (ro.ro_rt)
1465b835b6feSBjoern A. Zeeb 		RTFREE(ro.ro_rt);
1466b835b6feSBjoern A. Zeeb 
1467df8bae1dSRodney W. Grimes 	if (error)
146886425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
1469df8bae1dSRodney W. Grimes 	else {
147086425c62SRobert Watson 		IPSTAT_INC(ips_forward);
1471df8bae1dSRodney W. Grimes 		if (type)
147286425c62SRobert Watson 			IPSTAT_INC(ips_redirectsent);
1473df8bae1dSRodney W. Grimes 		else {
14749188b4a1SAndre Oppermann 			if (mcopy)
1475df8bae1dSRodney W. Grimes 				m_freem(mcopy);
1476df8bae1dSRodney W. Grimes 			return;
1477df8bae1dSRodney W. Grimes 		}
1478df8bae1dSRodney W. Grimes 	}
1479df8bae1dSRodney W. Grimes 	if (mcopy == NULL)
1480df8bae1dSRodney W. Grimes 		return;
1481df8bae1dSRodney W. Grimes 
1482df8bae1dSRodney W. Grimes 	switch (error) {
1483df8bae1dSRodney W. Grimes 
1484df8bae1dSRodney W. Grimes 	case 0:				/* forwarded, but need redirect */
1485df8bae1dSRodney W. Grimes 		/* type, code set above */
1486df8bae1dSRodney W. Grimes 		break;
1487df8bae1dSRodney W. Grimes 
1488df8bae1dSRodney W. Grimes 	case ENETUNREACH:		/* shouldn't happen, checked above */
1489df8bae1dSRodney W. Grimes 	case EHOSTUNREACH:
1490df8bae1dSRodney W. Grimes 	case ENETDOWN:
1491df8bae1dSRodney W. Grimes 	case EHOSTDOWN:
1492df8bae1dSRodney W. Grimes 	default:
1493df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1494df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_HOST;
1495df8bae1dSRodney W. Grimes 		break;
1496df8bae1dSRodney W. Grimes 
1497df8bae1dSRodney W. Grimes 	case EMSGSIZE:
1498df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1499df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_NEEDFRAG;
15001dfcf0d2SAndre Oppermann 
1501b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
1502b835b6feSBjoern A. Zeeb 		/*
1503b835b6feSBjoern A. Zeeb 		 * If IPsec is configured for this path,
1504b835b6feSBjoern A. Zeeb 		 * override any possibly mtu value set by ip_output.
1505b835b6feSBjoern A. Zeeb 		 */
1506b835b6feSBjoern A. Zeeb 		mtu = ip_ipsec_mtu(m, mtu);
1507b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
15089b932e9eSAndre Oppermann 		/*
1509b835b6feSBjoern A. Zeeb 		 * If the MTU was set before make sure we are below the
1510b835b6feSBjoern A. Zeeb 		 * interface MTU.
1511ab48768bSAndre Oppermann 		 * If the MTU wasn't set before use the interface mtu or
1512ab48768bSAndre Oppermann 		 * fall back to the next smaller mtu step compared to the
1513ab48768bSAndre Oppermann 		 * current packet size.
15149b932e9eSAndre Oppermann 		 */
1515b835b6feSBjoern A. Zeeb 		if (mtu != 0) {
1516b835b6feSBjoern A. Zeeb 			if (ia != NULL)
1517b835b6feSBjoern A. Zeeb 				mtu = min(mtu, ia->ia_ifp->if_mtu);
1518b835b6feSBjoern A. Zeeb 		} else {
1519ab48768bSAndre Oppermann 			if (ia != NULL)
1520c773494eSAndre Oppermann 				mtu = ia->ia_ifp->if_mtu;
1521ab48768bSAndre Oppermann 			else
1522ab48768bSAndre Oppermann 				mtu = ip_next_mtu(ip->ip_len, 0);
1523ab48768bSAndre Oppermann 		}
152486425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
1525df8bae1dSRodney W. Grimes 		break;
1526df8bae1dSRodney W. Grimes 
1527df8bae1dSRodney W. Grimes 	case ENOBUFS:
1528df285b3dSMike Silbersack 		/*
1529df285b3dSMike Silbersack 		 * A router should not generate ICMP_SOURCEQUENCH as
1530df285b3dSMike Silbersack 		 * required in RFC1812 Requirements for IP Version 4 Routers.
1531df285b3dSMike Silbersack 		 * Source quench could be a big problem under DoS attacks,
1532df285b3dSMike Silbersack 		 * or if the underlying interface is rate-limited.
1533df285b3dSMike Silbersack 		 * Those who need source quench packets may re-enable them
1534df285b3dSMike Silbersack 		 * via the net.inet.ip.sendsourcequench sysctl.
1535df285b3dSMike Silbersack 		 */
1536603724d3SBjoern A. Zeeb 		if (V_ip_sendsourcequench == 0) {
1537df285b3dSMike Silbersack 			m_freem(mcopy);
1538df285b3dSMike Silbersack 			return;
1539df285b3dSMike Silbersack 		} else {
1540df8bae1dSRodney W. Grimes 			type = ICMP_SOURCEQUENCH;
1541df8bae1dSRodney W. Grimes 			code = 0;
1542df285b3dSMike Silbersack 		}
1543df8bae1dSRodney W. Grimes 		break;
15443a06e3e0SRuslan Ermilov 
15453a06e3e0SRuslan Ermilov 	case EACCES:			/* ipfw denied packet */
15463a06e3e0SRuslan Ermilov 		m_freem(mcopy);
15473a06e3e0SRuslan Ermilov 		return;
1548df8bae1dSRodney W. Grimes 	}
1549c773494eSAndre Oppermann 	icmp_error(mcopy, type, code, dest.s_addr, mtu);
1550df8bae1dSRodney W. Grimes }
1551df8bae1dSRodney W. Grimes 
155282c23ebaSBill Fenner void
1553f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1554f2565d68SRobert Watson     struct mbuf *m)
155582c23ebaSBill Fenner {
15568b615593SMarko Zec 	INIT_VNET_NET(inp->inp_vnet);
15578b615593SMarko Zec 
1558be8a62e8SPoul-Henning Kamp 	if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1559be8a62e8SPoul-Henning Kamp 		struct bintime bt;
1560be8a62e8SPoul-Henning Kamp 
1561be8a62e8SPoul-Henning Kamp 		bintime(&bt);
1562be8a62e8SPoul-Henning Kamp 		if (inp->inp_socket->so_options & SO_BINTIME) {
1563be8a62e8SPoul-Henning Kamp 			*mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt),
1564be8a62e8SPoul-Henning Kamp 			SCM_BINTIME, SOL_SOCKET);
1565be8a62e8SPoul-Henning Kamp 			if (*mp)
1566be8a62e8SPoul-Henning Kamp 				mp = &(*mp)->m_next;
1567be8a62e8SPoul-Henning Kamp 		}
156882c23ebaSBill Fenner 		if (inp->inp_socket->so_options & SO_TIMESTAMP) {
156982c23ebaSBill Fenner 			struct timeval tv;
157082c23ebaSBill Fenner 
1571be8a62e8SPoul-Henning Kamp 			bintime2timeval(&bt, &tv);
157282c23ebaSBill Fenner 			*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
157382c23ebaSBill Fenner 				SCM_TIMESTAMP, SOL_SOCKET);
157482c23ebaSBill Fenner 			if (*mp)
157582c23ebaSBill Fenner 				mp = &(*mp)->m_next;
15764cc20ab1SSeigo Tanimura 		}
1577be8a62e8SPoul-Henning Kamp 	}
157882c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVDSTADDR) {
157982c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
158082c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
158182c23ebaSBill Fenner 		if (*mp)
158282c23ebaSBill Fenner 			mp = &(*mp)->m_next;
158382c23ebaSBill Fenner 	}
15844957466bSMatthew N. Dodd 	if (inp->inp_flags & INP_RECVTTL) {
15854957466bSMatthew N. Dodd 		*mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
15864957466bSMatthew N. Dodd 		    sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
15874957466bSMatthew N. Dodd 		if (*mp)
15884957466bSMatthew N. Dodd 			mp = &(*mp)->m_next;
15894957466bSMatthew N. Dodd 	}
159082c23ebaSBill Fenner #ifdef notyet
159182c23ebaSBill Fenner 	/* XXX
159282c23ebaSBill Fenner 	 * Moving these out of udp_input() made them even more broken
159382c23ebaSBill Fenner 	 * than they already were.
159482c23ebaSBill Fenner 	 */
159582c23ebaSBill Fenner 	/* options were tossed already */
159682c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVOPTS) {
159782c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
159882c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
159982c23ebaSBill Fenner 		if (*mp)
160082c23ebaSBill Fenner 			mp = &(*mp)->m_next;
160182c23ebaSBill Fenner 	}
160282c23ebaSBill Fenner 	/* ip_srcroute doesn't do what we want here, need to fix */
160382c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVRETOPTS) {
1604e0982661SAndre Oppermann 		*mp = sbcreatecontrol((caddr_t) ip_srcroute(m),
160582c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
160682c23ebaSBill Fenner 		if (*mp)
160782c23ebaSBill Fenner 			mp = &(*mp)->m_next;
160882c23ebaSBill Fenner 	}
160982c23ebaSBill Fenner #endif
161082c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVIF) {
1611d314ad7bSJulian Elischer 		struct ifnet *ifp;
1612d314ad7bSJulian Elischer 		struct sdlbuf {
161382c23ebaSBill Fenner 			struct sockaddr_dl sdl;
1614d314ad7bSJulian Elischer 			u_char	pad[32];
1615d314ad7bSJulian Elischer 		} sdlbuf;
1616d314ad7bSJulian Elischer 		struct sockaddr_dl *sdp;
1617d314ad7bSJulian Elischer 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
161882c23ebaSBill Fenner 
1619d314ad7bSJulian Elischer 		if (((ifp = m->m_pkthdr.rcvif))
1620603724d3SBjoern A. Zeeb 		&& ( ifp->if_index && (ifp->if_index <= V_if_index))) {
16214a0d6638SRuslan Ermilov 			sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1622d314ad7bSJulian Elischer 			/*
1623d314ad7bSJulian Elischer 			 * Change our mind and don't try copy.
1624d314ad7bSJulian Elischer 			 */
1625d314ad7bSJulian Elischer 			if ((sdp->sdl_family != AF_LINK)
1626d314ad7bSJulian Elischer 			|| (sdp->sdl_len > sizeof(sdlbuf))) {
1627d314ad7bSJulian Elischer 				goto makedummy;
1628d314ad7bSJulian Elischer 			}
1629d314ad7bSJulian Elischer 			bcopy(sdp, sdl2, sdp->sdl_len);
1630d314ad7bSJulian Elischer 		} else {
1631d314ad7bSJulian Elischer makedummy:
1632d314ad7bSJulian Elischer 			sdl2->sdl_len
1633d314ad7bSJulian Elischer 				= offsetof(struct sockaddr_dl, sdl_data[0]);
1634d314ad7bSJulian Elischer 			sdl2->sdl_family = AF_LINK;
1635d314ad7bSJulian Elischer 			sdl2->sdl_index = 0;
1636d314ad7bSJulian Elischer 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1637d314ad7bSJulian Elischer 		}
1638d314ad7bSJulian Elischer 		*mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
163982c23ebaSBill Fenner 			IP_RECVIF, IPPROTO_IP);
164082c23ebaSBill Fenner 		if (*mp)
164182c23ebaSBill Fenner 			mp = &(*mp)->m_next;
164282c23ebaSBill Fenner 	}
164382c23ebaSBill Fenner }
164482c23ebaSBill Fenner 
16454d2e3692SLuigi Rizzo /*
164630916a2dSRobert Watson  * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
164730916a2dSRobert Watson  * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
164830916a2dSRobert Watson  * locking.  This code remains in ip_input.c as ip_mroute.c is optionally
164930916a2dSRobert Watson  * compiled.
16504d2e3692SLuigi Rizzo  */
1651df8bae1dSRodney W. Grimes int
1652f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so)
1653f0068c4aSGarrett Wollman {
16548b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
16558b615593SMarko Zec 
1656f0068c4aSGarrett Wollman 	if (so->so_type != SOCK_RAW ||
1657f0068c4aSGarrett Wollman 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1658f0068c4aSGarrett Wollman 		return EOPNOTSUPP;
1659f0068c4aSGarrett Wollman 
1660603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL)
1661f0068c4aSGarrett Wollman 		return EADDRINUSE;
1662f0068c4aSGarrett Wollman 
1663603724d3SBjoern A. Zeeb 	V_ip_rsvpd = so;
16641c5de19aSGarrett Wollman 	/*
16651c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-increment
16661c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
16671c5de19aSGarrett Wollman 	 */
1668603724d3SBjoern A. Zeeb 	if (!V_ip_rsvp_on) {
1669603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 1;
1670603724d3SBjoern A. Zeeb 		V_rsvp_on++;
16711c5de19aSGarrett Wollman 	}
1672f0068c4aSGarrett Wollman 
1673f0068c4aSGarrett Wollman 	return 0;
1674f0068c4aSGarrett Wollman }
1675f0068c4aSGarrett Wollman 
1676f0068c4aSGarrett Wollman int
1677f0068c4aSGarrett Wollman ip_rsvp_done(void)
1678f0068c4aSGarrett Wollman {
16798b615593SMarko Zec 	INIT_VNET_INET(curvnet);
16808b615593SMarko Zec 
1681603724d3SBjoern A. Zeeb 	V_ip_rsvpd = NULL;
16821c5de19aSGarrett Wollman 	/*
16831c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-decrement
16841c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
16851c5de19aSGarrett Wollman 	 */
1686603724d3SBjoern A. Zeeb 	if (V_ip_rsvp_on) {
1687603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 0;
1688603724d3SBjoern A. Zeeb 		V_rsvp_on--;
16891c5de19aSGarrett Wollman 	}
1690f0068c4aSGarrett Wollman 	return 0;
1691f0068c4aSGarrett Wollman }
1692bbb4330bSLuigi Rizzo 
1693bbb4330bSLuigi Rizzo void
1694bbb4330bSLuigi Rizzo rsvp_input(struct mbuf *m, int off)	/* XXX must fixup manually */
1695bbb4330bSLuigi Rizzo {
16968b615593SMarko Zec 	INIT_VNET_INET(curvnet);
16978b615593SMarko Zec 
1698bbb4330bSLuigi Rizzo 	if (rsvp_input_p) { /* call the real one if loaded */
1699bbb4330bSLuigi Rizzo 		rsvp_input_p(m, off);
1700bbb4330bSLuigi Rizzo 		return;
1701bbb4330bSLuigi Rizzo 	}
1702bbb4330bSLuigi Rizzo 
1703bbb4330bSLuigi Rizzo 	/* Can still get packets with rsvp_on = 0 if there is a local member
1704bbb4330bSLuigi Rizzo 	 * of the group to which the RSVP packet is addressed.  But in this
1705bbb4330bSLuigi Rizzo 	 * case we want to throw the packet away.
1706bbb4330bSLuigi Rizzo 	 */
1707bbb4330bSLuigi Rizzo 
1708603724d3SBjoern A. Zeeb 	if (!V_rsvp_on) {
1709bbb4330bSLuigi Rizzo 		m_freem(m);
1710bbb4330bSLuigi Rizzo 		return;
1711bbb4330bSLuigi Rizzo 	}
1712bbb4330bSLuigi Rizzo 
1713603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL) {
1714bbb4330bSLuigi Rizzo 		rip_input(m, off);
1715bbb4330bSLuigi Rizzo 		return;
1716bbb4330bSLuigi Rizzo 	}
1717bbb4330bSLuigi Rizzo 	/* Drop the packet */
1718bbb4330bSLuigi Rizzo 	m_freem(m);
1719bbb4330bSLuigi Rizzo }
1720