2 * Copyright (c) 1998-2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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_socket2.c 8.1 (Berkeley) 6/10/93
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/domain.h>
73 #include <sys/kernel.h>
74 #include <sys/proc_internal.h>
75 #include <sys/kauth.h>
76 #include <sys/malloc.h>
78 #include <sys/mcache.h>
79 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/signalvar.h>
84 #include <sys/sysctl.h>
85 #include <sys/syslog.h>
87 #include <kern/locks.h>
88 #include <net/route.h>
89 #include <net/content_filter.h>
90 #include <netinet/in.h>
91 #include <netinet/in_pcb.h>
92 #include <sys/kdebug.h>
93 #include <libkern/OSAtomic.h>
96 #include <security/mac_framework.h>
99 #include <mach/vm_param.h>
102 #include <netinet/mptcp_var.h>
105 #define DBG_FNC_SBDROP NETDBG_CODE(DBG_NETSOCK, 4)
106 #define DBG_FNC_SBAPPEND NETDBG_CODE(DBG_NETSOCK, 5)
108 SYSCTL_DECL(_kern_ipc
);
110 __private_extern__ u_int32_t net_io_policy_throttle_best_effort
= 0;
111 SYSCTL_INT(_kern_ipc
, OID_AUTO
, throttle_best_effort
,
112 CTLFLAG_RW
| CTLFLAG_LOCKED
, &net_io_policy_throttle_best_effort
, 0, "");
114 static inline void sbcompress(struct sockbuf
*, struct mbuf
*, struct mbuf
*);
115 static struct socket
*sonewconn_internal(struct socket
*, int);
116 static int sbappendaddr_internal(struct sockbuf
*, struct sockaddr
*,
117 struct mbuf
*, struct mbuf
*);
118 static int sbappendcontrol_internal(struct sockbuf
*, struct mbuf
*,
120 static void soevent_ifdenied(struct socket
*);
123 * Primitive routines for operating on sockets and socket buffers
125 static int soqlimitcompat
= 1;
126 static int soqlencomp
= 0;
129 * Based on the number of mbuf clusters configured, high_sb_max and sb_max can
130 * get scaled up or down to suit that memory configuration. high_sb_max is a
131 * higher limit on sb_max that is checked when sb_max gets set through sysctl.
134 u_int32_t sb_max
= SB_MAX
; /* XXX should be static */
135 u_int32_t high_sb_max
= SB_MAX
;
137 static u_int32_t sb_efficiency
= 8; /* parameter for sbreserve() */
138 int32_t total_sbmb_cnt
__attribute__((aligned(8))) = 0;
139 int32_t total_sbmb_cnt_peak
__attribute__((aligned(8))) = 0;
140 int32_t total_snd_byte_count
__attribute__((aligned(8))) = 0;
141 int64_t sbmb_limreached
__attribute__((aligned(8))) = 0;
143 /* Control whether to throttle sockets eligible to be throttled */
144 __private_extern__ u_int32_t net_io_policy_throttled
= 0;
145 static int sysctl_io_policy_throttled SYSCTL_HANDLER_ARGS
;
147 u_int32_t net_io_policy_log
= 0; /* log socket policy changes */
148 #if CONFIG_PROC_UUID_POLICY
149 u_int32_t net_io_policy_uuid
= 1; /* enable UUID socket policy */
150 #endif /* CONFIG_PROC_UUID_POLICY */
153 * Procedures to manipulate state flags of socket
154 * and do appropriate wakeups. Normal sequence from the
155 * active (originating) side is that soisconnecting() is
156 * called during processing of connect() call,
157 * resulting in an eventual call to soisconnected() if/when the
158 * connection is established. When the connection is torn down
159 * soisdisconnecting() is called during processing of disconnect() call,
160 * and soisdisconnected() is called when the connection to the peer
161 * is totally severed. The semantics of these routines are such that
162 * connectionless protocols can call soisconnected() and soisdisconnected()
163 * only, bypassing the in-progress calls when setting up a ``connection''
166 * From the passive side, a socket is created with
167 * two queues of sockets: so_incomp for connections in progress
168 * and so_comp for connections already made and awaiting user acceptance.
169 * As a protocol is preparing incoming connections, it creates a socket
170 * structure queued on so_incomp by calling sonewconn(). When the connection
171 * is established, soisconnected() is called, and transfers the
172 * socket structure to so_comp, making it available to accept().
174 * If a socket is closed with sockets on either
175 * so_incomp or so_comp, these sockets are dropped.
177 * If higher level protocols are implemented in
178 * the kernel, the wakeups done here will sometimes
179 * cause software-interrupt process scheduling.
182 soisconnecting(struct socket
*so
)
185 so
->so_state
&= ~(SS_ISCONNECTED
|SS_ISDISCONNECTING
);
186 so
->so_state
|= SS_ISCONNECTING
;
188 sflt_notify(so
, sock_evt_connecting
, NULL
);
192 soisconnected(struct socket
*so
)
194 struct socket
*head
= so
->so_head
;
196 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISDISCONNECTING
|SS_ISCONFIRMING
);
197 so
->so_state
|= SS_ISCONNECTED
;
199 soreserve_preconnect(so
, 0);
201 sflt_notify(so
, sock_evt_connected
, NULL
);
203 if (head
&& (so
->so_state
& SS_INCOMP
)) {
204 so
->so_state
&= ~SS_INCOMP
;
205 so
->so_state
|= SS_COMP
;
206 if (head
->so_proto
->pr_getlock
!= NULL
) {
207 socket_unlock(so
, 0);
208 socket_lock(head
, 1);
210 postevent(head
, 0, EV_RCONN
);
211 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
213 TAILQ_INSERT_TAIL(&head
->so_comp
, so
, so_list
);
215 wakeup_one((caddr_t
)&head
->so_timeo
);
216 if (head
->so_proto
->pr_getlock
!= NULL
) {
217 socket_unlock(head
, 1);
221 postevent(so
, 0, EV_WCONN
);
222 wakeup((caddr_t
)&so
->so_timeo
);
225 soevent(so
, SO_FILT_HINT_LOCKED
| SO_FILT_HINT_CONNECTED
|
226 SO_FILT_HINT_CONNINFO_UPDATED
);
231 socanwrite(struct socket
*so
)
233 return ((so
->so_state
& SS_ISCONNECTED
) ||
234 !(so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) ||
235 (so
->so_flags1
& SOF1_PRECONNECT_DATA
));
240 soisdisconnecting(struct socket
*so
)
242 so
->so_state
&= ~SS_ISCONNECTING
;
243 so
->so_state
|= (SS_ISDISCONNECTING
|SS_CANTRCVMORE
|SS_CANTSENDMORE
);
244 soevent(so
, SO_FILT_HINT_LOCKED
);
245 sflt_notify(so
, sock_evt_disconnecting
, NULL
);
246 wakeup((caddr_t
)&so
->so_timeo
);
252 soisdisconnected(struct socket
*so
)
254 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
255 so
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
);
256 soevent(so
, SO_FILT_HINT_LOCKED
| SO_FILT_HINT_DISCONNECTED
|
257 SO_FILT_HINT_CONNINFO_UPDATED
);
258 sflt_notify(so
, sock_evt_disconnected
, NULL
);
259 wakeup((caddr_t
)&so
->so_timeo
);
264 /* Notify content filters as soon as we cannot send/receive data */
265 cfil_sock_notify_shutdown(so
, SHUT_RDWR
);
266 #endif /* CONTENT_FILTER */
270 * This function will issue a wakeup like soisdisconnected but it will not
271 * notify the socket filters. This will avoid unlocking the socket
272 * in the midst of closing it.
275 sodisconnectwakeup(struct socket
*so
)
277 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
278 so
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
);
279 soevent(so
, SO_FILT_HINT_LOCKED
| SO_FILT_HINT_DISCONNECTED
|
280 SO_FILT_HINT_CONNINFO_UPDATED
);
281 wakeup((caddr_t
)&so
->so_timeo
);
286 /* Notify content filters as soon as we cannot send/receive data */
287 cfil_sock_notify_shutdown(so
, SHUT_RDWR
);
288 #endif /* CONTENT_FILTER */
292 * When an attempt at a new connection is noted on a socket
293 * which accepts connections, sonewconn is called. If the
294 * connection is possible (subject to space constraints, etc.)
295 * then we allocate a new structure, propoerly linked into the
296 * data structure of the original socket, and return this.
297 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
299 static struct socket
*
300 sonewconn_internal(struct socket
*head
, int connstatus
)
302 int so_qlen
, error
= 0;
304 lck_mtx_t
*mutex_held
;
306 if (head
->so_proto
->pr_getlock
!= NULL
)
307 mutex_held
= (*head
->so_proto
->pr_getlock
)(head
, 0);
309 mutex_held
= head
->so_proto
->pr_domain
->dom_mtx
;
310 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
314 * This is the default case; so_qlen represents the
315 * sum of both incomplete and completed queues.
317 so_qlen
= head
->so_qlen
;
320 * When kern.ipc.soqlencomp is set to 1, so_qlen
321 * represents only the completed queue. Since we
322 * cannot let the incomplete queue goes unbounded
323 * (in case of SYN flood), we cap the incomplete
324 * queue length to at most somaxconn, and use that
325 * as so_qlen so that we fail immediately below.
327 so_qlen
= head
->so_qlen
- head
->so_incqlen
;
328 if (head
->so_incqlen
> somaxconn
)
333 (soqlimitcompat
? head
->so_qlimit
: (3 * head
->so_qlimit
/ 2)))
334 return ((struct socket
*)0);
335 so
= soalloc(1, SOCK_DOM(head
), head
->so_type
);
337 return ((struct socket
*)0);
338 /* check if head was closed during the soalloc */
339 if (head
->so_proto
== NULL
) {
341 return ((struct socket
*)0);
344 so
->so_type
= head
->so_type
;
345 so
->so_options
= head
->so_options
&~ SO_ACCEPTCONN
;
346 so
->so_linger
= head
->so_linger
;
347 so
->so_state
= head
->so_state
| SS_NOFDREF
;
348 so
->so_proto
= head
->so_proto
;
349 so
->so_timeo
= head
->so_timeo
;
350 so
->so_pgid
= head
->so_pgid
;
351 kauth_cred_ref(head
->so_cred
);
352 so
->so_cred
= head
->so_cred
;
353 so
->last_pid
= head
->last_pid
;
354 so
->last_upid
= head
->last_upid
;
355 memcpy(so
->last_uuid
, head
->last_uuid
, sizeof (so
->last_uuid
));
356 if (head
->so_flags
& SOF_DELEGATED
) {
357 so
->e_pid
= head
->e_pid
;
358 so
->e_upid
= head
->e_upid
;
359 memcpy(so
->e_uuid
, head
->e_uuid
, sizeof (so
->e_uuid
));
361 /* inherit socket options stored in so_flags */
362 so
->so_flags
= head
->so_flags
&
363 (SOF_NOSIGPIPE
| SOF_NOADDRAVAIL
| SOF_REUSESHAREUID
|
364 SOF_NOTIFYCONFLICT
| SOF_BINDRANDOMPORT
| SOF_NPX_SETOPTSHUT
|
365 SOF_NODEFUNCT
| SOF_PRIVILEGED_TRAFFIC_CLASS
| SOF_NOTSENT_LOWAT
|
366 SOF_USELRO
| SOF_DELEGATED
);
368 so
->next_lock_lr
= 0;
369 so
->next_unlock_lr
= 0;
371 so
->so_rcv
.sb_flags
|= SB_RECV
; /* XXX */
372 so
->so_rcv
.sb_so
= so
->so_snd
.sb_so
= so
;
373 TAILQ_INIT(&so
->so_evlist
);
375 #if CONFIG_MACF_SOCKET
376 mac_socket_label_associate_accept(head
, so
);
379 /* inherit traffic management properties of listener */
380 so
->so_traffic_mgt_flags
=
381 head
->so_traffic_mgt_flags
& (TRAFFIC_MGT_SO_BACKGROUND
);
382 so
->so_background_thread
= head
->so_background_thread
;
383 so
->so_traffic_class
= head
->so_traffic_class
;
385 if (soreserve(so
, head
->so_snd
.sb_hiwat
, head
->so_rcv
.sb_hiwat
)) {
387 return ((struct socket
*)0);
389 so
->so_rcv
.sb_flags
|= (head
->so_rcv
.sb_flags
& SB_USRSIZE
);
390 so
->so_snd
.sb_flags
|= (head
->so_snd
.sb_flags
& SB_USRSIZE
);
393 * Must be done with head unlocked to avoid deadlock
394 * for protocol with per socket mutexes.
396 if (head
->so_proto
->pr_unlock
)
397 socket_unlock(head
, 0);
398 if (((*so
->so_proto
->pr_usrreqs
->pru_attach
)(so
, 0, NULL
) != 0) ||
401 if (head
->so_proto
->pr_unlock
)
402 socket_lock(head
, 0);
403 return ((struct socket
*)0);
405 if (head
->so_proto
->pr_unlock
) {
406 socket_lock(head
, 0);
408 * Radar 7385998 Recheck that the head is still accepting
409 * to avoid race condition when head is getting closed.
411 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
412 so
->so_state
&= ~SS_NOFDREF
;
414 return ((struct socket
*)0);
418 atomic_add_32(&so
->so_proto
->pr_domain
->dom_refs
, 1);
420 /* Insert in head appropriate lists */
424 * Since this socket is going to be inserted into the incomp
425 * queue, it can be picked up by another thread in
426 * tcp_dropdropablreq to get dropped before it is setup..
427 * To prevent this race, set in-progress flag which can be
430 so
->so_flags
|= SOF_INCOMP_INPROGRESS
;
433 TAILQ_INSERT_TAIL(&head
->so_comp
, so
, so_list
);
434 so
->so_state
|= SS_COMP
;
436 TAILQ_INSERT_TAIL(&head
->so_incomp
, so
, so_list
);
437 so
->so_state
|= SS_INCOMP
;
442 /* Attach socket filters for this protocol */
446 so
->so_state
|= connstatus
;
448 wakeup((caddr_t
)&head
->so_timeo
);
455 sonewconn(struct socket
*head
, int connstatus
, const struct sockaddr
*from
)
457 int error
= sflt_connectin(head
, from
);
462 return (sonewconn_internal(head
, connstatus
));
466 * Socantsendmore indicates that no more data will be sent on the
467 * socket; it would normally be applied to a socket when the user
468 * informs the system that no more data is to be sent, by the protocol
469 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
470 * will be received, and will normally be applied to the socket by a
471 * protocol when it detects that the peer will send no more data.
472 * Data queued for reading in the socket may yet be read.
476 socantsendmore(struct socket
*so
)
478 so
->so_state
|= SS_CANTSENDMORE
;
479 soevent(so
, SO_FILT_HINT_LOCKED
| SO_FILT_HINT_CANTSENDMORE
);
480 sflt_notify(so
, sock_evt_cantsendmore
, NULL
);
485 socantrcvmore(struct socket
*so
)
487 so
->so_state
|= SS_CANTRCVMORE
;
488 soevent(so
, SO_FILT_HINT_LOCKED
| SO_FILT_HINT_CANTRCVMORE
);
489 sflt_notify(so
, sock_evt_cantrecvmore
, NULL
);
494 * Wait for data to arrive at/drain from a socket buffer.
497 sbwait(struct sockbuf
*sb
)
499 boolean_t nointr
= (sb
->sb_flags
& SB_NOINTR
);
500 void *lr_saved
= __builtin_return_address(0);
501 struct socket
*so
= sb
->sb_so
;
502 lck_mtx_t
*mutex_held
;
507 panic("%s: null so, sb=%p sb_flags=0x%x lr=%p\n",
508 __func__
, sb
, sb
->sb_flags
, lr_saved
);
510 } else if (so
->so_usecount
< 1) {
511 panic("%s: sb=%p sb_flags=0x%x sb_so=%p usecount=%d lr=%p "
512 "lrh= %s\n", __func__
, sb
, sb
->sb_flags
, so
,
513 so
->so_usecount
, lr_saved
, solockhistory_nr(so
));
517 if (so
->so_proto
->pr_getlock
!= NULL
)
518 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
520 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
522 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
524 ts
.tv_sec
= sb
->sb_timeo
.tv_sec
;
525 ts
.tv_nsec
= sb
->sb_timeo
.tv_usec
* 1000;
528 VERIFY(sb
->sb_waiters
!= 0);
530 error
= msleep((caddr_t
)&sb
->sb_cc
, mutex_held
,
531 nointr
? PSOCK
: PSOCK
| PCATCH
,
532 nointr
? "sbwait_nointr" : "sbwait", &ts
);
534 VERIFY(sb
->sb_waiters
!= 0);
537 if (so
->so_usecount
< 1) {
538 panic("%s: 2 sb=%p sb_flags=0x%x sb_so=%p usecount=%d lr=%p "
539 "lrh= %s\n", __func__
, sb
, sb
->sb_flags
, so
,
540 so
->so_usecount
, lr_saved
, solockhistory_nr(so
));
544 if ((so
->so_state
& SS_DRAINING
) || (so
->so_flags
& SOF_DEFUNCT
)) {
546 if (so
->so_flags
& SOF_DEFUNCT
) {
547 SODEFUNCTLOG(("%s[%d]: defunct so 0x%llx [%d,%d] "
548 "(%d)\n", __func__
, proc_selfpid(),
549 (uint64_t)VM_KERNEL_ADDRPERM(so
),
550 SOCK_DOM(so
), SOCK_TYPE(so
), error
));
558 sbwakeup(struct sockbuf
*sb
)
560 if (sb
->sb_waiters
> 0)
561 wakeup((caddr_t
)&sb
->sb_cc
);
565 * Wakeup processes waiting on a socket buffer.
566 * Do asynchronous notification via SIGIO
567 * if the socket has the SS_ASYNC flag set.
570 sowakeup(struct socket
*so
, struct sockbuf
*sb
)
572 if (so
->so_flags
& SOF_DEFUNCT
) {
573 SODEFUNCTLOG(("%s[%d]: defunct so 0x%llx [%d,%d] si 0x%x, "
574 "fl 0x%x [%s]\n", __func__
, proc_selfpid(),
575 (uint64_t)VM_KERNEL_ADDRPERM(so
), SOCK_DOM(so
),
576 SOCK_TYPE(so
), (uint32_t)sb
->sb_sel
.si_flags
, sb
->sb_flags
,
577 (sb
->sb_flags
& SB_RECV
) ? "rcv" : "snd"));
580 sb
->sb_flags
&= ~SB_SEL
;
581 selwakeup(&sb
->sb_sel
);
583 if (so
->so_state
& SS_ASYNC
) {
585 gsignal(-so
->so_pgid
, SIGIO
);
586 else if (so
->so_pgid
> 0)
587 proc_signal(so
->so_pgid
, SIGIO
);
589 if (sb
->sb_flags
& SB_KNOTE
) {
590 KNOTE(&sb
->sb_sel
.si_note
, SO_FILT_HINT_LOCKED
);
592 if (sb
->sb_flags
& SB_UPCALL
) {
593 void (*sb_upcall
)(struct socket
*, void *, int);
594 caddr_t sb_upcallarg
;
596 sb_upcall
= sb
->sb_upcall
;
597 sb_upcallarg
= sb
->sb_upcallarg
;
598 /* Let close know that we're about to do an upcall */
599 so
->so_upcallusecount
++;
601 socket_unlock(so
, 0);
602 (*sb_upcall
)(so
, sb_upcallarg
, M_DONTWAIT
);
605 so
->so_upcallusecount
--;
606 /* Tell close that it's safe to proceed */
607 if ((so
->so_flags
& SOF_CLOSEWAIT
) &&
608 so
->so_upcallusecount
== 0)
609 wakeup((caddr_t
)&so
->so_upcallusecount
);
613 * Trap disconnection events for content filters
615 if ((so
->so_flags
& SOF_CONTENT_FILTER
) != 0) {
616 if ((sb
->sb_flags
& SB_RECV
)) {
617 if (so
->so_state
& (SS_CANTRCVMORE
))
618 cfil_sock_notify_shutdown(so
, SHUT_RD
);
620 if (so
->so_state
& (SS_CANTSENDMORE
))
621 cfil_sock_notify_shutdown(so
, SHUT_WR
);
624 #endif /* CONTENT_FILTER */
628 * Socket buffer (struct sockbuf) utility routines.
630 * Each socket contains two socket buffers: one for sending data and
631 * one for receiving data. Each buffer contains a queue of mbufs,
632 * information about the number of mbufs and amount of data in the
633 * queue, and other fields allowing select() statements and notification
634 * on data availability to be implemented.
636 * Data stored in a socket buffer is maintained as a list of records.
637 * Each record is a list of mbufs chained together with the m_next
638 * field. Records are chained together with the m_nextpkt field. The upper
639 * level routine soreceive() expects the following conventions to be
640 * observed when placing information in the receive buffer:
642 * 1. If the protocol requires each message be preceded by the sender's
643 * name, then a record containing that name must be present before
644 * any associated data (mbuf's must be of type MT_SONAME).
645 * 2. If the protocol supports the exchange of ``access rights'' (really
646 * just additional data associated with the message), and there are
647 * ``rights'' to be received, then a record containing this data
648 * should be present (mbuf's must be of type MT_RIGHTS).
649 * 3. If a name or rights record exists, then it must be followed by
650 * a data record, perhaps of zero length.
652 * Before using a new socket structure it is first necessary to reserve
653 * buffer space to the socket, by calling sbreserve(). This should commit
654 * some of the available buffer space in the system buffer pool for the
655 * socket (currently, it does nothing but enforce limits). The space
656 * should be released by calling sbrelease() when the socket is destroyed.
664 soreserve(struct socket
*so
, u_int32_t sndcc
, u_int32_t rcvcc
)
667 if (sbreserve(&so
->so_snd
, sndcc
) == 0)
670 so
->so_snd
.sb_idealsize
= sndcc
;
672 if (sbreserve(&so
->so_rcv
, rcvcc
) == 0)
675 so
->so_rcv
.sb_idealsize
= rcvcc
;
677 if (so
->so_rcv
.sb_lowat
== 0)
678 so
->so_rcv
.sb_lowat
= 1;
679 if (so
->so_snd
.sb_lowat
== 0)
680 so
->so_snd
.sb_lowat
= MCLBYTES
;
681 if (so
->so_snd
.sb_lowat
> so
->so_snd
.sb_hiwat
)
682 so
->so_snd
.sb_lowat
= so
->so_snd
.sb_hiwat
;
685 so
->so_snd
.sb_flags
&= ~SB_SEL
;
686 selthreadclear(&so
->so_snd
.sb_sel
);
687 sbrelease(&so
->so_snd
);
693 soreserve_preconnect(struct socket
*so
, unsigned int pre_cc
)
695 /* As of now, same bytes for both preconnect read and write */
696 so
->so_snd
.sb_preconn_hiwat
= pre_cc
;
697 so
->so_rcv
.sb_preconn_hiwat
= pre_cc
;
701 * Allot mbufs to a sockbuf.
702 * Attempt to scale mbmax so that mbcnt doesn't become limiting
703 * if buffering efficiency is near the normal case.
706 sbreserve(struct sockbuf
*sb
, u_int32_t cc
)
708 if ((u_quad_t
)cc
> (u_quad_t
)sb_max
* MCLBYTES
/ (MSIZE
+ MCLBYTES
))
711 sb
->sb_mbmax
= min(cc
* sb_efficiency
, sb_max
);
712 if (sb
->sb_lowat
> sb
->sb_hiwat
)
713 sb
->sb_lowat
= sb
->sb_hiwat
;
718 * Free mbufs held by a socket, and reserved mbuf space.
720 /* WARNING needs to do selthreadclear() before calling this */
722 sbrelease(struct sockbuf
*sb
)
730 * Routines to add and remove
731 * data from an mbuf queue.
733 * The routines sbappend() or sbappendrecord() are normally called to
734 * append new mbufs to a socket buffer, after checking that adequate
735 * space is available, comparing the function sbspace() with the amount
736 * of data to be added. sbappendrecord() differs from sbappend() in
737 * that data supplied is treated as the beginning of a new record.
738 * To place a sender's address, optional access rights, and data in a
739 * socket receive buffer, sbappendaddr() should be used. To place
740 * access rights and data in a socket receive buffer, sbappendrights()
741 * should be used. In either case, the new data begins a new record.
742 * Note that unlike sbappend() and sbappendrecord(), these routines check
743 * for the caller that there will be enough space to store the data.
744 * Each fails if there is not enough space, or if it cannot find mbufs
745 * to store additional information in.
747 * Reliable protocols may use the socket send buffer to hold data
748 * awaiting acknowledgement. Data is normally copied from a socket
749 * send buffer in a protocol with m_copy for output to a peer,
750 * and then removing the data from the socket buffer with sbdrop()
751 * or sbdroprecord() when the data is acknowledged by the peer.
755 * Append mbuf chain m to the last record in the
756 * socket buffer sb. The additional space associated
757 * the mbuf chain is recorded in sb. Empty mbufs are
758 * discarded and mbufs are compacted where possible.
761 sbappend(struct sockbuf
*sb
, struct mbuf
*m
)
763 struct socket
*so
= sb
->sb_so
;
765 if (m
== NULL
|| (sb
->sb_flags
& SB_DROP
)) {
771 SBLASTRECORDCHK(sb
, "sbappend 1");
773 if (sb
->sb_lastrecord
!= NULL
&& (sb
->sb_mbtail
->m_flags
& M_EOR
))
774 return (sbappendrecord(sb
, m
));
776 if (sb
->sb_flags
& SB_RECV
&& !(m
&& m
->m_flags
& M_SKIPCFIL
)) {
777 int error
= sflt_data_in(so
, NULL
, &m
, NULL
, 0);
778 SBLASTRECORDCHK(sb
, "sbappend 2");
782 error
= cfil_sock_data_in(so
, NULL
, m
, NULL
, 0);
783 #endif /* CONTENT_FILTER */
786 if (error
!= EJUSTRETURN
)
791 m
->m_flags
&= ~M_SKIPCFIL
;
794 /* If this is the first record, it's also the last record */
795 if (sb
->sb_lastrecord
== NULL
)
796 sb
->sb_lastrecord
= m
;
798 sbcompress(sb
, m
, sb
->sb_mbtail
);
799 SBLASTRECORDCHK(sb
, "sbappend 3");
804 * Similar to sbappend, except that this is optimized for stream sockets.
807 sbappendstream(struct sockbuf
*sb
, struct mbuf
*m
)
809 struct socket
*so
= sb
->sb_so
;
811 if (m
== NULL
|| (sb
->sb_flags
& SB_DROP
)) {
817 if (m
->m_nextpkt
!= NULL
|| (sb
->sb_mb
!= sb
->sb_lastrecord
)) {
818 panic("sbappendstream: nexpkt %p || mb %p != lastrecord %p\n",
819 m
->m_nextpkt
, sb
->sb_mb
, sb
->sb_lastrecord
);
823 SBLASTMBUFCHK(sb
, __func__
);
825 if (sb
->sb_flags
& SB_RECV
&& !(m
&& m
->m_flags
& M_SKIPCFIL
)) {
826 int error
= sflt_data_in(so
, NULL
, &m
, NULL
, 0);
827 SBLASTRECORDCHK(sb
, "sbappendstream 1");
831 error
= cfil_sock_data_in(so
, NULL
, m
, NULL
, 0);
832 #endif /* CONTENT_FILTER */
835 if (error
!= EJUSTRETURN
)
840 m
->m_flags
&= ~M_SKIPCFIL
;
843 sbcompress(sb
, m
, sb
->sb_mbtail
);
844 sb
->sb_lastrecord
= sb
->sb_mb
;
845 SBLASTRECORDCHK(sb
, "sbappendstream 2");
851 sbcheck(struct sockbuf
*sb
)
855 u_int32_t len
= 0, mbcnt
= 0;
856 lck_mtx_t
*mutex_held
;
858 if (sb
->sb_so
->so_proto
->pr_getlock
!= NULL
)
859 mutex_held
= (*sb
->sb_so
->so_proto
->pr_getlock
)(sb
->sb_so
, 0);
861 mutex_held
= sb
->sb_so
->so_proto
->pr_domain
->dom_mtx
;
863 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
868 for (m
= sb
->sb_mb
; m
; m
= n
) {
870 for (; m
; m
= m
->m_next
) {
873 /* XXX pretty sure this is bogus */
874 if (m
->m_flags
& M_EXT
)
875 mbcnt
+= m
->m_ext
.ext_size
;
878 if (len
!= sb
->sb_cc
|| mbcnt
!= sb
->sb_mbcnt
) {
879 panic("cc %ld != %ld || mbcnt %ld != %ld\n", len
, sb
->sb_cc
,
880 mbcnt
, sb
->sb_mbcnt
);
886 sblastrecordchk(struct sockbuf
*sb
, const char *where
)
888 struct mbuf
*m
= sb
->sb_mb
;
890 while (m
&& m
->m_nextpkt
)
893 if (m
!= sb
->sb_lastrecord
) {
894 printf("sblastrecordchk: mb 0x%llx lastrecord 0x%llx "
896 (uint64_t)VM_KERNEL_ADDRPERM(sb
->sb_mb
),
897 (uint64_t)VM_KERNEL_ADDRPERM(sb
->sb_lastrecord
),
898 (uint64_t)VM_KERNEL_ADDRPERM(m
));
899 printf("packet chain:\n");
900 for (m
= sb
->sb_mb
; m
!= NULL
; m
= m
->m_nextpkt
)
901 printf("\t0x%llx\n", (uint64_t)VM_KERNEL_ADDRPERM(m
));
902 panic("sblastrecordchk from %s", where
);
907 sblastmbufchk(struct sockbuf
*sb
, const char *where
)
909 struct mbuf
*m
= sb
->sb_mb
;
912 while (m
&& m
->m_nextpkt
)
915 while (m
&& m
->m_next
)
918 if (m
!= sb
->sb_mbtail
) {
919 printf("sblastmbufchk: mb 0x%llx mbtail 0x%llx last 0x%llx\n",
920 (uint64_t)VM_KERNEL_ADDRPERM(sb
->sb_mb
),
921 (uint64_t)VM_KERNEL_ADDRPERM(sb
->sb_mbtail
),
922 (uint64_t)VM_KERNEL_ADDRPERM(m
));
923 printf("packet tree:\n");
924 for (m
= sb
->sb_mb
; m
!= NULL
; m
= m
->m_nextpkt
) {
926 for (n
= m
; n
!= NULL
; n
= n
->m_next
)
928 (uint64_t)VM_KERNEL_ADDRPERM(n
));
931 panic("sblastmbufchk from %s", where
);
936 * Similar to sbappend, except the mbuf chain begins a new record.
939 sbappendrecord(struct sockbuf
*sb
, struct mbuf
*m0
)
944 if (m0
== NULL
|| (sb
->sb_flags
& SB_DROP
)) {
950 for (m
= m0
; m
!= NULL
; m
= m
->m_next
)
953 if (space
> sbspace(sb
) && !(sb
->sb_flags
& SB_UNIX
)) {
958 if (sb
->sb_flags
& SB_RECV
&& !(m0
&& m0
->m_flags
& M_SKIPCFIL
)) {
959 int error
= sflt_data_in(sb
->sb_so
, NULL
, &m0
, NULL
,
960 sock_data_filt_flag_record
);
964 error
= cfil_sock_data_in(sb
->sb_so
, NULL
, m0
, NULL
, 0);
965 #endif /* CONTENT_FILTER */
968 SBLASTRECORDCHK(sb
, "sbappendrecord 1");
969 if (error
!= EJUSTRETURN
)
974 m0
->m_flags
&= ~M_SKIPCFIL
;
978 * Note this permits zero length records.
981 SBLASTRECORDCHK(sb
, "sbappendrecord 2");
982 if (sb
->sb_lastrecord
!= NULL
) {
983 sb
->sb_lastrecord
->m_nextpkt
= m0
;
987 sb
->sb_lastrecord
= m0
;
992 if (m
&& (m0
->m_flags
& M_EOR
)) {
993 m0
->m_flags
&= ~M_EOR
;
996 sbcompress(sb
, m
, m0
);
997 SBLASTRECORDCHK(sb
, "sbappendrecord 3");
1002 * As above except that OOB data
1003 * is inserted at the beginning of the sockbuf,
1004 * but after any other OOB data.
1007 sbinsertoob(struct sockbuf
*sb
, struct mbuf
*m0
)
1015 SBLASTRECORDCHK(sb
, "sbinsertoob 1");
1017 if ((sb
->sb_flags
& SB_RECV
&& !(m0
->m_flags
& M_SKIPCFIL
)) != 0) {
1018 int error
= sflt_data_in(sb
->sb_so
, NULL
, &m0
, NULL
,
1019 sock_data_filt_flag_oob
);
1021 SBLASTRECORDCHK(sb
, "sbinsertoob 2");
1025 error
= cfil_sock_data_in(sb
->sb_so
, NULL
, m0
, NULL
, 0);
1026 #endif /* CONTENT_FILTER */
1029 if (error
!= EJUSTRETURN
) {
1035 m0
->m_flags
&= ~M_SKIPCFIL
;
1038 for (mp
= &sb
->sb_mb
; *mp
; mp
= &((*mp
)->m_nextpkt
)) {
1041 switch (m
->m_type
) {
1044 continue; /* WANT next train */
1049 goto again
; /* inspect THIS train further */
1054 * Put the first mbuf on the queue.
1055 * Note this permits zero length records.
1058 m0
->m_nextpkt
= *mp
;
1060 /* m0 is actually the new tail */
1061 sb
->sb_lastrecord
= m0
;
1066 if (m
&& (m0
->m_flags
& M_EOR
)) {
1067 m0
->m_flags
&= ~M_EOR
;
1068 m
->m_flags
|= M_EOR
;
1070 sbcompress(sb
, m
, m0
);
1071 SBLASTRECORDCHK(sb
, "sbinsertoob 3");
1076 * Append address and data, and optionally, control (ancillary) data
1077 * to the receive queue of a socket. If present,
1078 * m0 must include a packet header with total length.
1079 * Returns 0 if no space in sockbuf or insufficient mbufs.
1081 * Returns: 0 No space/out of mbufs
1085 sbappendaddr_internal(struct sockbuf
*sb
, struct sockaddr
*asa
,
1086 struct mbuf
*m0
, struct mbuf
*control
)
1088 struct mbuf
*m
, *n
, *nlast
;
1089 int space
= asa
->sa_len
;
1091 if (m0
&& (m0
->m_flags
& M_PKTHDR
) == 0)
1092 panic("sbappendaddr");
1095 space
+= m0
->m_pkthdr
.len
;
1096 for (n
= control
; n
; n
= n
->m_next
) {
1098 if (n
->m_next
== 0) /* keep pointer to last control buf */
1101 if (space
> sbspace(sb
))
1103 if (asa
->sa_len
> MLEN
)
1105 MGET(m
, M_DONTWAIT
, MT_SONAME
);
1108 m
->m_len
= asa
->sa_len
;
1109 bcopy((caddr_t
)asa
, mtod(m
, caddr_t
), asa
->sa_len
);
1111 n
->m_next
= m0
; /* concatenate data to control */
1114 m
->m_next
= control
;
1116 SBLASTRECORDCHK(sb
, "sbappendadddr 1");
1118 for (n
= m
; n
->m_next
!= NULL
; n
= n
->m_next
)
1123 if (sb
->sb_lastrecord
!= NULL
) {
1124 sb
->sb_lastrecord
->m_nextpkt
= m
;
1128 sb
->sb_lastrecord
= m
;
1129 sb
->sb_mbtail
= nlast
;
1131 SBLASTMBUFCHK(sb
, __func__
);
1132 SBLASTRECORDCHK(sb
, "sbappendadddr 2");
1134 postevent(0, sb
, EV_RWBYTES
);
1139 * Returns: 0 Error: No space/out of mbufs/etc.
1142 * Imputed: (*error_out) errno for error
1144 * sflt_data_in:??? [whatever a filter author chooses]
1147 sbappendaddr(struct sockbuf
*sb
, struct sockaddr
*asa
, struct mbuf
*m0
,
1148 struct mbuf
*control
, int *error_out
)
1151 boolean_t sb_unix
= (sb
->sb_flags
& SB_UNIX
);
1156 if (m0
&& (m0
->m_flags
& M_PKTHDR
) == 0)
1157 panic("sbappendaddrorfree");
1159 if (sb
->sb_flags
& SB_DROP
) {
1162 if (control
!= NULL
&& !sb_unix
)
1164 if (error_out
!= NULL
)
1165 *error_out
= EINVAL
;
1169 /* Call socket data in filters */
1170 if (sb
->sb_flags
& SB_RECV
&& !(m0
&& m0
->m_flags
& M_SKIPCFIL
)) {
1172 error
= sflt_data_in(sb
->sb_so
, asa
, &m0
, &control
, 0);
1173 SBLASTRECORDCHK(sb
, __func__
);
1177 error
= cfil_sock_data_in(sb
->sb_so
, asa
, m0
, control
,
1179 #endif /* CONTENT_FILTER */
1182 if (error
!= EJUSTRETURN
) {
1185 if (control
!= NULL
&& !sb_unix
)
1193 m0
->m_flags
&= ~M_SKIPCFIL
;
1196 result
= sbappendaddr_internal(sb
, asa
, m0
, control
);
1200 if (control
!= NULL
&& !sb_unix
)
1203 *error_out
= ENOBUFS
;
1210 sbappendcontrol_internal(struct sockbuf
*sb
, struct mbuf
*m0
,
1211 struct mbuf
*control
)
1213 struct mbuf
*m
, *mlast
, *n
;
1217 panic("sbappendcontrol");
1219 for (m
= control
; ; m
= m
->m_next
) {
1224 n
= m
; /* save pointer to last control buffer */
1225 for (m
= m0
; m
; m
= m
->m_next
)
1227 if (space
> sbspace(sb
) && !(sb
->sb_flags
& SB_UNIX
))
1229 n
->m_next
= m0
; /* concatenate data to control */
1230 SBLASTRECORDCHK(sb
, "sbappendcontrol 1");
1232 for (m
= control
; m
->m_next
!= NULL
; m
= m
->m_next
)
1237 if (sb
->sb_lastrecord
!= NULL
) {
1238 sb
->sb_lastrecord
->m_nextpkt
= control
;
1240 sb
->sb_mb
= control
;
1242 sb
->sb_lastrecord
= control
;
1243 sb
->sb_mbtail
= mlast
;
1245 SBLASTMBUFCHK(sb
, __func__
);
1246 SBLASTRECORDCHK(sb
, "sbappendcontrol 2");
1248 postevent(0, sb
, EV_RWBYTES
);
1253 sbappendcontrol(struct sockbuf
*sb
, struct mbuf
*m0
, struct mbuf
*control
,
1257 boolean_t sb_unix
= (sb
->sb_flags
& SB_UNIX
);
1262 if (sb
->sb_flags
& SB_DROP
) {
1265 if (control
!= NULL
&& !sb_unix
)
1267 if (error_out
!= NULL
)
1268 *error_out
= EINVAL
;
1272 if (sb
->sb_flags
& SB_RECV
&& !(m0
&& m0
->m_flags
& M_SKIPCFIL
)) {
1275 error
= sflt_data_in(sb
->sb_so
, NULL
, &m0
, &control
, 0);
1276 SBLASTRECORDCHK(sb
, __func__
);
1280 error
= cfil_sock_data_in(sb
->sb_so
, NULL
, m0
, control
,
1282 #endif /* CONTENT_FILTER */
1285 if (error
!= EJUSTRETURN
) {
1288 if (control
!= NULL
&& !sb_unix
)
1296 m0
->m_flags
&= ~M_SKIPCFIL
;
1299 result
= sbappendcontrol_internal(sb
, m0
, control
);
1303 if (control
!= NULL
&& !sb_unix
)
1306 *error_out
= ENOBUFS
;
1313 * Append a contiguous TCP data blob with TCP sequence number as control data
1314 * as a new msg to the receive socket buffer.
1317 sbappendmsgstream_rcv(struct sockbuf
*sb
, struct mbuf
*m
, uint32_t seqnum
,
1320 struct mbuf
*m_eor
= NULL
;
1321 u_int32_t data_len
= 0;
1323 struct socket
*so
= sb
->sb_so
;
1325 VERIFY((m
->m_flags
& M_PKTHDR
) && m_pktlen(m
) > 0);
1326 VERIFY(so
->so_msg_state
!= NULL
);
1327 VERIFY(sb
->sb_flags
& SB_RECV
);
1329 /* Keep the TCP sequence number in the mbuf pkthdr */
1330 m
->m_pkthdr
.msg_seq
= seqnum
;
1332 /* find last mbuf and set M_EOR */
1333 for (m_eor
= m
; ; m_eor
= m_eor
->m_next
) {
1335 * If the msg is unordered, we need to account for
1336 * these bytes in receive socket buffer size. Otherwise,
1337 * the receive window advertised will shrink because
1338 * of the additional unordered bytes added to the
1342 m_eor
->m_flags
|= M_UNORDERED_DATA
;
1343 data_len
+= m_eor
->m_len
;
1344 so
->so_msg_state
->msg_uno_bytes
+= m_eor
->m_len
;
1346 m_eor
->m_flags
&= ~M_UNORDERED_DATA
;
1348 if (m_eor
->m_next
== NULL
)
1352 /* set EOR flag at end of byte blob */
1353 m_eor
->m_flags
|= M_EOR
;
1355 /* expand the receive socket buffer to allow unordered data */
1356 if (unordered
&& !sbreserve(sb
, sb
->sb_hiwat
+ data_len
)) {
1358 * Could not allocate memory for unordered data, it
1359 * means this packet will have to be delivered in order
1361 printf("%s: could not reserve space for unordered data\n",
1365 if (!unordered
&& (sb
->sb_mbtail
!= NULL
) &&
1366 !(sb
->sb_mbtail
->m_flags
& M_UNORDERED_DATA
)) {
1367 sb
->sb_mbtail
->m_flags
&= ~M_EOR
;
1368 sbcompress(sb
, m
, sb
->sb_mbtail
);
1371 ret
= sbappendrecord(sb
, m
);
1373 VERIFY(sb
->sb_mbtail
->m_flags
& M_EOR
);
1378 * TCP streams have message based out of order delivery support, or have
1379 * Multipath TCP support, or are regular TCP sockets
1382 sbappendstream_rcvdemux(struct socket
*so
, struct mbuf
*m
, uint32_t seqnum
,
1387 if ((m
!= NULL
) && (m_pktlen(m
) <= 0)) {
1392 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
1393 ret
= sbappendmsgstream_rcv(&so
->so_rcv
, m
, seqnum
, unordered
);
1396 else if (so
->so_flags
& SOF_MPTCP_TRUE
) {
1397 ret
= sbappendmptcpstream_rcv(&so
->so_rcv
, m
);
1401 ret
= sbappendstream(&so
->so_rcv
, m
);
1408 sbappendmptcpstream_rcv(struct sockbuf
*sb
, struct mbuf
*m
)
1410 struct socket
*so
= sb
->sb_so
;
1412 VERIFY(m
== NULL
|| (m
->m_flags
& M_PKTHDR
));
1413 /* SB_NOCOMPRESS must be set prevent loss of M_PKTHDR data */
1414 VERIFY((sb
->sb_flags
& (SB_RECV
|SB_NOCOMPRESS
)) ==
1415 (SB_RECV
|SB_NOCOMPRESS
));
1417 if (m
== NULL
|| m_pktlen(m
) == 0 || (sb
->sb_flags
& SB_DROP
) ||
1418 (so
->so_state
& SS_CANTRCVMORE
)) {
1423 /* the socket is not closed, so SOF_MP_SUBFLOW must be set */
1424 VERIFY(so
->so_flags
& SOF_MP_SUBFLOW
);
1426 if (m
->m_nextpkt
!= NULL
|| (sb
->sb_mb
!= sb
->sb_lastrecord
)) {
1427 panic("%s: nexpkt %p || mb %p != lastrecord %p\n", __func__
,
1428 m
->m_nextpkt
, sb
->sb_mb
, sb
->sb_lastrecord
);
1432 SBLASTMBUFCHK(sb
, __func__
);
1434 if (mptcp_adj_rmap(so
, m
) != 0)
1437 /* No filter support (SB_RECV) on mptcp subflow sockets */
1439 sbcompress(sb
, m
, sb
->sb_mbtail
);
1440 sb
->sb_lastrecord
= sb
->sb_mb
;
1441 SBLASTRECORDCHK(sb
, __func__
);
1447 * Append message to send socket buffer based on priority.
1450 sbappendmsg_snd(struct sockbuf
*sb
, struct mbuf
*m
)
1452 struct socket
*so
= sb
->sb_so
;
1453 struct msg_priq
*priq
;
1456 VERIFY(so
->so_msg_state
!= NULL
);
1458 if (m
->m_nextpkt
!= NULL
|| (sb
->sb_mb
!= sb
->sb_lastrecord
))
1459 panic("sbappendstream: nexpkt %p || mb %p != lastrecord %p\n",
1460 m
->m_nextpkt
, sb
->sb_mb
, sb
->sb_lastrecord
);
1462 SBLASTMBUFCHK(sb
, __func__
);
1464 if (m
== NULL
|| (sb
->sb_flags
& SB_DROP
) || so
->so_msg_state
== NULL
) {
1470 priq
= &so
->so_msg_state
->msg_priq
[m
->m_pkthdr
.msg_pri
];
1472 /* note if we need to propogate M_EOR to the last mbuf */
1473 if (m
->m_flags
& M_EOR
) {
1476 /* Reset M_EOR from the first mbuf */
1477 m
->m_flags
&= ~(M_EOR
);
1480 if (priq
->msgq_head
== NULL
) {
1481 VERIFY(priq
->msgq_tail
== NULL
&& priq
->msgq_lastmsg
== NULL
);
1482 priq
->msgq_head
= priq
->msgq_lastmsg
= m
;
1484 VERIFY(priq
->msgq_tail
->m_next
== NULL
);
1486 /* Check if the last message has M_EOR flag set */
1487 if (priq
->msgq_tail
->m_flags
& M_EOR
) {
1488 /* Insert as a new message */
1489 priq
->msgq_lastmsg
->m_nextpkt
= m
;
1491 /* move the lastmsg pointer */
1492 priq
->msgq_lastmsg
= m
;
1494 /* Append to the existing message */
1495 priq
->msgq_tail
->m_next
= m
;
1499 /* Update accounting and the queue tail pointer */
1501 while (m
->m_next
!= NULL
) {
1503 priq
->msgq_bytes
+= m
->m_len
;
1507 priq
->msgq_bytes
+= m
->m_len
;
1510 m
->m_flags
|= M_EOR
;
1513 * Since the user space can not write a new msg
1514 * without completing the previous one, we can
1515 * reset this flag to start sending again.
1517 priq
->msgq_flags
&= ~(MSGQ_MSG_NOTDONE
);
1520 priq
->msgq_tail
= m
;
1522 SBLASTRECORDCHK(sb
, "sbappendstream 2");
1523 postevent(0, sb
, EV_RWBYTES
);
1528 * Pull data from priority queues to the serial snd queue
1529 * right before sending.
1532 sbpull_unordered_data(struct socket
*so
, int32_t off
, int32_t len
)
1535 struct msg_priq
*priq
= NULL
;
1537 VERIFY(so
->so_msg_state
!= NULL
);
1539 topull
= (off
+ len
) - so
->so_msg_state
->msg_serial_bytes
;
1542 while (i
>= MSG_PRI_MIN
&& topull
> 0) {
1543 struct mbuf
*m
= NULL
, *mqhead
= NULL
, *mend
= NULL
;
1544 priq
= &so
->so_msg_state
->msg_priq
[i
];
1545 if ((priq
->msgq_flags
& MSGQ_MSG_NOTDONE
) &&
1546 priq
->msgq_head
== NULL
) {
1548 * We were in the middle of sending
1549 * a message and we have not seen the
1552 VERIFY(priq
->msgq_lastmsg
== NULL
&&
1553 priq
->msgq_tail
== NULL
);
1556 if (priq
->msgq_head
!= NULL
) {
1557 int32_t bytes
= 0, topull_tmp
= topull
;
1559 * We found a msg while scanning the priority
1560 * queue from high to low priority.
1562 m
= priq
->msgq_head
;
1567 * Move bytes from the priority queue to the
1568 * serial queue. Compute the number of bytes
1571 while (mqhead
->m_next
!= NULL
&& topull_tmp
> 0) {
1572 bytes
+= mqhead
->m_len
;
1573 topull_tmp
-= mqhead
->m_len
;
1575 mqhead
= mqhead
->m_next
;
1578 if (mqhead
->m_next
== NULL
) {
1580 * If we have only one more mbuf left,
1581 * move the last mbuf of this message to
1582 * serial queue and set the head of the
1583 * queue to be the next message.
1585 bytes
+= mqhead
->m_len
;
1587 mqhead
= m
->m_nextpkt
;
1588 if (!(mend
->m_flags
& M_EOR
)) {
1590 * We have not seen the end of
1591 * this message, so we can not
1594 priq
->msgq_flags
|= MSGQ_MSG_NOTDONE
;
1597 mend
->m_flags
&= ~(M_EOR
);
1600 /* propogate the next msg pointer */
1601 mqhead
->m_nextpkt
= m
->m_nextpkt
;
1603 priq
->msgq_head
= mqhead
;
1606 * if the lastmsg pointer points to
1607 * the mbuf that is being dequeued, update
1608 * it to point to the new head.
1610 if (priq
->msgq_lastmsg
== m
)
1611 priq
->msgq_lastmsg
= priq
->msgq_head
;
1613 m
->m_nextpkt
= NULL
;
1614 mend
->m_next
= NULL
;
1616 if (priq
->msgq_head
== NULL
) {
1617 /* Moved all messages, update tail */
1618 priq
->msgq_tail
= NULL
;
1619 VERIFY(priq
->msgq_lastmsg
== NULL
);
1622 /* Move it to serial sb_mb queue */
1623 if (so
->so_snd
.sb_mb
== NULL
) {
1624 so
->so_snd
.sb_mb
= m
;
1626 so
->so_snd
.sb_mbtail
->m_next
= m
;
1629 priq
->msgq_bytes
-= bytes
;
1630 VERIFY(priq
->msgq_bytes
>= 0);
1631 sbwakeup(&so
->so_snd
);
1633 so
->so_msg_state
->msg_serial_bytes
+= bytes
;
1634 so
->so_snd
.sb_mbtail
= mend
;
1635 so
->so_snd
.sb_lastrecord
= so
->so_snd
.sb_mb
;
1638 (off
+ len
) - so
->so_msg_state
->msg_serial_bytes
;
1640 if (priq
->msgq_flags
& MSGQ_MSG_NOTDONE
)
1646 sblastrecordchk(&so
->so_snd
, "sbpull_unordered_data");
1647 sblastmbufchk(&so
->so_snd
, "sbpull_unordered_data");
1651 * Compress mbuf chain m into the socket
1652 * buffer sb following mbuf n. If n
1653 * is null, the buffer is presumed empty.
1656 sbcompress(struct sockbuf
*sb
, struct mbuf
*m
, struct mbuf
*n
)
1658 int eor
= 0, compress
= (!(sb
->sb_flags
& SB_NOCOMPRESS
));
1662 /* There is nothing to compress; just update the tail */
1663 for (; n
->m_next
!= NULL
; n
= n
->m_next
)
1670 eor
|= m
->m_flags
& M_EOR
;
1671 if (compress
&& m
->m_len
== 0 && (eor
== 0 ||
1672 (((o
= m
->m_next
) || (o
= n
)) && o
->m_type
== m
->m_type
))) {
1673 if (sb
->sb_lastrecord
== m
)
1674 sb
->sb_lastrecord
= m
->m_next
;
1678 if (compress
&& n
!= NULL
&& (n
->m_flags
& M_EOR
) == 0 &&
1682 m
->m_len
<= MCLBYTES
/ 4 && /* XXX: Don't copy too much */
1683 m
->m_len
<= M_TRAILINGSPACE(n
) &&
1684 n
->m_type
== m
->m_type
) {
1685 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
) + n
->m_len
,
1686 (unsigned)m
->m_len
);
1687 n
->m_len
+= m
->m_len
;
1688 sb
->sb_cc
+= m
->m_len
;
1689 if (m
->m_type
!= MT_DATA
&& m
->m_type
!= MT_HEADER
&&
1690 m
->m_type
!= MT_OOBDATA
) {
1691 /* XXX: Probably don't need */
1692 sb
->sb_ctl
+= m
->m_len
;
1704 m
->m_flags
&= ~M_EOR
;
1712 printf("semi-panic: sbcompress\n");
1715 SBLASTMBUFCHK(sb
, __func__
);
1716 postevent(0, sb
, EV_RWBYTES
);
1720 sb_empty_assert(struct sockbuf
*sb
, const char *where
)
1722 if (!(sb
->sb_cc
== 0 && sb
->sb_mb
== NULL
&& sb
->sb_mbcnt
== 0 &&
1723 sb
->sb_mbtail
== NULL
&& sb
->sb_lastrecord
== NULL
)) {
1724 panic("%s: sb %p so %p cc %d mbcnt %d mb %p mbtail %p "
1725 "lastrecord %p\n", where
, sb
, sb
->sb_so
, sb
->sb_cc
,
1726 sb
->sb_mbcnt
, sb
->sb_mb
, sb
->sb_mbtail
,
1733 sbflush_priq(struct msg_priq
*priq
)
1736 m
= priq
->msgq_head
;
1739 priq
->msgq_head
= priq
->msgq_tail
= priq
->msgq_lastmsg
= NULL
;
1740 priq
->msgq_bytes
= priq
->msgq_flags
= 0;
1744 * Free all mbufs in a sockbuf.
1745 * Check that all resources are reclaimed.
1748 sbflush(struct sockbuf
*sb
)
1750 void *lr_saved
= __builtin_return_address(0);
1751 struct socket
*so
= sb
->sb_so
;
1753 lck_mtx_t
*mutex_held
;
1757 /* so_usecount may be 0 if we get here from sofreelastref() */
1759 panic("%s: null so, sb=%p sb_flags=0x%x lr=%p\n",
1760 __func__
, sb
, sb
->sb_flags
, lr_saved
);
1762 } else if (so
->so_usecount
< 0) {
1763 panic("%s: sb=%p sb_flags=0x%x sb_so=%p usecount=%d lr=%p "
1764 "lrh= %s\n", __func__
, sb
, sb
->sb_flags
, so
,
1765 so
->so_usecount
, lr_saved
, solockhistory_nr(so
));
1770 * XXX: This code is currently commented out, because we may get here
1771 * as part of sofreelastref(), and at that time, pr_getlock() may no
1772 * longer be able to return us the lock; this will be fixed in future.
1774 if (so
->so_proto
->pr_getlock
!= NULL
)
1775 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
1777 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
1779 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
1783 * Obtain lock on the socket buffer (SB_LOCK). This is required
1784 * to prevent the socket buffer from being unexpectedly altered
1785 * while it is used by another thread in socket send/receive.
1787 * sblock() must not fail here, hence the assertion.
1789 (void) sblock(sb
, SBL_WAIT
| SBL_NOINTR
| SBL_IGNDEFUNCT
);
1790 VERIFY(sb
->sb_flags
& SB_LOCK
);
1792 while (sb
->sb_mbcnt
> 0) {
1794 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
1795 * we would loop forever. Panic instead.
1797 if (!sb
->sb_cc
&& (sb
->sb_mb
== NULL
|| sb
->sb_mb
->m_len
))
1799 sbdrop(sb
, (int)sb
->sb_cc
);
1802 if (!(sb
->sb_flags
& SB_RECV
) && (so
->so_flags
& SOF_ENABLE_MSGS
)) {
1803 VERIFY(so
->so_msg_state
!= NULL
);
1804 for (i
= MSG_PRI_MIN
; i
<= MSG_PRI_MAX
; ++i
) {
1805 sbflush_priq(&so
->so_msg_state
->msg_priq
[i
]);
1807 so
->so_msg_state
->msg_serial_bytes
= 0;
1808 so
->so_msg_state
->msg_uno_bytes
= 0;
1811 sb_empty_assert(sb
, __func__
);
1812 postevent(0, sb
, EV_RWBYTES
);
1814 sbunlock(sb
, TRUE
); /* keep socket locked */
1818 * Drop data from (the front of) a sockbuf.
1819 * use m_freem_list to free the mbuf structures
1820 * under a single lock... this is done by pruning
1821 * the top of the tree from the body by keeping track
1822 * of where we get to in the tree and then zeroing the
1823 * two pertinent pointers m_nextpkt and m_next
1824 * the socket buffer is then updated to point at the new
1825 * top of the tree and the pruned area is released via
1829 sbdrop(struct sockbuf
*sb
, int len
)
1831 struct mbuf
*m
, *free_list
, *ml
;
1832 struct mbuf
*next
, *last
;
1834 next
= (m
= sb
->sb_mb
) ? m
->m_nextpkt
: 0;
1836 if ((m
!= NULL
) && (len
> 0) &&
1837 (!(sb
->sb_flags
& SB_RECV
)) &&
1838 ((sb
->sb_so
->so_flags
& SOF_MP_SUBFLOW
) ||
1839 ((SOCK_CHECK_DOM(sb
->sb_so
, PF_MULTIPATH
)) &&
1840 (SOCK_CHECK_PROTO(sb
->sb_so
, IPPROTO_TCP
)))) &&
1841 (!(sb
->sb_so
->so_flags1
& SOF1_POST_FALLBACK_SYNC
))) {
1842 mptcp_preproc_sbdrop(m
, (unsigned int)len
);
1845 KERNEL_DEBUG((DBG_FNC_SBDROP
| DBG_FUNC_START
), sb
, len
, 0, 0, 0);
1847 free_list
= last
= m
;
1848 ml
= (struct mbuf
*)0;
1854 * temporarily replacing this panic with printf
1855 * because it occurs occasionally when closing
1856 * a socket when there is no harm in ignoring
1857 * it. This problem will be investigated
1860 /* panic("sbdrop"); */
1861 printf("sbdrop - count not zero\n");
1864 * zero the counts. if we have no mbufs,
1865 * we have no data (PR-2986815)
1869 if (!(sb
->sb_flags
& SB_RECV
) &&
1870 (sb
->sb_so
->so_flags
& SOF_ENABLE_MSGS
)) {
1871 sb
->sb_so
->so_msg_state
->
1872 msg_serial_bytes
= 0;
1877 next
= m
->m_nextpkt
;
1880 if (m
->m_len
> len
) {
1884 if (m
->m_type
!= MT_DATA
&& m
->m_type
!= MT_HEADER
&&
1885 m
->m_type
!= MT_OOBDATA
)
1895 while (m
&& m
->m_len
== 0) {
1902 ml
->m_next
= (struct mbuf
*)0;
1903 last
->m_nextpkt
= (struct mbuf
*)0;
1904 m_freem_list(free_list
);
1908 m
->m_nextpkt
= next
;
1914 * First part is an inline SB_EMPTY_FIXUP(). Second part
1915 * makes sure sb_lastrecord is up-to-date if we dropped
1916 * part of the last record.
1920 sb
->sb_mbtail
= NULL
;
1921 sb
->sb_lastrecord
= NULL
;
1922 } else if (m
->m_nextpkt
== NULL
) {
1923 sb
->sb_lastrecord
= m
;
1927 cfil_sock_buf_update(sb
);
1928 #endif /* CONTENT_FILTER */
1930 postevent(0, sb
, EV_RWBYTES
);
1932 KERNEL_DEBUG((DBG_FNC_SBDROP
| DBG_FUNC_END
), sb
, 0, 0, 0, 0);
1936 * Drop a record off the front of a sockbuf
1937 * and move the next record to the front.
1940 sbdroprecord(struct sockbuf
*sb
)
1942 struct mbuf
*m
, *mn
;
1946 sb
->sb_mb
= m
->m_nextpkt
;
1954 postevent(0, sb
, EV_RWBYTES
);
1958 * Create a "control" mbuf containing the specified data
1959 * with the specified type for presentation on a socket buffer.
1962 sbcreatecontrol(caddr_t p
, int size
, int type
, int level
)
1967 if (CMSG_SPACE((u_int
)size
) > MLEN
)
1968 return ((struct mbuf
*)NULL
);
1969 if ((m
= m_get(M_DONTWAIT
, MT_CONTROL
)) == NULL
)
1970 return ((struct mbuf
*)NULL
);
1971 cp
= mtod(m
, struct cmsghdr
*);
1972 VERIFY(IS_P2ALIGNED(cp
, sizeof (u_int32_t
)));
1973 /* XXX check size? */
1974 (void) memcpy(CMSG_DATA(cp
), p
, size
);
1975 m
->m_len
= CMSG_SPACE(size
);
1976 cp
->cmsg_len
= CMSG_LEN(size
);
1977 cp
->cmsg_level
= level
;
1978 cp
->cmsg_type
= type
;
1983 sbcreatecontrol_mbuf(caddr_t p
, int size
, int type
, int level
, struct mbuf
**mp
)
1989 *mp
= sbcreatecontrol(p
, size
, type
, level
);
1993 if (CMSG_SPACE((u_int
)size
) + (*mp
)->m_len
> MLEN
) {
1994 mp
= &(*mp
)->m_next
;
1995 *mp
= sbcreatecontrol(p
, size
, type
, level
);
2001 cp
= (struct cmsghdr
*)(void *)(mtod(m
, char *) + m
->m_len
);
2002 /* CMSG_SPACE ensures 32-bit alignment */
2003 VERIFY(IS_P2ALIGNED(cp
, sizeof (u_int32_t
)));
2004 m
->m_len
+= CMSG_SPACE(size
);
2006 /* XXX check size? */
2007 (void) memcpy(CMSG_DATA(cp
), p
, size
);
2008 cp
->cmsg_len
= CMSG_LEN(size
);
2009 cp
->cmsg_level
= level
;
2010 cp
->cmsg_type
= type
;
2017 * Some routines that return EOPNOTSUPP for entry points that are not
2018 * supported by a protocol. Fill in as needed.
2021 pru_abort_notsupp(struct socket
*so
)
2024 return (EOPNOTSUPP
);
2028 pru_accept_notsupp(struct socket
*so
, struct sockaddr
**nam
)
2030 #pragma unused(so, nam)
2031 return (EOPNOTSUPP
);
2035 pru_attach_notsupp(struct socket
*so
, int proto
, struct proc
*p
)
2037 #pragma unused(so, proto, p)
2038 return (EOPNOTSUPP
);
2042 pru_bind_notsupp(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
2044 #pragma unused(so, nam, p)
2045 return (EOPNOTSUPP
);
2049 pru_connect_notsupp(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
2051 #pragma unused(so, nam, p)
2052 return (EOPNOTSUPP
);
2056 pru_connect2_notsupp(struct socket
*so1
, struct socket
*so2
)
2058 #pragma unused(so1, so2)
2059 return (EOPNOTSUPP
);
2063 pru_connectx_notsupp(struct socket
*so
, struct sockaddr_list
**src_sl
,
2064 struct sockaddr_list
**dst_sl
, struct proc
*p
, uint32_t ifscope
,
2065 sae_associd_t aid
, sae_connid_t
*pcid
, uint32_t flags
, void *arg
,
2066 uint32_t arglen
, struct uio
*uio
, user_ssize_t
*bytes_written
)
2068 #pragma unused(so, src_sl, dst_sl, p, ifscope, aid, pcid, flags, arg, arglen, uio, bytes_written)
2069 return (EOPNOTSUPP
);
2073 pru_control_notsupp(struct socket
*so
, u_long cmd
, caddr_t data
,
2074 struct ifnet
*ifp
, struct proc
*p
)
2076 #pragma unused(so, cmd, data, ifp, p)
2077 return (EOPNOTSUPP
);
2081 pru_detach_notsupp(struct socket
*so
)
2084 return (EOPNOTSUPP
);
2088 pru_disconnect_notsupp(struct socket
*so
)
2091 return (EOPNOTSUPP
);
2095 pru_disconnectx_notsupp(struct socket
*so
, sae_associd_t aid
, sae_connid_t cid
)
2097 #pragma unused(so, aid, cid)
2098 return (EOPNOTSUPP
);
2102 pru_listen_notsupp(struct socket
*so
, struct proc
*p
)
2104 #pragma unused(so, p)
2105 return (EOPNOTSUPP
);
2109 pru_peeloff_notsupp(struct socket
*so
, sae_associd_t aid
, struct socket
**psop
)
2111 #pragma unused(so, aid, psop)
2112 return (EOPNOTSUPP
);
2116 pru_peeraddr_notsupp(struct socket
*so
, struct sockaddr
**nam
)
2118 #pragma unused(so, nam)
2119 return (EOPNOTSUPP
);
2123 pru_rcvd_notsupp(struct socket
*so
, int flags
)
2125 #pragma unused(so, flags)
2126 return (EOPNOTSUPP
);
2130 pru_rcvoob_notsupp(struct socket
*so
, struct mbuf
*m
, int flags
)
2132 #pragma unused(so, m, flags)
2133 return (EOPNOTSUPP
);
2137 pru_send_notsupp(struct socket
*so
, int flags
, struct mbuf
*m
,
2138 struct sockaddr
*addr
, struct mbuf
*control
, struct proc
*p
)
2140 #pragma unused(so, flags, m, addr, control, p)
2141 return (EOPNOTSUPP
);
2145 pru_send_list_notsupp(struct socket
*so
, int flags
, struct mbuf
*m
,
2146 struct sockaddr
*addr
, struct mbuf
*control
, struct proc
*p
)
2148 #pragma unused(so, flags, m, addr, control, p)
2149 return (EOPNOTSUPP
);
2153 * This isn't really a ``null'' operation, but it's the default one
2154 * and doesn't do anything destructive.
2157 pru_sense_null(struct socket
*so
, void *ub
, int isstat64
)
2159 if (isstat64
!= 0) {
2160 struct stat64
*sb64
;
2162 sb64
= (struct stat64
*)ub
;
2163 sb64
->st_blksize
= so
->so_snd
.sb_hiwat
;
2167 sb
= (struct stat
*)ub
;
2168 sb
->st_blksize
= so
->so_snd
.sb_hiwat
;
2176 pru_sosend_notsupp(struct socket
*so
, struct sockaddr
*addr
, struct uio
*uio
,
2177 struct mbuf
*top
, struct mbuf
*control
, int flags
)
2179 #pragma unused(so, addr, uio, top, control, flags)
2180 return (EOPNOTSUPP
);
2184 pru_sosend_list_notsupp(struct socket
*so
, struct uio
**uio
,
2185 u_int uiocnt
, int flags
)
2187 #pragma unused(so, uio, uiocnt, flags)
2188 return (EOPNOTSUPP
);
2192 pru_soreceive_notsupp(struct socket
*so
, struct sockaddr
**paddr
,
2193 struct uio
*uio
, struct mbuf
**mp0
, struct mbuf
**controlp
, int *flagsp
)
2195 #pragma unused(so, paddr, uio, mp0, controlp, flagsp)
2196 return (EOPNOTSUPP
);
2200 pru_soreceive_list_notsupp(struct socket
*so
,
2201 struct recv_msg_elem
*recv_msg_array
, u_int uiocnt
, int *flagsp
)
2203 #pragma unused(so, recv_msg_array, uiocnt, flagsp)
2204 return (EOPNOTSUPP
);
2208 pru_shutdown_notsupp(struct socket
*so
)
2211 return (EOPNOTSUPP
);
2215 pru_sockaddr_notsupp(struct socket
*so
, struct sockaddr
**nam
)
2217 #pragma unused(so, nam)
2218 return (EOPNOTSUPP
);
2222 pru_sopoll_notsupp(struct socket
*so
, int events
, kauth_cred_t cred
, void *wql
)
2224 #pragma unused(so, events, cred, wql)
2225 return (EOPNOTSUPP
);
2229 pru_socheckopt_null(struct socket
*so
, struct sockopt
*sopt
)
2231 #pragma unused(so, sopt)
2233 * Allow all options for set/get by default.
2239 pru_preconnect_null(struct socket
*so
)
2246 pru_sanitize(struct pr_usrreqs
*pru
)
2248 #define DEFAULT(foo, bar) if ((foo) == NULL) (foo) = (bar)
2249 DEFAULT(pru
->pru_abort
, pru_abort_notsupp
);
2250 DEFAULT(pru
->pru_accept
, pru_accept_notsupp
);
2251 DEFAULT(pru
->pru_attach
, pru_attach_notsupp
);
2252 DEFAULT(pru
->pru_bind
, pru_bind_notsupp
);
2253 DEFAULT(pru
->pru_connect
, pru_connect_notsupp
);
2254 DEFAULT(pru
->pru_connect2
, pru_connect2_notsupp
);
2255 DEFAULT(pru
->pru_connectx
, pru_connectx_notsupp
);
2256 DEFAULT(pru
->pru_control
, pru_control_notsupp
);
2257 DEFAULT(pru
->pru_detach
, pru_detach_notsupp
);
2258 DEFAULT(pru
->pru_disconnect
, pru_disconnect_notsupp
);
2259 DEFAULT(pru
->pru_disconnectx
, pru_disconnectx_notsupp
);
2260 DEFAULT(pru
->pru_listen
, pru_listen_notsupp
);
2261 DEFAULT(pru
->pru_peeloff
, pru_peeloff_notsupp
);
2262 DEFAULT(pru
->pru_peeraddr
, pru_peeraddr_notsupp
);
2263 DEFAULT(pru
->pru_rcvd
, pru_rcvd_notsupp
);
2264 DEFAULT(pru
->pru_rcvoob
, pru_rcvoob_notsupp
);
2265 DEFAULT(pru
->pru_send
, pru_send_notsupp
);
2266 DEFAULT(pru
->pru_send_list
, pru_send_list_notsupp
);
2267 DEFAULT(pru
->pru_sense
, pru_sense_null
);
2268 DEFAULT(pru
->pru_shutdown
, pru_shutdown_notsupp
);
2269 DEFAULT(pru
->pru_sockaddr
, pru_sockaddr_notsupp
);
2270 DEFAULT(pru
->pru_sopoll
, pru_sopoll_notsupp
);
2271 DEFAULT(pru
->pru_soreceive
, pru_soreceive_notsupp
);
2272 DEFAULT(pru
->pru_soreceive_list
, pru_soreceive_list_notsupp
);
2273 DEFAULT(pru
->pru_sosend
, pru_sosend_notsupp
);
2274 DEFAULT(pru
->pru_sosend_list
, pru_sosend_list_notsupp
);
2275 DEFAULT(pru
->pru_socheckopt
, pru_socheckopt_null
);
2276 DEFAULT(pru
->pru_preconnect
, pru_preconnect_null
);
2281 * The following are macros on BSD and functions on Darwin
2285 * Do we need to notify the other side when I/O is possible?
2289 sb_notify(struct sockbuf
*sb
)
2291 return (sb
->sb_waiters
> 0 ||
2292 (sb
->sb_flags
& (SB_SEL
|SB_ASYNC
|SB_UPCALL
|SB_KNOTE
)));
2296 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
2297 * This is problematical if the fields are unsigned, as the space might
2298 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
2299 * overflow and return 0.
2302 sbspace(struct sockbuf
*sb
)
2305 int space
= imin((int)(sb
->sb_hiwat
- sb
->sb_cc
),
2306 (int)(sb
->sb_mbmax
- sb
->sb_mbcnt
));
2308 if (sb
->sb_preconn_hiwat
!= 0)
2309 space
= imin((int)(sb
->sb_preconn_hiwat
- sb
->sb_cc
), space
);
2314 /* Compensate for data being processed by content filters */
2316 pending
= cfil_sock_data_space(sb
);
2317 #endif /* CONTENT_FILTER */
2318 if (pending
> space
)
2327 * If this socket has priority queues, check if there is enough
2328 * space in the priority queue for this msg.
2331 msgq_sbspace(struct socket
*so
, struct mbuf
*control
)
2333 int space
= 0, error
;
2335 VERIFY(so
->so_type
== SOCK_STREAM
&&
2336 SOCK_PROTO(so
) == IPPROTO_TCP
);
2337 if (control
!= NULL
) {
2338 error
= tcp_get_msg_priority(control
, &msgpri
);
2344 space
= (so
->so_snd
.sb_idealsize
/ MSG_PRI_COUNT
) -
2345 so
->so_msg_state
->msg_priq
[msgpri
].msgq_bytes
;
2351 /* do we have to send all at once on a socket? */
2353 sosendallatonce(struct socket
*so
)
2355 return (so
->so_proto
->pr_flags
& PR_ATOMIC
);
2358 /* can we read something from so? */
2360 soreadable(struct socket
*so
)
2362 return (so
->so_rcv
.sb_cc
>= so
->so_rcv
.sb_lowat
||
2363 ((so
->so_state
& SS_CANTRCVMORE
)
2365 && cfil_sock_data_pending(&so
->so_rcv
) == 0
2366 #endif /* CONTENT_FILTER */
2368 so
->so_comp
.tqh_first
|| so
->so_error
);
2371 /* can we write something to so? */
2374 sowriteable(struct socket
*so
)
2376 if ((so
->so_state
& SS_CANTSENDMORE
) ||
2379 if (so_wait_for_if_feedback(so
) || !socanwrite(so
))
2381 if (so
->so_flags1
& SOF1_PRECONNECT_DATA
)
2384 if (sbspace(&(so
)->so_snd
) >= (so
)->so_snd
.sb_lowat
) {
2385 if (so
->so_flags
& SOF_NOTSENT_LOWAT
) {
2386 if ((SOCK_DOM(so
) == PF_INET6
||
2387 SOCK_DOM(so
) == PF_INET
) &&
2388 so
->so_type
== SOCK_STREAM
) {
2389 return (tcp_notsent_lowat_check(so
));
2392 else if ((SOCK_DOM(so
) == PF_MULTIPATH
) &&
2393 (SOCK_PROTO(so
) == IPPROTO_TCP
)) {
2394 return (mptcp_notsent_lowat_check(so
));
2407 /* adjust counters in sb reflecting allocation of m */
2410 sballoc(struct sockbuf
*sb
, struct mbuf
*m
)
2413 sb
->sb_cc
+= m
->m_len
;
2414 if (m
->m_type
!= MT_DATA
&& m
->m_type
!= MT_HEADER
&&
2415 m
->m_type
!= MT_OOBDATA
)
2416 sb
->sb_ctl
+= m
->m_len
;
2417 sb
->sb_mbcnt
+= MSIZE
;
2419 if (m
->m_flags
& M_EXT
) {
2420 sb
->sb_mbcnt
+= m
->m_ext
.ext_size
;
2421 cnt
+= (m
->m_ext
.ext_size
>> MSIZESHIFT
);
2423 OSAddAtomic(cnt
, &total_sbmb_cnt
);
2424 VERIFY(total_sbmb_cnt
> 0);
2425 if (total_sbmb_cnt
> total_sbmb_cnt_peak
)
2426 total_sbmb_cnt_peak
= total_sbmb_cnt
;
2429 * If data is being appended to the send socket buffer,
2430 * update the send byte count
2432 if (!(sb
->sb_flags
& SB_RECV
))
2433 OSAddAtomic(cnt
, &total_snd_byte_count
);
2436 /* adjust counters in sb reflecting freeing of m */
2438 sbfree(struct sockbuf
*sb
, struct mbuf
*m
)
2442 sb
->sb_cc
-= m
->m_len
;
2443 if (m
->m_type
!= MT_DATA
&& m
->m_type
!= MT_HEADER
&&
2444 m
->m_type
!= MT_OOBDATA
)
2445 sb
->sb_ctl
-= m
->m_len
;
2446 sb
->sb_mbcnt
-= MSIZE
;
2447 if (m
->m_flags
& M_EXT
) {
2448 sb
->sb_mbcnt
-= m
->m_ext
.ext_size
;
2449 cnt
-= (m
->m_ext
.ext_size
>> MSIZESHIFT
);
2451 OSAddAtomic(cnt
, &total_sbmb_cnt
);
2452 VERIFY(total_sbmb_cnt
>= 0);
2455 * If data is being removed from the send socket buffer,
2456 * update the send byte count
2458 if (!(sb
->sb_flags
& SB_RECV
)) {
2459 OSAddAtomic(cnt
, &total_snd_byte_count
);
2464 * Set lock on sockbuf sb; sleep if lock is already held.
2465 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
2466 * Returns error without lock if sleep is interrupted.
2469 sblock(struct sockbuf
*sb
, uint32_t flags
)
2471 boolean_t nointr
= ((sb
->sb_flags
& SB_NOINTR
) || (flags
& SBL_NOINTR
));
2472 void *lr_saved
= __builtin_return_address(0);
2473 struct socket
*so
= sb
->sb_so
;
2476 thread_t tp
= current_thread();
2478 VERIFY((flags
& SBL_VALID
) == flags
);
2480 /* so_usecount may be 0 if we get here from sofreelastref() */
2482 panic("%s: null so, sb=%p sb_flags=0x%x lr=%p\n",
2483 __func__
, sb
, sb
->sb_flags
, lr_saved
);
2485 } else if (so
->so_usecount
< 0) {
2486 panic("%s: sb=%p sb_flags=0x%x sb_so=%p usecount=%d lr=%p "
2487 "lrh= %s\n", __func__
, sb
, sb
->sb_flags
, so
,
2488 so
->so_usecount
, lr_saved
, solockhistory_nr(so
));
2493 * The content filter thread must hold the sockbuf lock
2495 if ((so
->so_flags
& SOF_CONTENT_FILTER
) && sb
->sb_cfil_thread
== tp
) {
2497 * Don't panic if we are defunct because SB_LOCK has
2498 * been cleared by sodefunct()
2500 if (!(so
->so_flags
& SOF_DEFUNCT
) && !(sb
->sb_flags
& SB_LOCK
))
2501 panic("%s: SB_LOCK not held for %p\n",
2504 /* Keep the sockbuf locked */
2508 if ((sb
->sb_flags
& SB_LOCK
) && !(flags
& SBL_WAIT
))
2509 return (EWOULDBLOCK
);
2511 * We may get here from sorflush(), in which case "sb" may not
2512 * point to the real socket buffer. Use the actual socket buffer
2513 * address from the socket instead.
2515 wchan
= (sb
->sb_flags
& SB_RECV
) ?
2516 &so
->so_rcv
.sb_flags
: &so
->so_snd
.sb_flags
;
2519 * A content filter thread has exclusive access to the sockbuf
2520 * until it clears the
2522 while ((sb
->sb_flags
& SB_LOCK
) ||
2523 ((so
->so_flags
& SOF_CONTENT_FILTER
) &&
2524 sb
->sb_cfil_thread
!= NULL
)) {
2525 lck_mtx_t
*mutex_held
;
2528 * XXX: This code should be moved up above outside of this loop;
2529 * however, we may get here as part of sofreelastref(), and
2530 * at that time pr_getlock() may no longer be able to return
2531 * us the lock. This will be fixed in future.
2533 if (so
->so_proto
->pr_getlock
!= NULL
)
2534 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
2536 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
2538 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
2541 VERIFY(sb
->sb_wantlock
!= 0);
2543 error
= msleep(wchan
, mutex_held
,
2544 nointr
? PSOCK
: PSOCK
| PCATCH
,
2545 nointr
? "sb_lock_nointr" : "sb_lock", NULL
);
2547 VERIFY(sb
->sb_wantlock
!= 0);
2550 if (error
== 0 && (so
->so_flags
& SOF_DEFUNCT
) &&
2551 !(flags
& SBL_IGNDEFUNCT
)) {
2553 SODEFUNCTLOG(("%s[%d]: defunct so 0x%llx [%d,%d] "
2554 "(%d)\n", __func__
, proc_selfpid(),
2555 (uint64_t)VM_KERNEL_ADDRPERM(so
),
2556 SOCK_DOM(so
), SOCK_TYPE(so
), error
));
2562 sb
->sb_flags
|= SB_LOCK
;
2567 * Release lock on sockbuf sb
2570 sbunlock(struct sockbuf
*sb
, boolean_t keeplocked
)
2572 void *lr_saved
= __builtin_return_address(0);
2573 struct socket
*so
= sb
->sb_so
;
2574 thread_t tp
= current_thread();
2576 /* so_usecount may be 0 if we get here from sofreelastref() */
2578 panic("%s: null so, sb=%p sb_flags=0x%x lr=%p\n",
2579 __func__
, sb
, sb
->sb_flags
, lr_saved
);
2581 } else if (so
->so_usecount
< 0) {
2582 panic("%s: sb=%p sb_flags=0x%x sb_so=%p usecount=%d lr=%p "
2583 "lrh= %s\n", __func__
, sb
, sb
->sb_flags
, so
,
2584 so
->so_usecount
, lr_saved
, solockhistory_nr(so
));
2589 * The content filter thread must hold the sockbuf lock
2591 if ((so
->so_flags
& SOF_CONTENT_FILTER
) && sb
->sb_cfil_thread
== tp
) {
2593 * Don't panic if we are defunct because SB_LOCK has
2594 * been cleared by sodefunct()
2596 if (!(so
->so_flags
& SOF_DEFUNCT
) &&
2597 !(sb
->sb_flags
& SB_LOCK
) &&
2598 !(so
->so_state
& SS_DEFUNCT
) &&
2599 !(so
->so_flags1
& SOF1_DEFUNCTINPROG
)) {
2600 panic("%s: SB_LOCK not held for %p\n",
2603 /* Keep the sockbuf locked and proceed */
2605 VERIFY((sb
->sb_flags
& SB_LOCK
) ||
2606 (so
->so_state
& SS_DEFUNCT
) ||
2607 (so
->so_flags1
& SOF1_DEFUNCTINPROG
));
2609 sb
->sb_flags
&= ~SB_LOCK
;
2611 if (sb
->sb_wantlock
> 0) {
2613 * We may get here from sorflush(), in which case "sb"
2614 * may not point to the real socket buffer. Use the
2615 * actual socket buffer address from the socket instead.
2617 wakeup((sb
->sb_flags
& SB_RECV
) ? &so
->so_rcv
.sb_flags
:
2618 &so
->so_snd
.sb_flags
);
2622 if (!keeplocked
) { /* unlock on exit */
2623 lck_mtx_t
*mutex_held
;
2625 if (so
->so_proto
->pr_getlock
!= NULL
)
2626 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
2628 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
2630 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
2632 VERIFY(so
->so_usecount
!= 0);
2634 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
2635 so
->next_unlock_lr
= (so
->next_unlock_lr
+ 1) % SO_LCKDBG_MAX
;
2636 lck_mtx_unlock(mutex_held
);
2641 sorwakeup(struct socket
*so
)
2643 if (sb_notify(&so
->so_rcv
))
2644 sowakeup(so
, &so
->so_rcv
);
2648 sowwakeup(struct socket
*so
)
2650 if (sb_notify(&so
->so_snd
))
2651 sowakeup(so
, &so
->so_snd
);
2655 soevent(struct socket
*so
, long hint
)
2657 if (so
->so_flags
& SOF_KNOTE
)
2658 KNOTE(&so
->so_klist
, hint
);
2660 soevupcall(so
, hint
);
2663 * Don't post an event if this a subflow socket or
2664 * the app has opted out of using cellular interface
2666 if ((hint
& SO_FILT_HINT_IFDENIED
) &&
2667 !(so
->so_flags
& SOF_MP_SUBFLOW
) &&
2668 !(so
->so_restrictions
& SO_RESTRICT_DENY_CELLULAR
) &&
2669 !(so
->so_restrictions
& SO_RESTRICT_DENY_EXPENSIVE
))
2670 soevent_ifdenied(so
);
2674 soevupcall(struct socket
*so
, u_int32_t hint
)
2676 if (so
->so_event
!= NULL
) {
2677 caddr_t so_eventarg
= so
->so_eventarg
;
2678 int locked
= hint
& SO_FILT_HINT_LOCKED
;
2680 hint
&= so
->so_eventmask
;
2683 socket_unlock(so
, 0);
2685 so
->so_event(so
, so_eventarg
, hint
);
2694 soevent_ifdenied(struct socket
*so
)
2696 struct kev_netpolicy_ifdenied ev_ifdenied
;
2698 bzero(&ev_ifdenied
, sizeof (ev_ifdenied
));
2700 * The event consumer is interested about the effective {upid,pid,uuid}
2701 * info which can be different than the those related to the process
2702 * that recently performed a system call on the socket, i.e. when the
2703 * socket is delegated.
2705 if (so
->so_flags
& SOF_DELEGATED
) {
2706 ev_ifdenied
.ev_data
.eupid
= so
->e_upid
;
2707 ev_ifdenied
.ev_data
.epid
= so
->e_pid
;
2708 uuid_copy(ev_ifdenied
.ev_data
.euuid
, so
->e_uuid
);
2710 ev_ifdenied
.ev_data
.eupid
= so
->last_upid
;
2711 ev_ifdenied
.ev_data
.epid
= so
->last_pid
;
2712 uuid_copy(ev_ifdenied
.ev_data
.euuid
, so
->last_uuid
);
2715 if (++so
->so_ifdenied_notifies
> 1) {
2717 * Allow for at most one kernel event to be generated per
2718 * socket; so_ifdenied_notifies is reset upon changes in
2719 * the UUID policy. See comments in inp_update_policy.
2721 if (net_io_policy_log
) {
2724 uuid_unparse(ev_ifdenied
.ev_data
.euuid
, buf
);
2725 log(LOG_DEBUG
, "%s[%d]: so 0x%llx [%d,%d] epid %d "
2726 "euuid %s%s has %d redundant events supressed\n",
2727 __func__
, so
->last_pid
,
2728 (uint64_t)VM_KERNEL_ADDRPERM(so
), SOCK_DOM(so
),
2729 SOCK_TYPE(so
), ev_ifdenied
.ev_data
.epid
, buf
,
2730 ((so
->so_flags
& SOF_DELEGATED
) ?
2731 " [delegated]" : ""), so
->so_ifdenied_notifies
);
2734 if (net_io_policy_log
) {
2737 uuid_unparse(ev_ifdenied
.ev_data
.euuid
, buf
);
2738 log(LOG_DEBUG
, "%s[%d]: so 0x%llx [%d,%d] epid %d "
2739 "euuid %s%s event posted\n", __func__
,
2740 so
->last_pid
, (uint64_t)VM_KERNEL_ADDRPERM(so
),
2741 SOCK_DOM(so
), SOCK_TYPE(so
),
2742 ev_ifdenied
.ev_data
.epid
, buf
,
2743 ((so
->so_flags
& SOF_DELEGATED
) ?
2744 " [delegated]" : ""));
2746 netpolicy_post_msg(KEV_NETPOLICY_IFDENIED
, &ev_ifdenied
.ev_data
,
2747 sizeof (ev_ifdenied
));
2752 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
2755 dup_sockaddr(struct sockaddr
*sa
, int canwait
)
2757 struct sockaddr
*sa2
;
2759 MALLOC(sa2
, struct sockaddr
*, sa
->sa_len
, M_SONAME
,
2760 canwait
? M_WAITOK
: M_NOWAIT
);
2762 bcopy(sa
, sa2
, sa
->sa_len
);
2767 * Create an external-format (``xsocket'') structure using the information
2768 * in the kernel-format socket structure pointed to by so. This is done
2769 * to reduce the spew of irrelevant information over this interface,
2770 * to isolate user code from changes in the kernel structure, and
2771 * potentially to provide information-hiding if we decide that
2772 * some of this information should be hidden from users.
2775 sotoxsocket(struct socket
*so
, struct xsocket
*xso
)
2777 xso
->xso_len
= sizeof (*xso
);
2778 xso
->xso_so
= (_XSOCKET_PTR(struct socket
*))VM_KERNEL_ADDRPERM(so
);
2779 xso
->so_type
= so
->so_type
;
2780 xso
->so_options
= (short)(so
->so_options
& 0xffff);
2781 xso
->so_linger
= so
->so_linger
;
2782 xso
->so_state
= so
->so_state
;
2783 xso
->so_pcb
= (_XSOCKET_PTR(caddr_t
))VM_KERNEL_ADDRPERM(so
->so_pcb
);
2785 xso
->xso_protocol
= SOCK_PROTO(so
);
2786 xso
->xso_family
= SOCK_DOM(so
);
2788 xso
->xso_protocol
= xso
->xso_family
= 0;
2790 xso
->so_qlen
= so
->so_qlen
;
2791 xso
->so_incqlen
= so
->so_incqlen
;
2792 xso
->so_qlimit
= so
->so_qlimit
;
2793 xso
->so_timeo
= so
->so_timeo
;
2794 xso
->so_error
= so
->so_error
;
2795 xso
->so_pgid
= so
->so_pgid
;
2796 xso
->so_oobmark
= so
->so_oobmark
;
2797 sbtoxsockbuf(&so
->so_snd
, &xso
->so_snd
);
2798 sbtoxsockbuf(&so
->so_rcv
, &xso
->so_rcv
);
2799 xso
->so_uid
= kauth_cred_getuid(so
->so_cred
);
2805 sotoxsocket64(struct socket
*so
, struct xsocket64
*xso
)
2807 xso
->xso_len
= sizeof (*xso
);
2808 xso
->xso_so
= (u_int64_t
)VM_KERNEL_ADDRPERM(so
);
2809 xso
->so_type
= so
->so_type
;
2810 xso
->so_options
= (short)(so
->so_options
& 0xffff);
2811 xso
->so_linger
= so
->so_linger
;
2812 xso
->so_state
= so
->so_state
;
2813 xso
->so_pcb
= (u_int64_t
)VM_KERNEL_ADDRPERM(so
->so_pcb
);
2815 xso
->xso_protocol
= SOCK_PROTO(so
);
2816 xso
->xso_family
= SOCK_DOM(so
);
2818 xso
->xso_protocol
= xso
->xso_family
= 0;
2820 xso
->so_qlen
= so
->so_qlen
;
2821 xso
->so_incqlen
= so
->so_incqlen
;
2822 xso
->so_qlimit
= so
->so_qlimit
;
2823 xso
->so_timeo
= so
->so_timeo
;
2824 xso
->so_error
= so
->so_error
;
2825 xso
->so_pgid
= so
->so_pgid
;
2826 xso
->so_oobmark
= so
->so_oobmark
;
2827 sbtoxsockbuf(&so
->so_snd
, &xso
->so_snd
);
2828 sbtoxsockbuf(&so
->so_rcv
, &xso
->so_rcv
);
2829 xso
->so_uid
= kauth_cred_getuid(so
->so_cred
);
2834 * This does the same for sockbufs. Note that the xsockbuf structure,
2835 * since it is always embedded in a socket, does not include a self
2836 * pointer nor a length. We make this entry point public in case
2837 * some other mechanism needs it.
2840 sbtoxsockbuf(struct sockbuf
*sb
, struct xsockbuf
*xsb
)
2842 xsb
->sb_cc
= sb
->sb_cc
;
2843 xsb
->sb_hiwat
= sb
->sb_hiwat
;
2844 xsb
->sb_mbcnt
= sb
->sb_mbcnt
;
2845 xsb
->sb_mbmax
= sb
->sb_mbmax
;
2846 xsb
->sb_lowat
= sb
->sb_lowat
;
2847 xsb
->sb_flags
= sb
->sb_flags
;
2848 xsb
->sb_timeo
= (short)
2849 (sb
->sb_timeo
.tv_sec
* hz
) + sb
->sb_timeo
.tv_usec
/ tick
;
2850 if (xsb
->sb_timeo
== 0 && sb
->sb_timeo
.tv_usec
!= 0)
2855 * Based on the policy set by an all knowing decison maker, throttle sockets
2856 * that either have been marked as belonging to "background" process.
2859 soisthrottled(struct socket
*so
)
2862 * On non-embedded, we rely on implicit throttling by the
2863 * application, as we're missing the system wide "decision maker"
2866 (so
->so_traffic_mgt_flags
& TRAFFIC_MGT_SO_BACKGROUND
));
2870 soisprivilegedtraffic(struct socket
*so
)
2872 return ((so
->so_flags
& SOF_PRIVILEGED_TRAFFIC_CLASS
) ? 1 : 0);
2876 soissrcbackground(struct socket
*so
)
2878 return ((so
->so_traffic_mgt_flags
& TRAFFIC_MGT_SO_BACKGROUND
) ||
2879 IS_SO_TC_BACKGROUND(so
->so_traffic_class
));
2883 soissrcrealtime(struct socket
*so
)
2885 return (so
->so_traffic_class
>= SO_TC_AV
&&
2886 so
->so_traffic_class
<= SO_TC_VO
);
2890 soissrcbesteffort(struct socket
*so
)
2892 return (so
->so_traffic_class
== SO_TC_BE
||
2893 so
->so_traffic_class
== SO_TC_RD
||
2894 so
->so_traffic_class
== SO_TC_OAM
);
2898 sonullevent(struct socket
*so
, void *arg
, uint32_t hint
)
2900 #pragma unused(so, arg, hint)
2904 * Here is the definition of some of the basic objects in the kern.ipc
2905 * branch of the MIB.
2907 SYSCTL_NODE(_kern
, KERN_IPC
, ipc
,
2908 CTLFLAG_RW
|CTLFLAG_LOCKED
|CTLFLAG_ANYBODY
, 0, "IPC");
2910 /* Check that the maximum socket buffer size is within a range */
2913 sysctl_sb_max SYSCTL_HANDLER_ARGS
2915 #pragma unused(oidp, arg1, arg2)
2916 u_int32_t new_value
;
2918 int error
= sysctl_io_number(req
, sb_max
, sizeof (u_int32_t
),
2919 &new_value
, &changed
);
2920 if (!error
&& changed
) {
2921 if (new_value
> LOW_SB_MAX
&& new_value
<= high_sb_max
) {
2931 sysctl_io_policy_throttled SYSCTL_HANDLER_ARGS
2933 #pragma unused(arg1, arg2)
2936 i
= net_io_policy_throttled
;
2938 err
= sysctl_handle_int(oidp
, &i
, 0, req
);
2939 if (err
!= 0 || req
->newptr
== USER_ADDR_NULL
)
2942 if (i
!= net_io_policy_throttled
)
2943 SOTHROTTLELOG(("throttle: network IO policy throttling is "
2944 "now %s\n", i
? "ON" : "OFF"));
2946 net_io_policy_throttled
= i
;
2951 SYSCTL_PROC(_kern_ipc
, KIPC_MAXSOCKBUF
, maxsockbuf
,
2952 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
2953 &sb_max
, 0, &sysctl_sb_max
, "IU", "Maximum socket buffer size");
2955 SYSCTL_INT(_kern_ipc
, KIPC_SOCKBUF_WASTE
, sockbuf_waste_factor
,
2956 CTLFLAG_RW
| CTLFLAG_LOCKED
, &sb_efficiency
, 0, "");
2958 SYSCTL_INT(_kern_ipc
, KIPC_NMBCLUSTERS
, nmbclusters
,
2959 CTLFLAG_RD
| CTLFLAG_LOCKED
, &nmbclusters
, 0, "");
2961 SYSCTL_INT(_kern_ipc
, OID_AUTO
, njcl
,
2962 CTLFLAG_RD
| CTLFLAG_LOCKED
, &njcl
, 0, "");
2964 SYSCTL_INT(_kern_ipc
, OID_AUTO
, njclbytes
,
2965 CTLFLAG_RD
| CTLFLAG_LOCKED
, &njclbytes
, 0, "");
2967 SYSCTL_INT(_kern_ipc
, KIPC_SOQLIMITCOMPAT
, soqlimitcompat
,
2968 CTLFLAG_RW
| CTLFLAG_LOCKED
, &soqlimitcompat
, 1,
2969 "Enable socket queue limit compatibility");
2971 SYSCTL_INT(_kern_ipc
, OID_AUTO
, soqlencomp
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
2972 &soqlencomp
, 0, "Listen backlog represents only complete queue");
2974 SYSCTL_NODE(_kern_ipc
, OID_AUTO
, io_policy
, CTLFLAG_RW
, 0, "network IO policy");
2976 SYSCTL_PROC(_kern_ipc_io_policy
, OID_AUTO
, throttled
,
2977 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &net_io_policy_throttled
, 0,
2978 sysctl_io_policy_throttled
, "I", "");
2980 SYSCTL_INT(_kern_ipc_io_policy
, OID_AUTO
, log
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
2981 &net_io_policy_log
, 0, "");
2983 #if CONFIG_PROC_UUID_POLICY
2984 SYSCTL_INT(_kern_ipc_io_policy
, OID_AUTO
, uuid
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
2985 &net_io_policy_uuid
, 0, "");
2986 #endif /* CONFIG_PROC_UUID_POLICY */