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 ((*psa
== 0) && (flags
& MSG_NEEDSA
)) {
1431 error
= EWOULDBLOCK
;
1435 if (flags
& MSG_PEEK
) {
1438 sbfree(&so
->so_rcv
, m
);
1439 MFREE(m
, so
->so_rcv
.sb_mb
);
1440 m
= so
->so_rcv
.sb_mb
;
1443 while (m
&& m
->m_type
== MT_CONTROL
&& error
== 0) {
1444 if (flags
& MSG_PEEK
) {
1446 *controlp
= m_copy(m
, 0, m
->m_len
);
1449 sbfree(&so
->so_rcv
, m
);
1451 if (pr
->pr_domain
->dom_externalize
&&
1452 mtod(m
, struct cmsghdr
*)->cmsg_type
==
1454 error
= (*pr
->pr_domain
->dom_externalize
)(m
);
1456 so
->so_rcv
.sb_mb
= m
->m_next
;
1458 m
= so
->so_rcv
.sb_mb
;
1460 MFREE(m
, so
->so_rcv
.sb_mb
);
1461 m
= so
->so_rcv
.sb_mb
;
1466 controlp
= &(*controlp
)->m_next
;
1470 if ((flags
& MSG_PEEK
) == 0)
1471 m
->m_nextpkt
= nextrecord
;
1473 if (type
== MT_OOBDATA
)
1479 if (!(flags
& MSG_PEEK
) && uio
->uio_resid
> sorecvmincopy
)
1487 while (m
&& (uio
->uio_resid
- delayed_copy_len
) > 0 && error
== 0) {
1488 if (m
->m_type
== MT_OOBDATA
) {
1489 if (type
!= MT_OOBDATA
)
1491 } else if (type
== MT_OOBDATA
)
1495 * This assertion needs rework. The trouble is Appletalk is uses many
1496 * mbuf types (NOT listed in mbuf.h!) which will trigger this panic.
1497 * For now just remove the assertion... CSM 9/98
1500 KASSERT(m
->m_type
== MT_DATA
|| m
->m_type
== MT_HEADER
,
1504 * Make sure to allways set MSG_OOB event when getting
1505 * out of band data inline.
1507 if ((so
->so_options
& SO_WANTOOBFLAG
) != 0 &&
1508 (so
->so_options
& SO_OOBINLINE
) != 0 &&
1509 (so
->so_state
& SS_RCVATMARK
) != 0) {
1513 so
->so_state
&= ~SS_RCVATMARK
;
1514 len
= uio
->uio_resid
- delayed_copy_len
;
1515 if (so
->so_oobmark
&& len
> so
->so_oobmark
- offset
)
1516 len
= so
->so_oobmark
- offset
;
1517 if (len
> m
->m_len
- moff
)
1518 len
= m
->m_len
- moff
;
1520 * If mp is set, just pass back the mbufs.
1521 * Otherwise copy them out via the uio, then free.
1522 * Sockbuf must be consistent here (points to current mbuf,
1523 * it points to next record) when we drop priority;
1524 * we must note any additions to the sockbuf when we
1525 * block interrupts again.
1528 if (can_delay
&& len
== m
->m_len
) {
1530 * only delay the copy if we're consuming the
1531 * mbuf and we're NOT in MSG_PEEK mode
1532 * and we have enough data to make it worthwile
1533 * to drop and retake the funnel... can_delay
1534 * reflects the state of the 2 latter constraints
1535 * moff should always be zero in these cases
1537 delayed_copy_len
+= len
;
1541 if (delayed_copy_len
) {
1542 error
= sodelayed_copy(uio
, &free_list
, &delayed_copy_len
);
1548 if (m
!= so
->so_rcv
.sb_mb
) {
1550 * can only get here if MSG_PEEK is not set
1551 * therefore, m should point at the head of the rcv queue...
1552 * if it doesn't, it means something drastically changed
1553 * while we were out from behind the funnel in sodelayed_copy...
1554 * perhaps a RST on the stream... in any event, the stream has
1555 * been interrupted... it's probably best just to return
1556 * whatever data we've moved and let the caller sort it out...
1561 error
= uiomove(mtod(m
, caddr_t
) + moff
, (int)len
, uio
);
1568 uio
->uio_resid
-= len
;
1570 if (len
== m
->m_len
- moff
) {
1571 if (m
->m_flags
& M_EOR
)
1573 if (flags
& MSG_PEEK
) {
1577 nextrecord
= m
->m_nextpkt
;
1578 sbfree(&so
->so_rcv
, m
);
1583 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
1584 *mp
= (struct mbuf
*)0;
1587 if (free_list
== NULL
)
1592 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
1596 m
->m_nextpkt
= nextrecord
;
1599 if (flags
& MSG_PEEK
)
1603 *mp
= m_copym(m
, 0, len
, M_WAIT
);
1606 so
->so_rcv
.sb_cc
-= len
;
1609 if (so
->so_oobmark
) {
1610 if ((flags
& MSG_PEEK
) == 0) {
1611 so
->so_oobmark
-= len
;
1612 if (so
->so_oobmark
== 0) {
1613 so
->so_state
|= SS_RCVATMARK
;
1615 * delay posting the actual event until after
1616 * any delayed copy processing has finished
1623 if (offset
== so
->so_oobmark
)
1627 if (flags
& MSG_EOR
)
1630 * If the MSG_WAITALL or MSG_WAITSTREAM flag is set (for non-atomic socket),
1631 * we must not quit until "uio->uio_resid == 0" or an error
1632 * termination. If a signal/timeout occurs, return
1633 * with a short count but without error.
1634 * Keep sockbuf locked against other readers.
1636 while (flags
& (MSG_WAITALL
|MSG_WAITSTREAM
) && m
== 0 && (uio
->uio_resid
- delayed_copy_len
) > 0 &&
1637 !sosendallatonce(so
) && !nextrecord
) {
1638 if (so
->so_error
|| so
->so_state
& SS_CANTRCVMORE
)
1641 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
)
1642 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, flags
);
1643 if (sbwait(&so
->so_rcv
)) {
1648 * have to wait until after we get back from the sbwait to do the copy because
1649 * we will drop the funnel if we have enough data that has been delayed... by dropping
1650 * the funnel we open up a window allowing the netisr thread to process the incoming packets
1651 * and to change the state of this socket... we're issuing the sbwait because
1652 * the socket is empty and we're expecting the netisr thread to wake us up when more
1653 * packets arrive... if we allow that processing to happen and then sbwait, we
1654 * could stall forever with packets sitting in the socket if no further packets
1655 * arrive from the remote side.
1657 * we want to copy before we've collected all the data to satisfy this request to
1658 * allow the copy to overlap the incoming packet processing on an MP system
1660 if (delayed_copy_len
> sorecvmincopy
&& (delayed_copy_len
> (so
->so_rcv
.sb_hiwat
/ 2))) {
1662 error
= sodelayed_copy(uio
, &free_list
, &delayed_copy_len
);
1667 m
= so
->so_rcv
.sb_mb
;
1669 nextrecord
= m
->m_nextpkt
;
1674 if (m
&& pr
->pr_flags
& PR_ATOMIC
) {
1676 if (so
->so_options
& SO_DONTTRUNC
)
1677 flags
|= MSG_RCVMORE
;
1681 if ((flags
& MSG_PEEK
) == 0)
1682 (void) sbdroprecord(&so
->so_rcv
);
1687 if ((flags
& MSG_PEEK
) == 0) {
1689 so
->so_rcv
.sb_mb
= nextrecord
;
1690 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
)
1691 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, flags
);
1694 if ((so
->so_options
& SO_WANTMORE
) && so
->so_rcv
.sb_cc
> 0)
1695 flags
|= MSG_HAVEMORE
;
1697 if (delayed_copy_len
) {
1698 error
= sodelayed_copy(uio
, &free_list
, &delayed_copy_len
);
1704 m_freem_list((struct mbuf
*)free_list
);
1705 free_list
= (struct mbuf
*)0;
1708 postevent(so
, 0, EV_OOB
);
1710 if (orig_resid
== uio
->uio_resid
&& orig_resid
&&
1711 (flags
& MSG_EOR
) == 0 && (so
->so_state
& SS_CANTRCVMORE
) == 0) {
1712 sbunlock(&so
->so_rcv
);
1720 if (delayed_copy_len
) {
1721 error
= sodelayed_copy(uio
, &free_list
, &delayed_copy_len
);
1724 m_freem_list((struct mbuf
*)free_list
);
1726 sbunlock(&so
->so_rcv
);
1729 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
,
1740 int sodelayed_copy(struct uio
*uio
, struct mbuf
**free_list
, int *resid
)
1743 boolean_t dropped_funnel
= FALSE
;
1748 if (*resid
>= sorecvmincopy
) {
1749 dropped_funnel
= TRUE
;
1751 (void)thread_funnel_set(network_flock
, FALSE
);
1753 while (m
&& error
== 0) {
1755 error
= uiomove(mtod(m
, caddr_t
), (int)m
->m_len
, uio
);
1759 m_freem_list(*free_list
);
1761 *free_list
= (struct mbuf
*)NULL
;
1764 if (dropped_funnel
== TRUE
)
1765 (void)thread_funnel_set(network_flock
, TRUE
);
1773 register struct socket
*so
;
1776 register struct protosw
*pr
= so
->so_proto
;
1781 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_START
, 0,0,0,0,0);
1782 kp
= sotokextcb(so
);
1784 if (kp
->e_soif
&& kp
->e_soif
->sf_soshutdown
) {
1785 ret
= (*kp
->e_soif
->sf_soshutdown
)(so
, how
, kp
);
1787 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
1788 return((ret
== EJUSTRETURN
) ? 0 : ret
);
1794 if (how
!= SHUT_WR
) {
1796 postevent(so
, 0, EV_RCLOSED
);
1798 if (how
!= SHUT_RD
) {
1799 ret
= ((*pr
->pr_usrreqs
->pru_shutdown
)(so
));
1800 postevent(so
, 0, EV_WCLOSED
);
1801 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
1805 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
1811 register struct socket
*so
;
1813 register struct sockbuf
*sb
= &so
->so_rcv
;
1814 register struct protosw
*pr
= so
->so_proto
;
1815 register int s
, error
;
1819 kp
= sotokextcb(so
);
1821 if (kp
->e_soif
&& kp
->e_soif
->sf_sorflush
) {
1822 if ((*kp
->e_soif
->sf_sorflush
)(so
, kp
))
1828 sb
->sb_flags
|= SB_NOINTR
;
1829 (void) sblock(sb
, M_WAIT
);
1834 selthreadclear(&sb
->sb_sel
);
1837 bzero((caddr_t
)sb
, sizeof (*sb
));
1838 if (asb
.sb_flags
& SB_KNOTE
) {
1839 sb
->sb_sel
.si_note
= asb
.sb_sel
.si_note
;
1840 sb
->sb_flags
= SB_KNOTE
;
1843 if (pr
->pr_flags
& PR_RIGHTS
&& pr
->pr_domain
->dom_dispose
)
1844 (*pr
->pr_domain
->dom_dispose
)(asb
.sb_mb
);
1850 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1851 * an additional variant to handle the case where the option value needs
1852 * to be some kind of integer, but not a specific size.
1853 * In addition to their use here, these functions are also called by the
1854 * protocol-level pr_ctloutput() routines.
1857 sooptcopyin(sopt
, buf
, len
, minlen
)
1858 struct sockopt
*sopt
;
1866 * If the user gives us more than we wanted, we ignore it,
1867 * but if we don't get the minimum length the caller
1868 * wants, we return EINVAL. On success, sopt->sopt_valsize
1869 * is set to however much we actually retrieved.
1871 if ((valsize
= sopt
->sopt_valsize
) < minlen
)
1874 sopt
->sopt_valsize
= valsize
= len
;
1876 if (sopt
->sopt_p
!= 0)
1877 return (copyin(sopt
->sopt_val
, buf
, valsize
));
1879 bcopy(sopt
->sopt_val
, buf
, valsize
);
1886 struct sockopt
*sopt
;
1894 if (sopt
->sopt_dir
!= SOPT_SET
) {
1895 sopt
->sopt_dir
= SOPT_SET
;
1898 kp
= sotokextcb(so
);
1900 if (kp
->e_soif
&& kp
->e_soif
->sf_socontrol
) {
1901 error
= (*kp
->e_soif
->sf_socontrol
)(so
, sopt
, kp
);
1903 return((error
== EJUSTRETURN
) ? 0 : error
);
1909 if (sopt
->sopt_level
!= SOL_SOCKET
) {
1910 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
)
1911 return ((*so
->so_proto
->pr_ctloutput
)
1913 error
= ENOPROTOOPT
;
1915 switch (sopt
->sopt_name
) {
1917 error
= sooptcopyin(sopt
, &l
, sizeof l
, sizeof l
);
1921 so
->so_linger
= l
.l_linger
;
1923 so
->so_options
|= SO_LINGER
;
1925 so
->so_options
&= ~SO_LINGER
;
1931 case SO_USELOOPBACK
:
1940 case SO_WANTOOBFLAG
:
1942 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1947 so
->so_options
|= sopt
->sopt_name
;
1949 so
->so_options
&= ~sopt
->sopt_name
;
1956 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1962 * Values < 1 make no sense for any of these
1963 * options, so disallow them.
1970 switch (sopt
->sopt_name
) {
1973 if (sbreserve(sopt
->sopt_name
== SO_SNDBUF
?
1974 &so
->so_snd
: &so
->so_rcv
,
1975 (u_long
) optval
) == 0) {
1982 * Make sure the low-water is never greater than
1986 so
->so_snd
.sb_lowat
=
1987 (optval
> so
->so_snd
.sb_hiwat
) ?
1988 so
->so_snd
.sb_hiwat
: optval
;
1991 so
->so_rcv
.sb_lowat
=
1992 (optval
> so
->so_rcv
.sb_hiwat
) ?
1993 so
->so_rcv
.sb_hiwat
: optval
;
2000 error
= sooptcopyin(sopt
, &tv
, sizeof tv
,
2005 /* assert(hz > 0); */
2006 if (tv
.tv_sec
< 0 || tv
.tv_sec
> SHRT_MAX
/ hz
||
2007 tv
.tv_usec
< 0 || tv
.tv_usec
>= 1000000) {
2011 /* assert(tick > 0); */
2012 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
2014 long tmp
= (u_long
)(tv
.tv_sec
* hz
) + tv
.tv_usec
/ tick
;
2015 if (tmp
> SHRT_MAX
) {
2022 switch (sopt
->sopt_name
) {
2024 so
->so_snd
.sb_timeo
= val
;
2027 so
->so_rcv
.sb_timeo
= val
;
2035 struct NFDescriptor
*nf1
, *nf2
= NULL
;
2037 error
= sooptcopyin(sopt
, &nke
,
2038 sizeof nke
, sizeof nke
);
2042 error
= nke_insert(so
, &nke
);
2047 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2052 so
->so_flags
|= SOF_NOSIGPIPE
;
2054 so
->so_flags
&= ~SOF_NOSIGPIPE
;
2059 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2064 so
->so_flags
|= SOF_NOADDRAVAIL
;
2066 so
->so_flags
&= ~SOF_NOADDRAVAIL
;
2071 error
= ENOPROTOOPT
;
2074 if (error
== 0 && so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
2075 (void) ((*so
->so_proto
->pr_ctloutput
)
2083 /* Helper routine for getsockopt */
2085 sooptcopyout(sopt
, buf
, len
)
2086 struct sockopt
*sopt
;
2096 * Documented get behavior is that we always return a value,
2097 * possibly truncated to fit in the user's buffer.
2098 * Traditional behavior is that we always tell the user
2099 * precisely how much we copied, rather than something useful
2100 * like the total amount we had available for her.
2101 * Note that this interface is not idempotent; the entire answer must
2102 * generated ahead of time.
2104 valsize
= min(len
, sopt
->sopt_valsize
);
2105 sopt
->sopt_valsize
= valsize
;
2106 if (sopt
->sopt_val
!= 0) {
2107 if (sopt
->sopt_p
!= 0)
2108 error
= copyout(buf
, sopt
->sopt_val
, valsize
);
2110 bcopy(buf
, sopt
->sopt_val
, valsize
);
2118 struct sockopt
*sopt
;
2126 if (sopt
->sopt_dir
!= SOPT_GET
) {
2127 sopt
->sopt_dir
= SOPT_GET
;
2130 kp
= sotokextcb(so
);
2132 if (kp
->e_soif
&& kp
->e_soif
->sf_socontrol
) {
2133 error
= (*kp
->e_soif
->sf_socontrol
)(so
, sopt
, kp
);
2135 return((error
== EJUSTRETURN
) ? 0 : error
);
2141 if (sopt
->sopt_level
!= SOL_SOCKET
) {
2142 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
2143 return ((*so
->so_proto
->pr_ctloutput
)
2146 return (ENOPROTOOPT
);
2148 switch (sopt
->sopt_name
) {
2150 l
.l_onoff
= so
->so_options
& SO_LINGER
;
2151 l
.l_linger
= so
->so_linger
;
2152 error
= sooptcopyout(sopt
, &l
, sizeof l
);
2155 case SO_USELOOPBACK
:
2167 case SO_WANTOOBFLAG
:
2169 optval
= so
->so_options
& sopt
->sopt_name
;
2171 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2175 optval
= so
->so_type
;
2185 m1
= so
->so_rcv
.sb_mb
;
2186 if (so
->so_proto
->pr_flags
& PR_ATOMIC
)
2189 kprintf("SKT CC: %d\n", so
->so_rcv
.sb_cc
);
2192 if (m1
->m_type
== MT_DATA
)
2193 pkt_total
+= m1
->m_len
;
2195 kprintf("CNT: %d/%d\n", m1
->m_len
, pkt_total
);
2201 optval
= so
->so_rcv
.sb_cc
;
2203 kprintf("RTN: %d\n", optval
);
2209 optval
= so
->so_error
;
2214 optval
= so
->so_snd
.sb_hiwat
;
2218 optval
= so
->so_rcv
.sb_hiwat
;
2222 optval
= so
->so_snd
.sb_lowat
;
2226 optval
= so
->so_rcv
.sb_lowat
;
2231 optval
= (sopt
->sopt_name
== SO_SNDTIMEO
?
2232 so
->so_snd
.sb_timeo
: so
->so_rcv
.sb_timeo
);
2234 tv
.tv_sec
= optval
/ hz
;
2235 tv
.tv_usec
= (optval
% hz
) * tick
;
2236 error
= sooptcopyout(sopt
, &tv
, sizeof tv
);
2240 optval
= (so
->so_flags
& SOF_NOSIGPIPE
);
2244 optval
= (so
->so_flags
& SOF_NOADDRAVAIL
);
2248 error
= ENOPROTOOPT
;
2257 * Network filter support
2259 /* Run the list of filters, creating extension control blocks */
2260 sfilter_init(register struct socket
*so
)
2261 { struct kextcb
*kp
, **kpp
;
2262 struct protosw
*prp
;
2263 struct NFDescriptor
*nfp
;
2266 nfp
= prp
->pr_sfilter
.tqh_first
; /* non-null */
2270 { MALLOC(kp
, struct kextcb
*, sizeof(*kp
),
2273 return(ENOBUFS
); /* so_free will clean up */
2279 kp
->e_soif
= nfp
->nf_soif
;
2280 kp
->e_sout
= nfp
->nf_soutil
;
2282 * Ignore return value for create
2283 * Everyone gets a chance at startup
2285 if (kp
->e_soif
&& kp
->e_soif
->sf_socreate
)
2286 (*kp
->e_soif
->sf_socreate
)(so
, prp
, kp
);
2287 nfp
= nfp
->nf_next
.tqe_next
;
2293 * Run the list of filters, freeing extension control blocks
2294 * Assumes the soif/soutil blocks have been handled.
2296 sfilter_term(struct socket
*so
)
2297 { struct kextcb
*kp
, *kp1
;
2303 * Ignore return code on termination; everyone must
2306 if (kp
->e_soif
&& kp
->e_soif
->sf_sofree
)
2307 kp
->e_soif
->sf_sofree(so
, kp
);
2315 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
2317 soopt_getm(struct sockopt
*sopt
, struct mbuf
**mp
)
2319 struct mbuf
*m
, *m_prev
;
2320 int sopt_size
= sopt
->sopt_valsize
;
2322 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
2325 if (sopt_size
> MLEN
) {
2326 MCLGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
);
2327 if ((m
->m_flags
& M_EXT
) == 0) {
2331 m
->m_len
= min(MCLBYTES
, sopt_size
);
2333 m
->m_len
= min(MLEN
, sopt_size
);
2335 sopt_size
-= m
->m_len
;
2340 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
2345 if (sopt_size
> MLEN
) {
2346 MCLGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
);
2347 if ((m
->m_flags
& M_EXT
) == 0) {
2351 m
->m_len
= min(MCLBYTES
, sopt_size
);
2353 m
->m_len
= min(MLEN
, sopt_size
);
2355 sopt_size
-= m
->m_len
;
2362 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2364 soopt_mcopyin(struct sockopt
*sopt
, struct mbuf
*m
)
2366 struct mbuf
*m0
= m
;
2368 if (sopt
->sopt_val
== NULL
)
2370 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
2371 if (sopt
->sopt_p
!= NULL
) {
2374 error
= copyin(sopt
->sopt_val
, mtod(m
, char *),
2381 bcopy(sopt
->sopt_val
, mtod(m
, char *), m
->m_len
);
2382 sopt
->sopt_valsize
-= m
->m_len
;
2383 (caddr_t
)sopt
->sopt_val
+= m
->m_len
;
2386 if (m
!= NULL
) /* should be allocated enoughly at ip6_sooptmcopyin() */
2387 panic("soopt_mcopyin");
2391 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2393 soopt_mcopyout(struct sockopt
*sopt
, struct mbuf
*m
)
2395 struct mbuf
*m0
= m
;
2398 if (sopt
->sopt_val
== NULL
)
2400 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
2401 if (sopt
->sopt_p
!= NULL
) {
2404 error
= copyout(mtod(m
, char *), sopt
->sopt_val
,
2411 bcopy(mtod(m
, char *), sopt
->sopt_val
, m
->m_len
);
2412 sopt
->sopt_valsize
-= m
->m_len
;
2413 (caddr_t
)sopt
->sopt_val
+= m
->m_len
;
2414 valsize
+= m
->m_len
;
2418 /* enough soopt buffer should be given from user-land */
2422 sopt
->sopt_valsize
= valsize
;
2428 register struct socket
*so
;
2433 kp
= sotokextcb(so
);
2435 if (kp
->e_soif
&& kp
->e_soif
->sf_sohasoutofband
) {
2436 if ((*kp
->e_soif
->sf_sohasoutofband
)(so
, kp
))
2441 if (so
->so_pgid
< 0)
2442 gsignal(-so
->so_pgid
, SIGURG
);
2443 else if (so
->so_pgid
> 0 && (p
= pfind(so
->so_pgid
)) != 0)
2445 selwakeup(&so
->so_rcv
.sb_sel
);
2449 sopoll(struct socket
*so
, int events
, struct ucred
*cred
, void * wql
)
2451 struct proc
*p
= current_proc();
2455 if (events
& (POLLIN
| POLLRDNORM
))
2457 revents
|= events
& (POLLIN
| POLLRDNORM
);
2459 if (events
& (POLLOUT
| POLLWRNORM
))
2460 if (sowriteable(so
))
2461 revents
|= events
& (POLLOUT
| POLLWRNORM
);
2463 if (events
& (POLLPRI
| POLLRDBAND
))
2464 if (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
))
2465 revents
|= events
& (POLLPRI
| POLLRDBAND
);
2468 if (events
& (POLLIN
| POLLPRI
| POLLRDNORM
| POLLRDBAND
)) {
2469 /* Darwin sets the flag first, BSD calls selrecord first */
2470 so
->so_rcv
.sb_flags
|= SB_SEL
;
2471 selrecord(p
, &so
->so_rcv
.sb_sel
, wql
);
2474 if (events
& (POLLOUT
| POLLWRNORM
)) {
2475 /* Darwin sets the flag first, BSD calls selrecord first */
2476 so
->so_snd
.sb_flags
|= SB_SEL
;
2477 selrecord(p
, &so
->so_snd
.sb_sel
, wql
);
2487 soo_kqfilter(struct file
*fp
, struct knote
*kn
, struct proc
*p
)
2489 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2493 switch (kn
->kn_filter
) {
2495 if (so
->so_options
& SO_ACCEPTCONN
)
2496 kn
->kn_fop
= &solisten_filtops
;
2498 kn
->kn_fop
= &soread_filtops
;
2502 kn
->kn_fop
= &sowrite_filtops
;
2509 if (sb
->sb_sel
.si_flags
& SI_INITED
)
2513 if (KNOTE_ATTACH(&sb
->sb_sel
.si_note
, kn
))
2514 sb
->sb_flags
|= SB_KNOTE
;
2520 filt_sordetach(struct knote
*kn
)
2522 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2525 if (so
->so_rcv
.sb_flags
& SB_KNOTE
&&
2526 !(so
->so_rcv
.sb_sel
.si_flags
& SI_INITED
))
2527 if (KNOTE_DETACH(&so
->so_rcv
.sb_sel
.si_note
, kn
))
2528 so
->so_rcv
.sb_flags
&= ~SB_KNOTE
;
2534 filt_soread(struct knote
*kn
, long hint
)
2536 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2538 kn
->kn_data
= so
->so_rcv
.sb_cc
;
2539 if (so
->so_state
& SS_CANTRCVMORE
) {
2540 kn
->kn_flags
|= EV_EOF
;
2541 kn
->kn_fflags
= so
->so_error
;
2544 if (so
->so_error
) /* temporary udp error */
2546 if (kn
->kn_sfflags
& NOTE_LOWAT
)
2547 return (kn
->kn_data
>= kn
->kn_sdata
);
2548 return (kn
->kn_data
>= so
->so_rcv
.sb_lowat
);
2552 filt_sowdetach(struct knote
*kn
)
2554 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2557 if(so
->so_snd
.sb_flags
& SB_KNOTE
&&
2558 !(so
->so_snd
.sb_sel
.si_flags
& SI_INITED
))
2559 if (KNOTE_DETACH(&so
->so_snd
.sb_sel
.si_note
, kn
))
2560 so
->so_snd
.sb_flags
&= ~SB_KNOTE
;
2566 filt_sowrite(struct knote
*kn
, long hint
)
2568 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2570 kn
->kn_data
= sbspace(&so
->so_snd
);
2571 if (so
->so_state
& SS_CANTSENDMORE
) {
2572 kn
->kn_flags
|= EV_EOF
;
2573 kn
->kn_fflags
= so
->so_error
;
2576 if (so
->so_error
) /* temporary udp error */
2578 if (((so
->so_state
& SS_ISCONNECTED
) == 0) &&
2579 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
))
2581 if (kn
->kn_sfflags
& NOTE_LOWAT
)
2582 return (kn
->kn_data
>= kn
->kn_sdata
);
2583 return (kn
->kn_data
>= so
->so_snd
.sb_lowat
);
2588 filt_solisten(struct knote
*kn
, long hint
)
2590 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2592 kn
->kn_data
= so
->so_qlen
;
2593 return (! TAILQ_EMPTY(&so
->so_comp
));