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