xref: /freebsd/sys/netinet/ip_input.c (revision b6ea1aa58df0e0ca203982a21d89f98ec941dc70)
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"
4774a9466cSGary Palmer 
4882c23ebaSBill Fenner #include <stddef.h>
4982c23ebaSBill Fenner 
50df8bae1dSRodney W. Grimes #include <sys/param.h>
51df8bae1dSRodney W. Grimes #include <sys/systm.h>
52df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
53b715f178SLuigi Rizzo #include <sys/malloc.h>
54df8bae1dSRodney W. Grimes #include <sys/domain.h>
55df8bae1dSRodney W. Grimes #include <sys/protosw.h>
56df8bae1dSRodney W. Grimes #include <sys/socket.h>
57df8bae1dSRodney W. Grimes #include <sys/time.h>
58df8bae1dSRodney W. Grimes #include <sys/kernel.h>
591025071fSGarrett Wollman #include <sys/syslog.h>
60b5e8ce9fSBruce Evans #include <sys/sysctl.h>
61df8bae1dSRodney W. Grimes 
62c85540ddSAndrey A. Chernov #include <net/pfil.h>
63df8bae1dSRodney W. Grimes #include <net/if.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 
796a800098SYoshinobu Inoue #include <netinet/ipprotosw.h>
806a800098SYoshinobu Inoue 
81f0068c4aSGarrett Wollman #include <sys/socketvar.h>
826ddbf1e2SGary Palmer 
836ddbf1e2SGary Palmer #include <netinet/ip_fw.h>
846ddbf1e2SGary Palmer 
856a800098SYoshinobu Inoue #ifdef IPSEC
866a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
876a800098SYoshinobu Inoue #include <netkey/key.h>
886a800098SYoshinobu Inoue #endif
896a800098SYoshinobu Inoue 
906a800098SYoshinobu Inoue #include "faith.h"
916a800098SYoshinobu Inoue #if defined(NFAITH) && NFAITH > 0
926a800098SYoshinobu Inoue #include <net/if_types.h>
936a800098SYoshinobu Inoue #endif
946a800098SYoshinobu Inoue 
95b715f178SLuigi Rizzo #ifdef DUMMYNET
96b715f178SLuigi Rizzo #include <netinet/ip_dummynet.h>
97b715f178SLuigi Rizzo #endif
98b715f178SLuigi Rizzo 
991c5de19aSGarrett Wollman int rsvp_on = 0;
100f708ef1bSPoul-Henning Kamp static int ip_rsvp_on;
101f0068c4aSGarrett Wollman struct socket *ip_rsvpd;
102f0068c4aSGarrett Wollman 
1031f91d8c5SDavid Greenman int	ipforwarding = 0;
1040312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
1053d177f46SBill Fumerola     &ipforwarding, 0, "Enable IP forwarding between interfaces");
1060312fbe9SPoul-Henning Kamp 
107d4fb926cSGarrett Wollman static int	ipsendredirects = 1; /* XXX */
1080312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
1093d177f46SBill Fumerola     &ipsendredirects, 0, "Enable sending IP redirects");
1100312fbe9SPoul-Henning Kamp 
111df8bae1dSRodney W. Grimes int	ip_defttl = IPDEFTTL;
1120312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
1133d177f46SBill Fumerola     &ip_defttl, 0, "Maximum TTL on IP packets");
1140312fbe9SPoul-Henning Kamp 
1150312fbe9SPoul-Henning Kamp static int	ip_dosourceroute = 0;
1160312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
1173d177f46SBill Fumerola     &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
1184fce5804SGuido van Rooij 
1194fce5804SGuido van Rooij static int	ip_acceptsourceroute = 0;
1204fce5804SGuido van Rooij SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
1213d177f46SBill Fumerola     CTLFLAG_RW, &ip_acceptsourceroute, 0,
1223d177f46SBill Fumerola     "Enable accepting source routed IP packets");
1236a800098SYoshinobu Inoue 
1246a800098SYoshinobu Inoue static int	ip_keepfaith = 0;
1256a800098SYoshinobu Inoue SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
1266a800098SYoshinobu Inoue 	&ip_keepfaith,	0,
1276a800098SYoshinobu Inoue 	"Enable packet capture for FAITH IPv4->IPv6 translater daemon");
1286a800098SYoshinobu Inoue 
129df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1300312fbe9SPoul-Henning Kamp static int	ipprintfs = 0;
131df8bae1dSRodney W. Grimes #endif
132df8bae1dSRodney W. Grimes 
133df8bae1dSRodney W. Grimes extern	struct domain inetdomain;
1346a800098SYoshinobu Inoue extern	struct ipprotosw inetsw[];
135df8bae1dSRodney W. Grimes u_char	ip_protox[IPPROTO_MAX];
1360312fbe9SPoul-Henning Kamp static int	ipqmaxlen = IFQ_MAXLEN;
13759562606SGarrett Wollman struct	in_ifaddrhead in_ifaddrhead; /* first inet address */
138afed1375SDavid Greenman SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
1393d177f46SBill Fumerola     &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
1400312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
1413d177f46SBill Fumerola     &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue");
142df8bae1dSRodney W. Grimes 
143f23b4c91SGarrett Wollman struct ipstat ipstat;
1446fce01c9SGarrett Wollman SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RD,
1453d177f46SBill Fumerola     &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
146194a213eSAndrey A. Chernov 
147194a213eSAndrey A. Chernov /* Packet reassembly stuff */
148194a213eSAndrey A. Chernov #define IPREASS_NHASH_LOG2      6
149194a213eSAndrey A. Chernov #define IPREASS_NHASH           (1 << IPREASS_NHASH_LOG2)
150194a213eSAndrey A. Chernov #define IPREASS_HMASK           (IPREASS_NHASH - 1)
151194a213eSAndrey A. Chernov #define IPREASS_HASH(x,y) \
152831a80b0SMatthew Dillon 	(((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
153194a213eSAndrey A. Chernov 
154194a213eSAndrey A. Chernov static struct ipq ipq[IPREASS_NHASH];
155194a213eSAndrey A. Chernov static int    nipq = 0;         /* total # of reass queues */
156194a213eSAndrey A. Chernov static int    maxnipq;
157367d34f8SBrian Somers const  int    ipintrq_present = 1;
158f23b4c91SGarrett Wollman 
1590312fbe9SPoul-Henning Kamp #ifdef IPCTL_DEFMTU
1600312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
1613d177f46SBill Fumerola     &ip_mtu, 0, "Default MTU");
1620312fbe9SPoul-Henning Kamp #endif
1630312fbe9SPoul-Henning Kamp 
1641b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
1651b968362SDag-Erling Smørgrav static int	ipstealth = 0;
1661b968362SDag-Erling Smørgrav SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
1671b968362SDag-Erling Smørgrav     &ipstealth, 0, "");
1681b968362SDag-Erling Smørgrav #endif
1691b968362SDag-Erling Smørgrav 
170cfe8b629SGarrett Wollman 
17123bf9953SPoul-Henning Kamp /* Firewall hooks */
17223bf9953SPoul-Henning Kamp ip_fw_chk_t *ip_fw_chk_ptr;
17323bf9953SPoul-Henning Kamp ip_fw_ctl_t *ip_fw_ctl_ptr;
1749fcc0795SLuigi Rizzo int fw_enable = 1 ;
175e7319babSPoul-Henning Kamp 
176b715f178SLuigi Rizzo #ifdef DUMMYNET
177b715f178SLuigi Rizzo ip_dn_ctl_t *ip_dn_ctl_ptr;
178b715f178SLuigi Rizzo #endif
179b715f178SLuigi Rizzo 
180afed1b49SDarren Reed 
181e7319babSPoul-Henning Kamp /*
182df8bae1dSRodney W. Grimes  * We need to save the IP options in case a protocol wants to respond
183df8bae1dSRodney W. Grimes  * to an incoming packet over the same route if the packet got here
184df8bae1dSRodney W. Grimes  * using IP source routing.  This allows connection establishment and
185df8bae1dSRodney W. Grimes  * maintenance when the remote end is on a network that is not known
186df8bae1dSRodney W. Grimes  * to us.
187df8bae1dSRodney W. Grimes  */
1880312fbe9SPoul-Henning Kamp static int	ip_nhops = 0;
189df8bae1dSRodney W. Grimes static	struct ip_srcrt {
190df8bae1dSRodney W. Grimes 	struct	in_addr dst;			/* final destination */
191df8bae1dSRodney W. Grimes 	char	nop;				/* one NOP to align */
192df8bae1dSRodney W. Grimes 	char	srcopt[IPOPT_OFFSET + 1];	/* OPTVAL, OLEN and OFFSET */
193df8bae1dSRodney W. Grimes 	struct	in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
194df8bae1dSRodney W. Grimes } ip_srcrt;
195df8bae1dSRodney W. Grimes 
196f9e354dfSJulian Elischer struct sockaddr_in *ip_fw_fwd_addr;
197f9e354dfSJulian Elischer 
198df8bae1dSRodney W. Grimes static void	save_rte __P((u_char *, struct in_addr));
1990312fbe9SPoul-Henning Kamp static int	ip_dooptions __P((struct mbuf *));
2000312fbe9SPoul-Henning Kamp static void	ip_forward __P((struct mbuf *, int));
2010312fbe9SPoul-Henning Kamp static void	ip_freef __P((struct ipq *));
2028948e4baSArchie Cobbs #ifdef IPDIVERT
2036a800098SYoshinobu Inoue static struct	mbuf *ip_reass __P((struct mbuf *,
2048948e4baSArchie Cobbs 			struct ipq *, struct ipq *, u_int32_t *, u_int16_t *));
2058948e4baSArchie Cobbs #else
2066a800098SYoshinobu Inoue static struct	mbuf *ip_reass __P((struct mbuf *, struct ipq *, struct ipq *));
2078948e4baSArchie Cobbs #endif
2088948e4baSArchie Cobbs static struct	in_ifaddr *ip_rtaddr __P((struct in_addr));
2090312fbe9SPoul-Henning Kamp static void	ipintr __P((void));
2108948e4baSArchie Cobbs 
211df8bae1dSRodney W. Grimes /*
212df8bae1dSRodney W. Grimes  * IP initialization: fill in IP protocol switch table.
213df8bae1dSRodney W. Grimes  * All protocols not implemented in kernel go to raw IP protocol handler.
214df8bae1dSRodney W. Grimes  */
215df8bae1dSRodney W. Grimes void
216df8bae1dSRodney W. Grimes ip_init()
217df8bae1dSRodney W. Grimes {
2186a800098SYoshinobu Inoue 	register struct ipprotosw *pr;
219df8bae1dSRodney W. Grimes 	register int i;
220df8bae1dSRodney W. Grimes 
22159562606SGarrett Wollman 	TAILQ_INIT(&in_ifaddrhead);
2226a800098SYoshinobu Inoue 	pr = (struct ipprotosw *)pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
223df8bae1dSRodney W. Grimes 	if (pr == 0)
224df8bae1dSRodney W. Grimes 		panic("ip_init");
225df8bae1dSRodney W. Grimes 	for (i = 0; i < IPPROTO_MAX; i++)
226df8bae1dSRodney W. Grimes 		ip_protox[i] = pr - inetsw;
2276a800098SYoshinobu Inoue 	for (pr = (struct ipprotosw *)inetdomain.dom_protosw;
2286a800098SYoshinobu Inoue 	    pr < (struct ipprotosw *)inetdomain.dom_protoswNPROTOSW; pr++)
229df8bae1dSRodney W. Grimes 		if (pr->pr_domain->dom_family == PF_INET &&
230df8bae1dSRodney W. Grimes 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
231df8bae1dSRodney W. Grimes 			ip_protox[pr->pr_protocol] = pr - inetsw;
232194a213eSAndrey A. Chernov 
233194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++)
234194a213eSAndrey A. Chernov 	    ipq[i].next = ipq[i].prev = &ipq[i];
235194a213eSAndrey A. Chernov 
236194a213eSAndrey A. Chernov 	maxnipq = nmbclusters/4;
237194a213eSAndrey A. Chernov 
238227ee8a1SPoul-Henning Kamp 	ip_id = time_second & 0xffff;
239df8bae1dSRodney W. Grimes 	ipintrq.ifq_maxlen = ipqmaxlen;
240242c5536SPeter Wemm 
241242c5536SPeter Wemm 	register_netisr(NETISR_IP, ipintr);
242df8bae1dSRodney W. Grimes }
243df8bae1dSRodney W. Grimes 
2440312fbe9SPoul-Henning Kamp static struct	sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
245f708ef1bSPoul-Henning Kamp static struct	route ipforward_rt;
246df8bae1dSRodney W. Grimes 
247df8bae1dSRodney W. Grimes /*
248df8bae1dSRodney W. Grimes  * Ip input routine.  Checksum and byte swap header.  If fragmented
249df8bae1dSRodney W. Grimes  * try to reassemble.  Process options.  Pass to next level.
250df8bae1dSRodney W. Grimes  */
251c67b1d17SGarrett Wollman void
252c67b1d17SGarrett Wollman ip_input(struct mbuf *m)
253df8bae1dSRodney W. Grimes {
25423bf9953SPoul-Henning Kamp 	struct ip *ip;
25523bf9953SPoul-Henning Kamp 	struct ipq *fp;
2565da9f8faSJosef Karthauser 	struct in_ifaddr *ia = NULL;
257b6ea1aa5SRuslan Ermilov 	int    i, hlen;
25847c861ecSBrian Somers 	u_short sum;
2598948e4baSArchie Cobbs 	u_int16_t divert_cookie;		/* firewall cookie */
2608948e4baSArchie Cobbs #ifdef IPDIVERT
2618948e4baSArchie Cobbs 	u_int32_t divert_info = 0;		/* packet divert/tee info */
262b715f178SLuigi Rizzo #endif
263b715f178SLuigi Rizzo 	struct ip_fw_chain *rule = NULL;
264c4ac87eaSDarren Reed #ifdef PFIL_HOOKS
265c4ac87eaSDarren Reed 	struct packet_filter_hook *pfh;
266c4ac87eaSDarren Reed 	struct mbuf *m0;
267c4ac87eaSDarren Reed 	int rv;
268c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */
269b715f178SLuigi Rizzo 
2708948e4baSArchie Cobbs #ifdef IPDIVERT
2718948e4baSArchie Cobbs 	/* Get and reset firewall cookie */
2728948e4baSArchie Cobbs 	divert_cookie = ip_divert_cookie;
2738948e4baSArchie Cobbs 	ip_divert_cookie = 0;
2748948e4baSArchie Cobbs #else
2758948e4baSArchie Cobbs 	divert_cookie = 0;
2768948e4baSArchie Cobbs #endif
2778948e4baSArchie Cobbs 
278b715f178SLuigi Rizzo #if defined(IPFIREWALL) && defined(DUMMYNET)
279b715f178SLuigi Rizzo         /*
280b715f178SLuigi Rizzo          * dummynet packet are prepended a vestigial mbuf with
281b715f178SLuigi Rizzo          * m_type = MT_DUMMYNET and m_data pointing to the matching
282b715f178SLuigi Rizzo          * rule.
283b715f178SLuigi Rizzo          */
284b715f178SLuigi Rizzo         if (m->m_type == MT_DUMMYNET) {
285b715f178SLuigi Rizzo             rule = (struct ip_fw_chain *)(m->m_data) ;
286b715f178SLuigi Rizzo             m = m->m_next ;
287b715f178SLuigi Rizzo             ip = mtod(m, struct ip *);
288b715f178SLuigi Rizzo             hlen = IP_VHL_HL(ip->ip_vhl) << 2;
289b715f178SLuigi Rizzo             goto iphack ;
290b715f178SLuigi Rizzo         } else
291b715f178SLuigi Rizzo             rule = NULL ;
292b715f178SLuigi Rizzo #endif
293df8bae1dSRodney W. Grimes 
294df8bae1dSRodney W. Grimes #ifdef	DIAGNOSTIC
295ed7509acSJulian Elischer 	if (m == NULL || (m->m_flags & M_PKTHDR) == 0)
29658938916SGarrett Wollman 		panic("ip_input no HDR");
297df8bae1dSRodney W. Grimes #endif
298df8bae1dSRodney W. Grimes 	ipstat.ips_total++;
29958938916SGarrett Wollman 
30058938916SGarrett Wollman 	if (m->m_pkthdr.len < sizeof(struct ip))
30158938916SGarrett Wollman 		goto tooshort;
30258938916SGarrett Wollman 
303df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct ip) &&
304df8bae1dSRodney W. Grimes 	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
305df8bae1dSRodney W. Grimes 		ipstat.ips_toosmall++;
306c67b1d17SGarrett Wollman 		return;
307df8bae1dSRodney W. Grimes 	}
308df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
30958938916SGarrett Wollman 
31058938916SGarrett Wollman 	if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
311df8bae1dSRodney W. Grimes 		ipstat.ips_badvers++;
312df8bae1dSRodney W. Grimes 		goto bad;
313df8bae1dSRodney W. Grimes 	}
31458938916SGarrett Wollman 
31558938916SGarrett Wollman 	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
316df8bae1dSRodney W. Grimes 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
317df8bae1dSRodney W. Grimes 		ipstat.ips_badhlen++;
318df8bae1dSRodney W. Grimes 		goto bad;
319df8bae1dSRodney W. Grimes 	}
320df8bae1dSRodney W. Grimes 	if (hlen > m->m_len) {
321df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, hlen)) == 0) {
322df8bae1dSRodney W. Grimes 			ipstat.ips_badhlen++;
323c67b1d17SGarrett Wollman 			return;
324df8bae1dSRodney W. Grimes 		}
325df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
326df8bae1dSRodney W. Grimes 	}
327db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
328db4f9cc7SJonathan Lemon 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
329db4f9cc7SJonathan Lemon 	} else {
33058938916SGarrett Wollman 		if (hlen == sizeof(struct ip)) {
33147c861ecSBrian Somers 			sum = in_cksum_hdr(ip);
33258938916SGarrett Wollman 		} else {
33347c861ecSBrian Somers 			sum = in_cksum(m, hlen);
33458938916SGarrett Wollman 		}
335db4f9cc7SJonathan Lemon 	}
33647c861ecSBrian Somers 	if (sum) {
337df8bae1dSRodney W. Grimes 		ipstat.ips_badsum++;
338df8bae1dSRodney W. Grimes 		goto bad;
339df8bae1dSRodney W. Grimes 	}
340df8bae1dSRodney W. Grimes 
341df8bae1dSRodney W. Grimes 	/*
342df8bae1dSRodney W. Grimes 	 * Convert fields to host representation.
343df8bae1dSRodney W. Grimes 	 */
344df8bae1dSRodney W. Grimes 	NTOHS(ip->ip_len);
345df8bae1dSRodney W. Grimes 	if (ip->ip_len < hlen) {
346df8bae1dSRodney W. Grimes 		ipstat.ips_badlen++;
347df8bae1dSRodney W. Grimes 		goto bad;
348df8bae1dSRodney W. Grimes 	}
349df8bae1dSRodney W. Grimes 	NTOHS(ip->ip_off);
350df8bae1dSRodney W. Grimes 
351df8bae1dSRodney W. Grimes 	/*
352df8bae1dSRodney W. Grimes 	 * Check that the amount of data in the buffers
353df8bae1dSRodney W. Grimes 	 * is as at least much as the IP header would have us expect.
354df8bae1dSRodney W. Grimes 	 * Trim mbufs if longer than we expect.
355df8bae1dSRodney W. Grimes 	 * Drop packet if shorter than we expect.
356df8bae1dSRodney W. Grimes 	 */
357df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len < ip->ip_len) {
35858938916SGarrett Wollman tooshort:
359df8bae1dSRodney W. Grimes 		ipstat.ips_tooshort++;
360df8bae1dSRodney W. Grimes 		goto bad;
361df8bae1dSRodney W. Grimes 	}
362df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len > ip->ip_len) {
363df8bae1dSRodney W. Grimes 		if (m->m_len == m->m_pkthdr.len) {
364df8bae1dSRodney W. Grimes 			m->m_len = ip->ip_len;
365df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = ip->ip_len;
366df8bae1dSRodney W. Grimes 		} else
367df8bae1dSRodney W. Grimes 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
368df8bae1dSRodney W. Grimes 	}
3694dd1662bSUgen J.S. Antsilevich 	/*
3704dd1662bSUgen J.S. Antsilevich 	 * IpHack's section.
3714dd1662bSUgen J.S. Antsilevich 	 * Right now when no processing on packet has done
3724dd1662bSUgen J.S. Antsilevich 	 * and it is still fresh out of network we do our black
3734dd1662bSUgen J.S. Antsilevich 	 * deals with it.
37493e0e116SJulian Elischer 	 * - Firewall: deny/allow/divert
375fed1c7e9SSøren Schmidt 	 * - Xlate: translate packet's addr/port (NAT).
376b715f178SLuigi Rizzo 	 * - Pipe: pass pkt through dummynet.
3774dd1662bSUgen J.S. Antsilevich 	 * - Wrap: fake packet's addr/port <unimpl.>
3784dd1662bSUgen J.S. Antsilevich 	 * - Encapsulate: put it in another IP and send out. <unimp.>
3794dd1662bSUgen J.S. Antsilevich  	 */
380b715f178SLuigi Rizzo 
381dee383e0SEivind Eklund #if defined(IPFIREWALL) && defined(DUMMYNET)
382b715f178SLuigi Rizzo iphack:
383dee383e0SEivind Eklund #endif
384df8bae1dSRodney W. Grimes 
385c4ac87eaSDarren Reed #ifdef PFIL_HOOKS
386c4ac87eaSDarren Reed 	/*
387c4ac87eaSDarren Reed 	 * Run through list of hooks for input packets.  If there are any
388c4ac87eaSDarren Reed 	 * filters which require that additional packets in the flow are
389c4ac87eaSDarren Reed 	 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
390c4ac87eaSDarren Reed 	 * Note that filters must _never_ set this flag, as another filter
391c4ac87eaSDarren Reed 	 * in the list may have previously cleared it.
392c4ac87eaSDarren Reed 	 */
393c4ac87eaSDarren Reed 	m0 = m;
394c4ac87eaSDarren Reed 	pfh = pfil_hook_get(PFIL_IN, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
395c4ac87eaSDarren Reed 	for (; pfh; pfh = pfh->pfil_link.tqe_next)
396c4ac87eaSDarren Reed 		if (pfh->pfil_func) {
397c4ac87eaSDarren Reed 			rv = pfh->pfil_func(ip, hlen,
398c4ac87eaSDarren Reed 					    m->m_pkthdr.rcvif, 0, &m0);
399c4ac87eaSDarren Reed 			if (rv)
400beec8214SDarren Reed 				return;
401c4ac87eaSDarren Reed 			m = m0;
402c4ac87eaSDarren Reed 			if (m == NULL)
403c4ac87eaSDarren Reed 				return;
404c4ac87eaSDarren Reed 			ip = mtod(m, struct ip *);
405beec8214SDarren Reed 		}
406c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */
407c4ac87eaSDarren Reed 
4086bc748b0SLuigi Rizzo 	if (fw_enable && ip_fw_chk_ptr) {
409f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
410f9e354dfSJulian Elischer 		/*
411f9e354dfSJulian Elischer 		 * If we've been forwarded from the output side, then
412f9e354dfSJulian Elischer 		 * skip the firewall a second time
413f9e354dfSJulian Elischer 		 */
414f9e354dfSJulian Elischer 		if (ip_fw_fwd_addr)
415f9e354dfSJulian Elischer 			goto ours;
416f9e354dfSJulian Elischer #endif	/* IPFIREWALL_FORWARD */
417f9e354dfSJulian Elischer 		/*
4188948e4baSArchie Cobbs 		 * See the comment in ip_output for the return values
419b715f178SLuigi Rizzo 		 * produced by the firewall.
420f9e354dfSJulian Elischer 		 */
4218948e4baSArchie Cobbs 		i = (*ip_fw_chk_ptr)(&ip,
4228948e4baSArchie Cobbs 		    hlen, NULL, &divert_cookie, &m, &rule, &ip_fw_fwd_addr);
4238948e4baSArchie Cobbs 		if (m == NULL)		/* Packet discarded by firewall */
424b715f178SLuigi Rizzo 			return;
425b715f178SLuigi Rizzo 		if (i == 0 && ip_fw_fwd_addr == NULL)	/* common case */
426b715f178SLuigi Rizzo 			goto pass;
427b715f178SLuigi Rizzo #ifdef DUMMYNET
4288948e4baSArchie Cobbs                 if ((i & IP_FW_PORT_DYNT_FLAG) != 0) {
4298948e4baSArchie Cobbs                         /* Send packet to the appropriate pipe */
4306a800098SYoshinobu Inoue                         dummynet_io(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule,
4316a800098SYoshinobu Inoue 				    0);
432e4676ba6SJulian Elischer 			return;
43393e0e116SJulian Elischer 		}
434b715f178SLuigi Rizzo #endif
435b715f178SLuigi Rizzo #ifdef IPDIVERT
4368948e4baSArchie Cobbs 		if (i != 0 && (i & IP_FW_PORT_DYNT_FLAG) == 0) {
4378948e4baSArchie Cobbs 			/* Divert or tee packet */
4388948e4baSArchie Cobbs 			divert_info = i;
439b715f178SLuigi Rizzo 			goto ours;
440b715f178SLuigi Rizzo 		}
441b715f178SLuigi Rizzo #endif
442b715f178SLuigi Rizzo #ifdef IPFIREWALL_FORWARD
443b715f178SLuigi Rizzo 		if (i == 0 && ip_fw_fwd_addr != NULL)
444b715f178SLuigi Rizzo 			goto pass;
445b715f178SLuigi Rizzo #endif
446b715f178SLuigi Rizzo 		/*
447b715f178SLuigi Rizzo 		 * if we get here, the packet must be dropped
448b715f178SLuigi Rizzo 		 */
449b715f178SLuigi Rizzo 		m_freem(m);
450b715f178SLuigi Rizzo 		return;
451b715f178SLuigi Rizzo 	}
452b715f178SLuigi Rizzo pass:
453100ba1a6SJordan K. Hubbard 
454df8bae1dSRodney W. Grimes 	/*
455df8bae1dSRodney W. Grimes 	 * Process options and, if not destined for us,
456df8bae1dSRodney W. Grimes 	 * ship it on.  ip_dooptions returns 1 when an
457df8bae1dSRodney W. Grimes 	 * error was detected (causing an icmp message
458df8bae1dSRodney W. Grimes 	 * to be sent and the original packet to be freed).
459df8bae1dSRodney W. Grimes 	 */
460df8bae1dSRodney W. Grimes 	ip_nhops = 0;		/* for source routed packets */
461ed1ff184SJulian Elischer 	if (hlen > sizeof (struct ip) && ip_dooptions(m)) {
462ed1ff184SJulian Elischer #ifdef IPFIREWALL_FORWARD
463ed1ff184SJulian Elischer 		ip_fw_fwd_addr = NULL;
464ed1ff184SJulian Elischer #endif
465c67b1d17SGarrett Wollman 		return;
466ed1ff184SJulian Elischer 	}
467df8bae1dSRodney W. Grimes 
468f0068c4aSGarrett Wollman         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
469f0068c4aSGarrett Wollman          * matter if it is destined to another node, or whether it is
470f0068c4aSGarrett Wollman          * a multicast one, RSVP wants it! and prevents it from being forwarded
471f0068c4aSGarrett Wollman          * anywhere else. Also checks if the rsvp daemon is running before
472f0068c4aSGarrett Wollman 	 * grabbing the packet.
473f0068c4aSGarrett Wollman          */
4741c5de19aSGarrett Wollman 	if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
475f0068c4aSGarrett Wollman 		goto ours;
476f0068c4aSGarrett Wollman 
477df8bae1dSRodney W. Grimes 	/*
478df8bae1dSRodney W. Grimes 	 * Check our list of addresses, to see if the packet is for us.
479cc766e04SGarrett Wollman 	 * If we don't have any addresses, assume any unicast packet
480cc766e04SGarrett Wollman 	 * we receive might be for us (and let the upper layers deal
481cc766e04SGarrett Wollman 	 * with it).
482df8bae1dSRodney W. Grimes 	 */
483cc766e04SGarrett Wollman 	if (TAILQ_EMPTY(&in_ifaddrhead) &&
484cc766e04SGarrett Wollman 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
485cc766e04SGarrett Wollman 		goto ours;
486cc766e04SGarrett Wollman 
487d4295c32SJulian Elischer 	for (ia = TAILQ_FIRST(&in_ifaddrhead); ia;
488f9e354dfSJulian Elischer 					ia = TAILQ_NEXT(ia, ia_link)) {
489df8bae1dSRodney W. Grimes #define	satosin(sa)	((struct sockaddr_in *)(sa))
490df8bae1dSRodney W. Grimes 
491432aad0eSTor Egge #ifdef BOOTP_COMPAT
492432aad0eSTor Egge 		if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
493432aad0eSTor Egge 			goto ours;
494432aad0eSTor Egge #endif
495f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
496f9e354dfSJulian Elischer 		/*
497f9e354dfSJulian Elischer 		 * If the addr to forward to is one of ours, we pretend to
498f9e354dfSJulian Elischer 		 * be the destination for this packet.
499f9e354dfSJulian Elischer 		 */
500ed1ff184SJulian Elischer 		if (ip_fw_fwd_addr == NULL) {
501ed1ff184SJulian Elischer 			if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
502ed1ff184SJulian Elischer 				goto ours;
503ed1ff184SJulian Elischer 		} else if (IA_SIN(ia)->sin_addr.s_addr ==
504f9e354dfSJulian Elischer 					 ip_fw_fwd_addr->sin_addr.s_addr)
505f9e354dfSJulian Elischer 			goto ours;
506ed1ff184SJulian Elischer #else
507ed1ff184SJulian Elischer 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
508ed1ff184SJulian Elischer 			goto ours;
509f9e354dfSJulian Elischer #endif
5106ed666afSPoul-Henning Kamp 		if (ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) {
511df8bae1dSRodney W. Grimes 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
512df8bae1dSRodney W. Grimes 			    ip->ip_dst.s_addr)
513df8bae1dSRodney W. Grimes 				goto ours;
514df8bae1dSRodney W. Grimes 			if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
515df8bae1dSRodney W. Grimes 				goto ours;
516df8bae1dSRodney W. Grimes 		}
517df8bae1dSRodney W. Grimes 	}
518df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
519df8bae1dSRodney W. Grimes 		struct in_multi *inm;
520df8bae1dSRodney W. Grimes 		if (ip_mrouter) {
521df8bae1dSRodney W. Grimes 			/*
522df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, all
523df8bae1dSRodney W. Grimes 			 * incoming multicast packets are passed to the
524df8bae1dSRodney W. Grimes 			 * kernel-level multicast forwarding function.
525df8bae1dSRodney W. Grimes 			 * The packet is returned (relatively) intact; if
526df8bae1dSRodney W. Grimes 			 * ip_mforward() returns a non-zero value, the packet
527df8bae1dSRodney W. Grimes 			 * must be discarded, else it may be accepted below.
528df8bae1dSRodney W. Grimes 			 */
529f0068c4aSGarrett Wollman 			if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
530df8bae1dSRodney W. Grimes 				ipstat.ips_cantforward++;
531df8bae1dSRodney W. Grimes 				m_freem(m);
532c67b1d17SGarrett Wollman 				return;
533df8bae1dSRodney W. Grimes 			}
534df8bae1dSRodney W. Grimes 
535df8bae1dSRodney W. Grimes 			/*
536df8bae1dSRodney W. Grimes 			 * The process-level routing demon needs to receive
537df8bae1dSRodney W. Grimes 			 * all multicast IGMP packets, whether or not this
538df8bae1dSRodney W. Grimes 			 * host belongs to their destination groups.
539df8bae1dSRodney W. Grimes 			 */
540df8bae1dSRodney W. Grimes 			if (ip->ip_p == IPPROTO_IGMP)
541df8bae1dSRodney W. Grimes 				goto ours;
542df8bae1dSRodney W. Grimes 			ipstat.ips_forward++;
543df8bae1dSRodney W. Grimes 		}
544df8bae1dSRodney W. Grimes 		/*
545df8bae1dSRodney W. Grimes 		 * See if we belong to the destination multicast group on the
546df8bae1dSRodney W. Grimes 		 * arrival interface.
547df8bae1dSRodney W. Grimes 		 */
548df8bae1dSRodney W. Grimes 		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
549df8bae1dSRodney W. Grimes 		if (inm == NULL) {
55082c39223SGarrett Wollman 			ipstat.ips_notmember++;
551df8bae1dSRodney W. Grimes 			m_freem(m);
552c67b1d17SGarrett Wollman 			return;
553df8bae1dSRodney W. Grimes 		}
554df8bae1dSRodney W. Grimes 		goto ours;
555df8bae1dSRodney W. Grimes 	}
556df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
557df8bae1dSRodney W. Grimes 		goto ours;
558df8bae1dSRodney W. Grimes 	if (ip->ip_dst.s_addr == INADDR_ANY)
559df8bae1dSRodney W. Grimes 		goto ours;
560df8bae1dSRodney W. Grimes 
5616a800098SYoshinobu Inoue #if defined(NFAITH) && 0 < NFAITH
5626a800098SYoshinobu Inoue 	/*
5636a800098SYoshinobu Inoue 	 * FAITH(Firewall Aided Internet Translator)
5646a800098SYoshinobu Inoue 	 */
5656a800098SYoshinobu Inoue 	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
5666a800098SYoshinobu Inoue 		if (ip_keepfaith) {
5676a800098SYoshinobu Inoue 			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
5686a800098SYoshinobu Inoue 				goto ours;
5696a800098SYoshinobu Inoue 		}
5706a800098SYoshinobu Inoue 		m_freem(m);
5716a800098SYoshinobu Inoue 		return;
5726a800098SYoshinobu Inoue 	}
5736a800098SYoshinobu Inoue #endif
574df8bae1dSRodney W. Grimes 	/*
575df8bae1dSRodney W. Grimes 	 * Not for us; forward if possible and desirable.
576df8bae1dSRodney W. Grimes 	 */
577df8bae1dSRodney W. Grimes 	if (ipforwarding == 0) {
578df8bae1dSRodney W. Grimes 		ipstat.ips_cantforward++;
579df8bae1dSRodney W. Grimes 		m_freem(m);
580df8bae1dSRodney W. Grimes 	} else
581df8bae1dSRodney W. Grimes 		ip_forward(m, 0);
582ed1ff184SJulian Elischer #ifdef IPFIREWALL_FORWARD
583ed1ff184SJulian Elischer 	ip_fw_fwd_addr = NULL;
584ed1ff184SJulian Elischer #endif
585c67b1d17SGarrett Wollman 	return;
586df8bae1dSRodney W. Grimes 
587df8bae1dSRodney W. Grimes ours:
5885da9f8faSJosef Karthauser 	/* Count the packet in the ip address stats */
5895da9f8faSJosef Karthauser 	if (ia != NULL) {
5905da9f8faSJosef Karthauser 		ia->ia_ifa.if_ipackets++;
5915da9f8faSJosef Karthauser 		ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
5925da9f8faSJosef Karthauser 	}
593100ba1a6SJordan K. Hubbard 
59463f8d699SJordan K. Hubbard 	/*
595df8bae1dSRodney W. Grimes 	 * If offset or IP_MF are set, must reassemble.
596df8bae1dSRodney W. Grimes 	 * Otherwise, nothing need be done.
597df8bae1dSRodney W. Grimes 	 * (We could look in the reassembly queue to see
598df8bae1dSRodney W. Grimes 	 * if the packet was previously fragmented,
599df8bae1dSRodney W. Grimes 	 * but it's not worth the time; just let them time out.)
600df8bae1dSRodney W. Grimes 	 */
601b6ea1aa5SRuslan Ermilov 	if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
6026a800098SYoshinobu Inoue 
603194a213eSAndrey A. Chernov 		sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
604df8bae1dSRodney W. Grimes 		/*
605df8bae1dSRodney W. Grimes 		 * Look for queue of fragments
606df8bae1dSRodney W. Grimes 		 * of this datagram.
607df8bae1dSRodney W. Grimes 		 */
608194a213eSAndrey A. Chernov 		for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next)
609df8bae1dSRodney W. Grimes 			if (ip->ip_id == fp->ipq_id &&
610df8bae1dSRodney W. Grimes 			    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
611df8bae1dSRodney W. Grimes 			    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
612df8bae1dSRodney W. Grimes 			    ip->ip_p == fp->ipq_p)
613df8bae1dSRodney W. Grimes 				goto found;
614df8bae1dSRodney W. Grimes 
615194a213eSAndrey A. Chernov 		fp = 0;
616194a213eSAndrey A. Chernov 
617194a213eSAndrey A. Chernov 		/* check if there's a place for the new queue */
618194a213eSAndrey A. Chernov 		if (nipq > maxnipq) {
619194a213eSAndrey A. Chernov 		    /*
620194a213eSAndrey A. Chernov 		     * drop something from the tail of the current queue
621194a213eSAndrey A. Chernov 		     * before proceeding further
622194a213eSAndrey A. Chernov 		     */
623194a213eSAndrey A. Chernov 		    if (ipq[sum].prev == &ipq[sum]) {   /* gak */
624194a213eSAndrey A. Chernov 			for (i = 0; i < IPREASS_NHASH; i++) {
625194a213eSAndrey A. Chernov 			    if (ipq[i].prev != &ipq[i]) {
626194a213eSAndrey A. Chernov 				ip_freef(ipq[i].prev);
627194a213eSAndrey A. Chernov 				break;
628194a213eSAndrey A. Chernov 			    }
629194a213eSAndrey A. Chernov 			}
630194a213eSAndrey A. Chernov 		    } else
631194a213eSAndrey A. Chernov 			ip_freef(ipq[sum].prev);
632194a213eSAndrey A. Chernov 		}
633194a213eSAndrey A. Chernov found:
634df8bae1dSRodney W. Grimes 		/*
635df8bae1dSRodney W. Grimes 		 * Adjust ip_len to not reflect header,
636df8bae1dSRodney W. Grimes 		 * convert offset of this to bytes.
637df8bae1dSRodney W. Grimes 		 */
638df8bae1dSRodney W. Grimes 		ip->ip_len -= hlen;
639b6ea1aa5SRuslan Ermilov 		if (ip->ip_off & IP_MF) {
6406effc713SDoug Rabson 		        /*
6416effc713SDoug Rabson 		         * Make sure that fragments have a data length
6426effc713SDoug Rabson 			 * that's a non-zero multiple of 8 bytes.
6436effc713SDoug Rabson 		         */
6446effc713SDoug Rabson 			if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
6456effc713SDoug Rabson 				ipstat.ips_toosmall++; /* XXX */
6466effc713SDoug Rabson 				goto bad;
6476effc713SDoug Rabson 			}
6486effc713SDoug Rabson 			m->m_flags |= M_FRAG;
6496effc713SDoug Rabson 		}
650df8bae1dSRodney W. Grimes 		ip->ip_off <<= 3;
651df8bae1dSRodney W. Grimes 
652df8bae1dSRodney W. Grimes 		/*
653b6ea1aa5SRuslan Ermilov 		 * Attempt reassembly; if it succeeds, proceed.
654df8bae1dSRodney W. Grimes 		 */
655df8bae1dSRodney W. Grimes 		ipstat.ips_fragments++;
656487bdb38SRuslan Ermilov 		m->m_pkthdr.header = ip;
6578948e4baSArchie Cobbs #ifdef IPDIVERT
6586a800098SYoshinobu Inoue 		m = ip_reass(m,
6598948e4baSArchie Cobbs 		    fp, &ipq[sum], &divert_info, &divert_cookie);
6608948e4baSArchie Cobbs #else
6616a800098SYoshinobu Inoue 		m = ip_reass(m, fp, &ipq[sum]);
6628948e4baSArchie Cobbs #endif
6636a800098SYoshinobu Inoue 		if (m == 0) {
664f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
665f9e354dfSJulian Elischer 			ip_fw_fwd_addr = NULL;
666f9e354dfSJulian Elischer #endif
667c67b1d17SGarrett Wollman 			return;
668f9e354dfSJulian Elischer 		}
669df8bae1dSRodney W. Grimes 		ipstat.ips_reassembled++;
6706a800098SYoshinobu Inoue 		ip = mtod(m, struct ip *);
6717e2df452SRuslan Ermilov 		/* Get the header length of the reassembled packet */
6727e2df452SRuslan Ermilov 		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
673af782f1cSBrian Somers #ifdef IPDIVERT
6748948e4baSArchie Cobbs 		/* Restore original checksum before diverting packet */
6758948e4baSArchie Cobbs 		if (divert_info != 0) {
676af782f1cSBrian Somers 			ip->ip_len += hlen;
677af782f1cSBrian Somers 			HTONS(ip->ip_len);
678af782f1cSBrian Somers 			HTONS(ip->ip_off);
679af782f1cSBrian Somers 			ip->ip_sum = 0;
680af782f1cSBrian Somers 			ip->ip_sum = in_cksum_hdr(ip);
681af782f1cSBrian Somers 			NTOHS(ip->ip_off);
682af782f1cSBrian Somers 			NTOHS(ip->ip_len);
683af782f1cSBrian Somers 			ip->ip_len -= hlen;
684af782f1cSBrian Somers 		}
685af782f1cSBrian Somers #endif
686df8bae1dSRodney W. Grimes 	} else
687df8bae1dSRodney W. Grimes 		ip->ip_len -= hlen;
688df8bae1dSRodney W. Grimes 
68993e0e116SJulian Elischer #ifdef IPDIVERT
69093e0e116SJulian Elischer 	/*
6918948e4baSArchie Cobbs 	 * Divert or tee packet to the divert protocol if required.
6928948e4baSArchie Cobbs 	 *
6938948e4baSArchie Cobbs 	 * If divert_info is zero then cookie should be too, so we shouldn't
6948948e4baSArchie Cobbs 	 * need to clear them here.  Assume divert_packet() does so also.
69593e0e116SJulian Elischer 	 */
6968948e4baSArchie Cobbs 	if (divert_info != 0) {
6978948e4baSArchie Cobbs 		struct mbuf *clone = NULL;
6988948e4baSArchie Cobbs 
6998948e4baSArchie Cobbs 		/* Clone packet if we're doing a 'tee' */
7008948e4baSArchie Cobbs 		if ((divert_info & IP_FW_PORT_TEE_FLAG) != 0)
7018948e4baSArchie Cobbs 			clone = m_dup(m, M_DONTWAIT);
7028948e4baSArchie Cobbs 
7038948e4baSArchie Cobbs 		/* Restore packet header fields to original values */
7048948e4baSArchie Cobbs 		ip->ip_len += hlen;
7058948e4baSArchie Cobbs 		HTONS(ip->ip_len);
7068948e4baSArchie Cobbs 		HTONS(ip->ip_off);
7078948e4baSArchie Cobbs 
7088948e4baSArchie Cobbs 		/* Deliver packet to divert input routine */
7098948e4baSArchie Cobbs 		ip_divert_cookie = divert_cookie;
7108948e4baSArchie Cobbs 		divert_packet(m, 1, divert_info & 0xffff);
711e4676ba6SJulian Elischer 		ipstat.ips_delivered++;
7128948e4baSArchie Cobbs 
7138948e4baSArchie Cobbs 		/* If 'tee', continue with original packet */
7148948e4baSArchie Cobbs 		if (clone == NULL)
71593e0e116SJulian Elischer 			return;
7168948e4baSArchie Cobbs 		m = clone;
7178948e4baSArchie Cobbs 		ip = mtod(m, struct ip *);
71893e0e116SJulian Elischer 	}
71993e0e116SJulian Elischer #endif
72093e0e116SJulian Elischer 
721df8bae1dSRodney W. Grimes 	/*
722df8bae1dSRodney W. Grimes 	 * Switch out to protocol's input routine.
723df8bae1dSRodney W. Grimes 	 */
724df8bae1dSRodney W. Grimes 	ipstat.ips_delivered++;
7256a800098SYoshinobu Inoue     {
7266a800098SYoshinobu Inoue 	int off = hlen, nh = ip->ip_p;
7276a800098SYoshinobu Inoue 
7286a800098SYoshinobu Inoue 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, off, nh);
729f9e354dfSJulian Elischer #ifdef	IPFIREWALL_FORWARD
730f9e354dfSJulian Elischer 	ip_fw_fwd_addr = NULL;	/* tcp needed it */
731f9e354dfSJulian Elischer #endif
732c67b1d17SGarrett Wollman 	return;
7336a800098SYoshinobu Inoue     }
734df8bae1dSRodney W. Grimes bad:
735f9e354dfSJulian Elischer #ifdef	IPFIREWALL_FORWARD
736f9e354dfSJulian Elischer 	ip_fw_fwd_addr = NULL;
737f9e354dfSJulian Elischer #endif
738df8bae1dSRodney W. Grimes 	m_freem(m);
739c67b1d17SGarrett Wollman }
740c67b1d17SGarrett Wollman 
741c67b1d17SGarrett Wollman /*
742c67b1d17SGarrett Wollman  * IP software interrupt routine - to go away sometime soon
743c67b1d17SGarrett Wollman  */
744c67b1d17SGarrett Wollman static void
745c67b1d17SGarrett Wollman ipintr(void)
746c67b1d17SGarrett Wollman {
747c67b1d17SGarrett Wollman 	int s;
748c67b1d17SGarrett Wollman 	struct mbuf *m;
749c67b1d17SGarrett Wollman 
750c67b1d17SGarrett Wollman 	while(1) {
751c67b1d17SGarrett Wollman 		s = splimp();
752c67b1d17SGarrett Wollman 		IF_DEQUEUE(&ipintrq, m);
753c67b1d17SGarrett Wollman 		splx(s);
754c67b1d17SGarrett Wollman 		if (m == 0)
755c67b1d17SGarrett Wollman 			return;
756c67b1d17SGarrett Wollman 		ip_input(m);
757c67b1d17SGarrett Wollman 	}
758df8bae1dSRodney W. Grimes }
759df8bae1dSRodney W. Grimes 
760df8bae1dSRodney W. Grimes /*
7618948e4baSArchie Cobbs  * Take incoming datagram fragment and try to reassemble it into
7628948e4baSArchie Cobbs  * whole datagram.  If a chain for reassembly of this datagram already
7638948e4baSArchie Cobbs  * exists, then it is given as fp; otherwise have to make a chain.
7648948e4baSArchie Cobbs  *
7658948e4baSArchie Cobbs  * When IPDIVERT enabled, keep additional state with each packet that
7668948e4baSArchie Cobbs  * tells us if we need to divert or tee the packet we're building.
767df8bae1dSRodney W. Grimes  */
7688948e4baSArchie Cobbs 
7696a800098SYoshinobu Inoue static struct mbuf *
7708948e4baSArchie Cobbs #ifdef IPDIVERT
7718948e4baSArchie Cobbs ip_reass(m, fp, where, divinfo, divcookie)
7728948e4baSArchie Cobbs #else
7736effc713SDoug Rabson ip_reass(m, fp, where)
7748948e4baSArchie Cobbs #endif
7756effc713SDoug Rabson 	register struct mbuf *m;
776df8bae1dSRodney W. Grimes 	register struct ipq *fp;
777194a213eSAndrey A. Chernov 	struct   ipq    *where;
7788948e4baSArchie Cobbs #ifdef IPDIVERT
7798948e4baSArchie Cobbs 	u_int32_t *divinfo;
7808948e4baSArchie Cobbs 	u_int16_t *divcookie;
7818948e4baSArchie Cobbs #endif
782df8bae1dSRodney W. Grimes {
7836effc713SDoug Rabson 	struct ip *ip = mtod(m, struct ip *);
784b6ea1aa5SRuslan Ermilov 	register struct mbuf *p, *q, *nq;
785df8bae1dSRodney W. Grimes 	struct mbuf *t;
7866effc713SDoug Rabson 	int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
787df8bae1dSRodney W. Grimes 	int i, next;
788df8bae1dSRodney W. Grimes 
789df8bae1dSRodney W. Grimes 	/*
790df8bae1dSRodney W. Grimes 	 * Presence of header sizes in mbufs
791df8bae1dSRodney W. Grimes 	 * would confuse code below.
792df8bae1dSRodney W. Grimes 	 */
793df8bae1dSRodney W. Grimes 	m->m_data += hlen;
794df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
795df8bae1dSRodney W. Grimes 
796df8bae1dSRodney W. Grimes 	/*
797df8bae1dSRodney W. Grimes 	 * If first fragment to arrive, create a reassembly queue.
798df8bae1dSRodney W. Grimes 	 */
799df8bae1dSRodney W. Grimes 	if (fp == 0) {
800df8bae1dSRodney W. Grimes 		if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
801df8bae1dSRodney W. Grimes 			goto dropfrag;
802df8bae1dSRodney W. Grimes 		fp = mtod(t, struct ipq *);
803194a213eSAndrey A. Chernov 		insque(fp, where);
804194a213eSAndrey A. Chernov 		nipq++;
805df8bae1dSRodney W. Grimes 		fp->ipq_ttl = IPFRAGTTL;
806df8bae1dSRodney W. Grimes 		fp->ipq_p = ip->ip_p;
807df8bae1dSRodney W. Grimes 		fp->ipq_id = ip->ip_id;
8086effc713SDoug Rabson 		fp->ipq_src = ip->ip_src;
8096effc713SDoug Rabson 		fp->ipq_dst = ip->ip_dst;
810af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
811af38c68cSLuigi Rizzo 		m->m_nextpkt = NULL;
81293e0e116SJulian Elischer #ifdef IPDIVERT
8138948e4baSArchie Cobbs 		fp->ipq_div_info = 0;
814bb60f459SJulian Elischer 		fp->ipq_div_cookie = 0;
81593e0e116SJulian Elischer #endif
816af38c68cSLuigi Rizzo 		goto inserted;
817df8bae1dSRodney W. Grimes 	}
818df8bae1dSRodney W. Grimes 
8196effc713SDoug Rabson #define GETIP(m)	((struct ip*)((m)->m_pkthdr.header))
8206effc713SDoug Rabson 
821df8bae1dSRodney W. Grimes 	/*
822df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
823df8bae1dSRodney W. Grimes 	 */
8246effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
8256effc713SDoug Rabson 		if (GETIP(q)->ip_off > ip->ip_off)
826df8bae1dSRodney W. Grimes 			break;
827df8bae1dSRodney W. Grimes 
828df8bae1dSRodney W. Grimes 	/*
829df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
830df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
831af38c68cSLuigi Rizzo 	 * segment.  If it provides all of our data, drop us, otherwise
832af38c68cSLuigi Rizzo 	 * stick new segment in the proper place.
833db4f9cc7SJonathan Lemon 	 *
834db4f9cc7SJonathan Lemon 	 * If some of the data is dropped from the the preceding
835db4f9cc7SJonathan Lemon 	 * segment, then it's checksum is invalidated.
836df8bae1dSRodney W. Grimes 	 */
8376effc713SDoug Rabson 	if (p) {
8386effc713SDoug Rabson 		i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
839df8bae1dSRodney W. Grimes 		if (i > 0) {
840df8bae1dSRodney W. Grimes 			if (i >= ip->ip_len)
841df8bae1dSRodney W. Grimes 				goto dropfrag;
8426a800098SYoshinobu Inoue 			m_adj(m, i);
843db4f9cc7SJonathan Lemon 			m->m_pkthdr.csum_flags = 0;
844df8bae1dSRodney W. Grimes 			ip->ip_off += i;
845df8bae1dSRodney W. Grimes 			ip->ip_len -= i;
846df8bae1dSRodney W. Grimes 		}
847af38c68cSLuigi Rizzo 		m->m_nextpkt = p->m_nextpkt;
848af38c68cSLuigi Rizzo 		p->m_nextpkt = m;
849af38c68cSLuigi Rizzo 	} else {
850af38c68cSLuigi Rizzo 		m->m_nextpkt = fp->ipq_frags;
851af38c68cSLuigi Rizzo 		fp->ipq_frags = m;
852df8bae1dSRodney W. Grimes 	}
853df8bae1dSRodney W. Grimes 
854df8bae1dSRodney W. Grimes 	/*
855df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
856df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
857df8bae1dSRodney W. Grimes 	 */
8586effc713SDoug Rabson 	for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
859af38c68cSLuigi Rizzo 	     q = nq) {
8606effc713SDoug Rabson 		i = (ip->ip_off + ip->ip_len) -
8616effc713SDoug Rabson 		    GETIP(q)->ip_off;
8626effc713SDoug Rabson 		if (i < GETIP(q)->ip_len) {
8636effc713SDoug Rabson 			GETIP(q)->ip_len -= i;
8646effc713SDoug Rabson 			GETIP(q)->ip_off += i;
8656effc713SDoug Rabson 			m_adj(q, i);
866db4f9cc7SJonathan Lemon 			q->m_pkthdr.csum_flags = 0;
867df8bae1dSRodney W. Grimes 			break;
868df8bae1dSRodney W. Grimes 		}
8696effc713SDoug Rabson 		nq = q->m_nextpkt;
870af38c68cSLuigi Rizzo 		m->m_nextpkt = nq;
8716effc713SDoug Rabson 		m_freem(q);
872df8bae1dSRodney W. Grimes 	}
873df8bae1dSRodney W. Grimes 
874af38c68cSLuigi Rizzo inserted:
87593e0e116SJulian Elischer 
87693e0e116SJulian Elischer #ifdef IPDIVERT
87793e0e116SJulian Elischer 	/*
8788948e4baSArchie Cobbs 	 * Transfer firewall instructions to the fragment structure.
8798948e4baSArchie Cobbs 	 * Any fragment diverting causes the whole packet to divert.
88093e0e116SJulian Elischer 	 */
8818948e4baSArchie Cobbs 	fp->ipq_div_info = *divinfo;
8828948e4baSArchie Cobbs 	fp->ipq_div_cookie = *divcookie;
8838948e4baSArchie Cobbs 	*divinfo = 0;
8848948e4baSArchie Cobbs 	*divcookie = 0;
88593e0e116SJulian Elischer #endif
88693e0e116SJulian Elischer 
887df8bae1dSRodney W. Grimes 	/*
888af38c68cSLuigi Rizzo 	 * Check for complete reassembly.
889df8bae1dSRodney W. Grimes 	 */
8906effc713SDoug Rabson 	next = 0;
8916effc713SDoug Rabson 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
8926effc713SDoug Rabson 		if (GETIP(q)->ip_off != next)
8936effc713SDoug Rabson 			return (0);
8946effc713SDoug Rabson 		next += GETIP(q)->ip_len;
8956effc713SDoug Rabson 	}
8966effc713SDoug Rabson 	/* Make sure the last packet didn't have the IP_MF flag */
8976effc713SDoug Rabson 	if (p->m_flags & M_FRAG)
898df8bae1dSRodney W. Grimes 		return (0);
899df8bae1dSRodney W. Grimes 
900df8bae1dSRodney W. Grimes 	/*
901430d30d8SBill Fenner 	 * Reassembly is complete.  Make sure the packet is a sane size.
902430d30d8SBill Fenner 	 */
9036effc713SDoug Rabson 	q = fp->ipq_frags;
9046effc713SDoug Rabson 	ip = GETIP(q);
9056effc713SDoug Rabson 	if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) {
906430d30d8SBill Fenner 		ipstat.ips_toolong++;
907430d30d8SBill Fenner 		ip_freef(fp);
908430d30d8SBill Fenner 		return (0);
909430d30d8SBill Fenner 	}
910430d30d8SBill Fenner 
911430d30d8SBill Fenner 	/*
912430d30d8SBill Fenner 	 * Concatenate fragments.
913df8bae1dSRodney W. Grimes 	 */
9146effc713SDoug Rabson 	m = q;
915df8bae1dSRodney W. Grimes 	t = m->m_next;
916df8bae1dSRodney W. Grimes 	m->m_next = 0;
917df8bae1dSRodney W. Grimes 	m_cat(m, t);
9186effc713SDoug Rabson 	nq = q->m_nextpkt;
919945aa40dSDoug Rabson 	q->m_nextpkt = 0;
9206effc713SDoug Rabson 	for (q = nq; q != NULL; q = nq) {
9216effc713SDoug Rabson 		nq = q->m_nextpkt;
922945aa40dSDoug Rabson 		q->m_nextpkt = NULL;
923db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
924db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
925a8db1d93SJonathan Lemon 		m_cat(m, q);
926df8bae1dSRodney W. Grimes 	}
927df8bae1dSRodney W. Grimes 
92893e0e116SJulian Elischer #ifdef IPDIVERT
92993e0e116SJulian Elischer 	/*
9308948e4baSArchie Cobbs 	 * Extract firewall instructions from the fragment structure.
93193e0e116SJulian Elischer 	 */
9328948e4baSArchie Cobbs 	*divinfo = fp->ipq_div_info;
9338948e4baSArchie Cobbs 	*divcookie = fp->ipq_div_cookie;
93493e0e116SJulian Elischer #endif
93593e0e116SJulian Elischer 
936df8bae1dSRodney W. Grimes 	/*
937df8bae1dSRodney W. Grimes 	 * Create header for new ip packet by
938df8bae1dSRodney W. Grimes 	 * modifying header of first packet;
939df8bae1dSRodney W. Grimes 	 * dequeue and discard fragment reassembly header.
940df8bae1dSRodney W. Grimes 	 * Make header visible.
941df8bae1dSRodney W. Grimes 	 */
942df8bae1dSRodney W. Grimes 	ip->ip_len = next;
9436effc713SDoug Rabson 	ip->ip_src = fp->ipq_src;
9446effc713SDoug Rabson 	ip->ip_dst = fp->ipq_dst;
945df8bae1dSRodney W. Grimes 	remque(fp);
946194a213eSAndrey A. Chernov 	nipq--;
947df8bae1dSRodney W. Grimes 	(void) m_free(dtom(fp));
9486effc713SDoug Rabson 	m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2);
9496effc713SDoug Rabson 	m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2);
950df8bae1dSRodney W. Grimes 	/* some debugging cruft by sklower, below, will go away soon */
951df8bae1dSRodney W. Grimes 	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
952df8bae1dSRodney W. Grimes 		register int plen = 0;
9536a800098SYoshinobu Inoue 		for (t = m; t; t = t->m_next)
9546a800098SYoshinobu Inoue 			plen += t->m_len;
9556a800098SYoshinobu Inoue 		m->m_pkthdr.len = plen;
956df8bae1dSRodney W. Grimes 	}
9576a800098SYoshinobu Inoue 	return (m);
958df8bae1dSRodney W. Grimes 
959df8bae1dSRodney W. Grimes dropfrag:
960efe39c6aSJulian Elischer #ifdef IPDIVERT
9618948e4baSArchie Cobbs 	*divinfo = 0;
9628948e4baSArchie Cobbs 	*divcookie = 0;
963efe39c6aSJulian Elischer #endif
964df8bae1dSRodney W. Grimes 	ipstat.ips_fragdropped++;
965df8bae1dSRodney W. Grimes 	m_freem(m);
966df8bae1dSRodney W. Grimes 	return (0);
9676effc713SDoug Rabson 
9686effc713SDoug Rabson #undef GETIP
969df8bae1dSRodney W. Grimes }
970df8bae1dSRodney W. Grimes 
971df8bae1dSRodney W. Grimes /*
972df8bae1dSRodney W. Grimes  * Free a fragment reassembly header and all
973df8bae1dSRodney W. Grimes  * associated datagrams.
974df8bae1dSRodney W. Grimes  */
9750312fbe9SPoul-Henning Kamp static void
976df8bae1dSRodney W. Grimes ip_freef(fp)
977df8bae1dSRodney W. Grimes 	struct ipq *fp;
978df8bae1dSRodney W. Grimes {
9796effc713SDoug Rabson 	register struct mbuf *q;
980df8bae1dSRodney W. Grimes 
9816effc713SDoug Rabson 	while (fp->ipq_frags) {
9826effc713SDoug Rabson 		q = fp->ipq_frags;
9836effc713SDoug Rabson 		fp->ipq_frags = q->m_nextpkt;
9846effc713SDoug Rabson 		m_freem(q);
985df8bae1dSRodney W. Grimes 	}
986df8bae1dSRodney W. Grimes 	remque(fp);
987df8bae1dSRodney W. Grimes 	(void) m_free(dtom(fp));
988194a213eSAndrey A. Chernov 	nipq--;
989df8bae1dSRodney W. Grimes }
990df8bae1dSRodney W. Grimes 
991df8bae1dSRodney W. Grimes /*
992df8bae1dSRodney W. Grimes  * IP timer processing;
993df8bae1dSRodney W. Grimes  * if a timer expires on a reassembly
994df8bae1dSRodney W. Grimes  * queue, discard it.
995df8bae1dSRodney W. Grimes  */
996df8bae1dSRodney W. Grimes void
997df8bae1dSRodney W. Grimes ip_slowtimo()
998df8bae1dSRodney W. Grimes {
999df8bae1dSRodney W. Grimes 	register struct ipq *fp;
1000df8bae1dSRodney W. Grimes 	int s = splnet();
1001194a213eSAndrey A. Chernov 	int i;
1002df8bae1dSRodney W. Grimes 
1003194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1004194a213eSAndrey A. Chernov 		fp = ipq[i].next;
1005194a213eSAndrey A. Chernov 		if (fp == 0)
1006194a213eSAndrey A. Chernov 			continue;
1007194a213eSAndrey A. Chernov 		while (fp != &ipq[i]) {
1008df8bae1dSRodney W. Grimes 			--fp->ipq_ttl;
1009df8bae1dSRodney W. Grimes 			fp = fp->next;
1010df8bae1dSRodney W. Grimes 			if (fp->prev->ipq_ttl == 0) {
1011df8bae1dSRodney W. Grimes 				ipstat.ips_fragtimeout++;
1012df8bae1dSRodney W. Grimes 				ip_freef(fp->prev);
1013df8bae1dSRodney W. Grimes 			}
1014df8bae1dSRodney W. Grimes 		}
1015194a213eSAndrey A. Chernov 	}
10161f91d8c5SDavid Greenman 	ipflow_slowtimo();
1017df8bae1dSRodney W. Grimes 	splx(s);
1018df8bae1dSRodney W. Grimes }
1019df8bae1dSRodney W. Grimes 
1020df8bae1dSRodney W. Grimes /*
1021df8bae1dSRodney W. Grimes  * Drain off all datagram fragments.
1022df8bae1dSRodney W. Grimes  */
1023df8bae1dSRodney W. Grimes void
1024df8bae1dSRodney W. Grimes ip_drain()
1025df8bae1dSRodney W. Grimes {
1026194a213eSAndrey A. Chernov 	int     i;
1027ce29ab3aSGarrett Wollman 
1028194a213eSAndrey A. Chernov 	for (i = 0; i < IPREASS_NHASH; i++) {
1029194a213eSAndrey A. Chernov 		while (ipq[i].next != &ipq[i]) {
1030194a213eSAndrey A. Chernov 			ipstat.ips_fragdropped++;
1031194a213eSAndrey A. Chernov 			ip_freef(ipq[i].next);
1032194a213eSAndrey A. Chernov 		}
1033194a213eSAndrey A. Chernov 	}
1034ce29ab3aSGarrett Wollman 	in_rtqdrain();
1035df8bae1dSRodney W. Grimes }
1036df8bae1dSRodney W. Grimes 
1037df8bae1dSRodney W. Grimes /*
1038df8bae1dSRodney W. Grimes  * Do option processing on a datagram,
1039df8bae1dSRodney W. Grimes  * possibly discarding it if bad options are encountered,
1040df8bae1dSRodney W. Grimes  * or forwarding it if source-routed.
1041df8bae1dSRodney W. Grimes  * Returns 1 if packet has been forwarded/freed,
1042df8bae1dSRodney W. Grimes  * 0 if the packet should be processed further.
1043df8bae1dSRodney W. Grimes  */
10440312fbe9SPoul-Henning Kamp static int
1045df8bae1dSRodney W. Grimes ip_dooptions(m)
1046df8bae1dSRodney W. Grimes 	struct mbuf *m;
1047df8bae1dSRodney W. Grimes {
1048df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
1049df8bae1dSRodney W. Grimes 	register u_char *cp;
1050df8bae1dSRodney W. Grimes 	register struct ip_timestamp *ipt;
1051df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia;
1052df8bae1dSRodney W. Grimes 	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1053df8bae1dSRodney W. Grimes 	struct in_addr *sin, dst;
1054df8bae1dSRodney W. Grimes 	n_time ntime;
1055df8bae1dSRodney W. Grimes 
1056df8bae1dSRodney W. Grimes 	dst = ip->ip_dst;
1057df8bae1dSRodney W. Grimes 	cp = (u_char *)(ip + 1);
105858938916SGarrett Wollman 	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1059df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1060df8bae1dSRodney W. Grimes 		opt = cp[IPOPT_OPTVAL];
1061df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1062df8bae1dSRodney W. Grimes 			break;
1063df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP)
1064df8bae1dSRodney W. Grimes 			optlen = 1;
1065df8bae1dSRodney W. Grimes 		else {
1066fdcb8debSJun-ichiro itojun Hagino 			if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1067fdcb8debSJun-ichiro itojun Hagino 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1068fdcb8debSJun-ichiro itojun Hagino 				goto bad;
1069fdcb8debSJun-ichiro itojun Hagino 			}
1070df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
1071707d00a3SJonathan Lemon 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1072df8bae1dSRodney W. Grimes 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1073df8bae1dSRodney W. Grimes 				goto bad;
1074df8bae1dSRodney W. Grimes 			}
1075df8bae1dSRodney W. Grimes 		}
1076df8bae1dSRodney W. Grimes 		switch (opt) {
1077df8bae1dSRodney W. Grimes 
1078df8bae1dSRodney W. Grimes 		default:
1079df8bae1dSRodney W. Grimes 			break;
1080df8bae1dSRodney W. Grimes 
1081df8bae1dSRodney W. Grimes 		/*
1082df8bae1dSRodney W. Grimes 		 * Source routing with record.
1083df8bae1dSRodney W. Grimes 		 * Find interface with current destination address.
1084df8bae1dSRodney W. Grimes 		 * If none on this machine then drop if strictly routed,
1085df8bae1dSRodney W. Grimes 		 * or do nothing if loosely routed.
1086df8bae1dSRodney W. Grimes 		 * Record interface address and bring up next address
1087df8bae1dSRodney W. Grimes 		 * component.  If strictly routed make sure next
1088df8bae1dSRodney W. Grimes 		 * address is on directly accessible net.
1089df8bae1dSRodney W. Grimes 		 */
1090df8bae1dSRodney W. Grimes 		case IPOPT_LSRR:
1091df8bae1dSRodney W. Grimes 		case IPOPT_SSRR:
1092df8bae1dSRodney W. Grimes 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1093df8bae1dSRodney W. Grimes 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1094df8bae1dSRodney W. Grimes 				goto bad;
1095df8bae1dSRodney W. Grimes 			}
1096df8bae1dSRodney W. Grimes 			ipaddr.sin_addr = ip->ip_dst;
1097df8bae1dSRodney W. Grimes 			ia = (struct in_ifaddr *)
1098df8bae1dSRodney W. Grimes 				ifa_ifwithaddr((struct sockaddr *)&ipaddr);
1099df8bae1dSRodney W. Grimes 			if (ia == 0) {
1100df8bae1dSRodney W. Grimes 				if (opt == IPOPT_SSRR) {
1101df8bae1dSRodney W. Grimes 					type = ICMP_UNREACH;
1102df8bae1dSRodney W. Grimes 					code = ICMP_UNREACH_SRCFAIL;
1103df8bae1dSRodney W. Grimes 					goto bad;
1104df8bae1dSRodney W. Grimes 				}
1105bc189bf8SGuido van Rooij 				if (!ip_dosourceroute)
1106bc189bf8SGuido van Rooij 					goto nosourcerouting;
1107df8bae1dSRodney W. Grimes 				/*
1108df8bae1dSRodney W. Grimes 				 * Loose routing, and not at next destination
1109df8bae1dSRodney W. Grimes 				 * yet; nothing to do except forward.
1110df8bae1dSRodney W. Grimes 				 */
1111df8bae1dSRodney W. Grimes 				break;
1112df8bae1dSRodney W. Grimes 			}
1113df8bae1dSRodney W. Grimes 			off--;			/* 0 origin */
11145d5d5fc0SJonathan Lemon 			if (off > optlen - (int)sizeof(struct in_addr)) {
1115df8bae1dSRodney W. Grimes 				/*
1116df8bae1dSRodney W. Grimes 				 * End of source route.  Should be for us.
1117df8bae1dSRodney W. Grimes 				 */
11184fce5804SGuido van Rooij 				if (!ip_acceptsourceroute)
11194fce5804SGuido van Rooij 					goto nosourcerouting;
1120df8bae1dSRodney W. Grimes 				save_rte(cp, ip->ip_src);
1121df8bae1dSRodney W. Grimes 				break;
1122df8bae1dSRodney W. Grimes 			}
11231025071fSGarrett Wollman 
11241025071fSGarrett Wollman 			if (!ip_dosourceroute) {
11250af8d3ecSDavid Greenman 				if (ipforwarding) {
11260af8d3ecSDavid Greenman 					char buf[16]; /* aaa.bbb.ccc.ddd\0 */
11270af8d3ecSDavid Greenman 					/*
11280af8d3ecSDavid Greenman 					 * Acting as a router, so generate ICMP
11290af8d3ecSDavid Greenman 					 */
1130efa48587SGuido van Rooij nosourcerouting:
1131bc189bf8SGuido van Rooij 					strcpy(buf, inet_ntoa(ip->ip_dst));
11321025071fSGarrett Wollman 					log(LOG_WARNING,
11331025071fSGarrett Wollman 					    "attempted source route from %s to %s\n",
11341025071fSGarrett Wollman 					    inet_ntoa(ip->ip_src), buf);
11351025071fSGarrett Wollman 					type = ICMP_UNREACH;
11361025071fSGarrett Wollman 					code = ICMP_UNREACH_SRCFAIL;
11371025071fSGarrett Wollman 					goto bad;
11380af8d3ecSDavid Greenman 				} else {
11390af8d3ecSDavid Greenman 					/*
11400af8d3ecSDavid Greenman 					 * Not acting as a router, so silently drop.
11410af8d3ecSDavid Greenman 					 */
11420af8d3ecSDavid Greenman 					ipstat.ips_cantforward++;
11430af8d3ecSDavid Greenman 					m_freem(m);
11440af8d3ecSDavid Greenman 					return (1);
11450af8d3ecSDavid Greenman 				}
11461025071fSGarrett Wollman 			}
11471025071fSGarrett Wollman 
1148df8bae1dSRodney W. Grimes 			/*
1149df8bae1dSRodney W. Grimes 			 * locate outgoing interface
1150df8bae1dSRodney W. Grimes 			 */
115194a5d9b6SDavid Greenman 			(void)memcpy(&ipaddr.sin_addr, cp + off,
1152df8bae1dSRodney W. Grimes 			    sizeof(ipaddr.sin_addr));
11531025071fSGarrett Wollman 
1154df8bae1dSRodney W. Grimes 			if (opt == IPOPT_SSRR) {
1155df8bae1dSRodney W. Grimes #define	INA	struct in_ifaddr *
1156df8bae1dSRodney W. Grimes #define	SA	struct sockaddr *
1157df8bae1dSRodney W. Grimes 			    if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
1158df8bae1dSRodney W. Grimes 				ia = (INA)ifa_ifwithnet((SA)&ipaddr);
1159df8bae1dSRodney W. Grimes 			} else
1160df8bae1dSRodney W. Grimes 				ia = ip_rtaddr(ipaddr.sin_addr);
1161df8bae1dSRodney W. Grimes 			if (ia == 0) {
1162df8bae1dSRodney W. Grimes 				type = ICMP_UNREACH;
1163df8bae1dSRodney W. Grimes 				code = ICMP_UNREACH_SRCFAIL;
1164df8bae1dSRodney W. Grimes 				goto bad;
1165df8bae1dSRodney W. Grimes 			}
1166df8bae1dSRodney W. Grimes 			ip->ip_dst = ipaddr.sin_addr;
116794a5d9b6SDavid Greenman 			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
116894a5d9b6SDavid Greenman 			    sizeof(struct in_addr));
1169df8bae1dSRodney W. Grimes 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1170df8bae1dSRodney W. Grimes 			/*
1171df8bae1dSRodney W. Grimes 			 * Let ip_intr's mcast routing check handle mcast pkts
1172df8bae1dSRodney W. Grimes 			 */
1173df8bae1dSRodney W. Grimes 			forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1174df8bae1dSRodney W. Grimes 			break;
1175df8bae1dSRodney W. Grimes 
1176df8bae1dSRodney W. Grimes 		case IPOPT_RR:
1177707d00a3SJonathan Lemon 			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1178707d00a3SJonathan Lemon 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1179707d00a3SJonathan Lemon 				goto bad;
1180707d00a3SJonathan Lemon 			}
1181df8bae1dSRodney W. Grimes 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1182df8bae1dSRodney W. Grimes 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1183df8bae1dSRodney W. Grimes 				goto bad;
1184df8bae1dSRodney W. Grimes 			}
1185df8bae1dSRodney W. Grimes 			/*
1186df8bae1dSRodney W. Grimes 			 * If no space remains, ignore.
1187df8bae1dSRodney W. Grimes 			 */
1188df8bae1dSRodney W. Grimes 			off--;			/* 0 origin */
11895d5d5fc0SJonathan Lemon 			if (off > optlen - (int)sizeof(struct in_addr))
1190df8bae1dSRodney W. Grimes 				break;
119194a5d9b6SDavid Greenman 			(void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1192df8bae1dSRodney W. Grimes 			    sizeof(ipaddr.sin_addr));
1193df8bae1dSRodney W. Grimes 			/*
1194df8bae1dSRodney W. Grimes 			 * locate outgoing interface; if we're the destination,
1195df8bae1dSRodney W. Grimes 			 * use the incoming interface (should be same).
1196df8bae1dSRodney W. Grimes 			 */
1197df8bae1dSRodney W. Grimes 			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
1198df8bae1dSRodney W. Grimes 			    (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
1199df8bae1dSRodney W. Grimes 				type = ICMP_UNREACH;
1200df8bae1dSRodney W. Grimes 				code = ICMP_UNREACH_HOST;
1201df8bae1dSRodney W. Grimes 				goto bad;
1202df8bae1dSRodney W. Grimes 			}
120394a5d9b6SDavid Greenman 			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
120494a5d9b6SDavid Greenman 			    sizeof(struct in_addr));
1205df8bae1dSRodney W. Grimes 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1206df8bae1dSRodney W. Grimes 			break;
1207df8bae1dSRodney W. Grimes 
1208df8bae1dSRodney W. Grimes 		case IPOPT_TS:
1209df8bae1dSRodney W. Grimes 			code = cp - (u_char *)ip;
1210df8bae1dSRodney W. Grimes 			ipt = (struct ip_timestamp *)cp;
1211df8bae1dSRodney W. Grimes 			if (ipt->ipt_len < 5)
1212df8bae1dSRodney W. Grimes 				goto bad;
12135d5d5fc0SJonathan Lemon 			if (ipt->ipt_ptr >
12145d5d5fc0SJonathan Lemon 			    ipt->ipt_len - (int)sizeof(int32_t)) {
1215df8bae1dSRodney W. Grimes 				if (++ipt->ipt_oflw == 0)
1216df8bae1dSRodney W. Grimes 					goto bad;
1217df8bae1dSRodney W. Grimes 				break;
1218df8bae1dSRodney W. Grimes 			}
1219df8bae1dSRodney W. Grimes 			sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
1220df8bae1dSRodney W. Grimes 			switch (ipt->ipt_flg) {
1221df8bae1dSRodney W. Grimes 
1222df8bae1dSRodney W. Grimes 			case IPOPT_TS_TSONLY:
1223df8bae1dSRodney W. Grimes 				break;
1224df8bae1dSRodney W. Grimes 
1225df8bae1dSRodney W. Grimes 			case IPOPT_TS_TSANDADDR:
1226b8e8c209SDavid Greenman 				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1227df8bae1dSRodney W. Grimes 				    sizeof(struct in_addr) > ipt->ipt_len)
1228df8bae1dSRodney W. Grimes 					goto bad;
1229df8bae1dSRodney W. Grimes 				ipaddr.sin_addr = dst;
1230df8bae1dSRodney W. Grimes 				ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
1231df8bae1dSRodney W. Grimes 							    m->m_pkthdr.rcvif);
1232df8bae1dSRodney W. Grimes 				if (ia == 0)
1233df8bae1dSRodney W. Grimes 					continue;
123494a5d9b6SDavid Greenman 				(void)memcpy(sin, &IA_SIN(ia)->sin_addr,
123594a5d9b6SDavid Greenman 				    sizeof(struct in_addr));
1236df8bae1dSRodney W. Grimes 				ipt->ipt_ptr += sizeof(struct in_addr);
1237df8bae1dSRodney W. Grimes 				break;
1238df8bae1dSRodney W. Grimes 
1239df8bae1dSRodney W. Grimes 			case IPOPT_TS_PRESPEC:
1240b8e8c209SDavid Greenman 				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1241df8bae1dSRodney W. Grimes 				    sizeof(struct in_addr) > ipt->ipt_len)
1242df8bae1dSRodney W. Grimes 					goto bad;
124394a5d9b6SDavid Greenman 				(void)memcpy(&ipaddr.sin_addr, sin,
1244df8bae1dSRodney W. Grimes 				    sizeof(struct in_addr));
1245df8bae1dSRodney W. Grimes 				if (ifa_ifwithaddr((SA)&ipaddr) == 0)
1246df8bae1dSRodney W. Grimes 					continue;
1247df8bae1dSRodney W. Grimes 				ipt->ipt_ptr += sizeof(struct in_addr);
1248df8bae1dSRodney W. Grimes 				break;
1249df8bae1dSRodney W. Grimes 
1250df8bae1dSRodney W. Grimes 			default:
1251df8bae1dSRodney W. Grimes 				goto bad;
1252df8bae1dSRodney W. Grimes 			}
1253df8bae1dSRodney W. Grimes 			ntime = iptime();
125494a5d9b6SDavid Greenman 			(void)memcpy(cp + ipt->ipt_ptr - 1, &ntime,
1255df8bae1dSRodney W. Grimes 			    sizeof(n_time));
1256df8bae1dSRodney W. Grimes 			ipt->ipt_ptr += sizeof(n_time);
1257df8bae1dSRodney W. Grimes 		}
1258df8bae1dSRodney W. Grimes 	}
125947174b49SAndrey A. Chernov 	if (forward && ipforwarding) {
1260df8bae1dSRodney W. Grimes 		ip_forward(m, 1);
1261df8bae1dSRodney W. Grimes 		return (1);
1262df8bae1dSRodney W. Grimes 	}
1263df8bae1dSRodney W. Grimes 	return (0);
1264df8bae1dSRodney W. Grimes bad:
1265df8bae1dSRodney W. Grimes 	icmp_error(m, type, code, 0, 0);
1266df8bae1dSRodney W. Grimes 	ipstat.ips_badoptions++;
1267df8bae1dSRodney W. Grimes 	return (1);
1268df8bae1dSRodney W. Grimes }
1269df8bae1dSRodney W. Grimes 
1270df8bae1dSRodney W. Grimes /*
1271df8bae1dSRodney W. Grimes  * Given address of next destination (final or next hop),
1272df8bae1dSRodney W. Grimes  * return internet address info of interface to be used to get there.
1273df8bae1dSRodney W. Grimes  */
12740312fbe9SPoul-Henning Kamp static struct in_ifaddr *
1275df8bae1dSRodney W. Grimes ip_rtaddr(dst)
1276df8bae1dSRodney W. Grimes 	 struct in_addr dst;
1277df8bae1dSRodney W. Grimes {
1278df8bae1dSRodney W. Grimes 	register struct sockaddr_in *sin;
1279df8bae1dSRodney W. Grimes 
1280df8bae1dSRodney W. Grimes 	sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
1281df8bae1dSRodney W. Grimes 
1282df8bae1dSRodney W. Grimes 	if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
1283df8bae1dSRodney W. Grimes 		if (ipforward_rt.ro_rt) {
1284df8bae1dSRodney W. Grimes 			RTFREE(ipforward_rt.ro_rt);
1285df8bae1dSRodney W. Grimes 			ipforward_rt.ro_rt = 0;
1286df8bae1dSRodney W. Grimes 		}
1287df8bae1dSRodney W. Grimes 		sin->sin_family = AF_INET;
1288df8bae1dSRodney W. Grimes 		sin->sin_len = sizeof(*sin);
1289df8bae1dSRodney W. Grimes 		sin->sin_addr = dst;
1290df8bae1dSRodney W. Grimes 
12912c17fe93SGarrett Wollman 		rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1292df8bae1dSRodney W. Grimes 	}
1293df8bae1dSRodney W. Grimes 	if (ipforward_rt.ro_rt == 0)
1294df8bae1dSRodney W. Grimes 		return ((struct in_ifaddr *)0);
1295df8bae1dSRodney W. Grimes 	return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
1296df8bae1dSRodney W. Grimes }
1297df8bae1dSRodney W. Grimes 
1298df8bae1dSRodney W. Grimes /*
1299df8bae1dSRodney W. Grimes  * Save incoming source route for use in replies,
1300df8bae1dSRodney W. Grimes  * to be picked up later by ip_srcroute if the receiver is interested.
1301df8bae1dSRodney W. Grimes  */
1302df8bae1dSRodney W. Grimes void
1303df8bae1dSRodney W. Grimes save_rte(option, dst)
1304df8bae1dSRodney W. Grimes 	u_char *option;
1305df8bae1dSRodney W. Grimes 	struct in_addr dst;
1306df8bae1dSRodney W. Grimes {
1307df8bae1dSRodney W. Grimes 	unsigned olen;
1308df8bae1dSRodney W. Grimes 
1309df8bae1dSRodney W. Grimes 	olen = option[IPOPT_OLEN];
1310df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1311df8bae1dSRodney W. Grimes 	if (ipprintfs)
1312df8bae1dSRodney W. Grimes 		printf("save_rte: olen %d\n", olen);
1313df8bae1dSRodney W. Grimes #endif
1314df8bae1dSRodney W. Grimes 	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1315df8bae1dSRodney W. Grimes 		return;
13160453d3cbSBruce Evans 	bcopy(option, ip_srcrt.srcopt, olen);
1317df8bae1dSRodney W. Grimes 	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1318df8bae1dSRodney W. Grimes 	ip_srcrt.dst = dst;
1319df8bae1dSRodney W. Grimes }
1320df8bae1dSRodney W. Grimes 
1321df8bae1dSRodney W. Grimes /*
1322df8bae1dSRodney W. Grimes  * Retrieve incoming source route for use in replies,
1323df8bae1dSRodney W. Grimes  * in the same form used by setsockopt.
1324df8bae1dSRodney W. Grimes  * The first hop is placed before the options, will be removed later.
1325df8bae1dSRodney W. Grimes  */
1326df8bae1dSRodney W. Grimes struct mbuf *
1327df8bae1dSRodney W. Grimes ip_srcroute()
1328df8bae1dSRodney W. Grimes {
1329df8bae1dSRodney W. Grimes 	register struct in_addr *p, *q;
1330df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1331df8bae1dSRodney W. Grimes 
1332df8bae1dSRodney W. Grimes 	if (ip_nhops == 0)
1333df8bae1dSRodney W. Grimes 		return ((struct mbuf *)0);
1334cfe8b629SGarrett Wollman 	m = m_get(M_DONTWAIT, MT_HEADER);
1335df8bae1dSRodney W. Grimes 	if (m == 0)
1336df8bae1dSRodney W. Grimes 		return ((struct mbuf *)0);
1337df8bae1dSRodney W. Grimes 
1338df8bae1dSRodney W. Grimes #define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1339df8bae1dSRodney W. Grimes 
1340df8bae1dSRodney W. Grimes 	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1341df8bae1dSRodney W. Grimes 	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1342df8bae1dSRodney W. Grimes 	    OPTSIZ;
1343df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1344df8bae1dSRodney W. Grimes 	if (ipprintfs)
1345df8bae1dSRodney W. Grimes 		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1346df8bae1dSRodney W. Grimes #endif
1347df8bae1dSRodney W. Grimes 
1348df8bae1dSRodney W. Grimes 	/*
1349df8bae1dSRodney W. Grimes 	 * First save first hop for return route
1350df8bae1dSRodney W. Grimes 	 */
1351df8bae1dSRodney W. Grimes 	p = &ip_srcrt.route[ip_nhops - 1];
1352df8bae1dSRodney W. Grimes 	*(mtod(m, struct in_addr *)) = *p--;
1353df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1354df8bae1dSRodney W. Grimes 	if (ipprintfs)
1355af38c68cSLuigi Rizzo 		printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr));
1356df8bae1dSRodney W. Grimes #endif
1357df8bae1dSRodney W. Grimes 
1358df8bae1dSRodney W. Grimes 	/*
1359df8bae1dSRodney W. Grimes 	 * Copy option fields and padding (nop) to mbuf.
1360df8bae1dSRodney W. Grimes 	 */
1361df8bae1dSRodney W. Grimes 	ip_srcrt.nop = IPOPT_NOP;
1362df8bae1dSRodney W. Grimes 	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
136394a5d9b6SDavid Greenman 	(void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
136494a5d9b6SDavid Greenman 	    &ip_srcrt.nop, OPTSIZ);
1365df8bae1dSRodney W. Grimes 	q = (struct in_addr *)(mtod(m, caddr_t) +
1366df8bae1dSRodney W. Grimes 	    sizeof(struct in_addr) + OPTSIZ);
1367df8bae1dSRodney W. Grimes #undef OPTSIZ
1368df8bae1dSRodney W. Grimes 	/*
1369df8bae1dSRodney W. Grimes 	 * Record return path as an IP source route,
1370df8bae1dSRodney W. Grimes 	 * reversing the path (pointers are now aligned).
1371df8bae1dSRodney W. Grimes 	 */
1372df8bae1dSRodney W. Grimes 	while (p >= ip_srcrt.route) {
1373df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1374df8bae1dSRodney W. Grimes 		if (ipprintfs)
1375af38c68cSLuigi Rizzo 			printf(" %lx", (u_long)ntohl(q->s_addr));
1376df8bae1dSRodney W. Grimes #endif
1377df8bae1dSRodney W. Grimes 		*q++ = *p--;
1378df8bae1dSRodney W. Grimes 	}
1379df8bae1dSRodney W. Grimes 	/*
1380df8bae1dSRodney W. Grimes 	 * Last hop goes to final destination.
1381df8bae1dSRodney W. Grimes 	 */
1382df8bae1dSRodney W. Grimes 	*q = ip_srcrt.dst;
1383df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1384df8bae1dSRodney W. Grimes 	if (ipprintfs)
1385af38c68cSLuigi Rizzo 		printf(" %lx\n", (u_long)ntohl(q->s_addr));
1386df8bae1dSRodney W. Grimes #endif
1387df8bae1dSRodney W. Grimes 	return (m);
1388df8bae1dSRodney W. Grimes }
1389df8bae1dSRodney W. Grimes 
1390df8bae1dSRodney W. Grimes /*
1391df8bae1dSRodney W. Grimes  * Strip out IP options, at higher
1392df8bae1dSRodney W. Grimes  * level protocol in the kernel.
1393df8bae1dSRodney W. Grimes  * Second argument is buffer to which options
1394df8bae1dSRodney W. Grimes  * will be moved, and return value is their length.
1395df8bae1dSRodney W. Grimes  * XXX should be deleted; last arg currently ignored.
1396df8bae1dSRodney W. Grimes  */
1397df8bae1dSRodney W. Grimes void
1398df8bae1dSRodney W. Grimes ip_stripoptions(m, mopt)
1399df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1400df8bae1dSRodney W. Grimes 	struct mbuf *mopt;
1401df8bae1dSRodney W. Grimes {
1402df8bae1dSRodney W. Grimes 	register int i;
1403df8bae1dSRodney W. Grimes 	struct ip *ip = mtod(m, struct ip *);
1404df8bae1dSRodney W. Grimes 	register caddr_t opts;
1405df8bae1dSRodney W. Grimes 	int olen;
1406df8bae1dSRodney W. Grimes 
140758938916SGarrett Wollman 	olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1408df8bae1dSRodney W. Grimes 	opts = (caddr_t)(ip + 1);
1409df8bae1dSRodney W. Grimes 	i = m->m_len - (sizeof (struct ip) + olen);
1410df8bae1dSRodney W. Grimes 	bcopy(opts + olen, opts, (unsigned)i);
1411df8bae1dSRodney W. Grimes 	m->m_len -= olen;
1412df8bae1dSRodney W. Grimes 	if (m->m_flags & M_PKTHDR)
1413df8bae1dSRodney W. Grimes 		m->m_pkthdr.len -= olen;
141458938916SGarrett Wollman 	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
1415df8bae1dSRodney W. Grimes }
1416df8bae1dSRodney W. Grimes 
1417df8bae1dSRodney W. Grimes u_char inetctlerrmap[PRC_NCMDS] = {
1418df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1419df8bae1dSRodney W. Grimes 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1420df8bae1dSRodney W. Grimes 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1421df8bae1dSRodney W. Grimes 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1422df8bae1dSRodney W. Grimes 	0,		0,		0,		0,
1423df8bae1dSRodney W. Grimes 	ENOPROTOOPT
1424df8bae1dSRodney W. Grimes };
1425df8bae1dSRodney W. Grimes 
1426df8bae1dSRodney W. Grimes /*
1427df8bae1dSRodney W. Grimes  * Forward a packet.  If some error occurs return the sender
1428df8bae1dSRodney W. Grimes  * an icmp packet.  Note we can't always generate a meaningful
1429df8bae1dSRodney W. Grimes  * icmp message because icmp doesn't have a large enough repertoire
1430df8bae1dSRodney W. Grimes  * of codes and types.
1431df8bae1dSRodney W. Grimes  *
1432df8bae1dSRodney W. Grimes  * If not forwarding, just drop the packet.  This could be confusing
1433df8bae1dSRodney W. Grimes  * if ipforwarding was zero but some routing protocol was advancing
1434df8bae1dSRodney W. Grimes  * us as a gateway to somewhere.  However, we must let the routing
1435df8bae1dSRodney W. Grimes  * protocol deal with that.
1436df8bae1dSRodney W. Grimes  *
1437df8bae1dSRodney W. Grimes  * The srcrt parameter indicates whether the packet is being forwarded
1438df8bae1dSRodney W. Grimes  * via a source route.
1439df8bae1dSRodney W. Grimes  */
14400312fbe9SPoul-Henning Kamp static void
1441df8bae1dSRodney W. Grimes ip_forward(m, srcrt)
1442df8bae1dSRodney W. Grimes 	struct mbuf *m;
1443df8bae1dSRodney W. Grimes 	int srcrt;
1444df8bae1dSRodney W. Grimes {
1445df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
1446df8bae1dSRodney W. Grimes 	register struct sockaddr_in *sin;
1447df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
144826f9a767SRodney W. Grimes 	int error, type = 0, code = 0;
1449df8bae1dSRodney W. Grimes 	struct mbuf *mcopy;
1450df8bae1dSRodney W. Grimes 	n_long dest;
1451df8bae1dSRodney W. Grimes 	struct ifnet *destifp;
14526a800098SYoshinobu Inoue #ifdef IPSEC
14536a800098SYoshinobu Inoue 	struct ifnet dummyifp;
14546a800098SYoshinobu Inoue #endif
1455df8bae1dSRodney W. Grimes 
1456df8bae1dSRodney W. Grimes 	dest = 0;
1457df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1458df8bae1dSRodney W. Grimes 	if (ipprintfs)
145961ce519bSPoul-Henning Kamp 		printf("forward: src %lx dst %lx ttl %x\n",
1460162886e2SBruce Evans 		    (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr,
1461162886e2SBruce Evans 		    ip->ip_ttl);
1462df8bae1dSRodney W. Grimes #endif
1463100ba1a6SJordan K. Hubbard 
1464100ba1a6SJordan K. Hubbard 
146592af003dSGarrett Wollman 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1466df8bae1dSRodney W. Grimes 		ipstat.ips_cantforward++;
1467df8bae1dSRodney W. Grimes 		m_freem(m);
1468df8bae1dSRodney W. Grimes 		return;
1469df8bae1dSRodney W. Grimes 	}
14701b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
14711b968362SDag-Erling Smørgrav 	if (!ipstealth) {
14721b968362SDag-Erling Smørgrav #endif
1473df8bae1dSRodney W. Grimes 		if (ip->ip_ttl <= IPTTLDEC) {
14741b968362SDag-Erling Smørgrav 			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
14751b968362SDag-Erling Smørgrav 			    dest, 0);
1476df8bae1dSRodney W. Grimes 			return;
1477df8bae1dSRodney W. Grimes 		}
14781b968362SDag-Erling Smørgrav #ifdef IPSTEALTH
14791b968362SDag-Erling Smørgrav 	}
14801b968362SDag-Erling Smørgrav #endif
1481df8bae1dSRodney W. Grimes 
1482df8bae1dSRodney W. Grimes 	sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
1483df8bae1dSRodney W. Grimes 	if ((rt = ipforward_rt.ro_rt) == 0 ||
1484df8bae1dSRodney W. Grimes 	    ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
1485df8bae1dSRodney W. Grimes 		if (ipforward_rt.ro_rt) {
1486df8bae1dSRodney W. Grimes 			RTFREE(ipforward_rt.ro_rt);
1487df8bae1dSRodney W. Grimes 			ipforward_rt.ro_rt = 0;
1488df8bae1dSRodney W. Grimes 		}
1489df8bae1dSRodney W. Grimes 		sin->sin_family = AF_INET;
1490df8bae1dSRodney W. Grimes 		sin->sin_len = sizeof(*sin);
1491df8bae1dSRodney W. Grimes 		sin->sin_addr = ip->ip_dst;
1492df8bae1dSRodney W. Grimes 
14932c17fe93SGarrett Wollman 		rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1494df8bae1dSRodney W. Grimes 		if (ipforward_rt.ro_rt == 0) {
1495df8bae1dSRodney W. Grimes 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1496df8bae1dSRodney W. Grimes 			return;
1497df8bae1dSRodney W. Grimes 		}
1498df8bae1dSRodney W. Grimes 		rt = ipforward_rt.ro_rt;
1499df8bae1dSRodney W. Grimes 	}
1500df8bae1dSRodney W. Grimes 
1501df8bae1dSRodney W. Grimes 	/*
1502df8bae1dSRodney W. Grimes 	 * Save at most 64 bytes of the packet in case
1503df8bae1dSRodney W. Grimes 	 * we need to generate an ICMP message to the src.
1504df8bae1dSRodney W. Grimes 	 */
1505df8bae1dSRodney W. Grimes 	mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64));
150604287599SRuslan Ermilov 	if (mcopy && (mcopy->m_flags & M_EXT))
150704287599SRuslan Ermilov 		m_copydata(mcopy, 0, sizeof(struct ip), mtod(mcopy, caddr_t));
150804287599SRuslan Ermilov 
150904287599SRuslan Ermilov #ifdef IPSTEALTH
151004287599SRuslan Ermilov 	if (!ipstealth) {
151104287599SRuslan Ermilov #endif
151204287599SRuslan Ermilov 		ip->ip_ttl -= IPTTLDEC;
151304287599SRuslan Ermilov #ifdef IPSTEALTH
151404287599SRuslan Ermilov 	}
151504287599SRuslan Ermilov #endif
1516df8bae1dSRodney W. Grimes 
1517df8bae1dSRodney W. Grimes 	/*
1518df8bae1dSRodney W. Grimes 	 * If forwarding packet using same interface that it came in on,
1519df8bae1dSRodney W. Grimes 	 * perhaps should send a redirect to sender to shortcut a hop.
1520df8bae1dSRodney W. Grimes 	 * Only send redirect if source is sending directly to us,
1521df8bae1dSRodney W. Grimes 	 * and if packet was not source routed (or has any options).
1522df8bae1dSRodney W. Grimes 	 * Also, don't send redirect if forwarding using a default route
1523df8bae1dSRodney W. Grimes 	 * or a route modified by a redirect.
1524df8bae1dSRodney W. Grimes 	 */
1525df8bae1dSRodney W. Grimes #define	satosin(sa)	((struct sockaddr_in *)(sa))
1526df8bae1dSRodney W. Grimes 	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1527df8bae1dSRodney W. Grimes 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1528df8bae1dSRodney W. Grimes 	    satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1529df8bae1dSRodney W. Grimes 	    ipsendredirects && !srcrt) {
1530df8bae1dSRodney W. Grimes #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1531df8bae1dSRodney W. Grimes 		u_long src = ntohl(ip->ip_src.s_addr);
1532df8bae1dSRodney W. Grimes 
1533df8bae1dSRodney W. Grimes 		if (RTA(rt) &&
1534df8bae1dSRodney W. Grimes 		    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1535df8bae1dSRodney W. Grimes 		    if (rt->rt_flags & RTF_GATEWAY)
1536df8bae1dSRodney W. Grimes 			dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1537df8bae1dSRodney W. Grimes 		    else
1538df8bae1dSRodney W. Grimes 			dest = ip->ip_dst.s_addr;
1539df8bae1dSRodney W. Grimes 		    /* Router requirements says to only send host redirects */
1540df8bae1dSRodney W. Grimes 		    type = ICMP_REDIRECT;
1541df8bae1dSRodney W. Grimes 		    code = ICMP_REDIRECT_HOST;
1542df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1543df8bae1dSRodney W. Grimes 		    if (ipprintfs)
1544df8bae1dSRodney W. Grimes 		        printf("redirect (%d) to %lx\n", code, (u_long)dest);
1545df8bae1dSRodney W. Grimes #endif
1546df8bae1dSRodney W. Grimes 		}
1547df8bae1dSRodney W. Grimes 	}
1548df8bae1dSRodney W. Grimes 
1549b97d15cbSGarrett Wollman 	error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1550b97d15cbSGarrett Wollman 			  IP_FORWARDING, 0);
1551df8bae1dSRodney W. Grimes 	if (error)
1552df8bae1dSRodney W. Grimes 		ipstat.ips_cantforward++;
1553df8bae1dSRodney W. Grimes 	else {
1554df8bae1dSRodney W. Grimes 		ipstat.ips_forward++;
1555df8bae1dSRodney W. Grimes 		if (type)
1556df8bae1dSRodney W. Grimes 			ipstat.ips_redirectsent++;
1557df8bae1dSRodney W. Grimes 		else {
15581f91d8c5SDavid Greenman 			if (mcopy) {
15591f91d8c5SDavid Greenman 				ipflow_create(&ipforward_rt, mcopy);
1560df8bae1dSRodney W. Grimes 				m_freem(mcopy);
15611f91d8c5SDavid Greenman 			}
1562df8bae1dSRodney W. Grimes 			return;
1563df8bae1dSRodney W. Grimes 		}
1564df8bae1dSRodney W. Grimes 	}
1565df8bae1dSRodney W. Grimes 	if (mcopy == NULL)
1566df8bae1dSRodney W. Grimes 		return;
1567df8bae1dSRodney W. Grimes 	destifp = NULL;
1568df8bae1dSRodney W. Grimes 
1569df8bae1dSRodney W. Grimes 	switch (error) {
1570df8bae1dSRodney W. Grimes 
1571df8bae1dSRodney W. Grimes 	case 0:				/* forwarded, but need redirect */
1572df8bae1dSRodney W. Grimes 		/* type, code set above */
1573df8bae1dSRodney W. Grimes 		break;
1574df8bae1dSRodney W. Grimes 
1575df8bae1dSRodney W. Grimes 	case ENETUNREACH:		/* shouldn't happen, checked above */
1576df8bae1dSRodney W. Grimes 	case EHOSTUNREACH:
1577df8bae1dSRodney W. Grimes 	case ENETDOWN:
1578df8bae1dSRodney W. Grimes 	case EHOSTDOWN:
1579df8bae1dSRodney W. Grimes 	default:
1580df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1581df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_HOST;
1582df8bae1dSRodney W. Grimes 		break;
1583df8bae1dSRodney W. Grimes 
1584df8bae1dSRodney W. Grimes 	case EMSGSIZE:
1585df8bae1dSRodney W. Grimes 		type = ICMP_UNREACH;
1586df8bae1dSRodney W. Grimes 		code = ICMP_UNREACH_NEEDFRAG;
15876a800098SYoshinobu Inoue #ifndef IPSEC
1588df8bae1dSRodney W. Grimes 		if (ipforward_rt.ro_rt)
1589df8bae1dSRodney W. Grimes 			destifp = ipforward_rt.ro_rt->rt_ifp;
15906a800098SYoshinobu Inoue #else
15916a800098SYoshinobu Inoue 		/*
15926a800098SYoshinobu Inoue 		 * If the packet is routed over IPsec tunnel, tell the
15936a800098SYoshinobu Inoue 		 * originator the tunnel MTU.
15946a800098SYoshinobu Inoue 		 *	tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
15956a800098SYoshinobu Inoue 		 * XXX quickhack!!!
15966a800098SYoshinobu Inoue 		 */
15976a800098SYoshinobu Inoue 		if (ipforward_rt.ro_rt) {
15986a800098SYoshinobu Inoue 			struct secpolicy *sp = NULL;
15996a800098SYoshinobu Inoue 			int ipsecerror;
16006a800098SYoshinobu Inoue 			int ipsechdr;
16016a800098SYoshinobu Inoue 			struct route *ro;
16026a800098SYoshinobu Inoue 
16036a800098SYoshinobu Inoue 			sp = ipsec4_getpolicybyaddr(mcopy,
16046a800098SYoshinobu Inoue 						    IPSEC_DIR_OUTBOUND,
16056a800098SYoshinobu Inoue 			                            IP_FORWARDING,
16066a800098SYoshinobu Inoue 			                            &ipsecerror);
16076a800098SYoshinobu Inoue 
16086a800098SYoshinobu Inoue 			if (sp == NULL)
16096a800098SYoshinobu Inoue 				destifp = ipforward_rt.ro_rt->rt_ifp;
16106a800098SYoshinobu Inoue 			else {
16116a800098SYoshinobu Inoue 				/* count IPsec header size */
16126a800098SYoshinobu Inoue 				ipsechdr = ipsec4_hdrsiz(mcopy,
16136a800098SYoshinobu Inoue 							 IPSEC_DIR_OUTBOUND,
16146a800098SYoshinobu Inoue 							 NULL);
16156a800098SYoshinobu Inoue 
16166a800098SYoshinobu Inoue 				/*
16176a800098SYoshinobu Inoue 				 * find the correct route for outer IPv4
16186a800098SYoshinobu Inoue 				 * header, compute tunnel MTU.
16196a800098SYoshinobu Inoue 				 *
16206a800098SYoshinobu Inoue 				 * XXX BUG ALERT
16216a800098SYoshinobu Inoue 				 * The "dummyifp" code relies upon the fact
16226a800098SYoshinobu Inoue 				 * that icmp_error() touches only ifp->if_mtu.
16236a800098SYoshinobu Inoue 				 */
16246a800098SYoshinobu Inoue 				/*XXX*/
16256a800098SYoshinobu Inoue 				destifp = NULL;
16266a800098SYoshinobu Inoue 				if (sp->req != NULL
16276a800098SYoshinobu Inoue 				 && sp->req->sav != NULL
16286a800098SYoshinobu Inoue 				 && sp->req->sav->sah != NULL) {
16296a800098SYoshinobu Inoue 					ro = &sp->req->sav->sah->sa_route;
16306a800098SYoshinobu Inoue 					if (ro->ro_rt && ro->ro_rt->rt_ifp) {
16316a800098SYoshinobu Inoue 						dummyifp.if_mtu =
16326a800098SYoshinobu Inoue 						    ro->ro_rt->rt_ifp->if_mtu;
16336a800098SYoshinobu Inoue 						dummyifp.if_mtu -= ipsechdr;
16346a800098SYoshinobu Inoue 						destifp = &dummyifp;
16356a800098SYoshinobu Inoue 					}
16366a800098SYoshinobu Inoue 				}
16376a800098SYoshinobu Inoue 
16386a800098SYoshinobu Inoue 				key_freesp(sp);
16396a800098SYoshinobu Inoue 			}
16406a800098SYoshinobu Inoue 		}
16416a800098SYoshinobu Inoue #endif /*IPSEC*/
1642df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
1643df8bae1dSRodney W. Grimes 		break;
1644df8bae1dSRodney W. Grimes 
1645df8bae1dSRodney W. Grimes 	case ENOBUFS:
1646df8bae1dSRodney W. Grimes 		type = ICMP_SOURCEQUENCH;
1647df8bae1dSRodney W. Grimes 		code = 0;
1648df8bae1dSRodney W. Grimes 		break;
16493a06e3e0SRuslan Ermilov 
16503a06e3e0SRuslan Ermilov 	case EACCES:			/* ipfw denied packet */
16513a06e3e0SRuslan Ermilov 		m_freem(mcopy);
16523a06e3e0SRuslan Ermilov 		return;
1653df8bae1dSRodney W. Grimes 	}
165404287599SRuslan Ermilov 	if (mcopy->m_flags & M_EXT)
165504287599SRuslan Ermilov 		m_copyback(mcopy, 0, sizeof(struct ip), mtod(mcopy, caddr_t));
1656df8bae1dSRodney W. Grimes 	icmp_error(mcopy, type, code, dest, destifp);
1657df8bae1dSRodney W. Grimes }
1658df8bae1dSRodney W. Grimes 
165982c23ebaSBill Fenner void
166082c23ebaSBill Fenner ip_savecontrol(inp, mp, ip, m)
166182c23ebaSBill Fenner 	register struct inpcb *inp;
166282c23ebaSBill Fenner 	register struct mbuf **mp;
166382c23ebaSBill Fenner 	register struct ip *ip;
166482c23ebaSBill Fenner 	register struct mbuf *m;
166582c23ebaSBill Fenner {
166682c23ebaSBill Fenner 	if (inp->inp_socket->so_options & SO_TIMESTAMP) {
166782c23ebaSBill Fenner 		struct timeval tv;
166882c23ebaSBill Fenner 
166982c23ebaSBill Fenner 		microtime(&tv);
167082c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
167182c23ebaSBill Fenner 			SCM_TIMESTAMP, SOL_SOCKET);
167282c23ebaSBill Fenner 		if (*mp)
167382c23ebaSBill Fenner 			mp = &(*mp)->m_next;
167482c23ebaSBill Fenner 	}
167582c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVDSTADDR) {
167682c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
167782c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
167882c23ebaSBill Fenner 		if (*mp)
167982c23ebaSBill Fenner 			mp = &(*mp)->m_next;
168082c23ebaSBill Fenner 	}
168182c23ebaSBill Fenner #ifdef notyet
168282c23ebaSBill Fenner 	/* XXX
168382c23ebaSBill Fenner 	 * Moving these out of udp_input() made them even more broken
168482c23ebaSBill Fenner 	 * than they already were.
168582c23ebaSBill Fenner 	 */
168682c23ebaSBill Fenner 	/* options were tossed already */
168782c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVOPTS) {
168882c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
168982c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
169082c23ebaSBill Fenner 		if (*mp)
169182c23ebaSBill Fenner 			mp = &(*mp)->m_next;
169282c23ebaSBill Fenner 	}
169382c23ebaSBill Fenner 	/* ip_srcroute doesn't do what we want here, need to fix */
169482c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVRETOPTS) {
169582c23ebaSBill Fenner 		*mp = sbcreatecontrol((caddr_t) ip_srcroute(),
169682c23ebaSBill Fenner 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
169782c23ebaSBill Fenner 		if (*mp)
169882c23ebaSBill Fenner 			mp = &(*mp)->m_next;
169982c23ebaSBill Fenner 	}
170082c23ebaSBill Fenner #endif
170182c23ebaSBill Fenner 	if (inp->inp_flags & INP_RECVIF) {
1702d314ad7bSJulian Elischer 		struct ifnet *ifp;
1703d314ad7bSJulian Elischer 		struct sdlbuf {
170482c23ebaSBill Fenner 			struct sockaddr_dl sdl;
1705d314ad7bSJulian Elischer 			u_char	pad[32];
1706d314ad7bSJulian Elischer 		} sdlbuf;
1707d314ad7bSJulian Elischer 		struct sockaddr_dl *sdp;
1708d314ad7bSJulian Elischer 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
170982c23ebaSBill Fenner 
1710d314ad7bSJulian Elischer 		if (((ifp = m->m_pkthdr.rcvif))
1711d314ad7bSJulian Elischer 		&& ( ifp->if_index && (ifp->if_index <= if_index))) {
1712d314ad7bSJulian Elischer 			sdp = (struct sockaddr_dl *)(ifnet_addrs
1713d314ad7bSJulian Elischer 					[ifp->if_index - 1]->ifa_addr);
1714d314ad7bSJulian Elischer 			/*
1715d314ad7bSJulian Elischer 			 * Change our mind and don't try copy.
1716d314ad7bSJulian Elischer 			 */
1717d314ad7bSJulian Elischer 			if ((sdp->sdl_family != AF_LINK)
1718d314ad7bSJulian Elischer 			|| (sdp->sdl_len > sizeof(sdlbuf))) {
1719d314ad7bSJulian Elischer 				goto makedummy;
1720d314ad7bSJulian Elischer 			}
1721d314ad7bSJulian Elischer 			bcopy(sdp, sdl2, sdp->sdl_len);
1722d314ad7bSJulian Elischer 		} else {
1723d314ad7bSJulian Elischer makedummy:
1724d314ad7bSJulian Elischer 			sdl2->sdl_len
1725d314ad7bSJulian Elischer 				= offsetof(struct sockaddr_dl, sdl_data[0]);
1726d314ad7bSJulian Elischer 			sdl2->sdl_family = AF_LINK;
1727d314ad7bSJulian Elischer 			sdl2->sdl_index = 0;
1728d314ad7bSJulian Elischer 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1729d314ad7bSJulian Elischer 		}
1730d314ad7bSJulian Elischer 		*mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
173182c23ebaSBill Fenner 			IP_RECVIF, IPPROTO_IP);
173282c23ebaSBill Fenner 		if (*mp)
173382c23ebaSBill Fenner 			mp = &(*mp)->m_next;
173482c23ebaSBill Fenner 	}
173582c23ebaSBill Fenner }
173682c23ebaSBill Fenner 
1737df8bae1dSRodney W. Grimes int
1738f0068c4aSGarrett Wollman ip_rsvp_init(struct socket *so)
1739f0068c4aSGarrett Wollman {
1740f0068c4aSGarrett Wollman 	if (so->so_type != SOCK_RAW ||
1741f0068c4aSGarrett Wollman 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1742f0068c4aSGarrett Wollman 	  return EOPNOTSUPP;
1743f0068c4aSGarrett Wollman 
1744f0068c4aSGarrett Wollman 	if (ip_rsvpd != NULL)
1745f0068c4aSGarrett Wollman 	  return EADDRINUSE;
1746f0068c4aSGarrett Wollman 
1747f0068c4aSGarrett Wollman 	ip_rsvpd = so;
17481c5de19aSGarrett Wollman 	/*
17491c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-increment
17501c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
17511c5de19aSGarrett Wollman 	 */
17521c5de19aSGarrett Wollman 	if (!ip_rsvp_on) {
17531c5de19aSGarrett Wollman 		ip_rsvp_on = 1;
17541c5de19aSGarrett Wollman 		rsvp_on++;
17551c5de19aSGarrett Wollman 	}
1756f0068c4aSGarrett Wollman 
1757f0068c4aSGarrett Wollman 	return 0;
1758f0068c4aSGarrett Wollman }
1759f0068c4aSGarrett Wollman 
1760f0068c4aSGarrett Wollman int
1761f0068c4aSGarrett Wollman ip_rsvp_done(void)
1762f0068c4aSGarrett Wollman {
1763f0068c4aSGarrett Wollman 	ip_rsvpd = NULL;
17641c5de19aSGarrett Wollman 	/*
17651c5de19aSGarrett Wollman 	 * This may seem silly, but we need to be sure we don't over-decrement
17661c5de19aSGarrett Wollman 	 * the RSVP counter, in case something slips up.
17671c5de19aSGarrett Wollman 	 */
17681c5de19aSGarrett Wollman 	if (ip_rsvp_on) {
17691c5de19aSGarrett Wollman 		ip_rsvp_on = 0;
17701c5de19aSGarrett Wollman 		rsvp_on--;
17711c5de19aSGarrett Wollman 	}
1772f0068c4aSGarrett Wollman 	return 0;
1773f0068c4aSGarrett Wollman }
1774