2 * Copyright (c) 2000-2015 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>
105 #include <net/net_perf.h>
107 #include <net/pfvar.h>
110 #include <netinet/in.h>
111 #include <netinet/in_systm.h>
112 #include <netinet/in_var.h>
113 #include <netinet/in_arp.h>
114 #include <netinet/ip.h>
115 #include <netinet/in_pcb.h>
116 #include <netinet/ip_var.h>
117 #include <netinet/ip_icmp.h>
118 #include <netinet/ip_fw.h>
119 #include <netinet/ip_divert.h>
120 #include <netinet/kpi_ipfilter_var.h>
121 #include <netinet/udp.h>
122 #include <netinet/udp_var.h>
123 #include <netinet/bootp.h>
124 #include <netinet/lro_ext.h>
127 #include <netinet/ip_dummynet.h>
128 #endif /* DUMMYNET */
131 #include <security/mac_framework.h>
132 #endif /* CONFIG_MACF_NET */
135 #include <netinet6/ipsec.h>
136 #include <netkey/key.h>
139 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 0)
140 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 2)
141 #define DBG_FNC_IP_INPUT NETDBG_CODE(DBG_NETIP, (2 << 8))
144 extern int ipsec_bypass
;
145 extern lck_mtx_t
*sadb_mutex
;
147 lck_grp_t
*sadb_stat_mutex_grp
;
148 lck_grp_attr_t
*sadb_stat_mutex_grp_attr
;
149 lck_attr_t
*sadb_stat_mutex_attr
;
150 decl_lck_mtx_data(, sadb_stat_mutex_data
);
151 lck_mtx_t
*sadb_stat_mutex
= &sadb_stat_mutex_data
;
156 static int frag_timeout_run
; /* frag timer is scheduled to run */
157 static void frag_timeout(void *);
158 static void frag_sched_timeout(void);
160 static struct ipq
*ipq_alloc(int);
161 static void ipq_free(struct ipq
*);
162 static void ipq_updateparams(void);
163 static void ip_input_second_pass(struct mbuf
*, struct ifnet
*,
164 u_int32_t
, int, int, struct ip_fw_in_args
*, int);
166 decl_lck_mtx_data(static, ipqlock
);
167 static lck_attr_t
*ipqlock_attr
;
168 static lck_grp_t
*ipqlock_grp
;
169 static lck_grp_attr_t
*ipqlock_grp_attr
;
171 /* Packet reassembly stuff */
172 #define IPREASS_NHASH_LOG2 6
173 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
174 #define IPREASS_HMASK (IPREASS_NHASH - 1)
175 #define IPREASS_HASH(x, y) \
176 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
178 /* IP fragment reassembly queues (protected by ipqlock) */
179 static TAILQ_HEAD(ipqhead
, ipq
) ipq
[IPREASS_NHASH
]; /* ip reassembly queues */
180 static int maxnipq
; /* max packets in reass queues */
181 static u_int32_t maxfragsperpacket
; /* max frags/packet in reass queues */
182 static u_int32_t nipq
; /* # of packets in reass queues */
183 static u_int32_t ipq_limit
; /* ipq allocation limit */
184 static u_int32_t ipq_count
; /* current # of allocated ipq's */
186 static int sysctl_ipforwarding SYSCTL_HANDLER_ARGS
;
187 static int sysctl_maxnipq SYSCTL_HANDLER_ARGS
;
188 static int sysctl_maxfragsperpacket SYSCTL_HANDLER_ARGS
;
189 static int sysctl_reset_ip_input_stats SYSCTL_HANDLER_ARGS
;
190 static int sysctl_ip_input_measure_bins SYSCTL_HANDLER_ARGS
;
191 static int sysctl_ip_input_getperf SYSCTL_HANDLER_ARGS
;
193 int ipforwarding
= 0;
194 SYSCTL_PROC(_net_inet_ip
, IPCTL_FORWARDING
, forwarding
,
195 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &ipforwarding
, 0,
196 sysctl_ipforwarding
, "I", "Enable IP forwarding between interfaces");
198 static int ipsendredirects
= 1; /* XXX */
199 SYSCTL_INT(_net_inet_ip
, IPCTL_SENDREDIRECTS
, redirect
,
200 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ipsendredirects
, 0,
201 "Enable sending IP redirects");
203 int ip_defttl
= IPDEFTTL
;
204 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFTTL
, ttl
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
205 &ip_defttl
, 0, "Maximum TTL on IP packets");
207 static int ip_dosourceroute
= 0;
208 SYSCTL_INT(_net_inet_ip
, IPCTL_SOURCEROUTE
, sourceroute
,
209 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_dosourceroute
, 0,
210 "Enable forwarding source routed IP packets");
212 static int ip_acceptsourceroute
= 0;
213 SYSCTL_INT(_net_inet_ip
, IPCTL_ACCEPTSOURCEROUTE
, accept_sourceroute
,
214 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_acceptsourceroute
, 0,
215 "Enable accepting source routed IP packets");
217 static int ip_sendsourcequench
= 0;
218 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, sendsourcequench
,
219 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_sendsourcequench
, 0,
220 "Enable the transmission of source quench packets");
222 SYSCTL_PROC(_net_inet_ip
, OID_AUTO
, maxfragpackets
,
223 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &maxnipq
, 0, sysctl_maxnipq
,
224 "I", "Maximum number of IPv4 fragment reassembly queue entries");
226 SYSCTL_UINT(_net_inet_ip
, OID_AUTO
, fragpackets
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
227 &nipq
, 0, "Current number of IPv4 fragment reassembly queue entries");
229 SYSCTL_PROC(_net_inet_ip
, OID_AUTO
, maxfragsperpacket
,
230 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &maxfragsperpacket
, 0,
231 sysctl_maxfragsperpacket
, "I",
232 "Maximum number of IPv4 fragments allowed per packet");
234 int ip_doscopedroute
= 1;
235 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, scopedroute
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
236 &ip_doscopedroute
, 0, "Enable IPv4 scoped routing");
238 static uint32_t ip_adj_clear_hwcksum
= 0;
239 SYSCTL_UINT(_net_inet_ip
, OID_AUTO
, adj_clear_hwcksum
,
240 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_adj_clear_hwcksum
, 0,
241 "Invalidate hwcksum info when adjusting length");
244 * XXX - Setting ip_checkinterface mostly implements the receive side of
245 * the Strong ES model described in RFC 1122, but since the routing table
246 * and transmit implementation do not implement the Strong ES model,
247 * setting this to 1 results in an odd hybrid.
249 * XXX - ip_checkinterface currently must be disabled if you use ipnat
250 * to translate the destination address to another local interface.
252 * XXX - ip_checkinterface must be disabled if you add IP aliases
253 * to the loopback interface instead of the interface where the
254 * packets for those addresses are received.
256 static int ip_checkinterface
= 0;
257 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, check_interface
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
258 &ip_checkinterface
, 0, "Verify packet arrives on correct interface");
260 static int ip_chaining
= 1;
261 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, rx_chaining
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
262 &ip_chaining
, 1, "Do receive side ip address based chaining");
264 static int ip_chainsz
= 6;
265 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, rx_chainsz
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
266 &ip_chainsz
, 1, "IP receive side max chaining");
268 static int ip_input_measure
= 0;
269 SYSCTL_PROC(_net_inet_ip
, OID_AUTO
, input_perf
,
270 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
271 &ip_input_measure
, 0, sysctl_reset_ip_input_stats
, "I", "Do time measurement");
273 static uint64_t ip_input_measure_bins
= 0;
274 SYSCTL_PROC(_net_inet_ip
, OID_AUTO
, input_perf_bins
,
275 CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_input_measure_bins
, 0,
276 sysctl_ip_input_measure_bins
, "I",
277 "bins for chaining performance data histogram");
279 static net_perf_t net_perf
;
280 SYSCTL_PROC(_net_inet_ip
, OID_AUTO
, input_perf_data
,
281 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
282 0, 0, sysctl_ip_input_getperf
, "S,net_perf",
283 "IP input performance data (struct net_perf, net/net_perf.h)");
286 static int ipprintfs
= 0;
289 struct protosw
*ip_protox
[IPPROTO_MAX
];
291 static lck_grp_attr_t
*in_ifaddr_rwlock_grp_attr
;
292 static lck_grp_t
*in_ifaddr_rwlock_grp
;
293 static lck_attr_t
*in_ifaddr_rwlock_attr
;
294 decl_lck_rw_data(, in_ifaddr_rwlock_data
);
295 lck_rw_t
*in_ifaddr_rwlock
= &in_ifaddr_rwlock_data
;
297 /* Protected by in_ifaddr_rwlock */
298 struct in_ifaddrhead in_ifaddrhead
; /* first inet address */
299 struct in_ifaddrhashhead
*in_ifaddrhashtbl
; /* inet addr hash table */
301 #define INADDR_NHASH 61
302 static u_int32_t inaddr_nhash
; /* hash table size */
303 static u_int32_t inaddr_hashp
; /* next largest prime */
305 static int ip_getstat SYSCTL_HANDLER_ARGS
;
306 struct ipstat ipstat
;
307 SYSCTL_PROC(_net_inet_ip
, IPCTL_STATS
, stats
,
308 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
309 0, 0, ip_getstat
, "S,ipstat",
310 "IP statistics (struct ipstat, netinet/ip_var.h)");
313 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFMTU
, mtu
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
314 &ip_mtu
, 0, "Default MTU");
315 #endif /* IPCTL_DEFMTU */
318 static int ipstealth
= 0;
319 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, stealth
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
321 #endif /* IPSTEALTH */
325 ip_fw_chk_t
*ip_fw_chk_ptr
;
329 #endif /* IPFIREWALL */
332 ip_dn_io_t
*ip_dn_io_ptr
;
333 #endif /* DUMMYNET */
335 SYSCTL_NODE(_net_inet_ip
, OID_AUTO
, linklocal
,
336 CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "link local");
338 struct ip_linklocal_stat ip_linklocal_stat
;
339 SYSCTL_STRUCT(_net_inet_ip_linklocal
, OID_AUTO
, stat
,
340 CTLFLAG_RD
| CTLFLAG_LOCKED
, &ip_linklocal_stat
, ip_linklocal_stat
,
341 "Number of link local packets with TTL less than 255");
343 SYSCTL_NODE(_net_inet_ip_linklocal
, OID_AUTO
, in
,
344 CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "link local input");
346 int ip_linklocal_in_allowbadttl
= 1;
347 SYSCTL_INT(_net_inet_ip_linklocal_in
, OID_AUTO
, allowbadttl
,
348 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_linklocal_in_allowbadttl
, 0,
349 "Allow incoming link local packets with TTL less than 255");
353 * We need to save the IP options in case a protocol wants to respond
354 * to an incoming packet over the same route if the packet got here
355 * using IP source routing. This allows connection establishment and
356 * maintenance when the remote end is on a network that is not known
359 static int ip_nhops
= 0;
360 static struct ip_srcrt
{
361 struct in_addr dst
; /* final destination */
362 char nop
; /* one NOP to align */
363 char srcopt
[IPOPT_OFFSET
+ 1]; /* OPTVAL, OLEN and OFFSET */
364 struct in_addr route
[MAX_IPOPTLEN
/ sizeof (struct in_addr
)];
367 static void in_ifaddrhashtbl_init(void);
368 static void save_rte(u_char
*, struct in_addr
);
369 static int ip_dooptions(struct mbuf
*, int, struct sockaddr_in
*);
370 static void ip_forward(struct mbuf
*, int, struct sockaddr_in
*);
371 static void frag_freef(struct ipqhead
*, struct ipq
*);
374 static struct mbuf
*ip_reass(struct mbuf
*, u_int32_t
*, u_int16_t
*);
375 #else /* !IPDIVERT_44 */
376 static struct mbuf
*ip_reass(struct mbuf
*, u_int16_t
*, u_int16_t
*);
377 #endif /* !IPDIVERT_44 */
378 #else /* !IPDIVERT */
379 static struct mbuf
*ip_reass(struct mbuf
*);
380 #endif /* !IPDIVERT */
381 static void ip_fwd_route_copyout(struct ifnet
*, struct route
*);
382 static void ip_fwd_route_copyin(struct ifnet
*, struct route
*);
383 static inline u_short
ip_cksum(struct mbuf
*, int);
385 int ip_use_randomid
= 1;
386 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, random_id
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
387 &ip_use_randomid
, 0, "Randomize IP packets IDs");
390 * On platforms which require strict alignment (currently for anything but
391 * i386 or x86_64), check if the IP header pointer is 32-bit aligned; if not,
392 * copy the contents of the mbuf chain into a new chain, and free the original
393 * one. Create some head room in the first mbuf of the new chain, in case
394 * it's needed later on.
396 #if defined(__i386__) || defined(__x86_64__)
397 #define IP_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { } while (0)
398 #else /* !__i386__ && !__x86_64__ */
399 #define IP_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { \
400 if (!IP_HDR_ALIGNED_P(mtod(_m, caddr_t))) { \
402 struct ifnet *__ifp = (_ifp); \
403 atomic_add_64(&(__ifp)->if_alignerrs, 1); \
404 if (((_m)->m_flags & M_PKTHDR) && \
405 (_m)->m_pkthdr.pkt_hdr != NULL) \
406 (_m)->m_pkthdr.pkt_hdr = NULL; \
407 _n = m_defrag_offset(_m, max_linkhdr, M_NOWAIT); \
409 atomic_add_32(&ipstat.ips_toosmall, 1); \
414 VERIFY(_n != (_m)); \
419 #endif /* !__i386__ && !__x86_64__ */
422 * GRE input handler function, settable via ip_gre_register_input() for PPTP.
424 static gre_input_func_t gre_input_func
;
427 * IP initialization: fill in IP protocol switch table.
428 * All protocols not implemented in kernel go to raw IP protocol handler.
431 ip_init(struct protosw
*pp
, struct domain
*dp
)
433 static int ip_initialized
= 0;
438 domain_proto_mtx_lock_assert_held();
439 VERIFY((pp
->pr_flags
& (PR_INITIALIZED
|PR_ATTACHED
)) == PR_ATTACHED
);
441 /* ipq_alloc() uses mbufs for IP fragment queue structures */
442 _CASSERT(sizeof (struct ipq
) <= _MLEN
);
445 * Some ioctls (e.g. SIOCAIFADDR) use ifaliasreq struct, which is
446 * interchangeable with in_aliasreq; they must have the same size.
448 _CASSERT(sizeof (struct ifaliasreq
) == sizeof (struct in_aliasreq
));
454 PE_parse_boot_argn("net.inet.ip.scopedroute",
455 &ip_doscopedroute
, sizeof (ip_doscopedroute
));
459 in_ifaddr_rwlock_grp_attr
= lck_grp_attr_alloc_init();
460 in_ifaddr_rwlock_grp
= lck_grp_alloc_init("in_ifaddr_rwlock",
461 in_ifaddr_rwlock_grp_attr
);
462 in_ifaddr_rwlock_attr
= lck_attr_alloc_init();
463 lck_rw_init(in_ifaddr_rwlock
, in_ifaddr_rwlock_grp
,
464 in_ifaddr_rwlock_attr
);
466 TAILQ_INIT(&in_ifaddrhead
);
467 in_ifaddrhashtbl_init();
471 pr
= pffindproto_locked(PF_INET
, IPPROTO_RAW
, SOCK_RAW
);
473 panic("%s: Unable to find [PF_INET,IPPROTO_RAW,SOCK_RAW]\n",
478 /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
479 for (i
= 0; i
< IPPROTO_MAX
; i
++)
482 * Cycle through IP protocols and put them into the appropriate place
483 * in ip_protox[], skipping protocols IPPROTO_{IP,RAW}.
485 VERIFY(dp
== inetdomain
&& dp
->dom_family
== PF_INET
);
486 TAILQ_FOREACH(pr
, &dp
->dom_protosw
, pr_entry
) {
487 VERIFY(pr
->pr_domain
== dp
);
488 if (pr
->pr_protocol
!= 0 && pr
->pr_protocol
!= IPPROTO_RAW
) {
489 /* Be careful to only index valid IP protocols. */
490 if (pr
->pr_protocol
< IPPROTO_MAX
)
491 ip_protox
[pr
->pr_protocol
] = pr
;
495 /* IP fragment reassembly queue lock */
496 ipqlock_grp_attr
= lck_grp_attr_alloc_init();
497 ipqlock_grp
= lck_grp_alloc_init("ipqlock", ipqlock_grp_attr
);
498 ipqlock_attr
= lck_attr_alloc_init();
499 lck_mtx_init(&ipqlock
, ipqlock_grp
, ipqlock_attr
);
501 lck_mtx_lock(&ipqlock
);
502 /* Initialize IP reassembly queue. */
503 for (i
= 0; i
< IPREASS_NHASH
; i
++)
506 maxnipq
= nmbclusters
/ 32;
507 maxfragsperpacket
= 128; /* enough for 64k in 512 byte fragments */
509 lck_mtx_unlock(&ipqlock
);
512 ip_id
= RandomULong() ^ tv
.tv_usec
;
518 sadb_stat_mutex_grp_attr
= lck_grp_attr_alloc_init();
519 sadb_stat_mutex_grp
= lck_grp_alloc_init("sadb_stat",
520 sadb_stat_mutex_grp_attr
);
521 sadb_stat_mutex_attr
= lck_attr_alloc_init();
522 lck_mtx_init(sadb_stat_mutex
, sadb_stat_mutex_grp
,
523 sadb_stat_mutex_attr
);
530 * Initialize IPv4 source address hash table.
533 in_ifaddrhashtbl_init(void)
537 if (in_ifaddrhashtbl
!= NULL
)
540 PE_parse_boot_argn("inaddr_nhash", &inaddr_nhash
,
541 sizeof (inaddr_nhash
));
542 if (inaddr_nhash
== 0)
543 inaddr_nhash
= INADDR_NHASH
;
545 MALLOC(in_ifaddrhashtbl
, struct in_ifaddrhashhead
*,
546 inaddr_nhash
* sizeof (*in_ifaddrhashtbl
),
547 M_IFADDR
, M_WAITOK
| M_ZERO
);
548 if (in_ifaddrhashtbl
== NULL
)
549 panic("in_ifaddrhashtbl_init allocation failed");
552 * Generate the next largest prime greater than inaddr_nhash.
554 k
= (inaddr_nhash
% 2 == 0) ? inaddr_nhash
+ 1 : inaddr_nhash
+ 2;
557 for (i
= 3; i
* i
<= k
; i
+= 2) {
569 inaddr_hashval(u_int32_t key
)
572 * The hash index is the computed prime times the key modulo
573 * the hash size, as documented in "Introduction to Algorithms"
574 * (Cormen, Leiserson, Rivest).
576 if (inaddr_nhash
> 1)
577 return ((key
* inaddr_hashp
) % inaddr_nhash
);
583 ip_proto_dispatch_in_wrapper(struct mbuf
*m
, int hlen
, u_int8_t proto
)
585 ip_proto_dispatch_in(m
, hlen
, proto
, 0);
588 __private_extern__
void
589 ip_proto_dispatch_in(struct mbuf
*m
, int hlen
, u_int8_t proto
,
590 ipfilter_t inject_ipfref
)
592 struct ipfilter
*filter
;
593 int seen
= (inject_ipfref
== NULL
);
594 int changed_header
= 0;
596 void (*pr_input
)(struct mbuf
*, int len
);
598 if (!TAILQ_EMPTY(&ipv4_filters
)) {
600 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
602 if ((struct ipfilter
*)inject_ipfref
== filter
)
604 } else if (filter
->ipf_filter
.ipf_input
) {
607 if (changed_header
== 0) {
609 * Perform IP header alignment fixup,
610 * if needed, before passing packet
613 IP_HDR_ALIGNMENT_FIXUP(m
,
614 m
->m_pkthdr
.rcvif
, ipf_unref());
616 /* ipf_unref() already called */
621 ip
= mtod(m
, struct ip
*);
622 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
623 ip
->ip_off
= htons(ip
->ip_off
);
625 ip
->ip_sum
= ip_cksum_hdr_in(m
, hlen
);
627 result
= filter
->ipf_filter
.ipf_input(
628 filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
,
630 if (result
== EJUSTRETURN
) {
644 /* Perform IP header alignment fixup (post-filters), if needed */
645 IP_HDR_ALIGNMENT_FIXUP(m
, m
->m_pkthdr
.rcvif
, return);
648 * If there isn't a specific lock for the protocol
649 * we're about to call, use the generic lock for AF_INET.
650 * otherwise let the protocol deal with its own locking
652 ip
= mtod(m
, struct ip
*);
654 if (changed_header
) {
655 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
656 ip
->ip_off
= ntohs(ip
->ip_off
);
659 if ((pr_input
= ip_protox
[ip
->ip_p
]->pr_input
) == NULL
) {
661 } else if (!(ip_protox
[ip
->ip_p
]->pr_flags
& PR_PROTOLOCK
)) {
662 lck_mtx_lock(inet_domain_mutex
);
664 lck_mtx_unlock(inet_domain_mutex
);
670 struct pktchain_elm
{
671 struct mbuf
*pkte_head
;
672 struct mbuf
*pkte_tail
;
673 struct in_addr pkte_saddr
;
674 struct in_addr pkte_daddr
;
677 uint32_t pkte_nbytes
;
680 typedef struct pktchain_elm pktchain_elm_t
;
682 /* Store upto PKTTBL_SZ unique flows on the stack */
686 ip_chain_insert(struct mbuf
*packet
, pktchain_elm_t
*tbl
)
691 ip
= mtod(packet
, struct ip
*);
693 /* reusing the hash function from inaddr_hashval */
694 pkttbl_idx
= inaddr_hashval(ntohs(ip
->ip_src
.s_addr
)) % PKTTBL_SZ
;
695 if (tbl
[pkttbl_idx
].pkte_head
== NULL
) {
696 tbl
[pkttbl_idx
].pkte_head
= packet
;
697 tbl
[pkttbl_idx
].pkte_saddr
.s_addr
= ip
->ip_src
.s_addr
;
698 tbl
[pkttbl_idx
].pkte_daddr
.s_addr
= ip
->ip_dst
.s_addr
;
699 tbl
[pkttbl_idx
].pkte_proto
= ip
->ip_p
;
701 if ((ip
->ip_dst
.s_addr
== tbl
[pkttbl_idx
].pkte_daddr
.s_addr
) &&
702 (ip
->ip_src
.s_addr
== tbl
[pkttbl_idx
].pkte_saddr
.s_addr
) &&
703 (ip
->ip_p
== tbl
[pkttbl_idx
].pkte_proto
)) {
708 if (tbl
[pkttbl_idx
].pkte_tail
!= NULL
)
709 mbuf_setnextpkt(tbl
[pkttbl_idx
].pkte_tail
, packet
);
711 tbl
[pkttbl_idx
].pkte_tail
= packet
;
712 tbl
[pkttbl_idx
].pkte_npkts
+= 1;
713 tbl
[pkttbl_idx
].pkte_nbytes
+= packet
->m_pkthdr
.len
;
717 /* args is a dummy variable here for backward compatibility */
719 ip_input_second_pass_loop_tbl(pktchain_elm_t
*tbl
, struct ip_fw_in_args
*args
)
723 for (i
= 0; i
< PKTTBL_SZ
; i
++) {
724 if (tbl
[i
].pkte_head
!= NULL
) {
725 struct mbuf
*m
= tbl
[i
].pkte_head
;
726 ip_input_second_pass(m
, m
->m_pkthdr
.rcvif
, 0,
727 tbl
[i
].pkte_npkts
, tbl
[i
].pkte_nbytes
, args
, 0);
729 if (tbl
[i
].pkte_npkts
> 2)
730 ipstat
.ips_rxc_chainsz_gt2
++;
731 if (tbl
[i
].pkte_npkts
> 4)
732 ipstat
.ips_rxc_chainsz_gt4
++;
734 if (ip_input_measure
)
735 net_perf_histogram(&net_perf
, tbl
[i
].pkte_npkts
);
737 tbl
[i
].pkte_head
= tbl
[i
].pkte_tail
= NULL
;
738 tbl
[i
].pkte_npkts
= 0;
739 tbl
[i
].pkte_nbytes
= 0;
740 /* no need to initialize address and protocol in tbl */
746 ip_input_cpout_args(struct ip_fw_in_args
*args
, struct ip_fw_args
*args1
,
747 boolean_t
*done_init
)
749 if (*done_init
== FALSE
) {
750 bzero(args1
, sizeof(struct ip_fw_args
));
753 args1
->fwa_next_hop
= args
->fwai_next_hop
;
754 args1
->fwa_ipfw_rule
= args
->fwai_ipfw_rule
;
755 args1
->fwa_pf_rule
= args
->fwai_pf_rule
;
756 args1
->fwa_divert_rule
= args
->fwai_divert_rule
;
760 ip_input_cpin_args(struct ip_fw_args
*args1
, struct ip_fw_in_args
*args
)
762 args
->fwai_next_hop
= args1
->fwa_next_hop
;
763 args
->fwai_ipfw_rule
= args1
->fwa_ipfw_rule
;
764 args
->fwai_pf_rule
= args1
->fwa_pf_rule
;
765 args
->fwai_divert_rule
= args1
->fwa_divert_rule
;
773 } ipinput_chain_ret_t
;
776 ip_input_update_nstat(struct ifnet
*ifp
, struct in_addr src_ip
,
777 u_int32_t packets
, u_int32_t bytes
)
780 struct rtentry
*rt
= ifnet_cached_rtlookup_inet(ifp
,
783 nstat_route_rx(rt
, packets
, bytes
, 0);
790 ip_input_dispatch_chain(struct mbuf
*m
)
792 struct mbuf
*tmp_mbuf
= m
;
793 struct mbuf
*nxt_mbuf
= NULL
;
794 struct ip
*ip
= NULL
;
797 ip
= mtod(tmp_mbuf
, struct ip
*);
798 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
800 nxt_mbuf
= mbuf_nextpkt(tmp_mbuf
);
801 mbuf_setnextpkt(tmp_mbuf
, NULL
);
803 if ((sw_lro
) && (ip
->ip_p
== IPPROTO_TCP
))
804 tmp_mbuf
= tcp_lro(tmp_mbuf
, hlen
);
806 ip_proto_dispatch_in(tmp_mbuf
, hlen
, ip
->ip_p
, 0);
809 ip
= mtod(tmp_mbuf
, struct ip
*);
810 /* first mbuf of chain already has adjusted ip_len */
811 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
818 ip_input_setdst_chain(struct mbuf
*m
, uint32_t ifindex
, struct in_ifaddr
*ia
)
820 struct mbuf
*tmp_mbuf
= m
;
823 ip_setdstifaddr_info(tmp_mbuf
, ifindex
, ia
);
824 tmp_mbuf
= mbuf_nextpkt(tmp_mbuf
);
829 * First pass does all essential packet validation and places on a per flow
830 * queue for doing operations that have same outcome for all packets of a flow.
831 * div_info is packet divert/tee info
833 static ipinput_chain_ret_t
834 ip_input_first_pass(struct mbuf
*m
, u_int32_t
*div_info
,
835 struct ip_fw_in_args
*args
, int *ours
, struct mbuf
**modm
)
840 int retval
= IPINPUT_DOCHAIN
;
842 struct in_addr src_ip
;
846 #if IPFIREWALL || DUMMYNET
849 boolean_t
delete = FALSE
;
850 struct ip_fw_args args1
;
851 boolean_t init
= FALSE
;
853 ipfilter_t inject_filter_ref
= NULL
;
856 #pragma unused (args)
860 #pragma unused (div_info)
861 #pragma unused (ours)
864 #if !IPFIREWALL_FORWARD
865 #pragma unused (ours)
868 /* Check if the mbuf is still valid after interface filter processing */
869 MBUF_INPUT_CHECK(m
, m
->m_pkthdr
.rcvif
);
870 inifp
= mbuf_pkthdr_rcvif(m
);
871 VERIFY(inifp
!= NULL
);
873 /* Perform IP header alignment fixup, if needed */
874 IP_HDR_ALIGNMENT_FIXUP(m
, inifp
, goto bad
);
876 m
->m_pkthdr
.pkt_flags
&= ~PKTF_FORWARDED
;
878 #if IPFIREWALL || DUMMYNET
881 * Don't bother searching for tag(s) if there's none.
883 if (SLIST_EMPTY(&m
->m_pkthdr
.tags
))
886 /* Grab info from mtags prepended to the chain */
889 if (p
->m_tag_id
== KERNEL_MODULE_TAG_ID
) {
891 if (p
->m_tag_type
== KERNEL_TAG_TYPE_DUMMYNET
) {
892 struct dn_pkt_tag
*dn_tag
;
894 dn_tag
= (struct dn_pkt_tag
*)(p
+1);
895 args
->fwai_ipfw_rule
= dn_tag
->dn_ipfw_rule
;
896 args
->fwai_pf_rule
= dn_tag
->dn_pf_rule
;
902 if (p
->m_tag_type
== KERNEL_TAG_TYPE_DIVERT
) {
903 struct divert_tag
*div_tag
;
905 div_tag
= (struct divert_tag
*)(p
+1);
906 args
->fwai_divert_rule
= div_tag
->cookie
;
911 if (p
->m_tag_type
== KERNEL_TAG_TYPE_IPFORWARD
) {
912 struct ip_fwd_tag
*ipfwd_tag
;
914 ipfwd_tag
= (struct ip_fwd_tag
*)(p
+1);
915 args
->fwai_next_hop
= ipfwd_tag
->next_hop
;
921 p
= m_tag_next(m
, p
);
922 m_tag_delete(m
, copy
);
924 p
= m_tag_next(m
, p
);
927 p
= m_tag_next(m
, p
);
932 if (m
== NULL
|| !(m
->m_flags
& M_PKTHDR
))
933 panic("ip_input no HDR");
937 if (args
->fwai_ipfw_rule
|| args
->fwai_pf_rule
) {
938 /* dummynet already filtered us */
939 ip
= mtod(m
, struct ip
*);
940 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
941 inject_filter_ref
= ipf_get_inject_filter(m
);
943 if (args
->fwai_ipfw_rule
)
945 #endif /* IPFIREWALL */
946 if (args
->fwai_pf_rule
)
949 #endif /* DUMMYNET */
951 #endif /* IPFIREWALL || DUMMYNET */
954 * No need to process packet twice if we've already seen it.
956 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
))
957 inject_filter_ref
= ipf_get_inject_filter(m
);
958 if (inject_filter_ref
!= NULL
) {
959 ip
= mtod(m
, struct ip
*);
960 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
962 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
963 struct ip
*, ip
, struct ifnet
*, inifp
,
964 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
966 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
967 ip
->ip_off
= ntohs(ip
->ip_off
);
968 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, inject_filter_ref
);
969 return (IPINPUT_DONE
);
972 if (m
->m_pkthdr
.len
< sizeof (struct ip
)) {
973 OSAddAtomic(1, &ipstat
.ips_total
);
974 OSAddAtomic(1, &ipstat
.ips_tooshort
);
976 return (IPINPUT_FREED
);
979 if (m
->m_len
< sizeof (struct ip
) &&
980 (m
= m_pullup(m
, sizeof (struct ip
))) == NULL
) {
981 OSAddAtomic(1, &ipstat
.ips_total
);
982 OSAddAtomic(1, &ipstat
.ips_toosmall
);
983 return (IPINPUT_FREED
);
986 ip
= mtod(m
, struct ip
*);
989 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
, ip
->ip_src
.s_addr
,
990 ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
992 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
993 OSAddAtomic(1, &ipstat
.ips_total
);
994 OSAddAtomic(1, &ipstat
.ips_badvers
);
995 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
997 return (IPINPUT_FREED
);
1000 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1001 if (hlen
< sizeof (struct ip
)) {
1002 OSAddAtomic(1, &ipstat
.ips_total
);
1003 OSAddAtomic(1, &ipstat
.ips_badhlen
);
1004 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1006 return (IPINPUT_FREED
);
1009 if (hlen
> m
->m_len
) {
1010 if ((m
= m_pullup(m
, hlen
)) == NULL
) {
1011 OSAddAtomic(1, &ipstat
.ips_total
);
1012 OSAddAtomic(1, &ipstat
.ips_badhlen
);
1013 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1014 return (IPINPUT_FREED
);
1016 ip
= mtod(m
, struct ip
*);
1020 /* 127/8 must not appear on wire - RFC1122 */
1021 if ((ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
1022 (ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
) {
1024 * Allow for the following exceptions:
1026 * 1. If the packet was sent to loopback (i.e. rcvif
1027 * would have been set earlier at output time.)
1029 * 2. If the packet was sent out on loopback from a local
1030 * source address which belongs to a non-loopback
1031 * interface (i.e. rcvif may not necessarily be a
1032 * loopback interface, hence the test for PKTF_LOOP.)
1033 * Unlike IPv6, there is no interface scope ID, and
1034 * therefore we don't care so much about PKTF_IFINFO.
1036 if (!(inifp
->if_flags
& IFF_LOOPBACK
) &&
1037 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
1038 OSAddAtomic(1, &ipstat
.ips_total
);
1039 OSAddAtomic(1, &ipstat
.ips_badaddr
);
1040 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1042 return (IPINPUT_FREED
);
1046 /* IPv4 Link-Local Addresses as defined in RFC3927 */
1047 if ((IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
)) ||
1048 IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)))) {
1049 ip_linklocal_stat
.iplls_in_total
++;
1050 if (ip
->ip_ttl
!= MAXTTL
) {
1051 OSAddAtomic(1, &ip_linklocal_stat
.iplls_in_badttl
);
1052 /* Silently drop link local traffic with bad TTL */
1053 if (!ip_linklocal_in_allowbadttl
) {
1054 OSAddAtomic(1, &ipstat
.ips_total
);
1055 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1057 return (IPINPUT_FREED
);
1062 if (ip_cksum(m
, hlen
)) {
1063 OSAddAtomic(1, &ipstat
.ips_total
);
1064 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1066 return (IPINPUT_FREED
);
1069 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1070 struct ip
*, ip
, struct ifnet
*, inifp
,
1071 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
1074 * Convert fields to host representation.
1076 #if BYTE_ORDER != BIG_ENDIAN
1080 if (ip
->ip_len
< hlen
) {
1081 OSAddAtomic(1, &ipstat
.ips_total
);
1082 OSAddAtomic(1, &ipstat
.ips_badlen
);
1083 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1085 return (IPINPUT_FREED
);
1088 #if BYTE_ORDER != BIG_ENDIAN
1093 * Check that the amount of data in the buffers
1094 * is as at least much as the IP header would have us expect.
1095 * Trim mbufs if longer than we expect.
1096 * Drop packet if shorter than we expect.
1098 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
1099 OSAddAtomic(1, &ipstat
.ips_total
);
1100 OSAddAtomic(1, &ipstat
.ips_tooshort
);
1101 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1103 return (IPINPUT_FREED
);
1106 if (m
->m_pkthdr
.len
> ip
->ip_len
) {
1108 * Invalidate hardware checksum info if ip_adj_clear_hwcksum
1109 * is set; useful to handle buggy drivers. Note that this
1110 * should not be enabled by default, as we may get here due
1111 * to link-layer padding.
1113 if (ip_adj_clear_hwcksum
&&
1114 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
) &&
1115 !(inifp
->if_flags
& IFF_LOOPBACK
) &&
1116 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
1117 m
->m_pkthdr
.csum_flags
&= ~CSUM_DATA_VALID
;
1118 m
->m_pkthdr
.csum_data
= 0;
1119 ipstat
.ips_adj_hwcsum_clr
++;
1123 if (m
->m_len
== m
->m_pkthdr
.len
) {
1124 m
->m_len
= ip
->ip_len
;
1125 m
->m_pkthdr
.len
= ip
->ip_len
;
1127 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
1130 /* for consistency */
1131 m
->m_pkthdr
.pkt_proto
= ip
->ip_p
;
1133 /* for netstat route statistics */
1134 src_ip
= ip
->ip_src
;
1135 len
= m
->m_pkthdr
.len
;
1141 /* Invoke inbound packet filter */
1142 if (PF_IS_ENABLED
) {
1144 ip_input_cpout_args(args
, &args1
, &init
);
1147 error
= pf_af_hook(inifp
, NULL
, &m
, AF_INET
, TRUE
, &args1
);
1149 error
= pf_af_hook(inifp
, NULL
, &m
, AF_INET
, TRUE
, NULL
);
1150 #endif /* DUMMYNET */
1151 if (error
!= 0 || m
== NULL
) {
1153 panic("%s: unexpected packet %p\n",
1157 /* Already freed by callee */
1158 ip_input_update_nstat(inifp
, src_ip
, 1, len
);
1159 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1160 OSAddAtomic(1, &ipstat
.ips_total
);
1161 return (IPINPUT_FREED
);
1163 ip
= mtod(m
, struct ip
*);
1164 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1166 ip_input_cpin_args(&args1
, args
);
1171 if (ipsec_bypass
== 0 && ipsec_gethist(m
, NULL
)) {
1172 retval
= IPINPUT_DONTCHAIN
; /* XXX scope for chaining here? */
1180 #endif /* DUMMYNET */
1182 * Check if we want to allow this packet to be processed.
1183 * Consider it to be bad if not.
1185 if (fw_enable
&& IPFW_LOADED
) {
1186 #if IPFIREWALL_FORWARD
1188 * If we've been forwarded from the output side, then
1189 * skip the firewall a second time
1191 if (args
->fwai_next_hop
) {
1193 return (IPINPUT_DONTCHAIN
);
1195 #endif /* IPFIREWALL_FORWARD */
1196 ip_input_cpout_args(args
, &args1
, &init
);
1199 i
= ip_fw_chk_ptr(&args1
);
1202 if ((i
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) { /* drop */
1205 ip_input_update_nstat(inifp
, src_ip
, 1, len
);
1206 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1207 OSAddAtomic(1, &ipstat
.ips_total
);
1208 return (IPINPUT_FREED
);
1210 ip
= mtod(m
, struct ip
*); /* just in case m changed */
1212 ip_input_cpin_args(&args1
, args
);
1214 if (i
== 0 && args
->fwai_next_hop
== NULL
) { /* common case */
1218 if (DUMMYNET_LOADED
&& (i
& IP_FW_PORT_DYNT_FLAG
) != 0) {
1219 /* Send packet to the appropriate pipe */
1220 ip_dn_io_ptr(m
, i
&0xffff, DN_TO_IP_IN
, &args1
,
1222 ip_input_update_nstat(inifp
, src_ip
, 1, len
);
1223 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1224 OSAddAtomic(1, &ipstat
.ips_total
);
1225 return (IPINPUT_FREED
);
1227 #endif /* DUMMYNET */
1229 if (i
!= 0 && (i
& IP_FW_PORT_DYNT_FLAG
) == 0) {
1230 /* Divert or tee packet */
1233 return (IPINPUT_DONTCHAIN
);
1236 #if IPFIREWALL_FORWARD
1237 if (i
== 0 && args
->fwai_next_hop
!= NULL
) {
1238 retval
= IPINPUT_DONTCHAIN
;
1243 * if we get here, the packet must be dropped
1245 ip_input_update_nstat(inifp
, src_ip
, 1, len
);
1246 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1248 OSAddAtomic(1, &ipstat
.ips_total
);
1249 return (IPINPUT_FREED
);
1251 #endif /* IPFIREWALL */
1252 #if IPSEC | IPFIREWALL
1256 * Process options and, if not destined for us,
1257 * ship it on. ip_dooptions returns 1 when an
1258 * error was detected (causing an icmp message
1259 * to be sent and the original packet to be freed).
1261 ip_nhops
= 0; /* for source routed packets */
1263 if (hlen
> sizeof (struct ip
) &&
1264 ip_dooptions(m
, 0, args
->fwai_next_hop
)) {
1265 #else /* !IPFIREWALL */
1266 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, NULL
)) {
1267 #endif /* !IPFIREWALL */
1268 ip_input_update_nstat(inifp
, src_ip
, 1, len
);
1269 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1270 OSAddAtomic(1, &ipstat
.ips_total
);
1271 return (IPINPUT_FREED
);
1275 * Don't chain fragmented packets as the process of determining
1276 * if it is our fragment or someone else's plus the complexity of
1277 * divert and fw args makes it harder to do chaining.
1279 if (ip
->ip_off
& ~(IP_DF
| IP_RF
))
1280 return (IPINPUT_DONTCHAIN
);
1282 /* Allow DHCP/BootP responses through */
1283 if ((inifp
->if_eflags
& IFEF_AUTOCONFIGURING
) &&
1284 hlen
== sizeof (struct ip
) && ip
->ip_p
== IPPROTO_UDP
) {
1285 struct udpiphdr
*ui
;
1287 if (m
->m_len
< sizeof (struct udpiphdr
) &&
1288 (m
= m_pullup(m
, sizeof (struct udpiphdr
))) == NULL
) {
1289 OSAddAtomic(1, &udpstat
.udps_hdrops
);
1290 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1291 OSAddAtomic(1, &ipstat
.ips_total
);
1292 return (IPINPUT_FREED
);
1295 ui
= mtod(m
, struct udpiphdr
*);
1296 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) {
1297 ip_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
1298 return (IPINPUT_DONTCHAIN
);
1302 /* Avoid chaining raw sockets as ipsec checks occur later for them */
1303 if (ip_protox
[ip
->ip_p
]->pr_flags
& PR_LASTHDR
)
1304 return (IPINPUT_DONTCHAIN
);
1307 #if !defined(__i386__) && !defined(__x86_64__)
1310 return (IPINPUT_FREED
);
1315 ip_input_second_pass(struct mbuf
*m
, struct ifnet
*inifp
, u_int32_t div_info
,
1316 int npkts_in_chain
, int bytes_in_chain
, struct ip_fw_in_args
*args
, int ours
)
1318 unsigned int checkif
;
1319 struct mbuf
*tmp_mbuf
= NULL
;
1320 struct in_ifaddr
*ia
= NULL
;
1321 struct in_addr pkt_dst
;
1325 #pragma unused (args)
1329 #pragma unused (div_info)
1332 struct ip
*ip
= mtod(m
, struct ip
*);
1333 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1335 OSAddAtomic(npkts_in_chain
, &ipstat
.ips_total
);
1338 * Naively assume we can attribute inbound data to the route we would
1339 * use to send to this destination. Asymmetric routing breaks this
1340 * assumption, but it still allows us to account for traffic from
1341 * a remote node in the routing table.
1342 * this has a very significant performance impact so we bypass
1343 * if nstat_collect is disabled. We may also bypass if the
1344 * protocol is tcp in the future because tcp will have a route that
1345 * we can use to attribute the data to. That does mean we would not
1346 * account for forwarded tcp traffic.
1348 ip_input_update_nstat(inifp
, ip
->ip_src
, npkts_in_chain
,
1355 * Check our list of addresses, to see if the packet is for us.
1356 * If we don't have any addresses, assume any unicast packet
1357 * we receive might be for us (and let the upper layers deal
1361 if (TAILQ_EMPTY(&in_ifaddrhead
)) {
1363 if (!(tmp_mbuf
->m_flags
& (M_MCAST
|M_BCAST
))) {
1364 ip_setdstifaddr_info(tmp_mbuf
, inifp
->if_index
,
1367 tmp_mbuf
= mbuf_nextpkt(tmp_mbuf
);
1372 * Cache the destination address of the packet; this may be
1373 * changed by use of 'ipfw fwd'.
1376 pkt_dst
= args
->fwai_next_hop
== NULL
?
1377 ip
->ip_dst
: args
->fwai_next_hop
->sin_addr
;
1378 #else /* !IPFIREWALL */
1379 pkt_dst
= ip
->ip_dst
;
1380 #endif /* !IPFIREWALL */
1383 * Enable a consistency check between the destination address
1384 * and the arrival interface for a unicast packet (the RFC 1122
1385 * strong ES model) if IP forwarding is disabled and the packet
1386 * is not locally generated and the packet is not subject to
1389 * XXX - Checking also should be disabled if the destination
1390 * address is ipnat'ed to a different interface.
1392 * XXX - Checking is incompatible with IP aliases added
1393 * to the loopback interface instead of the interface where
1394 * the packets are received.
1396 checkif
= ip_checkinterface
&& (ipforwarding
== 0) &&
1397 !(inifp
->if_flags
& IFF_LOOPBACK
) &&
1398 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)
1400 && (args
->fwai_next_hop
== NULL
);
1401 #else /* !IPFIREWALL */
1403 #endif /* !IPFIREWALL */
1406 * Check for exact addresses in the hash bucket.
1408 lck_rw_lock_shared(in_ifaddr_rwlock
);
1409 TAILQ_FOREACH(ia
, INADDR_HASH(pkt_dst
.s_addr
), ia_hash
) {
1411 * If the address matches, verify that the packet
1412 * arrived via the correct interface if checking is
1415 if (IA_SIN(ia
)->sin_addr
.s_addr
== pkt_dst
.s_addr
&&
1416 (!checkif
|| ia
->ia_ifp
== inifp
)) {
1417 ip_input_setdst_chain(m
, 0, ia
);
1418 lck_rw_done(in_ifaddr_rwlock
);
1422 lck_rw_done(in_ifaddr_rwlock
);
1425 * Check for broadcast addresses.
1427 * Only accept broadcast packets that arrive via the matching
1428 * interface. Reception of forwarded directed broadcasts would be
1429 * handled via ip_forward() and ether_frameout() with the loopback
1430 * into the stack for SIMPLEX interfaces handled by ether_frameout().
1432 if (inifp
->if_flags
& IFF_BROADCAST
) {
1435 ifnet_lock_shared(inifp
);
1436 TAILQ_FOREACH(ifa
, &inifp
->if_addrhead
, ifa_link
) {
1437 if (ifa
->ifa_addr
->sa_family
!= AF_INET
) {
1441 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
1442 pkt_dst
.s_addr
|| ia
->ia_netbroadcast
.s_addr
==
1444 ip_input_setdst_chain(m
, 0, ia
);
1445 ifnet_lock_done(inifp
);
1449 ifnet_lock_done(inifp
);
1452 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
1453 struct in_multi
*inm
;
1455 * See if we belong to the destination multicast group on the
1456 * arrival interface.
1458 in_multihead_lock_shared();
1459 IN_LOOKUP_MULTI(&ip
->ip_dst
, inifp
, inm
);
1460 in_multihead_lock_done();
1462 OSAddAtomic(npkts_in_chain
, &ipstat
.ips_notmember
);
1464 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1467 ip_input_setdst_chain(m
, inifp
->if_index
, NULL
);
1472 if (ip
->ip_dst
.s_addr
== (u_int32_t
)INADDR_BROADCAST
||
1473 ip
->ip_dst
.s_addr
== INADDR_ANY
) {
1474 ip_input_setdst_chain(m
, inifp
->if_index
, NULL
);
1478 if (ip
->ip_p
== IPPROTO_UDP
) {
1479 struct udpiphdr
*ui
;
1480 ui
= mtod(m
, struct udpiphdr
*);
1481 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) {
1487 struct mbuf
*nxt_mbuf
= NULL
;
1489 nxt_mbuf
= mbuf_nextpkt(tmp_mbuf
);
1491 * Not for us; forward if possible and desirable.
1493 mbuf_setnextpkt(tmp_mbuf
, NULL
);
1494 if (ipforwarding
== 0) {
1495 OSAddAtomic(1, &ipstat
.ips_cantforward
);
1499 ip_forward(tmp_mbuf
, 0, args
->fwai_next_hop
);
1501 ip_forward(tmp_mbuf
, 0, NULL
);
1504 tmp_mbuf
= nxt_mbuf
;
1506 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1510 * If offset or IP_MF are set, must reassemble.
1512 if (ip
->ip_off
& ~(IP_DF
| IP_RF
)) {
1513 VERIFY(npkts_in_chain
== 1);
1515 * ip_reass() will return a different mbuf, and update
1516 * the divert info in div_info and args->fwai_divert_rule.
1519 m
= ip_reass(m
, (u_int16_t
*)&div_info
, &args
->fwai_divert_rule
);
1525 ip
= mtod(m
, struct ip
*);
1526 /* Get the header length of the reassembled packet */
1527 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1529 /* Restore original checksum before diverting packet */
1530 if (div_info
!= 0) {
1531 VERIFY(npkts_in_chain
== 1);
1532 #if BYTE_ORDER != BIG_ENDIAN
1537 ip
->ip_sum
= ip_cksum_hdr_in(m
, hlen
);
1538 #if BYTE_ORDER != BIG_ENDIAN
1547 * Further protocols expect the packet length to be w/o the
1554 * Divert or tee packet to the divert protocol if required.
1556 * If div_info is zero then cookie should be too, so we shouldn't
1557 * need to clear them here. Assume divert_packet() does so also.
1559 if (div_info
!= 0) {
1560 struct mbuf
*clone
= NULL
;
1561 VERIFY(npkts_in_chain
== 1);
1563 /* Clone packet if we're doing a 'tee' */
1564 if (div_info
& IP_FW_PORT_TEE_FLAG
)
1565 clone
= m_dup(m
, M_DONTWAIT
);
1567 /* Restore packet header fields to original values */
1570 #if BYTE_ORDER != BIG_ENDIAN
1574 /* Deliver packet to divert input routine */
1575 OSAddAtomic(1, &ipstat
.ips_delivered
);
1576 divert_packet(m
, 1, div_info
& 0xffff, args
->fwai_divert_rule
);
1578 /* If 'tee', continue with original packet */
1579 if (clone
== NULL
) {
1583 ip
= mtod(m
, struct ip
*);
1589 * enforce IPsec policy checking if we are seeing last header.
1590 * note that we do not visit this with protocols with pcb layer
1591 * code - like udp/tcp/raw ip.
1593 if (ipsec_bypass
== 0 && (ip_protox
[ip
->ip_p
]->pr_flags
& PR_LASTHDR
)) {
1594 VERIFY(npkts_in_chain
== 1);
1595 if (ipsec4_in_reject(m
, NULL
)) {
1596 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
1603 * Switch out to protocol's input routine.
1605 OSAddAtomic(npkts_in_chain
, &ipstat
.ips_delivered
);
1608 if (args
->fwai_next_hop
&& ip
->ip_p
== IPPROTO_TCP
) {
1609 /* TCP needs IPFORWARD info if available */
1610 struct m_tag
*fwd_tag
;
1611 struct ip_fwd_tag
*ipfwd_tag
;
1613 VERIFY(npkts_in_chain
== 1);
1614 fwd_tag
= m_tag_create(KERNEL_MODULE_TAG_ID
,
1615 KERNEL_TAG_TYPE_IPFORWARD
, sizeof (*ipfwd_tag
),
1617 if (fwd_tag
== NULL
)
1620 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
1621 ipfwd_tag
->next_hop
= args
->fwai_next_hop
;
1623 m_tag_prepend(m
, fwd_tag
);
1625 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1626 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1628 /* TCP deals with its own locking */
1629 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1631 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1632 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1634 ip_input_dispatch_chain(m
);
1637 #else /* !IPFIREWALL */
1638 ip_input_dispatch_chain(m
);
1640 #endif /* !IPFIREWALL */
1641 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1644 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1649 ip_input_process_list(struct mbuf
*packet_list
)
1651 pktchain_elm_t pktchain_tbl
[PKTTBL_SZ
];
1653 struct mbuf
*packet
= NULL
;
1654 struct mbuf
*modm
= NULL
; /* modified mbuf */
1656 u_int32_t div_info
= 0;
1658 struct timeval start_tv
;
1661 struct ip_fw_in_args args
;
1663 if (ip_chaining
== 0) {
1664 struct mbuf
*m
= packet_list
;
1665 if (ip_input_measure
)
1666 net_perf_start_time(&net_perf
, &start_tv
);
1668 packet_list
= mbuf_nextpkt(m
);
1669 mbuf_setnextpkt(m
, NULL
);
1674 if (ip_input_measure
)
1675 net_perf_measure_time(&net_perf
, &start_tv
, num_pkts
);
1678 if (ip_input_measure
)
1679 net_perf_start_time(&net_perf
, &start_tv
);
1681 bzero(&pktchain_tbl
, sizeof(pktchain_tbl
));
1682 restart_list_process
:
1684 for (packet
= packet_list
; packet
; packet
= packet_list
) {
1685 packet_list
= mbuf_nextpkt(packet
);
1686 mbuf_setnextpkt(packet
, NULL
);
1691 bzero(&args
, sizeof (args
));
1693 retval
= ip_input_first_pass(packet
, &div_info
, &args
,
1696 if (retval
== IPINPUT_DOCHAIN
) {
1699 packet
= ip_chain_insert(packet
, &pktchain_tbl
[0]);
1700 if (packet
== NULL
) {
1701 ipstat
.ips_rxc_chained
++;
1703 if (chain
> ip_chainsz
)
1706 ipstat
.ips_rxc_collisions
++;
1709 } else if (retval
== IPINPUT_DONTCHAIN
) {
1710 /* in order to preserve order, exit from chaining */
1713 ipstat
.ips_rxc_notchain
++;
1716 /* packet was freed or delivered, do nothing. */
1720 /* do second pass here for pktchain_tbl */
1722 ip_input_second_pass_loop_tbl(&pktchain_tbl
[0], &args
);
1726 * equivalent update in chaining case if performed in
1727 * ip_input_second_pass_loop_tbl().
1729 if (ip_input_measure
)
1730 net_perf_histogram(&net_perf
, 1);
1732 ip_input_second_pass(packet
, packet
->m_pkthdr
.rcvif
, div_info
,
1733 1, packet
->m_pkthdr
.len
, &args
, ours
);
1737 goto restart_list_process
;
1739 if (ip_input_measure
)
1740 net_perf_measure_time(&net_perf
, &start_tv
, num_pkts
);
1743 * Ip input routine. Checksum and byte swap header. If fragmented
1744 * try to reassemble. Process options. Pass to next level.
1747 ip_input(struct mbuf
*m
)
1750 struct in_ifaddr
*ia
= NULL
;
1751 unsigned int hlen
, checkif
;
1753 struct in_addr pkt_dst
;
1756 u_int32_t div_info
= 0; /* packet divert/tee info */
1758 #if IPFIREWALL || DUMMYNET
1759 struct ip_fw_args args
;
1762 ipfilter_t inject_filter_ref
= NULL
;
1763 struct ifnet
*inifp
;
1765 /* Check if the mbuf is still valid after interface filter processing */
1766 MBUF_INPUT_CHECK(m
, m
->m_pkthdr
.rcvif
);
1767 inifp
= m
->m_pkthdr
.rcvif
;
1768 VERIFY(inifp
!= NULL
);
1770 ipstat
.ips_rxc_notlist
++;
1772 /* Perform IP header alignment fixup, if needed */
1773 IP_HDR_ALIGNMENT_FIXUP(m
, inifp
, goto bad
);
1775 m
->m_pkthdr
.pkt_flags
&= ~PKTF_FORWARDED
;
1777 #if IPFIREWALL || DUMMYNET
1778 bzero(&args
, sizeof (struct ip_fw_args
));
1781 * Don't bother searching for tag(s) if there's none.
1783 if (SLIST_EMPTY(&m
->m_pkthdr
.tags
))
1784 goto ipfw_tags_done
;
1786 /* Grab info from mtags prepended to the chain */
1788 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
1789 KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
1790 struct dn_pkt_tag
*dn_tag
;
1792 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
1793 args
.fwa_ipfw_rule
= dn_tag
->dn_ipfw_rule
;
1794 args
.fwa_pf_rule
= dn_tag
->dn_pf_rule
;
1796 m_tag_delete(m
, tag
);
1798 #endif /* DUMMYNET */
1801 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
1802 KERNEL_TAG_TYPE_DIVERT
, NULL
)) != NULL
) {
1803 struct divert_tag
*div_tag
;
1805 div_tag
= (struct divert_tag
*)(tag
+1);
1806 args
.fwa_divert_rule
= div_tag
->cookie
;
1808 m_tag_delete(m
, tag
);
1812 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
1813 KERNEL_TAG_TYPE_IPFORWARD
, NULL
)) != NULL
) {
1814 struct ip_fwd_tag
*ipfwd_tag
;
1816 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
1817 args
.fwa_next_hop
= ipfwd_tag
->next_hop
;
1819 m_tag_delete(m
, tag
);
1823 if (m
== NULL
|| !(m
->m_flags
& M_PKTHDR
))
1824 panic("ip_input no HDR");
1828 if (args
.fwa_ipfw_rule
|| args
.fwa_pf_rule
) {
1829 /* dummynet already filtered us */
1830 ip
= mtod(m
, struct ip
*);
1831 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1832 inject_filter_ref
= ipf_get_inject_filter(m
);
1834 if (args
.fwa_ipfw_rule
)
1836 #endif /* IPFIREWALL */
1837 if (args
.fwa_pf_rule
)
1840 #endif /* DUMMYNET */
1842 #endif /* IPFIREWALL || DUMMYNET */
1845 * No need to process packet twice if we've already seen it.
1847 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
))
1848 inject_filter_ref
= ipf_get_inject_filter(m
);
1849 if (inject_filter_ref
!= NULL
) {
1850 ip
= mtod(m
, struct ip
*);
1851 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1853 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1854 struct ip
*, ip
, struct ifnet
*, inifp
,
1855 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
1857 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
1858 ip
->ip_off
= ntohs(ip
->ip_off
);
1859 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, inject_filter_ref
);
1863 OSAddAtomic(1, &ipstat
.ips_total
);
1864 if (m
->m_pkthdr
.len
< sizeof (struct ip
))
1867 if (m
->m_len
< sizeof (struct ip
) &&
1868 (m
= m_pullup(m
, sizeof (struct ip
))) == NULL
) {
1869 OSAddAtomic(1, &ipstat
.ips_toosmall
);
1872 ip
= mtod(m
, struct ip
*);
1874 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
, ip
->ip_src
.s_addr
,
1875 ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1877 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
1878 OSAddAtomic(1, &ipstat
.ips_badvers
);
1882 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1883 if (hlen
< sizeof (struct ip
)) { /* minimum header length */
1884 OSAddAtomic(1, &ipstat
.ips_badhlen
);
1887 if (hlen
> m
->m_len
) {
1888 if ((m
= m_pullup(m
, hlen
)) == NULL
) {
1889 OSAddAtomic(1, &ipstat
.ips_badhlen
);
1892 ip
= mtod(m
, struct ip
*);
1895 /* 127/8 must not appear on wire - RFC1122 */
1896 if ((ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
1897 (ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
) {
1899 * Allow for the following exceptions:
1901 * 1. If the packet was sent to loopback (i.e. rcvif
1902 * would have been set earlier at output time.)
1904 * 2. If the packet was sent out on loopback from a local
1905 * source address which belongs to a non-loopback
1906 * interface (i.e. rcvif may not necessarily be a
1907 * loopback interface, hence the test for PKTF_LOOP.)
1908 * Unlike IPv6, there is no interface scope ID, and
1909 * therefore we don't care so much about PKTF_IFINFO.
1911 if (!(inifp
->if_flags
& IFF_LOOPBACK
) &&
1912 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
1913 OSAddAtomic(1, &ipstat
.ips_badaddr
);
1918 /* IPv4 Link-Local Addresses as defined in RFC3927 */
1919 if ((IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
)) ||
1920 IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)))) {
1921 ip_linklocal_stat
.iplls_in_total
++;
1922 if (ip
->ip_ttl
!= MAXTTL
) {
1923 OSAddAtomic(1, &ip_linklocal_stat
.iplls_in_badttl
);
1924 /* Silently drop link local traffic with bad TTL */
1925 if (!ip_linklocal_in_allowbadttl
)
1930 sum
= ip_cksum(m
, hlen
);
1935 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1936 struct ip
*, ip
, struct ifnet
*, inifp
,
1937 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
1940 * Naively assume we can attribute inbound data to the route we would
1941 * use to send to this destination. Asymmetric routing breaks this
1942 * assumption, but it still allows us to account for traffic from
1943 * a remote node in the routing table.
1944 * this has a very significant performance impact so we bypass
1945 * if nstat_collect is disabled. We may also bypass if the
1946 * protocol is tcp in the future because tcp will have a route that
1947 * we can use to attribute the data to. That does mean we would not
1948 * account for forwarded tcp traffic.
1950 if (nstat_collect
) {
1951 struct rtentry
*rt
=
1952 ifnet_cached_rtlookup_inet(inifp
, ip
->ip_src
);
1954 nstat_route_rx(rt
, 1, m
->m_pkthdr
.len
, 0);
1960 * Convert fields to host representation.
1962 #if BYTE_ORDER != BIG_ENDIAN
1966 if (ip
->ip_len
< hlen
) {
1967 OSAddAtomic(1, &ipstat
.ips_badlen
);
1971 #if BYTE_ORDER != BIG_ENDIAN
1975 * Check that the amount of data in the buffers
1976 * is as at least much as the IP header would have us expect.
1977 * Trim mbufs if longer than we expect.
1978 * Drop packet if shorter than we expect.
1980 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
1982 OSAddAtomic(1, &ipstat
.ips_tooshort
);
1985 if (m
->m_pkthdr
.len
> ip
->ip_len
) {
1987 * Invalidate hardware checksum info if ip_adj_clear_hwcksum
1988 * is set; useful to handle buggy drivers. Note that this
1989 * should not be enabled by default, as we may get here due
1990 * to link-layer padding.
1992 if (ip_adj_clear_hwcksum
&&
1993 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
) &&
1994 !(inifp
->if_flags
& IFF_LOOPBACK
) &&
1995 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
1996 m
->m_pkthdr
.csum_flags
&= ~CSUM_DATA_VALID
;
1997 m
->m_pkthdr
.csum_data
= 0;
1998 ipstat
.ips_adj_hwcsum_clr
++;
2002 if (m
->m_len
== m
->m_pkthdr
.len
) {
2003 m
->m_len
= ip
->ip_len
;
2004 m
->m_pkthdr
.len
= ip
->ip_len
;
2006 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
2009 /* for consistency */
2010 m
->m_pkthdr
.pkt_proto
= ip
->ip_p
;
2016 /* Invoke inbound packet filter */
2017 if (PF_IS_ENABLED
) {
2020 error
= pf_af_hook(inifp
, NULL
, &m
, AF_INET
, TRUE
, &args
);
2022 error
= pf_af_hook(inifp
, NULL
, &m
, AF_INET
, TRUE
, NULL
);
2023 #endif /* DUMMYNET */
2024 if (error
!= 0 || m
== NULL
) {
2026 panic("%s: unexpected packet %p\n",
2030 /* Already freed by callee */
2033 ip
= mtod(m
, struct ip
*);
2034 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
2039 if (ipsec_bypass
== 0 && ipsec_gethist(m
, NULL
))
2046 #endif /* DUMMYNET */
2048 * Check if we want to allow this packet to be processed.
2049 * Consider it to be bad if not.
2051 if (fw_enable
&& IPFW_LOADED
) {
2052 #if IPFIREWALL_FORWARD
2054 * If we've been forwarded from the output side, then
2055 * skip the firewall a second time
2057 if (args
.fwa_next_hop
)
2059 #endif /* IPFIREWALL_FORWARD */
2063 i
= ip_fw_chk_ptr(&args
);
2066 if ((i
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) { /* drop */
2071 ip
= mtod(m
, struct ip
*); /* just in case m changed */
2073 if (i
== 0 && args
.fwa_next_hop
== NULL
) { /* common case */
2077 if (DUMMYNET_LOADED
&& (i
& IP_FW_PORT_DYNT_FLAG
) != 0) {
2078 /* Send packet to the appropriate pipe */
2079 ip_dn_io_ptr(m
, i
&0xffff, DN_TO_IP_IN
, &args
,
2083 #endif /* DUMMYNET */
2085 if (i
!= 0 && (i
& IP_FW_PORT_DYNT_FLAG
) == 0) {
2086 /* Divert or tee packet */
2091 #if IPFIREWALL_FORWARD
2092 if (i
== 0 && args
.fwa_next_hop
!= NULL
) {
2097 * if we get here, the packet must be dropped
2102 #endif /* IPFIREWALL */
2103 #if IPSEC | IPFIREWALL
2107 * Process options and, if not destined for us,
2108 * ship it on. ip_dooptions returns 1 when an
2109 * error was detected (causing an icmp message
2110 * to be sent and the original packet to be freed).
2112 ip_nhops
= 0; /* for source routed packets */
2114 if (hlen
> sizeof (struct ip
) &&
2115 ip_dooptions(m
, 0, args
.fwa_next_hop
)) {
2116 #else /* !IPFIREWALL */
2117 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, NULL
)) {
2118 #endif /* !IPFIREWALL */
2123 * Check our list of addresses, to see if the packet is for us.
2124 * If we don't have any addresses, assume any unicast packet
2125 * we receive might be for us (and let the upper layers deal
2128 if (TAILQ_EMPTY(&in_ifaddrhead
) && !(m
->m_flags
& (M_MCAST
|M_BCAST
))) {
2129 ip_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
2134 * Cache the destination address of the packet; this may be
2135 * changed by use of 'ipfw fwd'.
2138 pkt_dst
= args
.fwa_next_hop
== NULL
?
2139 ip
->ip_dst
: args
.fwa_next_hop
->sin_addr
;
2140 #else /* !IPFIREWALL */
2141 pkt_dst
= ip
->ip_dst
;
2142 #endif /* !IPFIREWALL */
2145 * Enable a consistency check between the destination address
2146 * and the arrival interface for a unicast packet (the RFC 1122
2147 * strong ES model) if IP forwarding is disabled and the packet
2148 * is not locally generated and the packet is not subject to
2151 * XXX - Checking also should be disabled if the destination
2152 * address is ipnat'ed to a different interface.
2154 * XXX - Checking is incompatible with IP aliases added
2155 * to the loopback interface instead of the interface where
2156 * the packets are received.
2158 checkif
= ip_checkinterface
&& (ipforwarding
== 0) &&
2159 !(inifp
->if_flags
& IFF_LOOPBACK
) &&
2160 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)
2162 && (args
.fwa_next_hop
== NULL
);
2163 #else /* !IPFIREWALL */
2165 #endif /* !IPFIREWALL */
2168 * Check for exact addresses in the hash bucket.
2170 lck_rw_lock_shared(in_ifaddr_rwlock
);
2171 TAILQ_FOREACH(ia
, INADDR_HASH(pkt_dst
.s_addr
), ia_hash
) {
2173 * If the address matches, verify that the packet
2174 * arrived via the correct interface if checking is
2177 if (IA_SIN(ia
)->sin_addr
.s_addr
== pkt_dst
.s_addr
&&
2178 (!checkif
|| ia
->ia_ifp
== inifp
)) {
2179 ip_setdstifaddr_info(m
, 0, ia
);
2180 lck_rw_done(in_ifaddr_rwlock
);
2184 lck_rw_done(in_ifaddr_rwlock
);
2187 * Check for broadcast addresses.
2189 * Only accept broadcast packets that arrive via the matching
2190 * interface. Reception of forwarded directed broadcasts would be
2191 * handled via ip_forward() and ether_frameout() with the loopback
2192 * into the stack for SIMPLEX interfaces handled by ether_frameout().
2194 if (inifp
->if_flags
& IFF_BROADCAST
) {
2197 ifnet_lock_shared(inifp
);
2198 TAILQ_FOREACH(ifa
, &inifp
->if_addrhead
, ifa_link
) {
2199 if (ifa
->ifa_addr
->sa_family
!= AF_INET
) {
2203 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
2204 pkt_dst
.s_addr
|| ia
->ia_netbroadcast
.s_addr
==
2206 ip_setdstifaddr_info(m
, 0, ia
);
2207 ifnet_lock_done(inifp
);
2211 ifnet_lock_done(inifp
);
2214 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
2215 struct in_multi
*inm
;
2217 * See if we belong to the destination multicast group on the
2218 * arrival interface.
2220 in_multihead_lock_shared();
2221 IN_LOOKUP_MULTI(&ip
->ip_dst
, inifp
, inm
);
2222 in_multihead_lock_done();
2224 OSAddAtomic(1, &ipstat
.ips_notmember
);
2228 ip_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
2232 if (ip
->ip_dst
.s_addr
== (u_int32_t
)INADDR_BROADCAST
||
2233 ip
->ip_dst
.s_addr
== INADDR_ANY
) {
2234 ip_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
2238 /* Allow DHCP/BootP responses through */
2239 if ((inifp
->if_eflags
& IFEF_AUTOCONFIGURING
) &&
2240 hlen
== sizeof (struct ip
) && ip
->ip_p
== IPPROTO_UDP
) {
2241 struct udpiphdr
*ui
;
2243 if (m
->m_len
< sizeof (struct udpiphdr
) &&
2244 (m
= m_pullup(m
, sizeof (struct udpiphdr
))) == NULL
) {
2245 OSAddAtomic(1, &udpstat
.udps_hdrops
);
2248 ui
= mtod(m
, struct udpiphdr
*);
2249 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) {
2250 ip_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
2253 ip
= mtod(m
, struct ip
*); /* in case it changed */
2257 * Not for us; forward if possible and desirable.
2259 if (ipforwarding
== 0) {
2260 OSAddAtomic(1, &ipstat
.ips_cantforward
);
2264 ip_forward(m
, 0, args
.fwa_next_hop
);
2266 ip_forward(m
, 0, NULL
);
2273 * If offset or IP_MF are set, must reassemble.
2275 if (ip
->ip_off
& ~(IP_DF
| IP_RF
)) {
2277 * ip_reass() will return a different mbuf, and update
2278 * the divert info in div_info and args.fwa_divert_rule.
2281 m
= ip_reass(m
, (u_int16_t
*)&div_info
, &args
.fwa_divert_rule
);
2287 ip
= mtod(m
, struct ip
*);
2288 /* Get the header length of the reassembled packet */
2289 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
2291 /* Restore original checksum before diverting packet */
2292 if (div_info
!= 0) {
2293 #if BYTE_ORDER != BIG_ENDIAN
2298 ip
->ip_sum
= ip_cksum_hdr_in(m
, hlen
);
2299 #if BYTE_ORDER != BIG_ENDIAN
2308 * Further protocols expect the packet length to be w/o the
2315 * Divert or tee packet to the divert protocol if required.
2317 * If div_info is zero then cookie should be too, so we shouldn't
2318 * need to clear them here. Assume divert_packet() does so also.
2320 if (div_info
!= 0) {
2321 struct mbuf
*clone
= NULL
;
2323 /* Clone packet if we're doing a 'tee' */
2324 if (div_info
& IP_FW_PORT_TEE_FLAG
)
2325 clone
= m_dup(m
, M_DONTWAIT
);
2327 /* Restore packet header fields to original values */
2330 #if BYTE_ORDER != BIG_ENDIAN
2334 /* Deliver packet to divert input routine */
2335 OSAddAtomic(1, &ipstat
.ips_delivered
);
2336 divert_packet(m
, 1, div_info
& 0xffff, args
.fwa_divert_rule
);
2338 /* If 'tee', continue with original packet */
2339 if (clone
== NULL
) {
2343 ip
= mtod(m
, struct ip
*);
2349 * enforce IPsec policy checking if we are seeing last header.
2350 * note that we do not visit this with protocols with pcb layer
2351 * code - like udp/tcp/raw ip.
2353 if (ipsec_bypass
== 0 && (ip_protox
[ip
->ip_p
]->pr_flags
& PR_LASTHDR
)) {
2354 if (ipsec4_in_reject(m
, NULL
)) {
2355 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
2362 * Switch out to protocol's input routine.
2364 OSAddAtomic(1, &ipstat
.ips_delivered
);
2367 if (args
.fwa_next_hop
&& ip
->ip_p
== IPPROTO_TCP
) {
2368 /* TCP needs IPFORWARD info if available */
2369 struct m_tag
*fwd_tag
;
2370 struct ip_fwd_tag
*ipfwd_tag
;
2372 fwd_tag
= m_tag_create(KERNEL_MODULE_TAG_ID
,
2373 KERNEL_TAG_TYPE_IPFORWARD
, sizeof (*ipfwd_tag
),
2375 if (fwd_tag
== NULL
)
2378 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
2379 ipfwd_tag
->next_hop
= args
.fwa_next_hop
;
2381 m_tag_prepend(m
, fwd_tag
);
2383 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
2384 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
2386 /* TCP deals with its own locking */
2387 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
2389 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
2390 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
2392 if ((sw_lro
) && (ip
->ip_p
== IPPROTO_TCP
)) {
2393 m
= tcp_lro(m
, hlen
);
2398 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
2400 #else /* !IPFIREWALL */
2401 if ((sw_lro
) && (ip
->ip_p
== IPPROTO_TCP
)) {
2402 m
= tcp_lro(m
, hlen
);
2406 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
2407 #endif /* !IPFIREWALL */
2411 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
2416 ipq_updateparams(void)
2418 lck_mtx_assert(&ipqlock
, LCK_MTX_ASSERT_OWNED
);
2420 * -1 for unlimited allocation.
2425 * Positive number for specific bound.
2428 ipq_limit
= maxnipq
;
2430 * Zero specifies no further fragment queue allocation -- set the
2431 * bound very low, but rely on implementation elsewhere to actually
2432 * prevent allocation and reclaim current queues.
2437 * Arm the purge timer if not already and if there's work to do
2439 frag_sched_timeout();
2443 sysctl_maxnipq SYSCTL_HANDLER_ARGS
2445 #pragma unused(arg1, arg2)
2448 lck_mtx_lock(&ipqlock
);
2450 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
2451 if (error
|| req
->newptr
== USER_ADDR_NULL
)
2454 if (i
< -1 || i
> (nmbclusters
/ 4)) {
2461 lck_mtx_unlock(&ipqlock
);
2466 sysctl_maxfragsperpacket SYSCTL_HANDLER_ARGS
2468 #pragma unused(arg1, arg2)
2471 lck_mtx_lock(&ipqlock
);
2472 i
= maxfragsperpacket
;
2473 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
2474 if (error
|| req
->newptr
== USER_ADDR_NULL
)
2476 maxfragsperpacket
= i
;
2477 ipq_updateparams(); /* see if we need to arm timer */
2479 lck_mtx_unlock(&ipqlock
);
2484 * Take incoming datagram fragment and try to reassemble it into
2485 * whole datagram. If a chain for reassembly of this datagram already
2486 * exists, then it is given as fp; otherwise have to make a chain.
2488 * When IPDIVERT enabled, keep additional state with each packet that
2489 * tells us if we need to divert or tee the packet we're building.
2491 * The IP header is *NOT* adjusted out of iplen.
2493 static struct mbuf
*
2495 ip_reass(struct mbuf
*m
,
2498 #else /* IPDIVERT_44 */
2500 #endif /* IPDIVERT_44 */
2501 u_int16_t
*divcookie
)
2502 #else /* IPDIVERT */
2503 ip_reass(struct mbuf
*m
)
2504 #endif /* IPDIVERT */
2507 struct mbuf
*p
, *q
, *nq
, *t
;
2508 struct ipq
*fp
= NULL
;
2509 struct ipqhead
*head
;
2512 uint32_t csum
, csum_flags
;
2516 MBUFQ_INIT(&dfq
); /* for deferred frees */
2518 /* If maxnipq or maxfragsperpacket is 0, never accept fragments. */
2519 if (maxnipq
== 0 || maxfragsperpacket
== 0) {
2520 ipstat
.ips_fragments
++;
2521 ipstat
.ips_fragdropped
++;
2524 lck_mtx_lock(&ipqlock
);
2525 frag_sched_timeout(); /* purge stale fragments */
2526 lck_mtx_unlock(&ipqlock
);
2531 ip
= mtod(m
, struct ip
*);
2532 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
2534 lck_mtx_lock(&ipqlock
);
2536 hash
= IPREASS_HASH(ip
->ip_src
.s_addr
, ip
->ip_id
);
2540 * Look for queue of fragments
2543 TAILQ_FOREACH(fp
, head
, ipq_list
) {
2544 if (ip
->ip_id
== fp
->ipq_id
&&
2545 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
2546 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
2548 mac_ipq_label_compare(m
, fp
) &&
2550 ip
->ip_p
== fp
->ipq_p
)
2557 * Attempt to trim the number of allocated fragment queues if it
2558 * exceeds the administrative limit.
2560 if ((nipq
> (unsigned)maxnipq
) && (maxnipq
> 0)) {
2562 * drop something from the tail of the current queue
2563 * before proceeding further
2565 struct ipq
*fq
= TAILQ_LAST(head
, ipqhead
);
2566 if (fq
== NULL
) { /* gak */
2567 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
2568 struct ipq
*r
= TAILQ_LAST(&ipq
[i
], ipqhead
);
2570 ipstat
.ips_fragtimeout
+= r
->ipq_nfrags
;
2571 frag_freef(&ipq
[i
], r
);
2576 ipstat
.ips_fragtimeout
+= fq
->ipq_nfrags
;
2577 frag_freef(head
, fq
);
2583 * Leverage partial checksum offload for IP fragments. Narrow down
2584 * the scope to cover only UDP without IP options, as that is the
2587 * Perform 1's complement adjustment of octets that got included/
2588 * excluded in the hardware-calculated checksum value. Ignore cases
2589 * where the value includes or excludes the IP header span, as the
2590 * sum for those octets would already be 0xffff and thus no-op.
2592 if (ip
->ip_p
== IPPROTO_UDP
&& hlen
== sizeof (struct ip
) &&
2593 (m
->m_pkthdr
.csum_flags
&
2594 (CSUM_DATA_VALID
| CSUM_PARTIAL
| CSUM_PSEUDO_HDR
)) ==
2595 (CSUM_DATA_VALID
| CSUM_PARTIAL
)) {
2598 start
= m
->m_pkthdr
.csum_rx_start
;
2599 csum
= m
->m_pkthdr
.csum_rx_val
;
2601 if (start
!= 0 && start
!= hlen
) {
2602 #if BYTE_ORDER != BIG_ENDIAN
2608 /* callee folds in sum */
2609 csum
= m_adj_sum16(m
, start
, hlen
, csum
);
2610 #if BYTE_ORDER != BIG_ENDIAN
2617 csum_flags
= m
->m_pkthdr
.csum_flags
;
2623 /* Invalidate checksum */
2624 m
->m_pkthdr
.csum_flags
&= ~CSUM_DATA_VALID
;
2626 ipstat
.ips_fragments
++;
2629 * Adjust ip_len to not reflect header,
2630 * convert offset of this to bytes.
2633 if (ip
->ip_off
& IP_MF
) {
2635 * Make sure that fragments have a data length
2636 * that's a non-zero multiple of 8 bytes.
2638 if (ip
->ip_len
== 0 || (ip
->ip_len
& 0x7) != 0) {
2639 OSAddAtomic(1, &ipstat
.ips_toosmall
);
2641 * Reassembly queue may have been found if previous
2642 * fragments were valid; given that this one is bad,
2643 * we need to drop it. Make sure to set fp to NULL
2644 * if not already, since we don't want to decrement
2645 * ipq_nfrags as it doesn't include this packet.
2650 m
->m_flags
|= M_FRAG
;
2652 /* Clear the flag in case packet comes from loopback */
2653 m
->m_flags
&= ~M_FRAG
;
2657 m
->m_pkthdr
.pkt_hdr
= ip
;
2659 /* Previous ip_reass() started here. */
2661 * Presence of header sizes in mbufs
2662 * would confuse code below.
2668 * If first fragment to arrive, create a reassembly queue.
2671 fp
= ipq_alloc(M_DONTWAIT
);
2675 if (mac_ipq_label_init(fp
, M_NOWAIT
) != 0) {
2680 mac_ipq_label_associate(m
, fp
);
2682 TAILQ_INSERT_HEAD(head
, fp
, ipq_list
);
2685 fp
->ipq_ttl
= IPFRAGTTL
;
2686 fp
->ipq_p
= ip
->ip_p
;
2687 fp
->ipq_id
= ip
->ip_id
;
2688 fp
->ipq_src
= ip
->ip_src
;
2689 fp
->ipq_dst
= ip
->ip_dst
;
2691 m
->m_nextpkt
= NULL
;
2693 * If the first fragment has valid checksum offload
2694 * info, the rest of fragments are eligible as well.
2696 if (csum_flags
!= 0) {
2697 fp
->ipq_csum
= csum
;
2698 fp
->ipq_csum_flags
= csum_flags
;
2702 * Transfer firewall instructions to the fragment structure.
2703 * Only trust info in the fragment at offset 0.
2705 if (ip
->ip_off
== 0) {
2707 fp
->ipq_div_info
= *divinfo
;
2709 fp
->ipq_divert
= *divinfo
;
2711 fp
->ipq_div_cookie
= *divcookie
;
2715 #endif /* IPDIVERT */
2716 m
= NULL
; /* nothing to return */
2721 mac_ipq_label_update(m
, fp
);
2725 #define GETIP(m) ((struct ip *)((m)->m_pkthdr.pkt_hdr))
2728 * Handle ECN by comparing this segment with the first one;
2729 * if CE is set, do not lose CE.
2730 * drop if CE and not-ECT are mixed for the same packet.
2732 ecn
= ip
->ip_tos
& IPTOS_ECN_MASK
;
2733 ecn0
= GETIP(fp
->ipq_frags
)->ip_tos
& IPTOS_ECN_MASK
;
2734 if (ecn
== IPTOS_ECN_CE
) {
2735 if (ecn0
== IPTOS_ECN_NOTECT
)
2737 if (ecn0
!= IPTOS_ECN_CE
)
2738 GETIP(fp
->ipq_frags
)->ip_tos
|= IPTOS_ECN_CE
;
2740 if (ecn
== IPTOS_ECN_NOTECT
&& ecn0
!= IPTOS_ECN_NOTECT
)
2744 * Find a segment which begins after this one does.
2746 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
)
2747 if (GETIP(q
)->ip_off
> ip
->ip_off
)
2751 * If there is a preceding segment, it may provide some of
2752 * our data already. If so, drop the data from the incoming
2753 * segment. If it provides all of our data, drop us, otherwise
2754 * stick new segment in the proper place.
2756 * If some of the data is dropped from the preceding
2757 * segment, then it's checksum is invalidated.
2760 i
= GETIP(p
)->ip_off
+ GETIP(p
)->ip_len
- ip
->ip_off
;
2762 if (i
>= ip
->ip_len
)
2765 fp
->ipq_csum_flags
= 0;
2769 m
->m_nextpkt
= p
->m_nextpkt
;
2772 m
->m_nextpkt
= fp
->ipq_frags
;
2777 * While we overlap succeeding segments trim them or,
2778 * if they are completely covered, dequeue them.
2780 for (; q
!= NULL
&& ip
->ip_off
+ ip
->ip_len
> GETIP(q
)->ip_off
;
2782 i
= (ip
->ip_off
+ ip
->ip_len
) - GETIP(q
)->ip_off
;
2783 if (i
< GETIP(q
)->ip_len
) {
2784 GETIP(q
)->ip_len
-= i
;
2785 GETIP(q
)->ip_off
+= i
;
2787 fp
->ipq_csum_flags
= 0;
2792 ipstat
.ips_fragdropped
++;
2794 /* defer freeing until after lock is dropped */
2795 MBUFQ_ENQUEUE(&dfq
, q
);
2799 * If this fragment contains similar checksum offload info
2800 * as that of the existing ones, accumulate checksum. Otherwise,
2801 * invalidate checksum offload info for the entire datagram.
2803 if (csum_flags
!= 0 && csum_flags
== fp
->ipq_csum_flags
)
2804 fp
->ipq_csum
+= csum
;
2805 else if (fp
->ipq_csum_flags
!= 0)
2806 fp
->ipq_csum_flags
= 0;
2810 * Transfer firewall instructions to the fragment structure.
2811 * Only trust info in the fragment at offset 0.
2813 if (ip
->ip_off
== 0) {
2815 fp
->ipq_div_info
= *divinfo
;
2817 fp
->ipq_divert
= *divinfo
;
2819 fp
->ipq_div_cookie
= *divcookie
;
2823 #endif /* IPDIVERT */
2826 * Check for complete reassembly and perform frag per packet
2829 * Frag limiting is performed here so that the nth frag has
2830 * a chance to complete the packet before we drop the packet.
2831 * As a result, n+1 frags are actually allowed per packet, but
2832 * only n will ever be stored. (n = maxfragsperpacket.)
2836 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
) {
2837 if (GETIP(q
)->ip_off
!= next
) {
2838 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
2839 ipstat
.ips_fragdropped
+= fp
->ipq_nfrags
;
2840 frag_freef(head
, fp
);
2842 m
= NULL
; /* nothing to return */
2845 next
+= GETIP(q
)->ip_len
;
2847 /* Make sure the last packet didn't have the IP_MF flag */
2848 if (p
->m_flags
& M_FRAG
) {
2849 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
2850 ipstat
.ips_fragdropped
+= fp
->ipq_nfrags
;
2851 frag_freef(head
, fp
);
2853 m
= NULL
; /* nothing to return */
2858 * Reassembly is complete. Make sure the packet is a sane size.
2862 if (next
+ (IP_VHL_HL(ip
->ip_vhl
) << 2) > IP_MAXPACKET
) {
2863 ipstat
.ips_toolong
++;
2864 ipstat
.ips_fragdropped
+= fp
->ipq_nfrags
;
2865 frag_freef(head
, fp
);
2866 m
= NULL
; /* nothing to return */
2871 * Concatenate fragments.
2878 q
->m_nextpkt
= NULL
;
2879 for (q
= nq
; q
!= NULL
; q
= nq
) {
2881 q
->m_nextpkt
= NULL
;
2886 * Store partial hardware checksum info from the fragment queue;
2887 * the receive start offset is set to 20 bytes (see code at the
2888 * top of this routine.)
2890 if (fp
->ipq_csum_flags
!= 0) {
2891 csum
= fp
->ipq_csum
;
2895 m
->m_pkthdr
.csum_rx_val
= csum
;
2896 m
->m_pkthdr
.csum_rx_start
= sizeof (struct ip
);
2897 m
->m_pkthdr
.csum_flags
= fp
->ipq_csum_flags
;
2898 } else if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) ||
2899 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
2900 /* loopback checksums are always OK */
2901 m
->m_pkthdr
.csum_data
= 0xffff;
2902 m
->m_pkthdr
.csum_flags
&= ~CSUM_PARTIAL
;
2903 m
->m_pkthdr
.csum_flags
=
2904 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
2905 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
2910 * Extract firewall instructions from the fragment structure.
2913 *divinfo
= fp
->ipq_div_info
;
2915 *divinfo
= fp
->ipq_divert
;
2917 *divcookie
= fp
->ipq_div_cookie
;
2918 #endif /* IPDIVERT */
2921 mac_mbuf_label_associate_ipq(fp
, m
);
2922 mac_ipq_label_destroy(fp
);
2925 * Create header for new ip packet by modifying header of first
2926 * packet; dequeue and discard fragment reassembly header.
2927 * Make header visible.
2929 ip
->ip_len
= (IP_VHL_HL(ip
->ip_vhl
) << 2) + next
;
2930 ip
->ip_src
= fp
->ipq_src
;
2931 ip
->ip_dst
= fp
->ipq_dst
;
2933 fp
->ipq_frags
= NULL
; /* return to caller as 'm' */
2934 frag_freef(head
, fp
);
2937 m
->m_len
+= (IP_VHL_HL(ip
->ip_vhl
) << 2);
2938 m
->m_data
-= (IP_VHL_HL(ip
->ip_vhl
) << 2);
2939 /* some debugging cruft by sklower, below, will go away soon */
2940 if (m
->m_flags
& M_PKTHDR
) /* XXX this should be done elsewhere */
2942 ipstat
.ips_reassembled
++;
2944 /* arm the purge timer if not already and if there's work to do */
2945 frag_sched_timeout();
2946 lck_mtx_unlock(&ipqlock
);
2947 /* perform deferred free (if needed) now that lock is dropped */
2948 if (!MBUFQ_EMPTY(&dfq
))
2950 VERIFY(MBUFQ_EMPTY(&dfq
));
2955 /* arm the purge timer if not already and if there's work to do */
2956 frag_sched_timeout();
2957 lck_mtx_unlock(&ipqlock
);
2958 /* perform deferred free (if needed) */
2959 if (!MBUFQ_EMPTY(&dfq
))
2961 VERIFY(MBUFQ_EMPTY(&dfq
));
2968 #endif /* IPDIVERT */
2969 ipstat
.ips_fragdropped
++;
2972 /* arm the purge timer if not already and if there's work to do */
2973 frag_sched_timeout();
2974 lck_mtx_unlock(&ipqlock
);
2976 /* perform deferred free (if needed) */
2977 if (!MBUFQ_EMPTY(&dfq
))
2979 VERIFY(MBUFQ_EMPTY(&dfq
));
2985 * Free a fragment reassembly header and all
2986 * associated datagrams.
2989 frag_freef(struct ipqhead
*fhp
, struct ipq
*fp
)
2991 lck_mtx_assert(&ipqlock
, LCK_MTX_ASSERT_OWNED
);
2994 if (fp
->ipq_frags
!= NULL
) {
2995 m_freem_list(fp
->ipq_frags
);
2996 fp
->ipq_frags
= NULL
;
2998 TAILQ_REMOVE(fhp
, fp
, ipq_list
);
3004 * IP reassembly timer processing
3007 frag_timeout(void *arg
)
3014 * Update coarse-grained networking timestamp (in sec.); the idea
3015 * is to piggy-back on the timeout callout to update the counter
3016 * returnable via net_uptime().
3018 net_update_uptime();
3020 lck_mtx_lock(&ipqlock
);
3021 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
3022 for (fp
= TAILQ_FIRST(&ipq
[i
]); fp
; ) {
3026 fp
= TAILQ_NEXT(fp
, ipq_list
);
3027 if (--fpp
->ipq_ttl
== 0) {
3028 ipstat
.ips_fragtimeout
+= fpp
->ipq_nfrags
;
3029 frag_freef(&ipq
[i
], fpp
);
3034 * If we are over the maximum number of fragments
3035 * (due to the limit being lowered), drain off
3036 * enough to get down to the new limit.
3038 if (maxnipq
>= 0 && nipq
> (unsigned)maxnipq
) {
3039 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
3040 while (nipq
> (unsigned)maxnipq
&&
3041 !TAILQ_EMPTY(&ipq
[i
])) {
3042 ipstat
.ips_fragdropped
+=
3043 TAILQ_FIRST(&ipq
[i
])->ipq_nfrags
;
3044 frag_freef(&ipq
[i
], TAILQ_FIRST(&ipq
[i
]));
3048 /* re-arm the purge timer if there's work to do */
3049 frag_timeout_run
= 0;
3050 frag_sched_timeout();
3051 lck_mtx_unlock(&ipqlock
);
3055 frag_sched_timeout(void)
3057 lck_mtx_assert(&ipqlock
, LCK_MTX_ASSERT_OWNED
);
3059 if (!frag_timeout_run
&& nipq
> 0) {
3060 frag_timeout_run
= 1;
3061 timeout(frag_timeout
, NULL
, hz
);
3066 * Drain off all datagram fragments.
3073 lck_mtx_lock(&ipqlock
);
3074 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
3075 while (!TAILQ_EMPTY(&ipq
[i
])) {
3076 ipstat
.ips_fragdropped
+=
3077 TAILQ_FIRST(&ipq
[i
])->ipq_nfrags
;
3078 frag_freef(&ipq
[i
], TAILQ_FIRST(&ipq
[i
]));
3081 lck_mtx_unlock(&ipqlock
);
3091 * See comments in ipq_updateparams(). Keep the count separate
3092 * from nipq since the latter represents the elements already
3093 * in the reassembly queues.
3095 if (ipq_limit
> 0 && ipq_count
> ipq_limit
)
3098 t
= m_get(how
, MT_FTABLE
);
3100 atomic_add_32(&ipq_count
, 1);
3101 fp
= mtod(t
, struct ipq
*);
3102 bzero(fp
, sizeof (*fp
));
3110 ipq_free(struct ipq
*fp
)
3112 (void) m_free(dtom(fp
));
3113 atomic_add_32(&ipq_count
, -1);
3122 frag_drain(); /* fragments */
3123 in_rtqdrain(); /* protocol cloned routes */
3124 in_arpdrain(NULL
); /* cloned routes: ARP */
3128 * Do option processing on a datagram,
3129 * possibly discarding it if bad options are encountered,
3130 * or forwarding it if source-routed.
3131 * The pass argument is used when operating in the IPSTEALTH
3132 * mode to tell what options to process:
3133 * [LS]SRR (pass 0) or the others (pass 1).
3134 * The reason for as many as two passes is that when doing IPSTEALTH,
3135 * non-routing options should be processed only if the packet is for us.
3136 * Returns 1 if packet has been forwarded/freed,
3137 * 0 if the packet should be processed further.
3140 ip_dooptions(struct mbuf
*m
, int pass
, struct sockaddr_in
*next_hop
)
3142 #pragma unused(pass)
3143 struct ip
*ip
= mtod(m
, struct ip
*);
3145 struct ip_timestamp
*ipt
;
3146 struct in_ifaddr
*ia
;
3147 int opt
, optlen
, cnt
, off
, code
, type
= ICMP_PARAMPROB
, forward
= 0;
3148 struct in_addr
*sin
, dst
;
3150 struct sockaddr_in ipaddr
= {
3151 sizeof (ipaddr
), AF_INET
, 0, { 0 }, { 0, } };
3153 /* Expect 32-bit aligned data pointer on strict-align platforms */
3154 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
3157 cp
= (u_char
*)(ip
+ 1);
3158 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
3159 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
3160 opt
= cp
[IPOPT_OPTVAL
];
3161 if (opt
== IPOPT_EOL
)
3163 if (opt
== IPOPT_NOP
)
3166 if (cnt
< IPOPT_OLEN
+ sizeof (*cp
)) {
3167 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
3170 optlen
= cp
[IPOPT_OLEN
];
3171 if (optlen
< IPOPT_OLEN
+ sizeof (*cp
) ||
3173 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
3183 * Source routing with record.
3184 * Find interface with current destination address.
3185 * If none on this machine then drop if strictly routed,
3186 * or do nothing if loosely routed.
3187 * Record interface address and bring up next address
3188 * component. If strictly routed make sure next
3189 * address is on directly accessible net.
3193 if (optlen
< IPOPT_OFFSET
+ sizeof (*cp
)) {
3194 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
3197 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
3198 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
3201 ipaddr
.sin_addr
= ip
->ip_dst
;
3202 ia
= (struct in_ifaddr
*)ifa_ifwithaddr(SA(&ipaddr
));
3204 if (opt
== IPOPT_SSRR
) {
3205 type
= ICMP_UNREACH
;
3206 code
= ICMP_UNREACH_SRCFAIL
;
3209 if (!ip_dosourceroute
)
3210 goto nosourcerouting
;
3212 * Loose routing, and not at next destination
3213 * yet; nothing to do except forward.
3217 IFA_REMREF(&ia
->ia_ifa
);
3220 off
--; /* 0 origin */
3221 if (off
> optlen
- (int)sizeof (struct in_addr
)) {
3223 * End of source route. Should be for us.
3225 if (!ip_acceptsourceroute
)
3226 goto nosourcerouting
;
3227 save_rte(cp
, ip
->ip_src
);
3231 if (!ip_dosourceroute
) {
3233 char buf
[MAX_IPv4_STR_LEN
];
3234 char buf2
[MAX_IPv4_STR_LEN
];
3236 * Acting as a router, so generate ICMP
3240 "attempted source route from %s "
3242 inet_ntop(AF_INET
, &ip
->ip_src
,
3244 inet_ntop(AF_INET
, &ip
->ip_dst
,
3245 buf2
, sizeof (buf2
)));
3246 type
= ICMP_UNREACH
;
3247 code
= ICMP_UNREACH_SRCFAIL
;
3251 * Not acting as a router,
3254 OSAddAtomic(1, &ipstat
.ips_cantforward
);
3261 * locate outgoing interface
3263 (void) memcpy(&ipaddr
.sin_addr
, cp
+ off
,
3264 sizeof (ipaddr
.sin_addr
));
3266 if (opt
== IPOPT_SSRR
) {
3267 #define INA struct in_ifaddr *
3268 if ((ia
= (INA
)ifa_ifwithdstaddr(
3269 SA(&ipaddr
))) == NULL
) {
3270 ia
= (INA
)ifa_ifwithnet(SA(&ipaddr
));
3273 ia
= ip_rtaddr(ipaddr
.sin_addr
);
3276 type
= ICMP_UNREACH
;
3277 code
= ICMP_UNREACH_SRCFAIL
;
3280 ip
->ip_dst
= ipaddr
.sin_addr
;
3281 IFA_LOCK(&ia
->ia_ifa
);
3282 (void) memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
3283 sizeof (struct in_addr
));
3284 IFA_UNLOCK(&ia
->ia_ifa
);
3285 IFA_REMREF(&ia
->ia_ifa
);
3287 cp
[IPOPT_OFFSET
] += sizeof (struct in_addr
);
3289 * Let ip_intr's mcast routing check handle mcast pkts
3291 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
3295 if (optlen
< IPOPT_OFFSET
+ sizeof (*cp
)) {
3296 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
3299 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
3300 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
3304 * If no space remains, ignore.
3306 off
--; /* 0 origin */
3307 if (off
> optlen
- (int)sizeof (struct in_addr
))
3309 (void) memcpy(&ipaddr
.sin_addr
, &ip
->ip_dst
,
3310 sizeof (ipaddr
.sin_addr
));
3312 * locate outgoing interface; if we're the destination,
3313 * use the incoming interface (should be same).
3315 if ((ia
= (INA
)ifa_ifwithaddr(SA(&ipaddr
))) == NULL
) {
3316 if ((ia
= ip_rtaddr(ipaddr
.sin_addr
)) == NULL
) {
3317 type
= ICMP_UNREACH
;
3318 code
= ICMP_UNREACH_HOST
;
3322 IFA_LOCK(&ia
->ia_ifa
);
3323 (void) memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
3324 sizeof (struct in_addr
));
3325 IFA_UNLOCK(&ia
->ia_ifa
);
3326 IFA_REMREF(&ia
->ia_ifa
);
3328 cp
[IPOPT_OFFSET
] += sizeof (struct in_addr
);
3332 code
= cp
- (u_char
*)ip
;
3333 ipt
= (struct ip_timestamp
*)(void *)cp
;
3334 if (ipt
->ipt_len
< 4 || ipt
->ipt_len
> 40) {
3335 code
= (u_char
*)&ipt
->ipt_len
- (u_char
*)ip
;
3338 if (ipt
->ipt_ptr
< 5) {
3339 code
= (u_char
*)&ipt
->ipt_ptr
- (u_char
*)ip
;
3343 ipt
->ipt_len
- (int)sizeof (int32_t)) {
3344 if (++ipt
->ipt_oflw
== 0) {
3345 code
= (u_char
*)&ipt
->ipt_ptr
-
3351 sin
= (struct in_addr
*)(void *)(cp
+ ipt
->ipt_ptr
- 1);
3352 switch (ipt
->ipt_flg
) {
3354 case IPOPT_TS_TSONLY
:
3357 case IPOPT_TS_TSANDADDR
:
3358 if (ipt
->ipt_ptr
- 1 + sizeof (n_time
) +
3359 sizeof (struct in_addr
) > ipt
->ipt_len
) {
3360 code
= (u_char
*)&ipt
->ipt_ptr
-
3364 ipaddr
.sin_addr
= dst
;
3365 ia
= (INA
)ifaof_ifpforaddr(SA(&ipaddr
),
3369 IFA_LOCK(&ia
->ia_ifa
);
3370 (void) memcpy(sin
, &IA_SIN(ia
)->sin_addr
,
3371 sizeof (struct in_addr
));
3372 IFA_UNLOCK(&ia
->ia_ifa
);
3373 ipt
->ipt_ptr
+= sizeof (struct in_addr
);
3374 IFA_REMREF(&ia
->ia_ifa
);
3378 case IPOPT_TS_PRESPEC
:
3379 if (ipt
->ipt_ptr
- 1 + sizeof (n_time
) +
3380 sizeof (struct in_addr
) > ipt
->ipt_len
) {
3381 code
= (u_char
*)&ipt
->ipt_ptr
-
3385 (void) memcpy(&ipaddr
.sin_addr
, sin
,
3386 sizeof (struct in_addr
));
3387 if ((ia
= (struct in_ifaddr
*)ifa_ifwithaddr(
3388 SA(&ipaddr
))) == NULL
)
3390 IFA_REMREF(&ia
->ia_ifa
);
3392 ipt
->ipt_ptr
+= sizeof (struct in_addr
);
3396 /* XXX can't take &ipt->ipt_flg */
3397 code
= (u_char
*)&ipt
->ipt_ptr
-
3402 (void) memcpy(cp
+ ipt
->ipt_ptr
- 1, &ntime
,
3404 ipt
->ipt_ptr
+= sizeof (n_time
);
3407 if (forward
&& ipforwarding
) {
3408 ip_forward(m
, 1, next_hop
);
3413 icmp_error(m
, type
, code
, 0, 0);
3414 OSAddAtomic(1, &ipstat
.ips_badoptions
);
3419 * Check for the presence of the IP Router Alert option [RFC2113]
3420 * in the header of an IPv4 datagram.
3422 * This call is not intended for use from the forwarding path; it is here
3423 * so that protocol domains may check for the presence of the option.
3424 * Given how FreeBSD's IPv4 stack is currently structured, the Router Alert
3425 * option does not have much relevance to the implementation, though this
3426 * may change in future.
3427 * Router alert options SHOULD be passed if running in IPSTEALTH mode and
3428 * we are not the endpoint.
3429 * Length checks on individual options should already have been peformed
3430 * by ip_dooptions() therefore they are folded under DIAGNOSTIC here.
3432 * Return zero if not present or options are invalid, non-zero if present.
3435 ip_checkrouteralert(struct mbuf
*m
)
3437 struct ip
*ip
= mtod(m
, struct ip
*);
3439 int opt
, optlen
, cnt
, found_ra
;
3442 cp
= (u_char
*)(ip
+ 1);
3443 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
3444 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
3445 opt
= cp
[IPOPT_OPTVAL
];
3446 if (opt
== IPOPT_EOL
)
3448 if (opt
== IPOPT_NOP
)
3452 if (cnt
< IPOPT_OLEN
+ sizeof (*cp
))
3455 optlen
= cp
[IPOPT_OLEN
];
3457 if (optlen
< IPOPT_OLEN
+ sizeof (*cp
) || optlen
> cnt
)
3464 if (optlen
!= IPOPT_OFFSET
+ sizeof (uint16_t) ||
3465 (*((uint16_t *)(void *)&cp
[IPOPT_OFFSET
]) != 0))
3480 * Given address of next destination (final or next hop),
3481 * return internet address info of interface to be used to get there.
3484 ip_rtaddr(struct in_addr dst
)
3486 struct sockaddr_in
*sin
;
3487 struct ifaddr
*rt_ifa
;
3490 bzero(&ro
, sizeof (ro
));
3491 sin
= SIN(&ro
.ro_dst
);
3492 sin
->sin_family
= AF_INET
;
3493 sin
->sin_len
= sizeof (*sin
);
3494 sin
->sin_addr
= dst
;
3496 rtalloc_ign(&ro
, RTF_PRCLONING
);
3497 if (ro
.ro_rt
== NULL
) {
3503 if ((rt_ifa
= ro
.ro_rt
->rt_ifa
) != NULL
)
3505 RT_UNLOCK(ro
.ro_rt
);
3508 return ((struct in_ifaddr
*)rt_ifa
);
3512 * Save incoming source route for use in replies,
3513 * to be picked up later by ip_srcroute if the receiver is interested.
3516 save_rte(u_char
*option
, struct in_addr dst
)
3520 olen
= option
[IPOPT_OLEN
];
3523 printf("save_rte: olen %d\n", olen
);
3525 if (olen
> sizeof (ip_srcrt
) - (1 + sizeof (dst
)))
3527 bcopy(option
, ip_srcrt
.srcopt
, olen
);
3528 ip_nhops
= (olen
- IPOPT_OFFSET
- 1) / sizeof (struct in_addr
);
3533 * Retrieve incoming source route for use in replies,
3534 * in the same form used by setsockopt.
3535 * The first hop is placed before the options, will be removed later.
3540 struct in_addr
*p
, *q
;
3546 m
= m_get(M_DONTWAIT
, MT_HEADER
);
3550 #define OPTSIZ (sizeof (ip_srcrt.nop) + sizeof (ip_srcrt.srcopt))
3552 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
3553 m
->m_len
= ip_nhops
* sizeof (struct in_addr
) +
3554 sizeof (struct in_addr
) + OPTSIZ
;
3557 printf("ip_srcroute: nhops %d mlen %d", ip_nhops
, m
->m_len
);
3561 * First save first hop for return route
3563 p
= &ip_srcrt
.route
[ip_nhops
- 1];
3564 *(mtod(m
, struct in_addr
*)) = *p
--;
3568 (u_int32_t
)ntohl(mtod(m
, struct in_addr
*)->s_addr
));
3572 * Copy option fields and padding (nop) to mbuf.
3574 ip_srcrt
.nop
= IPOPT_NOP
;
3575 ip_srcrt
.srcopt
[IPOPT_OFFSET
] = IPOPT_MINOFF
;
3576 (void) memcpy(mtod(m
, caddr_t
) + sizeof (struct in_addr
),
3577 &ip_srcrt
.nop
, OPTSIZ
);
3578 q
= (struct in_addr
*)(void *)(mtod(m
, caddr_t
) +
3579 sizeof (struct in_addr
) + OPTSIZ
);
3582 * Record return path as an IP source route,
3583 * reversing the path (pointers are now aligned).
3585 while (p
>= ip_srcrt
.route
) {
3588 printf(" %lx", (u_int32_t
)ntohl(q
->s_addr
));
3593 * Last hop goes to final destination.
3598 printf(" %lx\n", (u_int32_t
)ntohl(q
->s_addr
));
3604 * Strip out IP options, at higher
3605 * level protocol in the kernel.
3606 * Second argument is buffer to which options
3607 * will be moved, and return value is their length.
3608 * XXX should be deleted; last arg currently ignored.
3611 ip_stripoptions(struct mbuf
*m
, struct mbuf
*mopt
)
3613 #pragma unused(mopt)
3615 struct ip
*ip
= mtod(m
, struct ip
*);
3619 /* Expect 32-bit aligned data pointer on strict-align platforms */
3620 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
3622 olen
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
3623 opts
= (caddr_t
)(ip
+ 1);
3624 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
3625 bcopy(opts
+ olen
, opts
, (unsigned)i
);
3627 if (m
->m_flags
& M_PKTHDR
)
3628 m
->m_pkthdr
.len
-= olen
;
3629 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof (struct ip
) >> 2);
3632 u_char inetctlerrmap
[PRC_NCMDS
] = {
3634 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
3635 ENETUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
3636 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
3638 ENOPROTOOPT
, ECONNREFUSED
3642 sysctl_ipforwarding SYSCTL_HANDLER_ARGS
3644 #pragma unused(arg1, arg2)
3645 int i
, was_ipforwarding
= ipforwarding
;
3647 i
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
3648 if (i
!= 0 || req
->newptr
== USER_ADDR_NULL
)
3651 if (was_ipforwarding
&& !ipforwarding
) {
3652 /* clean up IPv4 forwarding cached routes */
3653 ifnet_head_lock_shared();
3654 for (i
= 0; i
<= if_index
; i
++) {
3655 struct ifnet
*ifp
= ifindex2ifnet
[i
];
3657 lck_mtx_lock(&ifp
->if_cached_route_lock
);
3658 ROUTE_RELEASE(&ifp
->if_fwd_route
);
3659 bzero(&ifp
->if_fwd_route
,
3660 sizeof (ifp
->if_fwd_route
));
3661 lck_mtx_unlock(&ifp
->if_cached_route_lock
);
3671 * Similar to inp_route_{copyout,copyin} routines except that these copy
3672 * out the cached IPv4 forwarding route from struct ifnet instead of the
3673 * inpcb. See comments for those routines for explanations.
3676 ip_fwd_route_copyout(struct ifnet
*ifp
, struct route
*dst
)
3678 struct route
*src
= &ifp
->if_fwd_route
;
3680 lck_mtx_lock_spin(&ifp
->if_cached_route_lock
);
3681 lck_mtx_convert_spin(&ifp
->if_cached_route_lock
);
3683 /* Minor sanity check */
3684 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET
)
3685 panic("%s: wrong or corrupted route: %p", __func__
, src
);
3687 route_copyout(dst
, src
, sizeof (*dst
));
3689 lck_mtx_unlock(&ifp
->if_cached_route_lock
);
3693 ip_fwd_route_copyin(struct ifnet
*ifp
, struct route
*src
)
3695 struct route
*dst
= &ifp
->if_fwd_route
;
3697 lck_mtx_lock_spin(&ifp
->if_cached_route_lock
);
3698 lck_mtx_convert_spin(&ifp
->if_cached_route_lock
);
3700 /* Minor sanity check */
3701 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET
)
3702 panic("%s: wrong or corrupted route: %p", __func__
, src
);
3704 if (ifp
->if_fwd_cacheok
)
3705 route_copyin(src
, dst
, sizeof (*src
));
3707 lck_mtx_unlock(&ifp
->if_cached_route_lock
);
3711 * Forward a packet. If some error occurs return the sender
3712 * an icmp packet. Note we can't always generate a meaningful
3713 * icmp message because icmp doesn't have a large enough repertoire
3714 * of codes and types.
3716 * If not forwarding, just drop the packet. This could be confusing
3717 * if ipforwarding was zero but some routing protocol was advancing
3718 * us as a gateway to somewhere. However, we must let the routing
3719 * protocol deal with that.
3721 * The srcrt parameter indicates whether the packet is being forwarded
3722 * via a source route.
3725 ip_forward(struct mbuf
*m
, int srcrt
, struct sockaddr_in
*next_hop
)
3728 #pragma unused(next_hop)
3730 struct ip
*ip
= mtod(m
, struct ip
*);
3731 struct sockaddr_in
*sin
;
3733 struct route fwd_rt
;
3734 int error
, type
= 0, code
= 0;
3737 struct in_addr pkt_dst
;
3738 u_int32_t nextmtu
= 0, len
;
3739 struct ip_out_args ipoa
= { IFSCOPE_NONE
, { 0 }, 0, 0 };
3740 struct ifnet
*rcvifp
= m
->m_pkthdr
.rcvif
;
3742 struct secpolicy
*sp
= NULL
;
3746 struct pf_mtag
*pf_mtag
;
3752 * Cache the destination address of the packet; this may be
3753 * changed by use of 'ipfw fwd'.
3755 pkt_dst
= ((next_hop
!= NULL
) ? next_hop
->sin_addr
: ip
->ip_dst
);
3756 #else /* !IPFIREWALL */
3757 pkt_dst
= ip
->ip_dst
;
3758 #endif /* !IPFIREWALL */
3762 printf("forward: src %lx dst %lx ttl %x\n",
3763 (u_int32_t
)ip
->ip_src
.s_addr
, (u_int32_t
)pkt_dst
.s_addr
,
3767 if (m
->m_flags
& (M_BCAST
|M_MCAST
) || !in_canforward(pkt_dst
)) {
3768 OSAddAtomic(1, &ipstat
.ips_cantforward
);
3774 #endif /* IPSTEALTH */
3775 if (ip
->ip_ttl
<= IPTTLDEC
) {
3776 icmp_error(m
, ICMP_TIMXCEED
, ICMP_TIMXCEED_INTRANS
,
3782 #endif /* IPSTEALTH */
3785 pf_mtag
= pf_find_mtag(m
);
3786 if (pf_mtag
!= NULL
&& pf_mtag
->pftag_rtableid
!= IFSCOPE_NONE
) {
3787 ipoa
.ipoa_boundif
= pf_mtag
->pftag_rtableid
;
3788 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
3792 ip_fwd_route_copyout(rcvifp
, &fwd_rt
);
3794 sin
= SIN(&fwd_rt
.ro_dst
);
3795 if (ROUTE_UNUSABLE(&fwd_rt
) || pkt_dst
.s_addr
!= sin
->sin_addr
.s_addr
) {
3796 ROUTE_RELEASE(&fwd_rt
);
3798 sin
->sin_family
= AF_INET
;
3799 sin
->sin_len
= sizeof (*sin
);
3800 sin
->sin_addr
= pkt_dst
;
3802 rtalloc_scoped_ign(&fwd_rt
, RTF_PRCLONING
, ipoa
.ipoa_boundif
);
3803 if (fwd_rt
.ro_rt
== NULL
) {
3804 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_HOST
, dest
, 0);
3811 * Save the IP header and at most 8 bytes of the payload,
3812 * in case we need to generate an ICMP message to the src.
3814 * We don't use m_copy() because it might return a reference
3815 * to a shared cluster. Both this function and ip_output()
3816 * assume exclusive access to the IP header in `m', so any
3817 * data in a cluster may change before we reach icmp_error().
3819 MGET(mcopy
, M_DONTWAIT
, m
->m_type
);
3820 if (mcopy
!= NULL
) {
3821 M_COPY_PKTHDR(mcopy
, m
);
3822 mcopy
->m_len
= imin((IP_VHL_HL(ip
->ip_vhl
) << 2) + 8,
3824 m_copydata(m
, 0, mcopy
->m_len
, mtod(mcopy
, caddr_t
));
3829 #endif /* IPSTEALTH */
3830 ip
->ip_ttl
-= IPTTLDEC
;
3833 #endif /* IPSTEALTH */
3836 * If forwarding packet using same interface that it came in on,
3837 * perhaps should send a redirect to sender to shortcut a hop.
3838 * Only send redirect if source is sending directly to us,
3839 * and if packet was not source routed (or has any options).
3840 * Also, don't send redirect if forwarding using a default route
3841 * or a route modified by a redirect.
3844 if (rt
->rt_ifp
== m
->m_pkthdr
.rcvif
&&
3845 !(rt
->rt_flags
& (RTF_DYNAMIC
|RTF_MODIFIED
)) &&
3846 satosin(rt_key(rt
))->sin_addr
.s_addr
!= INADDR_ANY
&&
3847 ipsendredirects
&& !srcrt
&& rt
->rt_ifa
!= NULL
) {
3848 struct in_ifaddr
*ia
= (struct in_ifaddr
*)rt
->rt_ifa
;
3849 u_int32_t src
= ntohl(ip
->ip_src
.s_addr
);
3851 /* Become a regular mutex */
3852 RT_CONVERT_LOCK(rt
);
3853 IFA_LOCK_SPIN(&ia
->ia_ifa
);
3854 if ((src
& ia
->ia_subnetmask
) == ia
->ia_subnet
) {
3855 if (rt
->rt_flags
& RTF_GATEWAY
)
3856 dest
= satosin(rt
->rt_gateway
)->sin_addr
.s_addr
;
3858 dest
= pkt_dst
.s_addr
;
3860 * Router requirements says to only send
3863 type
= ICMP_REDIRECT
;
3864 code
= ICMP_REDIRECT_HOST
;
3867 printf("redirect (%d) to %lx\n", code
,
3871 IFA_UNLOCK(&ia
->ia_ifa
);
3876 if (next_hop
!= NULL
) {
3877 /* Pass IPFORWARD info if available */
3879 struct ip_fwd_tag
*ipfwd_tag
;
3881 tag
= m_tag_create(KERNEL_MODULE_TAG_ID
,
3882 KERNEL_TAG_TYPE_IPFORWARD
,
3883 sizeof (*ipfwd_tag
), M_NOWAIT
, m
);
3890 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
3891 ipfwd_tag
->next_hop
= next_hop
;
3893 m_tag_prepend(m
, tag
);
3895 #endif /* IPFIREWALL */
3897 /* Mark this packet as being forwarded from another interface */
3898 m
->m_pkthdr
.pkt_flags
|= PKTF_FORWARDED
;
3901 error
= ip_output(m
, NULL
, &fwd_rt
, IP_FORWARDING
| IP_OUTARGS
,
3904 /* Refresh rt since the route could have changed while in IP */
3908 OSAddAtomic(1, &ipstat
.ips_cantforward
);
3911 * Increment stats on the source interface; the ones
3912 * for destination interface has been taken care of
3913 * during output above by virtue of PKTF_FORWARDED.
3915 rcvifp
->if_fpackets
++;
3916 rcvifp
->if_fbytes
+= len
;
3918 OSAddAtomic(1, &ipstat
.ips_forward
);
3920 OSAddAtomic(1, &ipstat
.ips_redirectsent
);
3922 if (mcopy
!= NULL
) {
3924 * If we didn't have to go thru ipflow and
3925 * the packet was successfully consumed by
3926 * ip_output, the mcopy is rather a waste;
3927 * this could be further optimized.
3938 case 0: /* forwarded, but need redirect */
3939 /* type, code set above */
3942 case ENETUNREACH
: /* shouldn't happen, checked above */
3947 type
= ICMP_UNREACH
;
3948 code
= ICMP_UNREACH_HOST
;
3952 type
= ICMP_UNREACH
;
3953 code
= ICMP_UNREACH_NEEDFRAG
;
3959 if (rt
->rt_ifp
!= NULL
)
3960 nextmtu
= rt
->rt_ifp
->if_mtu
;
3968 * If the packet is routed over IPsec tunnel, tell the
3969 * originator the tunnel MTU.
3970 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
3973 sp
= ipsec4_getpolicybyaddr(mcopy
, IPSEC_DIR_OUTBOUND
,
3974 IP_FORWARDING
, &ipsecerror
);
3980 * find the correct route for outer IPv4
3981 * header, compute tunnel MTU.
3985 if (sp
->req
!= NULL
&&
3986 sp
->req
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
3987 struct secasindex saidx
;
3988 struct secasvar
*sav
;
3993 /* count IPsec header size */
3994 ipsechdr
= ipsec_hdrsiz(sp
);
3996 ipm
= mtod(mcopy
, struct ip
*);
3997 bcopy(&sp
->req
->saidx
, &saidx
, sizeof (saidx
));
3998 saidx
.mode
= sp
->req
->saidx
.mode
;
3999 saidx
.reqid
= sp
->req
->saidx
.reqid
;
4000 sin
= SIN(&saidx
.src
);
4001 if (sin
->sin_len
== 0) {
4002 sin
->sin_len
= sizeof (*sin
);
4003 sin
->sin_family
= AF_INET
;
4004 sin
->sin_port
= IPSEC_PORT_ANY
;
4005 bcopy(&ipm
->ip_src
, &sin
->sin_addr
,
4006 sizeof (sin
->sin_addr
));
4008 sin
= SIN(&saidx
.dst
);
4009 if (sin
->sin_len
== 0) {
4010 sin
->sin_len
= sizeof (*sin
);
4011 sin
->sin_family
= AF_INET
;
4012 sin
->sin_port
= IPSEC_PORT_ANY
;
4013 bcopy(&ipm
->ip_dst
, &sin
->sin_addr
,
4014 sizeof (sin
->sin_addr
));
4016 sav
= key_allocsa_policy(&saidx
);
4018 lck_mtx_lock(sadb_mutex
);
4019 if (sav
->sah
!= NULL
) {
4020 ro
= &sav
->sah
->sa_route
;
4021 if (ro
->ro_rt
!= NULL
) {
4023 if (ro
->ro_rt
->rt_ifp
!= NULL
) {
4024 nextmtu
= ro
->ro_rt
->
4026 nextmtu
-= ipsechdr
;
4028 RT_UNLOCK(ro
->ro_rt
);
4031 key_freesav(sav
, KEY_SADB_LOCKED
);
4032 lck_mtx_unlock(sadb_mutex
);
4035 key_freesp(sp
, KEY_SADB_UNLOCKED
);
4041 * A router should not generate ICMP_SOURCEQUENCH as
4042 * required in RFC1812 Requirements for IP Version 4 Routers.
4043 * Source quench could be a big problem under DoS attacks,
4044 * or if the underlying interface is rate-limited.
4045 * Those who need source quench packets may re-enable them
4046 * via the net.inet.ip.sendsourcequench sysctl.
4048 if (ip_sendsourcequench
== 0) {
4052 type
= ICMP_SOURCEQUENCH
;
4057 case EACCES
: /* ipfw denied packet */
4062 if (type
== ICMP_UNREACH
&& code
== ICMP_UNREACH_NEEDFRAG
)
4063 OSAddAtomic(1, &ipstat
.ips_cantfrag
);
4065 icmp_error(mcopy
, type
, code
, dest
, nextmtu
);
4067 ip_fwd_route_copyin(rcvifp
, &fwd_rt
);
4071 ip_savecontrol(struct inpcb
*inp
, struct mbuf
**mp
, struct ip
*ip
,
4075 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
4079 mp
= sbcreatecontrol_mbuf((caddr_t
)&tv
, sizeof (tv
),
4080 SCM_TIMESTAMP
, SOL_SOCKET
, mp
);
4085 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) {
4088 time
= mach_absolute_time();
4089 mp
= sbcreatecontrol_mbuf((caddr_t
)&time
, sizeof (time
),
4090 SCM_TIMESTAMP_MONOTONIC
, SOL_SOCKET
, mp
);
4095 if (inp
->inp_flags
& INP_RECVDSTADDR
) {
4096 mp
= sbcreatecontrol_mbuf((caddr_t
)&ip
->ip_dst
,
4097 sizeof (struct in_addr
), IP_RECVDSTADDR
, IPPROTO_IP
, mp
);
4105 * Moving these out of udp_input() made them even more broken
4106 * than they already were.
4108 /* options were tossed already */
4109 if (inp
->inp_flags
& INP_RECVOPTS
) {
4110 mp
= sbcreatecontrol_mbuf((caddr_t
)opts_deleted_above
,
4111 sizeof (struct in_addr
), IP_RECVOPTS
, IPPROTO_IP
, mp
);
4116 /* ip_srcroute doesn't do what we want here, need to fix */
4117 if (inp
->inp_flags
& INP_RECVRETOPTS
) {
4118 mp
= sbcreatecontrol_mbuf((caddr_t
)ip_srcroute(),
4119 sizeof (struct in_addr
), IP_RECVRETOPTS
, IPPROTO_IP
, mp
);
4125 if (inp
->inp_flags
& INP_RECVIF
) {
4127 uint8_t sdlbuf
[SOCK_MAXADDRLEN
+ 1];
4128 struct sockaddr_dl
*sdl2
= SDL(&sdlbuf
);
4131 * Make sure to accomodate the largest possible
4132 * size of SA(if_lladdr)->sa_len.
4134 _CASSERT(sizeof (sdlbuf
) == (SOCK_MAXADDRLEN
+ 1));
4136 ifnet_head_lock_shared();
4137 if ((ifp
= m
->m_pkthdr
.rcvif
) != NULL
&&
4138 ifp
->if_index
&& (ifp
->if_index
<= if_index
)) {
4139 struct ifaddr
*ifa
= ifnet_addrs
[ifp
->if_index
- 1];
4140 struct sockaddr_dl
*sdp
;
4142 if (!ifa
|| !ifa
->ifa_addr
)
4146 sdp
= SDL(ifa
->ifa_addr
);
4148 * Change our mind and don't try copy.
4150 if (sdp
->sdl_family
!= AF_LINK
) {
4154 /* the above _CASSERT ensures sdl_len fits in sdlbuf */
4155 bcopy(sdp
, sdl2
, sdp
->sdl_len
);
4160 offsetof(struct sockaddr_dl
, sdl_data
[0]);
4161 sdl2
->sdl_family
= AF_LINK
;
4162 sdl2
->sdl_index
= 0;
4163 sdl2
->sdl_nlen
= sdl2
->sdl_alen
= sdl2
->sdl_slen
= 0;
4166 mp
= sbcreatecontrol_mbuf((caddr_t
)sdl2
, sdl2
->sdl_len
,
4167 IP_RECVIF
, IPPROTO_IP
, mp
);
4172 if (inp
->inp_flags
& INP_RECVTTL
) {
4173 mp
= sbcreatecontrol_mbuf((caddr_t
)&ip
->ip_ttl
,
4174 sizeof (ip
->ip_ttl
), IP_RECVTTL
, IPPROTO_IP
, mp
);
4179 if (inp
->inp_socket
->so_flags
& SOF_RECV_TRAFFIC_CLASS
) {
4180 int tc
= m_get_traffic_class(m
);
4182 mp
= sbcreatecontrol_mbuf((caddr_t
)&tc
, sizeof (tc
),
4183 SO_TRAFFIC_CLASS
, SOL_SOCKET
, mp
);
4188 if (inp
->inp_flags
& INP_PKTINFO
) {
4189 struct in_pktinfo pi
;
4191 bzero(&pi
, sizeof (struct in_pktinfo
));
4192 bcopy(&ip
->ip_dst
, &pi
.ipi_addr
, sizeof (struct in_addr
));
4193 pi
.ipi_ifindex
= (m
!= NULL
&& m
->m_pkthdr
.rcvif
!= NULL
) ?
4194 m
->m_pkthdr
.rcvif
->if_index
: 0;
4196 mp
= sbcreatecontrol_mbuf((caddr_t
)&pi
,
4197 sizeof (struct in_pktinfo
), IP_RECVPKTINFO
, IPPROTO_IP
, mp
);
4205 ipstat
.ips_pktdropcntrl
++;
4209 static inline u_short
4210 ip_cksum(struct mbuf
*m
, int hlen
)
4214 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
4215 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
4216 } else if (!(m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) &&
4217 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
4219 * The packet arrived on an interface which isn't capable
4220 * of performing IP header checksum; compute it now.
4222 sum
= ip_cksum_hdr_in(m
, hlen
);
4225 m
->m_pkthdr
.csum_flags
|= (CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
4226 CSUM_IP_CHECKED
| CSUM_IP_VALID
);
4227 m
->m_pkthdr
.csum_data
= 0xffff;
4231 OSAddAtomic(1, &ipstat
.ips_badsum
);
4237 ip_getstat SYSCTL_HANDLER_ARGS
4239 #pragma unused(oidp, arg1, arg2)
4240 if (req
->oldptr
== USER_ADDR_NULL
)
4241 req
->oldlen
= (size_t)sizeof (struct ipstat
);
4243 return (SYSCTL_OUT(req
, &ipstat
, MIN(sizeof (ipstat
), req
->oldlen
)));
4247 ip_setsrcifaddr_info(struct mbuf
*m
, uint32_t src_idx
, struct in_ifaddr
*ia
)
4249 VERIFY(m
->m_flags
& M_PKTHDR
);
4252 * If the source ifaddr is specified, pick up the information
4253 * from there; otherwise just grab the passed-in ifindex as the
4254 * caller may not have the ifaddr available.
4257 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
4258 m
->m_pkthdr
.src_ifindex
= ia
->ia_ifp
->if_index
;
4260 m
->m_pkthdr
.src_ifindex
= src_idx
;
4262 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
4267 ip_setdstifaddr_info(struct mbuf
*m
, uint32_t dst_idx
, struct in_ifaddr
*ia
)
4269 VERIFY(m
->m_flags
& M_PKTHDR
);
4272 * If the destination ifaddr is specified, pick up the information
4273 * from there; otherwise just grab the passed-in ifindex as the
4274 * caller may not have the ifaddr available.
4277 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
4278 m
->m_pkthdr
.dst_ifindex
= ia
->ia_ifp
->if_index
;
4280 m
->m_pkthdr
.dst_ifindex
= dst_idx
;
4282 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
4287 ip_getsrcifaddr_info(struct mbuf
*m
, uint32_t *src_idx
, uint32_t *iaf
)
4289 VERIFY(m
->m_flags
& M_PKTHDR
);
4291 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
))
4294 if (src_idx
!= NULL
)
4295 *src_idx
= m
->m_pkthdr
.src_ifindex
;
4304 ip_getdstifaddr_info(struct mbuf
*m
, uint32_t *dst_idx
, uint32_t *iaf
)
4306 VERIFY(m
->m_flags
& M_PKTHDR
);
4308 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
))
4311 if (dst_idx
!= NULL
)
4312 *dst_idx
= m
->m_pkthdr
.dst_ifindex
;
4321 * Protocol input handler for IPPROTO_GRE.
4324 gre_input(struct mbuf
*m
, int off
)
4326 gre_input_func_t fn
= gre_input_func
;
4329 * If there is a registered GRE input handler, pass mbuf to it.
4332 lck_mtx_unlock(inet_domain_mutex
);
4333 m
= fn(m
, off
, (mtod(m
, struct ip
*))->ip_p
);
4334 lck_mtx_lock(inet_domain_mutex
);
4338 * If no matching tunnel that is up is found, we inject
4339 * the mbuf to raw ip socket to see if anyone picks it up.
4346 * Private KPI for PPP/PPTP.
4349 ip_gre_register_input(gre_input_func_t fn
)
4351 lck_mtx_lock(inet_domain_mutex
);
4352 gre_input_func
= fn
;
4353 lck_mtx_unlock(inet_domain_mutex
);
4359 sysctl_reset_ip_input_stats SYSCTL_HANDLER_ARGS
4361 #pragma unused(arg1, arg2)
4364 i
= ip_input_measure
;
4365 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
4366 if (error
|| req
->newptr
== USER_ADDR_NULL
)
4369 if (i
< 0 || i
> 1) {
4373 if (ip_input_measure
!= i
&& i
== 1) {
4374 net_perf_initialize(&net_perf
, ip_input_measure_bins
);
4376 ip_input_measure
= i
;
4382 sysctl_ip_input_measure_bins SYSCTL_HANDLER_ARGS
4384 #pragma unused(arg1, arg2)
4388 i
= ip_input_measure_bins
;
4389 error
= sysctl_handle_quad(oidp
, &i
, 0, req
);
4390 if (error
|| req
->newptr
== USER_ADDR_NULL
)
4393 if (!net_perf_validate_bins(i
)) {
4397 ip_input_measure_bins
= i
;
4403 sysctl_ip_input_getperf SYSCTL_HANDLER_ARGS
4405 #pragma unused(oidp, arg1, arg2)
4406 if (req
->oldptr
== USER_ADDR_NULL
)
4407 req
->oldlen
= (size_t)sizeof (struct ipstat
);
4409 return (SYSCTL_OUT(req
, &net_perf
, MIN(sizeof (net_perf
), req
->oldlen
)));