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(struct mbuf *mb, 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)
1876 return (EXT_FREE_OK);
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);
1883 return (EXT_FREE_OK);
1889 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1890 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1892 * Send a file specified by 'fd' and starting at 'offset' to a socket
1893 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1894 * 0. Optionally add a header and/or trailer to the socket output. If
1895 * specified, write the total number of bytes sent into *sbytes.
1898 sys_sendfile(struct thread *td, struct sendfile_args *uap)
1901 return (do_sendfile(td, uap, 0));
1905 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1907 struct sf_hdtr hdtr;
1908 struct uio *hdr_uio, *trl_uio;
1912 if (uap->offset < 0)
1915 hdr_uio = trl_uio = NULL;
1917 if (uap->hdtr != NULL) {
1918 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1921 if (hdtr.headers != NULL) {
1922 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1926 if (hdtr.trailers != NULL) {
1927 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1934 AUDIT_ARG_FD(uap->fd);
1937 * sendfile(2) can start at any offset within a file so we require
1938 * CAP_READ+CAP_SEEK = CAP_PREAD.
1940 if ((error = fget_read(td, uap->fd, CAP_PREAD, &fp)) != 0)
1943 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, uap->offset,
1944 uap->nbytes, uap->sbytes, uap->flags, compat ? SFK_COMPAT : 0, td);
1949 free(hdr_uio, M_IOV);
1951 free(trl_uio, M_IOV);
1955 #ifdef COMPAT_FREEBSD4
1957 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1959 struct sendfile_args args;
1963 args.offset = uap->offset;
1964 args.nbytes = uap->nbytes;
1965 args.hdtr = uap->hdtr;
1966 args.sbytes = uap->sbytes;
1967 args.flags = uap->flags;
1969 return (do_sendfile(td, &args, 1));
1971 #endif /* COMPAT_FREEBSD4 */
1974 vn_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio,
1975 struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags,
1976 int kflags, struct thread *td)
1978 struct vnode *vp = fp->f_vnode;
1979 struct file *sock_fp;
1980 struct vm_object *obj = NULL;
1981 struct socket *so = NULL;
1982 struct mbuf *m = NULL;
1986 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1987 int error, hdrlen = 0, mnw = 0;
1989 struct sendfile_sync *sfs = NULL;
1991 vn_lock(vp, LK_SHARED | LK_RETRY);
1992 if (vp->v_type == VREG) {
1993 bsize = vp->v_mount->mnt_stat.f_iosize;
1995 error = VOP_GETATTR(vp, &va, td->td_ucred);
2007 * Temporarily increase the backing VM
2008 * object's reference count so that a forced
2009 * reclamation of its vnode does not
2010 * immediately destroy it.
2012 VM_OBJECT_WLOCK(obj);
2013 if ((obj->flags & OBJ_DEAD) == 0) {
2014 vm_object_reference_locked(obj);
2015 VM_OBJECT_WUNLOCK(obj);
2017 VM_OBJECT_WUNLOCK(obj);
2022 bsize = 0; /* silence gcc */
2030 * The socket must be a stream socket and connected.
2031 * Remember if it a blocking or non-blocking socket.
2033 if ((error = getsock_cap(td->td_proc->p_fd, sockfd, CAP_SEND,
2034 &sock_fp, NULL)) != 0)
2036 so = sock_fp->f_data;
2037 if (so->so_type != SOCK_STREAM) {
2041 if ((so->so_state & SS_ISCONNECTED) == 0) {
2046 * Do not wait on memory allocations but return ENOMEM for
2047 * caller to retry later.
2048 * XXX: Experimental.
2050 if (flags & SF_MNOWAIT)
2053 if (flags & SF_SYNC) {
2054 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO);
2055 mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
2056 cv_init(&sfs->cv, "sendfile");
2060 error = mac_socket_check_send(td->td_ucred, so);
2065 /* If headers are specified copy them into mbufs. */
2066 if (hdr_uio != NULL) {
2067 hdr_uio->uio_td = td;
2068 hdr_uio->uio_rw = UIO_WRITE;
2069 if (hdr_uio->uio_resid > 0) {
2071 * In FBSD < 5.0 the nbytes to send also included
2072 * the header. If compat is specified subtract the
2073 * header size from nbytes.
2075 if (kflags & SFK_COMPAT) {
2076 if (nbytes > hdr_uio->uio_resid)
2077 nbytes -= hdr_uio->uio_resid;
2081 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
2084 error = mnw ? EAGAIN : ENOBUFS;
2087 hdrlen = m_length(m, NULL);
2092 * Protect against multiple writers to the socket.
2094 * XXXRW: Historically this has assumed non-interruptibility, so now
2095 * we implement that, but possibly shouldn't.
2097 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
2100 * Loop through the pages of the file, starting with the requested
2101 * offset. Get a file page (do I/O if necessary), map the file page
2102 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
2104 * This is done in two loops. The inner loop turns as many pages
2105 * as it can, up to available socket buffer space, without blocking
2106 * into mbufs to have it bulk delivered into the socket send buffer.
2107 * The outer loop checks the state and available space of the socket
2108 * and takes care of the overall progress.
2110 for (off = offset; ; ) {
2116 if ((nbytes != 0 && nbytes == fsbytes) ||
2117 (nbytes == 0 && va.va_size == fsbytes))
2126 * Check the socket state for ongoing connection,
2127 * no errors and space in socket buffer.
2128 * If space is low allow for the remainder of the
2129 * file to be processed if it fits the socket buffer.
2130 * Otherwise block in waiting for sufficient space
2131 * to proceed, or if the socket is nonblocking, return
2132 * to userland with EAGAIN while reporting how far
2134 * We wait until the socket buffer has significant free
2135 * space to do bulk sends. This makes good use of file
2136 * system read ahead and allows packet segmentation
2137 * offloading hardware to take over lots of work. If
2138 * we were not careful here we would send off only one
2141 SOCKBUF_LOCK(&so->so_snd);
2142 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
2143 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
2145 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2147 SOCKBUF_UNLOCK(&so->so_snd);
2149 } else if (so->so_error) {
2150 error = so->so_error;
2152 SOCKBUF_UNLOCK(&so->so_snd);
2155 space = sbspace(&so->so_snd);
2158 space < so->so_snd.sb_lowat)) {
2159 if (so->so_state & SS_NBIO) {
2160 SOCKBUF_UNLOCK(&so->so_snd);
2165 * sbwait drops the lock while sleeping.
2166 * When we loop back to retry_space the
2167 * state may have changed and we retest
2170 error = sbwait(&so->so_snd);
2172 * An error from sbwait usually indicates that we've
2173 * been interrupted by a signal. If we've sent anything
2174 * then return bytes sent, otherwise return the error.
2177 SOCKBUF_UNLOCK(&so->so_snd);
2182 SOCKBUF_UNLOCK(&so->so_snd);
2185 * Reduce space in the socket buffer by the size of
2186 * the header mbuf chain.
2187 * hdrlen is set to 0 after the first loop.
2191 error = vn_lock(vp, LK_SHARED);
2194 error = VOP_GETATTR(vp, &va, td->td_ucred);
2195 if (error != 0 || off >= va.va_size) {
2201 * Loop and construct maximum sized mbuf chain to be bulk
2202 * dumped into socket buffer.
2204 while (space > loopbytes) {
2210 * Calculate the amount to transfer.
2211 * Not to exceed a page, the EOF,
2212 * or the passed in nbytes.
2214 pgoff = (vm_offset_t)(off & PAGE_MASK);
2216 rem = (nbytes - fsbytes - loopbytes);
2219 offset - fsbytes - loopbytes;
2220 xfsize = omin(PAGE_SIZE - pgoff, rem);
2221 xfsize = omin(space - loopbytes, xfsize);
2223 done = 1; /* all data sent */
2228 * Attempt to look up the page. Allocate
2229 * if not found or wait and loop if busy.
2231 pindex = OFF_TO_IDX(off);
2232 VM_OBJECT_WLOCK(obj);
2233 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2234 VM_ALLOC_IGN_SBUSY | VM_ALLOC_NORMAL |
2238 * Check if page is valid for what we need,
2239 * otherwise initiate I/O.
2240 * If we already turned some pages into mbufs,
2241 * send them off before we come here again and
2244 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2245 VM_OBJECT_WUNLOCK(obj);
2247 error = EAGAIN; /* send what we already got */
2248 else if (flags & SF_NODISKIO)
2252 int readahead = sfreadahead * MAXBSIZE;
2254 VM_OBJECT_WUNLOCK(obj);
2257 * Get the page from backing store.
2258 * XXXMAC: Because we don't have fp->f_cred
2259 * here, we pass in NOCRED. This is probably
2260 * wrong, but is consistent with our original
2263 error = vn_rdwr(UIO_READ, vp, NULL, readahead,
2264 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2265 IO_VMIO | ((readahead / bsize) << IO_SEQSHIFT),
2266 td->td_ucred, NOCRED, &resid, td);
2267 SFSTAT_INC(sf_iocnt);
2269 VM_OBJECT_WLOCK(obj);
2273 vm_page_unwire(pg, 0);
2275 * See if anyone else might know about
2276 * this page. If not and it is not valid,
2279 if (pg->wire_count == 0 && pg->valid == 0 &&
2280 !vm_page_busied(pg))
2283 VM_OBJECT_WUNLOCK(obj);
2284 if (error == EAGAIN)
2285 error = 0; /* not a real error */
2290 * Get a sendfile buf. When allocating the
2291 * first buffer for mbuf chain, we usually
2292 * wait as long as necessary, but this wait
2293 * can be interrupted. For consequent
2294 * buffers, do not sleep, since several
2295 * threads might exhaust the buffers and then
2298 sf = sf_buf_alloc(pg, (mnw || m != NULL) ? SFB_NOWAIT :
2301 SFSTAT_INC(sf_allocfail);
2303 vm_page_unwire(pg, 0);
2304 KASSERT(pg->object != NULL,
2305 ("%s: object disappeared", __func__));
2308 error = (mnw ? EAGAIN : EINTR);
2313 * Get an mbuf and set it up as having
2316 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2318 error = (mnw ? EAGAIN : ENOBUFS);
2319 (void)sf_buf_mext(NULL, NULL, sf);
2322 if (m_extadd(m0, (caddr_t )sf_buf_kva(sf), PAGE_SIZE,
2323 sf_buf_mext, sfs, sf, M_RDONLY, EXT_SFBUF,
2324 (mnw ? M_NOWAIT : M_WAITOK)) != 0) {
2325 error = (mnw ? EAGAIN : ENOBUFS);
2326 (void)sf_buf_mext(NULL, NULL, sf);
2330 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2333 /* Append to mbuf chain. */
2337 m_last(m)->m_next = m0;
2342 /* Keep track of bits processed. */
2343 loopbytes += xfsize;
2347 mtx_lock(&sfs->mtx);
2349 mtx_unlock(&sfs->mtx);
2355 /* Add the buffer chain to the socket buffer. */
2359 mlen = m_length(m, NULL);
2360 SOCKBUF_LOCK(&so->so_snd);
2361 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2363 SOCKBUF_UNLOCK(&so->so_snd);
2366 SOCKBUF_UNLOCK(&so->so_snd);
2367 CURVNET_SET(so->so_vnet);
2368 /* Avoid error aliasing. */
2369 err = (*so->so_proto->pr_usrreqs->pru_send)
2370 (so, 0, m, NULL, NULL, td);
2374 * We need two counters to get the
2375 * file offset and nbytes to send
2377 * - sbytes contains the total amount
2378 * of bytes sent, including headers.
2379 * - fsbytes contains the total amount
2380 * of bytes sent from the file.
2388 } else if (error == 0)
2390 m = NULL; /* pru_send always consumes */
2393 /* Quit outer loop on error or when we're done. */
2401 * Send trailers. Wimp out and use writev(2).
2403 if (trl_uio != NULL) {
2404 sbunlock(&so->so_snd);
2405 error = kern_writev(td, sockfd, trl_uio);
2407 sbytes += td->td_retval[0];
2412 sbunlock(&so->so_snd);
2415 * If there was no error we have to clear td->td_retval[0]
2416 * because it may have been set by writev.
2419 td->td_retval[0] = 0;
2422 copyout(&sbytes, sent, sizeof(off_t));
2425 vm_object_deallocate(obj);
2432 mtx_lock(&sfs->mtx);
2433 if (sfs->count != 0)
2434 cv_wait(&sfs->cv, &sfs->mtx);
2435 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2436 cv_destroy(&sfs->cv);
2437 mtx_destroy(&sfs->mtx);
2441 if (error == ERESTART)
2449 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2450 * otherwise all return EOPNOTSUPP.
2451 * XXX: We should make this loadable one day.
2454 sys_sctp_peeloff(td, uap)
2456 struct sctp_peeloff_args /* {
2461 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2462 struct file *nfp = NULL;
2464 struct socket *head, *so;
2468 AUDIT_ARG_FD(uap->sd);
2469 error = fgetsock(td, uap->sd, CAP_PEELOFF, &head, &fflag);
2472 if (head->so_proto->pr_protocol != IPPROTO_SCTP) {
2476 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2480 * At this point we know we do have a assoc to pull
2481 * we proceed to get the fd setup. This may block
2485 error = falloc(td, &nfp, &fd, 0);
2488 td->td_retval[0] = fd;
2490 CURVNET_SET(head->so_vnet);
2491 so = sonewconn(head, SS_ISCONNECTED);
2497 * Before changing the flags on the socket, we have to bump the
2498 * reference count. Otherwise, if the protocol calls sofree(),
2499 * the socket will be released due to a zero refcount.
2502 soref(so); /* file descriptor reference */
2507 TAILQ_REMOVE(&head->so_comp, so, so_list);
2509 so->so_state |= (head->so_state & SS_NBIO);
2510 so->so_state &= ~SS_NOFDREF;
2511 so->so_qstate &= ~SQ_COMP;
2514 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2515 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2518 if (head->so_sigio != NULL)
2519 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2523 * close the new descriptor, assuming someone hasn't ripped it
2524 * out from under us.
2527 fdclose(td->td_proc->p_fd, nfp, fd, td);
2530 * Release explicitly held references before returning.
2540 return (EOPNOTSUPP);
2545 sys_sctp_generic_sendmsg (td, uap)
2547 struct sctp_generic_sendmsg_args /* {
2553 struct sctp_sndrcvinfo *sinfo,
2557 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2558 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2560 struct file *fp = NULL;
2562 struct sockaddr *to = NULL;
2564 struct uio *ktruio = NULL;
2567 struct iovec iov[1];
2568 cap_rights_t rights;
2571 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2579 error = getsockaddr(&to, uap->to, uap->tolen);
2584 rights |= CAP_CONNECT;
2587 AUDIT_ARG_FD(uap->sd);
2588 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2592 if (to && (KTRPOINT(td, KTR_STRUCT)))
2596 iov[0].iov_base = uap->msg;
2597 iov[0].iov_len = uap->mlen;
2599 so = (struct socket *)fp->f_data;
2600 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2605 error = mac_socket_check_send(td->td_ucred, so);
2611 auio.uio_iovcnt = 1;
2612 auio.uio_segflg = UIO_USERSPACE;
2613 auio.uio_rw = UIO_WRITE;
2615 auio.uio_offset = 0; /* XXX */
2617 len = auio.uio_resid = uap->mlen;
2618 CURVNET_SET(so->so_vnet);
2619 error = sctp_lower_sosend(so, to, &auio,
2620 (struct mbuf *)NULL, (struct mbuf *)NULL,
2621 uap->flags, u_sinfo, td);
2624 if (auio.uio_resid != len && (error == ERESTART ||
2625 error == EINTR || error == EWOULDBLOCK))
2627 /* Generation of SIGPIPE can be controlled per socket. */
2628 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2629 !(uap->flags & MSG_NOSIGNAL)) {
2630 PROC_LOCK(td->td_proc);
2631 tdsignal(td, SIGPIPE);
2632 PROC_UNLOCK(td->td_proc);
2636 td->td_retval[0] = len - auio.uio_resid;
2638 if (ktruio != NULL) {
2639 ktruio->uio_resid = td->td_retval[0];
2640 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2651 return (EOPNOTSUPP);
2656 sys_sctp_generic_sendmsg_iov(td, uap)
2658 struct sctp_generic_sendmsg_iov_args /* {
2664 struct sctp_sndrcvinfo *sinfo,
2668 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2669 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2671 struct file *fp = NULL;
2674 struct sockaddr *to = NULL;
2676 struct uio *ktruio = NULL;
2679 struct iovec *iov, *tiov;
2680 cap_rights_t rights;
2683 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2690 error = getsockaddr(&to, uap->to, uap->tolen);
2695 rights |= CAP_CONNECT;
2698 AUDIT_ARG_FD(uap->sd);
2699 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2703 #ifdef COMPAT_FREEBSD32
2704 if (SV_CURPROC_FLAG(SV_ILP32))
2705 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2706 uap->iovlen, &iov, EMSGSIZE);
2709 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2713 if (to && (KTRPOINT(td, KTR_STRUCT)))
2717 so = (struct socket *)fp->f_data;
2718 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2723 error = mac_socket_check_send(td->td_ucred, so);
2729 auio.uio_iovcnt = uap->iovlen;
2730 auio.uio_segflg = UIO_USERSPACE;
2731 auio.uio_rw = UIO_WRITE;
2733 auio.uio_offset = 0; /* XXX */
2736 for (i = 0; i <uap->iovlen; i++, tiov++) {
2737 if ((auio.uio_resid += tiov->iov_len) < 0) {
2742 len = auio.uio_resid;
2743 CURVNET_SET(so->so_vnet);
2744 error = sctp_lower_sosend(so, to, &auio,
2745 (struct mbuf *)NULL, (struct mbuf *)NULL,
2746 uap->flags, u_sinfo, td);
2749 if (auio.uio_resid != len && (error == ERESTART ||
2750 error == EINTR || error == EWOULDBLOCK))
2752 /* Generation of SIGPIPE can be controlled per socket */
2753 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2754 !(uap->flags & MSG_NOSIGNAL)) {
2755 PROC_LOCK(td->td_proc);
2756 tdsignal(td, SIGPIPE);
2757 PROC_UNLOCK(td->td_proc);
2761 td->td_retval[0] = len - auio.uio_resid;
2763 if (ktruio != NULL) {
2764 ktruio->uio_resid = td->td_retval[0];
2765 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2778 return (EOPNOTSUPP);
2783 sys_sctp_generic_recvmsg(td, uap)
2785 struct sctp_generic_recvmsg_args /* {
2789 struct sockaddr *from,
2790 __socklen_t *fromlenaddr,
2791 struct sctp_sndrcvinfo *sinfo,
2795 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2796 uint8_t sockbufstore[256];
2798 struct iovec *iov, *tiov;
2799 struct sctp_sndrcvinfo sinfo;
2801 struct file *fp = NULL;
2802 struct sockaddr *fromsa;
2808 struct uio *ktruio = NULL;
2811 AUDIT_ARG_FD(uap->sd);
2812 error = getsock_cap(td->td_proc->p_fd, uap->sd, CAP_RECV, &fp, NULL);
2816 #ifdef COMPAT_FREEBSD32
2817 if (SV_CURPROC_FLAG(SV_ILP32))
2818 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2819 uap->iovlen, &iov, EMSGSIZE);
2822 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2827 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2832 error = mac_socket_check_receive(td->td_ucred, so);
2838 if (uap->fromlenaddr) {
2839 error = copyin(uap->fromlenaddr,
2840 &fromlen, sizeof (fromlen));
2847 if (uap->msg_flags) {
2848 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2856 auio.uio_iovcnt = uap->iovlen;
2857 auio.uio_segflg = UIO_USERSPACE;
2858 auio.uio_rw = UIO_READ;
2860 auio.uio_offset = 0; /* XXX */
2863 for (i = 0; i <uap->iovlen; i++, tiov++) {
2864 if ((auio.uio_resid += tiov->iov_len) < 0) {
2869 len = auio.uio_resid;
2870 fromsa = (struct sockaddr *)sockbufstore;
2873 if (KTRPOINT(td, KTR_GENIO))
2874 ktruio = cloneuio(&auio);
2876 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
2877 CURVNET_SET(so->so_vnet);
2878 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2879 fromsa, fromlen, &msg_flags,
2880 (struct sctp_sndrcvinfo *)&sinfo, 1);
2883 if (auio.uio_resid != len && (error == ERESTART ||
2884 error == EINTR || error == EWOULDBLOCK))
2888 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2891 if (ktruio != NULL) {
2892 ktruio->uio_resid = len - auio.uio_resid;
2893 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2898 td->td_retval[0] = len - auio.uio_resid;
2900 if (fromlen && uap->from) {
2902 if (len <= 0 || fromsa == 0)
2905 len = MIN(len, fromsa->sa_len);
2906 error = copyout(fromsa, uap->from, (size_t)len);
2910 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2916 if (KTRPOINT(td, KTR_STRUCT))
2917 ktrsockaddr(fromsa);
2919 if (uap->msg_flags) {
2920 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2933 return (EOPNOTSUPP);