xref: /freebsd/sys/netinet/ip_input.c (revision 0aade26e6d061ecfaecfd3cb9e7a79e45468260b)
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>
6765111ec7SKip Macy #include <net/flowtable.h>
68df8bae1dSRodney W. Grimes 
69df8bae1dSRodney W. Grimes #include <netinet/in.h>
70df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
71b5e8ce9fSBruce Evans #include <netinet/in_var.h>
72df8bae1dSRodney W. Grimes #include <netinet/ip.h>
73df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
74df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
75df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
76ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
7758938916SGarrett Wollman #include <machine/in_cksum.h>
784b79449eSBjoern A. Zeeb #include <netinet/vinet.h>
79a9771948SGleb Smirnoff #ifdef DEV_CARP
80a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
81a9771948SGleb Smirnoff #endif
82b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
831dfcf0d2SAndre Oppermann #include <netinet/ip_ipsec.h>
84b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
85df8bae1dSRodney W. Grimes 
86f0068c4aSGarrett Wollman #include <sys/socketvar.h>
876ddbf1e2SGary Palmer 
88010b65f5SJulian Elischer /* XXX: Temporary until ipfw_ether and ipfw_bridge are converted. */
89010b65f5SJulian Elischer #include <netinet/ip_fw.h>
90010b65f5SJulian Elischer #include <netinet/ip_dummynet.h>
91010b65f5SJulian Elischer 
92aed55708SRobert Watson #include <security/mac/mac_framework.h>
93aed55708SRobert Watson 
94d2035ffbSEd Maste #ifdef CTASSERT
95d2035ffbSEd Maste CTASSERT(sizeof(struct ip) == 20);
96d2035ffbSEd Maste #endif
97d2035ffbSEd Maste 
98385195c0SMarko Zec #ifndef VIMAGE
99385195c0SMarko Zec #ifndef VIMAGE_GLOBALS
100385195c0SMarko Zec struct vnet_inet vnet_inet_0;
101385195c0SMarko Zec #endif
102385195c0SMarko Zec #endif
103385195c0SMarko Zec 
10444e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
10544e33a07SMarko Zec static int	ipsendredirects;
10644e33a07SMarko Zec static int	ip_checkinterface;
10744e33a07SMarko Zec static int	ip_keepfaith;
10844e33a07SMarko Zec static int	ip_sendsourcequench;
10944e33a07SMarko Zec int	ip_defttl;
11044e33a07SMarko Zec int	ip_do_randomid;
11144e33a07SMarko Zec int	ipforwarding;
11244e33a07SMarko Zec struct	in_ifaddrhead in_ifaddrhead; 		/* first inet address */
11344e33a07SMarko Zec struct	in_ifaddrhashhead *in_ifaddrhashtbl;	/* inet addr hash table  */
11444e33a07SMarko Zec u_long 	in_ifaddrhmask;				/* mask for hash table */
11544e33a07SMarko Zec struct ipstat ipstat;
11644e33a07SMarko Zec static int ip_rsvp_on;
11744e33a07SMarko Zec struct socket *ip_rsvpd;
11844e33a07SMarko Zec int	rsvp_on;
119f02493cbSMarko Zec static struct ipqhead ipq[IPREASS_NHASH];
12044e33a07SMarko Zec static int	maxnipq;	/* Administrative limit on # reass queues. */
12144e33a07SMarko Zec static int	maxfragsperpacket;
12244e33a07SMarko Zec int	ipstealth;
12344e33a07SMarko Zec static int	nipq;	/* Total # of reass queues */
12444e33a07SMarko Zec #endif
125f0068c4aSGarrett Wollman 
1268b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_FORWARDING,
1278b615593SMarko Zec     forwarding, CTLFLAG_RW, ipforwarding, 0,
1288b615593SMarko Zec     "Enable IP forwarding between interfaces");
1290312fbe9SPoul-Henning Kamp 
1308b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_SENDREDIRECTS,
1318b615593SMarko Zec     redirect, CTLFLAG_RW, ipsendredirects, 0,
1328b615593SMarko Zec     "Enable sending IP redirects");
1330312fbe9SPoul-Henning Kamp 
1348b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_DEFTTL,
1358b615593SMarko Zec     ttl, CTLFLAG_RW, ip_defttl, 0, "Maximum TTL on IP packets");
1360312fbe9SPoul-Henning Kamp 
1378b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_KEEPFAITH,
1388b615593SMarko Zec     keepfaith, CTLFLAG_RW, ip_keepfaith,	0,
1396a800098SYoshinobu Inoue     "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
1406a800098SYoshinobu Inoue 
1418b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO,
1428b615593SMarko Zec     sendsourcequench, CTLFLAG_RW, ip_sendsourcequench, 0,
143df285b3dSMike Silbersack     "Enable the transmission of source quench packets");
144df285b3dSMike Silbersack 
1458b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, random_id,
1468b615593SMarko Zec     CTLFLAG_RW, ip_do_randomid, 0, "Assign random ip_id values");
1471f44b0a1SDavid Malone 
148823db0e9SDon Lewis /*
149823db0e9SDon Lewis  * XXX - Setting ip_checkinterface mostly implements the receive side of
150823db0e9SDon Lewis  * the Strong ES model described in RFC 1122, but since the routing table
151a8f12100SDon Lewis  * and transmit implementation do not implement the Strong ES model,
152823db0e9SDon Lewis  * setting this to 1 results in an odd hybrid.
1533f67c834SDon Lewis  *
154a8f12100SDon Lewis  * XXX - ip_checkinterface currently must be disabled if you use ipnat
155a8f12100SDon Lewis  * to translate the destination address to another local interface.
1563f67c834SDon Lewis  *
1573f67c834SDon Lewis  * XXX - ip_checkinterface must be disabled if you add IP aliases
1583f67c834SDon Lewis  * to the loopback interface instead of the interface where the
1593f67c834SDon Lewis  * packets for those addresses are received.
160823db0e9SDon Lewis  */
1618b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO,
1628b615593SMarko Zec     check_interface, CTLFLAG_RW, ip_checkinterface, 0,
1638b615593SMarko Zec     "Verify packet arrives on correct interface");
164b3e95d4eSJonathan Lemon 
165c21fd232SAndre Oppermann struct pfil_head inet_pfil_hook;	/* Packet filter hooks */
166df8bae1dSRodney W. Grimes 
1671cafed39SJonathan Lemon static struct	ifqueue ipintrq;
168ca925d9cSJonathan Lemon static int	ipqmaxlen = IFQ_MAXLEN;
169ca925d9cSJonathan Lemon 
170df8bae1dSRodney W. Grimes extern	struct domain inetdomain;
171f0ffb944SJulian Elischer extern	struct protosw inetsw[];
172df8bae1dSRodney W. Grimes u_char	ip_protox[IPPROTO_MAX];
173ca925d9cSJonathan Lemon 
174afed1375SDavid Greenman SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
1753d177f46SBill Fumerola     &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
1760312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
1776489fe65SAndre Oppermann     &ipintrq.ifq_drops, 0,
1786489fe65SAndre Oppermann     "Number of packets dropped from the IP input queue");
179df8bae1dSRodney W. Grimes 
1808b615593SMarko Zec SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
1818b615593SMarko Zec     ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
182194a213eSAndrey A. Chernov 
183385195c0SMarko Zec #ifdef VIMAGE_GLOBALS
184d248c7d7SRobert Watson static uma_zone_t ipq_zone;
185385195c0SMarko Zec #endif
186dfa60d93SRobert Watson static struct mtx ipqlock;
1872fad1e93SSam Leffler 
1882fad1e93SSam Leffler #define	IPQ_LOCK()	mtx_lock(&ipqlock)
1892fad1e93SSam Leffler #define	IPQ_UNLOCK()	mtx_unlock(&ipqlock)
190888c2a3cSSam Leffler #define	IPQ_LOCK_INIT()	mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
191888c2a3cSSam Leffler #define	IPQ_LOCK_ASSERT()	mtx_assert(&ipqlock, MA_OWNED)
192f23b4c91SGarrett Wollman 
193d248c7d7SRobert Watson static void	maxnipq_update(void);
1944f590175SPaul Saab static void	ipq_zone_change(void *);
195d248c7d7SRobert Watson 
1968b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, fragpackets,
1978b615593SMarko Zec     CTLFLAG_RD, nipq, 0,
1988b615593SMarko Zec     "Current number of IPv4 fragment reassembly queue entries");
199d248c7d7SRobert Watson 
2008b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, maxfragsperpacket,
2018b615593SMarko Zec     CTLFLAG_RW, maxfragsperpacket, 0,
202d248c7d7SRobert Watson     "Maximum number of IPv4 fragments allowed per packet");
203d248c7d7SRobert Watson 
204d248c7d7SRobert Watson struct callout	ipport_tick_callout;
205d248c7d7SRobert Watson 
2060312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU
2070312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
2083d177f46SBill Fumerola     &ip_mtu, 0, "Default MTU");
2090312fbe9SPoul-Henning Kamp #endif
2100312fbe9SPoul-Henning Kamp 
2111b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
2128b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
2138b615593SMarko Zec     ipstealth, 0, "IP stealth mode, no TTL decrementation on forwarding");
2141b968362SDag-Erling Smørgrav #endif
21565111ec7SKip Macy static int ip_output_flowtable_size = 2048;
21665111ec7SKip Macy TUNABLE_INT("net.inet.ip.output_flowtable_size", &ip_output_flowtable_size);
21765111ec7SKip Macy SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, output_flowtable_size,
21865111ec7SKip Macy     CTLFLAG_RDTUN, ip_output_flowtable_size, 2048,
21965111ec7SKip Macy     "number of entries in the per-cpu output flow caches");
2201b968362SDag-Erling Smørgrav 
221010b65f5SJulian Elischer /*
222010b65f5SJulian Elischer  * ipfw_ether and ipfw_bridge hooks.
223010b65f5SJulian Elischer  * XXX: Temporary until those are converted to pfil_hooks as well.
224010b65f5SJulian Elischer  */
225010b65f5SJulian Elischer ip_fw_chk_t *ip_fw_chk_ptr = NULL;
226010b65f5SJulian Elischer ip_dn_io_t *ip_dn_io_ptr = NULL;
227385195c0SMarko Zec #ifdef VIMAGE_GLOBALS
228385195c0SMarko Zec int fw_one_pass;
229385195c0SMarko Zec #endif
23065111ec7SKip Macy struct flowtable *ip_ft;
231010b65f5SJulian Elischer 
2324d77a549SAlfred Perlstein static void	ip_freef(struct ipqhead *, struct ipq *);
2338948e4baSArchie Cobbs 
234bfe1aba4SMarko Zec #ifndef VIMAGE_GLOBALS
235bfe1aba4SMarko Zec static void vnet_inet_register(void);
236bfe1aba4SMarko Zec 
237bfe1aba4SMarko Zec static const vnet_modinfo_t vnet_inet_modinfo = {
238bfe1aba4SMarko Zec 	.vmi_id		= VNET_MOD_INET,
239bfe1aba4SMarko Zec 	.vmi_name	= "inet",
240bfe1aba4SMarko Zec };
241bfe1aba4SMarko Zec 
242bfe1aba4SMarko Zec static void vnet_inet_register()
243bfe1aba4SMarko Zec {
244bfe1aba4SMarko Zec 
245bfe1aba4SMarko Zec 	vnet_mod_register(&vnet_inet_modinfo);
246bfe1aba4SMarko Zec }
247bfe1aba4SMarko Zec 
248bfe1aba4SMarko Zec SYSINIT(inet, SI_SUB_PROTO_BEGIN, SI_ORDER_FIRST, vnet_inet_register, 0);
249bfe1aba4SMarko Zec #endif
250bfe1aba4SMarko Zec 
251df8bae1dSRodney W. Grimes /*
252df8bae1dSRodney W. Grimes  * IP initialization: fill in IP protocol switch table.
253df8bae1dSRodney W. Grimes  * All protocols not implemented in kernel go to raw IP protocol handler.
254df8bae1dSRodney W. Grimes  */
255df8bae1dSRodney W. Grimes void
256f2565d68SRobert Watson ip_init(void)
257df8bae1dSRodney W. Grimes {
2588b615593SMarko Zec 	INIT_VNET_INET(curvnet);
259f2565d68SRobert Watson 	struct protosw *pr;
260f2565d68SRobert Watson 	int i;
261df8bae1dSRodney W. Grimes 
26244e33a07SMarko Zec 	V_ipsendredirects = 1; /* XXX */
26344e33a07SMarko Zec 	V_ip_checkinterface = 0;
26444e33a07SMarko Zec 	V_ip_keepfaith = 0;
26544e33a07SMarko Zec 	V_ip_sendsourcequench = 0;
26644e33a07SMarko Zec 	V_rsvp_on = 0;
26744e33a07SMarko Zec 	V_ip_defttl = IPDEFTTL;
26844e33a07SMarko Zec 	V_ip_do_randomid = 0;
2691ed81b73SMarko Zec 	V_ip_id = time_second & 0xffff;
27044e33a07SMarko Zec 	V_ipforwarding = 0;
27144e33a07SMarko Zec 	V_ipstealth = 0;
27244e33a07SMarko Zec 	V_nipq = 0;	/* Total # of reass queues */
27344e33a07SMarko Zec 
27444e33a07SMarko Zec 	V_ipport_lowfirstauto = IPPORT_RESERVED - 1;	/* 1023 */
27544e33a07SMarko Zec 	V_ipport_lowlastauto = IPPORT_RESERVEDSTART;	/* 600 */
27644e33a07SMarko Zec 	V_ipport_firstauto = IPPORT_EPHEMERALFIRST;	/* 10000 */
27744e33a07SMarko Zec 	V_ipport_lastauto = IPPORT_EPHEMERALLAST;	/* 65535 */
27844e33a07SMarko Zec 	V_ipport_hifirstauto = IPPORT_HIFIRSTAUTO;	/* 49152 */
27944e33a07SMarko Zec 	V_ipport_hilastauto = IPPORT_HILASTAUTO;	/* 65535 */
28044e33a07SMarko Zec 	V_ipport_reservedhigh = IPPORT_RESERVED - 1;	/* 1023 */
28144e33a07SMarko Zec 	V_ipport_reservedlow = 0;
28244e33a07SMarko Zec 	V_ipport_randomized = 1;	/* user controlled via sysctl */
28344e33a07SMarko Zec 	V_ipport_randomcps = 10;	/* user controlled via sysctl */
28444e33a07SMarko Zec 	V_ipport_randomtime = 45;	/* user controlled via sysctl */
28544e33a07SMarko Zec 	V_ipport_stoprandom = 0;	/* toggled by ipport_tick */
28644e33a07SMarko Zec 
287385195c0SMarko Zec 	V_fw_one_pass = 1;
288385195c0SMarko Zec 
28944e33a07SMarko Zec #ifdef NOTYET
29044e33a07SMarko Zec 	/* XXX global static but not instantiated in this file */
29144e33a07SMarko Zec 	V_ipfastforward_active = 0;
29244e33a07SMarko Zec 	V_subnetsarelocal = 0;
29344e33a07SMarko Zec 	V_sameprefixcarponly = 0;
29444e33a07SMarko Zec #endif
29544e33a07SMarko Zec 
296603724d3SBjoern A. Zeeb 	TAILQ_INIT(&V_in_ifaddrhead);
297603724d3SBjoern A. Zeeb 	V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
2981ed81b73SMarko Zec 
2991ed81b73SMarko Zec 	/* Initialize IP reassembly queue. */
3001ed81b73SMarko Zec 	for (i = 0; i < IPREASS_NHASH; i++)
3011ed81b73SMarko Zec 		TAILQ_INIT(&V_ipq[i]);
3021ed81b73SMarko Zec 	V_maxnipq = nmbclusters / 32;
3031ed81b73SMarko Zec 	V_maxfragsperpacket = 16;
3041ed81b73SMarko Zec 	V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
3051ed81b73SMarko Zec 	    NULL, UMA_ALIGN_PTR, 0);
3061ed81b73SMarko Zec 	maxnipq_update();
3071ed81b73SMarko Zec 
3081ed81b73SMarko Zec 	/* Skip initialization of globals for non-default instances. */
3091ed81b73SMarko Zec 	if (!IS_DEFAULT_VNET(curvnet))
3101ed81b73SMarko Zec 		return;
3111ed81b73SMarko Zec 
312f0ffb944SJulian Elischer 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
31302410549SRobert Watson 	if (pr == NULL)
314db09bef3SAndre Oppermann 		panic("ip_init: PF_INET not found");
315db09bef3SAndre Oppermann 
316db09bef3SAndre Oppermann 	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
317df8bae1dSRodney W. Grimes 	for (i = 0; i < IPPROTO_MAX; i++)
318df8bae1dSRodney W. Grimes 		ip_protox[i] = pr - inetsw;
319db09bef3SAndre Oppermann 	/*
320db09bef3SAndre Oppermann 	 * Cycle through IP protocols and put them into the appropriate place
321db09bef3SAndre Oppermann 	 * in ip_protox[].
322db09bef3SAndre Oppermann 	 */
323f0ffb944SJulian Elischer 	for (pr = inetdomain.dom_protosw;
324f0ffb944SJulian Elischer 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
325df8bae1dSRodney W. Grimes 		if (pr->pr_domain->dom_family == PF_INET &&
326db09bef3SAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
327db09bef3SAndre Oppermann 			/* Be careful to only index valid IP protocols. */
328db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX)
329df8bae1dSRodney W. Grimes 				ip_protox[pr->pr_protocol] = pr - inetsw;
330db09bef3SAndre Oppermann 		}
331194a213eSAndrey A. Chernov 
332c21fd232SAndre Oppermann 	/* Initialize packet filter hooks. */
333134ea224SSam Leffler 	inet_pfil_hook.ph_type = PFIL_TYPE_AF;
334134ea224SSam Leffler 	inet_pfil_hook.ph_af = AF_INET;
335134ea224SSam Leffler 	if ((i = pfil_head_register(&inet_pfil_hook)) != 0)
336134ea224SSam Leffler 		printf("%s: WARNING: unable to register pfil hook, "
337134ea224SSam Leffler 			"error %d\n", __func__, i);
338134ea224SSam Leffler 
3395f311da2SMike Silbersack 	/* Start ipport_tick. */
3405f311da2SMike Silbersack 	callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
3415f311da2SMike Silbersack 	ipport_tick(NULL);
3425f311da2SMike Silbersack 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
3435f311da2SMike Silbersack 		SHUTDOWN_PRI_DEFAULT);
3444f590175SPaul Saab 	EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
3454f590175SPaul Saab 		NULL, EVENTHANDLER_PRI_ANY);
3465f311da2SMike Silbersack 
347db09bef3SAndre Oppermann 	/* Initialize various other remaining things. */
3481ed81b73SMarko Zec 	IPQ_LOCK_INIT();
349df8bae1dSRodney W. Grimes 	ipintrq.ifq_maxlen = ipqmaxlen;
3506008862bSJohn Baldwin 	mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF);
35159dd72d0SRobert Watson 	netisr_register(NETISR_IP, ip_input, &ipintrq, 0);
35265111ec7SKip Macy 
35365111ec7SKip Macy 	ip_ft = flowtable_alloc(ip_output_flowtable_size, FL_PCPU);
354df8bae1dSRodney W. Grimes }
355df8bae1dSRodney W. Grimes 
356f2565d68SRobert Watson void
357f2565d68SRobert Watson ip_fini(void *xtp)
3585f311da2SMike Silbersack {
359f2565d68SRobert Watson 
3605f311da2SMike Silbersack 	callout_stop(&ipport_tick_callout);
3615f311da2SMike Silbersack }
3625f311da2SMike Silbersack 
3634d2e3692SLuigi Rizzo /*
364df8bae1dSRodney W. Grimes  * Ip input routine.  Checksum and byte swap header.  If fragmented
365df8bae1dSRodney W. Grimes  * try to reassemble.  Process options.  Pass to next level.
366df8bae1dSRodney W. Grimes  */
367c67b1d17SGarrett Wollman void
368c67b1d17SGarrett Wollman ip_input(struct mbuf *m)
369df8bae1dSRodney W. Grimes {
3708b615593SMarko Zec 	INIT_VNET_INET(curvnet);
3719188b4a1SAndre Oppermann 	struct ip *ip = NULL;
3725da9f8faSJosef Karthauser 	struct in_ifaddr *ia = NULL;
373ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
3740aade26eSRobert Watson 	struct ifnet *ifp;
3759b932e9eSAndre Oppermann 	int    checkif, hlen = 0;
37647c861ecSBrian Somers 	u_short sum;
37702c1c707SAndre Oppermann 	int dchg = 0;				/* dest changed after fw */
378f51f805fSSam Leffler 	struct in_addr odst;			/* original dst address */
379b715f178SLuigi Rizzo 
380fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
381db40007dSAndrew R. Reiter 
382ac9d7e26SMax Laier 	if (m->m_flags & M_FASTFWD_OURS) {
3839b932e9eSAndre Oppermann 		/*
38476ff6dcfSAndre Oppermann 		 * Firewall or NAT changed destination to local.
38576ff6dcfSAndre Oppermann 		 * We expect ip_len and ip_off to be in host byte order.
3869b932e9eSAndre Oppermann 		 */
38776ff6dcfSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
38876ff6dcfSAndre Oppermann 		/* Set up some basics that will be used later. */
3892b25acc1SLuigi Rizzo 		ip = mtod(m, struct ip *);
39053be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
3919b932e9eSAndre Oppermann 		goto ours;
3922b25acc1SLuigi Rizzo 	}
3932b25acc1SLuigi Rizzo 
39486425c62SRobert Watson 	IPSTAT_INC(ips_total);
39558938916SGarrett Wollman 
39658938916SGarrett Wollman 	if (m->m_pkthdr.len < sizeof(struct ip))
39758938916SGarrett Wollman 		goto tooshort;
39858938916SGarrett Wollman 
399df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct ip) &&
4000b17fba7SAndre Oppermann 	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
40186425c62SRobert Watson 		IPSTAT_INC(ips_toosmall);
402c67b1d17SGarrett Wollman 		return;
403df8bae1dSRodney W. Grimes 	}
404df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
40558938916SGarrett Wollman 
40653be11f6SPoul-Henning Kamp 	if (ip->ip_v != IPVERSION) {
40786425c62SRobert Watson 		IPSTAT_INC(ips_badvers);
408df8bae1dSRodney W. Grimes 		goto bad;
409df8bae1dSRodney W. Grimes 	}
41058938916SGarrett Wollman 
41153be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
412df8bae1dSRodney W. Grimes 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
41386425c62SRobert Watson 		IPSTAT_INC(ips_badhlen);
414df8bae1dSRodney W. Grimes 		goto bad;
415df8bae1dSRodney W. Grimes 	}
416df8bae1dSRodney W. Grimes 	if (hlen > m->m_len) {
4170b17fba7SAndre Oppermann 		if ((m = m_pullup(m, hlen)) == NULL) {
41886425c62SRobert Watson 			IPSTAT_INC(ips_badhlen);
419c67b1d17SGarrett Wollman 			return;
420df8bae1dSRodney W. Grimes 		}
421df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
422df8bae1dSRodney W. Grimes 	}
42333841545SHajimu UMEMOTO 
42433841545SHajimu UMEMOTO 	/* 127/8 must not appear on wire - RFC1122 */
4250aade26eSRobert Watson 	ifp = m->m_pkthdr.rcvif;
42633841545SHajimu UMEMOTO 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
42733841545SHajimu UMEMOTO 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
4280aade26eSRobert Watson 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
42986425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
43033841545SHajimu UMEMOTO 			goto bad;
43133841545SHajimu UMEMOTO 		}
43233841545SHajimu UMEMOTO 	}
43333841545SHajimu UMEMOTO 
434db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
435db4f9cc7SJonathan Lemon 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
436db4f9cc7SJonathan Lemon 	} else {
43758938916SGarrett Wollman 		if (hlen == sizeof(struct ip)) {
43847c861ecSBrian Somers 			sum = in_cksum_hdr(ip);
43958938916SGarrett Wollman 		} else {
44047c861ecSBrian Somers 			sum = in_cksum(m, hlen);
44158938916SGarrett Wollman 		}
442db4f9cc7SJonathan Lemon 	}
44347c861ecSBrian Somers 	if (sum) {
44486425c62SRobert Watson 		IPSTAT_INC(ips_badsum);
445df8bae1dSRodney W. Grimes 		goto bad;
446df8bae1dSRodney W. Grimes 	}
447df8bae1dSRodney W. Grimes 
44802b199f1SMax Laier #ifdef ALTQ
44902b199f1SMax Laier 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
45002b199f1SMax Laier 		/* packet is dropped by traffic conditioner */
45102b199f1SMax Laier 		return;
45202b199f1SMax Laier #endif
45302b199f1SMax Laier 
454df8bae1dSRodney W. Grimes 	/*
455df8bae1dSRodney W. Grimes 	 * Convert fields to host representation.
456df8bae1dSRodney W. Grimes 	 */
457fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
458df8bae1dSRodney W. Grimes 	if (ip->ip_len < hlen) {
45986425c62SRobert Watson 		IPSTAT_INC(ips_badlen);
460df8bae1dSRodney W. Grimes 		goto bad;
461df8bae1dSRodney W. Grimes 	}
462fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
463df8bae1dSRodney W. Grimes 
464df8bae1dSRodney W. Grimes 	/*
465df8bae1dSRodney W. Grimes 	 * Check that the amount of data in the buffers
466df8bae1dSRodney W. Grimes 	 * is as at least much as the IP header would have us expect.
467df8bae1dSRodney W. Grimes 	 * Trim mbufs if longer than we expect.
468df8bae1dSRodney W. Grimes 	 * Drop packet if shorter than we expect.
469df8bae1dSRodney W. Grimes 	 */
470df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len < ip->ip_len) {
47158938916SGarrett Wollman tooshort:
47286425c62SRobert Watson 		IPSTAT_INC(ips_tooshort);
473df8bae1dSRodney W. Grimes 		goto bad;
474df8bae1dSRodney W. Grimes 	}
475df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len > ip->ip_len) {
476df8bae1dSRodney W. Grimes 		if (m->m_len == m->m_pkthdr.len) {
477df8bae1dSRodney W. Grimes 			m->m_len = ip->ip_len;
478df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = ip->ip_len;
479df8bae1dSRodney W. Grimes 		} else
480df8bae1dSRodney W. Grimes 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
481df8bae1dSRodney W. Grimes 	}
482b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
48314dd6717SSam Leffler 	/*
48414dd6717SSam Leffler 	 * Bypass packet filtering for packets from a tunnel (gif).
48514dd6717SSam Leffler 	 */
486cc977adcSBjoern A. Zeeb 	if (ip_ipsec_filtertunnel(m))
487c21fd232SAndre Oppermann 		goto passin;
488b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
4893f67c834SDon Lewis 
490c4ac87eaSDarren Reed 	/*
491134ea224SSam Leffler 	 * Run through list of hooks for input packets.
492f51f805fSSam Leffler 	 *
493f51f805fSSam Leffler 	 * NB: Beware of the destination address changing (e.g.
494f51f805fSSam Leffler 	 *     by NAT rewriting).  When this happens, tell
495f51f805fSSam Leffler 	 *     ip_forward to do the right thing.
496c4ac87eaSDarren Reed 	 */
497c21fd232SAndre Oppermann 
498c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
499604afec4SChristian S.J. Peron 	if (!PFIL_HOOKED(&inet_pfil_hook))
500c21fd232SAndre Oppermann 		goto passin;
501c21fd232SAndre Oppermann 
502f51f805fSSam Leffler 	odst = ip->ip_dst;
5030aade26eSRobert Watson 	if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0)
504beec8214SDarren Reed 		return;
505134ea224SSam Leffler 	if (m == NULL)			/* consumed by filter */
506c4ac87eaSDarren Reed 		return;
5079b932e9eSAndre Oppermann 
508c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
50902c1c707SAndre Oppermann 	dchg = (odst.s_addr != ip->ip_dst.s_addr);
5100aade26eSRobert Watson 	ifp = m->m_pkthdr.rcvif;
5119b932e9eSAndre Oppermann 
5129b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
5139b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
5149b932e9eSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
5159b932e9eSAndre Oppermann 		goto ours;
5169b932e9eSAndre Oppermann 	}
517099dd043SAndre Oppermann 	if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) {
518099dd043SAndre Oppermann 		/*
519099dd043SAndre Oppermann 		 * Directly ship on the packet.  This allows to forward packets
520099dd043SAndre Oppermann 		 * that were destined for us to some other directly connected
521099dd043SAndre Oppermann 		 * host.
522099dd043SAndre Oppermann 		 */
523099dd043SAndre Oppermann 		ip_forward(m, dchg);
524099dd043SAndre Oppermann 		return;
525099dd043SAndre Oppermann 	}
5269b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
5279b932e9eSAndre Oppermann 
528c21fd232SAndre Oppermann passin:
529df8bae1dSRodney W. Grimes 	/*
530df8bae1dSRodney W. Grimes 	 * Process options and, if not destined for us,
531df8bae1dSRodney W. Grimes 	 * ship it on.  ip_dooptions returns 1 when an
532df8bae1dSRodney W. Grimes 	 * error was detected (causing an icmp message
533df8bae1dSRodney W. Grimes 	 * to be sent and the original packet to be freed).
534df8bae1dSRodney W. Grimes 	 */
5359b932e9eSAndre Oppermann 	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
536c67b1d17SGarrett Wollman 		return;
537df8bae1dSRodney W. Grimes 
538f0068c4aSGarrett Wollman         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
539f0068c4aSGarrett Wollman          * matter if it is destined to another node, or whether it is
540f0068c4aSGarrett Wollman          * a multicast one, RSVP wants it! and prevents it from being forwarded
541f0068c4aSGarrett Wollman          * anywhere else. Also checks if the rsvp daemon is running before
542f0068c4aSGarrett Wollman 	 * grabbing the packet.
543f0068c4aSGarrett Wollman          */
544603724d3SBjoern A. Zeeb 	if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP)
545f0068c4aSGarrett Wollman 		goto ours;
546f0068c4aSGarrett Wollman 
547df8bae1dSRodney W. Grimes 	/*
548df8bae1dSRodney W. Grimes 	 * Check our list of addresses, to see if the packet is for us.
549cc766e04SGarrett Wollman 	 * If we don't have any addresses, assume any unicast packet
550cc766e04SGarrett Wollman 	 * we receive might be for us (and let the upper layers deal
551cc766e04SGarrett Wollman 	 * with it).
552df8bae1dSRodney W. Grimes 	 */
553603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
554cc766e04SGarrett Wollman 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
555cc766e04SGarrett Wollman 		goto ours;
556cc766e04SGarrett Wollman 
5577538a9a0SJonathan Lemon 	/*
558823db0e9SDon Lewis 	 * Enable a consistency check between the destination address
559823db0e9SDon Lewis 	 * and the arrival interface for a unicast packet (the RFC 1122
560823db0e9SDon Lewis 	 * strong ES model) if IP forwarding is disabled and the packet
561e15ae1b2SDon Lewis 	 * is not locally generated and the packet is not subject to
562e15ae1b2SDon Lewis 	 * 'ipfw fwd'.
5633f67c834SDon Lewis 	 *
5643f67c834SDon Lewis 	 * XXX - Checking also should be disabled if the destination
5653f67c834SDon Lewis 	 * address is ipnat'ed to a different interface.
5663f67c834SDon Lewis 	 *
567a8f12100SDon Lewis 	 * XXX - Checking is incompatible with IP aliases added
5683f67c834SDon Lewis 	 * to the loopback interface instead of the interface where
5693f67c834SDon Lewis 	 * the packets are received.
570a9771948SGleb Smirnoff 	 *
571a9771948SGleb Smirnoff 	 * XXX - This is the case for carp vhost IPs as well so we
572a9771948SGleb Smirnoff 	 * insert a workaround. If the packet got here, we already
573a9771948SGleb Smirnoff 	 * checked with carp_iamatch() and carp_forus().
574823db0e9SDon Lewis 	 */
575603724d3SBjoern A. Zeeb 	checkif = V_ip_checkinterface && (V_ipforwarding == 0) &&
5760aade26eSRobert Watson 	    ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) &&
577a9771948SGleb Smirnoff #ifdef DEV_CARP
5780aade26eSRobert Watson 	    !ifp->if_carp &&
579a9771948SGleb Smirnoff #endif
5809b932e9eSAndre Oppermann 	    (dchg == 0);
581823db0e9SDon Lewis 
582ca925d9cSJonathan Lemon 	/*
583ca925d9cSJonathan Lemon 	 * Check for exact addresses in the hash bucket.
584ca925d9cSJonathan Lemon 	 */
5859b932e9eSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
586f9e354dfSJulian Elischer 		/*
587823db0e9SDon Lewis 		 * If the address matches, verify that the packet
588823db0e9SDon Lewis 		 * arrived via the correct interface if checking is
589823db0e9SDon Lewis 		 * enabled.
590f9e354dfSJulian Elischer 		 */
5919b932e9eSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
5920aade26eSRobert Watson 		    (!checkif || ia->ia_ifp == ifp))
593ed1ff184SJulian Elischer 			goto ours;
594ca925d9cSJonathan Lemon 	}
595823db0e9SDon Lewis 	/*
596ca925d9cSJonathan Lemon 	 * Check for broadcast addresses.
597ca925d9cSJonathan Lemon 	 *
598ca925d9cSJonathan Lemon 	 * Only accept broadcast packets that arrive via the matching
599ca925d9cSJonathan Lemon 	 * interface.  Reception of forwarded directed broadcasts would
600ca925d9cSJonathan Lemon 	 * be handled via ip_forward() and ether_output() with the loopback
601ca925d9cSJonathan Lemon 	 * into the stack for SIMPLEX interfaces handled by ether_output().
602823db0e9SDon Lewis 	 */
6030aade26eSRobert Watson 	if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
6040aade26eSRobert Watson 		IF_ADDR_LOCK(ifp);
6050aade26eSRobert Watson 	        TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
606ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
607ca925d9cSJonathan Lemon 				continue;
608ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
609df8bae1dSRodney W. Grimes 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
6100aade26eSRobert Watson 			    ip->ip_dst.s_addr) {
6110aade26eSRobert Watson 				IF_ADDR_UNLOCK(ifp);
612df8bae1dSRodney W. Grimes 				goto ours;
6130aade26eSRobert Watson 			}
6140aade26eSRobert Watson 			if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr) {
6150aade26eSRobert Watson 				IF_ADDR_UNLOCK(ifp);
616df8bae1dSRodney W. Grimes 				goto ours;
6170aade26eSRobert Watson 			}
6180ac40133SBrian Somers #ifdef BOOTP_COMPAT
6190aade26eSRobert Watson 			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) {
6200aade26eSRobert Watson 				IF_ADDR_UNLOCK(ifp);
621ca925d9cSJonathan Lemon 				goto ours;
6220aade26eSRobert Watson 			}
6230ac40133SBrian Somers #endif
624df8bae1dSRodney W. Grimes 		}
6250aade26eSRobert Watson 		IF_ADDR_UNLOCK(ifp);
626df8bae1dSRodney W. Grimes 	}
627f8429ca2SBruce M Simpson 	/* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
628f8429ca2SBruce M Simpson 	if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
62986425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
630f8429ca2SBruce M Simpson 		m_freem(m);
631f8429ca2SBruce M Simpson 		return;
632f8429ca2SBruce M Simpson 	}
633df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
634603724d3SBjoern A. Zeeb 		if (V_ip_mrouter) {
635df8bae1dSRodney W. Grimes 			/*
636df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, all
637df8bae1dSRodney W. Grimes 			 * incoming multicast packets are passed to the
638df8bae1dSRodney W. Grimes 			 * kernel-level multicast forwarding function.
639df8bae1dSRodney W. Grimes 			 * The packet is returned (relatively) intact; if
640df8bae1dSRodney W. Grimes 			 * ip_mforward() returns a non-zero value, the packet
641df8bae1dSRodney W. Grimes 			 * must be discarded, else it may be accepted below.
642df8bae1dSRodney W. Grimes 			 */
6430aade26eSRobert Watson 			if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
64486425c62SRobert Watson 				IPSTAT_INC(ips_cantforward);
645df8bae1dSRodney W. Grimes 				m_freem(m);
646c67b1d17SGarrett Wollman 				return;
647df8bae1dSRodney W. Grimes 			}
648df8bae1dSRodney W. Grimes 
649df8bae1dSRodney W. Grimes 			/*
65011612afaSDima Dorfman 			 * The process-level routing daemon needs to receive
651df8bae1dSRodney W. Grimes 			 * all multicast IGMP packets, whether or not this
652df8bae1dSRodney W. Grimes 			 * host belongs to their destination groups.
653df8bae1dSRodney W. Grimes 			 */
654df8bae1dSRodney W. Grimes 			if (ip->ip_p == IPPROTO_IGMP)
655df8bae1dSRodney W. Grimes 				goto ours;
65686425c62SRobert Watson 			IPSTAT_INC(ips_forward);
657df8bae1dSRodney W. Grimes 		}
658df8bae1dSRodney W. Grimes 		/*
659d10910e6SBruce M Simpson 		 * Assume the packet is for us, to avoid prematurely taking
660d10910e6SBruce M Simpson 		 * a lock on the in_multi hash. Protocols must perform
661d10910e6SBruce M Simpson 		 * their own filtering and update statistics accordingly.
662df8bae1dSRodney W. Grimes 		 */
663df8bae1dSRodney W. Grimes 		goto ours;
664df8bae1dSRodney W. Grimes 	}
665df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
666df8bae1dSRodney W. Grimes 		goto ours;
667df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == INADDR_ANY)
668df8bae1dSRodney W. Grimes 		goto ours;
669df8bae1dSRodney W. Grimes 
6706a800098SYoshinobu Inoue 	/*
6716a800098SYoshinobu Inoue 	 * FAITH(Firewall Aided Internet Translator)
6726a800098SYoshinobu Inoue 	 */
6730aade26eSRobert Watson 	if (ifp && ifp->if_type == IFT_FAITH) {
674603724d3SBjoern A. Zeeb 		if (V_ip_keepfaith) {
6756a800098SYoshinobu Inoue 			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
6766a800098SYoshinobu Inoue 				goto ours;
6776a800098SYoshinobu Inoue 		}
6786a800098SYoshinobu Inoue 		m_freem(m);
6796a800098SYoshinobu Inoue 		return;
6806a800098SYoshinobu Inoue 	}
6819494d596SBrooks Davis 
682df8bae1dSRodney W. Grimes 	/*
683df8bae1dSRodney W. Grimes 	 * Not for us; forward if possible and desirable.
684df8bae1dSRodney W. Grimes 	 */
685603724d3SBjoern A. Zeeb 	if (V_ipforwarding == 0) {
68686425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
687df8bae1dSRodney W. Grimes 		m_freem(m);
688546f251bSChris D. Faulhaber 	} else {
689b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
6901dfcf0d2SAndre Oppermann 		if (ip_ipsec_fwd(m))
691546f251bSChris D. Faulhaber 			goto bad;
692b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
6939b932e9eSAndre Oppermann 		ip_forward(m, dchg);
694546f251bSChris D. Faulhaber 	}
695c67b1d17SGarrett Wollman 	return;
696df8bae1dSRodney W. Grimes 
697df8bae1dSRodney W. Grimes ours:
698d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH
699d0ebc0d2SYaroslav Tykhiy 	/*
700d0ebc0d2SYaroslav Tykhiy 	 * IPSTEALTH: Process non-routing options only
701d0ebc0d2SYaroslav Tykhiy 	 * if the packet is destined for us.
702d0ebc0d2SYaroslav Tykhiy 	 */
703603724d3SBjoern A. Zeeb 	if (V_ipstealth && hlen > sizeof (struct ip) &&
7049b932e9eSAndre Oppermann 	    ip_dooptions(m, 1))
705d0ebc0d2SYaroslav Tykhiy 		return;
706d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */
707d0ebc0d2SYaroslav Tykhiy 
7085da9f8faSJosef Karthauser 	/* Count the packet in the ip address stats */
7095da9f8faSJosef Karthauser 	if (ia != NULL) {
7105da9f8faSJosef Karthauser 		ia->ia_ifa.if_ipackets++;
7115da9f8faSJosef Karthauser 		ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
7125da9f8faSJosef Karthauser 	}
713100ba1a6SJordan K. Hubbard 
71463f8d699SJordan K. Hubbard 	/*
715b6ea1aa5SRuslan Ermilov 	 * Attempt reassembly; if it succeeds, proceed.
716ac9d7e26SMax Laier 	 * ip_reass() will return a different mbuf.
717df8bae1dSRodney W. Grimes 	 */
718f0cada84SAndre Oppermann 	if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
719f0cada84SAndre Oppermann 		m = ip_reass(m);
720f0cada84SAndre Oppermann 		if (m == NULL)
721c67b1d17SGarrett Wollman 			return;
7226a800098SYoshinobu Inoue 		ip = mtod(m, struct ip *);
7237e2df452SRuslan Ermilov 		/* Get the header length of the reassembled packet */
72453be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
725f0cada84SAndre Oppermann 	}
726f0cada84SAndre Oppermann 
727f0cada84SAndre Oppermann 	/*
728f0cada84SAndre Oppermann 	 * Further protocols expect the packet length to be w/o the
729f0cada84SAndre Oppermann 	 * IP header.
730f0cada84SAndre Oppermann 	 */
731df8bae1dSRodney W. Grimes 	ip->ip_len -= hlen;
732df8bae1dSRodney W. Grimes 
733b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
73433841545SHajimu UMEMOTO 	/*
73533841545SHajimu UMEMOTO 	 * enforce IPsec policy checking if we are seeing last header.
73633841545SHajimu UMEMOTO 	 * note that we do not visit this with protocols with pcb layer
73733841545SHajimu UMEMOTO 	 * code - like udp/tcp/raw ip.
73833841545SHajimu UMEMOTO 	 */
7391dfcf0d2SAndre Oppermann 	if (ip_ipsec_input(m))
74033841545SHajimu UMEMOTO 		goto bad;
741b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
74233841545SHajimu UMEMOTO 
743df8bae1dSRodney W. Grimes 	/*
744df8bae1dSRodney W. Grimes 	 * Switch out to protocol's input routine.
745df8bae1dSRodney W. Grimes 	 */
74686425c62SRobert Watson 	IPSTAT_INC(ips_delivered);
7479b932e9eSAndre Oppermann 
7482b25acc1SLuigi Rizzo 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
749c67b1d17SGarrett Wollman 	return;
750df8bae1dSRodney W. Grimes bad:
751df8bae1dSRodney W. Grimes 	m_freem(m);
752c67b1d17SGarrett Wollman }
753c67b1d17SGarrett Wollman 
754c67b1d17SGarrett Wollman /*
755d248c7d7SRobert Watson  * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
756d248c7d7SRobert Watson  * max has slightly different semantics than the sysctl, for historical
757d248c7d7SRobert Watson  * reasons.
758d248c7d7SRobert Watson  */
759d248c7d7SRobert Watson static void
760d248c7d7SRobert Watson maxnipq_update(void)
761d248c7d7SRobert Watson {
7628b615593SMarko Zec 	INIT_VNET_INET(curvnet);
763d248c7d7SRobert Watson 
764d248c7d7SRobert Watson 	/*
765d248c7d7SRobert Watson 	 * -1 for unlimited allocation.
766d248c7d7SRobert Watson 	 */
767603724d3SBjoern A. Zeeb 	if (V_maxnipq < 0)
768603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 0);
769d248c7d7SRobert Watson 	/*
770d248c7d7SRobert Watson 	 * Positive number for specific bound.
771d248c7d7SRobert Watson 	 */
772603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0)
773603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, V_maxnipq);
774d248c7d7SRobert Watson 	/*
775d248c7d7SRobert Watson 	 * Zero specifies no further fragment queue allocation -- set the
776d248c7d7SRobert Watson 	 * bound very low, but rely on implementation elsewhere to actually
777d248c7d7SRobert Watson 	 * prevent allocation and reclaim current queues.
778d248c7d7SRobert Watson 	 */
779603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0)
780603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 1);
781d248c7d7SRobert Watson }
782d248c7d7SRobert Watson 
7834f590175SPaul Saab static void
7844f590175SPaul Saab ipq_zone_change(void *tag)
7854f590175SPaul Saab {
7868b615593SMarko Zec 	INIT_VNET_INET(curvnet);
7874f590175SPaul Saab 
788603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) {
789603724d3SBjoern A. Zeeb 		V_maxnipq = nmbclusters / 32;
7904f590175SPaul Saab 		maxnipq_update();
7914f590175SPaul Saab 	}
7924f590175SPaul Saab }
7934f590175SPaul Saab 
794d248c7d7SRobert Watson static int
795d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
796d248c7d7SRobert Watson {
7978b615593SMarko Zec 	INIT_VNET_INET(curvnet);
798d248c7d7SRobert Watson 	int error, i;
799d248c7d7SRobert Watson 
800603724d3SBjoern A. Zeeb 	i = V_maxnipq;
801d248c7d7SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
802d248c7d7SRobert Watson 	if (error || !req->newptr)
803d248c7d7SRobert Watson 		return (error);
804d248c7d7SRobert Watson 
805d248c7d7SRobert Watson 	/*
806d248c7d7SRobert Watson 	 * XXXRW: Might be a good idea to sanity check the argument and place
807d248c7d7SRobert Watson 	 * an extreme upper bound.
808d248c7d7SRobert Watson 	 */
809d248c7d7SRobert Watson 	if (i < -1)
810d248c7d7SRobert Watson 		return (EINVAL);
811603724d3SBjoern A. Zeeb 	V_maxnipq = i;
812d248c7d7SRobert Watson 	maxnipq_update();
813d248c7d7SRobert Watson 	return (0);
814d248c7d7SRobert Watson }
815d248c7d7SRobert Watson 
816d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
817d248c7d7SRobert Watson     NULL, 0, sysctl_maxnipq, "I",
818d248c7d7SRobert Watson     "Maximum number of IPv4 fragment reassembly queue entries");
819d248c7d7SRobert Watson 
820d248c7d7SRobert Watson /*
8218948e4baSArchie Cobbs  * Take incoming datagram fragment and try to reassemble it into
822f0cada84SAndre Oppermann  * whole datagram.  If the argument is the first fragment or one
823f0cada84SAndre Oppermann  * in between the function will return NULL and store the mbuf
824f0cada84SAndre Oppermann  * in the fragment chain.  If the argument is the last fragment
825f0cada84SAndre Oppermann  * the packet will be reassembled and the pointer to the new
826f0cada84SAndre Oppermann  * mbuf returned for further processing.  Only m_tags attached
827f0cada84SAndre Oppermann  * to the first packet/fragment are preserved.
828f0cada84SAndre Oppermann  * The IP header is *NOT* adjusted out of iplen.
829df8bae1dSRodney W. Grimes  */
830f0cada84SAndre Oppermann struct mbuf *
831f0cada84SAndre Oppermann ip_reass(struct mbuf *m)
832df8bae1dSRodney W. Grimes {
8338b615593SMarko Zec 	INIT_VNET_INET(curvnet);
834f0cada84SAndre Oppermann 	struct ip *ip;
835f0cada84SAndre Oppermann 	struct mbuf *p, *q, *nq, *t;
836f0cada84SAndre Oppermann 	struct ipq *fp = NULL;
837f0cada84SAndre Oppermann 	struct ipqhead *head;
838f0cada84SAndre Oppermann 	int i, hlen, next;
83959dfcba4SHajimu UMEMOTO 	u_int8_t ecn, ecn0;
840f0cada84SAndre Oppermann 	u_short hash;
841df8bae1dSRodney W. Grimes 
842800af1fbSMaxim Konovalov 	/* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
843603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0 || V_maxfragsperpacket == 0) {
84486425c62SRobert Watson 		IPSTAT_INC(ips_fragments);
84586425c62SRobert Watson 		IPSTAT_INC(ips_fragdropped);
8469d804f81SAndre Oppermann 		m_freem(m);
8479d804f81SAndre Oppermann 		return (NULL);
848f0cada84SAndre Oppermann 	}
8492fad1e93SSam Leffler 
850f0cada84SAndre Oppermann 	ip = mtod(m, struct ip *);
851f0cada84SAndre Oppermann 	hlen = ip->ip_hl << 2;
852f0cada84SAndre Oppermann 
853f0cada84SAndre Oppermann 	hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
854603724d3SBjoern A. Zeeb 	head = &V_ipq[hash];
855f0cada84SAndre Oppermann 	IPQ_LOCK();
856f0cada84SAndre Oppermann 
857f0cada84SAndre Oppermann 	/*
858f0cada84SAndre Oppermann 	 * Look for queue of fragments
859f0cada84SAndre Oppermann 	 * of this datagram.
860f0cada84SAndre Oppermann 	 */
861f0cada84SAndre Oppermann 	TAILQ_FOREACH(fp, head, ipq_list)
862f0cada84SAndre Oppermann 		if (ip->ip_id == fp->ipq_id &&
863f0cada84SAndre Oppermann 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
864f0cada84SAndre Oppermann 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
865f0cada84SAndre Oppermann #ifdef MAC
86630d239bcSRobert Watson 		    mac_ipq_match(m, fp) &&
867f0cada84SAndre Oppermann #endif
868f0cada84SAndre Oppermann 		    ip->ip_p == fp->ipq_p)
869f0cada84SAndre Oppermann 			goto found;
870f0cada84SAndre Oppermann 
871f0cada84SAndre Oppermann 	fp = NULL;
872f0cada84SAndre Oppermann 
873f0cada84SAndre Oppermann 	/*
874d248c7d7SRobert Watson 	 * Attempt to trim the number of allocated fragment queues if it
875d248c7d7SRobert Watson 	 * exceeds the administrative limit.
876f0cada84SAndre Oppermann 	 */
877603724d3SBjoern A. Zeeb 	if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) {
878f0cada84SAndre Oppermann 		/*
879f0cada84SAndre Oppermann 		 * drop something from the tail of the current queue
880f0cada84SAndre Oppermann 		 * before proceeding further
881f0cada84SAndre Oppermann 		 */
882f0cada84SAndre Oppermann 		struct ipq *q = TAILQ_LAST(head, ipqhead);
883f0cada84SAndre Oppermann 		if (q == NULL) {   /* gak */
884f0cada84SAndre Oppermann 			for (i = 0; i < IPREASS_NHASH; i++) {
885603724d3SBjoern A. Zeeb 				struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead);
886f0cada84SAndre Oppermann 				if (r) {
88786425c62SRobert Watson 					IPSTAT_ADD(ips_fragtimeout,
88886425c62SRobert Watson 					    r->ipq_nfrags);
889603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], r);
890f0cada84SAndre Oppermann 					break;
891f0cada84SAndre Oppermann 				}
892f0cada84SAndre Oppermann 			}
893f0cada84SAndre Oppermann 		} else {
89486425c62SRobert Watson 			IPSTAT_ADD(ips_fragtimeout, q->ipq_nfrags);
895f0cada84SAndre Oppermann 			ip_freef(head, q);
896f0cada84SAndre Oppermann 		}
897f0cada84SAndre Oppermann 	}
898f0cada84SAndre Oppermann 
899f0cada84SAndre Oppermann found:
900f0cada84SAndre Oppermann 	/*
901f0cada84SAndre Oppermann 	 * Adjust ip_len to not reflect header,
902f0cada84SAndre Oppermann 	 * convert offset of this to bytes.
903f0cada84SAndre Oppermann 	 */
904f0cada84SAndre Oppermann 	ip->ip_len -= hlen;
905f0cada84SAndre Oppermann 	if (ip->ip_off & IP_MF) {
906f0cada84SAndre Oppermann 		/*
907f0cada84SAndre Oppermann 		 * Make sure that fragments have a data length
908f0cada84SAndre Oppermann 		 * that's a non-zero multiple of 8 bytes.
909f0cada84SAndre Oppermann 		 */
910f0cada84SAndre Oppermann 		if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
91186425c62SRobert Watson 			IPSTAT_INC(ips_toosmall); /* XXX */
912f0cada84SAndre Oppermann 			goto dropfrag;
913f0cada84SAndre Oppermann 		}
914f0cada84SAndre Oppermann 		m->m_flags |= M_FRAG;
915f0cada84SAndre Oppermann 	} else
916f0cada84SAndre Oppermann 		m->m_flags &= ~M_FRAG;
917f0cada84SAndre Oppermann 	ip->ip_off <<= 3;
918f0cada84SAndre Oppermann 
919f0cada84SAndre Oppermann 
920f0cada84SAndre Oppermann 	/*
921f0cada84SAndre Oppermann 	 * Attempt reassembly; if it succeeds, proceed.
922f0cada84SAndre Oppermann 	 * ip_reass() will return a different mbuf.
923f0cada84SAndre Oppermann 	 */
92486425c62SRobert Watson 	IPSTAT_INC(ips_fragments);
925f0cada84SAndre Oppermann 	m->m_pkthdr.header = ip;
926f0cada84SAndre Oppermann 
927f0cada84SAndre Oppermann 	/* Previous ip_reass() started here. */
928df8bae1dSRodney W. Grimes 	/*
929df8bae1dSRodney W. Grimes 	 * Presence of header sizes in mbufs
930df8bae1dSRodney W. Grimes 	 * would confuse code below.
931df8bae1dSRodney W. Grimes 	 */
932df8bae1dSRodney W. Grimes 	m->m_data += hlen;
933df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
934df8bae1dSRodney W. Grimes 
935df8bae1dSRodney W. Grimes 	/*
936df8bae1dSRodney W. Grimes 	 * If first fragment to arrive, create a reassembly queue.
937df8bae1dSRodney W. Grimes 	 */
938042bbfa3SRobert Watson 	if (fp == NULL) {
939603724d3SBjoern A. Zeeb 		fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
940d248c7d7SRobert Watson 		if (fp == NULL)
941df8bae1dSRodney W. Grimes 			goto dropfrag;
94236b0360bSRobert Watson #ifdef MAC
94330d239bcSRobert Watson 		if (mac_ipq_init(fp, M_NOWAIT) != 0) {
944603724d3SBjoern A. Zeeb 			uma_zfree(V_ipq_zone, fp);
9451d7d0bfeSPawel Jakub Dawidek 			fp = NULL;
9465e7ce478SRobert Watson 			goto dropfrag;
9475e7ce478SRobert Watson 		}
94830d239bcSRobert Watson 		mac_ipq_create(m, fp);
94936b0360bSRobert Watson #endif
950462b86feSPoul-Henning Kamp 		TAILQ_INSERT_HEAD(head, fp, ipq_list);
951603724d3SBjoern A. Zeeb 		V_nipq++;
952375386e2SMike Silbersack 		fp->ipq_nfrags = 1;
953df8bae1dSRodney W. Grimes 		fp->ipq_ttl = IPFRAGTTL;
954df8bae1dSRodney W. Grimes 		fp->ipq_p = ip->ip_p;
955df8bae1dSRodney W. Grimes 		fp->ipq_id = ip->ip_id;
9566effc713SDoug Rabson 		fp->ipq_src = ip->ip_src;
9576effc713SDoug Rabson 		fp->ipq_dst = ip->ip_dst;
958af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
959af38c68cSLuigi Rizzo 		m->m_nextpkt = NULL;
960800af1fbSMaxim Konovalov 		goto done;
96136b0360bSRobert Watson 	} else {
962375386e2SMike Silbersack 		fp->ipq_nfrags++;
96336b0360bSRobert Watson #ifdef MAC
96430d239bcSRobert Watson 		mac_ipq_update(m, fp);
96536b0360bSRobert Watson #endif
966df8bae1dSRodney W. Grimes 	}
967df8bae1dSRodney W. Grimes 
9686effc713SDoug Rabson #define GETIP(m)	((struct ip*)((m)->m_pkthdr.header))
9696effc713SDoug Rabson 
970df8bae1dSRodney W. Grimes 	/*
97159dfcba4SHajimu UMEMOTO 	 * Handle ECN by comparing this segment with the first one;
97259dfcba4SHajimu UMEMOTO 	 * if CE is set, do not lose CE.
97359dfcba4SHajimu UMEMOTO 	 * drop if CE and not-ECT are mixed for the same packet.
97459dfcba4SHajimu UMEMOTO 	 */
97559dfcba4SHajimu UMEMOTO 	ecn = ip->ip_tos & IPTOS_ECN_MASK;
97659dfcba4SHajimu UMEMOTO 	ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
97759dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_CE) {
97859dfcba4SHajimu UMEMOTO 		if (ecn0 == IPTOS_ECN_NOTECT)
97959dfcba4SHajimu UMEMOTO 			goto dropfrag;
98059dfcba4SHajimu UMEMOTO 		if (ecn0 != IPTOS_ECN_CE)
98159dfcba4SHajimu UMEMOTO 			GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
98259dfcba4SHajimu UMEMOTO 	}
98359dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
98459dfcba4SHajimu UMEMOTO 		goto dropfrag;
98559dfcba4SHajimu UMEMOTO 
98659dfcba4SHajimu UMEMOTO 	/*
987df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
988df8bae1dSRodney W. Grimes 	 */
9896effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
9906effc713SDoug Rabson 		if (GETIP(q)->ip_off > ip->ip_off)
991df8bae1dSRodney W. Grimes 			break;
992df8bae1dSRodney W. Grimes 
993df8bae1dSRodney W. Grimes 	/*
994df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
995df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
996af38c68cSLuigi Rizzo 	 * segment.  If it provides all of our data, drop us, otherwise
997af38c68cSLuigi Rizzo 	 * stick new segment in the proper place.
998db4f9cc7SJonathan Lemon 	 *
999db4f9cc7SJonathan Lemon 	 * If some of the data is dropped from the the preceding
1000db4f9cc7SJonathan Lemon 	 * segment, then it's checksum is invalidated.
1001df8bae1dSRodney W. Grimes 	 */
10026effc713SDoug Rabson 	if (p) {
10036effc713SDoug Rabson 		i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
1004df8bae1dSRodney W. Grimes 		if (i > 0) {
1005df8bae1dSRodney W. Grimes 			if (i >= ip->ip_len)
1006df8bae1dSRodney W. Grimes 				goto dropfrag;
10076a800098SYoshinobu Inoue 			m_adj(m, i);
1008db4f9cc7SJonathan Lemon 			m->m_pkthdr.csum_flags = 0;
1009df8bae1dSRodney W. Grimes 			ip->ip_off += i;
1010df8bae1dSRodney W. Grimes 			ip->ip_len -= i;
1011df8bae1dSRodney W. Grimes 		}
1012af38c68cSLuigi Rizzo 		m->m_nextpkt = p->m_nextpkt;
1013af38c68cSLuigi Rizzo 		p->m_nextpkt = m;
1014af38c68cSLuigi Rizzo 	} else {
1015af38c68cSLuigi Rizzo 		m->m_nextpkt = fp->ipq_frags;
1016af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
1017df8bae1dSRodney W. Grimes 	}
1018df8bae1dSRodney W. Grimes 
1019df8bae1dSRodney W. Grimes 	/*
1020df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
1021df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
1022df8bae1dSRodney W. Grimes 	 */
10236effc713SDoug Rabson 	for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
1024af38c68cSLuigi Rizzo 	     q = nq) {
1025b36f5b37SMaxim Konovalov 		i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
10266effc713SDoug Rabson 		if (i < GETIP(q)->ip_len) {
10276effc713SDoug Rabson 			GETIP(q)->ip_len -= i;
10286effc713SDoug Rabson 			GETIP(q)->ip_off += i;
10296effc713SDoug Rabson 			m_adj(q, i);
1030db4f9cc7SJonathan Lemon 			q->m_pkthdr.csum_flags = 0;
1031df8bae1dSRodney W. Grimes 			break;
1032df8bae1dSRodney W. Grimes 		}
10336effc713SDoug Rabson 		nq = q->m_nextpkt;
1034af38c68cSLuigi Rizzo 		m->m_nextpkt = nq;
103586425c62SRobert Watson 		IPSTAT_INC(ips_fragdropped);
1036375386e2SMike Silbersack 		fp->ipq_nfrags--;
10376effc713SDoug Rabson 		m_freem(q);
1038df8bae1dSRodney W. Grimes 	}
1039df8bae1dSRodney W. Grimes 
1040df8bae1dSRodney W. Grimes 	/*
1041375386e2SMike Silbersack 	 * Check for complete reassembly and perform frag per packet
1042375386e2SMike Silbersack 	 * limiting.
1043375386e2SMike Silbersack 	 *
1044375386e2SMike Silbersack 	 * Frag limiting is performed here so that the nth frag has
1045375386e2SMike Silbersack 	 * a chance to complete the packet before we drop the packet.
1046375386e2SMike Silbersack 	 * As a result, n+1 frags are actually allowed per packet, but
1047375386e2SMike Silbersack 	 * only n will ever be stored. (n = maxfragsperpacket.)
1048375386e2SMike Silbersack 	 *
1049df8bae1dSRodney W. Grimes 	 */
10506effc713SDoug Rabson 	next = 0;
10516effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
1052375386e2SMike Silbersack 		if (GETIP(q)->ip_off != next) {
1053603724d3SBjoern A. Zeeb 			if (fp->ipq_nfrags > V_maxfragsperpacket) {
105486425c62SRobert Watson 				IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1055375386e2SMike Silbersack 				ip_freef(head, fp);
105699e8617dSMaxim Konovalov 			}
1057f0cada84SAndre Oppermann 			goto done;
1058375386e2SMike Silbersack 		}
10596effc713SDoug Rabson 		next += GETIP(q)->ip_len;
10606effc713SDoug Rabson 	}
10616effc713SDoug Rabson 	/* Make sure the last packet didn't have the IP_MF flag */
1062375386e2SMike Silbersack 	if (p->m_flags & M_FRAG) {
1063603724d3SBjoern A. Zeeb 		if (fp->ipq_nfrags > V_maxfragsperpacket) {
106486425c62SRobert Watson 			IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1065375386e2SMike Silbersack 			ip_freef(head, fp);
106699e8617dSMaxim Konovalov 		}
1067f0cada84SAndre Oppermann 		goto done;
1068375386e2SMike Silbersack 	}
1069df8bae1dSRodney W. Grimes 
1070df8bae1dSRodney W. Grimes 	/*
1071430d30d8SBill Fenner 	 * Reassembly is complete.  Make sure the packet is a sane size.
1072430d30d8SBill Fenner 	 */
10736effc713SDoug Rabson 	q = fp->ipq_frags;
10746effc713SDoug Rabson 	ip = GETIP(q);
107553be11f6SPoul-Henning Kamp 	if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
107686425c62SRobert Watson 		IPSTAT_INC(ips_toolong);
107786425c62SRobert Watson 		IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1078462b86feSPoul-Henning Kamp 		ip_freef(head, fp);
1079f0cada84SAndre Oppermann 		goto done;
1080430d30d8SBill Fenner 	}
1081430d30d8SBill Fenner 
1082430d30d8SBill Fenner 	/*
1083430d30d8SBill Fenner 	 * Concatenate fragments.
1084df8bae1dSRodney W. Grimes 	 */
10856effc713SDoug Rabson 	m = q;
1086df8bae1dSRodney W. Grimes 	t = m->m_next;
108702410549SRobert Watson 	m->m_next = NULL;
1088df8bae1dSRodney W. Grimes 	m_cat(m, t);
10896effc713SDoug Rabson 	nq = q->m_nextpkt;
109002410549SRobert Watson 	q->m_nextpkt = NULL;
10916effc713SDoug Rabson 	for (q = nq; q != NULL; q = nq) {
10926effc713SDoug Rabson 		nq = q->m_nextpkt;
1093945aa40dSDoug Rabson 		q->m_nextpkt = NULL;
1094db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1095db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1096a8db1d93SJonathan Lemon 		m_cat(m, q);
1097df8bae1dSRodney W. Grimes 	}
10986edb555dSOleg Bulyzhin 	/*
10996edb555dSOleg Bulyzhin 	 * In order to do checksumming faster we do 'end-around carry' here
11006edb555dSOleg Bulyzhin 	 * (and not in for{} loop), though it implies we are not going to
11016edb555dSOleg Bulyzhin 	 * reassemble more than 64k fragments.
11026edb555dSOleg Bulyzhin 	 */
11036edb555dSOleg Bulyzhin 	m->m_pkthdr.csum_data =
11046edb555dSOleg Bulyzhin 	    (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16);
110536b0360bSRobert Watson #ifdef MAC
110630d239bcSRobert Watson 	mac_ipq_reassemble(fp, m);
110730d239bcSRobert Watson 	mac_ipq_destroy(fp);
110836b0360bSRobert Watson #endif
1109df8bae1dSRodney W. Grimes 
1110df8bae1dSRodney W. Grimes 	/*
1111f0cada84SAndre Oppermann 	 * Create header for new ip packet by modifying header of first
1112f0cada84SAndre Oppermann 	 * packet;  dequeue and discard fragment reassembly header.
1113df8bae1dSRodney W. Grimes 	 * Make header visible.
1114df8bae1dSRodney W. Grimes 	 */
1115f0cada84SAndre Oppermann 	ip->ip_len = (ip->ip_hl << 2) + next;
11166effc713SDoug Rabson 	ip->ip_src = fp->ipq_src;
11176effc713SDoug Rabson 	ip->ip_dst = fp->ipq_dst;
1118462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(head, fp, ipq_list);
1119603724d3SBjoern A. Zeeb 	V_nipq--;
1120603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
112153be11f6SPoul-Henning Kamp 	m->m_len += (ip->ip_hl << 2);
112253be11f6SPoul-Henning Kamp 	m->m_data -= (ip->ip_hl << 2);
1123df8bae1dSRodney W. Grimes 	/* some debugging cruft by sklower, below, will go away soon */
1124a5554bf0SPoul-Henning Kamp 	if (m->m_flags & M_PKTHDR)	/* XXX this should be done elsewhere */
1125a5554bf0SPoul-Henning Kamp 		m_fixhdr(m);
112686425c62SRobert Watson 	IPSTAT_INC(ips_reassembled);
1127f0cada84SAndre Oppermann 	IPQ_UNLOCK();
11286a800098SYoshinobu Inoue 	return (m);
1129df8bae1dSRodney W. Grimes 
1130df8bae1dSRodney W. Grimes dropfrag:
113186425c62SRobert Watson 	IPSTAT_INC(ips_fragdropped);
1132042bbfa3SRobert Watson 	if (fp != NULL)
1133375386e2SMike Silbersack 		fp->ipq_nfrags--;
1134df8bae1dSRodney W. Grimes 	m_freem(m);
1135f0cada84SAndre Oppermann done:
1136f0cada84SAndre Oppermann 	IPQ_UNLOCK();
1137f0cada84SAndre Oppermann 	return (NULL);
11386effc713SDoug Rabson 
11396effc713SDoug Rabson #undef GETIP
1140df8bae1dSRodney W. Grimes }
1141df8bae1dSRodney W. Grimes 
1142df8bae1dSRodney W. Grimes /*
1143df8bae1dSRodney W. Grimes  * Free a fragment reassembly header and all
1144df8bae1dSRodney W. Grimes  * associated datagrams.
1145df8bae1dSRodney W. Grimes  */
11460312fbe9SPoul-Henning Kamp static void
1147f2565d68SRobert Watson ip_freef(struct ipqhead *fhp, struct ipq *fp)
1148df8bae1dSRodney W. Grimes {
11498b615593SMarko Zec 	INIT_VNET_INET(curvnet);
1150f2565d68SRobert Watson 	struct mbuf *q;
1151df8bae1dSRodney W. Grimes 
11522fad1e93SSam Leffler 	IPQ_LOCK_ASSERT();
11532fad1e93SSam Leffler 
11546effc713SDoug Rabson 	while (fp->ipq_frags) {
11556effc713SDoug Rabson 		q = fp->ipq_frags;
11566effc713SDoug Rabson 		fp->ipq_frags = q->m_nextpkt;
11576effc713SDoug Rabson 		m_freem(q);
1158df8bae1dSRodney W. Grimes 	}
1159462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(fhp, fp, ipq_list);
1160603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
1161603724d3SBjoern A. Zeeb 	V_nipq--;
1162df8bae1dSRodney W. Grimes }
1163df8bae1dSRodney W. Grimes 
1164df8bae1dSRodney W. Grimes /*
1165df8bae1dSRodney W. Grimes  * IP timer processing;
1166df8bae1dSRodney W. Grimes  * if a timer expires on a reassembly
1167df8bae1dSRodney W. Grimes  * queue, discard it.
1168df8bae1dSRodney W. Grimes  */
1169df8bae1dSRodney W. Grimes void
1170f2565d68SRobert Watson ip_slowtimo(void)
1171df8bae1dSRodney W. Grimes {
11728b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1173f2565d68SRobert Watson 	struct ipq *fp;
1174194a213eSAndrey A. Chernov 	int i;
1175df8bae1dSRodney W. Grimes 
11762fad1e93SSam Leffler 	IPQ_LOCK();
11778b615593SMarko Zec 	VNET_LIST_RLOCK();
11788b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
11798b615593SMarko Zec 		CURVNET_SET(vnet_iter);
11808b615593SMarko Zec 		INIT_VNET_INET(vnet_iter);
1181194a213eSAndrey A. Chernov 		for (i = 0; i < IPREASS_NHASH; i++) {
1182603724d3SBjoern A. Zeeb 			for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) {
1183462b86feSPoul-Henning Kamp 				struct ipq *fpp;
1184462b86feSPoul-Henning Kamp 
1185462b86feSPoul-Henning Kamp 				fpp = fp;
1186462b86feSPoul-Henning Kamp 				fp = TAILQ_NEXT(fp, ipq_list);
1187462b86feSPoul-Henning Kamp 				if(--fpp->ipq_ttl == 0) {
118886425c62SRobert Watson 					IPSTAT_ADD(ips_fragtimeout,
118986425c62SRobert Watson 					    fpp->ipq_nfrags);
1190603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], fpp);
1191df8bae1dSRodney W. Grimes 				}
1192df8bae1dSRodney W. Grimes 			}
1193194a213eSAndrey A. Chernov 		}
1194690a6055SJesper Skriver 		/*
1195690a6055SJesper Skriver 		 * If we are over the maximum number of fragments
1196690a6055SJesper Skriver 		 * (due to the limit being lowered), drain off
1197690a6055SJesper Skriver 		 * enough to get down to the new limit.
1198690a6055SJesper Skriver 		 */
1199603724d3SBjoern A. Zeeb 		if (V_maxnipq >= 0 && V_nipq > V_maxnipq) {
1200690a6055SJesper Skriver 			for (i = 0; i < IPREASS_NHASH; i++) {
12018b615593SMarko Zec 				while (V_nipq > V_maxnipq &&
12028b615593SMarko Zec 				    !TAILQ_EMPTY(&V_ipq[i])) {
120386425c62SRobert Watson 					IPSTAT_ADD(ips_fragdropped,
120486425c62SRobert Watson 					    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
12058b615593SMarko Zec 					ip_freef(&V_ipq[i],
12068b615593SMarko Zec 					    TAILQ_FIRST(&V_ipq[i]));
1207690a6055SJesper Skriver 				}
1208690a6055SJesper Skriver 			}
1209690a6055SJesper Skriver 		}
12108b615593SMarko Zec 		CURVNET_RESTORE();
12118b615593SMarko Zec 	}
12128b615593SMarko Zec 	VNET_LIST_RUNLOCK();
12132fad1e93SSam Leffler 	IPQ_UNLOCK();
1214df8bae1dSRodney W. Grimes }
1215df8bae1dSRodney W. Grimes 
1216df8bae1dSRodney W. Grimes /*
1217df8bae1dSRodney W. Grimes  * Drain off all datagram fragments.
1218df8bae1dSRodney W. Grimes  */
1219df8bae1dSRodney W. Grimes void
1220f2565d68SRobert Watson ip_drain(void)
1221df8bae1dSRodney W. Grimes {
12228b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1223194a213eSAndrey A. Chernov 	int     i;
1224ce29ab3aSGarrett Wollman 
12252fad1e93SSam Leffler 	IPQ_LOCK();
12268b615593SMarko Zec 	VNET_LIST_RLOCK();
12278b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
12288b615593SMarko Zec 		CURVNET_SET(vnet_iter);
12298b615593SMarko Zec 		INIT_VNET_INET(vnet_iter);
1230194a213eSAndrey A. Chernov 		for (i = 0; i < IPREASS_NHASH; i++) {
1231603724d3SBjoern A. Zeeb 			while(!TAILQ_EMPTY(&V_ipq[i])) {
123286425c62SRobert Watson 				IPSTAT_ADD(ips_fragdropped,
123386425c62SRobert Watson 				    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
1234603724d3SBjoern A. Zeeb 				ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1235194a213eSAndrey A. Chernov 			}
1236194a213eSAndrey A. Chernov 		}
12378b615593SMarko Zec 		CURVNET_RESTORE();
12388b615593SMarko Zec 	}
12398b615593SMarko Zec 	VNET_LIST_RUNLOCK();
12402fad1e93SSam Leffler 	IPQ_UNLOCK();
1241ce29ab3aSGarrett Wollman 	in_rtqdrain();
1242df8bae1dSRodney W. Grimes }
1243df8bae1dSRodney W. Grimes 
1244df8bae1dSRodney W. Grimes /*
1245de38924dSAndre Oppermann  * The protocol to be inserted into ip_protox[] must be already registered
1246de38924dSAndre Oppermann  * in inetsw[], either statically or through pf_proto_register().
1247de38924dSAndre Oppermann  */
1248de38924dSAndre Oppermann int
1249de38924dSAndre Oppermann ipproto_register(u_char ipproto)
1250de38924dSAndre Oppermann {
1251de38924dSAndre Oppermann 	struct protosw *pr;
1252de38924dSAndre Oppermann 
1253de38924dSAndre Oppermann 	/* Sanity checks. */
1254de38924dSAndre Oppermann 	if (ipproto == 0)
1255de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1256de38924dSAndre Oppermann 
1257de38924dSAndre Oppermann 	/*
1258de38924dSAndre Oppermann 	 * The protocol slot must not be occupied by another protocol
1259de38924dSAndre Oppermann 	 * already.  An index pointing to IPPROTO_RAW is unused.
1260de38924dSAndre Oppermann 	 */
1261de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1262de38924dSAndre Oppermann 	if (pr == NULL)
1263de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1264de38924dSAndre Oppermann 	if (ip_protox[ipproto] != pr - inetsw)	/* IPPROTO_RAW */
1265de38924dSAndre Oppermann 		return (EEXIST);
1266de38924dSAndre Oppermann 
1267de38924dSAndre Oppermann 	/* Find the protocol position in inetsw[] and set the index. */
1268de38924dSAndre Oppermann 	for (pr = inetdomain.dom_protosw;
1269de38924dSAndre Oppermann 	     pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1270de38924dSAndre Oppermann 		if (pr->pr_domain->dom_family == PF_INET &&
1271de38924dSAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol == ipproto) {
1272de38924dSAndre Oppermann 			/* Be careful to only index valid IP protocols. */
1273db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX) {
1274de38924dSAndre Oppermann 				ip_protox[pr->pr_protocol] = pr - inetsw;
1275de38924dSAndre Oppermann 				return (0);
1276de38924dSAndre Oppermann 			} else
1277de38924dSAndre Oppermann 				return (EINVAL);
1278de38924dSAndre Oppermann 		}
1279de38924dSAndre Oppermann 	}
1280de38924dSAndre Oppermann 	return (EPROTONOSUPPORT);
1281de38924dSAndre Oppermann }
1282de38924dSAndre Oppermann 
1283de38924dSAndre Oppermann int
1284de38924dSAndre Oppermann ipproto_unregister(u_char ipproto)
1285de38924dSAndre Oppermann {
1286de38924dSAndre Oppermann 	struct protosw *pr;
1287de38924dSAndre Oppermann 
1288de38924dSAndre Oppermann 	/* Sanity checks. */
1289de38924dSAndre Oppermann 	if (ipproto == 0)
1290de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1291de38924dSAndre Oppermann 
1292de38924dSAndre Oppermann 	/* Check if the protocol was indeed registered. */
1293de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1294de38924dSAndre Oppermann 	if (pr == NULL)
1295de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1296de38924dSAndre Oppermann 	if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
1297de38924dSAndre Oppermann 		return (ENOENT);
1298de38924dSAndre Oppermann 
1299de38924dSAndre Oppermann 	/* Reset the protocol slot to IPPROTO_RAW. */
1300de38924dSAndre Oppermann 	ip_protox[ipproto] = pr - inetsw;
1301de38924dSAndre Oppermann 	return (0);
1302de38924dSAndre Oppermann }
1303de38924dSAndre Oppermann 
1304df8bae1dSRodney W. Grimes /*
1305df8bae1dSRodney W. Grimes  * Given address of next destination (final or next hop),
1306df8bae1dSRodney W. Grimes  * return internet address info of interface to be used to get there.
1307df8bae1dSRodney W. Grimes  */
1308bd714208SRuslan Ermilov struct in_ifaddr *
13098b07e49aSJulian Elischer ip_rtaddr(struct in_addr dst, u_int fibnum)
1310df8bae1dSRodney W. Grimes {
131197d8d152SAndre Oppermann 	struct route sro;
131202c1c707SAndre Oppermann 	struct sockaddr_in *sin;
131302c1c707SAndre Oppermann 	struct in_ifaddr *ifa;
1314df8bae1dSRodney W. Grimes 
13150cfbbe3bSAndre Oppermann 	bzero(&sro, sizeof(sro));
131697d8d152SAndre Oppermann 	sin = (struct sockaddr_in *)&sro.ro_dst;
1317df8bae1dSRodney W. Grimes 	sin->sin_family = AF_INET;
1318df8bae1dSRodney W. Grimes 	sin->sin_len = sizeof(*sin);
1319df8bae1dSRodney W. Grimes 	sin->sin_addr = dst;
13206e6b3f7cSQing Li 	in_rtalloc_ign(&sro, 0, fibnum);
1321df8bae1dSRodney W. Grimes 
132297d8d152SAndre Oppermann 	if (sro.ro_rt == NULL)
132302410549SRobert Watson 		return (NULL);
132402c1c707SAndre Oppermann 
132597d8d152SAndre Oppermann 	ifa = ifatoia(sro.ro_rt->rt_ifa);
132697d8d152SAndre Oppermann 	RTFREE(sro.ro_rt);
132702410549SRobert Watson 	return (ifa);
1328df8bae1dSRodney W. Grimes }
1329df8bae1dSRodney W. Grimes 
1330df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = {
1331df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1332df8bae1dSRodney W. Grimes 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1333df8bae1dSRodney W. Grimes 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1334df8bae1dSRodney W. Grimes 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1335fcaf9f91SMike Silbersack 	0,		0,		EHOSTUNREACH,	0,
13363b8123b7SJesper Skriver 	ENOPROTOOPT,	ECONNREFUSED
1337df8bae1dSRodney W. Grimes };
1338df8bae1dSRodney W. Grimes 
1339df8bae1dSRodney W. Grimes /*
1340df8bae1dSRodney W. Grimes  * Forward a packet.  If some error occurs return the sender
1341df8bae1dSRodney W. Grimes  * an icmp packet.  Note we can't always generate a meaningful
1342df8bae1dSRodney W. Grimes  * icmp message because icmp doesn't have a large enough repertoire
1343df8bae1dSRodney W. Grimes  * of codes and types.
1344df8bae1dSRodney W. Grimes  *
1345df8bae1dSRodney W. Grimes  * If not forwarding, just drop the packet.  This could be confusing
1346df8bae1dSRodney W. Grimes  * if ipforwarding was zero but some routing protocol was advancing
1347df8bae1dSRodney W. Grimes  * us as a gateway to somewhere.  However, we must let the routing
1348df8bae1dSRodney W. Grimes  * protocol deal with that.
1349df8bae1dSRodney W. Grimes  *
1350df8bae1dSRodney W. Grimes  * The srcrt parameter indicates whether the packet is being forwarded
1351df8bae1dSRodney W. Grimes  * via a source route.
1352df8bae1dSRodney W. Grimes  */
13539b932e9eSAndre Oppermann void
13549b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt)
1355df8bae1dSRodney W. Grimes {
13568b615593SMarko Zec 	INIT_VNET_INET(curvnet);
13572b25acc1SLuigi Rizzo 	struct ip *ip = mtod(m, struct ip *);
13589b932e9eSAndre Oppermann 	struct in_ifaddr *ia = NULL;
1359df8bae1dSRodney W. Grimes 	struct mbuf *mcopy;
13609b932e9eSAndre Oppermann 	struct in_addr dest;
1361b835b6feSBjoern A. Zeeb 	struct route ro;
1362c773494eSAndre Oppermann 	int error, type = 0, code = 0, mtu = 0;
13633efc3014SJulian Elischer 
13649b932e9eSAndre Oppermann 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
136586425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
1366df8bae1dSRodney W. Grimes 		m_freem(m);
1367df8bae1dSRodney W. Grimes 		return;
1368df8bae1dSRodney W. Grimes 	}
13691b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
1370603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
13711b968362SDag-Erling Smørgrav #endif
1372df8bae1dSRodney W. Grimes 		if (ip->ip_ttl <= IPTTLDEC) {
13731b968362SDag-Erling Smørgrav 			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
137402c1c707SAndre Oppermann 			    0, 0);
1375df8bae1dSRodney W. Grimes 			return;
1376df8bae1dSRodney W. Grimes 		}
13771b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
13781b968362SDag-Erling Smørgrav 	}
13791b968362SDag-Erling Smørgrav #endif
1380df8bae1dSRodney W. Grimes 
13818b07e49aSJulian Elischer 	ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
1382d23d475fSGuido van Rooij 	if (!srcrt && ia == NULL) {
138302c1c707SAndre Oppermann 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1384df8bae1dSRodney W. Grimes 		return;
138502c1c707SAndre Oppermann 	}
1386df8bae1dSRodney W. Grimes 
1387df8bae1dSRodney W. Grimes 	/*
1388bfef7ed4SIan Dowse 	 * Save the IP header and at most 8 bytes of the payload,
1389bfef7ed4SIan Dowse 	 * in case we need to generate an ICMP message to the src.
1390bfef7ed4SIan Dowse 	 *
13914d2e3692SLuigi Rizzo 	 * XXX this can be optimized a lot by saving the data in a local
13924d2e3692SLuigi Rizzo 	 * buffer on the stack (72 bytes at most), and only allocating the
13934d2e3692SLuigi Rizzo 	 * mbuf if really necessary. The vast majority of the packets
13944d2e3692SLuigi Rizzo 	 * are forwarded without having to send an ICMP back (either
13954d2e3692SLuigi Rizzo 	 * because unnecessary, or because rate limited), so we are
13964d2e3692SLuigi Rizzo 	 * really we are wasting a lot of work here.
13974d2e3692SLuigi Rizzo 	 *
1398bfef7ed4SIan Dowse 	 * We don't use m_copy() because it might return a reference
1399bfef7ed4SIan Dowse 	 * to a shared cluster. Both this function and ip_output()
1400bfef7ed4SIan Dowse 	 * assume exclusive access to the IP header in `m', so any
1401bfef7ed4SIan Dowse 	 * data in a cluster may change before we reach icmp_error().
1402df8bae1dSRodney W. Grimes 	 */
1403780b2f69SAndre Oppermann 	MGETHDR(mcopy, M_DONTWAIT, m->m_type);
1404a163d034SWarner Losh 	if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) {
14059967cafcSSam Leffler 		/*
14069967cafcSSam Leffler 		 * It's probably ok if the pkthdr dup fails (because
14079967cafcSSam Leffler 		 * the deep copy of the tag chain failed), but for now
14089967cafcSSam Leffler 		 * be conservative and just discard the copy since
14099967cafcSSam Leffler 		 * code below may some day want the tags.
14109967cafcSSam Leffler 		 */
14119967cafcSSam Leffler 		m_free(mcopy);
14129967cafcSSam Leffler 		mcopy = NULL;
14139967cafcSSam Leffler 	}
1414bfef7ed4SIan Dowse 	if (mcopy != NULL) {
1415780b2f69SAndre Oppermann 		mcopy->m_len = min(ip->ip_len, M_TRAILINGSPACE(mcopy));
1416e6b0a570SBruce M Simpson 		mcopy->m_pkthdr.len = mcopy->m_len;
1417bfef7ed4SIan Dowse 		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1418bfef7ed4SIan Dowse 	}
141904287599SRuslan Ermilov 
142004287599SRuslan Ermilov #ifdef IPSTEALTH
1421603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
142204287599SRuslan Ermilov #endif
142304287599SRuslan Ermilov 		ip->ip_ttl -= IPTTLDEC;
142404287599SRuslan Ermilov #ifdef IPSTEALTH
142504287599SRuslan Ermilov 	}
142604287599SRuslan Ermilov #endif
1427df8bae1dSRodney W. Grimes 
1428df8bae1dSRodney W. Grimes 	/*
1429df8bae1dSRodney W. Grimes 	 * If forwarding packet using same interface that it came in on,
1430df8bae1dSRodney W. Grimes 	 * perhaps should send a redirect to sender to shortcut a hop.
1431df8bae1dSRodney W. Grimes 	 * Only send redirect if source is sending directly to us,
1432df8bae1dSRodney W. Grimes 	 * and if packet was not source routed (or has any options).
1433df8bae1dSRodney W. Grimes 	 * Also, don't send redirect if forwarding using a default route
1434df8bae1dSRodney W. Grimes 	 * or a route modified by a redirect.
1435df8bae1dSRodney W. Grimes 	 */
14369b932e9eSAndre Oppermann 	dest.s_addr = 0;
1437603724d3SBjoern A. Zeeb 	if (!srcrt && V_ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) {
143802c1c707SAndre Oppermann 		struct sockaddr_in *sin;
143902c1c707SAndre Oppermann 		struct rtentry *rt;
144002c1c707SAndre Oppermann 
14410cfbbe3bSAndre Oppermann 		bzero(&ro, sizeof(ro));
144202c1c707SAndre Oppermann 		sin = (struct sockaddr_in *)&ro.ro_dst;
144302c1c707SAndre Oppermann 		sin->sin_family = AF_INET;
144402c1c707SAndre Oppermann 		sin->sin_len = sizeof(*sin);
14459b932e9eSAndre Oppermann 		sin->sin_addr = ip->ip_dst;
14466e6b3f7cSQing Li 		in_rtalloc_ign(&ro, 0, M_GETFIB(m));
144702c1c707SAndre Oppermann 
144802c1c707SAndre Oppermann 		rt = ro.ro_rt;
144902c1c707SAndre Oppermann 
145002c1c707SAndre Oppermann 		if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
14519b932e9eSAndre Oppermann 		    satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1452df8bae1dSRodney W. Grimes #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1453df8bae1dSRodney W. Grimes 			u_long src = ntohl(ip->ip_src.s_addr);
1454df8bae1dSRodney W. Grimes 
1455df8bae1dSRodney W. Grimes 			if (RTA(rt) &&
1456df8bae1dSRodney W. Grimes 			    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1457df8bae1dSRodney W. Grimes 				if (rt->rt_flags & RTF_GATEWAY)
14589b932e9eSAndre Oppermann 					dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1459df8bae1dSRodney W. Grimes 				else
14609b932e9eSAndre Oppermann 					dest.s_addr = ip->ip_dst.s_addr;
1461df8bae1dSRodney W. Grimes 				/* Router requirements says to only send host redirects */
1462df8bae1dSRodney W. Grimes 				type = ICMP_REDIRECT;
1463df8bae1dSRodney W. Grimes 				code = ICMP_REDIRECT_HOST;
1464df8bae1dSRodney W. Grimes 			}
1465df8bae1dSRodney W. Grimes 		}
146602c1c707SAndre Oppermann 		if (rt)
146702c1c707SAndre Oppermann 			RTFREE(rt);
146802c1c707SAndre Oppermann 	}
1469df8bae1dSRodney W. Grimes 
1470b835b6feSBjoern A. Zeeb 	/*
1471b835b6feSBjoern A. Zeeb 	 * Try to cache the route MTU from ip_output so we can consider it for
1472b835b6feSBjoern A. Zeeb 	 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191.
1473b835b6feSBjoern A. Zeeb 	 */
1474b835b6feSBjoern A. Zeeb 	bzero(&ro, sizeof(ro));
1475b835b6feSBjoern A. Zeeb 
1476b835b6feSBjoern A. Zeeb 	error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1477b835b6feSBjoern A. Zeeb 
1478b835b6feSBjoern A. Zeeb 	if (error == EMSGSIZE && ro.ro_rt)
1479b835b6feSBjoern A. Zeeb 		mtu = ro.ro_rt->rt_rmx.rmx_mtu;
1480b835b6feSBjoern A. Zeeb 	if (ro.ro_rt)
1481b835b6feSBjoern A. Zeeb 		RTFREE(ro.ro_rt);
1482b835b6feSBjoern A. Zeeb 
1483df8bae1dSRodney W. Grimes 	if (error)
148486425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
1485df8bae1dSRodney W. Grimes 	else {
148686425c62SRobert Watson 		IPSTAT_INC(ips_forward);
1487df8bae1dSRodney W. Grimes 		if (type)
148886425c62SRobert Watson 			IPSTAT_INC(ips_redirectsent);
1489df8bae1dSRodney W. Grimes 		else {
14909188b4a1SAndre Oppermann 			if (mcopy)
1491df8bae1dSRodney W. Grimes 				m_freem(mcopy);
1492df8bae1dSRodney W. Grimes 			return;
1493df8bae1dSRodney W. Grimes 		}
1494df8bae1dSRodney W. Grimes 	}
1495df8bae1dSRodney W. Grimes 	if (mcopy == NULL)
1496df8bae1dSRodney W. Grimes 		return;
1497df8bae1dSRodney W. Grimes 
1498df8bae1dSRodney W. Grimes 	switch (error) {
1499df8bae1dSRodney W. Grimes 
1500df8bae1dSRodney W. Grimes 	case 0:				/* forwarded, but need redirect */
1501df8bae1dSRodney W. Grimes 		/* type, code set above */
1502df8bae1dSRodney W. Grimes 		break;
1503df8bae1dSRodney W. Grimes 
1504df8bae1dSRodney W. Grimes 	case ENETUNREACH:		/* shouldn't happen, checked above */
1505df8bae1dSRodney W. Grimes 	case EHOSTUNREACH:
1506df8bae1dSRodney W. Grimes 	case ENETDOWN:
1507df8bae1dSRodney W. Grimes 	case EHOSTDOWN:
1508df8bae1dSRodney W. Grimes 	default:
1509df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1510df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_HOST;
1511df8bae1dSRodney W. Grimes 		break;
1512df8bae1dSRodney W. Grimes 
1513df8bae1dSRodney W. Grimes 	case EMSGSIZE:
1514df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1515df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_NEEDFRAG;
15161dfcf0d2SAndre Oppermann 
1517b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
1518b835b6feSBjoern A. Zeeb 		/*
1519b835b6feSBjoern A. Zeeb 		 * If IPsec is configured for this path,
1520b835b6feSBjoern A. Zeeb 		 * override any possibly mtu value set by ip_output.
1521b835b6feSBjoern A. Zeeb 		 */
1522b835b6feSBjoern A. Zeeb 		mtu = ip_ipsec_mtu(m, mtu);
1523b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
15249b932e9eSAndre Oppermann 		/*
1525b835b6feSBjoern A. Zeeb 		 * If the MTU was set before make sure we are below the
1526b835b6feSBjoern A. Zeeb 		 * interface MTU.
1527ab48768bSAndre Oppermann 		 * If the MTU wasn't set before use the interface mtu or
1528ab48768bSAndre Oppermann 		 * fall back to the next smaller mtu step compared to the
1529ab48768bSAndre Oppermann 		 * current packet size.
15309b932e9eSAndre Oppermann 		 */
1531b835b6feSBjoern A. Zeeb 		if (mtu != 0) {
1532b835b6feSBjoern A. Zeeb 			if (ia != NULL)
1533b835b6feSBjoern A. Zeeb 				mtu = min(mtu, ia->ia_ifp->if_mtu);
1534b835b6feSBjoern A. Zeeb 		} else {
1535ab48768bSAndre Oppermann 			if (ia != NULL)
1536c773494eSAndre Oppermann 				mtu = ia->ia_ifp->if_mtu;
1537ab48768bSAndre Oppermann 			else
1538ab48768bSAndre Oppermann 				mtu = ip_next_mtu(ip->ip_len, 0);
1539ab48768bSAndre Oppermann 		}
154086425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
1541df8bae1dSRodney W. Grimes 		break;
1542df8bae1dSRodney W. Grimes 
1543df8bae1dSRodney W. Grimes 	case ENOBUFS:
1544df285b3dSMike Silbersack 		/*
1545df285b3dSMike Silbersack 		 * A router should not generate ICMP_SOURCEQUENCH as
1546df285b3dSMike Silbersack 		 * required in RFC1812 Requirements for IP Version 4 Routers.
1547df285b3dSMike Silbersack 		 * Source quench could be a big problem under DoS attacks,
1548df285b3dSMike Silbersack 		 * or if the underlying interface is rate-limited.
1549df285b3dSMike Silbersack 		 * Those who need source quench packets may re-enable them
1550df285b3dSMike Silbersack 		 * via the net.inet.ip.sendsourcequench sysctl.
1551df285b3dSMike Silbersack 		 */
1552603724d3SBjoern A. Zeeb 		if (V_ip_sendsourcequench == 0) {
1553df285b3dSMike Silbersack 			m_freem(mcopy);
1554df285b3dSMike Silbersack 			return;
1555df285b3dSMike Silbersack 		} else {
1556df8bae1dSRodney W. Grimes 			type = ICMP_SOURCEQUENCH;
1557df8bae1dSRodney W. Grimes 			code = 0;
1558df285b3dSMike Silbersack 		}
1559df8bae1dSRodney W. Grimes 		break;
15603a06e3e0SRuslan Ermilov 
15613a06e3e0SRuslan Ermilov 	case EACCES:			/* ipfw denied packet */
15623a06e3e0SRuslan Ermilov 		m_freem(mcopy);
15633a06e3e0SRuslan Ermilov 		return;
1564df8bae1dSRodney W. Grimes 	}
1565c773494eSAndre Oppermann 	icmp_error(mcopy, type, code, dest.s_addr, mtu);
1566df8bae1dSRodney W. Grimes }
1567df8bae1dSRodney W. Grimes 
156882c23ebaSBill Fenner void
1569f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1570f2565d68SRobert Watson     struct mbuf *m)
157182c23ebaSBill Fenner {
15728b615593SMarko Zec 	INIT_VNET_NET(inp->inp_vnet);
15738b615593SMarko Zec 
1574be8a62e8SPoul-Henning Kamp 	if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1575be8a62e8SPoul-Henning Kamp 		struct bintime bt;
1576be8a62e8SPoul-Henning Kamp 
1577be8a62e8SPoul-Henning Kamp 		bintime(&bt);
1578be8a62e8SPoul-Henning Kamp 		if (inp->inp_socket->so_options & SO_BINTIME) {
1579be8a62e8SPoul-Henning Kamp 			*mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt),
1580be8a62e8SPoul-Henning Kamp 			SCM_BINTIME, SOL_SOCKET);
1581be8a62e8SPoul-Henning Kamp 			if (*mp)
1582be8a62e8SPoul-Henning Kamp 				mp = &(*mp)->m_next;
1583be8a62e8SPoul-Henning Kamp 		}
158482c23ebaSBill Fenner 		if (inp->inp_socket->so_options & SO_TIMESTAMP) {
158582c23ebaSBill Fenner 			struct timeval tv;
158682c23ebaSBill Fenner 
1587be8a62e8SPoul-Henning Kamp 			bintime2timeval(&bt, &tv);
158882c23ebaSBill Fenner 			*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
158982c23ebaSBill Fenner 				SCM_TIMESTAMP, SOL_SOCKET);
159082c23ebaSBill Fenner 			if (*mp)
159182c23ebaSBill Fenner 				mp = &(*mp)->m_next;
15924cc20ab1SSeigo Tanimura 		}
1593be8a62e8SPoul-Henning Kamp 	}
159482c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVDSTADDR) {
159582c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
159682c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
159782c23ebaSBill Fenner 		if (*mp)
159882c23ebaSBill Fenner 			mp = &(*mp)->m_next;
159982c23ebaSBill Fenner 	}
16004957466bSMatthew N. Dodd 	if (inp->inp_flags & INP_RECVTTL) {
16014957466bSMatthew N. Dodd 		*mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
16024957466bSMatthew N. Dodd 		    sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
16034957466bSMatthew N. Dodd 		if (*mp)
16044957466bSMatthew N. Dodd 			mp = &(*mp)->m_next;
16054957466bSMatthew N. Dodd 	}
160682c23ebaSBill Fenner #ifdef notyet
160782c23ebaSBill Fenner 	/* XXX
160882c23ebaSBill Fenner 	 * Moving these out of udp_input() made them even more broken
160982c23ebaSBill Fenner 	 * than they already were.
161082c23ebaSBill Fenner 	 */
161182c23ebaSBill Fenner 	/* options were tossed already */
161282c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVOPTS) {
161382c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
161482c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
161582c23ebaSBill Fenner 		if (*mp)
161682c23ebaSBill Fenner 			mp = &(*mp)->m_next;
161782c23ebaSBill Fenner 	}
161882c23ebaSBill Fenner 	/* ip_srcroute doesn't do what we want here, need to fix */
161982c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVRETOPTS) {
1620e0982661SAndre Oppermann 		*mp = sbcreatecontrol((caddr_t) ip_srcroute(m),
162182c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
162282c23ebaSBill Fenner 		if (*mp)
162382c23ebaSBill Fenner 			mp = &(*mp)->m_next;
162482c23ebaSBill Fenner 	}
162582c23ebaSBill Fenner #endif
162682c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVIF) {
1627d314ad7bSJulian Elischer 		struct ifnet *ifp;
1628d314ad7bSJulian Elischer 		struct sdlbuf {
162982c23ebaSBill Fenner 			struct sockaddr_dl sdl;
1630d314ad7bSJulian Elischer 			u_char	pad[32];
1631d314ad7bSJulian Elischer 		} sdlbuf;
1632d314ad7bSJulian Elischer 		struct sockaddr_dl *sdp;
1633d314ad7bSJulian Elischer 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
163482c23ebaSBill Fenner 
1635d314ad7bSJulian Elischer 		if (((ifp = m->m_pkthdr.rcvif))
1636603724d3SBjoern A. Zeeb 		&& ( ifp->if_index && (ifp->if_index <= V_if_index))) {
16374a0d6638SRuslan Ermilov 			sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1638d314ad7bSJulian Elischer 			/*
1639d314ad7bSJulian Elischer 			 * Change our mind and don't try copy.
1640d314ad7bSJulian Elischer 			 */
1641d314ad7bSJulian Elischer 			if ((sdp->sdl_family != AF_LINK)
1642d314ad7bSJulian Elischer 			|| (sdp->sdl_len > sizeof(sdlbuf))) {
1643d314ad7bSJulian Elischer 				goto makedummy;
1644d314ad7bSJulian Elischer 			}
1645d314ad7bSJulian Elischer 			bcopy(sdp, sdl2, sdp->sdl_len);
1646d314ad7bSJulian Elischer 		} else {
1647d314ad7bSJulian Elischer makedummy:
1648d314ad7bSJulian Elischer 			sdl2->sdl_len
1649d314ad7bSJulian Elischer 				= offsetof(struct sockaddr_dl, sdl_data[0]);
1650d314ad7bSJulian Elischer 			sdl2->sdl_family = AF_LINK;
1651d314ad7bSJulian Elischer 			sdl2->sdl_index = 0;
1652d314ad7bSJulian Elischer 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1653d314ad7bSJulian Elischer 		}
1654d314ad7bSJulian Elischer 		*mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
165582c23ebaSBill Fenner 			IP_RECVIF, IPPROTO_IP);
165682c23ebaSBill Fenner 		if (*mp)
165782c23ebaSBill Fenner 			mp = &(*mp)->m_next;
165882c23ebaSBill Fenner 	}
165982c23ebaSBill Fenner }
166082c23ebaSBill Fenner 
16614d2e3692SLuigi Rizzo /*
166230916a2dSRobert Watson  * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
166330916a2dSRobert Watson  * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
166430916a2dSRobert Watson  * locking.  This code remains in ip_input.c as ip_mroute.c is optionally
166530916a2dSRobert Watson  * compiled.
16664d2e3692SLuigi Rizzo  */
1667df8bae1dSRodney W. Grimes int
1668f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so)
1669f0068c4aSGarrett Wollman {
16708b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
16718b615593SMarko Zec 
1672f0068c4aSGarrett Wollman 	if (so->so_type != SOCK_RAW ||
1673f0068c4aSGarrett Wollman 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1674f0068c4aSGarrett Wollman 		return EOPNOTSUPP;
1675f0068c4aSGarrett Wollman 
1676603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL)
1677f0068c4aSGarrett Wollman 		return EADDRINUSE;
1678f0068c4aSGarrett Wollman 
1679603724d3SBjoern A. Zeeb 	V_ip_rsvpd = so;
16801c5de19aSGarrett Wollman 	/*
16811c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-increment
16821c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
16831c5de19aSGarrett Wollman 	 */
1684603724d3SBjoern A. Zeeb 	if (!V_ip_rsvp_on) {
1685603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 1;
1686603724d3SBjoern A. Zeeb 		V_rsvp_on++;
16871c5de19aSGarrett Wollman 	}
1688f0068c4aSGarrett Wollman 
1689f0068c4aSGarrett Wollman 	return 0;
1690f0068c4aSGarrett Wollman }
1691f0068c4aSGarrett Wollman 
1692f0068c4aSGarrett Wollman int
1693f0068c4aSGarrett Wollman ip_rsvp_done(void)
1694f0068c4aSGarrett Wollman {
16958b615593SMarko Zec 	INIT_VNET_INET(curvnet);
16968b615593SMarko Zec 
1697603724d3SBjoern A. Zeeb 	V_ip_rsvpd = NULL;
16981c5de19aSGarrett Wollman 	/*
16991c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-decrement
17001c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
17011c5de19aSGarrett Wollman 	 */
1702603724d3SBjoern A. Zeeb 	if (V_ip_rsvp_on) {
1703603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 0;
1704603724d3SBjoern A. Zeeb 		V_rsvp_on--;
17051c5de19aSGarrett Wollman 	}
1706f0068c4aSGarrett Wollman 	return 0;
1707f0068c4aSGarrett Wollman }
1708bbb4330bSLuigi Rizzo 
1709bbb4330bSLuigi Rizzo void
1710bbb4330bSLuigi Rizzo rsvp_input(struct mbuf *m, int off)	/* XXX must fixup manually */
1711bbb4330bSLuigi Rizzo {
17128b615593SMarko Zec 	INIT_VNET_INET(curvnet);
17138b615593SMarko Zec 
1714bbb4330bSLuigi Rizzo 	if (rsvp_input_p) { /* call the real one if loaded */
1715bbb4330bSLuigi Rizzo 		rsvp_input_p(m, off);
1716bbb4330bSLuigi Rizzo 		return;
1717bbb4330bSLuigi Rizzo 	}
1718bbb4330bSLuigi Rizzo 
1719bbb4330bSLuigi Rizzo 	/* Can still get packets with rsvp_on = 0 if there is a local member
1720bbb4330bSLuigi Rizzo 	 * of the group to which the RSVP packet is addressed.  But in this
1721bbb4330bSLuigi Rizzo 	 * case we want to throw the packet away.
1722bbb4330bSLuigi Rizzo 	 */
1723bbb4330bSLuigi Rizzo 
1724603724d3SBjoern A. Zeeb 	if (!V_rsvp_on) {
1725bbb4330bSLuigi Rizzo 		m_freem(m);
1726bbb4330bSLuigi Rizzo 		return;
1727bbb4330bSLuigi Rizzo 	}
1728bbb4330bSLuigi Rizzo 
1729603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL) {
1730bbb4330bSLuigi Rizzo 		rip_input(m, off);
1731bbb4330bSLuigi Rizzo 		return;
1732bbb4330bSLuigi Rizzo 	}
1733bbb4330bSLuigi Rizzo 	/* Drop the packet */
1734bbb4330bSLuigi Rizzo 	m_freem(m);
1735bbb4330bSLuigi Rizzo }
1736