2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1982, 1986, 1988, 1990, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
57 * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.16 2001/06/14 20:46:06 ume Exp $
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/filedesc.h>
65 #include <sys/fcntl.h>
66 #include <sys/malloc.h>
68 #include <sys/domain.h>
69 #include <sys/kernel.h>
70 #include <sys/event.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/resourcevar.h>
76 #include <sys/signalvar.h>
77 #include <sys/sysctl.h>
80 #include <sys/kdebug.h>
81 #include <net/route.h>
82 #include <netinet/in.h>
83 #include <netinet/in_pcb.h>
84 #include <kern/zalloc.h>
85 #include <machine/limits.h>
88 int so_cache_timeouts
= 0;
89 int so_cache_max_freed
= 0;
90 int cached_sock_count
= 0;
91 struct socket
*socket_cache_head
= 0;
92 struct socket
*socket_cache_tail
= 0;
93 u_long so_cache_time
= 0;
94 int so_cache_init_done
= 0;
95 struct zone
*so_cache_zone
;
96 extern int get_inpcb_str_size();
97 extern int get_tcp_str_size();
99 #include <machine/limits.h>
101 static void filt_sordetach(struct knote
*kn
);
102 static int filt_soread(struct knote
*kn
, long hint
);
103 static void filt_sowdetach(struct knote
*kn
);
104 static int filt_sowrite(struct knote
*kn
, long hint
);
105 static int filt_solisten(struct knote
*kn
, long hint
);
107 static struct filterops solisten_filtops
=
108 { 1, NULL
, filt_sordetach
, filt_solisten
};
109 static struct filterops soread_filtops
=
110 { 1, NULL
, filt_sordetach
, filt_soread
};
111 static struct filterops sowrite_filtops
=
112 { 1, NULL
, filt_sowdetach
, filt_sowrite
};
114 int socket_debug
= 0;
115 int socket_zone
= M_SOCKET
;
116 so_gen_t so_gencnt
; /* generation count for sockets */
118 MALLOC_DEFINE(M_SONAME
, "soname", "socket name");
119 MALLOC_DEFINE(M_PCB
, "pcb", "protocol control block");
121 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0)
122 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2)
123 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1)
124 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3)
125 #define DBG_FNC_SOSEND NETDBG_CODE(DBG_NETSOCK, (4 << 8) | 1)
126 #define DBG_FNC_SORECEIVE NETDBG_CODE(DBG_NETSOCK, (8 << 8))
127 #define DBG_FNC_SOSHUTDOWN NETDBG_CODE(DBG_NETSOCK, (9 << 8))
130 SYSCTL_DECL(_kern_ipc
);
132 static int somaxconn
= SOMAXCONN
;
133 SYSCTL_INT(_kern_ipc
, KIPC_SOMAXCONN
, somaxconn
, CTLFLAG_RW
, &somaxconn
,
136 /* Should we get a maximum also ??? */
137 static int sosendmaxchain
= 65536;
138 static int sosendminchain
= 16384;
139 static int sorecvmincopy
= 16384;
140 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sosendminchain
, CTLFLAG_RW
, &sosendminchain
,
142 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sorecvmincopy
, CTLFLAG_RW
, &sorecvmincopy
,
145 void so_cache_timer();
146 struct mbuf
*m_getpackets(int, int, int);
150 * Socket operation routines.
151 * These routines are called by the routines in
152 * sys_socket.c or from a system process, and
153 * implement the semantics of socket operations by
154 * switching out to the protocol specific routines.
162 so_cache_init_done
= 1;
164 timeout(so_cache_timer
, NULL
, (SO_CACHE_FLUSH_INTERVAL
* hz
));
165 str_size
= (vm_size_t
)( sizeof(struct socket
) + 4 +
166 get_inpcb_str_size() + 4 +
168 so_cache_zone
= zinit (str_size
, 120000*str_size
, 8192, "socache zone");
170 kprintf("cached_sock_alloc -- so_cache_zone size is %x\n", str_size
);
175 void cached_sock_alloc(so
, waitok
)
182 register u_long offset
;
186 if (cached_sock_count
) {
188 *so
= socket_cache_head
;
190 panic("cached_sock_alloc: cached sock is null");
192 socket_cache_head
= socket_cache_head
->cache_next
;
193 if (socket_cache_head
)
194 socket_cache_head
->cache_prev
= 0;
196 socket_cache_tail
= 0;
199 temp
= (*so
)->so_saved_pcb
;
200 bzero((caddr_t
)*so
, sizeof(struct socket
));
202 kprintf("cached_sock_alloc - retreiving cached sock %x - count == %d\n", *so
,
205 (*so
)->so_saved_pcb
= temp
;
209 kprintf("Allocating cached sock %x from memory\n", *so
);
214 *so
= (struct socket
*) zalloc(so_cache_zone
);
216 *so
= (struct socket
*) zalloc_noblock(so_cache_zone
);
221 bzero((caddr_t
)*so
, sizeof(struct socket
));
224 * Define offsets for extra structures into our single block of
225 * memory. Align extra structures on longword boundaries.
229 offset
= (u_long
) *so
;
230 offset
+= sizeof(struct socket
);
233 offset
&= 0xfffffffc;
235 (*so
)->so_saved_pcb
= (caddr_t
) offset
;
236 offset
+= get_inpcb_str_size();
239 offset
&= 0xfffffffc;
242 ((struct inpcb
*) (*so
)->so_saved_pcb
)->inp_saved_ppcb
= (caddr_t
) offset
;
244 kprintf("Allocating cached socket - %x, pcb=%x tcpcb=%x\n", *so
,
246 ((struct inpcb
*)(*so
)->so_saved_pcb
)->inp_saved_ppcb
);
250 (*so
)->cached_in_sock_layer
= 1;
254 void cached_sock_free(so
)
261 if (++cached_sock_count
> MAX_CACHED_SOCKETS
) {
265 kprintf("Freeing overflowed cached socket %x\n", so
);
267 zfree(so_cache_zone
, (vm_offset_t
) so
);
271 kprintf("Freeing socket %x into cache\n", so
);
273 if (so_cache_hw
< cached_sock_count
)
274 so_cache_hw
= cached_sock_count
;
276 so
->cache_next
= socket_cache_head
;
278 if (socket_cache_head
)
279 socket_cache_head
->cache_prev
= so
;
281 socket_cache_tail
= so
;
283 so
->cache_timestamp
= so_cache_time
;
284 socket_cache_head
= so
;
289 kprintf("Freed cached sock %x into cache - count is %d\n", so
, cached_sock_count
);
296 void so_cache_timer()
298 register struct socket
*p
;
300 register int n_freed
= 0;
301 boolean_t funnel_state
;
303 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
309 while (p
= socket_cache_tail
)
311 if ((so_cache_time
- p
->cache_timestamp
) < SO_CACHE_TIME_LIMIT
)
316 if (socket_cache_tail
= p
->cache_prev
)
317 p
->cache_prev
->cache_next
= 0;
318 if (--cached_sock_count
== 0)
319 socket_cache_head
= 0;
323 zfree(so_cache_zone
, (vm_offset_t
) p
);
326 if (++n_freed
>= SO_CACHE_MAX_FREE_BATCH
)
328 so_cache_max_freed
++;
334 timeout(so_cache_timer
, NULL
, (SO_CACHE_FLUSH_INTERVAL
* hz
));
336 (void) thread_funnel_set(network_flock
, FALSE
);
339 #endif /* __APPLE__ */
342 * Get a socket structure from our zone, and initialize it.
343 * We don't implement `waitok' yet (see comments in uipc_domain.c).
344 * Note that it would probably be better to allocate socket
345 * and PCB at the same time, but I'm not convinced that all
346 * the protocols can be easily modified to do this.
349 soalloc(waitok
, dom
, type
)
356 if ((dom
== PF_INET
) && (type
== SOCK_STREAM
))
357 cached_sock_alloc(&so
, waitok
);
360 so
= _MALLOC_ZONE(sizeof(*so
), socket_zone
, M_WAITOK
);
362 bzero(so
, sizeof *so
);
364 /* XXX race condition for reentrant kernel */
367 so
->so_gencnt
= ++so_gencnt
;
368 so
->so_zone
= socket_zone
;
375 socreate(dom
, aso
, type
, proto
)
381 struct proc
*p
= current_proc();
382 register struct protosw
*prp
;
383 register struct socket
*so
;
384 register int error
= 0;
386 extern int tcpconsdebug
;
389 prp
= pffindproto(dom
, proto
, type
);
391 prp
= pffindtype(dom
, type
);
393 if (prp
== 0 || prp
->pr_usrreqs
->pru_attach
== 0)
394 return (EPROTONOSUPPORT
);
397 if (p
->p_prison
&& jail_socket_unixiproute_only
&&
398 prp
->pr_domain
->dom_family
!= PF_LOCAL
&&
399 prp
->pr_domain
->dom_family
!= PF_INET
&&
400 prp
->pr_domain
->dom_family
!= PF_ROUTE
) {
401 return (EPROTONOSUPPORT
);
405 if (prp
->pr_type
!= type
)
407 so
= soalloc(p
!= 0, dom
, type
);
411 TAILQ_INIT(&so
->so_incomp
);
412 TAILQ_INIT(&so
->so_comp
);
417 if (p
->p_ucred
->cr_uid
== 0)
418 so
->so_state
= SS_PRIV
;
420 so
->so_uid
= p
->p_ucred
->cr_uid
;
423 so
->so_cred
= p
->p_ucred
;
428 so
->so_rcv
.sb_flags
|= SB_RECV
; /* XXX */
429 if (prp
->pr_sfilter
.tqh_first
)
430 error
= sfilter_init(so
);
433 error
= (*prp
->pr_usrreqs
->pru_attach
)(so
, proto
, p
);
437 * If so_pcb is not zero, the socket will be leaked,
438 * so protocol attachment handler must be coded carefuly
440 so
->so_state
|= SS_NOFDREF
;
445 prp
->pr_domain
->dom_refs
++;
446 so
->so_rcv
.sb_so
= so
->so_snd
.sb_so
= so
;
447 TAILQ_INIT(&so
->so_evlist
);
449 if (tcpconsdebug
== 2)
450 so
->so_options
|= SO_DEBUG
;
461 struct sockaddr
*nam
;
464 struct proc
*p
= current_proc();
469 error
= (*so
->so_proto
->pr_usrreqs
->pru_bind
)(so
, nam
, p
);
473 if (kp
->e_soif
&& kp
->e_soif
->sf_sobind
) {
474 error
= (*kp
->e_soif
->sf_sobind
)(so
, nam
, kp
);
476 if (error
== EJUSTRETURN
) {
495 so
->so_gencnt
= ++so_gencnt
;
498 if (so
->so_rcv
.sb_hiwat
)
499 (void)chgsbsize(so
->so_cred
->cr_uidinfo
,
500 &so
->so_rcv
.sb_hiwat
, 0, RLIM_INFINITY
);
501 if (so
->so_snd
.sb_hiwat
)
502 (void)chgsbsize(so
->so_cred
->cr_uidinfo
,
503 &so
->so_snd
.sb_hiwat
, 0, RLIM_INFINITY
);
505 if (so
->so_accf
!= NULL
) {
506 if (so
->so_accf
->so_accept_filter
!= NULL
&&
507 so
->so_accf
->so_accept_filter
->accf_destroy
!= NULL
) {
508 so
->so_accf
->so_accept_filter
->accf_destroy(so
);
510 if (so
->so_accf
->so_accept_filter_str
!= NULL
)
511 FREE(so
->so_accf
->so_accept_filter_str
, M_ACCF
);
512 FREE(so
->so_accf
, M_ACCF
);
516 zfreei(so
->so_zone
, so
);
518 if (so
->cached_in_sock_layer
== 1)
519 cached_sock_free(so
);
521 _FREE_ZONE(so
, sizeof(*so
), so
->so_zone
);
522 #endif /* __APPLE__ */
526 solisten(so
, backlog
)
527 register struct socket
*so
;
532 struct proc
*p
= current_proc();
536 error
= (*so
->so_proto
->pr_usrreqs
->pru_listen
)(so
, p
);
541 if (TAILQ_EMPTY(&so
->so_comp
))
542 so
->so_options
|= SO_ACCEPTCONN
;
543 if (backlog
< 0 || backlog
> somaxconn
)
545 so
->so_qlimit
= backlog
;
548 if (kp
->e_soif
&& kp
->e_soif
->sf_solisten
) {
549 error
= (*kp
->e_soif
->sf_solisten
)(so
, kp
);
551 if (error
== EJUSTRETURN
) {
569 register struct socket
*so
;
573 struct socket
*head
= so
->so_head
;
577 if (kp
->e_soif
&& kp
->e_soif
->sf_sofree
) {
578 error
= (*kp
->e_soif
->sf_sofree
)(so
, kp
);
580 selthreadclear(&so
->so_snd
.sb_sel
);
581 selthreadclear(&so
->so_rcv
.sb_sel
);
582 return; /* void fn */
588 if (so
->so_pcb
|| (so
->so_state
& SS_NOFDREF
) == 0) {
590 selthreadclear(&so
->so_snd
.sb_sel
);
591 selthreadclear(&so
->so_rcv
.sb_sel
);
596 if (so
->so_state
& SS_INCOMP
) {
597 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
599 } else if (so
->so_state
& SS_COMP
) {
601 * We must not decommission a socket that's
602 * on the accept(2) queue. If we do, then
603 * accept(2) may hang after select(2) indicated
604 * that the listening socket was ready.
607 selthreadclear(&so
->so_snd
.sb_sel
);
608 selthreadclear(&so
->so_rcv
.sb_sel
);
612 panic("sofree: not queued");
615 so
->so_state
&= ~SS_INCOMP
;
619 selthreadclear(&so
->so_snd
.sb_sel
);
620 sbrelease(&so
->so_snd
);
628 * Close a socket on last file table reference removal.
629 * Initiate disconnect if connected.
630 * Free socket when disconnect complete.
634 register struct socket
*so
;
636 int s
= splnet(); /* conservative */
641 funsetown(so
->so_sigio
);
645 if (kp
->e_soif
&& kp
->e_soif
->sf_soclose
) {
646 error
= (*kp
->e_soif
->sf_soclose
)(so
, kp
);
649 return((error
== EJUSTRETURN
) ? 0 : error
);
655 if (so
->so_options
& SO_ACCEPTCONN
) {
656 struct socket
*sp
, *sonext
;
658 sp
= TAILQ_FIRST(&so
->so_incomp
);
659 for (; sp
!= NULL
; sp
= sonext
) {
660 sonext
= TAILQ_NEXT(sp
, so_list
);
663 for (sp
= TAILQ_FIRST(&so
->so_comp
); sp
!= NULL
; sp
= sonext
) {
664 sonext
= TAILQ_NEXT(sp
, so_list
);
665 /* Dequeue from so_comp since sofree() won't do it */
666 TAILQ_REMOVE(&so
->so_comp
, sp
, so_list
);
668 sp
->so_state
&= ~SS_COMP
;
676 if (so
->so_state
& SS_ISCONNECTED
) {
677 if ((so
->so_state
& SS_ISDISCONNECTING
) == 0) {
678 error
= sodisconnect(so
);
682 if (so
->so_options
& SO_LINGER
) {
683 if ((so
->so_state
& SS_ISDISCONNECTING
) &&
684 (so
->so_state
& SS_NBIO
))
686 while (so
->so_state
& SS_ISCONNECTED
) {
687 error
= tsleep((caddr_t
)&so
->so_timeo
,
688 PSOCK
| PCATCH
, "soclos", so
->so_linger
);
696 int error2
= (*so
->so_proto
->pr_usrreqs
->pru_detach
)(so
);
701 if (so
->so_pcb
&& so
->so_state
& SS_NOFDREF
)
702 panic("soclose: NOFDREF");
703 so
->so_state
|= SS_NOFDREF
;
705 so
->so_proto
->pr_domain
->dom_refs
--;
714 * Must be called at splnet...
722 error
= (*so
->so_proto
->pr_usrreqs
->pru_abort
)(so
);
732 register struct socket
*so
;
733 struct sockaddr
**nam
;
739 if ((so
->so_state
& SS_NOFDREF
) == 0)
740 panic("soaccept: !NOFDREF");
741 so
->so_state
&= ~SS_NOFDREF
;
742 error
= (*so
->so_proto
->pr_usrreqs
->pru_accept
)(so
, nam
);
746 if (kp
->e_soif
&& kp
->e_soif
->sf_soaccept
) {
747 error
= (*kp
->e_soif
->sf_soaccept
)(so
, nam
, kp
);
749 if (error
== EJUSTRETURN
) {
768 register struct socket
*so
;
769 struct sockaddr
*nam
;
774 struct proc
*p
= current_proc();
777 if (so
->so_options
& SO_ACCEPTCONN
)
781 * If protocol is connection-based, can only connect once.
782 * Otherwise, if connected, try to disconnect first.
783 * This allows user to disconnect by connecting to, e.g.,
786 if (so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
) &&
787 ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) ||
788 (error
= sodisconnect(so
))))
792 * Run connect filter before calling protocol:
793 * - non-blocking connect returns before completion;
794 * - allows filters to modify address.
798 if (kp
->e_soif
&& kp
->e_soif
->sf_soconnect
) {
799 error
= (*kp
->e_soif
->sf_soconnect
)(so
, nam
, kp
);
801 if (error
== EJUSTRETURN
) {
810 error
= (*so
->so_proto
->pr_usrreqs
->pru_connect
)(so
, nam
, p
);
818 register struct socket
*so1
;
825 error
= (*so1
->so_proto
->pr_usrreqs
->pru_connect2
)(so1
, so2
);
827 kp
= sotokextcb(so1
);
829 if (kp
->e_soif
&& kp
->e_soif
->sf_soconnect2
) {
830 error
= (*kp
->e_soif
->sf_soconnect2
)(so1
, so2
, kp
);
832 if (error
== EJUSTRETURN
) {
849 register struct socket
*so
;
855 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
859 if (so
->so_state
& SS_ISDISCONNECTING
) {
863 error
= (*so
->so_proto
->pr_usrreqs
->pru_disconnect
)(so
);
867 if (kp
->e_soif
&& kp
->e_soif
->sf_sodisconnect
) {
868 error
= (*kp
->e_soif
->sf_sodisconnect
)(so
, kp
);
870 if (error
== EJUSTRETURN
) {
887 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_DONTWAIT : M_WAIT)
890 * If send must go all at once and message is larger than
891 * send buffering, then hard error.
892 * Lock against other senders.
893 * If must go all at once and not enough room now, then
894 * inform user that this would block and do nothing.
895 * Otherwise, if nonblocking, send as much as possible.
896 * The data to be sent is described by "uio" if nonzero,
897 * otherwise by the mbuf chain "top" (which must be null
898 * if uio is not). Data provided in mbuf chain must be small
899 * enough to send all at once.
901 * Returns nonzero on error, timeout or signal; callers
902 * must check for short counts if EINTR/ERESTART are returned.
903 * Data and control buffers are freed on return.
905 * MSG_HOLD: go thru most of sosend(), but just enqueue the mbuf
906 * MSG_SEND: go thru as for MSG_HOLD on current fragment, then
907 * point at the mbuf chain being constructed and go from there.
910 sosend(so
, addr
, uio
, top
, control
, flags
)
911 register struct socket
*so
;
912 struct sockaddr
*addr
;
915 struct mbuf
*control
;
920 register struct mbuf
*m
, *freelist
= NULL
;
921 register long space
, len
, resid
;
922 int clen
= 0, error
, s
, dontroute
, mlen
, sendflags
;
923 int atomic
= sosendallatonce(so
) || top
;
924 struct proc
*p
= current_proc();
928 resid
= uio
->uio_resid
;
930 resid
= top
->m_pkthdr
.len
;
932 KERNEL_DEBUG((DBG_FNC_SOSEND
| DBG_FUNC_START
),
937 so
->so_snd
.sb_hiwat
);
940 * In theory resid should be unsigned.
941 * However, space must be signed, as it might be less than 0
942 * if we over-committed, and we must use a signed comparison
943 * of space and resid. On the other hand, a negative resid
944 * causes us to loop sending 0-length segments to the protocol.
946 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
947 * type sockets since that's an error.
949 if (resid
< 0 || so
->so_type
== SOCK_STREAM
&& (flags
& MSG_EOR
)) {
955 (flags
& MSG_DONTROUTE
) && (so
->so_options
& SO_DONTROUTE
) == 0 &&
956 (so
->so_proto
->pr_flags
& PR_ATOMIC
);
958 p
->p_stats
->p_ru
.ru_msgsnd
++;
960 clen
= control
->m_len
;
961 #define snderr(errno) { error = errno; splx(s); goto release; }
964 error
= sblock(&so
->so_snd
, SBLOCKWAIT(flags
));
969 if (so
->so_state
& SS_CANTSENDMORE
)
972 error
= so
->so_error
;
977 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
979 * `sendto' and `sendmsg' is allowed on a connection-
980 * based socket if it supports implied connect.
981 * Return ENOTCONN if not connected and no address is
984 if ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) &&
985 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) == 0) {
986 if ((so
->so_state
& SS_ISCONFIRMING
) == 0 &&
987 !(resid
== 0 && clen
!= 0))
989 } else if (addr
== 0 && !(flags
&MSG_HOLD
))
990 snderr(so
->so_proto
->pr_flags
& PR_CONNREQUIRED
?
991 ENOTCONN
: EDESTADDRREQ
);
993 space
= sbspace(&so
->so_snd
);
996 if ((atomic
&& resid
> so
->so_snd
.sb_hiwat
) ||
997 clen
> so
->so_snd
.sb_hiwat
)
999 if (space
< resid
+ clen
&&
1000 (atomic
|| space
< so
->so_snd
.sb_lowat
|| space
< clen
)) {
1001 if (so
->so_state
& SS_NBIO
)
1002 snderr(EWOULDBLOCK
);
1003 sbunlock(&so
->so_snd
);
1004 error
= sbwait(&so
->so_snd
);
1017 * Data is prepackaged in "top".
1020 if (flags
& MSG_EOR
)
1021 top
->m_flags
|= M_EOR
;
1023 boolean_t dropped_funnel
= FALSE
;
1027 bytes_to_copy
= min(resid
, space
);
1029 if (sosendminchain
> 0) {
1030 if (bytes_to_copy
>= sosendminchain
) {
1031 dropped_funnel
= TRUE
;
1032 (void)thread_funnel_set(network_flock
, FALSE
);
1036 chainlength
= sosendmaxchain
;
1040 if (bytes_to_copy
>= MINCLSIZE
) {
1042 * try to maintain a local cache of mbuf clusters needed to complete this write
1043 * the list is further limited to the number that are currently needed to fill the socket
1044 * this mechanism allows a large number of mbufs/clusters to be grabbed under a single
1045 * mbuf lock... if we can't get any clusters, than fall back to trying for mbufs
1046 * if we fail early (or miscalcluate the number needed) make sure to release any clusters
1047 * we haven't yet consumed.
1049 if ((m
= freelist
) == NULL
) {
1051 int hdrs_needed
= 0;
1055 num_needed
= bytes_to_copy
/ MCLBYTES
;
1057 if ((bytes_to_copy
- (num_needed
* MCLBYTES
)) >= MINCLSIZE
)
1060 if ((freelist
= m_getpackets(num_needed
, hdrs_needed
, M_WAIT
)) == NULL
)
1061 goto getpackets_failed
;
1064 freelist
= m
->m_next
;
1068 len
= min(mlen
, bytes_to_copy
);
1072 MGETHDR(m
, M_WAIT
, MT_DATA
);
1074 m
->m_pkthdr
.len
= 0;
1075 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
1077 MGET(m
, M_WAIT
, MT_DATA
);
1080 len
= min(mlen
, bytes_to_copy
);
1082 * For datagram protocols, leave room
1083 * for protocol headers in first mbuf.
1085 if (atomic
&& top
== 0 && len
< mlen
)
1092 error
= uiomove(mtod(m
, caddr_t
), (int)len
, uio
);
1094 resid
= uio
->uio_resid
;
1098 top
->m_pkthdr
.len
+= len
;
1103 if (flags
& MSG_EOR
)
1104 top
->m_flags
|= M_EOR
;
1107 bytes_to_copy
= min(resid
, space
);
1109 } while (space
> 0 && (chainlength
< sosendmaxchain
|| atomic
|| resid
< MINCLSIZE
));
1111 if (dropped_funnel
== TRUE
)
1112 (void)thread_funnel_set(network_flock
, TRUE
);
1117 if (flags
& (MSG_HOLD
|MSG_SEND
))
1118 { /* Enqueue for later, go away if HOLD */
1119 register struct mbuf
*mb1
;
1120 if (so
->so_temp
&& (flags
& MSG_FLUSH
))
1121 { m_freem(so
->so_temp
);
1125 so
->so_tail
->m_next
= top
;
1139 so
->so_options
|= SO_DONTROUTE
;
1140 s
= splnet(); /* XXX */
1141 /* Compute flags here, for pru_send and NKEs */
1142 sendflags
= (flags
& MSG_OOB
) ? PRUS_OOB
:
1144 * If the user set MSG_EOF, the protocol
1145 * understands this flag and nothing left to
1146 * send then use PRU_SEND_EOF instead of PRU_SEND.
1148 ((flags
& MSG_EOF
) &&
1149 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) &&
1152 /* If there is more to send set PRUS_MORETOCOME */
1153 (resid
> 0 && space
> 0) ? PRUS_MORETOCOME
: 0;
1154 kp
= sotokextcb(so
);
1156 { if (kp
->e_soif
&& kp
->e_soif
->sf_sosend
) {
1157 error
= (*kp
->e_soif
->sf_sosend
)(so
, &addr
,
1164 if (error
== EJUSTRETURN
) {
1165 sbunlock(&so
->so_snd
);
1168 m_freem_list(freelist
);
1177 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)(so
,
1178 sendflags
, top
, addr
, control
, p
);
1181 if (flags
& MSG_SEND
)
1185 so
->so_options
&= ~SO_DONTROUTE
;
1192 } while (resid
&& space
> 0);
1196 sbunlock(&so
->so_snd
);
1203 m_freem_list(freelist
);
1205 KERNEL_DEBUG(DBG_FNC_SOSEND
| DBG_FUNC_END
,
1216 * Implement receive operations on a socket.
1217 * We depend on the way that records are added to the sockbuf
1218 * by sbappend*. In particular, each record (mbufs linked through m_next)
1219 * must begin with an address if the protocol so specifies,
1220 * followed by an optional mbuf or mbufs containing ancillary data,
1221 * and then zero or more mbufs of data.
1222 * In order to avoid blocking network interrupts for the entire time here,
1223 * we splx() while doing the actual copy to user space.
1224 * Although the sockbuf is locked, new data may still be appended,
1225 * and thus we must maintain consistency of the sockbuf during that time.
1227 * The caller may receive the data as a single mbuf chain by supplying
1228 * an mbuf **mp0 for use in returning the chain. The uio is then used
1229 * only for the count in uio_resid.
1232 soreceive(so
, psa
, uio
, mp0
, controlp
, flagsp
)
1233 register struct socket
*so
;
1234 struct sockaddr
**psa
;
1237 struct mbuf
**controlp
;
1240 register struct mbuf
*m
, **mp
, *ml
;
1241 register int flags
, len
, error
, s
, offset
;
1242 struct protosw
*pr
= so
->so_proto
;
1243 struct mbuf
*nextrecord
;
1245 int orig_resid
= uio
->uio_resid
;
1247 volatile struct mbuf
*free_list
;
1248 volatile int delayed_copy_len
;
1251 struct proc
*p
= current_proc();
1254 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_START
,
1258 so
->so_rcv
.sb_lowat
,
1259 so
->so_rcv
.sb_hiwat
);
1261 kp
= sotokextcb(so
);
1263 if (kp
->e_soif
&& kp
->e_soif
->sf_soreceive
) {
1264 error
= (*kp
->e_soif
->sf_soreceive
)(so
, psa
, &uio
,
1268 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1269 return((error
== EJUSTRETURN
) ? 0 : error
);
1281 flags
= *flagsp
&~ MSG_EOR
;
1285 * When SO_WANTOOBFLAG is set we try to get out-of-band data
1286 * regardless of the flags argument. Here is the case were
1287 * out-of-band data is not inline.
1289 if ((flags
& MSG_OOB
) ||
1290 ((so
->so_options
& SO_WANTOOBFLAG
) != 0 &&
1291 (so
->so_options
& SO_OOBINLINE
) == 0 &&
1292 (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
)))) {
1293 m
= m_get(M_WAIT
, MT_DATA
);
1295 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, ENOBUFS
,0,0,0,0);
1298 error
= (*pr
->pr_usrreqs
->pru_rcvoob
)(so
, m
, flags
& MSG_PEEK
);
1302 error
= uiomove(mtod(m
, caddr_t
),
1303 (int) min(uio
->uio_resid
, m
->m_len
), uio
);
1305 } while (uio
->uio_resid
&& error
== 0 && m
);
1310 if ((so
->so_options
& SO_WANTOOBFLAG
) != 0) {
1311 if (error
== EWOULDBLOCK
|| error
== EINVAL
) {
1313 * Let's try to get normal data:
1314 * EWOULDBLOCK: out-of-band data not receive yet;
1315 * EINVAL: out-of-band data already read.
1319 } else if (error
== 0 && flagsp
)
1322 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1328 *mp
= (struct mbuf
*)0;
1329 if (so
->so_state
& SS_ISCONFIRMING
&& uio
->uio_resid
)
1330 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, 0);
1333 free_list
= (struct mbuf
*)0;
1334 delayed_copy_len
= 0;
1336 error
= sblock(&so
->so_rcv
, SBLOCKWAIT(flags
));
1338 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1343 m
= so
->so_rcv
.sb_mb
;
1345 * If we have less data than requested, block awaiting more
1346 * (subject to any timeout) if:
1347 * 1. the current count is less than the low water mark, or
1348 * 2. MSG_WAITALL is set, and it is possible to do the entire
1349 * receive operation at once if we block (resid <= hiwat).
1350 * 3. MSG_DONTWAIT is not set
1351 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1352 * we have to do the receive in sections, and thus risk returning
1353 * a short count if a timeout or signal occurs after we start.
1355 if (m
== 0 || (((flags
& MSG_DONTWAIT
) == 0 &&
1356 so
->so_rcv
.sb_cc
< uio
->uio_resid
) &&
1357 (so
->so_rcv
.sb_cc
< so
->so_rcv
.sb_lowat
||
1358 ((flags
& MSG_WAITALL
) && uio
->uio_resid
<= so
->so_rcv
.sb_hiwat
)) &&
1359 m
->m_nextpkt
== 0 && (pr
->pr_flags
& PR_ATOMIC
) == 0)) {
1361 KASSERT(m
!= 0 || !so
->so_rcv
.sb_cc
, ("receive 1"));
1365 error
= so
->so_error
;
1366 if ((flags
& MSG_PEEK
) == 0)
1370 if (so
->so_state
& SS_CANTRCVMORE
) {
1376 for (; m
; m
= m
->m_next
)
1377 if (m
->m_type
== MT_OOBDATA
|| (m
->m_flags
& M_EOR
)) {
1378 m
= so
->so_rcv
.sb_mb
;
1381 if ((so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
)) == 0 &&
1382 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)) {
1386 if (uio
->uio_resid
== 0)
1388 if ((so
->so_state
& SS_NBIO
) || (flags
& MSG_DONTWAIT
)) {
1389 error
= EWOULDBLOCK
;
1392 sbunlock(&so
->so_rcv
);
1394 printf("Waiting for socket data\n");
1396 error
= sbwait(&so
->so_rcv
);
1398 printf("SORECEIVE - sbwait returned %d\n", error
);
1401 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1409 uio
->uio_procp
->p_stats
->p_ru
.ru_msgrcv
++;
1410 #else /* __APPLE__ */
1413 * This should be uio->uio-procp; however, some callers of this
1414 * function use auto variables with stack garbage, and fail to
1415 * fill out the uio structure properly.
1418 p
->p_stats
->p_ru
.ru_msgrcv
++;
1419 #endif /* __APPLE__ */
1420 nextrecord
= m
->m_nextpkt
;
1421 if ((pr
->pr_flags
& PR_ADDR
) && m
->m_type
== MT_SONAME
) {
1422 KASSERT(m
->m_type
== MT_SONAME
, ("receive 1a"));
1425 *psa
= dup_sockaddr(mtod(m
, struct sockaddr
*),
1427 if ((*psa
== 0) && (flags
& MSG_NEEDSA
)) {
1428 error
= EWOULDBLOCK
;
1432 if (flags
& MSG_PEEK
) {
1435 sbfree(&so
->so_rcv
, m
);
1436 MFREE(m
, so
->so_rcv
.sb_mb
);
1437 m
= so
->so_rcv
.sb_mb
;
1440 while (m
&& m
->m_type
== MT_CONTROL
&& error
== 0) {
1441 if (flags
& MSG_PEEK
) {
1443 *controlp
= m_copy(m
, 0, m
->m_len
);
1446 sbfree(&so
->so_rcv
, m
);
1448 if (pr
->pr_domain
->dom_externalize
&&
1449 mtod(m
, struct cmsghdr
*)->cmsg_type
==
1451 error
= (*pr
->pr_domain
->dom_externalize
)(m
);
1453 so
->so_rcv
.sb_mb
= m
->m_next
;
1455 m
= so
->so_rcv
.sb_mb
;
1457 MFREE(m
, so
->so_rcv
.sb_mb
);
1458 m
= so
->so_rcv
.sb_mb
;
1463 controlp
= &(*controlp
)->m_next
;
1467 if ((flags
& MSG_PEEK
) == 0)
1468 m
->m_nextpkt
= nextrecord
;
1470 if (type
== MT_OOBDATA
)
1476 if (!(flags
& MSG_PEEK
) && uio
->uio_resid
> sorecvmincopy
)
1484 while (m
&& (uio
->uio_resid
- delayed_copy_len
) > 0 && error
== 0) {
1485 if (m
->m_type
== MT_OOBDATA
) {
1486 if (type
!= MT_OOBDATA
)
1488 } else if (type
== MT_OOBDATA
)
1492 * This assertion needs rework. The trouble is Appletalk is uses many
1493 * mbuf types (NOT listed in mbuf.h!) which will trigger this panic.
1494 * For now just remove the assertion... CSM 9/98
1497 KASSERT(m
->m_type
== MT_DATA
|| m
->m_type
== MT_HEADER
,
1501 * Make sure to allways set MSG_OOB event when getting
1502 * out of band data inline.
1504 if ((so
->so_options
& SO_WANTOOBFLAG
) != 0 &&
1505 (so
->so_options
& SO_OOBINLINE
) != 0 &&
1506 (so
->so_state
& SS_RCVATMARK
) != 0) {
1510 so
->so_state
&= ~SS_RCVATMARK
;
1511 len
= uio
->uio_resid
- delayed_copy_len
;
1512 if (so
->so_oobmark
&& len
> so
->so_oobmark
- offset
)
1513 len
= so
->so_oobmark
- offset
;
1514 if (len
> m
->m_len
- moff
)
1515 len
= m
->m_len
- moff
;
1517 * If mp is set, just pass back the mbufs.
1518 * Otherwise copy them out via the uio, then free.
1519 * Sockbuf must be consistent here (points to current mbuf,
1520 * it points to next record) when we drop priority;
1521 * we must note any additions to the sockbuf when we
1522 * block interrupts again.
1525 if (can_delay
&& len
== m
->m_len
) {
1527 * only delay the copy if we're consuming the
1528 * mbuf and we're NOT in MSG_PEEK mode
1529 * and we have enough data to make it worthwile
1530 * to drop and retake the funnel... can_delay
1531 * reflects the state of the 2 latter constraints
1532 * moff should always be zero in these cases
1534 delayed_copy_len
+= len
;
1538 if (delayed_copy_len
) {
1539 error
= sodelayed_copy(uio
, &free_list
, &delayed_copy_len
);
1545 if (m
!= so
->so_rcv
.sb_mb
) {
1547 * can only get here if MSG_PEEK is not set
1548 * therefore, m should point at the head of the rcv queue...
1549 * if it doesn't, it means something drastically changed
1550 * while we were out from behind the funnel in sodelayed_copy...
1551 * perhaps a RST on the stream... in any event, the stream has
1552 * been interrupted... it's probably best just to return
1553 * whatever data we've moved and let the caller sort it out...
1558 error
= uiomove(mtod(m
, caddr_t
) + moff
, (int)len
, uio
);
1565 uio
->uio_resid
-= len
;
1567 if (len
== m
->m_len
- moff
) {
1568 if (m
->m_flags
& M_EOR
)
1570 if (flags
& MSG_PEEK
) {
1574 nextrecord
= m
->m_nextpkt
;
1575 sbfree(&so
->so_rcv
, m
);
1580 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
1581 *mp
= (struct mbuf
*)0;
1584 if (free_list
== NULL
)
1589 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
1593 m
->m_nextpkt
= nextrecord
;
1596 if (flags
& MSG_PEEK
)
1600 *mp
= m_copym(m
, 0, len
, M_WAIT
);
1603 so
->so_rcv
.sb_cc
-= len
;
1606 if (so
->so_oobmark
) {
1607 if ((flags
& MSG_PEEK
) == 0) {
1608 so
->so_oobmark
-= len
;
1609 if (so
->so_oobmark
== 0) {
1610 so
->so_state
|= SS_RCVATMARK
;
1612 * delay posting the actual event until after
1613 * any delayed copy processing has finished
1620 if (offset
== so
->so_oobmark
)
1624 if (flags
& MSG_EOR
)
1627 * If the MSG_WAITALL or MSG_WAITSTREAM flag is set (for non-atomic socket),
1628 * we must not quit until "uio->uio_resid == 0" or an error
1629 * termination. If a signal/timeout occurs, return
1630 * with a short count but without error.
1631 * Keep sockbuf locked against other readers.
1633 while (flags
& (MSG_WAITALL
|MSG_WAITSTREAM
) && m
== 0 && (uio
->uio_resid
- delayed_copy_len
) > 0 &&
1634 !sosendallatonce(so
) && !nextrecord
) {
1635 if (so
->so_error
|| so
->so_state
& SS_CANTRCVMORE
)
1638 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
)
1639 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, flags
);
1640 if (sbwait(&so
->so_rcv
)) {
1645 * have to wait until after we get back from the sbwait to do the copy because
1646 * we will drop the funnel if we have enough data that has been delayed... by dropping
1647 * the funnel we open up a window allowing the netisr thread to process the incoming packets
1648 * and to change the state of this socket... we're issuing the sbwait because
1649 * the socket is empty and we're expecting the netisr thread to wake us up when more
1650 * packets arrive... if we allow that processing to happen and then sbwait, we
1651 * could stall forever with packets sitting in the socket if no further packets
1652 * arrive from the remote side.
1654 * we want to copy before we've collected all the data to satisfy this request to
1655 * allow the copy to overlap the incoming packet processing on an MP system
1657 if (delayed_copy_len
> sorecvmincopy
&& (delayed_copy_len
> (so
->so_rcv
.sb_hiwat
/ 2))) {
1659 error
= sodelayed_copy(uio
, &free_list
, &delayed_copy_len
);
1664 m
= so
->so_rcv
.sb_mb
;
1666 nextrecord
= m
->m_nextpkt
;
1671 if (m
&& pr
->pr_flags
& PR_ATOMIC
) {
1673 if (so
->so_options
& SO_DONTTRUNC
)
1674 flags
|= MSG_RCVMORE
;
1678 if ((flags
& MSG_PEEK
) == 0)
1679 (void) sbdroprecord(&so
->so_rcv
);
1684 if ((flags
& MSG_PEEK
) == 0) {
1686 so
->so_rcv
.sb_mb
= nextrecord
;
1687 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
)
1688 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, flags
);
1691 if ((so
->so_options
& SO_WANTMORE
) && so
->so_rcv
.sb_cc
> 0)
1692 flags
|= MSG_HAVEMORE
;
1694 if (delayed_copy_len
) {
1695 error
= sodelayed_copy(uio
, &free_list
, &delayed_copy_len
);
1701 m_freem_list((struct mbuf
*)free_list
);
1702 free_list
= (struct mbuf
*)0;
1705 postevent(so
, 0, EV_OOB
);
1707 if (orig_resid
== uio
->uio_resid
&& orig_resid
&&
1708 (flags
& MSG_EOR
) == 0 && (so
->so_state
& SS_CANTRCVMORE
) == 0) {
1709 sbunlock(&so
->so_rcv
);
1717 if (delayed_copy_len
) {
1718 error
= sodelayed_copy(uio
, &free_list
, &delayed_copy_len
);
1721 m_freem_list((struct mbuf
*)free_list
);
1723 sbunlock(&so
->so_rcv
);
1726 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
,
1737 int sodelayed_copy(struct uio
*uio
, struct mbuf
**free_list
, int *resid
)
1740 boolean_t dropped_funnel
= FALSE
;
1745 if (*resid
>= sorecvmincopy
) {
1746 dropped_funnel
= TRUE
;
1748 (void)thread_funnel_set(network_flock
, FALSE
);
1750 while (m
&& error
== 0) {
1752 error
= uiomove(mtod(m
, caddr_t
), (int)m
->m_len
, uio
);
1756 m_freem_list(*free_list
);
1758 *free_list
= (struct mbuf
*)NULL
;
1761 if (dropped_funnel
== TRUE
)
1762 (void)thread_funnel_set(network_flock
, TRUE
);
1770 register struct socket
*so
;
1773 register struct protosw
*pr
= so
->so_proto
;
1778 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_START
, 0,0,0,0,0);
1779 kp
= sotokextcb(so
);
1781 if (kp
->e_soif
&& kp
->e_soif
->sf_soshutdown
) {
1782 ret
= (*kp
->e_soif
->sf_soshutdown
)(so
, how
, kp
);
1784 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
1785 return((ret
== EJUSTRETURN
) ? 0 : ret
);
1791 if (how
!= SHUT_WR
) {
1793 postevent(so
, 0, EV_RCLOSED
);
1795 if (how
!= SHUT_RD
) {
1796 ret
= ((*pr
->pr_usrreqs
->pru_shutdown
)(so
));
1797 postevent(so
, 0, EV_WCLOSED
);
1798 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
1802 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
1808 register struct socket
*so
;
1810 register struct sockbuf
*sb
= &so
->so_rcv
;
1811 register struct protosw
*pr
= so
->so_proto
;
1812 register int s
, error
;
1816 kp
= sotokextcb(so
);
1818 if (kp
->e_soif
&& kp
->e_soif
->sf_sorflush
) {
1819 if ((*kp
->e_soif
->sf_sorflush
)(so
, kp
))
1825 sb
->sb_flags
|= SB_NOINTR
;
1826 (void) sblock(sb
, M_WAIT
);
1831 selthreadclear(&sb
->sb_sel
);
1834 bzero((caddr_t
)sb
, sizeof (*sb
));
1835 if (asb
.sb_flags
& SB_KNOTE
) {
1836 sb
->sb_sel
.si_note
= asb
.sb_sel
.si_note
;
1837 sb
->sb_flags
= SB_KNOTE
;
1840 if (pr
->pr_flags
& PR_RIGHTS
&& pr
->pr_domain
->dom_dispose
)
1841 (*pr
->pr_domain
->dom_dispose
)(asb
.sb_mb
);
1847 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1848 * an additional variant to handle the case where the option value needs
1849 * to be some kind of integer, but not a specific size.
1850 * In addition to their use here, these functions are also called by the
1851 * protocol-level pr_ctloutput() routines.
1854 sooptcopyin(sopt
, buf
, len
, minlen
)
1855 struct sockopt
*sopt
;
1863 * If the user gives us more than we wanted, we ignore it,
1864 * but if we don't get the minimum length the caller
1865 * wants, we return EINVAL. On success, sopt->sopt_valsize
1866 * is set to however much we actually retrieved.
1868 if ((valsize
= sopt
->sopt_valsize
) < minlen
)
1871 sopt
->sopt_valsize
= valsize
= len
;
1873 if (sopt
->sopt_p
!= 0)
1874 return (copyin(sopt
->sopt_val
, buf
, valsize
));
1876 bcopy(sopt
->sopt_val
, buf
, valsize
);
1883 struct sockopt
*sopt
;
1891 if (sopt
->sopt_dir
!= SOPT_SET
) {
1892 sopt
->sopt_dir
= SOPT_SET
;
1895 kp
= sotokextcb(so
);
1897 if (kp
->e_soif
&& kp
->e_soif
->sf_socontrol
) {
1898 error
= (*kp
->e_soif
->sf_socontrol
)(so
, sopt
, kp
);
1900 return((error
== EJUSTRETURN
) ? 0 : error
);
1906 if (sopt
->sopt_level
!= SOL_SOCKET
) {
1907 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
)
1908 return ((*so
->so_proto
->pr_ctloutput
)
1910 error
= ENOPROTOOPT
;
1912 switch (sopt
->sopt_name
) {
1914 error
= sooptcopyin(sopt
, &l
, sizeof l
, sizeof l
);
1918 so
->so_linger
= l
.l_linger
;
1920 so
->so_options
|= SO_LINGER
;
1922 so
->so_options
&= ~SO_LINGER
;
1928 case SO_USELOOPBACK
:
1937 case SO_WANTOOBFLAG
:
1939 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1944 so
->so_options
|= sopt
->sopt_name
;
1946 so
->so_options
&= ~sopt
->sopt_name
;
1953 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1959 * Values < 1 make no sense for any of these
1960 * options, so disallow them.
1967 switch (sopt
->sopt_name
) {
1970 if (sbreserve(sopt
->sopt_name
== SO_SNDBUF
?
1971 &so
->so_snd
: &so
->so_rcv
,
1972 (u_long
) optval
) == 0) {
1979 * Make sure the low-water is never greater than
1983 so
->so_snd
.sb_lowat
=
1984 (optval
> so
->so_snd
.sb_hiwat
) ?
1985 so
->so_snd
.sb_hiwat
: optval
;
1988 so
->so_rcv
.sb_lowat
=
1989 (optval
> so
->so_rcv
.sb_hiwat
) ?
1990 so
->so_rcv
.sb_hiwat
: optval
;
1997 error
= sooptcopyin(sopt
, &tv
, sizeof tv
,
2002 /* assert(hz > 0); */
2003 if (tv
.tv_sec
< 0 || tv
.tv_sec
> SHRT_MAX
/ hz
||
2004 tv
.tv_usec
< 0 || tv
.tv_usec
>= 1000000) {
2008 /* assert(tick > 0); */
2009 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
2011 long tmp
= (u_long
)(tv
.tv_sec
* hz
) + tv
.tv_usec
/ tick
;
2012 if (tmp
> SHRT_MAX
) {
2019 switch (sopt
->sopt_name
) {
2021 so
->so_snd
.sb_timeo
= val
;
2024 so
->so_rcv
.sb_timeo
= val
;
2032 struct NFDescriptor
*nf1
, *nf2
= NULL
;
2034 error
= sooptcopyin(sopt
, &nke
,
2035 sizeof nke
, sizeof nke
);
2039 error
= nke_insert(so
, &nke
);
2044 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2049 so
->so_flags
|= SOF_NOSIGPIPE
;
2051 so
->so_flags
&= ~SOF_NOSIGPIPE
;
2056 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2061 so
->so_flags
|= SOF_NOADDRAVAIL
;
2063 so
->so_flags
&= ~SOF_NOADDRAVAIL
;
2068 error
= ENOPROTOOPT
;
2071 if (error
== 0 && so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
2072 (void) ((*so
->so_proto
->pr_ctloutput
)
2080 /* Helper routine for getsockopt */
2082 sooptcopyout(sopt
, buf
, len
)
2083 struct sockopt
*sopt
;
2093 * Documented get behavior is that we always return a value,
2094 * possibly truncated to fit in the user's buffer.
2095 * Traditional behavior is that we always tell the user
2096 * precisely how much we copied, rather than something useful
2097 * like the total amount we had available for her.
2098 * Note that this interface is not idempotent; the entire answer must
2099 * generated ahead of time.
2101 valsize
= min(len
, sopt
->sopt_valsize
);
2102 sopt
->sopt_valsize
= valsize
;
2103 if (sopt
->sopt_val
!= 0) {
2104 if (sopt
->sopt_p
!= 0)
2105 error
= copyout(buf
, sopt
->sopt_val
, valsize
);
2107 bcopy(buf
, sopt
->sopt_val
, valsize
);
2115 struct sockopt
*sopt
;
2123 if (sopt
->sopt_dir
!= SOPT_GET
) {
2124 sopt
->sopt_dir
= SOPT_GET
;
2127 kp
= sotokextcb(so
);
2129 if (kp
->e_soif
&& kp
->e_soif
->sf_socontrol
) {
2130 error
= (*kp
->e_soif
->sf_socontrol
)(so
, sopt
, kp
);
2132 return((error
== EJUSTRETURN
) ? 0 : error
);
2138 if (sopt
->sopt_level
!= SOL_SOCKET
) {
2139 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
2140 return ((*so
->so_proto
->pr_ctloutput
)
2143 return (ENOPROTOOPT
);
2145 switch (sopt
->sopt_name
) {
2147 l
.l_onoff
= so
->so_options
& SO_LINGER
;
2148 l
.l_linger
= so
->so_linger
;
2149 error
= sooptcopyout(sopt
, &l
, sizeof l
);
2152 case SO_USELOOPBACK
:
2164 case SO_WANTOOBFLAG
:
2166 optval
= so
->so_options
& sopt
->sopt_name
;
2168 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2172 optval
= so
->so_type
;
2182 m1
= so
->so_rcv
.sb_mb
;
2183 if (so
->so_proto
->pr_flags
& PR_ATOMIC
)
2186 kprintf("SKT CC: %d\n", so
->so_rcv
.sb_cc
);
2189 if (m1
->m_type
== MT_DATA
)
2190 pkt_total
+= m1
->m_len
;
2192 kprintf("CNT: %d/%d\n", m1
->m_len
, pkt_total
);
2198 optval
= so
->so_rcv
.sb_cc
;
2200 kprintf("RTN: %d\n", optval
);
2206 optval
= so
->so_error
;
2211 optval
= so
->so_snd
.sb_hiwat
;
2215 optval
= so
->so_rcv
.sb_hiwat
;
2219 optval
= so
->so_snd
.sb_lowat
;
2223 optval
= so
->so_rcv
.sb_lowat
;
2228 optval
= (sopt
->sopt_name
== SO_SNDTIMEO
?
2229 so
->so_snd
.sb_timeo
: so
->so_rcv
.sb_timeo
);
2231 tv
.tv_sec
= optval
/ hz
;
2232 tv
.tv_usec
= (optval
% hz
) * tick
;
2233 error
= sooptcopyout(sopt
, &tv
, sizeof tv
);
2237 optval
= (so
->so_flags
& SOF_NOSIGPIPE
);
2241 optval
= (so
->so_flags
& SOF_NOADDRAVAIL
);
2245 error
= ENOPROTOOPT
;
2254 * Network filter support
2256 /* Run the list of filters, creating extension control blocks */
2257 sfilter_init(register struct socket
*so
)
2258 { struct kextcb
*kp
, **kpp
;
2259 struct protosw
*prp
;
2260 struct NFDescriptor
*nfp
;
2263 nfp
= prp
->pr_sfilter
.tqh_first
; /* non-null */
2267 { MALLOC(kp
, struct kextcb
*, sizeof(*kp
),
2270 return(ENOBUFS
); /* so_free will clean up */
2276 kp
->e_soif
= nfp
->nf_soif
;
2277 kp
->e_sout
= nfp
->nf_soutil
;
2279 * Ignore return value for create
2280 * Everyone gets a chance at startup
2282 if (kp
->e_soif
&& kp
->e_soif
->sf_socreate
)
2283 (*kp
->e_soif
->sf_socreate
)(so
, prp
, kp
);
2284 nfp
= nfp
->nf_next
.tqe_next
;
2290 * Run the list of filters, freeing extension control blocks
2291 * Assumes the soif/soutil blocks have been handled.
2293 sfilter_term(struct socket
*so
)
2294 { struct kextcb
*kp
, *kp1
;
2300 * Ignore return code on termination; everyone must
2303 if (kp
->e_soif
&& kp
->e_soif
->sf_sofree
)
2304 kp
->e_soif
->sf_sofree(so
, kp
);
2312 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
2314 soopt_getm(struct sockopt
*sopt
, struct mbuf
**mp
)
2316 struct mbuf
*m
, *m_prev
;
2317 int sopt_size
= sopt
->sopt_valsize
;
2319 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
2322 if (sopt_size
> MLEN
) {
2323 MCLGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
);
2324 if ((m
->m_flags
& M_EXT
) == 0) {
2328 m
->m_len
= min(MCLBYTES
, sopt_size
);
2330 m
->m_len
= min(MLEN
, sopt_size
);
2332 sopt_size
-= m
->m_len
;
2337 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
2342 if (sopt_size
> MLEN
) {
2343 MCLGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
);
2344 if ((m
->m_flags
& M_EXT
) == 0) {
2348 m
->m_len
= min(MCLBYTES
, sopt_size
);
2350 m
->m_len
= min(MLEN
, sopt_size
);
2352 sopt_size
-= m
->m_len
;
2359 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2361 soopt_mcopyin(struct sockopt
*sopt
, struct mbuf
*m
)
2363 struct mbuf
*m0
= m
;
2365 if (sopt
->sopt_val
== NULL
)
2367 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
2368 if (sopt
->sopt_p
!= NULL
) {
2371 error
= copyin(sopt
->sopt_val
, mtod(m
, char *),
2378 bcopy(sopt
->sopt_val
, mtod(m
, char *), m
->m_len
);
2379 sopt
->sopt_valsize
-= m
->m_len
;
2380 (caddr_t
)sopt
->sopt_val
+= m
->m_len
;
2383 if (m
!= NULL
) /* should be allocated enoughly at ip6_sooptmcopyin() */
2384 panic("soopt_mcopyin");
2388 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2390 soopt_mcopyout(struct sockopt
*sopt
, struct mbuf
*m
)
2392 struct mbuf
*m0
= m
;
2395 if (sopt
->sopt_val
== NULL
)
2397 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
2398 if (sopt
->sopt_p
!= NULL
) {
2401 error
= copyout(mtod(m
, char *), sopt
->sopt_val
,
2408 bcopy(mtod(m
, char *), sopt
->sopt_val
, m
->m_len
);
2409 sopt
->sopt_valsize
-= m
->m_len
;
2410 (caddr_t
)sopt
->sopt_val
+= m
->m_len
;
2411 valsize
+= m
->m_len
;
2415 /* enough soopt buffer should be given from user-land */
2419 sopt
->sopt_valsize
= valsize
;
2425 register struct socket
*so
;
2430 kp
= sotokextcb(so
);
2432 if (kp
->e_soif
&& kp
->e_soif
->sf_sohasoutofband
) {
2433 if ((*kp
->e_soif
->sf_sohasoutofband
)(so
, kp
))
2438 if (so
->so_pgid
< 0)
2439 gsignal(-so
->so_pgid
, SIGURG
);
2440 else if (so
->so_pgid
> 0 && (p
= pfind(so
->so_pgid
)) != 0)
2442 selwakeup(&so
->so_rcv
.sb_sel
);
2446 sopoll(struct socket
*so
, int events
, struct ucred
*cred
, void * wql
)
2448 struct proc
*p
= current_proc();
2452 if (events
& (POLLIN
| POLLRDNORM
))
2454 revents
|= events
& (POLLIN
| POLLRDNORM
);
2456 if (events
& (POLLOUT
| POLLWRNORM
))
2457 if (sowriteable(so
))
2458 revents
|= events
& (POLLOUT
| POLLWRNORM
);
2460 if (events
& (POLLPRI
| POLLRDBAND
))
2461 if (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
))
2462 revents
|= events
& (POLLPRI
| POLLRDBAND
);
2465 if (events
& (POLLIN
| POLLPRI
| POLLRDNORM
| POLLRDBAND
)) {
2466 /* Darwin sets the flag first, BSD calls selrecord first */
2467 so
->so_rcv
.sb_flags
|= SB_SEL
;
2468 selrecord(p
, &so
->so_rcv
.sb_sel
, wql
);
2471 if (events
& (POLLOUT
| POLLWRNORM
)) {
2472 /* Darwin sets the flag first, BSD calls selrecord first */
2473 so
->so_snd
.sb_flags
|= SB_SEL
;
2474 selrecord(p
, &so
->so_snd
.sb_sel
, wql
);
2484 soo_kqfilter(struct file
*fp
, struct knote
*kn
, struct proc
*p
)
2486 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2490 switch (kn
->kn_filter
) {
2492 if (so
->so_options
& SO_ACCEPTCONN
)
2493 kn
->kn_fop
= &solisten_filtops
;
2495 kn
->kn_fop
= &soread_filtops
;
2499 kn
->kn_fop
= &sowrite_filtops
;
2506 if (sb
->sb_sel
.si_flags
& SI_INITED
)
2510 if (KNOTE_ATTACH(&sb
->sb_sel
.si_note
, kn
))
2511 sb
->sb_flags
|= SB_KNOTE
;
2517 filt_sordetach(struct knote
*kn
)
2519 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2522 if (so
->so_rcv
.sb_flags
& SB_KNOTE
&&
2523 !(so
->so_rcv
.sb_sel
.si_flags
& SI_INITED
))
2524 if (KNOTE_DETACH(&so
->so_rcv
.sb_sel
.si_note
, kn
))
2525 so
->so_rcv
.sb_flags
&= ~SB_KNOTE
;
2531 filt_soread(struct knote
*kn
, long hint
)
2533 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2535 kn
->kn_data
= so
->so_rcv
.sb_cc
;
2536 if (so
->so_state
& SS_CANTRCVMORE
) {
2537 kn
->kn_flags
|= EV_EOF
;
2538 kn
->kn_fflags
= so
->so_error
;
2541 if (so
->so_error
) /* temporary udp error */
2543 if (kn
->kn_sfflags
& NOTE_LOWAT
)
2544 return (kn
->kn_data
>= kn
->kn_sdata
);
2545 return (kn
->kn_data
>= so
->so_rcv
.sb_lowat
);
2549 filt_sowdetach(struct knote
*kn
)
2551 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2554 if(so
->so_snd
.sb_flags
& SB_KNOTE
&&
2555 !(so
->so_snd
.sb_sel
.si_flags
& SI_INITED
))
2556 if (KNOTE_DETACH(&so
->so_snd
.sb_sel
.si_note
, kn
))
2557 so
->so_snd
.sb_flags
&= ~SB_KNOTE
;
2563 filt_sowrite(struct knote
*kn
, long hint
)
2565 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2567 kn
->kn_data
= sbspace(&so
->so_snd
);
2568 if (so
->so_state
& SS_CANTSENDMORE
) {
2569 kn
->kn_flags
|= EV_EOF
;
2570 kn
->kn_fflags
= so
->so_error
;
2573 if (so
->so_error
) /* temporary udp error */
2575 if (((so
->so_state
& SS_ISCONNECTED
) == 0) &&
2576 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
))
2578 if (kn
->kn_sfflags
& NOTE_LOWAT
)
2579 return (kn
->kn_data
>= kn
->kn_sdata
);
2580 return (kn
->kn_data
>= so
->so_snd
.sb_lowat
);
2585 filt_solisten(struct knote
*kn
, long hint
)
2587 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
2589 kn
->kn_data
= so
->so_qlen
;
2590 return (! TAILQ_EMPTY(&so
->so_comp
));