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