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