2 * Copyright (c) 1982, 1986, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * sendfile(2) and related extensions:
6 * Copyright (c) 1998, David Greenman. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
33 * $FreeBSD: src/sys/kern/uipc_syscalls.c,v 1.65.2.17 2003/04/04 17:11:16 tegge Exp $
36 #include "opt_ktrace.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/sysproto.h>
42 #include <sys/malloc.h>
43 #include <sys/filedesc.h>
44 #include <sys/event.h>
46 #include <sys/fcntl.h>
48 #include <sys/filio.h>
49 #include <sys/kern_syscall.h>
51 #include <sys/protosw.h>
52 #include <sys/sfbuf.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/socketops.h>
57 #include <sys/vnode.h>
59 #include <sys/mount.h>
61 #include <sys/ktrace.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_page.h>
66 #include <vm/vm_pageout.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_extern.h>
69 #include <sys/file2.h>
70 #include <sys/signalvar.h>
71 #include <sys/serialize.h>
73 #include <sys/thread2.h>
74 #include <sys/msgport2.h>
75 #include <sys/socketvar2.h>
76 #include <net/netmsg2.h>
77 #include <vm/vm_page2.h>
79 extern int use_soaccept_pred_fast
;
80 extern int use_sendfile_async
;
81 extern int use_soconnect_async
;
84 * System call interface to the socket abstraction.
87 extern struct fileops socketops
;
90 * socket_args(int domain, int type, int protocol)
93 kern_socket(int domain
, int type
, int protocol
, int *res
)
95 struct thread
*td
= curthread
;
96 struct filedesc
*fdp
= td
->td_proc
->p_fd
;
103 KKASSERT(td
->td_lwp
);
105 if (type
& SOCK_NONBLOCK
) {
106 type
&= ~SOCK_NONBLOCK
;
109 if (type
& SOCK_CLOEXEC
) {
110 type
&= ~SOCK_CLOEXEC
;
114 error
= falloc(td
->td_lwp
, &fp
, &fd
);
117 error
= socreate(domain
, &so
, type
, protocol
, td
);
119 fsetfd(fdp
, NULL
, fd
);
121 fp
->f_type
= DTYPE_SOCKET
;
122 fp
->f_flag
= FREAD
| FWRITE
| fflags
;
123 fp
->f_ops
= &socketops
;
125 if (oflags
& O_CLOEXEC
)
126 fdp
->fd_files
[fd
].fileflags
|= UF_EXCLOSE
;
138 sys_socket(struct socket_args
*uap
)
142 error
= kern_socket(uap
->domain
, uap
->type
, uap
->protocol
,
143 &uap
->sysmsg_iresult
);
149 kern_bind(int s
, struct sockaddr
*sa
)
151 struct thread
*td
= curthread
;
152 struct proc
*p
= td
->td_proc
;
157 error
= holdsock(p
->p_fd
, s
, &fp
);
160 error
= sobind((struct socket
*)fp
->f_data
, sa
, td
);
166 * bind_args(int s, caddr_t name, int namelen)
171 sys_bind(struct bind_args
*uap
)
176 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
179 error
= kern_bind(uap
->s
, sa
);
186 kern_listen(int s
, int backlog
)
188 struct thread
*td
= curthread
;
189 struct proc
*p
= td
->td_proc
;
194 error
= holdsock(p
->p_fd
, s
, &fp
);
197 error
= solisten((struct socket
*)fp
->f_data
, backlog
, td
);
203 * listen_args(int s, int backlog)
208 sys_listen(struct listen_args
*uap
)
212 error
= kern_listen(uap
->s
, uap
->backlog
);
217 * Returns the accepted socket as well.
219 * NOTE! The sockets sitting on so_comp/so_incomp might have 0 refs, the
220 * pool token is absolutely required to avoid a sofree() race,
221 * as well as to avoid tailq handling races.
224 soaccept_predicate(struct netmsg_so_notify
*msg
)
226 struct socket
*head
= msg
->base
.nm_so
;
229 if (head
->so_error
!= 0) {
230 msg
->base
.lmsg
.ms_error
= head
->so_error
;
233 lwkt_getpooltoken(head
);
234 if (!TAILQ_EMPTY(&head
->so_comp
)) {
235 /* Abuse nm_so field as copy in/copy out parameter. XXX JH */
236 so
= TAILQ_FIRST(&head
->so_comp
);
237 KKASSERT((so
->so_state
& (SS_INCOMP
| SS_COMP
)) == SS_COMP
);
238 TAILQ_REMOVE(&head
->so_comp
, so
, so_list
);
240 soclrstate(so
, SS_COMP
);
243 * Keep a reference before clearing the so_head
244 * to avoid racing socket close in netisr.
249 lwkt_relpooltoken(head
);
251 msg
->base
.lmsg
.ms_error
= 0;
252 msg
->base
.nm_so
= so
;
255 lwkt_relpooltoken(head
);
256 if (head
->so_state
& SS_CANTRCVMORE
) {
257 msg
->base
.lmsg
.ms_error
= ECONNABORTED
;
260 if (msg
->nm_fflags
& FNONBLOCK
) {
261 msg
->base
.lmsg
.ms_error
= EWOULDBLOCK
;
269 * The second argument to kern_accept() is a handle to a struct sockaddr.
270 * This allows kern_accept() to return a pointer to an allocated struct
271 * sockaddr which must be freed later with FREE(). The caller must
272 * initialize *name to NULL.
275 kern_accept(int s
, int fflags
, struct sockaddr
**name
, int *namelen
, int *res
,
278 struct thread
*td
= curthread
;
279 struct filedesc
*fdp
= td
->td_proc
->p_fd
;
280 struct file
*lfp
= NULL
;
281 struct file
*nfp
= NULL
;
283 struct socket
*head
, *so
;
284 struct netmsg_so_notify msg
;
286 u_int fflag
; /* type must match fp->f_flag */
290 if (name
&& namelen
&& *namelen
< 0)
293 error
= holdsock(td
->td_proc
->p_fd
, s
, &lfp
);
297 error
= falloc(td
->td_lwp
, &nfp
, &fd
);
298 if (error
) { /* Probably ran out of file descriptors. */
302 head
= (struct socket
*)lfp
->f_data
;
303 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
308 if (fflags
& O_FBLOCKING
)
309 fflags
|= lfp
->f_flag
& ~FNONBLOCK
;
310 else if (fflags
& O_FNONBLOCKING
)
311 fflags
|= lfp
->f_flag
| FNONBLOCK
;
313 fflags
= lfp
->f_flag
;
315 if (use_soaccept_pred_fast
) {
318 /* Initialize necessary parts for soaccept_predicate() */
319 netmsg_init(&msg
.base
, head
, &netisr_apanic_rport
, 0, NULL
);
320 msg
.nm_fflags
= fflags
;
322 lwkt_getpooltoken(head
);
323 pred
= soaccept_predicate(&msg
);
324 lwkt_relpooltoken(head
);
327 error
= msg
.base
.lmsg
.ms_error
;
335 /* optimize for uniprocessor case later XXX JH */
336 netmsg_init_abortable(&msg
.base
, head
, &curthread
->td_msgport
,
337 0, netmsg_so_notify
, netmsg_so_notify_doabort
);
338 msg
.nm_predicate
= soaccept_predicate
;
339 msg
.nm_fflags
= fflags
;
340 msg
.nm_etype
= NM_REVENT
;
341 error
= lwkt_domsg(head
->so_port
, &msg
.base
.lmsg
, PCATCH
);
347 * At this point we have the connection that's ready to be accepted.
349 * NOTE! soaccept_predicate() ref'd so for us, and soaccept() expects
350 * to eat the ref and turn it into a descriptor.
356 /* connection has been removed from the listen queue */
357 KNOTE(&head
->so_rcv
.ssb_kq
.ki_note
, 0);
359 if (sockflags
& SOCK_KERN_NOINHERIT
) {
360 fflag
&= ~(FASYNC
| FNONBLOCK
);
361 if (sockflags
& SOCK_NONBLOCK
)
364 if (head
->so_sigio
!= NULL
)
365 fsetown(fgetown(&head
->so_sigio
), &so
->so_sigio
);
368 nfp
->f_type
= DTYPE_SOCKET
;
370 nfp
->f_ops
= &socketops
;
372 /* Sync socket async state with file flags */
373 tmp
= fflag
& FASYNC
;
374 fo_ioctl(nfp
, FIOASYNC
, (caddr_t
)&tmp
, td
->td_ucred
, NULL
);
377 if (so
->so_faddr
!= NULL
) {
381 soaccept_generic(so
);
384 error
= soaccept(so
, &sa
);
388 * Set the returned name and namelen as applicable. Set the returned
389 * namelen to 0 for older code which might ignore the return value
393 if (sa
&& name
&& namelen
) {
394 if (*namelen
> sa
->sa_len
)
395 *namelen
= sa
->sa_len
;
405 * If an error occured clear the reserved descriptor, else associate
408 * Note that *res is normally ignored if an error is returned but
409 * a syscall message will still have access to the result code.
412 fsetfd(fdp
, NULL
, fd
);
414 if (sockflags
& SOCK_CLOEXEC
)
415 fdp
->fd_files
[fd
].fileflags
|= UF_EXCLOSE
;
417 fsetfd(fdp
, nfp
, fd
);
425 * accept(int s, caddr_t name, int *anamelen)
430 sys_accept(struct accept_args
*uap
)
432 struct sockaddr
*sa
= NULL
;
437 error
= copyin(uap
->anamelen
, &sa_len
, sizeof(sa_len
));
441 error
= kern_accept(uap
->s
, 0, &sa
, &sa_len
,
442 &uap
->sysmsg_iresult
, 0);
445 error
= copyout(sa
, uap
->name
, sa_len
);
447 error
= copyout(&sa_len
, uap
->anamelen
,
448 sizeof(*uap
->anamelen
));
453 error
= kern_accept(uap
->s
, 0, NULL
, 0,
454 &uap
->sysmsg_iresult
, 0);
460 * extaccept(int s, int fflags, caddr_t name, int *anamelen)
465 sys_extaccept(struct extaccept_args
*uap
)
467 struct sockaddr
*sa
= NULL
;
470 int fflags
= uap
->flags
& O_FMASK
;
473 error
= copyin(uap
->anamelen
, &sa_len
, sizeof(sa_len
));
477 error
= kern_accept(uap
->s
, fflags
, &sa
, &sa_len
,
478 &uap
->sysmsg_iresult
, 0);
481 error
= copyout(sa
, uap
->name
, sa_len
);
483 error
= copyout(&sa_len
, uap
->anamelen
,
484 sizeof(*uap
->anamelen
));
489 error
= kern_accept(uap
->s
, fflags
, NULL
, 0,
490 &uap
->sysmsg_iresult
, 0);
496 * accept4(int s, caddr_t name, int *anamelen, int flags)
501 sys_accept4(struct accept4_args
*uap
)
503 struct sockaddr
*sa
= NULL
;
508 if (uap
->flags
& ~(SOCK_NONBLOCK
| SOCK_CLOEXEC
))
510 sockflags
= uap
->flags
| SOCK_KERN_NOINHERIT
;
513 error
= copyin(uap
->anamelen
, &sa_len
, sizeof(sa_len
));
517 error
= kern_accept(uap
->s
, 0, &sa
, &sa_len
,
518 &uap
->sysmsg_iresult
, sockflags
);
521 error
= copyout(sa
, uap
->name
, sa_len
);
523 error
= copyout(&sa_len
, uap
->anamelen
,
524 sizeof(*uap
->anamelen
));
529 error
= kern_accept(uap
->s
, 0, NULL
, 0,
530 &uap
->sysmsg_iresult
, sockflags
);
536 * Returns TRUE if predicate satisfied.
539 soconnected_predicate(struct netmsg_so_notify
*msg
)
541 struct socket
*so
= msg
->base
.nm_so
;
543 /* check predicate */
544 if (!(so
->so_state
& SS_ISCONNECTING
) || so
->so_error
!= 0) {
545 msg
->base
.lmsg
.ms_error
= so
->so_error
;
553 kern_connect(int s
, int fflags
, struct sockaddr
*sa
)
555 struct thread
*td
= curthread
;
556 struct proc
*p
= td
->td_proc
;
559 int error
, interrupted
= 0;
561 error
= holdsock(p
->p_fd
, s
, &fp
);
564 so
= (struct socket
*)fp
->f_data
;
566 if (fflags
& O_FBLOCKING
)
567 /* fflags &= ~FNONBLOCK; */;
568 else if (fflags
& O_FNONBLOCKING
)
573 if (so
->so_state
& SS_ISCONNECTING
) {
577 error
= soconnect(so
, sa
, td
, use_soconnect_async
? FALSE
: TRUE
);
580 if ((fflags
& FNONBLOCK
) && (so
->so_state
& SS_ISCONNECTING
)) {
584 if ((so
->so_state
& SS_ISCONNECTING
) && so
->so_error
== 0) {
585 struct netmsg_so_notify msg
;
587 netmsg_init_abortable(&msg
.base
, so
,
588 &curthread
->td_msgport
,
591 netmsg_so_notify_doabort
);
592 msg
.nm_predicate
= soconnected_predicate
;
593 msg
.nm_etype
= NM_REVENT
;
594 error
= lwkt_domsg(so
->so_port
, &msg
.base
.lmsg
, PCATCH
);
595 if (error
== EINTR
|| error
== ERESTART
)
599 error
= so
->so_error
;
604 soclrstate(so
, SS_ISCONNECTING
);
605 if (error
== ERESTART
)
613 * connect_args(int s, caddr_t name, int namelen)
618 sys_connect(struct connect_args
*uap
)
623 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
626 error
= kern_connect(uap
->s
, 0, sa
);
633 * connect_args(int s, int fflags, caddr_t name, int namelen)
638 sys_extconnect(struct extconnect_args
*uap
)
642 int fflags
= uap
->flags
& O_FMASK
;
644 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
647 error
= kern_connect(uap
->s
, fflags
, sa
);
654 kern_socketpair(int domain
, int type
, int protocol
, int *sv
)
656 struct thread
*td
= curthread
;
657 struct filedesc
*fdp
;
658 struct file
*fp1
, *fp2
;
659 struct socket
*so1
, *so2
;
664 if (type
& SOCK_NONBLOCK
) {
665 type
&= ~SOCK_NONBLOCK
;
668 if (type
& SOCK_CLOEXEC
) {
669 type
&= ~SOCK_CLOEXEC
;
673 fdp
= td
->td_proc
->p_fd
;
674 error
= socreate(domain
, &so1
, type
, protocol
, td
);
677 error
= socreate(domain
, &so2
, type
, protocol
, td
);
680 error
= falloc(td
->td_lwp
, &fp1
, &fd1
);
685 error
= falloc(td
->td_lwp
, &fp2
, &fd2
);
690 error
= soconnect2(so1
, so2
);
693 if (type
== SOCK_DGRAM
) {
695 * Datagram socket connection is asymmetric.
697 error
= soconnect2(so2
, so1
);
701 fp1
->f_type
= fp2
->f_type
= DTYPE_SOCKET
;
702 fp1
->f_flag
= fp2
->f_flag
= FREAD
|FWRITE
|fflags
;
703 fp1
->f_ops
= fp2
->f_ops
= &socketops
;
704 if (oflags
& O_CLOEXEC
) {
705 fdp
->fd_files
[fd1
].fileflags
|= UF_EXCLOSE
;
706 fdp
->fd_files
[fd2
].fileflags
|= UF_EXCLOSE
;
708 fsetfd(fdp
, fp1
, fd1
);
709 fsetfd(fdp
, fp2
, fd2
);
714 fsetfd(fdp
, NULL
, fd2
);
717 fsetfd(fdp
, NULL
, fd1
);
720 (void)soclose(so2
, 0);
722 (void)soclose(so1
, 0);
727 * socketpair(int domain, int type, int protocol, int *rsv)
730 sys_socketpair(struct socketpair_args
*uap
)
734 error
= kern_socketpair(uap
->domain
, uap
->type
, uap
->protocol
, sockv
);
737 error
= copyout(sockv
, uap
->rsv
, sizeof(sockv
));
740 kern_close(sockv
[0]);
741 kern_close(sockv
[1]);
749 kern_sendmsg(int s
, struct sockaddr
*sa
, struct uio
*auio
,
750 struct mbuf
*control
, int flags
, size_t *res
)
752 struct thread
*td
= curthread
;
753 struct lwp
*lp
= td
->td_lwp
;
754 struct proc
*p
= td
->td_proc
;
760 struct iovec
*ktriov
= NULL
;
764 error
= holdsock(p
->p_fd
, s
, &fp
);
768 if (KTRPOINT(td
, KTR_GENIO
)) {
769 int iovlen
= auio
->uio_iovcnt
* sizeof (struct iovec
);
771 ktriov
= kmalloc(iovlen
, M_TEMP
, M_WAITOK
);
772 bcopy((caddr_t
)auio
->uio_iov
, (caddr_t
)ktriov
, iovlen
);
776 len
= auio
->uio_resid
;
777 so
= (struct socket
*)fp
->f_data
;
778 if ((flags
& (MSG_FNONBLOCKING
|MSG_FBLOCKING
)) == 0) {
779 if (fp
->f_flag
& FNONBLOCK
)
780 flags
|= MSG_FNONBLOCKING
;
782 error
= so_pru_sosend(so
, sa
, auio
, NULL
, control
, flags
, td
);
784 if (auio
->uio_resid
!= len
&& (error
== ERESTART
||
785 error
== EINTR
|| error
== EWOULDBLOCK
))
787 if (error
== EPIPE
&& !(flags
& MSG_NOSIGNAL
) &&
788 !(so
->so_options
& SO_NOSIGPIPE
))
789 lwpsignal(p
, lp
, SIGPIPE
);
792 if (ktriov
!= NULL
) {
794 ktruio
.uio_iov
= ktriov
;
795 ktruio
.uio_resid
= len
- auio
->uio_resid
;
796 ktrgenio(lp
, s
, UIO_WRITE
, &ktruio
, error
);
798 kfree(ktriov
, M_TEMP
);
802 *res
= len
- auio
->uio_resid
;
808 * sendto_args(int s, caddr_t buf, size_t len, int flags, caddr_t to, int tolen)
813 sys_sendto(struct sendto_args
*uap
)
815 struct thread
*td
= curthread
;
818 struct sockaddr
*sa
= NULL
;
822 error
= getsockaddr(&sa
, uap
->to
, uap
->tolen
);
826 aiov
.iov_base
= uap
->buf
;
827 aiov
.iov_len
= uap
->len
;
828 auio
.uio_iov
= &aiov
;
831 auio
.uio_resid
= uap
->len
;
832 auio
.uio_segflg
= UIO_USERSPACE
;
833 auio
.uio_rw
= UIO_WRITE
;
836 error
= kern_sendmsg(uap
->s
, sa
, &auio
, NULL
, uap
->flags
,
837 &uap
->sysmsg_szresult
);
845 * sendmsg_args(int s, caddr_t msg, int flags)
850 sys_sendmsg(struct sendmsg_args
*uap
)
852 struct thread
*td
= curthread
;
855 struct iovec aiov
[UIO_SMALLIOV
], *iov
= NULL
;
856 struct sockaddr
*sa
= NULL
;
857 struct mbuf
*control
= NULL
;
860 error
= copyin(uap
->msg
, (caddr_t
)&msg
, sizeof(msg
));
865 * Conditionally copyin msg.msg_name.
868 error
= getsockaddr(&sa
, msg
.msg_name
, msg
.msg_namelen
);
876 error
= iovec_copyin(msg
.msg_iov
, &iov
, aiov
, msg
.msg_iovlen
,
881 auio
.uio_iovcnt
= msg
.msg_iovlen
;
883 auio
.uio_segflg
= UIO_USERSPACE
;
884 auio
.uio_rw
= UIO_WRITE
;
888 * Conditionally copyin msg.msg_control.
890 if (msg
.msg_control
) {
891 if (msg
.msg_controllen
< sizeof(struct cmsghdr
) ||
892 msg
.msg_controllen
> MLEN
) {
896 control
= m_get(M_WAITOK
, MT_CONTROL
);
897 if (control
== NULL
) {
901 control
->m_len
= msg
.msg_controllen
;
902 error
= copyin(msg
.msg_control
, mtod(control
, caddr_t
),
910 error
= kern_sendmsg(uap
->s
, sa
, &auio
, control
, uap
->flags
,
911 &uap
->sysmsg_szresult
);
914 iovec_free(&iov
, aiov
);
922 * kern_recvmsg() takes a handle to sa and control. If the handle is non-
923 * null, it returns a dynamically allocated struct sockaddr and an mbuf.
924 * Don't forget to FREE() and m_free() these if they are returned.
927 kern_recvmsg(int s
, struct sockaddr
**sa
, struct uio
*auio
,
928 struct mbuf
**control
, int *flags
, size_t *res
)
930 struct thread
*td
= curthread
;
931 struct proc
*p
= td
->td_proc
;
938 struct iovec
*ktriov
= NULL
;
942 error
= holdsock(p
->p_fd
, s
, &fp
);
946 if (KTRPOINT(td
, KTR_GENIO
)) {
947 int iovlen
= auio
->uio_iovcnt
* sizeof (struct iovec
);
949 ktriov
= kmalloc(iovlen
, M_TEMP
, M_WAITOK
);
950 bcopy(auio
->uio_iov
, ktriov
, iovlen
);
954 len
= auio
->uio_resid
;
955 so
= (struct socket
*)fp
->f_data
;
957 if (flags
== NULL
|| (*flags
& (MSG_FNONBLOCKING
|MSG_FBLOCKING
)) == 0) {
958 if (fp
->f_flag
& FNONBLOCK
) {
960 *flags
|= MSG_FNONBLOCKING
;
962 lflags
= MSG_FNONBLOCKING
;
968 error
= so_pru_soreceive(so
, sa
, auio
, NULL
, control
, flags
);
970 if (auio
->uio_resid
!= len
&& (error
== ERESTART
||
971 error
== EINTR
|| error
== EWOULDBLOCK
))
975 if (ktriov
!= NULL
) {
977 ktruio
.uio_iov
= ktriov
;
978 ktruio
.uio_resid
= len
- auio
->uio_resid
;
979 ktrgenio(td
->td_lwp
, s
, UIO_READ
, &ktruio
, error
);
981 kfree(ktriov
, M_TEMP
);
985 *res
= len
- auio
->uio_resid
;
991 * recvfrom_args(int s, caddr_t buf, size_t len, int flags,
992 * caddr_t from, int *fromlenaddr)
997 sys_recvfrom(struct recvfrom_args
*uap
)
999 struct thread
*td
= curthread
;
1002 struct sockaddr
*sa
= NULL
;
1005 if (uap
->from
&& uap
->fromlenaddr
) {
1006 error
= copyin(uap
->fromlenaddr
, &fromlen
, sizeof(fromlen
));
1014 aiov
.iov_base
= uap
->buf
;
1015 aiov
.iov_len
= uap
->len
;
1016 auio
.uio_iov
= &aiov
;
1017 auio
.uio_iovcnt
= 1;
1018 auio
.uio_offset
= 0;
1019 auio
.uio_resid
= uap
->len
;
1020 auio
.uio_segflg
= UIO_USERSPACE
;
1021 auio
.uio_rw
= UIO_READ
;
1024 error
= kern_recvmsg(uap
->s
, uap
->from
? &sa
: NULL
, &auio
, NULL
,
1025 &uap
->flags
, &uap
->sysmsg_szresult
);
1027 if (error
== 0 && uap
->from
) {
1028 /* note: sa may still be NULL */
1030 fromlen
= MIN(fromlen
, sa
->sa_len
);
1031 error
= copyout(sa
, uap
->from
, fromlen
);
1036 error
= copyout(&fromlen
, uap
->fromlenaddr
,
1041 kfree(sa
, M_SONAME
);
1047 * recvmsg_args(int s, struct msghdr *msg, int flags)
1052 sys_recvmsg(struct recvmsg_args
*uap
)
1054 struct thread
*td
= curthread
;
1057 struct iovec aiov
[UIO_SMALLIOV
], *iov
= NULL
;
1058 struct mbuf
*m
, *control
= NULL
;
1059 struct sockaddr
*sa
= NULL
;
1061 socklen_t
*ufromlenp
, *ucontrollenp
;
1062 int error
, fromlen
, controllen
, len
, flags
, *uflagsp
;
1065 * This copyin handles everything except the iovec.
1067 error
= copyin(uap
->msg
, &msg
, sizeof(msg
));
1071 if (msg
.msg_name
&& msg
.msg_namelen
< 0)
1073 if (msg
.msg_control
&& msg
.msg_controllen
< 0)
1076 ufromlenp
= (socklen_t
*)((caddr_t
)uap
->msg
+ offsetof(struct msghdr
,
1078 ucontrollenp
= (socklen_t
*)((caddr_t
)uap
->msg
+ offsetof(struct msghdr
,
1080 uflagsp
= (int *)((caddr_t
)uap
->msg
+ offsetof(struct msghdr
,
1086 error
= iovec_copyin(msg
.msg_iov
, &iov
, aiov
, msg
.msg_iovlen
,
1091 auio
.uio_iovcnt
= msg
.msg_iovlen
;
1092 auio
.uio_offset
= 0;
1093 auio
.uio_segflg
= UIO_USERSPACE
;
1094 auio
.uio_rw
= UIO_READ
;
1099 error
= kern_recvmsg(uap
->s
,
1100 (msg
.msg_name
? &sa
: NULL
), &auio
,
1101 (msg
.msg_control
? &control
: NULL
), &flags
,
1102 &uap
->sysmsg_szresult
);
1105 * Conditionally copyout the name and populate the namelen field.
1107 if (error
== 0 && msg
.msg_name
) {
1108 /* note: sa may still be NULL */
1110 fromlen
= MIN(msg
.msg_namelen
, sa
->sa_len
);
1111 error
= copyout(sa
, msg
.msg_name
, fromlen
);
1116 error
= copyout(&fromlen
, ufromlenp
,
1117 sizeof(*ufromlenp
));
1121 * Copyout msg.msg_control and msg.msg_controllen.
1123 if (error
== 0 && msg
.msg_control
) {
1124 len
= msg
.msg_controllen
;
1126 ctlbuf
= (caddr_t
)msg
.msg_control
;
1128 while(m
&& len
> 0) {
1129 unsigned int tocopy
;
1131 if (len
>= m
->m_len
) {
1134 msg
.msg_flags
|= MSG_CTRUNC
;
1138 error
= copyout(mtod(m
, caddr_t
), ctlbuf
, tocopy
);
1146 controllen
= ctlbuf
- (caddr_t
)msg
.msg_control
;
1147 error
= copyout(&controllen
, ucontrollenp
,
1148 sizeof(*ucontrollenp
));
1152 error
= copyout(&flags
, uflagsp
, sizeof(*uflagsp
));
1156 kfree(sa
, M_SONAME
);
1157 iovec_free(&iov
, aiov
);
1164 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
1165 * in kernel pointer instead of a userland pointer. This allows us
1166 * to manipulate socket options in the emulation code.
1169 kern_setsockopt(int s
, struct sockopt
*sopt
)
1171 struct thread
*td
= curthread
;
1172 struct proc
*p
= td
->td_proc
;
1176 if (sopt
->sopt_val
== NULL
&& sopt
->sopt_valsize
!= 0)
1178 if (sopt
->sopt_val
!= NULL
&& sopt
->sopt_valsize
== 0)
1180 if (sopt
->sopt_valsize
> SOMAXOPT_SIZE
) /* unsigned */
1183 error
= holdsock(p
->p_fd
, s
, &fp
);
1187 error
= sosetopt((struct socket
*)fp
->f_data
, sopt
);
1193 * setsockopt_args(int s, int level, int name, caddr_t val, int valsize)
1198 sys_setsockopt(struct setsockopt_args
*uap
)
1200 struct thread
*td
= curthread
;
1201 struct sockopt sopt
;
1204 sopt
.sopt_level
= uap
->level
;
1205 sopt
.sopt_name
= uap
->name
;
1206 sopt
.sopt_valsize
= uap
->valsize
;
1208 sopt
.sopt_val
= NULL
;
1210 if (sopt
.sopt_valsize
> SOMAXOPT_SIZE
) /* unsigned */
1213 sopt
.sopt_val
= kmalloc(sopt
.sopt_valsize
, M_TEMP
, M_WAITOK
);
1214 error
= copyin(uap
->val
, sopt
.sopt_val
, sopt
.sopt_valsize
);
1219 error
= kern_setsockopt(uap
->s
, &sopt
);
1222 kfree(sopt
.sopt_val
, M_TEMP
);
1227 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
1228 * in kernel pointer instead of a userland pointer. This allows us
1229 * to manipulate socket options in the emulation code.
1232 kern_getsockopt(int s
, struct sockopt
*sopt
)
1234 struct thread
*td
= curthread
;
1235 struct proc
*p
= td
->td_proc
;
1239 if (sopt
->sopt_val
== NULL
&& sopt
->sopt_valsize
!= 0)
1241 if (sopt
->sopt_val
!= NULL
&& sopt
->sopt_valsize
== 0)
1244 error
= holdsock(p
->p_fd
, s
, &fp
);
1248 error
= sogetopt((struct socket
*)fp
->f_data
, sopt
);
1254 * getsockopt_args(int s, int level, int name, caddr_t val, int *avalsize)
1259 sys_getsockopt(struct getsockopt_args
*uap
)
1261 struct thread
*td
= curthread
;
1262 struct sockopt sopt
;
1263 int error
, valsize
, valszmax
, mflag
= 0;
1266 error
= copyin(uap
->avalsize
, &valsize
, sizeof(valsize
));
1273 sopt
.sopt_level
= uap
->level
;
1274 sopt
.sopt_name
= uap
->name
;
1275 sopt
.sopt_valsize
= valsize
;
1277 sopt
.sopt_val
= NULL
;
1279 if (td
->td_proc
->p_ucred
->cr_uid
== 0) {
1280 valszmax
= SOMAXOPT_SIZE0
;
1283 valszmax
= SOMAXOPT_SIZE
;
1285 if (sopt
.sopt_valsize
> valszmax
) /* unsigned */
1288 sopt
.sopt_val
= kmalloc(sopt
.sopt_valsize
, M_TEMP
,
1290 if (sopt
.sopt_val
== NULL
)
1292 error
= copyin(uap
->val
, sopt
.sopt_val
, sopt
.sopt_valsize
);
1297 error
= kern_getsockopt(uap
->s
, &sopt
);
1300 valsize
= sopt
.sopt_valsize
;
1301 error
= copyout(&valsize
, uap
->avalsize
, sizeof(valsize
));
1305 error
= copyout(sopt
.sopt_val
, uap
->val
, sopt
.sopt_valsize
);
1308 kfree(sopt
.sopt_val
, M_TEMP
);
1313 * The second argument to kern_getsockname() is a handle to a struct sockaddr.
1314 * This allows kern_getsockname() to return a pointer to an allocated struct
1315 * sockaddr which must be freed later with FREE(). The caller must
1316 * initialize *name to NULL.
1319 kern_getsockname(int s
, struct sockaddr
**name
, int *namelen
)
1321 struct thread
*td
= curthread
;
1322 struct proc
*p
= td
->td_proc
;
1325 struct sockaddr
*sa
= NULL
;
1328 error
= holdsock(p
->p_fd
, s
, &fp
);
1335 so
= (struct socket
*)fp
->f_data
;
1336 error
= so_pru_sockaddr(so
, &sa
);
1341 *namelen
= MIN(*namelen
, sa
->sa_len
);
1351 * getsockname_args(int fdes, caddr_t asa, int *alen)
1358 sys_getsockname(struct getsockname_args
*uap
)
1360 struct sockaddr
*sa
= NULL
;
1363 error
= copyin(uap
->alen
, &sa_len
, sizeof(sa_len
));
1367 error
= kern_getsockname(uap
->fdes
, &sa
, &sa_len
);
1370 error
= copyout(sa
, uap
->asa
, sa_len
);
1372 error
= copyout(&sa_len
, uap
->alen
, sizeof(*uap
->alen
));
1374 kfree(sa
, M_SONAME
);
1379 * The second argument to kern_getpeername() is a handle to a struct sockaddr.
1380 * This allows kern_getpeername() to return a pointer to an allocated struct
1381 * sockaddr which must be freed later with FREE(). The caller must
1382 * initialize *name to NULL.
1385 kern_getpeername(int s
, struct sockaddr
**name
, int *namelen
)
1387 struct thread
*td
= curthread
;
1388 struct proc
*p
= td
->td_proc
;
1391 struct sockaddr
*sa
= NULL
;
1394 error
= holdsock(p
->p_fd
, s
, &fp
);
1401 so
= (struct socket
*)fp
->f_data
;
1402 if ((so
->so_state
& (SS_ISCONNECTED
|SS_ISCONFIRMING
)) == 0) {
1406 error
= so_pru_peeraddr(so
, &sa
);
1411 *namelen
= MIN(*namelen
, sa
->sa_len
);
1421 * getpeername_args(int fdes, caddr_t asa, int *alen)
1423 * Get name of peer for connected socket.
1428 sys_getpeername(struct getpeername_args
*uap
)
1430 struct sockaddr
*sa
= NULL
;
1433 error
= copyin(uap
->alen
, &sa_len
, sizeof(sa_len
));
1437 error
= kern_getpeername(uap
->fdes
, &sa
, &sa_len
);
1440 error
= copyout(sa
, uap
->asa
, sa_len
);
1442 error
= copyout(&sa_len
, uap
->alen
, sizeof(*uap
->alen
));
1444 kfree(sa
, M_SONAME
);
1449 getsockaddr(struct sockaddr
**namp
, caddr_t uaddr
, size_t len
)
1451 struct sockaddr
*sa
;
1455 if (len
> SOCK_MAXADDRLEN
)
1456 return ENAMETOOLONG
;
1457 if (len
< offsetof(struct sockaddr
, sa_data
[0]))
1459 sa
= kmalloc(len
, M_SONAME
, M_WAITOK
);
1460 error
= copyin(uaddr
, sa
, len
);
1462 kfree(sa
, M_SONAME
);
1464 #if BYTE_ORDER != BIG_ENDIAN
1466 * The bind(), connect(), and sendto() syscalls were not
1467 * versioned for COMPAT_43. Thus, this check must stay.
1469 if (sa
->sa_family
== 0 && sa
->sa_len
< AF_MAX
)
1470 sa
->sa_family
= sa
->sa_len
;
1479 * Detach a mapped page and release resources back to the system.
1480 * We must release our wiring and if the object is ripped out
1481 * from under the vm_page we become responsible for freeing the
1487 sf_buf_mfree(void *arg
)
1489 struct sf_buf
*sf
= arg
;
1492 m
= sf_buf_page(sf
);
1493 if (sf_buf_free(sf
)) {
1494 /* sf invalid now */
1495 vm_page_sbusy_drop(m
);
1497 if (m
->object
== NULL
&&
1498 m
->wire_count
== 0 &&
1499 (m
->flags
& PG_NEED_COMMIT
) == 0) {
1510 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1511 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1513 * Send a file specified by 'fd' and starting at 'offset' to a socket
1514 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1515 * nbytes == 0. Optionally add a header and/or trailer to the socket
1516 * output. If specified, write the total number of bytes sent into *sbytes.
1518 * In FreeBSD kern/uipc_syscalls.c,v 1.103, a bug was fixed that caused
1519 * the headers to count against the remaining bytes to be sent from
1520 * the file descriptor. We may wish to implement a compatibility syscall
1526 sys_sendfile(struct sendfile_args
*uap
)
1528 struct thread
*td
= curthread
;
1529 struct proc
*p
= td
->td_proc
;
1531 struct vnode
*vp
= NULL
;
1532 struct sf_hdtr hdtr
;
1533 struct iovec aiov
[UIO_SMALLIOV
], *iov
= NULL
;
1535 struct mbuf
*mheader
= NULL
;
1538 off_t hdtr_size
= 0;
1545 * Do argument checking. Must be a regular file in, stream
1546 * type and connected socket out, positive offset.
1548 fp
= holdfp(p
->p_fd
, uap
->fd
, FREAD
);
1552 if (fp
->f_type
!= DTYPE_VNODE
) {
1556 vp
= (struct vnode
*)fp
->f_data
;
1561 * If specified, get the pointer to the sf_hdtr struct for
1562 * any headers/trailers.
1565 error
= copyin(uap
->hdtr
, &hdtr
, sizeof(hdtr
));
1572 error
= iovec_copyin(hdtr
.headers
, &iov
, aiov
,
1573 hdtr
.hdr_cnt
, &hbytes
);
1577 auio
.uio_iovcnt
= hdtr
.hdr_cnt
;
1578 auio
.uio_offset
= 0;
1579 auio
.uio_segflg
= UIO_USERSPACE
;
1580 auio
.uio_rw
= UIO_WRITE
;
1582 auio
.uio_resid
= hbytes
;
1584 mheader
= m_uiomove(&auio
);
1586 iovec_free(&iov
, aiov
);
1587 if (mheader
== NULL
)
1592 error
= kern_sendfile(vp
, uap
->s
, uap
->offset
, uap
->nbytes
, mheader
,
1593 &sbytes
, uap
->flags
);
1598 * Send trailers. Wimp out and use writev(2).
1600 if (uap
->hdtr
!= NULL
&& hdtr
.trailers
!= NULL
) {
1601 error
= iovec_copyin(hdtr
.trailers
, &iov
, aiov
,
1602 hdtr
.trl_cnt
, &auio
.uio_resid
);
1606 auio
.uio_iovcnt
= hdtr
.trl_cnt
;
1607 auio
.uio_offset
= 0;
1608 auio
.uio_segflg
= UIO_USERSPACE
;
1609 auio
.uio_rw
= UIO_WRITE
;
1612 tbytes
= 0; /* avoid gcc warnings */
1613 error
= kern_sendmsg(uap
->s
, NULL
, &auio
, NULL
, 0, &tbytes
);
1615 iovec_free(&iov
, aiov
);
1618 hdtr_size
+= tbytes
; /* trailer bytes successfully sent */
1624 if (uap
->sbytes
!= NULL
) {
1625 sbytes
+= hdtr_size
;
1626 copyout(&sbytes
, uap
->sbytes
, sizeof(off_t
));
1632 kern_sendfile(struct vnode
*vp
, int sfd
, off_t offset
, size_t nbytes
,
1633 struct mbuf
*mheader
, off_t
*sbytes
, int flags
)
1635 struct thread
*td
= curthread
;
1636 struct proc
*p
= td
->td_proc
;
1637 struct vm_object
*obj
;
1640 struct mbuf
*m
, *mp
;
1643 off_t off
, xfsize
, xbytes
;
1647 if (vp
->v_type
!= VREG
) {
1651 if ((obj
= vp
->v_object
) == NULL
) {
1655 error
= holdsock(p
->p_fd
, sfd
, &fp
);
1658 so
= (struct socket
*)fp
->f_data
;
1659 if (so
->so_type
!= SOCK_STREAM
) {
1663 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
1673 * preallocation is required for asynchronous passing of mbufs,
1674 * otherwise we can wind up building up an infinite number of
1675 * mbufs during the asynchronous latency.
1677 if ((so
->so_snd
.ssb_flags
& (SSB_PREALLOC
| SSB_STOPSUPP
)) == 0) {
1686 * Protect against multiple writers to the socket.
1687 * We need at least a shared lock on the VM object
1689 ssb_lock(&so
->so_snd
, M_WAITOK
);
1690 vm_object_hold_shared(obj
);
1693 * Loop through the pages in the file, starting with the requested
1694 * offset. Get a file page (do I/O if necessary), map the file page
1695 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1698 for (off
= offset
; ;
1699 off
+= xfsize
, *sbytes
+= xfsize
+ hbytes
, xbytes
+= xfsize
) {
1705 pindex
= OFF_TO_IDX(off
);
1710 * Calculate the amount to transfer. Not to exceed a page,
1711 * the EOF, or the passed in nbytes.
1713 xfsize
= vp
->v_filesize
- off
;
1714 if (xfsize
> PAGE_SIZE
)
1716 pgoff
= (vm_offset_t
)(off
& PAGE_MASK
);
1717 if (PAGE_SIZE
- pgoff
< xfsize
)
1718 xfsize
= PAGE_SIZE
- pgoff
;
1719 if (nbytes
&& xfsize
> (nbytes
- xbytes
))
1720 xfsize
= nbytes
- xbytes
;
1724 * Optimize the non-blocking case by looking at the socket space
1725 * before going to the extra work of constituting the sf_buf.
1727 if (so
->so_snd
.ssb_flags
& SSB_PREALLOC
)
1728 space
= ssb_space_prealloc(&so
->so_snd
);
1730 space
= ssb_space(&so
->so_snd
);
1732 if ((fp
->f_flag
& FNONBLOCK
) && space
<= 0) {
1733 if (so
->so_state
& SS_CANTSENDMORE
)
1741 * Attempt to look up the page.
1743 * Try to find the data using a shared vm_object token and
1744 * vm_page_lookup_sbusy_try() first.
1746 * If data is missing, use a UIO_NOCOPY VOP_READ to load
1747 * the missing data and loop back up. We avoid all sorts
1748 * of problems by not trying to hold onto the page during
1751 * NOTE: The soft-busy will temporary block filesystem
1752 * truncation operations when a file is removed
1753 * while the sendfile is running.
1755 pg
= vm_page_lookup_sbusy_try(obj
, pindex
, pgoff
, xfsize
);
1761 if (++loops
> 100000) {
1762 kprintf("sendfile: VOP operation failed "
1763 "to retain page\n");
1768 vm_object_drop(obj
);
1769 bsize
= vp
->v_mount
->mnt_stat
.f_iosize
;
1770 auio
.uio_iov
= &aiov
;
1771 auio
.uio_iovcnt
= 1;
1773 aiov
.iov_len
= MAXBSIZE
;
1774 auio
.uio_resid
= MAXBSIZE
;
1775 auio
.uio_offset
= trunc_page(off
);
1776 auio
.uio_segflg
= UIO_NOCOPY
;
1777 auio
.uio_rw
= UIO_READ
;
1780 vn_lock(vp
, LK_SHARED
| LK_RETRY
);
1781 error
= VOP_READ(vp
, &auio
,
1782 IO_VMIO
| ((MAXBSIZE
/ bsize
) << 16),
1785 vm_object_hold_shared(obj
);
1793 * Get a sendfile buf. We usually wait as long as necessary,
1794 * but this wait can be interrupted.
1796 if ((sf
= sf_buf_alloc(pg
)) == NULL
) {
1797 vm_page_sbusy_drop(pg
);
1798 /* vm_page_try_to_free(pg); */
1804 * Get an mbuf header and set it up as having external storage.
1806 MGETHDR(m
, M_WAITOK
, MT_DATA
);
1809 vm_page_sbusy_drop(pg
);
1810 /* vm_page_try_to_free(pg); */
1815 m
->m_ext
.ext_free
= sf_buf_mfree
;
1816 m
->m_ext
.ext_ref
= sf_buf_ref
;
1817 m
->m_ext
.ext_arg
= sf
;
1818 m
->m_ext
.ext_buf
= (void *)sf_buf_kva(sf
);
1819 m
->m_ext
.ext_size
= PAGE_SIZE
;
1820 m
->m_data
= (char *)sf_buf_kva(sf
) + pgoff
;
1821 m
->m_flags
|= M_EXT
;
1822 m
->m_pkthdr
.len
= m
->m_len
= xfsize
;
1823 KKASSERT((m
->m_flags
& (M_EXT_CLUSTER
)) == 0);
1825 if (mheader
!= NULL
) {
1826 hbytes
= mheader
->m_pkthdr
.len
;
1827 mheader
->m_pkthdr
.len
+= m
->m_pkthdr
.len
;
1836 * Add the buffer to the socket buffer chain.
1841 * Make sure that the socket is still able to take more data.
1842 * CANTSENDMORE being true usually means that the connection
1843 * was closed. so_error is true when an error was sensed after
1845 * The state is checked after the page mapping and buffer
1846 * allocation above since those operations may block and make
1847 * any socket checks stale. From this point forward, nothing
1848 * blocks before the pru_send (or more accurately, any blocking
1849 * results in a loop back to here to re-check).
1851 if ((so
->so_state
& SS_CANTSENDMORE
) || so
->so_error
) {
1852 if (so
->so_state
& SS_CANTSENDMORE
) {
1855 error
= so
->so_error
;
1863 * Wait for socket space to become available. We do this just
1864 * after checking the connection state above in order to avoid
1865 * a race condition with ssb_wait().
1867 if (so
->so_snd
.ssb_flags
& SSB_PREALLOC
)
1868 space
= ssb_space_prealloc(&so
->so_snd
);
1870 space
= ssb_space(&so
->so_snd
);
1872 if (space
< m
->m_pkthdr
.len
&& space
< so
->so_snd
.ssb_lowat
) {
1873 if (fp
->f_flag
& FNONBLOCK
) {
1879 error
= ssb_wait(&so
->so_snd
);
1881 * An error from ssb_wait usually indicates that we've
1882 * been interrupted by a signal. If we've sent anything
1883 * then return bytes sent, otherwise return the error.
1893 if (so
->so_snd
.ssb_flags
& SSB_PREALLOC
) {
1894 for (mp
= m
; mp
!= NULL
; mp
= mp
->m_next
)
1895 ssb_preallocstream(&so
->so_snd
, mp
);
1897 if (use_sendfile_async
)
1898 error
= so_pru_senda(so
, 0, m
, NULL
, NULL
, td
);
1900 error
= so_pru_send(so
, 0, m
, NULL
, NULL
, td
);
1906 if (mheader
!= NULL
) {
1907 *sbytes
+= mheader
->m_pkthdr
.len
;
1909 if (so
->so_snd
.ssb_flags
& SSB_PREALLOC
) {
1910 for (mp
= mheader
; mp
!= NULL
; mp
= mp
->m_next
)
1911 ssb_preallocstream(&so
->so_snd
, mp
);
1913 if (use_sendfile_async
)
1914 error
= so_pru_senda(so
, 0, mheader
, NULL
, NULL
, td
);
1916 error
= so_pru_send(so
, 0, mheader
, NULL
, NULL
, td
);
1921 vm_object_drop(obj
);
1922 ssb_unlock(&so
->so_snd
);
1926 if (mheader
!= NULL
)