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