2 * Copyright (c) 1998-2012 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
62 * $FreeBSD: src/sys/kern/uipc_socket2.c,v 1.55.2.9 2001/07/26 18:53:02 peter Exp $
65 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
66 * support for mandatory and extensible security protections. This notice
67 * is included in support of clause 2.2 (b) of the Apple Public License,
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/domain.h>
74 #include <sys/kernel.h>
75 #include <sys/proc_internal.h>
76 #include <sys/kauth.h>
77 #include <sys/malloc.h>
79 #include <sys/mcache.h>
80 #include <sys/protosw.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/signalvar.h>
85 #include <sys/sysctl.h>
87 #include <kern/locks.h>
88 #include <net/route.h>
89 #include <netinet/in.h>
90 #include <netinet/in_pcb.h>
91 #include <sys/kdebug.h>
92 #include <libkern/OSAtomic.h>
95 #include <security/mac_framework.h>
98 #include <mach/vm_param.h>
100 /* TODO: this should be in a header file somewhere */
101 extern void postevent(struct socket
*, struct sockbuf
*, int);
103 #define DBG_FNC_SBDROP NETDBG_CODE(DBG_NETSOCK, 4)
104 #define DBG_FNC_SBAPPEND NETDBG_CODE(DBG_NETSOCK, 5)
106 static inline void sbcompress(struct sockbuf
*, struct mbuf
*, struct mbuf
*);
107 static struct socket
*sonewconn_internal(struct socket
*, int);
108 static int sbappendaddr_internal(struct sockbuf
*, struct sockaddr
*,
109 struct mbuf
*, struct mbuf
*);
110 static int sbappendcontrol_internal(struct sockbuf
*, struct mbuf
*,
114 * Primitive routines for operating on sockets and socket buffers
116 static int soqlimitcompat
= 1;
117 static int soqlencomp
= 0;
119 /* Based on the number of mbuf clusters configured, high_sb_max and sb_max can get
120 * scaled up or down to suit that memory configuration. high_sb_max is a higher
121 * limit on sb_max that is checked when sb_max gets set through sysctl.
124 u_int32_t sb_max
= SB_MAX
; /* XXX should be static */
125 u_int32_t high_sb_max
= SB_MAX
;
127 static u_int32_t sb_efficiency
= 8; /* parameter for sbreserve() */
128 __private_extern__
int32_t total_sbmb_cnt
= 0;
130 /* Control whether to throttle sockets eligible to be throttled */
131 __private_extern__ u_int32_t net_io_policy_throttled
= 0;
132 static int sysctl_io_policy_throttled SYSCTL_HANDLER_ARGS
;
135 * Procedures to manipulate state flags of socket
136 * and do appropriate wakeups. Normal sequence from the
137 * active (originating) side is that soisconnecting() is
138 * called during processing of connect() call,
139 * resulting in an eventual call to soisconnected() if/when the
140 * connection is established. When the connection is torn down
141 * soisdisconnecting() is called during processing of disconnect() call,
142 * and soisdisconnected() is called when the connection to the peer
143 * is totally severed. The semantics of these routines are such that
144 * connectionless protocols can call soisconnected() and soisdisconnected()
145 * only, bypassing the in-progress calls when setting up a ``connection''
148 * From the passive side, a socket is created with
149 * two queues of sockets: so_incomp for connections in progress
150 * and so_comp for connections already made and awaiting user acceptance.
151 * As a protocol is preparing incoming connections, it creates a socket
152 * structure queued on so_incomp by calling sonewconn(). When the connection
153 * is established, soisconnected() is called, and transfers the
154 * socket structure to so_comp, making it available to accept().
156 * If a socket is closed with sockets on either
157 * so_incomp or so_comp, these sockets are dropped.
159 * If higher level protocols are implemented in
160 * the kernel, the wakeups done here will sometimes
161 * cause software-interrupt process scheduling.
164 soisconnecting(struct socket
*so
)
167 so
->so_state
&= ~(SS_ISCONNECTED
|SS_ISDISCONNECTING
);
168 so
->so_state
|= SS_ISCONNECTING
;
170 sflt_notify(so
, sock_evt_connecting
, NULL
);
174 soisconnected(struct socket
*so
)
176 struct socket
*head
= so
->so_head
;
178 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISDISCONNECTING
|SS_ISCONFIRMING
);
179 so
->so_state
|= SS_ISCONNECTED
;
181 sflt_notify(so
, sock_evt_connected
, NULL
);
183 if (head
&& (so
->so_state
& SS_INCOMP
)) {
184 so
->so_state
&= ~SS_INCOMP
;
185 so
->so_state
|= SS_COMP
;
186 if (head
->so_proto
->pr_getlock
!= NULL
) {
187 socket_unlock(so
, 0);
188 socket_lock(head
, 1);
190 postevent(head
, 0, EV_RCONN
);
191 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
193 TAILQ_INSERT_TAIL(&head
->so_comp
, so
, so_list
);
195 wakeup_one((caddr_t
)&head
->so_timeo
);
196 if (head
->so_proto
->pr_getlock
!= NULL
) {
197 socket_unlock(head
, 1);
201 postevent(so
, 0, EV_WCONN
);
202 wakeup((caddr_t
)&so
->so_timeo
);
205 soevent(so
, SO_FILT_HINT_LOCKED
);
210 soisdisconnecting(struct socket
*so
)
212 so
->so_state
&= ~SS_ISCONNECTING
;
213 so
->so_state
|= (SS_ISDISCONNECTING
|SS_CANTRCVMORE
|SS_CANTSENDMORE
);
214 soevent(so
, SO_FILT_HINT_LOCKED
);
215 sflt_notify(so
, sock_evt_disconnecting
, NULL
);
216 wakeup((caddr_t
)&so
->so_timeo
);
222 soisdisconnected(struct socket
*so
)
224 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
225 so
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
);
226 soevent(so
, SO_FILT_HINT_LOCKED
);
227 sflt_notify(so
, sock_evt_disconnected
, NULL
);
228 wakeup((caddr_t
)&so
->so_timeo
);
233 /* This function will issue a wakeup like soisdisconnected but it will not
234 * notify the socket filters. This will avoid unlocking the socket
235 * in the midst of closing it.
238 sodisconnectwakeup(struct socket
*so
)
240 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
241 so
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
);
242 soevent(so
, SO_FILT_HINT_LOCKED
);
243 wakeup((caddr_t
)&so
->so_timeo
);
249 * When an attempt at a new connection is noted on a socket
250 * which accepts connections, sonewconn is called. If the
251 * connection is possible (subject to space constraints, etc.)
252 * then we allocate a new structure, propoerly linked into the
253 * data structure of the original socket, and return this.
254 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
256 static struct socket
*
257 sonewconn_internal(struct socket
*head
, int connstatus
)
259 int so_qlen
, error
= 0;
261 lck_mtx_t
*mutex_held
;
263 if (head
->so_proto
->pr_getlock
!= NULL
)
264 mutex_held
= (*head
->so_proto
->pr_getlock
)(head
, 0);
266 mutex_held
= head
->so_proto
->pr_domain
->dom_mtx
;
267 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
271 * This is the default case; so_qlen represents the
272 * sum of both incomplete and completed queues.
274 so_qlen
= head
->so_qlen
;
277 * When kern.ipc.soqlencomp is set to 1, so_qlen
278 * represents only the completed queue. Since we
279 * cannot let the incomplete queue goes unbounded
280 * (in case of SYN flood), we cap the incomplete
281 * queue length to at most somaxconn, and use that
282 * as so_qlen so that we fail immediately below.
284 so_qlen
= head
->so_qlen
- head
->so_incqlen
;
285 if (head
->so_incqlen
> somaxconn
)
290 (soqlimitcompat
? head
->so_qlimit
: (3 * head
->so_qlimit
/ 2)))
291 return ((struct socket
*)0);
292 so
= soalloc(1, head
->so_proto
->pr_domain
->dom_family
,
295 return ((struct socket
*)0);
296 /* check if head was closed during the soalloc */
297 if (head
->so_proto
== NULL
) {
299 return ((struct socket
*)0);
302 so
->so_type
= head
->so_type
;
303 so
->so_options
= head
->so_options
&~ SO_ACCEPTCONN
;
304 so
->so_linger
= head
->so_linger
;
305 so
->so_state
= head
->so_state
| SS_NOFDREF
;
306 so
->so_proto
= head
->so_proto
;
307 so
->so_timeo
= head
->so_timeo
;
308 so
->so_pgid
= head
->so_pgid
;
309 kauth_cred_ref(head
->so_cred
);
310 so
->so_cred
= head
->so_cred
;
311 so
->last_pid
= head
->last_pid
;
312 so
->last_upid
= head
->last_upid
;
313 /* inherit socket options stored in so_flags */
314 so
->so_flags
= head
->so_flags
& (SOF_NOSIGPIPE
|
321 SOF_PRIVILEGED_TRAFFIC_CLASS
|
325 so
->next_lock_lr
= 0;
326 so
->next_unlock_lr
= 0;
329 so
->so_rcv
.sb_flags
|= SB_RECV
; /* XXX */
330 so
->so_rcv
.sb_so
= so
->so_snd
.sb_so
= so
;
331 TAILQ_INIT(&so
->so_evlist
);
334 #if CONFIG_MACF_SOCKET
335 mac_socket_label_associate_accept(head
, so
);
338 /* inherit traffic management properties of listener */
339 so
->so_traffic_mgt_flags
= head
->so_traffic_mgt_flags
& (TRAFFIC_MGT_SO_BACKGROUND
);
340 so
->so_background_thread
= head
->so_background_thread
;
341 so
->so_traffic_class
= head
->so_traffic_class
;
343 if (soreserve(so
, head
->so_snd
.sb_hiwat
, head
->so_rcv
.sb_hiwat
)) {
345 return ((struct socket
*)0);
347 so
->so_rcv
.sb_flags
|= (head
->so_rcv
.sb_flags
& SB_USRSIZE
);
348 so
->so_snd
.sb_flags
|= (head
->so_snd
.sb_flags
& SB_USRSIZE
);
351 * Must be done with head unlocked to avoid deadlock
352 * for protocol with per socket mutexes.
354 if (head
->so_proto
->pr_unlock
)
355 socket_unlock(head
, 0);
356 if (((*so
->so_proto
->pr_usrreqs
->pru_attach
)(so
, 0, NULL
) != 0) ||
359 if (head
->so_proto
->pr_unlock
)
360 socket_lock(head
, 0);
361 return ((struct socket
*)0);
363 if (head
->so_proto
->pr_unlock
) {
364 socket_lock(head
, 0);
365 /* Radar 7385998 Recheck that the head is still accepting
366 * to avoid race condition when head is getting closed.
368 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
369 so
->so_state
&= ~SS_NOFDREF
;
371 return ((struct socket
*)0);
376 so
->so_proto
->pr_domain
->dom_refs
++;
378 /* Insert in head appropriate lists */
381 /* Since this socket is going to be inserted into the incomp
382 * queue, it can be picked up by another thread in
383 * tcp_dropdropablreq to get dropped before it is setup..
384 * To prevent this race, set in-progress flag which can be
387 so
->so_flags
|= SOF_INCOMP_INPROGRESS
;
390 TAILQ_INSERT_TAIL(&head
->so_comp
, so
, so_list
);
391 so
->so_state
|= SS_COMP
;
393 TAILQ_INSERT_TAIL(&head
->so_incomp
, so
, so_list
);
394 so
->so_state
|= SS_INCOMP
;
400 /* Attach socket filters for this protocol */
405 so
->so_state
|= connstatus
;
407 wakeup((caddr_t
)&head
->so_timeo
);
414 sonewconn(struct socket
*head
, int connstatus
, const struct sockaddr
*from
)
416 int error
= sflt_connectin(head
, from
);
421 return (sonewconn_internal(head
, connstatus
));
425 * Socantsendmore indicates that no more data will be sent on the
426 * socket; it would normally be applied to a socket when the user
427 * informs the system that no more data is to be sent, by the protocol
428 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
429 * will be received, and will normally be applied to the socket by a
430 * protocol when it detects that the peer will send no more data.
431 * Data queued for reading in the socket may yet be read.
435 socantsendmore(struct socket
*so
)
437 so
->so_state
|= SS_CANTSENDMORE
;
438 soevent(so
, SO_FILT_HINT_LOCKED
);
439 sflt_notify(so
, sock_evt_cantsendmore
, NULL
);
444 socantrcvmore(struct socket
*so
)
446 so
->so_state
|= SS_CANTRCVMORE
;
447 soevent(so
, SO_FILT_HINT_LOCKED
);
448 sflt_notify(so
, sock_evt_cantrecvmore
, NULL
);
453 * Wait for data to arrive at/drain from a socket buffer.
460 sbwait(struct sockbuf
*sb
)
464 struct socket
*so
= sb
->sb_so
;
465 lck_mtx_t
*mutex_held
;
468 lr_saved
= (uintptr_t) __builtin_return_address(0);
470 if (so
->so_proto
->pr_getlock
!= NULL
)
471 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
473 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
474 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
476 sb
->sb_flags
|= SB_WAIT
;
478 if (so
->so_usecount
< 1)
479 panic("sbwait: so=%p refcount=%d\n", so
, so
->so_usecount
);
480 ts
.tv_sec
= sb
->sb_timeo
.tv_sec
;
481 ts
.tv_nsec
= sb
->sb_timeo
.tv_usec
* 1000;
482 error
= msleep((caddr_t
)&sb
->sb_cc
, mutex_held
,
483 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
| PCATCH
, "sbwait", &ts
);
485 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
487 if (so
->so_usecount
< 1)
488 panic("sbwait: so=%p refcount=%d\n", so
, so
->so_usecount
);
490 if ((so
->so_state
& SS_DRAINING
) || (so
->so_flags
& SOF_DEFUNCT
)) {
492 if (so
->so_flags
& SOF_DEFUNCT
) {
493 SODEFUNCTLOG(("%s[%d]: defunct so %p [%d,%d] (%d)\n",
494 __func__
, proc_selfpid(), so
, INP_SOCKAF(so
),
495 INP_SOCKTYPE(so
), error
));
503 * Lock a sockbuf already known to be locked;
504 * return any error returned from sleep (EINTR).
510 sb_lock(struct sockbuf
*sb
)
512 struct socket
*so
= sb
->sb_so
;
513 lck_mtx_t
*mutex_held
;
517 panic("sb_lock: null so back pointer sb=%p\n", sb
);
519 while (sb
->sb_flags
& SB_LOCK
) {
520 sb
->sb_flags
|= SB_WANT
;
522 if (so
->so_proto
->pr_getlock
!= NULL
)
523 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
525 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
526 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
528 if (so
->so_usecount
< 1)
529 panic("sb_lock: so=%p refcount=%d\n", so
,
532 error
= msleep((caddr_t
)&sb
->sb_flags
, mutex_held
,
533 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
| PCATCH
,
535 if (so
->so_usecount
< 1)
536 panic("sb_lock: 2 so=%p refcount=%d\n", so
,
539 if (error
== 0 && (so
->so_flags
& SOF_DEFUNCT
)) {
541 SODEFUNCTLOG(("%s[%d]: defunct so %p [%d,%d] (%d)\n",
542 __func__
, proc_selfpid(), so
, INP_SOCKAF(so
),
543 INP_SOCKTYPE(so
), error
));
549 sb
->sb_flags
|= SB_LOCK
;
554 sbwakeup(struct sockbuf
*sb
)
556 if (sb
->sb_flags
& SB_WAIT
) {
557 sb
->sb_flags
&= ~SB_WAIT
;
558 wakeup((caddr_t
)&sb
->sb_cc
);
563 * Wakeup processes waiting on a socket buffer.
564 * Do asynchronous notification via SIGIO
565 * if the socket has the SS_ASYNC flag set.
568 sowakeup(struct socket
*so
, struct sockbuf
*sb
)
570 if (so
->so_flags
& SOF_DEFUNCT
) {
571 SODEFUNCTLOG(("%s[%d]: defunct so %p [%d,%d] si 0x%x, "
572 "fl 0x%x [%s]\n", __func__
, proc_selfpid(), so
,
573 INP_SOCKAF(so
), INP_SOCKTYPE(so
),
574 (uint32_t)sb
->sb_sel
.si_flags
, (uint16_t)sb
->sb_flags
,
575 (sb
->sb_flags
& SB_RECV
) ? "rcv" : "snd"));
578 sb
->sb_flags
&= ~SB_SEL
;
579 selwakeup(&sb
->sb_sel
);
581 if (so
->so_state
& SS_ASYNC
) {
583 gsignal(-so
->so_pgid
, SIGIO
);
584 else if (so
->so_pgid
> 0)
585 proc_signal(so
->so_pgid
, SIGIO
);
587 if (sb
->sb_flags
& SB_KNOTE
) {
588 KNOTE(&sb
->sb_sel
.si_note
, SO_FILT_HINT_LOCKED
);
590 if (sb
->sb_flags
& SB_UPCALL
) {
591 void (*so_upcall
)(struct socket
*, caddr_t
, int);
592 caddr_t so_upcallarg
;
594 so_upcall
= so
->so_upcall
;
595 so_upcallarg
= so
->so_upcallarg
;
596 /* Let close know that we're about to do an upcall */
597 so
->so_upcallusecount
++;
599 socket_unlock(so
, 0);
600 (*so_upcall
)(so
, so_upcallarg
, M_DONTWAIT
);
603 so
->so_upcallusecount
--;
604 /* Tell close that it's safe to proceed */
605 if (so
->so_flags
& SOF_CLOSEWAIT
&& so
->so_upcallusecount
== 0)
606 wakeup((caddr_t
)&so
->so_upcall
);
611 * Socket buffer (struct sockbuf) utility routines.
613 * Each socket contains two socket buffers: one for sending data and
614 * one for receiving data. Each buffer contains a queue of mbufs,
615 * information about the number of mbufs and amount of data in the
616 * queue, and other fields allowing select() statements and notification
617 * on data availability to be implemented.
619 * Data stored in a socket buffer is maintained as a list of records.
620 * Each record is a list of mbufs chained together with the m_next
621 * field. Records are chained together with the m_nextpkt field. The upper
622 * level routine soreceive() expects the following conventions to be
623 * observed when placing information in the receive buffer:
625 * 1. If the protocol requires each message be preceded by the sender's
626 * name, then a record containing that name must be present before
627 * any associated data (mbuf's must be of type MT_SONAME).
628 * 2. If the protocol supports the exchange of ``access rights'' (really
629 * just additional data associated with the message), and there are
630 * ``rights'' to be received, then a record containing this data
631 * should be present (mbuf's must be of type MT_RIGHTS).
632 * 3. If a name or rights record exists, then it must be followed by
633 * a data record, perhaps of zero length.
635 * Before using a new socket structure it is first necessary to reserve
636 * buffer space to the socket, by calling sbreserve(). This should commit
637 * some of the available buffer space in the system buffer pool for the
638 * socket (currently, it does nothing but enforce limits). The space
639 * should be released by calling sbrelease() when the socket is destroyed.
647 soreserve(struct socket
*so
, u_int32_t sndcc
, u_int32_t rcvcc
)
650 if (sbreserve(&so
->so_snd
, sndcc
) == 0)
653 so
->so_snd
.sb_idealsize
= sndcc
;
655 if (sbreserve(&so
->so_rcv
, rcvcc
) == 0)
658 so
->so_rcv
.sb_idealsize
= rcvcc
;
660 if (so
->so_rcv
.sb_lowat
== 0)
661 so
->so_rcv
.sb_lowat
= 1;
662 if (so
->so_snd
.sb_lowat
== 0)
663 so
->so_snd
.sb_lowat
= MCLBYTES
;
664 if (so
->so_snd
.sb_lowat
> so
->so_snd
.sb_hiwat
)
665 so
->so_snd
.sb_lowat
= so
->so_snd
.sb_hiwat
;
669 selthreadclear(&so
->so_snd
.sb_sel
);
671 sbrelease(&so
->so_snd
);
677 * Allot mbufs to a sockbuf.
678 * Attempt to scale mbmax so that mbcnt doesn't become limiting
679 * if buffering efficiency is near the normal case.
682 sbreserve(struct sockbuf
*sb
, u_int32_t cc
)
684 if ((u_quad_t
)cc
> (u_quad_t
)sb_max
* MCLBYTES
/ (MSIZE
+ MCLBYTES
))
687 sb
->sb_mbmax
= min(cc
* sb_efficiency
, sb_max
);
688 if (sb
->sb_lowat
> sb
->sb_hiwat
)
689 sb
->sb_lowat
= sb
->sb_hiwat
;
694 * Free mbufs held by a socket, and reserved mbuf space.
696 /* WARNING needs to do selthreadclear() before calling this */
698 sbrelease(struct sockbuf
*sb
)
706 * Routines to add and remove
707 * data from an mbuf queue.
709 * The routines sbappend() or sbappendrecord() are normally called to
710 * append new mbufs to a socket buffer, after checking that adequate
711 * space is available, comparing the function sbspace() with the amount
712 * of data to be added. sbappendrecord() differs from sbappend() in
713 * that data supplied is treated as the beginning of a new record.
714 * To place a sender's address, optional access rights, and data in a
715 * socket receive buffer, sbappendaddr() should be used. To place
716 * access rights and data in a socket receive buffer, sbappendrights()
717 * should be used. In either case, the new data begins a new record.
718 * Note that unlike sbappend() and sbappendrecord(), these routines check
719 * for the caller that there will be enough space to store the data.
720 * Each fails if there is not enough space, or if it cannot find mbufs
721 * to store additional information in.
723 * Reliable protocols may use the socket send buffer to hold data
724 * awaiting acknowledgement. Data is normally copied from a socket
725 * send buffer in a protocol with m_copy for output to a peer,
726 * and then removing the data from the socket buffer with sbdrop()
727 * or sbdroprecord() when the data is acknowledged by the peer.
731 * Append mbuf chain m to the last record in the
732 * socket buffer sb. The additional space associated
733 * the mbuf chain is recorded in sb. Empty mbufs are
734 * discarded and mbufs are compacted where possible.
737 sbappend(struct sockbuf
*sb
, struct mbuf
*m
)
739 struct socket
*so
= sb
->sb_so
;
741 if (m
== NULL
|| (sb
->sb_flags
& SB_DROP
)) {
747 SBLASTRECORDCHK(sb
, "sbappend 1");
749 if (sb
->sb_lastrecord
!= NULL
&& (sb
->sb_mbtail
->m_flags
& M_EOR
))
750 return (sbappendrecord(sb
, m
));
752 if (sb
->sb_flags
& SB_RECV
) {
753 int error
= sflt_data_in(so
, NULL
, &m
, NULL
, 0);
754 SBLASTRECORDCHK(sb
, "sbappend 2");
756 if (error
!= EJUSTRETURN
)
762 /* If this is the first record, it's also the last record */
763 if (sb
->sb_lastrecord
== NULL
)
764 sb
->sb_lastrecord
= m
;
766 sbcompress(sb
, m
, sb
->sb_mbtail
);
767 SBLASTRECORDCHK(sb
, "sbappend 3");
772 * Similar to sbappend, except that this is optimized for stream sockets.
775 sbappendstream(struct sockbuf
*sb
, struct mbuf
*m
)
777 struct socket
*so
= sb
->sb_so
;
779 if (m
->m_nextpkt
!= NULL
|| (sb
->sb_mb
!= sb
->sb_lastrecord
))
780 panic("sbappendstream: nexpkt %p || mb %p != lastrecord %p\n",
781 m
->m_nextpkt
, sb
->sb_mb
, sb
->sb_lastrecord
);
783 SBLASTMBUFCHK(sb
, __func__
);
785 if (m
== NULL
|| (sb
->sb_flags
& SB_DROP
)) {
791 if (sb
->sb_flags
& SB_RECV
) {
792 int error
= sflt_data_in(so
, NULL
, &m
, NULL
, 0);
793 SBLASTRECORDCHK(sb
, "sbappendstream 1");
795 if (error
!= EJUSTRETURN
)
801 sbcompress(sb
, m
, sb
->sb_mbtail
);
802 sb
->sb_lastrecord
= sb
->sb_mb
;
803 SBLASTRECORDCHK(sb
, "sbappendstream 2");
809 sbcheck(struct sockbuf
*sb
)
813 u_int32_t len
= 0, mbcnt
= 0;
814 lck_mtx_t
*mutex_held
;
816 if (sb
->sb_so
->so_proto
->pr_getlock
!= NULL
)
817 mutex_held
= (*sb
->sb_so
->so_proto
->pr_getlock
)(sb
->sb_so
, 0);
819 mutex_held
= sb
->sb_so
->so_proto
->pr_domain
->dom_mtx
;
821 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
826 for (m
= sb
->sb_mb
; m
; m
= n
) {
828 for (; m
; m
= m
->m_next
) {
831 /* XXX pretty sure this is bogus */
832 if (m
->m_flags
& M_EXT
)
833 mbcnt
+= m
->m_ext
.ext_size
;
836 if (len
!= sb
->sb_cc
|| mbcnt
!= sb
->sb_mbcnt
) {
837 panic("cc %ld != %ld || mbcnt %ld != %ld\n", len
, sb
->sb_cc
,
838 mbcnt
, sb
->sb_mbcnt
);
844 sblastrecordchk(struct sockbuf
*sb
, const char *where
)
846 struct mbuf
*m
= sb
->sb_mb
;
848 while (m
&& m
->m_nextpkt
)
851 if (m
!= sb
->sb_lastrecord
) {
852 printf("sblastrecordchk: mb %p lastrecord %p last %p\n",
853 sb
->sb_mb
, sb
->sb_lastrecord
, m
);
854 printf("packet chain:\n");
855 for (m
= sb
->sb_mb
; m
!= NULL
; m
= m
->m_nextpkt
)
857 panic("sblastrecordchk from %s", where
);
862 sblastmbufchk(struct sockbuf
*sb
, const char *where
)
864 struct mbuf
*m
= sb
->sb_mb
;
867 while (m
&& m
->m_nextpkt
)
870 while (m
&& m
->m_next
)
873 if (m
!= sb
->sb_mbtail
) {
874 printf("sblastmbufchk: mb %p mbtail %p last %p\n",
875 sb
->sb_mb
, sb
->sb_mbtail
, m
);
876 printf("packet tree:\n");
877 for (m
= sb
->sb_mb
; m
!= NULL
; m
= m
->m_nextpkt
) {
879 for (n
= m
; n
!= NULL
; n
= n
->m_next
)
883 panic("sblastmbufchk from %s", where
);
888 * Similar to sbappend, except the mbuf chain begins a new record.
891 sbappendrecord(struct sockbuf
*sb
, struct mbuf
*m0
)
896 if (m0
== NULL
|| (sb
->sb_flags
& SB_DROP
)) {
902 for (m
= m0
; m
!= NULL
; m
= m
->m_next
)
905 if (space
> sbspace(sb
) && !(sb
->sb_flags
& SB_UNIX
)) {
910 if (sb
->sb_flags
& SB_RECV
) {
911 int error
= sflt_data_in(sb
->sb_so
, NULL
, &m0
, NULL
,
912 sock_data_filt_flag_record
);
914 SBLASTRECORDCHK(sb
, "sbappendrecord 1");
915 if (error
!= EJUSTRETURN
)
922 * Note this permits zero length records.
925 SBLASTRECORDCHK(sb
, "sbappendrecord 2");
926 if (sb
->sb_lastrecord
!= NULL
) {
927 sb
->sb_lastrecord
->m_nextpkt
= m0
;
931 sb
->sb_lastrecord
= m0
;
936 if (m
&& (m0
->m_flags
& M_EOR
)) {
937 m0
->m_flags
&= ~M_EOR
;
940 sbcompress(sb
, m
, m0
);
941 SBLASTRECORDCHK(sb
, "sbappendrecord 3");
946 * As above except that OOB data
947 * is inserted at the beginning of the sockbuf,
948 * but after any other OOB data.
951 sbinsertoob(struct sockbuf
*sb
, struct mbuf
*m0
)
959 SBLASTRECORDCHK(sb
, "sbinsertoob 1");
961 if ((sb
->sb_flags
& SB_RECV
) != 0) {
962 int error
= sflt_data_in(sb
->sb_so
, NULL
, &m0
, NULL
,
963 sock_data_filt_flag_oob
);
965 SBLASTRECORDCHK(sb
, "sbinsertoob 2");
967 if (error
!= EJUSTRETURN
) {
974 for (mp
= &sb
->sb_mb
; *mp
; mp
= &((*mp
)->m_nextpkt
)) {
980 continue; /* WANT next train */
985 goto again
; /* inspect THIS train further */
990 * Put the first mbuf on the queue.
991 * Note this permits zero length records.
996 /* m0 is actually the new tail */
997 sb
->sb_lastrecord
= m0
;
1002 if (m
&& (m0
->m_flags
& M_EOR
)) {
1003 m0
->m_flags
&= ~M_EOR
;
1004 m
->m_flags
|= M_EOR
;
1006 sbcompress(sb
, m
, m0
);
1007 SBLASTRECORDCHK(sb
, "sbinsertoob 3");
1012 * Append address and data, and optionally, control (ancillary) data
1013 * to the receive queue of a socket. If present,
1014 * m0 must include a packet header with total length.
1015 * Returns 0 if no space in sockbuf or insufficient mbufs.
1017 * Returns: 0 No space/out of mbufs
1021 sbappendaddr_internal(struct sockbuf
*sb
, struct sockaddr
*asa
,
1022 struct mbuf
*m0
, struct mbuf
*control
)
1024 struct mbuf
*m
, *n
, *nlast
;
1025 int space
= asa
->sa_len
;
1027 if (m0
&& (m0
->m_flags
& M_PKTHDR
) == 0)
1028 panic("sbappendaddr");
1031 space
+= m0
->m_pkthdr
.len
;
1032 for (n
= control
; n
; n
= n
->m_next
) {
1034 if (n
->m_next
== 0) /* keep pointer to last control buf */
1037 if (space
> sbspace(sb
))
1039 if (asa
->sa_len
> MLEN
)
1041 MGET(m
, M_DONTWAIT
, MT_SONAME
);
1044 m
->m_len
= asa
->sa_len
;
1045 bcopy((caddr_t
)asa
, mtod(m
, caddr_t
), asa
->sa_len
);
1047 n
->m_next
= m0
; /* concatenate data to control */
1050 m
->m_next
= control
;
1052 SBLASTRECORDCHK(sb
, "sbappendadddr 1");
1054 for (n
= m
; n
->m_next
!= NULL
; n
= n
->m_next
)
1059 if (sb
->sb_lastrecord
!= NULL
) {
1060 sb
->sb_lastrecord
->m_nextpkt
= m
;
1064 sb
->sb_lastrecord
= m
;
1065 sb
->sb_mbtail
= nlast
;
1067 SBLASTMBUFCHK(sb
, __func__
);
1068 SBLASTRECORDCHK(sb
, "sbappendadddr 2");
1070 postevent(0, sb
, EV_RWBYTES
);
1075 * Returns: 0 Error: No space/out of mbufs/etc.
1078 * Imputed: (*error_out) errno for error
1080 * sflt_data_in:??? [whatever a filter author chooses]
1083 sbappendaddr(struct sockbuf
*sb
, struct sockaddr
*asa
, struct mbuf
*m0
,
1084 struct mbuf
*control
, int *error_out
)
1087 boolean_t sb_unix
= (sb
->sb_flags
& SB_UNIX
);
1092 if (m0
&& (m0
->m_flags
& M_PKTHDR
) == 0)
1093 panic("sbappendaddrorfree");
1095 if (sb
->sb_flags
& SB_DROP
) {
1098 if (control
!= NULL
&& !sb_unix
)
1100 if (error_out
!= NULL
)
1101 *error_out
= EINVAL
;
1105 /* Call socket data in filters */
1106 if ((sb
->sb_flags
& SB_RECV
) != 0) {
1108 error
= sflt_data_in(sb
->sb_so
, asa
, &m0
, &control
, 0);
1109 SBLASTRECORDCHK(sb
, __func__
);
1111 if (error
!= EJUSTRETURN
) {
1114 if (control
!= NULL
&& !sb_unix
)
1123 result
= sbappendaddr_internal(sb
, asa
, m0
, control
);
1127 if (control
!= NULL
&& !sb_unix
)
1130 *error_out
= ENOBUFS
;
1137 sbappendcontrol_internal(struct sockbuf
*sb
, struct mbuf
*m0
,
1138 struct mbuf
*control
)
1140 struct mbuf
*m
, *mlast
, *n
;
1144 panic("sbappendcontrol");
1146 for (m
= control
; ; m
= m
->m_next
) {
1151 n
= m
; /* save pointer to last control buffer */
1152 for (m
= m0
; m
; m
= m
->m_next
)
1154 if (space
> sbspace(sb
) && !(sb
->sb_flags
& SB_UNIX
))
1156 n
->m_next
= m0
; /* concatenate data to control */
1158 SBLASTRECORDCHK(sb
, "sbappendcontrol 1");
1160 for (m
= control
; m
->m_next
!= NULL
; m
= m
->m_next
)
1165 if (sb
->sb_lastrecord
!= NULL
) {
1166 sb
->sb_lastrecord
->m_nextpkt
= control
;
1168 sb
->sb_mb
= control
;
1170 sb
->sb_lastrecord
= control
;
1171 sb
->sb_mbtail
= mlast
;
1173 SBLASTMBUFCHK(sb
, __func__
);
1174 SBLASTRECORDCHK(sb
, "sbappendcontrol 2");
1176 postevent(0, sb
, EV_RWBYTES
);
1181 sbappendcontrol(struct sockbuf
*sb
, struct mbuf
*m0
, struct mbuf
*control
,
1185 boolean_t sb_unix
= (sb
->sb_flags
& SB_UNIX
);
1190 if (sb
->sb_flags
& SB_DROP
) {
1193 if (control
!= NULL
&& !sb_unix
)
1195 if (error_out
!= NULL
)
1196 *error_out
= EINVAL
;
1200 if (sb
->sb_flags
& SB_RECV
) {
1203 error
= sflt_data_in(sb
->sb_so
, NULL
, &m0
, &control
, 0);
1204 SBLASTRECORDCHK(sb
, __func__
);
1206 if (error
!= EJUSTRETURN
) {
1209 if (control
!= NULL
&& !sb_unix
)
1218 result
= sbappendcontrol_internal(sb
, m0
, control
);
1222 if (control
!= NULL
&& !sb_unix
)
1225 *error_out
= ENOBUFS
;
1232 * Compress mbuf chain m into the socket
1233 * buffer sb following mbuf n. If n
1234 * is null, the buffer is presumed empty.
1237 sbcompress(struct sockbuf
*sb
, struct mbuf
*m
, struct mbuf
*n
)
1243 /* There is nothing to compress; just update the tail */
1244 for (; n
->m_next
!= NULL
; n
= n
->m_next
)
1251 eor
|= m
->m_flags
& M_EOR
;
1252 if (m
->m_len
== 0 && (eor
== 0 ||
1253 (((o
= m
->m_next
) || (o
= n
)) && o
->m_type
== m
->m_type
))) {
1254 if (sb
->sb_lastrecord
== m
)
1255 sb
->sb_lastrecord
= m
->m_next
;
1259 if (n
&& (n
->m_flags
& M_EOR
) == 0 &&
1263 m
->m_len
<= MCLBYTES
/ 4 && /* XXX: Don't copy too much */
1264 m
->m_len
<= M_TRAILINGSPACE(n
) &&
1265 n
->m_type
== m
->m_type
) {
1266 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
) + n
->m_len
,
1267 (unsigned)m
->m_len
);
1268 n
->m_len
+= m
->m_len
;
1269 sb
->sb_cc
+= m
->m_len
;
1270 if (m
->m_type
!= MT_DATA
&& m
->m_type
!= MT_HEADER
&&
1271 m
->m_type
!= MT_OOBDATA
)
1272 /* XXX: Probably don't need.*/
1273 sb
->sb_ctl
+= m
->m_len
;
1284 m
->m_flags
&= ~M_EOR
;
1292 printf("semi-panic: sbcompress\n");
1295 SBLASTMBUFCHK(sb
, __func__
);
1296 postevent(0, sb
, EV_RWBYTES
);
1300 sb_empty_assert(struct sockbuf
*sb
, const char *where
)
1302 if (!(sb
->sb_cc
== 0 && sb
->sb_mb
== NULL
&& sb
->sb_mbcnt
== 0 &&
1303 sb
->sb_mbtail
== NULL
&& sb
->sb_lastrecord
== NULL
)) {
1304 panic("%s: sb %p so %p cc %d mbcnt %d mb %p mbtail %p "
1305 "lastrecord %p\n", where
, sb
, sb
->sb_so
, sb
->sb_cc
,
1306 sb
->sb_mbcnt
, sb
->sb_mb
, sb
->sb_mbtail
, sb
->sb_lastrecord
);
1312 * Free all mbufs in a sockbuf.
1313 * Check that all resources are reclaimed.
1316 sbflush(struct sockbuf
*sb
)
1318 if (sb
->sb_so
== NULL
)
1319 panic("sbflush sb->sb_so already null sb=%p\n", sb
);
1320 (void) sblock(sb
, M_WAIT
);
1321 while (sb
->sb_mbcnt
) {
1323 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
1324 * we would loop forever. Panic instead.
1326 if (!sb
->sb_cc
&& (sb
->sb_mb
== NULL
|| sb
->sb_mb
->m_len
))
1328 sbdrop(sb
, (int)sb
->sb_cc
);
1330 sb_empty_assert(sb
, __func__
);
1331 postevent(0, sb
, EV_RWBYTES
);
1332 sbunlock(sb
, 1); /* keep socket locked */
1337 * Drop data from (the front of) a sockbuf.
1338 * use m_freem_list to free the mbuf structures
1339 * under a single lock... this is done by pruning
1340 * the top of the tree from the body by keeping track
1341 * of where we get to in the tree and then zeroing the
1342 * two pertinent pointers m_nextpkt and m_next
1343 * the socket buffer is then updated to point at the new
1344 * top of the tree and the pruned area is released via
1348 sbdrop(struct sockbuf
*sb
, int len
)
1350 struct mbuf
*m
, *free_list
, *ml
;
1351 struct mbuf
*next
, *last
;
1353 KERNEL_DEBUG((DBG_FNC_SBDROP
| DBG_FUNC_START
), sb
, len
, 0, 0, 0);
1355 next
= (m
= sb
->sb_mb
) ? m
->m_nextpkt
: 0;
1356 free_list
= last
= m
;
1357 ml
= (struct mbuf
*)0;
1363 * temporarily replacing this panic with printf
1364 * because it occurs occasionally when closing
1365 * a socket when there is no harm in ignoring
1366 * it. This problem will be investigated
1369 /* panic("sbdrop"); */
1370 printf("sbdrop - count not zero\n");
1373 * zero the counts. if we have no mbufs,
1374 * we have no data (PR-2986815)
1381 next
= m
->m_nextpkt
;
1384 if (m
->m_len
> len
) {
1388 if (m
->m_type
!= MT_DATA
&& m
->m_type
!= MT_HEADER
&&
1389 m
->m_type
!= MT_OOBDATA
)
1399 while (m
&& m
->m_len
== 0) {
1406 ml
->m_next
= (struct mbuf
*)0;
1407 last
->m_nextpkt
= (struct mbuf
*)0;
1408 m_freem_list(free_list
);
1412 m
->m_nextpkt
= next
;
1418 * First part is an inline SB_EMPTY_FIXUP(). Second part
1419 * makes sure sb_lastrecord is up-to-date if we dropped
1420 * part of the last record.
1424 sb
->sb_mbtail
= NULL
;
1425 sb
->sb_lastrecord
= NULL
;
1426 } else if (m
->m_nextpkt
== NULL
) {
1427 sb
->sb_lastrecord
= m
;
1430 postevent(0, sb
, EV_RWBYTES
);
1432 KERNEL_DEBUG((DBG_FNC_SBDROP
| DBG_FUNC_END
), sb
, 0, 0, 0, 0);
1436 * Drop a record off the front of a sockbuf
1437 * and move the next record to the front.
1440 sbdroprecord(struct sockbuf
*sb
)
1442 struct mbuf
*m
, *mn
;
1446 sb
->sb_mb
= m
->m_nextpkt
;
1454 postevent(0, sb
, EV_RWBYTES
);
1458 * Create a "control" mbuf containing the specified data
1459 * with the specified type for presentation on a socket buffer.
1462 sbcreatecontrol(caddr_t p
, int size
, int type
, int level
)
1467 if (CMSG_SPACE((u_int
)size
) > MLEN
)
1468 return ((struct mbuf
*)NULL
);
1469 if ((m
= m_get(M_DONTWAIT
, MT_CONTROL
)) == NULL
)
1470 return ((struct mbuf
*)NULL
);
1471 cp
= mtod(m
, struct cmsghdr
*);
1472 VERIFY(IS_P2ALIGNED(cp
, sizeof (u_int32_t
)));
1473 /* XXX check size? */
1474 (void) memcpy(CMSG_DATA(cp
), p
, size
);
1475 m
->m_len
= CMSG_SPACE(size
);
1476 cp
->cmsg_len
= CMSG_LEN(size
);
1477 cp
->cmsg_level
= level
;
1478 cp
->cmsg_type
= type
;
1483 sbcreatecontrol_mbuf(caddr_t p
, int size
, int type
, int level
, struct mbuf
** mp
)
1489 *mp
= sbcreatecontrol(p
, size
, type
, level
);
1493 if (CMSG_SPACE((u_int
)size
) + (*mp
)->m_len
> MLEN
){
1494 mp
= &(*mp
)->m_next
;
1495 *mp
= sbcreatecontrol(p
, size
, type
, level
);
1501 cp
= (struct cmsghdr
*)(void *)(mtod(m
, char *) + m
->m_len
);
1502 /* CMSG_SPACE ensures 32-bit alignment */
1503 VERIFY(IS_P2ALIGNED(cp
, sizeof (u_int32_t
)));
1504 m
->m_len
+= CMSG_SPACE(size
);
1506 /* XXX check size? */
1507 (void) memcpy(CMSG_DATA(cp
), p
, size
);
1508 cp
->cmsg_len
= CMSG_LEN(size
);
1509 cp
->cmsg_level
= level
;
1510 cp
->cmsg_type
= type
;
1517 * Some routines that return EOPNOTSUPP for entry points that are not
1518 * supported by a protocol. Fill in as needed.
1521 pru_abort_notsupp(__unused
struct socket
*so
)
1523 return (EOPNOTSUPP
);
1527 pru_accept_notsupp(__unused
struct socket
*so
, __unused
struct sockaddr
**nam
)
1529 return (EOPNOTSUPP
);
1533 pru_attach_notsupp(__unused
struct socket
*so
, __unused
int proto
,
1534 __unused
struct proc
*p
)
1536 return (EOPNOTSUPP
);
1540 pru_bind_notsupp(__unused
struct socket
*so
, __unused
struct sockaddr
*nam
,
1541 __unused
struct proc
*p
)
1543 return (EOPNOTSUPP
);
1547 pru_connect_notsupp(__unused
struct socket
*so
, __unused
struct sockaddr
*nam
,
1548 __unused
struct proc
*p
)
1550 return (EOPNOTSUPP
);
1554 pru_connect2_notsupp(__unused
struct socket
*so1
, __unused
struct socket
*so2
)
1556 return (EOPNOTSUPP
);
1560 pru_control_notsupp(__unused
struct socket
*so
, __unused u_long cmd
,
1561 __unused caddr_t data
, __unused
struct ifnet
*ifp
, __unused
struct proc
*p
)
1563 return (EOPNOTSUPP
);
1567 pru_detach_notsupp(__unused
struct socket
*so
)
1569 return (EOPNOTSUPP
);
1573 pru_disconnect_notsupp(__unused
struct socket
*so
)
1575 return (EOPNOTSUPP
);
1579 pru_listen_notsupp(__unused
struct socket
*so
, __unused
struct proc
*p
)
1581 return (EOPNOTSUPP
);
1585 pru_peeraddr_notsupp(__unused
struct socket
*so
, __unused
struct sockaddr
**nam
)
1587 return (EOPNOTSUPP
);
1591 pru_rcvd_notsupp(__unused
struct socket
*so
, __unused
int flags
)
1593 return (EOPNOTSUPP
);
1597 pru_rcvoob_notsupp(__unused
struct socket
*so
, __unused
struct mbuf
*m
,
1600 return (EOPNOTSUPP
);
1604 pru_send_notsupp(__unused
struct socket
*so
, __unused
int flags
,
1605 __unused
struct mbuf
*m
, __unused
struct sockaddr
*addr
,
1606 __unused
struct mbuf
*control
, __unused
struct proc
*p
)
1609 return (EOPNOTSUPP
);
1614 * This isn't really a ``null'' operation, but it's the default one
1615 * and doesn't do anything destructive.
1618 pru_sense_null(struct socket
*so
, void *ub
, int isstat64
)
1620 if (isstat64
!= 0) {
1621 struct stat64
*sb64
;
1623 sb64
= (struct stat64
*)ub
;
1624 sb64
->st_blksize
= so
->so_snd
.sb_hiwat
;
1628 sb
= (struct stat
*)ub
;
1629 sb
->st_blksize
= so
->so_snd
.sb_hiwat
;
1637 pru_sosend_notsupp(__unused
struct socket
*so
, __unused
struct sockaddr
*addr
,
1638 __unused
struct uio
*uio
, __unused
struct mbuf
*top
,
1639 __unused
struct mbuf
*control
, __unused
int flags
)
1642 return (EOPNOTSUPP
);
1646 pru_soreceive_notsupp(__unused
struct socket
*so
,
1647 __unused
struct sockaddr
**paddr
,
1648 __unused
struct uio
*uio
, __unused
struct mbuf
**mp0
,
1649 __unused
struct mbuf
**controlp
, __unused
int *flagsp
)
1651 return (EOPNOTSUPP
);
1655 pru_shutdown_notsupp(__unused
struct socket
*so
)
1657 return (EOPNOTSUPP
);
1661 pru_sockaddr_notsupp(__unused
struct socket
*so
, __unused
struct sockaddr
**nam
)
1663 return (EOPNOTSUPP
);
1667 pru_sopoll_notsupp(__unused
struct socket
*so
, __unused
int events
,
1668 __unused kauth_cred_t cred
, __unused
void *wql
)
1670 return (EOPNOTSUPP
);
1676 * The following are macros on BSD and functions on Darwin
1680 * Do we need to notify the other side when I/O is possible?
1684 sb_notify(struct sockbuf
*sb
)
1686 return ((sb
->sb_flags
&
1687 (SB_WAIT
|SB_SEL
|SB_ASYNC
|SB_UPCALL
|SB_KNOTE
)) != 0);
1691 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
1692 * This is problematical if the fields are unsigned, as the space might
1693 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
1694 * overflow and return 0.
1697 sbspace(struct sockbuf
*sb
)
1700 imin((int)(sb
->sb_hiwat
- sb
->sb_cc
),
1701 (int)(sb
->sb_mbmax
- sb
->sb_mbcnt
));
1708 /* do we have to send all at once on a socket? */
1710 sosendallatonce(struct socket
*so
)
1712 return (so
->so_proto
->pr_flags
& PR_ATOMIC
);
1715 /* can we read something from so? */
1717 soreadable(struct socket
*so
)
1719 return (so
->so_rcv
.sb_cc
>= so
->so_rcv
.sb_lowat
||
1720 (so
->so_state
& SS_CANTRCVMORE
) ||
1721 so
->so_comp
.tqh_first
|| so
->so_error
);
1724 /* can we write something to so? */
1727 sowriteable(struct socket
*so
)
1729 return ((!so_wait_for_if_feedback(so
) &&
1730 sbspace(&(so
)->so_snd
) >= (so
)->so_snd
.sb_lowat
&&
1731 ((so
->so_state
& SS_ISCONNECTED
) ||
1732 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) == 0)) ||
1733 (so
->so_state
& SS_CANTSENDMORE
) ||
1737 /* adjust counters in sb reflecting allocation of m */
1740 sballoc(struct sockbuf
*sb
, struct mbuf
*m
)
1743 sb
->sb_cc
+= m
->m_len
;
1744 if (m
->m_type
!= MT_DATA
&& m
->m_type
!= MT_HEADER
&&
1745 m
->m_type
!= MT_OOBDATA
)
1746 sb
->sb_ctl
+= m
->m_len
;
1747 sb
->sb_mbcnt
+= MSIZE
;
1749 if (m
->m_flags
& M_EXT
) {
1750 sb
->sb_mbcnt
+= m
->m_ext
.ext_size
;
1751 cnt
+= (m
->m_ext
.ext_size
>> MSIZESHIFT
) ;
1753 OSAddAtomic(cnt
, &total_sbmb_cnt
);
1754 VERIFY(total_sbmb_cnt
> 0);
1757 /* adjust counters in sb reflecting freeing of m */
1759 sbfree(struct sockbuf
*sb
, struct mbuf
*m
)
1763 sb
->sb_cc
-= m
->m_len
;
1764 if (m
->m_type
!= MT_DATA
&& m
->m_type
!= MT_HEADER
&&
1765 m
->m_type
!= MT_OOBDATA
)
1766 sb
->sb_ctl
-= m
->m_len
;
1767 sb
->sb_mbcnt
-= MSIZE
;
1768 if (m
->m_flags
& M_EXT
) {
1769 sb
->sb_mbcnt
-= m
->m_ext
.ext_size
;
1770 cnt
-= (m
->m_ext
.ext_size
>> MSIZESHIFT
) ;
1772 OSAddAtomic(cnt
, &total_sbmb_cnt
);
1773 VERIFY(total_sbmb_cnt
>= 0);
1777 * Set lock on sockbuf sb; sleep if lock is already held.
1778 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
1779 * Returns error without lock if sleep is interrupted.
1781 * Returns: 0 Success
1786 sblock(struct sockbuf
*sb
, int wf
)
1790 if (sb
->sb_flags
& SB_LOCK
)
1791 error
= (wf
== M_WAIT
) ? sb_lock(sb
) : EWOULDBLOCK
;
1793 sb
->sb_flags
|= SB_LOCK
;
1798 /* release lock on sockbuf sb */
1800 sbunlock(struct sockbuf
*sb
, int keeplocked
)
1802 struct socket
*so
= sb
->sb_so
;
1804 lck_mtx_t
*mutex_held
;
1806 lr_saved
= __builtin_return_address(0);
1808 sb
->sb_flags
&= ~SB_LOCK
;
1810 if (sb
->sb_flags
& SB_WANT
) {
1811 sb
->sb_flags
&= ~SB_WANT
;
1812 if (so
->so_usecount
< 0) {
1813 panic("sbunlock: b4 wakeup so=%p ref=%d lr=%p "
1814 "sb_flags=%x lrh= %s\n", sb
->sb_so
, so
->so_usecount
,
1815 lr_saved
, sb
->sb_flags
, solockhistory_nr(so
));
1818 wakeup((caddr_t
)&(sb
)->sb_flags
);
1820 if (keeplocked
== 0) { /* unlock on exit */
1821 if (so
->so_proto
->pr_getlock
!= NULL
)
1822 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
1824 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
1826 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
1829 if (so
->so_usecount
< 0)
1830 panic("sbunlock: unlock on exit so=%p ref=%d lr=%p "
1831 "sb_flags=%x lrh= %s\n", so
, so
->so_usecount
, lr_saved
,
1832 sb
->sb_flags
, solockhistory_nr(so
));
1833 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
1834 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
1835 lck_mtx_unlock(mutex_held
);
1840 sorwakeup(struct socket
*so
)
1842 if (sb_notify(&so
->so_rcv
))
1843 sowakeup(so
, &so
->so_rcv
);
1847 sowwakeup(struct socket
*so
)
1849 if (sb_notify(&so
->so_snd
))
1850 sowakeup(so
, &so
->so_snd
);
1854 soevent(struct socket
*so
, long hint
)
1856 if (so
->so_flags
& SOF_KNOTE
)
1857 KNOTE(&so
->so_klist
, hint
);
1860 #endif /* __APPLE__ */
1863 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
1866 dup_sockaddr(struct sockaddr
*sa
, int canwait
)
1868 struct sockaddr
*sa2
;
1870 MALLOC(sa2
, struct sockaddr
*, sa
->sa_len
, M_SONAME
,
1871 canwait
? M_WAITOK
: M_NOWAIT
);
1873 bcopy(sa
, sa2
, sa
->sa_len
);
1878 * Create an external-format (``xsocket'') structure using the information
1879 * in the kernel-format socket structure pointed to by so. This is done
1880 * to reduce the spew of irrelevant information over this interface,
1881 * to isolate user code from changes in the kernel structure, and
1882 * potentially to provide information-hiding if we decide that
1883 * some of this information should be hidden from users.
1886 sotoxsocket(struct socket
*so
, struct xsocket
*xso
)
1888 xso
->xso_len
= sizeof (*xso
);
1889 xso
->xso_so
= (_XSOCKET_PTR(struct socket
*))VM_KERNEL_ADDRPERM(so
);
1890 xso
->so_type
= so
->so_type
;
1891 xso
->so_options
= (short)(so
->so_options
& 0xffff);
1892 xso
->so_linger
= so
->so_linger
;
1893 xso
->so_state
= so
->so_state
;
1894 xso
->so_pcb
= (_XSOCKET_PTR(caddr_t
))VM_KERNEL_ADDRPERM(so
->so_pcb
);
1896 xso
->xso_protocol
= so
->so_proto
->pr_protocol
;
1897 xso
->xso_family
= so
->so_proto
->pr_domain
->dom_family
;
1899 xso
->xso_protocol
= xso
->xso_family
= 0;
1901 xso
->so_qlen
= so
->so_qlen
;
1902 xso
->so_incqlen
= so
->so_incqlen
;
1903 xso
->so_qlimit
= so
->so_qlimit
;
1904 xso
->so_timeo
= so
->so_timeo
;
1905 xso
->so_error
= so
->so_error
;
1906 xso
->so_pgid
= so
->so_pgid
;
1907 xso
->so_oobmark
= so
->so_oobmark
;
1908 sbtoxsockbuf(&so
->so_snd
, &xso
->so_snd
);
1909 sbtoxsockbuf(&so
->so_rcv
, &xso
->so_rcv
);
1910 xso
->so_uid
= kauth_cred_getuid(so
->so_cred
);
1914 #if !CONFIG_EMBEDDED
1917 sotoxsocket64(struct socket
*so
, struct xsocket64
*xso
)
1919 xso
->xso_len
= sizeof (*xso
);
1920 xso
->xso_so
= (u_int64_t
)VM_KERNEL_ADDRPERM(so
);
1921 xso
->so_type
= so
->so_type
;
1922 xso
->so_options
= (short)(so
->so_options
& 0xffff);
1923 xso
->so_linger
= so
->so_linger
;
1924 xso
->so_state
= so
->so_state
;
1925 xso
->so_pcb
= (u_int64_t
)VM_KERNEL_ADDRPERM(so
->so_pcb
);
1927 xso
->xso_protocol
= so
->so_proto
->pr_protocol
;
1928 xso
->xso_family
= so
->so_proto
->pr_domain
->dom_family
;
1930 xso
->xso_protocol
= xso
->xso_family
= 0;
1932 xso
->so_qlen
= so
->so_qlen
;
1933 xso
->so_incqlen
= so
->so_incqlen
;
1934 xso
->so_qlimit
= so
->so_qlimit
;
1935 xso
->so_timeo
= so
->so_timeo
;
1936 xso
->so_error
= so
->so_error
;
1937 xso
->so_pgid
= so
->so_pgid
;
1938 xso
->so_oobmark
= so
->so_oobmark
;
1939 sbtoxsockbuf(&so
->so_snd
, &xso
->so_snd
);
1940 sbtoxsockbuf(&so
->so_rcv
, &xso
->so_rcv
);
1941 xso
->so_uid
= kauth_cred_getuid(so
->so_cred
);
1944 #endif /* !CONFIG_EMBEDDED */
1947 * This does the same for sockbufs. Note that the xsockbuf structure,
1948 * since it is always embedded in a socket, does not include a self
1949 * pointer nor a length. We make this entry point public in case
1950 * some other mechanism needs it.
1953 sbtoxsockbuf(struct sockbuf
*sb
, struct xsockbuf
*xsb
)
1955 xsb
->sb_cc
= sb
->sb_cc
;
1956 xsb
->sb_hiwat
= sb
->sb_hiwat
;
1957 xsb
->sb_mbcnt
= sb
->sb_mbcnt
;
1958 xsb
->sb_mbmax
= sb
->sb_mbmax
;
1959 xsb
->sb_lowat
= sb
->sb_lowat
;
1960 xsb
->sb_flags
= sb
->sb_flags
;
1961 xsb
->sb_timeo
= (short)
1962 (sb
->sb_timeo
.tv_sec
* hz
) + sb
->sb_timeo
.tv_usec
/ tick
;
1963 if (xsb
->sb_timeo
== 0 && sb
->sb_timeo
.tv_usec
!= 0)
1968 * Based on the policy set by an all knowing decison maker, throttle sockets
1969 * that either have been marked as belonging to "background" process.
1972 soisthrottled(struct socket
*so
)
1975 * On non-embedded, we rely on implicit throttling by the application,
1976 * as we're missing the system-wide "decision maker".
1980 net_io_policy_throttled
&&
1981 #endif /* CONFIG_EMBEDDED */
1982 (so
->so_traffic_mgt_flags
& TRAFFIC_MGT_SO_BACKGROUND
));
1986 soisprivilegedtraffic(struct socket
*so
)
1988 return (so
->so_flags
& SOF_PRIVILEGED_TRAFFIC_CLASS
);
1992 * Here is the definition of some of the basic objects in the kern.ipc
1993 * branch of the MIB.
1995 SYSCTL_NODE(_kern
, KERN_IPC
, ipc
, CTLFLAG_RW
|CTLFLAG_LOCKED
|CTLFLAG_ANYBODY
, 0, "IPC");
1997 /* Check that the maximum socket buffer size is within a range */
2000 sysctl_sb_max(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
2001 __unused
int arg2
, struct sysctl_req
*req
)
2003 u_int32_t new_value
;
2005 int error
= sysctl_io_number(req
, sb_max
, sizeof(u_int32_t
), &new_value
,
2007 if (!error
&& changed
) {
2008 if (new_value
> LOW_SB_MAX
&&
2009 new_value
<= high_sb_max
) {
2019 sysctl_io_policy_throttled SYSCTL_HANDLER_ARGS
2021 #pragma unused(arg1, arg2)
2024 i
= net_io_policy_throttled
;
2026 err
= sysctl_handle_int(oidp
, &i
, 0, req
);
2027 if (err
!= 0 || req
->newptr
== USER_ADDR_NULL
)
2030 if (i
!= net_io_policy_throttled
)
2031 SOTHROTTLELOG(("throttle: network IO policy throttling is "
2032 "now %s\n", i
? "ON" : "OFF"));
2034 net_io_policy_throttled
= i
;
2039 SYSCTL_PROC(_kern_ipc
, KIPC_MAXSOCKBUF
, maxsockbuf
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
2040 &sb_max
, 0, &sysctl_sb_max
, "IU", "Maximum socket buffer size");
2042 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxsockets
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2043 &maxsockets
, 0, "Maximum number of sockets avaliable");
2044 SYSCTL_INT(_kern_ipc
, KIPC_SOCKBUF_WASTE
, sockbuf_waste_factor
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
2045 &sb_efficiency
, 0, "");
2046 SYSCTL_INT(_kern_ipc
, KIPC_NMBCLUSTERS
, nmbclusters
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2047 &nmbclusters
, 0, "");
2048 SYSCTL_INT(_kern_ipc
, OID_AUTO
, njcl
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &njcl
, 0, "");
2049 SYSCTL_INT(_kern_ipc
, OID_AUTO
, njclbytes
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &njclbytes
, 0, "");
2050 SYSCTL_INT(_kern_ipc
, KIPC_SOQLIMITCOMPAT
, soqlimitcompat
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
2051 &soqlimitcompat
, 1, "Enable socket queue limit compatibility");
2052 SYSCTL_INT(_kern_ipc
, OID_AUTO
, soqlencomp
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
2053 &soqlencomp
, 0, "Listen backlog represents only complete queue");
2055 SYSCTL_NODE(_kern_ipc
, OID_AUTO
, io_policy
, CTLFLAG_RW
, 0, "network IO policy");
2057 SYSCTL_PROC(_kern_ipc_io_policy
, OID_AUTO
, throttled
,
2058 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &net_io_policy_throttled
, 0,
2059 sysctl_io_policy_throttled
, "I", "");