1/*
2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * Copyright (c) 1982, 1986, 1988, 1993
30 *	The Regents of the University of California.  All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 *    notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 *    notice, this list of conditions and the following disclaimer in the
39 *    documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 *    must display the following acknowledgement:
42 *	This product includes software developed by the University of
43 *	California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 *    may be used to endorse or promote products derived from this software
46 *    without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
61 * $FreeBSD: src/sys/netinet/ip_input.c,v 1.130.2.25 2001/08/29 21:41:37 jesper Exp $
62 */
63/*
64 * NOTICE: This file was modified by SPARTA, Inc. in 2007 to introduce
65 * support for mandatory and extensible security protections.  This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
67 * Version 2.0.
68 */
69
70#define	_IP_VHL
71
72#include <sys/param.h>
73#include <sys/systm.h>
74#include <sys/mbuf.h>
75#include <sys/malloc.h>
76#include <sys/domain.h>
77#include <sys/protosw.h>
78#include <sys/socket.h>
79#include <sys/time.h>
80#include <sys/kernel.h>
81#include <sys/syslog.h>
82#include <sys/sysctl.h>
83#include <sys/mcache.h>
84#include <mach/mach_time.h>
85
86#include <machine/endian.h>
87
88#include <kern/queue.h>
89#include <kern/locks.h>
90
91#include <pexpert/pexpert.h>
92
93#include <net/if.h>
94#include <net/if_var.h>
95#include <net/if_dl.h>
96#include <net/route.h>
97#include <net/kpi_protocol.h>
98#include <net/ntstat.h>
99
100#include <netinet/in.h>
101#include <netinet/in_systm.h>
102#include <netinet/in_var.h>
103#include <netinet/in_arp.h>
104#include <netinet/ip.h>
105#include <netinet/in_pcb.h>
106#include <netinet/ip_var.h>
107#include <netinet/ip_icmp.h>
108#include <sys/socketvar.h>
109
110#include <netinet/ip_fw.h>
111#include <netinet/ip_divert.h>
112
113#include <netinet/kpi_ipfilter_var.h>
114
115/* needed for AUTOCONFIGURING: */
116#include <netinet/udp.h>
117#include <netinet/udp_var.h>
118#include <netinet/bootp.h>
119#include <mach/sdt.h>
120
121#if CONFIG_MACF_NET
122#include <security/mac_framework.h>
123#endif
124
125#include <sys/kdebug.h>
126#include <libkern/OSAtomic.h>
127
128#define DBG_LAYER_BEG		NETDBG_CODE(DBG_NETIP, 0)
129#define DBG_LAYER_END		NETDBG_CODE(DBG_NETIP, 2)
130#define DBG_FNC_IP_INPUT	NETDBG_CODE(DBG_NETIP, (2 << 8))
131
132
133#if IPSEC
134#include <netinet6/ipsec.h>
135#include <netkey/key.h>
136#endif
137
138#if DUMMYNET
139#include <netinet/ip_dummynet.h>
140#endif
141
142#if PF
143#include <net/pfvar.h>
144#endif /* PF */
145
146#include <netinet/lro_ext.h>
147
148#if IPSEC
149extern int ipsec_bypass;
150extern lck_mtx_t *sadb_mutex;
151
152lck_grp_t         *sadb_stat_mutex_grp;
153lck_grp_attr_t    *sadb_stat_mutex_grp_attr;
154lck_attr_t        *sadb_stat_mutex_attr;
155decl_lck_mtx_data(, sadb_stat_mutex_data);
156lck_mtx_t         *sadb_stat_mutex = &sadb_stat_mutex_data;
157
158#endif
159
160int rsvp_on = 0;
161static int ip_rsvp_on;
162struct socket *ip_rsvpd;
163
164static int sysctl_ipforwarding SYSCTL_HANDLER_ARGS;
165
166int	ipforwarding = 0;
167SYSCTL_PROC(_net_inet_ip, IPCTL_FORWARDING, forwarding,
168    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &ipforwarding, 0,
169    sysctl_ipforwarding, "I", "Enable IP forwarding between interfaces");
170
171static int	ipsendredirects = 1; /* XXX */
172SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW | CTLFLAG_LOCKED,
173    &ipsendredirects, 0, "Enable sending IP redirects");
174
175int	ip_defttl = IPDEFTTL;
176SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW | CTLFLAG_LOCKED,
177    &ip_defttl, 0, "Maximum TTL on IP packets");
178
179static int	ip_dosourceroute = 0;
180SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW | CTLFLAG_LOCKED,
181    &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
182
183static int	ip_acceptsourceroute = 0;
184SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
185    CTLFLAG_RW | CTLFLAG_LOCKED, &ip_acceptsourceroute, 0,
186    "Enable accepting source routed IP packets");
187
188static int	ip_keepfaith = 0;
189SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RD | CTLFLAG_LOCKED,
190	&ip_keepfaith,	0, "");
191
192static int	nipq = 0;	/* total # of reass queues */
193static int	maxnipq;
194SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_RW | CTLFLAG_LOCKED,
195	&maxnipq, 0,
196	"Maximum number of IPv4 fragment reassembly queue entries");
197
198static int    maxfragsperpacket;
199SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW | CTLFLAG_LOCKED,
200	&maxfragsperpacket, 0,
201	"Maximum number of IPv4 fragments allowed per packet");
202
203static int    maxfrags;
204SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfrags, CTLFLAG_RW | CTLFLAG_LOCKED,
205	&maxfrags, 0, "Maximum number of IPv4 fragments allowed");
206
207static int    currentfrags = 0;
208
209int	ip_doscopedroute = 1;
210SYSCTL_INT(_net_inet_ip, OID_AUTO, scopedroute, CTLFLAG_RD | CTLFLAG_LOCKED,
211     &ip_doscopedroute, 0, "Enable IPv4 scoped routing");
212
213int	ip_restrictrecvif = 1;
214SYSCTL_INT(_net_inet_ip, OID_AUTO, restrictrecvif, CTLFLAG_RW | CTLFLAG_LOCKED,
215     &ip_restrictrecvif, 0, "Enable inbound interface restrictions");
216
217/*
218 * XXX - Setting ip_checkinterface mostly implements the receive side of
219 * the Strong ES model described in RFC 1122, but since the routing table
220 * and transmit implementation do not implement the Strong ES model,
221 * setting this to 1 results in an odd hybrid.
222 *
223 * XXX - ip_checkinterface currently must be disabled if you use ipnat
224 * to translate the destination address to another local interface.
225 *
226 * XXX - ip_checkinterface must be disabled if you add IP aliases
227 * to the loopback interface instead of the interface where the
228 * packets for those addresses are received.
229 */
230static int	ip_checkinterface = 0;
231SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW | CTLFLAG_LOCKED,
232    &ip_checkinterface, 0, "Verify packet arrives on correct interface");
233
234#if DIAGNOSTIC
235static int	ipprintfs = 0;
236#endif
237
238extern int in_proto_count;
239extern	struct domain inetdomain;
240extern	struct protosw inetsw[];
241struct protosw *ip_protox[IPPROTO_MAX];
242static int	ipqmaxlen = IFQ_MAXLEN;
243
244static lck_grp_attr_t	*in_ifaddr_rwlock_grp_attr;
245static lck_grp_t	*in_ifaddr_rwlock_grp;
246static lck_attr_t	*in_ifaddr_rwlock_attr;
247decl_lck_rw_data(, in_ifaddr_rwlock_data);
248lck_rw_t		*in_ifaddr_rwlock = &in_ifaddr_rwlock_data;
249
250/* Protected by in_ifaddr_rwlock */
251struct in_ifaddrhead in_ifaddrhead;		/* first inet address */
252struct in_ifaddrhashhead *in_ifaddrhashtbl;	/* inet addr hash table  */
253
254#define	INADDR_NHASH	61
255static u_int32_t inaddr_nhash;			/* hash table size */
256static u_int32_t inaddr_hashp;			/* next largest prime */
257
258struct	ifqueue ipintrq;
259SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW | CTLFLAG_LOCKED,
260    &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
261SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD | CTLFLAG_LOCKED,
262    &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue");
263
264struct ipstat ipstat;
265SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RD | CTLFLAG_LOCKED,
266    &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
267
268/* Packet reassembly stuff */
269#define IPREASS_NHASH_LOG2      6
270#define IPREASS_NHASH           (1 << IPREASS_NHASH_LOG2)
271#define IPREASS_HMASK           (IPREASS_NHASH - 1)
272#define IPREASS_HASH(x,y) \
273	(((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
274
275static struct ipq ipq[IPREASS_NHASH];
276static TAILQ_HEAD(ipq_list, ipq) ipq_list =
277	TAILQ_HEAD_INITIALIZER(ipq_list);
278const  int    ipintrq_present = 1;
279lck_mtx_t		*ip_mutex;
280lck_attr_t		*ip_mutex_attr;
281lck_grp_t		*ip_mutex_grp;
282lck_grp_attr_t		*ip_mutex_grp_attr;
283lck_mtx_t 		*inet_domain_mutex;
284
285#if IPCTL_DEFMTU
286SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW | CTLFLAG_LOCKED,
287    &ip_mtu, 0, "Default MTU");
288#endif
289
290#if IPSTEALTH
291static int	ipstealth = 0;
292SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW | CTLFLAG_LOCKED,
293    &ipstealth, 0, "");
294#endif
295
296
297/* Firewall hooks */
298#if IPFIREWALL
299ip_fw_chk_t *ip_fw_chk_ptr;
300int fw_enable = 1;
301int fw_bypass = 1;
302int fw_one_pass = 0;
303#endif /* IPFIREWALL */
304
305#if DUMMYNET
306ip_dn_io_t *ip_dn_io_ptr;
307#endif
308
309SYSCTL_NODE(_net_inet_ip, OID_AUTO, linklocal, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "link local");
310
311struct ip_linklocal_stat ip_linklocal_stat;
312SYSCTL_STRUCT(_net_inet_ip_linklocal, OID_AUTO, stat, CTLFLAG_RD | CTLFLAG_LOCKED,
313        &ip_linklocal_stat, ip_linklocal_stat,
314        "Number of link local packets with TTL less than 255");
315
316SYSCTL_NODE(_net_inet_ip_linklocal, OID_AUTO, in, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "link local input");
317
318int ip_linklocal_in_allowbadttl = 1;
319SYSCTL_INT(_net_inet_ip_linklocal_in, OID_AUTO, allowbadttl, CTLFLAG_RW | CTLFLAG_LOCKED,
320        &ip_linklocal_in_allowbadttl, 0,
321        "Allow incoming link local packets with TTL less than 255");
322
323
324/*
325 * We need to save the IP options in case a protocol wants to respond
326 * to an incoming packet over the same route if the packet got here
327 * using IP source routing.  This allows connection establishment and
328 * maintenance when the remote end is on a network that is not known
329 * to us.
330 */
331static int	ip_nhops = 0;
332static	struct ip_srcrt {
333	struct	in_addr dst;			/* final destination */
334	char	nop;				/* one NOP to align */
335	char	srcopt[IPOPT_OFFSET + 1];	/* OPTVAL, OLEN and OFFSET */
336	struct	in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
337} ip_srcrt;
338
339static void	in_ifaddrhashtbl_init(void);
340static void	save_rte(u_char *, struct in_addr);
341static int	ip_dooptions(struct mbuf *, int, struct sockaddr_in *);
342static void	ip_forward(struct mbuf *, int, struct sockaddr_in *);
343static void	ip_freef(struct ipq *);
344#if IPDIVERT
345#ifdef IPDIVERT_44
346static struct	mbuf *ip_reass(struct mbuf *,
347			struct ipq *, struct ipq *, u_int32_t *, u_int16_t *);
348#else
349static struct	mbuf *ip_reass(struct mbuf *,
350			struct ipq *, struct ipq *, u_int16_t *, u_int16_t *);
351#endif
352#else
353static struct	mbuf *ip_reass(struct mbuf *, struct ipq *, struct ipq *);
354#endif
355static void ip_fwd_route_copyout(struct ifnet *, struct route *);
356static void ip_fwd_route_copyin(struct ifnet *, struct route *);
357void	ipintr(void);
358void	in_dinit(void);
359static inline u_short ip_cksum(struct mbuf *, int);
360
361#if RANDOM_IP_ID
362extern u_short ip_id;
363
364int	ip_use_randomid = 1;
365SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW | CTLFLAG_LOCKED,
366    &ip_use_randomid, 0, "Randomize IP packets IDs");
367#endif
368
369/*
370 * On platforms which require strict alignment (currently for anything but
371 * i386 or x86_64), check if the IP header pointer is 32-bit aligned; if not,
372 * copy the contents of the mbuf chain into a new chain, and free the original
373 * one.  Create some head room in the first mbuf of the new chain, in case
374 * it's needed later on.
375 */
376#if defined(__i386__) || defined(__x86_64__)
377#define	IP_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { } while (0)
378#else /* !__i386__ && !__x86_64__ */
379#define	IP_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do {			\
380	if (!IP_HDR_ALIGNED_P(mtod(_m, caddr_t))) {			\
381		struct mbuf *_n;					\
382		struct ifnet *__ifp = (_ifp);				\
383		atomic_add_64(&(__ifp)->if_alignerrs, 1);		\
384		if (((_m)->m_flags & M_PKTHDR) &&			\
385		    (_m)->m_pkthdr.header != NULL)			\
386			(_m)->m_pkthdr.header = NULL;			\
387		_n = m_defrag_offset(_m, max_linkhdr, M_NOWAIT);	\
388		if (_n == NULL) {					\
389			atomic_add_32(&ipstat.ips_toosmall, 1);		\
390			m_freem(_m);					\
391			(_m) = NULL;					\
392			_action						\
393		} else {						\
394			VERIFY(_n != (_m));				\
395			(_m) = _n;					\
396		}							\
397	}								\
398} while (0)
399#endif /* !__i386__ && !__x86_64__ */
400
401/*
402 * IP initialization: fill in IP protocol switch table.
403 * All protocols not implemented in kernel go to raw IP protocol handler.
404 */
405void
406ip_init(void)
407{
408	struct protosw *pr;
409	int i;
410	static int ip_initialized = 0;
411
412	if (!ip_initialized)
413	{
414		PE_parse_boot_argn("net.inet.ip.scopedroute",
415		    &ip_doscopedroute, sizeof (ip_doscopedroute));
416
417		in_ifaddr_init();
418
419		in_ifaddr_rwlock_grp_attr = lck_grp_attr_alloc_init();
420		in_ifaddr_rwlock_grp = lck_grp_alloc_init("in_ifaddr_rwlock",
421		    in_ifaddr_rwlock_grp_attr);
422		in_ifaddr_rwlock_attr = lck_attr_alloc_init();
423		lck_rw_init(in_ifaddr_rwlock, in_ifaddr_rwlock_grp,
424		    in_ifaddr_rwlock_attr);
425
426		TAILQ_INIT(&in_ifaddrhead);
427		in_ifaddrhashtbl_init();
428
429		ip_moptions_init();
430
431		pr = pffindproto_locked(PF_INET, IPPROTO_RAW, SOCK_RAW);
432		if (pr == 0)
433			panic("ip_init");
434		for (i = 0; i < IPPROTO_MAX; i++)
435			ip_protox[i] = pr;
436		for (pr = inetdomain.dom_protosw; pr; pr = pr->pr_next) {
437			if (pr->pr_domain == NULL)
438				continue;    /* If uninitialized, skip */
439			if (pr->pr_domain->dom_family == PF_INET &&
440			    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
441				ip_protox[pr->pr_protocol] = pr;
442		}
443		for (i = 0; i < IPREASS_NHASH; i++)
444		    ipq[i].next = ipq[i].prev = &ipq[i];
445
446	maxnipq = nmbclusters / 32;
447	maxfrags = maxnipq * 2;
448	maxfragsperpacket = 128; /* enough for 64k in 512 byte fragments */
449
450#if RANDOM_IP_ID
451		{
452			struct timeval timenow;
453			getmicrotime(&timenow);
454			ip_id = timenow.tv_sec & 0xffff;
455		}
456#endif
457		ipintrq.ifq_maxlen = ipqmaxlen;
458
459		ipf_init();
460
461		ip_mutex_grp_attr  = lck_grp_attr_alloc_init();
462
463		ip_mutex_grp = lck_grp_alloc_init("ip", ip_mutex_grp_attr);
464
465		ip_mutex_attr = lck_attr_alloc_init();
466
467		if ((ip_mutex = lck_mtx_alloc_init(ip_mutex_grp, ip_mutex_attr)) == NULL) {
468			printf("ip_init: can't alloc ip_mutex\n");
469			return;
470		}
471
472#if IPSEC
473
474		sadb_stat_mutex_grp_attr = lck_grp_attr_alloc_init();
475		sadb_stat_mutex_grp = lck_grp_alloc_init("sadb_stat", sadb_stat_mutex_grp_attr);
476		sadb_stat_mutex_attr = lck_attr_alloc_init();
477
478		lck_mtx_init(sadb_stat_mutex, sadb_stat_mutex_grp, sadb_stat_mutex_attr);
479
480#endif
481		arp_init();
482
483		ip_initialized = 1;
484	}
485}
486
487/*
488 * Initialize IPv4 source address hash table.
489 */
490static void
491in_ifaddrhashtbl_init(void)
492{
493	int i, k, p;
494
495	if (in_ifaddrhashtbl != NULL)
496		return;
497
498	PE_parse_boot_argn("inaddr_nhash", &inaddr_nhash, sizeof (inaddr_nhash));
499	if (inaddr_nhash == 0)
500		inaddr_nhash = INADDR_NHASH;
501
502	MALLOC(in_ifaddrhashtbl, struct in_ifaddrhashhead *,
503	    inaddr_nhash * sizeof (*in_ifaddrhashtbl),
504	    M_IFADDR, M_WAITOK | M_ZERO);
505	if (in_ifaddrhashtbl == NULL)
506		panic("in_ifaddrhashtbl_init allocation failed");
507
508	/*
509	 * Generate the next largest prime greater than inaddr_nhash.
510	 */
511	k = (inaddr_nhash % 2 == 0) ? inaddr_nhash + 1 : inaddr_nhash + 2;
512	for (;;) {
513		p = 1;
514		for (i = 3; i * i <= k; i += 2) {
515			if (k % i == 0)
516				p = 0;
517		}
518		if (p == 1)
519			break;
520		k += 2;
521	}
522	inaddr_hashp = k;
523}
524
525u_int32_t
526inaddr_hashval(u_int32_t key)
527{
528	/*
529	 * The hash index is the computed prime times the key modulo
530	 * the hash size, as documented in "Introduction to Algorithms"
531	 * (Cormen, Leiserson, Rivest).
532	 */
533	if (inaddr_nhash > 1)
534		return ((key * inaddr_hashp) % inaddr_nhash);
535	else
536		return (0);
537}
538
539static void
540ip_proto_input(
541	protocol_family_t	__unused protocol,
542	mbuf_t				packet_list)
543{
544	mbuf_t	packet;
545	int how_many = 0 ;
546
547	/* ip_input should handle a list of packets but does not yet */
548
549	for (packet = packet_list; packet; packet = packet_list) {
550		how_many++;
551		packet_list = mbuf_nextpkt(packet);
552		mbuf_setnextpkt(packet, NULL);
553		ip_input(packet);
554	}
555}
556
557/* Initialize the PF_INET domain, and add in the pre-defined protos */
558void
559in_dinit(void)
560{
561	int i;
562	struct protosw *pr;
563	struct domain *dp;
564	static int inetdomain_initted = 0;
565
566	if (!inetdomain_initted)
567	{
568		dp = &inetdomain;
569		dp->dom_flags = DOM_REENTRANT;
570
571		for (i=0, pr = &inetsw[0]; i<in_proto_count; i++, pr++)
572			net_add_proto(pr, dp);
573		inet_domain_mutex = dp->dom_mtx;
574		inetdomain_initted = 1;
575
576		domain_proto_mtx_unlock(TRUE);
577		proto_register_input(PF_INET, ip_proto_input, NULL, 1);
578		domain_proto_mtx_lock();
579	}
580}
581
582void
583ip_proto_dispatch_in_wrapper(struct mbuf *m, int hlen, u_int8_t proto)
584{
585	ip_proto_dispatch_in(m, hlen, proto, 0);
586}
587
588__private_extern__ void
589ip_proto_dispatch_in(struct mbuf *m, int hlen, u_int8_t proto,
590    ipfilter_t inject_ipfref)
591{
592	struct ipfilter *filter;
593	int seen = (inject_ipfref == 0);
594	int	changed_header = 0;
595	struct ip *ip;
596	void (*pr_input)(struct mbuf *, int len);
597
598	if (!TAILQ_EMPTY(&ipv4_filters)) {
599		ipf_ref();
600		TAILQ_FOREACH(filter, &ipv4_filters, ipf_link) {
601			if (seen == 0) {
602				if ((struct ipfilter *)inject_ipfref == filter)
603					seen = 1;
604			} else if (filter->ipf_filter.ipf_input) {
605				errno_t result;
606
607				if (changed_header == 0) {
608					/*
609					 * Perform IP header alignment fixup,
610					 * if needed, before passing packet
611					 * into filter(s).
612					 */
613					IP_HDR_ALIGNMENT_FIXUP(m,
614					    m->m_pkthdr.rcvif,
615					    ipf_unref(); return;);
616
617					changed_header = 1;
618					ip = mtod(m, struct ip *);
619					ip->ip_len = htons(ip->ip_len + hlen);
620					ip->ip_off = htons(ip->ip_off);
621					ip->ip_sum = 0;
622					ip->ip_sum = in_cksum(m, hlen);
623				}
624				result = filter->ipf_filter.ipf_input(
625				    filter->ipf_filter.cookie, (mbuf_t*)&m,
626				    hlen, proto);
627				if (result == EJUSTRETURN) {
628					ipf_unref();
629					return;
630				}
631				if (result != 0) {
632					ipf_unref();
633					m_freem(m);
634					return;
635				}
636			}
637		}
638		ipf_unref();
639	}
640
641	/* Perform IP header alignment fixup (post-filters), if needed */
642	IP_HDR_ALIGNMENT_FIXUP(m, m->m_pkthdr.rcvif, return;);
643
644	/*
645	 * If there isn't a specific lock for the protocol
646	 * we're about to call, use the generic lock for AF_INET.
647	 * otherwise let the protocol deal with its own locking
648	 */
649	ip = mtod(m, struct ip *);
650
651	if (changed_header) {
652		ip->ip_len = ntohs(ip->ip_len) - hlen;
653		ip->ip_off = ntohs(ip->ip_off);
654	}
655
656	if ((pr_input = ip_protox[ip->ip_p]->pr_input) == NULL) {
657		m_freem(m);
658	} else if (!(ip_protox[ip->ip_p]->pr_flags & PR_PROTOLOCK)) {
659		lck_mtx_lock(inet_domain_mutex);
660		pr_input(m, hlen);
661		lck_mtx_unlock(inet_domain_mutex);
662	} else {
663		pr_input(m, hlen);
664	}
665}
666
667/*
668 * Ip input routine.  Checksum and byte swap header.  If fragmented
669 * try to reassemble.  Process options.  Pass to next level.
670 */
671void
672ip_input(struct mbuf *m)
673{
674	struct ip *ip;
675	struct ipq *fp;
676	struct in_ifaddr *ia = NULL;
677	unsigned int    hlen, checkif;
678	u_short sum = 0;
679	struct in_addr pkt_dst;
680#if IPFIREWALL
681	int i;
682	u_int32_t div_info = 0;		/* packet divert/tee info */
683#endif
684#if IPFIREWALL || DUMMYNET
685	struct ip_fw_args args;
686	struct m_tag	*tag;
687#endif
688	ipfilter_t inject_filter_ref = 0;
689
690	/* Check if the mbuf is still valid after interface filter processing */
691	MBUF_INPUT_CHECK(m, m->m_pkthdr.rcvif);
692
693	/* Perform IP header alignment fixup, if needed */
694	IP_HDR_ALIGNMENT_FIXUP(m, m->m_pkthdr.rcvif, goto bad;);
695
696#if IPFIREWALL || DUMMYNET
697	bzero(&args, sizeof(struct ip_fw_args));
698
699	/*
700	 * Don't bother searching for tag(s) if there's none.
701	 */
702	if (SLIST_EMPTY(&m->m_pkthdr.tags))
703		goto ipfw_tags_done;
704
705	/* Grab info from mtags prepended to the chain */
706#if DUMMYNET
707	if ((tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
708	    KERNEL_TAG_TYPE_DUMMYNET, NULL)) != NULL) {
709		struct dn_pkt_tag	*dn_tag;
710
711		dn_tag = (struct dn_pkt_tag *)(tag+1);
712		args.fwa_ipfw_rule = dn_tag->dn_ipfw_rule;
713		args.fwa_pf_rule = dn_tag->dn_pf_rule;
714
715		m_tag_delete(m, tag);
716	}
717#endif /* DUMMYNET */
718
719#if IPDIVERT
720	if ((tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
721	    KERNEL_TAG_TYPE_DIVERT, NULL)) != NULL) {
722		struct divert_tag	*div_tag;
723
724		div_tag = (struct divert_tag *)(tag+1);
725		args.fwa_divert_rule = div_tag->cookie;
726
727		m_tag_delete(m, tag);
728	}
729#endif
730
731	if ((tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
732	    KERNEL_TAG_TYPE_IPFORWARD, NULL)) != NULL) {
733		struct ip_fwd_tag	*ipfwd_tag;
734
735		ipfwd_tag = (struct ip_fwd_tag *)(tag+1);
736		args.fwa_next_hop = ipfwd_tag->next_hop;
737
738		m_tag_delete(m, tag);
739	}
740
741#if	DIAGNOSTIC
742	if (m == NULL || (m->m_flags & M_PKTHDR) == 0)
743		panic("ip_input no HDR");
744#endif
745
746#if DUMMYNET
747	if (args.fwa_ipfw_rule || args.fwa_pf_rule) {	/* dummynet already filtered us */
748		ip = mtod(m, struct ip *);
749		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
750		inject_filter_ref = ipf_get_inject_filter(m);
751#if IPFIREWALL
752		if (args.fwa_ipfw_rule)
753			goto iphack;
754#endif /* IPFIREWALL */
755		if (args.fwa_pf_rule)
756			goto check_with_pf;
757	}
758#endif /* DUMMYNET */
759ipfw_tags_done:
760#endif /* IPFIREWALL || DUMMYNET*/
761
762	/*
763	 * No need to process packet twice if we've already seen it.
764	 */
765	if (!SLIST_EMPTY(&m->m_pkthdr.tags))
766		inject_filter_ref = ipf_get_inject_filter(m);
767	if (inject_filter_ref != 0) {
768		ip = mtod(m, struct ip *);
769		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
770
771		DTRACE_IP6(receive, struct mbuf *, m, struct inpcb *, NULL,
772			struct ip *, ip, struct ifnet *, m->m_pkthdr.rcvif,
773			struct ip *, ip, struct ip6_hdr *, NULL);
774
775		ip->ip_len = ntohs(ip->ip_len) - hlen;
776		ip->ip_off = ntohs(ip->ip_off);
777		ip_proto_dispatch_in(m, hlen, ip->ip_p, inject_filter_ref);
778		return;
779	}
780
781	OSAddAtomic(1, &ipstat.ips_total);
782	if (m->m_pkthdr.len < sizeof(struct ip))
783		goto tooshort;
784
785	if (m->m_len < sizeof (struct ip) &&
786	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
787		OSAddAtomic(1, &ipstat.ips_toosmall);
788		return;
789	}
790	ip = mtod(m, struct ip *);
791
792	KERNEL_DEBUG(DBG_LAYER_BEG, ip->ip_dst.s_addr,
793		     ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len);
794
795	if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
796		OSAddAtomic(1, &ipstat.ips_badvers);
797		goto bad;
798	}
799
800	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
801	if (hlen < sizeof(struct ip)) {	/* minimum header length */
802		OSAddAtomic(1, &ipstat.ips_badhlen);
803		goto bad;
804	}
805	if (hlen > m->m_len) {
806		if ((m = m_pullup(m, hlen)) == 0) {
807			OSAddAtomic(1, &ipstat.ips_badhlen);
808			return;
809		}
810		ip = mtod(m, struct ip *);
811	}
812
813	/* 127/8 must not appear on wire - RFC1122 */
814	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
815	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
816		if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
817			OSAddAtomic(1, &ipstat.ips_badaddr);
818			goto bad;
819		}
820	}
821
822	/* IPv4 Link-Local Addresses as defined in <draft-ietf-zeroconf-ipv4-linklocal-05.txt> */
823	if ((IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
824	    IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)))) {
825		ip_linklocal_stat.iplls_in_total++;
826		if (ip->ip_ttl != MAXTTL) {
827			OSAddAtomic(1, &ip_linklocal_stat.iplls_in_badttl);
828			/* Silently drop link local traffic with bad TTL */
829			if (!ip_linklocal_in_allowbadttl)
830				goto bad;
831		}
832	}
833
834	sum = ip_cksum(m, hlen);
835	if (sum) {
836		goto bad;
837	}
838
839	DTRACE_IP6(receive, struct mbuf *, m, struct inpcb *, NULL,
840		struct ip *, ip, struct ifnet *, m->m_pkthdr.rcvif,
841		struct ip *, ip, struct ip6_hdr *, NULL);
842
843	/*
844	 * Naively assume we can attribute inbound data to the route we would
845	 * use to send to this destination. Asymetric routing breaks this
846	 * assumption, but it still allows us to account for traffic from
847	 * a remote node in the routing table.
848	 * this has a very significant performance impact so we bypass
849	 * if nstat_collect is disabled. We may also bypass if the
850	 * protocol is tcp in the future because tcp will have a route that
851	 * we can use to attribute the data to. That does mean we would not
852	 * account for forwarded tcp traffic.
853	 */
854	if (nstat_collect) {
855		struct rtentry *rt =
856		    ifnet_cached_rtlookup_inet(m->m_pkthdr.rcvif, ip->ip_src);
857		if (rt != NULL) {
858			nstat_route_rx(rt, 1, m->m_pkthdr.len, 0);
859			rtfree(rt);
860		}
861	}
862
863	/*
864	 * Convert fields to host representation.
865	 */
866#if BYTE_ORDER != BIG_ENDIAN
867	NTOHS(ip->ip_len);
868#endif
869
870	if (ip->ip_len < hlen) {
871		OSAddAtomic(1, &ipstat.ips_badlen);
872		goto bad;
873	}
874
875#if BYTE_ORDER != BIG_ENDIAN
876	NTOHS(ip->ip_off);
877#endif
878	/*
879	 * Check that the amount of data in the buffers
880	 * is as at least much as the IP header would have us expect.
881	 * Trim mbufs if longer than we expect.
882	 * Drop packet if shorter than we expect.
883	 */
884	if (m->m_pkthdr.len < ip->ip_len) {
885tooshort:
886		OSAddAtomic(1, &ipstat.ips_tooshort);
887		goto bad;
888	}
889	if (m->m_pkthdr.len > ip->ip_len) {
890		/* Invalidate hwcksuming */
891		m->m_pkthdr.csum_flags = 0;
892		m->m_pkthdr.csum_data = 0;
893
894		if (m->m_len == m->m_pkthdr.len) {
895			m->m_len = ip->ip_len;
896			m->m_pkthdr.len = ip->ip_len;
897		} else
898			m_adj(m, ip->ip_len - m->m_pkthdr.len);
899	}
900
901
902#if DUMMYNET
903check_with_pf:
904#endif
905#if PF
906	/* Invoke inbound packet filter */
907	if (PF_IS_ENABLED) {
908		int error;
909#if DUMMYNET
910		error = pf_af_hook(m->m_pkthdr.rcvif, NULL, &m, AF_INET, TRUE, &args);
911#else
912		error = pf_af_hook(m->m_pkthdr.rcvif, NULL, &m, AF_INET, TRUE, NULL);
913#endif /* DUMMYNET */
914		if (error != 0 || m == NULL) {
915			if (m != NULL) {
916				panic("%s: unexpected packet %p\n", __func__, m);
917				/* NOTREACHED */
918			}
919			/* Already freed by callee */
920			return;
921		}
922		ip = mtod(m, struct ip *);
923		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
924	}
925#endif /* PF */
926
927#if IPSEC
928	if (ipsec_bypass == 0 && ipsec_gethist(m, NULL))
929		goto pass;
930#endif
931
932#if IPFIREWALL
933#if DUMMYNET
934iphack:
935#endif /* DUMMYNET */
936	/*
937	 * Check if we want to allow this packet to be processed.
938	 * Consider it to be bad if not.
939	 */
940	if (fw_enable && IPFW_LOADED) {
941#if IPFIREWALL_FORWARD
942		/*
943		 * If we've been forwarded from the output side, then
944		 * skip the firewall a second time
945		 */
946		if (args.fwa_next_hop)
947			goto ours;
948#endif	/* IPFIREWALL_FORWARD */
949
950		args.fwa_m = m;
951
952		i = ip_fw_chk_ptr(&args);
953		m = args.fwa_m;
954
955		if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) { /* drop */
956			if (m)
957				m_freem(m);
958			return;
959		}
960		ip = mtod(m, struct ip *); /* just in case m changed */
961
962		if (i == 0 && args.fwa_next_hop == NULL) {	/* common case */
963			goto pass;
964		}
965#if DUMMYNET
966                if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG) != 0) {
967			/* Send packet to the appropriate pipe */
968			ip_dn_io_ptr(m, i&0xffff, DN_TO_IP_IN, &args, DN_CLIENT_IPFW);
969			return;
970		}
971#endif /* DUMMYNET */
972#if IPDIVERT
973		if (i != 0 && (i & IP_FW_PORT_DYNT_FLAG) == 0) {
974			/* Divert or tee packet */
975			div_info = i;
976			goto ours;
977		}
978#endif
979#if IPFIREWALL_FORWARD
980		if (i == 0 && args.fwa_next_hop != NULL) {
981			goto pass;
982		}
983#endif
984		/*
985		 * if we get here, the packet must be dropped
986		 */
987		m_freem(m);
988		return;
989	}
990#endif /* IPFIREWALL */
991pass:
992
993	/*
994	 * Process options and, if not destined for us,
995	 * ship it on.  ip_dooptions returns 1 when an
996	 * error was detected (causing an icmp message
997	 * to be sent and the original packet to be freed).
998	 */
999	ip_nhops = 0;		/* for source routed packets */
1000#if IPFIREWALL
1001	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0, args.fwa_next_hop)) {
1002#else
1003	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0, NULL)) {
1004#endif
1005		return;
1006	}
1007
1008        /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
1009         * matter if it is destined to another node, or whether it is
1010         * a multicast one, RSVP wants it! and prevents it from being forwarded
1011         * anywhere else. Also checks if the rsvp daemon is running before
1012	 * grabbing the packet.
1013         */
1014	if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
1015		goto ours;
1016
1017	/*
1018	 * Check our list of addresses, to see if the packet is for us.
1019	 * If we don't have any addresses, assume any unicast packet
1020	 * we receive might be for us (and let the upper layers deal
1021	 * with it).
1022	 */
1023	if (TAILQ_EMPTY(&in_ifaddrhead) &&
1024	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
1025		goto ours;
1026
1027	/*
1028	 * Cache the destination address of the packet; this may be
1029	 * changed by use of 'ipfw fwd'.
1030	 */
1031#if IPFIREWALL
1032	pkt_dst = args.fwa_next_hop == NULL ?
1033	    ip->ip_dst : args.fwa_next_hop->sin_addr;
1034#else
1035	pkt_dst = ip->ip_dst;
1036#endif
1037
1038	/*
1039	 * Enable a consistency check between the destination address
1040	 * and the arrival interface for a unicast packet (the RFC 1122
1041	 * strong ES model) if IP forwarding is disabled and the packet
1042	 * is not locally generated and the packet is not subject to
1043	 * 'ipfw fwd'.
1044	 *
1045	 * XXX - Checking also should be disabled if the destination
1046	 * address is ipnat'ed to a different interface.
1047	 *
1048	 * XXX - Checking is incompatible with IP aliases added
1049	 * to the loopback interface instead of the interface where
1050	 * the packets are received.
1051	 */
1052	checkif = ip_checkinterface && (ipforwarding == 0) &&
1053	    ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0)
1054#if IPFIREWALL
1055	    && (args.fwa_next_hop == NULL);
1056#else
1057		;
1058#endif
1059
1060	/*
1061	 * Check for exact addresses in the hash bucket.
1062	 */
1063	lck_rw_lock_shared(in_ifaddr_rwlock);
1064	TAILQ_FOREACH(ia, INADDR_HASH(pkt_dst.s_addr), ia_hash) {
1065		/*
1066		 * If the address matches, verify that the packet
1067		 * arrived via the correct interface if checking is
1068		 * enabled.
1069		 */
1070		IFA_LOCK_SPIN(&ia->ia_ifa);
1071		if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst.s_addr &&
1072		    (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif)) {
1073			IFA_UNLOCK(&ia->ia_ifa);
1074			lck_rw_done(in_ifaddr_rwlock);
1075			goto ours;
1076		}
1077		IFA_UNLOCK(&ia->ia_ifa);
1078	}
1079	lck_rw_done(in_ifaddr_rwlock);
1080
1081	/*
1082	 * Check for broadcast addresses.
1083	 *
1084	 * Only accept broadcast packets that arrive via the matching
1085	 * interface.  Reception of forwarded directed broadcasts would be
1086	 * handled via ip_forward() and ether_frameout() with the loopback
1087	 * into the stack for SIMPLEX interfaces handled by ether_frameout().
1088	 */
1089	if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
1090		struct ifaddr *ifa;
1091		struct ifnet *ifp = m->m_pkthdr.rcvif;
1092		ifnet_lock_shared(ifp);
1093		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1094			IFA_LOCK_SPIN(ifa);
1095			if (ifa->ifa_addr->sa_family != AF_INET) {
1096				IFA_UNLOCK(ifa);
1097				continue;
1098			}
1099			ia = ifatoia(ifa);
1100			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
1101			    pkt_dst.s_addr || ia->ia_netbroadcast.s_addr ==
1102			    pkt_dst.s_addr) {
1103				IFA_UNLOCK(ifa);
1104				ifnet_lock_done(ifp);
1105				goto ours;
1106			}
1107			IFA_UNLOCK(ifa);
1108		}
1109		ifnet_lock_done(ifp);
1110	}
1111
1112	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
1113		struct in_multi *inm;
1114		struct ifnet *ifp = m->m_pkthdr.rcvif;
1115#if MROUTING
1116		if (ip_mrouter) {
1117			/*
1118			 * If we are acting as a multicast router, all
1119			 * incoming multicast packets are passed to the
1120			 * kernel-level multicast forwarding function.
1121			 * The packet is returned (relatively) intact; if
1122			 * ip_mforward() returns a non-zero value, the packet
1123			 * must be discarded, else it may be accepted below.
1124			 */
1125			lck_mtx_lock(ip_mutex);
1126			if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
1127				OSAddAtomic(1, &ipstat.ips_cantforward);
1128				m_freem(m);
1129				lck_mtx_unlock(ip_mutex);
1130				return;
1131			}
1132
1133			/*
1134			 * The process-level routing daemon needs to receive
1135			 * all multicast IGMP packets, whether or not this
1136			 * host belongs to their destination groups.
1137			 */
1138			if (ip->ip_p == IPPROTO_IGMP)
1139				goto ours;
1140			OSAddAtomic(1, &ipstat.ips_forward);
1141		}
1142#endif /* MROUTING */
1143		/*
1144		 * See if we belong to the destination multicast group on the
1145		 * arrival interface.
1146		 */
1147		in_multihead_lock_shared();
1148		IN_LOOKUP_MULTI(&ip->ip_dst, ifp, inm);
1149		in_multihead_lock_done();
1150		if (inm == NULL) {
1151			OSAddAtomic(1, &ipstat.ips_notmember);
1152			m_freem(m);
1153			return;
1154		}
1155		INM_REMREF(inm);
1156		goto ours;
1157	}
1158	if (ip->ip_dst.s_addr == (u_int32_t)INADDR_BROADCAST)
1159		goto ours;
1160	if (ip->ip_dst.s_addr == INADDR_ANY)
1161		goto ours;
1162
1163	/* Allow DHCP/BootP responses through */
1164	if (m->m_pkthdr.rcvif != NULL
1165	    && (m->m_pkthdr.rcvif->if_eflags & IFEF_AUTOCONFIGURING)
1166	    && hlen == sizeof(struct ip)
1167	    && ip->ip_p == IPPROTO_UDP) {
1168		struct udpiphdr *ui;
1169		if (m->m_len < sizeof(struct udpiphdr)
1170		    && (m = m_pullup(m, sizeof(struct udpiphdr))) == 0) {
1171			OSAddAtomic(1, &udpstat.udps_hdrops);
1172			return;
1173		}
1174		ui = mtod(m, struct udpiphdr *);
1175		if (ntohs(ui->ui_dport) == IPPORT_BOOTPC) {
1176			goto ours;
1177		}
1178		ip = mtod(m, struct ip *); /* in case it changed */
1179	}
1180
1181	/*
1182	 * Not for us; forward if possible and desirable.
1183	 */
1184	if (ipforwarding == 0) {
1185		OSAddAtomic(1, &ipstat.ips_cantforward);
1186		m_freem(m);
1187	} else {
1188#if IPFIREWALL
1189		ip_forward(m, 0, args.fwa_next_hop);
1190#else
1191		ip_forward(m, 0, NULL);
1192#endif
1193	}
1194	return;
1195
1196ours:
1197	/*
1198	 * If offset or IP_MF are set, must reassemble.
1199	 * Otherwise, nothing need be done.
1200	 * (We could look in the reassembly queue to see
1201	 * if the packet was previously fragmented,
1202	 * but it's not worth the time; just let them time out.)
1203	 */
1204	if (ip->ip_off & (IP_MF | IP_OFFMASK | IP_RF)) {
1205
1206		/* If maxnipq is 0, never accept fragments. */
1207		if (maxnipq == 0) {
1208
1209			OSAddAtomic(1, &ipstat.ips_fragments);
1210			OSAddAtomic(1, &ipstat.ips_fragdropped);
1211			goto bad;
1212		}
1213
1214		/*
1215		 * If we will exceed the number of fragments in queues, timeout the
1216		 * oldest fragemented packet to make space.
1217		 */
1218		lck_mtx_lock(ip_mutex);
1219		if (currentfrags >= maxfrags) {
1220			fp = TAILQ_LAST(&ipq_list, ipq_list);
1221			OSAddAtomic(fp->ipq_nfrags, &ipstat.ips_fragtimeout);
1222
1223			if (ip->ip_id == fp->ipq_id &&
1224				ip->ip_src.s_addr == fp->ipq_src.s_addr &&
1225				ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
1226				ip->ip_p == fp->ipq_p) {
1227				/*
1228				 * If we match the fragment queue we were going to
1229				 * discard, drop this packet too.
1230				 */
1231				OSAddAtomic(1, &ipstat.ips_fragdropped);
1232				ip_freef(fp);
1233				lck_mtx_unlock(ip_mutex);
1234				goto bad;
1235			}
1236
1237			ip_freef(fp);
1238		}
1239
1240		sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
1241		/*
1242		 * Look for queue of fragments
1243		 * of this datagram.
1244		 */
1245		for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next)
1246			if (ip->ip_id == fp->ipq_id &&
1247			    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
1248			    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
1249#if CONFIG_MACF_NET
1250			    mac_ipq_label_compare(m, fp) &&
1251#endif
1252			    ip->ip_p == fp->ipq_p)
1253				goto found;
1254
1255		/*
1256		 * Enforce upper bound on number of fragmented packets
1257		 * for which we attempt reassembly;
1258		 * If maxnipq is -1, accept all fragments without limitation.
1259		 */
1260		if ((nipq > maxnipq) && (maxnipq > 0)) {
1261		    /*
1262		     * drop the oldest fragment before proceeding further
1263		     */
1264		    fp = TAILQ_LAST(&ipq_list, ipq_list);
1265		    OSAddAtomic(fp->ipq_nfrags, &ipstat.ips_fragtimeout);
1266		    ip_freef(fp);
1267		}
1268
1269		fp = NULL;
1270
1271found:
1272		/*
1273		 * Adjust ip_len to not reflect header,
1274		 * convert offset of this to bytes.
1275		 */
1276		ip->ip_len -= hlen;
1277		if (ip->ip_off & IP_MF) {
1278		        /*
1279		         * Make sure that fragments have a data length
1280				 * that's a non-zero multiple of 8 bytes.
1281		         */
1282			if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
1283				OSAddAtomic(1, &ipstat.ips_toosmall);
1284				lck_mtx_unlock(ip_mutex);
1285				goto bad;
1286			}
1287			m->m_flags |= M_FRAG;
1288		} else {
1289			/* Clear the flag in case packet comes from loopback */
1290			m->m_flags &= ~M_FRAG;
1291		}
1292		ip->ip_off <<= 3;
1293
1294		/*
1295		 * Attempt reassembly; if it succeeds, proceed.
1296		 * ip_reass() will return a different mbuf, and update
1297		 * the divert info in div_info and args.fwa_divert_rule.
1298		 */
1299			OSAddAtomic(1, &ipstat.ips_fragments);
1300			m->m_pkthdr.header = ip;
1301#if IPDIVERT
1302			m = ip_reass(m, fp, &ipq[sum],
1303			    (u_int16_t *)&div_info, &args.fwa_divert_rule);
1304#else
1305			m = ip_reass(m, fp, &ipq[sum]);
1306#endif
1307			if (m == 0) {
1308				lck_mtx_unlock(ip_mutex);
1309				return;
1310			}
1311			OSAddAtomic(1, &ipstat.ips_reassembled);
1312			ip = mtod(m, struct ip *);
1313			/* Get the header length of the reassembled packet */
1314			hlen = IP_VHL_HL(ip->ip_vhl) << 2;
1315
1316#if IPDIVERT
1317			/* Restore original checksum before diverting packet */
1318			if (div_info != 0) {
1319				ip->ip_len += hlen;
1320
1321#if BYTE_ORDER != BIG_ENDIAN
1322				HTONS(ip->ip_len);
1323				HTONS(ip->ip_off);
1324#endif
1325
1326				ip->ip_sum = 0;
1327				ip->ip_sum = in_cksum(m, hlen);
1328
1329#if BYTE_ORDER != BIG_ENDIAN
1330				NTOHS(ip->ip_off);
1331				NTOHS(ip->ip_len);
1332#endif
1333
1334				ip->ip_len -= hlen;
1335			}
1336#endif
1337		lck_mtx_unlock(ip_mutex);
1338		} else
1339		ip->ip_len -= hlen;
1340
1341#if IPDIVERT
1342	/*
1343	 * Divert or tee packet to the divert protocol if required.
1344	 *
1345	 * If div_info is zero then cookie should be too, so we shouldn't
1346	 * need to clear them here.  Assume divert_packet() does so also.
1347	 */
1348	if (div_info != 0) {
1349		struct mbuf *clone = NULL;
1350
1351		/* Clone packet if we're doing a 'tee' */
1352		if ((div_info & IP_FW_PORT_TEE_FLAG) != 0)
1353			clone = m_dup(m, M_DONTWAIT);
1354
1355		/* Restore packet header fields to original values */
1356		ip->ip_len += hlen;
1357
1358#if BYTE_ORDER != BIG_ENDIAN
1359		HTONS(ip->ip_len);
1360		HTONS(ip->ip_off);
1361#endif
1362		/* Deliver packet to divert input routine */
1363		OSAddAtomic(1, &ipstat.ips_delivered);
1364		divert_packet(m, 1, div_info & 0xffff, args.fwa_divert_rule);
1365
1366		/* If 'tee', continue with original packet */
1367		if (clone == NULL) {
1368			return;
1369		}
1370		m = clone;
1371		ip = mtod(m, struct ip *);
1372	}
1373#endif
1374
1375#if IPSEC
1376	/*
1377	 * enforce IPsec policy checking if we are seeing last header.
1378	 * note that we do not visit this with protocols with pcb layer
1379	 * code - like udp/tcp/raw ip.
1380	 */
1381	if (ipsec_bypass == 0 && (ip_protox[ip->ip_p]->pr_flags & PR_LASTHDR) != 0) {
1382		if (ipsec4_in_reject(m, NULL)) {
1383			IPSEC_STAT_INCREMENT(ipsecstat.in_polvio);
1384			goto bad;
1385		}
1386	}
1387#endif
1388
1389	/*
1390	 * Switch out to protocol's input routine.
1391	 */
1392	OSAddAtomic(1, &ipstat.ips_delivered);
1393	{
1394#if IPFIREWALL
1395		if (args.fwa_next_hop && ip->ip_p == IPPROTO_TCP) {
1396			/* TCP needs IPFORWARD info if available */
1397			struct m_tag *fwd_tag;
1398			struct ip_fwd_tag	*ipfwd_tag;
1399
1400			fwd_tag = m_tag_create(KERNEL_MODULE_TAG_ID,
1401			    KERNEL_TAG_TYPE_IPFORWARD, sizeof (*ipfwd_tag),
1402			    M_NOWAIT, m);
1403			if (fwd_tag == NULL) {
1404				goto bad;
1405			}
1406
1407			ipfwd_tag = (struct ip_fwd_tag *)(fwd_tag+1);
1408			ipfwd_tag->next_hop = args.fwa_next_hop;
1409
1410			m_tag_prepend(m, fwd_tag);
1411
1412			KERNEL_DEBUG(DBG_LAYER_END, ip->ip_dst.s_addr,
1413			     ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len);
1414
1415			if (sw_lro) {
1416				m = tcp_lro(m, hlen);
1417				if (m == NULL)
1418					return;
1419			}
1420			/* TCP deals with its own locking */
1421			ip_proto_dispatch_in(m, hlen, ip->ip_p, 0);
1422		} else {
1423			KERNEL_DEBUG(DBG_LAYER_END, ip->ip_dst.s_addr,
1424			     ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len);
1425
1426			ip_proto_dispatch_in(m, hlen, ip->ip_p, 0);
1427		}
1428#else
1429		if ((sw_lro) && (ip->ip_p == IPPROTO_TCP)) {
1430			m = tcp_lro(m, hlen);
1431			if (m == NULL)
1432				return;
1433		}
1434		ip_proto_dispatch_in(m, hlen, ip->ip_p, 0);
1435#endif
1436
1437		return;
1438	}
1439bad:
1440	KERNEL_DEBUG(DBG_LAYER_END, 0,0,0,0,0);
1441	m_freem(m);
1442}
1443
1444/*
1445 * Take incoming datagram fragment and try to reassemble it into
1446 * whole datagram.  If a chain for reassembly of this datagram already
1447 * exists, then it is given as fp; otherwise have to make a chain.
1448 *
1449 * When IPDIVERT enabled, keep additional state with each packet that
1450 * tells us if we need to divert or tee the packet we're building.
1451 */
1452
1453static struct mbuf *
1454#if IPDIVERT
1455ip_reass(struct mbuf *m, struct ipq *fp, struct ipq *where,
1456#ifdef IPDIVERT_44
1457	 u_int32_t *divinfo,
1458#else /* IPDIVERT_44 */
1459	 u_int16_t *divinfo,
1460#endif /* IPDIVERT_44 */
1461	 u_int16_t *divcookie)
1462#else /* IPDIVERT */
1463ip_reass(struct mbuf *m, struct ipq *fp, struct ipq *where)
1464#endif /* IPDIVERT */
1465{
1466	struct ip *ip = mtod(m, struct ip *);
1467	struct mbuf *p = 0, *q, *nq;
1468	struct mbuf *t;
1469	int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
1470	int i, next;
1471	u_int8_t ecn, ecn0;
1472
1473	lck_mtx_assert(ip_mutex, LCK_MTX_ASSERT_OWNED);
1474	/*
1475	 * Presence of header sizes in mbufs
1476	 * would confuse code below.
1477	 */
1478	m->m_data += hlen;
1479	m->m_len -= hlen;
1480
1481	if (m->m_pkthdr.csum_flags & CSUM_TCP_SUM16)
1482               	m->m_pkthdr.csum_flags = 0;
1483	/*
1484	 * If first fragment to arrive, create a reassembly queue.
1485	 */
1486	if (fp == 0) {
1487		if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
1488			goto dropfrag;
1489		fp = mtod(t, struct ipq *);
1490#if CONFIG_MACF_NET
1491		if (mac_ipq_label_init(fp, M_NOWAIT) != 0) {
1492			m_free(t);
1493			fp = NULL;
1494			goto dropfrag;
1495		}
1496		mac_ipq_label_associate(m, fp);
1497#endif
1498		insque((void*)fp, (void*)where);
1499		nipq++;
1500		fp->ipq_nfrags = 1;
1501		fp->ipq_ttl = IPFRAGTTL;
1502		fp->ipq_p = ip->ip_p;
1503		fp->ipq_id = ip->ip_id;
1504		fp->ipq_src = ip->ip_src;
1505		fp->ipq_dst = ip->ip_dst;
1506		fp->ipq_frags = m;
1507		m->m_nextpkt = NULL;
1508#if IPDIVERT
1509#ifdef IPDIVERT_44
1510		fp->ipq_div_info = 0;
1511#else
1512		fp->ipq_divert = 0;
1513#endif
1514		fp->ipq_div_cookie = 0;
1515#endif
1516		TAILQ_INSERT_HEAD(&ipq_list, fp, ipq_list);
1517		goto inserted;
1518	} else {
1519		fp->ipq_nfrags++;
1520#if CONFIG_MACF_NET
1521		mac_ipq_label_update(m, fp);
1522#endif
1523	}
1524
1525#define GETIP(m)	((struct ip*)((m)->m_pkthdr.header))
1526
1527	/*
1528	 * Handle ECN by comparing this segment with the first one;
1529	 * if CE is set, do not lose CE.
1530	 * drop if CE and not-ECT are mixed for the same packet.
1531	 */
1532	ecn = ip->ip_tos & IPTOS_ECN_MASK;
1533	ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
1534	if (ecn == IPTOS_ECN_CE) {
1535		if (ecn0 == IPTOS_ECN_NOTECT)
1536			goto dropfrag;
1537		if (ecn0 != IPTOS_ECN_CE)
1538			GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
1539	}
1540	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
1541		goto dropfrag;
1542
1543	/*
1544	 * Find a segment which begins after this one does.
1545	 */
1546	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
1547		if (GETIP(q)->ip_off > ip->ip_off)
1548			break;
1549
1550	/*
1551	 * If there is a preceding segment, it may provide some of
1552	 * our data already.  If so, drop the data from the incoming
1553	 * segment.  If it provides all of our data, drop us, otherwise
1554	 * stick new segment in the proper place.
1555	 *
1556	 * If some of the data is dropped from the the preceding
1557	 * segment, then it's checksum is invalidated.
1558	 */
1559	if (p) {
1560		i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
1561		if (i > 0) {
1562			if (i >= ip->ip_len)
1563				goto dropfrag;
1564			m_adj(m, i);
1565			m->m_pkthdr.csum_flags = 0;
1566			ip->ip_off += i;
1567			ip->ip_len -= i;
1568		}
1569		m->m_nextpkt = p->m_nextpkt;
1570		p->m_nextpkt = m;
1571	} else {
1572		m->m_nextpkt = fp->ipq_frags;
1573		fp->ipq_frags = m;
1574	}
1575
1576	/*
1577	 * While we overlap succeeding segments trim them or,
1578	 * if they are completely covered, dequeue them.
1579	 */
1580	for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
1581	     q = nq) {
1582		i = (ip->ip_off + ip->ip_len) -
1583		    GETIP(q)->ip_off;
1584		if (i < GETIP(q)->ip_len) {
1585			GETIP(q)->ip_len -= i;
1586			GETIP(q)->ip_off += i;
1587			m_adj(q, i);
1588			q->m_pkthdr.csum_flags = 0;
1589			break;
1590		}
1591		nq = q->m_nextpkt;
1592		m->m_nextpkt = nq;
1593		OSAddAtomic(1, &ipstat.ips_fragdropped);
1594		fp->ipq_nfrags--;
1595		m_freem(q);
1596	}
1597
1598inserted:
1599	currentfrags++;
1600
1601#if IPDIVERT
1602	/*
1603	 * Transfer firewall instructions to the fragment structure.
1604	 * Only trust info in the fragment at offset 0.
1605	 */
1606	if (ip->ip_off == 0) {
1607#ifdef IPDIVERT_44
1608	fp->ipq_div_info = *divinfo;
1609#else
1610	fp->ipq_divert = *divinfo;
1611#endif
1612	fp->ipq_div_cookie = *divcookie;
1613	}
1614	*divinfo = 0;
1615	*divcookie = 0;
1616#endif
1617
1618	/*
1619	 * Check for complete reassembly and perform frag per packet
1620	 * limiting.
1621	 *
1622	 * Frag limiting is performed here so that the nth frag has
1623	 * a chance to complete the packet before we drop the packet.
1624	 * As a result, n+1 frags are actually allowed per packet, but
1625	 * only n will ever be stored. (n = maxfragsperpacket.)
1626	 *
1627	 */
1628	next = 0;
1629	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
1630		if (GETIP(q)->ip_off != next) {
1631			if (fp->ipq_nfrags > maxfragsperpacket) {
1632				OSAddAtomic(fp->ipq_nfrags, &ipstat.ips_fragdropped);
1633				ip_freef(fp);
1634			}
1635			return (0);
1636		}
1637		next += GETIP(q)->ip_len;
1638	}
1639	/* Make sure the last packet didn't have the IP_MF flag */
1640	if (p->m_flags & M_FRAG) {
1641		if (fp->ipq_nfrags > maxfragsperpacket) {
1642			OSAddAtomic(fp->ipq_nfrags, &ipstat.ips_fragdropped);
1643			ip_freef(fp);
1644		}
1645		return (0);
1646	}
1647
1648	/*
1649	 * Reassembly is complete.  Make sure the packet is a sane size.
1650	 */
1651	q = fp->ipq_frags;
1652	ip = GETIP(q);
1653	if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) {
1654		OSAddAtomic(1, &ipstat.ips_toolong);
1655		OSAddAtomic(fp->ipq_nfrags, &ipstat.ips_fragdropped);
1656		ip_freef(fp);
1657		return (0);
1658	}
1659
1660	/*
1661	 * Concatenate fragments.
1662	 */
1663	m = q;
1664	t = m->m_next;
1665	m->m_next = 0;
1666	m_cat(m, t);
1667	nq = q->m_nextpkt;
1668	q->m_nextpkt = 0;
1669	for (q = nq; q != NULL; q = nq) {
1670		nq = q->m_nextpkt;
1671		q->m_nextpkt = NULL;
1672		if (q->m_pkthdr.csum_flags & CSUM_TCP_SUM16)
1673	    		m->m_pkthdr.csum_flags = 0;
1674		else {
1675			m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1676			m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1677		}
1678		m_cat(m, q);
1679	}
1680
1681#if IPDIVERT
1682	/*
1683	 * Extract firewall instructions from the fragment structure.
1684	 */
1685#ifdef IPDIVERT_44
1686	*divinfo = fp->ipq_div_info;
1687#else
1688	*divinfo = fp->ipq_divert;
1689#endif
1690	*divcookie = fp->ipq_div_cookie;
1691#endif
1692
1693#if CONFIG_MACF_NET
1694	mac_mbuf_label_associate_ipq(fp, m);
1695	mac_ipq_label_destroy(fp);
1696#endif
1697	/*
1698	 * Create header for new ip packet by
1699	 * modifying header of first packet;
1700	 * dequeue and discard fragment reassembly header.
1701	 * Make header visible.
1702	 */
1703	ip->ip_len = next;
1704	ip->ip_src = fp->ipq_src;
1705	ip->ip_dst = fp->ipq_dst;
1706	remque((void*)fp);
1707	TAILQ_REMOVE(&ipq_list, fp, ipq_list);
1708	currentfrags -= fp->ipq_nfrags;
1709	nipq--;
1710	(void) m_free(dtom(fp));
1711	m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2);
1712	m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2);
1713	/* some debugging cruft by sklower, below, will go away soon */
1714	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
1715		int plen = 0;
1716		for (t = m; t; t = t->m_next)
1717			plen += t->m_len;
1718		m->m_pkthdr.len = plen;
1719	}
1720	return (m);
1721
1722dropfrag:
1723#if IPDIVERT
1724	*divinfo = 0;
1725	*divcookie = 0;
1726#endif
1727	OSAddAtomic(1, &ipstat.ips_fragdropped);
1728	if (fp != 0)
1729		fp->ipq_nfrags--;
1730	m_freem(m);
1731	return (0);
1732
1733#undef GETIP
1734}
1735
1736/*
1737 * Free a fragment reassembly header and all
1738 * associated datagrams.
1739 */
1740static void
1741ip_freef(struct ipq *fp)
1742{
1743	lck_mtx_assert(ip_mutex, LCK_MTX_ASSERT_OWNED);
1744	currentfrags -= fp->ipq_nfrags;
1745	m_freem_list(fp->ipq_frags);
1746	remque((void*)fp);
1747	TAILQ_REMOVE(&ipq_list, fp, ipq_list);
1748	(void) m_free(dtom(fp));
1749	nipq--;
1750}
1751
1752/*
1753 * IP timer processing;
1754 * if a timer expires on a reassembly
1755 * queue, discard it.
1756 */
1757void
1758ip_slowtimo(void)
1759{
1760	struct ipq *fp;
1761	int i;
1762	lck_mtx_lock(ip_mutex);
1763	for (i = 0; i < IPREASS_NHASH; i++) {
1764		fp = ipq[i].next;
1765		if (fp == 0)
1766			continue;
1767		while (fp != &ipq[i]) {
1768			--fp->ipq_ttl;
1769			fp = fp->next;
1770			if (fp->prev->ipq_ttl == 0) {
1771				OSAddAtomic(fp->ipq_nfrags, &ipstat.ips_fragtimeout);
1772				ip_freef(fp->prev);
1773			}
1774		}
1775	}
1776	/*
1777	 * If we are over the maximum number of fragments
1778	 * (due to the limit being lowered), drain off
1779	 * enough to get down to the new limit.
1780	 */
1781	if (maxnipq >= 0 && nipq > maxnipq) {
1782	for (i = 0; i < IPREASS_NHASH; i++) {
1783			while (nipq > maxnipq &&
1784				(ipq[i].next != &ipq[i])) {
1785				OSAddAtomic(ipq[i].next->ipq_nfrags, &ipstat.ips_fragdropped);
1786				ip_freef(ipq[i].next);
1787			}
1788		}
1789	}
1790	lck_mtx_unlock(ip_mutex);
1791}
1792
1793/*
1794 * Drain off all datagram fragments.
1795 */
1796void
1797ip_drain(void)
1798{
1799	int     i;
1800
1801	lck_mtx_lock(ip_mutex);
1802	for (i = 0; i < IPREASS_NHASH; i++) {
1803		while (ipq[i].next != &ipq[i]) {
1804			OSAddAtomic(ipq[i].next->ipq_nfrags, &ipstat.ips_fragdropped);
1805			ip_freef(ipq[i].next);
1806		}
1807	}
1808	lck_mtx_unlock(ip_mutex);
1809	in_rtqdrain();
1810}
1811
1812/*
1813 * Do option processing on a datagram,
1814 * possibly discarding it if bad options are encountered,
1815 * or forwarding it if source-routed.
1816 * The pass argument is used when operating in the IPSTEALTH
1817 * mode to tell what options to process:
1818 * [LS]SRR (pass 0) or the others (pass 1).
1819 * The reason for as many as two passes is that when doing IPSTEALTH,
1820 * non-routing options should be processed only if the packet is for us.
1821 * Returns 1 if packet has been forwarded/freed,
1822 * 0 if the packet should be processed further.
1823 */
1824static int
1825ip_dooptions(struct mbuf *m, __unused int pass, struct sockaddr_in *next_hop)
1826{
1827	struct ip *ip = mtod(m, struct ip *);
1828	u_char *cp;
1829	struct ip_timestamp *ipt;
1830	struct in_ifaddr *ia;
1831	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1832	struct in_addr *sin, dst;
1833	n_time ntime;
1834	struct sockaddr_in ipaddr = {
1835	    sizeof (ipaddr), AF_INET , 0 , { 0 }, { 0, } };
1836
1837	/* Expect 32-bit aligned data pointer on strict-align platforms */
1838	MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
1839
1840	dst = ip->ip_dst;
1841	cp = (u_char *)(ip + 1);
1842	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1843	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1844		opt = cp[IPOPT_OPTVAL];
1845		if (opt == IPOPT_EOL)
1846			break;
1847		if (opt == IPOPT_NOP)
1848			optlen = 1;
1849		else {
1850			if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1851				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1852				goto bad;
1853			}
1854			optlen = cp[IPOPT_OLEN];
1855			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1856				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1857				goto bad;
1858			}
1859		}
1860		switch (opt) {
1861
1862		default:
1863			break;
1864
1865		/*
1866		 * Source routing with record.
1867		 * Find interface with current destination address.
1868		 * If none on this machine then drop if strictly routed,
1869		 * or do nothing if loosely routed.
1870		 * Record interface address and bring up next address
1871		 * component.  If strictly routed make sure next
1872		 * address is on directly accessible net.
1873		 */
1874		case IPOPT_LSRR:
1875		case IPOPT_SSRR:
1876			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1877				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1878				goto bad;
1879			}
1880			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1881				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1882				goto bad;
1883			}
1884			ipaddr.sin_addr = ip->ip_dst;
1885			ia = (struct in_ifaddr *)
1886				ifa_ifwithaddr((struct sockaddr *)&ipaddr);
1887			if (ia == 0) {
1888				if (opt == IPOPT_SSRR) {
1889					type = ICMP_UNREACH;
1890					code = ICMP_UNREACH_SRCFAIL;
1891					goto bad;
1892				}
1893				if (!ip_dosourceroute)
1894					goto nosourcerouting;
1895				/*
1896				 * Loose routing, and not at next destination
1897				 * yet; nothing to do except forward.
1898				 */
1899				break;
1900			}
1901			else {
1902				IFA_REMREF(&ia->ia_ifa);
1903				ia = NULL;
1904			}
1905			off--;			/* 0 origin */
1906			if (off > optlen - (int)sizeof(struct in_addr)) {
1907				/*
1908				 * End of source route.  Should be for us.
1909				 */
1910				if (!ip_acceptsourceroute)
1911					goto nosourcerouting;
1912				save_rte(cp, ip->ip_src);
1913				break;
1914			}
1915
1916			if (!ip_dosourceroute) {
1917				if (ipforwarding) {
1918					char buf[MAX_IPv4_STR_LEN];
1919					char buf2[MAX_IPv4_STR_LEN];
1920					/*
1921					 * Acting as a router, so generate ICMP
1922					 */
1923nosourcerouting:
1924					log(LOG_WARNING,
1925					    "attempted source route from %s to %s\n",
1926					    inet_ntop(AF_INET, &ip->ip_src, buf, sizeof(buf)),
1927					    inet_ntop(AF_INET, &ip->ip_dst, buf2, sizeof(buf2)));
1928					type = ICMP_UNREACH;
1929					code = ICMP_UNREACH_SRCFAIL;
1930					goto bad;
1931				} else {
1932					/*
1933					 * Not acting as a router, so silently drop.
1934					 */
1935					OSAddAtomic(1, &ipstat.ips_cantforward);
1936					m_freem(m);
1937					return (1);
1938				}
1939			}
1940
1941			/*
1942			 * locate outgoing interface
1943			 */
1944			(void)memcpy(&ipaddr.sin_addr, cp + off,
1945			    sizeof(ipaddr.sin_addr));
1946
1947			if (opt == IPOPT_SSRR) {
1948#define	INA	struct in_ifaddr *
1949				if ((ia = (INA)ifa_ifwithdstaddr(
1950				    (struct sockaddr *)&ipaddr)) == 0) {
1951					ia = (INA)ifa_ifwithnet(
1952					    (struct sockaddr *)&ipaddr);
1953				}
1954			} else {
1955				ia = ip_rtaddr(ipaddr.sin_addr);
1956			}
1957			if (ia == 0) {
1958				type = ICMP_UNREACH;
1959				code = ICMP_UNREACH_SRCFAIL;
1960				goto bad;
1961			}
1962			ip->ip_dst = ipaddr.sin_addr;
1963			IFA_LOCK(&ia->ia_ifa);
1964			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1965			    sizeof(struct in_addr));
1966			IFA_UNLOCK(&ia->ia_ifa);
1967			IFA_REMREF(&ia->ia_ifa);
1968			ia = NULL;
1969			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1970			/*
1971			 * Let ip_intr's mcast routing check handle mcast pkts
1972			 */
1973			forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1974			break;
1975
1976		case IPOPT_RR:
1977			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1978				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1979				goto bad;
1980			}
1981			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1982				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1983				goto bad;
1984			}
1985			/*
1986			 * If no space remains, ignore.
1987			 */
1988			off--;			/* 0 origin */
1989			if (off > optlen - (int)sizeof(struct in_addr))
1990				break;
1991			(void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1992			    sizeof(ipaddr.sin_addr));
1993			/*
1994			 * locate outgoing interface; if we're the destination,
1995			 * use the incoming interface (should be same).
1996			 */
1997			if ((ia = (INA)ifa_ifwithaddr((struct sockaddr *)
1998			    &ipaddr)) == 0) {
1999				if ((ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
2000					type = ICMP_UNREACH;
2001					code = ICMP_UNREACH_HOST;
2002					goto bad;
2003				}
2004			}
2005			IFA_LOCK(&ia->ia_ifa);
2006			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
2007			    sizeof(struct in_addr));
2008			IFA_UNLOCK(&ia->ia_ifa);
2009			IFA_REMREF(&ia->ia_ifa);
2010			ia = NULL;
2011			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
2012			break;
2013
2014		case IPOPT_TS:
2015			code = cp - (u_char *)ip;
2016			ipt = (struct ip_timestamp *)(void *)cp;
2017			if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
2018				code = (u_char *)&ipt->ipt_len - (u_char *)ip;
2019				goto bad;
2020			}
2021			if (ipt->ipt_ptr < 5) {
2022				code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
2023				goto bad;
2024			}
2025			if (ipt->ipt_ptr >
2026			    ipt->ipt_len - (int)sizeof(int32_t)) {
2027				if (++ipt->ipt_oflw == 0) {
2028					code = (u_char *)&ipt->ipt_ptr -
2029					    (u_char *)ip;
2030					goto bad;
2031				}
2032				break;
2033			}
2034			sin = (struct in_addr *)(void *)(cp + ipt->ipt_ptr - 1);
2035			switch (ipt->ipt_flg) {
2036
2037			case IPOPT_TS_TSONLY:
2038				break;
2039
2040			case IPOPT_TS_TSANDADDR:
2041				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
2042				    sizeof(struct in_addr) > ipt->ipt_len) {
2043					code = (u_char *)&ipt->ipt_ptr -
2044					    (u_char *)ip;
2045					goto bad;
2046				}
2047				ipaddr.sin_addr = dst;
2048				ia = (INA)ifaof_ifpforaddr((struct sockaddr *)
2049				    &ipaddr, m->m_pkthdr.rcvif);
2050				if (ia == 0)
2051					continue;
2052				IFA_LOCK(&ia->ia_ifa);
2053				(void)memcpy(sin, &IA_SIN(ia)->sin_addr,
2054				    sizeof(struct in_addr));
2055				IFA_UNLOCK(&ia->ia_ifa);
2056				ipt->ipt_ptr += sizeof(struct in_addr);
2057				IFA_REMREF(&ia->ia_ifa);
2058				ia = NULL;
2059				break;
2060
2061			case IPOPT_TS_PRESPEC:
2062				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
2063				    sizeof(struct in_addr) > ipt->ipt_len) {
2064					code = (u_char *)&ipt->ipt_ptr -
2065					    (u_char *)ip;
2066					goto bad;
2067				}
2068				(void)memcpy(&ipaddr.sin_addr, sin,
2069				    sizeof(struct in_addr));
2070				if ((ia = (struct in_ifaddr*)ifa_ifwithaddr(
2071				    (struct sockaddr *)&ipaddr)) == 0)
2072					continue;
2073				IFA_REMREF(&ia->ia_ifa);
2074				ia = NULL;
2075				ipt->ipt_ptr += sizeof(struct in_addr);
2076				break;
2077
2078			default:
2079				/* XXX can't take &ipt->ipt_flg */
2080				code = (u_char *)&ipt->ipt_ptr -
2081				    (u_char *)ip + 1;
2082				goto bad;
2083			}
2084			ntime = iptime();
2085			(void)memcpy(cp + ipt->ipt_ptr - 1, &ntime,
2086			    sizeof(n_time));
2087			ipt->ipt_ptr += sizeof(n_time);
2088		}
2089	}
2090	if (forward && ipforwarding) {
2091		ip_forward(m, 1, next_hop);
2092		return (1);
2093	}
2094	return (0);
2095bad:
2096	ip->ip_len -= IP_VHL_HL(ip->ip_vhl) << 2;   /* XXX icmp_error adds in hdr length */
2097	icmp_error(m, type, code, 0, 0);
2098	OSAddAtomic(1, &ipstat.ips_badoptions);
2099	return (1);
2100}
2101
2102/*
2103 * Given address of next destination (final or next hop),
2104 * return internet address info of interface to be used to get there.
2105 */
2106struct in_ifaddr *
2107ip_rtaddr(struct in_addr dst)
2108{
2109	struct sockaddr_in *sin;
2110	struct ifaddr *rt_ifa;
2111	struct route ro;
2112
2113	bzero(&ro, sizeof (ro));
2114	sin = (struct sockaddr_in *)(void *)&ro.ro_dst;
2115	sin->sin_family = AF_INET;
2116	sin->sin_len = sizeof (*sin);
2117	sin->sin_addr = dst;
2118
2119	rtalloc_ign(&ro, RTF_PRCLONING);
2120	if (ro.ro_rt == NULL)
2121		return (NULL);
2122
2123	RT_LOCK(ro.ro_rt);
2124	if ((rt_ifa = ro.ro_rt->rt_ifa) != NULL)
2125		IFA_ADDREF(rt_ifa);
2126	RT_UNLOCK(ro.ro_rt);
2127	rtfree(ro.ro_rt);
2128
2129	return ((struct in_ifaddr *)rt_ifa);
2130}
2131
2132/*
2133 * Save incoming source route for use in replies,
2134 * to be picked up later by ip_srcroute if the receiver is interested.
2135 */
2136void
2137save_rte(u_char *option, struct in_addr dst)
2138{
2139	unsigned olen;
2140
2141	olen = option[IPOPT_OLEN];
2142#if DIAGNOSTIC
2143	if (ipprintfs)
2144		printf("save_rte: olen %d\n", olen);
2145#endif
2146	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
2147		return;
2148	bcopy(option, ip_srcrt.srcopt, olen);
2149	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
2150	ip_srcrt.dst = dst;
2151}
2152
2153/*
2154 * Retrieve incoming source route for use in replies,
2155 * in the same form used by setsockopt.
2156 * The first hop is placed before the options, will be removed later.
2157 */
2158struct mbuf *
2159ip_srcroute(void)
2160{
2161	struct in_addr *p, *q;
2162	struct mbuf *m;
2163
2164	if (ip_nhops == 0)
2165		return ((struct mbuf *)0);
2166	m = m_get(M_DONTWAIT, MT_HEADER);
2167	if (m == 0)
2168		return ((struct mbuf *)0);
2169
2170#define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
2171
2172	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
2173	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
2174	    OPTSIZ;
2175#if DIAGNOSTIC
2176	if (ipprintfs)
2177		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
2178#endif
2179
2180	/*
2181	 * First save first hop for return route
2182	 */
2183	p = &ip_srcrt.route[ip_nhops - 1];
2184	*(mtod(m, struct in_addr *)) = *p--;
2185#if DIAGNOSTIC
2186	if (ipprintfs)
2187		printf(" hops %lx", (u_int32_t)ntohl(mtod(m, struct in_addr *)->s_addr));
2188#endif
2189
2190	/*
2191	 * Copy option fields and padding (nop) to mbuf.
2192	 */
2193	ip_srcrt.nop = IPOPT_NOP;
2194	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
2195	(void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
2196	    &ip_srcrt.nop, OPTSIZ);
2197	q = (struct in_addr *)(void *)(mtod(m, caddr_t) +
2198	    sizeof(struct in_addr) + OPTSIZ);
2199#undef OPTSIZ
2200	/*
2201	 * Record return path as an IP source route,
2202	 * reversing the path (pointers are now aligned).
2203	 */
2204	while (p >= ip_srcrt.route) {
2205#if DIAGNOSTIC
2206		if (ipprintfs)
2207			printf(" %lx", (u_int32_t)ntohl(q->s_addr));
2208#endif
2209		*q++ = *p--;
2210	}
2211	/*
2212	 * Last hop goes to final destination.
2213	 */
2214	*q = ip_srcrt.dst;
2215#if DIAGNOSTIC
2216	if (ipprintfs)
2217		printf(" %lx\n", (u_int32_t)ntohl(q->s_addr));
2218#endif
2219	return (m);
2220}
2221
2222/*
2223 * Strip out IP options, at higher
2224 * level protocol in the kernel.
2225 * Second argument is buffer to which options
2226 * will be moved, and return value is their length.
2227 * XXX should be deleted; last arg currently ignored.
2228 */
2229void
2230ip_stripoptions(struct mbuf *m, __unused struct mbuf *mopt)
2231{
2232	int i;
2233	struct ip *ip = mtod(m, struct ip *);
2234	caddr_t opts;
2235	int olen;
2236
2237	/* Expect 32-bit aligned data pointer on strict-align platforms */
2238	MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
2239
2240	olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
2241	opts = (caddr_t)(ip + 1);
2242	i = m->m_len - (sizeof (struct ip) + olen);
2243	bcopy(opts + olen, opts, (unsigned)i);
2244	m->m_len -= olen;
2245	if (m->m_flags & M_PKTHDR)
2246		m->m_pkthdr.len -= olen;
2247	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
2248}
2249
2250u_char inetctlerrmap[PRC_NCMDS] = {
2251	0,		0,		0,		0,
2252	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
2253	ENETUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
2254	EMSGSIZE,	EHOSTUNREACH,	0,		0,
2255	0,		0,		0,		0,
2256	ENOPROTOOPT,	ECONNREFUSED
2257};
2258
2259static int
2260sysctl_ipforwarding SYSCTL_HANDLER_ARGS
2261{
2262#pragma unused(arg1, arg2)
2263	int i, was_ipforwarding = ipforwarding;
2264
2265	i = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
2266	if (i != 0 || req->newptr == USER_ADDR_NULL)
2267		return (i);
2268
2269	if (was_ipforwarding && !ipforwarding) {
2270		/* clean up IPv4 forwarding cached routes */
2271		ifnet_head_lock_shared();
2272		for (i = 0; i <= if_index; i++) {
2273			struct ifnet *ifp = ifindex2ifnet[i];
2274			if (ifp != NULL) {
2275				lck_mtx_lock(&ifp->if_cached_route_lock);
2276				if (ifp->if_fwd_route.ro_rt != NULL)
2277					rtfree(ifp->if_fwd_route.ro_rt);
2278				bzero(&ifp->if_fwd_route,
2279				    sizeof (ifp->if_fwd_route));
2280				lck_mtx_unlock(&ifp->if_cached_route_lock);
2281			}
2282		}
2283		ifnet_head_done();
2284	}
2285
2286	return (0);
2287}
2288
2289/*
2290 * Similar to inp_route_{copyout,copyin} routines except that these copy
2291 * out the cached IPv4 forwarding route from struct ifnet instead of the
2292 * inpcb.  See comments for those routines for explanations.
2293 */
2294static void
2295ip_fwd_route_copyout(struct ifnet *ifp, struct route *dst)
2296{
2297	struct route *src = &ifp->if_fwd_route;
2298
2299	lck_mtx_lock_spin(&ifp->if_cached_route_lock);
2300	lck_mtx_convert_spin(&ifp->if_cached_route_lock);
2301
2302	/* Minor sanity check */
2303	if (src->ro_rt != NULL && rt_key(src->ro_rt)->sa_family != AF_INET)
2304		panic("%s: wrong or corrupted route: %p", __func__, src);
2305
2306	route_copyout(dst, src, sizeof(*dst));
2307
2308	lck_mtx_unlock(&ifp->if_cached_route_lock);
2309}
2310
2311static void
2312ip_fwd_route_copyin(struct ifnet *ifp, struct route *src)
2313{
2314	struct route *dst = &ifp->if_fwd_route;
2315
2316	lck_mtx_lock_spin(&ifp->if_cached_route_lock);
2317	lck_mtx_convert_spin(&ifp->if_cached_route_lock);
2318
2319	/* Minor sanity check */
2320	if (src->ro_rt != NULL && rt_key(src->ro_rt)->sa_family != AF_INET)
2321		panic("%s: wrong or corrupted route: %p", __func__, src);
2322
2323	if (ifp->if_fwd_cacheok)
2324		route_copyin(src, dst, sizeof(*src));
2325
2326	lck_mtx_unlock(&ifp->if_cached_route_lock);
2327}
2328
2329/*
2330 * Forward a packet.  If some error occurs return the sender
2331 * an icmp packet.  Note we can't always generate a meaningful
2332 * icmp message because icmp doesn't have a large enough repertoire
2333 * of codes and types.
2334 *
2335 * If not forwarding, just drop the packet.  This could be confusing
2336 * if ipforwarding was zero but some routing protocol was advancing
2337 * us as a gateway to somewhere.  However, we must let the routing
2338 * protocol deal with that.
2339 *
2340 * The srcrt parameter indicates whether the packet is being forwarded
2341 * via a source route.
2342 */
2343static void
2344ip_forward(struct mbuf *m, int srcrt, struct sockaddr_in *next_hop)
2345{
2346#if !IPFIREWALL
2347#pragma unused(next_hop)
2348#endif
2349	struct ip *ip = mtod(m, struct ip *);
2350	struct sockaddr_in *sin;
2351	struct rtentry *rt;
2352	struct route fwd_rt;
2353	int error, type = 0, code = 0;
2354	struct mbuf *mcopy;
2355	n_long dest;
2356	struct in_addr pkt_dst;
2357	u_int32_t nextmtu = 0;
2358	struct ip_out_args ipoa = { IFSCOPE_NONE, { 0 }, 0 };
2359	struct ifnet *ifp = m->m_pkthdr.rcvif;
2360#if PF
2361	struct pf_mtag *pf_mtag;
2362#endif /* PF */
2363
2364	dest = 0;
2365#if IPFIREWALL
2366	/*
2367	 * Cache the destination address of the packet; this may be
2368	 * changed by use of 'ipfw fwd'.
2369	 */
2370	pkt_dst = next_hop ? next_hop->sin_addr : ip->ip_dst;
2371#else
2372	pkt_dst = ip->ip_dst;
2373#endif
2374
2375#if DIAGNOSTIC
2376	if (ipprintfs)
2377		printf("forward: src %lx dst %lx ttl %x\n",
2378		    (u_int32_t)ip->ip_src.s_addr, (u_int32_t)pkt_dst.s_addr,
2379		    ip->ip_ttl);
2380#endif
2381
2382	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(pkt_dst) == 0) {
2383		OSAddAtomic(1, &ipstat.ips_cantforward);
2384		m_freem(m);
2385		return;
2386	}
2387#if IPSTEALTH
2388	if (!ipstealth) {
2389#endif
2390		if (ip->ip_ttl <= IPTTLDEC) {
2391			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
2392			    dest, 0);
2393			return;
2394		}
2395#if IPSTEALTH
2396	}
2397#endif
2398
2399#if PF
2400	pf_mtag = pf_find_mtag(m);
2401	if (pf_mtag != NULL && pf_mtag->pftag_rtableid != IFSCOPE_NONE) {
2402		ipoa.ipoa_boundif = pf_mtag->pftag_rtableid;
2403		ipoa.ipoa_flags |= IPOAF_BOUND_IF;
2404	}
2405#endif /* PF */
2406
2407	ip_fwd_route_copyout(ifp, &fwd_rt);
2408
2409	sin = (struct sockaddr_in *)(void *)&fwd_rt.ro_dst;
2410	if (fwd_rt.ro_rt == NULL ||
2411	    fwd_rt.ro_rt->generation_id != route_generation ||
2412	    pkt_dst.s_addr != sin->sin_addr.s_addr) {
2413		if (fwd_rt.ro_rt != NULL) {
2414			rtfree(fwd_rt.ro_rt);
2415			fwd_rt.ro_rt = NULL;
2416		}
2417		sin->sin_family = AF_INET;
2418		sin->sin_len = sizeof (*sin);
2419		sin->sin_addr = pkt_dst;
2420
2421		rtalloc_scoped_ign(&fwd_rt, RTF_PRCLONING, ipoa.ipoa_boundif);
2422		if (fwd_rt.ro_rt == NULL) {
2423			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
2424			goto done;
2425		}
2426	}
2427	rt = fwd_rt.ro_rt;
2428
2429	/*
2430	 * Save the IP header and at most 8 bytes of the payload,
2431	 * in case we need to generate an ICMP message to the src.
2432	 *
2433	 * We don't use m_copy() because it might return a reference
2434	 * to a shared cluster. Both this function and ip_output()
2435	 * assume exclusive access to the IP header in `m', so any
2436	 * data in a cluster may change before we reach icmp_error().
2437	 */
2438	MGET(mcopy, M_DONTWAIT, m->m_type);
2439	if (mcopy != NULL) {
2440		M_COPY_PKTHDR(mcopy, m);
2441		mcopy->m_len = imin((IP_VHL_HL(ip->ip_vhl) << 2) + 8,
2442		    (int)ip->ip_len);
2443		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
2444	}
2445
2446#if IPSTEALTH
2447	if (!ipstealth) {
2448#endif
2449		ip->ip_ttl -= IPTTLDEC;
2450#if IPSTEALTH
2451	}
2452#endif
2453
2454	/*
2455	 * If forwarding packet using same interface that it came in on,
2456	 * perhaps should send a redirect to sender to shortcut a hop.
2457	 * Only send redirect if source is sending directly to us,
2458	 * and if packet was not source routed (or has any options).
2459	 * Also, don't send redirect if forwarding using a default route
2460	 * or a route modified by a redirect.
2461	 */
2462	RT_LOCK_SPIN(rt);
2463	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
2464	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
2465	    satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
2466	    ipsendredirects && !srcrt && rt->rt_ifa != NULL) {
2467		struct in_ifaddr *ia = (struct in_ifaddr *)rt->rt_ifa;
2468		u_int32_t src = ntohl(ip->ip_src.s_addr);
2469
2470		/* Become a regular mutex */
2471		RT_CONVERT_LOCK(rt);
2472		IFA_LOCK_SPIN(&ia->ia_ifa);
2473		if ((src & ia->ia_subnetmask) == ia->ia_subnet) {
2474			if (rt->rt_flags & RTF_GATEWAY)
2475				dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
2476			else
2477				dest = pkt_dst.s_addr;
2478			/* Router requirements says to only send host redirects */
2479			type = ICMP_REDIRECT;
2480			code = ICMP_REDIRECT_HOST;
2481#if DIAGNOSTIC
2482			if (ipprintfs)
2483				printf("redirect (%d) to %lx\n", code, (u_int32_t)dest);
2484#endif
2485		}
2486		IFA_UNLOCK(&ia->ia_ifa);
2487	}
2488	RT_UNLOCK(rt);
2489
2490#if IPFIREWALL
2491	if (next_hop) {
2492		/* Pass IPFORWARD info if available */
2493		struct m_tag *tag;
2494		struct ip_fwd_tag	*ipfwd_tag;
2495
2496		tag = m_tag_create(KERNEL_MODULE_TAG_ID,
2497		    KERNEL_TAG_TYPE_IPFORWARD,
2498		    sizeof (*ipfwd_tag), M_NOWAIT, m);
2499		if (tag == NULL) {
2500			error = ENOBUFS;
2501			m_freem(m);
2502			goto done;
2503		}
2504
2505		ipfwd_tag = (struct ip_fwd_tag *)(tag+1);
2506		ipfwd_tag->next_hop = next_hop;
2507
2508		m_tag_prepend(m, tag);
2509	}
2510#endif
2511	error = ip_output_list(m, 0, NULL, &fwd_rt,
2512	    IP_FORWARDING | IP_OUTARGS, 0, &ipoa);
2513
2514	/* Refresh rt since the route could have changed while in IP */
2515	rt = fwd_rt.ro_rt;
2516
2517	if (error) {
2518		OSAddAtomic(1, &ipstat.ips_cantforward);
2519	} else {
2520		OSAddAtomic(1, &ipstat.ips_forward);
2521		if (type)
2522			OSAddAtomic(1, &ipstat.ips_redirectsent);
2523		else {
2524			if (mcopy) {
2525				/*
2526				 * If we didn't have to go thru ipflow and
2527				 * the packet was successfully consumed by
2528				 * ip_output, the mcopy is rather a waste;
2529				 * this could be further optimized.
2530				 */
2531				m_freem(mcopy);
2532			}
2533			goto done;
2534		}
2535	}
2536	if (mcopy == NULL)
2537		goto done;
2538
2539	switch (error) {
2540
2541	case 0:				/* forwarded, but need redirect */
2542		/* type, code set above */
2543		break;
2544
2545	case ENETUNREACH:		/* shouldn't happen, checked above */
2546	case EHOSTUNREACH:
2547	case ENETDOWN:
2548	case EHOSTDOWN:
2549	default:
2550		type = ICMP_UNREACH;
2551		code = ICMP_UNREACH_HOST;
2552		break;
2553
2554	case EMSGSIZE:
2555		type = ICMP_UNREACH;
2556		code = ICMP_UNREACH_NEEDFRAG;
2557#ifndef IPSEC
2558		if (rt != NULL) {
2559			RT_LOCK_SPIN(rt);
2560			if (rt->rt_ifp != NULL)
2561				nextmtu = rt->rt_ifp->if_mtu;
2562			RT_UNLOCK(rt);
2563		}
2564#else
2565		/*
2566		 * If the packet is routed over IPsec tunnel, tell the
2567		 * originator the tunnel MTU.
2568		 *	tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
2569		 * XXX quickhack!!!
2570		 */
2571		if (rt != NULL) {
2572			struct secpolicy *sp = NULL;
2573			int ipsecerror;
2574			int ipsechdr;
2575			struct route *ro;
2576
2577			RT_LOCK_SPIN(rt);
2578			if (rt->rt_ifp != NULL)
2579				nextmtu = rt->rt_ifp->if_mtu;
2580			RT_UNLOCK(rt);
2581
2582			if (ipsec_bypass) {
2583				OSAddAtomic(1, &ipstat.ips_cantfrag);
2584				break;
2585			}
2586			sp = ipsec4_getpolicybyaddr(mcopy,
2587						    IPSEC_DIR_OUTBOUND,
2588			                            IP_FORWARDING,
2589			                            &ipsecerror);
2590
2591			if (sp != NULL) {
2592				/* count IPsec header size */
2593				ipsechdr = ipsec_hdrsiz(sp);
2594
2595				/*
2596				 * find the correct route for outer IPv4
2597				 * header, compute tunnel MTU.
2598				 */
2599				nextmtu = 0;
2600
2601				if (sp->req != NULL) {
2602					if (sp->req->saidx.mode == IPSEC_MODE_TUNNEL) {
2603						struct secasindex saidx;
2604						struct ip *ipm;
2605						struct secasvar *sav;
2606
2607						ipm = mtod(mcopy, struct ip *);
2608						bcopy(&sp->req->saidx, &saidx, sizeof(saidx));
2609						saidx.mode = sp->req->saidx.mode;
2610						saidx.reqid = sp->req->saidx.reqid;
2611						sin = (struct sockaddr_in *)&saidx.src;
2612						if (sin->sin_len == 0) {
2613							sin->sin_len = sizeof(*sin);
2614							sin->sin_family = AF_INET;
2615							sin->sin_port = IPSEC_PORT_ANY;
2616							bcopy(&ipm->ip_src, &sin->sin_addr,
2617								sizeof(sin->sin_addr));
2618						}
2619						sin = (struct sockaddr_in *)&saidx.dst;
2620						if (sin->sin_len == 0) {
2621							sin->sin_len = sizeof(*sin);
2622							sin->sin_family = AF_INET;
2623							sin->sin_port = IPSEC_PORT_ANY;
2624							bcopy(&ipm->ip_dst, &sin->sin_addr,
2625								sizeof(sin->sin_addr));
2626						}
2627						sav = key_allocsa_policy(&saidx);
2628						if (sav != NULL) {
2629							lck_mtx_lock(sadb_mutex);
2630							if (sav->sah != NULL) {
2631								ro = &sav->sah->sa_route;
2632								if (ro->ro_rt != NULL) {
2633									RT_LOCK(ro->ro_rt);
2634									if (ro->ro_rt->rt_ifp != NULL) {
2635										nextmtu = ro->ro_rt->rt_ifp->if_mtu;
2636										nextmtu -= ipsechdr;
2637									}
2638									RT_UNLOCK(ro->ro_rt);
2639								}
2640							}
2641							key_freesav(sav, KEY_SADB_LOCKED);
2642							lck_mtx_unlock(sadb_mutex);
2643						}
2644					}
2645				}
2646				key_freesp(sp, KEY_SADB_UNLOCKED);
2647			}
2648		}
2649#endif /*IPSEC*/
2650		OSAddAtomic(1, &ipstat.ips_cantfrag);
2651		break;
2652
2653	case ENOBUFS:
2654		type = ICMP_SOURCEQUENCH;
2655		code = 0;
2656		break;
2657
2658	case EACCES:			/* ipfw denied packet */
2659		m_freem(mcopy);
2660		goto done;
2661	}
2662
2663	icmp_error(mcopy, type, code, dest, nextmtu);
2664done:
2665	ip_fwd_route_copyin(ifp, &fwd_rt);
2666}
2667
2668int
2669ip_savecontrol(
2670	struct inpcb *inp,
2671	struct mbuf **mp,
2672	struct ip *ip,
2673	struct mbuf *m)
2674{
2675	*mp = NULL;
2676	if (inp->inp_socket->so_options & SO_TIMESTAMP) {
2677		struct timeval tv;
2678
2679		microtime(&tv);
2680		mp = sbcreatecontrol_mbuf((caddr_t) &tv, sizeof(tv),
2681			SCM_TIMESTAMP, SOL_SOCKET, mp);
2682		if (*mp == NULL) {
2683			goto no_mbufs;
2684		}
2685	}
2686	if ((inp->inp_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) {
2687		uint64_t time;
2688
2689		time = mach_absolute_time();
2690		mp = sbcreatecontrol_mbuf((caddr_t) &time, sizeof(time),
2691			SCM_TIMESTAMP_MONOTONIC, SOL_SOCKET, mp);
2692
2693		if (*mp == NULL) {
2694			goto no_mbufs;
2695		}
2696	}
2697	if (inp->inp_flags & INP_RECVDSTADDR) {
2698		mp = sbcreatecontrol_mbuf((caddr_t) &ip->ip_dst,
2699			sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP, mp);
2700		if (*mp == NULL) {
2701			goto no_mbufs;
2702		}
2703	}
2704#ifdef notyet
2705	/* XXX
2706	 * Moving these out of udp_input() made them even more broken
2707	 * than they already were.
2708	 */
2709	/* options were tossed already */
2710	if (inp->inp_flags & INP_RECVOPTS) {
2711		mp = sbcreatecontrol_mbuf((caddr_t) opts_deleted_above,
2712			sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP, mp);
2713		if (*mp == NULL) {
2714			goto no_mbufs;
2715		}
2716	}
2717	/* ip_srcroute doesn't do what we want here, need to fix */
2718	if (inp->inp_flags & INP_RECVRETOPTS) {
2719		mp = sbcreatecontrol_mbuf((caddr_t) ip_srcroute(),
2720			sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP, mp);
2721		if (*mp == NULL) {
2722			goto no_mbufs;
2723		}
2724	}
2725#endif
2726	if (inp->inp_flags & INP_RECVIF) {
2727		struct ifnet *ifp;
2728		struct sdlbuf {
2729			struct sockaddr_dl sdl;
2730			u_char	pad[32];
2731		} sdlbuf;
2732		struct sockaddr_dl *sdp;
2733		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
2734
2735		ifnet_head_lock_shared();
2736		if ((ifp = m->m_pkthdr.rcvif) != NULL &&
2737		    ifp->if_index && (ifp->if_index <= if_index)) {
2738			struct ifaddr *ifa = ifnet_addrs[ifp->if_index - 1];
2739
2740			if (!ifa || !ifa->ifa_addr)
2741				goto makedummy;
2742
2743			IFA_LOCK_SPIN(ifa);
2744			sdp = (struct sockaddr_dl *)(void *)ifa->ifa_addr;
2745			/*
2746			 * Change our mind and don't try copy.
2747			 */
2748			if ((sdp->sdl_family != AF_LINK) ||
2749			    (sdp->sdl_len > sizeof(sdlbuf))) {
2750				IFA_UNLOCK(ifa);
2751				goto makedummy;
2752			}
2753			bcopy(sdp, sdl2, sdp->sdl_len);
2754			IFA_UNLOCK(ifa);
2755		} else {
2756makedummy:
2757			sdl2->sdl_len
2758				= offsetof(struct sockaddr_dl, sdl_data[0]);
2759			sdl2->sdl_family = AF_LINK;
2760			sdl2->sdl_index = 0;
2761			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
2762		}
2763		ifnet_head_done();
2764		mp = sbcreatecontrol_mbuf((caddr_t) sdl2, sdl2->sdl_len,
2765			IP_RECVIF, IPPROTO_IP, mp);
2766		if (*mp == NULL) {
2767			goto no_mbufs;
2768		}
2769	}
2770	if (inp->inp_flags & INP_RECVTTL) {
2771		mp = sbcreatecontrol_mbuf((caddr_t)&ip->ip_ttl, sizeof(ip->ip_ttl),
2772			IP_RECVTTL, IPPROTO_IP, mp);
2773		if (*mp == NULL) {
2774			goto no_mbufs;
2775		}
2776	}
2777	if ((inp->inp_socket->so_flags & SOF_RECV_TRAFFIC_CLASS) != 0) {
2778		int tc = m_get_traffic_class(m);
2779
2780		mp = sbcreatecontrol_mbuf((caddr_t) &tc, sizeof(tc),
2781			SO_TRAFFIC_CLASS, SOL_SOCKET, mp);
2782		if (*mp == NULL) {
2783			goto no_mbufs;
2784		}
2785	}
2786	if (inp->inp_flags & INP_PKTINFO) {
2787		struct in_pktinfo pi;
2788
2789		bzero(&pi, sizeof(struct in_pktinfo));
2790		bcopy(&ip->ip_dst, &pi.ipi_addr, sizeof(struct in_addr));
2791		pi.ipi_ifindex = (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
2792
2793		mp = sbcreatecontrol_mbuf((caddr_t)&pi, sizeof(struct in_pktinfo),
2794			IP_RECVPKTINFO, IPPROTO_IP, mp);
2795		if (*mp == NULL) {
2796			goto no_mbufs;
2797		}
2798	}
2799	return 0;
2800
2801no_mbufs:
2802	ipstat.ips_pktdropcntrl++;
2803	return ENOBUFS;
2804}
2805
2806int
2807ip_rsvp_init(struct socket *so)
2808{
2809	if (so->so_type != SOCK_RAW ||
2810	    so->so_proto->pr_protocol != IPPROTO_RSVP)
2811	  return EOPNOTSUPP;
2812
2813	if (ip_rsvpd != NULL)
2814	  return EADDRINUSE;
2815
2816	ip_rsvpd = so;
2817	/*
2818	 * This may seem silly, but we need to be sure we don't over-increment
2819	 * the RSVP counter, in case something slips up.
2820	 */
2821	if (!ip_rsvp_on) {
2822		ip_rsvp_on = 1;
2823		rsvp_on++;
2824	}
2825
2826	return 0;
2827}
2828
2829int
2830ip_rsvp_done(void)
2831{
2832	ip_rsvpd = NULL;
2833	/*
2834	 * This may seem silly, but we need to be sure we don't over-decrement
2835	 * the RSVP counter, in case something slips up.
2836	 */
2837	if (ip_rsvp_on) {
2838		ip_rsvp_on = 0;
2839		rsvp_on--;
2840	}
2841	return 0;
2842}
2843
2844static inline u_short
2845ip_cksum(struct mbuf *m, int hlen)
2846{
2847
2848	u_short sum;
2849	struct ip *ip;
2850
2851	ip = mtod(m, struct ip *);
2852
2853	if ((IF_HWASSIST_CSUM_FLAGS(m->m_pkthdr.rcvif->if_hwassist) == 0)
2854		    || (apple_hwcksum_rx == 0) ||
2855		   ((m->m_pkthdr.csum_flags & CSUM_TCP_SUM16) && ip->ip_p != IPPROTO_TCP)) {
2856		m->m_pkthdr.csum_flags = 0; /* invalidate HW generated checksum flags */
2857
2858	}
2859
2860	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
2861		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
2862	} else if (!(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
2863		    apple_hwcksum_tx == 0) {
2864		/*
2865		 * Either this is not loopback packet coming from an interface
2866		 * that does not support checksum offloading, or it is loopback
2867		 * packet that has undergone software checksumming at the send
2868		 * side because apple_hwcksum_tx was set to 0.  In this case,
2869		 * calculate the checksum in software to validate the packet.
2870		 */
2871		sum = in_cksum(m, hlen);
2872	} else {
2873		/*
2874		 * This is a loopback packet without any valid checksum since
2875		 * the send side has bypassed it (apple_hwcksum_tx set to 1).
2876		 * We get here because apple_hwcksum_rx was set to 0, and so
2877		 * we pretend that all is well.
2878		 */
2879		sum = 0;
2880		m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR |
2881			    CSUM_IP_CHECKED | CSUM_IP_VALID;
2882	    m->m_pkthdr.csum_data = 0xffff;
2883	}
2884
2885	if (sum) {
2886		OSAddAtomic(1, &ipstat.ips_badsum);
2887	}
2888
2889	return sum;
2890}
2891