xref: /freebsd/sys/netinet/ip_input.c (revision d2035ffb7a70d9b52950c67970072257c5eef974)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
324b421e2dSMike Silbersack #include <sys/cdefs.h>
334b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
344b421e2dSMike Silbersack 
350ac40133SBrian Somers #include "opt_bootp.h"
3674a9466cSGary Palmer #include "opt_ipfw.h"
3727108a15SDag-Erling Smørgrav #include "opt_ipstealth.h"
386a800098SYoshinobu Inoue #include "opt_ipsec.h"
3936b0360bSRobert Watson #include "opt_mac.h"
40a9771948SGleb Smirnoff #include "opt_carp.h"
4174a9466cSGary Palmer 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
445f311da2SMike Silbersack #include <sys/callout.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46b715f178SLuigi Rizzo #include <sys/malloc.h>
47df8bae1dSRodney W. Grimes #include <sys/domain.h>
48df8bae1dSRodney W. Grimes #include <sys/protosw.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/time.h>
51df8bae1dSRodney W. Grimes #include <sys/kernel.h>
521025071fSGarrett Wollman #include <sys/syslog.h>
53b5e8ce9fSBruce Evans #include <sys/sysctl.h>
54603724d3SBjoern A. Zeeb #include <sys/vimage.h>
55df8bae1dSRodney W. Grimes 
56c85540ddSAndrey A. Chernov #include <net/pfil.h>
57df8bae1dSRodney W. Grimes #include <net/if.h>
589494d596SBrooks Davis #include <net/if_types.h>
59d314ad7bSJulian Elischer #include <net/if_var.h>
6082c23ebaSBill Fenner #include <net/if_dl.h>
61df8bae1dSRodney W. Grimes #include <net/route.h>
62748e0b0aSGarrett Wollman #include <net/netisr.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <netinet/in.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
66b5e8ce9fSBruce Evans #include <netinet/in_var.h>
67df8bae1dSRodney W. Grimes #include <netinet/ip.h>
68df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
69df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
70df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
71ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
7258938916SGarrett Wollman #include <machine/in_cksum.h>
73a9771948SGleb Smirnoff #ifdef DEV_CARP
74a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
75a9771948SGleb Smirnoff #endif
76b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
771dfcf0d2SAndre Oppermann #include <netinet/ip_ipsec.h>
78b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
79df8bae1dSRodney W. Grimes 
80f0068c4aSGarrett Wollman #include <sys/socketvar.h>
816ddbf1e2SGary Palmer 
82010b65f5SJulian Elischer /* XXX: Temporary until ipfw_ether and ipfw_bridge are converted. */
83010b65f5SJulian Elischer #include <netinet/ip_fw.h>
84010b65f5SJulian Elischer #include <netinet/ip_dummynet.h>
85010b65f5SJulian Elischer 
86aed55708SRobert Watson #include <security/mac/mac_framework.h>
87aed55708SRobert Watson 
88d2035ffbSEd Maste #ifdef CTASSERT
89d2035ffbSEd Maste CTASSERT(sizeof(struct ip) == 20);
90d2035ffbSEd Maste #endif
91d2035ffbSEd Maste 
921c5de19aSGarrett Wollman int rsvp_on = 0;
93f0068c4aSGarrett Wollman 
941f91d8c5SDavid Greenman int	ipforwarding = 0;
950312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
963d177f46SBill Fumerola     &ipforwarding, 0, "Enable IP forwarding between interfaces");
970312fbe9SPoul-Henning Kamp 
98d4fb926cSGarrett Wollman static int	ipsendredirects = 1; /* XXX */
990312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
1003d177f46SBill Fumerola     &ipsendredirects, 0, "Enable sending IP redirects");
1010312fbe9SPoul-Henning Kamp 
102df8bae1dSRodney W. Grimes int	ip_defttl = IPDEFTTL;
1030312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
1043d177f46SBill Fumerola     &ip_defttl, 0, "Maximum TTL on IP packets");
1050312fbe9SPoul-Henning Kamp 
1066a800098SYoshinobu Inoue static int	ip_keepfaith = 0;
1076a800098SYoshinobu Inoue SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
1086a800098SYoshinobu Inoue     &ip_keepfaith,	0,
1096a800098SYoshinobu Inoue     "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
1106a800098SYoshinobu Inoue 
111df285b3dSMike Silbersack static int	ip_sendsourcequench = 0;
112df285b3dSMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW,
113df285b3dSMike Silbersack     &ip_sendsourcequench, 0,
114df285b3dSMike Silbersack     "Enable the transmission of source quench packets");
115df285b3dSMike Silbersack 
1161f44b0a1SDavid Malone int	ip_do_randomid = 0;
1171f44b0a1SDavid Malone SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW,
1181f44b0a1SDavid Malone     &ip_do_randomid, 0,
1191f44b0a1SDavid Malone     "Assign random ip_id values");
1201f44b0a1SDavid Malone 
121823db0e9SDon Lewis /*
122823db0e9SDon Lewis  * XXX - Setting ip_checkinterface mostly implements the receive side of
123823db0e9SDon Lewis  * the Strong ES model described in RFC 1122, but since the routing table
124a8f12100SDon Lewis  * and transmit implementation do not implement the Strong ES model,
125823db0e9SDon Lewis  * setting this to 1 results in an odd hybrid.
1263f67c834SDon Lewis  *
127a8f12100SDon Lewis  * XXX - ip_checkinterface currently must be disabled if you use ipnat
128a8f12100SDon Lewis  * to translate the destination address to another local interface.
1293f67c834SDon Lewis  *
1303f67c834SDon Lewis  * XXX - ip_checkinterface must be disabled if you add IP aliases
1313f67c834SDon Lewis  * to the loopback interface instead of the interface where the
1323f67c834SDon Lewis  * packets for those addresses are received.
133823db0e9SDon Lewis  */
1344bc37f98SMaxim Konovalov static int	ip_checkinterface = 0;
135b3e95d4eSJonathan Lemon SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
136b3e95d4eSJonathan Lemon     &ip_checkinterface, 0, "Verify packet arrives on correct interface");
137b3e95d4eSJonathan Lemon 
138c21fd232SAndre Oppermann struct pfil_head inet_pfil_hook;	/* Packet filter hooks */
139df8bae1dSRodney W. Grimes 
1401cafed39SJonathan Lemon static struct	ifqueue ipintrq;
141ca925d9cSJonathan Lemon static int	ipqmaxlen = IFQ_MAXLEN;
142ca925d9cSJonathan Lemon 
143df8bae1dSRodney W. Grimes extern	struct domain inetdomain;
144f0ffb944SJulian Elischer extern	struct protosw inetsw[];
145df8bae1dSRodney W. Grimes u_char	ip_protox[IPPROTO_MAX];
14659562606SGarrett Wollman struct	in_ifaddrhead in_ifaddrhead; 		/* first inet address */
147ca925d9cSJonathan Lemon struct	in_ifaddrhashhead *in_ifaddrhashtbl;	/* inet addr hash table  */
148ca925d9cSJonathan Lemon u_long 	in_ifaddrhmask;				/* mask for hash table */
149ca925d9cSJonathan Lemon 
150afed1375SDavid Greenman SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
1513d177f46SBill Fumerola     &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
1520312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
1536489fe65SAndre Oppermann     &ipintrq.ifq_drops, 0,
1546489fe65SAndre Oppermann     "Number of packets dropped from the IP input queue");
155df8bae1dSRodney W. Grimes 
156f23b4c91SGarrett Wollman struct ipstat ipstat;
157c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
1583d177f46SBill Fumerola     &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
159194a213eSAndrey A. Chernov 
160d248c7d7SRobert Watson /*
161d248c7d7SRobert Watson  * IP datagram reassembly.
162d248c7d7SRobert Watson  */
163194a213eSAndrey A. Chernov #define IPREASS_NHASH_LOG2      6
164194a213eSAndrey A. Chernov #define IPREASS_NHASH           (1 << IPREASS_NHASH_LOG2)
165194a213eSAndrey A. Chernov #define IPREASS_HMASK           (IPREASS_NHASH - 1)
166194a213eSAndrey A. Chernov #define IPREASS_HASH(x,y) \
167831a80b0SMatthew Dillon 	(((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
168194a213eSAndrey A. Chernov 
169d248c7d7SRobert Watson static uma_zone_t ipq_zone;
170462b86feSPoul-Henning Kamp static TAILQ_HEAD(ipqhead, ipq) ipq[IPREASS_NHASH];
171dfa60d93SRobert Watson static struct mtx ipqlock;
1722fad1e93SSam Leffler 
1732fad1e93SSam Leffler #define	IPQ_LOCK()	mtx_lock(&ipqlock)
1742fad1e93SSam Leffler #define	IPQ_UNLOCK()	mtx_unlock(&ipqlock)
175888c2a3cSSam Leffler #define	IPQ_LOCK_INIT()	mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
176888c2a3cSSam Leffler #define	IPQ_LOCK_ASSERT()	mtx_assert(&ipqlock, MA_OWNED)
177f23b4c91SGarrett Wollman 
178d248c7d7SRobert Watson static void	maxnipq_update(void);
1794f590175SPaul Saab static void	ipq_zone_change(void *);
180d248c7d7SRobert Watson 
181d248c7d7SRobert Watson static int	maxnipq;	/* Administrative limit on # reass queues. */
182d248c7d7SRobert Watson static int	nipq = 0;	/* Total # of reass queues */
1836489fe65SAndre Oppermann SYSCTL_INT(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_RD,
1846489fe65SAndre Oppermann     &nipq, 0, "Current number of IPv4 fragment reassembly queue entries");
185d248c7d7SRobert Watson 
186d248c7d7SRobert Watson static int	maxfragsperpacket;
187d248c7d7SRobert Watson SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW,
188d248c7d7SRobert Watson     &maxfragsperpacket, 0,
189d248c7d7SRobert Watson     "Maximum number of IPv4 fragments allowed per packet");
190d248c7d7SRobert Watson 
191d248c7d7SRobert Watson struct callout	ipport_tick_callout;
192d248c7d7SRobert Watson 
1930312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU
1940312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
1953d177f46SBill Fumerola     &ip_mtu, 0, "Default MTU");
1960312fbe9SPoul-Henning Kamp #endif
1970312fbe9SPoul-Henning Kamp 
1981b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
199c76ff708SAndre Oppermann int	ipstealth = 0;
2001b968362SDag-Erling Smørgrav SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
2016489fe65SAndre Oppermann     &ipstealth, 0, "IP stealth mode, no TTL decrementation on forwarding");
2021b968362SDag-Erling Smørgrav #endif
2031b968362SDag-Erling Smørgrav 
204010b65f5SJulian Elischer /*
205010b65f5SJulian Elischer  * ipfw_ether and ipfw_bridge hooks.
206010b65f5SJulian Elischer  * XXX: Temporary until those are converted to pfil_hooks as well.
207010b65f5SJulian Elischer  */
208010b65f5SJulian Elischer ip_fw_chk_t *ip_fw_chk_ptr = NULL;
209010b65f5SJulian Elischer ip_dn_io_t *ip_dn_io_ptr = NULL;
210010b65f5SJulian Elischer int fw_one_pass = 1;
211010b65f5SJulian Elischer 
2124d77a549SAlfred Perlstein static void	ip_freef(struct ipqhead *, struct ipq *);
2138948e4baSArchie Cobbs 
214df8bae1dSRodney W. Grimes /*
215df8bae1dSRodney W. Grimes  * IP initialization: fill in IP protocol switch table.
216df8bae1dSRodney W. Grimes  * All protocols not implemented in kernel go to raw IP protocol handler.
217df8bae1dSRodney W. Grimes  */
218df8bae1dSRodney W. Grimes void
219f2565d68SRobert Watson ip_init(void)
220df8bae1dSRodney W. Grimes {
221f2565d68SRobert Watson 	struct protosw *pr;
222f2565d68SRobert Watson 	int i;
223df8bae1dSRodney W. Grimes 
224603724d3SBjoern A. Zeeb 	TAILQ_INIT(&V_in_ifaddrhead);
225603724d3SBjoern A. Zeeb 	V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
226f0ffb944SJulian Elischer 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
22702410549SRobert Watson 	if (pr == NULL)
228db09bef3SAndre Oppermann 		panic("ip_init: PF_INET not found");
229db09bef3SAndre Oppermann 
230db09bef3SAndre Oppermann 	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
231df8bae1dSRodney W. Grimes 	for (i = 0; i < IPPROTO_MAX; i++)
232df8bae1dSRodney W. Grimes 		ip_protox[i] = pr - inetsw;
233db09bef3SAndre Oppermann 	/*
234db09bef3SAndre Oppermann 	 * Cycle through IP protocols and put them into the appropriate place
235db09bef3SAndre Oppermann 	 * in ip_protox[].
236db09bef3SAndre Oppermann 	 */
237f0ffb944SJulian Elischer 	for (pr = inetdomain.dom_protosw;
238f0ffb944SJulian Elischer 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
239df8bae1dSRodney W. Grimes 		if (pr->pr_domain->dom_family == PF_INET &&
240db09bef3SAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
241db09bef3SAndre Oppermann 			/* Be careful to only index valid IP protocols. */
242db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX)
243df8bae1dSRodney W. Grimes 				ip_protox[pr->pr_protocol] = pr - inetsw;
244db09bef3SAndre Oppermann 		}
245194a213eSAndrey A. Chernov 
246c21fd232SAndre Oppermann 	/* Initialize packet filter hooks. */
247134ea224SSam Leffler 	inet_pfil_hook.ph_type = PFIL_TYPE_AF;
248134ea224SSam Leffler 	inet_pfil_hook.ph_af = AF_INET;
249134ea224SSam Leffler 	if ((i = pfil_head_register(&inet_pfil_hook)) != 0)
250134ea224SSam Leffler 		printf("%s: WARNING: unable to register pfil hook, "
251134ea224SSam Leffler 			"error %d\n", __func__, i);
252134ea224SSam Leffler 
253db09bef3SAndre Oppermann 	/* Initialize IP reassembly queue. */
2542fad1e93SSam Leffler 	IPQ_LOCK_INIT();
255194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++)
256603724d3SBjoern A. Zeeb 	    TAILQ_INIT(&V_ipq[i]);
257603724d3SBjoern A. Zeeb 	V_maxnipq = nmbclusters / 32;
258603724d3SBjoern A. Zeeb 	V_maxfragsperpacket = 16;
259603724d3SBjoern A. Zeeb 	V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
260d248c7d7SRobert Watson 	    NULL, UMA_ALIGN_PTR, 0);
261d248c7d7SRobert Watson 	maxnipq_update();
262194a213eSAndrey A. Chernov 
2635f311da2SMike Silbersack 	/* Start ipport_tick. */
2645f311da2SMike Silbersack 	callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
2655f311da2SMike Silbersack 	ipport_tick(NULL);
2665f311da2SMike Silbersack 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2675f311da2SMike Silbersack 		SHUTDOWN_PRI_DEFAULT);
2684f590175SPaul Saab 	EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
2694f590175SPaul Saab 		NULL, EVENTHANDLER_PRI_ANY);
2705f311da2SMike Silbersack 
271db09bef3SAndre Oppermann 	/* Initialize various other remaining things. */
272b53c8130SJulian Elischer 	V_ip_id = time_second & 0xffff;
273df8bae1dSRodney W. Grimes 	ipintrq.ifq_maxlen = ipqmaxlen;
2746008862bSJohn Baldwin 	mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF);
27559dd72d0SRobert Watson 	netisr_register(NETISR_IP, ip_input, &ipintrq, 0);
276df8bae1dSRodney W. Grimes }
277df8bae1dSRodney W. Grimes 
278f2565d68SRobert Watson void
279f2565d68SRobert Watson ip_fini(void *xtp)
2805f311da2SMike Silbersack {
281f2565d68SRobert Watson 
2825f311da2SMike Silbersack 	callout_stop(&ipport_tick_callout);
2835f311da2SMike Silbersack }
2845f311da2SMike Silbersack 
2854d2e3692SLuigi Rizzo /*
286df8bae1dSRodney W. Grimes  * Ip input routine.  Checksum and byte swap header.  If fragmented
287df8bae1dSRodney W. Grimes  * try to reassemble.  Process options.  Pass to next level.
288df8bae1dSRodney W. Grimes  */
289c67b1d17SGarrett Wollman void
290c67b1d17SGarrett Wollman ip_input(struct mbuf *m)
291df8bae1dSRodney W. Grimes {
2929188b4a1SAndre Oppermann 	struct ip *ip = NULL;
2935da9f8faSJosef Karthauser 	struct in_ifaddr *ia = NULL;
294ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
2959b932e9eSAndre Oppermann 	int    checkif, hlen = 0;
29647c861ecSBrian Somers 	u_short sum;
29702c1c707SAndre Oppermann 	int dchg = 0;				/* dest changed after fw */
298f51f805fSSam Leffler 	struct in_addr odst;			/* original dst address */
299b715f178SLuigi Rizzo 
300fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
301db40007dSAndrew R. Reiter 
302ac9d7e26SMax Laier 	if (m->m_flags & M_FASTFWD_OURS) {
3039b932e9eSAndre Oppermann 		/*
30476ff6dcfSAndre Oppermann 		 * Firewall or NAT changed destination to local.
30576ff6dcfSAndre Oppermann 		 * We expect ip_len and ip_off to be in host byte order.
3069b932e9eSAndre Oppermann 		 */
30776ff6dcfSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
30876ff6dcfSAndre Oppermann 		/* Set up some basics that will be used later. */
3092b25acc1SLuigi Rizzo 		ip = mtod(m, struct ip *);
31053be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
3119b932e9eSAndre Oppermann 		goto ours;
3122b25acc1SLuigi Rizzo 	}
3132b25acc1SLuigi Rizzo 
314603724d3SBjoern A. Zeeb 	V_ipstat.ips_total++;
31558938916SGarrett Wollman 
31658938916SGarrett Wollman 	if (m->m_pkthdr.len < sizeof(struct ip))
31758938916SGarrett Wollman 		goto tooshort;
31858938916SGarrett Wollman 
319df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct ip) &&
3200b17fba7SAndre Oppermann 	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
321603724d3SBjoern A. Zeeb 		V_ipstat.ips_toosmall++;
322c67b1d17SGarrett Wollman 		return;
323df8bae1dSRodney W. Grimes 	}
324df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
32558938916SGarrett Wollman 
32653be11f6SPoul-Henning Kamp 	if (ip->ip_v != IPVERSION) {
327603724d3SBjoern A. Zeeb 		V_ipstat.ips_badvers++;
328df8bae1dSRodney W. Grimes 		goto bad;
329df8bae1dSRodney W. Grimes 	}
33058938916SGarrett Wollman 
33153be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
332df8bae1dSRodney W. Grimes 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
333603724d3SBjoern A. Zeeb 		V_ipstat.ips_badhlen++;
334df8bae1dSRodney W. Grimes 		goto bad;
335df8bae1dSRodney W. Grimes 	}
336df8bae1dSRodney W. Grimes 	if (hlen > m->m_len) {
3370b17fba7SAndre Oppermann 		if ((m = m_pullup(m, hlen)) == NULL) {
338603724d3SBjoern A. Zeeb 			V_ipstat.ips_badhlen++;
339c67b1d17SGarrett Wollman 			return;
340df8bae1dSRodney W. Grimes 		}
341df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
342df8bae1dSRodney W. Grimes 	}
34333841545SHajimu UMEMOTO 
34433841545SHajimu UMEMOTO 	/* 127/8 must not appear on wire - RFC1122 */
34533841545SHajimu UMEMOTO 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
34633841545SHajimu UMEMOTO 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
34733841545SHajimu UMEMOTO 		if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
348603724d3SBjoern A. Zeeb 			V_ipstat.ips_badaddr++;
34933841545SHajimu UMEMOTO 			goto bad;
35033841545SHajimu UMEMOTO 		}
35133841545SHajimu UMEMOTO 	}
35233841545SHajimu UMEMOTO 
353db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
354db4f9cc7SJonathan Lemon 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
355db4f9cc7SJonathan Lemon 	} else {
35658938916SGarrett Wollman 		if (hlen == sizeof(struct ip)) {
35747c861ecSBrian Somers 			sum = in_cksum_hdr(ip);
35858938916SGarrett Wollman 		} else {
35947c861ecSBrian Somers 			sum = in_cksum(m, hlen);
36058938916SGarrett Wollman 		}
361db4f9cc7SJonathan Lemon 	}
36247c861ecSBrian Somers 	if (sum) {
363603724d3SBjoern A. Zeeb 		V_ipstat.ips_badsum++;
364df8bae1dSRodney W. Grimes 		goto bad;
365df8bae1dSRodney W. Grimes 	}
366df8bae1dSRodney W. Grimes 
36702b199f1SMax Laier #ifdef ALTQ
36802b199f1SMax Laier 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
36902b199f1SMax Laier 		/* packet is dropped by traffic conditioner */
37002b199f1SMax Laier 		return;
37102b199f1SMax Laier #endif
37202b199f1SMax Laier 
373df8bae1dSRodney W. Grimes 	/*
374df8bae1dSRodney W. Grimes 	 * Convert fields to host representation.
375df8bae1dSRodney W. Grimes 	 */
376fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
377df8bae1dSRodney W. Grimes 	if (ip->ip_len < hlen) {
378603724d3SBjoern A. Zeeb 		V_ipstat.ips_badlen++;
379df8bae1dSRodney W. Grimes 		goto bad;
380df8bae1dSRodney W. Grimes 	}
381fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
382df8bae1dSRodney W. Grimes 
383df8bae1dSRodney W. Grimes 	/*
384df8bae1dSRodney W. Grimes 	 * Check that the amount of data in the buffers
385df8bae1dSRodney W. Grimes 	 * is as at least much as the IP header would have us expect.
386df8bae1dSRodney W. Grimes 	 * Trim mbufs if longer than we expect.
387df8bae1dSRodney W. Grimes 	 * Drop packet if shorter than we expect.
388df8bae1dSRodney W. Grimes 	 */
389df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len < ip->ip_len) {
39058938916SGarrett Wollman tooshort:
391603724d3SBjoern A. Zeeb 		V_ipstat.ips_tooshort++;
392df8bae1dSRodney W. Grimes 		goto bad;
393df8bae1dSRodney W. Grimes 	}
394df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len > ip->ip_len) {
395df8bae1dSRodney W. Grimes 		if (m->m_len == m->m_pkthdr.len) {
396df8bae1dSRodney W. Grimes 			m->m_len = ip->ip_len;
397df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = ip->ip_len;
398df8bae1dSRodney W. Grimes 		} else
399df8bae1dSRodney W. Grimes 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
400df8bae1dSRodney W. Grimes 	}
401b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
40214dd6717SSam Leffler 	/*
40314dd6717SSam Leffler 	 * Bypass packet filtering for packets from a tunnel (gif).
40414dd6717SSam Leffler 	 */
405cc977adcSBjoern A. Zeeb 	if (ip_ipsec_filtertunnel(m))
406c21fd232SAndre Oppermann 		goto passin;
407b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
4083f67c834SDon Lewis 
409c4ac87eaSDarren Reed 	/*
410134ea224SSam Leffler 	 * Run through list of hooks for input packets.
411f51f805fSSam Leffler 	 *
412f51f805fSSam Leffler 	 * NB: Beware of the destination address changing (e.g.
413f51f805fSSam Leffler 	 *     by NAT rewriting).  When this happens, tell
414f51f805fSSam Leffler 	 *     ip_forward to do the right thing.
415c4ac87eaSDarren Reed 	 */
416c21fd232SAndre Oppermann 
417c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
418604afec4SChristian S.J. Peron 	if (!PFIL_HOOKED(&inet_pfil_hook))
419c21fd232SAndre Oppermann 		goto passin;
420c21fd232SAndre Oppermann 
421f51f805fSSam Leffler 	odst = ip->ip_dst;
422134ea224SSam Leffler 	if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
423d6a8d588SMax Laier 	    PFIL_IN, NULL) != 0)
424beec8214SDarren Reed 		return;
425134ea224SSam Leffler 	if (m == NULL)			/* consumed by filter */
426c4ac87eaSDarren Reed 		return;
4279b932e9eSAndre Oppermann 
428c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
42902c1c707SAndre Oppermann 	dchg = (odst.s_addr != ip->ip_dst.s_addr);
4309b932e9eSAndre Oppermann 
4319b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
4329b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
4339b932e9eSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
4349b932e9eSAndre Oppermann 		goto ours;
4359b932e9eSAndre Oppermann 	}
436099dd043SAndre Oppermann 	if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) {
437099dd043SAndre Oppermann 		/*
438099dd043SAndre Oppermann 		 * Directly ship on the packet.  This allows to forward packets
439099dd043SAndre Oppermann 		 * that were destined for us to some other directly connected
440099dd043SAndre Oppermann 		 * host.
441099dd043SAndre Oppermann 		 */
442099dd043SAndre Oppermann 		ip_forward(m, dchg);
443099dd043SAndre Oppermann 		return;
444099dd043SAndre Oppermann 	}
4459b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
4469b932e9eSAndre Oppermann 
447c21fd232SAndre Oppermann passin:
448df8bae1dSRodney W. Grimes 	/*
449df8bae1dSRodney W. Grimes 	 * Process options and, if not destined for us,
450df8bae1dSRodney W. Grimes 	 * ship it on.  ip_dooptions returns 1 when an
451df8bae1dSRodney W. Grimes 	 * error was detected (causing an icmp message
452df8bae1dSRodney W. Grimes 	 * to be sent and the original packet to be freed).
453df8bae1dSRodney W. Grimes 	 */
4549b932e9eSAndre Oppermann 	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
455c67b1d17SGarrett Wollman 		return;
456df8bae1dSRodney W. Grimes 
457f0068c4aSGarrett Wollman         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
458f0068c4aSGarrett Wollman          * matter if it is destined to another node, or whether it is
459f0068c4aSGarrett Wollman          * a multicast one, RSVP wants it! and prevents it from being forwarded
460f0068c4aSGarrett Wollman          * anywhere else. Also checks if the rsvp daemon is running before
461f0068c4aSGarrett Wollman 	 * grabbing the packet.
462f0068c4aSGarrett Wollman          */
463603724d3SBjoern A. Zeeb 	if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP)
464f0068c4aSGarrett Wollman 		goto ours;
465f0068c4aSGarrett Wollman 
466df8bae1dSRodney W. Grimes 	/*
467df8bae1dSRodney W. Grimes 	 * Check our list of addresses, to see if the packet is for us.
468cc766e04SGarrett Wollman 	 * If we don't have any addresses, assume any unicast packet
469cc766e04SGarrett Wollman 	 * we receive might be for us (and let the upper layers deal
470cc766e04SGarrett Wollman 	 * with it).
471df8bae1dSRodney W. Grimes 	 */
472603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
473cc766e04SGarrett Wollman 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
474cc766e04SGarrett Wollman 		goto ours;
475cc766e04SGarrett Wollman 
4767538a9a0SJonathan Lemon 	/*
477823db0e9SDon Lewis 	 * Enable a consistency check between the destination address
478823db0e9SDon Lewis 	 * and the arrival interface for a unicast packet (the RFC 1122
479823db0e9SDon Lewis 	 * strong ES model) if IP forwarding is disabled and the packet
480e15ae1b2SDon Lewis 	 * is not locally generated and the packet is not subject to
481e15ae1b2SDon Lewis 	 * 'ipfw fwd'.
4823f67c834SDon Lewis 	 *
4833f67c834SDon Lewis 	 * XXX - Checking also should be disabled if the destination
4843f67c834SDon Lewis 	 * address is ipnat'ed to a different interface.
4853f67c834SDon Lewis 	 *
486a8f12100SDon Lewis 	 * XXX - Checking is incompatible with IP aliases added
4873f67c834SDon Lewis 	 * to the loopback interface instead of the interface where
4883f67c834SDon Lewis 	 * the packets are received.
489a9771948SGleb Smirnoff 	 *
490a9771948SGleb Smirnoff 	 * XXX - This is the case for carp vhost IPs as well so we
491a9771948SGleb Smirnoff 	 * insert a workaround. If the packet got here, we already
492a9771948SGleb Smirnoff 	 * checked with carp_iamatch() and carp_forus().
493823db0e9SDon Lewis 	 */
494603724d3SBjoern A. Zeeb 	checkif = V_ip_checkinterface && (V_ipforwarding == 0) &&
4959494d596SBrooks Davis 	    m->m_pkthdr.rcvif != NULL &&
496e15ae1b2SDon Lewis 	    ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
497a9771948SGleb Smirnoff #ifdef DEV_CARP
498a9771948SGleb Smirnoff 	    !m->m_pkthdr.rcvif->if_carp &&
499a9771948SGleb Smirnoff #endif
5009b932e9eSAndre Oppermann 	    (dchg == 0);
501823db0e9SDon Lewis 
502ca925d9cSJonathan Lemon 	/*
503ca925d9cSJonathan Lemon 	 * Check for exact addresses in the hash bucket.
504ca925d9cSJonathan Lemon 	 */
5059b932e9eSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
506f9e354dfSJulian Elischer 		/*
507823db0e9SDon Lewis 		 * If the address matches, verify that the packet
508823db0e9SDon Lewis 		 * arrived via the correct interface if checking is
509823db0e9SDon Lewis 		 * enabled.
510f9e354dfSJulian Elischer 		 */
5119b932e9eSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
512823db0e9SDon Lewis 		    (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
513ed1ff184SJulian Elischer 			goto ours;
514ca925d9cSJonathan Lemon 	}
515823db0e9SDon Lewis 	/*
516ca925d9cSJonathan Lemon 	 * Check for broadcast addresses.
517ca925d9cSJonathan Lemon 	 *
518ca925d9cSJonathan Lemon 	 * Only accept broadcast packets that arrive via the matching
519ca925d9cSJonathan Lemon 	 * interface.  Reception of forwarded directed broadcasts would
520ca925d9cSJonathan Lemon 	 * be handled via ip_forward() and ether_output() with the loopback
521ca925d9cSJonathan Lemon 	 * into the stack for SIMPLEX interfaces handled by ether_output().
522823db0e9SDon Lewis 	 */
5234f450ff9SBruce M Simpson 	if (m->m_pkthdr.rcvif != NULL &&
5244f450ff9SBruce M Simpson 	    m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
525ca925d9cSJonathan Lemon 	        TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
526ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
527ca925d9cSJonathan Lemon 				continue;
528ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
529df8bae1dSRodney W. Grimes 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
5309b932e9eSAndre Oppermann 			    ip->ip_dst.s_addr)
531df8bae1dSRodney W. Grimes 				goto ours;
5329b932e9eSAndre Oppermann 			if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr)
533df8bae1dSRodney W. Grimes 				goto ours;
5340ac40133SBrian Somers #ifdef BOOTP_COMPAT
535ca925d9cSJonathan Lemon 			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
536ca925d9cSJonathan Lemon 				goto ours;
5370ac40133SBrian Somers #endif
538df8bae1dSRodney W. Grimes 		}
539df8bae1dSRodney W. Grimes 	}
540f8429ca2SBruce M Simpson 	/* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
541f8429ca2SBruce M Simpson 	if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
542603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantforward++;
543f8429ca2SBruce M Simpson 		m_freem(m);
544f8429ca2SBruce M Simpson 		return;
545f8429ca2SBruce M Simpson 	}
546df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
547df8bae1dSRodney W. Grimes 		struct in_multi *inm;
548603724d3SBjoern A. Zeeb 		if (V_ip_mrouter) {
549df8bae1dSRodney W. Grimes 			/*
550df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, all
551df8bae1dSRodney W. Grimes 			 * incoming multicast packets are passed to the
552df8bae1dSRodney W. Grimes 			 * kernel-level multicast forwarding function.
553df8bae1dSRodney W. Grimes 			 * The packet is returned (relatively) intact; if
554df8bae1dSRodney W. Grimes 			 * ip_mforward() returns a non-zero value, the packet
555df8bae1dSRodney W. Grimes 			 * must be discarded, else it may be accepted below.
556df8bae1dSRodney W. Grimes 			 */
557bbb4330bSLuigi Rizzo 			if (ip_mforward &&
558bbb4330bSLuigi Rizzo 			    ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
559603724d3SBjoern A. Zeeb 				V_ipstat.ips_cantforward++;
560df8bae1dSRodney W. Grimes 				m_freem(m);
561c67b1d17SGarrett Wollman 				return;
562df8bae1dSRodney W. Grimes 			}
563df8bae1dSRodney W. Grimes 
564df8bae1dSRodney W. Grimes 			/*
56511612afaSDima Dorfman 			 * The process-level routing daemon needs to receive
566df8bae1dSRodney W. Grimes 			 * all multicast IGMP packets, whether or not this
567df8bae1dSRodney W. Grimes 			 * host belongs to their destination groups.
568df8bae1dSRodney W. Grimes 			 */
569df8bae1dSRodney W. Grimes 			if (ip->ip_p == IPPROTO_IGMP)
570df8bae1dSRodney W. Grimes 				goto ours;
571603724d3SBjoern A. Zeeb 			V_ipstat.ips_forward++;
572df8bae1dSRodney W. Grimes 		}
573df8bae1dSRodney W. Grimes 		/*
574df8bae1dSRodney W. Grimes 		 * See if we belong to the destination multicast group on the
575df8bae1dSRodney W. Grimes 		 * arrival interface.
576df8bae1dSRodney W. Grimes 		 */
577dd5a318bSRobert Watson 		IN_MULTI_LOCK();
578df8bae1dSRodney W. Grimes 		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
579dd5a318bSRobert Watson 		IN_MULTI_UNLOCK();
580df8bae1dSRodney W. Grimes 		if (inm == NULL) {
581603724d3SBjoern A. Zeeb 			V_ipstat.ips_notmember++;
582df8bae1dSRodney W. Grimes 			m_freem(m);
583c67b1d17SGarrett Wollman 			return;
584df8bae1dSRodney W. Grimes 		}
585df8bae1dSRodney W. Grimes 		goto ours;
586df8bae1dSRodney W. Grimes 	}
587df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
588df8bae1dSRodney W. Grimes 		goto ours;
589df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == INADDR_ANY)
590df8bae1dSRodney W. Grimes 		goto ours;
591df8bae1dSRodney W. Grimes 
5926a800098SYoshinobu Inoue 	/*
5936a800098SYoshinobu Inoue 	 * FAITH(Firewall Aided Internet Translator)
5946a800098SYoshinobu Inoue 	 */
5956a800098SYoshinobu Inoue 	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
596603724d3SBjoern A. Zeeb 		if (V_ip_keepfaith) {
5976a800098SYoshinobu Inoue 			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
5986a800098SYoshinobu Inoue 				goto ours;
5996a800098SYoshinobu Inoue 		}
6006a800098SYoshinobu Inoue 		m_freem(m);
6016a800098SYoshinobu Inoue 		return;
6026a800098SYoshinobu Inoue 	}
6039494d596SBrooks Davis 
604df8bae1dSRodney W. Grimes 	/*
605df8bae1dSRodney W. Grimes 	 * Not for us; forward if possible and desirable.
606df8bae1dSRodney W. Grimes 	 */
607603724d3SBjoern A. Zeeb 	if (V_ipforwarding == 0) {
608603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantforward++;
609df8bae1dSRodney W. Grimes 		m_freem(m);
610546f251bSChris D. Faulhaber 	} else {
611b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
6121dfcf0d2SAndre Oppermann 		if (ip_ipsec_fwd(m))
613546f251bSChris D. Faulhaber 			goto bad;
614b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
6159b932e9eSAndre Oppermann 		ip_forward(m, dchg);
616546f251bSChris D. Faulhaber 	}
617c67b1d17SGarrett Wollman 	return;
618df8bae1dSRodney W. Grimes 
619df8bae1dSRodney W. Grimes ours:
620d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH
621d0ebc0d2SYaroslav Tykhiy 	/*
622d0ebc0d2SYaroslav Tykhiy 	 * IPSTEALTH: Process non-routing options only
623d0ebc0d2SYaroslav Tykhiy 	 * if the packet is destined for us.
624d0ebc0d2SYaroslav Tykhiy 	 */
625603724d3SBjoern A. Zeeb 	if (V_ipstealth && hlen > sizeof (struct ip) &&
6269b932e9eSAndre Oppermann 	    ip_dooptions(m, 1))
627d0ebc0d2SYaroslav Tykhiy 		return;
628d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */
629d0ebc0d2SYaroslav Tykhiy 
6305da9f8faSJosef Karthauser 	/* Count the packet in the ip address stats */
6315da9f8faSJosef Karthauser 	if (ia != NULL) {
6325da9f8faSJosef Karthauser 		ia->ia_ifa.if_ipackets++;
6335da9f8faSJosef Karthauser 		ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
6345da9f8faSJosef Karthauser 	}
635100ba1a6SJordan K. Hubbard 
63663f8d699SJordan K. Hubbard 	/*
637b6ea1aa5SRuslan Ermilov 	 * Attempt reassembly; if it succeeds, proceed.
638ac9d7e26SMax Laier 	 * ip_reass() will return a different mbuf.
639df8bae1dSRodney W. Grimes 	 */
640f0cada84SAndre Oppermann 	if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
641f0cada84SAndre Oppermann 		m = ip_reass(m);
642f0cada84SAndre Oppermann 		if (m == NULL)
643c67b1d17SGarrett Wollman 			return;
6446a800098SYoshinobu Inoue 		ip = mtod(m, struct ip *);
6457e2df452SRuslan Ermilov 		/* Get the header length of the reassembled packet */
64653be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
647f0cada84SAndre Oppermann 	}
648f0cada84SAndre Oppermann 
649f0cada84SAndre Oppermann 	/*
650f0cada84SAndre Oppermann 	 * Further protocols expect the packet length to be w/o the
651f0cada84SAndre Oppermann 	 * IP header.
652f0cada84SAndre Oppermann 	 */
653df8bae1dSRodney W. Grimes 	ip->ip_len -= hlen;
654df8bae1dSRodney W. Grimes 
655b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
65633841545SHajimu UMEMOTO 	/*
65733841545SHajimu UMEMOTO 	 * enforce IPsec policy checking if we are seeing last header.
65833841545SHajimu UMEMOTO 	 * note that we do not visit this with protocols with pcb layer
65933841545SHajimu UMEMOTO 	 * code - like udp/tcp/raw ip.
66033841545SHajimu UMEMOTO 	 */
6611dfcf0d2SAndre Oppermann 	if (ip_ipsec_input(m))
66233841545SHajimu UMEMOTO 		goto bad;
663b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
66433841545SHajimu UMEMOTO 
665df8bae1dSRodney W. Grimes 	/*
666df8bae1dSRodney W. Grimes 	 * Switch out to protocol's input routine.
667df8bae1dSRodney W. Grimes 	 */
668603724d3SBjoern A. Zeeb 	V_ipstat.ips_delivered++;
6699b932e9eSAndre Oppermann 
6702b25acc1SLuigi Rizzo 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
671c67b1d17SGarrett Wollman 	return;
672df8bae1dSRodney W. Grimes bad:
673df8bae1dSRodney W. Grimes 	m_freem(m);
674c67b1d17SGarrett Wollman }
675c67b1d17SGarrett Wollman 
676c67b1d17SGarrett Wollman /*
677d248c7d7SRobert Watson  * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
678d248c7d7SRobert Watson  * max has slightly different semantics than the sysctl, for historical
679d248c7d7SRobert Watson  * reasons.
680d248c7d7SRobert Watson  */
681d248c7d7SRobert Watson static void
682d248c7d7SRobert Watson maxnipq_update(void)
683d248c7d7SRobert Watson {
684d248c7d7SRobert Watson 
685d248c7d7SRobert Watson 	/*
686d248c7d7SRobert Watson 	 * -1 for unlimited allocation.
687d248c7d7SRobert Watson 	 */
688603724d3SBjoern A. Zeeb 	if (V_maxnipq < 0)
689603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 0);
690d248c7d7SRobert Watson 	/*
691d248c7d7SRobert Watson 	 * Positive number for specific bound.
692d248c7d7SRobert Watson 	 */
693603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0)
694603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, V_maxnipq);
695d248c7d7SRobert Watson 	/*
696d248c7d7SRobert Watson 	 * Zero specifies no further fragment queue allocation -- set the
697d248c7d7SRobert Watson 	 * bound very low, but rely on implementation elsewhere to actually
698d248c7d7SRobert Watson 	 * prevent allocation and reclaim current queues.
699d248c7d7SRobert Watson 	 */
700603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0)
701603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 1);
702d248c7d7SRobert Watson }
703d248c7d7SRobert Watson 
7044f590175SPaul Saab static void
7054f590175SPaul Saab ipq_zone_change(void *tag)
7064f590175SPaul Saab {
7074f590175SPaul Saab 
708603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) {
709603724d3SBjoern A. Zeeb 		V_maxnipq = nmbclusters / 32;
7104f590175SPaul Saab 		maxnipq_update();
7114f590175SPaul Saab 	}
7124f590175SPaul Saab }
7134f590175SPaul Saab 
714d248c7d7SRobert Watson static int
715d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
716d248c7d7SRobert Watson {
717d248c7d7SRobert Watson 	int error, i;
718d248c7d7SRobert Watson 
719603724d3SBjoern A. Zeeb 	i = V_maxnipq;
720d248c7d7SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
721d248c7d7SRobert Watson 	if (error || !req->newptr)
722d248c7d7SRobert Watson 		return (error);
723d248c7d7SRobert Watson 
724d248c7d7SRobert Watson 	/*
725d248c7d7SRobert Watson 	 * XXXRW: Might be a good idea to sanity check the argument and place
726d248c7d7SRobert Watson 	 * an extreme upper bound.
727d248c7d7SRobert Watson 	 */
728d248c7d7SRobert Watson 	if (i < -1)
729d248c7d7SRobert Watson 		return (EINVAL);
730603724d3SBjoern A. Zeeb 	V_maxnipq = i;
731d248c7d7SRobert Watson 	maxnipq_update();
732d248c7d7SRobert Watson 	return (0);
733d248c7d7SRobert Watson }
734d248c7d7SRobert Watson 
735d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
736d248c7d7SRobert Watson     NULL, 0, sysctl_maxnipq, "I",
737d248c7d7SRobert Watson     "Maximum number of IPv4 fragment reassembly queue entries");
738d248c7d7SRobert Watson 
739d248c7d7SRobert Watson /*
7408948e4baSArchie Cobbs  * Take incoming datagram fragment and try to reassemble it into
741f0cada84SAndre Oppermann  * whole datagram.  If the argument is the first fragment or one
742f0cada84SAndre Oppermann  * in between the function will return NULL and store the mbuf
743f0cada84SAndre Oppermann  * in the fragment chain.  If the argument is the last fragment
744f0cada84SAndre Oppermann  * the packet will be reassembled and the pointer to the new
745f0cada84SAndre Oppermann  * mbuf returned for further processing.  Only m_tags attached
746f0cada84SAndre Oppermann  * to the first packet/fragment are preserved.
747f0cada84SAndre Oppermann  * The IP header is *NOT* adjusted out of iplen.
748df8bae1dSRodney W. Grimes  */
749f0cada84SAndre Oppermann struct mbuf *
750f0cada84SAndre Oppermann ip_reass(struct mbuf *m)
751df8bae1dSRodney W. Grimes {
752f0cada84SAndre Oppermann 	struct ip *ip;
753f0cada84SAndre Oppermann 	struct mbuf *p, *q, *nq, *t;
754f0cada84SAndre Oppermann 	struct ipq *fp = NULL;
755f0cada84SAndre Oppermann 	struct ipqhead *head;
756f0cada84SAndre Oppermann 	int i, hlen, next;
75759dfcba4SHajimu UMEMOTO 	u_int8_t ecn, ecn0;
758f0cada84SAndre Oppermann 	u_short hash;
759df8bae1dSRodney W. Grimes 
760800af1fbSMaxim Konovalov 	/* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
761603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0 || V_maxfragsperpacket == 0) {
762603724d3SBjoern A. Zeeb 		V_ipstat.ips_fragments++;
763603724d3SBjoern A. Zeeb 		V_ipstat.ips_fragdropped++;
7649d804f81SAndre Oppermann 		m_freem(m);
7659d804f81SAndre Oppermann 		return (NULL);
766f0cada84SAndre Oppermann 	}
7672fad1e93SSam Leffler 
768f0cada84SAndre Oppermann 	ip = mtod(m, struct ip *);
769f0cada84SAndre Oppermann 	hlen = ip->ip_hl << 2;
770f0cada84SAndre Oppermann 
771f0cada84SAndre Oppermann 	hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
772603724d3SBjoern A. Zeeb 	head = &V_ipq[hash];
773f0cada84SAndre Oppermann 	IPQ_LOCK();
774f0cada84SAndre Oppermann 
775f0cada84SAndre Oppermann 	/*
776f0cada84SAndre Oppermann 	 * Look for queue of fragments
777f0cada84SAndre Oppermann 	 * of this datagram.
778f0cada84SAndre Oppermann 	 */
779f0cada84SAndre Oppermann 	TAILQ_FOREACH(fp, head, ipq_list)
780f0cada84SAndre Oppermann 		if (ip->ip_id == fp->ipq_id &&
781f0cada84SAndre Oppermann 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
782f0cada84SAndre Oppermann 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
783f0cada84SAndre Oppermann #ifdef MAC
78430d239bcSRobert Watson 		    mac_ipq_match(m, fp) &&
785f0cada84SAndre Oppermann #endif
786f0cada84SAndre Oppermann 		    ip->ip_p == fp->ipq_p)
787f0cada84SAndre Oppermann 			goto found;
788f0cada84SAndre Oppermann 
789f0cada84SAndre Oppermann 	fp = NULL;
790f0cada84SAndre Oppermann 
791f0cada84SAndre Oppermann 	/*
792d248c7d7SRobert Watson 	 * Attempt to trim the number of allocated fragment queues if it
793d248c7d7SRobert Watson 	 * exceeds the administrative limit.
794f0cada84SAndre Oppermann 	 */
795603724d3SBjoern A. Zeeb 	if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) {
796f0cada84SAndre Oppermann 		/*
797f0cada84SAndre Oppermann 		 * drop something from the tail of the current queue
798f0cada84SAndre Oppermann 		 * before proceeding further
799f0cada84SAndre Oppermann 		 */
800f0cada84SAndre Oppermann 		struct ipq *q = TAILQ_LAST(head, ipqhead);
801f0cada84SAndre Oppermann 		if (q == NULL) {   /* gak */
802f0cada84SAndre Oppermann 			for (i = 0; i < IPREASS_NHASH; i++) {
803603724d3SBjoern A. Zeeb 				struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead);
804f0cada84SAndre Oppermann 				if (r) {
805ac957cd2SJulian Elischer 					V_ipstat.ips_fragtimeout +=
806ac957cd2SJulian Elischer 					    r->ipq_nfrags;
807603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], r);
808f0cada84SAndre Oppermann 					break;
809f0cada84SAndre Oppermann 				}
810f0cada84SAndre Oppermann 			}
811f0cada84SAndre Oppermann 		} else {
812603724d3SBjoern A. Zeeb 			V_ipstat.ips_fragtimeout += q->ipq_nfrags;
813f0cada84SAndre Oppermann 			ip_freef(head, q);
814f0cada84SAndre Oppermann 		}
815f0cada84SAndre Oppermann 	}
816f0cada84SAndre Oppermann 
817f0cada84SAndre Oppermann found:
818f0cada84SAndre Oppermann 	/*
819f0cada84SAndre Oppermann 	 * Adjust ip_len to not reflect header,
820f0cada84SAndre Oppermann 	 * convert offset of this to bytes.
821f0cada84SAndre Oppermann 	 */
822f0cada84SAndre Oppermann 	ip->ip_len -= hlen;
823f0cada84SAndre Oppermann 	if (ip->ip_off & IP_MF) {
824f0cada84SAndre Oppermann 		/*
825f0cada84SAndre Oppermann 		 * Make sure that fragments have a data length
826f0cada84SAndre Oppermann 		 * that's a non-zero multiple of 8 bytes.
827f0cada84SAndre Oppermann 		 */
828f0cada84SAndre Oppermann 		if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
829603724d3SBjoern A. Zeeb 			V_ipstat.ips_toosmall++; /* XXX */
830f0cada84SAndre Oppermann 			goto dropfrag;
831f0cada84SAndre Oppermann 		}
832f0cada84SAndre Oppermann 		m->m_flags |= M_FRAG;
833f0cada84SAndre Oppermann 	} else
834f0cada84SAndre Oppermann 		m->m_flags &= ~M_FRAG;
835f0cada84SAndre Oppermann 	ip->ip_off <<= 3;
836f0cada84SAndre Oppermann 
837f0cada84SAndre Oppermann 
838f0cada84SAndre Oppermann 	/*
839f0cada84SAndre Oppermann 	 * Attempt reassembly; if it succeeds, proceed.
840f0cada84SAndre Oppermann 	 * ip_reass() will return a different mbuf.
841f0cada84SAndre Oppermann 	 */
842603724d3SBjoern A. Zeeb 	V_ipstat.ips_fragments++;
843f0cada84SAndre Oppermann 	m->m_pkthdr.header = ip;
844f0cada84SAndre Oppermann 
845f0cada84SAndre Oppermann 	/* Previous ip_reass() started here. */
846df8bae1dSRodney W. Grimes 	/*
847df8bae1dSRodney W. Grimes 	 * Presence of header sizes in mbufs
848df8bae1dSRodney W. Grimes 	 * would confuse code below.
849df8bae1dSRodney W. Grimes 	 */
850df8bae1dSRodney W. Grimes 	m->m_data += hlen;
851df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
852df8bae1dSRodney W. Grimes 
853df8bae1dSRodney W. Grimes 	/*
854df8bae1dSRodney W. Grimes 	 * If first fragment to arrive, create a reassembly queue.
855df8bae1dSRodney W. Grimes 	 */
856042bbfa3SRobert Watson 	if (fp == NULL) {
857603724d3SBjoern A. Zeeb 		fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
858d248c7d7SRobert Watson 		if (fp == NULL)
859df8bae1dSRodney W. Grimes 			goto dropfrag;
86036b0360bSRobert Watson #ifdef MAC
86130d239bcSRobert Watson 		if (mac_ipq_init(fp, M_NOWAIT) != 0) {
862603724d3SBjoern A. Zeeb 			uma_zfree(V_ipq_zone, fp);
8631d7d0bfeSPawel Jakub Dawidek 			fp = NULL;
8645e7ce478SRobert Watson 			goto dropfrag;
8655e7ce478SRobert Watson 		}
86630d239bcSRobert Watson 		mac_ipq_create(m, fp);
86736b0360bSRobert Watson #endif
868462b86feSPoul-Henning Kamp 		TAILQ_INSERT_HEAD(head, fp, ipq_list);
869603724d3SBjoern A. Zeeb 		V_nipq++;
870375386e2SMike Silbersack 		fp->ipq_nfrags = 1;
871df8bae1dSRodney W. Grimes 		fp->ipq_ttl = IPFRAGTTL;
872df8bae1dSRodney W. Grimes 		fp->ipq_p = ip->ip_p;
873df8bae1dSRodney W. Grimes 		fp->ipq_id = ip->ip_id;
8746effc713SDoug Rabson 		fp->ipq_src = ip->ip_src;
8756effc713SDoug Rabson 		fp->ipq_dst = ip->ip_dst;
876af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
877af38c68cSLuigi Rizzo 		m->m_nextpkt = NULL;
878800af1fbSMaxim Konovalov 		goto done;
87936b0360bSRobert Watson 	} else {
880375386e2SMike Silbersack 		fp->ipq_nfrags++;
88136b0360bSRobert Watson #ifdef MAC
88230d239bcSRobert Watson 		mac_ipq_update(m, fp);
88336b0360bSRobert Watson #endif
884df8bae1dSRodney W. Grimes 	}
885df8bae1dSRodney W. Grimes 
8866effc713SDoug Rabson #define GETIP(m)	((struct ip*)((m)->m_pkthdr.header))
8876effc713SDoug Rabson 
888df8bae1dSRodney W. Grimes 	/*
88959dfcba4SHajimu UMEMOTO 	 * Handle ECN by comparing this segment with the first one;
89059dfcba4SHajimu UMEMOTO 	 * if CE is set, do not lose CE.
89159dfcba4SHajimu UMEMOTO 	 * drop if CE and not-ECT are mixed for the same packet.
89259dfcba4SHajimu UMEMOTO 	 */
89359dfcba4SHajimu UMEMOTO 	ecn = ip->ip_tos & IPTOS_ECN_MASK;
89459dfcba4SHajimu UMEMOTO 	ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
89559dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_CE) {
89659dfcba4SHajimu UMEMOTO 		if (ecn0 == IPTOS_ECN_NOTECT)
89759dfcba4SHajimu UMEMOTO 			goto dropfrag;
89859dfcba4SHajimu UMEMOTO 		if (ecn0 != IPTOS_ECN_CE)
89959dfcba4SHajimu UMEMOTO 			GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
90059dfcba4SHajimu UMEMOTO 	}
90159dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
90259dfcba4SHajimu UMEMOTO 		goto dropfrag;
90359dfcba4SHajimu UMEMOTO 
90459dfcba4SHajimu UMEMOTO 	/*
905df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
906df8bae1dSRodney W. Grimes 	 */
9076effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
9086effc713SDoug Rabson 		if (GETIP(q)->ip_off > ip->ip_off)
909df8bae1dSRodney W. Grimes 			break;
910df8bae1dSRodney W. Grimes 
911df8bae1dSRodney W. Grimes 	/*
912df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
913df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
914af38c68cSLuigi Rizzo 	 * segment.  If it provides all of our data, drop us, otherwise
915af38c68cSLuigi Rizzo 	 * stick new segment in the proper place.
916db4f9cc7SJonathan Lemon 	 *
917db4f9cc7SJonathan Lemon 	 * If some of the data is dropped from the the preceding
918db4f9cc7SJonathan Lemon 	 * segment, then it's checksum is invalidated.
919df8bae1dSRodney W. Grimes 	 */
9206effc713SDoug Rabson 	if (p) {
9216effc713SDoug Rabson 		i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
922df8bae1dSRodney W. Grimes 		if (i > 0) {
923df8bae1dSRodney W. Grimes 			if (i >= ip->ip_len)
924df8bae1dSRodney W. Grimes 				goto dropfrag;
9256a800098SYoshinobu Inoue 			m_adj(m, i);
926db4f9cc7SJonathan Lemon 			m->m_pkthdr.csum_flags = 0;
927df8bae1dSRodney W. Grimes 			ip->ip_off += i;
928df8bae1dSRodney W. Grimes 			ip->ip_len -= i;
929df8bae1dSRodney W. Grimes 		}
930af38c68cSLuigi Rizzo 		m->m_nextpkt = p->m_nextpkt;
931af38c68cSLuigi Rizzo 		p->m_nextpkt = m;
932af38c68cSLuigi Rizzo 	} else {
933af38c68cSLuigi Rizzo 		m->m_nextpkt = fp->ipq_frags;
934af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
935df8bae1dSRodney W. Grimes 	}
936df8bae1dSRodney W. Grimes 
937df8bae1dSRodney W. Grimes 	/*
938df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
939df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
940df8bae1dSRodney W. Grimes 	 */
9416effc713SDoug Rabson 	for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
942af38c68cSLuigi Rizzo 	     q = nq) {
943b36f5b37SMaxim Konovalov 		i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
9446effc713SDoug Rabson 		if (i < GETIP(q)->ip_len) {
9456effc713SDoug Rabson 			GETIP(q)->ip_len -= i;
9466effc713SDoug Rabson 			GETIP(q)->ip_off += i;
9476effc713SDoug Rabson 			m_adj(q, i);
948db4f9cc7SJonathan Lemon 			q->m_pkthdr.csum_flags = 0;
949df8bae1dSRodney W. Grimes 			break;
950df8bae1dSRodney W. Grimes 		}
9516effc713SDoug Rabson 		nq = q->m_nextpkt;
952af38c68cSLuigi Rizzo 		m->m_nextpkt = nq;
953603724d3SBjoern A. Zeeb 		V_ipstat.ips_fragdropped++;
954375386e2SMike Silbersack 		fp->ipq_nfrags--;
9556effc713SDoug Rabson 		m_freem(q);
956df8bae1dSRodney W. Grimes 	}
957df8bae1dSRodney W. Grimes 
958df8bae1dSRodney W. Grimes 	/*
959375386e2SMike Silbersack 	 * Check for complete reassembly and perform frag per packet
960375386e2SMike Silbersack 	 * limiting.
961375386e2SMike Silbersack 	 *
962375386e2SMike Silbersack 	 * Frag limiting is performed here so that the nth frag has
963375386e2SMike Silbersack 	 * a chance to complete the packet before we drop the packet.
964375386e2SMike Silbersack 	 * As a result, n+1 frags are actually allowed per packet, but
965375386e2SMike Silbersack 	 * only n will ever be stored. (n = maxfragsperpacket.)
966375386e2SMike Silbersack 	 *
967df8bae1dSRodney W. Grimes 	 */
9686effc713SDoug Rabson 	next = 0;
9696effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
970375386e2SMike Silbersack 		if (GETIP(q)->ip_off != next) {
971603724d3SBjoern A. Zeeb 			if (fp->ipq_nfrags > V_maxfragsperpacket) {
972603724d3SBjoern A. Zeeb 				V_ipstat.ips_fragdropped += fp->ipq_nfrags;
973375386e2SMike Silbersack 				ip_freef(head, fp);
97499e8617dSMaxim Konovalov 			}
975f0cada84SAndre Oppermann 			goto done;
976375386e2SMike Silbersack 		}
9776effc713SDoug Rabson 		next += GETIP(q)->ip_len;
9786effc713SDoug Rabson 	}
9796effc713SDoug Rabson 	/* Make sure the last packet didn't have the IP_MF flag */
980375386e2SMike Silbersack 	if (p->m_flags & M_FRAG) {
981603724d3SBjoern A. Zeeb 		if (fp->ipq_nfrags > V_maxfragsperpacket) {
982603724d3SBjoern A. Zeeb 			V_ipstat.ips_fragdropped += fp->ipq_nfrags;
983375386e2SMike Silbersack 			ip_freef(head, fp);
98499e8617dSMaxim Konovalov 		}
985f0cada84SAndre Oppermann 		goto done;
986375386e2SMike Silbersack 	}
987df8bae1dSRodney W. Grimes 
988df8bae1dSRodney W. Grimes 	/*
989430d30d8SBill Fenner 	 * Reassembly is complete.  Make sure the packet is a sane size.
990430d30d8SBill Fenner 	 */
9916effc713SDoug Rabson 	q = fp->ipq_frags;
9926effc713SDoug Rabson 	ip = GETIP(q);
99353be11f6SPoul-Henning Kamp 	if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
994603724d3SBjoern A. Zeeb 		V_ipstat.ips_toolong++;
995603724d3SBjoern A. Zeeb 		V_ipstat.ips_fragdropped += fp->ipq_nfrags;
996462b86feSPoul-Henning Kamp 		ip_freef(head, fp);
997f0cada84SAndre Oppermann 		goto done;
998430d30d8SBill Fenner 	}
999430d30d8SBill Fenner 
1000430d30d8SBill Fenner 	/*
1001430d30d8SBill Fenner 	 * Concatenate fragments.
1002df8bae1dSRodney W. Grimes 	 */
10036effc713SDoug Rabson 	m = q;
1004df8bae1dSRodney W. Grimes 	t = m->m_next;
100502410549SRobert Watson 	m->m_next = NULL;
1006df8bae1dSRodney W. Grimes 	m_cat(m, t);
10076effc713SDoug Rabson 	nq = q->m_nextpkt;
100802410549SRobert Watson 	q->m_nextpkt = NULL;
10096effc713SDoug Rabson 	for (q = nq; q != NULL; q = nq) {
10106effc713SDoug Rabson 		nq = q->m_nextpkt;
1011945aa40dSDoug Rabson 		q->m_nextpkt = NULL;
1012db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1013db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1014a8db1d93SJonathan Lemon 		m_cat(m, q);
1015df8bae1dSRodney W. Grimes 	}
10166edb555dSOleg Bulyzhin 	/*
10176edb555dSOleg Bulyzhin 	 * In order to do checksumming faster we do 'end-around carry' here
10186edb555dSOleg Bulyzhin 	 * (and not in for{} loop), though it implies we are not going to
10196edb555dSOleg Bulyzhin 	 * reassemble more than 64k fragments.
10206edb555dSOleg Bulyzhin 	 */
10216edb555dSOleg Bulyzhin 	m->m_pkthdr.csum_data =
10226edb555dSOleg Bulyzhin 	    (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16);
102336b0360bSRobert Watson #ifdef MAC
102430d239bcSRobert Watson 	mac_ipq_reassemble(fp, m);
102530d239bcSRobert Watson 	mac_ipq_destroy(fp);
102636b0360bSRobert Watson #endif
1027df8bae1dSRodney W. Grimes 
1028df8bae1dSRodney W. Grimes 	/*
1029f0cada84SAndre Oppermann 	 * Create header for new ip packet by modifying header of first
1030f0cada84SAndre Oppermann 	 * packet;  dequeue and discard fragment reassembly header.
1031df8bae1dSRodney W. Grimes 	 * Make header visible.
1032df8bae1dSRodney W. Grimes 	 */
1033f0cada84SAndre Oppermann 	ip->ip_len = (ip->ip_hl << 2) + next;
10346effc713SDoug Rabson 	ip->ip_src = fp->ipq_src;
10356effc713SDoug Rabson 	ip->ip_dst = fp->ipq_dst;
1036462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(head, fp, ipq_list);
1037603724d3SBjoern A. Zeeb 	V_nipq--;
1038603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
103953be11f6SPoul-Henning Kamp 	m->m_len += (ip->ip_hl << 2);
104053be11f6SPoul-Henning Kamp 	m->m_data -= (ip->ip_hl << 2);
1041df8bae1dSRodney W. Grimes 	/* some debugging cruft by sklower, below, will go away soon */
1042a5554bf0SPoul-Henning Kamp 	if (m->m_flags & M_PKTHDR)	/* XXX this should be done elsewhere */
1043a5554bf0SPoul-Henning Kamp 		m_fixhdr(m);
1044603724d3SBjoern A. Zeeb 	V_ipstat.ips_reassembled++;
1045f0cada84SAndre Oppermann 	IPQ_UNLOCK();
10466a800098SYoshinobu Inoue 	return (m);
1047df8bae1dSRodney W. Grimes 
1048df8bae1dSRodney W. Grimes dropfrag:
1049603724d3SBjoern A. Zeeb 	V_ipstat.ips_fragdropped++;
1050042bbfa3SRobert Watson 	if (fp != NULL)
1051375386e2SMike Silbersack 		fp->ipq_nfrags--;
1052df8bae1dSRodney W. Grimes 	m_freem(m);
1053f0cada84SAndre Oppermann done:
1054f0cada84SAndre Oppermann 	IPQ_UNLOCK();
1055f0cada84SAndre Oppermann 	return (NULL);
10566effc713SDoug Rabson 
10576effc713SDoug Rabson #undef GETIP
1058df8bae1dSRodney W. Grimes }
1059df8bae1dSRodney W. Grimes 
1060df8bae1dSRodney W. Grimes /*
1061df8bae1dSRodney W. Grimes  * Free a fragment reassembly header and all
1062df8bae1dSRodney W. Grimes  * associated datagrams.
1063df8bae1dSRodney W. Grimes  */
10640312fbe9SPoul-Henning Kamp static void
1065f2565d68SRobert Watson ip_freef(struct ipqhead *fhp, struct ipq *fp)
1066df8bae1dSRodney W. Grimes {
1067f2565d68SRobert Watson 	struct mbuf *q;
1068df8bae1dSRodney W. Grimes 
10692fad1e93SSam Leffler 	IPQ_LOCK_ASSERT();
10702fad1e93SSam Leffler 
10716effc713SDoug Rabson 	while (fp->ipq_frags) {
10726effc713SDoug Rabson 		q = fp->ipq_frags;
10736effc713SDoug Rabson 		fp->ipq_frags = q->m_nextpkt;
10746effc713SDoug Rabson 		m_freem(q);
1075df8bae1dSRodney W. Grimes 	}
1076462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(fhp, fp, ipq_list);
1077603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
1078603724d3SBjoern A. Zeeb 	V_nipq--;
1079df8bae1dSRodney W. Grimes }
1080df8bae1dSRodney W. Grimes 
1081df8bae1dSRodney W. Grimes /*
1082df8bae1dSRodney W. Grimes  * IP timer processing;
1083df8bae1dSRodney W. Grimes  * if a timer expires on a reassembly
1084df8bae1dSRodney W. Grimes  * queue, discard it.
1085df8bae1dSRodney W. Grimes  */
1086df8bae1dSRodney W. Grimes void
1087f2565d68SRobert Watson ip_slowtimo(void)
1088df8bae1dSRodney W. Grimes {
1089f2565d68SRobert Watson 	struct ipq *fp;
1090194a213eSAndrey A. Chernov 	int i;
1091df8bae1dSRodney W. Grimes 
10922fad1e93SSam Leffler 	IPQ_LOCK();
1093194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1094603724d3SBjoern A. Zeeb 		for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) {
1095462b86feSPoul-Henning Kamp 			struct ipq *fpp;
1096462b86feSPoul-Henning Kamp 
1097462b86feSPoul-Henning Kamp 			fpp = fp;
1098462b86feSPoul-Henning Kamp 			fp = TAILQ_NEXT(fp, ipq_list);
1099462b86feSPoul-Henning Kamp 			if(--fpp->ipq_ttl == 0) {
1100603724d3SBjoern A. Zeeb 				V_ipstat.ips_fragtimeout += fpp->ipq_nfrags;
1101603724d3SBjoern A. Zeeb 				ip_freef(&V_ipq[i], fpp);
1102df8bae1dSRodney W. Grimes 			}
1103df8bae1dSRodney W. Grimes 		}
1104194a213eSAndrey A. Chernov 	}
1105690a6055SJesper Skriver 	/*
1106690a6055SJesper Skriver 	 * If we are over the maximum number of fragments
1107690a6055SJesper Skriver 	 * (due to the limit being lowered), drain off
1108690a6055SJesper Skriver 	 * enough to get down to the new limit.
1109690a6055SJesper Skriver 	 */
1110603724d3SBjoern A. Zeeb 	if (V_maxnipq >= 0 && V_nipq > V_maxnipq) {
1111690a6055SJesper Skriver 		for (i = 0; i < IPREASS_NHASH; i++) {
1112603724d3SBjoern A. Zeeb 			while (V_nipq > V_maxnipq && !TAILQ_EMPTY(&V_ipq[i])) {
1113603724d3SBjoern A. Zeeb 				V_ipstat.ips_fragdropped +=
1114603724d3SBjoern A. Zeeb 				    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags;
1115603724d3SBjoern A. Zeeb 				ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1116690a6055SJesper Skriver 			}
1117690a6055SJesper Skriver 		}
1118690a6055SJesper Skriver 	}
11192fad1e93SSam Leffler 	IPQ_UNLOCK();
1120df8bae1dSRodney W. Grimes }
1121df8bae1dSRodney W. Grimes 
1122df8bae1dSRodney W. Grimes /*
1123df8bae1dSRodney W. Grimes  * Drain off all datagram fragments.
1124df8bae1dSRodney W. Grimes  */
1125df8bae1dSRodney W. Grimes void
1126f2565d68SRobert Watson ip_drain(void)
1127df8bae1dSRodney W. Grimes {
1128194a213eSAndrey A. Chernov 	int     i;
1129ce29ab3aSGarrett Wollman 
11302fad1e93SSam Leffler 	IPQ_LOCK();
1131194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1132603724d3SBjoern A. Zeeb 		while(!TAILQ_EMPTY(&V_ipq[i])) {
1133603724d3SBjoern A. Zeeb 			V_ipstat.ips_fragdropped +=
1134603724d3SBjoern A. Zeeb 			    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags;
1135603724d3SBjoern A. Zeeb 			ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1136194a213eSAndrey A. Chernov 		}
1137194a213eSAndrey A. Chernov 	}
11382fad1e93SSam Leffler 	IPQ_UNLOCK();
1139ce29ab3aSGarrett Wollman 	in_rtqdrain();
1140df8bae1dSRodney W. Grimes }
1141df8bae1dSRodney W. Grimes 
1142df8bae1dSRodney W. Grimes /*
1143de38924dSAndre Oppermann  * The protocol to be inserted into ip_protox[] must be already registered
1144de38924dSAndre Oppermann  * in inetsw[], either statically or through pf_proto_register().
1145de38924dSAndre Oppermann  */
1146de38924dSAndre Oppermann int
1147de38924dSAndre Oppermann ipproto_register(u_char ipproto)
1148de38924dSAndre Oppermann {
1149de38924dSAndre Oppermann 	struct protosw *pr;
1150de38924dSAndre Oppermann 
1151de38924dSAndre Oppermann 	/* Sanity checks. */
1152de38924dSAndre Oppermann 	if (ipproto == 0)
1153de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1154de38924dSAndre Oppermann 
1155de38924dSAndre Oppermann 	/*
1156de38924dSAndre Oppermann 	 * The protocol slot must not be occupied by another protocol
1157de38924dSAndre Oppermann 	 * already.  An index pointing to IPPROTO_RAW is unused.
1158de38924dSAndre Oppermann 	 */
1159de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1160de38924dSAndre Oppermann 	if (pr == NULL)
1161de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1162de38924dSAndre Oppermann 	if (ip_protox[ipproto] != pr - inetsw)	/* IPPROTO_RAW */
1163de38924dSAndre Oppermann 		return (EEXIST);
1164de38924dSAndre Oppermann 
1165de38924dSAndre Oppermann 	/* Find the protocol position in inetsw[] and set the index. */
1166de38924dSAndre Oppermann 	for (pr = inetdomain.dom_protosw;
1167de38924dSAndre Oppermann 	     pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1168de38924dSAndre Oppermann 		if (pr->pr_domain->dom_family == PF_INET &&
1169de38924dSAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol == ipproto) {
1170de38924dSAndre Oppermann 			/* Be careful to only index valid IP protocols. */
1171db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX) {
1172de38924dSAndre Oppermann 				ip_protox[pr->pr_protocol] = pr - inetsw;
1173de38924dSAndre Oppermann 				return (0);
1174de38924dSAndre Oppermann 			} else
1175de38924dSAndre Oppermann 				return (EINVAL);
1176de38924dSAndre Oppermann 		}
1177de38924dSAndre Oppermann 	}
1178de38924dSAndre Oppermann 	return (EPROTONOSUPPORT);
1179de38924dSAndre Oppermann }
1180de38924dSAndre Oppermann 
1181de38924dSAndre Oppermann int
1182de38924dSAndre Oppermann ipproto_unregister(u_char ipproto)
1183de38924dSAndre Oppermann {
1184de38924dSAndre Oppermann 	struct protosw *pr;
1185de38924dSAndre Oppermann 
1186de38924dSAndre Oppermann 	/* Sanity checks. */
1187de38924dSAndre Oppermann 	if (ipproto == 0)
1188de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1189de38924dSAndre Oppermann 
1190de38924dSAndre Oppermann 	/* Check if the protocol was indeed registered. */
1191de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1192de38924dSAndre Oppermann 	if (pr == NULL)
1193de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1194de38924dSAndre Oppermann 	if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
1195de38924dSAndre Oppermann 		return (ENOENT);
1196de38924dSAndre Oppermann 
1197de38924dSAndre Oppermann 	/* Reset the protocol slot to IPPROTO_RAW. */
1198de38924dSAndre Oppermann 	ip_protox[ipproto] = pr - inetsw;
1199de38924dSAndre Oppermann 	return (0);
1200de38924dSAndre Oppermann }
1201de38924dSAndre Oppermann 
1202df8bae1dSRodney W. Grimes /*
1203df8bae1dSRodney W. Grimes  * Given address of next destination (final or next hop),
1204df8bae1dSRodney W. Grimes  * return internet address info of interface to be used to get there.
1205df8bae1dSRodney W. Grimes  */
1206bd714208SRuslan Ermilov struct in_ifaddr *
12078b07e49aSJulian Elischer ip_rtaddr(struct in_addr dst, u_int fibnum)
1208df8bae1dSRodney W. Grimes {
120997d8d152SAndre Oppermann 	struct route sro;
121002c1c707SAndre Oppermann 	struct sockaddr_in *sin;
121102c1c707SAndre Oppermann 	struct in_ifaddr *ifa;
1212df8bae1dSRodney W. Grimes 
12130cfbbe3bSAndre Oppermann 	bzero(&sro, sizeof(sro));
121497d8d152SAndre Oppermann 	sin = (struct sockaddr_in *)&sro.ro_dst;
1215df8bae1dSRodney W. Grimes 	sin->sin_family = AF_INET;
1216df8bae1dSRodney W. Grimes 	sin->sin_len = sizeof(*sin);
1217df8bae1dSRodney W. Grimes 	sin->sin_addr = dst;
12188b07e49aSJulian Elischer 	in_rtalloc_ign(&sro, RTF_CLONING, fibnum);
1219df8bae1dSRodney W. Grimes 
122097d8d152SAndre Oppermann 	if (sro.ro_rt == NULL)
122102410549SRobert Watson 		return (NULL);
122202c1c707SAndre Oppermann 
122397d8d152SAndre Oppermann 	ifa = ifatoia(sro.ro_rt->rt_ifa);
122497d8d152SAndre Oppermann 	RTFREE(sro.ro_rt);
122502410549SRobert Watson 	return (ifa);
1226df8bae1dSRodney W. Grimes }
1227df8bae1dSRodney W. Grimes 
1228df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = {
1229df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1230df8bae1dSRodney W. Grimes 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1231df8bae1dSRodney W. Grimes 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1232df8bae1dSRodney W. Grimes 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1233fcaf9f91SMike Silbersack 	0,		0,		EHOSTUNREACH,	0,
12343b8123b7SJesper Skriver 	ENOPROTOOPT,	ECONNREFUSED
1235df8bae1dSRodney W. Grimes };
1236df8bae1dSRodney W. Grimes 
1237df8bae1dSRodney W. Grimes /*
1238df8bae1dSRodney W. Grimes  * Forward a packet.  If some error occurs return the sender
1239df8bae1dSRodney W. Grimes  * an icmp packet.  Note we can't always generate a meaningful
1240df8bae1dSRodney W. Grimes  * icmp message because icmp doesn't have a large enough repertoire
1241df8bae1dSRodney W. Grimes  * of codes and types.
1242df8bae1dSRodney W. Grimes  *
1243df8bae1dSRodney W. Grimes  * If not forwarding, just drop the packet.  This could be confusing
1244df8bae1dSRodney W. Grimes  * if ipforwarding was zero but some routing protocol was advancing
1245df8bae1dSRodney W. Grimes  * us as a gateway to somewhere.  However, we must let the routing
1246df8bae1dSRodney W. Grimes  * protocol deal with that.
1247df8bae1dSRodney W. Grimes  *
1248df8bae1dSRodney W. Grimes  * The srcrt parameter indicates whether the packet is being forwarded
1249df8bae1dSRodney W. Grimes  * via a source route.
1250df8bae1dSRodney W. Grimes  */
12519b932e9eSAndre Oppermann void
12529b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt)
1253df8bae1dSRodney W. Grimes {
12542b25acc1SLuigi Rizzo 	struct ip *ip = mtod(m, struct ip *);
12559b932e9eSAndre Oppermann 	struct in_ifaddr *ia = NULL;
1256df8bae1dSRodney W. Grimes 	struct mbuf *mcopy;
12579b932e9eSAndre Oppermann 	struct in_addr dest;
1258b835b6feSBjoern A. Zeeb 	struct route ro;
1259c773494eSAndre Oppermann 	int error, type = 0, code = 0, mtu = 0;
12603efc3014SJulian Elischer 
12619b932e9eSAndre Oppermann 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1262603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantforward++;
1263df8bae1dSRodney W. Grimes 		m_freem(m);
1264df8bae1dSRodney W. Grimes 		return;
1265df8bae1dSRodney W. Grimes 	}
12661b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
1267603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
12681b968362SDag-Erling Smørgrav #endif
1269df8bae1dSRodney W. Grimes 		if (ip->ip_ttl <= IPTTLDEC) {
12701b968362SDag-Erling Smørgrav 			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
127102c1c707SAndre Oppermann 			    0, 0);
1272df8bae1dSRodney W. Grimes 			return;
1273df8bae1dSRodney W. Grimes 		}
12741b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
12751b968362SDag-Erling Smørgrav 	}
12761b968362SDag-Erling Smørgrav #endif
1277df8bae1dSRodney W. Grimes 
12788b07e49aSJulian Elischer 	ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
1279d23d475fSGuido van Rooij 	if (!srcrt && ia == NULL) {
128002c1c707SAndre Oppermann 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1281df8bae1dSRodney W. Grimes 		return;
128202c1c707SAndre Oppermann 	}
1283df8bae1dSRodney W. Grimes 
1284df8bae1dSRodney W. Grimes 	/*
1285bfef7ed4SIan Dowse 	 * Save the IP header and at most 8 bytes of the payload,
1286bfef7ed4SIan Dowse 	 * in case we need to generate an ICMP message to the src.
1287bfef7ed4SIan Dowse 	 *
12884d2e3692SLuigi Rizzo 	 * XXX this can be optimized a lot by saving the data in a local
12894d2e3692SLuigi Rizzo 	 * buffer on the stack (72 bytes at most), and only allocating the
12904d2e3692SLuigi Rizzo 	 * mbuf if really necessary. The vast majority of the packets
12914d2e3692SLuigi Rizzo 	 * are forwarded without having to send an ICMP back (either
12924d2e3692SLuigi Rizzo 	 * because unnecessary, or because rate limited), so we are
12934d2e3692SLuigi Rizzo 	 * really we are wasting a lot of work here.
12944d2e3692SLuigi Rizzo 	 *
1295bfef7ed4SIan Dowse 	 * We don't use m_copy() because it might return a reference
1296bfef7ed4SIan Dowse 	 * to a shared cluster. Both this function and ip_output()
1297bfef7ed4SIan Dowse 	 * assume exclusive access to the IP header in `m', so any
1298bfef7ed4SIan Dowse 	 * data in a cluster may change before we reach icmp_error().
1299df8bae1dSRodney W. Grimes 	 */
1300780b2f69SAndre Oppermann 	MGETHDR(mcopy, M_DONTWAIT, m->m_type);
1301a163d034SWarner Losh 	if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) {
13029967cafcSSam Leffler 		/*
13039967cafcSSam Leffler 		 * It's probably ok if the pkthdr dup fails (because
13049967cafcSSam Leffler 		 * the deep copy of the tag chain failed), but for now
13059967cafcSSam Leffler 		 * be conservative and just discard the copy since
13069967cafcSSam Leffler 		 * code below may some day want the tags.
13079967cafcSSam Leffler 		 */
13089967cafcSSam Leffler 		m_free(mcopy);
13099967cafcSSam Leffler 		mcopy = NULL;
13109967cafcSSam Leffler 	}
1311bfef7ed4SIan Dowse 	if (mcopy != NULL) {
1312780b2f69SAndre Oppermann 		mcopy->m_len = min(ip->ip_len, M_TRAILINGSPACE(mcopy));
1313e6b0a570SBruce M Simpson 		mcopy->m_pkthdr.len = mcopy->m_len;
1314bfef7ed4SIan Dowse 		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1315bfef7ed4SIan Dowse 	}
131604287599SRuslan Ermilov 
131704287599SRuslan Ermilov #ifdef IPSTEALTH
1318603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
131904287599SRuslan Ermilov #endif
132004287599SRuslan Ermilov 		ip->ip_ttl -= IPTTLDEC;
132104287599SRuslan Ermilov #ifdef IPSTEALTH
132204287599SRuslan Ermilov 	}
132304287599SRuslan Ermilov #endif
1324df8bae1dSRodney W. Grimes 
1325df8bae1dSRodney W. Grimes 	/*
1326df8bae1dSRodney W. Grimes 	 * If forwarding packet using same interface that it came in on,
1327df8bae1dSRodney W. Grimes 	 * perhaps should send a redirect to sender to shortcut a hop.
1328df8bae1dSRodney W. Grimes 	 * Only send redirect if source is sending directly to us,
1329df8bae1dSRodney W. Grimes 	 * and if packet was not source routed (or has any options).
1330df8bae1dSRodney W. Grimes 	 * Also, don't send redirect if forwarding using a default route
1331df8bae1dSRodney W. Grimes 	 * or a route modified by a redirect.
1332df8bae1dSRodney W. Grimes 	 */
13339b932e9eSAndre Oppermann 	dest.s_addr = 0;
1334603724d3SBjoern A. Zeeb 	if (!srcrt && V_ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) {
133502c1c707SAndre Oppermann 		struct sockaddr_in *sin;
133602c1c707SAndre Oppermann 		struct rtentry *rt;
133702c1c707SAndre Oppermann 
13380cfbbe3bSAndre Oppermann 		bzero(&ro, sizeof(ro));
133902c1c707SAndre Oppermann 		sin = (struct sockaddr_in *)&ro.ro_dst;
134002c1c707SAndre Oppermann 		sin->sin_family = AF_INET;
134102c1c707SAndre Oppermann 		sin->sin_len = sizeof(*sin);
13429b932e9eSAndre Oppermann 		sin->sin_addr = ip->ip_dst;
13438b07e49aSJulian Elischer 		in_rtalloc_ign(&ro, RTF_CLONING, M_GETFIB(m));
134402c1c707SAndre Oppermann 
134502c1c707SAndre Oppermann 		rt = ro.ro_rt;
134602c1c707SAndre Oppermann 
134702c1c707SAndre Oppermann 		if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
13489b932e9eSAndre Oppermann 		    satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1349df8bae1dSRodney W. Grimes #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1350df8bae1dSRodney W. Grimes 			u_long src = ntohl(ip->ip_src.s_addr);
1351df8bae1dSRodney W. Grimes 
1352df8bae1dSRodney W. Grimes 			if (RTA(rt) &&
1353df8bae1dSRodney W. Grimes 			    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1354df8bae1dSRodney W. Grimes 				if (rt->rt_flags & RTF_GATEWAY)
13559b932e9eSAndre Oppermann 					dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1356df8bae1dSRodney W. Grimes 				else
13579b932e9eSAndre Oppermann 					dest.s_addr = ip->ip_dst.s_addr;
1358df8bae1dSRodney W. Grimes 				/* Router requirements says to only send host redirects */
1359df8bae1dSRodney W. Grimes 				type = ICMP_REDIRECT;
1360df8bae1dSRodney W. Grimes 				code = ICMP_REDIRECT_HOST;
1361df8bae1dSRodney W. Grimes 			}
1362df8bae1dSRodney W. Grimes 		}
136302c1c707SAndre Oppermann 		if (rt)
136402c1c707SAndre Oppermann 			RTFREE(rt);
136502c1c707SAndre Oppermann 	}
1366df8bae1dSRodney W. Grimes 
1367b835b6feSBjoern A. Zeeb 	/*
1368b835b6feSBjoern A. Zeeb 	 * Try to cache the route MTU from ip_output so we can consider it for
1369b835b6feSBjoern A. Zeeb 	 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191.
1370b835b6feSBjoern A. Zeeb 	 */
1371b835b6feSBjoern A. Zeeb 	bzero(&ro, sizeof(ro));
1372b835b6feSBjoern A. Zeeb 
1373b835b6feSBjoern A. Zeeb 	error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1374b835b6feSBjoern A. Zeeb 
1375b835b6feSBjoern A. Zeeb 	if (error == EMSGSIZE && ro.ro_rt)
1376b835b6feSBjoern A. Zeeb 		mtu = ro.ro_rt->rt_rmx.rmx_mtu;
1377b835b6feSBjoern A. Zeeb 	if (ro.ro_rt)
1378b835b6feSBjoern A. Zeeb 		RTFREE(ro.ro_rt);
1379b835b6feSBjoern A. Zeeb 
1380df8bae1dSRodney W. Grimes 	if (error)
1381603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantforward++;
1382df8bae1dSRodney W. Grimes 	else {
1383603724d3SBjoern A. Zeeb 		V_ipstat.ips_forward++;
1384df8bae1dSRodney W. Grimes 		if (type)
1385603724d3SBjoern A. Zeeb 			V_ipstat.ips_redirectsent++;
1386df8bae1dSRodney W. Grimes 		else {
13879188b4a1SAndre Oppermann 			if (mcopy)
1388df8bae1dSRodney W. Grimes 				m_freem(mcopy);
1389df8bae1dSRodney W. Grimes 			return;
1390df8bae1dSRodney W. Grimes 		}
1391df8bae1dSRodney W. Grimes 	}
1392df8bae1dSRodney W. Grimes 	if (mcopy == NULL)
1393df8bae1dSRodney W. Grimes 		return;
1394df8bae1dSRodney W. Grimes 
1395df8bae1dSRodney W. Grimes 	switch (error) {
1396df8bae1dSRodney W. Grimes 
1397df8bae1dSRodney W. Grimes 	case 0:				/* forwarded, but need redirect */
1398df8bae1dSRodney W. Grimes 		/* type, code set above */
1399df8bae1dSRodney W. Grimes 		break;
1400df8bae1dSRodney W. Grimes 
1401df8bae1dSRodney W. Grimes 	case ENETUNREACH:		/* shouldn't happen, checked above */
1402df8bae1dSRodney W. Grimes 	case EHOSTUNREACH:
1403df8bae1dSRodney W. Grimes 	case ENETDOWN:
1404df8bae1dSRodney W. Grimes 	case EHOSTDOWN:
1405df8bae1dSRodney W. Grimes 	default:
1406df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1407df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_HOST;
1408df8bae1dSRodney W. Grimes 		break;
1409df8bae1dSRodney W. Grimes 
1410df8bae1dSRodney W. Grimes 	case EMSGSIZE:
1411df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1412df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_NEEDFRAG;
14131dfcf0d2SAndre Oppermann 
1414b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
1415b835b6feSBjoern A. Zeeb 		/*
1416b835b6feSBjoern A. Zeeb 		 * If IPsec is configured for this path,
1417b835b6feSBjoern A. Zeeb 		 * override any possibly mtu value set by ip_output.
1418b835b6feSBjoern A. Zeeb 		 */
1419b835b6feSBjoern A. Zeeb 		mtu = ip_ipsec_mtu(m, mtu);
1420b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
14219b932e9eSAndre Oppermann 		/*
1422b835b6feSBjoern A. Zeeb 		 * If the MTU was set before make sure we are below the
1423b835b6feSBjoern A. Zeeb 		 * interface MTU.
1424ab48768bSAndre Oppermann 		 * If the MTU wasn't set before use the interface mtu or
1425ab48768bSAndre Oppermann 		 * fall back to the next smaller mtu step compared to the
1426ab48768bSAndre Oppermann 		 * current packet size.
14279b932e9eSAndre Oppermann 		 */
1428b835b6feSBjoern A. Zeeb 		if (mtu != 0) {
1429b835b6feSBjoern A. Zeeb 			if (ia != NULL)
1430b835b6feSBjoern A. Zeeb 				mtu = min(mtu, ia->ia_ifp->if_mtu);
1431b835b6feSBjoern A. Zeeb 		} else {
1432ab48768bSAndre Oppermann 			if (ia != NULL)
1433c773494eSAndre Oppermann 				mtu = ia->ia_ifp->if_mtu;
1434ab48768bSAndre Oppermann 			else
1435ab48768bSAndre Oppermann 				mtu = ip_next_mtu(ip->ip_len, 0);
1436ab48768bSAndre Oppermann 		}
1437603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantfrag++;
1438df8bae1dSRodney W. Grimes 		break;
1439df8bae1dSRodney W. Grimes 
1440df8bae1dSRodney W. Grimes 	case ENOBUFS:
1441df285b3dSMike Silbersack 		/*
1442df285b3dSMike Silbersack 		 * A router should not generate ICMP_SOURCEQUENCH as
1443df285b3dSMike Silbersack 		 * required in RFC1812 Requirements for IP Version 4 Routers.
1444df285b3dSMike Silbersack 		 * Source quench could be a big problem under DoS attacks,
1445df285b3dSMike Silbersack 		 * or if the underlying interface is rate-limited.
1446df285b3dSMike Silbersack 		 * Those who need source quench packets may re-enable them
1447df285b3dSMike Silbersack 		 * via the net.inet.ip.sendsourcequench sysctl.
1448df285b3dSMike Silbersack 		 */
1449603724d3SBjoern A. Zeeb 		if (V_ip_sendsourcequench == 0) {
1450df285b3dSMike Silbersack 			m_freem(mcopy);
1451df285b3dSMike Silbersack 			return;
1452df285b3dSMike Silbersack 		} else {
1453df8bae1dSRodney W. Grimes 			type = ICMP_SOURCEQUENCH;
1454df8bae1dSRodney W. Grimes 			code = 0;
1455df285b3dSMike Silbersack 		}
1456df8bae1dSRodney W. Grimes 		break;
14573a06e3e0SRuslan Ermilov 
14583a06e3e0SRuslan Ermilov 	case EACCES:			/* ipfw denied packet */
14593a06e3e0SRuslan Ermilov 		m_freem(mcopy);
14603a06e3e0SRuslan Ermilov 		return;
1461df8bae1dSRodney W. Grimes 	}
1462c773494eSAndre Oppermann 	icmp_error(mcopy, type, code, dest.s_addr, mtu);
1463df8bae1dSRodney W. Grimes }
1464df8bae1dSRodney W. Grimes 
146582c23ebaSBill Fenner void
1466f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1467f2565d68SRobert Watson     struct mbuf *m)
146882c23ebaSBill Fenner {
1469be8a62e8SPoul-Henning Kamp 	if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1470be8a62e8SPoul-Henning Kamp 		struct bintime bt;
1471be8a62e8SPoul-Henning Kamp 
1472be8a62e8SPoul-Henning Kamp 		bintime(&bt);
1473be8a62e8SPoul-Henning Kamp 		if (inp->inp_socket->so_options & SO_BINTIME) {
1474be8a62e8SPoul-Henning Kamp 			*mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt),
1475be8a62e8SPoul-Henning Kamp 			SCM_BINTIME, SOL_SOCKET);
1476be8a62e8SPoul-Henning Kamp 			if (*mp)
1477be8a62e8SPoul-Henning Kamp 				mp = &(*mp)->m_next;
1478be8a62e8SPoul-Henning Kamp 		}
147982c23ebaSBill Fenner 		if (inp->inp_socket->so_options & SO_TIMESTAMP) {
148082c23ebaSBill Fenner 			struct timeval tv;
148182c23ebaSBill Fenner 
1482be8a62e8SPoul-Henning Kamp 			bintime2timeval(&bt, &tv);
148382c23ebaSBill Fenner 			*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
148482c23ebaSBill Fenner 				SCM_TIMESTAMP, SOL_SOCKET);
148582c23ebaSBill Fenner 			if (*mp)
148682c23ebaSBill Fenner 				mp = &(*mp)->m_next;
14874cc20ab1SSeigo Tanimura 		}
1488be8a62e8SPoul-Henning Kamp 	}
148982c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVDSTADDR) {
149082c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
149182c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
149282c23ebaSBill Fenner 		if (*mp)
149382c23ebaSBill Fenner 			mp = &(*mp)->m_next;
149482c23ebaSBill Fenner 	}
14954957466bSMatthew N. Dodd 	if (inp->inp_flags & INP_RECVTTL) {
14964957466bSMatthew N. Dodd 		*mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
14974957466bSMatthew N. Dodd 		    sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
14984957466bSMatthew N. Dodd 		if (*mp)
14994957466bSMatthew N. Dodd 			mp = &(*mp)->m_next;
15004957466bSMatthew N. Dodd 	}
150182c23ebaSBill Fenner #ifdef notyet
150282c23ebaSBill Fenner 	/* XXX
150382c23ebaSBill Fenner 	 * Moving these out of udp_input() made them even more broken
150482c23ebaSBill Fenner 	 * than they already were.
150582c23ebaSBill Fenner 	 */
150682c23ebaSBill Fenner 	/* options were tossed already */
150782c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVOPTS) {
150882c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
150982c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
151082c23ebaSBill Fenner 		if (*mp)
151182c23ebaSBill Fenner 			mp = &(*mp)->m_next;
151282c23ebaSBill Fenner 	}
151382c23ebaSBill Fenner 	/* ip_srcroute doesn't do what we want here, need to fix */
151482c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVRETOPTS) {
1515e0982661SAndre Oppermann 		*mp = sbcreatecontrol((caddr_t) ip_srcroute(m),
151682c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
151782c23ebaSBill Fenner 		if (*mp)
151882c23ebaSBill Fenner 			mp = &(*mp)->m_next;
151982c23ebaSBill Fenner 	}
152082c23ebaSBill Fenner #endif
152182c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVIF) {
1522d314ad7bSJulian Elischer 		struct ifnet *ifp;
1523d314ad7bSJulian Elischer 		struct sdlbuf {
152482c23ebaSBill Fenner 			struct sockaddr_dl sdl;
1525d314ad7bSJulian Elischer 			u_char	pad[32];
1526d314ad7bSJulian Elischer 		} sdlbuf;
1527d314ad7bSJulian Elischer 		struct sockaddr_dl *sdp;
1528d314ad7bSJulian Elischer 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
152982c23ebaSBill Fenner 
1530d314ad7bSJulian Elischer 		if (((ifp = m->m_pkthdr.rcvif))
1531603724d3SBjoern A. Zeeb 		&& ( ifp->if_index && (ifp->if_index <= V_if_index))) {
15324a0d6638SRuslan Ermilov 			sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1533d314ad7bSJulian Elischer 			/*
1534d314ad7bSJulian Elischer 			 * Change our mind and don't try copy.
1535d314ad7bSJulian Elischer 			 */
1536d314ad7bSJulian Elischer 			if ((sdp->sdl_family != AF_LINK)
1537d314ad7bSJulian Elischer 			|| (sdp->sdl_len > sizeof(sdlbuf))) {
1538d314ad7bSJulian Elischer 				goto makedummy;
1539d314ad7bSJulian Elischer 			}
1540d314ad7bSJulian Elischer 			bcopy(sdp, sdl2, sdp->sdl_len);
1541d314ad7bSJulian Elischer 		} else {
1542d314ad7bSJulian Elischer makedummy:
1543d314ad7bSJulian Elischer 			sdl2->sdl_len
1544d314ad7bSJulian Elischer 				= offsetof(struct sockaddr_dl, sdl_data[0]);
1545d314ad7bSJulian Elischer 			sdl2->sdl_family = AF_LINK;
1546d314ad7bSJulian Elischer 			sdl2->sdl_index = 0;
1547d314ad7bSJulian Elischer 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1548d314ad7bSJulian Elischer 		}
1549d314ad7bSJulian Elischer 		*mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
155082c23ebaSBill Fenner 			IP_RECVIF, IPPROTO_IP);
155182c23ebaSBill Fenner 		if (*mp)
155282c23ebaSBill Fenner 			mp = &(*mp)->m_next;
155382c23ebaSBill Fenner 	}
155482c23ebaSBill Fenner }
155582c23ebaSBill Fenner 
15564d2e3692SLuigi Rizzo /*
155730916a2dSRobert Watson  * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
155830916a2dSRobert Watson  * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
155930916a2dSRobert Watson  * locking.  This code remains in ip_input.c as ip_mroute.c is optionally
156030916a2dSRobert Watson  * compiled.
15614d2e3692SLuigi Rizzo  */
15624d2e3692SLuigi Rizzo static int ip_rsvp_on;
15634d2e3692SLuigi Rizzo struct socket *ip_rsvpd;
1564df8bae1dSRodney W. Grimes int
1565f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so)
1566f0068c4aSGarrett Wollman {
1567f0068c4aSGarrett Wollman 	if (so->so_type != SOCK_RAW ||
1568f0068c4aSGarrett Wollman 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1569f0068c4aSGarrett Wollman 		return EOPNOTSUPP;
1570f0068c4aSGarrett Wollman 
1571603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL)
1572f0068c4aSGarrett Wollman 		return EADDRINUSE;
1573f0068c4aSGarrett Wollman 
1574603724d3SBjoern A. Zeeb 	V_ip_rsvpd = so;
15751c5de19aSGarrett Wollman 	/*
15761c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-increment
15771c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
15781c5de19aSGarrett Wollman 	 */
1579603724d3SBjoern A. Zeeb 	if (!V_ip_rsvp_on) {
1580603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 1;
1581603724d3SBjoern A. Zeeb 		V_rsvp_on++;
15821c5de19aSGarrett Wollman 	}
1583f0068c4aSGarrett Wollman 
1584f0068c4aSGarrett Wollman 	return 0;
1585f0068c4aSGarrett Wollman }
1586f0068c4aSGarrett Wollman 
1587f0068c4aSGarrett Wollman int
1588f0068c4aSGarrett Wollman ip_rsvp_done(void)
1589f0068c4aSGarrett Wollman {
1590603724d3SBjoern A. Zeeb 	V_ip_rsvpd = NULL;
15911c5de19aSGarrett Wollman 	/*
15921c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-decrement
15931c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
15941c5de19aSGarrett Wollman 	 */
1595603724d3SBjoern A. Zeeb 	if (V_ip_rsvp_on) {
1596603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 0;
1597603724d3SBjoern A. Zeeb 		V_rsvp_on--;
15981c5de19aSGarrett Wollman 	}
1599f0068c4aSGarrett Wollman 	return 0;
1600f0068c4aSGarrett Wollman }
1601bbb4330bSLuigi Rizzo 
1602bbb4330bSLuigi Rizzo void
1603bbb4330bSLuigi Rizzo rsvp_input(struct mbuf *m, int off)	/* XXX must fixup manually */
1604bbb4330bSLuigi Rizzo {
1605bbb4330bSLuigi Rizzo 	if (rsvp_input_p) { /* call the real one if loaded */
1606bbb4330bSLuigi Rizzo 		rsvp_input_p(m, off);
1607bbb4330bSLuigi Rizzo 		return;
1608bbb4330bSLuigi Rizzo 	}
1609bbb4330bSLuigi Rizzo 
1610bbb4330bSLuigi Rizzo 	/* Can still get packets with rsvp_on = 0 if there is a local member
1611bbb4330bSLuigi Rizzo 	 * of the group to which the RSVP packet is addressed.  But in this
1612bbb4330bSLuigi Rizzo 	 * case we want to throw the packet away.
1613bbb4330bSLuigi Rizzo 	 */
1614bbb4330bSLuigi Rizzo 
1615603724d3SBjoern A. Zeeb 	if (!V_rsvp_on) {
1616bbb4330bSLuigi Rizzo 		m_freem(m);
1617bbb4330bSLuigi Rizzo 		return;
1618bbb4330bSLuigi Rizzo 	}
1619bbb4330bSLuigi Rizzo 
1620603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL) {
1621bbb4330bSLuigi Rizzo 		rip_input(m, off);
1622bbb4330bSLuigi Rizzo 		return;
1623bbb4330bSLuigi Rizzo 	}
1624bbb4330bSLuigi Rizzo 	/* Drop the packet */
1625bbb4330bSLuigi Rizzo 	m_freem(m);
1626bbb4330bSLuigi Rizzo }
1627