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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
37 * $FreeBSD: src/sys/kern/uipc_syscalls.c,v 1.65.2.17 2003/04/04 17:11:16 tegge Exp $
38 * $DragonFly: src/sys/kern/uipc_syscalls.c,v 1.29 2004/03/29 15:46:18 dillon Exp $
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/sysproto.h>
47 #include <sys/malloc.h>
48 #include <sys/filedesc.h>
49 #include <sys/event.h>
51 #include <sys/fcntl.h>
53 #include <sys/filio.h>
54 #include <sys/kern_syscall.h>
56 #include <sys/protosw.h>
57 #include <sys/sfbuf.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
60 #include <sys/socketops.h>
62 #include <sys/vnode.h>
64 #include <sys/mount.h>
66 #include <sys/ktrace.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_page.h>
71 #include <vm/vm_pageout.h>
72 #include <vm/vm_kern.h>
73 #include <vm/vm_extern.h>
74 #include <sys/file2.h>
75 #include <sys/signalvar.h>
78 * System call interface to the socket abstraction.
81 extern struct fileops socketops;
84 * socket_args(int domain, int type, int protocol)
87 kern_socket(int domain, int type, int protocol, int *res)
89 struct thread *td = curthread;
90 struct proc *p = td->td_proc;
99 error = falloc(p, &fp, &fd);
103 error = socreate(domain, &so, type, protocol, td);
105 if (fdp->fd_ofiles[fd] == fp) {
106 fdp->fd_ofiles[fd] = NULL;
110 fp->f_data = (caddr_t)so;
111 fp->f_flag = FREAD|FWRITE;
112 fp->f_ops = &socketops;
113 fp->f_type = DTYPE_SOCKET;
121 socket(struct socket_args *uap)
125 error = kern_socket(uap->domain, uap->type, uap->protocol,
126 &uap->sysmsg_result);
132 kern_bind(int s, struct sockaddr *sa)
134 struct thread *td = curthread;
135 struct proc *p = td->td_proc;
140 error = holdsock(p->p_fd, s, &fp);
143 error = sobind((struct socket *)fp->f_data, sa, td);
149 * bind_args(int s, caddr_t name, int namelen)
152 bind(struct bind_args *uap)
157 error = getsockaddr(&sa, uap->name, uap->namelen);
160 error = kern_bind(uap->s, sa);
167 kern_listen(int s, int backlog)
169 struct thread *td = curthread;
170 struct proc *p = td->td_proc;
175 error = holdsock(p->p_fd, s, &fp);
178 error = solisten((struct socket *)fp->f_data, backlog, td);
184 * listen_args(int s, int backlog)
187 listen(struct listen_args *uap)
191 error = kern_listen(uap->s, uap->backlog);
196 * The second argument to kern_accept() is a handle to a struct sockaddr.
197 * This allows kern_accept() to return a pointer to an allocated struct
198 * sockaddr which must be freed later with FREE(). The caller must
199 * initialize *name to NULL.
202 kern_accept(int s, struct sockaddr **name, int *namelen, int *res)
204 struct thread *td = curthread;
205 struct proc *p = td->td_proc;
206 struct filedesc *fdp = p->p_fd;
207 struct file *lfp = NULL;
208 struct file *nfp = NULL;
211 struct socket *head, *so;
213 u_int fflag; /* type must match fp->f_flag */
216 if (name && namelen && *namelen < 0)
219 error = holdsock(fdp, s, &lfp);
223 head = (struct socket *)lfp->f_data;
224 if ((head->so_options & SO_ACCEPTCONN) == 0) {
229 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
230 if (head->so_state & SS_CANTRCVMORE) {
231 head->so_error = ECONNABORTED;
234 if ((head->so_state & SS_NBIO) != 0) {
235 head->so_error = EWOULDBLOCK;
238 error = tsleep((caddr_t)&head->so_timeo, PCATCH, "accept", 0);
244 if (head->so_error) {
245 error = head->so_error;
252 * At this point we know that there is at least one connection
253 * ready to be accepted. Remove it from the queue prior to
254 * allocating the file descriptor for it since falloc() may
255 * block allowing another process to accept the connection
258 so = TAILQ_FIRST(&head->so_comp);
259 TAILQ_REMOVE(&head->so_comp, so, so_list);
263 error = falloc(p, &nfp, &fd);
266 * Probably ran out of file descriptors. Put the
267 * unaccepted connection back onto the queue and
268 * do another wakeup so some other process might
269 * have a chance at it.
271 TAILQ_INSERT_HEAD(&head->so_comp, so, so_list);
273 wakeup_one(&head->so_timeo);
280 /* connection has been removed from the listen queue */
281 KNOTE(&head->so_rcv.sb_sel.si_note, 0);
283 so->so_state &= ~SS_COMP;
285 if (head->so_sigio != NULL)
286 fsetown(fgetown(head->so_sigio), &so->so_sigio);
288 nfp->f_data = (caddr_t)so;
290 nfp->f_ops = &socketops;
291 nfp->f_type = DTYPE_SOCKET;
292 /* Sync socket nonblocking/async state with file flags */
293 tmp = fflag & FNONBLOCK;
294 (void) fo_ioctl(nfp, FIONBIO, (caddr_t)&tmp, td);
295 tmp = fflag & FASYNC;
296 (void) fo_ioctl(nfp, FIOASYNC, (caddr_t)&tmp, td);
299 error = soaccept(so, &sa);
302 * Set the returned name and namelen as applicable. Set the returned
303 * namelen to 0 for older code which might ignore the return value
307 if (sa && name && namelen) {
308 if (*namelen > sa->sa_len)
309 *namelen = sa->sa_len;
318 * close the new descriptor, assuming someone hasn't ripped it
319 * out from under us. Note that *res is normally ignored if an
320 * error is returned but a syscall message will still have access
321 * to the result code.
325 if (fdp->fd_ofiles[fd] == nfp) {
326 fdp->fd_ofiles[fd] = NULL;
333 * Release explicitly held references before returning.
343 * accept_args(int s, caddr_t name, int *anamelen)
346 accept(struct accept_args *uap)
348 struct sockaddr *sa = NULL;
353 error = copyin(uap->anamelen, &sa_len, sizeof(sa_len));
357 error = kern_accept(uap->s, &sa, &sa_len, &uap->sysmsg_result);
360 error = copyout(sa, uap->name, sa_len);
362 error = copyout(&sa_len, uap->anamelen,
363 sizeof(*uap->anamelen));
368 error = kern_accept(uap->s, NULL, 0, &uap->sysmsg_result);
374 kern_connect(int s, struct sockaddr *sa)
376 struct thread *td = curthread;
377 struct proc *p = td->td_proc;
382 error = holdsock(p->p_fd, s, &fp);
385 so = (struct socket *)fp->f_data;
386 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
390 error = soconnect(so, sa, td);
393 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
398 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
399 error = tsleep((caddr_t)&so->so_timeo, PCATCH, "connec", 0);
404 error = so->so_error;
409 so->so_state &= ~SS_ISCONNECTING;
410 if (error == ERESTART)
418 * connect_args(int s, caddr_t name, int namelen)
421 connect(struct connect_args *uap)
426 error = getsockaddr(&sa, uap->name, uap->namelen);
429 error = kern_connect(uap->s, sa);
436 kern_socketpair(int domain, int type, int protocol, int *sv)
438 struct thread *td = curthread;
439 struct proc *p = td->td_proc;
440 struct filedesc *fdp;
441 struct file *fp1, *fp2;
442 struct socket *so1, *so2;
447 error = socreate(domain, &so1, type, protocol, td);
450 error = socreate(domain, &so2, type, protocol, td);
453 error = falloc(p, &fp1, &fd);
458 fp1->f_data = (caddr_t)so1;
459 error = falloc(p, &fp2, &fd);
463 fp2->f_data = (caddr_t)so2;
465 error = soconnect2(so1, so2);
468 if (type == SOCK_DGRAM) {
470 * Datagram socket connection is asymmetric.
472 error = soconnect2(so2, so1);
476 fp1->f_flag = fp2->f_flag = FREAD|FWRITE;
477 fp1->f_ops = fp2->f_ops = &socketops;
478 fp1->f_type = fp2->f_type = DTYPE_SOCKET;
483 if (fdp->fd_ofiles[sv[1]] == fp2) {
484 fdp->fd_ofiles[sv[1]] = NULL;
489 if (fdp->fd_ofiles[sv[0]] == fp1) {
490 fdp->fd_ofiles[sv[0]] = NULL;
502 * socketpair(int domain, int type, int protocol, int *rsv)
505 socketpair(struct socketpair_args *uap)
509 error = kern_socketpair(uap->domain, uap->type, uap->protocol, sockv);
512 error = copyout(sockv, uap->rsv, sizeof(sockv));
517 kern_sendmsg(int s, struct sockaddr *sa, struct uio *auio,
518 struct mbuf *control, int flags, int *res)
520 struct thread *td = curthread;
521 struct proc *p = td->td_proc;
526 struct iovec *ktriov = NULL;
530 error = holdsock(p->p_fd, s, &fp);
533 if (auio->uio_resid < 0) {
538 if (KTRPOINT(td, KTR_GENIO)) {
539 int iovlen = auio->uio_iovcnt * sizeof (struct iovec);
541 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
542 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
546 len = auio->uio_resid;
547 so = (struct socket *)fp->f_data;
548 error = so_pru_sosend(so, sa, auio, NULL, control, flags, td);
550 if (auio->uio_resid != len && (error == ERESTART ||
551 error == EINTR || error == EWOULDBLOCK))
557 if (ktriov != NULL) {
559 ktruio.uio_iov = ktriov;
560 ktruio.uio_resid = len - auio->uio_resid;
561 ktrgenio(p->p_tracep, s, UIO_WRITE, &ktruio, error);
563 FREE(ktriov, M_TEMP);
567 *res = len - auio->uio_resid;
574 * sendto_args(int s, caddr_t buf, size_t len, int flags, caddr_t to, int tolen)
577 sendto(struct sendto_args *uap)
579 struct thread *td = curthread;
582 struct sockaddr *sa = NULL;
586 error = getsockaddr(&sa, uap->to, uap->tolen);
590 aiov.iov_base = uap->buf;
591 aiov.iov_len = uap->len;
592 auio.uio_iov = &aiov;
595 auio.uio_resid = uap->len;
596 auio.uio_segflg = UIO_USERSPACE;
597 auio.uio_rw = UIO_WRITE;
600 error = kern_sendmsg(uap->s, sa, &auio, NULL, uap->flags,
601 &uap->sysmsg_result);
609 * sendmsg_args(int s, caddr_t msg, int flags)
612 sendmsg(struct sendmsg_args *uap)
614 struct thread *td = curthread;
617 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
618 struct sockaddr *sa = NULL;
619 struct mbuf *control = NULL;
622 error = copyin(uap->msg, (caddr_t)&msg, sizeof(msg));
627 * Conditionally copyin msg.msg_name.
630 error = getsockaddr(&sa, msg.msg_name, msg.msg_namelen);
638 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
643 auio.uio_iovcnt = msg.msg_iovlen;
645 auio.uio_segflg = UIO_USERSPACE;
646 auio.uio_rw = UIO_WRITE;
650 * Conditionally copyin msg.msg_control.
652 if (msg.msg_control) {
653 if (msg.msg_controllen < sizeof(struct cmsghdr) ||
654 msg.msg_controllen > MLEN) {
658 control = m_get(M_WAIT, MT_CONTROL);
659 if (control == NULL) {
663 control->m_len = msg.msg_controllen;
664 error = copyin(msg.msg_control, mtod(control, caddr_t),
672 error = kern_sendmsg(uap->s, sa, &auio, control, uap->flags,
673 &uap->sysmsg_result);
678 iovec_free(&iov, aiov);
683 * kern_recvmsg() takes a handle to sa and control. If the handle is non-
684 * null, it returns a dynamically allocated struct sockaddr and an mbuf.
685 * Don't forget to FREE() and m_free() these if they are returned.
688 kern_recvmsg(int s, struct sockaddr **sa, struct uio *auio,
689 struct mbuf **control, int *flags, int *res)
691 struct thread *td = curthread;
692 struct proc *p = td->td_proc;
697 struct iovec *ktriov = NULL;
701 error = holdsock(p->p_fd, s, &fp);
704 if (auio->uio_resid < 0) {
709 if (KTRPOINT(td, KTR_GENIO)) {
710 int iovlen = auio->uio_iovcnt * sizeof (struct iovec);
712 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
713 bcopy(auio->uio_iov, ktriov, iovlen);
717 len = auio->uio_resid;
718 so = (struct socket *)fp->f_data;
719 error = so_pru_soreceive(so, sa, auio, NULL, control, flags);
721 if (auio->uio_resid != len && (error == ERESTART ||
722 error == EINTR || error == EWOULDBLOCK))
726 if (ktriov != NULL) {
728 ktruio.uio_iov = ktriov;
729 ktruio.uio_resid = len - auio->uio_resid;
730 ktrgenio(p->p_tracep, s, UIO_READ, &ktruio, error);
732 FREE(ktriov, M_TEMP);
736 *res = len - auio->uio_resid;
743 * recvfrom_args(int s, caddr_t buf, size_t len, int flags,
744 * caddr_t from, int *fromlenaddr)
747 recvfrom(struct recvfrom_args *uap)
749 struct thread *td = curthread;
752 struct sockaddr *sa = NULL;
755 if (uap->from && uap->fromlenaddr) {
756 error = copyin(uap->fromlenaddr, &fromlen, sizeof(fromlen));
764 aiov.iov_base = uap->buf;
765 aiov.iov_len = uap->len;
766 auio.uio_iov = &aiov;
769 auio.uio_resid = uap->len;
770 auio.uio_segflg = UIO_USERSPACE;
771 auio.uio_rw = UIO_READ;
774 error = kern_recvmsg(uap->s, uap->from ? &sa : NULL, &auio, NULL,
775 &uap->flags, &uap->sysmsg_result);
777 if (error == 0 && uap->from) {
778 /* note: sa may still be NULL */
780 fromlen = MIN(fromlen, sa->sa_len);
781 error = copyout(sa, uap->from, fromlen);
786 error = copyout(&fromlen, uap->fromlenaddr,
797 * recvmsg_args(int s, struct msghdr *msg, int flags)
800 recvmsg(struct recvmsg_args *uap)
802 struct thread *td = curthread;
805 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
806 struct mbuf *m, *control = NULL;
807 struct sockaddr *sa = NULL;
809 socklen_t *ufromlenp, *ucontrollenp;
810 int error, fromlen, controllen, len, flags, *uflagsp;
813 * This copyin handles everything except the iovec.
815 error = copyin(uap->msg, &msg, sizeof(msg));
819 if (msg.msg_name && msg.msg_namelen < 0)
821 if (msg.msg_control && msg.msg_controllen < 0)
824 ufromlenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
826 ucontrollenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
828 uflagsp = (int *)((caddr_t)uap->msg + offsetof(struct msghdr,
834 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
839 auio.uio_iovcnt = msg.msg_iovlen;
841 auio.uio_segflg = UIO_USERSPACE;
842 auio.uio_rw = UIO_READ;
845 flags = msg.msg_flags;
847 error = kern_recvmsg(uap->s, msg.msg_name ? &sa : NULL, &auio,
848 msg.msg_control ? &control : NULL, &flags, &uap->sysmsg_result);
851 * Conditionally copyout the name and populate the namelen field.
853 if (error == 0 && msg.msg_name) {
854 fromlen = MIN(msg.msg_namelen, sa->sa_len);
855 error = copyout(sa, msg.msg_name, fromlen);
857 error = copyout(&fromlen, ufromlenp,
862 * Copyout msg.msg_control and msg.msg_controllen.
864 if (error == 0 && msg.msg_control) {
865 len = msg.msg_controllen;
867 ctlbuf = (caddr_t)msg.msg_control;
869 while(m && len > 0) {
872 if (len >= m->m_len) {
875 msg.msg_flags |= MSG_CTRUNC;
879 error = copyout(mtod(m, caddr_t), ctlbuf, tocopy);
887 controllen = ctlbuf - (caddr_t)msg.msg_control;
888 error = copyout(&controllen, ucontrollenp,
889 sizeof(*ucontrollenp));
893 error = copyout(&flags, uflagsp, sizeof(*uflagsp));
898 iovec_free(&iov, aiov);
905 * shutdown_args(int s, int how)
908 kern_shutdown(int s, int how)
910 struct thread *td = curthread;
911 struct proc *p = td->td_proc;
916 error = holdsock(p->p_fd, s, &fp);
919 error = soshutdown((struct socket *)fp->f_data, how);
925 shutdown(struct shutdown_args *uap)
929 error = kern_shutdown(uap->s, uap->how);
935 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
936 * in kernel pointer instead of a userland pointer. This allows us
937 * to manipulate socket options in the emulation code.
940 kern_setsockopt(int s, struct sockopt *sopt)
942 struct thread *td = curthread;
943 struct proc *p = td->td_proc;
947 if (sopt->sopt_val == 0 && sopt->sopt_valsize != 0)
949 if (sopt->sopt_valsize < 0)
952 error = holdsock(p->p_fd, s, &fp);
956 error = sosetopt((struct socket *)fp->f_data, sopt);
962 * setsockopt_args(int s, int level, int name, caddr_t val, int valsize)
965 setsockopt(struct setsockopt_args *uap)
967 struct thread *td = curthread;
971 sopt.sopt_dir = SOPT_SET;
972 sopt.sopt_level = uap->level;
973 sopt.sopt_name = uap->name;
974 sopt.sopt_val = uap->val;
975 sopt.sopt_valsize = uap->valsize;
978 error = kern_setsockopt(uap->s, &sopt);
983 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
984 * in kernel pointer instead of a userland pointer. This allows us
985 * to manipulate socket options in the emulation code.
988 kern_getsockopt(int s, struct sockopt *sopt)
990 struct thread *td = curthread;
991 struct proc *p = td->td_proc;
995 if (sopt->sopt_val == 0 && sopt->sopt_valsize != 0)
997 if (sopt->sopt_valsize < 0)
1000 error = holdsock(p->p_fd, s, &fp);
1004 error = sogetopt((struct socket *)fp->f_data, sopt);
1010 * getsockopt_Args(int s, int level, int name, caddr_t val, int *avalsize)
1013 getsockopt(struct getsockopt_args *uap)
1015 struct thread *td = curthread;
1016 struct sockopt sopt;
1020 error = copyin(uap->avalsize, &valsize, sizeof(valsize));
1029 sopt.sopt_dir = SOPT_GET;
1030 sopt.sopt_level = uap->level;
1031 sopt.sopt_name = uap->name;
1032 sopt.sopt_val = uap->val;
1033 sopt.sopt_valsize = valsize;
1036 error = kern_getsockopt(uap->s, &sopt);
1038 valsize = sopt.sopt_valsize;
1039 error = copyout(&valsize, uap->avalsize, sizeof(valsize));
1045 * The second argument to kern_getsockname() is a handle to a struct sockaddr.
1046 * This allows kern_getsockname() to return a pointer to an allocated struct
1047 * sockaddr which must be freed later with FREE(). The caller must
1048 * initialize *name to NULL.
1051 kern_getsockname(int s, struct sockaddr **name, int *namelen)
1053 struct thread *td = curthread;
1054 struct proc *p = td->td_proc;
1057 struct sockaddr *sa = NULL;
1060 error = holdsock(p->p_fd, s, &fp);
1067 so = (struct socket *)fp->f_data;
1068 error = so_pru_sockaddr(so, &sa);
1073 *namelen = MIN(*namelen, sa->sa_len);
1083 * getsockname_args(int fdes, caddr_t asa, int *alen)
1088 getsockname(struct getsockname_args *uap)
1090 struct sockaddr *sa = NULL;
1093 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1097 error = kern_getsockname(uap->fdes, &sa, &sa_len);
1100 error = copyout(sa, uap->asa, sa_len);
1102 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1109 * The second argument to kern_getpeername() is a handle to a struct sockaddr.
1110 * This allows kern_getpeername() to return a pointer to an allocated struct
1111 * sockaddr which must be freed later with FREE(). The caller must
1112 * initialize *name to NULL.
1115 kern_getpeername(int s, struct sockaddr **name, int *namelen)
1117 struct thread *td = curthread;
1118 struct proc *p = td->td_proc;
1121 struct sockaddr *sa = NULL;
1124 error = holdsock(p->p_fd, s, &fp);
1131 so = (struct socket *)fp->f_data;
1132 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1136 error = so_pru_peeraddr(so, &sa);
1141 *namelen = MIN(*namelen, sa->sa_len);
1151 * getpeername_args(int fdes, caddr_t asa, int *alen)
1153 * Get name of peer for connected socket.
1156 getpeername(struct getpeername_args *uap)
1158 struct sockaddr *sa = NULL;
1161 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1165 error = kern_getpeername(uap->fdes, &sa, &sa_len);
1168 error = copyout(sa, uap->asa, sa_len);
1170 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1177 getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
1179 struct sockaddr *sa;
1183 if (len > SOCK_MAXADDRLEN)
1184 return ENAMETOOLONG;
1185 if (len < offsetof(struct sockaddr, sa_data[0]))
1187 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1188 error = copyin(uaddr, sa, len);
1192 #if BYTE_ORDER != BIG_ENDIAN
1194 * The bind(), connect(), and sendto() syscalls were not
1195 * versioned for COMPAT_43. Thus, this check must stay.
1197 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1198 sa->sa_family = sa->sa_len;
1207 * holdsock() - load the struct file pointer associated
1208 * with a socket into *fpp. If an error occurs, non-zero
1209 * will be returned and *fpp will be set to NULL.
1212 holdsock(fdp, fdes, fpp)
1213 struct filedesc *fdp;
1217 struct file *fp = NULL;
1220 if ((unsigned)fdes >= fdp->fd_nfiles ||
1221 (fp = fdp->fd_ofiles[fdes]) == NULL) {
1223 } else if (fp->f_type != DTYPE_SOCKET) {
1234 * Detach a mapped page and release resources back to the system.
1235 * We must release our wiring and if the object is ripped out
1236 * from under the vm_page we become responsible for freeing the
1239 * XXX HACK XXX TEMPORARY UNTIL WE IMPLEMENT EXT MBUF REFERENCE COUNTING
1242 sf_buf_mref(caddr_t addr, u_int size)
1246 sf = sf_buf_tosf(addr);
1251 sf_buf_mext(caddr_t addr, u_int size)
1258 sf = sf_buf_tosf(addr);
1259 KKASSERT(sf->aux2 > 0);
1260 if (--sf->aux2 == 0) {
1261 m = sf_buf_page(sf);
1268 vm_page_unwire(m, 0);
1270 if (m->wire_count == 0 && m->object == NULL)
1278 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1279 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1281 * Send a file specified by 'fd' and starting at 'offset' to a socket
1282 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1283 * nbytes == 0. Optionally add a header and/or trailer to the socket
1284 * output. If specified, write the total number of bytes sent into *sbytes.
1286 * In FreeBSD kern/uipc_syscalls.c,v 1.103, a bug was fixed that caused
1287 * the headers to count against the remaining bytes to be sent from
1288 * the file descriptor. We may wish to implement a compatibility syscall
1292 sendfile(struct sendfile_args *uap)
1294 struct thread *td = curthread;
1295 struct proc *p = td->td_proc;
1297 struct filedesc *fdp;
1298 struct vnode *vp = NULL;
1299 struct sf_hdtr hdtr;
1300 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
1302 struct mbuf *mheader = NULL;
1303 off_t hdtr_size = 0, sbytes;
1304 int error, hbytes = 0, tbytes;
1310 * Do argument checking. Must be a regular file in, stream
1311 * type and connected socket out, positive offset.
1313 fp = holdfp(fdp, uap->fd, FREAD);
1317 if (fp->f_type != DTYPE_VNODE) {
1321 vp = (struct vnode *)fp->f_data;
1326 * If specified, get the pointer to the sf_hdtr struct for
1327 * any headers/trailers.
1330 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1337 error = iovec_copyin(hdtr.headers, &iov, aiov,
1338 hdtr.hdr_cnt, &hbytes);
1342 auio.uio_iovcnt = hdtr.hdr_cnt;
1343 auio.uio_offset = 0;
1344 auio.uio_segflg = UIO_USERSPACE;
1345 auio.uio_rw = UIO_WRITE;
1347 auio.uio_resid = hbytes;
1349 mheader = m_uiomove(&auio, M_WAIT, 0);
1351 iovec_free(&iov, aiov);
1352 if (mheader == NULL)
1357 error = kern_sendfile(vp, uap->s, uap->offset, uap->nbytes, mheader,
1358 &sbytes, uap->flags);
1361 hdtr_size += hbytes; /* account for header bytes successfully sent */
1364 * Send trailers. Wimp out and use writev(2).
1366 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
1367 error = iovec_copyin(hdtr.trailers, &iov, aiov,
1368 hdtr.trl_cnt, &auio.uio_resid);
1372 auio.uio_iovcnt = hdtr.trl_cnt;
1373 auio.uio_offset = 0;
1374 auio.uio_segflg = UIO_USERSPACE;
1375 auio.uio_rw = UIO_WRITE;
1378 error = kern_sendmsg(uap->s, NULL, &auio, NULL, 0, &tbytes);
1380 iovec_free(&iov, aiov);
1383 hdtr_size += tbytes; /* trailer bytes successfully sent */
1387 if (uap->sbytes != NULL) {
1388 sbytes += hdtr_size;
1389 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1397 kern_sendfile(struct vnode *vp, int s, off_t offset, size_t nbytes,
1398 struct mbuf *mheader, off_t *sbytes, int flags)
1400 struct thread *td = curthread;
1401 struct proc *p = td->td_proc;
1402 struct vm_object *obj;
1411 if (vp->v_type != VREG || VOP_GETVOBJECT(vp, &obj) != 0) {
1415 error = holdsock(p->p_fd, s, &fp);
1418 so = (struct socket *)fp->f_data;
1419 if (so->so_type != SOCK_STREAM) {
1423 if ((so->so_state & SS_ISCONNECTED) == 0) {
1434 * Protect against multiple writers to the socket.
1436 (void) sblock(&so->so_snd, M_WAITOK);
1439 * Loop through the pages in the file, starting with the requested
1440 * offset. Get a file page (do I/O if necessary), map the file page
1441 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1444 for (off = offset; ; off += xfsize, *sbytes += xfsize) {
1448 pindex = OFF_TO_IDX(off);
1451 * Calculate the amount to transfer. Not to exceed a page,
1452 * the EOF, or the passed in nbytes.
1454 xfsize = obj->un_pager.vnp.vnp_size - off;
1455 if (xfsize > PAGE_SIZE)
1457 pgoff = (vm_offset_t)(off & PAGE_MASK);
1458 if (PAGE_SIZE - pgoff < xfsize)
1459 xfsize = PAGE_SIZE - pgoff;
1460 if (nbytes && xfsize > (nbytes - *sbytes))
1461 xfsize = nbytes - *sbytes;
1465 * Optimize the non-blocking case by looking at the socket space
1466 * before going to the extra work of constituting the sf_buf.
1468 if ((so->so_state & SS_NBIO) && sbspace(&so->so_snd) <= 0) {
1469 if (so->so_state & SS_CANTSENDMORE)
1473 sbunlock(&so->so_snd);
1477 * Attempt to look up the page.
1479 * Allocate if not found
1481 * Wait and loop if busy.
1483 pg = vm_page_lookup(obj, pindex);
1486 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NORMAL);
1492 } else if (vm_page_sleep_busy(pg, TRUE, "sfpbsy")) {
1497 * Wire the page so it does not get ripped out from under
1504 * If page is not valid for what we need, initiate I/O
1507 if (!pg->valid || !vm_page_is_valid(pg, pgoff, xfsize)) {
1513 * Ensure that our page is still around when the I/O
1516 vm_page_io_start(pg);
1519 * Get the page from backing store.
1521 bsize = vp->v_mount->mnt_stat.f_iosize;
1522 auio.uio_iov = &aiov;
1523 auio.uio_iovcnt = 1;
1525 aiov.iov_len = MAXBSIZE;
1526 auio.uio_resid = MAXBSIZE;
1527 auio.uio_offset = trunc_page(off);
1528 auio.uio_segflg = UIO_NOCOPY;
1529 auio.uio_rw = UIO_READ;
1531 vn_lock(vp, NULL, LK_SHARED | LK_NOPAUSE | LK_RETRY, td);
1532 error = VOP_READ(vp, &auio,
1533 IO_VMIO | ((MAXBSIZE / bsize) << 16),
1535 VOP_UNLOCK(vp, NULL, 0, td);
1536 vm_page_flag_clear(pg, PG_ZERO);
1537 vm_page_io_finish(pg);
1539 vm_page_unwire(pg, 0);
1541 * See if anyone else might know about this page.
1542 * If not and it is not valid, then free it.
1544 if (pg->wire_count == 0 && pg->valid == 0 &&
1545 pg->busy == 0 && !(pg->flags & PG_BUSY) &&
1546 pg->hold_count == 0) {
1550 sbunlock(&so->so_snd);
1557 * Get a sendfile buf. We usually wait as long as necessary,
1558 * but this wait can be interrupted.
1560 if ((sf = sf_buf_alloc(pg)) == NULL) {
1562 vm_page_unwire(pg, 0);
1563 if (pg->wire_count == 0 && pg->object == NULL)
1566 sbunlock(&so->so_snd);
1572 * Get an mbuf header and set it up as having external storage.
1574 MGETHDR(m, M_WAIT, MT_DATA);
1578 sbunlock(&so->so_snd);
1581 ++sf->aux1; /* wiring count */
1582 ++sf->aux2; /* initial reference */
1583 m->m_ext.ext_free = sf_buf_mext;
1584 m->m_ext.ext_ref = sf_buf_mref;
1585 m->m_ext.ext_buf = (void *)sf->kva;
1586 m->m_ext.ext_size = PAGE_SIZE;
1587 m->m_data = (char *) sf->kva + pgoff;
1588 m->m_flags |= M_EXT;
1589 m->m_pkthdr.len = m->m_len = xfsize;
1592 mheader->m_pkthdr.len += m->m_pkthdr.len;
1599 * Add the buffer to the socket buffer chain.
1604 * Make sure that the socket is still able to take more data.
1605 * CANTSENDMORE being true usually means that the connection
1606 * was closed. so_error is true when an error was sensed after
1608 * The state is checked after the page mapping and buffer
1609 * allocation above since those operations may block and make
1610 * any socket checks stale. From this point forward, nothing
1611 * blocks before the pru_send (or more accurately, any blocking
1612 * results in a loop back to here to re-check).
1614 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) {
1615 if (so->so_state & SS_CANTSENDMORE) {
1618 error = so->so_error;
1622 sbunlock(&so->so_snd);
1627 * Wait for socket space to become available. We do this just
1628 * after checking the connection state above in order to avoid
1629 * a race condition with sbwait().
1631 if (sbspace(&so->so_snd) < so->so_snd.sb_lowat) {
1632 if (so->so_state & SS_NBIO) {
1634 sbunlock(&so->so_snd);
1639 error = sbwait(&so->so_snd);
1641 * An error from sbwait usually indicates that we've
1642 * been interrupted by a signal. If we've sent anything
1643 * then return bytes sent, otherwise return the error.
1647 sbunlock(&so->so_snd);
1653 error = so_pru_send(so, 0, m, NULL, NULL, td);
1656 sbunlock(&so->so_snd);
1660 sbunlock(&so->so_snd);
1665 if (mheader != NULL)