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