2 * Copyright (c) 2000-2007 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;
193 * XXX - Setting ip_checkinterface mostly implements the receive side of
194 * the Strong ES model described in RFC 1122, but since the routing table
195 * and transmit implementation do not implement the Strong ES model,
196 * setting this to 1 results in an odd hybrid.
198 * XXX - ip_checkinterface currently must be disabled if you use ipnat
199 * to translate the destination address to another local interface.
201 * XXX - ip_checkinterface must be disabled if you add IP aliases
202 * to the loopback interface instead of the interface where the
203 * packets for those addresses are received.
205 static int ip_checkinterface
= 0;
206 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, check_interface
, CTLFLAG_RW
,
207 &ip_checkinterface
, 0, "Verify packet arrives on correct interface");
211 static int ipprintfs
= 0;
214 extern int in_proto_count
;
215 extern struct domain inetdomain
;
216 extern struct protosw inetsw
[];
217 struct protosw
*ip_protox
[IPPROTO_MAX
];
218 static int ipqmaxlen
= IFQ_MAXLEN
;
219 struct in_ifaddrhead in_ifaddrhead
; /* first inet address */
220 struct ifqueue ipintrq
;
221 SYSCTL_INT(_net_inet_ip
, IPCTL_INTRQMAXLEN
, intr_queue_maxlen
, CTLFLAG_RW
,
222 &ipintrq
.ifq_maxlen
, 0, "Maximum size of the IP input queue");
223 SYSCTL_INT(_net_inet_ip
, IPCTL_INTRQDROPS
, intr_queue_drops
, CTLFLAG_RD
,
224 &ipintrq
.ifq_drops
, 0, "Number of packets dropped from the IP input queue");
226 struct ipstat ipstat
;
227 SYSCTL_STRUCT(_net_inet_ip
, IPCTL_STATS
, stats
, CTLFLAG_RD
,
228 &ipstat
, ipstat
, "IP statistics (struct ipstat, netinet/ip_var.h)");
230 /* Packet reassembly stuff */
231 #define IPREASS_NHASH_LOG2 6
232 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
233 #define IPREASS_HMASK (IPREASS_NHASH - 1)
234 #define IPREASS_HASH(x,y) \
235 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
237 static struct ipq ipq
[IPREASS_NHASH
];
238 static TAILQ_HEAD(ipq_list
, ipq
) ipq_list
=
239 TAILQ_HEAD_INITIALIZER(ipq_list
);
240 const int ipintrq_present
= 1;
242 lck_attr_t
*ip_mutex_attr
;
243 lck_grp_t
*ip_mutex_grp
;
244 lck_grp_attr_t
*ip_mutex_grp_attr
;
245 lck_mtx_t
*inet_domain_mutex
;
246 extern lck_mtx_t
*domain_proto_mtx
;
249 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFMTU
, mtu
, CTLFLAG_RW
,
250 &ip_mtu
, 0, "Default MTU");
254 static int ipstealth
= 0;
255 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, stealth
, CTLFLAG_RW
,
262 ip_fw_chk_t
*ip_fw_chk_ptr
;
268 ip_dn_io_t
*ip_dn_io_ptr
;
271 int (*fr_checkp
)(struct ip
*, int, struct ifnet
*, int, struct mbuf
**) = NULL
;
272 #endif /* IPFIREWALL */
274 SYSCTL_NODE(_net_inet_ip
, OID_AUTO
, linklocal
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "link local");
276 struct ip_linklocal_stat ip_linklocal_stat
;
277 SYSCTL_STRUCT(_net_inet_ip_linklocal
, OID_AUTO
, stat
, CTLFLAG_RD
,
278 &ip_linklocal_stat
, ip_linklocal_stat
,
279 "Number of link local packets with TTL less than 255");
281 SYSCTL_NODE(_net_inet_ip_linklocal
, OID_AUTO
, in
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "link local input");
283 int ip_linklocal_in_allowbadttl
= 1;
284 SYSCTL_INT(_net_inet_ip_linklocal_in
, OID_AUTO
, allowbadttl
, CTLFLAG_RW
,
285 &ip_linklocal_in_allowbadttl
, 0,
286 "Allow incoming link local packets with TTL less than 255");
290 * We need to save the IP options in case a protocol wants to respond
291 * to an incoming packet over the same route if the packet got here
292 * using IP source routing. This allows connection establishment and
293 * maintenance when the remote end is on a network that is not known
296 static int ip_nhops
= 0;
297 static struct ip_srcrt
{
298 struct in_addr dst
; /* final destination */
299 char nop
; /* one NOP to align */
300 char srcopt
[IPOPT_OFFSET
+ 1]; /* OPTVAL, OLEN and OFFSET */
301 struct in_addr route
[MAX_IPOPTLEN
/sizeof(struct in_addr
)];
305 static void save_rte(u_char
*, struct in_addr
);
306 static int ip_dooptions(struct mbuf
*, int, struct sockaddr_in
*, struct route
*ipforward_rt
);
307 static void ip_forward(struct mbuf
*, int, struct sockaddr_in
*, struct route
*ipforward_rt
);
308 static void ip_freef(struct ipq
*);
311 static struct mbuf
*ip_reass(struct mbuf
*,
312 struct ipq
*, struct ipq
*, u_int32_t
*, u_int16_t
*);
314 static struct mbuf
*ip_reass(struct mbuf
*,
315 struct ipq
*, struct ipq
*, u_int16_t
*, u_int16_t
*);
318 static struct mbuf
*ip_reass(struct mbuf
*, struct ipq
*, struct ipq
*);
324 extern u_short ip_id
;
326 int ip_use_randomid
= 1;
327 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, random_id
, CTLFLAG_RW
,
328 &ip_use_randomid
, 0, "Randomize IP packets IDs");
331 extern u_long route_generation
;
334 * IP initialization: fill in IP protocol switch table.
335 * All protocols not implemented in kernel go to raw IP protocol handler.
342 static int ip_initialized
= 0;
347 TAILQ_INIT(&in_ifaddrhead
);
348 pr
= pffindproto_locked(PF_INET
, IPPROTO_RAW
, SOCK_RAW
);
351 for (i
= 0; i
< IPPROTO_MAX
; i
++)
353 for (pr
= inetdomain
.dom_protosw
; pr
; pr
= pr
->pr_next
)
354 { if(!((unsigned int)pr
->pr_domain
)) continue; /* If uninitialized, skip */
355 if (pr
->pr_domain
->dom_family
== PF_INET
&&
356 pr
->pr_protocol
&& pr
->pr_protocol
!= IPPROTO_RAW
)
357 ip_protox
[pr
->pr_protocol
] = pr
;
359 for (i
= 0; i
< IPREASS_NHASH
; i
++)
360 ipq
[i
].next
= ipq
[i
].prev
= &ipq
[i
];
362 maxnipq
= nmbclusters
/ 32;
363 maxfrags
= maxnipq
* 2;
364 maxfragsperpacket
= 128; /* enough for 64k in 512 byte fragments */
368 struct timeval timenow
;
369 getmicrotime(&timenow
);
370 ip_id
= timenow
.tv_sec
& 0xffff;
373 ipintrq
.ifq_maxlen
= ipqmaxlen
;
377 ip_mutex_grp_attr
= lck_grp_attr_alloc_init();
379 ip_mutex_grp
= lck_grp_alloc_init("ip", ip_mutex_grp_attr
);
381 ip_mutex_attr
= lck_attr_alloc_init();
383 if ((ip_mutex
= lck_mtx_alloc_init(ip_mutex_grp
, ip_mutex_attr
)) == NULL
) {
384 printf("ip_init: can't alloc ip_mutex\n");
390 sadb_stat_mutex_grp_attr
= lck_grp_attr_alloc_init();
391 sadb_stat_mutex_grp
= lck_grp_alloc_init("sadb_stat", sadb_stat_mutex_grp_attr
);
392 sadb_stat_mutex_attr
= lck_attr_alloc_init();
394 if ((sadb_stat_mutex
= lck_mtx_alloc_init(sadb_stat_mutex_grp
, sadb_stat_mutex_attr
)) == NULL
) {
395 printf("ip_init: can't alloc sadb_stat_mutex\n");
406 protocol_family_t __unused protocol
,
412 /* ip_input should handle a list of packets but does not yet */
414 for (packet
= packet_list
; packet
; packet
= packet_list
) {
416 packet_list
= mbuf_nextpkt(packet
);
417 mbuf_setnextpkt(packet
, NULL
);
422 /* Initialize the PF_INET domain, and add in the pre-defined protos */
429 static int inetdomain_initted
= 0;
431 if (!inetdomain_initted
)
434 kprintf("Initing %d protosw entries\n", in_proto_count
);
437 dp
->dom_flags
= DOM_REENTRANT
;
439 for (i
=0, pr
= &inetsw
[0]; i
<in_proto_count
; i
++, pr
++)
440 net_add_proto(pr
, dp
);
441 inet_domain_mutex
= dp
->dom_mtx
;
442 inetdomain_initted
= 1;
444 lck_mtx_unlock(domain_proto_mtx
);
445 proto_register_input(PF_INET
, ip_proto_input
, NULL
, 1);
446 lck_mtx_lock(domain_proto_mtx
);
450 __private_extern__
void
451 ip_proto_dispatch_in(
455 ipfilter_t inject_ipfref
)
457 struct ipfilter
*filter
;
458 int seen
= (inject_ipfref
== 0);
459 int changed_header
= 0;
462 if (!TAILQ_EMPTY(&ipv4_filters
)) {
464 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
466 if ((struct ipfilter
*)inject_ipfref
== filter
)
468 } else if (filter
->ipf_filter
.ipf_input
) {
471 if (changed_header
== 0) {
473 ip
= mtod(m
, struct ip
*);
474 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
475 ip
->ip_off
= htons(ip
->ip_off
);
477 ip
->ip_sum
= in_cksum(m
, hlen
);
479 result
= filter
->ipf_filter
.ipf_input(
480 filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, hlen
, proto
);
481 if (result
== EJUSTRETURN
) {
495 * If there isn't a specific lock for the protocol
496 * we're about to call, use the generic lock for AF_INET.
497 * otherwise let the protocol deal with its own locking
499 ip
= mtod(m
, struct ip
*);
501 if (changed_header
) {
502 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
503 ip
->ip_off
= ntohs(ip
->ip_off
);
506 if (!(ip_protox
[ip
->ip_p
]->pr_flags
& PR_PROTOLOCK
)) {
507 lck_mtx_lock(inet_domain_mutex
);
508 (*ip_protox
[ip
->ip_p
]->pr_input
)(m
, hlen
);
509 lck_mtx_unlock(inet_domain_mutex
);
512 (*ip_protox
[ip
->ip_p
]->pr_input
)(m
, hlen
);
517 * ipforward_rt cleared in in_addroute()
518 * when a new route is successfully created.
520 static struct sockaddr_in ipaddr
= { sizeof(ipaddr
), AF_INET
, 0 , {0}, {0,0,0,0,0,0,0,0} };
523 * Ip input routine. Checksum and byte swap header. If fragmented
524 * try to reassemble. Process options. Pass to next level.
527 ip_input(struct mbuf
*m
)
531 struct in_ifaddr
*ia
= NULL
;
532 int i
, hlen
, checkif
;
534 struct in_addr pkt_dst
;
535 u_int32_t div_info
= 0; /* packet divert/tee info */
537 struct ip_fw_args args
;
539 ipfilter_t inject_filter_ref
= 0;
541 struct route ipforward_rt
;
543 bzero(&ipforward_rt
, sizeof(struct route
));
549 args
.divert_rule
= 0; /* divert cookie */
550 args
.next_hop
= NULL
;
552 /* Grab info from mtags prepended to the chain */
554 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
555 struct dn_pkt_tag
*dn_tag
;
557 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
558 args
.rule
= dn_tag
->rule
;
560 m_tag_delete(m
, tag
);
562 #endif /* DUMMYNET */
565 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_DIVERT
, NULL
)) != NULL
) {
566 struct divert_tag
*div_tag
;
568 div_tag
= (struct divert_tag
*)(tag
+1);
569 args
.divert_rule
= div_tag
->cookie
;
571 m_tag_delete(m
, tag
);
575 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
, NULL
)) != NULL
) {
576 struct ip_fwd_tag
*ipfwd_tag
;
578 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
579 args
.next_hop
= ipfwd_tag
->next_hop
;
581 m_tag_delete(m
, tag
);
585 if (m
== NULL
|| (m
->m_flags
& M_PKTHDR
) == 0)
586 panic("ip_input no HDR");
589 if (args
.rule
) { /* dummynet already filtered us */
590 ip
= mtod(m
, struct ip
*);
591 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
592 inject_filter_ref
= ipf_get_inject_filter(m
);
595 #endif /* IPFIREWALL */
598 * No need to proccess packet twice if we've
601 inject_filter_ref
= ipf_get_inject_filter(m
);
602 if (inject_filter_ref
!= 0) {
603 ip
= mtod(m
, struct ip
*);
604 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
605 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
606 ip
->ip_off
= ntohs(ip
->ip_off
);
607 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, inject_filter_ref
);
611 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_total
);
613 if (m
->m_pkthdr
.len
< sizeof(struct ip
))
616 if (m
->m_len
< sizeof (struct ip
) &&
617 (m
= m_pullup(m
, sizeof (struct ip
))) == 0) {
618 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_toosmall
);
621 ip
= mtod(m
, struct ip
*);
623 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
624 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
626 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
627 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_badvers
);
631 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
632 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
633 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_badhlen
);
636 if (hlen
> m
->m_len
) {
637 if ((m
= m_pullup(m
, hlen
)) == 0) {
638 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_badhlen
);
641 ip
= mtod(m
, struct ip
*);
644 /* 127/8 must not appear on wire - RFC1122 */
645 if ((ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
646 (ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
) {
647 if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) {
648 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_badaddr
);
653 /* IPv4 Link-Local Addresses as defined in <draft-ietf-zeroconf-ipv4-linklocal-05.txt> */
654 if ((IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
)) ||
655 IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)))) {
656 ip_linklocal_stat
.iplls_in_total
++;
657 if (ip
->ip_ttl
!= MAXTTL
) {
658 OSAddAtomic(1, (SInt32
*)&ip_linklocal_stat
.iplls_in_badttl
);
659 /* Silently drop link local traffic with bad TTL */
660 if (!ip_linklocal_in_allowbadttl
)
664 if ((IF_HWASSIST_CSUM_FLAGS(m
->m_pkthdr
.rcvif
->if_hwassist
) == 0)
665 || (apple_hwcksum_rx
== 0) ||
666 ((m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
) && ip
->ip_p
!= IPPROTO_TCP
)) {
667 m
->m_pkthdr
.csum_flags
= 0; /* invalidate HW generated checksum flags */
670 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
671 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
672 } else if (!(m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) ||
673 apple_hwcksum_tx
== 0) {
675 * Either this is not loopback packet coming from an interface
676 * that does not support checksum offloading, or it is loopback
677 * packet that has undergone software checksumming at the send
678 * side because apple_hwcksum_tx was set to 0. In this case,
679 * calculate the checksum in software to validate the packet.
681 sum
= in_cksum(m
, hlen
);
684 * This is a loopback packet without any valid checksum since
685 * the send side has bypassed it (apple_hwcksum_tx set to 1).
686 * We get here because apple_hwcksum_rx was set to 0, and so
687 * we pretend that all is well.
690 m
->m_pkthdr
.csum_flags
|= CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
691 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
692 m
->m_pkthdr
.csum_data
= 0xffff;
695 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_badsum
);
700 * Convert fields to host representation.
703 if (ip
->ip_len
< hlen
) {
704 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_badlen
);
710 * Check that the amount of data in the buffers
711 * is as at least much as the IP header would have us expect.
712 * Trim mbufs if longer than we expect.
713 * Drop packet if shorter than we expect.
715 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
717 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_tooshort
);
720 if (m
->m_pkthdr
.len
> ip
->ip_len
) {
721 /* Invalidate hwcksuming */
722 m
->m_pkthdr
.csum_flags
= 0;
723 m
->m_pkthdr
.csum_data
= 0;
725 if (m
->m_len
== m
->m_pkthdr
.len
) {
726 m
->m_len
= ip
->ip_len
;
727 m
->m_pkthdr
.len
= ip
->ip_len
;
729 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
733 if (ipsec_bypass
== 0 && ipsec_gethist(m
, NULL
))
739 * Right now when no processing on packet has done
740 * and it is still fresh out of network we do our black
742 * - Firewall: deny/allow/divert
743 * - Xlate: translate packet's addr/port (NAT).
744 * - Pipe: pass pkt through dummynet.
745 * - Wrap: fake packet's addr/port <unimpl.>
746 * - Encapsulate: put it in another IP and send out. <unimp.>
752 #endif /* DUMMYNET */
754 * Check if we want to allow this packet to be processed.
755 * Consider it to be bad if not.
760 if (fr_checkp(ip
, hlen
, m
->m_pkthdr
.rcvif
, 0, &m1
) || !m1
) {
763 ip
= mtod(m
= m1
, struct ip
*);
765 if (fw_enable
&& IPFW_LOADED
) {
766 #if IPFIREWALL_FORWARD
768 * If we've been forwarded from the output side, then
769 * skip the firewall a second time
773 #endif /* IPFIREWALL_FORWARD */
777 i
= ip_fw_chk_ptr(&args
);
780 if ( (i
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) { /* drop */
785 ip
= mtod(m
, struct ip
*); /* just in case m changed */
787 if (i
== 0 && args
.next_hop
== NULL
) { /* common case */
791 if (DUMMYNET_LOADED
&& (i
& IP_FW_PORT_DYNT_FLAG
) != 0) {
792 /* Send packet to the appropriate pipe */
793 ip_dn_io_ptr(m
, i
&0xffff, DN_TO_IP_IN
, &args
);
796 #endif /* DUMMYNET */
798 if (i
!= 0 && (i
& IP_FW_PORT_DYNT_FLAG
) == 0) {
799 /* Divert or tee packet */
804 #if IPFIREWALL_FORWARD
805 if (i
== 0 && args
.next_hop
!= NULL
) {
810 * if we get here, the packet must be dropped
815 #endif /* IPFIREWALL */
819 * Process options and, if not destined for us,
820 * ship it on. ip_dooptions returns 1 when an
821 * error was detected (causing an icmp message
822 * to be sent and the original packet to be freed).
824 ip_nhops
= 0; /* for source routed packets */
826 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, args
.next_hop
, &ipforward_rt
)) {
828 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, NULL
, &ipforward_rt
)) {
833 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
834 * matter if it is destined to another node, or whether it is
835 * a multicast one, RSVP wants it! and prevents it from being forwarded
836 * anywhere else. Also checks if the rsvp daemon is running before
837 * grabbing the packet.
839 if (rsvp_on
&& ip
->ip_p
==IPPROTO_RSVP
)
843 * Check our list of addresses, to see if the packet is for us.
844 * If we don't have any addresses, assume any unicast packet
845 * we receive might be for us (and let the upper layers deal
848 if (TAILQ_EMPTY(&in_ifaddrhead
) &&
849 (m
->m_flags
& (M_MCAST
|M_BCAST
)) == 0)
853 * Cache the destination address of the packet; this may be
854 * changed by use of 'ipfw fwd'.
857 pkt_dst
= args
.next_hop
== NULL
?
858 ip
->ip_dst
: args
.next_hop
->sin_addr
;
860 pkt_dst
= ip
->ip_dst
;
864 * Enable a consistency check between the destination address
865 * and the arrival interface for a unicast packet (the RFC 1122
866 * strong ES model) if IP forwarding is disabled and the packet
867 * is not locally generated and the packet is not subject to
870 * XXX - Checking also should be disabled if the destination
871 * address is ipnat'ed to a different interface.
873 * XXX - Checking is incompatible with IP aliases added
874 * to the loopback interface instead of the interface where
875 * the packets are received.
877 checkif
= ip_checkinterface
&& (ipforwarding
== 0) &&
878 ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0)
880 && (args
.next_hop
== NULL
);
885 lck_mtx_lock(rt_mtx
);
886 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
) {
887 #define satosin(sa) ((struct sockaddr_in *)(sa))
889 if (IA_SIN(ia
)->sin_addr
.s_addr
== INADDR_ANY
) {
890 lck_mtx_unlock(rt_mtx
);
895 * If the address matches, verify that the packet
896 * arrived via the correct interface if checking is
899 if (IA_SIN(ia
)->sin_addr
.s_addr
== pkt_dst
.s_addr
&&
900 (!checkif
|| ia
->ia_ifp
== m
->m_pkthdr
.rcvif
)) {
901 lck_mtx_unlock(rt_mtx
);
905 * Only accept broadcast packets that arrive via the
906 * matching interface. Reception of forwarded directed
907 * broadcasts would be handled via ip_forward() and
908 * ether_output() with the loopback into the stack for
909 * SIMPLEX interfaces handled by ether_output().
911 if ((!checkif
|| ia
->ia_ifp
== m
->m_pkthdr
.rcvif
) &&
912 ia
->ia_ifp
&& ia
->ia_ifp
->if_flags
& IFF_BROADCAST
) {
913 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
915 lck_mtx_unlock(rt_mtx
);
918 if (ia
->ia_netbroadcast
.s_addr
== pkt_dst
.s_addr
) {
919 lck_mtx_unlock(rt_mtx
);
924 lck_mtx_unlock(rt_mtx
);
925 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
926 struct in_multi
*inm
;
930 * If we are acting as a multicast router, all
931 * incoming multicast packets are passed to the
932 * kernel-level multicast forwarding function.
933 * The packet is returned (relatively) intact; if
934 * ip_mforward() returns a non-zero value, the packet
935 * must be discarded, else it may be accepted below.
937 lck_mtx_lock(ip_mutex
);
939 ip_mforward(ip
, m
->m_pkthdr
.rcvif
, m
, 0) != 0) {
940 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_cantforward
);
942 lck_mtx_unlock(ip_mutex
);
947 * The process-level routing daemon needs to receive
948 * all multicast IGMP packets, whether or not this
949 * host belongs to their destination groups.
951 if (ip
->ip_p
== IPPROTO_IGMP
)
953 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_forward
);
955 #endif /* MROUTING */
957 * See if we belong to the destination multicast group on the
960 IN_LOOKUP_MULTI(ip
->ip_dst
, m
->m_pkthdr
.rcvif
, inm
);
962 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_notmember
);
968 if (ip
->ip_dst
.s_addr
== (u_long
)INADDR_BROADCAST
)
970 if (ip
->ip_dst
.s_addr
== INADDR_ANY
)
973 /* Allow DHCP/BootP responses through */
974 if (m
->m_pkthdr
.rcvif
!= NULL
975 && (m
->m_pkthdr
.rcvif
->if_eflags
& IFEF_AUTOCONFIGURING
)
976 && hlen
== sizeof(struct ip
)
977 && ip
->ip_p
== IPPROTO_UDP
) {
979 if (m
->m_len
< sizeof(struct udpiphdr
)
980 && (m
= m_pullup(m
, sizeof(struct udpiphdr
))) == 0) {
981 OSAddAtomic(1, (SInt32
*)&udpstat
.udps_hdrops
);
984 ui
= mtod(m
, struct udpiphdr
*);
985 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) {
988 ip
= mtod(m
, struct ip
*); /* in case it changed */
991 #if defined(NFAITH) && 0 < NFAITH
993 * FAITH(Firewall Aided Internet Translator)
995 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
== IFT_FAITH
) {
997 if (ip
->ip_p
== IPPROTO_TCP
|| ip
->ip_p
== IPPROTO_ICMP
)
1005 * Not for us; forward if possible and desirable.
1007 if (ipforwarding
== 0) {
1008 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_cantforward
);
1012 ip_forward(m
, 0, args
.next_hop
, &ipforward_rt
);
1014 ip_forward(m
, 0, NULL
, &ipforward_rt
);
1016 if (ipforward_rt
.ro_rt
!= NULL
) {
1017 rtfree(ipforward_rt
.ro_rt
);
1018 ipforward_rt
.ro_rt
= NULL
;
1025 * If offset or IP_MF are set, must reassemble.
1026 * Otherwise, nothing need be done.
1027 * (We could look in the reassembly queue to see
1028 * if the packet was previously fragmented,
1029 * but it's not worth the time; just let them time out.)
1031 if (ip
->ip_off
& (IP_MF
| IP_OFFMASK
| IP_RF
)) {
1033 /* If maxnipq is 0, never accept fragments. */
1036 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_fragments
);
1037 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_fragdropped
);
1042 * If we will exceed the number of fragments in queues, timeout the
1043 * oldest fragemented packet to make space.
1045 lck_mtx_lock(ip_mutex
);
1046 if (currentfrags
>= maxfrags
) {
1047 fp
= TAILQ_LAST(&ipq_list
, ipq_list
);
1048 OSAddAtomic(fp
->ipq_nfrags
, (SInt32
*)&ipstat
.ips_fragtimeout
);
1050 if (ip
->ip_id
== fp
->ipq_id
&&
1051 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
1052 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
1053 ip
->ip_p
== fp
->ipq_p
) {
1055 * If we match the fragment queue we were going to
1056 * discard, drop this packet too.
1058 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_fragdropped
);
1060 lck_mtx_unlock(ip_mutex
);
1067 sum
= IPREASS_HASH(ip
->ip_src
.s_addr
, ip
->ip_id
);
1069 * Look for queue of fragments
1072 for (fp
= ipq
[sum
].next
; fp
!= &ipq
[sum
]; fp
= fp
->next
)
1073 if (ip
->ip_id
== fp
->ipq_id
&&
1074 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
1075 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
1077 mac_ipq_label_compare(m
, fp
) &&
1079 ip
->ip_p
== fp
->ipq_p
)
1083 * Enforce upper bound on number of fragmented packets
1084 * for which we attempt reassembly;
1085 * If maxnipq is -1, accept all fragments without limitation.
1087 if ((nipq
> maxnipq
) && (maxnipq
> 0)) {
1089 * drop the oldest fragment before proceeding further
1091 fp
= TAILQ_LAST(&ipq_list
, ipq_list
);
1092 OSAddAtomic(fp
->ipq_nfrags
, (SInt32
*)&ipstat
.ips_fragtimeout
);
1100 * Adjust ip_len to not reflect header,
1101 * convert offset of this to bytes.
1104 if (ip
->ip_off
& IP_MF
) {
1106 * Make sure that fragments have a data length
1107 * that's a non-zero multiple of 8 bytes.
1109 if (ip
->ip_len
== 0 || (ip
->ip_len
& 0x7) != 0) {
1110 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_toosmall
);
1111 lck_mtx_unlock(ip_mutex
);
1114 m
->m_flags
|= M_FRAG
;
1116 /* Clear the flag in case packet comes from loopback */
1117 m
->m_flags
&= ~M_FRAG
;
1122 * Attempt reassembly; if it succeeds, proceed.
1123 * ip_reass() will return a different mbuf, and update
1124 * the divert info in div_info and args.divert_rule.
1126 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_fragments
);
1127 m
->m_pkthdr
.header
= ip
;
1130 fp
, &ipq
[sum
], &div_info
, &args
.divert_rule
);
1132 m
= ip_reass(m
, fp
, &ipq
[sum
]);
1135 lck_mtx_unlock(ip_mutex
);
1138 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_reassembled
);
1139 ip
= mtod(m
, struct ip
*);
1140 /* Get the header length of the reassembled packet */
1141 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1143 /* Restore original checksum before diverting packet */
1144 if (div_info
!= 0) {
1149 ip
->ip_sum
= in_cksum(m
, hlen
);
1155 lck_mtx_unlock(ip_mutex
);
1161 * Divert or tee packet to the divert protocol if required.
1163 * If div_info is zero then cookie should be too, so we shouldn't
1164 * need to clear them here. Assume divert_packet() does so also.
1166 if (div_info
!= 0) {
1167 struct mbuf
*clone
= NULL
;
1169 /* Clone packet if we're doing a 'tee' */
1170 if ((div_info
& IP_FW_PORT_TEE_FLAG
) != 0)
1171 clone
= m_dup(m
, M_DONTWAIT
);
1173 /* Restore packet header fields to original values */
1178 /* Deliver packet to divert input routine */
1179 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_delivered
);
1180 divert_packet(m
, 1, div_info
& 0xffff, args
.divert_rule
);
1182 /* If 'tee', continue with original packet */
1183 if (clone
== NULL
) {
1187 ip
= mtod(m
, struct ip
*);
1193 * enforce IPsec policy checking if we are seeing last header.
1194 * note that we do not visit this with protocols with pcb layer
1195 * code - like udp/tcp/raw ip.
1197 if (ipsec_bypass
== 0 && (ip_protox
[ip
->ip_p
]->pr_flags
& PR_LASTHDR
) != 0) {
1198 if (ipsec4_in_reject(m
, NULL
)) {
1199 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
1206 * Switch out to protocol's input routine.
1208 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_delivered
);
1211 if (args
.next_hop
&& ip
->ip_p
== IPPROTO_TCP
) {
1212 /* TCP needs IPFORWARD info if available */
1213 struct m_tag
*fwd_tag
;
1214 struct ip_fwd_tag
*ipfwd_tag
;
1216 fwd_tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
,
1217 sizeof(struct sockaddr_in
), M_NOWAIT
);
1218 if (fwd_tag
== NULL
) {
1222 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
1223 ipfwd_tag
->next_hop
= args
.next_hop
;
1225 m_tag_prepend(m
, fwd_tag
);
1227 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1228 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1231 /* TCP deals with its own locking */
1232 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1234 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1235 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1237 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1240 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1246 KERNEL_DEBUG(DBG_LAYER_END
, 0,0,0,0,0);
1251 * Take incoming datagram fragment and try to reassemble it into
1252 * whole datagram. If a chain for reassembly of this datagram already
1253 * exists, then it is given as fp; otherwise have to make a chain.
1255 * When IPDIVERT enabled, keep additional state with each packet that
1256 * tells us if we need to divert or tee the packet we're building.
1259 static struct mbuf
*
1261 ip_reass(struct mbuf
*m
, struct ipq
*fp
, struct ipq
*where
,
1264 #else /* IPDIVERT_44 */
1266 #endif /* IPDIVERT_44 */
1267 u_int16_t
*divcookie
)
1268 #else /* IPDIVERT */
1269 ip_reass(struct mbuf
*m
, struct ipq
*fp
, struct ipq
*where
)
1270 #endif /* IPDIVERT */
1272 struct ip
*ip
= mtod(m
, struct ip
*);
1273 struct mbuf
*p
= 0, *q
, *nq
;
1275 int hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1279 lck_mtx_assert(ip_mutex
, LCK_MTX_ASSERT_OWNED
);
1281 * Presence of header sizes in mbufs
1282 * would confuse code below.
1287 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1288 m
->m_pkthdr
.csum_flags
= 0;
1290 * If first fragment to arrive, create a reassembly queue.
1293 if ((t
= m_get(M_DONTWAIT
, MT_FTABLE
)) == NULL
)
1295 fp
= mtod(t
, struct ipq
*);
1297 if (mac_ipq_label_init(fp
, M_NOWAIT
) != 0) {
1302 mac_ipq_label_associate(m
, fp
);
1304 insque((void*)fp
, (void*)where
);
1307 fp
->ipq_ttl
= IPFRAGTTL
;
1308 fp
->ipq_p
= ip
->ip_p
;
1309 fp
->ipq_id
= ip
->ip_id
;
1310 fp
->ipq_src
= ip
->ip_src
;
1311 fp
->ipq_dst
= ip
->ip_dst
;
1313 m
->m_nextpkt
= NULL
;
1316 fp
->ipq_div_info
= 0;
1320 fp
->ipq_div_cookie
= 0;
1322 TAILQ_INSERT_HEAD(&ipq_list
, fp
, ipq_list
);
1327 mac_ipq_label_update(m
, fp
);
1331 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
1334 * Handle ECN by comparing this segment with the first one;
1335 * if CE is set, do not lose CE.
1336 * drop if CE and not-ECT are mixed for the same packet.
1338 ecn
= ip
->ip_tos
& IPTOS_ECN_MASK
;
1339 ecn0
= GETIP(fp
->ipq_frags
)->ip_tos
& IPTOS_ECN_MASK
;
1340 if (ecn
== IPTOS_ECN_CE
) {
1341 if (ecn0
== IPTOS_ECN_NOTECT
)
1343 if (ecn0
!= IPTOS_ECN_CE
)
1344 GETIP(fp
->ipq_frags
)->ip_tos
|= IPTOS_ECN_CE
;
1346 if (ecn
== IPTOS_ECN_NOTECT
&& ecn0
!= IPTOS_ECN_NOTECT
)
1350 * Find a segment which begins after this one does.
1352 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
)
1353 if (GETIP(q
)->ip_off
> ip
->ip_off
)
1357 * If there is a preceding segment, it may provide some of
1358 * our data already. If so, drop the data from the incoming
1359 * segment. If it provides all of our data, drop us, otherwise
1360 * stick new segment in the proper place.
1362 * If some of the data is dropped from the the preceding
1363 * segment, then it's checksum is invalidated.
1366 i
= GETIP(p
)->ip_off
+ GETIP(p
)->ip_len
- ip
->ip_off
;
1368 if (i
>= ip
->ip_len
)
1371 m
->m_pkthdr
.csum_flags
= 0;
1375 m
->m_nextpkt
= p
->m_nextpkt
;
1378 m
->m_nextpkt
= fp
->ipq_frags
;
1383 * While we overlap succeeding segments trim them or,
1384 * if they are completely covered, dequeue them.
1386 for (; q
!= NULL
&& ip
->ip_off
+ ip
->ip_len
> GETIP(q
)->ip_off
;
1388 i
= (ip
->ip_off
+ ip
->ip_len
) -
1390 if (i
< GETIP(q
)->ip_len
) {
1391 GETIP(q
)->ip_len
-= i
;
1392 GETIP(q
)->ip_off
+= i
;
1394 q
->m_pkthdr
.csum_flags
= 0;
1399 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_fragdropped
);
1409 * Transfer firewall instructions to the fragment structure.
1410 * Only trust info in the fragment at offset 0.
1412 if (ip
->ip_off
== 0) {
1414 fp
->ipq_div_info
= *divinfo
;
1416 fp
->ipq_divert
= *divinfo
;
1418 fp
->ipq_div_cookie
= *divcookie
;
1425 * Check for complete reassembly and perform frag per packet
1428 * Frag limiting is performed here so that the nth frag has
1429 * a chance to complete the packet before we drop the packet.
1430 * As a result, n+1 frags are actually allowed per packet, but
1431 * only n will ever be stored. (n = maxfragsperpacket.)
1435 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
) {
1436 if (GETIP(q
)->ip_off
!= next
) {
1437 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
1438 OSAddAtomic(fp
->ipq_nfrags
, (SInt32
*)&ipstat
.ips_fragdropped
);
1443 next
+= GETIP(q
)->ip_len
;
1445 /* Make sure the last packet didn't have the IP_MF flag */
1446 if (p
->m_flags
& M_FRAG
) {
1447 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
1448 OSAddAtomic(fp
->ipq_nfrags
, (SInt32
*)&ipstat
.ips_fragdropped
);
1455 * Reassembly is complete. Make sure the packet is a sane size.
1459 if (next
+ (IP_VHL_HL(ip
->ip_vhl
) << 2) > IP_MAXPACKET
) {
1460 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_toolong
);
1461 OSAddAtomic(fp
->ipq_nfrags
, (SInt32
*)&ipstat
.ips_fragdropped
);
1467 * Concatenate fragments.
1475 for (q
= nq
; q
!= NULL
; q
= nq
) {
1477 q
->m_nextpkt
= NULL
;
1478 if (q
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1479 m
->m_pkthdr
.csum_flags
= 0;
1481 m
->m_pkthdr
.csum_flags
&= q
->m_pkthdr
.csum_flags
;
1482 m
->m_pkthdr
.csum_data
+= q
->m_pkthdr
.csum_data
;
1489 * Extract firewall instructions from the fragment structure.
1492 *divinfo
= fp
->ipq_div_info
;
1494 *divinfo
= fp
->ipq_divert
;
1496 *divcookie
= fp
->ipq_div_cookie
;
1500 mac_mbuf_label_associate_ipq(fp
, m
);
1501 mac_ipq_label_destroy(fp
);
1504 * Create header for new ip packet by
1505 * modifying header of first packet;
1506 * dequeue and discard fragment reassembly header.
1507 * Make header visible.
1510 ip
->ip_src
= fp
->ipq_src
;
1511 ip
->ip_dst
= fp
->ipq_dst
;
1513 TAILQ_REMOVE(&ipq_list
, fp
, ipq_list
);
1514 currentfrags
-= fp
->ipq_nfrags
;
1516 (void) m_free(dtom(fp
));
1517 m
->m_len
+= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1518 m
->m_data
-= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1519 /* some debugging cruft by sklower, below, will go away soon */
1520 if (m
->m_flags
& M_PKTHDR
) { /* XXX this should be done elsewhere */
1522 for (t
= m
; t
; t
= t
->m_next
)
1524 m
->m_pkthdr
.len
= plen
;
1533 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_fragdropped
);
1543 * Free a fragment reassembly header and all
1544 * associated datagrams.
1547 ip_freef(struct ipq
*fp
)
1549 lck_mtx_assert(ip_mutex
, LCK_MTX_ASSERT_OWNED
);
1550 currentfrags
-= fp
->ipq_nfrags
;
1551 m_freem_list(fp
->ipq_frags
);
1553 TAILQ_REMOVE(&ipq_list
, fp
, ipq_list
);
1554 (void) m_free(dtom(fp
));
1559 * IP timer processing;
1560 * if a timer expires on a reassembly
1561 * queue, discard it.
1568 lck_mtx_lock(ip_mutex
);
1569 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1573 while (fp
!= &ipq
[i
]) {
1576 if (fp
->prev
->ipq_ttl
== 0) {
1577 OSAddAtomic(fp
->ipq_nfrags
, (SInt32
*)&ipstat
.ips_fragtimeout
);
1583 * If we are over the maximum number of fragments
1584 * (due to the limit being lowered), drain off
1585 * enough to get down to the new limit.
1587 if (maxnipq
>= 0 && nipq
> maxnipq
) {
1588 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1589 while (nipq
> maxnipq
&&
1590 (ipq
[i
].next
!= &ipq
[i
])) {
1591 OSAddAtomic(ipq
[i
].next
->ipq_nfrags
, (SInt32
*)&ipstat
.ips_fragdropped
);
1592 ip_freef(ipq
[i
].next
);
1597 lck_mtx_unlock(ip_mutex
);
1601 * Drain off all datagram fragments.
1608 lck_mtx_lock(ip_mutex
);
1609 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1610 while (ipq
[i
].next
!= &ipq
[i
]) {
1611 OSAddAtomic(ipq
[i
].next
->ipq_nfrags
, (SInt32
*)&ipstat
.ips_fragdropped
);
1612 ip_freef(ipq
[i
].next
);
1615 lck_mtx_unlock(ip_mutex
);
1620 * Do option processing on a datagram,
1621 * possibly discarding it if bad options are encountered,
1622 * or forwarding it if source-routed.
1623 * The pass argument is used when operating in the IPSTEALTH
1624 * mode to tell what options to process:
1625 * [LS]SRR (pass 0) or the others (pass 1).
1626 * The reason for as many as two passes is that when doing IPSTEALTH,
1627 * non-routing options should be processed only if the packet is for us.
1628 * Returns 1 if packet has been forwarded/freed,
1629 * 0 if the packet should be processed further.
1632 ip_dooptions(struct mbuf
*m
, __unused
int pass
, struct sockaddr_in
*next_hop
, struct route
*ipforward_rt
)
1634 struct ip
*ip
= mtod(m
, struct ip
*);
1636 struct ip_timestamp
*ipt
;
1637 struct in_ifaddr
*ia
;
1638 int opt
, optlen
, cnt
, off
, code
, type
= ICMP_PARAMPROB
, forward
= 0;
1639 struct in_addr
*sin
, dst
;
1643 cp
= (u_char
*)(ip
+ 1);
1644 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1645 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1646 opt
= cp
[IPOPT_OPTVAL
];
1647 if (opt
== IPOPT_EOL
)
1649 if (opt
== IPOPT_NOP
)
1652 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
)) {
1653 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1656 optlen
= cp
[IPOPT_OLEN
];
1657 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
) {
1658 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1668 * Source routing with record.
1669 * Find interface with current destination address.
1670 * If none on this machine then drop if strictly routed,
1671 * or do nothing if loosely routed.
1672 * Record interface address and bring up next address
1673 * component. If strictly routed make sure next
1674 * address is on directly accessible net.
1678 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1679 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1682 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1683 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1686 ipaddr
.sin_addr
= ip
->ip_dst
;
1687 ia
= (struct in_ifaddr
*)
1688 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
1690 if (opt
== IPOPT_SSRR
) {
1691 type
= ICMP_UNREACH
;
1692 code
= ICMP_UNREACH_SRCFAIL
;
1695 if (!ip_dosourceroute
)
1696 goto nosourcerouting
;
1698 * Loose routing, and not at next destination
1699 * yet; nothing to do except forward.
1704 ifafree(&ia
->ia_ifa
);
1707 off
--; /* 0 origin */
1708 if (off
> optlen
- (int)sizeof(struct in_addr
)) {
1710 * End of source route. Should be for us.
1712 if (!ip_acceptsourceroute
)
1713 goto nosourcerouting
;
1714 save_rte(cp
, ip
->ip_src
);
1718 if (!ip_dosourceroute
) {
1720 char buf
[MAX_IPv4_STR_LEN
];
1721 char buf2
[MAX_IPv4_STR_LEN
];
1723 * Acting as a router, so generate ICMP
1727 "attempted source route from %s to %s\n",
1728 inet_ntop(AF_INET
, &ip
->ip_src
, buf
, sizeof(buf
)),
1729 inet_ntop(AF_INET
, &ip
->ip_dst
, buf2
, sizeof(buf2
)));
1730 type
= ICMP_UNREACH
;
1731 code
= ICMP_UNREACH_SRCFAIL
;
1735 * Not acting as a router, so silently drop.
1737 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_cantforward
);
1744 * locate outgoing interface
1746 (void)memcpy(&ipaddr
.sin_addr
, cp
+ off
,
1747 sizeof(ipaddr
.sin_addr
));
1749 if (opt
== IPOPT_SSRR
) {
1750 #define INA struct in_ifaddr *
1751 #define SA struct sockaddr *
1752 if ((ia
= (INA
)ifa_ifwithdstaddr((SA
)&ipaddr
)) == 0) {
1753 ia
= (INA
)ifa_ifwithnet((SA
)&ipaddr
);
1756 ia
= ip_rtaddr(ipaddr
.sin_addr
, ipforward_rt
);
1759 type
= ICMP_UNREACH
;
1760 code
= ICMP_UNREACH_SRCFAIL
;
1763 ip
->ip_dst
= ipaddr
.sin_addr
;
1764 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1765 sizeof(struct in_addr
));
1766 ifafree(&ia
->ia_ifa
);
1768 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1770 * Let ip_intr's mcast routing check handle mcast pkts
1772 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
1776 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1777 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1780 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1781 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1785 * If no space remains, ignore.
1787 off
--; /* 0 origin */
1788 if (off
> optlen
- (int)sizeof(struct in_addr
))
1790 (void)memcpy(&ipaddr
.sin_addr
, &ip
->ip_dst
,
1791 sizeof(ipaddr
.sin_addr
));
1793 * locate outgoing interface; if we're the destination,
1794 * use the incoming interface (should be same).
1796 if ((ia
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0) {
1797 if ((ia
= ip_rtaddr(ipaddr
.sin_addr
, ipforward_rt
)) == 0) {
1798 type
= ICMP_UNREACH
;
1799 code
= ICMP_UNREACH_HOST
;
1803 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1804 sizeof(struct in_addr
));
1805 ifafree(&ia
->ia_ifa
);
1807 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1811 code
= cp
- (u_char
*)ip
;
1812 ipt
= (struct ip_timestamp
*)cp
;
1813 if (ipt
->ipt_len
< 4 || ipt
->ipt_len
> 40) {
1814 code
= (u_char
*)&ipt
->ipt_len
- (u_char
*)ip
;
1817 if (ipt
->ipt_ptr
< 5) {
1818 code
= (u_char
*)&ipt
->ipt_ptr
- (u_char
*)ip
;
1822 ipt
->ipt_len
- (int)sizeof(int32_t)) {
1823 if (++ipt
->ipt_oflw
== 0) {
1824 code
= (u_char
*)&ipt
->ipt_ptr
-
1830 sin
= (struct in_addr
*)(cp
+ ipt
->ipt_ptr
- 1);
1831 switch (ipt
->ipt_flg
) {
1833 case IPOPT_TS_TSONLY
:
1836 case IPOPT_TS_TSANDADDR
:
1837 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1838 sizeof(struct in_addr
) > ipt
->ipt_len
) {
1839 code
= (u_char
*)&ipt
->ipt_ptr
-
1843 ipaddr
.sin_addr
= dst
;
1844 ia
= (INA
)ifaof_ifpforaddr((SA
)&ipaddr
,
1848 (void)memcpy(sin
, &IA_SIN(ia
)->sin_addr
,
1849 sizeof(struct in_addr
));
1850 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
1851 ifafree(&ia
->ia_ifa
);
1855 case IPOPT_TS_PRESPEC
:
1856 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1857 sizeof(struct in_addr
) > ipt
->ipt_len
) {
1858 code
= (u_char
*)&ipt
->ipt_ptr
-
1862 (void)memcpy(&ipaddr
.sin_addr
, sin
,
1863 sizeof(struct in_addr
));
1864 if ((ia
= (struct in_ifaddr
*)ifa_ifwithaddr((SA
)&ipaddr
)) == 0)
1866 ifafree(&ia
->ia_ifa
);
1868 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
1872 /* XXX can't take &ipt->ipt_flg */
1873 code
= (u_char
*)&ipt
->ipt_ptr
-
1878 (void)memcpy(cp
+ ipt
->ipt_ptr
- 1, &ntime
,
1880 ipt
->ipt_ptr
+= sizeof(n_time
);
1883 if (forward
&& ipforwarding
) {
1884 ip_forward(m
, 1, next_hop
, ipforward_rt
);
1885 if (ipforward_rt
->ro_rt
!= NULL
) {
1886 rtfree(ipforward_rt
->ro_rt
);
1887 ipforward_rt
->ro_rt
= NULL
;
1893 ip
->ip_len
-= IP_VHL_HL(ip
->ip_vhl
) << 2; /* XXX icmp_error adds in hdr length */
1894 icmp_error(m
, type
, code
, 0, 0);
1895 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_badoptions
);
1900 * Given address of next destination (final or next hop),
1901 * return internet address info of interface to be used to get there.
1904 ip_rtaddr(struct in_addr dst
, struct route
*rt
)
1906 struct sockaddr_in
*sin
;
1908 sin
= (struct sockaddr_in
*)&rt
->ro_dst
;
1910 lck_mtx_lock(rt_mtx
);
1911 if (rt
->ro_rt
== 0 || dst
.s_addr
!= sin
->sin_addr
.s_addr
||
1912 rt
->ro_rt
->generation_id
!= route_generation
) {
1914 rtfree_locked(rt
->ro_rt
);
1917 sin
->sin_family
= AF_INET
;
1918 sin
->sin_len
= sizeof(*sin
);
1919 sin
->sin_addr
= dst
;
1921 rtalloc_ign_locked(rt
, RTF_PRCLONING
);
1923 if (rt
->ro_rt
== 0) {
1924 lck_mtx_unlock(rt_mtx
);
1925 return ((struct in_ifaddr
*)0);
1928 if (rt
->ro_rt
->rt_ifa
)
1929 ifaref(rt
->ro_rt
->rt_ifa
);
1930 lck_mtx_unlock(rt_mtx
);
1931 return ((struct in_ifaddr
*) rt
->ro_rt
->rt_ifa
);
1935 * Save incoming source route for use in replies,
1936 * to be picked up later by ip_srcroute if the receiver is interested.
1939 save_rte(u_char
*option
, struct in_addr dst
)
1943 olen
= option
[IPOPT_OLEN
];
1946 printf("save_rte: olen %d\n", olen
);
1948 if (olen
> sizeof(ip_srcrt
) - (1 + sizeof(dst
)))
1950 bcopy(option
, ip_srcrt
.srcopt
, olen
);
1951 ip_nhops
= (olen
- IPOPT_OFFSET
- 1) / sizeof(struct in_addr
);
1956 * Retrieve incoming source route for use in replies,
1957 * in the same form used by setsockopt.
1958 * The first hop is placed before the options, will be removed later.
1963 struct in_addr
*p
, *q
;
1967 return ((struct mbuf
*)0);
1968 m
= m_get(M_DONTWAIT
, MT_HEADER
);
1970 return ((struct mbuf
*)0);
1972 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1974 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1975 m
->m_len
= ip_nhops
* sizeof(struct in_addr
) + sizeof(struct in_addr
) +
1979 printf("ip_srcroute: nhops %d mlen %d", ip_nhops
, m
->m_len
);
1983 * First save first hop for return route
1985 p
= &ip_srcrt
.route
[ip_nhops
- 1];
1986 *(mtod(m
, struct in_addr
*)) = *p
--;
1989 printf(" hops %lx", (u_long
)ntohl(mtod(m
, struct in_addr
*)->s_addr
));
1993 * Copy option fields and padding (nop) to mbuf.
1995 ip_srcrt
.nop
= IPOPT_NOP
;
1996 ip_srcrt
.srcopt
[IPOPT_OFFSET
] = IPOPT_MINOFF
;
1997 (void)memcpy(mtod(m
, caddr_t
) + sizeof(struct in_addr
),
1998 &ip_srcrt
.nop
, OPTSIZ
);
1999 q
= (struct in_addr
*)(mtod(m
, caddr_t
) +
2000 sizeof(struct in_addr
) + OPTSIZ
);
2003 * Record return path as an IP source route,
2004 * reversing the path (pointers are now aligned).
2006 while (p
>= ip_srcrt
.route
) {
2009 printf(" %lx", (u_long
)ntohl(q
->s_addr
));
2014 * Last hop goes to final destination.
2019 printf(" %lx\n", (u_long
)ntohl(q
->s_addr
));
2025 * Strip out IP options, at higher
2026 * level protocol in the kernel.
2027 * Second argument is buffer to which options
2028 * will be moved, and return value is their length.
2029 * XXX should be deleted; last arg currently ignored.
2032 ip_stripoptions(struct mbuf
*m
, __unused
struct mbuf
*mopt
)
2035 struct ip
*ip
= mtod(m
, struct ip
*);
2039 olen
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
2040 opts
= (caddr_t
)(ip
+ 1);
2041 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
2042 bcopy(opts
+ olen
, opts
, (unsigned)i
);
2044 if (m
->m_flags
& M_PKTHDR
)
2045 m
->m_pkthdr
.len
-= olen
;
2046 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
2049 u_char inetctlerrmap
[PRC_NCMDS
] = {
2051 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
2052 ENETUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
2053 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
2055 ENOPROTOOPT
, ECONNREFUSED
2059 * Forward a packet. If some error occurs return the sender
2060 * an icmp packet. Note we can't always generate a meaningful
2061 * icmp message because icmp doesn't have a large enough repertoire
2062 * of codes and types.
2064 * If not forwarding, just drop the packet. This could be confusing
2065 * if ipforwarding was zero but some routing protocol was advancing
2066 * us as a gateway to somewhere. However, we must let the routing
2067 * protocol deal with that.
2069 * The srcrt parameter indicates whether the packet is being forwarded
2070 * via a source route.
2073 ip_forward(struct mbuf
*m
, int srcrt
, struct sockaddr_in
*next_hop
, struct route
*ipforward_rt
)
2075 struct ip
*ip
= mtod(m
, struct ip
*);
2076 struct sockaddr_in
*sin
;
2078 int error
, type
= 0, code
= 0;
2081 struct in_addr pkt_dst
;
2082 struct ifnet
*destifp
;
2083 struct ifnet
*rcvif
= m
->m_pkthdr
.rcvif
;
2085 struct ifnet dummyifp
;
2088 m
->m_pkthdr
.rcvif
= NULL
;
2092 * Cache the destination address of the packet; this may be
2093 * changed by use of 'ipfw fwd'.
2095 pkt_dst
= next_hop
? next_hop
->sin_addr
: ip
->ip_dst
;
2099 printf("forward: src %lx dst %lx ttl %x\n",
2100 (u_long
)ip
->ip_src
.s_addr
, (u_long
)pkt_dst
.s_addr
,
2105 if (m
->m_flags
& (M_BCAST
|M_MCAST
) || in_canforward(pkt_dst
) == 0) {
2106 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_cantforward
);
2113 if (ip
->ip_ttl
<= IPTTLDEC
) {
2114 icmp_error(m
, ICMP_TIMXCEED
, ICMP_TIMXCEED_INTRANS
,
2122 sin
= (struct sockaddr_in
*)&ipforward_rt
->ro_dst
;
2123 if ((rt
= ipforward_rt
->ro_rt
) == 0 ||
2124 pkt_dst
.s_addr
!= sin
->sin_addr
.s_addr
||
2125 ipforward_rt
->ro_rt
->generation_id
!= route_generation
) {
2126 if (ipforward_rt
->ro_rt
) {
2127 rtfree(ipforward_rt
->ro_rt
);
2128 ipforward_rt
->ro_rt
= 0;
2130 sin
->sin_family
= AF_INET
;
2131 sin
->sin_len
= sizeof(*sin
);
2132 sin
->sin_addr
= pkt_dst
;
2134 rtalloc_ign(ipforward_rt
, RTF_PRCLONING
);
2135 if (ipforward_rt
->ro_rt
== 0) {
2136 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_HOST
, dest
, 0);
2139 rt
= ipforward_rt
->ro_rt
;
2143 * Save the IP header and at most 8 bytes of the payload,
2144 * in case we need to generate an ICMP message to the src.
2146 * We don't use m_copy() because it might return a reference
2147 * to a shared cluster. Both this function and ip_output()
2148 * assume exclusive access to the IP header in `m', so any
2149 * data in a cluster may change before we reach icmp_error().
2151 MGET(mcopy
, M_DONTWAIT
, m
->m_type
);
2152 if (mcopy
!= NULL
) {
2153 M_COPY_PKTHDR(mcopy
, m
);
2154 mcopy
->m_len
= imin((IP_VHL_HL(ip
->ip_vhl
) << 2) + 8,
2156 m_copydata(m
, 0, mcopy
->m_len
, mtod(mcopy
, caddr_t
));
2162 ip
->ip_ttl
-= IPTTLDEC
;
2168 * If forwarding packet using same interface that it came in on,
2169 * perhaps should send a redirect to sender to shortcut a hop.
2170 * Only send redirect if source is sending directly to us,
2171 * and if packet was not source routed (or has any options).
2172 * Also, don't send redirect if forwarding using a default route
2173 * or a route modified by a redirect.
2175 #define satosin(sa) ((struct sockaddr_in *)(sa))
2176 if (rt
->rt_ifp
== m
->m_pkthdr
.rcvif
&&
2177 (rt
->rt_flags
& (RTF_DYNAMIC
|RTF_MODIFIED
)) == 0 &&
2178 satosin(rt_key(rt
))->sin_addr
.s_addr
!= 0 &&
2179 ipsendredirects
&& !srcrt
) {
2180 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
2181 u_long src
= ntohl(ip
->ip_src
.s_addr
);
2184 (src
& RTA(rt
)->ia_subnetmask
) == RTA(rt
)->ia_subnet
) {
2185 if (rt
->rt_flags
& RTF_GATEWAY
)
2186 dest
= satosin(rt
->rt_gateway
)->sin_addr
.s_addr
;
2188 dest
= pkt_dst
.s_addr
;
2189 /* Router requirements says to only send host redirects */
2190 type
= ICMP_REDIRECT
;
2191 code
= ICMP_REDIRECT_HOST
;
2194 printf("redirect (%d) to %lx\n", code
, (u_long
)dest
);
2201 /* Pass IPFORWARD info if available */
2203 struct ip_fwd_tag
*ipfwd_tag
;
2205 tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
,
2206 sizeof(struct sockaddr_in
), M_NOWAIT
);
2213 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
2214 ipfwd_tag
->next_hop
= next_hop
;
2216 m_tag_prepend(m
, tag
);
2218 error
= ip_output_list(m
, 0, (struct mbuf
*)0, ipforward_rt
,
2219 IP_FORWARDING
, 0, NULL
);
2222 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_cantforward
);
2224 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_forward
);
2226 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_redirectsent
);
2229 ipflow_create(ipforward_rt
, mcopy
);
2241 case 0: /* forwarded, but need redirect */
2242 /* type, code set above */
2245 case ENETUNREACH
: /* shouldn't happen, checked above */
2250 type
= ICMP_UNREACH
;
2251 code
= ICMP_UNREACH_HOST
;
2255 type
= ICMP_UNREACH
;
2256 code
= ICMP_UNREACH_NEEDFRAG
;
2258 if (ipforward_rt
->ro_rt
)
2259 destifp
= ipforward_rt
->ro_rt
->rt_ifp
;
2262 * If the packet is routed over IPsec tunnel, tell the
2263 * originator the tunnel MTU.
2264 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
2267 if (ipforward_rt
->ro_rt
) {
2268 struct secpolicy
*sp
= NULL
;
2274 destifp
= ipforward_rt
->ro_rt
->rt_ifp
;
2275 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_cantfrag
);
2278 sp
= ipsec4_getpolicybyaddr(mcopy
,
2284 destifp
= ipforward_rt
->ro_rt
->rt_ifp
;
2286 /* count IPsec header size */
2287 ipsechdr
= ipsec_hdrsiz(sp
);
2290 * find the correct route for outer IPv4
2291 * header, compute tunnel MTU.
2294 * The "dummyifp" code relies upon the fact
2295 * that icmp_error() touches only ifp->if_mtu.
2300 if (sp
->req
!= NULL
) {
2301 if (sp
->req
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2302 struct secasindex saidx
;
2304 struct secasvar
*sav
;
2306 ipm
= mtod(mcopy
, struct ip
*);
2307 bcopy(&sp
->req
->saidx
, &saidx
, sizeof(saidx
));
2308 saidx
.mode
= sp
->req
->saidx
.mode
;
2309 saidx
.reqid
= sp
->req
->saidx
.reqid
;
2310 sin
= (struct sockaddr_in
*)&saidx
.src
;
2311 if (sin
->sin_len
== 0) {
2312 sin
->sin_len
= sizeof(*sin
);
2313 sin
->sin_family
= AF_INET
;
2314 sin
->sin_port
= IPSEC_PORT_ANY
;
2315 bcopy(&ipm
->ip_src
, &sin
->sin_addr
,
2316 sizeof(sin
->sin_addr
));
2318 sin
= (struct sockaddr_in
*)&saidx
.dst
;
2319 if (sin
->sin_len
== 0) {
2320 sin
->sin_len
= sizeof(*sin
);
2321 sin
->sin_family
= AF_INET
;
2322 sin
->sin_port
= IPSEC_PORT_ANY
;
2323 bcopy(&ipm
->ip_dst
, &sin
->sin_addr
,
2324 sizeof(sin
->sin_addr
));
2326 sav
= key_allocsa_policy(&saidx
);
2328 if (sav
->sah
!= NULL
) {
2329 ro
= &sav
->sah
->sa_route
;
2330 if (ro
->ro_rt
&& ro
->ro_rt
->rt_ifp
) {
2332 ro
->ro_rt
->rt_ifp
->if_mtu
;
2333 dummyifp
.if_mtu
-= ipsechdr
;
2334 destifp
= &dummyifp
;
2337 key_freesav(sav
, KEY_SADB_UNLOCKED
);
2341 key_freesp(sp
, KEY_SADB_UNLOCKED
);
2345 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_cantfrag
);
2349 type
= ICMP_SOURCEQUENCH
;
2353 case EACCES
: /* ipfw denied packet */
2357 icmp_error(mcopy
, type
, code
, dest
, destifp
);
2367 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
2371 *mp
= sbcreatecontrol((caddr_t
) &tv
, sizeof(tv
),
2372 SCM_TIMESTAMP
, SOL_SOCKET
);
2374 mp
= &(*mp
)->m_next
;
2376 if (inp
->inp_flags
& INP_RECVDSTADDR
) {
2377 *mp
= sbcreatecontrol((caddr_t
) &ip
->ip_dst
,
2378 sizeof(struct in_addr
), IP_RECVDSTADDR
, IPPROTO_IP
);
2380 mp
= &(*mp
)->m_next
;
2384 * Moving these out of udp_input() made them even more broken
2385 * than they already were.
2387 /* options were tossed already */
2388 if (inp
->inp_flags
& INP_RECVOPTS
) {
2389 *mp
= sbcreatecontrol((caddr_t
) opts_deleted_above
,
2390 sizeof(struct in_addr
), IP_RECVOPTS
, IPPROTO_IP
);
2392 mp
= &(*mp
)->m_next
;
2394 /* ip_srcroute doesn't do what we want here, need to fix */
2395 if (inp
->inp_flags
& INP_RECVRETOPTS
) {
2396 *mp
= sbcreatecontrol((caddr_t
) ip_srcroute(),
2397 sizeof(struct in_addr
), IP_RECVRETOPTS
, IPPROTO_IP
);
2399 mp
= &(*mp
)->m_next
;
2402 if (inp
->inp_flags
& INP_RECVIF
) {
2405 struct sockaddr_dl sdl
;
2408 struct sockaddr_dl
*sdp
;
2409 struct sockaddr_dl
*sdl2
= &sdlbuf
.sdl
;
2411 ifnet_head_lock_shared();
2412 if (((ifp
= m
->m_pkthdr
.rcvif
))
2413 && ( ifp
->if_index
&& (ifp
->if_index
<= if_index
))) {
2414 struct ifaddr
*ifa
= ifnet_addrs
[ifp
->if_index
- 1];
2416 if (!ifa
|| !ifa
->ifa_addr
)
2419 sdp
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
2421 * Change our mind and don't try copy.
2423 if ((sdp
->sdl_family
!= AF_LINK
)
2424 || (sdp
->sdl_len
> sizeof(sdlbuf
))) {
2427 bcopy(sdp
, sdl2
, sdp
->sdl_len
);
2431 = offsetof(struct sockaddr_dl
, sdl_data
[0]);
2432 sdl2
->sdl_family
= AF_LINK
;
2433 sdl2
->sdl_index
= 0;
2434 sdl2
->sdl_nlen
= sdl2
->sdl_alen
= sdl2
->sdl_slen
= 0;
2437 *mp
= sbcreatecontrol((caddr_t
) sdl2
, sdl2
->sdl_len
,
2438 IP_RECVIF
, IPPROTO_IP
);
2440 mp
= &(*mp
)->m_next
;
2442 if (inp
->inp_flags
& INP_RECVTTL
) {
2443 *mp
= sbcreatecontrol((caddr_t
)&ip
->ip_ttl
, sizeof(ip
->ip_ttl
), IP_RECVTTL
, IPPROTO_IP
);
2444 if (*mp
) mp
= &(*mp
)->m_next
;
2449 ip_rsvp_init(struct socket
*so
)
2451 if (so
->so_type
!= SOCK_RAW
||
2452 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2455 if (ip_rsvpd
!= NULL
)
2460 * This may seem silly, but we need to be sure we don't over-increment
2461 * the RSVP counter, in case something slips up.
2476 * This may seem silly, but we need to be sure we don't over-decrement
2477 * the RSVP counter, in case something slips up.