ip_input.c revision 1.149
1/*	$NetBSD: ip_input.c,v 1.149 2002/05/12 15:48:39 wiz Exp $	*/
2
3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32/*-
33 * Copyright (c) 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Public Access Networks Corporation ("Panix").  It was developed under
38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 *    notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 *    notice, this list of conditions and the following disclaimer in the
47 *    documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 *    must display the following acknowledgement:
50 *	This product includes software developed by the NetBSD
51 *	Foundation, Inc. and its contributors.
52 * 4. Neither the name of The NetBSD Foundation nor the names of its
53 *    contributors may be used to endorse or promote products derived
54 *    from this software without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 * POSSIBILITY OF SUCH DAMAGE.
67 */
68
69/*
70 * Copyright (c) 1982, 1986, 1988, 1993
71 *	The Regents of the University of California.  All rights reserved.
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 *    notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 *    notice, this list of conditions and the following disclaimer in the
80 *    documentation and/or other materials provided with the distribution.
81 * 3. All advertising materials mentioning features or use of this software
82 *    must display the following acknowledgement:
83 *	This product includes software developed by the University of
84 *	California, Berkeley and its contributors.
85 * 4. Neither the name of the University nor the names of its contributors
86 *    may be used to endorse or promote products derived from this software
87 *    without specific prior written permission.
88 *
89 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
92 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
95 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
96 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
97 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99 * SUCH DAMAGE.
100 *
101 *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
102 */
103
104#include <sys/cdefs.h>
105__KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.149 2002/05/12 15:48:39 wiz Exp $");
106
107#include "opt_gateway.h"
108#include "opt_pfil_hooks.h"
109#include "opt_ipsec.h"
110#include "opt_mrouting.h"
111#include "opt_inet_csum.h"
112
113#include <sys/param.h>
114#include <sys/systm.h>
115#include <sys/malloc.h>
116#include <sys/mbuf.h>
117#include <sys/domain.h>
118#include <sys/protosw.h>
119#include <sys/socket.h>
120#include <sys/socketvar.h>
121#include <sys/errno.h>
122#include <sys/time.h>
123#include <sys/kernel.h>
124#include <sys/pool.h>
125#include <sys/sysctl.h>
126
127#include <net/if.h>
128#include <net/if_dl.h>
129#include <net/route.h>
130#include <net/pfil.h>
131
132#include <netinet/in.h>
133#include <netinet/in_systm.h>
134#include <netinet/ip.h>
135#include <netinet/in_pcb.h>
136#include <netinet/in_var.h>
137#include <netinet/ip_var.h>
138#include <netinet/ip_icmp.h>
139/* just for gif_ttl */
140#include <netinet/in_gif.h>
141#include "gif.h"
142#include <net/if_gre.h>
143#include "gre.h"
144
145#ifdef MROUTING
146#include <netinet/ip_mroute.h>
147#endif
148
149#ifdef IPSEC
150#include <netinet6/ipsec.h>
151#include <netkey/key.h>
152#endif
153
154#ifndef	IPFORWARDING
155#ifdef GATEWAY
156#define	IPFORWARDING	1	/* forward IP packets not for us */
157#else /* GATEWAY */
158#define	IPFORWARDING	0	/* don't forward IP packets not for us */
159#endif /* GATEWAY */
160#endif /* IPFORWARDING */
161#ifndef	IPSENDREDIRECTS
162#define	IPSENDREDIRECTS	1
163#endif
164#ifndef IPFORWSRCRT
165#define	IPFORWSRCRT	1	/* forward source-routed packets */
166#endif
167#ifndef IPALLOWSRCRT
168#define	IPALLOWSRCRT	1	/* allow source-routed packets */
169#endif
170#ifndef IPMTUDISC
171#define IPMTUDISC	0
172#endif
173#ifndef IPMTUDISCTIMEOUT
174#define IPMTUDISCTIMEOUT (10 * 60)	/* as per RFC 1191 */
175#endif
176
177/*
178 * Note: DIRECTED_BROADCAST is handled this way so that previous
179 * configuration using this option will Just Work.
180 */
181#ifndef IPDIRECTEDBCAST
182#ifdef DIRECTED_BROADCAST
183#define IPDIRECTEDBCAST	1
184#else
185#define	IPDIRECTEDBCAST	0
186#endif /* DIRECTED_BROADCAST */
187#endif /* IPDIRECTEDBCAST */
188int	ipforwarding = IPFORWARDING;
189int	ipsendredirects = IPSENDREDIRECTS;
190int	ip_defttl = IPDEFTTL;
191int	ip_forwsrcrt = IPFORWSRCRT;
192int	ip_directedbcast = IPDIRECTEDBCAST;
193int	ip_allowsrcrt = IPALLOWSRCRT;
194int	ip_mtudisc = IPMTUDISC;
195u_int	ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
196#ifdef DIAGNOSTIC
197int	ipprintfs = 0;
198#endif
199
200struct rttimer_queue *ip_mtudisc_timeout_q = NULL;
201
202extern	struct domain inetdomain;
203int	ipqmaxlen = IFQ_MAXLEN;
204struct	in_ifaddrhead in_ifaddr;
205struct	in_ifaddrhashhead *in_ifaddrhashtbl;
206struct	ifqueue ipintrq;
207struct	ipstat	ipstat;
208u_int16_t	ip_id;
209
210#ifdef PFIL_HOOKS
211struct pfil_head inet_pfil_hook;
212#endif
213
214struct ipqhead ipq;
215int	ipq_locked;
216int	ip_nfragpackets = 0;
217int	ip_maxfragpackets = 200;
218
219static __inline int ipq_lock_try __P((void));
220static __inline void ipq_unlock __P((void));
221
222static __inline int
223ipq_lock_try()
224{
225	int s;
226
227	/*
228	 * Use splvm() -- we're blocking things that would cause
229	 * mbuf allocation.
230	 */
231	s = splvm();
232	if (ipq_locked) {
233		splx(s);
234		return (0);
235	}
236	ipq_locked = 1;
237	splx(s);
238	return (1);
239}
240
241static __inline void
242ipq_unlock()
243{
244	int s;
245
246	s = splvm();
247	ipq_locked = 0;
248	splx(s);
249}
250
251#ifdef DIAGNOSTIC
252#define	IPQ_LOCK()							\
253do {									\
254	if (ipq_lock_try() == 0) {					\
255		printf("%s:%d: ipq already locked\n", __FILE__, __LINE__); \
256		panic("ipq_lock");					\
257	}								\
258} while (0)
259#define	IPQ_LOCK_CHECK()						\
260do {									\
261	if (ipq_locked == 0) {						\
262		printf("%s:%d: ipq lock not held\n", __FILE__, __LINE__); \
263		panic("ipq lock check");				\
264	}								\
265} while (0)
266#else
267#define	IPQ_LOCK()		(void) ipq_lock_try()
268#define	IPQ_LOCK_CHECK()	/* nothing */
269#endif
270
271#define	IPQ_UNLOCK()		ipq_unlock()
272
273struct pool ipqent_pool;
274
275#ifdef INET_CSUM_COUNTERS
276#include <sys/device.h>
277
278struct evcnt ip_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
279    NULL, "inet", "hwcsum bad");
280struct evcnt ip_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
281    NULL, "inet", "hwcsum ok");
282struct evcnt ip_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
283    NULL, "inet", "swcsum");
284
285#define	INET_CSUM_COUNTER_INCR(ev)	(ev)->ev_count++
286
287#else
288
289#define	INET_CSUM_COUNTER_INCR(ev)	/* nothing */
290
291#endif /* INET_CSUM_COUNTERS */
292
293/*
294 * We need to save the IP options in case a protocol wants to respond
295 * to an incoming packet over the same route if the packet got here
296 * using IP source routing.  This allows connection establishment and
297 * maintenance when the remote end is on a network that is not known
298 * to us.
299 */
300int	ip_nhops = 0;
301static	struct ip_srcrt {
302	struct	in_addr dst;			/* final destination */
303	char	nop;				/* one NOP to align */
304	char	srcopt[IPOPT_OFFSET + 1];	/* OPTVAL, OLEN and OFFSET */
305	struct	in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
306} ip_srcrt;
307
308static void save_rte __P((u_char *, struct in_addr));
309
310/*
311 * IP initialization: fill in IP protocol switch table.
312 * All protocols not implemented in kernel go to raw IP protocol handler.
313 */
314void
315ip_init()
316{
317	struct protosw *pr;
318	int i;
319
320	pool_init(&ipqent_pool, sizeof(struct ipqent), 0, 0, 0, "ipqepl",
321	    NULL);
322
323	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
324	if (pr == 0)
325		panic("ip_init");
326	for (i = 0; i < IPPROTO_MAX; i++)
327		ip_protox[i] = pr - inetsw;
328	for (pr = inetdomain.dom_protosw;
329	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
330		if (pr->pr_domain->dom_family == PF_INET &&
331		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
332			ip_protox[pr->pr_protocol] = pr - inetsw;
333	LIST_INIT(&ipq);
334	ip_id = time.tv_sec & 0xffff;
335	ipintrq.ifq_maxlen = ipqmaxlen;
336	TAILQ_INIT(&in_ifaddr);
337	in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, M_IFADDR,
338	    M_WAITOK, &in_ifaddrhash);
339	if (ip_mtudisc != 0)
340		ip_mtudisc_timeout_q =
341		    rt_timer_queue_create(ip_mtudisc_timeout);
342#ifdef GATEWAY
343	ipflow_init();
344#endif
345
346#ifdef PFIL_HOOKS
347	/* Register our Packet Filter hook. */
348	inet_pfil_hook.ph_type = PFIL_TYPE_AF;
349	inet_pfil_hook.ph_af   = AF_INET;
350	i = pfil_head_register(&inet_pfil_hook);
351	if (i != 0)
352		printf("ip_init: WARNING: unable to register pfil hook, "
353		    "error %d\n", i);
354#endif /* PFIL_HOOKS */
355
356#ifdef INET_CSUM_COUNTERS
357	evcnt_attach_static(&ip_hwcsum_bad);
358	evcnt_attach_static(&ip_hwcsum_ok);
359	evcnt_attach_static(&ip_swcsum);
360#endif /* INET_CSUM_COUNTERS */
361}
362
363struct	sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
364struct	route ipforward_rt;
365
366/*
367 * IP software interrupt routine
368 */
369void
370ipintr()
371{
372	int s;
373	struct mbuf *m;
374
375	while (1) {
376		s = splnet();
377		IF_DEQUEUE(&ipintrq, m);
378		splx(s);
379		if (m == 0)
380			return;
381		ip_input(m);
382	}
383}
384
385/*
386 * Ip input routine.  Checksum and byte swap header.  If fragmented
387 * try to reassemble.  Process options.  Pass to next level.
388 */
389void
390ip_input(struct mbuf *m)
391{
392	struct ip *ip = NULL;
393	struct ipq *fp;
394	struct in_ifaddr *ia;
395	struct ifaddr *ifa;
396	struct ipqent *ipqe;
397	int hlen = 0, mff, len;
398	int downmatch;
399
400#ifdef	DIAGNOSTIC
401	if ((m->m_flags & M_PKTHDR) == 0)
402		panic("ipintr no HDR");
403#endif
404#ifdef IPSEC
405	/*
406	 * should the inner packet be considered authentic?
407	 * see comment in ah4_input().
408	 */
409	if (m) {
410		m->m_flags &= ~M_AUTHIPHDR;
411		m->m_flags &= ~M_AUTHIPDGM;
412	}
413#endif
414	/*
415	 * If no IP addresses have been set yet but the interfaces
416	 * are receiving, can't do anything with incoming packets yet.
417	 */
418	if (TAILQ_FIRST(&in_ifaddr) == 0)
419		goto bad;
420	ipstat.ips_total++;
421	if (m->m_len < sizeof (struct ip) &&
422	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
423		ipstat.ips_toosmall++;
424		return;
425	}
426	ip = mtod(m, struct ip *);
427	if (ip->ip_v != IPVERSION) {
428		ipstat.ips_badvers++;
429		goto bad;
430	}
431	hlen = ip->ip_hl << 2;
432	if (hlen < sizeof(struct ip)) {	/* minimum header length */
433		ipstat.ips_badhlen++;
434		goto bad;
435	}
436	if (hlen > m->m_len) {
437		if ((m = m_pullup(m, hlen)) == 0) {
438			ipstat.ips_badhlen++;
439			return;
440		}
441		ip = mtod(m, struct ip *);
442	}
443
444	/*
445	 * RFC1122: packets with a multicast source address are
446	 * not allowed.
447	 */
448	if (IN_MULTICAST(ip->ip_src.s_addr)) {
449		ipstat.ips_badaddr++;
450		goto bad;
451	}
452
453	/* 127/8 must not appear on wire - RFC1122 */
454	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
455	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
456		if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
457			ipstat.ips_badaddr++;
458			goto bad;
459		}
460	}
461
462	switch (m->m_pkthdr.csum_flags &
463		((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
464		 M_CSUM_IPv4_BAD)) {
465	case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
466		INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad);
467		goto badcsum;
468
469	case M_CSUM_IPv4:
470		/* Checksum was okay. */
471		INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok);
472		break;
473
474	default:
475		/* Must compute it ourselves. */
476		INET_CSUM_COUNTER_INCR(&ip_swcsum);
477		if (in_cksum(m, hlen) != 0)
478			goto bad;
479		break;
480	}
481
482	/* Retrieve the packet length. */
483	len = ntohs(ip->ip_len);
484
485	/*
486	 * Check for additional length bogosity
487	 */
488	if (len < hlen) {
489	 	ipstat.ips_badlen++;
490		goto bad;
491	}
492
493	/*
494	 * Check that the amount of data in the buffers
495	 * is as at least much as the IP header would have us expect.
496	 * Trim mbufs if longer than we expect.
497	 * Drop packet if shorter than we expect.
498	 */
499	if (m->m_pkthdr.len < len) {
500		ipstat.ips_tooshort++;
501		goto bad;
502	}
503	if (m->m_pkthdr.len > len) {
504		if (m->m_len == m->m_pkthdr.len) {
505			m->m_len = len;
506			m->m_pkthdr.len = len;
507		} else
508			m_adj(m, len - m->m_pkthdr.len);
509	}
510
511#ifdef IPSEC
512	/* ipflow (IP fast forwarding) is not compatible with IPsec. */
513	m->m_flags &= ~M_CANFASTFWD;
514#else
515	/*
516	 * Assume that we can create a fast-forward IP flow entry
517	 * based on this packet.
518	 */
519	m->m_flags |= M_CANFASTFWD;
520#endif
521
522#ifdef PFIL_HOOKS
523	/*
524	 * Run through list of hooks for input packets.  If there are any
525	 * filters which require that additional packets in the flow are
526	 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
527	 * Note that filters must _never_ set this flag, as another filter
528	 * in the list may have previously cleared it.
529	 */
530	/*
531	 * let ipfilter look at packet on the wire,
532	 * not the decapsulated packet.
533	 */
534#ifdef IPSEC
535	if (!ipsec_getnhist(m))
536#else
537	if (1)
538#endif
539	{
540		if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
541				   PFIL_IN) != 0)
542		return;
543		if (m == NULL)
544			return;
545		ip = mtod(m, struct ip *);
546		hlen = ip->ip_hl << 2;
547	}
548#endif /* PFIL_HOOKS */
549
550#ifdef ALTQ
551	/* XXX Temporary until ALTQ is changed to use a pfil hook */
552	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) {
553		/* packet dropped by traffic conditioner */
554		return;
555	}
556#endif
557
558	/*
559	 * Convert fields to host representation.
560	 */
561	NTOHS(ip->ip_len);
562	NTOHS(ip->ip_off);
563
564	/*
565	 * Process options and, if not destined for us,
566	 * ship it on.  ip_dooptions returns 1 when an
567	 * error was detected (causing an icmp message
568	 * to be sent and the original packet to be freed).
569	 */
570	ip_nhops = 0;		/* for source routed packets */
571	if (hlen > sizeof (struct ip) && ip_dooptions(m))
572		return;
573
574	/*
575	 * Check our list of addresses, to see if the packet is for us.
576	 *
577	 * Traditional 4.4BSD did not consult IFF_UP at all.
578	 * The behavior here is to treat addresses on !IFF_UP interface
579	 * as not mine.
580	 */
581	downmatch = 0;
582	LIST_FOREACH(ia, &IN_IFADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
583		if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) {
584			if ((ia->ia_ifp->if_flags & IFF_UP) != 0)
585				break;
586			else
587				downmatch++;
588		}
589	}
590	if (ia != NULL)
591		goto ours;
592	if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
593		TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrlist, ifa_list) {
594			if (ifa->ifa_addr->sa_family != AF_INET)
595				continue;
596			ia = ifatoia(ifa);
597			if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
598			    in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
599			    /*
600			     * Look for all-0's host part (old broadcast addr),
601			     * either for subnet or net.
602			     */
603			    ip->ip_dst.s_addr == ia->ia_subnet ||
604			    ip->ip_dst.s_addr == ia->ia_net)
605				goto ours;
606			/*
607			 * An interface with IP address zero accepts
608			 * all packets that arrive on that interface.
609			 */
610			if (in_nullhost(ia->ia_addr.sin_addr))
611				goto ours;
612		}
613	}
614	if (IN_MULTICAST(ip->ip_dst.s_addr)) {
615		struct in_multi *inm;
616#ifdef MROUTING
617		extern struct socket *ip_mrouter;
618
619		if (M_READONLY(m)) {
620			if ((m = m_pullup(m, hlen)) == 0) {
621				ipstat.ips_toosmall++;
622				return;
623			}
624			ip = mtod(m, struct ip *);
625		}
626
627		if (ip_mrouter) {
628			/*
629			 * If we are acting as a multicast router, all
630			 * incoming multicast packets are passed to the
631			 * kernel-level multicast forwarding function.
632			 * The packet is returned (relatively) intact; if
633			 * ip_mforward() returns a non-zero value, the packet
634			 * must be discarded, else it may be accepted below.
635			 *
636			 * (The IP ident field is put in the same byte order
637			 * as expected when ip_mforward() is called from
638			 * ip_output().)
639			 */
640			if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
641				ipstat.ips_cantforward++;
642				m_freem(m);
643				return;
644			}
645
646			/*
647			 * The process-level routing demon needs to receive
648			 * all multicast IGMP packets, whether or not this
649			 * host belongs to their destination groups.
650			 */
651			if (ip->ip_p == IPPROTO_IGMP)
652				goto ours;
653			ipstat.ips_forward++;
654		}
655#endif
656		/*
657		 * See if we belong to the destination multicast group on the
658		 * arrival interface.
659		 */
660		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
661		if (inm == NULL) {
662			ipstat.ips_cantforward++;
663			m_freem(m);
664			return;
665		}
666		goto ours;
667	}
668	if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
669	    in_nullhost(ip->ip_dst))
670		goto ours;
671
672	/*
673	 * Not for us; forward if possible and desirable.
674	 */
675	if (ipforwarding == 0) {
676		ipstat.ips_cantforward++;
677		m_freem(m);
678	} else {
679		/*
680		 * If ip_dst matched any of my address on !IFF_UP interface,
681		 * and there's no IFF_UP interface that matches ip_dst,
682		 * send icmp unreach.  Forwarding it will result in in-kernel
683		 * forwarding loop till TTL goes to 0.
684		 */
685		if (downmatch) {
686			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
687			ipstat.ips_cantforward++;
688			return;
689		}
690#ifdef IPSEC
691		if (ipsec4_in_reject(m, NULL)) {
692			ipsecstat.in_polvio++;
693			goto bad;
694		}
695#endif
696
697		ip_forward(m, 0);
698	}
699	return;
700
701ours:
702	/*
703	 * If offset or IP_MF are set, must reassemble.
704	 * Otherwise, nothing need be done.
705	 * (We could look in the reassembly queue to see
706	 * if the packet was previously fragmented,
707	 * but it's not worth the time; just let them time out.)
708	 */
709	if (ip->ip_off & ~(IP_DF|IP_RF)) {
710		/*
711		 * Look for queue of fragments
712		 * of this datagram.
713		 */
714		IPQ_LOCK();
715		LIST_FOREACH(fp, &ipq, ipq_q)
716			if (ip->ip_id == fp->ipq_id &&
717			    in_hosteq(ip->ip_src, fp->ipq_src) &&
718			    in_hosteq(ip->ip_dst, fp->ipq_dst) &&
719			    ip->ip_p == fp->ipq_p)
720				goto found;
721		fp = 0;
722found:
723
724		/*
725		 * Adjust ip_len to not reflect header,
726		 * set ipqe_mff if more fragments are expected,
727		 * convert offset of this to bytes.
728		 */
729		ip->ip_len -= hlen;
730		mff = (ip->ip_off & IP_MF) != 0;
731		if (mff) {
732		        /*
733		         * Make sure that fragments have a data length
734			 * that's a non-zero multiple of 8 bytes.
735		         */
736			if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
737				ipstat.ips_badfrags++;
738				IPQ_UNLOCK();
739				goto bad;
740			}
741		}
742		ip->ip_off <<= 3;
743
744		/*
745		 * If datagram marked as having more fragments
746		 * or if this is not the first fragment,
747		 * attempt reassembly; if it succeeds, proceed.
748		 */
749		if (mff || ip->ip_off) {
750			ipstat.ips_fragments++;
751			ipqe = pool_get(&ipqent_pool, PR_NOWAIT);
752			if (ipqe == NULL) {
753				ipstat.ips_rcvmemdrop++;
754				IPQ_UNLOCK();
755				goto bad;
756			}
757			ipqe->ipqe_mff = mff;
758			ipqe->ipqe_m = m;
759			ipqe->ipqe_ip = ip;
760			m = ip_reass(ipqe, fp);
761			if (m == 0) {
762				IPQ_UNLOCK();
763				return;
764			}
765			ipstat.ips_reassembled++;
766			ip = mtod(m, struct ip *);
767			hlen = ip->ip_hl << 2;
768			ip->ip_len += hlen;
769		} else
770			if (fp)
771				ip_freef(fp);
772		IPQ_UNLOCK();
773	}
774
775#ifdef IPSEC
776	/*
777	 * enforce IPsec policy checking if we are seeing last header.
778	 * note that we do not visit this with protocols with pcb layer
779	 * code - like udp/tcp/raw ip.
780	 */
781	if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
782	    ipsec4_in_reject(m, NULL)) {
783		ipsecstat.in_polvio++;
784		goto bad;
785	}
786#endif
787
788	/*
789	 * Switch out to protocol's input routine.
790	 */
791#if IFA_STATS
792	if (ia && ip)
793		ia->ia_ifa.ifa_data.ifad_inbytes += ip->ip_len;
794#endif
795	ipstat.ips_delivered++;
796    {
797	int off = hlen, nh = ip->ip_p;
798
799	(*inetsw[ip_protox[nh]].pr_input)(m, off, nh);
800	return;
801    }
802bad:
803	m_freem(m);
804	return;
805
806badcsum:
807	ipstat.ips_badsum++;
808	m_freem(m);
809}
810
811/*
812 * Take incoming datagram fragment and try to
813 * reassemble it into whole datagram.  If a chain for
814 * reassembly of this datagram already exists, then it
815 * is given as fp; otherwise have to make a chain.
816 */
817struct mbuf *
818ip_reass(ipqe, fp)
819	struct ipqent *ipqe;
820	struct ipq *fp;
821{
822	struct mbuf *m = ipqe->ipqe_m;
823	struct ipqent *nq, *p, *q;
824	struct ip *ip;
825	struct mbuf *t;
826	int hlen = ipqe->ipqe_ip->ip_hl << 2;
827	int i, next;
828
829	IPQ_LOCK_CHECK();
830
831	/*
832	 * Presence of header sizes in mbufs
833	 * would confuse code below.
834	 */
835	m->m_data += hlen;
836	m->m_len -= hlen;
837
838	/*
839	 * If first fragment to arrive, create a reassembly queue.
840	 */
841	if (fp == 0) {
842		/*
843		 * Enforce upper bound on number of fragmented packets
844		 * for which we attempt reassembly;
845		 * If maxfrag is 0, never accept fragments.
846		 * If maxfrag is -1, accept all fragments without limitation.
847		 */
848		if (ip_maxfragpackets < 0)
849			;
850		else if (ip_nfragpackets >= ip_maxfragpackets)
851			goto dropfrag;
852		ip_nfragpackets++;
853		MALLOC(fp, struct ipq *, sizeof (struct ipq),
854		    M_FTABLE, M_NOWAIT);
855		if (fp == NULL)
856			goto dropfrag;
857		LIST_INSERT_HEAD(&ipq, fp, ipq_q);
858		fp->ipq_ttl = IPFRAGTTL;
859		fp->ipq_p = ipqe->ipqe_ip->ip_p;
860		fp->ipq_id = ipqe->ipqe_ip->ip_id;
861		TAILQ_INIT(&fp->ipq_fragq);
862		fp->ipq_src = ipqe->ipqe_ip->ip_src;
863		fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
864		p = NULL;
865		goto insert;
866	}
867
868	/*
869	 * Find a segment which begins after this one does.
870	 */
871	for (p = NULL, q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL;
872	    p = q, q = TAILQ_NEXT(q, ipqe_q))
873		if (q->ipqe_ip->ip_off > ipqe->ipqe_ip->ip_off)
874			break;
875
876	/*
877	 * If there is a preceding segment, it may provide some of
878	 * our data already.  If so, drop the data from the incoming
879	 * segment.  If it provides all of our data, drop us.
880	 */
881	if (p != NULL) {
882		i = p->ipqe_ip->ip_off + p->ipqe_ip->ip_len -
883		    ipqe->ipqe_ip->ip_off;
884		if (i > 0) {
885			if (i >= ipqe->ipqe_ip->ip_len)
886				goto dropfrag;
887			m_adj(ipqe->ipqe_m, i);
888			ipqe->ipqe_ip->ip_off += i;
889			ipqe->ipqe_ip->ip_len -= i;
890		}
891	}
892
893	/*
894	 * While we overlap succeeding segments trim them or,
895	 * if they are completely covered, dequeue them.
896	 */
897	for (; q != NULL && ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len >
898	    q->ipqe_ip->ip_off; q = nq) {
899		i = (ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len) -
900		    q->ipqe_ip->ip_off;
901		if (i < q->ipqe_ip->ip_len) {
902			q->ipqe_ip->ip_len -= i;
903			q->ipqe_ip->ip_off += i;
904			m_adj(q->ipqe_m, i);
905			break;
906		}
907		nq = TAILQ_NEXT(q, ipqe_q);
908		m_freem(q->ipqe_m);
909		TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q);
910		pool_put(&ipqent_pool, q);
911	}
912
913insert:
914	/*
915	 * Stick new segment in its place;
916	 * check for complete reassembly.
917	 */
918	if (p == NULL) {
919		TAILQ_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
920	} else {
921		TAILQ_INSERT_AFTER(&fp->ipq_fragq, p, ipqe, ipqe_q);
922	}
923	next = 0;
924	for (p = NULL, q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL;
925	    p = q, q = TAILQ_NEXT(q, ipqe_q)) {
926		if (q->ipqe_ip->ip_off != next)
927			return (0);
928		next += q->ipqe_ip->ip_len;
929	}
930	if (p->ipqe_mff)
931		return (0);
932
933	/*
934	 * Reassembly is complete.  Check for a bogus message size and
935	 * concatenate fragments.
936	 */
937	q = TAILQ_FIRST(&fp->ipq_fragq);
938	ip = q->ipqe_ip;
939	if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
940		ipstat.ips_toolong++;
941		ip_freef(fp);
942		return (0);
943	}
944	m = q->ipqe_m;
945	t = m->m_next;
946	m->m_next = 0;
947	m_cat(m, t);
948	nq = TAILQ_NEXT(q, ipqe_q);
949	pool_put(&ipqent_pool, q);
950	for (q = nq; q != NULL; q = nq) {
951		t = q->ipqe_m;
952		nq = TAILQ_NEXT(q, ipqe_q);
953		pool_put(&ipqent_pool, q);
954		m_cat(m, t);
955	}
956
957	/*
958	 * Create header for new ip packet by
959	 * modifying header of first packet;
960	 * dequeue and discard fragment reassembly header.
961	 * Make header visible.
962	 */
963	ip->ip_len = next;
964	ip->ip_src = fp->ipq_src;
965	ip->ip_dst = fp->ipq_dst;
966	LIST_REMOVE(fp, ipq_q);
967	FREE(fp, M_FTABLE);
968	ip_nfragpackets--;
969	m->m_len += (ip->ip_hl << 2);
970	m->m_data -= (ip->ip_hl << 2);
971	/* some debugging cruft by sklower, below, will go away soon */
972	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
973		int plen = 0;
974		for (t = m; t; t = t->m_next)
975			plen += t->m_len;
976		m->m_pkthdr.len = plen;
977	}
978	return (m);
979
980dropfrag:
981	ipstat.ips_fragdropped++;
982	m_freem(m);
983	pool_put(&ipqent_pool, ipqe);
984	return (0);
985}
986
987/*
988 * Free a fragment reassembly header and all
989 * associated datagrams.
990 */
991void
992ip_freef(fp)
993	struct ipq *fp;
994{
995	struct ipqent *q, *p;
996
997	IPQ_LOCK_CHECK();
998
999	for (q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL; q = p) {
1000		p = TAILQ_NEXT(q, ipqe_q);
1001		m_freem(q->ipqe_m);
1002		TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q);
1003		pool_put(&ipqent_pool, q);
1004	}
1005	LIST_REMOVE(fp, ipq_q);
1006	FREE(fp, M_FTABLE);
1007	ip_nfragpackets--;
1008}
1009
1010/*
1011 * IP timer processing;
1012 * if a timer expires on a reassembly
1013 * queue, discard it.
1014 */
1015void
1016ip_slowtimo()
1017{
1018	struct ipq *fp, *nfp;
1019	int s = splsoftnet();
1020
1021	IPQ_LOCK();
1022	for (fp = LIST_FIRST(&ipq); fp != NULL; fp = nfp) {
1023		nfp = LIST_NEXT(fp, ipq_q);
1024		if (--fp->ipq_ttl == 0) {
1025			ipstat.ips_fragtimeout++;
1026			ip_freef(fp);
1027		}
1028	}
1029	/*
1030	 * If we are over the maximum number of fragments
1031	 * (due to the limit being lowered), drain off
1032	 * enough to get down to the new limit.
1033	 */
1034	if (ip_maxfragpackets < 0)
1035		;
1036	else {
1037		while (ip_nfragpackets > ip_maxfragpackets && LIST_FIRST(&ipq))
1038			ip_freef(LIST_FIRST(&ipq));
1039	}
1040	IPQ_UNLOCK();
1041#ifdef GATEWAY
1042	ipflow_slowtimo();
1043#endif
1044	splx(s);
1045}
1046
1047/*
1048 * Drain off all datagram fragments.
1049 */
1050void
1051ip_drain()
1052{
1053
1054	/*
1055	 * We may be called from a device's interrupt context.  If
1056	 * the ipq is already busy, just bail out now.
1057	 */
1058	if (ipq_lock_try() == 0)
1059		return;
1060
1061	while (LIST_FIRST(&ipq) != NULL) {
1062		ipstat.ips_fragdropped++;
1063		ip_freef(LIST_FIRST(&ipq));
1064	}
1065
1066	IPQ_UNLOCK();
1067}
1068
1069/*
1070 * Do option processing on a datagram,
1071 * possibly discarding it if bad options are encountered,
1072 * or forwarding it if source-routed.
1073 * Returns 1 if packet has been forwarded/freed,
1074 * 0 if the packet should be processed further.
1075 */
1076int
1077ip_dooptions(m)
1078	struct mbuf *m;
1079{
1080	struct ip *ip = mtod(m, struct ip *);
1081	u_char *cp, *cp0;
1082	struct ip_timestamp *ipt;
1083	struct in_ifaddr *ia;
1084	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1085	struct in_addr dst;
1086	n_time ntime;
1087
1088	dst = ip->ip_dst;
1089	cp = (u_char *)(ip + 1);
1090	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1091	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1092		opt = cp[IPOPT_OPTVAL];
1093		if (opt == IPOPT_EOL)
1094			break;
1095		if (opt == IPOPT_NOP)
1096			optlen = 1;
1097		else {
1098			if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1099				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1100				goto bad;
1101			}
1102			optlen = cp[IPOPT_OLEN];
1103			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1104				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1105				goto bad;
1106			}
1107		}
1108		switch (opt) {
1109
1110		default:
1111			break;
1112
1113		/*
1114		 * Source routing with record.
1115		 * Find interface with current destination address.
1116		 * If none on this machine then drop if strictly routed,
1117		 * or do nothing if loosely routed.
1118		 * Record interface address and bring up next address
1119		 * component.  If strictly routed make sure next
1120		 * address is on directly accessible net.
1121		 */
1122		case IPOPT_LSRR:
1123		case IPOPT_SSRR:
1124			if (ip_allowsrcrt == 0) {
1125				type = ICMP_UNREACH;
1126				code = ICMP_UNREACH_NET_PROHIB;
1127				goto bad;
1128			}
1129			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1130				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1131				goto bad;
1132			}
1133			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1134				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1135				goto bad;
1136			}
1137			ipaddr.sin_addr = ip->ip_dst;
1138			ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
1139			if (ia == 0) {
1140				if (opt == IPOPT_SSRR) {
1141					type = ICMP_UNREACH;
1142					code = ICMP_UNREACH_SRCFAIL;
1143					goto bad;
1144				}
1145				/*
1146				 * Loose routing, and not at next destination
1147				 * yet; nothing to do except forward.
1148				 */
1149				break;
1150			}
1151			off--;			/* 0 origin */
1152			if ((off + sizeof(struct in_addr)) > optlen) {
1153				/*
1154				 * End of source route.  Should be for us.
1155				 */
1156				save_rte(cp, ip->ip_src);
1157				break;
1158			}
1159			/*
1160			 * locate outgoing interface
1161			 */
1162			bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
1163			    sizeof(ipaddr.sin_addr));
1164			if (opt == IPOPT_SSRR)
1165				ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
1166			else
1167				ia = ip_rtaddr(ipaddr.sin_addr);
1168			if (ia == 0) {
1169				type = ICMP_UNREACH;
1170				code = ICMP_UNREACH_SRCFAIL;
1171				goto bad;
1172			}
1173			ip->ip_dst = ipaddr.sin_addr;
1174			bcopy((caddr_t)&ia->ia_addr.sin_addr,
1175			    (caddr_t)(cp + off), sizeof(struct in_addr));
1176			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1177			/*
1178			 * Let ip_intr's mcast routing check handle mcast pkts
1179			 */
1180			forward = !IN_MULTICAST(ip->ip_dst.s_addr);
1181			break;
1182
1183		case IPOPT_RR:
1184			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1185				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1186				goto bad;
1187			}
1188			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1189				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1190				goto bad;
1191			}
1192			/*
1193			 * If no space remains, ignore.
1194			 */
1195			off--;			/* 0 origin */
1196			if ((off + sizeof(struct in_addr)) > optlen)
1197				break;
1198			bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
1199			    sizeof(ipaddr.sin_addr));
1200			/*
1201			 * locate outgoing interface; if we're the destination,
1202			 * use the incoming interface (should be same).
1203			 */
1204			if ((ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))))
1205			    == NULL &&
1206			    (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) {
1207				type = ICMP_UNREACH;
1208				code = ICMP_UNREACH_HOST;
1209				goto bad;
1210			}
1211			bcopy((caddr_t)&ia->ia_addr.sin_addr,
1212			    (caddr_t)(cp + off), sizeof(struct in_addr));
1213			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1214			break;
1215
1216		case IPOPT_TS:
1217			code = cp - (u_char *)ip;
1218			ipt = (struct ip_timestamp *)cp;
1219			if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
1220				code = (u_char *)&ipt->ipt_len - (u_char *)ip;
1221				goto bad;
1222			}
1223			if (ipt->ipt_ptr < 5) {
1224				code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
1225				goto bad;
1226			}
1227			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
1228				if (++ipt->ipt_oflw == 0) {
1229					code = (u_char *)&ipt->ipt_ptr -
1230					    (u_char *)ip;
1231					goto bad;
1232				}
1233				break;
1234			}
1235			cp0 = (cp + ipt->ipt_ptr - 1);
1236			switch (ipt->ipt_flg) {
1237
1238			case IPOPT_TS_TSONLY:
1239				break;
1240
1241			case IPOPT_TS_TSANDADDR:
1242				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1243				    sizeof(struct in_addr) > ipt->ipt_len) {
1244					code = (u_char *)&ipt->ipt_ptr -
1245					    (u_char *)ip;
1246					goto bad;
1247				}
1248				ipaddr.sin_addr = dst;
1249				ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr),
1250				    m->m_pkthdr.rcvif));
1251				if (ia == 0)
1252					continue;
1253				bcopy(&ia->ia_addr.sin_addr,
1254				    cp0, sizeof(struct in_addr));
1255				ipt->ipt_ptr += sizeof(struct in_addr);
1256				break;
1257
1258			case IPOPT_TS_PRESPEC:
1259				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1260				    sizeof(struct in_addr) > ipt->ipt_len) {
1261					code = (u_char *)&ipt->ipt_ptr -
1262					    (u_char *)ip;
1263					goto bad;
1264				}
1265				bcopy(cp0, &ipaddr.sin_addr,
1266				    sizeof(struct in_addr));
1267				if (ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)))
1268				    == NULL)
1269					continue;
1270				ipt->ipt_ptr += sizeof(struct in_addr);
1271				break;
1272
1273			default:
1274				/* XXX can't take &ipt->ipt_flg */
1275				code = (u_char *)&ipt->ipt_ptr -
1276				    (u_char *)ip + 1;
1277				goto bad;
1278			}
1279			ntime = iptime();
1280			cp0 = (u_char *) &ntime; /* XXX grumble, GCC... */
1281			bcopy(cp0, (caddr_t)cp + ipt->ipt_ptr - 1,
1282			    sizeof(n_time));
1283			ipt->ipt_ptr += sizeof(n_time);
1284		}
1285	}
1286	if (forward) {
1287		if (ip_forwsrcrt == 0) {
1288			type = ICMP_UNREACH;
1289			code = ICMP_UNREACH_SRCFAIL;
1290			goto bad;
1291		}
1292		ip_forward(m, 1);
1293		return (1);
1294	}
1295	return (0);
1296bad:
1297	icmp_error(m, type, code, 0, 0);
1298	ipstat.ips_badoptions++;
1299	return (1);
1300}
1301
1302/*
1303 * Given address of next destination (final or next hop),
1304 * return internet address info of interface to be used to get there.
1305 */
1306struct in_ifaddr *
1307ip_rtaddr(dst)
1308	 struct in_addr dst;
1309{
1310	struct sockaddr_in *sin;
1311
1312	sin = satosin(&ipforward_rt.ro_dst);
1313
1314	if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) {
1315		if (ipforward_rt.ro_rt) {
1316			RTFREE(ipforward_rt.ro_rt);
1317			ipforward_rt.ro_rt = 0;
1318		}
1319		sin->sin_family = AF_INET;
1320		sin->sin_len = sizeof(*sin);
1321		sin->sin_addr = dst;
1322
1323		rtalloc(&ipforward_rt);
1324	}
1325	if (ipforward_rt.ro_rt == 0)
1326		return ((struct in_ifaddr *)0);
1327	return (ifatoia(ipforward_rt.ro_rt->rt_ifa));
1328}
1329
1330/*
1331 * Save incoming source route for use in replies,
1332 * to be picked up later by ip_srcroute if the receiver is interested.
1333 */
1334void
1335save_rte(option, dst)
1336	u_char *option;
1337	struct in_addr dst;
1338{
1339	unsigned olen;
1340
1341	olen = option[IPOPT_OLEN];
1342#ifdef DIAGNOSTIC
1343	if (ipprintfs)
1344		printf("save_rte: olen %d\n", olen);
1345#endif /* 0 */
1346	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1347		return;
1348	bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
1349	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1350	ip_srcrt.dst = dst;
1351}
1352
1353/*
1354 * Retrieve incoming source route for use in replies,
1355 * in the same form used by setsockopt.
1356 * The first hop is placed before the options, will be removed later.
1357 */
1358struct mbuf *
1359ip_srcroute()
1360{
1361	struct in_addr *p, *q;
1362	struct mbuf *m;
1363
1364	if (ip_nhops == 0)
1365		return ((struct mbuf *)0);
1366	m = m_get(M_DONTWAIT, MT_SOOPTS);
1367	if (m == 0)
1368		return ((struct mbuf *)0);
1369
1370#define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1371
1372	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1373	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1374	    OPTSIZ;
1375#ifdef DIAGNOSTIC
1376	if (ipprintfs)
1377		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1378#endif
1379
1380	/*
1381	 * First save first hop for return route
1382	 */
1383	p = &ip_srcrt.route[ip_nhops - 1];
1384	*(mtod(m, struct in_addr *)) = *p--;
1385#ifdef DIAGNOSTIC
1386	if (ipprintfs)
1387		printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
1388#endif
1389
1390	/*
1391	 * Copy option fields and padding (nop) to mbuf.
1392	 */
1393	ip_srcrt.nop = IPOPT_NOP;
1394	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1395	bcopy((caddr_t)&ip_srcrt.nop,
1396	    mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
1397	q = (struct in_addr *)(mtod(m, caddr_t) +
1398	    sizeof(struct in_addr) + OPTSIZ);
1399#undef OPTSIZ
1400	/*
1401	 * Record return path as an IP source route,
1402	 * reversing the path (pointers are now aligned).
1403	 */
1404	while (p >= ip_srcrt.route) {
1405#ifdef DIAGNOSTIC
1406		if (ipprintfs)
1407			printf(" %x", ntohl(q->s_addr));
1408#endif
1409		*q++ = *p--;
1410	}
1411	/*
1412	 * Last hop goes to final destination.
1413	 */
1414	*q = ip_srcrt.dst;
1415#ifdef DIAGNOSTIC
1416	if (ipprintfs)
1417		printf(" %x\n", ntohl(q->s_addr));
1418#endif
1419	return (m);
1420}
1421
1422/*
1423 * Strip out IP options, at higher
1424 * level protocol in the kernel.
1425 * Second argument is buffer to which options
1426 * will be moved, and return value is their length.
1427 * XXX should be deleted; last arg currently ignored.
1428 */
1429void
1430ip_stripoptions(m, mopt)
1431	struct mbuf *m;
1432	struct mbuf *mopt;
1433{
1434	int i;
1435	struct ip *ip = mtod(m, struct ip *);
1436	caddr_t opts;
1437	int olen;
1438
1439	olen = (ip->ip_hl << 2) - sizeof (struct ip);
1440	opts = (caddr_t)(ip + 1);
1441	i = m->m_len - (sizeof (struct ip) + olen);
1442	bcopy(opts  + olen, opts, (unsigned)i);
1443	m->m_len -= olen;
1444	if (m->m_flags & M_PKTHDR)
1445		m->m_pkthdr.len -= olen;
1446	ip->ip_len -= olen;
1447	ip->ip_hl = sizeof (struct ip) >> 2;
1448}
1449
1450const int inetctlerrmap[PRC_NCMDS] = {
1451	0,		0,		0,		0,
1452	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1453	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1454	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1455	0,		0,		0,		0,
1456	ENOPROTOOPT
1457};
1458
1459/*
1460 * Forward a packet.  If some error occurs return the sender
1461 * an icmp packet.  Note we can't always generate a meaningful
1462 * icmp message because icmp doesn't have a large enough repertoire
1463 * of codes and types.
1464 *
1465 * If not forwarding, just drop the packet.  This could be confusing
1466 * if ipforwarding was zero but some routing protocol was advancing
1467 * us as a gateway to somewhere.  However, we must let the routing
1468 * protocol deal with that.
1469 *
1470 * The srcrt parameter indicates whether the packet is being forwarded
1471 * via a source route.
1472 */
1473void
1474ip_forward(m, srcrt)
1475	struct mbuf *m;
1476	int srcrt;
1477{
1478	struct ip *ip = mtod(m, struct ip *);
1479	struct sockaddr_in *sin;
1480	struct rtentry *rt;
1481	int error, type = 0, code = 0;
1482	struct mbuf *mcopy;
1483	n_long dest;
1484	struct ifnet *destifp;
1485#ifdef IPSEC
1486	struct ifnet dummyifp;
1487#endif
1488
1489	/*
1490	 * Clear any in-bound checksum flags for this packet.
1491	 */
1492	m->m_pkthdr.csum_flags = 0;
1493
1494	dest = 0;
1495#ifdef DIAGNOSTIC
1496	if (ipprintfs)
1497		printf("forward: src %2.2x dst %2.2x ttl %x\n",
1498		    ntohl(ip->ip_src.s_addr),
1499		    ntohl(ip->ip_dst.s_addr), ip->ip_ttl);
1500#endif
1501	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1502		ipstat.ips_cantforward++;
1503		m_freem(m);
1504		return;
1505	}
1506	if (ip->ip_ttl <= IPTTLDEC) {
1507		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1508		return;
1509	}
1510	ip->ip_ttl -= IPTTLDEC;
1511
1512	sin = satosin(&ipforward_rt.ro_dst);
1513	if ((rt = ipforward_rt.ro_rt) == 0 ||
1514	    !in_hosteq(ip->ip_dst, sin->sin_addr)) {
1515		if (ipforward_rt.ro_rt) {
1516			RTFREE(ipforward_rt.ro_rt);
1517			ipforward_rt.ro_rt = 0;
1518		}
1519		sin->sin_family = AF_INET;
1520		sin->sin_len = sizeof(struct sockaddr_in);
1521		sin->sin_addr = ip->ip_dst;
1522
1523		rtalloc(&ipforward_rt);
1524		if (ipforward_rt.ro_rt == 0) {
1525			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1526			return;
1527		}
1528		rt = ipforward_rt.ro_rt;
1529	}
1530
1531	/*
1532	 * Save at most 68 bytes of the packet in case
1533	 * we need to generate an ICMP message to the src.
1534	 * Pullup to avoid sharing mbuf cluster between m and mcopy.
1535	 */
1536	mcopy = m_copym(m, 0, imin((int)ip->ip_len, 68), M_DONTWAIT);
1537	if (mcopy)
1538		mcopy = m_pullup(mcopy, ip->ip_hl << 2);
1539
1540	/*
1541	 * If forwarding packet using same interface that it came in on,
1542	 * perhaps should send a redirect to sender to shortcut a hop.
1543	 * Only send redirect if source is sending directly to us,
1544	 * and if packet was not source routed (or has any options).
1545	 * Also, don't send redirect if forwarding using a default route
1546	 * or a route modified by a redirect.
1547	 */
1548	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1549	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1550	    !in_nullhost(satosin(rt_key(rt))->sin_addr) &&
1551	    ipsendredirects && !srcrt) {
1552		if (rt->rt_ifa &&
1553		    (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
1554		    ifatoia(rt->rt_ifa)->ia_subnet) {
1555			if (rt->rt_flags & RTF_GATEWAY)
1556				dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1557			else
1558				dest = ip->ip_dst.s_addr;
1559			/*
1560			 * Router requirements says to only send host
1561			 * redirects.
1562			 */
1563			type = ICMP_REDIRECT;
1564			code = ICMP_REDIRECT_HOST;
1565#ifdef DIAGNOSTIC
1566			if (ipprintfs)
1567				printf("redirect (%d) to %x\n", code,
1568				    (u_int32_t)dest);
1569#endif
1570		}
1571	}
1572
1573#ifdef IPSEC
1574	/* Don't lookup socket in forwarding case */
1575	(void)ipsec_setsocket(m, NULL);
1576#endif
1577	error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1578	    (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 0);
1579	if (error)
1580		ipstat.ips_cantforward++;
1581	else {
1582		ipstat.ips_forward++;
1583		if (type)
1584			ipstat.ips_redirectsent++;
1585		else {
1586			if (mcopy) {
1587#ifdef GATEWAY
1588				if (mcopy->m_flags & M_CANFASTFWD)
1589					ipflow_create(&ipforward_rt, mcopy);
1590#endif
1591				m_freem(mcopy);
1592			}
1593			return;
1594		}
1595	}
1596	if (mcopy == NULL)
1597		return;
1598	destifp = NULL;
1599
1600	switch (error) {
1601
1602	case 0:				/* forwarded, but need redirect */
1603		/* type, code set above */
1604		break;
1605
1606	case ENETUNREACH:		/* shouldn't happen, checked above */
1607	case EHOSTUNREACH:
1608	case ENETDOWN:
1609	case EHOSTDOWN:
1610	default:
1611		type = ICMP_UNREACH;
1612		code = ICMP_UNREACH_HOST;
1613		break;
1614
1615	case EMSGSIZE:
1616		type = ICMP_UNREACH;
1617		code = ICMP_UNREACH_NEEDFRAG;
1618#ifndef IPSEC
1619		if (ipforward_rt.ro_rt)
1620			destifp = ipforward_rt.ro_rt->rt_ifp;
1621#else
1622		/*
1623		 * If the packet is routed over IPsec tunnel, tell the
1624		 * originator the tunnel MTU.
1625		 *	tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1626		 * XXX quickhack!!!
1627		 */
1628		if (ipforward_rt.ro_rt) {
1629			struct secpolicy *sp;
1630			int ipsecerror;
1631			size_t ipsechdr;
1632			struct route *ro;
1633
1634			sp = ipsec4_getpolicybyaddr(mcopy,
1635			                            IPSEC_DIR_OUTBOUND,
1636			                            IP_FORWARDING,
1637			                            &ipsecerror);
1638
1639			if (sp == NULL)
1640				destifp = ipforward_rt.ro_rt->rt_ifp;
1641			else {
1642				/* count IPsec header size */
1643				ipsechdr = ipsec4_hdrsiz(mcopy,
1644				                         IPSEC_DIR_OUTBOUND,
1645				                         NULL);
1646
1647				/*
1648				 * find the correct route for outer IPv4
1649				 * header, compute tunnel MTU.
1650				 *
1651				 * XXX BUG ALERT
1652				 * The "dummyifp" code relies upon the fact
1653				 * that icmp_error() touches only ifp->if_mtu.
1654				 */
1655				/*XXX*/
1656				destifp = NULL;
1657				if (sp->req != NULL
1658				 && sp->req->sav != NULL
1659				 && sp->req->sav->sah != NULL) {
1660					ro = &sp->req->sav->sah->sa_route;
1661					if (ro->ro_rt && ro->ro_rt->rt_ifp) {
1662						dummyifp.if_mtu =
1663						    ro->ro_rt->rt_ifp->if_mtu;
1664						dummyifp.if_mtu -= ipsechdr;
1665						destifp = &dummyifp;
1666					}
1667				}
1668
1669				key_freesp(sp);
1670			}
1671		}
1672#endif /*IPSEC*/
1673		ipstat.ips_cantfrag++;
1674		break;
1675
1676	case ENOBUFS:
1677#if 1
1678		/*
1679		 * a router should not generate ICMP_SOURCEQUENCH as
1680		 * required in RFC1812 Requirements for IP Version 4 Routers.
1681		 * source quench could be a big problem under DoS attacks,
1682		 * or if the underlying interface is rate-limited.
1683		 */
1684		if (mcopy)
1685			m_freem(mcopy);
1686		return;
1687#else
1688		type = ICMP_SOURCEQUENCH;
1689		code = 0;
1690		break;
1691#endif
1692	}
1693	icmp_error(mcopy, type, code, dest, destifp);
1694}
1695
1696void
1697ip_savecontrol(inp, mp, ip, m)
1698	struct inpcb *inp;
1699	struct mbuf **mp;
1700	struct ip *ip;
1701	struct mbuf *m;
1702{
1703
1704	if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1705		struct timeval tv;
1706
1707		microtime(&tv);
1708		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1709		    SCM_TIMESTAMP, SOL_SOCKET);
1710		if (*mp)
1711			mp = &(*mp)->m_next;
1712	}
1713	if (inp->inp_flags & INP_RECVDSTADDR) {
1714		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1715		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1716		if (*mp)
1717			mp = &(*mp)->m_next;
1718	}
1719#ifdef notyet
1720	/*
1721	 * XXX
1722	 * Moving these out of udp_input() made them even more broken
1723	 * than they already were.
1724	 *	- fenner@parc.xerox.com
1725	 */
1726	/* options were tossed already */
1727	if (inp->inp_flags & INP_RECVOPTS) {
1728		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1729		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1730		if (*mp)
1731			mp = &(*mp)->m_next;
1732	}
1733	/* ip_srcroute doesn't do what we want here, need to fix */
1734	if (inp->inp_flags & INP_RECVRETOPTS) {
1735		*mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1736		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1737		if (*mp)
1738			mp = &(*mp)->m_next;
1739	}
1740#endif
1741	if (inp->inp_flags & INP_RECVIF) {
1742		struct sockaddr_dl sdl;
1743
1744		sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]);
1745		sdl.sdl_family = AF_LINK;
1746		sdl.sdl_index = m->m_pkthdr.rcvif ?
1747		    m->m_pkthdr.rcvif->if_index : 0;
1748		sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0;
1749		*mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len,
1750		    IP_RECVIF, IPPROTO_IP);
1751		if (*mp)
1752			mp = &(*mp)->m_next;
1753	}
1754}
1755
1756int
1757ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1758	int *name;
1759	u_int namelen;
1760	void *oldp;
1761	size_t *oldlenp;
1762	void *newp;
1763	size_t newlen;
1764{
1765	extern int subnetsarelocal, hostzeroisbroadcast;
1766
1767	int error, old;
1768
1769	/* All sysctl names at this level are terminal. */
1770	if (namelen != 1)
1771		return (ENOTDIR);
1772
1773	switch (name[0]) {
1774	case IPCTL_FORWARDING:
1775		return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding));
1776	case IPCTL_SENDREDIRECTS:
1777		return (sysctl_int(oldp, oldlenp, newp, newlen,
1778			&ipsendredirects));
1779	case IPCTL_DEFTTL:
1780		return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl));
1781#ifdef notyet
1782	case IPCTL_DEFMTU:
1783		return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu));
1784#endif
1785	case IPCTL_FORWSRCRT:
1786		/* Don't allow this to change in a secure environment.  */
1787		if (securelevel > 0)
1788			return (sysctl_rdint(oldp, oldlenp, newp,
1789			    ip_forwsrcrt));
1790		else
1791			return (sysctl_int(oldp, oldlenp, newp, newlen,
1792			    &ip_forwsrcrt));
1793	case IPCTL_DIRECTEDBCAST:
1794		return (sysctl_int(oldp, oldlenp, newp, newlen,
1795		    &ip_directedbcast));
1796	case IPCTL_ALLOWSRCRT:
1797		return (sysctl_int(oldp, oldlenp, newp, newlen,
1798		    &ip_allowsrcrt));
1799	case IPCTL_SUBNETSARELOCAL:
1800		return (sysctl_int(oldp, oldlenp, newp, newlen,
1801		    &subnetsarelocal));
1802	case IPCTL_MTUDISC:
1803		error = sysctl_int(oldp, oldlenp, newp, newlen,
1804		    &ip_mtudisc);
1805		if (ip_mtudisc != 0 && ip_mtudisc_timeout_q == NULL) {
1806			ip_mtudisc_timeout_q =
1807			    rt_timer_queue_create(ip_mtudisc_timeout);
1808		} else if (ip_mtudisc == 0 && ip_mtudisc_timeout_q != NULL) {
1809			rt_timer_queue_destroy(ip_mtudisc_timeout_q, TRUE);
1810			ip_mtudisc_timeout_q = NULL;
1811		}
1812		return error;
1813	case IPCTL_ANONPORTMIN:
1814		old = anonportmin;
1815		error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmin);
1816		if (anonportmin >= anonportmax || anonportmin < 0
1817		    || anonportmin > 65535
1818#ifndef IPNOPRIVPORTS
1819		    || anonportmin < IPPORT_RESERVED
1820#endif
1821		    ) {
1822			anonportmin = old;
1823			return (EINVAL);
1824		}
1825		return (error);
1826	case IPCTL_ANONPORTMAX:
1827		old = anonportmax;
1828		error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmax);
1829		if (anonportmin >= anonportmax || anonportmax < 0
1830		    || anonportmax > 65535
1831#ifndef IPNOPRIVPORTS
1832		    || anonportmax < IPPORT_RESERVED
1833#endif
1834		    ) {
1835			anonportmax = old;
1836			return (EINVAL);
1837		}
1838		return (error);
1839	case IPCTL_MTUDISCTIMEOUT:
1840		error = sysctl_int(oldp, oldlenp, newp, newlen,
1841		   &ip_mtudisc_timeout);
1842		if (ip_mtudisc_timeout_q != NULL)
1843			rt_timer_queue_change(ip_mtudisc_timeout_q,
1844					      ip_mtudisc_timeout);
1845		return (error);
1846#ifdef GATEWAY
1847	case IPCTL_MAXFLOWS:
1848	    {
1849		int s;
1850
1851		error = sysctl_int(oldp, oldlenp, newp, newlen,
1852		   &ip_maxflows);
1853		s = splsoftnet();
1854		ipflow_reap(0);
1855		splx(s);
1856		return (error);
1857	    }
1858#endif
1859	case IPCTL_HOSTZEROBROADCAST:
1860		return (sysctl_int(oldp, oldlenp, newp, newlen,
1861		    &hostzeroisbroadcast));
1862#if NGIF > 0
1863	case IPCTL_GIF_TTL:
1864		return(sysctl_int(oldp, oldlenp, newp, newlen,
1865				  &ip_gif_ttl));
1866#endif
1867
1868#if NGRE > 0
1869	case IPCTL_GRE_TTL:
1870		return(sysctl_int(oldp, oldlenp, newp, newlen,
1871				  &ip_gre_ttl));
1872#endif
1873
1874#ifndef IPNOPRIVPORTS
1875	case IPCTL_LOWPORTMIN:
1876		old = lowportmin;
1877		error = sysctl_int(oldp, oldlenp, newp, newlen, &lowportmin);
1878		if (lowportmin >= lowportmax
1879		    || lowportmin > IPPORT_RESERVEDMAX
1880		    || lowportmin < IPPORT_RESERVEDMIN
1881		    ) {
1882			lowportmin = old;
1883			return (EINVAL);
1884		}
1885		return (error);
1886	case IPCTL_LOWPORTMAX:
1887		old = lowportmax;
1888		error = sysctl_int(oldp, oldlenp, newp, newlen, &lowportmax);
1889		if (lowportmin >= lowportmax
1890		    || lowportmax > IPPORT_RESERVEDMAX
1891		    || lowportmax < IPPORT_RESERVEDMIN
1892		    ) {
1893			lowportmax = old;
1894			return (EINVAL);
1895		}
1896		return (error);
1897#endif
1898
1899	case IPCTL_MAXFRAGPACKETS:
1900		return (sysctl_int(oldp, oldlenp, newp, newlen,
1901		    &ip_maxfragpackets));
1902
1903	default:
1904		return (EOPNOTSUPP);
1905	}
1906	/* NOTREACHED */
1907}
1908