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