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;
562 void (*pr_input
)(struct mbuf
*, int len
);
564 if (!TAILQ_EMPTY(&ipv4_filters
)) {
566 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
568 if ((struct ipfilter
*)inject_ipfref
== filter
)
570 } else if (filter
->ipf_filter
.ipf_input
) {
573 if (changed_header
== 0) {
575 ip
= mtod(m
, struct ip
*);
576 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
577 ip
->ip_off
= htons(ip
->ip_off
);
579 ip
->ip_sum
= in_cksum(m
, hlen
);
581 result
= filter
->ipf_filter
.ipf_input(
582 filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, hlen
, proto
);
583 if (result
== EJUSTRETURN
) {
597 * If there isn't a specific lock for the protocol
598 * we're about to call, use the generic lock for AF_INET.
599 * otherwise let the protocol deal with its own locking
601 ip
= mtod(m
, struct ip
*);
603 if (changed_header
) {
604 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
605 ip
->ip_off
= ntohs(ip
->ip_off
);
608 if ((pr_input
= ip_protox
[ip
->ip_p
]->pr_input
) == NULL
) {
610 } else if (!(ip_protox
[ip
->ip_p
]->pr_flags
& PR_PROTOLOCK
)) {
611 lck_mtx_lock(inet_domain_mutex
);
613 lck_mtx_unlock(inet_domain_mutex
);
620 * Ip input routine. Checksum and byte swap header. If fragmented
621 * try to reassemble. Process options. Pass to next level.
624 ip_input(struct mbuf
*m
)
628 struct in_ifaddr
*ia
= NULL
;
631 struct in_addr pkt_dst
;
634 u_int32_t div_info
= 0; /* packet divert/tee info */
635 struct ip_fw_args args
;
638 ipfilter_t inject_filter_ref
= 0;
644 args
.divert_rule
= 0; /* divert cookie */
645 args
.next_hop
= NULL
;
648 * Don't bother searching for tag(s) if there's none.
650 if (SLIST_EMPTY(&m
->m_pkthdr
.tags
))
653 /* Grab info from mtags prepended to the chain */
655 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
656 KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
657 struct dn_pkt_tag
*dn_tag
;
659 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
660 args
.rule
= dn_tag
->rule
;
662 m_tag_delete(m
, tag
);
664 #endif /* DUMMYNET */
667 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
668 KERNEL_TAG_TYPE_DIVERT
, NULL
)) != NULL
) {
669 struct divert_tag
*div_tag
;
671 div_tag
= (struct divert_tag
*)(tag
+1);
672 args
.divert_rule
= div_tag
->cookie
;
674 m_tag_delete(m
, tag
);
678 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
679 KERNEL_TAG_TYPE_IPFORWARD
, NULL
)) != NULL
) {
680 struct ip_fwd_tag
*ipfwd_tag
;
682 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
683 args
.next_hop
= ipfwd_tag
->next_hop
;
685 m_tag_delete(m
, tag
);
689 if (m
== NULL
|| (m
->m_flags
& M_PKTHDR
) == 0)
690 panic("ip_input no HDR");
693 if (args
.rule
) { /* dummynet already filtered us */
694 ip
= mtod(m
, struct ip
*);
695 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
696 inject_filter_ref
= ipf_get_inject_filter(m
);
700 #endif /* IPFIREWALL */
703 * No need to proccess packet twice if we've already seen it.
705 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
))
706 inject_filter_ref
= ipf_get_inject_filter(m
);
707 if (inject_filter_ref
!= 0) {
708 ip
= mtod(m
, struct ip
*);
709 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
710 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
711 ip
->ip_off
= ntohs(ip
->ip_off
);
712 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, inject_filter_ref
);
716 OSAddAtomic(1, &ipstat
.ips_total
);
718 if (m
->m_pkthdr
.len
< sizeof(struct ip
))
721 if (m
->m_len
< sizeof (struct ip
) &&
722 (m
= m_pullup(m
, sizeof (struct ip
))) == 0) {
723 OSAddAtomic(1, &ipstat
.ips_toosmall
);
726 ip
= mtod(m
, struct ip
*);
728 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
729 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
731 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
732 OSAddAtomic(1, &ipstat
.ips_badvers
);
736 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
737 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
738 OSAddAtomic(1, &ipstat
.ips_badhlen
);
741 if (hlen
> m
->m_len
) {
742 if ((m
= m_pullup(m
, hlen
)) == 0) {
743 OSAddAtomic(1, &ipstat
.ips_badhlen
);
746 ip
= mtod(m
, struct ip
*);
749 /* 127/8 must not appear on wire - RFC1122 */
750 if ((ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
751 (ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
) {
752 if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) {
753 OSAddAtomic(1, &ipstat
.ips_badaddr
);
758 /* IPv4 Link-Local Addresses as defined in <draft-ietf-zeroconf-ipv4-linklocal-05.txt> */
759 if ((IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
)) ||
760 IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)))) {
761 ip_linklocal_stat
.iplls_in_total
++;
762 if (ip
->ip_ttl
!= MAXTTL
) {
763 OSAddAtomic(1, &ip_linklocal_stat
.iplls_in_badttl
);
764 /* Silently drop link local traffic with bad TTL */
765 if (!ip_linklocal_in_allowbadttl
)
769 if ((IF_HWASSIST_CSUM_FLAGS(m
->m_pkthdr
.rcvif
->if_hwassist
) == 0)
770 || (apple_hwcksum_rx
== 0) ||
771 ((m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
) && ip
->ip_p
!= IPPROTO_TCP
)) {
772 m
->m_pkthdr
.csum_flags
= 0; /* invalidate HW generated checksum flags */
775 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
776 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
777 } else if (!(m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) ||
778 apple_hwcksum_tx
== 0) {
780 * Either this is not loopback packet coming from an interface
781 * that does not support checksum offloading, or it is loopback
782 * packet that has undergone software checksumming at the send
783 * side because apple_hwcksum_tx was set to 0. In this case,
784 * calculate the checksum in software to validate the packet.
786 sum
= in_cksum(m
, hlen
);
789 * This is a loopback packet without any valid checksum since
790 * the send side has bypassed it (apple_hwcksum_tx set to 1).
791 * We get here because apple_hwcksum_rx was set to 0, and so
792 * we pretend that all is well.
795 m
->m_pkthdr
.csum_flags
|= CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
796 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
797 m
->m_pkthdr
.csum_data
= 0xffff;
800 OSAddAtomic(1, &ipstat
.ips_badsum
);
805 * Convert fields to host representation.
807 #if BYTE_ORDER != BIG_ENDIAN
811 if (ip
->ip_len
< hlen
) {
812 OSAddAtomic(1, &ipstat
.ips_badlen
);
816 #if BYTE_ORDER != BIG_ENDIAN
820 * Check that the amount of data in the buffers
821 * is as at least much as the IP header would have us expect.
822 * Trim mbufs if longer than we expect.
823 * Drop packet if shorter than we expect.
825 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
827 OSAddAtomic(1, &ipstat
.ips_tooshort
);
830 if (m
->m_pkthdr
.len
> ip
->ip_len
) {
831 /* Invalidate hwcksuming */
832 m
->m_pkthdr
.csum_flags
= 0;
833 m
->m_pkthdr
.csum_data
= 0;
835 if (m
->m_len
== m
->m_pkthdr
.len
) {
836 m
->m_len
= ip
->ip_len
;
837 m
->m_pkthdr
.len
= ip
->ip_len
;
839 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
843 if (ipsec_bypass
== 0 && ipsec_gethist(m
, NULL
))
849 * Right now when no processing on packet has done
850 * and it is still fresh out of network we do our black
852 * - Firewall: deny/allow/divert
853 * - Xlate: translate packet's addr/port (NAT).
854 * - Pipe: pass pkt through dummynet.
855 * - Wrap: fake packet's addr/port <unimpl.>
856 * - Encapsulate: put it in another IP and send out. <unimp.>
859 /* Invoke inbound packet filter */
860 if (pf_af_hook(m
->m_pkthdr
.rcvif
, NULL
, &m
, AF_INET
, TRUE
) != 0) {
862 panic("%s: unexpected packet %p\n", __func__
, m
);
865 /* Already freed by callee */
868 ip
= mtod(m
, struct ip
*);
869 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
875 #endif /* DUMMYNET */
877 * Check if we want to allow this packet to be processed.
878 * Consider it to be bad if not.
883 if (fr_checkp(ip
, hlen
, m
->m_pkthdr
.rcvif
, 0, &m1
) || !m1
) {
886 ip
= mtod(m
= m1
, struct ip
*);
888 if (fw_enable
&& IPFW_LOADED
) {
889 #if IPFIREWALL_FORWARD
891 * If we've been forwarded from the output side, then
892 * skip the firewall a second time
896 #endif /* IPFIREWALL_FORWARD */
900 i
= ip_fw_chk_ptr(&args
);
903 if ( (i
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) { /* drop */
908 ip
= mtod(m
, struct ip
*); /* just in case m changed */
910 if (i
== 0 && args
.next_hop
== NULL
) { /* common case */
914 if (DUMMYNET_LOADED
&& (i
& IP_FW_PORT_DYNT_FLAG
) != 0) {
915 /* Send packet to the appropriate pipe */
916 ip_dn_io_ptr(m
, i
&0xffff, DN_TO_IP_IN
, &args
);
919 #endif /* DUMMYNET */
921 if (i
!= 0 && (i
& IP_FW_PORT_DYNT_FLAG
) == 0) {
922 /* Divert or tee packet */
927 #if IPFIREWALL_FORWARD
928 if (i
== 0 && args
.next_hop
!= NULL
) {
933 * if we get here, the packet must be dropped
938 #endif /* IPFIREWALL */
942 * Process options and, if not destined for us,
943 * ship it on. ip_dooptions returns 1 when an
944 * error was detected (causing an icmp message
945 * to be sent and the original packet to be freed).
947 ip_nhops
= 0; /* for source routed packets */
949 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, args
.next_hop
)) {
951 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, NULL
)) {
956 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
957 * matter if it is destined to another node, or whether it is
958 * a multicast one, RSVP wants it! and prevents it from being forwarded
959 * anywhere else. Also checks if the rsvp daemon is running before
960 * grabbing the packet.
962 if (rsvp_on
&& ip
->ip_p
==IPPROTO_RSVP
)
966 * Check our list of addresses, to see if the packet is for us.
967 * If we don't have any addresses, assume any unicast packet
968 * we receive might be for us (and let the upper layers deal
971 if (TAILQ_EMPTY(&in_ifaddrhead
) &&
972 (m
->m_flags
& (M_MCAST
|M_BCAST
)) == 0)
976 * Cache the destination address of the packet; this may be
977 * changed by use of 'ipfw fwd'.
980 pkt_dst
= args
.next_hop
== NULL
?
981 ip
->ip_dst
: args
.next_hop
->sin_addr
;
983 pkt_dst
= ip
->ip_dst
;
987 * Enable a consistency check between the destination address
988 * and the arrival interface for a unicast packet (the RFC 1122
989 * strong ES model) if IP forwarding is disabled and the packet
990 * is not locally generated and the packet is not subject to
993 * XXX - Checking also should be disabled if the destination
994 * address is ipnat'ed to a different interface.
996 * XXX - Checking is incompatible with IP aliases added
997 * to the loopback interface instead of the interface where
998 * the packets are received.
1000 checkif
= ip_checkinterface
&& (ipforwarding
== 0) &&
1001 ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0)
1003 && (args
.next_hop
== NULL
);
1009 * Check for exact addresses in the hash bucket.
1011 lck_rw_lock_shared(in_ifaddr_rwlock
);
1012 TAILQ_FOREACH(ia
, INADDR_HASH(pkt_dst
.s_addr
), ia_hash
) {
1014 * If the address matches, verify that the packet
1015 * arrived via the correct interface if checking is
1018 if (IA_SIN(ia
)->sin_addr
.s_addr
== pkt_dst
.s_addr
&&
1019 (!checkif
|| ia
->ia_ifp
== m
->m_pkthdr
.rcvif
)) {
1020 lck_rw_done(in_ifaddr_rwlock
);
1024 lck_rw_done(in_ifaddr_rwlock
);
1027 * Check for broadcast addresses.
1029 * Only accept broadcast packets that arrive via the matching
1030 * interface. Reception of forwarded directed broadcasts would be
1031 * handled via ip_forward() and ether_frameout() with the loopback
1032 * into the stack for SIMPLEX interfaces handled by ether_frameout().
1034 if (m
->m_pkthdr
.rcvif
->if_flags
& IFF_BROADCAST
) {
1036 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1038 ifnet_lock_shared(ifp
);
1039 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1040 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
1043 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
1044 pkt_dst
.s_addr
|| ia
->ia_netbroadcast
.s_addr
==
1046 ifnet_lock_done(ifp
);
1050 ifnet_lock_done(ifp
);
1053 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
1054 struct in_multi
*inm
;
1055 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1059 * If we are acting as a multicast router, all
1060 * incoming multicast packets are passed to the
1061 * kernel-level multicast forwarding function.
1062 * The packet is returned (relatively) intact; if
1063 * ip_mforward() returns a non-zero value, the packet
1064 * must be discarded, else it may be accepted below.
1066 lck_mtx_lock(ip_mutex
);
1067 if (ip_mforward
&& ip_mforward(ip
, ifp
, m
, 0) != 0) {
1068 OSAddAtomic(1, &ipstat
.ips_cantforward
);
1070 lck_mtx_unlock(ip_mutex
);
1075 * The process-level routing daemon needs to receive
1076 * all multicast IGMP packets, whether or not this
1077 * host belongs to their destination groups.
1079 if (ip
->ip_p
== IPPROTO_IGMP
)
1081 OSAddAtomic(1, &ipstat
.ips_forward
);
1083 #endif /* MROUTING */
1085 * See if we belong to the destination multicast group on the
1086 * arrival interface.
1088 ifnet_lock_shared(ifp
);
1089 IN_LOOKUP_MULTI(ip
->ip_dst
, ifp
, inm
);
1090 ifnet_lock_done(ifp
);
1092 OSAddAtomic(1, &ipstat
.ips_notmember
);
1098 if (ip
->ip_dst
.s_addr
== (u_int32_t
)INADDR_BROADCAST
)
1100 if (ip
->ip_dst
.s_addr
== INADDR_ANY
)
1103 /* Allow DHCP/BootP responses through */
1104 if (m
->m_pkthdr
.rcvif
!= NULL
1105 && (m
->m_pkthdr
.rcvif
->if_eflags
& IFEF_AUTOCONFIGURING
)
1106 && hlen
== sizeof(struct ip
)
1107 && ip
->ip_p
== IPPROTO_UDP
) {
1108 struct udpiphdr
*ui
;
1109 if (m
->m_len
< sizeof(struct udpiphdr
)
1110 && (m
= m_pullup(m
, sizeof(struct udpiphdr
))) == 0) {
1111 OSAddAtomic(1, &udpstat
.udps_hdrops
);
1114 ui
= mtod(m
, struct udpiphdr
*);
1115 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) {
1118 ip
= mtod(m
, struct ip
*); /* in case it changed */
1121 #if defined(NFAITH) && 0 < NFAITH
1123 * FAITH(Firewall Aided Internet Translator)
1125 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
== IFT_FAITH
) {
1127 if (ip
->ip_p
== IPPROTO_TCP
|| ip
->ip_p
== IPPROTO_ICMP
)
1135 * Not for us; forward if possible and desirable.
1137 if (ipforwarding
== 0) {
1138 OSAddAtomic(1, &ipstat
.ips_cantforward
);
1142 ip_forward(m
, 0, args
.next_hop
);
1144 ip_forward(m
, 0, NULL
);
1151 * If offset or IP_MF are set, must reassemble.
1152 * Otherwise, nothing need be done.
1153 * (We could look in the reassembly queue to see
1154 * if the packet was previously fragmented,
1155 * but it's not worth the time; just let them time out.)
1157 if (ip
->ip_off
& (IP_MF
| IP_OFFMASK
| IP_RF
)) {
1159 /* If maxnipq is 0, never accept fragments. */
1162 OSAddAtomic(1, &ipstat
.ips_fragments
);
1163 OSAddAtomic(1, &ipstat
.ips_fragdropped
);
1168 * If we will exceed the number of fragments in queues, timeout the
1169 * oldest fragemented packet to make space.
1171 lck_mtx_lock(ip_mutex
);
1172 if (currentfrags
>= maxfrags
) {
1173 fp
= TAILQ_LAST(&ipq_list
, ipq_list
);
1174 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragtimeout
);
1176 if (ip
->ip_id
== fp
->ipq_id
&&
1177 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
1178 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
1179 ip
->ip_p
== fp
->ipq_p
) {
1181 * If we match the fragment queue we were going to
1182 * discard, drop this packet too.
1184 OSAddAtomic(1, &ipstat
.ips_fragdropped
);
1186 lck_mtx_unlock(ip_mutex
);
1193 sum
= IPREASS_HASH(ip
->ip_src
.s_addr
, ip
->ip_id
);
1195 * Look for queue of fragments
1198 for (fp
= ipq
[sum
].next
; fp
!= &ipq
[sum
]; fp
= fp
->next
)
1199 if (ip
->ip_id
== fp
->ipq_id
&&
1200 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
1201 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
1203 mac_ipq_label_compare(m
, fp
) &&
1205 ip
->ip_p
== fp
->ipq_p
)
1209 * Enforce upper bound on number of fragmented packets
1210 * for which we attempt reassembly;
1211 * If maxnipq is -1, accept all fragments without limitation.
1213 if ((nipq
> maxnipq
) && (maxnipq
> 0)) {
1215 * drop the oldest fragment before proceeding further
1217 fp
= TAILQ_LAST(&ipq_list
, ipq_list
);
1218 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragtimeout
);
1226 * Adjust ip_len to not reflect header,
1227 * convert offset of this to bytes.
1230 if (ip
->ip_off
& IP_MF
) {
1232 * Make sure that fragments have a data length
1233 * that's a non-zero multiple of 8 bytes.
1235 if (ip
->ip_len
== 0 || (ip
->ip_len
& 0x7) != 0) {
1236 OSAddAtomic(1, &ipstat
.ips_toosmall
);
1237 lck_mtx_unlock(ip_mutex
);
1240 m
->m_flags
|= M_FRAG
;
1242 /* Clear the flag in case packet comes from loopback */
1243 m
->m_flags
&= ~M_FRAG
;
1248 * Attempt reassembly; if it succeeds, proceed.
1249 * ip_reass() will return a different mbuf, and update
1250 * the divert info in div_info and args.divert_rule.
1252 OSAddAtomic(1, &ipstat
.ips_fragments
);
1253 m
->m_pkthdr
.header
= ip
;
1255 m
= ip_reass(m
, fp
, &ipq
[sum
],
1256 (u_int16_t
*)&div_info
, &args
.divert_rule
);
1258 m
= ip_reass(m
, fp
, &ipq
[sum
]);
1261 lck_mtx_unlock(ip_mutex
);
1264 OSAddAtomic(1, &ipstat
.ips_reassembled
);
1265 ip
= mtod(m
, struct ip
*);
1266 /* Get the header length of the reassembled packet */
1267 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1270 /* Restore original checksum before diverting packet */
1271 if (div_info
!= 0) {
1274 #if BYTE_ORDER != BIG_ENDIAN
1280 ip
->ip_sum
= in_cksum(m
, hlen
);
1282 #if BYTE_ORDER != BIG_ENDIAN
1290 lck_mtx_unlock(ip_mutex
);
1296 * Divert or tee packet to the divert protocol if required.
1298 * If div_info is zero then cookie should be too, so we shouldn't
1299 * need to clear them here. Assume divert_packet() does so also.
1301 if (div_info
!= 0) {
1302 struct mbuf
*clone
= NULL
;
1304 /* Clone packet if we're doing a 'tee' */
1305 if ((div_info
& IP_FW_PORT_TEE_FLAG
) != 0)
1306 clone
= m_dup(m
, M_DONTWAIT
);
1308 /* Restore packet header fields to original values */
1311 #if BYTE_ORDER != BIG_ENDIAN
1315 /* Deliver packet to divert input routine */
1316 OSAddAtomic(1, &ipstat
.ips_delivered
);
1317 divert_packet(m
, 1, div_info
& 0xffff, args
.divert_rule
);
1319 /* If 'tee', continue with original packet */
1320 if (clone
== NULL
) {
1324 ip
= mtod(m
, struct ip
*);
1330 * enforce IPsec policy checking if we are seeing last header.
1331 * note that we do not visit this with protocols with pcb layer
1332 * code - like udp/tcp/raw ip.
1334 if (ipsec_bypass
== 0 && (ip_protox
[ip
->ip_p
]->pr_flags
& PR_LASTHDR
) != 0) {
1335 if (ipsec4_in_reject(m
, NULL
)) {
1336 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
1343 * Switch out to protocol's input routine.
1345 OSAddAtomic(1, &ipstat
.ips_delivered
);
1348 if (args
.next_hop
&& ip
->ip_p
== IPPROTO_TCP
) {
1349 /* TCP needs IPFORWARD info if available */
1350 struct m_tag
*fwd_tag
;
1351 struct ip_fwd_tag
*ipfwd_tag
;
1353 fwd_tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
,
1354 KERNEL_TAG_TYPE_IPFORWARD
, sizeof (*ipfwd_tag
),
1356 if (fwd_tag
== NULL
) {
1360 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
1361 ipfwd_tag
->next_hop
= args
.next_hop
;
1363 m_tag_prepend(m
, fwd_tag
);
1365 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1366 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1369 /* TCP deals with its own locking */
1370 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1372 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1373 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1375 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1378 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1384 KERNEL_DEBUG(DBG_LAYER_END
, 0,0,0,0,0);
1389 * Take incoming datagram fragment and try to reassemble it into
1390 * whole datagram. If a chain for reassembly of this datagram already
1391 * exists, then it is given as fp; otherwise have to make a chain.
1393 * When IPDIVERT enabled, keep additional state with each packet that
1394 * tells us if we need to divert or tee the packet we're building.
1397 static struct mbuf
*
1399 ip_reass(struct mbuf
*m
, struct ipq
*fp
, struct ipq
*where
,
1402 #else /* IPDIVERT_44 */
1404 #endif /* IPDIVERT_44 */
1405 u_int16_t
*divcookie
)
1406 #else /* IPDIVERT */
1407 ip_reass(struct mbuf
*m
, struct ipq
*fp
, struct ipq
*where
)
1408 #endif /* IPDIVERT */
1410 struct ip
*ip
= mtod(m
, struct ip
*);
1411 struct mbuf
*p
= 0, *q
, *nq
;
1413 int hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1417 lck_mtx_assert(ip_mutex
, LCK_MTX_ASSERT_OWNED
);
1419 * Presence of header sizes in mbufs
1420 * would confuse code below.
1425 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1426 m
->m_pkthdr
.csum_flags
= 0;
1428 * If first fragment to arrive, create a reassembly queue.
1431 if ((t
= m_get(M_DONTWAIT
, MT_FTABLE
)) == NULL
)
1433 fp
= mtod(t
, struct ipq
*);
1435 if (mac_ipq_label_init(fp
, M_NOWAIT
) != 0) {
1440 mac_ipq_label_associate(m
, fp
);
1442 insque((void*)fp
, (void*)where
);
1445 fp
->ipq_ttl
= IPFRAGTTL
;
1446 fp
->ipq_p
= ip
->ip_p
;
1447 fp
->ipq_id
= ip
->ip_id
;
1448 fp
->ipq_src
= ip
->ip_src
;
1449 fp
->ipq_dst
= ip
->ip_dst
;
1451 m
->m_nextpkt
= NULL
;
1454 fp
->ipq_div_info
= 0;
1458 fp
->ipq_div_cookie
= 0;
1460 TAILQ_INSERT_HEAD(&ipq_list
, fp
, ipq_list
);
1465 mac_ipq_label_update(m
, fp
);
1469 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
1472 * Handle ECN by comparing this segment with the first one;
1473 * if CE is set, do not lose CE.
1474 * drop if CE and not-ECT are mixed for the same packet.
1476 ecn
= ip
->ip_tos
& IPTOS_ECN_MASK
;
1477 ecn0
= GETIP(fp
->ipq_frags
)->ip_tos
& IPTOS_ECN_MASK
;
1478 if (ecn
== IPTOS_ECN_CE
) {
1479 if (ecn0
== IPTOS_ECN_NOTECT
)
1481 if (ecn0
!= IPTOS_ECN_CE
)
1482 GETIP(fp
->ipq_frags
)->ip_tos
|= IPTOS_ECN_CE
;
1484 if (ecn
== IPTOS_ECN_NOTECT
&& ecn0
!= IPTOS_ECN_NOTECT
)
1488 * Find a segment which begins after this one does.
1490 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
)
1491 if (GETIP(q
)->ip_off
> ip
->ip_off
)
1495 * If there is a preceding segment, it may provide some of
1496 * our data already. If so, drop the data from the incoming
1497 * segment. If it provides all of our data, drop us, otherwise
1498 * stick new segment in the proper place.
1500 * If some of the data is dropped from the the preceding
1501 * segment, then it's checksum is invalidated.
1504 i
= GETIP(p
)->ip_off
+ GETIP(p
)->ip_len
- ip
->ip_off
;
1506 if (i
>= ip
->ip_len
)
1509 m
->m_pkthdr
.csum_flags
= 0;
1513 m
->m_nextpkt
= p
->m_nextpkt
;
1516 m
->m_nextpkt
= fp
->ipq_frags
;
1521 * While we overlap succeeding segments trim them or,
1522 * if they are completely covered, dequeue them.
1524 for (; q
!= NULL
&& ip
->ip_off
+ ip
->ip_len
> GETIP(q
)->ip_off
;
1526 i
= (ip
->ip_off
+ ip
->ip_len
) -
1528 if (i
< GETIP(q
)->ip_len
) {
1529 GETIP(q
)->ip_len
-= i
;
1530 GETIP(q
)->ip_off
+= i
;
1532 q
->m_pkthdr
.csum_flags
= 0;
1537 OSAddAtomic(1, &ipstat
.ips_fragdropped
);
1547 * Transfer firewall instructions to the fragment structure.
1548 * Only trust info in the fragment at offset 0.
1550 if (ip
->ip_off
== 0) {
1552 fp
->ipq_div_info
= *divinfo
;
1554 fp
->ipq_divert
= *divinfo
;
1556 fp
->ipq_div_cookie
= *divcookie
;
1563 * Check for complete reassembly and perform frag per packet
1566 * Frag limiting is performed here so that the nth frag has
1567 * a chance to complete the packet before we drop the packet.
1568 * As a result, n+1 frags are actually allowed per packet, but
1569 * only n will ever be stored. (n = maxfragsperpacket.)
1573 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
) {
1574 if (GETIP(q
)->ip_off
!= next
) {
1575 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
1576 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragdropped
);
1581 next
+= GETIP(q
)->ip_len
;
1583 /* Make sure the last packet didn't have the IP_MF flag */
1584 if (p
->m_flags
& M_FRAG
) {
1585 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
1586 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragdropped
);
1593 * Reassembly is complete. Make sure the packet is a sane size.
1597 if (next
+ (IP_VHL_HL(ip
->ip_vhl
) << 2) > IP_MAXPACKET
) {
1598 OSAddAtomic(1, &ipstat
.ips_toolong
);
1599 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragdropped
);
1605 * Concatenate fragments.
1613 for (q
= nq
; q
!= NULL
; q
= nq
) {
1615 q
->m_nextpkt
= NULL
;
1616 if (q
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1617 m
->m_pkthdr
.csum_flags
= 0;
1619 m
->m_pkthdr
.csum_flags
&= q
->m_pkthdr
.csum_flags
;
1620 m
->m_pkthdr
.csum_data
+= q
->m_pkthdr
.csum_data
;
1627 * Extract firewall instructions from the fragment structure.
1630 *divinfo
= fp
->ipq_div_info
;
1632 *divinfo
= fp
->ipq_divert
;
1634 *divcookie
= fp
->ipq_div_cookie
;
1638 mac_mbuf_label_associate_ipq(fp
, m
);
1639 mac_ipq_label_destroy(fp
);
1642 * Create header for new ip packet by
1643 * modifying header of first packet;
1644 * dequeue and discard fragment reassembly header.
1645 * Make header visible.
1648 ip
->ip_src
= fp
->ipq_src
;
1649 ip
->ip_dst
= fp
->ipq_dst
;
1651 TAILQ_REMOVE(&ipq_list
, fp
, ipq_list
);
1652 currentfrags
-= fp
->ipq_nfrags
;
1654 (void) m_free(dtom(fp
));
1655 m
->m_len
+= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1656 m
->m_data
-= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1657 /* some debugging cruft by sklower, below, will go away soon */
1658 if (m
->m_flags
& M_PKTHDR
) { /* XXX this should be done elsewhere */
1660 for (t
= m
; t
; t
= t
->m_next
)
1662 m
->m_pkthdr
.len
= plen
;
1671 OSAddAtomic(1, &ipstat
.ips_fragdropped
);
1681 * Free a fragment reassembly header and all
1682 * associated datagrams.
1685 ip_freef(struct ipq
*fp
)
1687 lck_mtx_assert(ip_mutex
, LCK_MTX_ASSERT_OWNED
);
1688 currentfrags
-= fp
->ipq_nfrags
;
1689 m_freem_list(fp
->ipq_frags
);
1691 TAILQ_REMOVE(&ipq_list
, fp
, ipq_list
);
1692 (void) m_free(dtom(fp
));
1697 * IP timer processing;
1698 * if a timer expires on a reassembly
1699 * queue, discard it.
1706 lck_mtx_lock(ip_mutex
);
1707 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1711 while (fp
!= &ipq
[i
]) {
1714 if (fp
->prev
->ipq_ttl
== 0) {
1715 OSAddAtomic(fp
->ipq_nfrags
, &ipstat
.ips_fragtimeout
);
1721 * If we are over the maximum number of fragments
1722 * (due to the limit being lowered), drain off
1723 * enough to get down to the new limit.
1725 if (maxnipq
>= 0 && nipq
> maxnipq
) {
1726 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1727 while (nipq
> maxnipq
&&
1728 (ipq
[i
].next
!= &ipq
[i
])) {
1729 OSAddAtomic(ipq
[i
].next
->ipq_nfrags
, &ipstat
.ips_fragdropped
);
1730 ip_freef(ipq
[i
].next
);
1737 lck_mtx_unlock(ip_mutex
);
1741 * Drain off all datagram fragments.
1748 lck_mtx_lock(ip_mutex
);
1749 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1750 while (ipq
[i
].next
!= &ipq
[i
]) {
1751 OSAddAtomic(ipq
[i
].next
->ipq_nfrags
, &ipstat
.ips_fragdropped
);
1752 ip_freef(ipq
[i
].next
);
1755 lck_mtx_unlock(ip_mutex
);
1760 * Do option processing on a datagram,
1761 * possibly discarding it if bad options are encountered,
1762 * or forwarding it if source-routed.
1763 * The pass argument is used when operating in the IPSTEALTH
1764 * mode to tell what options to process:
1765 * [LS]SRR (pass 0) or the others (pass 1).
1766 * The reason for as many as two passes is that when doing IPSTEALTH,
1767 * non-routing options should be processed only if the packet is for us.
1768 * Returns 1 if packet has been forwarded/freed,
1769 * 0 if the packet should be processed further.
1772 ip_dooptions(struct mbuf
*m
, __unused
int pass
, struct sockaddr_in
*next_hop
)
1774 struct ip
*ip
= mtod(m
, struct ip
*);
1776 struct ip_timestamp
*ipt
;
1777 struct in_ifaddr
*ia
;
1778 int opt
, optlen
, cnt
, off
, code
, type
= ICMP_PARAMPROB
, forward
= 0;
1779 struct in_addr
*sin
, dst
;
1781 struct sockaddr_in ipaddr
= {
1782 sizeof (ipaddr
), AF_INET
, 0 , { 0 }, { 0, } };
1785 cp
= (u_char
*)(ip
+ 1);
1786 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1787 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1788 opt
= cp
[IPOPT_OPTVAL
];
1789 if (opt
== IPOPT_EOL
)
1791 if (opt
== IPOPT_NOP
)
1794 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
)) {
1795 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1798 optlen
= cp
[IPOPT_OLEN
];
1799 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
) {
1800 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1810 * Source routing with record.
1811 * Find interface with current destination address.
1812 * If none on this machine then drop if strictly routed,
1813 * or do nothing if loosely routed.
1814 * Record interface address and bring up next address
1815 * component. If strictly routed make sure next
1816 * address is on directly accessible net.
1820 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1821 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1824 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1825 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1828 ipaddr
.sin_addr
= ip
->ip_dst
;
1829 ia
= (struct in_ifaddr
*)
1830 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
1832 if (opt
== IPOPT_SSRR
) {
1833 type
= ICMP_UNREACH
;
1834 code
= ICMP_UNREACH_SRCFAIL
;
1837 if (!ip_dosourceroute
)
1838 goto nosourcerouting
;
1840 * Loose routing, and not at next destination
1841 * yet; nothing to do except forward.
1846 ifafree(&ia
->ia_ifa
);
1849 off
--; /* 0 origin */
1850 if (off
> optlen
- (int)sizeof(struct in_addr
)) {
1852 * End of source route. Should be for us.
1854 if (!ip_acceptsourceroute
)
1855 goto nosourcerouting
;
1856 save_rte(cp
, ip
->ip_src
);
1860 if (!ip_dosourceroute
) {
1862 char buf
[MAX_IPv4_STR_LEN
];
1863 char buf2
[MAX_IPv4_STR_LEN
];
1865 * Acting as a router, so generate ICMP
1869 "attempted source route from %s to %s\n",
1870 inet_ntop(AF_INET
, &ip
->ip_src
, buf
, sizeof(buf
)),
1871 inet_ntop(AF_INET
, &ip
->ip_dst
, buf2
, sizeof(buf2
)));
1872 type
= ICMP_UNREACH
;
1873 code
= ICMP_UNREACH_SRCFAIL
;
1877 * Not acting as a router, so silently drop.
1879 OSAddAtomic(1, &ipstat
.ips_cantforward
);
1886 * locate outgoing interface
1888 (void)memcpy(&ipaddr
.sin_addr
, cp
+ off
,
1889 sizeof(ipaddr
.sin_addr
));
1891 if (opt
== IPOPT_SSRR
) {
1892 #define INA struct in_ifaddr *
1893 #define SA struct sockaddr *
1894 if ((ia
= (INA
)ifa_ifwithdstaddr((SA
)&ipaddr
)) == 0) {
1895 ia
= (INA
)ifa_ifwithnet((SA
)&ipaddr
);
1898 ia
= ip_rtaddr(ipaddr
.sin_addr
);
1901 type
= ICMP_UNREACH
;
1902 code
= ICMP_UNREACH_SRCFAIL
;
1905 ip
->ip_dst
= ipaddr
.sin_addr
;
1906 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1907 sizeof(struct in_addr
));
1908 ifafree(&ia
->ia_ifa
);
1910 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1912 * Let ip_intr's mcast routing check handle mcast pkts
1914 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
1918 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1919 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1922 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1923 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1927 * If no space remains, ignore.
1929 off
--; /* 0 origin */
1930 if (off
> optlen
- (int)sizeof(struct in_addr
))
1932 (void)memcpy(&ipaddr
.sin_addr
, &ip
->ip_dst
,
1933 sizeof(ipaddr
.sin_addr
));
1935 * locate outgoing interface; if we're the destination,
1936 * use the incoming interface (should be same).
1938 if ((ia
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0) {
1939 if ((ia
= ip_rtaddr(ipaddr
.sin_addr
)) == 0) {
1940 type
= ICMP_UNREACH
;
1941 code
= ICMP_UNREACH_HOST
;
1945 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1946 sizeof(struct in_addr
));
1947 ifafree(&ia
->ia_ifa
);
1949 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1953 code
= cp
- (u_char
*)ip
;
1954 ipt
= (struct ip_timestamp
*)cp
;
1955 if (ipt
->ipt_len
< 4 || ipt
->ipt_len
> 40) {
1956 code
= (u_char
*)&ipt
->ipt_len
- (u_char
*)ip
;
1959 if (ipt
->ipt_ptr
< 5) {
1960 code
= (u_char
*)&ipt
->ipt_ptr
- (u_char
*)ip
;
1964 ipt
->ipt_len
- (int)sizeof(int32_t)) {
1965 if (++ipt
->ipt_oflw
== 0) {
1966 code
= (u_char
*)&ipt
->ipt_ptr
-
1972 sin
= (struct in_addr
*)(cp
+ ipt
->ipt_ptr
- 1);
1973 switch (ipt
->ipt_flg
) {
1975 case IPOPT_TS_TSONLY
:
1978 case IPOPT_TS_TSANDADDR
:
1979 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1980 sizeof(struct in_addr
) > ipt
->ipt_len
) {
1981 code
= (u_char
*)&ipt
->ipt_ptr
-
1985 ipaddr
.sin_addr
= dst
;
1986 ia
= (INA
)ifaof_ifpforaddr((SA
)&ipaddr
,
1990 (void)memcpy(sin
, &IA_SIN(ia
)->sin_addr
,
1991 sizeof(struct in_addr
));
1992 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
1993 ifafree(&ia
->ia_ifa
);
1997 case IPOPT_TS_PRESPEC
:
1998 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1999 sizeof(struct in_addr
) > ipt
->ipt_len
) {
2000 code
= (u_char
*)&ipt
->ipt_ptr
-
2004 (void)memcpy(&ipaddr
.sin_addr
, sin
,
2005 sizeof(struct in_addr
));
2006 if ((ia
= (struct in_ifaddr
*)ifa_ifwithaddr((SA
)&ipaddr
)) == 0)
2008 ifafree(&ia
->ia_ifa
);
2010 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
2014 /* XXX can't take &ipt->ipt_flg */
2015 code
= (u_char
*)&ipt
->ipt_ptr
-
2020 (void)memcpy(cp
+ ipt
->ipt_ptr
- 1, &ntime
,
2022 ipt
->ipt_ptr
+= sizeof(n_time
);
2025 if (forward
&& ipforwarding
) {
2026 ip_forward(m
, 1, next_hop
);
2031 ip
->ip_len
-= IP_VHL_HL(ip
->ip_vhl
) << 2; /* XXX icmp_error adds in hdr length */
2032 icmp_error(m
, type
, code
, 0, 0);
2033 OSAddAtomic(1, &ipstat
.ips_badoptions
);
2038 * Given address of next destination (final or next hop),
2039 * return internet address info of interface to be used to get there.
2042 ip_rtaddr(struct in_addr dst
)
2044 struct sockaddr_in
*sin
;
2045 struct ifaddr
*rt_ifa
;
2048 bzero(&ro
, sizeof (ro
));
2049 sin
= (struct sockaddr_in
*)&ro
.ro_dst
;
2050 sin
->sin_family
= AF_INET
;
2051 sin
->sin_len
= sizeof (*sin
);
2052 sin
->sin_addr
= dst
;
2054 rtalloc_ign(&ro
, RTF_PRCLONING
);
2055 if (ro
.ro_rt
== NULL
)
2059 if ((rt_ifa
= ro
.ro_rt
->rt_ifa
) != NULL
)
2061 RT_UNLOCK(ro
.ro_rt
);
2064 return ((struct in_ifaddr
*)rt_ifa
);
2068 * Save incoming source route for use in replies,
2069 * to be picked up later by ip_srcroute if the receiver is interested.
2072 save_rte(u_char
*option
, struct in_addr dst
)
2076 olen
= option
[IPOPT_OLEN
];
2079 printf("save_rte: olen %d\n", olen
);
2081 if (olen
> sizeof(ip_srcrt
) - (1 + sizeof(dst
)))
2083 bcopy(option
, ip_srcrt
.srcopt
, olen
);
2084 ip_nhops
= (olen
- IPOPT_OFFSET
- 1) / sizeof(struct in_addr
);
2089 * Retrieve incoming source route for use in replies,
2090 * in the same form used by setsockopt.
2091 * The first hop is placed before the options, will be removed later.
2096 struct in_addr
*p
, *q
;
2100 return ((struct mbuf
*)0);
2101 m
= m_get(M_DONTWAIT
, MT_HEADER
);
2103 return ((struct mbuf
*)0);
2105 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
2107 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
2108 m
->m_len
= ip_nhops
* sizeof(struct in_addr
) + sizeof(struct in_addr
) +
2112 printf("ip_srcroute: nhops %d mlen %d", ip_nhops
, m
->m_len
);
2116 * First save first hop for return route
2118 p
= &ip_srcrt
.route
[ip_nhops
- 1];
2119 *(mtod(m
, struct in_addr
*)) = *p
--;
2122 printf(" hops %lx", (u_int32_t
)ntohl(mtod(m
, struct in_addr
*)->s_addr
));
2126 * Copy option fields and padding (nop) to mbuf.
2128 ip_srcrt
.nop
= IPOPT_NOP
;
2129 ip_srcrt
.srcopt
[IPOPT_OFFSET
] = IPOPT_MINOFF
;
2130 (void)memcpy(mtod(m
, caddr_t
) + sizeof(struct in_addr
),
2131 &ip_srcrt
.nop
, OPTSIZ
);
2132 q
= (struct in_addr
*)(mtod(m
, caddr_t
) +
2133 sizeof(struct in_addr
) + OPTSIZ
);
2136 * Record return path as an IP source route,
2137 * reversing the path (pointers are now aligned).
2139 while (p
>= ip_srcrt
.route
) {
2142 printf(" %lx", (u_int32_t
)ntohl(q
->s_addr
));
2147 * Last hop goes to final destination.
2152 printf(" %lx\n", (u_int32_t
)ntohl(q
->s_addr
));
2158 * Strip out IP options, at higher
2159 * level protocol in the kernel.
2160 * Second argument is buffer to which options
2161 * will be moved, and return value is their length.
2162 * XXX should be deleted; last arg currently ignored.
2165 ip_stripoptions(struct mbuf
*m
, __unused
struct mbuf
*mopt
)
2168 struct ip
*ip
= mtod(m
, struct ip
*);
2172 olen
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
2173 opts
= (caddr_t
)(ip
+ 1);
2174 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
2175 bcopy(opts
+ olen
, opts
, (unsigned)i
);
2177 if (m
->m_flags
& M_PKTHDR
)
2178 m
->m_pkthdr
.len
-= olen
;
2179 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
2182 u_char inetctlerrmap
[PRC_NCMDS
] = {
2184 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
2185 ENETUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
2186 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
2188 ENOPROTOOPT
, ECONNREFUSED
2192 sysctl_ipforwarding SYSCTL_HANDLER_ARGS
2194 #pragma unused(arg1, arg2)
2195 int i
, was_ipforwarding
= ipforwarding
;
2197 i
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
2198 if (i
!= 0 || req
->newptr
== USER_ADDR_NULL
)
2201 if (was_ipforwarding
&& !ipforwarding
) {
2202 /* clean up IPv4 forwarding cached routes */
2203 ifnet_head_lock_shared();
2204 for (i
= 0; i
<= if_index
; i
++) {
2205 struct ifnet
*ifp
= ifindex2ifnet
[i
];
2207 lck_mtx_lock(ifp
->if_fwd_route_lock
);
2208 if (ifp
->if_fwd_route
.ro_rt
!= NULL
) {
2209 rtfree(ifp
->if_fwd_route
.ro_rt
);
2210 ifp
->if_fwd_route
.ro_rt
= NULL
;
2212 lck_mtx_unlock(ifp
->if_fwd_route_lock
);
2222 * Similar to inp_route_{copyout,copyin} routines except that these copy
2223 * out the cached IPv4 forwarding route from struct ifnet instead of the
2224 * inpcb. See comments for those routines for explanations.
2227 ip_fwd_route_copyout(struct ifnet
*ifp
, struct route
*dst
)
2229 struct route
*src
= &ifp
->if_fwd_route
;
2231 lck_mtx_lock(ifp
->if_fwd_route_lock
);
2233 /* Minor sanity check */
2234 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET
)
2235 panic("%s: wrong or corrupted route: %p", __func__
, src
);
2237 /* Copy everything (rt, dst, flags) from ifnet */
2238 bcopy(src
, dst
, sizeof (*dst
));
2240 /* Hold one reference for the local copy of struct route */
2241 if (dst
->ro_rt
!= NULL
)
2242 RT_ADDREF(dst
->ro_rt
);
2244 lck_mtx_unlock(ifp
->if_fwd_route_lock
);
2248 ip_fwd_route_copyin(struct ifnet
*ifp
, struct route
*src
)
2250 struct route
*dst
= &ifp
->if_fwd_route
;
2252 lck_mtx_lock(ifp
->if_fwd_route_lock
);
2254 /* Minor sanity check */
2255 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET
)
2256 panic("%s: wrong or corrupted route: %p", __func__
, src
);
2258 /* No cached route in the ifnet? */
2259 if (dst
->ro_rt
== NULL
) {
2261 * Copy everything (rt, dst, flags) from ip_forward();
2262 * the reference to the route was held at the time
2263 * it was allocated and is kept intact.
2265 bcopy(src
, dst
, sizeof (*dst
));
2266 } else if (src
->ro_rt
!= NULL
) {
2268 * If the same, update just the ro_flags and ditch the one
2269 * in the local copy. Else ditch the one that is currently
2270 * cached, and cache what we got back from ip_output().
2272 if (dst
->ro_rt
== src
->ro_rt
) {
2273 dst
->ro_flags
= src
->ro_flags
;
2278 bcopy(src
, dst
, sizeof (*dst
));
2282 lck_mtx_unlock(ifp
->if_fwd_route_lock
);
2286 * Forward a packet. If some error occurs return the sender
2287 * an icmp packet. Note we can't always generate a meaningful
2288 * icmp message because icmp doesn't have a large enough repertoire
2289 * of codes and types.
2291 * If not forwarding, just drop the packet. This could be confusing
2292 * if ipforwarding was zero but some routing protocol was advancing
2293 * us as a gateway to somewhere. However, we must let the routing
2294 * protocol deal with that.
2296 * The srcrt parameter indicates whether the packet is being forwarded
2297 * via a source route.
2300 ip_forward(struct mbuf
*m
, int srcrt
, struct sockaddr_in
*next_hop
)
2303 #pragma unused(next_hop)
2305 struct ip
*ip
= mtod(m
, struct ip
*);
2306 struct sockaddr_in
*sin
;
2308 struct route fwd_rt
;
2309 int error
, type
= 0, code
= 0;
2312 struct in_addr pkt_dst
;
2313 u_int32_t nextmtu
= 0;
2314 struct ip_out_args ipoa
= { IFSCOPE_NONE
};
2315 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
2317 struct pf_mtag
*pf_mtag
;
2323 * Cache the destination address of the packet; this may be
2324 * changed by use of 'ipfw fwd'.
2326 pkt_dst
= next_hop
? next_hop
->sin_addr
: ip
->ip_dst
;
2328 pkt_dst
= ip
->ip_dst
;
2333 printf("forward: src %lx dst %lx ttl %x\n",
2334 (u_int32_t
)ip
->ip_src
.s_addr
, (u_int32_t
)pkt_dst
.s_addr
,
2338 if (m
->m_flags
& (M_BCAST
|M_MCAST
) || in_canforward(pkt_dst
) == 0) {
2339 OSAddAtomic(1, &ipstat
.ips_cantforward
);
2346 if (ip
->ip_ttl
<= IPTTLDEC
) {
2347 icmp_error(m
, ICMP_TIMXCEED
, ICMP_TIMXCEED_INTRANS
,
2356 pf_mtag
= pf_find_mtag(m
);
2357 if (pf_mtag
!= NULL
&& pf_mtag
->rtableid
!= IFSCOPE_NONE
)
2358 ipoa
.ipoa_ifscope
= pf_mtag
->rtableid
;
2361 ip_fwd_route_copyout(ifp
, &fwd_rt
);
2363 sin
= (struct sockaddr_in
*)&fwd_rt
.ro_dst
;
2364 if (fwd_rt
.ro_rt
== NULL
||
2365 fwd_rt
.ro_rt
->generation_id
!= route_generation
||
2366 pkt_dst
.s_addr
!= sin
->sin_addr
.s_addr
) {
2367 if (fwd_rt
.ro_rt
!= NULL
) {
2368 rtfree(fwd_rt
.ro_rt
);
2369 fwd_rt
.ro_rt
= NULL
;
2371 sin
->sin_family
= AF_INET
;
2372 sin
->sin_len
= sizeof (*sin
);
2373 sin
->sin_addr
= pkt_dst
;
2375 rtalloc_scoped_ign(&fwd_rt
, RTF_PRCLONING
, ipoa
.ipoa_ifscope
);
2376 if (fwd_rt
.ro_rt
== NULL
) {
2377 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_HOST
, dest
, 0);
2384 * Save the IP header and at most 8 bytes of the payload,
2385 * in case we need to generate an ICMP message to the src.
2387 * We don't use m_copy() because it might return a reference
2388 * to a shared cluster. Both this function and ip_output()
2389 * assume exclusive access to the IP header in `m', so any
2390 * data in a cluster may change before we reach icmp_error().
2392 MGET(mcopy
, M_DONTWAIT
, m
->m_type
);
2393 if (mcopy
!= NULL
) {
2394 M_COPY_PKTHDR(mcopy
, m
);
2395 mcopy
->m_len
= imin((IP_VHL_HL(ip
->ip_vhl
) << 2) + 8,
2397 m_copydata(m
, 0, mcopy
->m_len
, mtod(mcopy
, caddr_t
));
2403 ip
->ip_ttl
-= IPTTLDEC
;
2409 * If forwarding packet using same interface that it came in on,
2410 * perhaps should send a redirect to sender to shortcut a hop.
2411 * Only send redirect if source is sending directly to us,
2412 * and if packet was not source routed (or has any options).
2413 * Also, don't send redirect if forwarding using a default route
2414 * or a route modified by a redirect.
2417 if (rt
->rt_ifp
== m
->m_pkthdr
.rcvif
&&
2418 (rt
->rt_flags
& (RTF_DYNAMIC
|RTF_MODIFIED
)) == 0 &&
2419 satosin(rt_key(rt
))->sin_addr
.s_addr
!= 0 &&
2420 ipsendredirects
&& !srcrt
) {
2421 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
2422 u_int32_t src
= ntohl(ip
->ip_src
.s_addr
);
2425 (src
& RTA(rt
)->ia_subnetmask
) == RTA(rt
)->ia_subnet
) {
2426 if (rt
->rt_flags
& RTF_GATEWAY
)
2427 dest
= satosin(rt
->rt_gateway
)->sin_addr
.s_addr
;
2429 dest
= pkt_dst
.s_addr
;
2430 /* Router requirements says to only send host redirects */
2431 type
= ICMP_REDIRECT
;
2432 code
= ICMP_REDIRECT_HOST
;
2435 printf("redirect (%d) to %lx\n", code
, (u_int32_t
)dest
);
2443 /* Pass IPFORWARD info if available */
2445 struct ip_fwd_tag
*ipfwd_tag
;
2447 tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
,
2448 KERNEL_TAG_TYPE_IPFORWARD
,
2449 sizeof (*ipfwd_tag
), M_NOWAIT
);
2456 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
2457 ipfwd_tag
->next_hop
= next_hop
;
2459 m_tag_prepend(m
, tag
);
2462 error
= ip_output_list(m
, 0, NULL
, &fwd_rt
,
2463 IP_FORWARDING
| IP_OUTARGS
, 0, &ipoa
);
2465 /* Refresh rt since the route could have changed while in IP */
2469 OSAddAtomic(1, &ipstat
.ips_cantforward
);
2471 OSAddAtomic(1, &ipstat
.ips_forward
);
2473 OSAddAtomic(1, &ipstat
.ips_redirectsent
);
2477 ipflow_create(&fwd_rt
, mcopy
);
2480 * If we didn't have to go thru ipflow and
2481 * the packet was successfully consumed by
2482 * ip_output, the mcopy is rather a waste;
2483 * this could be further optimized.
2495 case 0: /* forwarded, but need redirect */
2496 /* type, code set above */
2499 case ENETUNREACH
: /* shouldn't happen, checked above */
2504 type
= ICMP_UNREACH
;
2505 code
= ICMP_UNREACH_HOST
;
2509 type
= ICMP_UNREACH
;
2510 code
= ICMP_UNREACH_NEEDFRAG
;
2514 if (rt
->rt_ifp
!= NULL
)
2515 nextmtu
= rt
->rt_ifp
->if_mtu
;
2520 * If the packet is routed over IPsec tunnel, tell the
2521 * originator the tunnel MTU.
2522 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
2526 struct secpolicy
*sp
= NULL
;
2532 if (rt
->rt_ifp
!= NULL
)
2533 nextmtu
= rt
->rt_ifp
->if_mtu
;
2537 OSAddAtomic(1, &ipstat
.ips_cantfrag
);
2540 sp
= ipsec4_getpolicybyaddr(mcopy
,
2546 /* count IPsec header size */
2547 ipsechdr
= ipsec_hdrsiz(sp
);
2550 * find the correct route for outer IPv4
2551 * header, compute tunnel MTU.
2555 if (sp
->req
!= NULL
) {
2556 if (sp
->req
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2557 struct secasindex saidx
;
2559 struct secasvar
*sav
;
2561 ipm
= mtod(mcopy
, struct ip
*);
2562 bcopy(&sp
->req
->saidx
, &saidx
, sizeof(saidx
));
2563 saidx
.mode
= sp
->req
->saidx
.mode
;
2564 saidx
.reqid
= sp
->req
->saidx
.reqid
;
2565 sin
= (struct sockaddr_in
*)&saidx
.src
;
2566 if (sin
->sin_len
== 0) {
2567 sin
->sin_len
= sizeof(*sin
);
2568 sin
->sin_family
= AF_INET
;
2569 sin
->sin_port
= IPSEC_PORT_ANY
;
2570 bcopy(&ipm
->ip_src
, &sin
->sin_addr
,
2571 sizeof(sin
->sin_addr
));
2573 sin
= (struct sockaddr_in
*)&saidx
.dst
;
2574 if (sin
->sin_len
== 0) {
2575 sin
->sin_len
= sizeof(*sin
);
2576 sin
->sin_family
= AF_INET
;
2577 sin
->sin_port
= IPSEC_PORT_ANY
;
2578 bcopy(&ipm
->ip_dst
, &sin
->sin_addr
,
2579 sizeof(sin
->sin_addr
));
2581 sav
= key_allocsa_policy(&saidx
);
2583 if (sav
->sah
!= NULL
) {
2584 ro
= &sav
->sah
->sa_route
;
2585 if (ro
->ro_rt
!= NULL
) {
2587 if (ro
->ro_rt
->rt_ifp
!= NULL
) {
2588 nextmtu
= ro
->ro_rt
->rt_ifp
->if_mtu
;
2589 nextmtu
-= ipsechdr
;
2591 RT_UNLOCK(ro
->ro_rt
);
2594 key_freesav(sav
, KEY_SADB_UNLOCKED
);
2598 key_freesp(sp
, KEY_SADB_UNLOCKED
);
2602 OSAddAtomic(1, &ipstat
.ips_cantfrag
);
2606 type
= ICMP_SOURCEQUENCH
;
2610 case EACCES
: /* ipfw denied packet */
2615 icmp_error(mcopy
, type
, code
, dest
, nextmtu
);
2617 ip_fwd_route_copyin(ifp
, &fwd_rt
);
2627 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
2631 *mp
= sbcreatecontrol((caddr_t
) &tv
, sizeof(tv
),
2632 SCM_TIMESTAMP
, SOL_SOCKET
);
2634 mp
= &(*mp
)->m_next
;
2636 if (inp
->inp_flags
& INP_RECVDSTADDR
) {
2637 *mp
= sbcreatecontrol((caddr_t
) &ip
->ip_dst
,
2638 sizeof(struct in_addr
), IP_RECVDSTADDR
, IPPROTO_IP
);
2640 mp
= &(*mp
)->m_next
;
2644 * Moving these out of udp_input() made them even more broken
2645 * than they already were.
2647 /* options were tossed already */
2648 if (inp
->inp_flags
& INP_RECVOPTS
) {
2649 *mp
= sbcreatecontrol((caddr_t
) opts_deleted_above
,
2650 sizeof(struct in_addr
), IP_RECVOPTS
, IPPROTO_IP
);
2652 mp
= &(*mp
)->m_next
;
2654 /* ip_srcroute doesn't do what we want here, need to fix */
2655 if (inp
->inp_flags
& INP_RECVRETOPTS
) {
2656 *mp
= sbcreatecontrol((caddr_t
) ip_srcroute(),
2657 sizeof(struct in_addr
), IP_RECVRETOPTS
, IPPROTO_IP
);
2659 mp
= &(*mp
)->m_next
;
2662 if (inp
->inp_flags
& INP_RECVIF
) {
2665 struct sockaddr_dl sdl
;
2668 struct sockaddr_dl
*sdp
;
2669 struct sockaddr_dl
*sdl2
= &sdlbuf
.sdl
;
2671 ifnet_head_lock_shared();
2672 if (((ifp
= m
->m_pkthdr
.rcvif
))
2673 && ( ifp
->if_index
&& (ifp
->if_index
<= if_index
))) {
2674 struct ifaddr
*ifa
= ifnet_addrs
[ifp
->if_index
- 1];
2676 if (!ifa
|| !ifa
->ifa_addr
)
2679 sdp
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
2681 * Change our mind and don't try copy.
2683 if ((sdp
->sdl_family
!= AF_LINK
)
2684 || (sdp
->sdl_len
> sizeof(sdlbuf
))) {
2687 bcopy(sdp
, sdl2
, sdp
->sdl_len
);
2691 = offsetof(struct sockaddr_dl
, sdl_data
[0]);
2692 sdl2
->sdl_family
= AF_LINK
;
2693 sdl2
->sdl_index
= 0;
2694 sdl2
->sdl_nlen
= sdl2
->sdl_alen
= sdl2
->sdl_slen
= 0;
2697 *mp
= sbcreatecontrol((caddr_t
) sdl2
, sdl2
->sdl_len
,
2698 IP_RECVIF
, IPPROTO_IP
);
2700 mp
= &(*mp
)->m_next
;
2702 if (inp
->inp_flags
& INP_RECVTTL
) {
2703 *mp
= sbcreatecontrol((caddr_t
)&ip
->ip_ttl
, sizeof(ip
->ip_ttl
), IP_RECVTTL
, IPPROTO_IP
);
2704 if (*mp
) mp
= &(*mp
)->m_next
;
2709 ip_rsvp_init(struct socket
*so
)
2711 if (so
->so_type
!= SOCK_RAW
||
2712 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2715 if (ip_rsvpd
!= NULL
)
2720 * This may seem silly, but we need to be sure we don't over-increment
2721 * the RSVP counter, in case something slips up.
2736 * This may seem silly, but we need to be sure we don't over-decrement
2737 * the RSVP counter, in case something slips up.