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