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/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>
60 #include <sys/mount.h>
62 #include <sys/protosw.h>
63 #include <sys/rwlock.h>
64 #include <sys/sf_buf.h>
65 #include <sys/sysent.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
68 #include <sys/signalvar.h>
69 #include <sys/syscallsubr.h>
70 #include <sys/sysctl.h>
72 #include <sys/vnode.h>
74 #include <sys/ktrace.h>
76 #ifdef COMPAT_FREEBSD32
77 #include <compat/freebsd32/freebsd32_util.h>
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
86 #include <vm/vm_param.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_page.h>
89 #include <vm/vm_pageout.h>
90 #include <vm/vm_kern.h>
91 #include <vm/vm_extern.h>
93 #if defined(INET) || defined(INET6)
95 #include <netinet/sctp.h>
96 #include <netinet/sctp_peeloff.h>
98 #endif /* INET || INET6 */
101 * Flags for accept1() and kern_accept4(), in addition to SOCK_CLOEXEC
104 #define ACCEPT4_INHERIT 0x1
105 #define ACCEPT4_COMPAT 0x2
107 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
108 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
110 static int accept1(struct thread *td, int s, struct sockaddr *uname,
111 socklen_t *anamelen, int flags);
112 static int do_sendfile(struct thread *td, struct sendfile_args *uap,
114 static int getsockname1(struct thread *td, struct getsockname_args *uap,
116 static int getpeername1(struct thread *td, struct getpeername_args *uap,
119 counter_u64_t sfstat[sizeof(struct sfstat) / sizeof(uint64_t)];
122 * sendfile(2)-related variables and associated sysctls
127 static int sfreadahead = 1;
129 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
130 "Maximum number of sendfile(2) sf_bufs available");
131 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
132 "Number of sendfile(2) sf_bufs at peak usage");
133 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
134 "Number of sendfile(2) sf_bufs in use");
135 SYSCTL_INT(_kern_ipc, OID_AUTO, sfreadahead, CTLFLAG_RW, &sfreadahead, 0,
136 "Number of sendfile(2) read-ahead MAXBSIZE blocks");
140 sfstat_init(const void *unused)
143 COUNTER_ARRAY_ALLOC(sfstat, sizeof(struct sfstat) / sizeof(uint64_t),
146 SYSINIT(sfstat, SI_SUB_MBUF, SI_ORDER_FIRST, sfstat_init, NULL);
149 sfstat_sysctl(SYSCTL_HANDLER_ARGS)
153 COUNTER_ARRAY_COPY(sfstat, &s, sizeof(s) / sizeof(uint64_t));
155 COUNTER_ARRAY_ZERO(sfstat, sizeof(s) / sizeof(uint64_t));
156 return (SYSCTL_OUT(req, &s, sizeof(s)));
158 SYSCTL_PROC(_kern_ipc, OID_AUTO, sfstat, CTLTYPE_OPAQUE | CTLFLAG_RW,
159 NULL, 0, sfstat_sysctl, "I", "sendfile statistics");
162 * Convert a user file descriptor to a kernel file entry and check if required
163 * capability rights are present.
164 * A reference on the file entry is held upon returning.
167 getsock_cap(struct filedesc *fdp, int fd, cap_rights_t rights,
168 struct file **fpp, u_int *fflagp)
173 error = fget_unlocked(fdp, fd, rights, 0, &fp, NULL);
176 if (fp->f_type != DTYPE_SOCKET) {
177 fdrop(fp, curthread);
181 *fflagp = fp->f_flag;
187 * System call interface to the socket abstraction.
189 #if defined(COMPAT_43)
190 #define COMPAT_OLDSOCK
196 struct socket_args /* {
204 int fd, error, type, oflag, fflag;
206 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
211 if ((type & SOCK_CLOEXEC) != 0) {
212 type &= ~SOCK_CLOEXEC;
215 if ((type & SOCK_NONBLOCK) != 0) {
216 type &= ~SOCK_NONBLOCK;
221 error = mac_socket_check_create(td->td_ucred, uap->domain, type,
226 error = falloc(td, &fp, &fd, oflag);
229 /* An extra reference on `fp' has been held for us by falloc(). */
230 error = socreate(uap->domain, &so, type, uap->protocol,
233 fdclose(td->td_proc->p_fd, fp, fd, td);
235 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
236 if ((fflag & FNONBLOCK) != 0)
237 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
238 td->td_retval[0] = fd;
248 struct bind_args /* {
257 error = getsockaddr(&sa, uap->name, uap->namelen);
259 error = kern_bind(td, uap->s, sa);
266 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
273 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
274 error = getsock_cap(td->td_proc->p_fd, fd, CAP_BIND, &fp, NULL);
279 if (KTRPOINT(td, KTR_STRUCT))
283 error = mac_socket_check_bind(td->td_ucred, so, sa);
286 if (dirfd == AT_FDCWD)
287 error = sobind(so, sa, td);
289 error = sobindat(dirfd, so, sa, td);
298 kern_bind(struct thread *td, int fd, struct sockaddr *sa)
301 return (kern_bindat(td, AT_FDCWD, fd, sa));
308 struct bindat_args /* {
318 error = getsockaddr(&sa, uap->name, uap->namelen);
320 error = kern_bindat(td, uap->fd, uap->s, sa);
330 struct listen_args /* {
339 AUDIT_ARG_FD(uap->s);
340 error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_LISTEN, &fp, NULL);
344 error = mac_socket_check_listen(td->td_ucred, so);
347 error = solisten(so, uap->backlog, td);
357 accept1(td, s, uname, anamelen, flags)
360 struct sockaddr *uname;
364 struct sockaddr *name;
370 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
372 error = copyin(anamelen, &namelen, sizeof (namelen));
376 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
379 * return a namelen of zero for older code which might
380 * ignore the return value from accept.
383 (void) copyout(&namelen, anamelen, sizeof(*anamelen));
387 if (error == 0 && uname != NULL) {
388 #ifdef COMPAT_OLDSOCK
389 if (flags & ACCEPT4_COMPAT)
390 ((struct osockaddr *)name)->sa_family =
393 error = copyout(name, uname, namelen);
396 error = copyout(&namelen, anamelen,
399 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
401 free(name, M_SONAME);
406 kern_accept(struct thread *td, int s, struct sockaddr **name,
407 socklen_t *namelen, struct file **fp)
409 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
413 kern_accept4(struct thread *td, int s, struct sockaddr **name,
414 socklen_t *namelen, int flags, struct file **fp)
416 struct filedesc *fdp;
417 struct file *headfp, *nfp = NULL;
418 struct sockaddr *sa = NULL;
420 struct socket *head, *so;
430 fdp = td->td_proc->p_fd;
431 error = getsock_cap(fdp, s, CAP_ACCEPT, &headfp, &fflag);
434 head = headfp->f_data;
435 if ((head->so_options & SO_ACCEPTCONN) == 0) {
440 error = mac_socket_check_accept(td->td_ucred, head);
444 error = falloc(td, &nfp, &fd, (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0);
448 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
453 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
454 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
455 head->so_error = ECONNABORTED;
458 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
465 if (head->so_error) {
466 error = head->so_error;
471 so = TAILQ_FIRST(&head->so_comp);
472 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
473 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
476 * Before changing the flags on the socket, we have to bump the
477 * reference count. Otherwise, if the protocol calls sofree(),
478 * the socket will be released due to a zero refcount.
480 SOCK_LOCK(so); /* soref() and so_state update */
481 soref(so); /* file descriptor reference */
483 TAILQ_REMOVE(&head->so_comp, so, so_list);
485 if (flags & ACCEPT4_INHERIT)
486 so->so_state |= (head->so_state & SS_NBIO);
488 so->so_state |= (flags & SOCK_NONBLOCK) ? SS_NBIO : 0;
489 so->so_qstate &= ~SQ_COMP;
495 /* An extra reference on `nfp' has been held for us by falloc(). */
496 td->td_retval[0] = fd;
498 /* connection has been removed from the listen queue */
499 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
501 if (flags & ACCEPT4_INHERIT) {
502 pgid = fgetown(&head->so_sigio);
504 fsetown(pgid, &so->so_sigio);
506 fflag &= ~(FNONBLOCK | FASYNC);
507 if (flags & SOCK_NONBLOCK)
511 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
512 /* Sync socket nonblocking/async state with file flags */
513 tmp = fflag & FNONBLOCK;
514 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
515 tmp = fflag & FASYNC;
516 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
518 error = soaccept(so, &sa);
521 * return a namelen of zero for older code which might
522 * ignore the return value from accept.
533 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
535 /* check sa_len before it is destroyed */
536 if (*namelen > sa->sa_len)
537 *namelen = sa->sa_len;
539 if (KTRPOINT(td, KTR_STRUCT))
550 * close the new descriptor, assuming someone hasn't ripped it
554 fdclose(fdp, nfp, fd, td);
557 * Release explicitly held references before returning. We return
558 * a reference on nfp to the caller on success if they request it.
577 struct accept_args *uap;
580 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
586 struct accept4_args *uap;
588 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
591 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
594 #ifdef COMPAT_OLDSOCK
598 struct accept_args *uap;
601 return (accept1(td, uap->s, uap->name, uap->anamelen,
602 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
604 #endif /* COMPAT_OLDSOCK */
610 struct connect_args /* {
619 error = getsockaddr(&sa, uap->name, uap->namelen);
621 error = kern_connect(td, uap->s, sa);
628 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
636 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
637 error = getsock_cap(td->td_proc->p_fd, fd, CAP_CONNECT, &fp, NULL);
641 if (so->so_state & SS_ISCONNECTING) {
646 if (KTRPOINT(td, KTR_STRUCT))
650 error = mac_socket_check_connect(td->td_ucred, so, sa);
654 if (dirfd == AT_FDCWD)
655 error = soconnect(so, sa, td);
657 error = soconnectat(dirfd, so, sa, td);
660 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
665 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
666 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
669 if (error == EINTR || error == ERESTART)
675 error = so->so_error;
681 so->so_state &= ~SS_ISCONNECTING;
682 if (error == ERESTART)
690 kern_connect(struct thread *td, int fd, struct sockaddr *sa)
693 return (kern_connectat(td, AT_FDCWD, fd, sa));
698 sys_connectat(td, uap)
700 struct connectat_args /* {
710 error = getsockaddr(&sa, uap->name, uap->namelen);
712 error = kern_connectat(td, uap->fd, uap->s, sa);
719 kern_socketpair(struct thread *td, int domain, int type, int protocol,
722 struct filedesc *fdp = td->td_proc->p_fd;
723 struct file *fp1, *fp2;
724 struct socket *so1, *so2;
725 int fd, error, oflag, fflag;
727 AUDIT_ARG_SOCKET(domain, type, protocol);
731 if ((type & SOCK_CLOEXEC) != 0) {
732 type &= ~SOCK_CLOEXEC;
735 if ((type & SOCK_NONBLOCK) != 0) {
736 type &= ~SOCK_NONBLOCK;
740 /* We might want to have a separate check for socket pairs. */
741 error = mac_socket_check_create(td->td_ucred, domain, type,
746 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
749 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
752 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
753 error = falloc(td, &fp1, &fd, oflag);
757 fp1->f_data = so1; /* so1 already has ref count */
758 error = falloc(td, &fp2, &fd, oflag);
761 fp2->f_data = so2; /* so2 already has ref count */
763 error = soconnect2(so1, so2);
766 if (type == SOCK_DGRAM) {
768 * Datagram socket connection is asymmetric.
770 error = soconnect2(so2, so1);
774 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
776 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
778 if ((fflag & FNONBLOCK) != 0) {
779 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
780 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
786 fdclose(fdp, fp2, rsv[1], td);
789 fdclose(fdp, fp1, rsv[0], td);
801 sys_socketpair(struct thread *td, struct socketpair_args *uap)
805 error = kern_socketpair(td, uap->domain, uap->type,
809 error = copyout(sv, uap->rsv, 2 * sizeof(int));
811 (void)kern_close(td, sv[0]);
812 (void)kern_close(td, sv[1]);
818 sendit(td, s, mp, flags)
824 struct mbuf *control;
828 #ifdef CAPABILITY_MODE
829 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
833 if (mp->msg_name != NULL) {
834 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
844 if (mp->msg_control) {
845 if (mp->msg_controllen < sizeof(struct cmsghdr)
846 #ifdef COMPAT_OLDSOCK
847 && mp->msg_flags != MSG_COMPAT
853 error = sockargs(&control, mp->msg_control,
854 mp->msg_controllen, MT_CONTROL);
857 #ifdef COMPAT_OLDSOCK
858 if (mp->msg_flags == MSG_COMPAT) {
861 M_PREPEND(control, sizeof(*cm), M_WAITOK);
862 cm = mtod(control, struct cmsghdr *);
863 cm->cmsg_len = control->m_len;
864 cm->cmsg_level = SOL_SOCKET;
865 cm->cmsg_type = SCM_RIGHTS;
872 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
881 kern_sendit(td, s, mp, flags, control, segflg)
886 struct mbuf *control;
897 struct uio *ktruio = NULL;
902 if (mp->msg_name != NULL) {
903 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
904 rights |= CAP_CONNECT;
906 error = getsock_cap(td->td_proc->p_fd, s, rights, &fp, NULL);
909 so = (struct socket *)fp->f_data;
912 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
913 ktrsockaddr(mp->msg_name);
916 if (mp->msg_name != NULL) {
917 error = mac_socket_check_connect(td->td_ucred, so,
922 error = mac_socket_check_send(td->td_ucred, so);
927 auio.uio_iov = mp->msg_iov;
928 auio.uio_iovcnt = mp->msg_iovlen;
929 auio.uio_segflg = segflg;
930 auio.uio_rw = UIO_WRITE;
932 auio.uio_offset = 0; /* XXX */
935 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
936 if ((auio.uio_resid += iov->iov_len) < 0) {
942 if (KTRPOINT(td, KTR_GENIO))
943 ktruio = cloneuio(&auio);
945 len = auio.uio_resid;
946 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
948 if (auio.uio_resid != len && (error == ERESTART ||
949 error == EINTR || error == EWOULDBLOCK))
951 /* Generation of SIGPIPE can be controlled per socket */
952 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
953 !(flags & MSG_NOSIGNAL)) {
954 PROC_LOCK(td->td_proc);
955 tdsignal(td, SIGPIPE);
956 PROC_UNLOCK(td->td_proc);
960 td->td_retval[0] = len - auio.uio_resid;
962 if (ktruio != NULL) {
963 ktruio->uio_resid = td->td_retval[0];
964 ktrgenio(s, UIO_WRITE, ktruio, error);
975 struct sendto_args /* {
988 msg.msg_name = uap->to;
989 msg.msg_namelen = uap->tolen;
993 #ifdef COMPAT_OLDSOCK
996 aiov.iov_base = uap->buf;
997 aiov.iov_len = uap->len;
998 error = sendit(td, uap->s, &msg, uap->flags);
1002 #ifdef COMPAT_OLDSOCK
1006 struct osend_args /* {
1018 msg.msg_namelen = 0;
1019 msg.msg_iov = &aiov;
1021 aiov.iov_base = uap->buf;
1022 aiov.iov_len = uap->len;
1023 msg.msg_control = 0;
1025 error = sendit(td, uap->s, &msg, uap->flags);
1032 struct osendmsg_args /* {
1042 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1045 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1049 msg.msg_flags = MSG_COMPAT;
1050 error = sendit(td, uap->s, &msg, uap->flags);
1057 sys_sendmsg(td, uap)
1059 struct sendmsg_args /* {
1069 error = copyin(uap->msg, &msg, sizeof (msg));
1072 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1076 #ifdef COMPAT_OLDSOCK
1079 error = sendit(td, uap->s, &msg, uap->flags);
1085 kern_recvit(td, s, mp, fromseg, controlp)
1089 enum uio_seg fromseg;
1090 struct mbuf **controlp;
1097 struct mbuf *m, *control = NULL;
1101 struct sockaddr *fromsa = NULL;
1103 struct uio *ktruio = NULL;
1106 if (controlp != NULL)
1110 error = getsock_cap(td->td_proc->p_fd, s, CAP_RECV, &fp, NULL);
1116 error = mac_socket_check_receive(td->td_ucred, so);
1123 auio.uio_iov = mp->msg_iov;
1124 auio.uio_iovcnt = mp->msg_iovlen;
1125 auio.uio_segflg = UIO_USERSPACE;
1126 auio.uio_rw = UIO_READ;
1128 auio.uio_offset = 0; /* XXX */
1131 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
1132 if ((auio.uio_resid += iov->iov_len) < 0) {
1138 if (KTRPOINT(td, KTR_GENIO))
1139 ktruio = cloneuio(&auio);
1141 len = auio.uio_resid;
1142 error = soreceive(so, &fromsa, &auio, NULL,
1143 (mp->msg_control || controlp) ? &control : NULL,
1146 if (auio.uio_resid != len && (error == ERESTART ||
1147 error == EINTR || error == EWOULDBLOCK))
1151 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
1153 if (ktruio != NULL) {
1154 ktruio->uio_resid = len - auio.uio_resid;
1155 ktrgenio(s, UIO_READ, ktruio, error);
1160 td->td_retval[0] = len - auio.uio_resid;
1162 len = mp->msg_namelen;
1163 if (len <= 0 || fromsa == NULL)
1166 /* save sa_len before it is destroyed by MSG_COMPAT */
1167 len = MIN(len, fromsa->sa_len);
1168 #ifdef COMPAT_OLDSOCK
1169 if (mp->msg_flags & MSG_COMPAT)
1170 ((struct osockaddr *)fromsa)->sa_family =
1173 if (fromseg == UIO_USERSPACE) {
1174 error = copyout(fromsa, mp->msg_name,
1179 bcopy(fromsa, mp->msg_name, len);
1181 mp->msg_namelen = len;
1183 if (mp->msg_control && controlp == NULL) {
1184 #ifdef COMPAT_OLDSOCK
1186 * We assume that old recvmsg calls won't receive access
1187 * rights and other control info, esp. as control info
1188 * is always optional and those options didn't exist in 4.3.
1189 * If we receive rights, trim the cmsghdr; anything else
1192 if (control && mp->msg_flags & MSG_COMPAT) {
1193 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1195 mtod(control, struct cmsghdr *)->cmsg_type !=
1197 mp->msg_controllen = 0;
1200 control->m_len -= sizeof (struct cmsghdr);
1201 control->m_data += sizeof (struct cmsghdr);
1204 len = mp->msg_controllen;
1206 mp->msg_controllen = 0;
1207 ctlbuf = mp->msg_control;
1209 while (m && len > 0) {
1210 unsigned int tocopy;
1212 if (len >= m->m_len)
1215 mp->msg_flags |= MSG_CTRUNC;
1219 if ((error = copyout(mtod(m, caddr_t),
1220 ctlbuf, tocopy)) != 0)
1227 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1232 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1233 ktrsockaddr(fromsa);
1236 free(fromsa, M_SONAME);
1238 if (error == 0 && controlp != NULL)
1239 *controlp = control;
1247 recvit(td, s, mp, namelenp)
1255 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1259 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1260 #ifdef COMPAT_OLDSOCK
1261 if (mp->msg_flags & MSG_COMPAT)
1262 error = 0; /* old recvfrom didn't check */
1269 sys_recvfrom(td, uap)
1271 struct recvfrom_args /* {
1276 struct sockaddr * __restrict from;
1277 socklen_t * __restrict fromlenaddr;
1284 if (uap->fromlenaddr) {
1285 error = copyin(uap->fromlenaddr,
1286 &msg.msg_namelen, sizeof (msg.msg_namelen));
1290 msg.msg_namelen = 0;
1292 msg.msg_name = uap->from;
1293 msg.msg_iov = &aiov;
1295 aiov.iov_base = uap->buf;
1296 aiov.iov_len = uap->len;
1297 msg.msg_control = 0;
1298 msg.msg_flags = uap->flags;
1299 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1304 #ifdef COMPAT_OLDSOCK
1308 struct recvfrom_args *uap;
1311 uap->flags |= MSG_COMPAT;
1312 return (sys_recvfrom(td, uap));
1316 #ifdef COMPAT_OLDSOCK
1320 struct orecv_args /* {
1332 msg.msg_namelen = 0;
1333 msg.msg_iov = &aiov;
1335 aiov.iov_base = uap->buf;
1336 aiov.iov_len = uap->len;
1337 msg.msg_control = 0;
1338 msg.msg_flags = uap->flags;
1339 error = recvit(td, uap->s, &msg, NULL);
1344 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1345 * overlays the new one, missing only the flags, and with the (old) access
1346 * rights where the control fields are now.
1351 struct orecvmsg_args /* {
1353 struct omsghdr *msg;
1361 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1364 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1367 msg.msg_flags = uap->flags | MSG_COMPAT;
1369 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1370 if (msg.msg_controllen && error == 0)
1371 error = copyout(&msg.msg_controllen,
1372 &uap->msg->msg_accrightslen, sizeof (int));
1379 sys_recvmsg(td, uap)
1381 struct recvmsg_args /* {
1388 struct iovec *uiov, *iov;
1391 error = copyin(uap->msg, &msg, sizeof (msg));
1394 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1397 msg.msg_flags = uap->flags;
1398 #ifdef COMPAT_OLDSOCK
1399 msg.msg_flags &= ~MSG_COMPAT;
1403 error = recvit(td, uap->s, &msg, NULL);
1406 error = copyout(&msg, uap->msg, sizeof(msg));
1414 sys_shutdown(td, uap)
1416 struct shutdown_args /* {
1425 AUDIT_ARG_FD(uap->s);
1426 error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_SHUTDOWN, &fp,
1430 error = soshutdown(so, uap->how);
1438 sys_setsockopt(td, uap)
1440 struct setsockopt_args /* {
1449 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1450 uap->val, UIO_USERSPACE, uap->valsize));
1454 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1460 enum uio_seg valseg;
1466 struct sockopt sopt;
1468 if (val == NULL && valsize != 0)
1470 if ((int)valsize < 0)
1473 sopt.sopt_dir = SOPT_SET;
1474 sopt.sopt_level = level;
1475 sopt.sopt_name = name;
1476 sopt.sopt_val = val;
1477 sopt.sopt_valsize = valsize;
1483 sopt.sopt_td = NULL;
1486 panic("kern_setsockopt called with bad valseg");
1490 error = getsock_cap(td->td_proc->p_fd, s, CAP_SETSOCKOPT, &fp, NULL);
1493 error = sosetopt(so, &sopt);
1501 sys_getsockopt(td, uap)
1503 struct getsockopt_args /* {
1507 void * __restrict val;
1508 socklen_t * __restrict avalsize;
1515 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1520 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1521 uap->val, UIO_USERSPACE, &valsize);
1524 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1529 * Kernel version of getsockopt.
1530 * optval can be a userland or userspace. optlen is always a kernel pointer.
1533 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1539 enum uio_seg valseg;
1545 struct sockopt sopt;
1549 if ((int)*valsize < 0)
1552 sopt.sopt_dir = SOPT_GET;
1553 sopt.sopt_level = level;
1554 sopt.sopt_name = name;
1555 sopt.sopt_val = val;
1556 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1562 sopt.sopt_td = NULL;
1565 panic("kern_getsockopt called with bad valseg");
1569 error = getsock_cap(td->td_proc->p_fd, s, CAP_GETSOCKOPT, &fp, NULL);
1572 error = sogetopt(so, &sopt);
1573 *valsize = sopt.sopt_valsize;
1580 * getsockname1() - Get socket name.
1584 getsockname1(td, uap, compat)
1586 struct getsockname_args /* {
1588 struct sockaddr * __restrict asa;
1589 socklen_t * __restrict alen;
1593 struct sockaddr *sa;
1597 error = copyin(uap->alen, &len, sizeof(len));
1601 error = kern_getsockname(td, uap->fdes, &sa, &len);
1606 #ifdef COMPAT_OLDSOCK
1608 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1610 error = copyout(sa, uap->asa, (u_int)len);
1614 error = copyout(&len, uap->alen, sizeof(len));
1619 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1628 error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETSOCKNAME, &fp, NULL);
1633 CURVNET_SET(so->so_vnet);
1634 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1641 len = MIN(*alen, (*sa)->sa_len);
1644 if (KTRPOINT(td, KTR_STRUCT))
1650 free(*sa, M_SONAME);
1657 sys_getsockname(td, uap)
1659 struct getsockname_args *uap;
1662 return (getsockname1(td, uap, 0));
1665 #ifdef COMPAT_OLDSOCK
1667 ogetsockname(td, uap)
1669 struct getsockname_args *uap;
1672 return (getsockname1(td, uap, 1));
1674 #endif /* COMPAT_OLDSOCK */
1677 * getpeername1() - Get name of peer for connected socket.
1681 getpeername1(td, uap, compat)
1683 struct getpeername_args /* {
1685 struct sockaddr * __restrict asa;
1686 socklen_t * __restrict alen;
1690 struct sockaddr *sa;
1694 error = copyin(uap->alen, &len, sizeof (len));
1698 error = kern_getpeername(td, uap->fdes, &sa, &len);
1703 #ifdef COMPAT_OLDSOCK
1705 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1707 error = copyout(sa, uap->asa, (u_int)len);
1711 error = copyout(&len, uap->alen, sizeof(len));
1716 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1725 error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETPEERNAME, &fp, NULL);
1729 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1734 CURVNET_SET(so->so_vnet);
1735 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1742 len = MIN(*alen, (*sa)->sa_len);
1745 if (KTRPOINT(td, KTR_STRUCT))
1750 free(*sa, M_SONAME);
1759 sys_getpeername(td, uap)
1761 struct getpeername_args *uap;
1764 return (getpeername1(td, uap, 0));
1767 #ifdef COMPAT_OLDSOCK
1769 ogetpeername(td, uap)
1771 struct ogetpeername_args *uap;
1774 /* XXX uap should have type `getpeername_args *' to begin with. */
1775 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1777 #endif /* COMPAT_OLDSOCK */
1780 sockargs(mp, buf, buflen, type)
1785 struct sockaddr *sa;
1789 if (buflen > MLEN) {
1790 #ifdef COMPAT_OLDSOCK
1791 if (type == MT_SONAME && buflen <= 112)
1792 buflen = MLEN; /* unix domain compat. hack */
1795 if (buflen > MCLBYTES)
1798 m = m_get2(buflen, M_WAITOK, type, 0);
1800 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1805 if (type == MT_SONAME) {
1806 sa = mtod(m, struct sockaddr *);
1808 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1809 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1810 sa->sa_family = sa->sa_len;
1812 sa->sa_len = buflen;
1819 getsockaddr(namp, uaddr, len)
1820 struct sockaddr **namp;
1824 struct sockaddr *sa;
1827 if (len > SOCK_MAXADDRLEN)
1828 return (ENAMETOOLONG);
1829 if (len < offsetof(struct sockaddr, sa_data[0]))
1831 sa = malloc(len, M_SONAME, M_WAITOK);
1832 error = copyin(uaddr, sa, len);
1836 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1837 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1838 sa->sa_family = sa->sa_len;
1846 #include <sys/condvar.h>
1848 struct sendfile_sync {
1855 * Detach mapped page and release resources back to the system.
1858 sf_buf_mext(void *addr, void *args)
1861 struct sendfile_sync *sfs;
1863 m = sf_buf_page(args);
1866 vm_page_unwire(m, 0);
1868 * Check for the object going away on us. This can
1869 * happen since we don't hold a reference to it.
1870 * If so, we're responsible for freeing the page.
1872 if (m->wire_count == 0 && m->object == NULL)
1878 mtx_lock(&sfs->mtx);
1879 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1880 if (--sfs->count == 0)
1881 cv_signal(&sfs->cv);
1882 mtx_unlock(&sfs->mtx);
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;
1911 if (uap->offset < 0)
1914 hdr_uio = trl_uio = NULL;
1916 if (uap->hdtr != NULL) {
1917 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1920 if (hdtr.headers != NULL) {
1921 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1925 if (hdtr.trailers != NULL) {
1926 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1933 AUDIT_ARG_FD(uap->fd);
1936 * sendfile(2) can start at any offset within a file so we require
1937 * CAP_READ+CAP_SEEK = CAP_PREAD.
1939 if ((error = fget_read(td, uap->fd, CAP_PREAD, &fp)) != 0)
1942 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, uap->offset,
1943 uap->nbytes, uap->sbytes, uap->flags, compat ? SFK_COMPAT : 0, td);
1948 free(hdr_uio, M_IOV);
1950 free(trl_uio, M_IOV);
1954 #ifdef COMPAT_FREEBSD4
1956 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1958 struct sendfile_args args;
1962 args.offset = uap->offset;
1963 args.nbytes = uap->nbytes;
1964 args.hdtr = uap->hdtr;
1965 args.sbytes = uap->sbytes;
1966 args.flags = uap->flags;
1968 return (do_sendfile(td, &args, 1));
1970 #endif /* COMPAT_FREEBSD4 */
1973 vn_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio,
1974 struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags,
1975 int kflags, struct thread *td)
1977 struct vnode *vp = fp->f_vnode;
1978 struct file *sock_fp;
1979 struct vm_object *obj = NULL;
1980 struct socket *so = NULL;
1981 struct mbuf *m = NULL;
1985 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1986 int error, hdrlen = 0, mnw = 0;
1988 struct sendfile_sync *sfs = NULL;
1990 vn_lock(vp, LK_SHARED | LK_RETRY);
1991 if (vp->v_type == VREG) {
1992 bsize = vp->v_mount->mnt_stat.f_iosize;
1994 error = VOP_GETATTR(vp, &va, td->td_ucred);
2006 * Temporarily increase the backing VM
2007 * object's reference count so that a forced
2008 * reclamation of its vnode does not
2009 * immediately destroy it.
2011 VM_OBJECT_WLOCK(obj);
2012 if ((obj->flags & OBJ_DEAD) == 0) {
2013 vm_object_reference_locked(obj);
2014 VM_OBJECT_WUNLOCK(obj);
2016 VM_OBJECT_WUNLOCK(obj);
2021 bsize = 0; /* silence gcc */
2029 * The socket must be a stream socket and connected.
2030 * Remember if it a blocking or non-blocking socket.
2032 if ((error = getsock_cap(td->td_proc->p_fd, sockfd, CAP_SEND,
2033 &sock_fp, NULL)) != 0)
2035 so = sock_fp->f_data;
2036 if (so->so_type != SOCK_STREAM) {
2040 if ((so->so_state & SS_ISCONNECTED) == 0) {
2045 * Do not wait on memory allocations but return ENOMEM for
2046 * caller to retry later.
2047 * XXX: Experimental.
2049 if (flags & SF_MNOWAIT)
2052 if (flags & SF_SYNC) {
2053 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO);
2054 mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
2055 cv_init(&sfs->cv, "sendfile");
2059 error = mac_socket_check_send(td->td_ucred, so);
2064 /* If headers are specified copy them into mbufs. */
2065 if (hdr_uio != NULL) {
2066 hdr_uio->uio_td = td;
2067 hdr_uio->uio_rw = UIO_WRITE;
2068 if (hdr_uio->uio_resid > 0) {
2070 * In FBSD < 5.0 the nbytes to send also included
2071 * the header. If compat is specified subtract the
2072 * header size from nbytes.
2074 if (kflags & SFK_COMPAT) {
2075 if (nbytes > hdr_uio->uio_resid)
2076 nbytes -= hdr_uio->uio_resid;
2080 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
2083 error = mnw ? EAGAIN : ENOBUFS;
2086 hdrlen = m_length(m, NULL);
2091 * Protect against multiple writers to the socket.
2093 * XXXRW: Historically this has assumed non-interruptibility, so now
2094 * we implement that, but possibly shouldn't.
2096 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
2099 * Loop through the pages of the file, starting with the requested
2100 * offset. Get a file page (do I/O if necessary), map the file page
2101 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
2103 * This is done in two loops. The inner loop turns as many pages
2104 * as it can, up to available socket buffer space, without blocking
2105 * into mbufs to have it bulk delivered into the socket send buffer.
2106 * The outer loop checks the state and available space of the socket
2107 * and takes care of the overall progress.
2109 for (off = offset; ; ) {
2115 if ((nbytes != 0 && nbytes == fsbytes) ||
2116 (nbytes == 0 && va.va_size == fsbytes))
2125 * Check the socket state for ongoing connection,
2126 * no errors and space in socket buffer.
2127 * If space is low allow for the remainder of the
2128 * file to be processed if it fits the socket buffer.
2129 * Otherwise block in waiting for sufficient space
2130 * to proceed, or if the socket is nonblocking, return
2131 * to userland with EAGAIN while reporting how far
2133 * We wait until the socket buffer has significant free
2134 * space to do bulk sends. This makes good use of file
2135 * system read ahead and allows packet segmentation
2136 * offloading hardware to take over lots of work. If
2137 * we were not careful here we would send off only one
2140 SOCKBUF_LOCK(&so->so_snd);
2141 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
2142 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
2144 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2146 SOCKBUF_UNLOCK(&so->so_snd);
2148 } else if (so->so_error) {
2149 error = so->so_error;
2151 SOCKBUF_UNLOCK(&so->so_snd);
2154 space = sbspace(&so->so_snd);
2157 space < so->so_snd.sb_lowat)) {
2158 if (so->so_state & SS_NBIO) {
2159 SOCKBUF_UNLOCK(&so->so_snd);
2164 * sbwait drops the lock while sleeping.
2165 * When we loop back to retry_space the
2166 * state may have changed and we retest
2169 error = sbwait(&so->so_snd);
2171 * An error from sbwait usually indicates that we've
2172 * been interrupted by a signal. If we've sent anything
2173 * then return bytes sent, otherwise return the error.
2176 SOCKBUF_UNLOCK(&so->so_snd);
2181 SOCKBUF_UNLOCK(&so->so_snd);
2184 * Reduce space in the socket buffer by the size of
2185 * the header mbuf chain.
2186 * hdrlen is set to 0 after the first loop.
2190 error = vn_lock(vp, LK_SHARED);
2193 error = VOP_GETATTR(vp, &va, td->td_ucred);
2194 if (error != 0 || off >= va.va_size) {
2200 * Loop and construct maximum sized mbuf chain to be bulk
2201 * dumped into socket buffer.
2203 while (space > loopbytes) {
2209 * Calculate the amount to transfer.
2210 * Not to exceed a page, the EOF,
2211 * or the passed in nbytes.
2213 pgoff = (vm_offset_t)(off & PAGE_MASK);
2215 rem = (nbytes - fsbytes - loopbytes);
2218 offset - fsbytes - loopbytes;
2219 xfsize = omin(PAGE_SIZE - pgoff, rem);
2220 xfsize = omin(space - loopbytes, xfsize);
2222 done = 1; /* all data sent */
2227 * Attempt to look up the page. Allocate
2228 * if not found or wait and loop if busy.
2230 pindex = OFF_TO_IDX(off);
2231 VM_OBJECT_WLOCK(obj);
2232 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2233 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
2236 * Check if page is valid for what we need,
2237 * otherwise initiate I/O.
2238 * If we already turned some pages into mbufs,
2239 * send them off before we come here again and
2242 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2243 VM_OBJECT_WUNLOCK(obj);
2245 error = EAGAIN; /* send what we already got */
2246 else if (flags & SF_NODISKIO)
2250 int readahead = sfreadahead * MAXBSIZE;
2252 VM_OBJECT_WUNLOCK(obj);
2255 * Get the page from backing store.
2256 * XXXMAC: Because we don't have fp->f_cred
2257 * here, we pass in NOCRED. This is probably
2258 * wrong, but is consistent with our original
2261 error = vn_rdwr(UIO_READ, vp, NULL, readahead,
2262 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2263 IO_VMIO | ((readahead / bsize) << IO_SEQSHIFT),
2264 td->td_ucred, NOCRED, &resid, td);
2265 SFSTAT_INC(sf_iocnt);
2267 VM_OBJECT_WLOCK(obj);
2271 vm_page_unwire(pg, 0);
2273 * See if anyone else might know about
2274 * this page. If not and it is not valid,
2277 if (pg->wire_count == 0 && pg->valid == 0 &&
2278 !vm_page_busied(pg))
2281 VM_OBJECT_WUNLOCK(obj);
2282 if (error == EAGAIN)
2283 error = 0; /* not a real error */
2288 * Get a sendfile buf. When allocating the
2289 * first buffer for mbuf chain, we usually
2290 * wait as long as necessary, but this wait
2291 * can be interrupted. For consequent
2292 * buffers, do not sleep, since several
2293 * threads might exhaust the buffers and then
2296 sf = sf_buf_alloc(pg, (mnw || m != NULL) ? SFB_NOWAIT :
2299 SFSTAT_INC(sf_allocfail);
2301 vm_page_unwire(pg, 0);
2302 KASSERT(pg->object != NULL,
2303 ("%s: object disappeared", __func__));
2306 error = (mnw ? EAGAIN : EINTR);
2311 * Get an mbuf and set it up as having
2314 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2316 error = (mnw ? EAGAIN : ENOBUFS);
2317 sf_buf_mext(NULL, sf);
2320 if (m_extadd(m0, (caddr_t )sf_buf_kva(sf), PAGE_SIZE,
2321 sf_buf_mext, sfs, sf, M_RDONLY, EXT_SFBUF,
2322 (mnw ? M_NOWAIT : M_WAITOK)) != 0) {
2323 error = (mnw ? EAGAIN : ENOBUFS);
2324 sf_buf_mext(NULL, sf);
2328 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2331 /* Append to mbuf chain. */
2335 m_last(m)->m_next = m0;
2340 /* Keep track of bits processed. */
2341 loopbytes += xfsize;
2345 mtx_lock(&sfs->mtx);
2347 mtx_unlock(&sfs->mtx);
2353 /* Add the buffer chain to the socket buffer. */
2357 mlen = m_length(m, NULL);
2358 SOCKBUF_LOCK(&so->so_snd);
2359 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2361 SOCKBUF_UNLOCK(&so->so_snd);
2364 SOCKBUF_UNLOCK(&so->so_snd);
2365 CURVNET_SET(so->so_vnet);
2366 /* Avoid error aliasing. */
2367 err = (*so->so_proto->pr_usrreqs->pru_send)
2368 (so, 0, m, NULL, NULL, td);
2372 * We need two counters to get the
2373 * file offset and nbytes to send
2375 * - sbytes contains the total amount
2376 * of bytes sent, including headers.
2377 * - fsbytes contains the total amount
2378 * of bytes sent from the file.
2386 } else if (error == 0)
2388 m = NULL; /* pru_send always consumes */
2391 /* Quit outer loop on error or when we're done. */
2399 * Send trailers. Wimp out and use writev(2).
2401 if (trl_uio != NULL) {
2402 sbunlock(&so->so_snd);
2403 error = kern_writev(td, sockfd, trl_uio);
2405 sbytes += td->td_retval[0];
2410 sbunlock(&so->so_snd);
2413 * If there was no error we have to clear td->td_retval[0]
2414 * because it may have been set by writev.
2417 td->td_retval[0] = 0;
2420 copyout(&sbytes, sent, sizeof(off_t));
2423 vm_object_deallocate(obj);
2430 mtx_lock(&sfs->mtx);
2431 if (sfs->count != 0)
2432 cv_wait(&sfs->cv, &sfs->mtx);
2433 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2434 cv_destroy(&sfs->cv);
2435 mtx_destroy(&sfs->mtx);
2439 if (error == ERESTART)
2447 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2448 * otherwise all return EOPNOTSUPP.
2449 * XXX: We should make this loadable one day.
2452 sys_sctp_peeloff(td, uap)
2454 struct sctp_peeloff_args /* {
2459 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2460 struct file *nfp = NULL;
2462 struct socket *head, *so;
2466 AUDIT_ARG_FD(uap->sd);
2467 error = fgetsock(td, uap->sd, CAP_PEELOFF, &head, &fflag);
2470 if (head->so_proto->pr_protocol != IPPROTO_SCTP) {
2474 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2478 * At this point we know we do have a assoc to pull
2479 * we proceed to get the fd setup. This may block
2483 error = falloc(td, &nfp, &fd, 0);
2486 td->td_retval[0] = fd;
2488 CURVNET_SET(head->so_vnet);
2489 so = sonewconn(head, SS_ISCONNECTED);
2495 * Before changing the flags on the socket, we have to bump the
2496 * reference count. Otherwise, if the protocol calls sofree(),
2497 * the socket will be released due to a zero refcount.
2500 soref(so); /* file descriptor reference */
2505 TAILQ_REMOVE(&head->so_comp, so, so_list);
2507 so->so_state |= (head->so_state & SS_NBIO);
2508 so->so_state &= ~SS_NOFDREF;
2509 so->so_qstate &= ~SQ_COMP;
2512 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2513 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2516 if (head->so_sigio != NULL)
2517 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2521 * close the new descriptor, assuming someone hasn't ripped it
2522 * out from under us.
2525 fdclose(td->td_proc->p_fd, nfp, fd, td);
2528 * Release explicitly held references before returning.
2538 return (EOPNOTSUPP);
2543 sys_sctp_generic_sendmsg (td, uap)
2545 struct sctp_generic_sendmsg_args /* {
2551 struct sctp_sndrcvinfo *sinfo,
2555 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2556 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2558 struct file *fp = NULL;
2560 struct sockaddr *to = NULL;
2562 struct uio *ktruio = NULL;
2565 struct iovec iov[1];
2566 cap_rights_t rights;
2569 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2577 error = getsockaddr(&to, uap->to, uap->tolen);
2582 rights |= CAP_CONNECT;
2585 AUDIT_ARG_FD(uap->sd);
2586 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2590 if (to && (KTRPOINT(td, KTR_STRUCT)))
2594 iov[0].iov_base = uap->msg;
2595 iov[0].iov_len = uap->mlen;
2597 so = (struct socket *)fp->f_data;
2598 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2603 error = mac_socket_check_send(td->td_ucred, so);
2609 auio.uio_iovcnt = 1;
2610 auio.uio_segflg = UIO_USERSPACE;
2611 auio.uio_rw = UIO_WRITE;
2613 auio.uio_offset = 0; /* XXX */
2615 len = auio.uio_resid = uap->mlen;
2616 CURVNET_SET(so->so_vnet);
2617 error = sctp_lower_sosend(so, to, &auio,
2618 (struct mbuf *)NULL, (struct mbuf *)NULL,
2619 uap->flags, u_sinfo, td);
2622 if (auio.uio_resid != len && (error == ERESTART ||
2623 error == EINTR || error == EWOULDBLOCK))
2625 /* Generation of SIGPIPE can be controlled per socket. */
2626 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2627 !(uap->flags & MSG_NOSIGNAL)) {
2628 PROC_LOCK(td->td_proc);
2629 tdsignal(td, SIGPIPE);
2630 PROC_UNLOCK(td->td_proc);
2634 td->td_retval[0] = len - auio.uio_resid;
2636 if (ktruio != NULL) {
2637 ktruio->uio_resid = td->td_retval[0];
2638 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2649 return (EOPNOTSUPP);
2654 sys_sctp_generic_sendmsg_iov(td, uap)
2656 struct sctp_generic_sendmsg_iov_args /* {
2662 struct sctp_sndrcvinfo *sinfo,
2666 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2667 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2669 struct file *fp = NULL;
2672 struct sockaddr *to = NULL;
2674 struct uio *ktruio = NULL;
2677 struct iovec *iov, *tiov;
2678 cap_rights_t rights;
2681 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2688 error = getsockaddr(&to, uap->to, uap->tolen);
2693 rights |= CAP_CONNECT;
2696 AUDIT_ARG_FD(uap->sd);
2697 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2701 #ifdef COMPAT_FREEBSD32
2702 if (SV_CURPROC_FLAG(SV_ILP32))
2703 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2704 uap->iovlen, &iov, EMSGSIZE);
2707 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2711 if (to && (KTRPOINT(td, KTR_STRUCT)))
2715 so = (struct socket *)fp->f_data;
2716 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2721 error = mac_socket_check_send(td->td_ucred, so);
2727 auio.uio_iovcnt = uap->iovlen;
2728 auio.uio_segflg = UIO_USERSPACE;
2729 auio.uio_rw = UIO_WRITE;
2731 auio.uio_offset = 0; /* XXX */
2734 for (i = 0; i <uap->iovlen; i++, tiov++) {
2735 if ((auio.uio_resid += tiov->iov_len) < 0) {
2740 len = auio.uio_resid;
2741 CURVNET_SET(so->so_vnet);
2742 error = sctp_lower_sosend(so, to, &auio,
2743 (struct mbuf *)NULL, (struct mbuf *)NULL,
2744 uap->flags, u_sinfo, td);
2747 if (auio.uio_resid != len && (error == ERESTART ||
2748 error == EINTR || error == EWOULDBLOCK))
2750 /* Generation of SIGPIPE can be controlled per socket */
2751 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2752 !(uap->flags & MSG_NOSIGNAL)) {
2753 PROC_LOCK(td->td_proc);
2754 tdsignal(td, SIGPIPE);
2755 PROC_UNLOCK(td->td_proc);
2759 td->td_retval[0] = len - auio.uio_resid;
2761 if (ktruio != NULL) {
2762 ktruio->uio_resid = td->td_retval[0];
2763 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2776 return (EOPNOTSUPP);
2781 sys_sctp_generic_recvmsg(td, uap)
2783 struct sctp_generic_recvmsg_args /* {
2787 struct sockaddr *from,
2788 __socklen_t *fromlenaddr,
2789 struct sctp_sndrcvinfo *sinfo,
2793 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2794 uint8_t sockbufstore[256];
2796 struct iovec *iov, *tiov;
2797 struct sctp_sndrcvinfo sinfo;
2799 struct file *fp = NULL;
2800 struct sockaddr *fromsa;
2806 struct uio *ktruio = NULL;
2809 AUDIT_ARG_FD(uap->sd);
2810 error = getsock_cap(td->td_proc->p_fd, uap->sd, CAP_RECV, &fp, NULL);
2814 #ifdef COMPAT_FREEBSD32
2815 if (SV_CURPROC_FLAG(SV_ILP32))
2816 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2817 uap->iovlen, &iov, EMSGSIZE);
2820 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2825 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2830 error = mac_socket_check_receive(td->td_ucred, so);
2836 if (uap->fromlenaddr) {
2837 error = copyin(uap->fromlenaddr,
2838 &fromlen, sizeof (fromlen));
2845 if (uap->msg_flags) {
2846 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2854 auio.uio_iovcnt = uap->iovlen;
2855 auio.uio_segflg = UIO_USERSPACE;
2856 auio.uio_rw = UIO_READ;
2858 auio.uio_offset = 0; /* XXX */
2861 for (i = 0; i <uap->iovlen; i++, tiov++) {
2862 if ((auio.uio_resid += tiov->iov_len) < 0) {
2867 len = auio.uio_resid;
2868 fromsa = (struct sockaddr *)sockbufstore;
2871 if (KTRPOINT(td, KTR_GENIO))
2872 ktruio = cloneuio(&auio);
2874 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
2875 CURVNET_SET(so->so_vnet);
2876 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2877 fromsa, fromlen, &msg_flags,
2878 (struct sctp_sndrcvinfo *)&sinfo, 1);
2881 if (auio.uio_resid != len && (error == ERESTART ||
2882 error == EINTR || error == EWOULDBLOCK))
2886 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2889 if (ktruio != NULL) {
2890 ktruio->uio_resid = len - auio.uio_resid;
2891 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2896 td->td_retval[0] = len - auio.uio_resid;
2898 if (fromlen && uap->from) {
2900 if (len <= 0 || fromsa == 0)
2903 len = MIN(len, fromsa->sa_len);
2904 error = copyout(fromsa, uap->from, (size_t)len);
2908 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2914 if (KTRPOINT(td, KTR_STRUCT))
2915 ktrsockaddr(fromsa);
2917 if (uap->msg_flags) {
2918 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2931 return (EOPNOTSUPP);