ip_input.c revision 1.80
1/*	$NetBSD: ip_input.c,v 1.80 1999/01/19 23:39:57 mycroft 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 that the amount of data in the buffers
357	 * is as at least much as the IP header would have us expect.
358	 * Trim mbufs if longer than we expect.
359	 * Drop packet if shorter than we expect.
360	 */
361	if (m->m_pkthdr.len < len) {
362		ipstat.ips_tooshort++;
363		goto bad;
364	}
365	if (m->m_pkthdr.len > len) {
366		if (m->m_len == m->m_pkthdr.len) {
367			m->m_len = len;
368			m->m_pkthdr.len = len;
369		} else
370			m_adj(m, len - m->m_pkthdr.len);
371	}
372
373	/*
374	 * Assume that we can create a fast-forward IP flow entry
375	 * based on this packet.
376	 */
377	m->m_flags |= M_CANFASTFWD;
378
379#ifdef PFIL_HOOKS
380	/*
381	 * Run through list of hooks for input packets.  If there are any
382	 * filters which require that additional packets in the flow are
383	 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
384	 * Note that filters must _never_ set this flag, as another filter
385	 * in the list may have previously cleared it.
386	 */
387	m0 = m;
388	for (pfh = pfil_hook_get(PFIL_IN); pfh; pfh = pfh->pfil_link.tqe_next)
389		if (pfh->pfil_func) {
390			rv = pfh->pfil_func(ip, hlen, m->m_pkthdr.rcvif, 0, &m0);
391			if (rv)
392				goto next;
393			m = m0;
394			if (m == NULL)
395				goto next;
396			ip = mtod(m, struct ip *);
397		}
398#endif /* PFIL_HOOKS */
399
400	/*
401	 * Process options and, if not destined for us,
402	 * ship it on.  ip_dooptions returns 1 when an
403	 * error was detected (causing an icmp message
404	 * to be sent and the original packet to be freed).
405	 */
406	ip_nhops = 0;		/* for source routed packets */
407	if (hlen > sizeof (struct ip) && ip_dooptions(m))
408		goto next;
409
410	/*
411	 * Check our list of addresses, to see if the packet is for us.
412	 */
413	INADDR_TO_IA(ip->ip_dst, ia);
414	if (ia != NULL) goto ours;
415	if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
416		for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
417		    ifa != NULL; ifa = ifa->ifa_list.tqe_next) {
418			if (ifa->ifa_addr->sa_family != AF_INET) continue;
419			ia = ifatoia(ifa);
420			if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
421			    in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
422			    /*
423			     * Look for all-0's host part (old broadcast addr),
424			     * either for subnet or net.
425			     */
426			    ip->ip_dst.s_addr == ia->ia_subnet ||
427			    ip->ip_dst.s_addr == ia->ia_net)
428				goto ours;
429			/*
430			 * An interface with IP address zero accepts
431			 * all packets that arrive on that interface.
432			 */
433			if (in_nullhost(ia->ia_addr.sin_addr))
434				goto ours;
435		}
436	}
437	if (IN_MULTICAST(ip->ip_dst.s_addr)) {
438		struct in_multi *inm;
439#ifdef MROUTING
440		extern struct socket *ip_mrouter;
441
442		if (m->m_flags & M_EXT) {
443			if ((m = m_pullup(m, hlen)) == 0) {
444				ipstat.ips_toosmall++;
445				goto next;
446			}
447			ip = mtod(m, struct ip *);
448		}
449
450		if (ip_mrouter) {
451			/*
452			 * If we are acting as a multicast router, all
453			 * incoming multicast packets are passed to the
454			 * kernel-level multicast forwarding function.
455			 * The packet is returned (relatively) intact; if
456			 * ip_mforward() returns a non-zero value, the packet
457			 * must be discarded, else it may be accepted below.
458			 *
459			 * (The IP ident field is put in the same byte order
460			 * as expected when ip_mforward() is called from
461			 * ip_output().)
462			 */
463			if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
464				ipstat.ips_cantforward++;
465				m_freem(m);
466				goto next;
467			}
468
469			/*
470			 * The process-level routing demon needs to receive
471			 * all multicast IGMP packets, whether or not this
472			 * host belongs to their destination groups.
473			 */
474			if (ip->ip_p == IPPROTO_IGMP)
475				goto ours;
476			ipstat.ips_forward++;
477		}
478#endif
479		/*
480		 * See if we belong to the destination multicast group on the
481		 * arrival interface.
482		 */
483		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
484		if (inm == NULL) {
485			ipstat.ips_cantforward++;
486			m_freem(m);
487			goto next;
488		}
489		goto ours;
490	}
491	if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
492	    in_nullhost(ip->ip_dst))
493		goto ours;
494
495	/*
496	 * Not for us; forward if possible and desirable.
497	 */
498	if (ipforwarding == 0) {
499		ipstat.ips_cantforward++;
500		m_freem(m);
501	} else
502		ip_forward(m, 0);
503	goto next;
504
505ours:
506	/*
507	 * If offset or IP_MF are set, must reassemble.
508	 * Otherwise, nothing need be done.
509	 * (We could look in the reassembly queue to see
510	 * if the packet was previously fragmented,
511	 * but it's not worth the time; just let them time out.)
512	 */
513	if (ip->ip_off & ~(IP_DF|IP_RF)) {
514		/*
515		 * Look for queue of fragments
516		 * of this datagram.
517		 */
518		IPQ_LOCK();
519		for (fp = ipq.lh_first; fp != NULL; fp = fp->ipq_q.le_next)
520			if (ip->ip_id == fp->ipq_id &&
521			    in_hosteq(ip->ip_src, fp->ipq_src) &&
522			    in_hosteq(ip->ip_dst, fp->ipq_dst) &&
523			    ip->ip_p == fp->ipq_p)
524				goto found;
525		fp = 0;
526found:
527
528		/*
529		 * Adjust ip_len to not reflect header,
530		 * set ipqe_mff if more fragments are expected,
531		 * convert offset of this to bytes.
532		 */
533		ip->ip_len -= hlen;
534		mff = (ip->ip_off & IP_MF) != 0;
535		if (mff) {
536		        /*
537		         * Make sure that fragments have a data length
538			 * that's a non-zero multiple of 8 bytes.
539		         */
540			if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
541				ipstat.ips_badfrags++;
542				IPQ_UNLOCK();
543				goto bad;
544			}
545		}
546		ip->ip_off <<= 3;
547
548		/*
549		 * If datagram marked as having more fragments
550		 * or if this is not the first fragment,
551		 * attempt reassembly; if it succeeds, proceed.
552		 */
553		if (mff || ip->ip_off) {
554			ipstat.ips_fragments++;
555			ipqe = pool_get(&ipqent_pool, PR_NOWAIT);
556			if (ipqe == NULL) {
557				ipstat.ips_rcvmemdrop++;
558				IPQ_UNLOCK();
559				goto bad;
560			}
561			ipqe->ipqe_mff = mff;
562			ipqe->ipqe_m = m;
563			ipqe->ipqe_ip = ip;
564			m = ip_reass(ipqe, fp);
565			if (m == 0) {
566				IPQ_UNLOCK();
567				goto next;
568			}
569			ipstat.ips_reassembled++;
570			ip = mtod(m, struct ip *);
571			hlen = ip->ip_hl << 2;
572			ip->ip_len += hlen;
573		} else
574			if (fp)
575				ip_freef(fp);
576		IPQ_UNLOCK();
577	}
578
579	/*
580	 * Switch out to protocol's input routine.
581	 */
582	ipstat.ips_delivered++;
583	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
584	goto next;
585bad:
586	m_freem(m);
587	goto next;
588}
589
590/*
591 * Take incoming datagram fragment and try to
592 * reassemble it into whole datagram.  If a chain for
593 * reassembly of this datagram already exists, then it
594 * is given as fp; otherwise have to make a chain.
595 */
596struct mbuf *
597ip_reass(ipqe, fp)
598	register struct ipqent *ipqe;
599	register struct ipq *fp;
600{
601	register struct mbuf *m = ipqe->ipqe_m;
602	register struct ipqent *nq, *p, *q;
603	struct ip *ip;
604	struct mbuf *t;
605	int hlen = ipqe->ipqe_ip->ip_hl << 2;
606	int i, next;
607
608	IPQ_LOCK_CHECK();
609
610	/*
611	 * Presence of header sizes in mbufs
612	 * would confuse code below.
613	 */
614	m->m_data += hlen;
615	m->m_len -= hlen;
616
617	/*
618	 * If first fragment to arrive, create a reassembly queue.
619	 */
620	if (fp == 0) {
621		MALLOC(fp, struct ipq *, sizeof (struct ipq),
622		    M_FTABLE, M_NOWAIT);
623		if (fp == NULL)
624			goto dropfrag;
625		LIST_INSERT_HEAD(&ipq, fp, ipq_q);
626		fp->ipq_ttl = IPFRAGTTL;
627		fp->ipq_p = ipqe->ipqe_ip->ip_p;
628		fp->ipq_id = ipqe->ipqe_ip->ip_id;
629		LIST_INIT(&fp->ipq_fragq);
630		fp->ipq_src = ipqe->ipqe_ip->ip_src;
631		fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
632		p = NULL;
633		goto insert;
634	}
635
636	/*
637	 * Find a segment which begins after this one does.
638	 */
639	for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
640	    p = q, q = q->ipqe_q.le_next)
641		if (q->ipqe_ip->ip_off > ipqe->ipqe_ip->ip_off)
642			break;
643
644	/*
645	 * If there is a preceding segment, it may provide some of
646	 * our data already.  If so, drop the data from the incoming
647	 * segment.  If it provides all of our data, drop us.
648	 */
649	if (p != NULL) {
650		i = p->ipqe_ip->ip_off + p->ipqe_ip->ip_len -
651		    ipqe->ipqe_ip->ip_off;
652		if (i > 0) {
653			if (i >= ipqe->ipqe_ip->ip_len)
654				goto dropfrag;
655			m_adj(ipqe->ipqe_m, i);
656			ipqe->ipqe_ip->ip_off += i;
657			ipqe->ipqe_ip->ip_len -= i;
658		}
659	}
660
661	/*
662	 * While we overlap succeeding segments trim them or,
663	 * if they are completely covered, dequeue them.
664	 */
665	for (; q != NULL && ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len >
666	    q->ipqe_ip->ip_off; q = nq) {
667		i = (ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len) -
668		    q->ipqe_ip->ip_off;
669		if (i < q->ipqe_ip->ip_len) {
670			q->ipqe_ip->ip_len -= i;
671			q->ipqe_ip->ip_off += i;
672			m_adj(q->ipqe_m, i);
673			break;
674		}
675		nq = q->ipqe_q.le_next;
676		m_freem(q->ipqe_m);
677		LIST_REMOVE(q, ipqe_q);
678		pool_put(&ipqent_pool, q);
679	}
680
681insert:
682	/*
683	 * Stick new segment in its place;
684	 * check for complete reassembly.
685	 */
686	if (p == NULL) {
687		LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
688	} else {
689		LIST_INSERT_AFTER(p, ipqe, ipqe_q);
690	}
691	next = 0;
692	for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
693	    p = q, q = q->ipqe_q.le_next) {
694		if (q->ipqe_ip->ip_off != next)
695			return (0);
696		next += q->ipqe_ip->ip_len;
697	}
698	if (p->ipqe_mff)
699		return (0);
700
701	/*
702	 * Reassembly is complete.  Check for a bogus message size and
703	 * concatenate fragments.
704	 */
705	q = fp->ipq_fragq.lh_first;
706	ip = q->ipqe_ip;
707	if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
708		ipstat.ips_toolong++;
709		ip_freef(fp);
710		return (0);
711	}
712	m = q->ipqe_m;
713	t = m->m_next;
714	m->m_next = 0;
715	m_cat(m, t);
716	nq = q->ipqe_q.le_next;
717	pool_put(&ipqent_pool, q);
718	for (q = nq; q != NULL; q = nq) {
719		t = q->ipqe_m;
720		nq = q->ipqe_q.le_next;
721		pool_put(&ipqent_pool, q);
722		m_cat(m, t);
723	}
724
725	/*
726	 * Create header for new ip packet by
727	 * modifying header of first packet;
728	 * dequeue and discard fragment reassembly header.
729	 * Make header visible.
730	 */
731	ip->ip_len = next;
732	ip->ip_src = fp->ipq_src;
733	ip->ip_dst = fp->ipq_dst;
734	LIST_REMOVE(fp, ipq_q);
735	FREE(fp, M_FTABLE);
736	m->m_len += (ip->ip_hl << 2);
737	m->m_data -= (ip->ip_hl << 2);
738	/* some debugging cruft by sklower, below, will go away soon */
739	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
740		register int plen = 0;
741		for (t = m; t; t = t->m_next)
742			plen += t->m_len;
743		m->m_pkthdr.len = plen;
744	}
745	return (m);
746
747dropfrag:
748	ipstat.ips_fragdropped++;
749	m_freem(m);
750	pool_put(&ipqent_pool, ipqe);
751	return (0);
752}
753
754/*
755 * Free a fragment reassembly header and all
756 * associated datagrams.
757 */
758void
759ip_freef(fp)
760	struct ipq *fp;
761{
762	register struct ipqent *q, *p;
763
764	IPQ_LOCK_CHECK();
765
766	for (q = fp->ipq_fragq.lh_first; q != NULL; q = p) {
767		p = q->ipqe_q.le_next;
768		m_freem(q->ipqe_m);
769		LIST_REMOVE(q, ipqe_q);
770		pool_put(&ipqent_pool, q);
771	}
772	LIST_REMOVE(fp, ipq_q);
773	FREE(fp, M_FTABLE);
774}
775
776/*
777 * IP timer processing;
778 * if a timer expires on a reassembly
779 * queue, discard it.
780 */
781void
782ip_slowtimo()
783{
784	register struct ipq *fp, *nfp;
785	int s = splsoftnet();
786
787	IPQ_LOCK();
788	for (fp = ipq.lh_first; fp != NULL; fp = nfp) {
789		nfp = fp->ipq_q.le_next;
790		if (--fp->ipq_ttl == 0) {
791			ipstat.ips_fragtimeout++;
792			ip_freef(fp);
793		}
794	}
795	IPQ_UNLOCK();
796#ifdef GATEWAY
797	ipflow_slowtimo();
798#endif
799	splx(s);
800}
801
802/*
803 * Drain off all datagram fragments.
804 */
805void
806ip_drain()
807{
808
809	/*
810	 * We may be called from a device's interrupt context.  If
811	 * the ipq is already busy, just bail out now.
812	 */
813	if (ipq_lock_try() == 0)
814		return;
815
816	while (ipq.lh_first != NULL) {
817		ipstat.ips_fragdropped++;
818		ip_freef(ipq.lh_first);
819	}
820
821	IPQ_UNLOCK();
822}
823
824/*
825 * Do option processing on a datagram,
826 * possibly discarding it if bad options are encountered,
827 * or forwarding it if source-routed.
828 * Returns 1 if packet has been forwarded/freed,
829 * 0 if the packet should be processed further.
830 */
831int
832ip_dooptions(m)
833	struct mbuf *m;
834{
835	register struct ip *ip = mtod(m, struct ip *);
836	register u_char *cp;
837	register struct ip_timestamp *ipt;
838	register struct in_ifaddr *ia;
839	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
840	struct in_addr *sin, dst;
841	n_time ntime;
842
843	dst = ip->ip_dst;
844	cp = (u_char *)(ip + 1);
845	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
846	for (; cnt > 0; cnt -= optlen, cp += optlen) {
847		opt = cp[IPOPT_OPTVAL];
848		if (opt == IPOPT_EOL)
849			break;
850		if (opt == IPOPT_NOP)
851			optlen = 1;
852		else {
853			optlen = cp[IPOPT_OLEN];
854			if (optlen <= 0 || optlen > cnt) {
855				code = &cp[IPOPT_OLEN] - (u_char *)ip;
856				goto bad;
857			}
858		}
859		switch (opt) {
860
861		default:
862			break;
863
864		/*
865		 * Source routing with record.
866		 * Find interface with current destination address.
867		 * If none on this machine then drop if strictly routed,
868		 * or do nothing if loosely routed.
869		 * Record interface address and bring up next address
870		 * component.  If strictly routed make sure next
871		 * address is on directly accessible net.
872		 */
873		case IPOPT_LSRR:
874		case IPOPT_SSRR:
875			if (ip_allowsrcrt == 0) {
876				type = ICMP_UNREACH;
877				code = ICMP_UNREACH_NET_PROHIB;
878				goto bad;
879			}
880			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
881				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
882				goto bad;
883			}
884			ipaddr.sin_addr = ip->ip_dst;
885			ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
886			if (ia == 0) {
887				if (opt == IPOPT_SSRR) {
888					type = ICMP_UNREACH;
889					code = ICMP_UNREACH_SRCFAIL;
890					goto bad;
891				}
892				/*
893				 * Loose routing, and not at next destination
894				 * yet; nothing to do except forward.
895				 */
896				break;
897			}
898			off--;			/* 0 origin */
899			if (off > optlen - sizeof(struct in_addr)) {
900				/*
901				 * End of source route.  Should be for us.
902				 */
903				save_rte(cp, ip->ip_src);
904				break;
905			}
906			/*
907			 * locate outgoing interface
908			 */
909			bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
910			    sizeof(ipaddr.sin_addr));
911			if (opt == IPOPT_SSRR) {
912#define	INA	struct in_ifaddr *
913#define	SA	struct sockaddr *
914			    ia = (INA)ifa_ifwithladdr((SA)&ipaddr);
915			} else
916				ia = ip_rtaddr(ipaddr.sin_addr);
917			if (ia == 0) {
918				type = ICMP_UNREACH;
919				code = ICMP_UNREACH_SRCFAIL;
920				goto bad;
921			}
922			ip->ip_dst = ipaddr.sin_addr;
923			bcopy((caddr_t)&ia->ia_addr.sin_addr,
924			    (caddr_t)(cp + off), sizeof(struct in_addr));
925			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
926			/*
927			 * Let ip_intr's mcast routing check handle mcast pkts
928			 */
929			forward = !IN_MULTICAST(ip->ip_dst.s_addr);
930			break;
931
932		case IPOPT_RR:
933			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
934				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
935				goto bad;
936			}
937			/*
938			 * If no space remains, ignore.
939			 */
940			off--;			/* 0 origin */
941			if (off > optlen - sizeof(struct in_addr))
942				break;
943			bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
944			    sizeof(ipaddr.sin_addr));
945			/*
946			 * locate outgoing interface; if we're the destination,
947			 * use the incoming interface (should be same).
948			 */
949			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
950			    (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
951				type = ICMP_UNREACH;
952				code = ICMP_UNREACH_HOST;
953				goto bad;
954			}
955			bcopy((caddr_t)&ia->ia_addr.sin_addr,
956			    (caddr_t)(cp + off), sizeof(struct in_addr));
957			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
958			break;
959
960		case IPOPT_TS:
961			code = cp - (u_char *)ip;
962			ipt = (struct ip_timestamp *)cp;
963			if (ipt->ipt_len < 5)
964				goto bad;
965			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
966				if (++ipt->ipt_oflw == 0)
967					goto bad;
968				break;
969			}
970			sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
971			switch (ipt->ipt_flg) {
972
973			case IPOPT_TS_TSONLY:
974				break;
975
976			case IPOPT_TS_TSANDADDR:
977				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
978				    sizeof(struct in_addr) > ipt->ipt_len)
979					goto bad;
980				ipaddr.sin_addr = dst;
981				ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
982							    m->m_pkthdr.rcvif);
983				if (ia == 0)
984					continue;
985				bcopy((caddr_t)&ia->ia_addr.sin_addr,
986				    (caddr_t)sin, sizeof(struct in_addr));
987				ipt->ipt_ptr += sizeof(struct in_addr);
988				break;
989
990			case IPOPT_TS_PRESPEC:
991				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
992				    sizeof(struct in_addr) > ipt->ipt_len)
993					goto bad;
994				bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
995				    sizeof(struct in_addr));
996				if (ifa_ifwithaddr((SA)&ipaddr) == 0)
997					continue;
998				ipt->ipt_ptr += sizeof(struct in_addr);
999				break;
1000
1001			default:
1002				goto bad;
1003			}
1004			ntime = iptime();
1005			bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
1006			    sizeof(n_time));
1007			ipt->ipt_ptr += sizeof(n_time);
1008		}
1009	}
1010	if (forward) {
1011		if (ip_forwsrcrt == 0) {
1012			type = ICMP_UNREACH;
1013			code = ICMP_UNREACH_SRCFAIL;
1014			goto bad;
1015		}
1016		ip_forward(m, 1);
1017		return (1);
1018	}
1019	return (0);
1020bad:
1021	icmp_error(m, type, code, 0, 0);
1022	ipstat.ips_badoptions++;
1023	return (1);
1024}
1025
1026/*
1027 * Given address of next destination (final or next hop),
1028 * return internet address info of interface to be used to get there.
1029 */
1030struct in_ifaddr *
1031ip_rtaddr(dst)
1032	 struct in_addr dst;
1033{
1034	register struct sockaddr_in *sin;
1035
1036	sin = satosin(&ipforward_rt.ro_dst);
1037
1038	if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) {
1039		if (ipforward_rt.ro_rt) {
1040			RTFREE(ipforward_rt.ro_rt);
1041			ipforward_rt.ro_rt = 0;
1042		}
1043		sin->sin_family = AF_INET;
1044		sin->sin_len = sizeof(*sin);
1045		sin->sin_addr = dst;
1046
1047		rtalloc(&ipforward_rt);
1048	}
1049	if (ipforward_rt.ro_rt == 0)
1050		return ((struct in_ifaddr *)0);
1051	return (ifatoia(ipforward_rt.ro_rt->rt_ifa));
1052}
1053
1054/*
1055 * Save incoming source route for use in replies,
1056 * to be picked up later by ip_srcroute if the receiver is interested.
1057 */
1058void
1059save_rte(option, dst)
1060	u_char *option;
1061	struct in_addr dst;
1062{
1063	unsigned olen;
1064
1065	olen = option[IPOPT_OLEN];
1066#ifdef DIAGNOSTIC
1067	if (ipprintfs)
1068		printf("save_rte: olen %d\n", olen);
1069#endif
1070	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1071		return;
1072	bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
1073	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1074	ip_srcrt.dst = dst;
1075}
1076
1077/*
1078 * Retrieve incoming source route for use in replies,
1079 * in the same form used by setsockopt.
1080 * The first hop is placed before the options, will be removed later.
1081 */
1082struct mbuf *
1083ip_srcroute()
1084{
1085	register struct in_addr *p, *q;
1086	register struct mbuf *m;
1087
1088	if (ip_nhops == 0)
1089		return ((struct mbuf *)0);
1090	m = m_get(M_DONTWAIT, MT_SOOPTS);
1091	if (m == 0)
1092		return ((struct mbuf *)0);
1093
1094#define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1095
1096	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1097	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1098	    OPTSIZ;
1099#ifdef DIAGNOSTIC
1100	if (ipprintfs)
1101		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1102#endif
1103
1104	/*
1105	 * First save first hop for return route
1106	 */
1107	p = &ip_srcrt.route[ip_nhops - 1];
1108	*(mtod(m, struct in_addr *)) = *p--;
1109#ifdef DIAGNOSTIC
1110	if (ipprintfs)
1111		printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
1112#endif
1113
1114	/*
1115	 * Copy option fields and padding (nop) to mbuf.
1116	 */
1117	ip_srcrt.nop = IPOPT_NOP;
1118	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1119	bcopy((caddr_t)&ip_srcrt.nop,
1120	    mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
1121	q = (struct in_addr *)(mtod(m, caddr_t) +
1122	    sizeof(struct in_addr) + OPTSIZ);
1123#undef OPTSIZ
1124	/*
1125	 * Record return path as an IP source route,
1126	 * reversing the path (pointers are now aligned).
1127	 */
1128	while (p >= ip_srcrt.route) {
1129#ifdef DIAGNOSTIC
1130		if (ipprintfs)
1131			printf(" %x", ntohl(q->s_addr));
1132#endif
1133		*q++ = *p--;
1134	}
1135	/*
1136	 * Last hop goes to final destination.
1137	 */
1138	*q = ip_srcrt.dst;
1139#ifdef DIAGNOSTIC
1140	if (ipprintfs)
1141		printf(" %x\n", ntohl(q->s_addr));
1142#endif
1143	return (m);
1144}
1145
1146/*
1147 * Strip out IP options, at higher
1148 * level protocol in the kernel.
1149 * Second argument is buffer to which options
1150 * will be moved, and return value is their length.
1151 * XXX should be deleted; last arg currently ignored.
1152 */
1153void
1154ip_stripoptions(m, mopt)
1155	register struct mbuf *m;
1156	struct mbuf *mopt;
1157{
1158	register int i;
1159	struct ip *ip = mtod(m, struct ip *);
1160	register caddr_t opts;
1161	int olen;
1162
1163	olen = (ip->ip_hl << 2) - sizeof (struct ip);
1164	opts = (caddr_t)(ip + 1);
1165	i = m->m_len - (sizeof (struct ip) + olen);
1166	bcopy(opts  + olen, opts, (unsigned)i);
1167	m->m_len -= olen;
1168	if (m->m_flags & M_PKTHDR)
1169		m->m_pkthdr.len -= olen;
1170	ip->ip_len -= olen;
1171	ip->ip_hl = sizeof (struct ip) >> 2;
1172}
1173
1174int inetctlerrmap[PRC_NCMDS] = {
1175	0,		0,		0,		0,
1176	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1177	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1178	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1179	0,		0,		0,		0,
1180	ENOPROTOOPT
1181};
1182
1183/*
1184 * Forward a packet.  If some error occurs return the sender
1185 * an icmp packet.  Note we can't always generate a meaningful
1186 * icmp message because icmp doesn't have a large enough repertoire
1187 * of codes and types.
1188 *
1189 * If not forwarding, just drop the packet.  This could be confusing
1190 * if ipforwarding was zero but some routing protocol was advancing
1191 * us as a gateway to somewhere.  However, we must let the routing
1192 * protocol deal with that.
1193 *
1194 * The srcrt parameter indicates whether the packet is being forwarded
1195 * via a source route.
1196 */
1197void
1198ip_forward(m, srcrt)
1199	struct mbuf *m;
1200	int srcrt;
1201{
1202	register struct ip *ip = mtod(m, struct ip *);
1203	register struct sockaddr_in *sin;
1204	register struct rtentry *rt;
1205	int error, type = 0, code = 0;
1206	struct mbuf *mcopy;
1207	n_long dest;
1208	struct ifnet *destifp;
1209
1210	dest = 0;
1211#ifdef DIAGNOSTIC
1212	if (ipprintfs)
1213		printf("forward: src %2.2x dst %2.2x ttl %x\n",
1214		    ntohl(ip->ip_src.s_addr),
1215		    ntohl(ip->ip_dst.s_addr), ip->ip_ttl);
1216#endif
1217	if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
1218		ipstat.ips_cantforward++;
1219		m_freem(m);
1220		return;
1221	}
1222	if (ip->ip_ttl <= IPTTLDEC) {
1223		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1224		return;
1225	}
1226	ip->ip_ttl -= IPTTLDEC;
1227
1228	sin = satosin(&ipforward_rt.ro_dst);
1229	if ((rt = ipforward_rt.ro_rt) == 0 ||
1230	    !in_hosteq(ip->ip_dst, sin->sin_addr)) {
1231		if (ipforward_rt.ro_rt) {
1232			RTFREE(ipforward_rt.ro_rt);
1233			ipforward_rt.ro_rt = 0;
1234		}
1235		sin->sin_family = AF_INET;
1236		sin->sin_len = sizeof(struct sockaddr_in);
1237		sin->sin_addr = ip->ip_dst;
1238
1239		rtalloc(&ipforward_rt);
1240		if (ipforward_rt.ro_rt == 0) {
1241			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1242			return;
1243		}
1244		rt = ipforward_rt.ro_rt;
1245	}
1246
1247	/*
1248	 * Save at most 68 bytes of the packet in case
1249	 * we need to generate an ICMP message to the src.
1250	 */
1251	mcopy = m_copy(m, 0, imin((int)ip->ip_len, 68));
1252
1253	/*
1254	 * If forwarding packet using same interface that it came in on,
1255	 * perhaps should send a redirect to sender to shortcut a hop.
1256	 * Only send redirect if source is sending directly to us,
1257	 * and if packet was not source routed (or has any options).
1258	 * Also, don't send redirect if forwarding using a default route
1259	 * or a route modified by a redirect.
1260	 */
1261	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1262	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1263	    !in_nullhost(satosin(rt_key(rt))->sin_addr) &&
1264	    ipsendredirects && !srcrt) {
1265		if (rt->rt_ifa &&
1266		    (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
1267		    ifatoia(rt->rt_ifa)->ia_subnet) {
1268			if (rt->rt_flags & RTF_GATEWAY)
1269				dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1270			else
1271				dest = ip->ip_dst.s_addr;
1272			/*
1273			 * Router requirements says to only send host
1274			 * redirects.
1275			 */
1276			type = ICMP_REDIRECT;
1277			code = ICMP_REDIRECT_HOST;
1278#ifdef DIAGNOSTIC
1279			if (ipprintfs)
1280				printf("redirect (%d) to %x\n", code,
1281				    (u_int32_t)dest);
1282#endif
1283		}
1284	}
1285
1286	error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1287	    (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 0);
1288	if (error)
1289		ipstat.ips_cantforward++;
1290	else {
1291		ipstat.ips_forward++;
1292		if (type)
1293			ipstat.ips_redirectsent++;
1294		else {
1295			if (mcopy) {
1296#ifdef GATEWAY
1297				if (mcopy->m_flags & M_CANFASTFWD)
1298					ipflow_create(&ipforward_rt, mcopy);
1299#endif
1300				m_freem(mcopy);
1301			}
1302			return;
1303		}
1304	}
1305	if (mcopy == NULL)
1306		return;
1307	destifp = NULL;
1308
1309	switch (error) {
1310
1311	case 0:				/* forwarded, but need redirect */
1312		/* type, code set above */
1313		break;
1314
1315	case ENETUNREACH:		/* shouldn't happen, checked above */
1316	case EHOSTUNREACH:
1317	case ENETDOWN:
1318	case EHOSTDOWN:
1319	default:
1320		type = ICMP_UNREACH;
1321		code = ICMP_UNREACH_HOST;
1322		break;
1323
1324	case EMSGSIZE:
1325		type = ICMP_UNREACH;
1326		code = ICMP_UNREACH_NEEDFRAG;
1327		if (ipforward_rt.ro_rt)
1328			destifp = ipforward_rt.ro_rt->rt_ifp;
1329		ipstat.ips_cantfrag++;
1330		break;
1331
1332	case ENOBUFS:
1333		type = ICMP_SOURCEQUENCH;
1334		code = 0;
1335		break;
1336	}
1337	icmp_error(mcopy, type, code, dest, destifp);
1338}
1339
1340void
1341ip_savecontrol(inp, mp, ip, m)
1342	register struct inpcb *inp;
1343	register struct mbuf **mp;
1344	register struct ip *ip;
1345	register struct mbuf *m;
1346{
1347
1348	if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1349		struct timeval tv;
1350
1351		microtime(&tv);
1352		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1353		    SCM_TIMESTAMP, SOL_SOCKET);
1354		if (*mp)
1355			mp = &(*mp)->m_next;
1356	}
1357	if (inp->inp_flags & INP_RECVDSTADDR) {
1358		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1359		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1360		if (*mp)
1361			mp = &(*mp)->m_next;
1362	}
1363#ifdef notyet
1364	/*
1365	 * XXX
1366	 * Moving these out of udp_input() made them even more broken
1367	 * than they already were.
1368	 *	- fenner@parc.xerox.com
1369	 */
1370	/* options were tossed already */
1371	if (inp->inp_flags & INP_RECVOPTS) {
1372		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1373		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1374		if (*mp)
1375			mp = &(*mp)->m_next;
1376	}
1377	/* ip_srcroute doesn't do what we want here, need to fix */
1378	if (inp->inp_flags & INP_RECVRETOPTS) {
1379		*mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1380		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1381		if (*mp)
1382			mp = &(*mp)->m_next;
1383	}
1384#endif
1385	if (inp->inp_flags & INP_RECVIF) {
1386		struct sockaddr_dl sdl;
1387
1388		sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]);
1389		sdl.sdl_family = AF_LINK;
1390		sdl.sdl_index = m->m_pkthdr.rcvif ?
1391		    m->m_pkthdr.rcvif->if_index : 0;
1392		sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0;
1393		*mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len,
1394		    IP_RECVIF, IPPROTO_IP);
1395		if (*mp)
1396			mp = &(*mp)->m_next;
1397	}
1398}
1399
1400int
1401ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1402	int *name;
1403	u_int namelen;
1404	void *oldp;
1405	size_t *oldlenp;
1406	void *newp;
1407	size_t newlen;
1408{
1409	extern int subnetsarelocal;
1410
1411	int error, old;
1412
1413	/* All sysctl names at this level are terminal. */
1414	if (namelen != 1)
1415		return (ENOTDIR);
1416
1417	switch (name[0]) {
1418	case IPCTL_FORWARDING:
1419		return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding));
1420	case IPCTL_SENDREDIRECTS:
1421		return (sysctl_int(oldp, oldlenp, newp, newlen,
1422			&ipsendredirects));
1423	case IPCTL_DEFTTL:
1424		return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl));
1425#ifdef notyet
1426	case IPCTL_DEFMTU:
1427		return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu));
1428#endif
1429	case IPCTL_FORWSRCRT:
1430		/* Don't allow this to change in a secure environment.  */
1431		if (securelevel > 0)
1432			return (sysctl_rdint(oldp, oldlenp, newp,
1433			    ip_forwsrcrt));
1434		else
1435			return (sysctl_int(oldp, oldlenp, newp, newlen,
1436			    &ip_forwsrcrt));
1437	case IPCTL_DIRECTEDBCAST:
1438		return (sysctl_int(oldp, oldlenp, newp, newlen,
1439		    &ip_directedbcast));
1440	case IPCTL_ALLOWSRCRT:
1441		return (sysctl_int(oldp, oldlenp, newp, newlen,
1442		    &ip_allowsrcrt));
1443	case IPCTL_SUBNETSARELOCAL:
1444		return (sysctl_int(oldp, oldlenp, newp, newlen,
1445		    &subnetsarelocal));
1446	case IPCTL_MTUDISC:
1447		error = sysctl_int(oldp, oldlenp, newp, newlen,
1448		    &ip_mtudisc);
1449		if (ip_mtudisc != 0 && ip_mtudisc_timeout_q == NULL) {
1450			ip_mtudisc_timeout_q =
1451			    rt_timer_queue_create(ip_mtudisc_timeout);
1452		} else if (ip_mtudisc == 0 && ip_mtudisc_timeout_q != NULL) {
1453			rt_timer_queue_destroy(ip_mtudisc_timeout_q, TRUE);
1454			ip_mtudisc_timeout_q = NULL;
1455		}
1456		return error;
1457	case IPCTL_ANONPORTMIN:
1458		old = anonportmin;
1459		error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmin);
1460		if (anonportmin >= anonportmax || anonportmin > 65535
1461#ifndef IPNOPRIVPORTS
1462		    || anonportmin < IPPORT_RESERVED
1463#endif
1464		    ) {
1465			anonportmin = old;
1466			return (EINVAL);
1467		}
1468		return (error);
1469	case IPCTL_ANONPORTMAX:
1470		old = anonportmax;
1471		error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmax);
1472		if (anonportmin >= anonportmax || anonportmax > 65535
1473#ifndef IPNOPRIVPORTS
1474		    || anonportmax < IPPORT_RESERVED
1475#endif
1476		    ) {
1477			anonportmax = old;
1478			return (EINVAL);
1479		}
1480		return (error);
1481	case IPCTL_MTUDISCTIMEOUT:
1482		error = sysctl_int(oldp, oldlenp, newp, newlen,
1483		   &ip_mtudisc_timeout);
1484		if (ip_mtudisc_timeout_q != NULL)
1485			rt_timer_queue_change(ip_mtudisc_timeout_q,
1486					      ip_mtudisc_timeout);
1487		return (error);
1488#ifdef GATEWAY
1489	case IPCTL_MAXFLOWS:
1490	    {
1491		int s;
1492
1493		error = sysctl_int(oldp, oldlenp, newp, newlen,
1494		   &ip_maxflows);
1495		s = splsoftnet();
1496		ipflow_reap(0);
1497		splx(s);
1498		return (error);
1499	    }
1500#endif
1501	default:
1502		return (EOPNOTSUPP);
1503	}
1504	/* NOTREACHED */
1505}
1506