2 * Copyright (c) 2000-2013 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
63 * NOTICE: This file was modified by SPARTA, Inc. in 2007 to introduce
64 * support for mandatory and extensible security protections. This notice
65 * is included in support of clause 2.2 (b) of the Apple Public License,
71 #include <sys/param.h>
72 #include <sys/systm.h>
74 #include <sys/malloc.h>
75 #include <sys/domain.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
79 #include <sys/kernel.h>
80 #include <sys/syslog.h>
81 #include <sys/sysctl.h>
82 #include <sys/mcache.h>
83 #include <sys/socketvar.h>
84 #include <sys/kdebug.h>
85 #include <mach/mach_time.h>
88 #include <machine/endian.h>
89 #include <dev/random/randomdev.h>
91 #include <kern/queue.h>
92 #include <kern/locks.h>
93 #include <libkern/OSAtomic.h>
95 #include <pexpert/pexpert.h>
98 #include <net/if_var.h>
99 #include <net/if_dl.h>
100 #include <net/route.h>
101 #include <net/kpi_protocol.h>
102 #include <net/ntstat.h>
103 #include <net/dlil.h>
104 #include <net/classq/classq.h>
106 #include <net/pfvar.h>
109 #include <netinet/in.h>
110 #include <netinet/in_systm.h>
111 #include <netinet/in_var.h>
112 #include <netinet/in_arp.h>
113 #include <netinet/ip.h>
114 #include <netinet/in_pcb.h>
115 #include <netinet/ip_var.h>
116 #include <netinet/ip_icmp.h>
117 #include <netinet/ip_fw.h>
118 #include <netinet/ip_divert.h>
119 #include <netinet/kpi_ipfilter_var.h>
120 #include <netinet/udp.h>
121 #include <netinet/udp_var.h>
122 #include <netinet/bootp.h>
123 #include <netinet/lro_ext.h>
126 #include <netinet/ip_dummynet.h>
127 #endif /* DUMMYNET */
130 #include <security/mac_framework.h>
131 #endif /* CONFIG_MACF_NET */
134 #include <netinet6/ipsec.h>
135 #include <netkey/key.h>
138 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 0)
139 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 2)
140 #define DBG_FNC_IP_INPUT NETDBG_CODE(DBG_NETIP, (2 << 8))
143 extern int ipsec_bypass
;
144 extern lck_mtx_t
*sadb_mutex
;
146 lck_grp_t
*sadb_stat_mutex_grp
;
147 lck_grp_attr_t
*sadb_stat_mutex_grp_attr
;
148 lck_attr_t
*sadb_stat_mutex_attr
;
149 decl_lck_mtx_data(, sadb_stat_mutex_data
);
150 lck_mtx_t
*sadb_stat_mutex
= &sadb_stat_mutex_data
;
155 static int frag_timeout_run
; /* frag timer is scheduled to run */
156 static void frag_timeout(void *);
157 static void frag_sched_timeout(void);
159 static struct ipq
*ipq_alloc(int);
160 static void ipq_free(struct ipq
*);
161 static void ipq_updateparams(void);
163 decl_lck_mtx_data(static, ipqlock
);
164 static lck_attr_t
*ipqlock_attr
;
165 static lck_grp_t
*ipqlock_grp
;
166 static lck_grp_attr_t
*ipqlock_grp_attr
;
168 /* Packet reassembly stuff */
169 #define IPREASS_NHASH_LOG2 6
170 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
171 #define IPREASS_HMASK (IPREASS_NHASH - 1)
172 #define IPREASS_HASH(x, y) \
173 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
175 /* IP fragment reassembly queues (protected by ipqlock) */
176 static TAILQ_HEAD(ipqhead
, ipq
) ipq
[IPREASS_NHASH
]; /* ip reassembly queues */
177 static int maxnipq
; /* max packets in reass queues */
178 static u_int32_t maxfragsperpacket
; /* max frags/packet in reass queues */
179 static u_int32_t nipq
; /* # of packets in reass queues */
180 static u_int32_t ipq_limit
; /* ipq allocation limit */
181 static u_int32_t ipq_count
; /* current # of allocated ipq's */
183 static int sysctl_ipforwarding SYSCTL_HANDLER_ARGS
;
184 static int sysctl_maxnipq SYSCTL_HANDLER_ARGS
;
185 static int sysctl_maxfragsperpacket SYSCTL_HANDLER_ARGS
;
187 int ipforwarding
= 0;
188 SYSCTL_PROC(_net_inet_ip
, IPCTL_FORWARDING
, forwarding
,
189 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &ipforwarding
, 0,
190 sysctl_ipforwarding
, "I", "Enable IP forwarding between interfaces");
192 static int ipsendredirects
= 1; /* XXX */
193 SYSCTL_INT(_net_inet_ip
, IPCTL_SENDREDIRECTS
, redirect
,
194 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ipsendredirects
, 0,
195 "Enable sending IP redirects");
197 int ip_defttl
= IPDEFTTL
;
198 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFTTL
, ttl
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
199 &ip_defttl
, 0, "Maximum TTL on IP packets");
201 static int ip_dosourceroute
= 0;
202 SYSCTL_INT(_net_inet_ip
, IPCTL_SOURCEROUTE
, sourceroute
,
203 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_dosourceroute
, 0,
204 "Enable forwarding source routed IP packets");
206 static int ip_acceptsourceroute
= 0;
207 SYSCTL_INT(_net_inet_ip
, IPCTL_ACCEPTSOURCEROUTE
, accept_sourceroute
,
208 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_acceptsourceroute
, 0,
209 "Enable accepting source routed IP packets");
211 static int ip_sendsourcequench
= 0;
212 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, sendsourcequench
,
213 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_sendsourcequench
, 0,
214 "Enable the transmission of source quench packets");
216 SYSCTL_PROC(_net_inet_ip
, OID_AUTO
, maxfragpackets
,
217 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &maxnipq
, 0, sysctl_maxnipq
,
218 "I", "Maximum number of IPv4 fragment reassembly queue entries");
220 SYSCTL_UINT(_net_inet_ip
, OID_AUTO
, fragpackets
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
221 &nipq
, 0, "Current number of IPv4 fragment reassembly queue entries");
223 SYSCTL_PROC(_net_inet_ip
, OID_AUTO
, maxfragsperpacket
,
224 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &maxfragsperpacket
, 0,
225 sysctl_maxfragsperpacket
, "I",
226 "Maximum number of IPv4 fragments allowed per packet");
228 int ip_doscopedroute
= 1;
229 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, scopedroute
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
230 &ip_doscopedroute
, 0, "Enable IPv4 scoped routing");
232 static uint32_t ip_adj_clear_hwcksum
= 0;
233 SYSCTL_UINT(_net_inet_ip
, OID_AUTO
, adj_clear_hwcksum
,
234 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_adj_clear_hwcksum
, 0,
235 "Invalidate hwcksum info when adjusting length");
238 * XXX - Setting ip_checkinterface mostly implements the receive side of
239 * the Strong ES model described in RFC 1122, but since the routing table
240 * and transmit implementation do not implement the Strong ES model,
241 * setting this to 1 results in an odd hybrid.
243 * XXX - ip_checkinterface currently must be disabled if you use ipnat
244 * to translate the destination address to another local interface.
246 * XXX - ip_checkinterface must be disabled if you add IP aliases
247 * to the loopback interface instead of the interface where the
248 * packets for those addresses are received.
250 static int ip_checkinterface
= 0;
251 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, check_interface
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
252 &ip_checkinterface
, 0, "Verify packet arrives on correct interface");
255 static int ipprintfs
= 0;
258 struct protosw
*ip_protox
[IPPROTO_MAX
];
260 static lck_grp_attr_t
*in_ifaddr_rwlock_grp_attr
;
261 static lck_grp_t
*in_ifaddr_rwlock_grp
;
262 static lck_attr_t
*in_ifaddr_rwlock_attr
;
263 decl_lck_rw_data(, in_ifaddr_rwlock_data
);
264 lck_rw_t
*in_ifaddr_rwlock
= &in_ifaddr_rwlock_data
;
266 /* Protected by in_ifaddr_rwlock */
267 struct in_ifaddrhead in_ifaddrhead
; /* first inet address */
268 struct in_ifaddrhashhead
*in_ifaddrhashtbl
; /* inet addr hash table */
270 #define INADDR_NHASH 61
271 static u_int32_t inaddr_nhash
; /* hash table size */
272 static u_int32_t inaddr_hashp
; /* next largest prime */
274 static int ip_getstat SYSCTL_HANDLER_ARGS
;
275 struct ipstat ipstat
;
276 SYSCTL_PROC(_net_inet_ip
, IPCTL_STATS
, stats
,
277 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
278 0, 0, ip_getstat
, "S,ipstat",
279 "IP statistics (struct ipstat, netinet/ip_var.h)");
282 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFMTU
, mtu
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
283 &ip_mtu
, 0, "Default MTU");
284 #endif /* IPCTL_DEFMTU */
287 static int ipstealth
= 0;
288 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, stealth
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
290 #endif /* IPSTEALTH */
294 ip_fw_chk_t
*ip_fw_chk_ptr
;
298 #endif /* IPFIREWALL */
301 ip_dn_io_t
*ip_dn_io_ptr
;
302 #endif /* DUMMYNET */
304 SYSCTL_NODE(_net_inet_ip
, OID_AUTO
, linklocal
,
305 CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "link local");
307 struct ip_linklocal_stat ip_linklocal_stat
;
308 SYSCTL_STRUCT(_net_inet_ip_linklocal
, OID_AUTO
, stat
,
309 CTLFLAG_RD
| CTLFLAG_LOCKED
, &ip_linklocal_stat
, ip_linklocal_stat
,
310 "Number of link local packets with TTL less than 255");
312 SYSCTL_NODE(_net_inet_ip_linklocal
, OID_AUTO
, in
,
313 CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "link local input");
315 int ip_linklocal_in_allowbadttl
= 1;
316 SYSCTL_INT(_net_inet_ip_linklocal_in
, OID_AUTO
, allowbadttl
,
317 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_linklocal_in_allowbadttl
, 0,
318 "Allow incoming link local packets with TTL less than 255");
322 * We need to save the IP options in case a protocol wants to respond
323 * to an incoming packet over the same route if the packet got here
324 * using IP source routing. This allows connection establishment and
325 * maintenance when the remote end is on a network that is not known
328 static int ip_nhops
= 0;
329 static struct ip_srcrt
{
330 struct in_addr dst
; /* final destination */
331 char nop
; /* one NOP to align */
332 char srcopt
[IPOPT_OFFSET
+ 1]; /* OPTVAL, OLEN and OFFSET */
333 struct in_addr route
[MAX_IPOPTLEN
/ sizeof (struct in_addr
)];
336 static void in_ifaddrhashtbl_init(void);
337 static void save_rte(u_char
*, struct in_addr
);
338 static int ip_dooptions(struct mbuf
*, int, struct sockaddr_in
*);
339 static void ip_forward(struct mbuf
*, int, struct sockaddr_in
*);
340 static void frag_freef(struct ipqhead
*, struct ipq
*);
343 static struct mbuf
*ip_reass(struct mbuf
*, u_int32_t
*, u_int16_t
*);
344 #else /* !IPDIVERT_44 */
345 static struct mbuf
*ip_reass(struct mbuf
*, u_int16_t
*, u_int16_t
*);
346 #endif /* !IPDIVERT_44 */
347 #else /* !IPDIVERT */
348 static struct mbuf
*ip_reass(struct mbuf
*);
349 #endif /* !IPDIVERT */
350 static void ip_fwd_route_copyout(struct ifnet
*, struct route
*);
351 static void ip_fwd_route_copyin(struct ifnet
*, struct route
*);
352 static inline u_short
ip_cksum(struct mbuf
*, int);
354 int ip_use_randomid
= 1;
355 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, random_id
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
356 &ip_use_randomid
, 0, "Randomize IP packets IDs");
359 * On platforms which require strict alignment (currently for anything but
360 * i386 or x86_64), check if the IP header pointer is 32-bit aligned; if not,
361 * copy the contents of the mbuf chain into a new chain, and free the original
362 * one. Create some head room in the first mbuf of the new chain, in case
363 * it's needed later on.
365 #if defined(__i386__) || defined(__x86_64__)
366 #define IP_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { } while (0)
367 #else /* !__i386__ && !__x86_64__ */
368 #define IP_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { \
369 if (!IP_HDR_ALIGNED_P(mtod(_m, caddr_t))) { \
371 struct ifnet *__ifp = (_ifp); \
372 atomic_add_64(&(__ifp)->if_alignerrs, 1); \
373 if (((_m)->m_flags & M_PKTHDR) && \
374 (_m)->m_pkthdr.pkt_hdr != NULL) \
375 (_m)->m_pkthdr.pkt_hdr = NULL; \
376 _n = m_defrag_offset(_m, max_linkhdr, M_NOWAIT); \
378 atomic_add_32(&ipstat.ips_toosmall, 1); \
383 VERIFY(_n != (_m)); \
388 #endif /* !__i386__ && !__x86_64__ */
391 * GRE input handler function, settable via ip_gre_register_input() for PPTP.
393 static gre_input_func_t gre_input_func
;
396 * IP initialization: fill in IP protocol switch table.
397 * All protocols not implemented in kernel go to raw IP protocol handler.
400 ip_init(struct protosw
*pp
, struct domain
*dp
)
402 static int ip_initialized
= 0;
407 domain_proto_mtx_lock_assert_held();
408 VERIFY((pp
->pr_flags
& (PR_INITIALIZED
|PR_ATTACHED
)) == PR_ATTACHED
);
410 /* ipq_alloc() uses mbufs for IP fragment queue structures */
411 _CASSERT(sizeof (struct ipq
) <= _MLEN
);
414 * Some ioctls (e.g. SIOCAIFADDR) use ifaliasreq struct, which is
415 * interchangeable with in_aliasreq; they must have the same size.
417 _CASSERT(sizeof (struct ifaliasreq
) == sizeof (struct in_aliasreq
));
423 PE_parse_boot_argn("net.inet.ip.scopedroute",
424 &ip_doscopedroute
, sizeof (ip_doscopedroute
));
428 in_ifaddr_rwlock_grp_attr
= lck_grp_attr_alloc_init();
429 in_ifaddr_rwlock_grp
= lck_grp_alloc_init("in_ifaddr_rwlock",
430 in_ifaddr_rwlock_grp_attr
);
431 in_ifaddr_rwlock_attr
= lck_attr_alloc_init();
432 lck_rw_init(in_ifaddr_rwlock
, in_ifaddr_rwlock_grp
,
433 in_ifaddr_rwlock_attr
);
435 TAILQ_INIT(&in_ifaddrhead
);
436 in_ifaddrhashtbl_init();
440 pr
= pffindproto_locked(PF_INET
, IPPROTO_RAW
, SOCK_RAW
);
442 panic("%s: Unable to find [PF_INET,IPPROTO_RAW,SOCK_RAW]\n",
447 /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
448 for (i
= 0; i
< IPPROTO_MAX
; i
++)
451 * Cycle through IP protocols and put them into the appropriate place
452 * in ip_protox[], skipping protocols IPPROTO_{IP,RAW}.
454 VERIFY(dp
== inetdomain
&& dp
->dom_family
== PF_INET
);
455 TAILQ_FOREACH(pr
, &dp
->dom_protosw
, pr_entry
) {
456 VERIFY(pr
->pr_domain
== dp
);
457 if (pr
->pr_protocol
!= 0 && pr
->pr_protocol
!= IPPROTO_RAW
) {
458 /* Be careful to only index valid IP protocols. */
459 if (pr
->pr_protocol
< IPPROTO_MAX
)
460 ip_protox
[pr
->pr_protocol
] = pr
;
464 /* IP fragment reassembly queue lock */
465 ipqlock_grp_attr
= lck_grp_attr_alloc_init();
466 ipqlock_grp
= lck_grp_alloc_init("ipqlock", ipqlock_grp_attr
);
467 ipqlock_attr
= lck_attr_alloc_init();
468 lck_mtx_init(&ipqlock
, ipqlock_grp
, ipqlock_attr
);
470 lck_mtx_lock(&ipqlock
);
471 /* Initialize IP reassembly queue. */
472 for (i
= 0; i
< IPREASS_NHASH
; i
++)
475 maxnipq
= nmbclusters
/ 32;
476 maxfragsperpacket
= 128; /* enough for 64k in 512 byte fragments */
478 lck_mtx_unlock(&ipqlock
);
481 ip_id
= RandomULong() ^ tv
.tv_usec
;
487 sadb_stat_mutex_grp_attr
= lck_grp_attr_alloc_init();
488 sadb_stat_mutex_grp
= lck_grp_alloc_init("sadb_stat",
489 sadb_stat_mutex_grp_attr
);
490 sadb_stat_mutex_attr
= lck_attr_alloc_init();
491 lck_mtx_init(sadb_stat_mutex
, sadb_stat_mutex_grp
,
492 sadb_stat_mutex_attr
);
499 * Initialize IPv4 source address hash table.
502 in_ifaddrhashtbl_init(void)
506 if (in_ifaddrhashtbl
!= NULL
)
509 PE_parse_boot_argn("inaddr_nhash", &inaddr_nhash
,
510 sizeof (inaddr_nhash
));
511 if (inaddr_nhash
== 0)
512 inaddr_nhash
= INADDR_NHASH
;
514 MALLOC(in_ifaddrhashtbl
, struct in_ifaddrhashhead
*,
515 inaddr_nhash
* sizeof (*in_ifaddrhashtbl
),
516 M_IFADDR
, M_WAITOK
| M_ZERO
);
517 if (in_ifaddrhashtbl
== NULL
)
518 panic("in_ifaddrhashtbl_init allocation failed");
521 * Generate the next largest prime greater than inaddr_nhash.
523 k
= (inaddr_nhash
% 2 == 0) ? inaddr_nhash
+ 1 : inaddr_nhash
+ 2;
526 for (i
= 3; i
* i
<= k
; i
+= 2) {
538 inaddr_hashval(u_int32_t key
)
541 * The hash index is the computed prime times the key modulo
542 * the hash size, as documented in "Introduction to Algorithms"
543 * (Cormen, Leiserson, Rivest).
545 if (inaddr_nhash
> 1)
546 return ((key
* inaddr_hashp
) % inaddr_nhash
);
552 ip_proto_dispatch_in_wrapper(struct mbuf
*m
, int hlen
, u_int8_t proto
)
554 ip_proto_dispatch_in(m
, hlen
, proto
, 0);
557 __private_extern__
void
558 ip_proto_dispatch_in(struct mbuf
*m
, int hlen
, u_int8_t proto
,
559 ipfilter_t inject_ipfref
)
561 struct ipfilter
*filter
;
562 int seen
= (inject_ipfref
== NULL
);
563 int changed_header
= 0;
565 void (*pr_input
)(struct mbuf
*, int len
);
567 if (!TAILQ_EMPTY(&ipv4_filters
)) {
569 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
571 if ((struct ipfilter
*)inject_ipfref
== filter
)
573 } else if (filter
->ipf_filter
.ipf_input
) {
576 if (changed_header
== 0) {
578 * Perform IP header alignment fixup,
579 * if needed, before passing packet
582 IP_HDR_ALIGNMENT_FIXUP(m
,
583 m
->m_pkthdr
.rcvif
, ipf_unref());
585 /* ipf_unref() already called */
590 ip
= mtod(m
, struct ip
*);
591 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
592 ip
->ip_off
= htons(ip
->ip_off
);
594 ip
->ip_sum
= ip_cksum_hdr_in(m
, hlen
);
596 result
= filter
->ipf_filter
.ipf_input(
597 filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
,
599 if (result
== EJUSTRETURN
) {
613 /* Perform IP header alignment fixup (post-filters), if needed */
614 IP_HDR_ALIGNMENT_FIXUP(m
, m
->m_pkthdr
.rcvif
, return);
617 * If there isn't a specific lock for the protocol
618 * we're about to call, use the generic lock for AF_INET.
619 * otherwise let the protocol deal with its own locking
621 ip
= mtod(m
, struct ip
*);
623 if (changed_header
) {
624 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
625 ip
->ip_off
= ntohs(ip
->ip_off
);
628 if ((pr_input
= ip_protox
[ip
->ip_p
]->pr_input
) == NULL
) {
630 } else if (!(ip_protox
[ip
->ip_p
]->pr_flags
& PR_PROTOLOCK
)) {
631 lck_mtx_lock(inet_domain_mutex
);
633 lck_mtx_unlock(inet_domain_mutex
);
640 * Ip input routine. Checksum and byte swap header. If fragmented
641 * try to reassemble. Process options. Pass to next level.
644 ip_input(struct mbuf
*m
)
647 struct in_ifaddr
*ia
= NULL
;
648 unsigned int hlen
, checkif
;
650 struct in_addr pkt_dst
;
653 u_int32_t div_info
= 0; /* packet divert/tee info */
655 #if IPFIREWALL || DUMMYNET
656 struct ip_fw_args args
;
659 ipfilter_t inject_filter_ref
= NULL
;
662 /* Check if the mbuf is still valid after interface filter processing */
663 MBUF_INPUT_CHECK(m
, m
->m_pkthdr
.rcvif
);
664 inifp
= m
->m_pkthdr
.rcvif
;
665 VERIFY(inifp
!= NULL
);
667 /* Perform IP header alignment fixup, if needed */
668 IP_HDR_ALIGNMENT_FIXUP(m
, inifp
, goto bad
);
670 m
->m_pkthdr
.pkt_flags
&= ~PKTF_FORWARDED
;
672 #if IPFIREWALL || DUMMYNET
673 bzero(&args
, sizeof (struct ip_fw_args
));
676 * Don't bother searching for tag(s) if there's none.
678 if (SLIST_EMPTY(&m
->m_pkthdr
.tags
))
681 /* Grab info from mtags prepended to the chain */
683 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
684 KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
685 struct dn_pkt_tag
*dn_tag
;
687 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
688 args
.fwa_ipfw_rule
= dn_tag
->dn_ipfw_rule
;
689 args
.fwa_pf_rule
= dn_tag
->dn_pf_rule
;
691 m_tag_delete(m
, tag
);
693 #endif /* DUMMYNET */
696 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
697 KERNEL_TAG_TYPE_DIVERT
, NULL
)) != NULL
) {
698 struct divert_tag
*div_tag
;
700 div_tag
= (struct divert_tag
*)(tag
+1);
701 args
.fwa_divert_rule
= div_tag
->cookie
;
703 m_tag_delete(m
, tag
);
707 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
708 KERNEL_TAG_TYPE_IPFORWARD
, NULL
)) != NULL
) {
709 struct ip_fwd_tag
*ipfwd_tag
;
711 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
712 args
.fwa_next_hop
= ipfwd_tag
->next_hop
;
714 m_tag_delete(m
, tag
);
718 if (m
== NULL
|| !(m
->m_flags
& M_PKTHDR
))
719 panic("ip_input no HDR");
723 if (args
.fwa_ipfw_rule
|| args
.fwa_pf_rule
) {
724 /* dummynet already filtered us */
725 ip
= mtod(m
, struct ip
*);
726 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
727 inject_filter_ref
= ipf_get_inject_filter(m
);
729 if (args
.fwa_ipfw_rule
)
731 #endif /* IPFIREWALL */
732 if (args
.fwa_pf_rule
)
735 #endif /* DUMMYNET */
737 #endif /* IPFIREWALL || DUMMYNET */
740 * No need to process packet twice if we've already seen it.
742 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
))
743 inject_filter_ref
= ipf_get_inject_filter(m
);
744 if (inject_filter_ref
!= NULL
) {
745 ip
= mtod(m
, struct ip
*);
746 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
748 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
749 struct ip
*, ip
, struct ifnet
*, inifp
,
750 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
752 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
753 ip
->ip_off
= ntohs(ip
->ip_off
);
754 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, inject_filter_ref
);
758 OSAddAtomic(1, &ipstat
.ips_total
);
759 if (m
->m_pkthdr
.len
< sizeof (struct ip
))
762 if (m
->m_len
< sizeof (struct ip
) &&
763 (m
= m_pullup(m
, sizeof (struct ip
))) == NULL
) {
764 OSAddAtomic(1, &ipstat
.ips_toosmall
);
767 ip
= mtod(m
, struct ip
*);
769 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
, ip
->ip_src
.s_addr
,
770 ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
772 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
773 OSAddAtomic(1, &ipstat
.ips_badvers
);
777 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
778 if (hlen
< sizeof (struct ip
)) { /* minimum header length */
779 OSAddAtomic(1, &ipstat
.ips_badhlen
);
782 if (hlen
> m
->m_len
) {
783 if ((m
= m_pullup(m
, hlen
)) == NULL
) {
784 OSAddAtomic(1, &ipstat
.ips_badhlen
);
787 ip
= mtod(m
, struct ip
*);
790 /* 127/8 must not appear on wire - RFC1122 */
791 if ((ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
792 (ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
) {
794 * Allow for the following exceptions:
796 * 1. If the packet was sent to loopback (i.e. rcvif
797 * would have been set earlier at output time.)
799 * 2. If the packet was sent out on loopback from a local
800 * source address which belongs to a non-loopback
801 * interface (i.e. rcvif may not necessarily be a
802 * loopback interface, hence the test for PKTF_LOOP.)
803 * Unlike IPv6, there is no interface scope ID, and
804 * therefore we don't care so much about PKTF_IFINFO.
806 if (!(inifp
->if_flags
& IFF_LOOPBACK
) &&
807 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
808 OSAddAtomic(1, &ipstat
.ips_badaddr
);
813 /* IPv4 Link-Local Addresses as defined in RFC3927 */
814 if ((IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
)) ||
815 IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)))) {
816 ip_linklocal_stat
.iplls_in_total
++;
817 if (ip
->ip_ttl
!= MAXTTL
) {
818 OSAddAtomic(1, &ip_linklocal_stat
.iplls_in_badttl
);
819 /* Silently drop link local traffic with bad TTL */
820 if (!ip_linklocal_in_allowbadttl
)
825 sum
= ip_cksum(m
, hlen
);
830 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
831 struct ip
*, ip
, struct ifnet
*, inifp
,
832 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
835 * Naively assume we can attribute inbound data to the route we would
836 * use to send to this destination. Asymetric routing breaks this
837 * assumption, but it still allows us to account for traffic from
838 * a remote node in the routing table.
839 * this has a very significant performance impact so we bypass
840 * if nstat_collect is disabled. We may also bypass if the
841 * protocol is tcp in the future because tcp will have a route that
842 * we can use to attribute the data to. That does mean we would not
843 * account for forwarded tcp traffic.
847 ifnet_cached_rtlookup_inet(inifp
, ip
->ip_src
);
849 nstat_route_rx(rt
, 1, m
->m_pkthdr
.len
, 0);
855 * Convert fields to host representation.
857 #if BYTE_ORDER != BIG_ENDIAN
861 if (ip
->ip_len
< hlen
) {
862 OSAddAtomic(1, &ipstat
.ips_badlen
);
866 #if BYTE_ORDER != BIG_ENDIAN
870 * Check that the amount of data in the buffers
871 * is as at least much as the IP header would have us expect.
872 * Trim mbufs if longer than we expect.
873 * Drop packet if shorter than we expect.
875 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
877 OSAddAtomic(1, &ipstat
.ips_tooshort
);
880 if (m
->m_pkthdr
.len
> ip
->ip_len
) {
882 * Invalidate hardware checksum info if ip_adj_clear_hwcksum
883 * is set; useful to handle buggy drivers. Note that this
884 * should not be enabled by default, as we may get here due
885 * to link-layer padding.
887 if (ip_adj_clear_hwcksum
&&
888 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
) &&
889 !(inifp
->if_flags
& IFF_LOOPBACK
) &&
890 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
891 m
->m_pkthdr
.csum_flags
&= ~CSUM_DATA_VALID
;
892 m
->m_pkthdr
.csum_data
= 0;
893 ipstat
.ips_adj_hwcsum_clr
++;
897 if (m
->m_len
== m
->m_pkthdr
.len
) {
898 m
->m_len
= ip
->ip_len
;
899 m
->m_pkthdr
.len
= ip
->ip_len
;
901 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
904 /* for consistency */
905 m
->m_pkthdr
.pkt_proto
= ip
->ip_p
;
911 /* Invoke inbound packet filter */
915 error
= pf_af_hook(inifp
, NULL
, &m
, AF_INET
, TRUE
, &args
);
917 error
= pf_af_hook(inifp
, NULL
, &m
, AF_INET
, TRUE
, NULL
);
918 #endif /* DUMMYNET */
919 if (error
!= 0 || m
== NULL
) {
921 panic("%s: unexpected packet %p\n",
925 /* Already freed by callee */
928 ip
= mtod(m
, struct ip
*);
929 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
934 if (ipsec_bypass
== 0 && ipsec_gethist(m
, NULL
))
941 #endif /* DUMMYNET */
943 * Check if we want to allow this packet to be processed.
944 * Consider it to be bad if not.
946 if (fw_enable
&& IPFW_LOADED
) {
947 #if IPFIREWALL_FORWARD
949 * If we've been forwarded from the output side, then
950 * skip the firewall a second time
952 if (args
.fwa_next_hop
)
954 #endif /* IPFIREWALL_FORWARD */
958 i
= ip_fw_chk_ptr(&args
);
961 if ((i
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) { /* drop */
966 ip
= mtod(m
, struct ip
*); /* just in case m changed */
968 if (i
== 0 && args
.fwa_next_hop
== NULL
) { /* common case */
972 if (DUMMYNET_LOADED
&& (i
& IP_FW_PORT_DYNT_FLAG
) != 0) {
973 /* Send packet to the appropriate pipe */
974 ip_dn_io_ptr(m
, i
&0xffff, DN_TO_IP_IN
, &args
,
978 #endif /* DUMMYNET */
980 if (i
!= 0 && (i
& IP_FW_PORT_DYNT_FLAG
) == 0) {
981 /* Divert or tee packet */
986 #if IPFIREWALL_FORWARD
987 if (i
== 0 && args
.fwa_next_hop
!= NULL
) {
992 * if we get here, the packet must be dropped
997 #endif /* IPFIREWALL */
998 #if IPSEC | IPFIREWALL
1002 * Process options and, if not destined for us,
1003 * ship it on. ip_dooptions returns 1 when an
1004 * error was detected (causing an icmp message
1005 * to be sent and the original packet to be freed).
1007 ip_nhops
= 0; /* for source routed packets */
1009 if (hlen
> sizeof (struct ip
) &&
1010 ip_dooptions(m
, 0, args
.fwa_next_hop
)) {
1011 #else /* !IPFIREWALL */
1012 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, NULL
)) {
1013 #endif /* !IPFIREWALL */
1018 * Check our list of addresses, to see if the packet is for us.
1019 * If we don't have any addresses, assume any unicast packet
1020 * we receive might be for us (and let the upper layers deal
1023 if (TAILQ_EMPTY(&in_ifaddrhead
) && !(m
->m_flags
& (M_MCAST
|M_BCAST
))) {
1024 ip_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
1029 * Cache the destination address of the packet; this may be
1030 * changed by use of 'ipfw fwd'.
1033 pkt_dst
= args
.fwa_next_hop
== NULL
?
1034 ip
->ip_dst
: args
.fwa_next_hop
->sin_addr
;
1035 #else /* !IPFIREWALL */
1036 pkt_dst
= ip
->ip_dst
;
1037 #endif /* !IPFIREWALL */
1040 * Enable a consistency check between the destination address
1041 * and the arrival interface for a unicast packet (the RFC 1122
1042 * strong ES model) if IP forwarding is disabled and the packet
1043 * is not locally generated and the packet is not subject to
1046 * XXX - Checking also should be disabled if the destination
1047 * address is ipnat'ed to a different interface.
1049 * XXX - Checking is incompatible with IP aliases added
1050 * to the loopback interface instead of the interface where
1051 * the packets are received.
1053 checkif
= ip_checkinterface
&& (ipforwarding
== 0) &&
1054 !(inifp
->if_flags
& IFF_LOOPBACK
) &&
1055 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)
1057 && (args
.fwa_next_hop
== NULL
);
1058 #else /* !IPFIREWALL */
1060 #endif /* !IPFIREWALL */
1063 * Check for exact addresses in the hash bucket.
1065 lck_rw_lock_shared(in_ifaddr_rwlock
);
1066 TAILQ_FOREACH(ia
, INADDR_HASH(pkt_dst
.s_addr
), ia_hash
) {
1068 * If the address matches, verify that the packet
1069 * arrived via the correct interface if checking is
1072 if (IA_SIN(ia
)->sin_addr
.s_addr
== pkt_dst
.s_addr
&&
1073 (!checkif
|| ia
->ia_ifp
== inifp
)) {
1074 ip_setdstifaddr_info(m
, 0, ia
);
1075 lck_rw_done(in_ifaddr_rwlock
);
1079 lck_rw_done(in_ifaddr_rwlock
);
1082 * Check for broadcast addresses.
1084 * Only accept broadcast packets that arrive via the matching
1085 * interface. Reception of forwarded directed broadcasts would be
1086 * handled via ip_forward() and ether_frameout() with the loopback
1087 * into the stack for SIMPLEX interfaces handled by ether_frameout().
1089 if (inifp
->if_flags
& IFF_BROADCAST
) {
1092 ifnet_lock_shared(inifp
);
1093 TAILQ_FOREACH(ifa
, &inifp
->if_addrhead
, ifa_link
) {
1094 if (ifa
->ifa_addr
->sa_family
!= AF_INET
) {
1098 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
1099 pkt_dst
.s_addr
|| ia
->ia_netbroadcast
.s_addr
==
1101 ip_setdstifaddr_info(m
, 0, ia
);
1102 ifnet_lock_done(inifp
);
1106 ifnet_lock_done(inifp
);
1109 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
1110 struct in_multi
*inm
;
1112 * See if we belong to the destination multicast group on the
1113 * arrival interface.
1115 in_multihead_lock_shared();
1116 IN_LOOKUP_MULTI(&ip
->ip_dst
, inifp
, inm
);
1117 in_multihead_lock_done();
1119 OSAddAtomic(1, &ipstat
.ips_notmember
);
1123 ip_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
1127 if (ip
->ip_dst
.s_addr
== (u_int32_t
)INADDR_BROADCAST
||
1128 ip
->ip_dst
.s_addr
== INADDR_ANY
) {
1129 ip_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
1133 /* Allow DHCP/BootP responses through */
1134 if ((inifp
->if_eflags
& IFEF_AUTOCONFIGURING
) &&
1135 hlen
== sizeof (struct ip
) && ip
->ip_p
== IPPROTO_UDP
) {
1136 struct udpiphdr
*ui
;
1138 if (m
->m_len
< sizeof (struct udpiphdr
) &&
1139 (m
= m_pullup(m
, sizeof (struct udpiphdr
))) == NULL
) {
1140 OSAddAtomic(1, &udpstat
.udps_hdrops
);
1143 ui
= mtod(m
, struct udpiphdr
*);
1144 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) {
1145 ip_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
1148 ip
= mtod(m
, struct ip
*); /* in case it changed */
1152 * Not for us; forward if possible and desirable.
1154 if (ipforwarding
== 0) {
1155 OSAddAtomic(1, &ipstat
.ips_cantforward
);
1159 ip_forward(m
, 0, args
.fwa_next_hop
);
1161 ip_forward(m
, 0, NULL
);
1168 * If offset or IP_MF are set, must reassemble.
1170 if (ip
->ip_off
& ~(IP_DF
| IP_RF
)) {
1172 * ip_reass() will return a different mbuf, and update
1173 * the divert info in div_info and args.fwa_divert_rule.
1176 m
= ip_reass(m
, (u_int16_t
*)&div_info
, &args
.fwa_divert_rule
);
1182 ip
= mtod(m
, struct ip
*);
1183 /* Get the header length of the reassembled packet */
1184 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1186 /* Restore original checksum before diverting packet */
1187 if (div_info
!= 0) {
1188 #if BYTE_ORDER != BIG_ENDIAN
1193 ip
->ip_sum
= ip_cksum_hdr_in(m
, hlen
);
1194 #if BYTE_ORDER != BIG_ENDIAN
1203 * Further protocols expect the packet length to be w/o the
1210 * Divert or tee packet to the divert protocol if required.
1212 * If div_info is zero then cookie should be too, so we shouldn't
1213 * need to clear them here. Assume divert_packet() does so also.
1215 if (div_info
!= 0) {
1216 struct mbuf
*clone
= NULL
;
1218 /* Clone packet if we're doing a 'tee' */
1219 if (div_info
& IP_FW_PORT_TEE_FLAG
)
1220 clone
= m_dup(m
, M_DONTWAIT
);
1222 /* Restore packet header fields to original values */
1225 #if BYTE_ORDER != BIG_ENDIAN
1229 /* Deliver packet to divert input routine */
1230 OSAddAtomic(1, &ipstat
.ips_delivered
);
1231 divert_packet(m
, 1, div_info
& 0xffff, args
.fwa_divert_rule
);
1233 /* If 'tee', continue with original packet */
1234 if (clone
== NULL
) {
1238 ip
= mtod(m
, struct ip
*);
1244 * enforce IPsec policy checking if we are seeing last header.
1245 * note that we do not visit this with protocols with pcb layer
1246 * code - like udp/tcp/raw ip.
1248 if (ipsec_bypass
== 0 && (ip_protox
[ip
->ip_p
]->pr_flags
& PR_LASTHDR
)) {
1249 if (ipsec4_in_reject(m
, NULL
)) {
1250 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
1257 * Switch out to protocol's input routine.
1259 OSAddAtomic(1, &ipstat
.ips_delivered
);
1262 if (args
.fwa_next_hop
&& ip
->ip_p
== IPPROTO_TCP
) {
1263 /* TCP needs IPFORWARD info if available */
1264 struct m_tag
*fwd_tag
;
1265 struct ip_fwd_tag
*ipfwd_tag
;
1267 fwd_tag
= m_tag_create(KERNEL_MODULE_TAG_ID
,
1268 KERNEL_TAG_TYPE_IPFORWARD
, sizeof (*ipfwd_tag
),
1270 if (fwd_tag
== NULL
)
1273 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
1274 ipfwd_tag
->next_hop
= args
.fwa_next_hop
;
1276 m_tag_prepend(m
, fwd_tag
);
1278 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1279 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1281 /* TCP deals with its own locking */
1282 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1284 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1285 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1287 if ((sw_lro
) && (ip
->ip_p
== IPPROTO_TCP
)) {
1288 m
= tcp_lro(m
, hlen
);
1293 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1295 #else /* !IPFIREWALL */
1296 if ((sw_lro
) && (ip
->ip_p
== IPPROTO_TCP
)) {
1297 m
= tcp_lro(m
, hlen
);
1301 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1302 #endif /* !IPFIREWALL */
1306 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1311 ipq_updateparams(void)
1313 lck_mtx_assert(&ipqlock
, LCK_MTX_ASSERT_OWNED
);
1315 * -1 for unlimited allocation.
1320 * Positive number for specific bound.
1323 ipq_limit
= maxnipq
;
1325 * Zero specifies no further fragment queue allocation -- set the
1326 * bound very low, but rely on implementation elsewhere to actually
1327 * prevent allocation and reclaim current queues.
1332 * Arm the purge timer if not already and if there's work to do
1334 frag_sched_timeout();
1338 sysctl_maxnipq SYSCTL_HANDLER_ARGS
1340 #pragma unused(arg1, arg2)
1343 lck_mtx_lock(&ipqlock
);
1345 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
1346 if (error
|| req
->newptr
== USER_ADDR_NULL
)
1349 if (i
< -1 || i
> (nmbclusters
/ 4)) {
1356 lck_mtx_unlock(&ipqlock
);
1361 sysctl_maxfragsperpacket SYSCTL_HANDLER_ARGS
1363 #pragma unused(arg1, arg2)
1366 lck_mtx_lock(&ipqlock
);
1367 i
= maxfragsperpacket
;
1368 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
1369 if (error
|| req
->newptr
== USER_ADDR_NULL
)
1371 maxfragsperpacket
= i
;
1372 ipq_updateparams(); /* see if we need to arm timer */
1374 lck_mtx_unlock(&ipqlock
);
1379 * Take incoming datagram fragment and try to reassemble it into
1380 * whole datagram. If a chain for reassembly of this datagram already
1381 * exists, then it is given as fp; otherwise have to make a chain.
1383 * When IPDIVERT enabled, keep additional state with each packet that
1384 * tells us if we need to divert or tee the packet we're building.
1386 * The IP header is *NOT* adjusted out of iplen.
1388 static struct mbuf
*
1390 ip_reass(struct mbuf
*m
,
1393 #else /* IPDIVERT_44 */
1395 #endif /* IPDIVERT_44 */
1396 u_int16_t
*divcookie
)
1397 #else /* IPDIVERT */
1398 ip_reass(struct mbuf
*m
)
1399 #endif /* IPDIVERT */
1402 struct mbuf
*p
, *q
, *nq
, *t
;
1403 struct ipq
*fp
= NULL
;
1404 struct ipqhead
*head
;
1407 uint32_t csum
, csum_flags
;
1411 MBUFQ_INIT(&dfq
); /* for deferred frees */
1413 /* If maxnipq or maxfragsperpacket is 0, never accept fragments. */
1414 if (maxnipq
== 0 || maxfragsperpacket
== 0) {
1415 ipstat
.ips_fragments
++;
1416 ipstat
.ips_fragdropped
++;
1419 lck_mtx_lock(&ipqlock
);
1420 frag_sched_timeout(); /* purge stale fragments */
1421 lck_mtx_unlock(&ipqlock
);
1426 ip
= mtod(m
, struct ip
*);
1427 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1429 lck_mtx_lock(&ipqlock
);
1431 hash
= IPREASS_HASH(ip
->ip_src
.s_addr
, ip
->ip_id
);
1435 * Look for queue of fragments
1438 TAILQ_FOREACH(fp
, head
, ipq_list
) {
1439 if (ip
->ip_id
== fp
->ipq_id
&&
1440 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
1441 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
1443 mac_ipq_label_compare(m
, fp
) &&
1445 ip
->ip_p
== fp
->ipq_p
)
1452 * Attempt to trim the number of allocated fragment queues if it
1453 * exceeds the administrative limit.
1455 if ((nipq
> (unsigned)maxnipq
) && (maxnipq
> 0)) {
1457 * drop something from the tail of the current queue
1458 * before proceeding further
1460 struct ipq
*fq
= TAILQ_LAST(head
, ipqhead
);
1461 if (fq
== NULL
) { /* gak */
1462 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1463 struct ipq
*r
= TAILQ_LAST(&ipq
[i
], ipqhead
);
1465 ipstat
.ips_fragtimeout
+= r
->ipq_nfrags
;
1466 frag_freef(&ipq
[i
], r
);
1471 ipstat
.ips_fragtimeout
+= fq
->ipq_nfrags
;
1472 frag_freef(head
, fq
);
1478 * Leverage partial checksum offload for IP fragments. Narrow down
1479 * the scope to cover only UDP without IP options, as that is the
1482 * Perform 1's complement adjustment of octets that got included/
1483 * excluded in the hardware-calculated checksum value. Ignore cases
1484 * where the value includes or excludes the IP header span, as the
1485 * sum for those octets would already be 0xffff and thus no-op.
1487 if (ip
->ip_p
== IPPROTO_UDP
&& hlen
== sizeof (struct ip
) &&
1488 (m
->m_pkthdr
.csum_flags
&
1489 (CSUM_DATA_VALID
| CSUM_PARTIAL
| CSUM_PSEUDO_HDR
)) ==
1490 (CSUM_DATA_VALID
| CSUM_PARTIAL
)) {
1493 start
= m
->m_pkthdr
.csum_rx_start
;
1494 csum
= m
->m_pkthdr
.csum_rx_val
;
1496 if (start
!= 0 && start
!= hlen
) {
1497 #if BYTE_ORDER != BIG_ENDIAN
1503 /* callee folds in sum */
1504 csum
= m_adj_sum16(m
, start
, hlen
, csum
);
1505 #if BYTE_ORDER != BIG_ENDIAN
1512 csum_flags
= m
->m_pkthdr
.csum_flags
;
1518 /* Invalidate checksum */
1519 m
->m_pkthdr
.csum_flags
&= ~CSUM_DATA_VALID
;
1521 ipstat
.ips_fragments
++;
1524 * Adjust ip_len to not reflect header,
1525 * convert offset of this to bytes.
1528 if (ip
->ip_off
& IP_MF
) {
1530 * Make sure that fragments have a data length
1531 * that's a non-zero multiple of 8 bytes.
1533 if (ip
->ip_len
== 0 || (ip
->ip_len
& 0x7) != 0) {
1534 OSAddAtomic(1, &ipstat
.ips_toosmall
);
1536 * Reassembly queue may have been found if previous
1537 * fragments were valid; given that this one is bad,
1538 * we need to drop it. Make sure to set fp to NULL
1539 * if not already, since we don't want to decrement
1540 * ipq_nfrags as it doesn't include this packet.
1545 m
->m_flags
|= M_FRAG
;
1547 /* Clear the flag in case packet comes from loopback */
1548 m
->m_flags
&= ~M_FRAG
;
1552 m
->m_pkthdr
.pkt_hdr
= ip
;
1554 /* Previous ip_reass() started here. */
1556 * Presence of header sizes in mbufs
1557 * would confuse code below.
1563 * If first fragment to arrive, create a reassembly queue.
1566 fp
= ipq_alloc(M_DONTWAIT
);
1570 if (mac_ipq_label_init(fp
, M_NOWAIT
) != 0) {
1575 mac_ipq_label_associate(m
, fp
);
1577 TAILQ_INSERT_HEAD(head
, fp
, ipq_list
);
1580 fp
->ipq_ttl
= IPFRAGTTL
;
1581 fp
->ipq_p
= ip
->ip_p
;
1582 fp
->ipq_id
= ip
->ip_id
;
1583 fp
->ipq_src
= ip
->ip_src
;
1584 fp
->ipq_dst
= ip
->ip_dst
;
1586 m
->m_nextpkt
= NULL
;
1588 * If the first fragment has valid checksum offload
1589 * info, the rest of fragments are eligible as well.
1591 if (csum_flags
!= 0) {
1592 fp
->ipq_csum
= csum
;
1593 fp
->ipq_csum_flags
= csum_flags
;
1597 * Transfer firewall instructions to the fragment structure.
1598 * Only trust info in the fragment at offset 0.
1600 if (ip
->ip_off
== 0) {
1602 fp
->ipq_div_info
= *divinfo
;
1604 fp
->ipq_divert
= *divinfo
;
1606 fp
->ipq_div_cookie
= *divcookie
;
1610 #endif /* IPDIVERT */
1611 m
= NULL
; /* nothing to return */
1616 mac_ipq_label_update(m
, fp
);
1620 #define GETIP(m) ((struct ip *)((m)->m_pkthdr.pkt_hdr))
1623 * Handle ECN by comparing this segment with the first one;
1624 * if CE is set, do not lose CE.
1625 * drop if CE and not-ECT are mixed for the same packet.
1627 ecn
= ip
->ip_tos
& IPTOS_ECN_MASK
;
1628 ecn0
= GETIP(fp
->ipq_frags
)->ip_tos
& IPTOS_ECN_MASK
;
1629 if (ecn
== IPTOS_ECN_CE
) {
1630 if (ecn0
== IPTOS_ECN_NOTECT
)
1632 if (ecn0
!= IPTOS_ECN_CE
)
1633 GETIP(fp
->ipq_frags
)->ip_tos
|= IPTOS_ECN_CE
;
1635 if (ecn
== IPTOS_ECN_NOTECT
&& ecn0
!= IPTOS_ECN_NOTECT
)
1639 * Find a segment which begins after this one does.
1641 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
)
1642 if (GETIP(q
)->ip_off
> ip
->ip_off
)
1646 * If there is a preceding segment, it may provide some of
1647 * our data already. If so, drop the data from the incoming
1648 * segment. If it provides all of our data, drop us, otherwise
1649 * stick new segment in the proper place.
1651 * If some of the data is dropped from the preceding
1652 * segment, then it's checksum is invalidated.
1655 i
= GETIP(p
)->ip_off
+ GETIP(p
)->ip_len
- ip
->ip_off
;
1657 if (i
>= ip
->ip_len
)
1660 fp
->ipq_csum_flags
= 0;
1664 m
->m_nextpkt
= p
->m_nextpkt
;
1667 m
->m_nextpkt
= fp
->ipq_frags
;
1672 * While we overlap succeeding segments trim them or,
1673 * if they are completely covered, dequeue them.
1675 for (; q
!= NULL
&& ip
->ip_off
+ ip
->ip_len
> GETIP(q
)->ip_off
;
1677 i
= (ip
->ip_off
+ ip
->ip_len
) - GETIP(q
)->ip_off
;
1678 if (i
< GETIP(q
)->ip_len
) {
1679 GETIP(q
)->ip_len
-= i
;
1680 GETIP(q
)->ip_off
+= i
;
1682 fp
->ipq_csum_flags
= 0;
1687 ipstat
.ips_fragdropped
++;
1689 /* defer freeing until after lock is dropped */
1690 MBUFQ_ENQUEUE(&dfq
, q
);
1694 * If this fragment contains similar checksum offload info
1695 * as that of the existing ones, accumulate checksum. Otherwise,
1696 * invalidate checksum offload info for the entire datagram.
1698 if (csum_flags
!= 0 && csum_flags
== fp
->ipq_csum_flags
)
1699 fp
->ipq_csum
+= csum
;
1700 else if (fp
->ipq_csum_flags
!= 0)
1701 fp
->ipq_csum_flags
= 0;
1705 * Transfer firewall instructions to the fragment structure.
1706 * Only trust info in the fragment at offset 0.
1708 if (ip
->ip_off
== 0) {
1710 fp
->ipq_div_info
= *divinfo
;
1712 fp
->ipq_divert
= *divinfo
;
1714 fp
->ipq_div_cookie
= *divcookie
;
1718 #endif /* IPDIVERT */
1721 * Check for complete reassembly and perform frag per packet
1724 * Frag limiting is performed here so that the nth frag has
1725 * a chance to complete the packet before we drop the packet.
1726 * As a result, n+1 frags are actually allowed per packet, but
1727 * only n will ever be stored. (n = maxfragsperpacket.)
1731 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
) {
1732 if (GETIP(q
)->ip_off
!= next
) {
1733 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
1734 ipstat
.ips_fragdropped
+= fp
->ipq_nfrags
;
1735 frag_freef(head
, fp
);
1737 m
= NULL
; /* nothing to return */
1740 next
+= GETIP(q
)->ip_len
;
1742 /* Make sure the last packet didn't have the IP_MF flag */
1743 if (p
->m_flags
& M_FRAG
) {
1744 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
1745 ipstat
.ips_fragdropped
+= fp
->ipq_nfrags
;
1746 frag_freef(head
, fp
);
1748 m
= NULL
; /* nothing to return */
1753 * Reassembly is complete. Make sure the packet is a sane size.
1757 if (next
+ (IP_VHL_HL(ip
->ip_vhl
) << 2) > IP_MAXPACKET
) {
1758 ipstat
.ips_toolong
++;
1759 ipstat
.ips_fragdropped
+= fp
->ipq_nfrags
;
1760 frag_freef(head
, fp
);
1761 m
= NULL
; /* nothing to return */
1766 * Concatenate fragments.
1773 q
->m_nextpkt
= NULL
;
1774 for (q
= nq
; q
!= NULL
; q
= nq
) {
1776 q
->m_nextpkt
= NULL
;
1781 * Store partial hardware checksum info from the fragment queue;
1782 * the receive start offset is set to 20 bytes (see code at the
1783 * top of this routine.)
1785 if (fp
->ipq_csum_flags
!= 0) {
1786 csum
= fp
->ipq_csum
;
1790 m
->m_pkthdr
.csum_rx_val
= csum
;
1791 m
->m_pkthdr
.csum_rx_start
= sizeof (struct ip
);
1792 m
->m_pkthdr
.csum_flags
= fp
->ipq_csum_flags
;
1793 } else if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) ||
1794 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
1795 /* loopback checksums are always OK */
1796 m
->m_pkthdr
.csum_data
= 0xffff;
1797 m
->m_pkthdr
.csum_flags
&= ~CSUM_PARTIAL
;
1798 m
->m_pkthdr
.csum_flags
=
1799 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
1800 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
1805 * Extract firewall instructions from the fragment structure.
1808 *divinfo
= fp
->ipq_div_info
;
1810 *divinfo
= fp
->ipq_divert
;
1812 *divcookie
= fp
->ipq_div_cookie
;
1813 #endif /* IPDIVERT */
1816 mac_mbuf_label_associate_ipq(fp
, m
);
1817 mac_ipq_label_destroy(fp
);
1820 * Create header for new ip packet by modifying header of first
1821 * packet; dequeue and discard fragment reassembly header.
1822 * Make header visible.
1824 ip
->ip_len
= (IP_VHL_HL(ip
->ip_vhl
) << 2) + next
;
1825 ip
->ip_src
= fp
->ipq_src
;
1826 ip
->ip_dst
= fp
->ipq_dst
;
1828 fp
->ipq_frags
= NULL
; /* return to caller as 'm' */
1829 frag_freef(head
, fp
);
1832 m
->m_len
+= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1833 m
->m_data
-= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1834 /* some debugging cruft by sklower, below, will go away soon */
1835 if (m
->m_flags
& M_PKTHDR
) /* XXX this should be done elsewhere */
1837 ipstat
.ips_reassembled
++;
1839 /* arm the purge timer if not already and if there's work to do */
1840 frag_sched_timeout();
1841 lck_mtx_unlock(&ipqlock
);
1842 /* perform deferred free (if needed) now that lock is dropped */
1843 if (!MBUFQ_EMPTY(&dfq
))
1845 VERIFY(MBUFQ_EMPTY(&dfq
));
1850 /* arm the purge timer if not already and if there's work to do */
1851 frag_sched_timeout();
1852 lck_mtx_unlock(&ipqlock
);
1853 /* perform deferred free (if needed) */
1854 if (!MBUFQ_EMPTY(&dfq
))
1856 VERIFY(MBUFQ_EMPTY(&dfq
));
1863 #endif /* IPDIVERT */
1864 ipstat
.ips_fragdropped
++;
1867 /* arm the purge timer if not already and if there's work to do */
1868 frag_sched_timeout();
1869 lck_mtx_unlock(&ipqlock
);
1871 /* perform deferred free (if needed) */
1872 if (!MBUFQ_EMPTY(&dfq
))
1874 VERIFY(MBUFQ_EMPTY(&dfq
));
1880 * Free a fragment reassembly header and all
1881 * associated datagrams.
1884 frag_freef(struct ipqhead
*fhp
, struct ipq
*fp
)
1886 lck_mtx_assert(&ipqlock
, LCK_MTX_ASSERT_OWNED
);
1889 if (fp
->ipq_frags
!= NULL
) {
1890 m_freem_list(fp
->ipq_frags
);
1891 fp
->ipq_frags
= NULL
;
1893 TAILQ_REMOVE(fhp
, fp
, ipq_list
);
1899 * IP reassembly timer processing
1902 frag_timeout(void *arg
)
1909 * Update coarse-grained networking timestamp (in sec.); the idea
1910 * is to piggy-back on the timeout callout to update the counter
1911 * returnable via net_uptime().
1913 net_update_uptime();
1915 lck_mtx_lock(&ipqlock
);
1916 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1917 for (fp
= TAILQ_FIRST(&ipq
[i
]); fp
; ) {
1921 fp
= TAILQ_NEXT(fp
, ipq_list
);
1922 if (--fpp
->ipq_ttl
== 0) {
1923 ipstat
.ips_fragtimeout
+= fpp
->ipq_nfrags
;
1924 frag_freef(&ipq
[i
], fpp
);
1929 * If we are over the maximum number of fragments
1930 * (due to the limit being lowered), drain off
1931 * enough to get down to the new limit.
1933 if (maxnipq
>= 0 && nipq
> (unsigned)maxnipq
) {
1934 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1935 while (nipq
> (unsigned)maxnipq
&&
1936 !TAILQ_EMPTY(&ipq
[i
])) {
1937 ipstat
.ips_fragdropped
+=
1938 TAILQ_FIRST(&ipq
[i
])->ipq_nfrags
;
1939 frag_freef(&ipq
[i
], TAILQ_FIRST(&ipq
[i
]));
1943 /* re-arm the purge timer if there's work to do */
1944 frag_timeout_run
= 0;
1945 frag_sched_timeout();
1946 lck_mtx_unlock(&ipqlock
);
1950 frag_sched_timeout(void)
1952 lck_mtx_assert(&ipqlock
, LCK_MTX_ASSERT_OWNED
);
1954 if (!frag_timeout_run
&& nipq
> 0) {
1955 frag_timeout_run
= 1;
1956 timeout(frag_timeout
, NULL
, hz
);
1961 * Drain off all datagram fragments.
1968 lck_mtx_lock(&ipqlock
);
1969 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1970 while (!TAILQ_EMPTY(&ipq
[i
])) {
1971 ipstat
.ips_fragdropped
+=
1972 TAILQ_FIRST(&ipq
[i
])->ipq_nfrags
;
1973 frag_freef(&ipq
[i
], TAILQ_FIRST(&ipq
[i
]));
1976 lck_mtx_unlock(&ipqlock
);
1986 * See comments in ipq_updateparams(). Keep the count separate
1987 * from nipq since the latter represents the elements already
1988 * in the reassembly queues.
1990 if (ipq_limit
> 0 && ipq_count
> ipq_limit
)
1993 t
= m_get(how
, MT_FTABLE
);
1995 atomic_add_32(&ipq_count
, 1);
1996 fp
= mtod(t
, struct ipq
*);
1997 bzero(fp
, sizeof (*fp
));
2005 ipq_free(struct ipq
*fp
)
2007 (void) m_free(dtom(fp
));
2008 atomic_add_32(&ipq_count
, -1);
2017 frag_drain(); /* fragments */
2018 in_rtqdrain(); /* protocol cloned routes */
2019 in_arpdrain(NULL
); /* cloned routes: ARP */
2023 * Do option processing on a datagram,
2024 * possibly discarding it if bad options are encountered,
2025 * or forwarding it if source-routed.
2026 * The pass argument is used when operating in the IPSTEALTH
2027 * mode to tell what options to process:
2028 * [LS]SRR (pass 0) or the others (pass 1).
2029 * The reason for as many as two passes is that when doing IPSTEALTH,
2030 * non-routing options should be processed only if the packet is for us.
2031 * Returns 1 if packet has been forwarded/freed,
2032 * 0 if the packet should be processed further.
2035 ip_dooptions(struct mbuf
*m
, int pass
, struct sockaddr_in
*next_hop
)
2037 #pragma unused(pass)
2038 struct ip
*ip
= mtod(m
, struct ip
*);
2040 struct ip_timestamp
*ipt
;
2041 struct in_ifaddr
*ia
;
2042 int opt
, optlen
, cnt
, off
, code
, type
= ICMP_PARAMPROB
, forward
= 0;
2043 struct in_addr
*sin
, dst
;
2045 struct sockaddr_in ipaddr
= {
2046 sizeof (ipaddr
), AF_INET
, 0, { 0 }, { 0, } };
2048 /* Expect 32-bit aligned data pointer on strict-align platforms */
2049 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
2052 cp
= (u_char
*)(ip
+ 1);
2053 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
2054 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
2055 opt
= cp
[IPOPT_OPTVAL
];
2056 if (opt
== IPOPT_EOL
)
2058 if (opt
== IPOPT_NOP
)
2061 if (cnt
< IPOPT_OLEN
+ sizeof (*cp
)) {
2062 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
2065 optlen
= cp
[IPOPT_OLEN
];
2066 if (optlen
< IPOPT_OLEN
+ sizeof (*cp
) ||
2068 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
2078 * Source routing with record.
2079 * Find interface with current destination address.
2080 * If none on this machine then drop if strictly routed,
2081 * or do nothing if loosely routed.
2082 * Record interface address and bring up next address
2083 * component. If strictly routed make sure next
2084 * address is on directly accessible net.
2088 if (optlen
< IPOPT_OFFSET
+ sizeof (*cp
)) {
2089 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
2092 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
2093 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
2096 ipaddr
.sin_addr
= ip
->ip_dst
;
2097 ia
= (struct in_ifaddr
*)ifa_ifwithaddr(SA(&ipaddr
));
2099 if (opt
== IPOPT_SSRR
) {
2100 type
= ICMP_UNREACH
;
2101 code
= ICMP_UNREACH_SRCFAIL
;
2104 if (!ip_dosourceroute
)
2105 goto nosourcerouting
;
2107 * Loose routing, and not at next destination
2108 * yet; nothing to do except forward.
2112 IFA_REMREF(&ia
->ia_ifa
);
2115 off
--; /* 0 origin */
2116 if (off
> optlen
- (int)sizeof (struct in_addr
)) {
2118 * End of source route. Should be for us.
2120 if (!ip_acceptsourceroute
)
2121 goto nosourcerouting
;
2122 save_rte(cp
, ip
->ip_src
);
2126 if (!ip_dosourceroute
) {
2128 char buf
[MAX_IPv4_STR_LEN
];
2129 char buf2
[MAX_IPv4_STR_LEN
];
2131 * Acting as a router, so generate ICMP
2135 "attempted source route from %s "
2137 inet_ntop(AF_INET
, &ip
->ip_src
,
2139 inet_ntop(AF_INET
, &ip
->ip_dst
,
2140 buf2
, sizeof (buf2
)));
2141 type
= ICMP_UNREACH
;
2142 code
= ICMP_UNREACH_SRCFAIL
;
2146 * Not acting as a router,
2149 OSAddAtomic(1, &ipstat
.ips_cantforward
);
2156 * locate outgoing interface
2158 (void) memcpy(&ipaddr
.sin_addr
, cp
+ off
,
2159 sizeof (ipaddr
.sin_addr
));
2161 if (opt
== IPOPT_SSRR
) {
2162 #define INA struct in_ifaddr *
2163 if ((ia
= (INA
)ifa_ifwithdstaddr(
2164 SA(&ipaddr
))) == NULL
) {
2165 ia
= (INA
)ifa_ifwithnet(SA(&ipaddr
));
2168 ia
= ip_rtaddr(ipaddr
.sin_addr
);
2171 type
= ICMP_UNREACH
;
2172 code
= ICMP_UNREACH_SRCFAIL
;
2175 ip
->ip_dst
= ipaddr
.sin_addr
;
2176 IFA_LOCK(&ia
->ia_ifa
);
2177 (void) memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
2178 sizeof (struct in_addr
));
2179 IFA_UNLOCK(&ia
->ia_ifa
);
2180 IFA_REMREF(&ia
->ia_ifa
);
2182 cp
[IPOPT_OFFSET
] += sizeof (struct in_addr
);
2184 * Let ip_intr's mcast routing check handle mcast pkts
2186 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
2190 if (optlen
< IPOPT_OFFSET
+ sizeof (*cp
)) {
2191 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
2194 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
2195 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
2199 * If no space remains, ignore.
2201 off
--; /* 0 origin */
2202 if (off
> optlen
- (int)sizeof (struct in_addr
))
2204 (void) memcpy(&ipaddr
.sin_addr
, &ip
->ip_dst
,
2205 sizeof (ipaddr
.sin_addr
));
2207 * locate outgoing interface; if we're the destination,
2208 * use the incoming interface (should be same).
2210 if ((ia
= (INA
)ifa_ifwithaddr(SA(&ipaddr
))) == NULL
) {
2211 if ((ia
= ip_rtaddr(ipaddr
.sin_addr
)) == NULL
) {
2212 type
= ICMP_UNREACH
;
2213 code
= ICMP_UNREACH_HOST
;
2217 IFA_LOCK(&ia
->ia_ifa
);
2218 (void) memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
2219 sizeof (struct in_addr
));
2220 IFA_UNLOCK(&ia
->ia_ifa
);
2221 IFA_REMREF(&ia
->ia_ifa
);
2223 cp
[IPOPT_OFFSET
] += sizeof (struct in_addr
);
2227 code
= cp
- (u_char
*)ip
;
2228 ipt
= (struct ip_timestamp
*)(void *)cp
;
2229 if (ipt
->ipt_len
< 4 || ipt
->ipt_len
> 40) {
2230 code
= (u_char
*)&ipt
->ipt_len
- (u_char
*)ip
;
2233 if (ipt
->ipt_ptr
< 5) {
2234 code
= (u_char
*)&ipt
->ipt_ptr
- (u_char
*)ip
;
2238 ipt
->ipt_len
- (int)sizeof (int32_t)) {
2239 if (++ipt
->ipt_oflw
== 0) {
2240 code
= (u_char
*)&ipt
->ipt_ptr
-
2246 sin
= (struct in_addr
*)(void *)(cp
+ ipt
->ipt_ptr
- 1);
2247 switch (ipt
->ipt_flg
) {
2249 case IPOPT_TS_TSONLY
:
2252 case IPOPT_TS_TSANDADDR
:
2253 if (ipt
->ipt_ptr
- 1 + sizeof (n_time
) +
2254 sizeof (struct in_addr
) > ipt
->ipt_len
) {
2255 code
= (u_char
*)&ipt
->ipt_ptr
-
2259 ipaddr
.sin_addr
= dst
;
2260 ia
= (INA
)ifaof_ifpforaddr(SA(&ipaddr
),
2264 IFA_LOCK(&ia
->ia_ifa
);
2265 (void) memcpy(sin
, &IA_SIN(ia
)->sin_addr
,
2266 sizeof (struct in_addr
));
2267 IFA_UNLOCK(&ia
->ia_ifa
);
2268 ipt
->ipt_ptr
+= sizeof (struct in_addr
);
2269 IFA_REMREF(&ia
->ia_ifa
);
2273 case IPOPT_TS_PRESPEC
:
2274 if (ipt
->ipt_ptr
- 1 + sizeof (n_time
) +
2275 sizeof (struct in_addr
) > ipt
->ipt_len
) {
2276 code
= (u_char
*)&ipt
->ipt_ptr
-
2280 (void) memcpy(&ipaddr
.sin_addr
, sin
,
2281 sizeof (struct in_addr
));
2282 if ((ia
= (struct in_ifaddr
*)ifa_ifwithaddr(
2283 SA(&ipaddr
))) == NULL
)
2285 IFA_REMREF(&ia
->ia_ifa
);
2287 ipt
->ipt_ptr
+= sizeof (struct in_addr
);
2291 /* XXX can't take &ipt->ipt_flg */
2292 code
= (u_char
*)&ipt
->ipt_ptr
-
2297 (void) memcpy(cp
+ ipt
->ipt_ptr
- 1, &ntime
,
2299 ipt
->ipt_ptr
+= sizeof (n_time
);
2302 if (forward
&& ipforwarding
) {
2303 ip_forward(m
, 1, next_hop
);
2308 /* XXX icmp_error adds in hdr length */
2309 ip
->ip_len
-= IP_VHL_HL(ip
->ip_vhl
) << 2;
2310 icmp_error(m
, type
, code
, 0, 0);
2311 OSAddAtomic(1, &ipstat
.ips_badoptions
);
2316 * Check for the presence of the IP Router Alert option [RFC2113]
2317 * in the header of an IPv4 datagram.
2319 * This call is not intended for use from the forwarding path; it is here
2320 * so that protocol domains may check for the presence of the option.
2321 * Given how FreeBSD's IPv4 stack is currently structured, the Router Alert
2322 * option does not have much relevance to the implementation, though this
2323 * may change in future.
2324 * Router alert options SHOULD be passed if running in IPSTEALTH mode and
2325 * we are not the endpoint.
2326 * Length checks on individual options should already have been peformed
2327 * by ip_dooptions() therefore they are folded under DIAGNOSTIC here.
2329 * Return zero if not present or options are invalid, non-zero if present.
2332 ip_checkrouteralert(struct mbuf
*m
)
2334 struct ip
*ip
= mtod(m
, struct ip
*);
2336 int opt
, optlen
, cnt
, found_ra
;
2339 cp
= (u_char
*)(ip
+ 1);
2340 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
2341 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
2342 opt
= cp
[IPOPT_OPTVAL
];
2343 if (opt
== IPOPT_EOL
)
2345 if (opt
== IPOPT_NOP
)
2349 if (cnt
< IPOPT_OLEN
+ sizeof (*cp
))
2352 optlen
= cp
[IPOPT_OLEN
];
2354 if (optlen
< IPOPT_OLEN
+ sizeof (*cp
) || optlen
> cnt
)
2361 if (optlen
!= IPOPT_OFFSET
+ sizeof (uint16_t) ||
2362 (*((uint16_t *)(void *)&cp
[IPOPT_OFFSET
]) != 0))
2377 * Given address of next destination (final or next hop),
2378 * return internet address info of interface to be used to get there.
2381 ip_rtaddr(struct in_addr dst
)
2383 struct sockaddr_in
*sin
;
2384 struct ifaddr
*rt_ifa
;
2387 bzero(&ro
, sizeof (ro
));
2388 sin
= SIN(&ro
.ro_dst
);
2389 sin
->sin_family
= AF_INET
;
2390 sin
->sin_len
= sizeof (*sin
);
2391 sin
->sin_addr
= dst
;
2393 rtalloc_ign(&ro
, RTF_PRCLONING
);
2394 if (ro
.ro_rt
== NULL
) {
2400 if ((rt_ifa
= ro
.ro_rt
->rt_ifa
) != NULL
)
2402 RT_UNLOCK(ro
.ro_rt
);
2405 return ((struct in_ifaddr
*)rt_ifa
);
2409 * Save incoming source route for use in replies,
2410 * to be picked up later by ip_srcroute if the receiver is interested.
2413 save_rte(u_char
*option
, struct in_addr dst
)
2417 olen
= option
[IPOPT_OLEN
];
2420 printf("save_rte: olen %d\n", olen
);
2422 if (olen
> sizeof (ip_srcrt
) - (1 + sizeof (dst
)))
2424 bcopy(option
, ip_srcrt
.srcopt
, olen
);
2425 ip_nhops
= (olen
- IPOPT_OFFSET
- 1) / sizeof (struct in_addr
);
2430 * Retrieve incoming source route for use in replies,
2431 * in the same form used by setsockopt.
2432 * The first hop is placed before the options, will be removed later.
2437 struct in_addr
*p
, *q
;
2443 m
= m_get(M_DONTWAIT
, MT_HEADER
);
2447 #define OPTSIZ (sizeof (ip_srcrt.nop) + sizeof (ip_srcrt.srcopt))
2449 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
2450 m
->m_len
= ip_nhops
* sizeof (struct in_addr
) +
2451 sizeof (struct in_addr
) + OPTSIZ
;
2454 printf("ip_srcroute: nhops %d mlen %d", ip_nhops
, m
->m_len
);
2458 * First save first hop for return route
2460 p
= &ip_srcrt
.route
[ip_nhops
- 1];
2461 *(mtod(m
, struct in_addr
*)) = *p
--;
2465 (u_int32_t
)ntohl(mtod(m
, struct in_addr
*)->s_addr
));
2469 * Copy option fields and padding (nop) to mbuf.
2471 ip_srcrt
.nop
= IPOPT_NOP
;
2472 ip_srcrt
.srcopt
[IPOPT_OFFSET
] = IPOPT_MINOFF
;
2473 (void) memcpy(mtod(m
, caddr_t
) + sizeof (struct in_addr
),
2474 &ip_srcrt
.nop
, OPTSIZ
);
2475 q
= (struct in_addr
*)(void *)(mtod(m
, caddr_t
) +
2476 sizeof (struct in_addr
) + OPTSIZ
);
2479 * Record return path as an IP source route,
2480 * reversing the path (pointers are now aligned).
2482 while (p
>= ip_srcrt
.route
) {
2485 printf(" %lx", (u_int32_t
)ntohl(q
->s_addr
));
2490 * Last hop goes to final destination.
2495 printf(" %lx\n", (u_int32_t
)ntohl(q
->s_addr
));
2501 * Strip out IP options, at higher
2502 * level protocol in the kernel.
2503 * Second argument is buffer to which options
2504 * will be moved, and return value is their length.
2505 * XXX should be deleted; last arg currently ignored.
2508 ip_stripoptions(struct mbuf
*m
, struct mbuf
*mopt
)
2510 #pragma unused(mopt)
2512 struct ip
*ip
= mtod(m
, struct ip
*);
2516 /* Expect 32-bit aligned data pointer on strict-align platforms */
2517 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
2519 olen
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
2520 opts
= (caddr_t
)(ip
+ 1);
2521 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
2522 bcopy(opts
+ olen
, opts
, (unsigned)i
);
2524 if (m
->m_flags
& M_PKTHDR
)
2525 m
->m_pkthdr
.len
-= olen
;
2526 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof (struct ip
) >> 2);
2529 u_char inetctlerrmap
[PRC_NCMDS
] = {
2531 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
2532 ENETUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
2533 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
2535 ENOPROTOOPT
, ECONNREFUSED
2539 sysctl_ipforwarding SYSCTL_HANDLER_ARGS
2541 #pragma unused(arg1, arg2)
2542 int i
, was_ipforwarding
= ipforwarding
;
2544 i
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
2545 if (i
!= 0 || req
->newptr
== USER_ADDR_NULL
)
2548 if (was_ipforwarding
&& !ipforwarding
) {
2549 /* clean up IPv4 forwarding cached routes */
2550 ifnet_head_lock_shared();
2551 for (i
= 0; i
<= if_index
; i
++) {
2552 struct ifnet
*ifp
= ifindex2ifnet
[i
];
2554 lck_mtx_lock(&ifp
->if_cached_route_lock
);
2555 ROUTE_RELEASE(&ifp
->if_fwd_route
);
2556 bzero(&ifp
->if_fwd_route
,
2557 sizeof (ifp
->if_fwd_route
));
2558 lck_mtx_unlock(&ifp
->if_cached_route_lock
);
2568 * Similar to inp_route_{copyout,copyin} routines except that these copy
2569 * out the cached IPv4 forwarding route from struct ifnet instead of the
2570 * inpcb. See comments for those routines for explanations.
2573 ip_fwd_route_copyout(struct ifnet
*ifp
, struct route
*dst
)
2575 struct route
*src
= &ifp
->if_fwd_route
;
2577 lck_mtx_lock_spin(&ifp
->if_cached_route_lock
);
2578 lck_mtx_convert_spin(&ifp
->if_cached_route_lock
);
2580 /* Minor sanity check */
2581 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET
)
2582 panic("%s: wrong or corrupted route: %p", __func__
, src
);
2584 route_copyout(dst
, src
, sizeof (*dst
));
2586 lck_mtx_unlock(&ifp
->if_cached_route_lock
);
2590 ip_fwd_route_copyin(struct ifnet
*ifp
, struct route
*src
)
2592 struct route
*dst
= &ifp
->if_fwd_route
;
2594 lck_mtx_lock_spin(&ifp
->if_cached_route_lock
);
2595 lck_mtx_convert_spin(&ifp
->if_cached_route_lock
);
2597 /* Minor sanity check */
2598 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET
)
2599 panic("%s: wrong or corrupted route: %p", __func__
, src
);
2601 if (ifp
->if_fwd_cacheok
)
2602 route_copyin(src
, dst
, sizeof (*src
));
2604 lck_mtx_unlock(&ifp
->if_cached_route_lock
);
2608 * Forward a packet. If some error occurs return the sender
2609 * an icmp packet. Note we can't always generate a meaningful
2610 * icmp message because icmp doesn't have a large enough repertoire
2611 * of codes and types.
2613 * If not forwarding, just drop the packet. This could be confusing
2614 * if ipforwarding was zero but some routing protocol was advancing
2615 * us as a gateway to somewhere. However, we must let the routing
2616 * protocol deal with that.
2618 * The srcrt parameter indicates whether the packet is being forwarded
2619 * via a source route.
2622 ip_forward(struct mbuf
*m
, int srcrt
, struct sockaddr_in
*next_hop
)
2625 #pragma unused(next_hop)
2627 struct ip
*ip
= mtod(m
, struct ip
*);
2628 struct sockaddr_in
*sin
;
2630 struct route fwd_rt
;
2631 int error
, type
= 0, code
= 0;
2634 struct in_addr pkt_dst
;
2635 u_int32_t nextmtu
= 0, len
;
2636 struct ip_out_args ipoa
= { IFSCOPE_NONE
, { 0 }, 0, 0 };
2637 struct ifnet
*rcvifp
= m
->m_pkthdr
.rcvif
;
2639 struct secpolicy
*sp
= NULL
;
2643 struct pf_mtag
*pf_mtag
;
2649 * Cache the destination address of the packet; this may be
2650 * changed by use of 'ipfw fwd'.
2652 pkt_dst
= ((next_hop
!= NULL
) ? next_hop
->sin_addr
: ip
->ip_dst
);
2653 #else /* !IPFIREWALL */
2654 pkt_dst
= ip
->ip_dst
;
2655 #endif /* !IPFIREWALL */
2659 printf("forward: src %lx dst %lx ttl %x\n",
2660 (u_int32_t
)ip
->ip_src
.s_addr
, (u_int32_t
)pkt_dst
.s_addr
,
2664 if (m
->m_flags
& (M_BCAST
|M_MCAST
) || !in_canforward(pkt_dst
)) {
2665 OSAddAtomic(1, &ipstat
.ips_cantforward
);
2671 #endif /* IPSTEALTH */
2672 if (ip
->ip_ttl
<= IPTTLDEC
) {
2673 icmp_error(m
, ICMP_TIMXCEED
, ICMP_TIMXCEED_INTRANS
,
2679 #endif /* IPSTEALTH */
2682 pf_mtag
= pf_find_mtag(m
);
2683 if (pf_mtag
!= NULL
&& pf_mtag
->pftag_rtableid
!= IFSCOPE_NONE
) {
2684 ipoa
.ipoa_boundif
= pf_mtag
->pftag_rtableid
;
2685 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
2689 ip_fwd_route_copyout(rcvifp
, &fwd_rt
);
2691 sin
= SIN(&fwd_rt
.ro_dst
);
2692 if (ROUTE_UNUSABLE(&fwd_rt
) || pkt_dst
.s_addr
!= sin
->sin_addr
.s_addr
) {
2693 ROUTE_RELEASE(&fwd_rt
);
2695 sin
->sin_family
= AF_INET
;
2696 sin
->sin_len
= sizeof (*sin
);
2697 sin
->sin_addr
= pkt_dst
;
2699 rtalloc_scoped_ign(&fwd_rt
, RTF_PRCLONING
, ipoa
.ipoa_boundif
);
2700 if (fwd_rt
.ro_rt
== NULL
) {
2701 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_HOST
, dest
, 0);
2708 * Save the IP header and at most 8 bytes of the payload,
2709 * in case we need to generate an ICMP message to the src.
2711 * We don't use m_copy() because it might return a reference
2712 * to a shared cluster. Both this function and ip_output()
2713 * assume exclusive access to the IP header in `m', so any
2714 * data in a cluster may change before we reach icmp_error().
2716 MGET(mcopy
, M_DONTWAIT
, m
->m_type
);
2717 if (mcopy
!= NULL
) {
2718 M_COPY_PKTHDR(mcopy
, m
);
2719 mcopy
->m_len
= imin((IP_VHL_HL(ip
->ip_vhl
) << 2) + 8,
2721 m_copydata(m
, 0, mcopy
->m_len
, mtod(mcopy
, caddr_t
));
2726 #endif /* IPSTEALTH */
2727 ip
->ip_ttl
-= IPTTLDEC
;
2730 #endif /* IPSTEALTH */
2733 * If forwarding packet using same interface that it came in on,
2734 * perhaps should send a redirect to sender to shortcut a hop.
2735 * Only send redirect if source is sending directly to us,
2736 * and if packet was not source routed (or has any options).
2737 * Also, don't send redirect if forwarding using a default route
2738 * or a route modified by a redirect.
2741 if (rt
->rt_ifp
== m
->m_pkthdr
.rcvif
&&
2742 !(rt
->rt_flags
& (RTF_DYNAMIC
|RTF_MODIFIED
)) &&
2743 satosin(rt_key(rt
))->sin_addr
.s_addr
!= INADDR_ANY
&&
2744 ipsendredirects
&& !srcrt
&& rt
->rt_ifa
!= NULL
) {
2745 struct in_ifaddr
*ia
= (struct in_ifaddr
*)rt
->rt_ifa
;
2746 u_int32_t src
= ntohl(ip
->ip_src
.s_addr
);
2748 /* Become a regular mutex */
2749 RT_CONVERT_LOCK(rt
);
2750 IFA_LOCK_SPIN(&ia
->ia_ifa
);
2751 if ((src
& ia
->ia_subnetmask
) == ia
->ia_subnet
) {
2752 if (rt
->rt_flags
& RTF_GATEWAY
)
2753 dest
= satosin(rt
->rt_gateway
)->sin_addr
.s_addr
;
2755 dest
= pkt_dst
.s_addr
;
2757 * Router requirements says to only send
2760 type
= ICMP_REDIRECT
;
2761 code
= ICMP_REDIRECT_HOST
;
2764 printf("redirect (%d) to %lx\n", code
,
2768 IFA_UNLOCK(&ia
->ia_ifa
);
2773 if (next_hop
!= NULL
) {
2774 /* Pass IPFORWARD info if available */
2776 struct ip_fwd_tag
*ipfwd_tag
;
2778 tag
= m_tag_create(KERNEL_MODULE_TAG_ID
,
2779 KERNEL_TAG_TYPE_IPFORWARD
,
2780 sizeof (*ipfwd_tag
), M_NOWAIT
, m
);
2787 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
2788 ipfwd_tag
->next_hop
= next_hop
;
2790 m_tag_prepend(m
, tag
);
2792 #endif /* IPFIREWALL */
2794 /* Mark this packet as being forwarded from another interface */
2795 m
->m_pkthdr
.pkt_flags
|= PKTF_FORWARDED
;
2798 error
= ip_output(m
, NULL
, &fwd_rt
, IP_FORWARDING
| IP_OUTARGS
,
2801 /* Refresh rt since the route could have changed while in IP */
2805 OSAddAtomic(1, &ipstat
.ips_cantforward
);
2808 * Increment stats on the source interface; the ones
2809 * for destination interface has been taken care of
2810 * during output above by virtue of PKTF_FORWARDED.
2812 rcvifp
->if_fpackets
++;
2813 rcvifp
->if_fbytes
+= len
;
2815 OSAddAtomic(1, &ipstat
.ips_forward
);
2817 OSAddAtomic(1, &ipstat
.ips_redirectsent
);
2819 if (mcopy
!= NULL
) {
2821 * If we didn't have to go thru ipflow and
2822 * the packet was successfully consumed by
2823 * ip_output, the mcopy is rather a waste;
2824 * this could be further optimized.
2835 case 0: /* forwarded, but need redirect */
2836 /* type, code set above */
2839 case ENETUNREACH
: /* shouldn't happen, checked above */
2844 type
= ICMP_UNREACH
;
2845 code
= ICMP_UNREACH_HOST
;
2849 type
= ICMP_UNREACH
;
2850 code
= ICMP_UNREACH_NEEDFRAG
;
2856 if (rt
->rt_ifp
!= NULL
)
2857 nextmtu
= rt
->rt_ifp
->if_mtu
;
2865 * If the packet is routed over IPsec tunnel, tell the
2866 * originator the tunnel MTU.
2867 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
2870 sp
= ipsec4_getpolicybyaddr(mcopy
, IPSEC_DIR_OUTBOUND
,
2871 IP_FORWARDING
, &ipsecerror
);
2877 * find the correct route for outer IPv4
2878 * header, compute tunnel MTU.
2882 if (sp
->req
!= NULL
&&
2883 sp
->req
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2884 struct secasindex saidx
;
2885 struct secasvar
*sav
;
2890 /* count IPsec header size */
2891 ipsechdr
= ipsec_hdrsiz(sp
);
2893 ipm
= mtod(mcopy
, struct ip
*);
2894 bcopy(&sp
->req
->saidx
, &saidx
, sizeof (saidx
));
2895 saidx
.mode
= sp
->req
->saidx
.mode
;
2896 saidx
.reqid
= sp
->req
->saidx
.reqid
;
2897 sin
= SIN(&saidx
.src
);
2898 if (sin
->sin_len
== 0) {
2899 sin
->sin_len
= sizeof (*sin
);
2900 sin
->sin_family
= AF_INET
;
2901 sin
->sin_port
= IPSEC_PORT_ANY
;
2902 bcopy(&ipm
->ip_src
, &sin
->sin_addr
,
2903 sizeof (sin
->sin_addr
));
2905 sin
= SIN(&saidx
.dst
);
2906 if (sin
->sin_len
== 0) {
2907 sin
->sin_len
= sizeof (*sin
);
2908 sin
->sin_family
= AF_INET
;
2909 sin
->sin_port
= IPSEC_PORT_ANY
;
2910 bcopy(&ipm
->ip_dst
, &sin
->sin_addr
,
2911 sizeof (sin
->sin_addr
));
2913 sav
= key_allocsa_policy(&saidx
);
2915 lck_mtx_lock(sadb_mutex
);
2916 if (sav
->sah
!= NULL
) {
2917 ro
= &sav
->sah
->sa_route
;
2918 if (ro
->ro_rt
!= NULL
) {
2920 if (ro
->ro_rt
->rt_ifp
!= NULL
) {
2921 nextmtu
= ro
->ro_rt
->
2923 nextmtu
-= ipsechdr
;
2925 RT_UNLOCK(ro
->ro_rt
);
2928 key_freesav(sav
, KEY_SADB_LOCKED
);
2929 lck_mtx_unlock(sadb_mutex
);
2932 key_freesp(sp
, KEY_SADB_UNLOCKED
);
2938 * A router should not generate ICMP_SOURCEQUENCH as
2939 * required in RFC1812 Requirements for IP Version 4 Routers.
2940 * Source quench could be a big problem under DoS attacks,
2941 * or if the underlying interface is rate-limited.
2942 * Those who need source quench packets may re-enable them
2943 * via the net.inet.ip.sendsourcequench sysctl.
2945 if (ip_sendsourcequench
== 0) {
2949 type
= ICMP_SOURCEQUENCH
;
2954 case EACCES
: /* ipfw denied packet */
2959 if (type
== ICMP_UNREACH
&& code
== ICMP_UNREACH_NEEDFRAG
)
2960 OSAddAtomic(1, &ipstat
.ips_cantfrag
);
2962 icmp_error(mcopy
, type
, code
, dest
, nextmtu
);
2964 ip_fwd_route_copyin(rcvifp
, &fwd_rt
);
2968 ip_savecontrol(struct inpcb
*inp
, struct mbuf
**mp
, struct ip
*ip
,
2972 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
2976 mp
= sbcreatecontrol_mbuf((caddr_t
)&tv
, sizeof (tv
),
2977 SCM_TIMESTAMP
, SOL_SOCKET
, mp
);
2982 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) {
2985 time
= mach_absolute_time();
2986 mp
= sbcreatecontrol_mbuf((caddr_t
)&time
, sizeof (time
),
2987 SCM_TIMESTAMP_MONOTONIC
, SOL_SOCKET
, mp
);
2992 if (inp
->inp_flags
& INP_RECVDSTADDR
) {
2993 mp
= sbcreatecontrol_mbuf((caddr_t
)&ip
->ip_dst
,
2994 sizeof (struct in_addr
), IP_RECVDSTADDR
, IPPROTO_IP
, mp
);
3002 * Moving these out of udp_input() made them even more broken
3003 * than they already were.
3005 /* options were tossed already */
3006 if (inp
->inp_flags
& INP_RECVOPTS
) {
3007 mp
= sbcreatecontrol_mbuf((caddr_t
)opts_deleted_above
,
3008 sizeof (struct in_addr
), IP_RECVOPTS
, IPPROTO_IP
, mp
);
3013 /* ip_srcroute doesn't do what we want here, need to fix */
3014 if (inp
->inp_flags
& INP_RECVRETOPTS
) {
3015 mp
= sbcreatecontrol_mbuf((caddr_t
)ip_srcroute(),
3016 sizeof (struct in_addr
), IP_RECVRETOPTS
, IPPROTO_IP
, mp
);
3022 if (inp
->inp_flags
& INP_RECVIF
) {
3024 uint8_t sdlbuf
[SOCK_MAXADDRLEN
+ 1];
3025 struct sockaddr_dl
*sdl2
= SDL(&sdlbuf
);
3028 * Make sure to accomodate the largest possible
3029 * size of SA(if_lladdr)->sa_len.
3031 _CASSERT(sizeof (sdlbuf
) == (SOCK_MAXADDRLEN
+ 1));
3033 ifnet_head_lock_shared();
3034 if ((ifp
= m
->m_pkthdr
.rcvif
) != NULL
&&
3035 ifp
->if_index
&& (ifp
->if_index
<= if_index
)) {
3036 struct ifaddr
*ifa
= ifnet_addrs
[ifp
->if_index
- 1];
3037 struct sockaddr_dl
*sdp
;
3039 if (!ifa
|| !ifa
->ifa_addr
)
3043 sdp
= SDL(ifa
->ifa_addr
);
3045 * Change our mind and don't try copy.
3047 if (sdp
->sdl_family
!= AF_LINK
) {
3051 /* the above _CASSERT ensures sdl_len fits in sdlbuf */
3052 bcopy(sdp
, sdl2
, sdp
->sdl_len
);
3057 offsetof(struct sockaddr_dl
, sdl_data
[0]);
3058 sdl2
->sdl_family
= AF_LINK
;
3059 sdl2
->sdl_index
= 0;
3060 sdl2
->sdl_nlen
= sdl2
->sdl_alen
= sdl2
->sdl_slen
= 0;
3063 mp
= sbcreatecontrol_mbuf((caddr_t
)sdl2
, sdl2
->sdl_len
,
3064 IP_RECVIF
, IPPROTO_IP
, mp
);
3069 if (inp
->inp_flags
& INP_RECVTTL
) {
3070 mp
= sbcreatecontrol_mbuf((caddr_t
)&ip
->ip_ttl
,
3071 sizeof (ip
->ip_ttl
), IP_RECVTTL
, IPPROTO_IP
, mp
);
3076 if (inp
->inp_socket
->so_flags
& SOF_RECV_TRAFFIC_CLASS
) {
3077 int tc
= m_get_traffic_class(m
);
3079 mp
= sbcreatecontrol_mbuf((caddr_t
)&tc
, sizeof (tc
),
3080 SO_TRAFFIC_CLASS
, SOL_SOCKET
, mp
);
3085 if (inp
->inp_flags
& INP_PKTINFO
) {
3086 struct in_pktinfo pi
;
3088 bzero(&pi
, sizeof (struct in_pktinfo
));
3089 bcopy(&ip
->ip_dst
, &pi
.ipi_addr
, sizeof (struct in_addr
));
3090 pi
.ipi_ifindex
= (m
!= NULL
&& m
->m_pkthdr
.rcvif
!= NULL
) ?
3091 m
->m_pkthdr
.rcvif
->if_index
: 0;
3093 mp
= sbcreatecontrol_mbuf((caddr_t
)&pi
,
3094 sizeof (struct in_pktinfo
), IP_RECVPKTINFO
, IPPROTO_IP
, mp
);
3102 ipstat
.ips_pktdropcntrl
++;
3106 static inline u_short
3107 ip_cksum(struct mbuf
*m
, int hlen
)
3111 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
3112 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
3113 } else if (!(m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) &&
3114 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
3116 * The packet arrived on an interface which isn't capable
3117 * of performing IP header checksum; compute it now.
3119 sum
= ip_cksum_hdr_in(m
, hlen
);
3122 m
->m_pkthdr
.csum_flags
|= (CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
3123 CSUM_IP_CHECKED
| CSUM_IP_VALID
);
3124 m
->m_pkthdr
.csum_data
= 0xffff;
3128 OSAddAtomic(1, &ipstat
.ips_badsum
);
3134 ip_getstat SYSCTL_HANDLER_ARGS
3136 #pragma unused(oidp, arg1, arg2)
3137 if (req
->oldptr
== USER_ADDR_NULL
)
3138 req
->oldlen
= (size_t)sizeof (struct ipstat
);
3140 return (SYSCTL_OUT(req
, &ipstat
, MIN(sizeof (ipstat
), req
->oldlen
)));
3144 ip_setsrcifaddr_info(struct mbuf
*m
, uint32_t src_idx
, struct in_ifaddr
*ia
)
3146 VERIFY(m
->m_flags
& M_PKTHDR
);
3149 * If the source ifaddr is specified, pick up the information
3150 * from there; otherwise just grab the passed-in ifindex as the
3151 * caller may not have the ifaddr available.
3154 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
3155 m
->m_pkthdr
.src_ifindex
= ia
->ia_ifp
->if_index
;
3157 m
->m_pkthdr
.src_ifindex
= src_idx
;
3159 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
3164 ip_setdstifaddr_info(struct mbuf
*m
, uint32_t dst_idx
, struct in_ifaddr
*ia
)
3166 VERIFY(m
->m_flags
& M_PKTHDR
);
3169 * If the destination ifaddr is specified, pick up the information
3170 * from there; otherwise just grab the passed-in ifindex as the
3171 * caller may not have the ifaddr available.
3174 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
3175 m
->m_pkthdr
.dst_ifindex
= ia
->ia_ifp
->if_index
;
3177 m
->m_pkthdr
.dst_ifindex
= dst_idx
;
3179 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
3184 ip_getsrcifaddr_info(struct mbuf
*m
, uint32_t *src_idx
, uint32_t *iaf
)
3186 VERIFY(m
->m_flags
& M_PKTHDR
);
3188 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
))
3191 if (src_idx
!= NULL
)
3192 *src_idx
= m
->m_pkthdr
.src_ifindex
;
3201 ip_getdstifaddr_info(struct mbuf
*m
, uint32_t *dst_idx
, uint32_t *iaf
)
3203 VERIFY(m
->m_flags
& M_PKTHDR
);
3205 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
))
3208 if (dst_idx
!= NULL
)
3209 *dst_idx
= m
->m_pkthdr
.dst_ifindex
;
3218 * Protocol input handler for IPPROTO_GRE.
3221 gre_input(struct mbuf
*m
, int off
)
3223 gre_input_func_t fn
= gre_input_func
;
3226 * If there is a registered GRE input handler, pass mbuf to it.
3229 lck_mtx_unlock(inet_domain_mutex
);
3230 m
= fn(m
, off
, (mtod(m
, struct ip
*))->ip_p
);
3231 lck_mtx_lock(inet_domain_mutex
);
3235 * If no matching tunnel that is up is found, we inject
3236 * the mbuf to raw ip socket to see if anyone picks it up.
3243 * Private KPI for PPP/PPTP.
3246 ip_gre_register_input(gre_input_func_t fn
)
3248 lck_mtx_lock(inet_domain_mutex
);
3249 gre_input_func
= fn
;
3250 lck_mtx_unlock(inet_domain_mutex
);