xref: /freebsd/sys/netinet/ip_input.c (revision f2565d68a4e5922928b92ad6043be96ada8cb7b3)
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
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
330ac40133SBrian Somers #include "opt_bootp.h"
3474a9466cSGary Palmer #include "opt_ipfw.h"
3527108a15SDag-Erling Smørgrav #include "opt_ipstealth.h"
366a800098SYoshinobu Inoue #include "opt_ipsec.h"
3736b0360bSRobert Watson #include "opt_mac.h"
38a9771948SGleb Smirnoff #include "opt_carp.h"
3974a9466cSGary Palmer 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
41df8bae1dSRodney W. Grimes #include <sys/systm.h>
425f311da2SMike Silbersack #include <sys/callout.h>
43df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
44b715f178SLuigi Rizzo #include <sys/malloc.h>
45df8bae1dSRodney W. Grimes #include <sys/domain.h>
46df8bae1dSRodney W. Grimes #include <sys/protosw.h>
47df8bae1dSRodney W. Grimes #include <sys/socket.h>
48df8bae1dSRodney W. Grimes #include <sys/time.h>
49df8bae1dSRodney W. Grimes #include <sys/kernel.h>
501025071fSGarrett Wollman #include <sys/syslog.h>
51b5e8ce9fSBruce Evans #include <sys/sysctl.h>
52df8bae1dSRodney W. Grimes 
53c85540ddSAndrey A. Chernov #include <net/pfil.h>
54df8bae1dSRodney W. Grimes #include <net/if.h>
559494d596SBrooks Davis #include <net/if_types.h>
56d314ad7bSJulian Elischer #include <net/if_var.h>
5782c23ebaSBill Fenner #include <net/if_dl.h>
58df8bae1dSRodney W. Grimes #include <net/route.h>
59748e0b0aSGarrett Wollman #include <net/netisr.h>
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes #include <netinet/in.h>
62df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
63b5e8ce9fSBruce Evans #include <netinet/in_var.h>
64df8bae1dSRodney W. Grimes #include <netinet/ip.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
66df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
67df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
68ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
6958938916SGarrett Wollman #include <machine/in_cksum.h>
70a9771948SGleb Smirnoff #ifdef DEV_CARP
71a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
72a9771948SGleb Smirnoff #endif
731dfcf0d2SAndre Oppermann #if defined(IPSEC) || defined(FAST_IPSEC)
741dfcf0d2SAndre Oppermann #include <netinet/ip_ipsec.h>
751dfcf0d2SAndre Oppermann #endif /* IPSEC */
76df8bae1dSRodney W. Grimes 
77f0068c4aSGarrett Wollman #include <sys/socketvar.h>
786ddbf1e2SGary Palmer 
79010b65f5SJulian Elischer /* XXX: Temporary until ipfw_ether and ipfw_bridge are converted. */
80010b65f5SJulian Elischer #include <netinet/ip_fw.h>
81010b65f5SJulian Elischer #include <netinet/ip_dummynet.h>
82010b65f5SJulian Elischer 
83aed55708SRobert Watson #include <security/mac/mac_framework.h>
84aed55708SRobert Watson 
851c5de19aSGarrett Wollman int rsvp_on = 0;
86f0068c4aSGarrett Wollman 
871f91d8c5SDavid Greenman int	ipforwarding = 0;
880312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
893d177f46SBill Fumerola     &ipforwarding, 0, "Enable IP forwarding between interfaces");
900312fbe9SPoul-Henning Kamp 
91d4fb926cSGarrett Wollman static int	ipsendredirects = 1; /* XXX */
920312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
933d177f46SBill Fumerola     &ipsendredirects, 0, "Enable sending IP redirects");
940312fbe9SPoul-Henning Kamp 
95df8bae1dSRodney W. Grimes int	ip_defttl = IPDEFTTL;
960312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
973d177f46SBill Fumerola     &ip_defttl, 0, "Maximum TTL on IP packets");
980312fbe9SPoul-Henning Kamp 
996a800098SYoshinobu Inoue static int	ip_keepfaith = 0;
1006a800098SYoshinobu Inoue SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
1016a800098SYoshinobu Inoue     &ip_keepfaith,	0,
1026a800098SYoshinobu Inoue     "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
1036a800098SYoshinobu Inoue 
104df285b3dSMike Silbersack static int	ip_sendsourcequench = 0;
105df285b3dSMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW,
106df285b3dSMike Silbersack     &ip_sendsourcequench, 0,
107df285b3dSMike Silbersack     "Enable the transmission of source quench packets");
108df285b3dSMike Silbersack 
1091f44b0a1SDavid Malone int	ip_do_randomid = 0;
1101f44b0a1SDavid Malone SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW,
1111f44b0a1SDavid Malone     &ip_do_randomid, 0,
1121f44b0a1SDavid Malone     "Assign random ip_id values");
1131f44b0a1SDavid Malone 
114823db0e9SDon Lewis /*
115823db0e9SDon Lewis  * XXX - Setting ip_checkinterface mostly implements the receive side of
116823db0e9SDon Lewis  * the Strong ES model described in RFC 1122, but since the routing table
117a8f12100SDon Lewis  * and transmit implementation do not implement the Strong ES model,
118823db0e9SDon Lewis  * setting this to 1 results in an odd hybrid.
1193f67c834SDon Lewis  *
120a8f12100SDon Lewis  * XXX - ip_checkinterface currently must be disabled if you use ipnat
121a8f12100SDon Lewis  * to translate the destination address to another local interface.
1223f67c834SDon Lewis  *
1233f67c834SDon Lewis  * XXX - ip_checkinterface must be disabled if you add IP aliases
1243f67c834SDon Lewis  * to the loopback interface instead of the interface where the
1253f67c834SDon Lewis  * packets for those addresses are received.
126823db0e9SDon Lewis  */
1274bc37f98SMaxim Konovalov static int	ip_checkinterface = 0;
128b3e95d4eSJonathan Lemon SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
129b3e95d4eSJonathan Lemon     &ip_checkinterface, 0, "Verify packet arrives on correct interface");
130b3e95d4eSJonathan Lemon 
131c21fd232SAndre Oppermann struct pfil_head inet_pfil_hook;	/* Packet filter hooks */
132df8bae1dSRodney W. Grimes 
1331cafed39SJonathan Lemon static struct	ifqueue ipintrq;
134ca925d9cSJonathan Lemon static int	ipqmaxlen = IFQ_MAXLEN;
135ca925d9cSJonathan Lemon 
136df8bae1dSRodney W. Grimes extern	struct domain inetdomain;
137f0ffb944SJulian Elischer extern	struct protosw inetsw[];
138df8bae1dSRodney W. Grimes u_char	ip_protox[IPPROTO_MAX];
13959562606SGarrett Wollman struct	in_ifaddrhead in_ifaddrhead; 		/* first inet address */
140ca925d9cSJonathan Lemon struct	in_ifaddrhashhead *in_ifaddrhashtbl;	/* inet addr hash table  */
141ca925d9cSJonathan Lemon u_long 	in_ifaddrhmask;				/* mask for hash table */
142ca925d9cSJonathan Lemon 
143afed1375SDavid Greenman SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
1443d177f46SBill Fumerola     &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
1450312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
1466489fe65SAndre Oppermann     &ipintrq.ifq_drops, 0,
1476489fe65SAndre Oppermann     "Number of packets dropped from the IP input queue");
148df8bae1dSRodney W. Grimes 
149f23b4c91SGarrett Wollman struct ipstat ipstat;
150c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
1513d177f46SBill Fumerola     &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
152194a213eSAndrey A. Chernov 
153d248c7d7SRobert Watson /*
154d248c7d7SRobert Watson  * IP datagram reassembly.
155d248c7d7SRobert Watson  */
156194a213eSAndrey A. Chernov #define IPREASS_NHASH_LOG2      6
157194a213eSAndrey A. Chernov #define IPREASS_NHASH           (1 << IPREASS_NHASH_LOG2)
158194a213eSAndrey A. Chernov #define IPREASS_HMASK           (IPREASS_NHASH - 1)
159194a213eSAndrey A. Chernov #define IPREASS_HASH(x,y) \
160831a80b0SMatthew Dillon 	(((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
161194a213eSAndrey A. Chernov 
162d248c7d7SRobert Watson static uma_zone_t ipq_zone;
163462b86feSPoul-Henning Kamp static TAILQ_HEAD(ipqhead, ipq) ipq[IPREASS_NHASH];
164dfa60d93SRobert Watson static struct mtx ipqlock;
1652fad1e93SSam Leffler 
1662fad1e93SSam Leffler #define	IPQ_LOCK()	mtx_lock(&ipqlock)
1672fad1e93SSam Leffler #define	IPQ_UNLOCK()	mtx_unlock(&ipqlock)
168888c2a3cSSam Leffler #define	IPQ_LOCK_INIT()	mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
169888c2a3cSSam Leffler #define	IPQ_LOCK_ASSERT()	mtx_assert(&ipqlock, MA_OWNED)
170f23b4c91SGarrett Wollman 
171d248c7d7SRobert Watson static void	maxnipq_update(void);
1724f590175SPaul Saab static void	ipq_zone_change(void *);
173d248c7d7SRobert Watson 
174d248c7d7SRobert Watson static int	maxnipq;	/* Administrative limit on # reass queues. */
175d248c7d7SRobert Watson static int	nipq = 0;	/* Total # of reass queues */
1766489fe65SAndre Oppermann SYSCTL_INT(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_RD,
1776489fe65SAndre Oppermann     &nipq, 0, "Current number of IPv4 fragment reassembly queue entries");
178d248c7d7SRobert Watson 
179d248c7d7SRobert Watson static int	maxfragsperpacket;
180d248c7d7SRobert Watson SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW,
181d248c7d7SRobert Watson     &maxfragsperpacket, 0,
182d248c7d7SRobert Watson     "Maximum number of IPv4 fragments allowed per packet");
183d248c7d7SRobert Watson 
184d248c7d7SRobert Watson struct callout	ipport_tick_callout;
185d248c7d7SRobert Watson 
1860312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU
1870312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
1883d177f46SBill Fumerola     &ip_mtu, 0, "Default MTU");
1890312fbe9SPoul-Henning Kamp #endif
1900312fbe9SPoul-Henning Kamp 
1911b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
192c76ff708SAndre Oppermann int	ipstealth = 0;
1931b968362SDag-Erling Smørgrav SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
1946489fe65SAndre Oppermann     &ipstealth, 0, "IP stealth mode, no TTL decrementation on forwarding");
1951b968362SDag-Erling Smørgrav #endif
1961b968362SDag-Erling Smørgrav 
197010b65f5SJulian Elischer /*
198010b65f5SJulian Elischer  * ipfw_ether and ipfw_bridge hooks.
199010b65f5SJulian Elischer  * XXX: Temporary until those are converted to pfil_hooks as well.
200010b65f5SJulian Elischer  */
201010b65f5SJulian Elischer ip_fw_chk_t *ip_fw_chk_ptr = NULL;
202010b65f5SJulian Elischer ip_dn_io_t *ip_dn_io_ptr = NULL;
203010b65f5SJulian Elischer int fw_one_pass = 1;
204010b65f5SJulian Elischer 
2054d77a549SAlfred Perlstein static void	ip_freef(struct ipqhead *, struct ipq *);
2068948e4baSArchie Cobbs 
207df8bae1dSRodney W. Grimes /*
208df8bae1dSRodney W. Grimes  * IP initialization: fill in IP protocol switch table.
209df8bae1dSRodney W. Grimes  * All protocols not implemented in kernel go to raw IP protocol handler.
210df8bae1dSRodney W. Grimes  */
211df8bae1dSRodney W. Grimes void
212f2565d68SRobert Watson ip_init(void)
213df8bae1dSRodney W. Grimes {
214f2565d68SRobert Watson 	struct protosw *pr;
215f2565d68SRobert Watson 	int i;
216df8bae1dSRodney W. Grimes 
21759562606SGarrett Wollman 	TAILQ_INIT(&in_ifaddrhead);
218ca925d9cSJonathan Lemon 	in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &in_ifaddrhmask);
219f0ffb944SJulian Elischer 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
22002410549SRobert Watson 	if (pr == NULL)
221db09bef3SAndre Oppermann 		panic("ip_init: PF_INET not found");
222db09bef3SAndre Oppermann 
223db09bef3SAndre Oppermann 	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
224df8bae1dSRodney W. Grimes 	for (i = 0; i < IPPROTO_MAX; i++)
225df8bae1dSRodney W. Grimes 		ip_protox[i] = pr - inetsw;
226db09bef3SAndre Oppermann 	/*
227db09bef3SAndre Oppermann 	 * Cycle through IP protocols and put them into the appropriate place
228db09bef3SAndre Oppermann 	 * in ip_protox[].
229db09bef3SAndre Oppermann 	 */
230f0ffb944SJulian Elischer 	for (pr = inetdomain.dom_protosw;
231f0ffb944SJulian Elischer 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
232df8bae1dSRodney W. Grimes 		if (pr->pr_domain->dom_family == PF_INET &&
233db09bef3SAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
234db09bef3SAndre Oppermann 			/* Be careful to only index valid IP protocols. */
235db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX)
236df8bae1dSRodney W. Grimes 				ip_protox[pr->pr_protocol] = pr - inetsw;
237db09bef3SAndre Oppermann 		}
238194a213eSAndrey A. Chernov 
239c21fd232SAndre Oppermann 	/* Initialize packet filter hooks. */
240134ea224SSam Leffler 	inet_pfil_hook.ph_type = PFIL_TYPE_AF;
241134ea224SSam Leffler 	inet_pfil_hook.ph_af = AF_INET;
242134ea224SSam Leffler 	if ((i = pfil_head_register(&inet_pfil_hook)) != 0)
243134ea224SSam Leffler 		printf("%s: WARNING: unable to register pfil hook, "
244134ea224SSam Leffler 			"error %d\n", __func__, i);
245134ea224SSam Leffler 
246db09bef3SAndre Oppermann 	/* Initialize IP reassembly queue. */
2472fad1e93SSam Leffler 	IPQ_LOCK_INIT();
248194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++)
249462b86feSPoul-Henning Kamp 	    TAILQ_INIT(&ipq[i]);
250375386e2SMike Silbersack 	maxnipq = nmbclusters / 32;
251375386e2SMike Silbersack 	maxfragsperpacket = 16;
252d248c7d7SRobert Watson 	ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
253d248c7d7SRobert Watson 	    NULL, UMA_ALIGN_PTR, 0);
254d248c7d7SRobert Watson 	maxnipq_update();
255194a213eSAndrey A. Chernov 
2565f311da2SMike Silbersack 	/* Start ipport_tick. */
2575f311da2SMike Silbersack 	callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
2585f311da2SMike Silbersack 	ipport_tick(NULL);
2595f311da2SMike Silbersack 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2605f311da2SMike Silbersack 		SHUTDOWN_PRI_DEFAULT);
2614f590175SPaul Saab 	EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
2624f590175SPaul Saab 		NULL, EVENTHANDLER_PRI_ANY);
2635f311da2SMike Silbersack 
264db09bef3SAndre Oppermann 	/* Initialize various other remaining things. */
265227ee8a1SPoul-Henning Kamp 	ip_id = time_second & 0xffff;
266df8bae1dSRodney W. Grimes 	ipintrq.ifq_maxlen = ipqmaxlen;
2676008862bSJohn Baldwin 	mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF);
2687902224cSSam Leffler 	netisr_register(NETISR_IP, ip_input, &ipintrq, NETISR_MPSAFE);
269df8bae1dSRodney W. Grimes }
270df8bae1dSRodney W. Grimes 
271f2565d68SRobert Watson void
272f2565d68SRobert Watson ip_fini(void *xtp)
2735f311da2SMike Silbersack {
274f2565d68SRobert Watson 
2755f311da2SMike Silbersack 	callout_stop(&ipport_tick_callout);
2765f311da2SMike Silbersack }
2775f311da2SMike Silbersack 
2784d2e3692SLuigi Rizzo /*
279df8bae1dSRodney W. Grimes  * Ip input routine.  Checksum and byte swap header.  If fragmented
280df8bae1dSRodney W. Grimes  * try to reassemble.  Process options.  Pass to next level.
281df8bae1dSRodney W. Grimes  */
282c67b1d17SGarrett Wollman void
283c67b1d17SGarrett Wollman ip_input(struct mbuf *m)
284df8bae1dSRodney W. Grimes {
2859188b4a1SAndre Oppermann 	struct ip *ip = NULL;
2865da9f8faSJosef Karthauser 	struct in_ifaddr *ia = NULL;
287ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
2889b932e9eSAndre Oppermann 	int    checkif, hlen = 0;
28947c861ecSBrian Somers 	u_short sum;
29002c1c707SAndre Oppermann 	int dchg = 0;				/* dest changed after fw */
291f51f805fSSam Leffler 	struct in_addr odst;			/* original dst address */
292b715f178SLuigi Rizzo 
293fe584538SDag-Erling Smørgrav   	M_ASSERTPKTHDR(m);
294db40007dSAndrew R. Reiter 
295ac9d7e26SMax Laier 	if (m->m_flags & M_FASTFWD_OURS) {
2969b932e9eSAndre Oppermann 		/*
29776ff6dcfSAndre Oppermann 		 * Firewall or NAT changed destination to local.
29876ff6dcfSAndre Oppermann 		 * We expect ip_len and ip_off to be in host byte order.
2999b932e9eSAndre Oppermann 		 */
30076ff6dcfSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
30176ff6dcfSAndre Oppermann 		/* Set up some basics that will be used later. */
3022b25acc1SLuigi Rizzo 		ip = mtod(m, struct ip *);
30353be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
3049b932e9eSAndre Oppermann   		goto ours;
3052b25acc1SLuigi Rizzo   	}
3062b25acc1SLuigi Rizzo 
307df8bae1dSRodney W. Grimes 	ipstat.ips_total++;
30858938916SGarrett Wollman 
30958938916SGarrett Wollman 	if (m->m_pkthdr.len < sizeof(struct ip))
31058938916SGarrett Wollman 		goto tooshort;
31158938916SGarrett Wollman 
312df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct ip) &&
3130b17fba7SAndre Oppermann 	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
314df8bae1dSRodney W. Grimes 		ipstat.ips_toosmall++;
315c67b1d17SGarrett Wollman 		return;
316df8bae1dSRodney W. Grimes 	}
317df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
31858938916SGarrett Wollman 
31953be11f6SPoul-Henning Kamp 	if (ip->ip_v != IPVERSION) {
320df8bae1dSRodney W. Grimes 		ipstat.ips_badvers++;
321df8bae1dSRodney W. Grimes 		goto bad;
322df8bae1dSRodney W. Grimes 	}
32358938916SGarrett Wollman 
32453be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
325df8bae1dSRodney W. Grimes 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
326df8bae1dSRodney W. Grimes 		ipstat.ips_badhlen++;
327df8bae1dSRodney W. Grimes 		goto bad;
328df8bae1dSRodney W. Grimes 	}
329df8bae1dSRodney W. Grimes 	if (hlen > m->m_len) {
3300b17fba7SAndre Oppermann 		if ((m = m_pullup(m, hlen)) == NULL) {
331df8bae1dSRodney W. Grimes 			ipstat.ips_badhlen++;
332c67b1d17SGarrett Wollman 			return;
333df8bae1dSRodney W. Grimes 		}
334df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
335df8bae1dSRodney W. Grimes 	}
33633841545SHajimu UMEMOTO 
33733841545SHajimu UMEMOTO 	/* 127/8 must not appear on wire - RFC1122 */
33833841545SHajimu UMEMOTO 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
33933841545SHajimu UMEMOTO 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
34033841545SHajimu UMEMOTO 		if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
34133841545SHajimu UMEMOTO 			ipstat.ips_badaddr++;
34233841545SHajimu UMEMOTO 			goto bad;
34333841545SHajimu UMEMOTO 		}
34433841545SHajimu UMEMOTO 	}
34533841545SHajimu UMEMOTO 
346db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
347db4f9cc7SJonathan Lemon 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
348db4f9cc7SJonathan Lemon 	} else {
34958938916SGarrett Wollman 		if (hlen == sizeof(struct ip)) {
35047c861ecSBrian Somers 			sum = in_cksum_hdr(ip);
35158938916SGarrett Wollman 		} else {
35247c861ecSBrian Somers 			sum = in_cksum(m, hlen);
35358938916SGarrett Wollman 		}
354db4f9cc7SJonathan Lemon 	}
35547c861ecSBrian Somers 	if (sum) {
356df8bae1dSRodney W. Grimes 		ipstat.ips_badsum++;
357df8bae1dSRodney W. Grimes 		goto bad;
358df8bae1dSRodney W. Grimes 	}
359df8bae1dSRodney W. Grimes 
36002b199f1SMax Laier #ifdef ALTQ
36102b199f1SMax Laier 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
36202b199f1SMax Laier 		/* packet is dropped by traffic conditioner */
36302b199f1SMax Laier 		return;
36402b199f1SMax Laier #endif
36502b199f1SMax Laier 
366df8bae1dSRodney W. Grimes 	/*
367df8bae1dSRodney W. Grimes 	 * Convert fields to host representation.
368df8bae1dSRodney W. Grimes 	 */
369fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
370df8bae1dSRodney W. Grimes 	if (ip->ip_len < hlen) {
371df8bae1dSRodney W. Grimes 		ipstat.ips_badlen++;
372df8bae1dSRodney W. Grimes 		goto bad;
373df8bae1dSRodney W. Grimes 	}
374fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
375df8bae1dSRodney W. Grimes 
376df8bae1dSRodney W. Grimes 	/*
377df8bae1dSRodney W. Grimes 	 * Check that the amount of data in the buffers
378df8bae1dSRodney W. Grimes 	 * is as at least much as the IP header would have us expect.
379df8bae1dSRodney W. Grimes 	 * Trim mbufs if longer than we expect.
380df8bae1dSRodney W. Grimes 	 * Drop packet if shorter than we expect.
381df8bae1dSRodney W. Grimes 	 */
382df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len < ip->ip_len) {
38358938916SGarrett Wollman tooshort:
384df8bae1dSRodney W. Grimes 		ipstat.ips_tooshort++;
385df8bae1dSRodney W. Grimes 		goto bad;
386df8bae1dSRodney W. Grimes 	}
387df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len > ip->ip_len) {
388df8bae1dSRodney W. Grimes 		if (m->m_len == m->m_pkthdr.len) {
389df8bae1dSRodney W. Grimes 			m->m_len = ip->ip_len;
390df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = ip->ip_len;
391df8bae1dSRodney W. Grimes 		} else
392df8bae1dSRodney W. Grimes 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
393df8bae1dSRodney W. Grimes 	}
3941dfcf0d2SAndre Oppermann #if defined(IPSEC) || defined(FAST_IPSEC)
39514dd6717SSam Leffler 	/*
39614dd6717SSam Leffler 	 * Bypass packet filtering for packets from a tunnel (gif).
39714dd6717SSam Leffler 	 */
3981dfcf0d2SAndre Oppermann 	if (ip_ipsec_filtergif(m))
399c21fd232SAndre Oppermann 		goto passin;
4001dfcf0d2SAndre Oppermann #endif /* IPSEC */
4013f67c834SDon Lewis 
402c4ac87eaSDarren Reed 	/*
403134ea224SSam Leffler 	 * Run through list of hooks for input packets.
404f51f805fSSam Leffler 	 *
405f51f805fSSam Leffler 	 * NB: Beware of the destination address changing (e.g.
406f51f805fSSam Leffler 	 *     by NAT rewriting).  When this happens, tell
407f51f805fSSam Leffler 	 *     ip_forward to do the right thing.
408c4ac87eaSDarren Reed 	 */
409c21fd232SAndre Oppermann 
410c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
411604afec4SChristian S.J. Peron 	if (!PFIL_HOOKED(&inet_pfil_hook))
412c21fd232SAndre Oppermann 		goto passin;
413c21fd232SAndre Oppermann 
414f51f805fSSam Leffler 	odst = ip->ip_dst;
415134ea224SSam Leffler 	if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
416d6a8d588SMax Laier 	    PFIL_IN, NULL) != 0)
417beec8214SDarren Reed 		return;
418134ea224SSam Leffler 	if (m == NULL)			/* consumed by filter */
419c4ac87eaSDarren Reed 		return;
4209b932e9eSAndre Oppermann 
421c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
42202c1c707SAndre Oppermann 	dchg = (odst.s_addr != ip->ip_dst.s_addr);
4239b932e9eSAndre Oppermann 
4249b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
4259b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
4269b932e9eSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
4279b932e9eSAndre Oppermann 		goto ours;
4289b932e9eSAndre Oppermann 	}
429099dd043SAndre Oppermann 	if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) {
430099dd043SAndre Oppermann 		/*
431099dd043SAndre Oppermann 		 * Directly ship on the packet.  This allows to forward packets
432099dd043SAndre Oppermann 		 * that were destined for us to some other directly connected
433099dd043SAndre Oppermann 		 * host.
434099dd043SAndre Oppermann 		 */
435099dd043SAndre Oppermann 		ip_forward(m, dchg);
436099dd043SAndre Oppermann 		return;
437099dd043SAndre Oppermann 	}
4389b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
4399b932e9eSAndre Oppermann 
440c21fd232SAndre Oppermann passin:
441df8bae1dSRodney W. Grimes 	/*
442df8bae1dSRodney W. Grimes 	 * Process options and, if not destined for us,
443df8bae1dSRodney W. Grimes 	 * ship it on.  ip_dooptions returns 1 when an
444df8bae1dSRodney W. Grimes 	 * error was detected (causing an icmp message
445df8bae1dSRodney W. Grimes 	 * to be sent and the original packet to be freed).
446df8bae1dSRodney W. Grimes 	 */
4479b932e9eSAndre Oppermann 	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
448c67b1d17SGarrett Wollman 		return;
449df8bae1dSRodney W. Grimes 
450f0068c4aSGarrett Wollman         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
451f0068c4aSGarrett Wollman          * matter if it is destined to another node, or whether it is
452f0068c4aSGarrett Wollman          * a multicast one, RSVP wants it! and prevents it from being forwarded
453f0068c4aSGarrett Wollman          * anywhere else. Also checks if the rsvp daemon is running before
454f0068c4aSGarrett Wollman 	 * grabbing the packet.
455f0068c4aSGarrett Wollman          */
4561c5de19aSGarrett Wollman 	if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
457f0068c4aSGarrett Wollman 		goto ours;
458f0068c4aSGarrett Wollman 
459df8bae1dSRodney W. Grimes 	/*
460df8bae1dSRodney W. Grimes 	 * Check our list of addresses, to see if the packet is for us.
461cc766e04SGarrett Wollman 	 * If we don't have any addresses, assume any unicast packet
462cc766e04SGarrett Wollman 	 * we receive might be for us (and let the upper layers deal
463cc766e04SGarrett Wollman 	 * with it).
464df8bae1dSRodney W. Grimes 	 */
465cc766e04SGarrett Wollman 	if (TAILQ_EMPTY(&in_ifaddrhead) &&
466cc766e04SGarrett Wollman 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
467cc766e04SGarrett Wollman 		goto ours;
468cc766e04SGarrett Wollman 
4697538a9a0SJonathan Lemon 	/*
470823db0e9SDon Lewis 	 * Enable a consistency check between the destination address
471823db0e9SDon Lewis 	 * and the arrival interface for a unicast packet (the RFC 1122
472823db0e9SDon Lewis 	 * strong ES model) if IP forwarding is disabled and the packet
473e15ae1b2SDon Lewis 	 * is not locally generated and the packet is not subject to
474e15ae1b2SDon Lewis 	 * 'ipfw fwd'.
4753f67c834SDon Lewis 	 *
4763f67c834SDon Lewis 	 * XXX - Checking also should be disabled if the destination
4773f67c834SDon Lewis 	 * address is ipnat'ed to a different interface.
4783f67c834SDon Lewis 	 *
479a8f12100SDon Lewis 	 * XXX - Checking is incompatible with IP aliases added
4803f67c834SDon Lewis 	 * to the loopback interface instead of the interface where
4813f67c834SDon Lewis 	 * the packets are received.
482a9771948SGleb Smirnoff 	 *
483a9771948SGleb Smirnoff 	 * XXX - This is the case for carp vhost IPs as well so we
484a9771948SGleb Smirnoff 	 * insert a workaround. If the packet got here, we already
485a9771948SGleb Smirnoff 	 * checked with carp_iamatch() and carp_forus().
486823db0e9SDon Lewis 	 */
487823db0e9SDon Lewis 	checkif = ip_checkinterface && (ipforwarding == 0) &&
4889494d596SBrooks Davis 	    m->m_pkthdr.rcvif != NULL &&
489e15ae1b2SDon Lewis 	    ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
490a9771948SGleb Smirnoff #ifdef DEV_CARP
491a9771948SGleb Smirnoff 	    !m->m_pkthdr.rcvif->if_carp &&
492a9771948SGleb Smirnoff #endif
4939b932e9eSAndre Oppermann 	    (dchg == 0);
494823db0e9SDon Lewis 
495ca925d9cSJonathan Lemon 	/*
496ca925d9cSJonathan Lemon 	 * Check for exact addresses in the hash bucket.
497ca925d9cSJonathan Lemon 	 */
4989b932e9eSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
499f9e354dfSJulian Elischer 		/*
500823db0e9SDon Lewis 		 * If the address matches, verify that the packet
501823db0e9SDon Lewis 		 * arrived via the correct interface if checking is
502823db0e9SDon Lewis 		 * enabled.
503f9e354dfSJulian Elischer 		 */
5049b932e9eSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
505823db0e9SDon Lewis 		    (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
506ed1ff184SJulian Elischer 			goto ours;
507ca925d9cSJonathan Lemon 	}
508823db0e9SDon Lewis 	/*
509ca925d9cSJonathan Lemon 	 * Check for broadcast addresses.
510ca925d9cSJonathan Lemon 	 *
511ca925d9cSJonathan Lemon 	 * Only accept broadcast packets that arrive via the matching
512ca925d9cSJonathan Lemon 	 * interface.  Reception of forwarded directed broadcasts would
513ca925d9cSJonathan Lemon 	 * be handled via ip_forward() and ether_output() with the loopback
514ca925d9cSJonathan Lemon 	 * into the stack for SIMPLEX interfaces handled by ether_output().
515823db0e9SDon Lewis 	 */
5164f450ff9SBruce M Simpson 	if (m->m_pkthdr.rcvif != NULL &&
5174f450ff9SBruce M Simpson 	    m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
518ca925d9cSJonathan Lemon 	        TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
519ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
520ca925d9cSJonathan Lemon 				continue;
521ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
522df8bae1dSRodney W. Grimes 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
5239b932e9eSAndre Oppermann 			    ip->ip_dst.s_addr)
524df8bae1dSRodney W. Grimes 				goto ours;
5259b932e9eSAndre Oppermann 			if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr)
526df8bae1dSRodney W. Grimes 				goto ours;
5270ac40133SBrian Somers #ifdef BOOTP_COMPAT
528ca925d9cSJonathan Lemon 			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
529ca925d9cSJonathan Lemon 				goto ours;
5300ac40133SBrian Somers #endif
531df8bae1dSRodney W. Grimes 		}
532df8bae1dSRodney W. Grimes 	}
533f8429ca2SBruce M Simpson 	/* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
534f8429ca2SBruce M Simpson 	if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
535f8429ca2SBruce M Simpson 		ipstat.ips_cantforward++;
536f8429ca2SBruce M Simpson 		m_freem(m);
537f8429ca2SBruce M Simpson 		return;
538f8429ca2SBruce M Simpson 	}
539df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
540df8bae1dSRodney W. Grimes 		struct in_multi *inm;
541df8bae1dSRodney W. Grimes 		if (ip_mrouter) {
542df8bae1dSRodney W. Grimes 			/*
543df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, all
544df8bae1dSRodney W. Grimes 			 * incoming multicast packets are passed to the
545df8bae1dSRodney W. Grimes 			 * kernel-level multicast forwarding function.
546df8bae1dSRodney W. Grimes 			 * The packet is returned (relatively) intact; if
547df8bae1dSRodney W. Grimes 			 * ip_mforward() returns a non-zero value, the packet
548df8bae1dSRodney W. Grimes 			 * must be discarded, else it may be accepted below.
549df8bae1dSRodney W. Grimes 			 */
550bbb4330bSLuigi Rizzo 			if (ip_mforward &&
551bbb4330bSLuigi Rizzo 			    ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
552df8bae1dSRodney W. Grimes 				ipstat.ips_cantforward++;
553df8bae1dSRodney W. Grimes 				m_freem(m);
554c67b1d17SGarrett Wollman 				return;
555df8bae1dSRodney W. Grimes 			}
556df8bae1dSRodney W. Grimes 
557df8bae1dSRodney W. Grimes 			/*
55811612afaSDima Dorfman 			 * The process-level routing daemon needs to receive
559df8bae1dSRodney W. Grimes 			 * all multicast IGMP packets, whether or not this
560df8bae1dSRodney W. Grimes 			 * host belongs to their destination groups.
561df8bae1dSRodney W. Grimes 			 */
562df8bae1dSRodney W. Grimes 			if (ip->ip_p == IPPROTO_IGMP)
563df8bae1dSRodney W. Grimes 				goto ours;
564df8bae1dSRodney W. Grimes 			ipstat.ips_forward++;
565df8bae1dSRodney W. Grimes 		}
566df8bae1dSRodney W. Grimes 		/*
567df8bae1dSRodney W. Grimes 		 * See if we belong to the destination multicast group on the
568df8bae1dSRodney W. Grimes 		 * arrival interface.
569df8bae1dSRodney W. Grimes 		 */
570dd5a318bSRobert Watson 		IN_MULTI_LOCK();
571df8bae1dSRodney W. Grimes 		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
572dd5a318bSRobert Watson 		IN_MULTI_UNLOCK();
573df8bae1dSRodney W. Grimes 		if (inm == NULL) {
57482c39223SGarrett Wollman 			ipstat.ips_notmember++;
575df8bae1dSRodney W. Grimes 			m_freem(m);
576c67b1d17SGarrett Wollman 			return;
577df8bae1dSRodney W. Grimes 		}
578df8bae1dSRodney W. Grimes 		goto ours;
579df8bae1dSRodney W. Grimes 	}
580df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
581df8bae1dSRodney W. Grimes 		goto ours;
582df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == INADDR_ANY)
583df8bae1dSRodney W. Grimes 		goto ours;
584df8bae1dSRodney W. Grimes 
5856a800098SYoshinobu Inoue 	/*
5866a800098SYoshinobu Inoue 	 * FAITH(Firewall Aided Internet Translator)
5876a800098SYoshinobu Inoue 	 */
5886a800098SYoshinobu Inoue 	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
5896a800098SYoshinobu Inoue 		if (ip_keepfaith) {
5906a800098SYoshinobu Inoue 			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
5916a800098SYoshinobu Inoue 				goto ours;
5926a800098SYoshinobu Inoue 		}
5936a800098SYoshinobu Inoue 		m_freem(m);
5946a800098SYoshinobu Inoue 		return;
5956a800098SYoshinobu Inoue 	}
5969494d596SBrooks Davis 
597df8bae1dSRodney W. Grimes 	/*
598df8bae1dSRodney W. Grimes 	 * Not for us; forward if possible and desirable.
599df8bae1dSRodney W. Grimes 	 */
600df8bae1dSRodney W. Grimes 	if (ipforwarding == 0) {
601df8bae1dSRodney W. Grimes 		ipstat.ips_cantforward++;
602df8bae1dSRodney W. Grimes 		m_freem(m);
603546f251bSChris D. Faulhaber 	} else {
6041dfcf0d2SAndre Oppermann #if defined(IPSEC) || defined(FAST_IPSEC)
6051dfcf0d2SAndre Oppermann 		if (ip_ipsec_fwd(m))
606546f251bSChris D. Faulhaber 			goto bad;
607546f251bSChris D. Faulhaber #endif /* IPSEC */
6089b932e9eSAndre Oppermann 		ip_forward(m, dchg);
609546f251bSChris D. Faulhaber 	}
610c67b1d17SGarrett Wollman 	return;
611df8bae1dSRodney W. Grimes 
612df8bae1dSRodney W. Grimes ours:
613d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH
614d0ebc0d2SYaroslav Tykhiy 	/*
615d0ebc0d2SYaroslav Tykhiy 	 * IPSTEALTH: Process non-routing options only
616d0ebc0d2SYaroslav Tykhiy 	 * if the packet is destined for us.
617d0ebc0d2SYaroslav Tykhiy 	 */
6182b25acc1SLuigi Rizzo 	if (ipstealth && hlen > sizeof (struct ip) &&
6199b932e9eSAndre Oppermann 	    ip_dooptions(m, 1))
620d0ebc0d2SYaroslav Tykhiy 		return;
621d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */
622d0ebc0d2SYaroslav Tykhiy 
6235da9f8faSJosef Karthauser 	/* Count the packet in the ip address stats */
6245da9f8faSJosef Karthauser 	if (ia != NULL) {
6255da9f8faSJosef Karthauser 		ia->ia_ifa.if_ipackets++;
6265da9f8faSJosef Karthauser 		ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
6275da9f8faSJosef Karthauser 	}
628100ba1a6SJordan K. Hubbard 
62963f8d699SJordan K. Hubbard 	/*
630b6ea1aa5SRuslan Ermilov 	 * Attempt reassembly; if it succeeds, proceed.
631ac9d7e26SMax Laier 	 * ip_reass() will return a different mbuf.
632df8bae1dSRodney W. Grimes 	 */
633f0cada84SAndre Oppermann 	if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
634f0cada84SAndre Oppermann 		m = ip_reass(m);
635f0cada84SAndre Oppermann 		if (m == NULL)
636c67b1d17SGarrett Wollman 			return;
6376a800098SYoshinobu Inoue 		ip = mtod(m, struct ip *);
6387e2df452SRuslan Ermilov 		/* Get the header length of the reassembled packet */
63953be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
640f0cada84SAndre Oppermann 	}
641f0cada84SAndre Oppermann 
642f0cada84SAndre Oppermann 	/*
643f0cada84SAndre Oppermann 	 * Further protocols expect the packet length to be w/o the
644f0cada84SAndre Oppermann 	 * IP header.
645f0cada84SAndre Oppermann 	 */
646df8bae1dSRodney W. Grimes 	ip->ip_len -= hlen;
647df8bae1dSRodney W. Grimes 
6481dfcf0d2SAndre Oppermann #if defined(IPSEC) || defined(FAST_IPSEC)
64933841545SHajimu UMEMOTO 	/*
65033841545SHajimu UMEMOTO 	 * enforce IPsec policy checking if we are seeing last header.
65133841545SHajimu UMEMOTO 	 * note that we do not visit this with protocols with pcb layer
65233841545SHajimu UMEMOTO 	 * code - like udp/tcp/raw ip.
65333841545SHajimu UMEMOTO 	 */
6541dfcf0d2SAndre Oppermann 	if (ip_ipsec_input(m))
65533841545SHajimu UMEMOTO 		goto bad;
6561dfcf0d2SAndre Oppermann #endif /* IPSEC */
65733841545SHajimu UMEMOTO 
658df8bae1dSRodney W. Grimes 	/*
659df8bae1dSRodney W. Grimes 	 * Switch out to protocol's input routine.
660df8bae1dSRodney W. Grimes 	 */
661df8bae1dSRodney W. Grimes 	ipstat.ips_delivered++;
6629b932e9eSAndre Oppermann 
6632b25acc1SLuigi Rizzo 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
664c67b1d17SGarrett Wollman 	return;
665df8bae1dSRodney W. Grimes bad:
666df8bae1dSRodney W. Grimes 	m_freem(m);
667c67b1d17SGarrett Wollman }
668c67b1d17SGarrett Wollman 
669c67b1d17SGarrett Wollman /*
670d248c7d7SRobert Watson  * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
671d248c7d7SRobert Watson  * max has slightly different semantics than the sysctl, for historical
672d248c7d7SRobert Watson  * reasons.
673d248c7d7SRobert Watson  */
674d248c7d7SRobert Watson static void
675d248c7d7SRobert Watson maxnipq_update(void)
676d248c7d7SRobert Watson {
677d248c7d7SRobert Watson 
678d248c7d7SRobert Watson 	/*
679d248c7d7SRobert Watson 	 * -1 for unlimited allocation.
680d248c7d7SRobert Watson 	 */
681d248c7d7SRobert Watson 	if (maxnipq < 0)
682d248c7d7SRobert Watson 		uma_zone_set_max(ipq_zone, 0);
683d248c7d7SRobert Watson 	/*
684d248c7d7SRobert Watson 	 * Positive number for specific bound.
685d248c7d7SRobert Watson 	 */
686d248c7d7SRobert Watson 	if (maxnipq > 0)
687d248c7d7SRobert Watson 		uma_zone_set_max(ipq_zone, maxnipq);
688d248c7d7SRobert Watson 	/*
689d248c7d7SRobert Watson 	 * Zero specifies no further fragment queue allocation -- set the
690d248c7d7SRobert Watson 	 * bound very low, but rely on implementation elsewhere to actually
691d248c7d7SRobert Watson 	 * prevent allocation and reclaim current queues.
692d248c7d7SRobert Watson 	 */
693d248c7d7SRobert Watson 	if (maxnipq == 0)
694d248c7d7SRobert Watson 		uma_zone_set_max(ipq_zone, 1);
695d248c7d7SRobert Watson }
696d248c7d7SRobert Watson 
6974f590175SPaul Saab static void
6984f590175SPaul Saab ipq_zone_change(void *tag)
6994f590175SPaul Saab {
7004f590175SPaul Saab 
7014f590175SPaul Saab 	if (maxnipq > 0 && maxnipq < (nmbclusters / 32)) {
7024f590175SPaul Saab 		maxnipq = nmbclusters / 32;
7034f590175SPaul Saab 		maxnipq_update();
7044f590175SPaul Saab 	}
7054f590175SPaul Saab }
7064f590175SPaul Saab 
707d248c7d7SRobert Watson static int
708d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
709d248c7d7SRobert Watson {
710d248c7d7SRobert Watson 	int error, i;
711d248c7d7SRobert Watson 
712d248c7d7SRobert Watson 	i = maxnipq;
713d248c7d7SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
714d248c7d7SRobert Watson 	if (error || !req->newptr)
715d248c7d7SRobert Watson 		return (error);
716d248c7d7SRobert Watson 
717d248c7d7SRobert Watson 	/*
718d248c7d7SRobert Watson 	 * XXXRW: Might be a good idea to sanity check the argument and place
719d248c7d7SRobert Watson 	 * an extreme upper bound.
720d248c7d7SRobert Watson 	 */
721d248c7d7SRobert Watson 	if (i < -1)
722d248c7d7SRobert Watson 		return (EINVAL);
723d248c7d7SRobert Watson 	maxnipq = i;
724d248c7d7SRobert Watson 	maxnipq_update();
725d248c7d7SRobert Watson 	return (0);
726d248c7d7SRobert Watson }
727d248c7d7SRobert Watson 
728d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
729d248c7d7SRobert Watson     NULL, 0, sysctl_maxnipq, "I",
730d248c7d7SRobert Watson     "Maximum number of IPv4 fragment reassembly queue entries");
731d248c7d7SRobert Watson 
732d248c7d7SRobert Watson /*
7338948e4baSArchie Cobbs  * Take incoming datagram fragment and try to reassemble it into
734f0cada84SAndre Oppermann  * whole datagram.  If the argument is the first fragment or one
735f0cada84SAndre Oppermann  * in between the function will return NULL and store the mbuf
736f0cada84SAndre Oppermann  * in the fragment chain.  If the argument is the last fragment
737f0cada84SAndre Oppermann  * the packet will be reassembled and the pointer to the new
738f0cada84SAndre Oppermann  * mbuf returned for further processing.  Only m_tags attached
739f0cada84SAndre Oppermann  * to the first packet/fragment are preserved.
740f0cada84SAndre Oppermann  * The IP header is *NOT* adjusted out of iplen.
741df8bae1dSRodney W. Grimes  */
742f0cada84SAndre Oppermann struct mbuf *
743f0cada84SAndre Oppermann ip_reass(struct mbuf *m)
744df8bae1dSRodney W. Grimes {
745f0cada84SAndre Oppermann 	struct ip *ip;
746f0cada84SAndre Oppermann 	struct mbuf *p, *q, *nq, *t;
747f0cada84SAndre Oppermann 	struct ipq *fp = NULL;
748f0cada84SAndre Oppermann 	struct ipqhead *head;
749f0cada84SAndre Oppermann 	int i, hlen, next;
75059dfcba4SHajimu UMEMOTO 	u_int8_t ecn, ecn0;
751f0cada84SAndre Oppermann 	u_short hash;
752df8bae1dSRodney W. Grimes 
753800af1fbSMaxim Konovalov 	/* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
754800af1fbSMaxim Konovalov 	if (maxnipq == 0 || maxfragsperpacket == 0) {
755f0cada84SAndre Oppermann 		ipstat.ips_fragments++;
756f0cada84SAndre Oppermann 		ipstat.ips_fragdropped++;
7579d804f81SAndre Oppermann 		m_freem(m);
7589d804f81SAndre Oppermann 		return (NULL);
759f0cada84SAndre Oppermann 	}
7602fad1e93SSam Leffler 
761f0cada84SAndre Oppermann 	ip = mtod(m, struct ip *);
762f0cada84SAndre Oppermann 	hlen = ip->ip_hl << 2;
763f0cada84SAndre Oppermann 
764f0cada84SAndre Oppermann 	hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
765f0cada84SAndre Oppermann 	head = &ipq[hash];
766f0cada84SAndre Oppermann 	IPQ_LOCK();
767f0cada84SAndre Oppermann 
768f0cada84SAndre Oppermann 	/*
769f0cada84SAndre Oppermann 	 * Look for queue of fragments
770f0cada84SAndre Oppermann 	 * of this datagram.
771f0cada84SAndre Oppermann 	 */
772f0cada84SAndre Oppermann 	TAILQ_FOREACH(fp, head, ipq_list)
773f0cada84SAndre Oppermann 		if (ip->ip_id == fp->ipq_id &&
774f0cada84SAndre Oppermann 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
775f0cada84SAndre Oppermann 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
776f0cada84SAndre Oppermann #ifdef MAC
777f0cada84SAndre Oppermann 		    mac_fragment_match(m, fp) &&
778f0cada84SAndre Oppermann #endif
779f0cada84SAndre Oppermann 		    ip->ip_p == fp->ipq_p)
780f0cada84SAndre Oppermann 			goto found;
781f0cada84SAndre Oppermann 
782f0cada84SAndre Oppermann 	fp = NULL;
783f0cada84SAndre Oppermann 
784f0cada84SAndre Oppermann 	/*
785d248c7d7SRobert Watson 	 * Attempt to trim the number of allocated fragment queues if it
786d248c7d7SRobert Watson 	 * exceeds the administrative limit.
787f0cada84SAndre Oppermann 	 */
788f0cada84SAndre Oppermann 	if ((nipq > maxnipq) && (maxnipq > 0)) {
789f0cada84SAndre Oppermann 		/*
790f0cada84SAndre Oppermann 		 * drop something from the tail of the current queue
791f0cada84SAndre Oppermann 		 * before proceeding further
792f0cada84SAndre Oppermann 		 */
793f0cada84SAndre Oppermann 		struct ipq *q = TAILQ_LAST(head, ipqhead);
794f0cada84SAndre Oppermann 		if (q == NULL) {   /* gak */
795f0cada84SAndre Oppermann 			for (i = 0; i < IPREASS_NHASH; i++) {
796f0cada84SAndre Oppermann 				struct ipq *r = TAILQ_LAST(&ipq[i], ipqhead);
797f0cada84SAndre Oppermann 				if (r) {
798f0cada84SAndre Oppermann 					ipstat.ips_fragtimeout += r->ipq_nfrags;
799f0cada84SAndre Oppermann 					ip_freef(&ipq[i], r);
800f0cada84SAndre Oppermann 					break;
801f0cada84SAndre Oppermann 				}
802f0cada84SAndre Oppermann 			}
803f0cada84SAndre Oppermann 		} else {
804f0cada84SAndre Oppermann 			ipstat.ips_fragtimeout += q->ipq_nfrags;
805f0cada84SAndre Oppermann 			ip_freef(head, q);
806f0cada84SAndre Oppermann 		}
807f0cada84SAndre Oppermann 	}
808f0cada84SAndre Oppermann 
809f0cada84SAndre Oppermann found:
810f0cada84SAndre Oppermann 	/*
811f0cada84SAndre Oppermann 	 * Adjust ip_len to not reflect header,
812f0cada84SAndre Oppermann 	 * convert offset of this to bytes.
813f0cada84SAndre Oppermann 	 */
814f0cada84SAndre Oppermann 	ip->ip_len -= hlen;
815f0cada84SAndre Oppermann 	if (ip->ip_off & IP_MF) {
816f0cada84SAndre Oppermann 		/*
817f0cada84SAndre Oppermann 		 * Make sure that fragments have a data length
818f0cada84SAndre Oppermann 		 * that's a non-zero multiple of 8 bytes.
819f0cada84SAndre Oppermann 		 */
820f0cada84SAndre Oppermann 		if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
821f0cada84SAndre Oppermann 			ipstat.ips_toosmall++; /* XXX */
822f0cada84SAndre Oppermann 			goto dropfrag;
823f0cada84SAndre Oppermann 		}
824f0cada84SAndre Oppermann 		m->m_flags |= M_FRAG;
825f0cada84SAndre Oppermann 	} else
826f0cada84SAndre Oppermann 		m->m_flags &= ~M_FRAG;
827f0cada84SAndre Oppermann 	ip->ip_off <<= 3;
828f0cada84SAndre Oppermann 
829f0cada84SAndre Oppermann 
830f0cada84SAndre Oppermann 	/*
831f0cada84SAndre Oppermann 	 * Attempt reassembly; if it succeeds, proceed.
832f0cada84SAndre Oppermann 	 * ip_reass() will return a different mbuf.
833f0cada84SAndre Oppermann 	 */
834f0cada84SAndre Oppermann 	ipstat.ips_fragments++;
835f0cada84SAndre Oppermann 	m->m_pkthdr.header = ip;
836f0cada84SAndre Oppermann 
837f0cada84SAndre Oppermann 	/* Previous ip_reass() started here. */
838df8bae1dSRodney W. Grimes 	/*
839df8bae1dSRodney W. Grimes 	 * Presence of header sizes in mbufs
840df8bae1dSRodney W. Grimes 	 * would confuse code below.
841df8bae1dSRodney W. Grimes 	 */
842df8bae1dSRodney W. Grimes 	m->m_data += hlen;
843df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
844df8bae1dSRodney W. Grimes 
845df8bae1dSRodney W. Grimes 	/*
846df8bae1dSRodney W. Grimes 	 * If first fragment to arrive, create a reassembly queue.
847df8bae1dSRodney W. Grimes 	 */
848042bbfa3SRobert Watson 	if (fp == NULL) {
849d248c7d7SRobert Watson 		fp = uma_zalloc(ipq_zone, M_NOWAIT);
850d248c7d7SRobert Watson 		if (fp == NULL)
851df8bae1dSRodney W. Grimes 			goto dropfrag;
85236b0360bSRobert Watson #ifdef MAC
8535e7ce478SRobert Watson 		if (mac_init_ipq(fp, M_NOWAIT) != 0) {
854d248c7d7SRobert Watson 			uma_zfree(ipq_zone, fp);
8551d7d0bfeSPawel Jakub Dawidek 			fp = NULL;
8565e7ce478SRobert Watson 			goto dropfrag;
8575e7ce478SRobert Watson 		}
85836b0360bSRobert Watson 		mac_create_ipq(m, fp);
85936b0360bSRobert Watson #endif
860462b86feSPoul-Henning Kamp 		TAILQ_INSERT_HEAD(head, fp, ipq_list);
861194a213eSAndrey A. Chernov 		nipq++;
862375386e2SMike Silbersack 		fp->ipq_nfrags = 1;
863df8bae1dSRodney W. Grimes 		fp->ipq_ttl = IPFRAGTTL;
864df8bae1dSRodney W. Grimes 		fp->ipq_p = ip->ip_p;
865df8bae1dSRodney W. Grimes 		fp->ipq_id = ip->ip_id;
8666effc713SDoug Rabson 		fp->ipq_src = ip->ip_src;
8676effc713SDoug Rabson 		fp->ipq_dst = ip->ip_dst;
868af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
869af38c68cSLuigi Rizzo 		m->m_nextpkt = NULL;
870800af1fbSMaxim Konovalov 		goto done;
87136b0360bSRobert Watson 	} else {
872375386e2SMike Silbersack 		fp->ipq_nfrags++;
87336b0360bSRobert Watson #ifdef MAC
87436b0360bSRobert Watson 		mac_update_ipq(m, fp);
87536b0360bSRobert Watson #endif
876df8bae1dSRodney W. Grimes 	}
877df8bae1dSRodney W. Grimes 
8786effc713SDoug Rabson #define GETIP(m)	((struct ip*)((m)->m_pkthdr.header))
8796effc713SDoug Rabson 
880df8bae1dSRodney W. Grimes 	/*
88159dfcba4SHajimu UMEMOTO 	 * Handle ECN by comparing this segment with the first one;
88259dfcba4SHajimu UMEMOTO 	 * if CE is set, do not lose CE.
88359dfcba4SHajimu UMEMOTO 	 * drop if CE and not-ECT are mixed for the same packet.
88459dfcba4SHajimu UMEMOTO 	 */
88559dfcba4SHajimu UMEMOTO 	ecn = ip->ip_tos & IPTOS_ECN_MASK;
88659dfcba4SHajimu UMEMOTO 	ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
88759dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_CE) {
88859dfcba4SHajimu UMEMOTO 		if (ecn0 == IPTOS_ECN_NOTECT)
88959dfcba4SHajimu UMEMOTO 			goto dropfrag;
89059dfcba4SHajimu UMEMOTO 		if (ecn0 != IPTOS_ECN_CE)
89159dfcba4SHajimu UMEMOTO 			GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
89259dfcba4SHajimu UMEMOTO 	}
89359dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
89459dfcba4SHajimu UMEMOTO 		goto dropfrag;
89559dfcba4SHajimu UMEMOTO 
89659dfcba4SHajimu UMEMOTO 	/*
897df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
898df8bae1dSRodney W. Grimes 	 */
8996effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
9006effc713SDoug Rabson 		if (GETIP(q)->ip_off > ip->ip_off)
901df8bae1dSRodney W. Grimes 			break;
902df8bae1dSRodney W. Grimes 
903df8bae1dSRodney W. Grimes 	/*
904df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
905df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
906af38c68cSLuigi Rizzo 	 * segment.  If it provides all of our data, drop us, otherwise
907af38c68cSLuigi Rizzo 	 * stick new segment in the proper place.
908db4f9cc7SJonathan Lemon 	 *
909db4f9cc7SJonathan Lemon 	 * If some of the data is dropped from the the preceding
910db4f9cc7SJonathan Lemon 	 * segment, then it's checksum is invalidated.
911df8bae1dSRodney W. Grimes 	 */
9126effc713SDoug Rabson 	if (p) {
9136effc713SDoug Rabson 		i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
914df8bae1dSRodney W. Grimes 		if (i > 0) {
915df8bae1dSRodney W. Grimes 			if (i >= ip->ip_len)
916df8bae1dSRodney W. Grimes 				goto dropfrag;
9176a800098SYoshinobu Inoue 			m_adj(m, i);
918db4f9cc7SJonathan Lemon 			m->m_pkthdr.csum_flags = 0;
919df8bae1dSRodney W. Grimes 			ip->ip_off += i;
920df8bae1dSRodney W. Grimes 			ip->ip_len -= i;
921df8bae1dSRodney W. Grimes 		}
922af38c68cSLuigi Rizzo 		m->m_nextpkt = p->m_nextpkt;
923af38c68cSLuigi Rizzo 		p->m_nextpkt = m;
924af38c68cSLuigi Rizzo 	} else {
925af38c68cSLuigi Rizzo 		m->m_nextpkt = fp->ipq_frags;
926af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
927df8bae1dSRodney W. Grimes 	}
928df8bae1dSRodney W. Grimes 
929df8bae1dSRodney W. Grimes 	/*
930df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
931df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
932df8bae1dSRodney W. Grimes 	 */
9336effc713SDoug Rabson 	for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
934af38c68cSLuigi Rizzo 	     q = nq) {
935b36f5b37SMaxim Konovalov 		i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
9366effc713SDoug Rabson 		if (i < GETIP(q)->ip_len) {
9376effc713SDoug Rabson 			GETIP(q)->ip_len -= i;
9386effc713SDoug Rabson 			GETIP(q)->ip_off += i;
9396effc713SDoug Rabson 			m_adj(q, i);
940db4f9cc7SJonathan Lemon 			q->m_pkthdr.csum_flags = 0;
941df8bae1dSRodney W. Grimes 			break;
942df8bae1dSRodney W. Grimes 		}
9436effc713SDoug Rabson 		nq = q->m_nextpkt;
944af38c68cSLuigi Rizzo 		m->m_nextpkt = nq;
94599e8617dSMaxim Konovalov 		ipstat.ips_fragdropped++;
946375386e2SMike Silbersack 		fp->ipq_nfrags--;
9476effc713SDoug Rabson 		m_freem(q);
948df8bae1dSRodney W. Grimes 	}
949df8bae1dSRodney W. Grimes 
950df8bae1dSRodney W. Grimes 	/*
951375386e2SMike Silbersack 	 * Check for complete reassembly and perform frag per packet
952375386e2SMike Silbersack 	 * limiting.
953375386e2SMike Silbersack 	 *
954375386e2SMike Silbersack 	 * Frag limiting is performed here so that the nth frag has
955375386e2SMike Silbersack 	 * a chance to complete the packet before we drop the packet.
956375386e2SMike Silbersack 	 * As a result, n+1 frags are actually allowed per packet, but
957375386e2SMike Silbersack 	 * only n will ever be stored. (n = maxfragsperpacket.)
958375386e2SMike Silbersack 	 *
959df8bae1dSRodney W. Grimes 	 */
9606effc713SDoug Rabson 	next = 0;
9616effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
962375386e2SMike Silbersack 		if (GETIP(q)->ip_off != next) {
96399e8617dSMaxim Konovalov 			if (fp->ipq_nfrags > maxfragsperpacket) {
96499e8617dSMaxim Konovalov 				ipstat.ips_fragdropped += fp->ipq_nfrags;
965375386e2SMike Silbersack 				ip_freef(head, fp);
96699e8617dSMaxim Konovalov 			}
967f0cada84SAndre Oppermann 			goto done;
968375386e2SMike Silbersack 		}
9696effc713SDoug Rabson 		next += GETIP(q)->ip_len;
9706effc713SDoug Rabson 	}
9716effc713SDoug Rabson 	/* Make sure the last packet didn't have the IP_MF flag */
972375386e2SMike Silbersack 	if (p->m_flags & M_FRAG) {
97399e8617dSMaxim Konovalov 		if (fp->ipq_nfrags > maxfragsperpacket) {
97499e8617dSMaxim Konovalov 			ipstat.ips_fragdropped += fp->ipq_nfrags;
975375386e2SMike Silbersack 			ip_freef(head, fp);
97699e8617dSMaxim Konovalov 		}
977f0cada84SAndre Oppermann 		goto done;
978375386e2SMike Silbersack 	}
979df8bae1dSRodney W. Grimes 
980df8bae1dSRodney W. Grimes 	/*
981430d30d8SBill Fenner 	 * Reassembly is complete.  Make sure the packet is a sane size.
982430d30d8SBill Fenner 	 */
9836effc713SDoug Rabson 	q = fp->ipq_frags;
9846effc713SDoug Rabson 	ip = GETIP(q);
98553be11f6SPoul-Henning Kamp 	if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
986430d30d8SBill Fenner 		ipstat.ips_toolong++;
98799e8617dSMaxim Konovalov 		ipstat.ips_fragdropped += fp->ipq_nfrags;
988462b86feSPoul-Henning Kamp 		ip_freef(head, fp);
989f0cada84SAndre Oppermann 		goto done;
990430d30d8SBill Fenner 	}
991430d30d8SBill Fenner 
992430d30d8SBill Fenner 	/*
993430d30d8SBill Fenner 	 * Concatenate fragments.
994df8bae1dSRodney W. Grimes 	 */
9956effc713SDoug Rabson 	m = q;
996df8bae1dSRodney W. Grimes 	t = m->m_next;
99702410549SRobert Watson 	m->m_next = NULL;
998df8bae1dSRodney W. Grimes 	m_cat(m, t);
9996effc713SDoug Rabson 	nq = q->m_nextpkt;
100002410549SRobert Watson 	q->m_nextpkt = NULL;
10016effc713SDoug Rabson 	for (q = nq; q != NULL; q = nq) {
10026effc713SDoug Rabson 		nq = q->m_nextpkt;
1003945aa40dSDoug Rabson 		q->m_nextpkt = NULL;
1004db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1005db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1006a8db1d93SJonathan Lemon 		m_cat(m, q);
1007df8bae1dSRodney W. Grimes 	}
10086edb555dSOleg Bulyzhin 	/*
10096edb555dSOleg Bulyzhin 	 * In order to do checksumming faster we do 'end-around carry' here
10106edb555dSOleg Bulyzhin 	 * (and not in for{} loop), though it implies we are not going to
10116edb555dSOleg Bulyzhin 	 * reassemble more than 64k fragments.
10126edb555dSOleg Bulyzhin 	 */
10136edb555dSOleg Bulyzhin 	m->m_pkthdr.csum_data =
10146edb555dSOleg Bulyzhin 	    (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16);
101536b0360bSRobert Watson #ifdef MAC
101636b0360bSRobert Watson 	mac_create_datagram_from_ipq(fp, m);
101736b0360bSRobert Watson 	mac_destroy_ipq(fp);
101836b0360bSRobert Watson #endif
1019df8bae1dSRodney W. Grimes 
1020df8bae1dSRodney W. Grimes 	/*
1021f0cada84SAndre Oppermann 	 * Create header for new ip packet by modifying header of first
1022f0cada84SAndre Oppermann 	 * packet;  dequeue and discard fragment reassembly header.
1023df8bae1dSRodney W. Grimes 	 * Make header visible.
1024df8bae1dSRodney W. Grimes 	 */
1025f0cada84SAndre Oppermann 	ip->ip_len = (ip->ip_hl << 2) + next;
10266effc713SDoug Rabson 	ip->ip_src = fp->ipq_src;
10276effc713SDoug Rabson 	ip->ip_dst = fp->ipq_dst;
1028462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(head, fp, ipq_list);
1029194a213eSAndrey A. Chernov 	nipq--;
1030d248c7d7SRobert Watson 	uma_zfree(ipq_zone, fp);
103153be11f6SPoul-Henning Kamp 	m->m_len += (ip->ip_hl << 2);
103253be11f6SPoul-Henning Kamp 	m->m_data -= (ip->ip_hl << 2);
1033df8bae1dSRodney W. Grimes 	/* some debugging cruft by sklower, below, will go away soon */
1034a5554bf0SPoul-Henning Kamp 	if (m->m_flags & M_PKTHDR)	/* XXX this should be done elsewhere */
1035a5554bf0SPoul-Henning Kamp 		m_fixhdr(m);
1036f0cada84SAndre Oppermann 	ipstat.ips_reassembled++;
1037f0cada84SAndre Oppermann 	IPQ_UNLOCK();
10386a800098SYoshinobu Inoue 	return (m);
1039df8bae1dSRodney W. Grimes 
1040df8bae1dSRodney W. Grimes dropfrag:
1041df8bae1dSRodney W. Grimes 	ipstat.ips_fragdropped++;
1042042bbfa3SRobert Watson 	if (fp != NULL)
1043375386e2SMike Silbersack 		fp->ipq_nfrags--;
1044df8bae1dSRodney W. Grimes 	m_freem(m);
1045f0cada84SAndre Oppermann done:
1046f0cada84SAndre Oppermann 	IPQ_UNLOCK();
1047f0cada84SAndre Oppermann 	return (NULL);
10486effc713SDoug Rabson 
10496effc713SDoug Rabson #undef GETIP
1050df8bae1dSRodney W. Grimes }
1051df8bae1dSRodney W. Grimes 
1052df8bae1dSRodney W. Grimes /*
1053df8bae1dSRodney W. Grimes  * Free a fragment reassembly header and all
1054df8bae1dSRodney W. Grimes  * associated datagrams.
1055df8bae1dSRodney W. Grimes  */
10560312fbe9SPoul-Henning Kamp static void
1057f2565d68SRobert Watson ip_freef(struct ipqhead *fhp, struct ipq *fp)
1058df8bae1dSRodney W. Grimes {
1059f2565d68SRobert Watson 	struct mbuf *q;
1060df8bae1dSRodney W. Grimes 
10612fad1e93SSam Leffler 	IPQ_LOCK_ASSERT();
10622fad1e93SSam Leffler 
10636effc713SDoug Rabson 	while (fp->ipq_frags) {
10646effc713SDoug Rabson 		q = fp->ipq_frags;
10656effc713SDoug Rabson 		fp->ipq_frags = q->m_nextpkt;
10666effc713SDoug Rabson 		m_freem(q);
1067df8bae1dSRodney W. Grimes 	}
1068462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(fhp, fp, ipq_list);
1069d248c7d7SRobert Watson 	uma_zfree(ipq_zone, fp);
1070194a213eSAndrey A. Chernov 	nipq--;
1071df8bae1dSRodney W. Grimes }
1072df8bae1dSRodney W. Grimes 
1073df8bae1dSRodney W. Grimes /*
1074df8bae1dSRodney W. Grimes  * IP timer processing;
1075df8bae1dSRodney W. Grimes  * if a timer expires on a reassembly
1076df8bae1dSRodney W. Grimes  * queue, discard it.
1077df8bae1dSRodney W. Grimes  */
1078df8bae1dSRodney W. Grimes void
1079f2565d68SRobert Watson ip_slowtimo(void)
1080df8bae1dSRodney W. Grimes {
1081f2565d68SRobert Watson 	struct ipq *fp;
1082194a213eSAndrey A. Chernov 	int i;
1083df8bae1dSRodney W. Grimes 
10842fad1e93SSam Leffler 	IPQ_LOCK();
1085194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1086462b86feSPoul-Henning Kamp 		for(fp = TAILQ_FIRST(&ipq[i]); fp;) {
1087462b86feSPoul-Henning Kamp 			struct ipq *fpp;
1088462b86feSPoul-Henning Kamp 
1089462b86feSPoul-Henning Kamp 			fpp = fp;
1090462b86feSPoul-Henning Kamp 			fp = TAILQ_NEXT(fp, ipq_list);
1091462b86feSPoul-Henning Kamp 			if(--fpp->ipq_ttl == 0) {
109299e8617dSMaxim Konovalov 				ipstat.ips_fragtimeout += fpp->ipq_nfrags;
1093462b86feSPoul-Henning Kamp 				ip_freef(&ipq[i], fpp);
1094df8bae1dSRodney W. Grimes 			}
1095df8bae1dSRodney W. Grimes 		}
1096194a213eSAndrey A. Chernov 	}
1097690a6055SJesper Skriver 	/*
1098690a6055SJesper Skriver 	 * If we are over the maximum number of fragments
1099690a6055SJesper Skriver 	 * (due to the limit being lowered), drain off
1100690a6055SJesper Skriver 	 * enough to get down to the new limit.
1101690a6055SJesper Skriver 	 */
1102a75a485dSMike Silbersack 	if (maxnipq >= 0 && nipq > maxnipq) {
1103690a6055SJesper Skriver 		for (i = 0; i < IPREASS_NHASH; i++) {
1104b36f5b37SMaxim Konovalov 			while (nipq > maxnipq && !TAILQ_EMPTY(&ipq[i])) {
110599e8617dSMaxim Konovalov 				ipstat.ips_fragdropped +=
110699e8617dSMaxim Konovalov 				    TAILQ_FIRST(&ipq[i])->ipq_nfrags;
1107690a6055SJesper Skriver 				ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
1108690a6055SJesper Skriver 			}
1109690a6055SJesper Skriver 		}
1110690a6055SJesper Skriver 	}
11112fad1e93SSam Leffler 	IPQ_UNLOCK();
1112df8bae1dSRodney W. Grimes }
1113df8bae1dSRodney W. Grimes 
1114df8bae1dSRodney W. Grimes /*
1115df8bae1dSRodney W. Grimes  * Drain off all datagram fragments.
1116df8bae1dSRodney W. Grimes  */
1117df8bae1dSRodney W. Grimes void
1118f2565d68SRobert Watson ip_drain(void)
1119df8bae1dSRodney W. Grimes {
1120194a213eSAndrey A. Chernov 	int     i;
1121ce29ab3aSGarrett Wollman 
11222fad1e93SSam Leffler 	IPQ_LOCK();
1123194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1124462b86feSPoul-Henning Kamp 		while(!TAILQ_EMPTY(&ipq[i])) {
112599e8617dSMaxim Konovalov 			ipstat.ips_fragdropped +=
112699e8617dSMaxim Konovalov 			    TAILQ_FIRST(&ipq[i])->ipq_nfrags;
1127462b86feSPoul-Henning Kamp 			ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
1128194a213eSAndrey A. Chernov 		}
1129194a213eSAndrey A. Chernov 	}
11302fad1e93SSam Leffler 	IPQ_UNLOCK();
1131ce29ab3aSGarrett Wollman 	in_rtqdrain();
1132df8bae1dSRodney W. Grimes }
1133df8bae1dSRodney W. Grimes 
1134df8bae1dSRodney W. Grimes /*
1135de38924dSAndre Oppermann  * The protocol to be inserted into ip_protox[] must be already registered
1136de38924dSAndre Oppermann  * in inetsw[], either statically or through pf_proto_register().
1137de38924dSAndre Oppermann  */
1138de38924dSAndre Oppermann int
1139de38924dSAndre Oppermann ipproto_register(u_char ipproto)
1140de38924dSAndre Oppermann {
1141de38924dSAndre Oppermann 	struct protosw *pr;
1142de38924dSAndre Oppermann 
1143de38924dSAndre Oppermann 	/* Sanity checks. */
1144de38924dSAndre Oppermann 	if (ipproto == 0)
1145de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1146de38924dSAndre Oppermann 
1147de38924dSAndre Oppermann 	/*
1148de38924dSAndre Oppermann 	 * The protocol slot must not be occupied by another protocol
1149de38924dSAndre Oppermann 	 * already.  An index pointing to IPPROTO_RAW is unused.
1150de38924dSAndre Oppermann 	 */
1151de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1152de38924dSAndre Oppermann 	if (pr == NULL)
1153de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1154de38924dSAndre Oppermann 	if (ip_protox[ipproto] != pr - inetsw)	/* IPPROTO_RAW */
1155de38924dSAndre Oppermann 		return (EEXIST);
1156de38924dSAndre Oppermann 
1157de38924dSAndre Oppermann 	/* Find the protocol position in inetsw[] and set the index. */
1158de38924dSAndre Oppermann 	for (pr = inetdomain.dom_protosw;
1159de38924dSAndre Oppermann 	     pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1160de38924dSAndre Oppermann 		if (pr->pr_domain->dom_family == PF_INET &&
1161de38924dSAndre Oppermann 		    pr->pr_protocol && pr->pr_protocol == ipproto) {
1162de38924dSAndre Oppermann 			/* Be careful to only index valid IP protocols. */
1163db77984cSSam Leffler 			if (pr->pr_protocol < IPPROTO_MAX) {
1164de38924dSAndre Oppermann 				ip_protox[pr->pr_protocol] = pr - inetsw;
1165de38924dSAndre Oppermann 				return (0);
1166de38924dSAndre Oppermann 			} else
1167de38924dSAndre Oppermann 				return (EINVAL);
1168de38924dSAndre Oppermann 		}
1169de38924dSAndre Oppermann 	}
1170de38924dSAndre Oppermann 	return (EPROTONOSUPPORT);
1171de38924dSAndre Oppermann }
1172de38924dSAndre Oppermann 
1173de38924dSAndre Oppermann int
1174de38924dSAndre Oppermann ipproto_unregister(u_char ipproto)
1175de38924dSAndre Oppermann {
1176de38924dSAndre Oppermann 	struct protosw *pr;
1177de38924dSAndre Oppermann 
1178de38924dSAndre Oppermann 	/* Sanity checks. */
1179de38924dSAndre Oppermann 	if (ipproto == 0)
1180de38924dSAndre Oppermann 		return (EPROTONOSUPPORT);
1181de38924dSAndre Oppermann 
1182de38924dSAndre Oppermann 	/* Check if the protocol was indeed registered. */
1183de38924dSAndre Oppermann 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1184de38924dSAndre Oppermann 	if (pr == NULL)
1185de38924dSAndre Oppermann 		return (EPFNOSUPPORT);
1186de38924dSAndre Oppermann 	if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
1187de38924dSAndre Oppermann 		return (ENOENT);
1188de38924dSAndre Oppermann 
1189de38924dSAndre Oppermann 	/* Reset the protocol slot to IPPROTO_RAW. */
1190de38924dSAndre Oppermann 	ip_protox[ipproto] = pr - inetsw;
1191de38924dSAndre Oppermann 	return (0);
1192de38924dSAndre Oppermann }
1193de38924dSAndre Oppermann 
1194df8bae1dSRodney W. Grimes /*
1195df8bae1dSRodney W. Grimes  * Given address of next destination (final or next hop),
1196df8bae1dSRodney W. Grimes  * return internet address info of interface to be used to get there.
1197df8bae1dSRodney W. Grimes  */
1198bd714208SRuslan Ermilov struct in_ifaddr *
1199f2565d68SRobert Watson ip_rtaddr(struct in_addr dst)
1200df8bae1dSRodney W. Grimes {
120197d8d152SAndre Oppermann 	struct route sro;
120202c1c707SAndre Oppermann 	struct sockaddr_in *sin;
120302c1c707SAndre Oppermann 	struct in_ifaddr *ifa;
1204df8bae1dSRodney W. Grimes 
12050cfbbe3bSAndre Oppermann 	bzero(&sro, sizeof(sro));
120697d8d152SAndre Oppermann 	sin = (struct sockaddr_in *)&sro.ro_dst;
1207df8bae1dSRodney W. Grimes 	sin->sin_family = AF_INET;
1208df8bae1dSRodney W. Grimes 	sin->sin_len = sizeof(*sin);
1209df8bae1dSRodney W. Grimes 	sin->sin_addr = dst;
121097d8d152SAndre Oppermann 	rtalloc_ign(&sro, RTF_CLONING);
1211df8bae1dSRodney W. Grimes 
121297d8d152SAndre Oppermann 	if (sro.ro_rt == NULL)
121302410549SRobert Watson 		return (NULL);
121402c1c707SAndre Oppermann 
121597d8d152SAndre Oppermann 	ifa = ifatoia(sro.ro_rt->rt_ifa);
121697d8d152SAndre Oppermann 	RTFREE(sro.ro_rt);
121702410549SRobert Watson 	return (ifa);
1218df8bae1dSRodney W. Grimes }
1219df8bae1dSRodney W. Grimes 
1220df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = {
1221df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1222df8bae1dSRodney W. Grimes 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1223df8bae1dSRodney W. Grimes 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1224df8bae1dSRodney W. Grimes 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1225fcaf9f91SMike Silbersack 	0,		0,		EHOSTUNREACH,	0,
12263b8123b7SJesper Skriver 	ENOPROTOOPT,	ECONNREFUSED
1227df8bae1dSRodney W. Grimes };
1228df8bae1dSRodney W. Grimes 
1229df8bae1dSRodney W. Grimes /*
1230df8bae1dSRodney W. Grimes  * Forward a packet.  If some error occurs return the sender
1231df8bae1dSRodney W. Grimes  * an icmp packet.  Note we can't always generate a meaningful
1232df8bae1dSRodney W. Grimes  * icmp message because icmp doesn't have a large enough repertoire
1233df8bae1dSRodney W. Grimes  * of codes and types.
1234df8bae1dSRodney W. Grimes  *
1235df8bae1dSRodney W. Grimes  * If not forwarding, just drop the packet.  This could be confusing
1236df8bae1dSRodney W. Grimes  * if ipforwarding was zero but some routing protocol was advancing
1237df8bae1dSRodney W. Grimes  * us as a gateway to somewhere.  However, we must let the routing
1238df8bae1dSRodney W. Grimes  * protocol deal with that.
1239df8bae1dSRodney W. Grimes  *
1240df8bae1dSRodney W. Grimes  * The srcrt parameter indicates whether the packet is being forwarded
1241df8bae1dSRodney W. Grimes  * via a source route.
1242df8bae1dSRodney W. Grimes  */
12439b932e9eSAndre Oppermann void
12449b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt)
1245df8bae1dSRodney W. Grimes {
12462b25acc1SLuigi Rizzo 	struct ip *ip = mtod(m, struct ip *);
12479b932e9eSAndre Oppermann 	struct in_ifaddr *ia = NULL;
1248df8bae1dSRodney W. Grimes 	struct mbuf *mcopy;
12499b932e9eSAndre Oppermann 	struct in_addr dest;
1250c773494eSAndre Oppermann 	int error, type = 0, code = 0, mtu = 0;
12513efc3014SJulian Elischer 
12529b932e9eSAndre Oppermann 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1253df8bae1dSRodney W. Grimes 		ipstat.ips_cantforward++;
1254df8bae1dSRodney W. Grimes 		m_freem(m);
1255df8bae1dSRodney W. Grimes 		return;
1256df8bae1dSRodney W. Grimes 	}
12571b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
12581b968362SDag-Erling Smørgrav 	if (!ipstealth) {
12591b968362SDag-Erling Smørgrav #endif
1260df8bae1dSRodney W. Grimes 		if (ip->ip_ttl <= IPTTLDEC) {
12611b968362SDag-Erling Smørgrav 			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
126202c1c707SAndre Oppermann 			    0, 0);
1263df8bae1dSRodney W. Grimes 			return;
1264df8bae1dSRodney W. Grimes 		}
12651b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
12661b968362SDag-Erling Smørgrav 	}
12671b968362SDag-Erling Smørgrav #endif
1268df8bae1dSRodney W. Grimes 
12699b932e9eSAndre Oppermann 	if (!srcrt && (ia = ip_rtaddr(ip->ip_dst)) == NULL) {
127002c1c707SAndre Oppermann 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1271df8bae1dSRodney W. Grimes 		return;
127202c1c707SAndre Oppermann 	}
1273df8bae1dSRodney W. Grimes 
1274df8bae1dSRodney W. Grimes 	/*
1275bfef7ed4SIan Dowse 	 * Save the IP header and at most 8 bytes of the payload,
1276bfef7ed4SIan Dowse 	 * in case we need to generate an ICMP message to the src.
1277bfef7ed4SIan Dowse 	 *
12784d2e3692SLuigi Rizzo 	 * XXX this can be optimized a lot by saving the data in a local
12794d2e3692SLuigi Rizzo 	 * buffer on the stack (72 bytes at most), and only allocating the
12804d2e3692SLuigi Rizzo 	 * mbuf if really necessary. The vast majority of the packets
12814d2e3692SLuigi Rizzo 	 * are forwarded without having to send an ICMP back (either
12824d2e3692SLuigi Rizzo 	 * because unnecessary, or because rate limited), so we are
12834d2e3692SLuigi Rizzo 	 * really we are wasting a lot of work here.
12844d2e3692SLuigi Rizzo 	 *
1285bfef7ed4SIan Dowse 	 * We don't use m_copy() because it might return a reference
1286bfef7ed4SIan Dowse 	 * to a shared cluster. Both this function and ip_output()
1287bfef7ed4SIan Dowse 	 * assume exclusive access to the IP header in `m', so any
1288bfef7ed4SIan Dowse 	 * data in a cluster may change before we reach icmp_error().
1289df8bae1dSRodney W. Grimes 	 */
1290780b2f69SAndre Oppermann 	MGETHDR(mcopy, M_DONTWAIT, m->m_type);
1291a163d034SWarner Losh 	if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) {
12929967cafcSSam Leffler 		/*
12939967cafcSSam Leffler 		 * It's probably ok if the pkthdr dup fails (because
12949967cafcSSam Leffler 		 * the deep copy of the tag chain failed), but for now
12959967cafcSSam Leffler 		 * be conservative and just discard the copy since
12969967cafcSSam Leffler 		 * code below may some day want the tags.
12979967cafcSSam Leffler 		 */
12989967cafcSSam Leffler 		m_free(mcopy);
12999967cafcSSam Leffler 		mcopy = NULL;
13009967cafcSSam Leffler 	}
1301bfef7ed4SIan Dowse 	if (mcopy != NULL) {
1302780b2f69SAndre Oppermann 		mcopy->m_len = min(ip->ip_len, M_TRAILINGSPACE(mcopy));
1303e6b0a570SBruce M Simpson 		mcopy->m_pkthdr.len = mcopy->m_len;
1304bfef7ed4SIan Dowse 		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1305bfef7ed4SIan Dowse 	}
130604287599SRuslan Ermilov 
130704287599SRuslan Ermilov #ifdef IPSTEALTH
130804287599SRuslan Ermilov 	if (!ipstealth) {
130904287599SRuslan Ermilov #endif
131004287599SRuslan Ermilov 		ip->ip_ttl -= IPTTLDEC;
131104287599SRuslan Ermilov #ifdef IPSTEALTH
131204287599SRuslan Ermilov 	}
131304287599SRuslan Ermilov #endif
1314df8bae1dSRodney W. Grimes 
1315df8bae1dSRodney W. Grimes 	/*
1316df8bae1dSRodney W. Grimes 	 * If forwarding packet using same interface that it came in on,
1317df8bae1dSRodney W. Grimes 	 * perhaps should send a redirect to sender to shortcut a hop.
1318df8bae1dSRodney W. Grimes 	 * Only send redirect if source is sending directly to us,
1319df8bae1dSRodney W. Grimes 	 * and if packet was not source routed (or has any options).
1320df8bae1dSRodney W. Grimes 	 * Also, don't send redirect if forwarding using a default route
1321df8bae1dSRodney W. Grimes 	 * or a route modified by a redirect.
1322df8bae1dSRodney W. Grimes 	 */
13239b932e9eSAndre Oppermann 	dest.s_addr = 0;
13249b932e9eSAndre Oppermann 	if (!srcrt && ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) {
132502c1c707SAndre Oppermann 		struct sockaddr_in *sin;
132602c1c707SAndre Oppermann 		struct route ro;
132702c1c707SAndre Oppermann 		struct rtentry *rt;
132802c1c707SAndre Oppermann 
13290cfbbe3bSAndre Oppermann 		bzero(&ro, sizeof(ro));
133002c1c707SAndre Oppermann 		sin = (struct sockaddr_in *)&ro.ro_dst;
133102c1c707SAndre Oppermann 		sin->sin_family = AF_INET;
133202c1c707SAndre Oppermann 		sin->sin_len = sizeof(*sin);
13339b932e9eSAndre Oppermann 		sin->sin_addr = ip->ip_dst;
133426d02ca7SAndre Oppermann 		rtalloc_ign(&ro, RTF_CLONING);
133502c1c707SAndre Oppermann 
133602c1c707SAndre Oppermann 		rt = ro.ro_rt;
133702c1c707SAndre Oppermann 
133802c1c707SAndre Oppermann 		if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
13399b932e9eSAndre Oppermann 		    satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1340df8bae1dSRodney W. Grimes #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1341df8bae1dSRodney W. Grimes 			u_long src = ntohl(ip->ip_src.s_addr);
1342df8bae1dSRodney W. Grimes 
1343df8bae1dSRodney W. Grimes 			if (RTA(rt) &&
1344df8bae1dSRodney W. Grimes 			    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1345df8bae1dSRodney W. Grimes 				if (rt->rt_flags & RTF_GATEWAY)
13469b932e9eSAndre Oppermann 					dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1347df8bae1dSRodney W. Grimes 				else
13489b932e9eSAndre Oppermann 					dest.s_addr = ip->ip_dst.s_addr;
1349df8bae1dSRodney W. Grimes 				/* Router requirements says to only send host redirects */
1350df8bae1dSRodney W. Grimes 				type = ICMP_REDIRECT;
1351df8bae1dSRodney W. Grimes 				code = ICMP_REDIRECT_HOST;
1352df8bae1dSRodney W. Grimes 			}
1353df8bae1dSRodney W. Grimes 		}
135402c1c707SAndre Oppermann 		if (rt)
135502c1c707SAndre Oppermann 			RTFREE(rt);
135602c1c707SAndre Oppermann 	}
1357df8bae1dSRodney W. Grimes 
135802410549SRobert Watson 	error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1359df8bae1dSRodney W. Grimes 	if (error)
1360df8bae1dSRodney W. Grimes 		ipstat.ips_cantforward++;
1361df8bae1dSRodney W. Grimes 	else {
1362df8bae1dSRodney W. Grimes 		ipstat.ips_forward++;
1363df8bae1dSRodney W. Grimes 		if (type)
1364df8bae1dSRodney W. Grimes 			ipstat.ips_redirectsent++;
1365df8bae1dSRodney W. Grimes 		else {
13669188b4a1SAndre Oppermann 			if (mcopy)
1367df8bae1dSRodney W. Grimes 				m_freem(mcopy);
1368df8bae1dSRodney W. Grimes 			return;
1369df8bae1dSRodney W. Grimes 		}
1370df8bae1dSRodney W. Grimes 	}
1371df8bae1dSRodney W. Grimes 	if (mcopy == NULL)
1372df8bae1dSRodney W. Grimes 		return;
1373df8bae1dSRodney W. Grimes 
1374df8bae1dSRodney W. Grimes 	switch (error) {
1375df8bae1dSRodney W. Grimes 
1376df8bae1dSRodney W. Grimes 	case 0:				/* forwarded, but need redirect */
1377df8bae1dSRodney W. Grimes 		/* type, code set above */
1378df8bae1dSRodney W. Grimes 		break;
1379df8bae1dSRodney W. Grimes 
1380df8bae1dSRodney W. Grimes 	case ENETUNREACH:		/* shouldn't happen, checked above */
1381df8bae1dSRodney W. Grimes 	case EHOSTUNREACH:
1382df8bae1dSRodney W. Grimes 	case ENETDOWN:
1383df8bae1dSRodney W. Grimes 	case EHOSTDOWN:
1384df8bae1dSRodney W. Grimes 	default:
1385df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1386df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_HOST;
1387df8bae1dSRodney W. Grimes 		break;
1388df8bae1dSRodney W. Grimes 
1389df8bae1dSRodney W. Grimes 	case EMSGSIZE:
1390df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1391df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_NEEDFRAG;
13921dfcf0d2SAndre Oppermann 
139302c1c707SAndre Oppermann #if defined(IPSEC) || defined(FAST_IPSEC)
13941dfcf0d2SAndre Oppermann 		mtu = ip_ipsec_mtu(m);
13951dfcf0d2SAndre Oppermann #endif /* IPSEC */
13969b932e9eSAndre Oppermann 		/*
1397ab48768bSAndre Oppermann 		 * If the MTU wasn't set before use the interface mtu or
1398ab48768bSAndre Oppermann 		 * fall back to the next smaller mtu step compared to the
1399ab48768bSAndre Oppermann 		 * current packet size.
14009b932e9eSAndre Oppermann 		 */
1401ab48768bSAndre Oppermann 		if (mtu == 0) {
1402ab48768bSAndre Oppermann 			if (ia != NULL)
1403c773494eSAndre Oppermann 				mtu = ia->ia_ifp->if_mtu;
1404ab48768bSAndre Oppermann 			else
1405ab48768bSAndre Oppermann 				mtu = ip_next_mtu(ip->ip_len, 0);
1406ab48768bSAndre Oppermann 		}
1407df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
1408df8bae1dSRodney W. Grimes 		break;
1409df8bae1dSRodney W. Grimes 
1410df8bae1dSRodney W. Grimes 	case ENOBUFS:
1411df285b3dSMike Silbersack 		/*
1412df285b3dSMike Silbersack 		 * A router should not generate ICMP_SOURCEQUENCH as
1413df285b3dSMike Silbersack 		 * required in RFC1812 Requirements for IP Version 4 Routers.
1414df285b3dSMike Silbersack 		 * Source quench could be a big problem under DoS attacks,
1415df285b3dSMike Silbersack 		 * or if the underlying interface is rate-limited.
1416df285b3dSMike Silbersack 		 * Those who need source quench packets may re-enable them
1417df285b3dSMike Silbersack 		 * via the net.inet.ip.sendsourcequench sysctl.
1418df285b3dSMike Silbersack 		 */
1419df285b3dSMike Silbersack 		if (ip_sendsourcequench == 0) {
1420df285b3dSMike Silbersack 			m_freem(mcopy);
1421df285b3dSMike Silbersack 			return;
1422df285b3dSMike Silbersack 		} else {
1423df8bae1dSRodney W. Grimes 			type = ICMP_SOURCEQUENCH;
1424df8bae1dSRodney W. Grimes 			code = 0;
1425df285b3dSMike Silbersack 		}
1426df8bae1dSRodney W. Grimes 		break;
14273a06e3e0SRuslan Ermilov 
14283a06e3e0SRuslan Ermilov 	case EACCES:			/* ipfw denied packet */
14293a06e3e0SRuslan Ermilov 		m_freem(mcopy);
14303a06e3e0SRuslan Ermilov 		return;
1431df8bae1dSRodney W. Grimes 	}
1432c773494eSAndre Oppermann 	icmp_error(mcopy, type, code, dest.s_addr, mtu);
1433df8bae1dSRodney W. Grimes }
1434df8bae1dSRodney W. Grimes 
143582c23ebaSBill Fenner void
1436f2565d68SRobert Watson ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1437f2565d68SRobert Watson     struct mbuf *m)
143882c23ebaSBill Fenner {
1439be8a62e8SPoul-Henning Kamp 	if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1440be8a62e8SPoul-Henning Kamp 		struct bintime bt;
1441be8a62e8SPoul-Henning Kamp 
1442be8a62e8SPoul-Henning Kamp 		bintime(&bt);
1443be8a62e8SPoul-Henning Kamp 		if (inp->inp_socket->so_options & SO_BINTIME) {
1444be8a62e8SPoul-Henning Kamp 			*mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt),
1445be8a62e8SPoul-Henning Kamp 			SCM_BINTIME, SOL_SOCKET);
1446be8a62e8SPoul-Henning Kamp 			if (*mp)
1447be8a62e8SPoul-Henning Kamp 				mp = &(*mp)->m_next;
1448be8a62e8SPoul-Henning Kamp 		}
144982c23ebaSBill Fenner 		if (inp->inp_socket->so_options & SO_TIMESTAMP) {
145082c23ebaSBill Fenner 			struct timeval tv;
145182c23ebaSBill Fenner 
1452be8a62e8SPoul-Henning Kamp 			bintime2timeval(&bt, &tv);
145382c23ebaSBill Fenner 			*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
145482c23ebaSBill Fenner 				SCM_TIMESTAMP, SOL_SOCKET);
145582c23ebaSBill Fenner 			if (*mp)
145682c23ebaSBill Fenner 				mp = &(*mp)->m_next;
14574cc20ab1SSeigo Tanimura 		}
1458be8a62e8SPoul-Henning Kamp 	}
145982c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVDSTADDR) {
146082c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
146182c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
146282c23ebaSBill Fenner 		if (*mp)
146382c23ebaSBill Fenner 			mp = &(*mp)->m_next;
146482c23ebaSBill Fenner 	}
14654957466bSMatthew N. Dodd 	if (inp->inp_flags & INP_RECVTTL) {
14664957466bSMatthew N. Dodd 		*mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
14674957466bSMatthew N. Dodd 		    sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
14684957466bSMatthew N. Dodd 		if (*mp)
14694957466bSMatthew N. Dodd 			mp = &(*mp)->m_next;
14704957466bSMatthew N. Dodd 	}
147182c23ebaSBill Fenner #ifdef notyet
147282c23ebaSBill Fenner 	/* XXX
147382c23ebaSBill Fenner 	 * Moving these out of udp_input() made them even more broken
147482c23ebaSBill Fenner 	 * than they already were.
147582c23ebaSBill Fenner 	 */
147682c23ebaSBill Fenner 	/* options were tossed already */
147782c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVOPTS) {
147882c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
147982c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
148082c23ebaSBill Fenner 		if (*mp)
148182c23ebaSBill Fenner 			mp = &(*mp)->m_next;
148282c23ebaSBill Fenner 	}
148382c23ebaSBill Fenner 	/* ip_srcroute doesn't do what we want here, need to fix */
148482c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVRETOPTS) {
1485e0982661SAndre Oppermann 		*mp = sbcreatecontrol((caddr_t) ip_srcroute(m),
148682c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
148782c23ebaSBill Fenner 		if (*mp)
148882c23ebaSBill Fenner 			mp = &(*mp)->m_next;
148982c23ebaSBill Fenner 	}
149082c23ebaSBill Fenner #endif
149182c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVIF) {
1492d314ad7bSJulian Elischer 		struct ifnet *ifp;
1493d314ad7bSJulian Elischer 		struct sdlbuf {
149482c23ebaSBill Fenner 			struct sockaddr_dl sdl;
1495d314ad7bSJulian Elischer 			u_char	pad[32];
1496d314ad7bSJulian Elischer 		} sdlbuf;
1497d314ad7bSJulian Elischer 		struct sockaddr_dl *sdp;
1498d314ad7bSJulian Elischer 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
149982c23ebaSBill Fenner 
1500d314ad7bSJulian Elischer 		if (((ifp = m->m_pkthdr.rcvif))
1501d314ad7bSJulian Elischer 		&& ( ifp->if_index && (ifp->if_index <= if_index))) {
15024a0d6638SRuslan Ermilov 			sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1503d314ad7bSJulian Elischer 			/*
1504d314ad7bSJulian Elischer 			 * Change our mind and don't try copy.
1505d314ad7bSJulian Elischer 			 */
1506d314ad7bSJulian Elischer 			if ((sdp->sdl_family != AF_LINK)
1507d314ad7bSJulian Elischer 			|| (sdp->sdl_len > sizeof(sdlbuf))) {
1508d314ad7bSJulian Elischer 				goto makedummy;
1509d314ad7bSJulian Elischer 			}
1510d314ad7bSJulian Elischer 			bcopy(sdp, sdl2, sdp->sdl_len);
1511d314ad7bSJulian Elischer 		} else {
1512d314ad7bSJulian Elischer makedummy:
1513d314ad7bSJulian Elischer 			sdl2->sdl_len
1514d314ad7bSJulian Elischer 				= offsetof(struct sockaddr_dl, sdl_data[0]);
1515d314ad7bSJulian Elischer 			sdl2->sdl_family = AF_LINK;
1516d314ad7bSJulian Elischer 			sdl2->sdl_index = 0;
1517d314ad7bSJulian Elischer 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1518d314ad7bSJulian Elischer 		}
1519d314ad7bSJulian Elischer 		*mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
152082c23ebaSBill Fenner 			IP_RECVIF, IPPROTO_IP);
152182c23ebaSBill Fenner 		if (*mp)
152282c23ebaSBill Fenner 			mp = &(*mp)->m_next;
152382c23ebaSBill Fenner 	}
152482c23ebaSBill Fenner }
152582c23ebaSBill Fenner 
15264d2e3692SLuigi Rizzo /*
152730916a2dSRobert Watson  * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
152830916a2dSRobert Watson  * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
152930916a2dSRobert Watson  * locking.  This code remains in ip_input.c as ip_mroute.c is optionally
153030916a2dSRobert Watson  * compiled.
15314d2e3692SLuigi Rizzo  */
15324d2e3692SLuigi Rizzo static int ip_rsvp_on;
15334d2e3692SLuigi Rizzo struct socket *ip_rsvpd;
1534df8bae1dSRodney W. Grimes int
1535f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so)
1536f0068c4aSGarrett Wollman {
1537f0068c4aSGarrett Wollman 	if (so->so_type != SOCK_RAW ||
1538f0068c4aSGarrett Wollman 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1539f0068c4aSGarrett Wollman 		return EOPNOTSUPP;
1540f0068c4aSGarrett Wollman 
1541f0068c4aSGarrett Wollman 	if (ip_rsvpd != NULL)
1542f0068c4aSGarrett Wollman 		return EADDRINUSE;
1543f0068c4aSGarrett Wollman 
1544f0068c4aSGarrett Wollman 	ip_rsvpd = so;
15451c5de19aSGarrett Wollman 	/*
15461c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-increment
15471c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
15481c5de19aSGarrett Wollman 	 */
15491c5de19aSGarrett Wollman 	if (!ip_rsvp_on) {
15501c5de19aSGarrett Wollman 		ip_rsvp_on = 1;
15511c5de19aSGarrett Wollman 		rsvp_on++;
15521c5de19aSGarrett Wollman 	}
1553f0068c4aSGarrett Wollman 
1554f0068c4aSGarrett Wollman 	return 0;
1555f0068c4aSGarrett Wollman }
1556f0068c4aSGarrett Wollman 
1557f0068c4aSGarrett Wollman int
1558f0068c4aSGarrett Wollman ip_rsvp_done(void)
1559f0068c4aSGarrett Wollman {
1560f0068c4aSGarrett Wollman 	ip_rsvpd = NULL;
15611c5de19aSGarrett Wollman 	/*
15621c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-decrement
15631c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
15641c5de19aSGarrett Wollman 	 */
15651c5de19aSGarrett Wollman 	if (ip_rsvp_on) {
15661c5de19aSGarrett Wollman 		ip_rsvp_on = 0;
15671c5de19aSGarrett Wollman 		rsvp_on--;
15681c5de19aSGarrett Wollman 	}
1569f0068c4aSGarrett Wollman 	return 0;
1570f0068c4aSGarrett Wollman }
1571bbb4330bSLuigi Rizzo 
1572bbb4330bSLuigi Rizzo void
1573bbb4330bSLuigi Rizzo rsvp_input(struct mbuf *m, int off)	/* XXX must fixup manually */
1574bbb4330bSLuigi Rizzo {
1575bbb4330bSLuigi Rizzo 	if (rsvp_input_p) { /* call the real one if loaded */
1576bbb4330bSLuigi Rizzo 		rsvp_input_p(m, off);
1577bbb4330bSLuigi Rizzo 		return;
1578bbb4330bSLuigi Rizzo 	}
1579bbb4330bSLuigi Rizzo 
1580bbb4330bSLuigi Rizzo 	/* Can still get packets with rsvp_on = 0 if there is a local member
1581bbb4330bSLuigi Rizzo 	 * of the group to which the RSVP packet is addressed.  But in this
1582bbb4330bSLuigi Rizzo 	 * case we want to throw the packet away.
1583bbb4330bSLuigi Rizzo 	 */
1584bbb4330bSLuigi Rizzo 
1585bbb4330bSLuigi Rizzo 	if (!rsvp_on) {
1586bbb4330bSLuigi Rizzo 		m_freem(m);
1587bbb4330bSLuigi Rizzo 		return;
1588bbb4330bSLuigi Rizzo 	}
1589bbb4330bSLuigi Rizzo 
1590bbb4330bSLuigi Rizzo 	if (ip_rsvpd != NULL) {
1591bbb4330bSLuigi Rizzo 		rip_input(m, off);
1592bbb4330bSLuigi Rizzo 		return;
1593bbb4330bSLuigi Rizzo 	}
1594bbb4330bSLuigi Rizzo 	/* Drop the packet */
1595bbb4330bSLuigi Rizzo 	m_freem(m);
1596bbb4330bSLuigi Rizzo }
1597