xref: /freebsd/sys/netinet/ip_input.c (revision 603724d3abed34351087e20b8cb363d8e02072c1)
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 
881c5de19aSGarrett Wollman int rsvp_on = 0;
89f0068c4aSGarrett Wollman 
901f91d8c5SDavid Greenman int	ipforwarding = 0;
910312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
923d177f46SBill Fumerola     &ipforwarding, 0, "Enable IP forwarding between interfaces");
930312fbe9SPoul-Henning Kamp 
94d4fb926cSGarrett Wollman static int	ipsendredirects = 1; /* XXX */
950312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
963d177f46SBill Fumerola     &ipsendredirects, 0, "Enable sending IP redirects");
970312fbe9SPoul-Henning Kamp 
98df8bae1dSRodney W. Grimes int	ip_defttl = IPDEFTTL;
990312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
1003d177f46SBill Fumerola     &ip_defttl, 0, "Maximum TTL on IP packets");
1010312fbe9SPoul-Henning Kamp 
1026a800098SYoshinobu Inoue static int	ip_keepfaith = 0;
1036a800098SYoshinobu Inoue SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
1046a800098SYoshinobu Inoue     &ip_keepfaith,	0,
1056a800098SYoshinobu Inoue     "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
1066a800098SYoshinobu Inoue 
107df285b3dSMike Silbersack static int	ip_sendsourcequench = 0;
108df285b3dSMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW,
109df285b3dSMike Silbersack     &ip_sendsourcequench, 0,
110df285b3dSMike Silbersack     "Enable the transmission of source quench packets");
111df285b3dSMike Silbersack 
1121f44b0a1SDavid Malone int	ip_do_randomid = 0;
1131f44b0a1SDavid Malone SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW,
1141f44b0a1SDavid Malone     &ip_do_randomid, 0,
1151f44b0a1SDavid Malone     "Assign random ip_id values");
1161f44b0a1SDavid Malone 
117823db0e9SDon Lewis /*
118823db0e9SDon Lewis  * XXX - Setting ip_checkinterface mostly implements the receive side of
119823db0e9SDon Lewis  * the Strong ES model described in RFC 1122, but since the routing table
120a8f12100SDon Lewis  * and transmit implementation do not implement the Strong ES model,
121823db0e9SDon Lewis  * setting this to 1 results in an odd hybrid.
1223f67c834SDon Lewis  *
123a8f12100SDon Lewis  * XXX - ip_checkinterface currently must be disabled if you use ipnat
124a8f12100SDon Lewis  * to translate the destination address to another local interface.
1253f67c834SDon Lewis  *
1263f67c834SDon Lewis  * XXX - ip_checkinterface must be disabled if you add IP aliases
1273f67c834SDon Lewis  * to the loopback interface instead of the interface where the
1283f67c834SDon Lewis  * packets for those addresses are received.
129823db0e9SDon Lewis  */
1304bc37f98SMaxim Konovalov static int	ip_checkinterface = 0;
131b3e95d4eSJonathan Lemon SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
132b3e95d4eSJonathan Lemon     &ip_checkinterface, 0, "Verify packet arrives on correct interface");
133b3e95d4eSJonathan Lemon 
134c21fd232SAndre Oppermann struct pfil_head inet_pfil_hook;	/* Packet filter hooks */
135df8bae1dSRodney W. Grimes 
1361cafed39SJonathan Lemon static struct	ifqueue ipintrq;
137ca925d9cSJonathan Lemon static int	ipqmaxlen = IFQ_MAXLEN;
138ca925d9cSJonathan Lemon 
139df8bae1dSRodney W. Grimes extern	struct domain inetdomain;
140f0ffb944SJulian Elischer extern	struct protosw inetsw[];
141df8bae1dSRodney W. Grimes u_char	ip_protox[IPPROTO_MAX];
14259562606SGarrett Wollman struct	in_ifaddrhead in_ifaddrhead; 		/* first inet address */
143ca925d9cSJonathan Lemon struct	in_ifaddrhashhead *in_ifaddrhashtbl;	/* inet addr hash table  */
144ca925d9cSJonathan Lemon u_long 	in_ifaddrhmask;				/* mask for hash table */
145ca925d9cSJonathan Lemon 
146afed1375SDavid Greenman SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
1473d177f46SBill Fumerola     &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
1480312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
1496489fe65SAndre Oppermann     &ipintrq.ifq_drops, 0,
1506489fe65SAndre Oppermann     "Number of packets dropped from the IP input queue");
151df8bae1dSRodney W. Grimes 
152f23b4c91SGarrett Wollman struct ipstat ipstat;
153c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
1543d177f46SBill Fumerola     &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
155194a213eSAndrey A. Chernov 
156d248c7d7SRobert Watson /*
157d248c7d7SRobert Watson  * IP datagram reassembly.
158d248c7d7SRobert Watson  */
159194a213eSAndrey A. Chernov #define IPREASS_NHASH_LOG2      6
160194a213eSAndrey A. Chernov #define IPREASS_NHASH           (1 << IPREASS_NHASH_LOG2)
161194a213eSAndrey A. Chernov #define IPREASS_HMASK           (IPREASS_NHASH - 1)
162194a213eSAndrey A. Chernov #define IPREASS_HASH(x,y) \
163831a80b0SMatthew Dillon 	(((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
164194a213eSAndrey A. Chernov 
165d248c7d7SRobert Watson static uma_zone_t ipq_zone;
166462b86feSPoul-Henning Kamp static TAILQ_HEAD(ipqhead, ipq) ipq[IPREASS_NHASH];
167dfa60d93SRobert Watson static struct mtx ipqlock;
1682fad1e93SSam Leffler 
1692fad1e93SSam Leffler #define	IPQ_LOCK()	mtx_lock(&ipqlock)
1702fad1e93SSam Leffler #define	IPQ_UNLOCK()	mtx_unlock(&ipqlock)
171888c2a3cSSam Leffler #define	IPQ_LOCK_INIT()	mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
172888c2a3cSSam Leffler #define	IPQ_LOCK_ASSERT()	mtx_assert(&ipqlock, MA_OWNED)
173f23b4c91SGarrett Wollman 
174d248c7d7SRobert Watson static void	maxnipq_update(void);
1754f590175SPaul Saab static void	ipq_zone_change(void *);
176d248c7d7SRobert Watson 
177d248c7d7SRobert Watson static int	maxnipq;	/* Administrative limit on # reass queues. */
178d248c7d7SRobert Watson static int	nipq = 0;	/* Total # of reass queues */
1796489fe65SAndre Oppermann SYSCTL_INT(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_RD,
1806489fe65SAndre Oppermann     &nipq, 0, "Current number of IPv4 fragment reassembly queue entries");
181d248c7d7SRobert Watson 
182d248c7d7SRobert Watson static int	maxfragsperpacket;
183d248c7d7SRobert Watson SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW,
184d248c7d7SRobert Watson     &maxfragsperpacket, 0,
185d248c7d7SRobert Watson     "Maximum number of IPv4 fragments allowed per packet");
186d248c7d7SRobert Watson 
187d248c7d7SRobert Watson struct callout	ipport_tick_callout;
188d248c7d7SRobert Watson 
1890312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU
1900312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
1913d177f46SBill Fumerola     &ip_mtu, 0, "Default MTU");
1920312fbe9SPoul-Henning Kamp #endif
1930312fbe9SPoul-Henning Kamp 
1941b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
195c76ff708SAndre Oppermann int	ipstealth = 0;
1961b968362SDag-Erling Smørgrav SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
1976489fe65SAndre Oppermann     &ipstealth, 0, "IP stealth mode, no TTL decrementation on forwarding");
1981b968362SDag-Erling Smørgrav #endif
1991b968362SDag-Erling Smørgrav 
200010b65f5SJulian Elischer /*
201010b65f5SJulian Elischer  * ipfw_ether and ipfw_bridge hooks.
202010b65f5SJulian Elischer  * XXX: Temporary until those are converted to pfil_hooks as well.
203010b65f5SJulian Elischer  */
204010b65f5SJulian Elischer ip_fw_chk_t *ip_fw_chk_ptr = NULL;
205010b65f5SJulian Elischer ip_dn_io_t *ip_dn_io_ptr = NULL;
206010b65f5SJulian Elischer int fw_one_pass = 1;
207010b65f5SJulian Elischer 
2084d77a549SAlfred Perlstein static void	ip_freef(struct ipqhead *, struct ipq *);
2098948e4baSArchie Cobbs 
210df8bae1dSRodney W. Grimes /*
211df8bae1dSRodney W. Grimes  * IP initialization: fill in IP protocol switch table.
212df8bae1dSRodney W. Grimes  * All protocols not implemented in kernel go to raw IP protocol handler.
213df8bae1dSRodney W. Grimes  */
214df8bae1dSRodney W. Grimes void
215f2565d68SRobert Watson ip_init(void)
216df8bae1dSRodney W. Grimes {
217f2565d68SRobert Watson 	struct protosw *pr;
218f2565d68SRobert Watson 	int i;
219df8bae1dSRodney W. Grimes 
220603724d3SBjoern A. Zeeb 	TAILQ_INIT(&V_in_ifaddrhead);
221603724d3SBjoern A. Zeeb 	V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
222f0ffb944SJulian Elischer 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
22302410549SRobert Watson 	if (pr == NULL)
224db09bef3SAndre Oppermann 		panic("ip_init: PF_INET not found");
225db09bef3SAndre Oppermann 
226db09bef3SAndre Oppermann 	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
227df8bae1dSRodney W. Grimes 	for (i = 0; i < IPPROTO_MAX; i++)
228df8bae1dSRodney W. Grimes 		ip_protox[i] = pr - inetsw;
229db09bef3SAndre Oppermann 	/*
230db09bef3SAndre Oppermann 	 * Cycle through IP protocols and put them into the appropriate place
231db09bef3SAndre Oppermann 	 * in ip_protox[].
232db09bef3SAndre Oppermann 	 */
233f0ffb944SJulian Elischer 	for (pr = inetdomain.dom_protosw;
234f0ffb944SJulian Elischer 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
235df8bae1dSRodney W. Grimes 		if (pr->pr_domain->dom_family == PF_INET &&
236db09bef3SAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
237db09bef3SAndre Oppermann 			/* Be careful to only index valid IP protocols. */
238db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX)
239df8bae1dSRodney W. Grimes 				ip_protox[pr->pr_protocol] = pr - inetsw;
240db09bef3SAndre Oppermann 		}
241194a213eSAndrey A. Chernov 
242c21fd232SAndre Oppermann 	/* Initialize packet filter hooks. */
243134ea224SSam Leffler 	inet_pfil_hook.ph_type = PFIL_TYPE_AF;
244134ea224SSam Leffler 	inet_pfil_hook.ph_af = AF_INET;
245134ea224SSam Leffler 	if ((i = pfil_head_register(&inet_pfil_hook)) != 0)
246134ea224SSam Leffler 		printf("%s: WARNING: unable to register pfil hook, "
247134ea224SSam Leffler 			"error %d\n", __func__, i);
248134ea224SSam Leffler 
249db09bef3SAndre Oppermann 	/* Initialize IP reassembly queue. */
2502fad1e93SSam Leffler 	IPQ_LOCK_INIT();
251194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++)
252603724d3SBjoern A. Zeeb 	    TAILQ_INIT(&V_ipq[i]);
253603724d3SBjoern A. Zeeb 	V_maxnipq = nmbclusters / 32;
254603724d3SBjoern A. Zeeb 	V_maxfragsperpacket = 16;
255603724d3SBjoern A. Zeeb 	V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
256d248c7d7SRobert Watson 	    NULL, UMA_ALIGN_PTR, 0);
257d248c7d7SRobert Watson 	maxnipq_update();
258194a213eSAndrey A. Chernov 
2595f311da2SMike Silbersack 	/* Start ipport_tick. */
2605f311da2SMike Silbersack 	callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
2615f311da2SMike Silbersack 	ipport_tick(NULL);
2625f311da2SMike Silbersack 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2635f311da2SMike Silbersack 		SHUTDOWN_PRI_DEFAULT);
2644f590175SPaul Saab 	EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
2654f590175SPaul Saab 		NULL, EVENTHANDLER_PRI_ANY);
2665f311da2SMike Silbersack 
267db09bef3SAndre Oppermann 	/* Initialize various other remaining things. */
268227ee8a1SPoul-Henning Kamp 	ip_id = time_second & 0xffff;
269df8bae1dSRodney W. Grimes 	ipintrq.ifq_maxlen = ipqmaxlen;
2706008862bSJohn Baldwin 	mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF);
27159dd72d0SRobert Watson 	netisr_register(NETISR_IP, ip_input, &ipintrq, 0);
272df8bae1dSRodney W. Grimes }
273df8bae1dSRodney W. Grimes 
274f2565d68SRobert Watson void
275f2565d68SRobert Watson ip_fini(void *xtp)
2765f311da2SMike Silbersack {
277f2565d68SRobert Watson 
2785f311da2SMike Silbersack 	callout_stop(&ipport_tick_callout);
2795f311da2SMike Silbersack }
2805f311da2SMike Silbersack 
2814d2e3692SLuigi Rizzo /*
282df8bae1dSRodney W. Grimes  * Ip input routine.  Checksum and byte swap header.  If fragmented
283df8bae1dSRodney W. Grimes  * try to reassemble.  Process options.  Pass to next level.
284df8bae1dSRodney W. Grimes  */
285c67b1d17SGarrett Wollman void
286c67b1d17SGarrett Wollman ip_input(struct mbuf *m)
287df8bae1dSRodney W. Grimes {
2889188b4a1SAndre Oppermann 	struct ip *ip = NULL;
2895da9f8faSJosef Karthauser 	struct in_ifaddr *ia = NULL;
290ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
2919b932e9eSAndre Oppermann 	int    checkif, hlen = 0;
29247c861ecSBrian Somers 	u_short sum;
29302c1c707SAndre Oppermann 	int dchg = 0;				/* dest changed after fw */
294f51f805fSSam Leffler 	struct in_addr odst;			/* original dst address */
295b715f178SLuigi Rizzo 
296fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
297db40007dSAndrew R. Reiter 
298ac9d7e26SMax Laier 	if (m->m_flags & M_FASTFWD_OURS) {
2999b932e9eSAndre Oppermann 		/*
30076ff6dcfSAndre Oppermann 		 * Firewall or NAT changed destination to local.
30176ff6dcfSAndre Oppermann 		 * We expect ip_len and ip_off to be in host byte order.
3029b932e9eSAndre Oppermann 		 */
30376ff6dcfSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
30476ff6dcfSAndre Oppermann 		/* Set up some basics that will be used later. */
3052b25acc1SLuigi Rizzo 		ip = mtod(m, struct ip *);
30653be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
3079b932e9eSAndre Oppermann 		goto ours;
3082b25acc1SLuigi Rizzo 	}
3092b25acc1SLuigi Rizzo 
310603724d3SBjoern A. Zeeb 	V_ipstat.ips_total++;
31158938916SGarrett Wollman 
31258938916SGarrett Wollman 	if (m->m_pkthdr.len < sizeof(struct ip))
31358938916SGarrett Wollman 		goto tooshort;
31458938916SGarrett Wollman 
315df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct ip) &&
3160b17fba7SAndre Oppermann 	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
317603724d3SBjoern A. Zeeb 		V_ipstat.ips_toosmall++;
318c67b1d17SGarrett Wollman 		return;
319df8bae1dSRodney W. Grimes 	}
320df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
32158938916SGarrett Wollman 
32253be11f6SPoul-Henning Kamp 	if (ip->ip_v != IPVERSION) {
323603724d3SBjoern A. Zeeb 		V_ipstat.ips_badvers++;
324df8bae1dSRodney W. Grimes 		goto bad;
325df8bae1dSRodney W. Grimes 	}
32658938916SGarrett Wollman 
32753be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
328df8bae1dSRodney W. Grimes 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
329603724d3SBjoern A. Zeeb 		V_ipstat.ips_badhlen++;
330df8bae1dSRodney W. Grimes 		goto bad;
331df8bae1dSRodney W. Grimes 	}
332df8bae1dSRodney W. Grimes 	if (hlen > m->m_len) {
3330b17fba7SAndre Oppermann 		if ((m = m_pullup(m, hlen)) == NULL) {
334603724d3SBjoern A. Zeeb 			V_ipstat.ips_badhlen++;
335c67b1d17SGarrett Wollman 			return;
336df8bae1dSRodney W. Grimes 		}
337df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
338df8bae1dSRodney W. Grimes 	}
33933841545SHajimu UMEMOTO 
34033841545SHajimu UMEMOTO 	/* 127/8 must not appear on wire - RFC1122 */
34133841545SHajimu UMEMOTO 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
34233841545SHajimu UMEMOTO 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
34333841545SHajimu UMEMOTO 		if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
344603724d3SBjoern A. Zeeb 			V_ipstat.ips_badaddr++;
34533841545SHajimu UMEMOTO 			goto bad;
34633841545SHajimu UMEMOTO 		}
34733841545SHajimu UMEMOTO 	}
34833841545SHajimu UMEMOTO 
349db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
350db4f9cc7SJonathan Lemon 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
351db4f9cc7SJonathan Lemon 	} else {
35258938916SGarrett Wollman 		if (hlen == sizeof(struct ip)) {
35347c861ecSBrian Somers 			sum = in_cksum_hdr(ip);
35458938916SGarrett Wollman 		} else {
35547c861ecSBrian Somers 			sum = in_cksum(m, hlen);
35658938916SGarrett Wollman 		}
357db4f9cc7SJonathan Lemon 	}
35847c861ecSBrian Somers 	if (sum) {
359603724d3SBjoern A. Zeeb 		V_ipstat.ips_badsum++;
360df8bae1dSRodney W. Grimes 		goto bad;
361df8bae1dSRodney W. Grimes 	}
362df8bae1dSRodney W. Grimes 
36302b199f1SMax Laier #ifdef ALTQ
36402b199f1SMax Laier 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
36502b199f1SMax Laier 		/* packet is dropped by traffic conditioner */
36602b199f1SMax Laier 		return;
36702b199f1SMax Laier #endif
36802b199f1SMax Laier 
369df8bae1dSRodney W. Grimes 	/*
370df8bae1dSRodney W. Grimes 	 * Convert fields to host representation.
371df8bae1dSRodney W. Grimes 	 */
372fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
373df8bae1dSRodney W. Grimes 	if (ip->ip_len < hlen) {
374603724d3SBjoern A. Zeeb 		V_ipstat.ips_badlen++;
375df8bae1dSRodney W. Grimes 		goto bad;
376df8bae1dSRodney W. Grimes 	}
377fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
378df8bae1dSRodney W. Grimes 
379df8bae1dSRodney W. Grimes 	/*
380df8bae1dSRodney W. Grimes 	 * Check that the amount of data in the buffers
381df8bae1dSRodney W. Grimes 	 * is as at least much as the IP header would have us expect.
382df8bae1dSRodney W. Grimes 	 * Trim mbufs if longer than we expect.
383df8bae1dSRodney W. Grimes 	 * Drop packet if shorter than we expect.
384df8bae1dSRodney W. Grimes 	 */
385df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len < ip->ip_len) {
38658938916SGarrett Wollman tooshort:
387603724d3SBjoern A. Zeeb 		V_ipstat.ips_tooshort++;
388df8bae1dSRodney W. Grimes 		goto bad;
389df8bae1dSRodney W. Grimes 	}
390df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len > ip->ip_len) {
391df8bae1dSRodney W. Grimes 		if (m->m_len == m->m_pkthdr.len) {
392df8bae1dSRodney W. Grimes 			m->m_len = ip->ip_len;
393df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = ip->ip_len;
394df8bae1dSRodney W. Grimes 		} else
395df8bae1dSRodney W. Grimes 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
396df8bae1dSRodney W. Grimes 	}
397b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
39814dd6717SSam Leffler 	/*
39914dd6717SSam Leffler 	 * Bypass packet filtering for packets from a tunnel (gif).
40014dd6717SSam Leffler 	 */
401cc977adcSBjoern A. Zeeb 	if (ip_ipsec_filtertunnel(m))
402c21fd232SAndre Oppermann 		goto passin;
403b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
4043f67c834SDon Lewis 
405c4ac87eaSDarren Reed 	/*
406134ea224SSam Leffler 	 * Run through list of hooks for input packets.
407f51f805fSSam Leffler 	 *
408f51f805fSSam Leffler 	 * NB: Beware of the destination address changing (e.g.
409f51f805fSSam Leffler 	 *     by NAT rewriting).  When this happens, tell
410f51f805fSSam Leffler 	 *     ip_forward to do the right thing.
411c4ac87eaSDarren Reed 	 */
412c21fd232SAndre Oppermann 
413c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
414604afec4SChristian S.J. Peron 	if (!PFIL_HOOKED(&inet_pfil_hook))
415c21fd232SAndre Oppermann 		goto passin;
416c21fd232SAndre Oppermann 
417f51f805fSSam Leffler 	odst = ip->ip_dst;
418134ea224SSam Leffler 	if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
419d6a8d588SMax Laier 	    PFIL_IN, NULL) != 0)
420beec8214SDarren Reed 		return;
421134ea224SSam Leffler 	if (m == NULL)			/* consumed by filter */
422c4ac87eaSDarren Reed 		return;
4239b932e9eSAndre Oppermann 
424c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
42502c1c707SAndre Oppermann 	dchg = (odst.s_addr != ip->ip_dst.s_addr);
4269b932e9eSAndre Oppermann 
4279b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
4289b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
4299b932e9eSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
4309b932e9eSAndre Oppermann 		goto ours;
4319b932e9eSAndre Oppermann 	}
432099dd043SAndre Oppermann 	if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) {
433099dd043SAndre Oppermann 		/*
434099dd043SAndre Oppermann 		 * Directly ship on the packet.  This allows to forward packets
435099dd043SAndre Oppermann 		 * that were destined for us to some other directly connected
436099dd043SAndre Oppermann 		 * host.
437099dd043SAndre Oppermann 		 */
438099dd043SAndre Oppermann 		ip_forward(m, dchg);
439099dd043SAndre Oppermann 		return;
440099dd043SAndre Oppermann 	}
4419b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
4429b932e9eSAndre Oppermann 
443c21fd232SAndre Oppermann passin:
444df8bae1dSRodney W. Grimes 	/*
445df8bae1dSRodney W. Grimes 	 * Process options and, if not destined for us,
446df8bae1dSRodney W. Grimes 	 * ship it on.  ip_dooptions returns 1 when an
447df8bae1dSRodney W. Grimes 	 * error was detected (causing an icmp message
448df8bae1dSRodney W. Grimes 	 * to be sent and the original packet to be freed).
449df8bae1dSRodney W. Grimes 	 */
4509b932e9eSAndre Oppermann 	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
451c67b1d17SGarrett Wollman 		return;
452df8bae1dSRodney W. Grimes 
453f0068c4aSGarrett Wollman         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
454f0068c4aSGarrett Wollman          * matter if it is destined to another node, or whether it is
455f0068c4aSGarrett Wollman          * a multicast one, RSVP wants it! and prevents it from being forwarded
456f0068c4aSGarrett Wollman          * anywhere else. Also checks if the rsvp daemon is running before
457f0068c4aSGarrett Wollman 	 * grabbing the packet.
458f0068c4aSGarrett Wollman          */
459603724d3SBjoern A. Zeeb 	if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP)
460f0068c4aSGarrett Wollman 		goto ours;
461f0068c4aSGarrett Wollman 
462df8bae1dSRodney W. Grimes 	/*
463df8bae1dSRodney W. Grimes 	 * Check our list of addresses, to see if the packet is for us.
464cc766e04SGarrett Wollman 	 * If we don't have any addresses, assume any unicast packet
465cc766e04SGarrett Wollman 	 * we receive might be for us (and let the upper layers deal
466cc766e04SGarrett Wollman 	 * with it).
467df8bae1dSRodney W. Grimes 	 */
468603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
469cc766e04SGarrett Wollman 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
470cc766e04SGarrett Wollman 		goto ours;
471cc766e04SGarrett Wollman 
4727538a9a0SJonathan Lemon 	/*
473823db0e9SDon Lewis 	 * Enable a consistency check between the destination address
474823db0e9SDon Lewis 	 * and the arrival interface for a unicast packet (the RFC 1122
475823db0e9SDon Lewis 	 * strong ES model) if IP forwarding is disabled and the packet
476e15ae1b2SDon Lewis 	 * is not locally generated and the packet is not subject to
477e15ae1b2SDon Lewis 	 * 'ipfw fwd'.
4783f67c834SDon Lewis 	 *
4793f67c834SDon Lewis 	 * XXX - Checking also should be disabled if the destination
4803f67c834SDon Lewis 	 * address is ipnat'ed to a different interface.
4813f67c834SDon Lewis 	 *
482a8f12100SDon Lewis 	 * XXX - Checking is incompatible with IP aliases added
4833f67c834SDon Lewis 	 * to the loopback interface instead of the interface where
4843f67c834SDon Lewis 	 * the packets are received.
485a9771948SGleb Smirnoff 	 *
486a9771948SGleb Smirnoff 	 * XXX - This is the case for carp vhost IPs as well so we
487a9771948SGleb Smirnoff 	 * insert a workaround. If the packet got here, we already
488a9771948SGleb Smirnoff 	 * checked with carp_iamatch() and carp_forus().
489823db0e9SDon Lewis 	 */
490603724d3SBjoern A. Zeeb 	checkif = V_ip_checkinterface && (V_ipforwarding == 0) &&
4919494d596SBrooks Davis 	    m->m_pkthdr.rcvif != NULL &&
492e15ae1b2SDon Lewis 	    ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
493a9771948SGleb Smirnoff #ifdef DEV_CARP
494a9771948SGleb Smirnoff 	    !m->m_pkthdr.rcvif->if_carp &&
495a9771948SGleb Smirnoff #endif
4969b932e9eSAndre Oppermann 	    (dchg == 0);
497823db0e9SDon Lewis 
498ca925d9cSJonathan Lemon 	/*
499ca925d9cSJonathan Lemon 	 * Check for exact addresses in the hash bucket.
500ca925d9cSJonathan Lemon 	 */
5019b932e9eSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
502f9e354dfSJulian Elischer 		/*
503823db0e9SDon Lewis 		 * If the address matches, verify that the packet
504823db0e9SDon Lewis 		 * arrived via the correct interface if checking is
505823db0e9SDon Lewis 		 * enabled.
506f9e354dfSJulian Elischer 		 */
5079b932e9eSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
508823db0e9SDon Lewis 		    (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
509ed1ff184SJulian Elischer 			goto ours;
510ca925d9cSJonathan Lemon 	}
511823db0e9SDon Lewis 	/*
512ca925d9cSJonathan Lemon 	 * Check for broadcast addresses.
513ca925d9cSJonathan Lemon 	 *
514ca925d9cSJonathan Lemon 	 * Only accept broadcast packets that arrive via the matching
515ca925d9cSJonathan Lemon 	 * interface.  Reception of forwarded directed broadcasts would
516ca925d9cSJonathan Lemon 	 * be handled via ip_forward() and ether_output() with the loopback
517ca925d9cSJonathan Lemon 	 * into the stack for SIMPLEX interfaces handled by ether_output().
518823db0e9SDon Lewis 	 */
5194f450ff9SBruce M Simpson 	if (m->m_pkthdr.rcvif != NULL &&
5204f450ff9SBruce M Simpson 	    m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
521ca925d9cSJonathan Lemon 	        TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
522ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
523ca925d9cSJonathan Lemon 				continue;
524ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
525df8bae1dSRodney W. Grimes 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
5269b932e9eSAndre Oppermann 			    ip->ip_dst.s_addr)
527df8bae1dSRodney W. Grimes 				goto ours;
5289b932e9eSAndre Oppermann 			if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr)
529df8bae1dSRodney W. Grimes 				goto ours;
5300ac40133SBrian Somers #ifdef BOOTP_COMPAT
531ca925d9cSJonathan Lemon 			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
532ca925d9cSJonathan Lemon 				goto ours;
5330ac40133SBrian Somers #endif
534df8bae1dSRodney W. Grimes 		}
535df8bae1dSRodney W. Grimes 	}
536f8429ca2SBruce M Simpson 	/* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
537f8429ca2SBruce M Simpson 	if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
538603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantforward++;
539f8429ca2SBruce M Simpson 		m_freem(m);
540f8429ca2SBruce M Simpson 		return;
541f8429ca2SBruce M Simpson 	}
542df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
543df8bae1dSRodney W. Grimes 		struct in_multi *inm;
544603724d3SBjoern A. Zeeb 		if (V_ip_mrouter) {
545df8bae1dSRodney W. Grimes 			/*
546df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, all
547df8bae1dSRodney W. Grimes 			 * incoming multicast packets are passed to the
548df8bae1dSRodney W. Grimes 			 * kernel-level multicast forwarding function.
549df8bae1dSRodney W. Grimes 			 * The packet is returned (relatively) intact; if
550df8bae1dSRodney W. Grimes 			 * ip_mforward() returns a non-zero value, the packet
551df8bae1dSRodney W. Grimes 			 * must be discarded, else it may be accepted below.
552df8bae1dSRodney W. Grimes 			 */
553bbb4330bSLuigi Rizzo 			if (ip_mforward &&
554bbb4330bSLuigi Rizzo 			    ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
555603724d3SBjoern A. Zeeb 				V_ipstat.ips_cantforward++;
556df8bae1dSRodney W. Grimes 				m_freem(m);
557c67b1d17SGarrett Wollman 				return;
558df8bae1dSRodney W. Grimes 			}
559df8bae1dSRodney W. Grimes 
560df8bae1dSRodney W. Grimes 			/*
56111612afaSDima Dorfman 			 * The process-level routing daemon needs to receive
562df8bae1dSRodney W. Grimes 			 * all multicast IGMP packets, whether or not this
563df8bae1dSRodney W. Grimes 			 * host belongs to their destination groups.
564df8bae1dSRodney W. Grimes 			 */
565df8bae1dSRodney W. Grimes 			if (ip->ip_p == IPPROTO_IGMP)
566df8bae1dSRodney W. Grimes 				goto ours;
567603724d3SBjoern A. Zeeb 			V_ipstat.ips_forward++;
568df8bae1dSRodney W. Grimes 		}
569df8bae1dSRodney W. Grimes 		/*
570df8bae1dSRodney W. Grimes 		 * See if we belong to the destination multicast group on the
571df8bae1dSRodney W. Grimes 		 * arrival interface.
572df8bae1dSRodney W. Grimes 		 */
573dd5a318bSRobert Watson 		IN_MULTI_LOCK();
574df8bae1dSRodney W. Grimes 		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
575dd5a318bSRobert Watson 		IN_MULTI_UNLOCK();
576df8bae1dSRodney W. Grimes 		if (inm == NULL) {
577603724d3SBjoern A. Zeeb 			V_ipstat.ips_notmember++;
578df8bae1dSRodney W. Grimes 			m_freem(m);
579c67b1d17SGarrett Wollman 			return;
580df8bae1dSRodney W. Grimes 		}
581df8bae1dSRodney W. Grimes 		goto ours;
582df8bae1dSRodney W. Grimes 	}
583df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
584df8bae1dSRodney W. Grimes 		goto ours;
585df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == INADDR_ANY)
586df8bae1dSRodney W. Grimes 		goto ours;
587df8bae1dSRodney W. Grimes 
5886a800098SYoshinobu Inoue 	/*
5896a800098SYoshinobu Inoue 	 * FAITH(Firewall Aided Internet Translator)
5906a800098SYoshinobu Inoue 	 */
5916a800098SYoshinobu Inoue 	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
592603724d3SBjoern A. Zeeb 		if (V_ip_keepfaith) {
5936a800098SYoshinobu Inoue 			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
5946a800098SYoshinobu Inoue 				goto ours;
5956a800098SYoshinobu Inoue 		}
5966a800098SYoshinobu Inoue 		m_freem(m);
5976a800098SYoshinobu Inoue 		return;
5986a800098SYoshinobu Inoue 	}
5999494d596SBrooks Davis 
600df8bae1dSRodney W. Grimes 	/*
601df8bae1dSRodney W. Grimes 	 * Not for us; forward if possible and desirable.
602df8bae1dSRodney W. Grimes 	 */
603603724d3SBjoern A. Zeeb 	if (V_ipforwarding == 0) {
604603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantforward++;
605df8bae1dSRodney W. Grimes 		m_freem(m);
606546f251bSChris D. Faulhaber 	} else {
607b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
6081dfcf0d2SAndre Oppermann 		if (ip_ipsec_fwd(m))
609546f251bSChris D. Faulhaber 			goto bad;
610b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
6119b932e9eSAndre Oppermann 		ip_forward(m, dchg);
612546f251bSChris D. Faulhaber 	}
613c67b1d17SGarrett Wollman 	return;
614df8bae1dSRodney W. Grimes 
615df8bae1dSRodney W. Grimes ours:
616d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH
617d0ebc0d2SYaroslav Tykhiy 	/*
618d0ebc0d2SYaroslav Tykhiy 	 * IPSTEALTH: Process non-routing options only
619d0ebc0d2SYaroslav Tykhiy 	 * if the packet is destined for us.
620d0ebc0d2SYaroslav Tykhiy 	 */
621603724d3SBjoern A. Zeeb 	if (V_ipstealth && hlen > sizeof (struct ip) &&
6229b932e9eSAndre Oppermann 	    ip_dooptions(m, 1))
623d0ebc0d2SYaroslav Tykhiy 		return;
624d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */
625d0ebc0d2SYaroslav Tykhiy 
6265da9f8faSJosef Karthauser 	/* Count the packet in the ip address stats */
6275da9f8faSJosef Karthauser 	if (ia != NULL) {
6285da9f8faSJosef Karthauser 		ia->ia_ifa.if_ipackets++;
6295da9f8faSJosef Karthauser 		ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
6305da9f8faSJosef Karthauser 	}
631100ba1a6SJordan K. Hubbard 
63263f8d699SJordan K. Hubbard 	/*
633b6ea1aa5SRuslan Ermilov 	 * Attempt reassembly; if it succeeds, proceed.
634ac9d7e26SMax Laier 	 * ip_reass() will return a different mbuf.
635df8bae1dSRodney W. Grimes 	 */
636f0cada84SAndre Oppermann 	if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
637f0cada84SAndre Oppermann 		m = ip_reass(m);
638f0cada84SAndre Oppermann 		if (m == NULL)
639c67b1d17SGarrett Wollman 			return;
6406a800098SYoshinobu Inoue 		ip = mtod(m, struct ip *);
6417e2df452SRuslan Ermilov 		/* Get the header length of the reassembled packet */
64253be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
643f0cada84SAndre Oppermann 	}
644f0cada84SAndre Oppermann 
645f0cada84SAndre Oppermann 	/*
646f0cada84SAndre Oppermann 	 * Further protocols expect the packet length to be w/o the
647f0cada84SAndre Oppermann 	 * IP header.
648f0cada84SAndre Oppermann 	 */
649df8bae1dSRodney W. Grimes 	ip->ip_len -= hlen;
650df8bae1dSRodney W. Grimes 
651b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
65233841545SHajimu UMEMOTO 	/*
65333841545SHajimu UMEMOTO 	 * enforce IPsec policy checking if we are seeing last header.
65433841545SHajimu UMEMOTO 	 * note that we do not visit this with protocols with pcb layer
65533841545SHajimu UMEMOTO 	 * code - like udp/tcp/raw ip.
65633841545SHajimu UMEMOTO 	 */
6571dfcf0d2SAndre Oppermann 	if (ip_ipsec_input(m))
65833841545SHajimu UMEMOTO 		goto bad;
659b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
66033841545SHajimu UMEMOTO 
661df8bae1dSRodney W. Grimes 	/*
662df8bae1dSRodney W. Grimes 	 * Switch out to protocol's input routine.
663df8bae1dSRodney W. Grimes 	 */
664603724d3SBjoern A. Zeeb 	V_ipstat.ips_delivered++;
6659b932e9eSAndre Oppermann 
6662b25acc1SLuigi Rizzo 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
667c67b1d17SGarrett Wollman 	return;
668df8bae1dSRodney W. Grimes bad:
669df8bae1dSRodney W. Grimes 	m_freem(m);
670c67b1d17SGarrett Wollman }
671c67b1d17SGarrett Wollman 
672c67b1d17SGarrett Wollman /*
673d248c7d7SRobert Watson  * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
674d248c7d7SRobert Watson  * max has slightly different semantics than the sysctl, for historical
675d248c7d7SRobert Watson  * reasons.
676d248c7d7SRobert Watson  */
677d248c7d7SRobert Watson static void
678d248c7d7SRobert Watson maxnipq_update(void)
679d248c7d7SRobert Watson {
680d248c7d7SRobert Watson 
681d248c7d7SRobert Watson 	/*
682d248c7d7SRobert Watson 	 * -1 for unlimited allocation.
683d248c7d7SRobert Watson 	 */
684603724d3SBjoern A. Zeeb 	if (V_maxnipq < 0)
685603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 0);
686d248c7d7SRobert Watson 	/*
687d248c7d7SRobert Watson 	 * Positive number for specific bound.
688d248c7d7SRobert Watson 	 */
689603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0)
690603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, V_maxnipq);
691d248c7d7SRobert Watson 	/*
692d248c7d7SRobert Watson 	 * Zero specifies no further fragment queue allocation -- set the
693d248c7d7SRobert Watson 	 * bound very low, but rely on implementation elsewhere to actually
694d248c7d7SRobert Watson 	 * prevent allocation and reclaim current queues.
695d248c7d7SRobert Watson 	 */
696603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0)
697603724d3SBjoern A. Zeeb 		uma_zone_set_max(V_ipq_zone, 1);
698d248c7d7SRobert Watson }
699d248c7d7SRobert Watson 
7004f590175SPaul Saab static void
7014f590175SPaul Saab ipq_zone_change(void *tag)
7024f590175SPaul Saab {
7034f590175SPaul Saab 
704603724d3SBjoern A. Zeeb 	if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) {
705603724d3SBjoern A. Zeeb 		V_maxnipq = nmbclusters / 32;
7064f590175SPaul Saab 		maxnipq_update();
7074f590175SPaul Saab 	}
7084f590175SPaul Saab }
7094f590175SPaul Saab 
710d248c7d7SRobert Watson static int
711d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
712d248c7d7SRobert Watson {
713d248c7d7SRobert Watson 	int error, i;
714d248c7d7SRobert Watson 
715603724d3SBjoern A. Zeeb 	i = V_maxnipq;
716d248c7d7SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
717d248c7d7SRobert Watson 	if (error || !req->newptr)
718d248c7d7SRobert Watson 		return (error);
719d248c7d7SRobert Watson 
720d248c7d7SRobert Watson 	/*
721d248c7d7SRobert Watson 	 * XXXRW: Might be a good idea to sanity check the argument and place
722d248c7d7SRobert Watson 	 * an extreme upper bound.
723d248c7d7SRobert Watson 	 */
724d248c7d7SRobert Watson 	if (i < -1)
725d248c7d7SRobert Watson 		return (EINVAL);
726603724d3SBjoern A. Zeeb 	V_maxnipq = i;
727d248c7d7SRobert Watson 	maxnipq_update();
728d248c7d7SRobert Watson 	return (0);
729d248c7d7SRobert Watson }
730d248c7d7SRobert Watson 
731d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
732d248c7d7SRobert Watson     NULL, 0, sysctl_maxnipq, "I",
733d248c7d7SRobert Watson     "Maximum number of IPv4 fragment reassembly queue entries");
734d248c7d7SRobert Watson 
735d248c7d7SRobert Watson /*
7368948e4baSArchie Cobbs  * Take incoming datagram fragment and try to reassemble it into
737f0cada84SAndre Oppermann  * whole datagram.  If the argument is the first fragment or one
738f0cada84SAndre Oppermann  * in between the function will return NULL and store the mbuf
739f0cada84SAndre Oppermann  * in the fragment chain.  If the argument is the last fragment
740f0cada84SAndre Oppermann  * the packet will be reassembled and the pointer to the new
741f0cada84SAndre Oppermann  * mbuf returned for further processing.  Only m_tags attached
742f0cada84SAndre Oppermann  * to the first packet/fragment are preserved.
743f0cada84SAndre Oppermann  * The IP header is *NOT* adjusted out of iplen.
744df8bae1dSRodney W. Grimes  */
745f0cada84SAndre Oppermann struct mbuf *
746f0cada84SAndre Oppermann ip_reass(struct mbuf *m)
747df8bae1dSRodney W. Grimes {
748f0cada84SAndre Oppermann 	struct ip *ip;
749f0cada84SAndre Oppermann 	struct mbuf *p, *q, *nq, *t;
750f0cada84SAndre Oppermann 	struct ipq *fp = NULL;
751f0cada84SAndre Oppermann 	struct ipqhead *head;
752f0cada84SAndre Oppermann 	int i, hlen, next;
75359dfcba4SHajimu UMEMOTO 	u_int8_t ecn, ecn0;
754f0cada84SAndre Oppermann 	u_short hash;
755df8bae1dSRodney W. Grimes 
756800af1fbSMaxim Konovalov 	/* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
757603724d3SBjoern A. Zeeb 	if (V_maxnipq == 0 || V_maxfragsperpacket == 0) {
758603724d3SBjoern A. Zeeb 		V_ipstat.ips_fragments++;
759603724d3SBjoern A. Zeeb 		V_ipstat.ips_fragdropped++;
7609d804f81SAndre Oppermann 		m_freem(m);
7619d804f81SAndre Oppermann 		return (NULL);
762f0cada84SAndre Oppermann 	}
7632fad1e93SSam Leffler 
764f0cada84SAndre Oppermann 	ip = mtod(m, struct ip *);
765f0cada84SAndre Oppermann 	hlen = ip->ip_hl << 2;
766f0cada84SAndre Oppermann 
767f0cada84SAndre Oppermann 	hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
768603724d3SBjoern A. Zeeb 	head = &V_ipq[hash];
769f0cada84SAndre Oppermann 	IPQ_LOCK();
770f0cada84SAndre Oppermann 
771f0cada84SAndre Oppermann 	/*
772f0cada84SAndre Oppermann 	 * Look for queue of fragments
773f0cada84SAndre Oppermann 	 * of this datagram.
774f0cada84SAndre Oppermann 	 */
775f0cada84SAndre Oppermann 	TAILQ_FOREACH(fp, head, ipq_list)
776f0cada84SAndre Oppermann 		if (ip->ip_id == fp->ipq_id &&
777f0cada84SAndre Oppermann 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
778f0cada84SAndre Oppermann 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
779f0cada84SAndre Oppermann #ifdef MAC
78030d239bcSRobert Watson 		    mac_ipq_match(m, fp) &&
781f0cada84SAndre Oppermann #endif
782f0cada84SAndre Oppermann 		    ip->ip_p == fp->ipq_p)
783f0cada84SAndre Oppermann 			goto found;
784f0cada84SAndre Oppermann 
785f0cada84SAndre Oppermann 	fp = NULL;
786f0cada84SAndre Oppermann 
787f0cada84SAndre Oppermann 	/*
788d248c7d7SRobert Watson 	 * Attempt to trim the number of allocated fragment queues if it
789d248c7d7SRobert Watson 	 * exceeds the administrative limit.
790f0cada84SAndre Oppermann 	 */
791603724d3SBjoern A. Zeeb 	if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) {
792f0cada84SAndre Oppermann 		/*
793f0cada84SAndre Oppermann 		 * drop something from the tail of the current queue
794f0cada84SAndre Oppermann 		 * before proceeding further
795f0cada84SAndre Oppermann 		 */
796f0cada84SAndre Oppermann 		struct ipq *q = TAILQ_LAST(head, ipqhead);
797f0cada84SAndre Oppermann 		if (q == NULL) {   /* gak */
798f0cada84SAndre Oppermann 			for (i = 0; i < IPREASS_NHASH; i++) {
799603724d3SBjoern A. Zeeb 				struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead);
800f0cada84SAndre Oppermann 				if (r) {
801603724d3SBjoern A. Zeeb 					V_ipstat.ips_fragtimeout += r->ipq_nfrags;
802603724d3SBjoern A. Zeeb 					ip_freef(&V_ipq[i], r);
803f0cada84SAndre Oppermann 					break;
804f0cada84SAndre Oppermann 				}
805f0cada84SAndre Oppermann 			}
806f0cada84SAndre Oppermann 		} else {
807603724d3SBjoern A. Zeeb 			V_ipstat.ips_fragtimeout += q->ipq_nfrags;
808f0cada84SAndre Oppermann 			ip_freef(head, q);
809f0cada84SAndre Oppermann 		}
810f0cada84SAndre Oppermann 	}
811f0cada84SAndre Oppermann 
812f0cada84SAndre Oppermann found:
813f0cada84SAndre Oppermann 	/*
814f0cada84SAndre Oppermann 	 * Adjust ip_len to not reflect header,
815f0cada84SAndre Oppermann 	 * convert offset of this to bytes.
816f0cada84SAndre Oppermann 	 */
817f0cada84SAndre Oppermann 	ip->ip_len -= hlen;
818f0cada84SAndre Oppermann 	if (ip->ip_off & IP_MF) {
819f0cada84SAndre Oppermann 		/*
820f0cada84SAndre Oppermann 		 * Make sure that fragments have a data length
821f0cada84SAndre Oppermann 		 * that's a non-zero multiple of 8 bytes.
822f0cada84SAndre Oppermann 		 */
823f0cada84SAndre Oppermann 		if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
824603724d3SBjoern A. Zeeb 			V_ipstat.ips_toosmall++; /* XXX */
825f0cada84SAndre Oppermann 			goto dropfrag;
826f0cada84SAndre Oppermann 		}
827f0cada84SAndre Oppermann 		m->m_flags |= M_FRAG;
828f0cada84SAndre Oppermann 	} else
829f0cada84SAndre Oppermann 		m->m_flags &= ~M_FRAG;
830f0cada84SAndre Oppermann 	ip->ip_off <<= 3;
831f0cada84SAndre Oppermann 
832f0cada84SAndre Oppermann 
833f0cada84SAndre Oppermann 	/*
834f0cada84SAndre Oppermann 	 * Attempt reassembly; if it succeeds, proceed.
835f0cada84SAndre Oppermann 	 * ip_reass() will return a different mbuf.
836f0cada84SAndre Oppermann 	 */
837603724d3SBjoern A. Zeeb 	V_ipstat.ips_fragments++;
838f0cada84SAndre Oppermann 	m->m_pkthdr.header = ip;
839f0cada84SAndre Oppermann 
840f0cada84SAndre Oppermann 	/* Previous ip_reass() started here. */
841df8bae1dSRodney W. Grimes 	/*
842df8bae1dSRodney W. Grimes 	 * Presence of header sizes in mbufs
843df8bae1dSRodney W. Grimes 	 * would confuse code below.
844df8bae1dSRodney W. Grimes 	 */
845df8bae1dSRodney W. Grimes 	m->m_data += hlen;
846df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
847df8bae1dSRodney W. Grimes 
848df8bae1dSRodney W. Grimes 	/*
849df8bae1dSRodney W. Grimes 	 * If first fragment to arrive, create a reassembly queue.
850df8bae1dSRodney W. Grimes 	 */
851042bbfa3SRobert Watson 	if (fp == NULL) {
852603724d3SBjoern A. Zeeb 		fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
853d248c7d7SRobert Watson 		if (fp == NULL)
854df8bae1dSRodney W. Grimes 			goto dropfrag;
85536b0360bSRobert Watson #ifdef MAC
85630d239bcSRobert Watson 		if (mac_ipq_init(fp, M_NOWAIT) != 0) {
857603724d3SBjoern A. Zeeb 			uma_zfree(V_ipq_zone, fp);
8581d7d0bfeSPawel Jakub Dawidek 			fp = NULL;
8595e7ce478SRobert Watson 			goto dropfrag;
8605e7ce478SRobert Watson 		}
86130d239bcSRobert Watson 		mac_ipq_create(m, fp);
86236b0360bSRobert Watson #endif
863462b86feSPoul-Henning Kamp 		TAILQ_INSERT_HEAD(head, fp, ipq_list);
864603724d3SBjoern A. Zeeb 		V_nipq++;
865375386e2SMike Silbersack 		fp->ipq_nfrags = 1;
866df8bae1dSRodney W. Grimes 		fp->ipq_ttl = IPFRAGTTL;
867df8bae1dSRodney W. Grimes 		fp->ipq_p = ip->ip_p;
868df8bae1dSRodney W. Grimes 		fp->ipq_id = ip->ip_id;
8696effc713SDoug Rabson 		fp->ipq_src = ip->ip_src;
8706effc713SDoug Rabson 		fp->ipq_dst = ip->ip_dst;
871af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
872af38c68cSLuigi Rizzo 		m->m_nextpkt = NULL;
873800af1fbSMaxim Konovalov 		goto done;
87436b0360bSRobert Watson 	} else {
875375386e2SMike Silbersack 		fp->ipq_nfrags++;
87636b0360bSRobert Watson #ifdef MAC
87730d239bcSRobert Watson 		mac_ipq_update(m, fp);
87836b0360bSRobert Watson #endif
879df8bae1dSRodney W. Grimes 	}
880df8bae1dSRodney W. Grimes 
8816effc713SDoug Rabson #define GETIP(m)	((struct ip*)((m)->m_pkthdr.header))
8826effc713SDoug Rabson 
883df8bae1dSRodney W. Grimes 	/*
88459dfcba4SHajimu UMEMOTO 	 * Handle ECN by comparing this segment with the first one;
88559dfcba4SHajimu UMEMOTO 	 * if CE is set, do not lose CE.
88659dfcba4SHajimu UMEMOTO 	 * drop if CE and not-ECT are mixed for the same packet.
88759dfcba4SHajimu UMEMOTO 	 */
88859dfcba4SHajimu UMEMOTO 	ecn = ip->ip_tos & IPTOS_ECN_MASK;
88959dfcba4SHajimu UMEMOTO 	ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
89059dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_CE) {
89159dfcba4SHajimu UMEMOTO 		if (ecn0 == IPTOS_ECN_NOTECT)
89259dfcba4SHajimu UMEMOTO 			goto dropfrag;
89359dfcba4SHajimu UMEMOTO 		if (ecn0 != IPTOS_ECN_CE)
89459dfcba4SHajimu UMEMOTO 			GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
89559dfcba4SHajimu UMEMOTO 	}
89659dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
89759dfcba4SHajimu UMEMOTO 		goto dropfrag;
89859dfcba4SHajimu UMEMOTO 
89959dfcba4SHajimu UMEMOTO 	/*
900df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
901df8bae1dSRodney W. Grimes 	 */
9026effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
9036effc713SDoug Rabson 		if (GETIP(q)->ip_off > ip->ip_off)
904df8bae1dSRodney W. Grimes 			break;
905df8bae1dSRodney W. Grimes 
906df8bae1dSRodney W. Grimes 	/*
907df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
908df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
909af38c68cSLuigi Rizzo 	 * segment.  If it provides all of our data, drop us, otherwise
910af38c68cSLuigi Rizzo 	 * stick new segment in the proper place.
911db4f9cc7SJonathan Lemon 	 *
912db4f9cc7SJonathan Lemon 	 * If some of the data is dropped from the the preceding
913db4f9cc7SJonathan Lemon 	 * segment, then it's checksum is invalidated.
914df8bae1dSRodney W. Grimes 	 */
9156effc713SDoug Rabson 	if (p) {
9166effc713SDoug Rabson 		i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
917df8bae1dSRodney W. Grimes 		if (i > 0) {
918df8bae1dSRodney W. Grimes 			if (i >= ip->ip_len)
919df8bae1dSRodney W. Grimes 				goto dropfrag;
9206a800098SYoshinobu Inoue 			m_adj(m, i);
921db4f9cc7SJonathan Lemon 			m->m_pkthdr.csum_flags = 0;
922df8bae1dSRodney W. Grimes 			ip->ip_off += i;
923df8bae1dSRodney W. Grimes 			ip->ip_len -= i;
924df8bae1dSRodney W. Grimes 		}
925af38c68cSLuigi Rizzo 		m->m_nextpkt = p->m_nextpkt;
926af38c68cSLuigi Rizzo 		p->m_nextpkt = m;
927af38c68cSLuigi Rizzo 	} else {
928af38c68cSLuigi Rizzo 		m->m_nextpkt = fp->ipq_frags;
929af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
930df8bae1dSRodney W. Grimes 	}
931df8bae1dSRodney W. Grimes 
932df8bae1dSRodney W. Grimes 	/*
933df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
934df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
935df8bae1dSRodney W. Grimes 	 */
9366effc713SDoug Rabson 	for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
937af38c68cSLuigi Rizzo 	     q = nq) {
938b36f5b37SMaxim Konovalov 		i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
9396effc713SDoug Rabson 		if (i < GETIP(q)->ip_len) {
9406effc713SDoug Rabson 			GETIP(q)->ip_len -= i;
9416effc713SDoug Rabson 			GETIP(q)->ip_off += i;
9426effc713SDoug Rabson 			m_adj(q, i);
943db4f9cc7SJonathan Lemon 			q->m_pkthdr.csum_flags = 0;
944df8bae1dSRodney W. Grimes 			break;
945df8bae1dSRodney W. Grimes 		}
9466effc713SDoug Rabson 		nq = q->m_nextpkt;
947af38c68cSLuigi Rizzo 		m->m_nextpkt = nq;
948603724d3SBjoern A. Zeeb 		V_ipstat.ips_fragdropped++;
949375386e2SMike Silbersack 		fp->ipq_nfrags--;
9506effc713SDoug Rabson 		m_freem(q);
951df8bae1dSRodney W. Grimes 	}
952df8bae1dSRodney W. Grimes 
953df8bae1dSRodney W. Grimes 	/*
954375386e2SMike Silbersack 	 * Check for complete reassembly and perform frag per packet
955375386e2SMike Silbersack 	 * limiting.
956375386e2SMike Silbersack 	 *
957375386e2SMike Silbersack 	 * Frag limiting is performed here so that the nth frag has
958375386e2SMike Silbersack 	 * a chance to complete the packet before we drop the packet.
959375386e2SMike Silbersack 	 * As a result, n+1 frags are actually allowed per packet, but
960375386e2SMike Silbersack 	 * only n will ever be stored. (n = maxfragsperpacket.)
961375386e2SMike Silbersack 	 *
962df8bae1dSRodney W. Grimes 	 */
9636effc713SDoug Rabson 	next = 0;
9646effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
965375386e2SMike Silbersack 		if (GETIP(q)->ip_off != next) {
966603724d3SBjoern A. Zeeb 			if (fp->ipq_nfrags > V_maxfragsperpacket) {
967603724d3SBjoern A. Zeeb 				V_ipstat.ips_fragdropped += fp->ipq_nfrags;
968375386e2SMike Silbersack 				ip_freef(head, fp);
96999e8617dSMaxim Konovalov 			}
970f0cada84SAndre Oppermann 			goto done;
971375386e2SMike Silbersack 		}
9726effc713SDoug Rabson 		next += GETIP(q)->ip_len;
9736effc713SDoug Rabson 	}
9746effc713SDoug Rabson 	/* Make sure the last packet didn't have the IP_MF flag */
975375386e2SMike Silbersack 	if (p->m_flags & M_FRAG) {
976603724d3SBjoern A. Zeeb 		if (fp->ipq_nfrags > V_maxfragsperpacket) {
977603724d3SBjoern A. Zeeb 			V_ipstat.ips_fragdropped += fp->ipq_nfrags;
978375386e2SMike Silbersack 			ip_freef(head, fp);
97999e8617dSMaxim Konovalov 		}
980f0cada84SAndre Oppermann 		goto done;
981375386e2SMike Silbersack 	}
982df8bae1dSRodney W. Grimes 
983df8bae1dSRodney W. Grimes 	/*
984430d30d8SBill Fenner 	 * Reassembly is complete.  Make sure the packet is a sane size.
985430d30d8SBill Fenner 	 */
9866effc713SDoug Rabson 	q = fp->ipq_frags;
9876effc713SDoug Rabson 	ip = GETIP(q);
98853be11f6SPoul-Henning Kamp 	if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
989603724d3SBjoern A. Zeeb 		V_ipstat.ips_toolong++;
990603724d3SBjoern A. Zeeb 		V_ipstat.ips_fragdropped += fp->ipq_nfrags;
991462b86feSPoul-Henning Kamp 		ip_freef(head, fp);
992f0cada84SAndre Oppermann 		goto done;
993430d30d8SBill Fenner 	}
994430d30d8SBill Fenner 
995430d30d8SBill Fenner 	/*
996430d30d8SBill Fenner 	 * Concatenate fragments.
997df8bae1dSRodney W. Grimes 	 */
9986effc713SDoug Rabson 	m = q;
999df8bae1dSRodney W. Grimes 	t = m->m_next;
100002410549SRobert Watson 	m->m_next = NULL;
1001df8bae1dSRodney W. Grimes 	m_cat(m, t);
10026effc713SDoug Rabson 	nq = q->m_nextpkt;
100302410549SRobert Watson 	q->m_nextpkt = NULL;
10046effc713SDoug Rabson 	for (q = nq; q != NULL; q = nq) {
10056effc713SDoug Rabson 		nq = q->m_nextpkt;
1006945aa40dSDoug Rabson 		q->m_nextpkt = NULL;
1007db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1008db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1009a8db1d93SJonathan Lemon 		m_cat(m, q);
1010df8bae1dSRodney W. Grimes 	}
10116edb555dSOleg Bulyzhin 	/*
10126edb555dSOleg Bulyzhin 	 * In order to do checksumming faster we do 'end-around carry' here
10136edb555dSOleg Bulyzhin 	 * (and not in for{} loop), though it implies we are not going to
10146edb555dSOleg Bulyzhin 	 * reassemble more than 64k fragments.
10156edb555dSOleg Bulyzhin 	 */
10166edb555dSOleg Bulyzhin 	m->m_pkthdr.csum_data =
10176edb555dSOleg Bulyzhin 	    (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16);
101836b0360bSRobert Watson #ifdef MAC
101930d239bcSRobert Watson 	mac_ipq_reassemble(fp, m);
102030d239bcSRobert Watson 	mac_ipq_destroy(fp);
102136b0360bSRobert Watson #endif
1022df8bae1dSRodney W. Grimes 
1023df8bae1dSRodney W. Grimes 	/*
1024f0cada84SAndre Oppermann 	 * Create header for new ip packet by modifying header of first
1025f0cada84SAndre Oppermann 	 * packet;  dequeue and discard fragment reassembly header.
1026df8bae1dSRodney W. Grimes 	 * Make header visible.
1027df8bae1dSRodney W. Grimes 	 */
1028f0cada84SAndre Oppermann 	ip->ip_len = (ip->ip_hl << 2) + next;
10296effc713SDoug Rabson 	ip->ip_src = fp->ipq_src;
10306effc713SDoug Rabson 	ip->ip_dst = fp->ipq_dst;
1031462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(head, fp, ipq_list);
1032603724d3SBjoern A. Zeeb 	V_nipq--;
1033603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
103453be11f6SPoul-Henning Kamp 	m->m_len += (ip->ip_hl << 2);
103553be11f6SPoul-Henning Kamp 	m->m_data -= (ip->ip_hl << 2);
1036df8bae1dSRodney W. Grimes 	/* some debugging cruft by sklower, below, will go away soon */
1037a5554bf0SPoul-Henning Kamp 	if (m->m_flags & M_PKTHDR)	/* XXX this should be done elsewhere */
1038a5554bf0SPoul-Henning Kamp 		m_fixhdr(m);
1039603724d3SBjoern A. Zeeb 	V_ipstat.ips_reassembled++;
1040f0cada84SAndre Oppermann 	IPQ_UNLOCK();
10416a800098SYoshinobu Inoue 	return (m);
1042df8bae1dSRodney W. Grimes 
1043df8bae1dSRodney W. Grimes dropfrag:
1044603724d3SBjoern A. Zeeb 	V_ipstat.ips_fragdropped++;
1045042bbfa3SRobert Watson 	if (fp != NULL)
1046375386e2SMike Silbersack 		fp->ipq_nfrags--;
1047df8bae1dSRodney W. Grimes 	m_freem(m);
1048f0cada84SAndre Oppermann done:
1049f0cada84SAndre Oppermann 	IPQ_UNLOCK();
1050f0cada84SAndre Oppermann 	return (NULL);
10516effc713SDoug Rabson 
10526effc713SDoug Rabson #undef GETIP
1053df8bae1dSRodney W. Grimes }
1054df8bae1dSRodney W. Grimes 
1055df8bae1dSRodney W. Grimes /*
1056df8bae1dSRodney W. Grimes  * Free a fragment reassembly header and all
1057df8bae1dSRodney W. Grimes  * associated datagrams.
1058df8bae1dSRodney W. Grimes  */
10590312fbe9SPoul-Henning Kamp static void
1060f2565d68SRobert Watson ip_freef(struct ipqhead *fhp, struct ipq *fp)
1061df8bae1dSRodney W. Grimes {
1062f2565d68SRobert Watson 	struct mbuf *q;
1063df8bae1dSRodney W. Grimes 
10642fad1e93SSam Leffler 	IPQ_LOCK_ASSERT();
10652fad1e93SSam Leffler 
10666effc713SDoug Rabson 	while (fp->ipq_frags) {
10676effc713SDoug Rabson 		q = fp->ipq_frags;
10686effc713SDoug Rabson 		fp->ipq_frags = q->m_nextpkt;
10696effc713SDoug Rabson 		m_freem(q);
1070df8bae1dSRodney W. Grimes 	}
1071462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(fhp, fp, ipq_list);
1072603724d3SBjoern A. Zeeb 	uma_zfree(V_ipq_zone, fp);
1073603724d3SBjoern A. Zeeb 	V_nipq--;
1074df8bae1dSRodney W. Grimes }
1075df8bae1dSRodney W. Grimes 
1076df8bae1dSRodney W. Grimes /*
1077df8bae1dSRodney W. Grimes  * IP timer processing;
1078df8bae1dSRodney W. Grimes  * if a timer expires on a reassembly
1079df8bae1dSRodney W. Grimes  * queue, discard it.
1080df8bae1dSRodney W. Grimes  */
1081df8bae1dSRodney W. Grimes void
1082f2565d68SRobert Watson ip_slowtimo(void)
1083df8bae1dSRodney W. Grimes {
1084f2565d68SRobert Watson 	struct ipq *fp;
1085194a213eSAndrey A. Chernov 	int i;
1086df8bae1dSRodney W. Grimes 
10872fad1e93SSam Leffler 	IPQ_LOCK();
1088194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1089603724d3SBjoern A. Zeeb 		for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) {
1090462b86feSPoul-Henning Kamp 			struct ipq *fpp;
1091462b86feSPoul-Henning Kamp 
1092462b86feSPoul-Henning Kamp 			fpp = fp;
1093462b86feSPoul-Henning Kamp 			fp = TAILQ_NEXT(fp, ipq_list);
1094462b86feSPoul-Henning Kamp 			if(--fpp->ipq_ttl == 0) {
1095603724d3SBjoern A. Zeeb 				V_ipstat.ips_fragtimeout += fpp->ipq_nfrags;
1096603724d3SBjoern A. Zeeb 				ip_freef(&V_ipq[i], fpp);
1097df8bae1dSRodney W. Grimes 			}
1098df8bae1dSRodney W. Grimes 		}
1099194a213eSAndrey A. Chernov 	}
1100690a6055SJesper Skriver 	/*
1101690a6055SJesper Skriver 	 * If we are over the maximum number of fragments
1102690a6055SJesper Skriver 	 * (due to the limit being lowered), drain off
1103690a6055SJesper Skriver 	 * enough to get down to the new limit.
1104690a6055SJesper Skriver 	 */
1105603724d3SBjoern A. Zeeb 	if (V_maxnipq >= 0 && V_nipq > V_maxnipq) {
1106690a6055SJesper Skriver 		for (i = 0; i < IPREASS_NHASH; i++) {
1107603724d3SBjoern A. Zeeb 			while (V_nipq > V_maxnipq && !TAILQ_EMPTY(&V_ipq[i])) {
1108603724d3SBjoern A. Zeeb 				V_ipstat.ips_fragdropped +=
1109603724d3SBjoern A. Zeeb 				    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags;
1110603724d3SBjoern A. Zeeb 				ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1111690a6055SJesper Skriver 			}
1112690a6055SJesper Skriver 		}
1113690a6055SJesper Skriver 	}
11142fad1e93SSam Leffler 	IPQ_UNLOCK();
1115df8bae1dSRodney W. Grimes }
1116df8bae1dSRodney W. Grimes 
1117df8bae1dSRodney W. Grimes /*
1118df8bae1dSRodney W. Grimes  * Drain off all datagram fragments.
1119df8bae1dSRodney W. Grimes  */
1120df8bae1dSRodney W. Grimes void
1121f2565d68SRobert Watson ip_drain(void)
1122df8bae1dSRodney W. Grimes {
1123194a213eSAndrey A. Chernov 	int     i;
1124ce29ab3aSGarrett Wollman 
11252fad1e93SSam Leffler 	IPQ_LOCK();
1126194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1127603724d3SBjoern A. Zeeb 		while(!TAILQ_EMPTY(&V_ipq[i])) {
1128603724d3SBjoern A. Zeeb 			V_ipstat.ips_fragdropped +=
1129603724d3SBjoern A. Zeeb 			    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags;
1130603724d3SBjoern A. Zeeb 			ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1131194a213eSAndrey A. Chernov 		}
1132194a213eSAndrey A. Chernov 	}
11332fad1e93SSam Leffler 	IPQ_UNLOCK();
1134ce29ab3aSGarrett Wollman 	in_rtqdrain();
1135df8bae1dSRodney W. Grimes }
1136df8bae1dSRodney W. Grimes 
1137df8bae1dSRodney W. Grimes /*
1138de38924dSAndre Oppermann  * The protocol to be inserted into ip_protox[] must be already registered
1139de38924dSAndre Oppermann  * in inetsw[], either statically or through pf_proto_register().
1140de38924dSAndre Oppermann  */
1141de38924dSAndre Oppermann int
1142de38924dSAndre Oppermann ipproto_register(u_char ipproto)
1143de38924dSAndre Oppermann {
1144de38924dSAndre Oppermann 	struct protosw *pr;
1145de38924dSAndre Oppermann 
1146de38924dSAndre Oppermann 	/* Sanity checks. */
1147de38924dSAndre Oppermann 	if (ipproto == 0)
1148de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1149de38924dSAndre Oppermann 
1150de38924dSAndre Oppermann 	/*
1151de38924dSAndre Oppermann 	 * The protocol slot must not be occupied by another protocol
1152de38924dSAndre Oppermann 	 * already.  An index pointing to IPPROTO_RAW is unused.
1153de38924dSAndre Oppermann 	 */
1154de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1155de38924dSAndre Oppermann 	if (pr == NULL)
1156de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1157de38924dSAndre Oppermann 	if (ip_protox[ipproto] != pr - inetsw)	/* IPPROTO_RAW */
1158de38924dSAndre Oppermann 		return (EEXIST);
1159de38924dSAndre Oppermann 
1160de38924dSAndre Oppermann 	/* Find the protocol position in inetsw[] and set the index. */
1161de38924dSAndre Oppermann 	for (pr = inetdomain.dom_protosw;
1162de38924dSAndre Oppermann 	     pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1163de38924dSAndre Oppermann 		if (pr->pr_domain->dom_family == PF_INET &&
1164de38924dSAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol == ipproto) {
1165de38924dSAndre Oppermann 			/* Be careful to only index valid IP protocols. */
1166db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX) {
1167de38924dSAndre Oppermann 				ip_protox[pr->pr_protocol] = pr - inetsw;
1168de38924dSAndre Oppermann 				return (0);
1169de38924dSAndre Oppermann 			} else
1170de38924dSAndre Oppermann 				return (EINVAL);
1171de38924dSAndre Oppermann 		}
1172de38924dSAndre Oppermann 	}
1173de38924dSAndre Oppermann 	return (EPROTONOSUPPORT);
1174de38924dSAndre Oppermann }
1175de38924dSAndre Oppermann 
1176de38924dSAndre Oppermann int
1177de38924dSAndre Oppermann ipproto_unregister(u_char ipproto)
1178de38924dSAndre Oppermann {
1179de38924dSAndre Oppermann 	struct protosw *pr;
1180de38924dSAndre Oppermann 
1181de38924dSAndre Oppermann 	/* Sanity checks. */
1182de38924dSAndre Oppermann 	if (ipproto == 0)
1183de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1184de38924dSAndre Oppermann 
1185de38924dSAndre Oppermann 	/* Check if the protocol was indeed registered. */
1186de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1187de38924dSAndre Oppermann 	if (pr == NULL)
1188de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1189de38924dSAndre Oppermann 	if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
1190de38924dSAndre Oppermann 		return (ENOENT);
1191de38924dSAndre Oppermann 
1192de38924dSAndre Oppermann 	/* Reset the protocol slot to IPPROTO_RAW. */
1193de38924dSAndre Oppermann 	ip_protox[ipproto] = pr - inetsw;
1194de38924dSAndre Oppermann 	return (0);
1195de38924dSAndre Oppermann }
1196de38924dSAndre Oppermann 
1197df8bae1dSRodney W. Grimes /*
1198df8bae1dSRodney W. Grimes  * Given address of next destination (final or next hop),
1199df8bae1dSRodney W. Grimes  * return internet address info of interface to be used to get there.
1200df8bae1dSRodney W. Grimes  */
1201bd714208SRuslan Ermilov struct in_ifaddr *
12028b07e49aSJulian Elischer ip_rtaddr(struct in_addr dst, u_int fibnum)
1203df8bae1dSRodney W. Grimes {
120497d8d152SAndre Oppermann 	struct route sro;
120502c1c707SAndre Oppermann 	struct sockaddr_in *sin;
120602c1c707SAndre Oppermann 	struct in_ifaddr *ifa;
1207df8bae1dSRodney W. Grimes 
12080cfbbe3bSAndre Oppermann 	bzero(&sro, sizeof(sro));
120997d8d152SAndre Oppermann 	sin = (struct sockaddr_in *)&sro.ro_dst;
1210df8bae1dSRodney W. Grimes 	sin->sin_family = AF_INET;
1211df8bae1dSRodney W. Grimes 	sin->sin_len = sizeof(*sin);
1212df8bae1dSRodney W. Grimes 	sin->sin_addr = dst;
12138b07e49aSJulian Elischer 	in_rtalloc_ign(&sro, RTF_CLONING, fibnum);
1214df8bae1dSRodney W. Grimes 
121597d8d152SAndre Oppermann 	if (sro.ro_rt == NULL)
121602410549SRobert Watson 		return (NULL);
121702c1c707SAndre Oppermann 
121897d8d152SAndre Oppermann 	ifa = ifatoia(sro.ro_rt->rt_ifa);
121997d8d152SAndre Oppermann 	RTFREE(sro.ro_rt);
122002410549SRobert Watson 	return (ifa);
1221df8bae1dSRodney W. Grimes }
1222df8bae1dSRodney W. Grimes 
1223df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = {
1224df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1225df8bae1dSRodney W. Grimes 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1226df8bae1dSRodney W. Grimes 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1227df8bae1dSRodney W. Grimes 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1228fcaf9f91SMike Silbersack 	0,		0,		EHOSTUNREACH,	0,
12293b8123b7SJesper Skriver 	ENOPROTOOPT,	ECONNREFUSED
1230df8bae1dSRodney W. Grimes };
1231df8bae1dSRodney W. Grimes 
1232df8bae1dSRodney W. Grimes /*
1233df8bae1dSRodney W. Grimes  * Forward a packet.  If some error occurs return the sender
1234df8bae1dSRodney W. Grimes  * an icmp packet.  Note we can't always generate a meaningful
1235df8bae1dSRodney W. Grimes  * icmp message because icmp doesn't have a large enough repertoire
1236df8bae1dSRodney W. Grimes  * of codes and types.
1237df8bae1dSRodney W. Grimes  *
1238df8bae1dSRodney W. Grimes  * If not forwarding, just drop the packet.  This could be confusing
1239df8bae1dSRodney W. Grimes  * if ipforwarding was zero but some routing protocol was advancing
1240df8bae1dSRodney W. Grimes  * us as a gateway to somewhere.  However, we must let the routing
1241df8bae1dSRodney W. Grimes  * protocol deal with that.
1242df8bae1dSRodney W. Grimes  *
1243df8bae1dSRodney W. Grimes  * The srcrt parameter indicates whether the packet is being forwarded
1244df8bae1dSRodney W. Grimes  * via a source route.
1245df8bae1dSRodney W. Grimes  */
12469b932e9eSAndre Oppermann void
12479b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt)
1248df8bae1dSRodney W. Grimes {
12492b25acc1SLuigi Rizzo 	struct ip *ip = mtod(m, struct ip *);
12509b932e9eSAndre Oppermann 	struct in_ifaddr *ia = NULL;
1251df8bae1dSRodney W. Grimes 	struct mbuf *mcopy;
12529b932e9eSAndre Oppermann 	struct in_addr dest;
1253b835b6feSBjoern A. Zeeb 	struct route ro;
1254c773494eSAndre Oppermann 	int error, type = 0, code = 0, mtu = 0;
12553efc3014SJulian Elischer 
12569b932e9eSAndre Oppermann 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1257603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantforward++;
1258df8bae1dSRodney W. Grimes 		m_freem(m);
1259df8bae1dSRodney W. Grimes 		return;
1260df8bae1dSRodney W. Grimes 	}
12611b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
1262603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
12631b968362SDag-Erling Smørgrav #endif
1264df8bae1dSRodney W. Grimes 		if (ip->ip_ttl <= IPTTLDEC) {
12651b968362SDag-Erling Smørgrav 			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
126602c1c707SAndre Oppermann 			    0, 0);
1267df8bae1dSRodney W. Grimes 			return;
1268df8bae1dSRodney W. Grimes 		}
12691b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
12701b968362SDag-Erling Smørgrav 	}
12711b968362SDag-Erling Smørgrav #endif
1272df8bae1dSRodney W. Grimes 
12738b07e49aSJulian Elischer 	ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
1274d23d475fSGuido van Rooij 	if (!srcrt && ia == NULL) {
127502c1c707SAndre Oppermann 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1276df8bae1dSRodney W. Grimes 		return;
127702c1c707SAndre Oppermann 	}
1278df8bae1dSRodney W. Grimes 
1279df8bae1dSRodney W. Grimes 	/*
1280bfef7ed4SIan Dowse 	 * Save the IP header and at most 8 bytes of the payload,
1281bfef7ed4SIan Dowse 	 * in case we need to generate an ICMP message to the src.
1282bfef7ed4SIan Dowse 	 *
12834d2e3692SLuigi Rizzo 	 * XXX this can be optimized a lot by saving the data in a local
12844d2e3692SLuigi Rizzo 	 * buffer on the stack (72 bytes at most), and only allocating the
12854d2e3692SLuigi Rizzo 	 * mbuf if really necessary. The vast majority of the packets
12864d2e3692SLuigi Rizzo 	 * are forwarded without having to send an ICMP back (either
12874d2e3692SLuigi Rizzo 	 * because unnecessary, or because rate limited), so we are
12884d2e3692SLuigi Rizzo 	 * really we are wasting a lot of work here.
12894d2e3692SLuigi Rizzo 	 *
1290bfef7ed4SIan Dowse 	 * We don't use m_copy() because it might return a reference
1291bfef7ed4SIan Dowse 	 * to a shared cluster. Both this function and ip_output()
1292bfef7ed4SIan Dowse 	 * assume exclusive access to the IP header in `m', so any
1293bfef7ed4SIan Dowse 	 * data in a cluster may change before we reach icmp_error().
1294df8bae1dSRodney W. Grimes 	 */
1295780b2f69SAndre Oppermann 	MGETHDR(mcopy, M_DONTWAIT, m->m_type);
1296a163d034SWarner Losh 	if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) {
12979967cafcSSam Leffler 		/*
12989967cafcSSam Leffler 		 * It's probably ok if the pkthdr dup fails (because
12999967cafcSSam Leffler 		 * the deep copy of the tag chain failed), but for now
13009967cafcSSam Leffler 		 * be conservative and just discard the copy since
13019967cafcSSam Leffler 		 * code below may some day want the tags.
13029967cafcSSam Leffler 		 */
13039967cafcSSam Leffler 		m_free(mcopy);
13049967cafcSSam Leffler 		mcopy = NULL;
13059967cafcSSam Leffler 	}
1306bfef7ed4SIan Dowse 	if (mcopy != NULL) {
1307780b2f69SAndre Oppermann 		mcopy->m_len = min(ip->ip_len, M_TRAILINGSPACE(mcopy));
1308e6b0a570SBruce M Simpson 		mcopy->m_pkthdr.len = mcopy->m_len;
1309bfef7ed4SIan Dowse 		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1310bfef7ed4SIan Dowse 	}
131104287599SRuslan Ermilov 
131204287599SRuslan Ermilov #ifdef IPSTEALTH
1313603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
131404287599SRuslan Ermilov #endif
131504287599SRuslan Ermilov 		ip->ip_ttl -= IPTTLDEC;
131604287599SRuslan Ermilov #ifdef IPSTEALTH
131704287599SRuslan Ermilov 	}
131804287599SRuslan Ermilov #endif
1319df8bae1dSRodney W. Grimes 
1320df8bae1dSRodney W. Grimes 	/*
1321df8bae1dSRodney W. Grimes 	 * If forwarding packet using same interface that it came in on,
1322df8bae1dSRodney W. Grimes 	 * perhaps should send a redirect to sender to shortcut a hop.
1323df8bae1dSRodney W. Grimes 	 * Only send redirect if source is sending directly to us,
1324df8bae1dSRodney W. Grimes 	 * and if packet was not source routed (or has any options).
1325df8bae1dSRodney W. Grimes 	 * Also, don't send redirect if forwarding using a default route
1326df8bae1dSRodney W. Grimes 	 * or a route modified by a redirect.
1327df8bae1dSRodney W. Grimes 	 */
13289b932e9eSAndre Oppermann 	dest.s_addr = 0;
1329603724d3SBjoern A. Zeeb 	if (!srcrt && V_ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) {
133002c1c707SAndre Oppermann 		struct sockaddr_in *sin;
133102c1c707SAndre Oppermann 		struct rtentry *rt;
133202c1c707SAndre Oppermann 
13330cfbbe3bSAndre Oppermann 		bzero(&ro, sizeof(ro));
133402c1c707SAndre Oppermann 		sin = (struct sockaddr_in *)&ro.ro_dst;
133502c1c707SAndre Oppermann 		sin->sin_family = AF_INET;
133602c1c707SAndre Oppermann 		sin->sin_len = sizeof(*sin);
13379b932e9eSAndre Oppermann 		sin->sin_addr = ip->ip_dst;
13388b07e49aSJulian Elischer 		in_rtalloc_ign(&ro, RTF_CLONING, M_GETFIB(m));
133902c1c707SAndre Oppermann 
134002c1c707SAndre Oppermann 		rt = ro.ro_rt;
134102c1c707SAndre Oppermann 
134202c1c707SAndre Oppermann 		if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
13439b932e9eSAndre Oppermann 		    satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1344df8bae1dSRodney W. Grimes #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1345df8bae1dSRodney W. Grimes 			u_long src = ntohl(ip->ip_src.s_addr);
1346df8bae1dSRodney W. Grimes 
1347df8bae1dSRodney W. Grimes 			if (RTA(rt) &&
1348df8bae1dSRodney W. Grimes 			    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1349df8bae1dSRodney W. Grimes 				if (rt->rt_flags & RTF_GATEWAY)
13509b932e9eSAndre Oppermann 					dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1351df8bae1dSRodney W. Grimes 				else
13529b932e9eSAndre Oppermann 					dest.s_addr = ip->ip_dst.s_addr;
1353df8bae1dSRodney W. Grimes 				/* Router requirements says to only send host redirects */
1354df8bae1dSRodney W. Grimes 				type = ICMP_REDIRECT;
1355df8bae1dSRodney W. Grimes 				code = ICMP_REDIRECT_HOST;
1356df8bae1dSRodney W. Grimes 			}
1357df8bae1dSRodney W. Grimes 		}
135802c1c707SAndre Oppermann 		if (rt)
135902c1c707SAndre Oppermann 			RTFREE(rt);
136002c1c707SAndre Oppermann 	}
1361df8bae1dSRodney W. Grimes 
1362b835b6feSBjoern A. Zeeb 	/*
1363b835b6feSBjoern A. Zeeb 	 * Try to cache the route MTU from ip_output so we can consider it for
1364b835b6feSBjoern A. Zeeb 	 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191.
1365b835b6feSBjoern A. Zeeb 	 */
1366b835b6feSBjoern A. Zeeb 	bzero(&ro, sizeof(ro));
1367b835b6feSBjoern A. Zeeb 
1368b835b6feSBjoern A. Zeeb 	error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1369b835b6feSBjoern A. Zeeb 
1370b835b6feSBjoern A. Zeeb 	if (error == EMSGSIZE && ro.ro_rt)
1371b835b6feSBjoern A. Zeeb 		mtu = ro.ro_rt->rt_rmx.rmx_mtu;
1372b835b6feSBjoern A. Zeeb 	if (ro.ro_rt)
1373b835b6feSBjoern A. Zeeb 		RTFREE(ro.ro_rt);
1374b835b6feSBjoern A. Zeeb 
1375df8bae1dSRodney W. Grimes 	if (error)
1376603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantforward++;
1377df8bae1dSRodney W. Grimes 	else {
1378603724d3SBjoern A. Zeeb 		V_ipstat.ips_forward++;
1379df8bae1dSRodney W. Grimes 		if (type)
1380603724d3SBjoern A. Zeeb 			V_ipstat.ips_redirectsent++;
1381df8bae1dSRodney W. Grimes 		else {
13829188b4a1SAndre Oppermann 			if (mcopy)
1383df8bae1dSRodney W. Grimes 				m_freem(mcopy);
1384df8bae1dSRodney W. Grimes 			return;
1385df8bae1dSRodney W. Grimes 		}
1386df8bae1dSRodney W. Grimes 	}
1387df8bae1dSRodney W. Grimes 	if (mcopy == NULL)
1388df8bae1dSRodney W. Grimes 		return;
1389df8bae1dSRodney W. Grimes 
1390df8bae1dSRodney W. Grimes 	switch (error) {
1391df8bae1dSRodney W. Grimes 
1392df8bae1dSRodney W. Grimes 	case 0:				/* forwarded, but need redirect */
1393df8bae1dSRodney W. Grimes 		/* type, code set above */
1394df8bae1dSRodney W. Grimes 		break;
1395df8bae1dSRodney W. Grimes 
1396df8bae1dSRodney W. Grimes 	case ENETUNREACH:		/* shouldn't happen, checked above */
1397df8bae1dSRodney W. Grimes 	case EHOSTUNREACH:
1398df8bae1dSRodney W. Grimes 	case ENETDOWN:
1399df8bae1dSRodney W. Grimes 	case EHOSTDOWN:
1400df8bae1dSRodney W. Grimes 	default:
1401df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1402df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_HOST;
1403df8bae1dSRodney W. Grimes 		break;
1404df8bae1dSRodney W. Grimes 
1405df8bae1dSRodney W. Grimes 	case EMSGSIZE:
1406df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1407df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_NEEDFRAG;
14081dfcf0d2SAndre Oppermann 
1409b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
1410b835b6feSBjoern A. Zeeb 		/*
1411b835b6feSBjoern A. Zeeb 		 * If IPsec is configured for this path,
1412b835b6feSBjoern A. Zeeb 		 * override any possibly mtu value set by ip_output.
1413b835b6feSBjoern A. Zeeb 		 */
1414b835b6feSBjoern A. Zeeb 		mtu = ip_ipsec_mtu(m, mtu);
1415b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
14169b932e9eSAndre Oppermann 		/*
1417b835b6feSBjoern A. Zeeb 		 * If the MTU was set before make sure we are below the
1418b835b6feSBjoern A. Zeeb 		 * interface MTU.
1419ab48768bSAndre Oppermann 		 * If the MTU wasn't set before use the interface mtu or
1420ab48768bSAndre Oppermann 		 * fall back to the next smaller mtu step compared to the
1421ab48768bSAndre Oppermann 		 * current packet size.
14229b932e9eSAndre Oppermann 		 */
1423b835b6feSBjoern A. Zeeb 		if (mtu != 0) {
1424b835b6feSBjoern A. Zeeb 			if (ia != NULL)
1425b835b6feSBjoern A. Zeeb 				mtu = min(mtu, ia->ia_ifp->if_mtu);
1426b835b6feSBjoern A. Zeeb 		} else {
1427ab48768bSAndre Oppermann 			if (ia != NULL)
1428c773494eSAndre Oppermann 				mtu = ia->ia_ifp->if_mtu;
1429ab48768bSAndre Oppermann 			else
1430ab48768bSAndre Oppermann 				mtu = ip_next_mtu(ip->ip_len, 0);
1431ab48768bSAndre Oppermann 		}
1432603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantfrag++;
1433df8bae1dSRodney W. Grimes 		break;
1434df8bae1dSRodney W. Grimes 
1435df8bae1dSRodney W. Grimes 	case ENOBUFS:
1436df285b3dSMike Silbersack 		/*
1437df285b3dSMike Silbersack 		 * A router should not generate ICMP_SOURCEQUENCH as
1438df285b3dSMike Silbersack 		 * required in RFC1812 Requirements for IP Version 4 Routers.
1439df285b3dSMike Silbersack 		 * Source quench could be a big problem under DoS attacks,
1440df285b3dSMike Silbersack 		 * or if the underlying interface is rate-limited.
1441df285b3dSMike Silbersack 		 * Those who need source quench packets may re-enable them
1442df285b3dSMike Silbersack 		 * via the net.inet.ip.sendsourcequench sysctl.
1443df285b3dSMike Silbersack 		 */
1444603724d3SBjoern A. Zeeb 		if (V_ip_sendsourcequench == 0) {
1445df285b3dSMike Silbersack 			m_freem(mcopy);
1446df285b3dSMike Silbersack 			return;
1447df285b3dSMike Silbersack 		} else {
1448df8bae1dSRodney W. Grimes 			type = ICMP_SOURCEQUENCH;
1449df8bae1dSRodney W. Grimes 			code = 0;
1450df285b3dSMike Silbersack 		}
1451df8bae1dSRodney W. Grimes 		break;
14523a06e3e0SRuslan Ermilov 
14533a06e3e0SRuslan Ermilov 	case EACCES:			/* ipfw denied packet */
14543a06e3e0SRuslan Ermilov 		m_freem(mcopy);
14553a06e3e0SRuslan Ermilov 		return;
1456df8bae1dSRodney W. Grimes 	}
1457c773494eSAndre Oppermann 	icmp_error(mcopy, type, code, dest.s_addr, mtu);
1458df8bae1dSRodney W. Grimes }
1459df8bae1dSRodney W. Grimes 
146082c23ebaSBill Fenner void
1461f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1462f2565d68SRobert Watson     struct mbuf *m)
146382c23ebaSBill Fenner {
1464be8a62e8SPoul-Henning Kamp 	if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1465be8a62e8SPoul-Henning Kamp 		struct bintime bt;
1466be8a62e8SPoul-Henning Kamp 
1467be8a62e8SPoul-Henning Kamp 		bintime(&bt);
1468be8a62e8SPoul-Henning Kamp 		if (inp->inp_socket->so_options & SO_BINTIME) {
1469be8a62e8SPoul-Henning Kamp 			*mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt),
1470be8a62e8SPoul-Henning Kamp 			SCM_BINTIME, SOL_SOCKET);
1471be8a62e8SPoul-Henning Kamp 			if (*mp)
1472be8a62e8SPoul-Henning Kamp 				mp = &(*mp)->m_next;
1473be8a62e8SPoul-Henning Kamp 		}
147482c23ebaSBill Fenner 		if (inp->inp_socket->so_options & SO_TIMESTAMP) {
147582c23ebaSBill Fenner 			struct timeval tv;
147682c23ebaSBill Fenner 
1477be8a62e8SPoul-Henning Kamp 			bintime2timeval(&bt, &tv);
147882c23ebaSBill Fenner 			*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
147982c23ebaSBill Fenner 				SCM_TIMESTAMP, SOL_SOCKET);
148082c23ebaSBill Fenner 			if (*mp)
148182c23ebaSBill Fenner 				mp = &(*mp)->m_next;
14824cc20ab1SSeigo Tanimura 		}
1483be8a62e8SPoul-Henning Kamp 	}
148482c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVDSTADDR) {
148582c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
148682c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
148782c23ebaSBill Fenner 		if (*mp)
148882c23ebaSBill Fenner 			mp = &(*mp)->m_next;
148982c23ebaSBill Fenner 	}
14904957466bSMatthew N. Dodd 	if (inp->inp_flags & INP_RECVTTL) {
14914957466bSMatthew N. Dodd 		*mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
14924957466bSMatthew N. Dodd 		    sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
14934957466bSMatthew N. Dodd 		if (*mp)
14944957466bSMatthew N. Dodd 			mp = &(*mp)->m_next;
14954957466bSMatthew N. Dodd 	}
149682c23ebaSBill Fenner #ifdef notyet
149782c23ebaSBill Fenner 	/* XXX
149882c23ebaSBill Fenner 	 * Moving these out of udp_input() made them even more broken
149982c23ebaSBill Fenner 	 * than they already were.
150082c23ebaSBill Fenner 	 */
150182c23ebaSBill Fenner 	/* options were tossed already */
150282c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVOPTS) {
150382c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
150482c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
150582c23ebaSBill Fenner 		if (*mp)
150682c23ebaSBill Fenner 			mp = &(*mp)->m_next;
150782c23ebaSBill Fenner 	}
150882c23ebaSBill Fenner 	/* ip_srcroute doesn't do what we want here, need to fix */
150982c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVRETOPTS) {
1510e0982661SAndre Oppermann 		*mp = sbcreatecontrol((caddr_t) ip_srcroute(m),
151182c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
151282c23ebaSBill Fenner 		if (*mp)
151382c23ebaSBill Fenner 			mp = &(*mp)->m_next;
151482c23ebaSBill Fenner 	}
151582c23ebaSBill Fenner #endif
151682c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVIF) {
1517d314ad7bSJulian Elischer 		struct ifnet *ifp;
1518d314ad7bSJulian Elischer 		struct sdlbuf {
151982c23ebaSBill Fenner 			struct sockaddr_dl sdl;
1520d314ad7bSJulian Elischer 			u_char	pad[32];
1521d314ad7bSJulian Elischer 		} sdlbuf;
1522d314ad7bSJulian Elischer 		struct sockaddr_dl *sdp;
1523d314ad7bSJulian Elischer 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
152482c23ebaSBill Fenner 
1525d314ad7bSJulian Elischer 		if (((ifp = m->m_pkthdr.rcvif))
1526603724d3SBjoern A. Zeeb 		&& ( ifp->if_index && (ifp->if_index <= V_if_index))) {
15274a0d6638SRuslan Ermilov 			sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1528d314ad7bSJulian Elischer 			/*
1529d314ad7bSJulian Elischer 			 * Change our mind and don't try copy.
1530d314ad7bSJulian Elischer 			 */
1531d314ad7bSJulian Elischer 			if ((sdp->sdl_family != AF_LINK)
1532d314ad7bSJulian Elischer 			|| (sdp->sdl_len > sizeof(sdlbuf))) {
1533d314ad7bSJulian Elischer 				goto makedummy;
1534d314ad7bSJulian Elischer 			}
1535d314ad7bSJulian Elischer 			bcopy(sdp, sdl2, sdp->sdl_len);
1536d314ad7bSJulian Elischer 		} else {
1537d314ad7bSJulian Elischer makedummy:
1538d314ad7bSJulian Elischer 			sdl2->sdl_len
1539d314ad7bSJulian Elischer 				= offsetof(struct sockaddr_dl, sdl_data[0]);
1540d314ad7bSJulian Elischer 			sdl2->sdl_family = AF_LINK;
1541d314ad7bSJulian Elischer 			sdl2->sdl_index = 0;
1542d314ad7bSJulian Elischer 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1543d314ad7bSJulian Elischer 		}
1544d314ad7bSJulian Elischer 		*mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
154582c23ebaSBill Fenner 			IP_RECVIF, IPPROTO_IP);
154682c23ebaSBill Fenner 		if (*mp)
154782c23ebaSBill Fenner 			mp = &(*mp)->m_next;
154882c23ebaSBill Fenner 	}
154982c23ebaSBill Fenner }
155082c23ebaSBill Fenner 
15514d2e3692SLuigi Rizzo /*
155230916a2dSRobert Watson  * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
155330916a2dSRobert Watson  * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
155430916a2dSRobert Watson  * locking.  This code remains in ip_input.c as ip_mroute.c is optionally
155530916a2dSRobert Watson  * compiled.
15564d2e3692SLuigi Rizzo  */
15574d2e3692SLuigi Rizzo static int ip_rsvp_on;
15584d2e3692SLuigi Rizzo struct socket *ip_rsvpd;
1559df8bae1dSRodney W. Grimes int
1560f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so)
1561f0068c4aSGarrett Wollman {
1562f0068c4aSGarrett Wollman 	if (so->so_type != SOCK_RAW ||
1563f0068c4aSGarrett Wollman 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1564f0068c4aSGarrett Wollman 		return EOPNOTSUPP;
1565f0068c4aSGarrett Wollman 
1566603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL)
1567f0068c4aSGarrett Wollman 		return EADDRINUSE;
1568f0068c4aSGarrett Wollman 
1569603724d3SBjoern A. Zeeb 	V_ip_rsvpd = so;
15701c5de19aSGarrett Wollman 	/*
15711c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-increment
15721c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
15731c5de19aSGarrett Wollman 	 */
1574603724d3SBjoern A. Zeeb 	if (!V_ip_rsvp_on) {
1575603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 1;
1576603724d3SBjoern A. Zeeb 		V_rsvp_on++;
15771c5de19aSGarrett Wollman 	}
1578f0068c4aSGarrett Wollman 
1579f0068c4aSGarrett Wollman 	return 0;
1580f0068c4aSGarrett Wollman }
1581f0068c4aSGarrett Wollman 
1582f0068c4aSGarrett Wollman int
1583f0068c4aSGarrett Wollman ip_rsvp_done(void)
1584f0068c4aSGarrett Wollman {
1585603724d3SBjoern A. Zeeb 	V_ip_rsvpd = NULL;
15861c5de19aSGarrett Wollman 	/*
15871c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-decrement
15881c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
15891c5de19aSGarrett Wollman 	 */
1590603724d3SBjoern A. Zeeb 	if (V_ip_rsvp_on) {
1591603724d3SBjoern A. Zeeb 		V_ip_rsvp_on = 0;
1592603724d3SBjoern A. Zeeb 		V_rsvp_on--;
15931c5de19aSGarrett Wollman 	}
1594f0068c4aSGarrett Wollman 	return 0;
1595f0068c4aSGarrett Wollman }
1596bbb4330bSLuigi Rizzo 
1597bbb4330bSLuigi Rizzo void
1598bbb4330bSLuigi Rizzo rsvp_input(struct mbuf *m, int off)	/* XXX must fixup manually */
1599bbb4330bSLuigi Rizzo {
1600bbb4330bSLuigi Rizzo 	if (rsvp_input_p) { /* call the real one if loaded */
1601bbb4330bSLuigi Rizzo 		rsvp_input_p(m, off);
1602bbb4330bSLuigi Rizzo 		return;
1603bbb4330bSLuigi Rizzo 	}
1604bbb4330bSLuigi Rizzo 
1605bbb4330bSLuigi Rizzo 	/* Can still get packets with rsvp_on = 0 if there is a local member
1606bbb4330bSLuigi Rizzo 	 * of the group to which the RSVP packet is addressed.  But in this
1607bbb4330bSLuigi Rizzo 	 * case we want to throw the packet away.
1608bbb4330bSLuigi Rizzo 	 */
1609bbb4330bSLuigi Rizzo 
1610603724d3SBjoern A. Zeeb 	if (!V_rsvp_on) {
1611bbb4330bSLuigi Rizzo 		m_freem(m);
1612bbb4330bSLuigi Rizzo 		return;
1613bbb4330bSLuigi Rizzo 	}
1614bbb4330bSLuigi Rizzo 
1615603724d3SBjoern A. Zeeb 	if (V_ip_rsvpd != NULL) {
1616bbb4330bSLuigi Rizzo 		rip_input(m, off);
1617bbb4330bSLuigi Rizzo 		return;
1618bbb4330bSLuigi Rizzo 	}
1619bbb4330bSLuigi Rizzo 	/* Drop the packet */
1620bbb4330bSLuigi Rizzo 	m_freem(m);
1621bbb4330bSLuigi Rizzo }
1622