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