2 * Copyright (c) 2000-2011 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 #if defined(NFAITH) && NFAITH > 0
140 #include <net/if_types.h>
144 #include <netinet/ip_dummynet.h>
148 #include <net/pfvar.h>
152 extern int ipsec_bypass
;
153 extern lck_mtx_t
*sadb_mutex
;
155 lck_grp_t
*sadb_stat_mutex_grp
;
156 lck_grp_attr_t
*sadb_stat_mutex_grp_attr
;
157 lck_attr_t
*sadb_stat_mutex_attr
;
158 lck_mtx_t
*sadb_stat_mutex
;
163 static int ip_rsvp_on
;
164 struct socket
*ip_rsvpd
;
166 static int sysctl_ipforwarding SYSCTL_HANDLER_ARGS
;
168 int ipforwarding
= 0;
169 SYSCTL_PROC(_net_inet_ip
, IPCTL_FORWARDING
, forwarding
,
170 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &ipforwarding
, 0,
171 sysctl_ipforwarding
, "I", "Enable IP forwarding between interfaces");
173 static int ipsendredirects
= 1; /* XXX */
174 SYSCTL_INT(_net_inet_ip
, IPCTL_SENDREDIRECTS
, redirect
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
175 &ipsendredirects
, 0, "Enable sending IP redirects");
177 int ip_defttl
= IPDEFTTL
;
178 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFTTL
, ttl
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
179 &ip_defttl
, 0, "Maximum TTL on IP packets");
181 static int ip_dosourceroute
= 0;
182 SYSCTL_INT(_net_inet_ip
, IPCTL_SOURCEROUTE
, sourceroute
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
183 &ip_dosourceroute
, 0, "Enable forwarding source routed IP packets");
185 static int ip_acceptsourceroute
= 0;
186 SYSCTL_INT(_net_inet_ip
, IPCTL_ACCEPTSOURCEROUTE
, accept_sourceroute
,
187 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_acceptsourceroute
, 0,
188 "Enable accepting source routed IP packets");
190 static int ip_keepfaith
= 0;
191 SYSCTL_INT(_net_inet_ip
, IPCTL_KEEPFAITH
, keepfaith
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
193 "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
195 static int nipq
= 0; /* total # of reass queues */
197 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfragpackets
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
199 "Maximum number of IPv4 fragment reassembly queue entries");
201 static int maxfragsperpacket
;
202 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfragsperpacket
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
203 &maxfragsperpacket
, 0,
204 "Maximum number of IPv4 fragments allowed per packet");
207 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfrags
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
208 &maxfrags
, 0, "Maximum number of IPv4 fragments allowed");
210 static int currentfrags
= 0;
212 int ip_doscopedroute
= 1;
213 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, scopedroute
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
214 &ip_doscopedroute
, 0, "Enable IPv4 scoped routing");
217 * XXX - Setting ip_checkinterface mostly implements the receive side of
218 * the Strong ES model described in RFC 1122, but since the routing table
219 * and transmit implementation do not implement the Strong ES model,
220 * setting this to 1 results in an odd hybrid.
222 * XXX - ip_checkinterface currently must be disabled if you use ipnat
223 * to translate the destination address to another local interface.
225 * XXX - ip_checkinterface must be disabled if you add IP aliases
226 * to the loopback interface instead of the interface where the
227 * packets for those addresses are received.
229 static int ip_checkinterface
= 0;
230 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, check_interface
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
231 &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 lck_rw_t
*in_ifaddr_rwlock
;
249 /* Protected by in_ifaddr_rwlock */
250 struct in_ifaddrhead in_ifaddrhead
; /* first inet address */
251 struct in_ifaddrhashhead
*in_ifaddrhashtbl
; /* inet addr hash table */
253 #define INADDR_NHASH 61
254 static u_int32_t inaddr_nhash
; /* hash table size */
255 static u_int32_t inaddr_hashp
; /* next largest prime */
257 struct ifqueue ipintrq
;
258 SYSCTL_INT(_net_inet_ip
, IPCTL_INTRQMAXLEN
, intr_queue_maxlen
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
259 &ipintrq
.ifq_maxlen
, 0, "Maximum size of the IP input queue");
260 SYSCTL_INT(_net_inet_ip
, IPCTL_INTRQDROPS
, intr_queue_drops
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
261 &ipintrq
.ifq_drops
, 0, "Number of packets dropped from the IP input queue");
263 struct ipstat ipstat
;
264 SYSCTL_STRUCT(_net_inet_ip
, IPCTL_STATS
, stats
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
265 &ipstat
, ipstat
, "IP statistics (struct ipstat, netinet/ip_var.h)");
267 /* Packet reassembly stuff */
268 #define IPREASS_NHASH_LOG2 6
269 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
270 #define IPREASS_HMASK (IPREASS_NHASH - 1)
271 #define IPREASS_HASH(x,y) \
272 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
274 static struct ipq ipq
[IPREASS_NHASH
];
275 static TAILQ_HEAD(ipq_list
, ipq
) ipq_list
=
276 TAILQ_HEAD_INITIALIZER(ipq_list
);
277 const int ipintrq_present
= 1;
279 lck_attr_t
*ip_mutex_attr
;
280 lck_grp_t
*ip_mutex_grp
;
281 lck_grp_attr_t
*ip_mutex_grp_attr
;
282 lck_mtx_t
*inet_domain_mutex
;
283 extern lck_mtx_t
*domain_proto_mtx
;
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
;
305 ip_dn_io_t
*ip_dn_io_ptr
;
308 int (*fr_checkp
)(struct ip
*, int, struct ifnet
*, int, struct mbuf
**) = NULL
;
309 #endif /* IPFIREWALL */
311 SYSCTL_NODE(_net_inet_ip
, OID_AUTO
, linklocal
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "link local");
313 struct ip_linklocal_stat ip_linklocal_stat
;
314 SYSCTL_STRUCT(_net_inet_ip_linklocal
, OID_AUTO
, stat
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
315 &ip_linklocal_stat
, ip_linklocal_stat
,
316 "Number of link local packets with TTL less than 255");
318 SYSCTL_NODE(_net_inet_ip_linklocal
, OID_AUTO
, in
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "link local input");
320 int ip_linklocal_in_allowbadttl
= 1;
321 SYSCTL_INT(_net_inet_ip_linklocal_in
, OID_AUTO
, allowbadttl
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
322 &ip_linklocal_in_allowbadttl
, 0,
323 "Allow incoming link local packets with TTL less than 255");
327 * We need to save the IP options in case a protocol wants to respond
328 * to an incoming packet over the same route if the packet got here
329 * using IP source routing. This allows connection establishment and
330 * maintenance when the remote end is on a network that is not known
333 static int ip_nhops
= 0;
334 static struct ip_srcrt
{
335 struct in_addr dst
; /* final destination */
336 char nop
; /* one NOP to align */
337 char srcopt
[IPOPT_OFFSET
+ 1]; /* OPTVAL, OLEN and OFFSET */
338 struct in_addr route
[MAX_IPOPTLEN
/sizeof(struct in_addr
)];
341 static void in_ifaddrhashtbl_init(void);
342 static void save_rte(u_char
*, struct in_addr
);
343 static int ip_dooptions(struct mbuf
*, int, struct sockaddr_in
*);
344 static void ip_forward(struct mbuf
*, int, struct sockaddr_in
*);
345 static void ip_freef(struct ipq
*);
348 static struct mbuf
*ip_reass(struct mbuf
*,
349 struct ipq
*, struct ipq
*, u_int32_t
*, u_int16_t
*);
351 static struct mbuf
*ip_reass(struct mbuf
*,
352 struct ipq
*, struct ipq
*, u_int16_t
*, u_int16_t
*);
355 static struct mbuf
*ip_reass(struct mbuf
*, struct ipq
*, struct ipq
*);
357 static void ip_fwd_route_copyout(struct ifnet
*, struct route
*);
358 static void ip_fwd_route_copyin(struct ifnet
*, struct route
*);
363 extern u_short ip_id
;
365 int ip_use_randomid
= 1;
366 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, random_id
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
367 &ip_use_randomid
, 0, "Randomize IP packets IDs");
370 #define satosin(sa) ((struct sockaddr_in *)(sa))
371 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
374 * IP initialization: fill in IP protocol switch table.
375 * All protocols not implemented in kernel go to raw IP protocol handler.
382 static int ip_initialized
= 0;
386 PE_parse_boot_argn("net.inet.ip.scopedroute",
387 &ip_doscopedroute
, sizeof (ip_doscopedroute
));
391 in_ifaddr_rwlock_grp_attr
= lck_grp_attr_alloc_init();
392 in_ifaddr_rwlock_grp
= lck_grp_alloc_init("in_ifaddr_rwlock",
393 in_ifaddr_rwlock_grp_attr
);
394 in_ifaddr_rwlock_attr
= lck_attr_alloc_init();
395 in_ifaddr_rwlock
= lck_rw_alloc_init(in_ifaddr_rwlock_grp
,
396 in_ifaddr_rwlock_attr
);
398 TAILQ_INIT(&in_ifaddrhead
);
399 in_ifaddrhashtbl_init();
403 pr
= pffindproto_locked(PF_INET
, IPPROTO_RAW
, SOCK_RAW
);
406 for (i
= 0; i
< IPPROTO_MAX
; i
++)
408 for (pr
= inetdomain
.dom_protosw
; pr
; pr
= pr
->pr_next
) {
409 if (pr
->pr_domain
== NULL
)
410 continue; /* If uninitialized, skip */
411 if (pr
->pr_domain
->dom_family
== PF_INET
&&
412 pr
->pr_protocol
&& pr
->pr_protocol
!= IPPROTO_RAW
)
413 ip_protox
[pr
->pr_protocol
] = pr
;
415 for (i
= 0; i
< IPREASS_NHASH
; i
++)
416 ipq
[i
].next
= ipq
[i
].prev
= &ipq
[i
];
418 maxnipq
= nmbclusters
/ 32;
419 maxfrags
= maxnipq
* 2;
420 maxfragsperpacket
= 128; /* enough for 64k in 512 byte fragments */
424 struct timeval timenow
;
425 getmicrotime(&timenow
);
426 ip_id
= timenow
.tv_sec
& 0xffff;
429 ipintrq
.ifq_maxlen
= ipqmaxlen
;
433 ip_mutex_grp_attr
= lck_grp_attr_alloc_init();
435 ip_mutex_grp
= lck_grp_alloc_init("ip", ip_mutex_grp_attr
);
437 ip_mutex_attr
= lck_attr_alloc_init();
439 if ((ip_mutex
= lck_mtx_alloc_init(ip_mutex_grp
, ip_mutex_attr
)) == NULL
) {
440 printf("ip_init: can't alloc ip_mutex\n");
446 sadb_stat_mutex_grp_attr
= lck_grp_attr_alloc_init();
447 sadb_stat_mutex_grp
= lck_grp_alloc_init("sadb_stat", sadb_stat_mutex_grp_attr
);
448 sadb_stat_mutex_attr
= lck_attr_alloc_init();
450 if ((sadb_stat_mutex
= lck_mtx_alloc_init(sadb_stat_mutex_grp
, sadb_stat_mutex_attr
)) == NULL
) {
451 printf("ip_init: can't alloc sadb_stat_mutex\n");
463 * Initialize IPv4 source address hash table.
466 in_ifaddrhashtbl_init(void)
470 if (in_ifaddrhashtbl
!= NULL
)
473 PE_parse_boot_argn("inaddr_nhash", &inaddr_nhash
, sizeof (inaddr_nhash
));
474 if (inaddr_nhash
== 0)
475 inaddr_nhash
= INADDR_NHASH
;
477 MALLOC(in_ifaddrhashtbl
, struct in_ifaddrhashhead
*,
478 inaddr_nhash
* sizeof (*in_ifaddrhashtbl
),
479 M_IFADDR
, M_WAITOK
| M_ZERO
);
480 if (in_ifaddrhashtbl
== NULL
)
481 panic("in_ifaddrhashtbl_init allocation failed");
484 * Generate the next largest prime greater than inaddr_nhash.
486 k
= (inaddr_nhash
% 2 == 0) ? inaddr_nhash
+ 1 : inaddr_nhash
+ 2;
489 for (i
= 3; i
* i
<= k
; i
+= 2) {
501 inaddr_hashval(u_int32_t key
)
504 * The hash index is the computed prime times the key modulo
505 * the hash size, as documented in "Introduction to Algorithms"
506 * (Cormen, Leiserson, Rivest).
508 if (inaddr_nhash
> 1)
509 return ((key
* inaddr_hashp
) % inaddr_nhash
);
516 protocol_family_t __unused protocol
,
522 /* ip_input should handle a list of packets but does not yet */
524 for (packet
= packet_list
; packet
; packet
= packet_list
) {
526 packet_list
= mbuf_nextpkt(packet
);
527 mbuf_setnextpkt(packet
, NULL
);
532 /* Initialize the PF_INET domain, and add in the pre-defined protos */
539 static int inetdomain_initted
= 0;
541 if (!inetdomain_initted
)
543 /* kprintf("Initing %d protosw entries\n", in_proto_count); */
545 dp
->dom_flags
= DOM_REENTRANT
;
547 for (i
=0, pr
= &inetsw
[0]; i
<in_proto_count
; i
++, pr
++)
548 net_add_proto(pr
, dp
);
549 inet_domain_mutex
= dp
->dom_mtx
;
550 inetdomain_initted
= 1;
552 lck_mtx_unlock(domain_proto_mtx
);
553 proto_register_input(PF_INET
, ip_proto_input
, NULL
, 1);
554 lck_mtx_lock(domain_proto_mtx
);
558 __private_extern__
void
559 ip_proto_dispatch_in(
563 ipfilter_t inject_ipfref
)
565 struct ipfilter
*filter
;
566 int seen
= (inject_ipfref
== 0);
567 int changed_header
= 0;
569 void (*pr_input
)(struct mbuf
*, int len
);
571 if (!TAILQ_EMPTY(&ipv4_filters
)) {
573 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
575 if ((struct ipfilter
*)inject_ipfref
== filter
)
577 } else if (filter
->ipf_filter
.ipf_input
) {
580 if (changed_header
== 0) {
582 ip
= mtod(m
, struct ip
*);
583 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
584 ip
->ip_off
= htons(ip
->ip_off
);
586 ip
->ip_sum
= in_cksum(m
, hlen
);
588 result
= filter
->ipf_filter
.ipf_input(
589 filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, hlen
, proto
);
590 if (result
== EJUSTRETURN
) {
604 * If there isn't a specific lock for the protocol
605 * we're about to call, use the generic lock for AF_INET.
606 * otherwise let the protocol deal with its own locking
608 ip
= mtod(m
, struct ip
*);
610 if (changed_header
) {
611 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
612 ip
->ip_off
= ntohs(ip
->ip_off
);
615 if ((pr_input
= ip_protox
[ip
->ip_p
]->pr_input
) == NULL
) {
617 } else if (!(ip_protox
[ip
->ip_p
]->pr_flags
& PR_PROTOLOCK
)) {
618 lck_mtx_lock(inet_domain_mutex
);
620 lck_mtx_unlock(inet_domain_mutex
);
627 * Ip input routine. Checksum and byte swap header. If fragmented
628 * try to reassemble. Process options. Pass to next level.
631 ip_input(struct mbuf
*m
)
635 struct in_ifaddr
*ia
= NULL
;
638 struct in_addr pkt_dst
;
641 u_int32_t div_info
= 0; /* packet divert/tee info */
642 struct ip_fw_args args
;
645 ipfilter_t inject_filter_ref
= 0;
647 /* Check if the mbuf is still valid after interface filter processing */
648 MBUF_INPUT_CHECK(m
, m
->m_pkthdr
.rcvif
);
654 args
.divert_rule
= 0; /* divert cookie */
655 args
.next_hop
= NULL
;
658 * Don't bother searching for tag(s) if there's none.
660 if (SLIST_EMPTY(&m
->m_pkthdr
.tags
))
663 /* Grab info from mtags prepended to the chain */
665 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
666 KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
667 struct dn_pkt_tag
*dn_tag
;
669 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
670 args
.rule
= dn_tag
->rule
;
672 m_tag_delete(m
, tag
);
674 #endif /* DUMMYNET */
677 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
678 KERNEL_TAG_TYPE_DIVERT
, NULL
)) != NULL
) {
679 struct divert_tag
*div_tag
;
681 div_tag
= (struct divert_tag
*)(tag
+1);
682 args
.divert_rule
= div_tag
->cookie
;
684 m_tag_delete(m
, tag
);
688 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
689 KERNEL_TAG_TYPE_IPFORWARD
, NULL
)) != NULL
) {
690 struct ip_fwd_tag
*ipfwd_tag
;
692 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
693 args
.next_hop
= ipfwd_tag
->next_hop
;
695 m_tag_delete(m
, tag
);
699 if (m
== NULL
|| (m
->m_flags
& M_PKTHDR
) == 0)
700 panic("ip_input no HDR");
703 if (args
.rule
) { /* dummynet already filtered us */
704 ip
= mtod(m
, struct ip
*);
705 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
706 inject_filter_ref
= ipf_get_inject_filter(m
);
710 #endif /* IPFIREWALL */
713 * No need to proccess packet twice if we've already seen it.
715 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
))
716 inject_filter_ref
= ipf_get_inject_filter(m
);
717 if (inject_filter_ref
!= 0) {
718 ip
= mtod(m
, struct ip
*);
719 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
721 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
722 struct ip
*, ip
, struct ifnet
*, m
->m_pkthdr
.rcvif
,
723 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
725 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
726 ip
->ip_off
= ntohs(ip
->ip_off
);
727 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, inject_filter_ref
);
731 OSAddAtomic(1, &ipstat
.ips_total
);
733 if (m
->m_pkthdr
.len
< sizeof(struct ip
))
736 if (m
->m_len
< sizeof (struct ip
) &&
737 (m
= m_pullup(m
, sizeof (struct ip
))) == 0) {
738 OSAddAtomic(1, &ipstat
.ips_toosmall
);
741 ip
= mtod(m
, struct ip
*);
743 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
744 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
746 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
747 OSAddAtomic(1, &ipstat
.ips_badvers
);
751 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
752 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
753 OSAddAtomic(1, &ipstat
.ips_badhlen
);
756 if (hlen
> m
->m_len
) {
757 if ((m
= m_pullup(m
, hlen
)) == 0) {
758 OSAddAtomic(1, &ipstat
.ips_badhlen
);
761 ip
= mtod(m
, struct ip
*);
764 /* 127/8 must not appear on wire - RFC1122 */
765 if ((ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
766 (ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
) {
767 if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) {
768 OSAddAtomic(1, &ipstat
.ips_badaddr
);
773 /* IPv4 Link-Local Addresses as defined in <draft-ietf-zeroconf-ipv4-linklocal-05.txt> */
774 if ((IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
)) ||
775 IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)))) {
776 ip_linklocal_stat
.iplls_in_total
++;
777 if (ip
->ip_ttl
!= MAXTTL
) {
778 OSAddAtomic(1, &ip_linklocal_stat
.iplls_in_badttl
);
779 /* Silently drop link local traffic with bad TTL */
780 if (!ip_linklocal_in_allowbadttl
)
784 if ((IF_HWASSIST_CSUM_FLAGS(m
->m_pkthdr
.rcvif
->if_hwassist
) == 0)
785 || (apple_hwcksum_rx
== 0) ||
786 ((m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
) && ip
->ip_p
!= IPPROTO_TCP
)) {
787 m
->m_pkthdr
.csum_flags
= 0; /* invalidate HW generated checksum flags */
790 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
791 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
792 } else if (!(m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) ||
793 apple_hwcksum_tx
== 0) {
795 * Either this is not loopback packet coming from an interface
796 * that does not support checksum offloading, or it is loopback
797 * packet that has undergone software checksumming at the send
798 * side because apple_hwcksum_tx was set to 0. In this case,
799 * calculate the checksum in software to validate the packet.
801 sum
= in_cksum(m
, hlen
);
804 * This is a loopback packet without any valid checksum since
805 * the send side has bypassed it (apple_hwcksum_tx set to 1).
806 * We get here because apple_hwcksum_rx was set to 0, and so
807 * we pretend that all is well.
810 m
->m_pkthdr
.csum_flags
|= CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
811 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
812 m
->m_pkthdr
.csum_data
= 0xffff;
815 OSAddAtomic(1, &ipstat
.ips_badsum
);
819 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
820 struct ip
*, ip
, struct ifnet
*, m
->m_pkthdr
.rcvif
,
821 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
824 * Naively assume we can attribute inbound data to the route we would
825 * use to send to this destination. Asymetric routing breaks this
826 * assumption, but it still allows us to account for traffic from
827 * a remote node in the routing table.
828 * this has a very significant performance impact so we bypass
829 * if nstat_collect is disabled. We may also bypass if the
830 * protocol is tcp in the future because tcp will have a route that
831 * we can use to attribute the data to. That does mean we would not
832 * account for forwarded tcp traffic.
836 ifnet_cached_rtlookup_inet(m
->m_pkthdr
.rcvif
, ip
->ip_src
);
838 nstat_route_rx(rt
, 1, m
->m_pkthdr
.len
, 0);
844 * Convert fields to host representation.
846 #if BYTE_ORDER != BIG_ENDIAN
850 if (ip
->ip_len
< hlen
) {
851 OSAddAtomic(1, &ipstat
.ips_badlen
);
855 #if BYTE_ORDER != BIG_ENDIAN
859 * Check that the amount of data in the buffers
860 * is as at least much as the IP header would have us expect.
861 * Trim mbufs if longer than we expect.
862 * Drop packet if shorter than we expect.
864 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
866 OSAddAtomic(1, &ipstat
.ips_tooshort
);
869 if (m
->m_pkthdr
.len
> ip
->ip_len
) {
870 /* Invalidate hwcksuming */
871 m
->m_pkthdr
.csum_flags
= 0;
872 m
->m_pkthdr
.csum_data
= 0;
874 if (m
->m_len
== m
->m_pkthdr
.len
) {
875 m
->m_len
= ip
->ip_len
;
876 m
->m_pkthdr
.len
= ip
->ip_len
;
878 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
882 /* Invoke inbound packet filter */
885 error
= pf_af_hook(m
->m_pkthdr
.rcvif
, NULL
, &m
, AF_INET
, TRUE
);
888 panic("%s: unexpected packet %p\n", __func__
, m
);
891 /* Already freed by callee */
894 ip
= mtod(m
, struct ip
*);
895 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
900 if (ipsec_bypass
== 0 && ipsec_gethist(m
, NULL
))
907 #endif /* DUMMYNET */
909 * Check if we want to allow this packet to be processed.
910 * Consider it to be bad if not.
915 if (fr_checkp(ip
, hlen
, m
->m_pkthdr
.rcvif
, 0, &m1
) || !m1
) {
918 ip
= mtod(m
= m1
, struct ip
*);
920 if (fw_enable
&& IPFW_LOADED
) {
921 #if IPFIREWALL_FORWARD
923 * If we've been forwarded from the output side, then
924 * skip the firewall a second time
928 #endif /* IPFIREWALL_FORWARD */
932 i
= ip_fw_chk_ptr(&args
);
935 if ( (i
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) { /* drop */
940 ip
= mtod(m
, struct ip
*); /* just in case m changed */
942 if (i
== 0 && args
.next_hop
== NULL
) { /* common case */
946 if (DUMMYNET_LOADED
&& (i
& IP_FW_PORT_DYNT_FLAG
) != 0) {
947 /* Send packet to the appropriate pipe */
948 ip_dn_io_ptr(m
, i
&0xffff, DN_TO_IP_IN
, &args
);
951 #endif /* DUMMYNET */
953 if (i
!= 0 && (i
& IP_FW_PORT_DYNT_FLAG
) == 0) {
954 /* Divert or tee packet */
959 #if IPFIREWALL_FORWARD
960 if (i
== 0 && args
.next_hop
!= NULL
) {
965 * if we get here, the packet must be dropped
970 #endif /* IPFIREWALL */
974 * Process options and, if not destined for us,
975 * ship it on. ip_dooptions returns 1 when an
976 * error was detected (causing an icmp message
977 * to be sent and the original packet to be freed).
979 ip_nhops
= 0; /* for source routed packets */
981 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, args
.next_hop
)) {
983 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, NULL
)) {
988 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
989 * matter if it is destined to another node, or whether it is
990 * a multicast one, RSVP wants it! and prevents it from being forwarded
991 * anywhere else. Also checks if the rsvp daemon is running before
992 * grabbing the packet.
994 if (rsvp_on
&& ip
->ip_p
==IPPROTO_RSVP
)
998 * Check our list of addresses, to see if the packet is for us.
999 * If we don't have any addresses, assume any unicast packet
1000 * we receive might be for us (and let the upper layers deal
1003 if (TAILQ_EMPTY(&in_ifaddrhead
) &&
1004 (m
->m_flags
& (M_MCAST
|M_BCAST
)) == 0)
1008 * Cache the destination address of the packet; this may be
1009 * changed by use of 'ipfw fwd'.
1012 pkt_dst
= args
.next_hop
== NULL
?
1013 ip
->ip_dst
: args
.next_hop
->sin_addr
;
1015 pkt_dst
= ip
->ip_dst
;
1019 * Enable a consistency check between the destination address
1020 * and the arrival interface for a unicast packet (the RFC 1122
1021 * strong ES model) if IP forwarding is disabled and the packet
1022 * is not locally generated and the packet is not subject to
1025 * XXX - Checking also should be disabled if the destination
1026 * address is ipnat'ed to a different interface.
1028 * XXX - Checking is incompatible with IP aliases added
1029 * to the loopback interface instead of the interface where
1030 * the packets are received.
1032 checkif
= ip_checkinterface
&& (ipforwarding
== 0) &&
1033 ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0)
1035 && (args
.next_hop
== NULL
);
1041 * Check for exact addresses in the hash bucket.
1043 lck_rw_lock_shared(in_ifaddr_rwlock
);
1044 TAILQ_FOREACH(ia
, INADDR_HASH(pkt_dst
.s_addr
), ia_hash
) {
1046 * If the address matches, verify that the packet
1047 * arrived via the correct interface if checking is
1050 IFA_LOCK_SPIN(&ia
->ia_ifa
);
1051 if (IA_SIN(ia
)->sin_addr
.s_addr
== pkt_dst
.s_addr
&&
1052 (!checkif
|| ia
->ia_ifp
== m
->m_pkthdr
.rcvif
)) {
1053 IFA_UNLOCK(&ia
->ia_ifa
);
1054 lck_rw_done(in_ifaddr_rwlock
);
1057 IFA_UNLOCK(&ia
->ia_ifa
);
1059 lck_rw_done(in_ifaddr_rwlock
);
1062 * Check for broadcast addresses.
1064 * Only accept broadcast packets that arrive via the matching
1065 * interface. Reception of forwarded directed broadcasts would be
1066 * handled via ip_forward() and ether_frameout() with the loopback
1067 * into the stack for SIMPLEX interfaces handled by ether_frameout().
1069 if (m
->m_pkthdr
.rcvif
->if_flags
& IFF_BROADCAST
) {
1071 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1073 ifnet_lock_shared(ifp
);
1074 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1076 if (ifa
->ifa_addr
->sa_family
!= AF_INET
) {
1081 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
1082 pkt_dst
.s_addr
|| ia
->ia_netbroadcast
.s_addr
==
1085 ifnet_lock_done(ifp
);
1090 ifnet_lock_done(ifp
);
1093 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
1094 struct in_multi
*inm
;
1095 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1099 * If we are acting as a multicast router, all
1100 * incoming multicast packets are passed to the
1101 * kernel-level multicast forwarding function.
1102 * The packet is returned (relatively) intact; if
1103 * ip_mforward() returns a non-zero value, the packet
1104 * must be discarded, else it may be accepted below.
1106 lck_mtx_lock(ip_mutex
);
1107 if (ip_mforward
&& ip_mforward(ip
, ifp
, m
, 0) != 0) {
1108 OSAddAtomic(1, &ipstat
.ips_cantforward
);
1110 lck_mtx_unlock(ip_mutex
);
1115 * The process-level routing daemon needs to receive
1116 * all multicast IGMP packets, whether or not this
1117 * host belongs to their destination groups.
1119 if (ip
->ip_p
== IPPROTO_IGMP
)
1121 OSAddAtomic(1, &ipstat
.ips_forward
);
1123 #endif /* MROUTING */
1125 * See if we belong to the destination multicast group on the
1126 * arrival interface.
1128 in_multihead_lock_shared();
1129 IN_LOOKUP_MULTI(&ip
->ip_dst
, ifp
, inm
);
1130 in_multihead_lock_done();
1132 OSAddAtomic(1, &ipstat
.ips_notmember
);
1139 if (ip
->ip_dst
.s_addr
== (u_int32_t
)INADDR_BROADCAST
)
1141 if (ip
->ip_dst
.s_addr
== INADDR_ANY
)
1144 /* Allow DHCP/BootP responses through */
1145 if (m
->m_pkthdr
.rcvif
!= NULL
1146 && (m
->m_pkthdr
.rcvif
->if_eflags
& IFEF_AUTOCONFIGURING
)
1147 && hlen
== sizeof(struct ip
)
1148 && ip
->ip_p
== IPPROTO_UDP
) {
1149 struct udpiphdr
*ui
;
1150 if (m
->m_len
< sizeof(struct udpiphdr
)
1151 && (m
= m_pullup(m
, sizeof(struct udpiphdr
))) == 0) {
1152 OSAddAtomic(1, &udpstat
.udps_hdrops
);
1155 ui
= mtod(m
, struct udpiphdr
*);
1156 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) {
1159 ip
= mtod(m
, struct ip
*); /* in case it changed */
1162 #if defined(NFAITH) && 0 < NFAITH
1164 * FAITH(Firewall Aided Internet Translator)
1166 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
== IFT_FAITH
) {
1168 if (ip
->ip_p
== IPPROTO_TCP
|| ip
->ip_p
== IPPROTO_ICMP
)
1176 * Not for us; forward if possible and desirable.
1178 if (ipforwarding
== 0) {
1179 OSAddAtomic(1, &ipstat
.ips_cantforward
);
1183 ip_forward(m
, 0, args
.next_hop
);
1185 ip_forward(m
, 0, NULL
);
1192 * If offset or IP_MF are set, must reassemble.
1193 * Otherwise, nothing need be done.
1194 * (We could look in the reassembly queue to see
1195 * if the packet was previously fragmented,
1196 * but it's not worth the time; just let them time out.)
1198 if (ip
->ip_off
& (IP_MF
| IP_OFFMASK
| IP_RF
)) {
1200 /* If maxnipq is 0, never accept fragments. */
1203 OSAddAtomic(1, &ipstat
.ips_fragments
);
1204 OSAddAtomic(1, &ipstat
.ips_fragdropped
);
1209 * If we will exceed the number of fragments in queues, timeout the
1210 * oldest fragemented packet to make space.
1212 lck_mtx_lock(ip_mutex
);
1213 if (currentfrags
>= maxfrags
) {
1214 fp
= TAILQ_LAST(&ipq_list
, ipq_list
);
1215 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragtimeout
);
1217 if (ip
->ip_id
== fp
->ipq_id
&&
1218 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
1219 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
1220 ip
->ip_p
== fp
->ipq_p
) {
1222 * If we match the fragment queue we were going to
1223 * discard, drop this packet too.
1225 OSAddAtomic(1, &ipstat
.ips_fragdropped
);
1227 lck_mtx_unlock(ip_mutex
);
1234 sum
= IPREASS_HASH(ip
->ip_src
.s_addr
, ip
->ip_id
);
1236 * Look for queue of fragments
1239 for (fp
= ipq
[sum
].next
; fp
!= &ipq
[sum
]; fp
= fp
->next
)
1240 if (ip
->ip_id
== fp
->ipq_id
&&
1241 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
1242 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
1244 mac_ipq_label_compare(m
, fp
) &&
1246 ip
->ip_p
== fp
->ipq_p
)
1250 * Enforce upper bound on number of fragmented packets
1251 * for which we attempt reassembly;
1252 * If maxnipq is -1, accept all fragments without limitation.
1254 if ((nipq
> maxnipq
) && (maxnipq
> 0)) {
1256 * drop the oldest fragment before proceeding further
1258 fp
= TAILQ_LAST(&ipq_list
, ipq_list
);
1259 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragtimeout
);
1267 * Adjust ip_len to not reflect header,
1268 * convert offset of this to bytes.
1271 if (ip
->ip_off
& IP_MF
) {
1273 * Make sure that fragments have a data length
1274 * that's a non-zero multiple of 8 bytes.
1276 if (ip
->ip_len
== 0 || (ip
->ip_len
& 0x7) != 0) {
1277 OSAddAtomic(1, &ipstat
.ips_toosmall
);
1278 lck_mtx_unlock(ip_mutex
);
1281 m
->m_flags
|= M_FRAG
;
1283 /* Clear the flag in case packet comes from loopback */
1284 m
->m_flags
&= ~M_FRAG
;
1289 * Attempt reassembly; if it succeeds, proceed.
1290 * ip_reass() will return a different mbuf, and update
1291 * the divert info in div_info and args.divert_rule.
1293 OSAddAtomic(1, &ipstat
.ips_fragments
);
1294 m
->m_pkthdr
.header
= ip
;
1296 m
= ip_reass(m
, fp
, &ipq
[sum
],
1297 (u_int16_t
*)&div_info
, &args
.divert_rule
);
1299 m
= ip_reass(m
, fp
, &ipq
[sum
]);
1302 lck_mtx_unlock(ip_mutex
);
1305 OSAddAtomic(1, &ipstat
.ips_reassembled
);
1306 ip
= mtod(m
, struct ip
*);
1307 /* Get the header length of the reassembled packet */
1308 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1311 /* Restore original checksum before diverting packet */
1312 if (div_info
!= 0) {
1315 #if BYTE_ORDER != BIG_ENDIAN
1321 ip
->ip_sum
= in_cksum(m
, hlen
);
1323 #if BYTE_ORDER != BIG_ENDIAN
1331 lck_mtx_unlock(ip_mutex
);
1337 * Divert or tee packet to the divert protocol if required.
1339 * If div_info is zero then cookie should be too, so we shouldn't
1340 * need to clear them here. Assume divert_packet() does so also.
1342 if (div_info
!= 0) {
1343 struct mbuf
*clone
= NULL
;
1345 /* Clone packet if we're doing a 'tee' */
1346 if ((div_info
& IP_FW_PORT_TEE_FLAG
) != 0)
1347 clone
= m_dup(m
, M_DONTWAIT
);
1349 /* Restore packet header fields to original values */
1352 #if BYTE_ORDER != BIG_ENDIAN
1356 /* Deliver packet to divert input routine */
1357 OSAddAtomic(1, &ipstat
.ips_delivered
);
1358 divert_packet(m
, 1, div_info
& 0xffff, args
.divert_rule
);
1360 /* If 'tee', continue with original packet */
1361 if (clone
== NULL
) {
1365 ip
= mtod(m
, struct ip
*);
1371 * enforce IPsec policy checking if we are seeing last header.
1372 * note that we do not visit this with protocols with pcb layer
1373 * code - like udp/tcp/raw ip.
1375 if (ipsec_bypass
== 0 && (ip_protox
[ip
->ip_p
]->pr_flags
& PR_LASTHDR
) != 0) {
1376 if (ipsec4_in_reject(m
, NULL
)) {
1377 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
1384 * Switch out to protocol's input routine.
1386 OSAddAtomic(1, &ipstat
.ips_delivered
);
1389 if (args
.next_hop
&& ip
->ip_p
== IPPROTO_TCP
) {
1390 /* TCP needs IPFORWARD info if available */
1391 struct m_tag
*fwd_tag
;
1392 struct ip_fwd_tag
*ipfwd_tag
;
1394 fwd_tag
= m_tag_create(KERNEL_MODULE_TAG_ID
,
1395 KERNEL_TAG_TYPE_IPFORWARD
, sizeof (*ipfwd_tag
),
1397 if (fwd_tag
== NULL
) {
1401 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
1402 ipfwd_tag
->next_hop
= args
.next_hop
;
1404 m_tag_prepend(m
, fwd_tag
);
1406 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1407 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1410 /* TCP deals with its own locking */
1411 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1413 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1414 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1416 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1419 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1425 KERNEL_DEBUG(DBG_LAYER_END
, 0,0,0,0,0);
1430 * Take incoming datagram fragment and try to reassemble it into
1431 * whole datagram. If a chain for reassembly of this datagram already
1432 * exists, then it is given as fp; otherwise have to make a chain.
1434 * When IPDIVERT enabled, keep additional state with each packet that
1435 * tells us if we need to divert or tee the packet we're building.
1438 static struct mbuf
*
1440 ip_reass(struct mbuf
*m
, struct ipq
*fp
, struct ipq
*where
,
1443 #else /* IPDIVERT_44 */
1445 #endif /* IPDIVERT_44 */
1446 u_int16_t
*divcookie
)
1447 #else /* IPDIVERT */
1448 ip_reass(struct mbuf
*m
, struct ipq
*fp
, struct ipq
*where
)
1449 #endif /* IPDIVERT */
1451 struct ip
*ip
= mtod(m
, struct ip
*);
1452 struct mbuf
*p
= 0, *q
, *nq
;
1454 int hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1458 lck_mtx_assert(ip_mutex
, LCK_MTX_ASSERT_OWNED
);
1460 * Presence of header sizes in mbufs
1461 * would confuse code below.
1466 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1467 m
->m_pkthdr
.csum_flags
= 0;
1469 * If first fragment to arrive, create a reassembly queue.
1472 if ((t
= m_get(M_DONTWAIT
, MT_FTABLE
)) == NULL
)
1474 fp
= mtod(t
, struct ipq
*);
1476 if (mac_ipq_label_init(fp
, M_NOWAIT
) != 0) {
1481 mac_ipq_label_associate(m
, fp
);
1483 insque((void*)fp
, (void*)where
);
1486 fp
->ipq_ttl
= IPFRAGTTL
;
1487 fp
->ipq_p
= ip
->ip_p
;
1488 fp
->ipq_id
= ip
->ip_id
;
1489 fp
->ipq_src
= ip
->ip_src
;
1490 fp
->ipq_dst
= ip
->ip_dst
;
1492 m
->m_nextpkt
= NULL
;
1495 fp
->ipq_div_info
= 0;
1499 fp
->ipq_div_cookie
= 0;
1501 TAILQ_INSERT_HEAD(&ipq_list
, fp
, ipq_list
);
1506 mac_ipq_label_update(m
, fp
);
1510 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
1513 * Handle ECN by comparing this segment with the first one;
1514 * if CE is set, do not lose CE.
1515 * drop if CE and not-ECT are mixed for the same packet.
1517 ecn
= ip
->ip_tos
& IPTOS_ECN_MASK
;
1518 ecn0
= GETIP(fp
->ipq_frags
)->ip_tos
& IPTOS_ECN_MASK
;
1519 if (ecn
== IPTOS_ECN_CE
) {
1520 if (ecn0
== IPTOS_ECN_NOTECT
)
1522 if (ecn0
!= IPTOS_ECN_CE
)
1523 GETIP(fp
->ipq_frags
)->ip_tos
|= IPTOS_ECN_CE
;
1525 if (ecn
== IPTOS_ECN_NOTECT
&& ecn0
!= IPTOS_ECN_NOTECT
)
1529 * Find a segment which begins after this one does.
1531 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
)
1532 if (GETIP(q
)->ip_off
> ip
->ip_off
)
1536 * If there is a preceding segment, it may provide some of
1537 * our data already. If so, drop the data from the incoming
1538 * segment. If it provides all of our data, drop us, otherwise
1539 * stick new segment in the proper place.
1541 * If some of the data is dropped from the the preceding
1542 * segment, then it's checksum is invalidated.
1545 i
= GETIP(p
)->ip_off
+ GETIP(p
)->ip_len
- ip
->ip_off
;
1547 if (i
>= ip
->ip_len
)
1550 m
->m_pkthdr
.csum_flags
= 0;
1554 m
->m_nextpkt
= p
->m_nextpkt
;
1557 m
->m_nextpkt
= fp
->ipq_frags
;
1562 * While we overlap succeeding segments trim them or,
1563 * if they are completely covered, dequeue them.
1565 for (; q
!= NULL
&& ip
->ip_off
+ ip
->ip_len
> GETIP(q
)->ip_off
;
1567 i
= (ip
->ip_off
+ ip
->ip_len
) -
1569 if (i
< GETIP(q
)->ip_len
) {
1570 GETIP(q
)->ip_len
-= i
;
1571 GETIP(q
)->ip_off
+= i
;
1573 q
->m_pkthdr
.csum_flags
= 0;
1578 OSAddAtomic(1, &ipstat
.ips_fragdropped
);
1588 * Transfer firewall instructions to the fragment structure.
1589 * Only trust info in the fragment at offset 0.
1591 if (ip
->ip_off
== 0) {
1593 fp
->ipq_div_info
= *divinfo
;
1595 fp
->ipq_divert
= *divinfo
;
1597 fp
->ipq_div_cookie
= *divcookie
;
1604 * Check for complete reassembly and perform frag per packet
1607 * Frag limiting is performed here so that the nth frag has
1608 * a chance to complete the packet before we drop the packet.
1609 * As a result, n+1 frags are actually allowed per packet, but
1610 * only n will ever be stored. (n = maxfragsperpacket.)
1614 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
) {
1615 if (GETIP(q
)->ip_off
!= next
) {
1616 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
1617 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragdropped
);
1622 next
+= GETIP(q
)->ip_len
;
1624 /* Make sure the last packet didn't have the IP_MF flag */
1625 if (p
->m_flags
& M_FRAG
) {
1626 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
1627 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragdropped
);
1634 * Reassembly is complete. Make sure the packet is a sane size.
1638 if (next
+ (IP_VHL_HL(ip
->ip_vhl
) << 2) > IP_MAXPACKET
) {
1639 OSAddAtomic(1, &ipstat
.ips_toolong
);
1640 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragdropped
);
1646 * Concatenate fragments.
1654 for (q
= nq
; q
!= NULL
; q
= nq
) {
1656 q
->m_nextpkt
= NULL
;
1657 if (q
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1658 m
->m_pkthdr
.csum_flags
= 0;
1660 m
->m_pkthdr
.csum_flags
&= q
->m_pkthdr
.csum_flags
;
1661 m
->m_pkthdr
.csum_data
+= q
->m_pkthdr
.csum_data
;
1668 * Extract firewall instructions from the fragment structure.
1671 *divinfo
= fp
->ipq_div_info
;
1673 *divinfo
= fp
->ipq_divert
;
1675 *divcookie
= fp
->ipq_div_cookie
;
1679 mac_mbuf_label_associate_ipq(fp
, m
);
1680 mac_ipq_label_destroy(fp
);
1683 * Create header for new ip packet by
1684 * modifying header of first packet;
1685 * dequeue and discard fragment reassembly header.
1686 * Make header visible.
1689 ip
->ip_src
= fp
->ipq_src
;
1690 ip
->ip_dst
= fp
->ipq_dst
;
1692 TAILQ_REMOVE(&ipq_list
, fp
, ipq_list
);
1693 currentfrags
-= fp
->ipq_nfrags
;
1695 (void) m_free(dtom(fp
));
1696 m
->m_len
+= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1697 m
->m_data
-= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1698 /* some debugging cruft by sklower, below, will go away soon */
1699 if (m
->m_flags
& M_PKTHDR
) { /* XXX this should be done elsewhere */
1701 for (t
= m
; t
; t
= t
->m_next
)
1703 m
->m_pkthdr
.len
= plen
;
1712 OSAddAtomic(1, &ipstat
.ips_fragdropped
);
1722 * Free a fragment reassembly header and all
1723 * associated datagrams.
1726 ip_freef(struct ipq
*fp
)
1728 lck_mtx_assert(ip_mutex
, LCK_MTX_ASSERT_OWNED
);
1729 currentfrags
-= fp
->ipq_nfrags
;
1730 m_freem_list(fp
->ipq_frags
);
1732 TAILQ_REMOVE(&ipq_list
, fp
, ipq_list
);
1733 (void) m_free(dtom(fp
));
1738 * IP timer processing;
1739 * if a timer expires on a reassembly
1740 * queue, discard it.
1747 lck_mtx_lock(ip_mutex
);
1748 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1752 while (fp
!= &ipq
[i
]) {
1755 if (fp
->prev
->ipq_ttl
== 0) {
1756 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragtimeout
);
1762 * If we are over the maximum number of fragments
1763 * (due to the limit being lowered), drain off
1764 * enough to get down to the new limit.
1766 if (maxnipq
>= 0 && nipq
> maxnipq
) {
1767 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1768 while (nipq
> maxnipq
&&
1769 (ipq
[i
].next
!= &ipq
[i
])) {
1770 OSAddAtomic(ipq
[i
].next
->ipq_nfrags
, &ipstat
.ips_fragdropped
);
1771 ip_freef(ipq
[i
].next
);
1775 lck_mtx_unlock(ip_mutex
);
1779 * Drain off all datagram fragments.
1786 lck_mtx_lock(ip_mutex
);
1787 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1788 while (ipq
[i
].next
!= &ipq
[i
]) {
1789 OSAddAtomic(ipq
[i
].next
->ipq_nfrags
, &ipstat
.ips_fragdropped
);
1790 ip_freef(ipq
[i
].next
);
1793 lck_mtx_unlock(ip_mutex
);
1798 * Do option processing on a datagram,
1799 * possibly discarding it if bad options are encountered,
1800 * or forwarding it if source-routed.
1801 * The pass argument is used when operating in the IPSTEALTH
1802 * mode to tell what options to process:
1803 * [LS]SRR (pass 0) or the others (pass 1).
1804 * The reason for as many as two passes is that when doing IPSTEALTH,
1805 * non-routing options should be processed only if the packet is for us.
1806 * Returns 1 if packet has been forwarded/freed,
1807 * 0 if the packet should be processed further.
1810 ip_dooptions(struct mbuf
*m
, __unused
int pass
, struct sockaddr_in
*next_hop
)
1812 struct ip
*ip
= mtod(m
, struct ip
*);
1814 struct ip_timestamp
*ipt
;
1815 struct in_ifaddr
*ia
;
1816 int opt
, optlen
, cnt
, off
, code
, type
= ICMP_PARAMPROB
, forward
= 0;
1817 struct in_addr
*sin
, dst
;
1819 struct sockaddr_in ipaddr
= {
1820 sizeof (ipaddr
), AF_INET
, 0 , { 0 }, { 0, } };
1823 cp
= (u_char
*)(ip
+ 1);
1824 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1825 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1826 opt
= cp
[IPOPT_OPTVAL
];
1827 if (opt
== IPOPT_EOL
)
1829 if (opt
== IPOPT_NOP
)
1832 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
)) {
1833 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1836 optlen
= cp
[IPOPT_OLEN
];
1837 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
) {
1838 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1848 * Source routing with record.
1849 * Find interface with current destination address.
1850 * If none on this machine then drop if strictly routed,
1851 * or do nothing if loosely routed.
1852 * Record interface address and bring up next address
1853 * component. If strictly routed make sure next
1854 * address is on directly accessible net.
1858 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1859 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1862 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1863 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1866 ipaddr
.sin_addr
= ip
->ip_dst
;
1867 ia
= (struct in_ifaddr
*)
1868 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
1870 if (opt
== IPOPT_SSRR
) {
1871 type
= ICMP_UNREACH
;
1872 code
= ICMP_UNREACH_SRCFAIL
;
1875 if (!ip_dosourceroute
)
1876 goto nosourcerouting
;
1878 * Loose routing, and not at next destination
1879 * yet; nothing to do except forward.
1884 IFA_REMREF(&ia
->ia_ifa
);
1887 off
--; /* 0 origin */
1888 if (off
> optlen
- (int)sizeof(struct in_addr
)) {
1890 * End of source route. Should be for us.
1892 if (!ip_acceptsourceroute
)
1893 goto nosourcerouting
;
1894 save_rte(cp
, ip
->ip_src
);
1898 if (!ip_dosourceroute
) {
1900 char buf
[MAX_IPv4_STR_LEN
];
1901 char buf2
[MAX_IPv4_STR_LEN
];
1903 * Acting as a router, so generate ICMP
1907 "attempted source route from %s to %s\n",
1908 inet_ntop(AF_INET
, &ip
->ip_src
, buf
, sizeof(buf
)),
1909 inet_ntop(AF_INET
, &ip
->ip_dst
, buf2
, sizeof(buf2
)));
1910 type
= ICMP_UNREACH
;
1911 code
= ICMP_UNREACH_SRCFAIL
;
1915 * Not acting as a router, so silently drop.
1917 OSAddAtomic(1, &ipstat
.ips_cantforward
);
1924 * locate outgoing interface
1926 (void)memcpy(&ipaddr
.sin_addr
, cp
+ off
,
1927 sizeof(ipaddr
.sin_addr
));
1929 if (opt
== IPOPT_SSRR
) {
1930 #define INA struct in_ifaddr *
1931 #define SA struct sockaddr *
1932 if ((ia
= (INA
)ifa_ifwithdstaddr((SA
)&ipaddr
)) == 0) {
1933 ia
= (INA
)ifa_ifwithnet((SA
)&ipaddr
);
1936 ia
= ip_rtaddr(ipaddr
.sin_addr
);
1939 type
= ICMP_UNREACH
;
1940 code
= ICMP_UNREACH_SRCFAIL
;
1943 ip
->ip_dst
= ipaddr
.sin_addr
;
1944 IFA_LOCK(&ia
->ia_ifa
);
1945 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1946 sizeof(struct in_addr
));
1947 IFA_UNLOCK(&ia
->ia_ifa
);
1948 IFA_REMREF(&ia
->ia_ifa
);
1950 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1952 * Let ip_intr's mcast routing check handle mcast pkts
1954 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
1958 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1959 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1962 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1963 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1967 * If no space remains, ignore.
1969 off
--; /* 0 origin */
1970 if (off
> optlen
- (int)sizeof(struct in_addr
))
1972 (void)memcpy(&ipaddr
.sin_addr
, &ip
->ip_dst
,
1973 sizeof(ipaddr
.sin_addr
));
1975 * locate outgoing interface; if we're the destination,
1976 * use the incoming interface (should be same).
1978 if ((ia
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0) {
1979 if ((ia
= ip_rtaddr(ipaddr
.sin_addr
)) == 0) {
1980 type
= ICMP_UNREACH
;
1981 code
= ICMP_UNREACH_HOST
;
1985 IFA_LOCK(&ia
->ia_ifa
);
1986 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1987 sizeof(struct in_addr
));
1988 IFA_UNLOCK(&ia
->ia_ifa
);
1989 IFA_REMREF(&ia
->ia_ifa
);
1991 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1995 code
= cp
- (u_char
*)ip
;
1996 ipt
= (struct ip_timestamp
*)cp
;
1997 if (ipt
->ipt_len
< 4 || ipt
->ipt_len
> 40) {
1998 code
= (u_char
*)&ipt
->ipt_len
- (u_char
*)ip
;
2001 if (ipt
->ipt_ptr
< 5) {
2002 code
= (u_char
*)&ipt
->ipt_ptr
- (u_char
*)ip
;
2006 ipt
->ipt_len
- (int)sizeof(int32_t)) {
2007 if (++ipt
->ipt_oflw
== 0) {
2008 code
= (u_char
*)&ipt
->ipt_ptr
-
2014 sin
= (struct in_addr
*)(cp
+ ipt
->ipt_ptr
- 1);
2015 switch (ipt
->ipt_flg
) {
2017 case IPOPT_TS_TSONLY
:
2020 case IPOPT_TS_TSANDADDR
:
2021 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
2022 sizeof(struct in_addr
) > ipt
->ipt_len
) {
2023 code
= (u_char
*)&ipt
->ipt_ptr
-
2027 ipaddr
.sin_addr
= dst
;
2028 ia
= (INA
)ifaof_ifpforaddr((SA
)&ipaddr
,
2032 IFA_LOCK(&ia
->ia_ifa
);
2033 (void)memcpy(sin
, &IA_SIN(ia
)->sin_addr
,
2034 sizeof(struct in_addr
));
2035 IFA_UNLOCK(&ia
->ia_ifa
);
2036 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
2037 IFA_REMREF(&ia
->ia_ifa
);
2041 case IPOPT_TS_PRESPEC
:
2042 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
2043 sizeof(struct in_addr
) > ipt
->ipt_len
) {
2044 code
= (u_char
*)&ipt
->ipt_ptr
-
2048 (void)memcpy(&ipaddr
.sin_addr
, sin
,
2049 sizeof(struct in_addr
));
2050 if ((ia
= (struct in_ifaddr
*)ifa_ifwithaddr((SA
)&ipaddr
)) == 0)
2052 IFA_REMREF(&ia
->ia_ifa
);
2054 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
2058 /* XXX can't take &ipt->ipt_flg */
2059 code
= (u_char
*)&ipt
->ipt_ptr
-
2064 (void)memcpy(cp
+ ipt
->ipt_ptr
- 1, &ntime
,
2066 ipt
->ipt_ptr
+= sizeof(n_time
);
2069 if (forward
&& ipforwarding
) {
2070 ip_forward(m
, 1, next_hop
);
2075 ip
->ip_len
-= IP_VHL_HL(ip
->ip_vhl
) << 2; /* XXX icmp_error adds in hdr length */
2076 icmp_error(m
, type
, code
, 0, 0);
2077 OSAddAtomic(1, &ipstat
.ips_badoptions
);
2082 * Given address of next destination (final or next hop),
2083 * return internet address info of interface to be used to get there.
2086 ip_rtaddr(struct in_addr dst
)
2088 struct sockaddr_in
*sin
;
2089 struct ifaddr
*rt_ifa
;
2092 bzero(&ro
, sizeof (ro
));
2093 sin
= (struct sockaddr_in
*)&ro
.ro_dst
;
2094 sin
->sin_family
= AF_INET
;
2095 sin
->sin_len
= sizeof (*sin
);
2096 sin
->sin_addr
= dst
;
2098 rtalloc_ign(&ro
, RTF_PRCLONING
);
2099 if (ro
.ro_rt
== NULL
)
2103 if ((rt_ifa
= ro
.ro_rt
->rt_ifa
) != NULL
)
2105 RT_UNLOCK(ro
.ro_rt
);
2108 return ((struct in_ifaddr
*)rt_ifa
);
2112 * Save incoming source route for use in replies,
2113 * to be picked up later by ip_srcroute if the receiver is interested.
2116 save_rte(u_char
*option
, struct in_addr dst
)
2120 olen
= option
[IPOPT_OLEN
];
2123 printf("save_rte: olen %d\n", olen
);
2125 if (olen
> sizeof(ip_srcrt
) - (1 + sizeof(dst
)))
2127 bcopy(option
, ip_srcrt
.srcopt
, olen
);
2128 ip_nhops
= (olen
- IPOPT_OFFSET
- 1) / sizeof(struct in_addr
);
2133 * Retrieve incoming source route for use in replies,
2134 * in the same form used by setsockopt.
2135 * The first hop is placed before the options, will be removed later.
2140 struct in_addr
*p
, *q
;
2144 return ((struct mbuf
*)0);
2145 m
= m_get(M_DONTWAIT
, MT_HEADER
);
2147 return ((struct mbuf
*)0);
2149 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
2151 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
2152 m
->m_len
= ip_nhops
* sizeof(struct in_addr
) + sizeof(struct in_addr
) +
2156 printf("ip_srcroute: nhops %d mlen %d", ip_nhops
, m
->m_len
);
2160 * First save first hop for return route
2162 p
= &ip_srcrt
.route
[ip_nhops
- 1];
2163 *(mtod(m
, struct in_addr
*)) = *p
--;
2166 printf(" hops %lx", (u_int32_t
)ntohl(mtod(m
, struct in_addr
*)->s_addr
));
2170 * Copy option fields and padding (nop) to mbuf.
2172 ip_srcrt
.nop
= IPOPT_NOP
;
2173 ip_srcrt
.srcopt
[IPOPT_OFFSET
] = IPOPT_MINOFF
;
2174 (void)memcpy(mtod(m
, caddr_t
) + sizeof(struct in_addr
),
2175 &ip_srcrt
.nop
, OPTSIZ
);
2176 q
= (struct in_addr
*)(mtod(m
, caddr_t
) +
2177 sizeof(struct in_addr
) + OPTSIZ
);
2180 * Record return path as an IP source route,
2181 * reversing the path (pointers are now aligned).
2183 while (p
>= ip_srcrt
.route
) {
2186 printf(" %lx", (u_int32_t
)ntohl(q
->s_addr
));
2191 * Last hop goes to final destination.
2196 printf(" %lx\n", (u_int32_t
)ntohl(q
->s_addr
));
2202 * Strip out IP options, at higher
2203 * level protocol in the kernel.
2204 * Second argument is buffer to which options
2205 * will be moved, and return value is their length.
2206 * XXX should be deleted; last arg currently ignored.
2209 ip_stripoptions(struct mbuf
*m
, __unused
struct mbuf
*mopt
)
2212 struct ip
*ip
= mtod(m
, struct ip
*);
2216 olen
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
2217 opts
= (caddr_t
)(ip
+ 1);
2218 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
2219 bcopy(opts
+ olen
, opts
, (unsigned)i
);
2221 if (m
->m_flags
& M_PKTHDR
)
2222 m
->m_pkthdr
.len
-= olen
;
2223 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
2226 u_char inetctlerrmap
[PRC_NCMDS
] = {
2228 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
2229 ENETUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
2230 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
2232 ENOPROTOOPT
, ECONNREFUSED
2236 sysctl_ipforwarding SYSCTL_HANDLER_ARGS
2238 #pragma unused(arg1, arg2)
2239 int i
, was_ipforwarding
= ipforwarding
;
2241 i
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
2242 if (i
!= 0 || req
->newptr
== USER_ADDR_NULL
)
2245 if (was_ipforwarding
&& !ipforwarding
) {
2246 /* clean up IPv4 forwarding cached routes */
2247 ifnet_head_lock_shared();
2248 for (i
= 0; i
<= if_index
; i
++) {
2249 struct ifnet
*ifp
= ifindex2ifnet
[i
];
2251 lck_mtx_lock(&ifp
->if_cached_route_lock
);
2252 if (ifp
->if_fwd_route
.ro_rt
!= NULL
)
2253 rtfree(ifp
->if_fwd_route
.ro_rt
);
2254 bzero(&ifp
->if_fwd_route
,
2255 sizeof (ifp
->if_fwd_route
));
2256 lck_mtx_unlock(&ifp
->if_cached_route_lock
);
2266 * Similar to inp_route_{copyout,copyin} routines except that these copy
2267 * out the cached IPv4 forwarding route from struct ifnet instead of the
2268 * inpcb. See comments for those routines for explanations.
2271 ip_fwd_route_copyout(struct ifnet
*ifp
, struct route
*dst
)
2273 struct route
*src
= &ifp
->if_fwd_route
;
2275 lck_mtx_lock_spin(&ifp
->if_cached_route_lock
);
2276 lck_mtx_convert_spin(&ifp
->if_cached_route_lock
);
2278 /* Minor sanity check */
2279 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET
)
2280 panic("%s: wrong or corrupted route: %p", __func__
, src
);
2282 route_copyout(dst
, src
, sizeof(*dst
));
2284 lck_mtx_unlock(&ifp
->if_cached_route_lock
);
2288 ip_fwd_route_copyin(struct ifnet
*ifp
, struct route
*src
)
2290 struct route
*dst
= &ifp
->if_fwd_route
;
2292 lck_mtx_lock_spin(&ifp
->if_cached_route_lock
);
2293 lck_mtx_convert_spin(&ifp
->if_cached_route_lock
);
2295 /* Minor sanity check */
2296 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET
)
2297 panic("%s: wrong or corrupted route: %p", __func__
, src
);
2299 if (ifp
->if_fwd_cacheok
)
2300 route_copyin(src
, dst
, sizeof(*src
));
2302 lck_mtx_unlock(&ifp
->if_cached_route_lock
);
2306 * Forward a packet. If some error occurs return the sender
2307 * an icmp packet. Note we can't always generate a meaningful
2308 * icmp message because icmp doesn't have a large enough repertoire
2309 * of codes and types.
2311 * If not forwarding, just drop the packet. This could be confusing
2312 * if ipforwarding was zero but some routing protocol was advancing
2313 * us as a gateway to somewhere. However, we must let the routing
2314 * protocol deal with that.
2316 * The srcrt parameter indicates whether the packet is being forwarded
2317 * via a source route.
2320 ip_forward(struct mbuf
*m
, int srcrt
, struct sockaddr_in
*next_hop
)
2323 #pragma unused(next_hop)
2325 struct ip
*ip
= mtod(m
, struct ip
*);
2326 struct sockaddr_in
*sin
;
2328 struct route fwd_rt
;
2329 int error
, type
= 0, code
= 0;
2332 struct in_addr pkt_dst
;
2333 u_int32_t nextmtu
= 0;
2334 struct ip_out_args ipoa
= { IFSCOPE_NONE
, 0 };
2335 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
2337 struct pf_mtag
*pf_mtag
;
2343 * Cache the destination address of the packet; this may be
2344 * changed by use of 'ipfw fwd'.
2346 pkt_dst
= next_hop
? next_hop
->sin_addr
: ip
->ip_dst
;
2348 pkt_dst
= ip
->ip_dst
;
2353 printf("forward: src %lx dst %lx ttl %x\n",
2354 (u_int32_t
)ip
->ip_src
.s_addr
, (u_int32_t
)pkt_dst
.s_addr
,
2358 if (m
->m_flags
& (M_BCAST
|M_MCAST
) || in_canforward(pkt_dst
) == 0) {
2359 OSAddAtomic(1, &ipstat
.ips_cantforward
);
2366 if (ip
->ip_ttl
<= IPTTLDEC
) {
2367 icmp_error(m
, ICMP_TIMXCEED
, ICMP_TIMXCEED_INTRANS
,
2376 pf_mtag
= pf_find_mtag(m
);
2377 if (pf_mtag
!= NULL
&& pf_mtag
->rtableid
!= IFSCOPE_NONE
)
2378 ipoa
.ipoa_boundif
= pf_mtag
->rtableid
;
2381 ip_fwd_route_copyout(ifp
, &fwd_rt
);
2383 sin
= (struct sockaddr_in
*)&fwd_rt
.ro_dst
;
2384 if (fwd_rt
.ro_rt
== NULL
||
2385 fwd_rt
.ro_rt
->generation_id
!= route_generation
||
2386 pkt_dst
.s_addr
!= sin
->sin_addr
.s_addr
) {
2387 if (fwd_rt
.ro_rt
!= NULL
) {
2388 rtfree(fwd_rt
.ro_rt
);
2389 fwd_rt
.ro_rt
= NULL
;
2391 sin
->sin_family
= AF_INET
;
2392 sin
->sin_len
= sizeof (*sin
);
2393 sin
->sin_addr
= pkt_dst
;
2395 rtalloc_scoped_ign(&fwd_rt
, RTF_PRCLONING
, ipoa
.ipoa_boundif
);
2396 if (fwd_rt
.ro_rt
== NULL
) {
2397 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_HOST
, dest
, 0);
2404 * Save the IP header and at most 8 bytes of the payload,
2405 * in case we need to generate an ICMP message to the src.
2407 * We don't use m_copy() because it might return a reference
2408 * to a shared cluster. Both this function and ip_output()
2409 * assume exclusive access to the IP header in `m', so any
2410 * data in a cluster may change before we reach icmp_error().
2412 MGET(mcopy
, M_DONTWAIT
, m
->m_type
);
2413 if (mcopy
!= NULL
) {
2414 M_COPY_PKTHDR(mcopy
, m
);
2415 mcopy
->m_len
= imin((IP_VHL_HL(ip
->ip_vhl
) << 2) + 8,
2417 m_copydata(m
, 0, mcopy
->m_len
, mtod(mcopy
, caddr_t
));
2423 ip
->ip_ttl
-= IPTTLDEC
;
2429 * If forwarding packet using same interface that it came in on,
2430 * perhaps should send a redirect to sender to shortcut a hop.
2431 * Only send redirect if source is sending directly to us,
2432 * and if packet was not source routed (or has any options).
2433 * Also, don't send redirect if forwarding using a default route
2434 * or a route modified by a redirect.
2437 if (rt
->rt_ifp
== m
->m_pkthdr
.rcvif
&&
2438 (rt
->rt_flags
& (RTF_DYNAMIC
|RTF_MODIFIED
)) == 0 &&
2439 satosin(rt_key(rt
))->sin_addr
.s_addr
!= 0 &&
2440 ipsendredirects
&& !srcrt
&& rt
->rt_ifa
!= NULL
) {
2441 struct in_ifaddr
*ia
= (struct in_ifaddr
*)rt
->rt_ifa
;
2442 u_int32_t src
= ntohl(ip
->ip_src
.s_addr
);
2444 /* Become a regular mutex */
2445 RT_CONVERT_LOCK(rt
);
2446 IFA_LOCK_SPIN(&ia
->ia_ifa
);
2447 if ((src
& ia
->ia_subnetmask
) == ia
->ia_subnet
) {
2448 if (rt
->rt_flags
& RTF_GATEWAY
)
2449 dest
= satosin(rt
->rt_gateway
)->sin_addr
.s_addr
;
2451 dest
= pkt_dst
.s_addr
;
2452 /* Router requirements says to only send host redirects */
2453 type
= ICMP_REDIRECT
;
2454 code
= ICMP_REDIRECT_HOST
;
2457 printf("redirect (%d) to %lx\n", code
, (u_int32_t
)dest
);
2460 IFA_UNLOCK(&ia
->ia_ifa
);
2466 /* Pass IPFORWARD info if available */
2468 struct ip_fwd_tag
*ipfwd_tag
;
2470 tag
= m_tag_create(KERNEL_MODULE_TAG_ID
,
2471 KERNEL_TAG_TYPE_IPFORWARD
,
2472 sizeof (*ipfwd_tag
), M_NOWAIT
, m
);
2479 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
2480 ipfwd_tag
->next_hop
= next_hop
;
2482 m_tag_prepend(m
, tag
);
2485 error
= ip_output_list(m
, 0, NULL
, &fwd_rt
,
2486 IP_FORWARDING
| IP_OUTARGS
, 0, &ipoa
);
2488 /* Refresh rt since the route could have changed while in IP */
2492 OSAddAtomic(1, &ipstat
.ips_cantforward
);
2494 OSAddAtomic(1, &ipstat
.ips_forward
);
2496 OSAddAtomic(1, &ipstat
.ips_redirectsent
);
2500 * If we didn't have to go thru ipflow and
2501 * the packet was successfully consumed by
2502 * ip_output, the mcopy is rather a waste;
2503 * this could be further optimized.
2515 case 0: /* forwarded, but need redirect */
2516 /* type, code set above */
2519 case ENETUNREACH
: /* shouldn't happen, checked above */
2524 type
= ICMP_UNREACH
;
2525 code
= ICMP_UNREACH_HOST
;
2529 type
= ICMP_UNREACH
;
2530 code
= ICMP_UNREACH_NEEDFRAG
;
2534 if (rt
->rt_ifp
!= NULL
)
2535 nextmtu
= rt
->rt_ifp
->if_mtu
;
2540 * If the packet is routed over IPsec tunnel, tell the
2541 * originator the tunnel MTU.
2542 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
2546 struct secpolicy
*sp
= NULL
;
2552 if (rt
->rt_ifp
!= NULL
)
2553 nextmtu
= rt
->rt_ifp
->if_mtu
;
2557 OSAddAtomic(1, &ipstat
.ips_cantfrag
);
2560 sp
= ipsec4_getpolicybyaddr(mcopy
,
2566 /* count IPsec header size */
2567 ipsechdr
= ipsec_hdrsiz(sp
);
2570 * find the correct route for outer IPv4
2571 * header, compute tunnel MTU.
2575 if (sp
->req
!= NULL
) {
2576 if (sp
->req
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2577 struct secasindex saidx
;
2579 struct secasvar
*sav
;
2581 ipm
= mtod(mcopy
, struct ip
*);
2582 bcopy(&sp
->req
->saidx
, &saidx
, sizeof(saidx
));
2583 saidx
.mode
= sp
->req
->saidx
.mode
;
2584 saidx
.reqid
= sp
->req
->saidx
.reqid
;
2585 sin
= (struct sockaddr_in
*)&saidx
.src
;
2586 if (sin
->sin_len
== 0) {
2587 sin
->sin_len
= sizeof(*sin
);
2588 sin
->sin_family
= AF_INET
;
2589 sin
->sin_port
= IPSEC_PORT_ANY
;
2590 bcopy(&ipm
->ip_src
, &sin
->sin_addr
,
2591 sizeof(sin
->sin_addr
));
2593 sin
= (struct sockaddr_in
*)&saidx
.dst
;
2594 if (sin
->sin_len
== 0) {
2595 sin
->sin_len
= sizeof(*sin
);
2596 sin
->sin_family
= AF_INET
;
2597 sin
->sin_port
= IPSEC_PORT_ANY
;
2598 bcopy(&ipm
->ip_dst
, &sin
->sin_addr
,
2599 sizeof(sin
->sin_addr
));
2601 sav
= key_allocsa_policy(&saidx
);
2603 lck_mtx_lock(sadb_mutex
);
2604 if (sav
->sah
!= NULL
) {
2605 ro
= &sav
->sah
->sa_route
;
2606 if (ro
->ro_rt
!= NULL
) {
2608 if (ro
->ro_rt
->rt_ifp
!= NULL
) {
2609 nextmtu
= ro
->ro_rt
->rt_ifp
->if_mtu
;
2610 nextmtu
-= ipsechdr
;
2612 RT_UNLOCK(ro
->ro_rt
);
2615 key_freesav(sav
, KEY_SADB_LOCKED
);
2616 lck_mtx_unlock(sadb_mutex
);
2620 key_freesp(sp
, KEY_SADB_UNLOCKED
);
2624 OSAddAtomic(1, &ipstat
.ips_cantfrag
);
2628 type
= ICMP_SOURCEQUENCH
;
2632 case EACCES
: /* ipfw denied packet */
2637 icmp_error(mcopy
, type
, code
, dest
, nextmtu
);
2639 ip_fwd_route_copyin(ifp
, &fwd_rt
);
2650 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
2654 mp
= sbcreatecontrol_mbuf((caddr_t
) &tv
, sizeof(tv
),
2655 SCM_TIMESTAMP
, SOL_SOCKET
, mp
);
2660 if ((inp
->inp_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
2663 time
= mach_absolute_time();
2664 mp
= sbcreatecontrol_mbuf((caddr_t
) &time
, sizeof(time
),
2665 SCM_TIMESTAMP_MONOTONIC
, SOL_SOCKET
, mp
);
2671 if (inp
->inp_flags
& INP_RECVDSTADDR
) {
2672 mp
= sbcreatecontrol_mbuf((caddr_t
) &ip
->ip_dst
,
2673 sizeof(struct in_addr
), IP_RECVDSTADDR
, IPPROTO_IP
, mp
);
2680 * Moving these out of udp_input() made them even more broken
2681 * than they already were.
2683 /* options were tossed already */
2684 if (inp
->inp_flags
& INP_RECVOPTS
) {
2685 mp
= sbcreatecontrol_mbuf((caddr_t
) opts_deleted_above
,
2686 sizeof(struct in_addr
), IP_RECVOPTS
, IPPROTO_IP
, mp
);
2691 /* ip_srcroute doesn't do what we want here, need to fix */
2692 if (inp
->inp_flags
& INP_RECVRETOPTS
) {
2693 mp
= sbcreatecontrol_mbuf((caddr_t
) ip_srcroute(),
2694 sizeof(struct in_addr
), IP_RECVRETOPTS
, IPPROTO_IP
, mp
);
2700 if (inp
->inp_flags
& INP_RECVIF
) {
2703 struct sockaddr_dl sdl
;
2706 struct sockaddr_dl
*sdp
;
2707 struct sockaddr_dl
*sdl2
= &sdlbuf
.sdl
;
2709 ifnet_head_lock_shared();
2710 if ((ifp
= m
->m_pkthdr
.rcvif
) != NULL
&&
2711 ifp
->if_index
&& (ifp
->if_index
<= if_index
)) {
2712 struct ifaddr
*ifa
= ifnet_addrs
[ifp
->if_index
- 1];
2714 if (!ifa
|| !ifa
->ifa_addr
)
2718 sdp
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
2720 * Change our mind and don't try copy.
2722 if ((sdp
->sdl_family
!= AF_LINK
) ||
2723 (sdp
->sdl_len
> sizeof(sdlbuf
))) {
2727 bcopy(sdp
, sdl2
, sdp
->sdl_len
);
2732 = offsetof(struct sockaddr_dl
, sdl_data
[0]);
2733 sdl2
->sdl_family
= AF_LINK
;
2734 sdl2
->sdl_index
= 0;
2735 sdl2
->sdl_nlen
= sdl2
->sdl_alen
= sdl2
->sdl_slen
= 0;
2738 mp
= sbcreatecontrol_mbuf((caddr_t
) sdl2
, sdl2
->sdl_len
,
2739 IP_RECVIF
, IPPROTO_IP
, mp
);
2744 if (inp
->inp_flags
& INP_RECVTTL
) {
2745 mp
= sbcreatecontrol_mbuf((caddr_t
)&ip
->ip_ttl
, sizeof(ip
->ip_ttl
),
2746 IP_RECVTTL
, IPPROTO_IP
, mp
);
2751 if ((inp
->inp_socket
->so_flags
& SOF_RECV_TRAFFIC_CLASS
) != 0) {
2752 int tc
= m
->m_pkthdr
.prio
;
2754 mp
= sbcreatecontrol_mbuf((caddr_t
) &tc
, sizeof(tc
),
2755 SO_TRAFFIC_CLASS
, SOL_SOCKET
, mp
);
2760 if (inp
->inp_flags
& INP_PKTINFO
) {
2761 struct in_pktinfo pi
;
2763 bzero(&pi
, sizeof(struct in_pktinfo
));
2764 bcopy(&ip
->ip_dst
, &pi
.ipi_addr
, sizeof(struct in_addr
));
2765 pi
.ipi_ifindex
= (m
&& m
->m_pkthdr
.rcvif
) ? m
->m_pkthdr
.rcvif
->if_index
: 0;
2767 mp
= sbcreatecontrol_mbuf((caddr_t
)&pi
, sizeof(struct in_pktinfo
),
2768 IP_RECVPKTINFO
, IPPROTO_IP
, mp
);
2776 ipstat
.ips_pktdropcntrl
++;
2781 ip_rsvp_init(struct socket
*so
)
2783 if (so
->so_type
!= SOCK_RAW
||
2784 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2787 if (ip_rsvpd
!= NULL
)
2792 * This may seem silly, but we need to be sure we don't over-increment
2793 * the RSVP counter, in case something slips up.
2808 * This may seem silly, but we need to be sure we don't over-decrement
2809 * the RSVP counter, in case something slips up.