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