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