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@
23 * Copyright (c) 1982, 1986, 1989, 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * sendfile(2) and related extensions:
27 * Copyright (c) 1998, David Greenman. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/filedesc.h>
68 #include <sys/malloc.h>
70 #include <sys/protosw.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
74 #include <sys/ktrace.h>
76 #include <sys/kernel.h>
78 #include <sys/kdebug.h>
82 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0)
83 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2)
84 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1)
85 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3)
86 #define DBG_FNC_SENDMSG NETDBG_CODE(DBG_NETSOCK, (1 << 8) | 1)
87 #define DBG_FNC_SENDTO NETDBG_CODE(DBG_NETSOCK, (2 << 8) | 1)
88 #define DBG_FNC_SENDIT NETDBG_CODE(DBG_NETSOCK, (3 << 8) | 1)
89 #define DBG_FNC_RECVFROM NETDBG_CODE(DBG_NETSOCK, (5 << 8))
90 #define DBG_FNC_RECVMSG NETDBG_CODE(DBG_NETSOCK, (6 << 8))
91 #define DBG_FNC_RECVIT NETDBG_CODE(DBG_NETSOCK, (7 << 8))
95 struct getsockname_args
{
101 struct getsockopt_args
{
115 struct getpeername_args
{
125 static void sf_buf_init(void *arg
);
126 SYSINIT(sock_sf
, SI_SUB_MBUF
, SI_ORDER_ANY
, sf_buf_init
, NULL
)
127 static struct sf_buf
*sf_buf_alloc(void);
128 static void sf_buf_ref(caddr_t addr
, u_int size
);
129 static void sf_buf_free(caddr_t addr
, u_int size
);
131 static SLIST_HEAD(, sf_buf
) sf_freelist
;
132 static vm_offset_t sf_base
;
133 static struct sf_buf
*sf_bufs
;
134 static int sf_buf_alloc_want
;
137 static int sendit
__P((struct proc
*p
, int s
, struct msghdr
*mp
, int flags
, register_t
*retval
));
138 static int recvit
__P((struct proc
*p
, int s
, struct msghdr
*mp
,
139 caddr_t namelenp
, register_t
*retval
));
141 static int accept1
__P((struct proc
*p
, struct accept_args
*uap
, register_t
*retval
, int compat
));
142 static int getsockname1
__P((struct proc
*p
, struct getsockname_args
*uap
,
143 register_t
*retval
, int compat
));
144 static int getpeername1
__P((struct proc
*p
, struct getpeername_args
*uap
,
145 register_t
*retval
, int compat
));
148 * System call interface to the socket abstraction.
150 #if COMPAT_43 || defined(COMPAT_SUNOS)
151 #define COMPAT_OLDSOCK
154 extern struct fileops socketops
;
162 socket(p
, uap
, retval
)
164 register struct socket_args
*uap
;
167 struct filedesc
*fdp
= p
->p_fd
;
172 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
173 error
= falloc(p
, &fp
, &fd
);
174 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
178 fp
->f_flag
= FREAD
|FWRITE
;
179 fp
->f_type
= DTYPE_SOCKET
;
180 fp
->f_ops
= &socketops
;
181 if (error
= socreate(uap
->domain
, &so
, uap
->type
,
183 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
186 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
188 fp
->f_data
= (caddr_t
)so
;
189 *fdflags(p
, fd
) &= ~UF_RESERVED
;
205 register struct bind_args
*uap
;
212 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
215 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
218 error
= sobind((struct socket
*)fp
->f_data
, sa
);
231 listen(p
, uap
, retval
)
233 register struct listen_args
*uap
;
239 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
242 return (solisten((struct socket
*)fp
->f_data
, uap
->backlog
));
245 #ifndef COMPAT_OLDSOCK
246 #define accept1 accept
252 accept1(p
, uap
, retval
, compat
)
254 register struct accept_args
*uap
;
262 struct socket
*head
, *so
;
264 short fflag
; /* type must match fp->f_flag */
268 error
= copyin((caddr_t
)uap
->anamelen
, (caddr_t
)&namelen
,
273 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
277 head
= (struct socket
*)fp
->f_data
;
278 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
282 if ((head
->so_state
& SS_NBIO
) && head
->so_comp
.tqh_first
== NULL
) {
284 return (EWOULDBLOCK
);
286 while (TAILQ_EMPTY(&head
->so_comp
) && head
->so_error
== 0) {
287 if (head
->so_state
& SS_CANTRCVMORE
) {
288 head
->so_error
= ECONNABORTED
;
291 error
= tsleep((caddr_t
)&head
->so_timeo
, PSOCK
| PCATCH
,
298 if (head
->so_error
) {
299 error
= head
->so_error
;
307 * At this point we know that there is at least one connection
308 * ready to be accepted. Remove it from the queue prior to
309 * allocating the file descriptor for it since falloc() may
310 * block allowing another process to accept the connection
313 so
= TAILQ_FIRST(&head
->so_comp
);
314 TAILQ_REMOVE(&head
->so_comp
, so
, so_list
);
318 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
319 error
= falloc(p
, &fp
, &fd
);
320 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
323 * Probably ran out of file descriptors. Put the
324 * unaccepted connection back onto the queue and
325 * do another wakeup so some other process might
326 * have a chance at it.
328 TAILQ_INSERT_HEAD(&head
->so_comp
, so
, so_list
);
330 wakeup_one(&head
->so_timeo
);
334 *fdflags(p
, fd
) &= ~UF_RESERVED
;
338 so
->so_state
&= ~SS_COMP
;
340 fp
->f_type
= DTYPE_SOCKET
;
342 fp
->f_ops
= &socketops
;
343 fp
->f_data
= (caddr_t
)so
;
345 (void) soaccept(so
, &sa
);
353 /* check sa_len before it is destroyed */
354 if (namelen
> sa
->sa_len
)
355 namelen
= sa
->sa_len
;
356 #ifdef COMPAT_OLDSOCK
358 ((struct osockaddr
*)sa
)->sa_family
=
361 error
= copyout(sa
, (caddr_t
)uap
->name
, (u_int
)namelen
);
364 error
= copyout((caddr_t
)&namelen
,
365 (caddr_t
)uap
->anamelen
, sizeof (*uap
->anamelen
));
373 accept(p
, uap
, retval
)
375 struct accept_args
*uap
;
379 return (accept1(p
, uap
, retval
, 0));
382 #ifdef COMPAT_OLDSOCK
384 oaccept(p
, uap
, retval
)
386 struct accept_args
*uap
;
390 return (accept1(p
, uap
, retval
, 1));
392 #endif /* COMPAT_OLDSOCK */
394 struct connect_args
{
401 connect(p
, uap
, retval
)
403 register struct connect_args
*uap
;
407 register struct socket
*so
;
411 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
414 so
= (struct socket
*)fp
->f_data
;
415 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
))
417 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
420 error
= soconnect(so
, sa
);
423 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
425 return (EINPROGRESS
);
428 while ((so
->so_state
& SS_ISCONNECTING
) && so
->so_error
== 0) {
429 error
= tsleep((caddr_t
)&so
->so_timeo
, PSOCK
| PCATCH
,
435 error
= so
->so_error
;
440 so
->so_state
&= ~SS_ISCONNECTING
;
442 if (error
== ERESTART
)
447 struct socketpair_args
{
454 socketpair(p
, uap
, retval
)
456 register struct socketpair_args
*uap
;
459 register struct filedesc
*fdp
= p
->p_fd
;
460 struct file
*fp1
, *fp2
;
461 struct socket
*so1
, *so2
;
462 int fd
, error
, sv
[2];
464 error
= socreate(uap
->domain
, &so1
, uap
->type
, uap
->protocol
);
467 error
= socreate(uap
->domain
, &so2
, uap
->type
, uap
->protocol
);
470 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
471 error
= falloc(p
, &fp1
, &fd
);
475 fp1
->f_flag
= FREAD
|FWRITE
;
476 fp1
->f_type
= DTYPE_SOCKET
;
477 fp1
->f_ops
= &socketops
;
478 fp1
->f_data
= (caddr_t
)so1
;
479 error
= falloc(p
, &fp2
, &fd
);
482 fp2
->f_flag
= FREAD
|FWRITE
;
483 fp2
->f_type
= DTYPE_SOCKET
;
484 fp2
->f_ops
= &socketops
;
485 fp2
->f_data
= (caddr_t
)so2
;
487 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
488 error
= soconnect2(so1
, so2
);
490 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
494 if (uap
->type
== SOCK_DGRAM
) {
496 * Datagram socket connection is asymmetric.
498 error
= soconnect2(so2
, so1
);
500 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
504 *fdflags(p
, sv
[0]) &= ~UF_RESERVED
;
505 *fdflags(p
, sv
[1]) &= ~UF_RESERVED
;
506 error
= copyout((caddr_t
)sv
, (caddr_t
)uap
->rsv
,
508 #if 0 /* old pipe(2) syscall compatability, unused these days */
509 retval
[0] = sv
[0]; /* XXX ??? */
510 retval
[1] = sv
[1]; /* XXX ??? */
520 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
528 sendit(p
, s
, mp
, flags
, retsize
)
529 register struct proc
*p
;
531 register struct msghdr
*mp
;
537 register struct iovec
*iov
;
539 struct mbuf
*control
;
544 struct iovec
*ktriov
= NULL
;
547 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_START
, 0,0,0,0,0);
549 if (error
= getsock(p
->p_fd
, s
, &fp
))
551 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
555 auio
.uio_iov
= mp
->msg_iov
;
556 auio
.uio_iovcnt
= mp
->msg_iovlen
;
557 auio
.uio_segflg
= UIO_USERSPACE
;
558 auio
.uio_rw
= UIO_WRITE
;
560 auio
.uio_offset
= 0; /* XXX */
563 for (i
= 0; i
< mp
->msg_iovlen
; i
++, iov
++) {
564 if (iov
->iov_len
< 0)
566 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
570 if ((auio
.uio_resid
+= iov
->iov_len
) < 0)
572 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
577 error
= getsockaddr(&to
, mp
->msg_name
, mp
->msg_namelen
);
579 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
584 if (mp
->msg_control
) {
585 if (mp
->msg_controllen
< sizeof(struct cmsghdr
)
586 #ifdef COMPAT_OLDSOCK
587 && mp
->msg_flags
!= MSG_COMPAT
593 error
= sockargs(&control
, mp
->msg_control
,
594 mp
->msg_controllen
, MT_CONTROL
);
597 #ifdef COMPAT_OLDSOCK
598 if (mp
->msg_flags
== MSG_COMPAT
) {
599 register struct cmsghdr
*cm
;
601 M_PREPEND(control
, sizeof(*cm
), M_WAIT
);
606 cm
= mtod(control
, struct cmsghdr
*);
607 cm
->cmsg_len
= control
->m_len
;
608 cm
->cmsg_level
= SOL_SOCKET
;
609 cm
->cmsg_type
= SCM_RIGHTS
;
616 len
= auio
.uio_resid
;
617 so
= (struct socket
*)fp
->f_data
;
618 error
= so
->so_proto
->pr_usrreqs
->pru_sosend(so
, to
, &auio
, 0, control
,
621 if (auio
.uio_resid
!= len
&& (error
== ERESTART
||
622 error
== EINTR
|| error
== EWOULDBLOCK
))
628 *retsize
= len
- auio
.uio_resid
;
630 if (ktriov
!= NULL
) {
632 ktrgenio(p
->p_tracep
, s
, UIO_WRITE
,
633 ktriov
, *retsize
, error
);
634 FREE(ktriov
, M_TEMP
);
640 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
655 sendto(p
, uap
, retval
)
657 register struct sendto_args
/* {
672 KERNEL_DEBUG(DBG_FNC_SENDTO
| DBG_FUNC_START
, 0,0,0,0,0);
674 msg
.msg_name
= uap
->to
;
675 msg
.msg_namelen
= uap
->tolen
;
679 #ifdef COMPAT_OLDSOCK
682 aiov
.iov_base
= uap
->buf
;
683 aiov
.iov_len
= uap
->len
;
684 stat
= sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
);
685 KERNEL_DEBUG(DBG_FNC_SENDTO
| DBG_FUNC_END
, stat
, *retval
,0,0,0);
689 #ifdef COMPAT_OLDSOCK
698 osend(p
, uap
, retval
)
700 register struct osend_args
/* {
716 aiov
.iov_base
= uap
->buf
;
717 aiov
.iov_len
= uap
->len
;
720 return (sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
));
722 struct osendmsg_args
{
729 osendmsg(p
, uap
, retval
)
731 register struct osendmsg_args
/* {
740 struct iovec aiov
[UIO_SMALLIOV
], *iov
;
743 error
= copyin(uap
->msg
, (caddr_t
)&msg
, sizeof (struct omsghdr
));
746 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
747 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
)
749 MALLOC(iov
, struct iovec
*,
750 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
754 error
= copyin((caddr_t
)msg
.msg_iov
, (caddr_t
)iov
,
755 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)));
758 msg
.msg_flags
= MSG_COMPAT
;
760 error
= sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
);
768 struct sendmsg_args
{
775 sendmsg(p
, uap
, retval
)
777 register struct sendmsg_args
*uap
;
781 struct iovec aiov
[UIO_SMALLIOV
], *iov
;
784 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_START
, 0,0,0,0,0);
785 if (error
= copyin(uap
->msg
, (caddr_t
)&msg
, sizeof (msg
)))
787 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, error
,0,0,0,0);
791 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
792 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
) {
793 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, EMSGSIZE
,0,0,0,0);
796 MALLOC(iov
, struct iovec
*,
797 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
801 if (msg
.msg_iovlen
&&
802 (error
= copyin((caddr_t
)msg
.msg_iov
, (caddr_t
)iov
,
803 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)))))
806 #ifdef COMPAT_OLDSOCK
809 error
= sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
);
813 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, error
,0,0,0,0);
818 recvit(p
, s
, mp
, namelenp
, retval
)
819 register struct proc
*p
;
821 register struct msghdr
*mp
;
827 register struct iovec
*iov
;
830 struct mbuf
*m
, *control
= 0;
833 struct sockaddr
*fromsa
= 0;
835 struct iovec
*ktriov
= NULL
;
838 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_START
, 0,0,0,0,0);
839 if (error
= getsock(p
->p_fd
, s
, &fp
))
841 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, error
,0,0,0,0);
845 auio
.uio_iov
= mp
->msg_iov
;
846 auio
.uio_iovcnt
= mp
->msg_iovlen
;
847 auio
.uio_segflg
= UIO_USERSPACE
;
848 auio
.uio_rw
= UIO_READ
;
850 auio
.uio_offset
= 0; /* XXX */
853 for (i
= 0; i
< mp
->msg_iovlen
; i
++, iov
++) {
854 if ((auio
.uio_resid
+= iov
->iov_len
) < 0) {
855 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
860 if (KTRPOINT(p
, KTR_GENIO
)) {
861 int iovlen
= auio
.uio_iovcnt
* sizeof (struct iovec
);
863 MALLOC(ktriov
, struct iovec
*, iovlen
, M_TEMP
, M_WAITOK
);
864 bcopy((caddr_t
)auio
.uio_iov
, (caddr_t
)ktriov
, iovlen
);
867 len
= auio
.uio_resid
;
868 so
= (struct socket
*)fp
->f_data
;
869 error
= so
->so_proto
->pr_usrreqs
->pru_soreceive(so
, &fromsa
, &auio
,
870 (struct mbuf
**)0, mp
->msg_control
? &control
: (struct mbuf
**)0,
873 if (auio
.uio_resid
!= len
&& (error
== ERESTART
||
874 error
== EINTR
|| error
== EWOULDBLOCK
))
878 if (ktriov
!= NULL
) {
880 ktrgenio(p
->p_tracep
, s
, UIO_WRITE
,
881 ktriov
, len
- auio
.uio_resid
, error
);
882 FREE(ktriov
, M_TEMP
);
887 *retval
= len
- auio
.uio_resid
;
889 len
= mp
->msg_namelen
;
890 if (len
<= 0 || fromsa
== 0)
894 #define MIN(a,b) ((a)>(b)?(b):(a))
896 /* save sa_len before it is destroyed by MSG_COMPAT */
897 len
= MIN(len
, fromsa
->sa_len
);
898 #ifdef COMPAT_OLDSOCK
899 if (mp
->msg_flags
& MSG_COMPAT
)
900 ((struct osockaddr
*)fromsa
)->sa_family
=
903 error
= copyout(fromsa
,
904 (caddr_t
)mp
->msg_name
, (unsigned)len
);
908 mp
->msg_namelen
= len
;
910 (error
= copyout((caddr_t
)&len
, namelenp
, sizeof (int)))) {
911 #ifdef COMPAT_OLDSOCK
912 if (mp
->msg_flags
& MSG_COMPAT
)
913 error
= 0; /* old recvfrom didn't check */
919 if (mp
->msg_control
) {
920 #ifdef COMPAT_OLDSOCK
922 * We assume that old recvmsg calls won't receive access
923 * rights and other control info, esp. as control info
924 * is always optional and those options didn't exist in 4.3.
925 * If we receive rights, trim the cmsghdr; anything else
928 if (control
&& mp
->msg_flags
& MSG_COMPAT
) {
929 if (mtod(control
, struct cmsghdr
*)->cmsg_level
!=
931 mtod(control
, struct cmsghdr
*)->cmsg_type
!=
933 mp
->msg_controllen
= 0;
936 control
->m_len
-= sizeof (struct cmsghdr
);
937 control
->m_data
+= sizeof (struct cmsghdr
);
940 len
= mp
->msg_controllen
;
942 mp
->msg_controllen
= 0;
943 ctlbuf
= (caddr_t
) mp
->msg_control
;
945 while (m
&& len
> 0) {
951 mp
->msg_flags
|= MSG_CTRUNC
;
955 if (error
= copyout((caddr_t
)mtod(m
, caddr_t
),
963 mp
->msg_controllen
= ctlbuf
- mp
->msg_control
;
967 FREE(fromsa
, M_SONAME
);
970 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, error
,0,0,0,0);
975 struct recvfrom_args
{
985 recvfrom(p
, uap
, retval
)
987 register struct recvfrom_args
/* {
1001 KERNEL_DEBUG(DBG_FNC_RECVFROM
| DBG_FUNC_START
, 0,0,0,0,0);
1003 if (uap
->fromlenaddr
) {
1004 error
= copyin((caddr_t
)uap
->fromlenaddr
,
1005 (caddr_t
)&msg
.msg_namelen
, sizeof (msg
.msg_namelen
));
1009 msg
.msg_namelen
= 0;
1010 msg
.msg_name
= uap
->from
;
1011 msg
.msg_iov
= &aiov
;
1013 aiov
.iov_base
= uap
->buf
;
1014 aiov
.iov_len
= uap
->len
;
1015 msg
.msg_control
= 0;
1016 msg
.msg_flags
= uap
->flags
;
1017 KERNEL_DEBUG(DBG_FNC_RECVFROM
| DBG_FUNC_END
, error
,0,0,0,0);
1018 return (recvit(p
, uap
->s
, &msg
, (caddr_t
)uap
->fromlenaddr
, retval
));
1021 #ifdef COMPAT_OLDSOCK
1023 orecvfrom(p
, uap
, retval
)
1025 struct recvfrom_args
*uap
;
1029 uap
->flags
|= MSG_COMPAT
;
1030 return (recvfrom(p
, uap
));
1035 #ifdef COMPAT_OLDSOCK
1037 orecv(p
, uap
, retval
)
1039 register struct orecv_args
{
1051 msg
.msg_namelen
= 0;
1052 msg
.msg_iov
= &aiov
;
1054 aiov
.iov_base
= uap
->buf
;
1055 aiov
.iov_len
= uap
->len
;
1056 msg
.msg_control
= 0;
1057 msg
.msg_flags
= uap
->flags
;
1058 return (recvit(p
, uap
->s
, &msg
, (caddr_t
)0, retval
));
1062 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1063 * overlays the new one, missing only the flags, and with the (old) access
1064 * rights where the control fields are now.
1067 orecvmsg(p
, uap
, retval
)
1069 register struct orecvmsg_args
{
1071 struct omsghdr
*msg
;
1077 struct iovec aiov
[UIO_SMALLIOV
], *iov
;
1080 error
= copyin((caddr_t
)uap
->msg
, (caddr_t
)&msg
,
1081 sizeof (struct omsghdr
));
1084 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
1085 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
)
1087 MALLOC(iov
, struct iovec
*,
1088 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
1092 msg
.msg_flags
= uap
->flags
| MSG_COMPAT
;
1093 error
= copyin((caddr_t
)msg
.msg_iov
, (caddr_t
)iov
,
1094 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)));
1098 error
= recvit(p
, uap
->s
, &msg
, (caddr_t
)&uap
->msg
->msg_namelen
, retval
);
1100 if (msg
.msg_controllen
&& error
== 0)
1101 error
= copyout((caddr_t
)&msg
.msg_controllen
,
1102 (caddr_t
)&uap
->msg
->msg_accrightslen
, sizeof (int));
1111 recvmsg(p
, uap
, retval
)
1113 register struct recvmsg_args
{
1121 struct iovec aiov
[UIO_SMALLIOV
], *uiov
, *iov
;
1124 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_START
, 0,0,0,0,0);
1125 if (error
= copyin((caddr_t
)uap
->msg
, (caddr_t
)&msg
,
1128 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, error
,0,0,0,0);
1132 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
1133 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
) {
1134 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, EMSGSIZE
,0,0,0,0);
1137 MALLOC(iov
, struct iovec
*,
1138 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
1142 #ifdef COMPAT_OLDSOCK
1143 msg
.msg_flags
= uap
->flags
&~ MSG_COMPAT
;
1145 msg
.msg_flags
= uap
->flags
;
1149 error
= copyin((caddr_t
)uiov
, (caddr_t
)iov
,
1150 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)));
1153 error
= recvit(p
, uap
->s
, &msg
, (caddr_t
)0, retval
);
1156 error
= copyout((caddr_t
)&msg
, (caddr_t
)uap
->msg
, sizeof(msg
));
1161 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, error
,0,0,0,0);
1167 shutdown(p
, uap
, retval
)
1169 register struct shutdown_args
{
1178 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1181 return (soshutdown((struct socket
*)fp
->f_data
, uap
->how
));
1190 setsockopt(p
, uap
, retval
)
1192 register struct setsockopt_args
{
1202 struct sockopt sopt
;
1205 if (uap
->val
== 0 && uap
->valsize
!= 0)
1207 if (uap
->valsize
< 0)
1210 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1214 sopt
.sopt_dir
= SOPT_SET
;
1215 sopt
.sopt_level
= uap
->level
;
1216 sopt
.sopt_name
= uap
->name
;
1217 sopt
.sopt_val
= uap
->val
;
1218 sopt
.sopt_valsize
= uap
->valsize
;
1221 return (sosetopt((struct socket
*)fp
->f_data
, &sopt
));
1227 getsockopt(p
, uap
, retval
)
1229 struct getsockopt_args
*uap
;
1234 struct sockopt sopt
;
1236 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1240 error
= copyin((caddr_t
)uap
->avalsize
, (caddr_t
)&valsize
,
1249 sopt
.sopt_dir
= SOPT_GET
;
1250 sopt
.sopt_level
= uap
->level
;
1251 sopt
.sopt_name
= uap
->name
;
1252 sopt
.sopt_val
= uap
->val
;
1253 sopt
.sopt_valsize
= (size_t)valsize
; /* checked non-negative above */
1256 error
= sogetopt((struct socket
*)fp
->f_data
, &sopt
);
1258 valsize
= sopt
.sopt_valsize
;
1259 error
= copyout((caddr_t
)&valsize
,
1260 (caddr_t
)uap
->avalsize
, sizeof (valsize
));
1272 pipe(p
, uap
, retval
)
1274 struct pipe_args
*uap
;
1277 struct file
*rf
, *wf
;
1278 struct socket
*rso
, *wso
;
1281 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1282 if (error
= socreate(AF_UNIX
, &rso
, SOCK_STREAM
, 0)) {
1283 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1286 if (error
= socreate(AF_UNIX
, &wso
, SOCK_STREAM
, 0)) {
1289 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1290 error
= falloc(p
, &rf
, &fd
);
1295 rf
->f_type
= DTYPE_SOCKET
;
1296 rf
->f_ops
= &socketops
;
1297 rf
->f_data
= (caddr_t
)rso
;
1298 if (error
= falloc(p
, &wf
, &fd
))
1300 wf
->f_flag
= FWRITE
;
1301 wf
->f_type
= DTYPE_SOCKET
;
1302 wf
->f_ops
= &socketops
;
1303 wf
->f_data
= (caddr_t
)wso
;
1306 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1307 error
= unp_connect2(wso
, rso
);
1308 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1311 *fdflags(p
, retval
[0]) &= ~UF_RESERVED
;
1312 *fdflags(p
, retval
[1]) &= ~UF_RESERVED
;
1315 fdrelse(p
, retval
[1]);
1318 fdrelse(p
, retval
[0]);
1321 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1326 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1336 getsockname1(p
, uap
, retval
, compat
)
1338 register struct getsockname_args
*uap
;
1343 register struct socket
*so
;
1344 struct sockaddr
*sa
;
1348 error
= getsock(p
->p_fd
, uap
->fdes
, &fp
);
1351 error
= copyin((caddr_t
)uap
->alen
, (caddr_t
)&len
, sizeof (len
));
1354 so
= (struct socket
*)fp
->f_data
;
1356 error
= (*so
->so_proto
->pr_usrreqs
->pru_sockaddr
)(so
, &sa
);
1364 len
= MIN(len
, sa
->sa_len
);
1365 #ifdef COMPAT_OLDSOCK
1367 ((struct osockaddr
*)sa
)->sa_family
= sa
->sa_family
;
1369 error
= copyout(sa
, (caddr_t
)uap
->asa
, (u_int
)len
);
1372 error
= copyout((caddr_t
)&len
, (caddr_t
)uap
->alen
,
1381 getsockname(p
, uap
, retval
)
1383 struct getsockname_args
*uap
;
1387 return (getsockname1(p
, uap
, retval
, 0));
1390 #ifdef COMPAT_OLDSOCK
1392 ogetsockname(p
, uap
, retval
)
1394 struct getsockname_args
*uap
;
1398 return (getsockname1(p
, uap
, retval
, 1));
1400 #endif /* COMPAT_OLDSOCK */
1403 * Get name of peer for connected socket.
1407 getpeername1(p
, uap
, retval
, compat
)
1409 register struct getpeername_args
*uap
;
1414 register struct socket
*so
;
1415 struct sockaddr
*sa
;
1419 error
= getsock(p
->p_fd
, uap
->fdes
, &fp
);
1422 so
= (struct socket
*)fp
->f_data
;
1423 if ((so
->so_state
& (SS_ISCONNECTED
|SS_ISCONFIRMING
)) == 0)
1425 error
= copyin((caddr_t
)uap
->alen
, (caddr_t
)&len
, sizeof (len
));
1429 error
= (*so
->so_proto
->pr_usrreqs
->pru_peeraddr
)(so
, &sa
);
1436 len
= MIN(len
, sa
->sa_len
);
1437 #ifdef COMPAT_OLDSOCK
1439 ((struct osockaddr
*)sa
)->sa_family
=
1442 error
= copyout(sa
, (caddr_t
)uap
->asa
, (u_int
)len
);
1446 error
= copyout((caddr_t
)&len
, (caddr_t
)uap
->alen
, sizeof (len
));
1448 if (sa
) FREE(sa
, M_SONAME
);
1453 getpeername(p
, uap
, retval
)
1455 struct getpeername_args
*uap
;
1459 return (getpeername1(p
, uap
, retval
, 0));
1462 #ifdef COMPAT_OLDSOCK
1464 ogetpeername(p
, uap
, retval
)
1466 struct ogetpeername_args
*uap
;
1470 /* XXX uap should have type `getpeername_args *' to begin with. */
1471 return (getpeername1(p
, (struct getpeername_args
*)uap
, retval
, 1));
1473 #endif /* COMPAT_OLDSOCK */
1476 sockargs(mp
, buf
, buflen
, type
)
1481 register struct sockaddr
*sa
;
1482 register struct mbuf
*m
;
1485 if ((u_int
)buflen
> MLEN
) {
1486 #ifdef COMPAT_OLDSOCK
1487 if (type
== MT_SONAME
&& (u_int
)buflen
<= 112)
1488 buflen
= MLEN
; /* unix domain compat. hack */
1493 m
= m_get(M_WAIT
, type
);
1497 error
= copyin(buf
, mtod(m
, caddr_t
), (u_int
)buflen
);
1502 if (type
== MT_SONAME
) {
1503 sa
= mtod(m
, struct sockaddr
*);
1505 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1506 if (sa
->sa_family
== 0 && sa
->sa_len
< AF_MAX
)
1507 sa
->sa_family
= sa
->sa_len
;
1509 sa
->sa_len
= buflen
;
1516 getsockaddr(namp
, uaddr
, len
)
1517 struct sockaddr
**namp
;
1521 struct sockaddr
*sa
;
1524 if (len
> SOCK_MAXADDRLEN
)
1525 return ENAMETOOLONG
;
1530 MALLOC(sa
, struct sockaddr
*, len
, M_SONAME
, M_WAITOK
);
1531 error
= copyin(uaddr
, sa
, len
);
1535 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1536 if (sa
->sa_family
== 0 && sa
->sa_len
< AF_MAX
)
1537 sa
->sa_family
= sa
->sa_len
;
1546 getsock(fdp
, fdes
, fpp
)
1547 struct filedesc
*fdp
;
1551 register struct file
*fp
;
1553 if ((unsigned)fdes
>= fdp
->fd_nfiles
||
1554 (fp
= fdp
->fd_ofiles
[fdes
]) == NULL
||
1555 (fdp
->fd_ofileflags
[fdes
] & UF_RESERVED
))
1557 if (fp
->f_type
!= DTYPE_SOCKET
)
1565 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
1566 * XXX - The sf_buf functions are currently private to sendfile(2), so have
1567 * been made static, but may be useful in the future for doing zero-copy in
1568 * other parts of the networking code.
1571 sf_buf_init(void *arg
)
1575 SLIST_INIT(&sf_freelist
);
1576 sf_base
= kmem_alloc_pageable(kernel_map
, nsfbufs
* PAGE_SIZE
);
1577 sf_bufs
= _MALLOC(nsfbufs
* sizeof(struct sf_buf
), M_TEMP
, M_NOWAIT
);
1578 bzero(sf_bufs
, nsfbufs
* sizeof(struct sf_buf
));
1579 for (i
= 0; i
< nsfbufs
; i
++) {
1580 sf_bufs
[i
].kva
= sf_base
+ i
* PAGE_SIZE
;
1581 SLIST_INSERT_HEAD(&sf_freelist
, &sf_bufs
[i
], free_list
);
1586 * Get an sf_buf from the freelist. Will block if none are available.
1588 static struct sf_buf
*
1595 while ((sf
= SLIST_FIRST(&sf_freelist
)) == NULL
) {
1596 sf_buf_alloc_want
= 1;
1597 tsleep(&sf_freelist
, PVM
, "sfbufa", 0);
1599 SLIST_REMOVE_HEAD(&sf_freelist
, free_list
);
1605 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
1607 sf_buf_ref(caddr_t addr
, u_int size
)
1612 if (sf
->refcnt
== 0)
1613 panic("sf_buf_ref: referencing a free sf_buf");
1618 * Lose a reference to an sf_buf. When none left, detach mapped page
1619 * and release resources back to the system.
1621 * Must be called at splimp.
1624 sf_buf_free(caddr_t addr
, u_int size
)
1631 if (sf
->refcnt
== 0)
1632 panic("sf_buf_free: freeing free sf_buf");
1634 if (sf
->refcnt
== 0) {
1635 pmap_qremove((vm_offset_t
)addr
, 1);
1638 vm_page_unwire(m
, 0);
1640 * Check for the object going away on us. This can
1641 * happen since we don't hold a reference to it.
1642 * If so, we're responsible for freeing the page.
1644 if (m
->wire_count
== 0 && m
->object
== NULL
)
1645 vm_page_lock_queues();
1647 vm_page_unlock_queues();
1650 SLIST_INSERT_HEAD(&sf_freelist
, sf
, free_list
);
1651 if (sf_buf_alloc_want
) {
1652 sf_buf_alloc_want
= 0;
1653 wakeup(&sf_freelist
);
1660 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1661 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1663 * Send a file specified by 'fd' and starting at 'offset' to a socket
1664 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1665 * nbytes == 0. Optionally add a header and/or trailer to the socket
1666 * output. If specified, write the total number of bytes sent into *sbytes.
1669 sendfile(struct proc
*p
, struct sendfile_args
*uap
)
1672 struct filedesc
*fdp
= p
->p_fd
;
1674 struct vm_object
*obj
;
1679 struct writev_args nuap
;
1680 struct sf_hdtr hdtr
;
1681 off_t off
, xfsize
, sbytes
= 0;
1685 * Do argument checking. Must be a regular file in, stream
1686 * type and connected socket out, positive offset.
1688 if (((u_int
)uap
->fd
) >= fdp
->fd_nfiles
||
1689 (fp
= fdp
->fd_ofiles
[uap
->fd
]) == NULL
||
1690 (fp
->f_flag
& FREAD
) == 0) {
1694 if (fp
->f_type
!= DTYPE_VNODE
) {
1698 vp
= (struct vnode
*)fp
->f_data
;
1700 if (vp
->v_type
!= VREG
|| obj
== NULL
) {
1704 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1707 so
= (struct socket
*)fp
->f_data
;
1708 if (so
->so_type
!= SOCK_STREAM
) {
1712 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
1716 if (uap
->offset
< 0) {
1722 * If specified, get the pointer to the sf_hdtr struct for
1723 * any headers/trailers.
1725 if (uap
->hdtr
!= NULL
) {
1726 error
= copyin(uap
->hdtr
, &hdtr
, sizeof(hdtr
));
1730 * Send any headers. Wimp out and use writev(2).
1732 if (hdtr
.headers
!= NULL
) {
1734 nuap
.iovp
= hdtr
.headers
;
1735 nuap
.iovcnt
= hdtr
.hdr_cnt
;
1736 error
= writev(p
, &nuap
);
1739 sbytes
+= p
->p_retval
[0];
1744 * Protect against multiple writers to the socket.
1746 (void) sblock(&so
->so_snd
, M_WAIT
);
1749 * Loop through the pages in the file, starting with the requested
1750 * offset. Get a file page (do I/O if necessary), map the file page
1751 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1754 for (off
= uap
->offset
; ; off
+= xfsize
, sbytes
+= xfsize
) {
1755 vm_object_offset_t pindex
;
1756 vm_object_offset_t pgoff
;
1758 pindex
= OFF_TO_IDX(off
);
1761 * Calculate the amount to transfer. Not to exceed a page,
1762 * the EOF, or the passed in nbytes.
1764 xfsize
= obj
->un_pager
.vnp
.vnp_size
- off
;
1765 if (xfsize
> PAGE_SIZE_64
)
1767 pgoff
= (vm_object_offset_t
)(off
& PAGE_MASK_64
);
1768 if (PAGE_SIZE
- pgoff
< xfsize
)
1769 xfsize
= PAGE_SIZE_64
- pgoff
;
1770 if (uap
->nbytes
&& xfsize
> (uap
->nbytes
- sbytes
))
1771 xfsize
= uap
->nbytes
- sbytes
;
1775 * Optimize the non-blocking case by looking at the socket space
1776 * before going to the extra work of constituting the sf_buf.
1778 if ((so
->so_state
& SS_NBIO
) && sbspace(&so
->so_snd
) <= 0) {
1779 if (so
->so_state
& SS_CANTSENDMORE
)
1783 sbunlock(&so
->so_snd
);
1787 * Attempt to look up the page. If the page doesn't exist or the
1788 * part we're interested in isn't valid, then read it from disk.
1789 * If some other part of the kernel has this page (i.e. it's busy),
1790 * then disk I/O may be occuring on it, so wait and retry.
1792 pg
= vm_page_lookup(obj
, pindex
);
1793 if (pg
== NULL
|| (!(pg
->flags
& PG_BUSY
) && !pg
->busy
&&
1794 !vm_page_is_valid(pg
, pgoff
, xfsize
))) {
1800 pg
= vm_page_alloc(obj
, pindex
, VM_ALLOC_NORMAL
);
1806 * don't just clear PG_BUSY manually -
1807 * vm_page_alloc() should be considered opaque,
1808 * use the VM routine provided to clear
1815 * Ensure that our page is still around when the I/O completes.
1817 vm_page_io_start(pg
);
1820 * Get the page from backing store.
1822 bsize
= vp
->v_mount
->mnt_stat
.f_iosize
;
1823 auio
.uio_iov
= &aiov
;
1824 auio
.uio_iovcnt
= 1;
1826 aiov
.iov_len
= MAXBSIZE
;
1827 auio
.uio_resid
= MAXBSIZE
;
1828 auio
.uio_offset
= trunc_page(off
);
1829 auio
.uio_segflg
= UIO_NOCOPY
;
1830 auio
.uio_rw
= UIO_READ
;
1832 vn_lock(vp
, LK_SHARED
| LK_NOPAUSE
| LK_RETRY
, p
);
1833 error
= VOP_READ(vp
, &auio
, IO_VMIO
| ((MAXBSIZE
/ bsize
) << 16),
1835 VOP_UNLOCK(vp
, 0, p
);
1836 vm_page_flag_clear(pg
, PG_ZERO
);
1837 vm_page_io_finish(pg
);
1839 vm_page_unwire(pg
, 0);
1841 * See if anyone else might know about this page.
1842 * If not and it is not valid, then free it.
1844 if (pg
->wire_count
== 0 && pg
->valid
== 0 &&
1845 pg
->busy
== 0 && !(pg
->flags
& PG_BUSY
) &&
1846 pg
->hold_count
== 0)
1847 vm_page_lock_queues();
1849 vm_page_unlock_queues();
1850 sbunlock(&so
->so_snd
);
1854 if ((pg
->flags
& PG_BUSY
) || pg
->busy
) {
1856 if ((pg
->flags
& PG_BUSY
) || pg
->busy
) {
1858 * Page is busy. Wait and retry.
1860 vm_page_flag_set(pg
, PG_WANTED
);
1861 tsleep(pg
, PVM
, "sfpbsy", 0);
1868 * Protect from having the page ripped out from beneath us.
1873 * Allocate a kernel virtual page and insert the physical page
1876 sf
= sf_buf_alloc();
1878 pmap_qenter(sf
->kva
, &pg
, 1);
1880 * Get an mbuf header and set it up as having external storage.
1882 MGETHDR(m
, M_WAIT
, MT_DATA
);
1883 m
->m_ext
.ext_free
= sf_buf_free
;
1884 m
->m_ext
.ext_ref
= sf_buf_ref
;
1885 m
->m_ext
.ext_buf
= (void *)sf
->kva
;
1886 m
->m_ext
.ext_size
= PAGE_SIZE
;
1887 m
->m_data
= (char *) sf
->kva
+ pgoff
;
1888 m
->m_flags
|= M_EXT
;
1889 m
->m_pkthdr
.len
= m
->m_len
= xfsize
;
1891 * Add the buffer to the socket buffer chain.
1896 * Make sure that the socket is still able to take more data.
1897 * CANTSENDMORE being true usually means that the connection
1898 * was closed. so_error is true when an error was sensed after
1900 * The state is checked after the page mapping and buffer
1901 * allocation above since those operations may block and make
1902 * any socket checks stale. From this point forward, nothing
1903 * blocks before the pru_send (or more accurately, any blocking
1904 * results in a loop back to here to re-check).
1906 if ((so
->so_state
& SS_CANTSENDMORE
) || so
->so_error
) {
1907 if (so
->so_state
& SS_CANTSENDMORE
) {
1910 error
= so
->so_error
;
1914 sbunlock(&so
->so_snd
);
1919 * Wait for socket space to become available. We do this just
1920 * after checking the connection state above in order to avoid
1921 * a race condition with sbwait().
1923 if (sbspace(&so
->so_snd
) < so
->so_snd
.sb_lowat
) {
1924 if (so
->so_state
& SS_NBIO
) {
1926 sbunlock(&so
->so_snd
);
1931 error
= sbwait(&so
->so_snd
);
1933 * An error from sbwait usually indicates that we've
1934 * been interrupted by a signal. If we've sent anything
1935 * then return bytes sent, otherwise return the error.
1939 sbunlock(&so
->so_snd
);
1945 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)(so
, 0, m
, 0, 0, p
);
1948 sbunlock(&so
->so_snd
);
1952 sbunlock(&so
->so_snd
);
1955 * Send trailers. Wimp out and use writev(2).
1957 if (uap
->hdtr
!= NULL
&& hdtr
.trailers
!= NULL
) {
1959 nuap
.iovp
= hdtr
.trailers
;
1960 nuap
.iovcnt
= hdtr
.trl_cnt
;
1961 error
= writev(p
, &nuap
);
1964 sbytes
+= p
->p_retval
[0];
1968 if (uap
->sbytes
!= NULL
) {
1969 copyout(&sbytes
, uap
->sbytes
, sizeof(off_t
));