xref: /freebsd/sys/netinet/ip_input.c (revision 9494d5968ff60672ca84b345e32dde28113cbf36)
1df8bae1dSRodney W. Grimes /*
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  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
3758938916SGarrett Wollman #define	_IP_VHL
3858938916SGarrett Wollman 
39e4f4247aSEivind Eklund #include "opt_bootp.h"
4074a9466cSGary Palmer #include "opt_ipfw.h"
41b715f178SLuigi Rizzo #include "opt_ipdn.h"
42fbd1372aSJoerg Wunsch #include "opt_ipdivert.h"
431ee25934SPeter Wemm #include "opt_ipfilter.h"
4427108a15SDag-Erling Smørgrav #include "opt_ipstealth.h"
456a800098SYoshinobu Inoue #include "opt_ipsec.h"
46c4ac87eaSDarren Reed #include "opt_pfil_hooks.h"
4764dddc18SKris Kennaway #include "opt_random_ip_id.h"
4874a9466cSGary Palmer 
49df8bae1dSRodney W. Grimes #include <sys/param.h>
50df8bae1dSRodney W. Grimes #include <sys/systm.h>
51df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
52b715f178SLuigi Rizzo #include <sys/malloc.h>
53df8bae1dSRodney W. Grimes #include <sys/domain.h>
54df8bae1dSRodney W. Grimes #include <sys/protosw.h>
55df8bae1dSRodney W. Grimes #include <sys/socket.h>
56df8bae1dSRodney W. Grimes #include <sys/time.h>
57df8bae1dSRodney W. Grimes #include <sys/kernel.h>
581025071fSGarrett Wollman #include <sys/syslog.h>
59b5e8ce9fSBruce Evans #include <sys/sysctl.h>
60df8bae1dSRodney W. Grimes 
61c85540ddSAndrey A. Chernov #include <net/pfil.h>
62df8bae1dSRodney W. Grimes #include <net/if.h>
639494d596SBrooks Davis #include <net/if_types.h>
64d314ad7bSJulian Elischer #include <net/if_var.h>
6582c23ebaSBill Fenner #include <net/if_dl.h>
66df8bae1dSRodney W. Grimes #include <net/route.h>
67748e0b0aSGarrett Wollman #include <net/netisr.h>
68367d34f8SBrian Somers #include <net/intrq.h>
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes #include <netinet/in.h>
71df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
72b5e8ce9fSBruce Evans #include <netinet/in_var.h>
73df8bae1dSRodney W. Grimes #include <netinet/ip.h>
74df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
75df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
76df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
7758938916SGarrett Wollman #include <machine/in_cksum.h>
78df8bae1dSRodney W. Grimes 
79f0068c4aSGarrett Wollman #include <sys/socketvar.h>
806ddbf1e2SGary Palmer 
816ddbf1e2SGary Palmer #include <netinet/ip_fw.h>
826ddbf1e2SGary Palmer 
836a800098SYoshinobu Inoue #ifdef IPSEC
846a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
856a800098SYoshinobu Inoue #include <netkey/key.h>
866a800098SYoshinobu Inoue #endif
876a800098SYoshinobu Inoue 
88b715f178SLuigi Rizzo #ifdef DUMMYNET
89b715f178SLuigi Rizzo #include <netinet/ip_dummynet.h>
90b715f178SLuigi Rizzo #endif
91b715f178SLuigi Rizzo 
921c5de19aSGarrett Wollman int rsvp_on = 0;
93f708ef1bSPoul-Henning Kamp static int ip_rsvp_on;
94f0068c4aSGarrett Wollman struct socket *ip_rsvpd;
95f0068c4aSGarrett Wollman 
961f91d8c5SDavid Greenman int	ipforwarding = 0;
970312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
983d177f46SBill Fumerola     &ipforwarding, 0, "Enable IP forwarding between interfaces");
990312fbe9SPoul-Henning Kamp 
100d4fb926cSGarrett Wollman static int	ipsendredirects = 1; /* XXX */
1010312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
1023d177f46SBill Fumerola     &ipsendredirects, 0, "Enable sending IP redirects");
1030312fbe9SPoul-Henning Kamp 
104df8bae1dSRodney W. Grimes int	ip_defttl = IPDEFTTL;
1050312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
1063d177f46SBill Fumerola     &ip_defttl, 0, "Maximum TTL on IP packets");
1070312fbe9SPoul-Henning Kamp 
1080312fbe9SPoul-Henning Kamp static int	ip_dosourceroute = 0;
1090312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
1103d177f46SBill Fumerola     &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
1114fce5804SGuido van Rooij 
1124fce5804SGuido van Rooij static int	ip_acceptsourceroute = 0;
1134fce5804SGuido van Rooij SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
1143d177f46SBill Fumerola     CTLFLAG_RW, &ip_acceptsourceroute, 0,
1153d177f46SBill Fumerola     "Enable accepting source routed IP packets");
1166a800098SYoshinobu Inoue 
1176a800098SYoshinobu Inoue static int	ip_keepfaith = 0;
1186a800098SYoshinobu Inoue SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
1196a800098SYoshinobu Inoue 	&ip_keepfaith,	0,
1206a800098SYoshinobu Inoue 	"Enable packet capture for FAITH IPv4->IPv6 translater daemon");
1216a800098SYoshinobu Inoue 
122690a6055SJesper Skriver static int	ip_nfragpackets = 0;
12396c2b042SJesper Skriver static int	ip_maxfragpackets;	/* initialized in ip_init() */
124690a6055SJesper Skriver SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_RW,
125690a6055SJesper Skriver 	&ip_maxfragpackets, 0,
126690a6055SJesper Skriver 	"Maximum number of IPv4 fragment reassembly queue entries");
127690a6055SJesper Skriver 
128823db0e9SDon Lewis /*
129823db0e9SDon Lewis  * XXX - Setting ip_checkinterface mostly implements the receive side of
130823db0e9SDon Lewis  * the Strong ES model described in RFC 1122, but since the routing table
131a8f12100SDon Lewis  * and transmit implementation do not implement the Strong ES model,
132823db0e9SDon Lewis  * setting this to 1 results in an odd hybrid.
1333f67c834SDon Lewis  *
134a8f12100SDon Lewis  * XXX - ip_checkinterface currently must be disabled if you use ipnat
135a8f12100SDon Lewis  * to translate the destination address to another local interface.
1363f67c834SDon Lewis  *
1373f67c834SDon Lewis  * XXX - ip_checkinterface must be disabled if you add IP aliases
1383f67c834SDon Lewis  * to the loopback interface instead of the interface where the
1393f67c834SDon Lewis  * packets for those addresses are received.
140823db0e9SDon Lewis  */
141b3e95d4eSJonathan Lemon static int	ip_checkinterface = 1;
142b3e95d4eSJonathan Lemon SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
143b3e95d4eSJonathan Lemon     &ip_checkinterface, 0, "Verify packet arrives on correct interface");
144b3e95d4eSJonathan Lemon 
145df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1460312fbe9SPoul-Henning Kamp static int	ipprintfs = 0;
147df8bae1dSRodney W. Grimes #endif
148df8bae1dSRodney W. Grimes 
149df8bae1dSRodney W. Grimes extern	struct domain inetdomain;
150f0ffb944SJulian Elischer extern	struct protosw inetsw[];
151df8bae1dSRodney W. Grimes u_char	ip_protox[IPPROTO_MAX];
1520312fbe9SPoul-Henning Kamp static int	ipqmaxlen = IFQ_MAXLEN;
15359562606SGarrett Wollman struct	in_ifaddrhead in_ifaddrhead; /* first inet address */
154afed1375SDavid Greenman SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
1553d177f46SBill Fumerola     &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
1560312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
1573d177f46SBill Fumerola     &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue");
158df8bae1dSRodney W. Grimes 
159f23b4c91SGarrett Wollman struct ipstat ipstat;
160c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
1613d177f46SBill Fumerola     &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
162194a213eSAndrey A. Chernov 
163194a213eSAndrey A. Chernov /* Packet reassembly stuff */
164194a213eSAndrey A. Chernov #define IPREASS_NHASH_LOG2      6
165194a213eSAndrey A. Chernov #define IPREASS_NHASH           (1 << IPREASS_NHASH_LOG2)
166194a213eSAndrey A. Chernov #define IPREASS_HMASK           (IPREASS_NHASH - 1)
167194a213eSAndrey A. Chernov #define IPREASS_HASH(x,y) \
168831a80b0SMatthew Dillon 	(((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
169194a213eSAndrey A. Chernov 
170462b86feSPoul-Henning Kamp static TAILQ_HEAD(ipqhead, ipq) ipq[IPREASS_NHASH];
171194a213eSAndrey A. Chernov static int    nipq = 0;         /* total # of reass queues */
172194a213eSAndrey A. Chernov static int    maxnipq;
173367d34f8SBrian Somers const  int    ipintrq_present = 1;
174f23b4c91SGarrett Wollman 
1750312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU
1760312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
1773d177f46SBill Fumerola     &ip_mtu, 0, "Default MTU");
1780312fbe9SPoul-Henning Kamp #endif
1790312fbe9SPoul-Henning Kamp 
1801b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
1811b968362SDag-Erling Smørgrav static int	ipstealth = 0;
1821b968362SDag-Erling Smørgrav SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
1831b968362SDag-Erling Smørgrav     &ipstealth, 0, "");
1841b968362SDag-Erling Smørgrav #endif
1851b968362SDag-Erling Smørgrav 
186cfe8b629SGarrett Wollman 
18723bf9953SPoul-Henning Kamp /* Firewall hooks */
18823bf9953SPoul-Henning Kamp ip_fw_chk_t *ip_fw_chk_ptr;
18923bf9953SPoul-Henning Kamp ip_fw_ctl_t *ip_fw_ctl_ptr;
1909fcc0795SLuigi Rizzo int fw_enable = 1 ;
191e7319babSPoul-Henning Kamp 
192b715f178SLuigi Rizzo #ifdef DUMMYNET
193b715f178SLuigi Rizzo ip_dn_ctl_t *ip_dn_ctl_ptr;
194b715f178SLuigi Rizzo #endif
195b715f178SLuigi Rizzo 
196afed1b49SDarren Reed 
197e7319babSPoul-Henning Kamp /*
198df8bae1dSRodney W. Grimes  * We need to save the IP options in case a protocol wants to respond
199df8bae1dSRodney W. Grimes  * to an incoming packet over the same route if the packet got here
200df8bae1dSRodney W. Grimes  * using IP source routing.  This allows connection establishment and
201df8bae1dSRodney W. Grimes  * maintenance when the remote end is on a network that is not known
202df8bae1dSRodney W. Grimes  * to us.
203df8bae1dSRodney W. Grimes  */
2040312fbe9SPoul-Henning Kamp static int	ip_nhops = 0;
205df8bae1dSRodney W. Grimes static	struct ip_srcrt {
206df8bae1dSRodney W. Grimes 	struct	in_addr dst;			/* final destination */
207df8bae1dSRodney W. Grimes 	char	nop;				/* one NOP to align */
208df8bae1dSRodney W. Grimes 	char	srcopt[IPOPT_OFFSET + 1];	/* OPTVAL, OLEN and OFFSET */
209df8bae1dSRodney W. Grimes 	struct	in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
210df8bae1dSRodney W. Grimes } ip_srcrt;
211df8bae1dSRodney W. Grimes 
212f9e354dfSJulian Elischer struct sockaddr_in *ip_fw_fwd_addr;
213f9e354dfSJulian Elischer 
214df8bae1dSRodney W. Grimes static void	save_rte __P((u_char *, struct in_addr));
2150312fbe9SPoul-Henning Kamp static int	ip_dooptions __P((struct mbuf *));
2160312fbe9SPoul-Henning Kamp static void	ip_forward __P((struct mbuf *, int));
217462b86feSPoul-Henning Kamp static void	ip_freef __P((struct ipqhead *, struct ipq *));
2188948e4baSArchie Cobbs #ifdef IPDIVERT
219462b86feSPoul-Henning Kamp static struct	mbuf *ip_reass __P((struct mbuf *, struct ipqhead *, struct ipq *, u_int32_t *, u_int16_t *));
2208948e4baSArchie Cobbs #else
221462b86feSPoul-Henning Kamp static struct	mbuf *ip_reass __P((struct mbuf *, struct ipqhead *, struct ipq *));
2228948e4baSArchie Cobbs #endif
2238948e4baSArchie Cobbs static struct	in_ifaddr *ip_rtaddr __P((struct in_addr));
2240312fbe9SPoul-Henning Kamp static void	ipintr __P((void));
2258948e4baSArchie Cobbs 
226df8bae1dSRodney W. Grimes /*
227df8bae1dSRodney W. Grimes  * IP initialization: fill in IP protocol switch table.
228df8bae1dSRodney W. Grimes  * All protocols not implemented in kernel go to raw IP protocol handler.
229df8bae1dSRodney W. Grimes  */
230df8bae1dSRodney W. Grimes void
231df8bae1dSRodney W. Grimes ip_init()
232df8bae1dSRodney W. Grimes {
233f0ffb944SJulian Elischer 	register struct protosw *pr;
234df8bae1dSRodney W. Grimes 	register int i;
235df8bae1dSRodney W. Grimes 
23659562606SGarrett Wollman 	TAILQ_INIT(&in_ifaddrhead);
237f0ffb944SJulian Elischer 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
238df8bae1dSRodney W. Grimes 	if (pr == 0)
239df8bae1dSRodney W. Grimes 		panic("ip_init");
240df8bae1dSRodney W. Grimes 	for (i = 0; i < IPPROTO_MAX; i++)
241df8bae1dSRodney W. Grimes 		ip_protox[i] = pr - inetsw;
242f0ffb944SJulian Elischer 	for (pr = inetdomain.dom_protosw;
243f0ffb944SJulian Elischer 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
244df8bae1dSRodney W. Grimes 		if (pr->pr_domain->dom_family == PF_INET &&
245df8bae1dSRodney W. Grimes 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
246df8bae1dSRodney W. Grimes 			ip_protox[pr->pr_protocol] = pr - inetsw;
247194a213eSAndrey A. Chernov 
248194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++)
249462b86feSPoul-Henning Kamp 	    TAILQ_INIT(&ipq[i]);
250194a213eSAndrey A. Chernov 
251194a213eSAndrey A. Chernov 	maxnipq = nmbclusters / 4;
25296c2b042SJesper Skriver 	ip_maxfragpackets = nmbclusters / 4;
253194a213eSAndrey A. Chernov 
25464dddc18SKris Kennaway #ifndef RANDOM_IP_ID
255227ee8a1SPoul-Henning Kamp 	ip_id = time_second & 0xffff;
25664dddc18SKris Kennaway #endif
257df8bae1dSRodney W. Grimes 	ipintrq.ifq_maxlen = ipqmaxlen;
258df5e1987SJonathan Lemon 	mtx_init(&ipintrq.ifq_mtx, "ip_inq", MTX_DEF);
259242c5536SPeter Wemm 
260242c5536SPeter Wemm 	register_netisr(NETISR_IP, ipintr);
261df8bae1dSRodney W. Grimes }
262df8bae1dSRodney W. Grimes 
2630312fbe9SPoul-Henning Kamp static struct	sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
2641e3d5af0SRuslan Ermilov struct	route ipforward_rt;
265df8bae1dSRodney W. Grimes 
266df8bae1dSRodney W. Grimes /*
267df8bae1dSRodney W. Grimes  * Ip input routine.  Checksum and byte swap header.  If fragmented
268df8bae1dSRodney W. Grimes  * try to reassemble.  Process options.  Pass to next level.
269df8bae1dSRodney W. Grimes  */
270c67b1d17SGarrett Wollman void
271c67b1d17SGarrett Wollman ip_input(struct mbuf *m)
272df8bae1dSRodney W. Grimes {
27323bf9953SPoul-Henning Kamp 	struct ip *ip;
27423bf9953SPoul-Henning Kamp 	struct ipq *fp;
2755da9f8faSJosef Karthauser 	struct in_ifaddr *ia = NULL;
276823db0e9SDon Lewis 	int    i, hlen, checkif;
27747c861ecSBrian Somers 	u_short sum;
2788948e4baSArchie Cobbs 	u_int16_t divert_cookie;		/* firewall cookie */
2797538a9a0SJonathan Lemon 	struct in_addr pkt_dst;
2808948e4baSArchie Cobbs #ifdef IPDIVERT
2818948e4baSArchie Cobbs 	u_int32_t divert_info = 0;		/* packet divert/tee info */
282b715f178SLuigi Rizzo #endif
283b715f178SLuigi Rizzo 	struct ip_fw_chain *rule = NULL;
284c4ac87eaSDarren Reed #ifdef PFIL_HOOKS
285c4ac87eaSDarren Reed 	struct packet_filter_hook *pfh;
286c4ac87eaSDarren Reed 	struct mbuf *m0;
287c4ac87eaSDarren Reed 	int rv;
288c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */
289b715f178SLuigi Rizzo 
2908948e4baSArchie Cobbs #ifdef IPDIVERT
2918948e4baSArchie Cobbs 	/* Get and reset firewall cookie */
2928948e4baSArchie Cobbs 	divert_cookie = ip_divert_cookie;
2938948e4baSArchie Cobbs 	ip_divert_cookie = 0;
2948948e4baSArchie Cobbs #else
2958948e4baSArchie Cobbs 	divert_cookie = 0;
2968948e4baSArchie Cobbs #endif
2978948e4baSArchie Cobbs 
298b715f178SLuigi Rizzo #if defined(IPFIREWALL) && defined(DUMMYNET)
299b715f178SLuigi Rizzo         /*
300b715f178SLuigi Rizzo          * dummynet packet are prepended a vestigial mbuf with
301b715f178SLuigi Rizzo          * m_type = MT_DUMMYNET and m_data pointing to the matching
302b715f178SLuigi Rizzo          * rule.
303b715f178SLuigi Rizzo          */
304b715f178SLuigi Rizzo         if (m->m_type == MT_DUMMYNET) {
305b715f178SLuigi Rizzo             rule = (struct ip_fw_chain *)(m->m_data) ;
306b715f178SLuigi Rizzo             m = m->m_next ;
307b715f178SLuigi Rizzo             ip = mtod(m, struct ip *);
308b715f178SLuigi Rizzo             hlen = IP_VHL_HL(ip->ip_vhl) << 2;
309b715f178SLuigi Rizzo             goto iphack ;
310b715f178SLuigi Rizzo         } else
311b715f178SLuigi Rizzo             rule = NULL ;
312b715f178SLuigi Rizzo #endif
313df8bae1dSRodney W. Grimes 
314df8bae1dSRodney W. Grimes #ifdef	DIAGNOSTIC
315ed7509acSJulian Elischer 	if (m == NULL || (m->m_flags & M_PKTHDR) == 0)
31658938916SGarrett Wollman 		panic("ip_input no HDR");
317df8bae1dSRodney W. Grimes #endif
318df8bae1dSRodney W. Grimes 	ipstat.ips_total++;
31958938916SGarrett Wollman 
32058938916SGarrett Wollman 	if (m->m_pkthdr.len < sizeof(struct ip))
32158938916SGarrett Wollman 		goto tooshort;
32258938916SGarrett Wollman 
323df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct ip) &&
324df8bae1dSRodney W. Grimes 	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
325df8bae1dSRodney W. Grimes 		ipstat.ips_toosmall++;
326c67b1d17SGarrett Wollman 		return;
327df8bae1dSRodney W. Grimes 	}
328df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
32958938916SGarrett Wollman 
33058938916SGarrett Wollman 	if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
331df8bae1dSRodney W. Grimes 		ipstat.ips_badvers++;
332df8bae1dSRodney W. Grimes 		goto bad;
333df8bae1dSRodney W. Grimes 	}
33458938916SGarrett Wollman 
33558938916SGarrett Wollman 	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
336df8bae1dSRodney W. Grimes 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
337df8bae1dSRodney W. Grimes 		ipstat.ips_badhlen++;
338df8bae1dSRodney W. Grimes 		goto bad;
339df8bae1dSRodney W. Grimes 	}
340df8bae1dSRodney W. Grimes 	if (hlen > m->m_len) {
341df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, hlen)) == 0) {
342df8bae1dSRodney W. Grimes 			ipstat.ips_badhlen++;
343c67b1d17SGarrett Wollman 			return;
344df8bae1dSRodney W. Grimes 		}
345df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
346df8bae1dSRodney W. Grimes 	}
34733841545SHajimu UMEMOTO 
34833841545SHajimu UMEMOTO 	/* 127/8 must not appear on wire - RFC1122 */
34933841545SHajimu UMEMOTO 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
35033841545SHajimu UMEMOTO 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
35133841545SHajimu UMEMOTO 		if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
35233841545SHajimu UMEMOTO 			ipstat.ips_badaddr++;
35333841545SHajimu UMEMOTO 			goto bad;
35433841545SHajimu UMEMOTO 		}
35533841545SHajimu UMEMOTO 	}
35633841545SHajimu UMEMOTO 
357db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
358db4f9cc7SJonathan Lemon 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
359db4f9cc7SJonathan Lemon 	} else {
36058938916SGarrett Wollman 		if (hlen == sizeof(struct ip)) {
36147c861ecSBrian Somers 			sum = in_cksum_hdr(ip);
36258938916SGarrett Wollman 		} else {
36347c861ecSBrian Somers 			sum = in_cksum(m, hlen);
36458938916SGarrett Wollman 		}
365db4f9cc7SJonathan Lemon 	}
36647c861ecSBrian Somers 	if (sum) {
367df8bae1dSRodney W. Grimes 		ipstat.ips_badsum++;
368df8bae1dSRodney W. Grimes 		goto bad;
369df8bae1dSRodney W. Grimes 	}
370df8bae1dSRodney W. Grimes 
371df8bae1dSRodney W. Grimes 	/*
372df8bae1dSRodney W. Grimes 	 * Convert fields to host representation.
373df8bae1dSRodney W. Grimes 	 */
374df8bae1dSRodney W. Grimes 	NTOHS(ip->ip_len);
375df8bae1dSRodney W. Grimes 	if (ip->ip_len < hlen) {
376df8bae1dSRodney W. Grimes 		ipstat.ips_badlen++;
377df8bae1dSRodney W. Grimes 		goto bad;
378df8bae1dSRodney W. Grimes 	}
379df8bae1dSRodney W. Grimes 	NTOHS(ip->ip_off);
380df8bae1dSRodney W. Grimes 
381df8bae1dSRodney W. Grimes 	/*
382df8bae1dSRodney W. Grimes 	 * Check that the amount of data in the buffers
383df8bae1dSRodney W. Grimes 	 * is as at least much as the IP header would have us expect.
384df8bae1dSRodney W. Grimes 	 * Trim mbufs if longer than we expect.
385df8bae1dSRodney W. Grimes 	 * Drop packet if shorter than we expect.
386df8bae1dSRodney W. Grimes 	 */
387df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len < ip->ip_len) {
38858938916SGarrett Wollman tooshort:
389df8bae1dSRodney W. Grimes 		ipstat.ips_tooshort++;
390df8bae1dSRodney W. Grimes 		goto bad;
391df8bae1dSRodney W. Grimes 	}
392df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len > ip->ip_len) {
393df8bae1dSRodney W. Grimes 		if (m->m_len == m->m_pkthdr.len) {
394df8bae1dSRodney W. Grimes 			m->m_len = ip->ip_len;
395df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = ip->ip_len;
396df8bae1dSRodney W. Grimes 		} else
397df8bae1dSRodney W. Grimes 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
398df8bae1dSRodney W. Grimes 	}
3993f67c834SDon Lewis 
40033841545SHajimu UMEMOTO #ifdef IPSEC
40133841545SHajimu UMEMOTO 	if (ipsec_gethist(m, NULL))
40233841545SHajimu UMEMOTO 		goto pass;
40333841545SHajimu UMEMOTO #endif
4043f67c834SDon Lewis 
4054dd1662bSUgen J.S. Antsilevich 	/*
4064dd1662bSUgen J.S. Antsilevich 	 * IpHack's section.
4074dd1662bSUgen J.S. Antsilevich 	 * Right now when no processing on packet has done
4084dd1662bSUgen J.S. Antsilevich 	 * and it is still fresh out of network we do our black
4094dd1662bSUgen J.S. Antsilevich 	 * deals with it.
41093e0e116SJulian Elischer 	 * - Firewall: deny/allow/divert
411fed1c7e9SSøren Schmidt 	 * - Xlate: translate packet's addr/port (NAT).
412b715f178SLuigi Rizzo 	 * - Pipe: pass pkt through dummynet.
4134dd1662bSUgen J.S. Antsilevich 	 * - Wrap: fake packet's addr/port <unimpl.>
4144dd1662bSUgen J.S. Antsilevich 	 * - Encapsulate: put it in another IP and send out. <unimp.>
4154dd1662bSUgen J.S. Antsilevich  	 */
416b715f178SLuigi Rizzo 
417dee383e0SEivind Eklund #if defined(IPFIREWALL) && defined(DUMMYNET)
418b715f178SLuigi Rizzo iphack:
419dee383e0SEivind Eklund #endif
420df8bae1dSRodney W. Grimes 
421c4ac87eaSDarren Reed #ifdef PFIL_HOOKS
422c4ac87eaSDarren Reed 	/*
423c4ac87eaSDarren Reed 	 * Run through list of hooks for input packets.  If there are any
424c4ac87eaSDarren Reed 	 * filters which require that additional packets in the flow are
425c4ac87eaSDarren Reed 	 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
426c4ac87eaSDarren Reed 	 * Note that filters must _never_ set this flag, as another filter
427c4ac87eaSDarren Reed 	 * in the list may have previously cleared it.
428c4ac87eaSDarren Reed 	 */
429c4ac87eaSDarren Reed 	m0 = m;
430c4ac87eaSDarren Reed 	pfh = pfil_hook_get(PFIL_IN, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
431fc2ffbe6SPoul-Henning Kamp 	for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link))
432c4ac87eaSDarren Reed 		if (pfh->pfil_func) {
433c4ac87eaSDarren Reed 			rv = pfh->pfil_func(ip, hlen,
434c4ac87eaSDarren Reed 					    m->m_pkthdr.rcvif, 0, &m0);
435c4ac87eaSDarren Reed 			if (rv)
436beec8214SDarren Reed 				return;
437c4ac87eaSDarren Reed 			m = m0;
438c4ac87eaSDarren Reed 			if (m == NULL)
439c4ac87eaSDarren Reed 				return;
440c4ac87eaSDarren Reed 			ip = mtod(m, struct ip *);
441beec8214SDarren Reed 		}
442c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */
443c4ac87eaSDarren Reed 
4446bc748b0SLuigi Rizzo 	if (fw_enable && ip_fw_chk_ptr) {
445f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
446f9e354dfSJulian Elischer 		/*
447f9e354dfSJulian Elischer 		 * If we've been forwarded from the output side, then
448f9e354dfSJulian Elischer 		 * skip the firewall a second time
449f9e354dfSJulian Elischer 		 */
450f9e354dfSJulian Elischer 		if (ip_fw_fwd_addr)
451f9e354dfSJulian Elischer 			goto ours;
452f9e354dfSJulian Elischer #endif	/* IPFIREWALL_FORWARD */
453f9e354dfSJulian Elischer 		/*
4548948e4baSArchie Cobbs 		 * See the comment in ip_output for the return values
455b715f178SLuigi Rizzo 		 * produced by the firewall.
456f9e354dfSJulian Elischer 		 */
4578948e4baSArchie Cobbs 		i = (*ip_fw_chk_ptr)(&ip,
4588948e4baSArchie Cobbs 		    hlen, NULL, &divert_cookie, &m, &rule, &ip_fw_fwd_addr);
459507b4b54SLuigi Rizzo 		if (i & IP_FW_PORT_DENY_FLAG) { /* XXX new interface-denied */
460507b4b54SLuigi Rizzo 		    if (m)
461507b4b54SLuigi Rizzo 			m_freem(m);
462b715f178SLuigi Rizzo 		    return ;
463507b4b54SLuigi Rizzo 		}
464507b4b54SLuigi Rizzo 		if (m == NULL) {	/* Packet discarded by firewall */
465507b4b54SLuigi Rizzo 		    static int __debug=10;
466507b4b54SLuigi Rizzo 		    if (__debug >0) {
467507b4b54SLuigi Rizzo 			printf("firewall returns NULL, please update!\n");
468507b4b54SLuigi Rizzo 			__debug-- ;
469507b4b54SLuigi Rizzo 		    }
470507b4b54SLuigi Rizzo 		    return;
471507b4b54SLuigi Rizzo 		}
472b715f178SLuigi Rizzo 		if (i == 0 && ip_fw_fwd_addr == NULL)	/* common case */
473b715f178SLuigi Rizzo 			goto pass;
474b715f178SLuigi Rizzo #ifdef DUMMYNET
4758948e4baSArchie Cobbs                 if ((i & IP_FW_PORT_DYNT_FLAG) != 0) {
4768948e4baSArchie Cobbs                         /* Send packet to the appropriate pipe */
4776a800098SYoshinobu Inoue                         dummynet_io(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule,
4786a800098SYoshinobu Inoue 				    0);
479e4676ba6SJulian Elischer 			return;
48093e0e116SJulian Elischer 		}
481b715f178SLuigi Rizzo #endif
482b715f178SLuigi Rizzo #ifdef IPDIVERT
4838948e4baSArchie Cobbs 		if (i != 0 && (i & IP_FW_PORT_DYNT_FLAG) == 0) {
4848948e4baSArchie Cobbs 			/* Divert or tee packet */
4858948e4baSArchie Cobbs 			divert_info = i;
486b715f178SLuigi Rizzo 			goto ours;
487b715f178SLuigi Rizzo 		}
488b715f178SLuigi Rizzo #endif
489b715f178SLuigi Rizzo #ifdef IPFIREWALL_FORWARD
490b715f178SLuigi Rizzo 		if (i == 0 && ip_fw_fwd_addr != NULL)
491b715f178SLuigi Rizzo 			goto pass;
492b715f178SLuigi Rizzo #endif
493b715f178SLuigi Rizzo 		/*
494b715f178SLuigi Rizzo 		 * if we get here, the packet must be dropped
495b715f178SLuigi Rizzo 		 */
496b715f178SLuigi Rizzo 		m_freem(m);
497b715f178SLuigi Rizzo 		return;
498b715f178SLuigi Rizzo 	}
499b715f178SLuigi Rizzo pass:
500100ba1a6SJordan K. Hubbard 
501df8bae1dSRodney W. Grimes 	/*
502df8bae1dSRodney W. Grimes 	 * Process options and, if not destined for us,
503df8bae1dSRodney W. Grimes 	 * ship it on.  ip_dooptions returns 1 when an
504df8bae1dSRodney W. Grimes 	 * error was detected (causing an icmp message
505df8bae1dSRodney W. Grimes 	 * to be sent and the original packet to be freed).
506df8bae1dSRodney W. Grimes 	 */
507df8bae1dSRodney W. Grimes 	ip_nhops = 0;		/* for source routed packets */
508ed1ff184SJulian Elischer 	if (hlen > sizeof (struct ip) && ip_dooptions(m)) {
509ed1ff184SJulian Elischer #ifdef IPFIREWALL_FORWARD
510ed1ff184SJulian Elischer 		ip_fw_fwd_addr = NULL;
511ed1ff184SJulian Elischer #endif
512c67b1d17SGarrett Wollman 		return;
513ed1ff184SJulian Elischer 	}
514df8bae1dSRodney W. Grimes 
515f0068c4aSGarrett Wollman         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
516f0068c4aSGarrett Wollman          * matter if it is destined to another node, or whether it is
517f0068c4aSGarrett Wollman          * a multicast one, RSVP wants it! and prevents it from being forwarded
518f0068c4aSGarrett Wollman          * anywhere else. Also checks if the rsvp daemon is running before
519f0068c4aSGarrett Wollman 	 * grabbing the packet.
520f0068c4aSGarrett Wollman          */
5211c5de19aSGarrett Wollman 	if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
522f0068c4aSGarrett Wollman 		goto ours;
523f0068c4aSGarrett Wollman 
524df8bae1dSRodney W. Grimes 	/*
525df8bae1dSRodney W. Grimes 	 * Check our list of addresses, to see if the packet is for us.
526cc766e04SGarrett Wollman 	 * If we don't have any addresses, assume any unicast packet
527cc766e04SGarrett Wollman 	 * we receive might be for us (and let the upper layers deal
528cc766e04SGarrett Wollman 	 * with it).
529df8bae1dSRodney W. Grimes 	 */
530cc766e04SGarrett Wollman 	if (TAILQ_EMPTY(&in_ifaddrhead) &&
531cc766e04SGarrett Wollman 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
532cc766e04SGarrett Wollman 		goto ours;
533cc766e04SGarrett Wollman 
5347538a9a0SJonathan Lemon 	/*
5357538a9a0SJonathan Lemon 	 * Cache the destination address of the packet; this may be
5367538a9a0SJonathan Lemon 	 * changed by use of 'ipfw fwd'.
5377538a9a0SJonathan Lemon 	 */
5387538a9a0SJonathan Lemon 	pkt_dst = ip_fw_fwd_addr == NULL ?
5397538a9a0SJonathan Lemon 	    ip->ip_dst : ip_fw_fwd_addr->sin_addr;
5407538a9a0SJonathan Lemon 
541823db0e9SDon Lewis 	/*
542823db0e9SDon Lewis 	 * Enable a consistency check between the destination address
543823db0e9SDon Lewis 	 * and the arrival interface for a unicast packet (the RFC 1122
544823db0e9SDon Lewis 	 * strong ES model) if IP forwarding is disabled and the packet
545e15ae1b2SDon Lewis 	 * is not locally generated and the packet is not subject to
546e15ae1b2SDon Lewis 	 * 'ipfw fwd'.
5473f67c834SDon Lewis 	 *
5483f67c834SDon Lewis          * XXX - Checking also should be disabled if the destination
5493f67c834SDon Lewis 	 * address is ipnat'ed to a different interface.
5503f67c834SDon Lewis 	 *
551a8f12100SDon Lewis 	 * XXX - Checking is incompatible with IP aliases added
5523f67c834SDon Lewis 	 * to the loopback interface instead of the interface where
5533f67c834SDon Lewis 	 * the packets are received.
554823db0e9SDon Lewis 	 */
555823db0e9SDon Lewis 	checkif = ip_checkinterface && (ipforwarding == 0) &&
5569494d596SBrooks Davis 	    m->m_pkthdr.rcvif != NULL &&
557e15ae1b2SDon Lewis 	    ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
558e15ae1b2SDon Lewis 	    (ip_fw_fwd_addr == NULL);
559823db0e9SDon Lewis 
56037d40066SPoul-Henning Kamp 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
561df8bae1dSRodney W. Grimes #define	satosin(sa)	((struct sockaddr_in *)(sa))
562df8bae1dSRodney W. Grimes 
563432aad0eSTor Egge #ifdef BOOTP_COMPAT
564432aad0eSTor Egge 		if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
565432aad0eSTor Egge 			goto ours;
566432aad0eSTor Egge #endif
567f9e354dfSJulian Elischer 		/*
568823db0e9SDon Lewis 		 * If the address matches, verify that the packet
569823db0e9SDon Lewis 		 * arrived via the correct interface if checking is
570823db0e9SDon Lewis 		 * enabled.
571f9e354dfSJulian Elischer 		 */
572823db0e9SDon Lewis 		if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst.s_addr &&
573823db0e9SDon Lewis 		    (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
574ed1ff184SJulian Elischer 			goto ours;
575823db0e9SDon Lewis 		/*
576823db0e9SDon Lewis 		 * Only accept broadcast packets that arrive via the
577823db0e9SDon Lewis 		 * matching interface.  Reception of forwarded directed
578823db0e9SDon Lewis 		 * broadcasts would be handled via ip_forward() and
579823db0e9SDon Lewis 		 * ether_output() with the loopback into the stack for
580823db0e9SDon Lewis 		 * SIMPLEX interfaces handled by ether_output().
581823db0e9SDon Lewis 		 */
582823db0e9SDon Lewis 		if (ia->ia_ifp == m->m_pkthdr.rcvif &&
583823db0e9SDon Lewis 		    ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) {
584df8bae1dSRodney W. Grimes 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
5857538a9a0SJonathan Lemon 			    pkt_dst.s_addr)
586df8bae1dSRodney W. Grimes 				goto ours;
5877538a9a0SJonathan Lemon 			if (ia->ia_netbroadcast.s_addr == pkt_dst.s_addr)
588df8bae1dSRodney W. Grimes 				goto ours;
589df8bae1dSRodney W. Grimes 		}
590df8bae1dSRodney W. Grimes 	}
591df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
592df8bae1dSRodney W. Grimes 		struct in_multi *inm;
593df8bae1dSRodney W. Grimes 		if (ip_mrouter) {
594df8bae1dSRodney W. Grimes 			/*
595df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, all
596df8bae1dSRodney W. Grimes 			 * incoming multicast packets are passed to the
597df8bae1dSRodney W. Grimes 			 * kernel-level multicast forwarding function.
598df8bae1dSRodney W. Grimes 			 * The packet is returned (relatively) intact; if
599df8bae1dSRodney W. Grimes 			 * ip_mforward() returns a non-zero value, the packet
600df8bae1dSRodney W. Grimes 			 * must be discarded, else it may be accepted below.
601df8bae1dSRodney W. Grimes 			 */
602f0068c4aSGarrett Wollman 			if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
603df8bae1dSRodney W. Grimes 				ipstat.ips_cantforward++;
604df8bae1dSRodney W. Grimes 				m_freem(m);
605c67b1d17SGarrett Wollman 				return;
606df8bae1dSRodney W. Grimes 			}
607df8bae1dSRodney W. Grimes 
608df8bae1dSRodney W. Grimes 			/*
609df8bae1dSRodney W. Grimes 			 * The process-level routing demon needs to receive
610df8bae1dSRodney W. Grimes 			 * all multicast IGMP packets, whether or not this
611df8bae1dSRodney W. Grimes 			 * host belongs to their destination groups.
612df8bae1dSRodney W. Grimes 			 */
613df8bae1dSRodney W. Grimes 			if (ip->ip_p == IPPROTO_IGMP)
614df8bae1dSRodney W. Grimes 				goto ours;
615df8bae1dSRodney W. Grimes 			ipstat.ips_forward++;
616df8bae1dSRodney W. Grimes 		}
617df8bae1dSRodney W. Grimes 		/*
618df8bae1dSRodney W. Grimes 		 * See if we belong to the destination multicast group on the
619df8bae1dSRodney W. Grimes 		 * arrival interface.
620df8bae1dSRodney W. Grimes 		 */
621df8bae1dSRodney W. Grimes 		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
622df8bae1dSRodney W. Grimes 		if (inm == NULL) {
62382c39223SGarrett Wollman 			ipstat.ips_notmember++;
624df8bae1dSRodney W. Grimes 			m_freem(m);
625c67b1d17SGarrett Wollman 			return;
626df8bae1dSRodney W. Grimes 		}
627df8bae1dSRodney W. Grimes 		goto ours;
628df8bae1dSRodney W. Grimes 	}
629df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
630df8bae1dSRodney W. Grimes 		goto ours;
631df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == INADDR_ANY)
632df8bae1dSRodney W. Grimes 		goto ours;
633df8bae1dSRodney W. Grimes 
6346a800098SYoshinobu Inoue 	/*
6356a800098SYoshinobu Inoue 	 * FAITH(Firewall Aided Internet Translator)
6366a800098SYoshinobu Inoue 	 */
6376a800098SYoshinobu Inoue 	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
6386a800098SYoshinobu Inoue 		if (ip_keepfaith) {
6396a800098SYoshinobu Inoue 			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
6406a800098SYoshinobu Inoue 				goto ours;
6416a800098SYoshinobu Inoue 		}
6426a800098SYoshinobu Inoue 		m_freem(m);
6436a800098SYoshinobu Inoue 		return;
6446a800098SYoshinobu Inoue 	}
6459494d596SBrooks Davis 
646df8bae1dSRodney W. Grimes 	/*
647df8bae1dSRodney W. Grimes 	 * Not for us; forward if possible and desirable.
648df8bae1dSRodney W. Grimes 	 */
649df8bae1dSRodney W. Grimes 	if (ipforwarding == 0) {
650df8bae1dSRodney W. Grimes 		ipstat.ips_cantforward++;
651df8bae1dSRodney W. Grimes 		m_freem(m);
652df8bae1dSRodney W. Grimes 	} else
653df8bae1dSRodney W. Grimes 		ip_forward(m, 0);
654ed1ff184SJulian Elischer #ifdef IPFIREWALL_FORWARD
655ed1ff184SJulian Elischer 	ip_fw_fwd_addr = NULL;
656ed1ff184SJulian Elischer #endif
657c67b1d17SGarrett Wollman 	return;
658df8bae1dSRodney W. Grimes 
659df8bae1dSRodney W. Grimes ours:
6605da9f8faSJosef Karthauser 	/* Count the packet in the ip address stats */
6615da9f8faSJosef Karthauser 	if (ia != NULL) {
6625da9f8faSJosef Karthauser 		ia->ia_ifa.if_ipackets++;
6635da9f8faSJosef Karthauser 		ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
6645da9f8faSJosef Karthauser 	}
665100ba1a6SJordan K. Hubbard 
66663f8d699SJordan K. Hubbard 	/*
667df8bae1dSRodney W. Grimes 	 * If offset or IP_MF are set, must reassemble.
668df8bae1dSRodney W. Grimes 	 * Otherwise, nothing need be done.
669df8bae1dSRodney W. Grimes 	 * (We could look in the reassembly queue to see
670df8bae1dSRodney W. Grimes 	 * if the packet was previously fragmented,
671df8bae1dSRodney W. Grimes 	 * but it's not worth the time; just let them time out.)
672df8bae1dSRodney W. Grimes 	 */
673b6ea1aa5SRuslan Ermilov 	if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
6746a800098SYoshinobu Inoue 
675194a213eSAndrey A. Chernov 		sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
676df8bae1dSRodney W. Grimes 		/*
677df8bae1dSRodney W. Grimes 		 * Look for queue of fragments
678df8bae1dSRodney W. Grimes 		 * of this datagram.
679df8bae1dSRodney W. Grimes 		 */
680462b86feSPoul-Henning Kamp 		TAILQ_FOREACH(fp, &ipq[sum], ipq_list)
681df8bae1dSRodney W. Grimes 			if (ip->ip_id == fp->ipq_id &&
682df8bae1dSRodney W. Grimes 			    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
683df8bae1dSRodney W. Grimes 			    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
684df8bae1dSRodney W. Grimes 			    ip->ip_p == fp->ipq_p)
685df8bae1dSRodney W. Grimes 				goto found;
686df8bae1dSRodney W. Grimes 
687194a213eSAndrey A. Chernov 		fp = 0;
688194a213eSAndrey A. Chernov 
689194a213eSAndrey A. Chernov 		/* check if there's a place for the new queue */
690194a213eSAndrey A. Chernov 		if (nipq > maxnipq) {
691194a213eSAndrey A. Chernov 		    /*
692194a213eSAndrey A. Chernov 		     * drop something from the tail of the current queue
693194a213eSAndrey A. Chernov 		     * before proceeding further
694194a213eSAndrey A. Chernov 		     */
695462b86feSPoul-Henning Kamp 		    struct ipq *q = TAILQ_LAST(&ipq[sum], ipqhead);
696462b86feSPoul-Henning Kamp 		    if (q == NULL) {   /* gak */
697194a213eSAndrey A. Chernov 			for (i = 0; i < IPREASS_NHASH; i++) {
698462b86feSPoul-Henning Kamp 			    struct ipq *r = TAILQ_LAST(&ipq[i], ipqhead);
699462b86feSPoul-Henning Kamp 			    if (r) {
700462b86feSPoul-Henning Kamp 				ip_freef(&ipq[i], r);
701194a213eSAndrey A. Chernov 				break;
702194a213eSAndrey A. Chernov 			    }
703194a213eSAndrey A. Chernov 			}
704194a213eSAndrey A. Chernov 		    } else
705462b86feSPoul-Henning Kamp 			ip_freef(&ipq[sum], q);
706194a213eSAndrey A. Chernov 		}
707194a213eSAndrey A. Chernov found:
708df8bae1dSRodney W. Grimes 		/*
709df8bae1dSRodney W. Grimes 		 * Adjust ip_len to not reflect header,
710df8bae1dSRodney W. Grimes 		 * convert offset of this to bytes.
711df8bae1dSRodney W. Grimes 		 */
712df8bae1dSRodney W. Grimes 		ip->ip_len -= hlen;
713b6ea1aa5SRuslan Ermilov 		if (ip->ip_off & IP_MF) {
7146effc713SDoug Rabson 		        /*
7156effc713SDoug Rabson 		         * Make sure that fragments have a data length
7166effc713SDoug Rabson 			 * that's a non-zero multiple of 8 bytes.
7176effc713SDoug Rabson 		         */
7186effc713SDoug Rabson 			if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
7196effc713SDoug Rabson 				ipstat.ips_toosmall++; /* XXX */
7206effc713SDoug Rabson 				goto bad;
7216effc713SDoug Rabson 			}
7226effc713SDoug Rabson 			m->m_flags |= M_FRAG;
7236effc713SDoug Rabson 		}
724df8bae1dSRodney W. Grimes 		ip->ip_off <<= 3;
725df8bae1dSRodney W. Grimes 
726df8bae1dSRodney W. Grimes 		/*
727b6ea1aa5SRuslan Ermilov 		 * Attempt reassembly; if it succeeds, proceed.
728df8bae1dSRodney W. Grimes 		 */
729df8bae1dSRodney W. Grimes 		ipstat.ips_fragments++;
730487bdb38SRuslan Ermilov 		m->m_pkthdr.header = ip;
7318948e4baSArchie Cobbs #ifdef IPDIVERT
7326a800098SYoshinobu Inoue 		m = ip_reass(m,
733462b86feSPoul-Henning Kamp 		    &ipq[sum], fp, &divert_info, &divert_cookie);
7348948e4baSArchie Cobbs #else
735462b86feSPoul-Henning Kamp 		m = ip_reass(m, &ipq[sum], fp);
7368948e4baSArchie Cobbs #endif
7376a800098SYoshinobu Inoue 		if (m == 0) {
738f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
739f9e354dfSJulian Elischer 			ip_fw_fwd_addr = NULL;
740f9e354dfSJulian Elischer #endif
741c67b1d17SGarrett Wollman 			return;
742f9e354dfSJulian Elischer 		}
743df8bae1dSRodney W. Grimes 		ipstat.ips_reassembled++;
7446a800098SYoshinobu Inoue 		ip = mtod(m, struct ip *);
7457e2df452SRuslan Ermilov 		/* Get the header length of the reassembled packet */
7467e2df452SRuslan Ermilov 		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
747af782f1cSBrian Somers #ifdef IPDIVERT
7488948e4baSArchie Cobbs 		/* Restore original checksum before diverting packet */
7498948e4baSArchie Cobbs 		if (divert_info != 0) {
750af782f1cSBrian Somers 			ip->ip_len += hlen;
751af782f1cSBrian Somers 			HTONS(ip->ip_len);
752af782f1cSBrian Somers 			HTONS(ip->ip_off);
753af782f1cSBrian Somers 			ip->ip_sum = 0;
75460123168SRuslan Ermilov 			if (hlen == sizeof(struct ip))
755af782f1cSBrian Somers 				ip->ip_sum = in_cksum_hdr(ip);
75660123168SRuslan Ermilov 			else
75760123168SRuslan Ermilov 				ip->ip_sum = in_cksum(m, hlen);
758af782f1cSBrian Somers 			NTOHS(ip->ip_off);
759af782f1cSBrian Somers 			NTOHS(ip->ip_len);
760af782f1cSBrian Somers 			ip->ip_len -= hlen;
761af782f1cSBrian Somers 		}
762af782f1cSBrian Somers #endif
763df8bae1dSRodney W. Grimes 	} else
764df8bae1dSRodney W. Grimes 		ip->ip_len -= hlen;
765df8bae1dSRodney W. Grimes 
76693e0e116SJulian Elischer #ifdef IPDIVERT
76793e0e116SJulian Elischer 	/*
7688948e4baSArchie Cobbs 	 * Divert or tee packet to the divert protocol if required.
7698948e4baSArchie Cobbs 	 *
7708948e4baSArchie Cobbs 	 * If divert_info is zero then cookie should be too, so we shouldn't
7718948e4baSArchie Cobbs 	 * need to clear them here.  Assume divert_packet() does so also.
77293e0e116SJulian Elischer 	 */
7738948e4baSArchie Cobbs 	if (divert_info != 0) {
7748948e4baSArchie Cobbs 		struct mbuf *clone = NULL;
7758948e4baSArchie Cobbs 
7768948e4baSArchie Cobbs 		/* Clone packet if we're doing a 'tee' */
7778948e4baSArchie Cobbs 		if ((divert_info & IP_FW_PORT_TEE_FLAG) != 0)
7788948e4baSArchie Cobbs 			clone = m_dup(m, M_DONTWAIT);
7798948e4baSArchie Cobbs 
7808948e4baSArchie Cobbs 		/* Restore packet header fields to original values */
7818948e4baSArchie Cobbs 		ip->ip_len += hlen;
7828948e4baSArchie Cobbs 		HTONS(ip->ip_len);
7838948e4baSArchie Cobbs 		HTONS(ip->ip_off);
7848948e4baSArchie Cobbs 
7858948e4baSArchie Cobbs 		/* Deliver packet to divert input routine */
7868948e4baSArchie Cobbs 		ip_divert_cookie = divert_cookie;
7878948e4baSArchie Cobbs 		divert_packet(m, 1, divert_info & 0xffff);
788e4676ba6SJulian Elischer 		ipstat.ips_delivered++;
7898948e4baSArchie Cobbs 
7908948e4baSArchie Cobbs 		/* If 'tee', continue with original packet */
7918948e4baSArchie Cobbs 		if (clone == NULL)
79293e0e116SJulian Elischer 			return;
7938948e4baSArchie Cobbs 		m = clone;
7948948e4baSArchie Cobbs 		ip = mtod(m, struct ip *);
79593e0e116SJulian Elischer 	}
79693e0e116SJulian Elischer #endif
79793e0e116SJulian Elischer 
79833841545SHajimu UMEMOTO #ifdef IPSEC
79933841545SHajimu UMEMOTO 	/*
80033841545SHajimu UMEMOTO 	 * enforce IPsec policy checking if we are seeing last header.
80133841545SHajimu UMEMOTO 	 * note that we do not visit this with protocols with pcb layer
80233841545SHajimu UMEMOTO 	 * code - like udp/tcp/raw ip.
80333841545SHajimu UMEMOTO 	 */
80433841545SHajimu UMEMOTO 	if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
80533841545SHajimu UMEMOTO 	    ipsec4_in_reject(m, NULL)) {
80633841545SHajimu UMEMOTO 		ipsecstat.in_polvio++;
80733841545SHajimu UMEMOTO 		goto bad;
80833841545SHajimu UMEMOTO 	}
80933841545SHajimu UMEMOTO #endif
81033841545SHajimu UMEMOTO 
811df8bae1dSRodney W. Grimes 	/*
812df8bae1dSRodney W. Grimes 	 * Switch out to protocol's input routine.
813df8bae1dSRodney W. Grimes 	 */
814df8bae1dSRodney W. Grimes 	ipstat.ips_delivered++;
8156a800098SYoshinobu Inoue     {
816f0ffb944SJulian Elischer 	int off = hlen;
8176a800098SYoshinobu Inoue 
818f0ffb944SJulian Elischer 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, off);
819f9e354dfSJulian Elischer #ifdef	IPFIREWALL_FORWARD
820f9e354dfSJulian Elischer 	ip_fw_fwd_addr = NULL;	/* tcp needed it */
821f9e354dfSJulian Elischer #endif
822c67b1d17SGarrett Wollman 	return;
8236a800098SYoshinobu Inoue     }
824df8bae1dSRodney W. Grimes bad:
825f9e354dfSJulian Elischer #ifdef	IPFIREWALL_FORWARD
826f9e354dfSJulian Elischer 	ip_fw_fwd_addr = NULL;
827f9e354dfSJulian Elischer #endif
828df8bae1dSRodney W. Grimes 	m_freem(m);
829c67b1d17SGarrett Wollman }
830c67b1d17SGarrett Wollman 
831c67b1d17SGarrett Wollman /*
832c67b1d17SGarrett Wollman  * IP software interrupt routine - to go away sometime soon
833c67b1d17SGarrett Wollman  */
834c67b1d17SGarrett Wollman static void
835c67b1d17SGarrett Wollman ipintr(void)
836c67b1d17SGarrett Wollman {
837c67b1d17SGarrett Wollman 	struct mbuf *m;
838c67b1d17SGarrett Wollman 
839c67b1d17SGarrett Wollman 	while (1) {
840c67b1d17SGarrett Wollman 		IF_DEQUEUE(&ipintrq, m);
841c67b1d17SGarrett Wollman 		if (m == 0)
842c67b1d17SGarrett Wollman 			return;
843c67b1d17SGarrett Wollman 		ip_input(m);
844c67b1d17SGarrett Wollman 	}
845df8bae1dSRodney W. Grimes }
846df8bae1dSRodney W. Grimes 
847df8bae1dSRodney W. Grimes /*
8488948e4baSArchie Cobbs  * Take incoming datagram fragment and try to reassemble it into
8498948e4baSArchie Cobbs  * whole datagram.  If a chain for reassembly of this datagram already
8508948e4baSArchie Cobbs  * exists, then it is given as fp; otherwise have to make a chain.
8518948e4baSArchie Cobbs  *
8528948e4baSArchie Cobbs  * When IPDIVERT enabled, keep additional state with each packet that
8538948e4baSArchie Cobbs  * tells us if we need to divert or tee the packet we're building.
854df8bae1dSRodney W. Grimes  */
8558948e4baSArchie Cobbs 
8566a800098SYoshinobu Inoue static struct mbuf *
8578948e4baSArchie Cobbs #ifdef IPDIVERT
858462b86feSPoul-Henning Kamp ip_reass(m, head, fp, divinfo, divcookie)
8598948e4baSArchie Cobbs #else
860462b86feSPoul-Henning Kamp ip_reass(m, head, fp)
8618948e4baSArchie Cobbs #endif
862462b86feSPoul-Henning Kamp 	struct mbuf *m;
863462b86feSPoul-Henning Kamp 	struct ipqhead *head;
864462b86feSPoul-Henning Kamp 	struct ipq *fp;
8658948e4baSArchie Cobbs #ifdef IPDIVERT
8668948e4baSArchie Cobbs 	u_int32_t *divinfo;
8678948e4baSArchie Cobbs 	u_int16_t *divcookie;
8688948e4baSArchie Cobbs #endif
869df8bae1dSRodney W. Grimes {
8706effc713SDoug Rabson 	struct ip *ip = mtod(m, struct ip *);
871b6ea1aa5SRuslan Ermilov 	register struct mbuf *p, *q, *nq;
872df8bae1dSRodney W. Grimes 	struct mbuf *t;
8736effc713SDoug Rabson 	int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
874df8bae1dSRodney W. Grimes 	int i, next;
875df8bae1dSRodney W. Grimes 
876df8bae1dSRodney W. Grimes 	/*
877df8bae1dSRodney W. Grimes 	 * Presence of header sizes in mbufs
878df8bae1dSRodney W. Grimes 	 * would confuse code below.
879df8bae1dSRodney W. Grimes 	 */
880df8bae1dSRodney W. Grimes 	m->m_data += hlen;
881df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
882df8bae1dSRodney W. Grimes 
883df8bae1dSRodney W. Grimes 	/*
884df8bae1dSRodney W. Grimes 	 * If first fragment to arrive, create a reassembly queue.
885df8bae1dSRodney W. Grimes 	 */
886df8bae1dSRodney W. Grimes 	if (fp == 0) {
887690a6055SJesper Skriver 		/*
888690a6055SJesper Skriver 		 * Enforce upper bound on number of fragmented packets
889690a6055SJesper Skriver 		 * for which we attempt reassembly;
890690a6055SJesper Skriver 		 * If maxfrag is 0, never accept fragments.
891690a6055SJesper Skriver 		 * If maxfrag is -1, accept all fragments without limitation.
892690a6055SJesper Skriver 		 */
893690a6055SJesper Skriver 		if ((ip_maxfragpackets >= 0) && (ip_nfragpackets >= ip_maxfragpackets))
894690a6055SJesper Skriver 			goto dropfrag;
895690a6055SJesper Skriver 		ip_nfragpackets++;
896df8bae1dSRodney W. Grimes 		if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
897df8bae1dSRodney W. Grimes 			goto dropfrag;
898df8bae1dSRodney W. Grimes 		fp = mtod(t, struct ipq *);
899462b86feSPoul-Henning Kamp 		TAILQ_INSERT_HEAD(head, fp, ipq_list);
900194a213eSAndrey A. Chernov 		nipq++;
901df8bae1dSRodney W. Grimes 		fp->ipq_ttl = IPFRAGTTL;
902df8bae1dSRodney W. Grimes 		fp->ipq_p = ip->ip_p;
903df8bae1dSRodney W. Grimes 		fp->ipq_id = ip->ip_id;
9046effc713SDoug Rabson 		fp->ipq_src = ip->ip_src;
9056effc713SDoug Rabson 		fp->ipq_dst = ip->ip_dst;
906af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
907af38c68cSLuigi Rizzo 		m->m_nextpkt = NULL;
90893e0e116SJulian Elischer #ifdef IPDIVERT
9098948e4baSArchie Cobbs 		fp->ipq_div_info = 0;
910bb60f459SJulian Elischer 		fp->ipq_div_cookie = 0;
91193e0e116SJulian Elischer #endif
912af38c68cSLuigi Rizzo 		goto inserted;
913df8bae1dSRodney W. Grimes 	}
914df8bae1dSRodney W. Grimes 
9156effc713SDoug Rabson #define GETIP(m)	((struct ip*)((m)->m_pkthdr.header))
9166effc713SDoug Rabson 
917df8bae1dSRodney W. Grimes 	/*
918df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
919df8bae1dSRodney W. Grimes 	 */
9206effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
9216effc713SDoug Rabson 		if (GETIP(q)->ip_off > ip->ip_off)
922df8bae1dSRodney W. Grimes 			break;
923df8bae1dSRodney W. Grimes 
924df8bae1dSRodney W. Grimes 	/*
925df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
926df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
927af38c68cSLuigi Rizzo 	 * segment.  If it provides all of our data, drop us, otherwise
928af38c68cSLuigi Rizzo 	 * stick new segment in the proper place.
929db4f9cc7SJonathan Lemon 	 *
930db4f9cc7SJonathan Lemon 	 * If some of the data is dropped from the the preceding
931db4f9cc7SJonathan Lemon 	 * segment, then it's checksum is invalidated.
932df8bae1dSRodney W. Grimes 	 */
9336effc713SDoug Rabson 	if (p) {
9346effc713SDoug Rabson 		i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
935df8bae1dSRodney W. Grimes 		if (i > 0) {
936df8bae1dSRodney W. Grimes 			if (i >= ip->ip_len)
937df8bae1dSRodney W. Grimes 				goto dropfrag;
9386a800098SYoshinobu Inoue 			m_adj(m, i);
939db4f9cc7SJonathan Lemon 			m->m_pkthdr.csum_flags = 0;
940df8bae1dSRodney W. Grimes 			ip->ip_off += i;
941df8bae1dSRodney W. Grimes 			ip->ip_len -= i;
942df8bae1dSRodney W. Grimes 		}
943af38c68cSLuigi Rizzo 		m->m_nextpkt = p->m_nextpkt;
944af38c68cSLuigi Rizzo 		p->m_nextpkt = m;
945af38c68cSLuigi Rizzo 	} else {
946af38c68cSLuigi Rizzo 		m->m_nextpkt = fp->ipq_frags;
947af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
948df8bae1dSRodney W. Grimes 	}
949df8bae1dSRodney W. Grimes 
950df8bae1dSRodney W. Grimes 	/*
951df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
952df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
953df8bae1dSRodney W. Grimes 	 */
9546effc713SDoug Rabson 	for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
955af38c68cSLuigi Rizzo 	     q = nq) {
9566effc713SDoug Rabson 		i = (ip->ip_off + ip->ip_len) -
9576effc713SDoug Rabson 		    GETIP(q)->ip_off;
9586effc713SDoug Rabson 		if (i < GETIP(q)->ip_len) {
9596effc713SDoug Rabson 			GETIP(q)->ip_len -= i;
9606effc713SDoug Rabson 			GETIP(q)->ip_off += i;
9616effc713SDoug Rabson 			m_adj(q, i);
962db4f9cc7SJonathan Lemon 			q->m_pkthdr.csum_flags = 0;
963df8bae1dSRodney W. Grimes 			break;
964df8bae1dSRodney W. Grimes 		}
9656effc713SDoug Rabson 		nq = q->m_nextpkt;
966af38c68cSLuigi Rizzo 		m->m_nextpkt = nq;
9676effc713SDoug Rabson 		m_freem(q);
968df8bae1dSRodney W. Grimes 	}
969df8bae1dSRodney W. Grimes 
970af38c68cSLuigi Rizzo inserted:
97193e0e116SJulian Elischer 
97293e0e116SJulian Elischer #ifdef IPDIVERT
97393e0e116SJulian Elischer 	/*
9748948e4baSArchie Cobbs 	 * Transfer firewall instructions to the fragment structure.
9758948e4baSArchie Cobbs 	 * Any fragment diverting causes the whole packet to divert.
97693e0e116SJulian Elischer 	 */
9778948e4baSArchie Cobbs 	fp->ipq_div_info = *divinfo;
9788948e4baSArchie Cobbs 	fp->ipq_div_cookie = *divcookie;
9798948e4baSArchie Cobbs 	*divinfo = 0;
9808948e4baSArchie Cobbs 	*divcookie = 0;
98193e0e116SJulian Elischer #endif
98293e0e116SJulian Elischer 
983df8bae1dSRodney W. Grimes 	/*
984af38c68cSLuigi Rizzo 	 * Check for complete reassembly.
985df8bae1dSRodney W. Grimes 	 */
9866effc713SDoug Rabson 	next = 0;
9876effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
9886effc713SDoug Rabson 		if (GETIP(q)->ip_off != next)
9896effc713SDoug Rabson 			return (0);
9906effc713SDoug Rabson 		next += GETIP(q)->ip_len;
9916effc713SDoug Rabson 	}
9926effc713SDoug Rabson 	/* Make sure the last packet didn't have the IP_MF flag */
9936effc713SDoug Rabson 	if (p->m_flags & M_FRAG)
994df8bae1dSRodney W. Grimes 		return (0);
995df8bae1dSRodney W. Grimes 
996df8bae1dSRodney W. Grimes 	/*
997430d30d8SBill Fenner 	 * Reassembly is complete.  Make sure the packet is a sane size.
998430d30d8SBill Fenner 	 */
9996effc713SDoug Rabson 	q = fp->ipq_frags;
10006effc713SDoug Rabson 	ip = GETIP(q);
10016effc713SDoug Rabson 	if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) {
1002430d30d8SBill Fenner 		ipstat.ips_toolong++;
1003462b86feSPoul-Henning Kamp 		ip_freef(head, fp);
1004430d30d8SBill Fenner 		return (0);
1005430d30d8SBill Fenner 	}
1006430d30d8SBill Fenner 
1007430d30d8SBill Fenner 	/*
1008430d30d8SBill Fenner 	 * Concatenate fragments.
1009df8bae1dSRodney W. Grimes 	 */
10106effc713SDoug Rabson 	m = q;
1011df8bae1dSRodney W. Grimes 	t = m->m_next;
1012df8bae1dSRodney W. Grimes 	m->m_next = 0;
1013df8bae1dSRodney W. Grimes 	m_cat(m, t);
10146effc713SDoug Rabson 	nq = q->m_nextpkt;
1015945aa40dSDoug Rabson 	q->m_nextpkt = 0;
10166effc713SDoug Rabson 	for (q = nq; q != NULL; q = nq) {
10176effc713SDoug Rabson 		nq = q->m_nextpkt;
1018945aa40dSDoug Rabson 		q->m_nextpkt = NULL;
1019db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1020db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1021a8db1d93SJonathan Lemon 		m_cat(m, q);
1022df8bae1dSRodney W. Grimes 	}
1023df8bae1dSRodney W. Grimes 
102493e0e116SJulian Elischer #ifdef IPDIVERT
102593e0e116SJulian Elischer 	/*
10268948e4baSArchie Cobbs 	 * Extract firewall instructions from the fragment structure.
102793e0e116SJulian Elischer 	 */
10288948e4baSArchie Cobbs 	*divinfo = fp->ipq_div_info;
10298948e4baSArchie Cobbs 	*divcookie = fp->ipq_div_cookie;
103093e0e116SJulian Elischer #endif
103193e0e116SJulian Elischer 
1032df8bae1dSRodney W. Grimes 	/*
1033df8bae1dSRodney W. Grimes 	 * Create header for new ip packet by
1034df8bae1dSRodney W. Grimes 	 * modifying header of first packet;
1035df8bae1dSRodney W. Grimes 	 * dequeue and discard fragment reassembly header.
1036df8bae1dSRodney W. Grimes 	 * Make header visible.
1037df8bae1dSRodney W. Grimes 	 */
1038df8bae1dSRodney W. Grimes 	ip->ip_len = next;
10396effc713SDoug Rabson 	ip->ip_src = fp->ipq_src;
10406effc713SDoug Rabson 	ip->ip_dst = fp->ipq_dst;
1041462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(head, fp, ipq_list);
1042194a213eSAndrey A. Chernov 	nipq--;
1043df8bae1dSRodney W. Grimes 	(void) m_free(dtom(fp));
1044690a6055SJesper Skriver 	ip_nfragpackets--;
10456effc713SDoug Rabson 	m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2);
10466effc713SDoug Rabson 	m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2);
1047df8bae1dSRodney W. Grimes 	/* some debugging cruft by sklower, below, will go away soon */
1048df8bae1dSRodney W. Grimes 	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
1049df8bae1dSRodney W. Grimes 		register int plen = 0;
10506a800098SYoshinobu Inoue 		for (t = m; t; t = t->m_next)
10516a800098SYoshinobu Inoue 			plen += t->m_len;
10526a800098SYoshinobu Inoue 		m->m_pkthdr.len = plen;
1053df8bae1dSRodney W. Grimes 	}
10546a800098SYoshinobu Inoue 	return (m);
1055df8bae1dSRodney W. Grimes 
1056df8bae1dSRodney W. Grimes dropfrag:
1057efe39c6aSJulian Elischer #ifdef IPDIVERT
10588948e4baSArchie Cobbs 	*divinfo = 0;
10598948e4baSArchie Cobbs 	*divcookie = 0;
1060efe39c6aSJulian Elischer #endif
1061df8bae1dSRodney W. Grimes 	ipstat.ips_fragdropped++;
1062df8bae1dSRodney W. Grimes 	m_freem(m);
1063df8bae1dSRodney W. Grimes 	return (0);
10646effc713SDoug Rabson 
10656effc713SDoug Rabson #undef GETIP
1066df8bae1dSRodney W. Grimes }
1067df8bae1dSRodney W. Grimes 
1068df8bae1dSRodney W. Grimes /*
1069df8bae1dSRodney W. Grimes  * Free a fragment reassembly header and all
1070df8bae1dSRodney W. Grimes  * associated datagrams.
1071df8bae1dSRodney W. Grimes  */
10720312fbe9SPoul-Henning Kamp static void
1073462b86feSPoul-Henning Kamp ip_freef(fhp, fp)
1074462b86feSPoul-Henning Kamp 	struct ipqhead *fhp;
1075df8bae1dSRodney W. Grimes 	struct ipq *fp;
1076df8bae1dSRodney W. Grimes {
10776effc713SDoug Rabson 	register struct mbuf *q;
1078df8bae1dSRodney W. Grimes 
10796effc713SDoug Rabson 	while (fp->ipq_frags) {
10806effc713SDoug Rabson 		q = fp->ipq_frags;
10816effc713SDoug Rabson 		fp->ipq_frags = q->m_nextpkt;
10826effc713SDoug Rabson 		m_freem(q);
1083df8bae1dSRodney W. Grimes 	}
1084462b86feSPoul-Henning Kamp 	TAILQ_REMOVE(fhp, fp, ipq_list);
1085df8bae1dSRodney W. Grimes 	(void) m_free(dtom(fp));
1086690a6055SJesper Skriver 	ip_nfragpackets--;
1087194a213eSAndrey A. Chernov 	nipq--;
1088df8bae1dSRodney W. Grimes }
1089df8bae1dSRodney W. Grimes 
1090df8bae1dSRodney W. Grimes /*
1091df8bae1dSRodney W. Grimes  * IP timer processing;
1092df8bae1dSRodney W. Grimes  * if a timer expires on a reassembly
1093df8bae1dSRodney W. Grimes  * queue, discard it.
1094df8bae1dSRodney W. Grimes  */
1095df8bae1dSRodney W. Grimes void
1096df8bae1dSRodney W. Grimes ip_slowtimo()
1097df8bae1dSRodney W. Grimes {
1098df8bae1dSRodney W. Grimes 	register struct ipq *fp;
1099df8bae1dSRodney W. Grimes 	int s = splnet();
1100194a213eSAndrey A. Chernov 	int i;
1101df8bae1dSRodney W. Grimes 
1102194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1103462b86feSPoul-Henning Kamp 		for(fp = TAILQ_FIRST(&ipq[i]); fp;) {
1104462b86feSPoul-Henning Kamp 			struct ipq *fpp;
1105462b86feSPoul-Henning Kamp 
1106462b86feSPoul-Henning Kamp 			fpp = fp;
1107462b86feSPoul-Henning Kamp 			fp = TAILQ_NEXT(fp, ipq_list);
1108462b86feSPoul-Henning Kamp 			if(--fpp->ipq_ttl == 0) {
1109df8bae1dSRodney W. Grimes 				ipstat.ips_fragtimeout++;
1110462b86feSPoul-Henning Kamp 				ip_freef(&ipq[i], fpp);
1111df8bae1dSRodney W. Grimes 			}
1112df8bae1dSRodney W. Grimes 		}
1113194a213eSAndrey A. Chernov 	}
1114690a6055SJesper Skriver 	/*
1115690a6055SJesper Skriver 	 * If we are over the maximum number of fragments
1116690a6055SJesper Skriver 	 * (due to the limit being lowered), drain off
1117690a6055SJesper Skriver 	 * enough to get down to the new limit.
1118690a6055SJesper Skriver 	 */
1119690a6055SJesper Skriver 	for (i = 0; i < IPREASS_NHASH; i++) {
1120690a6055SJesper Skriver 		if (ip_maxfragpackets >= 0) {
1121690a6055SJesper Skriver 			while (ip_nfragpackets > ip_maxfragpackets &&
1122690a6055SJesper Skriver 				!TAILQ_EMPTY(&ipq[i])) {
1123690a6055SJesper Skriver 				ipstat.ips_fragdropped++;
1124690a6055SJesper Skriver 				ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
1125690a6055SJesper Skriver 			}
1126690a6055SJesper Skriver 		}
1127690a6055SJesper Skriver 	}
11281f91d8c5SDavid Greenman 	ipflow_slowtimo();
1129df8bae1dSRodney W. Grimes 	splx(s);
1130df8bae1dSRodney W. Grimes }
1131df8bae1dSRodney W. Grimes 
1132df8bae1dSRodney W. Grimes /*
1133df8bae1dSRodney W. Grimes  * Drain off all datagram fragments.
1134df8bae1dSRodney W. Grimes  */
1135df8bae1dSRodney W. Grimes void
1136df8bae1dSRodney W. Grimes ip_drain()
1137df8bae1dSRodney W. Grimes {
1138194a213eSAndrey A. Chernov 	int     i;
1139ce29ab3aSGarrett Wollman 
1140194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1141462b86feSPoul-Henning Kamp 		while(!TAILQ_EMPTY(&ipq[i])) {
1142194a213eSAndrey A. Chernov 			ipstat.ips_fragdropped++;
1143462b86feSPoul-Henning Kamp 			ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
1144194a213eSAndrey A. Chernov 		}
1145194a213eSAndrey A. Chernov 	}
1146ce29ab3aSGarrett Wollman 	in_rtqdrain();
1147df8bae1dSRodney W. Grimes }
1148df8bae1dSRodney W. Grimes 
1149df8bae1dSRodney W. Grimes /*
1150df8bae1dSRodney W. Grimes  * Do option processing on a datagram,
1151df8bae1dSRodney W. Grimes  * possibly discarding it if bad options are encountered,
1152df8bae1dSRodney W. Grimes  * or forwarding it if source-routed.
1153df8bae1dSRodney W. Grimes  * Returns 1 if packet has been forwarded/freed,
1154df8bae1dSRodney W. Grimes  * 0 if the packet should be processed further.
1155df8bae1dSRodney W. Grimes  */
11560312fbe9SPoul-Henning Kamp static int
1157df8bae1dSRodney W. Grimes ip_dooptions(m)
1158df8bae1dSRodney W. Grimes 	struct mbuf *m;
1159df8bae1dSRodney W. Grimes {
1160df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
1161df8bae1dSRodney W. Grimes 	register u_char *cp;
1162df8bae1dSRodney W. Grimes 	register struct ip_timestamp *ipt;
1163df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia;
1164df8bae1dSRodney W. Grimes 	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1165df8bae1dSRodney W. Grimes 	struct in_addr *sin, dst;
1166df8bae1dSRodney W. Grimes 	n_time ntime;
1167df8bae1dSRodney W. Grimes 
1168df8bae1dSRodney W. Grimes 	dst = ip->ip_dst;
1169df8bae1dSRodney W. Grimes 	cp = (u_char *)(ip + 1);
117058938916SGarrett Wollman 	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1171df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1172df8bae1dSRodney W. Grimes 		opt = cp[IPOPT_OPTVAL];
1173df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1174df8bae1dSRodney W. Grimes 			break;
1175df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP)
1176df8bae1dSRodney W. Grimes 			optlen = 1;
1177df8bae1dSRodney W. Grimes 		else {
1178fdcb8debSJun-ichiro itojun Hagino 			if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1179fdcb8debSJun-ichiro itojun Hagino 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1180fdcb8debSJun-ichiro itojun Hagino 				goto bad;
1181fdcb8debSJun-ichiro itojun Hagino 			}
1182df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
1183707d00a3SJonathan Lemon 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1184df8bae1dSRodney W. Grimes 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1185df8bae1dSRodney W. Grimes 				goto bad;
1186df8bae1dSRodney W. Grimes 			}
1187df8bae1dSRodney W. Grimes 		}
1188df8bae1dSRodney W. Grimes 		switch (opt) {
1189df8bae1dSRodney W. Grimes 
1190df8bae1dSRodney W. Grimes 		default:
1191df8bae1dSRodney W. Grimes 			break;
1192df8bae1dSRodney W. Grimes 
1193df8bae1dSRodney W. Grimes 		/*
1194df8bae1dSRodney W. Grimes 		 * Source routing with record.
1195df8bae1dSRodney W. Grimes 		 * Find interface with current destination address.
1196df8bae1dSRodney W. Grimes 		 * If none on this machine then drop if strictly routed,
1197df8bae1dSRodney W. Grimes 		 * or do nothing if loosely routed.
1198df8bae1dSRodney W. Grimes 		 * Record interface address and bring up next address
1199df8bae1dSRodney W. Grimes 		 * component.  If strictly routed make sure next
1200df8bae1dSRodney W. Grimes 		 * address is on directly accessible net.
1201df8bae1dSRodney W. Grimes 		 */
1202df8bae1dSRodney W. Grimes 		case IPOPT_LSRR:
1203df8bae1dSRodney W. Grimes 		case IPOPT_SSRR:
120433841545SHajimu UMEMOTO 			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
120533841545SHajimu UMEMOTO 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
120633841545SHajimu UMEMOTO 				goto bad;
120733841545SHajimu UMEMOTO 			}
1208df8bae1dSRodney W. Grimes 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1209df8bae1dSRodney W. Grimes 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1210df8bae1dSRodney W. Grimes 				goto bad;
1211df8bae1dSRodney W. Grimes 			}
1212df8bae1dSRodney W. Grimes 			ipaddr.sin_addr = ip->ip_dst;
1213df8bae1dSRodney W. Grimes 			ia = (struct in_ifaddr *)
1214df8bae1dSRodney W. Grimes 				ifa_ifwithaddr((struct sockaddr *)&ipaddr);
1215df8bae1dSRodney W. Grimes 			if (ia == 0) {
1216df8bae1dSRodney W. Grimes 				if (opt == IPOPT_SSRR) {
1217df8bae1dSRodney W. Grimes 					type = ICMP_UNREACH;
1218df8bae1dSRodney W. Grimes 					code = ICMP_UNREACH_SRCFAIL;
1219df8bae1dSRodney W. Grimes 					goto bad;
1220df8bae1dSRodney W. Grimes 				}
1221bc189bf8SGuido van Rooij 				if (!ip_dosourceroute)
1222bc189bf8SGuido van Rooij 					goto nosourcerouting;
1223df8bae1dSRodney W. Grimes 				/*
1224df8bae1dSRodney W. Grimes 				 * Loose routing, and not at next destination
1225df8bae1dSRodney W. Grimes 				 * yet; nothing to do except forward.
1226df8bae1dSRodney W. Grimes 				 */
1227df8bae1dSRodney W. Grimes 				break;
1228df8bae1dSRodney W. Grimes 			}
1229df8bae1dSRodney W. Grimes 			off--;			/* 0 origin */
12305d5d5fc0SJonathan Lemon 			if (off > optlen - (int)sizeof(struct in_addr)) {
1231df8bae1dSRodney W. Grimes 				/*
1232df8bae1dSRodney W. Grimes 				 * End of source route.  Should be for us.
1233df8bae1dSRodney W. Grimes 				 */
12344fce5804SGuido van Rooij 				if (!ip_acceptsourceroute)
12354fce5804SGuido van Rooij 					goto nosourcerouting;
1236df8bae1dSRodney W. Grimes 				save_rte(cp, ip->ip_src);
1237df8bae1dSRodney W. Grimes 				break;
1238df8bae1dSRodney W. Grimes 			}
12391025071fSGarrett Wollman 
12401025071fSGarrett Wollman 			if (!ip_dosourceroute) {
12410af8d3ecSDavid Greenman 				if (ipforwarding) {
12420af8d3ecSDavid Greenman 					char buf[16]; /* aaa.bbb.ccc.ddd\0 */
12430af8d3ecSDavid Greenman 					/*
12440af8d3ecSDavid Greenman 					 * Acting as a router, so generate ICMP
12450af8d3ecSDavid Greenman 					 */
1246efa48587SGuido van Rooij nosourcerouting:
1247bc189bf8SGuido van Rooij 					strcpy(buf, inet_ntoa(ip->ip_dst));
12481025071fSGarrett Wollman 					log(LOG_WARNING,
12491025071fSGarrett Wollman 					    "attempted source route from %s to %s\n",
12501025071fSGarrett Wollman 					    inet_ntoa(ip->ip_src), buf);
12511025071fSGarrett Wollman 					type = ICMP_UNREACH;
12521025071fSGarrett Wollman 					code = ICMP_UNREACH_SRCFAIL;
12531025071fSGarrett Wollman 					goto bad;
12540af8d3ecSDavid Greenman 				} else {
12550af8d3ecSDavid Greenman 					/*
12560af8d3ecSDavid Greenman 					 * Not acting as a router, so silently drop.
12570af8d3ecSDavid Greenman 					 */
12580af8d3ecSDavid Greenman 					ipstat.ips_cantforward++;
12590af8d3ecSDavid Greenman 					m_freem(m);
12600af8d3ecSDavid Greenman 					return (1);
12610af8d3ecSDavid Greenman 				}
12621025071fSGarrett Wollman 			}
12631025071fSGarrett Wollman 
1264df8bae1dSRodney W. Grimes 			/*
1265df8bae1dSRodney W. Grimes 			 * locate outgoing interface
1266df8bae1dSRodney W. Grimes 			 */
126794a5d9b6SDavid Greenman 			(void)memcpy(&ipaddr.sin_addr, cp + off,
1268df8bae1dSRodney W. Grimes 			    sizeof(ipaddr.sin_addr));
12691025071fSGarrett Wollman 
1270df8bae1dSRodney W. Grimes 			if (opt == IPOPT_SSRR) {
1271df8bae1dSRodney W. Grimes #define	INA	struct in_ifaddr *
1272df8bae1dSRodney W. Grimes #define	SA	struct sockaddr *
1273df8bae1dSRodney W. Grimes 			    if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
1274df8bae1dSRodney W. Grimes 				ia = (INA)ifa_ifwithnet((SA)&ipaddr);
1275df8bae1dSRodney W. Grimes 			} else
1276df8bae1dSRodney W. Grimes 				ia = ip_rtaddr(ipaddr.sin_addr);
1277df8bae1dSRodney W. Grimes 			if (ia == 0) {
1278df8bae1dSRodney W. Grimes 				type = ICMP_UNREACH;
1279df8bae1dSRodney W. Grimes 				code = ICMP_UNREACH_SRCFAIL;
1280df8bae1dSRodney W. Grimes 				goto bad;
1281df8bae1dSRodney W. Grimes 			}
1282df8bae1dSRodney W. Grimes 			ip->ip_dst = ipaddr.sin_addr;
128394a5d9b6SDavid Greenman 			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
128494a5d9b6SDavid Greenman 			    sizeof(struct in_addr));
1285df8bae1dSRodney W. Grimes 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1286df8bae1dSRodney W. Grimes 			/*
1287df8bae1dSRodney W. Grimes 			 * Let ip_intr's mcast routing check handle mcast pkts
1288df8bae1dSRodney W. Grimes 			 */
1289df8bae1dSRodney W. Grimes 			forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1290df8bae1dSRodney W. Grimes 			break;
1291df8bae1dSRodney W. Grimes 
1292df8bae1dSRodney W. Grimes 		case IPOPT_RR:
1293707d00a3SJonathan Lemon 			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1294707d00a3SJonathan Lemon 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1295707d00a3SJonathan Lemon 				goto bad;
1296707d00a3SJonathan Lemon 			}
1297df8bae1dSRodney W. Grimes 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1298df8bae1dSRodney W. Grimes 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1299df8bae1dSRodney W. Grimes 				goto bad;
1300df8bae1dSRodney W. Grimes 			}
1301df8bae1dSRodney W. Grimes 			/*
1302df8bae1dSRodney W. Grimes 			 * If no space remains, ignore.
1303df8bae1dSRodney W. Grimes 			 */
1304df8bae1dSRodney W. Grimes 			off--;			/* 0 origin */
13055d5d5fc0SJonathan Lemon 			if (off > optlen - (int)sizeof(struct in_addr))
1306df8bae1dSRodney W. Grimes 				break;
130794a5d9b6SDavid Greenman 			(void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1308df8bae1dSRodney W. Grimes 			    sizeof(ipaddr.sin_addr));
1309df8bae1dSRodney W. Grimes 			/*
1310df8bae1dSRodney W. Grimes 			 * locate outgoing interface; if we're the destination,
1311df8bae1dSRodney W. Grimes 			 * use the incoming interface (should be same).
1312df8bae1dSRodney W. Grimes 			 */
1313df8bae1dSRodney W. Grimes 			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
1314df8bae1dSRodney W. Grimes 			    (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
1315df8bae1dSRodney W. Grimes 				type = ICMP_UNREACH;
1316df8bae1dSRodney W. Grimes 				code = ICMP_UNREACH_HOST;
1317df8bae1dSRodney W. Grimes 				goto bad;
1318df8bae1dSRodney W. Grimes 			}
131994a5d9b6SDavid Greenman 			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
132094a5d9b6SDavid Greenman 			    sizeof(struct in_addr));
1321df8bae1dSRodney W. Grimes 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1322df8bae1dSRodney W. Grimes 			break;
1323df8bae1dSRodney W. Grimes 
1324df8bae1dSRodney W. Grimes 		case IPOPT_TS:
1325df8bae1dSRodney W. Grimes 			code = cp - (u_char *)ip;
1326df8bae1dSRodney W. Grimes 			ipt = (struct ip_timestamp *)cp;
132733841545SHajimu UMEMOTO 			if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
132833841545SHajimu UMEMOTO 				code = (u_char *)&ipt->ipt_len - (u_char *)ip;
1329df8bae1dSRodney W. Grimes 				goto bad;
133033841545SHajimu UMEMOTO 			}
133133841545SHajimu UMEMOTO 			if (ipt->ipt_ptr < 5) {
133233841545SHajimu UMEMOTO 				code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
133333841545SHajimu UMEMOTO 				goto bad;
133433841545SHajimu UMEMOTO 			}
13355d5d5fc0SJonathan Lemon 			if (ipt->ipt_ptr >
13365d5d5fc0SJonathan Lemon 			    ipt->ipt_len - (int)sizeof(int32_t)) {
133733841545SHajimu UMEMOTO 				if (++ipt->ipt_oflw == 0) {
133833841545SHajimu UMEMOTO 					code = (u_char *)&ipt->ipt_ptr -
133933841545SHajimu UMEMOTO 					    (u_char *)ip;
1340df8bae1dSRodney W. Grimes 					goto bad;
134133841545SHajimu UMEMOTO 				}
1342df8bae1dSRodney W. Grimes 				break;
1343df8bae1dSRodney W. Grimes 			}
1344df8bae1dSRodney W. Grimes 			sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
1345df8bae1dSRodney W. Grimes 			switch (ipt->ipt_flg) {
1346df8bae1dSRodney W. Grimes 
1347df8bae1dSRodney W. Grimes 			case IPOPT_TS_TSONLY:
1348df8bae1dSRodney W. Grimes 				break;
1349df8bae1dSRodney W. Grimes 
1350df8bae1dSRodney W. Grimes 			case IPOPT_TS_TSANDADDR:
1351b8e8c209SDavid Greenman 				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
135233841545SHajimu UMEMOTO 				    sizeof(struct in_addr) > ipt->ipt_len) {
135333841545SHajimu UMEMOTO 					code = (u_char *)&ipt->ipt_ptr -
135433841545SHajimu UMEMOTO 					    (u_char *)ip;
1355df8bae1dSRodney W. Grimes 					goto bad;
135633841545SHajimu UMEMOTO 				}
1357df8bae1dSRodney W. Grimes 				ipaddr.sin_addr = dst;
1358df8bae1dSRodney W. Grimes 				ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
1359df8bae1dSRodney W. Grimes 							    m->m_pkthdr.rcvif);
1360df8bae1dSRodney W. Grimes 				if (ia == 0)
1361df8bae1dSRodney W. Grimes 					continue;
136294a5d9b6SDavid Greenman 				(void)memcpy(sin, &IA_SIN(ia)->sin_addr,
136394a5d9b6SDavid Greenman 				    sizeof(struct in_addr));
1364df8bae1dSRodney W. Grimes 				ipt->ipt_ptr += sizeof(struct in_addr);
1365df8bae1dSRodney W. Grimes 				break;
1366df8bae1dSRodney W. Grimes 
1367df8bae1dSRodney W. Grimes 			case IPOPT_TS_PRESPEC:
1368b8e8c209SDavid Greenman 				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
136933841545SHajimu UMEMOTO 				    sizeof(struct in_addr) > ipt->ipt_len) {
137033841545SHajimu UMEMOTO 					code = (u_char *)&ipt->ipt_ptr -
137133841545SHajimu UMEMOTO 					    (u_char *)ip;
1372df8bae1dSRodney W. Grimes 					goto bad;
137333841545SHajimu UMEMOTO 				}
137494a5d9b6SDavid Greenman 				(void)memcpy(&ipaddr.sin_addr, sin,
1375df8bae1dSRodney W. Grimes 				    sizeof(struct in_addr));
1376df8bae1dSRodney W. Grimes 				if (ifa_ifwithaddr((SA)&ipaddr) == 0)
1377df8bae1dSRodney W. Grimes 					continue;
1378df8bae1dSRodney W. Grimes 				ipt->ipt_ptr += sizeof(struct in_addr);
1379df8bae1dSRodney W. Grimes 				break;
1380df8bae1dSRodney W. Grimes 
1381df8bae1dSRodney W. Grimes 			default:
138233841545SHajimu UMEMOTO 				/* XXX can't take &ipt->ipt_flg */
138333841545SHajimu UMEMOTO 				code = (u_char *)&ipt->ipt_ptr -
138433841545SHajimu UMEMOTO 				    (u_char *)ip + 1;
1385df8bae1dSRodney W. Grimes 				goto bad;
1386df8bae1dSRodney W. Grimes 			}
1387df8bae1dSRodney W. Grimes 			ntime = iptime();
138894a5d9b6SDavid Greenman 			(void)memcpy(cp + ipt->ipt_ptr - 1, &ntime,
1389df8bae1dSRodney W. Grimes 			    sizeof(n_time));
1390df8bae1dSRodney W. Grimes 			ipt->ipt_ptr += sizeof(n_time);
1391df8bae1dSRodney W. Grimes 		}
1392df8bae1dSRodney W. Grimes 	}
139347174b49SAndrey A. Chernov 	if (forward && ipforwarding) {
1394df8bae1dSRodney W. Grimes 		ip_forward(m, 1);
1395df8bae1dSRodney W. Grimes 		return (1);
1396df8bae1dSRodney W. Grimes 	}
1397df8bae1dSRodney W. Grimes 	return (0);
1398df8bae1dSRodney W. Grimes bad:
1399df8bae1dSRodney W. Grimes 	icmp_error(m, type, code, 0, 0);
1400df8bae1dSRodney W. Grimes 	ipstat.ips_badoptions++;
1401df8bae1dSRodney W. Grimes 	return (1);
1402df8bae1dSRodney W. Grimes }
1403df8bae1dSRodney W. Grimes 
1404df8bae1dSRodney W. Grimes /*
1405df8bae1dSRodney W. Grimes  * Given address of next destination (final or next hop),
1406df8bae1dSRodney W. Grimes  * return internet address info of interface to be used to get there.
1407df8bae1dSRodney W. Grimes  */
14080312fbe9SPoul-Henning Kamp static struct in_ifaddr *
1409df8bae1dSRodney W. Grimes ip_rtaddr(dst)
1410df8bae1dSRodney W. Grimes 	 struct in_addr dst;
1411df8bae1dSRodney W. Grimes {
1412df8bae1dSRodney W. Grimes 	register struct sockaddr_in *sin;
1413df8bae1dSRodney W. Grimes 
1414df8bae1dSRodney W. Grimes 	sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
1415df8bae1dSRodney W. Grimes 
14164078ffb1SRuslan Ermilov 	if (ipforward_rt.ro_rt == 0 ||
14174078ffb1SRuslan Ermilov 	    !(ipforward_rt.ro_rt->rt_flags & RTF_UP) ||
14184078ffb1SRuslan Ermilov 	    dst.s_addr != sin->sin_addr.s_addr) {
1419df8bae1dSRodney W. Grimes 		if (ipforward_rt.ro_rt) {
1420df8bae1dSRodney W. Grimes 			RTFREE(ipforward_rt.ro_rt);
1421df8bae1dSRodney W. Grimes 			ipforward_rt.ro_rt = 0;
1422df8bae1dSRodney W. Grimes 		}
1423df8bae1dSRodney W. Grimes 		sin->sin_family = AF_INET;
1424df8bae1dSRodney W. Grimes 		sin->sin_len = sizeof(*sin);
1425df8bae1dSRodney W. Grimes 		sin->sin_addr = dst;
1426df8bae1dSRodney W. Grimes 
14272c17fe93SGarrett Wollman 		rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1428df8bae1dSRodney W. Grimes 	}
1429df8bae1dSRodney W. Grimes 	if (ipforward_rt.ro_rt == 0)
1430df8bae1dSRodney W. Grimes 		return ((struct in_ifaddr *)0);
1431df8bae1dSRodney W. Grimes 	return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
1432df8bae1dSRodney W. Grimes }
1433df8bae1dSRodney W. Grimes 
1434df8bae1dSRodney W. Grimes /*
1435df8bae1dSRodney W. Grimes  * Save incoming source route for use in replies,
1436df8bae1dSRodney W. Grimes  * to be picked up later by ip_srcroute if the receiver is interested.
1437df8bae1dSRodney W. Grimes  */
1438df8bae1dSRodney W. Grimes void
1439df8bae1dSRodney W. Grimes save_rte(option, dst)
1440df8bae1dSRodney W. Grimes 	u_char *option;
1441df8bae1dSRodney W. Grimes 	struct in_addr dst;
1442df8bae1dSRodney W. Grimes {
1443df8bae1dSRodney W. Grimes 	unsigned olen;
1444df8bae1dSRodney W. Grimes 
1445df8bae1dSRodney W. Grimes 	olen = option[IPOPT_OLEN];
1446df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1447df8bae1dSRodney W. Grimes 	if (ipprintfs)
1448df8bae1dSRodney W. Grimes 		printf("save_rte: olen %d\n", olen);
1449df8bae1dSRodney W. Grimes #endif
1450df8bae1dSRodney W. Grimes 	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1451df8bae1dSRodney W. Grimes 		return;
14520453d3cbSBruce Evans 	bcopy(option, ip_srcrt.srcopt, olen);
1453df8bae1dSRodney W. Grimes 	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1454df8bae1dSRodney W. Grimes 	ip_srcrt.dst = dst;
1455df8bae1dSRodney W. Grimes }
1456df8bae1dSRodney W. Grimes 
1457df8bae1dSRodney W. Grimes /*
1458df8bae1dSRodney W. Grimes  * Retrieve incoming source route for use in replies,
1459df8bae1dSRodney W. Grimes  * in the same form used by setsockopt.
1460df8bae1dSRodney W. Grimes  * The first hop is placed before the options, will be removed later.
1461df8bae1dSRodney W. Grimes  */
1462df8bae1dSRodney W. Grimes struct mbuf *
1463df8bae1dSRodney W. Grimes ip_srcroute()
1464df8bae1dSRodney W. Grimes {
1465df8bae1dSRodney W. Grimes 	register struct in_addr *p, *q;
1466df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1467df8bae1dSRodney W. Grimes 
1468df8bae1dSRodney W. Grimes 	if (ip_nhops == 0)
1469df8bae1dSRodney W. Grimes 		return ((struct mbuf *)0);
1470cfe8b629SGarrett Wollman 	m = m_get(M_DONTWAIT, MT_HEADER);
1471df8bae1dSRodney W. Grimes 	if (m == 0)
1472df8bae1dSRodney W. Grimes 		return ((struct mbuf *)0);
1473df8bae1dSRodney W. Grimes 
1474df8bae1dSRodney W. Grimes #define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1475df8bae1dSRodney W. Grimes 
1476df8bae1dSRodney W. Grimes 	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1477df8bae1dSRodney W. Grimes 	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1478df8bae1dSRodney W. Grimes 	    OPTSIZ;
1479df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1480df8bae1dSRodney W. Grimes 	if (ipprintfs)
1481df8bae1dSRodney W. Grimes 		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1482df8bae1dSRodney W. Grimes #endif
1483df8bae1dSRodney W. Grimes 
1484df8bae1dSRodney W. Grimes 	/*
1485df8bae1dSRodney W. Grimes 	 * First save first hop for return route
1486df8bae1dSRodney W. Grimes 	 */
1487df8bae1dSRodney W. Grimes 	p = &ip_srcrt.route[ip_nhops - 1];
1488df8bae1dSRodney W. Grimes 	*(mtod(m, struct in_addr *)) = *p--;
1489df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1490df8bae1dSRodney W. Grimes 	if (ipprintfs)
1491af38c68cSLuigi Rizzo 		printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr));
1492df8bae1dSRodney W. Grimes #endif
1493df8bae1dSRodney W. Grimes 
1494df8bae1dSRodney W. Grimes 	/*
1495df8bae1dSRodney W. Grimes 	 * Copy option fields and padding (nop) to mbuf.
1496df8bae1dSRodney W. Grimes 	 */
1497df8bae1dSRodney W. Grimes 	ip_srcrt.nop = IPOPT_NOP;
1498df8bae1dSRodney W. Grimes 	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
149994a5d9b6SDavid Greenman 	(void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
150094a5d9b6SDavid Greenman 	    &ip_srcrt.nop, OPTSIZ);
1501df8bae1dSRodney W. Grimes 	q = (struct in_addr *)(mtod(m, caddr_t) +
1502df8bae1dSRodney W. Grimes 	    sizeof(struct in_addr) + OPTSIZ);
1503df8bae1dSRodney W. Grimes #undef OPTSIZ
1504df8bae1dSRodney W. Grimes 	/*
1505df8bae1dSRodney W. Grimes 	 * Record return path as an IP source route,
1506df8bae1dSRodney W. Grimes 	 * reversing the path (pointers are now aligned).
1507df8bae1dSRodney W. Grimes 	 */
1508df8bae1dSRodney W. Grimes 	while (p >= ip_srcrt.route) {
1509df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1510df8bae1dSRodney W. Grimes 		if (ipprintfs)
1511af38c68cSLuigi Rizzo 			printf(" %lx", (u_long)ntohl(q->s_addr));
1512df8bae1dSRodney W. Grimes #endif
1513df8bae1dSRodney W. Grimes 		*q++ = *p--;
1514df8bae1dSRodney W. Grimes 	}
1515df8bae1dSRodney W. Grimes 	/*
1516df8bae1dSRodney W. Grimes 	 * Last hop goes to final destination.
1517df8bae1dSRodney W. Grimes 	 */
1518df8bae1dSRodney W. Grimes 	*q = ip_srcrt.dst;
1519df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1520df8bae1dSRodney W. Grimes 	if (ipprintfs)
1521af38c68cSLuigi Rizzo 		printf(" %lx\n", (u_long)ntohl(q->s_addr));
1522df8bae1dSRodney W. Grimes #endif
1523df8bae1dSRodney W. Grimes 	return (m);
1524df8bae1dSRodney W. Grimes }
1525df8bae1dSRodney W. Grimes 
1526df8bae1dSRodney W. Grimes /*
1527df8bae1dSRodney W. Grimes  * Strip out IP options, at higher
1528df8bae1dSRodney W. Grimes  * level protocol in the kernel.
1529df8bae1dSRodney W. Grimes  * Second argument is buffer to which options
1530df8bae1dSRodney W. Grimes  * will be moved, and return value is their length.
1531df8bae1dSRodney W. Grimes  * XXX should be deleted; last arg currently ignored.
1532df8bae1dSRodney W. Grimes  */
1533df8bae1dSRodney W. Grimes void
1534df8bae1dSRodney W. Grimes ip_stripoptions(m, mopt)
1535df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1536df8bae1dSRodney W. Grimes 	struct mbuf *mopt;
1537df8bae1dSRodney W. Grimes {
1538df8bae1dSRodney W. Grimes 	register int i;
1539df8bae1dSRodney W. Grimes 	struct ip *ip = mtod(m, struct ip *);
1540df8bae1dSRodney W. Grimes 	register caddr_t opts;
1541df8bae1dSRodney W. Grimes 	int olen;
1542df8bae1dSRodney W. Grimes 
154358938916SGarrett Wollman 	olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1544df8bae1dSRodney W. Grimes 	opts = (caddr_t)(ip + 1);
1545df8bae1dSRodney W. Grimes 	i = m->m_len - (sizeof (struct ip) + olen);
1546df8bae1dSRodney W. Grimes 	bcopy(opts + olen, opts, (unsigned)i);
1547df8bae1dSRodney W. Grimes 	m->m_len -= olen;
1548df8bae1dSRodney W. Grimes 	if (m->m_flags & M_PKTHDR)
1549df8bae1dSRodney W. Grimes 		m->m_pkthdr.len -= olen;
155058938916SGarrett Wollman 	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
1551df8bae1dSRodney W. Grimes }
1552df8bae1dSRodney W. Grimes 
1553df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = {
1554df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1555df8bae1dSRodney W. Grimes 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1556df8bae1dSRodney W. Grimes 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1557df8bae1dSRodney W. Grimes 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1558df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
15593b8123b7SJesper Skriver 	ENOPROTOOPT,	ECONNREFUSED
1560df8bae1dSRodney W. Grimes };
1561df8bae1dSRodney W. Grimes 
1562df8bae1dSRodney W. Grimes /*
1563df8bae1dSRodney W. Grimes  * Forward a packet.  If some error occurs return the sender
1564df8bae1dSRodney W. Grimes  * an icmp packet.  Note we can't always generate a meaningful
1565df8bae1dSRodney W. Grimes  * icmp message because icmp doesn't have a large enough repertoire
1566df8bae1dSRodney W. Grimes  * of codes and types.
1567df8bae1dSRodney W. Grimes  *
1568df8bae1dSRodney W. Grimes  * If not forwarding, just drop the packet.  This could be confusing
1569df8bae1dSRodney W. Grimes  * if ipforwarding was zero but some routing protocol was advancing
1570df8bae1dSRodney W. Grimes  * us as a gateway to somewhere.  However, we must let the routing
1571df8bae1dSRodney W. Grimes  * protocol deal with that.
1572df8bae1dSRodney W. Grimes  *
1573df8bae1dSRodney W. Grimes  * The srcrt parameter indicates whether the packet is being forwarded
1574df8bae1dSRodney W. Grimes  * via a source route.
1575df8bae1dSRodney W. Grimes  */
15760312fbe9SPoul-Henning Kamp static void
1577df8bae1dSRodney W. Grimes ip_forward(m, srcrt)
1578df8bae1dSRodney W. Grimes 	struct mbuf *m;
1579df8bae1dSRodney W. Grimes 	int srcrt;
1580df8bae1dSRodney W. Grimes {
1581df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
1582df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
158326f9a767SRodney W. Grimes 	int error, type = 0, code = 0;
1584df8bae1dSRodney W. Grimes 	struct mbuf *mcopy;
1585df8bae1dSRodney W. Grimes 	n_long dest;
1586df8bae1dSRodney W. Grimes 	struct ifnet *destifp;
15876a800098SYoshinobu Inoue #ifdef IPSEC
15886a800098SYoshinobu Inoue 	struct ifnet dummyifp;
15896a800098SYoshinobu Inoue #endif
1590df8bae1dSRodney W. Grimes 
1591df8bae1dSRodney W. Grimes 	dest = 0;
1592df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1593df8bae1dSRodney W. Grimes 	if (ipprintfs)
159461ce519bSPoul-Henning Kamp 		printf("forward: src %lx dst %lx ttl %x\n",
1595162886e2SBruce Evans 		    (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr,
1596162886e2SBruce Evans 		    ip->ip_ttl);
1597df8bae1dSRodney W. Grimes #endif
1598100ba1a6SJordan K. Hubbard 
1599100ba1a6SJordan K. Hubbard 
160092af003dSGarrett Wollman 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1601df8bae1dSRodney W. Grimes 		ipstat.ips_cantforward++;
1602df8bae1dSRodney W. Grimes 		m_freem(m);
1603df8bae1dSRodney W. Grimes 		return;
1604df8bae1dSRodney W. Grimes 	}
16051b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
16061b968362SDag-Erling Smørgrav 	if (!ipstealth) {
16071b968362SDag-Erling Smørgrav #endif
1608df8bae1dSRodney W. Grimes 		if (ip->ip_ttl <= IPTTLDEC) {
16091b968362SDag-Erling Smørgrav 			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
16101b968362SDag-Erling Smørgrav 			    dest, 0);
1611df8bae1dSRodney W. Grimes 			return;
1612df8bae1dSRodney W. Grimes 		}
16131b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
16141b968362SDag-Erling Smørgrav 	}
16151b968362SDag-Erling Smørgrav #endif
1616df8bae1dSRodney W. Grimes 
16174078ffb1SRuslan Ermilov 	if (ip_rtaddr(ip->ip_dst) == 0) {
1618df8bae1dSRodney W. Grimes 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1619df8bae1dSRodney W. Grimes 		return;
16204078ffb1SRuslan Ermilov 	} else
1621df8bae1dSRodney W. Grimes 		rt = ipforward_rt.ro_rt;
1622df8bae1dSRodney W. Grimes 
1623df8bae1dSRodney W. Grimes 	/*
1624bfef7ed4SIan Dowse 	 * Save the IP header and at most 8 bytes of the payload,
1625bfef7ed4SIan Dowse 	 * in case we need to generate an ICMP message to the src.
1626bfef7ed4SIan Dowse 	 *
1627bfef7ed4SIan Dowse 	 * We don't use m_copy() because it might return a reference
1628bfef7ed4SIan Dowse 	 * to a shared cluster. Both this function and ip_output()
1629bfef7ed4SIan Dowse 	 * assume exclusive access to the IP header in `m', so any
1630bfef7ed4SIan Dowse 	 * data in a cluster may change before we reach icmp_error().
1631df8bae1dSRodney W. Grimes 	 */
1632bfef7ed4SIan Dowse 	MGET(mcopy, M_DONTWAIT, m->m_type);
1633bfef7ed4SIan Dowse 	if (mcopy != NULL) {
1634bfef7ed4SIan Dowse 		M_COPY_PKTHDR(mcopy, m);
1635bfef7ed4SIan Dowse 		mcopy->m_len = imin((IP_VHL_HL(ip->ip_vhl) << 2) + 8,
1636bfef7ed4SIan Dowse 		    (int)ip->ip_len);
1637bfef7ed4SIan Dowse 		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1638bfef7ed4SIan Dowse 	}
163904287599SRuslan Ermilov 
164004287599SRuslan Ermilov #ifdef IPSTEALTH
164104287599SRuslan Ermilov 	if (!ipstealth) {
164204287599SRuslan Ermilov #endif
164304287599SRuslan Ermilov 		ip->ip_ttl -= IPTTLDEC;
164404287599SRuslan Ermilov #ifdef IPSTEALTH
164504287599SRuslan Ermilov 	}
164604287599SRuslan Ermilov #endif
1647df8bae1dSRodney W. Grimes 
1648df8bae1dSRodney W. Grimes 	/*
1649df8bae1dSRodney W. Grimes 	 * If forwarding packet using same interface that it came in on,
1650df8bae1dSRodney W. Grimes 	 * perhaps should send a redirect to sender to shortcut a hop.
1651df8bae1dSRodney W. Grimes 	 * Only send redirect if source is sending directly to us,
1652df8bae1dSRodney W. Grimes 	 * and if packet was not source routed (or has any options).
1653df8bae1dSRodney W. Grimes 	 * Also, don't send redirect if forwarding using a default route
1654df8bae1dSRodney W. Grimes 	 * or a route modified by a redirect.
1655df8bae1dSRodney W. Grimes 	 */
1656df8bae1dSRodney W. Grimes #define	satosin(sa)	((struct sockaddr_in *)(sa))
1657df8bae1dSRodney W. Grimes 	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1658df8bae1dSRodney W. Grimes 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1659df8bae1dSRodney W. Grimes 	    satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1660df8bae1dSRodney W. Grimes 	    ipsendredirects && !srcrt) {
1661df8bae1dSRodney W. Grimes #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1662df8bae1dSRodney W. Grimes 		u_long src = ntohl(ip->ip_src.s_addr);
1663df8bae1dSRodney W. Grimes 
1664df8bae1dSRodney W. Grimes 		if (RTA(rt) &&
1665df8bae1dSRodney W. Grimes 		    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1666df8bae1dSRodney W. Grimes 		    if (rt->rt_flags & RTF_GATEWAY)
1667df8bae1dSRodney W. Grimes 			dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1668df8bae1dSRodney W. Grimes 		    else
1669df8bae1dSRodney W. Grimes 			dest = ip->ip_dst.s_addr;
1670df8bae1dSRodney W. Grimes 		    /* Router requirements says to only send host redirects */
1671df8bae1dSRodney W. Grimes 		    type = ICMP_REDIRECT;
1672df8bae1dSRodney W. Grimes 		    code = ICMP_REDIRECT_HOST;
1673df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1674df8bae1dSRodney W. Grimes 		    if (ipprintfs)
1675df8bae1dSRodney W. Grimes 		        printf("redirect (%d) to %lx\n", code, (u_long)dest);
1676df8bae1dSRodney W. Grimes #endif
1677df8bae1dSRodney W. Grimes 		}
1678df8bae1dSRodney W. Grimes 	}
1679df8bae1dSRodney W. Grimes 
1680b97d15cbSGarrett Wollman 	error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1681b97d15cbSGarrett Wollman 			  IP_FORWARDING, 0);
1682df8bae1dSRodney W. Grimes 	if (error)
1683df8bae1dSRodney W. Grimes 		ipstat.ips_cantforward++;
1684df8bae1dSRodney W. Grimes 	else {
1685df8bae1dSRodney W. Grimes 		ipstat.ips_forward++;
1686df8bae1dSRodney W. Grimes 		if (type)
1687df8bae1dSRodney W. Grimes 			ipstat.ips_redirectsent++;
1688df8bae1dSRodney W. Grimes 		else {
16891f91d8c5SDavid Greenman 			if (mcopy) {
16901f91d8c5SDavid Greenman 				ipflow_create(&ipforward_rt, mcopy);
1691df8bae1dSRodney W. Grimes 				m_freem(mcopy);
16921f91d8c5SDavid Greenman 			}
1693df8bae1dSRodney W. Grimes 			return;
1694df8bae1dSRodney W. Grimes 		}
1695df8bae1dSRodney W. Grimes 	}
1696df8bae1dSRodney W. Grimes 	if (mcopy == NULL)
1697df8bae1dSRodney W. Grimes 		return;
1698df8bae1dSRodney W. Grimes 	destifp = NULL;
1699df8bae1dSRodney W. Grimes 
1700df8bae1dSRodney W. Grimes 	switch (error) {
1701df8bae1dSRodney W. Grimes 
1702df8bae1dSRodney W. Grimes 	case 0:				/* forwarded, but need redirect */
1703df8bae1dSRodney W. Grimes 		/* type, code set above */
1704df8bae1dSRodney W. Grimes 		break;
1705df8bae1dSRodney W. Grimes 
1706df8bae1dSRodney W. Grimes 	case ENETUNREACH:		/* shouldn't happen, checked above */
1707df8bae1dSRodney W. Grimes 	case EHOSTUNREACH:
1708df8bae1dSRodney W. Grimes 	case ENETDOWN:
1709df8bae1dSRodney W. Grimes 	case EHOSTDOWN:
1710df8bae1dSRodney W. Grimes 	default:
1711df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1712df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_HOST;
1713df8bae1dSRodney W. Grimes 		break;
1714df8bae1dSRodney W. Grimes 
1715df8bae1dSRodney W. Grimes 	case EMSGSIZE:
1716df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1717df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_NEEDFRAG;
17186a800098SYoshinobu Inoue #ifndef IPSEC
1719df8bae1dSRodney W. Grimes 		if (ipforward_rt.ro_rt)
1720df8bae1dSRodney W. Grimes 			destifp = ipforward_rt.ro_rt->rt_ifp;
17216a800098SYoshinobu Inoue #else
17226a800098SYoshinobu Inoue 		/*
17236a800098SYoshinobu Inoue 		 * If the packet is routed over IPsec tunnel, tell the
17246a800098SYoshinobu Inoue 		 * originator the tunnel MTU.
17256a800098SYoshinobu Inoue 		 *	tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
17266a800098SYoshinobu Inoue 		 * XXX quickhack!!!
17276a800098SYoshinobu Inoue 		 */
17286a800098SYoshinobu Inoue 		if (ipforward_rt.ro_rt) {
17296a800098SYoshinobu Inoue 			struct secpolicy *sp = NULL;
17306a800098SYoshinobu Inoue 			int ipsecerror;
17316a800098SYoshinobu Inoue 			int ipsechdr;
17326a800098SYoshinobu Inoue 			struct route *ro;
17336a800098SYoshinobu Inoue 
17346a800098SYoshinobu Inoue 			sp = ipsec4_getpolicybyaddr(mcopy,
17356a800098SYoshinobu Inoue 						    IPSEC_DIR_OUTBOUND,
17366a800098SYoshinobu Inoue 			                            IP_FORWARDING,
17376a800098SYoshinobu Inoue 			                            &ipsecerror);
17386a800098SYoshinobu Inoue 
17396a800098SYoshinobu Inoue 			if (sp == NULL)
17406a800098SYoshinobu Inoue 				destifp = ipforward_rt.ro_rt->rt_ifp;
17416a800098SYoshinobu Inoue 			else {
17426a800098SYoshinobu Inoue 				/* count IPsec header size */
17436a800098SYoshinobu Inoue 				ipsechdr = ipsec4_hdrsiz(mcopy,
17446a800098SYoshinobu Inoue 							 IPSEC_DIR_OUTBOUND,
17456a800098SYoshinobu Inoue 							 NULL);
17466a800098SYoshinobu Inoue 
17476a800098SYoshinobu Inoue 				/*
17486a800098SYoshinobu Inoue 				 * find the correct route for outer IPv4
17496a800098SYoshinobu Inoue 				 * header, compute tunnel MTU.
17506a800098SYoshinobu Inoue 				 *
17516a800098SYoshinobu Inoue 				 * XXX BUG ALERT
17526a800098SYoshinobu Inoue 				 * The "dummyifp" code relies upon the fact
17536a800098SYoshinobu Inoue 				 * that icmp_error() touches only ifp->if_mtu.
17546a800098SYoshinobu Inoue 				 */
17556a800098SYoshinobu Inoue 				/*XXX*/
17566a800098SYoshinobu Inoue 				destifp = NULL;
17576a800098SYoshinobu Inoue 				if (sp->req != NULL
17586a800098SYoshinobu Inoue 				 && sp->req->sav != NULL
17596a800098SYoshinobu Inoue 				 && sp->req->sav->sah != NULL) {
17606a800098SYoshinobu Inoue 					ro = &sp->req->sav->sah->sa_route;
17616a800098SYoshinobu Inoue 					if (ro->ro_rt && ro->ro_rt->rt_ifp) {
17626a800098SYoshinobu Inoue 						dummyifp.if_mtu =
17636a800098SYoshinobu Inoue 						    ro->ro_rt->rt_ifp->if_mtu;
17646a800098SYoshinobu Inoue 						dummyifp.if_mtu -= ipsechdr;
17656a800098SYoshinobu Inoue 						destifp = &dummyifp;
17666a800098SYoshinobu Inoue 					}
17676a800098SYoshinobu Inoue 				}
17686a800098SYoshinobu Inoue 
17696a800098SYoshinobu Inoue 				key_freesp(sp);
17706a800098SYoshinobu Inoue 			}
17716a800098SYoshinobu Inoue 		}
17726a800098SYoshinobu Inoue #endif /*IPSEC*/
1773df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
1774df8bae1dSRodney W. Grimes 		break;
1775df8bae1dSRodney W. Grimes 
1776df8bae1dSRodney W. Grimes 	case ENOBUFS:
1777df8bae1dSRodney W. Grimes 		type = ICMP_SOURCEQUENCH;
1778df8bae1dSRodney W. Grimes 		code = 0;
1779df8bae1dSRodney W. Grimes 		break;
17803a06e3e0SRuslan Ermilov 
17813a06e3e0SRuslan Ermilov 	case EACCES:			/* ipfw denied packet */
17823a06e3e0SRuslan Ermilov 		m_freem(mcopy);
17833a06e3e0SRuslan Ermilov 		return;
1784df8bae1dSRodney W. Grimes 	}
1785df8bae1dSRodney W. Grimes 	icmp_error(mcopy, type, code, dest, destifp);
1786df8bae1dSRodney W. Grimes }
1787df8bae1dSRodney W. Grimes 
178882c23ebaSBill Fenner void
178982c23ebaSBill Fenner ip_savecontrol(inp, mp, ip, m)
179082c23ebaSBill Fenner 	register struct inpcb *inp;
179182c23ebaSBill Fenner 	register struct mbuf **mp;
179282c23ebaSBill Fenner 	register struct ip *ip;
179382c23ebaSBill Fenner 	register struct mbuf *m;
179482c23ebaSBill Fenner {
179582c23ebaSBill Fenner 	if (inp->inp_socket->so_options & SO_TIMESTAMP) {
179682c23ebaSBill Fenner 		struct timeval tv;
179782c23ebaSBill Fenner 
179882c23ebaSBill Fenner 		microtime(&tv);
179982c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
180082c23ebaSBill Fenner 			SCM_TIMESTAMP, SOL_SOCKET);
180182c23ebaSBill Fenner 		if (*mp)
180282c23ebaSBill Fenner 			mp = &(*mp)->m_next;
180382c23ebaSBill Fenner 	}
180482c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVDSTADDR) {
180582c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
180682c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
180782c23ebaSBill Fenner 		if (*mp)
180882c23ebaSBill Fenner 			mp = &(*mp)->m_next;
180982c23ebaSBill Fenner 	}
181082c23ebaSBill Fenner #ifdef notyet
181182c23ebaSBill Fenner 	/* XXX
181282c23ebaSBill Fenner 	 * Moving these out of udp_input() made them even more broken
181382c23ebaSBill Fenner 	 * than they already were.
181482c23ebaSBill Fenner 	 */
181582c23ebaSBill Fenner 	/* options were tossed already */
181682c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVOPTS) {
181782c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
181882c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
181982c23ebaSBill Fenner 		if (*mp)
182082c23ebaSBill Fenner 			mp = &(*mp)->m_next;
182182c23ebaSBill Fenner 	}
182282c23ebaSBill Fenner 	/* ip_srcroute doesn't do what we want here, need to fix */
182382c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVRETOPTS) {
182482c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) ip_srcroute(),
182582c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
182682c23ebaSBill Fenner 		if (*mp)
182782c23ebaSBill Fenner 			mp = &(*mp)->m_next;
182882c23ebaSBill Fenner 	}
182982c23ebaSBill Fenner #endif
183082c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVIF) {
1831d314ad7bSJulian Elischer 		struct ifnet *ifp;
1832d314ad7bSJulian Elischer 		struct sdlbuf {
183382c23ebaSBill Fenner 			struct sockaddr_dl sdl;
1834d314ad7bSJulian Elischer 			u_char	pad[32];
1835d314ad7bSJulian Elischer 		} sdlbuf;
1836d314ad7bSJulian Elischer 		struct sockaddr_dl *sdp;
1837d314ad7bSJulian Elischer 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
183882c23ebaSBill Fenner 
1839d314ad7bSJulian Elischer 		if (((ifp = m->m_pkthdr.rcvif))
1840d314ad7bSJulian Elischer 		&& ( ifp->if_index && (ifp->if_index <= if_index))) {
1841f9132cebSJonathan Lemon 			sdp = (struct sockaddr_dl *)
1842f9132cebSJonathan Lemon 			    (ifaddr_byindex(ifp->if_index)->ifa_addr);
1843d314ad7bSJulian Elischer 			/*
1844d314ad7bSJulian Elischer 			 * Change our mind and don't try copy.
1845d314ad7bSJulian Elischer 			 */
1846d314ad7bSJulian Elischer 			if ((sdp->sdl_family != AF_LINK)
1847d314ad7bSJulian Elischer 			|| (sdp->sdl_len > sizeof(sdlbuf))) {
1848d314ad7bSJulian Elischer 				goto makedummy;
1849d314ad7bSJulian Elischer 			}
1850d314ad7bSJulian Elischer 			bcopy(sdp, sdl2, sdp->sdl_len);
1851d314ad7bSJulian Elischer 		} else {
1852d314ad7bSJulian Elischer makedummy:
1853d314ad7bSJulian Elischer 			sdl2->sdl_len
1854d314ad7bSJulian Elischer 				= offsetof(struct sockaddr_dl, sdl_data[0]);
1855d314ad7bSJulian Elischer 			sdl2->sdl_family = AF_LINK;
1856d314ad7bSJulian Elischer 			sdl2->sdl_index = 0;
1857d314ad7bSJulian Elischer 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1858d314ad7bSJulian Elischer 		}
1859d314ad7bSJulian Elischer 		*mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
186082c23ebaSBill Fenner 			IP_RECVIF, IPPROTO_IP);
186182c23ebaSBill Fenner 		if (*mp)
186282c23ebaSBill Fenner 			mp = &(*mp)->m_next;
186382c23ebaSBill Fenner 	}
186482c23ebaSBill Fenner }
186582c23ebaSBill Fenner 
1866df8bae1dSRodney W. Grimes int
1867f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so)
1868f0068c4aSGarrett Wollman {
1869f0068c4aSGarrett Wollman 	if (so->so_type != SOCK_RAW ||
1870f0068c4aSGarrett Wollman 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1871f0068c4aSGarrett Wollman 	  return EOPNOTSUPP;
1872f0068c4aSGarrett Wollman 
1873f0068c4aSGarrett Wollman 	if (ip_rsvpd != NULL)
1874f0068c4aSGarrett Wollman 	  return EADDRINUSE;
1875f0068c4aSGarrett Wollman 
1876f0068c4aSGarrett Wollman 	ip_rsvpd = so;
18771c5de19aSGarrett Wollman 	/*
18781c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-increment
18791c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
18801c5de19aSGarrett Wollman 	 */
18811c5de19aSGarrett Wollman 	if (!ip_rsvp_on) {
18821c5de19aSGarrett Wollman 		ip_rsvp_on = 1;
18831c5de19aSGarrett Wollman 		rsvp_on++;
18841c5de19aSGarrett Wollman 	}
1885f0068c4aSGarrett Wollman 
1886f0068c4aSGarrett Wollman 	return 0;
1887f0068c4aSGarrett Wollman }
1888f0068c4aSGarrett Wollman 
1889f0068c4aSGarrett Wollman int
1890f0068c4aSGarrett Wollman ip_rsvp_done(void)
1891f0068c4aSGarrett Wollman {
1892f0068c4aSGarrett Wollman 	ip_rsvpd = NULL;
18931c5de19aSGarrett Wollman 	/*
18941c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-decrement
18951c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
18961c5de19aSGarrett Wollman 	 */
18971c5de19aSGarrett Wollman 	if (ip_rsvp_on) {
18981c5de19aSGarrett Wollman 		ip_rsvp_on = 0;
18991c5de19aSGarrett Wollman 		rsvp_on--;
19001c5de19aSGarrett Wollman 	}
1901f0068c4aSGarrett Wollman 	return 0;
1902f0068c4aSGarrett Wollman }
1903