2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1988, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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
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 $
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,
72 #include <sys/param.h>
73 #include <sys/systm.h>
75 #include <sys/malloc.h>
76 #include <sys/domain.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.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>
86 #include <machine/endian.h>
88 #include <kern/queue.h>
89 #include <kern/locks.h>
91 #include <pexpert/pexpert.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>
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>
110 #include <netinet/ip_fw.h>
111 #include <netinet/ip_divert.h>
113 #include <netinet/kpi_ipfilter_var.h>
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>
122 #include <security/mac_framework.h>
125 #include <sys/kdebug.h>
126 #include <libkern/OSAtomic.h>
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))
134 #include <netinet6/ipsec.h>
135 #include <netkey/key.h>
139 #include <netinet/ip_dummynet.h>
143 #include <net/pfvar.h>
146 #include <netinet/lro_ext.h>
149 extern int ipsec_bypass
;
150 extern lck_mtx_t
*sadb_mutex
;
152 lck_grp_t
*sadb_stat_mutex_grp
;
153 lck_grp_attr_t
*sadb_stat_mutex_grp_attr
;
154 lck_attr_t
*sadb_stat_mutex_attr
;
155 decl_lck_mtx_data(, sadb_stat_mutex_data
);
156 lck_mtx_t
*sadb_stat_mutex
= &sadb_stat_mutex_data
;
161 static int ip_rsvp_on
;
162 struct socket
*ip_rsvpd
;
164 static int sysctl_ipforwarding SYSCTL_HANDLER_ARGS
;
166 int ipforwarding
= 0;
167 SYSCTL_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");
171 static int ipsendredirects
= 1; /* XXX */
172 SYSCTL_INT(_net_inet_ip
, IPCTL_SENDREDIRECTS
, redirect
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
173 &ipsendredirects
, 0, "Enable sending IP redirects");
175 int ip_defttl
= IPDEFTTL
;
176 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFTTL
, ttl
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
177 &ip_defttl
, 0, "Maximum TTL on IP packets");
179 static int ip_dosourceroute
= 0;
180 SYSCTL_INT(_net_inet_ip
, IPCTL_SOURCEROUTE
, sourceroute
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
181 &ip_dosourceroute
, 0, "Enable forwarding source routed IP packets");
183 static int ip_acceptsourceroute
= 0;
184 SYSCTL_INT(_net_inet_ip
, IPCTL_ACCEPTSOURCEROUTE
, accept_sourceroute
,
185 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_acceptsourceroute
, 0,
186 "Enable accepting source routed IP packets");
188 static int ip_keepfaith
= 0;
189 SYSCTL_INT(_net_inet_ip
, IPCTL_KEEPFAITH
, keepfaith
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
190 &ip_keepfaith
, 0, "");
192 static int nipq
= 0; /* total # of reass queues */
194 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfragpackets
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
196 "Maximum number of IPv4 fragment reassembly queue entries");
198 static int maxfragsperpacket
;
199 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfragsperpacket
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
200 &maxfragsperpacket
, 0,
201 "Maximum number of IPv4 fragments allowed per packet");
204 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfrags
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
205 &maxfrags
, 0, "Maximum number of IPv4 fragments allowed");
207 static int currentfrags
= 0;
209 int ip_doscopedroute
= 1;
210 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, scopedroute
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
211 &ip_doscopedroute
, 0, "Enable IPv4 scoped routing");
213 int ip_restrictrecvif
= 1;
214 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, restrictrecvif
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
215 &ip_restrictrecvif
, 0, "Enable inbound interface restrictions");
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.
223 * XXX - ip_checkinterface currently must be disabled if you use ipnat
224 * to translate the destination address to another local interface.
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.
230 static int ip_checkinterface
= 0;
231 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, check_interface
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
232 &ip_checkinterface
, 0, "Verify packet arrives on correct interface");
235 static int ipprintfs
= 0;
238 extern int in_proto_count
;
239 extern struct domain inetdomain
;
240 extern struct protosw inetsw
[];
241 struct protosw
*ip_protox
[IPPROTO_MAX
];
242 static int ipqmaxlen
= IFQ_MAXLEN
;
244 static lck_grp_attr_t
*in_ifaddr_rwlock_grp_attr
;
245 static lck_grp_t
*in_ifaddr_rwlock_grp
;
246 static lck_attr_t
*in_ifaddr_rwlock_attr
;
247 decl_lck_rw_data(, in_ifaddr_rwlock_data
);
248 lck_rw_t
*in_ifaddr_rwlock
= &in_ifaddr_rwlock_data
;
250 /* Protected by in_ifaddr_rwlock */
251 struct in_ifaddrhead in_ifaddrhead
; /* first inet address */
252 struct in_ifaddrhashhead
*in_ifaddrhashtbl
; /* inet addr hash table */
254 #define INADDR_NHASH 61
255 static u_int32_t inaddr_nhash
; /* hash table size */
256 static u_int32_t inaddr_hashp
; /* next largest prime */
258 struct ifqueue ipintrq
;
259 SYSCTL_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");
261 SYSCTL_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");
264 struct ipstat ipstat
;
265 SYSCTL_STRUCT(_net_inet_ip
, IPCTL_STATS
, stats
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
266 &ipstat
, ipstat
, "IP statistics (struct ipstat, netinet/ip_var.h)");
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)
275 static struct ipq ipq
[IPREASS_NHASH
];
276 static TAILQ_HEAD(ipq_list
, ipq
) ipq_list
=
277 TAILQ_HEAD_INITIALIZER(ipq_list
);
278 const int ipintrq_present
= 1;
280 lck_attr_t
*ip_mutex_attr
;
281 lck_grp_t
*ip_mutex_grp
;
282 lck_grp_attr_t
*ip_mutex_grp_attr
;
283 lck_mtx_t
*inet_domain_mutex
;
286 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFMTU
, mtu
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
287 &ip_mtu
, 0, "Default MTU");
291 static int ipstealth
= 0;
292 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, stealth
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
299 ip_fw_chk_t
*ip_fw_chk_ptr
;
303 #endif /* IPFIREWALL */
306 ip_dn_io_t
*ip_dn_io_ptr
;
309 SYSCTL_NODE(_net_inet_ip
, OID_AUTO
, linklocal
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "link local");
311 struct ip_linklocal_stat ip_linklocal_stat
;
312 SYSCTL_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");
316 SYSCTL_NODE(_net_inet_ip_linklocal
, OID_AUTO
, in
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "link local input");
318 int ip_linklocal_in_allowbadttl
= 1;
319 SYSCTL_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");
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
331 static int ip_nhops
= 0;
332 static 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
)];
339 static void in_ifaddrhashtbl_init(void);
340 static void save_rte(u_char
*, struct in_addr
);
341 static int ip_dooptions(struct mbuf
*, int, struct sockaddr_in
*);
342 static void ip_forward(struct mbuf
*, int, struct sockaddr_in
*);
343 static void ip_freef(struct ipq
*);
346 static struct mbuf
*ip_reass(struct mbuf
*,
347 struct ipq
*, struct ipq
*, u_int32_t
*, u_int16_t
*);
349 static struct mbuf
*ip_reass(struct mbuf
*,
350 struct ipq
*, struct ipq
*, u_int16_t
*, u_int16_t
*);
353 static struct mbuf
*ip_reass(struct mbuf
*, struct ipq
*, struct ipq
*);
355 static void ip_fwd_route_copyout(struct ifnet
*, struct route
*);
356 static void ip_fwd_route_copyin(struct ifnet
*, struct route
*);
359 static inline u_short
ip_cksum(struct mbuf
*, int);
362 extern u_short ip_id
;
364 int ip_use_randomid
= 1;
365 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, random_id
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
366 &ip_use_randomid
, 0, "Randomize IP packets IDs");
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.
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))) { \
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); \
389 atomic_add_32(&ipstat.ips_toosmall, 1); \
394 VERIFY(_n != (_m)); \
399 #endif /* !__i386__ && !__x86_64__ */
402 * IP initialization: fill in IP protocol switch table.
403 * All protocols not implemented in kernel go to raw IP protocol handler.
410 static int ip_initialized
= 0;
414 PE_parse_boot_argn("net.inet.ip.scopedroute",
415 &ip_doscopedroute
, sizeof (ip_doscopedroute
));
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
);
426 TAILQ_INIT(&in_ifaddrhead
);
427 in_ifaddrhashtbl_init();
431 pr
= pffindproto_locked(PF_INET
, IPPROTO_RAW
, SOCK_RAW
);
434 for (i
= 0; i
< IPPROTO_MAX
; i
++)
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
;
443 for (i
= 0; i
< IPREASS_NHASH
; i
++)
444 ipq
[i
].next
= ipq
[i
].prev
= &ipq
[i
];
446 maxnipq
= nmbclusters
/ 32;
447 maxfrags
= maxnipq
* 2;
448 maxfragsperpacket
= 128; /* enough for 64k in 512 byte fragments */
452 struct timeval timenow
;
453 getmicrotime(&timenow
);
454 ip_id
= timenow
.tv_sec
& 0xffff;
457 ipintrq
.ifq_maxlen
= ipqmaxlen
;
461 ip_mutex_grp_attr
= lck_grp_attr_alloc_init();
463 ip_mutex_grp
= lck_grp_alloc_init("ip", ip_mutex_grp_attr
);
465 ip_mutex_attr
= lck_attr_alloc_init();
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");
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();
478 lck_mtx_init(sadb_stat_mutex
, sadb_stat_mutex_grp
, sadb_stat_mutex_attr
);
488 * Initialize IPv4 source address hash table.
491 in_ifaddrhashtbl_init(void)
495 if (in_ifaddrhashtbl
!= NULL
)
498 PE_parse_boot_argn("inaddr_nhash", &inaddr_nhash
, sizeof (inaddr_nhash
));
499 if (inaddr_nhash
== 0)
500 inaddr_nhash
= INADDR_NHASH
;
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");
509 * Generate the next largest prime greater than inaddr_nhash.
511 k
= (inaddr_nhash
% 2 == 0) ? inaddr_nhash
+ 1 : inaddr_nhash
+ 2;
514 for (i
= 3; i
* i
<= k
; i
+= 2) {
526 inaddr_hashval(u_int32_t key
)
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).
533 if (inaddr_nhash
> 1)
534 return ((key
* inaddr_hashp
) % inaddr_nhash
);
541 protocol_family_t __unused protocol
,
547 /* ip_input should handle a list of packets but does not yet */
549 for (packet
= packet_list
; packet
; packet
= packet_list
) {
551 packet_list
= mbuf_nextpkt(packet
);
552 mbuf_setnextpkt(packet
, NULL
);
557 /* Initialize the PF_INET domain, and add in the pre-defined protos */
564 static int inetdomain_initted
= 0;
566 if (!inetdomain_initted
)
569 dp
->dom_flags
= DOM_REENTRANT
;
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;
576 domain_proto_mtx_unlock(TRUE
);
577 proto_register_input(PF_INET
, ip_proto_input
, NULL
, 1);
578 domain_proto_mtx_lock();
583 ip_proto_dispatch_in_wrapper(struct mbuf
*m
, int hlen
, u_int8_t proto
)
585 ip_proto_dispatch_in(m
, hlen
, proto
, 0);
588 __private_extern__
void
589 ip_proto_dispatch_in(struct mbuf
*m
, int hlen
, u_int8_t proto
,
590 ipfilter_t inject_ipfref
)
592 struct ipfilter
*filter
;
593 int seen
= (inject_ipfref
== 0);
594 int changed_header
= 0;
596 void (*pr_input
)(struct mbuf
*, int len
);
598 if (!TAILQ_EMPTY(&ipv4_filters
)) {
600 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
602 if ((struct ipfilter
*)inject_ipfref
== filter
)
604 } else if (filter
->ipf_filter
.ipf_input
) {
607 if (changed_header
== 0) {
609 * Perform IP header alignment fixup,
610 * if needed, before passing packet
613 IP_HDR_ALIGNMENT_FIXUP(m
,
615 ipf_unref(); return;);
618 ip
= mtod(m
, struct ip
*);
619 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
620 ip
->ip_off
= htons(ip
->ip_off
);
622 ip
->ip_sum
= in_cksum(m
, hlen
);
624 result
= filter
->ipf_filter
.ipf_input(
625 filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
,
627 if (result
== EJUSTRETURN
) {
641 /* Perform IP header alignment fixup (post-filters), if needed */
642 IP_HDR_ALIGNMENT_FIXUP(m
, m
->m_pkthdr
.rcvif
, return;);
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
649 ip
= mtod(m
, struct ip
*);
651 if (changed_header
) {
652 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
653 ip
->ip_off
= ntohs(ip
->ip_off
);
656 if ((pr_input
= ip_protox
[ip
->ip_p
]->pr_input
) == NULL
) {
658 } else if (!(ip_protox
[ip
->ip_p
]->pr_flags
& PR_PROTOLOCK
)) {
659 lck_mtx_lock(inet_domain_mutex
);
661 lck_mtx_unlock(inet_domain_mutex
);
668 * Ip input routine. Checksum and byte swap header. If fragmented
669 * try to reassemble. Process options. Pass to next level.
672 ip_input(struct mbuf
*m
)
676 struct in_ifaddr
*ia
= NULL
;
677 unsigned int hlen
, checkif
;
679 struct in_addr pkt_dst
;
682 u_int32_t div_info
= 0; /* packet divert/tee info */
684 #if IPFIREWALL || DUMMYNET
685 struct ip_fw_args args
;
688 ipfilter_t inject_filter_ref
= 0;
690 /* Check if the mbuf is still valid after interface filter processing */
691 MBUF_INPUT_CHECK(m
, m
->m_pkthdr
.rcvif
);
693 /* Perform IP header alignment fixup, if needed */
694 IP_HDR_ALIGNMENT_FIXUP(m
, m
->m_pkthdr
.rcvif
, goto bad
;);
696 #if IPFIREWALL || DUMMYNET
697 bzero(&args
, sizeof(struct ip_fw_args
));
700 * Don't bother searching for tag(s) if there's none.
702 if (SLIST_EMPTY(&m
->m_pkthdr
.tags
))
705 /* Grab info from mtags prepended to the chain */
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
;
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
;
715 m_tag_delete(m
, tag
);
717 #endif /* DUMMYNET */
720 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
721 KERNEL_TAG_TYPE_DIVERT
, NULL
)) != NULL
) {
722 struct divert_tag
*div_tag
;
724 div_tag
= (struct divert_tag
*)(tag
+1);
725 args
.fwa_divert_rule
= div_tag
->cookie
;
727 m_tag_delete(m
, tag
);
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
;
735 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
736 args
.fwa_next_hop
= ipfwd_tag
->next_hop
;
738 m_tag_delete(m
, tag
);
742 if (m
== NULL
|| (m
->m_flags
& M_PKTHDR
) == 0)
743 panic("ip_input no HDR");
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
);
752 if (args
.fwa_ipfw_rule
)
754 #endif /* IPFIREWALL */
755 if (args
.fwa_pf_rule
)
758 #endif /* DUMMYNET */
760 #endif /* IPFIREWALL || DUMMYNET*/
763 * No need to process packet twice if we've already seen it.
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;
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
);
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
);
781 OSAddAtomic(1, &ipstat
.ips_total
);
782 if (m
->m_pkthdr
.len
< sizeof(struct ip
))
785 if (m
->m_len
< sizeof (struct ip
) &&
786 (m
= m_pullup(m
, sizeof (struct ip
))) == 0) {
787 OSAddAtomic(1, &ipstat
.ips_toosmall
);
790 ip
= mtod(m
, struct ip
*);
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
);
795 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
796 OSAddAtomic(1, &ipstat
.ips_badvers
);
800 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
801 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
802 OSAddAtomic(1, &ipstat
.ips_badhlen
);
805 if (hlen
> m
->m_len
) {
806 if ((m
= m_pullup(m
, hlen
)) == 0) {
807 OSAddAtomic(1, &ipstat
.ips_badhlen
);
810 ip
= mtod(m
, struct ip
*);
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
);
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
)
834 sum
= ip_cksum(m
, hlen
);
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
);
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.
856 ifnet_cached_rtlookup_inet(m
->m_pkthdr
.rcvif
, ip
->ip_src
);
858 nstat_route_rx(rt
, 1, m
->m_pkthdr
.len
, 0);
864 * Convert fields to host representation.
866 #if BYTE_ORDER != BIG_ENDIAN
870 if (ip
->ip_len
< hlen
) {
871 OSAddAtomic(1, &ipstat
.ips_badlen
);
875 #if BYTE_ORDER != BIG_ENDIAN
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.
884 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
886 OSAddAtomic(1, &ipstat
.ips_tooshort
);
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;
894 if (m
->m_len
== m
->m_pkthdr
.len
) {
895 m
->m_len
= ip
->ip_len
;
896 m
->m_pkthdr
.len
= ip
->ip_len
;
898 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
906 /* Invoke inbound packet filter */
910 error
= pf_af_hook(m
->m_pkthdr
.rcvif
, NULL
, &m
, AF_INET
, TRUE
, &args
);
912 error
= pf_af_hook(m
->m_pkthdr
.rcvif
, NULL
, &m
, AF_INET
, TRUE
, NULL
);
913 #endif /* DUMMYNET */
914 if (error
!= 0 || m
== NULL
) {
916 panic("%s: unexpected packet %p\n", __func__
, m
);
919 /* Already freed by callee */
922 ip
= mtod(m
, struct ip
*);
923 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
928 if (ipsec_bypass
== 0 && ipsec_gethist(m
, NULL
))
935 #endif /* DUMMYNET */
937 * Check if we want to allow this packet to be processed.
938 * Consider it to be bad if not.
940 if (fw_enable
&& IPFW_LOADED
) {
941 #if IPFIREWALL_FORWARD
943 * If we've been forwarded from the output side, then
944 * skip the firewall a second time
946 if (args
.fwa_next_hop
)
948 #endif /* IPFIREWALL_FORWARD */
952 i
= ip_fw_chk_ptr(&args
);
955 if ( (i
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) { /* drop */
960 ip
= mtod(m
, struct ip
*); /* just in case m changed */
962 if (i
== 0 && args
.fwa_next_hop
== NULL
) { /* common case */
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
);
971 #endif /* DUMMYNET */
973 if (i
!= 0 && (i
& IP_FW_PORT_DYNT_FLAG
) == 0) {
974 /* Divert or tee packet */
979 #if IPFIREWALL_FORWARD
980 if (i
== 0 && args
.fwa_next_hop
!= NULL
) {
985 * if we get here, the packet must be dropped
990 #endif /* IPFIREWALL */
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).
999 ip_nhops
= 0; /* for source routed packets */
1001 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, args
.fwa_next_hop
)) {
1003 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, NULL
)) {
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.
1014 if (rsvp_on
&& ip
->ip_p
==IPPROTO_RSVP
)
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
1023 if (TAILQ_EMPTY(&in_ifaddrhead
) &&
1024 (m
->m_flags
& (M_MCAST
|M_BCAST
)) == 0)
1028 * Cache the destination address of the packet; this may be
1029 * changed by use of 'ipfw fwd'.
1032 pkt_dst
= args
.fwa_next_hop
== NULL
?
1033 ip
->ip_dst
: args
.fwa_next_hop
->sin_addr
;
1035 pkt_dst
= ip
->ip_dst
;
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
1045 * XXX - Checking also should be disabled if the destination
1046 * address is ipnat'ed to a different interface.
1048 * XXX - Checking is incompatible with IP aliases added
1049 * to the loopback interface instead of the interface where
1050 * the packets are received.
1052 checkif
= ip_checkinterface
&& (ipforwarding
== 0) &&
1053 ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0)
1055 && (args
.fwa_next_hop
== NULL
);
1061 * Check for exact addresses in the hash bucket.
1063 lck_rw_lock_shared(in_ifaddr_rwlock
);
1064 TAILQ_FOREACH(ia
, INADDR_HASH(pkt_dst
.s_addr
), ia_hash
) {
1066 * If the address matches, verify that the packet
1067 * arrived via the correct interface if checking is
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
);
1077 IFA_UNLOCK(&ia
->ia_ifa
);
1079 lck_rw_done(in_ifaddr_rwlock
);
1082 * Check for broadcast addresses.
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().
1089 if (m
->m_pkthdr
.rcvif
->if_flags
& IFF_BROADCAST
) {
1091 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1092 ifnet_lock_shared(ifp
);
1093 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1095 if (ifa
->ifa_addr
->sa_family
!= AF_INET
) {
1100 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
1101 pkt_dst
.s_addr
|| ia
->ia_netbroadcast
.s_addr
==
1104 ifnet_lock_done(ifp
);
1109 ifnet_lock_done(ifp
);
1112 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
1113 struct in_multi
*inm
;
1114 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
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.
1125 lck_mtx_lock(ip_mutex
);
1126 if (ip_mforward
&& ip_mforward(ip
, ifp
, m
, 0) != 0) {
1127 OSAddAtomic(1, &ipstat
.ips_cantforward
);
1129 lck_mtx_unlock(ip_mutex
);
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.
1138 if (ip
->ip_p
== IPPROTO_IGMP
)
1140 OSAddAtomic(1, &ipstat
.ips_forward
);
1142 #endif /* MROUTING */
1144 * See if we belong to the destination multicast group on the
1145 * arrival interface.
1147 in_multihead_lock_shared();
1148 IN_LOOKUP_MULTI(&ip
->ip_dst
, ifp
, inm
);
1149 in_multihead_lock_done();
1151 OSAddAtomic(1, &ipstat
.ips_notmember
);
1158 if (ip
->ip_dst
.s_addr
== (u_int32_t
)INADDR_BROADCAST
)
1160 if (ip
->ip_dst
.s_addr
== INADDR_ANY
)
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
);
1174 ui
= mtod(m
, struct udpiphdr
*);
1175 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) {
1178 ip
= mtod(m
, struct ip
*); /* in case it changed */
1182 * Not for us; forward if possible and desirable.
1184 if (ipforwarding
== 0) {
1185 OSAddAtomic(1, &ipstat
.ips_cantforward
);
1189 ip_forward(m
, 0, args
.fwa_next_hop
);
1191 ip_forward(m
, 0, NULL
);
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.)
1204 if (ip
->ip_off
& (IP_MF
| IP_OFFMASK
| IP_RF
)) {
1206 /* If maxnipq is 0, never accept fragments. */
1209 OSAddAtomic(1, &ipstat
.ips_fragments
);
1210 OSAddAtomic(1, &ipstat
.ips_fragdropped
);
1215 * If we will exceed the number of fragments in queues, timeout the
1216 * oldest fragemented packet to make space.
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
);
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
) {
1228 * If we match the fragment queue we were going to
1229 * discard, drop this packet too.
1231 OSAddAtomic(1, &ipstat
.ips_fragdropped
);
1233 lck_mtx_unlock(ip_mutex
);
1240 sum
= IPREASS_HASH(ip
->ip_src
.s_addr
, ip
->ip_id
);
1242 * Look for queue of fragments
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
&&
1250 mac_ipq_label_compare(m
, fp
) &&
1252 ip
->ip_p
== fp
->ipq_p
)
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.
1260 if ((nipq
> maxnipq
) && (maxnipq
> 0)) {
1262 * drop the oldest fragment before proceeding further
1264 fp
= TAILQ_LAST(&ipq_list
, ipq_list
);
1265 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragtimeout
);
1273 * Adjust ip_len to not reflect header,
1274 * convert offset of this to bytes.
1277 if (ip
->ip_off
& IP_MF
) {
1279 * Make sure that fragments have a data length
1280 * that's a non-zero multiple of 8 bytes.
1282 if (ip
->ip_len
== 0 || (ip
->ip_len
& 0x7) != 0) {
1283 OSAddAtomic(1, &ipstat
.ips_toosmall
);
1284 lck_mtx_unlock(ip_mutex
);
1287 m
->m_flags
|= M_FRAG
;
1289 /* Clear the flag in case packet comes from loopback */
1290 m
->m_flags
&= ~M_FRAG
;
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.
1299 OSAddAtomic(1, &ipstat
.ips_fragments
);
1300 m
->m_pkthdr
.header
= ip
;
1302 m
= ip_reass(m
, fp
, &ipq
[sum
],
1303 (u_int16_t
*)&div_info
, &args
.fwa_divert_rule
);
1305 m
= ip_reass(m
, fp
, &ipq
[sum
]);
1308 lck_mtx_unlock(ip_mutex
);
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;
1317 /* Restore original checksum before diverting packet */
1318 if (div_info
!= 0) {
1321 #if BYTE_ORDER != BIG_ENDIAN
1327 ip
->ip_sum
= in_cksum(m
, hlen
);
1329 #if BYTE_ORDER != BIG_ENDIAN
1337 lck_mtx_unlock(ip_mutex
);
1343 * Divert or tee packet to the divert protocol if required.
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.
1348 if (div_info
!= 0) {
1349 struct mbuf
*clone
= NULL
;
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
);
1355 /* Restore packet header fields to original values */
1358 #if BYTE_ORDER != BIG_ENDIAN
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
);
1366 /* If 'tee', continue with original packet */
1367 if (clone
== NULL
) {
1371 ip
= mtod(m
, struct ip
*);
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.
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
);
1390 * Switch out to protocol's input routine.
1392 OSAddAtomic(1, &ipstat
.ips_delivered
);
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
;
1400 fwd_tag
= m_tag_create(KERNEL_MODULE_TAG_ID
,
1401 KERNEL_TAG_TYPE_IPFORWARD
, sizeof (*ipfwd_tag
),
1403 if (fwd_tag
== NULL
) {
1407 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
1408 ipfwd_tag
->next_hop
= args
.fwa_next_hop
;
1410 m_tag_prepend(m
, fwd_tag
);
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
);
1416 m
= tcp_lro(m
, hlen
);
1420 /* TCP deals with its own locking */
1421 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
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
);
1426 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1429 if ((sw_lro
) && (ip
->ip_p
== IPPROTO_TCP
)) {
1430 m
= tcp_lro(m
, hlen
);
1434 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1440 KERNEL_DEBUG(DBG_LAYER_END
, 0,0,0,0,0);
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.
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.
1453 static struct mbuf
*
1455 ip_reass(struct mbuf
*m
, struct ipq
*fp
, struct ipq
*where
,
1458 #else /* IPDIVERT_44 */
1460 #endif /* IPDIVERT_44 */
1461 u_int16_t
*divcookie
)
1462 #else /* IPDIVERT */
1463 ip_reass(struct mbuf
*m
, struct ipq
*fp
, struct ipq
*where
)
1464 #endif /* IPDIVERT */
1466 struct ip
*ip
= mtod(m
, struct ip
*);
1467 struct mbuf
*p
= 0, *q
, *nq
;
1469 int hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1473 lck_mtx_assert(ip_mutex
, LCK_MTX_ASSERT_OWNED
);
1475 * Presence of header sizes in mbufs
1476 * would confuse code below.
1481 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1482 m
->m_pkthdr
.csum_flags
= 0;
1484 * If first fragment to arrive, create a reassembly queue.
1487 if ((t
= m_get(M_DONTWAIT
, MT_FTABLE
)) == NULL
)
1489 fp
= mtod(t
, struct ipq
*);
1491 if (mac_ipq_label_init(fp
, M_NOWAIT
) != 0) {
1496 mac_ipq_label_associate(m
, fp
);
1498 insque((void*)fp
, (void*)where
);
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
;
1507 m
->m_nextpkt
= NULL
;
1510 fp
->ipq_div_info
= 0;
1514 fp
->ipq_div_cookie
= 0;
1516 TAILQ_INSERT_HEAD(&ipq_list
, fp
, ipq_list
);
1521 mac_ipq_label_update(m
, fp
);
1525 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
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.
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
)
1537 if (ecn0
!= IPTOS_ECN_CE
)
1538 GETIP(fp
->ipq_frags
)->ip_tos
|= IPTOS_ECN_CE
;
1540 if (ecn
== IPTOS_ECN_NOTECT
&& ecn0
!= IPTOS_ECN_NOTECT
)
1544 * Find a segment which begins after this one does.
1546 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
)
1547 if (GETIP(q
)->ip_off
> ip
->ip_off
)
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.
1556 * If some of the data is dropped from the the preceding
1557 * segment, then it's checksum is invalidated.
1560 i
= GETIP(p
)->ip_off
+ GETIP(p
)->ip_len
- ip
->ip_off
;
1562 if (i
>= ip
->ip_len
)
1565 m
->m_pkthdr
.csum_flags
= 0;
1569 m
->m_nextpkt
= p
->m_nextpkt
;
1572 m
->m_nextpkt
= fp
->ipq_frags
;
1577 * While we overlap succeeding segments trim them or,
1578 * if they are completely covered, dequeue them.
1580 for (; q
!= NULL
&& ip
->ip_off
+ ip
->ip_len
> GETIP(q
)->ip_off
;
1582 i
= (ip
->ip_off
+ ip
->ip_len
) -
1584 if (i
< GETIP(q
)->ip_len
) {
1585 GETIP(q
)->ip_len
-= i
;
1586 GETIP(q
)->ip_off
+= i
;
1588 q
->m_pkthdr
.csum_flags
= 0;
1593 OSAddAtomic(1, &ipstat
.ips_fragdropped
);
1603 * Transfer firewall instructions to the fragment structure.
1604 * Only trust info in the fragment at offset 0.
1606 if (ip
->ip_off
== 0) {
1608 fp
->ipq_div_info
= *divinfo
;
1610 fp
->ipq_divert
= *divinfo
;
1612 fp
->ipq_div_cookie
= *divcookie
;
1619 * Check for complete reassembly and perform frag per packet
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.)
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
);
1637 next
+= GETIP(q
)->ip_len
;
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
);
1649 * Reassembly is complete. Make sure the packet is a sane size.
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
);
1661 * Concatenate fragments.
1669 for (q
= nq
; q
!= NULL
; q
= nq
) {
1671 q
->m_nextpkt
= NULL
;
1672 if (q
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1673 m
->m_pkthdr
.csum_flags
= 0;
1675 m
->m_pkthdr
.csum_flags
&= q
->m_pkthdr
.csum_flags
;
1676 m
->m_pkthdr
.csum_data
+= q
->m_pkthdr
.csum_data
;
1683 * Extract firewall instructions from the fragment structure.
1686 *divinfo
= fp
->ipq_div_info
;
1688 *divinfo
= fp
->ipq_divert
;
1690 *divcookie
= fp
->ipq_div_cookie
;
1694 mac_mbuf_label_associate_ipq(fp
, m
);
1695 mac_ipq_label_destroy(fp
);
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.
1704 ip
->ip_src
= fp
->ipq_src
;
1705 ip
->ip_dst
= fp
->ipq_dst
;
1707 TAILQ_REMOVE(&ipq_list
, fp
, ipq_list
);
1708 currentfrags
-= fp
->ipq_nfrags
;
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 */
1716 for (t
= m
; t
; t
= t
->m_next
)
1718 m
->m_pkthdr
.len
= plen
;
1727 OSAddAtomic(1, &ipstat
.ips_fragdropped
);
1737 * Free a fragment reassembly header and all
1738 * associated datagrams.
1741 ip_freef(struct ipq
*fp
)
1743 lck_mtx_assert(ip_mutex
, LCK_MTX_ASSERT_OWNED
);
1744 currentfrags
-= fp
->ipq_nfrags
;
1745 m_freem_list(fp
->ipq_frags
);
1747 TAILQ_REMOVE(&ipq_list
, fp
, ipq_list
);
1748 (void) m_free(dtom(fp
));
1753 * IP timer processing;
1754 * if a timer expires on a reassembly
1755 * queue, discard it.
1762 lck_mtx_lock(ip_mutex
);
1763 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1767 while (fp
!= &ipq
[i
]) {
1770 if (fp
->prev
->ipq_ttl
== 0) {
1771 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragtimeout
);
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.
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
);
1790 lck_mtx_unlock(ip_mutex
);
1794 * Drain off all datagram fragments.
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
);
1808 lck_mtx_unlock(ip_mutex
);
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.
1825 ip_dooptions(struct mbuf
*m
, __unused
int pass
, struct sockaddr_in
*next_hop
)
1827 struct ip
*ip
= mtod(m
, struct ip
*);
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
;
1834 struct sockaddr_in ipaddr
= {
1835 sizeof (ipaddr
), AF_INET
, 0 , { 0 }, { 0, } };
1837 /* Expect 32-bit aligned data pointer on strict-align platforms */
1838 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
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
)
1847 if (opt
== IPOPT_NOP
)
1850 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
)) {
1851 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1854 optlen
= cp
[IPOPT_OLEN
];
1855 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
) {
1856 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
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.
1876 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1877 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1880 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1881 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1884 ipaddr
.sin_addr
= ip
->ip_dst
;
1885 ia
= (struct in_ifaddr
*)
1886 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
1888 if (opt
== IPOPT_SSRR
) {
1889 type
= ICMP_UNREACH
;
1890 code
= ICMP_UNREACH_SRCFAIL
;
1893 if (!ip_dosourceroute
)
1894 goto nosourcerouting
;
1896 * Loose routing, and not at next destination
1897 * yet; nothing to do except forward.
1902 IFA_REMREF(&ia
->ia_ifa
);
1905 off
--; /* 0 origin */
1906 if (off
> optlen
- (int)sizeof(struct in_addr
)) {
1908 * End of source route. Should be for us.
1910 if (!ip_acceptsourceroute
)
1911 goto nosourcerouting
;
1912 save_rte(cp
, ip
->ip_src
);
1916 if (!ip_dosourceroute
) {
1918 char buf
[MAX_IPv4_STR_LEN
];
1919 char buf2
[MAX_IPv4_STR_LEN
];
1921 * Acting as a router, so generate ICMP
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
;
1933 * Not acting as a router, so silently drop.
1935 OSAddAtomic(1, &ipstat
.ips_cantforward
);
1942 * locate outgoing interface
1944 (void)memcpy(&ipaddr
.sin_addr
, cp
+ off
,
1945 sizeof(ipaddr
.sin_addr
));
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
);
1955 ia
= ip_rtaddr(ipaddr
.sin_addr
);
1958 type
= ICMP_UNREACH
;
1959 code
= ICMP_UNREACH_SRCFAIL
;
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
);
1969 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1971 * Let ip_intr's mcast routing check handle mcast pkts
1973 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
1977 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1978 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1981 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1982 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1986 * If no space remains, ignore.
1988 off
--; /* 0 origin */
1989 if (off
> optlen
- (int)sizeof(struct in_addr
))
1991 (void)memcpy(&ipaddr
.sin_addr
, &ip
->ip_dst
,
1992 sizeof(ipaddr
.sin_addr
));
1994 * locate outgoing interface; if we're the destination,
1995 * use the incoming interface (should be same).
1997 if ((ia
= (INA
)ifa_ifwithaddr((struct sockaddr
*)
1999 if ((ia
= ip_rtaddr(ipaddr
.sin_addr
)) == 0) {
2000 type
= ICMP_UNREACH
;
2001 code
= ICMP_UNREACH_HOST
;
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
);
2011 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
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
;
2021 if (ipt
->ipt_ptr
< 5) {
2022 code
= (u_char
*)&ipt
->ipt_ptr
- (u_char
*)ip
;
2026 ipt
->ipt_len
- (int)sizeof(int32_t)) {
2027 if (++ipt
->ipt_oflw
== 0) {
2028 code
= (u_char
*)&ipt
->ipt_ptr
-
2034 sin
= (struct in_addr
*)(void *)(cp
+ ipt
->ipt_ptr
- 1);
2035 switch (ipt
->ipt_flg
) {
2037 case IPOPT_TS_TSONLY
:
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
-
2047 ipaddr
.sin_addr
= dst
;
2048 ia
= (INA
)ifaof_ifpforaddr((struct sockaddr
*)
2049 &ipaddr
, m
->m_pkthdr
.rcvif
);
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
);
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
-
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)
2073 IFA_REMREF(&ia
->ia_ifa
);
2075 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
2079 /* XXX can't take &ipt->ipt_flg */
2080 code
= (u_char
*)&ipt
->ipt_ptr
-
2085 (void)memcpy(cp
+ ipt
->ipt_ptr
- 1, &ntime
,
2087 ipt
->ipt_ptr
+= sizeof(n_time
);
2090 if (forward
&& ipforwarding
) {
2091 ip_forward(m
, 1, next_hop
);
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
);
2103 * Given address of next destination (final or next hop),
2104 * return internet address info of interface to be used to get there.
2107 ip_rtaddr(struct in_addr dst
)
2109 struct sockaddr_in
*sin
;
2110 struct ifaddr
*rt_ifa
;
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
;
2119 rtalloc_ign(&ro
, RTF_PRCLONING
);
2120 if (ro
.ro_rt
== NULL
)
2124 if ((rt_ifa
= ro
.ro_rt
->rt_ifa
) != NULL
)
2126 RT_UNLOCK(ro
.ro_rt
);
2129 return ((struct in_ifaddr
*)rt_ifa
);
2133 * Save incoming source route for use in replies,
2134 * to be picked up later by ip_srcroute if the receiver is interested.
2137 save_rte(u_char
*option
, struct in_addr dst
)
2141 olen
= option
[IPOPT_OLEN
];
2144 printf("save_rte: olen %d\n", olen
);
2146 if (olen
> sizeof(ip_srcrt
) - (1 + sizeof(dst
)))
2148 bcopy(option
, ip_srcrt
.srcopt
, olen
);
2149 ip_nhops
= (olen
- IPOPT_OFFSET
- 1) / sizeof(struct in_addr
);
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.
2161 struct in_addr
*p
, *q
;
2165 return ((struct mbuf
*)0);
2166 m
= m_get(M_DONTWAIT
, MT_HEADER
);
2168 return ((struct mbuf
*)0);
2170 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
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
) +
2177 printf("ip_srcroute: nhops %d mlen %d", ip_nhops
, m
->m_len
);
2181 * First save first hop for return route
2183 p
= &ip_srcrt
.route
[ip_nhops
- 1];
2184 *(mtod(m
, struct in_addr
*)) = *p
--;
2187 printf(" hops %lx", (u_int32_t
)ntohl(mtod(m
, struct in_addr
*)->s_addr
));
2191 * Copy option fields and padding (nop) to mbuf.
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
);
2201 * Record return path as an IP source route,
2202 * reversing the path (pointers are now aligned).
2204 while (p
>= ip_srcrt
.route
) {
2207 printf(" %lx", (u_int32_t
)ntohl(q
->s_addr
));
2212 * Last hop goes to final destination.
2217 printf(" %lx\n", (u_int32_t
)ntohl(q
->s_addr
));
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.
2230 ip_stripoptions(struct mbuf
*m
, __unused
struct mbuf
*mopt
)
2233 struct ip
*ip
= mtod(m
, struct ip
*);
2237 /* Expect 32-bit aligned data pointer on strict-align platforms */
2238 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
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
);
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);
2250 u_char inetctlerrmap
[PRC_NCMDS
] = {
2252 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
2253 ENETUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
2254 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
2256 ENOPROTOOPT
, ECONNREFUSED
2260 sysctl_ipforwarding SYSCTL_HANDLER_ARGS
2262 #pragma unused(arg1, arg2)
2263 int i
, was_ipforwarding
= ipforwarding
;
2265 i
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
2266 if (i
!= 0 || req
->newptr
== USER_ADDR_NULL
)
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
];
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
);
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.
2295 ip_fwd_route_copyout(struct ifnet
*ifp
, struct route
*dst
)
2297 struct route
*src
= &ifp
->if_fwd_route
;
2299 lck_mtx_lock_spin(&ifp
->if_cached_route_lock
);
2300 lck_mtx_convert_spin(&ifp
->if_cached_route_lock
);
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
);
2306 route_copyout(dst
, src
, sizeof(*dst
));
2308 lck_mtx_unlock(&ifp
->if_cached_route_lock
);
2312 ip_fwd_route_copyin(struct ifnet
*ifp
, struct route
*src
)
2314 struct route
*dst
= &ifp
->if_fwd_route
;
2316 lck_mtx_lock_spin(&ifp
->if_cached_route_lock
);
2317 lck_mtx_convert_spin(&ifp
->if_cached_route_lock
);
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
);
2323 if (ifp
->if_fwd_cacheok
)
2324 route_copyin(src
, dst
, sizeof(*src
));
2326 lck_mtx_unlock(&ifp
->if_cached_route_lock
);
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.
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.
2340 * The srcrt parameter indicates whether the packet is being forwarded
2341 * via a source route.
2344 ip_forward(struct mbuf
*m
, int srcrt
, struct sockaddr_in
*next_hop
)
2347 #pragma unused(next_hop)
2349 struct ip
*ip
= mtod(m
, struct ip
*);
2350 struct sockaddr_in
*sin
;
2352 struct route fwd_rt
;
2353 int error
, type
= 0, code
= 0;
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
;
2361 struct pf_mtag
*pf_mtag
;
2367 * Cache the destination address of the packet; this may be
2368 * changed by use of 'ipfw fwd'.
2370 pkt_dst
= next_hop
? next_hop
->sin_addr
: ip
->ip_dst
;
2372 pkt_dst
= ip
->ip_dst
;
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
,
2382 if (m
->m_flags
& (M_BCAST
|M_MCAST
) || in_canforward(pkt_dst
) == 0) {
2383 OSAddAtomic(1, &ipstat
.ips_cantforward
);
2390 if (ip
->ip_ttl
<= IPTTLDEC
) {
2391 icmp_error(m
, ICMP_TIMXCEED
, ICMP_TIMXCEED_INTRANS
,
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
;
2407 ip_fwd_route_copyout(ifp
, &fwd_rt
);
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
;
2417 sin
->sin_family
= AF_INET
;
2418 sin
->sin_len
= sizeof (*sin
);
2419 sin
->sin_addr
= pkt_dst
;
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);
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.
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().
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,
2443 m_copydata(m
, 0, mcopy
->m_len
, mtod(mcopy
, caddr_t
));
2449 ip
->ip_ttl
-= IPTTLDEC
;
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.
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
);
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
;
2477 dest
= pkt_dst
.s_addr
;
2478 /* Router requirements says to only send host redirects */
2479 type
= ICMP_REDIRECT
;
2480 code
= ICMP_REDIRECT_HOST
;
2483 printf("redirect (%d) to %lx\n", code
, (u_int32_t
)dest
);
2486 IFA_UNLOCK(&ia
->ia_ifa
);
2492 /* Pass IPFORWARD info if available */
2494 struct ip_fwd_tag
*ipfwd_tag
;
2496 tag
= m_tag_create(KERNEL_MODULE_TAG_ID
,
2497 KERNEL_TAG_TYPE_IPFORWARD
,
2498 sizeof (*ipfwd_tag
), M_NOWAIT
, m
);
2505 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
2506 ipfwd_tag
->next_hop
= next_hop
;
2508 m_tag_prepend(m
, tag
);
2511 error
= ip_output_list(m
, 0, NULL
, &fwd_rt
,
2512 IP_FORWARDING
| IP_OUTARGS
, 0, &ipoa
);
2514 /* Refresh rt since the route could have changed while in IP */
2518 OSAddAtomic(1, &ipstat
.ips_cantforward
);
2520 OSAddAtomic(1, &ipstat
.ips_forward
);
2522 OSAddAtomic(1, &ipstat
.ips_redirectsent
);
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.
2541 case 0: /* forwarded, but need redirect */
2542 /* type, code set above */
2545 case ENETUNREACH
: /* shouldn't happen, checked above */
2550 type
= ICMP_UNREACH
;
2551 code
= ICMP_UNREACH_HOST
;
2555 type
= ICMP_UNREACH
;
2556 code
= ICMP_UNREACH_NEEDFRAG
;
2560 if (rt
->rt_ifp
!= NULL
)
2561 nextmtu
= rt
->rt_ifp
->if_mtu
;
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
2572 struct secpolicy
*sp
= NULL
;
2578 if (rt
->rt_ifp
!= NULL
)
2579 nextmtu
= rt
->rt_ifp
->if_mtu
;
2583 OSAddAtomic(1, &ipstat
.ips_cantfrag
);
2586 sp
= ipsec4_getpolicybyaddr(mcopy
,
2592 /* count IPsec header size */
2593 ipsechdr
= ipsec_hdrsiz(sp
);
2596 * find the correct route for outer IPv4
2597 * header, compute tunnel MTU.
2601 if (sp
->req
!= NULL
) {
2602 if (sp
->req
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2603 struct secasindex saidx
;
2605 struct secasvar
*sav
;
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
));
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
));
2627 sav
= key_allocsa_policy(&saidx
);
2629 lck_mtx_lock(sadb_mutex
);
2630 if (sav
->sah
!= NULL
) {
2631 ro
= &sav
->sah
->sa_route
;
2632 if (ro
->ro_rt
!= NULL
) {
2634 if (ro
->ro_rt
->rt_ifp
!= NULL
) {
2635 nextmtu
= ro
->ro_rt
->rt_ifp
->if_mtu
;
2636 nextmtu
-= ipsechdr
;
2638 RT_UNLOCK(ro
->ro_rt
);
2641 key_freesav(sav
, KEY_SADB_LOCKED
);
2642 lck_mtx_unlock(sadb_mutex
);
2646 key_freesp(sp
, KEY_SADB_UNLOCKED
);
2650 OSAddAtomic(1, &ipstat
.ips_cantfrag
);
2654 type
= ICMP_SOURCEQUENCH
;
2658 case EACCES
: /* ipfw denied packet */
2663 icmp_error(mcopy
, type
, code
, dest
, nextmtu
);
2665 ip_fwd_route_copyin(ifp
, &fwd_rt
);
2676 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
2680 mp
= sbcreatecontrol_mbuf((caddr_t
) &tv
, sizeof(tv
),
2681 SCM_TIMESTAMP
, SOL_SOCKET
, mp
);
2686 if ((inp
->inp_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
2689 time
= mach_absolute_time();
2690 mp
= sbcreatecontrol_mbuf((caddr_t
) &time
, sizeof(time
),
2691 SCM_TIMESTAMP_MONOTONIC
, SOL_SOCKET
, mp
);
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
);
2706 * Moving these out of udp_input() made them even more broken
2707 * than they already were.
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
);
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
);
2726 if (inp
->inp_flags
& INP_RECVIF
) {
2729 struct sockaddr_dl sdl
;
2732 struct sockaddr_dl
*sdp
;
2733 struct sockaddr_dl
*sdl2
= &sdlbuf
.sdl
;
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];
2740 if (!ifa
|| !ifa
->ifa_addr
)
2744 sdp
= (struct sockaddr_dl
*)(void *)ifa
->ifa_addr
;
2746 * Change our mind and don't try copy.
2748 if ((sdp
->sdl_family
!= AF_LINK
) ||
2749 (sdp
->sdl_len
> sizeof(sdlbuf
))) {
2753 bcopy(sdp
, sdl2
, sdp
->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;
2764 mp
= sbcreatecontrol_mbuf((caddr_t
) sdl2
, sdl2
->sdl_len
,
2765 IP_RECVIF
, IPPROTO_IP
, mp
);
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
);
2777 if ((inp
->inp_socket
->so_flags
& SOF_RECV_TRAFFIC_CLASS
) != 0) {
2778 int tc
= m_get_traffic_class(m
);
2780 mp
= sbcreatecontrol_mbuf((caddr_t
) &tc
, sizeof(tc
),
2781 SO_TRAFFIC_CLASS
, SOL_SOCKET
, mp
);
2786 if (inp
->inp_flags
& INP_PKTINFO
) {
2787 struct in_pktinfo pi
;
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;
2793 mp
= sbcreatecontrol_mbuf((caddr_t
)&pi
, sizeof(struct in_pktinfo
),
2794 IP_RECVPKTINFO
, IPPROTO_IP
, mp
);
2802 ipstat
.ips_pktdropcntrl
++;
2807 ip_rsvp_init(struct socket
*so
)
2809 if (so
->so_type
!= SOCK_RAW
||
2810 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2813 if (ip_rsvpd
!= NULL
)
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.
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.
2844 static inline u_short
2845 ip_cksum(struct mbuf
*m
, int hlen
)
2851 ip
= mtod(m
, struct ip
*);
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 */
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) {
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.
2871 sum
= in_cksum(m
, hlen
);
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.
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;
2886 OSAddAtomic(1, &ipstat
.ips_badsum
);