2 * Copyright (c) 2000-2008 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>
84 #include <machine/endian.h>
86 #include <kern/queue.h>
87 #include <kern/locks.h>
89 #include <pexpert/pexpert.h>
92 #include <net/if_var.h>
93 #include <net/if_dl.h>
94 #include <net/route.h>
95 #include <net/kpi_protocol.h>
97 #include <netinet/in.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/in_var.h>
100 #include <netinet/in_arp.h>
101 #include <netinet/ip.h>
102 #include <netinet/in_pcb.h>
103 #include <netinet/ip_var.h>
104 #include <netinet/ip_icmp.h>
105 #include <sys/socketvar.h>
107 #include <netinet/ip_fw.h>
108 #include <netinet/ip_divert.h>
110 #include <netinet/kpi_ipfilter_var.h>
112 /* needed for AUTOCONFIGURING: */
113 #include <netinet/udp.h>
114 #include <netinet/udp_var.h>
115 #include <netinet/bootp.h>
118 #include <security/mac_framework.h>
121 #include <sys/kdebug.h>
122 #include <libkern/OSAtomic.h>
124 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 0)
125 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 2)
126 #define DBG_FNC_IP_INPUT NETDBG_CODE(DBG_NETIP, (2 << 8))
130 #include <netinet6/ipsec.h>
131 #include <netkey/key.h>
135 #if defined(NFAITH) && NFAITH > 0
136 #include <net/if_types.h>
140 #include <netinet/ip_dummynet.h>
144 #include <net/pfvar.h>
148 extern int ipsec_bypass
;
149 extern lck_mtx_t
*sadb_mutex
;
151 lck_grp_t
*sadb_stat_mutex_grp
;
152 lck_grp_attr_t
*sadb_stat_mutex_grp_attr
;
153 lck_attr_t
*sadb_stat_mutex_attr
;
154 lck_mtx_t
*sadb_stat_mutex
;
159 static int ip_rsvp_on
;
160 struct socket
*ip_rsvpd
;
162 static int sysctl_ipforwarding SYSCTL_HANDLER_ARGS
;
164 int ipforwarding
= 0;
165 SYSCTL_PROC(_net_inet_ip
, IPCTL_FORWARDING
, forwarding
,
166 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &ipforwarding
, 0,
167 sysctl_ipforwarding
, "I", "Enable IP forwarding between interfaces");
169 static int ipsendredirects
= 1; /* XXX */
170 SYSCTL_INT(_net_inet_ip
, IPCTL_SENDREDIRECTS
, redirect
, CTLFLAG_RW
,
171 &ipsendredirects
, 0, "Enable sending IP redirects");
173 int ip_defttl
= IPDEFTTL
;
174 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFTTL
, ttl
, CTLFLAG_RW
,
175 &ip_defttl
, 0, "Maximum TTL on IP packets");
177 static int ip_dosourceroute
= 0;
178 SYSCTL_INT(_net_inet_ip
, IPCTL_SOURCEROUTE
, sourceroute
, CTLFLAG_RW
,
179 &ip_dosourceroute
, 0, "Enable forwarding source routed IP packets");
181 static int ip_acceptsourceroute
= 0;
182 SYSCTL_INT(_net_inet_ip
, IPCTL_ACCEPTSOURCEROUTE
, accept_sourceroute
,
183 CTLFLAG_RW
, &ip_acceptsourceroute
, 0,
184 "Enable accepting source routed IP packets");
186 static int ip_keepfaith
= 0;
187 SYSCTL_INT(_net_inet_ip
, IPCTL_KEEPFAITH
, keepfaith
, CTLFLAG_RW
,
189 "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
191 static int nipq
= 0; /* total # of reass queues */
193 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfragpackets
, CTLFLAG_RW
,
195 "Maximum number of IPv4 fragment reassembly queue entries");
197 static int maxfragsperpacket
;
198 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfragsperpacket
, CTLFLAG_RW
,
199 &maxfragsperpacket
, 0,
200 "Maximum number of IPv4 fragments allowed per packet");
203 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfrags
, CTLFLAG_RW
,
204 &maxfrags
, 0, "Maximum number of IPv4 fragments allowed");
206 static int currentfrags
= 0;
208 int ip_doscopedroute
= 1;
209 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, scopedroute
, CTLFLAG_RW
,
210 &ip_doscopedroute
, 0, "Enable IPv4 scoped routing");
213 * XXX - Setting ip_checkinterface mostly implements the receive side of
214 * the Strong ES model described in RFC 1122, but since the routing table
215 * and transmit implementation do not implement the Strong ES model,
216 * setting this to 1 results in an odd hybrid.
218 * XXX - ip_checkinterface currently must be disabled if you use ipnat
219 * to translate the destination address to another local interface.
221 * XXX - ip_checkinterface must be disabled if you add IP aliases
222 * to the loopback interface instead of the interface where the
223 * packets for those addresses are received.
225 static int ip_checkinterface
= 0;
226 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, check_interface
, CTLFLAG_RW
,
227 &ip_checkinterface
, 0, "Verify packet arrives on correct interface");
231 static int ipprintfs
= 0;
234 extern int in_proto_count
;
235 extern struct domain inetdomain
;
236 extern struct protosw inetsw
[];
237 struct protosw
*ip_protox
[IPPROTO_MAX
];
238 static int ipqmaxlen
= IFQ_MAXLEN
;
240 static lck_grp_attr_t
*in_ifaddr_rwlock_grp_attr
;
241 static lck_grp_t
*in_ifaddr_rwlock_grp
;
242 static lck_attr_t
*in_ifaddr_rwlock_attr
;
243 lck_rw_t
*in_ifaddr_rwlock
;
245 /* Protected by in_ifaddr_rwlock */
246 struct in_ifaddrhead in_ifaddrhead
; /* first inet address */
247 struct in_ifaddrhashhead
*in_ifaddrhashtbl
; /* inet addr hash table */
249 #define INADDR_NHASH 61
250 static u_int32_t inaddr_nhash
; /* hash table size */
251 static u_int32_t inaddr_hashp
; /* next largest prime */
253 struct ifqueue ipintrq
;
254 SYSCTL_INT(_net_inet_ip
, IPCTL_INTRQMAXLEN
, intr_queue_maxlen
, CTLFLAG_RW
,
255 &ipintrq
.ifq_maxlen
, 0, "Maximum size of the IP input queue");
256 SYSCTL_INT(_net_inet_ip
, IPCTL_INTRQDROPS
, intr_queue_drops
, CTLFLAG_RD
,
257 &ipintrq
.ifq_drops
, 0, "Number of packets dropped from the IP input queue");
259 struct ipstat ipstat
;
260 SYSCTL_STRUCT(_net_inet_ip
, IPCTL_STATS
, stats
, CTLFLAG_RD
,
261 &ipstat
, ipstat
, "IP statistics (struct ipstat, netinet/ip_var.h)");
263 /* Packet reassembly stuff */
264 #define IPREASS_NHASH_LOG2 6
265 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
266 #define IPREASS_HMASK (IPREASS_NHASH - 1)
267 #define IPREASS_HASH(x,y) \
268 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
270 static struct ipq ipq
[IPREASS_NHASH
];
271 static TAILQ_HEAD(ipq_list
, ipq
) ipq_list
=
272 TAILQ_HEAD_INITIALIZER(ipq_list
);
273 const int ipintrq_present
= 1;
275 lck_attr_t
*ip_mutex_attr
;
276 lck_grp_t
*ip_mutex_grp
;
277 lck_grp_attr_t
*ip_mutex_grp_attr
;
278 lck_mtx_t
*inet_domain_mutex
;
279 extern lck_mtx_t
*domain_proto_mtx
;
282 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFMTU
, mtu
, CTLFLAG_RW
,
283 &ip_mtu
, 0, "Default MTU");
287 static int ipstealth
= 0;
288 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, stealth
, CTLFLAG_RW
,
295 ip_fw_chk_t
*ip_fw_chk_ptr
;
301 ip_dn_io_t
*ip_dn_io_ptr
;
304 int (*fr_checkp
)(struct ip
*, int, struct ifnet
*, int, struct mbuf
**) = NULL
;
305 #endif /* IPFIREWALL */
307 SYSCTL_NODE(_net_inet_ip
, OID_AUTO
, linklocal
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "link local");
309 struct ip_linklocal_stat ip_linklocal_stat
;
310 SYSCTL_STRUCT(_net_inet_ip_linklocal
, OID_AUTO
, stat
, CTLFLAG_RD
,
311 &ip_linklocal_stat
, ip_linklocal_stat
,
312 "Number of link local packets with TTL less than 255");
314 SYSCTL_NODE(_net_inet_ip_linklocal
, OID_AUTO
, in
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "link local input");
316 int ip_linklocal_in_allowbadttl
= 1;
317 SYSCTL_INT(_net_inet_ip_linklocal_in
, OID_AUTO
, allowbadttl
, CTLFLAG_RW
,
318 &ip_linklocal_in_allowbadttl
, 0,
319 "Allow incoming link local packets with TTL less than 255");
323 * We need to save the IP options in case a protocol wants to respond
324 * to an incoming packet over the same route if the packet got here
325 * using IP source routing. This allows connection establishment and
326 * maintenance when the remote end is on a network that is not known
329 static int ip_nhops
= 0;
330 static struct ip_srcrt
{
331 struct in_addr dst
; /* final destination */
332 char nop
; /* one NOP to align */
333 char srcopt
[IPOPT_OFFSET
+ 1]; /* OPTVAL, OLEN and OFFSET */
334 struct in_addr route
[MAX_IPOPTLEN
/sizeof(struct in_addr
)];
337 static void in_ifaddrhashtbl_init(void);
338 static void save_rte(u_char
*, struct in_addr
);
339 static int ip_dooptions(struct mbuf
*, int, struct sockaddr_in
*);
340 static void ip_forward(struct mbuf
*, int, struct sockaddr_in
*);
341 static void ip_freef(struct ipq
*);
344 static struct mbuf
*ip_reass(struct mbuf
*,
345 struct ipq
*, struct ipq
*, u_int32_t
*, u_int16_t
*);
347 static struct mbuf
*ip_reass(struct mbuf
*,
348 struct ipq
*, struct ipq
*, u_int16_t
*, u_int16_t
*);
351 static struct mbuf
*ip_reass(struct mbuf
*, struct ipq
*, struct ipq
*);
353 static void ip_fwd_route_copyout(struct ifnet
*, struct route
*);
354 static void ip_fwd_route_copyin(struct ifnet
*, struct route
*);
359 extern u_short ip_id
;
361 int ip_use_randomid
= 1;
362 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, random_id
, CTLFLAG_RW
,
363 &ip_use_randomid
, 0, "Randomize IP packets IDs");
366 #define satosin(sa) ((struct sockaddr_in *)(sa))
367 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
370 * IP initialization: fill in IP protocol switch table.
371 * All protocols not implemented in kernel go to raw IP protocol handler.
378 static int ip_initialized
= 0;
384 in_ifaddr_rwlock_grp_attr
= lck_grp_attr_alloc_init();
385 in_ifaddr_rwlock_grp
= lck_grp_alloc_init("in_ifaddr_rwlock",
386 in_ifaddr_rwlock_grp_attr
);
387 in_ifaddr_rwlock_attr
= lck_attr_alloc_init();
388 in_ifaddr_rwlock
= lck_rw_alloc_init(in_ifaddr_rwlock_grp
,
389 in_ifaddr_rwlock_attr
);
391 TAILQ_INIT(&in_ifaddrhead
);
392 in_ifaddrhashtbl_init();
394 pr
= pffindproto_locked(PF_INET
, IPPROTO_RAW
, SOCK_RAW
);
397 for (i
= 0; i
< IPPROTO_MAX
; i
++)
399 for (pr
= inetdomain
.dom_protosw
; pr
; pr
= pr
->pr_next
) {
400 if (pr
->pr_domain
== NULL
)
401 continue; /* If uninitialized, skip */
402 if (pr
->pr_domain
->dom_family
== PF_INET
&&
403 pr
->pr_protocol
&& pr
->pr_protocol
!= IPPROTO_RAW
)
404 ip_protox
[pr
->pr_protocol
] = pr
;
406 for (i
= 0; i
< IPREASS_NHASH
; i
++)
407 ipq
[i
].next
= ipq
[i
].prev
= &ipq
[i
];
409 maxnipq
= nmbclusters
/ 32;
410 maxfrags
= maxnipq
* 2;
411 maxfragsperpacket
= 128; /* enough for 64k in 512 byte fragments */
415 struct timeval timenow
;
416 getmicrotime(&timenow
);
417 ip_id
= timenow
.tv_sec
& 0xffff;
420 ipintrq
.ifq_maxlen
= ipqmaxlen
;
424 ip_mutex_grp_attr
= lck_grp_attr_alloc_init();
426 ip_mutex_grp
= lck_grp_alloc_init("ip", ip_mutex_grp_attr
);
428 ip_mutex_attr
= lck_attr_alloc_init();
430 if ((ip_mutex
= lck_mtx_alloc_init(ip_mutex_grp
, ip_mutex_attr
)) == NULL
) {
431 printf("ip_init: can't alloc ip_mutex\n");
437 sadb_stat_mutex_grp_attr
= lck_grp_attr_alloc_init();
438 sadb_stat_mutex_grp
= lck_grp_alloc_init("sadb_stat", sadb_stat_mutex_grp_attr
);
439 sadb_stat_mutex_attr
= lck_attr_alloc_init();
441 if ((sadb_stat_mutex
= lck_mtx_alloc_init(sadb_stat_mutex_grp
, sadb_stat_mutex_attr
)) == NULL
) {
442 printf("ip_init: can't alloc sadb_stat_mutex\n");
454 * Initialize IPv4 source address hash table.
457 in_ifaddrhashtbl_init(void)
461 if (in_ifaddrhashtbl
!= NULL
)
464 PE_parse_boot_argn("inaddr_nhash", &inaddr_nhash
, sizeof (inaddr_nhash
));
465 if (inaddr_nhash
== 0)
466 inaddr_nhash
= INADDR_NHASH
;
468 MALLOC(in_ifaddrhashtbl
, struct in_ifaddrhashhead
*,
469 inaddr_nhash
* sizeof (*in_ifaddrhashtbl
),
470 M_IFADDR
, M_WAITOK
| M_ZERO
);
471 if (in_ifaddrhashtbl
== NULL
)
472 panic("in_ifaddrhashtbl_init allocation failed");
475 * Generate the next largest prime greater than inaddr_nhash.
477 k
= (inaddr_nhash
% 2 == 0) ? inaddr_nhash
+ 1 : inaddr_nhash
+ 2;
480 for (i
= 3; i
* i
<= k
; i
+= 2) {
492 inaddr_hashval(u_int32_t key
)
495 * The hash index is the computed prime times the key modulo
496 * the hash size, as documented in "Introduction to Algorithms"
497 * (Cormen, Leiserson, Rivest).
499 if (inaddr_nhash
> 1)
500 return ((key
* inaddr_hashp
) % inaddr_nhash
);
507 protocol_family_t __unused protocol
,
513 /* ip_input should handle a list of packets but does not yet */
515 for (packet
= packet_list
; packet
; packet
= packet_list
) {
517 packet_list
= mbuf_nextpkt(packet
);
518 mbuf_setnextpkt(packet
, NULL
);
523 /* Initialize the PF_INET domain, and add in the pre-defined protos */
530 static int inetdomain_initted
= 0;
532 if (!inetdomain_initted
)
535 kprintf("Initing %d protosw entries\n", in_proto_count
);
538 dp
->dom_flags
= DOM_REENTRANT
;
540 for (i
=0, pr
= &inetsw
[0]; i
<in_proto_count
; i
++, pr
++)
541 net_add_proto(pr
, dp
);
542 inet_domain_mutex
= dp
->dom_mtx
;
543 inetdomain_initted
= 1;
545 lck_mtx_unlock(domain_proto_mtx
);
546 proto_register_input(PF_INET
, ip_proto_input
, NULL
, 1);
547 lck_mtx_lock(domain_proto_mtx
);
551 __private_extern__
void
552 ip_proto_dispatch_in(
556 ipfilter_t inject_ipfref
)
558 struct ipfilter
*filter
;
559 int seen
= (inject_ipfref
== 0);
560 int changed_header
= 0;
563 if (!TAILQ_EMPTY(&ipv4_filters
)) {
565 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
567 if ((struct ipfilter
*)inject_ipfref
== filter
)
569 } else if (filter
->ipf_filter
.ipf_input
) {
572 if (changed_header
== 0) {
574 ip
= mtod(m
, struct ip
*);
575 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
576 ip
->ip_off
= htons(ip
->ip_off
);
578 ip
->ip_sum
= in_cksum(m
, hlen
);
580 result
= filter
->ipf_filter
.ipf_input(
581 filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, hlen
, proto
);
582 if (result
== EJUSTRETURN
) {
596 * If there isn't a specific lock for the protocol
597 * we're about to call, use the generic lock for AF_INET.
598 * otherwise let the protocol deal with its own locking
600 ip
= mtod(m
, struct ip
*);
602 if (changed_header
) {
603 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
604 ip
->ip_off
= ntohs(ip
->ip_off
);
607 if (!(ip_protox
[ip
->ip_p
]->pr_flags
& PR_PROTOLOCK
)) {
608 lck_mtx_lock(inet_domain_mutex
);
609 (*ip_protox
[ip
->ip_p
]->pr_input
)(m
, hlen
);
610 lck_mtx_unlock(inet_domain_mutex
);
613 (*ip_protox
[ip
->ip_p
]->pr_input
)(m
, hlen
);
618 * Ip input routine. Checksum and byte swap header. If fragmented
619 * try to reassemble. Process options. Pass to next level.
622 ip_input(struct mbuf
*m
)
626 struct in_ifaddr
*ia
= NULL
;
627 int i
, hlen
, checkif
;
629 struct in_addr pkt_dst
;
630 u_int32_t div_info
= 0; /* packet divert/tee info */
632 struct ip_fw_args args
;
634 ipfilter_t inject_filter_ref
= 0;
641 args
.divert_rule
= 0; /* divert cookie */
642 args
.next_hop
= NULL
;
645 * Don't bother searching for tag(s) if there's none.
647 if (SLIST_EMPTY(&m
->m_pkthdr
.tags
))
650 /* Grab info from mtags prepended to the chain */
652 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
653 KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
654 struct dn_pkt_tag
*dn_tag
;
656 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
657 args
.rule
= dn_tag
->rule
;
659 m_tag_delete(m
, tag
);
661 #endif /* DUMMYNET */
664 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
665 KERNEL_TAG_TYPE_DIVERT
, NULL
)) != NULL
) {
666 struct divert_tag
*div_tag
;
668 div_tag
= (struct divert_tag
*)(tag
+1);
669 args
.divert_rule
= div_tag
->cookie
;
671 m_tag_delete(m
, tag
);
675 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
676 KERNEL_TAG_TYPE_IPFORWARD
, NULL
)) != NULL
) {
677 struct ip_fwd_tag
*ipfwd_tag
;
679 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
680 args
.next_hop
= ipfwd_tag
->next_hop
;
682 m_tag_delete(m
, tag
);
686 if (m
== NULL
|| (m
->m_flags
& M_PKTHDR
) == 0)
687 panic("ip_input no HDR");
690 if (args
.rule
) { /* dummynet already filtered us */
691 ip
= mtod(m
, struct ip
*);
692 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
693 inject_filter_ref
= ipf_get_inject_filter(m
);
697 #endif /* IPFIREWALL */
700 * No need to proccess packet twice if we've already seen it.
702 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
))
703 inject_filter_ref
= ipf_get_inject_filter(m
);
704 if (inject_filter_ref
!= 0) {
705 ip
= mtod(m
, struct ip
*);
706 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
707 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
708 ip
->ip_off
= ntohs(ip
->ip_off
);
709 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, inject_filter_ref
);
713 OSAddAtomic(1, &ipstat
.ips_total
);
715 if (m
->m_pkthdr
.len
< sizeof(struct ip
))
718 if (m
->m_len
< sizeof (struct ip
) &&
719 (m
= m_pullup(m
, sizeof (struct ip
))) == 0) {
720 OSAddAtomic(1, &ipstat
.ips_toosmall
);
723 ip
= mtod(m
, struct ip
*);
725 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
726 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
728 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
729 OSAddAtomic(1, &ipstat
.ips_badvers
);
733 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
734 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
735 OSAddAtomic(1, &ipstat
.ips_badhlen
);
738 if (hlen
> m
->m_len
) {
739 if ((m
= m_pullup(m
, hlen
)) == 0) {
740 OSAddAtomic(1, &ipstat
.ips_badhlen
);
743 ip
= mtod(m
, struct ip
*);
746 /* 127/8 must not appear on wire - RFC1122 */
747 if ((ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
748 (ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
) {
749 if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) {
750 OSAddAtomic(1, &ipstat
.ips_badaddr
);
755 /* IPv4 Link-Local Addresses as defined in <draft-ietf-zeroconf-ipv4-linklocal-05.txt> */
756 if ((IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
)) ||
757 IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)))) {
758 ip_linklocal_stat
.iplls_in_total
++;
759 if (ip
->ip_ttl
!= MAXTTL
) {
760 OSAddAtomic(1, &ip_linklocal_stat
.iplls_in_badttl
);
761 /* Silently drop link local traffic with bad TTL */
762 if (!ip_linklocal_in_allowbadttl
)
766 if ((IF_HWASSIST_CSUM_FLAGS(m
->m_pkthdr
.rcvif
->if_hwassist
) == 0)
767 || (apple_hwcksum_rx
== 0) ||
768 ((m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
) && ip
->ip_p
!= IPPROTO_TCP
)) {
769 m
->m_pkthdr
.csum_flags
= 0; /* invalidate HW generated checksum flags */
772 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
773 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
774 } else if (!(m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) ||
775 apple_hwcksum_tx
== 0) {
777 * Either this is not loopback packet coming from an interface
778 * that does not support checksum offloading, or it is loopback
779 * packet that has undergone software checksumming at the send
780 * side because apple_hwcksum_tx was set to 0. In this case,
781 * calculate the checksum in software to validate the packet.
783 sum
= in_cksum(m
, hlen
);
786 * This is a loopback packet without any valid checksum since
787 * the send side has bypassed it (apple_hwcksum_tx set to 1).
788 * We get here because apple_hwcksum_rx was set to 0, and so
789 * we pretend that all is well.
792 m
->m_pkthdr
.csum_flags
|= CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
793 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
794 m
->m_pkthdr
.csum_data
= 0xffff;
797 OSAddAtomic(1, &ipstat
.ips_badsum
);
802 * Convert fields to host representation.
804 #if BYTE_ORDER != BIG_ENDIAN
808 if (ip
->ip_len
< hlen
) {
809 OSAddAtomic(1, &ipstat
.ips_badlen
);
813 #if BYTE_ORDER != BIG_ENDIAN
817 * Check that the amount of data in the buffers
818 * is as at least much as the IP header would have us expect.
819 * Trim mbufs if longer than we expect.
820 * Drop packet if shorter than we expect.
822 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
824 OSAddAtomic(1, &ipstat
.ips_tooshort
);
827 if (m
->m_pkthdr
.len
> ip
->ip_len
) {
828 /* Invalidate hwcksuming */
829 m
->m_pkthdr
.csum_flags
= 0;
830 m
->m_pkthdr
.csum_data
= 0;
832 if (m
->m_len
== m
->m_pkthdr
.len
) {
833 m
->m_len
= ip
->ip_len
;
834 m
->m_pkthdr
.len
= ip
->ip_len
;
836 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
840 if (ipsec_bypass
== 0 && ipsec_gethist(m
, NULL
))
846 * Right now when no processing on packet has done
847 * and it is still fresh out of network we do our black
849 * - Firewall: deny/allow/divert
850 * - Xlate: translate packet's addr/port (NAT).
851 * - Pipe: pass pkt through dummynet.
852 * - Wrap: fake packet's addr/port <unimpl.>
853 * - Encapsulate: put it in another IP and send out. <unimp.>
856 /* Invoke inbound packet filter */
857 if (pf_af_hook(m
->m_pkthdr
.rcvif
, NULL
, &m
, AF_INET
, TRUE
) != 0) {
859 panic("%s: unexpected packet %p\n", __func__
, m
);
862 /* Already freed by callee */
865 ip
= mtod(m
, struct ip
*);
866 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
872 #endif /* DUMMYNET */
874 * Check if we want to allow this packet to be processed.
875 * Consider it to be bad if not.
880 if (fr_checkp(ip
, hlen
, m
->m_pkthdr
.rcvif
, 0, &m1
) || !m1
) {
883 ip
= mtod(m
= m1
, struct ip
*);
885 if (fw_enable
&& IPFW_LOADED
) {
886 #if IPFIREWALL_FORWARD
888 * If we've been forwarded from the output side, then
889 * skip the firewall a second time
893 #endif /* IPFIREWALL_FORWARD */
897 i
= ip_fw_chk_ptr(&args
);
900 if ( (i
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) { /* drop */
905 ip
= mtod(m
, struct ip
*); /* just in case m changed */
907 if (i
== 0 && args
.next_hop
== NULL
) { /* common case */
911 if (DUMMYNET_LOADED
&& (i
& IP_FW_PORT_DYNT_FLAG
) != 0) {
912 /* Send packet to the appropriate pipe */
913 ip_dn_io_ptr(m
, i
&0xffff, DN_TO_IP_IN
, &args
);
916 #endif /* DUMMYNET */
918 if (i
!= 0 && (i
& IP_FW_PORT_DYNT_FLAG
) == 0) {
919 /* Divert or tee packet */
924 #if IPFIREWALL_FORWARD
925 if (i
== 0 && args
.next_hop
!= NULL
) {
930 * if we get here, the packet must be dropped
935 #endif /* IPFIREWALL */
939 * Process options and, if not destined for us,
940 * ship it on. ip_dooptions returns 1 when an
941 * error was detected (causing an icmp message
942 * to be sent and the original packet to be freed).
944 ip_nhops
= 0; /* for source routed packets */
946 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, args
.next_hop
)) {
948 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, NULL
)) {
953 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
954 * matter if it is destined to another node, or whether it is
955 * a multicast one, RSVP wants it! and prevents it from being forwarded
956 * anywhere else. Also checks if the rsvp daemon is running before
957 * grabbing the packet.
959 if (rsvp_on
&& ip
->ip_p
==IPPROTO_RSVP
)
963 * Check our list of addresses, to see if the packet is for us.
964 * If we don't have any addresses, assume any unicast packet
965 * we receive might be for us (and let the upper layers deal
968 if (TAILQ_EMPTY(&in_ifaddrhead
) &&
969 (m
->m_flags
& (M_MCAST
|M_BCAST
)) == 0)
973 * Cache the destination address of the packet; this may be
974 * changed by use of 'ipfw fwd'.
977 pkt_dst
= args
.next_hop
== NULL
?
978 ip
->ip_dst
: args
.next_hop
->sin_addr
;
980 pkt_dst
= ip
->ip_dst
;
984 * Enable a consistency check between the destination address
985 * and the arrival interface for a unicast packet (the RFC 1122
986 * strong ES model) if IP forwarding is disabled and the packet
987 * is not locally generated and the packet is not subject to
990 * XXX - Checking also should be disabled if the destination
991 * address is ipnat'ed to a different interface.
993 * XXX - Checking is incompatible with IP aliases added
994 * to the loopback interface instead of the interface where
995 * the packets are received.
997 checkif
= ip_checkinterface
&& (ipforwarding
== 0) &&
998 ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0)
1000 && (args
.next_hop
== NULL
);
1006 * Check for exact addresses in the hash bucket.
1008 lck_rw_lock_shared(in_ifaddr_rwlock
);
1009 TAILQ_FOREACH(ia
, INADDR_HASH(pkt_dst
.s_addr
), ia_hash
) {
1011 * If the address matches, verify that the packet
1012 * arrived via the correct interface if checking is
1015 if (IA_SIN(ia
)->sin_addr
.s_addr
== pkt_dst
.s_addr
&&
1016 (!checkif
|| ia
->ia_ifp
== m
->m_pkthdr
.rcvif
)) {
1017 lck_rw_done(in_ifaddr_rwlock
);
1021 lck_rw_done(in_ifaddr_rwlock
);
1024 * Check for broadcast addresses.
1026 * Only accept broadcast packets that arrive via the matching
1027 * interface. Reception of forwarded directed broadcasts would be
1028 * handled via ip_forward() and ether_frameout() with the loopback
1029 * into the stack for SIMPLEX interfaces handled by ether_frameout().
1031 if (m
->m_pkthdr
.rcvif
->if_flags
& IFF_BROADCAST
) {
1033 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1035 ifnet_lock_shared(ifp
);
1036 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1037 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
1040 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
1041 pkt_dst
.s_addr
|| ia
->ia_netbroadcast
.s_addr
==
1043 ifnet_lock_done(ifp
);
1047 ifnet_lock_done(ifp
);
1050 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
1051 struct in_multi
*inm
;
1052 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1056 * If we are acting as a multicast router, all
1057 * incoming multicast packets are passed to the
1058 * kernel-level multicast forwarding function.
1059 * The packet is returned (relatively) intact; if
1060 * ip_mforward() returns a non-zero value, the packet
1061 * must be discarded, else it may be accepted below.
1063 lck_mtx_lock(ip_mutex
);
1064 if (ip_mforward
&& ip_mforward(ip
, ifp
, m
, 0) != 0) {
1065 OSAddAtomic(1, &ipstat
.ips_cantforward
);
1067 lck_mtx_unlock(ip_mutex
);
1072 * The process-level routing daemon needs to receive
1073 * all multicast IGMP packets, whether or not this
1074 * host belongs to their destination groups.
1076 if (ip
->ip_p
== IPPROTO_IGMP
)
1078 OSAddAtomic(1, &ipstat
.ips_forward
);
1080 #endif /* MROUTING */
1082 * See if we belong to the destination multicast group on the
1083 * arrival interface.
1085 ifnet_lock_shared(ifp
);
1086 IN_LOOKUP_MULTI(ip
->ip_dst
, ifp
, inm
);
1087 ifnet_lock_done(ifp
);
1089 OSAddAtomic(1, &ipstat
.ips_notmember
);
1095 if (ip
->ip_dst
.s_addr
== (u_int32_t
)INADDR_BROADCAST
)
1097 if (ip
->ip_dst
.s_addr
== INADDR_ANY
)
1100 /* Allow DHCP/BootP responses through */
1101 if (m
->m_pkthdr
.rcvif
!= NULL
1102 && (m
->m_pkthdr
.rcvif
->if_eflags
& IFEF_AUTOCONFIGURING
)
1103 && hlen
== sizeof(struct ip
)
1104 && ip
->ip_p
== IPPROTO_UDP
) {
1105 struct udpiphdr
*ui
;
1106 if (m
->m_len
< sizeof(struct udpiphdr
)
1107 && (m
= m_pullup(m
, sizeof(struct udpiphdr
))) == 0) {
1108 OSAddAtomic(1, &udpstat
.udps_hdrops
);
1111 ui
= mtod(m
, struct udpiphdr
*);
1112 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) {
1115 ip
= mtod(m
, struct ip
*); /* in case it changed */
1118 #if defined(NFAITH) && 0 < NFAITH
1120 * FAITH(Firewall Aided Internet Translator)
1122 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
== IFT_FAITH
) {
1124 if (ip
->ip_p
== IPPROTO_TCP
|| ip
->ip_p
== IPPROTO_ICMP
)
1132 * Not for us; forward if possible and desirable.
1134 if (ipforwarding
== 0) {
1135 OSAddAtomic(1, &ipstat
.ips_cantforward
);
1139 ip_forward(m
, 0, args
.next_hop
);
1141 ip_forward(m
, 0, NULL
);
1148 * If offset or IP_MF are set, must reassemble.
1149 * Otherwise, nothing need be done.
1150 * (We could look in the reassembly queue to see
1151 * if the packet was previously fragmented,
1152 * but it's not worth the time; just let them time out.)
1154 if (ip
->ip_off
& (IP_MF
| IP_OFFMASK
| IP_RF
)) {
1156 /* If maxnipq is 0, never accept fragments. */
1159 OSAddAtomic(1, &ipstat
.ips_fragments
);
1160 OSAddAtomic(1, &ipstat
.ips_fragdropped
);
1165 * If we will exceed the number of fragments in queues, timeout the
1166 * oldest fragemented packet to make space.
1168 lck_mtx_lock(ip_mutex
);
1169 if (currentfrags
>= maxfrags
) {
1170 fp
= TAILQ_LAST(&ipq_list
, ipq_list
);
1171 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragtimeout
);
1173 if (ip
->ip_id
== fp
->ipq_id
&&
1174 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
1175 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
1176 ip
->ip_p
== fp
->ipq_p
) {
1178 * If we match the fragment queue we were going to
1179 * discard, drop this packet too.
1181 OSAddAtomic(1, &ipstat
.ips_fragdropped
);
1183 lck_mtx_unlock(ip_mutex
);
1190 sum
= IPREASS_HASH(ip
->ip_src
.s_addr
, ip
->ip_id
);
1192 * Look for queue of fragments
1195 for (fp
= ipq
[sum
].next
; fp
!= &ipq
[sum
]; fp
= fp
->next
)
1196 if (ip
->ip_id
== fp
->ipq_id
&&
1197 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
1198 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
1200 mac_ipq_label_compare(m
, fp
) &&
1202 ip
->ip_p
== fp
->ipq_p
)
1206 * Enforce upper bound on number of fragmented packets
1207 * for which we attempt reassembly;
1208 * If maxnipq is -1, accept all fragments without limitation.
1210 if ((nipq
> maxnipq
) && (maxnipq
> 0)) {
1212 * drop the oldest fragment before proceeding further
1214 fp
= TAILQ_LAST(&ipq_list
, ipq_list
);
1215 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragtimeout
);
1223 * Adjust ip_len to not reflect header,
1224 * convert offset of this to bytes.
1227 if (ip
->ip_off
& IP_MF
) {
1229 * Make sure that fragments have a data length
1230 * that's a non-zero multiple of 8 bytes.
1232 if (ip
->ip_len
== 0 || (ip
->ip_len
& 0x7) != 0) {
1233 OSAddAtomic(1, &ipstat
.ips_toosmall
);
1234 lck_mtx_unlock(ip_mutex
);
1237 m
->m_flags
|= M_FRAG
;
1239 /* Clear the flag in case packet comes from loopback */
1240 m
->m_flags
&= ~M_FRAG
;
1245 * Attempt reassembly; if it succeeds, proceed.
1246 * ip_reass() will return a different mbuf, and update
1247 * the divert info in div_info and args.divert_rule.
1249 OSAddAtomic(1, &ipstat
.ips_fragments
);
1250 m
->m_pkthdr
.header
= ip
;
1252 m
= ip_reass(m
, fp
, &ipq
[sum
],
1253 (u_int16_t
*)&div_info
, &args
.divert_rule
);
1255 m
= ip_reass(m
, fp
, &ipq
[sum
]);
1258 lck_mtx_unlock(ip_mutex
);
1261 OSAddAtomic(1, &ipstat
.ips_reassembled
);
1262 ip
= mtod(m
, struct ip
*);
1263 /* Get the header length of the reassembled packet */
1264 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1267 /* Restore original checksum before diverting packet */
1268 if (div_info
!= 0) {
1271 #if BYTE_ORDER != BIG_ENDIAN
1277 ip
->ip_sum
= in_cksum(m
, hlen
);
1279 #if BYTE_ORDER != BIG_ENDIAN
1287 lck_mtx_unlock(ip_mutex
);
1293 * Divert or tee packet to the divert protocol if required.
1295 * If div_info is zero then cookie should be too, so we shouldn't
1296 * need to clear them here. Assume divert_packet() does so also.
1298 if (div_info
!= 0) {
1299 struct mbuf
*clone
= NULL
;
1301 /* Clone packet if we're doing a 'tee' */
1302 if ((div_info
& IP_FW_PORT_TEE_FLAG
) != 0)
1303 clone
= m_dup(m
, M_DONTWAIT
);
1305 /* Restore packet header fields to original values */
1308 #if BYTE_ORDER != BIG_ENDIAN
1312 /* Deliver packet to divert input routine */
1313 OSAddAtomic(1, &ipstat
.ips_delivered
);
1314 divert_packet(m
, 1, div_info
& 0xffff, args
.divert_rule
);
1316 /* If 'tee', continue with original packet */
1317 if (clone
== NULL
) {
1321 ip
= mtod(m
, struct ip
*);
1327 * enforce IPsec policy checking if we are seeing last header.
1328 * note that we do not visit this with protocols with pcb layer
1329 * code - like udp/tcp/raw ip.
1331 if (ipsec_bypass
== 0 && (ip_protox
[ip
->ip_p
]->pr_flags
& PR_LASTHDR
) != 0) {
1332 if (ipsec4_in_reject(m
, NULL
)) {
1333 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
1340 * Switch out to protocol's input routine.
1342 OSAddAtomic(1, &ipstat
.ips_delivered
);
1345 if (args
.next_hop
&& ip
->ip_p
== IPPROTO_TCP
) {
1346 /* TCP needs IPFORWARD info if available */
1347 struct m_tag
*fwd_tag
;
1348 struct ip_fwd_tag
*ipfwd_tag
;
1350 fwd_tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
,
1351 KERNEL_TAG_TYPE_IPFORWARD
, sizeof (*ipfwd_tag
),
1353 if (fwd_tag
== NULL
) {
1357 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
1358 ipfwd_tag
->next_hop
= args
.next_hop
;
1360 m_tag_prepend(m
, fwd_tag
);
1362 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1363 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1366 /* TCP deals with its own locking */
1367 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1369 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1370 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1372 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1375 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1381 KERNEL_DEBUG(DBG_LAYER_END
, 0,0,0,0,0);
1386 * Take incoming datagram fragment and try to reassemble it into
1387 * whole datagram. If a chain for reassembly of this datagram already
1388 * exists, then it is given as fp; otherwise have to make a chain.
1390 * When IPDIVERT enabled, keep additional state with each packet that
1391 * tells us if we need to divert or tee the packet we're building.
1394 static struct mbuf
*
1396 ip_reass(struct mbuf
*m
, struct ipq
*fp
, struct ipq
*where
,
1399 #else /* IPDIVERT_44 */
1401 #endif /* IPDIVERT_44 */
1402 u_int16_t
*divcookie
)
1403 #else /* IPDIVERT */
1404 ip_reass(struct mbuf
*m
, struct ipq
*fp
, struct ipq
*where
)
1405 #endif /* IPDIVERT */
1407 struct ip
*ip
= mtod(m
, struct ip
*);
1408 struct mbuf
*p
= 0, *q
, *nq
;
1410 int hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1414 lck_mtx_assert(ip_mutex
, LCK_MTX_ASSERT_OWNED
);
1416 * Presence of header sizes in mbufs
1417 * would confuse code below.
1422 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1423 m
->m_pkthdr
.csum_flags
= 0;
1425 * If first fragment to arrive, create a reassembly queue.
1428 if ((t
= m_get(M_DONTWAIT
, MT_FTABLE
)) == NULL
)
1430 fp
= mtod(t
, struct ipq
*);
1432 if (mac_ipq_label_init(fp
, M_NOWAIT
) != 0) {
1437 mac_ipq_label_associate(m
, fp
);
1439 insque((void*)fp
, (void*)where
);
1442 fp
->ipq_ttl
= IPFRAGTTL
;
1443 fp
->ipq_p
= ip
->ip_p
;
1444 fp
->ipq_id
= ip
->ip_id
;
1445 fp
->ipq_src
= ip
->ip_src
;
1446 fp
->ipq_dst
= ip
->ip_dst
;
1448 m
->m_nextpkt
= NULL
;
1451 fp
->ipq_div_info
= 0;
1455 fp
->ipq_div_cookie
= 0;
1457 TAILQ_INSERT_HEAD(&ipq_list
, fp
, ipq_list
);
1462 mac_ipq_label_update(m
, fp
);
1466 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
1469 * Handle ECN by comparing this segment with the first one;
1470 * if CE is set, do not lose CE.
1471 * drop if CE and not-ECT are mixed for the same packet.
1473 ecn
= ip
->ip_tos
& IPTOS_ECN_MASK
;
1474 ecn0
= GETIP(fp
->ipq_frags
)->ip_tos
& IPTOS_ECN_MASK
;
1475 if (ecn
== IPTOS_ECN_CE
) {
1476 if (ecn0
== IPTOS_ECN_NOTECT
)
1478 if (ecn0
!= IPTOS_ECN_CE
)
1479 GETIP(fp
->ipq_frags
)->ip_tos
|= IPTOS_ECN_CE
;
1481 if (ecn
== IPTOS_ECN_NOTECT
&& ecn0
!= IPTOS_ECN_NOTECT
)
1485 * Find a segment which begins after this one does.
1487 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
)
1488 if (GETIP(q
)->ip_off
> ip
->ip_off
)
1492 * If there is a preceding segment, it may provide some of
1493 * our data already. If so, drop the data from the incoming
1494 * segment. If it provides all of our data, drop us, otherwise
1495 * stick new segment in the proper place.
1497 * If some of the data is dropped from the the preceding
1498 * segment, then it's checksum is invalidated.
1501 i
= GETIP(p
)->ip_off
+ GETIP(p
)->ip_len
- ip
->ip_off
;
1503 if (i
>= ip
->ip_len
)
1506 m
->m_pkthdr
.csum_flags
= 0;
1510 m
->m_nextpkt
= p
->m_nextpkt
;
1513 m
->m_nextpkt
= fp
->ipq_frags
;
1518 * While we overlap succeeding segments trim them or,
1519 * if they are completely covered, dequeue them.
1521 for (; q
!= NULL
&& ip
->ip_off
+ ip
->ip_len
> GETIP(q
)->ip_off
;
1523 i
= (ip
->ip_off
+ ip
->ip_len
) -
1525 if (i
< GETIP(q
)->ip_len
) {
1526 GETIP(q
)->ip_len
-= i
;
1527 GETIP(q
)->ip_off
+= i
;
1529 q
->m_pkthdr
.csum_flags
= 0;
1534 OSAddAtomic(1, &ipstat
.ips_fragdropped
);
1544 * Transfer firewall instructions to the fragment structure.
1545 * Only trust info in the fragment at offset 0.
1547 if (ip
->ip_off
== 0) {
1549 fp
->ipq_div_info
= *divinfo
;
1551 fp
->ipq_divert
= *divinfo
;
1553 fp
->ipq_div_cookie
= *divcookie
;
1560 * Check for complete reassembly and perform frag per packet
1563 * Frag limiting is performed here so that the nth frag has
1564 * a chance to complete the packet before we drop the packet.
1565 * As a result, n+1 frags are actually allowed per packet, but
1566 * only n will ever be stored. (n = maxfragsperpacket.)
1570 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
) {
1571 if (GETIP(q
)->ip_off
!= next
) {
1572 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
1573 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragdropped
);
1578 next
+= GETIP(q
)->ip_len
;
1580 /* Make sure the last packet didn't have the IP_MF flag */
1581 if (p
->m_flags
& M_FRAG
) {
1582 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
1583 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragdropped
);
1590 * Reassembly is complete. Make sure the packet is a sane size.
1594 if (next
+ (IP_VHL_HL(ip
->ip_vhl
) << 2) > IP_MAXPACKET
) {
1595 OSAddAtomic(1, &ipstat
.ips_toolong
);
1596 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragdropped
);
1602 * Concatenate fragments.
1610 for (q
= nq
; q
!= NULL
; q
= nq
) {
1612 q
->m_nextpkt
= NULL
;
1613 if (q
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1614 m
->m_pkthdr
.csum_flags
= 0;
1616 m
->m_pkthdr
.csum_flags
&= q
->m_pkthdr
.csum_flags
;
1617 m
->m_pkthdr
.csum_data
+= q
->m_pkthdr
.csum_data
;
1624 * Extract firewall instructions from the fragment structure.
1627 *divinfo
= fp
->ipq_div_info
;
1629 *divinfo
= fp
->ipq_divert
;
1631 *divcookie
= fp
->ipq_div_cookie
;
1635 mac_mbuf_label_associate_ipq(fp
, m
);
1636 mac_ipq_label_destroy(fp
);
1639 * Create header for new ip packet by
1640 * modifying header of first packet;
1641 * dequeue and discard fragment reassembly header.
1642 * Make header visible.
1645 ip
->ip_src
= fp
->ipq_src
;
1646 ip
->ip_dst
= fp
->ipq_dst
;
1648 TAILQ_REMOVE(&ipq_list
, fp
, ipq_list
);
1649 currentfrags
-= fp
->ipq_nfrags
;
1651 (void) m_free(dtom(fp
));
1652 m
->m_len
+= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1653 m
->m_data
-= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1654 /* some debugging cruft by sklower, below, will go away soon */
1655 if (m
->m_flags
& M_PKTHDR
) { /* XXX this should be done elsewhere */
1657 for (t
= m
; t
; t
= t
->m_next
)
1659 m
->m_pkthdr
.len
= plen
;
1668 OSAddAtomic(1, &ipstat
.ips_fragdropped
);
1678 * Free a fragment reassembly header and all
1679 * associated datagrams.
1682 ip_freef(struct ipq
*fp
)
1684 lck_mtx_assert(ip_mutex
, LCK_MTX_ASSERT_OWNED
);
1685 currentfrags
-= fp
->ipq_nfrags
;
1686 m_freem_list(fp
->ipq_frags
);
1688 TAILQ_REMOVE(&ipq_list
, fp
, ipq_list
);
1689 (void) m_free(dtom(fp
));
1694 * IP timer processing;
1695 * if a timer expires on a reassembly
1696 * queue, discard it.
1703 lck_mtx_lock(ip_mutex
);
1704 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1708 while (fp
!= &ipq
[i
]) {
1711 if (fp
->prev
->ipq_ttl
== 0) {
1712 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragtimeout
);
1718 * If we are over the maximum number of fragments
1719 * (due to the limit being lowered), drain off
1720 * enough to get down to the new limit.
1722 if (maxnipq
>= 0 && nipq
> maxnipq
) {
1723 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1724 while (nipq
> maxnipq
&&
1725 (ipq
[i
].next
!= &ipq
[i
])) {
1726 OSAddAtomic(ipq
[i
].next
->ipq_nfrags
, &ipstat
.ips_fragdropped
);
1727 ip_freef(ipq
[i
].next
);
1734 lck_mtx_unlock(ip_mutex
);
1738 * Drain off all datagram fragments.
1745 lck_mtx_lock(ip_mutex
);
1746 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1747 while (ipq
[i
].next
!= &ipq
[i
]) {
1748 OSAddAtomic(ipq
[i
].next
->ipq_nfrags
, &ipstat
.ips_fragdropped
);
1749 ip_freef(ipq
[i
].next
);
1752 lck_mtx_unlock(ip_mutex
);
1757 * Do option processing on a datagram,
1758 * possibly discarding it if bad options are encountered,
1759 * or forwarding it if source-routed.
1760 * The pass argument is used when operating in the IPSTEALTH
1761 * mode to tell what options to process:
1762 * [LS]SRR (pass 0) or the others (pass 1).
1763 * The reason for as many as two passes is that when doing IPSTEALTH,
1764 * non-routing options should be processed only if the packet is for us.
1765 * Returns 1 if packet has been forwarded/freed,
1766 * 0 if the packet should be processed further.
1769 ip_dooptions(struct mbuf
*m
, __unused
int pass
, struct sockaddr_in
*next_hop
)
1771 struct ip
*ip
= mtod(m
, struct ip
*);
1773 struct ip_timestamp
*ipt
;
1774 struct in_ifaddr
*ia
;
1775 int opt
, optlen
, cnt
, off
, code
, type
= ICMP_PARAMPROB
, forward
= 0;
1776 struct in_addr
*sin
, dst
;
1778 struct sockaddr_in ipaddr
= {
1779 sizeof (ipaddr
), AF_INET
, 0 , { 0 }, { 0, } };
1782 cp
= (u_char
*)(ip
+ 1);
1783 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1784 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1785 opt
= cp
[IPOPT_OPTVAL
];
1786 if (opt
== IPOPT_EOL
)
1788 if (opt
== IPOPT_NOP
)
1791 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
)) {
1792 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1795 optlen
= cp
[IPOPT_OLEN
];
1796 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
) {
1797 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1807 * Source routing with record.
1808 * Find interface with current destination address.
1809 * If none on this machine then drop if strictly routed,
1810 * or do nothing if loosely routed.
1811 * Record interface address and bring up next address
1812 * component. If strictly routed make sure next
1813 * address is on directly accessible net.
1817 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1818 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1821 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1822 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1825 ipaddr
.sin_addr
= ip
->ip_dst
;
1826 ia
= (struct in_ifaddr
*)
1827 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
1829 if (opt
== IPOPT_SSRR
) {
1830 type
= ICMP_UNREACH
;
1831 code
= ICMP_UNREACH_SRCFAIL
;
1834 if (!ip_dosourceroute
)
1835 goto nosourcerouting
;
1837 * Loose routing, and not at next destination
1838 * yet; nothing to do except forward.
1843 ifafree(&ia
->ia_ifa
);
1846 off
--; /* 0 origin */
1847 if (off
> optlen
- (int)sizeof(struct in_addr
)) {
1849 * End of source route. Should be for us.
1851 if (!ip_acceptsourceroute
)
1852 goto nosourcerouting
;
1853 save_rte(cp
, ip
->ip_src
);
1857 if (!ip_dosourceroute
) {
1859 char buf
[MAX_IPv4_STR_LEN
];
1860 char buf2
[MAX_IPv4_STR_LEN
];
1862 * Acting as a router, so generate ICMP
1866 "attempted source route from %s to %s\n",
1867 inet_ntop(AF_INET
, &ip
->ip_src
, buf
, sizeof(buf
)),
1868 inet_ntop(AF_INET
, &ip
->ip_dst
, buf2
, sizeof(buf2
)));
1869 type
= ICMP_UNREACH
;
1870 code
= ICMP_UNREACH_SRCFAIL
;
1874 * Not acting as a router, so silently drop.
1876 OSAddAtomic(1, &ipstat
.ips_cantforward
);
1883 * locate outgoing interface
1885 (void)memcpy(&ipaddr
.sin_addr
, cp
+ off
,
1886 sizeof(ipaddr
.sin_addr
));
1888 if (opt
== IPOPT_SSRR
) {
1889 #define INA struct in_ifaddr *
1890 #define SA struct sockaddr *
1891 if ((ia
= (INA
)ifa_ifwithdstaddr((SA
)&ipaddr
)) == 0) {
1892 ia
= (INA
)ifa_ifwithnet((SA
)&ipaddr
);
1895 ia
= ip_rtaddr(ipaddr
.sin_addr
);
1898 type
= ICMP_UNREACH
;
1899 code
= ICMP_UNREACH_SRCFAIL
;
1902 ip
->ip_dst
= ipaddr
.sin_addr
;
1903 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1904 sizeof(struct in_addr
));
1905 ifafree(&ia
->ia_ifa
);
1907 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1909 * Let ip_intr's mcast routing check handle mcast pkts
1911 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
1915 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1916 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1919 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1920 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1924 * If no space remains, ignore.
1926 off
--; /* 0 origin */
1927 if (off
> optlen
- (int)sizeof(struct in_addr
))
1929 (void)memcpy(&ipaddr
.sin_addr
, &ip
->ip_dst
,
1930 sizeof(ipaddr
.sin_addr
));
1932 * locate outgoing interface; if we're the destination,
1933 * use the incoming interface (should be same).
1935 if ((ia
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0) {
1936 if ((ia
= ip_rtaddr(ipaddr
.sin_addr
)) == 0) {
1937 type
= ICMP_UNREACH
;
1938 code
= ICMP_UNREACH_HOST
;
1942 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1943 sizeof(struct in_addr
));
1944 ifafree(&ia
->ia_ifa
);
1946 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1950 code
= cp
- (u_char
*)ip
;
1951 ipt
= (struct ip_timestamp
*)cp
;
1952 if (ipt
->ipt_len
< 4 || ipt
->ipt_len
> 40) {
1953 code
= (u_char
*)&ipt
->ipt_len
- (u_char
*)ip
;
1956 if (ipt
->ipt_ptr
< 5) {
1957 code
= (u_char
*)&ipt
->ipt_ptr
- (u_char
*)ip
;
1961 ipt
->ipt_len
- (int)sizeof(int32_t)) {
1962 if (++ipt
->ipt_oflw
== 0) {
1963 code
= (u_char
*)&ipt
->ipt_ptr
-
1969 sin
= (struct in_addr
*)(cp
+ ipt
->ipt_ptr
- 1);
1970 switch (ipt
->ipt_flg
) {
1972 case IPOPT_TS_TSONLY
:
1975 case IPOPT_TS_TSANDADDR
:
1976 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1977 sizeof(struct in_addr
) > ipt
->ipt_len
) {
1978 code
= (u_char
*)&ipt
->ipt_ptr
-
1982 ipaddr
.sin_addr
= dst
;
1983 ia
= (INA
)ifaof_ifpforaddr((SA
)&ipaddr
,
1987 (void)memcpy(sin
, &IA_SIN(ia
)->sin_addr
,
1988 sizeof(struct in_addr
));
1989 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
1990 ifafree(&ia
->ia_ifa
);
1994 case IPOPT_TS_PRESPEC
:
1995 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1996 sizeof(struct in_addr
) > ipt
->ipt_len
) {
1997 code
= (u_char
*)&ipt
->ipt_ptr
-
2001 (void)memcpy(&ipaddr
.sin_addr
, sin
,
2002 sizeof(struct in_addr
));
2003 if ((ia
= (struct in_ifaddr
*)ifa_ifwithaddr((SA
)&ipaddr
)) == 0)
2005 ifafree(&ia
->ia_ifa
);
2007 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
2011 /* XXX can't take &ipt->ipt_flg */
2012 code
= (u_char
*)&ipt
->ipt_ptr
-
2017 (void)memcpy(cp
+ ipt
->ipt_ptr
- 1, &ntime
,
2019 ipt
->ipt_ptr
+= sizeof(n_time
);
2022 if (forward
&& ipforwarding
) {
2023 ip_forward(m
, 1, next_hop
);
2028 ip
->ip_len
-= IP_VHL_HL(ip
->ip_vhl
) << 2; /* XXX icmp_error adds in hdr length */
2029 icmp_error(m
, type
, code
, 0, 0);
2030 OSAddAtomic(1, &ipstat
.ips_badoptions
);
2035 * Given address of next destination (final or next hop),
2036 * return internet address info of interface to be used to get there.
2039 ip_rtaddr(struct in_addr dst
)
2041 struct sockaddr_in
*sin
;
2042 struct ifaddr
*rt_ifa
;
2045 bzero(&ro
, sizeof (ro
));
2046 sin
= (struct sockaddr_in
*)&ro
.ro_dst
;
2047 sin
->sin_family
= AF_INET
;
2048 sin
->sin_len
= sizeof (*sin
);
2049 sin
->sin_addr
= dst
;
2051 rtalloc_ign(&ro
, RTF_PRCLONING
);
2052 if (ro
.ro_rt
== NULL
)
2056 if ((rt_ifa
= ro
.ro_rt
->rt_ifa
) != NULL
)
2058 RT_UNLOCK(ro
.ro_rt
);
2061 return ((struct in_ifaddr
*)rt_ifa
);
2065 * Save incoming source route for use in replies,
2066 * to be picked up later by ip_srcroute if the receiver is interested.
2069 save_rte(u_char
*option
, struct in_addr dst
)
2073 olen
= option
[IPOPT_OLEN
];
2076 printf("save_rte: olen %d\n", olen
);
2078 if (olen
> sizeof(ip_srcrt
) - (1 + sizeof(dst
)))
2080 bcopy(option
, ip_srcrt
.srcopt
, olen
);
2081 ip_nhops
= (olen
- IPOPT_OFFSET
- 1) / sizeof(struct in_addr
);
2086 * Retrieve incoming source route for use in replies,
2087 * in the same form used by setsockopt.
2088 * The first hop is placed before the options, will be removed later.
2093 struct in_addr
*p
, *q
;
2097 return ((struct mbuf
*)0);
2098 m
= m_get(M_DONTWAIT
, MT_HEADER
);
2100 return ((struct mbuf
*)0);
2102 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
2104 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
2105 m
->m_len
= ip_nhops
* sizeof(struct in_addr
) + sizeof(struct in_addr
) +
2109 printf("ip_srcroute: nhops %d mlen %d", ip_nhops
, m
->m_len
);
2113 * First save first hop for return route
2115 p
= &ip_srcrt
.route
[ip_nhops
- 1];
2116 *(mtod(m
, struct in_addr
*)) = *p
--;
2119 printf(" hops %lx", (u_int32_t
)ntohl(mtod(m
, struct in_addr
*)->s_addr
));
2123 * Copy option fields and padding (nop) to mbuf.
2125 ip_srcrt
.nop
= IPOPT_NOP
;
2126 ip_srcrt
.srcopt
[IPOPT_OFFSET
] = IPOPT_MINOFF
;
2127 (void)memcpy(mtod(m
, caddr_t
) + sizeof(struct in_addr
),
2128 &ip_srcrt
.nop
, OPTSIZ
);
2129 q
= (struct in_addr
*)(mtod(m
, caddr_t
) +
2130 sizeof(struct in_addr
) + OPTSIZ
);
2133 * Record return path as an IP source route,
2134 * reversing the path (pointers are now aligned).
2136 while (p
>= ip_srcrt
.route
) {
2139 printf(" %lx", (u_int32_t
)ntohl(q
->s_addr
));
2144 * Last hop goes to final destination.
2149 printf(" %lx\n", (u_int32_t
)ntohl(q
->s_addr
));
2155 * Strip out IP options, at higher
2156 * level protocol in the kernel.
2157 * Second argument is buffer to which options
2158 * will be moved, and return value is their length.
2159 * XXX should be deleted; last arg currently ignored.
2162 ip_stripoptions(struct mbuf
*m
, __unused
struct mbuf
*mopt
)
2165 struct ip
*ip
= mtod(m
, struct ip
*);
2169 olen
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
2170 opts
= (caddr_t
)(ip
+ 1);
2171 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
2172 bcopy(opts
+ olen
, opts
, (unsigned)i
);
2174 if (m
->m_flags
& M_PKTHDR
)
2175 m
->m_pkthdr
.len
-= olen
;
2176 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
2179 u_char inetctlerrmap
[PRC_NCMDS
] = {
2181 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
2182 ENETUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
2183 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
2185 ENOPROTOOPT
, ECONNREFUSED
2189 sysctl_ipforwarding SYSCTL_HANDLER_ARGS
2191 #pragma unused(arg1, arg2)
2192 int i
, was_ipforwarding
= ipforwarding
;
2194 i
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
2195 if (i
!= 0 || req
->newptr
== USER_ADDR_NULL
)
2198 if (was_ipforwarding
&& !ipforwarding
) {
2199 /* clean up IPv4 forwarding cached routes */
2200 ifnet_head_lock_shared();
2201 for (i
= 0; i
<= if_index
; i
++) {
2202 struct ifnet
*ifp
= ifindex2ifnet
[i
];
2204 lck_mtx_lock(ifp
->if_fwd_route_lock
);
2205 if (ifp
->if_fwd_route
.ro_rt
!= NULL
) {
2206 rtfree(ifp
->if_fwd_route
.ro_rt
);
2207 ifp
->if_fwd_route
.ro_rt
= NULL
;
2209 lck_mtx_unlock(ifp
->if_fwd_route_lock
);
2219 * Similar to inp_route_{copyout,copyin} routines except that these copy
2220 * out the cached IPv4 forwarding route from struct ifnet instead of the
2221 * inpcb. See comments for those routines for explanations.
2224 ip_fwd_route_copyout(struct ifnet
*ifp
, struct route
*dst
)
2226 struct route
*src
= &ifp
->if_fwd_route
;
2228 lck_mtx_lock(ifp
->if_fwd_route_lock
);
2230 /* Minor sanity check */
2231 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET
)
2232 panic("%s: wrong or corrupted route: %p", __func__
, src
);
2234 /* Copy everything (rt, dst, flags) from ifnet */
2235 bcopy(src
, dst
, sizeof (*dst
));
2237 /* Hold one reference for the local copy of struct route */
2238 if (dst
->ro_rt
!= NULL
)
2239 RT_ADDREF(dst
->ro_rt
);
2241 lck_mtx_unlock(ifp
->if_fwd_route_lock
);
2245 ip_fwd_route_copyin(struct ifnet
*ifp
, struct route
*src
)
2247 struct route
*dst
= &ifp
->if_fwd_route
;
2249 lck_mtx_lock(ifp
->if_fwd_route_lock
);
2251 /* Minor sanity check */
2252 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET
)
2253 panic("%s: wrong or corrupted route: %p", __func__
, src
);
2255 /* No cached route in the ifnet? */
2256 if (dst
->ro_rt
== NULL
) {
2258 * Copy everything (rt, dst, flags) from ip_forward();
2259 * the reference to the route was held at the time
2260 * it was allocated and is kept intact.
2262 bcopy(src
, dst
, sizeof (*dst
));
2263 } else if (src
->ro_rt
!= NULL
) {
2265 * If the same, update just the ro_flags and ditch the one
2266 * in the local copy. Else ditch the one that is currently
2267 * cached, and cache what we got back from ip_output().
2269 if (dst
->ro_rt
== src
->ro_rt
) {
2270 dst
->ro_flags
= src
->ro_flags
;
2275 bcopy(src
, dst
, sizeof (*dst
));
2279 lck_mtx_unlock(ifp
->if_fwd_route_lock
);
2283 * Forward a packet. If some error occurs return the sender
2284 * an icmp packet. Note we can't always generate a meaningful
2285 * icmp message because icmp doesn't have a large enough repertoire
2286 * of codes and types.
2288 * If not forwarding, just drop the packet. This could be confusing
2289 * if ipforwarding was zero but some routing protocol was advancing
2290 * us as a gateway to somewhere. However, we must let the routing
2291 * protocol deal with that.
2293 * The srcrt parameter indicates whether the packet is being forwarded
2294 * via a source route.
2297 ip_forward(struct mbuf
*m
, int srcrt
, struct sockaddr_in
*next_hop
)
2300 #pragma unused(next_hop)
2302 struct ip
*ip
= mtod(m
, struct ip
*);
2303 struct sockaddr_in
*sin
;
2305 struct route fwd_rt
;
2306 int error
, type
= 0, code
= 0;
2309 struct in_addr pkt_dst
;
2310 u_int32_t nextmtu
= 0;
2311 struct ip_out_args ipoa
= { IFSCOPE_NONE
};
2312 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
2314 struct pf_mtag
*pf_mtag
;
2320 * Cache the destination address of the packet; this may be
2321 * changed by use of 'ipfw fwd'.
2323 pkt_dst
= next_hop
? next_hop
->sin_addr
: ip
->ip_dst
;
2325 pkt_dst
= ip
->ip_dst
;
2330 printf("forward: src %lx dst %lx ttl %x\n",
2331 (u_int32_t
)ip
->ip_src
.s_addr
, (u_int32_t
)pkt_dst
.s_addr
,
2335 if (m
->m_flags
& (M_BCAST
|M_MCAST
) || in_canforward(pkt_dst
) == 0) {
2336 OSAddAtomic(1, &ipstat
.ips_cantforward
);
2343 if (ip
->ip_ttl
<= IPTTLDEC
) {
2344 icmp_error(m
, ICMP_TIMXCEED
, ICMP_TIMXCEED_INTRANS
,
2353 pf_mtag
= pf_find_mtag(m
);
2354 if (pf_mtag
!= NULL
&& pf_mtag
->rtableid
!= IFSCOPE_NONE
)
2355 ipoa
.ipoa_ifscope
= pf_mtag
->rtableid
;
2358 ip_fwd_route_copyout(ifp
, &fwd_rt
);
2360 sin
= (struct sockaddr_in
*)&fwd_rt
.ro_dst
;
2361 if (fwd_rt
.ro_rt
== NULL
||
2362 fwd_rt
.ro_rt
->generation_id
!= route_generation
||
2363 pkt_dst
.s_addr
!= sin
->sin_addr
.s_addr
) {
2364 if (fwd_rt
.ro_rt
!= NULL
) {
2365 rtfree(fwd_rt
.ro_rt
);
2366 fwd_rt
.ro_rt
= NULL
;
2368 sin
->sin_family
= AF_INET
;
2369 sin
->sin_len
= sizeof (*sin
);
2370 sin
->sin_addr
= pkt_dst
;
2372 rtalloc_scoped_ign(&fwd_rt
, RTF_PRCLONING
, ipoa
.ipoa_ifscope
);
2373 if (fwd_rt
.ro_rt
== NULL
) {
2374 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_HOST
, dest
, 0);
2381 * Save the IP header and at most 8 bytes of the payload,
2382 * in case we need to generate an ICMP message to the src.
2384 * We don't use m_copy() because it might return a reference
2385 * to a shared cluster. Both this function and ip_output()
2386 * assume exclusive access to the IP header in `m', so any
2387 * data in a cluster may change before we reach icmp_error().
2389 MGET(mcopy
, M_DONTWAIT
, m
->m_type
);
2390 if (mcopy
!= NULL
) {
2391 M_COPY_PKTHDR(mcopy
, m
);
2392 mcopy
->m_len
= imin((IP_VHL_HL(ip
->ip_vhl
) << 2) + 8,
2394 m_copydata(m
, 0, mcopy
->m_len
, mtod(mcopy
, caddr_t
));
2400 ip
->ip_ttl
-= IPTTLDEC
;
2406 * If forwarding packet using same interface that it came in on,
2407 * perhaps should send a redirect to sender to shortcut a hop.
2408 * Only send redirect if source is sending directly to us,
2409 * and if packet was not source routed (or has any options).
2410 * Also, don't send redirect if forwarding using a default route
2411 * or a route modified by a redirect.
2414 if (rt
->rt_ifp
== m
->m_pkthdr
.rcvif
&&
2415 (rt
->rt_flags
& (RTF_DYNAMIC
|RTF_MODIFIED
)) == 0 &&
2416 satosin(rt_key(rt
))->sin_addr
.s_addr
!= 0 &&
2417 ipsendredirects
&& !srcrt
) {
2418 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
2419 u_int32_t src
= ntohl(ip
->ip_src
.s_addr
);
2422 (src
& RTA(rt
)->ia_subnetmask
) == RTA(rt
)->ia_subnet
) {
2423 if (rt
->rt_flags
& RTF_GATEWAY
)
2424 dest
= satosin(rt
->rt_gateway
)->sin_addr
.s_addr
;
2426 dest
= pkt_dst
.s_addr
;
2427 /* Router requirements says to only send host redirects */
2428 type
= ICMP_REDIRECT
;
2429 code
= ICMP_REDIRECT_HOST
;
2432 printf("redirect (%d) to %lx\n", code
, (u_int32_t
)dest
);
2440 /* Pass IPFORWARD info if available */
2442 struct ip_fwd_tag
*ipfwd_tag
;
2444 tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
,
2445 KERNEL_TAG_TYPE_IPFORWARD
,
2446 sizeof (*ipfwd_tag
), M_NOWAIT
);
2453 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
2454 ipfwd_tag
->next_hop
= next_hop
;
2456 m_tag_prepend(m
, tag
);
2459 error
= ip_output_list(m
, 0, NULL
, &fwd_rt
,
2460 IP_FORWARDING
| IP_OUTARGS
, 0, &ipoa
);
2462 /* Refresh rt since the route could have changed while in IP */
2466 OSAddAtomic(1, &ipstat
.ips_cantforward
);
2468 OSAddAtomic(1, &ipstat
.ips_forward
);
2470 OSAddAtomic(1, &ipstat
.ips_redirectsent
);
2474 ipflow_create(&fwd_rt
, mcopy
);
2477 * If we didn't have to go thru ipflow and
2478 * the packet was successfully consumed by
2479 * ip_output, the mcopy is rather a waste;
2480 * this could be further optimized.
2492 case 0: /* forwarded, but need redirect */
2493 /* type, code set above */
2496 case ENETUNREACH
: /* shouldn't happen, checked above */
2501 type
= ICMP_UNREACH
;
2502 code
= ICMP_UNREACH_HOST
;
2506 type
= ICMP_UNREACH
;
2507 code
= ICMP_UNREACH_NEEDFRAG
;
2511 if (rt
->rt_ifp
!= NULL
)
2512 nextmtu
= rt
->rt_ifp
->if_mtu
;
2517 * If the packet is routed over IPsec tunnel, tell the
2518 * originator the tunnel MTU.
2519 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
2523 struct secpolicy
*sp
= NULL
;
2529 if (rt
->rt_ifp
!= NULL
)
2530 nextmtu
= rt
->rt_ifp
->if_mtu
;
2534 OSAddAtomic(1, &ipstat
.ips_cantfrag
);
2537 sp
= ipsec4_getpolicybyaddr(mcopy
,
2543 /* count IPsec header size */
2544 ipsechdr
= ipsec_hdrsiz(sp
);
2547 * find the correct route for outer IPv4
2548 * header, compute tunnel MTU.
2552 if (sp
->req
!= NULL
) {
2553 if (sp
->req
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2554 struct secasindex saidx
;
2556 struct secasvar
*sav
;
2558 ipm
= mtod(mcopy
, struct ip
*);
2559 bcopy(&sp
->req
->saidx
, &saidx
, sizeof(saidx
));
2560 saidx
.mode
= sp
->req
->saidx
.mode
;
2561 saidx
.reqid
= sp
->req
->saidx
.reqid
;
2562 sin
= (struct sockaddr_in
*)&saidx
.src
;
2563 if (sin
->sin_len
== 0) {
2564 sin
->sin_len
= sizeof(*sin
);
2565 sin
->sin_family
= AF_INET
;
2566 sin
->sin_port
= IPSEC_PORT_ANY
;
2567 bcopy(&ipm
->ip_src
, &sin
->sin_addr
,
2568 sizeof(sin
->sin_addr
));
2570 sin
= (struct sockaddr_in
*)&saidx
.dst
;
2571 if (sin
->sin_len
== 0) {
2572 sin
->sin_len
= sizeof(*sin
);
2573 sin
->sin_family
= AF_INET
;
2574 sin
->sin_port
= IPSEC_PORT_ANY
;
2575 bcopy(&ipm
->ip_dst
, &sin
->sin_addr
,
2576 sizeof(sin
->sin_addr
));
2578 sav
= key_allocsa_policy(&saidx
);
2580 if (sav
->sah
!= NULL
) {
2581 ro
= &sav
->sah
->sa_route
;
2582 if (ro
->ro_rt
!= NULL
) {
2584 if (ro
->ro_rt
->rt_ifp
!= NULL
) {
2585 nextmtu
= ro
->ro_rt
->rt_ifp
->if_mtu
;
2586 nextmtu
-= ipsechdr
;
2588 RT_UNLOCK(ro
->ro_rt
);
2591 key_freesav(sav
, KEY_SADB_UNLOCKED
);
2595 key_freesp(sp
, KEY_SADB_UNLOCKED
);
2599 OSAddAtomic(1, &ipstat
.ips_cantfrag
);
2603 type
= ICMP_SOURCEQUENCH
;
2607 case EACCES
: /* ipfw denied packet */
2612 icmp_error(mcopy
, type
, code
, dest
, nextmtu
);
2614 ip_fwd_route_copyin(ifp
, &fwd_rt
);
2624 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
2628 *mp
= sbcreatecontrol((caddr_t
) &tv
, sizeof(tv
),
2629 SCM_TIMESTAMP
, SOL_SOCKET
);
2631 mp
= &(*mp
)->m_next
;
2633 if (inp
->inp_flags
& INP_RECVDSTADDR
) {
2634 *mp
= sbcreatecontrol((caddr_t
) &ip
->ip_dst
,
2635 sizeof(struct in_addr
), IP_RECVDSTADDR
, IPPROTO_IP
);
2637 mp
= &(*mp
)->m_next
;
2641 * Moving these out of udp_input() made them even more broken
2642 * than they already were.
2644 /* options were tossed already */
2645 if (inp
->inp_flags
& INP_RECVOPTS
) {
2646 *mp
= sbcreatecontrol((caddr_t
) opts_deleted_above
,
2647 sizeof(struct in_addr
), IP_RECVOPTS
, IPPROTO_IP
);
2649 mp
= &(*mp
)->m_next
;
2651 /* ip_srcroute doesn't do what we want here, need to fix */
2652 if (inp
->inp_flags
& INP_RECVRETOPTS
) {
2653 *mp
= sbcreatecontrol((caddr_t
) ip_srcroute(),
2654 sizeof(struct in_addr
), IP_RECVRETOPTS
, IPPROTO_IP
);
2656 mp
= &(*mp
)->m_next
;
2659 if (inp
->inp_flags
& INP_RECVIF
) {
2662 struct sockaddr_dl sdl
;
2665 struct sockaddr_dl
*sdp
;
2666 struct sockaddr_dl
*sdl2
= &sdlbuf
.sdl
;
2668 ifnet_head_lock_shared();
2669 if (((ifp
= m
->m_pkthdr
.rcvif
))
2670 && ( ifp
->if_index
&& (ifp
->if_index
<= if_index
))) {
2671 struct ifaddr
*ifa
= ifnet_addrs
[ifp
->if_index
- 1];
2673 if (!ifa
|| !ifa
->ifa_addr
)
2676 sdp
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
2678 * Change our mind and don't try copy.
2680 if ((sdp
->sdl_family
!= AF_LINK
)
2681 || (sdp
->sdl_len
> sizeof(sdlbuf
))) {
2684 bcopy(sdp
, sdl2
, sdp
->sdl_len
);
2688 = offsetof(struct sockaddr_dl
, sdl_data
[0]);
2689 sdl2
->sdl_family
= AF_LINK
;
2690 sdl2
->sdl_index
= 0;
2691 sdl2
->sdl_nlen
= sdl2
->sdl_alen
= sdl2
->sdl_slen
= 0;
2694 *mp
= sbcreatecontrol((caddr_t
) sdl2
, sdl2
->sdl_len
,
2695 IP_RECVIF
, IPPROTO_IP
);
2697 mp
= &(*mp
)->m_next
;
2699 if (inp
->inp_flags
& INP_RECVTTL
) {
2700 *mp
= sbcreatecontrol((caddr_t
)&ip
->ip_ttl
, sizeof(ip
->ip_ttl
), IP_RECVTTL
, IPPROTO_IP
);
2701 if (*mp
) mp
= &(*mp
)->m_next
;
2706 ip_rsvp_init(struct socket
*so
)
2708 if (so
->so_type
!= SOCK_RAW
||
2709 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2712 if (ip_rsvpd
!= NULL
)
2717 * This may seem silly, but we need to be sure we don't over-increment
2718 * the RSVP counter, in case something slips up.
2733 * This may seem silly, but we need to be sure we don't over-decrement
2734 * the RSVP counter, in case something slips up.