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