xref: /freebsd/sys/netinet/ip_input.c (revision 30916a2d1dd0b610ffd51b25322f892fac8e4f2d)
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
212df8bae1dSRodney W. Grimes ip_init()
213df8bae1dSRodney W. Grimes {
214f0ffb944SJulian Elischer 	register struct protosw *pr;
215df8bae1dSRodney W. Grimes 	register 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 
2715f311da2SMike Silbersack void ip_fini(xtp)
2725f311da2SMike Silbersack 	void *xtp;
2735f311da2SMike Silbersack {
2745f311da2SMike Silbersack 	callout_stop(&ipport_tick_callout);
2755f311da2SMike Silbersack }
2765f311da2SMike Silbersack 
2774d2e3692SLuigi Rizzo /*
278df8bae1dSRodney W. Grimes  * Ip input routine.  Checksum and byte swap header.  If fragmented
279df8bae1dSRodney W. Grimes  * try to reassemble.  Process options.  Pass to next level.
280df8bae1dSRodney W. Grimes  */
281c67b1d17SGarrett Wollman void
282c67b1d17SGarrett Wollman ip_input(struct mbuf *m)
283df8bae1dSRodney W. Grimes {
2849188b4a1SAndre Oppermann 	struct ip *ip = NULL;
2855da9f8faSJosef Karthauser 	struct in_ifaddr *ia = NULL;
286ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
2879b932e9eSAndre Oppermann 	int    checkif, hlen = 0;
28847c861ecSBrian Somers 	u_short sum;
28902c1c707SAndre Oppermann 	int dchg = 0;				/* dest changed after fw */
290f51f805fSSam Leffler 	struct in_addr odst;			/* original dst address */
291b715f178SLuigi Rizzo 
292fe584538SDag-Erling Smørgrav   	M_ASSERTPKTHDR(m);
293db40007dSAndrew R. Reiter 
294ac9d7e26SMax Laier 	if (m->m_flags & M_FASTFWD_OURS) {
2959b932e9eSAndre Oppermann 		/*
29676ff6dcfSAndre Oppermann 		 * Firewall or NAT changed destination to local.
29776ff6dcfSAndre Oppermann 		 * We expect ip_len and ip_off to be in host byte order.
2989b932e9eSAndre Oppermann 		 */
29976ff6dcfSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
30076ff6dcfSAndre Oppermann 		/* Set up some basics that will be used later. */
3012b25acc1SLuigi Rizzo 		ip = mtod(m, struct ip *);
30253be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
3039b932e9eSAndre Oppermann   		goto ours;
3042b25acc1SLuigi Rizzo   	}
3052b25acc1SLuigi Rizzo 
306df8bae1dSRodney W. Grimes 	ipstat.ips_total++;
30758938916SGarrett Wollman 
30858938916SGarrett Wollman 	if (m->m_pkthdr.len < sizeof(struct ip))
30958938916SGarrett Wollman 		goto tooshort;
31058938916SGarrett Wollman 
311df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct ip) &&
3120b17fba7SAndre Oppermann 	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
313df8bae1dSRodney W. Grimes 		ipstat.ips_toosmall++;
314c67b1d17SGarrett Wollman 		return;
315df8bae1dSRodney W. Grimes 	}
316df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
31758938916SGarrett Wollman 
31853be11f6SPoul-Henning Kamp 	if (ip->ip_v != IPVERSION) {
319df8bae1dSRodney W. Grimes 		ipstat.ips_badvers++;
320df8bae1dSRodney W. Grimes 		goto bad;
321df8bae1dSRodney W. Grimes 	}
32258938916SGarrett Wollman 
32353be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
324df8bae1dSRodney W. Grimes 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
325df8bae1dSRodney W. Grimes 		ipstat.ips_badhlen++;
326df8bae1dSRodney W. Grimes 		goto bad;
327df8bae1dSRodney W. Grimes 	}
328df8bae1dSRodney W. Grimes 	if (hlen > m->m_len) {
3290b17fba7SAndre Oppermann 		if ((m = m_pullup(m, hlen)) == NULL) {
330df8bae1dSRodney W. Grimes 			ipstat.ips_badhlen++;
331c67b1d17SGarrett Wollman 			return;
332df8bae1dSRodney W. Grimes 		}
333df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
334df8bae1dSRodney W. Grimes 	}
33533841545SHajimu UMEMOTO 
33633841545SHajimu UMEMOTO 	/* 127/8 must not appear on wire - RFC1122 */
33733841545SHajimu UMEMOTO 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
33833841545SHajimu UMEMOTO 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
33933841545SHajimu UMEMOTO 		if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
34033841545SHajimu UMEMOTO 			ipstat.ips_badaddr++;
34133841545SHajimu UMEMOTO 			goto bad;
34233841545SHajimu UMEMOTO 		}
34333841545SHajimu UMEMOTO 	}
34433841545SHajimu UMEMOTO 
345db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
346db4f9cc7SJonathan Lemon 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
347db4f9cc7SJonathan Lemon 	} else {
34858938916SGarrett Wollman 		if (hlen == sizeof(struct ip)) {
34947c861ecSBrian Somers 			sum = in_cksum_hdr(ip);
35058938916SGarrett Wollman 		} else {
35147c861ecSBrian Somers 			sum = in_cksum(m, hlen);
35258938916SGarrett Wollman 		}
353db4f9cc7SJonathan Lemon 	}
35447c861ecSBrian Somers 	if (sum) {
355df8bae1dSRodney W. Grimes 		ipstat.ips_badsum++;
356df8bae1dSRodney W. Grimes 		goto bad;
357df8bae1dSRodney W. Grimes 	}
358df8bae1dSRodney W. Grimes 
35902b199f1SMax Laier #ifdef ALTQ
36002b199f1SMax Laier 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
36102b199f1SMax Laier 		/* packet is dropped by traffic conditioner */
36202b199f1SMax Laier 		return;
36302b199f1SMax Laier #endif
36402b199f1SMax Laier 
365df8bae1dSRodney W. Grimes 	/*
366df8bae1dSRodney W. Grimes 	 * Convert fields to host representation.
367df8bae1dSRodney W. Grimes 	 */
368fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
369df8bae1dSRodney W. Grimes 	if (ip->ip_len < hlen) {
370df8bae1dSRodney W. Grimes 		ipstat.ips_badlen++;
371df8bae1dSRodney W. Grimes 		goto bad;
372df8bae1dSRodney W. Grimes 	}
373fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
374df8bae1dSRodney W. Grimes 
375df8bae1dSRodney W. Grimes 	/*
376df8bae1dSRodney W. Grimes 	 * Check that the amount of data in the buffers
377df8bae1dSRodney W. Grimes 	 * is as at least much as the IP header would have us expect.
378df8bae1dSRodney W. Grimes 	 * Trim mbufs if longer than we expect.
379df8bae1dSRodney W. Grimes 	 * Drop packet if shorter than we expect.
380df8bae1dSRodney W. Grimes 	 */
381df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len < ip->ip_len) {
38258938916SGarrett Wollman tooshort:
383df8bae1dSRodney W. Grimes 		ipstat.ips_tooshort++;
384df8bae1dSRodney W. Grimes 		goto bad;
385df8bae1dSRodney W. Grimes 	}
386df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len > ip->ip_len) {
387df8bae1dSRodney W. Grimes 		if (m->m_len == m->m_pkthdr.len) {
388df8bae1dSRodney W. Grimes 			m->m_len = ip->ip_len;
389df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = ip->ip_len;
390df8bae1dSRodney W. Grimes 		} else
391df8bae1dSRodney W. Grimes 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
392df8bae1dSRodney W. Grimes 	}
3931dfcf0d2SAndre Oppermann #if defined(IPSEC) || defined(FAST_IPSEC)
39414dd6717SSam Leffler 	/*
39514dd6717SSam Leffler 	 * Bypass packet filtering for packets from a tunnel (gif).
39614dd6717SSam Leffler 	 */
3971dfcf0d2SAndre Oppermann 	if (ip_ipsec_filtergif(m))
398c21fd232SAndre Oppermann 		goto passin;
3991dfcf0d2SAndre Oppermann #endif /* IPSEC */
4003f67c834SDon Lewis 
401c4ac87eaSDarren Reed 	/*
402134ea224SSam Leffler 	 * Run through list of hooks for input packets.
403f51f805fSSam Leffler 	 *
404f51f805fSSam Leffler 	 * NB: Beware of the destination address changing (e.g.
405f51f805fSSam Leffler 	 *     by NAT rewriting).  When this happens, tell
406f51f805fSSam Leffler 	 *     ip_forward to do the right thing.
407c4ac87eaSDarren Reed 	 */
408c21fd232SAndre Oppermann 
409c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
410604afec4SChristian S.J. Peron 	if (!PFIL_HOOKED(&inet_pfil_hook))
411c21fd232SAndre Oppermann 		goto passin;
412c21fd232SAndre Oppermann 
413f51f805fSSam Leffler 	odst = ip->ip_dst;
414134ea224SSam Leffler 	if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
415d6a8d588SMax Laier 	    PFIL_IN, NULL) != 0)
416beec8214SDarren Reed 		return;
417134ea224SSam Leffler 	if (m == NULL)			/* consumed by filter */
418c4ac87eaSDarren Reed 		return;
4199b932e9eSAndre Oppermann 
420c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
42102c1c707SAndre Oppermann 	dchg = (odst.s_addr != ip->ip_dst.s_addr);
4229b932e9eSAndre Oppermann 
4239b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
4249b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
4259b932e9eSAndre Oppermann 		m->m_flags &= ~M_FASTFWD_OURS;
4269b932e9eSAndre Oppermann 		goto ours;
4279b932e9eSAndre Oppermann 	}
428099dd043SAndre Oppermann 	if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) {
429099dd043SAndre Oppermann 		/*
430099dd043SAndre Oppermann 		 * Directly ship on the packet.  This allows to forward packets
431099dd043SAndre Oppermann 		 * that were destined for us to some other directly connected
432099dd043SAndre Oppermann 		 * host.
433099dd043SAndre Oppermann 		 */
434099dd043SAndre Oppermann 		ip_forward(m, dchg);
435099dd043SAndre Oppermann 		return;
436099dd043SAndre Oppermann 	}
4379b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
4389b932e9eSAndre Oppermann 
439c21fd232SAndre Oppermann passin:
440df8bae1dSRodney W. Grimes 	/*
441df8bae1dSRodney W. Grimes 	 * Process options and, if not destined for us,
442df8bae1dSRodney W. Grimes 	 * ship it on.  ip_dooptions returns 1 when an
443df8bae1dSRodney W. Grimes 	 * error was detected (causing an icmp message
444df8bae1dSRodney W. Grimes 	 * to be sent and the original packet to be freed).
445df8bae1dSRodney W. Grimes 	 */
4469b932e9eSAndre Oppermann 	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
447c67b1d17SGarrett Wollman 		return;
448df8bae1dSRodney W. Grimes 
449f0068c4aSGarrett Wollman         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
450f0068c4aSGarrett Wollman          * matter if it is destined to another node, or whether it is
451f0068c4aSGarrett Wollman          * a multicast one, RSVP wants it! and prevents it from being forwarded
452f0068c4aSGarrett Wollman          * anywhere else. Also checks if the rsvp daemon is running before
453f0068c4aSGarrett Wollman 	 * grabbing the packet.
454f0068c4aSGarrett Wollman          */
4551c5de19aSGarrett Wollman 	if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
456f0068c4aSGarrett Wollman 		goto ours;
457f0068c4aSGarrett Wollman 
458df8bae1dSRodney W. Grimes 	/*
459df8bae1dSRodney W. Grimes 	 * Check our list of addresses, to see if the packet is for us.
460cc766e04SGarrett Wollman 	 * If we don't have any addresses, assume any unicast packet
461cc766e04SGarrett Wollman 	 * we receive might be for us (and let the upper layers deal
462cc766e04SGarrett Wollman 	 * with it).
463df8bae1dSRodney W. Grimes 	 */
464cc766e04SGarrett Wollman 	if (TAILQ_EMPTY(&in_ifaddrhead) &&
465cc766e04SGarrett Wollman 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
466cc766e04SGarrett Wollman 		goto ours;
467cc766e04SGarrett Wollman 
4687538a9a0SJonathan Lemon 	/*
469823db0e9SDon Lewis 	 * Enable a consistency check between the destination address
470823db0e9SDon Lewis 	 * and the arrival interface for a unicast packet (the RFC 1122
471823db0e9SDon Lewis 	 * strong ES model) if IP forwarding is disabled and the packet
472e15ae1b2SDon Lewis 	 * is not locally generated and the packet is not subject to
473e15ae1b2SDon Lewis 	 * 'ipfw fwd'.
4743f67c834SDon Lewis 	 *
4753f67c834SDon Lewis 	 * XXX - Checking also should be disabled if the destination
4763f67c834SDon Lewis 	 * address is ipnat'ed to a different interface.
4773f67c834SDon Lewis 	 *
478a8f12100SDon Lewis 	 * XXX - Checking is incompatible with IP aliases added
4793f67c834SDon Lewis 	 * to the loopback interface instead of the interface where
4803f67c834SDon Lewis 	 * the packets are received.
481a9771948SGleb Smirnoff 	 *
482a9771948SGleb Smirnoff 	 * XXX - This is the case for carp vhost IPs as well so we
483a9771948SGleb Smirnoff 	 * insert a workaround. If the packet got here, we already
484a9771948SGleb Smirnoff 	 * checked with carp_iamatch() and carp_forus().
485823db0e9SDon Lewis 	 */
486823db0e9SDon Lewis 	checkif = ip_checkinterface && (ipforwarding == 0) &&
4879494d596SBrooks Davis 	    m->m_pkthdr.rcvif != NULL &&
488e15ae1b2SDon Lewis 	    ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
489a9771948SGleb Smirnoff #ifdef DEV_CARP
490a9771948SGleb Smirnoff 	    !m->m_pkthdr.rcvif->if_carp &&
491a9771948SGleb Smirnoff #endif
4929b932e9eSAndre Oppermann 	    (dchg == 0);
493823db0e9SDon Lewis 
494ca925d9cSJonathan Lemon 	/*
495ca925d9cSJonathan Lemon 	 * Check for exact addresses in the hash bucket.
496ca925d9cSJonathan Lemon 	 */
4979b932e9eSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
498f9e354dfSJulian Elischer 		/*
499823db0e9SDon Lewis 		 * If the address matches, verify that the packet
500823db0e9SDon Lewis 		 * arrived via the correct interface if checking is
501823db0e9SDon Lewis 		 * enabled.
502f9e354dfSJulian Elischer 		 */
5039b932e9eSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
504823db0e9SDon Lewis 		    (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
505ed1ff184SJulian Elischer 			goto ours;
506ca925d9cSJonathan Lemon 	}
507823db0e9SDon Lewis 	/*
508ca925d9cSJonathan Lemon 	 * Check for broadcast addresses.
509ca925d9cSJonathan Lemon 	 *
510ca925d9cSJonathan Lemon 	 * Only accept broadcast packets that arrive via the matching
511ca925d9cSJonathan Lemon 	 * interface.  Reception of forwarded directed broadcasts would
512ca925d9cSJonathan Lemon 	 * be handled via ip_forward() and ether_output() with the loopback
513ca925d9cSJonathan Lemon 	 * into the stack for SIMPLEX interfaces handled by ether_output().
514823db0e9SDon Lewis 	 */
5154f450ff9SBruce M Simpson 	if (m->m_pkthdr.rcvif != NULL &&
5164f450ff9SBruce M Simpson 	    m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
517ca925d9cSJonathan Lemon 	        TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
518ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
519ca925d9cSJonathan Lemon 				continue;
520ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
521df8bae1dSRodney W. Grimes 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
5229b932e9eSAndre Oppermann 			    ip->ip_dst.s_addr)
523df8bae1dSRodney W. Grimes 				goto ours;
5249b932e9eSAndre Oppermann 			if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr)
525df8bae1dSRodney W. Grimes 				goto ours;
5260ac40133SBrian Somers #ifdef BOOTP_COMPAT
527ca925d9cSJonathan Lemon 			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
528ca925d9cSJonathan Lemon 				goto ours;
5290ac40133SBrian Somers #endif
530df8bae1dSRodney W. Grimes 		}
531df8bae1dSRodney W. Grimes 	}
532f8429ca2SBruce M Simpson 	/* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
533f8429ca2SBruce M Simpson 	if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
534f8429ca2SBruce M Simpson 		ipstat.ips_cantforward++;
535f8429ca2SBruce M Simpson 		m_freem(m);
536f8429ca2SBruce M Simpson 		return;
537f8429ca2SBruce M Simpson 	}
538df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
539df8bae1dSRodney W. Grimes 		struct in_multi *inm;
540df8bae1dSRodney W. Grimes 		if (ip_mrouter) {
541df8bae1dSRodney W. Grimes 			/*
542df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, all
543df8bae1dSRodney W. Grimes 			 * incoming multicast packets are passed to the
544df8bae1dSRodney W. Grimes 			 * kernel-level multicast forwarding function.
545df8bae1dSRodney W. Grimes 			 * The packet is returned (relatively) intact; if
546df8bae1dSRodney W. Grimes 			 * ip_mforward() returns a non-zero value, the packet
547df8bae1dSRodney W. Grimes 			 * must be discarded, else it may be accepted below.
548df8bae1dSRodney W. Grimes 			 */
549bbb4330bSLuigi Rizzo 			if (ip_mforward &&
550bbb4330bSLuigi Rizzo 			    ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
551df8bae1dSRodney W. Grimes 				ipstat.ips_cantforward++;
552df8bae1dSRodney W. Grimes 				m_freem(m);
553c67b1d17SGarrett Wollman 				return;
554df8bae1dSRodney W. Grimes 			}
555df8bae1dSRodney W. Grimes 
556df8bae1dSRodney W. Grimes 			/*
55711612afaSDima Dorfman 			 * The process-level routing daemon needs to receive
558df8bae1dSRodney W. Grimes 			 * all multicast IGMP packets, whether or not this
559df8bae1dSRodney W. Grimes 			 * host belongs to their destination groups.
560df8bae1dSRodney W. Grimes 			 */
561df8bae1dSRodney W. Grimes 			if (ip->ip_p == IPPROTO_IGMP)
562df8bae1dSRodney W. Grimes 				goto ours;
563df8bae1dSRodney W. Grimes 			ipstat.ips_forward++;
564df8bae1dSRodney W. Grimes 		}
565df8bae1dSRodney W. Grimes 		/*
566df8bae1dSRodney W. Grimes 		 * See if we belong to the destination multicast group on the
567df8bae1dSRodney W. Grimes 		 * arrival interface.
568df8bae1dSRodney W. Grimes 		 */
569dd5a318bSRobert Watson 		IN_MULTI_LOCK();
570df8bae1dSRodney W. Grimes 		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
571dd5a318bSRobert Watson 		IN_MULTI_UNLOCK();
572df8bae1dSRodney W. Grimes 		if (inm == NULL) {
57382c39223SGarrett Wollman 			ipstat.ips_notmember++;
574df8bae1dSRodney W. Grimes 			m_freem(m);
575c67b1d17SGarrett Wollman 			return;
576df8bae1dSRodney W. Grimes 		}
577df8bae1dSRodney W. Grimes 		goto ours;
578df8bae1dSRodney W. Grimes 	}
579df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
580df8bae1dSRodney W. Grimes 		goto ours;
581df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == INADDR_ANY)
582df8bae1dSRodney W. Grimes 		goto ours;
583df8bae1dSRodney W. Grimes 
5846a800098SYoshinobu Inoue 	/*
5856a800098SYoshinobu Inoue 	 * FAITH(Firewall Aided Internet Translator)
5866a800098SYoshinobu Inoue 	 */
5876a800098SYoshinobu Inoue 	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
5886a800098SYoshinobu Inoue 		if (ip_keepfaith) {
5896a800098SYoshinobu Inoue 			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
5906a800098SYoshinobu Inoue 				goto ours;
5916a800098SYoshinobu Inoue 		}
5926a800098SYoshinobu Inoue 		m_freem(m);
5936a800098SYoshinobu Inoue 		return;
5946a800098SYoshinobu Inoue 	}
5959494d596SBrooks Davis 
596df8bae1dSRodney W. Grimes 	/*
597df8bae1dSRodney W. Grimes 	 * Not for us; forward if possible and desirable.
598df8bae1dSRodney W. Grimes 	 */
599df8bae1dSRodney W. Grimes 	if (ipforwarding == 0) {
600df8bae1dSRodney W. Grimes 		ipstat.ips_cantforward++;
601df8bae1dSRodney W. Grimes 		m_freem(m);
602546f251bSChris D. Faulhaber 	} else {
6031dfcf0d2SAndre Oppermann #if defined(IPSEC) || defined(FAST_IPSEC)
6041dfcf0d2SAndre Oppermann 		if (ip_ipsec_fwd(m))
605546f251bSChris D. Faulhaber 			goto bad;
606546f251bSChris D. Faulhaber #endif /* IPSEC */
6079b932e9eSAndre Oppermann 		ip_forward(m, dchg);
608546f251bSChris D. Faulhaber 	}
609c67b1d17SGarrett Wollman 	return;
610df8bae1dSRodney W. Grimes 
611df8bae1dSRodney W. Grimes ours:
612d0ebc0d2SYaroslav Tykhiy #ifdef IPSTEALTH
613d0ebc0d2SYaroslav Tykhiy 	/*
614d0ebc0d2SYaroslav Tykhiy 	 * IPSTEALTH: Process non-routing options only
615d0ebc0d2SYaroslav Tykhiy 	 * if the packet is destined for us.
616d0ebc0d2SYaroslav Tykhiy 	 */
6172b25acc1SLuigi Rizzo 	if (ipstealth && hlen > sizeof (struct ip) &&
6189b932e9eSAndre Oppermann 	    ip_dooptions(m, 1))
619d0ebc0d2SYaroslav Tykhiy 		return;
620d0ebc0d2SYaroslav Tykhiy #endif /* IPSTEALTH */
621d0ebc0d2SYaroslav Tykhiy 
6225da9f8faSJosef Karthauser 	/* Count the packet in the ip address stats */
6235da9f8faSJosef Karthauser 	if (ia != NULL) {
6245da9f8faSJosef Karthauser 		ia->ia_ifa.if_ipackets++;
6255da9f8faSJosef Karthauser 		ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
6265da9f8faSJosef Karthauser 	}
627100ba1a6SJordan K. Hubbard 
62863f8d699SJordan K. Hubbard 	/*
629b6ea1aa5SRuslan Ermilov 	 * Attempt reassembly; if it succeeds, proceed.
630ac9d7e26SMax Laier 	 * ip_reass() will return a different mbuf.
631df8bae1dSRodney W. Grimes 	 */
632f0cada84SAndre Oppermann 	if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
633f0cada84SAndre Oppermann 		m = ip_reass(m);
634f0cada84SAndre Oppermann 		if (m == NULL)
635c67b1d17SGarrett Wollman 			return;
6366a800098SYoshinobu Inoue 		ip = mtod(m, struct ip *);
6377e2df452SRuslan Ermilov 		/* Get the header length of the reassembled packet */
63853be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
639f0cada84SAndre Oppermann 	}
640f0cada84SAndre Oppermann 
641f0cada84SAndre Oppermann 	/*
642f0cada84SAndre Oppermann 	 * Further protocols expect the packet length to be w/o the
643f0cada84SAndre Oppermann 	 * IP header.
644f0cada84SAndre Oppermann 	 */
645df8bae1dSRodney W. Grimes 	ip->ip_len -= hlen;
646df8bae1dSRodney W. Grimes 
6471dfcf0d2SAndre Oppermann #if defined(IPSEC) || defined(FAST_IPSEC)
64833841545SHajimu UMEMOTO 	/*
64933841545SHajimu UMEMOTO 	 * enforce IPsec policy checking if we are seeing last header.
65033841545SHajimu UMEMOTO 	 * note that we do not visit this with protocols with pcb layer
65133841545SHajimu UMEMOTO 	 * code - like udp/tcp/raw ip.
65233841545SHajimu UMEMOTO 	 */
6531dfcf0d2SAndre Oppermann 	if (ip_ipsec_input(m))
65433841545SHajimu UMEMOTO 		goto bad;
6551dfcf0d2SAndre Oppermann #endif /* IPSEC */
65633841545SHajimu UMEMOTO 
657df8bae1dSRodney W. Grimes 	/*
658df8bae1dSRodney W. Grimes 	 * Switch out to protocol's input routine.
659df8bae1dSRodney W. Grimes 	 */
660df8bae1dSRodney W. Grimes 	ipstat.ips_delivered++;
6619b932e9eSAndre Oppermann 
6622b25acc1SLuigi Rizzo 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
663c67b1d17SGarrett Wollman 	return;
664df8bae1dSRodney W. Grimes bad:
665df8bae1dSRodney W. Grimes 	m_freem(m);
666c67b1d17SGarrett Wollman }
667c67b1d17SGarrett Wollman 
668c67b1d17SGarrett Wollman /*
669d248c7d7SRobert Watson  * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
670d248c7d7SRobert Watson  * max has slightly different semantics than the sysctl, for historical
671d248c7d7SRobert Watson  * reasons.
672d248c7d7SRobert Watson  */
673d248c7d7SRobert Watson static void
674d248c7d7SRobert Watson maxnipq_update(void)
675d248c7d7SRobert Watson {
676d248c7d7SRobert Watson 
677d248c7d7SRobert Watson 	/*
678d248c7d7SRobert Watson 	 * -1 for unlimited allocation.
679d248c7d7SRobert Watson 	 */
680d248c7d7SRobert Watson 	if (maxnipq < 0)
681d248c7d7SRobert Watson 		uma_zone_set_max(ipq_zone, 0);
682d248c7d7SRobert Watson 	/*
683d248c7d7SRobert Watson 	 * Positive number for specific bound.
684d248c7d7SRobert Watson 	 */
685d248c7d7SRobert Watson 	if (maxnipq > 0)
686d248c7d7SRobert Watson 		uma_zone_set_max(ipq_zone, maxnipq);
687d248c7d7SRobert Watson 	/*
688d248c7d7SRobert Watson 	 * Zero specifies no further fragment queue allocation -- set the
689d248c7d7SRobert Watson 	 * bound very low, but rely on implementation elsewhere to actually
690d248c7d7SRobert Watson 	 * prevent allocation and reclaim current queues.
691d248c7d7SRobert Watson 	 */
692d248c7d7SRobert Watson 	if (maxnipq == 0)
693d248c7d7SRobert Watson 		uma_zone_set_max(ipq_zone, 1);
694d248c7d7SRobert Watson }
695d248c7d7SRobert Watson 
6964f590175SPaul Saab static void
6974f590175SPaul Saab ipq_zone_change(void *tag)
6984f590175SPaul Saab {
6994f590175SPaul Saab 
7004f590175SPaul Saab 	if (maxnipq > 0 && maxnipq < (nmbclusters / 32)) {
7014f590175SPaul Saab 		maxnipq = nmbclusters / 32;
7024f590175SPaul Saab 		maxnipq_update();
7034f590175SPaul Saab 	}
7044f590175SPaul Saab }
7054f590175SPaul Saab 
706d248c7d7SRobert Watson static int
707d248c7d7SRobert Watson sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
708d248c7d7SRobert Watson {
709d248c7d7SRobert Watson 	int error, i;
710d248c7d7SRobert Watson 
711d248c7d7SRobert Watson 	i = maxnipq;
712d248c7d7SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
713d248c7d7SRobert Watson 	if (error || !req->newptr)
714d248c7d7SRobert Watson 		return (error);
715d248c7d7SRobert Watson 
716d248c7d7SRobert Watson 	/*
717d248c7d7SRobert Watson 	 * XXXRW: Might be a good idea to sanity check the argument and place
718d248c7d7SRobert Watson 	 * an extreme upper bound.
719d248c7d7SRobert Watson 	 */
720d248c7d7SRobert Watson 	if (i < -1)
721d248c7d7SRobert Watson 		return (EINVAL);
722d248c7d7SRobert Watson 	maxnipq = i;
723d248c7d7SRobert Watson 	maxnipq_update();
724d248c7d7SRobert Watson 	return (0);
725d248c7d7SRobert Watson }
726d248c7d7SRobert Watson 
727d248c7d7SRobert Watson SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
728d248c7d7SRobert Watson     NULL, 0, sysctl_maxnipq, "I",
729d248c7d7SRobert Watson     "Maximum number of IPv4 fragment reassembly queue entries");
730d248c7d7SRobert Watson 
731d248c7d7SRobert Watson /*
7328948e4baSArchie Cobbs  * Take incoming datagram fragment and try to reassemble it into
733f0cada84SAndre Oppermann  * whole datagram.  If the argument is the first fragment or one
734f0cada84SAndre Oppermann  * in between the function will return NULL and store the mbuf
735f0cada84SAndre Oppermann  * in the fragment chain.  If the argument is the last fragment
736f0cada84SAndre Oppermann  * the packet will be reassembled and the pointer to the new
737f0cada84SAndre Oppermann  * mbuf returned for further processing.  Only m_tags attached
738f0cada84SAndre Oppermann  * to the first packet/fragment are preserved.
739f0cada84SAndre Oppermann  * The IP header is *NOT* adjusted out of iplen.
740df8bae1dSRodney W. Grimes  */
7418948e4baSArchie Cobbs 
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
1057462b86feSPoul-Henning Kamp ip_freef(fhp, fp)
1058462b86feSPoul-Henning Kamp 	struct ipqhead *fhp;
1059df8bae1dSRodney W. Grimes 	struct ipq *fp;
1060df8bae1dSRodney W. Grimes {
10616effc713SDoug Rabson 	register 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
1081df8bae1dSRodney W. Grimes ip_slowtimo()
1082df8bae1dSRodney W. Grimes {
1083df8bae1dSRodney W. Grimes 	register 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
1120df8bae1dSRodney W. Grimes ip_drain()
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 *
120102c1c707SAndre Oppermann ip_rtaddr(dst)
1202df8bae1dSRodney W. Grimes 	struct in_addr dst;
1203df8bae1dSRodney W. Grimes {
120497d8d152SAndre Oppermann 	struct route sro;
120502c1c707SAndre Oppermann 	struct sockaddr_in *sin;
120602c1c707SAndre Oppermann 	struct in_ifaddr *ifa;
1207df8bae1dSRodney W. Grimes 
12080cfbbe3bSAndre Oppermann 	bzero(&sro, sizeof(sro));
120997d8d152SAndre Oppermann 	sin = (struct sockaddr_in *)&sro.ro_dst;
1210df8bae1dSRodney W. Grimes 	sin->sin_family = AF_INET;
1211df8bae1dSRodney W. Grimes 	sin->sin_len = sizeof(*sin);
1212df8bae1dSRodney W. Grimes 	sin->sin_addr = dst;
121397d8d152SAndre Oppermann 	rtalloc_ign(&sro, RTF_CLONING);
1214df8bae1dSRodney W. Grimes 
121597d8d152SAndre Oppermann 	if (sro.ro_rt == NULL)
121602410549SRobert Watson 		return (NULL);
121702c1c707SAndre Oppermann 
121897d8d152SAndre Oppermann 	ifa = ifatoia(sro.ro_rt->rt_ifa);
121997d8d152SAndre Oppermann 	RTFREE(sro.ro_rt);
122002410549SRobert Watson 	return (ifa);
1221df8bae1dSRodney W. Grimes }
1222df8bae1dSRodney W. Grimes 
1223df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = {
1224df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1225df8bae1dSRodney W. Grimes 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1226df8bae1dSRodney W. Grimes 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1227df8bae1dSRodney W. Grimes 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1228fcaf9f91SMike Silbersack 	0,		0,		EHOSTUNREACH,	0,
12293b8123b7SJesper Skriver 	ENOPROTOOPT,	ECONNREFUSED
1230df8bae1dSRodney W. Grimes };
1231df8bae1dSRodney W. Grimes 
1232df8bae1dSRodney W. Grimes /*
1233df8bae1dSRodney W. Grimes  * Forward a packet.  If some error occurs return the sender
1234df8bae1dSRodney W. Grimes  * an icmp packet.  Note we can't always generate a meaningful
1235df8bae1dSRodney W. Grimes  * icmp message because icmp doesn't have a large enough repertoire
1236df8bae1dSRodney W. Grimes  * of codes and types.
1237df8bae1dSRodney W. Grimes  *
1238df8bae1dSRodney W. Grimes  * If not forwarding, just drop the packet.  This could be confusing
1239df8bae1dSRodney W. Grimes  * if ipforwarding was zero but some routing protocol was advancing
1240df8bae1dSRodney W. Grimes  * us as a gateway to somewhere.  However, we must let the routing
1241df8bae1dSRodney W. Grimes  * protocol deal with that.
1242df8bae1dSRodney W. Grimes  *
1243df8bae1dSRodney W. Grimes  * The srcrt parameter indicates whether the packet is being forwarded
1244df8bae1dSRodney W. Grimes  * via a source route.
1245df8bae1dSRodney W. Grimes  */
12469b932e9eSAndre Oppermann void
12479b932e9eSAndre Oppermann ip_forward(struct mbuf *m, int srcrt)
1248df8bae1dSRodney W. Grimes {
12492b25acc1SLuigi Rizzo 	struct ip *ip = mtod(m, struct ip *);
12509b932e9eSAndre Oppermann 	struct in_ifaddr *ia = NULL;
1251df8bae1dSRodney W. Grimes 	struct mbuf *mcopy;
12529b932e9eSAndre Oppermann 	struct in_addr dest;
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 
12729b932e9eSAndre Oppermann 	if (!srcrt && (ia = ip_rtaddr(ip->ip_dst)) == NULL) {
127302c1c707SAndre Oppermann 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1274df8bae1dSRodney W. Grimes 		return;
127502c1c707SAndre Oppermann 	}
1276df8bae1dSRodney W. Grimes 
1277df8bae1dSRodney W. Grimes 	/*
1278bfef7ed4SIan Dowse 	 * Save the IP header and at most 8 bytes of the payload,
1279bfef7ed4SIan Dowse 	 * in case we need to generate an ICMP message to the src.
1280bfef7ed4SIan Dowse 	 *
12814d2e3692SLuigi Rizzo 	 * XXX this can be optimized a lot by saving the data in a local
12824d2e3692SLuigi Rizzo 	 * buffer on the stack (72 bytes at most), and only allocating the
12834d2e3692SLuigi Rizzo 	 * mbuf if really necessary. The vast majority of the packets
12844d2e3692SLuigi Rizzo 	 * are forwarded without having to send an ICMP back (either
12854d2e3692SLuigi Rizzo 	 * because unnecessary, or because rate limited), so we are
12864d2e3692SLuigi Rizzo 	 * really we are wasting a lot of work here.
12874d2e3692SLuigi Rizzo 	 *
1288bfef7ed4SIan Dowse 	 * We don't use m_copy() because it might return a reference
1289bfef7ed4SIan Dowse 	 * to a shared cluster. Both this function and ip_output()
1290bfef7ed4SIan Dowse 	 * assume exclusive access to the IP header in `m', so any
1291bfef7ed4SIan Dowse 	 * data in a cluster may change before we reach icmp_error().
1292df8bae1dSRodney W. Grimes 	 */
1293780b2f69SAndre Oppermann 	MGETHDR(mcopy, M_DONTWAIT, m->m_type);
1294a163d034SWarner Losh 	if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) {
12959967cafcSSam Leffler 		/*
12969967cafcSSam Leffler 		 * It's probably ok if the pkthdr dup fails (because
12979967cafcSSam Leffler 		 * the deep copy of the tag chain failed), but for now
12989967cafcSSam Leffler 		 * be conservative and just discard the copy since
12999967cafcSSam Leffler 		 * code below may some day want the tags.
13009967cafcSSam Leffler 		 */
13019967cafcSSam Leffler 		m_free(mcopy);
13029967cafcSSam Leffler 		mcopy = NULL;
13039967cafcSSam Leffler 	}
1304bfef7ed4SIan Dowse 	if (mcopy != NULL) {
1305780b2f69SAndre Oppermann 		mcopy->m_len = min(ip->ip_len, M_TRAILINGSPACE(mcopy));
1306e6b0a570SBruce M Simpson 		mcopy->m_pkthdr.len = mcopy->m_len;
1307bfef7ed4SIan Dowse 		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1308bfef7ed4SIan Dowse 	}
130904287599SRuslan Ermilov 
131004287599SRuslan Ermilov #ifdef IPSTEALTH
131104287599SRuslan Ermilov 	if (!ipstealth) {
131204287599SRuslan Ermilov #endif
131304287599SRuslan Ermilov 		ip->ip_ttl -= IPTTLDEC;
131404287599SRuslan Ermilov #ifdef IPSTEALTH
131504287599SRuslan Ermilov 	}
131604287599SRuslan Ermilov #endif
1317df8bae1dSRodney W. Grimes 
1318df8bae1dSRodney W. Grimes 	/*
1319df8bae1dSRodney W. Grimes 	 * If forwarding packet using same interface that it came in on,
1320df8bae1dSRodney W. Grimes 	 * perhaps should send a redirect to sender to shortcut a hop.
1321df8bae1dSRodney W. Grimes 	 * Only send redirect if source is sending directly to us,
1322df8bae1dSRodney W. Grimes 	 * and if packet was not source routed (or has any options).
1323df8bae1dSRodney W. Grimes 	 * Also, don't send redirect if forwarding using a default route
1324df8bae1dSRodney W. Grimes 	 * or a route modified by a redirect.
1325df8bae1dSRodney W. Grimes 	 */
13269b932e9eSAndre Oppermann 	dest.s_addr = 0;
13279b932e9eSAndre Oppermann 	if (!srcrt && ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) {
132802c1c707SAndre Oppermann 		struct sockaddr_in *sin;
132902c1c707SAndre Oppermann 		struct route ro;
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 
136102410549SRobert Watson 	error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1362df8bae1dSRodney W. Grimes 	if (error)
1363df8bae1dSRodney W. Grimes 		ipstat.ips_cantforward++;
1364df8bae1dSRodney W. Grimes 	else {
1365df8bae1dSRodney W. Grimes 		ipstat.ips_forward++;
1366df8bae1dSRodney W. Grimes 		if (type)
1367df8bae1dSRodney W. Grimes 			ipstat.ips_redirectsent++;
1368df8bae1dSRodney W. Grimes 		else {
13699188b4a1SAndre Oppermann 			if (mcopy)
1370df8bae1dSRodney W. Grimes 				m_freem(mcopy);
1371df8bae1dSRodney W. Grimes 			return;
1372df8bae1dSRodney W. Grimes 		}
1373df8bae1dSRodney W. Grimes 	}
1374df8bae1dSRodney W. Grimes 	if (mcopy == NULL)
1375df8bae1dSRodney W. Grimes 		return;
1376df8bae1dSRodney W. Grimes 
1377df8bae1dSRodney W. Grimes 	switch (error) {
1378df8bae1dSRodney W. Grimes 
1379df8bae1dSRodney W. Grimes 	case 0:				/* forwarded, but need redirect */
1380df8bae1dSRodney W. Grimes 		/* type, code set above */
1381df8bae1dSRodney W. Grimes 		break;
1382df8bae1dSRodney W. Grimes 
1383df8bae1dSRodney W. Grimes 	case ENETUNREACH:		/* shouldn't happen, checked above */
1384df8bae1dSRodney W. Grimes 	case EHOSTUNREACH:
1385df8bae1dSRodney W. Grimes 	case ENETDOWN:
1386df8bae1dSRodney W. Grimes 	case EHOSTDOWN:
1387df8bae1dSRodney W. Grimes 	default:
1388df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1389df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_HOST;
1390df8bae1dSRodney W. Grimes 		break;
1391df8bae1dSRodney W. Grimes 
1392df8bae1dSRodney W. Grimes 	case EMSGSIZE:
1393df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1394df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_NEEDFRAG;
13951dfcf0d2SAndre Oppermann 
139602c1c707SAndre Oppermann #if defined(IPSEC) || defined(FAST_IPSEC)
13971dfcf0d2SAndre Oppermann 		mtu = ip_ipsec_mtu(m);
13981dfcf0d2SAndre Oppermann #endif /* IPSEC */
13999b932e9eSAndre Oppermann 		/*
1400ab48768bSAndre Oppermann 		 * If the MTU wasn't set before use the interface mtu or
1401ab48768bSAndre Oppermann 		 * fall back to the next smaller mtu step compared to the
1402ab48768bSAndre Oppermann 		 * current packet size.
14039b932e9eSAndre Oppermann 		 */
1404ab48768bSAndre Oppermann 		if (mtu == 0) {
1405ab48768bSAndre Oppermann 			if (ia != NULL)
1406c773494eSAndre Oppermann 				mtu = ia->ia_ifp->if_mtu;
1407ab48768bSAndre Oppermann 			else
1408ab48768bSAndre Oppermann 				mtu = ip_next_mtu(ip->ip_len, 0);
1409ab48768bSAndre Oppermann 		}
1410df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
1411df8bae1dSRodney W. Grimes 		break;
1412df8bae1dSRodney W. Grimes 
1413df8bae1dSRodney W. Grimes 	case ENOBUFS:
1414df285b3dSMike Silbersack 		/*
1415df285b3dSMike Silbersack 		 * A router should not generate ICMP_SOURCEQUENCH as
1416df285b3dSMike Silbersack 		 * required in RFC1812 Requirements for IP Version 4 Routers.
1417df285b3dSMike Silbersack 		 * Source quench could be a big problem under DoS attacks,
1418df285b3dSMike Silbersack 		 * or if the underlying interface is rate-limited.
1419df285b3dSMike Silbersack 		 * Those who need source quench packets may re-enable them
1420df285b3dSMike Silbersack 		 * via the net.inet.ip.sendsourcequench sysctl.
1421df285b3dSMike Silbersack 		 */
1422df285b3dSMike Silbersack 		if (ip_sendsourcequench == 0) {
1423df285b3dSMike Silbersack 			m_freem(mcopy);
1424df285b3dSMike Silbersack 			return;
1425df285b3dSMike Silbersack 		} else {
1426df8bae1dSRodney W. Grimes 			type = ICMP_SOURCEQUENCH;
1427df8bae1dSRodney W. Grimes 			code = 0;
1428df285b3dSMike Silbersack 		}
1429df8bae1dSRodney W. Grimes 		break;
14303a06e3e0SRuslan Ermilov 
14313a06e3e0SRuslan Ermilov 	case EACCES:			/* ipfw denied packet */
14323a06e3e0SRuslan Ermilov 		m_freem(mcopy);
14333a06e3e0SRuslan Ermilov 		return;
1434df8bae1dSRodney W. Grimes 	}
1435c773494eSAndre Oppermann 	icmp_error(mcopy, type, code, dest.s_addr, mtu);
1436df8bae1dSRodney W. Grimes }
1437df8bae1dSRodney W. Grimes 
143882c23ebaSBill Fenner void
143982c23ebaSBill Fenner ip_savecontrol(inp, mp, ip, m)
144082c23ebaSBill Fenner 	register struct inpcb *inp;
144182c23ebaSBill Fenner 	register struct mbuf **mp;
144282c23ebaSBill Fenner 	register struct ip *ip;
144382c23ebaSBill Fenner 	register struct mbuf *m;
144482c23ebaSBill Fenner {
1445be8a62e8SPoul-Henning Kamp 	if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1446be8a62e8SPoul-Henning Kamp 		struct bintime bt;
1447be8a62e8SPoul-Henning Kamp 
1448be8a62e8SPoul-Henning Kamp 		bintime(&bt);
1449be8a62e8SPoul-Henning Kamp 		if (inp->inp_socket->so_options & SO_BINTIME) {
1450be8a62e8SPoul-Henning Kamp 			*mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt),
1451be8a62e8SPoul-Henning Kamp 			SCM_BINTIME, SOL_SOCKET);
1452be8a62e8SPoul-Henning Kamp 			if (*mp)
1453be8a62e8SPoul-Henning Kamp 				mp = &(*mp)->m_next;
1454be8a62e8SPoul-Henning Kamp 		}
145582c23ebaSBill Fenner 		if (inp->inp_socket->so_options & SO_TIMESTAMP) {
145682c23ebaSBill Fenner 			struct timeval tv;
145782c23ebaSBill Fenner 
1458be8a62e8SPoul-Henning Kamp 			bintime2timeval(&bt, &tv);
145982c23ebaSBill Fenner 			*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
146082c23ebaSBill Fenner 				SCM_TIMESTAMP, SOL_SOCKET);
146182c23ebaSBill Fenner 			if (*mp)
146282c23ebaSBill Fenner 				mp = &(*mp)->m_next;
14634cc20ab1SSeigo Tanimura 		}
1464be8a62e8SPoul-Henning Kamp 	}
146582c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVDSTADDR) {
146682c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
146782c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
146882c23ebaSBill Fenner 		if (*mp)
146982c23ebaSBill Fenner 			mp = &(*mp)->m_next;
147082c23ebaSBill Fenner 	}
14714957466bSMatthew N. Dodd 	if (inp->inp_flags & INP_RECVTTL) {
14724957466bSMatthew N. Dodd 		*mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
14734957466bSMatthew N. Dodd 		    sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
14744957466bSMatthew N. Dodd 		if (*mp)
14754957466bSMatthew N. Dodd 			mp = &(*mp)->m_next;
14764957466bSMatthew N. Dodd 	}
147782c23ebaSBill Fenner #ifdef notyet
147882c23ebaSBill Fenner 	/* XXX
147982c23ebaSBill Fenner 	 * Moving these out of udp_input() made them even more broken
148082c23ebaSBill Fenner 	 * than they already were.
148182c23ebaSBill Fenner 	 */
148282c23ebaSBill Fenner 	/* options were tossed already */
148382c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVOPTS) {
148482c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
148582c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
148682c23ebaSBill Fenner 		if (*mp)
148782c23ebaSBill Fenner 			mp = &(*mp)->m_next;
148882c23ebaSBill Fenner 	}
148982c23ebaSBill Fenner 	/* ip_srcroute doesn't do what we want here, need to fix */
149082c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVRETOPTS) {
1491e0982661SAndre Oppermann 		*mp = sbcreatecontrol((caddr_t) ip_srcroute(m),
149282c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
149382c23ebaSBill Fenner 		if (*mp)
149482c23ebaSBill Fenner 			mp = &(*mp)->m_next;
149582c23ebaSBill Fenner 	}
149682c23ebaSBill Fenner #endif
149782c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVIF) {
1498d314ad7bSJulian Elischer 		struct ifnet *ifp;
1499d314ad7bSJulian Elischer 		struct sdlbuf {
150082c23ebaSBill Fenner 			struct sockaddr_dl sdl;
1501d314ad7bSJulian Elischer 			u_char	pad[32];
1502d314ad7bSJulian Elischer 		} sdlbuf;
1503d314ad7bSJulian Elischer 		struct sockaddr_dl *sdp;
1504d314ad7bSJulian Elischer 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
150582c23ebaSBill Fenner 
1506d314ad7bSJulian Elischer 		if (((ifp = m->m_pkthdr.rcvif))
1507d314ad7bSJulian Elischer 		&& ( ifp->if_index && (ifp->if_index <= if_index))) {
15084a0d6638SRuslan Ermilov 			sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1509d314ad7bSJulian Elischer 			/*
1510d314ad7bSJulian Elischer 			 * Change our mind and don't try copy.
1511d314ad7bSJulian Elischer 			 */
1512d314ad7bSJulian Elischer 			if ((sdp->sdl_family != AF_LINK)
1513d314ad7bSJulian Elischer 			|| (sdp->sdl_len > sizeof(sdlbuf))) {
1514d314ad7bSJulian Elischer 				goto makedummy;
1515d314ad7bSJulian Elischer 			}
1516d314ad7bSJulian Elischer 			bcopy(sdp, sdl2, sdp->sdl_len);
1517d314ad7bSJulian Elischer 		} else {
1518d314ad7bSJulian Elischer makedummy:
1519d314ad7bSJulian Elischer 			sdl2->sdl_len
1520d314ad7bSJulian Elischer 				= offsetof(struct sockaddr_dl, sdl_data[0]);
1521d314ad7bSJulian Elischer 			sdl2->sdl_family = AF_LINK;
1522d314ad7bSJulian Elischer 			sdl2->sdl_index = 0;
1523d314ad7bSJulian Elischer 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1524d314ad7bSJulian Elischer 		}
1525d314ad7bSJulian Elischer 		*mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
152682c23ebaSBill Fenner 			IP_RECVIF, IPPROTO_IP);
152782c23ebaSBill Fenner 		if (*mp)
152882c23ebaSBill Fenner 			mp = &(*mp)->m_next;
152982c23ebaSBill Fenner 	}
153082c23ebaSBill Fenner }
153182c23ebaSBill Fenner 
15324d2e3692SLuigi Rizzo /*
153330916a2dSRobert Watson  * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
153430916a2dSRobert Watson  * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
153530916a2dSRobert Watson  * locking.  This code remains in ip_input.c as ip_mroute.c is optionally
153630916a2dSRobert Watson  * compiled.
15374d2e3692SLuigi Rizzo  */
15384d2e3692SLuigi Rizzo static int ip_rsvp_on;
15394d2e3692SLuigi Rizzo struct socket *ip_rsvpd;
1540df8bae1dSRodney W. Grimes int
1541f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so)
1542f0068c4aSGarrett Wollman {
1543f0068c4aSGarrett Wollman 	if (so->so_type != SOCK_RAW ||
1544f0068c4aSGarrett Wollman 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1545f0068c4aSGarrett Wollman 		return EOPNOTSUPP;
1546f0068c4aSGarrett Wollman 
1547f0068c4aSGarrett Wollman 	if (ip_rsvpd != NULL)
1548f0068c4aSGarrett Wollman 		return EADDRINUSE;
1549f0068c4aSGarrett Wollman 
1550f0068c4aSGarrett Wollman 	ip_rsvpd = so;
15511c5de19aSGarrett Wollman 	/*
15521c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-increment
15531c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
15541c5de19aSGarrett Wollman 	 */
15551c5de19aSGarrett Wollman 	if (!ip_rsvp_on) {
15561c5de19aSGarrett Wollman 		ip_rsvp_on = 1;
15571c5de19aSGarrett Wollman 		rsvp_on++;
15581c5de19aSGarrett Wollman 	}
1559f0068c4aSGarrett Wollman 
1560f0068c4aSGarrett Wollman 	return 0;
1561f0068c4aSGarrett Wollman }
1562f0068c4aSGarrett Wollman 
1563f0068c4aSGarrett Wollman int
1564f0068c4aSGarrett Wollman ip_rsvp_done(void)
1565f0068c4aSGarrett Wollman {
1566f0068c4aSGarrett Wollman 	ip_rsvpd = NULL;
15671c5de19aSGarrett Wollman 	/*
15681c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-decrement
15691c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
15701c5de19aSGarrett Wollman 	 */
15711c5de19aSGarrett Wollman 	if (ip_rsvp_on) {
15721c5de19aSGarrett Wollman 		ip_rsvp_on = 0;
15731c5de19aSGarrett Wollman 		rsvp_on--;
15741c5de19aSGarrett Wollman 	}
1575f0068c4aSGarrett Wollman 	return 0;
1576f0068c4aSGarrett Wollman }
1577bbb4330bSLuigi Rizzo 
1578bbb4330bSLuigi Rizzo void
1579bbb4330bSLuigi Rizzo rsvp_input(struct mbuf *m, int off)	/* XXX must fixup manually */
1580bbb4330bSLuigi Rizzo {
1581bbb4330bSLuigi Rizzo 	if (rsvp_input_p) { /* call the real one if loaded */
1582bbb4330bSLuigi Rizzo 		rsvp_input_p(m, off);
1583bbb4330bSLuigi Rizzo 		return;
1584bbb4330bSLuigi Rizzo 	}
1585bbb4330bSLuigi Rizzo 
1586bbb4330bSLuigi Rizzo 	/* Can still get packets with rsvp_on = 0 if there is a local member
1587bbb4330bSLuigi Rizzo 	 * of the group to which the RSVP packet is addressed.  But in this
1588bbb4330bSLuigi Rizzo 	 * case we want to throw the packet away.
1589bbb4330bSLuigi Rizzo 	 */
1590bbb4330bSLuigi Rizzo 
1591bbb4330bSLuigi Rizzo 	if (!rsvp_on) {
1592bbb4330bSLuigi Rizzo 		m_freem(m);
1593bbb4330bSLuigi Rizzo 		return;
1594bbb4330bSLuigi Rizzo 	}
1595bbb4330bSLuigi Rizzo 
1596bbb4330bSLuigi Rizzo 	if (ip_rsvpd != NULL) {
1597bbb4330bSLuigi Rizzo 		rip_input(m, off);
1598bbb4330bSLuigi Rizzo 		return;
1599bbb4330bSLuigi Rizzo 	}
1600bbb4330bSLuigi Rizzo 	/* Drop the packet */
1601bbb4330bSLuigi Rizzo 	m_freem(m);
1602bbb4330bSLuigi Rizzo }
1603