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 <kern/queue.h>
85 #include <kern/locks.h>
87 #include <pexpert/pexpert.h>
90 #include <net/if_var.h>
91 #include <net/if_dl.h>
92 #include <net/route.h>
93 #include <net/kpi_protocol.h>
95 #include <netinet/in.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/in_var.h>
98 #include <netinet/ip.h>
99 #include <netinet/in_pcb.h>
100 #include <netinet/ip_var.h>
101 #include <netinet/ip_icmp.h>
102 #include <sys/socketvar.h>
104 #include <netinet/ip_fw.h>
105 #include <netinet/ip_divert.h>
107 #include <netinet/kpi_ipfilter_var.h>
109 /* needed for AUTOCONFIGURING: */
110 #include <netinet/udp.h>
111 #include <netinet/udp_var.h>
112 #include <netinet/bootp.h>
115 #include <security/mac_framework.h>
118 #include <sys/kdebug.h>
119 #include <libkern/OSAtomic.h>
121 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 0)
122 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 2)
123 #define DBG_FNC_IP_INPUT NETDBG_CODE(DBG_NETIP, (2 << 8))
127 #include <netinet6/ipsec.h>
128 #include <netkey/key.h>
132 #if defined(NFAITH) && NFAITH > 0
133 #include <net/if_types.h>
137 #include <netinet/ip_dummynet.h>
141 extern int ipsec_bypass
;
142 extern lck_mtx_t
*sadb_mutex
;
146 static int ip_rsvp_on
;
147 struct socket
*ip_rsvpd
;
149 int ipforwarding
= 0;
150 SYSCTL_INT(_net_inet_ip
, IPCTL_FORWARDING
, forwarding
, CTLFLAG_RW
,
151 &ipforwarding
, 0, "Enable IP forwarding between interfaces");
153 static int ipsendredirects
= 1; /* XXX */
154 SYSCTL_INT(_net_inet_ip
, IPCTL_SENDREDIRECTS
, redirect
, CTLFLAG_RW
,
155 &ipsendredirects
, 0, "Enable sending IP redirects");
157 int ip_defttl
= IPDEFTTL
;
158 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFTTL
, ttl
, CTLFLAG_RW
,
159 &ip_defttl
, 0, "Maximum TTL on IP packets");
161 static int ip_dosourceroute
= 0;
162 SYSCTL_INT(_net_inet_ip
, IPCTL_SOURCEROUTE
, sourceroute
, CTLFLAG_RW
,
163 &ip_dosourceroute
, 0, "Enable forwarding source routed IP packets");
165 static int ip_acceptsourceroute
= 0;
166 SYSCTL_INT(_net_inet_ip
, IPCTL_ACCEPTSOURCEROUTE
, accept_sourceroute
,
167 CTLFLAG_RW
, &ip_acceptsourceroute
, 0,
168 "Enable accepting source routed IP packets");
170 static int ip_keepfaith
= 0;
171 SYSCTL_INT(_net_inet_ip
, IPCTL_KEEPFAITH
, keepfaith
, CTLFLAG_RW
,
173 "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
175 static int nipq
= 0; /* total # of reass queues */
177 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfragpackets
, CTLFLAG_RW
,
179 "Maximum number of IPv4 fragment reassembly queue entries");
181 static int maxfragsperpacket
;
182 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfragsperpacket
, CTLFLAG_RW
,
183 &maxfragsperpacket
, 0,
184 "Maximum number of IPv4 fragments allowed per packet");
187 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfrags
, CTLFLAG_RW
,
188 &maxfrags
, 0, "Maximum number of IPv4 fragments allowed");
190 static int currentfrags
= 0;
192 #if CONFIG_SCOPEDROUTING
193 int ip_doscopedroute
= 1;
195 int ip_doscopedroute
= 0;
197 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, scopedroute
, CTLFLAG_RW
,
198 &ip_doscopedroute
, 0, "Enable IPv4 scoped routing");
201 * XXX - Setting ip_checkinterface mostly implements the receive side of
202 * the Strong ES model described in RFC 1122, but since the routing table
203 * and transmit implementation do not implement the Strong ES model,
204 * setting this to 1 results in an odd hybrid.
206 * XXX - ip_checkinterface currently must be disabled if you use ipnat
207 * to translate the destination address to another local interface.
209 * XXX - ip_checkinterface must be disabled if you add IP aliases
210 * to the loopback interface instead of the interface where the
211 * packets for those addresses are received.
213 static int ip_checkinterface
= 0;
214 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, check_interface
, CTLFLAG_RW
,
215 &ip_checkinterface
, 0, "Verify packet arrives on correct interface");
219 static int ipprintfs
= 0;
222 extern int in_proto_count
;
223 extern struct domain inetdomain
;
224 extern struct protosw inetsw
[];
225 struct protosw
*ip_protox
[IPPROTO_MAX
];
226 static int ipqmaxlen
= IFQ_MAXLEN
;
227 struct in_ifaddrhead in_ifaddrhead
; /* first inet address */
228 struct ifqueue ipintrq
;
229 SYSCTL_INT(_net_inet_ip
, IPCTL_INTRQMAXLEN
, intr_queue_maxlen
, CTLFLAG_RW
,
230 &ipintrq
.ifq_maxlen
, 0, "Maximum size of the IP input queue");
231 SYSCTL_INT(_net_inet_ip
, IPCTL_INTRQDROPS
, intr_queue_drops
, CTLFLAG_RD
,
232 &ipintrq
.ifq_drops
, 0, "Number of packets dropped from the IP input queue");
234 struct ipstat ipstat
;
235 SYSCTL_STRUCT(_net_inet_ip
, IPCTL_STATS
, stats
, CTLFLAG_RD
,
236 &ipstat
, ipstat
, "IP statistics (struct ipstat, netinet/ip_var.h)");
238 /* Packet reassembly stuff */
239 #define IPREASS_NHASH_LOG2 6
240 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
241 #define IPREASS_HMASK (IPREASS_NHASH - 1)
242 #define IPREASS_HASH(x,y) \
243 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
245 static struct ipq ipq
[IPREASS_NHASH
];
246 static TAILQ_HEAD(ipq_list
, ipq
) ipq_list
=
247 TAILQ_HEAD_INITIALIZER(ipq_list
);
248 const int ipintrq_present
= 1;
250 lck_attr_t
*ip_mutex_attr
;
251 lck_grp_t
*ip_mutex_grp
;
252 lck_grp_attr_t
*ip_mutex_grp_attr
;
253 lck_mtx_t
*inet_domain_mutex
;
254 extern lck_mtx_t
*domain_proto_mtx
;
257 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFMTU
, mtu
, CTLFLAG_RW
,
258 &ip_mtu
, 0, "Default MTU");
262 static int ipstealth
= 0;
263 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, stealth
, CTLFLAG_RW
,
270 ip_fw_chk_t
*ip_fw_chk_ptr
;
276 ip_dn_io_t
*ip_dn_io_ptr
;
279 int (*fr_checkp
)(struct ip
*, int, struct ifnet
*, int, struct mbuf
**) = NULL
;
280 #endif /* IPFIREWALL */
282 SYSCTL_NODE(_net_inet_ip
, OID_AUTO
, linklocal
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "link local");
284 struct ip_linklocal_stat ip_linklocal_stat
;
285 SYSCTL_STRUCT(_net_inet_ip_linklocal
, OID_AUTO
, stat
, CTLFLAG_RD
,
286 &ip_linklocal_stat
, ip_linklocal_stat
,
287 "Number of link local packets with TTL less than 255");
289 SYSCTL_NODE(_net_inet_ip_linklocal
, OID_AUTO
, in
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "link local input");
291 int ip_linklocal_in_allowbadttl
= 1;
292 SYSCTL_INT(_net_inet_ip_linklocal_in
, OID_AUTO
, allowbadttl
, CTLFLAG_RW
,
293 &ip_linklocal_in_allowbadttl
, 0,
294 "Allow incoming link local packets with TTL less than 255");
298 * We need to save the IP options in case a protocol wants to respond
299 * to an incoming packet over the same route if the packet got here
300 * using IP source routing. This allows connection establishment and
301 * maintenance when the remote end is on a network that is not known
304 static int ip_nhops
= 0;
305 static struct ip_srcrt
{
306 struct in_addr dst
; /* final destination */
307 char nop
; /* one NOP to align */
308 char srcopt
[IPOPT_OFFSET
+ 1]; /* OPTVAL, OLEN and OFFSET */
309 struct in_addr route
[MAX_IPOPTLEN
/sizeof(struct in_addr
)];
313 static void save_rte(u_char
*, struct in_addr
);
314 static int ip_dooptions(struct mbuf
*, int, struct sockaddr_in
*, struct route
*ipforward_rt
);
315 static void ip_forward(struct mbuf
*, int, struct sockaddr_in
*, struct route
*ipforward_rt
);
316 static void ip_freef(struct ipq
*);
319 static struct mbuf
*ip_reass(struct mbuf
*,
320 struct ipq
*, struct ipq
*, u_int32_t
*, u_int16_t
*);
322 static struct mbuf
*ip_reass(struct mbuf
*,
323 struct ipq
*, struct ipq
*, u_int16_t
*, u_int16_t
*);
326 static struct mbuf
*ip_reass(struct mbuf
*, struct ipq
*, struct ipq
*);
332 extern u_short ip_id
;
334 int ip_use_randomid
= 1;
335 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, random_id
, CTLFLAG_RW
,
336 &ip_use_randomid
, 0, "Randomize IP packets IDs");
339 extern u_long route_generation
;
342 * IP initialization: fill in IP protocol switch table.
343 * All protocols not implemented in kernel go to raw IP protocol handler.
350 static int ip_initialized
= 0;
355 TAILQ_INIT(&in_ifaddrhead
);
356 pr
= pffindproto_locked(PF_INET
, IPPROTO_RAW
, SOCK_RAW
);
359 for (i
= 0; i
< IPPROTO_MAX
; i
++)
361 for (pr
= inetdomain
.dom_protosw
; pr
; pr
= pr
->pr_next
)
362 { if(!((unsigned int)pr
->pr_domain
)) continue; /* If uninitialized, skip */
363 if (pr
->pr_domain
->dom_family
== PF_INET
&&
364 pr
->pr_protocol
&& pr
->pr_protocol
!= IPPROTO_RAW
)
365 ip_protox
[pr
->pr_protocol
] = pr
;
367 for (i
= 0; i
< IPREASS_NHASH
; i
++)
368 ipq
[i
].next
= ipq
[i
].prev
= &ipq
[i
];
370 maxnipq
= nmbclusters
/ 32;
371 maxfrags
= maxnipq
* 2;
372 maxfragsperpacket
= 128; /* enough for 64k in 512 byte fragments */
376 struct timeval timenow
;
377 getmicrotime(&timenow
);
378 ip_id
= timenow
.tv_sec
& 0xffff;
381 ipintrq
.ifq_maxlen
= ipqmaxlen
;
385 ip_mutex_grp_attr
= lck_grp_attr_alloc_init();
387 ip_mutex_grp
= lck_grp_alloc_init("ip", ip_mutex_grp_attr
);
389 ip_mutex_attr
= lck_attr_alloc_init();
391 if ((ip_mutex
= lck_mtx_alloc_init(ip_mutex_grp
, ip_mutex_attr
)) == NULL
) {
392 printf("ip_init: can't alloc ip_mutex\n");
398 sadb_stat_mutex_grp_attr
= lck_grp_attr_alloc_init();
399 sadb_stat_mutex_grp
= lck_grp_alloc_init("sadb_stat", sadb_stat_mutex_grp_attr
);
400 sadb_stat_mutex_attr
= lck_attr_alloc_init();
402 if ((sadb_stat_mutex
= lck_mtx_alloc_init(sadb_stat_mutex_grp
, sadb_stat_mutex_attr
)) == NULL
) {
403 printf("ip_init: can't alloc sadb_stat_mutex\n");
414 protocol_family_t __unused protocol
,
420 /* ip_input should handle a list of packets but does not yet */
422 for (packet
= packet_list
; packet
; packet
= packet_list
) {
424 packet_list
= mbuf_nextpkt(packet
);
425 mbuf_setnextpkt(packet
, NULL
);
430 /* Initialize the PF_INET domain, and add in the pre-defined protos */
437 static int inetdomain_initted
= 0;
439 if (!inetdomain_initted
)
442 kprintf("Initing %d protosw entries\n", in_proto_count
);
445 dp
->dom_flags
= DOM_REENTRANT
;
447 for (i
=0, pr
= &inetsw
[0]; i
<in_proto_count
; i
++, pr
++)
448 net_add_proto(pr
, dp
);
449 inet_domain_mutex
= dp
->dom_mtx
;
450 inetdomain_initted
= 1;
452 lck_mtx_unlock(domain_proto_mtx
);
453 proto_register_input(PF_INET
, ip_proto_input
, NULL
, 1);
454 lck_mtx_lock(domain_proto_mtx
);
458 __private_extern__
void
459 ip_proto_dispatch_in(
463 ipfilter_t inject_ipfref
)
465 struct ipfilter
*filter
;
466 int seen
= (inject_ipfref
== 0);
467 int changed_header
= 0;
470 if (!TAILQ_EMPTY(&ipv4_filters
)) {
472 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
474 if ((struct ipfilter
*)inject_ipfref
== filter
)
476 } else if (filter
->ipf_filter
.ipf_input
) {
479 if (changed_header
== 0) {
481 ip
= mtod(m
, struct ip
*);
482 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
483 ip
->ip_off
= htons(ip
->ip_off
);
485 ip
->ip_sum
= in_cksum(m
, hlen
);
487 result
= filter
->ipf_filter
.ipf_input(
488 filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, hlen
, proto
);
489 if (result
== EJUSTRETURN
) {
503 * If there isn't a specific lock for the protocol
504 * we're about to call, use the generic lock for AF_INET.
505 * otherwise let the protocol deal with its own locking
507 ip
= mtod(m
, struct ip
*);
509 if (changed_header
) {
510 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
511 ip
->ip_off
= ntohs(ip
->ip_off
);
514 if (!(ip_protox
[ip
->ip_p
]->pr_flags
& PR_PROTOLOCK
)) {
515 lck_mtx_lock(inet_domain_mutex
);
516 (*ip_protox
[ip
->ip_p
]->pr_input
)(m
, hlen
);
517 lck_mtx_unlock(inet_domain_mutex
);
520 (*ip_protox
[ip
->ip_p
]->pr_input
)(m
, hlen
);
525 * ipforward_rt cleared in in_addroute()
526 * when a new route is successfully created.
528 static struct sockaddr_in ipaddr
= { sizeof(ipaddr
), AF_INET
, 0 , {0}, {0,0,0,0,0,0,0,0} };
531 * Ip input routine. Checksum and byte swap header. If fragmented
532 * try to reassemble. Process options. Pass to next level.
535 ip_input(struct mbuf
*m
)
539 struct in_ifaddr
*ia
= NULL
;
540 int i
, hlen
, checkif
;
542 struct in_addr pkt_dst
;
543 u_int32_t div_info
= 0; /* packet divert/tee info */
545 struct ip_fw_args args
;
547 ipfilter_t inject_filter_ref
= 0;
549 struct route ipforward_rt
;
551 bzero(&ipforward_rt
, sizeof(struct route
));
557 args
.divert_rule
= 0; /* divert cookie */
558 args
.next_hop
= NULL
;
560 /* Grab info from mtags prepended to the chain */
562 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
563 struct dn_pkt_tag
*dn_tag
;
565 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
566 args
.rule
= dn_tag
->rule
;
568 m_tag_delete(m
, tag
);
570 #endif /* DUMMYNET */
573 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_DIVERT
, NULL
)) != NULL
) {
574 struct divert_tag
*div_tag
;
576 div_tag
= (struct divert_tag
*)(tag
+1);
577 args
.divert_rule
= div_tag
->cookie
;
579 m_tag_delete(m
, tag
);
583 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
, NULL
)) != NULL
) {
584 struct ip_fwd_tag
*ipfwd_tag
;
586 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
587 args
.next_hop
= ipfwd_tag
->next_hop
;
589 m_tag_delete(m
, tag
);
593 if (m
== NULL
|| (m
->m_flags
& M_PKTHDR
) == 0)
594 panic("ip_input no HDR");
598 if (args
.rule
) { /* dummynet already filtered us */
599 ip
= mtod(m
, struct ip
*);
600 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
601 inject_filter_ref
= ipf_get_inject_filter(m
);
604 #endif /* DUMMYNET */
605 #endif /* IPFIREWALL */
608 * No need to proccess packet twice if we've
611 inject_filter_ref
= ipf_get_inject_filter(m
);
612 if (inject_filter_ref
!= 0) {
613 ip
= mtod(m
, struct ip
*);
614 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
615 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
616 ip
->ip_off
= ntohs(ip
->ip_off
);
617 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, inject_filter_ref
);
621 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_total
);
623 if (m
->m_pkthdr
.len
< sizeof(struct ip
))
626 if (m
->m_len
< sizeof (struct ip
) &&
627 (m
= m_pullup(m
, sizeof (struct ip
))) == 0) {
628 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_toosmall
);
631 ip
= mtod(m
, struct ip
*);
633 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
634 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
636 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
637 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_badvers
);
641 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
642 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
643 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_badhlen
);
646 if (hlen
> m
->m_len
) {
647 if ((m
= m_pullup(m
, hlen
)) == 0) {
648 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_badhlen
);
651 ip
= mtod(m
, struct ip
*);
654 /* 127/8 must not appear on wire - RFC1122 */
655 if ((ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
656 (ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
) {
657 if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) {
658 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_badaddr
);
663 /* IPv4 Link-Local Addresses as defined in <draft-ietf-zeroconf-ipv4-linklocal-05.txt> */
664 if ((IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
)) ||
665 IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)))) {
666 ip_linklocal_stat
.iplls_in_total
++;
667 if (ip
->ip_ttl
!= MAXTTL
) {
668 OSAddAtomic(1, (SInt32
*)&ip_linklocal_stat
.iplls_in_badttl
);
669 /* Silently drop link local traffic with bad TTL */
670 if (!ip_linklocal_in_allowbadttl
)
674 if ((IF_HWASSIST_CSUM_FLAGS(m
->m_pkthdr
.rcvif
->if_hwassist
) == 0)
675 || (apple_hwcksum_rx
== 0) ||
676 ((m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
) && ip
->ip_p
!= IPPROTO_TCP
)) {
677 m
->m_pkthdr
.csum_flags
= 0; /* invalidate HW generated checksum flags */
680 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
681 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
682 } else if (!(m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) ||
683 apple_hwcksum_tx
== 0) {
685 * Either this is not loopback packet coming from an interface
686 * that does not support checksum offloading, or it is loopback
687 * packet that has undergone software checksumming at the send
688 * side because apple_hwcksum_tx was set to 0. In this case,
689 * calculate the checksum in software to validate the packet.
691 sum
= in_cksum(m
, hlen
);
694 * This is a loopback packet without any valid checksum since
695 * the send side has bypassed it (apple_hwcksum_tx set to 1).
696 * We get here because apple_hwcksum_rx was set to 0, and so
697 * we pretend that all is well.
700 m
->m_pkthdr
.csum_flags
|= CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
701 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
702 m
->m_pkthdr
.csum_data
= 0xffff;
705 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_badsum
);
710 * Convert fields to host representation.
713 if (ip
->ip_len
< hlen
) {
714 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_badlen
);
720 * Check that the amount of data in the buffers
721 * is as at least much as the IP header would have us expect.
722 * Trim mbufs if longer than we expect.
723 * Drop packet if shorter than we expect.
725 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
727 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_tooshort
);
730 if (m
->m_pkthdr
.len
> ip
->ip_len
) {
731 /* Invalidate hwcksuming */
732 m
->m_pkthdr
.csum_flags
= 0;
733 m
->m_pkthdr
.csum_data
= 0;
735 if (m
->m_len
== m
->m_pkthdr
.len
) {
736 m
->m_len
= ip
->ip_len
;
737 m
->m_pkthdr
.len
= ip
->ip_len
;
739 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
743 if (ipsec_bypass
== 0 && ipsec_gethist(m
, NULL
))
749 * Right now when no processing on packet has done
750 * and it is still fresh out of network we do our black
752 * - Firewall: deny/allow/divert
753 * - Xlate: translate packet's addr/port (NAT).
754 * - Pipe: pass pkt through dummynet.
755 * - Wrap: fake packet's addr/port <unimpl.>
756 * - Encapsulate: put it in another IP and send out. <unimp.>
762 #endif /* DUMMYNET */
764 * Check if we want to allow this packet to be processed.
765 * Consider it to be bad if not.
770 if (fr_checkp(ip
, hlen
, m
->m_pkthdr
.rcvif
, 0, &m1
) || !m1
) {
773 ip
= mtod(m
= m1
, struct ip
*);
775 if (fw_enable
&& IPFW_LOADED
) {
776 #if IPFIREWALL_FORWARD
778 * If we've been forwarded from the output side, then
779 * skip the firewall a second time
783 #endif /* IPFIREWALL_FORWARD */
787 i
= ip_fw_chk_ptr(&args
);
790 if ( (i
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) { /* drop */
795 ip
= mtod(m
, struct ip
*); /* just in case m changed */
797 if (i
== 0 && args
.next_hop
== NULL
) { /* common case */
801 if (DUMMYNET_LOADED
&& (i
& IP_FW_PORT_DYNT_FLAG
) != 0) {
802 /* Send packet to the appropriate pipe */
803 ip_dn_io_ptr(m
, i
&0xffff, DN_TO_IP_IN
, &args
);
806 #endif /* DUMMYNET */
808 if (i
!= 0 && (i
& IP_FW_PORT_DYNT_FLAG
) == 0) {
809 /* Divert or tee packet */
814 #if IPFIREWALL_FORWARD
815 if (i
== 0 && args
.next_hop
!= NULL
) {
820 * if we get here, the packet must be dropped
825 #endif /* IPFIREWALL */
829 * Process options and, if not destined for us,
830 * ship it on. ip_dooptions returns 1 when an
831 * error was detected (causing an icmp message
832 * to be sent and the original packet to be freed).
834 ip_nhops
= 0; /* for source routed packets */
836 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, args
.next_hop
, &ipforward_rt
)) {
838 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, NULL
, &ipforward_rt
)) {
843 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
844 * matter if it is destined to another node, or whether it is
845 * a multicast one, RSVP wants it! and prevents it from being forwarded
846 * anywhere else. Also checks if the rsvp daemon is running before
847 * grabbing the packet.
849 if (rsvp_on
&& ip
->ip_p
==IPPROTO_RSVP
)
853 * Check our list of addresses, to see if the packet is for us.
854 * If we don't have any addresses, assume any unicast packet
855 * we receive might be for us (and let the upper layers deal
858 if (TAILQ_EMPTY(&in_ifaddrhead
) &&
859 (m
->m_flags
& (M_MCAST
|M_BCAST
)) == 0)
863 * Cache the destination address of the packet; this may be
864 * changed by use of 'ipfw fwd'.
867 pkt_dst
= args
.next_hop
== NULL
?
868 ip
->ip_dst
: args
.next_hop
->sin_addr
;
870 pkt_dst
= ip
->ip_dst
;
874 * Enable a consistency check between the destination address
875 * and the arrival interface for a unicast packet (the RFC 1122
876 * strong ES model) if IP forwarding is disabled and the packet
877 * is not locally generated and the packet is not subject to
880 * XXX - Checking also should be disabled if the destination
881 * address is ipnat'ed to a different interface.
883 * XXX - Checking is incompatible with IP aliases added
884 * to the loopback interface instead of the interface where
885 * the packets are received.
887 checkif
= ip_checkinterface
&& (ipforwarding
== 0) &&
888 ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0)
890 && (args
.next_hop
== NULL
);
895 lck_mtx_lock(rt_mtx
);
896 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
) {
897 #define satosin(sa) ((struct sockaddr_in *)(sa))
899 if (IA_SIN(ia
)->sin_addr
.s_addr
== INADDR_ANY
) {
900 lck_mtx_unlock(rt_mtx
);
905 * If the address matches, verify that the packet
906 * arrived via the correct interface if checking is
909 if (IA_SIN(ia
)->sin_addr
.s_addr
== pkt_dst
.s_addr
&&
910 (!checkif
|| ia
->ia_ifp
== m
->m_pkthdr
.rcvif
)) {
911 lck_mtx_unlock(rt_mtx
);
915 * Only accept broadcast packets that arrive via the
916 * matching interface. Reception of forwarded directed
917 * broadcasts would be handled via ip_forward() and
918 * ether_output() with the loopback into the stack for
919 * SIMPLEX interfaces handled by ether_output().
921 if ((!checkif
|| ia
->ia_ifp
== m
->m_pkthdr
.rcvif
) &&
922 ia
->ia_ifp
&& ia
->ia_ifp
->if_flags
& IFF_BROADCAST
) {
923 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
925 lck_mtx_unlock(rt_mtx
);
928 if (ia
->ia_netbroadcast
.s_addr
== pkt_dst
.s_addr
) {
929 lck_mtx_unlock(rt_mtx
);
934 lck_mtx_unlock(rt_mtx
);
935 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
936 struct in_multi
*inm
;
940 * If we are acting as a multicast router, all
941 * incoming multicast packets are passed to the
942 * kernel-level multicast forwarding function.
943 * The packet is returned (relatively) intact; if
944 * ip_mforward() returns a non-zero value, the packet
945 * must be discarded, else it may be accepted below.
947 lck_mtx_lock(ip_mutex
);
949 ip_mforward(ip
, m
->m_pkthdr
.rcvif
, m
, 0) != 0) {
950 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_cantforward
);
952 lck_mtx_unlock(ip_mutex
);
957 * The process-level routing daemon needs to receive
958 * all multicast IGMP packets, whether or not this
959 * host belongs to their destination groups.
961 if (ip
->ip_p
== IPPROTO_IGMP
)
963 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_forward
);
965 #endif /* MROUTING */
967 * See if we belong to the destination multicast group on the
970 IN_LOOKUP_MULTI(ip
->ip_dst
, m
->m_pkthdr
.rcvif
, inm
);
972 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_notmember
);
978 if (ip
->ip_dst
.s_addr
== (u_long
)INADDR_BROADCAST
)
980 if (ip
->ip_dst
.s_addr
== INADDR_ANY
)
983 /* Allow DHCP/BootP responses through */
984 if (m
->m_pkthdr
.rcvif
!= NULL
985 && (m
->m_pkthdr
.rcvif
->if_eflags
& IFEF_AUTOCONFIGURING
)
986 && hlen
== sizeof(struct ip
)
987 && ip
->ip_p
== IPPROTO_UDP
) {
989 if (m
->m_len
< sizeof(struct udpiphdr
)
990 && (m
= m_pullup(m
, sizeof(struct udpiphdr
))) == 0) {
991 OSAddAtomic(1, (SInt32
*)&udpstat
.udps_hdrops
);
994 ui
= mtod(m
, struct udpiphdr
*);
995 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) {
998 ip
= mtod(m
, struct ip
*); /* in case it changed */
1001 #if defined(NFAITH) && 0 < NFAITH
1003 * FAITH(Firewall Aided Internet Translator)
1005 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
== IFT_FAITH
) {
1007 if (ip
->ip_p
== IPPROTO_TCP
|| ip
->ip_p
== IPPROTO_ICMP
)
1015 * Not for us; forward if possible and desirable.
1017 if (ipforwarding
== 0) {
1018 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_cantforward
);
1022 ip_forward(m
, 0, args
.next_hop
, &ipforward_rt
);
1024 ip_forward(m
, 0, NULL
, &ipforward_rt
);
1026 if (ipforward_rt
.ro_rt
!= NULL
) {
1027 rtfree(ipforward_rt
.ro_rt
);
1028 ipforward_rt
.ro_rt
= NULL
;
1035 * If offset or IP_MF are set, must reassemble.
1036 * Otherwise, nothing need be done.
1037 * (We could look in the reassembly queue to see
1038 * if the packet was previously fragmented,
1039 * but it's not worth the time; just let them time out.)
1041 if (ip
->ip_off
& (IP_MF
| IP_OFFMASK
| IP_RF
)) {
1043 /* If maxnipq is 0, never accept fragments. */
1046 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_fragments
);
1047 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_fragdropped
);
1052 * If we will exceed the number of fragments in queues, timeout the
1053 * oldest fragemented packet to make space.
1055 lck_mtx_lock(ip_mutex
);
1056 if (currentfrags
>= maxfrags
) {
1057 fp
= TAILQ_LAST(&ipq_list
, ipq_list
);
1058 OSAddAtomic(fp
->ipq_nfrags
, (SInt32
*)&ipstat
.ips_fragtimeout
);
1060 if (ip
->ip_id
== fp
->ipq_id
&&
1061 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
1062 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
1063 ip
->ip_p
== fp
->ipq_p
) {
1065 * If we match the fragment queue we were going to
1066 * discard, drop this packet too.
1068 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_fragdropped
);
1070 lck_mtx_unlock(ip_mutex
);
1077 sum
= IPREASS_HASH(ip
->ip_src
.s_addr
, ip
->ip_id
);
1079 * Look for queue of fragments
1082 for (fp
= ipq
[sum
].next
; fp
!= &ipq
[sum
]; fp
= fp
->next
)
1083 if (ip
->ip_id
== fp
->ipq_id
&&
1084 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
1085 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
1087 mac_ipq_label_compare(m
, fp
) &&
1089 ip
->ip_p
== fp
->ipq_p
)
1093 * Enforce upper bound on number of fragmented packets
1094 * for which we attempt reassembly;
1095 * If maxnipq is -1, accept all fragments without limitation.
1097 if ((nipq
> maxnipq
) && (maxnipq
> 0)) {
1099 * drop the oldest fragment before proceeding further
1101 fp
= TAILQ_LAST(&ipq_list
, ipq_list
);
1102 OSAddAtomic(fp
->ipq_nfrags
, (SInt32
*)&ipstat
.ips_fragtimeout
);
1110 * Adjust ip_len to not reflect header,
1111 * convert offset of this to bytes.
1114 if (ip
->ip_off
& IP_MF
) {
1116 * Make sure that fragments have a data length
1117 * that's a non-zero multiple of 8 bytes.
1119 if (ip
->ip_len
== 0 || (ip
->ip_len
& 0x7) != 0) {
1120 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_toosmall
);
1121 lck_mtx_unlock(ip_mutex
);
1124 m
->m_flags
|= M_FRAG
;
1126 /* Clear the flag in case packet comes from loopback */
1127 m
->m_flags
&= ~M_FRAG
;
1132 * Attempt reassembly; if it succeeds, proceed.
1133 * ip_reass() will return a different mbuf, and update
1134 * the divert info in div_info and args.divert_rule.
1136 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_fragments
);
1137 m
->m_pkthdr
.header
= ip
;
1140 fp
, &ipq
[sum
], &div_info
, &args
.divert_rule
);
1142 m
= ip_reass(m
, fp
, &ipq
[sum
]);
1145 lck_mtx_unlock(ip_mutex
);
1148 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_reassembled
);
1149 ip
= mtod(m
, struct ip
*);
1150 /* Get the header length of the reassembled packet */
1151 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1153 /* Restore original checksum before diverting packet */
1154 if (div_info
!= 0) {
1159 ip
->ip_sum
= in_cksum(m
, hlen
);
1165 lck_mtx_unlock(ip_mutex
);
1171 * Divert or tee packet to the divert protocol if required.
1173 * If div_info is zero then cookie should be too, so we shouldn't
1174 * need to clear them here. Assume divert_packet() does so also.
1176 if (div_info
!= 0) {
1177 struct mbuf
*clone
= NULL
;
1179 /* Clone packet if we're doing a 'tee' */
1180 if ((div_info
& IP_FW_PORT_TEE_FLAG
) != 0)
1181 clone
= m_dup(m
, M_DONTWAIT
);
1183 /* Restore packet header fields to original values */
1188 /* Deliver packet to divert input routine */
1189 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_delivered
);
1190 divert_packet(m
, 1, div_info
& 0xffff, args
.divert_rule
);
1192 /* If 'tee', continue with original packet */
1193 if (clone
== NULL
) {
1197 ip
= mtod(m
, struct ip
*);
1203 * enforce IPsec policy checking if we are seeing last header.
1204 * note that we do not visit this with protocols with pcb layer
1205 * code - like udp/tcp/raw ip.
1207 if (ipsec_bypass
== 0 && (ip_protox
[ip
->ip_p
]->pr_flags
& PR_LASTHDR
) != 0) {
1208 if (ipsec4_in_reject(m
, NULL
)) {
1209 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
1216 * Switch out to protocol's input routine.
1218 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_delivered
);
1221 if (args
.next_hop
&& ip
->ip_p
== IPPROTO_TCP
) {
1222 /* TCP needs IPFORWARD info if available */
1223 struct m_tag
*fwd_tag
;
1224 struct ip_fwd_tag
*ipfwd_tag
;
1226 fwd_tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
,
1227 sizeof(struct sockaddr_in
), M_NOWAIT
);
1228 if (fwd_tag
== NULL
) {
1232 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
1233 ipfwd_tag
->next_hop
= args
.next_hop
;
1235 m_tag_prepend(m
, fwd_tag
);
1237 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1238 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1241 /* TCP deals with its own locking */
1242 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1244 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1245 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1247 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1250 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1256 KERNEL_DEBUG(DBG_LAYER_END
, 0,0,0,0,0);
1261 * Take incoming datagram fragment and try to reassemble it into
1262 * whole datagram. If a chain for reassembly of this datagram already
1263 * exists, then it is given as fp; otherwise have to make a chain.
1265 * When IPDIVERT enabled, keep additional state with each packet that
1266 * tells us if we need to divert or tee the packet we're building.
1269 static struct mbuf
*
1271 ip_reass(struct mbuf
*m
, struct ipq
*fp
, struct ipq
*where
,
1274 #else /* IPDIVERT_44 */
1276 #endif /* IPDIVERT_44 */
1277 u_int16_t
*divcookie
)
1278 #else /* IPDIVERT */
1279 ip_reass(struct mbuf
*m
, struct ipq
*fp
, struct ipq
*where
)
1280 #endif /* IPDIVERT */
1282 struct ip
*ip
= mtod(m
, struct ip
*);
1283 struct mbuf
*p
= 0, *q
, *nq
;
1285 int hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1289 lck_mtx_assert(ip_mutex
, LCK_MTX_ASSERT_OWNED
);
1291 * Presence of header sizes in mbufs
1292 * would confuse code below.
1297 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1298 m
->m_pkthdr
.csum_flags
= 0;
1300 * If first fragment to arrive, create a reassembly queue.
1303 if ((t
= m_get(M_DONTWAIT
, MT_FTABLE
)) == NULL
)
1305 fp
= mtod(t
, struct ipq
*);
1307 if (mac_ipq_label_init(fp
, M_NOWAIT
) != 0) {
1312 mac_ipq_label_associate(m
, fp
);
1314 insque((void*)fp
, (void*)where
);
1317 fp
->ipq_ttl
= IPFRAGTTL
;
1318 fp
->ipq_p
= ip
->ip_p
;
1319 fp
->ipq_id
= ip
->ip_id
;
1320 fp
->ipq_src
= ip
->ip_src
;
1321 fp
->ipq_dst
= ip
->ip_dst
;
1323 m
->m_nextpkt
= NULL
;
1326 fp
->ipq_div_info
= 0;
1330 fp
->ipq_div_cookie
= 0;
1332 TAILQ_INSERT_HEAD(&ipq_list
, fp
, ipq_list
);
1337 mac_ipq_label_update(m
, fp
);
1341 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
1344 * Handle ECN by comparing this segment with the first one;
1345 * if CE is set, do not lose CE.
1346 * drop if CE and not-ECT are mixed for the same packet.
1348 ecn
= ip
->ip_tos
& IPTOS_ECN_MASK
;
1349 ecn0
= GETIP(fp
->ipq_frags
)->ip_tos
& IPTOS_ECN_MASK
;
1350 if (ecn
== IPTOS_ECN_CE
) {
1351 if (ecn0
== IPTOS_ECN_NOTECT
)
1353 if (ecn0
!= IPTOS_ECN_CE
)
1354 GETIP(fp
->ipq_frags
)->ip_tos
|= IPTOS_ECN_CE
;
1356 if (ecn
== IPTOS_ECN_NOTECT
&& ecn0
!= IPTOS_ECN_NOTECT
)
1360 * Find a segment which begins after this one does.
1362 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
)
1363 if (GETIP(q
)->ip_off
> ip
->ip_off
)
1367 * If there is a preceding segment, it may provide some of
1368 * our data already. If so, drop the data from the incoming
1369 * segment. If it provides all of our data, drop us, otherwise
1370 * stick new segment in the proper place.
1372 * If some of the data is dropped from the the preceding
1373 * segment, then it's checksum is invalidated.
1376 i
= GETIP(p
)->ip_off
+ GETIP(p
)->ip_len
- ip
->ip_off
;
1378 if (i
>= ip
->ip_len
)
1381 m
->m_pkthdr
.csum_flags
= 0;
1385 m
->m_nextpkt
= p
->m_nextpkt
;
1388 m
->m_nextpkt
= fp
->ipq_frags
;
1393 * While we overlap succeeding segments trim them or,
1394 * if they are completely covered, dequeue them.
1396 for (; q
!= NULL
&& ip
->ip_off
+ ip
->ip_len
> GETIP(q
)->ip_off
;
1398 i
= (ip
->ip_off
+ ip
->ip_len
) -
1400 if (i
< GETIP(q
)->ip_len
) {
1401 GETIP(q
)->ip_len
-= i
;
1402 GETIP(q
)->ip_off
+= i
;
1404 q
->m_pkthdr
.csum_flags
= 0;
1409 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_fragdropped
);
1419 * Transfer firewall instructions to the fragment structure.
1420 * Only trust info in the fragment at offset 0.
1422 if (ip
->ip_off
== 0) {
1424 fp
->ipq_div_info
= *divinfo
;
1426 fp
->ipq_divert
= *divinfo
;
1428 fp
->ipq_div_cookie
= *divcookie
;
1435 * Check for complete reassembly and perform frag per packet
1438 * Frag limiting is performed here so that the nth frag has
1439 * a chance to complete the packet before we drop the packet.
1440 * As a result, n+1 frags are actually allowed per packet, but
1441 * only n will ever be stored. (n = maxfragsperpacket.)
1445 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
) {
1446 if (GETIP(q
)->ip_off
!= next
) {
1447 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
1448 OSAddAtomic(fp
->ipq_nfrags
, (SInt32
*)&ipstat
.ips_fragdropped
);
1453 next
+= GETIP(q
)->ip_len
;
1455 /* Make sure the last packet didn't have the IP_MF flag */
1456 if (p
->m_flags
& M_FRAG
) {
1457 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
1458 OSAddAtomic(fp
->ipq_nfrags
, (SInt32
*)&ipstat
.ips_fragdropped
);
1465 * Reassembly is complete. Make sure the packet is a sane size.
1469 if (next
+ (IP_VHL_HL(ip
->ip_vhl
) << 2) > IP_MAXPACKET
) {
1470 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_toolong
);
1471 OSAddAtomic(fp
->ipq_nfrags
, (SInt32
*)&ipstat
.ips_fragdropped
);
1477 * Concatenate fragments.
1485 for (q
= nq
; q
!= NULL
; q
= nq
) {
1487 q
->m_nextpkt
= NULL
;
1488 if (q
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1489 m
->m_pkthdr
.csum_flags
= 0;
1491 m
->m_pkthdr
.csum_flags
&= q
->m_pkthdr
.csum_flags
;
1492 m
->m_pkthdr
.csum_data
+= q
->m_pkthdr
.csum_data
;
1499 * Extract firewall instructions from the fragment structure.
1502 *divinfo
= fp
->ipq_div_info
;
1504 *divinfo
= fp
->ipq_divert
;
1506 *divcookie
= fp
->ipq_div_cookie
;
1510 mac_mbuf_label_associate_ipq(fp
, m
);
1511 mac_ipq_label_destroy(fp
);
1514 * Create header for new ip packet by
1515 * modifying header of first packet;
1516 * dequeue and discard fragment reassembly header.
1517 * Make header visible.
1520 ip
->ip_src
= fp
->ipq_src
;
1521 ip
->ip_dst
= fp
->ipq_dst
;
1523 TAILQ_REMOVE(&ipq_list
, fp
, ipq_list
);
1524 currentfrags
-= fp
->ipq_nfrags
;
1526 (void) m_free(dtom(fp
));
1527 m
->m_len
+= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1528 m
->m_data
-= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1529 /* some debugging cruft by sklower, below, will go away soon */
1530 if (m
->m_flags
& M_PKTHDR
) { /* XXX this should be done elsewhere */
1532 for (t
= m
; t
; t
= t
->m_next
)
1534 m
->m_pkthdr
.len
= plen
;
1543 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_fragdropped
);
1553 * Free a fragment reassembly header and all
1554 * associated datagrams.
1557 ip_freef(struct ipq
*fp
)
1559 lck_mtx_assert(ip_mutex
, LCK_MTX_ASSERT_OWNED
);
1560 currentfrags
-= fp
->ipq_nfrags
;
1561 m_freem_list(fp
->ipq_frags
);
1563 TAILQ_REMOVE(&ipq_list
, fp
, ipq_list
);
1564 (void) m_free(dtom(fp
));
1569 * IP timer processing;
1570 * if a timer expires on a reassembly
1571 * queue, discard it.
1578 lck_mtx_lock(ip_mutex
);
1579 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1583 while (fp
!= &ipq
[i
]) {
1586 if (fp
->prev
->ipq_ttl
== 0) {
1587 OSAddAtomic(fp
->ipq_nfrags
, (SInt32
*)&ipstat
.ips_fragtimeout
);
1593 * If we are over the maximum number of fragments
1594 * (due to the limit being lowered), drain off
1595 * enough to get down to the new limit.
1597 if (maxnipq
>= 0 && nipq
> maxnipq
) {
1598 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1599 while (nipq
> maxnipq
&&
1600 (ipq
[i
].next
!= &ipq
[i
])) {
1601 OSAddAtomic(ipq
[i
].next
->ipq_nfrags
, (SInt32
*)&ipstat
.ips_fragdropped
);
1602 ip_freef(ipq
[i
].next
);
1607 lck_mtx_unlock(ip_mutex
);
1611 * Drain off all datagram fragments.
1618 lck_mtx_lock(ip_mutex
);
1619 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1620 while (ipq
[i
].next
!= &ipq
[i
]) {
1621 OSAddAtomic(ipq
[i
].next
->ipq_nfrags
, (SInt32
*)&ipstat
.ips_fragdropped
);
1622 ip_freef(ipq
[i
].next
);
1625 lck_mtx_unlock(ip_mutex
);
1630 * Do option processing on a datagram,
1631 * possibly discarding it if bad options are encountered,
1632 * or forwarding it if source-routed.
1633 * The pass argument is used when operating in the IPSTEALTH
1634 * mode to tell what options to process:
1635 * [LS]SRR (pass 0) or the others (pass 1).
1636 * The reason for as many as two passes is that when doing IPSTEALTH,
1637 * non-routing options should be processed only if the packet is for us.
1638 * Returns 1 if packet has been forwarded/freed,
1639 * 0 if the packet should be processed further.
1642 ip_dooptions(struct mbuf
*m
, __unused
int pass
, struct sockaddr_in
*next_hop
, struct route
*ipforward_rt
)
1644 struct ip
*ip
= mtod(m
, struct ip
*);
1646 struct ip_timestamp
*ipt
;
1647 struct in_ifaddr
*ia
;
1648 int opt
, optlen
, cnt
, off
, code
, type
= ICMP_PARAMPROB
, forward
= 0;
1649 struct in_addr
*sin
, dst
;
1653 cp
= (u_char
*)(ip
+ 1);
1654 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1655 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1656 opt
= cp
[IPOPT_OPTVAL
];
1657 if (opt
== IPOPT_EOL
)
1659 if (opt
== IPOPT_NOP
)
1662 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
)) {
1663 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1666 optlen
= cp
[IPOPT_OLEN
];
1667 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
) {
1668 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1678 * Source routing with record.
1679 * Find interface with current destination address.
1680 * If none on this machine then drop if strictly routed,
1681 * or do nothing if loosely routed.
1682 * Record interface address and bring up next address
1683 * component. If strictly routed make sure next
1684 * address is on directly accessible net.
1688 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1689 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1692 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1693 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1696 ipaddr
.sin_addr
= ip
->ip_dst
;
1697 ia
= (struct in_ifaddr
*)
1698 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
1700 if (opt
== IPOPT_SSRR
) {
1701 type
= ICMP_UNREACH
;
1702 code
= ICMP_UNREACH_SRCFAIL
;
1705 if (!ip_dosourceroute
)
1706 goto nosourcerouting
;
1708 * Loose routing, and not at next destination
1709 * yet; nothing to do except forward.
1714 ifafree(&ia
->ia_ifa
);
1717 off
--; /* 0 origin */
1718 if (off
> optlen
- (int)sizeof(struct in_addr
)) {
1720 * End of source route. Should be for us.
1722 if (!ip_acceptsourceroute
)
1723 goto nosourcerouting
;
1724 save_rte(cp
, ip
->ip_src
);
1728 if (!ip_dosourceroute
) {
1730 char buf
[MAX_IPv4_STR_LEN
];
1731 char buf2
[MAX_IPv4_STR_LEN
];
1733 * Acting as a router, so generate ICMP
1737 "attempted source route from %s to %s\n",
1738 inet_ntop(AF_INET
, &ip
->ip_src
, buf
, sizeof(buf
)),
1739 inet_ntop(AF_INET
, &ip
->ip_dst
, buf2
, sizeof(buf2
)));
1740 type
= ICMP_UNREACH
;
1741 code
= ICMP_UNREACH_SRCFAIL
;
1745 * Not acting as a router, so silently drop.
1747 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_cantforward
);
1754 * locate outgoing interface
1756 (void)memcpy(&ipaddr
.sin_addr
, cp
+ off
,
1757 sizeof(ipaddr
.sin_addr
));
1759 if (opt
== IPOPT_SSRR
) {
1760 #define INA struct in_ifaddr *
1761 #define SA struct sockaddr *
1762 if ((ia
= (INA
)ifa_ifwithdstaddr((SA
)&ipaddr
)) == 0) {
1763 ia
= (INA
)ifa_ifwithnet((SA
)&ipaddr
);
1766 ia
= ip_rtaddr(ipaddr
.sin_addr
, ipforward_rt
);
1769 type
= ICMP_UNREACH
;
1770 code
= ICMP_UNREACH_SRCFAIL
;
1773 ip
->ip_dst
= ipaddr
.sin_addr
;
1774 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1775 sizeof(struct in_addr
));
1776 ifafree(&ia
->ia_ifa
);
1778 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1780 * Let ip_intr's mcast routing check handle mcast pkts
1782 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
1786 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1787 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1790 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1791 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1795 * If no space remains, ignore.
1797 off
--; /* 0 origin */
1798 if (off
> optlen
- (int)sizeof(struct in_addr
))
1800 (void)memcpy(&ipaddr
.sin_addr
, &ip
->ip_dst
,
1801 sizeof(ipaddr
.sin_addr
));
1803 * locate outgoing interface; if we're the destination,
1804 * use the incoming interface (should be same).
1806 if ((ia
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0) {
1807 if ((ia
= ip_rtaddr(ipaddr
.sin_addr
, ipforward_rt
)) == 0) {
1808 type
= ICMP_UNREACH
;
1809 code
= ICMP_UNREACH_HOST
;
1813 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1814 sizeof(struct in_addr
));
1815 ifafree(&ia
->ia_ifa
);
1817 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1821 code
= cp
- (u_char
*)ip
;
1822 ipt
= (struct ip_timestamp
*)cp
;
1823 if (ipt
->ipt_len
< 4 || ipt
->ipt_len
> 40) {
1824 code
= (u_char
*)&ipt
->ipt_len
- (u_char
*)ip
;
1827 if (ipt
->ipt_ptr
< 5) {
1828 code
= (u_char
*)&ipt
->ipt_ptr
- (u_char
*)ip
;
1832 ipt
->ipt_len
- (int)sizeof(int32_t)) {
1833 if (++ipt
->ipt_oflw
== 0) {
1834 code
= (u_char
*)&ipt
->ipt_ptr
-
1840 sin
= (struct in_addr
*)(cp
+ ipt
->ipt_ptr
- 1);
1841 switch (ipt
->ipt_flg
) {
1843 case IPOPT_TS_TSONLY
:
1846 case IPOPT_TS_TSANDADDR
:
1847 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1848 sizeof(struct in_addr
) > ipt
->ipt_len
) {
1849 code
= (u_char
*)&ipt
->ipt_ptr
-
1853 ipaddr
.sin_addr
= dst
;
1854 ia
= (INA
)ifaof_ifpforaddr((SA
)&ipaddr
,
1858 (void)memcpy(sin
, &IA_SIN(ia
)->sin_addr
,
1859 sizeof(struct in_addr
));
1860 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
1861 ifafree(&ia
->ia_ifa
);
1865 case IPOPT_TS_PRESPEC
:
1866 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1867 sizeof(struct in_addr
) > ipt
->ipt_len
) {
1868 code
= (u_char
*)&ipt
->ipt_ptr
-
1872 (void)memcpy(&ipaddr
.sin_addr
, sin
,
1873 sizeof(struct in_addr
));
1874 if ((ia
= (struct in_ifaddr
*)ifa_ifwithaddr((SA
)&ipaddr
)) == 0)
1876 ifafree(&ia
->ia_ifa
);
1878 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
1882 /* XXX can't take &ipt->ipt_flg */
1883 code
= (u_char
*)&ipt
->ipt_ptr
-
1888 (void)memcpy(cp
+ ipt
->ipt_ptr
- 1, &ntime
,
1890 ipt
->ipt_ptr
+= sizeof(n_time
);
1893 if (forward
&& ipforwarding
) {
1894 ip_forward(m
, 1, next_hop
, ipforward_rt
);
1895 if (ipforward_rt
->ro_rt
!= NULL
) {
1896 rtfree(ipforward_rt
->ro_rt
);
1897 ipforward_rt
->ro_rt
= NULL
;
1903 ip
->ip_len
-= IP_VHL_HL(ip
->ip_vhl
) << 2; /* XXX icmp_error adds in hdr length */
1904 icmp_error(m
, type
, code
, 0, 0);
1905 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_badoptions
);
1910 * Given address of next destination (final or next hop),
1911 * return internet address info of interface to be used to get there.
1914 ip_rtaddr(struct in_addr dst
, struct route
*rt
)
1916 struct sockaddr_in
*sin
;
1918 sin
= (struct sockaddr_in
*)&rt
->ro_dst
;
1920 lck_mtx_lock(rt_mtx
);
1921 if (rt
->ro_rt
== 0 || dst
.s_addr
!= sin
->sin_addr
.s_addr
||
1922 rt
->ro_rt
->generation_id
!= route_generation
) {
1924 rtfree_locked(rt
->ro_rt
);
1927 sin
->sin_family
= AF_INET
;
1928 sin
->sin_len
= sizeof(*sin
);
1929 sin
->sin_addr
= dst
;
1931 rtalloc_ign_locked(rt
, RTF_PRCLONING
);
1933 if (rt
->ro_rt
== 0) {
1934 lck_mtx_unlock(rt_mtx
);
1935 return ((struct in_ifaddr
*)0);
1938 if (rt
->ro_rt
->rt_ifa
)
1939 ifaref(rt
->ro_rt
->rt_ifa
);
1940 lck_mtx_unlock(rt_mtx
);
1941 return ((struct in_ifaddr
*) rt
->ro_rt
->rt_ifa
);
1945 * Save incoming source route for use in replies,
1946 * to be picked up later by ip_srcroute if the receiver is interested.
1949 save_rte(u_char
*option
, struct in_addr dst
)
1953 olen
= option
[IPOPT_OLEN
];
1956 printf("save_rte: olen %d\n", olen
);
1958 if (olen
> sizeof(ip_srcrt
) - (1 + sizeof(dst
)))
1960 bcopy(option
, ip_srcrt
.srcopt
, olen
);
1961 ip_nhops
= (olen
- IPOPT_OFFSET
- 1) / sizeof(struct in_addr
);
1966 * Retrieve incoming source route for use in replies,
1967 * in the same form used by setsockopt.
1968 * The first hop is placed before the options, will be removed later.
1973 struct in_addr
*p
, *q
;
1977 return ((struct mbuf
*)0);
1978 m
= m_get(M_DONTWAIT
, MT_HEADER
);
1980 return ((struct mbuf
*)0);
1982 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1984 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1985 m
->m_len
= ip_nhops
* sizeof(struct in_addr
) + sizeof(struct in_addr
) +
1989 printf("ip_srcroute: nhops %d mlen %d", ip_nhops
, m
->m_len
);
1993 * First save first hop for return route
1995 p
= &ip_srcrt
.route
[ip_nhops
- 1];
1996 *(mtod(m
, struct in_addr
*)) = *p
--;
1999 printf(" hops %lx", (u_long
)ntohl(mtod(m
, struct in_addr
*)->s_addr
));
2003 * Copy option fields and padding (nop) to mbuf.
2005 ip_srcrt
.nop
= IPOPT_NOP
;
2006 ip_srcrt
.srcopt
[IPOPT_OFFSET
] = IPOPT_MINOFF
;
2007 (void)memcpy(mtod(m
, caddr_t
) + sizeof(struct in_addr
),
2008 &ip_srcrt
.nop
, OPTSIZ
);
2009 q
= (struct in_addr
*)(mtod(m
, caddr_t
) +
2010 sizeof(struct in_addr
) + OPTSIZ
);
2013 * Record return path as an IP source route,
2014 * reversing the path (pointers are now aligned).
2016 while (p
>= ip_srcrt
.route
) {
2019 printf(" %lx", (u_long
)ntohl(q
->s_addr
));
2024 * Last hop goes to final destination.
2029 printf(" %lx\n", (u_long
)ntohl(q
->s_addr
));
2035 * Strip out IP options, at higher
2036 * level protocol in the kernel.
2037 * Second argument is buffer to which options
2038 * will be moved, and return value is their length.
2039 * XXX should be deleted; last arg currently ignored.
2042 ip_stripoptions(struct mbuf
*m
, __unused
struct mbuf
*mopt
)
2045 struct ip
*ip
= mtod(m
, struct ip
*);
2049 olen
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
2050 opts
= (caddr_t
)(ip
+ 1);
2051 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
2052 bcopy(opts
+ olen
, opts
, (unsigned)i
);
2054 if (m
->m_flags
& M_PKTHDR
)
2055 m
->m_pkthdr
.len
-= olen
;
2056 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
2059 u_char inetctlerrmap
[PRC_NCMDS
] = {
2061 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
2062 ENETUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
2063 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
2065 ENOPROTOOPT
, ECONNREFUSED
2069 * Forward a packet. If some error occurs return the sender
2070 * an icmp packet. Note we can't always generate a meaningful
2071 * icmp message because icmp doesn't have a large enough repertoire
2072 * of codes and types.
2074 * If not forwarding, just drop the packet. This could be confusing
2075 * if ipforwarding was zero but some routing protocol was advancing
2076 * us as a gateway to somewhere. However, we must let the routing
2077 * protocol deal with that.
2079 * The srcrt parameter indicates whether the packet is being forwarded
2080 * via a source route.
2083 ip_forward(struct mbuf
*m
, int srcrt
, struct sockaddr_in
*next_hop
, struct route
*ipforward_rt
)
2085 struct ip
*ip
= mtod(m
, struct ip
*);
2086 struct sockaddr_in
*sin
;
2088 int error
, type
= 0, code
= 0;
2091 struct in_addr pkt_dst
;
2092 struct ifnet
*destifp
;
2094 struct ifnet dummyifp
;
2099 * Cache the destination address of the packet; this may be
2100 * changed by use of 'ipfw fwd'.
2102 pkt_dst
= next_hop
? next_hop
->sin_addr
: ip
->ip_dst
;
2106 printf("forward: src %lx dst %lx ttl %x\n",
2107 (u_long
)ip
->ip_src
.s_addr
, (u_long
)pkt_dst
.s_addr
,
2112 if (m
->m_flags
& (M_BCAST
|M_MCAST
) || in_canforward(pkt_dst
) == 0) {
2113 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_cantforward
);
2120 if (ip
->ip_ttl
<= IPTTLDEC
) {
2121 icmp_error(m
, ICMP_TIMXCEED
, ICMP_TIMXCEED_INTRANS
,
2129 sin
= (struct sockaddr_in
*)&ipforward_rt
->ro_dst
;
2130 if ((rt
= ipforward_rt
->ro_rt
) == 0 ||
2131 pkt_dst
.s_addr
!= sin
->sin_addr
.s_addr
||
2132 ipforward_rt
->ro_rt
->generation_id
!= route_generation
) {
2133 if (ipforward_rt
->ro_rt
) {
2134 rtfree(ipforward_rt
->ro_rt
);
2135 ipforward_rt
->ro_rt
= 0;
2137 sin
->sin_family
= AF_INET
;
2138 sin
->sin_len
= sizeof(*sin
);
2139 sin
->sin_addr
= pkt_dst
;
2141 rtalloc_ign(ipforward_rt
, RTF_PRCLONING
);
2142 if (ipforward_rt
->ro_rt
== 0) {
2143 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_HOST
, dest
, 0);
2146 rt
= ipforward_rt
->ro_rt
;
2150 * Save the IP header and at most 8 bytes of the payload,
2151 * in case we need to generate an ICMP message to the src.
2153 * We don't use m_copy() because it might return a reference
2154 * to a shared cluster. Both this function and ip_output()
2155 * assume exclusive access to the IP header in `m', so any
2156 * data in a cluster may change before we reach icmp_error().
2158 MGET(mcopy
, M_DONTWAIT
, m
->m_type
);
2159 if (mcopy
!= NULL
) {
2160 M_COPY_PKTHDR(mcopy
, m
);
2161 mcopy
->m_len
= imin((IP_VHL_HL(ip
->ip_vhl
) << 2) + 8,
2163 m_copydata(m
, 0, mcopy
->m_len
, mtod(mcopy
, caddr_t
));
2169 ip
->ip_ttl
-= IPTTLDEC
;
2175 * If forwarding packet using same interface that it came in on,
2176 * perhaps should send a redirect to sender to shortcut a hop.
2177 * Only send redirect if source is sending directly to us,
2178 * and if packet was not source routed (or has any options).
2179 * Also, don't send redirect if forwarding using a default route
2180 * or a route modified by a redirect.
2182 #define satosin(sa) ((struct sockaddr_in *)(sa))
2183 if (rt
->rt_ifp
== m
->m_pkthdr
.rcvif
&&
2184 (rt
->rt_flags
& (RTF_DYNAMIC
|RTF_MODIFIED
)) == 0 &&
2185 satosin(rt_key(rt
))->sin_addr
.s_addr
!= 0 &&
2186 ipsendredirects
&& !srcrt
) {
2187 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
2188 u_long src
= ntohl(ip
->ip_src
.s_addr
);
2191 (src
& RTA(rt
)->ia_subnetmask
) == RTA(rt
)->ia_subnet
) {
2192 if (rt
->rt_flags
& RTF_GATEWAY
)
2193 dest
= satosin(rt
->rt_gateway
)->sin_addr
.s_addr
;
2195 dest
= pkt_dst
.s_addr
;
2196 /* Router requirements says to only send host redirects */
2197 type
= ICMP_REDIRECT
;
2198 code
= ICMP_REDIRECT_HOST
;
2201 printf("redirect (%d) to %lx\n", code
, (u_long
)dest
);
2208 /* Pass IPFORWARD info if available */
2210 struct ip_fwd_tag
*ipfwd_tag
;
2212 tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
,
2213 sizeof(struct sockaddr_in
), M_NOWAIT
);
2220 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
2221 ipfwd_tag
->next_hop
= next_hop
;
2223 m_tag_prepend(m
, tag
);
2225 error
= ip_output_list(m
, 0, (struct mbuf
*)0, ipforward_rt
,
2226 IP_FORWARDING
, 0, NULL
);
2229 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_cantforward
);
2231 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_forward
);
2233 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_redirectsent
);
2236 ipflow_create(ipforward_rt
, mcopy
);
2248 case 0: /* forwarded, but need redirect */
2249 /* type, code set above */
2252 case ENETUNREACH
: /* shouldn't happen, checked above */
2257 type
= ICMP_UNREACH
;
2258 code
= ICMP_UNREACH_HOST
;
2262 type
= ICMP_UNREACH
;
2263 code
= ICMP_UNREACH_NEEDFRAG
;
2265 if (ipforward_rt
->ro_rt
)
2266 destifp
= ipforward_rt
->ro_rt
->rt_ifp
;
2269 * If the packet is routed over IPsec tunnel, tell the
2270 * originator the tunnel MTU.
2271 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
2274 if (ipforward_rt
->ro_rt
) {
2275 struct secpolicy
*sp
= NULL
;
2281 destifp
= ipforward_rt
->ro_rt
->rt_ifp
;
2282 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_cantfrag
);
2285 sp
= ipsec4_getpolicybyaddr(mcopy
,
2291 destifp
= ipforward_rt
->ro_rt
->rt_ifp
;
2293 /* count IPsec header size */
2294 ipsechdr
= ipsec_hdrsiz(sp
);
2297 * find the correct route for outer IPv4
2298 * header, compute tunnel MTU.
2301 * The "dummyifp" code relies upon the fact
2302 * that icmp_error() touches only ifp->if_mtu.
2307 if (sp
->req
!= NULL
) {
2308 if (sp
->req
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2309 struct secasindex saidx
;
2311 struct secasvar
*sav
;
2313 ipm
= mtod(mcopy
, struct ip
*);
2314 bcopy(&sp
->req
->saidx
, &saidx
, sizeof(saidx
));
2315 saidx
.mode
= sp
->req
->saidx
.mode
;
2316 saidx
.reqid
= sp
->req
->saidx
.reqid
;
2317 sin
= (struct sockaddr_in
*)&saidx
.src
;
2318 if (sin
->sin_len
== 0) {
2319 sin
->sin_len
= sizeof(*sin
);
2320 sin
->sin_family
= AF_INET
;
2321 sin
->sin_port
= IPSEC_PORT_ANY
;
2322 bcopy(&ipm
->ip_src
, &sin
->sin_addr
,
2323 sizeof(sin
->sin_addr
));
2325 sin
= (struct sockaddr_in
*)&saidx
.dst
;
2326 if (sin
->sin_len
== 0) {
2327 sin
->sin_len
= sizeof(*sin
);
2328 sin
->sin_family
= AF_INET
;
2329 sin
->sin_port
= IPSEC_PORT_ANY
;
2330 bcopy(&ipm
->ip_dst
, &sin
->sin_addr
,
2331 sizeof(sin
->sin_addr
));
2333 sav
= key_allocsa_policy(&saidx
);
2335 if (sav
->sah
!= NULL
) {
2336 ro
= &sav
->sah
->sa_route
;
2337 if (ro
->ro_rt
&& ro
->ro_rt
->rt_ifp
) {
2339 ro
->ro_rt
->rt_ifp
->if_mtu
;
2340 dummyifp
.if_mtu
-= ipsechdr
;
2341 destifp
= &dummyifp
;
2344 key_freesav(sav
, KEY_SADB_UNLOCKED
);
2348 key_freesp(sp
, KEY_SADB_UNLOCKED
);
2352 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_cantfrag
);
2356 type
= ICMP_SOURCEQUENCH
;
2360 case EACCES
: /* ipfw denied packet */
2364 icmp_error(mcopy
, type
, code
, dest
, destifp
);
2374 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
2378 *mp
= sbcreatecontrol((caddr_t
) &tv
, sizeof(tv
),
2379 SCM_TIMESTAMP
, SOL_SOCKET
);
2381 mp
= &(*mp
)->m_next
;
2383 if (inp
->inp_flags
& INP_RECVDSTADDR
) {
2384 *mp
= sbcreatecontrol((caddr_t
) &ip
->ip_dst
,
2385 sizeof(struct in_addr
), IP_RECVDSTADDR
, IPPROTO_IP
);
2387 mp
= &(*mp
)->m_next
;
2391 * Moving these out of udp_input() made them even more broken
2392 * than they already were.
2394 /* options were tossed already */
2395 if (inp
->inp_flags
& INP_RECVOPTS
) {
2396 *mp
= sbcreatecontrol((caddr_t
) opts_deleted_above
,
2397 sizeof(struct in_addr
), IP_RECVOPTS
, IPPROTO_IP
);
2399 mp
= &(*mp
)->m_next
;
2401 /* ip_srcroute doesn't do what we want here, need to fix */
2402 if (inp
->inp_flags
& INP_RECVRETOPTS
) {
2403 *mp
= sbcreatecontrol((caddr_t
) ip_srcroute(),
2404 sizeof(struct in_addr
), IP_RECVRETOPTS
, IPPROTO_IP
);
2406 mp
= &(*mp
)->m_next
;
2409 if (inp
->inp_flags
& INP_RECVIF
) {
2412 struct sockaddr_dl sdl
;
2415 struct sockaddr_dl
*sdp
;
2416 struct sockaddr_dl
*sdl2
= &sdlbuf
.sdl
;
2418 ifnet_head_lock_shared();
2419 if (((ifp
= m
->m_pkthdr
.rcvif
))
2420 && ( ifp
->if_index
&& (ifp
->if_index
<= if_index
))) {
2421 struct ifaddr
*ifa
= ifnet_addrs
[ifp
->if_index
- 1];
2423 if (!ifa
|| !ifa
->ifa_addr
)
2426 sdp
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
2428 * Change our mind and don't try copy.
2430 if ((sdp
->sdl_family
!= AF_LINK
)
2431 || (sdp
->sdl_len
> sizeof(sdlbuf
))) {
2434 bcopy(sdp
, sdl2
, sdp
->sdl_len
);
2438 = offsetof(struct sockaddr_dl
, sdl_data
[0]);
2439 sdl2
->sdl_family
= AF_LINK
;
2440 sdl2
->sdl_index
= 0;
2441 sdl2
->sdl_nlen
= sdl2
->sdl_alen
= sdl2
->sdl_slen
= 0;
2444 *mp
= sbcreatecontrol((caddr_t
) sdl2
, sdl2
->sdl_len
,
2445 IP_RECVIF
, IPPROTO_IP
);
2447 mp
= &(*mp
)->m_next
;
2449 if (inp
->inp_flags
& INP_RECVTTL
) {
2450 *mp
= sbcreatecontrol((caddr_t
)&ip
->ip_ttl
, sizeof(ip
->ip_ttl
), IP_RECVTTL
, IPPROTO_IP
);
2451 if (*mp
) mp
= &(*mp
)->m_next
;
2456 ip_rsvp_init(struct socket
*so
)
2458 if (so
->so_type
!= SOCK_RAW
||
2459 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2462 if (ip_rsvpd
!= NULL
)
2467 * This may seem silly, but we need to be sure we don't over-increment
2468 * the RSVP counter, in case something slips up.
2483 * This may seem silly, but we need to be sure we don't over-decrement
2484 * the RSVP counter, in case something slips up.