2 * Copyright (c) 2000-2001 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@
26 * Copyright (c) 1982, 1986, 1989, 1990, 1993
27 * The Regents of the University of California. All rights reserved.
29 * sendfile(2) and related extensions:
30 * Copyright (c) 1998, David Greenman. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/filedesc.h>
71 #include <sys/malloc.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
77 #include <sys/ktrace.h>
79 #include <sys/kernel.h>
81 #include <sys/kdebug.h>
85 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0)
86 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2)
87 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1)
88 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3)
89 #define DBG_FNC_SENDMSG NETDBG_CODE(DBG_NETSOCK, (1 << 8) | 1)
90 #define DBG_FNC_SENDTO NETDBG_CODE(DBG_NETSOCK, (2 << 8) | 1)
91 #define DBG_FNC_SENDIT NETDBG_CODE(DBG_NETSOCK, (3 << 8) | 1)
92 #define DBG_FNC_RECVFROM NETDBG_CODE(DBG_NETSOCK, (5 << 8))
93 #define DBG_FNC_RECVMSG NETDBG_CODE(DBG_NETSOCK, (6 << 8))
94 #define DBG_FNC_RECVIT NETDBG_CODE(DBG_NETSOCK, (7 << 8))
98 struct getsockname_args
{
104 struct getsockopt_args
{
118 struct getpeername_args
{
128 static void sf_buf_init(void *arg
);
129 SYSINIT(sock_sf
, SI_SUB_MBUF
, SI_ORDER_ANY
, sf_buf_init
, NULL
)
130 static struct sf_buf
*sf_buf_alloc(void);
131 static void sf_buf_ref(caddr_t addr
, u_int size
);
132 static void sf_buf_free(caddr_t addr
, u_int size
);
134 static SLIST_HEAD(, sf_buf
) sf_freelist
;
135 static vm_offset_t sf_base
;
136 static struct sf_buf
*sf_bufs
;
137 static int sf_buf_alloc_want
;
140 static int sendit
__P((struct proc
*p
, int s
, struct msghdr
*mp
, int flags
, register_t
*retval
));
141 static int recvit
__P((struct proc
*p
, int s
, struct msghdr
*mp
,
142 caddr_t namelenp
, register_t
*retval
));
144 static int accept1
__P((struct proc
*p
, struct accept_args
*uap
, register_t
*retval
, int compat
));
145 static int getsockname1
__P((struct proc
*p
, struct getsockname_args
*uap
,
146 register_t
*retval
, int compat
));
147 static int getpeername1
__P((struct proc
*p
, struct getpeername_args
*uap
,
148 register_t
*retval
, int compat
));
151 * System call interface to the socket abstraction.
153 #if COMPAT_43 || defined(COMPAT_SUNOS)
154 #define COMPAT_OLDSOCK
157 extern struct fileops socketops
;
165 socket(p
, uap
, retval
)
167 register struct socket_args
*uap
;
170 struct filedesc
*fdp
= p
->p_fd
;
175 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
176 error
= falloc(p
, &fp
, &fd
);
177 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
181 fp
->f_flag
= FREAD
|FWRITE
;
182 fp
->f_type
= DTYPE_SOCKET
;
183 fp
->f_ops
= &socketops
;
184 if (error
= socreate(uap
->domain
, &so
, uap
->type
,
186 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
189 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
191 fp
->f_data
= (caddr_t
)so
;
192 *fdflags(p
, fd
) &= ~UF_RESERVED
;
208 register struct bind_args
*uap
;
215 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
218 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
221 error
= sobind((struct socket
*)fp
->f_data
, sa
);
234 listen(p
, uap
, retval
)
236 register struct listen_args
*uap
;
242 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
245 return (solisten((struct socket
*)fp
->f_data
, uap
->backlog
));
248 #ifndef COMPAT_OLDSOCK
249 #define accept1 accept
255 accept1(p
, uap
, retval
, compat
)
257 register struct accept_args
*uap
;
265 struct socket
*head
, *so
;
267 short fflag
; /* type must match fp->f_flag */
271 error
= copyin((caddr_t
)uap
->anamelen
, (caddr_t
)&namelen
,
276 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
280 head
= (struct socket
*)fp
->f_data
;
281 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
285 if ((head
->so_state
& SS_NBIO
) && head
->so_comp
.tqh_first
== NULL
) {
287 return (EWOULDBLOCK
);
289 while (TAILQ_EMPTY(&head
->so_comp
) && head
->so_error
== 0) {
290 if (head
->so_state
& SS_CANTRCVMORE
) {
291 head
->so_error
= ECONNABORTED
;
294 error
= tsleep((caddr_t
)&head
->so_timeo
, PSOCK
| PCATCH
,
301 if (head
->so_error
) {
302 error
= head
->so_error
;
310 * At this point we know that there is at least one connection
311 * ready to be accepted. Remove it from the queue prior to
312 * allocating the file descriptor for it since falloc() may
313 * block allowing another process to accept the connection
316 so
= TAILQ_FIRST(&head
->so_comp
);
317 TAILQ_REMOVE(&head
->so_comp
, so
, so_list
);
321 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
322 error
= falloc(p
, &fp
, &fd
);
323 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
326 * Probably ran out of file descriptors. Put the
327 * unaccepted connection back onto the queue and
328 * do another wakeup so some other process might
329 * have a chance at it.
331 TAILQ_INSERT_HEAD(&head
->so_comp
, so
, so_list
);
333 wakeup_one(&head
->so_timeo
);
337 *fdflags(p
, fd
) &= ~UF_RESERVED
;
341 so
->so_state
&= ~SS_COMP
;
343 fp
->f_type
= DTYPE_SOCKET
;
345 fp
->f_ops
= &socketops
;
346 fp
->f_data
= (caddr_t
)so
;
348 (void) soaccept(so
, &sa
);
356 /* check sa_len before it is destroyed */
357 if (namelen
> sa
->sa_len
)
358 namelen
= sa
->sa_len
;
359 #ifdef COMPAT_OLDSOCK
361 ((struct osockaddr
*)sa
)->sa_family
=
364 error
= copyout(sa
, (caddr_t
)uap
->name
, (u_int
)namelen
);
367 error
= copyout((caddr_t
)&namelen
,
368 (caddr_t
)uap
->anamelen
, sizeof (*uap
->anamelen
));
376 accept(p
, uap
, retval
)
378 struct accept_args
*uap
;
382 return (accept1(p
, uap
, retval
, 0));
385 #ifdef COMPAT_OLDSOCK
387 oaccept(p
, uap
, retval
)
389 struct accept_args
*uap
;
393 return (accept1(p
, uap
, retval
, 1));
395 #endif /* COMPAT_OLDSOCK */
397 struct connect_args
{
404 connect(p
, uap
, retval
)
406 register struct connect_args
*uap
;
410 register struct socket
*so
;
414 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
417 so
= (struct socket
*)fp
->f_data
;
418 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
))
420 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
423 error
= soconnect(so
, sa
);
426 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
428 return (EINPROGRESS
);
431 while ((so
->so_state
& SS_ISCONNECTING
) && so
->so_error
== 0) {
432 error
= tsleep((caddr_t
)&so
->so_timeo
, PSOCK
| PCATCH
,
438 error
= so
->so_error
;
443 so
->so_state
&= ~SS_ISCONNECTING
;
445 if (error
== ERESTART
)
450 struct socketpair_args
{
457 socketpair(p
, uap
, retval
)
459 register struct socketpair_args
*uap
;
462 register struct filedesc
*fdp
= p
->p_fd
;
463 struct file
*fp1
, *fp2
;
464 struct socket
*so1
, *so2
;
465 int fd
, error
, sv
[2];
467 error
= socreate(uap
->domain
, &so1
, uap
->type
, uap
->protocol
);
470 error
= socreate(uap
->domain
, &so2
, uap
->type
, uap
->protocol
);
473 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
474 error
= falloc(p
, &fp1
, &fd
);
478 fp1
->f_flag
= FREAD
|FWRITE
;
479 fp1
->f_type
= DTYPE_SOCKET
;
480 fp1
->f_ops
= &socketops
;
481 fp1
->f_data
= (caddr_t
)so1
;
482 error
= falloc(p
, &fp2
, &fd
);
485 fp2
->f_flag
= FREAD
|FWRITE
;
486 fp2
->f_type
= DTYPE_SOCKET
;
487 fp2
->f_ops
= &socketops
;
488 fp2
->f_data
= (caddr_t
)so2
;
490 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
491 error
= soconnect2(so1
, so2
);
493 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
497 if (uap
->type
== SOCK_DGRAM
) {
499 * Datagram socket connection is asymmetric.
501 error
= soconnect2(so2
, so1
);
503 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
507 *fdflags(p
, sv
[0]) &= ~UF_RESERVED
;
508 *fdflags(p
, sv
[1]) &= ~UF_RESERVED
;
509 error
= copyout((caddr_t
)sv
, (caddr_t
)uap
->rsv
,
511 #if 0 /* old pipe(2) syscall compatability, unused these days */
512 retval
[0] = sv
[0]; /* XXX ??? */
513 retval
[1] = sv
[1]; /* XXX ??? */
523 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
531 sendit(p
, s
, mp
, flags
, retsize
)
532 register struct proc
*p
;
534 register struct msghdr
*mp
;
540 register struct iovec
*iov
;
542 struct mbuf
*control
;
547 struct iovec
*ktriov
= NULL
;
551 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_START
, 0,0,0,0,0);
553 if (error
= getsock(p
->p_fd
, s
, &fp
))
555 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
559 auio
.uio_iov
= mp
->msg_iov
;
560 auio
.uio_iovcnt
= mp
->msg_iovlen
;
561 auio
.uio_segflg
= UIO_USERSPACE
;
562 auio
.uio_rw
= UIO_WRITE
;
564 auio
.uio_offset
= 0; /* XXX */
567 for (i
= 0; i
< mp
->msg_iovlen
; i
++, iov
++) {
568 if (iov
->iov_len
< 0)
570 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
574 if ((auio
.uio_resid
+= iov
->iov_len
) < 0)
576 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
581 error
= getsockaddr(&to
, mp
->msg_name
, mp
->msg_namelen
);
583 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
588 if (mp
->msg_control
) {
589 if (mp
->msg_controllen
< sizeof(struct cmsghdr
)
590 #ifdef COMPAT_OLDSOCK
591 && mp
->msg_flags
!= MSG_COMPAT
597 error
= sockargs(&control
, mp
->msg_control
,
598 mp
->msg_controllen
, MT_CONTROL
);
601 #ifdef COMPAT_OLDSOCK
602 if (mp
->msg_flags
== MSG_COMPAT
) {
603 register struct cmsghdr
*cm
;
605 M_PREPEND(control
, sizeof(*cm
), M_WAIT
);
610 cm
= mtod(control
, struct cmsghdr
*);
611 cm
->cmsg_len
= control
->m_len
;
612 cm
->cmsg_level
= SOL_SOCKET
;
613 cm
->cmsg_type
= SCM_RIGHTS
;
621 if (KTRPOINT(p
, KTR_GENIO
)) {
622 int iovlen
= auio
.uio_iovcnt
* sizeof (struct iovec
);
624 MALLOC(ktriov
, struct iovec
*, iovlen
, M_TEMP
, M_WAITOK
);
625 bcopy((caddr_t
)auio
.uio_iov
, (caddr_t
)ktriov
, iovlen
);
629 len
= auio
.uio_resid
;
630 so
= (struct socket
*)fp
->f_data
;
631 error
= so
->so_proto
->pr_usrreqs
->pru_sosend(so
, to
, &auio
, 0, control
,
634 if (auio
.uio_resid
!= len
&& (error
== ERESTART
||
635 error
== EINTR
|| error
== EWOULDBLOCK
))
637 /* Generation of SIGPIPE can be controlled per socket */
638 if (error
== EPIPE
&& !(so
->so_flags
& SOF_NOSIGPIPE
))
642 *retsize
= len
- auio
.uio_resid
;
644 if (ktriov
!= NULL
) {
646 ktruio
.uio_iov
= ktriov
;
647 ktruio
.uio_resid
= retsize
[0];
648 ktrgenio(p
->p_tracep
, s
, UIO_WRITE
, &ktruio
, error
, -1);
650 FREE(ktriov
, M_TEMP
);
656 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
671 sendto(p
, uap
, retval
)
673 register struct sendto_args
/* {
688 KERNEL_DEBUG(DBG_FNC_SENDTO
| DBG_FUNC_START
, 0,0,0,0,0);
690 msg
.msg_name
= uap
->to
;
691 msg
.msg_namelen
= uap
->tolen
;
695 #ifdef COMPAT_OLDSOCK
698 aiov
.iov_base
= uap
->buf
;
699 aiov
.iov_len
= uap
->len
;
700 stat
= sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
);
701 KERNEL_DEBUG(DBG_FNC_SENDTO
| DBG_FUNC_END
, stat
, *retval
,0,0,0);
705 #ifdef COMPAT_OLDSOCK
714 osend(p
, uap
, retval
)
716 register struct osend_args
/* {
732 aiov
.iov_base
= uap
->buf
;
733 aiov
.iov_len
= uap
->len
;
736 return (sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
));
738 struct osendmsg_args
{
745 osendmsg(p
, uap
, retval
)
747 register struct osendmsg_args
/* {
756 struct iovec aiov
[UIO_SMALLIOV
], *iov
;
759 error
= copyin(uap
->msg
, (caddr_t
)&msg
, sizeof (struct omsghdr
));
762 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
763 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
)
765 MALLOC(iov
, struct iovec
*,
766 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
770 error
= copyin((caddr_t
)msg
.msg_iov
, (caddr_t
)iov
,
771 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)));
774 msg
.msg_flags
= MSG_COMPAT
;
776 error
= sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
);
784 struct sendmsg_args
{
791 sendmsg(p
, uap
, retval
)
793 register struct sendmsg_args
*uap
;
797 struct iovec aiov
[UIO_SMALLIOV
], *iov
;
800 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_START
, 0,0,0,0,0);
801 if (error
= copyin(uap
->msg
, (caddr_t
)&msg
, sizeof (msg
)))
803 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, error
,0,0,0,0);
807 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
808 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
) {
809 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, EMSGSIZE
,0,0,0,0);
812 MALLOC(iov
, struct iovec
*,
813 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
817 if (msg
.msg_iovlen
&&
818 (error
= copyin((caddr_t
)msg
.msg_iov
, (caddr_t
)iov
,
819 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)))))
822 #ifdef COMPAT_OLDSOCK
825 error
= sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
);
829 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, error
,0,0,0,0);
834 recvit(p
, s
, mp
, namelenp
, retval
)
835 register struct proc
*p
;
837 register struct msghdr
*mp
;
843 register struct iovec
*iov
;
846 struct mbuf
*m
, *control
= 0;
849 struct sockaddr
*fromsa
= 0;
851 struct iovec
*ktriov
= NULL
;
855 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_START
, 0,0,0,0,0);
856 if (error
= getsock(p
->p_fd
, s
, &fp
))
858 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, error
,0,0,0,0);
862 auio
.uio_iov
= mp
->msg_iov
;
863 auio
.uio_iovcnt
= mp
->msg_iovlen
;
864 auio
.uio_segflg
= UIO_USERSPACE
;
865 auio
.uio_rw
= UIO_READ
;
867 auio
.uio_offset
= 0; /* XXX */
870 for (i
= 0; i
< mp
->msg_iovlen
; i
++, iov
++) {
871 if ((auio
.uio_resid
+= iov
->iov_len
) < 0) {
872 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
877 if (KTRPOINT(p
, KTR_GENIO
)) {
878 int iovlen
= auio
.uio_iovcnt
* sizeof (struct iovec
);
880 MALLOC(ktriov
, struct iovec
*, iovlen
, M_TEMP
, M_WAITOK
);
881 bcopy((caddr_t
)auio
.uio_iov
, (caddr_t
)ktriov
, iovlen
);
885 len
= auio
.uio_resid
;
886 so
= (struct socket
*)fp
->f_data
;
887 error
= so
->so_proto
->pr_usrreqs
->pru_soreceive(so
, &fromsa
, &auio
,
888 (struct mbuf
**)0, mp
->msg_control
? &control
: (struct mbuf
**)0,
891 if (auio
.uio_resid
!= len
&& (error
== ERESTART
||
892 error
== EINTR
|| error
== EWOULDBLOCK
))
896 if (ktriov
!= NULL
) {
898 ktruio
.uio_iov
= ktriov
;
899 ktruio
.uio_resid
= len
- auio
.uio_resid
;
900 ktrgenio(p
->p_tracep
, s
, UIO_WRITE
, &ktruio
, error
, -1);
902 FREE(ktriov
, M_TEMP
);
907 *retval
= len
- auio
.uio_resid
;
909 len
= mp
->msg_namelen
;
910 if (len
<= 0 || fromsa
== 0)
914 #define MIN(a,b) ((a)>(b)?(b):(a))
916 /* save sa_len before it is destroyed by MSG_COMPAT */
917 len
= MIN(len
, fromsa
->sa_len
);
918 #ifdef COMPAT_OLDSOCK
919 if (mp
->msg_flags
& MSG_COMPAT
)
920 ((struct osockaddr
*)fromsa
)->sa_family
=
923 error
= copyout(fromsa
,
924 (caddr_t
)mp
->msg_name
, (unsigned)len
);
928 mp
->msg_namelen
= len
;
930 (error
= copyout((caddr_t
)&len
, namelenp
, sizeof (int)))) {
931 #ifdef COMPAT_OLDSOCK
932 if (mp
->msg_flags
& MSG_COMPAT
)
933 error
= 0; /* old recvfrom didn't check */
939 if (mp
->msg_control
) {
940 #ifdef COMPAT_OLDSOCK
942 * We assume that old recvmsg calls won't receive access
943 * rights and other control info, esp. as control info
944 * is always optional and those options didn't exist in 4.3.
945 * If we receive rights, trim the cmsghdr; anything else
948 if (control
&& mp
->msg_flags
& MSG_COMPAT
) {
949 if (mtod(control
, struct cmsghdr
*)->cmsg_level
!=
951 mtod(control
, struct cmsghdr
*)->cmsg_type
!=
953 mp
->msg_controllen
= 0;
956 control
->m_len
-= sizeof (struct cmsghdr
);
957 control
->m_data
+= sizeof (struct cmsghdr
);
960 len
= mp
->msg_controllen
;
962 mp
->msg_controllen
= 0;
963 ctlbuf
= (caddr_t
) mp
->msg_control
;
965 while (m
&& len
> 0) {
971 mp
->msg_flags
|= MSG_CTRUNC
;
975 if (error
= copyout((caddr_t
)mtod(m
, caddr_t
),
983 mp
->msg_controllen
= ctlbuf
- mp
->msg_control
;
987 FREE(fromsa
, M_SONAME
);
990 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, error
,0,0,0,0);
995 struct recvfrom_args
{
1005 recvfrom(p
, uap
, retval
)
1007 register struct recvfrom_args
/* {
1021 KERNEL_DEBUG(DBG_FNC_RECVFROM
| DBG_FUNC_START
, 0,0,0,0,0);
1023 if (uap
->fromlenaddr
) {
1024 error
= copyin((caddr_t
)uap
->fromlenaddr
,
1025 (caddr_t
)&msg
.msg_namelen
, sizeof (msg
.msg_namelen
));
1029 msg
.msg_namelen
= 0;
1030 msg
.msg_name
= uap
->from
;
1031 msg
.msg_iov
= &aiov
;
1033 aiov
.iov_base
= uap
->buf
;
1034 aiov
.iov_len
= uap
->len
;
1035 msg
.msg_control
= 0;
1036 msg
.msg_flags
= uap
->flags
;
1037 KERNEL_DEBUG(DBG_FNC_RECVFROM
| DBG_FUNC_END
, error
,0,0,0,0);
1038 return (recvit(p
, uap
->s
, &msg
, (caddr_t
)uap
->fromlenaddr
, retval
));
1041 #ifdef COMPAT_OLDSOCK
1043 orecvfrom(p
, uap
, retval
)
1045 struct recvfrom_args
*uap
;
1049 uap
->flags
|= MSG_COMPAT
;
1050 return (recvfrom(p
, uap
));
1055 #ifdef COMPAT_OLDSOCK
1064 orecv(p
, uap
, retval
)
1066 struct orecv_args
*uap
;
1073 msg
.msg_namelen
= 0;
1074 msg
.msg_iov
= &aiov
;
1076 aiov
.iov_base
= uap
->buf
;
1077 aiov
.iov_len
= uap
->len
;
1078 msg
.msg_control
= 0;
1079 msg
.msg_flags
= uap
->flags
;
1080 return (recvit(p
, uap
->s
, &msg
, (caddr_t
)0, retval
));
1084 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1085 * overlays the new one, missing only the flags, and with the (old) access
1086 * rights where the control fields are now.
1088 struct orecvmsg_args
{
1090 struct omsghdr
*msg
;
1095 orecvmsg(p
, uap
, retval
)
1097 struct orecvmsg_args
*uap
;
1101 struct iovec aiov
[UIO_SMALLIOV
], *iov
;
1104 error
= copyin((caddr_t
)uap
->msg
, (caddr_t
)&msg
,
1105 sizeof (struct omsghdr
));
1108 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
1109 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
)
1111 MALLOC(iov
, struct iovec
*,
1112 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
1116 msg
.msg_flags
= uap
->flags
| MSG_COMPAT
;
1117 error
= copyin((caddr_t
)msg
.msg_iov
, (caddr_t
)iov
,
1118 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)));
1122 error
= recvit(p
, uap
->s
, &msg
, (caddr_t
)&uap
->msg
->msg_namelen
, retval
);
1124 if (msg
.msg_controllen
&& error
== 0)
1125 error
= copyout((caddr_t
)&msg
.msg_controllen
,
1126 (caddr_t
)&uap
->msg
->msg_accrightslen
, sizeof (int));
1134 struct recvmsg_args
{
1141 recvmsg(p
, uap
, retval
)
1143 struct recvmsg_args
*uap
;
1147 struct iovec aiov
[UIO_SMALLIOV
], *uiov
, *iov
;
1150 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_START
, 0,0,0,0,0);
1151 if (error
= copyin((caddr_t
)uap
->msg
, (caddr_t
)&msg
,
1154 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, error
,0,0,0,0);
1158 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
1159 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
) {
1160 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, EMSGSIZE
,0,0,0,0);
1163 MALLOC(iov
, struct iovec
*,
1164 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
1168 #ifdef COMPAT_OLDSOCK
1169 msg
.msg_flags
= uap
->flags
&~ MSG_COMPAT
;
1171 msg
.msg_flags
= uap
->flags
;
1175 error
= copyin((caddr_t
)uiov
, (caddr_t
)iov
,
1176 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)));
1179 error
= recvit(p
, uap
->s
, &msg
, (caddr_t
)0, retval
);
1182 error
= copyout((caddr_t
)&msg
, (caddr_t
)uap
->msg
, sizeof(msg
));
1187 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, error
,0,0,0,0);
1192 struct shutdown_args
{
1198 shutdown(p
, uap
, retval
)
1200 struct shutdown_args
*uap
;
1206 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1209 return (soshutdown((struct socket
*)fp
->f_data
, uap
->how
));
1217 struct setsockopt_args
{
1226 setsockopt(p
, uap
, retval
)
1228 struct setsockopt_args
*uap
;
1232 struct sockopt sopt
;
1235 if (uap
->val
== 0 && uap
->valsize
!= 0)
1237 if (uap
->valsize
< 0)
1240 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1244 sopt
.sopt_dir
= SOPT_SET
;
1245 sopt
.sopt_level
= uap
->level
;
1246 sopt
.sopt_name
= uap
->name
;
1247 sopt
.sopt_val
= uap
->val
;
1248 sopt
.sopt_valsize
= uap
->valsize
;
1251 return (sosetopt((struct socket
*)fp
->f_data
, &sopt
));
1257 getsockopt(p
, uap
, retval
)
1259 struct getsockopt_args
*uap
;
1264 struct sockopt sopt
;
1266 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1270 error
= copyin((caddr_t
)uap
->avalsize
, (caddr_t
)&valsize
,
1279 sopt
.sopt_dir
= SOPT_GET
;
1280 sopt
.sopt_level
= uap
->level
;
1281 sopt
.sopt_name
= uap
->name
;
1282 sopt
.sopt_val
= uap
->val
;
1283 sopt
.sopt_valsize
= (size_t)valsize
; /* checked non-negative above */
1286 error
= sogetopt((struct socket
*)fp
->f_data
, &sopt
);
1288 valsize
= sopt
.sopt_valsize
;
1289 error
= copyout((caddr_t
)&valsize
,
1290 (caddr_t
)uap
->avalsize
, sizeof (valsize
));
1302 pipe(p
, uap
, retval
)
1304 struct pipe_args
*uap
;
1307 struct file
*rf
, *wf
;
1308 struct socket
*rso
, *wso
;
1311 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1312 if (error
= socreate(AF_UNIX
, &rso
, SOCK_STREAM
, 0)) {
1313 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1316 if (error
= socreate(AF_UNIX
, &wso
, SOCK_STREAM
, 0)) {
1319 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1320 error
= falloc(p
, &rf
, &fd
);
1325 rf
->f_type
= DTYPE_SOCKET
;
1326 rf
->f_ops
= &socketops
;
1327 rf
->f_data
= (caddr_t
)rso
;
1328 if (error
= falloc(p
, &wf
, &fd
))
1330 wf
->f_flag
= FWRITE
;
1331 wf
->f_type
= DTYPE_SOCKET
;
1332 wf
->f_ops
= &socketops
;
1333 wf
->f_data
= (caddr_t
)wso
;
1336 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1337 error
= unp_connect2(wso
, rso
);
1338 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1341 *fdflags(p
, retval
[0]) &= ~UF_RESERVED
;
1342 *fdflags(p
, retval
[1]) &= ~UF_RESERVED
;
1345 fdrelse(p
, retval
[1]);
1348 fdrelse(p
, retval
[0]);
1351 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1356 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1366 getsockname1(p
, uap
, retval
, compat
)
1368 register struct getsockname_args
*uap
;
1373 register struct socket
*so
;
1374 struct sockaddr
*sa
;
1378 error
= getsock(p
->p_fd
, uap
->fdes
, &fp
);
1381 error
= copyin((caddr_t
)uap
->alen
, (caddr_t
)&len
, sizeof (len
));
1384 so
= (struct socket
*)fp
->f_data
;
1386 error
= (*so
->so_proto
->pr_usrreqs
->pru_sockaddr
)(so
, &sa
);
1394 len
= MIN(len
, sa
->sa_len
);
1395 #ifdef COMPAT_OLDSOCK
1397 ((struct osockaddr
*)sa
)->sa_family
= sa
->sa_family
;
1399 error
= copyout(sa
, (caddr_t
)uap
->asa
, (u_int
)len
);
1402 error
= copyout((caddr_t
)&len
, (caddr_t
)uap
->alen
,
1411 getsockname(p
, uap
, retval
)
1413 struct getsockname_args
*uap
;
1417 return (getsockname1(p
, uap
, retval
, 0));
1420 #ifdef COMPAT_OLDSOCK
1422 ogetsockname(p
, uap
, retval
)
1424 struct getsockname_args
*uap
;
1428 return (getsockname1(p
, uap
, retval
, 1));
1430 #endif /* COMPAT_OLDSOCK */
1433 * Get name of peer for connected socket.
1437 getpeername1(p
, uap
, retval
, compat
)
1439 register struct getpeername_args
*uap
;
1444 register struct socket
*so
;
1445 struct sockaddr
*sa
;
1449 error
= getsock(p
->p_fd
, uap
->fdes
, &fp
);
1452 so
= (struct socket
*)fp
->f_data
;
1453 if ((so
->so_state
& (SS_ISCONNECTED
|SS_ISCONFIRMING
)) == 0)
1455 error
= copyin((caddr_t
)uap
->alen
, (caddr_t
)&len
, sizeof (len
));
1459 error
= (*so
->so_proto
->pr_usrreqs
->pru_peeraddr
)(so
, &sa
);
1466 len
= MIN(len
, sa
->sa_len
);
1467 #ifdef COMPAT_OLDSOCK
1469 ((struct osockaddr
*)sa
)->sa_family
=
1472 error
= copyout(sa
, (caddr_t
)uap
->asa
, (u_int
)len
);
1476 error
= copyout((caddr_t
)&len
, (caddr_t
)uap
->alen
, sizeof (len
));
1478 if (sa
) FREE(sa
, M_SONAME
);
1483 getpeername(p
, uap
, retval
)
1485 struct getpeername_args
*uap
;
1489 return (getpeername1(p
, uap
, retval
, 0));
1492 #ifdef COMPAT_OLDSOCK
1494 ogetpeername(p
, uap
, retval
)
1496 struct ogetpeername_args
*uap
;
1500 /* XXX uap should have type `getpeername_args *' to begin with. */
1501 return (getpeername1(p
, (struct getpeername_args
*)uap
, retval
, 1));
1503 #endif /* COMPAT_OLDSOCK */
1506 sockargs(mp
, buf
, buflen
, type
)
1511 register struct sockaddr
*sa
;
1512 register struct mbuf
*m
;
1515 if ((u_int
)buflen
> MLEN
) {
1516 #ifdef COMPAT_OLDSOCK
1517 if (type
== MT_SONAME
&& (u_int
)buflen
<= 112)
1518 buflen
= MLEN
; /* unix domain compat. hack */
1523 m
= m_get(M_WAIT
, type
);
1527 error
= copyin(buf
, mtod(m
, caddr_t
), (u_int
)buflen
);
1532 if (type
== MT_SONAME
) {
1533 sa
= mtod(m
, struct sockaddr
*);
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
;
1539 sa
->sa_len
= buflen
;
1546 getsockaddr(namp
, uaddr
, len
)
1547 struct sockaddr
**namp
;
1551 struct sockaddr
*sa
;
1554 if (len
> SOCK_MAXADDRLEN
)
1555 return ENAMETOOLONG
;
1560 MALLOC(sa
, struct sockaddr
*, len
, M_SONAME
, M_WAITOK
);
1561 error
= copyin(uaddr
, sa
, len
);
1565 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1566 if (sa
->sa_family
== 0 && sa
->sa_len
< AF_MAX
)
1567 sa
->sa_family
= sa
->sa_len
;
1576 getsock(fdp
, fdes
, fpp
)
1577 struct filedesc
*fdp
;
1581 register struct file
*fp
;
1583 if ((unsigned)fdes
>= fdp
->fd_nfiles
||
1584 (fp
= fdp
->fd_ofiles
[fdes
]) == NULL
||
1585 (fdp
->fd_ofileflags
[fdes
] & UF_RESERVED
))
1587 if (fp
->f_type
!= DTYPE_SOCKET
)
1595 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
1596 * XXX - The sf_buf functions are currently private to sendfile(2), so have
1597 * been made static, but may be useful in the future for doing zero-copy in
1598 * other parts of the networking code.
1601 sf_buf_init(void *arg
)
1605 SLIST_INIT(&sf_freelist
);
1606 sf_base
= kmem_alloc_pageable(kernel_map
, nsfbufs
* PAGE_SIZE
);
1607 sf_bufs
= _MALLOC(nsfbufs
* sizeof(struct sf_buf
), M_TEMP
, M_NOWAIT
);
1608 bzero(sf_bufs
, nsfbufs
* sizeof(struct sf_buf
));
1609 for (i
= 0; i
< nsfbufs
; i
++) {
1610 sf_bufs
[i
].kva
= sf_base
+ i
* PAGE_SIZE
;
1611 SLIST_INSERT_HEAD(&sf_freelist
, &sf_bufs
[i
], free_list
);
1616 * Get an sf_buf from the freelist. Will block if none are available.
1618 static struct sf_buf
*
1625 while ((sf
= SLIST_FIRST(&sf_freelist
)) == NULL
) {
1626 sf_buf_alloc_want
= 1;
1627 tsleep(&sf_freelist
, PVM
, "sfbufa", 0);
1629 SLIST_REMOVE_HEAD(&sf_freelist
, free_list
);
1635 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
1637 sf_buf_ref(caddr_t addr
, u_int size
)
1642 if (sf
->refcnt
== 0)
1643 panic("sf_buf_ref: referencing a free sf_buf");
1648 * Lose a reference to an sf_buf. When none left, detach mapped page
1649 * and release resources back to the system.
1651 * Must be called at splimp.
1654 sf_buf_free(caddr_t addr
, u_int size
)
1661 if (sf
->refcnt
== 0)
1662 panic("sf_buf_free: freeing free sf_buf");
1664 if (sf
->refcnt
== 0) {
1665 pmap_qremove((vm_offset_t
)addr
, 1);
1668 vm_page_unwire(m
, 0);
1670 * Check for the object going away on us. This can
1671 * happen since we don't hold a reference to it.
1672 * If so, we're responsible for freeing the page.
1674 if (m
->wire_count
== 0 && m
->object
== NULL
)
1675 vm_page_lock_queues();
1677 vm_page_unlock_queues();
1680 SLIST_INSERT_HEAD(&sf_freelist
, sf
, free_list
);
1681 if (sf_buf_alloc_want
) {
1682 sf_buf_alloc_want
= 0;
1683 wakeup(&sf_freelist
);
1690 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1691 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1693 * Send a file specified by 'fd' and starting at 'offset' to a socket
1694 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1695 * nbytes == 0. Optionally add a header and/or trailer to the socket
1696 * output. If specified, write the total number of bytes sent into *sbytes.
1699 sendfile(struct proc
*p
, struct sendfile_args
*uap
)
1702 struct filedesc
*fdp
= p
->p_fd
;
1704 struct vm_object
*obj
;
1709 struct writev_args nuap
;
1710 struct sf_hdtr hdtr
;
1711 off_t off
, xfsize
, sbytes
= 0;
1715 * Do argument checking. Must be a regular file in, stream
1716 * type and connected socket out, positive offset.
1718 if (((u_int
)uap
->fd
) >= fdp
->fd_nfiles
||
1719 (fp
= fdp
->fd_ofiles
[uap
->fd
]) == NULL
||
1720 (fp
->f_flag
& FREAD
) == 0) {
1724 if (fp
->f_type
!= DTYPE_VNODE
) {
1728 vp
= (struct vnode
*)fp
->f_data
;
1730 if (vp
->v_type
!= VREG
|| obj
== NULL
) {
1734 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1737 so
= (struct socket
*)fp
->f_data
;
1738 if (so
->so_type
!= SOCK_STREAM
) {
1742 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
1746 if (uap
->offset
< 0) {
1752 * If specified, get the pointer to the sf_hdtr struct for
1753 * any headers/trailers.
1755 if (uap
->hdtr
!= NULL
) {
1756 error
= copyin(uap
->hdtr
, &hdtr
, sizeof(hdtr
));
1760 * Send any headers. Wimp out and use writev(2).
1762 if (hdtr
.headers
!= NULL
) {
1764 nuap
.iovp
= hdtr
.headers
;
1765 nuap
.iovcnt
= hdtr
.hdr_cnt
;
1766 error
= writev(p
, &nuap
);
1769 sbytes
+= p
->p_retval
[0];
1774 * Protect against multiple writers to the socket.
1776 (void) sblock(&so
->so_snd
, M_WAIT
);
1779 * Loop through the pages in the file, starting with the requested
1780 * offset. Get a file page (do I/O if necessary), map the file page
1781 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1784 for (off
= uap
->offset
; ; off
+= xfsize
, sbytes
+= xfsize
) {
1785 vm_object_offset_t pindex
;
1786 vm_object_offset_t pgoff
;
1788 pindex
= OFF_TO_IDX(off
);
1791 * Calculate the amount to transfer. Not to exceed a page,
1792 * the EOF, or the passed in nbytes.
1794 xfsize
= obj
->un_pager
.vnp
.vnp_size
- off
;
1795 if (xfsize
> PAGE_SIZE_64
)
1797 pgoff
= (vm_object_offset_t
)(off
& PAGE_MASK_64
);
1798 if (PAGE_SIZE
- pgoff
< xfsize
)
1799 xfsize
= PAGE_SIZE_64
- pgoff
;
1800 if (uap
->nbytes
&& xfsize
> (uap
->nbytes
- sbytes
))
1801 xfsize
= uap
->nbytes
- sbytes
;
1805 * Optimize the non-blocking case by looking at the socket space
1806 * before going to the extra work of constituting the sf_buf.
1808 if ((so
->so_state
& SS_NBIO
) && sbspace(&so
->so_snd
) <= 0) {
1809 if (so
->so_state
& SS_CANTSENDMORE
)
1813 sbunlock(&so
->so_snd
);
1817 * Attempt to look up the page. If the page doesn't exist or the
1818 * part we're interested in isn't valid, then read it from disk.
1819 * If some other part of the kernel has this page (i.e. it's busy),
1820 * then disk I/O may be occuring on it, so wait and retry.
1822 pg
= vm_page_lookup(obj
, pindex
);
1823 if (pg
== NULL
|| (!(pg
->flags
& PG_BUSY
) && !pg
->busy
&&
1824 !vm_page_is_valid(pg
, pgoff
, xfsize
))) {
1830 pg
= vm_page_alloc(obj
, pindex
, VM_ALLOC_NORMAL
);
1836 * don't just clear PG_BUSY manually -
1837 * vm_page_alloc() should be considered opaque,
1838 * use the VM routine provided to clear
1845 * Ensure that our page is still around when the I/O completes.
1847 vm_page_io_start(pg
);
1850 * Get the page from backing store.
1852 bsize
= vp
->v_mount
->mnt_stat
.f_iosize
;
1853 auio
.uio_iov
= &aiov
;
1854 auio
.uio_iovcnt
= 1;
1856 aiov
.iov_len
= MAXBSIZE
;
1857 auio
.uio_resid
= MAXBSIZE
;
1858 auio
.uio_offset
= trunc_page(off
);
1859 auio
.uio_segflg
= UIO_NOCOPY
;
1860 auio
.uio_rw
= UIO_READ
;
1862 vn_lock(vp
, LK_SHARED
| LK_NOPAUSE
| LK_RETRY
, p
);
1863 error
= VOP_READ(vp
, &auio
, IO_VMIO
| ((MAXBSIZE
/ bsize
) << 16),
1865 VOP_UNLOCK(vp
, 0, p
);
1866 vm_page_flag_clear(pg
, PG_ZERO
);
1867 vm_page_io_finish(pg
);
1869 vm_page_unwire(pg
, 0);
1871 * See if anyone else might know about this page.
1872 * If not and it is not valid, then free it.
1874 if (pg
->wire_count
== 0 && pg
->valid
== 0 &&
1875 pg
->busy
== 0 && !(pg
->flags
& PG_BUSY
) &&
1876 pg
->hold_count
== 0)
1877 vm_page_lock_queues();
1879 vm_page_unlock_queues();
1880 sbunlock(&so
->so_snd
);
1884 if ((pg
->flags
& PG_BUSY
) || pg
->busy
) {
1886 if ((pg
->flags
& PG_BUSY
) || pg
->busy
) {
1888 * Page is busy. Wait and retry.
1890 vm_page_flag_set(pg
, PG_WANTED
);
1891 tsleep(pg
, PVM
, "sfpbsy", 0);
1898 * Protect from having the page ripped out from beneath us.
1903 * Allocate a kernel virtual page and insert the physical page
1906 sf
= sf_buf_alloc();
1908 pmap_qenter(sf
->kva
, &pg
, 1);
1910 * Get an mbuf header and set it up as having external storage.
1912 MGETHDR(m
, M_WAIT
, MT_DATA
);
1913 m
->m_ext
.ext_free
= sf_buf_free
;
1914 m
->m_ext
.ext_ref
= sf_buf_ref
;
1915 m
->m_ext
.ext_buf
= (void *)sf
->kva
;
1916 m
->m_ext
.ext_size
= PAGE_SIZE
;
1917 m
->m_data
= (char *) sf
->kva
+ pgoff
;
1918 m
->m_flags
|= M_EXT
;
1919 m
->m_pkthdr
.len
= m
->m_len
= xfsize
;
1921 * Add the buffer to the socket buffer chain.
1926 * Make sure that the socket is still able to take more data.
1927 * CANTSENDMORE being true usually means that the connection
1928 * was closed. so_error is true when an error was sensed after
1930 * The state is checked after the page mapping and buffer
1931 * allocation above since those operations may block and make
1932 * any socket checks stale. From this point forward, nothing
1933 * blocks before the pru_send (or more accurately, any blocking
1934 * results in a loop back to here to re-check).
1936 if ((so
->so_state
& SS_CANTSENDMORE
) || so
->so_error
) {
1937 if (so
->so_state
& SS_CANTSENDMORE
) {
1940 error
= so
->so_error
;
1944 sbunlock(&so
->so_snd
);
1949 * Wait for socket space to become available. We do this just
1950 * after checking the connection state above in order to avoid
1951 * a race condition with sbwait().
1953 if (sbspace(&so
->so_snd
) < so
->so_snd
.sb_lowat
) {
1954 if (so
->so_state
& SS_NBIO
) {
1956 sbunlock(&so
->so_snd
);
1961 error
= sbwait(&so
->so_snd
);
1963 * An error from sbwait usually indicates that we've
1964 * been interrupted by a signal. If we've sent anything
1965 * then return bytes sent, otherwise return the error.
1969 sbunlock(&so
->so_snd
);
1975 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)(so
, 0, m
, 0, 0, p
);
1978 sbunlock(&so
->so_snd
);
1982 sbunlock(&so
->so_snd
);
1985 * Send trailers. Wimp out and use writev(2).
1987 if (uap
->hdtr
!= NULL
&& hdtr
.trailers
!= NULL
) {
1989 nuap
.iovp
= hdtr
.trailers
;
1990 nuap
.iovcnt
= hdtr
.trl_cnt
;
1991 error
= writev(p
, &nuap
);
1994 sbytes
+= p
->p_retval
[0];
1998 if (uap
->sbytes
!= NULL
) {
1999 copyout(&sbytes
, uap
->sbytes
, sizeof(off_t
));