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