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