2 * Copyright (c) 1998-2019 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@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1988, 1990, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
64 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/filedesc.h>
74 #include <sys/proc_internal.h>
75 #include <sys/kauth.h>
76 #include <sys/file_internal.h>
77 #include <sys/fcntl.h>
78 #include <sys/malloc.h>
80 #include <sys/domain.h>
81 #include <sys/kernel.h>
82 #include <sys/event.h>
84 #include <sys/protosw.h>
85 #include <sys/socket.h>
86 #include <sys/socketvar.h>
87 #include <sys/resourcevar.h>
88 #include <sys/signalvar.h>
89 #include <sys/sysctl.h>
90 #include <sys/syslog.h>
92 #include <sys/uio_internal.h>
94 #include <sys/kdebug.h>
98 #include <sys/kern_event.h>
99 #include <net/route.h>
100 #include <net/init.h>
101 #include <net/net_api_stats.h>
102 #include <net/ntstat.h>
103 #include <net/content_filter.h>
104 #include <netinet/in.h>
105 #include <netinet/in_pcb.h>
106 #include <netinet/in_tclass.h>
107 #include <netinet/in_var.h>
108 #include <netinet/tcp_var.h>
109 #include <netinet/ip6.h>
110 #include <netinet6/ip6_var.h>
111 #include <netinet/flow_divert.h>
112 #include <kern/zalloc.h>
113 #include <kern/locks.h>
114 #include <machine/limits.h>
115 #include <libkern/OSAtomic.h>
116 #include <pexpert/pexpert.h>
117 #include <kern/assert.h>
118 #include <kern/task.h>
119 #include <kern/policy_internal.h>
121 #include <sys/kpi_mbuf.h>
122 #include <sys/mcache.h>
123 #include <sys/unpcb.h>
124 #include <libkern/section_keywords.h>
127 #include <security/mac_framework.h>
131 #include <netinet/mp_pcb.h>
132 #include <netinet/mptcp_var.h>
133 #endif /* MULTIPATH */
135 #define ROUNDUP(a, b) (((a) + ((b) - 1)) & (~((b) - 1)))
137 #if DEBUG || DEVELOPMENT
138 #define DEBUG_KERNEL_ADDRPERM(_v) (_v)
140 #define DEBUG_KERNEL_ADDRPERM(_v) VM_KERNEL_ADDRPERM(_v)
143 /* TODO: this should be in a header file somewhere */
144 extern char *proc_name_address(void *p
);
146 static u_int32_t so_cache_hw
; /* High water mark for socache */
147 static u_int32_t so_cache_timeouts
; /* number of timeouts */
148 static u_int32_t so_cache_max_freed
; /* max freed per timeout */
149 static u_int32_t cached_sock_count
= 0;
150 STAILQ_HEAD(, socket
) so_cache_head
;
151 int max_cached_sock_count
= MAX_CACHED_SOCKETS
;
152 static u_int32_t so_cache_time
;
153 static int socketinit_done
;
154 static struct zone
*so_cache_zone
;
156 static lck_grp_t
*so_cache_mtx_grp
;
157 static lck_attr_t
*so_cache_mtx_attr
;
158 static lck_grp_attr_t
*so_cache_mtx_grp_attr
;
159 static lck_mtx_t
*so_cache_mtx
;
161 #include <machine/limits.h>
163 static int filt_sorattach(struct knote
*kn
, struct kevent_qos_s
*kev
);
164 static void filt_sordetach(struct knote
*kn
);
165 static int filt_soread(struct knote
*kn
, long hint
);
166 static int filt_sortouch(struct knote
*kn
, struct kevent_qos_s
*kev
);
167 static int filt_sorprocess(struct knote
*kn
, struct kevent_qos_s
*kev
);
169 static int filt_sowattach(struct knote
*kn
, struct kevent_qos_s
*kev
);
170 static void filt_sowdetach(struct knote
*kn
);
171 static int filt_sowrite(struct knote
*kn
, long hint
);
172 static int filt_sowtouch(struct knote
*kn
, struct kevent_qos_s
*kev
);
173 static int filt_sowprocess(struct knote
*kn
, struct kevent_qos_s
*kev
);
175 static int filt_sockattach(struct knote
*kn
, struct kevent_qos_s
*kev
);
176 static void filt_sockdetach(struct knote
*kn
);
177 static int filt_sockev(struct knote
*kn
, long hint
);
178 static int filt_socktouch(struct knote
*kn
, struct kevent_qos_s
*kev
);
179 static int filt_sockprocess(struct knote
*kn
, struct kevent_qos_s
*kev
);
181 static int sooptcopyin_timeval(struct sockopt
*, struct timeval
*);
182 static int sooptcopyout_timeval(struct sockopt
*, const struct timeval
*);
184 SECURITY_READ_ONLY_EARLY(struct filterops
) soread_filtops
= {
186 .f_attach
= filt_sorattach
,
187 .f_detach
= filt_sordetach
,
188 .f_event
= filt_soread
,
189 .f_touch
= filt_sortouch
,
190 .f_process
= filt_sorprocess
,
193 SECURITY_READ_ONLY_EARLY(struct filterops
) sowrite_filtops
= {
195 .f_attach
= filt_sowattach
,
196 .f_detach
= filt_sowdetach
,
197 .f_event
= filt_sowrite
,
198 .f_touch
= filt_sowtouch
,
199 .f_process
= filt_sowprocess
,
202 SECURITY_READ_ONLY_EARLY(struct filterops
) sock_filtops
= {
204 .f_attach
= filt_sockattach
,
205 .f_detach
= filt_sockdetach
,
206 .f_event
= filt_sockev
,
207 .f_touch
= filt_socktouch
,
208 .f_process
= filt_sockprocess
,
211 SECURITY_READ_ONLY_EARLY(struct filterops
) soexcept_filtops
= {
213 .f_attach
= filt_sorattach
,
214 .f_detach
= filt_sordetach
,
215 .f_event
= filt_soread
,
216 .f_touch
= filt_sortouch
,
217 .f_process
= filt_sorprocess
,
220 SYSCTL_DECL(_kern_ipc
);
222 #define EVEN_MORE_LOCKING_DEBUG 0
224 int socket_debug
= 0;
225 SYSCTL_INT(_kern_ipc
, OID_AUTO
, socket_debug
,
226 CTLFLAG_RW
| CTLFLAG_LOCKED
, &socket_debug
, 0, "");
228 static unsigned long sodefunct_calls
= 0;
229 SYSCTL_LONG(_kern_ipc
, OID_AUTO
, sodefunct_calls
, CTLFLAG_LOCKED
,
230 &sodefunct_calls
, "");
232 static int socket_zone
= M_SOCKET
;
233 so_gen_t so_gencnt
; /* generation count for sockets */
235 MALLOC_DEFINE(M_SONAME
, "soname", "socket name");
236 MALLOC_DEFINE(M_PCB
, "pcb", "protocol control block");
238 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0)
239 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2)
240 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1)
241 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3)
242 #define DBG_FNC_SOSEND NETDBG_CODE(DBG_NETSOCK, (4 << 8) | 1)
243 #define DBG_FNC_SOSEND_LIST NETDBG_CODE(DBG_NETSOCK, (4 << 8) | 3)
244 #define DBG_FNC_SORECEIVE NETDBG_CODE(DBG_NETSOCK, (8 << 8))
245 #define DBG_FNC_SORECEIVE_LIST NETDBG_CODE(DBG_NETSOCK, (8 << 8) | 3)
246 #define DBG_FNC_SOSHUTDOWN NETDBG_CODE(DBG_NETSOCK, (9 << 8))
248 #define MAX_SOOPTGETM_SIZE (128 * MCLBYTES)
250 int somaxconn
= SOMAXCONN
;
251 SYSCTL_INT(_kern_ipc
, KIPC_SOMAXCONN
, somaxconn
,
252 CTLFLAG_RW
| CTLFLAG_LOCKED
, &somaxconn
, 0, "");
254 /* Should we get a maximum also ??? */
255 static int sosendmaxchain
= 65536;
256 static int sosendminchain
= 16384;
257 static int sorecvmincopy
= 16384;
258 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sosendminchain
,
259 CTLFLAG_RW
| CTLFLAG_LOCKED
, &sosendminchain
, 0, "");
260 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sorecvmincopy
,
261 CTLFLAG_RW
| CTLFLAG_LOCKED
, &sorecvmincopy
, 0, "");
264 * Set to enable jumbo clusters (if available) for large writes when
265 * the socket is marked with SOF_MULTIPAGES; see below.
268 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sosendjcl
,
269 CTLFLAG_RW
| CTLFLAG_LOCKED
, &sosendjcl
, 0, "");
272 * Set this to ignore SOF_MULTIPAGES and use jumbo clusters for large
273 * writes on the socket for all protocols on any network interfaces,
274 * depending upon sosendjcl above. Be extra careful when setting this
275 * to 1, because sending down packets that cross physical pages down to
276 * broken drivers (those that falsely assume that the physical pages
277 * are contiguous) might lead to system panics or silent data corruption.
278 * When set to 0, the system will respect SOF_MULTIPAGES, which is set
279 * only for TCP sockets whose outgoing interface is IFNET_MULTIPAGES
280 * capable. Set this to 1 only for testing/debugging purposes.
282 int sosendjcl_ignore_capab
= 0;
283 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sosendjcl_ignore_capab
,
284 CTLFLAG_RW
| CTLFLAG_LOCKED
, &sosendjcl_ignore_capab
, 0, "");
287 * Set this to ignore SOF1_IF_2KCL and use big clusters for large
288 * writes on the socket for all protocols on any network interfaces.
289 * Be extra careful when setting this to 1, because sending down packets with
290 * clusters larger that 2 KB might lead to system panics or data corruption.
291 * When set to 0, the system will respect SOF1_IF_2KCL, which is set
292 * on the outgoing interface
293 * Set this to 1 for testing/debugging purposes only.
295 int sosendbigcl_ignore_capab
= 0;
296 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sosendbigcl_ignore_capab
,
297 CTLFLAG_RW
| CTLFLAG_LOCKED
, &sosendbigcl_ignore_capab
, 0, "");
299 int sodefunctlog
= 0;
300 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sodefunctlog
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
301 &sodefunctlog
, 0, "");
303 int sothrottlelog
= 0;
304 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sothrottlelog
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
305 &sothrottlelog
, 0, "");
307 int sorestrictrecv
= 1;
308 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sorestrictrecv
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
309 &sorestrictrecv
, 0, "Enable inbound interface restrictions");
311 int sorestrictsend
= 1;
312 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sorestrictsend
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
313 &sorestrictsend
, 0, "Enable outbound interface restrictions");
315 int soreserveheadroom
= 1;
316 SYSCTL_INT(_kern_ipc
, OID_AUTO
, soreserveheadroom
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
317 &soreserveheadroom
, 0, "To allocate contiguous datagram buffers");
319 #if (DEBUG || DEVELOPMENT)
320 int so_notsent_lowat_check
= 1;
321 SYSCTL_INT(_kern_ipc
, OID_AUTO
, notsent_lowat
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
322 &so_notsent_lowat_check
, 0, "enable/disable notsnet lowat check");
323 #endif /* DEBUG || DEVELOPMENT */
325 int so_accept_list_waits
= 0;
326 #if (DEBUG || DEVELOPMENT)
327 SYSCTL_INT(_kern_ipc
, OID_AUTO
, accept_list_waits
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
328 &so_accept_list_waits
, 0, "number of waits for listener incomp list");
329 #endif /* DEBUG || DEVELOPMENT */
331 extern struct inpcbinfo tcbinfo
;
333 /* TODO: these should be in header file */
334 extern int get_inpcb_str_size(void);
335 extern int get_tcp_str_size(void);
337 vm_size_t so_cache_zone_element_size
;
339 static int sodelayed_copy(struct socket
*, struct uio
*, struct mbuf
**,
341 static void cached_sock_alloc(struct socket
**, int);
342 static void cached_sock_free(struct socket
*);
345 * Maximum of extended background idle sockets per process
346 * Set to zero to disable further setting of the option
349 #define SO_IDLE_BK_IDLE_MAX_PER_PROC 1
350 #define SO_IDLE_BK_IDLE_TIME 600
351 #define SO_IDLE_BK_IDLE_RCV_HIWAT 131072
353 struct soextbkidlestat soextbkidlestat
;
355 SYSCTL_UINT(_kern_ipc
, OID_AUTO
, maxextbkidleperproc
,
356 CTLFLAG_RW
| CTLFLAG_LOCKED
, &soextbkidlestat
.so_xbkidle_maxperproc
, 0,
357 "Maximum of extended background idle sockets per process");
359 SYSCTL_UINT(_kern_ipc
, OID_AUTO
, extbkidletime
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
360 &soextbkidlestat
.so_xbkidle_time
, 0,
361 "Time in seconds to keep extended background idle sockets");
363 SYSCTL_UINT(_kern_ipc
, OID_AUTO
, extbkidlercvhiwat
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
364 &soextbkidlestat
.so_xbkidle_rcvhiwat
, 0,
365 "High water mark for extended background idle sockets");
367 SYSCTL_STRUCT(_kern_ipc
, OID_AUTO
, extbkidlestat
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
368 &soextbkidlestat
, soextbkidlestat
, "");
370 int so_set_extended_bk_idle(struct socket
*, int);
374 * SOTCDB_NO_DSCP is set by default, to prevent the networking stack from
375 * setting the DSCP code on the packet based on the service class; see
376 * <rdar://problem/11277343> for details.
378 __private_extern__ u_int32_t sotcdb
= 0;
379 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sotcdb
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
385 _CASSERT(sizeof(so_gencnt
) == sizeof(uint64_t));
386 VERIFY(IS_P2ALIGNED(&so_gencnt
, sizeof(uint32_t)));
389 _CASSERT(sizeof(struct sa_endpoints
) == sizeof(struct user64_sa_endpoints
));
390 _CASSERT(offsetof(struct sa_endpoints
, sae_srcif
) == offsetof(struct user64_sa_endpoints
, sae_srcif
));
391 _CASSERT(offsetof(struct sa_endpoints
, sae_srcaddr
) == offsetof(struct user64_sa_endpoints
, sae_srcaddr
));
392 _CASSERT(offsetof(struct sa_endpoints
, sae_srcaddrlen
) == offsetof(struct user64_sa_endpoints
, sae_srcaddrlen
));
393 _CASSERT(offsetof(struct sa_endpoints
, sae_dstaddr
) == offsetof(struct user64_sa_endpoints
, sae_dstaddr
));
394 _CASSERT(offsetof(struct sa_endpoints
, sae_dstaddrlen
) == offsetof(struct user64_sa_endpoints
, sae_dstaddrlen
));
396 _CASSERT(sizeof(struct sa_endpoints
) == sizeof(struct user32_sa_endpoints
));
397 _CASSERT(offsetof(struct sa_endpoints
, sae_srcif
) == offsetof(struct user32_sa_endpoints
, sae_srcif
));
398 _CASSERT(offsetof(struct sa_endpoints
, sae_srcaddr
) == offsetof(struct user32_sa_endpoints
, sae_srcaddr
));
399 _CASSERT(offsetof(struct sa_endpoints
, sae_srcaddrlen
) == offsetof(struct user32_sa_endpoints
, sae_srcaddrlen
));
400 _CASSERT(offsetof(struct sa_endpoints
, sae_dstaddr
) == offsetof(struct user32_sa_endpoints
, sae_dstaddr
));
401 _CASSERT(offsetof(struct sa_endpoints
, sae_dstaddrlen
) == offsetof(struct user32_sa_endpoints
, sae_dstaddrlen
));
404 if (socketinit_done
) {
405 printf("socketinit: already called...\n");
410 PE_parse_boot_argn("socket_debug", &socket_debug
,
411 sizeof(socket_debug
));
414 * allocate lock group attribute and group for socket cache mutex
416 so_cache_mtx_grp_attr
= lck_grp_attr_alloc_init();
417 so_cache_mtx_grp
= lck_grp_alloc_init("so_cache",
418 so_cache_mtx_grp_attr
);
421 * allocate the lock attribute for socket cache mutex
423 so_cache_mtx_attr
= lck_attr_alloc_init();
425 /* cached sockets mutex */
426 so_cache_mtx
= lck_mtx_alloc_init(so_cache_mtx_grp
, so_cache_mtx_attr
);
427 if (so_cache_mtx
== NULL
) {
428 panic("%s: unable to allocate so_cache_mtx\n", __func__
);
431 STAILQ_INIT(&so_cache_head
);
433 so_cache_zone_element_size
= (vm_size_t
)(sizeof(struct socket
) + 4
434 + get_inpcb_str_size() + 4 + get_tcp_str_size());
436 so_cache_zone
= zinit(so_cache_zone_element_size
,
437 (120000 * so_cache_zone_element_size
), 8192, "socache zone");
438 zone_change(so_cache_zone
, Z_CALLERACCT
, FALSE
);
439 zone_change(so_cache_zone
, Z_NOENCRYPT
, TRUE
);
441 bzero(&soextbkidlestat
, sizeof(struct soextbkidlestat
));
442 soextbkidlestat
.so_xbkidle_maxperproc
= SO_IDLE_BK_IDLE_MAX_PER_PROC
;
443 soextbkidlestat
.so_xbkidle_time
= SO_IDLE_BK_IDLE_TIME
;
444 soextbkidlestat
.so_xbkidle_rcvhiwat
= SO_IDLE_BK_IDLE_RCV_HIWAT
;
448 socket_tclass_init();
451 #endif /* MULTIPATH */
455 cached_sock_alloc(struct socket
**so
, int waitok
)
460 lck_mtx_lock(so_cache_mtx
);
462 if (!STAILQ_EMPTY(&so_cache_head
)) {
463 VERIFY(cached_sock_count
> 0);
465 *so
= STAILQ_FIRST(&so_cache_head
);
466 STAILQ_REMOVE_HEAD(&so_cache_head
, so_cache_ent
);
467 STAILQ_NEXT((*so
), so_cache_ent
) = NULL
;
470 lck_mtx_unlock(so_cache_mtx
);
472 temp
= (*so
)->so_saved_pcb
;
473 bzero((caddr_t
)*so
, sizeof(struct socket
));
475 (*so
)->so_saved_pcb
= temp
;
477 lck_mtx_unlock(so_cache_mtx
);
480 *so
= (struct socket
*)zalloc(so_cache_zone
);
482 *so
= (struct socket
*)zalloc_noblock(so_cache_zone
);
489 bzero((caddr_t
)*so
, sizeof(struct socket
));
492 * Define offsets for extra structures into our
493 * single block of memory. Align extra structures
494 * on longword boundaries.
497 offset
= (uintptr_t)*so
;
498 offset
+= sizeof(struct socket
);
500 offset
= ALIGN(offset
);
502 (*so
)->so_saved_pcb
= (caddr_t
)offset
;
503 offset
+= get_inpcb_str_size();
505 offset
= ALIGN(offset
);
507 ((struct inpcb
*)(void *)(*so
)->so_saved_pcb
)->inp_saved_ppcb
=
511 OSBitOrAtomic(SOF1_CACHED_IN_SOCK_LAYER
, &(*so
)->so_flags1
);
515 cached_sock_free(struct socket
*so
)
517 lck_mtx_lock(so_cache_mtx
);
519 so_cache_time
= net_uptime();
520 if (++cached_sock_count
> max_cached_sock_count
) {
522 lck_mtx_unlock(so_cache_mtx
);
523 zfree(so_cache_zone
, so
);
525 if (so_cache_hw
< cached_sock_count
) {
526 so_cache_hw
= cached_sock_count
;
529 STAILQ_INSERT_TAIL(&so_cache_head
, so
, so_cache_ent
);
531 so
->cache_timestamp
= so_cache_time
;
532 lck_mtx_unlock(so_cache_mtx
);
537 so_update_last_owner_locked(struct socket
*so
, proc_t self
)
539 if (so
->last_pid
!= 0) {
541 * last_pid and last_upid should remain zero for sockets
542 * created using sock_socket. The check above achieves that
544 if (self
== PROC_NULL
) {
545 self
= current_proc();
548 if (so
->last_upid
!= proc_uniqueid(self
) ||
549 so
->last_pid
!= proc_pid(self
)) {
550 so
->last_upid
= proc_uniqueid(self
);
551 so
->last_pid
= proc_pid(self
);
552 proc_getexecutableuuid(self
, so
->last_uuid
,
553 sizeof(so
->last_uuid
));
554 if (so
->so_proto
!= NULL
&& so
->so_proto
->pr_update_last_owner
!= NULL
) {
555 (*so
->so_proto
->pr_update_last_owner
)(so
, self
, NULL
);
558 proc_pidoriginatoruuid(so
->so_vuuid
, sizeof(so
->so_vuuid
));
563 so_update_policy(struct socket
*so
)
565 if (SOCK_DOM(so
) == PF_INET
|| SOCK_DOM(so
) == PF_INET6
) {
566 (void) inp_update_policy(sotoinpcb(so
));
572 so_update_necp_policy(struct socket
*so
, struct sockaddr
*override_local_addr
,
573 struct sockaddr
*override_remote_addr
)
575 if (SOCK_DOM(so
) == PF_INET
|| SOCK_DOM(so
) == PF_INET6
) {
576 inp_update_necp_policy(sotoinpcb(so
), override_local_addr
,
577 override_remote_addr
, 0);
587 boolean_t rc
= FALSE
;
589 lck_mtx_lock(so_cache_mtx
);
591 so_cache_time
= net_uptime();
593 while (!STAILQ_EMPTY(&so_cache_head
)) {
594 VERIFY(cached_sock_count
> 0);
595 p
= STAILQ_FIRST(&so_cache_head
);
596 if ((so_cache_time
- p
->cache_timestamp
) <
597 SO_CACHE_TIME_LIMIT
) {
601 STAILQ_REMOVE_HEAD(&so_cache_head
, so_cache_ent
);
604 zfree(so_cache_zone
, p
);
606 if (++n_freed
>= SO_CACHE_MAX_FREE_BATCH
) {
607 so_cache_max_freed
++;
612 /* Schedule again if there is more to cleanup */
613 if (!STAILQ_EMPTY(&so_cache_head
)) {
617 lck_mtx_unlock(so_cache_mtx
);
622 * Get a socket structure from our zone, and initialize it.
623 * We don't implement `waitok' yet (see comments in uipc_domain.c).
624 * Note that it would probably be better to allocate socket
625 * and PCB at the same time, but I'm not convinced that all
626 * the protocols can be easily modified to do this.
629 soalloc(int waitok
, int dom
, int type
)
633 if ((dom
== PF_INET
) && (type
== SOCK_STREAM
)) {
634 cached_sock_alloc(&so
, waitok
);
636 MALLOC_ZONE(so
, struct socket
*, sizeof(*so
), socket_zone
,
639 bzero(so
, sizeof(*so
));
643 so
->so_gencnt
= OSIncrementAtomic64((SInt64
*)&so_gencnt
);
644 so
->so_zone
= socket_zone
;
647 * Increment the socket allocation statistics
649 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_alloc_total
);
651 #if CONFIG_MACF_SOCKET
652 /* Convert waitok to M_WAITOK/M_NOWAIT for MAC Framework. */
653 if (mac_socket_label_init(so
, !waitok
) != 0) {
657 #endif /* MAC_SOCKET */
664 socreate_internal(int dom
, struct socket
**aso
, int type
, int proto
,
665 struct proc
*p
, uint32_t flags
, struct proc
*ep
)
672 extern int tcpconsdebug
;
679 prp
= pffindproto(dom
, proto
, type
);
681 prp
= pffindtype(dom
, type
);
684 if (prp
== NULL
|| prp
->pr_usrreqs
->pru_attach
== NULL
) {
685 if (pffinddomain(dom
) == NULL
) {
689 if (pffindprotonotype(dom
, proto
) != NULL
) {
693 return EPROTONOSUPPORT
;
695 if (prp
->pr_type
!= type
) {
698 so
= soalloc(1, dom
, type
);
705 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_domain_local_total
);
708 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_domain_inet_total
);
709 if (type
== SOCK_STREAM
) {
710 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_inet_stream_total
);
712 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_inet_dgram_total
);
716 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_domain_route_total
);
719 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_domain_ndrv_total
);
722 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_domain_key_total
);
725 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_domain_inet6_total
);
726 if (type
== SOCK_STREAM
) {
727 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_inet6_stream_total
);
729 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_inet6_dgram_total
);
733 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_domain_system_total
);
736 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_domain_multipath_total
);
739 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_domain_other_total
);
743 if (flags
& SOCF_MPTCP
) {
744 so
->so_state
|= SS_NBIO
;
747 TAILQ_INIT(&so
->so_incomp
);
748 TAILQ_INIT(&so
->so_comp
);
750 so
->last_upid
= proc_uniqueid(p
);
751 so
->last_pid
= proc_pid(p
);
752 proc_getexecutableuuid(p
, so
->last_uuid
, sizeof(so
->last_uuid
));
753 proc_pidoriginatoruuid(so
->so_vuuid
, sizeof(so
->so_vuuid
));
755 if (ep
!= PROC_NULL
&& ep
!= p
) {
756 so
->e_upid
= proc_uniqueid(ep
);
757 so
->e_pid
= proc_pid(ep
);
758 proc_getexecutableuuid(ep
, so
->e_uuid
, sizeof(so
->e_uuid
));
759 so
->so_flags
|= SOF_DELEGATED
;
762 so
->so_cred
= kauth_cred_proc_ref(p
);
763 if (!suser(kauth_cred_get(), NULL
)) {
764 so
->so_state
|= SS_PRIV
;
768 so
->so_rcv
.sb_flags
|= SB_RECV
;
769 so
->so_rcv
.sb_so
= so
->so_snd
.sb_so
= so
;
770 so
->next_lock_lr
= 0;
771 so
->next_unlock_lr
= 0;
773 #if CONFIG_MACF_SOCKET
774 mac_socket_label_associate(kauth_cred_get(), so
);
775 #endif /* MAC_SOCKET */
778 * Attachment will create the per pcb lock if necessary and
779 * increase refcount for creation, make sure it's done before
780 * socket is inserted in lists.
784 error
= (*prp
->pr_usrreqs
->pru_attach
)(so
, proto
, p
);
788 * If so_pcb is not zero, the socket will be leaked,
789 * so protocol attachment handler must be coded carefuly
791 so
->so_state
|= SS_NOFDREF
;
792 VERIFY(so
->so_usecount
> 0);
794 sofreelastref(so
, 1); /* will deallocate the socket */
799 * Note: needs so_pcb to be set after pru_attach
801 if (prp
->pr_update_last_owner
!= NULL
) {
802 (*prp
->pr_update_last_owner
)(so
, p
, ep
);
805 atomic_add_32(&prp
->pr_domain
->dom_refs
, 1);
806 TAILQ_INIT(&so
->so_evlist
);
808 /* Attach socket filters for this protocol */
811 if (tcpconsdebug
== 2) {
812 so
->so_options
|= SO_DEBUG
;
815 so_set_default_traffic_class(so
);
818 * If this thread or task is marked to create backgrounded sockets,
819 * mark the socket as background.
821 if (!(flags
& SOCF_MPTCP
) &&
822 proc_get_effective_thread_policy(current_thread(), TASK_POLICY_NEW_SOCKETS_BG
)) {
823 socket_set_traffic_mgt_flags(so
, TRAFFIC_MGT_SO_BACKGROUND
);
824 so
->so_background_thread
= current_thread();
829 * Don't mark Unix domain, system or multipath sockets as
830 * eligible for defunct by default.
835 so
->so_flags
|= SOF_NODEFUNCT
;
842 * Entitlements can't be checked at socket creation time except if the
843 * application requested a feature guarded by a privilege (c.f., socket
845 * The priv(9) and the Sandboxing APIs are designed with the idea that
846 * a privilege check should only be triggered by a userland request.
847 * A privilege check at socket creation time is time consuming and
848 * could trigger many authorisation error messages from the security
863 * <pru_attach>:ENOBUFS[AF_UNIX]
864 * <pru_attach>:ENOBUFS[TCP]
865 * <pru_attach>:ENOMEM[TCP]
866 * <pru_attach>:??? [other protocol families, IPSEC]
869 socreate(int dom
, struct socket
**aso
, int type
, int proto
)
871 return socreate_internal(dom
, aso
, type
, proto
, current_proc(), 0,
876 socreate_delegate(int dom
, struct socket
**aso
, int type
, int proto
, pid_t epid
)
879 struct proc
*ep
= PROC_NULL
;
881 if ((proc_selfpid() != epid
) && ((ep
= proc_find(epid
)) == PROC_NULL
)) {
886 error
= socreate_internal(dom
, aso
, type
, proto
, current_proc(), 0, ep
);
889 * It might not be wise to hold the proc reference when calling
890 * socreate_internal since it calls soalloc with M_WAITOK
893 if (ep
!= PROC_NULL
) {
902 * <pru_bind>:EINVAL Invalid argument [COMMON_START]
903 * <pru_bind>:EAFNOSUPPORT Address family not supported
904 * <pru_bind>:EADDRNOTAVAIL Address not available.
905 * <pru_bind>:EINVAL Invalid argument
906 * <pru_bind>:EAFNOSUPPORT Address family not supported [notdef]
907 * <pru_bind>:EACCES Permission denied
908 * <pru_bind>:EADDRINUSE Address in use
909 * <pru_bind>:EAGAIN Resource unavailable, try again
910 * <pru_bind>:EPERM Operation not permitted
914 * Notes: It's not possible to fully enumerate the return codes above,
915 * since socket filter authors and protocol family authors may
916 * not choose to limit their error returns to those listed, even
917 * though this may result in some software operating incorrectly.
919 * The error codes which are enumerated above are those known to
920 * be returned by the tcp_usr_bind function supplied.
923 sobindlock(struct socket
*so
, struct sockaddr
*nam
, int dolock
)
925 struct proc
*p
= current_proc();
932 so_update_last_owner_locked(so
, p
);
933 so_update_policy(so
);
936 so_update_necp_policy(so
, nam
, NULL
);
940 * If this is a bind request on a socket that has been marked
941 * as inactive, reject it now before we go any further.
943 if (so
->so_flags
& SOF_DEFUNCT
) {
945 SODEFUNCTLOG("%s[%d, %s]: defunct so 0x%llx [%d,%d] (%d)\n",
946 __func__
, proc_pid(p
), proc_best_name(p
),
947 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
948 SOCK_DOM(so
), SOCK_TYPE(so
), error
);
953 error
= sflt_bind(so
, nam
);
956 error
= (*so
->so_proto
->pr_usrreqs
->pru_bind
)(so
, nam
, p
);
960 socket_unlock(so
, 1);
963 if (error
== EJUSTRETURN
) {
971 sodealloc(struct socket
*so
)
973 kauth_cred_unref(&so
->so_cred
);
975 /* Remove any filters */
979 cfil_sock_detach(so
);
980 #endif /* CONTENT_FILTER */
982 /* Delete the state allocated for msg queues on a socket */
983 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
984 FREE(so
->so_msg_state
, M_TEMP
);
985 so
->so_msg_state
= NULL
;
987 VERIFY(so
->so_msg_state
== NULL
);
989 so
->so_gencnt
= OSIncrementAtomic64((SInt64
*)&so_gencnt
);
991 #if CONFIG_MACF_SOCKET
992 mac_socket_label_destroy(so
);
993 #endif /* MAC_SOCKET */
995 if (so
->so_flags1
& SOF1_CACHED_IN_SOCK_LAYER
) {
996 cached_sock_free(so
);
998 FREE_ZONE(so
, sizeof(*so
), so
->so_zone
);
1003 * Returns: 0 Success
1006 * <pru_listen>:EINVAL[AF_UNIX]
1007 * <pru_listen>:EINVAL[TCP]
1008 * <pru_listen>:EADDRNOTAVAIL[TCP] Address not available.
1009 * <pru_listen>:EINVAL[TCP] Invalid argument
1010 * <pru_listen>:EAFNOSUPPORT[TCP] Address family not supported [notdef]
1011 * <pru_listen>:EACCES[TCP] Permission denied
1012 * <pru_listen>:EADDRINUSE[TCP] Address in use
1013 * <pru_listen>:EAGAIN[TCP] Resource unavailable, try again
1014 * <pru_listen>:EPERM[TCP] Operation not permitted
1017 * Notes: Other <pru_listen> returns depend on the protocol family; all
1018 * <sf_listen> returns depend on what the filter author causes
1019 * their filter to return.
1022 solisten(struct socket
*so
, int backlog
)
1024 struct proc
*p
= current_proc();
1029 so_update_last_owner_locked(so
, p
);
1030 so_update_policy(so
);
1033 so_update_necp_policy(so
, NULL
, NULL
);
1036 if (so
->so_proto
== NULL
) {
1040 if ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) == 0) {
1046 * If the listen request is made on a socket that is not fully
1047 * disconnected, or on a socket that has been marked as inactive,
1048 * reject the request now.
1051 (SS_ISCONNECTED
| SS_ISCONNECTING
| SS_ISDISCONNECTING
)) ||
1052 (so
->so_flags
& SOF_DEFUNCT
)) {
1054 if (so
->so_flags
& SOF_DEFUNCT
) {
1055 SODEFUNCTLOG("%s[%d, %s]: defunct so 0x%llx [%d,%d] "
1056 "(%d)\n", __func__
, proc_pid(p
),
1058 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
1059 SOCK_DOM(so
), SOCK_TYPE(so
), error
);
1064 if ((so
->so_restrictions
& SO_RESTRICT_DENY_IN
) != 0) {
1069 error
= sflt_listen(so
);
1071 error
= (*so
->so_proto
->pr_usrreqs
->pru_listen
)(so
, p
);
1075 if (error
== EJUSTRETURN
) {
1081 if (TAILQ_EMPTY(&so
->so_comp
)) {
1082 so
->so_options
|= SO_ACCEPTCONN
;
1085 * POSIX: The implementation may have an upper limit on the length of
1086 * the listen queue-either global or per accepting socket. If backlog
1087 * exceeds this limit, the length of the listen queue is set to the
1090 * If listen() is called with a backlog argument value that is less
1091 * than 0, the function behaves as if it had been called with a backlog
1092 * argument value of 0.
1094 * A backlog argument of 0 may allow the socket to accept connections,
1095 * in which case the length of the listen queue may be set to an
1096 * implementation-defined minimum value.
1098 if (backlog
<= 0 || backlog
> somaxconn
) {
1099 backlog
= somaxconn
;
1102 so
->so_qlimit
= backlog
;
1104 socket_unlock(so
, 1);
1109 * The "accept list lock" protects the fields related to the listener queues
1110 * because we can unlock a socket to respect the lock ordering between
1111 * the listener socket and its clients sockets. The lock ordering is first to
1112 * acquire the client socket before the listener socket.
1114 * The accept list lock serializes access to the following fields:
1115 * - of the listener socket:
1120 * - of client sockets that are in so_comp or so_incomp:
1124 * As one can see the accept list lock protects the consistent of the
1125 * linkage of the client sockets.
1127 * Note that those fields may be read without holding the accept list lock
1128 * for a preflight provided the accept list lock is taken when committing
1129 * to take an action based on the result of the preflight. The preflight
1130 * saves the cost of doing the unlock/lock dance.
1133 so_acquire_accept_list(struct socket
*head
, struct socket
*so
)
1135 lck_mtx_t
*mutex_held
;
1137 if (head
->so_proto
->pr_getlock
== NULL
) {
1140 mutex_held
= (*head
->so_proto
->pr_getlock
)(head
, PR_F_WILLUNLOCK
);
1141 LCK_MTX_ASSERT(mutex_held
, LCK_MTX_ASSERT_OWNED
);
1143 if (!(head
->so_flags1
& SOF1_ACCEPT_LIST_HELD
)) {
1144 head
->so_flags1
|= SOF1_ACCEPT_LIST_HELD
;
1148 socket_unlock(so
, 0);
1150 while (head
->so_flags1
& SOF1_ACCEPT_LIST_HELD
) {
1151 so_accept_list_waits
+= 1;
1152 msleep((caddr_t
)&head
->so_incomp
, mutex_held
,
1153 PSOCK
| PCATCH
, __func__
, NULL
);
1155 head
->so_flags1
|= SOF1_ACCEPT_LIST_HELD
;
1157 socket_unlock(head
, 0);
1159 socket_lock(head
, 0);
1164 so_release_accept_list(struct socket
*head
)
1166 if (head
->so_proto
->pr_getlock
!= NULL
) {
1167 lck_mtx_t
*mutex_held
;
1169 mutex_held
= (*head
->so_proto
->pr_getlock
)(head
, 0);
1170 LCK_MTX_ASSERT(mutex_held
, LCK_MTX_ASSERT_OWNED
);
1172 head
->so_flags1
&= ~SOF1_ACCEPT_LIST_HELD
;
1173 wakeup((caddr_t
)&head
->so_incomp
);
1178 sofreelastref(struct socket
*so
, int dealloc
)
1180 struct socket
*head
= so
->so_head
;
1182 /* Assume socket is locked */
1184 if (!(so
->so_flags
& SOF_PCBCLEARING
) || !(so
->so_state
& SS_NOFDREF
)) {
1185 selthreadclear(&so
->so_snd
.sb_sel
);
1186 selthreadclear(&so
->so_rcv
.sb_sel
);
1187 so
->so_rcv
.sb_flags
&= ~(SB_SEL
| SB_UPCALL
);
1188 so
->so_snd
.sb_flags
&= ~(SB_SEL
| SB_UPCALL
);
1189 so
->so_event
= sonullevent
;
1194 * Need to lock the listener when the protocol has
1197 if (head
->so_proto
->pr_getlock
!= NULL
) {
1198 socket_lock(head
, 1);
1199 so_acquire_accept_list(head
, so
);
1201 if (so
->so_state
& SS_INCOMP
) {
1202 so
->so_state
&= ~SS_INCOMP
;
1203 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
1208 if (head
->so_proto
->pr_getlock
!= NULL
) {
1209 so_release_accept_list(head
);
1210 socket_unlock(head
, 1);
1212 } else if (so
->so_state
& SS_COMP
) {
1213 if (head
->so_proto
->pr_getlock
!= NULL
) {
1214 so_release_accept_list(head
);
1215 socket_unlock(head
, 1);
1218 * We must not decommission a socket that's
1219 * on the accept(2) queue. If we do, then
1220 * accept(2) may hang after select(2) indicated
1221 * that the listening socket was ready.
1223 selthreadclear(&so
->so_snd
.sb_sel
);
1224 selthreadclear(&so
->so_rcv
.sb_sel
);
1225 so
->so_rcv
.sb_flags
&= ~(SB_SEL
| SB_UPCALL
);
1226 so
->so_snd
.sb_flags
&= ~(SB_SEL
| SB_UPCALL
);
1227 so
->so_event
= sonullevent
;
1230 if (head
->so_proto
->pr_getlock
!= NULL
) {
1231 so_release_accept_list(head
);
1232 socket_unlock(head
, 1);
1234 printf("sofree: not queued\n");
1241 if (so
->so_flags
& SOF_FLOW_DIVERT
) {
1242 flow_divert_detach(so
);
1244 #endif /* FLOW_DIVERT */
1246 /* 3932268: disable upcall */
1247 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
1248 so
->so_snd
.sb_flags
&= ~(SB_UPCALL
| SB_SNDBYTE_CNT
);
1249 so
->so_event
= sonullevent
;
1257 soclose_wait_locked(struct socket
*so
)
1259 lck_mtx_t
*mutex_held
;
1261 if (so
->so_proto
->pr_getlock
!= NULL
) {
1262 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, PR_F_WILLUNLOCK
);
1264 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
1266 LCK_MTX_ASSERT(mutex_held
, LCK_MTX_ASSERT_OWNED
);
1269 * Double check here and return if there's no outstanding upcall;
1270 * otherwise proceed further only if SOF_UPCALLCLOSEWAIT is set.
1272 if (!so
->so_upcallusecount
|| !(so
->so_flags
& SOF_UPCALLCLOSEWAIT
)) {
1275 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
1276 so
->so_snd
.sb_flags
&= ~SB_UPCALL
;
1277 so
->so_flags
|= SOF_CLOSEWAIT
;
1279 (void) msleep((caddr_t
)&so
->so_upcallusecount
, mutex_held
, (PZERO
- 1),
1280 "soclose_wait_locked", NULL
);
1281 LCK_MTX_ASSERT(mutex_held
, LCK_MTX_ASSERT_OWNED
);
1282 so
->so_flags
&= ~SOF_CLOSEWAIT
;
1286 * Close a socket on last file table reference removal.
1287 * Initiate disconnect if connected.
1288 * Free socket when disconnect complete.
1291 soclose_locked(struct socket
*so
)
1296 if (so
->so_usecount
== 0) {
1297 panic("soclose: so=%p refcount=0\n", so
);
1301 sflt_notify(so
, sock_evt_closing
, NULL
);
1303 if (so
->so_upcallusecount
) {
1304 soclose_wait_locked(so
);
1309 * We have to wait until the content filters are done
1311 if ((so
->so_flags
& SOF_CONTENT_FILTER
) != 0) {
1312 cfil_sock_close_wait(so
);
1313 cfil_sock_is_closed(so
);
1314 cfil_sock_detach(so
);
1316 #endif /* CONTENT_FILTER */
1318 if (so
->so_flags1
& SOF1_EXTEND_BK_IDLE_INPROG
) {
1319 soresume(current_proc(), so
, 1);
1320 so
->so_flags1
&= ~SOF1_EXTEND_BK_IDLE_WANTED
;
1323 if ((so
->so_options
& SO_ACCEPTCONN
)) {
1324 struct socket
*sp
, *sonext
;
1325 int persocklock
= 0;
1326 int incomp_overflow_only
;
1329 * We do not want new connection to be added
1330 * to the connection queues
1332 so
->so_options
&= ~SO_ACCEPTCONN
;
1335 * We can drop the lock on the listener once
1336 * we've acquired the incoming list
1338 if (so
->so_proto
->pr_getlock
!= NULL
) {
1340 so_acquire_accept_list(so
, NULL
);
1341 socket_unlock(so
, 0);
1344 incomp_overflow_only
= 1;
1346 TAILQ_FOREACH_SAFE(sp
, &so
->so_incomp
, so_list
, sonext
) {
1349 * skip sockets thrown away by tcpdropdropblreq
1350 * they will get cleanup by the garbage collection.
1351 * otherwise, remove the incomp socket from the queue
1352 * and let soabort trigger the appropriate cleanup.
1354 if (sp
->so_flags
& SOF_OVERFLOW
) {
1358 if (persocklock
!= 0) {
1364 * The extra reference for the list insure the
1365 * validity of the socket pointer when we perform the
1366 * unlock of the head above
1368 if (sp
->so_state
& SS_INCOMP
) {
1369 sp
->so_state
&= ~SS_INCOMP
;
1371 TAILQ_REMOVE(&so
->so_incomp
, sp
, so_list
);
1377 panic("%s sp %p in so_incomp but !SS_INCOMP",
1381 if (persocklock
!= 0) {
1382 socket_unlock(sp
, 1);
1386 TAILQ_FOREACH_SAFE(sp
, &so
->so_comp
, so_list
, sonext
) {
1387 /* Dequeue from so_comp since sofree() won't do it */
1388 if (persocklock
!= 0) {
1392 if (sp
->so_state
& SS_COMP
) {
1393 sp
->so_state
&= ~SS_COMP
;
1395 TAILQ_REMOVE(&so
->so_comp
, sp
, so_list
);
1400 panic("%s sp %p in so_comp but !SS_COMP",
1405 socket_unlock(sp
, 1);
1409 if (incomp_overflow_only
== 0 && !TAILQ_EMPTY(&so
->so_incomp
)) {
1410 #if (DEBUG | DEVELOPMENT)
1411 panic("%s head %p so_comp not empty\n", __func__
, so
);
1412 #endif /* (DEVELOPMENT || DEBUG) */
1417 if (!TAILQ_EMPTY(&so
->so_comp
)) {
1418 #if (DEBUG | DEVELOPMENT)
1419 panic("%s head %p so_comp not empty\n", __func__
, so
);
1420 #endif /* (DEVELOPMENT || DEBUG) */
1427 so_release_accept_list(so
);
1430 if (so
->so_pcb
== NULL
) {
1431 /* 3915887: mark the socket as ready for dealloc */
1432 so
->so_flags
|= SOF_PCBCLEARING
;
1435 if (so
->so_state
& SS_ISCONNECTED
) {
1436 if ((so
->so_state
& SS_ISDISCONNECTING
) == 0) {
1437 error
= sodisconnectlocked(so
);
1442 if (so
->so_options
& SO_LINGER
) {
1443 lck_mtx_t
*mutex_held
;
1445 if ((so
->so_state
& SS_ISDISCONNECTING
) &&
1446 (so
->so_state
& SS_NBIO
)) {
1449 if (so
->so_proto
->pr_getlock
!= NULL
) {
1450 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, PR_F_WILLUNLOCK
);
1452 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
1454 while (so
->so_state
& SS_ISCONNECTED
) {
1455 ts
.tv_sec
= (so
->so_linger
/ 100);
1456 ts
.tv_nsec
= (so
->so_linger
% 100) *
1457 NSEC_PER_USEC
* 1000 * 10;
1458 error
= msleep((caddr_t
)&so
->so_timeo
,
1459 mutex_held
, PSOCK
| PCATCH
, "soclose", &ts
);
1462 * It's OK when the time fires,
1463 * don't report an error
1465 if (error
== EWOULDBLOCK
) {
1474 if (so
->so_usecount
== 0) {
1475 panic("soclose: usecount is zero so=%p\n", so
);
1478 if (so
->so_pcb
!= NULL
&& !(so
->so_flags
& SOF_PCBCLEARING
)) {
1479 int error2
= (*so
->so_proto
->pr_usrreqs
->pru_detach
)(so
);
1484 if (so
->so_usecount
<= 0) {
1485 panic("soclose: usecount is zero so=%p\n", so
);
1489 if (so
->so_pcb
!= NULL
&& !(so
->so_flags
& SOF_MP_SUBFLOW
) &&
1490 (so
->so_state
& SS_NOFDREF
)) {
1491 panic("soclose: NOFDREF");
1494 so
->so_state
|= SS_NOFDREF
;
1496 if ((so
->so_flags
& SOF_KNOTE
) != 0) {
1497 KNOTE(&so
->so_klist
, SO_FILT_HINT_LOCKED
);
1500 atomic_add_32(&so
->so_proto
->pr_domain
->dom_refs
, -1);
1503 VERIFY(so
->so_usecount
> 0);
1510 soclose(struct socket
*so
)
1515 if (so
->so_retaincnt
== 0) {
1516 error
= soclose_locked(so
);
1519 * if the FD is going away, but socket is
1520 * retained in kernel remove its reference
1523 if (so
->so_usecount
< 2) {
1524 panic("soclose: retaincnt non null and so=%p "
1525 "usecount=%d\n", so
, so
->so_usecount
);
1528 socket_unlock(so
, 1);
1533 * Must be called at splnet...
1535 /* Should already be locked */
1537 soabort(struct socket
*so
)
1541 #ifdef MORE_LOCKING_DEBUG
1542 lck_mtx_t
*mutex_held
;
1544 if (so
->so_proto
->pr_getlock
!= NULL
) {
1545 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
1547 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
1549 LCK_MTX_ASSERT(mutex_held
, LCK_MTX_ASSERT_OWNED
);
1552 if ((so
->so_flags
& SOF_ABORTED
) == 0) {
1553 so
->so_flags
|= SOF_ABORTED
;
1554 error
= (*so
->so_proto
->pr_usrreqs
->pru_abort
)(so
);
1564 soacceptlock(struct socket
*so
, struct sockaddr
**nam
, int dolock
)
1572 so_update_last_owner_locked(so
, PROC_NULL
);
1573 so_update_policy(so
);
1575 so_update_necp_policy(so
, NULL
, NULL
);
1578 if ((so
->so_state
& SS_NOFDREF
) == 0) {
1579 panic("soaccept: !NOFDREF");
1581 so
->so_state
&= ~SS_NOFDREF
;
1582 error
= (*so
->so_proto
->pr_usrreqs
->pru_accept
)(so
, nam
);
1585 socket_unlock(so
, 1);
1591 soaccept(struct socket
*so
, struct sockaddr
**nam
)
1593 return soacceptlock(so
, nam
, 1);
1597 soacceptfilter(struct socket
*so
, struct socket
*head
)
1599 struct sockaddr
*local
= NULL
, *remote
= NULL
;
1603 * Hold the lock even if this socket has not been made visible
1604 * to the filter(s). For sockets with global locks, this protects
1605 * against the head or peer going away
1608 if (sogetaddr_locked(so
, &remote
, 1) != 0 ||
1609 sogetaddr_locked(so
, &local
, 0) != 0) {
1610 so
->so_state
&= ~SS_NOFDREF
;
1611 socket_unlock(so
, 1);
1613 /* Out of resources; try it again next time */
1614 error
= ECONNABORTED
;
1618 error
= sflt_accept(head
, so
, local
, remote
);
1621 * If we get EJUSTRETURN from one of the filters, mark this socket
1622 * as inactive and return it anyway. This newly accepted socket
1623 * will be disconnected later before we hand it off to the caller.
1625 if (error
== EJUSTRETURN
) {
1627 (void) sosetdefunct(current_proc(), so
,
1628 SHUTDOWN_SOCKET_LEVEL_DISCONNECT_INTERNAL
, FALSE
);
1633 * This may seem like a duplication to the above error
1634 * handling part when we return ECONNABORTED, except
1635 * the following is done while holding the lock since
1636 * the socket has been exposed to the filter(s) earlier.
1638 so
->so_state
&= ~SS_NOFDREF
;
1639 socket_unlock(so
, 1);
1641 /* Propagate socket filter's error code to the caller */
1643 socket_unlock(so
, 1);
1646 /* Callee checks for NULL pointer */
1647 sock_freeaddr(remote
);
1648 sock_freeaddr(local
);
1653 * Returns: 0 Success
1654 * EOPNOTSUPP Operation not supported on socket
1655 * EISCONN Socket is connected
1656 * <pru_connect>:EADDRNOTAVAIL Address not available.
1657 * <pru_connect>:EINVAL Invalid argument
1658 * <pru_connect>:EAFNOSUPPORT Address family not supported [notdef]
1659 * <pru_connect>:EACCES Permission denied
1660 * <pru_connect>:EADDRINUSE Address in use
1661 * <pru_connect>:EAGAIN Resource unavailable, try again
1662 * <pru_connect>:EPERM Operation not permitted
1663 * <sf_connect_out>:??? [anything a filter writer might set]
1666 soconnectlock(struct socket
*so
, struct sockaddr
*nam
, int dolock
)
1669 struct proc
*p
= current_proc();
1675 so_update_last_owner_locked(so
, p
);
1676 so_update_policy(so
);
1679 so_update_necp_policy(so
, NULL
, nam
);
1683 * If this is a listening socket or if this is a previously-accepted
1684 * socket that has been marked as inactive, reject the connect request.
1686 if ((so
->so_options
& SO_ACCEPTCONN
) || (so
->so_flags
& SOF_DEFUNCT
)) {
1688 if (so
->so_flags
& SOF_DEFUNCT
) {
1689 SODEFUNCTLOG("%s[%d, %s]: defunct so 0x%llx [%d,%d] "
1690 "(%d)\n", __func__
, proc_pid(p
),
1692 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
1693 SOCK_DOM(so
), SOCK_TYPE(so
), error
);
1696 socket_unlock(so
, 1);
1701 if ((so
->so_restrictions
& SO_RESTRICT_DENY_OUT
) != 0) {
1703 socket_unlock(so
, 1);
1709 * If protocol is connection-based, can only connect once.
1710 * Otherwise, if connected, try to disconnect first.
1711 * This allows user to disconnect by connecting to, e.g.,
1714 if (so
->so_state
& (SS_ISCONNECTED
| SS_ISCONNECTING
) &&
1715 ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) ||
1716 (error
= sodisconnectlocked(so
)))) {
1720 * Run connect filter before calling protocol:
1721 * - non-blocking connect returns before completion;
1723 error
= sflt_connectout(so
, nam
);
1725 if (error
== EJUSTRETURN
) {
1729 error
= (*so
->so_proto
->pr_usrreqs
->pru_connect
)
1734 socket_unlock(so
, 1);
1740 soconnect(struct socket
*so
, struct sockaddr
*nam
)
1742 return soconnectlock(so
, nam
, 1);
1746 * Returns: 0 Success
1747 * <pru_connect2>:EINVAL[AF_UNIX]
1748 * <pru_connect2>:EPROTOTYPE[AF_UNIX]
1749 * <pru_connect2>:??? [other protocol families]
1751 * Notes: <pru_connect2> is not supported by [TCP].
1754 soconnect2(struct socket
*so1
, struct socket
*so2
)
1758 socket_lock(so1
, 1);
1759 if (so2
->so_proto
->pr_lock
) {
1760 socket_lock(so2
, 1);
1763 error
= (*so1
->so_proto
->pr_usrreqs
->pru_connect2
)(so1
, so2
);
1765 socket_unlock(so1
, 1);
1766 if (so2
->so_proto
->pr_lock
) {
1767 socket_unlock(so2
, 1);
1773 soconnectxlocked(struct socket
*so
, struct sockaddr
*src
,
1774 struct sockaddr
*dst
, struct proc
*p
, uint32_t ifscope
,
1775 sae_associd_t aid
, sae_connid_t
*pcid
, uint32_t flags
, void *arg
,
1776 uint32_t arglen
, uio_t auio
, user_ssize_t
*bytes_written
)
1780 so_update_last_owner_locked(so
, p
);
1781 so_update_policy(so
);
1784 * If this is a listening socket or if this is a previously-accepted
1785 * socket that has been marked as inactive, reject the connect request.
1787 if ((so
->so_options
& SO_ACCEPTCONN
) || (so
->so_flags
& SOF_DEFUNCT
)) {
1789 if (so
->so_flags
& SOF_DEFUNCT
) {
1790 SODEFUNCTLOG("%s[%d, %s]: defunct so 0x%llx [%d,%d] "
1791 "(%d)\n", __func__
, proc_pid(p
),
1793 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
1794 SOCK_DOM(so
), SOCK_TYPE(so
), error
);
1799 if ((so
->so_restrictions
& SO_RESTRICT_DENY_OUT
) != 0) {
1804 * If protocol is connection-based, can only connect once
1805 * unless PR_MULTICONN is set. Otherwise, if connected,
1806 * try to disconnect first. This allows user to disconnect
1807 * by connecting to, e.g., a null address.
1809 if ((so
->so_state
& (SS_ISCONNECTED
| SS_ISCONNECTING
)) &&
1810 !(so
->so_proto
->pr_flags
& PR_MULTICONN
) &&
1811 ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) ||
1812 (error
= sodisconnectlocked(so
)) != 0)) {
1816 * Run connect filter before calling protocol:
1817 * - non-blocking connect returns before completion;
1819 error
= sflt_connectout(so
, dst
);
1821 /* Disable PRECONNECT_DATA, as we don't need to send a SYN anymore. */
1822 so
->so_flags1
&= ~SOF1_PRECONNECT_DATA
;
1823 if (error
== EJUSTRETURN
) {
1827 error
= (*so
->so_proto
->pr_usrreqs
->pru_connectx
)
1828 (so
, src
, dst
, p
, ifscope
, aid
, pcid
,
1829 flags
, arg
, arglen
, auio
, bytes_written
);
1837 sodisconnectlocked(struct socket
*so
)
1841 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
1845 if (so
->so_state
& SS_ISDISCONNECTING
) {
1850 error
= (*so
->so_proto
->pr_usrreqs
->pru_disconnect
)(so
);
1852 sflt_notify(so
, sock_evt_disconnected
, NULL
);
1859 /* Locking version */
1861 sodisconnect(struct socket
*so
)
1866 error
= sodisconnectlocked(so
);
1867 socket_unlock(so
, 1);
1872 sodisconnectxlocked(struct socket
*so
, sae_associd_t aid
, sae_connid_t cid
)
1877 * Call the protocol disconnectx handler; let it handle all
1878 * matters related to the connection state of this session.
1880 error
= (*so
->so_proto
->pr_usrreqs
->pru_disconnectx
)(so
, aid
, cid
);
1883 * The event applies only for the session, not for
1884 * the disconnection of individual subflows.
1886 if (so
->so_state
& (SS_ISDISCONNECTING
| SS_ISDISCONNECTED
)) {
1887 sflt_notify(so
, sock_evt_disconnected
, NULL
);
1894 sodisconnectx(struct socket
*so
, sae_associd_t aid
, sae_connid_t cid
)
1899 error
= sodisconnectxlocked(so
, aid
, cid
);
1900 socket_unlock(so
, 1);
1904 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
1907 * sosendcheck will lock the socket buffer if it isn't locked and
1908 * verify that there is space for the data being inserted.
1910 * Returns: 0 Success
1912 * sblock:EWOULDBLOCK
1919 sosendcheck(struct socket
*so
, struct sockaddr
*addr
, user_ssize_t resid
,
1920 int32_t clen
, int32_t atomic
, int flags
, int *sblocked
,
1921 struct mbuf
*control
)
1928 if (*sblocked
== 0) {
1929 if ((so
->so_snd
.sb_flags
& SB_LOCK
) != 0 &&
1930 so
->so_send_filt_thread
!= 0 &&
1931 so
->so_send_filt_thread
== current_thread()) {
1933 * We're being called recursively from a filter,
1934 * allow this to continue. Radar 4150520.
1935 * Don't set sblocked because we don't want
1936 * to perform an unlock later.
1940 error
= sblock(&so
->so_snd
, SBLOCKWAIT(flags
));
1942 if (so
->so_flags
& SOF_DEFUNCT
) {
1952 * If a send attempt is made on a socket that has been marked
1953 * as inactive (disconnected), reject the request.
1955 if (so
->so_flags
& SOF_DEFUNCT
) {
1958 SODEFUNCTLOG("%s[%d, %s]: defunct so 0x%llx [%d,%d] (%d)\n",
1959 __func__
, proc_selfpid(), proc_best_name(current_proc()),
1960 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
1961 SOCK_DOM(so
), SOCK_TYPE(so
), error
);
1965 if (so
->so_state
& SS_CANTSENDMORE
) {
1968 * Can re-inject data of half closed connections
1970 if ((so
->so_state
& SS_ISDISCONNECTED
) == 0 &&
1971 so
->so_snd
.sb_cfil_thread
== current_thread() &&
1972 cfil_sock_data_pending(&so
->so_snd
) != 0) {
1974 "so %llx ignore SS_CANTSENDMORE",
1975 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
));
1977 #endif /* CONTENT_FILTER */
1981 error
= so
->so_error
;
1986 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
1987 if ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) != 0) {
1988 if (((so
->so_state
& SS_ISCONFIRMING
) == 0) &&
1989 (resid
!= 0 || clen
== 0) &&
1990 !(so
->so_flags1
& SOF1_PRECONNECT_DATA
)) {
1993 } else if (addr
== 0) {
1994 return (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) ?
1995 ENOTCONN
: EDESTADDRREQ
;
1999 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2000 space
= msgq_sbspace(so
, control
);
2002 space
= sbspace(&so
->so_snd
);
2005 if (flags
& MSG_OOB
) {
2008 if ((atomic
&& resid
> so
->so_snd
.sb_hiwat
) ||
2009 clen
> so
->so_snd
.sb_hiwat
) {
2013 if ((space
< resid
+ clen
&&
2014 (atomic
|| (space
< (int32_t)so
->so_snd
.sb_lowat
) ||
2016 (so
->so_type
== SOCK_STREAM
&& so_wait_for_if_feedback(so
))) {
2018 * don't block the connectx call when there's more data
2019 * than can be copied.
2021 if (so
->so_flags1
& SOF1_PRECONNECT_DATA
) {
2025 if (space
< (int32_t)so
->so_snd
.sb_lowat
) {
2029 if ((so
->so_state
& SS_NBIO
) || (flags
& MSG_NBIO
) ||
2033 sbunlock(&so
->so_snd
, TRUE
); /* keep socket locked */
2035 error
= sbwait(&so
->so_snd
);
2037 if (so
->so_flags
& SOF_DEFUNCT
) {
2049 * If send must go all at once and message is larger than
2050 * send buffering, then hard error.
2051 * Lock against other senders.
2052 * If must go all at once and not enough room now, then
2053 * inform user that this would block and do nothing.
2054 * Otherwise, if nonblocking, send as much as possible.
2055 * The data to be sent is described by "uio" if nonzero,
2056 * otherwise by the mbuf chain "top" (which must be null
2057 * if uio is not). Data provided in mbuf chain must be small
2058 * enough to send all at once.
2060 * Returns nonzero on error, timeout or signal; callers
2061 * must check for short counts if EINTR/ERESTART are returned.
2062 * Data and control buffers are freed on return.
2064 * Returns: 0 Success
2070 * sosendcheck:EWOULDBLOCK
2074 * sosendcheck:??? [value from so_error]
2075 * <pru_send>:ECONNRESET[TCP]
2076 * <pru_send>:EINVAL[TCP]
2077 * <pru_send>:ENOBUFS[TCP]
2078 * <pru_send>:EADDRINUSE[TCP]
2079 * <pru_send>:EADDRNOTAVAIL[TCP]
2080 * <pru_send>:EAFNOSUPPORT[TCP]
2081 * <pru_send>:EACCES[TCP]
2082 * <pru_send>:EAGAIN[TCP]
2083 * <pru_send>:EPERM[TCP]
2084 * <pru_send>:EMSGSIZE[TCP]
2085 * <pru_send>:EHOSTUNREACH[TCP]
2086 * <pru_send>:ENETUNREACH[TCP]
2087 * <pru_send>:ENETDOWN[TCP]
2088 * <pru_send>:ENOMEM[TCP]
2089 * <pru_send>:ENOBUFS[TCP]
2090 * <pru_send>:???[TCP] [ignorable: mostly IPSEC/firewall/DLIL]
2091 * <pru_send>:EINVAL[AF_UNIX]
2092 * <pru_send>:EOPNOTSUPP[AF_UNIX]
2093 * <pru_send>:EPIPE[AF_UNIX]
2094 * <pru_send>:ENOTCONN[AF_UNIX]
2095 * <pru_send>:EISCONN[AF_UNIX]
2096 * <pru_send>:???[AF_UNIX] [whatever a filter author chooses]
2097 * <sf_data_out>:??? [whatever a filter author chooses]
2099 * Notes: Other <pru_send> returns depend on the protocol family; all
2100 * <sf_data_out> returns depend on what the filter author causes
2101 * their filter to return.
2104 sosend(struct socket
*so
, struct sockaddr
*addr
, struct uio
*uio
,
2105 struct mbuf
*top
, struct mbuf
*control
, int flags
)
2108 struct mbuf
*m
, *freelist
= NULL
;
2109 user_ssize_t space
, len
, resid
, orig_resid
;
2110 int clen
= 0, error
, dontroute
, mlen
, sendflags
;
2111 int atomic
= sosendallatonce(so
) || top
;
2113 struct proc
*p
= current_proc();
2114 struct mbuf
*control_copy
= NULL
;
2115 uint16_t headroom
= 0;
2116 boolean_t en_tracing
= FALSE
;
2119 resid
= uio_resid(uio
);
2121 resid
= top
->m_pkthdr
.len
;
2124 KERNEL_DEBUG((DBG_FNC_SOSEND
| DBG_FUNC_START
), so
, resid
,
2125 so
->so_snd
.sb_cc
, so
->so_snd
.sb_lowat
, so
->so_snd
.sb_hiwat
);
2130 * trace if tracing & network (vs. unix) sockets & and
2133 if (ENTR_SHOULDTRACE
&&
2134 (SOCK_CHECK_DOM(so
, AF_INET
) || SOCK_CHECK_DOM(so
, AF_INET6
))) {
2135 struct inpcb
*inp
= sotoinpcb(so
);
2136 if (inp
->inp_last_outifp
!= NULL
&&
2137 !(inp
->inp_last_outifp
->if_flags
& IFF_LOOPBACK
)) {
2139 KERNEL_ENERGYTRACE(kEnTrActKernSockWrite
, DBG_FUNC_START
,
2140 VM_KERNEL_ADDRPERM(so
),
2141 ((so
->so_state
& SS_NBIO
) ? kEnTrFlagNonBlocking
: 0),
2148 * Re-injection should not affect process accounting
2150 if ((flags
& MSG_SKIPCFIL
) == 0) {
2151 so_update_last_owner_locked(so
, p
);
2152 so_update_policy(so
);
2155 so_update_necp_policy(so
, NULL
, addr
);
2159 if (so
->so_type
!= SOCK_STREAM
&& (flags
& MSG_OOB
) != 0) {
2165 * In theory resid should be unsigned.
2166 * However, space must be signed, as it might be less than 0
2167 * if we over-committed, and we must use a signed comparison
2168 * of space and resid. On the other hand, a negative resid
2169 * causes us to loop sending 0-length segments to the protocol.
2171 * Usually, MSG_EOR isn't used on SOCK_STREAM type sockets.
2172 * But it will be used by sockets doing message delivery.
2174 * Note: We limit resid to be a positive int value as we use
2175 * imin() to set bytes_to_copy -- radr://14558484
2177 if (resid
< 0 || resid
> INT_MAX
|| (so
->so_type
== SOCK_STREAM
&&
2178 !(so
->so_flags
& SOF_ENABLE_MSGS
) && (flags
& MSG_EOR
))) {
2183 dontroute
= (flags
& MSG_DONTROUTE
) &&
2184 (so
->so_options
& SO_DONTROUTE
) == 0 &&
2185 (so
->so_proto
->pr_flags
& PR_ATOMIC
);
2186 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_msgsnd
);
2188 if (control
!= NULL
) {
2189 clen
= control
->m_len
;
2192 if (soreserveheadroom
!= 0) {
2193 headroom
= so
->so_pktheadroom
;
2197 error
= sosendcheck(so
, addr
, resid
, clen
, atomic
, flags
,
2198 &sblocked
, control
);
2204 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2205 space
= msgq_sbspace(so
, control
);
2207 space
= sbspace(&so
->so_snd
) - clen
;
2209 space
+= ((flags
& MSG_OOB
) ? 1024 : 0);
2214 * Data is prepackaged in "top".
2217 if (flags
& MSG_EOR
) {
2218 top
->m_flags
|= M_EOR
;
2227 bytes_to_copy
= imin(resid
, space
);
2229 bytes_to_alloc
= bytes_to_copy
;
2231 bytes_to_alloc
+= headroom
;
2234 if (sosendminchain
> 0) {
2237 chainlength
= sosendmaxchain
;
2241 * Use big 4 KB cluster when the outgoing interface
2242 * does not prefer 2 KB clusters
2244 bigcl
= !(so
->so_flags1
& SOF1_IF_2KCL
) ||
2245 sosendbigcl_ignore_capab
;
2248 * Attempt to use larger than system page-size
2249 * clusters for large writes only if there is
2250 * a jumbo cluster pool and if the socket is
2251 * marked accordingly.
2253 jumbocl
= sosendjcl
&& njcl
> 0 &&
2254 ((so
->so_flags
& SOF_MULTIPAGES
) ||
2255 sosendjcl_ignore_capab
) &&
2258 socket_unlock(so
, 0);
2262 int hdrs_needed
= (top
== NULL
) ? 1 : 0;
2265 * try to maintain a local cache of mbuf
2266 * clusters needed to complete this
2267 * write the list is further limited to
2268 * the number that are currently needed
2269 * to fill the socket this mechanism
2270 * allows a large number of mbufs/
2271 * clusters to be grabbed under a single
2272 * mbuf lock... if we can't get any
2273 * clusters, than fall back to trying
2274 * for mbufs if we fail early (or
2275 * miscalcluate the number needed) make
2276 * sure to release any clusters we
2277 * haven't yet consumed.
2279 if (freelist
== NULL
&&
2280 bytes_to_alloc
> MBIGCLBYTES
&&
2283 bytes_to_alloc
/ M16KCLBYTES
;
2285 if ((bytes_to_alloc
-
2286 (num_needed
* M16KCLBYTES
))
2292 m_getpackets_internal(
2293 (unsigned int *)&num_needed
,
2294 hdrs_needed
, M_WAIT
, 0,
2297 * Fall back to 4K cluster size
2298 * if allocation failed
2302 if (freelist
== NULL
&&
2303 bytes_to_alloc
> MCLBYTES
&&
2306 bytes_to_alloc
/ MBIGCLBYTES
;
2308 if ((bytes_to_alloc
-
2309 (num_needed
* MBIGCLBYTES
)) >=
2315 m_getpackets_internal(
2316 (unsigned int *)&num_needed
,
2317 hdrs_needed
, M_WAIT
, 0,
2320 * Fall back to cluster size
2321 * if allocation failed
2326 * Allocate a cluster as we want to
2327 * avoid to split the data in more
2328 * that one segment and using MINCLSIZE
2329 * would lead us to allocate two mbufs
2331 if (soreserveheadroom
!= 0 &&
2334 bytes_to_alloc
> _MHLEN
) ||
2335 bytes_to_alloc
> _MLEN
)) {
2336 num_needed
= ROUNDUP(bytes_to_alloc
, MCLBYTES
) /
2339 m_getpackets_internal(
2340 (unsigned int *)&num_needed
,
2341 hdrs_needed
, M_WAIT
, 0,
2344 * Fall back to a single mbuf
2345 * if allocation failed
2347 } else if (freelist
== NULL
&&
2348 bytes_to_alloc
> MINCLSIZE
) {
2350 bytes_to_alloc
/ MCLBYTES
;
2352 if ((bytes_to_alloc
-
2353 (num_needed
* MCLBYTES
)) >=
2359 m_getpackets_internal(
2360 (unsigned int *)&num_needed
,
2361 hdrs_needed
, M_WAIT
, 0,
2364 * Fall back to a single mbuf
2365 * if allocation failed
2369 * For datagram protocols, leave
2370 * headroom for protocol headers
2371 * in the first cluster of the chain
2373 if (freelist
!= NULL
&& atomic
&&
2374 top
== NULL
&& headroom
> 0) {
2375 freelist
->m_data
+= headroom
;
2379 * Fall back to regular mbufs without
2380 * reserving the socket headroom
2382 if (freelist
== NULL
) {
2391 if (freelist
== NULL
) {
2397 * For datagram protocols,
2398 * leave room for protocol
2399 * headers in first mbuf.
2401 if (atomic
&& top
== NULL
&&
2402 bytes_to_copy
< MHLEN
) {
2408 freelist
= m
->m_next
;
2411 if ((m
->m_flags
& M_EXT
)) {
2412 mlen
= m
->m_ext
.ext_size
-
2414 } else if ((m
->m_flags
& M_PKTHDR
)) {
2416 MHLEN
- M_LEADINGSPACE(m
);
2418 mlen
= MLEN
- M_LEADINGSPACE(m
);
2420 len
= imin(mlen
, bytes_to_copy
);
2426 error
= uiomove(mtod(m
, caddr_t
),
2429 resid
= uio_resid(uio
);
2433 top
->m_pkthdr
.len
+= len
;
2439 if (flags
& MSG_EOR
) {
2440 top
->m_flags
|= M_EOR
;
2444 bytes_to_copy
= min(resid
, space
);
2445 } while (space
> 0 &&
2446 (chainlength
< sosendmaxchain
|| atomic
||
2447 resid
< MINCLSIZE
));
2457 so
->so_options
|= SO_DONTROUTE
;
2461 * Compute flags here, for pru_send and NKEs
2463 * If the user set MSG_EOF, the protocol
2464 * understands this flag and nothing left to
2465 * send then use PRU_SEND_EOF instead of PRU_SEND.
2467 sendflags
= (flags
& MSG_OOB
) ? PRUS_OOB
:
2468 ((flags
& MSG_EOF
) &&
2469 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) &&
2470 (resid
<= 0)) ? PRUS_EOF
:
2471 /* If there is more to send set PRUS_MORETOCOME */
2472 (resid
> 0 && space
> 0) ? PRUS_MORETOCOME
: 0;
2474 if ((flags
& MSG_SKIPCFIL
) == 0) {
2476 * Socket filter processing
2478 error
= sflt_data_out(so
, addr
, &top
,
2479 &control
, (sendflags
& MSG_OOB
) ?
2480 sock_data_filt_flag_oob
: 0);
2482 if (error
== EJUSTRETURN
) {
2492 * Content filter processing
2494 error
= cfil_sock_data_out(so
, addr
, top
,
2495 control
, sendflags
);
2497 if (error
== EJUSTRETURN
) {
2505 #endif /* CONTENT_FILTER */
2507 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2509 * Make a copy of control mbuf,
2510 * so that msg priority can be
2511 * passed to subsequent mbufs.
2513 control_copy
= m_dup(control
, M_NOWAIT
);
2515 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)
2516 (so
, sendflags
, top
, addr
, control
, p
);
2519 so
->so_options
&= ~SO_DONTROUTE
;
2523 control
= control_copy
;
2524 control_copy
= NULL
;
2530 } while (resid
&& space
> 0);
2535 sbunlock(&so
->so_snd
, FALSE
); /* will unlock socket */
2537 socket_unlock(so
, 1);
2542 if (control
!= NULL
) {
2545 if (freelist
!= NULL
) {
2546 m_freem_list(freelist
);
2548 if (control_copy
!= NULL
) {
2549 m_freem(control_copy
);
2552 soclearfastopen(so
);
2555 /* resid passed here is the bytes left in uio */
2556 KERNEL_ENERGYTRACE(kEnTrActKernSockWrite
, DBG_FUNC_END
,
2557 VM_KERNEL_ADDRPERM(so
),
2558 ((error
== EWOULDBLOCK
) ? kEnTrFlagNoWork
: 0),
2559 (int64_t)(orig_resid
- resid
));
2561 KERNEL_DEBUG(DBG_FNC_SOSEND
| DBG_FUNC_END
, so
, resid
,
2562 so
->so_snd
.sb_cc
, space
, error
);
2568 sosend_reinject(struct socket
*so
, struct sockaddr
*addr
, struct mbuf
*top
, struct mbuf
*control
, uint32_t sendflags
)
2570 struct mbuf
*m0
= NULL
, *control_end
= NULL
;
2572 socket_lock_assert_owned(so
);
2575 * top must points to mbuf chain to be sent.
2576 * If control is not NULL, top must be packet header
2578 VERIFY(top
!= NULL
&&
2579 (control
== NULL
|| top
->m_flags
& M_PKTHDR
));
2582 * If control is not passed in, see if we can get it
2585 if (control
== NULL
&& (top
->m_flags
& M_PKTHDR
) == 0) {
2586 // Locate start of control if present and start of data
2587 for (m0
= top
; m0
!= NULL
; m0
= m0
->m_next
) {
2588 if (m0
->m_flags
& M_PKTHDR
) {
2591 } else if (m0
->m_type
== MT_CONTROL
) {
2592 if (control
== NULL
) {
2593 // Found start of control
2596 if (control
!= NULL
&& m0
->m_next
!= NULL
&& m0
->m_next
->m_type
!= MT_CONTROL
) {
2597 // Found end of control
2602 if (control_end
!= NULL
) {
2603 control_end
->m_next
= NULL
;
2607 int error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)
2608 (so
, sendflags
, top
, addr
, control
, current_proc());
2614 * Supported only connected sockets (no address) without ancillary data
2615 * (control mbuf) for atomic protocols
2618 sosend_list(struct socket
*so
, struct uio
**uioarray
, u_int uiocnt
, int flags
)
2620 struct mbuf
*m
, *freelist
= NULL
;
2621 user_ssize_t len
, resid
;
2622 int error
, dontroute
, mlen
;
2623 int atomic
= sosendallatonce(so
);
2625 struct proc
*p
= current_proc();
2628 struct mbuf
*top
= NULL
;
2629 uint16_t headroom
= 0;
2632 KERNEL_DEBUG((DBG_FNC_SOSEND_LIST
| DBG_FUNC_START
), so
, uiocnt
,
2633 so
->so_snd
.sb_cc
, so
->so_snd
.sb_lowat
, so
->so_snd
.sb_hiwat
);
2635 if (so
->so_type
!= SOCK_DGRAM
) {
2643 if (so
->so_proto
->pr_usrreqs
->pru_send_list
== NULL
) {
2644 error
= EPROTONOSUPPORT
;
2647 if (flags
& ~(MSG_DONTWAIT
| MSG_NBIO
)) {
2651 resid
= uio_array_resid(uioarray
, uiocnt
);
2654 * In theory resid should be unsigned.
2655 * However, space must be signed, as it might be less than 0
2656 * if we over-committed, and we must use a signed comparison
2657 * of space and resid. On the other hand, a negative resid
2658 * causes us to loop sending 0-length segments to the protocol.
2660 * Note: We limit resid to be a positive int value as we use
2661 * imin() to set bytes_to_copy -- radr://14558484
2663 if (resid
< 0 || resid
> INT_MAX
) {
2669 so_update_last_owner_locked(so
, p
);
2670 so_update_policy(so
);
2673 so_update_necp_policy(so
, NULL
, NULL
);
2676 dontroute
= (flags
& MSG_DONTROUTE
) &&
2677 (so
->so_options
& SO_DONTROUTE
) == 0 &&
2678 (so
->so_proto
->pr_flags
& PR_ATOMIC
);
2679 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_msgsnd
);
2681 error
= sosendcheck(so
, NULL
, resid
, 0, atomic
, flags
,
2688 * Use big 4 KB clusters when the outgoing interface does not prefer
2691 bigcl
= !(so
->so_flags1
& SOF1_IF_2KCL
) || sosendbigcl_ignore_capab
;
2693 if (soreserveheadroom
!= 0) {
2694 headroom
= so
->so_pktheadroom
;
2701 size_t maxpktlen
= 0;
2704 if (sosendminchain
> 0) {
2707 chainlength
= sosendmaxchain
;
2710 socket_unlock(so
, 0);
2713 * Find a set of uio that fit in a reasonable number
2716 for (i
= uiofirst
; i
< uiocnt
; i
++) {
2717 struct uio
*auio
= uioarray
[i
];
2719 len
= uio_resid(auio
);
2721 /* Do nothing for empty messages */
2729 if (len
> maxpktlen
) {
2734 if (chainlength
> sosendmaxchain
) {
2739 * Nothing left to send
2741 if (num_needed
== 0) {
2746 * Allocate buffer large enough to include headroom space for
2747 * network and link header
2750 bytes_to_alloc
= maxpktlen
+ headroom
;
2753 * Allocate a single contiguous buffer of the smallest available
2754 * size when possible
2756 if (bytes_to_alloc
> MCLBYTES
&&
2757 bytes_to_alloc
<= MBIGCLBYTES
&& bigcl
) {
2758 freelist
= m_getpackets_internal(
2759 (unsigned int *)&num_needed
,
2760 num_needed
, M_WAIT
, 1,
2762 } else if (bytes_to_alloc
> _MHLEN
&&
2763 bytes_to_alloc
<= MCLBYTES
) {
2764 freelist
= m_getpackets_internal(
2765 (unsigned int *)&num_needed
,
2766 num_needed
, M_WAIT
, 1,
2769 freelist
= m_allocpacket_internal(
2770 (unsigned int *)&num_needed
,
2771 bytes_to_alloc
, NULL
, M_WAIT
, 1, 0);
2774 if (freelist
== NULL
) {
2780 * Copy each uio of the set into its own mbuf packet
2782 for (i
= uiofirst
, m
= freelist
;
2783 i
< uiolast
&& m
!= NULL
;
2787 struct uio
*auio
= uioarray
[i
];
2789 bytes_to_copy
= uio_resid(auio
);
2791 /* Do nothing for empty messages */
2792 if (bytes_to_copy
== 0) {
2796 * Leave headroom for protocol headers
2797 * in the first mbuf of the chain
2799 m
->m_data
+= headroom
;
2801 for (n
= m
; n
!= NULL
; n
= n
->m_next
) {
2802 if ((m
->m_flags
& M_EXT
)) {
2803 mlen
= m
->m_ext
.ext_size
-
2805 } else if ((m
->m_flags
& M_PKTHDR
)) {
2807 MHLEN
- M_LEADINGSPACE(m
);
2809 mlen
= MLEN
- M_LEADINGSPACE(m
);
2811 len
= imin(mlen
, bytes_to_copy
);
2814 * Note: uiomove() decrements the iovec
2817 error
= uiomove(mtod(n
, caddr_t
),
2823 m
->m_pkthdr
.len
+= len
;
2825 VERIFY(m
->m_pkthdr
.len
<= maxpktlen
);
2827 bytes_to_copy
-= len
;
2830 if (m
->m_pkthdr
.len
== 0) {
2832 "%s:%d so %llx pkt %llx type %u len null\n",
2834 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
2835 (uint64_t)DEBUG_KERNEL_ADDRPERM(m
),
2853 so
->so_options
|= SO_DONTROUTE
;
2856 if ((flags
& MSG_SKIPCFIL
) == 0) {
2857 struct mbuf
**prevnextp
= NULL
;
2859 for (i
= uiofirst
, m
= top
;
2860 i
< uiolast
&& m
!= NULL
;
2862 struct mbuf
*nextpkt
= m
->m_nextpkt
;
2865 * Socket filter processing
2867 error
= sflt_data_out(so
, NULL
, &m
,
2869 if (error
!= 0 && error
!= EJUSTRETURN
) {
2876 * Content filter processing
2878 error
= cfil_sock_data_out(so
, NULL
, m
,
2880 if (error
!= 0 && error
!= EJUSTRETURN
) {
2884 #endif /* CONTENT_FILTER */
2886 * Remove packet from the list when
2887 * swallowed by a filter
2889 if (error
== EJUSTRETURN
) {
2891 if (prevnextp
!= NULL
) {
2892 *prevnextp
= nextpkt
;
2900 prevnextp
= &m
->m_nextpkt
;
2905 error
= (*so
->so_proto
->pr_usrreqs
->pru_send_list
)
2906 (so
, 0, top
, NULL
, NULL
, p
);
2910 so
->so_options
&= ~SO_DONTROUTE
;
2915 } while (resid
> 0 && error
== 0);
2918 sbunlock(&so
->so_snd
, FALSE
); /* will unlock socket */
2920 socket_unlock(so
, 1);
2926 if (freelist
!= NULL
) {
2927 m_freem_list(freelist
);
2930 KERNEL_DEBUG(DBG_FNC_SOSEND_LIST
| DBG_FUNC_END
, so
, resid
,
2931 so
->so_snd
.sb_cc
, 0, error
);
2937 * May return ERESTART when packet is dropped by MAC policy check
2940 soreceive_addr(struct proc
*p
, struct socket
*so
, struct sockaddr
**psa
,
2941 int flags
, struct mbuf
**mp
, struct mbuf
**nextrecordp
, int canwait
)
2944 struct mbuf
*m
= *mp
;
2945 struct mbuf
*nextrecord
= *nextrecordp
;
2947 KASSERT(m
->m_type
== MT_SONAME
, ("receive 1a"));
2948 #if CONFIG_MACF_SOCKET_SUBSET
2950 * Call the MAC framework for policy checking if we're in
2951 * the user process context and the socket isn't connected.
2953 if (p
!= kernproc
&& !(so
->so_state
& SS_ISCONNECTED
)) {
2954 struct mbuf
*m0
= m
;
2956 * Dequeue this record (temporarily) from the receive
2957 * list since we're about to drop the socket's lock
2958 * where a new record may arrive and be appended to
2959 * the list. Upon MAC policy failure, the record
2960 * will be freed. Otherwise, we'll add it back to
2961 * the head of the list. We cannot rely on SB_LOCK
2962 * because append operation uses the socket's lock.
2965 m
->m_nextpkt
= NULL
;
2966 sbfree(&so
->so_rcv
, m
);
2968 } while (m
!= NULL
);
2970 so
->so_rcv
.sb_mb
= nextrecord
;
2971 SB_EMPTY_FIXUP(&so
->so_rcv
);
2972 SBLASTRECORDCHK(&so
->so_rcv
, "soreceive 1a");
2973 SBLASTMBUFCHK(&so
->so_rcv
, "soreceive 1a");
2974 socket_unlock(so
, 0);
2976 if (mac_socket_check_received(proc_ucred(p
), so
,
2977 mtod(m
, struct sockaddr
*)) != 0) {
2979 * MAC policy failure; free this record and
2980 * process the next record (or block until
2981 * one is available). We have adjusted sb_cc
2982 * and sb_mbcnt above so there is no need to
2983 * call sbfree() again.
2987 * Clear SB_LOCK but don't unlock the socket.
2988 * Process the next record or wait for one.
2991 sbunlock(&so
->so_rcv
, TRUE
); /* stay locked */
2997 * If the socket has been defunct'd, drop it.
2999 if (so
->so_flags
& SOF_DEFUNCT
) {
3005 * Re-adjust the socket receive list and re-enqueue
3006 * the record in front of any packets which may have
3007 * been appended while we dropped the lock.
3009 for (m
= m0
; m
->m_next
!= NULL
; m
= m
->m_next
) {
3010 sballoc(&so
->so_rcv
, m
);
3012 sballoc(&so
->so_rcv
, m
);
3013 if (so
->so_rcv
.sb_mb
== NULL
) {
3014 so
->so_rcv
.sb_lastrecord
= m0
;
3015 so
->so_rcv
.sb_mbtail
= m
;
3018 nextrecord
= m
->m_nextpkt
= so
->so_rcv
.sb_mb
;
3019 so
->so_rcv
.sb_mb
= m
;
3020 SBLASTRECORDCHK(&so
->so_rcv
, "soreceive 1b");
3021 SBLASTMBUFCHK(&so
->so_rcv
, "soreceive 1b");
3023 #endif /* CONFIG_MACF_SOCKET_SUBSET */
3025 *psa
= dup_sockaddr(mtod(m
, struct sockaddr
*), canwait
);
3026 if ((*psa
== NULL
) && (flags
& MSG_NEEDSA
)) {
3027 error
= EWOULDBLOCK
;
3031 if (flags
& MSG_PEEK
) {
3034 sbfree(&so
->so_rcv
, m
);
3035 if (m
->m_next
== NULL
&& so
->so_rcv
.sb_cc
!= 0) {
3036 panic("%s: about to create invalid socketbuf",
3040 MFREE(m
, so
->so_rcv
.sb_mb
);
3041 m
= so
->so_rcv
.sb_mb
;
3043 m
->m_nextpkt
= nextrecord
;
3045 so
->so_rcv
.sb_mb
= nextrecord
;
3046 SB_EMPTY_FIXUP(&so
->so_rcv
);
3051 *nextrecordp
= nextrecord
;
3057 * Process one or more MT_CONTROL mbufs present before any data mbufs
3058 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we
3059 * just copy the data; if !MSG_PEEK, we call into the protocol to
3060 * perform externalization.
3063 soreceive_ctl(struct socket
*so
, struct mbuf
**controlp
, int flags
,
3064 struct mbuf
**mp
, struct mbuf
**nextrecordp
)
3067 struct mbuf
*cm
= NULL
, *cmn
;
3068 struct mbuf
**cme
= &cm
;
3069 struct sockbuf
*sb_rcv
= &so
->so_rcv
;
3070 struct mbuf
**msgpcm
= NULL
;
3071 struct mbuf
*m
= *mp
;
3072 struct mbuf
*nextrecord
= *nextrecordp
;
3073 struct protosw
*pr
= so
->so_proto
;
3076 * Externalizing the control messages would require us to
3077 * drop the socket's lock below. Once we re-acquire the
3078 * lock, the mbuf chain might change. In order to preserve
3079 * consistency, we unlink all control messages from the
3080 * first mbuf chain in one shot and link them separately
3081 * onto a different chain.
3084 if (flags
& MSG_PEEK
) {
3085 if (controlp
!= NULL
) {
3086 if (*controlp
== NULL
) {
3089 *controlp
= m_copy(m
, 0, m
->m_len
);
3092 * If we failed to allocate an mbuf,
3093 * release any previously allocated
3094 * mbufs for control data. Return
3095 * an error. Keep the mbufs in the
3096 * socket as this is using
3099 if (*controlp
== NULL
) {
3104 controlp
= &(*controlp
)->m_next
;
3108 m
->m_nextpkt
= NULL
;
3110 sb_rcv
->sb_mb
= m
->m_next
;
3113 cme
= &(*cme
)->m_next
;
3116 } while (m
!= NULL
&& m
->m_type
== MT_CONTROL
);
3118 if (!(flags
& MSG_PEEK
)) {
3119 if (sb_rcv
->sb_mb
!= NULL
) {
3120 sb_rcv
->sb_mb
->m_nextpkt
= nextrecord
;
3122 sb_rcv
->sb_mb
= nextrecord
;
3123 SB_EMPTY_FIXUP(sb_rcv
);
3125 if (nextrecord
== NULL
) {
3126 sb_rcv
->sb_lastrecord
= m
;
3130 SBLASTRECORDCHK(&so
->so_rcv
, "soreceive ctl");
3131 SBLASTMBUFCHK(&so
->so_rcv
, "soreceive ctl");
3133 while (cm
!= NULL
) {
3138 cmsg_type
= mtod(cm
, struct cmsghdr
*)->cmsg_type
;
3141 * Call the protocol to externalize SCM_RIGHTS message
3142 * and return the modified message to the caller upon
3143 * success. Otherwise, all other control messages are
3144 * returned unmodified to the caller. Note that we
3145 * only get into this loop if MSG_PEEK is not set.
3147 if (pr
->pr_domain
->dom_externalize
!= NULL
&&
3148 cmsg_type
== SCM_RIGHTS
) {
3150 * Release socket lock: see 3903171. This
3151 * would also allow more records to be appended
3152 * to the socket buffer. We still have SB_LOCK
3153 * set on it, so we can be sure that the head
3154 * of the mbuf chain won't change.
3156 socket_unlock(so
, 0);
3157 error
= (*pr
->pr_domain
->dom_externalize
)(cm
);
3163 if (controlp
!= NULL
&& error
== 0) {
3165 controlp
= &(*controlp
)->m_next
;
3172 * Update the value of nextrecord in case we received new
3173 * records when the socket was unlocked above for
3174 * externalizing SCM_RIGHTS.
3177 nextrecord
= sb_rcv
->sb_mb
->m_nextpkt
;
3179 nextrecord
= sb_rcv
->sb_mb
;
3184 *nextrecordp
= nextrecord
;
3190 * Implement receive operations on a socket.
3191 * We depend on the way that records are added to the sockbuf
3192 * by sbappend*. In particular, each record (mbufs linked through m_next)
3193 * must begin with an address if the protocol so specifies,
3194 * followed by an optional mbuf or mbufs containing ancillary data,
3195 * and then zero or more mbufs of data.
3196 * In order to avoid blocking network interrupts for the entire time here,
3197 * we splx() while doing the actual copy to user space.
3198 * Although the sockbuf is locked, new data may still be appended,
3199 * and thus we must maintain consistency of the sockbuf during that time.
3201 * The caller may receive the data as a single mbuf chain by supplying
3202 * an mbuf **mp0 for use in returning the chain. The uio is then used
3203 * only for the count in uio_resid.
3205 * Returns: 0 Success
3210 * sblock:EWOULDBLOCK
3214 * sodelayed_copy:EFAULT
3215 * <pru_rcvoob>:EINVAL[TCP]
3216 * <pru_rcvoob>:EWOULDBLOCK[TCP]
3218 * <pr_domain->dom_externalize>:EMSGSIZE[AF_UNIX]
3219 * <pr_domain->dom_externalize>:ENOBUFS[AF_UNIX]
3220 * <pr_domain->dom_externalize>:???
3222 * Notes: Additional return values from calls through <pru_rcvoob> and
3223 * <pr_domain->dom_externalize> depend on protocols other than
3224 * TCP or AF_UNIX, which are documented above.
3227 soreceive(struct socket
*so
, struct sockaddr
**psa
, struct uio
*uio
,
3228 struct mbuf
**mp0
, struct mbuf
**controlp
, int *flagsp
)
3230 struct mbuf
*m
, **mp
, *ml
= NULL
;
3231 struct mbuf
*nextrecord
, *free_list
;
3232 int flags
, error
, offset
;
3234 struct protosw
*pr
= so
->so_proto
;
3236 user_ssize_t orig_resid
= uio_resid(uio
);
3237 user_ssize_t delayed_copy_len
;
3240 struct proc
*p
= current_proc();
3241 boolean_t en_tracing
= FALSE
;
3244 * Sanity check on the length passed by caller as we are making 'int'
3247 if (orig_resid
< 0 || orig_resid
> INT_MAX
) {
3251 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_START
, so
,
3252 uio_resid(uio
), so
->so_rcv
.sb_cc
, so
->so_rcv
.sb_lowat
,
3253 so
->so_rcv
.sb_hiwat
);
3256 so_update_last_owner_locked(so
, p
);
3257 so_update_policy(so
);
3259 #ifdef MORE_LOCKING_DEBUG
3260 if (so
->so_usecount
== 1) {
3261 panic("%s: so=%x no other reference on socket\n", __func__
, so
);
3269 if (controlp
!= NULL
) {
3272 if (flagsp
!= NULL
) {
3273 flags
= *flagsp
& ~MSG_EOR
;
3279 * If a recv attempt is made on a previously-accepted socket
3280 * that has been marked as inactive (disconnected), reject
3283 if (so
->so_flags
& SOF_DEFUNCT
) {
3284 struct sockbuf
*sb
= &so
->so_rcv
;
3287 SODEFUNCTLOG("%s[%d, %s]: defunct so 0x%llx [%d,%d] (%d)\n",
3288 __func__
, proc_pid(p
), proc_best_name(p
),
3289 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
3290 SOCK_DOM(so
), SOCK_TYPE(so
), error
);
3292 * This socket should have been disconnected and flushed
3293 * prior to being returned from sodefunct(); there should
3294 * be no data on its receive list, so panic otherwise.
3296 if (so
->so_state
& SS_DEFUNCT
) {
3297 sb_empty_assert(sb
, __func__
);
3299 socket_unlock(so
, 1);
3303 if ((so
->so_flags1
& SOF1_PRECONNECT_DATA
) &&
3304 pr
->pr_usrreqs
->pru_preconnect
) {
3306 * A user may set the CONNECT_RESUME_ON_READ_WRITE-flag but not
3307 * calling write() right after this. *If* the app calls a read
3308 * we do not want to block this read indefinetely. Thus,
3309 * we trigger a connect so that the session gets initiated.
3311 error
= (*pr
->pr_usrreqs
->pru_preconnect
)(so
);
3314 socket_unlock(so
, 1);
3319 if (ENTR_SHOULDTRACE
&&
3320 (SOCK_CHECK_DOM(so
, AF_INET
) || SOCK_CHECK_DOM(so
, AF_INET6
))) {
3322 * enable energy tracing for inet sockets that go over
3323 * non-loopback interfaces only.
3325 struct inpcb
*inp
= sotoinpcb(so
);
3326 if (inp
->inp_last_outifp
!= NULL
&&
3327 !(inp
->inp_last_outifp
->if_flags
& IFF_LOOPBACK
)) {
3329 KERNEL_ENERGYTRACE(kEnTrActKernSockRead
, DBG_FUNC_START
,
3330 VM_KERNEL_ADDRPERM(so
),
3331 ((so
->so_state
& SS_NBIO
) ?
3332 kEnTrFlagNonBlocking
: 0),
3333 (int64_t)orig_resid
);
3338 * When SO_WANTOOBFLAG is set we try to get out-of-band data
3339 * regardless of the flags argument. Here is the case were
3340 * out-of-band data is not inline.
3342 if ((flags
& MSG_OOB
) ||
3343 ((so
->so_options
& SO_WANTOOBFLAG
) != 0 &&
3344 (so
->so_options
& SO_OOBINLINE
) == 0 &&
3345 (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
)))) {
3346 m
= m_get(M_WAIT
, MT_DATA
);
3348 socket_unlock(so
, 1);
3349 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
,
3350 ENOBUFS
, 0, 0, 0, 0);
3353 error
= (*pr
->pr_usrreqs
->pru_rcvoob
)(so
, m
, flags
& MSG_PEEK
);
3357 socket_unlock(so
, 0);
3359 error
= uiomove(mtod(m
, caddr_t
),
3360 imin(uio_resid(uio
), m
->m_len
), uio
);
3362 } while (uio_resid(uio
) && error
== 0 && m
!= NULL
);
3369 if ((so
->so_options
& SO_WANTOOBFLAG
) != 0) {
3370 if (error
== EWOULDBLOCK
|| error
== EINVAL
) {
3372 * Let's try to get normal data:
3373 * EWOULDBLOCK: out-of-band data not
3374 * receive yet. EINVAL: out-of-band data
3379 } else if (error
== 0 && flagsp
!= NULL
) {
3383 socket_unlock(so
, 1);
3385 KERNEL_ENERGYTRACE(kEnTrActKernSockRead
, DBG_FUNC_END
,
3386 VM_KERNEL_ADDRPERM(so
), 0,
3387 (int64_t)(orig_resid
- uio_resid(uio
)));
3389 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,
3399 if (so
->so_state
& SS_ISCONFIRMING
&& uio_resid(uio
)) {
3400 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, 0);
3404 delayed_copy_len
= 0;
3406 #ifdef MORE_LOCKING_DEBUG
3407 if (so
->so_usecount
<= 1) {
3408 printf("soreceive: sblock so=0x%llx ref=%d on socket\n",
3409 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
), so
->so_usecount
);
3413 * See if the socket has been closed (SS_NOFDREF|SS_CANTRCVMORE)
3414 * and if so just return to the caller. This could happen when
3415 * soreceive() is called by a socket upcall function during the
3416 * time the socket is freed. The socket buffer would have been
3417 * locked across the upcall, therefore we cannot put this thread
3418 * to sleep (else we will deadlock) or return EWOULDBLOCK (else
3419 * we may livelock), because the lock on the socket buffer will
3420 * only be released when the upcall routine returns to its caller.
3421 * Because the socket has been officially closed, there can be
3422 * no further read on it.
3424 * A multipath subflow socket would have its SS_NOFDREF set by
3425 * default, so check for SOF_MP_SUBFLOW socket flag; when the
3426 * socket is closed for real, SOF_MP_SUBFLOW would be cleared.
3428 if ((so
->so_state
& (SS_NOFDREF
| SS_CANTRCVMORE
)) ==
3429 (SS_NOFDREF
| SS_CANTRCVMORE
) && !(so
->so_flags
& SOF_MP_SUBFLOW
)) {
3430 socket_unlock(so
, 1);
3434 error
= sblock(&so
->so_rcv
, SBLOCKWAIT(flags
));
3436 socket_unlock(so
, 1);
3437 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,
3440 KERNEL_ENERGYTRACE(kEnTrActKernSockRead
, DBG_FUNC_END
,
3441 VM_KERNEL_ADDRPERM(so
), 0,
3442 (int64_t)(orig_resid
- uio_resid(uio
)));
3447 m
= so
->so_rcv
.sb_mb
;
3449 * If we have less data than requested, block awaiting more
3450 * (subject to any timeout) if:
3451 * 1. the current count is less than the low water mark, or
3452 * 2. MSG_WAITALL is set, and it is possible to do the entire
3453 * receive operation at once if we block (resid <= hiwat).
3454 * 3. MSG_DONTWAIT is not set
3455 * If MSG_WAITALL is set but resid is larger than the receive buffer,
3456 * we have to do the receive in sections, and thus risk returning
3457 * a short count if a timeout or signal occurs after we start.
3459 if (m
== NULL
|| (((flags
& MSG_DONTWAIT
) == 0 &&
3460 so
->so_rcv
.sb_cc
< uio_resid(uio
)) &&
3461 (so
->so_rcv
.sb_cc
< so
->so_rcv
.sb_lowat
||
3462 ((flags
& MSG_WAITALL
) && uio_resid(uio
) <= so
->so_rcv
.sb_hiwat
)) &&
3463 m
->m_nextpkt
== NULL
&& (pr
->pr_flags
& PR_ATOMIC
) == 0)) {
3465 * Panic if we notice inconsistencies in the socket's
3466 * receive list; both sb_mb and sb_cc should correctly
3467 * reflect the contents of the list, otherwise we may
3468 * end up with false positives during select() or poll()
3469 * which could put the application in a bad state.
3471 SB_MB_CHECK(&so
->so_rcv
);
3477 error
= so
->so_error
;
3478 if ((flags
& MSG_PEEK
) == 0) {
3483 if (so
->so_state
& SS_CANTRCVMORE
) {
3486 * Deal with half closed connections
3488 if ((so
->so_state
& SS_ISDISCONNECTED
) == 0 &&
3489 cfil_sock_data_pending(&so
->so_rcv
) != 0) {
3491 "so %llx ignore SS_CANTRCVMORE",
3492 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
));
3494 #endif /* CONTENT_FILTER */
3501 for (; m
!= NULL
; m
= m
->m_next
) {
3502 if (m
->m_type
== MT_OOBDATA
|| (m
->m_flags
& M_EOR
)) {
3503 m
= so
->so_rcv
.sb_mb
;
3507 if ((so
->so_state
& (SS_ISCONNECTED
| SS_ISCONNECTING
)) == 0 &&
3508 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)) {
3512 if (uio_resid(uio
) == 0) {
3516 if ((so
->so_state
& SS_NBIO
) ||
3517 (flags
& (MSG_DONTWAIT
| MSG_NBIO
))) {
3518 error
= EWOULDBLOCK
;
3521 SBLASTRECORDCHK(&so
->so_rcv
, "soreceive sbwait 1");
3522 SBLASTMBUFCHK(&so
->so_rcv
, "soreceive sbwait 1");
3523 sbunlock(&so
->so_rcv
, TRUE
); /* keep socket locked */
3524 #if EVEN_MORE_LOCKING_DEBUG
3526 printf("Waiting for socket data\n");
3530 error
= sbwait(&so
->so_rcv
);
3531 #if EVEN_MORE_LOCKING_DEBUG
3533 printf("SORECEIVE - sbwait returned %d\n", error
);
3536 if (so
->so_usecount
< 1) {
3537 panic("%s: after 2nd sblock so=%p ref=%d on socket\n",
3538 __func__
, so
, so
->so_usecount
);
3542 socket_unlock(so
, 1);
3543 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,
3546 KERNEL_ENERGYTRACE(kEnTrActKernSockRead
, DBG_FUNC_END
,
3547 VM_KERNEL_ADDRPERM(so
), 0,
3548 (int64_t)(orig_resid
- uio_resid(uio
)));
3555 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_msgrcv
);
3556 SBLASTRECORDCHK(&so
->so_rcv
, "soreceive 1");
3557 SBLASTMBUFCHK(&so
->so_rcv
, "soreceive 1");
3558 nextrecord
= m
->m_nextpkt
;
3560 if ((pr
->pr_flags
& PR_ADDR
) && m
->m_type
== MT_SONAME
) {
3561 error
= soreceive_addr(p
, so
, psa
, flags
, &m
, &nextrecord
,
3563 if (error
== ERESTART
) {
3565 } else if (error
!= 0) {
3572 * Process one or more MT_CONTROL mbufs present before any data mbufs
3573 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we
3574 * just copy the data; if !MSG_PEEK, we call into the protocol to
3575 * perform externalization.
3577 if (m
!= NULL
&& m
->m_type
== MT_CONTROL
) {
3578 error
= soreceive_ctl(so
, controlp
, flags
, &m
, &nextrecord
);
3586 * If the socket is a TCP socket with message delivery
3587 * enabled, then create a control msg to deliver the
3588 * relative TCP sequence number for this data. Waiting
3589 * until this point will protect against failures to
3590 * allocate an mbuf for control msgs.
3592 if (so
->so_type
== SOCK_STREAM
&& SOCK_PROTO(so
) == IPPROTO_TCP
&&
3593 (so
->so_flags
& SOF_ENABLE_MSGS
) && controlp
!= NULL
) {
3594 struct mbuf
*seq_cm
;
3596 seq_cm
= sbcreatecontrol((caddr_t
)&m
->m_pkthdr
.msg_seq
,
3597 sizeof(uint32_t), SCM_SEQNUM
, SOL_SOCKET
);
3598 if (seq_cm
== NULL
) {
3599 /* unable to allocate a control mbuf */
3604 controlp
= &seq_cm
->m_next
;
3608 if (!(flags
& MSG_PEEK
)) {
3610 * We get here because m points to an mbuf following
3611 * any MT_SONAME or MT_CONTROL mbufs which have been
3612 * processed above. In any case, m should be pointing
3613 * to the head of the mbuf chain, and the nextrecord
3614 * should be either NULL or equal to m->m_nextpkt.
3615 * See comments above about SB_LOCK.
3617 if (m
!= so
->so_rcv
.sb_mb
||
3618 m
->m_nextpkt
!= nextrecord
) {
3619 panic("%s: post-control !sync so=%p m=%p "
3620 "nextrecord=%p\n", __func__
, so
, m
,
3624 if (nextrecord
== NULL
) {
3625 so
->so_rcv
.sb_lastrecord
= m
;
3629 if (type
== MT_OOBDATA
) {
3633 if (!(flags
& MSG_PEEK
)) {
3634 SB_EMPTY_FIXUP(&so
->so_rcv
);
3637 SBLASTRECORDCHK(&so
->so_rcv
, "soreceive 2");
3638 SBLASTMBUFCHK(&so
->so_rcv
, "soreceive 2");
3643 if (!(flags
& MSG_PEEK
) && uio_resid(uio
) > sorecvmincopy
) {
3652 (uio_resid(uio
) - delayed_copy_len
) > 0 && error
== 0) {
3653 if (m
->m_type
== MT_OOBDATA
) {
3654 if (type
!= MT_OOBDATA
) {
3657 } else if (type
== MT_OOBDATA
) {
3661 * Make sure to allways set MSG_OOB event when getting
3662 * out of band data inline.
3664 if ((so
->so_options
& SO_WANTOOBFLAG
) != 0 &&
3665 (so
->so_options
& SO_OOBINLINE
) != 0 &&
3666 (so
->so_state
& SS_RCVATMARK
) != 0) {
3669 so
->so_state
&= ~SS_RCVATMARK
;
3670 len
= uio_resid(uio
) - delayed_copy_len
;
3671 if (so
->so_oobmark
&& len
> so
->so_oobmark
- offset
) {
3672 len
= so
->so_oobmark
- offset
;
3674 if (len
> m
->m_len
- moff
) {
3675 len
= m
->m_len
- moff
;
3678 * If mp is set, just pass back the mbufs.
3679 * Otherwise copy them out via the uio, then free.
3680 * Sockbuf must be consistent here (points to current mbuf,
3681 * it points to next record) when we drop priority;
3682 * we must note any additions to the sockbuf when we
3683 * block interrupts again.
3686 SBLASTRECORDCHK(&so
->so_rcv
, "soreceive uiomove");
3687 SBLASTMBUFCHK(&so
->so_rcv
, "soreceive uiomove");
3688 if (can_delay
&& len
== m
->m_len
) {
3690 * only delay the copy if we're consuming the
3691 * mbuf and we're NOT in MSG_PEEK mode
3692 * and we have enough data to make it worthwile
3693 * to drop and retake the lock... can_delay
3694 * reflects the state of the 2 latter
3695 * constraints moff should always be zero
3698 delayed_copy_len
+= len
;
3700 if (delayed_copy_len
) {
3701 error
= sodelayed_copy(so
, uio
,
3702 &free_list
, &delayed_copy_len
);
3708 * can only get here if MSG_PEEK is not
3709 * set therefore, m should point at the
3710 * head of the rcv queue; if it doesn't,
3711 * it means something drastically
3712 * changed while we were out from behind
3713 * the lock in sodelayed_copy. perhaps
3714 * a RST on the stream. in any event,
3715 * the stream has been interrupted. it's
3716 * probably best just to return whatever
3717 * data we've moved and let the caller
3720 if (m
!= so
->so_rcv
.sb_mb
) {
3724 socket_unlock(so
, 0);
3725 error
= uiomove(mtod(m
, caddr_t
) + moff
,
3734 uio_setresid(uio
, (uio_resid(uio
) - len
));
3736 if (len
== m
->m_len
- moff
) {
3737 if (m
->m_flags
& M_EOR
) {
3740 if (flags
& MSG_PEEK
) {
3744 nextrecord
= m
->m_nextpkt
;
3745 sbfree(&so
->so_rcv
, m
);
3746 m
->m_nextpkt
= NULL
;
3749 * If this packet is an unordered packet
3750 * (indicated by M_UNORDERED_DATA flag), remove
3751 * the additional bytes added to the
3752 * receive socket buffer size.
3754 if ((so
->so_flags
& SOF_ENABLE_MSGS
) &&
3756 (m
->m_flags
& M_UNORDERED_DATA
) &&
3757 sbreserve(&so
->so_rcv
,
3758 so
->so_rcv
.sb_hiwat
- m
->m_len
)) {
3759 if (so
->so_msg_state
->msg_uno_bytes
>
3762 msg_uno_bytes
-= m
->m_len
;
3767 m
->m_flags
&= ~M_UNORDERED_DATA
;
3773 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
3776 if (free_list
== NULL
) {
3782 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
3786 m
->m_nextpkt
= nextrecord
;
3787 if (nextrecord
== NULL
) {
3788 so
->so_rcv
.sb_lastrecord
= m
;
3791 so
->so_rcv
.sb_mb
= nextrecord
;
3792 SB_EMPTY_FIXUP(&so
->so_rcv
);
3794 SBLASTRECORDCHK(&so
->so_rcv
, "soreceive 3");
3795 SBLASTMBUFCHK(&so
->so_rcv
, "soreceive 3");
3798 if (flags
& MSG_PEEK
) {
3804 if (flags
& MSG_DONTWAIT
) {
3805 copy_flag
= M_DONTWAIT
;
3809 *mp
= m_copym(m
, 0, len
, copy_flag
);
3811 * Failed to allocate an mbuf?
3812 * Adjust uio_resid back, it was
3813 * adjusted down by len bytes which
3814 * we didn't copy over.
3818 (uio_resid(uio
) + len
));
3824 so
->so_rcv
.sb_cc
-= len
;
3827 if (so
->so_oobmark
) {
3828 if ((flags
& MSG_PEEK
) == 0) {
3829 so
->so_oobmark
-= len
;
3830 if (so
->so_oobmark
== 0) {
3831 so
->so_state
|= SS_RCVATMARK
;
3833 * delay posting the actual event until
3834 * after any delayed copy processing
3842 if (offset
== so
->so_oobmark
) {
3847 if (flags
& MSG_EOR
) {
3851 * If the MSG_WAITALL or MSG_WAITSTREAM flag is set
3852 * (for non-atomic socket), we must not quit until
3853 * "uio->uio_resid == 0" or an error termination.
3854 * If a signal/timeout occurs, return with a short
3855 * count but without error. Keep sockbuf locked
3856 * against other readers.
3858 while (flags
& (MSG_WAITALL
| MSG_WAITSTREAM
) && m
== NULL
&&
3859 (uio_resid(uio
) - delayed_copy_len
) > 0 &&
3860 !sosendallatonce(so
) && !nextrecord
) {
3861 if (so
->so_error
|| ((so
->so_state
& SS_CANTRCVMORE
)
3863 && cfil_sock_data_pending(&so
->so_rcv
) == 0
3864 #endif /* CONTENT_FILTER */
3870 * Depending on the protocol (e.g. TCP), the following
3871 * might cause the socket lock to be dropped and later
3872 * be reacquired, and more data could have arrived and
3873 * have been appended to the receive socket buffer by
3874 * the time it returns. Therefore, we only sleep in
3875 * sbwait() below if and only if the socket buffer is
3876 * empty, in order to avoid a false sleep.
3878 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
&&
3879 (((struct inpcb
*)so
->so_pcb
)->inp_state
!=
3880 INPCB_STATE_DEAD
)) {
3881 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, flags
);
3884 SBLASTRECORDCHK(&so
->so_rcv
, "soreceive sbwait 2");
3885 SBLASTMBUFCHK(&so
->so_rcv
, "soreceive sbwait 2");
3887 if (so
->so_rcv
.sb_mb
== NULL
&& sbwait(&so
->so_rcv
)) {
3892 * have to wait until after we get back from the sbwait
3893 * to do the copy because we will drop the lock if we
3894 * have enough data that has been delayed... by dropping
3895 * the lock we open up a window allowing the netisr
3896 * thread to process the incoming packets and to change
3897 * the state of this socket... we're issuing the sbwait
3898 * because the socket is empty and we're expecting the
3899 * netisr thread to wake us up when more packets arrive;
3900 * if we allow that processing to happen and then sbwait
3901 * we could stall forever with packets sitting in the
3902 * socket if no further packets arrive from the remote
3905 * we want to copy before we've collected all the data
3906 * to satisfy this request to allow the copy to overlap
3907 * the incoming packet processing on an MP system
3909 if (delayed_copy_len
> sorecvmincopy
&&
3910 (delayed_copy_len
> (so
->so_rcv
.sb_hiwat
/ 2))) {
3911 error
= sodelayed_copy(so
, uio
,
3912 &free_list
, &delayed_copy_len
);
3918 m
= so
->so_rcv
.sb_mb
;
3920 nextrecord
= m
->m_nextpkt
;
3922 SB_MB_CHECK(&so
->so_rcv
);
3925 #ifdef MORE_LOCKING_DEBUG
3926 if (so
->so_usecount
<= 1) {
3927 panic("%s: after big while so=%p ref=%d on socket\n",
3928 __func__
, so
, so
->so_usecount
);
3933 if (m
!= NULL
&& pr
->pr_flags
& PR_ATOMIC
) {
3934 if (so
->so_options
& SO_DONTTRUNC
) {
3935 flags
|= MSG_RCVMORE
;
3938 if ((flags
& MSG_PEEK
) == 0) {
3939 (void) sbdroprecord(&so
->so_rcv
);
3945 * pru_rcvd below (for TCP) may cause more data to be received
3946 * if the socket lock is dropped prior to sending the ACK; some
3947 * legacy OpenTransport applications don't handle this well
3948 * (if it receives less data than requested while MSG_HAVEMORE
3949 * is set), and so we set the flag now based on what we know
3950 * prior to calling pru_rcvd.
3952 if ((so
->so_options
& SO_WANTMORE
) && so
->so_rcv
.sb_cc
> 0) {
3953 flags
|= MSG_HAVEMORE
;
3956 if ((flags
& MSG_PEEK
) == 0) {
3958 so
->so_rcv
.sb_mb
= nextrecord
;
3960 * First part is an inline SB_EMPTY_FIXUP(). Second
3961 * part makes sure sb_lastrecord is up-to-date if
3962 * there is still data in the socket buffer.
3964 if (so
->so_rcv
.sb_mb
== NULL
) {
3965 so
->so_rcv
.sb_mbtail
= NULL
;
3966 so
->so_rcv
.sb_lastrecord
= NULL
;
3967 } else if (nextrecord
->m_nextpkt
== NULL
) {
3968 so
->so_rcv
.sb_lastrecord
= nextrecord
;
3970 SB_MB_CHECK(&so
->so_rcv
);
3972 SBLASTRECORDCHK(&so
->so_rcv
, "soreceive 4");
3973 SBLASTMBUFCHK(&so
->so_rcv
, "soreceive 4");
3974 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
) {
3975 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, flags
);
3979 if (delayed_copy_len
) {
3980 error
= sodelayed_copy(so
, uio
, &free_list
, &delayed_copy_len
);
3985 if (free_list
!= NULL
) {
3986 m_freem_list(free_list
);
3990 postevent(so
, 0, EV_OOB
);
3993 if (orig_resid
== uio_resid(uio
) && orig_resid
&&
3994 (flags
& MSG_EOR
) == 0 && (so
->so_state
& SS_CANTRCVMORE
) == 0) {
3995 sbunlock(&so
->so_rcv
, TRUE
); /* keep socket locked */
3999 if (flagsp
!= NULL
) {
4003 #ifdef MORE_LOCKING_DEBUG
4004 if (so
->so_usecount
<= 1) {
4005 panic("%s: release so=%p ref=%d on socket\n", __func__
,
4006 so
, so
->so_usecount
);
4010 if (delayed_copy_len
) {
4011 error
= sodelayed_copy(so
, uio
, &free_list
, &delayed_copy_len
);
4014 if (free_list
!= NULL
) {
4015 m_freem_list(free_list
);
4018 sbunlock(&so
->so_rcv
, FALSE
); /* will unlock socket */
4021 KERNEL_ENERGYTRACE(kEnTrActKernSockRead
, DBG_FUNC_END
,
4022 VM_KERNEL_ADDRPERM(so
),
4023 ((error
== EWOULDBLOCK
) ? kEnTrFlagNoWork
: 0),
4024 (int64_t)(orig_resid
- uio_resid(uio
)));
4026 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, so
, uio_resid(uio
),
4027 so
->so_rcv
.sb_cc
, 0, error
);
4033 * Returns: 0 Success
4037 sodelayed_copy(struct socket
*so
, struct uio
*uio
, struct mbuf
**free_list
,
4038 user_ssize_t
*resid
)
4045 socket_unlock(so
, 0);
4047 while (m
!= NULL
&& error
== 0) {
4048 error
= uiomove(mtod(m
, caddr_t
), (int)m
->m_len
, uio
);
4051 m_freem_list(*free_list
);
4062 sodelayed_copy_list(struct socket
*so
, struct recv_msg_elem
*msgarray
,
4063 u_int uiocnt
, struct mbuf
**free_list
, user_ssize_t
*resid
)
4067 struct mbuf
*ml
, *m
;
4071 for (ml
= *free_list
, i
= 0; ml
!= NULL
&& i
< uiocnt
;
4072 ml
= ml
->m_nextpkt
, i
++) {
4073 auio
= msgarray
[i
].uio
;
4074 for (m
= ml
; m
!= NULL
; m
= m
->m_next
) {
4075 error
= uiomove(mtod(m
, caddr_t
), m
->m_len
, auio
);
4082 m_freem_list(*free_list
);
4091 soreceive_list(struct socket
*so
, struct recv_msg_elem
*msgarray
, u_int uiocnt
,
4095 struct mbuf
*nextrecord
;
4096 struct mbuf
*ml
= NULL
, *free_list
= NULL
, *free_tail
= NULL
;
4098 user_ssize_t len
, pktlen
, delayed_copy_len
= 0;
4099 struct protosw
*pr
= so
->so_proto
;
4101 struct proc
*p
= current_proc();
4102 struct uio
*auio
= NULL
;
4105 struct sockaddr
**psa
= NULL
;
4106 struct mbuf
**controlp
= NULL
;
4109 struct mbuf
*free_others
= NULL
;
4111 KERNEL_DEBUG(DBG_FNC_SORECEIVE_LIST
| DBG_FUNC_START
,
4113 so
->so_rcv
.sb_cc
, so
->so_rcv
.sb_lowat
, so
->so_rcv
.sb_hiwat
);
4117 * - Only supports don't wait flags
4118 * - Only support datagram sockets (could be extended to raw)
4120 * - Protocol must support packet chains
4121 * - The uio array is NULL (should we panic?)
4123 if (flagsp
!= NULL
) {
4128 if (flags
& ~(MSG_PEEK
| MSG_WAITALL
| MSG_DONTWAIT
| MSG_NEEDSA
|
4130 printf("%s invalid flags 0x%x\n", __func__
, flags
);
4134 if (so
->so_type
!= SOCK_DGRAM
) {
4138 if (sosendallatonce(so
) == 0) {
4142 if (so
->so_proto
->pr_usrreqs
->pru_send_list
== NULL
) {
4143 error
= EPROTONOSUPPORT
;
4146 if (msgarray
== NULL
) {
4147 printf("%s uioarray is NULL\n", __func__
);
4152 printf("%s uiocnt is 0\n", __func__
);
4157 * Sanity check on the length passed by caller as we are making 'int'
4160 resid
= recv_msg_array_resid(msgarray
, uiocnt
);
4161 if (resid
< 0 || resid
> INT_MAX
) {
4166 if (!(flags
& MSG_PEEK
) && sorecvmincopy
> 0) {
4173 so_update_last_owner_locked(so
, p
);
4174 so_update_policy(so
);
4177 so_update_necp_policy(so
, NULL
, NULL
);
4181 * If a recv attempt is made on a previously-accepted socket
4182 * that has been marked as inactive (disconnected), reject
4185 if (so
->so_flags
& SOF_DEFUNCT
) {
4186 struct sockbuf
*sb
= &so
->so_rcv
;
4189 SODEFUNCTLOG("%s[%d, %s]: defunct so 0x%llx [%d,%d] (%d)\n",
4190 __func__
, proc_pid(p
), proc_best_name(p
),
4191 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
4192 SOCK_DOM(so
), SOCK_TYPE(so
), error
);
4194 * This socket should have been disconnected and flushed
4195 * prior to being returned from sodefunct(); there should
4196 * be no data on its receive list, so panic otherwise.
4198 if (so
->so_state
& SS_DEFUNCT
) {
4199 sb_empty_assert(sb
, __func__
);
4206 * The uio may be empty
4208 if (npkts
>= uiocnt
) {
4214 * See if the socket has been closed (SS_NOFDREF|SS_CANTRCVMORE)
4215 * and if so just return to the caller. This could happen when
4216 * soreceive() is called by a socket upcall function during the
4217 * time the socket is freed. The socket buffer would have been
4218 * locked across the upcall, therefore we cannot put this thread
4219 * to sleep (else we will deadlock) or return EWOULDBLOCK (else
4220 * we may livelock), because the lock on the socket buffer will
4221 * only be released when the upcall routine returns to its caller.
4222 * Because the socket has been officially closed, there can be
4223 * no further read on it.
4225 if ((so
->so_state
& (SS_NOFDREF
| SS_CANTRCVMORE
)) ==
4226 (SS_NOFDREF
| SS_CANTRCVMORE
)) {
4231 error
= sblock(&so
->so_rcv
, SBLOCKWAIT(flags
));
4237 m
= so
->so_rcv
.sb_mb
;
4239 * Block awaiting more datagram if needed
4241 if (m
== NULL
|| (((flags
& MSG_DONTWAIT
) == 0 &&
4242 (so
->so_rcv
.sb_cc
< so
->so_rcv
.sb_lowat
||
4243 ((flags
& MSG_WAITALL
) && npkts
< uiocnt
))))) {
4245 * Panic if we notice inconsistencies in the socket's
4246 * receive list; both sb_mb and sb_cc should correctly
4247 * reflect the contents of the list, otherwise we may
4248 * end up with false positives during select() or poll()
4249 * which could put the application in a bad state.
4251 SB_MB_CHECK(&so
->so_rcv
);
4254 error
= so
->so_error
;
4255 if ((flags
& MSG_PEEK
) == 0) {
4260 if (so
->so_state
& SS_CANTRCVMORE
) {
4263 if ((so
->so_state
& (SS_ISCONNECTED
| SS_ISCONNECTING
)) == 0 &&
4264 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)) {
4268 if ((so
->so_state
& SS_NBIO
) ||
4269 (flags
& (MSG_DONTWAIT
| MSG_NBIO
))) {
4270 error
= EWOULDBLOCK
;
4274 * Do not block if we got some data
4276 if (free_list
!= NULL
) {
4281 SBLASTRECORDCHK(&so
->so_rcv
, "soreceive sbwait 1");
4282 SBLASTMBUFCHK(&so
->so_rcv
, "soreceive sbwait 1");
4284 sbunlock(&so
->so_rcv
, TRUE
); /* keep socket locked */
4287 error
= sbwait(&so
->so_rcv
);
4294 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_msgrcv
);
4295 SBLASTRECORDCHK(&so
->so_rcv
, "soreceive 1");
4296 SBLASTMBUFCHK(&so
->so_rcv
, "soreceive 1");
4299 * Consume the current uio index as we have a datagram
4301 auio
= msgarray
[npkts
].uio
;
4302 resid
= uio_resid(auio
);
4303 msgarray
[npkts
].which
|= SOCK_MSG_DATA
;
4304 psa
= (msgarray
[npkts
].which
& SOCK_MSG_SA
) ?
4305 &msgarray
[npkts
].psa
: NULL
;
4306 controlp
= (msgarray
[npkts
].which
& SOCK_MSG_CONTROL
) ?
4307 &msgarray
[npkts
].controlp
: NULL
;
4309 nextrecord
= m
->m_nextpkt
;
4311 if ((pr
->pr_flags
& PR_ADDR
) && m
->m_type
== MT_SONAME
) {
4312 error
= soreceive_addr(p
, so
, psa
, flags
, &m
, &nextrecord
, 1);
4313 if (error
== ERESTART
) {
4315 } else if (error
!= 0) {
4320 if (m
!= NULL
&& m
->m_type
== MT_CONTROL
) {
4321 error
= soreceive_ctl(so
, controlp
, flags
, &m
, &nextrecord
);
4327 if (m
->m_pkthdr
.len
== 0) {
4328 printf("%s:%d so %llx pkt %llx type %u pktlen null\n",
4330 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
4331 (uint64_t)DEBUG_KERNEL_ADDRPERM(m
),
4336 * Loop to copy the mbufs of the current record
4337 * Support zero length packets
4341 while (m
!= NULL
&& (len
= resid
- pktlen
) >= 0 && error
== 0) {
4342 if (m
->m_len
== 0) {
4343 panic("%p m_len zero", m
);
4345 if (m
->m_type
== 0) {
4346 panic("%p m_type zero", m
);
4349 * Clip to the residual length
4351 if (len
> m
->m_len
) {
4356 * Copy the mbufs via the uio or delay the copy
4357 * Sockbuf must be consistent here (points to current mbuf,
4358 * it points to next record) when we drop priority;
4359 * we must note any additions to the sockbuf when we
4360 * block interrupts again.
4362 if (len
> 0 && can_delay
== 0) {
4363 socket_unlock(so
, 0);
4364 error
= uiomove(mtod(m
, caddr_t
), (int)len
, auio
);
4370 delayed_copy_len
+= len
;
4373 if (len
== m
->m_len
) {
4375 * m was entirely copied
4377 sbfree(&so
->so_rcv
, m
);
4378 nextrecord
= m
->m_nextpkt
;
4379 m
->m_nextpkt
= NULL
;
4382 * Set the first packet to the head of the free list
4384 if (free_list
== NULL
) {
4388 * Link current packet to tail of free list
4391 if (free_tail
!= NULL
) {
4392 free_tail
->m_nextpkt
= m
;
4397 * Link current mbuf to last mbuf of current packet
4405 * Move next buf to head of socket buffer
4407 so
->so_rcv
.sb_mb
= m
= ml
->m_next
;
4411 m
->m_nextpkt
= nextrecord
;
4412 if (nextrecord
== NULL
) {
4413 so
->so_rcv
.sb_lastrecord
= m
;
4416 so
->so_rcv
.sb_mb
= nextrecord
;
4417 SB_EMPTY_FIXUP(&so
->so_rcv
);
4419 SBLASTRECORDCHK(&so
->so_rcv
, "soreceive 3");
4420 SBLASTMBUFCHK(&so
->so_rcv
, "soreceive 3");
4423 * Stop the loop on partial copy
4428 #ifdef MORE_LOCKING_DEBUG
4429 if (so
->so_usecount
<= 1) {
4430 panic("%s: after big while so=%llx ref=%d on socket\n",
4432 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
), so
->so_usecount
);
4437 * Tell the caller we made a partial copy
4440 if (so
->so_options
& SO_DONTTRUNC
) {
4442 * Copyout first the freelist then the partial mbuf
4444 socket_unlock(so
, 0);
4445 if (delayed_copy_len
) {
4446 error
= sodelayed_copy_list(so
, msgarray
,
4447 uiocnt
, &free_list
, &delayed_copy_len
);
4451 error
= uiomove(mtod(m
, caddr_t
), (int)len
,
4461 so
->so_rcv
.sb_cc
-= len
;
4462 flags
|= MSG_RCVMORE
;
4464 (void) sbdroprecord(&so
->so_rcv
);
4465 nextrecord
= so
->so_rcv
.sb_mb
;
4472 so
->so_rcv
.sb_mb
= nextrecord
;
4474 * First part is an inline SB_EMPTY_FIXUP(). Second
4475 * part makes sure sb_lastrecord is up-to-date if
4476 * there is still data in the socket buffer.
4478 if (so
->so_rcv
.sb_mb
== NULL
) {
4479 so
->so_rcv
.sb_mbtail
= NULL
;
4480 so
->so_rcv
.sb_lastrecord
= NULL
;
4481 } else if (nextrecord
->m_nextpkt
== NULL
) {
4482 so
->so_rcv
.sb_lastrecord
= nextrecord
;
4484 SB_MB_CHECK(&so
->so_rcv
);
4486 SBLASTRECORDCHK(&so
->so_rcv
, "soreceive 4");
4487 SBLASTMBUFCHK(&so
->so_rcv
, "soreceive 4");
4490 * We can continue to the next packet as long as:
4491 * - We haven't exhausted the uio array
4492 * - There was no error
4493 * - A packet was not truncated
4494 * - We can still receive more data
4496 if (npkts
< uiocnt
&& error
== 0 &&
4497 (flags
& (MSG_RCVMORE
| MSG_TRUNC
)) == 0 &&
4498 (so
->so_state
& SS_CANTRCVMORE
) == 0) {
4499 sbunlock(&so
->so_rcv
, TRUE
); /* keep socket locked */
4504 if (flagsp
!= NULL
) {
4510 * pru_rcvd may cause more data to be received if the socket lock
4511 * is dropped so we set MSG_HAVEMORE now based on what we know.
4512 * That way the caller won't be surprised if it receives less data
4515 if ((so
->so_options
& SO_WANTMORE
) && so
->so_rcv
.sb_cc
> 0) {
4516 flags
|= MSG_HAVEMORE
;
4519 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
) {
4520 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, flags
);
4524 sbunlock(&so
->so_rcv
, FALSE
); /* will unlock socket */
4526 socket_unlock(so
, 1);
4529 if (delayed_copy_len
) {
4530 error
= sodelayed_copy_list(so
, msgarray
, uiocnt
,
4531 &free_list
, &delayed_copy_len
);
4535 * Amortize the cost of freeing the mbufs
4537 if (free_list
!= NULL
) {
4538 m_freem_list(free_list
);
4540 if (free_others
!= NULL
) {
4541 m_freem_list(free_others
);
4544 KERNEL_DEBUG(DBG_FNC_SORECEIVE_LIST
| DBG_FUNC_END
, error
,
4550 so_statistics_event_to_nstat_event(int64_t *input_options
,
4551 uint64_t *nstat_event
)
4554 switch (*input_options
) {
4555 case SO_STATISTICS_EVENT_ENTER_CELLFALLBACK
:
4556 *nstat_event
= NSTAT_EVENT_SRC_ENTER_CELLFALLBACK
;
4558 case SO_STATISTICS_EVENT_EXIT_CELLFALLBACK
:
4559 *nstat_event
= NSTAT_EVENT_SRC_EXIT_CELLFALLBACK
;
4561 #if (DEBUG || DEVELOPMENT)
4562 case SO_STATISTICS_EVENT_RESERVED_1
:
4563 *nstat_event
= NSTAT_EVENT_SRC_RESERVED_1
;
4565 case SO_STATISTICS_EVENT_RESERVED_2
:
4566 *nstat_event
= NSTAT_EVENT_SRC_RESERVED_2
;
4568 #endif /* (DEBUG || DEVELOPMENT) */
4577 * Returns: 0 Success
4580 * <pru_shutdown>:EINVAL
4581 * <pru_shutdown>:EADDRNOTAVAIL[TCP]
4582 * <pru_shutdown>:ENOBUFS[TCP]
4583 * <pru_shutdown>:EMSGSIZE[TCP]
4584 * <pru_shutdown>:EHOSTUNREACH[TCP]
4585 * <pru_shutdown>:ENETUNREACH[TCP]
4586 * <pru_shutdown>:ENETDOWN[TCP]
4587 * <pru_shutdown>:ENOMEM[TCP]
4588 * <pru_shutdown>:EACCES[TCP]
4589 * <pru_shutdown>:EMSGSIZE[TCP]
4590 * <pru_shutdown>:ENOBUFS[TCP]
4591 * <pru_shutdown>:???[TCP] [ignorable: mostly IPSEC/firewall/DLIL]
4592 * <pru_shutdown>:??? [other protocol families]
4595 soshutdown(struct socket
*so
, int how
)
4599 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_START
, how
, 0, 0, 0, 0);
4607 (SS_ISCONNECTED
| SS_ISCONNECTING
| SS_ISDISCONNECTING
)) == 0) {
4610 error
= soshutdownlock(so
, how
);
4612 socket_unlock(so
, 1);
4619 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, how
, error
, 0, 0, 0);
4625 soshutdownlock_final(struct socket
*so
, int how
)
4627 struct protosw
*pr
= so
->so_proto
;
4630 sflt_notify(so
, sock_evt_shutdown
, &how
);
4632 if (how
!= SHUT_WR
) {
4633 if ((so
->so_state
& SS_CANTRCVMORE
) != 0) {
4634 /* read already shut down */
4639 postevent(so
, 0, EV_RCLOSED
);
4641 if (how
!= SHUT_RD
) {
4642 if ((so
->so_state
& SS_CANTSENDMORE
) != 0) {
4643 /* write already shut down */
4647 error
= (*pr
->pr_usrreqs
->pru_shutdown
)(so
);
4648 postevent(so
, 0, EV_WCLOSED
);
4651 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
, how
, 1, 0, 0, 0);
4656 soshutdownlock(struct socket
*so
, int how
)
4662 * A content filter may delay the actual shutdown until it
4663 * has processed the pending data
4665 if (so
->so_flags
& SOF_CONTENT_FILTER
) {
4666 error
= cfil_sock_shutdown(so
, &how
);
4667 if (error
== EJUSTRETURN
) {
4670 } else if (error
!= 0) {
4674 #endif /* CONTENT_FILTER */
4676 error
= soshutdownlock_final(so
, how
);
4683 sowflush(struct socket
*so
)
4685 struct sockbuf
*sb
= &so
->so_snd
;
4688 * Obtain lock on the socket buffer (SB_LOCK). This is required
4689 * to prevent the socket buffer from being unexpectedly altered
4690 * while it is used by another thread in socket send/receive.
4692 * sblock() must not fail here, hence the assertion.
4694 (void) sblock(sb
, SBL_WAIT
| SBL_NOINTR
| SBL_IGNDEFUNCT
);
4695 VERIFY(sb
->sb_flags
& SB_LOCK
);
4697 sb
->sb_flags
&= ~(SB_SEL
| SB_UPCALL
);
4698 sb
->sb_flags
|= SB_DROP
;
4699 sb
->sb_upcall
= NULL
;
4700 sb
->sb_upcallarg
= NULL
;
4702 sbunlock(sb
, TRUE
); /* keep socket locked */
4704 selthreadclear(&sb
->sb_sel
);
4709 sorflush(struct socket
*so
)
4711 struct sockbuf
*sb
= &so
->so_rcv
;
4712 struct protosw
*pr
= so
->so_proto
;
4715 lck_mtx_t
*mutex_held
;
4717 * XXX: This code is currently commented out, because we may get here
4718 * as part of sofreelastref(), and at that time, pr_getlock() may no
4719 * longer be able to return us the lock; this will be fixed in future.
4721 if (so
->so_proto
->pr_getlock
!= NULL
) {
4722 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
4724 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
4727 LCK_MTX_ASSERT(mutex_held
, LCK_MTX_ASSERT_OWNED
);
4730 sflt_notify(so
, sock_evt_flush_read
, NULL
);
4735 * Obtain lock on the socket buffer (SB_LOCK). This is required
4736 * to prevent the socket buffer from being unexpectedly altered
4737 * while it is used by another thread in socket send/receive.
4739 * sblock() must not fail here, hence the assertion.
4741 (void) sblock(sb
, SBL_WAIT
| SBL_NOINTR
| SBL_IGNDEFUNCT
);
4742 VERIFY(sb
->sb_flags
& SB_LOCK
);
4745 * Copy only the relevant fields from "sb" to "asb" which we
4746 * need for sbrelease() to function. In particular, skip
4747 * sb_sel as it contains the wait queue linkage, which would
4748 * wreak havoc if we were to issue selthreadclear() on "asb".
4749 * Make sure to not carry over SB_LOCK in "asb", as we need
4750 * to acquire it later as part of sbrelease().
4752 bzero(&asb
, sizeof(asb
));
4753 asb
.sb_cc
= sb
->sb_cc
;
4754 asb
.sb_hiwat
= sb
->sb_hiwat
;
4755 asb
.sb_mbcnt
= sb
->sb_mbcnt
;
4756 asb
.sb_mbmax
= sb
->sb_mbmax
;
4757 asb
.sb_ctl
= sb
->sb_ctl
;
4758 asb
.sb_lowat
= sb
->sb_lowat
;
4759 asb
.sb_mb
= sb
->sb_mb
;
4760 asb
.sb_mbtail
= sb
->sb_mbtail
;
4761 asb
.sb_lastrecord
= sb
->sb_lastrecord
;
4762 asb
.sb_so
= sb
->sb_so
;
4763 asb
.sb_flags
= sb
->sb_flags
;
4764 asb
.sb_flags
&= ~(SB_LOCK
| SB_SEL
| SB_KNOTE
| SB_UPCALL
);
4765 asb
.sb_flags
|= SB_DROP
;
4768 * Ideally we'd bzero() these and preserve the ones we need;
4769 * but to do that we'd need to shuffle things around in the
4770 * sockbuf, and we can't do it now because there are KEXTS
4771 * that are directly referring to the socket structure.
4773 * Setting SB_DROP acts as a barrier to prevent further appends.
4774 * Clearing SB_SEL is done for selthreadclear() below.
4783 sb
->sb_mbtail
= NULL
;
4784 sb
->sb_lastrecord
= NULL
;
4785 sb
->sb_timeo
.tv_sec
= 0;
4786 sb
->sb_timeo
.tv_usec
= 0;
4787 sb
->sb_upcall
= NULL
;
4788 sb
->sb_upcallarg
= NULL
;
4789 sb
->sb_flags
&= ~(SB_SEL
| SB_UPCALL
);
4790 sb
->sb_flags
|= SB_DROP
;
4792 sbunlock(sb
, TRUE
); /* keep socket locked */
4795 * Note that selthreadclear() is called on the original "sb" and
4796 * not the local "asb" because of the way wait queue linkage is
4797 * implemented. Given that selwakeup() may be triggered, SB_SEL
4798 * should no longer be set (cleared above.)
4800 selthreadclear(&sb
->sb_sel
);
4802 if ((pr
->pr_flags
& PR_RIGHTS
) && pr
->pr_domain
->dom_dispose
) {
4803 (*pr
->pr_domain
->dom_dispose
)(asb
.sb_mb
);
4810 * Perhaps this routine, and sooptcopyout(), below, ought to come in
4811 * an additional variant to handle the case where the option value needs
4812 * to be some kind of integer, but not a specific size.
4813 * In addition to their use here, these functions are also called by the
4814 * protocol-level pr_ctloutput() routines.
4816 * Returns: 0 Success
4821 sooptcopyin(struct sockopt
*sopt
, void *buf
, size_t len
, size_t minlen
)
4826 * If the user gives us more than we wanted, we ignore it,
4827 * but if we don't get the minimum length the caller
4828 * wants, we return EINVAL. On success, sopt->sopt_valsize
4829 * is set to however much we actually retrieved.
4831 if ((valsize
= sopt
->sopt_valsize
) < minlen
) {
4834 if (valsize
> len
) {
4835 sopt
->sopt_valsize
= valsize
= len
;
4838 if (sopt
->sopt_p
!= kernproc
) {
4839 return copyin(sopt
->sopt_val
, buf
, valsize
);
4842 bcopy(CAST_DOWN(caddr_t
, sopt
->sopt_val
), buf
, valsize
);
4847 * sooptcopyin_timeval
4848 * Copy in a timeval value into tv_p, and take into account whether the
4849 * the calling process is 64-bit or 32-bit. Moved the sanity checking
4850 * code here so that we can verify the 64-bit tv_sec value before we lose
4851 * the top 32-bits assigning tv64.tv_sec to tv_p->tv_sec.
4854 sooptcopyin_timeval(struct sockopt
*sopt
, struct timeval
*tv_p
)
4858 if (proc_is64bit(sopt
->sopt_p
)) {
4859 struct user64_timeval tv64
;
4861 if (sopt
->sopt_valsize
< sizeof(tv64
)) {
4865 sopt
->sopt_valsize
= sizeof(tv64
);
4866 if (sopt
->sopt_p
!= kernproc
) {
4867 error
= copyin(sopt
->sopt_val
, &tv64
, sizeof(tv64
));
4872 bcopy(CAST_DOWN(caddr_t
, sopt
->sopt_val
), &tv64
,
4875 if (tv64
.tv_sec
< 0 || tv64
.tv_sec
> LONG_MAX
||
4876 tv64
.tv_usec
< 0 || tv64
.tv_usec
>= 1000000) {
4880 tv_p
->tv_sec
= tv64
.tv_sec
;
4881 tv_p
->tv_usec
= tv64
.tv_usec
;
4883 struct user32_timeval tv32
;
4885 if (sopt
->sopt_valsize
< sizeof(tv32
)) {
4889 sopt
->sopt_valsize
= sizeof(tv32
);
4890 if (sopt
->sopt_p
!= kernproc
) {
4891 error
= copyin(sopt
->sopt_val
, &tv32
, sizeof(tv32
));
4896 bcopy(CAST_DOWN(caddr_t
, sopt
->sopt_val
), &tv32
,
4901 * K64todo "comparison is always false due to
4902 * limited range of data type"
4904 if (tv32
.tv_sec
< 0 || tv32
.tv_sec
> LONG_MAX
||
4905 tv32
.tv_usec
< 0 || tv32
.tv_usec
>= 1000000) {
4909 tv_p
->tv_sec
= tv32
.tv_sec
;
4910 tv_p
->tv_usec
= tv32
.tv_usec
;
4916 soopt_cred_check(struct socket
*so
, int priv
, boolean_t allow_root
,
4917 boolean_t ignore_delegate
)
4919 kauth_cred_t cred
= NULL
;
4920 proc_t ep
= PROC_NULL
;
4924 if (ignore_delegate
== false && so
->so_flags
& SOF_DELEGATED
) {
4925 ep
= proc_find(so
->e_pid
);
4927 cred
= kauth_cred_proc_ref(ep
);
4931 uid
= kauth_cred_getuid(cred
? cred
: so
->so_cred
);
4933 /* uid is 0 for root */
4934 if (uid
!= 0 || !allow_root
) {
4935 error
= priv_check_cred(cred
? cred
: so
->so_cred
, priv
, 0);
4938 kauth_cred_unref(&cred
);
4940 if (ep
!= PROC_NULL
) {
4948 * Returns: 0 Success
4953 * sooptcopyin:EINVAL
4954 * sooptcopyin:EFAULT
4955 * sooptcopyin_timeval:EINVAL
4956 * sooptcopyin_timeval:EFAULT
4957 * sooptcopyin_timeval:EDOM
4958 * <pr_ctloutput>:EOPNOTSUPP[AF_UNIX]
4959 * <pr_ctloutput>:???w
4960 * sflt_attach_private:??? [whatever a filter author chooses]
4961 * <sf_setoption>:??? [whatever a filter author chooses]
4963 * Notes: Other <pru_listen> returns depend on the protocol family; all
4964 * <sf_listen> returns depend on what the filter author causes
4965 * their filter to return.
4968 sosetoptlock(struct socket
*so
, struct sockopt
*sopt
, int dolock
)
4971 int64_t long_optval
;
4974 #if CONFIG_MACF_SOCKET
4976 #endif /* MAC_SOCKET */
4978 if (sopt
->sopt_dir
!= SOPT_SET
) {
4979 sopt
->sopt_dir
= SOPT_SET
;
4986 if ((so
->so_state
& (SS_CANTRCVMORE
| SS_CANTSENDMORE
)) ==
4987 (SS_CANTRCVMORE
| SS_CANTSENDMORE
) &&
4988 (so
->so_flags
& SOF_NPX_SETOPTSHUT
) == 0) {
4989 /* the socket has been shutdown, no more sockopt's */
4994 error
= sflt_setsockopt(so
, sopt
);
4996 if (error
== EJUSTRETURN
) {
5002 if (sopt
->sopt_level
!= SOL_SOCKET
) {
5003 if (so
->so_proto
!= NULL
&&
5004 so
->so_proto
->pr_ctloutput
!= NULL
) {
5005 error
= (*so
->so_proto
->pr_ctloutput
)(so
, sopt
);
5008 error
= ENOPROTOOPT
;
5011 * Allow socket-level (SOL_SOCKET) options to be filtered by
5012 * the protocol layer, if needed. A zero value returned from
5013 * the handler means use default socket-level processing as
5014 * done by the rest of this routine. Otherwise, any other
5015 * return value indicates that the option is unsupported.
5017 if (so
->so_proto
!= NULL
&& (error
= so
->so_proto
->pr_usrreqs
->
5018 pru_socheckopt(so
, sopt
)) != 0) {
5023 switch (sopt
->sopt_name
) {
5026 error
= sooptcopyin(sopt
, &l
, sizeof(l
), sizeof(l
));
5031 so
->so_linger
= (sopt
->sopt_name
== SO_LINGER
) ?
5032 l
.l_linger
: l
.l_linger
* hz
;
5033 if (l
.l_onoff
!= 0) {
5034 so
->so_options
|= SO_LINGER
;
5036 so
->so_options
&= ~SO_LINGER
;
5043 case SO_USELOOPBACK
:
5049 case SO_TIMESTAMP_MONOTONIC
:
5050 case SO_TIMESTAMP_CONTINUOUS
:
5053 case SO_WANTOOBFLAG
:
5054 case SO_NOWAKEFROMSLEEP
:
5055 case SO_NOAPNFALLBK
:
5056 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5062 so
->so_options
|= sopt
->sopt_name
;
5064 so
->so_options
&= ~sopt
->sopt_name
;
5072 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5079 * Values < 1 make no sense for any of these
5080 * options, so disallow them.
5087 switch (sopt
->sopt_name
) {
5090 struct sockbuf
*sb
=
5091 (sopt
->sopt_name
== SO_SNDBUF
) ?
5092 &so
->so_snd
: &so
->so_rcv
;
5093 if (sbreserve(sb
, (u_int32_t
)optval
) == 0) {
5097 sb
->sb_flags
|= SB_USRSIZE
;
5098 sb
->sb_flags
&= ~SB_AUTOSIZE
;
5099 sb
->sb_idealsize
= (u_int32_t
)optval
;
5103 * Make sure the low-water is never greater than
5107 int space
= sbspace(&so
->so_snd
);
5108 u_int32_t hiwat
= so
->so_snd
.sb_hiwat
;
5110 if (so
->so_snd
.sb_flags
& SB_UNIX
) {
5112 (struct unpcb
*)(so
->so_pcb
);
5114 unp
->unp_conn
!= NULL
) {
5115 hiwat
+= unp
->unp_conn
->unp_cc
;
5119 so
->so_snd
.sb_lowat
=
5123 if (space
>= so
->so_snd
.sb_lowat
) {
5130 so
->so_rcv
.sb_lowat
=
5131 (optval
> so
->so_rcv
.sb_hiwat
) ?
5132 so
->so_rcv
.sb_hiwat
: optval
;
5133 data_len
= so
->so_rcv
.sb_cc
5134 - so
->so_rcv
.sb_ctl
;
5135 if (data_len
>= so
->so_rcv
.sb_lowat
) {
5145 error
= sooptcopyin_timeval(sopt
, &tv
);
5150 switch (sopt
->sopt_name
) {
5152 so
->so_snd
.sb_timeo
= tv
;
5155 so
->so_rcv
.sb_timeo
= tv
;
5163 error
= sooptcopyin(sopt
, &nke
, sizeof(nke
),
5169 error
= sflt_attach_internal(so
, nke
.nke_handle
);
5174 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5180 so
->so_flags
|= SOF_NOSIGPIPE
;
5182 so
->so_flags
&= ~SOF_NOSIGPIPE
;
5187 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5193 so
->so_flags
|= SOF_NOADDRAVAIL
;
5195 so
->so_flags
&= ~SOF_NOADDRAVAIL
;
5199 case SO_REUSESHAREUID
:
5200 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5206 so
->so_flags
|= SOF_REUSESHAREUID
;
5208 so
->so_flags
&= ~SOF_REUSESHAREUID
;
5212 case SO_NOTIFYCONFLICT
:
5213 if (kauth_cred_issuser(kauth_cred_get()) == 0) {
5217 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5223 so
->so_flags
|= SOF_NOTIFYCONFLICT
;
5225 so
->so_flags
&= ~SOF_NOTIFYCONFLICT
;
5229 case SO_RESTRICTIONS
:
5230 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5236 error
= so_set_restrictions(so
, optval
);
5239 case SO_AWDL_UNRESTRICTED
:
5240 if (SOCK_DOM(so
) != PF_INET
&&
5241 SOCK_DOM(so
) != PF_INET6
) {
5245 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5251 error
= soopt_cred_check(so
,
5252 PRIV_NET_RESTRICTED_AWDL
, false, false);
5254 inp_set_awdl_unrestricted(
5258 inp_clear_awdl_unrestricted(sotoinpcb(so
));
5261 case SO_INTCOPROC_ALLOW
:
5262 if (SOCK_DOM(so
) != PF_INET6
) {
5266 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5272 inp_get_intcoproc_allowed(sotoinpcb(so
)) == FALSE
) {
5273 error
= soopt_cred_check(so
,
5274 PRIV_NET_RESTRICTED_INTCOPROC
, false, false);
5276 inp_set_intcoproc_allowed(
5279 } else if (optval
== 0) {
5280 inp_clear_intcoproc_allowed(sotoinpcb(so
));
5285 #if CONFIG_MACF_SOCKET
5286 if ((error
= sooptcopyin(sopt
, &extmac
, sizeof(extmac
),
5287 sizeof(extmac
))) != 0) {
5291 error
= mac_setsockopt_label(proc_ucred(sopt
->sopt_p
),
5295 #endif /* MAC_SOCKET */
5298 case SO_UPCALLCLOSEWAIT
:
5299 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5305 so
->so_flags
|= SOF_UPCALLCLOSEWAIT
;
5307 so
->so_flags
&= ~SOF_UPCALLCLOSEWAIT
;
5312 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5318 so
->so_flags
|= SOF_BINDRANDOMPORT
;
5320 so
->so_flags
&= ~SOF_BINDRANDOMPORT
;
5324 case SO_NP_EXTENSIONS
: {
5325 struct so_np_extensions sonpx
;
5327 error
= sooptcopyin(sopt
, &sonpx
, sizeof(sonpx
),
5332 if (sonpx
.npx_mask
& ~SONPX_MASK_VALID
) {
5337 * Only one bit defined for now
5339 if ((sonpx
.npx_mask
& SONPX_SETOPTSHUT
)) {
5340 if ((sonpx
.npx_flags
& SONPX_SETOPTSHUT
)) {
5341 so
->so_flags
|= SOF_NPX_SETOPTSHUT
;
5343 so
->so_flags
&= ~SOF_NPX_SETOPTSHUT
;
5349 case SO_TRAFFIC_CLASS
: {
5350 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5355 if (optval
>= SO_TC_NET_SERVICE_OFFSET
) {
5356 int netsvc
= optval
- SO_TC_NET_SERVICE_OFFSET
;
5357 error
= so_set_net_service_type(so
, netsvc
);
5360 error
= so_set_traffic_class(so
, optval
);
5364 so
->so_flags1
&= ~SOF1_TC_NET_SERV_TYPE
;
5365 so
->so_netsvctype
= _NET_SERVICE_TYPE_UNSPEC
;
5369 case SO_RECV_TRAFFIC_CLASS
: {
5370 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5376 so
->so_flags
&= ~SOF_RECV_TRAFFIC_CLASS
;
5378 so
->so_flags
|= SOF_RECV_TRAFFIC_CLASS
;
5383 #if (DEVELOPMENT || DEBUG)
5384 case SO_TRAFFIC_CLASS_DBG
: {
5385 struct so_tcdbg so_tcdbg
;
5387 error
= sooptcopyin(sopt
, &so_tcdbg
,
5388 sizeof(struct so_tcdbg
), sizeof(struct so_tcdbg
));
5392 error
= so_set_tcdbg(so
, &so_tcdbg
);
5398 #endif /* (DEVELOPMENT || DEBUG) */
5400 case SO_PRIVILEGED_TRAFFIC_CLASS
:
5401 error
= priv_check_cred(kauth_cred_get(),
5402 PRIV_NET_PRIVILEGED_TRAFFIC_CLASS
, 0);
5406 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5412 so
->so_flags
&= ~SOF_PRIVILEGED_TRAFFIC_CLASS
;
5414 so
->so_flags
|= SOF_PRIVILEGED_TRAFFIC_CLASS
;
5418 #if (DEVELOPMENT || DEBUG)
5420 error
= sosetdefunct(current_proc(), so
, 0, FALSE
);
5422 error
= sodefunct(current_proc(), so
, 0);
5426 #endif /* (DEVELOPMENT || DEBUG) */
5429 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5431 if (error
!= 0 || (so
->so_flags
& SOF_DEFUNCT
)) {
5438 * Any process can set SO_DEFUNCTOK (clear
5439 * SOF_NODEFUNCT), but only root can clear
5440 * SO_DEFUNCTOK (set SOF_NODEFUNCT).
5443 kauth_cred_issuser(kauth_cred_get()) == 0) {
5448 so
->so_flags
&= ~SOF_NODEFUNCT
;
5450 so
->so_flags
|= SOF_NODEFUNCT
;
5453 if (SOCK_DOM(so
) == PF_INET
||
5454 SOCK_DOM(so
) == PF_INET6
) {
5455 char s
[MAX_IPv6_STR_LEN
];
5456 char d
[MAX_IPv6_STR_LEN
];
5457 struct inpcb
*inp
= sotoinpcb(so
);
5459 SODEFUNCTLOG("%s[%d, %s]: so 0x%llx "
5460 "[%s %s:%d -> %s:%d] is now marked "
5461 "as %seligible for "
5462 "defunct\n", __func__
, proc_selfpid(),
5463 proc_best_name(current_proc()),
5464 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
5465 (SOCK_TYPE(so
) == SOCK_STREAM
) ?
5466 "TCP" : "UDP", inet_ntop(SOCK_DOM(so
),
5467 ((SOCK_DOM(so
) == PF_INET
) ?
5468 (void *)&inp
->inp_laddr
.s_addr
:
5469 (void *)&inp
->in6p_laddr
), s
, sizeof(s
)),
5470 ntohs(inp
->in6p_lport
),
5471 inet_ntop(SOCK_DOM(so
),
5472 (SOCK_DOM(so
) == PF_INET
) ?
5473 (void *)&inp
->inp_faddr
.s_addr
:
5474 (void *)&inp
->in6p_faddr
, d
, sizeof(d
)),
5475 ntohs(inp
->in6p_fport
),
5476 (so
->so_flags
& SOF_NODEFUNCT
) ?
5479 SODEFUNCTLOG("%s[%d, %s]: so 0x%llx [%d,%d] "
5480 "is now marked as %seligible for "
5482 __func__
, proc_selfpid(),
5483 proc_best_name(current_proc()),
5484 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
5485 SOCK_DOM(so
), SOCK_TYPE(so
),
5486 (so
->so_flags
& SOF_NODEFUNCT
) ?
5492 /* This option is not settable */
5496 case SO_OPPORTUNISTIC
:
5497 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5500 error
= so_set_opportunistic(so
, optval
);
5505 /* This option is handled by lower layer(s) */
5510 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5513 error
= so_set_recv_anyif(so
, optval
);
5517 case SO_TRAFFIC_MGT_BACKGROUND
: {
5518 /* This option is handled by lower layer(s) */
5524 case SO_FLOW_DIVERT_TOKEN
:
5525 error
= flow_divert_token_set(so
, sopt
);
5527 #endif /* FLOW_DIVERT */
5531 if ((error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5532 sizeof(optval
))) != 0) {
5536 error
= so_set_effective_pid(so
, optval
, sopt
->sopt_p
, true);
5539 case SO_DELEGATED_UUID
: {
5542 if ((error
= sooptcopyin(sopt
, &euuid
, sizeof(euuid
),
5543 sizeof(euuid
))) != 0) {
5547 error
= so_set_effective_uuid(so
, euuid
, sopt
->sopt_p
, true);
5552 case SO_NECP_ATTRIBUTES
:
5553 error
= necp_set_socket_attributes(so
, sopt
);
5556 case SO_NECP_CLIENTUUID
: {
5557 if (SOCK_DOM(so
) == PF_MULTIPATH
) {
5558 /* Handled by MPTCP itself */
5562 if (SOCK_DOM(so
) != PF_INET
&& SOCK_DOM(so
) != PF_INET6
) {
5567 struct inpcb
*inp
= sotoinpcb(so
);
5568 if (!uuid_is_null(inp
->necp_client_uuid
)) {
5569 // Clear out the old client UUID if present
5570 necp_inpcb_remove_cb(inp
);
5573 error
= sooptcopyin(sopt
, &inp
->necp_client_uuid
,
5574 sizeof(uuid_t
), sizeof(uuid_t
));
5579 if (uuid_is_null(inp
->necp_client_uuid
)) {
5584 pid_t current_pid
= proc_pid(current_proc());
5585 error
= necp_client_register_socket_flow(current_pid
,
5586 inp
->necp_client_uuid
, inp
);
5588 uuid_clear(inp
->necp_client_uuid
);
5592 if (inp
->inp_lport
!= 0) {
5593 // There is a bound local port, so this is not
5594 // a fresh socket. Assign to the client.
5595 necp_client_assign_from_socket(current_pid
, inp
->necp_client_uuid
, inp
);
5600 case SO_NECP_LISTENUUID
: {
5601 if (SOCK_DOM(so
) != PF_INET
&& SOCK_DOM(so
) != PF_INET6
) {
5606 struct inpcb
*inp
= sotoinpcb(so
);
5607 if (!uuid_is_null(inp
->necp_client_uuid
)) {
5612 error
= sooptcopyin(sopt
, &inp
->necp_client_uuid
,
5613 sizeof(uuid_t
), sizeof(uuid_t
));
5618 if (uuid_is_null(inp
->necp_client_uuid
)) {
5623 error
= necp_client_register_socket_listener(proc_pid(current_proc()),
5624 inp
->necp_client_uuid
, inp
);
5626 uuid_clear(inp
->necp_client_uuid
);
5630 // Mark that the port registration is held by NECP
5631 inp
->inp_flags2
|= INP2_EXTERNAL_PORT
;
5637 case SO_EXTENDED_BK_IDLE
:
5638 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5641 error
= so_set_extended_bk_idle(so
, optval
);
5645 case SO_MARK_CELLFALLBACK
:
5646 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5656 so
->so_flags1
&= ~SOF1_CELLFALLBACK
;
5658 so
->so_flags1
|= SOF1_CELLFALLBACK
;
5662 case SO_STATISTICS_EVENT
:
5663 error
= sooptcopyin(sopt
, &long_optval
,
5664 sizeof(long_optval
), sizeof(long_optval
));
5668 u_int64_t nstat_event
= 0;
5669 error
= so_statistics_event_to_nstat_event(
5670 &long_optval
, &nstat_event
);
5674 nstat_pcb_event(sotoinpcb(so
), nstat_event
);
5677 case SO_NET_SERVICE_TYPE
: {
5678 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5683 error
= so_set_net_service_type(so
, optval
);
5687 case SO_QOSMARKING_POLICY_OVERRIDE
:
5688 error
= priv_check_cred(kauth_cred_get(),
5689 PRIV_NET_QOSMARKING_POLICY_OVERRIDE
, 0);
5693 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
5699 so
->so_flags1
&= ~SOF1_QOSMARKING_POLICY_OVERRIDE
;
5701 so
->so_flags1
|= SOF1_QOSMARKING_POLICY_OVERRIDE
;
5705 case SO_MPKL_SEND_INFO
: {
5706 struct so_mpkl_send_info so_mpkl_send_info
;
5708 error
= sooptcopyin(sopt
, &so_mpkl_send_info
,
5709 sizeof(struct so_mpkl_send_info
), sizeof(struct so_mpkl_send_info
));
5713 uuid_copy(so
->so_mpkl_send_uuid
, so_mpkl_send_info
.mpkl_uuid
);
5714 so
->so_mpkl_send_proto
= so_mpkl_send_info
.mpkl_proto
;
5716 if (uuid_is_null(so
->so_mpkl_send_uuid
) && so
->so_mpkl_send_proto
== 0) {
5717 so
->so_flags1
&= ~SOF1_MPKL_SEND_INFO
;
5719 so
->so_flags1
|= SOF1_MPKL_SEND_INFO
;
5724 error
= ENOPROTOOPT
;
5727 if (error
== 0 && so
->so_proto
!= NULL
&&
5728 so
->so_proto
->pr_ctloutput
!= NULL
) {
5729 (void) so
->so_proto
->pr_ctloutput(so
, sopt
);
5734 socket_unlock(so
, 1);
5739 /* Helper routines for getsockopt */
5741 sooptcopyout(struct sockopt
*sopt
, void *buf
, size_t len
)
5749 * Documented get behavior is that we always return a value,
5750 * possibly truncated to fit in the user's buffer.
5751 * Traditional behavior is that we always tell the user
5752 * precisely how much we copied, rather than something useful
5753 * like the total amount we had available for her.
5754 * Note that this interface is not idempotent; the entire answer must
5755 * generated ahead of time.
5757 valsize
= min(len
, sopt
->sopt_valsize
);
5758 sopt
->sopt_valsize
= valsize
;
5759 if (sopt
->sopt_val
!= USER_ADDR_NULL
) {
5760 if (sopt
->sopt_p
!= kernproc
) {
5761 error
= copyout(buf
, sopt
->sopt_val
, valsize
);
5763 bcopy(buf
, CAST_DOWN(caddr_t
, sopt
->sopt_val
), valsize
);
5770 sooptcopyout_timeval(struct sockopt
*sopt
, const struct timeval
*tv_p
)
5774 struct user64_timeval tv64
= {};
5775 struct user32_timeval tv32
= {};
5780 if (proc_is64bit(sopt
->sopt_p
)) {
5782 tv64
.tv_sec
= tv_p
->tv_sec
;
5783 tv64
.tv_usec
= tv_p
->tv_usec
;
5787 tv32
.tv_sec
= tv_p
->tv_sec
;
5788 tv32
.tv_usec
= tv_p
->tv_usec
;
5791 valsize
= min(len
, sopt
->sopt_valsize
);
5792 sopt
->sopt_valsize
= valsize
;
5793 if (sopt
->sopt_val
!= USER_ADDR_NULL
) {
5794 if (sopt
->sopt_p
!= kernproc
) {
5795 error
= copyout(val
, sopt
->sopt_val
, valsize
);
5797 bcopy(val
, CAST_DOWN(caddr_t
, sopt
->sopt_val
), valsize
);
5806 * <pr_ctloutput>:EOPNOTSUPP[AF_UNIX]
5807 * <pr_ctloutput>:???
5808 * <sf_getoption>:???
5811 sogetoptlock(struct socket
*so
, struct sockopt
*sopt
, int dolock
)
5816 #if CONFIG_MACF_SOCKET
5818 #endif /* MAC_SOCKET */
5820 if (sopt
->sopt_dir
!= SOPT_GET
) {
5821 sopt
->sopt_dir
= SOPT_GET
;
5828 error
= sflt_getsockopt(so
, sopt
);
5830 if (error
== EJUSTRETURN
) {
5836 if (sopt
->sopt_level
!= SOL_SOCKET
) {
5837 if (so
->so_proto
!= NULL
&&
5838 so
->so_proto
->pr_ctloutput
!= NULL
) {
5839 error
= (*so
->so_proto
->pr_ctloutput
)(so
, sopt
);
5842 error
= ENOPROTOOPT
;
5845 * Allow socket-level (SOL_SOCKET) options to be filtered by
5846 * the protocol layer, if needed. A zero value returned from
5847 * the handler means use default socket-level processing as
5848 * done by the rest of this routine. Otherwise, any other
5849 * return value indicates that the option is unsupported.
5851 if (so
->so_proto
!= NULL
&& (error
= so
->so_proto
->pr_usrreqs
->
5852 pru_socheckopt(so
, sopt
)) != 0) {
5857 switch (sopt
->sopt_name
) {
5860 l
.l_onoff
= ((so
->so_options
& SO_LINGER
) ? 1 : 0);
5861 l
.l_linger
= (sopt
->sopt_name
== SO_LINGER
) ?
5862 so
->so_linger
: so
->so_linger
/ hz
;
5863 error
= sooptcopyout(sopt
, &l
, sizeof(l
));
5866 case SO_USELOOPBACK
:
5875 case SO_TIMESTAMP_MONOTONIC
:
5876 case SO_TIMESTAMP_CONTINUOUS
:
5879 case SO_WANTOOBFLAG
:
5880 case SO_NOWAKEFROMSLEEP
:
5881 case SO_NOAPNFALLBK
:
5882 optval
= so
->so_options
& sopt
->sopt_name
;
5884 error
= sooptcopyout(sopt
, &optval
, sizeof(optval
));
5888 optval
= so
->so_type
;
5892 if (so
->so_proto
->pr_flags
& PR_ATOMIC
) {
5897 m1
= so
->so_rcv
.sb_mb
;
5898 while (m1
!= NULL
) {
5899 if (m1
->m_type
== MT_DATA
||
5900 m1
->m_type
== MT_HEADER
||
5901 m1
->m_type
== MT_OOBDATA
) {
5902 pkt_total
+= m1
->m_len
;
5908 optval
= so
->so_rcv
.sb_cc
- so
->so_rcv
.sb_ctl
;
5913 if (so
->so_proto
->pr_flags
& PR_ATOMIC
) {
5917 m1
= so
->so_rcv
.sb_mb
;
5918 while (m1
!= NULL
) {
5925 error
= ENOPROTOOPT
;
5930 optval
= so
->so_snd
.sb_cc
;
5934 optval
= so
->so_error
;
5939 u_int32_t hiwat
= so
->so_snd
.sb_hiwat
;
5941 if (so
->so_snd
.sb_flags
& SB_UNIX
) {
5943 (struct unpcb
*)(so
->so_pcb
);
5944 if (unp
!= NULL
&& unp
->unp_conn
!= NULL
) {
5945 hiwat
+= unp
->unp_conn
->unp_cc
;
5953 optval
= so
->so_rcv
.sb_hiwat
;
5957 optval
= so
->so_snd
.sb_lowat
;
5961 optval
= so
->so_rcv
.sb_lowat
;
5966 tv
= (sopt
->sopt_name
== SO_SNDTIMEO
?
5967 so
->so_snd
.sb_timeo
: so
->so_rcv
.sb_timeo
);
5969 error
= sooptcopyout_timeval(sopt
, &tv
);
5973 optval
= (so
->so_flags
& SOF_NOSIGPIPE
);
5977 optval
= (so
->so_flags
& SOF_NOADDRAVAIL
);
5980 case SO_REUSESHAREUID
:
5981 optval
= (so
->so_flags
& SOF_REUSESHAREUID
);
5985 case SO_NOTIFYCONFLICT
:
5986 optval
= (so
->so_flags
& SOF_NOTIFYCONFLICT
);
5989 case SO_RESTRICTIONS
:
5990 optval
= so_get_restrictions(so
);
5993 case SO_AWDL_UNRESTRICTED
:
5994 if (SOCK_DOM(so
) == PF_INET
||
5995 SOCK_DOM(so
) == PF_INET6
) {
5996 optval
= inp_get_awdl_unrestricted(
6004 case SO_INTCOPROC_ALLOW
:
6005 if (SOCK_DOM(so
) == PF_INET6
) {
6006 optval
= inp_get_intcoproc_allowed(
6015 #if CONFIG_MACF_SOCKET
6016 if ((error
= sooptcopyin(sopt
, &extmac
, sizeof(extmac
),
6017 sizeof(extmac
))) != 0 ||
6018 (error
= mac_socket_label_get(proc_ucred(
6019 sopt
->sopt_p
), so
, &extmac
)) != 0) {
6023 error
= sooptcopyout(sopt
, &extmac
, sizeof(extmac
));
6026 #endif /* MAC_SOCKET */
6030 #if CONFIG_MACF_SOCKET
6031 if ((error
= sooptcopyin(sopt
, &extmac
, sizeof(extmac
),
6032 sizeof(extmac
))) != 0 ||
6033 (error
= mac_socketpeer_label_get(proc_ucred(
6034 sopt
->sopt_p
), so
, &extmac
)) != 0) {
6038 error
= sooptcopyout(sopt
, &extmac
, sizeof(extmac
));
6041 #endif /* MAC_SOCKET */
6044 #ifdef __APPLE_API_PRIVATE
6045 case SO_UPCALLCLOSEWAIT
:
6046 optval
= (so
->so_flags
& SOF_UPCALLCLOSEWAIT
);
6050 optval
= (so
->so_flags
& SOF_BINDRANDOMPORT
);
6053 case SO_NP_EXTENSIONS
: {
6054 struct so_np_extensions sonpx
= {};
6056 sonpx
.npx_flags
= (so
->so_flags
& SOF_NPX_SETOPTSHUT
) ?
6057 SONPX_SETOPTSHUT
: 0;
6058 sonpx
.npx_mask
= SONPX_MASK_VALID
;
6060 error
= sooptcopyout(sopt
, &sonpx
,
6061 sizeof(struct so_np_extensions
));
6065 case SO_TRAFFIC_CLASS
:
6066 optval
= so
->so_traffic_class
;
6069 case SO_RECV_TRAFFIC_CLASS
:
6070 optval
= (so
->so_flags
& SOF_RECV_TRAFFIC_CLASS
);
6073 case SO_TRAFFIC_CLASS_STATS
:
6074 error
= sooptcopyout(sopt
, &so
->so_tc_stats
,
6075 sizeof(so
->so_tc_stats
));
6078 #if (DEVELOPMENT || DEBUG)
6079 case SO_TRAFFIC_CLASS_DBG
:
6080 error
= sogetopt_tcdbg(so
, sopt
);
6082 #endif /* (DEVELOPMENT || DEBUG) */
6084 case SO_PRIVILEGED_TRAFFIC_CLASS
:
6085 optval
= (so
->so_flags
& SOF_PRIVILEGED_TRAFFIC_CLASS
);
6089 optval
= !(so
->so_flags
& SOF_NODEFUNCT
);
6093 optval
= (so
->so_flags
& SOF_DEFUNCT
);
6096 case SO_OPPORTUNISTIC
:
6097 optval
= so_get_opportunistic(so
);
6101 /* This option is not gettable */
6106 optval
= so_get_recv_anyif(so
);
6109 case SO_TRAFFIC_MGT_BACKGROUND
:
6110 /* This option is handled by lower layer(s) */
6111 if (so
->so_proto
!= NULL
&&
6112 so
->so_proto
->pr_ctloutput
!= NULL
) {
6113 (void) so
->so_proto
->pr_ctloutput(so
, sopt
);
6118 case SO_FLOW_DIVERT_TOKEN
:
6119 error
= flow_divert_token_get(so
, sopt
);
6121 #endif /* FLOW_DIVERT */
6124 case SO_NECP_ATTRIBUTES
:
6125 error
= necp_get_socket_attributes(so
, sopt
);
6128 case SO_NECP_CLIENTUUID
: {
6131 if (SOCK_DOM(so
) == PF_MULTIPATH
) {
6132 ncu
= &mpsotomppcb(so
)->necp_client_uuid
;
6133 } else if (SOCK_DOM(so
) == PF_INET
|| SOCK_DOM(so
) == PF_INET6
) {
6134 ncu
= &sotoinpcb(so
)->necp_client_uuid
;
6140 error
= sooptcopyout(sopt
, ncu
, sizeof(uuid_t
));
6144 case SO_NECP_LISTENUUID
: {
6147 if (SOCK_DOM(so
) == PF_INET
|| SOCK_DOM(so
) == PF_INET6
) {
6148 if (sotoinpcb(so
)->inp_flags2
& INP2_EXTERNAL_PORT
) {
6149 nlu
= &sotoinpcb(so
)->necp_client_uuid
;
6159 error
= sooptcopyout(sopt
, nlu
, sizeof(uuid_t
));
6165 case SO_CFIL_SOCK_ID
: {
6166 cfil_sock_id_t sock_id
;
6168 sock_id
= cfil_sock_id_from_socket(so
);
6170 error
= sooptcopyout(sopt
, &sock_id
,
6171 sizeof(cfil_sock_id_t
));
6174 #endif /* CONTENT_FILTER */
6176 case SO_EXTENDED_BK_IDLE
:
6177 optval
= (so
->so_flags1
& SOF1_EXTEND_BK_IDLE_WANTED
);
6179 case SO_MARK_CELLFALLBACK
:
6180 optval
= ((so
->so_flags1
& SOF1_CELLFALLBACK
) > 0)
6183 case SO_NET_SERVICE_TYPE
: {
6184 if ((so
->so_flags1
& SOF1_TC_NET_SERV_TYPE
)) {
6185 optval
= so
->so_netsvctype
;
6187 optval
= NET_SERVICE_TYPE_BE
;
6191 case SO_NETSVC_MARKING_LEVEL
:
6192 optval
= so_get_netsvc_marking_level(so
);
6195 case SO_MPKL_SEND_INFO
: {
6196 struct so_mpkl_send_info so_mpkl_send_info
;
6198 uuid_copy(so_mpkl_send_info
.mpkl_uuid
, so
->so_mpkl_send_uuid
);
6199 so_mpkl_send_info
.mpkl_proto
= so
->so_mpkl_send_proto
;
6200 error
= sooptcopyout(sopt
, &so_mpkl_send_info
,
6201 sizeof(struct so_mpkl_send_info
));
6205 error
= ENOPROTOOPT
;
6211 socket_unlock(so
, 1);
6217 * The size limits on our soopt_getm is different from that on FreeBSD.
6218 * We limit the size of options to MCLBYTES. This will have to change
6219 * if we need to define options that need more space than MCLBYTES.
6222 soopt_getm(struct sockopt
*sopt
, struct mbuf
**mp
)
6224 struct mbuf
*m
, *m_prev
;
6225 int sopt_size
= sopt
->sopt_valsize
;
6228 if (sopt_size
<= 0 || sopt_size
> MCLBYTES
) {
6232 how
= sopt
->sopt_p
!= kernproc
? M_WAIT
: M_DONTWAIT
;
6233 MGET(m
, how
, MT_DATA
);
6237 if (sopt_size
> MLEN
) {
6239 if ((m
->m_flags
& M_EXT
) == 0) {
6243 m
->m_len
= min(MCLBYTES
, sopt_size
);
6245 m
->m_len
= min(MLEN
, sopt_size
);
6247 sopt_size
-= m
->m_len
;
6251 while (sopt_size
> 0) {
6252 MGET(m
, how
, MT_DATA
);
6257 if (sopt_size
> MLEN
) {
6259 if ((m
->m_flags
& M_EXT
) == 0) {
6264 m
->m_len
= min(MCLBYTES
, sopt_size
);
6266 m
->m_len
= min(MLEN
, sopt_size
);
6268 sopt_size
-= m
->m_len
;
6275 /* copyin sopt data into mbuf chain */
6277 soopt_mcopyin(struct sockopt
*sopt
, struct mbuf
*m
)
6279 struct mbuf
*m0
= m
;
6281 if (sopt
->sopt_val
== USER_ADDR_NULL
) {
6284 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
6285 if (sopt
->sopt_p
!= kernproc
) {
6288 error
= copyin(sopt
->sopt_val
, mtod(m
, char *),
6295 bcopy(CAST_DOWN(caddr_t
, sopt
->sopt_val
),
6296 mtod(m
, char *), m
->m_len
);
6298 sopt
->sopt_valsize
-= m
->m_len
;
6299 sopt
->sopt_val
+= m
->m_len
;
6302 /* should be allocated enoughly at ip6_sooptmcopyin() */
6304 panic("soopt_mcopyin");
6310 /* copyout mbuf chain data into soopt */
6312 soopt_mcopyout(struct sockopt
*sopt
, struct mbuf
*m
)
6314 struct mbuf
*m0
= m
;
6317 if (sopt
->sopt_val
== USER_ADDR_NULL
) {
6320 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
6321 if (sopt
->sopt_p
!= kernproc
) {
6324 error
= copyout(mtod(m
, char *), sopt
->sopt_val
,
6331 bcopy(mtod(m
, char *),
6332 CAST_DOWN(caddr_t
, sopt
->sopt_val
), m
->m_len
);
6334 sopt
->sopt_valsize
-= m
->m_len
;
6335 sopt
->sopt_val
+= m
->m_len
;
6336 valsize
+= m
->m_len
;
6340 /* enough soopt buffer should be given from user-land */
6344 sopt
->sopt_valsize
= valsize
;
6349 sohasoutofband(struct socket
*so
)
6351 if (so
->so_pgid
< 0) {
6352 gsignal(-so
->so_pgid
, SIGURG
);
6353 } else if (so
->so_pgid
> 0) {
6354 proc_signal(so
->so_pgid
, SIGURG
);
6356 selwakeup(&so
->so_rcv
.sb_sel
);
6357 if (so
->so_rcv
.sb_flags
& SB_KNOTE
) {
6358 KNOTE(&so
->so_rcv
.sb_sel
.si_note
,
6359 (NOTE_OOB
| SO_FILT_HINT_LOCKED
));
6364 sopoll(struct socket
*so
, int events
, kauth_cred_t cred
, void * wql
)
6366 #pragma unused(cred)
6367 struct proc
*p
= current_proc();
6371 so_update_last_owner_locked(so
, PROC_NULL
);
6372 so_update_policy(so
);
6374 if (events
& (POLLIN
| POLLRDNORM
)) {
6375 if (soreadable(so
)) {
6376 revents
|= events
& (POLLIN
| POLLRDNORM
);
6380 if (events
& (POLLOUT
| POLLWRNORM
)) {
6381 if (sowriteable(so
)) {
6382 revents
|= events
& (POLLOUT
| POLLWRNORM
);
6386 if (events
& (POLLPRI
| POLLRDBAND
)) {
6387 if (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
)) {
6388 revents
|= events
& (POLLPRI
| POLLRDBAND
);
6393 if (events
& (POLLIN
| POLLPRI
| POLLRDNORM
| POLLRDBAND
)) {
6395 * Darwin sets the flag first,
6396 * BSD calls selrecord first
6398 so
->so_rcv
.sb_flags
|= SB_SEL
;
6399 selrecord(p
, &so
->so_rcv
.sb_sel
, wql
);
6402 if (events
& (POLLOUT
| POLLWRNORM
)) {
6404 * Darwin sets the flag first,
6405 * BSD calls selrecord first
6407 so
->so_snd
.sb_flags
|= SB_SEL
;
6408 selrecord(p
, &so
->so_snd
.sb_sel
, wql
);
6412 socket_unlock(so
, 1);
6417 soo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct kevent_qos_s
*kev
)
6419 struct socket
*so
= (struct socket
*)fp
->f_fglob
->fg_data
;
6423 so_update_last_owner_locked(so
, PROC_NULL
);
6424 so_update_policy(so
);
6426 #if CONFIG_MACF_SOCKET
6427 proc_t p
= knote_get_kq(kn
)->kq_p
;
6428 if (mac_socket_check_kqfilter(proc_ucred(p
), kn
, so
) != 0) {
6429 socket_unlock(so
, 1);
6430 knote_set_error(kn
, EPERM
);
6433 #endif /* MAC_SOCKET */
6435 switch (kn
->kn_filter
) {
6437 kn
->kn_filtid
= EVFILTID_SOREAD
;
6440 kn
->kn_filtid
= EVFILTID_SOWRITE
;
6443 kn
->kn_filtid
= EVFILTID_SCK
;
6446 kn
->kn_filtid
= EVFILTID_SOEXCEPT
;
6449 socket_unlock(so
, 1);
6450 knote_set_error(kn
, EINVAL
);
6455 * call the appropriate sub-filter attach
6456 * with the socket still locked
6458 result
= knote_fops(kn
)->f_attach(kn
, kev
);
6460 socket_unlock(so
, 1);
6466 filt_soread_common(struct knote
*kn
, struct kevent_qos_s
*kev
, struct socket
*so
)
6471 if (so
->so_options
& SO_ACCEPTCONN
) {
6473 * Radar 6615193 handle the listen case dynamically
6474 * for kqueue read filter. This allows to call listen()
6475 * after registering the kqueue EVFILT_READ.
6478 retval
= !TAILQ_EMPTY(&so
->so_comp
);
6483 /* socket isn't a listener */
6485 * NOTE_LOWAT specifies new low water mark in data, i.e.
6486 * the bytes of protocol data. We therefore exclude any
6489 data
= so
->so_rcv
.sb_cc
- so
->so_rcv
.sb_ctl
;
6491 if (kn
->kn_sfflags
& NOTE_OOB
) {
6492 if (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
)) {
6493 kn
->kn_fflags
|= NOTE_OOB
;
6494 data
-= so
->so_oobmark
;
6500 if ((so
->so_state
& SS_CANTRCVMORE
)
6502 && cfil_sock_data_pending(&so
->so_rcv
) == 0
6503 #endif /* CONTENT_FILTER */
6505 kn
->kn_flags
|= EV_EOF
;
6506 kn
->kn_fflags
= so
->so_error
;
6511 if (so
->so_error
) { /* temporary udp error */
6516 int64_t lowwat
= so
->so_rcv
.sb_lowat
;
6518 * Ensure that when NOTE_LOWAT is used, the derived
6519 * low water mark is bounded by socket's rcv buf's
6520 * high and low water mark values.
6522 if (kn
->kn_sfflags
& NOTE_LOWAT
) {
6523 if (kn
->kn_sdata
> so
->so_rcv
.sb_hiwat
) {
6524 lowwat
= so
->so_rcv
.sb_hiwat
;
6525 } else if (kn
->kn_sdata
> lowwat
) {
6526 lowwat
= kn
->kn_sdata
;
6530 retval
= (data
>= lowwat
);
6533 if (retval
&& kev
) {
6534 knote_fill_kevent(kn
, kev
, data
);
6540 filt_sorattach(struct knote
*kn
, __unused
struct kevent_qos_s
*kev
)
6542 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
6547 * If the caller explicitly asked for OOB results (e.g. poll())
6548 * from EVFILT_READ, then save that off in the hookid field
6549 * and reserve the kn_flags EV_OOBAND bit for output only.
6551 if (kn
->kn_filter
== EVFILT_READ
&&
6552 kn
->kn_flags
& EV_OOBAND
) {
6553 kn
->kn_flags
&= ~EV_OOBAND
;
6554 kn
->kn_hook32
= EV_OOBAND
;
6558 if (KNOTE_ATTACH(&so
->so_rcv
.sb_sel
.si_note
, kn
)) {
6559 so
->so_rcv
.sb_flags
|= SB_KNOTE
;
6562 /* indicate if event is already fired */
6563 return filt_soread_common(kn
, NULL
, so
);
6567 filt_sordetach(struct knote
*kn
)
6569 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
6572 if (so
->so_rcv
.sb_flags
& SB_KNOTE
) {
6573 if (KNOTE_DETACH(&so
->so_rcv
.sb_sel
.si_note
, kn
)) {
6574 so
->so_rcv
.sb_flags
&= ~SB_KNOTE
;
6577 socket_unlock(so
, 1);
6582 filt_soread(struct knote
*kn
, long hint
)
6584 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
6587 if ((hint
& SO_FILT_HINT_LOCKED
) == 0) {
6591 retval
= filt_soread_common(kn
, NULL
, so
);
6593 if ((hint
& SO_FILT_HINT_LOCKED
) == 0) {
6594 socket_unlock(so
, 1);
6601 filt_sortouch(struct knote
*kn
, struct kevent_qos_s
*kev
)
6603 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
6608 /* save off the new input fflags and data */
6609 kn
->kn_sfflags
= kev
->fflags
;
6610 kn
->kn_sdata
= kev
->data
;
6612 /* determine if changes result in fired events */
6613 retval
= filt_soread_common(kn
, NULL
, so
);
6615 socket_unlock(so
, 1);
6621 filt_sorprocess(struct knote
*kn
, struct kevent_qos_s
*kev
)
6623 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
6627 retval
= filt_soread_common(kn
, kev
, so
);
6628 socket_unlock(so
, 1);
6634 so_wait_for_if_feedback(struct socket
*so
)
6636 if ((SOCK_DOM(so
) == PF_INET
|| SOCK_DOM(so
) == PF_INET6
) &&
6637 (so
->so_state
& SS_ISCONNECTED
)) {
6638 struct inpcb
*inp
= sotoinpcb(so
);
6639 if (INP_WAIT_FOR_IF_FEEDBACK(inp
)) {
6647 filt_sowrite_common(struct knote
*kn
, struct kevent_qos_s
*kev
, struct socket
*so
)
6650 int64_t data
= sbspace(&so
->so_snd
);
6652 if (so
->so_state
& SS_CANTSENDMORE
) {
6653 kn
->kn_flags
|= EV_EOF
;
6654 kn
->kn_fflags
= so
->so_error
;
6659 if (so
->so_error
) { /* temporary udp error */
6664 if (!socanwrite(so
)) {
6669 if (so
->so_flags1
& SOF1_PRECONNECT_DATA
) {
6674 int64_t lowwat
= so
->so_snd
.sb_lowat
;
6676 if (kn
->kn_sfflags
& NOTE_LOWAT
) {
6677 if (kn
->kn_sdata
> so
->so_snd
.sb_hiwat
) {
6678 lowwat
= so
->so_snd
.sb_hiwat
;
6679 } else if (kn
->kn_sdata
> lowwat
) {
6680 lowwat
= kn
->kn_sdata
;
6684 if (data
>= lowwat
) {
6685 if ((so
->so_flags
& SOF_NOTSENT_LOWAT
)
6686 #if (DEBUG || DEVELOPMENT)
6687 && so_notsent_lowat_check
== 1
6688 #endif /* DEBUG || DEVELOPMENT */
6690 if ((SOCK_DOM(so
) == PF_INET
||
6691 SOCK_DOM(so
) == PF_INET6
) &&
6692 so
->so_type
== SOCK_STREAM
) {
6693 ret
= tcp_notsent_lowat_check(so
);
6696 else if ((SOCK_DOM(so
) == PF_MULTIPATH
) &&
6697 (SOCK_PROTO(so
) == IPPROTO_TCP
)) {
6698 ret
= mptcp_notsent_lowat_check(so
);
6709 if (so_wait_for_if_feedback(so
)) {
6715 knote_fill_kevent(kn
, kev
, data
);
6721 filt_sowattach(struct knote
*kn
, __unused
struct kevent_qos_s
*kev
)
6723 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
6726 if (KNOTE_ATTACH(&so
->so_snd
.sb_sel
.si_note
, kn
)) {
6727 so
->so_snd
.sb_flags
|= SB_KNOTE
;
6730 /* determine if its already fired */
6731 return filt_sowrite_common(kn
, NULL
, so
);
6735 filt_sowdetach(struct knote
*kn
)
6737 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
6740 if (so
->so_snd
.sb_flags
& SB_KNOTE
) {
6741 if (KNOTE_DETACH(&so
->so_snd
.sb_sel
.si_note
, kn
)) {
6742 so
->so_snd
.sb_flags
&= ~SB_KNOTE
;
6745 socket_unlock(so
, 1);
6750 filt_sowrite(struct knote
*kn
, long hint
)
6752 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
6755 if ((hint
& SO_FILT_HINT_LOCKED
) == 0) {
6759 ret
= filt_sowrite_common(kn
, NULL
, so
);
6761 if ((hint
& SO_FILT_HINT_LOCKED
) == 0) {
6762 socket_unlock(so
, 1);
6769 filt_sowtouch(struct knote
*kn
, struct kevent_qos_s
*kev
)
6771 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
6776 /*save off the new input fflags and data */
6777 kn
->kn_sfflags
= kev
->fflags
;
6778 kn
->kn_sdata
= kev
->data
;
6780 /* determine if these changes result in a triggered event */
6781 ret
= filt_sowrite_common(kn
, NULL
, so
);
6783 socket_unlock(so
, 1);
6789 filt_sowprocess(struct knote
*kn
, struct kevent_qos_s
*kev
)
6791 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
6795 ret
= filt_sowrite_common(kn
, kev
, so
);
6796 socket_unlock(so
, 1);
6802 filt_sockev_common(struct knote
*kn
, struct kevent_qos_s
*kev
,
6803 struct socket
*so
, long ev_hint
)
6807 uint32_t level_trigger
= 0;
6809 if (ev_hint
& SO_FILT_HINT_CONNRESET
) {
6810 kn
->kn_fflags
|= NOTE_CONNRESET
;
6812 if (ev_hint
& SO_FILT_HINT_TIMEOUT
) {
6813 kn
->kn_fflags
|= NOTE_TIMEOUT
;
6815 if (ev_hint
& SO_FILT_HINT_NOSRCADDR
) {
6816 kn
->kn_fflags
|= NOTE_NOSRCADDR
;
6818 if (ev_hint
& SO_FILT_HINT_IFDENIED
) {
6819 kn
->kn_fflags
|= NOTE_IFDENIED
;
6821 if (ev_hint
& SO_FILT_HINT_KEEPALIVE
) {
6822 kn
->kn_fflags
|= NOTE_KEEPALIVE
;
6824 if (ev_hint
& SO_FILT_HINT_ADAPTIVE_WTIMO
) {
6825 kn
->kn_fflags
|= NOTE_ADAPTIVE_WTIMO
;
6827 if (ev_hint
& SO_FILT_HINT_ADAPTIVE_RTIMO
) {
6828 kn
->kn_fflags
|= NOTE_ADAPTIVE_RTIMO
;
6830 if ((ev_hint
& SO_FILT_HINT_CONNECTED
) ||
6831 (so
->so_state
& SS_ISCONNECTED
)) {
6832 kn
->kn_fflags
|= NOTE_CONNECTED
;
6833 level_trigger
|= NOTE_CONNECTED
;
6835 if ((ev_hint
& SO_FILT_HINT_DISCONNECTED
) ||
6836 (so
->so_state
& SS_ISDISCONNECTED
)) {
6837 kn
->kn_fflags
|= NOTE_DISCONNECTED
;
6838 level_trigger
|= NOTE_DISCONNECTED
;
6840 if (ev_hint
& SO_FILT_HINT_CONNINFO_UPDATED
) {
6841 if (so
->so_proto
!= NULL
&&
6842 (so
->so_proto
->pr_flags
& PR_EVCONNINFO
)) {
6843 kn
->kn_fflags
|= NOTE_CONNINFO_UPDATED
;
6847 if ((ev_hint
& SO_FILT_HINT_NOTIFY_ACK
) ||
6848 tcp_notify_ack_active(so
)) {
6849 kn
->kn_fflags
|= NOTE_NOTIFY_ACK
;
6852 if ((so
->so_state
& SS_CANTRCVMORE
)
6854 && cfil_sock_data_pending(&so
->so_rcv
) == 0
6855 #endif /* CONTENT_FILTER */
6857 kn
->kn_fflags
|= NOTE_READCLOSED
;
6858 level_trigger
|= NOTE_READCLOSED
;
6861 if (so
->so_state
& SS_CANTSENDMORE
) {
6862 kn
->kn_fflags
|= NOTE_WRITECLOSED
;
6863 level_trigger
|= NOTE_WRITECLOSED
;
6866 if ((ev_hint
& SO_FILT_HINT_SUSPEND
) ||
6867 (so
->so_flags
& SOF_SUSPENDED
)) {
6868 kn
->kn_fflags
&= ~(NOTE_SUSPEND
| NOTE_RESUME
);
6870 /* If resume event was delivered before, reset it */
6871 kn
->kn_hook32
&= ~NOTE_RESUME
;
6873 kn
->kn_fflags
|= NOTE_SUSPEND
;
6874 level_trigger
|= NOTE_SUSPEND
;
6877 if ((ev_hint
& SO_FILT_HINT_RESUME
) ||
6878 (so
->so_flags
& SOF_SUSPENDED
) == 0) {
6879 kn
->kn_fflags
&= ~(NOTE_SUSPEND
| NOTE_RESUME
);
6881 /* If suspend event was delivered before, reset it */
6882 kn
->kn_hook32
&= ~NOTE_SUSPEND
;
6884 kn
->kn_fflags
|= NOTE_RESUME
;
6885 level_trigger
|= NOTE_RESUME
;
6888 if (so
->so_error
!= 0) {
6890 data
= so
->so_error
;
6891 kn
->kn_flags
|= EV_EOF
;
6894 get_sockev_state(so
, &data32
);
6898 /* Reset any events that are not requested on this knote */
6899 kn
->kn_fflags
&= (kn
->kn_sfflags
& EVFILT_SOCK_ALL_MASK
);
6900 level_trigger
&= (kn
->kn_sfflags
& EVFILT_SOCK_ALL_MASK
);
6902 /* Find the level triggerred events that are already delivered */
6903 level_trigger
&= kn
->kn_hook32
;
6904 level_trigger
&= EVFILT_SOCK_LEVEL_TRIGGER_MASK
;
6906 /* Do not deliver level triggerred events more than once */
6907 if ((kn
->kn_fflags
& ~level_trigger
) != 0) {
6913 * Store the state of the events being delivered. This
6914 * state can be used to deliver level triggered events
6915 * ateast once and still avoid waking up the application
6916 * multiple times as long as the event is active.
6918 if (kn
->kn_fflags
!= 0) {
6919 kn
->kn_hook32
|= (kn
->kn_fflags
&
6920 EVFILT_SOCK_LEVEL_TRIGGER_MASK
);
6924 * NOTE_RESUME and NOTE_SUSPEND are an exception, deliver
6925 * only one of them and remember the last one that was
6928 if (kn
->kn_fflags
& NOTE_SUSPEND
) {
6929 kn
->kn_hook32
&= ~NOTE_RESUME
;
6931 if (kn
->kn_fflags
& NOTE_RESUME
) {
6932 kn
->kn_hook32
&= ~NOTE_SUSPEND
;
6935 knote_fill_kevent(kn
, kev
, data
);
6941 filt_sockattach(struct knote
*kn
, __unused
struct kevent_qos_s
*kev
)
6943 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
6947 if (KNOTE_ATTACH(&so
->so_klist
, kn
)) {
6948 so
->so_flags
|= SOF_KNOTE
;
6951 /* determine if event already fired */
6952 return filt_sockev_common(kn
, NULL
, so
, 0);
6956 filt_sockdetach(struct knote
*kn
)
6958 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
6961 if ((so
->so_flags
& SOF_KNOTE
) != 0) {
6962 if (KNOTE_DETACH(&so
->so_klist
, kn
)) {
6963 so
->so_flags
&= ~SOF_KNOTE
;
6966 socket_unlock(so
, 1);
6970 filt_sockev(struct knote
*kn
, long hint
)
6972 int ret
= 0, locked
= 0;
6973 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
6974 long ev_hint
= (hint
& SO_FILT_HINT_EV
);
6976 if ((hint
& SO_FILT_HINT_LOCKED
) == 0) {
6981 ret
= filt_sockev_common(kn
, NULL
, so
, ev_hint
);
6984 socket_unlock(so
, 1);
6993 * filt_socktouch - update event state
6998 struct kevent_qos_s
*kev
)
7000 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
7001 uint32_t changed_flags
;
7006 /* save off the [result] data and fflags */
7007 changed_flags
= (kn
->kn_sfflags
^ kn
->kn_hook32
);
7009 /* save off the new input fflags and data */
7010 kn
->kn_sfflags
= kev
->fflags
;
7011 kn
->kn_sdata
= kev
->data
;
7013 /* restrict the current results to the (smaller?) set of new interest */
7015 * For compatibility with previous implementations, we leave kn_fflags
7016 * as they were before.
7018 //kn->kn_fflags &= kev->fflags;
7021 * Since we keep track of events that are already
7022 * delivered, if any of those events are not requested
7023 * anymore the state related to them can be reset
7025 kn
->kn_hook32
&= ~(changed_flags
& EVFILT_SOCK_LEVEL_TRIGGER_MASK
);
7027 /* determine if we have events to deliver */
7028 ret
= filt_sockev_common(kn
, NULL
, so
, 0);
7030 socket_unlock(so
, 1);
7036 * filt_sockprocess - query event fired state and return data
7039 filt_sockprocess(struct knote
*kn
, struct kevent_qos_s
*kev
)
7041 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
7046 ret
= filt_sockev_common(kn
, kev
, so
, 0);
7048 socket_unlock(so
, 1);
7054 get_sockev_state(struct socket
*so
, u_int32_t
*statep
)
7056 u_int32_t state
= *(statep
);
7059 * If the state variable is already used by a previous event,
7066 if (so
->so_state
& SS_ISCONNECTED
) {
7067 state
|= SOCKEV_CONNECTED
;
7069 state
&= ~(SOCKEV_CONNECTED
);
7071 state
|= ((so
->so_state
& SS_ISDISCONNECTED
) ? SOCKEV_DISCONNECTED
: 0);
7075 #define SO_LOCK_HISTORY_STR_LEN \
7076 (2 * SO_LCKDBG_MAX * (2 + (2 * sizeof (void *)) + 1) + 1)
7078 __private_extern__
const char *
7079 solockhistory_nr(struct socket
*so
)
7083 static char lock_history_str
[SO_LOCK_HISTORY_STR_LEN
];
7085 bzero(lock_history_str
, sizeof(lock_history_str
));
7086 for (i
= SO_LCKDBG_MAX
- 1; i
>= 0; i
--) {
7087 n
+= snprintf(lock_history_str
+ n
,
7088 SO_LOCK_HISTORY_STR_LEN
- n
, "%p:%p ",
7089 so
->lock_lr
[(so
->next_lock_lr
+ i
) % SO_LCKDBG_MAX
],
7090 so
->unlock_lr
[(so
->next_unlock_lr
+ i
) % SO_LCKDBG_MAX
]);
7092 return lock_history_str
;
7096 socket_getlock(struct socket
*so
, int flags
)
7098 if (so
->so_proto
->pr_getlock
!= NULL
) {
7099 return (*so
->so_proto
->pr_getlock
)(so
, flags
);
7101 return so
->so_proto
->pr_domain
->dom_mtx
;
7106 socket_lock(struct socket
*so
, int refcount
)
7110 lr_saved
= __builtin_return_address(0);
7112 if (so
->so_proto
->pr_lock
) {
7113 (*so
->so_proto
->pr_lock
)(so
, refcount
, lr_saved
);
7115 #ifdef MORE_LOCKING_DEBUG
7116 LCK_MTX_ASSERT(so
->so_proto
->pr_domain
->dom_mtx
,
7117 LCK_MTX_ASSERT_NOTOWNED
);
7119 lck_mtx_lock(so
->so_proto
->pr_domain
->dom_mtx
);
7123 so
->lock_lr
[so
->next_lock_lr
] = lr_saved
;
7124 so
->next_lock_lr
= (so
->next_lock_lr
+ 1) % SO_LCKDBG_MAX
;
7129 socket_lock_assert_owned(struct socket
*so
)
7131 lck_mtx_t
*mutex_held
;
7133 if (so
->so_proto
->pr_getlock
!= NULL
) {
7134 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
7136 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
7139 LCK_MTX_ASSERT(mutex_held
, LCK_MTX_ASSERT_OWNED
);
7143 socket_try_lock(struct socket
*so
)
7147 if (so
->so_proto
->pr_getlock
!= NULL
) {
7148 mtx
= (*so
->so_proto
->pr_getlock
)(so
, 0);
7150 mtx
= so
->so_proto
->pr_domain
->dom_mtx
;
7153 return lck_mtx_try_lock(mtx
);
7157 socket_unlock(struct socket
*so
, int refcount
)
7160 lck_mtx_t
*mutex_held
;
7162 lr_saved
= __builtin_return_address(0);
7164 if (so
== NULL
|| so
->so_proto
== NULL
) {
7165 panic("%s: null so_proto so=%p\n", __func__
, so
);
7169 if (so
->so_proto
->pr_unlock
) {
7170 (*so
->so_proto
->pr_unlock
)(so
, refcount
, lr_saved
);
7172 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
7173 #ifdef MORE_LOCKING_DEBUG
7174 LCK_MTX_ASSERT(mutex_held
, LCK_MTX_ASSERT_OWNED
);
7176 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
7177 so
->next_unlock_lr
= (so
->next_unlock_lr
+ 1) % SO_LCKDBG_MAX
;
7180 if (so
->so_usecount
<= 0) {
7181 panic("%s: bad refcount=%d so=%p (%d, %d, %d) "
7182 "lrh=%s", __func__
, so
->so_usecount
, so
,
7183 SOCK_DOM(so
), so
->so_type
,
7184 SOCK_PROTO(so
), solockhistory_nr(so
));
7189 if (so
->so_usecount
== 0) {
7190 sofreelastref(so
, 1);
7193 lck_mtx_unlock(mutex_held
);
7197 /* Called with socket locked, will unlock socket */
7199 sofree(struct socket
*so
)
7201 lck_mtx_t
*mutex_held
;
7203 if (so
->so_proto
->pr_getlock
!= NULL
) {
7204 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
7206 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
7208 LCK_MTX_ASSERT(mutex_held
, LCK_MTX_ASSERT_OWNED
);
7210 sofreelastref(so
, 0);
7214 soreference(struct socket
*so
)
7216 socket_lock(so
, 1); /* locks & take one reference on socket */
7217 socket_unlock(so
, 0); /* unlock only */
7221 sodereference(struct socket
*so
)
7224 socket_unlock(so
, 1);
7228 * Set or clear SOF_MULTIPAGES on the socket to enable or disable the
7229 * possibility of using jumbo clusters. Caller must ensure to hold
7233 somultipages(struct socket
*so
, boolean_t set
)
7236 so
->so_flags
|= SOF_MULTIPAGES
;
7238 so
->so_flags
&= ~SOF_MULTIPAGES
;
7243 soif2kcl(struct socket
*so
, boolean_t set
)
7246 so
->so_flags1
|= SOF1_IF_2KCL
;
7248 so
->so_flags1
&= ~SOF1_IF_2KCL
;
7253 so_isdstlocal(struct socket
*so
)
7255 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
7257 if (SOCK_DOM(so
) == PF_INET
) {
7258 return inaddr_local(inp
->inp_faddr
);
7259 } else if (SOCK_DOM(so
) == PF_INET6
) {
7260 return in6addr_local(&inp
->in6p_faddr
);
7267 sosetdefunct(struct proc
*p
, struct socket
*so
, int level
, boolean_t noforce
)
7269 struct sockbuf
*rcv
, *snd
;
7270 int err
= 0, defunct
;
7275 defunct
= (so
->so_flags
& SOF_DEFUNCT
);
7277 if (!(snd
->sb_flags
& rcv
->sb_flags
& SB_DROP
)) {
7278 panic("%s: SB_DROP not set", __func__
);
7284 if (so
->so_flags
& SOF_NODEFUNCT
) {
7287 if (p
!= PROC_NULL
) {
7288 SODEFUNCTLOG("%s[%d, %s]: (target pid %d "
7289 "name %s level %d) so 0x%llx [%d,%d] "
7290 "is not eligible for defunct "
7291 "(%d)\n", __func__
, proc_selfpid(),
7292 proc_best_name(current_proc()), proc_pid(p
),
7293 proc_best_name(p
), level
,
7294 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
7295 SOCK_DOM(so
), SOCK_TYPE(so
), err
);
7299 so
->so_flags
&= ~SOF_NODEFUNCT
;
7300 if (p
!= PROC_NULL
) {
7301 SODEFUNCTLOG("%s[%d, %s]: (target pid %d "
7302 "name %s level %d) so 0x%llx [%d,%d] "
7304 "(%d)\n", __func__
, proc_selfpid(),
7305 proc_best_name(current_proc()), proc_pid(p
),
7306 proc_best_name(p
), level
,
7307 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
7308 SOCK_DOM(so
), SOCK_TYPE(so
), err
);
7310 } else if (so
->so_flags1
& SOF1_EXTEND_BK_IDLE_WANTED
) {
7311 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
7312 struct ifnet
*ifp
= inp
->inp_last_outifp
;
7314 if (ifp
&& IFNET_IS_CELLULAR(ifp
)) {
7315 OSIncrementAtomic(&soextbkidlestat
.so_xbkidle_nocell
);
7316 } else if (so
->so_flags
& SOF_DELEGATED
) {
7317 OSIncrementAtomic(&soextbkidlestat
.so_xbkidle_nodlgtd
);
7318 } else if (soextbkidlestat
.so_xbkidle_time
== 0) {
7319 OSIncrementAtomic(&soextbkidlestat
.so_xbkidle_notime
);
7320 } else if (noforce
&& p
!= PROC_NULL
) {
7321 OSIncrementAtomic(&soextbkidlestat
.so_xbkidle_active
);
7323 so
->so_flags1
|= SOF1_EXTEND_BK_IDLE_INPROG
;
7324 so
->so_extended_bk_start
= net_uptime();
7325 OSBitOrAtomic(P_LXBKIDLEINPROG
, &p
->p_ladvflag
);
7327 inpcb_timer_sched(inp
->inp_pcbinfo
, INPCB_TIMER_LAZY
);
7330 SODEFUNCTLOG("%s[%d, %s]: (target pid %d "
7331 "name %s level %d) so 0x%llx [%d,%d] "
7333 "(%d)\n", __func__
, proc_selfpid(),
7334 proc_best_name(current_proc()), proc_pid(p
),
7335 proc_best_name(p
), level
,
7336 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
7337 SOCK_DOM(so
), SOCK_TYPE(so
), err
);
7340 OSIncrementAtomic(&soextbkidlestat
.so_xbkidle_forced
);
7344 so
->so_flags
|= SOF_DEFUNCT
;
7346 /* Prevent further data from being appended to the socket buffers */
7347 snd
->sb_flags
|= SB_DROP
;
7348 rcv
->sb_flags
|= SB_DROP
;
7350 /* Flush any existing data in the socket buffers */
7351 if (rcv
->sb_cc
!= 0) {
7352 rcv
->sb_flags
&= ~SB_SEL
;
7353 selthreadclear(&rcv
->sb_sel
);
7356 if (snd
->sb_cc
!= 0) {
7357 snd
->sb_flags
&= ~SB_SEL
;
7358 selthreadclear(&snd
->sb_sel
);
7363 if (p
!= PROC_NULL
) {
7364 SODEFUNCTLOG("%s[%d, %s]: (target pid %d name %s level %d) "
7365 "so 0x%llx [%d,%d] %s defunct%s\n", __func__
,
7366 proc_selfpid(), proc_best_name(current_proc()),
7367 proc_pid(p
), proc_best_name(p
), level
,
7368 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
), SOCK_DOM(so
),
7369 SOCK_TYPE(so
), defunct
? "is already" : "marked as",
7370 (so
->so_flags1
& SOF1_EXTEND_BK_IDLE_WANTED
) ?
7377 sodefunct(struct proc
*p
, struct socket
*so
, int level
)
7379 struct sockbuf
*rcv
, *snd
;
7381 if (!(so
->so_flags
& SOF_DEFUNCT
)) {
7382 panic("%s improperly called", __func__
);
7385 if (so
->so_state
& SS_DEFUNCT
) {
7392 if (SOCK_DOM(so
) == PF_INET
|| SOCK_DOM(so
) == PF_INET6
) {
7393 char s
[MAX_IPv6_STR_LEN
];
7394 char d
[MAX_IPv6_STR_LEN
];
7395 struct inpcb
*inp
= sotoinpcb(so
);
7397 if (p
!= PROC_NULL
) {
7399 "%s[%d, %s]: (target pid %d name %s level %d) "
7400 "so 0x%llx [%s %s:%d -> %s:%d] is now defunct "
7401 "[rcv_si 0x%x, snd_si 0x%x, rcv_fl 0x%x, "
7402 " snd_fl 0x%x]\n", __func__
,
7403 proc_selfpid(), proc_best_name(current_proc()),
7404 proc_pid(p
), proc_best_name(p
), level
,
7405 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
7406 (SOCK_TYPE(so
) == SOCK_STREAM
) ? "TCP" : "UDP",
7407 inet_ntop(SOCK_DOM(so
), ((SOCK_DOM(so
) == PF_INET
) ?
7408 (void *)&inp
->inp_laddr
.s_addr
:
7409 (void *)&inp
->in6p_laddr
),
7410 s
, sizeof(s
)), ntohs(inp
->in6p_lport
),
7411 inet_ntop(SOCK_DOM(so
), (SOCK_DOM(so
) == PF_INET
) ?
7412 (void *)&inp
->inp_faddr
.s_addr
:
7413 (void *)&inp
->in6p_faddr
,
7414 d
, sizeof(d
)), ntohs(inp
->in6p_fport
),
7415 (uint32_t)rcv
->sb_sel
.si_flags
,
7416 (uint32_t)snd
->sb_sel
.si_flags
,
7417 rcv
->sb_flags
, snd
->sb_flags
);
7419 } else if (p
!= PROC_NULL
) {
7420 SODEFUNCTLOG("%s[%d, %s]: (target pid %d name %s level %d) "
7421 "so 0x%llx [%d,%d] is now defunct [rcv_si 0x%x, "
7422 "snd_si 0x%x, rcv_fl 0x%x, snd_fl 0x%x]\n", __func__
,
7423 proc_selfpid(), proc_best_name(current_proc()),
7424 proc_pid(p
), proc_best_name(p
), level
,
7425 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
7426 SOCK_DOM(so
), SOCK_TYPE(so
),
7427 (uint32_t)rcv
->sb_sel
.si_flags
,
7428 (uint32_t)snd
->sb_sel
.si_flags
, rcv
->sb_flags
,
7433 * Unwedge threads blocked on sbwait() and sb_lock().
7438 so
->so_flags1
|= SOF1_DEFUNCTINPROG
;
7439 if (rcv
->sb_flags
& SB_LOCK
) {
7440 sbunlock(rcv
, TRUE
); /* keep socket locked */
7442 if (snd
->sb_flags
& SB_LOCK
) {
7443 sbunlock(snd
, TRUE
); /* keep socket locked */
7446 * Flush the buffers and disconnect. We explicitly call shutdown
7447 * on both data directions to ensure that SS_CANT{RCV,SEND}MORE
7448 * states are set for the socket. This would also flush out data
7449 * hanging off the receive list of this socket.
7451 (void) soshutdownlock_final(so
, SHUT_RD
);
7452 (void) soshutdownlock_final(so
, SHUT_WR
);
7453 (void) sodisconnectlocked(so
);
7456 * Explicitly handle connectionless-protocol disconnection
7457 * and release any remaining data in the socket buffers.
7459 if (!(so
->so_state
& SS_ISDISCONNECTED
)) {
7460 (void) soisdisconnected(so
);
7463 if (so
->so_error
== 0) {
7464 so
->so_error
= EBADF
;
7467 if (rcv
->sb_cc
!= 0) {
7468 rcv
->sb_flags
&= ~SB_SEL
;
7469 selthreadclear(&rcv
->sb_sel
);
7472 if (snd
->sb_cc
!= 0) {
7473 snd
->sb_flags
&= ~SB_SEL
;
7474 selthreadclear(&snd
->sb_sel
);
7477 so
->so_state
|= SS_DEFUNCT
;
7478 OSIncrementAtomicLong((volatile long *)&sodefunct_calls
);
7485 soresume(struct proc
*p
, struct socket
*so
, int locked
)
7491 if (so
->so_flags1
& SOF1_EXTEND_BK_IDLE_INPROG
) {
7492 SODEFUNCTLOG("%s[%d, %s]: (target pid %d name %s) so 0x%llx "
7493 "[%d,%d] resumed from bk idle\n",
7494 __func__
, proc_selfpid(), proc_best_name(current_proc()),
7495 proc_pid(p
), proc_best_name(p
),
7496 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
7497 SOCK_DOM(so
), SOCK_TYPE(so
));
7499 so
->so_flags1
&= ~SOF1_EXTEND_BK_IDLE_INPROG
;
7500 so
->so_extended_bk_start
= 0;
7501 OSBitAndAtomic(~P_LXBKIDLEINPROG
, &p
->p_ladvflag
);
7503 OSIncrementAtomic(&soextbkidlestat
.so_xbkidle_resumed
);
7504 OSDecrementAtomic(&soextbkidlestat
.so_xbkidle_active
);
7505 VERIFY(soextbkidlestat
.so_xbkidle_active
>= 0);
7508 socket_unlock(so
, 1);
7515 * Does not attempt to account for sockets that are delegated from
7516 * the current process
7519 so_set_extended_bk_idle(struct socket
*so
, int optval
)
7523 if ((SOCK_DOM(so
) != PF_INET
&& SOCK_DOM(so
) != PF_INET6
) ||
7524 SOCK_PROTO(so
) != IPPROTO_TCP
) {
7525 OSDecrementAtomic(&soextbkidlestat
.so_xbkidle_notsupp
);
7527 } else if (optval
== 0) {
7528 so
->so_flags1
&= ~SOF1_EXTEND_BK_IDLE_WANTED
;
7530 soresume(current_proc(), so
, 1);
7532 struct proc
*p
= current_proc();
7534 struct filedesc
*fdp
;
7538 * Unlock socket to avoid lock ordering issue with
7539 * the proc fd table lock
7541 socket_unlock(so
, 0);
7546 for (i
= 0; i
< fdp
->fd_nfiles
; i
++) {
7547 struct fileproc
*fp
= fdp
->fd_ofiles
[i
];
7551 (fdp
->fd_ofileflags
[i
] & UF_RESERVED
) != 0 ||
7552 FILEGLOB_DTYPE(fp
->f_fglob
) != DTYPE_SOCKET
) {
7556 so2
= (struct socket
*)fp
->f_fglob
->fg_data
;
7558 so2
->so_flags1
& SOF1_EXTEND_BK_IDLE_WANTED
) {
7561 if (count
>= soextbkidlestat
.so_xbkidle_maxperproc
) {
7569 if (count
>= soextbkidlestat
.so_xbkidle_maxperproc
) {
7570 OSIncrementAtomic(&soextbkidlestat
.so_xbkidle_toomany
);
7572 } else if (so
->so_flags
& SOF_DELEGATED
) {
7573 OSIncrementAtomic(&soextbkidlestat
.so_xbkidle_nodlgtd
);
7576 so
->so_flags1
|= SOF1_EXTEND_BK_IDLE_WANTED
;
7577 OSIncrementAtomic(&soextbkidlestat
.so_xbkidle_wantok
);
7579 SODEFUNCTLOG("%s[%d, %s]: so 0x%llx [%d,%d] "
7580 "%s marked for extended bk idle\n",
7581 __func__
, proc_selfpid(), proc_best_name(current_proc()),
7582 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
7583 SOCK_DOM(so
), SOCK_TYPE(so
),
7584 (so
->so_flags1
& SOF1_EXTEND_BK_IDLE_WANTED
) ?
7592 so_stop_extended_bk_idle(struct socket
*so
)
7594 so
->so_flags1
&= ~SOF1_EXTEND_BK_IDLE_INPROG
;
7595 so
->so_extended_bk_start
= 0;
7597 OSDecrementAtomic(&soextbkidlestat
.so_xbkidle_active
);
7598 VERIFY(soextbkidlestat
.so_xbkidle_active
>= 0);
7602 sosetdefunct(current_proc(), so
,
7603 SHUTDOWN_SOCKET_LEVEL_DISCONNECT_INTERNAL
, FALSE
);
7604 if (so
->so_flags
& SOF_DEFUNCT
) {
7605 sodefunct(current_proc(), so
,
7606 SHUTDOWN_SOCKET_LEVEL_DISCONNECT_INTERNAL
);
7611 so_drain_extended_bk_idle(struct socket
*so
)
7613 if (so
&& (so
->so_flags1
& SOF1_EXTEND_BK_IDLE_INPROG
)) {
7615 * Only penalize sockets that have outstanding data
7617 if (so
->so_rcv
.sb_cc
|| so
->so_snd
.sb_cc
) {
7618 so_stop_extended_bk_idle(so
);
7620 OSIncrementAtomic(&soextbkidlestat
.so_xbkidle_drained
);
7626 * Return values tells if socket is still in extended background idle
7629 so_check_extended_bk_idle_time(struct socket
*so
)
7633 if ((so
->so_flags1
& SOF1_EXTEND_BK_IDLE_INPROG
)) {
7634 SODEFUNCTLOG("%s[%d, %s]: so 0x%llx [%d,%d]\n",
7635 __func__
, proc_selfpid(), proc_best_name(current_proc()),
7636 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
7637 SOCK_DOM(so
), SOCK_TYPE(so
));
7638 if (net_uptime() - so
->so_extended_bk_start
>
7639 soextbkidlestat
.so_xbkidle_time
) {
7640 so_stop_extended_bk_idle(so
);
7642 OSIncrementAtomic(&soextbkidlestat
.so_xbkidle_expired
);
7646 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
7648 inpcb_timer_sched(inp
->inp_pcbinfo
, INPCB_TIMER_LAZY
);
7649 OSIncrementAtomic(&soextbkidlestat
.so_xbkidle_resched
);
7657 resume_proc_sockets(proc_t p
)
7659 if (p
->p_ladvflag
& P_LXBKIDLEINPROG
) {
7660 struct filedesc
*fdp
;
7665 for (i
= 0; i
< fdp
->fd_nfiles
; i
++) {
7666 struct fileproc
*fp
;
7669 fp
= fdp
->fd_ofiles
[i
];
7671 (fdp
->fd_ofileflags
[i
] & UF_RESERVED
) != 0 ||
7672 FILEGLOB_DTYPE(fp
->f_fglob
) != DTYPE_SOCKET
) {
7676 so
= (struct socket
*)fp
->f_fglob
->fg_data
;
7677 (void) soresume(p
, so
, 0);
7681 OSBitAndAtomic(~P_LXBKIDLEINPROG
, &p
->p_ladvflag
);
7685 __private_extern__
int
7686 so_set_recv_anyif(struct socket
*so
, int optval
)
7691 if (SOCK_DOM(so
) == PF_INET
|| SOCK_DOM(so
) == PF_INET6
) {
7693 if (SOCK_DOM(so
) == PF_INET
) {
7696 sotoinpcb(so
)->inp_flags
|= INP_RECV_ANYIF
;
7698 sotoinpcb(so
)->inp_flags
&= ~INP_RECV_ANYIF
;
7706 __private_extern__
int
7707 so_get_recv_anyif(struct socket
*so
)
7712 if (SOCK_DOM(so
) == PF_INET
|| SOCK_DOM(so
) == PF_INET6
) {
7714 if (SOCK_DOM(so
) == PF_INET
) {
7716 ret
= (sotoinpcb(so
)->inp_flags
& INP_RECV_ANYIF
) ? 1 : 0;
7723 so_set_restrictions(struct socket
*so
, uint32_t vals
)
7725 int nocell_old
, nocell_new
;
7726 int noexpensive_old
, noexpensive_new
;
7727 int noconstrained_old
, noconstrained_new
;
7730 * Deny-type restrictions are trapdoors; once set they cannot be
7731 * unset for the lifetime of the socket. This allows them to be
7732 * issued by a framework on behalf of the application without
7733 * having to worry that they can be undone.
7735 * Note here that socket-level restrictions overrides any protocol
7736 * level restrictions. For instance, SO_RESTRICT_DENY_CELLULAR
7737 * socket restriction issued on the socket has a higher precendence
7738 * than INP_NO_IFT_CELLULAR. The latter is affected by the UUID
7739 * policy PROC_UUID_NO_CELLULAR for unrestricted sockets only,
7740 * i.e. when SO_RESTRICT_DENY_CELLULAR has not been issued.
7742 nocell_old
= (so
->so_restrictions
& SO_RESTRICT_DENY_CELLULAR
);
7743 noexpensive_old
= (so
->so_restrictions
& SO_RESTRICT_DENY_EXPENSIVE
);
7744 noconstrained_old
= (so
->so_restrictions
& SO_RESTRICT_DENY_CONSTRAINED
);
7745 so
->so_restrictions
|= (vals
& (SO_RESTRICT_DENY_IN
|
7746 SO_RESTRICT_DENY_OUT
| SO_RESTRICT_DENY_CELLULAR
|
7747 SO_RESTRICT_DENY_EXPENSIVE
| SO_RESTRICT_DENY_CONSTRAINED
));
7748 nocell_new
= (so
->so_restrictions
& SO_RESTRICT_DENY_CELLULAR
);
7749 noexpensive_new
= (so
->so_restrictions
& SO_RESTRICT_DENY_EXPENSIVE
);
7750 noconstrained_new
= (so
->so_restrictions
& SO_RESTRICT_DENY_CONSTRAINED
);
7752 /* we can only set, not clear restrictions */
7753 if ((nocell_new
- nocell_old
) == 0 &&
7754 (noexpensive_new
- noexpensive_old
) == 0 &&
7755 (noconstrained_new
- noconstrained_old
) == 0) {
7759 if (SOCK_DOM(so
) == PF_INET
|| SOCK_DOM(so
) == PF_INET6
) {
7761 if (SOCK_DOM(so
) == PF_INET
) {
7763 if (nocell_new
- nocell_old
!= 0) {
7765 * if deny cellular is now set, do what's needed
7768 inp_set_nocellular(sotoinpcb(so
));
7770 if (noexpensive_new
- noexpensive_old
!= 0) {
7771 inp_set_noexpensive(sotoinpcb(so
));
7773 if (noconstrained_new
- noconstrained_old
!= 0) {
7774 inp_set_noconstrained(sotoinpcb(so
));
7778 if (SOCK_DOM(so
) == PF_MULTIPATH
) {
7779 mptcp_set_restrictions(so
);
7786 so_get_restrictions(struct socket
*so
)
7788 return so
->so_restrictions
& (SO_RESTRICT_DENY_IN
|
7789 SO_RESTRICT_DENY_OUT
|
7790 SO_RESTRICT_DENY_CELLULAR
| SO_RESTRICT_DENY_EXPENSIVE
);
7794 so_set_effective_pid(struct socket
*so
, int epid
, struct proc
*p
, boolean_t check_cred
)
7796 struct proc
*ep
= PROC_NULL
;
7799 /* pid 0 is reserved for kernel */
7806 * If this is an in-kernel socket, prevent its delegate
7807 * association from changing unless the socket option is
7808 * coming from within the kernel itself.
7810 if (so
->last_pid
== 0 && p
!= kernproc
) {
7816 * If this is issued by a process that's recorded as the
7817 * real owner of the socket, or if the pid is the same as
7818 * the process's own pid, then proceed. Otherwise ensure
7819 * that the issuing process has the necessary privileges.
7821 if (check_cred
&& (epid
!= so
->last_pid
|| epid
!= proc_pid(p
))) {
7822 if ((error
= priv_check_cred(kauth_cred_get(),
7823 PRIV_NET_PRIVILEGED_SOCKET_DELEGATE
, 0))) {
7829 /* Find the process that corresponds to the effective pid */
7830 if ((ep
= proc_find(epid
)) == PROC_NULL
) {
7836 * If a process tries to delegate the socket to itself, then
7837 * there's really nothing to do; treat it as a way for the
7838 * delegate association to be cleared. Note that we check
7839 * the passed-in proc rather than calling proc_selfpid(),
7840 * as we need to check the process issuing the socket option
7841 * which could be kernproc. Given that we don't allow 0 for
7842 * effective pid, it means that a delegated in-kernel socket
7843 * stays delegated during its lifetime (which is probably OK.)
7845 if (epid
== proc_pid(p
)) {
7846 so
->so_flags
&= ~SOF_DELEGATED
;
7849 uuid_clear(so
->e_uuid
);
7851 so
->so_flags
|= SOF_DELEGATED
;
7852 so
->e_upid
= proc_uniqueid(ep
);
7853 so
->e_pid
= proc_pid(ep
);
7854 proc_getexecutableuuid(ep
, so
->e_uuid
, sizeof(so
->e_uuid
));
7856 if (so
->so_proto
!= NULL
&& so
->so_proto
->pr_update_last_owner
!= NULL
) {
7857 (*so
->so_proto
->pr_update_last_owner
)(so
, NULL
, ep
);
7860 if (error
== 0 && net_io_policy_log
) {
7863 uuid_unparse(so
->e_uuid
, buf
);
7864 log(LOG_DEBUG
, "%s[%s,%d]: so 0x%llx [%d,%d] epid %d (%s) "
7865 "euuid %s%s\n", __func__
, proc_name_address(p
),
7866 proc_pid(p
), (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
7867 SOCK_DOM(so
), SOCK_TYPE(so
),
7868 so
->e_pid
, proc_name_address(ep
), buf
,
7869 ((so
->so_flags
& SOF_DELEGATED
) ? " [delegated]" : ""));
7870 } else if (error
!= 0 && net_io_policy_log
) {
7871 log(LOG_ERR
, "%s[%s,%d]: so 0x%llx [%d,%d] epid %d (%s) "
7872 "ERROR (%d)\n", __func__
, proc_name_address(p
),
7873 proc_pid(p
), (uint64_t)DEBUG_KERNEL_ADDRPERM(so
),
7874 SOCK_DOM(so
), SOCK_TYPE(so
),
7875 epid
, (ep
== PROC_NULL
) ? "PROC_NULL" :
7876 proc_name_address(ep
), error
);
7879 /* Update this socket's policy upon success */
7881 so
->so_policy_gencnt
*= -1;
7882 so_update_policy(so
);
7884 so_update_necp_policy(so
, NULL
, NULL
);
7888 if (ep
!= PROC_NULL
) {
7896 so_set_effective_uuid(struct socket
*so
, uuid_t euuid
, struct proc
*p
, boolean_t check_cred
)
7902 /* UUID must not be all-zeroes (reserved for kernel) */
7903 if (uuid_is_null(euuid
)) {
7909 * If this is an in-kernel socket, prevent its delegate
7910 * association from changing unless the socket option is
7911 * coming from within the kernel itself.
7913 if (so
->last_pid
== 0 && p
!= kernproc
) {
7918 /* Get the UUID of the issuing process */
7919 proc_getexecutableuuid(p
, uuid
, sizeof(uuid
));
7922 * If this is issued by a process that's recorded as the
7923 * real owner of the socket, or if the uuid is the same as
7924 * the process's own uuid, then proceed. Otherwise ensure
7925 * that the issuing process has the necessary privileges.
7928 (uuid_compare(euuid
, so
->last_uuid
) != 0 ||
7929 uuid_compare(euuid
, uuid
) != 0)) {
7930 if ((error
= priv_check_cred(kauth_cred_get(),
7931 PRIV_NET_PRIVILEGED_SOCKET_DELEGATE
, 0))) {
7938 * If a process tries to delegate the socket to itself, then
7939 * there's really nothing to do; treat it as a way for the
7940 * delegate association to be cleared. Note that we check
7941 * the uuid of the passed-in proc rather than that of the
7942 * current process, as we need to check the process issuing
7943 * the socket option which could be kernproc itself. Given
7944 * that we don't allow 0 for effective uuid, it means that
7945 * a delegated in-kernel socket stays delegated during its
7946 * lifetime (which is okay.)
7948 if (uuid_compare(euuid
, uuid
) == 0) {
7949 so
->so_flags
&= ~SOF_DELEGATED
;
7952 uuid_clear(so
->e_uuid
);
7954 so
->so_flags
|= SOF_DELEGATED
;
7956 * Unlike so_set_effective_pid(), we only have the UUID
7957 * here and the process ID is not known. Inherit the
7958 * real {pid,upid} of the socket.
7960 so
->e_upid
= so
->last_upid
;
7961 so
->e_pid
= so
->last_pid
;
7962 uuid_copy(so
->e_uuid
, euuid
);
7965 * The following will clear the effective process name as it's the same
7966 * as the real process
7968 if (so
->so_proto
!= NULL
&& so
->so_proto
->pr_update_last_owner
!= NULL
) {
7969 (*so
->so_proto
->pr_update_last_owner
)(so
, NULL
, NULL
);
7972 if (error
== 0 && net_io_policy_log
) {
7973 uuid_unparse(so
->e_uuid
, buf
);
7974 log(LOG_DEBUG
, "%s[%s,%d]: so 0x%llx [%d,%d] epid %d "
7975 "euuid %s%s\n", __func__
, proc_name_address(p
), proc_pid(p
),
7976 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
), SOCK_DOM(so
),
7977 SOCK_TYPE(so
), so
->e_pid
, buf
,
7978 ((so
->so_flags
& SOF_DELEGATED
) ? " [delegated]" : ""));
7979 } else if (error
!= 0 && net_io_policy_log
) {
7980 uuid_unparse(euuid
, buf
);
7981 log(LOG_DEBUG
, "%s[%s,%d]: so 0x%llx [%d,%d] euuid %s "
7982 "ERROR (%d)\n", __func__
, proc_name_address(p
), proc_pid(p
),
7983 (uint64_t)DEBUG_KERNEL_ADDRPERM(so
), SOCK_DOM(so
),
7984 SOCK_TYPE(so
), buf
, error
);
7987 /* Update this socket's policy upon success */
7989 so
->so_policy_gencnt
*= -1;
7990 so_update_policy(so
);
7992 so_update_necp_policy(so
, NULL
, NULL
);
8000 netpolicy_post_msg(uint32_t ev_code
, struct netpolicy_event_data
*ev_data
,
8001 uint32_t ev_datalen
)
8003 struct kev_msg ev_msg
;
8006 * A netpolicy event always starts with a netpolicy_event_data
8007 * structure, but the caller can provide for a longer event
8008 * structure to post, depending on the event code.
8010 VERIFY(ev_data
!= NULL
&& ev_datalen
>= sizeof(*ev_data
));
8012 bzero(&ev_msg
, sizeof(ev_msg
));
8013 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
8014 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
8015 ev_msg
.kev_subclass
= KEV_NETPOLICY_SUBCLASS
;
8016 ev_msg
.event_code
= ev_code
;
8018 ev_msg
.dv
[0].data_ptr
= ev_data
;
8019 ev_msg
.dv
[0].data_length
= ev_datalen
;
8021 kev_post_msg(&ev_msg
);
8025 socket_post_kev_msg(uint32_t ev_code
,
8026 struct kev_socket_event_data
*ev_data
,
8027 uint32_t ev_datalen
)
8029 struct kev_msg ev_msg
;
8031 bzero(&ev_msg
, sizeof(ev_msg
));
8032 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
8033 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
8034 ev_msg
.kev_subclass
= KEV_SOCKET_SUBCLASS
;
8035 ev_msg
.event_code
= ev_code
;
8037 ev_msg
.dv
[0].data_ptr
= ev_data
;
8038 ev_msg
.dv
[0].data_length
= ev_datalen
;
8040 kev_post_msg(&ev_msg
);
8044 socket_post_kev_msg_closed(struct socket
*so
)
8046 struct kev_socket_closed ev
;
8047 struct sockaddr
*socksa
= NULL
, *peersa
= NULL
;
8049 bzero(&ev
, sizeof(ev
));
8050 err
= (*so
->so_proto
->pr_usrreqs
->pru_sockaddr
)(so
, &socksa
);
8052 err
= (*so
->so_proto
->pr_usrreqs
->pru_peeraddr
)(so
,
8055 memcpy(&ev
.ev_data
.kev_sockname
, socksa
,
8057 sizeof(ev
.ev_data
.kev_sockname
)));
8058 memcpy(&ev
.ev_data
.kev_peername
, peersa
,
8060 sizeof(ev
.ev_data
.kev_peername
)));
8061 socket_post_kev_msg(KEV_SOCKET_CLOSED
,
8062 &ev
.ev_data
, sizeof(ev
));
8065 if (socksa
!= NULL
) {
8066 FREE(socksa
, M_SONAME
);
8068 if (peersa
!= NULL
) {
8069 FREE(peersa
, M_SONAME
);