xref: /freebsd/sys/netinet/ip_input.c (revision b835b6fe2b6323e95b1856f51a854657cecada1f)
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>
54df8bae1dSRodney W. Grimes 
55c85540ddSAndrey A. Chernov #include <net/pfil.h>
56df8bae1dSRodney W. Grimes #include <net/if.h>
579494d596SBrooks Davis #include <net/if_types.h>
58d314ad7bSJulian Elischer #include <net/if_var.h>
5982c23ebaSBill Fenner #include <net/if_dl.h>
60df8bae1dSRodney W. Grimes #include <net/route.h>
61748e0b0aSGarrett Wollman #include <net/netisr.h>
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes #include <netinet/in.h>
64df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
65b5e8ce9fSBruce Evans #include <netinet/in_var.h>
66df8bae1dSRodney W. Grimes #include <netinet/ip.h>
67df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
68df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
69df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
70ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
7158938916SGarrett Wollman #include <machine/in_cksum.h>
72a9771948SGleb Smirnoff #ifdef DEV_CARP
73a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
74a9771948SGleb Smirnoff #endif
75b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
761dfcf0d2SAndre Oppermann #include <netinet/ip_ipsec.h>
77b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
78df8bae1dSRodney W. Grimes 
79f0068c4aSGarrett Wollman #include <sys/socketvar.h>
806ddbf1e2SGary Palmer 
81010b65f5SJulian Elischer /* XXX: Temporary until ipfw_ether and ipfw_bridge are converted. */
82010b65f5SJulian Elischer #include <netinet/ip_fw.h>
83010b65f5SJulian Elischer #include <netinet/ip_dummynet.h>
84010b65f5SJulian Elischer 
85aed55708SRobert Watson #include <security/mac/mac_framework.h>
86aed55708SRobert Watson 
871c5de19aSGarrett Wollman int rsvp_on = 0;
88f0068c4aSGarrett Wollman 
891f91d8c5SDavid Greenman int	ipforwarding = 0;
900312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
913d177f46SBill Fumerola     &ipforwarding, 0, "Enable IP forwarding between interfaces");
920312fbe9SPoul-Henning Kamp 
93d4fb926cSGarrett Wollman static int	ipsendredirects = 1; /* XXX */
940312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
953d177f46SBill Fumerola     &ipsendredirects, 0, "Enable sending IP redirects");
960312fbe9SPoul-Henning Kamp 
97df8bae1dSRodney W. Grimes int	ip_defttl = IPDEFTTL;
980312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
993d177f46SBill Fumerola     &ip_defttl, 0, "Maximum TTL on IP packets");
1000312fbe9SPoul-Henning Kamp 
1016a800098SYoshinobu Inoue static int	ip_keepfaith = 0;
1026a800098SYoshinobu Inoue SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
1036a800098SYoshinobu Inoue     &ip_keepfaith,	0,
1046a800098SYoshinobu Inoue     "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
1056a800098SYoshinobu Inoue 
106df285b3dSMike Silbersack static int	ip_sendsourcequench = 0;
107df285b3dSMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW,
108df285b3dSMike Silbersack     &ip_sendsourcequench, 0,
109df285b3dSMike Silbersack     "Enable the transmission of source quench packets");
110df285b3dSMike Silbersack 
1111f44b0a1SDavid Malone int	ip_do_randomid = 0;
1121f44b0a1SDavid Malone SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW,
1131f44b0a1SDavid Malone     &ip_do_randomid, 0,
1141f44b0a1SDavid Malone     "Assign random ip_id values");
1151f44b0a1SDavid Malone 
116823db0e9SDon Lewis /*
117823db0e9SDon Lewis  * XXX - Setting ip_checkinterface mostly implements the receive side of
118823db0e9SDon Lewis  * the Strong ES model described in RFC 1122, but since the routing table
119a8f12100SDon Lewis  * and transmit implementation do not implement the Strong ES model,
120823db0e9SDon Lewis  * setting this to 1 results in an odd hybrid.
1213f67c834SDon Lewis  *
122a8f12100SDon Lewis  * XXX - ip_checkinterface currently must be disabled if you use ipnat
123a8f12100SDon Lewis  * to translate the destination address to another local interface.
1243f67c834SDon Lewis  *
1253f67c834SDon Lewis  * XXX - ip_checkinterface must be disabled if you add IP aliases
1263f67c834SDon Lewis  * to the loopback interface instead of the interface where the
1273f67c834SDon Lewis  * packets for those addresses are received.
128823db0e9SDon Lewis  */
1294bc37f98SMaxim Konovalov static int	ip_checkinterface = 0;
130b3e95d4eSJonathan Lemon SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
131b3e95d4eSJonathan Lemon     &ip_checkinterface, 0, "Verify packet arrives on correct interface");
132b3e95d4eSJonathan Lemon 
133c21fd232SAndre Oppermann struct pfil_head inet_pfil_hook;	/* Packet filter hooks */
134df8bae1dSRodney W. Grimes 
1351cafed39SJonathan Lemon static struct	ifqueue ipintrq;
136ca925d9cSJonathan Lemon static int	ipqmaxlen = IFQ_MAXLEN;
137ca925d9cSJonathan Lemon 
138df8bae1dSRodney W. Grimes extern	struct domain inetdomain;
139f0ffb944SJulian Elischer extern	struct protosw inetsw[];
140df8bae1dSRodney W. Grimes u_char	ip_protox[IPPROTO_MAX];
14159562606SGarrett Wollman struct	in_ifaddrhead in_ifaddrhead; 		/* first inet address */
142ca925d9cSJonathan Lemon struct	in_ifaddrhashhead *in_ifaddrhashtbl;	/* inet addr hash table  */
143ca925d9cSJonathan Lemon u_long 	in_ifaddrhmask;				/* mask for hash table */
144ca925d9cSJonathan Lemon 
145afed1375SDavid Greenman SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
1463d177f46SBill Fumerola     &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
1470312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
1486489fe65SAndre Oppermann     &ipintrq.ifq_drops, 0,
1496489fe65SAndre Oppermann     "Number of packets dropped from the IP input queue");
150df8bae1dSRodney W. Grimes 
151f23b4c91SGarrett Wollman struct ipstat ipstat;
152c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
1533d177f46SBill Fumerola     &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
154194a213eSAndrey A. Chernov 
155d248c7d7SRobert Watson /*
156d248c7d7SRobert Watson  * IP datagram reassembly.
157d248c7d7SRobert Watson  */
158194a213eSAndrey A. Chernov #define IPREASS_NHASH_LOG2      6
159194a213eSAndrey A. Chernov #define IPREASS_NHASH           (1 << IPREASS_NHASH_LOG2)
160194a213eSAndrey A. Chernov #define IPREASS_HMASK           (IPREASS_NHASH - 1)
161194a213eSAndrey A. Chernov #define IPREASS_HASH(x,y) \
162831a80b0SMatthew Dillon 	(((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
163194a213eSAndrey A. Chernov 
164d248c7d7SRobert Watson static uma_zone_t ipq_zone;
165462b86feSPoul-Henning Kamp static TAILQ_HEAD(ipqhead, ipq) ipq[IPREASS_NHASH];
166dfa60d93SRobert Watson static struct mtx ipqlock;
1672fad1e93SSam Leffler 
1682fad1e93SSam Leffler #define	IPQ_LOCK()	mtx_lock(&ipqlock)
1692fad1e93SSam Leffler #define	IPQ_UNLOCK()	mtx_unlock(&ipqlock)
170888c2a3cSSam Leffler #define	IPQ_LOCK_INIT()	mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
171888c2a3cSSam Leffler #define	IPQ_LOCK_ASSERT()	mtx_assert(&ipqlock, MA_OWNED)
172f23b4c91SGarrett Wollman 
173d248c7d7SRobert Watson static void	maxnipq_update(void);
1744f590175SPaul Saab static void	ipq_zone_change(void *);
175d248c7d7SRobert Watson 
176d248c7d7SRobert Watson static int	maxnipq;	/* Administrative limit on # reass queues. */
177d248c7d7SRobert Watson static int	nipq = 0;	/* Total # of reass queues */
1786489fe65SAndre Oppermann SYSCTL_INT(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_RD,
1796489fe65SAndre Oppermann     &nipq, 0, "Current number of IPv4 fragment reassembly queue entries");
180d248c7d7SRobert Watson 
181d248c7d7SRobert Watson static int	maxfragsperpacket;
182d248c7d7SRobert Watson SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW,
183d248c7d7SRobert Watson     &maxfragsperpacket, 0,
184d248c7d7SRobert Watson     "Maximum number of IPv4 fragments allowed per packet");
185d248c7d7SRobert Watson 
186d248c7d7SRobert Watson struct callout	ipport_tick_callout;
187d248c7d7SRobert Watson 
1880312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU
1890312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
1903d177f46SBill Fumerola     &ip_mtu, 0, "Default MTU");
1910312fbe9SPoul-Henning Kamp #endif
1920312fbe9SPoul-Henning Kamp 
1931b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
194c76ff708SAndre Oppermann int	ipstealth = 0;
1951b968362SDag-Erling Smørgrav SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
1966489fe65SAndre Oppermann     &ipstealth, 0, "IP stealth mode, no TTL decrementation on forwarding");
1971b968362SDag-Erling Smørgrav #endif
1981b968362SDag-Erling Smørgrav 
199010b65f5SJulian Elischer /*
200010b65f5SJulian Elischer  * ipfw_ether and ipfw_bridge hooks.
201010b65f5SJulian Elischer  * XXX: Temporary until those are converted to pfil_hooks as well.
202010b65f5SJulian Elischer  */
203010b65f5SJulian Elischer ip_fw_chk_t *ip_fw_chk_ptr = NULL;
204010b65f5SJulian Elischer ip_dn_io_t *ip_dn_io_ptr = NULL;
205010b65f5SJulian Elischer int fw_one_pass = 1;
206010b65f5SJulian Elischer 
2074d77a549SAlfred Perlstein static void	ip_freef(struct ipqhead *, struct ipq *);
2088948e4baSArchie Cobbs 
209df8bae1dSRodney W. Grimes /*
210df8bae1dSRodney W. Grimes  * IP initialization: fill in IP protocol switch table.
211df8bae1dSRodney W. Grimes  * All protocols not implemented in kernel go to raw IP protocol handler.
212df8bae1dSRodney W. Grimes  */
213df8bae1dSRodney W. Grimes void
214f2565d68SRobert Watson ip_init(void)
215df8bae1dSRodney W. Grimes {
216f2565d68SRobert Watson 	struct protosw *pr;
217f2565d68SRobert Watson 	int i;
218df8bae1dSRodney W. Grimes 
21959562606SGarrett Wollman 	TAILQ_INIT(&in_ifaddrhead);
220ca925d9cSJonathan Lemon 	in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &in_ifaddrhmask);
221f0ffb944SJulian Elischer 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
22202410549SRobert Watson 	if (pr == NULL)
223db09bef3SAndre Oppermann 		panic("ip_init: PF_INET not found");
224db09bef3SAndre Oppermann 
225db09bef3SAndre Oppermann 	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
226df8bae1dSRodney W. Grimes 	for (i = 0; i < IPPROTO_MAX; i++)
227df8bae1dSRodney W. Grimes 		ip_protox[i] = pr - inetsw;
228db09bef3SAndre Oppermann 	/*
229db09bef3SAndre Oppermann 	 * Cycle through IP protocols and put them into the appropriate place
230db09bef3SAndre Oppermann 	 * in ip_protox[].
231db09bef3SAndre Oppermann 	 */
232f0ffb944SJulian Elischer 	for (pr = inetdomain.dom_protosw;
233f0ffb944SJulian Elischer 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
234df8bae1dSRodney W. Grimes 		if (pr->pr_domain->dom_family == PF_INET &&
235db09bef3SAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
236db09bef3SAndre Oppermann 			/* Be careful to only index valid IP protocols. */
237db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX)
238df8bae1dSRodney W. Grimes 				ip_protox[pr->pr_protocol] = pr - inetsw;
239db09bef3SAndre Oppermann 		}
240194a213eSAndrey A. Chernov 
241c21fd232SAndre Oppermann 	/* Initialize packet filter hooks. */
242134ea224SSam Leffler 	inet_pfil_hook.ph_type = PFIL_TYPE_AF;
243134ea224SSam Leffler 	inet_pfil_hook.ph_af = AF_INET;
244134ea224SSam Leffler 	if ((i = pfil_head_register(&inet_pfil_hook)) != 0)
245134ea224SSam Leffler 		printf("%s: WARNING: unable to register pfil hook, "
246134ea224SSam Leffler 			"error %d\n", __func__, i);
247134ea224SSam Leffler 
248db09bef3SAndre Oppermann 	/* Initialize IP reassembly queue. */
2492fad1e93SSam Leffler 	IPQ_LOCK_INIT();
250194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++)
251462b86feSPoul-Henning Kamp 	    TAILQ_INIT(&ipq[i]);
252375386e2SMike Silbersack 	maxnipq = nmbclusters / 32;
253375386e2SMike Silbersack 	maxfragsperpacket = 16;
254d248c7d7SRobert Watson 	ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
255d248c7d7SRobert Watson 	    NULL, UMA_ALIGN_PTR, 0);
256d248c7d7SRobert Watson 	maxnipq_update();
257194a213eSAndrey A. Chernov 
2585f311da2SMike Silbersack 	/* Start ipport_tick. */
2595f311da2SMike Silbersack 	callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
2605f311da2SMike Silbersack 	ipport_tick(NULL);
2615f311da2SMike Silbersack 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2625f311da2SMike Silbersack 		SHUTDOWN_PRI_DEFAULT);
2634f590175SPaul Saab 	EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
2644f590175SPaul Saab 		NULL, EVENTHANDLER_PRI_ANY);
2655f311da2SMike Silbersack 
266db09bef3SAndre Oppermann 	/* Initialize various other remaining things. */
267227ee8a1SPoul-Henning Kamp 	ip_id = time_second & 0xffff;
268df8bae1dSRodney W. Grimes 	ipintrq.ifq_maxlen = ipqmaxlen;
2696008862bSJohn Baldwin 	mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF);
2707902224cSSam Leffler 	netisr_register(NETISR_IP, ip_input, &ipintrq, NETISR_MPSAFE);
271df8bae1dSRodney W. Grimes }
272df8bae1dSRodney W. Grimes 
273f2565d68SRobert Watson void
274f2565d68SRobert Watson ip_fini(void *xtp)
2755f311da2SMike Silbersack {
276f2565d68SRobert Watson 
2775f311da2SMike Silbersack 	callout_stop(&ipport_tick_callout);
2785f311da2SMike Silbersack }
2795f311da2SMike Silbersack 
2804d2e3692SLuigi Rizzo /*
281df8bae1dSRodney W. Grimes  * Ip input routine.  Checksum and byte swap header.  If fragmented
282df8bae1dSRodney W. Grimes  * try to reassemble.  Process options.  Pass to next level.
283df8bae1dSRodney W. Grimes  */
284c67b1d17SGarrett Wollman void
285c67b1d17SGarrett Wollman ip_input(struct mbuf *m)
286df8bae1dSRodney W. Grimes {
2879188b4a1SAndre Oppermann 	struct ip *ip = NULL;
2885da9f8faSJosef Karthauser 	struct in_ifaddr *ia = NULL;
289ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
2909b932e9eSAndre Oppermann 	int    checkif, hlen = 0;
29147c861ecSBrian Somers 	u_short sum;
29202c1c707SAndre Oppermann 	int dchg = 0;				/* dest changed after fw */
293f51f805fSSam Leffler 	struct in_addr odst;			/* original dst address */
294b715f178SLuigi Rizzo 
295fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
296db40007dSAndrew R. Reiter 
297ac9d7e26SMax Laier 	if (m->m_flags & M_FASTFWD_OURS) {
2989b932e9eSAndre Oppermann 		/*
29976ff6dcfSAndre Oppermann 		 * Firewall or NAT changed destination to local.
30076ff6dcfSAndre Oppermann 		 * We expect ip_len and ip_off to be in host byte order.
3019b932e9eSAndre Oppermann 		 */
30276ff6dcfSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
30376ff6dcfSAndre Oppermann 		/* Set up some basics that will be used later. */
3042b25acc1SLuigi Rizzo 		ip = mtod(m, struct ip *);
30553be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
3069b932e9eSAndre Oppermann 		goto ours;
3072b25acc1SLuigi Rizzo 	}
3082b25acc1SLuigi Rizzo 
309df8bae1dSRodney W. Grimes 	ipstat.ips_total++;
31058938916SGarrett Wollman 
31158938916SGarrett Wollman 	if (m->m_pkthdr.len < sizeof(struct ip))
31258938916SGarrett Wollman 		goto tooshort;
31358938916SGarrett Wollman 
314df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct ip) &&
3150b17fba7SAndre Oppermann 	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
316df8bae1dSRodney W. Grimes 		ipstat.ips_toosmall++;
317c67b1d17SGarrett Wollman 		return;
318df8bae1dSRodney W. Grimes 	}
319df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
32058938916SGarrett Wollman 
32153be11f6SPoul-Henning Kamp 	if (ip->ip_v != IPVERSION) {
322df8bae1dSRodney W. Grimes 		ipstat.ips_badvers++;
323df8bae1dSRodney W. Grimes 		goto bad;
324df8bae1dSRodney W. Grimes 	}
32558938916SGarrett Wollman 
32653be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
327df8bae1dSRodney W. Grimes 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
328df8bae1dSRodney W. Grimes 		ipstat.ips_badhlen++;
329df8bae1dSRodney W. Grimes 		goto bad;
330df8bae1dSRodney W. Grimes 	}
331df8bae1dSRodney W. Grimes 	if (hlen > m->m_len) {
3320b17fba7SAndre Oppermann 		if ((m = m_pullup(m, hlen)) == NULL) {
333df8bae1dSRodney W. Grimes 			ipstat.ips_badhlen++;
334c67b1d17SGarrett Wollman 			return;
335df8bae1dSRodney W. Grimes 		}
336df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
337df8bae1dSRodney W. Grimes 	}
33833841545SHajimu UMEMOTO 
33933841545SHajimu UMEMOTO 	/* 127/8 must not appear on wire - RFC1122 */
34033841545SHajimu UMEMOTO 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
34133841545SHajimu UMEMOTO 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
34233841545SHajimu UMEMOTO 		if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
34333841545SHajimu UMEMOTO 			ipstat.ips_badaddr++;
34433841545SHajimu UMEMOTO 			goto bad;
34533841545SHajimu UMEMOTO 		}
34633841545SHajimu UMEMOTO 	}
34733841545SHajimu UMEMOTO 
348db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
349db4f9cc7SJonathan Lemon 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
350db4f9cc7SJonathan Lemon 	} else {
35158938916SGarrett Wollman 		if (hlen == sizeof(struct ip)) {
35247c861ecSBrian Somers 			sum = in_cksum_hdr(ip);
35358938916SGarrett Wollman 		} else {
35447c861ecSBrian Somers 			sum = in_cksum(m, hlen);
35558938916SGarrett Wollman 		}
356db4f9cc7SJonathan Lemon 	}
35747c861ecSBrian Somers 	if (sum) {
358df8bae1dSRodney W. Grimes 		ipstat.ips_badsum++;
359df8bae1dSRodney W. Grimes 		goto bad;
360df8bae1dSRodney W. Grimes 	}
361df8bae1dSRodney W. Grimes 
36202b199f1SMax Laier #ifdef ALTQ
36302b199f1SMax Laier 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
36402b199f1SMax Laier 		/* packet is dropped by traffic conditioner */
36502b199f1SMax Laier 		return;
36602b199f1SMax Laier #endif
36702b199f1SMax Laier 
368df8bae1dSRodney W. Grimes 	/*
369df8bae1dSRodney W. Grimes 	 * Convert fields to host representation.
370df8bae1dSRodney W. Grimes 	 */
371fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
372df8bae1dSRodney W. Grimes 	if (ip->ip_len < hlen) {
373df8bae1dSRodney W. Grimes 		ipstat.ips_badlen++;
374df8bae1dSRodney W. Grimes 		goto bad;
375df8bae1dSRodney W. Grimes 	}
376fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
377df8bae1dSRodney W. Grimes 
378df8bae1dSRodney W. Grimes 	/*
379df8bae1dSRodney W. Grimes 	 * Check that the amount of data in the buffers
380df8bae1dSRodney W. Grimes 	 * is as at least much as the IP header would have us expect.
381df8bae1dSRodney W. Grimes 	 * Trim mbufs if longer than we expect.
382df8bae1dSRodney W. Grimes 	 * Drop packet if shorter than we expect.
383df8bae1dSRodney W. Grimes 	 */
384df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len < ip->ip_len) {
38558938916SGarrett Wollman tooshort:
386df8bae1dSRodney W. Grimes 		ipstat.ips_tooshort++;
387df8bae1dSRodney W. Grimes 		goto bad;
388df8bae1dSRodney W. Grimes 	}
389df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len > ip->ip_len) {
390df8bae1dSRodney W. Grimes 		if (m->m_len == m->m_pkthdr.len) {
391df8bae1dSRodney W. Grimes 			m->m_len = ip->ip_len;
392df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = ip->ip_len;
393df8bae1dSRodney W. Grimes 		} else
394df8bae1dSRodney W. Grimes 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
395df8bae1dSRodney W. Grimes 	}
396b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
39714dd6717SSam Leffler 	/*
39814dd6717SSam Leffler 	 * Bypass packet filtering for packets from a tunnel (gif).
39914dd6717SSam Leffler 	 */
400cc977adcSBjoern A. Zeeb 	if (ip_ipsec_filtertunnel(m))
401c21fd232SAndre Oppermann 		goto passin;
402b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
4033f67c834SDon Lewis 
404c4ac87eaSDarren Reed 	/*
405134ea224SSam Leffler 	 * Run through list of hooks for input packets.
406f51f805fSSam Leffler 	 *
407f51f805fSSam Leffler 	 * NB: Beware of the destination address changing (e.g.
408f51f805fSSam Leffler 	 *     by NAT rewriting).  When this happens, tell
409f51f805fSSam Leffler 	 *     ip_forward to do the right thing.
410c4ac87eaSDarren Reed 	 */
411c21fd232SAndre Oppermann 
412c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
413604afec4SChristian S.J. Peron 	if (!PFIL_HOOKED(&inet_pfil_hook))
414c21fd232SAndre Oppermann 		goto passin;
415c21fd232SAndre Oppermann 
416f51f805fSSam Leffler 	odst = ip->ip_dst;
417134ea224SSam Leffler 	if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
418d6a8d588SMax Laier 	    PFIL_IN, NULL) != 0)
419beec8214SDarren Reed 		return;
420134ea224SSam Leffler 	if (m == NULL)			/* consumed by filter */
421c4ac87eaSDarren Reed 		return;
4229b932e9eSAndre Oppermann 
423c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
42402c1c707SAndre Oppermann 	dchg = (odst.s_addr != ip->ip_dst.s_addr);
4259b932e9eSAndre Oppermann 
4269b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
4279b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
4289b932e9eSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
4299b932e9eSAndre Oppermann 		goto ours;
4309b932e9eSAndre Oppermann 	}
431099dd043SAndre Oppermann 	if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) {
432099dd043SAndre Oppermann 		/*
433099dd043SAndre Oppermann 		 * Directly ship on the packet.  This allows to forward packets
434099dd043SAndre Oppermann 		 * that were destined for us to some other directly connected
435099dd043SAndre Oppermann 		 * host.
436099dd043SAndre Oppermann 		 */
437099dd043SAndre Oppermann 		ip_forward(m, dchg);
438099dd043SAndre Oppermann 		return;
439099dd043SAndre Oppermann 	}
4409b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
4419b932e9eSAndre Oppermann 
442c21fd232SAndre Oppermann passin:
443df8bae1dSRodney W. Grimes 	/*
444df8bae1dSRodney W. Grimes 	 * Process options and, if not destined for us,
445df8bae1dSRodney W. Grimes 	 * ship it on.  ip_dooptions returns 1 when an
446df8bae1dSRodney W. Grimes 	 * error was detected (causing an icmp message
447df8bae1dSRodney W. Grimes 	 * to be sent and the original packet to be freed).
448df8bae1dSRodney W. Grimes 	 */
4499b932e9eSAndre Oppermann 	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
450c67b1d17SGarrett Wollman 		return;
451df8bae1dSRodney W. Grimes 
452f0068c4aSGarrett Wollman         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
453f0068c4aSGarrett Wollman          * matter if it is destined to another node, or whether it is
454f0068c4aSGarrett Wollman          * a multicast one, RSVP wants it! and prevents it from being forwarded
455f0068c4aSGarrett Wollman          * anywhere else. Also checks if the rsvp daemon is running before
456f0068c4aSGarrett Wollman 	 * grabbing the packet.
457f0068c4aSGarrett Wollman          */
4581c5de19aSGarrett Wollman 	if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
459f0068c4aSGarrett Wollman 		goto ours;
460f0068c4aSGarrett Wollman 
461df8bae1dSRodney W. Grimes 	/*
462df8bae1dSRodney W. Grimes 	 * Check our list of addresses, to see if the packet is for us.
463cc766e04SGarrett Wollman 	 * If we don't have any addresses, assume any unicast packet
464cc766e04SGarrett Wollman 	 * we receive might be for us (and let the upper layers deal
465cc766e04SGarrett Wollman 	 * with it).
466df8bae1dSRodney W. Grimes 	 */
467cc766e04SGarrett Wollman 	if (TAILQ_EMPTY(&in_ifaddrhead) &&
468cc766e04SGarrett Wollman 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
469cc766e04SGarrett Wollman 		goto ours;
470cc766e04SGarrett Wollman 
4717538a9a0SJonathan Lemon 	/*
472823db0e9SDon Lewis 	 * Enable a consistency check between the destination address
473823db0e9SDon Lewis 	 * and the arrival interface for a unicast packet (the RFC 1122
474823db0e9SDon Lewis 	 * strong ES model) if IP forwarding is disabled and the packet
475e15ae1b2SDon Lewis 	 * is not locally generated and the packet is not subject to
476e15ae1b2SDon Lewis 	 * 'ipfw fwd'.
4773f67c834SDon Lewis 	 *
4783f67c834SDon Lewis 	 * XXX - Checking also should be disabled if the destination
4793f67c834SDon Lewis 	 * address is ipnat'ed to a different interface.
4803f67c834SDon Lewis 	 *
481a8f12100SDon Lewis 	 * XXX - Checking is incompatible with IP aliases added
4823f67c834SDon Lewis 	 * to the loopback interface instead of the interface where
4833f67c834SDon Lewis 	 * the packets are received.
484a9771948SGleb Smirnoff 	 *
485a9771948SGleb Smirnoff 	 * XXX - This is the case for carp vhost IPs as well so we
486a9771948SGleb Smirnoff 	 * insert a workaround. If the packet got here, we already
487a9771948SGleb Smirnoff 	 * checked with carp_iamatch() and carp_forus().
488823db0e9SDon Lewis 	 */
489823db0e9SDon Lewis 	checkif = ip_checkinterface && (ipforwarding == 0) &&
4909494d596SBrooks Davis 	    m->m_pkthdr.rcvif != NULL &&
491e15ae1b2SDon Lewis 	    ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
492a9771948SGleb Smirnoff #ifdef DEV_CARP
493a9771948SGleb Smirnoff 	    !m->m_pkthdr.rcvif->if_carp &&
494a9771948SGleb Smirnoff #endif
4959b932e9eSAndre Oppermann 	    (dchg == 0);
496823db0e9SDon Lewis 
497ca925d9cSJonathan Lemon 	/*
498ca925d9cSJonathan Lemon 	 * Check for exact addresses in the hash bucket.
499ca925d9cSJonathan Lemon 	 */
5009b932e9eSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
501f9e354dfSJulian Elischer 		/*
502823db0e9SDon Lewis 		 * If the address matches, verify that the packet
503823db0e9SDon Lewis 		 * arrived via the correct interface if checking is
504823db0e9SDon Lewis 		 * enabled.
505f9e354dfSJulian Elischer 		 */
5069b932e9eSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
507823db0e9SDon Lewis 		    (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
508ed1ff184SJulian Elischer 			goto ours;
509ca925d9cSJonathan Lemon 	}
510823db0e9SDon Lewis 	/*
511ca925d9cSJonathan Lemon 	 * Check for broadcast addresses.
512ca925d9cSJonathan Lemon 	 *
513ca925d9cSJonathan Lemon 	 * Only accept broadcast packets that arrive via the matching
514ca925d9cSJonathan Lemon 	 * interface.  Reception of forwarded directed broadcasts would
515ca925d9cSJonathan Lemon 	 * be handled via ip_forward() and ether_output() with the loopback
516ca925d9cSJonathan Lemon 	 * into the stack for SIMPLEX interfaces handled by ether_output().
517823db0e9SDon Lewis 	 */
5184f450ff9SBruce M Simpson 	if (m->m_pkthdr.rcvif != NULL &&
5194f450ff9SBruce M Simpson 	    m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
520ca925d9cSJonathan Lemon 	        TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
521ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
522ca925d9cSJonathan Lemon 				continue;
523ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
524df8bae1dSRodney W. Grimes 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
5259b932e9eSAndre Oppermann 			    ip->ip_dst.s_addr)
526df8bae1dSRodney W. Grimes 				goto ours;
5279b932e9eSAndre Oppermann 			if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr)
528df8bae1dSRodney W. Grimes 				goto ours;
5290ac40133SBrian Somers #ifdef BOOTP_COMPAT
530ca925d9cSJonathan Lemon 			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
531ca925d9cSJonathan Lemon 				goto ours;
5320ac40133SBrian Somers #endif
533df8bae1dSRodney W. Grimes 		}
534df8bae1dSRodney W. Grimes 	}
535f8429ca2SBruce M Simpson 	/* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
536f8429ca2SBruce M Simpson 	if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
537f8429ca2SBruce M Simpson 		ipstat.ips_cantforward++;
538f8429ca2SBruce M Simpson 		m_freem(m);
539f8429ca2SBruce M Simpson 		return;
540f8429ca2SBruce M Simpson 	}
541df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
542df8bae1dSRodney W. Grimes 		struct in_multi *inm;
543df8bae1dSRodney W. Grimes 		if (ip_mrouter) {
544df8bae1dSRodney W. Grimes 			/*
545df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, all
546df8bae1dSRodney W. Grimes 			 * incoming multicast packets are passed to the
547df8bae1dSRodney W. Grimes 			 * kernel-level multicast forwarding function.
548df8bae1dSRodney W. Grimes 			 * The packet is returned (relatively) intact; if
549df8bae1dSRodney W. Grimes 			 * ip_mforward() returns a non-zero value, the packet
550df8bae1dSRodney W. Grimes 			 * must be discarded, else it may be accepted below.
551df8bae1dSRodney W. Grimes 			 */
552bbb4330bSLuigi Rizzo 			if (ip_mforward &&
553bbb4330bSLuigi Rizzo 			    ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
554df8bae1dSRodney W. Grimes 				ipstat.ips_cantforward++;
555df8bae1dSRodney W. Grimes 				m_freem(m);
556c67b1d17SGarrett Wollman 				return;
557df8bae1dSRodney W. Grimes 			}
558df8bae1dSRodney W. Grimes 
559df8bae1dSRodney W. Grimes 			/*
56011612afaSDima Dorfman 			 * The process-level routing daemon needs to receive
561df8bae1dSRodney W. Grimes 			 * all multicast IGMP packets, whether or not this
562df8bae1dSRodney W. Grimes 			 * host belongs to their destination groups.
563df8bae1dSRodney W. Grimes 			 */
564df8bae1dSRodney W. Grimes 			if (ip->ip_p == IPPROTO_IGMP)
565df8bae1dSRodney W. Grimes 				goto ours;
566df8bae1dSRodney W. Grimes 			ipstat.ips_forward++;
567df8bae1dSRodney W. Grimes 		}
568df8bae1dSRodney W. Grimes 		/*
569df8bae1dSRodney W. Grimes 		 * See if we belong to the destination multicast group on the
570df8bae1dSRodney W. Grimes 		 * arrival interface.
571df8bae1dSRodney W. Grimes 		 */
572dd5a318bSRobert Watson 		IN_MULTI_LOCK();
573df8bae1dSRodney W. Grimes 		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
574dd5a318bSRobert Watson 		IN_MULTI_UNLOCK();
575df8bae1dSRodney W. Grimes 		if (inm == NULL) {
57682c39223SGarrett Wollman 			ipstat.ips_notmember++;
577df8bae1dSRodney W. Grimes 			m_freem(m);
578c67b1d17SGarrett Wollman 			return;
579df8bae1dSRodney W. Grimes 		}
580df8bae1dSRodney W. Grimes 		goto ours;
581df8bae1dSRodney W. Grimes 	}
582df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
583df8bae1dSRodney W. Grimes 		goto ours;
584df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == INADDR_ANY)
585df8bae1dSRodney W. Grimes 		goto ours;
586df8bae1dSRodney W. Grimes 
5876a800098SYoshinobu Inoue 	/*
5886a800098SYoshinobu Inoue 	 * FAITH(Firewall Aided Internet Translator)
5896a800098SYoshinobu Inoue 	 */
5906a800098SYoshinobu Inoue 	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
5916a800098SYoshinobu Inoue 		if (ip_keepfaith) {
5926a800098SYoshinobu Inoue 			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
5936a800098SYoshinobu Inoue 				goto ours;
5946a800098SYoshinobu Inoue 		}
5956a800098SYoshinobu Inoue 		m_freem(m);
5966a800098SYoshinobu Inoue 		return;
5976a800098SYoshinobu Inoue 	}
5989494d596SBrooks Davis 
599df8bae1dSRodney W. Grimes 	/*
600df8bae1dSRodney W. Grimes 	 * Not for us; forward if possible and desirable.
601df8bae1dSRodney W. Grimes 	 */
602df8bae1dSRodney W. Grimes 	if (ipforwarding == 0) {
603df8bae1dSRodney W. Grimes 		ipstat.ips_cantforward++;
604df8bae1dSRodney W. Grimes 		m_freem(m);
605546f251bSChris D. Faulhaber 	} else {
606b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
6071dfcf0d2SAndre Oppermann 		if (ip_ipsec_fwd(m))
608546f251bSChris D. Faulhaber 			goto bad;
609b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
6109b932e9eSAndre Oppermann 		ip_forward(m, dchg);
611546f251bSChris D. Faulhaber 	}
612c67b1d17SGarrett Wollman 	return;
613df8bae1dSRodney W. Grimes 
614df8bae1dSRodney W. Grimes ours:
615d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH
616d0ebc0d2SYaroslav Tykhiy 	/*
617d0ebc0d2SYaroslav Tykhiy 	 * IPSTEALTH: Process non-routing options only
618d0ebc0d2SYaroslav Tykhiy 	 * if the packet is destined for us.
619d0ebc0d2SYaroslav Tykhiy 	 */
6202b25acc1SLuigi Rizzo 	if (ipstealth && hlen > sizeof (struct ip) &&
6219b932e9eSAndre Oppermann 	    ip_dooptions(m, 1))
622d0ebc0d2SYaroslav Tykhiy 		return;
623d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */
624d0ebc0d2SYaroslav Tykhiy 
6255da9f8faSJosef Karthauser 	/* Count the packet in the ip address stats */
6265da9f8faSJosef Karthauser 	if (ia != NULL) {
6275da9f8faSJosef Karthauser 		ia->ia_ifa.if_ipackets++;
6285da9f8faSJosef Karthauser 		ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
6295da9f8faSJosef Karthauser 	}
630100ba1a6SJordan K. Hubbard 
63163f8d699SJordan K. Hubbard 	/*
632b6ea1aa5SRuslan Ermilov 	 * Attempt reassembly; if it succeeds, proceed.
633ac9d7e26SMax Laier 	 * ip_reass() will return a different mbuf.
634df8bae1dSRodney W. Grimes 	 */
635f0cada84SAndre Oppermann 	if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
636f0cada84SAndre Oppermann 		m = ip_reass(m);
637f0cada84SAndre Oppermann 		if (m == NULL)
638c67b1d17SGarrett Wollman 			return;
6396a800098SYoshinobu Inoue 		ip = mtod(m, struct ip *);
6407e2df452SRuslan Ermilov 		/* Get the header length of the reassembled packet */
64153be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
642f0cada84SAndre Oppermann 	}
643f0cada84SAndre Oppermann 
644f0cada84SAndre Oppermann 	/*
645f0cada84SAndre Oppermann 	 * Further protocols expect the packet length to be w/o the
646f0cada84SAndre Oppermann 	 * IP header.
647f0cada84SAndre Oppermann 	 */
648df8bae1dSRodney W. Grimes 	ip->ip_len -= hlen;
649df8bae1dSRodney W. Grimes 
650b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
65133841545SHajimu UMEMOTO 	/*
65233841545SHajimu UMEMOTO 	 * enforce IPsec policy checking if we are seeing last header.
65333841545SHajimu UMEMOTO 	 * note that we do not visit this with protocols with pcb layer
65433841545SHajimu UMEMOTO 	 * code - like udp/tcp/raw ip.
65533841545SHajimu UMEMOTO 	 */
6561dfcf0d2SAndre Oppermann 	if (ip_ipsec_input(m))
65733841545SHajimu UMEMOTO 		goto bad;
658b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
65933841545SHajimu UMEMOTO 
660df8bae1dSRodney W. Grimes 	/*
661df8bae1dSRodney W. Grimes 	 * Switch out to protocol's input routine.
662df8bae1dSRodney W. Grimes 	 */
663df8bae1dSRodney W. Grimes 	ipstat.ips_delivered++;
6649b932e9eSAndre Oppermann 
6652b25acc1SLuigi Rizzo 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
666c67b1d17SGarrett Wollman 	return;
667df8bae1dSRodney W. Grimes bad:
668df8bae1dSRodney W. Grimes 	m_freem(m);
669c67b1d17SGarrett Wollman }
670c67b1d17SGarrett Wollman 
671c67b1d17SGarrett Wollman /*
672d248c7d7SRobert Watson  * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
673d248c7d7SRobert Watson  * max has slightly different semantics than the sysctl, for historical
674d248c7d7SRobert Watson  * reasons.
675d248c7d7SRobert Watson  */
676d248c7d7SRobert Watson static void
677d248c7d7SRobert Watson maxnipq_update(void)
678d248c7d7SRobert Watson {
679d248c7d7SRobert Watson 
680d248c7d7SRobert Watson 	/*
681d248c7d7SRobert Watson 	 * -1 for unlimited allocation.
682d248c7d7SRobert Watson 	 */
683d248c7d7SRobert Watson 	if (maxnipq < 0)
684d248c7d7SRobert Watson 		uma_zone_set_max(ipq_zone, 0);
685d248c7d7SRobert Watson 	/*
686d248c7d7SRobert Watson 	 * Positive number for specific bound.
687d248c7d7SRobert Watson 	 */
688d248c7d7SRobert Watson 	if (maxnipq > 0)
689d248c7d7SRobert Watson 		uma_zone_set_max(ipq_zone, maxnipq);
690d248c7d7SRobert Watson 	/*
691d248c7d7SRobert Watson 	 * Zero specifies no further fragment queue allocation -- set the
692d248c7d7SRobert Watson 	 * bound very low, but rely on implementation elsewhere to actually
693d248c7d7SRobert Watson 	 * prevent allocation and reclaim current queues.
694d248c7d7SRobert Watson 	 */
695d248c7d7SRobert Watson 	if (maxnipq == 0)
696d248c7d7SRobert Watson 		uma_zone_set_max(ipq_zone, 1);
697d248c7d7SRobert Watson }
698d248c7d7SRobert Watson 
6994f590175SPaul Saab static void
7004f590175SPaul Saab ipq_zone_change(void *tag)
7014f590175SPaul Saab {
7024f590175SPaul Saab 
7034f590175SPaul Saab 	if (maxnipq > 0 && maxnipq < (nmbclusters / 32)) {
7044f590175SPaul Saab 		maxnipq = nmbclusters / 32;
7054f590175SPaul Saab 		maxnipq_update();
7064f590175SPaul Saab 	}
7074f590175SPaul Saab }
7084f590175SPaul Saab 
709d248c7d7SRobert Watson static int
710d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
711d248c7d7SRobert Watson {
712d248c7d7SRobert Watson 	int error, i;
713d248c7d7SRobert Watson 
714d248c7d7SRobert Watson 	i = maxnipq;
715d248c7d7SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
716d248c7d7SRobert Watson 	if (error || !req->newptr)
717d248c7d7SRobert Watson 		return (error);
718d248c7d7SRobert Watson 
719d248c7d7SRobert Watson 	/*
720d248c7d7SRobert Watson 	 * XXXRW: Might be a good idea to sanity check the argument and place
721d248c7d7SRobert Watson 	 * an extreme upper bound.
722d248c7d7SRobert Watson 	 */
723d248c7d7SRobert Watson 	if (i < -1)
724d248c7d7SRobert Watson 		return (EINVAL);
725d248c7d7SRobert Watson 	maxnipq = i;
726d248c7d7SRobert Watson 	maxnipq_update();
727d248c7d7SRobert Watson 	return (0);
728d248c7d7SRobert Watson }
729d248c7d7SRobert Watson 
730d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
731d248c7d7SRobert Watson     NULL, 0, sysctl_maxnipq, "I",
732d248c7d7SRobert Watson     "Maximum number of IPv4 fragment reassembly queue entries");
733d248c7d7SRobert Watson 
734d248c7d7SRobert Watson /*
7358948e4baSArchie Cobbs  * Take incoming datagram fragment and try to reassemble it into
736f0cada84SAndre Oppermann  * whole datagram.  If the argument is the first fragment or one
737f0cada84SAndre Oppermann  * in between the function will return NULL and store the mbuf
738f0cada84SAndre Oppermann  * in the fragment chain.  If the argument is the last fragment
739f0cada84SAndre Oppermann  * the packet will be reassembled and the pointer to the new
740f0cada84SAndre Oppermann  * mbuf returned for further processing.  Only m_tags attached
741f0cada84SAndre Oppermann  * to the first packet/fragment are preserved.
742f0cada84SAndre Oppermann  * The IP header is *NOT* adjusted out of iplen.
743df8bae1dSRodney W. Grimes  */
744f0cada84SAndre Oppermann struct mbuf *
745f0cada84SAndre Oppermann ip_reass(struct mbuf *m)
746df8bae1dSRodney W. Grimes {
747f0cada84SAndre Oppermann 	struct ip *ip;
748f0cada84SAndre Oppermann 	struct mbuf *p, *q, *nq, *t;
749f0cada84SAndre Oppermann 	struct ipq *fp = NULL;
750f0cada84SAndre Oppermann 	struct ipqhead *head;
751f0cada84SAndre Oppermann 	int i, hlen, next;
75259dfcba4SHajimu UMEMOTO 	u_int8_t ecn, ecn0;
753f0cada84SAndre Oppermann 	u_short hash;
754df8bae1dSRodney W. Grimes 
755800af1fbSMaxim Konovalov 	/* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
756800af1fbSMaxim Konovalov 	if (maxnipq == 0 || maxfragsperpacket == 0) {
757f0cada84SAndre Oppermann 		ipstat.ips_fragments++;
758f0cada84SAndre Oppermann 		ipstat.ips_fragdropped++;
7599d804f81SAndre Oppermann 		m_freem(m);
7609d804f81SAndre Oppermann 		return (NULL);
761f0cada84SAndre Oppermann 	}
7622fad1e93SSam Leffler 
763f0cada84SAndre Oppermann 	ip = mtod(m, struct ip *);
764f0cada84SAndre Oppermann 	hlen = ip->ip_hl << 2;
765f0cada84SAndre Oppermann 
766f0cada84SAndre Oppermann 	hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
767f0cada84SAndre Oppermann 	head = &ipq[hash];
768f0cada84SAndre Oppermann 	IPQ_LOCK();
769f0cada84SAndre Oppermann 
770f0cada84SAndre Oppermann 	/*
771f0cada84SAndre Oppermann 	 * Look for queue of fragments
772f0cada84SAndre Oppermann 	 * of this datagram.
773f0cada84SAndre Oppermann 	 */
774f0cada84SAndre Oppermann 	TAILQ_FOREACH(fp, head, ipq_list)
775f0cada84SAndre Oppermann 		if (ip->ip_id == fp->ipq_id &&
776f0cada84SAndre Oppermann 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
777f0cada84SAndre Oppermann 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
778f0cada84SAndre Oppermann #ifdef MAC
77930d239bcSRobert Watson 		    mac_ipq_match(m, fp) &&
780f0cada84SAndre Oppermann #endif
781f0cada84SAndre Oppermann 		    ip->ip_p == fp->ipq_p)
782f0cada84SAndre Oppermann 			goto found;
783f0cada84SAndre Oppermann 
784f0cada84SAndre Oppermann 	fp = NULL;
785f0cada84SAndre Oppermann 
786f0cada84SAndre Oppermann 	/*
787d248c7d7SRobert Watson 	 * Attempt to trim the number of allocated fragment queues if it
788d248c7d7SRobert Watson 	 * exceeds the administrative limit.
789f0cada84SAndre Oppermann 	 */
790f0cada84SAndre Oppermann 	if ((nipq > maxnipq) && (maxnipq > 0)) {
791f0cada84SAndre Oppermann 		/*
792f0cada84SAndre Oppermann 		 * drop something from the tail of the current queue
793f0cada84SAndre Oppermann 		 * before proceeding further
794f0cada84SAndre Oppermann 		 */
795f0cada84SAndre Oppermann 		struct ipq *q = TAILQ_LAST(head, ipqhead);
796f0cada84SAndre Oppermann 		if (q == NULL) {   /* gak */
797f0cada84SAndre Oppermann 			for (i = 0; i < IPREASS_NHASH; i++) {
798f0cada84SAndre Oppermann 				struct ipq *r = TAILQ_LAST(&ipq[i], ipqhead);
799f0cada84SAndre Oppermann 				if (r) {
800f0cada84SAndre Oppermann 					ipstat.ips_fragtimeout += r->ipq_nfrags;
801f0cada84SAndre Oppermann 					ip_freef(&ipq[i], r);
802f0cada84SAndre Oppermann 					break;
803f0cada84SAndre Oppermann 				}
804f0cada84SAndre Oppermann 			}
805f0cada84SAndre Oppermann 		} else {
806f0cada84SAndre Oppermann 			ipstat.ips_fragtimeout += q->ipq_nfrags;
807f0cada84SAndre Oppermann 			ip_freef(head, q);
808f0cada84SAndre Oppermann 		}
809f0cada84SAndre Oppermann 	}
810f0cada84SAndre Oppermann 
811f0cada84SAndre Oppermann found:
812f0cada84SAndre Oppermann 	/*
813f0cada84SAndre Oppermann 	 * Adjust ip_len to not reflect header,
814f0cada84SAndre Oppermann 	 * convert offset of this to bytes.
815f0cada84SAndre Oppermann 	 */
816f0cada84SAndre Oppermann 	ip->ip_len -= hlen;
817f0cada84SAndre Oppermann 	if (ip->ip_off & IP_MF) {
818f0cada84SAndre Oppermann 		/*
819f0cada84SAndre Oppermann 		 * Make sure that fragments have a data length
820f0cada84SAndre Oppermann 		 * that's a non-zero multiple of 8 bytes.
821f0cada84SAndre Oppermann 		 */
822f0cada84SAndre Oppermann 		if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
823f0cada84SAndre Oppermann 			ipstat.ips_toosmall++; /* XXX */
824f0cada84SAndre Oppermann 			goto dropfrag;
825f0cada84SAndre Oppermann 		}
826f0cada84SAndre Oppermann 		m->m_flags |= M_FRAG;
827f0cada84SAndre Oppermann 	} else
828f0cada84SAndre Oppermann 		m->m_flags &= ~M_FRAG;
829f0cada84SAndre Oppermann 	ip->ip_off <<= 3;
830f0cada84SAndre Oppermann 
831f0cada84SAndre Oppermann 
832f0cada84SAndre Oppermann 	/*
833f0cada84SAndre Oppermann 	 * Attempt reassembly; if it succeeds, proceed.
834f0cada84SAndre Oppermann 	 * ip_reass() will return a different mbuf.
835f0cada84SAndre Oppermann 	 */
836f0cada84SAndre Oppermann 	ipstat.ips_fragments++;
837f0cada84SAndre Oppermann 	m->m_pkthdr.header = ip;
838f0cada84SAndre Oppermann 
839f0cada84SAndre Oppermann 	/* Previous ip_reass() started here. */
840df8bae1dSRodney W. Grimes 	/*
841df8bae1dSRodney W. Grimes 	 * Presence of header sizes in mbufs
842df8bae1dSRodney W. Grimes 	 * would confuse code below.
843df8bae1dSRodney W. Grimes 	 */
844df8bae1dSRodney W. Grimes 	m->m_data += hlen;
845df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
846df8bae1dSRodney W. Grimes 
847df8bae1dSRodney W. Grimes 	/*
848df8bae1dSRodney W. Grimes 	 * If first fragment to arrive, create a reassembly queue.
849df8bae1dSRodney W. Grimes 	 */
850042bbfa3SRobert Watson 	if (fp == NULL) {
851d248c7d7SRobert Watson 		fp = uma_zalloc(ipq_zone, M_NOWAIT);
852d248c7d7SRobert Watson 		if (fp == NULL)
853df8bae1dSRodney W. Grimes 			goto dropfrag;
85436b0360bSRobert Watson #ifdef MAC
85530d239bcSRobert Watson 		if (mac_ipq_init(fp, M_NOWAIT) != 0) {
856d248c7d7SRobert Watson 			uma_zfree(ipq_zone, fp);
8571d7d0bfeSPawel Jakub Dawidek 			fp = NULL;
8585e7ce478SRobert Watson 			goto dropfrag;
8595e7ce478SRobert Watson 		}
86030d239bcSRobert Watson 		mac_ipq_create(m, fp);
86136b0360bSRobert Watson #endif
862462b86feSPoul-Henning Kamp 		TAILQ_INSERT_HEAD(head, fp, ipq_list);
863194a213eSAndrey A. Chernov 		nipq++;
864375386e2SMike Silbersack 		fp->ipq_nfrags = 1;
865df8bae1dSRodney W. Grimes 		fp->ipq_ttl = IPFRAGTTL;
866df8bae1dSRodney W. Grimes 		fp->ipq_p = ip->ip_p;
867df8bae1dSRodney W. Grimes 		fp->ipq_id = ip->ip_id;
8686effc713SDoug Rabson 		fp->ipq_src = ip->ip_src;
8696effc713SDoug Rabson 		fp->ipq_dst = ip->ip_dst;
870af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
871af38c68cSLuigi Rizzo 		m->m_nextpkt = NULL;
872800af1fbSMaxim Konovalov 		goto done;
87336b0360bSRobert Watson 	} else {
874375386e2SMike Silbersack 		fp->ipq_nfrags++;
87536b0360bSRobert Watson #ifdef MAC
87630d239bcSRobert Watson 		mac_ipq_update(m, fp);
87736b0360bSRobert Watson #endif
878df8bae1dSRodney W. Grimes 	}
879df8bae1dSRodney W. Grimes 
8806effc713SDoug Rabson #define GETIP(m)	((struct ip*)((m)->m_pkthdr.header))
8816effc713SDoug Rabson 
882df8bae1dSRodney W. Grimes 	/*
88359dfcba4SHajimu UMEMOTO 	 * Handle ECN by comparing this segment with the first one;
88459dfcba4SHajimu UMEMOTO 	 * if CE is set, do not lose CE.
88559dfcba4SHajimu UMEMOTO 	 * drop if CE and not-ECT are mixed for the same packet.
88659dfcba4SHajimu UMEMOTO 	 */
88759dfcba4SHajimu UMEMOTO 	ecn = ip->ip_tos & IPTOS_ECN_MASK;
88859dfcba4SHajimu UMEMOTO 	ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
88959dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_CE) {
89059dfcba4SHajimu UMEMOTO 		if (ecn0 == IPTOS_ECN_NOTECT)
89159dfcba4SHajimu UMEMOTO 			goto dropfrag;
89259dfcba4SHajimu UMEMOTO 		if (ecn0 != IPTOS_ECN_CE)
89359dfcba4SHajimu UMEMOTO 			GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
89459dfcba4SHajimu UMEMOTO 	}
89559dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
89659dfcba4SHajimu UMEMOTO 		goto dropfrag;
89759dfcba4SHajimu UMEMOTO 
89859dfcba4SHajimu UMEMOTO 	/*
899df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
900df8bae1dSRodney W. Grimes 	 */
9016effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
9026effc713SDoug Rabson 		if (GETIP(q)->ip_off > ip->ip_off)
903df8bae1dSRodney W. Grimes 			break;
904df8bae1dSRodney W. Grimes 
905df8bae1dSRodney W. Grimes 	/*
906df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
907df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
908af38c68cSLuigi Rizzo 	 * segment.  If it provides all of our data, drop us, otherwise
909af38c68cSLuigi Rizzo 	 * stick new segment in the proper place.
910db4f9cc7SJonathan Lemon 	 *
911db4f9cc7SJonathan Lemon 	 * If some of the data is dropped from the the preceding
912db4f9cc7SJonathan Lemon 	 * segment, then it's checksum is invalidated.
913df8bae1dSRodney W. Grimes 	 */
9146effc713SDoug Rabson 	if (p) {
9156effc713SDoug Rabson 		i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
916df8bae1dSRodney W. Grimes 		if (i > 0) {
917df8bae1dSRodney W. Grimes 			if (i >= ip->ip_len)
918df8bae1dSRodney W. Grimes 				goto dropfrag;
9196a800098SYoshinobu Inoue 			m_adj(m, i);
920db4f9cc7SJonathan Lemon 			m->m_pkthdr.csum_flags = 0;
921df8bae1dSRodney W. Grimes 			ip->ip_off += i;
922df8bae1dSRodney W. Grimes 			ip->ip_len -= i;
923df8bae1dSRodney W. Grimes 		}
924af38c68cSLuigi Rizzo 		m->m_nextpkt = p->m_nextpkt;
925af38c68cSLuigi Rizzo 		p->m_nextpkt = m;
926af38c68cSLuigi Rizzo 	} else {
927af38c68cSLuigi Rizzo 		m->m_nextpkt = fp->ipq_frags;
928af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
929df8bae1dSRodney W. Grimes 	}
930df8bae1dSRodney W. Grimes 
931df8bae1dSRodney W. Grimes 	/*
932df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
933df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
934df8bae1dSRodney W. Grimes 	 */
9356effc713SDoug Rabson 	for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
936af38c68cSLuigi Rizzo 	     q = nq) {
937b36f5b37SMaxim Konovalov 		i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
9386effc713SDoug Rabson 		if (i < GETIP(q)->ip_len) {
9396effc713SDoug Rabson 			GETIP(q)->ip_len -= i;
9406effc713SDoug Rabson 			GETIP(q)->ip_off += i;
9416effc713SDoug Rabson 			m_adj(q, i);
942db4f9cc7SJonathan Lemon 			q->m_pkthdr.csum_flags = 0;
943df8bae1dSRodney W. Grimes 			break;
944df8bae1dSRodney W. Grimes 		}
9456effc713SDoug Rabson 		nq = q->m_nextpkt;
946af38c68cSLuigi Rizzo 		m->m_nextpkt = nq;
94799e8617dSMaxim Konovalov 		ipstat.ips_fragdropped++;
948375386e2SMike Silbersack 		fp->ipq_nfrags--;
9496effc713SDoug Rabson 		m_freem(q);
950df8bae1dSRodney W. Grimes 	}
951df8bae1dSRodney W. Grimes 
952df8bae1dSRodney W. Grimes 	/*
953375386e2SMike Silbersack 	 * Check for complete reassembly and perform frag per packet
954375386e2SMike Silbersack 	 * limiting.
955375386e2SMike Silbersack 	 *
956375386e2SMike Silbersack 	 * Frag limiting is performed here so that the nth frag has
957375386e2SMike Silbersack 	 * a chance to complete the packet before we drop the packet.
958375386e2SMike Silbersack 	 * As a result, n+1 frags are actually allowed per packet, but
959375386e2SMike Silbersack 	 * only n will ever be stored. (n = maxfragsperpacket.)
960375386e2SMike Silbersack 	 *
961df8bae1dSRodney W. Grimes 	 */
9626effc713SDoug Rabson 	next = 0;
9636effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
964375386e2SMike Silbersack 		if (GETIP(q)->ip_off != next) {
96599e8617dSMaxim Konovalov 			if (fp->ipq_nfrags > maxfragsperpacket) {
96699e8617dSMaxim Konovalov 				ipstat.ips_fragdropped += fp->ipq_nfrags;
967375386e2SMike Silbersack 				ip_freef(head, fp);
96899e8617dSMaxim Konovalov 			}
969f0cada84SAndre Oppermann 			goto done;
970375386e2SMike Silbersack 		}
9716effc713SDoug Rabson 		next += GETIP(q)->ip_len;
9726effc713SDoug Rabson 	}
9736effc713SDoug Rabson 	/* Make sure the last packet didn't have the IP_MF flag */
974375386e2SMike Silbersack 	if (p->m_flags & M_FRAG) {
97599e8617dSMaxim Konovalov 		if (fp->ipq_nfrags > maxfragsperpacket) {
97699e8617dSMaxim Konovalov 			ipstat.ips_fragdropped += fp->ipq_nfrags;
977375386e2SMike Silbersack 			ip_freef(head, fp);
97899e8617dSMaxim Konovalov 		}
979f0cada84SAndre Oppermann 		goto done;
980375386e2SMike Silbersack 	}
981df8bae1dSRodney W. Grimes 
982df8bae1dSRodney W. Grimes 	/*
983430d30d8SBill Fenner 	 * Reassembly is complete.  Make sure the packet is a sane size.
984430d30d8SBill Fenner 	 */
9856effc713SDoug Rabson 	q = fp->ipq_frags;
9866effc713SDoug Rabson 	ip = GETIP(q);
98753be11f6SPoul-Henning Kamp 	if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
988430d30d8SBill Fenner 		ipstat.ips_toolong++;
98999e8617dSMaxim Konovalov 		ipstat.ips_fragdropped += fp->ipq_nfrags;
990462b86feSPoul-Henning Kamp 		ip_freef(head, fp);
991f0cada84SAndre Oppermann 		goto done;
992430d30d8SBill Fenner 	}
993430d30d8SBill Fenner 
994430d30d8SBill Fenner 	/*
995430d30d8SBill Fenner 	 * Concatenate fragments.
996df8bae1dSRodney W. Grimes 	 */
9976effc713SDoug Rabson 	m = q;
998df8bae1dSRodney W. Grimes 	t = m->m_next;
99902410549SRobert Watson 	m->m_next = NULL;
1000df8bae1dSRodney W. Grimes 	m_cat(m, t);
10016effc713SDoug Rabson 	nq = q->m_nextpkt;
100202410549SRobert Watson 	q->m_nextpkt = NULL;
10036effc713SDoug Rabson 	for (q = nq; q != NULL; q = nq) {
10046effc713SDoug Rabson 		nq = q->m_nextpkt;
1005945aa40dSDoug Rabson 		q->m_nextpkt = NULL;
1006db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1007db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1008a8db1d93SJonathan Lemon 		m_cat(m, q);
1009df8bae1dSRodney W. Grimes 	}
10106edb555dSOleg Bulyzhin 	/*
10116edb555dSOleg Bulyzhin 	 * In order to do checksumming faster we do 'end-around carry' here
10126edb555dSOleg Bulyzhin 	 * (and not in for{} loop), though it implies we are not going to
10136edb555dSOleg Bulyzhin 	 * reassemble more than 64k fragments.
10146edb555dSOleg Bulyzhin 	 */
10156edb555dSOleg Bulyzhin 	m->m_pkthdr.csum_data =
10166edb555dSOleg Bulyzhin 	    (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16);
101736b0360bSRobert Watson #ifdef MAC
101830d239bcSRobert Watson 	mac_ipq_reassemble(fp, m);
101930d239bcSRobert Watson 	mac_ipq_destroy(fp);
102036b0360bSRobert Watson #endif
1021df8bae1dSRodney W. Grimes 
1022df8bae1dSRodney W. Grimes 	/*
1023f0cada84SAndre Oppermann 	 * Create header for new ip packet by modifying header of first
1024f0cada84SAndre Oppermann 	 * packet;  dequeue and discard fragment reassembly header.
1025df8bae1dSRodney W. Grimes 	 * Make header visible.
1026df8bae1dSRodney W. Grimes 	 */
1027f0cada84SAndre Oppermann 	ip->ip_len = (ip->ip_hl << 2) + next;
10286effc713SDoug Rabson 	ip->ip_src = fp->ipq_src;
10296effc713SDoug Rabson 	ip->ip_dst = fp->ipq_dst;
1030462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(head, fp, ipq_list);
1031194a213eSAndrey A. Chernov 	nipq--;
1032d248c7d7SRobert Watson 	uma_zfree(ipq_zone, fp);
103353be11f6SPoul-Henning Kamp 	m->m_len += (ip->ip_hl << 2);
103453be11f6SPoul-Henning Kamp 	m->m_data -= (ip->ip_hl << 2);
1035df8bae1dSRodney W. Grimes 	/* some debugging cruft by sklower, below, will go away soon */
1036a5554bf0SPoul-Henning Kamp 	if (m->m_flags & M_PKTHDR)	/* XXX this should be done elsewhere */
1037a5554bf0SPoul-Henning Kamp 		m_fixhdr(m);
1038f0cada84SAndre Oppermann 	ipstat.ips_reassembled++;
1039f0cada84SAndre Oppermann 	IPQ_UNLOCK();
10406a800098SYoshinobu Inoue 	return (m);
1041df8bae1dSRodney W. Grimes 
1042df8bae1dSRodney W. Grimes dropfrag:
1043df8bae1dSRodney W. Grimes 	ipstat.ips_fragdropped++;
1044042bbfa3SRobert Watson 	if (fp != NULL)
1045375386e2SMike Silbersack 		fp->ipq_nfrags--;
1046df8bae1dSRodney W. Grimes 	m_freem(m);
1047f0cada84SAndre Oppermann done:
1048f0cada84SAndre Oppermann 	IPQ_UNLOCK();
1049f0cada84SAndre Oppermann 	return (NULL);
10506effc713SDoug Rabson 
10516effc713SDoug Rabson #undef GETIP
1052df8bae1dSRodney W. Grimes }
1053df8bae1dSRodney W. Grimes 
1054df8bae1dSRodney W. Grimes /*
1055df8bae1dSRodney W. Grimes  * Free a fragment reassembly header and all
1056df8bae1dSRodney W. Grimes  * associated datagrams.
1057df8bae1dSRodney W. Grimes  */
10580312fbe9SPoul-Henning Kamp static void
1059f2565d68SRobert Watson ip_freef(struct ipqhead *fhp, struct ipq *fp)
1060df8bae1dSRodney W. Grimes {
1061f2565d68SRobert Watson 	struct mbuf *q;
1062df8bae1dSRodney W. Grimes 
10632fad1e93SSam Leffler 	IPQ_LOCK_ASSERT();
10642fad1e93SSam Leffler 
10656effc713SDoug Rabson 	while (fp->ipq_frags) {
10666effc713SDoug Rabson 		q = fp->ipq_frags;
10676effc713SDoug Rabson 		fp->ipq_frags = q->m_nextpkt;
10686effc713SDoug Rabson 		m_freem(q);
1069df8bae1dSRodney W. Grimes 	}
1070462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(fhp, fp, ipq_list);
1071d248c7d7SRobert Watson 	uma_zfree(ipq_zone, fp);
1072194a213eSAndrey A. Chernov 	nipq--;
1073df8bae1dSRodney W. Grimes }
1074df8bae1dSRodney W. Grimes 
1075df8bae1dSRodney W. Grimes /*
1076df8bae1dSRodney W. Grimes  * IP timer processing;
1077df8bae1dSRodney W. Grimes  * if a timer expires on a reassembly
1078df8bae1dSRodney W. Grimes  * queue, discard it.
1079df8bae1dSRodney W. Grimes  */
1080df8bae1dSRodney W. Grimes void
1081f2565d68SRobert Watson ip_slowtimo(void)
1082df8bae1dSRodney W. Grimes {
1083f2565d68SRobert Watson 	struct ipq *fp;
1084194a213eSAndrey A. Chernov 	int i;
1085df8bae1dSRodney W. Grimes 
10862fad1e93SSam Leffler 	IPQ_LOCK();
1087194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1088462b86feSPoul-Henning Kamp 		for(fp = TAILQ_FIRST(&ipq[i]); fp;) {
1089462b86feSPoul-Henning Kamp 			struct ipq *fpp;
1090462b86feSPoul-Henning Kamp 
1091462b86feSPoul-Henning Kamp 			fpp = fp;
1092462b86feSPoul-Henning Kamp 			fp = TAILQ_NEXT(fp, ipq_list);
1093462b86feSPoul-Henning Kamp 			if(--fpp->ipq_ttl == 0) {
109499e8617dSMaxim Konovalov 				ipstat.ips_fragtimeout += fpp->ipq_nfrags;
1095462b86feSPoul-Henning Kamp 				ip_freef(&ipq[i], fpp);
1096df8bae1dSRodney W. Grimes 			}
1097df8bae1dSRodney W. Grimes 		}
1098194a213eSAndrey A. Chernov 	}
1099690a6055SJesper Skriver 	/*
1100690a6055SJesper Skriver 	 * If we are over the maximum number of fragments
1101690a6055SJesper Skriver 	 * (due to the limit being lowered), drain off
1102690a6055SJesper Skriver 	 * enough to get down to the new limit.
1103690a6055SJesper Skriver 	 */
1104a75a485dSMike Silbersack 	if (maxnipq >= 0 && nipq > maxnipq) {
1105690a6055SJesper Skriver 		for (i = 0; i < IPREASS_NHASH; i++) {
1106b36f5b37SMaxim Konovalov 			while (nipq > maxnipq && !TAILQ_EMPTY(&ipq[i])) {
110799e8617dSMaxim Konovalov 				ipstat.ips_fragdropped +=
110899e8617dSMaxim Konovalov 				    TAILQ_FIRST(&ipq[i])->ipq_nfrags;
1109690a6055SJesper Skriver 				ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
1110690a6055SJesper Skriver 			}
1111690a6055SJesper Skriver 		}
1112690a6055SJesper Skriver 	}
11132fad1e93SSam Leffler 	IPQ_UNLOCK();
1114df8bae1dSRodney W. Grimes }
1115df8bae1dSRodney W. Grimes 
1116df8bae1dSRodney W. Grimes /*
1117df8bae1dSRodney W. Grimes  * Drain off all datagram fragments.
1118df8bae1dSRodney W. Grimes  */
1119df8bae1dSRodney W. Grimes void
1120f2565d68SRobert Watson ip_drain(void)
1121df8bae1dSRodney W. Grimes {
1122194a213eSAndrey A. Chernov 	int     i;
1123ce29ab3aSGarrett Wollman 
11242fad1e93SSam Leffler 	IPQ_LOCK();
1125194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1126462b86feSPoul-Henning Kamp 		while(!TAILQ_EMPTY(&ipq[i])) {
112799e8617dSMaxim Konovalov 			ipstat.ips_fragdropped +=
112899e8617dSMaxim Konovalov 			    TAILQ_FIRST(&ipq[i])->ipq_nfrags;
1129462b86feSPoul-Henning Kamp 			ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
1130194a213eSAndrey A. Chernov 		}
1131194a213eSAndrey A. Chernov 	}
11322fad1e93SSam Leffler 	IPQ_UNLOCK();
1133ce29ab3aSGarrett Wollman 	in_rtqdrain();
1134df8bae1dSRodney W. Grimes }
1135df8bae1dSRodney W. Grimes 
1136df8bae1dSRodney W. Grimes /*
1137de38924dSAndre Oppermann  * The protocol to be inserted into ip_protox[] must be already registered
1138de38924dSAndre Oppermann  * in inetsw[], either statically or through pf_proto_register().
1139de38924dSAndre Oppermann  */
1140de38924dSAndre Oppermann int
1141de38924dSAndre Oppermann ipproto_register(u_char ipproto)
1142de38924dSAndre Oppermann {
1143de38924dSAndre Oppermann 	struct protosw *pr;
1144de38924dSAndre Oppermann 
1145de38924dSAndre Oppermann 	/* Sanity checks. */
1146de38924dSAndre Oppermann 	if (ipproto == 0)
1147de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1148de38924dSAndre Oppermann 
1149de38924dSAndre Oppermann 	/*
1150de38924dSAndre Oppermann 	 * The protocol slot must not be occupied by another protocol
1151de38924dSAndre Oppermann 	 * already.  An index pointing to IPPROTO_RAW is unused.
1152de38924dSAndre Oppermann 	 */
1153de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1154de38924dSAndre Oppermann 	if (pr == NULL)
1155de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1156de38924dSAndre Oppermann 	if (ip_protox[ipproto] != pr - inetsw)	/* IPPROTO_RAW */
1157de38924dSAndre Oppermann 		return (EEXIST);
1158de38924dSAndre Oppermann 
1159de38924dSAndre Oppermann 	/* Find the protocol position in inetsw[] and set the index. */
1160de38924dSAndre Oppermann 	for (pr = inetdomain.dom_protosw;
1161de38924dSAndre Oppermann 	     pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1162de38924dSAndre Oppermann 		if (pr->pr_domain->dom_family == PF_INET &&
1163de38924dSAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol == ipproto) {
1164de38924dSAndre Oppermann 			/* Be careful to only index valid IP protocols. */
1165db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX) {
1166de38924dSAndre Oppermann 				ip_protox[pr->pr_protocol] = pr - inetsw;
1167de38924dSAndre Oppermann 				return (0);
1168de38924dSAndre Oppermann 			} else
1169de38924dSAndre Oppermann 				return (EINVAL);
1170de38924dSAndre Oppermann 		}
1171de38924dSAndre Oppermann 	}
1172de38924dSAndre Oppermann 	return (EPROTONOSUPPORT);
1173de38924dSAndre Oppermann }
1174de38924dSAndre Oppermann 
1175de38924dSAndre Oppermann int
1176de38924dSAndre Oppermann ipproto_unregister(u_char ipproto)
1177de38924dSAndre Oppermann {
1178de38924dSAndre Oppermann 	struct protosw *pr;
1179de38924dSAndre Oppermann 
1180de38924dSAndre Oppermann 	/* Sanity checks. */
1181de38924dSAndre Oppermann 	if (ipproto == 0)
1182de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1183de38924dSAndre Oppermann 
1184de38924dSAndre Oppermann 	/* Check if the protocol was indeed registered. */
1185de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1186de38924dSAndre Oppermann 	if (pr == NULL)
1187de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1188de38924dSAndre Oppermann 	if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
1189de38924dSAndre Oppermann 		return (ENOENT);
1190de38924dSAndre Oppermann 
1191de38924dSAndre Oppermann 	/* Reset the protocol slot to IPPROTO_RAW. */
1192de38924dSAndre Oppermann 	ip_protox[ipproto] = pr - inetsw;
1193de38924dSAndre Oppermann 	return (0);
1194de38924dSAndre Oppermann }
1195de38924dSAndre Oppermann 
1196df8bae1dSRodney W. Grimes /*
1197df8bae1dSRodney W. Grimes  * Given address of next destination (final or next hop),
1198df8bae1dSRodney W. Grimes  * return internet address info of interface to be used to get there.
1199df8bae1dSRodney W. Grimes  */
1200bd714208SRuslan Ermilov struct in_ifaddr *
1201f2565d68SRobert Watson ip_rtaddr(struct in_addr dst)
1202df8bae1dSRodney W. Grimes {
120397d8d152SAndre Oppermann 	struct route sro;
120402c1c707SAndre Oppermann 	struct sockaddr_in *sin;
120502c1c707SAndre Oppermann 	struct in_ifaddr *ifa;
1206df8bae1dSRodney W. Grimes 
12070cfbbe3bSAndre Oppermann 	bzero(&sro, sizeof(sro));
120897d8d152SAndre Oppermann 	sin = (struct sockaddr_in *)&sro.ro_dst;
1209df8bae1dSRodney W. Grimes 	sin->sin_family = AF_INET;
1210df8bae1dSRodney W. Grimes 	sin->sin_len = sizeof(*sin);
1211df8bae1dSRodney W. Grimes 	sin->sin_addr = dst;
121297d8d152SAndre Oppermann 	rtalloc_ign(&sro, RTF_CLONING);
1213df8bae1dSRodney W. Grimes 
121497d8d152SAndre Oppermann 	if (sro.ro_rt == NULL)
121502410549SRobert Watson 		return (NULL);
121602c1c707SAndre Oppermann 
121797d8d152SAndre Oppermann 	ifa = ifatoia(sro.ro_rt->rt_ifa);
121897d8d152SAndre Oppermann 	RTFREE(sro.ro_rt);
121902410549SRobert Watson 	return (ifa);
1220df8bae1dSRodney W. Grimes }
1221df8bae1dSRodney W. Grimes 
1222df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = {
1223df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1224df8bae1dSRodney W. Grimes 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1225df8bae1dSRodney W. Grimes 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1226df8bae1dSRodney W. Grimes 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1227fcaf9f91SMike Silbersack 	0,		0,		EHOSTUNREACH,	0,
12283b8123b7SJesper Skriver 	ENOPROTOOPT,	ECONNREFUSED
1229df8bae1dSRodney W. Grimes };
1230df8bae1dSRodney W. Grimes 
1231df8bae1dSRodney W. Grimes /*
1232df8bae1dSRodney W. Grimes  * Forward a packet.  If some error occurs return the sender
1233df8bae1dSRodney W. Grimes  * an icmp packet.  Note we can't always generate a meaningful
1234df8bae1dSRodney W. Grimes  * icmp message because icmp doesn't have a large enough repertoire
1235df8bae1dSRodney W. Grimes  * of codes and types.
1236df8bae1dSRodney W. Grimes  *
1237df8bae1dSRodney W. Grimes  * If not forwarding, just drop the packet.  This could be confusing
1238df8bae1dSRodney W. Grimes  * if ipforwarding was zero but some routing protocol was advancing
1239df8bae1dSRodney W. Grimes  * us as a gateway to somewhere.  However, we must let the routing
1240df8bae1dSRodney W. Grimes  * protocol deal with that.
1241df8bae1dSRodney W. Grimes  *
1242df8bae1dSRodney W. Grimes  * The srcrt parameter indicates whether the packet is being forwarded
1243df8bae1dSRodney W. Grimes  * via a source route.
1244df8bae1dSRodney W. Grimes  */
12459b932e9eSAndre Oppermann void
12469b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt)
1247df8bae1dSRodney W. Grimes {
12482b25acc1SLuigi Rizzo 	struct ip *ip = mtod(m, struct ip *);
12499b932e9eSAndre Oppermann 	struct in_ifaddr *ia = NULL;
1250df8bae1dSRodney W. Grimes 	struct mbuf *mcopy;
12519b932e9eSAndre Oppermann 	struct in_addr dest;
1252b835b6feSBjoern A. Zeeb 	struct route ro;
1253c773494eSAndre Oppermann 	int error, type = 0, code = 0, mtu = 0;
12543efc3014SJulian Elischer 
12559b932e9eSAndre Oppermann 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1256df8bae1dSRodney W. Grimes 		ipstat.ips_cantforward++;
1257df8bae1dSRodney W. Grimes 		m_freem(m);
1258df8bae1dSRodney W. Grimes 		return;
1259df8bae1dSRodney W. Grimes 	}
12601b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
12611b968362SDag-Erling Smørgrav 	if (!ipstealth) {
12621b968362SDag-Erling Smørgrav #endif
1263df8bae1dSRodney W. Grimes 		if (ip->ip_ttl <= IPTTLDEC) {
12641b968362SDag-Erling Smørgrav 			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
126502c1c707SAndre Oppermann 			    0, 0);
1266df8bae1dSRodney W. Grimes 			return;
1267df8bae1dSRodney W. Grimes 		}
12681b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
12691b968362SDag-Erling Smørgrav 	}
12701b968362SDag-Erling Smørgrav #endif
1271df8bae1dSRodney W. Grimes 
1272d23d475fSGuido van Rooij 	ia = ip_rtaddr(ip->ip_dst);
1273d23d475fSGuido van Rooij 	if (!srcrt && ia == NULL) {
127402c1c707SAndre Oppermann 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1275df8bae1dSRodney W. Grimes 		return;
127602c1c707SAndre Oppermann 	}
1277df8bae1dSRodney W. Grimes 
1278df8bae1dSRodney W. Grimes 	/*
1279bfef7ed4SIan Dowse 	 * Save the IP header and at most 8 bytes of the payload,
1280bfef7ed4SIan Dowse 	 * in case we need to generate an ICMP message to the src.
1281bfef7ed4SIan Dowse 	 *
12824d2e3692SLuigi Rizzo 	 * XXX this can be optimized a lot by saving the data in a local
12834d2e3692SLuigi Rizzo 	 * buffer on the stack (72 bytes at most), and only allocating the
12844d2e3692SLuigi Rizzo 	 * mbuf if really necessary. The vast majority of the packets
12854d2e3692SLuigi Rizzo 	 * are forwarded without having to send an ICMP back (either
12864d2e3692SLuigi Rizzo 	 * because unnecessary, or because rate limited), so we are
12874d2e3692SLuigi Rizzo 	 * really we are wasting a lot of work here.
12884d2e3692SLuigi Rizzo 	 *
1289bfef7ed4SIan Dowse 	 * We don't use m_copy() because it might return a reference
1290bfef7ed4SIan Dowse 	 * to a shared cluster. Both this function and ip_output()
1291bfef7ed4SIan Dowse 	 * assume exclusive access to the IP header in `m', so any
1292bfef7ed4SIan Dowse 	 * data in a cluster may change before we reach icmp_error().
1293df8bae1dSRodney W. Grimes 	 */
1294780b2f69SAndre Oppermann 	MGETHDR(mcopy, M_DONTWAIT, m->m_type);
1295a163d034SWarner Losh 	if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) {
12969967cafcSSam Leffler 		/*
12979967cafcSSam Leffler 		 * It's probably ok if the pkthdr dup fails (because
12989967cafcSSam Leffler 		 * the deep copy of the tag chain failed), but for now
12999967cafcSSam Leffler 		 * be conservative and just discard the copy since
13009967cafcSSam Leffler 		 * code below may some day want the tags.
13019967cafcSSam Leffler 		 */
13029967cafcSSam Leffler 		m_free(mcopy);
13039967cafcSSam Leffler 		mcopy = NULL;
13049967cafcSSam Leffler 	}
1305bfef7ed4SIan Dowse 	if (mcopy != NULL) {
1306780b2f69SAndre Oppermann 		mcopy->m_len = min(ip->ip_len, M_TRAILINGSPACE(mcopy));
1307e6b0a570SBruce M Simpson 		mcopy->m_pkthdr.len = mcopy->m_len;
1308bfef7ed4SIan Dowse 		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1309bfef7ed4SIan Dowse 	}
131004287599SRuslan Ermilov 
131104287599SRuslan Ermilov #ifdef IPSTEALTH
131204287599SRuslan Ermilov 	if (!ipstealth) {
131304287599SRuslan Ermilov #endif
131404287599SRuslan Ermilov 		ip->ip_ttl -= IPTTLDEC;
131504287599SRuslan Ermilov #ifdef IPSTEALTH
131604287599SRuslan Ermilov 	}
131704287599SRuslan Ermilov #endif
1318df8bae1dSRodney W. Grimes 
1319df8bae1dSRodney W. Grimes 	/*
1320df8bae1dSRodney W. Grimes 	 * If forwarding packet using same interface that it came in on,
1321df8bae1dSRodney W. Grimes 	 * perhaps should send a redirect to sender to shortcut a hop.
1322df8bae1dSRodney W. Grimes 	 * Only send redirect if source is sending directly to us,
1323df8bae1dSRodney W. Grimes 	 * and if packet was not source routed (or has any options).
1324df8bae1dSRodney W. Grimes 	 * Also, don't send redirect if forwarding using a default route
1325df8bae1dSRodney W. Grimes 	 * or a route modified by a redirect.
1326df8bae1dSRodney W. Grimes 	 */
13279b932e9eSAndre Oppermann 	dest.s_addr = 0;
13289b932e9eSAndre Oppermann 	if (!srcrt && ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) {
132902c1c707SAndre Oppermann 		struct sockaddr_in *sin;
133002c1c707SAndre Oppermann 		struct rtentry *rt;
133102c1c707SAndre Oppermann 
13320cfbbe3bSAndre Oppermann 		bzero(&ro, sizeof(ro));
133302c1c707SAndre Oppermann 		sin = (struct sockaddr_in *)&ro.ro_dst;
133402c1c707SAndre Oppermann 		sin->sin_family = AF_INET;
133502c1c707SAndre Oppermann 		sin->sin_len = sizeof(*sin);
13369b932e9eSAndre Oppermann 		sin->sin_addr = ip->ip_dst;
133726d02ca7SAndre Oppermann 		rtalloc_ign(&ro, RTF_CLONING);
133802c1c707SAndre Oppermann 
133902c1c707SAndre Oppermann 		rt = ro.ro_rt;
134002c1c707SAndre Oppermann 
134102c1c707SAndre Oppermann 		if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
13429b932e9eSAndre Oppermann 		    satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1343df8bae1dSRodney W. Grimes #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1344df8bae1dSRodney W. Grimes 			u_long src = ntohl(ip->ip_src.s_addr);
1345df8bae1dSRodney W. Grimes 
1346df8bae1dSRodney W. Grimes 			if (RTA(rt) &&
1347df8bae1dSRodney W. Grimes 			    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1348df8bae1dSRodney W. Grimes 				if (rt->rt_flags & RTF_GATEWAY)
13499b932e9eSAndre Oppermann 					dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1350df8bae1dSRodney W. Grimes 				else
13519b932e9eSAndre Oppermann 					dest.s_addr = ip->ip_dst.s_addr;
1352df8bae1dSRodney W. Grimes 				/* Router requirements says to only send host redirects */
1353df8bae1dSRodney W. Grimes 				type = ICMP_REDIRECT;
1354df8bae1dSRodney W. Grimes 				code = ICMP_REDIRECT_HOST;
1355df8bae1dSRodney W. Grimes 			}
1356df8bae1dSRodney W. Grimes 		}
135702c1c707SAndre Oppermann 		if (rt)
135802c1c707SAndre Oppermann 			RTFREE(rt);
135902c1c707SAndre Oppermann 	}
1360df8bae1dSRodney W. Grimes 
1361b835b6feSBjoern A. Zeeb 	/*
1362b835b6feSBjoern A. Zeeb 	 * Try to cache the route MTU from ip_output so we can consider it for
1363b835b6feSBjoern A. Zeeb 	 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191.
1364b835b6feSBjoern A. Zeeb 	 */
1365b835b6feSBjoern A. Zeeb 	bzero(&ro, sizeof(ro));
1366b835b6feSBjoern A. Zeeb 	rtalloc_ign(&ro, RTF_CLONING);
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)
1376df8bae1dSRodney W. Grimes 		ipstat.ips_cantforward++;
1377df8bae1dSRodney W. Grimes 	else {
1378df8bae1dSRodney W. Grimes 		ipstat.ips_forward++;
1379df8bae1dSRodney W. Grimes 		if (type)
1380df8bae1dSRodney W. Grimes 			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 		}
1432df8bae1dSRodney W. Grimes 		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 		 */
1444df285b3dSMike Silbersack 		if (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))
1526d314ad7bSJulian Elischer 		&& ( ifp->if_index && (ifp->if_index <= 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 
1566f0068c4aSGarrett Wollman 	if (ip_rsvpd != NULL)
1567f0068c4aSGarrett Wollman 		return EADDRINUSE;
1568f0068c4aSGarrett Wollman 
1569f0068c4aSGarrett Wollman 	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 	 */
15741c5de19aSGarrett Wollman 	if (!ip_rsvp_on) {
15751c5de19aSGarrett Wollman 		ip_rsvp_on = 1;
15761c5de19aSGarrett Wollman 		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 {
1585f0068c4aSGarrett Wollman 	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 	 */
15901c5de19aSGarrett Wollman 	if (ip_rsvp_on) {
15911c5de19aSGarrett Wollman 		ip_rsvp_on = 0;
15921c5de19aSGarrett Wollman 		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 
1610bbb4330bSLuigi Rizzo 	if (!rsvp_on) {
1611bbb4330bSLuigi Rizzo 		m_freem(m);
1612bbb4330bSLuigi Rizzo 		return;
1613bbb4330bSLuigi Rizzo 	}
1614bbb4330bSLuigi Rizzo 
1615bbb4330bSLuigi Rizzo 	if (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