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