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 * 4. 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
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_capsicum.h"
40 #include "opt_inet6.h"
41 #include "opt_compat.h"
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/capsicum.h>
47 #include <sys/condvar.h>
48 #include <sys/kernel.h>
50 #include <sys/mutex.h>
51 #include <sys/sysproto.h>
52 #include <sys/malloc.h>
53 #include <sys/filedesc.h>
54 #include <sys/event.h>
56 #include <sys/fcntl.h>
58 #include <sys/filio.h>
61 #include <sys/mount.h>
63 #include <sys/protosw.h>
64 #include <sys/rwlock.h>
65 #include <sys/sf_buf.h>
66 #include <sys/sysent.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/signalvar.h>
70 #include <sys/syscallsubr.h>
71 #include <sys/sysctl.h>
74 #include <sys/unpcb.h>
75 #include <sys/vnode.h>
77 #include <sys/ktrace.h>
79 #ifdef COMPAT_FREEBSD32
80 #include <compat/freebsd32/freebsd32_util.h>
85 #include <security/audit/audit.h>
86 #include <security/mac/mac_framework.h>
89 #include <vm/vm_param.h>
90 #include <vm/vm_object.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_pager.h>
93 #include <vm/vm_kern.h>
94 #include <vm/vm_extern.h>
97 * Flags for accept1() and kern_accept4(), in addition to SOCK_CLOEXEC
100 #define ACCEPT4_INHERIT 0x1
101 #define ACCEPT4_COMPAT 0x2
103 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
104 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
106 static int accept1(struct thread *td, int s, struct sockaddr *uname,
107 socklen_t *anamelen, int flags);
108 static int do_sendfile(struct thread *td, struct sendfile_args *uap,
110 static int getsockname1(struct thread *td, struct getsockname_args *uap,
112 static int getpeername1(struct thread *td, struct getpeername_args *uap,
115 counter_u64_t sfstat[sizeof(struct sfstat) / sizeof(uint64_t)];
118 * sendfile(2)-related variables and associated sysctls
120 static SYSCTL_NODE(_kern_ipc, OID_AUTO, sendfile, CTLFLAG_RW, 0,
121 "sendfile(2) tunables");
122 static int sfreadahead = 1;
123 SYSCTL_INT(_kern_ipc_sendfile, OID_AUTO, readahead, CTLFLAG_RW,
124 &sfreadahead, 0, "Number of sendfile(2) read-ahead MAXBSIZE blocks");
128 sfstat_init(const void *unused)
131 COUNTER_ARRAY_ALLOC(sfstat, sizeof(struct sfstat) / sizeof(uint64_t),
134 SYSINIT(sfstat, SI_SUB_MBUF, SI_ORDER_FIRST, sfstat_init, NULL);
137 sfstat_sysctl(SYSCTL_HANDLER_ARGS)
141 COUNTER_ARRAY_COPY(sfstat, &s, sizeof(s) / sizeof(uint64_t));
143 COUNTER_ARRAY_ZERO(sfstat, sizeof(s) / sizeof(uint64_t));
144 return (SYSCTL_OUT(req, &s, sizeof(s)));
146 SYSCTL_PROC(_kern_ipc, OID_AUTO, sfstat, CTLTYPE_OPAQUE | CTLFLAG_RW,
147 NULL, 0, sfstat_sysctl, "I", "sendfile statistics");
150 * Convert a user file descriptor to a kernel file entry and check if required
151 * capability rights are present.
152 * A reference on the file entry is held upon returning.
155 getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
156 struct file **fpp, u_int *fflagp)
161 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, 0, &fp, NULL);
164 if (fp->f_type != DTYPE_SOCKET) {
169 *fflagp = fp->f_flag;
175 * System call interface to the socket abstraction.
177 #if defined(COMPAT_43)
178 #define COMPAT_OLDSOCK
184 struct socket_args /* {
192 int fd, error, type, oflag, fflag;
194 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
199 if ((type & SOCK_CLOEXEC) != 0) {
200 type &= ~SOCK_CLOEXEC;
203 if ((type & SOCK_NONBLOCK) != 0) {
204 type &= ~SOCK_NONBLOCK;
209 error = mac_socket_check_create(td->td_ucred, uap->domain, type,
214 error = falloc(td, &fp, &fd, oflag);
217 /* An extra reference on `fp' has been held for us by falloc(). */
218 error = socreate(uap->domain, &so, type, uap->protocol,
223 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
224 if ((fflag & FNONBLOCK) != 0)
225 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
226 td->td_retval[0] = fd;
236 struct bind_args /* {
245 error = getsockaddr(&sa, uap->name, uap->namelen);
247 error = kern_bind(td, uap->s, sa);
254 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
262 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
263 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_BIND),
269 if (KTRPOINT(td, KTR_STRUCT))
273 error = mac_socket_check_bind(td->td_ucred, so, sa);
276 if (dirfd == AT_FDCWD)
277 error = sobind(so, sa, td);
279 error = sobindat(dirfd, so, sa, td);
288 kern_bind(struct thread *td, int fd, struct sockaddr *sa)
291 return (kern_bindat(td, AT_FDCWD, fd, sa));
298 struct bindat_args /* {
308 error = getsockaddr(&sa, uap->name, uap->namelen);
310 error = kern_bindat(td, uap->fd, uap->s, sa);
320 struct listen_args /* {
330 AUDIT_ARG_FD(uap->s);
331 error = getsock_cap(td, uap->s, cap_rights_init(&rights, CAP_LISTEN),
336 error = mac_socket_check_listen(td->td_ucred, so);
339 error = solisten(so, uap->backlog, td);
349 accept1(td, s, uname, anamelen, flags)
352 struct sockaddr *uname;
356 struct sockaddr *name;
362 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
364 error = copyin(anamelen, &namelen, sizeof (namelen));
368 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
371 * return a namelen of zero for older code which might
372 * ignore the return value from accept.
375 (void) copyout(&namelen, anamelen, sizeof(*anamelen));
379 if (error == 0 && uname != NULL) {
380 #ifdef COMPAT_OLDSOCK
381 if (flags & ACCEPT4_COMPAT)
382 ((struct osockaddr *)name)->sa_family =
385 error = copyout(name, uname, namelen);
388 error = copyout(&namelen, anamelen,
391 fdclose(td, fp, td->td_retval[0]);
393 free(name, M_SONAME);
398 kern_accept(struct thread *td, int s, struct sockaddr **name,
399 socklen_t *namelen, struct file **fp)
401 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
405 kern_accept4(struct thread *td, int s, struct sockaddr **name,
406 socklen_t *namelen, int flags, struct file **fp)
408 struct file *headfp, *nfp = NULL;
409 struct sockaddr *sa = NULL;
410 struct socket *head, *so;
420 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_ACCEPT),
424 head = headfp->f_data;
425 if ((head->so_options & SO_ACCEPTCONN) == 0) {
430 error = mac_socket_check_accept(td->td_ucred, head);
434 error = falloc(td, &nfp, &fd, (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0);
438 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
443 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
444 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
445 head->so_error = ECONNABORTED;
448 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
455 if (head->so_error) {
456 error = head->so_error;
461 so = TAILQ_FIRST(&head->so_comp);
462 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
463 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
466 * Before changing the flags on the socket, we have to bump the
467 * reference count. Otherwise, if the protocol calls sofree(),
468 * the socket will be released due to a zero refcount.
470 SOCK_LOCK(so); /* soref() and so_state update */
471 soref(so); /* file descriptor reference */
473 TAILQ_REMOVE(&head->so_comp, so, so_list);
475 if (flags & ACCEPT4_INHERIT)
476 so->so_state |= (head->so_state & SS_NBIO);
478 so->so_state |= (flags & SOCK_NONBLOCK) ? SS_NBIO : 0;
479 so->so_qstate &= ~SQ_COMP;
485 /* An extra reference on `nfp' has been held for us by falloc(). */
486 td->td_retval[0] = fd;
488 /* connection has been removed from the listen queue */
489 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
491 if (flags & ACCEPT4_INHERIT) {
492 pgid = fgetown(&head->so_sigio);
494 fsetown(pgid, &so->so_sigio);
496 fflag &= ~(FNONBLOCK | FASYNC);
497 if (flags & SOCK_NONBLOCK)
501 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
502 /* Sync socket nonblocking/async state with file flags */
503 tmp = fflag & FNONBLOCK;
504 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
505 tmp = fflag & FASYNC;
506 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
508 error = soaccept(so, &sa);
511 * return a namelen of zero for older code which might
512 * ignore the return value from accept.
523 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
525 /* check sa_len before it is destroyed */
526 if (*namelen > sa->sa_len)
527 *namelen = sa->sa_len;
529 if (KTRPOINT(td, KTR_STRUCT))
539 * close the new descriptor, assuming someone hasn't ripped it
543 fdclose(td, nfp, fd);
546 * Release explicitly held references before returning. We return
547 * a reference on nfp to the caller on success if they request it.
566 struct accept_args *uap;
569 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
575 struct accept4_args *uap;
578 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
581 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
584 #ifdef COMPAT_OLDSOCK
588 struct accept_args *uap;
591 return (accept1(td, uap->s, uap->name, uap->anamelen,
592 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
594 #endif /* COMPAT_OLDSOCK */
600 struct connect_args /* {
609 error = getsockaddr(&sa, uap->name, uap->namelen);
611 error = kern_connect(td, uap->s, sa);
618 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
623 int error, interrupted = 0;
626 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
627 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_CONNECT),
632 if (so->so_state & SS_ISCONNECTING) {
637 if (KTRPOINT(td, KTR_STRUCT))
641 error = mac_socket_check_connect(td->td_ucred, so, sa);
645 if (dirfd == AT_FDCWD)
646 error = soconnect(so, sa, td);
648 error = soconnectat(dirfd, so, sa, td);
651 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
656 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
657 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
660 if (error == EINTR || error == ERESTART)
666 error = so->so_error;
672 so->so_state &= ~SS_ISCONNECTING;
673 if (error == ERESTART)
681 kern_connect(struct thread *td, int fd, struct sockaddr *sa)
684 return (kern_connectat(td, AT_FDCWD, fd, sa));
689 sys_connectat(td, uap)
691 struct connectat_args /* {
701 error = getsockaddr(&sa, uap->name, uap->namelen);
703 error = kern_connectat(td, uap->fd, uap->s, sa);
710 kern_socketpair(struct thread *td, int domain, int type, int protocol,
713 struct file *fp1, *fp2;
714 struct socket *so1, *so2;
715 int fd, error, oflag, fflag;
717 AUDIT_ARG_SOCKET(domain, type, protocol);
721 if ((type & SOCK_CLOEXEC) != 0) {
722 type &= ~SOCK_CLOEXEC;
725 if ((type & SOCK_NONBLOCK) != 0) {
726 type &= ~SOCK_NONBLOCK;
730 /* We might want to have a separate check for socket pairs. */
731 error = mac_socket_check_create(td->td_ucred, domain, type,
736 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
739 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
742 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
743 error = falloc(td, &fp1, &fd, oflag);
747 fp1->f_data = so1; /* so1 already has ref count */
748 error = falloc(td, &fp2, &fd, oflag);
751 fp2->f_data = so2; /* so2 already has ref count */
753 error = soconnect2(so1, so2);
756 if (type == SOCK_DGRAM) {
758 * Datagram socket connection is asymmetric.
760 error = soconnect2(so2, so1);
763 } else if (so1->so_proto->pr_flags & PR_CONNREQUIRED) {
764 struct unpcb *unp, *unp2;
765 unp = sotounpcb(so1);
766 unp2 = sotounpcb(so2);
768 * No need to lock the unps, because the sockets are brand-new.
769 * No other threads can be using them yet
771 unp_copy_peercred(td, unp, unp2, unp);
773 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
775 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
777 if ((fflag & FNONBLOCK) != 0) {
778 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
779 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
785 fdclose(td, fp2, rsv[1]);
788 fdclose(td, fp1, rsv[0]);
800 sys_socketpair(struct thread *td, struct socketpair_args *uap)
804 error = kern_socketpair(td, uap->domain, uap->type,
808 error = copyout(sv, uap->rsv, 2 * sizeof(int));
810 (void)kern_close(td, sv[0]);
811 (void)kern_close(td, sv[1]);
817 sendit(td, s, mp, flags)
823 struct mbuf *control;
827 #ifdef CAPABILITY_MODE
828 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
832 if (mp->msg_name != NULL) {
833 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
843 if (mp->msg_control) {
844 if (mp->msg_controllen < sizeof(struct cmsghdr)
845 #ifdef COMPAT_OLDSOCK
846 && mp->msg_flags != MSG_COMPAT
852 error = sockargs(&control, mp->msg_control,
853 mp->msg_controllen, MT_CONTROL);
856 #ifdef COMPAT_OLDSOCK
857 if (mp->msg_flags == MSG_COMPAT) {
860 M_PREPEND(control, sizeof(*cm), M_WAITOK);
861 cm = mtod(control, struct cmsghdr *);
862 cm->cmsg_len = control->m_len;
863 cm->cmsg_level = SOL_SOCKET;
864 cm->cmsg_type = SCM_RIGHTS;
871 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
879 kern_sendit(td, s, mp, flags, control, segflg)
884 struct mbuf *control;
893 struct uio *ktruio = NULL;
899 cap_rights_init(&rights, CAP_SEND);
900 if (mp->msg_name != NULL) {
901 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
902 cap_rights_set(&rights, CAP_CONNECT);
904 error = getsock_cap(td, s, &rights, &fp, NULL);
907 so = (struct socket *)fp->f_data;
910 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
911 ktrsockaddr(mp->msg_name);
914 if (mp->msg_name != NULL) {
915 error = mac_socket_check_connect(td->td_ucred, so,
920 error = mac_socket_check_send(td->td_ucred, so);
925 auio.uio_iov = mp->msg_iov;
926 auio.uio_iovcnt = mp->msg_iovlen;
927 auio.uio_segflg = segflg;
928 auio.uio_rw = UIO_WRITE;
930 auio.uio_offset = 0; /* XXX */
933 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
934 if ((auio.uio_resid += iov->iov_len) < 0) {
940 if (KTRPOINT(td, KTR_GENIO))
941 ktruio = cloneuio(&auio);
943 len = auio.uio_resid;
944 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
946 if (auio.uio_resid != len && (error == ERESTART ||
947 error == EINTR || error == EWOULDBLOCK))
949 /* Generation of SIGPIPE can be controlled per socket */
950 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
951 !(flags & MSG_NOSIGNAL)) {
952 PROC_LOCK(td->td_proc);
953 tdsignal(td, SIGPIPE);
954 PROC_UNLOCK(td->td_proc);
958 td->td_retval[0] = len - auio.uio_resid;
960 if (ktruio != NULL) {
961 ktruio->uio_resid = td->td_retval[0];
962 ktrgenio(s, UIO_WRITE, ktruio, error);
973 struct sendto_args /* {
985 msg.msg_name = uap->to;
986 msg.msg_namelen = uap->tolen;
990 #ifdef COMPAT_OLDSOCK
993 aiov.iov_base = uap->buf;
994 aiov.iov_len = uap->len;
995 return (sendit(td, uap->s, &msg, uap->flags));
998 #ifdef COMPAT_OLDSOCK
1002 struct osend_args /* {
1013 msg.msg_namelen = 0;
1014 msg.msg_iov = &aiov;
1016 aiov.iov_base = uap->buf;
1017 aiov.iov_len = uap->len;
1018 msg.msg_control = 0;
1020 return (sendit(td, uap->s, &msg, uap->flags));
1026 struct osendmsg_args /* {
1036 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1039 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1043 msg.msg_flags = MSG_COMPAT;
1044 error = sendit(td, uap->s, &msg, uap->flags);
1051 sys_sendmsg(td, uap)
1053 struct sendmsg_args /* {
1063 error = copyin(uap->msg, &msg, sizeof (msg));
1066 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1070 #ifdef COMPAT_OLDSOCK
1073 error = sendit(td, uap->s, &msg, uap->flags);
1079 kern_recvit(td, s, mp, fromseg, controlp)
1083 enum uio_seg fromseg;
1084 struct mbuf **controlp;
1088 struct mbuf *m, *control = NULL;
1092 struct sockaddr *fromsa = NULL;
1093 cap_rights_t rights;
1095 struct uio *ktruio = NULL;
1100 if (controlp != NULL)
1104 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_RECV),
1111 error = mac_socket_check_receive(td->td_ucred, so);
1118 auio.uio_iov = mp->msg_iov;
1119 auio.uio_iovcnt = mp->msg_iovlen;
1120 auio.uio_segflg = UIO_USERSPACE;
1121 auio.uio_rw = UIO_READ;
1123 auio.uio_offset = 0; /* XXX */
1126 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
1127 if ((auio.uio_resid += iov->iov_len) < 0) {
1133 if (KTRPOINT(td, KTR_GENIO))
1134 ktruio = cloneuio(&auio);
1136 len = auio.uio_resid;
1137 error = soreceive(so, &fromsa, &auio, NULL,
1138 (mp->msg_control || controlp) ? &control : NULL,
1141 if (auio.uio_resid != len && (error == ERESTART ||
1142 error == EINTR || error == EWOULDBLOCK))
1146 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
1148 if (ktruio != NULL) {
1149 ktruio->uio_resid = len - auio.uio_resid;
1150 ktrgenio(s, UIO_READ, ktruio, error);
1155 td->td_retval[0] = len - auio.uio_resid;
1157 len = mp->msg_namelen;
1158 if (len <= 0 || fromsa == NULL)
1161 /* save sa_len before it is destroyed by MSG_COMPAT */
1162 len = MIN(len, fromsa->sa_len);
1163 #ifdef COMPAT_OLDSOCK
1164 if (mp->msg_flags & MSG_COMPAT)
1165 ((struct osockaddr *)fromsa)->sa_family =
1168 if (fromseg == UIO_USERSPACE) {
1169 error = copyout(fromsa, mp->msg_name,
1174 bcopy(fromsa, mp->msg_name, len);
1176 mp->msg_namelen = len;
1178 if (mp->msg_control && controlp == NULL) {
1179 #ifdef COMPAT_OLDSOCK
1181 * We assume that old recvmsg calls won't receive access
1182 * rights and other control info, esp. as control info
1183 * is always optional and those options didn't exist in 4.3.
1184 * If we receive rights, trim the cmsghdr; anything else
1187 if (control && mp->msg_flags & MSG_COMPAT) {
1188 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1190 mtod(control, struct cmsghdr *)->cmsg_type !=
1192 mp->msg_controllen = 0;
1195 control->m_len -= sizeof (struct cmsghdr);
1196 control->m_data += sizeof (struct cmsghdr);
1199 len = mp->msg_controllen;
1201 mp->msg_controllen = 0;
1202 ctlbuf = mp->msg_control;
1204 while (m && len > 0) {
1205 unsigned int tocopy;
1207 if (len >= m->m_len)
1210 mp->msg_flags |= MSG_CTRUNC;
1214 if ((error = copyout(mtod(m, caddr_t),
1215 ctlbuf, tocopy)) != 0)
1222 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1227 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1228 ktrsockaddr(fromsa);
1230 free(fromsa, M_SONAME);
1232 if (error == 0 && controlp != NULL)
1233 *controlp = control;
1241 recvit(td, s, mp, namelenp)
1249 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1252 if (namelenp != NULL) {
1253 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1254 #ifdef COMPAT_OLDSOCK
1255 if (mp->msg_flags & MSG_COMPAT)
1256 error = 0; /* old recvfrom didn't check */
1263 sys_recvfrom(td, uap)
1265 struct recvfrom_args /* {
1270 struct sockaddr * __restrict from;
1271 socklen_t * __restrict fromlenaddr;
1278 if (uap->fromlenaddr) {
1279 error = copyin(uap->fromlenaddr,
1280 &msg.msg_namelen, sizeof (msg.msg_namelen));
1284 msg.msg_namelen = 0;
1286 msg.msg_name = uap->from;
1287 msg.msg_iov = &aiov;
1289 aiov.iov_base = uap->buf;
1290 aiov.iov_len = uap->len;
1291 msg.msg_control = 0;
1292 msg.msg_flags = uap->flags;
1293 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1298 #ifdef COMPAT_OLDSOCK
1302 struct recvfrom_args *uap;
1305 uap->flags |= MSG_COMPAT;
1306 return (sys_recvfrom(td, uap));
1310 #ifdef COMPAT_OLDSOCK
1314 struct orecv_args /* {
1325 msg.msg_namelen = 0;
1326 msg.msg_iov = &aiov;
1328 aiov.iov_base = uap->buf;
1329 aiov.iov_len = uap->len;
1330 msg.msg_control = 0;
1331 msg.msg_flags = uap->flags;
1332 return (recvit(td, uap->s, &msg, NULL));
1336 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1337 * overlays the new one, missing only the flags, and with the (old) access
1338 * rights where the control fields are now.
1343 struct orecvmsg_args /* {
1345 struct omsghdr *msg;
1353 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1356 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1359 msg.msg_flags = uap->flags | MSG_COMPAT;
1361 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1362 if (msg.msg_controllen && error == 0)
1363 error = copyout(&msg.msg_controllen,
1364 &uap->msg->msg_accrightslen, sizeof (int));
1371 sys_recvmsg(td, uap)
1373 struct recvmsg_args /* {
1380 struct iovec *uiov, *iov;
1383 error = copyin(uap->msg, &msg, sizeof (msg));
1386 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1389 msg.msg_flags = uap->flags;
1390 #ifdef COMPAT_OLDSOCK
1391 msg.msg_flags &= ~MSG_COMPAT;
1395 error = recvit(td, uap->s, &msg, NULL);
1398 error = copyout(&msg, uap->msg, sizeof(msg));
1406 sys_shutdown(td, uap)
1408 struct shutdown_args /* {
1415 cap_rights_t rights;
1418 AUDIT_ARG_FD(uap->s);
1419 error = getsock_cap(td, uap->s, cap_rights_init(&rights, CAP_SHUTDOWN),
1423 error = soshutdown(so, uap->how);
1431 sys_setsockopt(td, uap)
1433 struct setsockopt_args /* {
1442 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1443 uap->val, UIO_USERSPACE, uap->valsize));
1447 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1453 enum uio_seg valseg;
1458 struct sockopt sopt;
1459 cap_rights_t rights;
1462 if (val == NULL && valsize != 0)
1464 if ((int)valsize < 0)
1467 sopt.sopt_dir = SOPT_SET;
1468 sopt.sopt_level = level;
1469 sopt.sopt_name = name;
1470 sopt.sopt_val = val;
1471 sopt.sopt_valsize = valsize;
1477 sopt.sopt_td = NULL;
1480 panic("kern_setsockopt called with bad valseg");
1484 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SETSOCKOPT),
1488 error = sosetopt(so, &sopt);
1496 sys_getsockopt(td, uap)
1498 struct getsockopt_args /* {
1502 void * __restrict val;
1503 socklen_t * __restrict avalsize;
1510 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1515 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1516 uap->val, UIO_USERSPACE, &valsize);
1519 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1524 * Kernel version of getsockopt.
1525 * optval can be a userland or userspace. optlen is always a kernel pointer.
1528 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1534 enum uio_seg valseg;
1539 struct sockopt sopt;
1540 cap_rights_t rights;
1545 if ((int)*valsize < 0)
1548 sopt.sopt_dir = SOPT_GET;
1549 sopt.sopt_level = level;
1550 sopt.sopt_name = name;
1551 sopt.sopt_val = val;
1552 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1558 sopt.sopt_td = NULL;
1561 panic("kern_getsockopt called with bad valseg");
1565 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_GETSOCKOPT),
1569 error = sogetopt(so, &sopt);
1570 *valsize = sopt.sopt_valsize;
1577 * getsockname1() - Get socket name.
1581 getsockname1(td, uap, compat)
1583 struct getsockname_args /* {
1585 struct sockaddr * __restrict asa;
1586 socklen_t * __restrict alen;
1590 struct sockaddr *sa;
1594 error = copyin(uap->alen, &len, sizeof(len));
1598 error = kern_getsockname(td, uap->fdes, &sa, &len);
1603 #ifdef COMPAT_OLDSOCK
1605 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1607 error = copyout(sa, uap->asa, (u_int)len);
1611 error = copyout(&len, uap->alen, sizeof(len));
1616 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1621 cap_rights_t rights;
1626 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETSOCKNAME),
1632 CURVNET_SET(so->so_vnet);
1633 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1640 len = MIN(*alen, (*sa)->sa_len);
1643 if (KTRPOINT(td, KTR_STRUCT))
1648 if (error != 0 && *sa != NULL) {
1649 free(*sa, M_SONAME);
1656 sys_getsockname(td, uap)
1658 struct getsockname_args *uap;
1661 return (getsockname1(td, uap, 0));
1664 #ifdef COMPAT_OLDSOCK
1666 ogetsockname(td, uap)
1668 struct getsockname_args *uap;
1671 return (getsockname1(td, uap, 1));
1673 #endif /* COMPAT_OLDSOCK */
1676 * getpeername1() - Get name of peer for connected socket.
1680 getpeername1(td, uap, compat)
1682 struct getpeername_args /* {
1684 struct sockaddr * __restrict asa;
1685 socklen_t * __restrict alen;
1689 struct sockaddr *sa;
1693 error = copyin(uap->alen, &len, sizeof (len));
1697 error = kern_getpeername(td, uap->fdes, &sa, &len);
1702 #ifdef COMPAT_OLDSOCK
1704 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1706 error = copyout(sa, uap->asa, (u_int)len);
1710 error = copyout(&len, uap->alen, sizeof(len));
1715 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1720 cap_rights_t rights;
1725 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETPEERNAME),
1730 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1735 CURVNET_SET(so->so_vnet);
1736 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1743 len = MIN(*alen, (*sa)->sa_len);
1746 if (KTRPOINT(td, KTR_STRUCT))
1750 if (error != 0 && *sa != NULL) {
1751 free(*sa, M_SONAME);
1760 sys_getpeername(td, uap)
1762 struct getpeername_args *uap;
1765 return (getpeername1(td, uap, 0));
1768 #ifdef COMPAT_OLDSOCK
1770 ogetpeername(td, uap)
1772 struct ogetpeername_args *uap;
1775 /* XXX uap should have type `getpeername_args *' to begin with. */
1776 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1778 #endif /* COMPAT_OLDSOCK */
1781 sockargs(mp, buf, buflen, type)
1786 struct sockaddr *sa;
1793 if (buflen > MLEN) {
1794 #ifdef COMPAT_OLDSOCK
1795 if (type == MT_SONAME && buflen <= 112)
1796 buflen = MLEN; /* unix domain compat. hack */
1799 if (buflen > MCLBYTES)
1802 m = m_get2(buflen, M_WAITOK, type, 0);
1804 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1809 if (type == MT_SONAME) {
1810 sa = mtod(m, struct sockaddr *);
1812 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1813 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1814 sa->sa_family = sa->sa_len;
1816 sa->sa_len = buflen;
1823 getsockaddr(namp, uaddr, len)
1824 struct sockaddr **namp;
1828 struct sockaddr *sa;
1831 if (len > SOCK_MAXADDRLEN)
1832 return (ENAMETOOLONG);
1833 if (len < offsetof(struct sockaddr, sa_data[0]))
1835 sa = malloc(len, M_SONAME, M_WAITOK);
1836 error = copyin(uaddr, sa, len);
1840 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1841 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1842 sa->sa_family = sa->sa_len;
1850 struct sendfile_sync {
1857 * Detach mapped page and release resources back to the system.
1860 sf_buf_mext(struct mbuf *mb, void *addr, void *args)
1863 struct sendfile_sync *sfs;
1865 m = sf_buf_page(args);
1868 vm_page_unwire(m, 0);
1870 * Check for the object going away on us. This can
1871 * happen since we don't hold a reference to it.
1872 * If so, we're responsible for freeing the page.
1874 if (m->wire_count == 0 && m->object == NULL)
1878 return (EXT_FREE_OK);
1880 mtx_lock(&sfs->mtx);
1881 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1882 if (--sfs->count == 0)
1883 cv_signal(&sfs->cv);
1884 mtx_unlock(&sfs->mtx);
1885 return (EXT_FREE_OK);
1891 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1892 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1894 * Send a file specified by 'fd' and starting at 'offset' to a socket
1895 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1896 * 0. Optionally add a header and/or trailer to the socket output. If
1897 * specified, write the total number of bytes sent into *sbytes.
1900 sys_sendfile(struct thread *td, struct sendfile_args *uap)
1903 return (do_sendfile(td, uap, 0));
1907 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1909 struct sf_hdtr hdtr;
1910 struct uio *hdr_uio, *trl_uio;
1912 cap_rights_t rights;
1916 * File offset must be positive. If it goes beyond EOF
1917 * we send only the header/trailer and no payload data.
1919 if (uap->offset < 0)
1922 hdr_uio = trl_uio = NULL;
1924 if (uap->hdtr != NULL) {
1925 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1928 if (hdtr.headers != NULL) {
1929 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1933 if (hdtr.trailers != NULL) {
1934 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1941 AUDIT_ARG_FD(uap->fd);
1944 * sendfile(2) can start at any offset within a file so we require
1945 * CAP_READ+CAP_SEEK = CAP_PREAD.
1947 if ((error = fget_read(td, uap->fd,
1948 cap_rights_init(&rights, CAP_PREAD), &fp)) != 0) {
1952 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, uap->offset,
1953 uap->nbytes, uap->sbytes, uap->flags, compat ? SFK_COMPAT : 0, td);
1957 free(hdr_uio, M_IOV);
1958 free(trl_uio, M_IOV);
1962 #ifdef COMPAT_FREEBSD4
1964 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1966 struct sendfile_args args;
1970 args.offset = uap->offset;
1971 args.nbytes = uap->nbytes;
1972 args.hdtr = uap->hdtr;
1973 args.sbytes = uap->sbytes;
1974 args.flags = uap->flags;
1976 return (do_sendfile(td, &args, 1));
1978 #endif /* COMPAT_FREEBSD4 */
1981 sendfile_readpage(vm_object_t obj, struct vnode *vp, int nd,
1982 off_t off, int xfsize, int bsize, struct thread *td, vm_page_t *res)
1987 int error, readahead, rv;
1989 pindex = OFF_TO_IDX(off);
1990 VM_OBJECT_WLOCK(obj);
1991 m = vm_page_grab(obj, pindex, (vp != NULL ? VM_ALLOC_NOBUSY |
1992 VM_ALLOC_IGN_SBUSY : 0) | VM_ALLOC_WIRED | VM_ALLOC_NORMAL);
1995 * Check if page is valid for what we need, otherwise initiate I/O.
1997 * The non-zero nd argument prevents disk I/O, instead we
1998 * return the caller what he specified in nd. In particular,
1999 * if we already turned some pages into mbufs, nd == EAGAIN
2000 * and the main function send them the pages before we come
2001 * here again and block.
2003 if (m->valid != 0 && vm_page_is_valid(m, off & PAGE_MASK, xfsize)) {
2006 VM_OBJECT_WUNLOCK(obj);
2009 } else if (nd != 0) {
2017 * Get the page from backing store.
2021 VM_OBJECT_WUNLOCK(obj);
2022 readahead = sfreadahead * MAXBSIZE;
2025 * Use vn_rdwr() instead of the pager interface for
2026 * the vnode, to allow the read-ahead.
2028 * XXXMAC: Because we don't have fp->f_cred here, we
2029 * pass in NOCRED. This is probably wrong, but is
2030 * consistent with our original implementation.
2032 error = vn_rdwr(UIO_READ, vp, NULL, readahead, trunc_page(off),
2033 UIO_NOCOPY, IO_NODELOCKED | IO_VMIO | ((readahead /
2034 bsize) << IO_SEQSHIFT), td->td_ucred, NOCRED, &resid, td);
2035 SFSTAT_INC(sf_iocnt);
2036 VM_OBJECT_WLOCK(obj);
2038 if (vm_pager_has_page(obj, pindex, NULL, NULL)) {
2039 rv = vm_pager_get_pages(obj, &m, 1, 0);
2040 SFSTAT_INC(sf_iocnt);
2041 m = vm_page_lookup(obj, pindex);
2044 else if (rv != VM_PAGER_OK) {
2053 m->valid = VM_PAGE_BITS_ALL;
2061 } else if (m != NULL) {
2064 vm_page_unwire(m, 0);
2067 * See if anyone else might know about this page. If
2068 * not and it is not valid, then free it.
2070 if (m->wire_count == 0 && m->valid == 0 && !vm_page_busied(m))
2074 KASSERT(error != 0 || (m->wire_count > 0 &&
2075 vm_page_is_valid(m, off & PAGE_MASK, xfsize)),
2076 ("wrong page state m %p off %#jx xfsize %d", m, (uintmax_t)off,
2078 VM_OBJECT_WUNLOCK(obj);
2083 sendfile_getobj(struct thread *td, struct file *fp, vm_object_t *obj_res,
2084 struct vnode **vp_res, struct shmfd **shmfd_res, off_t *obj_size,
2090 struct shmfd *shmfd;
2093 vp = *vp_res = NULL;
2095 shmfd = *shmfd_res = NULL;
2099 * The file descriptor must be a regular file and have a
2100 * backing VM object.
2102 if (fp->f_type == DTYPE_VNODE) {
2104 vn_lock(vp, LK_SHARED | LK_RETRY);
2105 if (vp->v_type != VREG) {
2109 *bsize = vp->v_mount->mnt_stat.f_iosize;
2110 error = VOP_GETATTR(vp, &va, td->td_ucred);
2113 *obj_size = va.va_size;
2119 } else if (fp->f_type == DTYPE_SHM) {
2122 obj = shmfd->shm_object;
2123 *obj_size = shmfd->shm_size;
2129 VM_OBJECT_WLOCK(obj);
2130 if ((obj->flags & OBJ_DEAD) != 0) {
2131 VM_OBJECT_WUNLOCK(obj);
2137 * Temporarily increase the backing VM object's reference
2138 * count so that a forced reclamation of its vnode does not
2139 * immediately destroy it.
2141 vm_object_reference_locked(obj);
2142 VM_OBJECT_WUNLOCK(obj);
2154 kern_sendfile_getsock(struct thread *td, int s, struct file **sock_fp,
2157 cap_rights_t rights;
2164 * The socket must be a stream socket and connected.
2166 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SEND),
2170 *so = (*sock_fp)->f_data;
2171 if ((*so)->so_type != SOCK_STREAM)
2173 if (((*so)->so_state & SS_ISCONNECTED) == 0)
2179 vn_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio,
2180 struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags,
2181 int kflags, struct thread *td)
2183 struct file *sock_fp;
2185 struct vm_object *obj;
2190 struct shmfd *shmfd;
2191 struct sendfile_sync *sfs;
2193 off_t off, xfsize, fsbytes, sbytes, rem, obj_size;
2194 int error, bsize, nd, hdrlen, mnw;
2195 bool inflight_called;
2202 fsbytes = sbytes = 0;
2206 inflight_called = false;
2208 error = sendfile_getobj(td, fp, &obj, &vp, &shmfd, &obj_size, &bsize);
2214 error = kern_sendfile_getsock(td, sockfd, &sock_fp, &so);
2219 * Do not wait on memory allocations but return ENOMEM for
2220 * caller to retry later.
2221 * XXX: Experimental.
2223 if (flags & SF_MNOWAIT)
2226 if (flags & SF_SYNC) {
2227 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO);
2228 mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
2229 cv_init(&sfs->cv, "sendfile");
2233 error = mac_socket_check_send(td->td_ucred, so);
2238 /* If headers are specified copy them into mbufs. */
2239 if (hdr_uio != NULL) {
2240 hdr_uio->uio_td = td;
2241 hdr_uio->uio_rw = UIO_WRITE;
2242 if (hdr_uio->uio_resid > 0) {
2244 * In FBSD < 5.0 the nbytes to send also included
2245 * the header. If compat is specified subtract the
2246 * header size from nbytes.
2248 if (kflags & SFK_COMPAT) {
2249 if (nbytes > hdr_uio->uio_resid)
2250 nbytes -= hdr_uio->uio_resid;
2254 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
2257 error = mnw ? EAGAIN : ENOBUFS;
2260 hdrlen = m_length(m, NULL);
2265 * Protect against multiple writers to the socket.
2267 * XXXRW: Historically this has assumed non-interruptibility, so now
2268 * we implement that, but possibly shouldn't.
2270 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
2273 * Loop through the pages of the file, starting with the requested
2274 * offset. Get a file page (do I/O if necessary), map the file page
2275 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
2277 * This is done in two loops. The inner loop turns as many pages
2278 * as it can, up to available socket buffer space, without blocking
2279 * into mbufs to have it bulk delivered into the socket send buffer.
2280 * The outer loop checks the state and available space of the socket
2281 * and takes care of the overall progress.
2283 for (off = offset; ; ) {
2289 if ((nbytes != 0 && nbytes == fsbytes) ||
2290 (nbytes == 0 && obj_size == fsbytes))
2299 * Check the socket state for ongoing connection,
2300 * no errors and space in socket buffer.
2301 * If space is low allow for the remainder of the
2302 * file to be processed if it fits the socket buffer.
2303 * Otherwise block in waiting for sufficient space
2304 * to proceed, or if the socket is nonblocking, return
2305 * to userland with EAGAIN while reporting how far
2307 * We wait until the socket buffer has significant free
2308 * space to do bulk sends. This makes good use of file
2309 * system read ahead and allows packet segmentation
2310 * offloading hardware to take over lots of work. If
2311 * we were not careful here we would send off only one
2314 SOCKBUF_LOCK(&so->so_snd);
2315 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
2316 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
2318 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2320 SOCKBUF_UNLOCK(&so->so_snd);
2322 } else if (so->so_error) {
2323 error = so->so_error;
2325 SOCKBUF_UNLOCK(&so->so_snd);
2328 space = sbspace(&so->so_snd);
2331 space < so->so_snd.sb_lowat)) {
2332 if (so->so_state & SS_NBIO) {
2333 SOCKBUF_UNLOCK(&so->so_snd);
2338 * sbwait drops the lock while sleeping.
2339 * When we loop back to retry_space the
2340 * state may have changed and we retest
2343 error = sbwait(&so->so_snd);
2345 * An error from sbwait usually indicates that we've
2346 * been interrupted by a signal. If we've sent anything
2347 * then return bytes sent, otherwise return the error.
2350 SOCKBUF_UNLOCK(&so->so_snd);
2355 SOCKBUF_UNLOCK(&so->so_snd);
2358 * Reduce space in the socket buffer by the size of
2359 * the header mbuf chain.
2360 * hdrlen is set to 0 after the first loop.
2365 error = vn_lock(vp, LK_SHARED);
2368 error = VOP_GETATTR(vp, &va, td->td_ucred);
2369 if (error != 0 || off >= va.va_size) {
2373 obj_size = va.va_size;
2377 * Loop and construct maximum sized mbuf chain to be bulk
2378 * dumped into socket buffer.
2380 while (space > loopbytes) {
2385 * Calculate the amount to transfer.
2386 * Not to exceed a page, the EOF,
2387 * or the passed in nbytes.
2389 pgoff = (vm_offset_t)(off & PAGE_MASK);
2390 rem = obj_size - offset;
2392 rem = omin(rem, nbytes);
2393 rem -= fsbytes + loopbytes;
2394 xfsize = omin(PAGE_SIZE - pgoff, rem);
2395 xfsize = omin(space - loopbytes, xfsize);
2397 done = 1; /* all data sent */
2402 * Attempt to look up the page. Allocate
2403 * if not found or wait and loop if busy.
2406 nd = EAGAIN; /* send what we already got */
2407 else if ((flags & SF_NODISKIO) != 0)
2411 error = sendfile_readpage(obj, vp, nd, off,
2412 xfsize, bsize, td, &pg);
2414 if (error == EAGAIN)
2415 error = 0; /* not a real error */
2420 * Get a sendfile buf. When allocating the
2421 * first buffer for mbuf chain, we usually
2422 * wait as long as necessary, but this wait
2423 * can be interrupted. For consequent
2424 * buffers, do not sleep, since several
2425 * threads might exhaust the buffers and then
2428 sf = sf_buf_alloc(pg, (mnw || m != NULL) ? SFB_NOWAIT :
2431 SFSTAT_INC(sf_allocfail);
2433 vm_page_unwire(pg, 0);
2434 KASSERT(pg->object != NULL,
2435 ("%s: object disappeared", __func__));
2438 error = (mnw ? EAGAIN : EINTR);
2443 * Get an mbuf and set it up as having
2446 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2448 error = (mnw ? EAGAIN : ENOBUFS);
2449 (void)sf_buf_mext(NULL, NULL, sf);
2452 if (m_extadd(m0, (caddr_t )sf_buf_kva(sf), PAGE_SIZE,
2453 sf_buf_mext, sfs, sf, M_RDONLY, EXT_SFBUF,
2454 (mnw ? M_NOWAIT : M_WAITOK)) != 0) {
2455 error = (mnw ? EAGAIN : ENOBUFS);
2456 (void)sf_buf_mext(NULL, NULL, sf);
2460 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2463 /* Append to mbuf chain. */
2467 m_last(m)->m_next = m0;
2472 /* Keep track of bits processed. */
2473 loopbytes += xfsize;
2477 mtx_lock(&sfs->mtx);
2479 mtx_unlock(&sfs->mtx);
2486 /* Add the buffer chain to the socket buffer. */
2490 mlen = m_length(m, NULL);
2491 SOCKBUF_LOCK(&so->so_snd);
2492 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2494 SOCKBUF_UNLOCK(&so->so_snd);
2497 SOCKBUF_UNLOCK(&so->so_snd);
2498 CURVNET_SET(so->so_vnet);
2499 /* Avoid error aliasing. */
2500 err = (*so->so_proto->pr_usrreqs->pru_send)
2501 (so, 0, m, NULL, NULL, td);
2505 * We need two counters to get the
2506 * file offset and nbytes to send
2508 * - sbytes contains the total amount
2509 * of bytes sent, including headers.
2510 * - fsbytes contains the total amount
2511 * of bytes sent from the file.
2519 } else if (error == 0)
2521 m = NULL; /* pru_send always consumes */
2524 /* Quit outer loop on error or when we're done. */
2532 * Send trailers. Wimp out and use writev(2).
2534 if (trl_uio != NULL) {
2535 sbunlock(&so->so_snd);
2536 error = kern_writev(td, sockfd, trl_uio);
2538 sbytes += td->td_retval[0];
2543 sbunlock(&so->so_snd);
2546 * If there was no error we have to clear td->td_retval[0]
2547 * because it may have been set by writev.
2550 td->td_retval[0] = 0;
2553 copyout(&sbytes, sent, sizeof(off_t));
2556 vm_object_deallocate(obj);
2563 mtx_lock(&sfs->mtx);
2564 if (sfs->count != 0)
2565 cv_wait(&sfs->cv, &sfs->mtx);
2566 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2567 cv_destroy(&sfs->cv);
2568 mtx_destroy(&sfs->mtx);
2572 if (error == ERESTART)