2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
31 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
33 * Copyright (c) 1982, 1986, 1988, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
65 * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.16 2001/06/14 20:46:06 ume Exp $
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/filedesc.h>
71 #include <sys/proc_internal.h>
72 #include <sys/kauth.h>
73 #include <sys/file_internal.h>
74 #include <sys/fcntl.h>
75 #include <sys/malloc.h>
77 #include <sys/domain.h>
78 #include <sys/kernel.h>
79 #include <sys/event.h>
81 #include <sys/protosw.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/resourcevar.h>
85 #include <sys/signalvar.h>
86 #include <sys/sysctl.h>
89 #include <sys/kdebug.h>
90 #include <net/route.h>
91 #include <netinet/in.h>
92 #include <netinet/in_pcb.h>
93 #include <kern/zalloc.h>
94 #include <kern/locks.h>
95 #include <machine/limits.h>
98 int so_cache_timeouts
= 0;
99 int so_cache_max_freed
= 0;
100 int cached_sock_count
= 0;
101 struct socket
*socket_cache_head
= 0;
102 struct socket
*socket_cache_tail
= 0;
103 u_long so_cache_time
= 0;
104 int so_cache_init_done
= 0;
105 struct zone
*so_cache_zone
;
106 extern int get_inpcb_str_size();
107 extern int get_tcp_str_size();
109 static lck_grp_t
*so_cache_mtx_grp
;
110 static lck_attr_t
*so_cache_mtx_attr
;
111 static lck_grp_attr_t
*so_cache_mtx_grp_attr
;
112 lck_mtx_t
*so_cache_mtx
;
114 #include <machine/limits.h>
116 static void filt_sordetach(struct knote
*kn
);
117 static int filt_soread(struct knote
*kn
, long hint
);
118 static void filt_sowdetach(struct knote
*kn
);
119 static int filt_sowrite(struct knote
*kn
, long hint
);
120 static int filt_solisten(struct knote
*kn
, long hint
);
122 static struct filterops solisten_filtops
=
123 { 1, NULL
, filt_sordetach
, filt_solisten
};
124 static struct filterops soread_filtops
=
125 { 1, NULL
, filt_sordetach
, filt_soread
};
126 static struct filterops sowrite_filtops
=
127 { 1, NULL
, filt_sowdetach
, filt_sowrite
};
129 #define EVEN_MORE_LOCKING_DEBUG 0
130 int socket_debug
= 0;
131 int socket_zone
= M_SOCKET
;
132 so_gen_t so_gencnt
; /* generation count for sockets */
134 MALLOC_DEFINE(M_SONAME
, "soname", "socket name");
135 MALLOC_DEFINE(M_PCB
, "pcb", "protocol control block");
137 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0)
138 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2)
139 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1)
140 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3)
141 #define DBG_FNC_SOSEND NETDBG_CODE(DBG_NETSOCK, (4 << 8) | 1)
142 #define DBG_FNC_SORECEIVE NETDBG_CODE(DBG_NETSOCK, (8 << 8))
143 #define DBG_FNC_SOSHUTDOWN NETDBG_CODE(DBG_NETSOCK, (9 << 8))
145 #define MAX_SOOPTGETM_SIZE (128 * MCLBYTES)
148 SYSCTL_DECL(_kern_ipc
);
150 static int somaxconn
= SOMAXCONN
;
151 SYSCTL_INT(_kern_ipc
, KIPC_SOMAXCONN
, somaxconn
, CTLFLAG_RW
, &somaxconn
,
154 /* Should we get a maximum also ??? */
155 static int sosendmaxchain
= 65536;
156 static int sosendminchain
= 16384;
157 static int sorecvmincopy
= 16384;
158 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sosendminchain
, CTLFLAG_RW
, &sosendminchain
,
160 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sorecvmincopy
, CTLFLAG_RW
, &sorecvmincopy
,
163 void so_cache_timer();
166 * Socket operation routines.
167 * These routines are called by the routines in
168 * sys_socket.c or from a system process, and
169 * implement the semantics of socket operations by
170 * switching out to the protocol specific routines.
175 vm_size_t so_cache_zone_element_size
;
177 static int sodelayed_copy(struct socket
*so
, struct uio
*uio
, struct mbuf
**free_list
, int *resid
);
184 if (so_cache_init_done
) {
185 printf("socketinit: already called...\n");
190 * allocate lock group attribute and group for socket cache mutex
192 so_cache_mtx_grp_attr
= lck_grp_attr_alloc_init();
194 so_cache_mtx_grp
= lck_grp_alloc_init("so_cache", so_cache_mtx_grp_attr
);
197 * allocate the lock attribute for socket cache mutex
199 so_cache_mtx_attr
= lck_attr_alloc_init();
201 so_cache_init_done
= 1;
203 so_cache_mtx
= lck_mtx_alloc_init(so_cache_mtx_grp
, so_cache_mtx_attr
); /* cached sockets mutex */
205 if (so_cache_mtx
== NULL
)
206 return; /* we're hosed... */
208 str_size
= (vm_size_t
)( sizeof(struct socket
) + 4 +
209 get_inpcb_str_size() + 4 +
211 so_cache_zone
= zinit (str_size
, 120000*str_size
, 8192, "socache zone");
213 printf("cached_sock_alloc -- so_cache_zone size is %x\n", str_size
);
215 timeout(so_cache_timer
, NULL
, (SO_CACHE_FLUSH_INTERVAL
* hz
));
217 so_cache_zone_element_size
= str_size
;
223 void cached_sock_alloc(so
, waitok
)
229 register u_long offset
;
232 lck_mtx_lock(so_cache_mtx
);
234 if (cached_sock_count
) {
236 *so
= socket_cache_head
;
238 panic("cached_sock_alloc: cached sock is null");
240 socket_cache_head
= socket_cache_head
->cache_next
;
241 if (socket_cache_head
)
242 socket_cache_head
->cache_prev
= 0;
244 socket_cache_tail
= 0;
246 lck_mtx_unlock(so_cache_mtx
);
248 temp
= (*so
)->so_saved_pcb
;
249 bzero((caddr_t
)*so
, sizeof(struct socket
));
251 kprintf("cached_sock_alloc - retreiving cached sock %x - count == %d\n", *so
,
254 (*so
)->so_saved_pcb
= temp
;
255 (*so
)->cached_in_sock_layer
= 1;
260 kprintf("Allocating cached sock %x from memory\n", *so
);
263 lck_mtx_unlock(so_cache_mtx
);
266 *so
= (struct socket
*) zalloc(so_cache_zone
);
268 *so
= (struct socket
*) zalloc_noblock(so_cache_zone
);
273 bzero((caddr_t
)*so
, sizeof(struct socket
));
276 * Define offsets for extra structures into our single block of
277 * memory. Align extra structures on longword boundaries.
281 offset
= (u_long
) *so
;
282 offset
+= sizeof(struct socket
);
285 offset
&= 0xfffffffc;
287 (*so
)->so_saved_pcb
= (caddr_t
) offset
;
288 offset
+= get_inpcb_str_size();
291 offset
&= 0xfffffffc;
294 ((struct inpcb
*) (*so
)->so_saved_pcb
)->inp_saved_ppcb
= (caddr_t
) offset
;
296 kprintf("Allocating cached socket - %x, pcb=%x tcpcb=%x\n", *so
,
298 ((struct inpcb
*)(*so
)->so_saved_pcb
)->inp_saved_ppcb
);
302 (*so
)->cached_in_sock_layer
= 1;
306 void cached_sock_free(so
)
310 lck_mtx_lock(so_cache_mtx
);
312 if (++cached_sock_count
> MAX_CACHED_SOCKETS
) {
314 lck_mtx_unlock(so_cache_mtx
);
316 kprintf("Freeing overflowed cached socket %x\n", so
);
318 zfree(so_cache_zone
, so
);
322 kprintf("Freeing socket %x into cache\n", so
);
324 if (so_cache_hw
< cached_sock_count
)
325 so_cache_hw
= cached_sock_count
;
327 so
->cache_next
= socket_cache_head
;
329 if (socket_cache_head
)
330 socket_cache_head
->cache_prev
= so
;
332 socket_cache_tail
= so
;
334 so
->cache_timestamp
= so_cache_time
;
335 socket_cache_head
= so
;
336 lck_mtx_unlock(so_cache_mtx
);
340 kprintf("Freed cached sock %x into cache - count is %d\n", so
, cached_sock_count
);
347 void so_cache_timer()
349 register struct socket
*p
;
350 register int n_freed
= 0;
353 lck_mtx_lock(so_cache_mtx
);
357 while ( (p
= socket_cache_tail
) )
359 if ((so_cache_time
- p
->cache_timestamp
) < SO_CACHE_TIME_LIMIT
)
364 if ( (socket_cache_tail
= p
->cache_prev
) )
365 p
->cache_prev
->cache_next
= 0;
366 if (--cached_sock_count
== 0)
367 socket_cache_head
= 0;
370 zfree(so_cache_zone
, p
);
372 if (++n_freed
>= SO_CACHE_MAX_FREE_BATCH
)
374 so_cache_max_freed
++;
378 lck_mtx_unlock(so_cache_mtx
);
380 timeout(so_cache_timer
, NULL
, (SO_CACHE_FLUSH_INTERVAL
* hz
));
384 #endif /* __APPLE__ */
387 * Get a socket structure from our zone, and initialize it.
388 * We don't implement `waitok' yet (see comments in uipc_domain.c).
389 * Note that it would probably be better to allocate socket
390 * and PCB at the same time, but I'm not convinced that all
391 * the protocols can be easily modified to do this.
394 soalloc(waitok
, dom
, type
)
401 if ((dom
== PF_INET
) && (type
== SOCK_STREAM
))
402 cached_sock_alloc(&so
, waitok
);
405 MALLOC_ZONE(so
, struct socket
*, sizeof(*so
), socket_zone
, M_WAITOK
);
407 bzero(so
, sizeof *so
);
409 /* XXX race condition for reentrant kernel */
410 //###LD Atomic add for so_gencnt
412 so
->so_gencnt
= ++so_gencnt
;
413 so
->so_zone
= socket_zone
;
420 socreate(dom
, aso
, type
, proto
)
426 struct proc
*p
= current_proc();
427 register struct protosw
*prp
;
428 register struct socket
*so
;
429 register int error
= 0;
431 extern int tcpconsdebug
;
434 prp
= pffindproto(dom
, proto
, type
);
436 prp
= pffindtype(dom
, type
);
438 if (prp
== 0 || prp
->pr_usrreqs
->pru_attach
== 0)
439 return (EPROTONOSUPPORT
);
442 if (p
->p_prison
&& jail_socket_unixiproute_only
&&
443 prp
->pr_domain
->dom_family
!= PF_LOCAL
&&
444 prp
->pr_domain
->dom_family
!= PF_INET
&&
445 prp
->pr_domain
->dom_family
!= PF_ROUTE
) {
446 return (EPROTONOSUPPORT
);
450 if (prp
->pr_type
!= type
)
452 so
= soalloc(p
!= 0, dom
, type
);
456 TAILQ_INIT(&so
->so_incomp
);
457 TAILQ_INIT(&so
->so_comp
);
462 so
->so_uid
= kauth_cred_getuid(kauth_cred_get());
463 if (!suser(kauth_cred_get(),NULL
))
464 so
->so_state
= SS_PRIV
;
467 so
->so_cred
= kauth_cred_get_with_ref();
471 so
->so_rcv
.sb_flags
|= SB_RECV
; /* XXX */
472 so
->so_rcv
.sb_so
= so
->so_snd
.sb_so
= so
;
474 so
->next_lock_lr
= 0;
475 so
->next_unlock_lr
= 0;
478 //### Attachement will create the per pcb lock if necessary and increase refcount
479 so
->so_usecount
++; /* for creation, make sure it's done before socket is inserted in lists */
481 error
= (*prp
->pr_usrreqs
->pru_attach
)(so
, proto
, p
);
485 * If so_pcb is not zero, the socket will be leaked,
486 * so protocol attachment handler must be coded carefuly
488 so
->so_state
|= SS_NOFDREF
;
490 sofreelastref(so
, 1); /* will deallocate the socket */
494 prp
->pr_domain
->dom_refs
++;
495 TAILQ_INIT(&so
->so_evlist
);
497 /* Attach socket filters for this protocol */
500 if (tcpconsdebug
== 2)
501 so
->so_options
|= SO_DEBUG
;
512 struct sockaddr
*nam
;
515 struct proc
*p
= current_proc();
517 struct socket_filter_entry
*filter
;
524 for (filter
= so
->so_filt
; filter
&& (error
== 0);
525 filter
= filter
->sfe_next_onsocket
) {
526 if (filter
->sfe_filter
->sf_filter
.sf_bind
) {
530 socket_unlock(so
, 0);
532 error
= filter
->sfe_filter
->sf_filter
.sf_bind(
533 filter
->sfe_cookie
, so
, nam
);
540 /* End socket filter */
543 error
= (*so
->so_proto
->pr_usrreqs
->pru_bind
)(so
, nam
, p
);
545 socket_unlock(so
, 1);
547 if (error
== EJUSTRETURN
)
557 so
->so_gencnt
= ++so_gencnt
;
560 if (so
->so_rcv
.sb_hiwat
)
561 (void)chgsbsize(so
->so_cred
->cr_uidinfo
,
562 &so
->so_rcv
.sb_hiwat
, 0, RLIM_INFINITY
);
563 if (so
->so_snd
.sb_hiwat
)
564 (void)chgsbsize(so
->so_cred
->cr_uidinfo
,
565 &so
->so_snd
.sb_hiwat
, 0, RLIM_INFINITY
);
567 if (so
->so_accf
!= NULL
) {
568 if (so
->so_accf
->so_accept_filter
!= NULL
&&
569 so
->so_accf
->so_accept_filter
->accf_destroy
!= NULL
) {
570 so
->so_accf
->so_accept_filter
->accf_destroy(so
);
572 if (so
->so_accf
->so_accept_filter_str
!= NULL
)
573 FREE(so
->so_accf
->so_accept_filter_str
, M_ACCF
);
574 FREE(so
->so_accf
, M_ACCF
);
577 kauth_cred_rele(so
->so_cred
);
578 zfreei(so
->so_zone
, so
);
580 if (so
->cached_in_sock_layer
== 1)
581 cached_sock_free(so
);
583 if (so
->cached_in_sock_layer
== -1)
584 panic("sodealloc: double dealloc: so=%x\n", so
);
585 so
->cached_in_sock_layer
= -1;
586 FREE_ZONE(so
, sizeof(*so
), so
->so_zone
);
588 #endif /* __APPLE__ */
592 solisten(so
, backlog
)
593 register struct socket
*so
;
597 struct proc
*p
= current_proc();
603 struct socket_filter_entry
*filter
;
606 for (filter
= so
->so_filt
; filter
&& (error
== 0);
607 filter
= filter
->sfe_next_onsocket
) {
608 if (filter
->sfe_filter
->sf_filter
.sf_listen
) {
612 socket_unlock(so
, 0);
614 error
= filter
->sfe_filter
->sf_filter
.sf_listen(
615 filter
->sfe_cookie
, so
);
625 error
= (*so
->so_proto
->pr_usrreqs
->pru_listen
)(so
, p
);
629 socket_unlock(so
, 1);
630 if (error
== EJUSTRETURN
)
635 if (TAILQ_EMPTY(&so
->so_comp
))
636 so
->so_options
|= SO_ACCEPTCONN
;
637 if (backlog
< 0 || backlog
> somaxconn
)
639 so
->so_qlimit
= backlog
;
641 socket_unlock(so
, 1);
646 sofreelastref(so
, dealloc
)
647 register struct socket
*so
;
651 struct socket
*head
= so
->so_head
;
653 /*### Assume socket is locked */
655 /* Remove any filters - may be called more than once */
658 if ((!(so
->so_flags
& SOF_PCBCLEARING
)) || ((so
->so_state
& SS_NOFDREF
) == 0)) {
660 selthreadclear(&so
->so_snd
.sb_sel
);
661 selthreadclear(&so
->so_rcv
.sb_sel
);
662 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
663 so
->so_snd
.sb_flags
&= ~SB_UPCALL
;
668 socket_lock(head
, 1);
669 if (so
->so_state
& SS_INCOMP
) {
670 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
672 } else if (so
->so_state
& SS_COMP
) {
674 * We must not decommission a socket that's
675 * on the accept(2) queue. If we do, then
676 * accept(2) may hang after select(2) indicated
677 * that the listening socket was ready.
680 selthreadclear(&so
->so_snd
.sb_sel
);
681 selthreadclear(&so
->so_rcv
.sb_sel
);
682 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
683 so
->so_snd
.sb_flags
&= ~SB_UPCALL
;
685 socket_unlock(head
, 1);
688 panic("sofree: not queued");
691 so
->so_state
&= ~SS_INCOMP
;
693 socket_unlock(head
, 1);
696 selthreadclear(&so
->so_snd
.sb_sel
);
697 sbrelease(&so
->so_snd
);
701 /* 3932268: disable upcall */
702 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
703 so
->so_snd
.sb_flags
&= ~SB_UPCALL
;
710 * Close a socket on last file table reference removal.
711 * Initiate disconnect if connected.
712 * Free socket when disconnect complete.
716 register struct socket
*so
;
719 lck_mtx_t
* mutex_held
;
722 if (so
->so_usecount
== 0) {
723 panic("soclose: so=%x refcount=0\n", so
);
726 sflt_notify(so
, sock_evt_closing
, NULL
);
728 if ((so
->so_options
& SO_ACCEPTCONN
)) {
731 /* We do not want new connection to be added to the connection queues */
732 so
->so_options
&= ~SO_ACCEPTCONN
;
734 while ((sp
= TAILQ_FIRST(&so
->so_incomp
)) != NULL
) {
735 /* A bit tricky here. We need to keep
736 * a lock if it's a protocol global lock
737 * but we want the head, not the socket locked
738 * in the case of per-socket lock...
740 if (so
->so_proto
->pr_getlock
!= NULL
) {
741 socket_unlock(so
, 0);
745 if (so
->so_proto
->pr_getlock
!= NULL
) {
746 socket_unlock(sp
, 1);
751 while ((sp
= TAILQ_FIRST(&so
->so_comp
)) != NULL
) {
752 /* Dequeue from so_comp since sofree() won't do it */
753 TAILQ_REMOVE(&so
->so_comp
, sp
, so_list
);
756 if (so
->so_proto
->pr_getlock
!= NULL
) {
757 socket_unlock(so
, 0);
761 sp
->so_state
&= ~SS_COMP
;
765 if (so
->so_proto
->pr_getlock
!= NULL
) {
766 socket_unlock(sp
, 1);
771 if (so
->so_pcb
== 0) {
772 /* 3915887: mark the socket as ready for dealloc */
773 so
->so_flags
|= SOF_PCBCLEARING
;
776 if (so
->so_state
& SS_ISCONNECTED
) {
777 if ((so
->so_state
& SS_ISDISCONNECTING
) == 0) {
778 error
= sodisconnectlocked(so
);
782 if (so
->so_options
& SO_LINGER
) {
783 if ((so
->so_state
& SS_ISDISCONNECTING
) &&
784 (so
->so_state
& SS_NBIO
))
786 if (so
->so_proto
->pr_getlock
!= NULL
)
787 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
789 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
790 while (so
->so_state
& SS_ISCONNECTED
) {
791 ts
.tv_sec
= (so
->so_linger
/100);
792 ts
.tv_nsec
= (so
->so_linger
% 100) * NSEC_PER_USEC
* 1000 * 10;
793 error
= msleep((caddr_t
)&so
->so_timeo
, mutex_held
,
794 PSOCK
| PCATCH
, "soclos", &ts
);
796 /* It's OK when the time fires, don't report an error */
797 if (error
== EWOULDBLOCK
)
805 if (so
->so_usecount
== 0)
806 panic("soclose: usecount is zero so=%x\n", so
);
807 if (so
->so_pcb
&& !(so
->so_flags
& SOF_PCBCLEARING
)) {
808 int error2
= (*so
->so_proto
->pr_usrreqs
->pru_detach
)(so
);
812 if (so
->so_usecount
<= 0)
813 panic("soclose: usecount is zero so=%x\n", so
);
815 if (so
->so_pcb
&& so
->so_state
& SS_NOFDREF
)
816 panic("soclose: NOFDREF");
817 so
->so_state
|= SS_NOFDREF
;
819 so
->so_proto
->pr_domain
->dom_refs
--;
829 register struct socket
*so
;
833 if (so
->so_retaincnt
== 0)
834 error
= soclose_locked(so
);
835 else { /* if the FD is going away, but socket is retained in kernel remove its reference */
837 if (so
->so_usecount
< 2)
838 panic("soclose: retaincnt non null and so=%x usecount=%x\n", so
->so_usecount
);
840 socket_unlock(so
, 1);
846 * Must be called at splnet...
848 //#### Should already be locked
855 #ifdef MORE_LOCKING_DEBUG
856 lck_mtx_t
* mutex_held
;
858 if (so
->so_proto
->pr_getlock
!= NULL
)
859 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
861 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
862 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
865 error
= (*so
->so_proto
->pr_usrreqs
->pru_abort
)(so
);
874 soacceptlock(so
, nam
, dolock
)
875 register struct socket
*so
;
876 struct sockaddr
**nam
;
881 if (dolock
) socket_lock(so
, 1);
883 if ((so
->so_state
& SS_NOFDREF
) == 0)
884 panic("soaccept: !NOFDREF");
885 so
->so_state
&= ~SS_NOFDREF
;
886 error
= (*so
->so_proto
->pr_usrreqs
->pru_accept
)(so
, nam
);
888 if (dolock
) socket_unlock(so
, 1);
893 register struct socket
*so
;
894 struct sockaddr
**nam
;
896 return (soacceptlock(so
, nam
, 1));
900 soconnectlock(so
, nam
, dolock
)
901 register struct socket
*so
;
902 struct sockaddr
*nam
;
908 struct proc
*p
= current_proc();
910 if (dolock
) socket_lock(so
, 1);
912 if (so
->so_options
& SO_ACCEPTCONN
) {
913 if (dolock
) socket_unlock(so
, 1);
917 * If protocol is connection-based, can only connect once.
918 * Otherwise, if connected, try to disconnect first.
919 * This allows user to disconnect by connecting to, e.g.,
922 if (so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
) &&
923 ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) ||
924 (error
= sodisconnectlocked(so
))))
928 * Run connect filter before calling protocol:
929 * - non-blocking connect returns before completion;
932 struct socket_filter_entry
*filter
;
935 for (filter
= so
->so_filt
; filter
&& (error
== 0);
936 filter
= filter
->sfe_next_onsocket
) {
937 if (filter
->sfe_filter
->sf_filter
.sf_connect_out
) {
941 socket_unlock(so
, 0);
943 error
= filter
->sfe_filter
->sf_filter
.sf_connect_out(
944 filter
->sfe_cookie
, so
, nam
);
953 if (error
== EJUSTRETURN
)
955 if (dolock
) socket_unlock(so
, 1);
959 error
= (*so
->so_proto
->pr_usrreqs
->pru_connect
)(so
, nam
, p
);
961 if (dolock
) socket_unlock(so
, 1);
967 register struct socket
*so
;
968 struct sockaddr
*nam
;
970 return (soconnectlock(so
, nam
, 1));
975 register struct socket
*so1
;
981 if (so2
->so_proto
->pr_lock
)
984 error
= (*so1
->so_proto
->pr_usrreqs
->pru_connect2
)(so1
, so2
);
986 socket_unlock(so1
, 1);
987 if (so2
->so_proto
->pr_lock
)
988 socket_unlock(so2
, 1);
994 sodisconnectlocked(so
)
995 register struct socket
*so
;
999 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
1003 if (so
->so_state
& SS_ISDISCONNECTING
) {
1008 error
= (*so
->so_proto
->pr_usrreqs
->pru_disconnect
)(so
);
1011 sflt_notify(so
, sock_evt_disconnected
, NULL
);
1017 //### Locking version
1020 register struct socket
*so
;
1025 error
= sodisconnectlocked(so
);
1026 socket_unlock(so
, 1);
1030 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_DONTWAIT : M_WAIT)
1033 * sosendcheck will lock the socket buffer if it isn't locked and
1034 * verify that there is space for the data being inserted.
1040 struct sockaddr
*addr
,
1052 if (*sblocked
== 0) {
1053 if ((so
->so_snd
.sb_flags
& SB_LOCK
) != 0 &&
1054 so
->so_send_filt_thread
!= 0 &&
1055 so
->so_send_filt_thread
== current_thread()) {
1057 * We're being called recursively from a filter,
1058 * allow this to continue. Radar 4150520.
1059 * Don't set sblocked because we don't want
1060 * to perform an unlock later.
1065 error
= sblock(&so
->so_snd
, SBLOCKWAIT(flags
));
1073 if (so
->so_state
& SS_CANTSENDMORE
)
1077 error
= so
->so_error
;
1082 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
1084 * `sendto' and `sendmsg' is allowed on a connection-
1085 * based socket if it supports implied connect.
1086 * Return ENOTCONN if not connected and no address is
1089 if ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) &&
1090 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) == 0) {
1091 if ((so
->so_state
& SS_ISCONFIRMING
) == 0 &&
1092 !(resid
== 0 && clen
!= 0))
1094 } else if (addr
== 0 && !(flags
&MSG_HOLD
))
1095 return (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) ? ENOTCONN
: EDESTADDRREQ
;
1097 space
= sbspace(&so
->so_snd
);
1098 if (flags
& MSG_OOB
)
1100 if ((atomic
&& resid
> so
->so_snd
.sb_hiwat
) ||
1101 clen
> so
->so_snd
.sb_hiwat
)
1103 if (space
< resid
+ clen
&&
1104 (atomic
|| space
< so
->so_snd
.sb_lowat
|| space
< clen
)) {
1105 if ((so
->so_state
& SS_NBIO
) || (flags
& MSG_NBIO
) || assumelock
) {
1108 sbunlock(&so
->so_snd
, 1);
1109 error
= sbwait(&so
->so_snd
);
1121 * If send must go all at once and message is larger than
1122 * send buffering, then hard error.
1123 * Lock against other senders.
1124 * If must go all at once and not enough room now, then
1125 * inform user that this would block and do nothing.
1126 * Otherwise, if nonblocking, send as much as possible.
1127 * The data to be sent is described by "uio" if nonzero,
1128 * otherwise by the mbuf chain "top" (which must be null
1129 * if uio is not). Data provided in mbuf chain must be small
1130 * enough to send all at once.
1132 * Returns nonzero on error, timeout or signal; callers
1133 * must check for short counts if EINTR/ERESTART are returned.
1134 * Data and control buffers are freed on return.
1136 * MSG_HOLD: go thru most of sosend(), but just enqueue the mbuf
1137 * MSG_SEND: go thru as for MSG_HOLD on current fragment, then
1138 * point at the mbuf chain being constructed and go from there.
1141 sosend(so
, addr
, uio
, top
, control
, flags
)
1142 register struct socket
*so
;
1143 struct sockaddr
*addr
;
1146 struct mbuf
*control
;
1151 register struct mbuf
*m
, *freelist
= NULL
;
1152 register long space
, len
, resid
;
1153 int clen
= 0, error
, dontroute
, mlen
, sendflags
;
1154 int atomic
= sosendallatonce(so
) || top
;
1156 struct proc
*p
= current_proc();
1159 // LP64todo - fix this!
1160 resid
= uio_resid(uio
);
1162 resid
= top
->m_pkthdr
.len
;
1164 KERNEL_DEBUG((DBG_FNC_SOSEND
| DBG_FUNC_START
),
1168 so
->so_snd
.sb_lowat
,
1169 so
->so_snd
.sb_hiwat
);
1174 * In theory resid should be unsigned.
1175 * However, space must be signed, as it might be less than 0
1176 * if we over-committed, and we must use a signed comparison
1177 * of space and resid. On the other hand, a negative resid
1178 * causes us to loop sending 0-length segments to the protocol.
1180 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
1181 * type sockets since that's an error.
1183 if (resid
< 0 || (so
->so_type
== SOCK_STREAM
&& (flags
& MSG_EOR
))) {
1185 socket_unlock(so
, 1);
1190 (flags
& MSG_DONTROUTE
) && (so
->so_options
& SO_DONTROUTE
) == 0 &&
1191 (so
->so_proto
->pr_flags
& PR_ATOMIC
);
1193 p
->p_stats
->p_ru
.ru_msgsnd
++;
1195 clen
= control
->m_len
;
1198 error
= sosendcheck(so
, addr
, resid
, clen
, atomic
, flags
, &sblocked
);
1203 space
= sbspace(&so
->so_snd
) - clen
+ ((flags
& MSG_OOB
) ? 1024 : 0);
1209 * Data is prepackaged in "top".
1212 if (flags
& MSG_EOR
)
1213 top
->m_flags
|= M_EOR
;
1218 bytes_to_copy
= min(resid
, space
);
1220 if (sosendminchain
> 0) {
1223 chainlength
= sosendmaxchain
;
1225 socket_unlock(so
, 0);
1229 int hdrs_needed
= (top
== 0) ? 1 : 0;
1232 * try to maintain a local cache of mbuf clusters needed to complete this write
1233 * the list is further limited to the number that are currently needed to fill the socket
1234 * this mechanism allows a large number of mbufs/clusters to be grabbed under a single
1235 * mbuf lock... if we can't get any clusters, than fall back to trying for mbufs
1236 * if we fail early (or miscalcluate the number needed) make sure to release any clusters
1237 * we haven't yet consumed.
1239 if (freelist
== NULL
&& bytes_to_copy
> MCLBYTES
) {
1240 num_needed
= bytes_to_copy
/ NBPG
;
1242 if ((bytes_to_copy
- (num_needed
* NBPG
)) >= MINCLSIZE
)
1245 freelist
= m_getpackets_internal(&num_needed
, hdrs_needed
, M_WAIT
, 0, NBPG
);
1246 /* Fall back to cluster size if allocation failed */
1249 if (freelist
== NULL
&& bytes_to_copy
> MINCLSIZE
) {
1250 num_needed
= bytes_to_copy
/ MCLBYTES
;
1252 if ((bytes_to_copy
- (num_needed
* MCLBYTES
)) >= MINCLSIZE
)
1255 freelist
= m_getpackets_internal(&num_needed
, hdrs_needed
, M_WAIT
, 0, MCLBYTES
);
1256 /* Fall back to a single mbuf if allocation failed */
1259 if (freelist
== NULL
) {
1261 MGETHDR(freelist
, M_WAIT
, MT_DATA
);
1263 MGET(freelist
, M_WAIT
, MT_DATA
);
1265 if (freelist
== NULL
) {
1271 * For datagram protocols, leave room
1272 * for protocol headers in first mbuf.
1274 if (atomic
&& top
== 0 && bytes_to_copy
< MHLEN
)
1275 MH_ALIGN(freelist
, bytes_to_copy
);
1278 freelist
= m
->m_next
;
1281 if ((m
->m_flags
& M_EXT
))
1282 mlen
= m
->m_ext
.ext_size
;
1283 else if ((m
->m_flags
& M_PKTHDR
))
1284 mlen
= MHLEN
- m_leadingspace(m
);
1287 len
= min(mlen
, bytes_to_copy
);
1293 error
= uiomove(mtod(m
, caddr_t
), (int)len
, uio
);
1295 // LP64todo - fix this!
1296 resid
= uio_resid(uio
);
1300 top
->m_pkthdr
.len
+= len
;
1305 if (flags
& MSG_EOR
)
1306 top
->m_flags
|= M_EOR
;
1309 bytes_to_copy
= min(resid
, space
);
1311 } while (space
> 0 && (chainlength
< sosendmaxchain
|| atomic
|| resid
< MINCLSIZE
));
1319 if (flags
& (MSG_HOLD
|MSG_SEND
))
1321 /* Enqueue for later, go away if HOLD */
1322 register struct mbuf
*mb1
;
1323 if (so
->so_temp
&& (flags
& MSG_FLUSH
))
1325 m_freem(so
->so_temp
);
1329 so
->so_tail
->m_next
= top
;
1336 if (flags
& MSG_HOLD
)
1344 so
->so_options
|= SO_DONTROUTE
;
1345 /* Compute flags here, for pru_send and NKEs */
1346 sendflags
= (flags
& MSG_OOB
) ? PRUS_OOB
:
1348 * If the user set MSG_EOF, the protocol
1349 * understands this flag and nothing left to
1350 * send then use PRU_SEND_EOF instead of PRU_SEND.
1352 ((flags
& MSG_EOF
) &&
1353 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) &&
1356 /* If there is more to send set PRUS_MORETOCOME */
1357 (resid
> 0 && space
> 0) ? PRUS_MORETOCOME
: 0;
1360 * Socket filter processing
1363 struct socket_filter_entry
*filter
;
1368 for (filter
= so
->so_filt
; filter
&& (error
== 0);
1369 filter
= filter
->sfe_next_onsocket
) {
1370 if (filter
->sfe_filter
->sf_filter
.sf_data_out
) {
1372 if (filtered
== 0) {
1374 so
->so_send_filt_thread
= current_thread();
1376 socket_unlock(so
, 0);
1377 so_flags
= (sendflags
& MSG_OOB
) ? sock_data_filt_flag_oob
: 0;
1379 error
= filter
->sfe_filter
->sf_filter
.sf_data_out(
1380 filter
->sfe_cookie
, so
, addr
, &top
, &control
, so_flags
);
1386 * At this point, we've run at least one filter.
1387 * The socket is unlocked as is the socket buffer.
1391 so
->so_send_filt_thread
= 0;
1393 if (error
== EJUSTRETURN
) {
1405 * End Socket filter processing
1408 if (error
== EJUSTRETURN
) {
1409 /* A socket filter handled this data */
1413 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)(so
,
1414 sendflags
, top
, addr
, control
, p
);
1417 if (flags
& MSG_SEND
)
1421 so
->so_options
&= ~SO_DONTROUTE
;
1428 } while (resid
&& space
> 0);
1433 sbunlock(&so
->so_snd
, 0); /* will unlock socket */
1435 socket_unlock(so
, 1);
1442 m_freem_list(freelist
);
1444 KERNEL_DEBUG(DBG_FNC_SOSEND
| DBG_FUNC_END
,
1455 * Implement receive operations on a socket.
1456 * We depend on the way that records are added to the sockbuf
1457 * by sbappend*. In particular, each record (mbufs linked through m_next)
1458 * must begin with an address if the protocol so specifies,
1459 * followed by an optional mbuf or mbufs containing ancillary data,
1460 * and then zero or more mbufs of data.
1461 * In order to avoid blocking network interrupts for the entire time here,
1462 * we splx() while doing the actual copy to user space.
1463 * Although the sockbuf is locked, new data may still be appended,
1464 * and thus we must maintain consistency of the sockbuf during that time.
1466 * The caller may receive the data as a single mbuf chain by supplying
1467 * an mbuf **mp0 for use in returning the chain. The uio is then used
1468 * only for the count in uio_resid.
1471 soreceive(so
, psa
, uio
, mp0
, controlp
, flagsp
)
1472 register struct socket
*so
;
1473 struct sockaddr
**psa
;
1476 struct mbuf
**controlp
;
1479 register struct mbuf
*m
, **mp
, *ml
= NULL
;
1480 register int flags
, len
, error
, offset
;
1481 struct protosw
*pr
= so
->so_proto
;
1482 struct mbuf
*nextrecord
;
1484 // LP64todo - fix this!
1485 int orig_resid
= uio_resid(uio
);
1486 volatile struct mbuf
*free_list
;
1487 volatile int delayed_copy_len
;
1490 struct proc
*p
= current_proc();
1493 // LP64todo - fix this!
1494 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_START
,
1498 so
->so_rcv
.sb_lowat
,
1499 so
->so_rcv
.sb_hiwat
);
1503 #ifdef MORE_LOCKING_DEBUG
1504 if (so
->so_usecount
== 1)
1505 panic("soreceive: so=%x no other reference on socket\n", so
);
1513 flags
= *flagsp
&~ MSG_EOR
;
1517 * When SO_WANTOOBFLAG is set we try to get out-of-band data
1518 * regardless of the flags argument. Here is the case were
1519 * out-of-band data is not inline.
1521 if ((flags
& MSG_OOB
) ||
1522 ((so
->so_options
& SO_WANTOOBFLAG
) != 0 &&
1523 (so
->so_options
& SO_OOBINLINE
) == 0 &&
1524 (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
)))) {
1525 m
= m_get(M_WAIT
, MT_DATA
);
1527 socket_unlock(so
, 1);
1528 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, ENOBUFS
,0,0,0,0);
1531 error
= (*pr
->pr_usrreqs
->pru_rcvoob
)(so
, m
, flags
& MSG_PEEK
);
1534 socket_unlock(so
, 0);
1536 // LP64todo - fix this!
1537 error
= uiomove(mtod(m
, caddr_t
),
1538 (int) min(uio_resid(uio
), m
->m_len
), uio
);
1540 } while (uio_resid(uio
) && error
== 0 && m
);
1546 if ((so
->so_options
& SO_WANTOOBFLAG
) != 0) {
1547 if (error
== EWOULDBLOCK
|| error
== EINVAL
) {
1549 * Let's try to get normal data:
1550 * EWOULDBLOCK: out-of-band data not receive yet;
1551 * EINVAL: out-of-band data already read.
1555 } else if (error
== 0 && flagsp
)
1558 socket_unlock(so
, 1);
1559 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1565 *mp
= (struct mbuf
*)0;
1566 if (so
->so_state
& SS_ISCONFIRMING
&& uio_resid(uio
))
1567 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, 0);
1570 free_list
= (struct mbuf
*)0;
1571 delayed_copy_len
= 0;
1573 #ifdef MORE_LOCKING_DEBUG
1574 if (so
->so_usecount
<= 1)
1575 printf("soreceive: sblock so=%x ref=%d on socket\n", so
, so
->so_usecount
);
1577 error
= sblock(&so
->so_rcv
, SBLOCKWAIT(flags
));
1579 socket_unlock(so
, 1);
1580 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1584 m
= so
->so_rcv
.sb_mb
;
1586 * If we have less data than requested, block awaiting more
1587 * (subject to any timeout) if:
1588 * 1. the current count is less than the low water mark, or
1589 * 2. MSG_WAITALL is set, and it is possible to do the entire
1590 * receive operation at once if we block (resid <= hiwat).
1591 * 3. MSG_DONTWAIT is not set
1592 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1593 * we have to do the receive in sections, and thus risk returning
1594 * a short count if a timeout or signal occurs after we start.
1596 if (m
== 0 || (((flags
& MSG_DONTWAIT
) == 0 &&
1597 so
->so_rcv
.sb_cc
< uio_resid(uio
)) &&
1598 (so
->so_rcv
.sb_cc
< so
->so_rcv
.sb_lowat
||
1599 ((flags
& MSG_WAITALL
) && uio_resid(uio
) <= so
->so_rcv
.sb_hiwat
)) &&
1600 m
->m_nextpkt
== 0 && (pr
->pr_flags
& PR_ATOMIC
) == 0)) {
1602 KASSERT(m
!= 0 || !so
->so_rcv
.sb_cc
, ("receive 1"));
1606 error
= so
->so_error
;
1607 if ((flags
& MSG_PEEK
) == 0)
1611 if (so
->so_state
& SS_CANTRCVMORE
) {
1617 for (; m
; m
= m
->m_next
)
1618 if (m
->m_type
== MT_OOBDATA
|| (m
->m_flags
& M_EOR
)) {
1619 m
= so
->so_rcv
.sb_mb
;
1622 if ((so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
)) == 0 &&
1623 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)) {
1627 if (uio_resid(uio
) == 0)
1629 if ((so
->so_state
& SS_NBIO
) || (flags
& (MSG_DONTWAIT
|MSG_NBIO
))) {
1630 error
= EWOULDBLOCK
;
1633 sbunlock(&so
->so_rcv
, 1);
1634 #ifdef EVEN_MORE_LOCKING_DEBUG
1636 printf("Waiting for socket data\n");
1639 error
= sbwait(&so
->so_rcv
);
1640 #ifdef EVEN_MORE_LOCKING_DEBUG
1642 printf("SORECEIVE - sbwait returned %d\n", error
);
1644 if (so
->so_usecount
< 1)
1645 panic("soreceive: after 2nd sblock so=%x ref=%d on socket\n", so
, so
->so_usecount
);
1647 socket_unlock(so
, 1);
1648 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1656 uio
->uio_procp
->p_stats
->p_ru
.ru_msgrcv
++;
1657 #else /* __APPLE__ */
1660 * This should be uio->uio-procp; however, some callers of this
1661 * function use auto variables with stack garbage, and fail to
1662 * fill out the uio structure properly.
1665 p
->p_stats
->p_ru
.ru_msgrcv
++;
1666 #endif /* __APPLE__ */
1667 nextrecord
= m
->m_nextpkt
;
1668 if ((pr
->pr_flags
& PR_ADDR
) && m
->m_type
== MT_SONAME
) {
1669 KASSERT(m
->m_type
== MT_SONAME
, ("receive 1a"));
1672 *psa
= dup_sockaddr(mtod(m
, struct sockaddr
*),
1674 if ((*psa
== 0) && (flags
& MSG_NEEDSA
)) {
1675 error
= EWOULDBLOCK
;
1679 if (flags
& MSG_PEEK
) {
1682 sbfree(&so
->so_rcv
, m
);
1683 if (m
->m_next
== 0 && so
->so_rcv
.sb_cc
!= 0)
1684 panic("soreceive: about to create invalid socketbuf");
1685 MFREE(m
, so
->so_rcv
.sb_mb
);
1686 m
= so
->so_rcv
.sb_mb
;
1689 while (m
&& m
->m_type
== MT_CONTROL
&& error
== 0) {
1690 if (flags
& MSG_PEEK
) {
1692 *controlp
= m_copy(m
, 0, m
->m_len
);
1695 sbfree(&so
->so_rcv
, m
);
1697 if (pr
->pr_domain
->dom_externalize
&&
1698 mtod(m
, struct cmsghdr
*)->cmsg_type
==
1700 socket_unlock(so
, 0); /* release socket lock: see 3903171 */
1701 error
= (*pr
->pr_domain
->dom_externalize
)(m
);
1705 if (m
->m_next
== 0 && so
->so_rcv
.sb_cc
!= 0)
1706 panic("soreceive: so->so_rcv.sb_mb->m_next == 0 && so->so_rcv.sb_cc != 0");
1707 so
->so_rcv
.sb_mb
= m
->m_next
;
1709 m
= so
->so_rcv
.sb_mb
;
1711 MFREE(m
, so
->so_rcv
.sb_mb
);
1712 m
= so
->so_rcv
.sb_mb
;
1717 controlp
= &(*controlp
)->m_next
;
1721 if ((flags
& MSG_PEEK
) == 0)
1722 m
->m_nextpkt
= nextrecord
;
1724 if (type
== MT_OOBDATA
)
1730 if (!(flags
& MSG_PEEK
) && uio_resid(uio
) > sorecvmincopy
)
1737 while (m
&& (uio_resid(uio
) - delayed_copy_len
) > 0 && error
== 0) {
1738 if (m
->m_type
== MT_OOBDATA
) {
1739 if (type
!= MT_OOBDATA
)
1741 } else if (type
== MT_OOBDATA
)
1745 * This assertion needs rework. The trouble is Appletalk is uses many
1746 * mbuf types (NOT listed in mbuf.h!) which will trigger this panic.
1747 * For now just remove the assertion... CSM 9/98
1750 KASSERT(m
->m_type
== MT_DATA
|| m
->m_type
== MT_HEADER
,
1754 * Make sure to allways set MSG_OOB event when getting
1755 * out of band data inline.
1757 if ((so
->so_options
& SO_WANTOOBFLAG
) != 0 &&
1758 (so
->so_options
& SO_OOBINLINE
) != 0 &&
1759 (so
->so_state
& SS_RCVATMARK
) != 0) {
1763 so
->so_state
&= ~SS_RCVATMARK
;
1764 // LP64todo - fix this!
1765 len
= uio_resid(uio
) - delayed_copy_len
;
1766 if (so
->so_oobmark
&& len
> so
->so_oobmark
- offset
)
1767 len
= so
->so_oobmark
- offset
;
1768 if (len
> m
->m_len
- moff
)
1769 len
= m
->m_len
- moff
;
1771 * If mp is set, just pass back the mbufs.
1772 * Otherwise copy them out via the uio, then free.
1773 * Sockbuf must be consistent here (points to current mbuf,
1774 * it points to next record) when we drop priority;
1775 * we must note any additions to the sockbuf when we
1776 * block interrupts again.
1779 if (can_delay
&& len
== m
->m_len
) {
1781 * only delay the copy if we're consuming the
1782 * mbuf and we're NOT in MSG_PEEK mode
1783 * and we have enough data to make it worthwile
1784 * to drop and retake the funnel... can_delay
1785 * reflects the state of the 2 latter constraints
1786 * moff should always be zero in these cases
1788 delayed_copy_len
+= len
;
1791 if (delayed_copy_len
) {
1792 error
= sodelayed_copy(so
, uio
, &free_list
, &delayed_copy_len
);
1797 if (m
!= so
->so_rcv
.sb_mb
) {
1799 * can only get here if MSG_PEEK is not set
1800 * therefore, m should point at the head of the rcv queue...
1801 * if it doesn't, it means something drastically changed
1802 * while we were out from behind the funnel in sodelayed_copy...
1803 * perhaps a RST on the stream... in any event, the stream has
1804 * been interrupted... it's probably best just to return
1805 * whatever data we've moved and let the caller sort it out...
1810 socket_unlock(so
, 0);
1811 error
= uiomove(mtod(m
, caddr_t
) + moff
, (int)len
, uio
);
1818 uio_setresid(uio
, (uio_resid(uio
) - len
));
1820 if (len
== m
->m_len
- moff
) {
1821 if (m
->m_flags
& M_EOR
)
1823 if (flags
& MSG_PEEK
) {
1827 nextrecord
= m
->m_nextpkt
;
1828 sbfree(&so
->so_rcv
, m
);
1829 m
->m_nextpkt
= NULL
;
1834 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
1835 *mp
= (struct mbuf
*)0;
1837 if (free_list
== NULL
)
1842 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
1846 m
->m_nextpkt
= nextrecord
;
1849 if (flags
& MSG_PEEK
)
1853 *mp
= m_copym(m
, 0, len
, M_WAIT
);
1856 so
->so_rcv
.sb_cc
-= len
;
1859 if (so
->so_oobmark
) {
1860 if ((flags
& MSG_PEEK
) == 0) {
1861 so
->so_oobmark
-= len
;
1862 if (so
->so_oobmark
== 0) {
1863 so
->so_state
|= SS_RCVATMARK
;
1865 * delay posting the actual event until after
1866 * any delayed copy processing has finished
1873 if (offset
== so
->so_oobmark
)
1877 if (flags
& MSG_EOR
)
1880 * If the MSG_WAITALL or MSG_WAITSTREAM flag is set (for non-atomic socket),
1881 * we must not quit until "uio->uio_resid == 0" or an error
1882 * termination. If a signal/timeout occurs, return
1883 * with a short count but without error.
1884 * Keep sockbuf locked against other readers.
1886 while (flags
& (MSG_WAITALL
|MSG_WAITSTREAM
) && m
== 0 && (uio_resid(uio
) - delayed_copy_len
) > 0 &&
1887 !sosendallatonce(so
) && !nextrecord
) {
1888 if (so
->so_error
|| so
->so_state
& SS_CANTRCVMORE
)
1891 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
&& (((struct inpcb
*)so
->so_pcb
)->inp_state
!= INPCB_STATE_DEAD
))
1892 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, flags
);
1893 if (sbwait(&so
->so_rcv
)) {
1898 * have to wait until after we get back from the sbwait to do the copy because
1899 * we will drop the funnel if we have enough data that has been delayed... by dropping
1900 * the funnel we open up a window allowing the netisr thread to process the incoming packets
1901 * and to change the state of this socket... we're issuing the sbwait because
1902 * the socket is empty and we're expecting the netisr thread to wake us up when more
1903 * packets arrive... if we allow that processing to happen and then sbwait, we
1904 * could stall forever with packets sitting in the socket if no further packets
1905 * arrive from the remote side.
1907 * we want to copy before we've collected all the data to satisfy this request to
1908 * allow the copy to overlap the incoming packet processing on an MP system
1910 if (delayed_copy_len
> sorecvmincopy
&& (delayed_copy_len
> (so
->so_rcv
.sb_hiwat
/ 2))) {
1912 error
= sodelayed_copy(so
, uio
, &free_list
, &delayed_copy_len
);
1917 m
= so
->so_rcv
.sb_mb
;
1919 nextrecord
= m
->m_nextpkt
;
1923 #ifdef MORE_LOCKING_DEBUG
1924 if (so
->so_usecount
<= 1)
1925 panic("soreceive: after big while so=%x ref=%d on socket\n", so
, so
->so_usecount
);
1928 if (m
&& pr
->pr_flags
& PR_ATOMIC
) {
1930 if (so
->so_options
& SO_DONTTRUNC
)
1931 flags
|= MSG_RCVMORE
;
1935 if ((flags
& MSG_PEEK
) == 0)
1936 (void) sbdroprecord(&so
->so_rcv
);
1941 if ((flags
& MSG_PEEK
) == 0) {
1943 so
->so_rcv
.sb_mb
= nextrecord
;
1944 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
)
1945 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, flags
);
1948 if ((so
->so_options
& SO_WANTMORE
) && so
->so_rcv
.sb_cc
> 0)
1949 flags
|= MSG_HAVEMORE
;
1951 if (delayed_copy_len
) {
1952 error
= sodelayed_copy(so
, uio
, &free_list
, &delayed_copy_len
);
1958 m_freem_list((struct mbuf
*)free_list
);
1959 free_list
= (struct mbuf
*)0;
1962 postevent(so
, 0, EV_OOB
);
1964 if (orig_resid
== uio_resid(uio
) && orig_resid
&&
1965 (flags
& MSG_EOR
) == 0 && (so
->so_state
& SS_CANTRCVMORE
) == 0) {
1966 sbunlock(&so
->so_rcv
, 1);
1973 #ifdef MORE_LOCKING_DEBUG
1974 if (so
->so_usecount
<= 1)
1975 panic("soreceive: release so=%x ref=%d on socket\n", so
, so
->so_usecount
);
1977 if (delayed_copy_len
) {
1978 error
= sodelayed_copy(so
, uio
, &free_list
, &delayed_copy_len
);
1981 m_freem_list((struct mbuf
*)free_list
);
1983 sbunlock(&so
->so_rcv
, 0); /* will unlock socket */
1985 // LP64todo - fix this!
1986 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
,
1997 static int sodelayed_copy(struct socket
*so
, struct uio
*uio
, struct mbuf
**free_list
, int *resid
)
2004 socket_unlock(so
, 0);
2006 while (m
&& error
== 0) {
2008 error
= uiomove(mtod(m
, caddr_t
), (int)m
->m_len
, uio
);
2012 m_freem_list(*free_list
);
2014 *free_list
= (struct mbuf
*)NULL
;
2025 register struct socket
*so
;
2028 register struct protosw
*pr
= so
->so_proto
;
2033 sflt_notify(so
, sock_evt_shutdown
, &how
);
2035 if (how
!= SHUT_WR
) {
2037 postevent(so
, 0, EV_RCLOSED
);
2039 if (how
!= SHUT_RD
) {
2040 ret
= ((*pr
->pr_usrreqs
->pru_shutdown
)(so
));
2041 postevent(so
, 0, EV_WCLOSED
);
2042 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
2043 socket_unlock(so
, 1);
2047 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
2048 socket_unlock(so
, 1);
2054 register struct socket
*so
;
2056 register struct sockbuf
*sb
= &so
->so_rcv
;
2057 register struct protosw
*pr
= so
->so_proto
;
2060 #ifdef MORE_LOCKING_DEBUG
2061 lck_mtx_t
* mutex_held
;
2063 if (so
->so_proto
->pr_getlock
!= NULL
)
2064 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
2066 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
2067 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
2070 sflt_notify(so
, sock_evt_flush_read
, NULL
);
2072 sb
->sb_flags
|= SB_NOINTR
;
2073 (void) sblock(sb
, M_WAIT
);
2077 selthreadclear(&sb
->sb_sel
);
2080 bzero((caddr_t
)sb
, sizeof (*sb
));
2081 sb
->sb_so
= so
; /* reestablish link to socket */
2082 if (asb
.sb_flags
& SB_KNOTE
) {
2083 sb
->sb_sel
.si_note
= asb
.sb_sel
.si_note
;
2084 sb
->sb_flags
= SB_KNOTE
;
2086 if (pr
->pr_flags
& PR_RIGHTS
&& pr
->pr_domain
->dom_dispose
)
2087 (*pr
->pr_domain
->dom_dispose
)(asb
.sb_mb
);
2092 * Perhaps this routine, and sooptcopyout(), below, ought to come in
2093 * an additional variant to handle the case where the option value needs
2094 * to be some kind of integer, but not a specific size.
2095 * In addition to their use here, these functions are also called by the
2096 * protocol-level pr_ctloutput() routines.
2099 sooptcopyin(sopt
, buf
, len
, minlen
)
2100 struct sockopt
*sopt
;
2108 * If the user gives us more than we wanted, we ignore it,
2109 * but if we don't get the minimum length the caller
2110 * wants, we return EINVAL. On success, sopt->sopt_valsize
2111 * is set to however much we actually retrieved.
2113 if ((valsize
= sopt
->sopt_valsize
) < minlen
)
2116 sopt
->sopt_valsize
= valsize
= len
;
2118 if (sopt
->sopt_p
!= 0)
2119 return (copyin(sopt
->sopt_val
, buf
, valsize
));
2121 bcopy(CAST_DOWN(caddr_t
, sopt
->sopt_val
), buf
, valsize
);
2128 struct sockopt
*sopt
;
2137 if (sopt
->sopt_dir
!= SOPT_SET
) {
2138 sopt
->sopt_dir
= SOPT_SET
;
2142 struct socket_filter_entry
*filter
;
2145 for (filter
= so
->so_filt
; filter
&& (error
== 0);
2146 filter
= filter
->sfe_next_onsocket
) {
2147 if (filter
->sfe_filter
->sf_filter
.sf_setoption
) {
2148 if (filtered
== 0) {
2151 socket_unlock(so
, 0);
2153 error
= filter
->sfe_filter
->sf_filter
.sf_setoption(
2154 filter
->sfe_cookie
, so
, sopt
);
2158 if (filtered
!= 0) {
2163 if (error
== EJUSTRETURN
)
2171 if (sopt
->sopt_level
!= SOL_SOCKET
) {
2172 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
2173 error
= (*so
->so_proto
->pr_ctloutput
)
2175 socket_unlock(so
, 1);
2178 error
= ENOPROTOOPT
;
2180 switch (sopt
->sopt_name
) {
2183 error
= sooptcopyin(sopt
, &l
, sizeof l
, sizeof l
);
2187 so
->so_linger
= (sopt
->sopt_name
== SO_LINGER
) ? l
.l_linger
: l
.l_linger
* hz
;
2189 so
->so_options
|= SO_LINGER
;
2191 so
->so_options
&= ~SO_LINGER
;
2197 case SO_USELOOPBACK
:
2206 case SO_WANTOOBFLAG
:
2208 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2213 so
->so_options
|= sopt
->sopt_name
;
2215 so
->so_options
&= ~sopt
->sopt_name
;
2222 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2228 * Values < 1 make no sense for any of these
2229 * options, so disallow them.
2236 switch (sopt
->sopt_name
) {
2239 if (sbreserve(sopt
->sopt_name
== SO_SNDBUF
?
2240 &so
->so_snd
: &so
->so_rcv
,
2241 (u_long
) optval
) == 0) {
2248 * Make sure the low-water is never greater than
2252 so
->so_snd
.sb_lowat
=
2253 (optval
> so
->so_snd
.sb_hiwat
) ?
2254 so
->so_snd
.sb_hiwat
: optval
;
2257 so
->so_rcv
.sb_lowat
=
2258 (optval
> so
->so_rcv
.sb_hiwat
) ?
2259 so
->so_rcv
.sb_hiwat
: optval
;
2266 error
= sooptcopyin(sopt
, &tv
, sizeof tv
,
2271 if (tv
.tv_sec
< 0 || tv
.tv_sec
> LONG_MAX
||
2272 tv
.tv_usec
< 0 || tv
.tv_usec
>= 1000000) {
2277 switch (sopt
->sopt_name
) {
2279 so
->so_snd
.sb_timeo
= tv
;
2282 so
->so_rcv
.sb_timeo
= tv
;
2291 error
= sooptcopyin(sopt
, &nke
,
2292 sizeof nke
, sizeof nke
);
2296 error
= sflt_attach_private(so
, NULL
, nke
.nke_handle
, 1);
2301 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2306 so
->so_flags
|= SOF_NOSIGPIPE
;
2308 so
->so_flags
&= ~SOF_NOSIGPIPE
;
2313 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2318 so
->so_flags
|= SOF_NOADDRAVAIL
;
2320 so
->so_flags
&= ~SOF_NOADDRAVAIL
;
2325 error
= ENOPROTOOPT
;
2328 if (error
== 0 && so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
2329 (void) ((*so
->so_proto
->pr_ctloutput
)
2334 socket_unlock(so
, 1);
2338 /* Helper routine for getsockopt */
2340 sooptcopyout(sopt
, buf
, len
)
2341 struct sockopt
*sopt
;
2351 * Documented get behavior is that we always return a value,
2352 * possibly truncated to fit in the user's buffer.
2353 * Traditional behavior is that we always tell the user
2354 * precisely how much we copied, rather than something useful
2355 * like the total amount we had available for her.
2356 * Note that this interface is not idempotent; the entire answer must
2357 * generated ahead of time.
2359 valsize
= min(len
, sopt
->sopt_valsize
);
2360 sopt
->sopt_valsize
= valsize
;
2361 if (sopt
->sopt_val
!= USER_ADDR_NULL
) {
2362 if (sopt
->sopt_p
!= 0)
2363 error
= copyout(buf
, sopt
->sopt_val
, valsize
);
2365 bcopy(buf
, CAST_DOWN(caddr_t
, sopt
->sopt_val
), valsize
);
2373 struct sockopt
*sopt
;
2379 if (sopt
->sopt_dir
!= SOPT_GET
) {
2380 sopt
->sopt_dir
= SOPT_GET
;
2386 struct socket_filter_entry
*filter
;
2389 for (filter
= so
->so_filt
; filter
&& (error
== 0);
2390 filter
= filter
->sfe_next_onsocket
) {
2391 if (filter
->sfe_filter
->sf_filter
.sf_getoption
) {
2392 if (filtered
== 0) {
2395 socket_unlock(so
, 0);
2397 error
= filter
->sfe_filter
->sf_filter
.sf_getoption(
2398 filter
->sfe_cookie
, so
, sopt
);
2401 if (filtered
!= 0) {
2406 if (error
== EJUSTRETURN
)
2408 socket_unlock(so
, 1);
2415 if (sopt
->sopt_level
!= SOL_SOCKET
) {
2416 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
2417 error
= (*so
->so_proto
->pr_ctloutput
)
2419 socket_unlock(so
, 1);
2422 socket_unlock(so
, 1);
2423 return (ENOPROTOOPT
);
2426 switch (sopt
->sopt_name
) {
2429 l
.l_onoff
= so
->so_options
& SO_LINGER
;
2430 l
.l_linger
= (sopt
->sopt_name
== SO_LINGER
) ? so
->so_linger
:
2432 error
= sooptcopyout(sopt
, &l
, sizeof l
);
2435 case SO_USELOOPBACK
:
2447 case SO_WANTOOBFLAG
:
2449 optval
= so
->so_options
& sopt
->sopt_name
;
2451 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2455 optval
= so
->so_type
;
2465 m1
= so
->so_rcv
.sb_mb
;
2466 if (so
->so_proto
->pr_flags
& PR_ATOMIC
)
2469 if (m1
->m_type
== MT_DATA
)
2470 pkt_total
+= m1
->m_len
;
2475 optval
= so
->so_rcv
.sb_cc
;
2479 optval
= so
->so_snd
.sb_cc
;
2483 optval
= so
->so_error
;
2488 optval
= so
->so_snd
.sb_hiwat
;
2492 optval
= so
->so_rcv
.sb_hiwat
;
2496 optval
= so
->so_snd
.sb_lowat
;
2500 optval
= so
->so_rcv
.sb_lowat
;
2505 tv
= (sopt
->sopt_name
== SO_SNDTIMEO
?
2506 so
->so_snd
.sb_timeo
: so
->so_rcv
.sb_timeo
);
2508 error
= sooptcopyout(sopt
, &tv
, sizeof tv
);
2512 optval
= (so
->so_flags
& SOF_NOSIGPIPE
);
2516 optval
= (so
->so_flags
& SOF_NOADDRAVAIL
);
2520 error
= ENOPROTOOPT
;
2523 socket_unlock(so
, 1);
2528 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
2530 soopt_getm(struct sockopt
*sopt
, struct mbuf
**mp
)
2532 struct mbuf
*m
, *m_prev
;
2533 int sopt_size
= sopt
->sopt_valsize
;
2535 if (sopt_size
> MAX_SOOPTGETM_SIZE
)
2538 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
2541 if (sopt_size
> MLEN
) {
2542 MCLGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
);
2543 if ((m
->m_flags
& M_EXT
) == 0) {
2547 m
->m_len
= min(MCLBYTES
, sopt_size
);
2549 m
->m_len
= min(MLEN
, sopt_size
);
2551 sopt_size
-= m
->m_len
;
2556 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
2561 if (sopt_size
> MLEN
) {
2562 MCLGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
);
2563 if ((m
->m_flags
& M_EXT
) == 0) {
2567 m
->m_len
= min(MCLBYTES
, sopt_size
);
2569 m
->m_len
= min(MLEN
, sopt_size
);
2571 sopt_size
-= m
->m_len
;
2578 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2580 soopt_mcopyin(struct sockopt
*sopt
, struct mbuf
*m
)
2582 struct mbuf
*m0
= m
;
2584 if (sopt
->sopt_val
== USER_ADDR_NULL
)
2586 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
2587 if (sopt
->sopt_p
!= NULL
) {
2590 error
= copyin(sopt
->sopt_val
, mtod(m
, char *), m
->m_len
);
2596 bcopy(CAST_DOWN(caddr_t
, sopt
->sopt_val
), mtod(m
, char *), m
->m_len
);
2597 sopt
->sopt_valsize
-= m
->m_len
;
2598 sopt
->sopt_val
+= m
->m_len
;
2601 if (m
!= NULL
) /* should be allocated enoughly at ip6_sooptmcopyin() */
2602 panic("soopt_mcopyin");
2606 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2608 soopt_mcopyout(struct sockopt
*sopt
, struct mbuf
*m
)
2610 struct mbuf
*m0
= m
;
2613 if (sopt
->sopt_val
== USER_ADDR_NULL
)
2615 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
2616 if (sopt
->sopt_p
!= NULL
) {
2619 error
= copyout(mtod(m
, char *), sopt
->sopt_val
, m
->m_len
);
2625 bcopy(mtod(m
, char *), CAST_DOWN(caddr_t
, sopt
->sopt_val
), m
->m_len
);
2626 sopt
->sopt_valsize
-= m
->m_len
;
2627 sopt
->sopt_val
+= m
->m_len
;
2628 valsize
+= m
->m_len
;
2632 /* enough soopt buffer should be given from user-land */
2636 sopt
->sopt_valsize
= valsize
;
2642 register struct socket
*so
;
2646 if (so
->so_pgid
< 0)
2647 gsignal(-so
->so_pgid
, SIGURG
);
2648 else if (so
->so_pgid
> 0 && (p
= pfind(so
->so_pgid
)) != 0)
2650 selwakeup(&so
->so_rcv
.sb_sel
);
2654 sopoll(struct socket
*so
, int events
, __unused kauth_cred_t cred
, void * wql
)
2656 struct proc
*p
= current_proc();
2661 if (events
& (POLLIN
| POLLRDNORM
))
2663 revents
|= events
& (POLLIN
| POLLRDNORM
);
2665 if (events
& (POLLOUT
| POLLWRNORM
))
2666 if (sowriteable(so
))
2667 revents
|= events
& (POLLOUT
| POLLWRNORM
);
2669 if (events
& (POLLPRI
| POLLRDBAND
))
2670 if (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
))
2671 revents
|= events
& (POLLPRI
| POLLRDBAND
);
2674 if (events
& (POLLIN
| POLLPRI
| POLLRDNORM
| POLLRDBAND
)) {
2675 /* Darwin sets the flag first, BSD calls selrecord first */
2676 so
->so_rcv
.sb_flags
|= SB_SEL
;
2677 selrecord(p
, &so
->so_rcv
.sb_sel
, wql
);
2680 if (events
& (POLLOUT
| POLLWRNORM
)) {
2681 /* Darwin sets the flag first, BSD calls selrecord first */
2682 so
->so_snd
.sb_flags
|= SB_SEL
;
2683 selrecord(p
, &so
->so_snd
.sb_sel
, wql
);
2687 socket_unlock(so
, 1);
2691 int soo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
2694 soo_kqfilter(__unused
struct fileproc
*fp
, struct knote
*kn
, __unused
struct proc
*p
)
2696 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2700 switch (kn
->kn_filter
) {
2702 if (so
->so_options
& SO_ACCEPTCONN
)
2703 kn
->kn_fop
= &solisten_filtops
;
2705 kn
->kn_fop
= &soread_filtops
;
2709 kn
->kn_fop
= &sowrite_filtops
;
2713 socket_unlock(so
, 1);
2717 if (KNOTE_ATTACH(&sb
->sb_sel
.si_note
, kn
))
2718 sb
->sb_flags
|= SB_KNOTE
;
2719 socket_unlock(so
, 1);
2724 filt_sordetach(struct knote
*kn
)
2726 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2729 if (so
->so_rcv
.sb_flags
& SB_KNOTE
)
2730 if (KNOTE_DETACH(&so
->so_rcv
.sb_sel
.si_note
, kn
))
2731 so
->so_rcv
.sb_flags
&= ~SB_KNOTE
;
2732 socket_unlock(so
, 1);
2737 filt_soread(struct knote
*kn
, long hint
)
2739 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2741 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2744 if (so
->so_oobmark
) {
2745 if (kn
->kn_flags
& EV_OOBAND
) {
2746 kn
->kn_data
= so
->so_rcv
.sb_cc
- so
->so_oobmark
;
2747 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2748 socket_unlock(so
, 1);
2751 kn
->kn_data
= so
->so_oobmark
;
2752 kn
->kn_flags
|= EV_OOBAND
;
2754 kn
->kn_data
= so
->so_rcv
.sb_cc
;
2755 if (so
->so_state
& SS_CANTRCVMORE
) {
2756 kn
->kn_flags
|= EV_EOF
;
2757 kn
->kn_fflags
= so
->so_error
;
2758 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2759 socket_unlock(so
, 1);
2764 if (so
->so_state
& SS_RCVATMARK
) {
2765 if (kn
->kn_flags
& EV_OOBAND
) {
2766 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2767 socket_unlock(so
, 1);
2770 kn
->kn_flags
|= EV_OOBAND
;
2771 } else if (kn
->kn_flags
& EV_OOBAND
) {
2773 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2774 socket_unlock(so
, 1);
2778 if (so
->so_error
) { /* temporary udp error */
2779 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2780 socket_unlock(so
, 1);
2784 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2785 socket_unlock(so
, 1);
2787 return( kn
->kn_flags
& EV_OOBAND
||
2788 kn
->kn_data
>= ((kn
->kn_sfflags
& NOTE_LOWAT
) ?
2789 kn
->kn_sdata
: so
->so_rcv
.sb_lowat
));
2793 filt_sowdetach(struct knote
*kn
)
2795 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2798 if(so
->so_snd
.sb_flags
& SB_KNOTE
)
2799 if (KNOTE_DETACH(&so
->so_snd
.sb_sel
.si_note
, kn
))
2800 so
->so_snd
.sb_flags
&= ~SB_KNOTE
;
2801 socket_unlock(so
, 1);
2806 filt_sowrite(struct knote
*kn
, long hint
)
2808 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2810 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2813 kn
->kn_data
= sbspace(&so
->so_snd
);
2814 if (so
->so_state
& SS_CANTSENDMORE
) {
2815 kn
->kn_flags
|= EV_EOF
;
2816 kn
->kn_fflags
= so
->so_error
;
2817 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2818 socket_unlock(so
, 1);
2821 if (so
->so_error
) { /* temporary udp error */
2822 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2823 socket_unlock(so
, 1);
2826 if (((so
->so_state
& SS_ISCONNECTED
) == 0) &&
2827 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)) {
2828 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2829 socket_unlock(so
, 1);
2832 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2833 socket_unlock(so
, 1);
2834 if (kn
->kn_sfflags
& NOTE_LOWAT
)
2835 return (kn
->kn_data
>= kn
->kn_sdata
);
2836 return (kn
->kn_data
>= so
->so_snd
.sb_lowat
);
2841 filt_solisten(struct knote
*kn
, long hint
)
2843 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2846 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2848 kn
->kn_data
= so
->so_qlen
;
2849 isempty
= ! TAILQ_EMPTY(&so
->so_comp
);
2850 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2851 socket_unlock(so
, 1);
2857 socket_lock(so
, refcount
)
2861 int error
= 0, lr_saved
;
2863 lr_saved
= (unsigned int) __builtin_return_address(0);
2865 if (so
->so_proto
->pr_lock
) {
2866 error
= (*so
->so_proto
->pr_lock
)(so
, refcount
, lr_saved
);
2869 #ifdef MORE_LOCKING_DEBUG
2870 lck_mtx_assert(so
->so_proto
->pr_domain
->dom_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
2872 lck_mtx_lock(so
->so_proto
->pr_domain
->dom_mtx
);
2875 so
->lock_lr
[so
->next_lock_lr
] = (void *)lr_saved
;
2876 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
2884 socket_unlock(so
, refcount
)
2888 int error
= 0, lr_saved
;
2889 lck_mtx_t
* mutex_held
;
2891 lr_saved
= (unsigned int) __builtin_return_address(0);
2893 if (so
->so_proto
== NULL
)
2894 panic("socket_unlock null so_proto so=%x\n", so
);
2896 if (so
&& so
->so_proto
->pr_unlock
)
2897 error
= (*so
->so_proto
->pr_unlock
)(so
, refcount
, lr_saved
);
2899 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
2900 #ifdef MORE_LOCKING_DEBUG
2901 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
2903 so
->unlock_lr
[so
->next_unlock_lr
] = (void *)lr_saved
;
2904 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
2907 if (so
->so_usecount
<= 0)
2908 panic("socket_unlock: bad refcount so=%x value=%d\n", so
, so
->so_usecount
);
2910 if (so
->so_usecount
== 0) {
2911 sofreelastref(so
, 1);
2914 lck_mtx_unlock(mutex_held
);
2919 //### Called with socket locked, will unlock socket
2925 lck_mtx_t
* mutex_held
;
2926 if (so
->so_proto
->pr_getlock
!= NULL
)
2927 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
2929 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
2930 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
2932 sofreelastref(so
, 0);
2939 socket_lock(so
, 1); /* locks & take one reference on socket */
2940 socket_unlock(so
, 0); /* unlock only */
2948 socket_unlock(so
, 1);