2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
26 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
28 * Copyright (c) 1982, 1986, 1988, 1990, 1993
29 * The Regents of the University of California. All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
60 * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.16 2001/06/14 20:46:06 ume Exp $
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/filedesc.h>
68 #include <sys/fcntl.h>
69 #include <sys/malloc.h>
71 #include <sys/domain.h>
72 #include <sys/kernel.h>
73 #include <sys/event.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/resourcevar.h>
79 #include <sys/signalvar.h>
80 #include <sys/sysctl.h>
83 #include <sys/kdebug.h>
84 #include <net/route.h>
85 #include <netinet/in.h>
86 #include <netinet/in_pcb.h>
87 #include <kern/zalloc.h>
88 #include <machine/limits.h>
91 int so_cache_timeouts
= 0;
92 int so_cache_max_freed
= 0;
93 int cached_sock_count
= 0;
94 struct socket
*socket_cache_head
= 0;
95 struct socket
*socket_cache_tail
= 0;
96 u_long so_cache_time
= 0;
97 int so_cache_init_done
= 0;
98 struct zone
*so_cache_zone
;
99 extern int get_inpcb_str_size();
100 extern int get_tcp_str_size();
102 #include <machine/limits.h>
104 static void filt_sordetach(struct knote
*kn
);
105 static int filt_soread(struct knote
*kn
, long hint
);
106 static void filt_sowdetach(struct knote
*kn
);
107 static int filt_sowrite(struct knote
*kn
, long hint
);
108 static int filt_solisten(struct knote
*kn
, long hint
);
110 static struct filterops solisten_filtops
=
111 { 1, NULL
, filt_sordetach
, filt_solisten
};
112 static struct filterops soread_filtops
=
113 { 1, NULL
, filt_sordetach
, filt_soread
};
114 static struct filterops sowrite_filtops
=
115 { 1, NULL
, filt_sowdetach
, filt_sowrite
};
117 int socket_debug
= 0;
118 int socket_zone
= M_SOCKET
;
119 so_gen_t so_gencnt
; /* generation count for sockets */
121 MALLOC_DEFINE(M_SONAME
, "soname", "socket name");
122 MALLOC_DEFINE(M_PCB
, "pcb", "protocol control block");
124 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0)
125 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2)
126 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1)
127 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3)
128 #define DBG_FNC_SOSEND NETDBG_CODE(DBG_NETSOCK, (4 << 8) | 1)
129 #define DBG_FNC_SORECEIVE NETDBG_CODE(DBG_NETSOCK, (8 << 8))
130 #define DBG_FNC_SOSHUTDOWN NETDBG_CODE(DBG_NETSOCK, (9 << 8))
133 SYSCTL_DECL(_kern_ipc
);
135 static int somaxconn
= SOMAXCONN
;
136 SYSCTL_INT(_kern_ipc
, KIPC_SOMAXCONN
, somaxconn
, CTLFLAG_RW
, &somaxconn
,
139 /* Should we get a maximum also ??? */
140 static int sosendmaxchain
= 65536;
141 static int sosendminchain
= 16384;
142 static int sorecvmincopy
= 16384;
143 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sosendminchain
, CTLFLAG_RW
, &sosendminchain
,
145 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sorecvmincopy
, CTLFLAG_RW
, &sorecvmincopy
,
148 void so_cache_timer();
149 struct mbuf
*m_getpackets(int, int, int);
153 * Socket operation routines.
154 * These routines are called by the routines in
155 * sys_socket.c or from a system process, and
156 * implement the semantics of socket operations by
157 * switching out to the protocol specific routines.
165 so_cache_init_done
= 1;
167 timeout(so_cache_timer
, NULL
, (SO_CACHE_FLUSH_INTERVAL
* hz
));
168 str_size
= (vm_size_t
)( sizeof(struct socket
) + 4 +
169 get_inpcb_str_size() + 4 +
171 so_cache_zone
= zinit (str_size
, 120000*str_size
, 8192, "socache zone");
173 kprintf("cached_sock_alloc -- so_cache_zone size is %x\n", str_size
);
178 void cached_sock_alloc(so
, waitok
)
185 register u_long offset
;
189 if (cached_sock_count
) {
191 *so
= socket_cache_head
;
193 panic("cached_sock_alloc: cached sock is null");
195 socket_cache_head
= socket_cache_head
->cache_next
;
196 if (socket_cache_head
)
197 socket_cache_head
->cache_prev
= 0;
199 socket_cache_tail
= 0;
202 temp
= (*so
)->so_saved_pcb
;
203 bzero((caddr_t
)*so
, sizeof(struct socket
));
205 kprintf("cached_sock_alloc - retreiving cached sock %x - count == %d\n", *so
,
208 (*so
)->so_saved_pcb
= temp
;
212 kprintf("Allocating cached sock %x from memory\n", *so
);
217 *so
= (struct socket
*) zalloc(so_cache_zone
);
219 *so
= (struct socket
*) zalloc_noblock(so_cache_zone
);
224 bzero((caddr_t
)*so
, sizeof(struct socket
));
227 * Define offsets for extra structures into our single block of
228 * memory. Align extra structures on longword boundaries.
232 offset
= (u_long
) *so
;
233 offset
+= sizeof(struct socket
);
236 offset
&= 0xfffffffc;
238 (*so
)->so_saved_pcb
= (caddr_t
) offset
;
239 offset
+= get_inpcb_str_size();
242 offset
&= 0xfffffffc;
245 ((struct inpcb
*) (*so
)->so_saved_pcb
)->inp_saved_ppcb
= (caddr_t
) offset
;
247 kprintf("Allocating cached socket - %x, pcb=%x tcpcb=%x\n", *so
,
249 ((struct inpcb
*)(*so
)->so_saved_pcb
)->inp_saved_ppcb
);
253 (*so
)->cached_in_sock_layer
= 1;
257 void cached_sock_free(so
)
264 if (++cached_sock_count
> MAX_CACHED_SOCKETS
) {
268 kprintf("Freeing overflowed cached socket %x\n", so
);
270 zfree(so_cache_zone
, (vm_offset_t
) so
);
274 kprintf("Freeing socket %x into cache\n", so
);
276 if (so_cache_hw
< cached_sock_count
)
277 so_cache_hw
= cached_sock_count
;
279 so
->cache_next
= socket_cache_head
;
281 if (socket_cache_head
)
282 socket_cache_head
->cache_prev
= so
;
284 socket_cache_tail
= so
;
286 so
->cache_timestamp
= so_cache_time
;
287 socket_cache_head
= so
;
292 kprintf("Freed cached sock %x into cache - count is %d\n", so
, cached_sock_count
);
299 void so_cache_timer()
301 register struct socket
*p
;
303 register int n_freed
= 0;
304 boolean_t funnel_state
;
306 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
312 while (p
= socket_cache_tail
)
314 if ((so_cache_time
- p
->cache_timestamp
) < SO_CACHE_TIME_LIMIT
)
319 if (socket_cache_tail
= p
->cache_prev
)
320 p
->cache_prev
->cache_next
= 0;
321 if (--cached_sock_count
== 0)
322 socket_cache_head
= 0;
326 zfree(so_cache_zone
, (vm_offset_t
) p
);
329 if (++n_freed
>= SO_CACHE_MAX_FREE_BATCH
)
331 so_cache_max_freed
++;
337 timeout(so_cache_timer
, NULL
, (SO_CACHE_FLUSH_INTERVAL
* hz
));
339 (void) thread_funnel_set(network_flock
, FALSE
);
342 #endif /* __APPLE__ */
345 * Get a socket structure from our zone, and initialize it.
346 * We don't implement `waitok' yet (see comments in uipc_domain.c).
347 * Note that it would probably be better to allocate socket
348 * and PCB at the same time, but I'm not convinced that all
349 * the protocols can be easily modified to do this.
352 soalloc(waitok
, dom
, type
)
359 if ((dom
== PF_INET
) && (type
== SOCK_STREAM
))
360 cached_sock_alloc(&so
, waitok
);
363 so
= _MALLOC_ZONE(sizeof(*so
), socket_zone
, M_WAITOK
);
365 bzero(so
, sizeof *so
);
367 /* XXX race condition for reentrant kernel */
370 so
->so_gencnt
= ++so_gencnt
;
371 so
->so_zone
= socket_zone
;
378 socreate(dom
, aso
, type
, proto
)
384 struct proc
*p
= current_proc();
385 register struct protosw
*prp
;
386 register struct socket
*so
;
387 register int error
= 0;
389 extern int tcpconsdebug
;
392 prp
= pffindproto(dom
, proto
, type
);
394 prp
= pffindtype(dom
, type
);
396 if (prp
== 0 || prp
->pr_usrreqs
->pru_attach
== 0)
397 return (EPROTONOSUPPORT
);
400 if (p
->p_prison
&& jail_socket_unixiproute_only
&&
401 prp
->pr_domain
->dom_family
!= PF_LOCAL
&&
402 prp
->pr_domain
->dom_family
!= PF_INET
&&
403 prp
->pr_domain
->dom_family
!= PF_ROUTE
) {
404 return (EPROTONOSUPPORT
);
408 if (prp
->pr_type
!= type
)
410 so
= soalloc(p
!= 0, dom
, type
);
414 TAILQ_INIT(&so
->so_incomp
);
415 TAILQ_INIT(&so
->so_comp
);
420 if (p
->p_ucred
->cr_uid
== 0)
421 so
->so_state
= SS_PRIV
;
423 so
->so_uid
= p
->p_ucred
->cr_uid
;
426 so
->so_cred
= p
->p_ucred
;
431 so
->so_rcv
.sb_flags
|= SB_RECV
; /* XXX */
432 if (prp
->pr_sfilter
.tqh_first
)
433 error
= sfilter_init(so
);
436 error
= (*prp
->pr_usrreqs
->pru_attach
)(so
, proto
, p
);
440 * If so_pcb is not zero, the socket will be leaked,
441 * so protocol attachment handler must be coded carefuly
443 so
->so_state
|= SS_NOFDREF
;
448 prp
->pr_domain
->dom_refs
++;
449 so
->so_rcv
.sb_so
= so
->so_snd
.sb_so
= so
;
450 TAILQ_INIT(&so
->so_evlist
);
452 if (tcpconsdebug
== 2)
453 so
->so_options
|= SO_DEBUG
;
464 struct sockaddr
*nam
;
467 struct proc
*p
= current_proc();
472 error
= (*so
->so_proto
->pr_usrreqs
->pru_bind
)(so
, nam
, p
);
476 if (kp
->e_soif
&& kp
->e_soif
->sf_sobind
) {
477 error
= (*kp
->e_soif
->sf_sobind
)(so
, nam
, kp
);
479 if (error
== EJUSTRETURN
) {
498 so
->so_gencnt
= ++so_gencnt
;
501 if (so
->so_rcv
.sb_hiwat
)
502 (void)chgsbsize(so
->so_cred
->cr_uidinfo
,
503 &so
->so_rcv
.sb_hiwat
, 0, RLIM_INFINITY
);
504 if (so
->so_snd
.sb_hiwat
)
505 (void)chgsbsize(so
->so_cred
->cr_uidinfo
,
506 &so
->so_snd
.sb_hiwat
, 0, RLIM_INFINITY
);
508 if (so
->so_accf
!= NULL
) {
509 if (so
->so_accf
->so_accept_filter
!= NULL
&&
510 so
->so_accf
->so_accept_filter
->accf_destroy
!= NULL
) {
511 so
->so_accf
->so_accept_filter
->accf_destroy(so
);
513 if (so
->so_accf
->so_accept_filter_str
!= NULL
)
514 FREE(so
->so_accf
->so_accept_filter_str
, M_ACCF
);
515 FREE(so
->so_accf
, M_ACCF
);
519 zfreei(so
->so_zone
, so
);
521 if (so
->cached_in_sock_layer
== 1)
522 cached_sock_free(so
);
524 _FREE_ZONE(so
, sizeof(*so
), so
->so_zone
);
525 #endif /* __APPLE__ */
529 solisten(so
, backlog
)
530 register struct socket
*so
;
535 struct proc
*p
= current_proc();
539 error
= (*so
->so_proto
->pr_usrreqs
->pru_listen
)(so
, p
);
544 if (TAILQ_EMPTY(&so
->so_comp
))
545 so
->so_options
|= SO_ACCEPTCONN
;
546 if (backlog
< 0 || backlog
> somaxconn
)
548 so
->so_qlimit
= backlog
;
551 if (kp
->e_soif
&& kp
->e_soif
->sf_solisten
) {
552 error
= (*kp
->e_soif
->sf_solisten
)(so
, kp
);
554 if (error
== EJUSTRETURN
) {
572 register struct socket
*so
;
576 struct socket
*head
= so
->so_head
;
580 if (kp
->e_soif
&& kp
->e_soif
->sf_sofree
) {
581 error
= (*kp
->e_soif
->sf_sofree
)(so
, kp
);
583 selthreadclear(&so
->so_snd
.sb_sel
);
584 selthreadclear(&so
->so_rcv
.sb_sel
);
585 return; /* void fn */
591 if (so
->so_pcb
|| (so
->so_state
& SS_NOFDREF
) == 0) {
593 selthreadclear(&so
->so_snd
.sb_sel
);
594 selthreadclear(&so
->so_rcv
.sb_sel
);
599 if (so
->so_state
& SS_INCOMP
) {
600 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
602 } else if (so
->so_state
& SS_COMP
) {
604 * We must not decommission a socket that's
605 * on the accept(2) queue. If we do, then
606 * accept(2) may hang after select(2) indicated
607 * that the listening socket was ready.
610 selthreadclear(&so
->so_snd
.sb_sel
);
611 selthreadclear(&so
->so_rcv
.sb_sel
);
615 panic("sofree: not queued");
618 so
->so_state
&= ~SS_INCOMP
;
622 selthreadclear(&so
->so_snd
.sb_sel
);
623 sbrelease(&so
->so_snd
);
631 * Close a socket on last file table reference removal.
632 * Initiate disconnect if connected.
633 * Free socket when disconnect complete.
637 register struct socket
*so
;
639 int s
= splnet(); /* conservative */
644 funsetown(so
->so_sigio
);
648 if (kp
->e_soif
&& kp
->e_soif
->sf_soclose
) {
649 error
= (*kp
->e_soif
->sf_soclose
)(so
, kp
);
652 return((error
== EJUSTRETURN
) ? 0 : error
);
658 if (so
->so_options
& SO_ACCEPTCONN
) {
659 struct socket
*sp
, *sonext
;
661 sp
= TAILQ_FIRST(&so
->so_incomp
);
662 for (; sp
!= NULL
; sp
= sonext
) {
663 sonext
= TAILQ_NEXT(sp
, so_list
);
666 for (sp
= TAILQ_FIRST(&so
->so_comp
); sp
!= NULL
; sp
= sonext
) {
667 sonext
= TAILQ_NEXT(sp
, so_list
);
668 /* Dequeue from so_comp since sofree() won't do it */
669 TAILQ_REMOVE(&so
->so_comp
, sp
, so_list
);
671 sp
->so_state
&= ~SS_COMP
;
679 if (so
->so_state
& SS_ISCONNECTED
) {
680 if ((so
->so_state
& SS_ISDISCONNECTING
) == 0) {
681 error
= sodisconnect(so
);
685 if (so
->so_options
& SO_LINGER
) {
686 if ((so
->so_state
& SS_ISDISCONNECTING
) &&
687 (so
->so_state
& SS_NBIO
))
689 while (so
->so_state
& SS_ISCONNECTED
) {
690 error
= tsleep((caddr_t
)&so
->so_timeo
,
691 PSOCK
| PCATCH
, "soclos", so
->so_linger
);
699 int error2
= (*so
->so_proto
->pr_usrreqs
->pru_detach
)(so
);
704 if (so
->so_pcb
&& so
->so_state
& SS_NOFDREF
)
705 panic("soclose: NOFDREF");
706 so
->so_state
|= SS_NOFDREF
;
708 so
->so_proto
->pr_domain
->dom_refs
--;
717 * Must be called at splnet...
725 error
= (*so
->so_proto
->pr_usrreqs
->pru_abort
)(so
);
735 register struct socket
*so
;
736 struct sockaddr
**nam
;
742 if ((so
->so_state
& SS_NOFDREF
) == 0)
743 panic("soaccept: !NOFDREF");
744 so
->so_state
&= ~SS_NOFDREF
;
745 error
= (*so
->so_proto
->pr_usrreqs
->pru_accept
)(so
, nam
);
749 if (kp
->e_soif
&& kp
->e_soif
->sf_soaccept
) {
750 error
= (*kp
->e_soif
->sf_soaccept
)(so
, nam
, kp
);
752 if (error
== EJUSTRETURN
) {
771 register struct socket
*so
;
772 struct sockaddr
*nam
;
777 struct proc
*p
= current_proc();
780 if (so
->so_options
& SO_ACCEPTCONN
)
784 * If protocol is connection-based, can only connect once.
785 * Otherwise, if connected, try to disconnect first.
786 * This allows user to disconnect by connecting to, e.g.,
789 if (so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
) &&
790 ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) ||
791 (error
= sodisconnect(so
))))
795 * Run connect filter before calling protocol:
796 * - non-blocking connect returns before completion;
797 * - allows filters to modify address.
801 if (kp
->e_soif
&& kp
->e_soif
->sf_soconnect
) {
802 error
= (*kp
->e_soif
->sf_soconnect
)(so
, nam
, kp
);
804 if (error
== EJUSTRETURN
) {
813 error
= (*so
->so_proto
->pr_usrreqs
->pru_connect
)(so
, nam
, p
);
821 register struct socket
*so1
;
828 error
= (*so1
->so_proto
->pr_usrreqs
->pru_connect2
)(so1
, so2
);
830 kp
= sotokextcb(so1
);
832 if (kp
->e_soif
&& kp
->e_soif
->sf_soconnect2
) {
833 error
= (*kp
->e_soif
->sf_soconnect2
)(so1
, so2
, kp
);
835 if (error
== EJUSTRETURN
) {
852 register struct socket
*so
;
858 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
862 if (so
->so_state
& SS_ISDISCONNECTING
) {
866 error
= (*so
->so_proto
->pr_usrreqs
->pru_disconnect
)(so
);
870 if (kp
->e_soif
&& kp
->e_soif
->sf_sodisconnect
) {
871 error
= (*kp
->e_soif
->sf_sodisconnect
)(so
, kp
);
873 if (error
== EJUSTRETURN
) {
890 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_DONTWAIT : M_WAIT)
893 * If send must go all at once and message is larger than
894 * send buffering, then hard error.
895 * Lock against other senders.
896 * If must go all at once and not enough room now, then
897 * inform user that this would block and do nothing.
898 * Otherwise, if nonblocking, send as much as possible.
899 * The data to be sent is described by "uio" if nonzero,
900 * otherwise by the mbuf chain "top" (which must be null
901 * if uio is not). Data provided in mbuf chain must be small
902 * enough to send all at once.
904 * Returns nonzero on error, timeout or signal; callers
905 * must check for short counts if EINTR/ERESTART are returned.
906 * Data and control buffers are freed on return.
908 * MSG_HOLD: go thru most of sosend(), but just enqueue the mbuf
909 * MSG_SEND: go thru as for MSG_HOLD on current fragment, then
910 * point at the mbuf chain being constructed and go from there.
913 sosend(so
, addr
, uio
, top
, control
, flags
)
914 register struct socket
*so
;
915 struct sockaddr
*addr
;
918 struct mbuf
*control
;
923 register struct mbuf
*m
, *freelist
= NULL
;
924 register long space
, len
, resid
;
925 int clen
= 0, error
, s
, dontroute
, mlen
, sendflags
;
926 int atomic
= sosendallatonce(so
) || top
;
927 struct proc
*p
= current_proc();
931 resid
= uio
->uio_resid
;
933 resid
= top
->m_pkthdr
.len
;
935 KERNEL_DEBUG((DBG_FNC_SOSEND
| DBG_FUNC_START
),
940 so
->so_snd
.sb_hiwat
);
943 * In theory resid should be unsigned.
944 * However, space must be signed, as it might be less than 0
945 * if we over-committed, and we must use a signed comparison
946 * of space and resid. On the other hand, a negative resid
947 * causes us to loop sending 0-length segments to the protocol.
949 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
950 * type sockets since that's an error.
952 if (resid
< 0 || so
->so_type
== SOCK_STREAM
&& (flags
& MSG_EOR
)) {
958 (flags
& MSG_DONTROUTE
) && (so
->so_options
& SO_DONTROUTE
) == 0 &&
959 (so
->so_proto
->pr_flags
& PR_ATOMIC
);
961 p
->p_stats
->p_ru
.ru_msgsnd
++;
963 clen
= control
->m_len
;
964 #define snderr(errno) { error = errno; splx(s); goto release; }
967 error
= sblock(&so
->so_snd
, SBLOCKWAIT(flags
));
972 if (so
->so_state
& SS_CANTSENDMORE
)
975 error
= so
->so_error
;
980 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
982 * `sendto' and `sendmsg' is allowed on a connection-
983 * based socket if it supports implied connect.
984 * Return ENOTCONN if not connected and no address is
987 if ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) &&
988 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) == 0) {
989 if ((so
->so_state
& SS_ISCONFIRMING
) == 0 &&
990 !(resid
== 0 && clen
!= 0))
992 } else if (addr
== 0 && !(flags
&MSG_HOLD
))
993 snderr(so
->so_proto
->pr_flags
& PR_CONNREQUIRED
?
994 ENOTCONN
: EDESTADDRREQ
);
996 space
= sbspace(&so
->so_snd
);
999 if ((atomic
&& resid
> so
->so_snd
.sb_hiwat
) ||
1000 clen
> so
->so_snd
.sb_hiwat
)
1002 if (space
< resid
+ clen
&&
1003 (atomic
|| space
< so
->so_snd
.sb_lowat
|| space
< clen
)) {
1004 if (so
->so_state
& SS_NBIO
)
1005 snderr(EWOULDBLOCK
);
1006 sbunlock(&so
->so_snd
);
1007 error
= sbwait(&so
->so_snd
);
1020 * Data is prepackaged in "top".
1023 if (flags
& MSG_EOR
)
1024 top
->m_flags
|= M_EOR
;
1026 boolean_t dropped_funnel
= FALSE
;
1030 bytes_to_copy
= min(resid
, space
);
1032 if (sosendminchain
> 0) {
1033 if (bytes_to_copy
>= sosendminchain
) {
1034 dropped_funnel
= TRUE
;
1035 (void)thread_funnel_set(network_flock
, FALSE
);
1039 chainlength
= sosendmaxchain
;
1043 if (bytes_to_copy
>= MINCLSIZE
) {
1045 * try to maintain a local cache of mbuf clusters needed to complete this write
1046 * the list is further limited to the number that are currently needed to fill the socket
1047 * this mechanism allows a large number of mbufs/clusters to be grabbed under a single
1048 * mbuf lock... if we can't get any clusters, than fall back to trying for mbufs
1049 * if we fail early (or miscalcluate the number needed) make sure to release any clusters
1050 * we haven't yet consumed.
1052 if ((m
= freelist
) == NULL
) {
1054 int hdrs_needed
= 0;
1058 num_needed
= bytes_to_copy
/ MCLBYTES
;
1060 if ((bytes_to_copy
- (num_needed
* MCLBYTES
)) >= MINCLSIZE
)
1063 if ((freelist
= m_getpackets(num_needed
, hdrs_needed
, M_WAIT
)) == NULL
)
1064 goto getpackets_failed
;
1067 freelist
= m
->m_next
;
1071 len
= min(mlen
, bytes_to_copy
);
1075 MGETHDR(m
, M_WAIT
, MT_DATA
);
1077 m
->m_pkthdr
.len
= 0;
1078 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
1080 MGET(m
, M_WAIT
, MT_DATA
);
1083 len
= min(mlen
, bytes_to_copy
);
1085 * For datagram protocols, leave room
1086 * for protocol headers in first mbuf.
1088 if (atomic
&& top
== 0 && len
< mlen
)
1095 error
= uiomove(mtod(m
, caddr_t
), (int)len
, uio
);
1097 resid
= uio
->uio_resid
;
1101 top
->m_pkthdr
.len
+= len
;
1106 if (flags
& MSG_EOR
)
1107 top
->m_flags
|= M_EOR
;
1110 bytes_to_copy
= min(resid
, space
);
1112 } while (space
> 0 && (chainlength
< sosendmaxchain
|| atomic
|| resid
< MINCLSIZE
));
1114 if (dropped_funnel
== TRUE
)
1115 (void)thread_funnel_set(network_flock
, TRUE
);
1120 if (flags
& (MSG_HOLD
|MSG_SEND
))
1121 { /* Enqueue for later, go away if HOLD */
1122 register struct mbuf
*mb1
;
1123 if (so
->so_temp
&& (flags
& MSG_FLUSH
))
1124 { m_freem(so
->so_temp
);
1128 so
->so_tail
->m_next
= top
;
1142 so
->so_options
|= SO_DONTROUTE
;
1143 s
= splnet(); /* XXX */
1144 /* Compute flags here, for pru_send and NKEs */
1145 sendflags
= (flags
& MSG_OOB
) ? PRUS_OOB
:
1147 * If the user set MSG_EOF, the protocol
1148 * understands this flag and nothing left to
1149 * send then use PRU_SEND_EOF instead of PRU_SEND.
1151 ((flags
& MSG_EOF
) &&
1152 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) &&
1155 /* If there is more to send set PRUS_MORETOCOME */
1156 (resid
> 0 && space
> 0) ? PRUS_MORETOCOME
: 0;
1157 kp
= sotokextcb(so
);
1159 { if (kp
->e_soif
&& kp
->e_soif
->sf_sosend
) {
1160 error
= (*kp
->e_soif
->sf_sosend
)(so
, &addr
,
1167 if (error
== EJUSTRETURN
) {
1168 sbunlock(&so
->so_snd
);
1171 m_freem_list(freelist
);
1180 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)(so
,
1181 sendflags
, top
, addr
, control
, p
);
1184 if (flags
& MSG_SEND
)
1188 so
->so_options
&= ~SO_DONTROUTE
;
1195 } while (resid
&& space
> 0);
1199 sbunlock(&so
->so_snd
);
1206 m_freem_list(freelist
);
1208 KERNEL_DEBUG(DBG_FNC_SOSEND
| DBG_FUNC_END
,
1219 * Implement receive operations on a socket.
1220 * We depend on the way that records are added to the sockbuf
1221 * by sbappend*. In particular, each record (mbufs linked through m_next)
1222 * must begin with an address if the protocol so specifies,
1223 * followed by an optional mbuf or mbufs containing ancillary data,
1224 * and then zero or more mbufs of data.
1225 * In order to avoid blocking network interrupts for the entire time here,
1226 * we splx() while doing the actual copy to user space.
1227 * Although the sockbuf is locked, new data may still be appended,
1228 * and thus we must maintain consistency of the sockbuf during that time.
1230 * The caller may receive the data as a single mbuf chain by supplying
1231 * an mbuf **mp0 for use in returning the chain. The uio is then used
1232 * only for the count in uio_resid.
1235 soreceive(so
, psa
, uio
, mp0
, controlp
, flagsp
)
1236 register struct socket
*so
;
1237 struct sockaddr
**psa
;
1240 struct mbuf
**controlp
;
1243 register struct mbuf
*m
, **mp
, *ml
;
1244 register int flags
, len
, error
, s
, offset
;
1245 struct protosw
*pr
= so
->so_proto
;
1246 struct mbuf
*nextrecord
;
1248 int orig_resid
= uio
->uio_resid
;
1250 volatile struct mbuf
*free_list
;
1251 volatile int delayed_copy_len
;
1254 struct proc
*p
= current_proc();
1257 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_START
,
1261 so
->so_rcv
.sb_lowat
,
1262 so
->so_rcv
.sb_hiwat
);
1264 kp
= sotokextcb(so
);
1266 if (kp
->e_soif
&& kp
->e_soif
->sf_soreceive
) {
1267 error
= (*kp
->e_soif
->sf_soreceive
)(so
, psa
, &uio
,
1271 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1272 return((error
== EJUSTRETURN
) ? 0 : error
);
1284 flags
= *flagsp
&~ MSG_EOR
;
1288 * When SO_WANTOOBFLAG is set we try to get out-of-band data
1289 * regardless of the flags argument. Here is the case were
1290 * out-of-band data is not inline.
1292 if ((flags
& MSG_OOB
) ||
1293 ((so
->so_options
& SO_WANTOOBFLAG
) != 0 &&
1294 (so
->so_options
& SO_OOBINLINE
) == 0 &&
1295 (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
)))) {
1296 m
= m_get(M_WAIT
, MT_DATA
);
1298 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, ENOBUFS
,0,0,0,0);
1301 error
= (*pr
->pr_usrreqs
->pru_rcvoob
)(so
, m
, flags
& MSG_PEEK
);
1305 error
= uiomove(mtod(m
, caddr_t
),
1306 (int) min(uio
->uio_resid
, m
->m_len
), uio
);
1308 } while (uio
->uio_resid
&& error
== 0 && m
);
1313 if ((so
->so_options
& SO_WANTOOBFLAG
) != 0) {
1314 if (error
== EWOULDBLOCK
|| error
== EINVAL
) {
1316 * Let's try to get normal data:
1317 * EWOULDBLOCK: out-of-band data not receive yet;
1318 * EINVAL: out-of-band data already read.
1322 } else if (error
== 0 && flagsp
)
1325 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1331 *mp
= (struct mbuf
*)0;
1332 if (so
->so_state
& SS_ISCONFIRMING
&& uio
->uio_resid
)
1333 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, 0);
1336 free_list
= (struct mbuf
*)0;
1337 delayed_copy_len
= 0;
1339 error
= sblock(&so
->so_rcv
, SBLOCKWAIT(flags
));
1341 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1346 m
= so
->so_rcv
.sb_mb
;
1348 * If we have less data than requested, block awaiting more
1349 * (subject to any timeout) if:
1350 * 1. the current count is less than the low water mark, or
1351 * 2. MSG_WAITALL is set, and it is possible to do the entire
1352 * receive operation at once if we block (resid <= hiwat).
1353 * 3. MSG_DONTWAIT is not set
1354 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1355 * we have to do the receive in sections, and thus risk returning
1356 * a short count if a timeout or signal occurs after we start.
1358 if (m
== 0 || (((flags
& MSG_DONTWAIT
) == 0 &&
1359 so
->so_rcv
.sb_cc
< uio
->uio_resid
) &&
1360 (so
->so_rcv
.sb_cc
< so
->so_rcv
.sb_lowat
||
1361 ((flags
& MSG_WAITALL
) && uio
->uio_resid
<= so
->so_rcv
.sb_hiwat
)) &&
1362 m
->m_nextpkt
== 0 && (pr
->pr_flags
& PR_ATOMIC
) == 0)) {
1364 KASSERT(m
!= 0 || !so
->so_rcv
.sb_cc
, ("receive 1"));
1368 error
= so
->so_error
;
1369 if ((flags
& MSG_PEEK
) == 0)
1373 if (so
->so_state
& SS_CANTRCVMORE
) {
1379 for (; m
; m
= m
->m_next
)
1380 if (m
->m_type
== MT_OOBDATA
|| (m
->m_flags
& M_EOR
)) {
1381 m
= so
->so_rcv
.sb_mb
;
1384 if ((so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
)) == 0 &&
1385 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)) {
1389 if (uio
->uio_resid
== 0)
1391 if ((so
->so_state
& SS_NBIO
) || (flags
& MSG_DONTWAIT
)) {
1392 error
= EWOULDBLOCK
;
1395 sbunlock(&so
->so_rcv
);
1397 printf("Waiting for socket data\n");
1399 error
= sbwait(&so
->so_rcv
);
1401 printf("SORECEIVE - sbwait returned %d\n", error
);
1404 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1412 uio
->uio_procp
->p_stats
->p_ru
.ru_msgrcv
++;
1413 #else /* __APPLE__ */
1416 * This should be uio->uio-procp; however, some callers of this
1417 * function use auto variables with stack garbage, and fail to
1418 * fill out the uio structure properly.
1421 p
->p_stats
->p_ru
.ru_msgrcv
++;
1422 #endif /* __APPLE__ */
1423 nextrecord
= m
->m_nextpkt
;
1424 if ((pr
->pr_flags
& PR_ADDR
) && m
->m_type
== MT_SONAME
) {
1425 KASSERT(m
->m_type
== MT_SONAME
, ("receive 1a"));
1428 *psa
= dup_sockaddr(mtod(m
, struct sockaddr
*),
1430 if (flags
& MSG_PEEK
) {
1433 sbfree(&so
->so_rcv
, m
);
1434 MFREE(m
, so
->so_rcv
.sb_mb
);
1435 m
= so
->so_rcv
.sb_mb
;
1438 while (m
&& m
->m_type
== MT_CONTROL
&& error
== 0) {
1439 if (flags
& MSG_PEEK
) {
1441 *controlp
= m_copy(m
, 0, m
->m_len
);
1444 sbfree(&so
->so_rcv
, m
);
1446 if (pr
->pr_domain
->dom_externalize
&&
1447 mtod(m
, struct cmsghdr
*)->cmsg_type
==
1449 error
= (*pr
->pr_domain
->dom_externalize
)(m
);
1451 so
->so_rcv
.sb_mb
= m
->m_next
;
1453 m
= so
->so_rcv
.sb_mb
;
1455 MFREE(m
, so
->so_rcv
.sb_mb
);
1456 m
= so
->so_rcv
.sb_mb
;
1461 controlp
= &(*controlp
)->m_next
;
1465 if ((flags
& MSG_PEEK
) == 0)
1466 m
->m_nextpkt
= nextrecord
;
1468 if (type
== MT_OOBDATA
)
1474 if (!(flags
& MSG_PEEK
) && uio
->uio_resid
> sorecvmincopy
)
1482 while (m
&& (uio
->uio_resid
- delayed_copy_len
) > 0 && error
== 0) {
1483 if (m
->m_type
== MT_OOBDATA
) {
1484 if (type
!= MT_OOBDATA
)
1486 } else if (type
== MT_OOBDATA
)
1490 * This assertion needs rework. The trouble is Appletalk is uses many
1491 * mbuf types (NOT listed in mbuf.h!) which will trigger this panic.
1492 * For now just remove the assertion... CSM 9/98
1495 KASSERT(m
->m_type
== MT_DATA
|| m
->m_type
== MT_HEADER
,
1499 * Make sure to allways set MSG_OOB event when getting
1500 * out of band data inline.
1502 if ((so
->so_options
& SO_WANTOOBFLAG
) != 0 &&
1503 (so
->so_options
& SO_OOBINLINE
) != 0 &&
1504 (so
->so_state
& SS_RCVATMARK
) != 0) {
1508 so
->so_state
&= ~SS_RCVATMARK
;
1509 len
= uio
->uio_resid
- delayed_copy_len
;
1510 if (so
->so_oobmark
&& len
> so
->so_oobmark
- offset
)
1511 len
= so
->so_oobmark
- offset
;
1512 if (len
> m
->m_len
- moff
)
1513 len
= m
->m_len
- moff
;
1515 * If mp is set, just pass back the mbufs.
1516 * Otherwise copy them out via the uio, then free.
1517 * Sockbuf must be consistent here (points to current mbuf,
1518 * it points to next record) when we drop priority;
1519 * we must note any additions to the sockbuf when we
1520 * block interrupts again.
1523 if (can_delay
&& len
== m
->m_len
) {
1525 * only delay the copy if we're consuming the
1526 * mbuf and we're NOT in MSG_PEEK mode
1527 * and we have enough data to make it worthwile
1528 * to drop and retake the funnel... can_delay
1529 * reflects the state of the 2 latter constraints
1530 * moff should always be zero in these cases
1532 delayed_copy_len
+= len
;
1536 if (delayed_copy_len
) {
1537 error
= sodelayed_copy(uio
, &free_list
, &delayed_copy_len
);
1543 if (m
!= so
->so_rcv
.sb_mb
) {
1545 * can only get here if MSG_PEEK is not set
1546 * therefore, m should point at the head of the rcv queue...
1547 * if it doesn't, it means something drastically changed
1548 * while we were out from behind the funnel in sodelayed_copy...
1549 * perhaps a RST on the stream... in any event, the stream has
1550 * been interrupted... it's probably best just to return
1551 * whatever data we've moved and let the caller sort it out...
1556 error
= uiomove(mtod(m
, caddr_t
) + moff
, (int)len
, uio
);
1563 uio
->uio_resid
-= len
;
1565 if (len
== m
->m_len
- moff
) {
1566 if (m
->m_flags
& M_EOR
)
1568 if (flags
& MSG_PEEK
) {
1572 nextrecord
= m
->m_nextpkt
;
1573 sbfree(&so
->so_rcv
, m
);
1578 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
1579 *mp
= (struct mbuf
*)0;
1582 if (free_list
== NULL
)
1587 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
1591 m
->m_nextpkt
= nextrecord
;
1594 if (flags
& MSG_PEEK
)
1598 *mp
= m_copym(m
, 0, len
, M_WAIT
);
1601 so
->so_rcv
.sb_cc
-= len
;
1604 if (so
->so_oobmark
) {
1605 if ((flags
& MSG_PEEK
) == 0) {
1606 so
->so_oobmark
-= len
;
1607 if (so
->so_oobmark
== 0) {
1608 so
->so_state
|= SS_RCVATMARK
;
1610 * delay posting the actual event until after
1611 * any delayed copy processing has finished
1618 if (offset
== so
->so_oobmark
)
1622 if (flags
& MSG_EOR
)
1625 * If the MSG_WAITALL or MSG_WAITSTREAM flag is set (for non-atomic socket),
1626 * we must not quit until "uio->uio_resid == 0" or an error
1627 * termination. If a signal/timeout occurs, return
1628 * with a short count but without error.
1629 * Keep sockbuf locked against other readers.
1631 while (flags
& (MSG_WAITALL
|MSG_WAITSTREAM
) && m
== 0 && (uio
->uio_resid
- delayed_copy_len
) > 0 &&
1632 !sosendallatonce(so
) && !nextrecord
) {
1633 if (so
->so_error
|| so
->so_state
& SS_CANTRCVMORE
)
1636 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
)
1637 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, flags
);
1638 if (sbwait(&so
->so_rcv
)) {
1643 * have to wait until after we get back from the sbwait to do the copy because
1644 * we will drop the funnel if we have enough data that has been delayed... by dropping
1645 * the funnel we open up a window allowing the netisr thread to process the incoming packets
1646 * and to change the state of this socket... we're issuing the sbwait because
1647 * the socket is empty and we're expecting the netisr thread to wake us up when more
1648 * packets arrive... if we allow that processing to happen and then sbwait, we
1649 * could stall forever with packets sitting in the socket if no further packets
1650 * arrive from the remote side.
1652 * we want to copy before we've collected all the data to satisfy this request to
1653 * allow the copy to overlap the incoming packet processing on an MP system
1655 if (delayed_copy_len
> sorecvmincopy
&& (delayed_copy_len
> (so
->so_rcv
.sb_hiwat
/ 2))) {
1657 error
= sodelayed_copy(uio
, &free_list
, &delayed_copy_len
);
1662 m
= so
->so_rcv
.sb_mb
;
1664 nextrecord
= m
->m_nextpkt
;
1669 if (m
&& pr
->pr_flags
& PR_ATOMIC
) {
1671 if (so
->so_options
& SO_DONTTRUNC
)
1672 flags
|= MSG_RCVMORE
;
1676 if ((flags
& MSG_PEEK
) == 0)
1677 (void) sbdroprecord(&so
->so_rcv
);
1682 if ((flags
& MSG_PEEK
) == 0) {
1684 so
->so_rcv
.sb_mb
= nextrecord
;
1685 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
)
1686 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, flags
);
1689 if ((so
->so_options
& SO_WANTMORE
) && so
->so_rcv
.sb_cc
> 0)
1690 flags
|= MSG_HAVEMORE
;
1692 if (delayed_copy_len
) {
1693 error
= sodelayed_copy(uio
, &free_list
, &delayed_copy_len
);
1699 m_freem_list((struct mbuf
*)free_list
);
1700 free_list
= (struct mbuf
*)0;
1703 postevent(so
, 0, EV_OOB
);
1705 if (orig_resid
== uio
->uio_resid
&& orig_resid
&&
1706 (flags
& MSG_EOR
) == 0 && (so
->so_state
& SS_CANTRCVMORE
) == 0) {
1707 sbunlock(&so
->so_rcv
);
1715 if (delayed_copy_len
) {
1716 error
= sodelayed_copy(uio
, &free_list
, &delayed_copy_len
);
1719 m_freem_list((struct mbuf
*)free_list
);
1721 sbunlock(&so
->so_rcv
);
1724 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
,
1735 int sodelayed_copy(struct uio
*uio
, struct mbuf
**free_list
, int *resid
)
1738 boolean_t dropped_funnel
= FALSE
;
1743 if (*resid
>= sorecvmincopy
) {
1744 dropped_funnel
= TRUE
;
1746 (void)thread_funnel_set(network_flock
, FALSE
);
1748 while (m
&& error
== 0) {
1750 error
= uiomove(mtod(m
, caddr_t
), (int)m
->m_len
, uio
);
1754 m_freem_list(*free_list
);
1756 *free_list
= (struct mbuf
*)NULL
;
1759 if (dropped_funnel
== TRUE
)
1760 (void)thread_funnel_set(network_flock
, TRUE
);
1768 register struct socket
*so
;
1771 register struct protosw
*pr
= so
->so_proto
;
1776 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_START
, 0,0,0,0,0);
1777 kp
= sotokextcb(so
);
1779 if (kp
->e_soif
&& kp
->e_soif
->sf_soshutdown
) {
1780 ret
= (*kp
->e_soif
->sf_soshutdown
)(so
, how
, kp
);
1782 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
1783 return((ret
== EJUSTRETURN
) ? 0 : ret
);
1789 if (how
!= SHUT_WR
) {
1791 postevent(so
, 0, EV_RCLOSED
);
1793 if (how
!= SHUT_RD
) {
1794 ret
= ((*pr
->pr_usrreqs
->pru_shutdown
)(so
));
1795 postevent(so
, 0, EV_WCLOSED
);
1796 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
1800 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
1806 register struct socket
*so
;
1808 register struct sockbuf
*sb
= &so
->so_rcv
;
1809 register struct protosw
*pr
= so
->so_proto
;
1810 register int s
, error
;
1814 kp
= sotokextcb(so
);
1816 if (kp
->e_soif
&& kp
->e_soif
->sf_sorflush
) {
1817 if ((*kp
->e_soif
->sf_sorflush
)(so
, kp
))
1823 sb
->sb_flags
|= SB_NOINTR
;
1824 (void) sblock(sb
, M_WAIT
);
1829 selthreadclear(&sb
->sb_sel
);
1832 bzero((caddr_t
)sb
, sizeof (*sb
));
1833 if (asb
.sb_flags
& SB_KNOTE
) {
1834 sb
->sb_sel
.si_note
= asb
.sb_sel
.si_note
;
1835 sb
->sb_flags
= SB_KNOTE
;
1838 if (pr
->pr_flags
& PR_RIGHTS
&& pr
->pr_domain
->dom_dispose
)
1839 (*pr
->pr_domain
->dom_dispose
)(asb
.sb_mb
);
1845 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1846 * an additional variant to handle the case where the option value needs
1847 * to be some kind of integer, but not a specific size.
1848 * In addition to their use here, these functions are also called by the
1849 * protocol-level pr_ctloutput() routines.
1852 sooptcopyin(sopt
, buf
, len
, minlen
)
1853 struct sockopt
*sopt
;
1861 * If the user gives us more than we wanted, we ignore it,
1862 * but if we don't get the minimum length the caller
1863 * wants, we return EINVAL. On success, sopt->sopt_valsize
1864 * is set to however much we actually retrieved.
1866 if ((valsize
= sopt
->sopt_valsize
) < minlen
)
1869 sopt
->sopt_valsize
= valsize
= len
;
1871 if (sopt
->sopt_p
!= 0)
1872 return (copyin(sopt
->sopt_val
, buf
, valsize
));
1874 bcopy(sopt
->sopt_val
, buf
, valsize
);
1881 struct sockopt
*sopt
;
1889 if (sopt
->sopt_dir
!= SOPT_SET
) {
1890 sopt
->sopt_dir
= SOPT_SET
;
1893 kp
= sotokextcb(so
);
1895 if (kp
->e_soif
&& kp
->e_soif
->sf_socontrol
) {
1896 error
= (*kp
->e_soif
->sf_socontrol
)(so
, sopt
, kp
);
1898 return((error
== EJUSTRETURN
) ? 0 : error
);
1904 if (sopt
->sopt_level
!= SOL_SOCKET
) {
1905 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
)
1906 return ((*so
->so_proto
->pr_ctloutput
)
1908 error
= ENOPROTOOPT
;
1910 switch (sopt
->sopt_name
) {
1912 error
= sooptcopyin(sopt
, &l
, sizeof l
, sizeof l
);
1916 so
->so_linger
= l
.l_linger
;
1918 so
->so_options
|= SO_LINGER
;
1920 so
->so_options
&= ~SO_LINGER
;
1926 case SO_USELOOPBACK
:
1935 case SO_WANTOOBFLAG
:
1937 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1942 so
->so_options
|= sopt
->sopt_name
;
1944 so
->so_options
&= ~sopt
->sopt_name
;
1951 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1957 * Values < 1 make no sense for any of these
1958 * options, so disallow them.
1965 switch (sopt
->sopt_name
) {
1968 if (sbreserve(sopt
->sopt_name
== SO_SNDBUF
?
1969 &so
->so_snd
: &so
->so_rcv
,
1970 (u_long
) optval
) == 0) {
1977 * Make sure the low-water is never greater than
1981 so
->so_snd
.sb_lowat
=
1982 (optval
> so
->so_snd
.sb_hiwat
) ?
1983 so
->so_snd
.sb_hiwat
: optval
;
1986 so
->so_rcv
.sb_lowat
=
1987 (optval
> so
->so_rcv
.sb_hiwat
) ?
1988 so
->so_rcv
.sb_hiwat
: optval
;
1995 error
= sooptcopyin(sopt
, &tv
, sizeof tv
,
2000 /* assert(hz > 0); */
2001 if (tv
.tv_sec
< 0 || tv
.tv_sec
> SHRT_MAX
/ hz
||
2002 tv
.tv_usec
< 0 || tv
.tv_usec
>= 1000000) {
2006 /* assert(tick > 0); */
2007 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
2009 long tmp
= (u_long
)(tv
.tv_sec
* hz
) + tv
.tv_usec
/ tick
;
2010 if (tmp
> SHRT_MAX
) {
2017 switch (sopt
->sopt_name
) {
2019 so
->so_snd
.sb_timeo
= val
;
2022 so
->so_rcv
.sb_timeo
= val
;
2030 struct NFDescriptor
*nf1
, *nf2
= NULL
;
2032 error
= sooptcopyin(sopt
, &nke
,
2033 sizeof nke
, sizeof nke
);
2037 error
= nke_insert(so
, &nke
);
2042 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2047 so
->so_flags
|= SOF_NOSIGPIPE
;
2049 so
->so_flags
&= ~SOF_NOSIGPIPE
;
2054 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2059 so
->so_flags
|= SOF_NOADDRAVAIL
;
2061 so
->so_flags
&= ~SOF_NOADDRAVAIL
;
2066 error
= ENOPROTOOPT
;
2069 if (error
== 0 && so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
2070 (void) ((*so
->so_proto
->pr_ctloutput
)
2078 /* Helper routine for getsockopt */
2080 sooptcopyout(sopt
, buf
, len
)
2081 struct sockopt
*sopt
;
2091 * Documented get behavior is that we always return a value,
2092 * possibly truncated to fit in the user's buffer.
2093 * Traditional behavior is that we always tell the user
2094 * precisely how much we copied, rather than something useful
2095 * like the total amount we had available for her.
2096 * Note that this interface is not idempotent; the entire answer must
2097 * generated ahead of time.
2099 valsize
= min(len
, sopt
->sopt_valsize
);
2100 sopt
->sopt_valsize
= valsize
;
2101 if (sopt
->sopt_val
!= 0) {
2102 if (sopt
->sopt_p
!= 0)
2103 error
= copyout(buf
, sopt
->sopt_val
, valsize
);
2105 bcopy(buf
, sopt
->sopt_val
, valsize
);
2113 struct sockopt
*sopt
;
2121 if (sopt
->sopt_dir
!= SOPT_GET
) {
2122 sopt
->sopt_dir
= SOPT_GET
;
2125 kp
= sotokextcb(so
);
2127 if (kp
->e_soif
&& kp
->e_soif
->sf_socontrol
) {
2128 error
= (*kp
->e_soif
->sf_socontrol
)(so
, sopt
, kp
);
2130 return((error
== EJUSTRETURN
) ? 0 : error
);
2136 if (sopt
->sopt_level
!= SOL_SOCKET
) {
2137 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
2138 return ((*so
->so_proto
->pr_ctloutput
)
2141 return (ENOPROTOOPT
);
2143 switch (sopt
->sopt_name
) {
2145 l
.l_onoff
= so
->so_options
& SO_LINGER
;
2146 l
.l_linger
= so
->so_linger
;
2147 error
= sooptcopyout(sopt
, &l
, sizeof l
);
2150 case SO_USELOOPBACK
:
2162 case SO_WANTOOBFLAG
:
2164 optval
= so
->so_options
& sopt
->sopt_name
;
2166 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2170 optval
= so
->so_type
;
2180 m1
= so
->so_rcv
.sb_mb
;
2181 if (so
->so_proto
->pr_flags
& PR_ATOMIC
)
2184 kprintf("SKT CC: %d\n", so
->so_rcv
.sb_cc
);
2187 if (m1
->m_type
== MT_DATA
)
2188 pkt_total
+= m1
->m_len
;
2190 kprintf("CNT: %d/%d\n", m1
->m_len
, pkt_total
);
2196 optval
= so
->so_rcv
.sb_cc
;
2198 kprintf("RTN: %d\n", optval
);
2204 optval
= so
->so_error
;
2209 optval
= so
->so_snd
.sb_hiwat
;
2213 optval
= so
->so_rcv
.sb_hiwat
;
2217 optval
= so
->so_snd
.sb_lowat
;
2221 optval
= so
->so_rcv
.sb_lowat
;
2226 optval
= (sopt
->sopt_name
== SO_SNDTIMEO
?
2227 so
->so_snd
.sb_timeo
: so
->so_rcv
.sb_timeo
);
2229 tv
.tv_sec
= optval
/ hz
;
2230 tv
.tv_usec
= (optval
% hz
) * tick
;
2231 error
= sooptcopyout(sopt
, &tv
, sizeof tv
);
2235 optval
= (so
->so_flags
& SOF_NOSIGPIPE
);
2239 optval
= (so
->so_flags
& SOF_NOADDRAVAIL
);
2243 error
= ENOPROTOOPT
;
2252 * Network filter support
2254 /* Run the list of filters, creating extension control blocks */
2255 sfilter_init(register struct socket
*so
)
2256 { struct kextcb
*kp
, **kpp
;
2257 struct protosw
*prp
;
2258 struct NFDescriptor
*nfp
;
2261 nfp
= prp
->pr_sfilter
.tqh_first
; /* non-null */
2265 { MALLOC(kp
, struct kextcb
*, sizeof(*kp
),
2268 return(ENOBUFS
); /* so_free will clean up */
2274 kp
->e_soif
= nfp
->nf_soif
;
2275 kp
->e_sout
= nfp
->nf_soutil
;
2277 * Ignore return value for create
2278 * Everyone gets a chance at startup
2280 if (kp
->e_soif
&& kp
->e_soif
->sf_socreate
)
2281 (*kp
->e_soif
->sf_socreate
)(so
, prp
, kp
);
2282 nfp
= nfp
->nf_next
.tqe_next
;
2288 * Run the list of filters, freeing extension control blocks
2289 * Assumes the soif/soutil blocks have been handled.
2291 sfilter_term(struct socket
*so
)
2292 { struct kextcb
*kp
, *kp1
;
2298 * Ignore return code on termination; everyone must
2301 if (kp
->e_soif
&& kp
->e_soif
->sf_sofree
)
2302 kp
->e_soif
->sf_sofree(so
, kp
);
2310 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
2312 soopt_getm(struct sockopt
*sopt
, struct mbuf
**mp
)
2314 struct mbuf
*m
, *m_prev
;
2315 int sopt_size
= sopt
->sopt_valsize
;
2317 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
2320 if (sopt_size
> MLEN
) {
2321 MCLGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
);
2322 if ((m
->m_flags
& M_EXT
) == 0) {
2326 m
->m_len
= min(MCLBYTES
, sopt_size
);
2328 m
->m_len
= min(MLEN
, sopt_size
);
2330 sopt_size
-= m
->m_len
;
2335 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
2340 if (sopt_size
> MLEN
) {
2341 MCLGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
);
2342 if ((m
->m_flags
& M_EXT
) == 0) {
2346 m
->m_len
= min(MCLBYTES
, sopt_size
);
2348 m
->m_len
= min(MLEN
, sopt_size
);
2350 sopt_size
-= m
->m_len
;
2357 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2359 soopt_mcopyin(struct sockopt
*sopt
, struct mbuf
*m
)
2361 struct mbuf
*m0
= m
;
2363 if (sopt
->sopt_val
== NULL
)
2365 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
2366 if (sopt
->sopt_p
!= NULL
) {
2369 error
= copyin(sopt
->sopt_val
, mtod(m
, char *),
2376 bcopy(sopt
->sopt_val
, mtod(m
, char *), m
->m_len
);
2377 sopt
->sopt_valsize
-= m
->m_len
;
2378 (caddr_t
)sopt
->sopt_val
+= m
->m_len
;
2381 if (m
!= NULL
) /* should be allocated enoughly at ip6_sooptmcopyin() */
2382 panic("soopt_mcopyin");
2386 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2388 soopt_mcopyout(struct sockopt
*sopt
, struct mbuf
*m
)
2390 struct mbuf
*m0
= m
;
2393 if (sopt
->sopt_val
== NULL
)
2395 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
2396 if (sopt
->sopt_p
!= NULL
) {
2399 error
= copyout(mtod(m
, char *), sopt
->sopt_val
,
2406 bcopy(mtod(m
, char *), sopt
->sopt_val
, m
->m_len
);
2407 sopt
->sopt_valsize
-= m
->m_len
;
2408 (caddr_t
)sopt
->sopt_val
+= m
->m_len
;
2409 valsize
+= m
->m_len
;
2413 /* enough soopt buffer should be given from user-land */
2417 sopt
->sopt_valsize
= valsize
;
2423 register struct socket
*so
;
2428 kp
= sotokextcb(so
);
2430 if (kp
->e_soif
&& kp
->e_soif
->sf_sohasoutofband
) {
2431 if ((*kp
->e_soif
->sf_sohasoutofband
)(so
, kp
))
2436 if (so
->so_pgid
< 0)
2437 gsignal(-so
->so_pgid
, SIGURG
);
2438 else if (so
->so_pgid
> 0 && (p
= pfind(so
->so_pgid
)) != 0)
2440 selwakeup(&so
->so_rcv
.sb_sel
);
2444 sopoll(struct socket
*so
, int events
, struct ucred
*cred
, void * wql
)
2446 struct proc
*p
= current_proc();
2450 if (events
& (POLLIN
| POLLRDNORM
))
2452 revents
|= events
& (POLLIN
| POLLRDNORM
);
2454 if (events
& (POLLOUT
| POLLWRNORM
))
2455 if (sowriteable(so
))
2456 revents
|= events
& (POLLOUT
| POLLWRNORM
);
2458 if (events
& (POLLPRI
| POLLRDBAND
))
2459 if (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
))
2460 revents
|= events
& (POLLPRI
| POLLRDBAND
);
2463 if (events
& (POLLIN
| POLLPRI
| POLLRDNORM
| POLLRDBAND
)) {
2464 /* Darwin sets the flag first, BSD calls selrecord first */
2465 so
->so_rcv
.sb_flags
|= SB_SEL
;
2466 selrecord(p
, &so
->so_rcv
.sb_sel
, wql
);
2469 if (events
& (POLLOUT
| POLLWRNORM
)) {
2470 /* Darwin sets the flag first, BSD calls selrecord first */
2471 so
->so_snd
.sb_flags
|= SB_SEL
;
2472 selrecord(p
, &so
->so_snd
.sb_sel
, wql
);
2482 soo_kqfilter(struct file
*fp
, struct knote
*kn
, struct proc
*p
)
2484 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2488 switch (kn
->kn_filter
) {
2490 if (so
->so_options
& SO_ACCEPTCONN
)
2491 kn
->kn_fop
= &solisten_filtops
;
2493 kn
->kn_fop
= &soread_filtops
;
2497 kn
->kn_fop
= &sowrite_filtops
;
2504 if (sb
->sb_sel
.si_flags
& SI_INITED
)
2508 if (KNOTE_ATTACH(&sb
->sb_sel
.si_note
, kn
))
2509 sb
->sb_flags
|= SB_KNOTE
;
2515 filt_sordetach(struct knote
*kn
)
2517 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2520 if (so
->so_rcv
.sb_flags
& SB_KNOTE
&&
2521 !(so
->so_rcv
.sb_sel
.si_flags
& SI_INITED
))
2522 if (KNOTE_DETACH(&so
->so_rcv
.sb_sel
.si_note
, kn
))
2523 so
->so_rcv
.sb_flags
&= ~SB_KNOTE
;
2529 filt_soread(struct knote
*kn
, long hint
)
2531 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2533 kn
->kn_data
= so
->so_rcv
.sb_cc
;
2534 if (so
->so_state
& SS_CANTRCVMORE
) {
2535 kn
->kn_flags
|= EV_EOF
;
2536 kn
->kn_fflags
= so
->so_error
;
2539 if (so
->so_error
) /* temporary udp error */
2541 if (kn
->kn_sfflags
& NOTE_LOWAT
)
2542 return (kn
->kn_data
>= kn
->kn_sdata
);
2543 return (kn
->kn_data
>= so
->so_rcv
.sb_lowat
);
2547 filt_sowdetach(struct knote
*kn
)
2549 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2552 if(so
->so_snd
.sb_flags
& SB_KNOTE
&&
2553 !(so
->so_snd
.sb_sel
.si_flags
& SI_INITED
))
2554 if (KNOTE_DETACH(&so
->so_snd
.sb_sel
.si_note
, kn
))
2555 so
->so_snd
.sb_flags
&= ~SB_KNOTE
;
2561 filt_sowrite(struct knote
*kn
, long hint
)
2563 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2565 kn
->kn_data
= sbspace(&so
->so_snd
);
2566 if (so
->so_state
& SS_CANTSENDMORE
) {
2567 kn
->kn_flags
|= EV_EOF
;
2568 kn
->kn_fflags
= so
->so_error
;
2571 if (so
->so_error
) /* temporary udp error */
2573 if (((so
->so_state
& SS_ISCONNECTED
) == 0) &&
2574 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
))
2576 if (kn
->kn_sfflags
& NOTE_LOWAT
)
2577 return (kn
->kn_data
>= kn
->kn_sdata
);
2578 return (kn
->kn_data
>= so
->so_snd
.sb_lowat
);
2583 filt_solisten(struct knote
*kn
, long hint
)
2585 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2587 kn
->kn_data
= so
->so_qlen
;
2588 return (! TAILQ_EMPTY(&so
->so_comp
));