Home | History | Annotate | Line # | Download | only in netinet
ip_input.c revision 1.300
      1 /*	$NetBSD: ip_input.c,v 1.300 2012/06/02 21:36:47 dsl 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  *
     49  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     50  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     51  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     52  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     53  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     54  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     55  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     56  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     57  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     58  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     59  * POSSIBILITY OF SUCH DAMAGE.
     60  */
     61 
     62 /*
     63  * Copyright (c) 1982, 1986, 1988, 1993
     64  *	The Regents of the University of California.  All rights reserved.
     65  *
     66  * Redistribution and use in source and binary forms, with or without
     67  * modification, are permitted provided that the following conditions
     68  * are met:
     69  * 1. Redistributions of source code must retain the above copyright
     70  *    notice, this list of conditions and the following disclaimer.
     71  * 2. Redistributions in binary form must reproduce the above copyright
     72  *    notice, this list of conditions and the following disclaimer in the
     73  *    documentation and/or other materials provided with the distribution.
     74  * 3. Neither the name of the University nor the names of its contributors
     75  *    may be used to endorse or promote products derived from this software
     76  *    without specific prior written permission.
     77  *
     78  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     79  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     80  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     81  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     82  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     83  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     84  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     85  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     86  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     87  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     88  * SUCH DAMAGE.
     89  *
     90  *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
     91  */
     92 
     93 #include <sys/cdefs.h>
     94 __KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.300 2012/06/02 21:36:47 dsl Exp $");
     95 
     96 #include "opt_inet.h"
     97 #include "opt_compat_netbsd.h"
     98 #include "opt_gateway.h"
     99 #include "opt_pfil_hooks.h"
    100 #include "opt_ipsec.h"
    101 #include "opt_mrouting.h"
    102 #include "opt_mbuftrace.h"
    103 #include "opt_inet_csum.h"
    104 
    105 #include <sys/param.h>
    106 #include <sys/systm.h>
    107 #include <sys/mbuf.h>
    108 #include <sys/domain.h>
    109 #include <sys/protosw.h>
    110 #include <sys/socket.h>
    111 #include <sys/socketvar.h>
    112 #include <sys/errno.h>
    113 #include <sys/time.h>
    114 #include <sys/kernel.h>
    115 #include <sys/pool.h>
    116 #include <sys/sysctl.h>
    117 #include <sys/kauth.h>
    118 
    119 #include <net/if.h>
    120 #include <net/if_dl.h>
    121 #include <net/route.h>
    122 #include <net/pfil.h>
    123 
    124 #include <netinet/in.h>
    125 #include <netinet/in_systm.h>
    126 #include <netinet/ip.h>
    127 #include <netinet/in_pcb.h>
    128 #include <netinet/in_proto.h>
    129 #include <netinet/in_var.h>
    130 #include <netinet/ip_var.h>
    131 #include <netinet/ip_private.h>
    132 #include <netinet/ip_icmp.h>
    133 /* just for gif_ttl */
    134 #include <netinet/in_gif.h>
    135 #include "gif.h"
    136 #include <net/if_gre.h>
    137 #include "gre.h"
    138 
    139 #ifdef MROUTING
    140 #include <netinet/ip_mroute.h>
    141 #endif
    142 
    143 #ifdef FAST_IPSEC
    144 #include <netipsec/ipsec.h>
    145 #include <netipsec/key.h>
    146 #endif	/* FAST_IPSEC*/
    147 
    148 #ifndef	IPFORWARDING
    149 #ifdef GATEWAY
    150 #define	IPFORWARDING	1	/* forward IP packets not for us */
    151 #else /* GATEWAY */
    152 #define	IPFORWARDING	0	/* don't forward IP packets not for us */
    153 #endif /* GATEWAY */
    154 #endif /* IPFORWARDING */
    155 #ifndef	IPSENDREDIRECTS
    156 #define	IPSENDREDIRECTS	1
    157 #endif
    158 #ifndef IPFORWSRCRT
    159 #define	IPFORWSRCRT	1	/* forward source-routed packets */
    160 #endif
    161 #ifndef IPALLOWSRCRT
    162 #define	IPALLOWSRCRT	1	/* allow source-routed packets */
    163 #endif
    164 #ifndef IPMTUDISC
    165 #define IPMTUDISC	1
    166 #endif
    167 #ifndef IPMTUDISCTIMEOUT
    168 #define IPMTUDISCTIMEOUT (10 * 60)	/* as per RFC 1191 */
    169 #endif
    170 
    171 #ifdef COMPAT_50
    172 #include <compat/sys/time.h>
    173 #include <compat/sys/socket.h>
    174 #endif
    175 
    176 /*
    177  * Note: DIRECTED_BROADCAST is handled this way so that previous
    178  * configuration using this option will Just Work.
    179  */
    180 #ifndef IPDIRECTEDBCAST
    181 #ifdef DIRECTED_BROADCAST
    182 #define IPDIRECTEDBCAST	1
    183 #else
    184 #define	IPDIRECTEDBCAST	0
    185 #endif /* DIRECTED_BROADCAST */
    186 #endif /* IPDIRECTEDBCAST */
    187 int	ipforwarding = IPFORWARDING;
    188 int	ipsendredirects = IPSENDREDIRECTS;
    189 int	ip_defttl = IPDEFTTL;
    190 int	ip_forwsrcrt = IPFORWSRCRT;
    191 int	ip_directedbcast = IPDIRECTEDBCAST;
    192 int	ip_allowsrcrt = IPALLOWSRCRT;
    193 int	ip_mtudisc = IPMTUDISC;
    194 int	ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
    195 #ifdef DIAGNOSTIC
    196 int	ipprintfs = 0;
    197 #endif
    198 
    199 int	ip_do_randomid = 0;
    200 
    201 /*
    202  * XXX - Setting ip_checkinterface mostly implements the receive side of
    203  * the Strong ES model described in RFC 1122, but since the routing table
    204  * and transmit implementation do not implement the Strong ES model,
    205  * setting this to 1 results in an odd hybrid.
    206  *
    207  * XXX - ip_checkinterface currently must be disabled if you use ipnat
    208  * to translate the destination address to another local interface.
    209  *
    210  * XXX - ip_checkinterface must be disabled if you add IP aliases
    211  * to the loopback interface instead of the interface where the
    212  * packets for those addresses are received.
    213  */
    214 int	ip_checkinterface = 0;
    215 
    216 
    217 struct rttimer_queue *ip_mtudisc_timeout_q = NULL;
    218 
    219 u_long	in_ifaddrhash;				/* size of hash table - 1 */
    220 int	in_ifaddrentries;			/* total number of addrs */
    221 struct in_ifaddrhead in_ifaddrhead;
    222 struct	in_ifaddrhashhead *in_ifaddrhashtbl;
    223 u_long	in_multihash;				/* size of hash table - 1 */
    224 int	in_multientries;			/* total number of addrs */
    225 struct	in_multihashhead *in_multihashtbl;
    226 struct	ifqueue ipintrq;
    227 
    228 ipid_state_t *		ip_ids;
    229 uint16_t ip_id;
    230 
    231 percpu_t *ipstat_percpu;
    232 
    233 #ifdef PFIL_HOOKS
    234 struct pfil_head inet_pfil_hook;
    235 #endif
    236 
    237 struct pool inmulti_pool;
    238 
    239 #ifdef INET_CSUM_COUNTERS
    240 #include <sys/device.h>
    241 
    242 struct evcnt ip_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    243     NULL, "inet", "hwcsum bad");
    244 struct evcnt ip_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    245     NULL, "inet", "hwcsum ok");
    246 struct evcnt ip_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    247     NULL, "inet", "swcsum");
    248 
    249 #define	INET_CSUM_COUNTER_INCR(ev)	(ev)->ev_count++
    250 
    251 EVCNT_ATTACH_STATIC(ip_hwcsum_bad);
    252 EVCNT_ATTACH_STATIC(ip_hwcsum_ok);
    253 EVCNT_ATTACH_STATIC(ip_swcsum);
    254 
    255 #else
    256 
    257 #define	INET_CSUM_COUNTER_INCR(ev)	/* nothing */
    258 
    259 #endif /* INET_CSUM_COUNTERS */
    260 
    261 /*
    262  * We need to save the IP options in case a protocol wants to respond
    263  * to an incoming packet over the same route if the packet got here
    264  * using IP source routing.  This allows connection establishment and
    265  * maintenance when the remote end is on a network that is not known
    266  * to us.
    267  */
    268 int	ip_nhops = 0;
    269 static	struct ip_srcrt {
    270 	struct	in_addr dst;			/* final destination */
    271 	char	nop;				/* one NOP to align */
    272 	char	srcopt[IPOPT_OFFSET + 1];	/* OPTVAL, OLEN and OFFSET */
    273 	struct	in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
    274 } ip_srcrt;
    275 
    276 static int ip_drainwanted;
    277 
    278 static void save_rte(u_char *, struct in_addr);
    279 
    280 #ifdef MBUFTRACE
    281 struct mowner ip_rx_mowner = MOWNER_INIT("internet", "rx");
    282 struct mowner ip_tx_mowner = MOWNER_INIT("internet", "tx");
    283 #endif
    284 
    285 static void sysctl_net_inet_ip_setup(struct sysctllog **);
    286 
    287 /*
    288  * IP initialization: fill in IP protocol switch table.
    289  * All protocols not implemented in kernel go to raw IP protocol handler.
    290  */
    291 void
    292 ip_init(void)
    293 {
    294 	const struct protosw *pr;
    295 	int i;
    296 
    297 	sysctl_net_inet_ip_setup(NULL);
    298 
    299 	pool_init(&inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl",
    300 	    NULL, IPL_SOFTNET);
    301 
    302 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
    303 	if (pr == 0)
    304 		panic("ip_init");
    305 	for (i = 0; i < IPPROTO_MAX; i++)
    306 		ip_protox[i] = pr - inetsw;
    307 	for (pr = inetdomain.dom_protosw;
    308 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
    309 		if (pr->pr_domain->dom_family == PF_INET &&
    310 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
    311 			ip_protox[pr->pr_protocol] = pr - inetsw;
    312 
    313 	ip_reass_init();
    314 
    315 	ip_ids = ip_id_init();
    316 	ip_id = time_second & 0xfffff;
    317 
    318 	ipintrq.ifq_maxlen = IFQ_MAXLEN;
    319 
    320 	TAILQ_INIT(&in_ifaddrhead);
    321 	in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
    322 	    &in_ifaddrhash);
    323 	in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
    324 	    &in_multihash);
    325 	ip_mtudisc_timeout_q = rt_timer_queue_create(ip_mtudisc_timeout);
    326 #ifdef GATEWAY
    327 	ipflow_init(ip_hashsize);
    328 #endif
    329 
    330 #ifdef PFIL_HOOKS
    331 	/* Register our Packet Filter hook. */
    332 	inet_pfil_hook.ph_type = PFIL_TYPE_AF;
    333 	inet_pfil_hook.ph_af   = AF_INET;
    334 	i = pfil_head_register(&inet_pfil_hook);
    335 	if (i != 0)
    336 		printf("ip_init: WARNING: unable to register pfil hook, "
    337 		    "error %d\n", i);
    338 #endif /* PFIL_HOOKS */
    339 
    340 #ifdef MBUFTRACE
    341 	MOWNER_ATTACH(&ip_tx_mowner);
    342 	MOWNER_ATTACH(&ip_rx_mowner);
    343 #endif /* MBUFTRACE */
    344 
    345 	ipstat_percpu = percpu_alloc(sizeof(uint64_t) * IP_NSTATS);
    346 }
    347 
    348 struct	sockaddr_in ipaddr = {
    349 	.sin_len = sizeof(ipaddr),
    350 	.sin_family = AF_INET,
    351 };
    352 struct	route ipforward_rt;
    353 
    354 /*
    355  * IP software interrupt routine
    356  */
    357 void
    358 ipintr(void)
    359 {
    360 	int s;
    361 	struct mbuf *m;
    362 	struct ifqueue lcl_intrq;
    363 
    364 	memset(&lcl_intrq, 0, sizeof(lcl_intrq));
    365 
    366 	mutex_enter(softnet_lock);
    367 	KERNEL_LOCK(1, NULL);
    368 	if (!IF_IS_EMPTY(&ipintrq)) {
    369 		s = splnet();
    370 
    371 		/* Take existing queue onto stack */
    372 		lcl_intrq = ipintrq;
    373 
    374 		/* Zero out global queue, preserving maxlen and drops */
    375 		ipintrq.ifq_head = NULL;
    376 		ipintrq.ifq_tail = NULL;
    377 		ipintrq.ifq_len = 0;
    378 		ipintrq.ifq_maxlen = lcl_intrq.ifq_maxlen;
    379 		ipintrq.ifq_drops = lcl_intrq.ifq_drops;
    380 
    381 		splx(s);
    382 	}
    383 	KERNEL_UNLOCK_ONE(NULL);
    384 	while (!IF_IS_EMPTY(&lcl_intrq)) {
    385 		IF_DEQUEUE(&lcl_intrq, m);
    386 		if (m == NULL)
    387 			break;
    388 		ip_input(m);
    389 	}
    390 	mutex_exit(softnet_lock);
    391 }
    392 
    393 /*
    394  * Ip input routine.  Checksum and byte swap header.  If fragmented
    395  * try to reassemble.  Process options.  Pass to next level.
    396  */
    397 void
    398 ip_input(struct mbuf *m)
    399 {
    400 	struct ip *ip = NULL;
    401 	struct in_ifaddr *ia;
    402 	struct ifaddr *ifa;
    403 	int hlen = 0, len;
    404 	int downmatch;
    405 	int checkif;
    406 	int srcrt = 0;
    407 #ifdef FAST_IPSEC
    408 	struct m_tag *mtag;
    409 	struct tdb_ident *tdbi;
    410 	struct secpolicy *sp;
    411 	int error, s;
    412 #endif /* FAST_IPSEC */
    413 
    414 	MCLAIM(m, &ip_rx_mowner);
    415 	KASSERT((m->m_flags & M_PKTHDR) != 0);
    416 
    417 	/*
    418 	 * If no IP addresses have been set yet but the interfaces
    419 	 * are receiving, can't do anything with incoming packets yet.
    420 	 */
    421 	if (TAILQ_FIRST(&in_ifaddrhead) == 0)
    422 		goto bad;
    423 	IP_STATINC(IP_STAT_TOTAL);
    424 	/*
    425 	 * If the IP header is not aligned, slurp it up into a new
    426 	 * mbuf with space for link headers, in the event we forward
    427 	 * it.  Otherwise, if it is aligned, make sure the entire
    428 	 * base IP header is in the first mbuf of the chain.
    429 	 */
    430 	if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
    431 		if ((m = m_copyup(m, sizeof(struct ip),
    432 				  (max_linkhdr + 3) & ~3)) == NULL) {
    433 			/* XXXJRT new stat, please */
    434 			IP_STATINC(IP_STAT_TOOSMALL);
    435 			return;
    436 		}
    437 	} else if (__predict_false(m->m_len < sizeof (struct ip))) {
    438 		if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
    439 			IP_STATINC(IP_STAT_TOOSMALL);
    440 			return;
    441 		}
    442 	}
    443 	ip = mtod(m, struct ip *);
    444 	if (ip->ip_v != IPVERSION) {
    445 		IP_STATINC(IP_STAT_BADVERS);
    446 		goto bad;
    447 	}
    448 	hlen = ip->ip_hl << 2;
    449 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
    450 		IP_STATINC(IP_STAT_BADHLEN);
    451 		goto bad;
    452 	}
    453 	if (hlen > m->m_len) {
    454 		if ((m = m_pullup(m, hlen)) == NULL) {
    455 			IP_STATINC(IP_STAT_BADHLEN);
    456 			return;
    457 		}
    458 		ip = mtod(m, struct ip *);
    459 	}
    460 
    461 	/*
    462 	 * RFC1122: packets with a multicast source address are
    463 	 * not allowed.
    464 	 */
    465 	if (IN_MULTICAST(ip->ip_src.s_addr)) {
    466 		IP_STATINC(IP_STAT_BADADDR);
    467 		goto bad;
    468 	}
    469 
    470 	/* 127/8 must not appear on wire - RFC1122 */
    471 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
    472 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
    473 		if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
    474 			IP_STATINC(IP_STAT_BADADDR);
    475 			goto bad;
    476 		}
    477 	}
    478 
    479 	switch (m->m_pkthdr.csum_flags &
    480 		((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
    481 		 M_CSUM_IPv4_BAD)) {
    482 	case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
    483 		INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad);
    484 		goto badcsum;
    485 
    486 	case M_CSUM_IPv4:
    487 		/* Checksum was okay. */
    488 		INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok);
    489 		break;
    490 
    491 	default:
    492 		/*
    493 		 * Must compute it ourselves.  Maybe skip checksum on
    494 		 * loopback interfaces.
    495 		 */
    496 		if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
    497 				     IFF_LOOPBACK) || ip_do_loopback_cksum)) {
    498 			INET_CSUM_COUNTER_INCR(&ip_swcsum);
    499 			if (in_cksum(m, hlen) != 0)
    500 				goto badcsum;
    501 		}
    502 		break;
    503 	}
    504 
    505 	/* Retrieve the packet length. */
    506 	len = ntohs(ip->ip_len);
    507 
    508 	/*
    509 	 * Check for additional length bogosity
    510 	 */
    511 	if (len < hlen) {
    512 		IP_STATINC(IP_STAT_BADLEN);
    513 		goto bad;
    514 	}
    515 
    516 	/*
    517 	 * Check that the amount of data in the buffers
    518 	 * is as at least much as the IP header would have us expect.
    519 	 * Trim mbufs if longer than we expect.
    520 	 * Drop packet if shorter than we expect.
    521 	 */
    522 	if (m->m_pkthdr.len < len) {
    523 		IP_STATINC(IP_STAT_TOOSHORT);
    524 		goto bad;
    525 	}
    526 	if (m->m_pkthdr.len > len) {
    527 		if (m->m_len == m->m_pkthdr.len) {
    528 			m->m_len = len;
    529 			m->m_pkthdr.len = len;
    530 		} else
    531 			m_adj(m, len - m->m_pkthdr.len);
    532 	}
    533 
    534 	/*
    535 	 * Assume that we can create a fast-forward IP flow entry
    536 	 * based on this packet.
    537 	 */
    538 	m->m_flags |= M_CANFASTFWD;
    539 
    540 #ifdef PFIL_HOOKS
    541 	/*
    542 	 * Run through list of hooks for input packets.  If there are any
    543 	 * filters which require that additional packets in the flow are
    544 	 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
    545 	 * Note that filters must _never_ set this flag, as another filter
    546 	 * in the list may have previously cleared it.
    547 	 */
    548 	/*
    549 	 * let ipfilter look at packet on the wire,
    550 	 * not the decapsulated packet.
    551 	 */
    552 #if defined(FAST_IPSEC)
    553 	if (!ipsec_indone(m))
    554 #else
    555 	if (1)
    556 #endif
    557 	{
    558 		struct in_addr odst;
    559 
    560 		odst = ip->ip_dst;
    561 		if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
    562 		    PFIL_IN) != 0)
    563 			return;
    564 		if (m == NULL)
    565 			return;
    566 		ip = mtod(m, struct ip *);
    567 		hlen = ip->ip_hl << 2;
    568 		/*
    569 		 * XXX The setting of "srcrt" here is to prevent ip_forward()
    570 		 * from generating ICMP redirects for packets that have
    571 		 * been redirected by a hook back out on to the same LAN that
    572 		 * they came from and is not an indication that the packet
    573 		 * is being inffluenced by source routing options.  This
    574 		 * allows things like
    575 		 * "rdr tlp0 0/0 port 80 -> 1.1.1.200 3128 tcp"
    576 		 * where tlp0 is both on the 1.1.1.0/24 network and is the
    577 		 * default route for hosts on 1.1.1.0/24.  Of course this
    578 		 * also requires a "map tlp0 ..." to complete the story.
    579 		 * One might argue whether or not this kind of network config.
    580 		 * should be supported in this manner...
    581 		 */
    582 		srcrt = (odst.s_addr != ip->ip_dst.s_addr);
    583 	}
    584 #endif /* PFIL_HOOKS */
    585 
    586 #ifdef ALTQ
    587 	/* XXX Temporary until ALTQ is changed to use a pfil hook */
    588 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) {
    589 		/* packet dropped by traffic conditioner */
    590 		return;
    591 	}
    592 #endif
    593 
    594 	/*
    595 	 * Process options and, if not destined for us,
    596 	 * ship it on.  ip_dooptions returns 1 when an
    597 	 * error was detected (causing an icmp message
    598 	 * to be sent and the original packet to be freed).
    599 	 */
    600 	ip_nhops = 0;		/* for source routed packets */
    601 	if (hlen > sizeof (struct ip) && ip_dooptions(m))
    602 		return;
    603 
    604 	/*
    605 	 * Enable a consistency check between the destination address
    606 	 * and the arrival interface for a unicast packet (the RFC 1122
    607 	 * strong ES model) if IP forwarding is disabled and the packet
    608 	 * is not locally generated.
    609 	 *
    610 	 * XXX - Checking also should be disabled if the destination
    611 	 * address is ipnat'ed to a different interface.
    612 	 *
    613 	 * XXX - Checking is incompatible with IP aliases added
    614 	 * to the loopback interface instead of the interface where
    615 	 * the packets are received.
    616 	 *
    617 	 * XXX - We need to add a per ifaddr flag for this so that
    618 	 * we get finer grain control.
    619 	 */
    620 	checkif = ip_checkinterface && (ipforwarding == 0) &&
    621 	    (m->m_pkthdr.rcvif != NULL) &&
    622 	    ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0);
    623 
    624 	/*
    625 	 * Check our list of addresses, to see if the packet is for us.
    626 	 *
    627 	 * Traditional 4.4BSD did not consult IFF_UP at all.
    628 	 * The behavior here is to treat addresses on !IFF_UP interface
    629 	 * as not mine.
    630 	 */
    631 	downmatch = 0;
    632 	LIST_FOREACH(ia, &IN_IFADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
    633 		if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) {
    634 			if (checkif && ia->ia_ifp != m->m_pkthdr.rcvif)
    635 				continue;
    636 			if ((ia->ia_ifp->if_flags & IFF_UP) != 0)
    637 				break;
    638 			else
    639 				downmatch++;
    640 		}
    641 	}
    642 	if (ia != NULL)
    643 		goto ours;
    644 	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
    645 		IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
    646 			if (ifa->ifa_addr->sa_family != AF_INET)
    647 				continue;
    648 			ia = ifatoia(ifa);
    649 			if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
    650 			    in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
    651 			    /*
    652 			     * Look for all-0's host part (old broadcast addr),
    653 			     * either for subnet or net.
    654 			     */
    655 			    ip->ip_dst.s_addr == ia->ia_subnet ||
    656 			    ip->ip_dst.s_addr == ia->ia_net)
    657 				goto ours;
    658 			/*
    659 			 * An interface with IP address zero accepts
    660 			 * all packets that arrive on that interface.
    661 			 */
    662 			if (in_nullhost(ia->ia_addr.sin_addr))
    663 				goto ours;
    664 		}
    665 	}
    666 	if (IN_MULTICAST(ip->ip_dst.s_addr)) {
    667 		struct in_multi *inm;
    668 #ifdef MROUTING
    669 		extern struct socket *ip_mrouter;
    670 
    671 		if (ip_mrouter) {
    672 			/*
    673 			 * If we are acting as a multicast router, all
    674 			 * incoming multicast packets are passed to the
    675 			 * kernel-level multicast forwarding function.
    676 			 * The packet is returned (relatively) intact; if
    677 			 * ip_mforward() returns a non-zero value, the packet
    678 			 * must be discarded, else it may be accepted below.
    679 			 *
    680 			 * (The IP ident field is put in the same byte order
    681 			 * as expected when ip_mforward() is called from
    682 			 * ip_output().)
    683 			 */
    684 			if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
    685 				IP_STATINC(IP_STAT_CANTFORWARD);
    686 				m_freem(m);
    687 				return;
    688 			}
    689 
    690 			/*
    691 			 * The process-level routing demon needs to receive
    692 			 * all multicast IGMP packets, whether or not this
    693 			 * host belongs to their destination groups.
    694 			 */
    695 			if (ip->ip_p == IPPROTO_IGMP)
    696 				goto ours;
    697 			IP_STATINC(IP_STAT_CANTFORWARD);
    698 		}
    699 #endif
    700 		/*
    701 		 * See if we belong to the destination multicast group on the
    702 		 * arrival interface.
    703 		 */
    704 		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
    705 		if (inm == NULL) {
    706 			IP_STATINC(IP_STAT_CANTFORWARD);
    707 			m_freem(m);
    708 			return;
    709 		}
    710 		goto ours;
    711 	}
    712 	if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
    713 	    in_nullhost(ip->ip_dst))
    714 		goto ours;
    715 
    716 	/*
    717 	 * Not for us; forward if possible and desirable.
    718 	 */
    719 	if (ipforwarding == 0) {
    720 		IP_STATINC(IP_STAT_CANTFORWARD);
    721 		m_freem(m);
    722 	} else {
    723 		/*
    724 		 * If ip_dst matched any of my address on !IFF_UP interface,
    725 		 * and there's no IFF_UP interface that matches ip_dst,
    726 		 * send icmp unreach.  Forwarding it will result in in-kernel
    727 		 * forwarding loop till TTL goes to 0.
    728 		 */
    729 		if (downmatch) {
    730 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
    731 			IP_STATINC(IP_STAT_CANTFORWARD);
    732 			return;
    733 		}
    734 #ifdef FAST_IPSEC
    735 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
    736 		s = splsoftnet();
    737 		if (mtag != NULL) {
    738 			tdbi = (struct tdb_ident *)(mtag + 1);
    739 			sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
    740 		} else {
    741 			sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
    742 						   IP_FORWARDING, &error);
    743 		}
    744 		if (sp == NULL) {	/* NB: can happen if error */
    745 			splx(s);
    746 			/*XXX error stat???*/
    747 			DPRINTF(("ip_input: no SP for forwarding\n"));	/*XXX*/
    748 			goto bad;
    749 		}
    750 
    751 		/*
    752 		 * Check security policy against packet attributes.
    753 		 */
    754 		error = ipsec_in_reject(sp, m);
    755 		KEY_FREESP(&sp);
    756 		splx(s);
    757 		if (error) {
    758 			IP_STATINC(IP_STAT_CANTFORWARD);
    759 			goto bad;
    760 		}
    761 
    762 		/*
    763 		 * Peek at the outbound SP for this packet to determine if
    764 		 * it's a Fast Forward candidate.
    765 		 */
    766 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
    767 		if (mtag != NULL)
    768 			m->m_flags &= ~M_CANFASTFWD;
    769 		else {
    770 			s = splsoftnet();
    771 			sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND,
    772 			    (IP_FORWARDING |
    773 			     (ip_directedbcast ? IP_ALLOWBROADCAST : 0)),
    774 			    &error, NULL);
    775 			if (sp != NULL) {
    776 				m->m_flags &= ~M_CANFASTFWD;
    777 				KEY_FREESP(&sp);
    778 			}
    779 			splx(s);
    780 		}
    781 #endif	/* FAST_IPSEC */
    782 
    783 		ip_forward(m, srcrt);
    784 	}
    785 	return;
    786 
    787 ours:
    788 	/*
    789 	 * If offset or IP_MF are set, must reassemble.
    790 	 */
    791 	if (ip->ip_off & ~htons(IP_DF|IP_RF)) {
    792 		/*
    793 		 * Pass to IP reassembly mechanism.
    794 		 */
    795 		if (ip_reass_packet(&m, ip) != 0) {
    796 			/* Failed; invalid fragment(s) or packet. */
    797 			goto bad;
    798 		}
    799 		if (m == NULL) {
    800 			/* More fragments should come; silently return. */
    801 			return;
    802 		}
    803 		/*
    804 		 * Reassembly is done, we have the final packet.
    805 		 * Updated cached data in local variable(s).
    806 		 */
    807 		ip = mtod(m, struct ip *);
    808 		hlen = ip->ip_hl << 2;
    809 	}
    810 
    811 #ifdef FAST_IPSEC
    812 	/*
    813 	 * enforce IPsec policy checking if we are seeing last header.
    814 	 * note that we do not visit this with protocols with pcb layer
    815 	 * code - like udp/tcp/raw ip.
    816 	 */
    817 	if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
    818 		/*
    819 		 * Check if the packet has already had IPsec processing
    820 		 * done.  If so, then just pass it along.  This tag gets
    821 		 * set during AH, ESP, etc. input handling, before the
    822 		 * packet is returned to the ip input queue for delivery.
    823 		 */
    824 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
    825 		s = splsoftnet();
    826 		if (mtag != NULL) {
    827 			tdbi = (struct tdb_ident *)(mtag + 1);
    828 			sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
    829 		} else {
    830 			sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
    831 						   IP_FORWARDING, &error);
    832 		}
    833 		if (sp != NULL) {
    834 			/*
    835 			 * Check security policy against packet attributes.
    836 			 */
    837 			error = ipsec_in_reject(sp, m);
    838 			KEY_FREESP(&sp);
    839 		} else {
    840 			/* XXX error stat??? */
    841 			error = EINVAL;
    842 DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
    843 		}
    844 		splx(s);
    845 		if (error)
    846 			goto bad;
    847 	}
    848 #endif /* FAST_IPSEC */
    849 
    850 	/*
    851 	 * Switch out to protocol's input routine.
    852 	 */
    853 #if IFA_STATS
    854 	if (ia && ip)
    855 		ia->ia_ifa.ifa_data.ifad_inbytes += ntohs(ip->ip_len);
    856 #endif
    857 	IP_STATINC(IP_STAT_DELIVERED);
    858     {
    859 	int off = hlen, nh = ip->ip_p;
    860 
    861 	(*inetsw[ip_protox[nh]].pr_input)(m, off, nh);
    862 	return;
    863     }
    864 bad:
    865 	m_freem(m);
    866 	return;
    867 
    868 badcsum:
    869 	IP_STATINC(IP_STAT_BADSUM);
    870 	m_freem(m);
    871 }
    872 
    873 /*
    874  * IP timer processing.
    875  */
    876 void
    877 ip_slowtimo(void)
    878 {
    879 
    880 	mutex_enter(softnet_lock);
    881 	KERNEL_LOCK(1, NULL);
    882 
    883 	ip_reass_slowtimo();
    884 
    885 	KERNEL_UNLOCK_ONE(NULL);
    886 	mutex_exit(softnet_lock);
    887 }
    888 
    889 /*
    890  * IP drain processing.
    891  */
    892 void
    893 ip_drain(void)
    894 {
    895 
    896 	KERNEL_LOCK(1, NULL);
    897 	ip_reass_drain();
    898 	KERNEL_UNLOCK_ONE(NULL);
    899 }
    900 
    901 /*
    902  * Do option processing on a datagram,
    903  * possibly discarding it if bad options are encountered,
    904  * or forwarding it if source-routed.
    905  * Returns 1 if packet has been forwarded/freed,
    906  * 0 if the packet should be processed further.
    907  */
    908 int
    909 ip_dooptions(struct mbuf *m)
    910 {
    911 	struct ip *ip = mtod(m, struct ip *);
    912 	u_char *cp, *cp0;
    913 	struct ip_timestamp *ipt;
    914 	struct in_ifaddr *ia;
    915 	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
    916 	struct in_addr dst;
    917 	n_time ntime;
    918 
    919 	dst = ip->ip_dst;
    920 	cp = (u_char *)(ip + 1);
    921 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
    922 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
    923 		opt = cp[IPOPT_OPTVAL];
    924 		if (opt == IPOPT_EOL)
    925 			break;
    926 		if (opt == IPOPT_NOP)
    927 			optlen = 1;
    928 		else {
    929 			if (cnt < IPOPT_OLEN + sizeof(*cp)) {
    930 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
    931 				goto bad;
    932 			}
    933 			optlen = cp[IPOPT_OLEN];
    934 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
    935 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
    936 				goto bad;
    937 			}
    938 		}
    939 		switch (opt) {
    940 
    941 		default:
    942 			break;
    943 
    944 		/*
    945 		 * Source routing with record.
    946 		 * Find interface with current destination address.
    947 		 * If none on this machine then drop if strictly routed,
    948 		 * or do nothing if loosely routed.
    949 		 * Record interface address and bring up next address
    950 		 * component.  If strictly routed make sure next
    951 		 * address is on directly accessible net.
    952 		 */
    953 		case IPOPT_LSRR:
    954 		case IPOPT_SSRR:
    955 			if (ip_allowsrcrt == 0) {
    956 				type = ICMP_UNREACH;
    957 				code = ICMP_UNREACH_NET_PROHIB;
    958 				goto bad;
    959 			}
    960 			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
    961 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
    962 				goto bad;
    963 			}
    964 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
    965 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
    966 				goto bad;
    967 			}
    968 			ipaddr.sin_addr = ip->ip_dst;
    969 			ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
    970 			if (ia == 0) {
    971 				if (opt == IPOPT_SSRR) {
    972 					type = ICMP_UNREACH;
    973 					code = ICMP_UNREACH_SRCFAIL;
    974 					goto bad;
    975 				}
    976 				/*
    977 				 * Loose routing, and not at next destination
    978 				 * yet; nothing to do except forward.
    979 				 */
    980 				break;
    981 			}
    982 			off--;			/* 0 origin */
    983 			if ((off + sizeof(struct in_addr)) > optlen) {
    984 				/*
    985 				 * End of source route.  Should be for us.
    986 				 */
    987 				save_rte(cp, ip->ip_src);
    988 				break;
    989 			}
    990 			/*
    991 			 * locate outgoing interface
    992 			 */
    993 			memcpy((void *)&ipaddr.sin_addr, (void *)(cp + off),
    994 			    sizeof(ipaddr.sin_addr));
    995 			if (opt == IPOPT_SSRR)
    996 				ia = ifatoia(ifa_ifwithladdr(sintosa(&ipaddr)));
    997 			else
    998 				ia = ip_rtaddr(ipaddr.sin_addr);
    999 			if (ia == 0) {
   1000 				type = ICMP_UNREACH;
   1001 				code = ICMP_UNREACH_SRCFAIL;
   1002 				goto bad;
   1003 			}
   1004 			ip->ip_dst = ipaddr.sin_addr;
   1005 			bcopy((void *)&ia->ia_addr.sin_addr,
   1006 			    (void *)(cp + off), sizeof(struct in_addr));
   1007 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
   1008 			/*
   1009 			 * Let ip_intr's mcast routing check handle mcast pkts
   1010 			 */
   1011 			forward = !IN_MULTICAST(ip->ip_dst.s_addr);
   1012 			break;
   1013 
   1014 		case IPOPT_RR:
   1015 			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
   1016 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
   1017 				goto bad;
   1018 			}
   1019 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
   1020 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
   1021 				goto bad;
   1022 			}
   1023 			/*
   1024 			 * If no space remains, ignore.
   1025 			 */
   1026 			off--;			/* 0 origin */
   1027 			if ((off + sizeof(struct in_addr)) > optlen)
   1028 				break;
   1029 			memcpy((void *)&ipaddr.sin_addr, (void *)(&ip->ip_dst),
   1030 			    sizeof(ipaddr.sin_addr));
   1031 			/*
   1032 			 * locate outgoing interface; if we're the destination,
   1033 			 * use the incoming interface (should be same).
   1034 			 */
   1035 			if ((ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))))
   1036 			    == NULL &&
   1037 			    (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) {
   1038 				type = ICMP_UNREACH;
   1039 				code = ICMP_UNREACH_HOST;
   1040 				goto bad;
   1041 			}
   1042 			bcopy((void *)&ia->ia_addr.sin_addr,
   1043 			    (void *)(cp + off), sizeof(struct in_addr));
   1044 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
   1045 			break;
   1046 
   1047 		case IPOPT_TS:
   1048 			code = cp - (u_char *)ip;
   1049 			ipt = (struct ip_timestamp *)cp;
   1050 			if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
   1051 				code = (u_char *)&ipt->ipt_len - (u_char *)ip;
   1052 				goto bad;
   1053 			}
   1054 			if (ipt->ipt_ptr < 5) {
   1055 				code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
   1056 				goto bad;
   1057 			}
   1058 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
   1059 				if (++ipt->ipt_oflw == 0) {
   1060 					code = (u_char *)&ipt->ipt_ptr -
   1061 					    (u_char *)ip;
   1062 					goto bad;
   1063 				}
   1064 				break;
   1065 			}
   1066 			cp0 = (cp + ipt->ipt_ptr - 1);
   1067 			switch (ipt->ipt_flg) {
   1068 
   1069 			case IPOPT_TS_TSONLY:
   1070 				break;
   1071 
   1072 			case IPOPT_TS_TSANDADDR:
   1073 				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
   1074 				    sizeof(struct in_addr) > ipt->ipt_len) {
   1075 					code = (u_char *)&ipt->ipt_ptr -
   1076 					    (u_char *)ip;
   1077 					goto bad;
   1078 				}
   1079 				ipaddr.sin_addr = dst;
   1080 				ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr),
   1081 				    m->m_pkthdr.rcvif));
   1082 				if (ia == 0)
   1083 					continue;
   1084 				bcopy(&ia->ia_addr.sin_addr,
   1085 				    cp0, sizeof(struct in_addr));
   1086 				ipt->ipt_ptr += sizeof(struct in_addr);
   1087 				break;
   1088 
   1089 			case IPOPT_TS_PRESPEC:
   1090 				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
   1091 				    sizeof(struct in_addr) > ipt->ipt_len) {
   1092 					code = (u_char *)&ipt->ipt_ptr -
   1093 					    (u_char *)ip;
   1094 					goto bad;
   1095 				}
   1096 				memcpy(&ipaddr.sin_addr, cp0,
   1097 				    sizeof(struct in_addr));
   1098 				if (ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)))
   1099 				    == NULL)
   1100 					continue;
   1101 				ipt->ipt_ptr += sizeof(struct in_addr);
   1102 				break;
   1103 
   1104 			default:
   1105 				/* XXX can't take &ipt->ipt_flg */
   1106 				code = (u_char *)&ipt->ipt_ptr -
   1107 				    (u_char *)ip + 1;
   1108 				goto bad;
   1109 			}
   1110 			ntime = iptime();
   1111 			cp0 = (u_char *) &ntime; /* XXX grumble, GCC... */
   1112 			memmove((char *)cp + ipt->ipt_ptr - 1, cp0,
   1113 			    sizeof(n_time));
   1114 			ipt->ipt_ptr += sizeof(n_time);
   1115 		}
   1116 	}
   1117 	if (forward) {
   1118 		if (ip_forwsrcrt == 0) {
   1119 			type = ICMP_UNREACH;
   1120 			code = ICMP_UNREACH_SRCFAIL;
   1121 			goto bad;
   1122 		}
   1123 		ip_forward(m, 1);
   1124 		return (1);
   1125 	}
   1126 	return (0);
   1127 bad:
   1128 	icmp_error(m, type, code, 0, 0);
   1129 	IP_STATINC(IP_STAT_BADOPTIONS);
   1130 	return (1);
   1131 }
   1132 
   1133 /*
   1134  * Given address of next destination (final or next hop),
   1135  * return internet address info of interface to be used to get there.
   1136  */
   1137 struct in_ifaddr *
   1138 ip_rtaddr(struct in_addr dst)
   1139 {
   1140 	struct rtentry *rt;
   1141 	union {
   1142 		struct sockaddr		dst;
   1143 		struct sockaddr_in	dst4;
   1144 	} u;
   1145 
   1146 	sockaddr_in_init(&u.dst4, &dst, 0);
   1147 
   1148 	if ((rt = rtcache_lookup(&ipforward_rt, &u.dst)) == NULL)
   1149 		return NULL;
   1150 
   1151 	return ifatoia(rt->rt_ifa);
   1152 }
   1153 
   1154 /*
   1155  * Save incoming source route for use in replies,
   1156  * to be picked up later by ip_srcroute if the receiver is interested.
   1157  */
   1158 void
   1159 save_rte(u_char *option, struct in_addr dst)
   1160 {
   1161 	unsigned olen;
   1162 
   1163 	olen = option[IPOPT_OLEN];
   1164 #ifdef DIAGNOSTIC
   1165 	if (ipprintfs)
   1166 		printf("save_rte: olen %d\n", olen);
   1167 #endif /* 0 */
   1168 	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
   1169 		return;
   1170 	memcpy((void *)ip_srcrt.srcopt, (void *)option, olen);
   1171 	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
   1172 	ip_srcrt.dst = dst;
   1173 }
   1174 
   1175 /*
   1176  * Retrieve incoming source route for use in replies,
   1177  * in the same form used by setsockopt.
   1178  * The first hop is placed before the options, will be removed later.
   1179  */
   1180 struct mbuf *
   1181 ip_srcroute(void)
   1182 {
   1183 	struct in_addr *p, *q;
   1184 	struct mbuf *m;
   1185 
   1186 	if (ip_nhops == 0)
   1187 		return NULL;
   1188 	m = m_get(M_DONTWAIT, MT_SOOPTS);
   1189 	if (m == 0)
   1190 		return NULL;
   1191 
   1192 	MCLAIM(m, &inetdomain.dom_mowner);
   1193 #define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
   1194 
   1195 	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
   1196 	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
   1197 	    OPTSIZ;
   1198 #ifdef DIAGNOSTIC
   1199 	if (ipprintfs)
   1200 		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
   1201 #endif
   1202 
   1203 	/*
   1204 	 * First save first hop for return route
   1205 	 */
   1206 	p = &ip_srcrt.route[ip_nhops - 1];
   1207 	*(mtod(m, struct in_addr *)) = *p--;
   1208 #ifdef DIAGNOSTIC
   1209 	if (ipprintfs)
   1210 		printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
   1211 #endif
   1212 
   1213 	/*
   1214 	 * Copy option fields and padding (nop) to mbuf.
   1215 	 */
   1216 	ip_srcrt.nop = IPOPT_NOP;
   1217 	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
   1218 	memmove(mtod(m, char *) + sizeof(struct in_addr), &ip_srcrt.nop,
   1219 	    OPTSIZ);
   1220 	q = (struct in_addr *)(mtod(m, char *) +
   1221 	    sizeof(struct in_addr) + OPTSIZ);
   1222 #undef OPTSIZ
   1223 	/*
   1224 	 * Record return path as an IP source route,
   1225 	 * reversing the path (pointers are now aligned).
   1226 	 */
   1227 	while (p >= ip_srcrt.route) {
   1228 #ifdef DIAGNOSTIC
   1229 		if (ipprintfs)
   1230 			printf(" %x", ntohl(q->s_addr));
   1231 #endif
   1232 		*q++ = *p--;
   1233 	}
   1234 	/*
   1235 	 * Last hop goes to final destination.
   1236 	 */
   1237 	*q = ip_srcrt.dst;
   1238 #ifdef DIAGNOSTIC
   1239 	if (ipprintfs)
   1240 		printf(" %x\n", ntohl(q->s_addr));
   1241 #endif
   1242 	return (m);
   1243 }
   1244 
   1245 const int inetctlerrmap[PRC_NCMDS] = {
   1246 	[PRC_MSGSIZE] = EMSGSIZE,
   1247 	[PRC_HOSTDEAD] = EHOSTDOWN,
   1248 	[PRC_HOSTUNREACH] = EHOSTUNREACH,
   1249 	[PRC_UNREACH_NET] = EHOSTUNREACH,
   1250 	[PRC_UNREACH_HOST] = EHOSTUNREACH,
   1251 	[PRC_UNREACH_PROTOCOL] = ECONNREFUSED,
   1252 	[PRC_UNREACH_PORT] = ECONNREFUSED,
   1253 	[PRC_UNREACH_SRCFAIL] = EHOSTUNREACH,
   1254 	[PRC_PARAMPROB] = ENOPROTOOPT,
   1255 };
   1256 
   1257 void
   1258 ip_fasttimo(void)
   1259 {
   1260 	if (ip_drainwanted) {
   1261 		ip_drain();
   1262 		ip_drainwanted = 0;
   1263 	}
   1264 }
   1265 
   1266 void
   1267 ip_drainstub(void)
   1268 {
   1269 	ip_drainwanted = 1;
   1270 }
   1271 
   1272 /*
   1273  * Forward a packet.  If some error occurs return the sender
   1274  * an icmp packet.  Note we can't always generate a meaningful
   1275  * icmp message because icmp doesn't have a large enough repertoire
   1276  * of codes and types.
   1277  *
   1278  * If not forwarding, just drop the packet.  This could be confusing
   1279  * if ipforwarding was zero but some routing protocol was advancing
   1280  * us as a gateway to somewhere.  However, we must let the routing
   1281  * protocol deal with that.
   1282  *
   1283  * The srcrt parameter indicates whether the packet is being forwarded
   1284  * via a source route.
   1285  */
   1286 void
   1287 ip_forward(struct mbuf *m, int srcrt)
   1288 {
   1289 	struct ip *ip = mtod(m, struct ip *);
   1290 	struct rtentry *rt;
   1291 	int error, type = 0, code = 0, destmtu = 0;
   1292 	struct mbuf *mcopy;
   1293 	n_long dest;
   1294 	union {
   1295 		struct sockaddr		dst;
   1296 		struct sockaddr_in	dst4;
   1297 	} u;
   1298 
   1299 	/*
   1300 	 * We are now in the output path.
   1301 	 */
   1302 	MCLAIM(m, &ip_tx_mowner);
   1303 
   1304 	/*
   1305 	 * Clear any in-bound checksum flags for this packet.
   1306 	 */
   1307 	m->m_pkthdr.csum_flags = 0;
   1308 
   1309 	dest = 0;
   1310 #ifdef DIAGNOSTIC
   1311 	if (ipprintfs) {
   1312 		printf("forward: src %s ", inet_ntoa(ip->ip_src));
   1313 		printf("dst %s ttl %x\n", inet_ntoa(ip->ip_dst), ip->ip_ttl);
   1314 	}
   1315 #endif
   1316 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
   1317 		IP_STATINC(IP_STAT_CANTFORWARD);
   1318 		m_freem(m);
   1319 		return;
   1320 	}
   1321 	if (ip->ip_ttl <= IPTTLDEC) {
   1322 		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
   1323 		return;
   1324 	}
   1325 
   1326 	sockaddr_in_init(&u.dst4, &ip->ip_dst, 0);
   1327 	if ((rt = rtcache_lookup(&ipforward_rt, &u.dst)) == NULL) {
   1328 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, dest, 0);
   1329 		return;
   1330 	}
   1331 
   1332 	/*
   1333 	 * Save at most 68 bytes of the packet in case
   1334 	 * we need to generate an ICMP message to the src.
   1335 	 * Pullup to avoid sharing mbuf cluster between m and mcopy.
   1336 	 */
   1337 	mcopy = m_copym(m, 0, imin(ntohs(ip->ip_len), 68), M_DONTWAIT);
   1338 	if (mcopy)
   1339 		mcopy = m_pullup(mcopy, ip->ip_hl << 2);
   1340 
   1341 	ip->ip_ttl -= IPTTLDEC;
   1342 
   1343 	/*
   1344 	 * If forwarding packet using same interface that it came in on,
   1345 	 * perhaps should send a redirect to sender to shortcut a hop.
   1346 	 * Only send redirect if source is sending directly to us,
   1347 	 * and if packet was not source routed (or has any options).
   1348 	 * Also, don't send redirect if forwarding using a default route
   1349 	 * or a route modified by a redirect.
   1350 	 */
   1351 	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
   1352 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
   1353 	    !in_nullhost(satocsin(rt_getkey(rt))->sin_addr) &&
   1354 	    ipsendredirects && !srcrt) {
   1355 		if (rt->rt_ifa &&
   1356 		    (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
   1357 		    ifatoia(rt->rt_ifa)->ia_subnet) {
   1358 			if (rt->rt_flags & RTF_GATEWAY)
   1359 				dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
   1360 			else
   1361 				dest = ip->ip_dst.s_addr;
   1362 			/*
   1363 			 * Router requirements says to only send host
   1364 			 * redirects.
   1365 			 */
   1366 			type = ICMP_REDIRECT;
   1367 			code = ICMP_REDIRECT_HOST;
   1368 #ifdef DIAGNOSTIC
   1369 			if (ipprintfs)
   1370 				printf("redirect (%d) to %x\n", code,
   1371 				    (u_int32_t)dest);
   1372 #endif
   1373 		}
   1374 	}
   1375 
   1376 	error = ip_output(m, NULL, &ipforward_rt,
   1377 	    (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)),
   1378 	    NULL, NULL);
   1379 
   1380 	if (error)
   1381 		IP_STATINC(IP_STAT_CANTFORWARD);
   1382 	else {
   1383 		uint64_t *ips = IP_STAT_GETREF();
   1384 		ips[IP_STAT_FORWARD]++;
   1385 		if (type) {
   1386 			ips[IP_STAT_REDIRECTSENT]++;
   1387 			IP_STAT_PUTREF();
   1388 		} else {
   1389 			IP_STAT_PUTREF();
   1390 			if (mcopy) {
   1391 #ifdef GATEWAY
   1392 				if (mcopy->m_flags & M_CANFASTFWD)
   1393 					ipflow_create(&ipforward_rt, mcopy);
   1394 #endif
   1395 				m_freem(mcopy);
   1396 			}
   1397 			return;
   1398 		}
   1399 	}
   1400 	if (mcopy == NULL)
   1401 		return;
   1402 
   1403 	switch (error) {
   1404 
   1405 	case 0:				/* forwarded, but need redirect */
   1406 		/* type, code set above */
   1407 		break;
   1408 
   1409 	case ENETUNREACH:		/* shouldn't happen, checked above */
   1410 	case EHOSTUNREACH:
   1411 	case ENETDOWN:
   1412 	case EHOSTDOWN:
   1413 	default:
   1414 		type = ICMP_UNREACH;
   1415 		code = ICMP_UNREACH_HOST;
   1416 		break;
   1417 
   1418 	case EMSGSIZE:
   1419 		type = ICMP_UNREACH;
   1420 		code = ICMP_UNREACH_NEEDFRAG;
   1421 
   1422 		if ((rt = rtcache_validate(&ipforward_rt)) != NULL)
   1423 			destmtu = rt->rt_ifp->if_mtu;
   1424 
   1425 #if defined(FAST_IPSEC)
   1426 		{
   1427 			/*
   1428 			 * If the packet is routed over IPsec tunnel, tell the
   1429 			 * originator the tunnel MTU.
   1430 			 *	tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
   1431 			 * XXX quickhack!!!
   1432 			 */
   1433 
   1434 			struct secpolicy *sp;
   1435 			int ipsecerror;
   1436 			size_t ipsechdr;
   1437 			struct route *ro;
   1438 
   1439 			sp = ipsec4_getpolicybyaddr(mcopy,
   1440 			    IPSEC_DIR_OUTBOUND, IP_FORWARDING,
   1441 			    &ipsecerror);
   1442 
   1443 			if (sp != NULL) {
   1444 				/* count IPsec header size */
   1445 				ipsechdr = ipsec4_hdrsiz(mcopy,
   1446 				    IPSEC_DIR_OUTBOUND, NULL);
   1447 
   1448 				/*
   1449 				 * find the correct route for outer IPv4
   1450 				 * header, compute tunnel MTU.
   1451 				 */
   1452 
   1453 				if (sp->req != NULL
   1454 				 && sp->req->sav != NULL
   1455 				 && sp->req->sav->sah != NULL) {
   1456 					ro = &sp->req->sav->sah->sa_route;
   1457 					rt = rtcache_validate(ro);
   1458 					if (rt && rt->rt_ifp) {
   1459 						destmtu =
   1460 						    rt->rt_rmx.rmx_mtu ?
   1461 						    rt->rt_rmx.rmx_mtu :
   1462 						    rt->rt_ifp->if_mtu;
   1463 						destmtu -= ipsechdr;
   1464 					}
   1465 				}
   1466 
   1467 				KEY_FREESP(&sp);
   1468 			}
   1469 		}
   1470 #endif /*defined(FAST_IPSEC)*/
   1471 		IP_STATINC(IP_STAT_CANTFRAG);
   1472 		break;
   1473 
   1474 	case ENOBUFS:
   1475 #if 1
   1476 		/*
   1477 		 * a router should not generate ICMP_SOURCEQUENCH as
   1478 		 * required in RFC1812 Requirements for IP Version 4 Routers.
   1479 		 * source quench could be a big problem under DoS attacks,
   1480 		 * or if the underlying interface is rate-limited.
   1481 		 */
   1482 		if (mcopy)
   1483 			m_freem(mcopy);
   1484 		return;
   1485 #else
   1486 		type = ICMP_SOURCEQUENCH;
   1487 		code = 0;
   1488 		break;
   1489 #endif
   1490 	}
   1491 	icmp_error(mcopy, type, code, dest, destmtu);
   1492 }
   1493 
   1494 void
   1495 ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
   1496     struct mbuf *m)
   1497 {
   1498 
   1499 	if (inp->inp_socket->so_options & SO_TIMESTAMP
   1500 #ifdef SO_OTIMESTAMP
   1501 	    || inp->inp_socket->so_options & SO_OTIMESTAMP
   1502 #endif
   1503 	    ) {
   1504 		struct timeval tv;
   1505 
   1506 		microtime(&tv);
   1507 #ifdef SO_OTIMESTAMP
   1508 		if (inp->inp_socket->so_options & SO_OTIMESTAMP) {
   1509 			struct timeval50 tv50;
   1510 			timeval_to_timeval50(&tv, &tv50);
   1511 			*mp = sbcreatecontrol((void *) &tv50, sizeof(tv50),
   1512 			    SCM_OTIMESTAMP, SOL_SOCKET);
   1513 		} else
   1514 #endif
   1515 		*mp = sbcreatecontrol((void *) &tv, sizeof(tv),
   1516 		    SCM_TIMESTAMP, SOL_SOCKET);
   1517 		if (*mp)
   1518 			mp = &(*mp)->m_next;
   1519 	}
   1520 	if (inp->inp_flags & INP_RECVDSTADDR) {
   1521 		*mp = sbcreatecontrol((void *) &ip->ip_dst,
   1522 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
   1523 		if (*mp)
   1524 			mp = &(*mp)->m_next;
   1525 	}
   1526 #ifdef notyet
   1527 	/*
   1528 	 * XXX
   1529 	 * Moving these out of udp_input() made them even more broken
   1530 	 * than they already were.
   1531 	 *	- fenner (at) parc.xerox.com
   1532 	 */
   1533 	/* options were tossed already */
   1534 	if (inp->inp_flags & INP_RECVOPTS) {
   1535 		*mp = sbcreatecontrol((void *) opts_deleted_above,
   1536 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
   1537 		if (*mp)
   1538 			mp = &(*mp)->m_next;
   1539 	}
   1540 	/* ip_srcroute doesn't do what we want here, need to fix */
   1541 	if (inp->inp_flags & INP_RECVRETOPTS) {
   1542 		*mp = sbcreatecontrol((void *) ip_srcroute(),
   1543 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
   1544 		if (*mp)
   1545 			mp = &(*mp)->m_next;
   1546 	}
   1547 #endif
   1548 	if (inp->inp_flags & INP_RECVIF) {
   1549 		struct sockaddr_dl sdl;
   1550 
   1551 		sockaddr_dl_init(&sdl, sizeof(sdl),
   1552 		    (m->m_pkthdr.rcvif != NULL)
   1553 		        ?  m->m_pkthdr.rcvif->if_index
   1554 			: 0,
   1555 			0, NULL, 0, NULL, 0);
   1556 		*mp = sbcreatecontrol(&sdl, sdl.sdl_len, IP_RECVIF, IPPROTO_IP);
   1557 		if (*mp)
   1558 			mp = &(*mp)->m_next;
   1559 	}
   1560 	if (inp->inp_flags & INP_RECVTTL) {
   1561 		*mp = sbcreatecontrol((void *) &ip->ip_ttl,
   1562 		    sizeof(uint8_t), IP_RECVTTL, IPPROTO_IP);
   1563 		if (*mp)
   1564 			mp = &(*mp)->m_next;
   1565 	}
   1566 }
   1567 
   1568 /*
   1569  * sysctl helper routine for net.inet.ip.forwsrcrt.
   1570  */
   1571 static int
   1572 sysctl_net_inet_ip_forwsrcrt(SYSCTLFN_ARGS)
   1573 {
   1574 	int error, tmp;
   1575 	struct sysctlnode node;
   1576 
   1577 	node = *rnode;
   1578 	tmp = ip_forwsrcrt;
   1579 	node.sysctl_data = &tmp;
   1580 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1581 	if (error || newp == NULL)
   1582 		return (error);
   1583 
   1584 	error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_FORWSRCRT,
   1585 	    0, NULL, NULL, NULL);
   1586 	if (error)
   1587 		return (error);
   1588 
   1589 	ip_forwsrcrt = tmp;
   1590 
   1591 	return (0);
   1592 }
   1593 
   1594 /*
   1595  * sysctl helper routine for net.inet.ip.mtudisctimeout.  checks the
   1596  * range of the new value and tweaks timers if it changes.
   1597  */
   1598 static int
   1599 sysctl_net_inet_ip_pmtudto(SYSCTLFN_ARGS)
   1600 {
   1601 	int error, tmp;
   1602 	struct sysctlnode node;
   1603 
   1604 	node = *rnode;
   1605 	tmp = ip_mtudisc_timeout;
   1606 	node.sysctl_data = &tmp;
   1607 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1608 	if (error || newp == NULL)
   1609 		return (error);
   1610 	if (tmp < 0)
   1611 		return (EINVAL);
   1612 
   1613 	mutex_enter(softnet_lock);
   1614 
   1615 	ip_mtudisc_timeout = tmp;
   1616 	rt_timer_queue_change(ip_mtudisc_timeout_q, ip_mtudisc_timeout);
   1617 
   1618 	mutex_exit(softnet_lock);
   1619 
   1620 	return (0);
   1621 }
   1622 
   1623 #ifdef GATEWAY
   1624 /*
   1625  * sysctl helper routine for net.inet.ip.maxflows.
   1626  */
   1627 static int
   1628 sysctl_net_inet_ip_maxflows(SYSCTLFN_ARGS)
   1629 {
   1630 	int error;
   1631 
   1632 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
   1633 	if (error || newp == NULL)
   1634 		return (error);
   1635 
   1636 	mutex_enter(softnet_lock);
   1637 	KERNEL_LOCK(1, NULL);
   1638 
   1639 	ipflow_prune();
   1640 
   1641 	KERNEL_UNLOCK_ONE(NULL);
   1642 	mutex_exit(softnet_lock);
   1643 
   1644 	return (0);
   1645 }
   1646 
   1647 static int
   1648 sysctl_net_inet_ip_hashsize(SYSCTLFN_ARGS)
   1649 {
   1650 	int error, tmp;
   1651 	struct sysctlnode node;
   1652 
   1653 	node = *rnode;
   1654 	tmp = ip_hashsize;
   1655 	node.sysctl_data = &tmp;
   1656 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1657 	if (error || newp == NULL)
   1658 		return (error);
   1659 
   1660 	if ((tmp & (tmp - 1)) == 0 && tmp != 0) {
   1661 		/*
   1662 		 * Can only fail due to malloc()
   1663 		 */
   1664 		mutex_enter(softnet_lock);
   1665 		KERNEL_LOCK(1, NULL);
   1666 
   1667 		error = ipflow_invalidate_all(tmp);
   1668 
   1669 		KERNEL_UNLOCK_ONE(NULL);
   1670 		mutex_exit(softnet_lock);
   1671 
   1672 	} else {
   1673 		/*
   1674 		 * EINVAL if not a power of 2
   1675 	         */
   1676 		error = EINVAL;
   1677 	}
   1678 
   1679 	return error;
   1680 }
   1681 #endif /* GATEWAY */
   1682 
   1683 static int
   1684 sysctl_net_inet_ip_stats(SYSCTLFN_ARGS)
   1685 {
   1686 
   1687 	return (NETSTAT_SYSCTL(ipstat_percpu, IP_NSTATS));
   1688 }
   1689 
   1690 static void
   1691 sysctl_net_inet_ip_setup(struct sysctllog **clog)
   1692 {
   1693 	extern int subnetsarelocal, hostzeroisbroadcast;
   1694 
   1695 	sysctl_createv(clog, 0, NULL, NULL,
   1696 		       CTLFLAG_PERMANENT,
   1697 		       CTLTYPE_NODE, "net", NULL,
   1698 		       NULL, 0, NULL, 0,
   1699 		       CTL_NET, CTL_EOL);
   1700 	sysctl_createv(clog, 0, NULL, NULL,
   1701 		       CTLFLAG_PERMANENT,
   1702 		       CTLTYPE_NODE, "inet",
   1703 		       SYSCTL_DESCR("PF_INET related settings"),
   1704 		       NULL, 0, NULL, 0,
   1705 		       CTL_NET, PF_INET, CTL_EOL);
   1706 	sysctl_createv(clog, 0, NULL, NULL,
   1707 		       CTLFLAG_PERMANENT,
   1708 		       CTLTYPE_NODE, "ip",
   1709 		       SYSCTL_DESCR("IPv4 related settings"),
   1710 		       NULL, 0, NULL, 0,
   1711 		       CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
   1712 
   1713 	sysctl_createv(clog, 0, NULL, NULL,
   1714 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1715 		       CTLTYPE_INT, "forwarding",
   1716 		       SYSCTL_DESCR("Enable forwarding of INET datagrams"),
   1717 		       NULL, 0, &ipforwarding, 0,
   1718 		       CTL_NET, PF_INET, IPPROTO_IP,
   1719 		       IPCTL_FORWARDING, CTL_EOL);
   1720 	sysctl_createv(clog, 0, NULL, NULL,
   1721 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1722 		       CTLTYPE_INT, "redirect",
   1723 		       SYSCTL_DESCR("Enable sending of ICMP redirect messages"),
   1724 		       NULL, 0, &ipsendredirects, 0,
   1725 		       CTL_NET, PF_INET, IPPROTO_IP,
   1726 		       IPCTL_SENDREDIRECTS, CTL_EOL);
   1727 	sysctl_createv(clog, 0, NULL, NULL,
   1728 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1729 		       CTLTYPE_INT, "ttl",
   1730 		       SYSCTL_DESCR("Default TTL for an INET datagram"),
   1731 		       NULL, 0, &ip_defttl, 0,
   1732 		       CTL_NET, PF_INET, IPPROTO_IP,
   1733 		       IPCTL_DEFTTL, CTL_EOL);
   1734 #ifdef IPCTL_DEFMTU
   1735 	sysctl_createv(clog, 0, NULL, NULL,
   1736 		       CTLFLAG_PERMANENT /* |CTLFLAG_READWRITE? */,
   1737 		       CTLTYPE_INT, "mtu",
   1738 		       SYSCTL_DESCR("Default MTA for an INET route"),
   1739 		       NULL, 0, &ip_mtu, 0,
   1740 		       CTL_NET, PF_INET, IPPROTO_IP,
   1741 		       IPCTL_DEFMTU, CTL_EOL);
   1742 #endif /* IPCTL_DEFMTU */
   1743 	sysctl_createv(clog, 0, NULL, NULL,
   1744 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1745 		       CTLTYPE_INT, "forwsrcrt",
   1746 		       SYSCTL_DESCR("Enable forwarding of source-routed "
   1747 				    "datagrams"),
   1748 		       sysctl_net_inet_ip_forwsrcrt, 0, &ip_forwsrcrt, 0,
   1749 		       CTL_NET, PF_INET, IPPROTO_IP,
   1750 		       IPCTL_FORWSRCRT, CTL_EOL);
   1751 	sysctl_createv(clog, 0, NULL, NULL,
   1752 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1753 		       CTLTYPE_INT, "directed-broadcast",
   1754 		       SYSCTL_DESCR("Enable forwarding of broadcast datagrams"),
   1755 		       NULL, 0, &ip_directedbcast, 0,
   1756 		       CTL_NET, PF_INET, IPPROTO_IP,
   1757 		       IPCTL_DIRECTEDBCAST, CTL_EOL);
   1758 	sysctl_createv(clog, 0, NULL, NULL,
   1759 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1760 		       CTLTYPE_INT, "allowsrcrt",
   1761 		       SYSCTL_DESCR("Accept source-routed datagrams"),
   1762 		       NULL, 0, &ip_allowsrcrt, 0,
   1763 		       CTL_NET, PF_INET, IPPROTO_IP,
   1764 		       IPCTL_ALLOWSRCRT, CTL_EOL);
   1765 	sysctl_createv(clog, 0, NULL, NULL,
   1766 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1767 		       CTLTYPE_INT, "subnetsarelocal",
   1768 		       SYSCTL_DESCR("Whether logical subnets are considered "
   1769 				    "local"),
   1770 		       NULL, 0, &subnetsarelocal, 0,
   1771 		       CTL_NET, PF_INET, IPPROTO_IP,
   1772 		       IPCTL_SUBNETSARELOCAL, CTL_EOL);
   1773 	sysctl_createv(clog, 0, NULL, NULL,
   1774 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1775 		       CTLTYPE_INT, "mtudisc",
   1776 		       SYSCTL_DESCR("Use RFC1191 Path MTU Discovery"),
   1777 		       NULL, 0, &ip_mtudisc, 0,
   1778 		       CTL_NET, PF_INET, IPPROTO_IP,
   1779 		       IPCTL_MTUDISC, CTL_EOL);
   1780 	sysctl_createv(clog, 0, NULL, NULL,
   1781 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1782 		       CTLTYPE_INT, "anonportmin",
   1783 		       SYSCTL_DESCR("Lowest ephemeral port number to assign"),
   1784 		       sysctl_net_inet_ip_ports, 0, &anonportmin, 0,
   1785 		       CTL_NET, PF_INET, IPPROTO_IP,
   1786 		       IPCTL_ANONPORTMIN, CTL_EOL);
   1787 	sysctl_createv(clog, 0, NULL, NULL,
   1788 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1789 		       CTLTYPE_INT, "anonportmax",
   1790 		       SYSCTL_DESCR("Highest ephemeral port number to assign"),
   1791 		       sysctl_net_inet_ip_ports, 0, &anonportmax, 0,
   1792 		       CTL_NET, PF_INET, IPPROTO_IP,
   1793 		       IPCTL_ANONPORTMAX, CTL_EOL);
   1794 	sysctl_createv(clog, 0, NULL, NULL,
   1795 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1796 		       CTLTYPE_INT, "mtudisctimeout",
   1797 		       SYSCTL_DESCR("Lifetime of a Path MTU Discovered route"),
   1798 		       sysctl_net_inet_ip_pmtudto, 0, (void *)&ip_mtudisc_timeout, 0,
   1799 		       CTL_NET, PF_INET, IPPROTO_IP,
   1800 		       IPCTL_MTUDISCTIMEOUT, CTL_EOL);
   1801 #ifdef GATEWAY
   1802 	sysctl_createv(clog, 0, NULL, NULL,
   1803 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1804 		       CTLTYPE_INT, "maxflows",
   1805 		       SYSCTL_DESCR("Number of flows for fast forwarding"),
   1806 		       sysctl_net_inet_ip_maxflows, 0, &ip_maxflows, 0,
   1807 		       CTL_NET, PF_INET, IPPROTO_IP,
   1808 		       IPCTL_MAXFLOWS, CTL_EOL);
   1809 	sysctl_createv(clog, 0, NULL, NULL,
   1810 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1811 			CTLTYPE_INT, "hashsize",
   1812 			SYSCTL_DESCR("Size of hash table for fast forwarding (IPv4)"),
   1813 			sysctl_net_inet_ip_hashsize, 0, &ip_hashsize, 0,
   1814 			CTL_NET, PF_INET, IPPROTO_IP,
   1815 			CTL_CREATE, CTL_EOL);
   1816 #endif /* GATEWAY */
   1817 	sysctl_createv(clog, 0, NULL, NULL,
   1818 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1819 		       CTLTYPE_INT, "hostzerobroadcast",
   1820 		       SYSCTL_DESCR("All zeroes address is broadcast address"),
   1821 		       NULL, 0, &hostzeroisbroadcast, 0,
   1822 		       CTL_NET, PF_INET, IPPROTO_IP,
   1823 		       IPCTL_HOSTZEROBROADCAST, CTL_EOL);
   1824 #if NGIF > 0
   1825 	sysctl_createv(clog, 0, NULL, NULL,
   1826 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1827 		       CTLTYPE_INT, "gifttl",
   1828 		       SYSCTL_DESCR("Default TTL for a gif tunnel datagram"),
   1829 		       NULL, 0, &ip_gif_ttl, 0,
   1830 		       CTL_NET, PF_INET, IPPROTO_IP,
   1831 		       IPCTL_GIF_TTL, CTL_EOL);
   1832 #endif /* NGIF */
   1833 #ifndef IPNOPRIVPORTS
   1834 	sysctl_createv(clog, 0, NULL, NULL,
   1835 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1836 		       CTLTYPE_INT, "lowportmin",
   1837 		       SYSCTL_DESCR("Lowest privileged ephemeral port number "
   1838 				    "to assign"),
   1839 		       sysctl_net_inet_ip_ports, 0, &lowportmin, 0,
   1840 		       CTL_NET, PF_INET, IPPROTO_IP,
   1841 		       IPCTL_LOWPORTMIN, CTL_EOL);
   1842 	sysctl_createv(clog, 0, NULL, NULL,
   1843 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1844 		       CTLTYPE_INT, "lowportmax",
   1845 		       SYSCTL_DESCR("Highest privileged ephemeral port number "
   1846 				    "to assign"),
   1847 		       sysctl_net_inet_ip_ports, 0, &lowportmax, 0,
   1848 		       CTL_NET, PF_INET, IPPROTO_IP,
   1849 		       IPCTL_LOWPORTMAX, CTL_EOL);
   1850 #endif /* IPNOPRIVPORTS */
   1851 #if NGRE > 0
   1852 	sysctl_createv(clog, 0, NULL, NULL,
   1853 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1854 		       CTLTYPE_INT, "grettl",
   1855 		       SYSCTL_DESCR("Default TTL for a gre tunnel datagram"),
   1856 		       NULL, 0, &ip_gre_ttl, 0,
   1857 		       CTL_NET, PF_INET, IPPROTO_IP,
   1858 		       IPCTL_GRE_TTL, CTL_EOL);
   1859 #endif /* NGRE */
   1860 	sysctl_createv(clog, 0, NULL, NULL,
   1861 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1862 		       CTLTYPE_INT, "checkinterface",
   1863 		       SYSCTL_DESCR("Enable receive side of Strong ES model "
   1864 				    "from RFC1122"),
   1865 		       NULL, 0, &ip_checkinterface, 0,
   1866 		       CTL_NET, PF_INET, IPPROTO_IP,
   1867 		       IPCTL_CHECKINTERFACE, CTL_EOL);
   1868 	sysctl_createv(clog, 0, NULL, NULL,
   1869 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1870 		       CTLTYPE_INT, "random_id",
   1871 		       SYSCTL_DESCR("Assign random ip_id values"),
   1872 		       NULL, 0, &ip_do_randomid, 0,
   1873 		       CTL_NET, PF_INET, IPPROTO_IP,
   1874 		       IPCTL_RANDOMID, CTL_EOL);
   1875 	sysctl_createv(clog, 0, NULL, NULL,
   1876 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1877 		       CTLTYPE_INT, "do_loopback_cksum",
   1878 		       SYSCTL_DESCR("Perform IP checksum on loopback"),
   1879 		       NULL, 0, &ip_do_loopback_cksum, 0,
   1880 		       CTL_NET, PF_INET, IPPROTO_IP,
   1881 		       IPCTL_LOOPBACKCKSUM, CTL_EOL);
   1882 	sysctl_createv(clog, 0, NULL, NULL,
   1883 		       CTLFLAG_PERMANENT,
   1884 		       CTLTYPE_STRUCT, "stats",
   1885 		       SYSCTL_DESCR("IP statistics"),
   1886 		       sysctl_net_inet_ip_stats, 0, NULL, 0,
   1887 		       CTL_NET, PF_INET, IPPROTO_IP, IPCTL_STATS,
   1888 		       CTL_EOL);
   1889 }
   1890 
   1891 void
   1892 ip_statinc(u_int stat)
   1893 {
   1894 
   1895 	KASSERT(stat < IP_NSTATS);
   1896 	IP_STATINC(stat);
   1897 }
   1898