Home | History | Annotate | Line # | Download | only in netinet
ip_input.c revision 1.232
      1 /*	$NetBSD: ip_input.c,v 1.232 2006/09/19 21:42:30 elad Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * 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. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1998 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Public Access Networks Corporation ("Panix").  It was developed under
     38  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by the NetBSD
     51  *	Foundation, Inc. and its contributors.
     52  * 4. Neither the name of The NetBSD Foundation nor the names of its
     53  *    contributors may be used to endorse or promote products derived
     54  *    from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  * POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Copyright (c) 1982, 1986, 1988, 1993
     71  *	The Regents of the University of California.  All rights reserved.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. Neither the name of the University nor the names of its contributors
     82  *    may be used to endorse or promote products derived from this software
     83  *    without specific prior written permission.
     84  *
     85  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     86  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     87  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     88  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     89  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     90  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     91  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     92  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     93  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     94  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     95  * SUCH DAMAGE.
     96  *
     97  *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
     98  */
     99 
    100 #include <sys/cdefs.h>
    101 __KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.232 2006/09/19 21:42:30 elad Exp $");
    102 
    103 #include "opt_inet.h"
    104 #include "opt_gateway.h"
    105 #include "opt_pfil_hooks.h"
    106 #include "opt_ipsec.h"
    107 #include "opt_mrouting.h"
    108 #include "opt_mbuftrace.h"
    109 #include "opt_inet_csum.h"
    110 
    111 #include <sys/param.h>
    112 #include <sys/systm.h>
    113 #include <sys/malloc.h>
    114 #include <sys/mbuf.h>
    115 #include <sys/domain.h>
    116 #include <sys/protosw.h>
    117 #include <sys/socket.h>
    118 #include <sys/socketvar.h>
    119 #include <sys/errno.h>
    120 #include <sys/time.h>
    121 #include <sys/kernel.h>
    122 #include <sys/pool.h>
    123 #include <sys/sysctl.h>
    124 #include <sys/kauth.h>
    125 
    126 #include <net/if.h>
    127 #include <net/if_dl.h>
    128 #include <net/route.h>
    129 #include <net/pfil.h>
    130 
    131 #include <netinet/in.h>
    132 #include <netinet/in_systm.h>
    133 #include <netinet/ip.h>
    134 #include <netinet/in_pcb.h>
    135 #include <netinet/in_proto.h>
    136 #include <netinet/in_var.h>
    137 #include <netinet/ip_var.h>
    138 #include <netinet/ip_icmp.h>
    139 /* just for gif_ttl */
    140 #include <netinet/in_gif.h>
    141 #include "gif.h"
    142 #include <net/if_gre.h>
    143 #include "gre.h"
    144 
    145 #ifdef MROUTING
    146 #include <netinet/ip_mroute.h>
    147 #endif
    148 
    149 #ifdef IPSEC
    150 #include <netinet6/ipsec.h>
    151 #include <netkey/key.h>
    152 #endif
    153 #ifdef FAST_IPSEC
    154 #include <netipsec/ipsec.h>
    155 #include <netipsec/key.h>
    156 #endif	/* FAST_IPSEC*/
    157 
    158 #ifndef	IPFORWARDING
    159 #ifdef GATEWAY
    160 #define	IPFORWARDING	1	/* forward IP packets not for us */
    161 #else /* GATEWAY */
    162 #define	IPFORWARDING	0	/* don't forward IP packets not for us */
    163 #endif /* GATEWAY */
    164 #endif /* IPFORWARDING */
    165 #ifndef	IPSENDREDIRECTS
    166 #define	IPSENDREDIRECTS	1
    167 #endif
    168 #ifndef IPFORWSRCRT
    169 #define	IPFORWSRCRT	1	/* forward source-routed packets */
    170 #endif
    171 #ifndef IPALLOWSRCRT
    172 #define	IPALLOWSRCRT	1	/* allow source-routed packets */
    173 #endif
    174 #ifndef IPMTUDISC
    175 #define IPMTUDISC	1
    176 #endif
    177 #ifndef IPMTUDISCTIMEOUT
    178 #define IPMTUDISCTIMEOUT (10 * 60)	/* as per RFC 1191 */
    179 #endif
    180 
    181 /*
    182  * Note: DIRECTED_BROADCAST is handled this way so that previous
    183  * configuration using this option will Just Work.
    184  */
    185 #ifndef IPDIRECTEDBCAST
    186 #ifdef DIRECTED_BROADCAST
    187 #define IPDIRECTEDBCAST	1
    188 #else
    189 #define	IPDIRECTEDBCAST	0
    190 #endif /* DIRECTED_BROADCAST */
    191 #endif /* IPDIRECTEDBCAST */
    192 int	ipforwarding = IPFORWARDING;
    193 int	ipsendredirects = IPSENDREDIRECTS;
    194 int	ip_defttl = IPDEFTTL;
    195 int	ip_forwsrcrt = IPFORWSRCRT;
    196 int	ip_directedbcast = IPDIRECTEDBCAST;
    197 int	ip_allowsrcrt = IPALLOWSRCRT;
    198 int	ip_mtudisc = IPMTUDISC;
    199 int	ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
    200 #ifdef DIAGNOSTIC
    201 int	ipprintfs = 0;
    202 #endif
    203 
    204 int	ip_do_randomid = 0;
    205 
    206 /*
    207  * XXX - Setting ip_checkinterface mostly implements the receive side of
    208  * the Strong ES model described in RFC 1122, but since the routing table
    209  * and transmit implementation do not implement the Strong ES model,
    210  * setting this to 1 results in an odd hybrid.
    211  *
    212  * XXX - ip_checkinterface currently must be disabled if you use ipnat
    213  * to translate the destination address to another local interface.
    214  *
    215  * XXX - ip_checkinterface must be disabled if you add IP aliases
    216  * to the loopback interface instead of the interface where the
    217  * packets for those addresses are received.
    218  */
    219 int	ip_checkinterface = 0;
    220 
    221 
    222 struct rttimer_queue *ip_mtudisc_timeout_q = NULL;
    223 
    224 int	ipqmaxlen = IFQ_MAXLEN;
    225 u_long	in_ifaddrhash;				/* size of hash table - 1 */
    226 int	in_ifaddrentries;			/* total number of addrs */
    227 struct in_ifaddrhead in_ifaddrhead;
    228 struct	in_ifaddrhashhead *in_ifaddrhashtbl;
    229 u_long	in_multihash;				/* size of hash table - 1 */
    230 int	in_multientries;			/* total number of addrs */
    231 struct	in_multihashhead *in_multihashtbl;
    232 struct	ifqueue ipintrq;
    233 struct	ipstat	ipstat;
    234 uint16_t ip_id;
    235 
    236 #ifdef PFIL_HOOKS
    237 struct pfil_head inet_pfil_hook;
    238 #endif
    239 
    240 /*
    241  * Cached copy of nmbclusters. If nbclusters is different,
    242  * recalculate IP parameters derived from nmbclusters.
    243  */
    244 static int	ip_nmbclusters;			/* copy of nmbclusters */
    245 static void	ip_nmbclusters_changed(void);	/* recalc limits */
    246 
    247 #define CHECK_NMBCLUSTER_PARAMS()				\
    248 do {								\
    249 	if (__predict_false(ip_nmbclusters != nmbclusters))	\
    250 		ip_nmbclusters_changed();			\
    251 } while (/*CONSTCOND*/0)
    252 
    253 /* IP datagram reassembly queues (hashed) */
    254 #define IPREASS_NHASH_LOG2      6
    255 #define IPREASS_NHASH           (1 << IPREASS_NHASH_LOG2)
    256 #define IPREASS_HMASK           (IPREASS_NHASH - 1)
    257 #define IPREASS_HASH(x,y) \
    258 	(((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
    259 struct ipqhead ipq[IPREASS_NHASH];
    260 int	ipq_locked;
    261 static int	ip_nfragpackets;	/* packets in reass queue */
    262 static int	ip_nfrags;		/* total fragments in reass queues */
    263 
    264 int	ip_maxfragpackets = 200;	/* limit on packets. XXX sysctl */
    265 int	ip_maxfrags;		        /* limit on fragments. XXX sysctl */
    266 
    267 
    268 /*
    269  * Additive-Increase/Multiplicative-Decrease (AIMD) strategy for
    270  * IP reassembly queue buffer managment.
    271  *
    272  * We keep a count of total IP fragments (NB: not fragmented packets!)
    273  * awaiting reassembly (ip_nfrags) and a limit (ip_maxfrags) on fragments.
    274  * If ip_nfrags exceeds ip_maxfrags the limit, we drop half the
    275  * total fragments in  reassembly queues.This AIMD policy avoids
    276  * repeatedly deleting single packets under heavy fragmentation load
    277  * (e.g., from lossy NFS peers).
    278  */
    279 static u_int	ip_reass_ttl_decr(u_int ticks);
    280 static void	ip_reass_drophalf(void);
    281 
    282 
    283 static inline int ipq_lock_try(void);
    284 static inline void ipq_unlock(void);
    285 
    286 static inline int
    287 ipq_lock_try(void)
    288 {
    289 	int s;
    290 
    291 	/*
    292 	 * Use splvm() -- we're blocking things that would cause
    293 	 * mbuf allocation.
    294 	 */
    295 	s = splvm();
    296 	if (ipq_locked) {
    297 		splx(s);
    298 		return (0);
    299 	}
    300 	ipq_locked = 1;
    301 	splx(s);
    302 	return (1);
    303 }
    304 
    305 static inline void
    306 ipq_unlock(void)
    307 {
    308 	int s;
    309 
    310 	s = splvm();
    311 	ipq_locked = 0;
    312 	splx(s);
    313 }
    314 
    315 #ifdef DIAGNOSTIC
    316 #define	IPQ_LOCK()							\
    317 do {									\
    318 	if (ipq_lock_try() == 0) {					\
    319 		printf("%s:%d: ipq already locked\n", __FILE__, __LINE__); \
    320 		panic("ipq_lock");					\
    321 	}								\
    322 } while (/*CONSTCOND*/ 0)
    323 #define	IPQ_LOCK_CHECK()						\
    324 do {									\
    325 	if (ipq_locked == 0) {						\
    326 		printf("%s:%d: ipq lock not held\n", __FILE__, __LINE__); \
    327 		panic("ipq lock check");				\
    328 	}								\
    329 } while (/*CONSTCOND*/ 0)
    330 #else
    331 #define	IPQ_LOCK()		(void) ipq_lock_try()
    332 #define	IPQ_LOCK_CHECK()	/* nothing */
    333 #endif
    334 
    335 #define	IPQ_UNLOCK()		ipq_unlock()
    336 
    337 POOL_INIT(inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl", NULL);
    338 POOL_INIT(ipqent_pool, sizeof(struct ipqent), 0, 0, 0, "ipqepl", NULL);
    339 
    340 #ifdef INET_CSUM_COUNTERS
    341 #include <sys/device.h>
    342 
    343 struct evcnt ip_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    344     NULL, "inet", "hwcsum bad");
    345 struct evcnt ip_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    346     NULL, "inet", "hwcsum ok");
    347 struct evcnt ip_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    348     NULL, "inet", "swcsum");
    349 
    350 #define	INET_CSUM_COUNTER_INCR(ev)	(ev)->ev_count++
    351 
    352 EVCNT_ATTACH_STATIC(ip_hwcsum_bad);
    353 EVCNT_ATTACH_STATIC(ip_hwcsum_ok);
    354 EVCNT_ATTACH_STATIC(ip_swcsum);
    355 
    356 #else
    357 
    358 #define	INET_CSUM_COUNTER_INCR(ev)	/* nothing */
    359 
    360 #endif /* INET_CSUM_COUNTERS */
    361 
    362 /*
    363  * We need to save the IP options in case a protocol wants to respond
    364  * to an incoming packet over the same route if the packet got here
    365  * using IP source routing.  This allows connection establishment and
    366  * maintenance when the remote end is on a network that is not known
    367  * to us.
    368  */
    369 int	ip_nhops = 0;
    370 static	struct ip_srcrt {
    371 	struct	in_addr dst;			/* final destination */
    372 	char	nop;				/* one NOP to align */
    373 	char	srcopt[IPOPT_OFFSET + 1];	/* OPTVAL, OLEN and OFFSET */
    374 	struct	in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
    375 } ip_srcrt;
    376 
    377 static void save_rte(u_char *, struct in_addr);
    378 
    379 #ifdef MBUFTRACE
    380 struct mowner ip_rx_mowner = { "internet", "rx" };
    381 struct mowner ip_tx_mowner = { "internet", "tx" };
    382 #endif
    383 
    384 /*
    385  * Compute IP limits derived from the value of nmbclusters.
    386  */
    387 static void
    388 ip_nmbclusters_changed(void)
    389 {
    390 	ip_maxfrags = nmbclusters / 4;
    391 	ip_nmbclusters =  nmbclusters;
    392 }
    393 
    394 /*
    395  * IP initialization: fill in IP protocol switch table.
    396  * All protocols not implemented in kernel go to raw IP protocol handler.
    397  */
    398 void
    399 ip_init(void)
    400 {
    401 	const struct protosw *pr;
    402 	int i;
    403 
    404 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
    405 	if (pr == 0)
    406 		panic("ip_init");
    407 	for (i = 0; i < IPPROTO_MAX; i++)
    408 		ip_protox[i] = pr - inetsw;
    409 	for (pr = inetdomain.dom_protosw;
    410 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
    411 		if (pr->pr_domain->dom_family == PF_INET &&
    412 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
    413 			ip_protox[pr->pr_protocol] = pr - inetsw;
    414 
    415 	for (i = 0; i < IPREASS_NHASH; i++)
    416 	    	LIST_INIT(&ipq[i]);
    417 
    418 	ip_id = time_second & 0xfffff;
    419 
    420 	ipintrq.ifq_maxlen = ipqmaxlen;
    421 	ip_nmbclusters_changed();
    422 
    423 	TAILQ_INIT(&in_ifaddrhead);
    424 	in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, M_IFADDR,
    425 	    M_WAITOK, &in_ifaddrhash);
    426 	in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, M_IPMADDR,
    427 	    M_WAITOK, &in_multihash);
    428 	ip_mtudisc_timeout_q = rt_timer_queue_create(ip_mtudisc_timeout);
    429 #ifdef GATEWAY
    430 	ipflow_init();
    431 #endif
    432 
    433 #ifdef PFIL_HOOKS
    434 	/* Register our Packet Filter hook. */
    435 	inet_pfil_hook.ph_type = PFIL_TYPE_AF;
    436 	inet_pfil_hook.ph_af   = AF_INET;
    437 	i = pfil_head_register(&inet_pfil_hook);
    438 	if (i != 0)
    439 		printf("ip_init: WARNING: unable to register pfil hook, "
    440 		    "error %d\n", i);
    441 #endif /* PFIL_HOOKS */
    442 
    443 #ifdef MBUFTRACE
    444 	MOWNER_ATTACH(&ip_tx_mowner);
    445 	MOWNER_ATTACH(&ip_rx_mowner);
    446 #endif /* MBUFTRACE */
    447 }
    448 
    449 struct	sockaddr_in ipaddr = {
    450 	.sin_len = sizeof(ipaddr),
    451 	.sin_family = AF_INET,
    452 };
    453 struct	route ipforward_rt;
    454 
    455 /*
    456  * IP software interrupt routine
    457  */
    458 void
    459 ipintr(void)
    460 {
    461 	int s;
    462 	struct mbuf *m;
    463 
    464 	while (1) {
    465 		s = splnet();
    466 		IF_DEQUEUE(&ipintrq, m);
    467 		splx(s);
    468 		if (m == 0)
    469 			return;
    470 		MCLAIM(m, &ip_rx_mowner);
    471 		ip_input(m);
    472 	}
    473 }
    474 
    475 /*
    476  * Ip input routine.  Checksum and byte swap header.  If fragmented
    477  * try to reassemble.  Process options.  Pass to next level.
    478  */
    479 void
    480 ip_input(struct mbuf *m)
    481 {
    482 	struct ip *ip = NULL;
    483 	struct ipq *fp;
    484 	struct in_ifaddr *ia;
    485 	struct ifaddr *ifa;
    486 	struct ipqent *ipqe;
    487 	int hlen = 0, mff, len;
    488 	int downmatch;
    489 	int checkif;
    490 	int srcrt = 0;
    491 	u_int hash;
    492 #ifdef FAST_IPSEC
    493 	struct m_tag *mtag;
    494 	struct tdb_ident *tdbi;
    495 	struct secpolicy *sp;
    496 	int s, error;
    497 #endif /* FAST_IPSEC */
    498 
    499 	MCLAIM(m, &ip_rx_mowner);
    500 #ifdef	DIAGNOSTIC
    501 	if ((m->m_flags & M_PKTHDR) == 0)
    502 		panic("ipintr no HDR");
    503 #endif
    504 
    505 	/*
    506 	 * If no IP addresses have been set yet but the interfaces
    507 	 * are receiving, can't do anything with incoming packets yet.
    508 	 */
    509 	if (TAILQ_FIRST(&in_ifaddrhead) == 0)
    510 		goto bad;
    511 	ipstat.ips_total++;
    512 	/*
    513 	 * If the IP header is not aligned, slurp it up into a new
    514 	 * mbuf with space for link headers, in the event we forward
    515 	 * it.  Otherwise, if it is aligned, make sure the entire
    516 	 * base IP header is in the first mbuf of the chain.
    517 	 */
    518 	if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
    519 		if ((m = m_copyup(m, sizeof(struct ip),
    520 				  (max_linkhdr + 3) & ~3)) == NULL) {
    521 			/* XXXJRT new stat, please */
    522 			ipstat.ips_toosmall++;
    523 			return;
    524 		}
    525 	} else if (__predict_false(m->m_len < sizeof (struct ip))) {
    526 		if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
    527 			ipstat.ips_toosmall++;
    528 			return;
    529 		}
    530 	}
    531 	ip = mtod(m, struct ip *);
    532 	if (ip->ip_v != IPVERSION) {
    533 		ipstat.ips_badvers++;
    534 		goto bad;
    535 	}
    536 	hlen = ip->ip_hl << 2;
    537 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
    538 		ipstat.ips_badhlen++;
    539 		goto bad;
    540 	}
    541 	if (hlen > m->m_len) {
    542 		if ((m = m_pullup(m, hlen)) == 0) {
    543 			ipstat.ips_badhlen++;
    544 			return;
    545 		}
    546 		ip = mtod(m, struct ip *);
    547 	}
    548 
    549 	/*
    550 	 * RFC1122: packets with a multicast source address are
    551 	 * not allowed.
    552 	 */
    553 	if (IN_MULTICAST(ip->ip_src.s_addr)) {
    554 		ipstat.ips_badaddr++;
    555 		goto bad;
    556 	}
    557 
    558 	/* 127/8 must not appear on wire - RFC1122 */
    559 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
    560 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
    561 		if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
    562 			ipstat.ips_badaddr++;
    563 			goto bad;
    564 		}
    565 	}
    566 
    567 	switch (m->m_pkthdr.csum_flags &
    568 		((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
    569 		 M_CSUM_IPv4_BAD)) {
    570 	case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
    571 		INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad);
    572 		goto badcsum;
    573 
    574 	case M_CSUM_IPv4:
    575 		/* Checksum was okay. */
    576 		INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok);
    577 		break;
    578 
    579 	default:
    580 		/*
    581 		 * Must compute it ourselves.  Maybe skip checksum on
    582 		 * loopback interfaces.
    583 		 */
    584 		if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
    585 				     IFF_LOOPBACK) || ip_do_loopback_cksum)) {
    586 			INET_CSUM_COUNTER_INCR(&ip_swcsum);
    587 			if (in_cksum(m, hlen) != 0)
    588 				goto badcsum;
    589 		}
    590 		break;
    591 	}
    592 
    593 	/* Retrieve the packet length. */
    594 	len = ntohs(ip->ip_len);
    595 
    596 	/*
    597 	 * Check for additional length bogosity
    598 	 */
    599 	if (len < hlen) {
    600 	 	ipstat.ips_badlen++;
    601 		goto bad;
    602 	}
    603 
    604 	/*
    605 	 * Check that the amount of data in the buffers
    606 	 * is as at least much as the IP header would have us expect.
    607 	 * Trim mbufs if longer than we expect.
    608 	 * Drop packet if shorter than we expect.
    609 	 */
    610 	if (m->m_pkthdr.len < len) {
    611 		ipstat.ips_tooshort++;
    612 		goto bad;
    613 	}
    614 	if (m->m_pkthdr.len > len) {
    615 		if (m->m_len == m->m_pkthdr.len) {
    616 			m->m_len = len;
    617 			m->m_pkthdr.len = len;
    618 		} else
    619 			m_adj(m, len - m->m_pkthdr.len);
    620 	}
    621 
    622 #if defined(IPSEC)
    623 	/* ipflow (IP fast forwarding) is not compatible with IPsec. */
    624 	m->m_flags &= ~M_CANFASTFWD;
    625 #else
    626 	/*
    627 	 * Assume that we can create a fast-forward IP flow entry
    628 	 * based on this packet.
    629 	 */
    630 	m->m_flags |= M_CANFASTFWD;
    631 #endif
    632 
    633 #ifdef PFIL_HOOKS
    634 	/*
    635 	 * Run through list of hooks for input packets.  If there are any
    636 	 * filters which require that additional packets in the flow are
    637 	 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
    638 	 * Note that filters must _never_ set this flag, as another filter
    639 	 * in the list may have previously cleared it.
    640 	 */
    641 	/*
    642 	 * let ipfilter look at packet on the wire,
    643 	 * not the decapsulated packet.
    644 	 */
    645 #ifdef IPSEC
    646 	if (!ipsec_getnhist(m))
    647 #elif defined(FAST_IPSEC)
    648 	if (!ipsec_indone(m))
    649 #else
    650 	if (1)
    651 #endif
    652 	{
    653 		struct in_addr odst;
    654 
    655 		odst = ip->ip_dst;
    656 		if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
    657 		    PFIL_IN) != 0)
    658 			return;
    659 		if (m == NULL)
    660 			return;
    661 		ip = mtod(m, struct ip *);
    662 		hlen = ip->ip_hl << 2;
    663 		/*
    664 		 * XXX The setting of "srcrt" here is to prevent ip_forward()
    665 		 * from generating ICMP redirects for packets that have
    666 		 * been redirected by a hook back out on to the same LAN that
    667 		 * they came from and is not an indication that the packet
    668 		 * is being inffluenced by source routing options.  This
    669 		 * allows things like
    670 		 * "rdr tlp0 0/0 port 80 -> 1.1.1.200 3128 tcp"
    671 		 * where tlp0 is both on the 1.1.1.0/24 network and is the
    672 		 * default route for hosts on 1.1.1.0/24.  Of course this
    673 		 * also requires a "map tlp0 ..." to complete the story.
    674 		 * One might argue whether or not this kind of network config.
    675 		 * should be supported in this manner...
    676 		 */
    677 		srcrt = (odst.s_addr != ip->ip_dst.s_addr);
    678 	}
    679 #endif /* PFIL_HOOKS */
    680 
    681 #ifdef ALTQ
    682 	/* XXX Temporary until ALTQ is changed to use a pfil hook */
    683 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) {
    684 		/* packet dropped by traffic conditioner */
    685 		return;
    686 	}
    687 #endif
    688 
    689 	/*
    690 	 * Process options and, if not destined for us,
    691 	 * ship it on.  ip_dooptions returns 1 when an
    692 	 * error was detected (causing an icmp message
    693 	 * to be sent and the original packet to be freed).
    694 	 */
    695 	ip_nhops = 0;		/* for source routed packets */
    696 	if (hlen > sizeof (struct ip) && ip_dooptions(m))
    697 		return;
    698 
    699 	/*
    700 	 * Enable a consistency check between the destination address
    701 	 * and the arrival interface for a unicast packet (the RFC 1122
    702 	 * strong ES model) if IP forwarding is disabled and the packet
    703 	 * is not locally generated.
    704 	 *
    705 	 * XXX - Checking also should be disabled if the destination
    706 	 * address is ipnat'ed to a different interface.
    707 	 *
    708 	 * XXX - Checking is incompatible with IP aliases added
    709 	 * to the loopback interface instead of the interface where
    710 	 * the packets are received.
    711 	 *
    712 	 * XXX - We need to add a per ifaddr flag for this so that
    713 	 * we get finer grain control.
    714 	 */
    715 	checkif = ip_checkinterface && (ipforwarding == 0) &&
    716 	    (m->m_pkthdr.rcvif != NULL) &&
    717 	    ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0);
    718 
    719 	/*
    720 	 * Check our list of addresses, to see if the packet is for us.
    721 	 *
    722 	 * Traditional 4.4BSD did not consult IFF_UP at all.
    723 	 * The behavior here is to treat addresses on !IFF_UP interface
    724 	 * as not mine.
    725 	 */
    726 	downmatch = 0;
    727 	LIST_FOREACH(ia, &IN_IFADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
    728 		if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) {
    729 			if (checkif && ia->ia_ifp != m->m_pkthdr.rcvif)
    730 				continue;
    731 			if ((ia->ia_ifp->if_flags & IFF_UP) != 0)
    732 				break;
    733 			else
    734 				downmatch++;
    735 		}
    736 	}
    737 	if (ia != NULL)
    738 		goto ours;
    739 	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
    740 		IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
    741 			if (ifa->ifa_addr->sa_family != AF_INET)
    742 				continue;
    743 			ia = ifatoia(ifa);
    744 			if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
    745 			    in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
    746 			    /*
    747 			     * Look for all-0's host part (old broadcast addr),
    748 			     * either for subnet or net.
    749 			     */
    750 			    ip->ip_dst.s_addr == ia->ia_subnet ||
    751 			    ip->ip_dst.s_addr == ia->ia_net)
    752 				goto ours;
    753 			/*
    754 			 * An interface with IP address zero accepts
    755 			 * all packets that arrive on that interface.
    756 			 */
    757 			if (in_nullhost(ia->ia_addr.sin_addr))
    758 				goto ours;
    759 		}
    760 	}
    761 	if (IN_MULTICAST(ip->ip_dst.s_addr)) {
    762 		struct in_multi *inm;
    763 #ifdef MROUTING
    764 		extern struct socket *ip_mrouter;
    765 
    766 		if (ip_mrouter) {
    767 			/*
    768 			 * If we are acting as a multicast router, all
    769 			 * incoming multicast packets are passed to the
    770 			 * kernel-level multicast forwarding function.
    771 			 * The packet is returned (relatively) intact; if
    772 			 * ip_mforward() returns a non-zero value, the packet
    773 			 * must be discarded, else it may be accepted below.
    774 			 *
    775 			 * (The IP ident field is put in the same byte order
    776 			 * as expected when ip_mforward() is called from
    777 			 * ip_output().)
    778 			 */
    779 			if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
    780 				ipstat.ips_cantforward++;
    781 				m_freem(m);
    782 				return;
    783 			}
    784 
    785 			/*
    786 			 * The process-level routing demon needs to receive
    787 			 * all multicast IGMP packets, whether or not this
    788 			 * host belongs to their destination groups.
    789 			 */
    790 			if (ip->ip_p == IPPROTO_IGMP)
    791 				goto ours;
    792 			ipstat.ips_forward++;
    793 		}
    794 #endif
    795 		/*
    796 		 * See if we belong to the destination multicast group on the
    797 		 * arrival interface.
    798 		 */
    799 		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
    800 		if (inm == NULL) {
    801 			ipstat.ips_cantforward++;
    802 			m_freem(m);
    803 			return;
    804 		}
    805 		goto ours;
    806 	}
    807 	if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
    808 	    in_nullhost(ip->ip_dst))
    809 		goto ours;
    810 
    811 	/*
    812 	 * Not for us; forward if possible and desirable.
    813 	 */
    814 	if (ipforwarding == 0) {
    815 		ipstat.ips_cantforward++;
    816 		m_freem(m);
    817 	} else {
    818 		/*
    819 		 * If ip_dst matched any of my address on !IFF_UP interface,
    820 		 * and there's no IFF_UP interface that matches ip_dst,
    821 		 * send icmp unreach.  Forwarding it will result in in-kernel
    822 		 * forwarding loop till TTL goes to 0.
    823 		 */
    824 		if (downmatch) {
    825 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
    826 			ipstat.ips_cantforward++;
    827 			return;
    828 		}
    829 #ifdef IPSEC
    830 		if (ipsec4_in_reject(m, NULL)) {
    831 			ipsecstat.in_polvio++;
    832 			goto bad;
    833 		}
    834 #endif
    835 #ifdef FAST_IPSEC
    836 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
    837 		s = splsoftnet();
    838 		if (mtag != NULL) {
    839 			tdbi = (struct tdb_ident *)(mtag + 1);
    840 			sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
    841 		} else {
    842 			sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
    843 						   IP_FORWARDING, &error);
    844 		}
    845 		if (sp == NULL) {	/* NB: can happen if error */
    846 			splx(s);
    847 			/*XXX error stat???*/
    848 			DPRINTF(("ip_input: no SP for forwarding\n"));	/*XXX*/
    849 			goto bad;
    850 		}
    851 
    852 		/*
    853 		 * Check security policy against packet attributes.
    854 		 */
    855 		error = ipsec_in_reject(sp, m);
    856 		KEY_FREESP(&sp);
    857 		splx(s);
    858 		if (error) {
    859 			ipstat.ips_cantforward++;
    860 			goto bad;
    861 		}
    862 
    863 		/*
    864 		 * Peek at the outbound SP for this packet to determine if
    865 		 * it's a Fast Forward candidate.
    866 		 */
    867 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
    868 		if (mtag != NULL)
    869 			m->m_flags &= ~M_CANFASTFWD;
    870 		else {
    871 			s = splsoftnet();
    872 			sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND,
    873 			    (IP_FORWARDING |
    874 			     (ip_directedbcast ? IP_ALLOWBROADCAST : 0)),
    875 			    &error, NULL);
    876 			if (sp != NULL) {
    877 				m->m_flags &= ~M_CANFASTFWD;
    878 				KEY_FREESP(&sp);
    879 			}
    880 			splx(s);
    881 		}
    882 #endif	/* FAST_IPSEC */
    883 
    884 		ip_forward(m, srcrt);
    885 	}
    886 	return;
    887 
    888 ours:
    889 	/*
    890 	 * If offset or IP_MF are set, must reassemble.
    891 	 * Otherwise, nothing need be done.
    892 	 * (We could look in the reassembly queue to see
    893 	 * if the packet was previously fragmented,
    894 	 * but it's not worth the time; just let them time out.)
    895 	 */
    896 	if (ip->ip_off & ~htons(IP_DF|IP_RF)) {
    897 
    898 		/*
    899 		 * Look for queue of fragments
    900 		 * of this datagram.
    901 		 */
    902 		IPQ_LOCK();
    903 		hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
    904 		/* XXX LIST_FOREACH(fp, &ipq[hash], ipq_q) */
    905 		for (fp = LIST_FIRST(&ipq[hash]); fp != NULL;
    906 		     fp = LIST_NEXT(fp, ipq_q)) {
    907 			if (ip->ip_id == fp->ipq_id &&
    908 			    in_hosteq(ip->ip_src, fp->ipq_src) &&
    909 			    in_hosteq(ip->ip_dst, fp->ipq_dst) &&
    910 			    ip->ip_p == fp->ipq_p)
    911 				goto found;
    912 
    913 		}
    914 		fp = 0;
    915 found:
    916 
    917 		/*
    918 		 * Adjust ip_len to not reflect header,
    919 		 * set ipqe_mff if more fragments are expected,
    920 		 * convert offset of this to bytes.
    921 		 */
    922 		ip->ip_len = htons(ntohs(ip->ip_len) - hlen);
    923 		mff = (ip->ip_off & htons(IP_MF)) != 0;
    924 		if (mff) {
    925 		        /*
    926 		         * Make sure that fragments have a data length
    927 			 * that's a non-zero multiple of 8 bytes.
    928 		         */
    929 			if (ntohs(ip->ip_len) == 0 ||
    930 			    (ntohs(ip->ip_len) & 0x7) != 0) {
    931 				ipstat.ips_badfrags++;
    932 				IPQ_UNLOCK();
    933 				goto bad;
    934 			}
    935 		}
    936 		ip->ip_off = htons((ntohs(ip->ip_off) & IP_OFFMASK) << 3);
    937 
    938 		/*
    939 		 * If datagram marked as having more fragments
    940 		 * or if this is not the first fragment,
    941 		 * attempt reassembly; if it succeeds, proceed.
    942 		 */
    943 		if (mff || ip->ip_off != htons(0)) {
    944 			ipstat.ips_fragments++;
    945 			ipqe = pool_get(&ipqent_pool, PR_NOWAIT);
    946 			if (ipqe == NULL) {
    947 				ipstat.ips_rcvmemdrop++;
    948 				IPQ_UNLOCK();
    949 				goto bad;
    950 			}
    951 			ipqe->ipqe_mff = mff;
    952 			ipqe->ipqe_m = m;
    953 			ipqe->ipqe_ip = ip;
    954 			m = ip_reass(ipqe, fp, &ipq[hash]);
    955 			if (m == 0) {
    956 				IPQ_UNLOCK();
    957 				return;
    958 			}
    959 			ipstat.ips_reassembled++;
    960 			ip = mtod(m, struct ip *);
    961 			hlen = ip->ip_hl << 2;
    962 			ip->ip_len = htons(ntohs(ip->ip_len) + hlen);
    963 		} else
    964 			if (fp)
    965 				ip_freef(fp);
    966 		IPQ_UNLOCK();
    967 	}
    968 
    969 #if defined(IPSEC)
    970 	/*
    971 	 * enforce IPsec policy checking if we are seeing last header.
    972 	 * note that we do not visit this with protocols with pcb layer
    973 	 * code - like udp/tcp/raw ip.
    974 	 */
    975 	if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
    976 	    ipsec4_in_reject(m, NULL)) {
    977 		ipsecstat.in_polvio++;
    978 		goto bad;
    979 	}
    980 #endif
    981 #ifdef FAST_IPSEC
    982 	/*
    983 	 * enforce IPsec policy checking if we are seeing last header.
    984 	 * note that we do not visit this with protocols with pcb layer
    985 	 * code - like udp/tcp/raw ip.
    986 	 */
    987 	if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
    988 		/*
    989 		 * Check if the packet has already had IPsec processing
    990 		 * done.  If so, then just pass it along.  This tag gets
    991 		 * set during AH, ESP, etc. input handling, before the
    992 		 * packet is returned to the ip input queue for delivery.
    993 		 */
    994 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
    995 		s = splsoftnet();
    996 		if (mtag != NULL) {
    997 			tdbi = (struct tdb_ident *)(mtag + 1);
    998 			sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
    999 		} else {
   1000 			sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
   1001 						   IP_FORWARDING, &error);
   1002 		}
   1003 		if (sp != NULL) {
   1004 			/*
   1005 			 * Check security policy against packet attributes.
   1006 			 */
   1007 			error = ipsec_in_reject(sp, m);
   1008 			KEY_FREESP(&sp);
   1009 		} else {
   1010 			/* XXX error stat??? */
   1011 			error = EINVAL;
   1012 DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
   1013 			goto bad;
   1014 		}
   1015 		splx(s);
   1016 		if (error)
   1017 			goto bad;
   1018 	}
   1019 #endif /* FAST_IPSEC */
   1020 
   1021 	/*
   1022 	 * Switch out to protocol's input routine.
   1023 	 */
   1024 #if IFA_STATS
   1025 	if (ia && ip)
   1026 		ia->ia_ifa.ifa_data.ifad_inbytes += ntohs(ip->ip_len);
   1027 #endif
   1028 	ipstat.ips_delivered++;
   1029     {
   1030 	int off = hlen, nh = ip->ip_p;
   1031 
   1032 	(*inetsw[ip_protox[nh]].pr_input)(m, off, nh);
   1033 	return;
   1034     }
   1035 bad:
   1036 	m_freem(m);
   1037 	return;
   1038 
   1039 badcsum:
   1040 	ipstat.ips_badsum++;
   1041 	m_freem(m);
   1042 }
   1043 
   1044 /*
   1045  * Take incoming datagram fragment and try to
   1046  * reassemble it into whole datagram.  If a chain for
   1047  * reassembly of this datagram already exists, then it
   1048  * is given as fp; otherwise have to make a chain.
   1049  */
   1050 struct mbuf *
   1051 ip_reass(struct ipqent *ipqe, struct ipq *fp, struct ipqhead *ipqhead)
   1052 {
   1053 	struct mbuf *m = ipqe->ipqe_m;
   1054 	struct ipqent *nq, *p, *q;
   1055 	struct ip *ip;
   1056 	struct mbuf *t;
   1057 	int hlen = ipqe->ipqe_ip->ip_hl << 2;
   1058 	int i, next;
   1059 
   1060 	IPQ_LOCK_CHECK();
   1061 
   1062 	/*
   1063 	 * Presence of header sizes in mbufs
   1064 	 * would confuse code below.
   1065 	 */
   1066 	m->m_data += hlen;
   1067 	m->m_len -= hlen;
   1068 
   1069 #ifdef	notyet
   1070 	/* make sure fragment limit is up-to-date */
   1071 	CHECK_NMBCLUSTER_PARAMS();
   1072 
   1073 	/* If we have too many fragments, drop the older half. */
   1074 	if (ip_nfrags >= ip_maxfrags)
   1075 		ip_reass_drophalf(void);
   1076 #endif
   1077 
   1078 	/*
   1079 	 * We are about to add a fragment; increment frag count.
   1080 	 */
   1081 	ip_nfrags++;
   1082 
   1083 	/*
   1084 	 * If first fragment to arrive, create a reassembly queue.
   1085 	 */
   1086 	if (fp == 0) {
   1087 		/*
   1088 		 * Enforce upper bound on number of fragmented packets
   1089 		 * for which we attempt reassembly;
   1090 		 * If maxfrag is 0, never accept fragments.
   1091 		 * If maxfrag is -1, accept all fragments without limitation.
   1092 		 */
   1093 		if (ip_maxfragpackets < 0)
   1094 			;
   1095 		else if (ip_nfragpackets >= ip_maxfragpackets)
   1096 			goto dropfrag;
   1097 		ip_nfragpackets++;
   1098 		MALLOC(fp, struct ipq *, sizeof (struct ipq),
   1099 		    M_FTABLE, M_NOWAIT);
   1100 		if (fp == NULL)
   1101 			goto dropfrag;
   1102 		LIST_INSERT_HEAD(ipqhead, fp, ipq_q);
   1103 		fp->ipq_nfrags = 1;
   1104 		fp->ipq_ttl = IPFRAGTTL;
   1105 		fp->ipq_p = ipqe->ipqe_ip->ip_p;
   1106 		fp->ipq_id = ipqe->ipqe_ip->ip_id;
   1107 		TAILQ_INIT(&fp->ipq_fragq);
   1108 		fp->ipq_src = ipqe->ipqe_ip->ip_src;
   1109 		fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
   1110 		p = NULL;
   1111 		goto insert;
   1112 	} else {
   1113 		fp->ipq_nfrags++;
   1114 	}
   1115 
   1116 	/*
   1117 	 * Find a segment which begins after this one does.
   1118 	 */
   1119 	for (p = NULL, q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL;
   1120 	    p = q, q = TAILQ_NEXT(q, ipqe_q))
   1121 		if (ntohs(q->ipqe_ip->ip_off) > ntohs(ipqe->ipqe_ip->ip_off))
   1122 			break;
   1123 
   1124 	/*
   1125 	 * If there is a preceding segment, it may provide some of
   1126 	 * our data already.  If so, drop the data from the incoming
   1127 	 * segment.  If it provides all of our data, drop us.
   1128 	 */
   1129 	if (p != NULL) {
   1130 		i = ntohs(p->ipqe_ip->ip_off) + ntohs(p->ipqe_ip->ip_len) -
   1131 		    ntohs(ipqe->ipqe_ip->ip_off);
   1132 		if (i > 0) {
   1133 			if (i >= ntohs(ipqe->ipqe_ip->ip_len))
   1134 				goto dropfrag;
   1135 			m_adj(ipqe->ipqe_m, i);
   1136 			ipqe->ipqe_ip->ip_off =
   1137 			    htons(ntohs(ipqe->ipqe_ip->ip_off) + i);
   1138 			ipqe->ipqe_ip->ip_len =
   1139 			    htons(ntohs(ipqe->ipqe_ip->ip_len) - i);
   1140 		}
   1141 	}
   1142 
   1143 	/*
   1144 	 * While we overlap succeeding segments trim them or,
   1145 	 * if they are completely covered, dequeue them.
   1146 	 */
   1147 	for (; q != NULL &&
   1148 	    ntohs(ipqe->ipqe_ip->ip_off) + ntohs(ipqe->ipqe_ip->ip_len) >
   1149 	    ntohs(q->ipqe_ip->ip_off); q = nq) {
   1150 		i = (ntohs(ipqe->ipqe_ip->ip_off) +
   1151 		    ntohs(ipqe->ipqe_ip->ip_len)) - ntohs(q->ipqe_ip->ip_off);
   1152 		if (i < ntohs(q->ipqe_ip->ip_len)) {
   1153 			q->ipqe_ip->ip_len =
   1154 			    htons(ntohs(q->ipqe_ip->ip_len) - i);
   1155 			q->ipqe_ip->ip_off =
   1156 			    htons(ntohs(q->ipqe_ip->ip_off) + i);
   1157 			m_adj(q->ipqe_m, i);
   1158 			break;
   1159 		}
   1160 		nq = TAILQ_NEXT(q, ipqe_q);
   1161 		m_freem(q->ipqe_m);
   1162 		TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q);
   1163 		pool_put(&ipqent_pool, q);
   1164 		fp->ipq_nfrags--;
   1165 		ip_nfrags--;
   1166 	}
   1167 
   1168 insert:
   1169 	/*
   1170 	 * Stick new segment in its place;
   1171 	 * check for complete reassembly.
   1172 	 */
   1173 	if (p == NULL) {
   1174 		TAILQ_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
   1175 	} else {
   1176 		TAILQ_INSERT_AFTER(&fp->ipq_fragq, p, ipqe, ipqe_q);
   1177 	}
   1178 	next = 0;
   1179 	for (p = NULL, q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL;
   1180 	    p = q, q = TAILQ_NEXT(q, ipqe_q)) {
   1181 		if (ntohs(q->ipqe_ip->ip_off) != next)
   1182 			return (0);
   1183 		next += ntohs(q->ipqe_ip->ip_len);
   1184 	}
   1185 	if (p->ipqe_mff)
   1186 		return (0);
   1187 
   1188 	/*
   1189 	 * Reassembly is complete.  Check for a bogus message size and
   1190 	 * concatenate fragments.
   1191 	 */
   1192 	q = TAILQ_FIRST(&fp->ipq_fragq);
   1193 	ip = q->ipqe_ip;
   1194 	if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
   1195 		ipstat.ips_toolong++;
   1196 		ip_freef(fp);
   1197 		return (0);
   1198 	}
   1199 	m = q->ipqe_m;
   1200 	t = m->m_next;
   1201 	m->m_next = 0;
   1202 	m_cat(m, t);
   1203 	nq = TAILQ_NEXT(q, ipqe_q);
   1204 	pool_put(&ipqent_pool, q);
   1205 	for (q = nq; q != NULL; q = nq) {
   1206 		t = q->ipqe_m;
   1207 		nq = TAILQ_NEXT(q, ipqe_q);
   1208 		pool_put(&ipqent_pool, q);
   1209 		m_cat(m, t);
   1210 	}
   1211 	ip_nfrags -= fp->ipq_nfrags;
   1212 
   1213 	/*
   1214 	 * Create header for new ip packet by
   1215 	 * modifying header of first packet;
   1216 	 * dequeue and discard fragment reassembly header.
   1217 	 * Make header visible.
   1218 	 */
   1219 	ip->ip_len = htons(next);
   1220 	ip->ip_src = fp->ipq_src;
   1221 	ip->ip_dst = fp->ipq_dst;
   1222 	LIST_REMOVE(fp, ipq_q);
   1223 	FREE(fp, M_FTABLE);
   1224 	ip_nfragpackets--;
   1225 	m->m_len += (ip->ip_hl << 2);
   1226 	m->m_data -= (ip->ip_hl << 2);
   1227 	/* some debugging cruft by sklower, below, will go away soon */
   1228 	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
   1229 		int plen = 0;
   1230 		for (t = m; t; t = t->m_next)
   1231 			plen += t->m_len;
   1232 		m->m_pkthdr.len = plen;
   1233 		m->m_pkthdr.csum_flags = 0;
   1234 	}
   1235 	return (m);
   1236 
   1237 dropfrag:
   1238 	if (fp != 0)
   1239 		fp->ipq_nfrags--;
   1240 	ip_nfrags--;
   1241 	ipstat.ips_fragdropped++;
   1242 	m_freem(m);
   1243 	pool_put(&ipqent_pool, ipqe);
   1244 	return (0);
   1245 }
   1246 
   1247 /*
   1248  * Free a fragment reassembly header and all
   1249  * associated datagrams.
   1250  */
   1251 void
   1252 ip_freef(struct ipq *fp)
   1253 {
   1254 	struct ipqent *q, *p;
   1255 	u_int nfrags = 0;
   1256 
   1257 	IPQ_LOCK_CHECK();
   1258 
   1259 	for (q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL; q = p) {
   1260 		p = TAILQ_NEXT(q, ipqe_q);
   1261 		m_freem(q->ipqe_m);
   1262 		nfrags++;
   1263 		TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q);
   1264 		pool_put(&ipqent_pool, q);
   1265 	}
   1266 
   1267 	if (nfrags != fp->ipq_nfrags)
   1268 	    printf("ip_freef: nfrags %d != %d\n", fp->ipq_nfrags, nfrags);
   1269 	ip_nfrags -= nfrags;
   1270 	LIST_REMOVE(fp, ipq_q);
   1271 	FREE(fp, M_FTABLE);
   1272 	ip_nfragpackets--;
   1273 }
   1274 
   1275 /*
   1276  * IP reassembly TTL machinery for  multiplicative drop.
   1277  */
   1278 static u_int	fragttl_histo[(IPFRAGTTL+1)];
   1279 
   1280 
   1281 /*
   1282  * Decrement TTL of all reasembly queue entries by `ticks'.
   1283  * Count number of distinct fragments (as opposed to partial, fragmented
   1284  * datagrams) in the reassembly queue.  While we  traverse the entire
   1285  * reassembly queue, compute and return the median TTL over all fragments.
   1286  */
   1287 static u_int
   1288 ip_reass_ttl_decr(u_int ticks)
   1289 {
   1290 	u_int nfrags, median, dropfraction, keepfraction;
   1291 	struct ipq *fp, *nfp;
   1292 	int i;
   1293 
   1294 	nfrags = 0;
   1295 	memset(fragttl_histo, 0, sizeof fragttl_histo);
   1296 
   1297 	for (i = 0; i < IPREASS_NHASH; i++) {
   1298 		for (fp = LIST_FIRST(&ipq[i]); fp != NULL; fp = nfp) {
   1299 			fp->ipq_ttl = ((fp->ipq_ttl  <= ticks) ?
   1300 				       0 : fp->ipq_ttl - ticks);
   1301 			nfp = LIST_NEXT(fp, ipq_q);
   1302 			if (fp->ipq_ttl == 0) {
   1303 				ipstat.ips_fragtimeout++;
   1304 				ip_freef(fp);
   1305 			} else {
   1306 				nfrags += fp->ipq_nfrags;
   1307 				fragttl_histo[fp->ipq_ttl] += fp->ipq_nfrags;
   1308 			}
   1309 		}
   1310 	}
   1311 
   1312 	KASSERT(ip_nfrags == nfrags);
   1313 
   1314 	/* Find median (or other drop fraction) in histogram. */
   1315 	dropfraction = (ip_nfrags / 2);
   1316 	keepfraction = ip_nfrags - dropfraction;
   1317 	for (i = IPFRAGTTL, median = 0; i >= 0; i--) {
   1318 		median +=  fragttl_histo[i];
   1319 		if (median >= keepfraction)
   1320 			break;
   1321 	}
   1322 
   1323 	/* Return TTL of median (or other fraction). */
   1324 	return (u_int)i;
   1325 }
   1326 
   1327 void
   1328 ip_reass_drophalf(void)
   1329 {
   1330 
   1331 	u_int median_ticks;
   1332 	/*
   1333 	 * Compute median TTL of all fragments, and count frags
   1334 	 * with that TTL or lower (roughly half of all fragments).
   1335 	 */
   1336 	median_ticks = ip_reass_ttl_decr(0);
   1337 
   1338 	/* Drop half. */
   1339 	median_ticks = ip_reass_ttl_decr(median_ticks);
   1340 
   1341 }
   1342 
   1343 /*
   1344  * IP timer processing;
   1345  * if a timer expires on a reassembly
   1346  * queue, discard it.
   1347  */
   1348 void
   1349 ip_slowtimo(void)
   1350 {
   1351 	static u_int dropscanidx = 0;
   1352 	u_int i;
   1353 	u_int median_ttl;
   1354 	int s = splsoftnet();
   1355 
   1356 	IPQ_LOCK();
   1357 
   1358 	/* Age TTL of all fragments by 1 tick .*/
   1359 	median_ttl = ip_reass_ttl_decr(1);
   1360 
   1361 	/* make sure fragment limit is up-to-date */
   1362 	CHECK_NMBCLUSTER_PARAMS();
   1363 
   1364 	/* If we have too many fragments, drop the older half. */
   1365 	if (ip_nfrags > ip_maxfrags)
   1366 		ip_reass_ttl_decr(median_ttl);
   1367 
   1368 	/*
   1369 	 * If we are over the maximum number of fragmented packets
   1370 	 * (due to the limit being lowered), drain off
   1371 	 * enough to get down to the new limit. Start draining
   1372 	 * from the reassembly hashqueue most recently drained.
   1373 	 */
   1374 	if (ip_maxfragpackets < 0)
   1375 		;
   1376 	else {
   1377 		int wrapped = 0;
   1378 
   1379 		i = dropscanidx;
   1380 		while (ip_nfragpackets > ip_maxfragpackets && wrapped == 0) {
   1381 			while (LIST_FIRST(&ipq[i]) != NULL)
   1382 				ip_freef(LIST_FIRST(&ipq[i]));
   1383 			if (++i >= IPREASS_NHASH) {
   1384 				i = 0;
   1385 			}
   1386 			/*
   1387 			 * Dont scan forever even if fragment counters are
   1388 			 * wrong: stop after scanning entire reassembly queue.
   1389 			 */
   1390 			if (i == dropscanidx)
   1391 			    wrapped = 1;
   1392 		}
   1393 		dropscanidx = i;
   1394 	}
   1395 	IPQ_UNLOCK();
   1396 #ifdef GATEWAY
   1397 	ipflow_slowtimo();
   1398 #endif
   1399 	splx(s);
   1400 }
   1401 
   1402 /*
   1403  * Drain off all datagram fragments.
   1404  */
   1405 void
   1406 ip_drain(void)
   1407 {
   1408 
   1409 	/*
   1410 	 * We may be called from a device's interrupt context.  If
   1411 	 * the ipq is already busy, just bail out now.
   1412 	 */
   1413 	if (ipq_lock_try() == 0)
   1414 		return;
   1415 
   1416 	/*
   1417 	 * Drop half the total fragments now. If more mbufs are needed,
   1418 	 *  we will be called again soon.
   1419 	 */
   1420 	ip_reass_drophalf();
   1421 
   1422 	IPQ_UNLOCK();
   1423 }
   1424 
   1425 /*
   1426  * Do option processing on a datagram,
   1427  * possibly discarding it if bad options are encountered,
   1428  * or forwarding it if source-routed.
   1429  * Returns 1 if packet has been forwarded/freed,
   1430  * 0 if the packet should be processed further.
   1431  */
   1432 int
   1433 ip_dooptions(struct mbuf *m)
   1434 {
   1435 	struct ip *ip = mtod(m, struct ip *);
   1436 	u_char *cp, *cp0;
   1437 	struct ip_timestamp *ipt;
   1438 	struct in_ifaddr *ia;
   1439 	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
   1440 	struct in_addr dst;
   1441 	n_time ntime;
   1442 
   1443 	dst = ip->ip_dst;
   1444 	cp = (u_char *)(ip + 1);
   1445 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
   1446 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
   1447 		opt = cp[IPOPT_OPTVAL];
   1448 		if (opt == IPOPT_EOL)
   1449 			break;
   1450 		if (opt == IPOPT_NOP)
   1451 			optlen = 1;
   1452 		else {
   1453 			if (cnt < IPOPT_OLEN + sizeof(*cp)) {
   1454 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
   1455 				goto bad;
   1456 			}
   1457 			optlen = cp[IPOPT_OLEN];
   1458 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
   1459 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
   1460 				goto bad;
   1461 			}
   1462 		}
   1463 		switch (opt) {
   1464 
   1465 		default:
   1466 			break;
   1467 
   1468 		/*
   1469 		 * Source routing with record.
   1470 		 * Find interface with current destination address.
   1471 		 * If none on this machine then drop if strictly routed,
   1472 		 * or do nothing if loosely routed.
   1473 		 * Record interface address and bring up next address
   1474 		 * component.  If strictly routed make sure next
   1475 		 * address is on directly accessible net.
   1476 		 */
   1477 		case IPOPT_LSRR:
   1478 		case IPOPT_SSRR:
   1479 			if (ip_allowsrcrt == 0) {
   1480 				type = ICMP_UNREACH;
   1481 				code = ICMP_UNREACH_NET_PROHIB;
   1482 				goto bad;
   1483 			}
   1484 			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
   1485 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
   1486 				goto bad;
   1487 			}
   1488 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
   1489 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
   1490 				goto bad;
   1491 			}
   1492 			ipaddr.sin_addr = ip->ip_dst;
   1493 			ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
   1494 			if (ia == 0) {
   1495 				if (opt == IPOPT_SSRR) {
   1496 					type = ICMP_UNREACH;
   1497 					code = ICMP_UNREACH_SRCFAIL;
   1498 					goto bad;
   1499 				}
   1500 				/*
   1501 				 * Loose routing, and not at next destination
   1502 				 * yet; nothing to do except forward.
   1503 				 */
   1504 				break;
   1505 			}
   1506 			off--;			/* 0 origin */
   1507 			if ((off + sizeof(struct in_addr)) > optlen) {
   1508 				/*
   1509 				 * End of source route.  Should be for us.
   1510 				 */
   1511 				save_rte(cp, ip->ip_src);
   1512 				break;
   1513 			}
   1514 			/*
   1515 			 * locate outgoing interface
   1516 			 */
   1517 			bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
   1518 			    sizeof(ipaddr.sin_addr));
   1519 			if (opt == IPOPT_SSRR)
   1520 				ia = ifatoia(ifa_ifwithladdr(sintosa(&ipaddr)));
   1521 			else
   1522 				ia = ip_rtaddr(ipaddr.sin_addr);
   1523 			if (ia == 0) {
   1524 				type = ICMP_UNREACH;
   1525 				code = ICMP_UNREACH_SRCFAIL;
   1526 				goto bad;
   1527 			}
   1528 			ip->ip_dst = ipaddr.sin_addr;
   1529 			bcopy((caddr_t)&ia->ia_addr.sin_addr,
   1530 			    (caddr_t)(cp + off), sizeof(struct in_addr));
   1531 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
   1532 			/*
   1533 			 * Let ip_intr's mcast routing check handle mcast pkts
   1534 			 */
   1535 			forward = !IN_MULTICAST(ip->ip_dst.s_addr);
   1536 			break;
   1537 
   1538 		case IPOPT_RR:
   1539 			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
   1540 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
   1541 				goto bad;
   1542 			}
   1543 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
   1544 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
   1545 				goto bad;
   1546 			}
   1547 			/*
   1548 			 * If no space remains, ignore.
   1549 			 */
   1550 			off--;			/* 0 origin */
   1551 			if ((off + sizeof(struct in_addr)) > optlen)
   1552 				break;
   1553 			bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
   1554 			    sizeof(ipaddr.sin_addr));
   1555 			/*
   1556 			 * locate outgoing interface; if we're the destination,
   1557 			 * use the incoming interface (should be same).
   1558 			 */
   1559 			if ((ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))))
   1560 			    == NULL &&
   1561 			    (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) {
   1562 				type = ICMP_UNREACH;
   1563 				code = ICMP_UNREACH_HOST;
   1564 				goto bad;
   1565 			}
   1566 			bcopy((caddr_t)&ia->ia_addr.sin_addr,
   1567 			    (caddr_t)(cp + off), sizeof(struct in_addr));
   1568 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
   1569 			break;
   1570 
   1571 		case IPOPT_TS:
   1572 			code = cp - (u_char *)ip;
   1573 			ipt = (struct ip_timestamp *)cp;
   1574 			if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
   1575 				code = (u_char *)&ipt->ipt_len - (u_char *)ip;
   1576 				goto bad;
   1577 			}
   1578 			if (ipt->ipt_ptr < 5) {
   1579 				code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
   1580 				goto bad;
   1581 			}
   1582 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
   1583 				if (++ipt->ipt_oflw == 0) {
   1584 					code = (u_char *)&ipt->ipt_ptr -
   1585 					    (u_char *)ip;
   1586 					goto bad;
   1587 				}
   1588 				break;
   1589 			}
   1590 			cp0 = (cp + ipt->ipt_ptr - 1);
   1591 			switch (ipt->ipt_flg) {
   1592 
   1593 			case IPOPT_TS_TSONLY:
   1594 				break;
   1595 
   1596 			case IPOPT_TS_TSANDADDR:
   1597 				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
   1598 				    sizeof(struct in_addr) > ipt->ipt_len) {
   1599 					code = (u_char *)&ipt->ipt_ptr -
   1600 					    (u_char *)ip;
   1601 					goto bad;
   1602 				}
   1603 				ipaddr.sin_addr = dst;
   1604 				ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr),
   1605 				    m->m_pkthdr.rcvif));
   1606 				if (ia == 0)
   1607 					continue;
   1608 				bcopy(&ia->ia_addr.sin_addr,
   1609 				    cp0, sizeof(struct in_addr));
   1610 				ipt->ipt_ptr += sizeof(struct in_addr);
   1611 				break;
   1612 
   1613 			case IPOPT_TS_PRESPEC:
   1614 				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
   1615 				    sizeof(struct in_addr) > ipt->ipt_len) {
   1616 					code = (u_char *)&ipt->ipt_ptr -
   1617 					    (u_char *)ip;
   1618 					goto bad;
   1619 				}
   1620 				bcopy(cp0, &ipaddr.sin_addr,
   1621 				    sizeof(struct in_addr));
   1622 				if (ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)))
   1623 				    == NULL)
   1624 					continue;
   1625 				ipt->ipt_ptr += sizeof(struct in_addr);
   1626 				break;
   1627 
   1628 			default:
   1629 				/* XXX can't take &ipt->ipt_flg */
   1630 				code = (u_char *)&ipt->ipt_ptr -
   1631 				    (u_char *)ip + 1;
   1632 				goto bad;
   1633 			}
   1634 			ntime = iptime();
   1635 			cp0 = (u_char *) &ntime; /* XXX grumble, GCC... */
   1636 			bcopy(cp0, (caddr_t)cp + ipt->ipt_ptr - 1,
   1637 			    sizeof(n_time));
   1638 			ipt->ipt_ptr += sizeof(n_time);
   1639 		}
   1640 	}
   1641 	if (forward) {
   1642 		if (ip_forwsrcrt == 0) {
   1643 			type = ICMP_UNREACH;
   1644 			code = ICMP_UNREACH_SRCFAIL;
   1645 			goto bad;
   1646 		}
   1647 		ip_forward(m, 1);
   1648 		return (1);
   1649 	}
   1650 	return (0);
   1651 bad:
   1652 	icmp_error(m, type, code, 0, 0);
   1653 	ipstat.ips_badoptions++;
   1654 	return (1);
   1655 }
   1656 
   1657 /*
   1658  * Given address of next destination (final or next hop),
   1659  * return internet address info of interface to be used to get there.
   1660  */
   1661 struct in_ifaddr *
   1662 ip_rtaddr(struct in_addr dst)
   1663 {
   1664 	struct sockaddr_in *sin;
   1665 
   1666 	sin = satosin(&ipforward_rt.ro_dst);
   1667 
   1668 	if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) {
   1669 		if (ipforward_rt.ro_rt) {
   1670 			RTFREE(ipforward_rt.ro_rt);
   1671 			ipforward_rt.ro_rt = 0;
   1672 		}
   1673 		sin->sin_family = AF_INET;
   1674 		sin->sin_len = sizeof(*sin);
   1675 		sin->sin_addr = dst;
   1676 
   1677 		rtalloc(&ipforward_rt);
   1678 	}
   1679 	if (ipforward_rt.ro_rt == 0)
   1680 		return ((struct in_ifaddr *)0);
   1681 	return (ifatoia(ipforward_rt.ro_rt->rt_ifa));
   1682 }
   1683 
   1684 /*
   1685  * Save incoming source route for use in replies,
   1686  * to be picked up later by ip_srcroute if the receiver is interested.
   1687  */
   1688 void
   1689 save_rte(u_char *option, struct in_addr dst)
   1690 {
   1691 	unsigned olen;
   1692 
   1693 	olen = option[IPOPT_OLEN];
   1694 #ifdef DIAGNOSTIC
   1695 	if (ipprintfs)
   1696 		printf("save_rte: olen %d\n", olen);
   1697 #endif /* 0 */
   1698 	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
   1699 		return;
   1700 	bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
   1701 	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
   1702 	ip_srcrt.dst = dst;
   1703 }
   1704 
   1705 /*
   1706  * Retrieve incoming source route for use in replies,
   1707  * in the same form used by setsockopt.
   1708  * The first hop is placed before the options, will be removed later.
   1709  */
   1710 struct mbuf *
   1711 ip_srcroute(void)
   1712 {
   1713 	struct in_addr *p, *q;
   1714 	struct mbuf *m;
   1715 
   1716 	if (ip_nhops == 0)
   1717 		return ((struct mbuf *)0);
   1718 	m = m_get(M_DONTWAIT, MT_SOOPTS);
   1719 	if (m == 0)
   1720 		return ((struct mbuf *)0);
   1721 
   1722 	MCLAIM(m, &inetdomain.dom_mowner);
   1723 #define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
   1724 
   1725 	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
   1726 	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
   1727 	    OPTSIZ;
   1728 #ifdef DIAGNOSTIC
   1729 	if (ipprintfs)
   1730 		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
   1731 #endif
   1732 
   1733 	/*
   1734 	 * First save first hop for return route
   1735 	 */
   1736 	p = &ip_srcrt.route[ip_nhops - 1];
   1737 	*(mtod(m, struct in_addr *)) = *p--;
   1738 #ifdef DIAGNOSTIC
   1739 	if (ipprintfs)
   1740 		printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
   1741 #endif
   1742 
   1743 	/*
   1744 	 * Copy option fields and padding (nop) to mbuf.
   1745 	 */
   1746 	ip_srcrt.nop = IPOPT_NOP;
   1747 	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
   1748 	bcopy((caddr_t)&ip_srcrt.nop,
   1749 	    mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
   1750 	q = (struct in_addr *)(mtod(m, caddr_t) +
   1751 	    sizeof(struct in_addr) + OPTSIZ);
   1752 #undef OPTSIZ
   1753 	/*
   1754 	 * Record return path as an IP source route,
   1755 	 * reversing the path (pointers are now aligned).
   1756 	 */
   1757 	while (p >= ip_srcrt.route) {
   1758 #ifdef DIAGNOSTIC
   1759 		if (ipprintfs)
   1760 			printf(" %x", ntohl(q->s_addr));
   1761 #endif
   1762 		*q++ = *p--;
   1763 	}
   1764 	/*
   1765 	 * Last hop goes to final destination.
   1766 	 */
   1767 	*q = ip_srcrt.dst;
   1768 #ifdef DIAGNOSTIC
   1769 	if (ipprintfs)
   1770 		printf(" %x\n", ntohl(q->s_addr));
   1771 #endif
   1772 	return (m);
   1773 }
   1774 
   1775 /*
   1776  * Strip out IP options, at higher
   1777  * level protocol in the kernel.
   1778  * Second argument is buffer to which options
   1779  * will be moved, and return value is their length.
   1780  * XXX should be deleted; last arg currently ignored.
   1781  */
   1782 void
   1783 ip_stripoptions(struct mbuf *m, struct mbuf *mopt)
   1784 {
   1785 	int i;
   1786 	struct ip *ip = mtod(m, struct ip *);
   1787 	caddr_t opts;
   1788 	int olen;
   1789 
   1790 	olen = (ip->ip_hl << 2) - sizeof (struct ip);
   1791 	opts = (caddr_t)(ip + 1);
   1792 	i = m->m_len - (sizeof (struct ip) + olen);
   1793 	bcopy(opts  + olen, opts, (unsigned)i);
   1794 	m->m_len -= olen;
   1795 	if (m->m_flags & M_PKTHDR)
   1796 		m->m_pkthdr.len -= olen;
   1797 	ip->ip_len = htons(ntohs(ip->ip_len) - olen);
   1798 	ip->ip_hl = sizeof (struct ip) >> 2;
   1799 }
   1800 
   1801 const int inetctlerrmap[PRC_NCMDS] = {
   1802 	0,		0,		0,		0,
   1803 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
   1804 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
   1805 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
   1806 	0,		0,		0,		0,
   1807 	ENOPROTOOPT
   1808 };
   1809 
   1810 /*
   1811  * Forward a packet.  If some error occurs return the sender
   1812  * an icmp packet.  Note we can't always generate a meaningful
   1813  * icmp message because icmp doesn't have a large enough repertoire
   1814  * of codes and types.
   1815  *
   1816  * If not forwarding, just drop the packet.  This could be confusing
   1817  * if ipforwarding was zero but some routing protocol was advancing
   1818  * us as a gateway to somewhere.  However, we must let the routing
   1819  * protocol deal with that.
   1820  *
   1821  * The srcrt parameter indicates whether the packet is being forwarded
   1822  * via a source route.
   1823  */
   1824 void
   1825 ip_forward(struct mbuf *m, int srcrt)
   1826 {
   1827 	struct ip *ip = mtod(m, struct ip *);
   1828 	struct sockaddr_in *sin;
   1829 	struct rtentry *rt;
   1830 	int error, type = 0, code = 0, destmtu = 0;
   1831 	struct mbuf *mcopy;
   1832 	n_long dest;
   1833 
   1834 	/*
   1835 	 * We are now in the output path.
   1836 	 */
   1837 	MCLAIM(m, &ip_tx_mowner);
   1838 
   1839 	/*
   1840 	 * Clear any in-bound checksum flags for this packet.
   1841 	 */
   1842 	m->m_pkthdr.csum_flags = 0;
   1843 
   1844 	dest = 0;
   1845 #ifdef DIAGNOSTIC
   1846 	if (ipprintfs) {
   1847 		printf("forward: src %s ", inet_ntoa(ip->ip_src));
   1848 		printf("dst %s ttl %x\n", inet_ntoa(ip->ip_dst), ip->ip_ttl);
   1849 	}
   1850 #endif
   1851 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
   1852 		ipstat.ips_cantforward++;
   1853 		m_freem(m);
   1854 		return;
   1855 	}
   1856 	if (ip->ip_ttl <= IPTTLDEC) {
   1857 		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
   1858 		return;
   1859 	}
   1860 
   1861 	sin = satosin(&ipforward_rt.ro_dst);
   1862 	if ((rt = ipforward_rt.ro_rt) == 0 ||
   1863 	    !in_hosteq(ip->ip_dst, sin->sin_addr)) {
   1864 		if (ipforward_rt.ro_rt) {
   1865 			RTFREE(ipforward_rt.ro_rt);
   1866 			ipforward_rt.ro_rt = 0;
   1867 		}
   1868 		sin->sin_family = AF_INET;
   1869 		sin->sin_len = sizeof(struct sockaddr_in);
   1870 		sin->sin_addr = ip->ip_dst;
   1871 
   1872 		rtalloc(&ipforward_rt);
   1873 		if (ipforward_rt.ro_rt == 0) {
   1874 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, dest, 0);
   1875 			return;
   1876 		}
   1877 		rt = ipforward_rt.ro_rt;
   1878 	}
   1879 
   1880 	/*
   1881 	 * Save at most 68 bytes of the packet in case
   1882 	 * we need to generate an ICMP message to the src.
   1883 	 * Pullup to avoid sharing mbuf cluster between m and mcopy.
   1884 	 */
   1885 	mcopy = m_copym(m, 0, imin(ntohs(ip->ip_len), 68), M_DONTWAIT);
   1886 	if (mcopy)
   1887 		mcopy = m_pullup(mcopy, ip->ip_hl << 2);
   1888 
   1889 	ip->ip_ttl -= IPTTLDEC;
   1890 
   1891 	/*
   1892 	 * If forwarding packet using same interface that it came in on,
   1893 	 * perhaps should send a redirect to sender to shortcut a hop.
   1894 	 * Only send redirect if source is sending directly to us,
   1895 	 * and if packet was not source routed (or has any options).
   1896 	 * Also, don't send redirect if forwarding using a default route
   1897 	 * or a route modified by a redirect.
   1898 	 */
   1899 	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
   1900 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
   1901 	    !in_nullhost(satosin(rt_key(rt))->sin_addr) &&
   1902 	    ipsendredirects && !srcrt) {
   1903 		if (rt->rt_ifa &&
   1904 		    (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
   1905 		    ifatoia(rt->rt_ifa)->ia_subnet) {
   1906 			if (rt->rt_flags & RTF_GATEWAY)
   1907 				dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
   1908 			else
   1909 				dest = ip->ip_dst.s_addr;
   1910 			/*
   1911 			 * Router requirements says to only send host
   1912 			 * redirects.
   1913 			 */
   1914 			type = ICMP_REDIRECT;
   1915 			code = ICMP_REDIRECT_HOST;
   1916 #ifdef DIAGNOSTIC
   1917 			if (ipprintfs)
   1918 				printf("redirect (%d) to %x\n", code,
   1919 				    (u_int32_t)dest);
   1920 #endif
   1921 		}
   1922 	}
   1923 
   1924 	error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
   1925 	    (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)),
   1926 	    (struct ip_moptions *)NULL, (struct socket *)NULL);
   1927 
   1928 	if (error)
   1929 		ipstat.ips_cantforward++;
   1930 	else {
   1931 		ipstat.ips_forward++;
   1932 		if (type)
   1933 			ipstat.ips_redirectsent++;
   1934 		else {
   1935 			if (mcopy) {
   1936 #ifdef GATEWAY
   1937 				if (mcopy->m_flags & M_CANFASTFWD)
   1938 					ipflow_create(&ipforward_rt, mcopy);
   1939 #endif
   1940 				m_freem(mcopy);
   1941 			}
   1942 			return;
   1943 		}
   1944 	}
   1945 	if (mcopy == NULL)
   1946 		return;
   1947 
   1948 	switch (error) {
   1949 
   1950 	case 0:				/* forwarded, but need redirect */
   1951 		/* type, code set above */
   1952 		break;
   1953 
   1954 	case ENETUNREACH:		/* shouldn't happen, checked above */
   1955 	case EHOSTUNREACH:
   1956 	case ENETDOWN:
   1957 	case EHOSTDOWN:
   1958 	default:
   1959 		type = ICMP_UNREACH;
   1960 		code = ICMP_UNREACH_HOST;
   1961 		break;
   1962 
   1963 	case EMSGSIZE:
   1964 		type = ICMP_UNREACH;
   1965 		code = ICMP_UNREACH_NEEDFRAG;
   1966 #if !defined(IPSEC) && !defined(FAST_IPSEC)
   1967 		if (ipforward_rt.ro_rt)
   1968 			destmtu = ipforward_rt.ro_rt->rt_ifp->if_mtu;
   1969 #else
   1970 		/*
   1971 		 * If the packet is routed over IPsec tunnel, tell the
   1972 		 * originator the tunnel MTU.
   1973 		 *	tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
   1974 		 * XXX quickhack!!!
   1975 		 */
   1976 		if (ipforward_rt.ro_rt) {
   1977 			struct secpolicy *sp;
   1978 			int ipsecerror;
   1979 			size_t ipsechdr;
   1980 			struct route *ro;
   1981 
   1982 			sp = ipsec4_getpolicybyaddr(mcopy,
   1983 			    IPSEC_DIR_OUTBOUND, IP_FORWARDING,
   1984 			    &ipsecerror);
   1985 
   1986 			if (sp == NULL)
   1987 				destmtu = ipforward_rt.ro_rt->rt_ifp->if_mtu;
   1988 			else {
   1989 				/* count IPsec header size */
   1990 				ipsechdr = ipsec4_hdrsiz(mcopy,
   1991 				    IPSEC_DIR_OUTBOUND, NULL);
   1992 
   1993 				/*
   1994 				 * find the correct route for outer IPv4
   1995 				 * header, compute tunnel MTU.
   1996 				 */
   1997 
   1998 				if (sp->req != NULL
   1999 				 && sp->req->sav != NULL
   2000 				 && sp->req->sav->sah != NULL) {
   2001 					ro = &sp->req->sav->sah->sa_route;
   2002 					if (ro->ro_rt && ro->ro_rt->rt_ifp) {
   2003 						destmtu =
   2004 						    ro->ro_rt->rt_rmx.rmx_mtu ?
   2005 						    ro->ro_rt->rt_rmx.rmx_mtu :
   2006 						    ro->ro_rt->rt_ifp->if_mtu;
   2007 						destmtu -= ipsechdr;
   2008 					}
   2009 				}
   2010 
   2011 #ifdef	IPSEC
   2012 				key_freesp(sp);
   2013 #else
   2014 				KEY_FREESP(&sp);
   2015 #endif
   2016 			}
   2017 		}
   2018 #endif /*IPSEC*/
   2019 		ipstat.ips_cantfrag++;
   2020 		break;
   2021 
   2022 	case ENOBUFS:
   2023 #if 1
   2024 		/*
   2025 		 * a router should not generate ICMP_SOURCEQUENCH as
   2026 		 * required in RFC1812 Requirements for IP Version 4 Routers.
   2027 		 * source quench could be a big problem under DoS attacks,
   2028 		 * or if the underlying interface is rate-limited.
   2029 		 */
   2030 		if (mcopy)
   2031 			m_freem(mcopy);
   2032 		return;
   2033 #else
   2034 		type = ICMP_SOURCEQUENCH;
   2035 		code = 0;
   2036 		break;
   2037 #endif
   2038 	}
   2039 	icmp_error(mcopy, type, code, dest, destmtu);
   2040 }
   2041 
   2042 void
   2043 ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
   2044     struct mbuf *m)
   2045 {
   2046 
   2047 	if (inp->inp_socket->so_options & SO_TIMESTAMP) {
   2048 		struct timeval tv;
   2049 
   2050 		microtime(&tv);
   2051 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
   2052 		    SCM_TIMESTAMP, SOL_SOCKET);
   2053 		if (*mp)
   2054 			mp = &(*mp)->m_next;
   2055 	}
   2056 	if (inp->inp_flags & INP_RECVDSTADDR) {
   2057 		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
   2058 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
   2059 		if (*mp)
   2060 			mp = &(*mp)->m_next;
   2061 	}
   2062 #ifdef notyet
   2063 	/*
   2064 	 * XXX
   2065 	 * Moving these out of udp_input() made them even more broken
   2066 	 * than they already were.
   2067 	 *	- fenner (at) parc.xerox.com
   2068 	 */
   2069 	/* options were tossed already */
   2070 	if (inp->inp_flags & INP_RECVOPTS) {
   2071 		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
   2072 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
   2073 		if (*mp)
   2074 			mp = &(*mp)->m_next;
   2075 	}
   2076 	/* ip_srcroute doesn't do what we want here, need to fix */
   2077 	if (inp->inp_flags & INP_RECVRETOPTS) {
   2078 		*mp = sbcreatecontrol((caddr_t) ip_srcroute(),
   2079 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
   2080 		if (*mp)
   2081 			mp = &(*mp)->m_next;
   2082 	}
   2083 #endif
   2084 	if (inp->inp_flags & INP_RECVIF) {
   2085 		struct sockaddr_dl sdl;
   2086 
   2087 		sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]);
   2088 		sdl.sdl_family = AF_LINK;
   2089 		sdl.sdl_index = m->m_pkthdr.rcvif ?
   2090 		    m->m_pkthdr.rcvif->if_index : 0;
   2091 		sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0;
   2092 		*mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len,
   2093 		    IP_RECVIF, IPPROTO_IP);
   2094 		if (*mp)
   2095 			mp = &(*mp)->m_next;
   2096 	}
   2097 }
   2098 
   2099 /*
   2100  * sysctl helper routine for net.inet.ip.forwsrcrt.
   2101  */
   2102 static int
   2103 sysctl_net_inet_ip_forwsrcrt(SYSCTLFN_ARGS)
   2104 {
   2105 	int error, tmp;
   2106 	struct sysctlnode node;
   2107 
   2108 	node = *rnode;
   2109 	tmp = ip_forwsrcrt;
   2110 	node.sysctl_data = &tmp;
   2111 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2112 	if (error || newp == NULL)
   2113 		return (error);
   2114 
   2115 	if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_FORWSRCRT,
   2116 	    0, NULL, NULL, NULL))
   2117 		return (EPERM);
   2118 
   2119 	ip_forwsrcrt = tmp;
   2120 
   2121 	return (0);
   2122 }
   2123 
   2124 /*
   2125  * sysctl helper routine for net.inet.ip.mtudisctimeout.  checks the
   2126  * range of the new value and tweaks timers if it changes.
   2127  */
   2128 static int
   2129 sysctl_net_inet_ip_pmtudto(SYSCTLFN_ARGS)
   2130 {
   2131 	int error, tmp;
   2132 	struct sysctlnode node;
   2133 
   2134 	node = *rnode;
   2135 	tmp = ip_mtudisc_timeout;
   2136 	node.sysctl_data = &tmp;
   2137 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2138 	if (error || newp == NULL)
   2139 		return (error);
   2140 	if (tmp < 0)
   2141 		return (EINVAL);
   2142 
   2143 	ip_mtudisc_timeout = tmp;
   2144 	rt_timer_queue_change(ip_mtudisc_timeout_q, ip_mtudisc_timeout);
   2145 
   2146 	return (0);
   2147 }
   2148 
   2149 #ifdef GATEWAY
   2150 /*
   2151  * sysctl helper routine for net.inet.ip.maxflows.  apparently if
   2152  * maxflows is even looked up, we "reap flows".
   2153  */
   2154 static int
   2155 sysctl_net_inet_ip_maxflows(SYSCTLFN_ARGS)
   2156 {
   2157 	int s;
   2158 
   2159 	s = sysctl_lookup(SYSCTLFN_CALL(rnode));
   2160 	if (s)
   2161 		return (s);
   2162 
   2163 	s = splsoftnet();
   2164 	ipflow_reap(0);
   2165 	splx(s);
   2166 
   2167 	return (0);
   2168 }
   2169 #endif /* GATEWAY */
   2170 
   2171 
   2172 SYSCTL_SETUP(sysctl_net_inet_ip_setup, "sysctl net.inet.ip subtree setup")
   2173 {
   2174 	extern int subnetsarelocal, hostzeroisbroadcast;
   2175 
   2176 	sysctl_createv(clog, 0, NULL, NULL,
   2177 		       CTLFLAG_PERMANENT,
   2178 		       CTLTYPE_NODE, "net", NULL,
   2179 		       NULL, 0, NULL, 0,
   2180 		       CTL_NET, CTL_EOL);
   2181 	sysctl_createv(clog, 0, NULL, NULL,
   2182 		       CTLFLAG_PERMANENT,
   2183 		       CTLTYPE_NODE, "inet",
   2184 		       SYSCTL_DESCR("PF_INET related settings"),
   2185 		       NULL, 0, NULL, 0,
   2186 		       CTL_NET, PF_INET, CTL_EOL);
   2187 	sysctl_createv(clog, 0, NULL, NULL,
   2188 		       CTLFLAG_PERMANENT,
   2189 		       CTLTYPE_NODE, "ip",
   2190 		       SYSCTL_DESCR("IPv4 related settings"),
   2191 		       NULL, 0, NULL, 0,
   2192 		       CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
   2193 
   2194 	sysctl_createv(clog, 0, NULL, NULL,
   2195 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2196 		       CTLTYPE_INT, "forwarding",
   2197 		       SYSCTL_DESCR("Enable forwarding of INET datagrams"),
   2198 		       NULL, 0, &ipforwarding, 0,
   2199 		       CTL_NET, PF_INET, IPPROTO_IP,
   2200 		       IPCTL_FORWARDING, CTL_EOL);
   2201 	sysctl_createv(clog, 0, NULL, NULL,
   2202 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2203 		       CTLTYPE_INT, "redirect",
   2204 		       SYSCTL_DESCR("Enable sending of ICMP redirect messages"),
   2205 		       NULL, 0, &ipsendredirects, 0,
   2206 		       CTL_NET, PF_INET, IPPROTO_IP,
   2207 		       IPCTL_SENDREDIRECTS, CTL_EOL);
   2208 	sysctl_createv(clog, 0, NULL, NULL,
   2209 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2210 		       CTLTYPE_INT, "ttl",
   2211 		       SYSCTL_DESCR("Default TTL for an INET datagram"),
   2212 		       NULL, 0, &ip_defttl, 0,
   2213 		       CTL_NET, PF_INET, IPPROTO_IP,
   2214 		       IPCTL_DEFTTL, CTL_EOL);
   2215 #ifdef IPCTL_DEFMTU
   2216 	sysctl_createv(clog, 0, NULL, NULL,
   2217 		       CTLFLAG_PERMANENT /* |CTLFLAG_READWRITE? */,
   2218 		       CTLTYPE_INT, "mtu",
   2219 		       SYSCTL_DESCR("Default MTA for an INET route"),
   2220 		       NULL, 0, &ip_mtu, 0,
   2221 		       CTL_NET, PF_INET, IPPROTO_IP,
   2222 		       IPCTL_DEFMTU, CTL_EOL);
   2223 #endif /* IPCTL_DEFMTU */
   2224 	sysctl_createv(clog, 0, NULL, NULL,
   2225 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2226 		       CTLTYPE_INT, "forwsrcrt",
   2227 		       SYSCTL_DESCR("Enable forwarding of source-routed "
   2228 				    "datagrams"),
   2229 		       sysctl_net_inet_ip_forwsrcrt, 0, &ip_forwsrcrt, 0,
   2230 		       CTL_NET, PF_INET, IPPROTO_IP,
   2231 		       IPCTL_FORWSRCRT, CTL_EOL);
   2232 	sysctl_createv(clog, 0, NULL, NULL,
   2233 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2234 		       CTLTYPE_INT, "directed-broadcast",
   2235 		       SYSCTL_DESCR("Enable forwarding of broadcast datagrams"),
   2236 		       NULL, 0, &ip_directedbcast, 0,
   2237 		       CTL_NET, PF_INET, IPPROTO_IP,
   2238 		       IPCTL_DIRECTEDBCAST, CTL_EOL);
   2239 	sysctl_createv(clog, 0, NULL, NULL,
   2240 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2241 		       CTLTYPE_INT, "allowsrcrt",
   2242 		       SYSCTL_DESCR("Accept source-routed datagrams"),
   2243 		       NULL, 0, &ip_allowsrcrt, 0,
   2244 		       CTL_NET, PF_INET, IPPROTO_IP,
   2245 		       IPCTL_ALLOWSRCRT, CTL_EOL);
   2246 	sysctl_createv(clog, 0, NULL, NULL,
   2247 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2248 		       CTLTYPE_INT, "subnetsarelocal",
   2249 		       SYSCTL_DESCR("Whether logical subnets are considered "
   2250 				    "local"),
   2251 		       NULL, 0, &subnetsarelocal, 0,
   2252 		       CTL_NET, PF_INET, IPPROTO_IP,
   2253 		       IPCTL_SUBNETSARELOCAL, CTL_EOL);
   2254 	sysctl_createv(clog, 0, NULL, NULL,
   2255 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2256 		       CTLTYPE_INT, "mtudisc",
   2257 		       SYSCTL_DESCR("Use RFC1191 Path MTU Discovery"),
   2258 		       NULL, 0, &ip_mtudisc, 0,
   2259 		       CTL_NET, PF_INET, IPPROTO_IP,
   2260 		       IPCTL_MTUDISC, CTL_EOL);
   2261 	sysctl_createv(clog, 0, NULL, NULL,
   2262 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2263 		       CTLTYPE_INT, "anonportmin",
   2264 		       SYSCTL_DESCR("Lowest ephemeral port number to assign"),
   2265 		       sysctl_net_inet_ip_ports, 0, &anonportmin, 0,
   2266 		       CTL_NET, PF_INET, IPPROTO_IP,
   2267 		       IPCTL_ANONPORTMIN, CTL_EOL);
   2268 	sysctl_createv(clog, 0, NULL, NULL,
   2269 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2270 		       CTLTYPE_INT, "anonportmax",
   2271 		       SYSCTL_DESCR("Highest ephemeral port number to assign"),
   2272 		       sysctl_net_inet_ip_ports, 0, &anonportmax, 0,
   2273 		       CTL_NET, PF_INET, IPPROTO_IP,
   2274 		       IPCTL_ANONPORTMAX, CTL_EOL);
   2275 	sysctl_createv(clog, 0, NULL, NULL,
   2276 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2277 		       CTLTYPE_INT, "mtudisctimeout",
   2278 		       SYSCTL_DESCR("Lifetime of a Path MTU Discovered route"),
   2279 		       sysctl_net_inet_ip_pmtudto, 0, &ip_mtudisc_timeout, 0,
   2280 		       CTL_NET, PF_INET, IPPROTO_IP,
   2281 		       IPCTL_MTUDISCTIMEOUT, CTL_EOL);
   2282 #ifdef GATEWAY
   2283 	sysctl_createv(clog, 0, NULL, NULL,
   2284 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2285 		       CTLTYPE_INT, "maxflows",
   2286 		       SYSCTL_DESCR("Number of flows for fast forwarding"),
   2287 		       sysctl_net_inet_ip_maxflows, 0, &ip_maxflows, 0,
   2288 		       CTL_NET, PF_INET, IPPROTO_IP,
   2289 		       IPCTL_MAXFLOWS, CTL_EOL);
   2290 #endif /* GATEWAY */
   2291 	sysctl_createv(clog, 0, NULL, NULL,
   2292 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2293 		       CTLTYPE_INT, "hostzerobroadcast",
   2294 		       SYSCTL_DESCR("All zeroes address is broadcast address"),
   2295 		       NULL, 0, &hostzeroisbroadcast, 0,
   2296 		       CTL_NET, PF_INET, IPPROTO_IP,
   2297 		       IPCTL_HOSTZEROBROADCAST, CTL_EOL);
   2298 #if NGIF > 0
   2299 	sysctl_createv(clog, 0, NULL, NULL,
   2300 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2301 		       CTLTYPE_INT, "gifttl",
   2302 		       SYSCTL_DESCR("Default TTL for a gif tunnel datagram"),
   2303 		       NULL, 0, &ip_gif_ttl, 0,
   2304 		       CTL_NET, PF_INET, IPPROTO_IP,
   2305 		       IPCTL_GIF_TTL, CTL_EOL);
   2306 #endif /* NGIF */
   2307 #ifndef IPNOPRIVPORTS
   2308 	sysctl_createv(clog, 0, NULL, NULL,
   2309 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2310 		       CTLTYPE_INT, "lowportmin",
   2311 		       SYSCTL_DESCR("Lowest privileged ephemeral port number "
   2312 				    "to assign"),
   2313 		       sysctl_net_inet_ip_ports, 0, &lowportmin, 0,
   2314 		       CTL_NET, PF_INET, IPPROTO_IP,
   2315 		       IPCTL_LOWPORTMIN, CTL_EOL);
   2316 	sysctl_createv(clog, 0, NULL, NULL,
   2317 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2318 		       CTLTYPE_INT, "lowportmax",
   2319 		       SYSCTL_DESCR("Highest privileged ephemeral port number "
   2320 				    "to assign"),
   2321 		       sysctl_net_inet_ip_ports, 0, &lowportmax, 0,
   2322 		       CTL_NET, PF_INET, IPPROTO_IP,
   2323 		       IPCTL_LOWPORTMAX, CTL_EOL);
   2324 #endif /* IPNOPRIVPORTS */
   2325 	sysctl_createv(clog, 0, NULL, NULL,
   2326 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2327 		       CTLTYPE_INT, "maxfragpackets",
   2328 		       SYSCTL_DESCR("Maximum number of fragments to retain for "
   2329 				    "possible reassembly"),
   2330 		       NULL, 0, &ip_maxfragpackets, 0,
   2331 		       CTL_NET, PF_INET, IPPROTO_IP,
   2332 		       IPCTL_MAXFRAGPACKETS, CTL_EOL);
   2333 #if NGRE > 0
   2334 	sysctl_createv(clog, 0, NULL, NULL,
   2335 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2336 		       CTLTYPE_INT, "grettl",
   2337 		       SYSCTL_DESCR("Default TTL for a gre tunnel datagram"),
   2338 		       NULL, 0, &ip_gre_ttl, 0,
   2339 		       CTL_NET, PF_INET, IPPROTO_IP,
   2340 		       IPCTL_GRE_TTL, CTL_EOL);
   2341 #endif /* NGRE */
   2342 	sysctl_createv(clog, 0, NULL, NULL,
   2343 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2344 		       CTLTYPE_INT, "checkinterface",
   2345 		       SYSCTL_DESCR("Enable receive side of Strong ES model "
   2346 				    "from RFC1122"),
   2347 		       NULL, 0, &ip_checkinterface, 0,
   2348 		       CTL_NET, PF_INET, IPPROTO_IP,
   2349 		       IPCTL_CHECKINTERFACE, CTL_EOL);
   2350 	sysctl_createv(clog, 0, NULL, NULL,
   2351 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2352 		       CTLTYPE_INT, "random_id",
   2353 		       SYSCTL_DESCR("Assign random ip_id values"),
   2354 		       NULL, 0, &ip_do_randomid, 0,
   2355 		       CTL_NET, PF_INET, IPPROTO_IP,
   2356 		       IPCTL_RANDOMID, CTL_EOL);
   2357 	sysctl_createv(clog, 0, NULL, NULL,
   2358 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2359 		       CTLTYPE_INT, "do_loopback_cksum",
   2360 		       SYSCTL_DESCR("Perform IP checksum on loopback"),
   2361 		       NULL, 0, &ip_do_loopback_cksum, 0,
   2362 		       CTL_NET, PF_INET, IPPROTO_IP,
   2363 		       IPCTL_LOOPBACKCKSUM, CTL_EOL);
   2364 	sysctl_createv(clog, 0, NULL, NULL,
   2365 		       CTLFLAG_PERMANENT,
   2366 		       CTLTYPE_STRUCT, "stats",
   2367 		       SYSCTL_DESCR("IP statistics"),
   2368 		       NULL, 0, &ipstat, sizeof(ipstat),
   2369 		       CTL_NET, PF_INET, IPPROTO_IP, IPCTL_STATS,
   2370 		       CTL_EOL);
   2371 }
   2372