2 * Copyright (c) 1998-2011 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/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/signalvar.h>
84 #include <sys/sysctl.h>
86 #include <kern/locks.h>
87 #include <net/route.h>
88 #include <netinet/in.h>
89 #include <netinet/in_pcb.h>
90 #include <sys/kdebug.h>
91 #include <libkern/OSAtomic.h>
94 #include <security/mac_framework.h>
97 /* TODO: this should be in a header file somewhere */
98 extern void postevent(struct socket
*, struct sockbuf
*, int);
100 #define DBG_FNC_SBDROP NETDBG_CODE(DBG_NETSOCK, 4)
101 #define DBG_FNC_SBAPPEND NETDBG_CODE(DBG_NETSOCK, 5)
103 static inline void sbcompress(struct sockbuf
*, struct mbuf
*, struct mbuf
*);
104 static struct socket
*sonewconn_internal(struct socket
*, int);
105 static int sbappendaddr_internal(struct sockbuf
*, struct sockaddr
*,
106 struct mbuf
*, struct mbuf
*);
107 static int sbappendcontrol_internal(struct sockbuf
*, struct mbuf
*,
111 * Primitive routines for operating on sockets and socket buffers
113 static int soqlimitcompat
= 1;
114 static int soqlencomp
= 0;
116 /* Based on the number of mbuf clusters configured, high_sb_max and sb_max can get
117 * scaled up or down to suit that memory configuration. high_sb_max is a higher
118 * limit on sb_max that is checked when sb_max gets set through sysctl.
121 u_int32_t sb_max
= SB_MAX
; /* XXX should be static */
122 u_int32_t high_sb_max
= SB_MAX
;
124 static u_int32_t sb_efficiency
= 8; /* parameter for sbreserve() */
125 __private_extern__
unsigned int total_mb_cnt
= 0;
126 __private_extern__
unsigned int total_cl_cnt
= 0;
127 __private_extern__
int sbspace_factor
= 8;
130 * Procedures to manipulate state flags of socket
131 * and do appropriate wakeups. Normal sequence from the
132 * active (originating) side is that soisconnecting() is
133 * called during processing of connect() call,
134 * resulting in an eventual call to soisconnected() if/when the
135 * connection is established. When the connection is torn down
136 * soisdisconnecting() is called during processing of disconnect() call,
137 * and soisdisconnected() is called when the connection to the peer
138 * is totally severed. The semantics of these routines are such that
139 * connectionless protocols can call soisconnected() and soisdisconnected()
140 * only, bypassing the in-progress calls when setting up a ``connection''
143 * From the passive side, a socket is created with
144 * two queues of sockets: so_incomp for connections in progress
145 * and so_comp for connections already made and awaiting user acceptance.
146 * As a protocol is preparing incoming connections, it creates a socket
147 * structure queued on so_incomp by calling sonewconn(). When the connection
148 * is established, soisconnected() is called, and transfers the
149 * socket structure to so_comp, making it available to accept().
151 * If a socket is closed with sockets on either
152 * so_incomp or so_comp, these sockets are dropped.
154 * If higher level protocols are implemented in
155 * the kernel, the wakeups done here will sometimes
156 * cause software-interrupt process scheduling.
159 soisconnecting(struct socket
*so
)
162 so
->so_state
&= ~(SS_ISCONNECTED
|SS_ISDISCONNECTING
);
163 so
->so_state
|= SS_ISCONNECTING
;
165 sflt_notify(so
, sock_evt_connecting
, NULL
);
169 soisconnected(struct socket
*so
)
171 struct socket
*head
= so
->so_head
;
173 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISDISCONNECTING
|SS_ISCONFIRMING
);
174 so
->so_state
|= SS_ISCONNECTED
;
176 sflt_notify(so
, sock_evt_connected
, NULL
);
178 if (head
&& (so
->so_state
& SS_INCOMP
)) {
179 so
->so_state
&= ~SS_INCOMP
;
180 so
->so_state
|= SS_COMP
;
181 if (head
->so_proto
->pr_getlock
!= NULL
) {
182 socket_unlock(so
, 0);
183 socket_lock(head
, 1);
185 postevent(head
, 0, EV_RCONN
);
186 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
188 TAILQ_INSERT_TAIL(&head
->so_comp
, so
, so_list
);
190 wakeup_one((caddr_t
)&head
->so_timeo
);
191 if (head
->so_proto
->pr_getlock
!= NULL
) {
192 socket_unlock(head
, 1);
196 postevent(so
, 0, EV_WCONN
);
197 wakeup((caddr_t
)&so
->so_timeo
);
204 soisdisconnecting(struct socket
*so
)
206 so
->so_state
&= ~SS_ISCONNECTING
;
207 so
->so_state
|= (SS_ISDISCONNECTING
|SS_CANTRCVMORE
|SS_CANTSENDMORE
);
208 sflt_notify(so
, sock_evt_disconnecting
, NULL
);
209 wakeup((caddr_t
)&so
->so_timeo
);
215 soisdisconnected(struct socket
*so
)
217 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
218 so
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
);
219 sflt_notify(so
, sock_evt_disconnected
, NULL
);
220 wakeup((caddr_t
)&so
->so_timeo
);
225 /* This function will issue a wakeup like soisdisconnected but it will not
226 * notify the socket filters. This will avoid unlocking the socket
227 * in the midst of closing it.
230 sodisconnectwakeup(struct socket
*so
)
232 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
233 so
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
);
234 wakeup((caddr_t
)&so
->so_timeo
);
240 * When an attempt at a new connection is noted on a socket
241 * which accepts connections, sonewconn is called. If the
242 * connection is possible (subject to space constraints, etc.)
243 * then we allocate a new structure, propoerly linked into the
244 * data structure of the original socket, and return this.
245 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
247 static struct socket
*
248 sonewconn_internal(struct socket
*head
, int connstatus
)
250 int so_qlen
, error
= 0;
252 lck_mtx_t
*mutex_held
;
254 if (head
->so_proto
->pr_getlock
!= NULL
)
255 mutex_held
= (*head
->so_proto
->pr_getlock
)(head
, 0);
257 mutex_held
= head
->so_proto
->pr_domain
->dom_mtx
;
258 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
262 * This is the default case; so_qlen represents the
263 * sum of both incomplete and completed queues.
265 so_qlen
= head
->so_qlen
;
268 * When kern.ipc.soqlencomp is set to 1, so_qlen
269 * represents only the completed queue. Since we
270 * cannot let the incomplete queue goes unbounded
271 * (in case of SYN flood), we cap the incomplete
272 * queue length to at most somaxconn, and use that
273 * as so_qlen so that we fail immediately below.
275 so_qlen
= head
->so_qlen
- head
->so_incqlen
;
276 if (head
->so_incqlen
> somaxconn
)
281 (soqlimitcompat
? head
->so_qlimit
: (3 * head
->so_qlimit
/ 2)))
282 return ((struct socket
*)0);
283 so
= soalloc(1, head
->so_proto
->pr_domain
->dom_family
,
286 return ((struct socket
*)0);
287 /* check if head was closed during the soalloc */
288 if (head
->so_proto
== NULL
) {
290 return ((struct socket
*)0);
293 so
->so_type
= head
->so_type
;
294 so
->so_options
= head
->so_options
&~ SO_ACCEPTCONN
;
295 so
->so_linger
= head
->so_linger
;
296 so
->so_state
= head
->so_state
| SS_NOFDREF
;
297 so
->so_proto
= head
->so_proto
;
298 so
->so_timeo
= head
->so_timeo
;
299 so
->so_pgid
= head
->so_pgid
;
300 so
->so_uid
= head
->so_uid
;
301 so
->so_gid
= head
->so_gid
;
302 /* inherit socket options stored in so_flags */
303 so
->so_flags
= head
->so_flags
& (SOF_NOSIGPIPE
|
311 so
->next_lock_lr
= 0;
312 so
->next_unlock_lr
= 0;
315 so
->so_rcv
.sb_flags
|= SB_RECV
; /* XXX */
316 so
->so_rcv
.sb_so
= so
->so_snd
.sb_so
= so
;
317 TAILQ_INIT(&so
->so_evlist
);
320 #if CONFIG_MACF_SOCKET
321 mac_socket_label_associate_accept(head
, so
);
324 /* inherit traffic management properties of listener */
325 so
->so_traffic_mgt_flags
= head
->so_traffic_mgt_flags
& (TRAFFIC_MGT_SO_BACKGROUND
);
326 so
->so_background_thread
= head
->so_background_thread
;
327 so
->so_traffic_class
= head
->so_traffic_class
;
329 if (soreserve(so
, head
->so_snd
.sb_hiwat
, head
->so_rcv
.sb_hiwat
)) {
331 return ((struct socket
*)0);
335 * Must be done with head unlocked to avoid deadlock
336 * for protocol with per socket mutexes.
338 if (head
->so_proto
->pr_unlock
)
339 socket_unlock(head
, 0);
340 if (((*so
->so_proto
->pr_usrreqs
->pru_attach
)(so
, 0, NULL
) != 0) ||
343 if (head
->so_proto
->pr_unlock
)
344 socket_lock(head
, 0);
345 return ((struct socket
*)0);
347 if (head
->so_proto
->pr_unlock
) {
348 socket_lock(head
, 0);
349 /* Radar 7385998 Recheck that the head is still accepting
350 * to avoid race condition when head is getting closed.
352 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
353 so
->so_state
&= ~SS_NOFDREF
;
355 return ((struct socket
*)0);
360 so
->so_proto
->pr_domain
->dom_refs
++;
362 /* Insert in head appropriate lists */
365 /* Since this socket is going to be inserted into the incomp
366 * queue, it can be picked up by another thread in
367 * tcp_dropdropablreq to get dropped before it is setup..
368 * To prevent this race, set in-progress flag which can be
371 so
->so_flags
|= SOF_INCOMP_INPROGRESS
;
374 TAILQ_INSERT_TAIL(&head
->so_comp
, so
, so_list
);
375 so
->so_state
|= SS_COMP
;
377 TAILQ_INSERT_TAIL(&head
->so_incomp
, so
, so_list
);
378 so
->so_state
|= SS_INCOMP
;
384 /* Attach socket filters for this protocol */
389 so
->so_state
|= connstatus
;
391 wakeup((caddr_t
)&head
->so_timeo
);
398 sonewconn(struct socket
*head
, int connstatus
, const struct sockaddr
*from
)
400 int error
= sflt_connectin(head
, from
);
405 return (sonewconn_internal(head
, connstatus
));
409 * Socantsendmore indicates that no more data will be sent on the
410 * socket; it would normally be applied to a socket when the user
411 * informs the system that no more data is to be sent, by the protocol
412 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
413 * will be received, and will normally be applied to the socket by a
414 * protocol when it detects that the peer will send no more data.
415 * Data queued for reading in the socket may yet be read.
419 socantsendmore(struct socket
*so
)
421 so
->so_state
|= SS_CANTSENDMORE
;
422 sflt_notify(so
, sock_evt_cantsendmore
, NULL
);
427 socantrcvmore(struct socket
*so
)
429 so
->so_state
|= SS_CANTRCVMORE
;
430 sflt_notify(so
, sock_evt_cantrecvmore
, NULL
);
435 * Wait for data to arrive at/drain from a socket buffer.
442 sbwait(struct sockbuf
*sb
)
446 struct socket
*so
= sb
->sb_so
;
447 lck_mtx_t
*mutex_held
;
450 lr_saved
= (uintptr_t) __builtin_return_address(0);
452 if (so
->so_proto
->pr_getlock
!= NULL
)
453 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
455 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
456 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
458 sb
->sb_flags
|= SB_WAIT
;
460 if (so
->so_usecount
< 1)
461 panic("sbwait: so=%p refcount=%d\n", so
, so
->so_usecount
);
462 ts
.tv_sec
= sb
->sb_timeo
.tv_sec
;
463 ts
.tv_nsec
= sb
->sb_timeo
.tv_usec
* 1000;
464 error
= msleep((caddr_t
)&sb
->sb_cc
, mutex_held
,
465 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
| PCATCH
, "sbwait", &ts
);
467 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
469 if (so
->so_usecount
< 1)
470 panic("sbwait: so=%p refcount=%d\n", so
, so
->so_usecount
);
472 if ((so
->so_state
& SS_DRAINING
) || (so
->so_flags
& SOF_DEFUNCT
)) {
474 if (so
->so_flags
& SOF_DEFUNCT
) {
475 SODEFUNCTLOG(("%s[%d]: defunct so %p [%d,%d] (%d)\n",
476 __func__
, proc_selfpid(), so
, INP_SOCKAF(so
),
477 INP_SOCKTYPE(so
), error
));
485 * Lock a sockbuf already known to be locked;
486 * return any error returned from sleep (EINTR).
492 sb_lock(struct sockbuf
*sb
)
494 struct socket
*so
= sb
->sb_so
;
495 lck_mtx_t
*mutex_held
;
499 panic("sb_lock: null so back pointer sb=%p\n", sb
);
501 while (sb
->sb_flags
& SB_LOCK
) {
502 sb
->sb_flags
|= SB_WANT
;
504 if (so
->so_proto
->pr_getlock
!= NULL
)
505 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
507 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
508 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
510 if (so
->so_usecount
< 1)
511 panic("sb_lock: so=%p refcount=%d\n", so
,
514 error
= msleep((caddr_t
)&sb
->sb_flags
, mutex_held
,
515 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
| PCATCH
,
517 if (so
->so_usecount
< 1)
518 panic("sb_lock: 2 so=%p refcount=%d\n", so
,
521 if (error
== 0 && (so
->so_flags
& SOF_DEFUNCT
)) {
523 SODEFUNCTLOG(("%s[%d]: defunct so %p [%d,%d] (%d)\n",
524 __func__
, proc_selfpid(), so
, INP_SOCKAF(so
),
525 INP_SOCKTYPE(so
), error
));
531 sb
->sb_flags
|= SB_LOCK
;
536 sbwakeup(struct sockbuf
*sb
)
538 if (sb
->sb_flags
& SB_WAIT
) {
539 sb
->sb_flags
&= ~SB_WAIT
;
540 wakeup((caddr_t
)&sb
->sb_cc
);
545 * Wakeup processes waiting on a socket buffer.
546 * Do asynchronous notification via SIGIO
547 * if the socket has the SS_ASYNC flag set.
550 sowakeup(struct socket
*so
, struct sockbuf
*sb
)
552 if (so
->so_flags
& SOF_DEFUNCT
) {
553 SODEFUNCTLOG(("%s[%d]: defunct so %p [%d,%d] si 0x%x, "
554 "fl 0x%x [%s]\n", __func__
, proc_selfpid(), so
,
555 INP_SOCKAF(so
), INP_SOCKTYPE(so
),
556 (uint32_t)sb
->sb_sel
.si_flags
, (uint16_t)sb
->sb_flags
,
557 (sb
->sb_flags
& SB_RECV
) ? "rcv" : "snd"));
560 sb
->sb_flags
&= ~SB_SEL
;
561 selwakeup(&sb
->sb_sel
);
563 if (so
->so_state
& SS_ASYNC
) {
565 gsignal(-so
->so_pgid
, SIGIO
);
566 else if (so
->so_pgid
> 0)
567 proc_signal(so
->so_pgid
, SIGIO
);
569 if (sb
->sb_flags
& SB_KNOTE
) {
570 KNOTE(&sb
->sb_sel
.si_note
, SO_FILT_HINT_LOCKED
);
572 if (sb
->sb_flags
& SB_UPCALL
) {
573 void (*so_upcall
)(struct socket
*, caddr_t
, int);
574 caddr_t so_upcallarg
;
576 so_upcall
= so
->so_upcall
;
577 so_upcallarg
= so
->so_upcallarg
;
578 /* Let close know that we're about to do an upcall */
579 so
->so_flags
|= SOF_UPCALLINUSE
;
581 socket_unlock(so
, 0);
582 (*so_upcall
)(so
, so_upcallarg
, M_DONTWAIT
);
585 so
->so_flags
&= ~SOF_UPCALLINUSE
;
586 /* Tell close that it's safe to proceed */
587 if (so
->so_flags
& SOF_CLOSEWAIT
)
588 wakeup((caddr_t
)&so
->so_upcall
);
593 * Socket buffer (struct sockbuf) utility routines.
595 * Each socket contains two socket buffers: one for sending data and
596 * one for receiving data. Each buffer contains a queue of mbufs,
597 * information about the number of mbufs and amount of data in the
598 * queue, and other fields allowing select() statements and notification
599 * on data availability to be implemented.
601 * Data stored in a socket buffer is maintained as a list of records.
602 * Each record is a list of mbufs chained together with the m_next
603 * field. Records are chained together with the m_nextpkt field. The upper
604 * level routine soreceive() expects the following conventions to be
605 * observed when placing information in the receive buffer:
607 * 1. If the protocol requires each message be preceded by the sender's
608 * name, then a record containing that name must be present before
609 * any associated data (mbuf's must be of type MT_SONAME).
610 * 2. If the protocol supports the exchange of ``access rights'' (really
611 * just additional data associated with the message), and there are
612 * ``rights'' to be received, then a record containing this data
613 * should be present (mbuf's must be of type MT_RIGHTS).
614 * 3. If a name or rights record exists, then it must be followed by
615 * a data record, perhaps of zero length.
617 * Before using a new socket structure it is first necessary to reserve
618 * buffer space to the socket, by calling sbreserve(). This should commit
619 * some of the available buffer space in the system buffer pool for the
620 * socket (currently, it does nothing but enforce limits). The space
621 * should be released by calling sbrelease() when the socket is destroyed.
629 soreserve(struct socket
*so
, u_int32_t sndcc
, u_int32_t rcvcc
)
632 if (sbreserve(&so
->so_snd
, sndcc
) == 0)
634 if (sbreserve(&so
->so_rcv
, rcvcc
) == 0)
636 if (so
->so_rcv
.sb_lowat
== 0)
637 so
->so_rcv
.sb_lowat
= 1;
638 if (so
->so_snd
.sb_lowat
== 0)
639 so
->so_snd
.sb_lowat
= MCLBYTES
;
640 if (so
->so_snd
.sb_lowat
> so
->so_snd
.sb_hiwat
)
641 so
->so_snd
.sb_lowat
= so
->so_snd
.sb_hiwat
;
645 selthreadclear(&so
->so_snd
.sb_sel
);
647 sbrelease(&so
->so_snd
);
653 * Allot mbufs to a sockbuf.
654 * Attempt to scale mbmax so that mbcnt doesn't become limiting
655 * if buffering efficiency is near the normal case.
658 sbreserve(struct sockbuf
*sb
, u_int32_t cc
)
660 if ((u_quad_t
)cc
> (u_quad_t
)sb_max
* MCLBYTES
/ (MSIZE
+ MCLBYTES
))
663 sb
->sb_mbmax
= min(cc
* sb_efficiency
, sb_max
);
664 if (sb
->sb_lowat
> sb
->sb_hiwat
)
665 sb
->sb_lowat
= sb
->sb_hiwat
;
670 * Free mbufs held by a socket, and reserved mbuf space.
672 /* WARNING needs to do selthreadclear() before calling this */
674 sbrelease(struct sockbuf
*sb
)
682 * Routines to add and remove
683 * data from an mbuf queue.
685 * The routines sbappend() or sbappendrecord() are normally called to
686 * append new mbufs to a socket buffer, after checking that adequate
687 * space is available, comparing the function sbspace() with the amount
688 * of data to be added. sbappendrecord() differs from sbappend() in
689 * that data supplied is treated as the beginning of a new record.
690 * To place a sender's address, optional access rights, and data in a
691 * socket receive buffer, sbappendaddr() should be used. To place
692 * access rights and data in a socket receive buffer, sbappendrights()
693 * should be used. In either case, the new data begins a new record.
694 * Note that unlike sbappend() and sbappendrecord(), these routines check
695 * for the caller that there will be enough space to store the data.
696 * Each fails if there is not enough space, or if it cannot find mbufs
697 * to store additional information in.
699 * Reliable protocols may use the socket send buffer to hold data
700 * awaiting acknowledgement. Data is normally copied from a socket
701 * send buffer in a protocol with m_copy for output to a peer,
702 * and then removing the data from the socket buffer with sbdrop()
703 * or sbdroprecord() when the data is acknowledged by the peer.
707 * Append mbuf chain m to the last record in the
708 * socket buffer sb. The additional space associated
709 * the mbuf chain is recorded in sb. Empty mbufs are
710 * discarded and mbufs are compacted where possible.
713 sbappend(struct sockbuf
*sb
, struct mbuf
*m
)
715 struct socket
*so
= sb
->sb_so
;
717 if (m
== NULL
|| (sb
->sb_flags
& SB_DROP
)) {
723 SBLASTRECORDCHK(sb
, "sbappend 1");
725 if (sb
->sb_lastrecord
!= NULL
&& (sb
->sb_mbtail
->m_flags
& M_EOR
))
726 return (sbappendrecord(sb
, m
));
728 if (sb
->sb_flags
& SB_RECV
) {
729 int error
= sflt_data_in(so
, NULL
, &m
, NULL
, 0);
730 SBLASTRECORDCHK(sb
, "sbappend 2");
732 if (error
!= EJUSTRETURN
)
738 /* If this is the first record, it's also the last record */
739 if (sb
->sb_lastrecord
== NULL
)
740 sb
->sb_lastrecord
= m
;
742 sbcompress(sb
, m
, sb
->sb_mbtail
);
743 SBLASTRECORDCHK(sb
, "sbappend 3");
748 * Similar to sbappend, except that this is optimized for stream sockets.
751 sbappendstream(struct sockbuf
*sb
, struct mbuf
*m
)
753 struct socket
*so
= sb
->sb_so
;
755 if (m
->m_nextpkt
!= NULL
|| (sb
->sb_mb
!= sb
->sb_lastrecord
))
756 panic("sbappendstream: nexpkt %p || mb %p != lastrecord %p\n",
757 m
->m_nextpkt
, sb
->sb_mb
, sb
->sb_lastrecord
);
759 SBLASTMBUFCHK(sb
, __func__
);
761 if (m
== NULL
|| (sb
->sb_flags
& SB_DROP
)) {
767 if (sb
->sb_flags
& SB_RECV
) {
768 int error
= sflt_data_in(so
, NULL
, &m
, NULL
, 0);
769 SBLASTRECORDCHK(sb
, "sbappendstream 1");
771 if (error
!= EJUSTRETURN
)
777 sbcompress(sb
, m
, sb
->sb_mbtail
);
778 sb
->sb_lastrecord
= sb
->sb_mb
;
779 SBLASTRECORDCHK(sb
, "sbappendstream 2");
785 sbcheck(struct sockbuf
*sb
)
789 u_int32_t len
= 0, mbcnt
= 0;
790 lck_mtx_t
*mutex_held
;
792 if (sb
->sb_so
->so_proto
->pr_getlock
!= NULL
)
793 mutex_held
= (*sb
->sb_so
->so_proto
->pr_getlock
)(sb
->sb_so
, 0);
795 mutex_held
= sb
->sb_so
->so_proto
->pr_domain
->dom_mtx
;
797 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
802 for (m
= sb
->sb_mb
; m
; m
= n
) {
804 for (; m
; m
= m
->m_next
) {
807 /* XXX pretty sure this is bogus */
808 if (m
->m_flags
& M_EXT
)
809 mbcnt
+= m
->m_ext
.ext_size
;
812 if (len
!= sb
->sb_cc
|| mbcnt
!= sb
->sb_mbcnt
) {
813 panic("cc %ld != %ld || mbcnt %ld != %ld\n", len
, sb
->sb_cc
,
814 mbcnt
, sb
->sb_mbcnt
);
820 sblastrecordchk(struct sockbuf
*sb
, const char *where
)
822 struct mbuf
*m
= sb
->sb_mb
;
824 while (m
&& m
->m_nextpkt
)
827 if (m
!= sb
->sb_lastrecord
) {
828 printf("sblastrecordchk: mb %p lastrecord %p last %p\n",
829 sb
->sb_mb
, sb
->sb_lastrecord
, m
);
830 printf("packet chain:\n");
831 for (m
= sb
->sb_mb
; m
!= NULL
; m
= m
->m_nextpkt
)
833 panic("sblastrecordchk from %s", where
);
838 sblastmbufchk(struct sockbuf
*sb
, const char *where
)
840 struct mbuf
*m
= sb
->sb_mb
;
843 while (m
&& m
->m_nextpkt
)
846 while (m
&& m
->m_next
)
849 if (m
!= sb
->sb_mbtail
) {
850 printf("sblastmbufchk: mb %p mbtail %p last %p\n",
851 sb
->sb_mb
, sb
->sb_mbtail
, m
);
852 printf("packet tree:\n");
853 for (m
= sb
->sb_mb
; m
!= NULL
; m
= m
->m_nextpkt
) {
855 for (n
= m
; n
!= NULL
; n
= n
->m_next
)
859 panic("sblastmbufchk from %s", where
);
864 * Similar to sbappend, except the mbuf chain begins a new record.
867 sbappendrecord(struct sockbuf
*sb
, struct mbuf
*m0
)
872 if (m0
== NULL
|| (sb
->sb_flags
& SB_DROP
)) {
878 for (m
= m0
; m
!= NULL
; m
= m
->m_next
)
881 if (space
> sbspace(sb
) && !(sb
->sb_flags
& SB_UNIX
)) {
886 if (sb
->sb_flags
& SB_RECV
) {
887 int error
= sflt_data_in(sb
->sb_so
, NULL
, &m0
, NULL
,
888 sock_data_filt_flag_record
);
890 SBLASTRECORDCHK(sb
, "sbappendrecord 1");
891 if (error
!= EJUSTRETURN
)
898 * Note this permits zero length records.
901 SBLASTRECORDCHK(sb
, "sbappendrecord 2");
902 if (sb
->sb_lastrecord
!= NULL
) {
903 sb
->sb_lastrecord
->m_nextpkt
= m0
;
907 sb
->sb_lastrecord
= m0
;
912 if (m
&& (m0
->m_flags
& M_EOR
)) {
913 m0
->m_flags
&= ~M_EOR
;
916 sbcompress(sb
, m
, m0
);
917 SBLASTRECORDCHK(sb
, "sbappendrecord 3");
922 * As above except that OOB data
923 * is inserted at the beginning of the sockbuf,
924 * but after any other OOB data.
927 sbinsertoob(struct sockbuf
*sb
, struct mbuf
*m0
)
935 SBLASTRECORDCHK(sb
, "sbinsertoob 1");
937 if ((sb
->sb_flags
& SB_RECV
) != 0) {
938 int error
= sflt_data_in(sb
->sb_so
, NULL
, &m0
, NULL
,
939 sock_data_filt_flag_oob
);
941 SBLASTRECORDCHK(sb
, "sbinsertoob 2");
943 if (error
!= EJUSTRETURN
) {
950 for (mp
= &sb
->sb_mb
; *mp
; mp
= &((*mp
)->m_nextpkt
)) {
956 continue; /* WANT next train */
961 goto again
; /* inspect THIS train further */
966 * Put the first mbuf on the queue.
967 * Note this permits zero length records.
972 /* m0 is actually the new tail */
973 sb
->sb_lastrecord
= m0
;
978 if (m
&& (m0
->m_flags
& M_EOR
)) {
979 m0
->m_flags
&= ~M_EOR
;
982 sbcompress(sb
, m
, m0
);
983 SBLASTRECORDCHK(sb
, "sbinsertoob 3");
988 * Append address and data, and optionally, control (ancillary) data
989 * to the receive queue of a socket. If present,
990 * m0 must include a packet header with total length.
991 * Returns 0 if no space in sockbuf or insufficient mbufs.
993 * Returns: 0 No space/out of mbufs
997 sbappendaddr_internal(struct sockbuf
*sb
, struct sockaddr
*asa
,
998 struct mbuf
*m0
, struct mbuf
*control
)
1000 struct mbuf
*m
, *n
, *nlast
;
1001 int space
= asa
->sa_len
;
1003 if (m0
&& (m0
->m_flags
& M_PKTHDR
) == 0)
1004 panic("sbappendaddr");
1007 space
+= m0
->m_pkthdr
.len
;
1008 for (n
= control
; n
; n
= n
->m_next
) {
1010 if (n
->m_next
== 0) /* keep pointer to last control buf */
1013 if (space
> sbspace(sb
))
1015 if (asa
->sa_len
> MLEN
)
1017 MGET(m
, M_DONTWAIT
, MT_SONAME
);
1020 m
->m_len
= asa
->sa_len
;
1021 bcopy((caddr_t
)asa
, mtod(m
, caddr_t
), asa
->sa_len
);
1023 n
->m_next
= m0
; /* concatenate data to control */
1026 m
->m_next
= control
;
1028 SBLASTRECORDCHK(sb
, "sbappendadddr 1");
1030 for (n
= m
; n
->m_next
!= NULL
; n
= n
->m_next
)
1035 if (sb
->sb_lastrecord
!= NULL
) {
1036 sb
->sb_lastrecord
->m_nextpkt
= m
;
1040 sb
->sb_lastrecord
= m
;
1041 sb
->sb_mbtail
= nlast
;
1043 SBLASTMBUFCHK(sb
, __func__
);
1044 SBLASTRECORDCHK(sb
, "sbappendadddr 2");
1046 postevent(0, sb
, EV_RWBYTES
);
1051 * Returns: 0 Error: No space/out of mbufs/etc.
1054 * Imputed: (*error_out) errno for error
1056 * sflt_data_in:??? [whatever a filter author chooses]
1059 sbappendaddr(struct sockbuf
*sb
, struct sockaddr
*asa
, struct mbuf
*m0
,
1060 struct mbuf
*control
, int *error_out
)
1063 boolean_t sb_unix
= (sb
->sb_flags
& SB_UNIX
);
1068 if (m0
&& (m0
->m_flags
& M_PKTHDR
) == 0)
1069 panic("sbappendaddrorfree");
1071 if (sb
->sb_flags
& SB_DROP
) {
1074 if (control
!= NULL
&& !sb_unix
)
1076 if (error_out
!= NULL
)
1077 *error_out
= EINVAL
;
1081 /* Call socket data in filters */
1082 if ((sb
->sb_flags
& SB_RECV
) != 0) {
1084 error
= sflt_data_in(sb
->sb_so
, asa
, &m0
, &control
, 0);
1085 SBLASTRECORDCHK(sb
, __func__
);
1087 if (error
!= EJUSTRETURN
) {
1090 if (control
!= NULL
&& !sb_unix
)
1099 result
= sbappendaddr_internal(sb
, asa
, m0
, control
);
1103 if (control
!= NULL
&& !sb_unix
)
1106 *error_out
= ENOBUFS
;
1113 sbappendcontrol_internal(struct sockbuf
*sb
, struct mbuf
*m0
,
1114 struct mbuf
*control
)
1116 struct mbuf
*m
, *mlast
, *n
;
1120 panic("sbappendcontrol");
1122 for (m
= control
; ; m
= m
->m_next
) {
1127 n
= m
; /* save pointer to last control buffer */
1128 for (m
= m0
; m
; m
= m
->m_next
)
1130 if (space
> sbspace(sb
) && !(sb
->sb_flags
& SB_UNIX
))
1132 n
->m_next
= m0
; /* concatenate data to control */
1134 SBLASTRECORDCHK(sb
, "sbappendcontrol 1");
1136 for (m
= control
; m
->m_next
!= NULL
; m
= m
->m_next
)
1141 if (sb
->sb_lastrecord
!= NULL
) {
1142 sb
->sb_lastrecord
->m_nextpkt
= control
;
1144 sb
->sb_mb
= control
;
1146 sb
->sb_lastrecord
= control
;
1147 sb
->sb_mbtail
= mlast
;
1149 SBLASTMBUFCHK(sb
, __func__
);
1150 SBLASTRECORDCHK(sb
, "sbappendcontrol 2");
1152 postevent(0, sb
, EV_RWBYTES
);
1157 sbappendcontrol(struct sockbuf
*sb
, struct mbuf
*m0
, struct mbuf
*control
,
1161 boolean_t sb_unix
= (sb
->sb_flags
& SB_UNIX
);
1166 if (sb
->sb_flags
& SB_DROP
) {
1169 if (control
!= NULL
&& !sb_unix
)
1171 if (error_out
!= NULL
)
1172 *error_out
= EINVAL
;
1176 if (sb
->sb_flags
& SB_RECV
) {
1179 error
= sflt_data_in(sb
->sb_so
, NULL
, &m0
, &control
, 0);
1180 SBLASTRECORDCHK(sb
, __func__
);
1182 if (error
!= EJUSTRETURN
) {
1185 if (control
!= NULL
&& !sb_unix
)
1194 result
= sbappendcontrol_internal(sb
, m0
, control
);
1198 if (control
!= NULL
&& !sb_unix
)
1201 *error_out
= ENOBUFS
;
1208 * Compress mbuf chain m into the socket
1209 * buffer sb following mbuf n. If n
1210 * is null, the buffer is presumed empty.
1213 sbcompress(struct sockbuf
*sb
, struct mbuf
*m
, struct mbuf
*n
)
1219 /* There is nothing to compress; just update the tail */
1220 for (; n
->m_next
!= NULL
; n
= n
->m_next
)
1227 eor
|= m
->m_flags
& M_EOR
;
1228 if (m
->m_len
== 0 && (eor
== 0 ||
1229 (((o
= m
->m_next
) || (o
= n
)) && o
->m_type
== m
->m_type
))) {
1230 if (sb
->sb_lastrecord
== m
)
1231 sb
->sb_lastrecord
= m
->m_next
;
1235 if (n
&& (n
->m_flags
& M_EOR
) == 0 &&
1239 m
->m_len
<= MCLBYTES
/ 4 && /* XXX: Don't copy too much */
1240 m
->m_len
<= M_TRAILINGSPACE(n
) &&
1241 n
->m_type
== m
->m_type
) {
1242 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
) + n
->m_len
,
1243 (unsigned)m
->m_len
);
1244 n
->m_len
+= m
->m_len
;
1245 sb
->sb_cc
+= m
->m_len
;
1246 if (m
->m_type
!= MT_DATA
&& m
->m_type
!= MT_HEADER
&&
1247 m
->m_type
!= MT_OOBDATA
)
1248 /* XXX: Probably don't need.*/
1249 sb
->sb_ctl
+= m
->m_len
;
1260 m
->m_flags
&= ~M_EOR
;
1268 printf("semi-panic: sbcompress\n");
1271 SBLASTMBUFCHK(sb
, __func__
);
1272 postevent(0, sb
, EV_RWBYTES
);
1276 sb_empty_assert(struct sockbuf
*sb
, const char *where
)
1278 if (!(sb
->sb_cc
== 0 && sb
->sb_mb
== NULL
&& sb
->sb_mbcnt
== 0 &&
1279 sb
->sb_mbtail
== NULL
&& sb
->sb_lastrecord
== NULL
)) {
1280 panic("%s: sb %p so %p cc %d mbcnt %d mb %p mbtail %p "
1281 "lastrecord %p\n", where
, sb
, sb
->sb_so
, sb
->sb_cc
,
1282 sb
->sb_mbcnt
, sb
->sb_mb
, sb
->sb_mbtail
, sb
->sb_lastrecord
);
1288 * Free all mbufs in a sockbuf.
1289 * Check that all resources are reclaimed.
1292 sbflush(struct sockbuf
*sb
)
1294 if (sb
->sb_so
== NULL
)
1295 panic("sbflush sb->sb_so already null sb=%p\n", sb
);
1296 (void) sblock(sb
, M_WAIT
);
1297 while (sb
->sb_mbcnt
) {
1299 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
1300 * we would loop forever. Panic instead.
1302 if (!sb
->sb_cc
&& (sb
->sb_mb
== NULL
|| sb
->sb_mb
->m_len
))
1304 sbdrop(sb
, (int)sb
->sb_cc
);
1306 sb_empty_assert(sb
, __func__
);
1307 postevent(0, sb
, EV_RWBYTES
);
1308 sbunlock(sb
, 1); /* keep socket locked */
1313 * Drop data from (the front of) a sockbuf.
1314 * use m_freem_list to free the mbuf structures
1315 * under a single lock... this is done by pruning
1316 * the top of the tree from the body by keeping track
1317 * of where we get to in the tree and then zeroing the
1318 * two pertinent pointers m_nextpkt and m_next
1319 * the socket buffer is then updated to point at the new
1320 * top of the tree and the pruned area is released via
1324 sbdrop(struct sockbuf
*sb
, int len
)
1326 struct mbuf
*m
, *free_list
, *ml
;
1327 struct mbuf
*next
, *last
;
1329 KERNEL_DEBUG((DBG_FNC_SBDROP
| DBG_FUNC_START
), sb
, len
, 0, 0, 0);
1331 next
= (m
= sb
->sb_mb
) ? m
->m_nextpkt
: 0;
1332 free_list
= last
= m
;
1333 ml
= (struct mbuf
*)0;
1339 * temporarily replacing this panic with printf
1340 * because it occurs occasionally when closing
1341 * a socket when there is no harm in ignoring
1342 * it. This problem will be investigated
1345 /* panic("sbdrop"); */
1346 printf("sbdrop - count not zero\n");
1349 * zero the counts. if we have no mbufs,
1350 * we have no data (PR-2986815)
1357 next
= m
->m_nextpkt
;
1360 if (m
->m_len
> len
) {
1364 if (m
->m_type
!= MT_DATA
&& m
->m_type
!= MT_HEADER
&&
1365 m
->m_type
!= MT_OOBDATA
)
1375 while (m
&& m
->m_len
== 0) {
1382 ml
->m_next
= (struct mbuf
*)0;
1383 last
->m_nextpkt
= (struct mbuf
*)0;
1384 m_freem_list(free_list
);
1388 m
->m_nextpkt
= next
;
1394 * First part is an inline SB_EMPTY_FIXUP(). Second part
1395 * makes sure sb_lastrecord is up-to-date if we dropped
1396 * part of the last record.
1400 sb
->sb_mbtail
= NULL
;
1401 sb
->sb_lastrecord
= NULL
;
1402 } else if (m
->m_nextpkt
== NULL
) {
1403 sb
->sb_lastrecord
= m
;
1406 postevent(0, sb
, EV_RWBYTES
);
1408 KERNEL_DEBUG((DBG_FNC_SBDROP
| DBG_FUNC_END
), sb
, 0, 0, 0, 0);
1412 * Drop a record off the front of a sockbuf
1413 * and move the next record to the front.
1416 sbdroprecord(struct sockbuf
*sb
)
1418 struct mbuf
*m
, *mn
;
1422 sb
->sb_mb
= m
->m_nextpkt
;
1430 postevent(0, sb
, EV_RWBYTES
);
1434 * Create a "control" mbuf containing the specified data
1435 * with the specified type for presentation on a socket buffer.
1438 sbcreatecontrol(caddr_t p
, int size
, int type
, int level
)
1443 if (CMSG_SPACE((u_int
)size
) > MLEN
)
1444 return ((struct mbuf
*)NULL
);
1445 if ((m
= m_get(M_DONTWAIT
, MT_CONTROL
)) == NULL
)
1446 return ((struct mbuf
*)NULL
);
1447 cp
= mtod(m
, struct cmsghdr
*);
1448 /* XXX check size? */
1449 (void) memcpy(CMSG_DATA(cp
), p
, size
);
1450 m
->m_len
= CMSG_SPACE(size
);
1451 cp
->cmsg_len
= CMSG_LEN(size
);
1452 cp
->cmsg_level
= level
;
1453 cp
->cmsg_type
= type
;
1458 sbcreatecontrol_mbuf(caddr_t p
, int size
, int type
, int level
, struct mbuf
** mp
)
1464 *mp
= sbcreatecontrol(p
, size
, type
, level
);
1468 if (CMSG_SPACE((u_int
)size
) + (*mp
)->m_len
> MLEN
){
1469 mp
= &(*mp
)->m_next
;
1470 *mp
= sbcreatecontrol(p
, size
, type
, level
);
1476 cp
= (struct cmsghdr
*) (mtod(m
, char *) + m
->m_len
);
1477 m
->m_len
+= CMSG_SPACE(size
);
1479 /* XXX check size? */
1480 (void) memcpy(CMSG_DATA(cp
), p
, size
);
1481 cp
->cmsg_len
= CMSG_LEN(size
);
1482 cp
->cmsg_level
= level
;
1483 cp
->cmsg_type
= type
;
1490 * Some routines that return EOPNOTSUPP for entry points that are not
1491 * supported by a protocol. Fill in as needed.
1494 pru_abort_notsupp(__unused
struct socket
*so
)
1496 return (EOPNOTSUPP
);
1500 pru_accept_notsupp(__unused
struct socket
*so
, __unused
struct sockaddr
**nam
)
1502 return (EOPNOTSUPP
);
1506 pru_attach_notsupp(__unused
struct socket
*so
, __unused
int proto
,
1507 __unused
struct proc
*p
)
1509 return (EOPNOTSUPP
);
1513 pru_bind_notsupp(__unused
struct socket
*so
, __unused
struct sockaddr
*nam
,
1514 __unused
struct proc
*p
)
1516 return (EOPNOTSUPP
);
1520 pru_connect_notsupp(__unused
struct socket
*so
, __unused
struct sockaddr
*nam
,
1521 __unused
struct proc
*p
)
1523 return (EOPNOTSUPP
);
1527 pru_connect2_notsupp(__unused
struct socket
*so1
, __unused
struct socket
*so2
)
1529 return (EOPNOTSUPP
);
1533 pru_control_notsupp(__unused
struct socket
*so
, __unused u_long cmd
,
1534 __unused caddr_t data
, __unused
struct ifnet
*ifp
, __unused
struct proc
*p
)
1536 return (EOPNOTSUPP
);
1540 pru_detach_notsupp(__unused
struct socket
*so
)
1542 return (EOPNOTSUPP
);
1546 pru_disconnect_notsupp(__unused
struct socket
*so
)
1548 return (EOPNOTSUPP
);
1552 pru_listen_notsupp(__unused
struct socket
*so
, __unused
struct proc
*p
)
1554 return (EOPNOTSUPP
);
1558 pru_peeraddr_notsupp(__unused
struct socket
*so
, __unused
struct sockaddr
**nam
)
1560 return (EOPNOTSUPP
);
1564 pru_rcvd_notsupp(__unused
struct socket
*so
, __unused
int flags
)
1566 return (EOPNOTSUPP
);
1570 pru_rcvoob_notsupp(__unused
struct socket
*so
, __unused
struct mbuf
*m
,
1573 return (EOPNOTSUPP
);
1577 pru_send_notsupp(__unused
struct socket
*so
, __unused
int flags
,
1578 __unused
struct mbuf
*m
, __unused
struct sockaddr
*addr
,
1579 __unused
struct mbuf
*control
, __unused
struct proc
*p
)
1582 return (EOPNOTSUPP
);
1587 * This isn't really a ``null'' operation, but it's the default one
1588 * and doesn't do anything destructive.
1591 pru_sense_null(struct socket
*so
, void *ub
, int isstat64
)
1593 if (isstat64
!= 0) {
1594 struct stat64
*sb64
;
1596 sb64
= (struct stat64
*)ub
;
1597 sb64
->st_blksize
= so
->so_snd
.sb_hiwat
;
1601 sb
= (struct stat
*)ub
;
1602 sb
->st_blksize
= so
->so_snd
.sb_hiwat
;
1610 pru_sosend_notsupp(__unused
struct socket
*so
, __unused
struct sockaddr
*addr
,
1611 __unused
struct uio
*uio
, __unused
struct mbuf
*top
,
1612 __unused
struct mbuf
*control
, __unused
int flags
)
1615 return (EOPNOTSUPP
);
1619 pru_soreceive_notsupp(__unused
struct socket
*so
,
1620 __unused
struct sockaddr
**paddr
,
1621 __unused
struct uio
*uio
, __unused
struct mbuf
**mp0
,
1622 __unused
struct mbuf
**controlp
, __unused
int *flagsp
)
1624 return (EOPNOTSUPP
);
1628 pru_shutdown_notsupp(__unused
struct socket
*so
)
1630 return (EOPNOTSUPP
);
1634 pru_sockaddr_notsupp(__unused
struct socket
*so
, __unused
struct sockaddr
**nam
)
1636 return (EOPNOTSUPP
);
1640 pru_sopoll_notsupp(__unused
struct socket
*so
, __unused
int events
,
1641 __unused kauth_cred_t cred
, __unused
void *wql
)
1643 return (EOPNOTSUPP
);
1649 * The following are macros on BSD and functions on Darwin
1653 * Do we need to notify the other side when I/O is possible?
1657 sb_notify(struct sockbuf
*sb
)
1659 return ((sb
->sb_flags
&
1660 (SB_WAIT
|SB_SEL
|SB_ASYNC
|SB_UPCALL
|SB_KNOTE
)) != 0);
1664 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
1665 * This is problematical if the fields are unsigned, as the space might
1666 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
1667 * overflow and return 0.
1670 sbspace(struct sockbuf
*sb
)
1673 imin((int)(sb
->sb_hiwat
- sb
->sb_cc
),
1674 (int)(sb
->sb_mbmax
- sb
->sb_mbcnt
));
1681 /* do we have to send all at once on a socket? */
1683 sosendallatonce(struct socket
*so
)
1685 return (so
->so_proto
->pr_flags
& PR_ATOMIC
);
1688 /* can we read something from so? */
1690 soreadable(struct socket
*so
)
1692 return (so
->so_rcv
.sb_cc
>= so
->so_rcv
.sb_lowat
||
1693 (so
->so_state
& SS_CANTRCVMORE
) ||
1694 so
->so_comp
.tqh_first
|| so
->so_error
);
1697 /* can we write something to so? */
1700 sowriteable(struct socket
*so
)
1702 return ((sbspace(&(so
)->so_snd
) >= (so
)->so_snd
.sb_lowat
&&
1703 ((so
->so_state
&SS_ISCONNECTED
) ||
1704 (so
->so_proto
->pr_flags
&PR_CONNREQUIRED
) == 0)) ||
1705 (so
->so_state
& SS_CANTSENDMORE
) ||
1709 /* adjust counters in sb reflecting allocation of m */
1712 sballoc(struct sockbuf
*sb
, struct mbuf
*m
)
1715 sb
->sb_cc
+= m
->m_len
;
1716 if (m
->m_type
!= MT_DATA
&& m
->m_type
!= MT_HEADER
&&
1717 m
->m_type
!= MT_OOBDATA
)
1718 sb
->sb_ctl
+= m
->m_len
;
1719 sb
->sb_mbcnt
+= MSIZE
;
1721 if (m
->m_flags
& M_EXT
) {
1722 sb
->sb_mbcnt
+= m
->m_ext
.ext_size
;
1723 cnt
+= m
->m_ext
.ext_size
/ MSIZE
;
1725 OSAddAtomic(cnt
, &total_mb_cnt
);
1728 /* adjust counters in sb reflecting freeing of m */
1730 sbfree(struct sockbuf
*sb
, struct mbuf
*m
)
1733 sb
->sb_cc
-= m
->m_len
;
1734 if (m
->m_type
!= MT_DATA
&& m
->m_type
!= MT_HEADER
&&
1735 m
->m_type
!= MT_OOBDATA
)
1736 sb
->sb_ctl
-= m
->m_len
;
1737 sb
->sb_mbcnt
-= MSIZE
;
1738 if (m
->m_flags
& M_EXT
) {
1739 sb
->sb_mbcnt
-= m
->m_ext
.ext_size
;
1740 cnt
-= m
->m_ext
.ext_size
/ MSIZE
;
1742 OSAddAtomic(cnt
, &total_mb_cnt
);
1746 * Set lock on sockbuf sb; sleep if lock is already held.
1747 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
1748 * Returns error without lock if sleep is interrupted.
1750 * Returns: 0 Success
1755 sblock(struct sockbuf
*sb
, int wf
)
1759 if (sb
->sb_flags
& SB_LOCK
)
1760 error
= (wf
== M_WAIT
) ? sb_lock(sb
) : EWOULDBLOCK
;
1762 sb
->sb_flags
|= SB_LOCK
;
1767 /* release lock on sockbuf sb */
1769 sbunlock(struct sockbuf
*sb
, int keeplocked
)
1771 struct socket
*so
= sb
->sb_so
;
1773 lck_mtx_t
*mutex_held
;
1775 lr_saved
= __builtin_return_address(0);
1777 sb
->sb_flags
&= ~SB_LOCK
;
1779 if (sb
->sb_flags
& SB_WANT
) {
1780 sb
->sb_flags
&= ~SB_WANT
;
1781 if (so
->so_usecount
< 0) {
1782 panic("sbunlock: b4 wakeup so=%p ref=%d lr=%p "
1783 "sb_flags=%x lrh= %s\n", sb
->sb_so
, so
->so_usecount
,
1784 lr_saved
, sb
->sb_flags
, solockhistory_nr(so
));
1787 wakeup((caddr_t
)&(sb
)->sb_flags
);
1789 if (keeplocked
== 0) { /* unlock on exit */
1790 if (so
->so_proto
->pr_getlock
!= NULL
)
1791 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
1793 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
1795 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
1798 if (so
->so_usecount
< 0)
1799 panic("sbunlock: unlock on exit so=%p ref=%d lr=%p "
1800 "sb_flags=%x lrh= %s\n", so
, so
->so_usecount
, lr_saved
,
1801 sb
->sb_flags
, solockhistory_nr(so
));
1802 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
1803 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
1804 lck_mtx_unlock(mutex_held
);
1809 sorwakeup(struct socket
*so
)
1811 if (sb_notify(&so
->so_rcv
))
1812 sowakeup(so
, &so
->so_rcv
);
1816 sowwakeup(struct socket
*so
)
1818 if (sb_notify(&so
->so_snd
))
1819 sowakeup(so
, &so
->so_snd
);
1821 #endif /* __APPLE__ */
1824 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
1827 dup_sockaddr(struct sockaddr
*sa
, int canwait
)
1829 struct sockaddr
*sa2
;
1831 MALLOC(sa2
, struct sockaddr
*, sa
->sa_len
, M_SONAME
,
1832 canwait
? M_WAITOK
: M_NOWAIT
);
1834 bcopy(sa
, sa2
, sa
->sa_len
);
1839 * Create an external-format (``xsocket'') structure using the information
1840 * in the kernel-format socket structure pointed to by so. This is done
1841 * to reduce the spew of irrelevant information over this interface,
1842 * to isolate user code from changes in the kernel structure, and
1843 * potentially to provide information-hiding if we decide that
1844 * some of this information should be hidden from users.
1847 sotoxsocket(struct socket
*so
, struct xsocket
*xso
)
1849 xso
->xso_len
= sizeof (*xso
);
1850 xso
->xso_so
= (_XSOCKET_PTR(struct socket
*))(uintptr_t)so
;
1851 xso
->so_type
= so
->so_type
;
1852 xso
->so_options
= so
->so_options
;
1853 xso
->so_linger
= so
->so_linger
;
1854 xso
->so_state
= so
->so_state
;
1855 xso
->so_pcb
= (_XSOCKET_PTR(caddr_t
))(uintptr_t)so
->so_pcb
;
1857 xso
->xso_protocol
= so
->so_proto
->pr_protocol
;
1858 xso
->xso_family
= so
->so_proto
->pr_domain
->dom_family
;
1860 xso
->xso_protocol
= xso
->xso_family
= 0;
1862 xso
->so_qlen
= so
->so_qlen
;
1863 xso
->so_incqlen
= so
->so_incqlen
;
1864 xso
->so_qlimit
= so
->so_qlimit
;
1865 xso
->so_timeo
= so
->so_timeo
;
1866 xso
->so_error
= so
->so_error
;
1867 xso
->so_pgid
= so
->so_pgid
;
1868 xso
->so_oobmark
= so
->so_oobmark
;
1869 sbtoxsockbuf(&so
->so_snd
, &xso
->so_snd
);
1870 sbtoxsockbuf(&so
->so_rcv
, &xso
->so_rcv
);
1871 xso
->so_uid
= so
->so_uid
;
1875 #if !CONFIG_EMBEDDED
1878 sotoxsocket64(struct socket
*so
, struct xsocket64
*xso
)
1880 xso
->xso_len
= sizeof (*xso
);
1881 xso
->xso_so
= (u_int64_t
)(uintptr_t)so
;
1882 xso
->so_type
= so
->so_type
;
1883 xso
->so_options
= so
->so_options
;
1884 xso
->so_linger
= so
->so_linger
;
1885 xso
->so_state
= so
->so_state
;
1886 xso
->so_pcb
= (u_int64_t
)(uintptr_t)so
->so_pcb
;
1888 xso
->xso_protocol
= so
->so_proto
->pr_protocol
;
1889 xso
->xso_family
= so
->so_proto
->pr_domain
->dom_family
;
1891 xso
->xso_protocol
= xso
->xso_family
= 0;
1893 xso
->so_qlen
= so
->so_qlen
;
1894 xso
->so_incqlen
= so
->so_incqlen
;
1895 xso
->so_qlimit
= so
->so_qlimit
;
1896 xso
->so_timeo
= so
->so_timeo
;
1897 xso
->so_error
= so
->so_error
;
1898 xso
->so_pgid
= so
->so_pgid
;
1899 xso
->so_oobmark
= so
->so_oobmark
;
1900 sbtoxsockbuf(&so
->so_snd
, &xso
->so_snd
);
1901 sbtoxsockbuf(&so
->so_rcv
, &xso
->so_rcv
);
1902 xso
->so_uid
= so
->so_uid
;
1905 #endif /* !CONFIG_EMBEDDED */
1908 * This does the same for sockbufs. Note that the xsockbuf structure,
1909 * since it is always embedded in a socket, does not include a self
1910 * pointer nor a length. We make this entry point public in case
1911 * some other mechanism needs it.
1914 sbtoxsockbuf(struct sockbuf
*sb
, struct xsockbuf
*xsb
)
1916 xsb
->sb_cc
= sb
->sb_cc
;
1917 xsb
->sb_hiwat
= sb
->sb_hiwat
;
1918 xsb
->sb_mbcnt
= sb
->sb_mbcnt
;
1919 xsb
->sb_mbmax
= sb
->sb_mbmax
;
1920 xsb
->sb_lowat
= sb
->sb_lowat
;
1921 xsb
->sb_flags
= sb
->sb_flags
;
1922 xsb
->sb_timeo
= (short)
1923 (sb
->sb_timeo
.tv_sec
* hz
) + sb
->sb_timeo
.tv_usec
/ tick
;
1924 if (xsb
->sb_timeo
== 0 && sb
->sb_timeo
.tv_usec
!= 0)
1929 soisbackground(struct socket
*so
)
1931 return (so
->so_traffic_mgt_flags
& TRAFFIC_MGT_SO_BACKGROUND
);
1936 * Here is the definition of some of the basic objects in the kern.ipc
1937 * branch of the MIB.
1939 SYSCTL_NODE(_kern
, KERN_IPC
, ipc
, CTLFLAG_RW
|CTLFLAG_LOCKED
|CTLFLAG_ANYBODY
, 0, "IPC");
1941 /* Check that the maximum socket buffer size is within a range */
1944 sysctl_sb_max(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
1945 __unused
int arg2
, struct sysctl_req
*req
)
1947 u_int32_t new_value
;
1949 int error
= sysctl_io_number(req
, sb_max
, sizeof(u_int32_t
), &new_value
,
1951 if (!error
&& changed
) {
1952 if (new_value
> LOW_SB_MAX
&&
1953 new_value
<= high_sb_max
) {
1962 SYSCTL_PROC(_kern_ipc
, KIPC_MAXSOCKBUF
, maxsockbuf
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
1963 &sb_max
, 0, &sysctl_sb_max
, "IU", "Maximum socket buffer size");
1965 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxsockets
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
1966 &maxsockets
, 0, "Maximum number of sockets avaliable");
1967 SYSCTL_INT(_kern_ipc
, KIPC_SOCKBUF_WASTE
, sockbuf_waste_factor
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
1968 &sb_efficiency
, 0, "");
1969 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sbspace_factor
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
1970 &sbspace_factor
, 0, "Ratio of mbuf/cluster use for socket layers");
1971 SYSCTL_INT(_kern_ipc
, KIPC_NMBCLUSTERS
, nmbclusters
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
1972 &nmbclusters
, 0, "");
1973 SYSCTL_INT(_kern_ipc
, OID_AUTO
, njcl
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &njcl
, 0, "");
1974 SYSCTL_INT(_kern_ipc
, OID_AUTO
, njclbytes
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &njclbytes
, 0, "");
1975 SYSCTL_INT(_kern_ipc
, KIPC_SOQLIMITCOMPAT
, soqlimitcompat
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
1976 &soqlimitcompat
, 1, "Enable socket queue limit compatibility");
1977 SYSCTL_INT(_kern_ipc
, OID_AUTO
, soqlencomp
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
1978 &soqlencomp
, 0, "Listen backlog represents only complete queue");