2 * Copyright (c) 2000-2020 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, 1991, 1993, 1995
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 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
61 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.17 2001/08/13 16:26:17 ume Exp $
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/malloc.h>
68 #include <sys/domain.h>
69 #include <sys/protosw.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
73 #include <sys/kernel.h>
74 #include <sys/sysctl.h>
75 #include <sys/mcache.h>
76 #include <sys/kauth.h>
78 #include <sys/proc_uuid_policy.h>
79 #include <sys/syslog.h>
83 #include <libkern/OSAtomic.h>
84 #include <kern/locks.h>
86 #include <machine/limits.h>
88 #include <kern/zalloc.h>
91 #include <net/if_types.h>
92 #include <net/route.h>
93 #include <net/flowhash.h>
94 #include <net/flowadv.h>
95 #include <net/nat464_utils.h>
96 #include <net/ntstat.h>
97 #include <net/restricted_in_port.h>
99 #include <netinet/in.h>
100 #include <netinet/in_pcb.h>
101 #include <netinet/in_var.h>
102 #include <netinet/ip_var.h>
104 #include <netinet/ip6.h>
105 #include <netinet6/ip6_var.h>
107 #include <sys/kdebug.h>
108 #include <sys/random.h>
110 #include <dev/random/randomdev.h>
111 #include <mach/boolean.h>
113 #include <pexpert/pexpert.h>
116 #include <net/necp.h>
119 #include <sys/stat.h>
121 #include <sys/vnode.h>
125 extern const char *proc_name_address(struct proc
*);
127 static lck_grp_t
*inpcb_lock_grp
;
128 static lck_attr_t
*inpcb_lock_attr
;
129 static lck_grp_attr_t
*inpcb_lock_grp_attr
;
130 decl_lck_mtx_data(static, inpcb_lock
); /* global INPCB lock */
131 decl_lck_mtx_data(static, inpcb_timeout_lock
);
133 static TAILQ_HEAD(, inpcbinfo
) inpcb_head
= TAILQ_HEAD_INITIALIZER(inpcb_head
);
135 static u_int16_t inpcb_timeout_run
= 0; /* INPCB timer is scheduled to run */
136 static boolean_t inpcb_garbage_collecting
= FALSE
; /* gc timer is scheduled */
137 static boolean_t inpcb_ticking
= FALSE
; /* "slow" timer is scheduled */
138 static boolean_t inpcb_fast_timer_on
= FALSE
;
140 #define INPCB_GCREQ_THRESHOLD 50000
142 static thread_call_t inpcb_thread_call
, inpcb_fast_thread_call
;
143 static void inpcb_sched_timeout(void);
144 static void inpcb_sched_lazy_timeout(void);
145 static void _inpcb_sched_timeout(unsigned int);
146 static void inpcb_timeout(void *, void *);
147 const int inpcb_timeout_lazy
= 10; /* 10 seconds leeway for lazy timers */
148 extern int tvtohz(struct timeval
*);
150 #if CONFIG_PROC_UUID_POLICY
151 static void inp_update_cellular_policy(struct inpcb
*, boolean_t
);
153 static void inp_update_necp_want_app_policy(struct inpcb
*, boolean_t
);
155 #endif /* !CONFIG_PROC_UUID_POLICY */
157 #define DBG_FNC_PCB_LOOKUP NETDBG_CODE(DBG_NETTCP, (6 << 8))
158 #define DBG_FNC_PCB_HLOOKUP NETDBG_CODE(DBG_NETTCP, ((6 << 8) | 1))
160 int allow_udp_port_exhaustion
= 0;
163 * These configure the range of local port addresses assigned to
164 * "unspecified" outgoing connections/packets/whatever.
166 int ipport_lowfirstauto
= IPPORT_RESERVED
- 1; /* 1023 */
167 int ipport_lowlastauto
= IPPORT_RESERVEDSTART
; /* 600 */
168 int ipport_firstauto
= IPPORT_HIFIRSTAUTO
; /* 49152 */
169 int ipport_lastauto
= IPPORT_HILASTAUTO
; /* 65535 */
170 int ipport_hifirstauto
= IPPORT_HIFIRSTAUTO
; /* 49152 */
171 int ipport_hilastauto
= IPPORT_HILASTAUTO
; /* 65535 */
173 #define RANGECHK(var, min, max) \
174 if ((var) < (min)) { (var) = (min); } \
175 else if ((var) > (max)) { (var) = (max); }
178 sysctl_net_ipport_check SYSCTL_HANDLER_ARGS
180 #pragma unused(arg1, arg2)
182 int new_value
= *(int *)oidp
->oid_arg1
;
183 #if (DEBUG | DEVELOPMENT)
184 int old_value
= *(int *)oidp
->oid_arg1
;
186 * For unit testing allow a non-superuser process with the
187 * proper entitlement to modify the variables
190 if (proc_suser(current_proc()) != 0 &&
191 (error
= priv_check_cred(kauth_cred_get(),
192 PRIV_NETINET_RESERVEDPORT
, 0))) {
196 #endif /* (DEBUG | DEVELOPMENT) */
198 error
= sysctl_handle_int(oidp
, &new_value
, 0, req
);
200 if (oidp
->oid_arg1
== &ipport_lowfirstauto
|| oidp
->oid_arg1
== &ipport_lowlastauto
) {
201 RANGECHK(new_value
, 1, IPPORT_RESERVED
- 1);
203 RANGECHK(new_value
, IPPORT_RESERVED
, USHRT_MAX
);
205 *(int *)oidp
->oid_arg1
= new_value
;
208 #if (DEBUG | DEVELOPMENT)
209 os_log(OS_LOG_DEFAULT
,
210 "%s:%u sysctl net.restricted_port.verbose: %d -> %d)",
211 proc_best_name(current_proc()), proc_selfpid(),
212 old_value
, *(int *)oidp
->oid_arg1
);
213 #endif /* (DEBUG | DEVELOPMENT) */
220 SYSCTL_NODE(_net_inet_ip
, IPPROTO_IP
, portrange
,
221 CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "IP Ports");
223 #if (DEBUG | DEVELOPMENT)
224 #define CTLFAGS_IP_PORTRANGE (CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY)
226 #define CTLFAGS_IP_PORTRANGE (CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED)
227 #endif /* (DEBUG | DEVELOPMENT) */
229 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, lowfirst
,
230 CTLFAGS_IP_PORTRANGE
,
231 &ipport_lowfirstauto
, 0, &sysctl_net_ipport_check
, "I", "");
232 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, lowlast
,
233 CTLFAGS_IP_PORTRANGE
,
234 &ipport_lowlastauto
, 0, &sysctl_net_ipport_check
, "I", "");
235 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, first
,
236 CTLFAGS_IP_PORTRANGE
,
237 &ipport_firstauto
, 0, &sysctl_net_ipport_check
, "I", "");
238 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, last
,
239 CTLFAGS_IP_PORTRANGE
,
240 &ipport_lastauto
, 0, &sysctl_net_ipport_check
, "I", "");
241 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, hifirst
,
242 CTLFAGS_IP_PORTRANGE
,
243 &ipport_hifirstauto
, 0, &sysctl_net_ipport_check
, "I", "");
244 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, hilast
,
245 CTLFAGS_IP_PORTRANGE
,
246 &ipport_hilastauto
, 0, &sysctl_net_ipport_check
, "I", "");
247 SYSCTL_INT(_net_inet_ip_portrange
, OID_AUTO
, ipport_allow_udp_port_exhaustion
,
248 CTLFLAG_LOCKED
| CTLFLAG_RW
, &allow_udp_port_exhaustion
, 0, "");
250 static uint32_t apn_fallbk_debug
= 0;
251 #define apn_fallbk_log(x) do { if (apn_fallbk_debug >= 1) log x; } while (0)
253 #if !XNU_TARGET_OS_OSX
254 static boolean_t apn_fallbk_enabled
= TRUE
;
256 SYSCTL_DECL(_net_inet
);
257 SYSCTL_NODE(_net_inet
, OID_AUTO
, apn_fallback
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "APN Fallback");
258 SYSCTL_UINT(_net_inet_apn_fallback
, OID_AUTO
, enable
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
259 &apn_fallbk_enabled
, 0, "APN fallback enable");
260 SYSCTL_UINT(_net_inet_apn_fallback
, OID_AUTO
, debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
261 &apn_fallbk_debug
, 0, "APN fallback debug enable");
262 #else /* XNU_TARGET_OS_OSX */
263 static boolean_t apn_fallbk_enabled
= FALSE
;
264 #endif /* XNU_TARGET_OS_OSX */
266 extern int udp_use_randomport
;
267 extern int tcp_use_randomport
;
269 /* Structs used for flowhash computation */
270 struct inp_flowhash_key_addr
{
280 struct inp_flowhash_key
{
281 struct inp_flowhash_key_addr infh_laddr
;
282 struct inp_flowhash_key_addr infh_faddr
;
283 u_int32_t infh_lport
;
284 u_int32_t infh_fport
;
286 u_int32_t infh_proto
;
287 u_int32_t infh_rand1
;
288 u_int32_t infh_rand2
;
291 static u_int32_t inp_hash_seed
= 0;
293 static int infc_cmp(const struct inpcb
*, const struct inpcb
*);
295 /* Flags used by inp_fc_getinp */
296 #define INPFC_SOLOCKED 0x1
297 #define INPFC_REMOVE 0x2
298 static struct inpcb
*inp_fc_getinp(u_int32_t
, u_int32_t
);
300 static void inp_fc_feedback(struct inpcb
*);
301 extern void tcp_remove_from_time_wait(struct inpcb
*inp
);
303 decl_lck_mtx_data(static, inp_fc_lck
);
305 RB_HEAD(inp_fc_tree
, inpcb
) inp_fc_tree
;
306 RB_PROTOTYPE(inp_fc_tree
, inpcb
, infc_link
, infc_cmp
);
307 RB_GENERATE(inp_fc_tree
, inpcb
, infc_link
, infc_cmp
);
310 * Use this inp as a key to find an inp in the flowhash tree.
311 * Accesses to it are protected by inp_fc_lck.
313 struct inpcb key_inp
;
316 * in_pcb.c: manage the Protocol Control Blocks.
322 static int inpcb_initialized
= 0;
324 VERIFY(!inpcb_initialized
);
325 inpcb_initialized
= 1;
327 inpcb_lock_grp_attr
= lck_grp_attr_alloc_init();
328 inpcb_lock_grp
= lck_grp_alloc_init("inpcb", inpcb_lock_grp_attr
);
329 inpcb_lock_attr
= lck_attr_alloc_init();
330 lck_mtx_init(&inpcb_lock
, inpcb_lock_grp
, inpcb_lock_attr
);
331 lck_mtx_init(&inpcb_timeout_lock
, inpcb_lock_grp
, inpcb_lock_attr
);
332 inpcb_thread_call
= thread_call_allocate_with_priority(inpcb_timeout
,
333 NULL
, THREAD_CALL_PRIORITY_KERNEL
);
334 inpcb_fast_thread_call
= thread_call_allocate_with_priority(
335 inpcb_timeout
, NULL
, THREAD_CALL_PRIORITY_KERNEL
);
336 if (inpcb_thread_call
== NULL
|| inpcb_fast_thread_call
== NULL
) {
337 panic("unable to alloc the inpcb thread call");
341 * Initialize data structures required to deliver
344 lck_mtx_init(&inp_fc_lck
, inpcb_lock_grp
, inpcb_lock_attr
);
345 lck_mtx_lock(&inp_fc_lck
);
346 RB_INIT(&inp_fc_tree
);
347 bzero(&key_inp
, sizeof(key_inp
));
348 lck_mtx_unlock(&inp_fc_lck
);
351 #define INPCB_HAVE_TIMER_REQ(req) (((req).intimer_lazy > 0) || \
352 ((req).intimer_fast > 0) || ((req).intimer_nodelay > 0))
354 inpcb_timeout(void *arg0
, void *arg1
)
356 #pragma unused(arg0, arg1)
357 struct inpcbinfo
*ipi
;
359 struct intimercount gccnt
, tmcnt
;
362 * Update coarse-grained networking timestamp (in sec.); the idea
363 * is to piggy-back on the timeout callout to update the counter
364 * returnable via net_uptime().
368 bzero(&gccnt
, sizeof(gccnt
));
369 bzero(&tmcnt
, sizeof(tmcnt
));
371 lck_mtx_lock_spin(&inpcb_timeout_lock
);
372 gc
= inpcb_garbage_collecting
;
373 inpcb_garbage_collecting
= FALSE
;
376 inpcb_ticking
= FALSE
;
379 lck_mtx_unlock(&inpcb_timeout_lock
);
381 lck_mtx_lock(&inpcb_lock
);
382 TAILQ_FOREACH(ipi
, &inpcb_head
, ipi_entry
) {
383 if (INPCB_HAVE_TIMER_REQ(ipi
->ipi_gc_req
)) {
384 bzero(&ipi
->ipi_gc_req
,
385 sizeof(ipi
->ipi_gc_req
));
386 if (gc
&& ipi
->ipi_gc
!= NULL
) {
388 gccnt
.intimer_lazy
+=
389 ipi
->ipi_gc_req
.intimer_lazy
;
390 gccnt
.intimer_fast
+=
391 ipi
->ipi_gc_req
.intimer_fast
;
392 gccnt
.intimer_nodelay
+=
393 ipi
->ipi_gc_req
.intimer_nodelay
;
396 if (INPCB_HAVE_TIMER_REQ(ipi
->ipi_timer_req
)) {
397 bzero(&ipi
->ipi_timer_req
,
398 sizeof(ipi
->ipi_timer_req
));
399 if (t
&& ipi
->ipi_timer
!= NULL
) {
401 tmcnt
.intimer_lazy
+=
402 ipi
->ipi_timer_req
.intimer_lazy
;
403 tmcnt
.intimer_fast
+=
404 ipi
->ipi_timer_req
.intimer_fast
;
405 tmcnt
.intimer_nodelay
+=
406 ipi
->ipi_timer_req
.intimer_nodelay
;
410 lck_mtx_unlock(&inpcb_lock
);
411 lck_mtx_lock_spin(&inpcb_timeout_lock
);
414 /* lock was dropped above, so check first before overriding */
415 if (!inpcb_garbage_collecting
) {
416 inpcb_garbage_collecting
= INPCB_HAVE_TIMER_REQ(gccnt
);
418 if (!inpcb_ticking
) {
419 inpcb_ticking
= INPCB_HAVE_TIMER_REQ(tmcnt
);
422 /* re-arm the timer if there's work to do */
424 VERIFY(inpcb_timeout_run
>= 0 && inpcb_timeout_run
< 2);
426 if (gccnt
.intimer_nodelay
> 0 || tmcnt
.intimer_nodelay
> 0) {
427 inpcb_sched_timeout();
428 } else if ((gccnt
.intimer_fast
+ tmcnt
.intimer_fast
) <= 5) {
429 /* be lazy when idle with little activity */
430 inpcb_sched_lazy_timeout();
432 inpcb_sched_timeout();
435 lck_mtx_unlock(&inpcb_timeout_lock
);
439 inpcb_sched_timeout(void)
441 _inpcb_sched_timeout(0);
445 inpcb_sched_lazy_timeout(void)
447 _inpcb_sched_timeout(inpcb_timeout_lazy
);
451 _inpcb_sched_timeout(unsigned int offset
)
453 uint64_t deadline
, leeway
;
455 clock_interval_to_deadline(1, NSEC_PER_SEC
, &deadline
);
456 LCK_MTX_ASSERT(&inpcb_timeout_lock
, LCK_MTX_ASSERT_OWNED
);
457 if (inpcb_timeout_run
== 0 &&
458 (inpcb_garbage_collecting
|| inpcb_ticking
)) {
459 lck_mtx_convert_spin(&inpcb_timeout_lock
);
462 inpcb_fast_timer_on
= TRUE
;
463 thread_call_enter_delayed(inpcb_thread_call
,
466 inpcb_fast_timer_on
= FALSE
;
467 clock_interval_to_absolutetime_interval(offset
,
468 NSEC_PER_SEC
, &leeway
);
469 thread_call_enter_delayed_with_leeway(
470 inpcb_thread_call
, NULL
, deadline
, leeway
,
471 THREAD_CALL_DELAY_LEEWAY
);
473 } else if (inpcb_timeout_run
== 1 &&
474 offset
== 0 && !inpcb_fast_timer_on
) {
476 * Since the request was for a fast timer but the
477 * scheduled timer is a lazy timer, try to schedule
478 * another instance of fast timer also.
480 lck_mtx_convert_spin(&inpcb_timeout_lock
);
482 inpcb_fast_timer_on
= TRUE
;
483 thread_call_enter_delayed(inpcb_fast_thread_call
, deadline
);
488 inpcb_gc_sched(struct inpcbinfo
*ipi
, u_int32_t type
)
492 lck_mtx_lock_spin(&inpcb_timeout_lock
);
493 inpcb_garbage_collecting
= TRUE
;
494 gccnt
= ipi
->ipi_gc_req
.intimer_nodelay
+
495 ipi
->ipi_gc_req
.intimer_fast
;
497 if (gccnt
> INPCB_GCREQ_THRESHOLD
) {
498 type
= INPCB_TIMER_FAST
;
502 case INPCB_TIMER_NODELAY
:
503 atomic_add_32(&ipi
->ipi_gc_req
.intimer_nodelay
, 1);
504 inpcb_sched_timeout();
506 case INPCB_TIMER_FAST
:
507 atomic_add_32(&ipi
->ipi_gc_req
.intimer_fast
, 1);
508 inpcb_sched_timeout();
511 atomic_add_32(&ipi
->ipi_gc_req
.intimer_lazy
, 1);
512 inpcb_sched_lazy_timeout();
515 lck_mtx_unlock(&inpcb_timeout_lock
);
519 inpcb_timer_sched(struct inpcbinfo
*ipi
, u_int32_t type
)
521 lck_mtx_lock_spin(&inpcb_timeout_lock
);
522 inpcb_ticking
= TRUE
;
524 case INPCB_TIMER_NODELAY
:
525 atomic_add_32(&ipi
->ipi_timer_req
.intimer_nodelay
, 1);
526 inpcb_sched_timeout();
528 case INPCB_TIMER_FAST
:
529 atomic_add_32(&ipi
->ipi_timer_req
.intimer_fast
, 1);
530 inpcb_sched_timeout();
533 atomic_add_32(&ipi
->ipi_timer_req
.intimer_lazy
, 1);
534 inpcb_sched_lazy_timeout();
537 lck_mtx_unlock(&inpcb_timeout_lock
);
541 in_pcbinfo_attach(struct inpcbinfo
*ipi
)
543 struct inpcbinfo
*ipi0
;
545 lck_mtx_lock(&inpcb_lock
);
546 TAILQ_FOREACH(ipi0
, &inpcb_head
, ipi_entry
) {
548 panic("%s: ipi %p already in the list\n",
553 TAILQ_INSERT_TAIL(&inpcb_head
, ipi
, ipi_entry
);
554 lck_mtx_unlock(&inpcb_lock
);
558 in_pcbinfo_detach(struct inpcbinfo
*ipi
)
560 struct inpcbinfo
*ipi0
;
563 lck_mtx_lock(&inpcb_lock
);
564 TAILQ_FOREACH(ipi0
, &inpcb_head
, ipi_entry
) {
570 TAILQ_REMOVE(&inpcb_head
, ipi0
, ipi_entry
);
574 lck_mtx_unlock(&inpcb_lock
);
580 * Allocate a PCB and associate it with the socket.
587 in_pcballoc(struct socket
*so
, struct inpcbinfo
*pcbinfo
, struct proc
*p
)
593 if ((so
->so_flags1
& SOF1_CACHED_IN_SOCK_LAYER
) == 0) {
594 inp
= (struct inpcb
*)zalloc(pcbinfo
->ipi_zone
);
598 bzero((caddr_t
)inp
, sizeof(*inp
));
600 inp
= (struct inpcb
*)(void *)so
->so_saved_pcb
;
601 temp
= inp
->inp_saved_ppcb
;
602 bzero((caddr_t
)inp
, sizeof(*inp
));
603 inp
->inp_saved_ppcb
= temp
;
606 inp
->inp_gencnt
= ++pcbinfo
->ipi_gencnt
;
607 inp
->inp_pcbinfo
= pcbinfo
;
608 inp
->inp_socket
= so
;
609 /* make sure inp_stat is always 64-bit aligned */
610 inp
->inp_stat
= (struct inp_stat
*)P2ROUNDUP(inp
->inp_stat_store
,
612 if (((uintptr_t)inp
->inp_stat
- (uintptr_t)inp
->inp_stat_store
) +
613 sizeof(*inp
->inp_stat
) > sizeof(inp
->inp_stat_store
)) {
614 panic("%s: insufficient space to align inp_stat", __func__
);
618 /* make sure inp_cstat is always 64-bit aligned */
619 inp
->inp_cstat
= (struct inp_stat
*)P2ROUNDUP(inp
->inp_cstat_store
,
621 if (((uintptr_t)inp
->inp_cstat
- (uintptr_t)inp
->inp_cstat_store
) +
622 sizeof(*inp
->inp_cstat
) > sizeof(inp
->inp_cstat_store
)) {
623 panic("%s: insufficient space to align inp_cstat", __func__
);
627 /* make sure inp_wstat is always 64-bit aligned */
628 inp
->inp_wstat
= (struct inp_stat
*)P2ROUNDUP(inp
->inp_wstat_store
,
630 if (((uintptr_t)inp
->inp_wstat
- (uintptr_t)inp
->inp_wstat_store
) +
631 sizeof(*inp
->inp_wstat
) > sizeof(inp
->inp_wstat_store
)) {
632 panic("%s: insufficient space to align inp_wstat", __func__
);
636 /* make sure inp_Wstat is always 64-bit aligned */
637 inp
->inp_Wstat
= (struct inp_stat
*)P2ROUNDUP(inp
->inp_Wstat_store
,
639 if (((uintptr_t)inp
->inp_Wstat
- (uintptr_t)inp
->inp_Wstat_store
) +
640 sizeof(*inp
->inp_Wstat
) > sizeof(inp
->inp_Wstat_store
)) {
641 panic("%s: insufficient space to align inp_Wstat", __func__
);
645 so
->so_pcb
= (caddr_t
)inp
;
647 if (so
->so_proto
->pr_flags
& PR_PCBLOCK
) {
648 lck_mtx_init(&inp
->inpcb_mtx
, pcbinfo
->ipi_lock_grp
,
649 pcbinfo
->ipi_lock_attr
);
652 if (SOCK_DOM(so
) == PF_INET6
&& !ip6_mapped_addr_on
) {
653 inp
->inp_flags
|= IN6P_IPV6_V6ONLY
;
656 if (ip6_auto_flowlabel
) {
657 inp
->inp_flags
|= IN6P_AUTOFLOWLABEL
;
659 if (intcoproc_unrestricted
) {
660 inp
->inp_flags2
|= INP2_INTCOPROC_ALLOWED
;
663 (void) inp_update_policy(inp
);
665 lck_rw_lock_exclusive(pcbinfo
->ipi_lock
);
666 inp
->inp_gencnt
= ++pcbinfo
->ipi_gencnt
;
667 LIST_INSERT_HEAD(pcbinfo
->ipi_listhead
, inp
, inp_list
);
668 pcbinfo
->ipi_count
++;
669 lck_rw_done(pcbinfo
->ipi_lock
);
674 * in_pcblookup_local_and_cleanup does everything
675 * in_pcblookup_local does but it checks for a socket
676 * that's going away. Since we know that the lock is
677 * held read+write when this function is called, we
678 * can safely dispose of this socket like the slow
679 * timer would usually do and return NULL. This is
683 in_pcblookup_local_and_cleanup(struct inpcbinfo
*pcbinfo
, struct in_addr laddr
,
684 u_int lport_arg
, int wild_okay
)
688 /* Perform normal lookup */
689 inp
= in_pcblookup_local(pcbinfo
, laddr
, lport_arg
, wild_okay
);
691 /* Check if we found a match but it's waiting to be disposed */
692 if (inp
!= NULL
&& inp
->inp_wantcnt
== WNT_STOPUSING
) {
693 struct socket
*so
= inp
->inp_socket
;
697 if (so
->so_usecount
== 0) {
698 if (inp
->inp_state
!= INPCB_STATE_DEAD
) {
701 in_pcbdispose(inp
); /* will unlock & destroy */
704 socket_unlock(so
, 0);
712 in_pcb_conflict_post_msg(u_int16_t port
)
715 * Radar 5523020 send a kernel event notification if a
716 * non-participating socket tries to bind the port a socket
717 * who has set SOF_NOTIFYCONFLICT owns.
719 struct kev_msg ev_msg
;
720 struct kev_in_portinuse in_portinuse
;
722 bzero(&in_portinuse
, sizeof(struct kev_in_portinuse
));
723 bzero(&ev_msg
, sizeof(struct kev_msg
));
724 in_portinuse
.port
= ntohs(port
); /* port in host order */
725 in_portinuse
.req_pid
= proc_selfpid();
726 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
727 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
728 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
729 ev_msg
.event_code
= KEV_INET_PORTINUSE
;
730 ev_msg
.dv
[0].data_ptr
= &in_portinuse
;
731 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_portinuse
);
732 ev_msg
.dv
[1].data_length
= 0;
733 dlil_post_complete_msg(NULL
, &ev_msg
);
737 * Bind an INPCB to an address and/or port. This routine should not alter
738 * the caller-supplied local address "nam".
741 * EADDRNOTAVAIL Address not available.
742 * EINVAL Invalid argument
743 * EAFNOSUPPORT Address family not supported [notdef]
744 * EACCES Permission denied
745 * EADDRINUSE Address in use
746 * EAGAIN Resource unavailable, try again
747 * priv_check_cred:EPERM Operation not permitted
750 in_pcbbind(struct inpcb
*inp
, struct sockaddr
*nam
, struct proc
*p
)
752 struct socket
*so
= inp
->inp_socket
;
753 unsigned short *lastport
;
754 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
755 u_short lport
= 0, rand_port
= 0;
756 int wild
= 0, reuseport
= (so
->so_options
& SO_REUSEPORT
);
757 int error
, randomport
, conflict
= 0;
758 boolean_t anonport
= FALSE
;
760 struct in_addr laddr
;
761 struct ifnet
*outif
= NULL
;
763 if (TAILQ_EMPTY(&in_ifaddrhead
)) { /* XXX broken! */
764 return EADDRNOTAVAIL
;
766 if (!(so
->so_options
& (SO_REUSEADDR
| SO_REUSEPORT
))) {
770 bzero(&laddr
, sizeof(laddr
));
772 socket_unlock(so
, 0); /* keep reference on socket */
773 lck_rw_lock_exclusive(pcbinfo
->ipi_lock
);
774 if (inp
->inp_lport
!= 0 || inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
775 /* another thread completed the bind */
776 lck_rw_done(pcbinfo
->ipi_lock
);
782 if (nam
->sa_len
!= sizeof(struct sockaddr_in
)) {
783 lck_rw_done(pcbinfo
->ipi_lock
);
789 * We should check the family, but old programs
790 * incorrectly fail to initialize it.
792 if (nam
->sa_family
!= AF_INET
) {
793 lck_rw_done(pcbinfo
->ipi_lock
);
798 lport
= SIN(nam
)->sin_port
;
800 if (IN_MULTICAST(ntohl(SIN(nam
)->sin_addr
.s_addr
))) {
802 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
803 * allow complete duplication of binding if
804 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
805 * and a multicast address is bound on both
806 * new and duplicated sockets.
808 if (so
->so_options
& SO_REUSEADDR
) {
809 reuseport
= SO_REUSEADDR
| SO_REUSEPORT
;
811 } else if (SIN(nam
)->sin_addr
.s_addr
!= INADDR_ANY
) {
812 struct sockaddr_in sin
;
815 /* Sanitized for interface address searches */
816 bzero(&sin
, sizeof(sin
));
817 sin
.sin_family
= AF_INET
;
818 sin
.sin_len
= sizeof(struct sockaddr_in
);
819 sin
.sin_addr
.s_addr
= SIN(nam
)->sin_addr
.s_addr
;
821 ifa
= ifa_ifwithaddr(SA(&sin
));
823 lck_rw_done(pcbinfo
->ipi_lock
);
825 return EADDRNOTAVAIL
;
828 * Opportunistically determine the outbound
829 * interface that may be used; this may not
830 * hold true if we end up using a route
831 * going over a different interface, e.g.
832 * when sending to a local address. This
833 * will get updated again after sending.
836 outif
= ifa
->ifa_ifp
;
847 #if XNU_TARGET_OS_OSX
848 if (ntohs(lport
) < IPPORT_RESERVED
&&
849 SIN(nam
)->sin_addr
.s_addr
!= 0 &&
850 !(inp
->inp_flags2
& INP2_EXTERNAL_PORT
)) {
851 cred
= kauth_cred_proc_ref(p
);
852 error
= priv_check_cred(cred
,
853 PRIV_NETINET_RESERVEDPORT
, 0);
854 kauth_cred_unref(&cred
);
856 lck_rw_done(pcbinfo
->ipi_lock
);
861 #endif /* XNU_TARGET_OS_OSX */
863 * Check wether the process is allowed to bind to a restricted port
865 if (!current_task_can_use_restricted_in_port(lport
,
866 (uint8_t)so
->so_proto
->pr_protocol
, PORT_FLAGS_BSD
)) {
867 lck_rw_done(pcbinfo
->ipi_lock
);
872 if (!IN_MULTICAST(ntohl(SIN(nam
)->sin_addr
.s_addr
)) &&
873 (u
= kauth_cred_getuid(so
->so_cred
)) != 0 &&
874 (t
= in_pcblookup_local_and_cleanup(
875 inp
->inp_pcbinfo
, SIN(nam
)->sin_addr
, lport
,
876 INPLOOKUP_WILDCARD
)) != NULL
&&
877 (SIN(nam
)->sin_addr
.s_addr
!= INADDR_ANY
||
878 t
->inp_laddr
.s_addr
!= INADDR_ANY
||
879 !(t
->inp_socket
->so_options
& SO_REUSEPORT
)) &&
880 (u
!= kauth_cred_getuid(t
->inp_socket
->so_cred
)) &&
881 !(t
->inp_socket
->so_flags
& SOF_REUSESHAREUID
) &&
882 (SIN(nam
)->sin_addr
.s_addr
!= INADDR_ANY
||
883 t
->inp_laddr
.s_addr
!= INADDR_ANY
) &&
884 (!(t
->inp_flags2
& INP2_EXTERNAL_PORT
) ||
885 !(inp
->inp_flags2
& INP2_EXTERNAL_PORT
) ||
886 uuid_compare(t
->necp_client_uuid
, inp
->necp_client_uuid
) != 0)) {
887 if ((t
->inp_socket
->so_flags
&
888 SOF_NOTIFYCONFLICT
) &&
889 !(so
->so_flags
& SOF_NOTIFYCONFLICT
)) {
893 lck_rw_done(pcbinfo
->ipi_lock
);
896 in_pcb_conflict_post_msg(lport
);
902 t
= in_pcblookup_local_and_cleanup(pcbinfo
,
903 SIN(nam
)->sin_addr
, lport
, wild
);
905 (reuseport
& t
->inp_socket
->so_options
) == 0 &&
906 (!(t
->inp_flags2
& INP2_EXTERNAL_PORT
) ||
907 !(inp
->inp_flags2
& INP2_EXTERNAL_PORT
) ||
908 uuid_compare(t
->necp_client_uuid
, inp
->necp_client_uuid
) != 0)) {
909 if (SIN(nam
)->sin_addr
.s_addr
!= INADDR_ANY
||
910 t
->inp_laddr
.s_addr
!= INADDR_ANY
||
911 SOCK_DOM(so
) != PF_INET6
||
912 SOCK_DOM(t
->inp_socket
) != PF_INET6
) {
913 if ((t
->inp_socket
->so_flags
&
914 SOF_NOTIFYCONFLICT
) &&
915 !(so
->so_flags
& SOF_NOTIFYCONFLICT
)) {
919 lck_rw_done(pcbinfo
->ipi_lock
);
922 in_pcb_conflict_post_msg(lport
);
929 laddr
= SIN(nam
)->sin_addr
;
937 * Override wild = 1 for implicit bind (mainly used by connect)
938 * For implicit bind (lport == 0), we always use an unused port,
939 * so REUSEADDR|REUSEPORT don't apply
943 randomport
= (so
->so_flags
& SOF_BINDRANDOMPORT
) ||
944 (so
->so_type
== SOCK_STREAM
? tcp_use_randomport
:
948 * Even though this looks similar to the code in
949 * in6_pcbsetport, the v6 vs v4 checks are different.
952 if (inp
->inp_flags
& INP_HIGHPORT
) {
953 first
= (u_short
)ipport_hifirstauto
; /* sysctl */
954 last
= (u_short
)ipport_hilastauto
;
955 lastport
= &pcbinfo
->ipi_lasthi
;
956 } else if (inp
->inp_flags
& INP_LOWPORT
) {
957 cred
= kauth_cred_proc_ref(p
);
958 error
= priv_check_cred(cred
,
959 PRIV_NETINET_RESERVEDPORT
, 0);
960 kauth_cred_unref(&cred
);
962 lck_rw_done(pcbinfo
->ipi_lock
);
966 first
= (u_short
)ipport_lowfirstauto
; /* 1023 */
967 last
= (u_short
)ipport_lowlastauto
; /* 600 */
968 lastport
= &pcbinfo
->ipi_lastlow
;
970 first
= (u_short
)ipport_firstauto
; /* sysctl */
971 last
= (u_short
)ipport_lastauto
;
972 lastport
= &pcbinfo
->ipi_lastport
;
974 /* No point in randomizing if only one port is available */
980 * Simple check to ensure all ports are not used up causing
983 * We split the two cases (up and down) so that the direction
984 * is not being tested on each round of the loop.
987 struct in_addr lookup_addr
;
993 read_frandom(&rand_port
, sizeof(rand_port
));
995 first
- (rand_port
% (first
- last
));
997 count
= first
- last
;
999 lookup_addr
= (laddr
.s_addr
!= INADDR_ANY
) ? laddr
:
1004 if (count
-- < 0) { /* completely used? */
1005 lck_rw_done(pcbinfo
->ipi_lock
);
1007 return EADDRNOTAVAIL
;
1010 if (*lastport
> first
|| *lastport
< last
) {
1013 lport
= htons(*lastport
);
1016 * Skip if this is a restricted port as we do not want to
1017 * restricted ports as ephemeral
1019 if (IS_RESTRICTED_IN_PORT(lport
)) {
1023 found
= in_pcblookup_local_and_cleanup(pcbinfo
,
1024 lookup_addr
, lport
, wild
) == NULL
;
1027 struct in_addr lookup_addr
;
1033 read_frandom(&rand_port
, sizeof(rand_port
));
1035 first
+ (rand_port
% (first
- last
));
1037 count
= last
- first
;
1039 lookup_addr
= (laddr
.s_addr
!= INADDR_ANY
) ? laddr
:
1044 if (count
-- < 0) { /* completely used? */
1045 lck_rw_done(pcbinfo
->ipi_lock
);
1047 return EADDRNOTAVAIL
;
1050 if (*lastport
< first
|| *lastport
> last
) {
1053 lport
= htons(*lastport
);
1056 * Skip if this is a restricted port as we do not want to
1057 * restricted ports as ephemeral
1059 if (IS_RESTRICTED_IN_PORT(lport
)) {
1063 found
= in_pcblookup_local_and_cleanup(pcbinfo
,
1064 lookup_addr
, lport
, wild
) == NULL
;
1071 * We unlocked socket's protocol lock for a long time.
1072 * The socket might have been dropped/defuncted.
1073 * Checking if world has changed since.
1075 if (inp
->inp_state
== INPCB_STATE_DEAD
) {
1076 lck_rw_done(pcbinfo
->ipi_lock
);
1077 return ECONNABORTED
;
1080 if (inp
->inp_lport
!= 0 || inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
1081 lck_rw_done(pcbinfo
->ipi_lock
);
1085 if (laddr
.s_addr
!= INADDR_ANY
) {
1086 inp
->inp_laddr
= laddr
;
1087 inp
->inp_last_outifp
= outif
;
1089 inp
->inp_lport
= lport
;
1091 inp
->inp_flags
|= INP_ANONPORT
;
1094 if (in_pcbinshash(inp
, 1) != 0) {
1095 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
1096 inp
->inp_last_outifp
= NULL
;
1100 inp
->inp_flags
&= ~INP_ANONPORT
;
1102 lck_rw_done(pcbinfo
->ipi_lock
);
1105 lck_rw_done(pcbinfo
->ipi_lock
);
1106 sflt_notify(so
, sock_evt_bound
, NULL
);
1110 #define APN_FALLBACK_IP_FILTER(a) \
1111 (IN_LINKLOCAL(ntohl((a)->sin_addr.s_addr)) || \
1112 IN_LOOPBACK(ntohl((a)->sin_addr.s_addr)) || \
1113 IN_ZERONET(ntohl((a)->sin_addr.s_addr)) || \
1114 IN_MULTICAST(ntohl((a)->sin_addr.s_addr)) || \
1115 IN_PRIVATE(ntohl((a)->sin_addr.s_addr)))
1117 #define APN_FALLBACK_NOTIF_INTERVAL 2 /* Magic Number */
1118 static uint64_t last_apn_fallback
= 0;
1121 apn_fallback_required(proc_t proc
, struct socket
*so
, struct sockaddr_in
*p_dstv4
)
1124 struct sockaddr_storage lookup_default_addr
;
1125 struct rtentry
*rt
= NULL
;
1127 VERIFY(proc
!= NULL
);
1129 if (apn_fallbk_enabled
== FALSE
) {
1133 if (proc
== kernproc
) {
1137 if (so
&& (so
->so_options
& SO_NOAPNFALLBK
)) {
1141 timenow
= net_uptime();
1142 if ((timenow
- last_apn_fallback
) < APN_FALLBACK_NOTIF_INTERVAL
) {
1143 apn_fallbk_log((LOG_INFO
, "APN fallback notification throttled.\n"));
1147 if (p_dstv4
&& APN_FALLBACK_IP_FILTER(p_dstv4
)) {
1151 /* Check if we have unscoped IPv6 default route through cellular */
1152 bzero(&lookup_default_addr
, sizeof(lookup_default_addr
));
1153 lookup_default_addr
.ss_family
= AF_INET6
;
1154 lookup_default_addr
.ss_len
= sizeof(struct sockaddr_in6
);
1156 rt
= rtalloc1((struct sockaddr
*)&lookup_default_addr
, 0, 0);
1158 apn_fallbk_log((LOG_INFO
, "APN fallback notification could not find "
1159 "unscoped default IPv6 route.\n"));
1163 if (!IFNET_IS_CELLULAR(rt
->rt_ifp
)) {
1165 apn_fallbk_log((LOG_INFO
, "APN fallback notification could not find "
1166 "unscoped default IPv6 route through cellular interface.\n"));
1171 * We have a default IPv6 route, ensure that
1172 * we do not have IPv4 default route before triggering
1178 bzero(&lookup_default_addr
, sizeof(lookup_default_addr
));
1179 lookup_default_addr
.ss_family
= AF_INET
;
1180 lookup_default_addr
.ss_len
= sizeof(struct sockaddr_in
);
1182 rt
= rtalloc1((struct sockaddr
*)&lookup_default_addr
, 0, 0);
1187 apn_fallbk_log((LOG_INFO
, "APN fallback notification found unscoped "
1188 "IPv4 default route!\n"));
1194 * We disable APN fallback if the binary is not a third-party app.
1195 * Note that platform daemons use their process name as a
1196 * bundle ID so we filter out bundle IDs without dots.
1198 const char *bundle_id
= cs_identity_get(proc
);
1199 if (bundle_id
== NULL
||
1200 bundle_id
[0] == '\0' ||
1201 strchr(bundle_id
, '.') == NULL
||
1202 strncmp(bundle_id
, "com.apple.", sizeof("com.apple.") - 1) == 0) {
1203 apn_fallbk_log((LOG_INFO
, "Abort: APN fallback notification found first-"
1204 "party bundle ID \"%s\"!\n", (bundle_id
? bundle_id
: "NULL")));
1211 * The Apple App Store IPv6 requirement started on
1212 * June 1st, 2016 at 12:00:00 AM PDT.
1213 * We disable APN fallback if the binary is more recent than that.
1214 * We check both atime and birthtime since birthtime is not always supported.
1216 static const long ipv6_start_date
= 1464764400L;
1217 vfs_context_t context
;
1221 bzero(&sb
, sizeof(struct stat64
));
1222 context
= vfs_context_create(NULL
);
1223 vn_stat_error
= vn_stat(proc
->p_textvp
, &sb
, NULL
, 1, 0, context
);
1224 (void)vfs_context_rele(context
);
1226 if (vn_stat_error
!= 0 ||
1227 sb
.st_atimespec
.tv_sec
>= ipv6_start_date
||
1228 sb
.st_birthtimespec
.tv_sec
>= ipv6_start_date
) {
1229 apn_fallbk_log((LOG_INFO
, "Abort: APN fallback notification found binary "
1230 "too recent! (err %d atime %ld mtime %ld ctime %ld birthtime %ld)\n",
1231 vn_stat_error
, sb
.st_atimespec
.tv_sec
, sb
.st_mtimespec
.tv_sec
,
1232 sb
.st_ctimespec
.tv_sec
, sb
.st_birthtimespec
.tv_sec
));
1240 apn_fallback_trigger(proc_t proc
, struct socket
*so
)
1243 struct kev_msg ev_msg
;
1244 struct kev_netevent_apnfallbk_data apnfallbk_data
;
1246 last_apn_fallback
= net_uptime();
1247 pid
= proc_pid(proc
);
1248 uuid_t application_uuid
;
1249 uuid_clear(application_uuid
);
1250 proc_getexecutableuuid(proc
, application_uuid
,
1251 sizeof(application_uuid
));
1253 bzero(&ev_msg
, sizeof(struct kev_msg
));
1254 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1255 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1256 ev_msg
.kev_subclass
= KEV_NETEVENT_SUBCLASS
;
1257 ev_msg
.event_code
= KEV_NETEVENT_APNFALLBACK
;
1259 bzero(&apnfallbk_data
, sizeof(apnfallbk_data
));
1261 if (so
->so_flags
& SOF_DELEGATED
) {
1262 apnfallbk_data
.epid
= so
->e_pid
;
1263 uuid_copy(apnfallbk_data
.euuid
, so
->e_uuid
);
1265 apnfallbk_data
.epid
= so
->last_pid
;
1266 uuid_copy(apnfallbk_data
.euuid
, so
->last_uuid
);
1269 ev_msg
.dv
[0].data_ptr
= &apnfallbk_data
;
1270 ev_msg
.dv
[0].data_length
= sizeof(apnfallbk_data
);
1271 kev_post_msg(&ev_msg
);
1272 apn_fallbk_log((LOG_INFO
, "APN fallback notification issued.\n"));
1276 * Transform old in_pcbconnect() into an inner subroutine for new
1277 * in_pcbconnect(); do some validity-checking on the remote address
1278 * (in "nam") and then determine local host address (i.e., which
1279 * interface) to use to access that remote host.
1281 * This routine may alter the caller-supplied remote address "nam".
1283 * The caller may override the bound-to-interface setting of the socket
1284 * by specifying the ifscope parameter (e.g. from IP_PKTINFO.)
1286 * This routine might return an ifp with a reference held if the caller
1287 * provides a non-NULL outif, even in the error case. The caller is
1288 * responsible for releasing its reference.
1290 * Returns: 0 Success
1291 * EINVAL Invalid argument
1292 * EAFNOSUPPORT Address family not supported
1293 * EADDRNOTAVAIL Address not available
1296 in_pcbladdr(struct inpcb
*inp
, struct sockaddr
*nam
, struct in_addr
*laddr
,
1297 unsigned int ifscope
, struct ifnet
**outif
, int raw
)
1299 struct route
*ro
= &inp
->inp_route
;
1300 struct in_ifaddr
*ia
= NULL
;
1301 struct sockaddr_in sin
;
1303 boolean_t restricted
= FALSE
;
1305 if (outif
!= NULL
) {
1308 if (nam
->sa_len
!= sizeof(struct sockaddr_in
)) {
1311 if (SIN(nam
)->sin_family
!= AF_INET
) {
1312 return EAFNOSUPPORT
;
1314 if (raw
== 0 && SIN(nam
)->sin_port
== 0) {
1315 return EADDRNOTAVAIL
;
1319 * If the destination address is INADDR_ANY,
1320 * use the primary local address.
1321 * If the supplied address is INADDR_BROADCAST,
1322 * and the primary interface supports broadcast,
1323 * choose the broadcast address for that interface.
1325 if (raw
== 0 && (SIN(nam
)->sin_addr
.s_addr
== INADDR_ANY
||
1326 SIN(nam
)->sin_addr
.s_addr
== (u_int32_t
)INADDR_BROADCAST
)) {
1327 lck_rw_lock_shared(in_ifaddr_rwlock
);
1328 if (!TAILQ_EMPTY(&in_ifaddrhead
)) {
1329 ia
= TAILQ_FIRST(&in_ifaddrhead
);
1330 IFA_LOCK_SPIN(&ia
->ia_ifa
);
1331 if (SIN(nam
)->sin_addr
.s_addr
== INADDR_ANY
) {
1332 SIN(nam
)->sin_addr
= IA_SIN(ia
)->sin_addr
;
1333 } else if (ia
->ia_ifp
->if_flags
& IFF_BROADCAST
) {
1334 SIN(nam
)->sin_addr
=
1335 SIN(&ia
->ia_broadaddr
)->sin_addr
;
1337 IFA_UNLOCK(&ia
->ia_ifa
);
1340 lck_rw_done(in_ifaddr_rwlock
);
1343 * Otherwise, if the socket has already bound the source, just use it.
1345 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
1347 *laddr
= inp
->inp_laddr
;
1352 * If the ifscope is specified by the caller (e.g. IP_PKTINFO)
1353 * then it overrides the sticky ifscope set for the socket.
1355 if (ifscope
== IFSCOPE_NONE
&& (inp
->inp_flags
& INP_BOUND_IF
)) {
1356 ifscope
= inp
->inp_boundifp
->if_index
;
1360 * If route is known or can be allocated now,
1361 * our src addr is taken from the i/f, else punt.
1362 * Note that we should check the address family of the cached
1363 * destination, in case of sharing the cache with IPv6.
1365 if (ro
->ro_rt
!= NULL
) {
1366 RT_LOCK_SPIN(ro
->ro_rt
);
1368 if (ROUTE_UNUSABLE(ro
) || ro
->ro_dst
.sa_family
!= AF_INET
||
1369 SIN(&ro
->ro_dst
)->sin_addr
.s_addr
!= SIN(nam
)->sin_addr
.s_addr
||
1370 (inp
->inp_socket
->so_options
& SO_DONTROUTE
)) {
1371 if (ro
->ro_rt
!= NULL
) {
1372 RT_UNLOCK(ro
->ro_rt
);
1376 if (!(inp
->inp_socket
->so_options
& SO_DONTROUTE
) &&
1377 (ro
->ro_rt
== NULL
|| ro
->ro_rt
->rt_ifp
== NULL
)) {
1378 if (ro
->ro_rt
!= NULL
) {
1379 RT_UNLOCK(ro
->ro_rt
);
1382 /* No route yet, so try to acquire one */
1383 bzero(&ro
->ro_dst
, sizeof(struct sockaddr_in
));
1384 ro
->ro_dst
.sa_family
= AF_INET
;
1385 ro
->ro_dst
.sa_len
= sizeof(struct sockaddr_in
);
1386 SIN(&ro
->ro_dst
)->sin_addr
= SIN(nam
)->sin_addr
;
1387 rtalloc_scoped(ro
, ifscope
);
1388 if (ro
->ro_rt
!= NULL
) {
1389 RT_LOCK_SPIN(ro
->ro_rt
);
1392 /* Sanitized local copy for interface address searches */
1393 bzero(&sin
, sizeof(sin
));
1394 sin
.sin_family
= AF_INET
;
1395 sin
.sin_len
= sizeof(struct sockaddr_in
);
1396 sin
.sin_addr
.s_addr
= SIN(nam
)->sin_addr
.s_addr
;
1398 * If we did not find (or use) a route, assume dest is reachable
1399 * on a directly connected network and try to find a corresponding
1400 * interface to take the source address from.
1402 if (ro
->ro_rt
== NULL
) {
1403 proc_t proc
= current_proc();
1406 ia
= ifatoia(ifa_ifwithdstaddr(SA(&sin
)));
1408 ia
= ifatoia(ifa_ifwithnet_scoped(SA(&sin
), ifscope
));
1410 error
= ((ia
== NULL
) ? ENETUNREACH
: 0);
1412 if (apn_fallback_required(proc
, inp
->inp_socket
,
1414 apn_fallback_trigger(proc
, inp
->inp_socket
);
1419 RT_LOCK_ASSERT_HELD(ro
->ro_rt
);
1421 * If the outgoing interface on the route found is not
1422 * a loopback interface, use the address from that interface.
1424 if (!(ro
->ro_rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
)) {
1427 * If the route points to a cellular interface and the
1428 * caller forbids our using interfaces of such type,
1429 * pretend that there is no route.
1430 * Apply the same logic for expensive interfaces.
1432 if (inp_restricted_send(inp
, ro
->ro_rt
->rt_ifp
)) {
1433 RT_UNLOCK(ro
->ro_rt
);
1435 error
= EHOSTUNREACH
;
1438 /* Become a regular mutex */
1439 RT_CONVERT_LOCK(ro
->ro_rt
);
1440 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
1441 IFA_ADDREF(&ia
->ia_ifa
);
1444 * Mark the control block for notification of
1445 * a possible flow that might undergo clat46
1448 * We defer the decision to a later point when
1449 * inpcb is being disposed off.
1450 * The reason is that we only want to send notification
1451 * if the flow was ever used to send data.
1453 if (IS_INTF_CLAT46(ro
->ro_rt
->rt_ifp
)) {
1454 inp
->inp_flags2
|= INP2_CLAT46_FLOW
;
1457 RT_UNLOCK(ro
->ro_rt
);
1462 VERIFY(ro
->ro_rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
);
1463 RT_UNLOCK(ro
->ro_rt
);
1465 * The outgoing interface is marked with 'loopback net', so a route
1466 * to ourselves is here.
1467 * Try to find the interface of the destination address and then
1468 * take the address from there. That interface is not necessarily
1469 * a loopback interface.
1472 ia
= ifatoia(ifa_ifwithdstaddr(SA(&sin
)));
1474 ia
= ifatoia(ifa_ifwithaddr_scoped(SA(&sin
), ifscope
));
1477 ia
= ifatoia(ifa_ifwithnet_scoped(SA(&sin
), ifscope
));
1481 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
1483 IFA_ADDREF(&ia
->ia_ifa
);
1485 RT_UNLOCK(ro
->ro_rt
);
1487 error
= ((ia
== NULL
) ? ENETUNREACH
: 0);
1491 * If the destination address is multicast and an outgoing
1492 * interface has been set as a multicast option, use the
1493 * address of that interface as our source address.
1495 if (IN_MULTICAST(ntohl(SIN(nam
)->sin_addr
.s_addr
)) &&
1496 inp
->inp_moptions
!= NULL
) {
1497 struct ip_moptions
*imo
;
1500 imo
= inp
->inp_moptions
;
1502 if (imo
->imo_multicast_ifp
!= NULL
&& (ia
== NULL
||
1503 ia
->ia_ifp
!= imo
->imo_multicast_ifp
)) {
1504 ifp
= imo
->imo_multicast_ifp
;
1506 IFA_REMREF(&ia
->ia_ifa
);
1508 lck_rw_lock_shared(in_ifaddr_rwlock
);
1509 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
) {
1510 if (ia
->ia_ifp
== ifp
) {
1515 IFA_ADDREF(&ia
->ia_ifa
);
1517 lck_rw_done(in_ifaddr_rwlock
);
1519 error
= EADDRNOTAVAIL
;
1527 * Don't do pcblookup call here; return interface in laddr
1528 * and exit to caller, that will do the lookup.
1532 * If the source address belongs to a cellular interface
1533 * and the socket forbids our using interfaces of such
1534 * type, pretend that there is no source address.
1535 * Apply the same logic for expensive interfaces.
1537 IFA_LOCK_SPIN(&ia
->ia_ifa
);
1538 if (inp_restricted_send(inp
, ia
->ia_ifa
.ifa_ifp
)) {
1539 IFA_UNLOCK(&ia
->ia_ifa
);
1540 error
= EHOSTUNREACH
;
1542 } else if (error
== 0) {
1543 *laddr
= ia
->ia_addr
.sin_addr
;
1544 if (outif
!= NULL
) {
1547 if (ro
->ro_rt
!= NULL
) {
1548 ifp
= ro
->ro_rt
->rt_ifp
;
1553 VERIFY(ifp
!= NULL
);
1554 IFA_CONVERT_LOCK(&ia
->ia_ifa
);
1555 ifnet_reference(ifp
); /* for caller */
1556 if (*outif
!= NULL
) {
1557 ifnet_release(*outif
);
1561 IFA_UNLOCK(&ia
->ia_ifa
);
1563 IFA_UNLOCK(&ia
->ia_ifa
);
1565 IFA_REMREF(&ia
->ia_ifa
);
1569 if (restricted
&& error
== EHOSTUNREACH
) {
1570 soevent(inp
->inp_socket
, (SO_FILT_HINT_LOCKED
|
1571 SO_FILT_HINT_IFDENIED
));
1579 * Connect from a socket to a specified address.
1580 * Both address and port must be specified in argument sin.
1581 * If don't have a local address for this socket yet,
1584 * The caller may override the bound-to-interface setting of the socket
1585 * by specifying the ifscope parameter (e.g. from IP_PKTINFO.)
1588 in_pcbconnect(struct inpcb
*inp
, struct sockaddr
*nam
, struct proc
*p
,
1589 unsigned int ifscope
, struct ifnet
**outif
)
1591 struct in_addr laddr
;
1592 struct sockaddr_in
*sin
= (struct sockaddr_in
*)(void *)nam
;
1595 struct socket
*so
= inp
->inp_socket
;
1599 so
->so_state_change_cnt
++;
1604 * Call inner routine, to assign local interface address.
1606 if ((error
= in_pcbladdr(inp
, nam
, &laddr
, ifscope
, outif
, 0)) != 0) {
1610 socket_unlock(so
, 0);
1611 pcb
= in_pcblookup_hash(inp
->inp_pcbinfo
, sin
->sin_addr
, sin
->sin_port
,
1612 inp
->inp_laddr
.s_addr
? inp
->inp_laddr
: laddr
,
1613 inp
->inp_lport
, 0, NULL
);
1617 * Check if the socket is still in a valid state. When we unlock this
1618 * embryonic socket, it can get aborted if another thread is closing
1619 * the listener (radar 7947600).
1621 if ((so
->so_flags
& SOF_ABORTED
) != 0) {
1622 return ECONNREFUSED
;
1626 in_pcb_checkstate(pcb
, WNT_RELEASE
, pcb
== inp
? 1 : 0);
1629 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
1630 if (inp
->inp_lport
== 0) {
1631 error
= in_pcbbind(inp
, NULL
, p
);
1636 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
1638 * Lock inversion issue, mostly with udp
1639 * multicast packets.
1641 socket_unlock(so
, 0);
1642 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
1645 inp
->inp_laddr
= laddr
;
1646 /* no reference needed */
1647 inp
->inp_last_outifp
= (outif
!= NULL
) ? *outif
: NULL
;
1648 inp
->inp_flags
|= INP_INADDR_ANY
;
1651 * Usage of IP_PKTINFO, without local port already
1652 * speficified will cause kernel to panic,
1653 * see rdar://problem/18508185.
1654 * For now returning error to avoid a kernel panic
1655 * This routines can be refactored and handle this better
1658 if (inp
->inp_lport
== 0) {
1661 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
1663 * Lock inversion issue, mostly with udp
1664 * multicast packets.
1666 socket_unlock(so
, 0);
1667 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
1671 inp
->inp_faddr
= sin
->sin_addr
;
1672 inp
->inp_fport
= sin
->sin_port
;
1673 if (nstat_collect
&& SOCK_PROTO(so
) == IPPROTO_UDP
) {
1674 nstat_pcb_invalidate_cache(inp
);
1677 lck_rw_done(inp
->inp_pcbinfo
->ipi_lock
);
1682 in_pcbdisconnect(struct inpcb
*inp
)
1684 struct socket
*so
= inp
->inp_socket
;
1686 if (nstat_collect
&& SOCK_PROTO(so
) == IPPROTO_UDP
) {
1687 nstat_pcb_cache(inp
);
1690 inp
->inp_faddr
.s_addr
= INADDR_ANY
;
1695 so
->so_state_change_cnt
++;
1699 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
1700 /* lock inversion issue, mostly with udp multicast packets */
1701 socket_unlock(so
, 0);
1702 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
1707 lck_rw_done(inp
->inp_pcbinfo
->ipi_lock
);
1709 * A multipath subflow socket would have its SS_NOFDREF set by default,
1710 * so check for SOF_MP_SUBFLOW socket flag before detaching the PCB;
1711 * when the socket is closed for real, SOF_MP_SUBFLOW would be cleared.
1713 if (!(so
->so_flags
& SOF_MP_SUBFLOW
) && (so
->so_state
& SS_NOFDREF
)) {
1719 in_pcbdetach(struct inpcb
*inp
)
1721 struct socket
*so
= inp
->inp_socket
;
1723 if (so
->so_pcb
== NULL
) {
1724 /* PCB has been disposed */
1725 panic("%s: inp=%p so=%p proto=%d so_pcb is null!\n", __func__
,
1726 inp
, so
, SOCK_PROTO(so
));
1731 if (inp
->inp_sp
!= NULL
) {
1732 (void) ipsec4_delete_pcbpolicy(inp
);
1736 if (inp
->inp_stat
!= NULL
&& SOCK_PROTO(so
) == IPPROTO_UDP
) {
1737 if (inp
->inp_stat
->rxpackets
== 0 && inp
->inp_stat
->txpackets
== 0) {
1738 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_inet_dgram_no_data
);
1743 * Let NetworkStatistics know this PCB is going away
1744 * before we detach it.
1746 if (nstat_collect
&&
1747 (SOCK_PROTO(so
) == IPPROTO_TCP
|| SOCK_PROTO(so
) == IPPROTO_UDP
)) {
1748 nstat_pcb_detach(inp
);
1751 /* Free memory buffer held for generating keep alives */
1752 if (inp
->inp_keepalive_data
!= NULL
) {
1753 FREE(inp
->inp_keepalive_data
, M_TEMP
);
1754 inp
->inp_keepalive_data
= NULL
;
1757 /* mark socket state as dead */
1758 if (in_pcb_checkstate(inp
, WNT_STOPUSING
, 1) != WNT_STOPUSING
) {
1759 panic("%s: so=%p proto=%d couldn't set to STOPUSING\n",
1760 __func__
, so
, SOCK_PROTO(so
));
1764 if (!(so
->so_flags
& SOF_PCBCLEARING
)) {
1765 struct ip_moptions
*imo
;
1768 if (inp
->inp_options
!= NULL
) {
1769 (void) m_free(inp
->inp_options
);
1770 inp
->inp_options
= NULL
;
1772 ROUTE_RELEASE(&inp
->inp_route
);
1773 imo
= inp
->inp_moptions
;
1774 inp
->inp_moptions
= NULL
;
1775 sofreelastref(so
, 0);
1776 inp
->inp_state
= INPCB_STATE_DEAD
;
1779 * Enqueue an event to send kernel event notification
1780 * if the flow has to CLAT46 for data packets
1782 if (inp
->inp_flags2
& INP2_CLAT46_FLOW
) {
1784 * If there has been any exchange of data bytes
1786 * Schedule a notification to report that flow is
1787 * using client side translation.
1789 if (inp
->inp_stat
!= NULL
&&
1790 (inp
->inp_stat
->txbytes
!= 0 ||
1791 inp
->inp_stat
->rxbytes
!= 0)) {
1792 if (so
->so_flags
& SOF_DELEGATED
) {
1793 in6_clat46_event_enqueue_nwk_wq_entry(
1794 IN6_CLAT46_EVENT_V4_FLOW
,
1798 in6_clat46_event_enqueue_nwk_wq_entry(
1799 IN6_CLAT46_EVENT_V4_FLOW
,
1806 /* makes sure we're not called twice from so_close */
1807 so
->so_flags
|= SOF_PCBCLEARING
;
1809 inpcb_gc_sched(inp
->inp_pcbinfo
, INPCB_TIMER_FAST
);
1812 * See inp_join_group() for why we need to unlock
1815 socket_unlock(so
, 0);
1824 in_pcbdispose(struct inpcb
*inp
)
1826 struct socket
*so
= inp
->inp_socket
;
1827 struct inpcbinfo
*ipi
= inp
->inp_pcbinfo
;
1829 if (so
!= NULL
&& so
->so_usecount
!= 0) {
1830 panic("%s: so %p [%d,%d] usecount %d lockhistory %s\n",
1831 __func__
, so
, SOCK_DOM(so
), SOCK_TYPE(so
), so
->so_usecount
,
1832 solockhistory_nr(so
));
1834 } else if (inp
->inp_wantcnt
!= WNT_STOPUSING
) {
1836 panic_plain("%s: inp %p invalid wantcnt %d, so %p "
1837 "[%d,%d] usecount %d retaincnt %d state 0x%x "
1838 "flags 0x%x lockhistory %s\n", __func__
, inp
,
1839 inp
->inp_wantcnt
, so
, SOCK_DOM(so
), SOCK_TYPE(so
),
1840 so
->so_usecount
, so
->so_retaincnt
, so
->so_state
,
1841 so
->so_flags
, solockhistory_nr(so
));
1844 panic("%s: inp %p invalid wantcnt %d no socket\n",
1845 __func__
, inp
, inp
->inp_wantcnt
);
1850 LCK_RW_ASSERT(ipi
->ipi_lock
, LCK_RW_ASSERT_EXCLUSIVE
);
1852 inp
->inp_gencnt
= ++ipi
->ipi_gencnt
;
1853 /* access ipi in in_pcbremlists */
1854 in_pcbremlists(inp
);
1857 if (so
->so_proto
->pr_flags
& PR_PCBLOCK
) {
1858 sofreelastref(so
, 0);
1859 if (so
->so_rcv
.sb_cc
> 0 || so
->so_snd
.sb_cc
> 0) {
1861 * selthreadclear() already called
1862 * during sofreelastref() above.
1864 sbrelease(&so
->so_rcv
);
1865 sbrelease(&so
->so_snd
);
1867 if (so
->so_head
!= NULL
) {
1868 panic("%s: so=%p head still exist\n",
1872 lck_mtx_unlock(&inp
->inpcb_mtx
);
1875 necp_inpcb_remove_cb(inp
);
1878 lck_mtx_destroy(&inp
->inpcb_mtx
, ipi
->ipi_lock_grp
);
1880 /* makes sure we're not called twice from so_close */
1881 so
->so_flags
|= SOF_PCBCLEARING
;
1882 so
->so_saved_pcb
= (caddr_t
)inp
;
1884 inp
->inp_socket
= NULL
;
1886 necp_inpcb_dispose(inp
);
1889 * In case there a route cached after a detach (possible
1890 * in the tcp case), make sure that it is freed before
1891 * we deallocate the structure.
1893 ROUTE_RELEASE(&inp
->inp_route
);
1894 if ((so
->so_flags1
& SOF1_CACHED_IN_SOCK_LAYER
) == 0) {
1895 zfree(ipi
->ipi_zone
, inp
);
1902 * The calling convention of in_getsockaddr() and in_getpeeraddr() was
1903 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
1904 * in struct pr_usrreqs, so that protocols can just reference then directly
1905 * without the need for a wrapper function.
1908 in_getsockaddr(struct socket
*so
, struct sockaddr
**nam
)
1911 struct sockaddr_in
*sin
;
1914 * Do the malloc first in case it blocks.
1916 MALLOC(sin
, struct sockaddr_in
*, sizeof(*sin
), M_SONAME
, M_WAITOK
);
1920 bzero(sin
, sizeof(*sin
));
1921 sin
->sin_family
= AF_INET
;
1922 sin
->sin_len
= sizeof(*sin
);
1924 if ((inp
= sotoinpcb(so
)) == NULL
) {
1925 FREE(sin
, M_SONAME
);
1928 sin
->sin_port
= inp
->inp_lport
;
1929 sin
->sin_addr
= inp
->inp_laddr
;
1931 *nam
= (struct sockaddr
*)sin
;
1936 in_getsockaddr_s(struct socket
*so
, struct sockaddr_in
*ss
)
1938 struct sockaddr_in
*sin
= ss
;
1942 bzero(ss
, sizeof(*ss
));
1944 sin
->sin_family
= AF_INET
;
1945 sin
->sin_len
= sizeof(*sin
);
1947 if ((inp
= sotoinpcb(so
)) == NULL
) {
1951 sin
->sin_port
= inp
->inp_lport
;
1952 sin
->sin_addr
= inp
->inp_laddr
;
1957 in_getpeeraddr(struct socket
*so
, struct sockaddr
**nam
)
1960 struct sockaddr_in
*sin
;
1963 * Do the malloc first in case it blocks.
1965 MALLOC(sin
, struct sockaddr_in
*, sizeof(*sin
), M_SONAME
, M_WAITOK
);
1969 bzero((caddr_t
)sin
, sizeof(*sin
));
1970 sin
->sin_family
= AF_INET
;
1971 sin
->sin_len
= sizeof(*sin
);
1973 if ((inp
= sotoinpcb(so
)) == NULL
) {
1974 FREE(sin
, M_SONAME
);
1977 sin
->sin_port
= inp
->inp_fport
;
1978 sin
->sin_addr
= inp
->inp_faddr
;
1980 *nam
= (struct sockaddr
*)sin
;
1985 in_pcbnotifyall(struct inpcbinfo
*pcbinfo
, struct in_addr faddr
,
1986 int errno
, void (*notify
)(struct inpcb
*, int))
1990 lck_rw_lock_shared(pcbinfo
->ipi_lock
);
1992 LIST_FOREACH(inp
, pcbinfo
->ipi_listhead
, inp_list
) {
1993 if (!(inp
->inp_vflag
& INP_IPV4
)) {
1996 if (inp
->inp_faddr
.s_addr
!= faddr
.s_addr
||
1997 inp
->inp_socket
== NULL
) {
2000 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
) {
2003 socket_lock(inp
->inp_socket
, 1);
2004 (*notify
)(inp
, errno
);
2005 (void) in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
2006 socket_unlock(inp
->inp_socket
, 1);
2008 lck_rw_done(pcbinfo
->ipi_lock
);
2012 * Check for alternatives when higher level complains
2013 * about service problems. For now, invalidate cached
2014 * routing information. If the route was created dynamically
2015 * (by a redirect), time to try a default gateway again.
2018 in_losing(struct inpcb
*inp
)
2020 boolean_t release
= FALSE
;
2023 if ((rt
= inp
->inp_route
.ro_rt
) != NULL
) {
2024 struct in_ifaddr
*ia
= NULL
;
2027 if (rt
->rt_flags
& RTF_DYNAMIC
) {
2029 * Prevent another thread from modifying rt_key,
2030 * rt_gateway via rt_setgate() after rt_lock is
2031 * dropped by marking the route as defunct.
2033 rt
->rt_flags
|= RTF_CONDEMNED
;
2035 (void) rtrequest(RTM_DELETE
, rt_key(rt
),
2036 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, NULL
);
2040 /* if the address is gone keep the old route in the pcb */
2041 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
&&
2042 (ia
= ifa_foraddr(inp
->inp_laddr
.s_addr
)) != NULL
) {
2044 * Address is around; ditch the route. A new route
2045 * can be allocated the next time output is attempted.
2050 IFA_REMREF(&ia
->ia_ifa
);
2053 if (rt
== NULL
|| release
) {
2054 ROUTE_RELEASE(&inp
->inp_route
);
2059 * After a routing change, flush old routing
2060 * and allocate a (hopefully) better one.
2063 in_rtchange(struct inpcb
*inp
, int errno
)
2065 #pragma unused(errno)
2066 boolean_t release
= FALSE
;
2069 if ((rt
= inp
->inp_route
.ro_rt
) != NULL
) {
2070 struct in_ifaddr
*ia
= NULL
;
2072 /* if address is gone, keep the old route */
2073 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
&&
2074 (ia
= ifa_foraddr(inp
->inp_laddr
.s_addr
)) != NULL
) {
2076 * Address is around; ditch the route. A new route
2077 * can be allocated the next time output is attempted.
2082 IFA_REMREF(&ia
->ia_ifa
);
2085 if (rt
== NULL
|| release
) {
2086 ROUTE_RELEASE(&inp
->inp_route
);
2091 * Lookup a PCB based on the local address and port.
2094 in_pcblookup_local(struct inpcbinfo
*pcbinfo
, struct in_addr laddr
,
2095 unsigned int lport_arg
, int wild_okay
)
2098 int matchwild
= 3, wildcard
;
2099 u_short lport
= (u_short
)lport_arg
;
2101 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_START
, 0, 0, 0, 0, 0);
2104 struct inpcbhead
*head
;
2106 * Look for an unconnected (wildcard foreign addr) PCB that
2107 * matches the local address and port we're looking for.
2109 head
= &pcbinfo
->ipi_hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
2110 pcbinfo
->ipi_hashmask
)];
2111 LIST_FOREACH(inp
, head
, inp_hash
) {
2112 if (!(inp
->inp_vflag
& INP_IPV4
)) {
2115 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
&&
2116 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
2117 inp
->inp_lport
== lport
) {
2127 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
2130 struct inpcbporthead
*porthash
;
2131 struct inpcbport
*phd
;
2132 struct inpcb
*match
= NULL
;
2134 * Best fit PCB lookup.
2136 * First see if this local port is in use by looking on the
2139 porthash
= &pcbinfo
->ipi_porthashbase
[INP_PCBPORTHASH(lport
,
2140 pcbinfo
->ipi_porthashmask
)];
2141 LIST_FOREACH(phd
, porthash
, phd_hash
) {
2142 if (phd
->phd_port
== lport
) {
2148 * Port is in use by one or more PCBs. Look for best
2151 LIST_FOREACH(inp
, &phd
->phd_pcblist
, inp_portlist
) {
2153 if (!(inp
->inp_vflag
& INP_IPV4
)) {
2156 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
2159 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
2160 if (laddr
.s_addr
== INADDR_ANY
) {
2162 } else if (inp
->inp_laddr
.s_addr
!=
2167 if (laddr
.s_addr
!= INADDR_ANY
) {
2171 if (wildcard
< matchwild
) {
2173 matchwild
= wildcard
;
2174 if (matchwild
== 0) {
2180 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_END
, match
,
2187 * Check if PCB exists in hash list.
2190 in_pcblookup_hash_exists(struct inpcbinfo
*pcbinfo
, struct in_addr faddr
,
2191 u_int fport_arg
, struct in_addr laddr
, u_int lport_arg
, int wildcard
,
2192 uid_t
*uid
, gid_t
*gid
, struct ifnet
*ifp
)
2194 struct inpcbhead
*head
;
2196 u_short fport
= (u_short
)fport_arg
, lport
= (u_short
)lport_arg
;
2198 struct inpcb
*local_wild
= NULL
;
2199 struct inpcb
*local_wild_mapped
= NULL
;
2205 * We may have found the pcb in the last lookup - check this first.
2208 lck_rw_lock_shared(pcbinfo
->ipi_lock
);
2211 * First look for an exact match.
2213 head
= &pcbinfo
->ipi_hashbase
[INP_PCBHASH(faddr
.s_addr
, lport
, fport
,
2214 pcbinfo
->ipi_hashmask
)];
2215 LIST_FOREACH(inp
, head
, inp_hash
) {
2216 if (!(inp
->inp_vflag
& INP_IPV4
)) {
2219 if (inp_restricted_recv(inp
, ifp
)) {
2224 if (!necp_socket_is_allowed_to_recv_on_interface(inp
, ifp
)) {
2229 if (inp
->inp_faddr
.s_addr
== faddr
.s_addr
&&
2230 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
2231 inp
->inp_fport
== fport
&&
2232 inp
->inp_lport
== lport
) {
2233 if ((found
= (inp
->inp_socket
!= NULL
))) {
2237 *uid
= kauth_cred_getuid(
2238 inp
->inp_socket
->so_cred
);
2239 *gid
= kauth_cred_getgid(
2240 inp
->inp_socket
->so_cred
);
2242 lck_rw_done(pcbinfo
->ipi_lock
);
2251 lck_rw_done(pcbinfo
->ipi_lock
);
2255 head
= &pcbinfo
->ipi_hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
2256 pcbinfo
->ipi_hashmask
)];
2257 LIST_FOREACH(inp
, head
, inp_hash
) {
2258 if (!(inp
->inp_vflag
& INP_IPV4
)) {
2261 if (inp_restricted_recv(inp
, ifp
)) {
2266 if (!necp_socket_is_allowed_to_recv_on_interface(inp
, ifp
)) {
2271 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
&&
2272 inp
->inp_lport
== lport
) {
2273 if (inp
->inp_laddr
.s_addr
== laddr
.s_addr
) {
2274 if ((found
= (inp
->inp_socket
!= NULL
))) {
2275 *uid
= kauth_cred_getuid(
2276 inp
->inp_socket
->so_cred
);
2277 *gid
= kauth_cred_getgid(
2278 inp
->inp_socket
->so_cred
);
2280 lck_rw_done(pcbinfo
->ipi_lock
);
2282 } else if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
2283 if (inp
->inp_socket
&&
2284 SOCK_CHECK_DOM(inp
->inp_socket
, PF_INET6
)) {
2285 local_wild_mapped
= inp
;
2292 if (local_wild
== NULL
) {
2293 if (local_wild_mapped
!= NULL
) {
2294 if ((found
= (local_wild_mapped
->inp_socket
!= NULL
))) {
2295 *uid
= kauth_cred_getuid(
2296 local_wild_mapped
->inp_socket
->so_cred
);
2297 *gid
= kauth_cred_getgid(
2298 local_wild_mapped
->inp_socket
->so_cred
);
2300 lck_rw_done(pcbinfo
->ipi_lock
);
2303 lck_rw_done(pcbinfo
->ipi_lock
);
2306 if ((found
= (local_wild
->inp_socket
!= NULL
))) {
2307 *uid
= kauth_cred_getuid(
2308 local_wild
->inp_socket
->so_cred
);
2309 *gid
= kauth_cred_getgid(
2310 local_wild
->inp_socket
->so_cred
);
2312 lck_rw_done(pcbinfo
->ipi_lock
);
2317 * Lookup PCB in hash list.
2320 in_pcblookup_hash(struct inpcbinfo
*pcbinfo
, struct in_addr faddr
,
2321 u_int fport_arg
, struct in_addr laddr
, u_int lport_arg
, int wildcard
,
2324 struct inpcbhead
*head
;
2326 u_short fport
= (u_short
)fport_arg
, lport
= (u_short
)lport_arg
;
2327 struct inpcb
*local_wild
= NULL
;
2328 struct inpcb
*local_wild_mapped
= NULL
;
2331 * We may have found the pcb in the last lookup - check this first.
2334 lck_rw_lock_shared(pcbinfo
->ipi_lock
);
2337 * First look for an exact match.
2339 head
= &pcbinfo
->ipi_hashbase
[INP_PCBHASH(faddr
.s_addr
, lport
, fport
,
2340 pcbinfo
->ipi_hashmask
)];
2341 LIST_FOREACH(inp
, head
, inp_hash
) {
2342 if (!(inp
->inp_vflag
& INP_IPV4
)) {
2345 if (inp_restricted_recv(inp
, ifp
)) {
2350 if (!necp_socket_is_allowed_to_recv_on_interface(inp
, ifp
)) {
2355 if (inp
->inp_faddr
.s_addr
== faddr
.s_addr
&&
2356 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
2357 inp
->inp_fport
== fport
&&
2358 inp
->inp_lport
== lport
) {
2362 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) !=
2364 lck_rw_done(pcbinfo
->ipi_lock
);
2367 /* it's there but dead, say it isn't found */
2368 lck_rw_done(pcbinfo
->ipi_lock
);
2378 lck_rw_done(pcbinfo
->ipi_lock
);
2382 head
= &pcbinfo
->ipi_hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
2383 pcbinfo
->ipi_hashmask
)];
2384 LIST_FOREACH(inp
, head
, inp_hash
) {
2385 if (!(inp
->inp_vflag
& INP_IPV4
)) {
2388 if (inp_restricted_recv(inp
, ifp
)) {
2393 if (!necp_socket_is_allowed_to_recv_on_interface(inp
, ifp
)) {
2398 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
&&
2399 inp
->inp_lport
== lport
) {
2400 if (inp
->inp_laddr
.s_addr
== laddr
.s_addr
) {
2401 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) !=
2403 lck_rw_done(pcbinfo
->ipi_lock
);
2406 /* it's dead; say it isn't found */
2407 lck_rw_done(pcbinfo
->ipi_lock
);
2410 } else if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
2411 if (SOCK_CHECK_DOM(inp
->inp_socket
, PF_INET6
)) {
2412 local_wild_mapped
= inp
;
2419 if (local_wild
== NULL
) {
2420 if (local_wild_mapped
!= NULL
) {
2421 if (in_pcb_checkstate(local_wild_mapped
,
2422 WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
2423 lck_rw_done(pcbinfo
->ipi_lock
);
2424 return local_wild_mapped
;
2426 /* it's dead; say it isn't found */
2427 lck_rw_done(pcbinfo
->ipi_lock
);
2431 lck_rw_done(pcbinfo
->ipi_lock
);
2434 if (in_pcb_checkstate(local_wild
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
2435 lck_rw_done(pcbinfo
->ipi_lock
);
2439 * It's either not found or is already dead.
2441 lck_rw_done(pcbinfo
->ipi_lock
);
2446 * @brief Insert PCB onto various hash lists.
2448 * @param inp Pointer to internet protocol control block
2449 * @param locked Implies if ipi_lock (protecting pcb list)
2450 * is already locked or not.
2452 * @return int error on failure and 0 on success
2455 in_pcbinshash(struct inpcb
*inp
, int locked
)
2457 struct inpcbhead
*pcbhash
;
2458 struct inpcbporthead
*pcbporthash
;
2459 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
2460 struct inpcbport
*phd
;
2461 u_int32_t hashkey_faddr
;
2464 if (!lck_rw_try_lock_exclusive(pcbinfo
->ipi_lock
)) {
2466 * Lock inversion issue, mostly with udp
2469 socket_unlock(inp
->inp_socket
, 0);
2470 lck_rw_lock_exclusive(pcbinfo
->ipi_lock
);
2471 socket_lock(inp
->inp_socket
, 0);
2476 * This routine or its caller may have given up
2477 * socket's protocol lock briefly.
2478 * During that time the socket may have been dropped.
2479 * Safe-guarding against that.
2481 if (inp
->inp_state
== INPCB_STATE_DEAD
) {
2483 lck_rw_done(pcbinfo
->ipi_lock
);
2485 return ECONNABORTED
;
2489 if (inp
->inp_vflag
& INP_IPV6
) {
2490 hashkey_faddr
= inp
->in6p_faddr
.s6_addr32
[3] /* XXX */;
2492 hashkey_faddr
= inp
->inp_faddr
.s_addr
;
2495 inp
->inp_hash_element
= INP_PCBHASH(hashkey_faddr
, inp
->inp_lport
,
2496 inp
->inp_fport
, pcbinfo
->ipi_hashmask
);
2498 pcbhash
= &pcbinfo
->ipi_hashbase
[inp
->inp_hash_element
];
2500 pcbporthash
= &pcbinfo
->ipi_porthashbase
[INP_PCBPORTHASH(inp
->inp_lport
,
2501 pcbinfo
->ipi_porthashmask
)];
2504 * Go through port list and look for a head for this lport.
2506 LIST_FOREACH(phd
, pcbporthash
, phd_hash
) {
2507 if (phd
->phd_port
== inp
->inp_lport
) {
2513 * If none exists, malloc one and tack it on.
2516 MALLOC(phd
, struct inpcbport
*, sizeof(struct inpcbport
),
2520 lck_rw_done(pcbinfo
->ipi_lock
);
2522 return ENOBUFS
; /* XXX */
2524 phd
->phd_port
= inp
->inp_lport
;
2525 LIST_INIT(&phd
->phd_pcblist
);
2526 LIST_INSERT_HEAD(pcbporthash
, phd
, phd_hash
);
2529 VERIFY(!(inp
->inp_flags2
& INP2_INHASHLIST
));
2533 LIST_INSERT_HEAD(&phd
->phd_pcblist
, inp
, inp_portlist
);
2534 LIST_INSERT_HEAD(pcbhash
, inp
, inp_hash
);
2535 inp
->inp_flags2
|= INP2_INHASHLIST
;
2538 lck_rw_done(pcbinfo
->ipi_lock
);
2542 // This call catches the original setting of the local address
2543 inp_update_necp_policy(inp
, NULL
, NULL
, 0);
2550 * Move PCB to the proper hash bucket when { faddr, fport } have been
2551 * changed. NOTE: This does not handle the case of the lport changing (the
2552 * hashed port list would have to be updated as well), so the lport must
2553 * not change after in_pcbinshash() has been called.
2556 in_pcbrehash(struct inpcb
*inp
)
2558 struct inpcbhead
*head
;
2559 u_int32_t hashkey_faddr
;
2561 if (inp
->inp_vflag
& INP_IPV6
) {
2562 hashkey_faddr
= inp
->in6p_faddr
.s6_addr32
[3] /* XXX */;
2564 hashkey_faddr
= inp
->inp_faddr
.s_addr
;
2567 inp
->inp_hash_element
= INP_PCBHASH(hashkey_faddr
, inp
->inp_lport
,
2568 inp
->inp_fport
, inp
->inp_pcbinfo
->ipi_hashmask
);
2569 head
= &inp
->inp_pcbinfo
->ipi_hashbase
[inp
->inp_hash_element
];
2571 if (inp
->inp_flags2
& INP2_INHASHLIST
) {
2572 LIST_REMOVE(inp
, inp_hash
);
2573 inp
->inp_flags2
&= ~INP2_INHASHLIST
;
2576 VERIFY(!(inp
->inp_flags2
& INP2_INHASHLIST
));
2577 LIST_INSERT_HEAD(head
, inp
, inp_hash
);
2578 inp
->inp_flags2
|= INP2_INHASHLIST
;
2581 // This call catches updates to the remote addresses
2582 inp_update_necp_policy(inp
, NULL
, NULL
, 0);
2587 * Remove PCB from various lists.
2588 * Must be called pcbinfo lock is held in exclusive mode.
2591 in_pcbremlists(struct inpcb
*inp
)
2593 inp
->inp_gencnt
= ++inp
->inp_pcbinfo
->ipi_gencnt
;
2596 * Check if it's in hashlist -- an inp is placed in hashlist when
2597 * it's local port gets assigned. So it should also be present
2600 if (inp
->inp_flags2
& INP2_INHASHLIST
) {
2601 struct inpcbport
*phd
= inp
->inp_phd
;
2603 VERIFY(phd
!= NULL
&& inp
->inp_lport
> 0);
2605 LIST_REMOVE(inp
, inp_hash
);
2606 inp
->inp_hash
.le_next
= NULL
;
2607 inp
->inp_hash
.le_prev
= NULL
;
2609 LIST_REMOVE(inp
, inp_portlist
);
2610 inp
->inp_portlist
.le_next
= NULL
;
2611 inp
->inp_portlist
.le_prev
= NULL
;
2612 if (LIST_EMPTY(&phd
->phd_pcblist
)) {
2613 LIST_REMOVE(phd
, phd_hash
);
2616 inp
->inp_phd
= NULL
;
2617 inp
->inp_flags2
&= ~INP2_INHASHLIST
;
2619 VERIFY(!(inp
->inp_flags2
& INP2_INHASHLIST
));
2621 if (inp
->inp_flags2
& INP2_TIMEWAIT
) {
2622 /* Remove from time-wait queue */
2623 tcp_remove_from_time_wait(inp
);
2624 inp
->inp_flags2
&= ~INP2_TIMEWAIT
;
2625 VERIFY(inp
->inp_pcbinfo
->ipi_twcount
!= 0);
2626 inp
->inp_pcbinfo
->ipi_twcount
--;
2628 /* Remove from global inp list if it is not time-wait */
2629 LIST_REMOVE(inp
, inp_list
);
2632 if (inp
->inp_flags2
& INP2_IN_FCTREE
) {
2633 inp_fc_getinp(inp
->inp_flowhash
, (INPFC_SOLOCKED
| INPFC_REMOVE
));
2634 VERIFY(!(inp
->inp_flags2
& INP2_IN_FCTREE
));
2637 inp
->inp_pcbinfo
->ipi_count
--;
2641 * Mechanism used to defer the memory release of PCBs
2642 * The pcb list will contain the pcb until the reaper can clean it up if
2643 * the following conditions are met:
2645 * 2) wantcnt is STOPUSING
2647 * This function will be called to either mark the pcb as
2650 in_pcb_checkstate(struct inpcb
*pcb
, int mode
, int locked
)
2652 volatile UInt32
*wantcnt
= (volatile UInt32
*)&pcb
->inp_wantcnt
;
2659 * Try to mark the pcb as ready for recycling. CAS with
2660 * STOPUSING, if success we're good, if it's in use, will
2664 socket_lock(pcb
->inp_socket
, 1);
2666 pcb
->inp_state
= INPCB_STATE_DEAD
;
2669 if (pcb
->inp_socket
->so_usecount
< 0) {
2670 panic("%s: pcb=%p so=%p usecount is negative\n",
2671 __func__
, pcb
, pcb
->inp_socket
);
2675 socket_unlock(pcb
->inp_socket
, 1);
2678 inpcb_gc_sched(pcb
->inp_pcbinfo
, INPCB_TIMER_FAST
);
2680 origwant
= *wantcnt
;
2681 if ((UInt16
) origwant
== 0xffff) { /* should stop using */
2682 return WNT_STOPUSING
;
2685 if ((UInt16
) origwant
== 0) {
2686 /* try to mark it as unsuable now */
2687 OSCompareAndSwap(origwant
, newwant
, wantcnt
);
2689 return WNT_STOPUSING
;
2693 * Try to increase reference to pcb. If WNT_STOPUSING
2694 * should bail out. If socket state DEAD, try to set count
2695 * to STOPUSING, return failed otherwise increase cnt.
2698 origwant
= *wantcnt
;
2699 if ((UInt16
) origwant
== 0xffff) {
2700 /* should stop using */
2701 return WNT_STOPUSING
;
2703 newwant
= origwant
+ 1;
2704 } while (!OSCompareAndSwap(origwant
, newwant
, wantcnt
));
2709 * Release reference. If result is null and pcb state
2710 * is DEAD, set wanted bit to STOPUSING
2713 socket_lock(pcb
->inp_socket
, 1);
2717 origwant
= *wantcnt
;
2718 if ((UInt16
) origwant
== 0x0) {
2719 panic("%s: pcb=%p release with zero count",
2723 if ((UInt16
) origwant
== 0xffff) {
2724 /* should stop using */
2726 socket_unlock(pcb
->inp_socket
, 1);
2728 return WNT_STOPUSING
;
2730 newwant
= origwant
- 1;
2731 } while (!OSCompareAndSwap(origwant
, newwant
, wantcnt
));
2733 if (pcb
->inp_state
== INPCB_STATE_DEAD
) {
2736 if (pcb
->inp_socket
->so_usecount
< 0) {
2737 panic("%s: RELEASE pcb=%p so=%p usecount is negative\n",
2738 __func__
, pcb
, pcb
->inp_socket
);
2743 socket_unlock(pcb
->inp_socket
, 1);
2748 panic("%s: so=%p not a valid state =%x\n", __func__
,
2749 pcb
->inp_socket
, mode
);
2758 * inpcb_to_compat copies specific bits of an inpcb to a inpcb_compat.
2759 * The inpcb_compat data structure is passed to user space and must
2760 * not change. We intentionally avoid copying pointers.
2763 inpcb_to_compat(struct inpcb
*inp
, struct inpcb_compat
*inp_compat
)
2765 bzero(inp_compat
, sizeof(*inp_compat
));
2766 inp_compat
->inp_fport
= inp
->inp_fport
;
2767 inp_compat
->inp_lport
= inp
->inp_lport
;
2768 inp_compat
->nat_owner
= 0;
2769 inp_compat
->nat_cookie
= 0;
2770 inp_compat
->inp_gencnt
= inp
->inp_gencnt
;
2771 inp_compat
->inp_flags
= inp
->inp_flags
;
2772 inp_compat
->inp_flow
= inp
->inp_flow
;
2773 inp_compat
->inp_vflag
= inp
->inp_vflag
;
2774 inp_compat
->inp_ip_ttl
= inp
->inp_ip_ttl
;
2775 inp_compat
->inp_ip_p
= inp
->inp_ip_p
;
2776 inp_compat
->inp_dependfaddr
.inp6_foreign
=
2777 inp
->inp_dependfaddr
.inp6_foreign
;
2778 inp_compat
->inp_dependladdr
.inp6_local
=
2779 inp
->inp_dependladdr
.inp6_local
;
2780 inp_compat
->inp_depend4
.inp4_ip_tos
= inp
->inp_depend4
.inp4_ip_tos
;
2781 inp_compat
->inp_depend6
.inp6_hlim
= 0;
2782 inp_compat
->inp_depend6
.inp6_cksum
= inp
->inp_depend6
.inp6_cksum
;
2783 inp_compat
->inp_depend6
.inp6_ifindex
= 0;
2784 inp_compat
->inp_depend6
.inp6_hops
= inp
->inp_depend6
.inp6_hops
;
2787 #if XNU_TARGET_OS_OSX
2789 inpcb_to_xinpcb64(struct inpcb
*inp
, struct xinpcb64
*xinp
)
2791 xinp
->inp_fport
= inp
->inp_fport
;
2792 xinp
->inp_lport
= inp
->inp_lport
;
2793 xinp
->inp_gencnt
= inp
->inp_gencnt
;
2794 xinp
->inp_flags
= inp
->inp_flags
;
2795 xinp
->inp_flow
= inp
->inp_flow
;
2796 xinp
->inp_vflag
= inp
->inp_vflag
;
2797 xinp
->inp_ip_ttl
= inp
->inp_ip_ttl
;
2798 xinp
->inp_ip_p
= inp
->inp_ip_p
;
2799 xinp
->inp_dependfaddr
.inp6_foreign
= inp
->inp_dependfaddr
.inp6_foreign
;
2800 xinp
->inp_dependladdr
.inp6_local
= inp
->inp_dependladdr
.inp6_local
;
2801 xinp
->inp_depend4
.inp4_ip_tos
= inp
->inp_depend4
.inp4_ip_tos
;
2802 xinp
->inp_depend6
.inp6_hlim
= 0;
2803 xinp
->inp_depend6
.inp6_cksum
= inp
->inp_depend6
.inp6_cksum
;
2804 xinp
->inp_depend6
.inp6_ifindex
= 0;
2805 xinp
->inp_depend6
.inp6_hops
= inp
->inp_depend6
.inp6_hops
;
2807 #endif /* XNU_TARGET_OS_OSX */
2810 * The following routines implement this scheme:
2812 * Callers of ip_output() that intend to cache the route in the inpcb pass
2813 * a local copy of the struct route to ip_output(). Using a local copy of
2814 * the cached route significantly simplifies things as IP no longer has to
2815 * worry about having exclusive access to the passed in struct route, since
2816 * it's defined in the caller's stack; in essence, this allows for a lock-
2817 * less operation when updating the struct route at the IP level and below,
2818 * whenever necessary. The scheme works as follows:
2820 * Prior to dropping the socket's lock and calling ip_output(), the caller
2821 * copies the struct route from the inpcb into its stack, and adds a reference
2822 * to the cached route entry, if there was any. The socket's lock is then
2823 * dropped and ip_output() is called with a pointer to the copy of struct
2824 * route defined on the stack (not to the one in the inpcb.)
2826 * Upon returning from ip_output(), the caller then acquires the socket's
2827 * lock and synchronizes the cache; if there is no route cached in the inpcb,
2828 * it copies the local copy of struct route (which may or may not contain any
2829 * route) back into the cache; otherwise, if the inpcb has a route cached in
2830 * it, the one in the local copy will be freed, if there's any. Trashing the
2831 * cached route in the inpcb can be avoided because ip_output() is single-
2832 * threaded per-PCB (i.e. multiple transmits on a PCB are always serialized
2833 * by the socket/transport layer.)
2836 inp_route_copyout(struct inpcb
*inp
, struct route
*dst
)
2838 struct route
*src
= &inp
->inp_route
;
2840 socket_lock_assert_owned(inp
->inp_socket
);
2843 * If the route in the PCB is stale or not for IPv4, blow it away;
2844 * this is possible in the case of IPv4-mapped address case.
2846 if (ROUTE_UNUSABLE(src
) || rt_key(src
->ro_rt
)->sa_family
!= AF_INET
) {
2850 route_copyout(dst
, src
, sizeof(*dst
));
2854 inp_route_copyin(struct inpcb
*inp
, struct route
*src
)
2856 struct route
*dst
= &inp
->inp_route
;
2858 socket_lock_assert_owned(inp
->inp_socket
);
2860 /* Minor sanity check */
2861 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET
) {
2862 panic("%s: wrong or corrupted route: %p", __func__
, src
);
2865 route_copyin(src
, dst
, sizeof(*src
));
2869 * Handler for setting IP_BOUND_IF/IPV6_BOUND_IF socket option.
2872 inp_bindif(struct inpcb
*inp
, unsigned int ifscope
, struct ifnet
**pifp
)
2874 struct ifnet
*ifp
= NULL
;
2876 ifnet_head_lock_shared();
2877 if ((ifscope
> (unsigned)if_index
) || (ifscope
!= IFSCOPE_NONE
&&
2878 (ifp
= ifindex2ifnet
[ifscope
]) == NULL
)) {
2884 VERIFY(ifp
!= NULL
|| ifscope
== IFSCOPE_NONE
);
2887 * A zero interface scope value indicates an "unbind".
2888 * Otherwise, take in whatever value the app desires;
2889 * the app may already know the scope (or force itself
2890 * to such a scope) ahead of time before the interface
2891 * gets attached. It doesn't matter either way; any
2892 * route lookup from this point on will require an
2893 * exact match for the embedded interface scope.
2895 inp
->inp_boundifp
= ifp
;
2896 if (inp
->inp_boundifp
== NULL
) {
2897 inp
->inp_flags
&= ~INP_BOUND_IF
;
2899 inp
->inp_flags
|= INP_BOUND_IF
;
2902 /* Blow away any cached route in the PCB */
2903 ROUTE_RELEASE(&inp
->inp_route
);
2913 * Handler for setting IP_NO_IFT_CELLULAR/IPV6_NO_IFT_CELLULAR socket option,
2914 * as well as for setting PROC_UUID_NO_CELLULAR policy.
2917 inp_set_nocellular(struct inpcb
*inp
)
2919 inp
->inp_flags
|= INP_NO_IFT_CELLULAR
;
2921 /* Blow away any cached route in the PCB */
2922 ROUTE_RELEASE(&inp
->inp_route
);
2926 * Handler for clearing IP_NO_IFT_CELLULAR/IPV6_NO_IFT_CELLULAR socket option,
2927 * as well as for clearing PROC_UUID_NO_CELLULAR policy.
2930 inp_clear_nocellular(struct inpcb
*inp
)
2932 struct socket
*so
= inp
->inp_socket
;
2935 * SO_RESTRICT_DENY_CELLULAR socket restriction issued on the socket
2936 * has a higher precendence than INP_NO_IFT_CELLULAR. Clear the flag
2937 * if and only if the socket is unrestricted.
2939 if (so
!= NULL
&& !(so
->so_restrictions
& SO_RESTRICT_DENY_CELLULAR
)) {
2940 inp
->inp_flags
&= ~INP_NO_IFT_CELLULAR
;
2942 /* Blow away any cached route in the PCB */
2943 ROUTE_RELEASE(&inp
->inp_route
);
2948 inp_set_noexpensive(struct inpcb
*inp
)
2950 inp
->inp_flags2
|= INP2_NO_IFF_EXPENSIVE
;
2952 /* Blow away any cached route in the PCB */
2953 ROUTE_RELEASE(&inp
->inp_route
);
2957 inp_set_noconstrained(struct inpcb
*inp
)
2959 inp
->inp_flags2
|= INP2_NO_IFF_CONSTRAINED
;
2961 /* Blow away any cached route in the PCB */
2962 ROUTE_RELEASE(&inp
->inp_route
);
2966 inp_set_awdl_unrestricted(struct inpcb
*inp
)
2968 inp
->inp_flags2
|= INP2_AWDL_UNRESTRICTED
;
2970 /* Blow away any cached route in the PCB */
2971 ROUTE_RELEASE(&inp
->inp_route
);
2975 inp_get_awdl_unrestricted(struct inpcb
*inp
)
2977 return (inp
->inp_flags2
& INP2_AWDL_UNRESTRICTED
) ? TRUE
: FALSE
;
2981 inp_clear_awdl_unrestricted(struct inpcb
*inp
)
2983 inp
->inp_flags2
&= ~INP2_AWDL_UNRESTRICTED
;
2985 /* Blow away any cached route in the PCB */
2986 ROUTE_RELEASE(&inp
->inp_route
);
2990 inp_set_intcoproc_allowed(struct inpcb
*inp
)
2992 inp
->inp_flags2
|= INP2_INTCOPROC_ALLOWED
;
2994 /* Blow away any cached route in the PCB */
2995 ROUTE_RELEASE(&inp
->inp_route
);
2999 inp_get_intcoproc_allowed(struct inpcb
*inp
)
3001 return (inp
->inp_flags2
& INP2_INTCOPROC_ALLOWED
) ? TRUE
: FALSE
;
3005 inp_clear_intcoproc_allowed(struct inpcb
*inp
)
3007 inp
->inp_flags2
&= ~INP2_INTCOPROC_ALLOWED
;
3009 /* Blow away any cached route in the PCB */
3010 ROUTE_RELEASE(&inp
->inp_route
);
3015 * Called when PROC_UUID_NECP_APP_POLICY is set.
3018 inp_set_want_app_policy(struct inpcb
*inp
)
3020 inp
->inp_flags2
|= INP2_WANT_APP_POLICY
;
3024 * Called when PROC_UUID_NECP_APP_POLICY is cleared.
3027 inp_clear_want_app_policy(struct inpcb
*inp
)
3029 inp
->inp_flags2
&= ~INP2_WANT_APP_POLICY
;
3034 * Calculate flow hash for an inp, used by an interface to identify a
3035 * flow. When an interface provides flow control advisory, this flow
3036 * hash is used as an identifier.
3039 inp_calc_flowhash(struct inpcb
*inp
)
3041 struct inp_flowhash_key fh
__attribute__((aligned(8)));
3042 u_int32_t flowhash
= 0;
3043 struct inpcb
*tmp_inp
= NULL
;
3045 if (inp_hash_seed
== 0) {
3046 inp_hash_seed
= RandomULong();
3049 bzero(&fh
, sizeof(fh
));
3051 bcopy(&inp
->inp_dependladdr
, &fh
.infh_laddr
, sizeof(fh
.infh_laddr
));
3052 bcopy(&inp
->inp_dependfaddr
, &fh
.infh_faddr
, sizeof(fh
.infh_faddr
));
3054 fh
.infh_lport
= inp
->inp_lport
;
3055 fh
.infh_fport
= inp
->inp_fport
;
3056 fh
.infh_af
= (inp
->inp_vflag
& INP_IPV6
) ? AF_INET6
: AF_INET
;
3057 fh
.infh_proto
= inp
->inp_ip_p
;
3058 fh
.infh_rand1
= RandomULong();
3059 fh
.infh_rand2
= RandomULong();
3062 flowhash
= net_flowhash(&fh
, sizeof(fh
), inp_hash_seed
);
3063 if (flowhash
== 0) {
3064 /* try to get a non-zero flowhash */
3065 inp_hash_seed
= RandomULong();
3069 inp
->inp_flowhash
= flowhash
;
3071 /* Insert the inp into inp_fc_tree */
3072 lck_mtx_lock_spin(&inp_fc_lck
);
3073 tmp_inp
= RB_FIND(inp_fc_tree
, &inp_fc_tree
, inp
);
3074 if (tmp_inp
!= NULL
) {
3076 * There is a different inp with the same flowhash.
3077 * There can be a collision on flow hash but the
3078 * probability is low. Let's recompute the
3081 lck_mtx_unlock(&inp_fc_lck
);
3082 /* recompute hash seed */
3083 inp_hash_seed
= RandomULong();
3087 RB_INSERT(inp_fc_tree
, &inp_fc_tree
, inp
);
3088 inp
->inp_flags2
|= INP2_IN_FCTREE
;
3089 lck_mtx_unlock(&inp_fc_lck
);
3095 inp_flowadv(uint32_t flowhash
)
3099 inp
= inp_fc_getinp(flowhash
, 0);
3104 inp_fc_feedback(inp
);
3108 * Function to compare inp_fc_entries in inp flow control tree
3111 infc_cmp(const struct inpcb
*inp1
, const struct inpcb
*inp2
)
3113 return memcmp(&(inp1
->inp_flowhash
), &(inp2
->inp_flowhash
),
3114 sizeof(inp1
->inp_flowhash
));
3117 static struct inpcb
*
3118 inp_fc_getinp(u_int32_t flowhash
, u_int32_t flags
)
3120 struct inpcb
*inp
= NULL
;
3121 int locked
= (flags
& INPFC_SOLOCKED
) ? 1 : 0;
3123 lck_mtx_lock_spin(&inp_fc_lck
);
3124 key_inp
.inp_flowhash
= flowhash
;
3125 inp
= RB_FIND(inp_fc_tree
, &inp_fc_tree
, &key_inp
);
3127 /* inp is not present, return */
3128 lck_mtx_unlock(&inp_fc_lck
);
3132 if (flags
& INPFC_REMOVE
) {
3133 RB_REMOVE(inp_fc_tree
, &inp_fc_tree
, inp
);
3134 lck_mtx_unlock(&inp_fc_lck
);
3136 bzero(&(inp
->infc_link
), sizeof(inp
->infc_link
));
3137 inp
->inp_flags2
&= ~INP2_IN_FCTREE
;
3141 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, locked
) == WNT_STOPUSING
) {
3144 lck_mtx_unlock(&inp_fc_lck
);
3150 inp_fc_feedback(struct inpcb
*inp
)
3152 struct socket
*so
= inp
->inp_socket
;
3154 /* we already hold a want_cnt on this inp, socket can't be null */
3158 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
3159 socket_unlock(so
, 1);
3163 if (inp
->inp_sndinprog_cnt
> 0) {
3164 inp
->inp_flags
|= INP_FC_FEEDBACK
;
3168 * Return if the connection is not in flow-controlled state.
3169 * This can happen if the connection experienced
3170 * loss while it was in flow controlled state
3172 if (!INP_WAIT_FOR_IF_FEEDBACK(inp
)) {
3173 socket_unlock(so
, 1);
3176 inp_reset_fc_state(inp
);
3178 if (SOCK_TYPE(so
) == SOCK_STREAM
) {
3179 inp_fc_unthrottle_tcp(inp
);
3182 socket_unlock(so
, 1);
3186 inp_reset_fc_state(struct inpcb
*inp
)
3188 struct socket
*so
= inp
->inp_socket
;
3189 int suspended
= (INP_IS_FLOW_SUSPENDED(inp
)) ? 1 : 0;
3190 int needwakeup
= (INP_WAIT_FOR_IF_FEEDBACK(inp
)) ? 1 : 0;
3192 inp
->inp_flags
&= ~(INP_FLOW_CONTROLLED
| INP_FLOW_SUSPENDED
);
3195 so
->so_flags
&= ~(SOF_SUSPENDED
);
3196 soevent(so
, (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_RESUME
));
3199 /* Give a write wakeup to unblock the socket */
3206 inp_set_fc_state(struct inpcb
*inp
, int advcode
)
3208 boolean_t is_flow_controlled
= INP_WAIT_FOR_IF_FEEDBACK(inp
);
3209 struct inpcb
*tmp_inp
= NULL
;
3211 * If there was a feedback from the interface when
3212 * send operation was in progress, we should ignore
3213 * this flow advisory to avoid a race between setting
3214 * flow controlled state and receiving feedback from
3217 if (inp
->inp_flags
& INP_FC_FEEDBACK
) {
3221 inp
->inp_flags
&= ~(INP_FLOW_CONTROLLED
| INP_FLOW_SUSPENDED
);
3222 if ((tmp_inp
= inp_fc_getinp(inp
->inp_flowhash
,
3223 INPFC_SOLOCKED
)) != NULL
) {
3224 if (in_pcb_checkstate(tmp_inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
3227 VERIFY(tmp_inp
== inp
);
3229 case FADV_FLOW_CONTROLLED
:
3230 inp
->inp_flags
|= INP_FLOW_CONTROLLED
;
3232 case FADV_SUSPENDED
:
3233 inp
->inp_flags
|= INP_FLOW_SUSPENDED
;
3234 soevent(inp
->inp_socket
,
3235 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_SUSPEND
));
3237 /* Record the fact that suspend event was sent */
3238 inp
->inp_socket
->so_flags
|= SOF_SUSPENDED
;
3242 if (!is_flow_controlled
&& SOCK_TYPE(inp
->inp_socket
) == SOCK_STREAM
) {
3243 inp_fc_throttle_tcp(inp
);
3251 * Handler for SO_FLUSH socket option.
3254 inp_flush(struct inpcb
*inp
, int optval
)
3256 u_int32_t flowhash
= inp
->inp_flowhash
;
3257 struct ifnet
*rtifp
, *oifp
;
3259 /* Either all classes or one of the valid ones */
3260 if (optval
!= SO_TC_ALL
&& !SO_VALID_TC(optval
)) {
3264 /* We need a flow hash for identification */
3265 if (flowhash
== 0) {
3269 /* Grab the interfaces from the route and pcb */
3270 rtifp
= ((inp
->inp_route
.ro_rt
!= NULL
) ?
3271 inp
->inp_route
.ro_rt
->rt_ifp
: NULL
);
3272 oifp
= inp
->inp_last_outifp
;
3274 if (rtifp
!= NULL
) {
3275 if_qflush_sc(rtifp
, so_tc2msc(optval
), flowhash
, NULL
, NULL
, 0);
3277 if (oifp
!= NULL
&& oifp
!= rtifp
) {
3278 if_qflush_sc(oifp
, so_tc2msc(optval
), flowhash
, NULL
, NULL
, 0);
3285 * Clear the INP_INADDR_ANY flag (special case for PPP only)
3288 inp_clear_INP_INADDR_ANY(struct socket
*so
)
3290 struct inpcb
*inp
= NULL
;
3293 inp
= sotoinpcb(so
);
3295 inp
->inp_flags
&= ~INP_INADDR_ANY
;
3297 socket_unlock(so
, 1);
3301 inp_get_soprocinfo(struct inpcb
*inp
, struct so_procinfo
*soprocinfo
)
3303 struct socket
*so
= inp
->inp_socket
;
3305 soprocinfo
->spi_pid
= so
->last_pid
;
3306 strlcpy(&soprocinfo
->spi_proc_name
[0], &inp
->inp_last_proc_name
[0],
3307 sizeof(soprocinfo
->spi_proc_name
));
3308 if (so
->last_pid
!= 0) {
3309 uuid_copy(soprocinfo
->spi_uuid
, so
->last_uuid
);
3312 * When not delegated, the effective pid is the same as the real pid
3314 if (so
->so_flags
& SOF_DELEGATED
) {
3315 soprocinfo
->spi_delegated
= 1;
3316 soprocinfo
->spi_epid
= so
->e_pid
;
3317 uuid_copy(soprocinfo
->spi_euuid
, so
->e_uuid
);
3319 soprocinfo
->spi_delegated
= 0;
3320 soprocinfo
->spi_epid
= so
->last_pid
;
3322 strlcpy(&soprocinfo
->spi_e_proc_name
[0], &inp
->inp_e_proc_name
[0],
3323 sizeof(soprocinfo
->spi_e_proc_name
));
3327 inp_findinpcb_procinfo(struct inpcbinfo
*pcbinfo
, uint32_t flowhash
,
3328 struct so_procinfo
*soprocinfo
)
3330 struct inpcb
*inp
= NULL
;
3333 bzero(soprocinfo
, sizeof(struct so_procinfo
));
3339 lck_rw_lock_shared(pcbinfo
->ipi_lock
);
3340 LIST_FOREACH(inp
, pcbinfo
->ipi_listhead
, inp_list
) {
3341 if (inp
->inp_state
!= INPCB_STATE_DEAD
&&
3342 inp
->inp_socket
!= NULL
&&
3343 inp
->inp_flowhash
== flowhash
) {
3345 inp_get_soprocinfo(inp
, soprocinfo
);
3349 lck_rw_done(pcbinfo
->ipi_lock
);
3354 #if CONFIG_PROC_UUID_POLICY
3356 inp_update_cellular_policy(struct inpcb
*inp
, boolean_t set
)
3358 struct socket
*so
= inp
->inp_socket
;
3362 VERIFY(inp
->inp_state
!= INPCB_STATE_DEAD
);
3364 before
= INP_NO_CELLULAR(inp
);
3366 inp_set_nocellular(inp
);
3368 inp_clear_nocellular(inp
);
3370 after
= INP_NO_CELLULAR(inp
);
3371 if (net_io_policy_log
&& (before
!= after
)) {
3372 static const char *ok
= "OK";
3373 static const char *nok
= "NOACCESS";
3374 uuid_string_t euuid_buf
;
3377 if (so
->so_flags
& SOF_DELEGATED
) {
3378 uuid_unparse(so
->e_uuid
, euuid_buf
);
3381 uuid_unparse(so
->last_uuid
, euuid_buf
);
3382 epid
= so
->last_pid
;
3385 /* allow this socket to generate another notification event */
3386 so
->so_ifdenied_notifies
= 0;
3388 log(LOG_DEBUG
, "%s: so 0x%llx [%d,%d] epid %d "
3389 "euuid %s%s %s->%s\n", __func__
,
3390 (uint64_t)VM_KERNEL_ADDRPERM(so
), SOCK_DOM(so
),
3391 SOCK_TYPE(so
), epid
, euuid_buf
,
3392 (so
->so_flags
& SOF_DELEGATED
) ?
3393 " [delegated]" : "",
3394 ((before
< after
) ? ok
: nok
),
3395 ((before
< after
) ? nok
: ok
));
3401 inp_update_necp_want_app_policy(struct inpcb
*inp
, boolean_t set
)
3403 struct socket
*so
= inp
->inp_socket
;
3407 VERIFY(inp
->inp_state
!= INPCB_STATE_DEAD
);
3409 before
= (inp
->inp_flags2
& INP2_WANT_APP_POLICY
);
3411 inp_set_want_app_policy(inp
);
3413 inp_clear_want_app_policy(inp
);
3415 after
= (inp
->inp_flags2
& INP2_WANT_APP_POLICY
);
3416 if (net_io_policy_log
&& (before
!= after
)) {
3417 static const char *wanted
= "WANTED";
3418 static const char *unwanted
= "UNWANTED";
3419 uuid_string_t euuid_buf
;
3422 if (so
->so_flags
& SOF_DELEGATED
) {
3423 uuid_unparse(so
->e_uuid
, euuid_buf
);
3426 uuid_unparse(so
->last_uuid
, euuid_buf
);
3427 epid
= so
->last_pid
;
3430 log(LOG_DEBUG
, "%s: so 0x%llx [%d,%d] epid %d "
3431 "euuid %s%s %s->%s\n", __func__
,
3432 (uint64_t)VM_KERNEL_ADDRPERM(so
), SOCK_DOM(so
),
3433 SOCK_TYPE(so
), epid
, euuid_buf
,
3434 (so
->so_flags
& SOF_DELEGATED
) ?
3435 " [delegated]" : "",
3436 ((before
< after
) ? unwanted
: wanted
),
3437 ((before
< after
) ? wanted
: unwanted
));
3441 #endif /* !CONFIG_PROC_UUID_POLICY */
3445 inp_update_necp_policy(struct inpcb
*inp
, struct sockaddr
*override_local_addr
, struct sockaddr
*override_remote_addr
, u_int override_bound_interface
)
3447 necp_socket_find_policy_match(inp
, override_local_addr
, override_remote_addr
, override_bound_interface
);
3448 if (necp_socket_should_rescope(inp
) &&
3449 inp
->inp_lport
== 0 &&
3450 inp
->inp_laddr
.s_addr
== INADDR_ANY
&&
3451 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
3452 // If we should rescope, and the socket is not yet bound
3453 inp_bindif(inp
, necp_socket_get_rescope_if_index(inp
), NULL
);
3459 inp_update_policy(struct inpcb
*inp
)
3461 #if CONFIG_PROC_UUID_POLICY
3462 struct socket
*so
= inp
->inp_socket
;
3463 uint32_t pflags
= 0;
3466 uint8_t *lookup_uuid
= NULL
;
3468 if (!net_io_policy_uuid
||
3469 so
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
) {
3474 * Kernel-created sockets that aren't delegating other sockets
3475 * are currently exempted from UUID policy checks.
3477 if (so
->last_pid
== 0 && !(so
->so_flags
& SOF_DELEGATED
)) {
3481 #if defined(XNU_TARGET_OS_OSX)
3482 if (so
->so_rpid
> 0) {
3483 lookup_uuid
= so
->so_ruuid
;
3484 ogencnt
= so
->so_policy_gencnt
;
3485 err
= proc_uuid_policy_lookup(lookup_uuid
, &pflags
, &so
->so_policy_gencnt
);
3488 if (lookup_uuid
== NULL
|| err
== ENOENT
) {
3489 lookup_uuid
= ((so
->so_flags
& SOF_DELEGATED
) ? so
->e_uuid
: so
->last_uuid
);
3490 ogencnt
= so
->so_policy_gencnt
;
3491 err
= proc_uuid_policy_lookup(lookup_uuid
, &pflags
, &so
->so_policy_gencnt
);
3495 * Discard cached generation count if the entry is gone (ENOENT),
3496 * so that we go thru the checks below.
3498 if (err
== ENOENT
&& ogencnt
!= 0) {
3499 so
->so_policy_gencnt
= 0;
3503 * If the generation count has changed, inspect the policy flags
3504 * and act accordingly. If a policy flag was previously set and
3505 * the UUID is no longer present in the table (ENOENT), treat it
3506 * as if the flag has been cleared.
3508 if ((err
== 0 || err
== ENOENT
) && ogencnt
!= so
->so_policy_gencnt
) {
3509 /* update cellular policy for this socket */
3510 if (err
== 0 && (pflags
& PROC_UUID_NO_CELLULAR
)) {
3511 inp_update_cellular_policy(inp
, TRUE
);
3512 } else if (!(pflags
& PROC_UUID_NO_CELLULAR
)) {
3513 inp_update_cellular_policy(inp
, FALSE
);
3516 /* update necp want app policy for this socket */
3517 if (err
== 0 && (pflags
& PROC_UUID_NECP_APP_POLICY
)) {
3518 inp_update_necp_want_app_policy(inp
, TRUE
);
3519 } else if (!(pflags
& PROC_UUID_NECP_APP_POLICY
)) {
3520 inp_update_necp_want_app_policy(inp
, FALSE
);
3525 return (err
== ENOENT
) ? 0 : err
;
3526 #else /* !CONFIG_PROC_UUID_POLICY */
3529 #endif /* !CONFIG_PROC_UUID_POLICY */
3532 static unsigned int log_restricted
;
3533 SYSCTL_DECL(_net_inet
);
3534 SYSCTL_INT(_net_inet
, OID_AUTO
, log_restricted
,
3535 CTLFLAG_RW
| CTLFLAG_LOCKED
, &log_restricted
, 0,
3536 "Log network restrictions");
3538 * Called when we need to enforce policy restrictions in the input path.
3540 * Returns TRUE if we're not allowed to receive data, otherwise FALSE.
3543 _inp_restricted_recv(struct inpcb
*inp
, struct ifnet
*ifp
)
3545 VERIFY(inp
!= NULL
);
3548 * Inbound restrictions.
3550 if (!sorestrictrecv
) {
3558 if (IFNET_IS_CELLULAR(ifp
) && INP_NO_CELLULAR(inp
)) {
3562 if (IFNET_IS_EXPENSIVE(ifp
) && INP_NO_EXPENSIVE(inp
)) {
3566 if (IFNET_IS_CONSTRAINED(ifp
) && INP_NO_CONSTRAINED(inp
)) {
3570 if (IFNET_IS_AWDL_RESTRICTED(ifp
) && !INP_AWDL_UNRESTRICTED(inp
)) {
3574 if (!(ifp
->if_eflags
& IFEF_RESTRICTED_RECV
)) {
3578 if (inp
->inp_flags
& INP_RECV_ANYIF
) {
3582 if ((inp
->inp_flags
& INP_BOUND_IF
) && inp
->inp_boundifp
== ifp
) {
3586 if (IFNET_IS_INTCOPROC(ifp
) && !INP_INTCOPROC_ALLOWED(inp
)) {
3594 inp_restricted_recv(struct inpcb
*inp
, struct ifnet
*ifp
)
3598 ret
= _inp_restricted_recv(inp
, ifp
);
3599 if (ret
== TRUE
&& log_restricted
) {
3600 printf("pid %d (%s) is unable to receive packets on %s\n",
3601 current_proc()->p_pid
, proc_best_name(current_proc()),
3608 * Called when we need to enforce policy restrictions in the output path.
3610 * Returns TRUE if we're not allowed to send data out, otherwise FALSE.
3613 _inp_restricted_send(struct inpcb
*inp
, struct ifnet
*ifp
)
3615 VERIFY(inp
!= NULL
);
3618 * Outbound restrictions.
3620 if (!sorestrictsend
) {
3628 if (IFNET_IS_CELLULAR(ifp
) && INP_NO_CELLULAR(inp
)) {
3632 if (IFNET_IS_EXPENSIVE(ifp
) && INP_NO_EXPENSIVE(inp
)) {
3636 if (IFNET_IS_CONSTRAINED(ifp
) && INP_NO_CONSTRAINED(inp
)) {
3640 if (IFNET_IS_AWDL_RESTRICTED(ifp
) && !INP_AWDL_UNRESTRICTED(inp
)) {
3644 if (IFNET_IS_INTCOPROC(ifp
) && !INP_INTCOPROC_ALLOWED(inp
)) {
3652 inp_restricted_send(struct inpcb
*inp
, struct ifnet
*ifp
)
3656 ret
= _inp_restricted_send(inp
, ifp
);
3657 if (ret
== TRUE
&& log_restricted
) {
3658 printf("pid %d (%s) is unable to transmit packets on %s\n",
3659 current_proc()->p_pid
, proc_best_name(current_proc()),
3666 inp_count_sndbytes(struct inpcb
*inp
, u_int32_t th_ack
)
3668 struct ifnet
*ifp
= inp
->inp_last_outifp
;
3669 struct socket
*so
= inp
->inp_socket
;
3670 if (ifp
!= NULL
&& !(so
->so_flags
& SOF_MP_SUBFLOW
) &&
3671 (ifp
->if_type
== IFT_CELLULAR
|| IFNET_IS_WIFI(ifp
))) {
3674 so
->so_snd
.sb_flags
|= SB_SNDBYTE_CNT
;
3677 * There can be data outstanding before the connection
3678 * becomes established -- TFO case
3680 if (so
->so_snd
.sb_cc
> 0) {
3681 inp_incr_sndbytes_total(so
, so
->so_snd
.sb_cc
);
3684 unsent
= inp_get_sndbytes_allunsent(so
, th_ack
);
3686 inp_incr_sndbytes_unsent(so
, unsent
);
3692 inp_incr_sndbytes_total(struct socket
*so
, int32_t len
)
3694 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
3695 struct ifnet
*ifp
= inp
->inp_last_outifp
;
3698 VERIFY(ifp
->if_sndbyte_total
>= 0);
3699 OSAddAtomic64(len
, &ifp
->if_sndbyte_total
);
3704 inp_decr_sndbytes_total(struct socket
*so
, int32_t len
)
3706 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
3707 struct ifnet
*ifp
= inp
->inp_last_outifp
;
3710 VERIFY(ifp
->if_sndbyte_total
>= len
);
3711 OSAddAtomic64(-len
, &ifp
->if_sndbyte_total
);
3716 inp_incr_sndbytes_unsent(struct socket
*so
, int32_t len
)
3718 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
3719 struct ifnet
*ifp
= inp
->inp_last_outifp
;
3722 VERIFY(ifp
->if_sndbyte_unsent
>= 0);
3723 OSAddAtomic64(len
, &ifp
->if_sndbyte_unsent
);
3728 inp_decr_sndbytes_unsent(struct socket
*so
, int32_t len
)
3730 if (so
== NULL
|| !(so
->so_snd
.sb_flags
& SB_SNDBYTE_CNT
)) {
3734 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
3735 struct ifnet
*ifp
= inp
->inp_last_outifp
;
3738 if (ifp
->if_sndbyte_unsent
>= len
) {
3739 OSAddAtomic64(-len
, &ifp
->if_sndbyte_unsent
);
3741 ifp
->if_sndbyte_unsent
= 0;
3747 inp_decr_sndbytes_allunsent(struct socket
*so
, u_int32_t th_ack
)
3751 if (so
== NULL
|| !(so
->so_snd
.sb_flags
& SB_SNDBYTE_CNT
)) {
3755 len
= inp_get_sndbytes_allunsent(so
, th_ack
);
3756 inp_decr_sndbytes_unsent(so
, len
);
3761 inp_set_activity_bitmap(struct inpcb
*inp
)
3763 in_stat_set_activity_bitmap(&inp
->inp_nw_activity
, net_uptime());
3767 inp_get_activity_bitmap(struct inpcb
*inp
, activity_bitmap_t
*ab
)
3769 bcopy(&inp
->inp_nw_activity
, ab
, sizeof(*ab
));
3773 inp_update_last_owner(struct socket
*so
, struct proc
*p
, struct proc
*ep
)
3775 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
3782 strlcpy(&inp
->inp_last_proc_name
[0], proc_name_address(p
), sizeof(inp
->inp_last_proc_name
));
3784 if (so
->so_flags
& SOF_DELEGATED
) {
3786 strlcpy(&inp
->inp_e_proc_name
[0], proc_name_address(ep
), sizeof(inp
->inp_e_proc_name
));
3788 inp
->inp_e_proc_name
[0] = 0;
3791 inp
->inp_e_proc_name
[0] = 0;
3796 inp_copy_last_owner(struct socket
*so
, struct socket
*head
)
3798 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
3799 struct inpcb
*head_inp
= (struct inpcb
*)head
->so_pcb
;
3801 if (inp
== NULL
|| head_inp
== NULL
) {
3805 strlcpy(&inp
->inp_last_proc_name
[0], &head_inp
->inp_last_proc_name
[0], sizeof(inp
->inp_last_proc_name
));
3806 strlcpy(&inp
->inp_e_proc_name
[0], &head_inp
->inp_e_proc_name
[0], sizeof(inp
->inp_e_proc_name
));