ip_input.c revision 1.81
1/*	$NetBSD: ip_input.c,v 1.81 1999/03/26 08:51:36 proff Exp $	*/
2
3/*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Public Access Networks Corporation ("Panix").  It was developed under
9 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the NetBSD
22 *	Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 *    contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40/*
41 * Copyright (c) 1982, 1986, 1988, 1993
42 *	The Regents of the University of California.  All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 *    notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 *    notice, this list of conditions and the following disclaimer in the
51 *    documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 *    must display the following acknowledgement:
54 *	This product includes software developed by the University of
55 *	California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 *    may be used to endorse or promote products derived from this software
58 *    without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
73 */
74
75#include "opt_gateway.h"
76#include "opt_pfil_hooks.h"
77#include "opt_mrouting.h"
78
79#include <sys/param.h>
80#include <sys/systm.h>
81#include <sys/malloc.h>
82#include <sys/mbuf.h>
83#include <sys/domain.h>
84#include <sys/protosw.h>
85#include <sys/socket.h>
86#include <sys/socketvar.h>
87#include <sys/errno.h>
88#include <sys/time.h>
89#include <sys/kernel.h>
90#include <sys/proc.h>
91#include <sys/pool.h>
92
93#include <vm/vm.h>
94#include <sys/sysctl.h>
95
96#include <net/if.h>
97#include <net/if_dl.h>
98#include <net/route.h>
99#include <net/pfil.h>
100
101#include <netinet/in.h>
102#include <netinet/in_systm.h>
103#include <netinet/ip.h>
104#include <netinet/in_pcb.h>
105#include <netinet/in_var.h>
106#include <netinet/ip_var.h>
107#include <netinet/ip_icmp.h>
108
109#ifndef	IPFORWARDING
110#ifdef GATEWAY
111#define	IPFORWARDING	1	/* forward IP packets not for us */
112#else /* GATEWAY */
113#define	IPFORWARDING	0	/* don't forward IP packets not for us */
114#endif /* GATEWAY */
115#endif /* IPFORWARDING */
116#ifndef	IPSENDREDIRECTS
117#define	IPSENDREDIRECTS	1
118#endif
119#ifndef IPFORWSRCRT
120#define	IPFORWSRCRT	1	/* forward source-routed packets */
121#endif
122#ifndef IPALLOWSRCRT
123#define	IPALLOWSRCRT	1	/* allow source-routed packets */
124#endif
125#ifndef IPMTUDISC
126#define IPMTUDISC	0
127#endif
128#ifndef IPMTUDISCTIMEOUT
129#define IPMTUDISCTIMEOUT (10 * 60)	/* as per RFC 1191 */
130#endif
131
132/*
133 * Note: DIRECTED_BROADCAST is handled this way so that previous
134 * configuration using this option will Just Work.
135 */
136#ifndef IPDIRECTEDBCAST
137#ifdef DIRECTED_BROADCAST
138#define IPDIRECTEDBCAST	1
139#else
140#define	IPDIRECTEDBCAST	0
141#endif /* DIRECTED_BROADCAST */
142#endif /* IPDIRECTEDBCAST */
143int	ipforwarding = IPFORWARDING;
144int	ipsendredirects = IPSENDREDIRECTS;
145int	ip_defttl = IPDEFTTL;
146int	ip_forwsrcrt = IPFORWSRCRT;
147int	ip_directedbcast = IPDIRECTEDBCAST;
148int	ip_allowsrcrt = IPALLOWSRCRT;
149int	ip_mtudisc = IPMTUDISC;
150u_int	ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
151#ifdef DIAGNOSTIC
152int	ipprintfs = 0;
153#endif
154
155struct rttimer_queue *ip_mtudisc_timeout_q = NULL;
156
157extern	struct domain inetdomain;
158extern	struct protosw inetsw[];
159u_char	ip_protox[IPPROTO_MAX];
160int	ipqmaxlen = IFQ_MAXLEN;
161struct	in_ifaddrhead in_ifaddr;
162struct	in_ifaddrhashhead *in_ifaddrhashtbl;
163struct	ifqueue ipintrq;
164struct	ipstat	ipstat;
165u_int16_t	ip_id;
166int	ip_defttl;
167
168struct ipqhead ipq;
169int	ipq_locked;
170
171static __inline int ipq_lock_try __P((void));
172static __inline void ipq_unlock __P((void));
173
174static __inline int
175ipq_lock_try()
176{
177	int s;
178
179	s = splimp();
180	if (ipq_locked) {
181		splx(s);
182		return (0);
183	}
184	ipq_locked = 1;
185	splx(s);
186	return (1);
187}
188
189static __inline void
190ipq_unlock()
191{
192	int s;
193
194	s = splimp();
195	ipq_locked = 0;
196	splx(s);
197}
198
199#ifdef DIAGNOSTIC
200#define	IPQ_LOCK()							\
201do {									\
202	if (ipq_lock_try() == 0) {					\
203		printf("%s:%d: ipq already locked\n", __FILE__, __LINE__); \
204		panic("ipq_lock");					\
205	}								\
206} while (0)
207#define	IPQ_LOCK_CHECK()						\
208do {									\
209	if (ipq_locked == 0) {						\
210		printf("%s:%d: ipq lock not held\n", __FILE__, __LINE__); \
211		panic("ipq lock check");				\
212	}								\
213} while (0)
214#else
215#define	IPQ_LOCK()		(void) ipq_lock_try()
216#define	IPQ_LOCK_CHECK()	/* nothing */
217#endif
218
219#define	IPQ_UNLOCK()		ipq_unlock()
220
221struct pool ipqent_pool;
222
223/*
224 * We need to save the IP options in case a protocol wants to respond
225 * to an incoming packet over the same route if the packet got here
226 * using IP source routing.  This allows connection establishment and
227 * maintenance when the remote end is on a network that is not known
228 * to us.
229 */
230int	ip_nhops = 0;
231static	struct ip_srcrt {
232	struct	in_addr dst;			/* final destination */
233	char	nop;				/* one NOP to align */
234	char	srcopt[IPOPT_OFFSET + 1];	/* OPTVAL, OLEN and OFFSET */
235	struct	in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
236} ip_srcrt;
237
238static void save_rte __P((u_char *, struct in_addr));
239
240/*
241 * IP initialization: fill in IP protocol switch table.
242 * All protocols not implemented in kernel go to raw IP protocol handler.
243 */
244void
245ip_init()
246{
247	register struct protosw *pr;
248	register int i;
249
250	pool_init(&ipqent_pool, sizeof(struct ipqent), 0, 0, 0, "ipqepl",
251	    0, NULL, NULL, M_IPQ);
252
253	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
254	if (pr == 0)
255		panic("ip_init");
256	for (i = 0; i < IPPROTO_MAX; i++)
257		ip_protox[i] = pr - inetsw;
258	for (pr = inetdomain.dom_protosw;
259	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
260		if (pr->pr_domain->dom_family == PF_INET &&
261		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
262			ip_protox[pr->pr_protocol] = pr - inetsw;
263	LIST_INIT(&ipq);
264	ip_id = time.tv_sec & 0xffff;
265	ipintrq.ifq_maxlen = ipqmaxlen;
266	TAILQ_INIT(&in_ifaddr);
267	in_ifaddrhashtbl =
268	    hashinit(IN_IFADDR_HASH_SIZE, M_IFADDR, M_WAITOK, &in_ifaddrhash);
269	if (ip_mtudisc != 0)
270		ip_mtudisc_timeout_q =
271		    rt_timer_queue_create(ip_mtudisc_timeout);
272#ifdef GATEWAY
273	ipflow_init();
274#endif
275}
276
277struct	sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
278struct	route ipforward_rt;
279
280/*
281 * Ip input routine.  Checksum and byte swap header.  If fragmented
282 * try to reassemble.  Process options.  Pass to next level.
283 */
284void
285ipintr()
286{
287	register struct ip *ip = NULL;
288	register struct mbuf *m;
289	register struct ipq *fp;
290	register struct in_ifaddr *ia;
291	register struct ifaddr *ifa;
292	struct ipqent *ipqe;
293	int hlen = 0, mff, len, s;
294#ifdef PFIL_HOOKS
295	struct packet_filter_hook *pfh;
296	struct mbuf *m0;
297	int rv;
298#endif /* PFIL_HOOKS */
299
300next:
301	/*
302	 * Get next datagram off input queue and get IP header
303	 * in first mbuf.
304	 */
305	s = splimp();
306	IF_DEQUEUE(&ipintrq, m);
307	splx(s);
308	if (m == 0)
309		return;
310#ifdef	DIAGNOSTIC
311	if ((m->m_flags & M_PKTHDR) == 0)
312		panic("ipintr no HDR");
313#endif
314	/*
315	 * If no IP addresses have been set yet but the interfaces
316	 * are receiving, can't do anything with incoming packets yet.
317	 */
318	if (in_ifaddr.tqh_first == 0)
319		goto bad;
320	ipstat.ips_total++;
321	if (m->m_len < sizeof (struct ip) &&
322	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
323		ipstat.ips_toosmall++;
324		goto next;
325	}
326	ip = mtod(m, struct ip *);
327	if (ip->ip_v != IPVERSION) {
328		ipstat.ips_badvers++;
329		goto bad;
330	}
331	hlen = ip->ip_hl << 2;
332	if (hlen < sizeof(struct ip)) {	/* minimum header length */
333		ipstat.ips_badhlen++;
334		goto bad;
335	}
336	if (hlen > m->m_len) {
337		if ((m = m_pullup(m, hlen)) == 0) {
338			ipstat.ips_badhlen++;
339			goto next;
340		}
341		ip = mtod(m, struct ip *);
342	}
343	if (in_cksum(m, hlen) != 0) {
344		ipstat.ips_badsum++;
345		goto bad;
346	}
347
348	/*
349	 * Convert fields to host representation.
350	 */
351	NTOHS(ip->ip_len);
352	NTOHS(ip->ip_off);
353	len = ip->ip_len;
354
355	/*
356	 * Check for additional length bogosity
357	 */
358	if (len < hlen)
359	{
360	 	ipstat.ips_badlen++;
361		goto bad;
362	}
363
364	/*
365	 * Check that the amount of data in the buffers
366	 * is as at least much as the IP header would have us expect.
367	 * Trim mbufs if longer than we expect.
368	 * Drop packet if shorter than we expect.
369	 */
370	if (m->m_pkthdr.len < len) {
371		ipstat.ips_tooshort++;
372		goto bad;
373	}
374	if (m->m_pkthdr.len > len) {
375		if (m->m_len == m->m_pkthdr.len) {
376			m->m_len = len;
377			m->m_pkthdr.len = len;
378		} else
379			m_adj(m, len - m->m_pkthdr.len);
380	}
381
382	/*
383	 * Assume that we can create a fast-forward IP flow entry
384	 * based on this packet.
385	 */
386	m->m_flags |= M_CANFASTFWD;
387
388#ifdef PFIL_HOOKS
389	/*
390	 * Run through list of hooks for input packets.  If there are any
391	 * filters which require that additional packets in the flow are
392	 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
393	 * Note that filters must _never_ set this flag, as another filter
394	 * in the list may have previously cleared it.
395	 */
396	m0 = m;
397	for (pfh = pfil_hook_get(PFIL_IN); pfh; pfh = pfh->pfil_link.tqe_next)
398		if (pfh->pfil_func) {
399			rv = pfh->pfil_func(ip, hlen, m->m_pkthdr.rcvif, 0, &m0);
400			if (rv)
401				goto next;
402			m = m0;
403			if (m == NULL)
404				goto next;
405			ip = mtod(m, struct ip *);
406		}
407#endif /* PFIL_HOOKS */
408
409	/*
410	 * Process options and, if not destined for us,
411	 * ship it on.  ip_dooptions returns 1 when an
412	 * error was detected (causing an icmp message
413	 * to be sent and the original packet to be freed).
414	 */
415	ip_nhops = 0;		/* for source routed packets */
416	if (hlen > sizeof (struct ip) && ip_dooptions(m))
417		goto next;
418
419	/*
420	 * Check our list of addresses, to see if the packet is for us.
421	 */
422	INADDR_TO_IA(ip->ip_dst, ia);
423	if (ia != NULL) goto ours;
424	if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
425		for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
426		    ifa != NULL; ifa = ifa->ifa_list.tqe_next) {
427			if (ifa->ifa_addr->sa_family != AF_INET) continue;
428			ia = ifatoia(ifa);
429			if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
430			    in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
431			    /*
432			     * Look for all-0's host part (old broadcast addr),
433			     * either for subnet or net.
434			     */
435			    ip->ip_dst.s_addr == ia->ia_subnet ||
436			    ip->ip_dst.s_addr == ia->ia_net)
437				goto ours;
438			/*
439			 * An interface with IP address zero accepts
440			 * all packets that arrive on that interface.
441			 */
442			if (in_nullhost(ia->ia_addr.sin_addr))
443				goto ours;
444		}
445	}
446	if (IN_MULTICAST(ip->ip_dst.s_addr)) {
447		struct in_multi *inm;
448#ifdef MROUTING
449		extern struct socket *ip_mrouter;
450
451		if (m->m_flags & M_EXT) {
452			if ((m = m_pullup(m, hlen)) == 0) {
453				ipstat.ips_toosmall++;
454				goto next;
455			}
456			ip = mtod(m, struct ip *);
457		}
458
459		if (ip_mrouter) {
460			/*
461			 * If we are acting as a multicast router, all
462			 * incoming multicast packets are passed to the
463			 * kernel-level multicast forwarding function.
464			 * The packet is returned (relatively) intact; if
465			 * ip_mforward() returns a non-zero value, the packet
466			 * must be discarded, else it may be accepted below.
467			 *
468			 * (The IP ident field is put in the same byte order
469			 * as expected when ip_mforward() is called from
470			 * ip_output().)
471			 */
472			if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
473				ipstat.ips_cantforward++;
474				m_freem(m);
475				goto next;
476			}
477
478			/*
479			 * The process-level routing demon needs to receive
480			 * all multicast IGMP packets, whether or not this
481			 * host belongs to their destination groups.
482			 */
483			if (ip->ip_p == IPPROTO_IGMP)
484				goto ours;
485			ipstat.ips_forward++;
486		}
487#endif
488		/*
489		 * See if we belong to the destination multicast group on the
490		 * arrival interface.
491		 */
492		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
493		if (inm == NULL) {
494			ipstat.ips_cantforward++;
495			m_freem(m);
496			goto next;
497		}
498		goto ours;
499	}
500	if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
501	    in_nullhost(ip->ip_dst))
502		goto ours;
503
504	/*
505	 * Not for us; forward if possible and desirable.
506	 */
507	if (ipforwarding == 0) {
508		ipstat.ips_cantforward++;
509		m_freem(m);
510	} else
511		ip_forward(m, 0);
512	goto next;
513
514ours:
515	/*
516	 * If offset or IP_MF are set, must reassemble.
517	 * Otherwise, nothing need be done.
518	 * (We could look in the reassembly queue to see
519	 * if the packet was previously fragmented,
520	 * but it's not worth the time; just let them time out.)
521	 */
522	if (ip->ip_off & ~(IP_DF|IP_RF)) {
523		/*
524		 * Look for queue of fragments
525		 * of this datagram.
526		 */
527		IPQ_LOCK();
528		for (fp = ipq.lh_first; fp != NULL; fp = fp->ipq_q.le_next)
529			if (ip->ip_id == fp->ipq_id &&
530			    in_hosteq(ip->ip_src, fp->ipq_src) &&
531			    in_hosteq(ip->ip_dst, fp->ipq_dst) &&
532			    ip->ip_p == fp->ipq_p)
533				goto found;
534		fp = 0;
535found:
536
537		/*
538		 * Adjust ip_len to not reflect header,
539		 * set ipqe_mff if more fragments are expected,
540		 * convert offset of this to bytes.
541		 */
542		ip->ip_len -= hlen;
543		mff = (ip->ip_off & IP_MF) != 0;
544		if (mff) {
545		        /*
546		         * Make sure that fragments have a data length
547			 * that's a non-zero multiple of 8 bytes.
548		         */
549			if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
550				ipstat.ips_badfrags++;
551				IPQ_UNLOCK();
552				goto bad;
553			}
554		}
555		ip->ip_off <<= 3;
556
557		/*
558		 * If datagram marked as having more fragments
559		 * or if this is not the first fragment,
560		 * attempt reassembly; if it succeeds, proceed.
561		 */
562		if (mff || ip->ip_off) {
563			ipstat.ips_fragments++;
564			ipqe = pool_get(&ipqent_pool, PR_NOWAIT);
565			if (ipqe == NULL) {
566				ipstat.ips_rcvmemdrop++;
567				IPQ_UNLOCK();
568				goto bad;
569			}
570			ipqe->ipqe_mff = mff;
571			ipqe->ipqe_m = m;
572			ipqe->ipqe_ip = ip;
573			m = ip_reass(ipqe, fp);
574			if (m == 0) {
575				IPQ_UNLOCK();
576				goto next;
577			}
578			ipstat.ips_reassembled++;
579			ip = mtod(m, struct ip *);
580			hlen = ip->ip_hl << 2;
581			ip->ip_len += hlen;
582		} else
583			if (fp)
584				ip_freef(fp);
585		IPQ_UNLOCK();
586	}
587
588	/*
589	 * Switch out to protocol's input routine.
590	 */
591	ipstat.ips_delivered++;
592	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
593	goto next;
594bad:
595	m_freem(m);
596	goto next;
597}
598
599/*
600 * Take incoming datagram fragment and try to
601 * reassemble it into whole datagram.  If a chain for
602 * reassembly of this datagram already exists, then it
603 * is given as fp; otherwise have to make a chain.
604 */
605struct mbuf *
606ip_reass(ipqe, fp)
607	register struct ipqent *ipqe;
608	register struct ipq *fp;
609{
610	register struct mbuf *m = ipqe->ipqe_m;
611	register struct ipqent *nq, *p, *q;
612	struct ip *ip;
613	struct mbuf *t;
614	int hlen = ipqe->ipqe_ip->ip_hl << 2;
615	int i, next;
616
617	IPQ_LOCK_CHECK();
618
619	/*
620	 * Presence of header sizes in mbufs
621	 * would confuse code below.
622	 */
623	m->m_data += hlen;
624	m->m_len -= hlen;
625
626	/*
627	 * If first fragment to arrive, create a reassembly queue.
628	 */
629	if (fp == 0) {
630		MALLOC(fp, struct ipq *, sizeof (struct ipq),
631		    M_FTABLE, M_NOWAIT);
632		if (fp == NULL)
633			goto dropfrag;
634		LIST_INSERT_HEAD(&ipq, fp, ipq_q);
635		fp->ipq_ttl = IPFRAGTTL;
636		fp->ipq_p = ipqe->ipqe_ip->ip_p;
637		fp->ipq_id = ipqe->ipqe_ip->ip_id;
638		LIST_INIT(&fp->ipq_fragq);
639		fp->ipq_src = ipqe->ipqe_ip->ip_src;
640		fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
641		p = NULL;
642		goto insert;
643	}
644
645	/*
646	 * Find a segment which begins after this one does.
647	 */
648	for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
649	    p = q, q = q->ipqe_q.le_next)
650		if (q->ipqe_ip->ip_off > ipqe->ipqe_ip->ip_off)
651			break;
652
653	/*
654	 * If there is a preceding segment, it may provide some of
655	 * our data already.  If so, drop the data from the incoming
656	 * segment.  If it provides all of our data, drop us.
657	 */
658	if (p != NULL) {
659		i = p->ipqe_ip->ip_off + p->ipqe_ip->ip_len -
660		    ipqe->ipqe_ip->ip_off;
661		if (i > 0) {
662			if (i >= ipqe->ipqe_ip->ip_len)
663				goto dropfrag;
664			m_adj(ipqe->ipqe_m, i);
665			ipqe->ipqe_ip->ip_off += i;
666			ipqe->ipqe_ip->ip_len -= i;
667		}
668	}
669
670	/*
671	 * While we overlap succeeding segments trim them or,
672	 * if they are completely covered, dequeue them.
673	 */
674	for (; q != NULL && ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len >
675	    q->ipqe_ip->ip_off; q = nq) {
676		i = (ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len) -
677		    q->ipqe_ip->ip_off;
678		if (i < q->ipqe_ip->ip_len) {
679			q->ipqe_ip->ip_len -= i;
680			q->ipqe_ip->ip_off += i;
681			m_adj(q->ipqe_m, i);
682			break;
683		}
684		nq = q->ipqe_q.le_next;
685		m_freem(q->ipqe_m);
686		LIST_REMOVE(q, ipqe_q);
687		pool_put(&ipqent_pool, q);
688	}
689
690insert:
691	/*
692	 * Stick new segment in its place;
693	 * check for complete reassembly.
694	 */
695	if (p == NULL) {
696		LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
697	} else {
698		LIST_INSERT_AFTER(p, ipqe, ipqe_q);
699	}
700	next = 0;
701	for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
702	    p = q, q = q->ipqe_q.le_next) {
703		if (q->ipqe_ip->ip_off != next)
704			return (0);
705		next += q->ipqe_ip->ip_len;
706	}
707	if (p->ipqe_mff)
708		return (0);
709
710	/*
711	 * Reassembly is complete.  Check for a bogus message size and
712	 * concatenate fragments.
713	 */
714	q = fp->ipq_fragq.lh_first;
715	ip = q->ipqe_ip;
716	if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
717		ipstat.ips_toolong++;
718		ip_freef(fp);
719		return (0);
720	}
721	m = q->ipqe_m;
722	t = m->m_next;
723	m->m_next = 0;
724	m_cat(m, t);
725	nq = q->ipqe_q.le_next;
726	pool_put(&ipqent_pool, q);
727	for (q = nq; q != NULL; q = nq) {
728		t = q->ipqe_m;
729		nq = q->ipqe_q.le_next;
730		pool_put(&ipqent_pool, q);
731		m_cat(m, t);
732	}
733
734	/*
735	 * Create header for new ip packet by
736	 * modifying header of first packet;
737	 * dequeue and discard fragment reassembly header.
738	 * Make header visible.
739	 */
740	ip->ip_len = next;
741	ip->ip_src = fp->ipq_src;
742	ip->ip_dst = fp->ipq_dst;
743	LIST_REMOVE(fp, ipq_q);
744	FREE(fp, M_FTABLE);
745	m->m_len += (ip->ip_hl << 2);
746	m->m_data -= (ip->ip_hl << 2);
747	/* some debugging cruft by sklower, below, will go away soon */
748	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
749		register int plen = 0;
750		for (t = m; t; t = t->m_next)
751			plen += t->m_len;
752		m->m_pkthdr.len = plen;
753	}
754	return (m);
755
756dropfrag:
757	ipstat.ips_fragdropped++;
758	m_freem(m);
759	pool_put(&ipqent_pool, ipqe);
760	return (0);
761}
762
763/*
764 * Free a fragment reassembly header and all
765 * associated datagrams.
766 */
767void
768ip_freef(fp)
769	struct ipq *fp;
770{
771	register struct ipqent *q, *p;
772
773	IPQ_LOCK_CHECK();
774
775	for (q = fp->ipq_fragq.lh_first; q != NULL; q = p) {
776		p = q->ipqe_q.le_next;
777		m_freem(q->ipqe_m);
778		LIST_REMOVE(q, ipqe_q);
779		pool_put(&ipqent_pool, q);
780	}
781	LIST_REMOVE(fp, ipq_q);
782	FREE(fp, M_FTABLE);
783}
784
785/*
786 * IP timer processing;
787 * if a timer expires on a reassembly
788 * queue, discard it.
789 */
790void
791ip_slowtimo()
792{
793	register struct ipq *fp, *nfp;
794	int s = splsoftnet();
795
796	IPQ_LOCK();
797	for (fp = ipq.lh_first; fp != NULL; fp = nfp) {
798		nfp = fp->ipq_q.le_next;
799		if (--fp->ipq_ttl == 0) {
800			ipstat.ips_fragtimeout++;
801			ip_freef(fp);
802		}
803	}
804	IPQ_UNLOCK();
805#ifdef GATEWAY
806	ipflow_slowtimo();
807#endif
808	splx(s);
809}
810
811/*
812 * Drain off all datagram fragments.
813 */
814void
815ip_drain()
816{
817
818	/*
819	 * We may be called from a device's interrupt context.  If
820	 * the ipq is already busy, just bail out now.
821	 */
822	if (ipq_lock_try() == 0)
823		return;
824
825	while (ipq.lh_first != NULL) {
826		ipstat.ips_fragdropped++;
827		ip_freef(ipq.lh_first);
828	}
829
830	IPQ_UNLOCK();
831}
832
833/*
834 * Do option processing on a datagram,
835 * possibly discarding it if bad options are encountered,
836 * or forwarding it if source-routed.
837 * Returns 1 if packet has been forwarded/freed,
838 * 0 if the packet should be processed further.
839 */
840int
841ip_dooptions(m)
842	struct mbuf *m;
843{
844	register struct ip *ip = mtod(m, struct ip *);
845	register u_char *cp;
846	register struct ip_timestamp *ipt;
847	register struct in_ifaddr *ia;
848	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
849	struct in_addr *sin, dst;
850	n_time ntime;
851
852	dst = ip->ip_dst;
853	cp = (u_char *)(ip + 1);
854	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
855	for (; cnt > 0; cnt -= optlen, cp += optlen) {
856		opt = cp[IPOPT_OPTVAL];
857		if (opt == IPOPT_EOL)
858			break;
859		if (opt == IPOPT_NOP)
860			optlen = 1;
861		else {
862			optlen = cp[IPOPT_OLEN];
863			if (optlen <= 0 || optlen > cnt) {
864				code = &cp[IPOPT_OLEN] - (u_char *)ip;
865				goto bad;
866			}
867		}
868		switch (opt) {
869
870		default:
871			break;
872
873		/*
874		 * Source routing with record.
875		 * Find interface with current destination address.
876		 * If none on this machine then drop if strictly routed,
877		 * or do nothing if loosely routed.
878		 * Record interface address and bring up next address
879		 * component.  If strictly routed make sure next
880		 * address is on directly accessible net.
881		 */
882		case IPOPT_LSRR:
883		case IPOPT_SSRR:
884			if (ip_allowsrcrt == 0) {
885				type = ICMP_UNREACH;
886				code = ICMP_UNREACH_NET_PROHIB;
887				goto bad;
888			}
889			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
890				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
891				goto bad;
892			}
893			ipaddr.sin_addr = ip->ip_dst;
894			ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
895			if (ia == 0) {
896				if (opt == IPOPT_SSRR) {
897					type = ICMP_UNREACH;
898					code = ICMP_UNREACH_SRCFAIL;
899					goto bad;
900				}
901				/*
902				 * Loose routing, and not at next destination
903				 * yet; nothing to do except forward.
904				 */
905				break;
906			}
907			off--;			/* 0 origin */
908			if (off > optlen - sizeof(struct in_addr)) {
909				/*
910				 * End of source route.  Should be for us.
911				 */
912				save_rte(cp, ip->ip_src);
913				break;
914			}
915			/*
916			 * locate outgoing interface
917			 */
918			bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
919			    sizeof(ipaddr.sin_addr));
920			if (opt == IPOPT_SSRR) {
921#define	INA	struct in_ifaddr *
922#define	SA	struct sockaddr *
923			    ia = (INA)ifa_ifwithladdr((SA)&ipaddr);
924			} else
925				ia = ip_rtaddr(ipaddr.sin_addr);
926			if (ia == 0) {
927				type = ICMP_UNREACH;
928				code = ICMP_UNREACH_SRCFAIL;
929				goto bad;
930			}
931			ip->ip_dst = ipaddr.sin_addr;
932			bcopy((caddr_t)&ia->ia_addr.sin_addr,
933			    (caddr_t)(cp + off), sizeof(struct in_addr));
934			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
935			/*
936			 * Let ip_intr's mcast routing check handle mcast pkts
937			 */
938			forward = !IN_MULTICAST(ip->ip_dst.s_addr);
939			break;
940
941		case IPOPT_RR:
942			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
943				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
944				goto bad;
945			}
946			/*
947			 * If no space remains, ignore.
948			 */
949			off--;			/* 0 origin */
950			if (off > optlen - sizeof(struct in_addr))
951				break;
952			bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
953			    sizeof(ipaddr.sin_addr));
954			/*
955			 * locate outgoing interface; if we're the destination,
956			 * use the incoming interface (should be same).
957			 */
958			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
959			    (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
960				type = ICMP_UNREACH;
961				code = ICMP_UNREACH_HOST;
962				goto bad;
963			}
964			bcopy((caddr_t)&ia->ia_addr.sin_addr,
965			    (caddr_t)(cp + off), sizeof(struct in_addr));
966			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
967			break;
968
969		case IPOPT_TS:
970			code = cp - (u_char *)ip;
971			ipt = (struct ip_timestamp *)cp;
972			if (ipt->ipt_len < 5)
973				goto bad;
974			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
975				if (++ipt->ipt_oflw == 0)
976					goto bad;
977				break;
978			}
979			sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
980			switch (ipt->ipt_flg) {
981
982			case IPOPT_TS_TSONLY:
983				break;
984
985			case IPOPT_TS_TSANDADDR:
986				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
987				    sizeof(struct in_addr) > ipt->ipt_len)
988					goto bad;
989				ipaddr.sin_addr = dst;
990				ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
991							    m->m_pkthdr.rcvif);
992				if (ia == 0)
993					continue;
994				bcopy((caddr_t)&ia->ia_addr.sin_addr,
995				    (caddr_t)sin, sizeof(struct in_addr));
996				ipt->ipt_ptr += sizeof(struct in_addr);
997				break;
998
999			case IPOPT_TS_PRESPEC:
1000				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1001				    sizeof(struct in_addr) > ipt->ipt_len)
1002					goto bad;
1003				bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
1004				    sizeof(struct in_addr));
1005				if (ifa_ifwithaddr((SA)&ipaddr) == 0)
1006					continue;
1007				ipt->ipt_ptr += sizeof(struct in_addr);
1008				break;
1009
1010			default:
1011				goto bad;
1012			}
1013			ntime = iptime();
1014			bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
1015			    sizeof(n_time));
1016			ipt->ipt_ptr += sizeof(n_time);
1017		}
1018	}
1019	if (forward) {
1020		if (ip_forwsrcrt == 0) {
1021			type = ICMP_UNREACH;
1022			code = ICMP_UNREACH_SRCFAIL;
1023			goto bad;
1024		}
1025		ip_forward(m, 1);
1026		return (1);
1027	}
1028	return (0);
1029bad:
1030	icmp_error(m, type, code, 0, 0);
1031	ipstat.ips_badoptions++;
1032	return (1);
1033}
1034
1035/*
1036 * Given address of next destination (final or next hop),
1037 * return internet address info of interface to be used to get there.
1038 */
1039struct in_ifaddr *
1040ip_rtaddr(dst)
1041	 struct in_addr dst;
1042{
1043	register struct sockaddr_in *sin;
1044
1045	sin = satosin(&ipforward_rt.ro_dst);
1046
1047	if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) {
1048		if (ipforward_rt.ro_rt) {
1049			RTFREE(ipforward_rt.ro_rt);
1050			ipforward_rt.ro_rt = 0;
1051		}
1052		sin->sin_family = AF_INET;
1053		sin->sin_len = sizeof(*sin);
1054		sin->sin_addr = dst;
1055
1056		rtalloc(&ipforward_rt);
1057	}
1058	if (ipforward_rt.ro_rt == 0)
1059		return ((struct in_ifaddr *)0);
1060	return (ifatoia(ipforward_rt.ro_rt->rt_ifa));
1061}
1062
1063/*
1064 * Save incoming source route for use in replies,
1065 * to be picked up later by ip_srcroute if the receiver is interested.
1066 */
1067void
1068save_rte(option, dst)
1069	u_char *option;
1070	struct in_addr dst;
1071{
1072	unsigned olen;
1073
1074	olen = option[IPOPT_OLEN];
1075#ifdef DIAGNOSTIC
1076	if (ipprintfs)
1077		printf("save_rte: olen %d\n", olen);
1078#endif
1079	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1080		return;
1081	bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
1082	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1083	ip_srcrt.dst = dst;
1084}
1085
1086/*
1087 * Retrieve incoming source route for use in replies,
1088 * in the same form used by setsockopt.
1089 * The first hop is placed before the options, will be removed later.
1090 */
1091struct mbuf *
1092ip_srcroute()
1093{
1094	register struct in_addr *p, *q;
1095	register struct mbuf *m;
1096
1097	if (ip_nhops == 0)
1098		return ((struct mbuf *)0);
1099	m = m_get(M_DONTWAIT, MT_SOOPTS);
1100	if (m == 0)
1101		return ((struct mbuf *)0);
1102
1103#define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1104
1105	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1106	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1107	    OPTSIZ;
1108#ifdef DIAGNOSTIC
1109	if (ipprintfs)
1110		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1111#endif
1112
1113	/*
1114	 * First save first hop for return route
1115	 */
1116	p = &ip_srcrt.route[ip_nhops - 1];
1117	*(mtod(m, struct in_addr *)) = *p--;
1118#ifdef DIAGNOSTIC
1119	if (ipprintfs)
1120		printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
1121#endif
1122
1123	/*
1124	 * Copy option fields and padding (nop) to mbuf.
1125	 */
1126	ip_srcrt.nop = IPOPT_NOP;
1127	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1128	bcopy((caddr_t)&ip_srcrt.nop,
1129	    mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
1130	q = (struct in_addr *)(mtod(m, caddr_t) +
1131	    sizeof(struct in_addr) + OPTSIZ);
1132#undef OPTSIZ
1133	/*
1134	 * Record return path as an IP source route,
1135	 * reversing the path (pointers are now aligned).
1136	 */
1137	while (p >= ip_srcrt.route) {
1138#ifdef DIAGNOSTIC
1139		if (ipprintfs)
1140			printf(" %x", ntohl(q->s_addr));
1141#endif
1142		*q++ = *p--;
1143	}
1144	/*
1145	 * Last hop goes to final destination.
1146	 */
1147	*q = ip_srcrt.dst;
1148#ifdef DIAGNOSTIC
1149	if (ipprintfs)
1150		printf(" %x\n", ntohl(q->s_addr));
1151#endif
1152	return (m);
1153}
1154
1155/*
1156 * Strip out IP options, at higher
1157 * level protocol in the kernel.
1158 * Second argument is buffer to which options
1159 * will be moved, and return value is their length.
1160 * XXX should be deleted; last arg currently ignored.
1161 */
1162void
1163ip_stripoptions(m, mopt)
1164	register struct mbuf *m;
1165	struct mbuf *mopt;
1166{
1167	register int i;
1168	struct ip *ip = mtod(m, struct ip *);
1169	register caddr_t opts;
1170	int olen;
1171
1172	olen = (ip->ip_hl << 2) - sizeof (struct ip);
1173	opts = (caddr_t)(ip + 1);
1174	i = m->m_len - (sizeof (struct ip) + olen);
1175	bcopy(opts  + olen, opts, (unsigned)i);
1176	m->m_len -= olen;
1177	if (m->m_flags & M_PKTHDR)
1178		m->m_pkthdr.len -= olen;
1179	ip->ip_len -= olen;
1180	ip->ip_hl = sizeof (struct ip) >> 2;
1181}
1182
1183int inetctlerrmap[PRC_NCMDS] = {
1184	0,		0,		0,		0,
1185	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1186	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1187	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1188	0,		0,		0,		0,
1189	ENOPROTOOPT
1190};
1191
1192/*
1193 * Forward a packet.  If some error occurs return the sender
1194 * an icmp packet.  Note we can't always generate a meaningful
1195 * icmp message because icmp doesn't have a large enough repertoire
1196 * of codes and types.
1197 *
1198 * If not forwarding, just drop the packet.  This could be confusing
1199 * if ipforwarding was zero but some routing protocol was advancing
1200 * us as a gateway to somewhere.  However, we must let the routing
1201 * protocol deal with that.
1202 *
1203 * The srcrt parameter indicates whether the packet is being forwarded
1204 * via a source route.
1205 */
1206void
1207ip_forward(m, srcrt)
1208	struct mbuf *m;
1209	int srcrt;
1210{
1211	register struct ip *ip = mtod(m, struct ip *);
1212	register struct sockaddr_in *sin;
1213	register struct rtentry *rt;
1214	int error, type = 0, code = 0;
1215	struct mbuf *mcopy;
1216	n_long dest;
1217	struct ifnet *destifp;
1218
1219	dest = 0;
1220#ifdef DIAGNOSTIC
1221	if (ipprintfs)
1222		printf("forward: src %2.2x dst %2.2x ttl %x\n",
1223		    ntohl(ip->ip_src.s_addr),
1224		    ntohl(ip->ip_dst.s_addr), ip->ip_ttl);
1225#endif
1226	if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
1227		ipstat.ips_cantforward++;
1228		m_freem(m);
1229		return;
1230	}
1231	if (ip->ip_ttl <= IPTTLDEC) {
1232		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1233		return;
1234	}
1235	ip->ip_ttl -= IPTTLDEC;
1236
1237	sin = satosin(&ipforward_rt.ro_dst);
1238	if ((rt = ipforward_rt.ro_rt) == 0 ||
1239	    !in_hosteq(ip->ip_dst, sin->sin_addr)) {
1240		if (ipforward_rt.ro_rt) {
1241			RTFREE(ipforward_rt.ro_rt);
1242			ipforward_rt.ro_rt = 0;
1243		}
1244		sin->sin_family = AF_INET;
1245		sin->sin_len = sizeof(struct sockaddr_in);
1246		sin->sin_addr = ip->ip_dst;
1247
1248		rtalloc(&ipforward_rt);
1249		if (ipforward_rt.ro_rt == 0) {
1250			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1251			return;
1252		}
1253		rt = ipforward_rt.ro_rt;
1254	}
1255
1256	/*
1257	 * Save at most 68 bytes of the packet in case
1258	 * we need to generate an ICMP message to the src.
1259	 */
1260	mcopy = m_copy(m, 0, imin((int)ip->ip_len, 68));
1261
1262	/*
1263	 * If forwarding packet using same interface that it came in on,
1264	 * perhaps should send a redirect to sender to shortcut a hop.
1265	 * Only send redirect if source is sending directly to us,
1266	 * and if packet was not source routed (or has any options).
1267	 * Also, don't send redirect if forwarding using a default route
1268	 * or a route modified by a redirect.
1269	 */
1270	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1271	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1272	    !in_nullhost(satosin(rt_key(rt))->sin_addr) &&
1273	    ipsendredirects && !srcrt) {
1274		if (rt->rt_ifa &&
1275		    (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
1276		    ifatoia(rt->rt_ifa)->ia_subnet) {
1277			if (rt->rt_flags & RTF_GATEWAY)
1278				dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1279			else
1280				dest = ip->ip_dst.s_addr;
1281			/*
1282			 * Router requirements says to only send host
1283			 * redirects.
1284			 */
1285			type = ICMP_REDIRECT;
1286			code = ICMP_REDIRECT_HOST;
1287#ifdef DIAGNOSTIC
1288			if (ipprintfs)
1289				printf("redirect (%d) to %x\n", code,
1290				    (u_int32_t)dest);
1291#endif
1292		}
1293	}
1294
1295	error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1296	    (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 0);
1297	if (error)
1298		ipstat.ips_cantforward++;
1299	else {
1300		ipstat.ips_forward++;
1301		if (type)
1302			ipstat.ips_redirectsent++;
1303		else {
1304			if (mcopy) {
1305#ifdef GATEWAY
1306				if (mcopy->m_flags & M_CANFASTFWD)
1307					ipflow_create(&ipforward_rt, mcopy);
1308#endif
1309				m_freem(mcopy);
1310			}
1311			return;
1312		}
1313	}
1314	if (mcopy == NULL)
1315		return;
1316	destifp = NULL;
1317
1318	switch (error) {
1319
1320	case 0:				/* forwarded, but need redirect */
1321		/* type, code set above */
1322		break;
1323
1324	case ENETUNREACH:		/* shouldn't happen, checked above */
1325	case EHOSTUNREACH:
1326	case ENETDOWN:
1327	case EHOSTDOWN:
1328	default:
1329		type = ICMP_UNREACH;
1330		code = ICMP_UNREACH_HOST;
1331		break;
1332
1333	case EMSGSIZE:
1334		type = ICMP_UNREACH;
1335		code = ICMP_UNREACH_NEEDFRAG;
1336		if (ipforward_rt.ro_rt)
1337			destifp = ipforward_rt.ro_rt->rt_ifp;
1338		ipstat.ips_cantfrag++;
1339		break;
1340
1341	case ENOBUFS:
1342		type = ICMP_SOURCEQUENCH;
1343		code = 0;
1344		break;
1345	}
1346	icmp_error(mcopy, type, code, dest, destifp);
1347}
1348
1349void
1350ip_savecontrol(inp, mp, ip, m)
1351	register struct inpcb *inp;
1352	register struct mbuf **mp;
1353	register struct ip *ip;
1354	register struct mbuf *m;
1355{
1356
1357	if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1358		struct timeval tv;
1359
1360		microtime(&tv);
1361		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1362		    SCM_TIMESTAMP, SOL_SOCKET);
1363		if (*mp)
1364			mp = &(*mp)->m_next;
1365	}
1366	if (inp->inp_flags & INP_RECVDSTADDR) {
1367		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1368		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1369		if (*mp)
1370			mp = &(*mp)->m_next;
1371	}
1372#ifdef notyet
1373	/*
1374	 * XXX
1375	 * Moving these out of udp_input() made them even more broken
1376	 * than they already were.
1377	 *	- fenner@parc.xerox.com
1378	 */
1379	/* options were tossed already */
1380	if (inp->inp_flags & INP_RECVOPTS) {
1381		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1382		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1383		if (*mp)
1384			mp = &(*mp)->m_next;
1385	}
1386	/* ip_srcroute doesn't do what we want here, need to fix */
1387	if (inp->inp_flags & INP_RECVRETOPTS) {
1388		*mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1389		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1390		if (*mp)
1391			mp = &(*mp)->m_next;
1392	}
1393#endif
1394	if (inp->inp_flags & INP_RECVIF) {
1395		struct sockaddr_dl sdl;
1396
1397		sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]);
1398		sdl.sdl_family = AF_LINK;
1399		sdl.sdl_index = m->m_pkthdr.rcvif ?
1400		    m->m_pkthdr.rcvif->if_index : 0;
1401		sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0;
1402		*mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len,
1403		    IP_RECVIF, IPPROTO_IP);
1404		if (*mp)
1405			mp = &(*mp)->m_next;
1406	}
1407}
1408
1409int
1410ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1411	int *name;
1412	u_int namelen;
1413	void *oldp;
1414	size_t *oldlenp;
1415	void *newp;
1416	size_t newlen;
1417{
1418	extern int subnetsarelocal;
1419
1420	int error, old;
1421
1422	/* All sysctl names at this level are terminal. */
1423	if (namelen != 1)
1424		return (ENOTDIR);
1425
1426	switch (name[0]) {
1427	case IPCTL_FORWARDING:
1428		return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding));
1429	case IPCTL_SENDREDIRECTS:
1430		return (sysctl_int(oldp, oldlenp, newp, newlen,
1431			&ipsendredirects));
1432	case IPCTL_DEFTTL:
1433		return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl));
1434#ifdef notyet
1435	case IPCTL_DEFMTU:
1436		return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu));
1437#endif
1438	case IPCTL_FORWSRCRT:
1439		/* Don't allow this to change in a secure environment.  */
1440		if (securelevel > 0)
1441			return (sysctl_rdint(oldp, oldlenp, newp,
1442			    ip_forwsrcrt));
1443		else
1444			return (sysctl_int(oldp, oldlenp, newp, newlen,
1445			    &ip_forwsrcrt));
1446	case IPCTL_DIRECTEDBCAST:
1447		return (sysctl_int(oldp, oldlenp, newp, newlen,
1448		    &ip_directedbcast));
1449	case IPCTL_ALLOWSRCRT:
1450		return (sysctl_int(oldp, oldlenp, newp, newlen,
1451		    &ip_allowsrcrt));
1452	case IPCTL_SUBNETSARELOCAL:
1453		return (sysctl_int(oldp, oldlenp, newp, newlen,
1454		    &subnetsarelocal));
1455	case IPCTL_MTUDISC:
1456		error = sysctl_int(oldp, oldlenp, newp, newlen,
1457		    &ip_mtudisc);
1458		if (ip_mtudisc != 0 && ip_mtudisc_timeout_q == NULL) {
1459			ip_mtudisc_timeout_q =
1460			    rt_timer_queue_create(ip_mtudisc_timeout);
1461		} else if (ip_mtudisc == 0 && ip_mtudisc_timeout_q != NULL) {
1462			rt_timer_queue_destroy(ip_mtudisc_timeout_q, TRUE);
1463			ip_mtudisc_timeout_q = NULL;
1464		}
1465		return error;
1466	case IPCTL_ANONPORTMIN:
1467		old = anonportmin;
1468		error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmin);
1469		if (anonportmin >= anonportmax || anonportmin > 65535
1470#ifndef IPNOPRIVPORTS
1471		    || anonportmin < IPPORT_RESERVED
1472#endif
1473		    ) {
1474			anonportmin = old;
1475			return (EINVAL);
1476		}
1477		return (error);
1478	case IPCTL_ANONPORTMAX:
1479		old = anonportmax;
1480		error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmax);
1481		if (anonportmin >= anonportmax || anonportmax > 65535
1482#ifndef IPNOPRIVPORTS
1483		    || anonportmax < IPPORT_RESERVED
1484#endif
1485		    ) {
1486			anonportmax = old;
1487			return (EINVAL);
1488		}
1489		return (error);
1490	case IPCTL_MTUDISCTIMEOUT:
1491		error = sysctl_int(oldp, oldlenp, newp, newlen,
1492		   &ip_mtudisc_timeout);
1493		if (ip_mtudisc_timeout_q != NULL)
1494			rt_timer_queue_change(ip_mtudisc_timeout_q,
1495					      ip_mtudisc_timeout);
1496		return (error);
1497#ifdef GATEWAY
1498	case IPCTL_MAXFLOWS:
1499	    {
1500		int s;
1501
1502		error = sysctl_int(oldp, oldlenp, newp, newlen,
1503		   &ip_maxflows);
1504		s = splsoftnet();
1505		ipflow_reap(0);
1506		splx(s);
1507		return (error);
1508	    }
1509#endif
1510	default:
1511		return (EOPNOTSUPP);
1512	}
1513	/* NOTREACHED */
1514}
1515