xref: /freebsd/sys/netinet/ip_input.c (revision 385195c062ad27bfd78b9f9592a8bbcb9419acfb)
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"
3936b0360bSRobert Watson #include "opt_mac.h"
40a9771948SGleb Smirnoff #include "opt_carp.h"
4174a9466cSGary Palmer 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
445f311da2SMike Silbersack #include <sys/callout.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46b715f178SLuigi Rizzo #include <sys/malloc.h>
47df8bae1dSRodney W. Grimes #include <sys/domain.h>
48df8bae1dSRodney W. Grimes #include <sys/protosw.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/time.h>
51df8bae1dSRodney W. Grimes #include <sys/kernel.h>
52385195c0SMarko Zec #include <sys/lock.h>
53385195c0SMarko Zec #include <sys/rwlock.h>
541025071fSGarrett Wollman #include <sys/syslog.h>
55b5e8ce9fSBruce Evans #include <sys/sysctl.h>
56603724d3SBjoern A. Zeeb #include <sys/vimage.h>
57df8bae1dSRodney W. Grimes 
58c85540ddSAndrey A. Chernov #include <net/pfil.h>
59df8bae1dSRodney W. Grimes #include <net/if.h>
609494d596SBrooks Davis #include <net/if_types.h>
61d314ad7bSJulian Elischer #include <net/if_var.h>
6282c23ebaSBill Fenner #include <net/if_dl.h>
63df8bae1dSRodney W. Grimes #include <net/route.h>
64748e0b0aSGarrett Wollman #include <net/netisr.h>
654b79449eSBjoern A. Zeeb #include <net/vnet.h>
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes #include <netinet/in.h>
68df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
69b5e8ce9fSBruce Evans #include <netinet/in_var.h>
70df8bae1dSRodney W. Grimes #include <netinet/ip.h>
71df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
72df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
73df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
74ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
7558938916SGarrett Wollman #include <machine/in_cksum.h>
764b79449eSBjoern A. Zeeb #include <netinet/vinet.h>
77a9771948SGleb Smirnoff #ifdef DEV_CARP
78a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
79a9771948SGleb Smirnoff #endif
80b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
811dfcf0d2SAndre Oppermann #include <netinet/ip_ipsec.h>
82b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
83df8bae1dSRodney W. Grimes 
84f0068c4aSGarrett Wollman #include <sys/socketvar.h>
856ddbf1e2SGary Palmer 
86010b65f5SJulian Elischer /* XXX: Temporary until ipfw_ether and ipfw_bridge are converted. */
87010b65f5SJulian Elischer #include <netinet/ip_fw.h>
88010b65f5SJulian Elischer #include <netinet/ip_dummynet.h>
89010b65f5SJulian Elischer 
90aed55708SRobert Watson #include <security/mac/mac_framework.h>
91aed55708SRobert Watson 
92d2035ffbSEd Maste #ifdef CTASSERT
93d2035ffbSEd Maste CTASSERT(sizeof(struct ip) == 20);
94d2035ffbSEd Maste #endif
95d2035ffbSEd Maste 
96385195c0SMarko Zec #ifndef VIMAGE
97385195c0SMarko Zec #ifndef VIMAGE_GLOBALS
98385195c0SMarko Zec struct vnet_inet vnet_inet_0;
99385195c0SMarko Zec #endif
100385195c0SMarko Zec #endif
101385195c0SMarko Zec 
10244e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
10344e33a07SMarko Zec static int	ipsendredirects;
10444e33a07SMarko Zec static int	ip_checkinterface;
10544e33a07SMarko Zec static int	ip_keepfaith;
10644e33a07SMarko Zec static int	ip_sendsourcequench;
10744e33a07SMarko Zec int	ip_defttl;
10844e33a07SMarko Zec int	ip_do_randomid;
10944e33a07SMarko Zec int	ipforwarding;
11044e33a07SMarko Zec struct	in_ifaddrhead in_ifaddrhead; 		/* first inet address */
11144e33a07SMarko Zec struct	in_ifaddrhashhead *in_ifaddrhashtbl;	/* inet addr hash table  */
11244e33a07SMarko Zec u_long 	in_ifaddrhmask;				/* mask for hash table */
11344e33a07SMarko Zec struct ipstat ipstat;
11444e33a07SMarko Zec static int ip_rsvp_on;
11544e33a07SMarko Zec struct socket *ip_rsvpd;
11644e33a07SMarko Zec int	rsvp_on;
117f02493cbSMarko Zec static struct ipqhead ipq[IPREASS_NHASH];
11844e33a07SMarko Zec static int	maxnipq;	/* Administrative limit on # reass queues. */
11944e33a07SMarko Zec static int	maxfragsperpacket;
12044e33a07SMarko Zec int	ipstealth;
12144e33a07SMarko Zec static int	nipq;	/* Total # of reass queues */
12244e33a07SMarko Zec #endif
123f0068c4aSGarrett Wollman 
1248b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_FORWARDING,
1258b615593SMarko Zec     forwarding, CTLFLAG_RW, ipforwarding, 0,
1268b615593SMarko Zec     "Enable IP forwarding between interfaces");
1270312fbe9SPoul-Henning Kamp 
1288b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_SENDREDIRECTS,
1298b615593SMarko Zec     redirect, CTLFLAG_RW, ipsendredirects, 0,
1308b615593SMarko Zec     "Enable sending IP redirects");
1310312fbe9SPoul-Henning Kamp 
1328b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_DEFTTL,
1338b615593SMarko Zec     ttl, CTLFLAG_RW, ip_defttl, 0, "Maximum TTL on IP packets");
1340312fbe9SPoul-Henning Kamp 
1358b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_KEEPFAITH,
1368b615593SMarko Zec     keepfaith, CTLFLAG_RW, ip_keepfaith,	0,
1376a800098SYoshinobu Inoue     "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
1386a800098SYoshinobu Inoue 
1398b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO,
1408b615593SMarko Zec     sendsourcequench, CTLFLAG_RW, ip_sendsourcequench, 0,
141df285b3dSMike Silbersack     "Enable the transmission of source quench packets");
142df285b3dSMike Silbersack 
1438b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, random_id,
1448b615593SMarko Zec     CTLFLAG_RW, ip_do_randomid, 0, "Assign random ip_id values");
1451f44b0a1SDavid Malone 
146823db0e9SDon Lewis /*
147823db0e9SDon Lewis  * XXX - Setting ip_checkinterface mostly implements the receive side of
148823db0e9SDon Lewis  * the Strong ES model described in RFC 1122, but since the routing table
149a8f12100SDon Lewis  * and transmit implementation do not implement the Strong ES model,
150823db0e9SDon Lewis  * setting this to 1 results in an odd hybrid.
1513f67c834SDon Lewis  *
152a8f12100SDon Lewis  * XXX - ip_checkinterface currently must be disabled if you use ipnat
153a8f12100SDon Lewis  * to translate the destination address to another local interface.
1543f67c834SDon Lewis  *
1553f67c834SDon Lewis  * XXX - ip_checkinterface must be disabled if you add IP aliases
1563f67c834SDon Lewis  * to the loopback interface instead of the interface where the
1573f67c834SDon Lewis  * packets for those addresses are received.
158823db0e9SDon Lewis  */
1598b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO,
1608b615593SMarko Zec     check_interface, CTLFLAG_RW, ip_checkinterface, 0,
1618b615593SMarko Zec     "Verify packet arrives on correct interface");
162b3e95d4eSJonathan Lemon 
163c21fd232SAndre Oppermann struct pfil_head inet_pfil_hook;	/* Packet filter hooks */
164df8bae1dSRodney W. Grimes 
1651cafed39SJonathan Lemon static struct	ifqueue ipintrq;
166ca925d9cSJonathan Lemon static int	ipqmaxlen = IFQ_MAXLEN;
167ca925d9cSJonathan Lemon 
168df8bae1dSRodney W. Grimes extern	struct domain inetdomain;
169f0ffb944SJulian Elischer extern	struct protosw inetsw[];
170df8bae1dSRodney W. Grimes u_char	ip_protox[IPPROTO_MAX];
171ca925d9cSJonathan Lemon 
172afed1375SDavid Greenman SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
1733d177f46SBill Fumerola     &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
1740312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
1756489fe65SAndre Oppermann     &ipintrq.ifq_drops, 0,
1766489fe65SAndre Oppermann     "Number of packets dropped from the IP input queue");
177df8bae1dSRodney W. Grimes 
1788b615593SMarko Zec SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
1798b615593SMarko Zec     ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
180194a213eSAndrey A. Chernov 
181385195c0SMarko Zec #ifdef VIMAGE_GLOBALS
182d248c7d7SRobert Watson static uma_zone_t ipq_zone;
183385195c0SMarko Zec #endif
184dfa60d93SRobert Watson static struct mtx ipqlock;
1852fad1e93SSam Leffler 
1862fad1e93SSam Leffler #define	IPQ_LOCK()	mtx_lock(&ipqlock)
1872fad1e93SSam Leffler #define	IPQ_UNLOCK()	mtx_unlock(&ipqlock)
188888c2a3cSSam Leffler #define	IPQ_LOCK_INIT()	mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
189888c2a3cSSam Leffler #define	IPQ_LOCK_ASSERT()	mtx_assert(&ipqlock, MA_OWNED)
190f23b4c91SGarrett Wollman 
191d248c7d7SRobert Watson static void	maxnipq_update(void);
1924f590175SPaul Saab static void	ipq_zone_change(void *);
193d248c7d7SRobert Watson 
1948b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, fragpackets,
1958b615593SMarko Zec     CTLFLAG_RD, nipq, 0,
1968b615593SMarko Zec     "Current number of IPv4 fragment reassembly queue entries");
197d248c7d7SRobert Watson 
1988b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, maxfragsperpacket,
1998b615593SMarko Zec     CTLFLAG_RW, maxfragsperpacket, 0,
200d248c7d7SRobert Watson     "Maximum number of IPv4 fragments allowed per packet");
201d248c7d7SRobert Watson 
202d248c7d7SRobert Watson struct callout	ipport_tick_callout;
203d248c7d7SRobert Watson 
2040312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU
2050312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
2063d177f46SBill Fumerola     &ip_mtu, 0, "Default MTU");
2070312fbe9SPoul-Henning Kamp #endif
2080312fbe9SPoul-Henning Kamp 
2091b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
2108b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
2118b615593SMarko Zec     ipstealth, 0, "IP stealth mode, no TTL decrementation on forwarding");
2121b968362SDag-Erling Smørgrav #endif
2131b968362SDag-Erling Smørgrav 
214010b65f5SJulian Elischer /*
215010b65f5SJulian Elischer  * ipfw_ether and ipfw_bridge hooks.
216010b65f5SJulian Elischer  * XXX: Temporary until those are converted to pfil_hooks as well.
217010b65f5SJulian Elischer  */
218010b65f5SJulian Elischer ip_fw_chk_t *ip_fw_chk_ptr = NULL;
219010b65f5SJulian Elischer ip_dn_io_t *ip_dn_io_ptr = NULL;
220385195c0SMarko Zec #ifdef VIMAGE_GLOBALS
221385195c0SMarko Zec int fw_one_pass;
222385195c0SMarko Zec #endif
223010b65f5SJulian Elischer 
2244d77a549SAlfred Perlstein static void	ip_freef(struct ipqhead *, struct ipq *);
2258948e4baSArchie Cobbs 
226df8bae1dSRodney W. Grimes /*
227df8bae1dSRodney W. Grimes  * IP initialization: fill in IP protocol switch table.
228df8bae1dSRodney W. Grimes  * All protocols not implemented in kernel go to raw IP protocol handler.
229df8bae1dSRodney W. Grimes  */
230df8bae1dSRodney W. Grimes void
231f2565d68SRobert Watson ip_init(void)
232df8bae1dSRodney W. Grimes {
2338b615593SMarko Zec 	INIT_VNET_INET(curvnet);
234f2565d68SRobert Watson 	struct protosw *pr;
235f2565d68SRobert Watson 	int i;
236df8bae1dSRodney W. Grimes 
23744e33a07SMarko Zec 	V_ipsendredirects = 1; /* XXX */
23844e33a07SMarko Zec 	V_ip_checkinterface = 0;
23944e33a07SMarko Zec 	V_ip_keepfaith = 0;
24044e33a07SMarko Zec 	V_ip_sendsourcequench = 0;
24144e33a07SMarko Zec 	V_rsvp_on = 0;
24244e33a07SMarko Zec 	V_ip_defttl = IPDEFTTL;
24344e33a07SMarko Zec 	V_ip_do_randomid = 0;
24444e33a07SMarko Zec 	V_ipforwarding = 0;
24544e33a07SMarko Zec 	V_ipstealth = 0;
24644e33a07SMarko Zec 	V_nipq = 0;	/* Total # of reass queues */
24744e33a07SMarko Zec 
24844e33a07SMarko Zec 	V_ipport_lowfirstauto = IPPORT_RESERVED - 1;	/* 1023 */
24944e33a07SMarko Zec 	V_ipport_lowlastauto = IPPORT_RESERVEDSTART;	/* 600 */
25044e33a07SMarko Zec 	V_ipport_firstauto = IPPORT_EPHEMERALFIRST;	/* 10000 */
25144e33a07SMarko Zec 	V_ipport_lastauto = IPPORT_EPHEMERALLAST;	/* 65535 */
25244e33a07SMarko Zec 	V_ipport_hifirstauto = IPPORT_HIFIRSTAUTO;	/* 49152 */
25344e33a07SMarko Zec 	V_ipport_hilastauto = IPPORT_HILASTAUTO;	/* 65535 */
25444e33a07SMarko Zec 	V_ipport_reservedhigh = IPPORT_RESERVED - 1;	/* 1023 */
25544e33a07SMarko Zec 	V_ipport_reservedlow = 0;
25644e33a07SMarko Zec 	V_ipport_randomized = 1;	/* user controlled via sysctl */
25744e33a07SMarko Zec 	V_ipport_randomcps = 10;	/* user controlled via sysctl */
25844e33a07SMarko Zec 	V_ipport_randomtime = 45;	/* user controlled via sysctl */
25944e33a07SMarko Zec 	V_ipport_stoprandom = 0;	/* toggled by ipport_tick */
26044e33a07SMarko Zec 
261385195c0SMarko Zec 	V_fw_one_pass = 1;
262385195c0SMarko Zec 
26344e33a07SMarko Zec #ifdef NOTYET
26444e33a07SMarko Zec 	/* XXX global static but not instantiated in this file */
26544e33a07SMarko Zec 	V_ipfastforward_active = 0;
26644e33a07SMarko Zec 	V_subnetsarelocal = 0;
26744e33a07SMarko Zec 	V_sameprefixcarponly = 0;
26844e33a07SMarko Zec #endif
26944e33a07SMarko Zec 
270603724d3SBjoern A. Zeeb 	TAILQ_INIT(&V_in_ifaddrhead);
271603724d3SBjoern A. Zeeb 	V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
272f0ffb944SJulian Elischer 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
27302410549SRobert Watson 	if (pr == NULL)
274db09bef3SAndre Oppermann 		panic("ip_init: PF_INET not found");
275db09bef3SAndre Oppermann 
276db09bef3SAndre Oppermann 	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
277df8bae1dSRodney W. Grimes 	for (i = 0; i < IPPROTO_MAX; i++)
278df8bae1dSRodney W. Grimes 		ip_protox[i] = pr - inetsw;
279db09bef3SAndre Oppermann 	/*
280db09bef3SAndre Oppermann 	 * Cycle through IP protocols and put them into the appropriate place
281db09bef3SAndre Oppermann 	 * in ip_protox[].
282db09bef3SAndre Oppermann 	 */
283f0ffb944SJulian Elischer 	for (pr = inetdomain.dom_protosw;
284f0ffb944SJulian Elischer 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
285df8bae1dSRodney W. Grimes 		if (pr->pr_domain->dom_family == PF_INET &&
286db09bef3SAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
287db09bef3SAndre Oppermann 			/* Be careful to only index valid IP protocols. */
288db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX)
289df8bae1dSRodney W. Grimes 				ip_protox[pr->pr_protocol] = pr - inetsw;
290db09bef3SAndre Oppermann 		}
291194a213eSAndrey A. Chernov 
292c21fd232SAndre Oppermann 	/* Initialize packet filter hooks. */
293134ea224SSam Leffler 	inet_pfil_hook.ph_type = PFIL_TYPE_AF;
294134ea224SSam Leffler 	inet_pfil_hook.ph_af = AF_INET;
295134ea224SSam Leffler 	if ((i = pfil_head_register(&inet_pfil_hook)) != 0)
296134ea224SSam Leffler 		printf("%s: WARNING: unable to register pfil hook, "
297134ea224SSam Leffler 			"error %d\n", __func__, i);
298134ea224SSam Leffler 
299db09bef3SAndre Oppermann 	/* Initialize IP reassembly queue. */
3002fad1e93SSam Leffler 	IPQ_LOCK_INIT();
301194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++)
302603724d3SBjoern A. Zeeb 	    TAILQ_INIT(&V_ipq[i]);
303603724d3SBjoern A. Zeeb 	V_maxnipq = nmbclusters / 32;
304603724d3SBjoern A. Zeeb 	V_maxfragsperpacket = 16;
305603724d3SBjoern A. Zeeb 	V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
306d248c7d7SRobert Watson 	    NULL, UMA_ALIGN_PTR, 0);
307d248c7d7SRobert Watson 	maxnipq_update();
308194a213eSAndrey A. Chernov 
3095f311da2SMike Silbersack 	/* Start ipport_tick. */
3105f311da2SMike Silbersack 	callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
3115f311da2SMike Silbersack 	ipport_tick(NULL);
3125f311da2SMike Silbersack 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
3135f311da2SMike Silbersack 		SHUTDOWN_PRI_DEFAULT);
3144f590175SPaul Saab 	EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
3154f590175SPaul Saab 		NULL, EVENTHANDLER_PRI_ANY);
3165f311da2SMike Silbersack 
317db09bef3SAndre Oppermann 	/* Initialize various other remaining things. */
318b53c8130SJulian Elischer 	V_ip_id = time_second & 0xffff;
319df8bae1dSRodney W. Grimes 	ipintrq.ifq_maxlen = ipqmaxlen;
3206008862bSJohn Baldwin 	mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF);
32159dd72d0SRobert Watson 	netisr_register(NETISR_IP, ip_input, &ipintrq, 0);
322df8bae1dSRodney W. Grimes }
323df8bae1dSRodney W. Grimes 
324f2565d68SRobert Watson void
325f2565d68SRobert Watson ip_fini(void *xtp)
3265f311da2SMike Silbersack {
327f2565d68SRobert Watson 
3285f311da2SMike Silbersack 	callout_stop(&ipport_tick_callout);
3295f311da2SMike Silbersack }
3305f311da2SMike Silbersack 
3314d2e3692SLuigi Rizzo /*
332df8bae1dSRodney W. Grimes  * Ip input routine.  Checksum and byte swap header.  If fragmented
333df8bae1dSRodney W. Grimes  * try to reassemble.  Process options.  Pass to next level.
334df8bae1dSRodney W. Grimes  */
335c67b1d17SGarrett Wollman void
336c67b1d17SGarrett Wollman ip_input(struct mbuf *m)
337df8bae1dSRodney W. Grimes {
3388b615593SMarko Zec 	INIT_VNET_INET(curvnet);
3399188b4a1SAndre Oppermann 	struct ip *ip = NULL;
3405da9f8faSJosef Karthauser 	struct in_ifaddr *ia = NULL;
341ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
3429b932e9eSAndre Oppermann 	int    checkif, hlen = 0;
34347c861ecSBrian Somers 	u_short sum;
34402c1c707SAndre Oppermann 	int dchg = 0;				/* dest changed after fw */
345f51f805fSSam Leffler 	struct in_addr odst;			/* original dst address */
346b715f178SLuigi Rizzo 
347fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
348db40007dSAndrew R. Reiter 
349ac9d7e26SMax Laier 	if (m->m_flags & M_FASTFWD_OURS) {
3509b932e9eSAndre Oppermann 		/*
35176ff6dcfSAndre Oppermann 		 * Firewall or NAT changed destination to local.
35276ff6dcfSAndre Oppermann 		 * We expect ip_len and ip_off to be in host byte order.
3539b932e9eSAndre Oppermann 		 */
35476ff6dcfSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
35576ff6dcfSAndre Oppermann 		/* Set up some basics that will be used later. */
3562b25acc1SLuigi Rizzo 		ip = mtod(m, struct ip *);
35753be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
3589b932e9eSAndre Oppermann 		goto ours;
3592b25acc1SLuigi Rizzo 	}
3602b25acc1SLuigi Rizzo 
361603724d3SBjoern A. Zeeb 	V_ipstat.ips_total++;
36258938916SGarrett Wollman 
36358938916SGarrett Wollman 	if (m->m_pkthdr.len < sizeof(struct ip))
36458938916SGarrett Wollman 		goto tooshort;
36558938916SGarrett Wollman 
366df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct ip) &&
3670b17fba7SAndre Oppermann 	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
368603724d3SBjoern A. Zeeb 		V_ipstat.ips_toosmall++;
369c67b1d17SGarrett Wollman 		return;
370df8bae1dSRodney W. Grimes 	}
371df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
37258938916SGarrett Wollman 
37353be11f6SPoul-Henning Kamp 	if (ip->ip_v != IPVERSION) {
374603724d3SBjoern A. Zeeb 		V_ipstat.ips_badvers++;
375df8bae1dSRodney W. Grimes 		goto bad;
376df8bae1dSRodney W. Grimes 	}
37758938916SGarrett Wollman 
37853be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
379df8bae1dSRodney W. Grimes 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
380603724d3SBjoern A. Zeeb 		V_ipstat.ips_badhlen++;
381df8bae1dSRodney W. Grimes 		goto bad;
382df8bae1dSRodney W. Grimes 	}
383df8bae1dSRodney W. Grimes 	if (hlen > m->m_len) {
3840b17fba7SAndre Oppermann 		if ((m = m_pullup(m, hlen)) == NULL) {
385603724d3SBjoern A. Zeeb 			V_ipstat.ips_badhlen++;
386c67b1d17SGarrett Wollman 			return;
387df8bae1dSRodney W. Grimes 		}
388df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
389df8bae1dSRodney W. Grimes 	}
39033841545SHajimu UMEMOTO 
39133841545SHajimu UMEMOTO 	/* 127/8 must not appear on wire - RFC1122 */
39233841545SHajimu UMEMOTO 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
39333841545SHajimu UMEMOTO 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
39433841545SHajimu UMEMOTO 		if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
395603724d3SBjoern A. Zeeb 			V_ipstat.ips_badaddr++;
39633841545SHajimu UMEMOTO 			goto bad;
39733841545SHajimu UMEMOTO 		}
39833841545SHajimu UMEMOTO 	}
39933841545SHajimu UMEMOTO 
400db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
401db4f9cc7SJonathan Lemon 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
402db4f9cc7SJonathan Lemon 	} else {
40358938916SGarrett Wollman 		if (hlen == sizeof(struct ip)) {
40447c861ecSBrian Somers 			sum = in_cksum_hdr(ip);
40558938916SGarrett Wollman 		} else {
40647c861ecSBrian Somers 			sum = in_cksum(m, hlen);
40758938916SGarrett Wollman 		}
408db4f9cc7SJonathan Lemon 	}
40947c861ecSBrian Somers 	if (sum) {
410603724d3SBjoern A. Zeeb 		V_ipstat.ips_badsum++;
411df8bae1dSRodney W. Grimes 		goto bad;
412df8bae1dSRodney W. Grimes 	}
413df8bae1dSRodney W. Grimes 
41402b199f1SMax Laier #ifdef ALTQ
41502b199f1SMax Laier 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
41602b199f1SMax Laier 		/* packet is dropped by traffic conditioner */
41702b199f1SMax Laier 		return;
41802b199f1SMax Laier #endif
41902b199f1SMax Laier 
420df8bae1dSRodney W. Grimes 	/*
421df8bae1dSRodney W. Grimes 	 * Convert fields to host representation.
422df8bae1dSRodney W. Grimes 	 */
423fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
424df8bae1dSRodney W. Grimes 	if (ip->ip_len < hlen) {
425603724d3SBjoern A. Zeeb 		V_ipstat.ips_badlen++;
426df8bae1dSRodney W. Grimes 		goto bad;
427df8bae1dSRodney W. Grimes 	}
428fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
429df8bae1dSRodney W. Grimes 
430df8bae1dSRodney W. Grimes 	/*
431df8bae1dSRodney W. Grimes 	 * Check that the amount of data in the buffers
432df8bae1dSRodney W. Grimes 	 * is as at least much as the IP header would have us expect.
433df8bae1dSRodney W. Grimes 	 * Trim mbufs if longer than we expect.
434df8bae1dSRodney W. Grimes 	 * Drop packet if shorter than we expect.
435df8bae1dSRodney W. Grimes 	 */
436df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len < ip->ip_len) {
43758938916SGarrett Wollman tooshort:
438603724d3SBjoern A. Zeeb 		V_ipstat.ips_tooshort++;
439df8bae1dSRodney W. Grimes 		goto bad;
440df8bae1dSRodney W. Grimes 	}
441df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len > ip->ip_len) {
442df8bae1dSRodney W. Grimes 		if (m->m_len == m->m_pkthdr.len) {
443df8bae1dSRodney W. Grimes 			m->m_len = ip->ip_len;
444df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = ip->ip_len;
445df8bae1dSRodney W. Grimes 		} else
446df8bae1dSRodney W. Grimes 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
447df8bae1dSRodney W. Grimes 	}
448b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
44914dd6717SSam Leffler 	/*
45014dd6717SSam Leffler 	 * Bypass packet filtering for packets from a tunnel (gif).
45114dd6717SSam Leffler 	 */
452cc977adcSBjoern A. Zeeb 	if (ip_ipsec_filtertunnel(m))
453c21fd232SAndre Oppermann 		goto passin;
454b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
4553f67c834SDon Lewis 
456c4ac87eaSDarren Reed 	/*
457134ea224SSam Leffler 	 * Run through list of hooks for input packets.
458f51f805fSSam Leffler 	 *
459f51f805fSSam Leffler 	 * NB: Beware of the destination address changing (e.g.
460f51f805fSSam Leffler 	 *     by NAT rewriting).  When this happens, tell
461f51f805fSSam Leffler 	 *     ip_forward to do the right thing.
462c4ac87eaSDarren Reed 	 */
463c21fd232SAndre Oppermann 
464c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
465604afec4SChristian S.J. Peron 	if (!PFIL_HOOKED(&inet_pfil_hook))
466c21fd232SAndre Oppermann 		goto passin;
467c21fd232SAndre Oppermann 
468f51f805fSSam Leffler 	odst = ip->ip_dst;
469134ea224SSam Leffler 	if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
470d6a8d588SMax Laier 	    PFIL_IN, NULL) != 0)
471beec8214SDarren Reed 		return;
472134ea224SSam Leffler 	if (m == NULL)			/* consumed by filter */
473c4ac87eaSDarren Reed 		return;
4749b932e9eSAndre Oppermann 
475c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
47602c1c707SAndre Oppermann 	dchg = (odst.s_addr != ip->ip_dst.s_addr);
4779b932e9eSAndre Oppermann 
4789b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
4799b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
4809b932e9eSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
4819b932e9eSAndre Oppermann 		goto ours;
4829b932e9eSAndre Oppermann 	}
483099dd043SAndre Oppermann 	if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) {
484099dd043SAndre Oppermann 		/*
485099dd043SAndre Oppermann 		 * Directly ship on the packet.  This allows to forward packets
486099dd043SAndre Oppermann 		 * that were destined for us to some other directly connected
487099dd043SAndre Oppermann 		 * host.
488099dd043SAndre Oppermann 		 */
489099dd043SAndre Oppermann 		ip_forward(m, dchg);
490099dd043SAndre Oppermann 		return;
491099dd043SAndre Oppermann 	}
4929b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
4939b932e9eSAndre Oppermann 
494c21fd232SAndre Oppermann passin:
495df8bae1dSRodney W. Grimes 	/*
496df8bae1dSRodney W. Grimes 	 * Process options and, if not destined for us,
497df8bae1dSRodney W. Grimes 	 * ship it on.  ip_dooptions returns 1 when an
498df8bae1dSRodney W. Grimes 	 * error was detected (causing an icmp message
499df8bae1dSRodney W. Grimes 	 * to be sent and the original packet to be freed).
500df8bae1dSRodney W. Grimes 	 */
5019b932e9eSAndre Oppermann 	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
502c67b1d17SGarrett Wollman 		return;
503df8bae1dSRodney W. Grimes 
504f0068c4aSGarrett Wollman         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
505f0068c4aSGarrett Wollman          * matter if it is destined to another node, or whether it is
506f0068c4aSGarrett Wollman          * a multicast one, RSVP wants it! and prevents it from being forwarded
507f0068c4aSGarrett Wollman          * anywhere else. Also checks if the rsvp daemon is running before
508f0068c4aSGarrett Wollman 	 * grabbing the packet.
509f0068c4aSGarrett Wollman          */
510603724d3SBjoern A. Zeeb 	if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP)
511f0068c4aSGarrett Wollman 		goto ours;
512f0068c4aSGarrett Wollman 
513df8bae1dSRodney W. Grimes 	/*
514df8bae1dSRodney W. Grimes 	 * Check our list of addresses, to see if the packet is for us.
515cc766e04SGarrett Wollman 	 * If we don't have any addresses, assume any unicast packet
516cc766e04SGarrett Wollman 	 * we receive might be for us (and let the upper layers deal
517cc766e04SGarrett Wollman 	 * with it).
518df8bae1dSRodney W. Grimes 	 */
519603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
520cc766e04SGarrett Wollman 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
521cc766e04SGarrett Wollman 		goto ours;
522cc766e04SGarrett Wollman 
5237538a9a0SJonathan Lemon 	/*
524823db0e9SDon Lewis 	 * Enable a consistency check between the destination address
525823db0e9SDon Lewis 	 * and the arrival interface for a unicast packet (the RFC 1122
526823db0e9SDon Lewis 	 * strong ES model) if IP forwarding is disabled and the packet
527e15ae1b2SDon Lewis 	 * is not locally generated and the packet is not subject to
528e15ae1b2SDon Lewis 	 * 'ipfw fwd'.
5293f67c834SDon Lewis 	 *
5303f67c834SDon Lewis 	 * XXX - Checking also should be disabled if the destination
5313f67c834SDon Lewis 	 * address is ipnat'ed to a different interface.
5323f67c834SDon Lewis 	 *
533a8f12100SDon Lewis 	 * XXX - Checking is incompatible with IP aliases added
5343f67c834SDon Lewis 	 * to the loopback interface instead of the interface where
5353f67c834SDon Lewis 	 * the packets are received.
536a9771948SGleb Smirnoff 	 *
537a9771948SGleb Smirnoff 	 * XXX - This is the case for carp vhost IPs as well so we
538a9771948SGleb Smirnoff 	 * insert a workaround. If the packet got here, we already
539a9771948SGleb Smirnoff 	 * checked with carp_iamatch() and carp_forus().
540823db0e9SDon Lewis 	 */
541603724d3SBjoern A. Zeeb 	checkif = V_ip_checkinterface && (V_ipforwarding == 0) &&
5429494d596SBrooks Davis 	    m->m_pkthdr.rcvif != NULL &&
543e15ae1b2SDon Lewis 	    ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
544a9771948SGleb Smirnoff #ifdef DEV_CARP
545a9771948SGleb Smirnoff 	    !m->m_pkthdr.rcvif->if_carp &&
546a9771948SGleb Smirnoff #endif
5479b932e9eSAndre Oppermann 	    (dchg == 0);
548823db0e9SDon Lewis 
549ca925d9cSJonathan Lemon 	/*
550ca925d9cSJonathan Lemon 	 * Check for exact addresses in the hash bucket.
551ca925d9cSJonathan Lemon 	 */
5529b932e9eSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
553f9e354dfSJulian Elischer 		/*
554823db0e9SDon Lewis 		 * If the address matches, verify that the packet
555823db0e9SDon Lewis 		 * arrived via the correct interface if checking is
556823db0e9SDon Lewis 		 * enabled.
557f9e354dfSJulian Elischer 		 */
5589b932e9eSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
559823db0e9SDon Lewis 		    (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
560ed1ff184SJulian Elischer 			goto ours;
561ca925d9cSJonathan Lemon 	}
562823db0e9SDon Lewis 	/*
563ca925d9cSJonathan Lemon 	 * Check for broadcast addresses.
564ca925d9cSJonathan Lemon 	 *
565ca925d9cSJonathan Lemon 	 * Only accept broadcast packets that arrive via the matching
566ca925d9cSJonathan Lemon 	 * interface.  Reception of forwarded directed broadcasts would
567ca925d9cSJonathan Lemon 	 * be handled via ip_forward() and ether_output() with the loopback
568ca925d9cSJonathan Lemon 	 * into the stack for SIMPLEX interfaces handled by ether_output().
569823db0e9SDon Lewis 	 */
5704f450ff9SBruce M Simpson 	if (m->m_pkthdr.rcvif != NULL &&
5714f450ff9SBruce M Simpson 	    m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
572ca925d9cSJonathan Lemon 	        TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
573ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
574ca925d9cSJonathan Lemon 				continue;
575ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
576df8bae1dSRodney W. Grimes 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
5779b932e9eSAndre Oppermann 			    ip->ip_dst.s_addr)
578df8bae1dSRodney W. Grimes 				goto ours;
5799b932e9eSAndre Oppermann 			if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr)
580df8bae1dSRodney W. Grimes 				goto ours;
5810ac40133SBrian Somers #ifdef BOOTP_COMPAT
582ca925d9cSJonathan Lemon 			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
583ca925d9cSJonathan Lemon 				goto ours;
5840ac40133SBrian Somers #endif
585df8bae1dSRodney W. Grimes 		}
586df8bae1dSRodney W. Grimes 	}
587f8429ca2SBruce M Simpson 	/* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
588f8429ca2SBruce M Simpson 	if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
589603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantforward++;
590f8429ca2SBruce M Simpson 		m_freem(m);
591f8429ca2SBruce M Simpson 		return;
592f8429ca2SBruce M Simpson 	}
593df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
594df8bae1dSRodney W. Grimes 		struct in_multi *inm;
595603724d3SBjoern A. Zeeb 		if (V_ip_mrouter) {
596df8bae1dSRodney W. Grimes 			/*
597df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, all
598df8bae1dSRodney W. Grimes 			 * incoming multicast packets are passed to the
599df8bae1dSRodney W. Grimes 			 * kernel-level multicast forwarding function.
600df8bae1dSRodney W. Grimes 			 * The packet is returned (relatively) intact; if
601df8bae1dSRodney W. Grimes 			 * ip_mforward() returns a non-zero value, the packet
602df8bae1dSRodney W. Grimes 			 * must be discarded, else it may be accepted below.
603df8bae1dSRodney W. Grimes 			 */
604bbb4330bSLuigi Rizzo 			if (ip_mforward &&
605bbb4330bSLuigi Rizzo 			    ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
606603724d3SBjoern A. Zeeb 				V_ipstat.ips_cantforward++;
607df8bae1dSRodney W. Grimes 				m_freem(m);
608c67b1d17SGarrett Wollman 				return;
609df8bae1dSRodney W. Grimes 			}
610df8bae1dSRodney W. Grimes 
611df8bae1dSRodney W. Grimes 			/*
61211612afaSDima Dorfman 			 * The process-level routing daemon needs to receive
613df8bae1dSRodney W. Grimes 			 * all multicast IGMP packets, whether or not this
614df8bae1dSRodney W. Grimes 			 * host belongs to their destination groups.
615df8bae1dSRodney W. Grimes 			 */
616df8bae1dSRodney W. Grimes 			if (ip->ip_p == IPPROTO_IGMP)
617df8bae1dSRodney W. Grimes 				goto ours;
618603724d3SBjoern A. Zeeb 			V_ipstat.ips_forward++;
619df8bae1dSRodney W. Grimes 		}
620df8bae1dSRodney W. Grimes 		/*
621df8bae1dSRodney W. Grimes 		 * See if we belong to the destination multicast group on the
622df8bae1dSRodney W. Grimes 		 * arrival interface.
623df8bae1dSRodney W. Grimes 		 */
624dd5a318bSRobert Watson 		IN_MULTI_LOCK();
625df8bae1dSRodney W. Grimes 		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
626dd5a318bSRobert Watson 		IN_MULTI_UNLOCK();
627df8bae1dSRodney W. Grimes 		if (inm == NULL) {
628603724d3SBjoern A. Zeeb 			V_ipstat.ips_notmember++;
629df8bae1dSRodney W. Grimes 			m_freem(m);
630c67b1d17SGarrett Wollman 			return;
631df8bae1dSRodney W. Grimes 		}
632df8bae1dSRodney W. Grimes 		goto ours;
633df8bae1dSRodney W. Grimes 	}
634df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
635df8bae1dSRodney W. Grimes 		goto ours;
636df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == INADDR_ANY)
637df8bae1dSRodney W. Grimes 		goto ours;
638df8bae1dSRodney W. Grimes 
6396a800098SYoshinobu Inoue 	/*
6406a800098SYoshinobu Inoue 	 * FAITH(Firewall Aided Internet Translator)
6416a800098SYoshinobu Inoue 	 */
6426a800098SYoshinobu Inoue 	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
643603724d3SBjoern A. Zeeb 		if (V_ip_keepfaith) {
6446a800098SYoshinobu Inoue 			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
6456a800098SYoshinobu Inoue 				goto ours;
6466a800098SYoshinobu Inoue 		}
6476a800098SYoshinobu Inoue 		m_freem(m);
6486a800098SYoshinobu Inoue 		return;
6496a800098SYoshinobu Inoue 	}
6509494d596SBrooks Davis 
651df8bae1dSRodney W. Grimes 	/*
652df8bae1dSRodney W. Grimes 	 * Not for us; forward if possible and desirable.
653df8bae1dSRodney W. Grimes 	 */
654603724d3SBjoern A. Zeeb 	if (V_ipforwarding == 0) {
655603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantforward++;
656df8bae1dSRodney W. Grimes 		m_freem(m);
657546f251bSChris D. Faulhaber 	} else {
658b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
6591dfcf0d2SAndre Oppermann 		if (ip_ipsec_fwd(m))
660546f251bSChris D. Faulhaber 			goto bad;
661b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
6629b932e9eSAndre Oppermann 		ip_forward(m, dchg);
663546f251bSChris D. Faulhaber 	}
664c67b1d17SGarrett Wollman 	return;
665df8bae1dSRodney W. Grimes 
666df8bae1dSRodney W. Grimes ours:
667d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH
668d0ebc0d2SYaroslav Tykhiy 	/*
669d0ebc0d2SYaroslav Tykhiy 	 * IPSTEALTH: Process non-routing options only
670d0ebc0d2SYaroslav Tykhiy 	 * if the packet is destined for us.
671d0ebc0d2SYaroslav Tykhiy 	 */
672603724d3SBjoern A. Zeeb 	if (V_ipstealth && hlen > sizeof (struct ip) &&
6739b932e9eSAndre Oppermann 	    ip_dooptions(m, 1))
674d0ebc0d2SYaroslav Tykhiy 		return;
675d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */
676d0ebc0d2SYaroslav Tykhiy 
6775da9f8faSJosef Karthauser 	/* Count the packet in the ip address stats */
6785da9f8faSJosef Karthauser 	if (ia != NULL) {
6795da9f8faSJosef Karthauser 		ia->ia_ifa.if_ipackets++;
6805da9f8faSJosef Karthauser 		ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
6815da9f8faSJosef Karthauser 	}
682100ba1a6SJordan K. Hubbard 
68363f8d699SJordan K. Hubbard 	/*
684b6ea1aa5SRuslan Ermilov 	 * Attempt reassembly; if it succeeds, proceed.
685ac9d7e26SMax Laier 	 * ip_reass() will return a different mbuf.
686df8bae1dSRodney W. Grimes 	 */
687f0cada84SAndre Oppermann 	if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
688f0cada84SAndre Oppermann 		m = ip_reass(m);
689f0cada84SAndre Oppermann 		if (m == NULL)
690c67b1d17SGarrett Wollman 			return;
6916a800098SYoshinobu Inoue 		ip = mtod(m, struct ip *);
6927e2df452SRuslan Ermilov 		/* Get the header length of the reassembled packet */
69353be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
694f0cada84SAndre Oppermann 	}
695f0cada84SAndre Oppermann 
696f0cada84SAndre Oppermann 	/*
697f0cada84SAndre Oppermann 	 * Further protocols expect the packet length to be w/o the
698f0cada84SAndre Oppermann 	 * IP header.
699f0cada84SAndre Oppermann 	 */
700df8bae1dSRodney W. Grimes 	ip->ip_len -= hlen;
701df8bae1dSRodney W. Grimes 
702b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
70333841545SHajimu UMEMOTO 	/*
70433841545SHajimu UMEMOTO 	 * enforce IPsec policy checking if we are seeing last header.
70533841545SHajimu UMEMOTO 	 * note that we do not visit this with protocols with pcb layer
70633841545SHajimu UMEMOTO 	 * code - like udp/tcp/raw ip.
70733841545SHajimu UMEMOTO 	 */
7081dfcf0d2SAndre Oppermann 	if (ip_ipsec_input(m))
70933841545SHajimu UMEMOTO 		goto bad;
710b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
71133841545SHajimu UMEMOTO 
712df8bae1dSRodney W. Grimes 	/*
713df8bae1dSRodney W. Grimes 	 * Switch out to protocol's input routine.
714df8bae1dSRodney W. Grimes 	 */
715603724d3SBjoern A. Zeeb 	V_ipstat.ips_delivered++;
7169b932e9eSAndre Oppermann 
7172b25acc1SLuigi Rizzo 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
718c67b1d17SGarrett Wollman 	return;
719df8bae1dSRodney W. Grimes bad:
720df8bae1dSRodney W. Grimes 	m_freem(m);
721c67b1d17SGarrett Wollman }
722c67b1d17SGarrett Wollman 
723c67b1d17SGarrett Wollman /*
724d248c7d7SRobert Watson  * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
725d248c7d7SRobert Watson  * max has slightly different semantics than the sysctl, for historical
726d248c7d7SRobert Watson  * reasons.
727d248c7d7SRobert Watson  */
728d248c7d7SRobert Watson static void
729d248c7d7SRobert Watson maxnipq_update(void)
730d248c7d7SRobert Watson {
7318b615593SMarko Zec 	INIT_VNET_INET(curvnet);
732d248c7d7SRobert Watson 
733d248c7d7SRobert Watson 	/*
734d248c7d7SRobert Watson 	 * -1 for unlimited allocation.
735d248c7d7SRobert Watson 	 */
736603724d3SBjoern A. Zeeb 	if (V_maxnipq < 0)
737603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 0);
738d248c7d7SRobert Watson 	/*
739d248c7d7SRobert Watson 	 * Positive number for specific bound.
740d248c7d7SRobert Watson 	 */
741603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0)
742603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, V_maxnipq);
743d248c7d7SRobert Watson 	/*
744d248c7d7SRobert Watson 	 * Zero specifies no further fragment queue allocation -- set the
745d248c7d7SRobert Watson 	 * bound very low, but rely on implementation elsewhere to actually
746d248c7d7SRobert Watson 	 * prevent allocation and reclaim current queues.
747d248c7d7SRobert Watson 	 */
748603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0)
749603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 1);
750d248c7d7SRobert Watson }
751d248c7d7SRobert Watson 
7524f590175SPaul Saab static void
7534f590175SPaul Saab ipq_zone_change(void *tag)
7544f590175SPaul Saab {
7558b615593SMarko Zec 	INIT_VNET_INET(curvnet);
7564f590175SPaul Saab 
757603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) {
758603724d3SBjoern A. Zeeb 		V_maxnipq = nmbclusters / 32;
7594f590175SPaul Saab 		maxnipq_update();
7604f590175SPaul Saab 	}
7614f590175SPaul Saab }
7624f590175SPaul Saab 
763d248c7d7SRobert Watson static int
764d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
765d248c7d7SRobert Watson {
7668b615593SMarko Zec 	INIT_VNET_INET(curvnet);
767d248c7d7SRobert Watson 	int error, i;
768d248c7d7SRobert Watson 
769603724d3SBjoern A. Zeeb 	i = V_maxnipq;
770d248c7d7SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
771d248c7d7SRobert Watson 	if (error || !req->newptr)
772d248c7d7SRobert Watson 		return (error);
773d248c7d7SRobert Watson 
774d248c7d7SRobert Watson 	/*
775d248c7d7SRobert Watson 	 * XXXRW: Might be a good idea to sanity check the argument and place
776d248c7d7SRobert Watson 	 * an extreme upper bound.
777d248c7d7SRobert Watson 	 */
778d248c7d7SRobert Watson 	if (i < -1)
779d248c7d7SRobert Watson 		return (EINVAL);
780603724d3SBjoern A. Zeeb 	V_maxnipq = i;
781d248c7d7SRobert Watson 	maxnipq_update();
782d248c7d7SRobert Watson 	return (0);
783d248c7d7SRobert Watson }
784d248c7d7SRobert Watson 
785d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
786d248c7d7SRobert Watson     NULL, 0, sysctl_maxnipq, "I",
787d248c7d7SRobert Watson     "Maximum number of IPv4 fragment reassembly queue entries");
788d248c7d7SRobert Watson 
789d248c7d7SRobert Watson /*
7908948e4baSArchie Cobbs  * Take incoming datagram fragment and try to reassemble it into
791f0cada84SAndre Oppermann  * whole datagram.  If the argument is the first fragment or one
792f0cada84SAndre Oppermann  * in between the function will return NULL and store the mbuf
793f0cada84SAndre Oppermann  * in the fragment chain.  If the argument is the last fragment
794f0cada84SAndre Oppermann  * the packet will be reassembled and the pointer to the new
795f0cada84SAndre Oppermann  * mbuf returned for further processing.  Only m_tags attached
796f0cada84SAndre Oppermann  * to the first packet/fragment are preserved.
797f0cada84SAndre Oppermann  * The IP header is *NOT* adjusted out of iplen.
798df8bae1dSRodney W. Grimes  */
799f0cada84SAndre Oppermann struct mbuf *
800f0cada84SAndre Oppermann ip_reass(struct mbuf *m)
801df8bae1dSRodney W. Grimes {
8028b615593SMarko Zec 	INIT_VNET_INET(curvnet);
803f0cada84SAndre Oppermann 	struct ip *ip;
804f0cada84SAndre Oppermann 	struct mbuf *p, *q, *nq, *t;
805f0cada84SAndre Oppermann 	struct ipq *fp = NULL;
806f0cada84SAndre Oppermann 	struct ipqhead *head;
807f0cada84SAndre Oppermann 	int i, hlen, next;
80859dfcba4SHajimu UMEMOTO 	u_int8_t ecn, ecn0;
809f0cada84SAndre Oppermann 	u_short hash;
810df8bae1dSRodney W. Grimes 
811800af1fbSMaxim Konovalov 	/* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
812603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0 || V_maxfragsperpacket == 0) {
813603724d3SBjoern A. Zeeb 		V_ipstat.ips_fragments++;
814603724d3SBjoern A. Zeeb 		V_ipstat.ips_fragdropped++;
8159d804f81SAndre Oppermann 		m_freem(m);
8169d804f81SAndre Oppermann 		return (NULL);
817f0cada84SAndre Oppermann 	}
8182fad1e93SSam Leffler 
819f0cada84SAndre Oppermann 	ip = mtod(m, struct ip *);
820f0cada84SAndre Oppermann 	hlen = ip->ip_hl << 2;
821f0cada84SAndre Oppermann 
822f0cada84SAndre Oppermann 	hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
823603724d3SBjoern A. Zeeb 	head = &V_ipq[hash];
824f0cada84SAndre Oppermann 	IPQ_LOCK();
825f0cada84SAndre Oppermann 
826f0cada84SAndre Oppermann 	/*
827f0cada84SAndre Oppermann 	 * Look for queue of fragments
828f0cada84SAndre Oppermann 	 * of this datagram.
829f0cada84SAndre Oppermann 	 */
830f0cada84SAndre Oppermann 	TAILQ_FOREACH(fp, head, ipq_list)
831f0cada84SAndre Oppermann 		if (ip->ip_id == fp->ipq_id &&
832f0cada84SAndre Oppermann 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
833f0cada84SAndre Oppermann 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
834f0cada84SAndre Oppermann #ifdef MAC
83530d239bcSRobert Watson 		    mac_ipq_match(m, fp) &&
836f0cada84SAndre Oppermann #endif
837f0cada84SAndre Oppermann 		    ip->ip_p == fp->ipq_p)
838f0cada84SAndre Oppermann 			goto found;
839f0cada84SAndre Oppermann 
840f0cada84SAndre Oppermann 	fp = NULL;
841f0cada84SAndre Oppermann 
842f0cada84SAndre Oppermann 	/*
843d248c7d7SRobert Watson 	 * Attempt to trim the number of allocated fragment queues if it
844d248c7d7SRobert Watson 	 * exceeds the administrative limit.
845f0cada84SAndre Oppermann 	 */
846603724d3SBjoern A. Zeeb 	if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) {
847f0cada84SAndre Oppermann 		/*
848f0cada84SAndre Oppermann 		 * drop something from the tail of the current queue
849f0cada84SAndre Oppermann 		 * before proceeding further
850f0cada84SAndre Oppermann 		 */
851f0cada84SAndre Oppermann 		struct ipq *q = TAILQ_LAST(head, ipqhead);
852f0cada84SAndre Oppermann 		if (q == NULL) {   /* gak */
853f0cada84SAndre Oppermann 			for (i = 0; i < IPREASS_NHASH; i++) {
854603724d3SBjoern A. Zeeb 				struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead);
855f0cada84SAndre Oppermann 				if (r) {
856ac957cd2SJulian Elischer 					V_ipstat.ips_fragtimeout +=
857ac957cd2SJulian Elischer 					    r->ipq_nfrags;
858603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], r);
859f0cada84SAndre Oppermann 					break;
860f0cada84SAndre Oppermann 				}
861f0cada84SAndre Oppermann 			}
862f0cada84SAndre Oppermann 		} else {
863603724d3SBjoern A. Zeeb 			V_ipstat.ips_fragtimeout += q->ipq_nfrags;
864f0cada84SAndre Oppermann 			ip_freef(head, q);
865f0cada84SAndre Oppermann 		}
866f0cada84SAndre Oppermann 	}
867f0cada84SAndre Oppermann 
868f0cada84SAndre Oppermann found:
869f0cada84SAndre Oppermann 	/*
870f0cada84SAndre Oppermann 	 * Adjust ip_len to not reflect header,
871f0cada84SAndre Oppermann 	 * convert offset of this to bytes.
872f0cada84SAndre Oppermann 	 */
873f0cada84SAndre Oppermann 	ip->ip_len -= hlen;
874f0cada84SAndre Oppermann 	if (ip->ip_off & IP_MF) {
875f0cada84SAndre Oppermann 		/*
876f0cada84SAndre Oppermann 		 * Make sure that fragments have a data length
877f0cada84SAndre Oppermann 		 * that's a non-zero multiple of 8 bytes.
878f0cada84SAndre Oppermann 		 */
879f0cada84SAndre Oppermann 		if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
880603724d3SBjoern A. Zeeb 			V_ipstat.ips_toosmall++; /* XXX */
881f0cada84SAndre Oppermann 			goto dropfrag;
882f0cada84SAndre Oppermann 		}
883f0cada84SAndre Oppermann 		m->m_flags |= M_FRAG;
884f0cada84SAndre Oppermann 	} else
885f0cada84SAndre Oppermann 		m->m_flags &= ~M_FRAG;
886f0cada84SAndre Oppermann 	ip->ip_off <<= 3;
887f0cada84SAndre Oppermann 
888f0cada84SAndre Oppermann 
889f0cada84SAndre Oppermann 	/*
890f0cada84SAndre Oppermann 	 * Attempt reassembly; if it succeeds, proceed.
891f0cada84SAndre Oppermann 	 * ip_reass() will return a different mbuf.
892f0cada84SAndre Oppermann 	 */
893603724d3SBjoern A. Zeeb 	V_ipstat.ips_fragments++;
894f0cada84SAndre Oppermann 	m->m_pkthdr.header = ip;
895f0cada84SAndre Oppermann 
896f0cada84SAndre Oppermann 	/* Previous ip_reass() started here. */
897df8bae1dSRodney W. Grimes 	/*
898df8bae1dSRodney W. Grimes 	 * Presence of header sizes in mbufs
899df8bae1dSRodney W. Grimes 	 * would confuse code below.
900df8bae1dSRodney W. Grimes 	 */
901df8bae1dSRodney W. Grimes 	m->m_data += hlen;
902df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
903df8bae1dSRodney W. Grimes 
904df8bae1dSRodney W. Grimes 	/*
905df8bae1dSRodney W. Grimes 	 * If first fragment to arrive, create a reassembly queue.
906df8bae1dSRodney W. Grimes 	 */
907042bbfa3SRobert Watson 	if (fp == NULL) {
908603724d3SBjoern A. Zeeb 		fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
909d248c7d7SRobert Watson 		if (fp == NULL)
910df8bae1dSRodney W. Grimes 			goto dropfrag;
91136b0360bSRobert Watson #ifdef MAC
91230d239bcSRobert Watson 		if (mac_ipq_init(fp, M_NOWAIT) != 0) {
913603724d3SBjoern A. Zeeb 			uma_zfree(V_ipq_zone, fp);
9141d7d0bfeSPawel Jakub Dawidek 			fp = NULL;
9155e7ce478SRobert Watson 			goto dropfrag;
9165e7ce478SRobert Watson 		}
91730d239bcSRobert Watson 		mac_ipq_create(m, fp);
91836b0360bSRobert Watson #endif
919462b86feSPoul-Henning Kamp 		TAILQ_INSERT_HEAD(head, fp, ipq_list);
920603724d3SBjoern A. Zeeb 		V_nipq++;
921375386e2SMike Silbersack 		fp->ipq_nfrags = 1;
922df8bae1dSRodney W. Grimes 		fp->ipq_ttl = IPFRAGTTL;
923df8bae1dSRodney W. Grimes 		fp->ipq_p = ip->ip_p;
924df8bae1dSRodney W. Grimes 		fp->ipq_id = ip->ip_id;
9256effc713SDoug Rabson 		fp->ipq_src = ip->ip_src;
9266effc713SDoug Rabson 		fp->ipq_dst = ip->ip_dst;
927af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
928af38c68cSLuigi Rizzo 		m->m_nextpkt = NULL;
929800af1fbSMaxim Konovalov 		goto done;
93036b0360bSRobert Watson 	} else {
931375386e2SMike Silbersack 		fp->ipq_nfrags++;
93236b0360bSRobert Watson #ifdef MAC
93330d239bcSRobert Watson 		mac_ipq_update(m, fp);
93436b0360bSRobert Watson #endif
935df8bae1dSRodney W. Grimes 	}
936df8bae1dSRodney W. Grimes 
9376effc713SDoug Rabson #define GETIP(m)	((struct ip*)((m)->m_pkthdr.header))
9386effc713SDoug Rabson 
939df8bae1dSRodney W. Grimes 	/*
94059dfcba4SHajimu UMEMOTO 	 * Handle ECN by comparing this segment with the first one;
94159dfcba4SHajimu UMEMOTO 	 * if CE is set, do not lose CE.
94259dfcba4SHajimu UMEMOTO 	 * drop if CE and not-ECT are mixed for the same packet.
94359dfcba4SHajimu UMEMOTO 	 */
94459dfcba4SHajimu UMEMOTO 	ecn = ip->ip_tos & IPTOS_ECN_MASK;
94559dfcba4SHajimu UMEMOTO 	ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
94659dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_CE) {
94759dfcba4SHajimu UMEMOTO 		if (ecn0 == IPTOS_ECN_NOTECT)
94859dfcba4SHajimu UMEMOTO 			goto dropfrag;
94959dfcba4SHajimu UMEMOTO 		if (ecn0 != IPTOS_ECN_CE)
95059dfcba4SHajimu UMEMOTO 			GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
95159dfcba4SHajimu UMEMOTO 	}
95259dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
95359dfcba4SHajimu UMEMOTO 		goto dropfrag;
95459dfcba4SHajimu UMEMOTO 
95559dfcba4SHajimu UMEMOTO 	/*
956df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
957df8bae1dSRodney W. Grimes 	 */
9586effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
9596effc713SDoug Rabson 		if (GETIP(q)->ip_off > ip->ip_off)
960df8bae1dSRodney W. Grimes 			break;
961df8bae1dSRodney W. Grimes 
962df8bae1dSRodney W. Grimes 	/*
963df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
964df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
965af38c68cSLuigi Rizzo 	 * segment.  If it provides all of our data, drop us, otherwise
966af38c68cSLuigi Rizzo 	 * stick new segment in the proper place.
967db4f9cc7SJonathan Lemon 	 *
968db4f9cc7SJonathan Lemon 	 * If some of the data is dropped from the the preceding
969db4f9cc7SJonathan Lemon 	 * segment, then it's checksum is invalidated.
970df8bae1dSRodney W. Grimes 	 */
9716effc713SDoug Rabson 	if (p) {
9726effc713SDoug Rabson 		i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
973df8bae1dSRodney W. Grimes 		if (i > 0) {
974df8bae1dSRodney W. Grimes 			if (i >= ip->ip_len)
975df8bae1dSRodney W. Grimes 				goto dropfrag;
9766a800098SYoshinobu Inoue 			m_adj(m, i);
977db4f9cc7SJonathan Lemon 			m->m_pkthdr.csum_flags = 0;
978df8bae1dSRodney W. Grimes 			ip->ip_off += i;
979df8bae1dSRodney W. Grimes 			ip->ip_len -= i;
980df8bae1dSRodney W. Grimes 		}
981af38c68cSLuigi Rizzo 		m->m_nextpkt = p->m_nextpkt;
982af38c68cSLuigi Rizzo 		p->m_nextpkt = m;
983af38c68cSLuigi Rizzo 	} else {
984af38c68cSLuigi Rizzo 		m->m_nextpkt = fp->ipq_frags;
985af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
986df8bae1dSRodney W. Grimes 	}
987df8bae1dSRodney W. Grimes 
988df8bae1dSRodney W. Grimes 	/*
989df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
990df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
991df8bae1dSRodney W. Grimes 	 */
9926effc713SDoug Rabson 	for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
993af38c68cSLuigi Rizzo 	     q = nq) {
994b36f5b37SMaxim Konovalov 		i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
9956effc713SDoug Rabson 		if (i < GETIP(q)->ip_len) {
9966effc713SDoug Rabson 			GETIP(q)->ip_len -= i;
9976effc713SDoug Rabson 			GETIP(q)->ip_off += i;
9986effc713SDoug Rabson 			m_adj(q, i);
999db4f9cc7SJonathan Lemon 			q->m_pkthdr.csum_flags = 0;
1000df8bae1dSRodney W. Grimes 			break;
1001df8bae1dSRodney W. Grimes 		}
10026effc713SDoug Rabson 		nq = q->m_nextpkt;
1003af38c68cSLuigi Rizzo 		m->m_nextpkt = nq;
1004603724d3SBjoern A. Zeeb 		V_ipstat.ips_fragdropped++;
1005375386e2SMike Silbersack 		fp->ipq_nfrags--;
10066effc713SDoug Rabson 		m_freem(q);
1007df8bae1dSRodney W. Grimes 	}
1008df8bae1dSRodney W. Grimes 
1009df8bae1dSRodney W. Grimes 	/*
1010375386e2SMike Silbersack 	 * Check for complete reassembly and perform frag per packet
1011375386e2SMike Silbersack 	 * limiting.
1012375386e2SMike Silbersack 	 *
1013375386e2SMike Silbersack 	 * Frag limiting is performed here so that the nth frag has
1014375386e2SMike Silbersack 	 * a chance to complete the packet before we drop the packet.
1015375386e2SMike Silbersack 	 * As a result, n+1 frags are actually allowed per packet, but
1016375386e2SMike Silbersack 	 * only n will ever be stored. (n = maxfragsperpacket.)
1017375386e2SMike Silbersack 	 *
1018df8bae1dSRodney W. Grimes 	 */
10196effc713SDoug Rabson 	next = 0;
10206effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
1021375386e2SMike Silbersack 		if (GETIP(q)->ip_off != next) {
1022603724d3SBjoern A. Zeeb 			if (fp->ipq_nfrags > V_maxfragsperpacket) {
1023603724d3SBjoern A. Zeeb 				V_ipstat.ips_fragdropped += fp->ipq_nfrags;
1024375386e2SMike Silbersack 				ip_freef(head, fp);
102599e8617dSMaxim Konovalov 			}
1026f0cada84SAndre Oppermann 			goto done;
1027375386e2SMike Silbersack 		}
10286effc713SDoug Rabson 		next += GETIP(q)->ip_len;
10296effc713SDoug Rabson 	}
10306effc713SDoug Rabson 	/* Make sure the last packet didn't have the IP_MF flag */
1031375386e2SMike Silbersack 	if (p->m_flags & M_FRAG) {
1032603724d3SBjoern A. Zeeb 		if (fp->ipq_nfrags > V_maxfragsperpacket) {
1033603724d3SBjoern A. Zeeb 			V_ipstat.ips_fragdropped += fp->ipq_nfrags;
1034375386e2SMike Silbersack 			ip_freef(head, fp);
103599e8617dSMaxim Konovalov 		}
1036f0cada84SAndre Oppermann 		goto done;
1037375386e2SMike Silbersack 	}
1038df8bae1dSRodney W. Grimes 
1039df8bae1dSRodney W. Grimes 	/*
1040430d30d8SBill Fenner 	 * Reassembly is complete.  Make sure the packet is a sane size.
1041430d30d8SBill Fenner 	 */
10426effc713SDoug Rabson 	q = fp->ipq_frags;
10436effc713SDoug Rabson 	ip = GETIP(q);
104453be11f6SPoul-Henning Kamp 	if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
1045603724d3SBjoern A. Zeeb 		V_ipstat.ips_toolong++;
1046603724d3SBjoern A. Zeeb 		V_ipstat.ips_fragdropped += fp->ipq_nfrags;
1047462b86feSPoul-Henning Kamp 		ip_freef(head, fp);
1048f0cada84SAndre Oppermann 		goto done;
1049430d30d8SBill Fenner 	}
1050430d30d8SBill Fenner 
1051430d30d8SBill Fenner 	/*
1052430d30d8SBill Fenner 	 * Concatenate fragments.
1053df8bae1dSRodney W. Grimes 	 */
10546effc713SDoug Rabson 	m = q;
1055df8bae1dSRodney W. Grimes 	t = m->m_next;
105602410549SRobert Watson 	m->m_next = NULL;
1057df8bae1dSRodney W. Grimes 	m_cat(m, t);
10586effc713SDoug Rabson 	nq = q->m_nextpkt;
105902410549SRobert Watson 	q->m_nextpkt = NULL;
10606effc713SDoug Rabson 	for (q = nq; q != NULL; q = nq) {
10616effc713SDoug Rabson 		nq = q->m_nextpkt;
1062945aa40dSDoug Rabson 		q->m_nextpkt = NULL;
1063db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1064db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1065a8db1d93SJonathan Lemon 		m_cat(m, q);
1066df8bae1dSRodney W. Grimes 	}
10676edb555dSOleg Bulyzhin 	/*
10686edb555dSOleg Bulyzhin 	 * In order to do checksumming faster we do 'end-around carry' here
10696edb555dSOleg Bulyzhin 	 * (and not in for{} loop), though it implies we are not going to
10706edb555dSOleg Bulyzhin 	 * reassemble more than 64k fragments.
10716edb555dSOleg Bulyzhin 	 */
10726edb555dSOleg Bulyzhin 	m->m_pkthdr.csum_data =
10736edb555dSOleg Bulyzhin 	    (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16);
107436b0360bSRobert Watson #ifdef MAC
107530d239bcSRobert Watson 	mac_ipq_reassemble(fp, m);
107630d239bcSRobert Watson 	mac_ipq_destroy(fp);
107736b0360bSRobert Watson #endif
1078df8bae1dSRodney W. Grimes 
1079df8bae1dSRodney W. Grimes 	/*
1080f0cada84SAndre Oppermann 	 * Create header for new ip packet by modifying header of first
1081f0cada84SAndre Oppermann 	 * packet;  dequeue and discard fragment reassembly header.
1082df8bae1dSRodney W. Grimes 	 * Make header visible.
1083df8bae1dSRodney W. Grimes 	 */
1084f0cada84SAndre Oppermann 	ip->ip_len = (ip->ip_hl << 2) + next;
10856effc713SDoug Rabson 	ip->ip_src = fp->ipq_src;
10866effc713SDoug Rabson 	ip->ip_dst = fp->ipq_dst;
1087462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(head, fp, ipq_list);
1088603724d3SBjoern A. Zeeb 	V_nipq--;
1089603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
109053be11f6SPoul-Henning Kamp 	m->m_len += (ip->ip_hl << 2);
109153be11f6SPoul-Henning Kamp 	m->m_data -= (ip->ip_hl << 2);
1092df8bae1dSRodney W. Grimes 	/* some debugging cruft by sklower, below, will go away soon */
1093a5554bf0SPoul-Henning Kamp 	if (m->m_flags & M_PKTHDR)	/* XXX this should be done elsewhere */
1094a5554bf0SPoul-Henning Kamp 		m_fixhdr(m);
1095603724d3SBjoern A. Zeeb 	V_ipstat.ips_reassembled++;
1096f0cada84SAndre Oppermann 	IPQ_UNLOCK();
10976a800098SYoshinobu Inoue 	return (m);
1098df8bae1dSRodney W. Grimes 
1099df8bae1dSRodney W. Grimes dropfrag:
1100603724d3SBjoern A. Zeeb 	V_ipstat.ips_fragdropped++;
1101042bbfa3SRobert Watson 	if (fp != NULL)
1102375386e2SMike Silbersack 		fp->ipq_nfrags--;
1103df8bae1dSRodney W. Grimes 	m_freem(m);
1104f0cada84SAndre Oppermann done:
1105f0cada84SAndre Oppermann 	IPQ_UNLOCK();
1106f0cada84SAndre Oppermann 	return (NULL);
11076effc713SDoug Rabson 
11086effc713SDoug Rabson #undef GETIP
1109df8bae1dSRodney W. Grimes }
1110df8bae1dSRodney W. Grimes 
1111df8bae1dSRodney W. Grimes /*
1112df8bae1dSRodney W. Grimes  * Free a fragment reassembly header and all
1113df8bae1dSRodney W. Grimes  * associated datagrams.
1114df8bae1dSRodney W. Grimes  */
11150312fbe9SPoul-Henning Kamp static void
1116f2565d68SRobert Watson ip_freef(struct ipqhead *fhp, struct ipq *fp)
1117df8bae1dSRodney W. Grimes {
11188b615593SMarko Zec 	INIT_VNET_INET(curvnet);
1119f2565d68SRobert Watson 	struct mbuf *q;
1120df8bae1dSRodney W. Grimes 
11212fad1e93SSam Leffler 	IPQ_LOCK_ASSERT();
11222fad1e93SSam Leffler 
11236effc713SDoug Rabson 	while (fp->ipq_frags) {
11246effc713SDoug Rabson 		q = fp->ipq_frags;
11256effc713SDoug Rabson 		fp->ipq_frags = q->m_nextpkt;
11266effc713SDoug Rabson 		m_freem(q);
1127df8bae1dSRodney W. Grimes 	}
1128462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(fhp, fp, ipq_list);
1129603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
1130603724d3SBjoern A. Zeeb 	V_nipq--;
1131df8bae1dSRodney W. Grimes }
1132df8bae1dSRodney W. Grimes 
1133df8bae1dSRodney W. Grimes /*
1134df8bae1dSRodney W. Grimes  * IP timer processing;
1135df8bae1dSRodney W. Grimes  * if a timer expires on a reassembly
1136df8bae1dSRodney W. Grimes  * queue, discard it.
1137df8bae1dSRodney W. Grimes  */
1138df8bae1dSRodney W. Grimes void
1139f2565d68SRobert Watson ip_slowtimo(void)
1140df8bae1dSRodney W. Grimes {
11418b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1142f2565d68SRobert Watson 	struct ipq *fp;
1143194a213eSAndrey A. Chernov 	int i;
1144df8bae1dSRodney W. Grimes 
11452fad1e93SSam Leffler 	IPQ_LOCK();
11468b615593SMarko Zec 	VNET_LIST_RLOCK();
11478b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
11488b615593SMarko Zec 		CURVNET_SET(vnet_iter);
11498b615593SMarko Zec 		INIT_VNET_INET(vnet_iter);
1150194a213eSAndrey A. Chernov 		for (i = 0; i < IPREASS_NHASH; i++) {
1151603724d3SBjoern A. Zeeb 			for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) {
1152462b86feSPoul-Henning Kamp 				struct ipq *fpp;
1153462b86feSPoul-Henning Kamp 
1154462b86feSPoul-Henning Kamp 				fpp = fp;
1155462b86feSPoul-Henning Kamp 				fp = TAILQ_NEXT(fp, ipq_list);
1156462b86feSPoul-Henning Kamp 				if(--fpp->ipq_ttl == 0) {
11578b615593SMarko Zec 					V_ipstat.ips_fragtimeout +=
11588b615593SMarko Zec 					    fpp->ipq_nfrags;
1159603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], fpp);
1160df8bae1dSRodney W. Grimes 				}
1161df8bae1dSRodney W. Grimes 			}
1162194a213eSAndrey A. Chernov 		}
1163690a6055SJesper Skriver 		/*
1164690a6055SJesper Skriver 		 * If we are over the maximum number of fragments
1165690a6055SJesper Skriver 		 * (due to the limit being lowered), drain off
1166690a6055SJesper Skriver 		 * enough to get down to the new limit.
1167690a6055SJesper Skriver 		 */
1168603724d3SBjoern A. Zeeb 		if (V_maxnipq >= 0 && V_nipq > V_maxnipq) {
1169690a6055SJesper Skriver 			for (i = 0; i < IPREASS_NHASH; i++) {
11708b615593SMarko Zec 				while (V_nipq > V_maxnipq &&
11718b615593SMarko Zec 				    !TAILQ_EMPTY(&V_ipq[i])) {
1172603724d3SBjoern A. Zeeb 					V_ipstat.ips_fragdropped +=
1173603724d3SBjoern A. Zeeb 					    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags;
11748b615593SMarko Zec 					ip_freef(&V_ipq[i],
11758b615593SMarko Zec 					    TAILQ_FIRST(&V_ipq[i]));
1176690a6055SJesper Skriver 				}
1177690a6055SJesper Skriver 			}
1178690a6055SJesper Skriver 		}
11798b615593SMarko Zec 		CURVNET_RESTORE();
11808b615593SMarko Zec 	}
11818b615593SMarko Zec 	VNET_LIST_RUNLOCK();
11822fad1e93SSam Leffler 	IPQ_UNLOCK();
1183df8bae1dSRodney W. Grimes }
1184df8bae1dSRodney W. Grimes 
1185df8bae1dSRodney W. Grimes /*
1186df8bae1dSRodney W. Grimes  * Drain off all datagram fragments.
1187df8bae1dSRodney W. Grimes  */
1188df8bae1dSRodney W. Grimes void
1189f2565d68SRobert Watson ip_drain(void)
1190df8bae1dSRodney W. Grimes {
11918b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1192194a213eSAndrey A. Chernov 	int     i;
1193ce29ab3aSGarrett Wollman 
11942fad1e93SSam Leffler 	IPQ_LOCK();
11958b615593SMarko Zec 	VNET_LIST_RLOCK();
11968b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
11978b615593SMarko Zec 		CURVNET_SET(vnet_iter);
11988b615593SMarko Zec 		INIT_VNET_INET(vnet_iter);
1199194a213eSAndrey A. Chernov 		for (i = 0; i < IPREASS_NHASH; i++) {
1200603724d3SBjoern A. Zeeb 			while(!TAILQ_EMPTY(&V_ipq[i])) {
1201603724d3SBjoern A. Zeeb 				V_ipstat.ips_fragdropped +=
1202603724d3SBjoern A. Zeeb 				    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags;
1203603724d3SBjoern A. Zeeb 				ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1204194a213eSAndrey A. Chernov 			}
1205194a213eSAndrey A. Chernov 		}
12068b615593SMarko Zec 		CURVNET_RESTORE();
12078b615593SMarko Zec 	}
12088b615593SMarko Zec 	VNET_LIST_RUNLOCK();
12092fad1e93SSam Leffler 	IPQ_UNLOCK();
1210ce29ab3aSGarrett Wollman 	in_rtqdrain();
1211df8bae1dSRodney W. Grimes }
1212df8bae1dSRodney W. Grimes 
1213df8bae1dSRodney W. Grimes /*
1214de38924dSAndre Oppermann  * The protocol to be inserted into ip_protox[] must be already registered
1215de38924dSAndre Oppermann  * in inetsw[], either statically or through pf_proto_register().
1216de38924dSAndre Oppermann  */
1217de38924dSAndre Oppermann int
1218de38924dSAndre Oppermann ipproto_register(u_char ipproto)
1219de38924dSAndre Oppermann {
1220de38924dSAndre Oppermann 	struct protosw *pr;
1221de38924dSAndre Oppermann 
1222de38924dSAndre Oppermann 	/* Sanity checks. */
1223de38924dSAndre Oppermann 	if (ipproto == 0)
1224de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1225de38924dSAndre Oppermann 
1226de38924dSAndre Oppermann 	/*
1227de38924dSAndre Oppermann 	 * The protocol slot must not be occupied by another protocol
1228de38924dSAndre Oppermann 	 * already.  An index pointing to IPPROTO_RAW is unused.
1229de38924dSAndre Oppermann 	 */
1230de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1231de38924dSAndre Oppermann 	if (pr == NULL)
1232de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1233de38924dSAndre Oppermann 	if (ip_protox[ipproto] != pr - inetsw)	/* IPPROTO_RAW */
1234de38924dSAndre Oppermann 		return (EEXIST);
1235de38924dSAndre Oppermann 
1236de38924dSAndre Oppermann 	/* Find the protocol position in inetsw[] and set the index. */
1237de38924dSAndre Oppermann 	for (pr = inetdomain.dom_protosw;
1238de38924dSAndre Oppermann 	     pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1239de38924dSAndre Oppermann 		if (pr->pr_domain->dom_family == PF_INET &&
1240de38924dSAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol == ipproto) {
1241de38924dSAndre Oppermann 			/* Be careful to only index valid IP protocols. */
1242db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX) {
1243de38924dSAndre Oppermann 				ip_protox[pr->pr_protocol] = pr - inetsw;
1244de38924dSAndre Oppermann 				return (0);
1245de38924dSAndre Oppermann 			} else
1246de38924dSAndre Oppermann 				return (EINVAL);
1247de38924dSAndre Oppermann 		}
1248de38924dSAndre Oppermann 	}
1249de38924dSAndre Oppermann 	return (EPROTONOSUPPORT);
1250de38924dSAndre Oppermann }
1251de38924dSAndre Oppermann 
1252de38924dSAndre Oppermann int
1253de38924dSAndre Oppermann ipproto_unregister(u_char ipproto)
1254de38924dSAndre Oppermann {
1255de38924dSAndre Oppermann 	struct protosw *pr;
1256de38924dSAndre Oppermann 
1257de38924dSAndre Oppermann 	/* Sanity checks. */
1258de38924dSAndre Oppermann 	if (ipproto == 0)
1259de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1260de38924dSAndre Oppermann 
1261de38924dSAndre Oppermann 	/* Check if the protocol was indeed registered. */
1262de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1263de38924dSAndre Oppermann 	if (pr == NULL)
1264de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1265de38924dSAndre Oppermann 	if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
1266de38924dSAndre Oppermann 		return (ENOENT);
1267de38924dSAndre Oppermann 
1268de38924dSAndre Oppermann 	/* Reset the protocol slot to IPPROTO_RAW. */
1269de38924dSAndre Oppermann 	ip_protox[ipproto] = pr - inetsw;
1270de38924dSAndre Oppermann 	return (0);
1271de38924dSAndre Oppermann }
1272de38924dSAndre Oppermann 
1273df8bae1dSRodney W. Grimes /*
1274df8bae1dSRodney W. Grimes  * Given address of next destination (final or next hop),
1275df8bae1dSRodney W. Grimes  * return internet address info of interface to be used to get there.
1276df8bae1dSRodney W. Grimes  */
1277bd714208SRuslan Ermilov struct in_ifaddr *
12788b07e49aSJulian Elischer ip_rtaddr(struct in_addr dst, u_int fibnum)
1279df8bae1dSRodney W. Grimes {
128097d8d152SAndre Oppermann 	struct route sro;
128102c1c707SAndre Oppermann 	struct sockaddr_in *sin;
128202c1c707SAndre Oppermann 	struct in_ifaddr *ifa;
1283df8bae1dSRodney W. Grimes 
12840cfbbe3bSAndre Oppermann 	bzero(&sro, sizeof(sro));
128597d8d152SAndre Oppermann 	sin = (struct sockaddr_in *)&sro.ro_dst;
1286df8bae1dSRodney W. Grimes 	sin->sin_family = AF_INET;
1287df8bae1dSRodney W. Grimes 	sin->sin_len = sizeof(*sin);
1288df8bae1dSRodney W. Grimes 	sin->sin_addr = dst;
12898b07e49aSJulian Elischer 	in_rtalloc_ign(&sro, RTF_CLONING, fibnum);
1290df8bae1dSRodney W. Grimes 
129197d8d152SAndre Oppermann 	if (sro.ro_rt == NULL)
129202410549SRobert Watson 		return (NULL);
129302c1c707SAndre Oppermann 
129497d8d152SAndre Oppermann 	ifa = ifatoia(sro.ro_rt->rt_ifa);
129597d8d152SAndre Oppermann 	RTFREE(sro.ro_rt);
129602410549SRobert Watson 	return (ifa);
1297df8bae1dSRodney W. Grimes }
1298df8bae1dSRodney W. Grimes 
1299df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = {
1300df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1301df8bae1dSRodney W. Grimes 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1302df8bae1dSRodney W. Grimes 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1303df8bae1dSRodney W. Grimes 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1304fcaf9f91SMike Silbersack 	0,		0,		EHOSTUNREACH,	0,
13053b8123b7SJesper Skriver 	ENOPROTOOPT,	ECONNREFUSED
1306df8bae1dSRodney W. Grimes };
1307df8bae1dSRodney W. Grimes 
1308df8bae1dSRodney W. Grimes /*
1309df8bae1dSRodney W. Grimes  * Forward a packet.  If some error occurs return the sender
1310df8bae1dSRodney W. Grimes  * an icmp packet.  Note we can't always generate a meaningful
1311df8bae1dSRodney W. Grimes  * icmp message because icmp doesn't have a large enough repertoire
1312df8bae1dSRodney W. Grimes  * of codes and types.
1313df8bae1dSRodney W. Grimes  *
1314df8bae1dSRodney W. Grimes  * If not forwarding, just drop the packet.  This could be confusing
1315df8bae1dSRodney W. Grimes  * if ipforwarding was zero but some routing protocol was advancing
1316df8bae1dSRodney W. Grimes  * us as a gateway to somewhere.  However, we must let the routing
1317df8bae1dSRodney W. Grimes  * protocol deal with that.
1318df8bae1dSRodney W. Grimes  *
1319df8bae1dSRodney W. Grimes  * The srcrt parameter indicates whether the packet is being forwarded
1320df8bae1dSRodney W. Grimes  * via a source route.
1321df8bae1dSRodney W. Grimes  */
13229b932e9eSAndre Oppermann void
13239b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt)
1324df8bae1dSRodney W. Grimes {
13258b615593SMarko Zec 	INIT_VNET_INET(curvnet);
13262b25acc1SLuigi Rizzo 	struct ip *ip = mtod(m, struct ip *);
13279b932e9eSAndre Oppermann 	struct in_ifaddr *ia = NULL;
1328df8bae1dSRodney W. Grimes 	struct mbuf *mcopy;
13299b932e9eSAndre Oppermann 	struct in_addr dest;
1330b835b6feSBjoern A. Zeeb 	struct route ro;
1331c773494eSAndre Oppermann 	int error, type = 0, code = 0, mtu = 0;
13323efc3014SJulian Elischer 
13339b932e9eSAndre Oppermann 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1334603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantforward++;
1335df8bae1dSRodney W. Grimes 		m_freem(m);
1336df8bae1dSRodney W. Grimes 		return;
1337df8bae1dSRodney W. Grimes 	}
13381b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
1339603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
13401b968362SDag-Erling Smørgrav #endif
1341df8bae1dSRodney W. Grimes 		if (ip->ip_ttl <= IPTTLDEC) {
13421b968362SDag-Erling Smørgrav 			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
134302c1c707SAndre Oppermann 			    0, 0);
1344df8bae1dSRodney W. Grimes 			return;
1345df8bae1dSRodney W. Grimes 		}
13461b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
13471b968362SDag-Erling Smørgrav 	}
13481b968362SDag-Erling Smørgrav #endif
1349df8bae1dSRodney W. Grimes 
13508b07e49aSJulian Elischer 	ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
1351d23d475fSGuido van Rooij 	if (!srcrt && ia == NULL) {
135202c1c707SAndre Oppermann 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1353df8bae1dSRodney W. Grimes 		return;
135402c1c707SAndre Oppermann 	}
1355df8bae1dSRodney W. Grimes 
1356df8bae1dSRodney W. Grimes 	/*
1357bfef7ed4SIan Dowse 	 * Save the IP header and at most 8 bytes of the payload,
1358bfef7ed4SIan Dowse 	 * in case we need to generate an ICMP message to the src.
1359bfef7ed4SIan Dowse 	 *
13604d2e3692SLuigi Rizzo 	 * XXX this can be optimized a lot by saving the data in a local
13614d2e3692SLuigi Rizzo 	 * buffer on the stack (72 bytes at most), and only allocating the
13624d2e3692SLuigi Rizzo 	 * mbuf if really necessary. The vast majority of the packets
13634d2e3692SLuigi Rizzo 	 * are forwarded without having to send an ICMP back (either
13644d2e3692SLuigi Rizzo 	 * because unnecessary, or because rate limited), so we are
13654d2e3692SLuigi Rizzo 	 * really we are wasting a lot of work here.
13664d2e3692SLuigi Rizzo 	 *
1367bfef7ed4SIan Dowse 	 * We don't use m_copy() because it might return a reference
1368bfef7ed4SIan Dowse 	 * to a shared cluster. Both this function and ip_output()
1369bfef7ed4SIan Dowse 	 * assume exclusive access to the IP header in `m', so any
1370bfef7ed4SIan Dowse 	 * data in a cluster may change before we reach icmp_error().
1371df8bae1dSRodney W. Grimes 	 */
1372780b2f69SAndre Oppermann 	MGETHDR(mcopy, M_DONTWAIT, m->m_type);
1373a163d034SWarner Losh 	if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) {
13749967cafcSSam Leffler 		/*
13759967cafcSSam Leffler 		 * It's probably ok if the pkthdr dup fails (because
13769967cafcSSam Leffler 		 * the deep copy of the tag chain failed), but for now
13779967cafcSSam Leffler 		 * be conservative and just discard the copy since
13789967cafcSSam Leffler 		 * code below may some day want the tags.
13799967cafcSSam Leffler 		 */
13809967cafcSSam Leffler 		m_free(mcopy);
13819967cafcSSam Leffler 		mcopy = NULL;
13829967cafcSSam Leffler 	}
1383bfef7ed4SIan Dowse 	if (mcopy != NULL) {
1384780b2f69SAndre Oppermann 		mcopy->m_len = min(ip->ip_len, M_TRAILINGSPACE(mcopy));
1385e6b0a570SBruce M Simpson 		mcopy->m_pkthdr.len = mcopy->m_len;
1386bfef7ed4SIan Dowse 		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1387bfef7ed4SIan Dowse 	}
138804287599SRuslan Ermilov 
138904287599SRuslan Ermilov #ifdef IPSTEALTH
1390603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
139104287599SRuslan Ermilov #endif
139204287599SRuslan Ermilov 		ip->ip_ttl -= IPTTLDEC;
139304287599SRuslan Ermilov #ifdef IPSTEALTH
139404287599SRuslan Ermilov 	}
139504287599SRuslan Ermilov #endif
1396df8bae1dSRodney W. Grimes 
1397df8bae1dSRodney W. Grimes 	/*
1398df8bae1dSRodney W. Grimes 	 * If forwarding packet using same interface that it came in on,
1399df8bae1dSRodney W. Grimes 	 * perhaps should send a redirect to sender to shortcut a hop.
1400df8bae1dSRodney W. Grimes 	 * Only send redirect if source is sending directly to us,
1401df8bae1dSRodney W. Grimes 	 * and if packet was not source routed (or has any options).
1402df8bae1dSRodney W. Grimes 	 * Also, don't send redirect if forwarding using a default route
1403df8bae1dSRodney W. Grimes 	 * or a route modified by a redirect.
1404df8bae1dSRodney W. Grimes 	 */
14059b932e9eSAndre Oppermann 	dest.s_addr = 0;
1406603724d3SBjoern A. Zeeb 	if (!srcrt && V_ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) {
140702c1c707SAndre Oppermann 		struct sockaddr_in *sin;
140802c1c707SAndre Oppermann 		struct rtentry *rt;
140902c1c707SAndre Oppermann 
14100cfbbe3bSAndre Oppermann 		bzero(&ro, sizeof(ro));
141102c1c707SAndre Oppermann 		sin = (struct sockaddr_in *)&ro.ro_dst;
141202c1c707SAndre Oppermann 		sin->sin_family = AF_INET;
141302c1c707SAndre Oppermann 		sin->sin_len = sizeof(*sin);
14149b932e9eSAndre Oppermann 		sin->sin_addr = ip->ip_dst;
14158b07e49aSJulian Elischer 		in_rtalloc_ign(&ro, RTF_CLONING, M_GETFIB(m));
141602c1c707SAndre Oppermann 
141702c1c707SAndre Oppermann 		rt = ro.ro_rt;
141802c1c707SAndre Oppermann 
141902c1c707SAndre Oppermann 		if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
14209b932e9eSAndre Oppermann 		    satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1421df8bae1dSRodney W. Grimes #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1422df8bae1dSRodney W. Grimes 			u_long src = ntohl(ip->ip_src.s_addr);
1423df8bae1dSRodney W. Grimes 
1424df8bae1dSRodney W. Grimes 			if (RTA(rt) &&
1425df8bae1dSRodney W. Grimes 			    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1426df8bae1dSRodney W. Grimes 				if (rt->rt_flags & RTF_GATEWAY)
14279b932e9eSAndre Oppermann 					dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1428df8bae1dSRodney W. Grimes 				else
14299b932e9eSAndre Oppermann 					dest.s_addr = ip->ip_dst.s_addr;
1430df8bae1dSRodney W. Grimes 				/* Router requirements says to only send host redirects */
1431df8bae1dSRodney W. Grimes 				type = ICMP_REDIRECT;
1432df8bae1dSRodney W. Grimes 				code = ICMP_REDIRECT_HOST;
1433df8bae1dSRodney W. Grimes 			}
1434df8bae1dSRodney W. Grimes 		}
143502c1c707SAndre Oppermann 		if (rt)
143602c1c707SAndre Oppermann 			RTFREE(rt);
143702c1c707SAndre Oppermann 	}
1438df8bae1dSRodney W. Grimes 
1439b835b6feSBjoern A. Zeeb 	/*
1440b835b6feSBjoern A. Zeeb 	 * Try to cache the route MTU from ip_output so we can consider it for
1441b835b6feSBjoern A. Zeeb 	 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191.
1442b835b6feSBjoern A. Zeeb 	 */
1443b835b6feSBjoern A. Zeeb 	bzero(&ro, sizeof(ro));
1444b835b6feSBjoern A. Zeeb 
1445b835b6feSBjoern A. Zeeb 	error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1446b835b6feSBjoern A. Zeeb 
1447b835b6feSBjoern A. Zeeb 	if (error == EMSGSIZE && ro.ro_rt)
1448b835b6feSBjoern A. Zeeb 		mtu = ro.ro_rt->rt_rmx.rmx_mtu;
1449b835b6feSBjoern A. Zeeb 	if (ro.ro_rt)
1450b835b6feSBjoern A. Zeeb 		RTFREE(ro.ro_rt);
1451b835b6feSBjoern A. Zeeb 
1452df8bae1dSRodney W. Grimes 	if (error)
1453603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantforward++;
1454df8bae1dSRodney W. Grimes 	else {
1455603724d3SBjoern A. Zeeb 		V_ipstat.ips_forward++;
1456df8bae1dSRodney W. Grimes 		if (type)
1457603724d3SBjoern A. Zeeb 			V_ipstat.ips_redirectsent++;
1458df8bae1dSRodney W. Grimes 		else {
14599188b4a1SAndre Oppermann 			if (mcopy)
1460df8bae1dSRodney W. Grimes 				m_freem(mcopy);
1461df8bae1dSRodney W. Grimes 			return;
1462df8bae1dSRodney W. Grimes 		}
1463df8bae1dSRodney W. Grimes 	}
1464df8bae1dSRodney W. Grimes 	if (mcopy == NULL)
1465df8bae1dSRodney W. Grimes 		return;
1466df8bae1dSRodney W. Grimes 
1467df8bae1dSRodney W. Grimes 	switch (error) {
1468df8bae1dSRodney W. Grimes 
1469df8bae1dSRodney W. Grimes 	case 0:				/* forwarded, but need redirect */
1470df8bae1dSRodney W. Grimes 		/* type, code set above */
1471df8bae1dSRodney W. Grimes 		break;
1472df8bae1dSRodney W. Grimes 
1473df8bae1dSRodney W. Grimes 	case ENETUNREACH:		/* shouldn't happen, checked above */
1474df8bae1dSRodney W. Grimes 	case EHOSTUNREACH:
1475df8bae1dSRodney W. Grimes 	case ENETDOWN:
1476df8bae1dSRodney W. Grimes 	case EHOSTDOWN:
1477df8bae1dSRodney W. Grimes 	default:
1478df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1479df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_HOST;
1480df8bae1dSRodney W. Grimes 		break;
1481df8bae1dSRodney W. Grimes 
1482df8bae1dSRodney W. Grimes 	case EMSGSIZE:
1483df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1484df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_NEEDFRAG;
14851dfcf0d2SAndre Oppermann 
1486b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
1487b835b6feSBjoern A. Zeeb 		/*
1488b835b6feSBjoern A. Zeeb 		 * If IPsec is configured for this path,
1489b835b6feSBjoern A. Zeeb 		 * override any possibly mtu value set by ip_output.
1490b835b6feSBjoern A. Zeeb 		 */
1491b835b6feSBjoern A. Zeeb 		mtu = ip_ipsec_mtu(m, mtu);
1492b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
14939b932e9eSAndre Oppermann 		/*
1494b835b6feSBjoern A. Zeeb 		 * If the MTU was set before make sure we are below the
1495b835b6feSBjoern A. Zeeb 		 * interface MTU.
1496ab48768bSAndre Oppermann 		 * If the MTU wasn't set before use the interface mtu or
1497ab48768bSAndre Oppermann 		 * fall back to the next smaller mtu step compared to the
1498ab48768bSAndre Oppermann 		 * current packet size.
14999b932e9eSAndre Oppermann 		 */
1500b835b6feSBjoern A. Zeeb 		if (mtu != 0) {
1501b835b6feSBjoern A. Zeeb 			if (ia != NULL)
1502b835b6feSBjoern A. Zeeb 				mtu = min(mtu, ia->ia_ifp->if_mtu);
1503b835b6feSBjoern A. Zeeb 		} else {
1504ab48768bSAndre Oppermann 			if (ia != NULL)
1505c773494eSAndre Oppermann 				mtu = ia->ia_ifp->if_mtu;
1506ab48768bSAndre Oppermann 			else
1507ab48768bSAndre Oppermann 				mtu = ip_next_mtu(ip->ip_len, 0);
1508ab48768bSAndre Oppermann 		}
1509603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantfrag++;
1510df8bae1dSRodney W. Grimes 		break;
1511df8bae1dSRodney W. Grimes 
1512df8bae1dSRodney W. Grimes 	case ENOBUFS:
1513df285b3dSMike Silbersack 		/*
1514df285b3dSMike Silbersack 		 * A router should not generate ICMP_SOURCEQUENCH as
1515df285b3dSMike Silbersack 		 * required in RFC1812 Requirements for IP Version 4 Routers.
1516df285b3dSMike Silbersack 		 * Source quench could be a big problem under DoS attacks,
1517df285b3dSMike Silbersack 		 * or if the underlying interface is rate-limited.
1518df285b3dSMike Silbersack 		 * Those who need source quench packets may re-enable them
1519df285b3dSMike Silbersack 		 * via the net.inet.ip.sendsourcequench sysctl.
1520df285b3dSMike Silbersack 		 */
1521603724d3SBjoern A. Zeeb 		if (V_ip_sendsourcequench == 0) {
1522df285b3dSMike Silbersack 			m_freem(mcopy);
1523df285b3dSMike Silbersack 			return;
1524df285b3dSMike Silbersack 		} else {
1525df8bae1dSRodney W. Grimes 			type = ICMP_SOURCEQUENCH;
1526df8bae1dSRodney W. Grimes 			code = 0;
1527df285b3dSMike Silbersack 		}
1528df8bae1dSRodney W. Grimes 		break;
15293a06e3e0SRuslan Ermilov 
15303a06e3e0SRuslan Ermilov 	case EACCES:			/* ipfw denied packet */
15313a06e3e0SRuslan Ermilov 		m_freem(mcopy);
15323a06e3e0SRuslan Ermilov 		return;
1533df8bae1dSRodney W. Grimes 	}
1534c773494eSAndre Oppermann 	icmp_error(mcopy, type, code, dest.s_addr, mtu);
1535df8bae1dSRodney W. Grimes }
1536df8bae1dSRodney W. Grimes 
153782c23ebaSBill Fenner void
1538f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1539f2565d68SRobert Watson     struct mbuf *m)
154082c23ebaSBill Fenner {
15418b615593SMarko Zec 	INIT_VNET_NET(inp->inp_vnet);
15428b615593SMarko Zec 
1543be8a62e8SPoul-Henning Kamp 	if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1544be8a62e8SPoul-Henning Kamp 		struct bintime bt;
1545be8a62e8SPoul-Henning Kamp 
1546be8a62e8SPoul-Henning Kamp 		bintime(&bt);
1547be8a62e8SPoul-Henning Kamp 		if (inp->inp_socket->so_options & SO_BINTIME) {
1548be8a62e8SPoul-Henning Kamp 			*mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt),
1549be8a62e8SPoul-Henning Kamp 			SCM_BINTIME, SOL_SOCKET);
1550be8a62e8SPoul-Henning Kamp 			if (*mp)
1551be8a62e8SPoul-Henning Kamp 				mp = &(*mp)->m_next;
1552be8a62e8SPoul-Henning Kamp 		}
155382c23ebaSBill Fenner 		if (inp->inp_socket->so_options & SO_TIMESTAMP) {
155482c23ebaSBill Fenner 			struct timeval tv;
155582c23ebaSBill Fenner 
1556be8a62e8SPoul-Henning Kamp 			bintime2timeval(&bt, &tv);
155782c23ebaSBill Fenner 			*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
155882c23ebaSBill Fenner 				SCM_TIMESTAMP, SOL_SOCKET);
155982c23ebaSBill Fenner 			if (*mp)
156082c23ebaSBill Fenner 				mp = &(*mp)->m_next;
15614cc20ab1SSeigo Tanimura 		}
1562be8a62e8SPoul-Henning Kamp 	}
156382c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVDSTADDR) {
156482c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
156582c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
156682c23ebaSBill Fenner 		if (*mp)
156782c23ebaSBill Fenner 			mp = &(*mp)->m_next;
156882c23ebaSBill Fenner 	}
15694957466bSMatthew N. Dodd 	if (inp->inp_flags & INP_RECVTTL) {
15704957466bSMatthew N. Dodd 		*mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
15714957466bSMatthew N. Dodd 		    sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
15724957466bSMatthew N. Dodd 		if (*mp)
15734957466bSMatthew N. Dodd 			mp = &(*mp)->m_next;
15744957466bSMatthew N. Dodd 	}
157582c23ebaSBill Fenner #ifdef notyet
157682c23ebaSBill Fenner 	/* XXX
157782c23ebaSBill Fenner 	 * Moving these out of udp_input() made them even more broken
157882c23ebaSBill Fenner 	 * than they already were.
157982c23ebaSBill Fenner 	 */
158082c23ebaSBill Fenner 	/* options were tossed already */
158182c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVOPTS) {
158282c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
158382c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
158482c23ebaSBill Fenner 		if (*mp)
158582c23ebaSBill Fenner 			mp = &(*mp)->m_next;
158682c23ebaSBill Fenner 	}
158782c23ebaSBill Fenner 	/* ip_srcroute doesn't do what we want here, need to fix */
158882c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVRETOPTS) {
1589e0982661SAndre Oppermann 		*mp = sbcreatecontrol((caddr_t) ip_srcroute(m),
159082c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
159182c23ebaSBill Fenner 		if (*mp)
159282c23ebaSBill Fenner 			mp = &(*mp)->m_next;
159382c23ebaSBill Fenner 	}
159482c23ebaSBill Fenner #endif
159582c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVIF) {
1596d314ad7bSJulian Elischer 		struct ifnet *ifp;
1597d314ad7bSJulian Elischer 		struct sdlbuf {
159882c23ebaSBill Fenner 			struct sockaddr_dl sdl;
1599d314ad7bSJulian Elischer 			u_char	pad[32];
1600d314ad7bSJulian Elischer 		} sdlbuf;
1601d314ad7bSJulian Elischer 		struct sockaddr_dl *sdp;
1602d314ad7bSJulian Elischer 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
160382c23ebaSBill Fenner 
1604d314ad7bSJulian Elischer 		if (((ifp = m->m_pkthdr.rcvif))
1605603724d3SBjoern A. Zeeb 		&& ( ifp->if_index && (ifp->if_index <= V_if_index))) {
16064a0d6638SRuslan Ermilov 			sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1607d314ad7bSJulian Elischer 			/*
1608d314ad7bSJulian Elischer 			 * Change our mind and don't try copy.
1609d314ad7bSJulian Elischer 			 */
1610d314ad7bSJulian Elischer 			if ((sdp->sdl_family != AF_LINK)
1611d314ad7bSJulian Elischer 			|| (sdp->sdl_len > sizeof(sdlbuf))) {
1612d314ad7bSJulian Elischer 				goto makedummy;
1613d314ad7bSJulian Elischer 			}
1614d314ad7bSJulian Elischer 			bcopy(sdp, sdl2, sdp->sdl_len);
1615d314ad7bSJulian Elischer 		} else {
1616d314ad7bSJulian Elischer makedummy:
1617d314ad7bSJulian Elischer 			sdl2->sdl_len
1618d314ad7bSJulian Elischer 				= offsetof(struct sockaddr_dl, sdl_data[0]);
1619d314ad7bSJulian Elischer 			sdl2->sdl_family = AF_LINK;
1620d314ad7bSJulian Elischer 			sdl2->sdl_index = 0;
1621d314ad7bSJulian Elischer 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1622d314ad7bSJulian Elischer 		}
1623d314ad7bSJulian Elischer 		*mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
162482c23ebaSBill Fenner 			IP_RECVIF, IPPROTO_IP);
162582c23ebaSBill Fenner 		if (*mp)
162682c23ebaSBill Fenner 			mp = &(*mp)->m_next;
162782c23ebaSBill Fenner 	}
162882c23ebaSBill Fenner }
162982c23ebaSBill Fenner 
16304d2e3692SLuigi Rizzo /*
163130916a2dSRobert Watson  * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
163230916a2dSRobert Watson  * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
163330916a2dSRobert Watson  * locking.  This code remains in ip_input.c as ip_mroute.c is optionally
163430916a2dSRobert Watson  * compiled.
16354d2e3692SLuigi Rizzo  */
1636df8bae1dSRodney W. Grimes int
1637f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so)
1638f0068c4aSGarrett Wollman {
16398b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
16408b615593SMarko Zec 
1641f0068c4aSGarrett Wollman 	if (so->so_type != SOCK_RAW ||
1642f0068c4aSGarrett Wollman 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1643f0068c4aSGarrett Wollman 		return EOPNOTSUPP;
1644f0068c4aSGarrett Wollman 
1645603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL)
1646f0068c4aSGarrett Wollman 		return EADDRINUSE;
1647f0068c4aSGarrett Wollman 
1648603724d3SBjoern A. Zeeb 	V_ip_rsvpd = so;
16491c5de19aSGarrett Wollman 	/*
16501c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-increment
16511c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
16521c5de19aSGarrett Wollman 	 */
1653603724d3SBjoern A. Zeeb 	if (!V_ip_rsvp_on) {
1654603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 1;
1655603724d3SBjoern A. Zeeb 		V_rsvp_on++;
16561c5de19aSGarrett Wollman 	}
1657f0068c4aSGarrett Wollman 
1658f0068c4aSGarrett Wollman 	return 0;
1659f0068c4aSGarrett Wollman }
1660f0068c4aSGarrett Wollman 
1661f0068c4aSGarrett Wollman int
1662f0068c4aSGarrett Wollman ip_rsvp_done(void)
1663f0068c4aSGarrett Wollman {
16648b615593SMarko Zec 	INIT_VNET_INET(curvnet);
16658b615593SMarko Zec 
1666603724d3SBjoern A. Zeeb 	V_ip_rsvpd = NULL;
16671c5de19aSGarrett Wollman 	/*
16681c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-decrement
16691c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
16701c5de19aSGarrett Wollman 	 */
1671603724d3SBjoern A. Zeeb 	if (V_ip_rsvp_on) {
1672603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 0;
1673603724d3SBjoern A. Zeeb 		V_rsvp_on--;
16741c5de19aSGarrett Wollman 	}
1675f0068c4aSGarrett Wollman 	return 0;
1676f0068c4aSGarrett Wollman }
1677bbb4330bSLuigi Rizzo 
1678bbb4330bSLuigi Rizzo void
1679bbb4330bSLuigi Rizzo rsvp_input(struct mbuf *m, int off)	/* XXX must fixup manually */
1680bbb4330bSLuigi Rizzo {
16818b615593SMarko Zec 	INIT_VNET_INET(curvnet);
16828b615593SMarko Zec 
1683bbb4330bSLuigi Rizzo 	if (rsvp_input_p) { /* call the real one if loaded */
1684bbb4330bSLuigi Rizzo 		rsvp_input_p(m, off);
1685bbb4330bSLuigi Rizzo 		return;
1686bbb4330bSLuigi Rizzo 	}
1687bbb4330bSLuigi Rizzo 
1688bbb4330bSLuigi Rizzo 	/* Can still get packets with rsvp_on = 0 if there is a local member
1689bbb4330bSLuigi Rizzo 	 * of the group to which the RSVP packet is addressed.  But in this
1690bbb4330bSLuigi Rizzo 	 * case we want to throw the packet away.
1691bbb4330bSLuigi Rizzo 	 */
1692bbb4330bSLuigi Rizzo 
1693603724d3SBjoern A. Zeeb 	if (!V_rsvp_on) {
1694bbb4330bSLuigi Rizzo 		m_freem(m);
1695bbb4330bSLuigi Rizzo 		return;
1696bbb4330bSLuigi Rizzo 	}
1697bbb4330bSLuigi Rizzo 
1698603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL) {
1699bbb4330bSLuigi Rizzo 		rip_input(m, off);
1700bbb4330bSLuigi Rizzo 		return;
1701bbb4330bSLuigi Rizzo 	}
1702bbb4330bSLuigi Rizzo 	/* Drop the packet */
1703bbb4330bSLuigi Rizzo 	m_freem(m);
1704bbb4330bSLuigi Rizzo }
1705