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