2 * Copyright (c) 2000-2004 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 <bsm/audit_kernel.h>
80 #include <sys/kdebug.h>
84 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0)
85 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2)
86 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1)
87 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3)
88 #define DBG_FNC_SENDMSG NETDBG_CODE(DBG_NETSOCK, (1 << 8) | 1)
89 #define DBG_FNC_SENDTO NETDBG_CODE(DBG_NETSOCK, (2 << 8) | 1)
90 #define DBG_FNC_SENDIT NETDBG_CODE(DBG_NETSOCK, (3 << 8) | 1)
91 #define DBG_FNC_RECVFROM NETDBG_CODE(DBG_NETSOCK, (5 << 8))
92 #define DBG_FNC_RECVMSG NETDBG_CODE(DBG_NETSOCK, (6 << 8))
93 #define DBG_FNC_RECVIT NETDBG_CODE(DBG_NETSOCK, (7 << 8))
97 struct getsockname_args
{
103 struct getsockopt_args
{
117 struct getpeername_args
{
127 static void sf_buf_init(void *arg
);
128 SYSINIT(sock_sf
, SI_SUB_MBUF
, SI_ORDER_ANY
, sf_buf_init
, NULL
)
129 static struct sf_buf
*sf_buf_alloc(void);
130 static void sf_buf_ref(caddr_t addr
, u_int size
);
131 static void sf_buf_free(caddr_t addr
, u_int size
);
133 static SLIST_HEAD(, sf_buf
) sf_freelist
;
134 static vm_offset_t sf_base
;
135 static struct sf_buf
*sf_bufs
;
136 static int sf_buf_alloc_want
;
139 static int sendit
__P((struct proc
*p
, int s
, struct msghdr
*mp
, int flags
, register_t
*retval
));
140 static int recvit
__P((struct proc
*p
, int s
, struct msghdr
*mp
,
141 caddr_t namelenp
, register_t
*retval
));
143 static int accept1
__P((struct proc
*p
, struct accept_args
*uap
, register_t
*retval
, int compat
));
144 static int getsockname1
__P((struct proc
*p
, struct getsockname_args
*uap
,
145 register_t
*retval
, int compat
));
146 static int getpeername1
__P((struct proc
*p
, struct getpeername_args
*uap
,
147 register_t
*retval
, int compat
));
150 * System call interface to the socket abstraction.
152 #if COMPAT_43 || defined(COMPAT_SUNOS)
153 #define COMPAT_OLDSOCK
156 extern struct fileops socketops
;
164 socket(p
, uap
, retval
)
166 register struct socket_args
*uap
;
169 struct filedesc
*fdp
= p
->p_fd
;
174 AUDIT_ARG(socket
, uap
->domain
, uap
->type
, uap
->protocol
);
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 AUDIT_ARG(fd
, uap
->s
);
216 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
219 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
222 AUDIT_ARG(sockaddr
, p
, sa
);
223 if (fp
->f_data
!= NULL
)
224 error
= sobind((struct socket
*)fp
->f_data
, sa
);
239 listen(p
, uap
, retval
)
241 register struct listen_args
*uap
;
247 AUDIT_ARG(fd
, uap
->s
);
248 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
251 if (fp
->f_data
!= NULL
)
252 return (solisten((struct socket
*)fp
->f_data
, uap
->backlog
));
257 #ifndef COMPAT_OLDSOCK
258 #define accept1 accept
264 accept1(p
, uap
, retval
, compat
)
266 register struct accept_args
*uap
;
274 struct socket
*head
, *so
;
276 short fflag
; /* type must match fp->f_flag */
279 AUDIT_ARG(fd
, uap
->s
);
281 error
= copyin((caddr_t
)uap
->anamelen
, (caddr_t
)&namelen
,
286 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
290 head
= (struct socket
*)fp
->f_data
;
295 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
299 if ((head
->so_state
& SS_NBIO
) && head
->so_comp
.tqh_first
== NULL
) {
301 return (EWOULDBLOCK
);
303 while (TAILQ_EMPTY(&head
->so_comp
) && head
->so_error
== 0) {
304 if (head
->so_state
& SS_CANTRCVMORE
) {
305 head
->so_error
= ECONNABORTED
;
308 error
= tsleep((caddr_t
)&head
->so_timeo
, PSOCK
| PCATCH
,
315 if (head
->so_error
) {
316 error
= head
->so_error
;
324 * At this point we know that there is at least one connection
325 * ready to be accepted. Remove it from the queue prior to
326 * allocating the file descriptor for it since falloc() may
327 * block allowing another process to accept the connection
330 so
= TAILQ_FIRST(&head
->so_comp
);
331 TAILQ_REMOVE(&head
->so_comp
, so
, so_list
);
335 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
336 error
= falloc(p
, &fp
, &fd
);
337 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
340 * Probably ran out of file descriptors. Put the
341 * unaccepted connection back onto the queue and
342 * do another wakeup so some other process might
343 * have a chance at it.
345 TAILQ_INSERT_HEAD(&head
->so_comp
, so
, so_list
);
347 wakeup_one(&head
->so_timeo
);
351 *fdflags(p
, fd
) &= ~UF_RESERVED
;
355 so
->so_state
&= ~SS_COMP
;
357 fp
->f_type
= DTYPE_SOCKET
;
359 fp
->f_ops
= &socketops
;
360 fp
->f_data
= (caddr_t
)so
;
362 (void) soaccept(so
, &sa
);
369 AUDIT_ARG(sockaddr
, p
, sa
);
371 /* check sa_len before it is destroyed */
372 if (namelen
> sa
->sa_len
)
373 namelen
= sa
->sa_len
;
374 #ifdef COMPAT_OLDSOCK
376 ((struct osockaddr
*)sa
)->sa_family
=
379 error
= copyout(sa
, (caddr_t
)uap
->name
, (u_int
)namelen
);
382 error
= copyout((caddr_t
)&namelen
,
383 (caddr_t
)uap
->anamelen
, sizeof (*uap
->anamelen
));
391 accept(p
, uap
, retval
)
393 struct accept_args
*uap
;
397 return (accept1(p
, uap
, retval
, 0));
400 #ifdef COMPAT_OLDSOCK
402 oaccept(p
, uap
, retval
)
404 struct accept_args
*uap
;
408 return (accept1(p
, uap
, retval
, 1));
410 #endif /* COMPAT_OLDSOCK */
412 struct connect_args
{
419 connect(p
, uap
, retval
)
421 register struct connect_args
*uap
;
425 register struct socket
*so
;
429 AUDIT_ARG(fd
, uap
->s
);
430 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
433 so
= (struct socket
*)fp
->f_data
;
436 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
))
438 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
441 AUDIT_ARG(sockaddr
, p
, sa
);
442 error
= soconnect(so
, sa
);
445 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
447 return (EINPROGRESS
);
450 while ((so
->so_state
& SS_ISCONNECTING
) && so
->so_error
== 0) {
451 error
= tsleep((caddr_t
)&so
->so_timeo
, PSOCK
| PCATCH
,
457 error
= so
->so_error
;
462 so
->so_state
&= ~SS_ISCONNECTING
;
464 if (error
== ERESTART
)
469 struct socketpair_args
{
476 socketpair(p
, uap
, retval
)
478 register struct socketpair_args
*uap
;
481 register struct filedesc
*fdp
= p
->p_fd
;
482 struct file
*fp1
, *fp2
;
483 struct socket
*so1
, *so2
;
484 int fd
, error
, sv
[2];
486 AUDIT_ARG(socket
, uap
->domain
, uap
->type
, uap
->protocol
);
487 error
= socreate(uap
->domain
, &so1
, uap
->type
, uap
->protocol
);
490 error
= socreate(uap
->domain
, &so2
, uap
->type
, uap
->protocol
);
493 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
494 error
= falloc(p
, &fp1
, &fd
);
498 fp1
->f_flag
= FREAD
|FWRITE
;
499 fp1
->f_type
= DTYPE_SOCKET
;
500 fp1
->f_ops
= &socketops
;
501 fp1
->f_data
= (caddr_t
)so1
;
502 error
= falloc(p
, &fp2
, &fd
);
505 fp2
->f_flag
= FREAD
|FWRITE
;
506 fp2
->f_type
= DTYPE_SOCKET
;
507 fp2
->f_ops
= &socketops
;
508 fp2
->f_data
= (caddr_t
)so2
;
510 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
511 error
= soconnect2(so1
, so2
);
513 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
517 if (uap
->type
== SOCK_DGRAM
) {
519 * Datagram socket connection is asymmetric.
521 error
= soconnect2(so2
, so1
);
523 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
527 *fdflags(p
, sv
[0]) &= ~UF_RESERVED
;
528 *fdflags(p
, sv
[1]) &= ~UF_RESERVED
;
529 error
= copyout((caddr_t
)sv
, (caddr_t
)uap
->rsv
,
531 #if 0 /* old pipe(2) syscall compatability, unused these days */
532 retval
[0] = sv
[0]; /* XXX ??? */
533 retval
[1] = sv
[1]; /* XXX ??? */
543 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
551 sendit(p
, s
, mp
, flags
, retsize
)
552 register struct proc
*p
;
554 register struct msghdr
*mp
;
560 register struct iovec
*iov
;
562 struct mbuf
*control
;
567 struct iovec
*ktriov
= NULL
;
571 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_START
, 0,0,0,0,0);
573 if (error
= getsock(p
->p_fd
, s
, &fp
))
575 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
579 auio
.uio_iov
= mp
->msg_iov
;
580 auio
.uio_iovcnt
= mp
->msg_iovlen
;
581 auio
.uio_segflg
= UIO_USERSPACE
;
582 auio
.uio_rw
= UIO_WRITE
;
584 auio
.uio_offset
= 0; /* XXX */
587 for (i
= 0; i
< mp
->msg_iovlen
; i
++, iov
++) {
588 if (iov
->iov_len
< 0)
590 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
594 if ((auio
.uio_resid
+= iov
->iov_len
) < 0)
596 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
601 error
= getsockaddr(&to
, mp
->msg_name
, mp
->msg_namelen
);
603 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
606 AUDIT_ARG(sockaddr
, p
, to
);
609 if (mp
->msg_control
) {
610 if (mp
->msg_controllen
< sizeof(struct cmsghdr
)
611 #ifdef COMPAT_OLDSOCK
612 && mp
->msg_flags
!= MSG_COMPAT
618 error
= sockargs(&control
, mp
->msg_control
,
619 mp
->msg_controllen
, MT_CONTROL
);
622 #ifdef COMPAT_OLDSOCK
623 if (mp
->msg_flags
== MSG_COMPAT
) {
624 register struct cmsghdr
*cm
;
626 M_PREPEND(control
, sizeof(*cm
), M_WAIT
);
631 cm
= mtod(control
, struct cmsghdr
*);
632 cm
->cmsg_len
= control
->m_len
;
633 cm
->cmsg_level
= SOL_SOCKET
;
634 cm
->cmsg_type
= SCM_RIGHTS
;
642 if (KTRPOINT(p
, KTR_GENIO
)) {
643 int iovlen
= auio
.uio_iovcnt
* sizeof (struct iovec
);
645 MALLOC(ktriov
, struct iovec
*, iovlen
, M_TEMP
, M_WAITOK
);
646 bcopy((caddr_t
)auio
.uio_iov
, (caddr_t
)ktriov
, iovlen
);
650 len
= auio
.uio_resid
;
651 so
= (struct socket
*)fp
->f_data
;
655 error
= so
->so_proto
->pr_usrreqs
->pru_sosend(so
, to
, &auio
, 0, control
,
658 if (auio
.uio_resid
!= len
&& (error
== ERESTART
||
659 error
== EINTR
|| error
== EWOULDBLOCK
))
661 /* Generation of SIGPIPE can be controlled per socket */
662 if (error
== EPIPE
&& !(so
->so_flags
& SOF_NOSIGPIPE
))
666 *retsize
= len
- auio
.uio_resid
;
668 if (ktriov
!= NULL
) {
670 ktruio
.uio_iov
= ktriov
;
671 ktruio
.uio_resid
= retsize
[0];
672 ktrgenio(p
->p_tracep
, s
, UIO_WRITE
, &ktruio
, error
, -1);
674 FREE(ktriov
, M_TEMP
);
680 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
695 sendto(p
, uap
, retval
)
697 register struct sendto_args
/* {
712 KERNEL_DEBUG(DBG_FNC_SENDTO
| DBG_FUNC_START
, 0,0,0,0,0);
713 AUDIT_ARG(fd
, uap
->s
);
715 msg
.msg_name
= uap
->to
;
716 msg
.msg_namelen
= uap
->tolen
;
720 #ifdef COMPAT_OLDSOCK
723 aiov
.iov_base
= uap
->buf
;
724 aiov
.iov_len
= uap
->len
;
725 stat
= sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
);
726 KERNEL_DEBUG(DBG_FNC_SENDTO
| DBG_FUNC_END
, stat
, *retval
,0,0,0);
730 #ifdef COMPAT_OLDSOCK
739 osend(p
, uap
, retval
)
741 register struct osend_args
/* {
757 aiov
.iov_base
= uap
->buf
;
758 aiov
.iov_len
= uap
->len
;
761 return (sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
));
763 struct osendmsg_args
{
770 osendmsg(p
, uap
, retval
)
772 register struct osendmsg_args
/* {
781 struct iovec aiov
[UIO_SMALLIOV
], *iov
;
784 error
= copyin(uap
->msg
, (caddr_t
)&msg
, sizeof (struct omsghdr
));
787 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
788 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
)
790 MALLOC(iov
, struct iovec
*,
791 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
795 error
= copyin((caddr_t
)msg
.msg_iov
, (caddr_t
)iov
,
796 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)));
799 msg
.msg_flags
= MSG_COMPAT
;
801 error
= sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
);
809 struct sendmsg_args
{
816 sendmsg(p
, uap
, retval
)
818 register struct sendmsg_args
*uap
;
822 struct iovec aiov
[UIO_SMALLIOV
], *iov
;
825 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_START
, 0,0,0,0,0);
826 AUDIT_ARG(fd
, uap
->s
);
827 if (error
= copyin(uap
->msg
, (caddr_t
)&msg
, sizeof (msg
)))
829 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, error
,0,0,0,0);
833 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
834 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
) {
835 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, EMSGSIZE
,0,0,0,0);
838 MALLOC(iov
, struct iovec
*,
839 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
843 if (msg
.msg_iovlen
&&
844 (error
= copyin((caddr_t
)msg
.msg_iov
, (caddr_t
)iov
,
845 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)))))
848 #ifdef COMPAT_OLDSOCK
851 error
= sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
);
855 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, error
,0,0,0,0);
860 recvit(p
, s
, mp
, namelenp
, retval
)
861 register struct proc
*p
;
863 register struct msghdr
*mp
;
869 register struct iovec
*iov
;
872 struct mbuf
*m
, *control
= 0;
875 struct sockaddr
*fromsa
= 0;
877 struct iovec
*ktriov
= NULL
;
881 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_START
, 0,0,0,0,0);
882 if (error
= getsock(p
->p_fd
, s
, &fp
))
884 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, error
,0,0,0,0);
888 auio
.uio_iov
= mp
->msg_iov
;
889 auio
.uio_iovcnt
= mp
->msg_iovlen
;
890 auio
.uio_segflg
= UIO_USERSPACE
;
891 auio
.uio_rw
= UIO_READ
;
893 auio
.uio_offset
= 0; /* XXX */
896 for (i
= 0; i
< mp
->msg_iovlen
; i
++, iov
++) {
897 if ((auio
.uio_resid
+= iov
->iov_len
) < 0) {
898 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
903 if (KTRPOINT(p
, KTR_GENIO
)) {
904 int iovlen
= auio
.uio_iovcnt
* sizeof (struct iovec
);
906 MALLOC(ktriov
, struct iovec
*, iovlen
, M_TEMP
, M_WAITOK
);
907 bcopy((caddr_t
)auio
.uio_iov
, (caddr_t
)ktriov
, iovlen
);
911 len
= auio
.uio_resid
;
912 so
= (struct socket
*)fp
->f_data
;
916 error
= so
->so_proto
->pr_usrreqs
->pru_soreceive(so
, &fromsa
, &auio
,
917 (struct mbuf
**)0, mp
->msg_control
? &control
: (struct mbuf
**)0,
919 AUDIT_ARG(sockaddr
, p
, fromsa
);
921 if (auio
.uio_resid
!= len
&& (error
== ERESTART
||
922 error
== EINTR
|| error
== EWOULDBLOCK
))
926 if (ktriov
!= NULL
) {
928 ktruio
.uio_iov
= ktriov
;
929 ktruio
.uio_resid
= len
- auio
.uio_resid
;
930 ktrgenio(p
->p_tracep
, s
, UIO_WRITE
, &ktruio
, error
, -1);
932 FREE(ktriov
, M_TEMP
);
937 *retval
= len
- auio
.uio_resid
;
939 len
= mp
->msg_namelen
;
940 if (len
<= 0 || fromsa
== 0)
944 #define MIN(a,b) ((a)>(b)?(b):(a))
946 /* save sa_len before it is destroyed by MSG_COMPAT */
947 len
= MIN(len
, fromsa
->sa_len
);
948 #ifdef COMPAT_OLDSOCK
949 if (mp
->msg_flags
& MSG_COMPAT
)
950 ((struct osockaddr
*)fromsa
)->sa_family
=
953 error
= copyout(fromsa
,
954 (caddr_t
)mp
->msg_name
, (unsigned)len
);
958 mp
->msg_namelen
= len
;
960 (error
= copyout((caddr_t
)&len
, namelenp
, sizeof (int)))) {
961 #ifdef COMPAT_OLDSOCK
962 if (mp
->msg_flags
& MSG_COMPAT
)
963 error
= 0; /* old recvfrom didn't check */
969 if (mp
->msg_control
) {
970 #ifdef COMPAT_OLDSOCK
972 * We assume that old recvmsg calls won't receive access
973 * rights and other control info, esp. as control info
974 * is always optional and those options didn't exist in 4.3.
975 * If we receive rights, trim the cmsghdr; anything else
978 if (control
&& mp
->msg_flags
& MSG_COMPAT
) {
979 if (mtod(control
, struct cmsghdr
*)->cmsg_level
!=
981 mtod(control
, struct cmsghdr
*)->cmsg_type
!=
983 mp
->msg_controllen
= 0;
986 control
->m_len
-= sizeof (struct cmsghdr
);
987 control
->m_data
+= sizeof (struct cmsghdr
);
990 len
= mp
->msg_controllen
;
992 mp
->msg_controllen
= 0;
993 ctlbuf
= (caddr_t
) mp
->msg_control
;
995 while (m
&& len
> 0) {
1001 mp
->msg_flags
|= MSG_CTRUNC
;
1005 if (error
= copyout((caddr_t
)mtod(m
, caddr_t
),
1013 mp
->msg_controllen
= ctlbuf
- mp
->msg_control
;
1017 FREE(fromsa
, M_SONAME
);
1020 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, error
,0,0,0,0);
1025 struct recvfrom_args
{
1035 recvfrom(p
, uap
, retval
)
1037 register struct recvfrom_args
/* {
1051 KERNEL_DEBUG(DBG_FNC_RECVFROM
| DBG_FUNC_START
, 0,0,0,0,0);
1052 AUDIT_ARG(fd
, uap
->s
);
1054 if (uap
->fromlenaddr
) {
1055 error
= copyin((caddr_t
)uap
->fromlenaddr
,
1056 (caddr_t
)&msg
.msg_namelen
, sizeof (msg
.msg_namelen
));
1060 msg
.msg_namelen
= 0;
1061 msg
.msg_name
= uap
->from
;
1062 msg
.msg_iov
= &aiov
;
1064 aiov
.iov_base
= uap
->buf
;
1065 aiov
.iov_len
= uap
->len
;
1066 msg
.msg_control
= 0;
1067 msg
.msg_flags
= uap
->flags
;
1068 KERNEL_DEBUG(DBG_FNC_RECVFROM
| DBG_FUNC_END
, error
,0,0,0,0);
1069 return (recvit(p
, uap
->s
, &msg
, (caddr_t
)uap
->fromlenaddr
, retval
));
1072 #ifdef COMPAT_OLDSOCK
1074 orecvfrom(p
, uap
, retval
)
1076 struct recvfrom_args
*uap
;
1080 uap
->flags
|= MSG_COMPAT
;
1081 return (recvfrom(p
, uap
, retval
));
1086 #ifdef COMPAT_OLDSOCK
1095 orecv(p
, uap
, retval
)
1097 struct orecv_args
*uap
;
1104 msg
.msg_namelen
= 0;
1105 msg
.msg_iov
= &aiov
;
1107 aiov
.iov_base
= uap
->buf
;
1108 aiov
.iov_len
= uap
->len
;
1109 msg
.msg_control
= 0;
1110 msg
.msg_flags
= uap
->flags
;
1111 return (recvit(p
, uap
->s
, &msg
, (caddr_t
)0, retval
));
1115 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1116 * overlays the new one, missing only the flags, and with the (old) access
1117 * rights where the control fields are now.
1119 struct orecvmsg_args
{
1121 struct omsghdr
*msg
;
1126 orecvmsg(p
, uap
, retval
)
1128 struct orecvmsg_args
*uap
;
1132 struct iovec aiov
[UIO_SMALLIOV
], *iov
;
1135 error
= copyin((caddr_t
)uap
->msg
, (caddr_t
)&msg
,
1136 sizeof (struct omsghdr
));
1139 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
1140 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
)
1142 MALLOC(iov
, struct iovec
*,
1143 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
1147 msg
.msg_flags
= uap
->flags
| MSG_COMPAT
;
1148 error
= copyin((caddr_t
)msg
.msg_iov
, (caddr_t
)iov
,
1149 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)));
1153 error
= recvit(p
, uap
->s
, &msg
, (caddr_t
)&uap
->msg
->msg_namelen
, retval
);
1155 if (msg
.msg_controllen
&& error
== 0)
1156 error
= copyout((caddr_t
)&msg
.msg_controllen
,
1157 (caddr_t
)&uap
->msg
->msg_accrightslen
, sizeof (int));
1165 struct recvmsg_args
{
1172 recvmsg(p
, uap
, retval
)
1174 struct recvmsg_args
*uap
;
1178 struct iovec aiov
[UIO_SMALLIOV
], *uiov
, *iov
;
1181 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_START
, 0,0,0,0,0);
1182 AUDIT_ARG(fd
, uap
->s
);
1183 if (error
= copyin((caddr_t
)uap
->msg
, (caddr_t
)&msg
,
1186 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, error
,0,0,0,0);
1190 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
1191 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
) {
1192 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, EMSGSIZE
,0,0,0,0);
1195 MALLOC(iov
, struct iovec
*,
1196 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
1200 #ifdef COMPAT_OLDSOCK
1201 msg
.msg_flags
= uap
->flags
&~ MSG_COMPAT
;
1203 msg
.msg_flags
= uap
->flags
;
1207 error
= copyin((caddr_t
)uiov
, (caddr_t
)iov
,
1208 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)));
1211 error
= recvit(p
, uap
->s
, &msg
, (caddr_t
)0, retval
);
1214 error
= copyout((caddr_t
)&msg
, (caddr_t
)uap
->msg
, sizeof(msg
));
1219 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, error
,0,0,0,0);
1224 struct shutdown_args
{
1230 shutdown(p
, uap
, retval
)
1232 struct shutdown_args
*uap
;
1238 AUDIT_ARG(fd
, uap
->s
);
1239 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1242 if (fp
->f_data
== NULL
)
1244 return (soshutdown((struct socket
*)fp
->f_data
, uap
->how
));
1252 struct setsockopt_args
{
1261 setsockopt(p
, uap
, retval
)
1263 struct setsockopt_args
*uap
;
1267 struct sockopt sopt
;
1270 AUDIT_ARG(fd
, uap
->s
);
1271 if (uap
->val
== 0 && uap
->valsize
!= 0)
1273 if (uap
->valsize
< 0)
1276 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1280 sopt
.sopt_dir
= SOPT_SET
;
1281 sopt
.sopt_level
= uap
->level
;
1282 sopt
.sopt_name
= uap
->name
;
1283 sopt
.sopt_val
= uap
->val
;
1284 sopt
.sopt_valsize
= uap
->valsize
;
1287 if (fp
->f_data
== NULL
)
1289 return (sosetopt((struct socket
*)fp
->f_data
, &sopt
));
1295 getsockopt(p
, uap
, retval
)
1297 struct getsockopt_args
*uap
;
1302 struct sockopt sopt
;
1304 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1308 error
= copyin((caddr_t
)uap
->avalsize
, (caddr_t
)&valsize
,
1317 sopt
.sopt_dir
= SOPT_GET
;
1318 sopt
.sopt_level
= uap
->level
;
1319 sopt
.sopt_name
= uap
->name
;
1320 sopt
.sopt_val
= uap
->val
;
1321 sopt
.sopt_valsize
= (size_t)valsize
; /* checked non-negative above */
1324 if (fp
->f_data
== NULL
)
1326 error
= sogetopt((struct socket
*)fp
->f_data
, &sopt
);
1328 valsize
= sopt
.sopt_valsize
;
1329 error
= copyout((caddr_t
)&valsize
,
1330 (caddr_t
)uap
->avalsize
, sizeof (valsize
));
1342 pipe(p
, uap
, retval
)
1344 struct pipe_args
*uap
;
1347 struct file
*rf
, *wf
;
1348 struct socket
*rso
, *wso
;
1351 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1352 if (error
= socreate(AF_UNIX
, &rso
, SOCK_STREAM
, 0)) {
1353 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1356 if (error
= socreate(AF_UNIX
, &wso
, SOCK_STREAM
, 0)) {
1359 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1360 error
= falloc(p
, &rf
, &fd
);
1365 rf
->f_type
= DTYPE_SOCKET
;
1366 rf
->f_ops
= &socketops
;
1367 rf
->f_data
= (caddr_t
)rso
;
1368 if (error
= falloc(p
, &wf
, &fd
))
1370 wf
->f_flag
= FWRITE
;
1371 wf
->f_type
= DTYPE_SOCKET
;
1372 wf
->f_ops
= &socketops
;
1373 wf
->f_data
= (caddr_t
)wso
;
1376 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1377 error
= unp_connect2(wso
, rso
);
1378 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1381 *fdflags(p
, retval
[0]) &= ~UF_RESERVED
;
1382 *fdflags(p
, retval
[1]) &= ~UF_RESERVED
;
1385 fdrelse(p
, retval
[1]);
1388 fdrelse(p
, retval
[0]);
1391 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1396 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1406 getsockname1(p
, uap
, retval
, compat
)
1408 register struct getsockname_args
*uap
;
1413 register struct socket
*so
;
1414 struct sockaddr
*sa
;
1418 error
= getsock(p
->p_fd
, uap
->fdes
, &fp
);
1421 error
= copyin((caddr_t
)uap
->alen
, (caddr_t
)&len
, sizeof (len
));
1424 so
= (struct socket
*)fp
->f_data
;
1428 error
= (*so
->so_proto
->pr_usrreqs
->pru_sockaddr
)(so
, &sa
);
1436 len
= MIN(len
, sa
->sa_len
);
1437 #ifdef COMPAT_OLDSOCK
1439 ((struct osockaddr
*)sa
)->sa_family
= sa
->sa_family
;
1441 error
= copyout(sa
, (caddr_t
)uap
->asa
, (u_int
)len
);
1444 error
= copyout((caddr_t
)&len
, (caddr_t
)uap
->alen
,
1453 getsockname(p
, uap
, retval
)
1455 struct getsockname_args
*uap
;
1459 return (getsockname1(p
, uap
, retval
, 0));
1462 #ifdef COMPAT_OLDSOCK
1464 ogetsockname(p
, uap
, retval
)
1466 struct getsockname_args
*uap
;
1470 return (getsockname1(p
, uap
, retval
, 1));
1472 #endif /* COMPAT_OLDSOCK */
1475 * Get name of peer for connected socket.
1479 getpeername1(p
, uap
, retval
, compat
)
1481 register struct getpeername_args
*uap
;
1486 register struct socket
*so
;
1487 struct sockaddr
*sa
;
1491 error
= getsock(p
->p_fd
, uap
->fdes
, &fp
);
1494 so
= (struct socket
*)fp
->f_data
;
1497 if ((so
->so_state
& (SS_ISCONNECTED
|SS_ISCONFIRMING
)) == 0)
1499 error
= copyin((caddr_t
)uap
->alen
, (caddr_t
)&len
, sizeof (len
));
1503 error
= (*so
->so_proto
->pr_usrreqs
->pru_peeraddr
)(so
, &sa
);
1510 len
= MIN(len
, sa
->sa_len
);
1511 #ifdef COMPAT_OLDSOCK
1513 ((struct osockaddr
*)sa
)->sa_family
=
1516 error
= copyout(sa
, (caddr_t
)uap
->asa
, (u_int
)len
);
1520 error
= copyout((caddr_t
)&len
, (caddr_t
)uap
->alen
, sizeof (len
));
1522 if (sa
) FREE(sa
, M_SONAME
);
1527 getpeername(p
, uap
, retval
)
1529 struct getpeername_args
*uap
;
1533 return (getpeername1(p
, uap
, retval
, 0));
1536 #ifdef COMPAT_OLDSOCK
1538 ogetpeername(p
, uap
, retval
)
1540 struct ogetpeername_args
*uap
;
1544 /* XXX uap should have type `getpeername_args *' to begin with. */
1545 return (getpeername1(p
, (struct getpeername_args
*)uap
, retval
, 1));
1547 #endif /* COMPAT_OLDSOCK */
1550 sockargs(mp
, buf
, buflen
, type
)
1555 register struct sockaddr
*sa
;
1556 register struct mbuf
*m
;
1559 if ((u_int
)buflen
> MLEN
) {
1560 #ifdef COMPAT_OLDSOCK
1561 if (type
== MT_SONAME
&& (u_int
)buflen
<= 112)
1562 buflen
= MLEN
; /* unix domain compat. hack */
1567 m
= m_get(M_WAIT
, type
);
1571 error
= copyin(buf
, mtod(m
, caddr_t
), (u_int
)buflen
);
1576 if (type
== MT_SONAME
) {
1577 sa
= mtod(m
, struct sockaddr
*);
1579 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1580 if (sa
->sa_family
== 0 && sa
->sa_len
< AF_MAX
)
1581 sa
->sa_family
= sa
->sa_len
;
1583 sa
->sa_len
= buflen
;
1590 getsockaddr(namp
, uaddr
, len
)
1591 struct sockaddr
**namp
;
1595 struct sockaddr
*sa
;
1598 if (len
> SOCK_MAXADDRLEN
)
1599 return ENAMETOOLONG
;
1604 MALLOC(sa
, struct sockaddr
*, len
, M_SONAME
, M_WAITOK
);
1605 error
= copyin(uaddr
, sa
, len
);
1609 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1610 if (sa
->sa_family
== 0 && sa
->sa_len
< AF_MAX
)
1611 sa
->sa_family
= sa
->sa_len
;
1620 getsock(fdp
, fdes
, fpp
)
1621 struct filedesc
*fdp
;
1625 register struct file
*fp
;
1627 if ((unsigned)fdes
>= fdp
->fd_nfiles
||
1628 (fp
= fdp
->fd_ofiles
[fdes
]) == NULL
||
1629 (fdp
->fd_ofileflags
[fdes
] & UF_RESERVED
))
1631 if (fp
->f_type
!= DTYPE_SOCKET
)
1639 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
1640 * XXX - The sf_buf functions are currently private to sendfile(2), so have
1641 * been made static, but may be useful in the future for doing zero-copy in
1642 * other parts of the networking code.
1645 sf_buf_init(void *arg
)
1649 SLIST_INIT(&sf_freelist
);
1650 sf_base
= kmem_alloc_pageable(kernel_map
, nsfbufs
* PAGE_SIZE
);
1651 sf_bufs
= _MALLOC(nsfbufs
* sizeof(struct sf_buf
), M_TEMP
, M_NOWAIT
);
1652 bzero(sf_bufs
, nsfbufs
* sizeof(struct sf_buf
));
1653 for (i
= 0; i
< nsfbufs
; i
++) {
1654 sf_bufs
[i
].kva
= sf_base
+ i
* PAGE_SIZE
;
1655 SLIST_INSERT_HEAD(&sf_freelist
, &sf_bufs
[i
], free_list
);
1660 * Get an sf_buf from the freelist. Will block if none are available.
1662 static struct sf_buf
*
1669 while ((sf
= SLIST_FIRST(&sf_freelist
)) == NULL
) {
1670 sf_buf_alloc_want
= 1;
1671 tsleep(&sf_freelist
, PVM
, "sfbufa", 0);
1673 SLIST_REMOVE_HEAD(&sf_freelist
, free_list
);
1679 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
1681 sf_buf_ref(caddr_t addr
, u_int size
)
1686 if (sf
->refcnt
== 0)
1687 panic("sf_buf_ref: referencing a free sf_buf");
1692 * Lose a reference to an sf_buf. When none left, detach mapped page
1693 * and release resources back to the system.
1695 * Must be called at splimp.
1698 sf_buf_free(caddr_t addr
, u_int size
)
1705 if (sf
->refcnt
== 0)
1706 panic("sf_buf_free: freeing free sf_buf");
1708 if (sf
->refcnt
== 0) {
1709 pmap_qremove((vm_offset_t
)addr
, 1);
1712 vm_page_unwire(m
, 0);
1714 * Check for the object going away on us. This can
1715 * happen since we don't hold a reference to it.
1716 * If so, we're responsible for freeing the page.
1718 if (m
->wire_count
== 0 && m
->object
== NULL
)
1719 vm_page_lock_queues();
1721 vm_page_unlock_queues();
1724 SLIST_INSERT_HEAD(&sf_freelist
, sf
, free_list
);
1725 if (sf_buf_alloc_want
) {
1726 sf_buf_alloc_want
= 0;
1727 wakeup(&sf_freelist
);
1734 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1735 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1737 * Send a file specified by 'fd' and starting at 'offset' to a socket
1738 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1739 * nbytes == 0. Optionally add a header and/or trailer to the socket
1740 * output. If specified, write the total number of bytes sent into *sbytes.
1743 sendfile(struct proc
*p
, struct sendfile_args
*uap
)
1746 struct filedesc
*fdp
= p
->p_fd
;
1748 struct vm_object
*obj
;
1753 struct writev_args nuap
;
1754 struct sf_hdtr hdtr
;
1755 off_t off
, xfsize
, sbytes
= 0;
1759 * Do argument checking. Must be a regular file in, stream
1760 * type and connected socket out, positive offset.
1762 if (((u_int
)uap
->fd
) >= fdp
->fd_nfiles
||
1763 (fp
= fdp
->fd_ofiles
[uap
->fd
]) == NULL
||
1764 (fp
->f_flag
& FREAD
) == 0) {
1768 if (fp
->f_type
!= DTYPE_VNODE
) {
1772 vp
= (struct vnode
*)fp
->f_data
;
1774 if (vp
->v_type
!= VREG
|| obj
== NULL
) {
1778 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1781 so
= (struct socket
*)fp
->f_data
;
1786 if (so
->so_type
!= SOCK_STREAM
) {
1790 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
1794 if (uap
->offset
< 0) {
1800 * If specified, get the pointer to the sf_hdtr struct for
1801 * any headers/trailers.
1803 if (uap
->hdtr
!= NULL
) {
1804 error
= copyin(uap
->hdtr
, &hdtr
, sizeof(hdtr
));
1808 * Send any headers. Wimp out and use writev(2).
1810 if (hdtr
.headers
!= NULL
) {
1812 nuap
.iovp
= hdtr
.headers
;
1813 nuap
.iovcnt
= hdtr
.hdr_cnt
;
1814 error
= writev(p
, &nuap
);
1817 sbytes
+= p
->p_retval
[0];
1822 * Protect against multiple writers to the socket.
1824 (void) sblock(&so
->so_snd
, M_WAIT
);
1827 * Loop through the pages in the file, starting with the requested
1828 * offset. Get a file page (do I/O if necessary), map the file page
1829 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1832 for (off
= uap
->offset
; ; off
+= xfsize
, sbytes
+= xfsize
) {
1833 vm_object_offset_t pindex
;
1834 vm_object_offset_t pgoff
;
1836 pindex
= OFF_TO_IDX(off
);
1839 * Calculate the amount to transfer. Not to exceed a page,
1840 * the EOF, or the passed in nbytes.
1842 xfsize
= obj
->un_pager
.vnp
.vnp_size
- off
;
1843 if (xfsize
> PAGE_SIZE_64
)
1845 pgoff
= (vm_object_offset_t
)(off
& PAGE_MASK_64
);
1846 if (PAGE_SIZE
- pgoff
< xfsize
)
1847 xfsize
= PAGE_SIZE_64
- pgoff
;
1848 if (uap
->nbytes
&& xfsize
> (uap
->nbytes
- sbytes
))
1849 xfsize
= uap
->nbytes
- sbytes
;
1853 * Optimize the non-blocking case by looking at the socket space
1854 * before going to the extra work of constituting the sf_buf.
1856 if ((so
->so_state
& SS_NBIO
) && sbspace(&so
->so_snd
) <= 0) {
1857 if (so
->so_state
& SS_CANTSENDMORE
)
1861 sbunlock(&so
->so_snd
);
1865 * Attempt to look up the page. If the page doesn't exist or the
1866 * part we're interested in isn't valid, then read it from disk.
1867 * If some other part of the kernel has this page (i.e. it's busy),
1868 * then disk I/O may be occuring on it, so wait and retry.
1870 pg
= vm_page_lookup(obj
, pindex
);
1871 if (pg
== NULL
|| (!(pg
->flags
& PG_BUSY
) && !pg
->busy
&&
1872 !vm_page_is_valid(pg
, pgoff
, xfsize
))) {
1878 pg
= vm_page_alloc(obj
, pindex
, VM_ALLOC_NORMAL
);
1884 * don't just clear PG_BUSY manually -
1885 * vm_page_alloc() should be considered opaque,
1886 * use the VM routine provided to clear
1893 * Ensure that our page is still around when the I/O completes.
1895 vm_page_io_start(pg
);
1898 * Get the page from backing store.
1900 bsize
= vp
->v_mount
->mnt_stat
.f_iosize
;
1901 auio
.uio_iov
= &aiov
;
1902 auio
.uio_iovcnt
= 1;
1904 aiov
.iov_len
= MAXBSIZE
;
1905 auio
.uio_resid
= MAXBSIZE
;
1906 auio
.uio_offset
= trunc_page(off
);
1907 auio
.uio_segflg
= UIO_NOCOPY
;
1908 auio
.uio_rw
= UIO_READ
;
1910 vn_lock(vp
, LK_SHARED
| LK_NOPAUSE
| LK_RETRY
, p
);
1911 error
= VOP_READ(vp
, &auio
, IO_VMIO
| ((MAXBSIZE
/ bsize
) << 16),
1913 VOP_UNLOCK(vp
, 0, p
);
1914 vm_page_flag_clear(pg
, PG_ZERO
);
1915 vm_page_io_finish(pg
);
1917 vm_page_unwire(pg
, 0);
1919 * See if anyone else might know about this page.
1920 * If not and it is not valid, then free it.
1922 if (pg
->wire_count
== 0 && pg
->valid
== 0 &&
1923 pg
->busy
== 0 && !(pg
->flags
& PG_BUSY
) &&
1924 pg
->hold_count
== 0)
1925 vm_page_lock_queues();
1927 vm_page_unlock_queues();
1928 sbunlock(&so
->so_snd
);
1932 if ((pg
->flags
& PG_BUSY
) || pg
->busy
) {
1934 if ((pg
->flags
& PG_BUSY
) || pg
->busy
) {
1936 * Page is busy. Wait and retry.
1938 vm_page_flag_set(pg
, PG_WANTED
);
1939 tsleep(pg
, PVM
, "sfpbsy", 0);
1946 * Protect from having the page ripped out from beneath us.
1951 * Allocate a kernel virtual page and insert the physical page
1954 sf
= sf_buf_alloc();
1956 pmap_qenter(sf
->kva
, &pg
, 1);
1958 * Get an mbuf header and set it up as having external storage.
1960 MGETHDR(m
, M_WAIT
, MT_DATA
);
1961 m
->m_ext
.ext_free
= sf_buf_free
;
1962 m
->m_ext
.ext_ref
= sf_buf_ref
;
1963 m
->m_ext
.ext_buf
= (void *)sf
->kva
;
1964 m
->m_ext
.ext_size
= PAGE_SIZE
;
1965 m
->m_data
= (char *) sf
->kva
+ pgoff
;
1966 m
->m_flags
|= M_EXT
;
1967 m
->m_pkthdr
.len
= m
->m_len
= xfsize
;
1969 * Add the buffer to the socket buffer chain.
1974 * Make sure that the socket is still able to take more data.
1975 * CANTSENDMORE being true usually means that the connection
1976 * was closed. so_error is true when an error was sensed after
1978 * The state is checked after the page mapping and buffer
1979 * allocation above since those operations may block and make
1980 * any socket checks stale. From this point forward, nothing
1981 * blocks before the pru_send (or more accurately, any blocking
1982 * results in a loop back to here to re-check).
1984 if ((so
->so_state
& SS_CANTSENDMORE
) || so
->so_error
) {
1985 if (so
->so_state
& SS_CANTSENDMORE
) {
1988 error
= so
->so_error
;
1992 sbunlock(&so
->so_snd
);
1997 * Wait for socket space to become available. We do this just
1998 * after checking the connection state above in order to avoid
1999 * a race condition with sbwait().
2001 if (sbspace(&so
->so_snd
) < so
->so_snd
.sb_lowat
) {
2002 if (so
->so_state
& SS_NBIO
) {
2004 sbunlock(&so
->so_snd
);
2009 error
= sbwait(&so
->so_snd
);
2011 * An error from sbwait usually indicates that we've
2012 * been interrupted by a signal. If we've sent anything
2013 * then return bytes sent, otherwise return the error.
2017 sbunlock(&so
->so_snd
);
2023 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)(so
, 0, m
, 0, 0, p
);
2026 sbunlock(&so
->so_snd
);
2030 sbunlock(&so
->so_snd
);
2033 * Send trailers. Wimp out and use writev(2).
2035 if (uap
->hdtr
!= NULL
&& hdtr
.trailers
!= NULL
) {
2037 nuap
.iovp
= hdtr
.trailers
;
2038 nuap
.iovcnt
= hdtr
.trl_cnt
;
2039 error
= writev(p
, &nuap
);
2042 sbytes
+= p
->p_retval
[0];
2046 if (uap
->sbytes
!= NULL
) {
2047 copyout(&sbytes
, uap
->sbytes
, sizeof(off_t
));