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