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"
42 #include "opt_compat.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/capability.h>
48 #include <sys/condvar.h>
49 #include <sys/kernel.h>
51 #include <sys/mutex.h>
52 #include <sys/sysproto.h>
53 #include <sys/malloc.h>
54 #include <sys/filedesc.h>
55 #include <sys/event.h>
57 #include <sys/fcntl.h>
59 #include <sys/filio.h>
62 #include <sys/mount.h>
64 #include <sys/protosw.h>
65 #include <sys/rwlock.h>
66 #include <sys/sf_buf.h>
67 #include <sys/sysent.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
70 #include <sys/signalvar.h>
71 #include <sys/syscallsubr.h>
72 #include <sys/sysctl.h>
74 #include <sys/vnode.h>
76 #include <sys/ktrace.h>
78 #ifdef COMPAT_FREEBSD32
79 #include <compat/freebsd32/freebsd32_util.h>
84 #include <security/audit/audit.h>
85 #include <security/mac/mac_framework.h>
88 #include <vm/vm_param.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_page.h>
91 #include <vm/vm_pager.h>
92 #include <vm/vm_kern.h>
93 #include <vm/vm_extern.h>
95 #if defined(INET) || defined(INET6)
97 #include <netinet/sctp.h>
98 #include <netinet/sctp_peeloff.h>
100 #endif /* INET || INET6 */
103 * Flags for accept1() and kern_accept4(), in addition to SOCK_CLOEXEC
106 #define ACCEPT4_INHERIT 0x1
107 #define ACCEPT4_COMPAT 0x2
109 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
110 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
112 static int accept1(struct thread *td, int s, struct sockaddr *uname,
113 socklen_t *anamelen, int flags);
114 static int do_sendfile(struct thread *td, struct sendfile_args *uap,
116 static int getsockname1(struct thread *td, struct getsockname_args *uap,
118 static int getpeername1(struct thread *td, struct getpeername_args *uap,
121 counter_u64_t sfstat[sizeof(struct sfstat) / sizeof(uint64_t)];
124 * sendfile(2)-related variables and associated sysctls
126 static SYSCTL_NODE(_kern_ipc, OID_AUTO, sendfile, CTLFLAG_RW, 0,
127 "sendfile(2) tunables");
128 static int sfreadahead = 1;
129 SYSCTL_INT(_kern_ipc_sendfile, OID_AUTO, readahead, CTLFLAG_RW,
130 &sfreadahead, 0, "Number of sendfile(2) read-ahead MAXBSIZE blocks");
134 sfstat_init(const void *unused)
137 COUNTER_ARRAY_ALLOC(sfstat, sizeof(struct sfstat) / sizeof(uint64_t),
140 SYSINIT(sfstat, SI_SUB_MBUF, SI_ORDER_FIRST, sfstat_init, NULL);
143 sfstat_sysctl(SYSCTL_HANDLER_ARGS)
147 COUNTER_ARRAY_COPY(sfstat, &s, sizeof(s) / sizeof(uint64_t));
149 COUNTER_ARRAY_ZERO(sfstat, sizeof(s) / sizeof(uint64_t));
150 return (SYSCTL_OUT(req, &s, sizeof(s)));
152 SYSCTL_PROC(_kern_ipc, OID_AUTO, sfstat, CTLTYPE_OPAQUE | CTLFLAG_RW,
153 NULL, 0, sfstat_sysctl, "I", "sendfile statistics");
156 * Convert a user file descriptor to a kernel file entry and check if required
157 * capability rights are present.
158 * A reference on the file entry is held upon returning.
161 getsock_cap(struct filedesc *fdp, int fd, cap_rights_t *rightsp,
162 struct file **fpp, u_int *fflagp)
167 error = fget_unlocked(fdp, fd, rightsp, 0, &fp, NULL);
170 if (fp->f_type != DTYPE_SOCKET) {
171 fdrop(fp, curthread);
175 *fflagp = fp->f_flag;
181 * System call interface to the socket abstraction.
183 #if defined(COMPAT_43)
184 #define COMPAT_OLDSOCK
190 struct socket_args /* {
198 int fd, error, type, oflag, fflag;
200 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
205 if ((type & SOCK_CLOEXEC) != 0) {
206 type &= ~SOCK_CLOEXEC;
209 if ((type & SOCK_NONBLOCK) != 0) {
210 type &= ~SOCK_NONBLOCK;
215 error = mac_socket_check_create(td->td_ucred, uap->domain, type,
220 error = falloc(td, &fp, &fd, oflag);
223 /* An extra reference on `fp' has been held for us by falloc(). */
224 error = socreate(uap->domain, &so, type, uap->protocol,
227 fdclose(td->td_proc->p_fd, fp, fd, td);
229 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
230 if ((fflag & FNONBLOCK) != 0)
231 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
232 td->td_retval[0] = fd;
242 struct bind_args /* {
251 error = getsockaddr(&sa, uap->name, uap->namelen);
253 error = kern_bind(td, uap->s, sa);
260 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
268 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
269 error = getsock_cap(td->td_proc->p_fd, fd,
270 cap_rights_init(&rights, CAP_BIND), &fp, NULL);
275 if (KTRPOINT(td, KTR_STRUCT))
279 error = mac_socket_check_bind(td->td_ucred, so, sa);
282 if (dirfd == AT_FDCWD)
283 error = sobind(so, sa, td);
285 error = sobindat(dirfd, so, sa, td);
294 kern_bind(struct thread *td, int fd, struct sockaddr *sa)
297 return (kern_bindat(td, AT_FDCWD, fd, sa));
304 struct bindat_args /* {
314 error = getsockaddr(&sa, uap->name, uap->namelen);
316 error = kern_bindat(td, uap->fd, uap->s, sa);
326 struct listen_args /* {
336 AUDIT_ARG_FD(uap->s);
337 error = getsock_cap(td->td_proc->p_fd, uap->s,
338 cap_rights_init(&rights, CAP_LISTEN), &fp, NULL);
342 error = mac_socket_check_listen(td->td_ucred, so);
345 error = solisten(so, uap->backlog, td);
355 accept1(td, s, uname, anamelen, flags)
358 struct sockaddr *uname;
362 struct sockaddr *name;
368 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
370 error = copyin(anamelen, &namelen, sizeof (namelen));
374 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
377 * return a namelen of zero for older code which might
378 * ignore the return value from accept.
381 (void) copyout(&namelen, anamelen, sizeof(*anamelen));
385 if (error == 0 && uname != NULL) {
386 #ifdef COMPAT_OLDSOCK
387 if (flags & ACCEPT4_COMPAT)
388 ((struct osockaddr *)name)->sa_family =
391 error = copyout(name, uname, namelen);
394 error = copyout(&namelen, anamelen,
397 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
399 free(name, M_SONAME);
404 kern_accept(struct thread *td, int s, struct sockaddr **name,
405 socklen_t *namelen, struct file **fp)
407 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
411 kern_accept4(struct thread *td, int s, struct sockaddr **name,
412 socklen_t *namelen, int flags, struct file **fp)
414 struct filedesc *fdp;
415 struct file *headfp, *nfp = NULL;
416 struct sockaddr *sa = NULL;
417 struct socket *head, *so;
427 fdp = td->td_proc->p_fd;
428 error = getsock_cap(fdp, s, cap_rights_init(&rights, CAP_ACCEPT),
432 head = headfp->f_data;
433 if ((head->so_options & SO_ACCEPTCONN) == 0) {
438 error = mac_socket_check_accept(td->td_ucred, head);
442 error = falloc(td, &nfp, &fd, (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0);
446 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
451 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
452 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
453 head->so_error = ECONNABORTED;
456 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
463 if (head->so_error) {
464 error = head->so_error;
469 so = TAILQ_FIRST(&head->so_comp);
470 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
471 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
474 * Before changing the flags on the socket, we have to bump the
475 * reference count. Otherwise, if the protocol calls sofree(),
476 * the socket will be released due to a zero refcount.
478 SOCK_LOCK(so); /* soref() and so_state update */
479 soref(so); /* file descriptor reference */
481 TAILQ_REMOVE(&head->so_comp, so, so_list);
483 if (flags & ACCEPT4_INHERIT)
484 so->so_state |= (head->so_state & SS_NBIO);
486 so->so_state |= (flags & SOCK_NONBLOCK) ? SS_NBIO : 0;
487 so->so_qstate &= ~SQ_COMP;
493 /* An extra reference on `nfp' has been held for us by falloc(). */
494 td->td_retval[0] = fd;
496 /* connection has been removed from the listen queue */
497 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
499 if (flags & ACCEPT4_INHERIT) {
500 pgid = fgetown(&head->so_sigio);
502 fsetown(pgid, &so->so_sigio);
504 fflag &= ~(FNONBLOCK | FASYNC);
505 if (flags & SOCK_NONBLOCK)
509 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
510 /* Sync socket nonblocking/async state with file flags */
511 tmp = fflag & FNONBLOCK;
512 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
513 tmp = fflag & FASYNC;
514 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
516 error = soaccept(so, &sa);
519 * return a namelen of zero for older code which might
520 * ignore the return value from accept.
531 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
533 /* check sa_len before it is destroyed */
534 if (*namelen > sa->sa_len)
535 *namelen = sa->sa_len;
537 if (KTRPOINT(td, KTR_STRUCT))
547 * close the new descriptor, assuming someone hasn't ripped it
551 fdclose(fdp, nfp, fd, td);
554 * Release explicitly held references before returning. We return
555 * a reference on nfp to the caller on success if they request it.
574 struct accept_args *uap;
577 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
583 struct accept4_args *uap;
586 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
589 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
592 #ifdef COMPAT_OLDSOCK
596 struct accept_args *uap;
599 return (accept1(td, uap->s, uap->name, uap->anamelen,
600 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
602 #endif /* COMPAT_OLDSOCK */
608 struct connect_args /* {
617 error = getsockaddr(&sa, uap->name, uap->namelen);
619 error = kern_connect(td, uap->s, sa);
626 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
631 int error, interrupted = 0;
634 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
635 error = getsock_cap(td->td_proc->p_fd, fd,
636 cap_rights_init(&rights, CAP_CONNECT), &fp, NULL);
640 if (so->so_state & SS_ISCONNECTING) {
645 if (KTRPOINT(td, KTR_STRUCT))
649 error = mac_socket_check_connect(td->td_ucred, so, sa);
653 if (dirfd == AT_FDCWD)
654 error = soconnect(so, sa, td);
656 error = soconnectat(dirfd, so, sa, td);
659 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
664 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
665 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
668 if (error == EINTR || error == ERESTART)
674 error = so->so_error;
680 so->so_state &= ~SS_ISCONNECTING;
681 if (error == ERESTART)
689 kern_connect(struct thread *td, int fd, struct sockaddr *sa)
692 return (kern_connectat(td, AT_FDCWD, fd, sa));
697 sys_connectat(td, uap)
699 struct connectat_args /* {
709 error = getsockaddr(&sa, uap->name, uap->namelen);
711 error = kern_connectat(td, uap->fd, uap->s, sa);
718 kern_socketpair(struct thread *td, int domain, int type, int protocol,
721 struct filedesc *fdp = td->td_proc->p_fd;
722 struct file *fp1, *fp2;
723 struct socket *so1, *so2;
724 int fd, error, oflag, fflag;
726 AUDIT_ARG_SOCKET(domain, type, protocol);
730 if ((type & SOCK_CLOEXEC) != 0) {
731 type &= ~SOCK_CLOEXEC;
734 if ((type & SOCK_NONBLOCK) != 0) {
735 type &= ~SOCK_NONBLOCK;
739 /* We might want to have a separate check for socket pairs. */
740 error = mac_socket_check_create(td->td_ucred, domain, type,
745 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
748 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
751 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
752 error = falloc(td, &fp1, &fd, oflag);
756 fp1->f_data = so1; /* so1 already has ref count */
757 error = falloc(td, &fp2, &fd, oflag);
760 fp2->f_data = so2; /* so2 already has ref count */
762 error = soconnect2(so1, so2);
765 if (type == SOCK_DGRAM) {
767 * Datagram socket connection is asymmetric.
769 error = soconnect2(so2, so1);
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(fdp, fp2, rsv[1], td);
788 fdclose(fdp, fp1, rsv[0], td);
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->td_proc->p_fd, 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->td_proc->p_fd, s,
1105 cap_rights_init(&rights, CAP_RECV), &fp, NULL);
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->td_proc->p_fd, uap->s,
1420 cap_rights_init(&rights, CAP_SHUTDOWN), &fp, NULL);
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->td_proc->p_fd, s,
1485 cap_rights_init(&rights, CAP_SETSOCKOPT), &fp, NULL);
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->td_proc->p_fd, s,
1566 cap_rights_init(&rights, CAP_GETSOCKOPT), &fp, NULL);
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->td_proc->p_fd, fd,
1627 cap_rights_init(&rights, CAP_GETSOCKNAME), &fp, NULL);
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->td_proc->p_fd, fd,
1726 cap_rights_init(&rights, CAP_GETPEERNAME), &fp, NULL);
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;
1790 if (buflen > MLEN) {
1791 #ifdef COMPAT_OLDSOCK
1792 if (type == MT_SONAME && buflen <= 112)
1793 buflen = MLEN; /* unix domain compat. hack */
1796 if (buflen > MCLBYTES)
1799 m = m_get2(buflen, M_WAITOK, type, 0);
1801 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1806 if (type == MT_SONAME) {
1807 sa = mtod(m, struct sockaddr *);
1809 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1810 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1811 sa->sa_family = sa->sa_len;
1813 sa->sa_len = buflen;
1820 getsockaddr(namp, uaddr, len)
1821 struct sockaddr **namp;
1825 struct sockaddr *sa;
1828 if (len > SOCK_MAXADDRLEN)
1829 return (ENAMETOOLONG);
1830 if (len < offsetof(struct sockaddr, sa_data[0]))
1832 sa = malloc(len, M_SONAME, M_WAITOK);
1833 error = copyin(uaddr, sa, len);
1837 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1838 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1839 sa->sa_family = sa->sa_len;
1847 struct sendfile_sync {
1854 * Detach mapped page and release resources back to the system.
1857 sf_buf_mext(struct mbuf *mb, void *addr, void *args)
1860 struct sendfile_sync *sfs;
1862 m = sf_buf_page(args);
1865 vm_page_unwire(m, 0);
1867 * Check for the object going away on us. This can
1868 * happen since we don't hold a reference to it.
1869 * If so, we're responsible for freeing the page.
1871 if (m->wire_count == 0 && m->object == NULL)
1875 return (EXT_FREE_OK);
1877 mtx_lock(&sfs->mtx);
1878 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1879 if (--sfs->count == 0)
1880 cv_signal(&sfs->cv);
1881 mtx_unlock(&sfs->mtx);
1882 return (EXT_FREE_OK);
1888 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1889 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1891 * Send a file specified by 'fd' and starting at 'offset' to a socket
1892 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1893 * 0. Optionally add a header and/or trailer to the socket output. If
1894 * specified, write the total number of bytes sent into *sbytes.
1897 sys_sendfile(struct thread *td, struct sendfile_args *uap)
1900 return (do_sendfile(td, uap, 0));
1904 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1906 struct sf_hdtr hdtr;
1907 struct uio *hdr_uio, *trl_uio;
1909 cap_rights_t rights;
1913 * File offset must be positive. If it goes beyond EOF
1914 * we send only the header/trailer and no payload data.
1916 if (uap->offset < 0)
1919 hdr_uio = trl_uio = NULL;
1921 if (uap->hdtr != NULL) {
1922 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1925 if (hdtr.headers != NULL) {
1926 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1930 if (hdtr.trailers != NULL) {
1931 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1938 AUDIT_ARG_FD(uap->fd);
1941 * sendfile(2) can start at any offset within a file so we require
1942 * CAP_READ+CAP_SEEK = CAP_PREAD.
1944 if ((error = fget_read(td, uap->fd,
1945 cap_rights_init(&rights, CAP_PREAD), &fp)) != 0) {
1949 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, uap->offset,
1950 uap->nbytes, uap->sbytes, uap->flags, compat ? SFK_COMPAT : 0, td);
1954 free(hdr_uio, M_IOV);
1955 free(trl_uio, M_IOV);
1959 #ifdef COMPAT_FREEBSD4
1961 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1963 struct sendfile_args args;
1967 args.offset = uap->offset;
1968 args.nbytes = uap->nbytes;
1969 args.hdtr = uap->hdtr;
1970 args.sbytes = uap->sbytes;
1971 args.flags = uap->flags;
1973 return (do_sendfile(td, &args, 1));
1975 #endif /* COMPAT_FREEBSD4 */
1978 sendfile_readpage(vm_object_t obj, struct vnode *vp, int nd,
1979 off_t off, int xfsize, int bsize, struct thread *td, vm_page_t *res)
1984 int error, readahead, rv;
1986 pindex = OFF_TO_IDX(off);
1987 VM_OBJECT_WLOCK(obj);
1988 m = vm_page_grab(obj, pindex, (vp != NULL ? VM_ALLOC_NOBUSY |
1989 VM_ALLOC_IGN_SBUSY : 0) | VM_ALLOC_WIRED | VM_ALLOC_NORMAL);
1992 * Check if page is valid for what we need, otherwise initiate I/O.
1994 * The non-zero nd argument prevents disk I/O, instead we
1995 * return the caller what he specified in nd. In particular,
1996 * if we already turned some pages into mbufs, nd == EAGAIN
1997 * and the main function send them the pages before we come
1998 * here again and block.
2000 if (m->valid != 0 && vm_page_is_valid(m, off & PAGE_MASK, xfsize)) {
2003 VM_OBJECT_WUNLOCK(obj);
2006 } else if (nd != 0) {
2014 * Get the page from backing store.
2018 VM_OBJECT_WUNLOCK(obj);
2019 readahead = sfreadahead * MAXBSIZE;
2022 * Use vn_rdwr() instead of the pager interface for
2023 * the vnode, to allow the read-ahead.
2025 * XXXMAC: Because we don't have fp->f_cred here, we
2026 * pass in NOCRED. This is probably wrong, but is
2027 * consistent with our original implementation.
2029 error = vn_rdwr(UIO_READ, vp, NULL, readahead, trunc_page(off),
2030 UIO_NOCOPY, IO_NODELOCKED | IO_VMIO | ((readahead /
2031 bsize) << IO_SEQSHIFT), td->td_ucred, NOCRED, &resid, td);
2032 SFSTAT_INC(sf_iocnt);
2033 VM_OBJECT_WLOCK(obj);
2035 if (vm_pager_has_page(obj, pindex, NULL, NULL)) {
2036 rv = vm_pager_get_pages(obj, &m, 1, 0);
2037 SFSTAT_INC(sf_iocnt);
2038 m = vm_page_lookup(obj, pindex);
2041 else if (rv != VM_PAGER_OK) {
2050 m->valid = VM_PAGE_BITS_ALL;
2058 } else if (m != NULL) {
2061 vm_page_unwire(m, 0);
2064 * See if anyone else might know about this page. If
2065 * not and it is not valid, then free it.
2067 if (m->wire_count == 0 && m->valid == 0 && !vm_page_busied(m))
2071 KASSERT(error != 0 || (m->wire_count > 0 &&
2072 vm_page_is_valid(m, off & PAGE_MASK, xfsize)),
2073 ("wrong page state m %p off %#jx xfsize %d", m, (uintmax_t)off,
2075 VM_OBJECT_WUNLOCK(obj);
2080 sendfile_getobj(struct thread *td, struct file *fp, vm_object_t *obj_res,
2081 struct vnode **vp_res, struct shmfd **shmfd_res, off_t *obj_size,
2087 struct shmfd *shmfd;
2090 vp = *vp_res = NULL;
2092 shmfd = *shmfd_res = NULL;
2096 * The file descriptor must be a regular file and have a
2097 * backing VM object.
2099 if (fp->f_type == DTYPE_VNODE) {
2101 vn_lock(vp, LK_SHARED | LK_RETRY);
2102 if (vp->v_type != VREG) {
2106 *bsize = vp->v_mount->mnt_stat.f_iosize;
2107 error = VOP_GETATTR(vp, &va, td->td_ucred);
2110 *obj_size = va.va_size;
2116 } else if (fp->f_type == DTYPE_SHM) {
2118 obj = shmfd->shm_object;
2119 *obj_size = shmfd->shm_size;
2125 VM_OBJECT_WLOCK(obj);
2126 if ((obj->flags & OBJ_DEAD) != 0) {
2127 VM_OBJECT_WUNLOCK(obj);
2133 * Temporarily increase the backing VM object's reference
2134 * count so that a forced reclamation of its vnode does not
2135 * immediately destroy it.
2137 vm_object_reference_locked(obj);
2138 VM_OBJECT_WUNLOCK(obj);
2150 kern_sendfile_getsock(struct thread *td, int s, struct file **sock_fp,
2153 cap_rights_t rights;
2160 * The socket must be a stream socket and connected.
2162 error = getsock_cap(td->td_proc->p_fd, s, cap_rights_init(&rights,
2163 CAP_SEND), sock_fp, NULL);
2166 *so = (*sock_fp)->f_data;
2167 if ((*so)->so_type != SOCK_STREAM)
2169 if (((*so)->so_state & SS_ISCONNECTED) == 0)
2175 vn_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio,
2176 struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags,
2177 int kflags, struct thread *td)
2179 struct file *sock_fp;
2181 struct vm_object *obj;
2186 struct shmfd *shmfd;
2187 struct sendfile_sync *sfs;
2189 off_t off, xfsize, fsbytes, sbytes, rem, obj_size;
2190 int error, bsize, nd, hdrlen, mnw;
2191 bool inflight_called;
2198 fsbytes = sbytes = 0;
2202 inflight_called = false;
2204 error = sendfile_getobj(td, fp, &obj, &vp, &shmfd, &obj_size, &bsize);
2210 error = kern_sendfile_getsock(td, sockfd, &sock_fp, &so);
2215 * Do not wait on memory allocations but return ENOMEM for
2216 * caller to retry later.
2217 * XXX: Experimental.
2219 if (flags & SF_MNOWAIT)
2222 if (flags & SF_SYNC) {
2223 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO);
2224 mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
2225 cv_init(&sfs->cv, "sendfile");
2229 error = mac_socket_check_send(td->td_ucred, so);
2234 /* If headers are specified copy them into mbufs. */
2235 if (hdr_uio != NULL) {
2236 hdr_uio->uio_td = td;
2237 hdr_uio->uio_rw = UIO_WRITE;
2238 if (hdr_uio->uio_resid > 0) {
2240 * In FBSD < 5.0 the nbytes to send also included
2241 * the header. If compat is specified subtract the
2242 * header size from nbytes.
2244 if (kflags & SFK_COMPAT) {
2245 if (nbytes > hdr_uio->uio_resid)
2246 nbytes -= hdr_uio->uio_resid;
2250 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
2253 error = mnw ? EAGAIN : ENOBUFS;
2256 hdrlen = m_length(m, NULL);
2261 * Protect against multiple writers to the socket.
2263 * XXXRW: Historically this has assumed non-interruptibility, so now
2264 * we implement that, but possibly shouldn't.
2266 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
2269 * Loop through the pages of the file, starting with the requested
2270 * offset. Get a file page (do I/O if necessary), map the file page
2271 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
2273 * This is done in two loops. The inner loop turns as many pages
2274 * as it can, up to available socket buffer space, without blocking
2275 * into mbufs to have it bulk delivered into the socket send buffer.
2276 * The outer loop checks the state and available space of the socket
2277 * and takes care of the overall progress.
2279 for (off = offset; ; ) {
2285 if ((nbytes != 0 && nbytes == fsbytes) ||
2286 (nbytes == 0 && obj_size == fsbytes))
2295 * Check the socket state for ongoing connection,
2296 * no errors and space in socket buffer.
2297 * If space is low allow for the remainder of the
2298 * file to be processed if it fits the socket buffer.
2299 * Otherwise block in waiting for sufficient space
2300 * to proceed, or if the socket is nonblocking, return
2301 * to userland with EAGAIN while reporting how far
2303 * We wait until the socket buffer has significant free
2304 * space to do bulk sends. This makes good use of file
2305 * system read ahead and allows packet segmentation
2306 * offloading hardware to take over lots of work. If
2307 * we were not careful here we would send off only one
2310 SOCKBUF_LOCK(&so->so_snd);
2311 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
2312 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
2314 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2316 SOCKBUF_UNLOCK(&so->so_snd);
2318 } else if (so->so_error) {
2319 error = so->so_error;
2321 SOCKBUF_UNLOCK(&so->so_snd);
2324 space = sbspace(&so->so_snd);
2327 space < so->so_snd.sb_lowat)) {
2328 if (so->so_state & SS_NBIO) {
2329 SOCKBUF_UNLOCK(&so->so_snd);
2334 * sbwait drops the lock while sleeping.
2335 * When we loop back to retry_space the
2336 * state may have changed and we retest
2339 error = sbwait(&so->so_snd);
2341 * An error from sbwait usually indicates that we've
2342 * been interrupted by a signal. If we've sent anything
2343 * then return bytes sent, otherwise return the error.
2346 SOCKBUF_UNLOCK(&so->so_snd);
2351 SOCKBUF_UNLOCK(&so->so_snd);
2354 * Reduce space in the socket buffer by the size of
2355 * the header mbuf chain.
2356 * hdrlen is set to 0 after the first loop.
2361 error = vn_lock(vp, LK_SHARED);
2364 error = VOP_GETATTR(vp, &va, td->td_ucred);
2365 if (error != 0 || off >= va.va_size) {
2369 obj_size = va.va_size;
2373 * Loop and construct maximum sized mbuf chain to be bulk
2374 * dumped into socket buffer.
2376 while (space > loopbytes) {
2381 * Calculate the amount to transfer.
2382 * Not to exceed a page, the EOF,
2383 * or the passed in nbytes.
2385 pgoff = (vm_offset_t)(off & PAGE_MASK);
2386 rem = obj_size - offset;
2388 rem = omin(rem, nbytes);
2389 rem -= fsbytes + loopbytes;
2390 xfsize = omin(PAGE_SIZE - pgoff, rem);
2391 xfsize = omin(space - loopbytes, xfsize);
2393 done = 1; /* all data sent */
2398 * Attempt to look up the page. Allocate
2399 * if not found or wait and loop if busy.
2402 nd = EAGAIN; /* send what we already got */
2403 else if ((flags & SF_NODISKIO) != 0)
2407 error = sendfile_readpage(obj, vp, nd, off,
2408 xfsize, bsize, td, &pg);
2410 if (error == EAGAIN)
2411 error = 0; /* not a real error */
2416 * Get a sendfile buf. When allocating the
2417 * first buffer for mbuf chain, we usually
2418 * wait as long as necessary, but this wait
2419 * can be interrupted. For consequent
2420 * buffers, do not sleep, since several
2421 * threads might exhaust the buffers and then
2424 sf = sf_buf_alloc(pg, (mnw || m != NULL) ? SFB_NOWAIT :
2427 SFSTAT_INC(sf_allocfail);
2429 vm_page_unwire(pg, 0);
2430 KASSERT(pg->object != NULL,
2431 ("%s: object disappeared", __func__));
2434 error = (mnw ? EAGAIN : EINTR);
2439 * Get an mbuf and set it up as having
2442 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2444 error = (mnw ? EAGAIN : ENOBUFS);
2445 (void)sf_buf_mext(NULL, NULL, sf);
2448 if (m_extadd(m0, (caddr_t )sf_buf_kva(sf), PAGE_SIZE,
2449 sf_buf_mext, sfs, sf, M_RDONLY, EXT_SFBUF,
2450 (mnw ? M_NOWAIT : M_WAITOK)) != 0) {
2451 error = (mnw ? EAGAIN : ENOBUFS);
2452 (void)sf_buf_mext(NULL, NULL, sf);
2456 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2459 /* Append to mbuf chain. */
2463 m_last(m)->m_next = m0;
2468 /* Keep track of bits processed. */
2469 loopbytes += xfsize;
2473 mtx_lock(&sfs->mtx);
2475 mtx_unlock(&sfs->mtx);
2482 /* Add the buffer chain to the socket buffer. */
2486 mlen = m_length(m, NULL);
2487 SOCKBUF_LOCK(&so->so_snd);
2488 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2490 SOCKBUF_UNLOCK(&so->so_snd);
2493 SOCKBUF_UNLOCK(&so->so_snd);
2494 CURVNET_SET(so->so_vnet);
2495 /* Avoid error aliasing. */
2496 err = (*so->so_proto->pr_usrreqs->pru_send)
2497 (so, 0, m, NULL, NULL, td);
2501 * We need two counters to get the
2502 * file offset and nbytes to send
2504 * - sbytes contains the total amount
2505 * of bytes sent, including headers.
2506 * - fsbytes contains the total amount
2507 * of bytes sent from the file.
2515 } else if (error == 0)
2517 m = NULL; /* pru_send always consumes */
2520 /* Quit outer loop on error or when we're done. */
2528 * Send trailers. Wimp out and use writev(2).
2530 if (trl_uio != NULL) {
2531 sbunlock(&so->so_snd);
2532 error = kern_writev(td, sockfd, trl_uio);
2534 sbytes += td->td_retval[0];
2539 sbunlock(&so->so_snd);
2542 * If there was no error we have to clear td->td_retval[0]
2543 * because it may have been set by writev.
2546 td->td_retval[0] = 0;
2549 copyout(&sbytes, sent, sizeof(off_t));
2552 vm_object_deallocate(obj);
2559 mtx_lock(&sfs->mtx);
2560 if (sfs->count != 0)
2561 cv_wait(&sfs->cv, &sfs->mtx);
2562 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2563 cv_destroy(&sfs->cv);
2564 mtx_destroy(&sfs->mtx);
2568 if (error == ERESTART)
2576 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2577 * otherwise all return EOPNOTSUPP.
2578 * XXX: We should make this loadable one day.
2581 sys_sctp_peeloff(td, uap)
2583 struct sctp_peeloff_args /* {
2588 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2589 struct file *nfp = NULL;
2590 struct socket *head, *so;
2591 cap_rights_t rights;
2595 AUDIT_ARG_FD(uap->sd);
2596 error = fgetsock(td, uap->sd, cap_rights_init(&rights, CAP_PEELOFF),
2600 if (head->so_proto->pr_protocol != IPPROTO_SCTP) {
2604 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2608 * At this point we know we do have a assoc to pull
2609 * we proceed to get the fd setup. This may block
2613 error = falloc(td, &nfp, &fd, 0);
2616 td->td_retval[0] = fd;
2618 CURVNET_SET(head->so_vnet);
2619 so = sonewconn(head, SS_ISCONNECTED);
2625 * Before changing the flags on the socket, we have to bump the
2626 * reference count. Otherwise, if the protocol calls sofree(),
2627 * the socket will be released due to a zero refcount.
2630 soref(so); /* file descriptor reference */
2635 TAILQ_REMOVE(&head->so_comp, so, so_list);
2637 so->so_state |= (head->so_state & SS_NBIO);
2638 so->so_state &= ~SS_NOFDREF;
2639 so->so_qstate &= ~SQ_COMP;
2642 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2643 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2646 if (head->so_sigio != NULL)
2647 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2651 * close the new descriptor, assuming someone hasn't ripped it
2652 * out from under us.
2655 fdclose(td->td_proc->p_fd, nfp, fd, td);
2658 * Release explicitly held references before returning.
2668 return (EOPNOTSUPP);
2673 sys_sctp_generic_sendmsg (td, uap)
2675 struct sctp_generic_sendmsg_args /* {
2681 struct sctp_sndrcvinfo *sinfo,
2685 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2686 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2688 struct file *fp = NULL;
2689 struct sockaddr *to = NULL;
2691 struct uio *ktruio = NULL;
2694 struct iovec iov[1];
2695 cap_rights_t rights;
2698 if (uap->sinfo != NULL) {
2699 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2705 cap_rights_init(&rights, CAP_SEND);
2706 if (uap->tolen != 0) {
2707 error = getsockaddr(&to, uap->to, uap->tolen);
2712 cap_rights_set(&rights, CAP_CONNECT);
2715 AUDIT_ARG_FD(uap->sd);
2716 error = getsock_cap(td->td_proc->p_fd, uap->sd, &rights, &fp, NULL);
2720 if (to && (KTRPOINT(td, KTR_STRUCT)))
2724 iov[0].iov_base = uap->msg;
2725 iov[0].iov_len = uap->mlen;
2727 so = (struct socket *)fp->f_data;
2728 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2733 error = mac_socket_check_send(td->td_ucred, so);
2739 auio.uio_iovcnt = 1;
2740 auio.uio_segflg = UIO_USERSPACE;
2741 auio.uio_rw = UIO_WRITE;
2743 auio.uio_offset = 0; /* XXX */
2745 len = auio.uio_resid = uap->mlen;
2746 CURVNET_SET(so->so_vnet);
2747 error = sctp_lower_sosend(so, to, &auio, (struct mbuf *)NULL,
2748 (struct mbuf *)NULL, uap->flags, u_sinfo, td);
2751 if (auio.uio_resid != len && (error == ERESTART ||
2752 error == EINTR || error == EWOULDBLOCK))
2754 /* Generation of SIGPIPE can be controlled per socket. */
2755 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2756 !(uap->flags & MSG_NOSIGNAL)) {
2757 PROC_LOCK(td->td_proc);
2758 tdsignal(td, SIGPIPE);
2759 PROC_UNLOCK(td->td_proc);
2763 td->td_retval[0] = len - auio.uio_resid;
2765 if (ktruio != NULL) {
2766 ktruio->uio_resid = td->td_retval[0];
2767 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2777 return (EOPNOTSUPP);
2782 sys_sctp_generic_sendmsg_iov(td, uap)
2784 struct sctp_generic_sendmsg_iov_args /* {
2790 struct sctp_sndrcvinfo *sinfo,
2794 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2795 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2797 struct file *fp = NULL;
2798 struct sockaddr *to = NULL;
2800 struct uio *ktruio = NULL;
2803 struct iovec *iov, *tiov;
2804 cap_rights_t rights;
2808 if (uap->sinfo != NULL) {
2809 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2814 cap_rights_init(&rights, CAP_SEND);
2815 if (uap->tolen != 0) {
2816 error = getsockaddr(&to, uap->to, uap->tolen);
2821 cap_rights_set(&rights, CAP_CONNECT);
2824 AUDIT_ARG_FD(uap->sd);
2825 error = getsock_cap(td->td_proc->p_fd, uap->sd, &rights, &fp, NULL);
2829 #ifdef COMPAT_FREEBSD32
2830 if (SV_CURPROC_FLAG(SV_ILP32))
2831 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2832 uap->iovlen, &iov, EMSGSIZE);
2835 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2839 if (to && (KTRPOINT(td, KTR_STRUCT)))
2843 so = (struct socket *)fp->f_data;
2844 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2849 error = mac_socket_check_send(td->td_ucred, so);
2855 auio.uio_iovcnt = uap->iovlen;
2856 auio.uio_segflg = UIO_USERSPACE;
2857 auio.uio_rw = UIO_WRITE;
2859 auio.uio_offset = 0; /* XXX */
2862 for (i = 0; i <uap->iovlen; i++, tiov++) {
2863 if ((auio.uio_resid += tiov->iov_len) < 0) {
2868 len = auio.uio_resid;
2869 CURVNET_SET(so->so_vnet);
2870 error = sctp_lower_sosend(so, to, &auio,
2871 (struct mbuf *)NULL, (struct mbuf *)NULL,
2872 uap->flags, u_sinfo, td);
2875 if (auio.uio_resid != len && (error == ERESTART ||
2876 error == EINTR || error == EWOULDBLOCK))
2878 /* Generation of SIGPIPE can be controlled per socket */
2879 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2880 !(uap->flags & MSG_NOSIGNAL)) {
2881 PROC_LOCK(td->td_proc);
2882 tdsignal(td, SIGPIPE);
2883 PROC_UNLOCK(td->td_proc);
2887 td->td_retval[0] = len - auio.uio_resid;
2889 if (ktruio != NULL) {
2890 ktruio->uio_resid = td->td_retval[0];
2891 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2903 return (EOPNOTSUPP);
2908 sys_sctp_generic_recvmsg(td, uap)
2910 struct sctp_generic_recvmsg_args /* {
2914 struct sockaddr *from,
2915 __socklen_t *fromlenaddr,
2916 struct sctp_sndrcvinfo *sinfo,
2920 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2921 uint8_t sockbufstore[256];
2923 struct iovec *iov, *tiov;
2924 struct sctp_sndrcvinfo sinfo;
2926 struct file *fp = NULL;
2927 struct sockaddr *fromsa;
2928 cap_rights_t rights;
2930 struct uio *ktruio = NULL;
2933 int error, fromlen, i, msg_flags;
2935 AUDIT_ARG_FD(uap->sd);
2936 error = getsock_cap(td->td_proc->p_fd, uap->sd,
2937 cap_rights_init(&rights, CAP_RECV), &fp, NULL);
2940 #ifdef COMPAT_FREEBSD32
2941 if (SV_CURPROC_FLAG(SV_ILP32))
2942 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2943 uap->iovlen, &iov, EMSGSIZE);
2946 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2951 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2956 error = mac_socket_check_receive(td->td_ucred, so);
2961 if (uap->fromlenaddr != NULL) {
2962 error = copyin(uap->fromlenaddr, &fromlen, sizeof (fromlen));
2968 if (uap->msg_flags) {
2969 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2976 auio.uio_iovcnt = uap->iovlen;
2977 auio.uio_segflg = UIO_USERSPACE;
2978 auio.uio_rw = UIO_READ;
2980 auio.uio_offset = 0; /* XXX */
2983 for (i = 0; i <uap->iovlen; i++, tiov++) {
2984 if ((auio.uio_resid += tiov->iov_len) < 0) {
2989 len = auio.uio_resid;
2990 fromsa = (struct sockaddr *)sockbufstore;
2993 if (KTRPOINT(td, KTR_GENIO))
2994 ktruio = cloneuio(&auio);
2996 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
2997 CURVNET_SET(so->so_vnet);
2998 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2999 fromsa, fromlen, &msg_flags,
3000 (struct sctp_sndrcvinfo *)&sinfo, 1);
3003 if (auio.uio_resid != len && (error == ERESTART ||
3004 error == EINTR || error == EWOULDBLOCK))
3008 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
3011 if (ktruio != NULL) {
3012 ktruio->uio_resid = len - auio.uio_resid;
3013 ktrgenio(uap->sd, UIO_READ, ktruio, error);
3018 td->td_retval[0] = len - auio.uio_resid;
3020 if (fromlen && uap->from) {
3022 if (len <= 0 || fromsa == 0)
3025 len = MIN(len, fromsa->sa_len);
3026 error = copyout(fromsa, uap->from, (size_t)len);
3030 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
3035 if (KTRPOINT(td, KTR_STRUCT))
3036 ktrsockaddr(fromsa);
3038 if (uap->msg_flags) {
3039 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
3051 return (EOPNOTSUPP);