xref: /freebsd/sys/netinet/ip_input.c (revision 2d9cfabad4b7d8d7589a825030ac7165b29a6e00)
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"
40a9771948SGleb Smirnoff #include "opt_carp.h"
4174a9466cSGary Palmer 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
445f311da2SMike Silbersack #include <sys/callout.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46b715f178SLuigi Rizzo #include <sys/malloc.h>
47df8bae1dSRodney W. Grimes #include <sys/domain.h>
48df8bae1dSRodney W. Grimes #include <sys/protosw.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/time.h>
51df8bae1dSRodney W. Grimes #include <sys/kernel.h>
52385195c0SMarko Zec #include <sys/lock.h>
53385195c0SMarko Zec #include <sys/rwlock.h>
541025071fSGarrett Wollman #include <sys/syslog.h>
55b5e8ce9fSBruce Evans #include <sys/sysctl.h>
56603724d3SBjoern A. Zeeb #include <sys/vimage.h>
57df8bae1dSRodney W. Grimes 
58c85540ddSAndrey A. Chernov #include <net/pfil.h>
59df8bae1dSRodney W. Grimes #include <net/if.h>
609494d596SBrooks Davis #include <net/if_types.h>
61d314ad7bSJulian Elischer #include <net/if_var.h>
6282c23ebaSBill Fenner #include <net/if_dl.h>
63df8bae1dSRodney W. Grimes #include <net/route.h>
64748e0b0aSGarrett Wollman #include <net/netisr.h>
654b79449eSBjoern A. Zeeb #include <net/vnet.h>
6665111ec7SKip Macy #include <net/flowtable.h>
67df8bae1dSRodney W. Grimes 
68df8bae1dSRodney W. Grimes #include <netinet/in.h>
69df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
70b5e8ce9fSBruce Evans #include <netinet/in_var.h>
71df8bae1dSRodney W. Grimes #include <netinet/ip.h>
72df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
73df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
74df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
75ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
7658938916SGarrett Wollman #include <machine/in_cksum.h>
774b79449eSBjoern A. Zeeb #include <netinet/vinet.h>
78a9771948SGleb Smirnoff #ifdef DEV_CARP
79a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
80a9771948SGleb Smirnoff #endif
81b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
821dfcf0d2SAndre Oppermann #include <netinet/ip_ipsec.h>
83b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
84df8bae1dSRodney W. Grimes 
85f0068c4aSGarrett Wollman #include <sys/socketvar.h>
866ddbf1e2SGary Palmer 
87aed55708SRobert Watson #include <security/mac/mac_framework.h>
88aed55708SRobert Watson 
89d2035ffbSEd Maste #ifdef CTASSERT
90d2035ffbSEd Maste CTASSERT(sizeof(struct ip) == 20);
91d2035ffbSEd Maste #endif
92d2035ffbSEd Maste 
93385195c0SMarko Zec #ifndef VIMAGE
94385195c0SMarko Zec #ifndef VIMAGE_GLOBALS
95385195c0SMarko Zec struct vnet_inet vnet_inet_0;
96385195c0SMarko Zec #endif
97385195c0SMarko Zec #endif
98385195c0SMarko Zec 
9944e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
10044e33a07SMarko Zec static int	ipsendredirects;
10144e33a07SMarko Zec static int	ip_checkinterface;
10244e33a07SMarko Zec static int	ip_keepfaith;
10344e33a07SMarko Zec static int	ip_sendsourcequench;
10444e33a07SMarko Zec int	ip_defttl;
10544e33a07SMarko Zec int	ip_do_randomid;
10644e33a07SMarko Zec int	ipforwarding;
10744e33a07SMarko Zec struct	in_ifaddrhead in_ifaddrhead; 		/* first inet address */
10844e33a07SMarko Zec struct	in_ifaddrhashhead *in_ifaddrhashtbl;	/* inet addr hash table  */
10944e33a07SMarko Zec u_long 	in_ifaddrhmask;				/* mask for hash table */
11044e33a07SMarko Zec struct ipstat ipstat;
11144e33a07SMarko Zec static int ip_rsvp_on;
11244e33a07SMarko Zec struct socket *ip_rsvpd;
11344e33a07SMarko Zec int	rsvp_on;
114f02493cbSMarko Zec static struct ipqhead ipq[IPREASS_NHASH];
11544e33a07SMarko Zec static int	maxnipq;	/* Administrative limit on # reass queues. */
11644e33a07SMarko Zec static int	maxfragsperpacket;
11744e33a07SMarko Zec int	ipstealth;
11844e33a07SMarko Zec static int	nipq;	/* Total # of reass queues */
11944e33a07SMarko Zec #endif
1202d9cfabaSRobert Watson struct	rwlock in_ifaddr_lock;
121f0068c4aSGarrett Wollman 
1228b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_FORWARDING,
1238b615593SMarko Zec     forwarding, CTLFLAG_RW, ipforwarding, 0,
1248b615593SMarko Zec     "Enable IP forwarding between interfaces");
1250312fbe9SPoul-Henning Kamp 
1268b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_SENDREDIRECTS,
1278b615593SMarko Zec     redirect, CTLFLAG_RW, ipsendredirects, 0,
1288b615593SMarko Zec     "Enable sending IP redirects");
1290312fbe9SPoul-Henning Kamp 
1308b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_DEFTTL,
1318b615593SMarko Zec     ttl, CTLFLAG_RW, ip_defttl, 0, "Maximum TTL on IP packets");
1320312fbe9SPoul-Henning Kamp 
1338b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_KEEPFAITH,
1348b615593SMarko Zec     keepfaith, CTLFLAG_RW, ip_keepfaith,	0,
1356a800098SYoshinobu Inoue     "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
1366a800098SYoshinobu Inoue 
1378b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO,
1388b615593SMarko Zec     sendsourcequench, CTLFLAG_RW, ip_sendsourcequench, 0,
139df285b3dSMike Silbersack     "Enable the transmission of source quench packets");
140df285b3dSMike Silbersack 
1418b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, random_id,
1428b615593SMarko Zec     CTLFLAG_RW, ip_do_randomid, 0, "Assign random ip_id values");
1431f44b0a1SDavid Malone 
144823db0e9SDon Lewis /*
145823db0e9SDon Lewis  * XXX - Setting ip_checkinterface mostly implements the receive side of
146823db0e9SDon Lewis  * the Strong ES model described in RFC 1122, but since the routing table
147a8f12100SDon Lewis  * and transmit implementation do not implement the Strong ES model,
148823db0e9SDon Lewis  * setting this to 1 results in an odd hybrid.
1493f67c834SDon Lewis  *
150a8f12100SDon Lewis  * XXX - ip_checkinterface currently must be disabled if you use ipnat
151a8f12100SDon Lewis  * to translate the destination address to another local interface.
1523f67c834SDon Lewis  *
1533f67c834SDon Lewis  * XXX - ip_checkinterface must be disabled if you add IP aliases
1543f67c834SDon Lewis  * to the loopback interface instead of the interface where the
1553f67c834SDon Lewis  * packets for those addresses are received.
156823db0e9SDon Lewis  */
1578b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO,
1588b615593SMarko Zec     check_interface, CTLFLAG_RW, ip_checkinterface, 0,
1598b615593SMarko Zec     "Verify packet arrives on correct interface");
160b3e95d4eSJonathan Lemon 
161c21fd232SAndre Oppermann struct pfil_head inet_pfil_hook;	/* Packet filter hooks */
162df8bae1dSRodney W. Grimes 
163d4b5cae4SRobert Watson static struct netisr_handler ip_nh = {
164d4b5cae4SRobert Watson 	.nh_name = "ip",
165d4b5cae4SRobert Watson 	.nh_handler = ip_input,
166d4b5cae4SRobert Watson 	.nh_proto = NETISR_IP,
167d4b5cae4SRobert Watson 	.nh_policy = NETISR_POLICY_FLOW,
168d4b5cae4SRobert Watson };
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 
174df8bae1dSRodney W. Grimes 
1758b615593SMarko Zec SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
1768b615593SMarko Zec     ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
177194a213eSAndrey A. Chernov 
178385195c0SMarko Zec #ifdef VIMAGE_GLOBALS
179d248c7d7SRobert Watson static uma_zone_t ipq_zone;
180385195c0SMarko Zec #endif
181dfa60d93SRobert Watson static struct mtx ipqlock;
1822fad1e93SSam Leffler 
1832fad1e93SSam Leffler #define	IPQ_LOCK()	mtx_lock(&ipqlock)
1842fad1e93SSam Leffler #define	IPQ_UNLOCK()	mtx_unlock(&ipqlock)
185888c2a3cSSam Leffler #define	IPQ_LOCK_INIT()	mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
186888c2a3cSSam Leffler #define	IPQ_LOCK_ASSERT()	mtx_assert(&ipqlock, MA_OWNED)
187f23b4c91SGarrett Wollman 
188d248c7d7SRobert Watson static void	maxnipq_update(void);
1894f590175SPaul Saab static void	ipq_zone_change(void *);
190d248c7d7SRobert Watson 
1918b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, fragpackets,
1928b615593SMarko Zec     CTLFLAG_RD, nipq, 0,
1938b615593SMarko Zec     "Current number of IPv4 fragment reassembly queue entries");
194d248c7d7SRobert Watson 
1958b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, maxfragsperpacket,
1968b615593SMarko Zec     CTLFLAG_RW, maxfragsperpacket, 0,
197d248c7d7SRobert Watson     "Maximum number of IPv4 fragments allowed per packet");
198d248c7d7SRobert Watson 
199d248c7d7SRobert Watson struct callout	ipport_tick_callout;
200d248c7d7SRobert Watson 
2010312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU
2020312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
2033d177f46SBill Fumerola     &ip_mtu, 0, "Default MTU");
2040312fbe9SPoul-Henning Kamp #endif
2050312fbe9SPoul-Henning Kamp 
2061b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
2078b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
2088b615593SMarko Zec     ipstealth, 0, "IP stealth mode, no TTL decrementation on forwarding");
2091b968362SDag-Erling Smørgrav #endif
21053be8fcaSBjoern A. Zeeb #ifdef FLOWTABLE
211fa057b15SMarko Zec #ifdef VIMAGE_GLOBALS
212fa057b15SMarko Zec static int ip_output_flowtable_size;
213fa057b15SMarko Zec struct flowtable *ip_ft;
214fa057b15SMarko Zec #endif
21565111ec7SKip Macy SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, output_flowtable_size,
21665111ec7SKip Macy     CTLFLAG_RDTUN, ip_output_flowtable_size, 2048,
21765111ec7SKip Macy     "number of entries in the per-cpu output flow caches");
21853be8fcaSBjoern A. Zeeb #endif
21953be8fcaSBjoern A. Zeeb 
220385195c0SMarko Zec #ifdef VIMAGE_GLOBALS
221385195c0SMarko Zec int fw_one_pass;
222385195c0SMarko Zec #endif
223010b65f5SJulian Elischer 
2244d77a549SAlfred Perlstein static void	ip_freef(struct ipqhead *, struct ipq *);
2258948e4baSArchie Cobbs 
226bfe1aba4SMarko Zec #ifndef VIMAGE_GLOBALS
227bfe1aba4SMarko Zec static void vnet_inet_register(void);
228bfe1aba4SMarko Zec 
229bfe1aba4SMarko Zec static const vnet_modinfo_t vnet_inet_modinfo = {
230bfe1aba4SMarko Zec 	.vmi_id		= VNET_MOD_INET,
231bfe1aba4SMarko Zec 	.vmi_name	= "inet",
232f6dfe47aSMarko Zec 	.vmi_size	= sizeof(struct vnet_inet)
233bfe1aba4SMarko Zec };
234bfe1aba4SMarko Zec 
235bfe1aba4SMarko Zec static void vnet_inet_register()
236bfe1aba4SMarko Zec {
237bfe1aba4SMarko Zec 
238bfe1aba4SMarko Zec 	vnet_mod_register(&vnet_inet_modinfo);
239bfe1aba4SMarko Zec }
240bfe1aba4SMarko Zec 
241bfe1aba4SMarko Zec SYSINIT(inet, SI_SUB_PROTO_BEGIN, SI_ORDER_FIRST, vnet_inet_register, 0);
242bfe1aba4SMarko Zec #endif
243bfe1aba4SMarko Zec 
244d4b5cae4SRobert Watson static int
245d4b5cae4SRobert Watson sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
246d4b5cae4SRobert Watson {
247d4b5cae4SRobert Watson 	int error, qlimit;
248d4b5cae4SRobert Watson 
249d4b5cae4SRobert Watson 	netisr_getqlimit(&ip_nh, &qlimit);
250d4b5cae4SRobert Watson 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
251d4b5cae4SRobert Watson 	if (error || !req->newptr)
252d4b5cae4SRobert Watson 		return (error);
253d4b5cae4SRobert Watson 	if (qlimit < 1)
254d4b5cae4SRobert Watson 		return (EINVAL);
255d4b5cae4SRobert Watson 	return (netisr_setqlimit(&ip_nh, qlimit));
256d4b5cae4SRobert Watson }
257d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen,
258d4b5cae4SRobert Watson     CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_queue_maxlen, "I",
259d4b5cae4SRobert Watson     "Maximum size of the IP input queue");
260d4b5cae4SRobert Watson 
261d4b5cae4SRobert Watson static int
262d4b5cae4SRobert Watson sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS)
263d4b5cae4SRobert Watson {
264d4b5cae4SRobert Watson 	u_int64_t qdrops_long;
265d4b5cae4SRobert Watson 	int error, qdrops;
266d4b5cae4SRobert Watson 
267d4b5cae4SRobert Watson 	netisr_getqdrops(&ip_nh, &qdrops_long);
268d4b5cae4SRobert Watson 	qdrops = qdrops_long;
269d4b5cae4SRobert Watson 	error = sysctl_handle_int(oidp, &qdrops, 0, req);
270d4b5cae4SRobert Watson 	if (error || !req->newptr)
271d4b5cae4SRobert Watson 		return (error);
272d4b5cae4SRobert Watson 	if (qdrops != 0)
273d4b5cae4SRobert Watson 		return (EINVAL);
274d4b5cae4SRobert Watson 	netisr_clearqdrops(&ip_nh);
275d4b5cae4SRobert Watson 	return (0);
276d4b5cae4SRobert Watson }
277d4b5cae4SRobert Watson 
278d4b5cae4SRobert Watson SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops,
279d4b5cae4SRobert Watson     CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_queue_drops, "I",
280d4b5cae4SRobert Watson     "Number of packets dropped from the IP input queue");
281d4b5cae4SRobert Watson 
282df8bae1dSRodney W. Grimes /*
283df8bae1dSRodney W. Grimes  * IP initialization: fill in IP protocol switch table.
284df8bae1dSRodney W. Grimes  * All protocols not implemented in kernel go to raw IP protocol handler.
285df8bae1dSRodney W. Grimes  */
286df8bae1dSRodney W. Grimes void
287f2565d68SRobert Watson ip_init(void)
288df8bae1dSRodney W. Grimes {
2898b615593SMarko Zec 	INIT_VNET_INET(curvnet);
290f2565d68SRobert Watson 	struct protosw *pr;
291f2565d68SRobert Watson 	int i;
292df8bae1dSRodney W. Grimes 
29344e33a07SMarko Zec 	V_ipsendredirects = 1; /* XXX */
29444e33a07SMarko Zec 	V_ip_checkinterface = 0;
29544e33a07SMarko Zec 	V_ip_keepfaith = 0;
29644e33a07SMarko Zec 	V_ip_sendsourcequench = 0;
29744e33a07SMarko Zec 	V_rsvp_on = 0;
29844e33a07SMarko Zec 	V_ip_defttl = IPDEFTTL;
29944e33a07SMarko Zec 	V_ip_do_randomid = 0;
3001ed81b73SMarko Zec 	V_ip_id = time_second & 0xffff;
30144e33a07SMarko Zec 	V_ipforwarding = 0;
30244e33a07SMarko Zec 	V_ipstealth = 0;
30344e33a07SMarko Zec 	V_nipq = 0;	/* Total # of reass queues */
30444e33a07SMarko Zec 
30544e33a07SMarko Zec 	V_ipport_lowfirstauto = IPPORT_RESERVED - 1;	/* 1023 */
30644e33a07SMarko Zec 	V_ipport_lowlastauto = IPPORT_RESERVEDSTART;	/* 600 */
30744e33a07SMarko Zec 	V_ipport_firstauto = IPPORT_EPHEMERALFIRST;	/* 10000 */
30844e33a07SMarko Zec 	V_ipport_lastauto = IPPORT_EPHEMERALLAST;	/* 65535 */
30944e33a07SMarko Zec 	V_ipport_hifirstauto = IPPORT_HIFIRSTAUTO;	/* 49152 */
31044e33a07SMarko Zec 	V_ipport_hilastauto = IPPORT_HILASTAUTO;	/* 65535 */
31144e33a07SMarko Zec 	V_ipport_reservedhigh = IPPORT_RESERVED - 1;	/* 1023 */
31244e33a07SMarko Zec 	V_ipport_reservedlow = 0;
31344e33a07SMarko Zec 	V_ipport_randomized = 1;	/* user controlled via sysctl */
31444e33a07SMarko Zec 	V_ipport_randomcps = 10;	/* user controlled via sysctl */
31544e33a07SMarko Zec 	V_ipport_randomtime = 45;	/* user controlled via sysctl */
31644e33a07SMarko Zec 	V_ipport_stoprandom = 0;	/* toggled by ipport_tick */
31744e33a07SMarko Zec 
318385195c0SMarko Zec 	V_fw_one_pass = 1;
319385195c0SMarko Zec 
32044e33a07SMarko Zec #ifdef NOTYET
32144e33a07SMarko Zec 	/* XXX global static but not instantiated in this file */
32244e33a07SMarko Zec 	V_ipfastforward_active = 0;
32344e33a07SMarko Zec 	V_subnetsarelocal = 0;
32444e33a07SMarko Zec 	V_sameprefixcarponly = 0;
32544e33a07SMarko Zec #endif
32644e33a07SMarko Zec 
327603724d3SBjoern A. Zeeb 	TAILQ_INIT(&V_in_ifaddrhead);
328603724d3SBjoern A. Zeeb 	V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
3292d9cfabaSRobert Watson 	IN_IFADDR_LOCK_INIT();
3301ed81b73SMarko Zec 
3311ed81b73SMarko Zec 	/* Initialize IP reassembly queue. */
3321ed81b73SMarko Zec 	for (i = 0; i < IPREASS_NHASH; i++)
3331ed81b73SMarko Zec 		TAILQ_INIT(&V_ipq[i]);
3341ed81b73SMarko Zec 	V_maxnipq = nmbclusters / 32;
3351ed81b73SMarko Zec 	V_maxfragsperpacket = 16;
3361ed81b73SMarko Zec 	V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
3371ed81b73SMarko Zec 	    NULL, UMA_ALIGN_PTR, 0);
3381ed81b73SMarko Zec 	maxnipq_update();
3391ed81b73SMarko Zec 
340fa057b15SMarko Zec #ifdef FLOWTABLE
341fa057b15SMarko Zec 	V_ip_output_flowtable_size = 2048;
342fa057b15SMarko Zec 	TUNABLE_INT_FETCH("net.inet.ip.output_flowtable_size",
343fa057b15SMarko Zec 	    &V_ip_output_flowtable_size);
344fa057b15SMarko Zec 	V_ip_ft = flowtable_alloc(V_ip_output_flowtable_size, FL_PCPU);
345fa057b15SMarko Zec #endif
346fa057b15SMarko Zec 
3471ed81b73SMarko Zec 	/* Skip initialization of globals for non-default instances. */
3481ed81b73SMarko Zec 	if (!IS_DEFAULT_VNET(curvnet))
3491ed81b73SMarko Zec 		return;
3501ed81b73SMarko Zec 
351f0ffb944SJulian Elischer 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
35202410549SRobert Watson 	if (pr == NULL)
353db09bef3SAndre Oppermann 		panic("ip_init: PF_INET not found");
354db09bef3SAndre Oppermann 
355db09bef3SAndre Oppermann 	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
356df8bae1dSRodney W. Grimes 	for (i = 0; i < IPPROTO_MAX; i++)
357df8bae1dSRodney W. Grimes 		ip_protox[i] = pr - inetsw;
358db09bef3SAndre Oppermann 	/*
359db09bef3SAndre Oppermann 	 * Cycle through IP protocols and put them into the appropriate place
360db09bef3SAndre Oppermann 	 * in ip_protox[].
361db09bef3SAndre Oppermann 	 */
362f0ffb944SJulian Elischer 	for (pr = inetdomain.dom_protosw;
363f0ffb944SJulian Elischer 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
364df8bae1dSRodney W. Grimes 		if (pr->pr_domain->dom_family == PF_INET &&
365db09bef3SAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
366db09bef3SAndre Oppermann 			/* Be careful to only index valid IP protocols. */
367db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX)
368df8bae1dSRodney W. Grimes 				ip_protox[pr->pr_protocol] = pr - inetsw;
369db09bef3SAndre Oppermann 		}
370194a213eSAndrey A. Chernov 
371c21fd232SAndre Oppermann 	/* Initialize packet filter hooks. */
372134ea224SSam Leffler 	inet_pfil_hook.ph_type = PFIL_TYPE_AF;
373134ea224SSam Leffler 	inet_pfil_hook.ph_af = AF_INET;
374134ea224SSam Leffler 	if ((i = pfil_head_register(&inet_pfil_hook)) != 0)
375134ea224SSam Leffler 		printf("%s: WARNING: unable to register pfil hook, "
376134ea224SSam Leffler 			"error %d\n", __func__, i);
377134ea224SSam Leffler 
3785f311da2SMike Silbersack 	/* Start ipport_tick. */
3795f311da2SMike Silbersack 	callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
38021ca7b57SMarko Zec 	callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
3815f311da2SMike Silbersack 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
3825f311da2SMike Silbersack 		SHUTDOWN_PRI_DEFAULT);
3834f590175SPaul Saab 	EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
3844f590175SPaul Saab 		NULL, EVENTHANDLER_PRI_ANY);
3855f311da2SMike Silbersack 
386db09bef3SAndre Oppermann 	/* Initialize various other remaining things. */
3871ed81b73SMarko Zec 	IPQ_LOCK_INIT();
388d4b5cae4SRobert Watson 	netisr_register(&ip_nh);
389df8bae1dSRodney W. Grimes }
390df8bae1dSRodney W. Grimes 
391f2565d68SRobert Watson void
392f2565d68SRobert Watson ip_fini(void *xtp)
3935f311da2SMike Silbersack {
394f2565d68SRobert Watson 
3955f311da2SMike Silbersack 	callout_stop(&ipport_tick_callout);
3965f311da2SMike Silbersack }
3975f311da2SMike Silbersack 
3984d2e3692SLuigi Rizzo /*
399df8bae1dSRodney W. Grimes  * Ip input routine.  Checksum and byte swap header.  If fragmented
400df8bae1dSRodney W. Grimes  * try to reassemble.  Process options.  Pass to next level.
401df8bae1dSRodney W. Grimes  */
402c67b1d17SGarrett Wollman void
403c67b1d17SGarrett Wollman ip_input(struct mbuf *m)
404df8bae1dSRodney W. Grimes {
4058b615593SMarko Zec 	INIT_VNET_INET(curvnet);
4069188b4a1SAndre Oppermann 	struct ip *ip = NULL;
4075da9f8faSJosef Karthauser 	struct in_ifaddr *ia = NULL;
408ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
4090aade26eSRobert Watson 	struct ifnet *ifp;
4109b932e9eSAndre Oppermann 	int    checkif, hlen = 0;
41147c861ecSBrian Somers 	u_short sum;
41202c1c707SAndre Oppermann 	int dchg = 0;				/* dest changed after fw */
413f51f805fSSam Leffler 	struct in_addr odst;			/* original dst address */
414b715f178SLuigi Rizzo 
415fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
416db40007dSAndrew R. Reiter 
417ac9d7e26SMax Laier 	if (m->m_flags & M_FASTFWD_OURS) {
4189b932e9eSAndre Oppermann 		/*
41976ff6dcfSAndre Oppermann 		 * Firewall or NAT changed destination to local.
42076ff6dcfSAndre Oppermann 		 * We expect ip_len and ip_off to be in host byte order.
4219b932e9eSAndre Oppermann 		 */
42276ff6dcfSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
42376ff6dcfSAndre Oppermann 		/* Set up some basics that will be used later. */
4242b25acc1SLuigi Rizzo 		ip = mtod(m, struct ip *);
42553be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
4269b932e9eSAndre Oppermann 		goto ours;
4272b25acc1SLuigi Rizzo 	}
4282b25acc1SLuigi Rizzo 
42986425c62SRobert Watson 	IPSTAT_INC(ips_total);
43058938916SGarrett Wollman 
43158938916SGarrett Wollman 	if (m->m_pkthdr.len < sizeof(struct ip))
43258938916SGarrett Wollman 		goto tooshort;
43358938916SGarrett Wollman 
434df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct ip) &&
4350b17fba7SAndre Oppermann 	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
43686425c62SRobert Watson 		IPSTAT_INC(ips_toosmall);
437c67b1d17SGarrett Wollman 		return;
438df8bae1dSRodney W. Grimes 	}
439df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
44058938916SGarrett Wollman 
44153be11f6SPoul-Henning Kamp 	if (ip->ip_v != IPVERSION) {
44286425c62SRobert Watson 		IPSTAT_INC(ips_badvers);
443df8bae1dSRodney W. Grimes 		goto bad;
444df8bae1dSRodney W. Grimes 	}
44558938916SGarrett Wollman 
44653be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
447df8bae1dSRodney W. Grimes 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
44886425c62SRobert Watson 		IPSTAT_INC(ips_badhlen);
449df8bae1dSRodney W. Grimes 		goto bad;
450df8bae1dSRodney W. Grimes 	}
451df8bae1dSRodney W. Grimes 	if (hlen > m->m_len) {
4520b17fba7SAndre Oppermann 		if ((m = m_pullup(m, hlen)) == NULL) {
45386425c62SRobert Watson 			IPSTAT_INC(ips_badhlen);
454c67b1d17SGarrett Wollman 			return;
455df8bae1dSRodney W. Grimes 		}
456df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
457df8bae1dSRodney W. Grimes 	}
45833841545SHajimu UMEMOTO 
45933841545SHajimu UMEMOTO 	/* 127/8 must not appear on wire - RFC1122 */
4600aade26eSRobert Watson 	ifp = m->m_pkthdr.rcvif;
46133841545SHajimu UMEMOTO 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
46233841545SHajimu UMEMOTO 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
4630aade26eSRobert Watson 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
46486425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
46533841545SHajimu UMEMOTO 			goto bad;
46633841545SHajimu UMEMOTO 		}
46733841545SHajimu UMEMOTO 	}
46833841545SHajimu UMEMOTO 
469db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
470db4f9cc7SJonathan Lemon 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
471db4f9cc7SJonathan Lemon 	} else {
47258938916SGarrett Wollman 		if (hlen == sizeof(struct ip)) {
47347c861ecSBrian Somers 			sum = in_cksum_hdr(ip);
47458938916SGarrett Wollman 		} else {
47547c861ecSBrian Somers 			sum = in_cksum(m, hlen);
47658938916SGarrett Wollman 		}
477db4f9cc7SJonathan Lemon 	}
47847c861ecSBrian Somers 	if (sum) {
47986425c62SRobert Watson 		IPSTAT_INC(ips_badsum);
480df8bae1dSRodney W. Grimes 		goto bad;
481df8bae1dSRodney W. Grimes 	}
482df8bae1dSRodney W. Grimes 
48302b199f1SMax Laier #ifdef ALTQ
48402b199f1SMax Laier 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
48502b199f1SMax Laier 		/* packet is dropped by traffic conditioner */
48602b199f1SMax Laier 		return;
48702b199f1SMax Laier #endif
48802b199f1SMax Laier 
489df8bae1dSRodney W. Grimes 	/*
490df8bae1dSRodney W. Grimes 	 * Convert fields to host representation.
491df8bae1dSRodney W. Grimes 	 */
492fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
493df8bae1dSRodney W. Grimes 	if (ip->ip_len < hlen) {
49486425c62SRobert Watson 		IPSTAT_INC(ips_badlen);
495df8bae1dSRodney W. Grimes 		goto bad;
496df8bae1dSRodney W. Grimes 	}
497fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
498df8bae1dSRodney W. Grimes 
499df8bae1dSRodney W. Grimes 	/*
500df8bae1dSRodney W. Grimes 	 * Check that the amount of data in the buffers
501df8bae1dSRodney W. Grimes 	 * is as at least much as the IP header would have us expect.
502df8bae1dSRodney W. Grimes 	 * Trim mbufs if longer than we expect.
503df8bae1dSRodney W. Grimes 	 * Drop packet if shorter than we expect.
504df8bae1dSRodney W. Grimes 	 */
505df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len < ip->ip_len) {
50658938916SGarrett Wollman tooshort:
50786425c62SRobert Watson 		IPSTAT_INC(ips_tooshort);
508df8bae1dSRodney W. Grimes 		goto bad;
509df8bae1dSRodney W. Grimes 	}
510df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len > ip->ip_len) {
511df8bae1dSRodney W. Grimes 		if (m->m_len == m->m_pkthdr.len) {
512df8bae1dSRodney W. Grimes 			m->m_len = ip->ip_len;
513df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = ip->ip_len;
514df8bae1dSRodney W. Grimes 		} else
515df8bae1dSRodney W. Grimes 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
516df8bae1dSRodney W. Grimes 	}
517b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
51814dd6717SSam Leffler 	/*
51914dd6717SSam Leffler 	 * Bypass packet filtering for packets from a tunnel (gif).
52014dd6717SSam Leffler 	 */
521cc977adcSBjoern A. Zeeb 	if (ip_ipsec_filtertunnel(m))
522c21fd232SAndre Oppermann 		goto passin;
523b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
5243f67c834SDon Lewis 
525c4ac87eaSDarren Reed 	/*
526134ea224SSam Leffler 	 * Run through list of hooks for input packets.
527f51f805fSSam Leffler 	 *
528f51f805fSSam Leffler 	 * NB: Beware of the destination address changing (e.g.
529f51f805fSSam Leffler 	 *     by NAT rewriting).  When this happens, tell
530f51f805fSSam Leffler 	 *     ip_forward to do the right thing.
531c4ac87eaSDarren Reed 	 */
532c21fd232SAndre Oppermann 
533c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
534604afec4SChristian S.J. Peron 	if (!PFIL_HOOKED(&inet_pfil_hook))
535c21fd232SAndre Oppermann 		goto passin;
536c21fd232SAndre Oppermann 
537f51f805fSSam Leffler 	odst = ip->ip_dst;
5380aade26eSRobert Watson 	if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0)
539beec8214SDarren Reed 		return;
540134ea224SSam Leffler 	if (m == NULL)			/* consumed by filter */
541c4ac87eaSDarren Reed 		return;
5429b932e9eSAndre Oppermann 
543c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
54402c1c707SAndre Oppermann 	dchg = (odst.s_addr != ip->ip_dst.s_addr);
5450aade26eSRobert Watson 	ifp = m->m_pkthdr.rcvif;
5469b932e9eSAndre Oppermann 
5479b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
5489b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
5499b932e9eSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
5509b932e9eSAndre Oppermann 		goto ours;
5519b932e9eSAndre Oppermann 	}
552099dd043SAndre Oppermann 	if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) {
553099dd043SAndre Oppermann 		/*
554099dd043SAndre Oppermann 		 * Directly ship on the packet.  This allows to forward packets
555099dd043SAndre Oppermann 		 * that were destined for us to some other directly connected
556099dd043SAndre Oppermann 		 * host.
557099dd043SAndre Oppermann 		 */
558099dd043SAndre Oppermann 		ip_forward(m, dchg);
559099dd043SAndre Oppermann 		return;
560099dd043SAndre Oppermann 	}
5619b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
5629b932e9eSAndre Oppermann 
563c21fd232SAndre Oppermann passin:
564df8bae1dSRodney W. Grimes 	/*
565df8bae1dSRodney W. Grimes 	 * Process options and, if not destined for us,
566df8bae1dSRodney W. Grimes 	 * ship it on.  ip_dooptions returns 1 when an
567df8bae1dSRodney W. Grimes 	 * error was detected (causing an icmp message
568df8bae1dSRodney W. Grimes 	 * to be sent and the original packet to be freed).
569df8bae1dSRodney W. Grimes 	 */
5709b932e9eSAndre Oppermann 	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
571c67b1d17SGarrett Wollman 		return;
572df8bae1dSRodney W. Grimes 
573f0068c4aSGarrett Wollman         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
574f0068c4aSGarrett Wollman          * matter if it is destined to another node, or whether it is
575f0068c4aSGarrett Wollman          * a multicast one, RSVP wants it! and prevents it from being forwarded
576f0068c4aSGarrett Wollman          * anywhere else. Also checks if the rsvp daemon is running before
577f0068c4aSGarrett Wollman 	 * grabbing the packet.
578f0068c4aSGarrett Wollman          */
579603724d3SBjoern A. Zeeb 	if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP)
580f0068c4aSGarrett Wollman 		goto ours;
581f0068c4aSGarrett Wollman 
582df8bae1dSRodney W. Grimes 	/*
583df8bae1dSRodney W. Grimes 	 * Check our list of addresses, to see if the packet is for us.
584cc766e04SGarrett Wollman 	 * If we don't have any addresses, assume any unicast packet
585cc766e04SGarrett Wollman 	 * we receive might be for us (and let the upper layers deal
586cc766e04SGarrett Wollman 	 * with it).
587df8bae1dSRodney W. Grimes 	 */
588603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
589cc766e04SGarrett Wollman 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
590cc766e04SGarrett Wollman 		goto ours;
591cc766e04SGarrett Wollman 
5927538a9a0SJonathan Lemon 	/*
593823db0e9SDon Lewis 	 * Enable a consistency check between the destination address
594823db0e9SDon Lewis 	 * and the arrival interface for a unicast packet (the RFC 1122
595823db0e9SDon Lewis 	 * strong ES model) if IP forwarding is disabled and the packet
596e15ae1b2SDon Lewis 	 * is not locally generated and the packet is not subject to
597e15ae1b2SDon Lewis 	 * 'ipfw fwd'.
5983f67c834SDon Lewis 	 *
5993f67c834SDon Lewis 	 * XXX - Checking also should be disabled if the destination
6003f67c834SDon Lewis 	 * address is ipnat'ed to a different interface.
6013f67c834SDon Lewis 	 *
602a8f12100SDon Lewis 	 * XXX - Checking is incompatible with IP aliases added
6033f67c834SDon Lewis 	 * to the loopback interface instead of the interface where
6043f67c834SDon Lewis 	 * the packets are received.
605a9771948SGleb Smirnoff 	 *
606a9771948SGleb Smirnoff 	 * XXX - This is the case for carp vhost IPs as well so we
607a9771948SGleb Smirnoff 	 * insert a workaround. If the packet got here, we already
608a9771948SGleb Smirnoff 	 * checked with carp_iamatch() and carp_forus().
609823db0e9SDon Lewis 	 */
610603724d3SBjoern A. Zeeb 	checkif = V_ip_checkinterface && (V_ipforwarding == 0) &&
6110aade26eSRobert Watson 	    ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) &&
612a9771948SGleb Smirnoff #ifdef DEV_CARP
6130aade26eSRobert Watson 	    !ifp->if_carp &&
614a9771948SGleb Smirnoff #endif
6159b932e9eSAndre Oppermann 	    (dchg == 0);
616823db0e9SDon Lewis 
617ca925d9cSJonathan Lemon 	/*
618ca925d9cSJonathan Lemon 	 * Check for exact addresses in the hash bucket.
619ca925d9cSJonathan Lemon 	 */
6202d9cfabaSRobert Watson 	/* IN_IFADDR_RLOCK(); */
6219b932e9eSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
622f9e354dfSJulian Elischer 		/*
623823db0e9SDon Lewis 		 * If the address matches, verify that the packet
624823db0e9SDon Lewis 		 * arrived via the correct interface if checking is
625823db0e9SDon Lewis 		 * enabled.
626f9e354dfSJulian Elischer 		 */
6279b932e9eSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
6288c0fec80SRobert Watson 		    (!checkif || ia->ia_ifp == ifp)) {
6298c0fec80SRobert Watson 			ifa_ref(&ia->ia_ifa);
6302d9cfabaSRobert Watson 			/* IN_IFADDR_RUNLOCK(); */
631ed1ff184SJulian Elischer 			goto ours;
632ca925d9cSJonathan Lemon 		}
6338c0fec80SRobert Watson 	}
6342d9cfabaSRobert Watson 	/* IN_IFADDR_RUNLOCK(); */
6352d9cfabaSRobert Watson 
636823db0e9SDon Lewis 	/*
637ca925d9cSJonathan Lemon 	 * Check for broadcast addresses.
638ca925d9cSJonathan Lemon 	 *
639ca925d9cSJonathan Lemon 	 * Only accept broadcast packets that arrive via the matching
640ca925d9cSJonathan Lemon 	 * interface.  Reception of forwarded directed broadcasts would
641ca925d9cSJonathan Lemon 	 * be handled via ip_forward() and ether_output() with the loopback
642ca925d9cSJonathan Lemon 	 * into the stack for SIMPLEX interfaces handled by ether_output().
643823db0e9SDon Lewis 	 */
6440aade26eSRobert Watson 	if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
6450aade26eSRobert Watson 		IF_ADDR_LOCK(ifp);
6460aade26eSRobert Watson 	        TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
647ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
648ca925d9cSJonathan Lemon 				continue;
649ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
650df8bae1dSRodney W. Grimes 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
6510aade26eSRobert Watson 			    ip->ip_dst.s_addr) {
6528c0fec80SRobert Watson 				ifa_ref(ifa);
6530aade26eSRobert Watson 				IF_ADDR_UNLOCK(ifp);
654df8bae1dSRodney W. Grimes 				goto ours;
6550aade26eSRobert Watson 			}
6560aade26eSRobert Watson 			if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr) {
6578c0fec80SRobert Watson 				ifa_ref(ifa);
6580aade26eSRobert Watson 				IF_ADDR_UNLOCK(ifp);
659df8bae1dSRodney W. Grimes 				goto ours;
6600aade26eSRobert Watson 			}
6610ac40133SBrian Somers #ifdef BOOTP_COMPAT
6620aade26eSRobert Watson 			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) {
6638c0fec80SRobert Watson 				ifa_ref(ifa);
6640aade26eSRobert Watson 				IF_ADDR_UNLOCK(ifp);
665ca925d9cSJonathan Lemon 				goto ours;
6660aade26eSRobert Watson 			}
6670ac40133SBrian Somers #endif
668df8bae1dSRodney W. Grimes 		}
6690aade26eSRobert Watson 		IF_ADDR_UNLOCK(ifp);
67019e5b0a7SRobert Watson 		ia = NULL;
671df8bae1dSRodney W. Grimes 	}
672f8429ca2SBruce M Simpson 	/* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
673f8429ca2SBruce M Simpson 	if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
67486425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
675f8429ca2SBruce M Simpson 		m_freem(m);
676f8429ca2SBruce M Simpson 		return;
677f8429ca2SBruce M Simpson 	}
678df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
679603724d3SBjoern A. Zeeb 		if (V_ip_mrouter) {
680df8bae1dSRodney W. Grimes 			/*
681df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, all
682df8bae1dSRodney W. Grimes 			 * incoming multicast packets are passed to the
683df8bae1dSRodney W. Grimes 			 * kernel-level multicast forwarding function.
684df8bae1dSRodney W. Grimes 			 * The packet is returned (relatively) intact; if
685df8bae1dSRodney W. Grimes 			 * ip_mforward() returns a non-zero value, the packet
686df8bae1dSRodney W. Grimes 			 * must be discarded, else it may be accepted below.
687df8bae1dSRodney W. Grimes 			 */
6880aade26eSRobert Watson 			if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
68986425c62SRobert Watson 				IPSTAT_INC(ips_cantforward);
690df8bae1dSRodney W. Grimes 				m_freem(m);
691c67b1d17SGarrett Wollman 				return;
692df8bae1dSRodney W. Grimes 			}
693df8bae1dSRodney W. Grimes 
694df8bae1dSRodney W. Grimes 			/*
69511612afaSDima Dorfman 			 * The process-level routing daemon needs to receive
696df8bae1dSRodney W. Grimes 			 * all multicast IGMP packets, whether or not this
697df8bae1dSRodney W. Grimes 			 * host belongs to their destination groups.
698df8bae1dSRodney W. Grimes 			 */
699df8bae1dSRodney W. Grimes 			if (ip->ip_p == IPPROTO_IGMP)
700df8bae1dSRodney W. Grimes 				goto ours;
70186425c62SRobert Watson 			IPSTAT_INC(ips_forward);
702df8bae1dSRodney W. Grimes 		}
703df8bae1dSRodney W. Grimes 		/*
704d10910e6SBruce M Simpson 		 * Assume the packet is for us, to avoid prematurely taking
705d10910e6SBruce M Simpson 		 * a lock on the in_multi hash. Protocols must perform
706d10910e6SBruce M Simpson 		 * their own filtering and update statistics accordingly.
707df8bae1dSRodney W. Grimes 		 */
708df8bae1dSRodney W. Grimes 		goto ours;
709df8bae1dSRodney W. Grimes 	}
710df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
711df8bae1dSRodney W. Grimes 		goto ours;
712df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == INADDR_ANY)
713df8bae1dSRodney W. Grimes 		goto ours;
714df8bae1dSRodney W. Grimes 
7156a800098SYoshinobu Inoue 	/*
7166a800098SYoshinobu Inoue 	 * FAITH(Firewall Aided Internet Translator)
7176a800098SYoshinobu Inoue 	 */
7180aade26eSRobert Watson 	if (ifp && ifp->if_type == IFT_FAITH) {
719603724d3SBjoern A. Zeeb 		if (V_ip_keepfaith) {
7206a800098SYoshinobu Inoue 			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
7216a800098SYoshinobu Inoue 				goto ours;
7226a800098SYoshinobu Inoue 		}
7236a800098SYoshinobu Inoue 		m_freem(m);
7246a800098SYoshinobu Inoue 		return;
7256a800098SYoshinobu Inoue 	}
7269494d596SBrooks Davis 
727df8bae1dSRodney W. Grimes 	/*
728df8bae1dSRodney W. Grimes 	 * Not for us; forward if possible and desirable.
729df8bae1dSRodney W. Grimes 	 */
730603724d3SBjoern A. Zeeb 	if (V_ipforwarding == 0) {
73186425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
732df8bae1dSRodney W. Grimes 		m_freem(m);
733546f251bSChris D. Faulhaber 	} else {
734b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
7351dfcf0d2SAndre Oppermann 		if (ip_ipsec_fwd(m))
736546f251bSChris D. Faulhaber 			goto bad;
737b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
7389b932e9eSAndre Oppermann 		ip_forward(m, dchg);
739546f251bSChris D. Faulhaber 	}
740c67b1d17SGarrett Wollman 	return;
741df8bae1dSRodney W. Grimes 
742df8bae1dSRodney W. Grimes ours:
743d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH
744d0ebc0d2SYaroslav Tykhiy 	/*
745d0ebc0d2SYaroslav Tykhiy 	 * IPSTEALTH: Process non-routing options only
746d0ebc0d2SYaroslav Tykhiy 	 * if the packet is destined for us.
747d0ebc0d2SYaroslav Tykhiy 	 */
74819e5b0a7SRobert Watson 	if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1)) {
74919e5b0a7SRobert Watson 		if (ia != NULL)
75019e5b0a7SRobert Watson 			ifa_free(&ia->ia_ifa);
751d0ebc0d2SYaroslav Tykhiy 		return;
75219e5b0a7SRobert Watson 	}
753d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */
754d0ebc0d2SYaroslav Tykhiy 
7555da9f8faSJosef Karthauser 	/* Count the packet in the ip address stats */
7565da9f8faSJosef Karthauser 	if (ia != NULL) {
7575da9f8faSJosef Karthauser 		ia->ia_ifa.if_ipackets++;
7585da9f8faSJosef Karthauser 		ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
7598c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
7605da9f8faSJosef Karthauser 	}
761100ba1a6SJordan K. Hubbard 
76263f8d699SJordan K. Hubbard 	/*
763b6ea1aa5SRuslan Ermilov 	 * Attempt reassembly; if it succeeds, proceed.
764ac9d7e26SMax Laier 	 * ip_reass() will return a different mbuf.
765df8bae1dSRodney W. Grimes 	 */
766f0cada84SAndre Oppermann 	if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
767f0cada84SAndre Oppermann 		m = ip_reass(m);
768f0cada84SAndre Oppermann 		if (m == NULL)
769c67b1d17SGarrett Wollman 			return;
7706a800098SYoshinobu Inoue 		ip = mtod(m, struct ip *);
7717e2df452SRuslan Ermilov 		/* Get the header length of the reassembled packet */
77253be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
773f0cada84SAndre Oppermann 	}
774f0cada84SAndre Oppermann 
775f0cada84SAndre Oppermann 	/*
776f0cada84SAndre Oppermann 	 * Further protocols expect the packet length to be w/o the
777f0cada84SAndre Oppermann 	 * IP header.
778f0cada84SAndre Oppermann 	 */
779df8bae1dSRodney W. Grimes 	ip->ip_len -= hlen;
780df8bae1dSRodney W. Grimes 
781b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
78233841545SHajimu UMEMOTO 	/*
78333841545SHajimu UMEMOTO 	 * enforce IPsec policy checking if we are seeing last header.
78433841545SHajimu UMEMOTO 	 * note that we do not visit this with protocols with pcb layer
78533841545SHajimu UMEMOTO 	 * code - like udp/tcp/raw ip.
78633841545SHajimu UMEMOTO 	 */
7871dfcf0d2SAndre Oppermann 	if (ip_ipsec_input(m))
78833841545SHajimu UMEMOTO 		goto bad;
789b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
79033841545SHajimu UMEMOTO 
791df8bae1dSRodney W. Grimes 	/*
792df8bae1dSRodney W. Grimes 	 * Switch out to protocol's input routine.
793df8bae1dSRodney W. Grimes 	 */
79486425c62SRobert Watson 	IPSTAT_INC(ips_delivered);
7959b932e9eSAndre Oppermann 
7962b25acc1SLuigi Rizzo 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
797c67b1d17SGarrett Wollman 	return;
798df8bae1dSRodney W. Grimes bad:
799df8bae1dSRodney W. Grimes 	m_freem(m);
800c67b1d17SGarrett Wollman }
801c67b1d17SGarrett Wollman 
802c67b1d17SGarrett Wollman /*
803d248c7d7SRobert Watson  * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
804d248c7d7SRobert Watson  * max has slightly different semantics than the sysctl, for historical
805d248c7d7SRobert Watson  * reasons.
806d248c7d7SRobert Watson  */
807d248c7d7SRobert Watson static void
808d248c7d7SRobert Watson maxnipq_update(void)
809d248c7d7SRobert Watson {
8108b615593SMarko Zec 	INIT_VNET_INET(curvnet);
811d248c7d7SRobert Watson 
812d248c7d7SRobert Watson 	/*
813d248c7d7SRobert Watson 	 * -1 for unlimited allocation.
814d248c7d7SRobert Watson 	 */
815603724d3SBjoern A. Zeeb 	if (V_maxnipq < 0)
816603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 0);
817d248c7d7SRobert Watson 	/*
818d248c7d7SRobert Watson 	 * Positive number for specific bound.
819d248c7d7SRobert Watson 	 */
820603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0)
821603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, V_maxnipq);
822d248c7d7SRobert Watson 	/*
823d248c7d7SRobert Watson 	 * Zero specifies no further fragment queue allocation -- set the
824d248c7d7SRobert Watson 	 * bound very low, but rely on implementation elsewhere to actually
825d248c7d7SRobert Watson 	 * prevent allocation and reclaim current queues.
826d248c7d7SRobert Watson 	 */
827603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0)
828603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 1);
829d248c7d7SRobert Watson }
830d248c7d7SRobert Watson 
8314f590175SPaul Saab static void
8324f590175SPaul Saab ipq_zone_change(void *tag)
8334f590175SPaul Saab {
8348b615593SMarko Zec 	INIT_VNET_INET(curvnet);
8354f590175SPaul Saab 
836603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) {
837603724d3SBjoern A. Zeeb 		V_maxnipq = nmbclusters / 32;
8384f590175SPaul Saab 		maxnipq_update();
8394f590175SPaul Saab 	}
8404f590175SPaul Saab }
8414f590175SPaul Saab 
842d248c7d7SRobert Watson static int
843d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
844d248c7d7SRobert Watson {
8458b615593SMarko Zec 	INIT_VNET_INET(curvnet);
846d248c7d7SRobert Watson 	int error, i;
847d248c7d7SRobert Watson 
848603724d3SBjoern A. Zeeb 	i = V_maxnipq;
849d248c7d7SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
850d248c7d7SRobert Watson 	if (error || !req->newptr)
851d248c7d7SRobert Watson 		return (error);
852d248c7d7SRobert Watson 
853d248c7d7SRobert Watson 	/*
854d248c7d7SRobert Watson 	 * XXXRW: Might be a good idea to sanity check the argument and place
855d248c7d7SRobert Watson 	 * an extreme upper bound.
856d248c7d7SRobert Watson 	 */
857d248c7d7SRobert Watson 	if (i < -1)
858d248c7d7SRobert Watson 		return (EINVAL);
859603724d3SBjoern A. Zeeb 	V_maxnipq = i;
860d248c7d7SRobert Watson 	maxnipq_update();
861d248c7d7SRobert Watson 	return (0);
862d248c7d7SRobert Watson }
863d248c7d7SRobert Watson 
864d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
865d248c7d7SRobert Watson     NULL, 0, sysctl_maxnipq, "I",
866d248c7d7SRobert Watson     "Maximum number of IPv4 fragment reassembly queue entries");
867d248c7d7SRobert Watson 
868d248c7d7SRobert Watson /*
8698948e4baSArchie Cobbs  * Take incoming datagram fragment and try to reassemble it into
870f0cada84SAndre Oppermann  * whole datagram.  If the argument is the first fragment or one
871f0cada84SAndre Oppermann  * in between the function will return NULL and store the mbuf
872f0cada84SAndre Oppermann  * in the fragment chain.  If the argument is the last fragment
873f0cada84SAndre Oppermann  * the packet will be reassembled and the pointer to the new
874f0cada84SAndre Oppermann  * mbuf returned for further processing.  Only m_tags attached
875f0cada84SAndre Oppermann  * to the first packet/fragment are preserved.
876f0cada84SAndre Oppermann  * The IP header is *NOT* adjusted out of iplen.
877df8bae1dSRodney W. Grimes  */
878f0cada84SAndre Oppermann struct mbuf *
879f0cada84SAndre Oppermann ip_reass(struct mbuf *m)
880df8bae1dSRodney W. Grimes {
8818b615593SMarko Zec 	INIT_VNET_INET(curvnet);
882f0cada84SAndre Oppermann 	struct ip *ip;
883f0cada84SAndre Oppermann 	struct mbuf *p, *q, *nq, *t;
884f0cada84SAndre Oppermann 	struct ipq *fp = NULL;
885f0cada84SAndre Oppermann 	struct ipqhead *head;
886f0cada84SAndre Oppermann 	int i, hlen, next;
88759dfcba4SHajimu UMEMOTO 	u_int8_t ecn, ecn0;
888f0cada84SAndre Oppermann 	u_short hash;
889df8bae1dSRodney W. Grimes 
890800af1fbSMaxim Konovalov 	/* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
891603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0 || V_maxfragsperpacket == 0) {
89286425c62SRobert Watson 		IPSTAT_INC(ips_fragments);
89386425c62SRobert Watson 		IPSTAT_INC(ips_fragdropped);
8949d804f81SAndre Oppermann 		m_freem(m);
8959d804f81SAndre Oppermann 		return (NULL);
896f0cada84SAndre Oppermann 	}
8972fad1e93SSam Leffler 
898f0cada84SAndre Oppermann 	ip = mtod(m, struct ip *);
899f0cada84SAndre Oppermann 	hlen = ip->ip_hl << 2;
900f0cada84SAndre Oppermann 
901f0cada84SAndre Oppermann 	hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
902603724d3SBjoern A. Zeeb 	head = &V_ipq[hash];
903f0cada84SAndre Oppermann 	IPQ_LOCK();
904f0cada84SAndre Oppermann 
905f0cada84SAndre Oppermann 	/*
906f0cada84SAndre Oppermann 	 * Look for queue of fragments
907f0cada84SAndre Oppermann 	 * of this datagram.
908f0cada84SAndre Oppermann 	 */
909f0cada84SAndre Oppermann 	TAILQ_FOREACH(fp, head, ipq_list)
910f0cada84SAndre Oppermann 		if (ip->ip_id == fp->ipq_id &&
911f0cada84SAndre Oppermann 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
912f0cada84SAndre Oppermann 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
913f0cada84SAndre Oppermann #ifdef MAC
91430d239bcSRobert Watson 		    mac_ipq_match(m, fp) &&
915f0cada84SAndre Oppermann #endif
916f0cada84SAndre Oppermann 		    ip->ip_p == fp->ipq_p)
917f0cada84SAndre Oppermann 			goto found;
918f0cada84SAndre Oppermann 
919f0cada84SAndre Oppermann 	fp = NULL;
920f0cada84SAndre Oppermann 
921f0cada84SAndre Oppermann 	/*
922d248c7d7SRobert Watson 	 * Attempt to trim the number of allocated fragment queues if it
923d248c7d7SRobert Watson 	 * exceeds the administrative limit.
924f0cada84SAndre Oppermann 	 */
925603724d3SBjoern A. Zeeb 	if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) {
926f0cada84SAndre Oppermann 		/*
927f0cada84SAndre Oppermann 		 * drop something from the tail of the current queue
928f0cada84SAndre Oppermann 		 * before proceeding further
929f0cada84SAndre Oppermann 		 */
930f0cada84SAndre Oppermann 		struct ipq *q = TAILQ_LAST(head, ipqhead);
931f0cada84SAndre Oppermann 		if (q == NULL) {   /* gak */
932f0cada84SAndre Oppermann 			for (i = 0; i < IPREASS_NHASH; i++) {
933603724d3SBjoern A. Zeeb 				struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead);
934f0cada84SAndre Oppermann 				if (r) {
93586425c62SRobert Watson 					IPSTAT_ADD(ips_fragtimeout,
93686425c62SRobert Watson 					    r->ipq_nfrags);
937603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], r);
938f0cada84SAndre Oppermann 					break;
939f0cada84SAndre Oppermann 				}
940f0cada84SAndre Oppermann 			}
941f0cada84SAndre Oppermann 		} else {
94286425c62SRobert Watson 			IPSTAT_ADD(ips_fragtimeout, q->ipq_nfrags);
943f0cada84SAndre Oppermann 			ip_freef(head, q);
944f0cada84SAndre Oppermann 		}
945f0cada84SAndre Oppermann 	}
946f0cada84SAndre Oppermann 
947f0cada84SAndre Oppermann found:
948f0cada84SAndre Oppermann 	/*
949f0cada84SAndre Oppermann 	 * Adjust ip_len to not reflect header,
950f0cada84SAndre Oppermann 	 * convert offset of this to bytes.
951f0cada84SAndre Oppermann 	 */
952f0cada84SAndre Oppermann 	ip->ip_len -= hlen;
953f0cada84SAndre Oppermann 	if (ip->ip_off & IP_MF) {
954f0cada84SAndre Oppermann 		/*
955f0cada84SAndre Oppermann 		 * Make sure that fragments have a data length
956f0cada84SAndre Oppermann 		 * that's a non-zero multiple of 8 bytes.
957f0cada84SAndre Oppermann 		 */
958f0cada84SAndre Oppermann 		if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
95986425c62SRobert Watson 			IPSTAT_INC(ips_toosmall); /* XXX */
960f0cada84SAndre Oppermann 			goto dropfrag;
961f0cada84SAndre Oppermann 		}
962f0cada84SAndre Oppermann 		m->m_flags |= M_FRAG;
963f0cada84SAndre Oppermann 	} else
964f0cada84SAndre Oppermann 		m->m_flags &= ~M_FRAG;
965f0cada84SAndre Oppermann 	ip->ip_off <<= 3;
966f0cada84SAndre Oppermann 
967f0cada84SAndre Oppermann 
968f0cada84SAndre Oppermann 	/*
969f0cada84SAndre Oppermann 	 * Attempt reassembly; if it succeeds, proceed.
970f0cada84SAndre Oppermann 	 * ip_reass() will return a different mbuf.
971f0cada84SAndre Oppermann 	 */
97286425c62SRobert Watson 	IPSTAT_INC(ips_fragments);
973f0cada84SAndre Oppermann 	m->m_pkthdr.header = ip;
974f0cada84SAndre Oppermann 
975f0cada84SAndre Oppermann 	/* Previous ip_reass() started here. */
976df8bae1dSRodney W. Grimes 	/*
977df8bae1dSRodney W. Grimes 	 * Presence of header sizes in mbufs
978df8bae1dSRodney W. Grimes 	 * would confuse code below.
979df8bae1dSRodney W. Grimes 	 */
980df8bae1dSRodney W. Grimes 	m->m_data += hlen;
981df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
982df8bae1dSRodney W. Grimes 
983df8bae1dSRodney W. Grimes 	/*
984df8bae1dSRodney W. Grimes 	 * If first fragment to arrive, create a reassembly queue.
985df8bae1dSRodney W. Grimes 	 */
986042bbfa3SRobert Watson 	if (fp == NULL) {
987603724d3SBjoern A. Zeeb 		fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
988d248c7d7SRobert Watson 		if (fp == NULL)
989df8bae1dSRodney W. Grimes 			goto dropfrag;
99036b0360bSRobert Watson #ifdef MAC
99130d239bcSRobert Watson 		if (mac_ipq_init(fp, M_NOWAIT) != 0) {
992603724d3SBjoern A. Zeeb 			uma_zfree(V_ipq_zone, fp);
9931d7d0bfeSPawel Jakub Dawidek 			fp = NULL;
9945e7ce478SRobert Watson 			goto dropfrag;
9955e7ce478SRobert Watson 		}
99630d239bcSRobert Watson 		mac_ipq_create(m, fp);
99736b0360bSRobert Watson #endif
998462b86feSPoul-Henning Kamp 		TAILQ_INSERT_HEAD(head, fp, ipq_list);
999603724d3SBjoern A. Zeeb 		V_nipq++;
1000375386e2SMike Silbersack 		fp->ipq_nfrags = 1;
1001df8bae1dSRodney W. Grimes 		fp->ipq_ttl = IPFRAGTTL;
1002df8bae1dSRodney W. Grimes 		fp->ipq_p = ip->ip_p;
1003df8bae1dSRodney W. Grimes 		fp->ipq_id = ip->ip_id;
10046effc713SDoug Rabson 		fp->ipq_src = ip->ip_src;
10056effc713SDoug Rabson 		fp->ipq_dst = ip->ip_dst;
1006af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
1007af38c68cSLuigi Rizzo 		m->m_nextpkt = NULL;
1008800af1fbSMaxim Konovalov 		goto done;
100936b0360bSRobert Watson 	} else {
1010375386e2SMike Silbersack 		fp->ipq_nfrags++;
101136b0360bSRobert Watson #ifdef MAC
101230d239bcSRobert Watson 		mac_ipq_update(m, fp);
101336b0360bSRobert Watson #endif
1014df8bae1dSRodney W. Grimes 	}
1015df8bae1dSRodney W. Grimes 
10166effc713SDoug Rabson #define GETIP(m)	((struct ip*)((m)->m_pkthdr.header))
10176effc713SDoug Rabson 
1018df8bae1dSRodney W. Grimes 	/*
101959dfcba4SHajimu UMEMOTO 	 * Handle ECN by comparing this segment with the first one;
102059dfcba4SHajimu UMEMOTO 	 * if CE is set, do not lose CE.
102159dfcba4SHajimu UMEMOTO 	 * drop if CE and not-ECT are mixed for the same packet.
102259dfcba4SHajimu UMEMOTO 	 */
102359dfcba4SHajimu UMEMOTO 	ecn = ip->ip_tos & IPTOS_ECN_MASK;
102459dfcba4SHajimu UMEMOTO 	ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
102559dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_CE) {
102659dfcba4SHajimu UMEMOTO 		if (ecn0 == IPTOS_ECN_NOTECT)
102759dfcba4SHajimu UMEMOTO 			goto dropfrag;
102859dfcba4SHajimu UMEMOTO 		if (ecn0 != IPTOS_ECN_CE)
102959dfcba4SHajimu UMEMOTO 			GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
103059dfcba4SHajimu UMEMOTO 	}
103159dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
103259dfcba4SHajimu UMEMOTO 		goto dropfrag;
103359dfcba4SHajimu UMEMOTO 
103459dfcba4SHajimu UMEMOTO 	/*
1035df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
1036df8bae1dSRodney W. Grimes 	 */
10376effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
10386effc713SDoug Rabson 		if (GETIP(q)->ip_off > ip->ip_off)
1039df8bae1dSRodney W. Grimes 			break;
1040df8bae1dSRodney W. Grimes 
1041df8bae1dSRodney W. Grimes 	/*
1042df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
1043df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
1044af38c68cSLuigi Rizzo 	 * segment.  If it provides all of our data, drop us, otherwise
1045af38c68cSLuigi Rizzo 	 * stick new segment in the proper place.
1046db4f9cc7SJonathan Lemon 	 *
1047db4f9cc7SJonathan Lemon 	 * If some of the data is dropped from the the preceding
1048db4f9cc7SJonathan Lemon 	 * segment, then it's checksum is invalidated.
1049df8bae1dSRodney W. Grimes 	 */
10506effc713SDoug Rabson 	if (p) {
10516effc713SDoug Rabson 		i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
1052df8bae1dSRodney W. Grimes 		if (i > 0) {
1053df8bae1dSRodney W. Grimes 			if (i >= ip->ip_len)
1054df8bae1dSRodney W. Grimes 				goto dropfrag;
10556a800098SYoshinobu Inoue 			m_adj(m, i);
1056db4f9cc7SJonathan Lemon 			m->m_pkthdr.csum_flags = 0;
1057df8bae1dSRodney W. Grimes 			ip->ip_off += i;
1058df8bae1dSRodney W. Grimes 			ip->ip_len -= i;
1059df8bae1dSRodney W. Grimes 		}
1060af38c68cSLuigi Rizzo 		m->m_nextpkt = p->m_nextpkt;
1061af38c68cSLuigi Rizzo 		p->m_nextpkt = m;
1062af38c68cSLuigi Rizzo 	} else {
1063af38c68cSLuigi Rizzo 		m->m_nextpkt = fp->ipq_frags;
1064af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
1065df8bae1dSRodney W. Grimes 	}
1066df8bae1dSRodney W. Grimes 
1067df8bae1dSRodney W. Grimes 	/*
1068df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
1069df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
1070df8bae1dSRodney W. Grimes 	 */
10716effc713SDoug Rabson 	for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
1072af38c68cSLuigi Rizzo 	     q = nq) {
1073b36f5b37SMaxim Konovalov 		i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
10746effc713SDoug Rabson 		if (i < GETIP(q)->ip_len) {
10756effc713SDoug Rabson 			GETIP(q)->ip_len -= i;
10766effc713SDoug Rabson 			GETIP(q)->ip_off += i;
10776effc713SDoug Rabson 			m_adj(q, i);
1078db4f9cc7SJonathan Lemon 			q->m_pkthdr.csum_flags = 0;
1079df8bae1dSRodney W. Grimes 			break;
1080df8bae1dSRodney W. Grimes 		}
10816effc713SDoug Rabson 		nq = q->m_nextpkt;
1082af38c68cSLuigi Rizzo 		m->m_nextpkt = nq;
108386425c62SRobert Watson 		IPSTAT_INC(ips_fragdropped);
1084375386e2SMike Silbersack 		fp->ipq_nfrags--;
10856effc713SDoug Rabson 		m_freem(q);
1086df8bae1dSRodney W. Grimes 	}
1087df8bae1dSRodney W. Grimes 
1088df8bae1dSRodney W. Grimes 	/*
1089375386e2SMike Silbersack 	 * Check for complete reassembly and perform frag per packet
1090375386e2SMike Silbersack 	 * limiting.
1091375386e2SMike Silbersack 	 *
1092375386e2SMike Silbersack 	 * Frag limiting is performed here so that the nth frag has
1093375386e2SMike Silbersack 	 * a chance to complete the packet before we drop the packet.
1094375386e2SMike Silbersack 	 * As a result, n+1 frags are actually allowed per packet, but
1095375386e2SMike Silbersack 	 * only n will ever be stored. (n = maxfragsperpacket.)
1096375386e2SMike Silbersack 	 *
1097df8bae1dSRodney W. Grimes 	 */
10986effc713SDoug Rabson 	next = 0;
10996effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
1100375386e2SMike Silbersack 		if (GETIP(q)->ip_off != next) {
1101603724d3SBjoern A. Zeeb 			if (fp->ipq_nfrags > V_maxfragsperpacket) {
110286425c62SRobert Watson 				IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1103375386e2SMike Silbersack 				ip_freef(head, fp);
110499e8617dSMaxim Konovalov 			}
1105f0cada84SAndre Oppermann 			goto done;
1106375386e2SMike Silbersack 		}
11076effc713SDoug Rabson 		next += GETIP(q)->ip_len;
11086effc713SDoug Rabson 	}
11096effc713SDoug Rabson 	/* Make sure the last packet didn't have the IP_MF flag */
1110375386e2SMike Silbersack 	if (p->m_flags & M_FRAG) {
1111603724d3SBjoern A. Zeeb 		if (fp->ipq_nfrags > V_maxfragsperpacket) {
111286425c62SRobert Watson 			IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1113375386e2SMike Silbersack 			ip_freef(head, fp);
111499e8617dSMaxim Konovalov 		}
1115f0cada84SAndre Oppermann 		goto done;
1116375386e2SMike Silbersack 	}
1117df8bae1dSRodney W. Grimes 
1118df8bae1dSRodney W. Grimes 	/*
1119430d30d8SBill Fenner 	 * Reassembly is complete.  Make sure the packet is a sane size.
1120430d30d8SBill Fenner 	 */
11216effc713SDoug Rabson 	q = fp->ipq_frags;
11226effc713SDoug Rabson 	ip = GETIP(q);
112353be11f6SPoul-Henning Kamp 	if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
112486425c62SRobert Watson 		IPSTAT_INC(ips_toolong);
112586425c62SRobert Watson 		IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1126462b86feSPoul-Henning Kamp 		ip_freef(head, fp);
1127f0cada84SAndre Oppermann 		goto done;
1128430d30d8SBill Fenner 	}
1129430d30d8SBill Fenner 
1130430d30d8SBill Fenner 	/*
1131430d30d8SBill Fenner 	 * Concatenate fragments.
1132df8bae1dSRodney W. Grimes 	 */
11336effc713SDoug Rabson 	m = q;
1134df8bae1dSRodney W. Grimes 	t = m->m_next;
113502410549SRobert Watson 	m->m_next = NULL;
1136df8bae1dSRodney W. Grimes 	m_cat(m, t);
11376effc713SDoug Rabson 	nq = q->m_nextpkt;
113802410549SRobert Watson 	q->m_nextpkt = NULL;
11396effc713SDoug Rabson 	for (q = nq; q != NULL; q = nq) {
11406effc713SDoug Rabson 		nq = q->m_nextpkt;
1141945aa40dSDoug Rabson 		q->m_nextpkt = NULL;
1142db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1143db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1144a8db1d93SJonathan Lemon 		m_cat(m, q);
1145df8bae1dSRodney W. Grimes 	}
11466edb555dSOleg Bulyzhin 	/*
11476edb555dSOleg Bulyzhin 	 * In order to do checksumming faster we do 'end-around carry' here
11486edb555dSOleg Bulyzhin 	 * (and not in for{} loop), though it implies we are not going to
11496edb555dSOleg Bulyzhin 	 * reassemble more than 64k fragments.
11506edb555dSOleg Bulyzhin 	 */
11516edb555dSOleg Bulyzhin 	m->m_pkthdr.csum_data =
11526edb555dSOleg Bulyzhin 	    (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16);
115336b0360bSRobert Watson #ifdef MAC
115430d239bcSRobert Watson 	mac_ipq_reassemble(fp, m);
115530d239bcSRobert Watson 	mac_ipq_destroy(fp);
115636b0360bSRobert Watson #endif
1157df8bae1dSRodney W. Grimes 
1158df8bae1dSRodney W. Grimes 	/*
1159f0cada84SAndre Oppermann 	 * Create header for new ip packet by modifying header of first
1160f0cada84SAndre Oppermann 	 * packet;  dequeue and discard fragment reassembly header.
1161df8bae1dSRodney W. Grimes 	 * Make header visible.
1162df8bae1dSRodney W. Grimes 	 */
1163f0cada84SAndre Oppermann 	ip->ip_len = (ip->ip_hl << 2) + next;
11646effc713SDoug Rabson 	ip->ip_src = fp->ipq_src;
11656effc713SDoug Rabson 	ip->ip_dst = fp->ipq_dst;
1166462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(head, fp, ipq_list);
1167603724d3SBjoern A. Zeeb 	V_nipq--;
1168603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
116953be11f6SPoul-Henning Kamp 	m->m_len += (ip->ip_hl << 2);
117053be11f6SPoul-Henning Kamp 	m->m_data -= (ip->ip_hl << 2);
1171df8bae1dSRodney W. Grimes 	/* some debugging cruft by sklower, below, will go away soon */
1172a5554bf0SPoul-Henning Kamp 	if (m->m_flags & M_PKTHDR)	/* XXX this should be done elsewhere */
1173a5554bf0SPoul-Henning Kamp 		m_fixhdr(m);
117486425c62SRobert Watson 	IPSTAT_INC(ips_reassembled);
1175f0cada84SAndre Oppermann 	IPQ_UNLOCK();
11766a800098SYoshinobu Inoue 	return (m);
1177df8bae1dSRodney W. Grimes 
1178df8bae1dSRodney W. Grimes dropfrag:
117986425c62SRobert Watson 	IPSTAT_INC(ips_fragdropped);
1180042bbfa3SRobert Watson 	if (fp != NULL)
1181375386e2SMike Silbersack 		fp->ipq_nfrags--;
1182df8bae1dSRodney W. Grimes 	m_freem(m);
1183f0cada84SAndre Oppermann done:
1184f0cada84SAndre Oppermann 	IPQ_UNLOCK();
1185f0cada84SAndre Oppermann 	return (NULL);
11866effc713SDoug Rabson 
11876effc713SDoug Rabson #undef GETIP
1188df8bae1dSRodney W. Grimes }
1189df8bae1dSRodney W. Grimes 
1190df8bae1dSRodney W. Grimes /*
1191df8bae1dSRodney W. Grimes  * Free a fragment reassembly header and all
1192df8bae1dSRodney W. Grimes  * associated datagrams.
1193df8bae1dSRodney W. Grimes  */
11940312fbe9SPoul-Henning Kamp static void
1195f2565d68SRobert Watson ip_freef(struct ipqhead *fhp, struct ipq *fp)
1196df8bae1dSRodney W. Grimes {
11978b615593SMarko Zec 	INIT_VNET_INET(curvnet);
1198f2565d68SRobert Watson 	struct mbuf *q;
1199df8bae1dSRodney W. Grimes 
12002fad1e93SSam Leffler 	IPQ_LOCK_ASSERT();
12012fad1e93SSam Leffler 
12026effc713SDoug Rabson 	while (fp->ipq_frags) {
12036effc713SDoug Rabson 		q = fp->ipq_frags;
12046effc713SDoug Rabson 		fp->ipq_frags = q->m_nextpkt;
12056effc713SDoug Rabson 		m_freem(q);
1206df8bae1dSRodney W. Grimes 	}
1207462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(fhp, fp, ipq_list);
1208603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
1209603724d3SBjoern A. Zeeb 	V_nipq--;
1210df8bae1dSRodney W. Grimes }
1211df8bae1dSRodney W. Grimes 
1212df8bae1dSRodney W. Grimes /*
1213df8bae1dSRodney W. Grimes  * IP timer processing;
1214df8bae1dSRodney W. Grimes  * if a timer expires on a reassembly
1215df8bae1dSRodney W. Grimes  * queue, discard it.
1216df8bae1dSRodney W. Grimes  */
1217df8bae1dSRodney W. Grimes void
1218f2565d68SRobert Watson ip_slowtimo(void)
1219df8bae1dSRodney W. Grimes {
12208b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1221f2565d68SRobert Watson 	struct ipq *fp;
1222194a213eSAndrey A. Chernov 	int i;
1223df8bae1dSRodney W. Grimes 
12242fad1e93SSam Leffler 	IPQ_LOCK();
12258b615593SMarko Zec 	VNET_LIST_RLOCK();
12268b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
12278b615593SMarko Zec 		CURVNET_SET(vnet_iter);
12288b615593SMarko Zec 		INIT_VNET_INET(vnet_iter);
1229194a213eSAndrey A. Chernov 		for (i = 0; i < IPREASS_NHASH; i++) {
1230603724d3SBjoern A. Zeeb 			for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) {
1231462b86feSPoul-Henning Kamp 				struct ipq *fpp;
1232462b86feSPoul-Henning Kamp 
1233462b86feSPoul-Henning Kamp 				fpp = fp;
1234462b86feSPoul-Henning Kamp 				fp = TAILQ_NEXT(fp, ipq_list);
1235462b86feSPoul-Henning Kamp 				if(--fpp->ipq_ttl == 0) {
123686425c62SRobert Watson 					IPSTAT_ADD(ips_fragtimeout,
123786425c62SRobert Watson 					    fpp->ipq_nfrags);
1238603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], fpp);
1239df8bae1dSRodney W. Grimes 				}
1240df8bae1dSRodney W. Grimes 			}
1241194a213eSAndrey A. Chernov 		}
1242690a6055SJesper Skriver 		/*
1243690a6055SJesper Skriver 		 * If we are over the maximum number of fragments
1244690a6055SJesper Skriver 		 * (due to the limit being lowered), drain off
1245690a6055SJesper Skriver 		 * enough to get down to the new limit.
1246690a6055SJesper Skriver 		 */
1247603724d3SBjoern A. Zeeb 		if (V_maxnipq >= 0 && V_nipq > V_maxnipq) {
1248690a6055SJesper Skriver 			for (i = 0; i < IPREASS_NHASH; i++) {
12498b615593SMarko Zec 				while (V_nipq > V_maxnipq &&
12508b615593SMarko Zec 				    !TAILQ_EMPTY(&V_ipq[i])) {
125186425c62SRobert Watson 					IPSTAT_ADD(ips_fragdropped,
125286425c62SRobert Watson 					    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
12538b615593SMarko Zec 					ip_freef(&V_ipq[i],
12548b615593SMarko Zec 					    TAILQ_FIRST(&V_ipq[i]));
1255690a6055SJesper Skriver 				}
1256690a6055SJesper Skriver 			}
1257690a6055SJesper Skriver 		}
12588b615593SMarko Zec 		CURVNET_RESTORE();
12598b615593SMarko Zec 	}
12608b615593SMarko Zec 	VNET_LIST_RUNLOCK();
12612fad1e93SSam Leffler 	IPQ_UNLOCK();
1262df8bae1dSRodney W. Grimes }
1263df8bae1dSRodney W. Grimes 
1264df8bae1dSRodney W. Grimes /*
1265df8bae1dSRodney W. Grimes  * Drain off all datagram fragments.
1266df8bae1dSRodney W. Grimes  */
1267df8bae1dSRodney W. Grimes void
1268f2565d68SRobert Watson ip_drain(void)
1269df8bae1dSRodney W. Grimes {
12708b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1271194a213eSAndrey A. Chernov 	int     i;
1272ce29ab3aSGarrett Wollman 
12732fad1e93SSam Leffler 	IPQ_LOCK();
12748b615593SMarko Zec 	VNET_LIST_RLOCK();
12758b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
12768b615593SMarko Zec 		CURVNET_SET(vnet_iter);
12778b615593SMarko Zec 		INIT_VNET_INET(vnet_iter);
1278194a213eSAndrey A. Chernov 		for (i = 0; i < IPREASS_NHASH; i++) {
1279603724d3SBjoern A. Zeeb 			while(!TAILQ_EMPTY(&V_ipq[i])) {
128086425c62SRobert Watson 				IPSTAT_ADD(ips_fragdropped,
128186425c62SRobert Watson 				    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
1282603724d3SBjoern A. Zeeb 				ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1283194a213eSAndrey A. Chernov 			}
1284194a213eSAndrey A. Chernov 		}
12858b615593SMarko Zec 		CURVNET_RESTORE();
12868b615593SMarko Zec 	}
12878b615593SMarko Zec 	VNET_LIST_RUNLOCK();
12882fad1e93SSam Leffler 	IPQ_UNLOCK();
1289ce29ab3aSGarrett Wollman 	in_rtqdrain();
1290df8bae1dSRodney W. Grimes }
1291df8bae1dSRodney W. Grimes 
1292df8bae1dSRodney W. Grimes /*
1293de38924dSAndre Oppermann  * The protocol to be inserted into ip_protox[] must be already registered
1294de38924dSAndre Oppermann  * in inetsw[], either statically or through pf_proto_register().
1295de38924dSAndre Oppermann  */
1296de38924dSAndre Oppermann int
1297de38924dSAndre Oppermann ipproto_register(u_char ipproto)
1298de38924dSAndre Oppermann {
1299de38924dSAndre Oppermann 	struct protosw *pr;
1300de38924dSAndre Oppermann 
1301de38924dSAndre Oppermann 	/* Sanity checks. */
1302de38924dSAndre Oppermann 	if (ipproto == 0)
1303de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1304de38924dSAndre Oppermann 
1305de38924dSAndre Oppermann 	/*
1306de38924dSAndre Oppermann 	 * The protocol slot must not be occupied by another protocol
1307de38924dSAndre Oppermann 	 * already.  An index pointing to IPPROTO_RAW is unused.
1308de38924dSAndre Oppermann 	 */
1309de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1310de38924dSAndre Oppermann 	if (pr == NULL)
1311de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1312de38924dSAndre Oppermann 	if (ip_protox[ipproto] != pr - inetsw)	/* IPPROTO_RAW */
1313de38924dSAndre Oppermann 		return (EEXIST);
1314de38924dSAndre Oppermann 
1315de38924dSAndre Oppermann 	/* Find the protocol position in inetsw[] and set the index. */
1316de38924dSAndre Oppermann 	for (pr = inetdomain.dom_protosw;
1317de38924dSAndre Oppermann 	     pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1318de38924dSAndre Oppermann 		if (pr->pr_domain->dom_family == PF_INET &&
1319de38924dSAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol == ipproto) {
1320de38924dSAndre Oppermann 			/* Be careful to only index valid IP protocols. */
1321db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX) {
1322de38924dSAndre Oppermann 				ip_protox[pr->pr_protocol] = pr - inetsw;
1323de38924dSAndre Oppermann 				return (0);
1324de38924dSAndre Oppermann 			} else
1325de38924dSAndre Oppermann 				return (EINVAL);
1326de38924dSAndre Oppermann 		}
1327de38924dSAndre Oppermann 	}
1328de38924dSAndre Oppermann 	return (EPROTONOSUPPORT);
1329de38924dSAndre Oppermann }
1330de38924dSAndre Oppermann 
1331de38924dSAndre Oppermann int
1332de38924dSAndre Oppermann ipproto_unregister(u_char ipproto)
1333de38924dSAndre Oppermann {
1334de38924dSAndre Oppermann 	struct protosw *pr;
1335de38924dSAndre Oppermann 
1336de38924dSAndre Oppermann 	/* Sanity checks. */
1337de38924dSAndre Oppermann 	if (ipproto == 0)
1338de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1339de38924dSAndre Oppermann 
1340de38924dSAndre Oppermann 	/* Check if the protocol was indeed registered. */
1341de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1342de38924dSAndre Oppermann 	if (pr == NULL)
1343de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1344de38924dSAndre Oppermann 	if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
1345de38924dSAndre Oppermann 		return (ENOENT);
1346de38924dSAndre Oppermann 
1347de38924dSAndre Oppermann 	/* Reset the protocol slot to IPPROTO_RAW. */
1348de38924dSAndre Oppermann 	ip_protox[ipproto] = pr - inetsw;
1349de38924dSAndre Oppermann 	return (0);
1350de38924dSAndre Oppermann }
1351de38924dSAndre Oppermann 
1352df8bae1dSRodney W. Grimes /*
13538c0fec80SRobert Watson  * Given address of next destination (final or next hop), return (referenced)
13548c0fec80SRobert Watson  * internet address info of interface to be used to get there.
1355df8bae1dSRodney W. Grimes  */
1356bd714208SRuslan Ermilov struct in_ifaddr *
13578b07e49aSJulian Elischer ip_rtaddr(struct in_addr dst, u_int fibnum)
1358df8bae1dSRodney W. Grimes {
135997d8d152SAndre Oppermann 	struct route sro;
136002c1c707SAndre Oppermann 	struct sockaddr_in *sin;
136119e5b0a7SRobert Watson 	struct in_ifaddr *ia;
1362df8bae1dSRodney W. Grimes 
13630cfbbe3bSAndre Oppermann 	bzero(&sro, sizeof(sro));
136497d8d152SAndre Oppermann 	sin = (struct sockaddr_in *)&sro.ro_dst;
1365df8bae1dSRodney W. Grimes 	sin->sin_family = AF_INET;
1366df8bae1dSRodney W. Grimes 	sin->sin_len = sizeof(*sin);
1367df8bae1dSRodney W. Grimes 	sin->sin_addr = dst;
13686e6b3f7cSQing Li 	in_rtalloc_ign(&sro, 0, fibnum);
1369df8bae1dSRodney W. Grimes 
137097d8d152SAndre Oppermann 	if (sro.ro_rt == NULL)
137102410549SRobert Watson 		return (NULL);
137202c1c707SAndre Oppermann 
137319e5b0a7SRobert Watson 	ia = ifatoia(sro.ro_rt->rt_ifa);
137419e5b0a7SRobert Watson 	ifa_ref(&ia->ia_ifa);
137597d8d152SAndre Oppermann 	RTFREE(sro.ro_rt);
137619e5b0a7SRobert Watson 	return (ia);
1377df8bae1dSRodney W. Grimes }
1378df8bae1dSRodney W. Grimes 
1379df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = {
1380df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1381df8bae1dSRodney W. Grimes 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1382df8bae1dSRodney W. Grimes 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1383df8bae1dSRodney W. Grimes 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1384fcaf9f91SMike Silbersack 	0,		0,		EHOSTUNREACH,	0,
13853b8123b7SJesper Skriver 	ENOPROTOOPT,	ECONNREFUSED
1386df8bae1dSRodney W. Grimes };
1387df8bae1dSRodney W. Grimes 
1388df8bae1dSRodney W. Grimes /*
1389df8bae1dSRodney W. Grimes  * Forward a packet.  If some error occurs return the sender
1390df8bae1dSRodney W. Grimes  * an icmp packet.  Note we can't always generate a meaningful
1391df8bae1dSRodney W. Grimes  * icmp message because icmp doesn't have a large enough repertoire
1392df8bae1dSRodney W. Grimes  * of codes and types.
1393df8bae1dSRodney W. Grimes  *
1394df8bae1dSRodney W. Grimes  * If not forwarding, just drop the packet.  This could be confusing
1395df8bae1dSRodney W. Grimes  * if ipforwarding was zero but some routing protocol was advancing
1396df8bae1dSRodney W. Grimes  * us as a gateway to somewhere.  However, we must let the routing
1397df8bae1dSRodney W. Grimes  * protocol deal with that.
1398df8bae1dSRodney W. Grimes  *
1399df8bae1dSRodney W. Grimes  * The srcrt parameter indicates whether the packet is being forwarded
1400df8bae1dSRodney W. Grimes  * via a source route.
1401df8bae1dSRodney W. Grimes  */
14029b932e9eSAndre Oppermann void
14039b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt)
1404df8bae1dSRodney W. Grimes {
14058b615593SMarko Zec 	INIT_VNET_INET(curvnet);
14062b25acc1SLuigi Rizzo 	struct ip *ip = mtod(m, struct ip *);
1407efbad259SEdward Tomasz Napierala 	struct in_ifaddr *ia;
1408df8bae1dSRodney W. Grimes 	struct mbuf *mcopy;
14099b932e9eSAndre Oppermann 	struct in_addr dest;
1410b835b6feSBjoern A. Zeeb 	struct route ro;
1411c773494eSAndre Oppermann 	int error, type = 0, code = 0, mtu = 0;
14123efc3014SJulian Elischer 
14139b932e9eSAndre Oppermann 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
141486425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
1415df8bae1dSRodney W. Grimes 		m_freem(m);
1416df8bae1dSRodney W. Grimes 		return;
1417df8bae1dSRodney W. Grimes 	}
14181b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
1419603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
14201b968362SDag-Erling Smørgrav #endif
1421df8bae1dSRodney W. Grimes 		if (ip->ip_ttl <= IPTTLDEC) {
14221b968362SDag-Erling Smørgrav 			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
142302c1c707SAndre Oppermann 			    0, 0);
1424df8bae1dSRodney W. Grimes 			return;
1425df8bae1dSRodney W. Grimes 		}
14261b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
14271b968362SDag-Erling Smørgrav 	}
14281b968362SDag-Erling Smørgrav #endif
1429df8bae1dSRodney W. Grimes 
14308b07e49aSJulian Elischer 	ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
1431efbad259SEdward Tomasz Napierala #ifndef IPSEC
1432efbad259SEdward Tomasz Napierala 	/*
1433efbad259SEdward Tomasz Napierala 	 * 'ia' may be NULL if there is no route for this destination.
1434efbad259SEdward Tomasz Napierala 	 * In case of IPsec, Don't discard it just yet, but pass it to
1435efbad259SEdward Tomasz Napierala 	 * ip_output in case of outgoing IPsec policy.
1436efbad259SEdward Tomasz Napierala 	 */
1437d23d475fSGuido van Rooij 	if (!srcrt && ia == NULL) {
143802c1c707SAndre Oppermann 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1439df8bae1dSRodney W. Grimes 		return;
144002c1c707SAndre Oppermann 	}
1441efbad259SEdward Tomasz Napierala #endif
1442df8bae1dSRodney W. Grimes 
1443df8bae1dSRodney W. Grimes 	/*
1444bfef7ed4SIan Dowse 	 * Save the IP header and at most 8 bytes of the payload,
1445bfef7ed4SIan Dowse 	 * in case we need to generate an ICMP message to the src.
1446bfef7ed4SIan Dowse 	 *
14474d2e3692SLuigi Rizzo 	 * XXX this can be optimized a lot by saving the data in a local
14484d2e3692SLuigi Rizzo 	 * buffer on the stack (72 bytes at most), and only allocating the
14494d2e3692SLuigi Rizzo 	 * mbuf if really necessary. The vast majority of the packets
14504d2e3692SLuigi Rizzo 	 * are forwarded without having to send an ICMP back (either
14514d2e3692SLuigi Rizzo 	 * because unnecessary, or because rate limited), so we are
14524d2e3692SLuigi Rizzo 	 * really we are wasting a lot of work here.
14534d2e3692SLuigi Rizzo 	 *
1454bfef7ed4SIan Dowse 	 * We don't use m_copy() because it might return a reference
1455bfef7ed4SIan Dowse 	 * to a shared cluster. Both this function and ip_output()
1456bfef7ed4SIan Dowse 	 * assume exclusive access to the IP header in `m', so any
1457bfef7ed4SIan Dowse 	 * data in a cluster may change before we reach icmp_error().
1458df8bae1dSRodney W. Grimes 	 */
1459780b2f69SAndre Oppermann 	MGETHDR(mcopy, M_DONTWAIT, m->m_type);
1460a163d034SWarner Losh 	if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) {
14619967cafcSSam Leffler 		/*
14629967cafcSSam Leffler 		 * It's probably ok if the pkthdr dup fails (because
14639967cafcSSam Leffler 		 * the deep copy of the tag chain failed), but for now
14649967cafcSSam Leffler 		 * be conservative and just discard the copy since
14659967cafcSSam Leffler 		 * code below may some day want the tags.
14669967cafcSSam Leffler 		 */
14679967cafcSSam Leffler 		m_free(mcopy);
14689967cafcSSam Leffler 		mcopy = NULL;
14699967cafcSSam Leffler 	}
1470bfef7ed4SIan Dowse 	if (mcopy != NULL) {
1471780b2f69SAndre Oppermann 		mcopy->m_len = min(ip->ip_len, M_TRAILINGSPACE(mcopy));
1472e6b0a570SBruce M Simpson 		mcopy->m_pkthdr.len = mcopy->m_len;
1473bfef7ed4SIan Dowse 		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1474bfef7ed4SIan Dowse 	}
147504287599SRuslan Ermilov 
147604287599SRuslan Ermilov #ifdef IPSTEALTH
1477603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
147804287599SRuslan Ermilov #endif
147904287599SRuslan Ermilov 		ip->ip_ttl -= IPTTLDEC;
148004287599SRuslan Ermilov #ifdef IPSTEALTH
148104287599SRuslan Ermilov 	}
148204287599SRuslan Ermilov #endif
1483df8bae1dSRodney W. Grimes 
1484df8bae1dSRodney W. Grimes 	/*
1485df8bae1dSRodney W. Grimes 	 * If forwarding packet using same interface that it came in on,
1486df8bae1dSRodney W. Grimes 	 * perhaps should send a redirect to sender to shortcut a hop.
1487df8bae1dSRodney W. Grimes 	 * Only send redirect if source is sending directly to us,
1488df8bae1dSRodney W. Grimes 	 * and if packet was not source routed (or has any options).
1489df8bae1dSRodney W. Grimes 	 * Also, don't send redirect if forwarding using a default route
1490df8bae1dSRodney W. Grimes 	 * or a route modified by a redirect.
1491df8bae1dSRodney W. Grimes 	 */
14929b932e9eSAndre Oppermann 	dest.s_addr = 0;
1493efbad259SEdward Tomasz Napierala 	if (!srcrt && V_ipsendredirects &&
1494efbad259SEdward Tomasz Napierala 	    ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) {
149502c1c707SAndre Oppermann 		struct sockaddr_in *sin;
149602c1c707SAndre Oppermann 		struct rtentry *rt;
149702c1c707SAndre Oppermann 
14980cfbbe3bSAndre Oppermann 		bzero(&ro, sizeof(ro));
149902c1c707SAndre Oppermann 		sin = (struct sockaddr_in *)&ro.ro_dst;
150002c1c707SAndre Oppermann 		sin->sin_family = AF_INET;
150102c1c707SAndre Oppermann 		sin->sin_len = sizeof(*sin);
15029b932e9eSAndre Oppermann 		sin->sin_addr = ip->ip_dst;
15036e6b3f7cSQing Li 		in_rtalloc_ign(&ro, 0, M_GETFIB(m));
150402c1c707SAndre Oppermann 
150502c1c707SAndre Oppermann 		rt = ro.ro_rt;
150602c1c707SAndre Oppermann 
150702c1c707SAndre Oppermann 		if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
15089b932e9eSAndre Oppermann 		    satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1509df8bae1dSRodney W. Grimes #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1510df8bae1dSRodney W. Grimes 			u_long src = ntohl(ip->ip_src.s_addr);
1511df8bae1dSRodney W. Grimes 
1512df8bae1dSRodney W. Grimes 			if (RTA(rt) &&
1513df8bae1dSRodney W. Grimes 			    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1514df8bae1dSRodney W. Grimes 				if (rt->rt_flags & RTF_GATEWAY)
15159b932e9eSAndre Oppermann 					dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1516df8bae1dSRodney W. Grimes 				else
15179b932e9eSAndre Oppermann 					dest.s_addr = ip->ip_dst.s_addr;
1518df8bae1dSRodney W. Grimes 				/* Router requirements says to only send host redirects */
1519df8bae1dSRodney W. Grimes 				type = ICMP_REDIRECT;
1520df8bae1dSRodney W. Grimes 				code = ICMP_REDIRECT_HOST;
1521df8bae1dSRodney W. Grimes 			}
1522df8bae1dSRodney W. Grimes 		}
152302c1c707SAndre Oppermann 		if (rt)
152402c1c707SAndre Oppermann 			RTFREE(rt);
152502c1c707SAndre Oppermann 	}
1526df8bae1dSRodney W. Grimes 
1527b835b6feSBjoern A. Zeeb 	/*
1528b835b6feSBjoern A. Zeeb 	 * Try to cache the route MTU from ip_output so we can consider it for
1529b835b6feSBjoern A. Zeeb 	 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191.
1530b835b6feSBjoern A. Zeeb 	 */
1531b835b6feSBjoern A. Zeeb 	bzero(&ro, sizeof(ro));
1532b835b6feSBjoern A. Zeeb 
1533b835b6feSBjoern A. Zeeb 	error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1534b835b6feSBjoern A. Zeeb 
1535b835b6feSBjoern A. Zeeb 	if (error == EMSGSIZE && ro.ro_rt)
1536b835b6feSBjoern A. Zeeb 		mtu = ro.ro_rt->rt_rmx.rmx_mtu;
1537b835b6feSBjoern A. Zeeb 	if (ro.ro_rt)
1538b835b6feSBjoern A. Zeeb 		RTFREE(ro.ro_rt);
1539b835b6feSBjoern A. Zeeb 
1540df8bae1dSRodney W. Grimes 	if (error)
154186425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
1542df8bae1dSRodney W. Grimes 	else {
154386425c62SRobert Watson 		IPSTAT_INC(ips_forward);
1544df8bae1dSRodney W. Grimes 		if (type)
154586425c62SRobert Watson 			IPSTAT_INC(ips_redirectsent);
1546df8bae1dSRodney W. Grimes 		else {
15479188b4a1SAndre Oppermann 			if (mcopy)
1548df8bae1dSRodney W. Grimes 				m_freem(mcopy);
15498c0fec80SRobert Watson 			if (ia != NULL)
15508c0fec80SRobert Watson 				ifa_free(&ia->ia_ifa);
1551df8bae1dSRodney W. Grimes 			return;
1552df8bae1dSRodney W. Grimes 		}
1553df8bae1dSRodney W. Grimes 	}
15548c0fec80SRobert Watson 	if (mcopy == NULL) {
15558c0fec80SRobert Watson 		if (ia != NULL)
15568c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
1557df8bae1dSRodney W. Grimes 		return;
15588c0fec80SRobert Watson 	}
1559df8bae1dSRodney W. Grimes 
1560df8bae1dSRodney W. Grimes 	switch (error) {
1561df8bae1dSRodney W. Grimes 
1562df8bae1dSRodney W. Grimes 	case 0:				/* forwarded, but need redirect */
1563df8bae1dSRodney W. Grimes 		/* type, code set above */
1564df8bae1dSRodney W. Grimes 		break;
1565df8bae1dSRodney W. Grimes 
1566efbad259SEdward Tomasz Napierala 	case ENETUNREACH:
1567df8bae1dSRodney W. Grimes 	case EHOSTUNREACH:
1568df8bae1dSRodney W. Grimes 	case ENETDOWN:
1569df8bae1dSRodney W. Grimes 	case EHOSTDOWN:
1570df8bae1dSRodney W. Grimes 	default:
1571df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1572df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_HOST;
1573df8bae1dSRodney W. Grimes 		break;
1574df8bae1dSRodney W. Grimes 
1575df8bae1dSRodney W. Grimes 	case EMSGSIZE:
1576df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1577df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_NEEDFRAG;
15781dfcf0d2SAndre Oppermann 
1579b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
1580b835b6feSBjoern A. Zeeb 		/*
1581b835b6feSBjoern A. Zeeb 		 * If IPsec is configured for this path,
1582b835b6feSBjoern A. Zeeb 		 * override any possibly mtu value set by ip_output.
1583b835b6feSBjoern A. Zeeb 		 */
1584b835b6feSBjoern A. Zeeb 		mtu = ip_ipsec_mtu(m, mtu);
1585b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
15869b932e9eSAndre Oppermann 		/*
1587b835b6feSBjoern A. Zeeb 		 * If the MTU was set before make sure we are below the
1588b835b6feSBjoern A. Zeeb 		 * interface MTU.
1589ab48768bSAndre Oppermann 		 * If the MTU wasn't set before use the interface mtu or
1590ab48768bSAndre Oppermann 		 * fall back to the next smaller mtu step compared to the
1591ab48768bSAndre Oppermann 		 * current packet size.
15929b932e9eSAndre Oppermann 		 */
1593b835b6feSBjoern A. Zeeb 		if (mtu != 0) {
1594b835b6feSBjoern A. Zeeb 			if (ia != NULL)
1595b835b6feSBjoern A. Zeeb 				mtu = min(mtu, ia->ia_ifp->if_mtu);
1596b835b6feSBjoern A. Zeeb 		} else {
1597ab48768bSAndre Oppermann 			if (ia != NULL)
1598c773494eSAndre Oppermann 				mtu = ia->ia_ifp->if_mtu;
1599ab48768bSAndre Oppermann 			else
1600ab48768bSAndre Oppermann 				mtu = ip_next_mtu(ip->ip_len, 0);
1601ab48768bSAndre Oppermann 		}
160286425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
1603df8bae1dSRodney W. Grimes 		break;
1604df8bae1dSRodney W. Grimes 
1605df8bae1dSRodney W. Grimes 	case ENOBUFS:
1606df285b3dSMike Silbersack 		/*
1607df285b3dSMike Silbersack 		 * A router should not generate ICMP_SOURCEQUENCH as
1608df285b3dSMike Silbersack 		 * required in RFC1812 Requirements for IP Version 4 Routers.
1609df285b3dSMike Silbersack 		 * Source quench could be a big problem under DoS attacks,
1610df285b3dSMike Silbersack 		 * or if the underlying interface is rate-limited.
1611df285b3dSMike Silbersack 		 * Those who need source quench packets may re-enable them
1612df285b3dSMike Silbersack 		 * via the net.inet.ip.sendsourcequench sysctl.
1613df285b3dSMike Silbersack 		 */
1614603724d3SBjoern A. Zeeb 		if (V_ip_sendsourcequench == 0) {
1615df285b3dSMike Silbersack 			m_freem(mcopy);
16168c0fec80SRobert Watson 			if (ia != NULL)
16178c0fec80SRobert Watson 				ifa_free(&ia->ia_ifa);
1618df285b3dSMike Silbersack 			return;
1619df285b3dSMike Silbersack 		} else {
1620df8bae1dSRodney W. Grimes 			type = ICMP_SOURCEQUENCH;
1621df8bae1dSRodney W. Grimes 			code = 0;
1622df285b3dSMike Silbersack 		}
1623df8bae1dSRodney W. Grimes 		break;
16243a06e3e0SRuslan Ermilov 
16253a06e3e0SRuslan Ermilov 	case EACCES:			/* ipfw denied packet */
16263a06e3e0SRuslan Ermilov 		m_freem(mcopy);
16278c0fec80SRobert Watson 		if (ia != NULL)
16288c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
16293a06e3e0SRuslan Ermilov 		return;
1630df8bae1dSRodney W. Grimes 	}
16318c0fec80SRobert Watson 	if (ia != NULL)
16328c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
1633c773494eSAndre Oppermann 	icmp_error(mcopy, type, code, dest.s_addr, mtu);
1634df8bae1dSRodney W. Grimes }
1635df8bae1dSRodney W. Grimes 
163682c23ebaSBill Fenner void
1637f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1638f2565d68SRobert Watson     struct mbuf *m)
163982c23ebaSBill Fenner {
16408b615593SMarko Zec 	INIT_VNET_NET(inp->inp_vnet);
16418b615593SMarko Zec 
1642be8a62e8SPoul-Henning Kamp 	if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1643be8a62e8SPoul-Henning Kamp 		struct bintime bt;
1644be8a62e8SPoul-Henning Kamp 
1645be8a62e8SPoul-Henning Kamp 		bintime(&bt);
1646be8a62e8SPoul-Henning Kamp 		if (inp->inp_socket->so_options & SO_BINTIME) {
1647be8a62e8SPoul-Henning Kamp 			*mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt),
1648be8a62e8SPoul-Henning Kamp 			SCM_BINTIME, SOL_SOCKET);
1649be8a62e8SPoul-Henning Kamp 			if (*mp)
1650be8a62e8SPoul-Henning Kamp 				mp = &(*mp)->m_next;
1651be8a62e8SPoul-Henning Kamp 		}
165282c23ebaSBill Fenner 		if (inp->inp_socket->so_options & SO_TIMESTAMP) {
165382c23ebaSBill Fenner 			struct timeval tv;
165482c23ebaSBill Fenner 
1655be8a62e8SPoul-Henning Kamp 			bintime2timeval(&bt, &tv);
165682c23ebaSBill Fenner 			*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
165782c23ebaSBill Fenner 				SCM_TIMESTAMP, SOL_SOCKET);
165882c23ebaSBill Fenner 			if (*mp)
165982c23ebaSBill Fenner 				mp = &(*mp)->m_next;
16604cc20ab1SSeigo Tanimura 		}
1661be8a62e8SPoul-Henning Kamp 	}
166282c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVDSTADDR) {
166382c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
166482c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
166582c23ebaSBill Fenner 		if (*mp)
166682c23ebaSBill Fenner 			mp = &(*mp)->m_next;
166782c23ebaSBill Fenner 	}
16684957466bSMatthew N. Dodd 	if (inp->inp_flags & INP_RECVTTL) {
16694957466bSMatthew N. Dodd 		*mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
16704957466bSMatthew N. Dodd 		    sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
16714957466bSMatthew N. Dodd 		if (*mp)
16724957466bSMatthew N. Dodd 			mp = &(*mp)->m_next;
16734957466bSMatthew N. Dodd 	}
167482c23ebaSBill Fenner #ifdef notyet
167582c23ebaSBill Fenner 	/* XXX
167682c23ebaSBill Fenner 	 * Moving these out of udp_input() made them even more broken
167782c23ebaSBill Fenner 	 * than they already were.
167882c23ebaSBill Fenner 	 */
167982c23ebaSBill Fenner 	/* options were tossed already */
168082c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVOPTS) {
168182c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
168282c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
168382c23ebaSBill Fenner 		if (*mp)
168482c23ebaSBill Fenner 			mp = &(*mp)->m_next;
168582c23ebaSBill Fenner 	}
168682c23ebaSBill Fenner 	/* ip_srcroute doesn't do what we want here, need to fix */
168782c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVRETOPTS) {
1688e0982661SAndre Oppermann 		*mp = sbcreatecontrol((caddr_t) ip_srcroute(m),
168982c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
169082c23ebaSBill Fenner 		if (*mp)
169182c23ebaSBill Fenner 			mp = &(*mp)->m_next;
169282c23ebaSBill Fenner 	}
169382c23ebaSBill Fenner #endif
169482c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVIF) {
1695d314ad7bSJulian Elischer 		struct ifnet *ifp;
1696d314ad7bSJulian Elischer 		struct sdlbuf {
169782c23ebaSBill Fenner 			struct sockaddr_dl sdl;
1698d314ad7bSJulian Elischer 			u_char	pad[32];
1699d314ad7bSJulian Elischer 		} sdlbuf;
1700d314ad7bSJulian Elischer 		struct sockaddr_dl *sdp;
1701d314ad7bSJulian Elischer 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
170282c23ebaSBill Fenner 
1703d314ad7bSJulian Elischer 		if (((ifp = m->m_pkthdr.rcvif))
1704603724d3SBjoern A. Zeeb 		&& ( ifp->if_index && (ifp->if_index <= V_if_index))) {
17054a0d6638SRuslan Ermilov 			sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1706d314ad7bSJulian Elischer 			/*
1707d314ad7bSJulian Elischer 			 * Change our mind and don't try copy.
1708d314ad7bSJulian Elischer 			 */
1709d314ad7bSJulian Elischer 			if ((sdp->sdl_family != AF_LINK)
1710d314ad7bSJulian Elischer 			|| (sdp->sdl_len > sizeof(sdlbuf))) {
1711d314ad7bSJulian Elischer 				goto makedummy;
1712d314ad7bSJulian Elischer 			}
1713d314ad7bSJulian Elischer 			bcopy(sdp, sdl2, sdp->sdl_len);
1714d314ad7bSJulian Elischer 		} else {
1715d314ad7bSJulian Elischer makedummy:
1716d314ad7bSJulian Elischer 			sdl2->sdl_len
1717d314ad7bSJulian Elischer 				= offsetof(struct sockaddr_dl, sdl_data[0]);
1718d314ad7bSJulian Elischer 			sdl2->sdl_family = AF_LINK;
1719d314ad7bSJulian Elischer 			sdl2->sdl_index = 0;
1720d314ad7bSJulian Elischer 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1721d314ad7bSJulian Elischer 		}
1722d314ad7bSJulian Elischer 		*mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
172382c23ebaSBill Fenner 			IP_RECVIF, IPPROTO_IP);
172482c23ebaSBill Fenner 		if (*mp)
172582c23ebaSBill Fenner 			mp = &(*mp)->m_next;
172682c23ebaSBill Fenner 	}
172782c23ebaSBill Fenner }
172882c23ebaSBill Fenner 
17294d2e3692SLuigi Rizzo /*
173030916a2dSRobert Watson  * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
173130916a2dSRobert Watson  * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
173230916a2dSRobert Watson  * locking.  This code remains in ip_input.c as ip_mroute.c is optionally
173330916a2dSRobert Watson  * compiled.
17344d2e3692SLuigi Rizzo  */
1735df8bae1dSRodney W. Grimes int
1736f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so)
1737f0068c4aSGarrett Wollman {
17388b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
17398b615593SMarko Zec 
1740f0068c4aSGarrett Wollman 	if (so->so_type != SOCK_RAW ||
1741f0068c4aSGarrett Wollman 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1742f0068c4aSGarrett Wollman 		return EOPNOTSUPP;
1743f0068c4aSGarrett Wollman 
1744603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL)
1745f0068c4aSGarrett Wollman 		return EADDRINUSE;
1746f0068c4aSGarrett Wollman 
1747603724d3SBjoern A. Zeeb 	V_ip_rsvpd = so;
17481c5de19aSGarrett Wollman 	/*
17491c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-increment
17501c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
17511c5de19aSGarrett Wollman 	 */
1752603724d3SBjoern A. Zeeb 	if (!V_ip_rsvp_on) {
1753603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 1;
1754603724d3SBjoern A. Zeeb 		V_rsvp_on++;
17551c5de19aSGarrett Wollman 	}
1756f0068c4aSGarrett Wollman 
1757f0068c4aSGarrett Wollman 	return 0;
1758f0068c4aSGarrett Wollman }
1759f0068c4aSGarrett Wollman 
1760f0068c4aSGarrett Wollman int
1761f0068c4aSGarrett Wollman ip_rsvp_done(void)
1762f0068c4aSGarrett Wollman {
17638b615593SMarko Zec 	INIT_VNET_INET(curvnet);
17648b615593SMarko Zec 
1765603724d3SBjoern A. Zeeb 	V_ip_rsvpd = NULL;
17661c5de19aSGarrett Wollman 	/*
17671c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-decrement
17681c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
17691c5de19aSGarrett Wollman 	 */
1770603724d3SBjoern A. Zeeb 	if (V_ip_rsvp_on) {
1771603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 0;
1772603724d3SBjoern A. Zeeb 		V_rsvp_on--;
17731c5de19aSGarrett Wollman 	}
1774f0068c4aSGarrett Wollman 	return 0;
1775f0068c4aSGarrett Wollman }
1776bbb4330bSLuigi Rizzo 
1777bbb4330bSLuigi Rizzo void
1778bbb4330bSLuigi Rizzo rsvp_input(struct mbuf *m, int off)	/* XXX must fixup manually */
1779bbb4330bSLuigi Rizzo {
17808b615593SMarko Zec 	INIT_VNET_INET(curvnet);
17818b615593SMarko Zec 
1782bbb4330bSLuigi Rizzo 	if (rsvp_input_p) { /* call the real one if loaded */
1783bbb4330bSLuigi Rizzo 		rsvp_input_p(m, off);
1784bbb4330bSLuigi Rizzo 		return;
1785bbb4330bSLuigi Rizzo 	}
1786bbb4330bSLuigi Rizzo 
1787bbb4330bSLuigi Rizzo 	/* Can still get packets with rsvp_on = 0 if there is a local member
1788bbb4330bSLuigi Rizzo 	 * of the group to which the RSVP packet is addressed.  But in this
1789bbb4330bSLuigi Rizzo 	 * case we want to throw the packet away.
1790bbb4330bSLuigi Rizzo 	 */
1791bbb4330bSLuigi Rizzo 
1792603724d3SBjoern A. Zeeb 	if (!V_rsvp_on) {
1793bbb4330bSLuigi Rizzo 		m_freem(m);
1794bbb4330bSLuigi Rizzo 		return;
1795bbb4330bSLuigi Rizzo 	}
1796bbb4330bSLuigi Rizzo 
1797603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL) {
1798bbb4330bSLuigi Rizzo 		rip_input(m, off);
1799bbb4330bSLuigi Rizzo 		return;
1800bbb4330bSLuigi Rizzo 	}
1801bbb4330bSLuigi Rizzo 	/* Drop the packet */
1802bbb4330bSLuigi Rizzo 	m_freem(m);
1803bbb4330bSLuigi Rizzo }
1804