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