2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1982, 1986, 1989, 1990, 1993
25 * The Regents of the University of California. All rights reserved.
27 * sendfile(2) and related extensions:
28 * Copyright (c) 1998, David Greenman. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/filedesc.h>
66 #include <sys/proc_internal.h>
67 #include <sys/file_internal.h>
68 #include <sys/malloc.h>
70 #include <kern/lock.h>
71 #include <sys/domain.h>
72 #include <sys/protosw.h>
73 #include <sys/signalvar.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
77 #include <sys/ktrace.h>
79 #include <sys/kernel.h>
80 #include <sys/uio_internal.h>
82 #include <bsm/audit_kernel.h>
84 #include <sys/kdebug.h>
85 #include <sys/sysproto.h>
87 #define f_flag f_fglob->fg_flag
88 #define f_type f_fglob->fg_type
89 #define f_msgcount f_fglob->fg_msgcount
90 #define f_cred f_fglob->fg_cred
91 #define f_ops f_fglob->fg_ops
92 #define f_offset f_fglob->fg_offset
93 #define f_data f_fglob->fg_data
96 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0)
97 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2)
98 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1)
99 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3)
100 #define DBG_FNC_SENDMSG NETDBG_CODE(DBG_NETSOCK, (1 << 8) | 1)
101 #define DBG_FNC_SENDTO NETDBG_CODE(DBG_NETSOCK, (2 << 8) | 1)
102 #define DBG_FNC_SENDIT NETDBG_CODE(DBG_NETSOCK, (3 << 8) | 1)
103 #define DBG_FNC_RECVFROM NETDBG_CODE(DBG_NETSOCK, (5 << 8))
104 #define DBG_FNC_RECVMSG NETDBG_CODE(DBG_NETSOCK, (6 << 8))
105 #define DBG_FNC_RECVIT NETDBG_CODE(DBG_NETSOCK, (7 << 8))
110 #define HACK_FOR_4056224 1
112 static pid_t last_pid_4056224
= 0;
113 #endif /* HACK_FOR_4056224 */
117 static void sf_buf_init(void *arg
);
118 SYSINIT(sock_sf
, SI_SUB_MBUF
, SI_ORDER_ANY
, sf_buf_init
, NULL
)
119 static struct sf_buf
*sf_buf_alloc(void);
120 static void sf_buf_ref(caddr_t addr
, u_int size
);
121 static void sf_buf_free(caddr_t addr
, u_int size
);
123 static SLIST_HEAD(, sf_buf
) sf_freelist
;
124 static vm_offset_t sf_base
;
125 static struct sf_buf
*sf_bufs
;
126 static int sf_buf_alloc_want
;
129 static int sendit(struct proc
*p
, int s
, struct user_msghdr
*mp
, uio_t uiop
,
130 int flags
, register_t
*retval
);
131 static int recvit(struct proc
*p
, int s
, struct user_msghdr
*mp
, uio_t uiop
,
132 user_addr_t namelenp
, register_t
*retval
);
134 static int accept1(struct proc
*p
, struct accept_args
*uap
, register_t
*retval
, int compat
);
135 static int getsockname1(struct proc
*p
, struct getsockname_args
*uap
,
136 register_t
*retval
, int compat
);
137 static int getpeername1(struct proc
*p
, struct getpeername_args
*uap
,
138 register_t
*retval
, int compat
);
142 struct orecvmsg_args
{
147 struct osendmsg_args
{
165 int oaccept(struct proc
*p
, struct accept_args
*uap
, register_t
*retval
);
166 int ogetpeername(struct proc
*p
, struct getpeername_args
*uap
, register_t
*retval
);
167 int ogetsockname(struct proc
*p
, struct getsockname_args
*uap
, register_t
*retval
);
168 int orecv(struct proc
*p
, struct orecv_args
*uap
, register_t
*retval
);
169 int orecvfrom(struct proc
*p
, struct recvfrom_args
*uap
, register_t
*retval
);
170 int orecvmsg(struct proc
*p
, struct orecvmsg_args
*uap
, register_t
*retval
);
171 int osend(struct proc
*p
, struct osend_args
*uap
, register_t
*retval
);
172 int osendmsg(struct proc
*p
, struct osendmsg_args
*uap
, register_t
*retval
);
173 #endif // COMPAT_43_SOCKET
176 * System call interface to the socket abstraction.
179 extern struct fileops socketops
;
182 socket(p
, uap
, retval
)
184 register struct socket_args
*uap
;
191 AUDIT_ARG(socket
, uap
->domain
, uap
->type
, uap
->protocol
);
193 error
= falloc(p
, &fp
, &fd
);
197 fp
->f_flag
= FREAD
|FWRITE
;
198 fp
->f_type
= DTYPE_SOCKET
;
199 fp
->f_ops
= &socketops
;
201 error
= socreate(uap
->domain
, &so
, uap
->type
, uap
->protocol
);
205 fp
->f_data
= (caddr_t
)so
;
208 *fdflags(p
, fd
) &= ~UF_RESERVED
;
210 fp_drop(p
, fd
, fp
, 1);
220 bind(struct proc
*p
, struct bind_args
*uap
, __unused register_t
*retval
)
226 AUDIT_ARG(fd
, uap
->s
);
227 error
= file_socket(uap
->s
, &so
);
230 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
233 AUDIT_ARG(sockaddr
, p
, sa
);
235 error
= sobind(so
, sa
);
246 listen(__unused
struct proc
*p
, register struct listen_args
*uap
,
247 __unused register_t
*retval
)
252 AUDIT_ARG(fd
, uap
->s
);
253 error
= file_socket(uap
->s
, &so
);
257 error
= solisten(so
, uap
->backlog
);
264 #if !COMPAT_43_SOCKET
265 #define accept1 accept
271 accept1(struct proc
*p
, struct accept_args
*uap
, register_t
*retval
, int compat
)
277 struct socket
*head
, *so
= NULL
;
278 lck_mtx_t
*mutex_held
;
281 short fflag
; /* type must match fp->f_flag */
284 AUDIT_ARG(fd
, uap
->s
);
286 error
= copyin(uap
->anamelen
, (caddr_t
)&namelen
,
291 error
= fp_getfsock(p
, fd
, &fp
, &head
);
293 if (error
== EOPNOTSUPP
)
302 socket_lock(head
, 1);
304 if (head
->so_proto
->pr_getlock
!= NULL
) {
305 mutex_held
= (*head
->so_proto
->pr_getlock
)(head
, 0);
309 mutex_held
= head
->so_proto
->pr_domain
->dom_mtx
;
314 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
315 socket_unlock(head
, 1);
319 if ((head
->so_state
& SS_NBIO
) && head
->so_comp
.tqh_first
== NULL
) {
320 socket_unlock(head
, 1);
324 while (TAILQ_EMPTY(&head
->so_comp
) && head
->so_error
== 0) {
325 if (head
->so_state
& SS_CANTRCVMORE
) {
326 head
->so_error
= ECONNABORTED
;
329 if (head
->so_usecount
< 1)
330 panic("accept1: head=%x refcount=%d\n", head
, head
->so_usecount
);
331 error
= msleep((caddr_t
)&head
->so_timeo
, mutex_held
, PSOCK
| PCATCH
,
333 if (head
->so_usecount
< 1)
334 panic("accept1: 2 head=%x refcount=%d\n", head
, head
->so_usecount
);
335 if ((head
->so_state
& SS_DRAINING
)) {
336 error
= ECONNABORTED
;
339 socket_unlock(head
, 1);
343 if (head
->so_error
) {
344 error
= head
->so_error
;
346 socket_unlock(head
, 1);
352 * At this point we know that there is at least one connection
353 * ready to be accepted. Remove it from the queue prior to
354 * allocating the file descriptor for it since falloc() may
355 * block allowing another process to accept the connection
358 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
359 so
= TAILQ_FIRST(&head
->so_comp
);
360 TAILQ_REMOVE(&head
->so_comp
, so
, so_list
);
362 socket_unlock(head
, 0); /* unlock head to avoid deadlock with select, keep a ref on head */
365 error
= falloc_locked(p
, &fp
, &newfd
, 1);
368 * Probably ran out of file descriptors. Put the
369 * unaccepted connection back onto the queue and
370 * do another wakeup so some other process might
371 * have a chance at it.
374 socket_lock(head
, 0);
375 TAILQ_INSERT_HEAD(&head
->so_comp
, so
, so_list
);
377 wakeup_one((caddr_t
)&head
->so_timeo
);
378 socket_unlock(head
, 1);
381 *fdflags(p
, newfd
) &= ~UF_RESERVED
;
383 fp
->f_type
= DTYPE_SOCKET
;
385 fp
->f_ops
= &socketops
;
386 fp
->f_data
= (caddr_t
)so
;
387 fp_drop(p
, newfd
, fp
, 1);
389 socket_lock(head
, 0);
392 so
->so_state
&= ~SS_COMP
;
395 (void) soacceptlock(so
, &sa
, 0);
396 socket_unlock(head
, 1);
402 socket_unlock(so
, 1);
406 AUDIT_ARG(sockaddr
, p
, sa
);
408 /* check sa_len before it is destroyed */
409 if (namelen
> sa
->sa_len
)
410 namelen
= sa
->sa_len
;
413 ((struct osockaddr
*)sa
)->sa_family
=
416 error
= copyout(sa
, uap
->name
, namelen
);
419 error
= copyout((caddr_t
)&namelen
, uap
->anamelen
,
424 socket_unlock(so
, 1);
431 accept(struct proc
*p
, struct accept_args
*uap
, register_t
*retval
)
434 return (accept1(p
, uap
, retval
, 0));
439 oaccept(struct proc
*p
, struct accept_args
*uap
, register_t
*retval
)
442 return (accept1(p
, uap
, retval
, 1));
444 #endif /* COMPAT_43_SOCKET */
448 connect(struct proc
*p
, struct connect_args
*uap
, __unused register_t
*retval
)
452 lck_mtx_t
*mutex_held
;
456 AUDIT_ARG(fd
, uap
->s
);
457 error
= file_socket( fd
, &so
);
467 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
468 socket_unlock(so
, 1);
472 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
474 socket_unlock(so
, 1);
477 AUDIT_ARG(sockaddr
, p
, sa
);
478 error
= soconnectlock(so
, sa
, 0);
481 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
483 socket_unlock(so
, 1);
487 while ((so
->so_state
& SS_ISCONNECTING
) && so
->so_error
== 0) {
488 if (so
->so_proto
->pr_getlock
!= NULL
)
489 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
491 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
492 error
= msleep((caddr_t
)&so
->so_timeo
, mutex_held
, PSOCK
| PCATCH
,
494 if ((so
->so_state
& SS_DRAINING
)) {
495 error
= ECONNABORTED
;
501 error
= so
->so_error
;
505 so
->so_state
&= ~SS_ISCONNECTING
;
506 socket_unlock(so
, 1);
508 if (error
== ERESTART
)
516 socketpair(struct proc
*p
, struct socketpair_args
*uap
, __unused register_t
*retval
)
518 struct fileproc
*fp1
, *fp2
;
519 struct socket
*so1
, *so2
;
520 int fd
, error
, sv
[2];
522 AUDIT_ARG(socket
, uap
->domain
, uap
->type
, uap
->protocol
);
523 error
= socreate(uap
->domain
, &so1
, uap
->type
, uap
->protocol
);
526 error
= socreate(uap
->domain
, &so2
, uap
->type
, uap
->protocol
);
530 error
= falloc(p
, &fp1
, &fd
);
534 fp1
->f_flag
= FREAD
|FWRITE
;
535 fp1
->f_type
= DTYPE_SOCKET
;
536 fp1
->f_ops
= &socketops
;
537 fp1
->f_data
= (caddr_t
)so1
;
540 error
= falloc(p
, &fp2
, &fd
);
544 fp2
->f_flag
= FREAD
|FWRITE
;
545 fp2
->f_type
= DTYPE_SOCKET
;
546 fp2
->f_ops
= &socketops
;
547 fp2
->f_data
= (caddr_t
)so2
;
550 error
= soconnect2(so1
, so2
);
554 if (uap
->type
== SOCK_DGRAM
) {
556 * Datagram socket connection is asymmetric.
558 error
= soconnect2(so2
, so1
);
565 *fdflags(p
, sv
[0]) &= ~UF_RESERVED
;
566 *fdflags(p
, sv
[1]) &= ~UF_RESERVED
;
567 fp_drop(p
, sv
[0], fp1
, 1);
568 fp_drop(p
, sv
[1], fp2
, 1);
571 error
= copyout((caddr_t
)sv
, uap
->rsv
, 2 * sizeof(int));
572 #if 0 /* old pipe(2) syscall compatability, unused these days */
573 retval
[0] = sv
[0]; /* XXX ??? */
574 retval
[1] = sv
[1]; /* XXX ??? */
578 fp_free(p
, sv
[1], fp2
);
580 fp_free(p
, sv
[0], fp1
);
589 sendit(struct proc
*p
, int s
, struct user_msghdr
*mp
, uio_t uiop
,
590 int flags
, register_t
*retval
)
592 struct mbuf
*control
;
601 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_START
, 0,0,0,0,0);
603 error
= file_socket(s
, &so
);
606 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
611 error
= getsockaddr(&to
, mp
->msg_name
, mp
->msg_namelen
);
613 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
616 AUDIT_ARG(sockaddr
, p
, to
);
620 if (mp
->msg_control
) {
621 if (mp
->msg_controllen
< ((socklen_t
)sizeof(struct cmsghdr
))
623 && !(mp
->msg_flags
& MSG_COMPAT
)
629 error
= sockargs(&control
, mp
->msg_control
,
630 mp
->msg_controllen
, MT_CONTROL
);
634 if (mp
->msg_flags
& MSG_COMPAT
) {
635 register struct cmsghdr
*cm
;
637 M_PREPEND(control
, sizeof(*cm
), M_WAIT
);
642 cm
= mtod(control
, struct cmsghdr
*);
643 cm
->cmsg_len
= control
->m_len
;
644 cm
->cmsg_level
= SOL_SOCKET
;
645 cm
->cmsg_type
= SCM_RIGHTS
;
654 if (KTRPOINT(p
, KTR_GENIO
)) {
655 ktruio
= uio_duplicate(uiop
);
659 len
= uio_resid(uiop
);
663 error
= so
->so_proto
->pr_usrreqs
->pru_sosend(so
, to
, uiop
, 0, control
,
666 if (uio_resid(uiop
) != len
&& (error
== ERESTART
||
667 error
== EINTR
|| error
== EWOULDBLOCK
))
669 /* Generation of SIGPIPE can be controlled per socket */
670 if (error
== EPIPE
&& !(so
->so_flags
& SOF_NOSIGPIPE
))
674 *retval
= (int)(len
- uio_resid(uiop
));
677 if (ktruio
!= NULL
) {
679 uio_setresid(ktruio
, retval
[0]);
680 ktrgenio(p
->p_tracep
, s
, UIO_WRITE
, ktruio
, error
);
687 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
695 sendto(struct proc
*p
, struct sendto_args
*uap
, register_t
*retval
)
697 struct user_msghdr msg
;
701 KERNEL_DEBUG(DBG_FNC_SENDTO
| DBG_FUNC_START
, 0,0,0,0,0);
702 AUDIT_ARG(fd
, uap
->s
);
704 auio
= uio_create(1, 0,
705 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
710 uio_addiov(auio
, uap
->buf
, uap
->len
);
712 msg
.msg_name
= uap
->to
;
713 msg
.msg_namelen
= uap
->tolen
;
714 /* no need to set up msg_iov. sendit uses uio_t we send it */
720 error
= sendit(p
, uap
->s
, &msg
, auio
, uap
->flags
, retval
);
729 * Temporary workaround to let send() and recv() work over a pipe for binary compatibility
730 * This will be removed in the release following Tiger
732 if (error
== ENOTSOCK
) {
735 if (fp_lookup(p
, uap
->s
, &fp
, 0) == 0) {
736 (void) fp_drop(p
, uap
->s
, fp
,0);
738 if (fp
->f_type
== DTYPE_PIPE
) {
739 struct write_args write_uap
;
740 user_ssize_t write_retval
;
742 if (p
->p_pid
> last_pid_4056224
) {
743 last_pid_4056224
= p
->p_pid
;
745 printf("%s[%d] uses send/recv on a pipe\n",
746 p
->p_comm
, p
->p_pid
);
749 bzero(&write_uap
, sizeof(struct write_args
));
750 write_uap
.fd
= uap
->s
;
751 write_uap
.cbuf
= uap
->buf
;
752 write_uap
.nbyte
= uap
->len
;
754 error
= write(p
, &write_uap
, &write_retval
);
755 *retval
= (int)write_retval
;
759 #endif /* HACK_FOR_4056224 */
761 KERNEL_DEBUG(DBG_FNC_SENDTO
| DBG_FUNC_END
, error
, *retval
,0,0,0);
768 osend(__unused
struct proc
*p
,
769 __unused
struct osend_args
*uap
,
770 __unused register_t
*retval
)
772 /* these are no longer supported and in fact
773 * there is no way to call it directly.
774 * LP64todo - remove this once we're sure there are no clients
780 osendmsg(__unused
struct proc
*p
,
781 __unused
struct osendmsg_args
*uap
,
782 __unused register_t
*retval
)
784 /* these are no longer supported and in fact
785 * there is no way to call it directly.
786 * LP64todo - remove this once we're sure there are no clients
794 sendmsg(struct proc
*p
, register struct sendmsg_args
*uap
, register_t
*retval
)
797 struct user_msghdr user_msg
;
803 struct user_iovec
*iovp
;
805 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_START
, 0,0,0,0,0);
806 AUDIT_ARG(fd
, uap
->s
);
807 if (IS_64BIT_PROCESS(p
)) {
808 msghdrp
= (caddr_t
) &user_msg
;
809 size_of_msghdr
= sizeof(user_msg
);
810 size_of_iovec
= sizeof(struct user_iovec
);
813 msghdrp
= (caddr_t
) &msg
;
814 size_of_msghdr
= sizeof(msg
);
815 size_of_iovec
= sizeof(struct iovec
);
817 error
= copyin(uap
->msg
, msghdrp
, size_of_msghdr
);
820 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, error
,0,0,0,0);
824 /* only need to copy if user process is not 64-bit */
825 if (!IS_64BIT_PROCESS(p
)) {
826 user_msg
.msg_flags
= msg
.msg_flags
;
827 user_msg
.msg_controllen
= msg
.msg_controllen
;
828 user_msg
.msg_control
= CAST_USER_ADDR_T(msg
.msg_control
);
829 user_msg
.msg_iovlen
= msg
.msg_iovlen
;
830 user_msg
.msg_iov
= CAST_USER_ADDR_T(msg
.msg_iov
);
831 user_msg
.msg_namelen
= msg
.msg_namelen
;
832 user_msg
.msg_name
= CAST_USER_ADDR_T(msg
.msg_name
);
835 if (user_msg
.msg_iovlen
<= 0 || user_msg
.msg_iovlen
> UIO_MAXIOV
) {
836 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, EMSGSIZE
,0,0,0,0);
840 /* allocate a uio large enough to hold the number of iovecs passed */
841 auio
= uio_create(user_msg
.msg_iovlen
, 0,
842 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
849 if (user_msg
.msg_iovlen
) {
850 /* get location of iovecs within the uio. then copyin the iovecs from
853 iovp
= uio_iovsaddr(auio
);
858 error
= copyin(user_msg
.msg_iov
, (caddr_t
)iovp
, (user_msg
.msg_iovlen
* size_of_iovec
));
861 user_msg
.msg_iov
= CAST_USER_ADDR_T(iovp
);
863 /* finish setup of uio_t */
864 uio_calculateresid(auio
);
867 user_msg
.msg_iov
= 0;
871 user_msg
.msg_flags
= 0;
873 error
= sendit(p
, uap
->s
, &user_msg
, auio
, uap
->flags
, retval
);
878 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, error
,0,0,0,0);
884 recvit(p
, s
, mp
, uiop
, namelenp
, retval
)
885 register struct proc
*p
;
887 register struct user_msghdr
*mp
;
889 user_addr_t namelenp
;
893 struct mbuf
*m
, *control
= 0;
896 struct sockaddr
*fromsa
= 0;
902 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_START
, 0,0,0,0,0);
904 if ( (error
= fp_lookup(p
, s
, &fp
, 1)) ) {
905 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, error
,0,0,0,0);
909 if (fp
->f_type
!= DTYPE_SOCKET
) {
915 so
= (struct socket
*)fp
->f_data
;
918 if (uio_resid(uiop
) < 0) {
919 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
924 if (KTRPOINT(p
, KTR_GENIO
)) {
925 ktruio
= uio_duplicate(uiop
);
929 len
= uio_resid(uiop
);
933 error
= so
->so_proto
->pr_usrreqs
->pru_soreceive(so
, &fromsa
, uiop
,
934 (struct mbuf
**)0, mp
->msg_control
? &control
: (struct mbuf
**)0,
937 AUDIT_ARG(sockaddr
, p
, fromsa
);
939 if (uio_resid(uiop
) != len
&& (error
== ERESTART
||
940 error
== EINTR
|| error
== EWOULDBLOCK
))
944 if (ktruio
!= NULL
) {
946 uio_setresid(ktruio
, len
- uio_resid(uiop
));
947 ktrgenio(p
->p_tracep
, s
, UIO_WRITE
, ktruio
, error
);
954 *retval
= len
- uio_resid(uiop
);
956 len
= mp
->msg_namelen
;
957 if (len
<= 0 || fromsa
== 0)
961 #define MIN(a,b) ((a)>(b)?(b):(a))
963 /* save sa_len before it is destroyed by MSG_COMPAT */
964 len
= MIN(len
, fromsa
->sa_len
);
966 if (mp
->msg_flags
& MSG_COMPAT
)
967 ((struct osockaddr
*)fromsa
)->sa_family
=
970 error
= copyout(fromsa
, mp
->msg_name
, (unsigned)len
);
974 mp
->msg_namelen
= len
;
976 (error
= copyout((caddr_t
)&len
, namelenp
, sizeof (int)))) {
978 if (mp
->msg_flags
& MSG_COMPAT
)
979 error
= 0; /* old recvfrom didn't check */
985 if (mp
->msg_control
) {
988 * We assume that old recvmsg calls won't receive access
989 * rights and other control info, esp. as control info
990 * is always optional and those options didn't exist in 4.3.
991 * If we receive rights, trim the cmsghdr; anything else
994 if (control
&& mp
->msg_flags
& MSG_COMPAT
) {
995 if (mtod(control
, struct cmsghdr
*)->cmsg_level
!=
997 mtod(control
, struct cmsghdr
*)->cmsg_type
!=
999 mp
->msg_controllen
= 0;
1002 control
->m_len
-= sizeof (struct cmsghdr
);
1003 control
->m_data
+= sizeof (struct cmsghdr
);
1006 len
= mp
->msg_controllen
;
1008 mp
->msg_controllen
= 0;
1009 ctlbuf
= mp
->msg_control
;
1011 while (m
&& len
> 0) {
1012 unsigned int tocopy
;
1014 if (len
>= m
->m_len
)
1017 mp
->msg_flags
|= MSG_CTRUNC
;
1021 error
= copyout((caddr_t
)mtod(m
, caddr_t
), ctlbuf
, tocopy
);
1029 mp
->msg_controllen
= ctlbuf
- mp
->msg_control
;
1033 FREE(fromsa
, M_SONAME
);
1036 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, error
,0,0,0,0);
1038 fp_drop(p
, s
, fp
, 0);
1044 recvfrom(p
, uap
, retval
)
1046 register struct recvfrom_args
/* {
1056 struct user_msghdr msg
;
1060 KERNEL_DEBUG(DBG_FNC_RECVFROM
| DBG_FUNC_START
, 0,0,0,0,0);
1061 AUDIT_ARG(fd
, uap
->s
);
1063 if (uap
->fromlenaddr
) {
1064 error
= copyin(uap
->fromlenaddr
,
1065 (caddr_t
)&msg
.msg_namelen
, sizeof (msg
.msg_namelen
));
1069 msg
.msg_namelen
= 0;
1070 msg
.msg_name
= uap
->from
;
1071 auio
= uio_create(1, 0,
1072 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
1078 uio_addiov(auio
, uap
->buf
, uap
->len
);
1079 /* no need to set up msg_iov. recvit uses uio_t we send it */
1082 msg
.msg_control
= 0;
1083 msg
.msg_controllen
= 0;
1084 msg
.msg_flags
= uap
->flags
;
1085 error
= recvit(p
, uap
->s
, &msg
, auio
, uap
->fromlenaddr
, retval
);
1090 #if HACK_FOR_4056224
1093 * Temporary workaround to let send() and recv() work over a pipe for binary compatibility
1094 * This will be removed in the release following Tiger
1096 if (error
== ENOTSOCK
&& proc_is64bit(p
) == 0) {
1097 struct fileproc
*fp
;
1099 if (fp_lookup(p
, uap
->s
, &fp
, 0) == 0) {
1100 (void) fp_drop(p
, uap
->s
, fp
,0);
1102 if (fp
->f_type
== DTYPE_PIPE
) {
1103 struct read_args read_uap
;
1104 user_ssize_t read_retval
;
1106 if (p
->p_pid
> last_pid_4056224
) {
1107 last_pid_4056224
= p
->p_pid
;
1109 printf("%s[%d] uses send/recv on a pipe\n",
1110 p
->p_comm
, p
->p_pid
);
1113 bzero(&read_uap
, sizeof(struct read_args
));
1114 read_uap
.fd
= uap
->s
;
1115 read_uap
.cbuf
= uap
->buf
;
1116 read_uap
.nbyte
= uap
->len
;
1118 error
= read(p
, &read_uap
, &read_retval
);
1119 *retval
= (int)read_retval
;
1123 #endif /* HACK_FOR_4056224 */
1125 KERNEL_DEBUG(DBG_FNC_RECVFROM
| DBG_FUNC_END
, error
,0,0,0,0);
1130 #if COMPAT_43_SOCKET
1132 orecvfrom(struct proc
*p
, struct recvfrom_args
*uap
, register_t
*retval
)
1135 uap
->flags
|= MSG_COMPAT
;
1136 return (recvfrom(p
, uap
, retval
));
1141 #if COMPAT_43_SOCKET
1143 orecv(__unused
struct proc
*p
, __unused
struct orecv_args
*uap
,
1144 __unused register_t
*retval
)
1146 /* these are no longer supported and in fact
1147 * there is no way to call it directly.
1148 * LP64todo - remove this once we're sure there are no clients
1155 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1156 * overlays the new one, missing only the flags, and with the (old) access
1157 * rights where the control fields are now.
1160 orecvmsg(__unused
struct proc
*p
, __unused
struct orecvmsg_args
*uap
,
1161 __unused register_t
*retval
)
1163 /* these are no longer supported and in fact
1164 * there is no way to call it directly.
1165 * LP64todo - remove this once we're sure there are no clients
1174 recvmsg(p
, uap
, retval
)
1176 struct recvmsg_args
*uap
;
1180 struct user_msghdr user_msg
;
1187 struct user_iovec
*iovp
;
1189 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_START
, 0,0,0,0,0);
1190 AUDIT_ARG(fd
, uap
->s
);
1191 if (IS_64BIT_PROCESS(p
)) {
1192 msghdrp
= (caddr_t
) &user_msg
;
1193 size_of_msghdr
= sizeof(user_msg
);
1194 size_of_iovec
= sizeof(struct user_iovec
);
1197 msghdrp
= (caddr_t
) &msg
;
1198 size_of_msghdr
= sizeof(msg
);
1199 size_of_iovec
= sizeof(struct iovec
);
1201 error
= copyin(uap
->msg
, msghdrp
, size_of_msghdr
);
1204 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, error
,0,0,0,0);
1208 /* only need to copy if user process is not 64-bit */
1209 if (!IS_64BIT_PROCESS(p
)) {
1210 user_msg
.msg_flags
= msg
.msg_flags
;
1211 user_msg
.msg_controllen
= msg
.msg_controllen
;
1212 user_msg
.msg_control
= CAST_USER_ADDR_T(msg
.msg_control
);
1213 user_msg
.msg_iovlen
= msg
.msg_iovlen
;
1214 user_msg
.msg_iov
= CAST_USER_ADDR_T(msg
.msg_iov
);
1215 user_msg
.msg_namelen
= msg
.msg_namelen
;
1216 user_msg
.msg_name
= CAST_USER_ADDR_T(msg
.msg_name
);
1219 if (user_msg
.msg_iovlen
<= 0 || user_msg
.msg_iovlen
> UIO_MAXIOV
) {
1220 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, EMSGSIZE
,0,0,0,0);
1224 #if COMPAT_43_SOCKET
1225 user_msg
.msg_flags
= uap
->flags
&~ MSG_COMPAT
;
1227 user_msg
.msg_flags
= uap
->flags
;
1230 /* allocate a uio large enough to hold the number of iovecs passed */
1231 auio
= uio_create(user_msg
.msg_iovlen
, 0,
1232 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
1239 /* get location of iovecs within the uio. then copyin the iovecs from
1242 iovp
= uio_iovsaddr(auio
);
1247 uiov
= user_msg
.msg_iov
;
1248 user_msg
.msg_iov
= CAST_USER_ADDR_T(iovp
);
1249 error
= copyin(uiov
, (caddr_t
)iovp
, (user_msg
.msg_iovlen
* size_of_iovec
));
1253 /* finish setup of uio_t */
1254 uio_calculateresid(auio
);
1256 error
= recvit(p
, uap
->s
, &user_msg
, auio
, 0, retval
);
1258 user_msg
.msg_iov
= uiov
;
1259 /* only need to copy if user process is not 64-bit */
1260 if (!IS_64BIT_PROCESS(p
)) {
1261 // LP64todo - do all these change? if not, then no need to copy all of them!
1262 msg
.msg_flags
= user_msg
.msg_flags
;
1263 msg
.msg_controllen
= user_msg
.msg_controllen
;
1264 msg
.msg_control
= CAST_DOWN(caddr_t
, user_msg
.msg_control
);
1265 msg
.msg_iovlen
= user_msg
.msg_iovlen
;
1266 msg
.msg_iov
= (struct iovec
*) CAST_DOWN(caddr_t
, user_msg
.msg_iov
);
1267 msg
.msg_namelen
= user_msg
.msg_namelen
;
1268 msg
.msg_name
= CAST_DOWN(caddr_t
, user_msg
.msg_name
);
1270 error
= copyout(msghdrp
, uap
->msg
, size_of_msghdr
);
1276 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, error
,0,0,0,0);
1282 shutdown(__unused
struct proc
*p
, struct shutdown_args
*uap
, __unused register_t
*retval
)
1287 AUDIT_ARG(fd
, uap
->s
);
1288 error
= file_socket(uap
->s
, &so
);
1295 error
= soshutdown((struct socket
*)so
, uap
->how
);
1307 setsockopt(struct proc
*p
, struct setsockopt_args
*uap
, __unused register_t
*retval
)
1310 struct sockopt sopt
;
1313 AUDIT_ARG(fd
, uap
->s
);
1314 if (uap
->val
== 0 && uap
->valsize
!= 0)
1316 if (uap
->valsize
< 0)
1319 error
= file_socket(uap
->s
, &so
);
1323 sopt
.sopt_dir
= SOPT_SET
;
1324 sopt
.sopt_level
= uap
->level
;
1325 sopt
.sopt_name
= uap
->name
;
1326 sopt
.sopt_val
= uap
->val
;
1327 sopt
.sopt_valsize
= uap
->valsize
;
1334 error
= sosetopt(so
, &sopt
);
1343 getsockopt(struct proc
*p
, struct getsockopt_args
*uap
, __unused register_t
*retval
)
1347 struct sockopt sopt
;
1350 error
= file_socket(uap
->s
, &so
);
1354 error
= copyin(uap
->avalsize
, (caddr_t
)&valsize
, sizeof (valsize
));
1364 sopt
.sopt_dir
= SOPT_GET
;
1365 sopt
.sopt_level
= uap
->level
;
1366 sopt
.sopt_name
= uap
->name
;
1367 sopt
.sopt_val
= uap
->val
;
1368 sopt
.sopt_valsize
= (size_t)valsize
; /* checked non-negative above */
1375 error
= sogetopt((struct socket
*)so
, &sopt
);
1377 valsize
= sopt
.sopt_valsize
;
1378 error
= copyout((caddr_t
)&valsize
, uap
->avalsize
, sizeof (valsize
));
1391 getsockname1(__unused
struct proc
*p
, struct getsockname_args
*uap
, __unused register_t
*retval
,
1395 struct sockaddr
*sa
;
1399 error
= file_socket(uap
->fdes
, &so
);
1402 error
= copyin(uap
->alen
, (caddr_t
)&len
, sizeof(socklen_t
));
1411 error
= (*so
->so_proto
->pr_usrreqs
->pru_sockaddr
)(so
, &sa
);
1414 struct socket_filter_entry
*filter
;
1416 for (filter
= so
->so_filt
; filter
&& error
== 0;
1417 filter
= filter
->sfe_next_onsocket
) {
1418 if (filter
->sfe_filter
->sf_filter
.sf_getsockname
) {
1422 socket_unlock(so
, 0);
1424 error
= filter
->sfe_filter
->sf_filter
.sf_getsockname(filter
->sfe_cookie
,
1429 if (error
== EJUSTRETURN
)
1437 socket_unlock(so
, 1);
1445 len
= MIN(len
, sa
->sa_len
);
1446 #if COMPAT_43_SOCKET
1448 ((struct osockaddr
*)sa
)->sa_family
= sa
->sa_family
;
1450 error
= copyout((caddr_t
)sa
, uap
->asa
, len
);
1453 error
= copyout((caddr_t
)&len
, uap
->alen
, sizeof(socklen_t
));
1458 file_drop(uap
->fdes
);
1463 getsockname(struct proc
*p
, struct getsockname_args
*uap
, register_t
*retval
)
1465 return (getsockname1(p
, uap
, retval
, 0));
1468 #if COMPAT_43_SOCKET
1470 ogetsockname(struct proc
*p
, struct getsockname_args
*uap
, register_t
*retval
)
1472 return (getsockname1(p
, uap
, retval
, 1));
1474 #endif /* COMPAT_43_SOCKET */
1477 * Get name of peer for connected socket.
1481 getpeername1(__unused
struct proc
*p
, struct getpeername_args
*uap
, __unused register_t
*retval
,
1485 struct sockaddr
*sa
;
1489 error
= file_socket(uap
->fdes
, &so
);
1499 if ((so
->so_state
& (SS_ISCONNECTED
|SS_ISCONFIRMING
)) == 0) {
1500 socket_unlock(so
, 1);
1504 error
= copyin(uap
->alen
, (caddr_t
)&len
, sizeof(socklen_t
));
1506 socket_unlock(so
, 1);
1510 error
= (*so
->so_proto
->pr_usrreqs
->pru_peeraddr
)(so
, &sa
);
1513 struct socket_filter_entry
*filter
;
1515 for (filter
= so
->so_filt
; filter
&& error
== 0;
1516 filter
= filter
->sfe_next_onsocket
) {
1517 if (filter
->sfe_filter
->sf_filter
.sf_getpeername
) {
1521 socket_unlock(so
, 0);
1523 error
= filter
->sfe_filter
->sf_filter
.sf_getpeername(filter
->sfe_cookie
,
1528 if (error
== EJUSTRETURN
)
1536 socket_unlock(so
, 1);
1543 len
= MIN(len
, sa
->sa_len
);
1544 #if COMPAT_43_SOCKET
1546 ((struct osockaddr
*)sa
)->sa_family
=
1549 error
= copyout(sa
, uap
->asa
, len
);
1553 error
= copyout((caddr_t
)&len
, uap
->alen
, sizeof(socklen_t
));
1555 if (sa
) FREE(sa
, M_SONAME
);
1557 file_drop(uap
->fdes
);
1562 getpeername(struct proc
*p
, struct getpeername_args
*uap
, register_t
*retval
)
1565 return (getpeername1(p
, uap
, retval
, 0));
1568 #if COMPAT_43_SOCKET
1570 ogetpeername(struct proc
*p
, struct getpeername_args
*uap
, register_t
*retval
)
1573 return (getpeername1(p
, uap
, retval
, 1));
1575 #endif /* COMPAT_43_SOCKET */
1578 sockargs(mp
, data
, buflen
, type
)
1583 register struct sockaddr
*sa
;
1584 register struct mbuf
*m
;
1587 if ((u_int
)buflen
> MLEN
) {
1588 #if COMPAT_43_SOCKET
1589 if (type
== MT_SONAME
&& (u_int
)buflen
<= 112)
1590 buflen
= MLEN
; /* unix domain compat. hack */
1593 if ((u_int
)buflen
> MCLBYTES
)
1596 m
= m_get(M_WAIT
, type
);
1599 if ((u_int
)buflen
> MLEN
) {
1601 if ((m
->m_flags
& M_EXT
) == 0) {
1607 error
= copyin(data
, mtod(m
, caddr_t
), (u_int
)buflen
);
1612 if (type
== MT_SONAME
) {
1613 sa
= mtod(m
, struct sockaddr
*);
1615 #if COMPAT_43_SOCKET && BYTE_ORDER != BIG_ENDIAN
1616 if (sa
->sa_family
== 0 && sa
->sa_len
< AF_MAX
)
1617 sa
->sa_family
= sa
->sa_len
;
1619 sa
->sa_len
= buflen
;
1626 * Given a user_addr_t of length len, allocate and fill out a *sa.
1629 getsockaddr(struct sockaddr
**namp
, user_addr_t uaddr
, size_t len
)
1631 struct sockaddr
*sa
;
1634 if (len
> SOCK_MAXADDRLEN
)
1635 return ENAMETOOLONG
;
1640 MALLOC(sa
, struct sockaddr
*, len
, M_SONAME
, M_WAITOK
);
1644 error
= copyin(uaddr
, (caddr_t
)sa
, len
);
1648 #if COMPAT_43_SOCKET && BYTE_ORDER != BIG_ENDIAN
1649 if (sa
->sa_family
== 0 && sa
->sa_len
< AF_MAX
)
1650 sa
->sa_family
= sa
->sa_len
;
1661 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
1662 * XXX - The sf_buf functions are currently private to sendfile(2), so have
1663 * been made static, but may be useful in the future for doing zero-copy in
1664 * other parts of the networking code.
1667 sf_buf_init(void *arg
)
1671 SLIST_INIT(&sf_freelist
);
1672 kmem_alloc_pageable(kernel_map
, &sf_base
, nsfbufs
* PAGE_SIZE
);
1673 MALLOC(sf_bufs
, struct sf_buf
*, nsfbufs
* sizeof(struct sf_buf
), M_TEMP
, M_NOWAIT
|M_ZERO
);
1674 if (sf_bufs
== NULL
)
1675 return; /* XXX silently fail leaving sf_bufs NULL */
1677 for (i
= 0; i
< nsfbufs
; i
++) {
1678 sf_bufs
[i
].kva
= sf_base
+ i
* PAGE_SIZE
;
1679 SLIST_INSERT_HEAD(&sf_freelist
, &sf_bufs
[i
], free_list
);
1684 * Get an sf_buf from the freelist. Will block if none are available.
1686 static struct sf_buf
*
1691 while ((sf
= SLIST_FIRST(&sf_freelist
)) == NULL
) {
1692 sf_buf_alloc_want
= 1;
1693 tsleep(&sf_freelist
, PVM
, "sfbufa", 0);
1695 SLIST_REMOVE_HEAD(&sf_freelist
, free_list
);
1700 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
1702 sf_buf_ref(caddr_t addr
, u_int size
)
1707 if (sf
->refcnt
== 0)
1708 panic("sf_buf_ref: referencing a free sf_buf");
1713 * Lose a reference to an sf_buf. When none left, detach mapped page
1714 * and release resources back to the system.
1716 * Must be called at splimp.
1719 sf_buf_free(caddr_t addr
, u_int size
)
1725 if (sf
->refcnt
== 0)
1726 panic("sf_buf_free: freeing free sf_buf");
1728 if (sf
->refcnt
== 0) {
1729 pmap_qremove((vm_offset_t
)addr
, 1);
1731 vm_page_unwire(m
, 0);
1733 * Check for the object going away on us. This can
1734 * happen since we don't hold a reference to it.
1735 * If so, we're responsible for freeing the page.
1737 if (m
->wire_count
== 0 && m
->object
== NULL
)
1738 vm_page_lock_queues();
1740 vm_page_unlock_queues();
1742 SLIST_INSERT_HEAD(&sf_freelist
, sf
, free_list
);
1743 if (sf_buf_alloc_want
) {
1744 sf_buf_alloc_want
= 0;
1745 wakeup(&sf_freelist
);
1752 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1753 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1755 * Send a file specified by 'fd' and starting at 'offset' to a socket
1756 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1757 * nbytes == 0. Optionally add a header and/or trailer to the socket
1758 * output. If specified, write the total number of bytes sent into *sbytes.
1761 sendfile(struct proc
*p
, struct sendfile_args
*uap
)
1763 struct fileproc
*fp
;
1765 struct vm_object
*obj
;
1770 struct writev_args nuap
;
1771 struct sf_hdtr hdtr
;
1772 off_t off
, xfsize
, sbytes
= 0;
1775 if (sf_bufs
== NULL
) {
1776 /* Fail if initialization failed */
1781 * Do argument checking. Must be a regular file in, stream
1782 * type and connected socket out, positive offset.
1784 if (error
= fp_getfvp(p
, uap
->fd
, &fp
, &vp
))
1786 if (fp
->f_flag
& FREAD
) == 0) {
1791 if (vp
->v_type
!= VREG
|| obj
== NULL
) {
1795 error
= file_socket(uap
->s
, &so
);
1805 if (so
->so_type
!= SOCK_STREAM
) {
1809 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
1813 if (uap
->offset
< 0) {
1819 * If specified, get the pointer to the sf_hdtr struct for
1820 * any headers/trailers.
1822 if (uap
->hdtr
!= NULL
) {
1823 error
= copyin(CAST_USER_ADDR_T(uap
->hdtr
), &hdtr
, sizeof(hdtr
));
1827 * Send any headers. Wimp out and use writev(2).
1829 if (hdtr
.headers
!= NULL
) {
1831 nuap
.iovp
= hdtr
.headers
;
1832 nuap
.iovcnt
= hdtr
.hdr_cnt
;
1833 error
= writev(p
, &nuap
);
1836 sbytes
+= p
->p_retval
[0];
1841 * Protect against multiple writers to the socket.
1843 (void) sblock(&so
->so_snd
, M_WAIT
);
1846 * Loop through the pages in the file, starting with the requested
1847 * offset. Get a file page (do I/O if necessary), map the file page
1848 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1851 for (off
= uap
->offset
; ; off
+= xfsize
, sbytes
+= xfsize
) {
1852 vm_object_offset_t pindex
;
1853 vm_object_offset_t pgoff
;
1855 pindex
= OFF_TO_IDX(off
);
1858 * Calculate the amount to transfer. Not to exceed a page,
1859 * the EOF, or the passed in nbytes.
1861 xfsize
= obj
->un_pager
.vnp
.vnp_size
- off
;
1862 if (xfsize
> PAGE_SIZE_64
)
1864 pgoff
= (vm_object_offset_t
)(off
& PAGE_MASK_64
);
1865 if (PAGE_SIZE
- pgoff
< xfsize
)
1866 xfsize
= PAGE_SIZE_64
- pgoff
;
1867 if (uap
->nbytes
&& xfsize
> (uap
->nbytes
- sbytes
))
1868 xfsize
= uap
->nbytes
- sbytes
;
1872 * Optimize the non-blocking case by looking at the socket space
1873 * before going to the extra work of constituting the sf_buf.
1875 if ((so
->so_state
& SS_NBIO
) && sbspace(&so
->so_snd
) <= 0) {
1876 if (so
->so_state
& SS_CANTSENDMORE
)
1880 sbunlock(&so
->so_snd
, 0); /* will release lock */
1884 * Attempt to look up the page. If the page doesn't exist or the
1885 * part we're interested in isn't valid, then read it from disk.
1886 * If some other part of the kernel has this page (i.e. it's busy),
1887 * then disk I/O may be occuring on it, so wait and retry.
1889 pg
= vm_page_lookup(obj
, pindex
);
1890 if (pg
== NULL
|| (!(pg
->flags
& PG_BUSY
) && !pg
->busy
&&
1891 !vm_page_is_valid(pg
, pgoff
, xfsize
))) {
1897 pg
= vm_page_alloc(obj
, pindex
, VM_ALLOC_NORMAL
);
1903 * don't just clear PG_BUSY manually -
1904 * vm_page_alloc() should be considered opaque,
1905 * use the VM routine provided to clear
1912 * Ensure that our page is still around when the I/O completes.
1914 vm_page_io_start(pg
);
1917 * Get the page from backing store.
1919 bsize
= vp
->v_mount
->mnt_vfsstat
.f_iosize
;
1920 auio
.uio_iov
= &aiov
;
1921 auio
.uio_iovcnt
= 1;
1923 aiov
.iov_len
= MAXBSIZE
;
1924 auio
.uio_offset
= trunc_page(off
);
1925 auio
.uio_segflg
= UIO_NOCOPY
;
1926 auio
.uio_rw
= UIO_READ
;
1927 uio_setresid(&auio
, MAXBSIZE
);
1928 error
= VOP_READ(vp
, &auio
, IO_VMIO
| ((MAXBSIZE
/ bsize
) << 16),
1930 vm_page_flag_clear(pg
, PG_ZERO
);
1931 vm_page_io_finish(pg
);
1933 vm_page_unwire(pg
, 0);
1935 * See if anyone else might know about this page.
1936 * If not and it is not valid, then free it.
1938 if (pg
->wire_count
== 0 && pg
->valid
== 0 &&
1939 pg
->busy
== 0 && !(pg
->flags
& PG_BUSY
) &&
1940 pg
->hold_count
== 0)
1941 vm_page_lock_queues();
1943 vm_page_unlock_queues();
1944 sbunlock(&so
->so_snd
, 0); /* will release socket lock */
1948 if ((pg
->flags
& PG_BUSY
) || pg
->busy
) {
1950 if ((pg
->flags
& PG_BUSY
) || pg
->busy
) {
1952 * Page is busy. Wait and retry.
1954 vm_page_flag_set(pg
, PG_WANTED
);
1955 tsleep(pg
, PVM
, "sfpbsy", 0);
1960 * Protect from having the page ripped out from beneath us.
1965 * Allocate a kernel virtual page and insert the physical page
1968 sf
= sf_buf_alloc();
1970 pmap_qenter(sf
->kva
, &pg
, 1);
1972 * Get an mbuf header and set it up as having external storage.
1974 MGETHDR(m
, M_WAIT
, MT_DATA
);
1977 sbunlock(&so
->so_snd
, 0); /* will release socket lock */
1980 m
->m_ext
.ext_free
= sf_buf_free
;
1981 m
->m_ext
.ext_ref
= sf_buf_ref
;
1982 m
->m_ext
.ext_buf
= (void *)sf
->kva
;
1983 m
->m_ext
.ext_size
= PAGE_SIZE
;
1984 m
->m_data
= (char *) sf
->kva
+ pgoff
;
1985 m
->m_flags
|= M_EXT
;
1986 m
->m_pkthdr
.len
= m
->m_len
= xfsize
;
1988 * Add the buffer to the socket buffer chain.
1992 * Make sure that the socket is still able to take more data.
1993 * CANTSENDMORE being true usually means that the connection
1994 * was closed. so_error is true when an error was sensed after
1996 * The state is checked after the page mapping and buffer
1997 * allocation above since those operations may block and make
1998 * any socket checks stale. From this point forward, nothing
1999 * blocks before the pru_send (or more accurately, any blocking
2000 * results in a loop back to here to re-check).
2002 if ((so
->so_state
& SS_CANTSENDMORE
) || so
->so_error
) {
2003 if (so
->so_state
& SS_CANTSENDMORE
) {
2006 error
= so
->so_error
;
2010 sbunlock(&so
->so_snd
, 0); /* will release socket lock */
2014 * Wait for socket space to become available. We do this just
2015 * after checking the connection state above in order to avoid
2016 * a race condition with sbwait().
2018 if (sbspace(&so
->so_snd
) < so
->so_snd
.sb_lowat
) {
2019 if (so
->so_state
& SS_NBIO
) {
2021 sbunlock(&so
->so_snd
, 0); /* will release socket lock */
2025 error
= sbwait(&so
->so_snd
);
2027 * An error from sbwait usually indicates that we've
2028 * been interrupted by a signal. If we've sent anything
2029 * then return bytes sent, otherwise return the error.
2033 sbunlock(&so
->so_snd
, 0);
2038 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)(so
, 0, m
, 0, 0, p
);
2041 sbunlock(&so
->so_snd
, 0); /* will release socket lock */
2045 sbunlock(&so
->so_snd
, 0); /* will release socket lock */
2048 * Send trailers. Wimp out and use writev(2).
2050 if (uap
->hdtr
!= NULL
&& hdtr
.trailers
!= NULL
) {
2052 nuap
.iovp
= hdtr
.trailers
;
2053 nuap
.iovcnt
= hdtr
.trl_cnt
;
2054 error
= writev(p
, &nuap
);
2057 sbytes
+= p
->p_retval
[0];
2064 if (uap
->sbytes
!= NULL
) {
2065 /* XXX this appears bogus for some early failure conditions */
2066 copyout(&sbytes
, CAST_USER_ADDR_T(uap
->sbytes
), sizeof(off_t
));
2070 socket_unlock(so
, 1);