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