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