2 * Copyright (c) 2000-2016 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>
106 #include <net/init.h>
108 #include <net/pfvar.h>
111 #include <netinet/in.h>
112 #include <netinet/in_systm.h>
113 #include <netinet/in_var.h>
114 #include <netinet/in_arp.h>
115 #include <netinet/ip.h>
116 #include <netinet/in_pcb.h>
117 #include <netinet/ip_var.h>
118 #include <netinet/ip_icmp.h>
119 #include <netinet/ip_fw.h>
120 #include <netinet/ip_divert.h>
121 #include <netinet/kpi_ipfilter_var.h>
122 #include <netinet/udp.h>
123 #include <netinet/udp_var.h>
124 #include <netinet/bootp.h>
125 #include <netinet/lro_ext.h>
128 #include <netinet/ip_dummynet.h>
129 #endif /* DUMMYNET */
132 #include <security/mac_framework.h>
133 #endif /* CONFIG_MACF_NET */
136 #include <netinet6/ipsec.h>
137 #include <netkey/key.h>
140 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 0)
141 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 2)
142 #define DBG_FNC_IP_INPUT NETDBG_CODE(DBG_NETIP, (2 << 8))
145 extern int ipsec_bypass
;
146 extern lck_mtx_t
*sadb_mutex
;
148 lck_grp_t
*sadb_stat_mutex_grp
;
149 lck_grp_attr_t
*sadb_stat_mutex_grp_attr
;
150 lck_attr_t
*sadb_stat_mutex_attr
;
151 decl_lck_mtx_data(, sadb_stat_mutex_data
);
152 lck_mtx_t
*sadb_stat_mutex
= &sadb_stat_mutex_data
;
157 static int frag_timeout_run
; /* frag timer is scheduled to run */
158 static void frag_timeout(void *);
159 static void frag_sched_timeout(void);
161 static struct ipq
*ipq_alloc(int);
162 static void ipq_free(struct ipq
*);
163 static void ipq_updateparams(void);
164 static void ip_input_second_pass(struct mbuf
*, struct ifnet
*,
165 u_int32_t
, int, int, struct ip_fw_in_args
*, int);
167 decl_lck_mtx_data(static, ipqlock
);
168 static lck_attr_t
*ipqlock_attr
;
169 static lck_grp_t
*ipqlock_grp
;
170 static lck_grp_attr_t
*ipqlock_grp_attr
;
172 /* Packet reassembly stuff */
173 #define IPREASS_NHASH_LOG2 6
174 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
175 #define IPREASS_HMASK (IPREASS_NHASH - 1)
176 #define IPREASS_HASH(x, y) \
177 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
179 /* IP fragment reassembly queues (protected by ipqlock) */
180 static TAILQ_HEAD(ipqhead
, ipq
) ipq
[IPREASS_NHASH
]; /* ip reassembly queues */
181 static int maxnipq
; /* max packets in reass queues */
182 static u_int32_t maxfragsperpacket
; /* max frags/packet in reass queues */
183 static u_int32_t nipq
; /* # of packets in reass queues */
184 static u_int32_t ipq_limit
; /* ipq allocation limit */
185 static u_int32_t ipq_count
; /* current # of allocated ipq's */
187 static int sysctl_ipforwarding SYSCTL_HANDLER_ARGS
;
188 static int sysctl_maxnipq SYSCTL_HANDLER_ARGS
;
189 static int sysctl_maxfragsperpacket SYSCTL_HANDLER_ARGS
;
191 #if (DEBUG || DEVELOPMENT)
192 static int sysctl_reset_ip_input_stats SYSCTL_HANDLER_ARGS
;
193 static int sysctl_ip_input_measure_bins SYSCTL_HANDLER_ARGS
;
194 static int sysctl_ip_input_getperf SYSCTL_HANDLER_ARGS
;
195 #endif /* (DEBUG || DEVELOPMENT) */
197 int ipforwarding
= 0;
198 SYSCTL_PROC(_net_inet_ip
, IPCTL_FORWARDING
, forwarding
,
199 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &ipforwarding
, 0,
200 sysctl_ipforwarding
, "I", "Enable IP forwarding between interfaces");
202 static int ipsendredirects
= 1; /* XXX */
203 SYSCTL_INT(_net_inet_ip
, IPCTL_SENDREDIRECTS
, redirect
,
204 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ipsendredirects
, 0,
205 "Enable sending IP redirects");
207 int ip_defttl
= IPDEFTTL
;
208 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFTTL
, ttl
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
209 &ip_defttl
, 0, "Maximum TTL on IP packets");
211 static int ip_dosourceroute
= 0;
212 SYSCTL_INT(_net_inet_ip
, IPCTL_SOURCEROUTE
, sourceroute
,
213 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_dosourceroute
, 0,
214 "Enable forwarding source routed IP packets");
216 static int ip_acceptsourceroute
= 0;
217 SYSCTL_INT(_net_inet_ip
, IPCTL_ACCEPTSOURCEROUTE
, accept_sourceroute
,
218 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_acceptsourceroute
, 0,
219 "Enable accepting source routed IP packets");
221 static int ip_sendsourcequench
= 0;
222 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, sendsourcequench
,
223 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_sendsourcequench
, 0,
224 "Enable the transmission of source quench packets");
226 SYSCTL_PROC(_net_inet_ip
, OID_AUTO
, maxfragpackets
,
227 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &maxnipq
, 0, sysctl_maxnipq
,
228 "I", "Maximum number of IPv4 fragment reassembly queue entries");
230 SYSCTL_UINT(_net_inet_ip
, OID_AUTO
, fragpackets
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
231 &nipq
, 0, "Current number of IPv4 fragment reassembly queue entries");
233 SYSCTL_PROC(_net_inet_ip
, OID_AUTO
, maxfragsperpacket
,
234 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &maxfragsperpacket
, 0,
235 sysctl_maxfragsperpacket
, "I",
236 "Maximum number of IPv4 fragments allowed per packet");
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 #if (DEBUG || DEVELOPMENT)
269 static int ip_input_measure
= 0;
270 SYSCTL_PROC(_net_inet_ip
, OID_AUTO
, input_perf
,
271 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
272 &ip_input_measure
, 0, sysctl_reset_ip_input_stats
, "I", "Do time measurement");
274 static uint64_t ip_input_measure_bins
= 0;
275 SYSCTL_PROC(_net_inet_ip
, OID_AUTO
, input_perf_bins
,
276 CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_input_measure_bins
, 0,
277 sysctl_ip_input_measure_bins
, "I",
278 "bins for chaining performance data histogram");
280 static net_perf_t net_perf
;
281 SYSCTL_PROC(_net_inet_ip
, OID_AUTO
, input_perf_data
,
282 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
283 0, 0, sysctl_ip_input_getperf
, "S,net_perf",
284 "IP input performance data (struct net_perf, net/net_perf.h)");
285 #endif /* (DEBUG || DEVELOPMENT) */
288 static int ipprintfs
= 0;
291 struct protosw
*ip_protox
[IPPROTO_MAX
];
293 static lck_grp_attr_t
*in_ifaddr_rwlock_grp_attr
;
294 static lck_grp_t
*in_ifaddr_rwlock_grp
;
295 static lck_attr_t
*in_ifaddr_rwlock_attr
;
296 decl_lck_rw_data(, in_ifaddr_rwlock_data
);
297 lck_rw_t
*in_ifaddr_rwlock
= &in_ifaddr_rwlock_data
;
299 /* Protected by in_ifaddr_rwlock */
300 struct in_ifaddrhead in_ifaddrhead
; /* first inet address */
301 struct in_ifaddrhashhead
*in_ifaddrhashtbl
; /* inet addr hash table */
303 #define INADDR_NHASH 61
304 static u_int32_t inaddr_nhash
; /* hash table size */
305 static u_int32_t inaddr_hashp
; /* next largest prime */
307 static int ip_getstat SYSCTL_HANDLER_ARGS
;
308 struct ipstat ipstat
;
309 SYSCTL_PROC(_net_inet_ip
, IPCTL_STATS
, stats
,
310 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
311 0, 0, ip_getstat
, "S,ipstat",
312 "IP statistics (struct ipstat, netinet/ip_var.h)");
315 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFMTU
, mtu
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
316 &ip_mtu
, 0, "Default MTU");
317 #endif /* IPCTL_DEFMTU */
320 static int ipstealth
= 0;
321 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, stealth
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
323 #endif /* IPSTEALTH */
327 ip_fw_chk_t
*ip_fw_chk_ptr
;
331 #endif /* IPFIREWALL */
334 ip_dn_io_t
*ip_dn_io_ptr
;
335 #endif /* DUMMYNET */
337 SYSCTL_NODE(_net_inet_ip
, OID_AUTO
, linklocal
,
338 CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "link local");
340 struct ip_linklocal_stat ip_linklocal_stat
;
341 SYSCTL_STRUCT(_net_inet_ip_linklocal
, OID_AUTO
, stat
,
342 CTLFLAG_RD
| CTLFLAG_LOCKED
, &ip_linklocal_stat
, ip_linklocal_stat
,
343 "Number of link local packets with TTL less than 255");
345 SYSCTL_NODE(_net_inet_ip_linklocal
, OID_AUTO
, in
,
346 CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "link local input");
348 int ip_linklocal_in_allowbadttl
= 1;
349 SYSCTL_INT(_net_inet_ip_linklocal_in
, OID_AUTO
, allowbadttl
,
350 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip_linklocal_in_allowbadttl
, 0,
351 "Allow incoming link local packets with TTL less than 255");
355 * We need to save the IP options in case a protocol wants to respond
356 * to an incoming packet over the same route if the packet got here
357 * using IP source routing. This allows connection establishment and
358 * maintenance when the remote end is on a network that is not known
361 static int ip_nhops
= 0;
362 static struct ip_srcrt
{
363 struct in_addr dst
; /* final destination */
364 char nop
; /* one NOP to align */
365 char srcopt
[IPOPT_OFFSET
+ 1]; /* OPTVAL, OLEN and OFFSET */
366 struct in_addr route
[MAX_IPOPTLEN
/ sizeof (struct in_addr
)];
369 static void in_ifaddrhashtbl_init(void);
370 static void save_rte(u_char
*, struct in_addr
);
371 static int ip_dooptions(struct mbuf
*, int, struct sockaddr_in
*);
372 static void ip_forward(struct mbuf
*, int, struct sockaddr_in
*);
373 static void frag_freef(struct ipqhead
*, struct ipq
*);
376 static struct mbuf
*ip_reass(struct mbuf
*, u_int32_t
*, u_int16_t
*);
377 #else /* !IPDIVERT_44 */
378 static struct mbuf
*ip_reass(struct mbuf
*, u_int16_t
*, u_int16_t
*);
379 #endif /* !IPDIVERT_44 */
380 #else /* !IPDIVERT */
381 static struct mbuf
*ip_reass(struct mbuf
*);
382 #endif /* !IPDIVERT */
383 static void ip_fwd_route_copyout(struct ifnet
*, struct route
*);
384 static void ip_fwd_route_copyin(struct ifnet
*, struct route
*);
385 static inline u_short
ip_cksum(struct mbuf
*, int);
387 int ip_use_randomid
= 1;
388 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, random_id
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
389 &ip_use_randomid
, 0, "Randomize IP packets IDs");
392 * On platforms which require strict alignment (currently for anything but
393 * i386 or x86_64), check if the IP header pointer is 32-bit aligned; if not,
394 * copy the contents of the mbuf chain into a new chain, and free the original
395 * one. Create some head room in the first mbuf of the new chain, in case
396 * it's needed later on.
398 #if defined(__i386__) || defined(__x86_64__)
399 #define IP_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { } while (0)
400 #else /* !__i386__ && !__x86_64__ */
401 #define IP_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { \
402 if (!IP_HDR_ALIGNED_P(mtod(_m, caddr_t))) { \
404 struct ifnet *__ifp = (_ifp); \
405 atomic_add_64(&(__ifp)->if_alignerrs, 1); \
406 if (((_m)->m_flags & M_PKTHDR) && \
407 (_m)->m_pkthdr.pkt_hdr != NULL) \
408 (_m)->m_pkthdr.pkt_hdr = NULL; \
409 _n = m_defrag_offset(_m, max_linkhdr, M_NOWAIT); \
411 atomic_add_32(&ipstat.ips_toosmall, 1); \
416 VERIFY(_n != (_m)); \
421 #endif /* !__i386__ && !__x86_64__ */
424 * GRE input handler function, settable via ip_gre_register_input() for PPTP.
426 static gre_input_func_t gre_input_func
;
429 ip_init_delayed(void)
433 struct sockaddr_in
*sin
;
435 bzero(&ifr
, sizeof(ifr
));
436 strlcpy(ifr
.ifr_name
, "lo0", sizeof(ifr
.ifr_name
));
437 sin
= (struct sockaddr_in
*)(void *)&ifr
.ifr_addr
;
438 sin
->sin_len
= sizeof(struct sockaddr_in
);
439 sin
->sin_family
= AF_INET
;
440 sin
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
441 error
= in_control(NULL
, SIOCSIFADDR
, (caddr_t
)&ifr
, lo_ifp
, kernproc
);
443 printf("%s: failed to initialise lo0's address, error=%d\n",
448 * IP initialization: fill in IP protocol switch table.
449 * All protocols not implemented in kernel go to raw IP protocol handler.
452 ip_init(struct protosw
*pp
, struct domain
*dp
)
454 static int ip_initialized
= 0;
459 domain_proto_mtx_lock_assert_held();
460 VERIFY((pp
->pr_flags
& (PR_INITIALIZED
|PR_ATTACHED
)) == PR_ATTACHED
);
462 /* ipq_alloc() uses mbufs for IP fragment queue structures */
463 _CASSERT(sizeof (struct ipq
) <= _MLEN
);
466 * Some ioctls (e.g. SIOCAIFADDR) use ifaliasreq struct, which is
467 * interchangeable with in_aliasreq; they must have the same size.
469 _CASSERT(sizeof (struct ifaliasreq
) == sizeof (struct in_aliasreq
));
477 in_ifaddr_rwlock_grp_attr
= lck_grp_attr_alloc_init();
478 in_ifaddr_rwlock_grp
= lck_grp_alloc_init("in_ifaddr_rwlock",
479 in_ifaddr_rwlock_grp_attr
);
480 in_ifaddr_rwlock_attr
= lck_attr_alloc_init();
481 lck_rw_init(in_ifaddr_rwlock
, in_ifaddr_rwlock_grp
,
482 in_ifaddr_rwlock_attr
);
484 TAILQ_INIT(&in_ifaddrhead
);
485 in_ifaddrhashtbl_init();
489 pr
= pffindproto_locked(PF_INET
, IPPROTO_RAW
, SOCK_RAW
);
491 panic("%s: Unable to find [PF_INET,IPPROTO_RAW,SOCK_RAW]\n",
496 /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
497 for (i
= 0; i
< IPPROTO_MAX
; i
++)
500 * Cycle through IP protocols and put them into the appropriate place
501 * in ip_protox[], skipping protocols IPPROTO_{IP,RAW}.
503 VERIFY(dp
== inetdomain
&& dp
->dom_family
== PF_INET
);
504 TAILQ_FOREACH(pr
, &dp
->dom_protosw
, pr_entry
) {
505 VERIFY(pr
->pr_domain
== dp
);
506 if (pr
->pr_protocol
!= 0 && pr
->pr_protocol
!= IPPROTO_RAW
) {
507 /* Be careful to only index valid IP protocols. */
508 if (pr
->pr_protocol
< IPPROTO_MAX
)
509 ip_protox
[pr
->pr_protocol
] = pr
;
513 /* IP fragment reassembly queue lock */
514 ipqlock_grp_attr
= lck_grp_attr_alloc_init();
515 ipqlock_grp
= lck_grp_alloc_init("ipqlock", ipqlock_grp_attr
);
516 ipqlock_attr
= lck_attr_alloc_init();
517 lck_mtx_init(&ipqlock
, ipqlock_grp
, ipqlock_attr
);
519 lck_mtx_lock(&ipqlock
);
520 /* Initialize IP reassembly queue. */
521 for (i
= 0; i
< IPREASS_NHASH
; i
++)
524 maxnipq
= nmbclusters
/ 32;
525 maxfragsperpacket
= 128; /* enough for 64k in 512 byte fragments */
527 lck_mtx_unlock(&ipqlock
);
530 ip_id
= RandomULong() ^ tv
.tv_usec
;
536 sadb_stat_mutex_grp_attr
= lck_grp_attr_alloc_init();
537 sadb_stat_mutex_grp
= lck_grp_alloc_init("sadb_stat",
538 sadb_stat_mutex_grp_attr
);
539 sadb_stat_mutex_attr
= lck_attr_alloc_init();
540 lck_mtx_init(sadb_stat_mutex
, sadb_stat_mutex_grp
,
541 sadb_stat_mutex_attr
);
545 net_init_add(ip_init_delayed
);
549 * Initialize IPv4 source address hash table.
552 in_ifaddrhashtbl_init(void)
556 if (in_ifaddrhashtbl
!= NULL
)
559 PE_parse_boot_argn("inaddr_nhash", &inaddr_nhash
,
560 sizeof (inaddr_nhash
));
561 if (inaddr_nhash
== 0)
562 inaddr_nhash
= INADDR_NHASH
;
564 MALLOC(in_ifaddrhashtbl
, struct in_ifaddrhashhead
*,
565 inaddr_nhash
* sizeof (*in_ifaddrhashtbl
),
566 M_IFADDR
, M_WAITOK
| M_ZERO
);
567 if (in_ifaddrhashtbl
== NULL
)
568 panic("in_ifaddrhashtbl_init allocation failed");
571 * Generate the next largest prime greater than inaddr_nhash.
573 k
= (inaddr_nhash
% 2 == 0) ? inaddr_nhash
+ 1 : inaddr_nhash
+ 2;
576 for (i
= 3; i
* i
<= k
; i
+= 2) {
588 inaddr_hashval(u_int32_t key
)
591 * The hash index is the computed prime times the key modulo
592 * the hash size, as documented in "Introduction to Algorithms"
593 * (Cormen, Leiserson, Rivest).
595 if (inaddr_nhash
> 1)
596 return ((key
* inaddr_hashp
) % inaddr_nhash
);
602 ip_proto_dispatch_in_wrapper(struct mbuf
*m
, int hlen
, u_int8_t proto
)
604 ip_proto_dispatch_in(m
, hlen
, proto
, 0);
607 __private_extern__
void
608 ip_proto_dispatch_in(struct mbuf
*m
, int hlen
, u_int8_t proto
,
609 ipfilter_t inject_ipfref
)
611 struct ipfilter
*filter
;
612 int seen
= (inject_ipfref
== NULL
);
613 int changed_header
= 0;
615 void (*pr_input
)(struct mbuf
*, int len
);
617 if (!TAILQ_EMPTY(&ipv4_filters
)) {
619 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
621 if ((struct ipfilter
*)inject_ipfref
== filter
)
623 } else if (filter
->ipf_filter
.ipf_input
) {
626 if (changed_header
== 0) {
628 * Perform IP header alignment fixup,
629 * if needed, before passing packet
632 IP_HDR_ALIGNMENT_FIXUP(m
,
633 m
->m_pkthdr
.rcvif
, ipf_unref());
635 /* ipf_unref() already called */
640 ip
= mtod(m
, struct ip
*);
641 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
642 ip
->ip_off
= htons(ip
->ip_off
);
644 ip
->ip_sum
= ip_cksum_hdr_in(m
, hlen
);
646 result
= filter
->ipf_filter
.ipf_input(
647 filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
,
649 if (result
== EJUSTRETURN
) {
663 /* Perform IP header alignment fixup (post-filters), if needed */
664 IP_HDR_ALIGNMENT_FIXUP(m
, m
->m_pkthdr
.rcvif
, return);
667 * If there isn't a specific lock for the protocol
668 * we're about to call, use the generic lock for AF_INET.
669 * otherwise let the protocol deal with its own locking
671 ip
= mtod(m
, struct ip
*);
673 if (changed_header
) {
674 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
675 ip
->ip_off
= ntohs(ip
->ip_off
);
678 if ((pr_input
= ip_protox
[ip
->ip_p
]->pr_input
) == NULL
) {
680 } else if (!(ip_protox
[ip
->ip_p
]->pr_flags
& PR_PROTOLOCK
)) {
681 lck_mtx_lock(inet_domain_mutex
);
683 lck_mtx_unlock(inet_domain_mutex
);
689 struct pktchain_elm
{
690 struct mbuf
*pkte_head
;
691 struct mbuf
*pkte_tail
;
692 struct in_addr pkte_saddr
;
693 struct in_addr pkte_daddr
;
696 uint32_t pkte_nbytes
;
699 typedef struct pktchain_elm pktchain_elm_t
;
701 /* Store upto PKTTBL_SZ unique flows on the stack */
705 ip_chain_insert(struct mbuf
*packet
, pktchain_elm_t
*tbl
)
710 ip
= mtod(packet
, struct ip
*);
712 /* reusing the hash function from inaddr_hashval */
713 pkttbl_idx
= inaddr_hashval(ntohs(ip
->ip_src
.s_addr
)) % PKTTBL_SZ
;
714 if (tbl
[pkttbl_idx
].pkte_head
== NULL
) {
715 tbl
[pkttbl_idx
].pkte_head
= packet
;
716 tbl
[pkttbl_idx
].pkte_saddr
.s_addr
= ip
->ip_src
.s_addr
;
717 tbl
[pkttbl_idx
].pkte_daddr
.s_addr
= ip
->ip_dst
.s_addr
;
718 tbl
[pkttbl_idx
].pkte_proto
= ip
->ip_p
;
720 if ((ip
->ip_dst
.s_addr
== tbl
[pkttbl_idx
].pkte_daddr
.s_addr
) &&
721 (ip
->ip_src
.s_addr
== tbl
[pkttbl_idx
].pkte_saddr
.s_addr
) &&
722 (ip
->ip_p
== tbl
[pkttbl_idx
].pkte_proto
)) {
727 if (tbl
[pkttbl_idx
].pkte_tail
!= NULL
)
728 mbuf_setnextpkt(tbl
[pkttbl_idx
].pkte_tail
, packet
);
730 tbl
[pkttbl_idx
].pkte_tail
= packet
;
731 tbl
[pkttbl_idx
].pkte_npkts
+= 1;
732 tbl
[pkttbl_idx
].pkte_nbytes
+= packet
->m_pkthdr
.len
;
736 /* args is a dummy variable here for backward compatibility */
738 ip_input_second_pass_loop_tbl(pktchain_elm_t
*tbl
, struct ip_fw_in_args
*args
)
742 for (i
= 0; i
< PKTTBL_SZ
; i
++) {
743 if (tbl
[i
].pkte_head
!= NULL
) {
744 struct mbuf
*m
= tbl
[i
].pkte_head
;
745 ip_input_second_pass(m
, m
->m_pkthdr
.rcvif
, 0,
746 tbl
[i
].pkte_npkts
, tbl
[i
].pkte_nbytes
, args
, 0);
748 if (tbl
[i
].pkte_npkts
> 2)
749 ipstat
.ips_rxc_chainsz_gt2
++;
750 if (tbl
[i
].pkte_npkts
> 4)
751 ipstat
.ips_rxc_chainsz_gt4
++;
752 #if (DEBUG || DEVELOPMENT)
753 if (ip_input_measure
)
754 net_perf_histogram(&net_perf
, tbl
[i
].pkte_npkts
);
755 #endif /* (DEBUG || DEVELOPMENT) */
756 tbl
[i
].pkte_head
= tbl
[i
].pkte_tail
= NULL
;
757 tbl
[i
].pkte_npkts
= 0;
758 tbl
[i
].pkte_nbytes
= 0;
759 /* no need to initialize address and protocol in tbl */
765 ip_input_cpout_args(struct ip_fw_in_args
*args
, struct ip_fw_args
*args1
,
766 boolean_t
*done_init
)
768 if (*done_init
== FALSE
) {
769 bzero(args1
, sizeof(struct ip_fw_args
));
772 args1
->fwa_next_hop
= args
->fwai_next_hop
;
773 args1
->fwa_ipfw_rule
= args
->fwai_ipfw_rule
;
774 args1
->fwa_pf_rule
= args
->fwai_pf_rule
;
775 args1
->fwa_divert_rule
= args
->fwai_divert_rule
;
779 ip_input_cpin_args(struct ip_fw_args
*args1
, struct ip_fw_in_args
*args
)
781 args
->fwai_next_hop
= args1
->fwa_next_hop
;
782 args
->fwai_ipfw_rule
= args1
->fwa_ipfw_rule
;
783 args
->fwai_pf_rule
= args1
->fwa_pf_rule
;
784 args
->fwai_divert_rule
= args1
->fwa_divert_rule
;
792 } ipinput_chain_ret_t
;
795 ip_input_update_nstat(struct ifnet
*ifp
, struct in_addr src_ip
,
796 u_int32_t packets
, u_int32_t bytes
)
799 struct rtentry
*rt
= ifnet_cached_rtlookup_inet(ifp
,
802 nstat_route_rx(rt
, packets
, bytes
, 0);
809 ip_input_dispatch_chain(struct mbuf
*m
)
811 struct mbuf
*tmp_mbuf
= m
;
812 struct mbuf
*nxt_mbuf
= NULL
;
813 struct ip
*ip
= NULL
;
816 ip
= mtod(tmp_mbuf
, struct ip
*);
817 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
819 nxt_mbuf
= mbuf_nextpkt(tmp_mbuf
);
820 mbuf_setnextpkt(tmp_mbuf
, NULL
);
822 if ((sw_lro
) && (ip
->ip_p
== IPPROTO_TCP
))
823 tmp_mbuf
= tcp_lro(tmp_mbuf
, hlen
);
825 ip_proto_dispatch_in(tmp_mbuf
, hlen
, ip
->ip_p
, 0);
828 ip
= mtod(tmp_mbuf
, struct ip
*);
829 /* first mbuf of chain already has adjusted ip_len */
830 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
837 ip_input_setdst_chain(struct mbuf
*m
, uint32_t ifindex
, struct in_ifaddr
*ia
)
839 struct mbuf
*tmp_mbuf
= m
;
842 ip_setdstifaddr_info(tmp_mbuf
, ifindex
, ia
);
843 tmp_mbuf
= mbuf_nextpkt(tmp_mbuf
);
848 * First pass does all essential packet validation and places on a per flow
849 * queue for doing operations that have same outcome for all packets of a flow.
850 * div_info is packet divert/tee info
852 static ipinput_chain_ret_t
853 ip_input_first_pass(struct mbuf
*m
, u_int32_t
*div_info
,
854 struct ip_fw_in_args
*args
, int *ours
, struct mbuf
**modm
)
859 int retval
= IPINPUT_DOCHAIN
;
861 struct in_addr src_ip
;
865 #if IPFIREWALL || DUMMYNET
868 boolean_t
delete = FALSE
;
869 struct ip_fw_args args1
;
870 boolean_t init
= FALSE
;
872 ipfilter_t inject_filter_ref
= NULL
;
875 #pragma unused (args)
879 #pragma unused (div_info)
880 #pragma unused (ours)
883 #if !IPFIREWALL_FORWARD
884 #pragma unused (ours)
887 /* Check if the mbuf is still valid after interface filter processing */
888 MBUF_INPUT_CHECK(m
, m
->m_pkthdr
.rcvif
);
889 inifp
= mbuf_pkthdr_rcvif(m
);
890 VERIFY(inifp
!= NULL
);
892 /* Perform IP header alignment fixup, if needed */
893 IP_HDR_ALIGNMENT_FIXUP(m
, inifp
, goto bad
);
895 m
->m_pkthdr
.pkt_flags
&= ~PKTF_FORWARDED
;
897 #if IPFIREWALL || DUMMYNET
900 * Don't bother searching for tag(s) if there's none.
902 if (SLIST_EMPTY(&m
->m_pkthdr
.tags
))
905 /* Grab info from mtags prepended to the chain */
908 if (p
->m_tag_id
== KERNEL_MODULE_TAG_ID
) {
910 if (p
->m_tag_type
== KERNEL_TAG_TYPE_DUMMYNET
) {
911 struct dn_pkt_tag
*dn_tag
;
913 dn_tag
= (struct dn_pkt_tag
*)(p
+1);
914 args
->fwai_ipfw_rule
= dn_tag
->dn_ipfw_rule
;
915 args
->fwai_pf_rule
= dn_tag
->dn_pf_rule
;
921 if (p
->m_tag_type
== KERNEL_TAG_TYPE_DIVERT
) {
922 struct divert_tag
*div_tag
;
924 div_tag
= (struct divert_tag
*)(p
+1);
925 args
->fwai_divert_rule
= div_tag
->cookie
;
930 if (p
->m_tag_type
== KERNEL_TAG_TYPE_IPFORWARD
) {
931 struct ip_fwd_tag
*ipfwd_tag
;
933 ipfwd_tag
= (struct ip_fwd_tag
*)(p
+1);
934 args
->fwai_next_hop
= ipfwd_tag
->next_hop
;
940 p
= m_tag_next(m
, p
);
941 m_tag_delete(m
, copy
);
943 p
= m_tag_next(m
, p
);
946 p
= m_tag_next(m
, p
);
951 if (m
== NULL
|| !(m
->m_flags
& M_PKTHDR
))
952 panic("ip_input no HDR");
956 if (args
->fwai_ipfw_rule
|| args
->fwai_pf_rule
) {
957 /* dummynet already filtered us */
958 ip
= mtod(m
, struct ip
*);
959 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
960 inject_filter_ref
= ipf_get_inject_filter(m
);
962 if (args
->fwai_ipfw_rule
)
964 #endif /* IPFIREWALL */
965 if (args
->fwai_pf_rule
)
968 #endif /* DUMMYNET */
970 #endif /* IPFIREWALL || DUMMYNET */
973 * No need to process packet twice if we've already seen it.
975 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
))
976 inject_filter_ref
= ipf_get_inject_filter(m
);
977 if (inject_filter_ref
!= NULL
) {
978 ip
= mtod(m
, struct ip
*);
979 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
981 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
982 struct ip
*, ip
, struct ifnet
*, inifp
,
983 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
985 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
986 ip
->ip_off
= ntohs(ip
->ip_off
);
987 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, inject_filter_ref
);
988 return (IPINPUT_DONE
);
991 if (m
->m_pkthdr
.len
< sizeof (struct ip
)) {
992 OSAddAtomic(1, &ipstat
.ips_total
);
993 OSAddAtomic(1, &ipstat
.ips_tooshort
);
995 return (IPINPUT_FREED
);
998 if (m
->m_len
< sizeof (struct ip
) &&
999 (m
= m_pullup(m
, sizeof (struct ip
))) == NULL
) {
1000 OSAddAtomic(1, &ipstat
.ips_total
);
1001 OSAddAtomic(1, &ipstat
.ips_toosmall
);
1002 return (IPINPUT_FREED
);
1005 ip
= mtod(m
, struct ip
*);
1008 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
, ip
->ip_src
.s_addr
,
1009 ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1011 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
1012 OSAddAtomic(1, &ipstat
.ips_total
);
1013 OSAddAtomic(1, &ipstat
.ips_badvers
);
1014 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1016 return (IPINPUT_FREED
);
1019 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1020 if (hlen
< sizeof (struct ip
)) {
1021 OSAddAtomic(1, &ipstat
.ips_total
);
1022 OSAddAtomic(1, &ipstat
.ips_badhlen
);
1023 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1025 return (IPINPUT_FREED
);
1028 if (hlen
> m
->m_len
) {
1029 if ((m
= m_pullup(m
, hlen
)) == NULL
) {
1030 OSAddAtomic(1, &ipstat
.ips_total
);
1031 OSAddAtomic(1, &ipstat
.ips_badhlen
);
1032 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1033 return (IPINPUT_FREED
);
1035 ip
= mtod(m
, struct ip
*);
1039 /* 127/8 must not appear on wire - RFC1122 */
1040 if ((ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
1041 (ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
) {
1043 * Allow for the following exceptions:
1045 * 1. If the packet was sent to loopback (i.e. rcvif
1046 * would have been set earlier at output time.)
1048 * 2. If the packet was sent out on loopback from a local
1049 * source address which belongs to a non-loopback
1050 * interface (i.e. rcvif may not necessarily be a
1051 * loopback interface, hence the test for PKTF_LOOP.)
1052 * Unlike IPv6, there is no interface scope ID, and
1053 * therefore we don't care so much about PKTF_IFINFO.
1055 if (!(inifp
->if_flags
& IFF_LOOPBACK
) &&
1056 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
1057 OSAddAtomic(1, &ipstat
.ips_total
);
1058 OSAddAtomic(1, &ipstat
.ips_badaddr
);
1059 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1061 return (IPINPUT_FREED
);
1065 /* IPv4 Link-Local Addresses as defined in RFC3927 */
1066 if ((IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
)) ||
1067 IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)))) {
1068 ip_linklocal_stat
.iplls_in_total
++;
1069 if (ip
->ip_ttl
!= MAXTTL
) {
1070 OSAddAtomic(1, &ip_linklocal_stat
.iplls_in_badttl
);
1071 /* Silently drop link local traffic with bad TTL */
1072 if (!ip_linklocal_in_allowbadttl
) {
1073 OSAddAtomic(1, &ipstat
.ips_total
);
1074 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1076 return (IPINPUT_FREED
);
1081 if (ip_cksum(m
, hlen
)) {
1082 OSAddAtomic(1, &ipstat
.ips_total
);
1083 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1085 return (IPINPUT_FREED
);
1088 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1089 struct ip
*, ip
, struct ifnet
*, inifp
,
1090 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
1093 * Convert fields to host representation.
1095 #if BYTE_ORDER != BIG_ENDIAN
1099 if (ip
->ip_len
< hlen
) {
1100 OSAddAtomic(1, &ipstat
.ips_total
);
1101 OSAddAtomic(1, &ipstat
.ips_badlen
);
1102 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1104 return (IPINPUT_FREED
);
1107 #if BYTE_ORDER != BIG_ENDIAN
1112 * Check that the amount of data in the buffers
1113 * is as at least much as the IP header would have us expect.
1114 * Trim mbufs if longer than we expect.
1115 * Drop packet if shorter than we expect.
1117 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
1118 OSAddAtomic(1, &ipstat
.ips_total
);
1119 OSAddAtomic(1, &ipstat
.ips_tooshort
);
1120 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1122 return (IPINPUT_FREED
);
1125 if (m
->m_pkthdr
.len
> ip
->ip_len
) {
1127 * Invalidate hardware checksum info if ip_adj_clear_hwcksum
1128 * is set; useful to handle buggy drivers. Note that this
1129 * should not be enabled by default, as we may get here due
1130 * to link-layer padding.
1132 if (ip_adj_clear_hwcksum
&&
1133 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
) &&
1134 !(inifp
->if_flags
& IFF_LOOPBACK
) &&
1135 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
1136 m
->m_pkthdr
.csum_flags
&= ~CSUM_DATA_VALID
;
1137 m
->m_pkthdr
.csum_data
= 0;
1138 ipstat
.ips_adj_hwcsum_clr
++;
1142 if (m
->m_len
== m
->m_pkthdr
.len
) {
1143 m
->m_len
= ip
->ip_len
;
1144 m
->m_pkthdr
.len
= ip
->ip_len
;
1146 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
1149 /* for consistency */
1150 m
->m_pkthdr
.pkt_proto
= ip
->ip_p
;
1152 /* for netstat route statistics */
1153 src_ip
= ip
->ip_src
;
1154 len
= m
->m_pkthdr
.len
;
1160 /* Invoke inbound packet filter */
1161 if (PF_IS_ENABLED
) {
1163 ip_input_cpout_args(args
, &args1
, &init
);
1166 error
= pf_af_hook(inifp
, NULL
, &m
, AF_INET
, TRUE
, &args1
);
1168 error
= pf_af_hook(inifp
, NULL
, &m
, AF_INET
, TRUE
, NULL
);
1169 #endif /* DUMMYNET */
1170 if (error
!= 0 || m
== NULL
) {
1172 panic("%s: unexpected packet %p\n",
1176 /* Already freed by callee */
1177 ip_input_update_nstat(inifp
, src_ip
, 1, len
);
1178 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1179 OSAddAtomic(1, &ipstat
.ips_total
);
1180 return (IPINPUT_FREED
);
1182 ip
= mtod(m
, struct ip
*);
1183 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1185 ip_input_cpin_args(&args1
, args
);
1190 if (ipsec_bypass
== 0 && ipsec_gethist(m
, NULL
)) {
1191 retval
= IPINPUT_DONTCHAIN
; /* XXX scope for chaining here? */
1199 #endif /* DUMMYNET */
1201 * Check if we want to allow this packet to be processed.
1202 * Consider it to be bad if not.
1204 if (fw_enable
&& IPFW_LOADED
) {
1205 #if IPFIREWALL_FORWARD
1207 * If we've been forwarded from the output side, then
1208 * skip the firewall a second time
1210 if (args
->fwai_next_hop
) {
1212 return (IPINPUT_DONTCHAIN
);
1214 #endif /* IPFIREWALL_FORWARD */
1215 ip_input_cpout_args(args
, &args1
, &init
);
1218 i
= ip_fw_chk_ptr(&args1
);
1221 if ((i
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) { /* drop */
1224 ip_input_update_nstat(inifp
, src_ip
, 1, len
);
1225 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1226 OSAddAtomic(1, &ipstat
.ips_total
);
1227 return (IPINPUT_FREED
);
1229 ip
= mtod(m
, struct ip
*); /* just in case m changed */
1231 ip_input_cpin_args(&args1
, args
);
1233 if (i
== 0 && args
->fwai_next_hop
== NULL
) { /* common case */
1237 if (DUMMYNET_LOADED
&& (i
& IP_FW_PORT_DYNT_FLAG
) != 0) {
1238 /* Send packet to the appropriate pipe */
1239 ip_dn_io_ptr(m
, i
&0xffff, DN_TO_IP_IN
, &args1
,
1241 ip_input_update_nstat(inifp
, src_ip
, 1, len
);
1242 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1243 OSAddAtomic(1, &ipstat
.ips_total
);
1244 return (IPINPUT_FREED
);
1246 #endif /* DUMMYNET */
1248 if (i
!= 0 && (i
& IP_FW_PORT_DYNT_FLAG
) == 0) {
1249 /* Divert or tee packet */
1252 return (IPINPUT_DONTCHAIN
);
1255 #if IPFIREWALL_FORWARD
1256 if (i
== 0 && args
->fwai_next_hop
!= NULL
) {
1257 retval
= IPINPUT_DONTCHAIN
;
1262 * if we get here, the packet must be dropped
1264 ip_input_update_nstat(inifp
, src_ip
, 1, len
);
1265 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1267 OSAddAtomic(1, &ipstat
.ips_total
);
1268 return (IPINPUT_FREED
);
1270 #endif /* IPFIREWALL */
1271 #if IPSEC | IPFIREWALL
1275 * Process options and, if not destined for us,
1276 * ship it on. ip_dooptions returns 1 when an
1277 * error was detected (causing an icmp message
1278 * to be sent and the original packet to be freed).
1280 ip_nhops
= 0; /* for source routed packets */
1282 if (hlen
> sizeof (struct ip
) &&
1283 ip_dooptions(m
, 0, args
->fwai_next_hop
)) {
1284 #else /* !IPFIREWALL */
1285 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, NULL
)) {
1286 #endif /* !IPFIREWALL */
1287 ip_input_update_nstat(inifp
, src_ip
, 1, len
);
1288 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1289 OSAddAtomic(1, &ipstat
.ips_total
);
1290 return (IPINPUT_FREED
);
1294 * Don't chain fragmented packets as the process of determining
1295 * if it is our fragment or someone else's plus the complexity of
1296 * divert and fw args makes it harder to do chaining.
1298 if (ip
->ip_off
& ~(IP_DF
| IP_RF
))
1299 return (IPINPUT_DONTCHAIN
);
1301 /* Allow DHCP/BootP responses through */
1302 if ((inifp
->if_eflags
& IFEF_AUTOCONFIGURING
) &&
1303 hlen
== sizeof (struct ip
) && ip
->ip_p
== IPPROTO_UDP
) {
1304 struct udpiphdr
*ui
;
1306 if (m
->m_len
< sizeof (struct udpiphdr
) &&
1307 (m
= m_pullup(m
, sizeof (struct udpiphdr
))) == NULL
) {
1308 OSAddAtomic(1, &udpstat
.udps_hdrops
);
1309 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1310 OSAddAtomic(1, &ipstat
.ips_total
);
1311 return (IPINPUT_FREED
);
1314 ui
= mtod(m
, struct udpiphdr
*);
1315 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) {
1316 ip_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
1317 return (IPINPUT_DONTCHAIN
);
1321 /* Avoid chaining raw sockets as ipsec checks occur later for them */
1322 if (ip_protox
[ip
->ip_p
]->pr_flags
& PR_LASTHDR
)
1323 return (IPINPUT_DONTCHAIN
);
1326 #if !defined(__i386__) && !defined(__x86_64__)
1329 return (IPINPUT_FREED
);
1334 ip_input_second_pass(struct mbuf
*m
, struct ifnet
*inifp
, u_int32_t div_info
,
1335 int npkts_in_chain
, int bytes_in_chain
, struct ip_fw_in_args
*args
, int ours
)
1337 unsigned int checkif
;
1338 struct mbuf
*tmp_mbuf
= NULL
;
1339 struct in_ifaddr
*ia
= NULL
;
1340 struct in_addr pkt_dst
;
1344 #pragma unused (args)
1348 #pragma unused (div_info)
1351 struct ip
*ip
= mtod(m
, struct ip
*);
1352 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1354 OSAddAtomic(npkts_in_chain
, &ipstat
.ips_total
);
1357 * Naively assume we can attribute inbound data to the route we would
1358 * use to send to this destination. Asymmetric routing breaks this
1359 * assumption, but it still allows us to account for traffic from
1360 * a remote node in the routing table.
1361 * this has a very significant performance impact so we bypass
1362 * if nstat_collect is disabled. We may also bypass if the
1363 * protocol is tcp in the future because tcp will have a route that
1364 * we can use to attribute the data to. That does mean we would not
1365 * account for forwarded tcp traffic.
1367 ip_input_update_nstat(inifp
, ip
->ip_src
, npkts_in_chain
,
1374 * Check our list of addresses, to see if the packet is for us.
1375 * If we don't have any addresses, assume any unicast packet
1376 * we receive might be for us (and let the upper layers deal
1380 if (TAILQ_EMPTY(&in_ifaddrhead
)) {
1382 if (!(tmp_mbuf
->m_flags
& (M_MCAST
|M_BCAST
))) {
1383 ip_setdstifaddr_info(tmp_mbuf
, inifp
->if_index
,
1386 tmp_mbuf
= mbuf_nextpkt(tmp_mbuf
);
1391 * Cache the destination address of the packet; this may be
1392 * changed by use of 'ipfw fwd'.
1395 pkt_dst
= args
->fwai_next_hop
== NULL
?
1396 ip
->ip_dst
: args
->fwai_next_hop
->sin_addr
;
1397 #else /* !IPFIREWALL */
1398 pkt_dst
= ip
->ip_dst
;
1399 #endif /* !IPFIREWALL */
1402 * Enable a consistency check between the destination address
1403 * and the arrival interface for a unicast packet (the RFC 1122
1404 * strong ES model) if IP forwarding is disabled and the packet
1405 * is not locally generated and the packet is not subject to
1408 * XXX - Checking also should be disabled if the destination
1409 * address is ipnat'ed to a different interface.
1411 * XXX - Checking is incompatible with IP aliases added
1412 * to the loopback interface instead of the interface where
1413 * the packets are received.
1415 checkif
= ip_checkinterface
&& (ipforwarding
== 0) &&
1416 !(inifp
->if_flags
& IFF_LOOPBACK
) &&
1417 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)
1419 && (args
->fwai_next_hop
== NULL
);
1420 #else /* !IPFIREWALL */
1422 #endif /* !IPFIREWALL */
1425 * Check for exact addresses in the hash bucket.
1427 lck_rw_lock_shared(in_ifaddr_rwlock
);
1428 TAILQ_FOREACH(ia
, INADDR_HASH(pkt_dst
.s_addr
), ia_hash
) {
1430 * If the address matches, verify that the packet
1431 * arrived via the correct interface if checking is
1434 if (IA_SIN(ia
)->sin_addr
.s_addr
== pkt_dst
.s_addr
&&
1435 (!checkif
|| ia
->ia_ifp
== inifp
)) {
1436 ip_input_setdst_chain(m
, 0, ia
);
1437 lck_rw_done(in_ifaddr_rwlock
);
1441 lck_rw_done(in_ifaddr_rwlock
);
1444 * Check for broadcast addresses.
1446 * Only accept broadcast packets that arrive via the matching
1447 * interface. Reception of forwarded directed broadcasts would be
1448 * handled via ip_forward() and ether_frameout() with the loopback
1449 * into the stack for SIMPLEX interfaces handled by ether_frameout().
1451 if (inifp
->if_flags
& IFF_BROADCAST
) {
1454 ifnet_lock_shared(inifp
);
1455 TAILQ_FOREACH(ifa
, &inifp
->if_addrhead
, ifa_link
) {
1456 if (ifa
->ifa_addr
->sa_family
!= AF_INET
) {
1460 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
1461 pkt_dst
.s_addr
|| ia
->ia_netbroadcast
.s_addr
==
1463 ip_input_setdst_chain(m
, 0, ia
);
1464 ifnet_lock_done(inifp
);
1468 ifnet_lock_done(inifp
);
1471 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
1472 struct in_multi
*inm
;
1474 * See if we belong to the destination multicast group on the
1475 * arrival interface.
1477 in_multihead_lock_shared();
1478 IN_LOOKUP_MULTI(&ip
->ip_dst
, inifp
, inm
);
1479 in_multihead_lock_done();
1481 OSAddAtomic(npkts_in_chain
, &ipstat
.ips_notmember
);
1483 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1486 ip_input_setdst_chain(m
, inifp
->if_index
, NULL
);
1491 if (ip
->ip_dst
.s_addr
== (u_int32_t
)INADDR_BROADCAST
||
1492 ip
->ip_dst
.s_addr
== INADDR_ANY
) {
1493 ip_input_setdst_chain(m
, inifp
->if_index
, NULL
);
1497 if (ip
->ip_p
== IPPROTO_UDP
) {
1498 struct udpiphdr
*ui
;
1499 ui
= mtod(m
, struct udpiphdr
*);
1500 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) {
1506 struct mbuf
*nxt_mbuf
= NULL
;
1508 nxt_mbuf
= mbuf_nextpkt(tmp_mbuf
);
1510 * Not for us; forward if possible and desirable.
1512 mbuf_setnextpkt(tmp_mbuf
, NULL
);
1513 if (ipforwarding
== 0) {
1514 OSAddAtomic(1, &ipstat
.ips_cantforward
);
1518 ip_forward(tmp_mbuf
, 0, args
->fwai_next_hop
);
1520 ip_forward(tmp_mbuf
, 0, NULL
);
1523 tmp_mbuf
= nxt_mbuf
;
1525 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1529 * If offset or IP_MF are set, must reassemble.
1531 if (ip
->ip_off
& ~(IP_DF
| IP_RF
)) {
1532 VERIFY(npkts_in_chain
== 1);
1534 * ip_reass() will return a different mbuf, and update
1535 * the divert info in div_info and args->fwai_divert_rule.
1538 m
= ip_reass(m
, (u_int16_t
*)&div_info
, &args
->fwai_divert_rule
);
1544 ip
= mtod(m
, struct ip
*);
1545 /* Get the header length of the reassembled packet */
1546 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1548 /* Restore original checksum before diverting packet */
1549 if (div_info
!= 0) {
1550 VERIFY(npkts_in_chain
== 1);
1551 #if BYTE_ORDER != BIG_ENDIAN
1556 ip
->ip_sum
= ip_cksum_hdr_in(m
, hlen
);
1557 #if BYTE_ORDER != BIG_ENDIAN
1566 * Further protocols expect the packet length to be w/o the
1573 * Divert or tee packet to the divert protocol if required.
1575 * If div_info is zero then cookie should be too, so we shouldn't
1576 * need to clear them here. Assume divert_packet() does so also.
1578 if (div_info
!= 0) {
1579 struct mbuf
*clone
= NULL
;
1580 VERIFY(npkts_in_chain
== 1);
1582 /* Clone packet if we're doing a 'tee' */
1583 if (div_info
& IP_FW_PORT_TEE_FLAG
)
1584 clone
= m_dup(m
, M_DONTWAIT
);
1586 /* Restore packet header fields to original values */
1589 #if BYTE_ORDER != BIG_ENDIAN
1593 /* Deliver packet to divert input routine */
1594 OSAddAtomic(1, &ipstat
.ips_delivered
);
1595 divert_packet(m
, 1, div_info
& 0xffff, args
->fwai_divert_rule
);
1597 /* If 'tee', continue with original packet */
1598 if (clone
== NULL
) {
1602 ip
= mtod(m
, struct ip
*);
1608 * enforce IPsec policy checking if we are seeing last header.
1609 * note that we do not visit this with protocols with pcb layer
1610 * code - like udp/tcp/raw ip.
1612 if (ipsec_bypass
== 0 && (ip_protox
[ip
->ip_p
]->pr_flags
& PR_LASTHDR
)) {
1613 VERIFY(npkts_in_chain
== 1);
1614 if (ipsec4_in_reject(m
, NULL
)) {
1615 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
1622 * Switch out to protocol's input routine.
1624 OSAddAtomic(npkts_in_chain
, &ipstat
.ips_delivered
);
1627 if (args
->fwai_next_hop
&& ip
->ip_p
== IPPROTO_TCP
) {
1628 /* TCP needs IPFORWARD info if available */
1629 struct m_tag
*fwd_tag
;
1630 struct ip_fwd_tag
*ipfwd_tag
;
1632 VERIFY(npkts_in_chain
== 1);
1633 fwd_tag
= m_tag_create(KERNEL_MODULE_TAG_ID
,
1634 KERNEL_TAG_TYPE_IPFORWARD
, sizeof (*ipfwd_tag
),
1636 if (fwd_tag
== NULL
)
1639 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
1640 ipfwd_tag
->next_hop
= args
->fwai_next_hop
;
1642 m_tag_prepend(m
, fwd_tag
);
1644 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1645 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1647 /* TCP deals with its own locking */
1648 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1650 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1651 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1653 ip_input_dispatch_chain(m
);
1656 #else /* !IPFIREWALL */
1657 ip_input_dispatch_chain(m
);
1659 #endif /* !IPFIREWALL */
1660 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1663 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
1668 ip_input_process_list(struct mbuf
*packet_list
)
1670 pktchain_elm_t pktchain_tbl
[PKTTBL_SZ
];
1672 struct mbuf
*packet
= NULL
;
1673 struct mbuf
*modm
= NULL
; /* modified mbuf */
1675 u_int32_t div_info
= 0;
1677 #if (DEBUG || DEVELOPMENT)
1678 struct timeval start_tv
;
1679 #endif /* (DEBUG || DEVELOPMENT) */
1682 struct ip_fw_in_args args
;
1684 if (ip_chaining
== 0) {
1685 struct mbuf
*m
= packet_list
;
1686 #if (DEBUG || DEVELOPMENT)
1687 if (ip_input_measure
)
1688 net_perf_start_time(&net_perf
, &start_tv
);
1689 #endif /* (DEBUG || DEVELOPMENT) */
1692 packet_list
= mbuf_nextpkt(m
);
1693 mbuf_setnextpkt(m
, NULL
);
1698 #if (DEBUG || DEVELOPMENT)
1699 if (ip_input_measure
)
1700 net_perf_measure_time(&net_perf
, &start_tv
, num_pkts
);
1701 #endif /* (DEBUG || DEVELOPMENT) */
1704 #if (DEBUG || DEVELOPMENT)
1705 if (ip_input_measure
)
1706 net_perf_start_time(&net_perf
, &start_tv
);
1707 #endif /* (DEBUG || DEVELOPMENT) */
1709 bzero(&pktchain_tbl
, sizeof(pktchain_tbl
));
1710 restart_list_process
:
1712 for (packet
= packet_list
; packet
; packet
= packet_list
) {
1713 packet_list
= mbuf_nextpkt(packet
);
1714 mbuf_setnextpkt(packet
, NULL
);
1719 bzero(&args
, sizeof (args
));
1721 retval
= ip_input_first_pass(packet
, &div_info
, &args
,
1724 if (retval
== IPINPUT_DOCHAIN
) {
1727 packet
= ip_chain_insert(packet
, &pktchain_tbl
[0]);
1728 if (packet
== NULL
) {
1729 ipstat
.ips_rxc_chained
++;
1731 if (chain
> ip_chainsz
)
1734 ipstat
.ips_rxc_collisions
++;
1737 } else if (retval
== IPINPUT_DONTCHAIN
) {
1738 /* in order to preserve order, exit from chaining */
1741 ipstat
.ips_rxc_notchain
++;
1744 /* packet was freed or delivered, do nothing. */
1748 /* do second pass here for pktchain_tbl */
1750 ip_input_second_pass_loop_tbl(&pktchain_tbl
[0], &args
);
1754 * equivalent update in chaining case if performed in
1755 * ip_input_second_pass_loop_tbl().
1757 #if (DEBUG || DEVELOPMENT)
1758 if (ip_input_measure
)
1759 net_perf_histogram(&net_perf
, 1);
1760 #endif /* (DEBUG || DEVELOPMENT) */
1761 ip_input_second_pass(packet
, packet
->m_pkthdr
.rcvif
, div_info
,
1762 1, packet
->m_pkthdr
.len
, &args
, ours
);
1766 goto restart_list_process
;
1768 #if (DEBUG || DEVELOPMENT)
1769 if (ip_input_measure
)
1770 net_perf_measure_time(&net_perf
, &start_tv
, num_pkts
);
1771 #endif /* (DEBUG || DEVELOPMENT) */
1774 * Ip input routine. Checksum and byte swap header. If fragmented
1775 * try to reassemble. Process options. Pass to next level.
1778 ip_input(struct mbuf
*m
)
1781 struct in_ifaddr
*ia
= NULL
;
1782 unsigned int hlen
, checkif
;
1784 struct in_addr pkt_dst
;
1787 u_int32_t div_info
= 0; /* packet divert/tee info */
1789 #if IPFIREWALL || DUMMYNET
1790 struct ip_fw_args args
;
1793 ipfilter_t inject_filter_ref
= NULL
;
1794 struct ifnet
*inifp
;
1796 /* Check if the mbuf is still valid after interface filter processing */
1797 MBUF_INPUT_CHECK(m
, m
->m_pkthdr
.rcvif
);
1798 inifp
= m
->m_pkthdr
.rcvif
;
1799 VERIFY(inifp
!= NULL
);
1801 ipstat
.ips_rxc_notlist
++;
1803 /* Perform IP header alignment fixup, if needed */
1804 IP_HDR_ALIGNMENT_FIXUP(m
, inifp
, goto bad
);
1806 m
->m_pkthdr
.pkt_flags
&= ~PKTF_FORWARDED
;
1808 #if IPFIREWALL || DUMMYNET
1809 bzero(&args
, sizeof (struct ip_fw_args
));
1812 * Don't bother searching for tag(s) if there's none.
1814 if (SLIST_EMPTY(&m
->m_pkthdr
.tags
))
1815 goto ipfw_tags_done
;
1817 /* Grab info from mtags prepended to the chain */
1819 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
1820 KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
1821 struct dn_pkt_tag
*dn_tag
;
1823 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
1824 args
.fwa_ipfw_rule
= dn_tag
->dn_ipfw_rule
;
1825 args
.fwa_pf_rule
= dn_tag
->dn_pf_rule
;
1827 m_tag_delete(m
, tag
);
1829 #endif /* DUMMYNET */
1832 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
1833 KERNEL_TAG_TYPE_DIVERT
, NULL
)) != NULL
) {
1834 struct divert_tag
*div_tag
;
1836 div_tag
= (struct divert_tag
*)(tag
+1);
1837 args
.fwa_divert_rule
= div_tag
->cookie
;
1839 m_tag_delete(m
, tag
);
1843 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
1844 KERNEL_TAG_TYPE_IPFORWARD
, NULL
)) != NULL
) {
1845 struct ip_fwd_tag
*ipfwd_tag
;
1847 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
1848 args
.fwa_next_hop
= ipfwd_tag
->next_hop
;
1850 m_tag_delete(m
, tag
);
1854 if (m
== NULL
|| !(m
->m_flags
& M_PKTHDR
))
1855 panic("ip_input no HDR");
1859 if (args
.fwa_ipfw_rule
|| args
.fwa_pf_rule
) {
1860 /* dummynet already filtered us */
1861 ip
= mtod(m
, struct ip
*);
1862 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1863 inject_filter_ref
= ipf_get_inject_filter(m
);
1865 if (args
.fwa_ipfw_rule
)
1867 #endif /* IPFIREWALL */
1868 if (args
.fwa_pf_rule
)
1871 #endif /* DUMMYNET */
1873 #endif /* IPFIREWALL || DUMMYNET */
1876 * No need to process packet twice if we've already seen it.
1878 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
))
1879 inject_filter_ref
= ipf_get_inject_filter(m
);
1880 if (inject_filter_ref
!= NULL
) {
1881 ip
= mtod(m
, struct ip
*);
1882 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1884 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1885 struct ip
*, ip
, struct ifnet
*, inifp
,
1886 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
1888 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
1889 ip
->ip_off
= ntohs(ip
->ip_off
);
1890 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, inject_filter_ref
);
1894 OSAddAtomic(1, &ipstat
.ips_total
);
1895 if (m
->m_pkthdr
.len
< sizeof (struct ip
))
1898 if (m
->m_len
< sizeof (struct ip
) &&
1899 (m
= m_pullup(m
, sizeof (struct ip
))) == NULL
) {
1900 OSAddAtomic(1, &ipstat
.ips_toosmall
);
1903 ip
= mtod(m
, struct ip
*);
1905 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
, ip
->ip_src
.s_addr
,
1906 ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1908 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
1909 OSAddAtomic(1, &ipstat
.ips_badvers
);
1913 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1914 if (hlen
< sizeof (struct ip
)) { /* minimum header length */
1915 OSAddAtomic(1, &ipstat
.ips_badhlen
);
1918 if (hlen
> m
->m_len
) {
1919 if ((m
= m_pullup(m
, hlen
)) == NULL
) {
1920 OSAddAtomic(1, &ipstat
.ips_badhlen
);
1923 ip
= mtod(m
, struct ip
*);
1926 /* 127/8 must not appear on wire - RFC1122 */
1927 if ((ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
1928 (ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
) {
1930 * Allow for the following exceptions:
1932 * 1. If the packet was sent to loopback (i.e. rcvif
1933 * would have been set earlier at output time.)
1935 * 2. If the packet was sent out on loopback from a local
1936 * source address which belongs to a non-loopback
1937 * interface (i.e. rcvif may not necessarily be a
1938 * loopback interface, hence the test for PKTF_LOOP.)
1939 * Unlike IPv6, there is no interface scope ID, and
1940 * therefore we don't care so much about PKTF_IFINFO.
1942 if (!(inifp
->if_flags
& IFF_LOOPBACK
) &&
1943 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
1944 OSAddAtomic(1, &ipstat
.ips_badaddr
);
1949 /* IPv4 Link-Local Addresses as defined in RFC3927 */
1950 if ((IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
)) ||
1951 IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)))) {
1952 ip_linklocal_stat
.iplls_in_total
++;
1953 if (ip
->ip_ttl
!= MAXTTL
) {
1954 OSAddAtomic(1, &ip_linklocal_stat
.iplls_in_badttl
);
1955 /* Silently drop link local traffic with bad TTL */
1956 if (!ip_linklocal_in_allowbadttl
)
1961 sum
= ip_cksum(m
, hlen
);
1966 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1967 struct ip
*, ip
, struct ifnet
*, inifp
,
1968 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
1971 * Naively assume we can attribute inbound data to the route we would
1972 * use to send to this destination. Asymmetric routing breaks this
1973 * assumption, but it still allows us to account for traffic from
1974 * a remote node in the routing table.
1975 * this has a very significant performance impact so we bypass
1976 * if nstat_collect is disabled. We may also bypass if the
1977 * protocol is tcp in the future because tcp will have a route that
1978 * we can use to attribute the data to. That does mean we would not
1979 * account for forwarded tcp traffic.
1981 if (nstat_collect
) {
1982 struct rtentry
*rt
=
1983 ifnet_cached_rtlookup_inet(inifp
, ip
->ip_src
);
1985 nstat_route_rx(rt
, 1, m
->m_pkthdr
.len
, 0);
1991 * Convert fields to host representation.
1993 #if BYTE_ORDER != BIG_ENDIAN
1997 if (ip
->ip_len
< hlen
) {
1998 OSAddAtomic(1, &ipstat
.ips_badlen
);
2002 #if BYTE_ORDER != BIG_ENDIAN
2006 * Check that the amount of data in the buffers
2007 * is as at least much as the IP header would have us expect.
2008 * Trim mbufs if longer than we expect.
2009 * Drop packet if shorter than we expect.
2011 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
2013 OSAddAtomic(1, &ipstat
.ips_tooshort
);
2016 if (m
->m_pkthdr
.len
> ip
->ip_len
) {
2018 * Invalidate hardware checksum info if ip_adj_clear_hwcksum
2019 * is set; useful to handle buggy drivers. Note that this
2020 * should not be enabled by default, as we may get here due
2021 * to link-layer padding.
2023 if (ip_adj_clear_hwcksum
&&
2024 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
) &&
2025 !(inifp
->if_flags
& IFF_LOOPBACK
) &&
2026 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
2027 m
->m_pkthdr
.csum_flags
&= ~CSUM_DATA_VALID
;
2028 m
->m_pkthdr
.csum_data
= 0;
2029 ipstat
.ips_adj_hwcsum_clr
++;
2033 if (m
->m_len
== m
->m_pkthdr
.len
) {
2034 m
->m_len
= ip
->ip_len
;
2035 m
->m_pkthdr
.len
= ip
->ip_len
;
2037 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
2040 /* for consistency */
2041 m
->m_pkthdr
.pkt_proto
= ip
->ip_p
;
2047 /* Invoke inbound packet filter */
2048 if (PF_IS_ENABLED
) {
2051 error
= pf_af_hook(inifp
, NULL
, &m
, AF_INET
, TRUE
, &args
);
2053 error
= pf_af_hook(inifp
, NULL
, &m
, AF_INET
, TRUE
, NULL
);
2054 #endif /* DUMMYNET */
2055 if (error
!= 0 || m
== NULL
) {
2057 panic("%s: unexpected packet %p\n",
2061 /* Already freed by callee */
2064 ip
= mtod(m
, struct ip
*);
2065 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
2070 if (ipsec_bypass
== 0 && ipsec_gethist(m
, NULL
))
2077 #endif /* DUMMYNET */
2079 * Check if we want to allow this packet to be processed.
2080 * Consider it to be bad if not.
2082 if (fw_enable
&& IPFW_LOADED
) {
2083 #if IPFIREWALL_FORWARD
2085 * If we've been forwarded from the output side, then
2086 * skip the firewall a second time
2088 if (args
.fwa_next_hop
)
2090 #endif /* IPFIREWALL_FORWARD */
2094 i
= ip_fw_chk_ptr(&args
);
2097 if ((i
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) { /* drop */
2102 ip
= mtod(m
, struct ip
*); /* just in case m changed */
2104 if (i
== 0 && args
.fwa_next_hop
== NULL
) { /* common case */
2108 if (DUMMYNET_LOADED
&& (i
& IP_FW_PORT_DYNT_FLAG
) != 0) {
2109 /* Send packet to the appropriate pipe */
2110 ip_dn_io_ptr(m
, i
&0xffff, DN_TO_IP_IN
, &args
,
2114 #endif /* DUMMYNET */
2116 if (i
!= 0 && (i
& IP_FW_PORT_DYNT_FLAG
) == 0) {
2117 /* Divert or tee packet */
2122 #if IPFIREWALL_FORWARD
2123 if (i
== 0 && args
.fwa_next_hop
!= NULL
) {
2128 * if we get here, the packet must be dropped
2133 #endif /* IPFIREWALL */
2134 #if IPSEC | IPFIREWALL
2138 * Process options and, if not destined for us,
2139 * ship it on. ip_dooptions returns 1 when an
2140 * error was detected (causing an icmp message
2141 * to be sent and the original packet to be freed).
2143 ip_nhops
= 0; /* for source routed packets */
2145 if (hlen
> sizeof (struct ip
) &&
2146 ip_dooptions(m
, 0, args
.fwa_next_hop
)) {
2147 #else /* !IPFIREWALL */
2148 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, NULL
)) {
2149 #endif /* !IPFIREWALL */
2154 * Check our list of addresses, to see if the packet is for us.
2155 * If we don't have any addresses, assume any unicast packet
2156 * we receive might be for us (and let the upper layers deal
2159 if (TAILQ_EMPTY(&in_ifaddrhead
) && !(m
->m_flags
& (M_MCAST
|M_BCAST
))) {
2160 ip_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
2165 * Cache the destination address of the packet; this may be
2166 * changed by use of 'ipfw fwd'.
2169 pkt_dst
= args
.fwa_next_hop
== NULL
?
2170 ip
->ip_dst
: args
.fwa_next_hop
->sin_addr
;
2171 #else /* !IPFIREWALL */
2172 pkt_dst
= ip
->ip_dst
;
2173 #endif /* !IPFIREWALL */
2176 * Enable a consistency check between the destination address
2177 * and the arrival interface for a unicast packet (the RFC 1122
2178 * strong ES model) if IP forwarding is disabled and the packet
2179 * is not locally generated and the packet is not subject to
2182 * XXX - Checking also should be disabled if the destination
2183 * address is ipnat'ed to a different interface.
2185 * XXX - Checking is incompatible with IP aliases added
2186 * to the loopback interface instead of the interface where
2187 * the packets are received.
2189 checkif
= ip_checkinterface
&& (ipforwarding
== 0) &&
2190 !(inifp
->if_flags
& IFF_LOOPBACK
) &&
2191 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)
2193 && (args
.fwa_next_hop
== NULL
);
2194 #else /* !IPFIREWALL */
2196 #endif /* !IPFIREWALL */
2199 * Check for exact addresses in the hash bucket.
2201 lck_rw_lock_shared(in_ifaddr_rwlock
);
2202 TAILQ_FOREACH(ia
, INADDR_HASH(pkt_dst
.s_addr
), ia_hash
) {
2204 * If the address matches, verify that the packet
2205 * arrived via the correct interface if checking is
2208 if (IA_SIN(ia
)->sin_addr
.s_addr
== pkt_dst
.s_addr
&&
2209 (!checkif
|| ia
->ia_ifp
== inifp
)) {
2210 ip_setdstifaddr_info(m
, 0, ia
);
2211 lck_rw_done(in_ifaddr_rwlock
);
2215 lck_rw_done(in_ifaddr_rwlock
);
2218 * Check for broadcast addresses.
2220 * Only accept broadcast packets that arrive via the matching
2221 * interface. Reception of forwarded directed broadcasts would be
2222 * handled via ip_forward() and ether_frameout() with the loopback
2223 * into the stack for SIMPLEX interfaces handled by ether_frameout().
2225 if (inifp
->if_flags
& IFF_BROADCAST
) {
2228 ifnet_lock_shared(inifp
);
2229 TAILQ_FOREACH(ifa
, &inifp
->if_addrhead
, ifa_link
) {
2230 if (ifa
->ifa_addr
->sa_family
!= AF_INET
) {
2234 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
2235 pkt_dst
.s_addr
|| ia
->ia_netbroadcast
.s_addr
==
2237 ip_setdstifaddr_info(m
, 0, ia
);
2238 ifnet_lock_done(inifp
);
2242 ifnet_lock_done(inifp
);
2245 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
2246 struct in_multi
*inm
;
2248 * See if we belong to the destination multicast group on the
2249 * arrival interface.
2251 in_multihead_lock_shared();
2252 IN_LOOKUP_MULTI(&ip
->ip_dst
, inifp
, inm
);
2253 in_multihead_lock_done();
2255 OSAddAtomic(1, &ipstat
.ips_notmember
);
2259 ip_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
2263 if (ip
->ip_dst
.s_addr
== (u_int32_t
)INADDR_BROADCAST
||
2264 ip
->ip_dst
.s_addr
== INADDR_ANY
) {
2265 ip_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
2269 /* Allow DHCP/BootP responses through */
2270 if ((inifp
->if_eflags
& IFEF_AUTOCONFIGURING
) &&
2271 hlen
== sizeof (struct ip
) && ip
->ip_p
== IPPROTO_UDP
) {
2272 struct udpiphdr
*ui
;
2274 if (m
->m_len
< sizeof (struct udpiphdr
) &&
2275 (m
= m_pullup(m
, sizeof (struct udpiphdr
))) == NULL
) {
2276 OSAddAtomic(1, &udpstat
.udps_hdrops
);
2279 ui
= mtod(m
, struct udpiphdr
*);
2280 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) {
2281 ip_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
2284 ip
= mtod(m
, struct ip
*); /* in case it changed */
2288 * Not for us; forward if possible and desirable.
2290 if (ipforwarding
== 0) {
2291 OSAddAtomic(1, &ipstat
.ips_cantforward
);
2295 ip_forward(m
, 0, args
.fwa_next_hop
);
2297 ip_forward(m
, 0, NULL
);
2304 * If offset or IP_MF are set, must reassemble.
2306 if (ip
->ip_off
& ~(IP_DF
| IP_RF
)) {
2308 * ip_reass() will return a different mbuf, and update
2309 * the divert info in div_info and args.fwa_divert_rule.
2312 m
= ip_reass(m
, (u_int16_t
*)&div_info
, &args
.fwa_divert_rule
);
2318 ip
= mtod(m
, struct ip
*);
2319 /* Get the header length of the reassembled packet */
2320 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
2322 /* Restore original checksum before diverting packet */
2323 if (div_info
!= 0) {
2324 #if BYTE_ORDER != BIG_ENDIAN
2329 ip
->ip_sum
= ip_cksum_hdr_in(m
, hlen
);
2330 #if BYTE_ORDER != BIG_ENDIAN
2339 * Further protocols expect the packet length to be w/o the
2346 * Divert or tee packet to the divert protocol if required.
2348 * If div_info is zero then cookie should be too, so we shouldn't
2349 * need to clear them here. Assume divert_packet() does so also.
2351 if (div_info
!= 0) {
2352 struct mbuf
*clone
= NULL
;
2354 /* Clone packet if we're doing a 'tee' */
2355 if (div_info
& IP_FW_PORT_TEE_FLAG
)
2356 clone
= m_dup(m
, M_DONTWAIT
);
2358 /* Restore packet header fields to original values */
2361 #if BYTE_ORDER != BIG_ENDIAN
2365 /* Deliver packet to divert input routine */
2366 OSAddAtomic(1, &ipstat
.ips_delivered
);
2367 divert_packet(m
, 1, div_info
& 0xffff, args
.fwa_divert_rule
);
2369 /* If 'tee', continue with original packet */
2370 if (clone
== NULL
) {
2374 ip
= mtod(m
, struct ip
*);
2380 * enforce IPsec policy checking if we are seeing last header.
2381 * note that we do not visit this with protocols with pcb layer
2382 * code - like udp/tcp/raw ip.
2384 if (ipsec_bypass
== 0 && (ip_protox
[ip
->ip_p
]->pr_flags
& PR_LASTHDR
)) {
2385 if (ipsec4_in_reject(m
, NULL
)) {
2386 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
2393 * Switch out to protocol's input routine.
2395 OSAddAtomic(1, &ipstat
.ips_delivered
);
2398 if (args
.fwa_next_hop
&& ip
->ip_p
== IPPROTO_TCP
) {
2399 /* TCP needs IPFORWARD info if available */
2400 struct m_tag
*fwd_tag
;
2401 struct ip_fwd_tag
*ipfwd_tag
;
2403 fwd_tag
= m_tag_create(KERNEL_MODULE_TAG_ID
,
2404 KERNEL_TAG_TYPE_IPFORWARD
, sizeof (*ipfwd_tag
),
2406 if (fwd_tag
== NULL
)
2409 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
2410 ipfwd_tag
->next_hop
= args
.fwa_next_hop
;
2412 m_tag_prepend(m
, fwd_tag
);
2414 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
2415 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
2417 /* TCP deals with its own locking */
2418 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
2420 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
2421 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
2423 if ((sw_lro
) && (ip
->ip_p
== IPPROTO_TCP
)) {
2424 m
= tcp_lro(m
, hlen
);
2429 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
2431 #else /* !IPFIREWALL */
2432 if ((sw_lro
) && (ip
->ip_p
== IPPROTO_TCP
)) {
2433 m
= tcp_lro(m
, hlen
);
2437 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
2438 #endif /* !IPFIREWALL */
2442 KERNEL_DEBUG(DBG_LAYER_END
, 0, 0, 0, 0, 0);
2447 ipq_updateparams(void)
2449 lck_mtx_assert(&ipqlock
, LCK_MTX_ASSERT_OWNED
);
2451 * -1 for unlimited allocation.
2456 * Positive number for specific bound.
2459 ipq_limit
= maxnipq
;
2461 * Zero specifies no further fragment queue allocation -- set the
2462 * bound very low, but rely on implementation elsewhere to actually
2463 * prevent allocation and reclaim current queues.
2468 * Arm the purge timer if not already and if there's work to do
2470 frag_sched_timeout();
2474 sysctl_maxnipq SYSCTL_HANDLER_ARGS
2476 #pragma unused(arg1, arg2)
2479 lck_mtx_lock(&ipqlock
);
2481 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
2482 if (error
|| req
->newptr
== USER_ADDR_NULL
)
2485 if (i
< -1 || i
> (nmbclusters
/ 4)) {
2492 lck_mtx_unlock(&ipqlock
);
2497 sysctl_maxfragsperpacket SYSCTL_HANDLER_ARGS
2499 #pragma unused(arg1, arg2)
2502 lck_mtx_lock(&ipqlock
);
2503 i
= maxfragsperpacket
;
2504 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
2505 if (error
|| req
->newptr
== USER_ADDR_NULL
)
2507 maxfragsperpacket
= i
;
2508 ipq_updateparams(); /* see if we need to arm timer */
2510 lck_mtx_unlock(&ipqlock
);
2515 * Take incoming datagram fragment and try to reassemble it into
2516 * whole datagram. If a chain for reassembly of this datagram already
2517 * exists, then it is given as fp; otherwise have to make a chain.
2519 * When IPDIVERT enabled, keep additional state with each packet that
2520 * tells us if we need to divert or tee the packet we're building.
2522 * The IP header is *NOT* adjusted out of iplen.
2524 static struct mbuf
*
2526 ip_reass(struct mbuf
*m
,
2529 #else /* IPDIVERT_44 */
2531 #endif /* IPDIVERT_44 */
2532 u_int16_t
*divcookie
)
2533 #else /* IPDIVERT */
2534 ip_reass(struct mbuf
*m
)
2535 #endif /* IPDIVERT */
2538 struct mbuf
*p
, *q
, *nq
, *t
;
2539 struct ipq
*fp
= NULL
;
2540 struct ipqhead
*head
;
2543 uint32_t csum
, csum_flags
;
2547 MBUFQ_INIT(&dfq
); /* for deferred frees */
2549 /* If maxnipq or maxfragsperpacket is 0, never accept fragments. */
2550 if (maxnipq
== 0 || maxfragsperpacket
== 0) {
2551 ipstat
.ips_fragments
++;
2552 ipstat
.ips_fragdropped
++;
2555 lck_mtx_lock(&ipqlock
);
2556 frag_sched_timeout(); /* purge stale fragments */
2557 lck_mtx_unlock(&ipqlock
);
2562 ip
= mtod(m
, struct ip
*);
2563 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
2565 lck_mtx_lock(&ipqlock
);
2567 hash
= IPREASS_HASH(ip
->ip_src
.s_addr
, ip
->ip_id
);
2571 * Look for queue of fragments
2574 TAILQ_FOREACH(fp
, head
, ipq_list
) {
2575 if (ip
->ip_id
== fp
->ipq_id
&&
2576 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
2577 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
2579 mac_ipq_label_compare(m
, fp
) &&
2581 ip
->ip_p
== fp
->ipq_p
)
2588 * Attempt to trim the number of allocated fragment queues if it
2589 * exceeds the administrative limit.
2591 if ((nipq
> (unsigned)maxnipq
) && (maxnipq
> 0)) {
2593 * drop something from the tail of the current queue
2594 * before proceeding further
2596 struct ipq
*fq
= TAILQ_LAST(head
, ipqhead
);
2597 if (fq
== NULL
) { /* gak */
2598 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
2599 struct ipq
*r
= TAILQ_LAST(&ipq
[i
], ipqhead
);
2601 ipstat
.ips_fragtimeout
+= r
->ipq_nfrags
;
2602 frag_freef(&ipq
[i
], r
);
2607 ipstat
.ips_fragtimeout
+= fq
->ipq_nfrags
;
2608 frag_freef(head
, fq
);
2614 * Leverage partial checksum offload for IP fragments. Narrow down
2615 * the scope to cover only UDP without IP options, as that is the
2618 * Perform 1's complement adjustment of octets that got included/
2619 * excluded in the hardware-calculated checksum value. Ignore cases
2620 * where the value includes or excludes the IP header span, as the
2621 * sum for those octets would already be 0xffff and thus no-op.
2623 if (ip
->ip_p
== IPPROTO_UDP
&& hlen
== sizeof (struct ip
) &&
2624 (m
->m_pkthdr
.csum_flags
&
2625 (CSUM_DATA_VALID
| CSUM_PARTIAL
| CSUM_PSEUDO_HDR
)) ==
2626 (CSUM_DATA_VALID
| CSUM_PARTIAL
)) {
2629 start
= m
->m_pkthdr
.csum_rx_start
;
2630 csum
= m
->m_pkthdr
.csum_rx_val
;
2632 if (start
!= 0 && start
!= hlen
) {
2633 #if BYTE_ORDER != BIG_ENDIAN
2639 /* callee folds in sum */
2640 csum
= m_adj_sum16(m
, start
, hlen
, csum
);
2641 #if BYTE_ORDER != BIG_ENDIAN
2648 csum_flags
= m
->m_pkthdr
.csum_flags
;
2654 /* Invalidate checksum */
2655 m
->m_pkthdr
.csum_flags
&= ~CSUM_DATA_VALID
;
2657 ipstat
.ips_fragments
++;
2660 * Adjust ip_len to not reflect header,
2661 * convert offset of this to bytes.
2664 if (ip
->ip_off
& IP_MF
) {
2666 * Make sure that fragments have a data length
2667 * that's a non-zero multiple of 8 bytes.
2669 if (ip
->ip_len
== 0 || (ip
->ip_len
& 0x7) != 0) {
2670 OSAddAtomic(1, &ipstat
.ips_toosmall
);
2672 * Reassembly queue may have been found if previous
2673 * fragments were valid; given that this one is bad,
2674 * we need to drop it. Make sure to set fp to NULL
2675 * if not already, since we don't want to decrement
2676 * ipq_nfrags as it doesn't include this packet.
2681 m
->m_flags
|= M_FRAG
;
2683 /* Clear the flag in case packet comes from loopback */
2684 m
->m_flags
&= ~M_FRAG
;
2688 m
->m_pkthdr
.pkt_hdr
= ip
;
2690 /* Previous ip_reass() started here. */
2692 * Presence of header sizes in mbufs
2693 * would confuse code below.
2699 * If first fragment to arrive, create a reassembly queue.
2702 fp
= ipq_alloc(M_DONTWAIT
);
2706 if (mac_ipq_label_init(fp
, M_NOWAIT
) != 0) {
2711 mac_ipq_label_associate(m
, fp
);
2713 TAILQ_INSERT_HEAD(head
, fp
, ipq_list
);
2716 fp
->ipq_ttl
= IPFRAGTTL
;
2717 fp
->ipq_p
= ip
->ip_p
;
2718 fp
->ipq_id
= ip
->ip_id
;
2719 fp
->ipq_src
= ip
->ip_src
;
2720 fp
->ipq_dst
= ip
->ip_dst
;
2722 m
->m_nextpkt
= NULL
;
2724 * If the first fragment has valid checksum offload
2725 * info, the rest of fragments are eligible as well.
2727 if (csum_flags
!= 0) {
2728 fp
->ipq_csum
= csum
;
2729 fp
->ipq_csum_flags
= csum_flags
;
2733 * Transfer firewall instructions to the fragment structure.
2734 * Only trust info in the fragment at offset 0.
2736 if (ip
->ip_off
== 0) {
2738 fp
->ipq_div_info
= *divinfo
;
2740 fp
->ipq_divert
= *divinfo
;
2742 fp
->ipq_div_cookie
= *divcookie
;
2746 #endif /* IPDIVERT */
2747 m
= NULL
; /* nothing to return */
2752 mac_ipq_label_update(m
, fp
);
2756 #define GETIP(m) ((struct ip *)((m)->m_pkthdr.pkt_hdr))
2759 * Handle ECN by comparing this segment with the first one;
2760 * if CE is set, do not lose CE.
2761 * drop if CE and not-ECT are mixed for the same packet.
2763 ecn
= ip
->ip_tos
& IPTOS_ECN_MASK
;
2764 ecn0
= GETIP(fp
->ipq_frags
)->ip_tos
& IPTOS_ECN_MASK
;
2765 if (ecn
== IPTOS_ECN_CE
) {
2766 if (ecn0
== IPTOS_ECN_NOTECT
)
2768 if (ecn0
!= IPTOS_ECN_CE
)
2769 GETIP(fp
->ipq_frags
)->ip_tos
|= IPTOS_ECN_CE
;
2771 if (ecn
== IPTOS_ECN_NOTECT
&& ecn0
!= IPTOS_ECN_NOTECT
)
2775 * Find a segment which begins after this one does.
2777 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
)
2778 if (GETIP(q
)->ip_off
> ip
->ip_off
)
2782 * If there is a preceding segment, it may provide some of
2783 * our data already. If so, drop the data from the incoming
2784 * segment. If it provides all of our data, drop us, otherwise
2785 * stick new segment in the proper place.
2787 * If some of the data is dropped from the preceding
2788 * segment, then it's checksum is invalidated.
2791 i
= GETIP(p
)->ip_off
+ GETIP(p
)->ip_len
- ip
->ip_off
;
2793 if (i
>= ip
->ip_len
)
2796 fp
->ipq_csum_flags
= 0;
2800 m
->m_nextpkt
= p
->m_nextpkt
;
2803 m
->m_nextpkt
= fp
->ipq_frags
;
2808 * While we overlap succeeding segments trim them or,
2809 * if they are completely covered, dequeue them.
2811 for (; q
!= NULL
&& ip
->ip_off
+ ip
->ip_len
> GETIP(q
)->ip_off
;
2813 i
= (ip
->ip_off
+ ip
->ip_len
) - GETIP(q
)->ip_off
;
2814 if (i
< GETIP(q
)->ip_len
) {
2815 GETIP(q
)->ip_len
-= i
;
2816 GETIP(q
)->ip_off
+= i
;
2818 fp
->ipq_csum_flags
= 0;
2823 ipstat
.ips_fragdropped
++;
2825 /* defer freeing until after lock is dropped */
2826 MBUFQ_ENQUEUE(&dfq
, q
);
2830 * If this fragment contains similar checksum offload info
2831 * as that of the existing ones, accumulate checksum. Otherwise,
2832 * invalidate checksum offload info for the entire datagram.
2834 if (csum_flags
!= 0 && csum_flags
== fp
->ipq_csum_flags
)
2835 fp
->ipq_csum
+= csum
;
2836 else if (fp
->ipq_csum_flags
!= 0)
2837 fp
->ipq_csum_flags
= 0;
2841 * Transfer firewall instructions to the fragment structure.
2842 * Only trust info in the fragment at offset 0.
2844 if (ip
->ip_off
== 0) {
2846 fp
->ipq_div_info
= *divinfo
;
2848 fp
->ipq_divert
= *divinfo
;
2850 fp
->ipq_div_cookie
= *divcookie
;
2854 #endif /* IPDIVERT */
2857 * Check for complete reassembly and perform frag per packet
2860 * Frag limiting is performed here so that the nth frag has
2861 * a chance to complete the packet before we drop the packet.
2862 * As a result, n+1 frags are actually allowed per packet, but
2863 * only n will ever be stored. (n = maxfragsperpacket.)
2867 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
) {
2868 if (GETIP(q
)->ip_off
!= next
) {
2869 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
2870 ipstat
.ips_fragdropped
+= fp
->ipq_nfrags
;
2871 frag_freef(head
, fp
);
2873 m
= NULL
; /* nothing to return */
2876 next
+= GETIP(q
)->ip_len
;
2878 /* Make sure the last packet didn't have the IP_MF flag */
2879 if (p
->m_flags
& M_FRAG
) {
2880 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
2881 ipstat
.ips_fragdropped
+= fp
->ipq_nfrags
;
2882 frag_freef(head
, fp
);
2884 m
= NULL
; /* nothing to return */
2889 * Reassembly is complete. Make sure the packet is a sane size.
2893 if (next
+ (IP_VHL_HL(ip
->ip_vhl
) << 2) > IP_MAXPACKET
) {
2894 ipstat
.ips_toolong
++;
2895 ipstat
.ips_fragdropped
+= fp
->ipq_nfrags
;
2896 frag_freef(head
, fp
);
2897 m
= NULL
; /* nothing to return */
2902 * Concatenate fragments.
2909 q
->m_nextpkt
= NULL
;
2910 for (q
= nq
; q
!= NULL
; q
= nq
) {
2912 q
->m_nextpkt
= NULL
;
2917 * Store partial hardware checksum info from the fragment queue;
2918 * the receive start offset is set to 20 bytes (see code at the
2919 * top of this routine.)
2921 if (fp
->ipq_csum_flags
!= 0) {
2922 csum
= fp
->ipq_csum
;
2926 m
->m_pkthdr
.csum_rx_val
= csum
;
2927 m
->m_pkthdr
.csum_rx_start
= sizeof (struct ip
);
2928 m
->m_pkthdr
.csum_flags
= fp
->ipq_csum_flags
;
2929 } else if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) ||
2930 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
2931 /* loopback checksums are always OK */
2932 m
->m_pkthdr
.csum_data
= 0xffff;
2933 m
->m_pkthdr
.csum_flags
&= ~CSUM_PARTIAL
;
2934 m
->m_pkthdr
.csum_flags
=
2935 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
2936 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
2941 * Extract firewall instructions from the fragment structure.
2944 *divinfo
= fp
->ipq_div_info
;
2946 *divinfo
= fp
->ipq_divert
;
2948 *divcookie
= fp
->ipq_div_cookie
;
2949 #endif /* IPDIVERT */
2952 mac_mbuf_label_associate_ipq(fp
, m
);
2953 mac_ipq_label_destroy(fp
);
2956 * Create header for new ip packet by modifying header of first
2957 * packet; dequeue and discard fragment reassembly header.
2958 * Make header visible.
2960 ip
->ip_len
= (IP_VHL_HL(ip
->ip_vhl
) << 2) + next
;
2961 ip
->ip_src
= fp
->ipq_src
;
2962 ip
->ip_dst
= fp
->ipq_dst
;
2964 fp
->ipq_frags
= NULL
; /* return to caller as 'm' */
2965 frag_freef(head
, fp
);
2968 m
->m_len
+= (IP_VHL_HL(ip
->ip_vhl
) << 2);
2969 m
->m_data
-= (IP_VHL_HL(ip
->ip_vhl
) << 2);
2970 /* some debugging cruft by sklower, below, will go away soon */
2971 if (m
->m_flags
& M_PKTHDR
) /* XXX this should be done elsewhere */
2973 ipstat
.ips_reassembled
++;
2975 /* arm the purge timer if not already and if there's work to do */
2976 frag_sched_timeout();
2977 lck_mtx_unlock(&ipqlock
);
2978 /* perform deferred free (if needed) now that lock is dropped */
2979 if (!MBUFQ_EMPTY(&dfq
))
2981 VERIFY(MBUFQ_EMPTY(&dfq
));
2986 /* arm the purge timer if not already and if there's work to do */
2987 frag_sched_timeout();
2988 lck_mtx_unlock(&ipqlock
);
2989 /* perform deferred free (if needed) */
2990 if (!MBUFQ_EMPTY(&dfq
))
2992 VERIFY(MBUFQ_EMPTY(&dfq
));
2999 #endif /* IPDIVERT */
3000 ipstat
.ips_fragdropped
++;
3003 /* arm the purge timer if not already and if there's work to do */
3004 frag_sched_timeout();
3005 lck_mtx_unlock(&ipqlock
);
3007 /* perform deferred free (if needed) */
3008 if (!MBUFQ_EMPTY(&dfq
))
3010 VERIFY(MBUFQ_EMPTY(&dfq
));
3016 * Free a fragment reassembly header and all
3017 * associated datagrams.
3020 frag_freef(struct ipqhead
*fhp
, struct ipq
*fp
)
3022 lck_mtx_assert(&ipqlock
, LCK_MTX_ASSERT_OWNED
);
3025 if (fp
->ipq_frags
!= NULL
) {
3026 m_freem_list(fp
->ipq_frags
);
3027 fp
->ipq_frags
= NULL
;
3029 TAILQ_REMOVE(fhp
, fp
, ipq_list
);
3035 * IP reassembly timer processing
3038 frag_timeout(void *arg
)
3045 * Update coarse-grained networking timestamp (in sec.); the idea
3046 * is to piggy-back on the timeout callout to update the counter
3047 * returnable via net_uptime().
3049 net_update_uptime();
3051 lck_mtx_lock(&ipqlock
);
3052 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
3053 for (fp
= TAILQ_FIRST(&ipq
[i
]); fp
; ) {
3057 fp
= TAILQ_NEXT(fp
, ipq_list
);
3058 if (--fpp
->ipq_ttl
== 0) {
3059 ipstat
.ips_fragtimeout
+= fpp
->ipq_nfrags
;
3060 frag_freef(&ipq
[i
], fpp
);
3065 * If we are over the maximum number of fragments
3066 * (due to the limit being lowered), drain off
3067 * enough to get down to the new limit.
3069 if (maxnipq
>= 0 && nipq
> (unsigned)maxnipq
) {
3070 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
3071 while (nipq
> (unsigned)maxnipq
&&
3072 !TAILQ_EMPTY(&ipq
[i
])) {
3073 ipstat
.ips_fragdropped
+=
3074 TAILQ_FIRST(&ipq
[i
])->ipq_nfrags
;
3075 frag_freef(&ipq
[i
], TAILQ_FIRST(&ipq
[i
]));
3079 /* re-arm the purge timer if there's work to do */
3080 frag_timeout_run
= 0;
3081 frag_sched_timeout();
3082 lck_mtx_unlock(&ipqlock
);
3086 frag_sched_timeout(void)
3088 lck_mtx_assert(&ipqlock
, LCK_MTX_ASSERT_OWNED
);
3090 if (!frag_timeout_run
&& nipq
> 0) {
3091 frag_timeout_run
= 1;
3092 timeout(frag_timeout
, NULL
, hz
);
3097 * Drain off all datagram fragments.
3104 lck_mtx_lock(&ipqlock
);
3105 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
3106 while (!TAILQ_EMPTY(&ipq
[i
])) {
3107 ipstat
.ips_fragdropped
+=
3108 TAILQ_FIRST(&ipq
[i
])->ipq_nfrags
;
3109 frag_freef(&ipq
[i
], TAILQ_FIRST(&ipq
[i
]));
3112 lck_mtx_unlock(&ipqlock
);
3122 * See comments in ipq_updateparams(). Keep the count separate
3123 * from nipq since the latter represents the elements already
3124 * in the reassembly queues.
3126 if (ipq_limit
> 0 && ipq_count
> ipq_limit
)
3129 t
= m_get(how
, MT_FTABLE
);
3131 atomic_add_32(&ipq_count
, 1);
3132 fp
= mtod(t
, struct ipq
*);
3133 bzero(fp
, sizeof (*fp
));
3141 ipq_free(struct ipq
*fp
)
3143 (void) m_free(dtom(fp
));
3144 atomic_add_32(&ipq_count
, -1);
3153 frag_drain(); /* fragments */
3154 in_rtqdrain(); /* protocol cloned routes */
3155 in_arpdrain(NULL
); /* cloned routes: ARP */
3159 * Do option processing on a datagram,
3160 * possibly discarding it if bad options are encountered,
3161 * or forwarding it if source-routed.
3162 * The pass argument is used when operating in the IPSTEALTH
3163 * mode to tell what options to process:
3164 * [LS]SRR (pass 0) or the others (pass 1).
3165 * The reason for as many as two passes is that when doing IPSTEALTH,
3166 * non-routing options should be processed only if the packet is for us.
3167 * Returns 1 if packet has been forwarded/freed,
3168 * 0 if the packet should be processed further.
3171 ip_dooptions(struct mbuf
*m
, int pass
, struct sockaddr_in
*next_hop
)
3173 #pragma unused(pass)
3174 struct ip
*ip
= mtod(m
, struct ip
*);
3176 struct ip_timestamp
*ipt
;
3177 struct in_ifaddr
*ia
;
3178 int opt
, optlen
, cnt
, off
, code
, type
= ICMP_PARAMPROB
, forward
= 0;
3179 struct in_addr
*sin
, dst
;
3181 struct sockaddr_in ipaddr
= {
3182 sizeof (ipaddr
), AF_INET
, 0, { 0 }, { 0, } };
3184 /* Expect 32-bit aligned data pointer on strict-align platforms */
3185 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
3188 cp
= (u_char
*)(ip
+ 1);
3189 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
3190 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
3191 opt
= cp
[IPOPT_OPTVAL
];
3192 if (opt
== IPOPT_EOL
)
3194 if (opt
== IPOPT_NOP
)
3197 if (cnt
< IPOPT_OLEN
+ sizeof (*cp
)) {
3198 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
3201 optlen
= cp
[IPOPT_OLEN
];
3202 if (optlen
< IPOPT_OLEN
+ sizeof (*cp
) ||
3204 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
3214 * Source routing with record.
3215 * Find interface with current destination address.
3216 * If none on this machine then drop if strictly routed,
3217 * or do nothing if loosely routed.
3218 * Record interface address and bring up next address
3219 * component. If strictly routed make sure next
3220 * address is on directly accessible net.
3224 if (optlen
< IPOPT_OFFSET
+ sizeof (*cp
)) {
3225 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
3228 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
3229 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
3232 ipaddr
.sin_addr
= ip
->ip_dst
;
3233 ia
= (struct in_ifaddr
*)ifa_ifwithaddr(SA(&ipaddr
));
3235 if (opt
== IPOPT_SSRR
) {
3236 type
= ICMP_UNREACH
;
3237 code
= ICMP_UNREACH_SRCFAIL
;
3240 if (!ip_dosourceroute
)
3241 goto nosourcerouting
;
3243 * Loose routing, and not at next destination
3244 * yet; nothing to do except forward.
3248 IFA_REMREF(&ia
->ia_ifa
);
3251 off
--; /* 0 origin */
3252 if (off
> optlen
- (int)sizeof (struct in_addr
)) {
3254 * End of source route. Should be for us.
3256 if (!ip_acceptsourceroute
)
3257 goto nosourcerouting
;
3258 save_rte(cp
, ip
->ip_src
);
3262 if (!ip_dosourceroute
) {
3264 char buf
[MAX_IPv4_STR_LEN
];
3265 char buf2
[MAX_IPv4_STR_LEN
];
3267 * Acting as a router, so generate ICMP
3271 "attempted source route from %s "
3273 inet_ntop(AF_INET
, &ip
->ip_src
,
3275 inet_ntop(AF_INET
, &ip
->ip_dst
,
3276 buf2
, sizeof (buf2
)));
3277 type
= ICMP_UNREACH
;
3278 code
= ICMP_UNREACH_SRCFAIL
;
3282 * Not acting as a router,
3285 OSAddAtomic(1, &ipstat
.ips_cantforward
);
3292 * locate outgoing interface
3294 (void) memcpy(&ipaddr
.sin_addr
, cp
+ off
,
3295 sizeof (ipaddr
.sin_addr
));
3297 if (opt
== IPOPT_SSRR
) {
3298 #define INA struct in_ifaddr *
3299 if ((ia
= (INA
)ifa_ifwithdstaddr(
3300 SA(&ipaddr
))) == NULL
) {
3301 ia
= (INA
)ifa_ifwithnet(SA(&ipaddr
));
3304 ia
= ip_rtaddr(ipaddr
.sin_addr
);
3307 type
= ICMP_UNREACH
;
3308 code
= ICMP_UNREACH_SRCFAIL
;
3311 ip
->ip_dst
= ipaddr
.sin_addr
;
3312 IFA_LOCK(&ia
->ia_ifa
);
3313 (void) memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
3314 sizeof (struct in_addr
));
3315 IFA_UNLOCK(&ia
->ia_ifa
);
3316 IFA_REMREF(&ia
->ia_ifa
);
3318 cp
[IPOPT_OFFSET
] += sizeof (struct in_addr
);
3320 * Let ip_intr's mcast routing check handle mcast pkts
3322 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
3326 if (optlen
< IPOPT_OFFSET
+ sizeof (*cp
)) {
3327 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
3330 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
3331 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
3335 * If no space remains, ignore.
3337 off
--; /* 0 origin */
3338 if (off
> optlen
- (int)sizeof (struct in_addr
))
3340 (void) memcpy(&ipaddr
.sin_addr
, &ip
->ip_dst
,
3341 sizeof (ipaddr
.sin_addr
));
3343 * locate outgoing interface; if we're the destination,
3344 * use the incoming interface (should be same).
3346 if ((ia
= (INA
)ifa_ifwithaddr(SA(&ipaddr
))) == NULL
) {
3347 if ((ia
= ip_rtaddr(ipaddr
.sin_addr
)) == NULL
) {
3348 type
= ICMP_UNREACH
;
3349 code
= ICMP_UNREACH_HOST
;
3353 IFA_LOCK(&ia
->ia_ifa
);
3354 (void) memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
3355 sizeof (struct in_addr
));
3356 IFA_UNLOCK(&ia
->ia_ifa
);
3357 IFA_REMREF(&ia
->ia_ifa
);
3359 cp
[IPOPT_OFFSET
] += sizeof (struct in_addr
);
3363 code
= cp
- (u_char
*)ip
;
3364 ipt
= (struct ip_timestamp
*)(void *)cp
;
3365 if (ipt
->ipt_len
< 4 || ipt
->ipt_len
> 40) {
3366 code
= (u_char
*)&ipt
->ipt_len
- (u_char
*)ip
;
3369 if (ipt
->ipt_ptr
< 5) {
3370 code
= (u_char
*)&ipt
->ipt_ptr
- (u_char
*)ip
;
3374 ipt
->ipt_len
- (int)sizeof (int32_t)) {
3375 if (++ipt
->ipt_oflw
== 0) {
3376 code
= (u_char
*)&ipt
->ipt_ptr
-
3382 sin
= (struct in_addr
*)(void *)(cp
+ ipt
->ipt_ptr
- 1);
3383 switch (ipt
->ipt_flg
) {
3385 case IPOPT_TS_TSONLY
:
3388 case IPOPT_TS_TSANDADDR
:
3389 if (ipt
->ipt_ptr
- 1 + sizeof (n_time
) +
3390 sizeof (struct in_addr
) > ipt
->ipt_len
) {
3391 code
= (u_char
*)&ipt
->ipt_ptr
-
3395 ipaddr
.sin_addr
= dst
;
3396 ia
= (INA
)ifaof_ifpforaddr(SA(&ipaddr
),
3400 IFA_LOCK(&ia
->ia_ifa
);
3401 (void) memcpy(sin
, &IA_SIN(ia
)->sin_addr
,
3402 sizeof (struct in_addr
));
3403 IFA_UNLOCK(&ia
->ia_ifa
);
3404 ipt
->ipt_ptr
+= sizeof (struct in_addr
);
3405 IFA_REMREF(&ia
->ia_ifa
);
3409 case IPOPT_TS_PRESPEC
:
3410 if (ipt
->ipt_ptr
- 1 + sizeof (n_time
) +
3411 sizeof (struct in_addr
) > ipt
->ipt_len
) {
3412 code
= (u_char
*)&ipt
->ipt_ptr
-
3416 (void) memcpy(&ipaddr
.sin_addr
, sin
,
3417 sizeof (struct in_addr
));
3418 if ((ia
= (struct in_ifaddr
*)ifa_ifwithaddr(
3419 SA(&ipaddr
))) == NULL
)
3421 IFA_REMREF(&ia
->ia_ifa
);
3423 ipt
->ipt_ptr
+= sizeof (struct in_addr
);
3427 /* XXX can't take &ipt->ipt_flg */
3428 code
= (u_char
*)&ipt
->ipt_ptr
-
3433 (void) memcpy(cp
+ ipt
->ipt_ptr
- 1, &ntime
,
3435 ipt
->ipt_ptr
+= sizeof (n_time
);
3438 if (forward
&& ipforwarding
) {
3439 ip_forward(m
, 1, next_hop
);
3444 icmp_error(m
, type
, code
, 0, 0);
3445 OSAddAtomic(1, &ipstat
.ips_badoptions
);
3450 * Check for the presence of the IP Router Alert option [RFC2113]
3451 * in the header of an IPv4 datagram.
3453 * This call is not intended for use from the forwarding path; it is here
3454 * so that protocol domains may check for the presence of the option.
3455 * Given how FreeBSD's IPv4 stack is currently structured, the Router Alert
3456 * option does not have much relevance to the implementation, though this
3457 * may change in future.
3458 * Router alert options SHOULD be passed if running in IPSTEALTH mode and
3459 * we are not the endpoint.
3460 * Length checks on individual options should already have been peformed
3461 * by ip_dooptions() therefore they are folded under DIAGNOSTIC here.
3463 * Return zero if not present or options are invalid, non-zero if present.
3466 ip_checkrouteralert(struct mbuf
*m
)
3468 struct ip
*ip
= mtod(m
, struct ip
*);
3470 int opt
, optlen
, cnt
, found_ra
;
3473 cp
= (u_char
*)(ip
+ 1);
3474 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
3475 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
3476 opt
= cp
[IPOPT_OPTVAL
];
3477 if (opt
== IPOPT_EOL
)
3479 if (opt
== IPOPT_NOP
)
3483 if (cnt
< IPOPT_OLEN
+ sizeof (*cp
))
3486 optlen
= cp
[IPOPT_OLEN
];
3488 if (optlen
< IPOPT_OLEN
+ sizeof (*cp
) || optlen
> cnt
)
3495 if (optlen
!= IPOPT_OFFSET
+ sizeof (uint16_t) ||
3496 (*((uint16_t *)(void *)&cp
[IPOPT_OFFSET
]) != 0))
3511 * Given address of next destination (final or next hop),
3512 * return internet address info of interface to be used to get there.
3515 ip_rtaddr(struct in_addr dst
)
3517 struct sockaddr_in
*sin
;
3518 struct ifaddr
*rt_ifa
;
3521 bzero(&ro
, sizeof (ro
));
3522 sin
= SIN(&ro
.ro_dst
);
3523 sin
->sin_family
= AF_INET
;
3524 sin
->sin_len
= sizeof (*sin
);
3525 sin
->sin_addr
= dst
;
3527 rtalloc_ign(&ro
, RTF_PRCLONING
);
3528 if (ro
.ro_rt
== NULL
) {
3534 if ((rt_ifa
= ro
.ro_rt
->rt_ifa
) != NULL
)
3536 RT_UNLOCK(ro
.ro_rt
);
3539 return ((struct in_ifaddr
*)rt_ifa
);
3543 * Save incoming source route for use in replies,
3544 * to be picked up later by ip_srcroute if the receiver is interested.
3547 save_rte(u_char
*option
, struct in_addr dst
)
3551 olen
= option
[IPOPT_OLEN
];
3554 printf("save_rte: olen %d\n", olen
);
3556 if (olen
> sizeof (ip_srcrt
) - (1 + sizeof (dst
)))
3558 bcopy(option
, ip_srcrt
.srcopt
, olen
);
3559 ip_nhops
= (olen
- IPOPT_OFFSET
- 1) / sizeof (struct in_addr
);
3564 * Retrieve incoming source route for use in replies,
3565 * in the same form used by setsockopt.
3566 * The first hop is placed before the options, will be removed later.
3571 struct in_addr
*p
, *q
;
3577 m
= m_get(M_DONTWAIT
, MT_HEADER
);
3581 #define OPTSIZ (sizeof (ip_srcrt.nop) + sizeof (ip_srcrt.srcopt))
3583 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
3584 m
->m_len
= ip_nhops
* sizeof (struct in_addr
) +
3585 sizeof (struct in_addr
) + OPTSIZ
;
3588 printf("ip_srcroute: nhops %d mlen %d", ip_nhops
, m
->m_len
);
3592 * First save first hop for return route
3594 p
= &ip_srcrt
.route
[ip_nhops
- 1];
3595 *(mtod(m
, struct in_addr
*)) = *p
--;
3599 (u_int32_t
)ntohl(mtod(m
, struct in_addr
*)->s_addr
));
3603 * Copy option fields and padding (nop) to mbuf.
3605 ip_srcrt
.nop
= IPOPT_NOP
;
3606 ip_srcrt
.srcopt
[IPOPT_OFFSET
] = IPOPT_MINOFF
;
3607 (void) memcpy(mtod(m
, caddr_t
) + sizeof (struct in_addr
),
3608 &ip_srcrt
.nop
, OPTSIZ
);
3609 q
= (struct in_addr
*)(void *)(mtod(m
, caddr_t
) +
3610 sizeof (struct in_addr
) + OPTSIZ
);
3613 * Record return path as an IP source route,
3614 * reversing the path (pointers are now aligned).
3616 while (p
>= ip_srcrt
.route
) {
3619 printf(" %lx", (u_int32_t
)ntohl(q
->s_addr
));
3624 * Last hop goes to final destination.
3629 printf(" %lx\n", (u_int32_t
)ntohl(q
->s_addr
));
3635 * Strip out IP options, at higher
3636 * level protocol in the kernel.
3637 * Second argument is buffer to which options
3638 * will be moved, and return value is their length.
3639 * XXX should be deleted; last arg currently ignored.
3642 ip_stripoptions(struct mbuf
*m
, struct mbuf
*mopt
)
3644 #pragma unused(mopt)
3646 struct ip
*ip
= mtod(m
, struct ip
*);
3650 /* Expect 32-bit aligned data pointer on strict-align platforms */
3651 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
3653 olen
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
3654 opts
= (caddr_t
)(ip
+ 1);
3655 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
3656 bcopy(opts
+ olen
, opts
, (unsigned)i
);
3658 if (m
->m_flags
& M_PKTHDR
)
3659 m
->m_pkthdr
.len
-= olen
;
3660 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof (struct ip
) >> 2);
3663 u_char inetctlerrmap
[PRC_NCMDS
] = {
3665 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
3666 ENETUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
3667 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
3669 ENOPROTOOPT
, ECONNREFUSED
3673 sysctl_ipforwarding SYSCTL_HANDLER_ARGS
3675 #pragma unused(arg1, arg2)
3676 int i
, was_ipforwarding
= ipforwarding
;
3678 i
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
3679 if (i
!= 0 || req
->newptr
== USER_ADDR_NULL
)
3682 if (was_ipforwarding
&& !ipforwarding
) {
3683 /* clean up IPv4 forwarding cached routes */
3684 ifnet_head_lock_shared();
3685 for (i
= 0; i
<= if_index
; i
++) {
3686 struct ifnet
*ifp
= ifindex2ifnet
[i
];
3688 lck_mtx_lock(&ifp
->if_cached_route_lock
);
3689 ROUTE_RELEASE(&ifp
->if_fwd_route
);
3690 bzero(&ifp
->if_fwd_route
,
3691 sizeof (ifp
->if_fwd_route
));
3692 lck_mtx_unlock(&ifp
->if_cached_route_lock
);
3702 * Similar to inp_route_{copyout,copyin} routines except that these copy
3703 * out the cached IPv4 forwarding route from struct ifnet instead of the
3704 * inpcb. See comments for those routines for explanations.
3707 ip_fwd_route_copyout(struct ifnet
*ifp
, struct route
*dst
)
3709 struct route
*src
= &ifp
->if_fwd_route
;
3711 lck_mtx_lock_spin(&ifp
->if_cached_route_lock
);
3712 lck_mtx_convert_spin(&ifp
->if_cached_route_lock
);
3714 /* Minor sanity check */
3715 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET
)
3716 panic("%s: wrong or corrupted route: %p", __func__
, src
);
3718 route_copyout(dst
, src
, sizeof (*dst
));
3720 lck_mtx_unlock(&ifp
->if_cached_route_lock
);
3724 ip_fwd_route_copyin(struct ifnet
*ifp
, struct route
*src
)
3726 struct route
*dst
= &ifp
->if_fwd_route
;
3728 lck_mtx_lock_spin(&ifp
->if_cached_route_lock
);
3729 lck_mtx_convert_spin(&ifp
->if_cached_route_lock
);
3731 /* Minor sanity check */
3732 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET
)
3733 panic("%s: wrong or corrupted route: %p", __func__
, src
);
3735 if (ifp
->if_fwd_cacheok
)
3736 route_copyin(src
, dst
, sizeof (*src
));
3738 lck_mtx_unlock(&ifp
->if_cached_route_lock
);
3742 * Forward a packet. If some error occurs return the sender
3743 * an icmp packet. Note we can't always generate a meaningful
3744 * icmp message because icmp doesn't have a large enough repertoire
3745 * of codes and types.
3747 * If not forwarding, just drop the packet. This could be confusing
3748 * if ipforwarding was zero but some routing protocol was advancing
3749 * us as a gateway to somewhere. However, we must let the routing
3750 * protocol deal with that.
3752 * The srcrt parameter indicates whether the packet is being forwarded
3753 * via a source route.
3756 ip_forward(struct mbuf
*m
, int srcrt
, struct sockaddr_in
*next_hop
)
3759 #pragma unused(next_hop)
3761 struct ip
*ip
= mtod(m
, struct ip
*);
3762 struct sockaddr_in
*sin
;
3764 struct route fwd_rt
;
3765 int error
, type
= 0, code
= 0;
3768 struct in_addr pkt_dst
;
3769 u_int32_t nextmtu
= 0, len
;
3770 struct ip_out_args ipoa
= { IFSCOPE_NONE
, { 0 }, 0, 0,
3771 SO_TC_UNSPEC
, _NET_SERVICE_TYPE_UNSPEC
};
3772 struct ifnet
*rcvifp
= m
->m_pkthdr
.rcvif
;
3774 struct secpolicy
*sp
= NULL
;
3778 struct pf_mtag
*pf_mtag
;
3784 * Cache the destination address of the packet; this may be
3785 * changed by use of 'ipfw fwd'.
3787 pkt_dst
= ((next_hop
!= NULL
) ? next_hop
->sin_addr
: ip
->ip_dst
);
3788 #else /* !IPFIREWALL */
3789 pkt_dst
= ip
->ip_dst
;
3790 #endif /* !IPFIREWALL */
3794 printf("forward: src %lx dst %lx ttl %x\n",
3795 (u_int32_t
)ip
->ip_src
.s_addr
, (u_int32_t
)pkt_dst
.s_addr
,
3799 if (m
->m_flags
& (M_BCAST
|M_MCAST
) || !in_canforward(pkt_dst
)) {
3800 OSAddAtomic(1, &ipstat
.ips_cantforward
);
3806 #endif /* IPSTEALTH */
3807 if (ip
->ip_ttl
<= IPTTLDEC
) {
3808 icmp_error(m
, ICMP_TIMXCEED
, ICMP_TIMXCEED_INTRANS
,
3814 #endif /* IPSTEALTH */
3817 pf_mtag
= pf_find_mtag(m
);
3818 if (pf_mtag
!= NULL
&& pf_mtag
->pftag_rtableid
!= IFSCOPE_NONE
) {
3819 ipoa
.ipoa_boundif
= pf_mtag
->pftag_rtableid
;
3820 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
3824 ip_fwd_route_copyout(rcvifp
, &fwd_rt
);
3826 sin
= SIN(&fwd_rt
.ro_dst
);
3827 if (ROUTE_UNUSABLE(&fwd_rt
) || pkt_dst
.s_addr
!= sin
->sin_addr
.s_addr
) {
3828 ROUTE_RELEASE(&fwd_rt
);
3830 sin
->sin_family
= AF_INET
;
3831 sin
->sin_len
= sizeof (*sin
);
3832 sin
->sin_addr
= pkt_dst
;
3834 rtalloc_scoped_ign(&fwd_rt
, RTF_PRCLONING
, ipoa
.ipoa_boundif
);
3835 if (fwd_rt
.ro_rt
== NULL
) {
3836 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_HOST
, dest
, 0);
3843 * Save the IP header and at most 8 bytes of the payload,
3844 * in case we need to generate an ICMP message to the src.
3846 * We don't use m_copy() because it might return a reference
3847 * to a shared cluster. Both this function and ip_output()
3848 * assume exclusive access to the IP header in `m', so any
3849 * data in a cluster may change before we reach icmp_error().
3851 MGET(mcopy
, M_DONTWAIT
, m
->m_type
);
3852 if (mcopy
!= NULL
) {
3853 M_COPY_PKTHDR(mcopy
, m
);
3854 mcopy
->m_len
= imin((IP_VHL_HL(ip
->ip_vhl
) << 2) + 8,
3856 m_copydata(m
, 0, mcopy
->m_len
, mtod(mcopy
, caddr_t
));
3861 #endif /* IPSTEALTH */
3862 ip
->ip_ttl
-= IPTTLDEC
;
3865 #endif /* IPSTEALTH */
3868 * If forwarding packet using same interface that it came in on,
3869 * perhaps should send a redirect to sender to shortcut a hop.
3870 * Only send redirect if source is sending directly to us,
3871 * and if packet was not source routed (or has any options).
3872 * Also, don't send redirect if forwarding using a default route
3873 * or a route modified by a redirect.
3876 if (rt
->rt_ifp
== m
->m_pkthdr
.rcvif
&&
3877 !(rt
->rt_flags
& (RTF_DYNAMIC
|RTF_MODIFIED
)) &&
3878 satosin(rt_key(rt
))->sin_addr
.s_addr
!= INADDR_ANY
&&
3879 ipsendredirects
&& !srcrt
&& rt
->rt_ifa
!= NULL
) {
3880 struct in_ifaddr
*ia
= (struct in_ifaddr
*)rt
->rt_ifa
;
3881 u_int32_t src
= ntohl(ip
->ip_src
.s_addr
);
3883 /* Become a regular mutex */
3884 RT_CONVERT_LOCK(rt
);
3885 IFA_LOCK_SPIN(&ia
->ia_ifa
);
3886 if ((src
& ia
->ia_subnetmask
) == ia
->ia_subnet
) {
3887 if (rt
->rt_flags
& RTF_GATEWAY
)
3888 dest
= satosin(rt
->rt_gateway
)->sin_addr
.s_addr
;
3890 dest
= pkt_dst
.s_addr
;
3892 * Router requirements says to only send
3895 type
= ICMP_REDIRECT
;
3896 code
= ICMP_REDIRECT_HOST
;
3899 printf("redirect (%d) to %lx\n", code
,
3903 IFA_UNLOCK(&ia
->ia_ifa
);
3908 if (next_hop
!= NULL
) {
3909 /* Pass IPFORWARD info if available */
3911 struct ip_fwd_tag
*ipfwd_tag
;
3913 tag
= m_tag_create(KERNEL_MODULE_TAG_ID
,
3914 KERNEL_TAG_TYPE_IPFORWARD
,
3915 sizeof (*ipfwd_tag
), M_NOWAIT
, m
);
3922 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
3923 ipfwd_tag
->next_hop
= next_hop
;
3925 m_tag_prepend(m
, tag
);
3927 #endif /* IPFIREWALL */
3929 /* Mark this packet as being forwarded from another interface */
3930 m
->m_pkthdr
.pkt_flags
|= PKTF_FORWARDED
;
3933 error
= ip_output(m
, NULL
, &fwd_rt
, IP_FORWARDING
| IP_OUTARGS
,
3936 /* Refresh rt since the route could have changed while in IP */
3940 OSAddAtomic(1, &ipstat
.ips_cantforward
);
3943 * Increment stats on the source interface; the ones
3944 * for destination interface has been taken care of
3945 * during output above by virtue of PKTF_FORWARDED.
3947 rcvifp
->if_fpackets
++;
3948 rcvifp
->if_fbytes
+= len
;
3950 OSAddAtomic(1, &ipstat
.ips_forward
);
3952 OSAddAtomic(1, &ipstat
.ips_redirectsent
);
3954 if (mcopy
!= NULL
) {
3956 * If we didn't have to go thru ipflow and
3957 * the packet was successfully consumed by
3958 * ip_output, the mcopy is rather a waste;
3959 * this could be further optimized.
3970 case 0: /* forwarded, but need redirect */
3971 /* type, code set above */
3974 case ENETUNREACH
: /* shouldn't happen, checked above */
3979 type
= ICMP_UNREACH
;
3980 code
= ICMP_UNREACH_HOST
;
3984 type
= ICMP_UNREACH
;
3985 code
= ICMP_UNREACH_NEEDFRAG
;
3991 if (rt
->rt_ifp
!= NULL
)
3992 nextmtu
= rt
->rt_ifp
->if_mtu
;
4000 * If the packet is routed over IPsec tunnel, tell the
4001 * originator the tunnel MTU.
4002 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
4005 sp
= ipsec4_getpolicybyaddr(mcopy
, IPSEC_DIR_OUTBOUND
,
4006 IP_FORWARDING
, &ipsecerror
);
4012 * find the correct route for outer IPv4
4013 * header, compute tunnel MTU.
4017 if (sp
->req
!= NULL
&&
4018 sp
->req
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
4019 struct secasindex saidx
;
4020 struct secasvar
*sav
;
4025 /* count IPsec header size */
4026 ipsechdr
= ipsec_hdrsiz(sp
);
4028 ipm
= mtod(mcopy
, struct ip
*);
4029 bcopy(&sp
->req
->saidx
, &saidx
, sizeof (saidx
));
4030 saidx
.mode
= sp
->req
->saidx
.mode
;
4031 saidx
.reqid
= sp
->req
->saidx
.reqid
;
4032 sin
= SIN(&saidx
.src
);
4033 if (sin
->sin_len
== 0) {
4034 sin
->sin_len
= sizeof (*sin
);
4035 sin
->sin_family
= AF_INET
;
4036 sin
->sin_port
= IPSEC_PORT_ANY
;
4037 bcopy(&ipm
->ip_src
, &sin
->sin_addr
,
4038 sizeof (sin
->sin_addr
));
4040 sin
= SIN(&saidx
.dst
);
4041 if (sin
->sin_len
== 0) {
4042 sin
->sin_len
= sizeof (*sin
);
4043 sin
->sin_family
= AF_INET
;
4044 sin
->sin_port
= IPSEC_PORT_ANY
;
4045 bcopy(&ipm
->ip_dst
, &sin
->sin_addr
,
4046 sizeof (sin
->sin_addr
));
4048 sav
= key_allocsa_policy(&saidx
);
4050 lck_mtx_lock(sadb_mutex
);
4051 if (sav
->sah
!= NULL
) {
4052 ro
= &sav
->sah
->sa_route
;
4053 if (ro
->ro_rt
!= NULL
) {
4055 if (ro
->ro_rt
->rt_ifp
!= NULL
) {
4056 nextmtu
= ro
->ro_rt
->
4058 nextmtu
-= ipsechdr
;
4060 RT_UNLOCK(ro
->ro_rt
);
4063 key_freesav(sav
, KEY_SADB_LOCKED
);
4064 lck_mtx_unlock(sadb_mutex
);
4067 key_freesp(sp
, KEY_SADB_UNLOCKED
);
4073 * A router should not generate ICMP_SOURCEQUENCH as
4074 * required in RFC1812 Requirements for IP Version 4 Routers.
4075 * Source quench could be a big problem under DoS attacks,
4076 * or if the underlying interface is rate-limited.
4077 * Those who need source quench packets may re-enable them
4078 * via the net.inet.ip.sendsourcequench sysctl.
4080 if (ip_sendsourcequench
== 0) {
4084 type
= ICMP_SOURCEQUENCH
;
4089 case EACCES
: /* ipfw denied packet */
4094 if (type
== ICMP_UNREACH
&& code
== ICMP_UNREACH_NEEDFRAG
)
4095 OSAddAtomic(1, &ipstat
.ips_cantfrag
);
4097 icmp_error(mcopy
, type
, code
, dest
, nextmtu
);
4099 ip_fwd_route_copyin(rcvifp
, &fwd_rt
);
4103 ip_savecontrol(struct inpcb
*inp
, struct mbuf
**mp
, struct ip
*ip
,
4107 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
4111 mp
= sbcreatecontrol_mbuf((caddr_t
)&tv
, sizeof (tv
),
4112 SCM_TIMESTAMP
, SOL_SOCKET
, mp
);
4117 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) {
4120 time
= mach_absolute_time();
4121 mp
= sbcreatecontrol_mbuf((caddr_t
)&time
, sizeof (time
),
4122 SCM_TIMESTAMP_MONOTONIC
, SOL_SOCKET
, mp
);
4127 if (inp
->inp_flags
& INP_RECVDSTADDR
) {
4128 mp
= sbcreatecontrol_mbuf((caddr_t
)&ip
->ip_dst
,
4129 sizeof (struct in_addr
), IP_RECVDSTADDR
, IPPROTO_IP
, mp
);
4137 * Moving these out of udp_input() made them even more broken
4138 * than they already were.
4140 /* options were tossed already */
4141 if (inp
->inp_flags
& INP_RECVOPTS
) {
4142 mp
= sbcreatecontrol_mbuf((caddr_t
)opts_deleted_above
,
4143 sizeof (struct in_addr
), IP_RECVOPTS
, IPPROTO_IP
, mp
);
4148 /* ip_srcroute doesn't do what we want here, need to fix */
4149 if (inp
->inp_flags
& INP_RECVRETOPTS
) {
4150 mp
= sbcreatecontrol_mbuf((caddr_t
)ip_srcroute(),
4151 sizeof (struct in_addr
), IP_RECVRETOPTS
, IPPROTO_IP
, mp
);
4157 if (inp
->inp_flags
& INP_RECVIF
) {
4159 uint8_t sdlbuf
[SOCK_MAXADDRLEN
+ 1];
4160 struct sockaddr_dl
*sdl2
= SDL(&sdlbuf
);
4163 * Make sure to accomodate the largest possible
4164 * size of SA(if_lladdr)->sa_len.
4166 _CASSERT(sizeof (sdlbuf
) == (SOCK_MAXADDRLEN
+ 1));
4168 ifnet_head_lock_shared();
4169 if ((ifp
= m
->m_pkthdr
.rcvif
) != NULL
&&
4170 ifp
->if_index
&& (ifp
->if_index
<= if_index
)) {
4171 struct ifaddr
*ifa
= ifnet_addrs
[ifp
->if_index
- 1];
4172 struct sockaddr_dl
*sdp
;
4174 if (!ifa
|| !ifa
->ifa_addr
)
4178 sdp
= SDL(ifa
->ifa_addr
);
4180 * Change our mind and don't try copy.
4182 if (sdp
->sdl_family
!= AF_LINK
) {
4186 /* the above _CASSERT ensures sdl_len fits in sdlbuf */
4187 bcopy(sdp
, sdl2
, sdp
->sdl_len
);
4192 offsetof(struct sockaddr_dl
, sdl_data
[0]);
4193 sdl2
->sdl_family
= AF_LINK
;
4194 sdl2
->sdl_index
= 0;
4195 sdl2
->sdl_nlen
= sdl2
->sdl_alen
= sdl2
->sdl_slen
= 0;
4198 mp
= sbcreatecontrol_mbuf((caddr_t
)sdl2
, sdl2
->sdl_len
,
4199 IP_RECVIF
, IPPROTO_IP
, mp
);
4204 if (inp
->inp_flags
& INP_RECVTTL
) {
4205 mp
= sbcreatecontrol_mbuf((caddr_t
)&ip
->ip_ttl
,
4206 sizeof (ip
->ip_ttl
), IP_RECVTTL
, IPPROTO_IP
, mp
);
4211 if (inp
->inp_socket
->so_flags
& SOF_RECV_TRAFFIC_CLASS
) {
4212 int tc
= m_get_traffic_class(m
);
4214 mp
= sbcreatecontrol_mbuf((caddr_t
)&tc
, sizeof (tc
),
4215 SO_TRAFFIC_CLASS
, SOL_SOCKET
, mp
);
4220 if (inp
->inp_flags
& INP_PKTINFO
) {
4221 struct in_pktinfo pi
;
4223 bzero(&pi
, sizeof (struct in_pktinfo
));
4224 bcopy(&ip
->ip_dst
, &pi
.ipi_addr
, sizeof (struct in_addr
));
4225 pi
.ipi_ifindex
= (m
!= NULL
&& m
->m_pkthdr
.rcvif
!= NULL
) ?
4226 m
->m_pkthdr
.rcvif
->if_index
: 0;
4228 mp
= sbcreatecontrol_mbuf((caddr_t
)&pi
,
4229 sizeof (struct in_pktinfo
), IP_RECVPKTINFO
, IPPROTO_IP
, mp
);
4234 if (inp
->inp_flags
& INP_RECVTOS
) {
4235 mp
= sbcreatecontrol_mbuf((caddr_t
)&ip
->ip_tos
,
4236 sizeof(u_char
), IP_RECVTOS
, IPPROTO_IP
, mp
);
4244 ipstat
.ips_pktdropcntrl
++;
4248 static inline u_short
4249 ip_cksum(struct mbuf
*m
, int hlen
)
4253 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
4254 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
4255 } else if (!(m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) &&
4256 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
4258 * The packet arrived on an interface which isn't capable
4259 * of performing IP header checksum; compute it now.
4261 sum
= ip_cksum_hdr_in(m
, hlen
);
4264 m
->m_pkthdr
.csum_flags
|= (CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
4265 CSUM_IP_CHECKED
| CSUM_IP_VALID
);
4266 m
->m_pkthdr
.csum_data
= 0xffff;
4270 OSAddAtomic(1, &ipstat
.ips_badsum
);
4276 ip_getstat SYSCTL_HANDLER_ARGS
4278 #pragma unused(oidp, arg1, arg2)
4279 if (req
->oldptr
== USER_ADDR_NULL
)
4280 req
->oldlen
= (size_t)sizeof (struct ipstat
);
4282 return (SYSCTL_OUT(req
, &ipstat
, MIN(sizeof (ipstat
), req
->oldlen
)));
4286 ip_setsrcifaddr_info(struct mbuf
*m
, uint32_t src_idx
, struct in_ifaddr
*ia
)
4288 VERIFY(m
->m_flags
& M_PKTHDR
);
4291 * If the source ifaddr is specified, pick up the information
4292 * from there; otherwise just grab the passed-in ifindex as the
4293 * caller may not have the ifaddr available.
4296 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
4297 m
->m_pkthdr
.src_ifindex
= ia
->ia_ifp
->if_index
;
4299 m
->m_pkthdr
.src_ifindex
= src_idx
;
4301 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
4306 ip_setdstifaddr_info(struct mbuf
*m
, uint32_t dst_idx
, struct in_ifaddr
*ia
)
4308 VERIFY(m
->m_flags
& M_PKTHDR
);
4311 * If the destination ifaddr is specified, pick up the information
4312 * from there; otherwise just grab the passed-in ifindex as the
4313 * caller may not have the ifaddr available.
4316 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
4317 m
->m_pkthdr
.dst_ifindex
= ia
->ia_ifp
->if_index
;
4319 m
->m_pkthdr
.dst_ifindex
= dst_idx
;
4321 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
4326 ip_getsrcifaddr_info(struct mbuf
*m
, uint32_t *src_idx
, uint32_t *iaf
)
4328 VERIFY(m
->m_flags
& M_PKTHDR
);
4330 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
))
4333 if (src_idx
!= NULL
)
4334 *src_idx
= m
->m_pkthdr
.src_ifindex
;
4343 ip_getdstifaddr_info(struct mbuf
*m
, uint32_t *dst_idx
, uint32_t *iaf
)
4345 VERIFY(m
->m_flags
& M_PKTHDR
);
4347 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
))
4350 if (dst_idx
!= NULL
)
4351 *dst_idx
= m
->m_pkthdr
.dst_ifindex
;
4360 * Protocol input handler for IPPROTO_GRE.
4363 gre_input(struct mbuf
*m
, int off
)
4365 gre_input_func_t fn
= gre_input_func
;
4368 * If there is a registered GRE input handler, pass mbuf to it.
4371 lck_mtx_unlock(inet_domain_mutex
);
4372 m
= fn(m
, off
, (mtod(m
, struct ip
*))->ip_p
);
4373 lck_mtx_lock(inet_domain_mutex
);
4377 * If no matching tunnel that is up is found, we inject
4378 * the mbuf to raw ip socket to see if anyone picks it up.
4385 * Private KPI for PPP/PPTP.
4388 ip_gre_register_input(gre_input_func_t fn
)
4390 lck_mtx_lock(inet_domain_mutex
);
4391 gre_input_func
= fn
;
4392 lck_mtx_unlock(inet_domain_mutex
);
4397 #if (DEBUG || DEVELOPMENT)
4399 sysctl_reset_ip_input_stats SYSCTL_HANDLER_ARGS
4401 #pragma unused(arg1, arg2)
4404 i
= ip_input_measure
;
4405 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
4406 if (error
|| req
->newptr
== USER_ADDR_NULL
)
4409 if (i
< 0 || i
> 1) {
4413 if (ip_input_measure
!= i
&& i
== 1) {
4414 net_perf_initialize(&net_perf
, ip_input_measure_bins
);
4416 ip_input_measure
= i
;
4422 sysctl_ip_input_measure_bins SYSCTL_HANDLER_ARGS
4424 #pragma unused(arg1, arg2)
4428 i
= ip_input_measure_bins
;
4429 error
= sysctl_handle_quad(oidp
, &i
, 0, req
);
4430 if (error
|| req
->newptr
== USER_ADDR_NULL
)
4433 if (!net_perf_validate_bins(i
)) {
4437 ip_input_measure_bins
= i
;
4443 sysctl_ip_input_getperf SYSCTL_HANDLER_ARGS
4445 #pragma unused(oidp, arg1, arg2)
4446 if (req
->oldptr
== USER_ADDR_NULL
)
4447 req
->oldlen
= (size_t)sizeof (struct ipstat
);
4449 return (SYSCTL_OUT(req
, &net_perf
, MIN(sizeof (net_perf
), req
->oldlen
)));
4451 #endif /* (DEBUG || DEVELOPMENT) */