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