2 * Copyright (c) 2000-2005 Apple Computer, 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) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
29 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
31 * Copyright (c) 1982, 1986, 1988, 1990, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
63 * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.16 2001/06/14 20:46:06 ume Exp $
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/filedesc.h>
69 #include <sys/proc_internal.h>
70 #include <sys/kauth.h>
71 #include <sys/file_internal.h>
72 #include <sys/fcntl.h>
73 #include <sys/malloc.h>
75 #include <sys/domain.h>
76 #include <sys/kernel.h>
77 #include <sys/event.h>
79 #include <sys/protosw.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/resourcevar.h>
83 #include <sys/signalvar.h>
84 #include <sys/sysctl.h>
87 #include <sys/kdebug.h>
88 #include <net/route.h>
89 #include <netinet/in.h>
90 #include <netinet/in_pcb.h>
91 #include <kern/zalloc.h>
92 #include <kern/locks.h>
93 #include <machine/limits.h>
96 int so_cache_timeouts
= 0;
97 int so_cache_max_freed
= 0;
98 int cached_sock_count
= 0;
99 struct socket
*socket_cache_head
= 0;
100 struct socket
*socket_cache_tail
= 0;
101 u_long so_cache_time
= 0;
102 int so_cache_init_done
= 0;
103 struct zone
*so_cache_zone
;
104 extern int get_inpcb_str_size();
105 extern int get_tcp_str_size();
107 static lck_grp_t
*so_cache_mtx_grp
;
108 static lck_attr_t
*so_cache_mtx_attr
;
109 static lck_grp_attr_t
*so_cache_mtx_grp_attr
;
110 lck_mtx_t
*so_cache_mtx
;
112 #include <machine/limits.h>
114 static void filt_sordetach(struct knote
*kn
);
115 static int filt_soread(struct knote
*kn
, long hint
);
116 static void filt_sowdetach(struct knote
*kn
);
117 static int filt_sowrite(struct knote
*kn
, long hint
);
118 static int filt_solisten(struct knote
*kn
, long hint
);
120 static struct filterops solisten_filtops
=
121 { 1, NULL
, filt_sordetach
, filt_solisten
};
122 static struct filterops soread_filtops
=
123 { 1, NULL
, filt_sordetach
, filt_soread
};
124 static struct filterops sowrite_filtops
=
125 { 1, NULL
, filt_sowdetach
, filt_sowrite
};
127 #define EVEN_MORE_LOCKING_DEBUG 0
128 int socket_debug
= 0;
129 int socket_zone
= M_SOCKET
;
130 so_gen_t so_gencnt
; /* generation count for sockets */
132 MALLOC_DEFINE(M_SONAME
, "soname", "socket name");
133 MALLOC_DEFINE(M_PCB
, "pcb", "protocol control block");
135 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0)
136 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2)
137 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1)
138 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3)
139 #define DBG_FNC_SOSEND NETDBG_CODE(DBG_NETSOCK, (4 << 8) | 1)
140 #define DBG_FNC_SORECEIVE NETDBG_CODE(DBG_NETSOCK, (8 << 8))
141 #define DBG_FNC_SOSHUTDOWN NETDBG_CODE(DBG_NETSOCK, (9 << 8))
143 #define MAX_SOOPTGETM_SIZE (128 * MCLBYTES)
146 SYSCTL_DECL(_kern_ipc
);
148 static int somaxconn
= SOMAXCONN
;
149 SYSCTL_INT(_kern_ipc
, KIPC_SOMAXCONN
, somaxconn
, CTLFLAG_RW
, &somaxconn
,
152 /* Should we get a maximum also ??? */
153 static int sosendmaxchain
= 65536;
154 static int sosendminchain
= 16384;
155 static int sorecvmincopy
= 16384;
156 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sosendminchain
, CTLFLAG_RW
, &sosendminchain
,
158 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sorecvmincopy
, CTLFLAG_RW
, &sorecvmincopy
,
161 void so_cache_timer();
164 * Socket operation routines.
165 * These routines are called by the routines in
166 * sys_socket.c or from a system process, and
167 * implement the semantics of socket operations by
168 * switching out to the protocol specific routines.
173 vm_size_t so_cache_zone_element_size
;
175 static int sodelayed_copy(struct socket
*so
, struct uio
*uio
, struct mbuf
**free_list
, int *resid
);
182 if (so_cache_init_done
) {
183 printf("socketinit: already called...\n");
188 * allocate lock group attribute and group for socket cache mutex
190 so_cache_mtx_grp_attr
= lck_grp_attr_alloc_init();
192 so_cache_mtx_grp
= lck_grp_alloc_init("so_cache", so_cache_mtx_grp_attr
);
195 * allocate the lock attribute for socket cache mutex
197 so_cache_mtx_attr
= lck_attr_alloc_init();
199 so_cache_init_done
= 1;
201 so_cache_mtx
= lck_mtx_alloc_init(so_cache_mtx_grp
, so_cache_mtx_attr
); /* cached sockets mutex */
203 if (so_cache_mtx
== NULL
)
204 return; /* we're hosed... */
206 str_size
= (vm_size_t
)( sizeof(struct socket
) + 4 +
207 get_inpcb_str_size() + 4 +
209 so_cache_zone
= zinit (str_size
, 120000*str_size
, 8192, "socache zone");
211 printf("cached_sock_alloc -- so_cache_zone size is %x\n", str_size
);
213 timeout(so_cache_timer
, NULL
, (SO_CACHE_FLUSH_INTERVAL
* hz
));
215 so_cache_zone_element_size
= str_size
;
221 void cached_sock_alloc(so
, waitok
)
227 register u_long offset
;
230 lck_mtx_lock(so_cache_mtx
);
232 if (cached_sock_count
) {
234 *so
= socket_cache_head
;
236 panic("cached_sock_alloc: cached sock is null");
238 socket_cache_head
= socket_cache_head
->cache_next
;
239 if (socket_cache_head
)
240 socket_cache_head
->cache_prev
= 0;
242 socket_cache_tail
= 0;
244 lck_mtx_unlock(so_cache_mtx
);
246 temp
= (*so
)->so_saved_pcb
;
247 bzero((caddr_t
)*so
, sizeof(struct socket
));
249 kprintf("cached_sock_alloc - retreiving cached sock %x - count == %d\n", *so
,
252 (*so
)->so_saved_pcb
= temp
;
253 (*so
)->cached_in_sock_layer
= 1;
258 kprintf("Allocating cached sock %x from memory\n", *so
);
261 lck_mtx_unlock(so_cache_mtx
);
264 *so
= (struct socket
*) zalloc(so_cache_zone
);
266 *so
= (struct socket
*) zalloc_noblock(so_cache_zone
);
271 bzero((caddr_t
)*so
, sizeof(struct socket
));
274 * Define offsets for extra structures into our single block of
275 * memory. Align extra structures on longword boundaries.
279 offset
= (u_long
) *so
;
280 offset
+= sizeof(struct socket
);
283 offset
&= 0xfffffffc;
285 (*so
)->so_saved_pcb
= (caddr_t
) offset
;
286 offset
+= get_inpcb_str_size();
289 offset
&= 0xfffffffc;
292 ((struct inpcb
*) (*so
)->so_saved_pcb
)->inp_saved_ppcb
= (caddr_t
) offset
;
294 kprintf("Allocating cached socket - %x, pcb=%x tcpcb=%x\n", *so
,
296 ((struct inpcb
*)(*so
)->so_saved_pcb
)->inp_saved_ppcb
);
300 (*so
)->cached_in_sock_layer
= 1;
304 void cached_sock_free(so
)
308 lck_mtx_lock(so_cache_mtx
);
310 if (++cached_sock_count
> MAX_CACHED_SOCKETS
) {
312 lck_mtx_unlock(so_cache_mtx
);
314 kprintf("Freeing overflowed cached socket %x\n", so
);
316 zfree(so_cache_zone
, so
);
320 kprintf("Freeing socket %x into cache\n", so
);
322 if (so_cache_hw
< cached_sock_count
)
323 so_cache_hw
= cached_sock_count
;
325 so
->cache_next
= socket_cache_head
;
327 if (socket_cache_head
)
328 socket_cache_head
->cache_prev
= so
;
330 socket_cache_tail
= so
;
332 so
->cache_timestamp
= so_cache_time
;
333 socket_cache_head
= so
;
334 lck_mtx_unlock(so_cache_mtx
);
338 kprintf("Freed cached sock %x into cache - count is %d\n", so
, cached_sock_count
);
345 void so_cache_timer()
347 register struct socket
*p
;
348 register int n_freed
= 0;
351 lck_mtx_lock(so_cache_mtx
);
355 while ( (p
= socket_cache_tail
) )
357 if ((so_cache_time
- p
->cache_timestamp
) < SO_CACHE_TIME_LIMIT
)
362 if ( (socket_cache_tail
= p
->cache_prev
) )
363 p
->cache_prev
->cache_next
= 0;
364 if (--cached_sock_count
== 0)
365 socket_cache_head
= 0;
368 zfree(so_cache_zone
, p
);
370 if (++n_freed
>= SO_CACHE_MAX_FREE_BATCH
)
372 so_cache_max_freed
++;
376 lck_mtx_unlock(so_cache_mtx
);
378 timeout(so_cache_timer
, NULL
, (SO_CACHE_FLUSH_INTERVAL
* hz
));
382 #endif /* __APPLE__ */
385 * Get a socket structure from our zone, and initialize it.
386 * We don't implement `waitok' yet (see comments in uipc_domain.c).
387 * Note that it would probably be better to allocate socket
388 * and PCB at the same time, but I'm not convinced that all
389 * the protocols can be easily modified to do this.
392 soalloc(waitok
, dom
, type
)
399 if ((dom
== PF_INET
) && (type
== SOCK_STREAM
))
400 cached_sock_alloc(&so
, waitok
);
403 MALLOC_ZONE(so
, struct socket
*, sizeof(*so
), socket_zone
, M_WAITOK
);
405 bzero(so
, sizeof *so
);
407 /* XXX race condition for reentrant kernel */
408 //###LD Atomic add for so_gencnt
410 so
->so_gencnt
= ++so_gencnt
;
411 so
->so_zone
= socket_zone
;
418 socreate(dom
, aso
, type
, proto
)
424 struct proc
*p
= current_proc();
425 register struct protosw
*prp
;
426 register struct socket
*so
;
427 register int error
= 0;
429 extern int tcpconsdebug
;
432 prp
= pffindproto(dom
, proto
, type
);
434 prp
= pffindtype(dom
, type
);
436 if (prp
== 0 || prp
->pr_usrreqs
->pru_attach
== 0)
437 return (EPROTONOSUPPORT
);
440 if (p
->p_prison
&& jail_socket_unixiproute_only
&&
441 prp
->pr_domain
->dom_family
!= PF_LOCAL
&&
442 prp
->pr_domain
->dom_family
!= PF_INET
&&
443 prp
->pr_domain
->dom_family
!= PF_ROUTE
) {
444 return (EPROTONOSUPPORT
);
448 if (prp
->pr_type
!= type
)
450 so
= soalloc(p
!= 0, dom
, type
);
454 TAILQ_INIT(&so
->so_incomp
);
455 TAILQ_INIT(&so
->so_comp
);
460 so
->so_uid
= kauth_cred_getuid(kauth_cred_get());
461 if (!suser(kauth_cred_get(),NULL
))
462 so
->so_state
= SS_PRIV
;
465 so
->so_cred
= kauth_cred_get_with_ref();
469 so
->so_rcv
.sb_flags
|= SB_RECV
; /* XXX */
470 so
->so_rcv
.sb_so
= so
->so_snd
.sb_so
= so
;
472 so
->next_lock_lr
= 0;
473 so
->next_unlock_lr
= 0;
476 //### Attachement will create the per pcb lock if necessary and increase refcount
477 so
->so_usecount
++; /* for creation, make sure it's done before socket is inserted in lists */
479 error
= (*prp
->pr_usrreqs
->pru_attach
)(so
, proto
, p
);
483 * If so_pcb is not zero, the socket will be leaked,
484 * so protocol attachment handler must be coded carefuly
486 so
->so_state
|= SS_NOFDREF
;
488 sofreelastref(so
, 1); /* will deallocate the socket */
492 prp
->pr_domain
->dom_refs
++;
493 TAILQ_INIT(&so
->so_evlist
);
495 /* Attach socket filters for this protocol */
498 if (tcpconsdebug
== 2)
499 so
->so_options
|= SO_DEBUG
;
510 struct sockaddr
*nam
;
513 struct proc
*p
= current_proc();
515 struct socket_filter_entry
*filter
;
522 for (filter
= so
->so_filt
; filter
&& (error
== 0);
523 filter
= filter
->sfe_next_onsocket
) {
524 if (filter
->sfe_filter
->sf_filter
.sf_bind
) {
528 socket_unlock(so
, 0);
530 error
= filter
->sfe_filter
->sf_filter
.sf_bind(
531 filter
->sfe_cookie
, so
, nam
);
538 /* End socket filter */
541 error
= (*so
->so_proto
->pr_usrreqs
->pru_bind
)(so
, nam
, p
);
543 socket_unlock(so
, 1);
545 if (error
== EJUSTRETURN
)
555 so
->so_gencnt
= ++so_gencnt
;
558 if (so
->so_rcv
.sb_hiwat
)
559 (void)chgsbsize(so
->so_cred
->cr_uidinfo
,
560 &so
->so_rcv
.sb_hiwat
, 0, RLIM_INFINITY
);
561 if (so
->so_snd
.sb_hiwat
)
562 (void)chgsbsize(so
->so_cred
->cr_uidinfo
,
563 &so
->so_snd
.sb_hiwat
, 0, RLIM_INFINITY
);
565 if (so
->so_accf
!= NULL
) {
566 if (so
->so_accf
->so_accept_filter
!= NULL
&&
567 so
->so_accf
->so_accept_filter
->accf_destroy
!= NULL
) {
568 so
->so_accf
->so_accept_filter
->accf_destroy(so
);
570 if (so
->so_accf
->so_accept_filter_str
!= NULL
)
571 FREE(so
->so_accf
->so_accept_filter_str
, M_ACCF
);
572 FREE(so
->so_accf
, M_ACCF
);
575 kauth_cred_unref(&so
->so_cred
);
576 zfreei(so
->so_zone
, so
);
578 if (so
->cached_in_sock_layer
== 1)
579 cached_sock_free(so
);
581 if (so
->cached_in_sock_layer
== -1)
582 panic("sodealloc: double dealloc: so=%x\n", so
);
583 so
->cached_in_sock_layer
= -1;
584 FREE_ZONE(so
, sizeof(*so
), so
->so_zone
);
586 #endif /* __APPLE__ */
590 solisten(so
, backlog
)
591 register struct socket
*so
;
595 struct proc
*p
= current_proc();
601 struct socket_filter_entry
*filter
;
604 for (filter
= so
->so_filt
; filter
&& (error
== 0);
605 filter
= filter
->sfe_next_onsocket
) {
606 if (filter
->sfe_filter
->sf_filter
.sf_listen
) {
610 socket_unlock(so
, 0);
612 error
= filter
->sfe_filter
->sf_filter
.sf_listen(
613 filter
->sfe_cookie
, so
);
623 error
= (*so
->so_proto
->pr_usrreqs
->pru_listen
)(so
, p
);
627 socket_unlock(so
, 1);
628 if (error
== EJUSTRETURN
)
633 if (TAILQ_EMPTY(&so
->so_comp
))
634 so
->so_options
|= SO_ACCEPTCONN
;
635 if (backlog
< 0 || backlog
> somaxconn
)
637 so
->so_qlimit
= backlog
;
639 socket_unlock(so
, 1);
644 sofreelastref(so
, dealloc
)
645 register struct socket
*so
;
649 struct socket
*head
= so
->so_head
;
651 /*### Assume socket is locked */
653 /* Remove any filters - may be called more than once */
656 if ((!(so
->so_flags
& SOF_PCBCLEARING
)) || ((so
->so_state
& SS_NOFDREF
) == 0)) {
658 selthreadclear(&so
->so_snd
.sb_sel
);
659 selthreadclear(&so
->so_rcv
.sb_sel
);
660 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
661 so
->so_snd
.sb_flags
&= ~SB_UPCALL
;
666 socket_lock(head
, 1);
667 if (so
->so_state
& SS_INCOMP
) {
668 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
670 } else if (so
->so_state
& SS_COMP
) {
672 * We must not decommission a socket that's
673 * on the accept(2) queue. If we do, then
674 * accept(2) may hang after select(2) indicated
675 * that the listening socket was ready.
678 selthreadclear(&so
->so_snd
.sb_sel
);
679 selthreadclear(&so
->so_rcv
.sb_sel
);
680 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
681 so
->so_snd
.sb_flags
&= ~SB_UPCALL
;
683 socket_unlock(head
, 1);
686 panic("sofree: not queued");
689 so
->so_state
&= ~SS_INCOMP
;
691 socket_unlock(head
, 1);
694 selthreadclear(&so
->so_snd
.sb_sel
);
695 sbrelease(&so
->so_snd
);
699 /* 3932268: disable upcall */
700 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
701 so
->so_snd
.sb_flags
&= ~SB_UPCALL
;
708 * Close a socket on last file table reference removal.
709 * Initiate disconnect if connected.
710 * Free socket when disconnect complete.
714 register struct socket
*so
;
717 lck_mtx_t
* mutex_held
;
720 if (so
->so_usecount
== 0) {
721 panic("soclose: so=%x refcount=0\n", so
);
724 sflt_notify(so
, sock_evt_closing
, NULL
);
726 if ((so
->so_options
& SO_ACCEPTCONN
)) {
729 /* We do not want new connection to be added to the connection queues */
730 so
->so_options
&= ~SO_ACCEPTCONN
;
732 while ((sp
= TAILQ_FIRST(&so
->so_incomp
)) != NULL
) {
733 /* A bit tricky here. We need to keep
734 * a lock if it's a protocol global lock
735 * but we want the head, not the socket locked
736 * in the case of per-socket lock...
738 if (so
->so_proto
->pr_getlock
!= NULL
) {
739 socket_unlock(so
, 0);
743 if (so
->so_proto
->pr_getlock
!= NULL
) {
744 socket_unlock(sp
, 1);
749 while ((sp
= TAILQ_FIRST(&so
->so_comp
)) != NULL
) {
750 /* Dequeue from so_comp since sofree() won't do it */
751 TAILQ_REMOVE(&so
->so_comp
, sp
, so_list
);
754 if (so
->so_proto
->pr_getlock
!= NULL
) {
755 socket_unlock(so
, 0);
759 sp
->so_state
&= ~SS_COMP
;
763 if (so
->so_proto
->pr_getlock
!= NULL
) {
764 socket_unlock(sp
, 1);
769 if (so
->so_pcb
== 0) {
770 /* 3915887: mark the socket as ready for dealloc */
771 so
->so_flags
|= SOF_PCBCLEARING
;
774 if (so
->so_state
& SS_ISCONNECTED
) {
775 if ((so
->so_state
& SS_ISDISCONNECTING
) == 0) {
776 error
= sodisconnectlocked(so
);
780 if (so
->so_options
& SO_LINGER
) {
781 if ((so
->so_state
& SS_ISDISCONNECTING
) &&
782 (so
->so_state
& SS_NBIO
))
784 if (so
->so_proto
->pr_getlock
!= NULL
)
785 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
787 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
788 while (so
->so_state
& SS_ISCONNECTED
) {
789 ts
.tv_sec
= (so
->so_linger
/100);
790 ts
.tv_nsec
= (so
->so_linger
% 100) * NSEC_PER_USEC
* 1000 * 10;
791 error
= msleep((caddr_t
)&so
->so_timeo
, mutex_held
,
792 PSOCK
| PCATCH
, "soclos", &ts
);
794 /* It's OK when the time fires, don't report an error */
795 if (error
== EWOULDBLOCK
)
803 if (so
->so_usecount
== 0)
804 panic("soclose: usecount is zero so=%x\n", so
);
805 if (so
->so_pcb
&& !(so
->so_flags
& SOF_PCBCLEARING
)) {
806 int error2
= (*so
->so_proto
->pr_usrreqs
->pru_detach
)(so
);
810 if (so
->so_usecount
<= 0)
811 panic("soclose: usecount is zero so=%x\n", so
);
813 if (so
->so_pcb
&& so
->so_state
& SS_NOFDREF
)
814 panic("soclose: NOFDREF");
815 so
->so_state
|= SS_NOFDREF
;
817 so
->so_proto
->pr_domain
->dom_refs
--;
827 register struct socket
*so
;
831 if (so
->so_retaincnt
== 0)
832 error
= soclose_locked(so
);
833 else { /* if the FD is going away, but socket is retained in kernel remove its reference */
835 if (so
->so_usecount
< 2)
836 panic("soclose: retaincnt non null and so=%x usecount=%x\n", so
->so_usecount
);
838 socket_unlock(so
, 1);
844 * Must be called at splnet...
846 //#### Should already be locked
853 #ifdef MORE_LOCKING_DEBUG
854 lck_mtx_t
* mutex_held
;
856 if (so
->so_proto
->pr_getlock
!= NULL
)
857 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
859 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
860 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
863 error
= (*so
->so_proto
->pr_usrreqs
->pru_abort
)(so
);
872 soacceptlock(so
, nam
, dolock
)
873 register struct socket
*so
;
874 struct sockaddr
**nam
;
879 if (dolock
) socket_lock(so
, 1);
881 if ((so
->so_state
& SS_NOFDREF
) == 0)
882 panic("soaccept: !NOFDREF");
883 so
->so_state
&= ~SS_NOFDREF
;
884 error
= (*so
->so_proto
->pr_usrreqs
->pru_accept
)(so
, nam
);
886 if (dolock
) socket_unlock(so
, 1);
891 register struct socket
*so
;
892 struct sockaddr
**nam
;
894 return (soacceptlock(so
, nam
, 1));
898 soconnectlock(so
, nam
, dolock
)
899 register struct socket
*so
;
900 struct sockaddr
*nam
;
906 struct proc
*p
= current_proc();
908 if (dolock
) socket_lock(so
, 1);
910 if (so
->so_options
& SO_ACCEPTCONN
) {
911 if (dolock
) socket_unlock(so
, 1);
915 * If protocol is connection-based, can only connect once.
916 * Otherwise, if connected, try to disconnect first.
917 * This allows user to disconnect by connecting to, e.g.,
920 if (so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
) &&
921 ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) ||
922 (error
= sodisconnectlocked(so
))))
926 * Run connect filter before calling protocol:
927 * - non-blocking connect returns before completion;
930 struct socket_filter_entry
*filter
;
933 for (filter
= so
->so_filt
; filter
&& (error
== 0);
934 filter
= filter
->sfe_next_onsocket
) {
935 if (filter
->sfe_filter
->sf_filter
.sf_connect_out
) {
939 socket_unlock(so
, 0);
941 error
= filter
->sfe_filter
->sf_filter
.sf_connect_out(
942 filter
->sfe_cookie
, so
, nam
);
951 if (error
== EJUSTRETURN
)
953 if (dolock
) socket_unlock(so
, 1);
957 error
= (*so
->so_proto
->pr_usrreqs
->pru_connect
)(so
, nam
, p
);
959 if (dolock
) socket_unlock(so
, 1);
965 register struct socket
*so
;
966 struct sockaddr
*nam
;
968 return (soconnectlock(so
, nam
, 1));
973 register struct socket
*so1
;
979 if (so2
->so_proto
->pr_lock
)
982 error
= (*so1
->so_proto
->pr_usrreqs
->pru_connect2
)(so1
, so2
);
984 socket_unlock(so1
, 1);
985 if (so2
->so_proto
->pr_lock
)
986 socket_unlock(so2
, 1);
992 sodisconnectlocked(so
)
993 register struct socket
*so
;
997 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
1001 if (so
->so_state
& SS_ISDISCONNECTING
) {
1006 error
= (*so
->so_proto
->pr_usrreqs
->pru_disconnect
)(so
);
1009 sflt_notify(so
, sock_evt_disconnected
, NULL
);
1015 //### Locking version
1018 register struct socket
*so
;
1023 error
= sodisconnectlocked(so
);
1024 socket_unlock(so
, 1);
1028 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_DONTWAIT : M_WAIT)
1031 * sosendcheck will lock the socket buffer if it isn't locked and
1032 * verify that there is space for the data being inserted.
1038 struct sockaddr
*addr
,
1050 if (*sblocked
== 0) {
1051 if ((so
->so_snd
.sb_flags
& SB_LOCK
) != 0 &&
1052 so
->so_send_filt_thread
!= 0 &&
1053 so
->so_send_filt_thread
== current_thread()) {
1055 * We're being called recursively from a filter,
1056 * allow this to continue. Radar 4150520.
1057 * Don't set sblocked because we don't want
1058 * to perform an unlock later.
1063 error
= sblock(&so
->so_snd
, SBLOCKWAIT(flags
));
1071 if (so
->so_state
& SS_CANTSENDMORE
)
1075 error
= so
->so_error
;
1080 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
1082 * `sendto' and `sendmsg' is allowed on a connection-
1083 * based socket if it supports implied connect.
1084 * Return ENOTCONN if not connected and no address is
1087 if ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) &&
1088 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) == 0) {
1089 if ((so
->so_state
& SS_ISCONFIRMING
) == 0 &&
1090 !(resid
== 0 && clen
!= 0))
1092 } else if (addr
== 0 && !(flags
&MSG_HOLD
))
1093 return (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) ? ENOTCONN
: EDESTADDRREQ
;
1095 space
= sbspace(&so
->so_snd
);
1096 if (flags
& MSG_OOB
)
1098 if ((atomic
&& resid
> so
->so_snd
.sb_hiwat
) ||
1099 clen
> so
->so_snd
.sb_hiwat
)
1101 if (space
< resid
+ clen
&&
1102 (atomic
|| space
< so
->so_snd
.sb_lowat
|| space
< clen
)) {
1103 if ((so
->so_state
& SS_NBIO
) || (flags
& MSG_NBIO
) || assumelock
) {
1106 sbunlock(&so
->so_snd
, 1);
1107 error
= sbwait(&so
->so_snd
);
1119 * If send must go all at once and message is larger than
1120 * send buffering, then hard error.
1121 * Lock against other senders.
1122 * If must go all at once and not enough room now, then
1123 * inform user that this would block and do nothing.
1124 * Otherwise, if nonblocking, send as much as possible.
1125 * The data to be sent is described by "uio" if nonzero,
1126 * otherwise by the mbuf chain "top" (which must be null
1127 * if uio is not). Data provided in mbuf chain must be small
1128 * enough to send all at once.
1130 * Returns nonzero on error, timeout or signal; callers
1131 * must check for short counts if EINTR/ERESTART are returned.
1132 * Data and control buffers are freed on return.
1134 * MSG_HOLD: go thru most of sosend(), but just enqueue the mbuf
1135 * MSG_SEND: go thru as for MSG_HOLD on current fragment, then
1136 * point at the mbuf chain being constructed and go from there.
1139 sosend(so
, addr
, uio
, top
, control
, flags
)
1140 register struct socket
*so
;
1141 struct sockaddr
*addr
;
1144 struct mbuf
*control
;
1149 register struct mbuf
*m
, *freelist
= NULL
;
1150 register long space
, len
, resid
;
1151 int clen
= 0, error
, dontroute
, mlen
, sendflags
;
1152 int atomic
= sosendallatonce(so
) || top
;
1154 struct proc
*p
= current_proc();
1157 // LP64todo - fix this!
1158 resid
= uio_resid(uio
);
1160 resid
= top
->m_pkthdr
.len
;
1162 KERNEL_DEBUG((DBG_FNC_SOSEND
| DBG_FUNC_START
),
1166 so
->so_snd
.sb_lowat
,
1167 so
->so_snd
.sb_hiwat
);
1172 * In theory resid should be unsigned.
1173 * However, space must be signed, as it might be less than 0
1174 * if we over-committed, and we must use a signed comparison
1175 * of space and resid. On the other hand, a negative resid
1176 * causes us to loop sending 0-length segments to the protocol.
1178 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
1179 * type sockets since that's an error.
1181 if (resid
< 0 || (so
->so_type
== SOCK_STREAM
&& (flags
& MSG_EOR
))) {
1183 socket_unlock(so
, 1);
1188 (flags
& MSG_DONTROUTE
) && (so
->so_options
& SO_DONTROUTE
) == 0 &&
1189 (so
->so_proto
->pr_flags
& PR_ATOMIC
);
1191 p
->p_stats
->p_ru
.ru_msgsnd
++;
1193 clen
= control
->m_len
;
1196 error
= sosendcheck(so
, addr
, resid
, clen
, atomic
, flags
, &sblocked
);
1201 space
= sbspace(&so
->so_snd
) - clen
+ ((flags
& MSG_OOB
) ? 1024 : 0);
1207 * Data is prepackaged in "top".
1210 if (flags
& MSG_EOR
)
1211 top
->m_flags
|= M_EOR
;
1216 bytes_to_copy
= min(resid
, space
);
1218 if (sosendminchain
> 0) {
1221 chainlength
= sosendmaxchain
;
1223 socket_unlock(so
, 0);
1227 int hdrs_needed
= (top
== 0) ? 1 : 0;
1230 * try to maintain a local cache of mbuf clusters needed to complete this write
1231 * the list is further limited to the number that are currently needed to fill the socket
1232 * this mechanism allows a large number of mbufs/clusters to be grabbed under a single
1233 * mbuf lock... if we can't get any clusters, than fall back to trying for mbufs
1234 * if we fail early (or miscalcluate the number needed) make sure to release any clusters
1235 * we haven't yet consumed.
1237 if (freelist
== NULL
&& bytes_to_copy
> MCLBYTES
) {
1238 num_needed
= bytes_to_copy
/ NBPG
;
1240 if ((bytes_to_copy
- (num_needed
* NBPG
)) >= MINCLSIZE
)
1243 freelist
= m_getpackets_internal(&num_needed
, hdrs_needed
, M_WAIT
, 0, NBPG
);
1244 /* Fall back to cluster size if allocation failed */
1247 if (freelist
== NULL
&& bytes_to_copy
> MINCLSIZE
) {
1248 num_needed
= bytes_to_copy
/ MCLBYTES
;
1250 if ((bytes_to_copy
- (num_needed
* MCLBYTES
)) >= MINCLSIZE
)
1253 freelist
= m_getpackets_internal(&num_needed
, hdrs_needed
, M_WAIT
, 0, MCLBYTES
);
1254 /* Fall back to a single mbuf if allocation failed */
1257 if (freelist
== NULL
) {
1259 MGETHDR(freelist
, M_WAIT
, MT_DATA
);
1261 MGET(freelist
, M_WAIT
, MT_DATA
);
1263 if (freelist
== NULL
) {
1269 * For datagram protocols, leave room
1270 * for protocol headers in first mbuf.
1272 if (atomic
&& top
== 0 && bytes_to_copy
< MHLEN
)
1273 MH_ALIGN(freelist
, bytes_to_copy
);
1276 freelist
= m
->m_next
;
1279 if ((m
->m_flags
& M_EXT
))
1280 mlen
= m
->m_ext
.ext_size
;
1281 else if ((m
->m_flags
& M_PKTHDR
))
1282 mlen
= MHLEN
- m_leadingspace(m
);
1285 len
= min(mlen
, bytes_to_copy
);
1291 error
= uiomove(mtod(m
, caddr_t
), (int)len
, uio
);
1293 // LP64todo - fix this!
1294 resid
= uio_resid(uio
);
1298 top
->m_pkthdr
.len
+= len
;
1303 if (flags
& MSG_EOR
)
1304 top
->m_flags
|= M_EOR
;
1307 bytes_to_copy
= min(resid
, space
);
1309 } while (space
> 0 && (chainlength
< sosendmaxchain
|| atomic
|| resid
< MINCLSIZE
));
1317 if (flags
& (MSG_HOLD
|MSG_SEND
))
1319 /* Enqueue for later, go away if HOLD */
1320 register struct mbuf
*mb1
;
1321 if (so
->so_temp
&& (flags
& MSG_FLUSH
))
1323 m_freem(so
->so_temp
);
1327 so
->so_tail
->m_next
= top
;
1334 if (flags
& MSG_HOLD
)
1342 so
->so_options
|= SO_DONTROUTE
;
1343 /* Compute flags here, for pru_send and NKEs */
1344 sendflags
= (flags
& MSG_OOB
) ? PRUS_OOB
:
1346 * If the user set MSG_EOF, the protocol
1347 * understands this flag and nothing left to
1348 * send then use PRU_SEND_EOF instead of PRU_SEND.
1350 ((flags
& MSG_EOF
) &&
1351 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) &&
1354 /* If there is more to send set PRUS_MORETOCOME */
1355 (resid
> 0 && space
> 0) ? PRUS_MORETOCOME
: 0;
1358 * Socket filter processing
1361 struct socket_filter_entry
*filter
;
1366 for (filter
= so
->so_filt
; filter
&& (error
== 0);
1367 filter
= filter
->sfe_next_onsocket
) {
1368 if (filter
->sfe_filter
->sf_filter
.sf_data_out
) {
1370 if (filtered
== 0) {
1372 so
->so_send_filt_thread
= current_thread();
1374 socket_unlock(so
, 0);
1375 so_flags
= (sendflags
& MSG_OOB
) ? sock_data_filt_flag_oob
: 0;
1377 error
= filter
->sfe_filter
->sf_filter
.sf_data_out(
1378 filter
->sfe_cookie
, so
, addr
, &top
, &control
, so_flags
);
1384 * At this point, we've run at least one filter.
1385 * The socket is unlocked as is the socket buffer.
1389 so
->so_send_filt_thread
= 0;
1391 if (error
== EJUSTRETURN
) {
1403 * End Socket filter processing
1406 if (error
== EJUSTRETURN
) {
1407 /* A socket filter handled this data */
1411 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)(so
,
1412 sendflags
, top
, addr
, control
, p
);
1415 if (flags
& MSG_SEND
)
1419 so
->so_options
&= ~SO_DONTROUTE
;
1426 } while (resid
&& space
> 0);
1431 sbunlock(&so
->so_snd
, 0); /* will unlock socket */
1433 socket_unlock(so
, 1);
1440 m_freem_list(freelist
);
1442 KERNEL_DEBUG(DBG_FNC_SOSEND
| DBG_FUNC_END
,
1453 * Implement receive operations on a socket.
1454 * We depend on the way that records are added to the sockbuf
1455 * by sbappend*. In particular, each record (mbufs linked through m_next)
1456 * must begin with an address if the protocol so specifies,
1457 * followed by an optional mbuf or mbufs containing ancillary data,
1458 * and then zero or more mbufs of data.
1459 * In order to avoid blocking network interrupts for the entire time here,
1460 * we splx() while doing the actual copy to user space.
1461 * Although the sockbuf is locked, new data may still be appended,
1462 * and thus we must maintain consistency of the sockbuf during that time.
1464 * The caller may receive the data as a single mbuf chain by supplying
1465 * an mbuf **mp0 for use in returning the chain. The uio is then used
1466 * only for the count in uio_resid.
1469 soreceive(so
, psa
, uio
, mp0
, controlp
, flagsp
)
1470 register struct socket
*so
;
1471 struct sockaddr
**psa
;
1474 struct mbuf
**controlp
;
1477 register struct mbuf
*m
, **mp
, *ml
= NULL
;
1478 register int flags
, len
, error
, offset
;
1479 struct protosw
*pr
= so
->so_proto
;
1480 struct mbuf
*nextrecord
;
1482 // LP64todo - fix this!
1483 int orig_resid
= uio_resid(uio
);
1484 volatile struct mbuf
*free_list
;
1485 volatile int delayed_copy_len
;
1488 struct proc
*p
= current_proc();
1491 // LP64todo - fix this!
1492 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_START
,
1496 so
->so_rcv
.sb_lowat
,
1497 so
->so_rcv
.sb_hiwat
);
1501 #ifdef MORE_LOCKING_DEBUG
1502 if (so
->so_usecount
== 1)
1503 panic("soreceive: so=%x no other reference on socket\n", so
);
1511 flags
= *flagsp
&~ MSG_EOR
;
1515 * When SO_WANTOOBFLAG is set we try to get out-of-band data
1516 * regardless of the flags argument. Here is the case were
1517 * out-of-band data is not inline.
1519 if ((flags
& MSG_OOB
) ||
1520 ((so
->so_options
& SO_WANTOOBFLAG
) != 0 &&
1521 (so
->so_options
& SO_OOBINLINE
) == 0 &&
1522 (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
)))) {
1523 m
= m_get(M_WAIT
, MT_DATA
);
1525 socket_unlock(so
, 1);
1526 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, ENOBUFS
,0,0,0,0);
1529 error
= (*pr
->pr_usrreqs
->pru_rcvoob
)(so
, m
, flags
& MSG_PEEK
);
1532 socket_unlock(so
, 0);
1534 // LP64todo - fix this!
1535 error
= uiomove(mtod(m
, caddr_t
),
1536 (int) min(uio_resid(uio
), m
->m_len
), uio
);
1538 } while (uio_resid(uio
) && error
== 0 && m
);
1544 if ((so
->so_options
& SO_WANTOOBFLAG
) != 0) {
1545 if (error
== EWOULDBLOCK
|| error
== EINVAL
) {
1547 * Let's try to get normal data:
1548 * EWOULDBLOCK: out-of-band data not receive yet;
1549 * EINVAL: out-of-band data already read.
1553 } else if (error
== 0 && flagsp
)
1556 socket_unlock(so
, 1);
1557 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1563 *mp
= (struct mbuf
*)0;
1564 if (so
->so_state
& SS_ISCONFIRMING
&& uio_resid(uio
))
1565 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, 0);
1568 free_list
= (struct mbuf
*)0;
1569 delayed_copy_len
= 0;
1571 #ifdef MORE_LOCKING_DEBUG
1572 if (so
->so_usecount
<= 1)
1573 printf("soreceive: sblock so=%x ref=%d on socket\n", so
, so
->so_usecount
);
1575 error
= sblock(&so
->so_rcv
, SBLOCKWAIT(flags
));
1577 socket_unlock(so
, 1);
1578 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1582 m
= so
->so_rcv
.sb_mb
;
1584 * If we have less data than requested, block awaiting more
1585 * (subject to any timeout) if:
1586 * 1. the current count is less than the low water mark, or
1587 * 2. MSG_WAITALL is set, and it is possible to do the entire
1588 * receive operation at once if we block (resid <= hiwat).
1589 * 3. MSG_DONTWAIT is not set
1590 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1591 * we have to do the receive in sections, and thus risk returning
1592 * a short count if a timeout or signal occurs after we start.
1594 if (m
== 0 || (((flags
& MSG_DONTWAIT
) == 0 &&
1595 so
->so_rcv
.sb_cc
< uio_resid(uio
)) &&
1596 (so
->so_rcv
.sb_cc
< so
->so_rcv
.sb_lowat
||
1597 ((flags
& MSG_WAITALL
) && uio_resid(uio
) <= so
->so_rcv
.sb_hiwat
)) &&
1598 m
->m_nextpkt
== 0 && (pr
->pr_flags
& PR_ATOMIC
) == 0)) {
1600 KASSERT(m
!= 0 || !so
->so_rcv
.sb_cc
, ("receive 1"));
1604 error
= so
->so_error
;
1605 if ((flags
& MSG_PEEK
) == 0)
1609 if (so
->so_state
& SS_CANTRCVMORE
) {
1615 for (; m
; m
= m
->m_next
)
1616 if (m
->m_type
== MT_OOBDATA
|| (m
->m_flags
& M_EOR
)) {
1617 m
= so
->so_rcv
.sb_mb
;
1620 if ((so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
)) == 0 &&
1621 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)) {
1625 if (uio_resid(uio
) == 0)
1627 if ((so
->so_state
& SS_NBIO
) || (flags
& (MSG_DONTWAIT
|MSG_NBIO
))) {
1628 error
= EWOULDBLOCK
;
1631 sbunlock(&so
->so_rcv
, 1);
1632 #ifdef EVEN_MORE_LOCKING_DEBUG
1634 printf("Waiting for socket data\n");
1637 error
= sbwait(&so
->so_rcv
);
1638 #ifdef EVEN_MORE_LOCKING_DEBUG
1640 printf("SORECEIVE - sbwait returned %d\n", error
);
1642 if (so
->so_usecount
< 1)
1643 panic("soreceive: after 2nd sblock so=%x ref=%d on socket\n", so
, so
->so_usecount
);
1645 socket_unlock(so
, 1);
1646 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1654 uio
->uio_procp
->p_stats
->p_ru
.ru_msgrcv
++;
1655 #else /* __APPLE__ */
1658 * This should be uio->uio-procp; however, some callers of this
1659 * function use auto variables with stack garbage, and fail to
1660 * fill out the uio structure properly.
1663 p
->p_stats
->p_ru
.ru_msgrcv
++;
1664 #endif /* __APPLE__ */
1665 nextrecord
= m
->m_nextpkt
;
1666 if ((pr
->pr_flags
& PR_ADDR
) && m
->m_type
== MT_SONAME
) {
1667 KASSERT(m
->m_type
== MT_SONAME
, ("receive 1a"));
1670 *psa
= dup_sockaddr(mtod(m
, struct sockaddr
*),
1672 if ((*psa
== 0) && (flags
& MSG_NEEDSA
)) {
1673 error
= EWOULDBLOCK
;
1677 if (flags
& MSG_PEEK
) {
1680 sbfree(&so
->so_rcv
, m
);
1681 if (m
->m_next
== 0 && so
->so_rcv
.sb_cc
!= 0)
1682 panic("soreceive: about to create invalid socketbuf");
1683 MFREE(m
, so
->so_rcv
.sb_mb
);
1684 m
= so
->so_rcv
.sb_mb
;
1687 while (m
&& m
->m_type
== MT_CONTROL
&& error
== 0) {
1688 if (flags
& MSG_PEEK
) {
1690 *controlp
= m_copy(m
, 0, m
->m_len
);
1693 sbfree(&so
->so_rcv
, m
);
1695 if (pr
->pr_domain
->dom_externalize
&&
1696 mtod(m
, struct cmsghdr
*)->cmsg_type
==
1698 socket_unlock(so
, 0); /* release socket lock: see 3903171 */
1699 error
= (*pr
->pr_domain
->dom_externalize
)(m
);
1703 if (m
->m_next
== 0 && so
->so_rcv
.sb_cc
!= 0)
1704 panic("soreceive: so->so_rcv.sb_mb->m_next == 0 && so->so_rcv.sb_cc != 0");
1705 so
->so_rcv
.sb_mb
= m
->m_next
;
1707 m
= so
->so_rcv
.sb_mb
;
1709 MFREE(m
, so
->so_rcv
.sb_mb
);
1710 m
= so
->so_rcv
.sb_mb
;
1715 controlp
= &(*controlp
)->m_next
;
1719 if ((flags
& MSG_PEEK
) == 0)
1720 m
->m_nextpkt
= nextrecord
;
1722 if (type
== MT_OOBDATA
)
1728 if (!(flags
& MSG_PEEK
) && uio_resid(uio
) > sorecvmincopy
)
1735 while (m
&& (uio_resid(uio
) - delayed_copy_len
) > 0 && error
== 0) {
1736 if (m
->m_type
== MT_OOBDATA
) {
1737 if (type
!= MT_OOBDATA
)
1739 } else if (type
== MT_OOBDATA
)
1743 * This assertion needs rework. The trouble is Appletalk is uses many
1744 * mbuf types (NOT listed in mbuf.h!) which will trigger this panic.
1745 * For now just remove the assertion... CSM 9/98
1748 KASSERT(m
->m_type
== MT_DATA
|| m
->m_type
== MT_HEADER
,
1752 * Make sure to allways set MSG_OOB event when getting
1753 * out of band data inline.
1755 if ((so
->so_options
& SO_WANTOOBFLAG
) != 0 &&
1756 (so
->so_options
& SO_OOBINLINE
) != 0 &&
1757 (so
->so_state
& SS_RCVATMARK
) != 0) {
1761 so
->so_state
&= ~SS_RCVATMARK
;
1762 // LP64todo - fix this!
1763 len
= uio_resid(uio
) - delayed_copy_len
;
1764 if (so
->so_oobmark
&& len
> so
->so_oobmark
- offset
)
1765 len
= so
->so_oobmark
- offset
;
1766 if (len
> m
->m_len
- moff
)
1767 len
= m
->m_len
- moff
;
1769 * If mp is set, just pass back the mbufs.
1770 * Otherwise copy them out via the uio, then free.
1771 * Sockbuf must be consistent here (points to current mbuf,
1772 * it points to next record) when we drop priority;
1773 * we must note any additions to the sockbuf when we
1774 * block interrupts again.
1777 if (can_delay
&& len
== m
->m_len
) {
1779 * only delay the copy if we're consuming the
1780 * mbuf and we're NOT in MSG_PEEK mode
1781 * and we have enough data to make it worthwile
1782 * to drop and retake the funnel... can_delay
1783 * reflects the state of the 2 latter constraints
1784 * moff should always be zero in these cases
1786 delayed_copy_len
+= len
;
1789 if (delayed_copy_len
) {
1790 error
= sodelayed_copy(so
, uio
, &free_list
, &delayed_copy_len
);
1795 if (m
!= so
->so_rcv
.sb_mb
) {
1797 * can only get here if MSG_PEEK is not set
1798 * therefore, m should point at the head of the rcv queue...
1799 * if it doesn't, it means something drastically changed
1800 * while we were out from behind the funnel in sodelayed_copy...
1801 * perhaps a RST on the stream... in any event, the stream has
1802 * been interrupted... it's probably best just to return
1803 * whatever data we've moved and let the caller sort it out...
1808 socket_unlock(so
, 0);
1809 error
= uiomove(mtod(m
, caddr_t
) + moff
, (int)len
, uio
);
1816 uio_setresid(uio
, (uio_resid(uio
) - len
));
1818 if (len
== m
->m_len
- moff
) {
1819 if (m
->m_flags
& M_EOR
)
1821 if (flags
& MSG_PEEK
) {
1825 nextrecord
= m
->m_nextpkt
;
1826 sbfree(&so
->so_rcv
, m
);
1827 m
->m_nextpkt
= NULL
;
1832 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
1833 *mp
= (struct mbuf
*)0;
1835 if (free_list
== NULL
)
1840 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
1844 m
->m_nextpkt
= nextrecord
;
1847 if (flags
& MSG_PEEK
)
1851 *mp
= m_copym(m
, 0, len
, M_WAIT
);
1854 so
->so_rcv
.sb_cc
-= len
;
1857 if (so
->so_oobmark
) {
1858 if ((flags
& MSG_PEEK
) == 0) {
1859 so
->so_oobmark
-= len
;
1860 if (so
->so_oobmark
== 0) {
1861 so
->so_state
|= SS_RCVATMARK
;
1863 * delay posting the actual event until after
1864 * any delayed copy processing has finished
1871 if (offset
== so
->so_oobmark
)
1875 if (flags
& MSG_EOR
)
1878 * If the MSG_WAITALL or MSG_WAITSTREAM flag is set (for non-atomic socket),
1879 * we must not quit until "uio->uio_resid == 0" or an error
1880 * termination. If a signal/timeout occurs, return
1881 * with a short count but without error.
1882 * Keep sockbuf locked against other readers.
1884 while (flags
& (MSG_WAITALL
|MSG_WAITSTREAM
) && m
== 0 && (uio_resid(uio
) - delayed_copy_len
) > 0 &&
1885 !sosendallatonce(so
) && !nextrecord
) {
1886 if (so
->so_error
|| so
->so_state
& SS_CANTRCVMORE
)
1889 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
&& (((struct inpcb
*)so
->so_pcb
)->inp_state
!= INPCB_STATE_DEAD
))
1890 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, flags
);
1891 if (sbwait(&so
->so_rcv
)) {
1896 * have to wait until after we get back from the sbwait to do the copy because
1897 * we will drop the funnel if we have enough data that has been delayed... by dropping
1898 * the funnel we open up a window allowing the netisr thread to process the incoming packets
1899 * and to change the state of this socket... we're issuing the sbwait because
1900 * the socket is empty and we're expecting the netisr thread to wake us up when more
1901 * packets arrive... if we allow that processing to happen and then sbwait, we
1902 * could stall forever with packets sitting in the socket if no further packets
1903 * arrive from the remote side.
1905 * we want to copy before we've collected all the data to satisfy this request to
1906 * allow the copy to overlap the incoming packet processing on an MP system
1908 if (delayed_copy_len
> sorecvmincopy
&& (delayed_copy_len
> (so
->so_rcv
.sb_hiwat
/ 2))) {
1910 error
= sodelayed_copy(so
, uio
, &free_list
, &delayed_copy_len
);
1915 m
= so
->so_rcv
.sb_mb
;
1917 nextrecord
= m
->m_nextpkt
;
1921 #ifdef MORE_LOCKING_DEBUG
1922 if (so
->so_usecount
<= 1)
1923 panic("soreceive: after big while so=%x ref=%d on socket\n", so
, so
->so_usecount
);
1926 if (m
&& pr
->pr_flags
& PR_ATOMIC
) {
1928 if (so
->so_options
& SO_DONTTRUNC
)
1929 flags
|= MSG_RCVMORE
;
1933 if ((flags
& MSG_PEEK
) == 0)
1934 (void) sbdroprecord(&so
->so_rcv
);
1939 if ((flags
& MSG_PEEK
) == 0) {
1941 so
->so_rcv
.sb_mb
= nextrecord
;
1942 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
)
1943 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, flags
);
1946 if ((so
->so_options
& SO_WANTMORE
) && so
->so_rcv
.sb_cc
> 0)
1947 flags
|= MSG_HAVEMORE
;
1949 if (delayed_copy_len
) {
1950 error
= sodelayed_copy(so
, uio
, &free_list
, &delayed_copy_len
);
1956 m_freem_list((struct mbuf
*)free_list
);
1957 free_list
= (struct mbuf
*)0;
1960 postevent(so
, 0, EV_OOB
);
1962 if (orig_resid
== uio_resid(uio
) && orig_resid
&&
1963 (flags
& MSG_EOR
) == 0 && (so
->so_state
& SS_CANTRCVMORE
) == 0) {
1964 sbunlock(&so
->so_rcv
, 1);
1971 #ifdef MORE_LOCKING_DEBUG
1972 if (so
->so_usecount
<= 1)
1973 panic("soreceive: release so=%x ref=%d on socket\n", so
, so
->so_usecount
);
1975 if (delayed_copy_len
) {
1976 error
= sodelayed_copy(so
, uio
, &free_list
, &delayed_copy_len
);
1979 m_freem_list((struct mbuf
*)free_list
);
1981 sbunlock(&so
->so_rcv
, 0); /* will unlock socket */
1983 // LP64todo - fix this!
1984 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
,
1995 static int sodelayed_copy(struct socket
*so
, struct uio
*uio
, struct mbuf
**free_list
, int *resid
)
2002 socket_unlock(so
, 0);
2004 while (m
&& error
== 0) {
2006 error
= uiomove(mtod(m
, caddr_t
), (int)m
->m_len
, uio
);
2010 m_freem_list(*free_list
);
2012 *free_list
= (struct mbuf
*)NULL
;
2023 register struct socket
*so
;
2026 register struct protosw
*pr
= so
->so_proto
;
2031 sflt_notify(so
, sock_evt_shutdown
, &how
);
2033 if (how
!= SHUT_WR
) {
2035 postevent(so
, 0, EV_RCLOSED
);
2037 if (how
!= SHUT_RD
) {
2038 ret
= ((*pr
->pr_usrreqs
->pru_shutdown
)(so
));
2039 postevent(so
, 0, EV_WCLOSED
);
2040 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
2041 socket_unlock(so
, 1);
2045 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
2046 socket_unlock(so
, 1);
2052 register struct socket
*so
;
2054 register struct sockbuf
*sb
= &so
->so_rcv
;
2055 register struct protosw
*pr
= so
->so_proto
;
2058 #ifdef MORE_LOCKING_DEBUG
2059 lck_mtx_t
* mutex_held
;
2061 if (so
->so_proto
->pr_getlock
!= NULL
)
2062 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
2064 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
2065 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
2068 sflt_notify(so
, sock_evt_flush_read
, NULL
);
2070 sb
->sb_flags
|= SB_NOINTR
;
2071 (void) sblock(sb
, M_WAIT
);
2075 selthreadclear(&sb
->sb_sel
);
2078 bzero((caddr_t
)sb
, sizeof (*sb
));
2079 sb
->sb_so
= so
; /* reestablish link to socket */
2080 if (asb
.sb_flags
& SB_KNOTE
) {
2081 sb
->sb_sel
.si_note
= asb
.sb_sel
.si_note
;
2082 sb
->sb_flags
= SB_KNOTE
;
2084 if (pr
->pr_flags
& PR_RIGHTS
&& pr
->pr_domain
->dom_dispose
)
2085 (*pr
->pr_domain
->dom_dispose
)(asb
.sb_mb
);
2090 * Perhaps this routine, and sooptcopyout(), below, ought to come in
2091 * an additional variant to handle the case where the option value needs
2092 * to be some kind of integer, but not a specific size.
2093 * In addition to their use here, these functions are also called by the
2094 * protocol-level pr_ctloutput() routines.
2097 sooptcopyin(sopt
, buf
, len
, minlen
)
2098 struct sockopt
*sopt
;
2106 * If the user gives us more than we wanted, we ignore it,
2107 * but if we don't get the minimum length the caller
2108 * wants, we return EINVAL. On success, sopt->sopt_valsize
2109 * is set to however much we actually retrieved.
2111 if ((valsize
= sopt
->sopt_valsize
) < minlen
)
2114 sopt
->sopt_valsize
= valsize
= len
;
2116 if (sopt
->sopt_p
!= 0)
2117 return (copyin(sopt
->sopt_val
, buf
, valsize
));
2119 bcopy(CAST_DOWN(caddr_t
, sopt
->sopt_val
), buf
, valsize
);
2126 struct sockopt
*sopt
;
2135 if (sopt
->sopt_dir
!= SOPT_SET
) {
2136 sopt
->sopt_dir
= SOPT_SET
;
2140 struct socket_filter_entry
*filter
;
2143 for (filter
= so
->so_filt
; filter
&& (error
== 0);
2144 filter
= filter
->sfe_next_onsocket
) {
2145 if (filter
->sfe_filter
->sf_filter
.sf_setoption
) {
2146 if (filtered
== 0) {
2149 socket_unlock(so
, 0);
2151 error
= filter
->sfe_filter
->sf_filter
.sf_setoption(
2152 filter
->sfe_cookie
, so
, sopt
);
2156 if (filtered
!= 0) {
2161 if (error
== EJUSTRETURN
)
2169 if (sopt
->sopt_level
!= SOL_SOCKET
) {
2170 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
2171 error
= (*so
->so_proto
->pr_ctloutput
)
2173 socket_unlock(so
, 1);
2176 error
= ENOPROTOOPT
;
2178 switch (sopt
->sopt_name
) {
2181 error
= sooptcopyin(sopt
, &l
, sizeof l
, sizeof l
);
2185 so
->so_linger
= (sopt
->sopt_name
== SO_LINGER
) ? l
.l_linger
: l
.l_linger
* hz
;
2187 so
->so_options
|= SO_LINGER
;
2189 so
->so_options
&= ~SO_LINGER
;
2195 case SO_USELOOPBACK
:
2204 case SO_WANTOOBFLAG
:
2206 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2211 so
->so_options
|= sopt
->sopt_name
;
2213 so
->so_options
&= ~sopt
->sopt_name
;
2220 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2226 * Values < 1 make no sense for any of these
2227 * options, so disallow them.
2234 switch (sopt
->sopt_name
) {
2237 if (sbreserve(sopt
->sopt_name
== SO_SNDBUF
?
2238 &so
->so_snd
: &so
->so_rcv
,
2239 (u_long
) optval
) == 0) {
2246 * Make sure the low-water is never greater than
2250 so
->so_snd
.sb_lowat
=
2251 (optval
> so
->so_snd
.sb_hiwat
) ?
2252 so
->so_snd
.sb_hiwat
: optval
;
2255 so
->so_rcv
.sb_lowat
=
2256 (optval
> so
->so_rcv
.sb_hiwat
) ?
2257 so
->so_rcv
.sb_hiwat
: optval
;
2264 error
= sooptcopyin(sopt
, &tv
, sizeof tv
,
2269 if (tv
.tv_sec
< 0 || tv
.tv_sec
> LONG_MAX
||
2270 tv
.tv_usec
< 0 || tv
.tv_usec
>= 1000000) {
2275 switch (sopt
->sopt_name
) {
2277 so
->so_snd
.sb_timeo
= tv
;
2280 so
->so_rcv
.sb_timeo
= tv
;
2289 error
= sooptcopyin(sopt
, &nke
,
2290 sizeof nke
, sizeof nke
);
2294 error
= sflt_attach_private(so
, NULL
, nke
.nke_handle
, 1);
2299 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2304 so
->so_flags
|= SOF_NOSIGPIPE
;
2306 so
->so_flags
&= ~SOF_NOSIGPIPE
;
2311 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2316 so
->so_flags
|= SOF_NOADDRAVAIL
;
2318 so
->so_flags
&= ~SOF_NOADDRAVAIL
;
2323 error
= ENOPROTOOPT
;
2326 if (error
== 0 && so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
2327 (void) ((*so
->so_proto
->pr_ctloutput
)
2332 socket_unlock(so
, 1);
2336 /* Helper routine for getsockopt */
2338 sooptcopyout(sopt
, buf
, len
)
2339 struct sockopt
*sopt
;
2349 * Documented get behavior is that we always return a value,
2350 * possibly truncated to fit in the user's buffer.
2351 * Traditional behavior is that we always tell the user
2352 * precisely how much we copied, rather than something useful
2353 * like the total amount we had available for her.
2354 * Note that this interface is not idempotent; the entire answer must
2355 * generated ahead of time.
2357 valsize
= min(len
, sopt
->sopt_valsize
);
2358 sopt
->sopt_valsize
= valsize
;
2359 if (sopt
->sopt_val
!= USER_ADDR_NULL
) {
2360 if (sopt
->sopt_p
!= 0)
2361 error
= copyout(buf
, sopt
->sopt_val
, valsize
);
2363 bcopy(buf
, CAST_DOWN(caddr_t
, sopt
->sopt_val
), valsize
);
2371 struct sockopt
*sopt
;
2377 if (sopt
->sopt_dir
!= SOPT_GET
) {
2378 sopt
->sopt_dir
= SOPT_GET
;
2384 struct socket_filter_entry
*filter
;
2387 for (filter
= so
->so_filt
; filter
&& (error
== 0);
2388 filter
= filter
->sfe_next_onsocket
) {
2389 if (filter
->sfe_filter
->sf_filter
.sf_getoption
) {
2390 if (filtered
== 0) {
2393 socket_unlock(so
, 0);
2395 error
= filter
->sfe_filter
->sf_filter
.sf_getoption(
2396 filter
->sfe_cookie
, so
, sopt
);
2399 if (filtered
!= 0) {
2404 if (error
== EJUSTRETURN
)
2406 socket_unlock(so
, 1);
2413 if (sopt
->sopt_level
!= SOL_SOCKET
) {
2414 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
2415 error
= (*so
->so_proto
->pr_ctloutput
)
2417 socket_unlock(so
, 1);
2420 socket_unlock(so
, 1);
2421 return (ENOPROTOOPT
);
2424 switch (sopt
->sopt_name
) {
2427 l
.l_onoff
= so
->so_options
& SO_LINGER
;
2428 l
.l_linger
= (sopt
->sopt_name
== SO_LINGER
) ? so
->so_linger
:
2430 error
= sooptcopyout(sopt
, &l
, sizeof l
);
2433 case SO_USELOOPBACK
:
2445 case SO_WANTOOBFLAG
:
2447 optval
= so
->so_options
& sopt
->sopt_name
;
2449 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2453 optval
= so
->so_type
;
2463 m1
= so
->so_rcv
.sb_mb
;
2464 if (so
->so_proto
->pr_flags
& PR_ATOMIC
)
2467 if (m1
->m_type
== MT_DATA
)
2468 pkt_total
+= m1
->m_len
;
2473 optval
= so
->so_rcv
.sb_cc
;
2477 optval
= so
->so_snd
.sb_cc
;
2481 optval
= so
->so_error
;
2486 optval
= so
->so_snd
.sb_hiwat
;
2490 optval
= so
->so_rcv
.sb_hiwat
;
2494 optval
= so
->so_snd
.sb_lowat
;
2498 optval
= so
->so_rcv
.sb_lowat
;
2503 tv
= (sopt
->sopt_name
== SO_SNDTIMEO
?
2504 so
->so_snd
.sb_timeo
: so
->so_rcv
.sb_timeo
);
2506 error
= sooptcopyout(sopt
, &tv
, sizeof tv
);
2510 optval
= (so
->so_flags
& SOF_NOSIGPIPE
);
2514 optval
= (so
->so_flags
& SOF_NOADDRAVAIL
);
2518 error
= ENOPROTOOPT
;
2521 socket_unlock(so
, 1);
2526 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
2528 soopt_getm(struct sockopt
*sopt
, struct mbuf
**mp
)
2530 struct mbuf
*m
, *m_prev
;
2531 int sopt_size
= sopt
->sopt_valsize
;
2533 if (sopt_size
> MAX_SOOPTGETM_SIZE
)
2536 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
2539 if (sopt_size
> MLEN
) {
2540 MCLGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
);
2541 if ((m
->m_flags
& M_EXT
) == 0) {
2545 m
->m_len
= min(MCLBYTES
, sopt_size
);
2547 m
->m_len
= min(MLEN
, sopt_size
);
2549 sopt_size
-= m
->m_len
;
2554 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
2559 if (sopt_size
> MLEN
) {
2560 MCLGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
);
2561 if ((m
->m_flags
& M_EXT
) == 0) {
2565 m
->m_len
= min(MCLBYTES
, sopt_size
);
2567 m
->m_len
= min(MLEN
, sopt_size
);
2569 sopt_size
-= m
->m_len
;
2576 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2578 soopt_mcopyin(struct sockopt
*sopt
, struct mbuf
*m
)
2580 struct mbuf
*m0
= m
;
2582 if (sopt
->sopt_val
== USER_ADDR_NULL
)
2584 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
2585 if (sopt
->sopt_p
!= NULL
) {
2588 error
= copyin(sopt
->sopt_val
, mtod(m
, char *), m
->m_len
);
2594 bcopy(CAST_DOWN(caddr_t
, sopt
->sopt_val
), mtod(m
, char *), m
->m_len
);
2595 sopt
->sopt_valsize
-= m
->m_len
;
2596 sopt
->sopt_val
+= m
->m_len
;
2599 if (m
!= NULL
) /* should be allocated enoughly at ip6_sooptmcopyin() */
2600 panic("soopt_mcopyin");
2604 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2606 soopt_mcopyout(struct sockopt
*sopt
, struct mbuf
*m
)
2608 struct mbuf
*m0
= m
;
2611 if (sopt
->sopt_val
== USER_ADDR_NULL
)
2613 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
2614 if (sopt
->sopt_p
!= NULL
) {
2617 error
= copyout(mtod(m
, char *), sopt
->sopt_val
, m
->m_len
);
2623 bcopy(mtod(m
, char *), CAST_DOWN(caddr_t
, sopt
->sopt_val
), m
->m_len
);
2624 sopt
->sopt_valsize
-= m
->m_len
;
2625 sopt
->sopt_val
+= m
->m_len
;
2626 valsize
+= m
->m_len
;
2630 /* enough soopt buffer should be given from user-land */
2634 sopt
->sopt_valsize
= valsize
;
2640 register struct socket
*so
;
2644 if (so
->so_pgid
< 0)
2645 gsignal(-so
->so_pgid
, SIGURG
);
2646 else if (so
->so_pgid
> 0 && (p
= pfind(so
->so_pgid
)) != 0)
2648 selwakeup(&so
->so_rcv
.sb_sel
);
2652 sopoll(struct socket
*so
, int events
, __unused kauth_cred_t cred
, void * wql
)
2654 struct proc
*p
= current_proc();
2659 if (events
& (POLLIN
| POLLRDNORM
))
2661 revents
|= events
& (POLLIN
| POLLRDNORM
);
2663 if (events
& (POLLOUT
| POLLWRNORM
))
2664 if (sowriteable(so
))
2665 revents
|= events
& (POLLOUT
| POLLWRNORM
);
2667 if (events
& (POLLPRI
| POLLRDBAND
))
2668 if (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
))
2669 revents
|= events
& (POLLPRI
| POLLRDBAND
);
2672 if (events
& (POLLIN
| POLLPRI
| POLLRDNORM
| POLLRDBAND
)) {
2673 /* Darwin sets the flag first, BSD calls selrecord first */
2674 so
->so_rcv
.sb_flags
|= SB_SEL
;
2675 selrecord(p
, &so
->so_rcv
.sb_sel
, wql
);
2678 if (events
& (POLLOUT
| POLLWRNORM
)) {
2679 /* Darwin sets the flag first, BSD calls selrecord first */
2680 so
->so_snd
.sb_flags
|= SB_SEL
;
2681 selrecord(p
, &so
->so_snd
.sb_sel
, wql
);
2685 socket_unlock(so
, 1);
2689 int soo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
2692 soo_kqfilter(__unused
struct fileproc
*fp
, struct knote
*kn
, __unused
struct proc
*p
)
2694 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2698 switch (kn
->kn_filter
) {
2700 if (so
->so_options
& SO_ACCEPTCONN
)
2701 kn
->kn_fop
= &solisten_filtops
;
2703 kn
->kn_fop
= &soread_filtops
;
2707 kn
->kn_fop
= &sowrite_filtops
;
2711 socket_unlock(so
, 1);
2715 if (KNOTE_ATTACH(&sb
->sb_sel
.si_note
, kn
))
2716 sb
->sb_flags
|= SB_KNOTE
;
2717 socket_unlock(so
, 1);
2722 filt_sordetach(struct knote
*kn
)
2724 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2727 if (so
->so_rcv
.sb_flags
& SB_KNOTE
)
2728 if (KNOTE_DETACH(&so
->so_rcv
.sb_sel
.si_note
, kn
))
2729 so
->so_rcv
.sb_flags
&= ~SB_KNOTE
;
2730 socket_unlock(so
, 1);
2735 filt_soread(struct knote
*kn
, long hint
)
2737 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2739 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2742 if (so
->so_oobmark
) {
2743 if (kn
->kn_flags
& EV_OOBAND
) {
2744 kn
->kn_data
= so
->so_rcv
.sb_cc
- so
->so_oobmark
;
2745 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2746 socket_unlock(so
, 1);
2749 kn
->kn_data
= so
->so_oobmark
;
2750 kn
->kn_flags
|= EV_OOBAND
;
2752 kn
->kn_data
= so
->so_rcv
.sb_cc
;
2753 if (so
->so_state
& SS_CANTRCVMORE
) {
2754 kn
->kn_flags
|= EV_EOF
;
2755 kn
->kn_fflags
= so
->so_error
;
2756 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2757 socket_unlock(so
, 1);
2762 if (so
->so_state
& SS_RCVATMARK
) {
2763 if (kn
->kn_flags
& EV_OOBAND
) {
2764 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2765 socket_unlock(so
, 1);
2768 kn
->kn_flags
|= EV_OOBAND
;
2769 } else if (kn
->kn_flags
& EV_OOBAND
) {
2771 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2772 socket_unlock(so
, 1);
2776 if (so
->so_error
) { /* temporary udp error */
2777 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2778 socket_unlock(so
, 1);
2782 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2783 socket_unlock(so
, 1);
2785 return( kn
->kn_flags
& EV_OOBAND
||
2786 kn
->kn_data
>= ((kn
->kn_sfflags
& NOTE_LOWAT
) ?
2787 kn
->kn_sdata
: so
->so_rcv
.sb_lowat
));
2791 filt_sowdetach(struct knote
*kn
)
2793 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2796 if(so
->so_snd
.sb_flags
& SB_KNOTE
)
2797 if (KNOTE_DETACH(&so
->so_snd
.sb_sel
.si_note
, kn
))
2798 so
->so_snd
.sb_flags
&= ~SB_KNOTE
;
2799 socket_unlock(so
, 1);
2804 filt_sowrite(struct knote
*kn
, long hint
)
2806 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2808 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2811 kn
->kn_data
= sbspace(&so
->so_snd
);
2812 if (so
->so_state
& SS_CANTSENDMORE
) {
2813 kn
->kn_flags
|= EV_EOF
;
2814 kn
->kn_fflags
= so
->so_error
;
2815 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2816 socket_unlock(so
, 1);
2819 if (so
->so_error
) { /* temporary udp error */
2820 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2821 socket_unlock(so
, 1);
2824 if (((so
->so_state
& SS_ISCONNECTED
) == 0) &&
2825 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)) {
2826 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2827 socket_unlock(so
, 1);
2830 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2831 socket_unlock(so
, 1);
2832 if (kn
->kn_sfflags
& NOTE_LOWAT
)
2833 return (kn
->kn_data
>= kn
->kn_sdata
);
2834 return (kn
->kn_data
>= so
->so_snd
.sb_lowat
);
2839 filt_solisten(struct knote
*kn
, long hint
)
2841 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2844 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2846 kn
->kn_data
= so
->so_qlen
;
2847 isempty
= ! TAILQ_EMPTY(&so
->so_comp
);
2848 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2849 socket_unlock(so
, 1);
2855 socket_lock(so
, refcount
)
2859 int error
= 0, lr_saved
;
2861 lr_saved
= (unsigned int) __builtin_return_address(0);
2863 if (so
->so_proto
->pr_lock
) {
2864 error
= (*so
->so_proto
->pr_lock
)(so
, refcount
, lr_saved
);
2867 #ifdef MORE_LOCKING_DEBUG
2868 lck_mtx_assert(so
->so_proto
->pr_domain
->dom_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
2870 lck_mtx_lock(so
->so_proto
->pr_domain
->dom_mtx
);
2873 so
->lock_lr
[so
->next_lock_lr
] = (void *)lr_saved
;
2874 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
2882 socket_unlock(so
, refcount
)
2886 int error
= 0, lr_saved
;
2887 lck_mtx_t
* mutex_held
;
2889 lr_saved
= (unsigned int) __builtin_return_address(0);
2891 if (so
->so_proto
== NULL
)
2892 panic("socket_unlock null so_proto so=%x\n", so
);
2894 if (so
&& so
->so_proto
->pr_unlock
)
2895 error
= (*so
->so_proto
->pr_unlock
)(so
, refcount
, lr_saved
);
2897 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
2898 #ifdef MORE_LOCKING_DEBUG
2899 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
2901 so
->unlock_lr
[so
->next_unlock_lr
] = (void *)lr_saved
;
2902 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
2905 if (so
->so_usecount
<= 0)
2906 panic("socket_unlock: bad refcount so=%x value=%d\n", so
, so
->so_usecount
);
2908 if (so
->so_usecount
== 0) {
2909 sofreelastref(so
, 1);
2912 lck_mtx_unlock(mutex_held
);
2917 //### Called with socket locked, will unlock socket
2923 lck_mtx_t
* mutex_held
;
2924 if (so
->so_proto
->pr_getlock
!= NULL
)
2925 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
2927 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
2928 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
2930 sofreelastref(so
, 0);
2937 socket_lock(so
, 1); /* locks & take one reference on socket */
2938 socket_unlock(so
, 0); /* unlock only */
2946 socket_unlock(so
, 1);