2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1982, 1986, 1988, 1990, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
57 * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.16 2001/06/14 20:46:06 ume Exp $
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/filedesc.h>
63 #include <sys/proc_internal.h>
64 #include <sys/kauth.h>
65 #include <sys/file_internal.h>
66 #include <sys/fcntl.h>
67 #include <sys/malloc.h>
69 #include <sys/domain.h>
70 #include <sys/kernel.h>
71 #include <sys/event.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/resourcevar.h>
77 #include <sys/signalvar.h>
78 #include <sys/sysctl.h>
81 #include <sys/kdebug.h>
82 #include <net/route.h>
83 #include <netinet/in.h>
84 #include <netinet/in_pcb.h>
85 #include <kern/zalloc.h>
86 #include <kern/locks.h>
87 #include <machine/limits.h>
90 int so_cache_timeouts
= 0;
91 int so_cache_max_freed
= 0;
92 int cached_sock_count
= 0;
93 struct socket
*socket_cache_head
= 0;
94 struct socket
*socket_cache_tail
= 0;
95 u_long so_cache_time
= 0;
96 int so_cache_init_done
= 0;
97 struct zone
*so_cache_zone
;
98 extern int get_inpcb_str_size();
99 extern int get_tcp_str_size();
101 static lck_grp_t
*so_cache_mtx_grp
;
102 static lck_attr_t
*so_cache_mtx_attr
;
103 static lck_grp_attr_t
*so_cache_mtx_grp_attr
;
104 lck_mtx_t
*so_cache_mtx
;
106 #include <machine/limits.h>
108 static void filt_sordetach(struct knote
*kn
);
109 static int filt_soread(struct knote
*kn
, long hint
);
110 static void filt_sowdetach(struct knote
*kn
);
111 static int filt_sowrite(struct knote
*kn
, long hint
);
112 static int filt_solisten(struct knote
*kn
, long hint
);
114 static struct filterops solisten_filtops
=
115 { 1, NULL
, filt_sordetach
, filt_solisten
};
116 static struct filterops soread_filtops
=
117 { 1, NULL
, filt_sordetach
, filt_soread
};
118 static struct filterops sowrite_filtops
=
119 { 1, NULL
, filt_sowdetach
, filt_sowrite
};
121 #define EVEN_MORE_LOCKING_DEBUG 0
122 int socket_debug
= 0;
123 int socket_zone
= M_SOCKET
;
124 so_gen_t so_gencnt
; /* generation count for sockets */
126 MALLOC_DEFINE(M_SONAME
, "soname", "socket name");
127 MALLOC_DEFINE(M_PCB
, "pcb", "protocol control block");
129 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0)
130 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2)
131 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1)
132 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3)
133 #define DBG_FNC_SOSEND NETDBG_CODE(DBG_NETSOCK, (4 << 8) | 1)
134 #define DBG_FNC_SORECEIVE NETDBG_CODE(DBG_NETSOCK, (8 << 8))
135 #define DBG_FNC_SOSHUTDOWN NETDBG_CODE(DBG_NETSOCK, (9 << 8))
137 #define MAX_SOOPTGETM_SIZE (128 * MCLBYTES)
140 SYSCTL_DECL(_kern_ipc
);
142 static int somaxconn
= SOMAXCONN
;
143 SYSCTL_INT(_kern_ipc
, KIPC_SOMAXCONN
, somaxconn
, CTLFLAG_RW
, &somaxconn
,
146 /* Should we get a maximum also ??? */
147 static int sosendmaxchain
= 65536;
148 static int sosendminchain
= 16384;
149 static int sorecvmincopy
= 16384;
150 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sosendminchain
, CTLFLAG_RW
, &sosendminchain
,
152 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sorecvmincopy
, CTLFLAG_RW
, &sorecvmincopy
,
155 void so_cache_timer();
158 * Socket operation routines.
159 * These routines are called by the routines in
160 * sys_socket.c or from a system process, and
161 * implement the semantics of socket operations by
162 * switching out to the protocol specific routines.
167 vm_size_t so_cache_zone_element_size
;
169 static int sodelayed_copy(struct socket
*so
, struct uio
*uio
, struct mbuf
**free_list
, int *resid
);
176 if (so_cache_init_done
) {
177 printf("socketinit: already called...\n");
182 * allocate lock group attribute and group for socket cache mutex
184 so_cache_mtx_grp_attr
= lck_grp_attr_alloc_init();
185 lck_grp_attr_setdefault(so_cache_mtx_grp_attr
);
187 so_cache_mtx_grp
= lck_grp_alloc_init("so_cache", so_cache_mtx_grp_attr
);
190 * allocate the lock attribute for socket cache mutex
192 so_cache_mtx_attr
= lck_attr_alloc_init();
193 lck_attr_setdefault(so_cache_mtx_attr
);
195 so_cache_init_done
= 1;
197 so_cache_mtx
= lck_mtx_alloc_init(so_cache_mtx_grp
, so_cache_mtx_attr
); /* cached sockets mutex */
199 if (so_cache_mtx
== NULL
)
200 return; /* we're hosed... */
202 str_size
= (vm_size_t
)( sizeof(struct socket
) + 4 +
203 get_inpcb_str_size() + 4 +
205 so_cache_zone
= zinit (str_size
, 120000*str_size
, 8192, "socache zone");
207 printf("cached_sock_alloc -- so_cache_zone size is %x\n", str_size
);
209 timeout(so_cache_timer
, NULL
, (SO_CACHE_FLUSH_INTERVAL
* hz
));
211 so_cache_zone_element_size
= str_size
;
217 void cached_sock_alloc(so
, waitok
)
223 register u_long offset
;
226 lck_mtx_lock(so_cache_mtx
);
228 if (cached_sock_count
) {
230 *so
= socket_cache_head
;
232 panic("cached_sock_alloc: cached sock is null");
234 socket_cache_head
= socket_cache_head
->cache_next
;
235 if (socket_cache_head
)
236 socket_cache_head
->cache_prev
= 0;
238 socket_cache_tail
= 0;
240 lck_mtx_unlock(so_cache_mtx
);
242 temp
= (*so
)->so_saved_pcb
;
243 bzero((caddr_t
)*so
, sizeof(struct socket
));
245 kprintf("cached_sock_alloc - retreiving cached sock %x - count == %d\n", *so
,
248 (*so
)->so_saved_pcb
= temp
;
249 (*so
)->cached_in_sock_layer
= 1;
254 kprintf("Allocating cached sock %x from memory\n", *so
);
257 lck_mtx_unlock(so_cache_mtx
);
260 *so
= (struct socket
*) zalloc(so_cache_zone
);
262 *so
= (struct socket
*) zalloc_noblock(so_cache_zone
);
267 bzero((caddr_t
)*so
, sizeof(struct socket
));
270 * Define offsets for extra structures into our single block of
271 * memory. Align extra structures on longword boundaries.
275 offset
= (u_long
) *so
;
276 offset
+= sizeof(struct socket
);
279 offset
&= 0xfffffffc;
281 (*so
)->so_saved_pcb
= (caddr_t
) offset
;
282 offset
+= get_inpcb_str_size();
285 offset
&= 0xfffffffc;
288 ((struct inpcb
*) (*so
)->so_saved_pcb
)->inp_saved_ppcb
= (caddr_t
) offset
;
290 kprintf("Allocating cached socket - %x, pcb=%x tcpcb=%x\n", *so
,
292 ((struct inpcb
*)(*so
)->so_saved_pcb
)->inp_saved_ppcb
);
296 (*so
)->cached_in_sock_layer
= 1;
300 void cached_sock_free(so
)
304 lck_mtx_lock(so_cache_mtx
);
306 if (++cached_sock_count
> MAX_CACHED_SOCKETS
) {
308 lck_mtx_unlock(so_cache_mtx
);
310 kprintf("Freeing overflowed cached socket %x\n", so
);
312 zfree(so_cache_zone
, so
);
316 kprintf("Freeing socket %x into cache\n", so
);
318 if (so_cache_hw
< cached_sock_count
)
319 so_cache_hw
= cached_sock_count
;
321 so
->cache_next
= socket_cache_head
;
323 if (socket_cache_head
)
324 socket_cache_head
->cache_prev
= so
;
326 socket_cache_tail
= so
;
328 so
->cache_timestamp
= so_cache_time
;
329 socket_cache_head
= so
;
330 lck_mtx_unlock(so_cache_mtx
);
334 kprintf("Freed cached sock %x into cache - count is %d\n", so
, cached_sock_count
);
341 void so_cache_timer()
343 register struct socket
*p
;
344 register int n_freed
= 0;
347 lck_mtx_lock(so_cache_mtx
);
351 while ( (p
= socket_cache_tail
) )
353 if ((so_cache_time
- p
->cache_timestamp
) < SO_CACHE_TIME_LIMIT
)
358 if ( (socket_cache_tail
= p
->cache_prev
) )
359 p
->cache_prev
->cache_next
= 0;
360 if (--cached_sock_count
== 0)
361 socket_cache_head
= 0;
364 zfree(so_cache_zone
, p
);
366 if (++n_freed
>= SO_CACHE_MAX_FREE_BATCH
)
368 so_cache_max_freed
++;
372 lck_mtx_unlock(so_cache_mtx
);
374 timeout(so_cache_timer
, NULL
, (SO_CACHE_FLUSH_INTERVAL
* hz
));
378 #endif /* __APPLE__ */
381 * Get a socket structure from our zone, and initialize it.
382 * We don't implement `waitok' yet (see comments in uipc_domain.c).
383 * Note that it would probably be better to allocate socket
384 * and PCB at the same time, but I'm not convinced that all
385 * the protocols can be easily modified to do this.
388 soalloc(waitok
, dom
, type
)
395 if ((dom
== PF_INET
) && (type
== SOCK_STREAM
))
396 cached_sock_alloc(&so
, waitok
);
399 MALLOC_ZONE(so
, struct socket
*, sizeof(*so
), socket_zone
, M_WAITOK
);
401 bzero(so
, sizeof *so
);
403 /* XXX race condition for reentrant kernel */
404 //###LD Atomic add for so_gencnt
406 so
->so_gencnt
= ++so_gencnt
;
407 so
->so_zone
= socket_zone
;
414 socreate(dom
, aso
, type
, proto
)
420 struct proc
*p
= current_proc();
421 register struct protosw
*prp
;
422 register struct socket
*so
;
423 register int error
= 0;
425 extern int tcpconsdebug
;
428 prp
= pffindproto(dom
, proto
, type
);
430 prp
= pffindtype(dom
, type
);
432 if (prp
== 0 || prp
->pr_usrreqs
->pru_attach
== 0)
433 return (EPROTONOSUPPORT
);
436 if (p
->p_prison
&& jail_socket_unixiproute_only
&&
437 prp
->pr_domain
->dom_family
!= PF_LOCAL
&&
438 prp
->pr_domain
->dom_family
!= PF_INET
&&
439 prp
->pr_domain
->dom_family
!= PF_ROUTE
) {
440 return (EPROTONOSUPPORT
);
444 if (prp
->pr_type
!= type
)
446 so
= soalloc(p
!= 0, dom
, type
);
450 TAILQ_INIT(&so
->so_incomp
);
451 TAILQ_INIT(&so
->so_comp
);
456 so
->so_uid
= kauth_cred_getuid(kauth_cred_get());
457 if (!suser(kauth_cred_get(),NULL
))
458 so
->so_state
= SS_PRIV
;
461 so
->so_cred
= kauth_cred_get_with_ref();
465 so
->so_rcv
.sb_flags
|= SB_RECV
; /* XXX */
466 so
->so_rcv
.sb_so
= so
->so_snd
.sb_so
= so
;
469 //### Attachement will create the per pcb lock if necessary and increase refcount
471 error
= (*prp
->pr_usrreqs
->pru_attach
)(so
, proto
, p
);
475 * If so_pcb is not zero, the socket will be leaked,
476 * so protocol attachment handler must be coded carefuly
478 so
->so_state
|= SS_NOFDREF
;
479 sofreelastref(so
, 1);
484 prp
->pr_domain
->dom_refs
++;
485 TAILQ_INIT(&so
->so_evlist
);
487 /* Attach socket filters for this protocol */
490 if (tcpconsdebug
== 2)
491 so
->so_options
|= SO_DEBUG
;
502 struct sockaddr
*nam
;
505 struct proc
*p
= current_proc();
507 struct socket_filter_entry
*filter
;
514 for (filter
= so
->so_filt
; filter
&& (error
== 0);
515 filter
= filter
->sfe_next_onsocket
) {
516 if (filter
->sfe_filter
->sf_filter
.sf_bind
) {
520 socket_unlock(so
, 0);
522 error
= filter
->sfe_filter
->sf_filter
.sf_bind(
523 filter
->sfe_cookie
, so
, nam
);
530 /* End socket filter */
533 error
= (*so
->so_proto
->pr_usrreqs
->pru_bind
)(so
, nam
, p
);
535 socket_unlock(so
, 1);
537 if (error
== EJUSTRETURN
)
547 so
->so_gencnt
= ++so_gencnt
;
550 if (so
->so_rcv
.sb_hiwat
)
551 (void)chgsbsize(so
->so_cred
->cr_uidinfo
,
552 &so
->so_rcv
.sb_hiwat
, 0, RLIM_INFINITY
);
553 if (so
->so_snd
.sb_hiwat
)
554 (void)chgsbsize(so
->so_cred
->cr_uidinfo
,
555 &so
->so_snd
.sb_hiwat
, 0, RLIM_INFINITY
);
557 if (so
->so_accf
!= NULL
) {
558 if (so
->so_accf
->so_accept_filter
!= NULL
&&
559 so
->so_accf
->so_accept_filter
->accf_destroy
!= NULL
) {
560 so
->so_accf
->so_accept_filter
->accf_destroy(so
);
562 if (so
->so_accf
->so_accept_filter_str
!= NULL
)
563 FREE(so
->so_accf
->so_accept_filter_str
, M_ACCF
);
564 FREE(so
->so_accf
, M_ACCF
);
567 kauth_cred_rele(so
->so_cred
);
568 zfreei(so
->so_zone
, so
);
570 if (so
->cached_in_sock_layer
== 1)
571 cached_sock_free(so
);
573 if (so
->cached_in_sock_layer
== -1)
574 panic("sodealloc: double dealloc: so=%x\n", so
);
575 so
->cached_in_sock_layer
= -1;
576 FREE_ZONE(so
, sizeof(*so
), so
->so_zone
);
578 #endif /* __APPLE__ */
582 solisten(so
, backlog
)
583 register struct socket
*so
;
587 struct proc
*p
= current_proc();
593 struct socket_filter_entry
*filter
;
596 for (filter
= so
->so_filt
; filter
&& (error
== 0);
597 filter
= filter
->sfe_next_onsocket
) {
598 if (filter
->sfe_filter
->sf_filter
.sf_listen
) {
602 socket_unlock(so
, 0);
604 error
= filter
->sfe_filter
->sf_filter
.sf_listen(
605 filter
->sfe_cookie
, so
);
615 error
= (*so
->so_proto
->pr_usrreqs
->pru_listen
)(so
, p
);
619 socket_unlock(so
, 1);
620 if (error
== EJUSTRETURN
)
625 if (TAILQ_EMPTY(&so
->so_comp
))
626 so
->so_options
|= SO_ACCEPTCONN
;
627 if (backlog
< 0 || backlog
> somaxconn
)
629 so
->so_qlimit
= backlog
;
631 socket_unlock(so
, 1);
636 sofreelastref(so
, dealloc
)
637 register struct socket
*so
;
641 struct socket
*head
= so
->so_head
;
643 /*### Assume socket is locked */
645 /* Remove any filters - may be called more than once */
648 if ((!(so
->so_flags
& SOF_PCBCLEARING
)) || ((so
->so_state
& SS_NOFDREF
) == 0)) {
650 selthreadclear(&so
->so_snd
.sb_sel
);
651 selthreadclear(&so
->so_rcv
.sb_sel
);
652 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
653 so
->so_snd
.sb_flags
&= ~SB_UPCALL
;
658 socket_lock(head
, 1);
659 if (so
->so_state
& SS_INCOMP
) {
660 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
662 } else if (so
->so_state
& SS_COMP
) {
664 * We must not decommission a socket that's
665 * on the accept(2) queue. If we do, then
666 * accept(2) may hang after select(2) indicated
667 * that the listening socket was ready.
670 selthreadclear(&so
->so_snd
.sb_sel
);
671 selthreadclear(&so
->so_rcv
.sb_sel
);
672 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
673 so
->so_snd
.sb_flags
&= ~SB_UPCALL
;
675 socket_unlock(head
, 1);
678 panic("sofree: not queued");
681 so
->so_state
&= ~SS_INCOMP
;
683 socket_unlock(head
, 1);
686 selthreadclear(&so
->so_snd
.sb_sel
);
687 sbrelease(&so
->so_snd
);
691 /* 3932268: disable upcall */
692 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
693 so
->so_snd
.sb_flags
&= ~SB_UPCALL
;
700 * Close a socket on last file table reference removal.
701 * Initiate disconnect if connected.
702 * Free socket when disconnect complete.
706 register struct socket
*so
;
709 lck_mtx_t
* mutex_held
;
712 if (so
->so_usecount
== 0) {
713 panic("soclose: so=%x refcount=0\n", so
);
716 sflt_notify(so
, sock_evt_closing
, NULL
);
718 if ((so
->so_options
& SO_ACCEPTCONN
)) {
721 /* We do not want new connection to be added to the connection queues */
722 so
->so_options
&= ~SO_ACCEPTCONN
;
724 while ((sp
= TAILQ_FIRST(&so
->so_incomp
)) != NULL
) {
725 /* A bit tricky here. We need to keep
726 * a lock if it's a protocol global lock
727 * but we want the head, not the socket locked
728 * in the case of per-socket lock...
730 if (so
->so_proto
->pr_getlock
!= NULL
)
732 if (so
->so_proto
->pr_getlock
!= NULL
)
733 socket_unlock(so
, 0);
735 if (so
->so_proto
->pr_getlock
!= NULL
)
737 if (so
->so_proto
->pr_getlock
!= NULL
)
738 socket_unlock(sp
, 1);
741 while ((sp
= TAILQ_FIRST(&so
->so_comp
)) != NULL
) {
742 if (so
->so_proto
->pr_getlock
!= NULL
)
745 /* Dequeue from so_comp since sofree() won't do it */
746 TAILQ_REMOVE(&so
->so_comp
, sp
, so_list
);
748 sp
->so_state
&= ~SS_COMP
;
751 if (so
->so_proto
->pr_getlock
!= NULL
)
752 socket_unlock(so
, 0);
754 if (so
->so_proto
->pr_getlock
!= NULL
)
756 if (so
->so_proto
->pr_getlock
!= NULL
)
757 socket_unlock(sp
, 1);
760 if (so
->so_pcb
== 0) {
761 /* 3915887: mark the socket as ready for dealloc */
762 so
->so_flags
|= SOF_PCBCLEARING
;
765 if (so
->so_state
& SS_ISCONNECTED
) {
766 if ((so
->so_state
& SS_ISDISCONNECTING
) == 0) {
767 error
= sodisconnectlocked(so
);
771 if (so
->so_options
& SO_LINGER
) {
772 if ((so
->so_state
& SS_ISDISCONNECTING
) &&
773 (so
->so_state
& SS_NBIO
))
775 if (so
->so_proto
->pr_getlock
!= NULL
)
776 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
778 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
779 while (so
->so_state
& SS_ISCONNECTED
) {
780 ts
.tv_sec
= (so
->so_linger
/100);
781 ts
.tv_nsec
= (so
->so_linger
% 100) * NSEC_PER_USEC
* 1000 * 10;
782 error
= msleep((caddr_t
)&so
->so_timeo
, mutex_held
,
783 PSOCK
| PCATCH
, "soclos", &ts
);
785 /* It's OK when the time fires, don't report an error */
786 if (error
== EWOULDBLOCK
)
794 if (so
->so_usecount
== 0)
795 panic("soclose: usecount is zero so=%x\n", so
);
796 if (so
->so_pcb
&& !(so
->so_flags
& SOF_PCBCLEARING
)) {
797 int error2
= (*so
->so_proto
->pr_usrreqs
->pru_detach
)(so
);
801 if (so
->so_usecount
<= 0)
802 panic("soclose: usecount is zero so=%x\n", so
);
804 if (so
->so_pcb
&& so
->so_state
& SS_NOFDREF
)
805 panic("soclose: NOFDREF");
806 so
->so_state
|= SS_NOFDREF
;
808 so
->so_proto
->pr_domain
->dom_refs
--;
818 register struct socket
*so
;
822 if (so
->so_retaincnt
== 0)
823 error
= soclose_locked(so
);
824 else { /* if the FD is going away, but socket is retained in kernel remove its reference */
826 if (so
->so_usecount
< 2)
827 panic("soclose: retaincnt non null and so=%x usecount=%x\n", so
->so_usecount
);
829 socket_unlock(so
, 1);
835 * Must be called at splnet...
837 //#### Should already be locked
844 #ifdef MORE_LOCKING_DEBUG
845 lck_mtx_t
* mutex_held
;
847 if (so
->so_proto
->pr_getlock
!= NULL
)
848 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
850 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
851 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
854 error
= (*so
->so_proto
->pr_usrreqs
->pru_abort
)(so
);
863 soacceptlock(so
, nam
, dolock
)
864 register struct socket
*so
;
865 struct sockaddr
**nam
;
870 if (dolock
) socket_lock(so
, 1);
872 if ((so
->so_state
& SS_NOFDREF
) == 0)
873 panic("soaccept: !NOFDREF");
874 so
->so_state
&= ~SS_NOFDREF
;
875 error
= (*so
->so_proto
->pr_usrreqs
->pru_accept
)(so
, nam
);
877 if (dolock
) socket_unlock(so
, 1);
882 register struct socket
*so
;
883 struct sockaddr
**nam
;
885 return (soacceptlock(so
, nam
, 1));
889 soconnectlock(so
, nam
, dolock
)
890 register struct socket
*so
;
891 struct sockaddr
*nam
;
897 struct proc
*p
= current_proc();
899 if (dolock
) socket_lock(so
, 1);
901 if (so
->so_options
& SO_ACCEPTCONN
) {
902 if (dolock
) socket_unlock(so
, 1);
906 * If protocol is connection-based, can only connect once.
907 * Otherwise, if connected, try to disconnect first.
908 * This allows user to disconnect by connecting to, e.g.,
911 if (so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
) &&
912 ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) ||
913 (error
= sodisconnectlocked(so
))))
917 * Run connect filter before calling protocol:
918 * - non-blocking connect returns before completion;
921 struct socket_filter_entry
*filter
;
924 for (filter
= so
->so_filt
; filter
&& (error
== 0);
925 filter
= filter
->sfe_next_onsocket
) {
926 if (filter
->sfe_filter
->sf_filter
.sf_connect_out
) {
930 socket_unlock(so
, 0);
932 error
= filter
->sfe_filter
->sf_filter
.sf_connect_out(
933 filter
->sfe_cookie
, so
, nam
);
942 if (error
== EJUSTRETURN
)
944 if (dolock
) socket_unlock(so
, 1);
948 error
= (*so
->so_proto
->pr_usrreqs
->pru_connect
)(so
, nam
, p
);
950 if (dolock
) socket_unlock(so
, 1);
956 register struct socket
*so
;
957 struct sockaddr
*nam
;
959 return (soconnectlock(so
, nam
, 1));
964 register struct socket
*so1
;
968 //####### Assumes so1 is already locked /
972 error
= (*so1
->so_proto
->pr_usrreqs
->pru_connect2
)(so1
, so2
);
974 socket_unlock(so2
, 1);
980 sodisconnectlocked(so
)
981 register struct socket
*so
;
985 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
989 if (so
->so_state
& SS_ISDISCONNECTING
) {
994 error
= (*so
->so_proto
->pr_usrreqs
->pru_disconnect
)(so
);
997 sflt_notify(so
, sock_evt_disconnected
, NULL
);
1003 //### Locking version
1006 register struct socket
*so
;
1011 error
= sodisconnectlocked(so
);
1012 socket_unlock(so
, 1);
1016 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_DONTWAIT : M_WAIT)
1019 * sosendcheck will lock the socket buffer if it isn't locked and
1020 * verify that there is space for the data being inserted.
1026 struct sockaddr
*addr
,
1038 if (*sblocked
== 0) {
1039 if ((so
->so_snd
.sb_flags
& SB_LOCK
) != 0 &&
1040 so
->so_send_filt_thread
!= 0 &&
1041 so
->so_send_filt_thread
== current_thread()) {
1043 * We're being called recursively from a filter,
1044 * allow this to continue. Radar 4150520.
1045 * Don't set sblocked because we don't want
1046 * to perform an unlock later.
1051 error
= sblock(&so
->so_snd
, SBLOCKWAIT(flags
));
1059 if (so
->so_state
& SS_CANTSENDMORE
)
1063 error
= so
->so_error
;
1068 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
1070 * `sendto' and `sendmsg' is allowed on a connection-
1071 * based socket if it supports implied connect.
1072 * Return ENOTCONN if not connected and no address is
1075 if ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) &&
1076 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) == 0) {
1077 if ((so
->so_state
& SS_ISCONFIRMING
) == 0 &&
1078 !(resid
== 0 && clen
!= 0))
1080 } else if (addr
== 0 && !(flags
&MSG_HOLD
))
1081 return (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) ? ENOTCONN
: EDESTADDRREQ
;
1083 space
= sbspace(&so
->so_snd
);
1084 if (flags
& MSG_OOB
)
1086 if ((atomic
&& resid
> so
->so_snd
.sb_hiwat
) ||
1087 clen
> so
->so_snd
.sb_hiwat
)
1089 if (space
< resid
+ clen
&&
1090 (atomic
|| space
< so
->so_snd
.sb_lowat
|| space
< clen
)) {
1091 if ((so
->so_state
& SS_NBIO
) || (flags
& MSG_NBIO
) || assumelock
) {
1094 sbunlock(&so
->so_snd
, 1);
1095 error
= sbwait(&so
->so_snd
);
1107 * If send must go all at once and message is larger than
1108 * send buffering, then hard error.
1109 * Lock against other senders.
1110 * If must go all at once and not enough room now, then
1111 * inform user that this would block and do nothing.
1112 * Otherwise, if nonblocking, send as much as possible.
1113 * The data to be sent is described by "uio" if nonzero,
1114 * otherwise by the mbuf chain "top" (which must be null
1115 * if uio is not). Data provided in mbuf chain must be small
1116 * enough to send all at once.
1118 * Returns nonzero on error, timeout or signal; callers
1119 * must check for short counts if EINTR/ERESTART are returned.
1120 * Data and control buffers are freed on return.
1122 * MSG_HOLD: go thru most of sosend(), but just enqueue the mbuf
1123 * MSG_SEND: go thru as for MSG_HOLD on current fragment, then
1124 * point at the mbuf chain being constructed and go from there.
1127 sosend(so
, addr
, uio
, top
, control
, flags
)
1128 register struct socket
*so
;
1129 struct sockaddr
*addr
;
1132 struct mbuf
*control
;
1137 register struct mbuf
*m
, *freelist
= NULL
;
1138 register long space
, len
, resid
;
1139 int clen
= 0, error
, dontroute
, mlen
, sendflags
;
1140 int atomic
= sosendallatonce(so
) || top
;
1142 struct proc
*p
= current_proc();
1145 // LP64todo - fix this!
1146 resid
= uio_resid(uio
);
1148 resid
= top
->m_pkthdr
.len
;
1150 KERNEL_DEBUG((DBG_FNC_SOSEND
| DBG_FUNC_START
),
1154 so
->so_snd
.sb_lowat
,
1155 so
->so_snd
.sb_hiwat
);
1160 * In theory resid should be unsigned.
1161 * However, space must be signed, as it might be less than 0
1162 * if we over-committed, and we must use a signed comparison
1163 * of space and resid. On the other hand, a negative resid
1164 * causes us to loop sending 0-length segments to the protocol.
1166 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
1167 * type sockets since that's an error.
1169 if (resid
< 0 || (so
->so_type
== SOCK_STREAM
&& (flags
& MSG_EOR
))) {
1171 socket_unlock(so
, 1);
1176 (flags
& MSG_DONTROUTE
) && (so
->so_options
& SO_DONTROUTE
) == 0 &&
1177 (so
->so_proto
->pr_flags
& PR_ATOMIC
);
1179 p
->p_stats
->p_ru
.ru_msgsnd
++;
1181 clen
= control
->m_len
;
1184 error
= sosendcheck(so
, addr
, resid
, clen
, atomic
, flags
, &sblocked
);
1189 space
= sbspace(&so
->so_snd
) - clen
+ ((flags
& MSG_OOB
) ? 1024 : 0);
1195 * Data is prepackaged in "top".
1198 if (flags
& MSG_EOR
)
1199 top
->m_flags
|= M_EOR
;
1204 bytes_to_copy
= min(resid
, space
);
1206 if (sosendminchain
> 0) {
1209 chainlength
= sosendmaxchain
;
1211 socket_unlock(so
, 0);
1215 int hdrs_needed
= (top
== 0) ? 1 : 0;
1218 * try to maintain a local cache of mbuf clusters needed to complete this write
1219 * the list is further limited to the number that are currently needed to fill the socket
1220 * this mechanism allows a large number of mbufs/clusters to be grabbed under a single
1221 * mbuf lock... if we can't get any clusters, than fall back to trying for mbufs
1222 * if we fail early (or miscalcluate the number needed) make sure to release any clusters
1223 * we haven't yet consumed.
1225 if (freelist
== NULL
&& bytes_to_copy
> MCLBYTES
) {
1226 num_needed
= bytes_to_copy
/ NBPG
;
1228 if ((bytes_to_copy
- (num_needed
* NBPG
)) >= MINCLSIZE
)
1231 freelist
= m_getpackets_internal(&num_needed
, hdrs_needed
, M_WAIT
, 0, NBPG
);
1232 /* Fall back to cluster size if allocation failed */
1235 if (freelist
== NULL
&& bytes_to_copy
> MINCLSIZE
) {
1236 num_needed
= bytes_to_copy
/ MCLBYTES
;
1238 if ((bytes_to_copy
- (num_needed
* MCLBYTES
)) >= MINCLSIZE
)
1241 freelist
= m_getpackets_internal(&num_needed
, hdrs_needed
, M_WAIT
, 0, MCLBYTES
);
1242 /* Fall back to a single mbuf if allocation failed */
1245 if (freelist
== NULL
) {
1247 MGETHDR(freelist
, M_WAIT
, MT_DATA
);
1249 MGET(freelist
, M_WAIT
, MT_DATA
);
1251 if (freelist
== NULL
) {
1257 * For datagram protocols, leave room
1258 * for protocol headers in first mbuf.
1260 if (atomic
&& top
== 0 && bytes_to_copy
< MHLEN
)
1261 MH_ALIGN(freelist
, bytes_to_copy
);
1264 freelist
= m
->m_next
;
1267 if ((m
->m_flags
& M_EXT
))
1268 mlen
= m
->m_ext
.ext_size
;
1269 else if ((m
->m_flags
& M_PKTHDR
))
1270 mlen
= MHLEN
- m_leadingspace(m
);
1273 len
= min(mlen
, bytes_to_copy
);
1279 error
= uiomove(mtod(m
, caddr_t
), (int)len
, uio
);
1281 // LP64todo - fix this!
1282 resid
= uio_resid(uio
);
1286 top
->m_pkthdr
.len
+= len
;
1291 if (flags
& MSG_EOR
)
1292 top
->m_flags
|= M_EOR
;
1295 bytes_to_copy
= min(resid
, space
);
1297 } while (space
> 0 && (chainlength
< sosendmaxchain
|| atomic
|| resid
< MINCLSIZE
));
1305 if (flags
& (MSG_HOLD
|MSG_SEND
))
1307 /* Enqueue for later, go away if HOLD */
1308 register struct mbuf
*mb1
;
1309 if (so
->so_temp
&& (flags
& MSG_FLUSH
))
1311 m_freem(so
->so_temp
);
1315 so
->so_tail
->m_next
= top
;
1322 if (flags
& MSG_HOLD
)
1330 so
->so_options
|= SO_DONTROUTE
;
1331 /* Compute flags here, for pru_send and NKEs */
1332 sendflags
= (flags
& MSG_OOB
) ? PRUS_OOB
:
1334 * If the user set MSG_EOF, the protocol
1335 * understands this flag and nothing left to
1336 * send then use PRU_SEND_EOF instead of PRU_SEND.
1338 ((flags
& MSG_EOF
) &&
1339 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) &&
1342 /* If there is more to send set PRUS_MORETOCOME */
1343 (resid
> 0 && space
> 0) ? PRUS_MORETOCOME
: 0;
1346 * Socket filter processing
1349 struct socket_filter_entry
*filter
;
1354 for (filter
= so
->so_filt
; filter
&& (error
== 0);
1355 filter
= filter
->sfe_next_onsocket
) {
1356 if (filter
->sfe_filter
->sf_filter
.sf_data_out
) {
1358 if (filtered
== 0) {
1360 so
->so_send_filt_thread
= current_thread();
1361 socket_unlock(so
, 0);
1362 so_flags
= (sendflags
& MSG_OOB
) ? sock_data_filt_flag_oob
: 0;
1364 error
= filter
->sfe_filter
->sf_filter
.sf_data_out(
1365 filter
->sfe_cookie
, so
, addr
, &top
, &control
, so_flags
);
1371 * At this point, we've run at least one filter.
1372 * The socket is unlocked as is the socket buffer.
1375 so
->so_send_filt_thread
= 0;
1377 if (error
== EJUSTRETURN
) {
1389 * End Socket filter processing
1392 if (error
== EJUSTRETURN
) {
1393 /* A socket filter handled this data */
1397 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)(so
,
1398 sendflags
, top
, addr
, control
, p
);
1401 if (flags
& MSG_SEND
)
1405 so
->so_options
&= ~SO_DONTROUTE
;
1412 } while (resid
&& space
> 0);
1417 sbunlock(&so
->so_snd
, 0); /* will unlock socket */
1419 socket_unlock(so
, 1);
1426 m_freem_list(freelist
);
1428 KERNEL_DEBUG(DBG_FNC_SOSEND
| DBG_FUNC_END
,
1439 * Implement receive operations on a socket.
1440 * We depend on the way that records are added to the sockbuf
1441 * by sbappend*. In particular, each record (mbufs linked through m_next)
1442 * must begin with an address if the protocol so specifies,
1443 * followed by an optional mbuf or mbufs containing ancillary data,
1444 * and then zero or more mbufs of data.
1445 * In order to avoid blocking network interrupts for the entire time here,
1446 * we splx() while doing the actual copy to user space.
1447 * Although the sockbuf is locked, new data may still be appended,
1448 * and thus we must maintain consistency of the sockbuf during that time.
1450 * The caller may receive the data as a single mbuf chain by supplying
1451 * an mbuf **mp0 for use in returning the chain. The uio is then used
1452 * only for the count in uio_resid.
1455 soreceive(so
, psa
, uio
, mp0
, controlp
, flagsp
)
1456 register struct socket
*so
;
1457 struct sockaddr
**psa
;
1460 struct mbuf
**controlp
;
1463 register struct mbuf
*m
, **mp
, *ml
= NULL
;
1464 register int flags
, len
, error
, offset
;
1465 struct protosw
*pr
= so
->so_proto
;
1466 struct mbuf
*nextrecord
;
1468 // LP64todo - fix this!
1469 int orig_resid
= uio_resid(uio
);
1470 volatile struct mbuf
*free_list
;
1471 volatile int delayed_copy_len
;
1474 struct proc
*p
= current_proc();
1477 // LP64todo - fix this!
1478 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_START
,
1482 so
->so_rcv
.sb_lowat
,
1483 so
->so_rcv
.sb_hiwat
);
1487 #ifdef MORE_LOCKING_DEBUG
1488 if (so
->so_usecount
== 1)
1489 panic("soreceive: so=%x no other reference on socket\n", so
);
1497 flags
= *flagsp
&~ MSG_EOR
;
1501 * When SO_WANTOOBFLAG is set we try to get out-of-band data
1502 * regardless of the flags argument. Here is the case were
1503 * out-of-band data is not inline.
1505 if ((flags
& MSG_OOB
) ||
1506 ((so
->so_options
& SO_WANTOOBFLAG
) != 0 &&
1507 (so
->so_options
& SO_OOBINLINE
) == 0 &&
1508 (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
)))) {
1509 m
= m_get(M_WAIT
, MT_DATA
);
1511 socket_unlock(so
, 1);
1512 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, ENOBUFS
,0,0,0,0);
1515 error
= (*pr
->pr_usrreqs
->pru_rcvoob
)(so
, m
, flags
& MSG_PEEK
);
1518 socket_unlock(so
, 0);
1520 // LP64todo - fix this!
1521 error
= uiomove(mtod(m
, caddr_t
),
1522 (int) min(uio_resid(uio
), m
->m_len
), uio
);
1524 } while (uio_resid(uio
) && error
== 0 && m
);
1530 if ((so
->so_options
& SO_WANTOOBFLAG
) != 0) {
1531 if (error
== EWOULDBLOCK
|| error
== EINVAL
) {
1533 * Let's try to get normal data:
1534 * EWOULDBLOCK: out-of-band data not receive yet;
1535 * EINVAL: out-of-band data already read.
1539 } else if (error
== 0 && flagsp
)
1542 socket_unlock(so
, 1);
1543 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1549 *mp
= (struct mbuf
*)0;
1550 if (so
->so_state
& SS_ISCONFIRMING
&& uio_resid(uio
))
1551 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, 0);
1554 free_list
= (struct mbuf
*)0;
1555 delayed_copy_len
= 0;
1557 #ifdef MORE_LOCKING_DEBUG
1558 if (so
->so_usecount
<= 1)
1559 printf("soreceive: sblock so=%x ref=%d on socket\n", so
, so
->so_usecount
);
1561 error
= sblock(&so
->so_rcv
, SBLOCKWAIT(flags
));
1563 socket_unlock(so
, 1);
1564 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1568 m
= so
->so_rcv
.sb_mb
;
1570 * If we have less data than requested, block awaiting more
1571 * (subject to any timeout) if:
1572 * 1. the current count is less than the low water mark, or
1573 * 2. MSG_WAITALL is set, and it is possible to do the entire
1574 * receive operation at once if we block (resid <= hiwat).
1575 * 3. MSG_DONTWAIT is not set
1576 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1577 * we have to do the receive in sections, and thus risk returning
1578 * a short count if a timeout or signal occurs after we start.
1580 if (m
== 0 || (((flags
& MSG_DONTWAIT
) == 0 &&
1581 so
->so_rcv
.sb_cc
< uio_resid(uio
)) &&
1582 (so
->so_rcv
.sb_cc
< so
->so_rcv
.sb_lowat
||
1583 ((flags
& MSG_WAITALL
) && uio_resid(uio
) <= so
->so_rcv
.sb_hiwat
)) &&
1584 m
->m_nextpkt
== 0 && (pr
->pr_flags
& PR_ATOMIC
) == 0)) {
1586 KASSERT(m
!= 0 || !so
->so_rcv
.sb_cc
, ("receive 1"));
1590 error
= so
->so_error
;
1591 if ((flags
& MSG_PEEK
) == 0)
1595 if (so
->so_state
& SS_CANTRCVMORE
) {
1601 for (; m
; m
= m
->m_next
)
1602 if (m
->m_type
== MT_OOBDATA
|| (m
->m_flags
& M_EOR
)) {
1603 m
= so
->so_rcv
.sb_mb
;
1606 if ((so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
)) == 0 &&
1607 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)) {
1611 if (uio_resid(uio
) == 0)
1613 if ((so
->so_state
& SS_NBIO
) || (flags
& (MSG_DONTWAIT
|MSG_NBIO
))) {
1614 error
= EWOULDBLOCK
;
1617 sbunlock(&so
->so_rcv
, 1);
1618 #ifdef EVEN_MORE_LOCKING_DEBUG
1620 printf("Waiting for socket data\n");
1623 error
= sbwait(&so
->so_rcv
);
1624 #ifdef EVEN_MORE_LOCKING_DEBUG
1626 printf("SORECEIVE - sbwait returned %d\n", error
);
1628 if (so
->so_usecount
< 1)
1629 panic("soreceive: after 2nd sblock so=%x ref=%d on socket\n", so
, so
->so_usecount
);
1631 socket_unlock(so
, 1);
1632 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1640 uio
->uio_procp
->p_stats
->p_ru
.ru_msgrcv
++;
1641 #else /* __APPLE__ */
1644 * This should be uio->uio-procp; however, some callers of this
1645 * function use auto variables with stack garbage, and fail to
1646 * fill out the uio structure properly.
1649 p
->p_stats
->p_ru
.ru_msgrcv
++;
1650 #endif /* __APPLE__ */
1651 nextrecord
= m
->m_nextpkt
;
1652 if ((pr
->pr_flags
& PR_ADDR
) && m
->m_type
== MT_SONAME
) {
1653 KASSERT(m
->m_type
== MT_SONAME
, ("receive 1a"));
1656 *psa
= dup_sockaddr(mtod(m
, struct sockaddr
*),
1658 if ((*psa
== 0) && (flags
& MSG_NEEDSA
)) {
1659 error
= EWOULDBLOCK
;
1663 if (flags
& MSG_PEEK
) {
1666 sbfree(&so
->so_rcv
, m
);
1667 if (m
->m_next
== 0 && so
->so_rcv
.sb_cc
!= 0)
1668 panic("soreceive: about to create invalid socketbuf");
1669 MFREE(m
, so
->so_rcv
.sb_mb
);
1670 m
= so
->so_rcv
.sb_mb
;
1673 while (m
&& m
->m_type
== MT_CONTROL
&& error
== 0) {
1674 if (flags
& MSG_PEEK
) {
1676 *controlp
= m_copy(m
, 0, m
->m_len
);
1679 sbfree(&so
->so_rcv
, m
);
1681 if (pr
->pr_domain
->dom_externalize
&&
1682 mtod(m
, struct cmsghdr
*)->cmsg_type
==
1684 socket_unlock(so
, 0); /* release socket lock: see 3903171 */
1685 error
= (*pr
->pr_domain
->dom_externalize
)(m
);
1689 if (m
->m_next
== 0 && so
->so_rcv
.sb_cc
!= 0)
1690 panic("soreceive: so->so_rcv.sb_mb->m_next == 0 && so->so_rcv.sb_cc != 0");
1691 so
->so_rcv
.sb_mb
= m
->m_next
;
1693 m
= so
->so_rcv
.sb_mb
;
1695 MFREE(m
, so
->so_rcv
.sb_mb
);
1696 m
= so
->so_rcv
.sb_mb
;
1701 controlp
= &(*controlp
)->m_next
;
1705 if ((flags
& MSG_PEEK
) == 0)
1706 m
->m_nextpkt
= nextrecord
;
1708 if (type
== MT_OOBDATA
)
1714 if (!(flags
& MSG_PEEK
) && uio_resid(uio
) > sorecvmincopy
)
1721 while (m
&& (uio_resid(uio
) - delayed_copy_len
) > 0 && error
== 0) {
1722 if (m
->m_type
== MT_OOBDATA
) {
1723 if (type
!= MT_OOBDATA
)
1725 } else if (type
== MT_OOBDATA
)
1729 * This assertion needs rework. The trouble is Appletalk is uses many
1730 * mbuf types (NOT listed in mbuf.h!) which will trigger this panic.
1731 * For now just remove the assertion... CSM 9/98
1734 KASSERT(m
->m_type
== MT_DATA
|| m
->m_type
== MT_HEADER
,
1738 * Make sure to allways set MSG_OOB event when getting
1739 * out of band data inline.
1741 if ((so
->so_options
& SO_WANTOOBFLAG
) != 0 &&
1742 (so
->so_options
& SO_OOBINLINE
) != 0 &&
1743 (so
->so_state
& SS_RCVATMARK
) != 0) {
1747 so
->so_state
&= ~SS_RCVATMARK
;
1748 // LP64todo - fix this!
1749 len
= uio_resid(uio
) - delayed_copy_len
;
1750 if (so
->so_oobmark
&& len
> so
->so_oobmark
- offset
)
1751 len
= so
->so_oobmark
- offset
;
1752 if (len
> m
->m_len
- moff
)
1753 len
= m
->m_len
- moff
;
1755 * If mp is set, just pass back the mbufs.
1756 * Otherwise copy them out via the uio, then free.
1757 * Sockbuf must be consistent here (points to current mbuf,
1758 * it points to next record) when we drop priority;
1759 * we must note any additions to the sockbuf when we
1760 * block interrupts again.
1763 if (can_delay
&& len
== m
->m_len
) {
1765 * only delay the copy if we're consuming the
1766 * mbuf and we're NOT in MSG_PEEK mode
1767 * and we have enough data to make it worthwile
1768 * to drop and retake the funnel... can_delay
1769 * reflects the state of the 2 latter constraints
1770 * moff should always be zero in these cases
1772 delayed_copy_len
+= len
;
1775 if (delayed_copy_len
) {
1776 error
= sodelayed_copy(so
, uio
, &free_list
, &delayed_copy_len
);
1781 if (m
!= so
->so_rcv
.sb_mb
) {
1783 * can only get here if MSG_PEEK is not set
1784 * therefore, m should point at the head of the rcv queue...
1785 * if it doesn't, it means something drastically changed
1786 * while we were out from behind the funnel in sodelayed_copy...
1787 * perhaps a RST on the stream... in any event, the stream has
1788 * been interrupted... it's probably best just to return
1789 * whatever data we've moved and let the caller sort it out...
1794 socket_unlock(so
, 0);
1795 error
= uiomove(mtod(m
, caddr_t
) + moff
, (int)len
, uio
);
1802 uio_setresid(uio
, (uio_resid(uio
) - len
));
1804 if (len
== m
->m_len
- moff
) {
1805 if (m
->m_flags
& M_EOR
)
1807 if (flags
& MSG_PEEK
) {
1811 nextrecord
= m
->m_nextpkt
;
1812 sbfree(&so
->so_rcv
, m
);
1813 m
->m_nextpkt
= NULL
;
1818 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
1819 *mp
= (struct mbuf
*)0;
1821 if (free_list
== NULL
)
1826 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
1830 m
->m_nextpkt
= nextrecord
;
1833 if (flags
& MSG_PEEK
)
1837 *mp
= m_copym(m
, 0, len
, M_WAIT
);
1840 so
->so_rcv
.sb_cc
-= len
;
1843 if (so
->so_oobmark
) {
1844 if ((flags
& MSG_PEEK
) == 0) {
1845 so
->so_oobmark
-= len
;
1846 if (so
->so_oobmark
== 0) {
1847 so
->so_state
|= SS_RCVATMARK
;
1849 * delay posting the actual event until after
1850 * any delayed copy processing has finished
1857 if (offset
== so
->so_oobmark
)
1861 if (flags
& MSG_EOR
)
1864 * If the MSG_WAITALL or MSG_WAITSTREAM flag is set (for non-atomic socket),
1865 * we must not quit until "uio->uio_resid == 0" or an error
1866 * termination. If a signal/timeout occurs, return
1867 * with a short count but without error.
1868 * Keep sockbuf locked against other readers.
1870 while (flags
& (MSG_WAITALL
|MSG_WAITSTREAM
) && m
== 0 && (uio_resid(uio
) - delayed_copy_len
) > 0 &&
1871 !sosendallatonce(so
) && !nextrecord
) {
1872 if (so
->so_error
|| so
->so_state
& SS_CANTRCVMORE
)
1875 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
&& (((struct inpcb
*)so
->so_pcb
)->inp_state
!= INPCB_STATE_DEAD
))
1876 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, flags
);
1877 if (sbwait(&so
->so_rcv
)) {
1882 * have to wait until after we get back from the sbwait to do the copy because
1883 * we will drop the funnel if we have enough data that has been delayed... by dropping
1884 * the funnel we open up a window allowing the netisr thread to process the incoming packets
1885 * and to change the state of this socket... we're issuing the sbwait because
1886 * the socket is empty and we're expecting the netisr thread to wake us up when more
1887 * packets arrive... if we allow that processing to happen and then sbwait, we
1888 * could stall forever with packets sitting in the socket if no further packets
1889 * arrive from the remote side.
1891 * we want to copy before we've collected all the data to satisfy this request to
1892 * allow the copy to overlap the incoming packet processing on an MP system
1894 if (delayed_copy_len
> sorecvmincopy
&& (delayed_copy_len
> (so
->so_rcv
.sb_hiwat
/ 2))) {
1896 error
= sodelayed_copy(so
, uio
, &free_list
, &delayed_copy_len
);
1901 m
= so
->so_rcv
.sb_mb
;
1903 nextrecord
= m
->m_nextpkt
;
1907 #ifdef MORE_LOCKING_DEBUG
1908 if (so
->so_usecount
<= 1)
1909 panic("soreceive: after big while so=%x ref=%d on socket\n", so
, so
->so_usecount
);
1912 if (m
&& pr
->pr_flags
& PR_ATOMIC
) {
1914 if (so
->so_options
& SO_DONTTRUNC
)
1915 flags
|= MSG_RCVMORE
;
1919 if ((flags
& MSG_PEEK
) == 0)
1920 (void) sbdroprecord(&so
->so_rcv
);
1925 if ((flags
& MSG_PEEK
) == 0) {
1927 so
->so_rcv
.sb_mb
= nextrecord
;
1928 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
)
1929 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, flags
);
1932 if ((so
->so_options
& SO_WANTMORE
) && so
->so_rcv
.sb_cc
> 0)
1933 flags
|= MSG_HAVEMORE
;
1935 if (delayed_copy_len
) {
1936 error
= sodelayed_copy(so
, uio
, &free_list
, &delayed_copy_len
);
1942 m_freem_list((struct mbuf
*)free_list
);
1943 free_list
= (struct mbuf
*)0;
1946 postevent(so
, 0, EV_OOB
);
1948 if (orig_resid
== uio_resid(uio
) && orig_resid
&&
1949 (flags
& MSG_EOR
) == 0 && (so
->so_state
& SS_CANTRCVMORE
) == 0) {
1950 sbunlock(&so
->so_rcv
, 1);
1957 #ifdef MORE_LOCKING_DEBUG
1958 if (so
->so_usecount
<= 1)
1959 panic("soreceive: release so=%x ref=%d on socket\n", so
, so
->so_usecount
);
1961 if (delayed_copy_len
) {
1962 error
= sodelayed_copy(so
, uio
, &free_list
, &delayed_copy_len
);
1965 m_freem_list((struct mbuf
*)free_list
);
1967 sbunlock(&so
->so_rcv
, 0); /* will unlock socket */
1969 // LP64todo - fix this!
1970 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
,
1981 static int sodelayed_copy(struct socket
*so
, struct uio
*uio
, struct mbuf
**free_list
, int *resid
)
1988 socket_unlock(so
, 0);
1990 while (m
&& error
== 0) {
1992 error
= uiomove(mtod(m
, caddr_t
), (int)m
->m_len
, uio
);
1996 m_freem_list(*free_list
);
1998 *free_list
= (struct mbuf
*)NULL
;
2009 register struct socket
*so
;
2012 register struct protosw
*pr
= so
->so_proto
;
2017 sflt_notify(so
, sock_evt_shutdown
, &how
);
2019 if (how
!= SHUT_WR
) {
2021 postevent(so
, 0, EV_RCLOSED
);
2023 if (how
!= SHUT_RD
) {
2024 ret
= ((*pr
->pr_usrreqs
->pru_shutdown
)(so
));
2025 postevent(so
, 0, EV_WCLOSED
);
2026 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
2027 socket_unlock(so
, 1);
2031 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
2032 socket_unlock(so
, 1);
2038 register struct socket
*so
;
2040 register struct sockbuf
*sb
= &so
->so_rcv
;
2041 register struct protosw
*pr
= so
->so_proto
;
2044 #ifdef MORE_LOCKING_DEBUG
2045 lck_mtx_t
* mutex_held
;
2047 if (so
->so_proto
->pr_getlock
!= NULL
)
2048 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
2050 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
2051 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
2054 sflt_notify(so
, sock_evt_flush_read
, NULL
);
2056 sb
->sb_flags
|= SB_NOINTR
;
2057 (void) sblock(sb
, M_WAIT
);
2061 selthreadclear(&sb
->sb_sel
);
2064 bzero((caddr_t
)sb
, sizeof (*sb
));
2065 sb
->sb_so
= so
; /* reestablish link to socket */
2066 if (asb
.sb_flags
& SB_KNOTE
) {
2067 sb
->sb_sel
.si_note
= asb
.sb_sel
.si_note
;
2068 sb
->sb_flags
= SB_KNOTE
;
2070 if (pr
->pr_flags
& PR_RIGHTS
&& pr
->pr_domain
->dom_dispose
)
2071 (*pr
->pr_domain
->dom_dispose
)(asb
.sb_mb
);
2076 * Perhaps this routine, and sooptcopyout(), below, ought to come in
2077 * an additional variant to handle the case where the option value needs
2078 * to be some kind of integer, but not a specific size.
2079 * In addition to their use here, these functions are also called by the
2080 * protocol-level pr_ctloutput() routines.
2083 sooptcopyin(sopt
, buf
, len
, minlen
)
2084 struct sockopt
*sopt
;
2092 * If the user gives us more than we wanted, we ignore it,
2093 * but if we don't get the minimum length the caller
2094 * wants, we return EINVAL. On success, sopt->sopt_valsize
2095 * is set to however much we actually retrieved.
2097 if ((valsize
= sopt
->sopt_valsize
) < minlen
)
2100 sopt
->sopt_valsize
= valsize
= len
;
2102 if (sopt
->sopt_p
!= 0)
2103 return (copyin(sopt
->sopt_val
, buf
, valsize
));
2105 bcopy(CAST_DOWN(caddr_t
, sopt
->sopt_val
), buf
, valsize
);
2112 struct sockopt
*sopt
;
2121 if (sopt
->sopt_dir
!= SOPT_SET
) {
2122 sopt
->sopt_dir
= SOPT_SET
;
2126 struct socket_filter_entry
*filter
;
2129 for (filter
= so
->so_filt
; filter
&& (error
== 0);
2130 filter
= filter
->sfe_next_onsocket
) {
2131 if (filter
->sfe_filter
->sf_filter
.sf_setoption
) {
2132 if (filtered
== 0) {
2135 socket_unlock(so
, 0);
2137 error
= filter
->sfe_filter
->sf_filter
.sf_setoption(
2138 filter
->sfe_cookie
, so
, sopt
);
2142 if (filtered
!= 0) {
2147 if (error
== EJUSTRETURN
)
2155 if (sopt
->sopt_level
!= SOL_SOCKET
) {
2156 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
2157 error
= (*so
->so_proto
->pr_ctloutput
)
2159 socket_unlock(so
, 1);
2162 error
= ENOPROTOOPT
;
2164 switch (sopt
->sopt_name
) {
2167 error
= sooptcopyin(sopt
, &l
, sizeof l
, sizeof l
);
2171 so
->so_linger
= (sopt
->sopt_name
== SO_LINGER
) ? l
.l_linger
: l
.l_linger
* hz
;
2173 so
->so_options
|= SO_LINGER
;
2175 so
->so_options
&= ~SO_LINGER
;
2181 case SO_USELOOPBACK
:
2190 case SO_WANTOOBFLAG
:
2192 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2197 so
->so_options
|= sopt
->sopt_name
;
2199 so
->so_options
&= ~sopt
->sopt_name
;
2206 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2212 * Values < 1 make no sense for any of these
2213 * options, so disallow them.
2220 switch (sopt
->sopt_name
) {
2223 if (sbreserve(sopt
->sopt_name
== SO_SNDBUF
?
2224 &so
->so_snd
: &so
->so_rcv
,
2225 (u_long
) optval
) == 0) {
2232 * Make sure the low-water is never greater than
2236 so
->so_snd
.sb_lowat
=
2237 (optval
> so
->so_snd
.sb_hiwat
) ?
2238 so
->so_snd
.sb_hiwat
: optval
;
2241 so
->so_rcv
.sb_lowat
=
2242 (optval
> so
->so_rcv
.sb_hiwat
) ?
2243 so
->so_rcv
.sb_hiwat
: optval
;
2250 error
= sooptcopyin(sopt
, &tv
, sizeof tv
,
2255 if (tv
.tv_sec
< 0 || tv
.tv_sec
> LONG_MAX
||
2256 tv
.tv_usec
< 0 || tv
.tv_usec
>= 1000000) {
2261 switch (sopt
->sopt_name
) {
2263 so
->so_snd
.sb_timeo
= tv
;
2266 so
->so_rcv
.sb_timeo
= tv
;
2275 error
= sooptcopyin(sopt
, &nke
,
2276 sizeof nke
, sizeof nke
);
2280 error
= sflt_attach_private(so
, NULL
, nke
.nke_handle
, 1);
2285 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2290 so
->so_flags
|= SOF_NOSIGPIPE
;
2292 so
->so_flags
&= ~SOF_NOSIGPIPE
;
2297 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2302 so
->so_flags
|= SOF_NOADDRAVAIL
;
2304 so
->so_flags
&= ~SOF_NOADDRAVAIL
;
2309 error
= ENOPROTOOPT
;
2312 if (error
== 0 && so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
2313 (void) ((*so
->so_proto
->pr_ctloutput
)
2318 socket_unlock(so
, 1);
2322 /* Helper routine for getsockopt */
2324 sooptcopyout(sopt
, buf
, len
)
2325 struct sockopt
*sopt
;
2335 * Documented get behavior is that we always return a value,
2336 * possibly truncated to fit in the user's buffer.
2337 * Traditional behavior is that we always tell the user
2338 * precisely how much we copied, rather than something useful
2339 * like the total amount we had available for her.
2340 * Note that this interface is not idempotent; the entire answer must
2341 * generated ahead of time.
2343 valsize
= min(len
, sopt
->sopt_valsize
);
2344 sopt
->sopt_valsize
= valsize
;
2345 if (sopt
->sopt_val
!= USER_ADDR_NULL
) {
2346 if (sopt
->sopt_p
!= 0)
2347 error
= copyout(buf
, sopt
->sopt_val
, valsize
);
2349 bcopy(buf
, CAST_DOWN(caddr_t
, sopt
->sopt_val
), valsize
);
2357 struct sockopt
*sopt
;
2363 if (sopt
->sopt_dir
!= SOPT_GET
) {
2364 sopt
->sopt_dir
= SOPT_GET
;
2370 struct socket_filter_entry
*filter
;
2373 for (filter
= so
->so_filt
; filter
&& (error
== 0);
2374 filter
= filter
->sfe_next_onsocket
) {
2375 if (filter
->sfe_filter
->sf_filter
.sf_getoption
) {
2376 if (filtered
== 0) {
2379 socket_unlock(so
, 0);
2381 error
= filter
->sfe_filter
->sf_filter
.sf_getoption(
2382 filter
->sfe_cookie
, so
, sopt
);
2385 if (filtered
!= 0) {
2390 if (error
== EJUSTRETURN
)
2392 socket_unlock(so
, 1);
2399 if (sopt
->sopt_level
!= SOL_SOCKET
) {
2400 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
2401 error
= (*so
->so_proto
->pr_ctloutput
)
2403 socket_unlock(so
, 1);
2406 socket_unlock(so
, 1);
2407 return (ENOPROTOOPT
);
2410 switch (sopt
->sopt_name
) {
2413 l
.l_onoff
= so
->so_options
& SO_LINGER
;
2414 l
.l_linger
= (sopt
->sopt_name
== SO_LINGER
) ? so
->so_linger
:
2416 error
= sooptcopyout(sopt
, &l
, sizeof l
);
2419 case SO_USELOOPBACK
:
2431 case SO_WANTOOBFLAG
:
2433 optval
= so
->so_options
& sopt
->sopt_name
;
2435 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2439 optval
= so
->so_type
;
2449 m1
= so
->so_rcv
.sb_mb
;
2450 if (so
->so_proto
->pr_flags
& PR_ATOMIC
)
2453 if (m1
->m_type
== MT_DATA
)
2454 pkt_total
+= m1
->m_len
;
2459 optval
= so
->so_rcv
.sb_cc
;
2463 optval
= so
->so_snd
.sb_cc
;
2467 optval
= so
->so_error
;
2472 optval
= so
->so_snd
.sb_hiwat
;
2476 optval
= so
->so_rcv
.sb_hiwat
;
2480 optval
= so
->so_snd
.sb_lowat
;
2484 optval
= so
->so_rcv
.sb_lowat
;
2489 tv
= (sopt
->sopt_name
== SO_SNDTIMEO
?
2490 so
->so_snd
.sb_timeo
: so
->so_rcv
.sb_timeo
);
2492 error
= sooptcopyout(sopt
, &tv
, sizeof tv
);
2496 optval
= (so
->so_flags
& SOF_NOSIGPIPE
);
2500 optval
= (so
->so_flags
& SOF_NOADDRAVAIL
);
2504 error
= ENOPROTOOPT
;
2507 socket_unlock(so
, 1);
2512 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
2514 soopt_getm(struct sockopt
*sopt
, struct mbuf
**mp
)
2516 struct mbuf
*m
, *m_prev
;
2517 int sopt_size
= sopt
->sopt_valsize
;
2519 if (sopt_size
> MAX_SOOPTGETM_SIZE
)
2522 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
2525 if (sopt_size
> MLEN
) {
2526 MCLGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
);
2527 if ((m
->m_flags
& M_EXT
) == 0) {
2531 m
->m_len
= min(MCLBYTES
, sopt_size
);
2533 m
->m_len
= min(MLEN
, sopt_size
);
2535 sopt_size
-= m
->m_len
;
2540 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
2545 if (sopt_size
> MLEN
) {
2546 MCLGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
);
2547 if ((m
->m_flags
& M_EXT
) == 0) {
2551 m
->m_len
= min(MCLBYTES
, sopt_size
);
2553 m
->m_len
= min(MLEN
, sopt_size
);
2555 sopt_size
-= m
->m_len
;
2562 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2564 soopt_mcopyin(struct sockopt
*sopt
, struct mbuf
*m
)
2566 struct mbuf
*m0
= m
;
2568 if (sopt
->sopt_val
== USER_ADDR_NULL
)
2570 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
2571 if (sopt
->sopt_p
!= NULL
) {
2574 error
= copyin(sopt
->sopt_val
, mtod(m
, char *), m
->m_len
);
2580 bcopy(CAST_DOWN(caddr_t
, sopt
->sopt_val
), mtod(m
, char *), m
->m_len
);
2581 sopt
->sopt_valsize
-= m
->m_len
;
2582 sopt
->sopt_val
+= m
->m_len
;
2585 if (m
!= NULL
) /* should be allocated enoughly at ip6_sooptmcopyin() */
2586 panic("soopt_mcopyin");
2590 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2592 soopt_mcopyout(struct sockopt
*sopt
, struct mbuf
*m
)
2594 struct mbuf
*m0
= m
;
2597 if (sopt
->sopt_val
== USER_ADDR_NULL
)
2599 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
2600 if (sopt
->sopt_p
!= NULL
) {
2603 error
= copyout(mtod(m
, char *), sopt
->sopt_val
, m
->m_len
);
2609 bcopy(mtod(m
, char *), CAST_DOWN(caddr_t
, sopt
->sopt_val
), m
->m_len
);
2610 sopt
->sopt_valsize
-= m
->m_len
;
2611 sopt
->sopt_val
+= m
->m_len
;
2612 valsize
+= m
->m_len
;
2616 /* enough soopt buffer should be given from user-land */
2620 sopt
->sopt_valsize
= valsize
;
2626 register struct socket
*so
;
2630 if (so
->so_pgid
< 0)
2631 gsignal(-so
->so_pgid
, SIGURG
);
2632 else if (so
->so_pgid
> 0 && (p
= pfind(so
->so_pgid
)) != 0)
2634 selwakeup(&so
->so_rcv
.sb_sel
);
2638 sopoll(struct socket
*so
, int events
, __unused kauth_cred_t cred
, void * wql
)
2640 struct proc
*p
= current_proc();
2645 if (events
& (POLLIN
| POLLRDNORM
))
2647 revents
|= events
& (POLLIN
| POLLRDNORM
);
2649 if (events
& (POLLOUT
| POLLWRNORM
))
2650 if (sowriteable(so
))
2651 revents
|= events
& (POLLOUT
| POLLWRNORM
);
2653 if (events
& (POLLPRI
| POLLRDBAND
))
2654 if (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
))
2655 revents
|= events
& (POLLPRI
| POLLRDBAND
);
2658 if (events
& (POLLIN
| POLLPRI
| POLLRDNORM
| POLLRDBAND
)) {
2659 /* Darwin sets the flag first, BSD calls selrecord first */
2660 so
->so_rcv
.sb_flags
|= SB_SEL
;
2661 selrecord(p
, &so
->so_rcv
.sb_sel
, wql
);
2664 if (events
& (POLLOUT
| POLLWRNORM
)) {
2665 /* Darwin sets the flag first, BSD calls selrecord first */
2666 so
->so_snd
.sb_flags
|= SB_SEL
;
2667 selrecord(p
, &so
->so_snd
.sb_sel
, wql
);
2671 socket_unlock(so
, 1);
2675 int soo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
2678 soo_kqfilter(__unused
struct fileproc
*fp
, struct knote
*kn
, __unused
struct proc
*p
)
2680 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2684 switch (kn
->kn_filter
) {
2686 if (so
->so_options
& SO_ACCEPTCONN
)
2687 kn
->kn_fop
= &solisten_filtops
;
2689 kn
->kn_fop
= &soread_filtops
;
2693 kn
->kn_fop
= &sowrite_filtops
;
2697 socket_unlock(so
, 1);
2701 if (KNOTE_ATTACH(&sb
->sb_sel
.si_note
, kn
))
2702 sb
->sb_flags
|= SB_KNOTE
;
2703 socket_unlock(so
, 1);
2708 filt_sordetach(struct knote
*kn
)
2710 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2713 if (so
->so_rcv
.sb_flags
& SB_KNOTE
)
2714 if (KNOTE_DETACH(&so
->so_rcv
.sb_sel
.si_note
, kn
))
2715 so
->so_rcv
.sb_flags
&= ~SB_KNOTE
;
2716 socket_unlock(so
, 1);
2721 filt_soread(struct knote
*kn
, long hint
)
2723 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2725 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2728 if (so
->so_oobmark
) {
2729 if (kn
->kn_flags
& EV_OOBAND
) {
2730 kn
->kn_data
= so
->so_rcv
.sb_cc
- so
->so_oobmark
;
2731 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2732 socket_unlock(so
, 1);
2735 kn
->kn_data
= so
->so_oobmark
;
2736 kn
->kn_flags
|= EV_OOBAND
;
2738 kn
->kn_data
= so
->so_rcv
.sb_cc
;
2739 if (so
->so_state
& SS_CANTRCVMORE
) {
2740 kn
->kn_flags
|= EV_EOF
;
2741 kn
->kn_fflags
= so
->so_error
;
2742 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2743 socket_unlock(so
, 1);
2748 if (so
->so_state
& SS_RCVATMARK
) {
2749 if (kn
->kn_flags
& EV_OOBAND
) {
2750 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2751 socket_unlock(so
, 1);
2754 kn
->kn_flags
|= EV_OOBAND
;
2755 } else if (kn
->kn_flags
& EV_OOBAND
) {
2757 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2758 socket_unlock(so
, 1);
2762 if (so
->so_error
) { /* temporary udp error */
2763 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2764 socket_unlock(so
, 1);
2768 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2769 socket_unlock(so
, 1);
2771 return( kn
->kn_flags
& EV_OOBAND
||
2772 kn
->kn_data
>= ((kn
->kn_sfflags
& NOTE_LOWAT
) ?
2773 kn
->kn_sdata
: so
->so_rcv
.sb_lowat
));
2777 filt_sowdetach(struct knote
*kn
)
2779 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2782 if(so
->so_snd
.sb_flags
& SB_KNOTE
)
2783 if (KNOTE_DETACH(&so
->so_snd
.sb_sel
.si_note
, kn
))
2784 so
->so_snd
.sb_flags
&= ~SB_KNOTE
;
2785 socket_unlock(so
, 1);
2790 filt_sowrite(struct knote
*kn
, long hint
)
2792 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2794 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2797 kn
->kn_data
= sbspace(&so
->so_snd
);
2798 if (so
->so_state
& SS_CANTSENDMORE
) {
2799 kn
->kn_flags
|= EV_EOF
;
2800 kn
->kn_fflags
= so
->so_error
;
2801 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2802 socket_unlock(so
, 1);
2805 if (so
->so_error
) { /* temporary udp error */
2806 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2807 socket_unlock(so
, 1);
2810 if (((so
->so_state
& SS_ISCONNECTED
) == 0) &&
2811 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)) {
2812 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2813 socket_unlock(so
, 1);
2816 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2817 socket_unlock(so
, 1);
2818 if (kn
->kn_sfflags
& NOTE_LOWAT
)
2819 return (kn
->kn_data
>= kn
->kn_sdata
);
2820 return (kn
->kn_data
>= so
->so_snd
.sb_lowat
);
2825 filt_solisten(struct knote
*kn
, long hint
)
2827 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_fglob
->fg_data
;
2830 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2832 kn
->kn_data
= so
->so_qlen
;
2833 isempty
= ! TAILQ_EMPTY(&so
->so_comp
);
2834 if ((hint
& SO_FILT_HINT_LOCKED
) == 0)
2835 socket_unlock(so
, 1);
2841 socket_lock(so
, refcount
)
2845 int error
= 0, lr
, lr_saved
;
2847 __asm__
volatile("mflr %0" : "=r" (lr
));
2851 if (so
->so_proto
->pr_lock
) {
2852 error
= (*so
->so_proto
->pr_lock
)(so
, refcount
, lr_saved
);
2855 #ifdef MORE_LOCKING_DEBUG
2856 lck_mtx_assert(so
->so_proto
->pr_domain
->dom_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
2858 lck_mtx_lock(so
->so_proto
->pr_domain
->dom_mtx
);
2861 so
->reserved3
= (void*)lr_saved
; /* save caller for refcount going to zero */
2869 socket_unlock(so
, refcount
)
2873 int error
= 0, lr
, lr_saved
;
2874 lck_mtx_t
* mutex_held
;
2877 __asm__
volatile("mflr %0" : "=r" (lr
));
2883 if (so
->so_proto
== NULL
)
2884 panic("socket_unlock null so_proto so=%x\n", so
);
2886 if (so
&& so
->so_proto
->pr_unlock
)
2887 error
= (*so
->so_proto
->pr_unlock
)(so
, refcount
, lr_saved
);
2889 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
2890 #ifdef MORE_LOCKING_DEBUG
2891 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
2894 if (so
->so_usecount
<= 0)
2895 panic("socket_unlock: bad refcount so=%x value=%d\n", so
, so
->so_usecount
);
2897 if (so
->so_usecount
== 0) {
2898 sofreelastref(so
, 1);
2901 so
->reserved4
= (void*)lr_saved
; /* save caller */
2903 lck_mtx_unlock(mutex_held
);
2908 //### Called with socket locked, will unlock socket
2915 lck_mtx_t
* mutex_held
;
2917 __asm__
volatile("mflr %0" : "=r" (lr
));
2920 if (so
->so_proto
->pr_getlock
!= NULL
)
2921 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
2923 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
2924 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
2926 sofreelastref(so
, 0);
2933 socket_lock(so
, 1); /* locks & take one reference on socket */
2934 socket_unlock(so
, 0); /* unlock only */
2942 socket_unlock(so
, 1);