2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1982, 1986, 1989, 1990, 1993
32 * The Regents of the University of California. All rights reserved.
34 * sendfile(2) and related extensions:
35 * Copyright (c) 1998, David Greenman. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/filedesc.h>
73 #include <sys/proc_internal.h>
74 #include <sys/file_internal.h>
75 #include <sys/malloc.h>
77 #include <kern/lock.h>
78 #include <sys/domain.h>
79 #include <sys/protosw.h>
80 #include <sys/signalvar.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
84 #include <sys/ktrace.h>
86 #include <sys/kernel.h>
87 #include <sys/uio_internal.h>
89 #include <bsm/audit_kernel.h>
91 #include <sys/kdebug.h>
92 #include <sys/sysproto.h>
94 #define f_flag f_fglob->fg_flag
95 #define f_type f_fglob->fg_type
96 #define f_msgcount f_fglob->fg_msgcount
97 #define f_cred f_fglob->fg_cred
98 #define f_ops f_fglob->fg_ops
99 #define f_offset f_fglob->fg_offset
100 #define f_data f_fglob->fg_data
103 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0)
104 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2)
105 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1)
106 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3)
107 #define DBG_FNC_SENDMSG NETDBG_CODE(DBG_NETSOCK, (1 << 8) | 1)
108 #define DBG_FNC_SENDTO NETDBG_CODE(DBG_NETSOCK, (2 << 8) | 1)
109 #define DBG_FNC_SENDIT NETDBG_CODE(DBG_NETSOCK, (3 << 8) | 1)
110 #define DBG_FNC_RECVFROM NETDBG_CODE(DBG_NETSOCK, (5 << 8))
111 #define DBG_FNC_RECVMSG NETDBG_CODE(DBG_NETSOCK, (6 << 8))
112 #define DBG_FNC_RECVIT NETDBG_CODE(DBG_NETSOCK, (7 << 8))
117 #define HACK_FOR_4056224 1
119 static pid_t last_pid_4056224
= 0;
120 #endif /* HACK_FOR_4056224 */
124 static void sf_buf_init(void *arg
);
125 SYSINIT(sock_sf
, SI_SUB_MBUF
, SI_ORDER_ANY
, sf_buf_init
, NULL
)
126 static struct sf_buf
*sf_buf_alloc(void);
127 static void sf_buf_ref(caddr_t addr
, u_int size
);
128 static void sf_buf_free(caddr_t addr
, u_int size
);
130 static SLIST_HEAD(, sf_buf
) sf_freelist
;
131 static vm_offset_t sf_base
;
132 static struct sf_buf
*sf_bufs
;
133 static int sf_buf_alloc_want
;
136 static int sendit(struct proc
*p
, int s
, struct user_msghdr
*mp
, uio_t uiop
,
137 int flags
, register_t
*retval
);
138 static int recvit(struct proc
*p
, int s
, struct user_msghdr
*mp
, uio_t uiop
,
139 user_addr_t namelenp
, register_t
*retval
);
141 static int accept1(struct proc
*p
, struct accept_args
*uap
, register_t
*retval
, int compat
);
142 static int getsockname1(struct proc
*p
, struct getsockname_args
*uap
,
143 register_t
*retval
, int compat
);
144 static int getpeername1(struct proc
*p
, struct getpeername_args
*uap
,
145 register_t
*retval
, int compat
);
149 struct orecvmsg_args
{
154 struct osendmsg_args
{
172 int oaccept(struct proc
*p
, struct accept_args
*uap
, register_t
*retval
);
173 int ogetpeername(struct proc
*p
, struct getpeername_args
*uap
, register_t
*retval
);
174 int ogetsockname(struct proc
*p
, struct getsockname_args
*uap
, register_t
*retval
);
175 int orecv(struct proc
*p
, struct orecv_args
*uap
, register_t
*retval
);
176 int orecvfrom(struct proc
*p
, struct recvfrom_args
*uap
, register_t
*retval
);
177 int orecvmsg(struct proc
*p
, struct orecvmsg_args
*uap
, register_t
*retval
);
178 int osend(struct proc
*p
, struct osend_args
*uap
, register_t
*retval
);
179 int osendmsg(struct proc
*p
, struct osendmsg_args
*uap
, register_t
*retval
);
180 #endif // COMPAT_43_SOCKET
183 * System call interface to the socket abstraction.
186 extern struct fileops socketops
;
189 socket(p
, uap
, retval
)
191 register struct socket_args
*uap
;
198 AUDIT_ARG(socket
, uap
->domain
, uap
->type
, uap
->protocol
);
200 error
= falloc(p
, &fp
, &fd
);
204 fp
->f_flag
= FREAD
|FWRITE
;
205 fp
->f_type
= DTYPE_SOCKET
;
206 fp
->f_ops
= &socketops
;
208 error
= socreate(uap
->domain
, &so
, uap
->type
, uap
->protocol
);
212 fp
->f_data
= (caddr_t
)so
;
215 *fdflags(p
, fd
) &= ~UF_RESERVED
;
217 fp_drop(p
, fd
, fp
, 1);
227 bind(struct proc
*p
, struct bind_args
*uap
, __unused register_t
*retval
)
233 AUDIT_ARG(fd
, uap
->s
);
234 error
= file_socket(uap
->s
, &so
);
237 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
240 AUDIT_ARG(sockaddr
, p
, sa
);
242 error
= sobind(so
, sa
);
253 listen(__unused
struct proc
*p
, register struct listen_args
*uap
,
254 __unused register_t
*retval
)
259 AUDIT_ARG(fd
, uap
->s
);
260 error
= file_socket(uap
->s
, &so
);
264 error
= solisten(so
, uap
->backlog
);
271 #if !COMPAT_43_SOCKET
272 #define accept1 accept
278 accept1(struct proc
*p
, struct accept_args
*uap
, register_t
*retval
, int compat
)
284 struct socket
*head
, *so
= NULL
;
285 lck_mtx_t
*mutex_held
;
288 short fflag
; /* type must match fp->f_flag */
291 AUDIT_ARG(fd
, uap
->s
);
293 error
= copyin(uap
->anamelen
, (caddr_t
)&namelen
,
298 error
= fp_getfsock(p
, fd
, &fp
, &head
);
300 if (error
== EOPNOTSUPP
)
309 socket_lock(head
, 1);
311 if (head
->so_proto
->pr_getlock
!= NULL
) {
312 mutex_held
= (*head
->so_proto
->pr_getlock
)(head
, 0);
316 mutex_held
= head
->so_proto
->pr_domain
->dom_mtx
;
321 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
322 socket_unlock(head
, 1);
326 if ((head
->so_state
& SS_NBIO
) && head
->so_comp
.tqh_first
== NULL
) {
327 socket_unlock(head
, 1);
331 while (TAILQ_EMPTY(&head
->so_comp
) && head
->so_error
== 0) {
332 if (head
->so_state
& SS_CANTRCVMORE
) {
333 head
->so_error
= ECONNABORTED
;
336 if (head
->so_usecount
< 1)
337 panic("accept1: head=%x refcount=%d\n", head
, head
->so_usecount
);
338 error
= msleep((caddr_t
)&head
->so_timeo
, mutex_held
, PSOCK
| PCATCH
,
340 if (head
->so_usecount
< 1)
341 panic("accept1: 2 head=%x refcount=%d\n", head
, head
->so_usecount
);
342 if ((head
->so_state
& SS_DRAINING
)) {
343 error
= ECONNABORTED
;
346 socket_unlock(head
, 1);
350 if (head
->so_error
) {
351 error
= head
->so_error
;
353 socket_unlock(head
, 1);
359 * At this point we know that there is at least one connection
360 * ready to be accepted. Remove it from the queue prior to
361 * allocating the file descriptor for it since falloc() may
362 * block allowing another process to accept the connection
365 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
366 so
= TAILQ_FIRST(&head
->so_comp
);
367 TAILQ_REMOVE(&head
->so_comp
, so
, so_list
);
369 socket_unlock(head
, 0); /* unlock head to avoid deadlock with select, keep a ref on head */
372 error
= falloc_locked(p
, &fp
, &newfd
, 1);
375 * Probably ran out of file descriptors. Put the
376 * unaccepted connection back onto the queue and
377 * do another wakeup so some other process might
378 * have a chance at it.
381 socket_lock(head
, 0);
382 TAILQ_INSERT_HEAD(&head
->so_comp
, so
, so_list
);
384 wakeup_one((caddr_t
)&head
->so_timeo
);
385 socket_unlock(head
, 1);
388 *fdflags(p
, newfd
) &= ~UF_RESERVED
;
390 fp
->f_type
= DTYPE_SOCKET
;
392 fp
->f_ops
= &socketops
;
393 fp
->f_data
= (caddr_t
)so
;
394 fp_drop(p
, newfd
, fp
, 1);
396 socket_lock(head
, 0);
399 so
->so_state
&= ~SS_COMP
;
402 (void) soacceptlock(so
, &sa
, 0);
403 socket_unlock(head
, 1);
409 socket_unlock(so
, 1);
413 AUDIT_ARG(sockaddr
, p
, sa
);
415 /* check sa_len before it is destroyed */
416 if (namelen
> sa
->sa_len
)
417 namelen
= sa
->sa_len
;
420 ((struct osockaddr
*)sa
)->sa_family
=
423 error
= copyout(sa
, uap
->name
, namelen
);
426 error
= copyout((caddr_t
)&namelen
, uap
->anamelen
,
431 socket_unlock(so
, 1);
438 accept(struct proc
*p
, struct accept_args
*uap
, register_t
*retval
)
441 return (accept1(p
, uap
, retval
, 0));
446 oaccept(struct proc
*p
, struct accept_args
*uap
, register_t
*retval
)
449 return (accept1(p
, uap
, retval
, 1));
451 #endif /* COMPAT_43_SOCKET */
455 connect(struct proc
*p
, struct connect_args
*uap
, __unused register_t
*retval
)
459 lck_mtx_t
*mutex_held
;
463 AUDIT_ARG(fd
, uap
->s
);
464 error
= file_socket( fd
, &so
);
474 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
475 socket_unlock(so
, 1);
479 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
481 socket_unlock(so
, 1);
484 AUDIT_ARG(sockaddr
, p
, sa
);
485 error
= soconnectlock(so
, sa
, 0);
488 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
490 socket_unlock(so
, 1);
494 while ((so
->so_state
& SS_ISCONNECTING
) && so
->so_error
== 0) {
495 if (so
->so_proto
->pr_getlock
!= NULL
)
496 mutex_held
= (*so
->so_proto
->pr_getlock
)(so
, 0);
498 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
499 error
= msleep((caddr_t
)&so
->so_timeo
, mutex_held
, PSOCK
| PCATCH
,
501 if ((so
->so_state
& SS_DRAINING
)) {
502 error
= ECONNABORTED
;
508 error
= so
->so_error
;
512 so
->so_state
&= ~SS_ISCONNECTING
;
513 socket_unlock(so
, 1);
515 if (error
== ERESTART
)
523 socketpair(struct proc
*p
, struct socketpair_args
*uap
, __unused register_t
*retval
)
525 struct fileproc
*fp1
, *fp2
;
526 struct socket
*so1
, *so2
;
527 int fd
, error
, sv
[2];
529 AUDIT_ARG(socket
, uap
->domain
, uap
->type
, uap
->protocol
);
530 error
= socreate(uap
->domain
, &so1
, uap
->type
, uap
->protocol
);
533 error
= socreate(uap
->domain
, &so2
, uap
->type
, uap
->protocol
);
537 error
= falloc(p
, &fp1
, &fd
);
541 fp1
->f_flag
= FREAD
|FWRITE
;
542 fp1
->f_type
= DTYPE_SOCKET
;
543 fp1
->f_ops
= &socketops
;
544 fp1
->f_data
= (caddr_t
)so1
;
547 error
= falloc(p
, &fp2
, &fd
);
551 fp2
->f_flag
= FREAD
|FWRITE
;
552 fp2
->f_type
= DTYPE_SOCKET
;
553 fp2
->f_ops
= &socketops
;
554 fp2
->f_data
= (caddr_t
)so2
;
557 error
= soconnect2(so1
, so2
);
561 if (uap
->type
== SOCK_DGRAM
) {
563 * Datagram socket connection is asymmetric.
565 error
= soconnect2(so2
, so1
);
572 *fdflags(p
, sv
[0]) &= ~UF_RESERVED
;
573 *fdflags(p
, sv
[1]) &= ~UF_RESERVED
;
574 fp_drop(p
, sv
[0], fp1
, 1);
575 fp_drop(p
, sv
[1], fp2
, 1);
578 error
= copyout((caddr_t
)sv
, uap
->rsv
, 2 * sizeof(int));
579 #if 0 /* old pipe(2) syscall compatability, unused these days */
580 retval
[0] = sv
[0]; /* XXX ??? */
581 retval
[1] = sv
[1]; /* XXX ??? */
585 fp_free(p
, sv
[1], fp2
);
587 fp_free(p
, sv
[0], fp1
);
596 sendit(struct proc
*p
, int s
, struct user_msghdr
*mp
, uio_t uiop
,
597 int flags
, register_t
*retval
)
599 struct mbuf
*control
;
608 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_START
, 0,0,0,0,0);
610 error
= file_socket(s
, &so
);
613 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
618 error
= getsockaddr(&to
, mp
->msg_name
, mp
->msg_namelen
);
620 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
623 AUDIT_ARG(sockaddr
, p
, to
);
627 if (mp
->msg_control
) {
628 if (mp
->msg_controllen
< ((socklen_t
)sizeof(struct cmsghdr
))
630 && !(mp
->msg_flags
& MSG_COMPAT
)
636 error
= sockargs(&control
, mp
->msg_control
,
637 mp
->msg_controllen
, MT_CONTROL
);
641 if (mp
->msg_flags
& MSG_COMPAT
) {
642 register struct cmsghdr
*cm
;
644 M_PREPEND(control
, sizeof(*cm
), M_WAIT
);
649 cm
= mtod(control
, struct cmsghdr
*);
650 cm
->cmsg_len
= control
->m_len
;
651 cm
->cmsg_level
= SOL_SOCKET
;
652 cm
->cmsg_type
= SCM_RIGHTS
;
661 if (KTRPOINT(p
, KTR_GENIO
)) {
662 ktruio
= uio_duplicate(uiop
);
666 len
= uio_resid(uiop
);
670 error
= so
->so_proto
->pr_usrreqs
->pru_sosend(so
, to
, uiop
, 0, control
,
673 if (uio_resid(uiop
) != len
&& (error
== ERESTART
||
674 error
== EINTR
|| error
== EWOULDBLOCK
))
676 /* Generation of SIGPIPE can be controlled per socket */
677 if (error
== EPIPE
&& !(so
->so_flags
& SOF_NOSIGPIPE
))
681 *retval
= (int)(len
- uio_resid(uiop
));
684 if (ktruio
!= NULL
) {
686 uio_setresid(ktruio
, retval
[0]);
687 ktrgenio(p
->p_tracep
, s
, UIO_WRITE
, ktruio
, error
);
694 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
702 sendto(struct proc
*p
, struct sendto_args
*uap
, register_t
*retval
)
704 struct user_msghdr msg
;
708 KERNEL_DEBUG(DBG_FNC_SENDTO
| DBG_FUNC_START
, 0,0,0,0,0);
709 AUDIT_ARG(fd
, uap
->s
);
711 auio
= uio_create(1, 0,
712 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
717 uio_addiov(auio
, uap
->buf
, uap
->len
);
719 msg
.msg_name
= uap
->to
;
720 msg
.msg_namelen
= uap
->tolen
;
721 /* no need to set up msg_iov. sendit uses uio_t we send it */
727 error
= sendit(p
, uap
->s
, &msg
, auio
, uap
->flags
, retval
);
736 * Temporary workaround to let send() and recv() work over a pipe for binary compatibility
737 * This will be removed in the release following Tiger
739 if (error
== ENOTSOCK
) {
742 if (fp_lookup(p
, uap
->s
, &fp
, 0) == 0) {
743 (void) fp_drop(p
, uap
->s
, fp
,0);
745 if (fp
->f_type
== DTYPE_PIPE
) {
746 struct write_args write_uap
;
747 user_ssize_t write_retval
;
749 if (p
->p_pid
> last_pid_4056224
) {
750 last_pid_4056224
= p
->p_pid
;
752 printf("%s[%d] uses send/recv on a pipe\n",
753 p
->p_comm
, p
->p_pid
);
756 bzero(&write_uap
, sizeof(struct write_args
));
757 write_uap
.fd
= uap
->s
;
758 write_uap
.cbuf
= uap
->buf
;
759 write_uap
.nbyte
= uap
->len
;
761 error
= write(p
, &write_uap
, &write_retval
);
762 *retval
= (int)write_retval
;
766 #endif /* HACK_FOR_4056224 */
768 KERNEL_DEBUG(DBG_FNC_SENDTO
| DBG_FUNC_END
, error
, *retval
,0,0,0);
775 osend(__unused
struct proc
*p
,
776 __unused
struct osend_args
*uap
,
777 __unused register_t
*retval
)
779 /* these are no longer supported and in fact
780 * there is no way to call it directly.
781 * LP64todo - remove this once we're sure there are no clients
787 osendmsg(__unused
struct proc
*p
,
788 __unused
struct osendmsg_args
*uap
,
789 __unused register_t
*retval
)
791 /* these are no longer supported and in fact
792 * there is no way to call it directly.
793 * LP64todo - remove this once we're sure there are no clients
801 sendmsg(struct proc
*p
, register struct sendmsg_args
*uap
, register_t
*retval
)
804 struct user_msghdr user_msg
;
810 struct user_iovec
*iovp
;
812 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_START
, 0,0,0,0,0);
813 AUDIT_ARG(fd
, uap
->s
);
814 if (IS_64BIT_PROCESS(p
)) {
815 msghdrp
= (caddr_t
) &user_msg
;
816 size_of_msghdr
= sizeof(user_msg
);
817 size_of_iovec
= sizeof(struct user_iovec
);
820 msghdrp
= (caddr_t
) &msg
;
821 size_of_msghdr
= sizeof(msg
);
822 size_of_iovec
= sizeof(struct iovec
);
824 error
= copyin(uap
->msg
, msghdrp
, size_of_msghdr
);
827 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, error
,0,0,0,0);
831 /* only need to copy if user process is not 64-bit */
832 if (!IS_64BIT_PROCESS(p
)) {
833 user_msg
.msg_flags
= msg
.msg_flags
;
834 user_msg
.msg_controllen
= msg
.msg_controllen
;
835 user_msg
.msg_control
= CAST_USER_ADDR_T(msg
.msg_control
);
836 user_msg
.msg_iovlen
= msg
.msg_iovlen
;
837 user_msg
.msg_iov
= CAST_USER_ADDR_T(msg
.msg_iov
);
838 user_msg
.msg_namelen
= msg
.msg_namelen
;
839 user_msg
.msg_name
= CAST_USER_ADDR_T(msg
.msg_name
);
842 if (user_msg
.msg_iovlen
<= 0 || user_msg
.msg_iovlen
> UIO_MAXIOV
) {
843 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, EMSGSIZE
,0,0,0,0);
847 /* allocate a uio large enough to hold the number of iovecs passed */
848 auio
= uio_create(user_msg
.msg_iovlen
, 0,
849 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
856 if (user_msg
.msg_iovlen
) {
857 /* get location of iovecs within the uio. then copyin the iovecs from
860 iovp
= uio_iovsaddr(auio
);
865 error
= copyin(user_msg
.msg_iov
, (caddr_t
)iovp
, (user_msg
.msg_iovlen
* size_of_iovec
));
868 user_msg
.msg_iov
= CAST_USER_ADDR_T(iovp
);
870 /* finish setup of uio_t */
871 uio_calculateresid(auio
);
874 user_msg
.msg_iov
= 0;
878 user_msg
.msg_flags
= 0;
880 error
= sendit(p
, uap
->s
, &user_msg
, auio
, uap
->flags
, retval
);
885 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, error
,0,0,0,0);
891 recvit(p
, s
, mp
, uiop
, namelenp
, retval
)
892 register struct proc
*p
;
894 register struct user_msghdr
*mp
;
896 user_addr_t namelenp
;
900 struct mbuf
*m
, *control
= 0;
903 struct sockaddr
*fromsa
= 0;
909 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_START
, 0,0,0,0,0);
911 if ( (error
= fp_lookup(p
, s
, &fp
, 1)) ) {
912 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, error
,0,0,0,0);
916 if (fp
->f_type
!= DTYPE_SOCKET
) {
922 so
= (struct socket
*)fp
->f_data
;
925 if (uio_resid(uiop
) < 0) {
926 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
931 if (KTRPOINT(p
, KTR_GENIO
)) {
932 ktruio
= uio_duplicate(uiop
);
936 len
= uio_resid(uiop
);
940 error
= so
->so_proto
->pr_usrreqs
->pru_soreceive(so
, &fromsa
, uiop
,
941 (struct mbuf
**)0, mp
->msg_control
? &control
: (struct mbuf
**)0,
944 AUDIT_ARG(sockaddr
, p
, fromsa
);
946 if (uio_resid(uiop
) != len
&& (error
== ERESTART
||
947 error
== EINTR
|| error
== EWOULDBLOCK
))
951 if (ktruio
!= NULL
) {
953 uio_setresid(ktruio
, len
- uio_resid(uiop
));
954 ktrgenio(p
->p_tracep
, s
, UIO_WRITE
, ktruio
, error
);
961 *retval
= len
- uio_resid(uiop
);
963 len
= mp
->msg_namelen
;
964 if (len
<= 0 || fromsa
== 0)
968 #define MIN(a,b) ((a)>(b)?(b):(a))
970 /* save sa_len before it is destroyed by MSG_COMPAT */
971 len
= MIN(len
, fromsa
->sa_len
);
973 if (mp
->msg_flags
& MSG_COMPAT
)
974 ((struct osockaddr
*)fromsa
)->sa_family
=
977 error
= copyout(fromsa
, mp
->msg_name
, (unsigned)len
);
981 mp
->msg_namelen
= len
;
983 (error
= copyout((caddr_t
)&len
, namelenp
, sizeof (int)))) {
985 if (mp
->msg_flags
& MSG_COMPAT
)
986 error
= 0; /* old recvfrom didn't check */
992 if (mp
->msg_control
) {
995 * We assume that old recvmsg calls won't receive access
996 * rights and other control info, esp. as control info
997 * is always optional and those options didn't exist in 4.3.
998 * If we receive rights, trim the cmsghdr; anything else
1001 if (control
&& mp
->msg_flags
& MSG_COMPAT
) {
1002 if (mtod(control
, struct cmsghdr
*)->cmsg_level
!=
1004 mtod(control
, struct cmsghdr
*)->cmsg_type
!=
1006 mp
->msg_controllen
= 0;
1009 control
->m_len
-= sizeof (struct cmsghdr
);
1010 control
->m_data
+= sizeof (struct cmsghdr
);
1013 len
= mp
->msg_controllen
;
1015 mp
->msg_controllen
= 0;
1016 ctlbuf
= mp
->msg_control
;
1018 while (m
&& len
> 0) {
1019 unsigned int tocopy
;
1021 if (len
>= m
->m_len
)
1024 mp
->msg_flags
|= MSG_CTRUNC
;
1028 error
= copyout((caddr_t
)mtod(m
, caddr_t
), ctlbuf
, tocopy
);
1036 mp
->msg_controllen
= ctlbuf
- mp
->msg_control
;
1040 FREE(fromsa
, M_SONAME
);
1043 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, error
,0,0,0,0);
1045 fp_drop(p
, s
, fp
, 0);
1051 recvfrom(p
, uap
, retval
)
1053 register struct recvfrom_args
/* {
1063 struct user_msghdr msg
;
1067 KERNEL_DEBUG(DBG_FNC_RECVFROM
| DBG_FUNC_START
, 0,0,0,0,0);
1068 AUDIT_ARG(fd
, uap
->s
);
1070 if (uap
->fromlenaddr
) {
1071 error
= copyin(uap
->fromlenaddr
,
1072 (caddr_t
)&msg
.msg_namelen
, sizeof (msg
.msg_namelen
));
1076 msg
.msg_namelen
= 0;
1077 msg
.msg_name
= uap
->from
;
1078 auio
= uio_create(1, 0,
1079 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
1085 uio_addiov(auio
, uap
->buf
, uap
->len
);
1086 /* no need to set up msg_iov. recvit uses uio_t we send it */
1089 msg
.msg_control
= 0;
1090 msg
.msg_controllen
= 0;
1091 msg
.msg_flags
= uap
->flags
;
1092 error
= recvit(p
, uap
->s
, &msg
, auio
, uap
->fromlenaddr
, retval
);
1097 #if HACK_FOR_4056224
1100 * Temporary workaround to let send() and recv() work over a pipe for binary compatibility
1101 * This will be removed in the release following Tiger
1103 if (error
== ENOTSOCK
&& proc_is64bit(p
) == 0) {
1104 struct fileproc
*fp
;
1106 if (fp_lookup(p
, uap
->s
, &fp
, 0) == 0) {
1107 (void) fp_drop(p
, uap
->s
, fp
,0);
1109 if (fp
->f_type
== DTYPE_PIPE
) {
1110 struct read_args read_uap
;
1111 user_ssize_t read_retval
;
1113 if (p
->p_pid
> last_pid_4056224
) {
1114 last_pid_4056224
= p
->p_pid
;
1116 printf("%s[%d] uses send/recv on a pipe\n",
1117 p
->p_comm
, p
->p_pid
);
1120 bzero(&read_uap
, sizeof(struct read_args
));
1121 read_uap
.fd
= uap
->s
;
1122 read_uap
.cbuf
= uap
->buf
;
1123 read_uap
.nbyte
= uap
->len
;
1125 error
= read(p
, &read_uap
, &read_retval
);
1126 *retval
= (int)read_retval
;
1130 #endif /* HACK_FOR_4056224 */
1132 KERNEL_DEBUG(DBG_FNC_RECVFROM
| DBG_FUNC_END
, error
,0,0,0,0);
1137 #if COMPAT_43_SOCKET
1139 orecvfrom(struct proc
*p
, struct recvfrom_args
*uap
, register_t
*retval
)
1142 uap
->flags
|= MSG_COMPAT
;
1143 return (recvfrom(p
, uap
, retval
));
1148 #if COMPAT_43_SOCKET
1150 orecv(__unused
struct proc
*p
, __unused
struct orecv_args
*uap
,
1151 __unused register_t
*retval
)
1153 /* these are no longer supported and in fact
1154 * there is no way to call it directly.
1155 * LP64todo - remove this once we're sure there are no clients
1162 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1163 * overlays the new one, missing only the flags, and with the (old) access
1164 * rights where the control fields are now.
1167 orecvmsg(__unused
struct proc
*p
, __unused
struct orecvmsg_args
*uap
,
1168 __unused register_t
*retval
)
1170 /* these are no longer supported and in fact
1171 * there is no way to call it directly.
1172 * LP64todo - remove this once we're sure there are no clients
1181 recvmsg(p
, uap
, retval
)
1183 struct recvmsg_args
*uap
;
1187 struct user_msghdr user_msg
;
1194 struct user_iovec
*iovp
;
1196 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_START
, 0,0,0,0,0);
1197 AUDIT_ARG(fd
, uap
->s
);
1198 if (IS_64BIT_PROCESS(p
)) {
1199 msghdrp
= (caddr_t
) &user_msg
;
1200 size_of_msghdr
= sizeof(user_msg
);
1201 size_of_iovec
= sizeof(struct user_iovec
);
1204 msghdrp
= (caddr_t
) &msg
;
1205 size_of_msghdr
= sizeof(msg
);
1206 size_of_iovec
= sizeof(struct iovec
);
1208 error
= copyin(uap
->msg
, msghdrp
, size_of_msghdr
);
1211 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, error
,0,0,0,0);
1215 /* only need to copy if user process is not 64-bit */
1216 if (!IS_64BIT_PROCESS(p
)) {
1217 user_msg
.msg_flags
= msg
.msg_flags
;
1218 user_msg
.msg_controllen
= msg
.msg_controllen
;
1219 user_msg
.msg_control
= CAST_USER_ADDR_T(msg
.msg_control
);
1220 user_msg
.msg_iovlen
= msg
.msg_iovlen
;
1221 user_msg
.msg_iov
= CAST_USER_ADDR_T(msg
.msg_iov
);
1222 user_msg
.msg_namelen
= msg
.msg_namelen
;
1223 user_msg
.msg_name
= CAST_USER_ADDR_T(msg
.msg_name
);
1226 if (user_msg
.msg_iovlen
<= 0 || user_msg
.msg_iovlen
> UIO_MAXIOV
) {
1227 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, EMSGSIZE
,0,0,0,0);
1231 #if COMPAT_43_SOCKET
1232 user_msg
.msg_flags
= uap
->flags
&~ MSG_COMPAT
;
1234 user_msg
.msg_flags
= uap
->flags
;
1237 /* allocate a uio large enough to hold the number of iovecs passed */
1238 auio
= uio_create(user_msg
.msg_iovlen
, 0,
1239 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
1246 /* get location of iovecs within the uio. then copyin the iovecs from
1249 iovp
= uio_iovsaddr(auio
);
1254 uiov
= user_msg
.msg_iov
;
1255 user_msg
.msg_iov
= CAST_USER_ADDR_T(iovp
);
1256 error
= copyin(uiov
, (caddr_t
)iovp
, (user_msg
.msg_iovlen
* size_of_iovec
));
1260 /* finish setup of uio_t */
1261 uio_calculateresid(auio
);
1263 error
= recvit(p
, uap
->s
, &user_msg
, auio
, 0, retval
);
1265 user_msg
.msg_iov
= uiov
;
1266 /* only need to copy if user process is not 64-bit */
1267 if (!IS_64BIT_PROCESS(p
)) {
1268 // LP64todo - do all these change? if not, then no need to copy all of them!
1269 msg
.msg_flags
= user_msg
.msg_flags
;
1270 msg
.msg_controllen
= user_msg
.msg_controllen
;
1271 msg
.msg_control
= CAST_DOWN(caddr_t
, user_msg
.msg_control
);
1272 msg
.msg_iovlen
= user_msg
.msg_iovlen
;
1273 msg
.msg_iov
= (struct iovec
*) CAST_DOWN(caddr_t
, user_msg
.msg_iov
);
1274 msg
.msg_namelen
= user_msg
.msg_namelen
;
1275 msg
.msg_name
= CAST_DOWN(caddr_t
, user_msg
.msg_name
);
1277 error
= copyout(msghdrp
, uap
->msg
, size_of_msghdr
);
1283 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, error
,0,0,0,0);
1289 shutdown(__unused
struct proc
*p
, struct shutdown_args
*uap
, __unused register_t
*retval
)
1294 AUDIT_ARG(fd
, uap
->s
);
1295 error
= file_socket(uap
->s
, &so
);
1302 error
= soshutdown((struct socket
*)so
, uap
->how
);
1314 setsockopt(struct proc
*p
, struct setsockopt_args
*uap
, __unused register_t
*retval
)
1317 struct sockopt sopt
;
1320 AUDIT_ARG(fd
, uap
->s
);
1321 if (uap
->val
== 0 && uap
->valsize
!= 0)
1323 if (uap
->valsize
< 0)
1326 error
= file_socket(uap
->s
, &so
);
1330 sopt
.sopt_dir
= SOPT_SET
;
1331 sopt
.sopt_level
= uap
->level
;
1332 sopt
.sopt_name
= uap
->name
;
1333 sopt
.sopt_val
= uap
->val
;
1334 sopt
.sopt_valsize
= uap
->valsize
;
1341 error
= sosetopt(so
, &sopt
);
1350 getsockopt(struct proc
*p
, struct getsockopt_args
*uap
, __unused register_t
*retval
)
1354 struct sockopt sopt
;
1357 error
= file_socket(uap
->s
, &so
);
1361 error
= copyin(uap
->avalsize
, (caddr_t
)&valsize
, sizeof (valsize
));
1371 sopt
.sopt_dir
= SOPT_GET
;
1372 sopt
.sopt_level
= uap
->level
;
1373 sopt
.sopt_name
= uap
->name
;
1374 sopt
.sopt_val
= uap
->val
;
1375 sopt
.sopt_valsize
= (size_t)valsize
; /* checked non-negative above */
1382 error
= sogetopt((struct socket
*)so
, &sopt
);
1384 valsize
= sopt
.sopt_valsize
;
1385 error
= copyout((caddr_t
)&valsize
, uap
->avalsize
, sizeof (valsize
));
1398 getsockname1(__unused
struct proc
*p
, struct getsockname_args
*uap
, __unused register_t
*retval
,
1402 struct sockaddr
*sa
;
1406 error
= file_socket(uap
->fdes
, &so
);
1409 error
= copyin(uap
->alen
, (caddr_t
)&len
, sizeof(socklen_t
));
1418 error
= (*so
->so_proto
->pr_usrreqs
->pru_sockaddr
)(so
, &sa
);
1421 struct socket_filter_entry
*filter
;
1423 for (filter
= so
->so_filt
; filter
&& error
== 0;
1424 filter
= filter
->sfe_next_onsocket
) {
1425 if (filter
->sfe_filter
->sf_filter
.sf_getsockname
) {
1429 socket_unlock(so
, 0);
1431 error
= filter
->sfe_filter
->sf_filter
.sf_getsockname(filter
->sfe_cookie
,
1436 if (error
== EJUSTRETURN
)
1444 socket_unlock(so
, 1);
1452 len
= MIN(len
, sa
->sa_len
);
1453 #if COMPAT_43_SOCKET
1455 ((struct osockaddr
*)sa
)->sa_family
= sa
->sa_family
;
1457 error
= copyout((caddr_t
)sa
, uap
->asa
, len
);
1460 error
= copyout((caddr_t
)&len
, uap
->alen
, sizeof(socklen_t
));
1465 file_drop(uap
->fdes
);
1470 getsockname(struct proc
*p
, struct getsockname_args
*uap
, register_t
*retval
)
1472 return (getsockname1(p
, uap
, retval
, 0));
1475 #if COMPAT_43_SOCKET
1477 ogetsockname(struct proc
*p
, struct getsockname_args
*uap
, register_t
*retval
)
1479 return (getsockname1(p
, uap
, retval
, 1));
1481 #endif /* COMPAT_43_SOCKET */
1484 * Get name of peer for connected socket.
1488 getpeername1(__unused
struct proc
*p
, struct getpeername_args
*uap
, __unused register_t
*retval
,
1492 struct sockaddr
*sa
;
1496 error
= file_socket(uap
->fdes
, &so
);
1506 if ((so
->so_state
& (SS_ISCONNECTED
|SS_ISCONFIRMING
)) == 0) {
1507 socket_unlock(so
, 1);
1511 error
= copyin(uap
->alen
, (caddr_t
)&len
, sizeof(socklen_t
));
1513 socket_unlock(so
, 1);
1517 error
= (*so
->so_proto
->pr_usrreqs
->pru_peeraddr
)(so
, &sa
);
1520 struct socket_filter_entry
*filter
;
1522 for (filter
= so
->so_filt
; filter
&& error
== 0;
1523 filter
= filter
->sfe_next_onsocket
) {
1524 if (filter
->sfe_filter
->sf_filter
.sf_getpeername
) {
1528 socket_unlock(so
, 0);
1530 error
= filter
->sfe_filter
->sf_filter
.sf_getpeername(filter
->sfe_cookie
,
1535 if (error
== EJUSTRETURN
)
1543 socket_unlock(so
, 1);
1550 len
= MIN(len
, sa
->sa_len
);
1551 #if COMPAT_43_SOCKET
1553 ((struct osockaddr
*)sa
)->sa_family
=
1556 error
= copyout(sa
, uap
->asa
, len
);
1560 error
= copyout((caddr_t
)&len
, uap
->alen
, sizeof(socklen_t
));
1562 if (sa
) FREE(sa
, M_SONAME
);
1564 file_drop(uap
->fdes
);
1569 getpeername(struct proc
*p
, struct getpeername_args
*uap
, register_t
*retval
)
1572 return (getpeername1(p
, uap
, retval
, 0));
1575 #if COMPAT_43_SOCKET
1577 ogetpeername(struct proc
*p
, struct getpeername_args
*uap
, register_t
*retval
)
1580 return (getpeername1(p
, uap
, retval
, 1));
1582 #endif /* COMPAT_43_SOCKET */
1585 sockargs(mp
, data
, buflen
, type
)
1590 register struct sockaddr
*sa
;
1591 register struct mbuf
*m
;
1594 if ((u_int
)buflen
> MLEN
) {
1595 #if COMPAT_43_SOCKET
1596 if (type
== MT_SONAME
&& (u_int
)buflen
<= 112)
1597 buflen
= MLEN
; /* unix domain compat. hack */
1600 if ((u_int
)buflen
> MCLBYTES
)
1603 m
= m_get(M_WAIT
, type
);
1606 if ((u_int
)buflen
> MLEN
) {
1608 if ((m
->m_flags
& M_EXT
) == 0) {
1614 error
= copyin(data
, mtod(m
, caddr_t
), (u_int
)buflen
);
1619 if (type
== MT_SONAME
) {
1620 sa
= mtod(m
, struct sockaddr
*);
1622 #if COMPAT_43_SOCKET && BYTE_ORDER != BIG_ENDIAN
1623 if (sa
->sa_family
== 0 && sa
->sa_len
< AF_MAX
)
1624 sa
->sa_family
= sa
->sa_len
;
1626 sa
->sa_len
= buflen
;
1633 * Given a user_addr_t of length len, allocate and fill out a *sa.
1636 getsockaddr(struct sockaddr
**namp
, user_addr_t uaddr
, size_t len
)
1638 struct sockaddr
*sa
;
1641 if (len
> SOCK_MAXADDRLEN
)
1642 return ENAMETOOLONG
;
1647 MALLOC(sa
, struct sockaddr
*, len
, M_SONAME
, M_WAITOK
);
1651 error
= copyin(uaddr
, (caddr_t
)sa
, len
);
1655 #if COMPAT_43_SOCKET && BYTE_ORDER != BIG_ENDIAN
1656 if (sa
->sa_family
== 0 && sa
->sa_len
< AF_MAX
)
1657 sa
->sa_family
= sa
->sa_len
;
1668 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
1669 * XXX - The sf_buf functions are currently private to sendfile(2), so have
1670 * been made static, but may be useful in the future for doing zero-copy in
1671 * other parts of the networking code.
1674 sf_buf_init(void *arg
)
1678 SLIST_INIT(&sf_freelist
);
1679 kmem_alloc_pageable(kernel_map
, &sf_base
, nsfbufs
* PAGE_SIZE
);
1680 MALLOC(sf_bufs
, struct sf_buf
*, nsfbufs
* sizeof(struct sf_buf
), M_TEMP
, M_NOWAIT
|M_ZERO
);
1681 if (sf_bufs
== NULL
)
1682 return; /* XXX silently fail leaving sf_bufs NULL */
1684 for (i
= 0; i
< nsfbufs
; i
++) {
1685 sf_bufs
[i
].kva
= sf_base
+ i
* PAGE_SIZE
;
1686 SLIST_INSERT_HEAD(&sf_freelist
, &sf_bufs
[i
], free_list
);
1691 * Get an sf_buf from the freelist. Will block if none are available.
1693 static struct sf_buf
*
1698 while ((sf
= SLIST_FIRST(&sf_freelist
)) == NULL
) {
1699 sf_buf_alloc_want
= 1;
1700 tsleep(&sf_freelist
, PVM
, "sfbufa", 0);
1702 SLIST_REMOVE_HEAD(&sf_freelist
, free_list
);
1707 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
1709 sf_buf_ref(caddr_t addr
, u_int size
)
1714 if (sf
->refcnt
== 0)
1715 panic("sf_buf_ref: referencing a free sf_buf");
1720 * Lose a reference to an sf_buf. When none left, detach mapped page
1721 * and release resources back to the system.
1723 * Must be called at splimp.
1726 sf_buf_free(caddr_t addr
, u_int size
)
1732 if (sf
->refcnt
== 0)
1733 panic("sf_buf_free: freeing free sf_buf");
1735 if (sf
->refcnt
== 0) {
1736 pmap_qremove((vm_offset_t
)addr
, 1);
1738 vm_page_unwire(m
, 0);
1740 * Check for the object going away on us. This can
1741 * happen since we don't hold a reference to it.
1742 * If so, we're responsible for freeing the page.
1744 if (m
->wire_count
== 0 && m
->object
== NULL
)
1745 vm_page_lock_queues();
1747 vm_page_unlock_queues();
1749 SLIST_INSERT_HEAD(&sf_freelist
, sf
, free_list
);
1750 if (sf_buf_alloc_want
) {
1751 sf_buf_alloc_want
= 0;
1752 wakeup(&sf_freelist
);
1759 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1760 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1762 * Send a file specified by 'fd' and starting at 'offset' to a socket
1763 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1764 * nbytes == 0. Optionally add a header and/or trailer to the socket
1765 * output. If specified, write the total number of bytes sent into *sbytes.
1768 sendfile(struct proc
*p
, struct sendfile_args
*uap
)
1770 struct fileproc
*fp
;
1772 struct vm_object
*obj
;
1777 struct writev_args nuap
;
1778 struct sf_hdtr hdtr
;
1779 off_t off
, xfsize
, sbytes
= 0;
1782 if (sf_bufs
== NULL
) {
1783 /* Fail if initialization failed */
1788 * Do argument checking. Must be a regular file in, stream
1789 * type and connected socket out, positive offset.
1791 if (error
= fp_getfvp(p
, uap
->fd
, &fp
, &vp
))
1793 if (fp
->f_flag
& FREAD
) == 0) {
1798 if (vp
->v_type
!= VREG
|| obj
== NULL
) {
1802 error
= file_socket(uap
->s
, &so
);
1812 if (so
->so_type
!= SOCK_STREAM
) {
1816 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
1820 if (uap
->offset
< 0) {
1826 * If specified, get the pointer to the sf_hdtr struct for
1827 * any headers/trailers.
1829 if (uap
->hdtr
!= NULL
) {
1830 error
= copyin(CAST_USER_ADDR_T(uap
->hdtr
), &hdtr
, sizeof(hdtr
));
1834 * Send any headers. Wimp out and use writev(2).
1836 if (hdtr
.headers
!= NULL
) {
1838 nuap
.iovp
= hdtr
.headers
;
1839 nuap
.iovcnt
= hdtr
.hdr_cnt
;
1840 error
= writev(p
, &nuap
);
1843 sbytes
+= p
->p_retval
[0];
1848 * Protect against multiple writers to the socket.
1850 (void) sblock(&so
->so_snd
, M_WAIT
);
1853 * Loop through the pages in the file, starting with the requested
1854 * offset. Get a file page (do I/O if necessary), map the file page
1855 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1858 for (off
= uap
->offset
; ; off
+= xfsize
, sbytes
+= xfsize
) {
1859 vm_object_offset_t pindex
;
1860 vm_object_offset_t pgoff
;
1862 pindex
= OFF_TO_IDX(off
);
1865 * Calculate the amount to transfer. Not to exceed a page,
1866 * the EOF, or the passed in nbytes.
1868 xfsize
= obj
->un_pager
.vnp
.vnp_size
- off
;
1869 if (xfsize
> PAGE_SIZE_64
)
1871 pgoff
= (vm_object_offset_t
)(off
& PAGE_MASK_64
);
1872 if (PAGE_SIZE
- pgoff
< xfsize
)
1873 xfsize
= PAGE_SIZE_64
- pgoff
;
1874 if (uap
->nbytes
&& xfsize
> (uap
->nbytes
- sbytes
))
1875 xfsize
= uap
->nbytes
- sbytes
;
1879 * Optimize the non-blocking case by looking at the socket space
1880 * before going to the extra work of constituting the sf_buf.
1882 if ((so
->so_state
& SS_NBIO
) && sbspace(&so
->so_snd
) <= 0) {
1883 if (so
->so_state
& SS_CANTSENDMORE
)
1887 sbunlock(&so
->so_snd
, 0); /* will release lock */
1891 * Attempt to look up the page. If the page doesn't exist or the
1892 * part we're interested in isn't valid, then read it from disk.
1893 * If some other part of the kernel has this page (i.e. it's busy),
1894 * then disk I/O may be occuring on it, so wait and retry.
1896 pg
= vm_page_lookup(obj
, pindex
);
1897 if (pg
== NULL
|| (!(pg
->flags
& PG_BUSY
) && !pg
->busy
&&
1898 !vm_page_is_valid(pg
, pgoff
, xfsize
))) {
1904 pg
= vm_page_alloc(obj
, pindex
, VM_ALLOC_NORMAL
);
1910 * don't just clear PG_BUSY manually -
1911 * vm_page_alloc() should be considered opaque,
1912 * use the VM routine provided to clear
1919 * Ensure that our page is still around when the I/O completes.
1921 vm_page_io_start(pg
);
1924 * Get the page from backing store.
1926 bsize
= vp
->v_mount
->mnt_vfsstat
.f_iosize
;
1927 auio
.uio_iov
= &aiov
;
1928 auio
.uio_iovcnt
= 1;
1930 aiov
.iov_len
= MAXBSIZE
;
1931 auio
.uio_offset
= trunc_page(off
);
1932 auio
.uio_segflg
= UIO_NOCOPY
;
1933 auio
.uio_rw
= UIO_READ
;
1934 uio_setresid(&auio
, MAXBSIZE
);
1935 error
= VOP_READ(vp
, &auio
, IO_VMIO
| ((MAXBSIZE
/ bsize
) << 16),
1937 vm_page_flag_clear(pg
, PG_ZERO
);
1938 vm_page_io_finish(pg
);
1940 vm_page_unwire(pg
, 0);
1942 * See if anyone else might know about this page.
1943 * If not and it is not valid, then free it.
1945 if (pg
->wire_count
== 0 && pg
->valid
== 0 &&
1946 pg
->busy
== 0 && !(pg
->flags
& PG_BUSY
) &&
1947 pg
->hold_count
== 0)
1948 vm_page_lock_queues();
1950 vm_page_unlock_queues();
1951 sbunlock(&so
->so_snd
, 0); /* will release socket lock */
1955 if ((pg
->flags
& PG_BUSY
) || pg
->busy
) {
1957 if ((pg
->flags
& PG_BUSY
) || pg
->busy
) {
1959 * Page is busy. Wait and retry.
1961 vm_page_flag_set(pg
, PG_WANTED
);
1962 tsleep(pg
, PVM
, "sfpbsy", 0);
1967 * Protect from having the page ripped out from beneath us.
1972 * Allocate a kernel virtual page and insert the physical page
1975 sf
= sf_buf_alloc();
1977 pmap_qenter(sf
->kva
, &pg
, 1);
1979 * Get an mbuf header and set it up as having external storage.
1981 MGETHDR(m
, M_WAIT
, MT_DATA
);
1984 sbunlock(&so
->so_snd
, 0); /* will release socket lock */
1987 m
->m_ext
.ext_free
= sf_buf_free
;
1988 m
->m_ext
.ext_ref
= sf_buf_ref
;
1989 m
->m_ext
.ext_buf
= (void *)sf
->kva
;
1990 m
->m_ext
.ext_size
= PAGE_SIZE
;
1991 m
->m_data
= (char *) sf
->kva
+ pgoff
;
1992 m
->m_flags
|= M_EXT
;
1993 m
->m_pkthdr
.len
= m
->m_len
= xfsize
;
1995 * Add the buffer to the socket buffer chain.
1999 * Make sure that the socket is still able to take more data.
2000 * CANTSENDMORE being true usually means that the connection
2001 * was closed. so_error is true when an error was sensed after
2003 * The state is checked after the page mapping and buffer
2004 * allocation above since those operations may block and make
2005 * any socket checks stale. From this point forward, nothing
2006 * blocks before the pru_send (or more accurately, any blocking
2007 * results in a loop back to here to re-check).
2009 if ((so
->so_state
& SS_CANTSENDMORE
) || so
->so_error
) {
2010 if (so
->so_state
& SS_CANTSENDMORE
) {
2013 error
= so
->so_error
;
2017 sbunlock(&so
->so_snd
, 0); /* will release socket lock */
2021 * Wait for socket space to become available. We do this just
2022 * after checking the connection state above in order to avoid
2023 * a race condition with sbwait().
2025 if (sbspace(&so
->so_snd
) < so
->so_snd
.sb_lowat
) {
2026 if (so
->so_state
& SS_NBIO
) {
2028 sbunlock(&so
->so_snd
, 0); /* will release socket lock */
2032 error
= sbwait(&so
->so_snd
);
2034 * An error from sbwait usually indicates that we've
2035 * been interrupted by a signal. If we've sent anything
2036 * then return bytes sent, otherwise return the error.
2040 sbunlock(&so
->so_snd
, 0);
2045 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)(so
, 0, m
, 0, 0, p
);
2048 sbunlock(&so
->so_snd
, 0); /* will release socket lock */
2052 sbunlock(&so
->so_snd
, 0); /* will release socket lock */
2055 * Send trailers. Wimp out and use writev(2).
2057 if (uap
->hdtr
!= NULL
&& hdtr
.trailers
!= NULL
) {
2059 nuap
.iovp
= hdtr
.trailers
;
2060 nuap
.iovcnt
= hdtr
.trl_cnt
;
2061 error
= writev(p
, &nuap
);
2064 sbytes
+= p
->p_retval
[0];
2071 if (uap
->sbytes
!= NULL
) {
2072 /* XXX this appears bogus for some early failure conditions */
2073 copyout(&sbytes
, CAST_USER_ADDR_T(uap
->sbytes
), sizeof(off_t
));
2077 socket_unlock(so
, 1);