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