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 *rightsp,
168 struct file **fpp, u_int *fflagp)
173 error = fget_unlocked(fdp, fd, rightsp, 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)
274 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
275 error = getsock_cap(td->td_proc->p_fd, fd,
276 cap_rights_init(&rights, CAP_BIND), &fp, NULL);
281 if (KTRPOINT(td, KTR_STRUCT))
285 error = mac_socket_check_bind(td->td_ucred, so, sa);
288 if (dirfd == AT_FDCWD)
289 error = sobind(so, sa, td);
291 error = sobindat(dirfd, so, sa, td);
300 kern_bind(struct thread *td, int fd, struct sockaddr *sa)
303 return (kern_bindat(td, AT_FDCWD, fd, sa));
310 struct bindat_args /* {
320 error = getsockaddr(&sa, uap->name, uap->namelen);
322 error = kern_bindat(td, uap->fd, uap->s, sa);
332 struct listen_args /* {
342 AUDIT_ARG_FD(uap->s);
343 error = getsock_cap(td->td_proc->p_fd, uap->s,
344 cap_rights_init(&rights, CAP_LISTEN), &fp, NULL);
348 error = mac_socket_check_listen(td->td_ucred, so);
351 error = solisten(so, uap->backlog, td);
361 accept1(td, s, uname, anamelen, flags)
364 struct sockaddr *uname;
368 struct sockaddr *name;
374 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
376 error = copyin(anamelen, &namelen, sizeof (namelen));
380 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
383 * return a namelen of zero for older code which might
384 * ignore the return value from accept.
387 (void) copyout(&namelen, anamelen, sizeof(*anamelen));
391 if (error == 0 && uname != NULL) {
392 #ifdef COMPAT_OLDSOCK
393 if (flags & ACCEPT4_COMPAT)
394 ((struct osockaddr *)name)->sa_family =
397 error = copyout(name, uname, namelen);
400 error = copyout(&namelen, anamelen,
403 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
405 free(name, M_SONAME);
410 kern_accept(struct thread *td, int s, struct sockaddr **name,
411 socklen_t *namelen, struct file **fp)
413 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
417 kern_accept4(struct thread *td, int s, struct sockaddr **name,
418 socklen_t *namelen, int flags, struct file **fp)
420 struct filedesc *fdp;
421 struct file *headfp, *nfp = NULL;
422 struct sockaddr *sa = NULL;
423 struct socket *head, *so;
433 fdp = td->td_proc->p_fd;
434 error = getsock_cap(fdp, s, cap_rights_init(&rights, CAP_ACCEPT),
438 head = headfp->f_data;
439 if ((head->so_options & SO_ACCEPTCONN) == 0) {
444 error = mac_socket_check_accept(td->td_ucred, head);
448 error = falloc(td, &nfp, &fd, (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0);
452 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
457 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
458 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
459 head->so_error = ECONNABORTED;
462 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
469 if (head->so_error) {
470 error = head->so_error;
475 so = TAILQ_FIRST(&head->so_comp);
476 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
477 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
480 * Before changing the flags on the socket, we have to bump the
481 * reference count. Otherwise, if the protocol calls sofree(),
482 * the socket will be released due to a zero refcount.
484 SOCK_LOCK(so); /* soref() and so_state update */
485 soref(so); /* file descriptor reference */
487 TAILQ_REMOVE(&head->so_comp, so, so_list);
489 if (flags & ACCEPT4_INHERIT)
490 so->so_state |= (head->so_state & SS_NBIO);
492 so->so_state |= (flags & SOCK_NONBLOCK) ? SS_NBIO : 0;
493 so->so_qstate &= ~SQ_COMP;
499 /* An extra reference on `nfp' has been held for us by falloc(). */
500 td->td_retval[0] = fd;
502 /* connection has been removed from the listen queue */
503 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
505 if (flags & ACCEPT4_INHERIT) {
506 pgid = fgetown(&head->so_sigio);
508 fsetown(pgid, &so->so_sigio);
510 fflag &= ~(FNONBLOCK | FASYNC);
511 if (flags & SOCK_NONBLOCK)
515 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
516 /* Sync socket nonblocking/async state with file flags */
517 tmp = fflag & FNONBLOCK;
518 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
519 tmp = fflag & FASYNC;
520 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
522 error = soaccept(so, &sa);
525 * return a namelen of zero for older code which might
526 * ignore the return value from accept.
537 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
539 /* check sa_len before it is destroyed */
540 if (*namelen > sa->sa_len)
541 *namelen = sa->sa_len;
543 if (KTRPOINT(td, KTR_STRUCT))
553 * close the new descriptor, assuming someone hasn't ripped it
557 fdclose(fdp, nfp, fd, td);
560 * Release explicitly held references before returning. We return
561 * a reference on nfp to the caller on success if they request it.
580 struct accept_args *uap;
583 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
589 struct accept4_args *uap;
592 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
595 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
598 #ifdef COMPAT_OLDSOCK
602 struct accept_args *uap;
605 return (accept1(td, uap->s, uap->name, uap->anamelen,
606 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
608 #endif /* COMPAT_OLDSOCK */
614 struct connect_args /* {
623 error = getsockaddr(&sa, uap->name, uap->namelen);
625 error = kern_connect(td, uap->s, sa);
632 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
637 int error, interrupted = 0;
640 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
641 error = getsock_cap(td->td_proc->p_fd, fd,
642 cap_rights_init(&rights, CAP_CONNECT), &fp, NULL);
646 if (so->so_state & SS_ISCONNECTING) {
651 if (KTRPOINT(td, KTR_STRUCT))
655 error = mac_socket_check_connect(td->td_ucred, so, sa);
659 if (dirfd == AT_FDCWD)
660 error = soconnect(so, sa, td);
662 error = soconnectat(dirfd, so, sa, td);
665 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
670 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
671 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
674 if (error == EINTR || error == ERESTART)
680 error = so->so_error;
686 so->so_state &= ~SS_ISCONNECTING;
687 if (error == ERESTART)
695 kern_connect(struct thread *td, int fd, struct sockaddr *sa)
698 return (kern_connectat(td, AT_FDCWD, fd, sa));
703 sys_connectat(td, uap)
705 struct connectat_args /* {
715 error = getsockaddr(&sa, uap->name, uap->namelen);
717 error = kern_connectat(td, uap->fd, uap->s, sa);
724 kern_socketpair(struct thread *td, int domain, int type, int protocol,
727 struct filedesc *fdp = td->td_proc->p_fd;
728 struct file *fp1, *fp2;
729 struct socket *so1, *so2;
730 int fd, error, oflag, fflag;
732 AUDIT_ARG_SOCKET(domain, type, protocol);
736 if ((type & SOCK_CLOEXEC) != 0) {
737 type &= ~SOCK_CLOEXEC;
740 if ((type & SOCK_NONBLOCK) != 0) {
741 type &= ~SOCK_NONBLOCK;
745 /* We might want to have a separate check for socket pairs. */
746 error = mac_socket_check_create(td->td_ucred, domain, type,
751 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
754 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
757 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
758 error = falloc(td, &fp1, &fd, oflag);
762 fp1->f_data = so1; /* so1 already has ref count */
763 error = falloc(td, &fp2, &fd, oflag);
766 fp2->f_data = so2; /* so2 already has ref count */
768 error = soconnect2(so1, so2);
771 if (type == SOCK_DGRAM) {
773 * Datagram socket connection is asymmetric.
775 error = soconnect2(so2, so1);
779 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
781 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
783 if ((fflag & FNONBLOCK) != 0) {
784 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
785 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
791 fdclose(fdp, fp2, rsv[1], td);
794 fdclose(fdp, fp1, rsv[0], td);
806 sys_socketpair(struct thread *td, struct socketpair_args *uap)
810 error = kern_socketpair(td, uap->domain, uap->type,
814 error = copyout(sv, uap->rsv, 2 * sizeof(int));
816 (void)kern_close(td, sv[0]);
817 (void)kern_close(td, sv[1]);
823 sendit(td, s, mp, flags)
829 struct mbuf *control;
833 #ifdef CAPABILITY_MODE
834 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
838 if (mp->msg_name != NULL) {
839 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
849 if (mp->msg_control) {
850 if (mp->msg_controllen < sizeof(struct cmsghdr)
851 #ifdef COMPAT_OLDSOCK
852 && mp->msg_flags != MSG_COMPAT
858 error = sockargs(&control, mp->msg_control,
859 mp->msg_controllen, MT_CONTROL);
862 #ifdef COMPAT_OLDSOCK
863 if (mp->msg_flags == MSG_COMPAT) {
866 M_PREPEND(control, sizeof(*cm), M_WAITOK);
867 cm = mtod(control, struct cmsghdr *);
868 cm->cmsg_len = control->m_len;
869 cm->cmsg_level = SOL_SOCKET;
870 cm->cmsg_type = SCM_RIGHTS;
877 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
885 kern_sendit(td, s, mp, flags, control, segflg)
890 struct mbuf *control;
899 struct uio *ktruio = NULL;
905 cap_rights_init(&rights, CAP_SEND);
906 if (mp->msg_name != NULL) {
907 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
908 cap_rights_set(&rights, CAP_CONNECT);
910 error = getsock_cap(td->td_proc->p_fd, s, &rights, &fp, NULL);
913 so = (struct socket *)fp->f_data;
916 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
917 ktrsockaddr(mp->msg_name);
920 if (mp->msg_name != NULL) {
921 error = mac_socket_check_connect(td->td_ucred, so,
926 error = mac_socket_check_send(td->td_ucred, so);
931 auio.uio_iov = mp->msg_iov;
932 auio.uio_iovcnt = mp->msg_iovlen;
933 auio.uio_segflg = segflg;
934 auio.uio_rw = UIO_WRITE;
936 auio.uio_offset = 0; /* XXX */
939 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
940 if ((auio.uio_resid += iov->iov_len) < 0) {
946 if (KTRPOINT(td, KTR_GENIO))
947 ktruio = cloneuio(&auio);
949 len = auio.uio_resid;
950 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
952 if (auio.uio_resid != len && (error == ERESTART ||
953 error == EINTR || error == EWOULDBLOCK))
955 /* Generation of SIGPIPE can be controlled per socket */
956 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
957 !(flags & MSG_NOSIGNAL)) {
958 PROC_LOCK(td->td_proc);
959 tdsignal(td, SIGPIPE);
960 PROC_UNLOCK(td->td_proc);
964 td->td_retval[0] = len - auio.uio_resid;
966 if (ktruio != NULL) {
967 ktruio->uio_resid = td->td_retval[0];
968 ktrgenio(s, UIO_WRITE, ktruio, error);
979 struct sendto_args /* {
991 msg.msg_name = uap->to;
992 msg.msg_namelen = uap->tolen;
996 #ifdef COMPAT_OLDSOCK
999 aiov.iov_base = uap->buf;
1000 aiov.iov_len = uap->len;
1001 return (sendit(td, uap->s, &msg, uap->flags));
1004 #ifdef COMPAT_OLDSOCK
1008 struct osend_args /* {
1019 msg.msg_namelen = 0;
1020 msg.msg_iov = &aiov;
1022 aiov.iov_base = uap->buf;
1023 aiov.iov_len = uap->len;
1024 msg.msg_control = 0;
1026 return (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;
1094 struct mbuf *m, *control = NULL;
1098 struct sockaddr *fromsa = NULL;
1099 cap_rights_t rights;
1101 struct uio *ktruio = NULL;
1106 if (controlp != NULL)
1110 error = getsock_cap(td->td_proc->p_fd, s,
1111 cap_rights_init(&rights, CAP_RECV), &fp, NULL);
1117 error = mac_socket_check_receive(td->td_ucred, so);
1124 auio.uio_iov = mp->msg_iov;
1125 auio.uio_iovcnt = mp->msg_iovlen;
1126 auio.uio_segflg = UIO_USERSPACE;
1127 auio.uio_rw = UIO_READ;
1129 auio.uio_offset = 0; /* XXX */
1132 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
1133 if ((auio.uio_resid += iov->iov_len) < 0) {
1139 if (KTRPOINT(td, KTR_GENIO))
1140 ktruio = cloneuio(&auio);
1142 len = auio.uio_resid;
1143 error = soreceive(so, &fromsa, &auio, NULL,
1144 (mp->msg_control || controlp) ? &control : NULL,
1147 if (auio.uio_resid != len && (error == ERESTART ||
1148 error == EINTR || error == EWOULDBLOCK))
1152 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
1154 if (ktruio != NULL) {
1155 ktruio->uio_resid = len - auio.uio_resid;
1156 ktrgenio(s, UIO_READ, ktruio, error);
1161 td->td_retval[0] = len - auio.uio_resid;
1163 len = mp->msg_namelen;
1164 if (len <= 0 || fromsa == NULL)
1167 /* save sa_len before it is destroyed by MSG_COMPAT */
1168 len = MIN(len, fromsa->sa_len);
1169 #ifdef COMPAT_OLDSOCK
1170 if (mp->msg_flags & MSG_COMPAT)
1171 ((struct osockaddr *)fromsa)->sa_family =
1174 if (fromseg == UIO_USERSPACE) {
1175 error = copyout(fromsa, mp->msg_name,
1180 bcopy(fromsa, mp->msg_name, len);
1182 mp->msg_namelen = len;
1184 if (mp->msg_control && controlp == NULL) {
1185 #ifdef COMPAT_OLDSOCK
1187 * We assume that old recvmsg calls won't receive access
1188 * rights and other control info, esp. as control info
1189 * is always optional and those options didn't exist in 4.3.
1190 * If we receive rights, trim the cmsghdr; anything else
1193 if (control && mp->msg_flags & MSG_COMPAT) {
1194 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1196 mtod(control, struct cmsghdr *)->cmsg_type !=
1198 mp->msg_controllen = 0;
1201 control->m_len -= sizeof (struct cmsghdr);
1202 control->m_data += sizeof (struct cmsghdr);
1205 len = mp->msg_controllen;
1207 mp->msg_controllen = 0;
1208 ctlbuf = mp->msg_control;
1210 while (m && len > 0) {
1211 unsigned int tocopy;
1213 if (len >= m->m_len)
1216 mp->msg_flags |= MSG_CTRUNC;
1220 if ((error = copyout(mtod(m, caddr_t),
1221 ctlbuf, tocopy)) != 0)
1228 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1233 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1234 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);
1258 if (namelenp != 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 /* {
1331 msg.msg_namelen = 0;
1332 msg.msg_iov = &aiov;
1334 aiov.iov_base = uap->buf;
1335 aiov.iov_len = uap->len;
1336 msg.msg_control = 0;
1337 msg.msg_flags = uap->flags;
1338 return (recvit(td, uap->s, &msg, NULL));
1342 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1343 * overlays the new one, missing only the flags, and with the (old) access
1344 * rights where the control fields are now.
1349 struct orecvmsg_args /* {
1351 struct omsghdr *msg;
1359 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1362 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1365 msg.msg_flags = uap->flags | MSG_COMPAT;
1367 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1368 if (msg.msg_controllen && error == 0)
1369 error = copyout(&msg.msg_controllen,
1370 &uap->msg->msg_accrightslen, sizeof (int));
1377 sys_recvmsg(td, uap)
1379 struct recvmsg_args /* {
1386 struct iovec *uiov, *iov;
1389 error = copyin(uap->msg, &msg, sizeof (msg));
1392 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1395 msg.msg_flags = uap->flags;
1396 #ifdef COMPAT_OLDSOCK
1397 msg.msg_flags &= ~MSG_COMPAT;
1401 error = recvit(td, uap->s, &msg, NULL);
1404 error = copyout(&msg, uap->msg, sizeof(msg));
1412 sys_shutdown(td, uap)
1414 struct shutdown_args /* {
1421 cap_rights_t rights;
1424 AUDIT_ARG_FD(uap->s);
1425 error = getsock_cap(td->td_proc->p_fd, uap->s,
1426 cap_rights_init(&rights, CAP_SHUTDOWN), &fp, NULL);
1429 error = soshutdown(so, uap->how);
1437 sys_setsockopt(td, uap)
1439 struct setsockopt_args /* {
1448 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1449 uap->val, UIO_USERSPACE, uap->valsize));
1453 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1459 enum uio_seg valseg;
1464 struct sockopt sopt;
1465 cap_rights_t rights;
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,
1491 cap_rights_init(&rights, CAP_SETSOCKOPT), &fp, NULL);
1494 error = sosetopt(so, &sopt);
1502 sys_getsockopt(td, uap)
1504 struct getsockopt_args /* {
1508 void * __restrict val;
1509 socklen_t * __restrict avalsize;
1516 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1521 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1522 uap->val, UIO_USERSPACE, &valsize);
1525 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1530 * Kernel version of getsockopt.
1531 * optval can be a userland or userspace. optlen is always a kernel pointer.
1534 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1540 enum uio_seg valseg;
1545 struct sockopt sopt;
1546 cap_rights_t rights;
1551 if ((int)*valsize < 0)
1554 sopt.sopt_dir = SOPT_GET;
1555 sopt.sopt_level = level;
1556 sopt.sopt_name = name;
1557 sopt.sopt_val = val;
1558 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1564 sopt.sopt_td = NULL;
1567 panic("kern_getsockopt called with bad valseg");
1571 error = getsock_cap(td->td_proc->p_fd, s,
1572 cap_rights_init(&rights, CAP_GETSOCKOPT), &fp, NULL);
1575 error = sogetopt(so, &sopt);
1576 *valsize = sopt.sopt_valsize;
1583 * getsockname1() - Get socket name.
1587 getsockname1(td, uap, compat)
1589 struct getsockname_args /* {
1591 struct sockaddr * __restrict asa;
1592 socklen_t * __restrict alen;
1596 struct sockaddr *sa;
1600 error = copyin(uap->alen, &len, sizeof(len));
1604 error = kern_getsockname(td, uap->fdes, &sa, &len);
1609 #ifdef COMPAT_OLDSOCK
1611 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1613 error = copyout(sa, uap->asa, (u_int)len);
1617 error = copyout(&len, uap->alen, sizeof(len));
1622 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1627 cap_rights_t rights;
1632 error = getsock_cap(td->td_proc->p_fd, fd,
1633 cap_rights_init(&rights, CAP_GETSOCKNAME), &fp, NULL);
1638 CURVNET_SET(so->so_vnet);
1639 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1646 len = MIN(*alen, (*sa)->sa_len);
1649 if (KTRPOINT(td, KTR_STRUCT))
1654 if (error != 0 && *sa != NULL) {
1655 free(*sa, M_SONAME);
1662 sys_getsockname(td, uap)
1664 struct getsockname_args *uap;
1667 return (getsockname1(td, uap, 0));
1670 #ifdef COMPAT_OLDSOCK
1672 ogetsockname(td, uap)
1674 struct getsockname_args *uap;
1677 return (getsockname1(td, uap, 1));
1679 #endif /* COMPAT_OLDSOCK */
1682 * getpeername1() - Get name of peer for connected socket.
1686 getpeername1(td, uap, compat)
1688 struct getpeername_args /* {
1690 struct sockaddr * __restrict asa;
1691 socklen_t * __restrict alen;
1695 struct sockaddr *sa;
1699 error = copyin(uap->alen, &len, sizeof (len));
1703 error = kern_getpeername(td, uap->fdes, &sa, &len);
1708 #ifdef COMPAT_OLDSOCK
1710 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1712 error = copyout(sa, uap->asa, (u_int)len);
1716 error = copyout(&len, uap->alen, sizeof(len));
1721 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1726 cap_rights_t rights;
1731 error = getsock_cap(td->td_proc->p_fd, fd,
1732 cap_rights_init(&rights, CAP_GETPEERNAME), &fp, NULL);
1736 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1741 CURVNET_SET(so->so_vnet);
1742 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1749 len = MIN(*alen, (*sa)->sa_len);
1752 if (KTRPOINT(td, KTR_STRUCT))
1756 if (error != 0 && *sa != NULL) {
1757 free(*sa, M_SONAME);
1766 sys_getpeername(td, uap)
1768 struct getpeername_args *uap;
1771 return (getpeername1(td, uap, 0));
1774 #ifdef COMPAT_OLDSOCK
1776 ogetpeername(td, uap)
1778 struct ogetpeername_args *uap;
1781 /* XXX uap should have type `getpeername_args *' to begin with. */
1782 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1784 #endif /* COMPAT_OLDSOCK */
1787 sockargs(mp, buf, buflen, type)
1792 struct sockaddr *sa;
1796 if (buflen > MLEN) {
1797 #ifdef COMPAT_OLDSOCK
1798 if (type == MT_SONAME && buflen <= 112)
1799 buflen = MLEN; /* unix domain compat. hack */
1802 if (buflen > MCLBYTES)
1805 m = m_get2(buflen, M_WAITOK, type, 0);
1807 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1812 if (type == MT_SONAME) {
1813 sa = mtod(m, struct sockaddr *);
1815 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1816 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1817 sa->sa_family = sa->sa_len;
1819 sa->sa_len = buflen;
1826 getsockaddr(namp, uaddr, len)
1827 struct sockaddr **namp;
1831 struct sockaddr *sa;
1834 if (len > SOCK_MAXADDRLEN)
1835 return (ENAMETOOLONG);
1836 if (len < offsetof(struct sockaddr, sa_data[0]))
1838 sa = malloc(len, M_SONAME, M_WAITOK);
1839 error = copyin(uaddr, sa, len);
1843 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1844 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1845 sa->sa_family = sa->sa_len;
1853 #include <sys/condvar.h>
1855 struct sendfile_sync {
1862 * Detach mapped page and release resources back to the system.
1865 sf_buf_mext(struct mbuf *mb, void *addr, void *args)
1868 struct sendfile_sync *sfs;
1870 m = sf_buf_page(args);
1873 vm_page_unwire(m, 0);
1875 * Check for the object going away on us. This can
1876 * happen since we don't hold a reference to it.
1877 * If so, we're responsible for freeing the page.
1879 if (m->wire_count == 0 && m->object == NULL)
1883 return (EXT_FREE_OK);
1885 mtx_lock(&sfs->mtx);
1886 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1887 if (--sfs->count == 0)
1888 cv_signal(&sfs->cv);
1889 mtx_unlock(&sfs->mtx);
1890 return (EXT_FREE_OK);
1896 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1897 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1899 * Send a file specified by 'fd' and starting at 'offset' to a socket
1900 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1901 * 0. Optionally add a header and/or trailer to the socket output. If
1902 * specified, write the total number of bytes sent into *sbytes.
1905 sys_sendfile(struct thread *td, struct sendfile_args *uap)
1908 return (do_sendfile(td, uap, 0));
1912 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1914 struct sf_hdtr hdtr;
1915 struct uio *hdr_uio, *trl_uio;
1917 cap_rights_t rights;
1920 if (uap->offset < 0)
1923 hdr_uio = trl_uio = NULL;
1925 if (uap->hdtr != NULL) {
1926 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1929 if (hdtr.headers != NULL) {
1930 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1934 if (hdtr.trailers != NULL) {
1935 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1942 AUDIT_ARG_FD(uap->fd);
1945 * sendfile(2) can start at any offset within a file so we require
1946 * CAP_READ+CAP_SEEK = CAP_PREAD.
1948 if ((error = fget_read(td, uap->fd,
1949 cap_rights_init(&rights, CAP_PREAD), &fp)) != 0) {
1953 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, uap->offset,
1954 uap->nbytes, uap->sbytes, uap->flags, compat ? SFK_COMPAT : 0, td);
1958 free(hdr_uio, M_IOV);
1959 free(trl_uio, M_IOV);
1963 #ifdef COMPAT_FREEBSD4
1965 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1967 struct sendfile_args args;
1971 args.offset = uap->offset;
1972 args.nbytes = uap->nbytes;
1973 args.hdtr = uap->hdtr;
1974 args.sbytes = uap->sbytes;
1975 args.flags = uap->flags;
1977 return (do_sendfile(td, &args, 1));
1979 #endif /* COMPAT_FREEBSD4 */
1982 vn_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio,
1983 struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags,
1984 int kflags, struct thread *td)
1986 struct vnode *vp = fp->f_vnode;
1987 struct file *sock_fp;
1988 struct vm_object *obj = NULL;
1989 struct socket *so = NULL;
1990 struct mbuf *m = NULL;
1994 struct sendfile_sync *sfs = NULL;
1995 cap_rights_t rights;
1996 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1997 int bsize, error, hdrlen = 0, mnw = 0;
1999 vn_lock(vp, LK_SHARED | LK_RETRY);
2000 if (vp->v_type == VREG) {
2001 bsize = vp->v_mount->mnt_stat.f_iosize;
2003 error = VOP_GETATTR(vp, &va, td->td_ucred);
2015 * Temporarily increase the backing VM
2016 * object's reference count so that a forced
2017 * reclamation of its vnode does not
2018 * immediately destroy it.
2020 VM_OBJECT_WLOCK(obj);
2021 if ((obj->flags & OBJ_DEAD) == 0) {
2022 vm_object_reference_locked(obj);
2023 VM_OBJECT_WUNLOCK(obj);
2025 VM_OBJECT_WUNLOCK(obj);
2030 bsize = 0; /* silence gcc */
2038 * The socket must be a stream socket and connected.
2039 * Remember if it a blocking or non-blocking socket.
2041 error = getsock_cap(td->td_proc->p_fd, sockfd,
2042 cap_rights_init(&rights, CAP_SEND), &sock_fp, NULL);
2045 so = sock_fp->f_data;
2046 if (so->so_type != SOCK_STREAM) {
2050 if ((so->so_state & SS_ISCONNECTED) == 0) {
2055 * Do not wait on memory allocations but return ENOMEM for
2056 * caller to retry later.
2057 * XXX: Experimental.
2059 if (flags & SF_MNOWAIT)
2062 if (flags & SF_SYNC) {
2063 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO);
2064 mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
2065 cv_init(&sfs->cv, "sendfile");
2069 error = mac_socket_check_send(td->td_ucred, so);
2074 /* If headers are specified copy them into mbufs. */
2075 if (hdr_uio != NULL) {
2076 hdr_uio->uio_td = td;
2077 hdr_uio->uio_rw = UIO_WRITE;
2078 if (hdr_uio->uio_resid > 0) {
2080 * In FBSD < 5.0 the nbytes to send also included
2081 * the header. If compat is specified subtract the
2082 * header size from nbytes.
2084 if (kflags & SFK_COMPAT) {
2085 if (nbytes > hdr_uio->uio_resid)
2086 nbytes -= hdr_uio->uio_resid;
2090 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
2093 error = mnw ? EAGAIN : ENOBUFS;
2096 hdrlen = m_length(m, NULL);
2101 * Protect against multiple writers to the socket.
2103 * XXXRW: Historically this has assumed non-interruptibility, so now
2104 * we implement that, but possibly shouldn't.
2106 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
2109 * Loop through the pages of the file, starting with the requested
2110 * offset. Get a file page (do I/O if necessary), map the file page
2111 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
2113 * This is done in two loops. The inner loop turns as many pages
2114 * as it can, up to available socket buffer space, without blocking
2115 * into mbufs to have it bulk delivered into the socket send buffer.
2116 * The outer loop checks the state and available space of the socket
2117 * and takes care of the overall progress.
2119 for (off = offset; ; ) {
2125 if ((nbytes != 0 && nbytes == fsbytes) ||
2126 (nbytes == 0 && va.va_size == fsbytes))
2135 * Check the socket state for ongoing connection,
2136 * no errors and space in socket buffer.
2137 * If space is low allow for the remainder of the
2138 * file to be processed if it fits the socket buffer.
2139 * Otherwise block in waiting for sufficient space
2140 * to proceed, or if the socket is nonblocking, return
2141 * to userland with EAGAIN while reporting how far
2143 * We wait until the socket buffer has significant free
2144 * space to do bulk sends. This makes good use of file
2145 * system read ahead and allows packet segmentation
2146 * offloading hardware to take over lots of work. If
2147 * we were not careful here we would send off only one
2150 SOCKBUF_LOCK(&so->so_snd);
2151 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
2152 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
2154 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2156 SOCKBUF_UNLOCK(&so->so_snd);
2158 } else if (so->so_error) {
2159 error = so->so_error;
2161 SOCKBUF_UNLOCK(&so->so_snd);
2164 space = sbspace(&so->so_snd);
2167 space < so->so_snd.sb_lowat)) {
2168 if (so->so_state & SS_NBIO) {
2169 SOCKBUF_UNLOCK(&so->so_snd);
2174 * sbwait drops the lock while sleeping.
2175 * When we loop back to retry_space the
2176 * state may have changed and we retest
2179 error = sbwait(&so->so_snd);
2181 * An error from sbwait usually indicates that we've
2182 * been interrupted by a signal. If we've sent anything
2183 * then return bytes sent, otherwise return the error.
2186 SOCKBUF_UNLOCK(&so->so_snd);
2191 SOCKBUF_UNLOCK(&so->so_snd);
2194 * Reduce space in the socket buffer by the size of
2195 * the header mbuf chain.
2196 * hdrlen is set to 0 after the first loop.
2200 error = vn_lock(vp, LK_SHARED);
2203 error = VOP_GETATTR(vp, &va, td->td_ucred);
2204 if (error != 0 || off >= va.va_size) {
2210 * Loop and construct maximum sized mbuf chain to be bulk
2211 * dumped into socket buffer.
2213 while (space > loopbytes) {
2219 * Calculate the amount to transfer.
2220 * Not to exceed a page, the EOF,
2221 * or the passed in nbytes.
2223 pgoff = (vm_offset_t)(off & PAGE_MASK);
2225 rem = (nbytes - fsbytes - loopbytes);
2228 offset - fsbytes - loopbytes;
2229 xfsize = omin(PAGE_SIZE - pgoff, rem);
2230 xfsize = omin(space - loopbytes, xfsize);
2232 done = 1; /* all data sent */
2237 * Attempt to look up the page. Allocate
2238 * if not found or wait and loop if busy.
2240 pindex = OFF_TO_IDX(off);
2241 VM_OBJECT_WLOCK(obj);
2242 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2243 VM_ALLOC_IGN_SBUSY | VM_ALLOC_NORMAL |
2247 * Check if page is valid for what we need,
2248 * otherwise initiate I/O.
2249 * If we already turned some pages into mbufs,
2250 * send them off before we come here again and
2253 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2254 VM_OBJECT_WUNLOCK(obj);
2256 error = EAGAIN; /* send what we already got */
2257 else if (flags & SF_NODISKIO)
2261 int readahead = sfreadahead * MAXBSIZE;
2263 VM_OBJECT_WUNLOCK(obj);
2266 * Get the page from backing store.
2267 * XXXMAC: Because we don't have fp->f_cred
2268 * here, we pass in NOCRED. This is probably
2269 * wrong, but is consistent with our original
2272 error = vn_rdwr(UIO_READ, vp, NULL, readahead,
2273 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2274 IO_VMIO | ((readahead / bsize) << IO_SEQSHIFT),
2275 td->td_ucred, NOCRED, &resid, td);
2276 SFSTAT_INC(sf_iocnt);
2278 VM_OBJECT_WLOCK(obj);
2282 vm_page_unwire(pg, 0);
2284 * See if anyone else might know about
2285 * this page. If not and it is not valid,
2288 if (pg->wire_count == 0 && pg->valid == 0 &&
2289 !vm_page_busied(pg))
2292 VM_OBJECT_WUNLOCK(obj);
2293 if (error == EAGAIN)
2294 error = 0; /* not a real error */
2299 * Get a sendfile buf. When allocating the
2300 * first buffer for mbuf chain, we usually
2301 * wait as long as necessary, but this wait
2302 * can be interrupted. For consequent
2303 * buffers, do not sleep, since several
2304 * threads might exhaust the buffers and then
2307 sf = sf_buf_alloc(pg, (mnw || m != NULL) ? SFB_NOWAIT :
2310 SFSTAT_INC(sf_allocfail);
2312 vm_page_unwire(pg, 0);
2313 KASSERT(pg->object != NULL,
2314 ("%s: object disappeared", __func__));
2317 error = (mnw ? EAGAIN : EINTR);
2322 * Get an mbuf and set it up as having
2325 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2327 error = (mnw ? EAGAIN : ENOBUFS);
2328 (void)sf_buf_mext(NULL, NULL, sf);
2331 if (m_extadd(m0, (caddr_t )sf_buf_kva(sf), PAGE_SIZE,
2332 sf_buf_mext, sfs, sf, M_RDONLY, EXT_SFBUF,
2333 (mnw ? M_NOWAIT : M_WAITOK)) != 0) {
2334 error = (mnw ? EAGAIN : ENOBUFS);
2335 (void)sf_buf_mext(NULL, NULL, sf);
2339 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2342 /* Append to mbuf chain. */
2346 m_last(m)->m_next = m0;
2351 /* Keep track of bits processed. */
2352 loopbytes += xfsize;
2356 mtx_lock(&sfs->mtx);
2358 mtx_unlock(&sfs->mtx);
2364 /* Add the buffer chain to the socket buffer. */
2368 mlen = m_length(m, NULL);
2369 SOCKBUF_LOCK(&so->so_snd);
2370 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2372 SOCKBUF_UNLOCK(&so->so_snd);
2375 SOCKBUF_UNLOCK(&so->so_snd);
2376 CURVNET_SET(so->so_vnet);
2377 /* Avoid error aliasing. */
2378 err = (*so->so_proto->pr_usrreqs->pru_send)
2379 (so, 0, m, NULL, NULL, td);
2383 * We need two counters to get the
2384 * file offset and nbytes to send
2386 * - sbytes contains the total amount
2387 * of bytes sent, including headers.
2388 * - fsbytes contains the total amount
2389 * of bytes sent from the file.
2397 } else if (error == 0)
2399 m = NULL; /* pru_send always consumes */
2402 /* Quit outer loop on error or when we're done. */
2410 * Send trailers. Wimp out and use writev(2).
2412 if (trl_uio != NULL) {
2413 sbunlock(&so->so_snd);
2414 error = kern_writev(td, sockfd, trl_uio);
2416 sbytes += td->td_retval[0];
2421 sbunlock(&so->so_snd);
2424 * If there was no error we have to clear td->td_retval[0]
2425 * because it may have been set by writev.
2428 td->td_retval[0] = 0;
2431 copyout(&sbytes, sent, sizeof(off_t));
2434 vm_object_deallocate(obj);
2441 mtx_lock(&sfs->mtx);
2442 if (sfs->count != 0)
2443 cv_wait(&sfs->cv, &sfs->mtx);
2444 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2445 cv_destroy(&sfs->cv);
2446 mtx_destroy(&sfs->mtx);
2450 if (error == ERESTART)
2458 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2459 * otherwise all return EOPNOTSUPP.
2460 * XXX: We should make this loadable one day.
2463 sys_sctp_peeloff(td, uap)
2465 struct sctp_peeloff_args /* {
2470 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2471 struct file *nfp = NULL;
2472 struct socket *head, *so;
2473 cap_rights_t rights;
2477 AUDIT_ARG_FD(uap->sd);
2478 error = fgetsock(td, uap->sd, cap_rights_init(&rights, CAP_PEELOFF),
2482 if (head->so_proto->pr_protocol != IPPROTO_SCTP) {
2486 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2490 * At this point we know we do have a assoc to pull
2491 * we proceed to get the fd setup. This may block
2495 error = falloc(td, &nfp, &fd, 0);
2498 td->td_retval[0] = fd;
2500 CURVNET_SET(head->so_vnet);
2501 so = sonewconn(head, SS_ISCONNECTED);
2507 * Before changing the flags on the socket, we have to bump the
2508 * reference count. Otherwise, if the protocol calls sofree(),
2509 * the socket will be released due to a zero refcount.
2512 soref(so); /* file descriptor reference */
2517 TAILQ_REMOVE(&head->so_comp, so, so_list);
2519 so->so_state |= (head->so_state & SS_NBIO);
2520 so->so_state &= ~SS_NOFDREF;
2521 so->so_qstate &= ~SQ_COMP;
2524 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2525 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2528 if (head->so_sigio != NULL)
2529 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2533 * close the new descriptor, assuming someone hasn't ripped it
2534 * out from under us.
2537 fdclose(td->td_proc->p_fd, nfp, fd, td);
2540 * Release explicitly held references before returning.
2550 return (EOPNOTSUPP);
2555 sys_sctp_generic_sendmsg (td, uap)
2557 struct sctp_generic_sendmsg_args /* {
2563 struct sctp_sndrcvinfo *sinfo,
2567 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2568 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2570 struct file *fp = NULL;
2571 struct sockaddr *to = NULL;
2573 struct uio *ktruio = NULL;
2576 struct iovec iov[1];
2577 cap_rights_t rights;
2580 if (uap->sinfo != NULL) {
2581 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2587 cap_rights_init(&rights, CAP_SEND);
2588 if (uap->tolen != 0) {
2589 error = getsockaddr(&to, uap->to, uap->tolen);
2594 cap_rights_set(&rights, CAP_CONNECT);
2597 AUDIT_ARG_FD(uap->sd);
2598 error = getsock_cap(td->td_proc->p_fd, uap->sd, &rights, &fp, NULL);
2602 if (to && (KTRPOINT(td, KTR_STRUCT)))
2606 iov[0].iov_base = uap->msg;
2607 iov[0].iov_len = uap->mlen;
2609 so = (struct socket *)fp->f_data;
2610 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2615 error = mac_socket_check_send(td->td_ucred, so);
2621 auio.uio_iovcnt = 1;
2622 auio.uio_segflg = UIO_USERSPACE;
2623 auio.uio_rw = UIO_WRITE;
2625 auio.uio_offset = 0; /* XXX */
2627 len = auio.uio_resid = uap->mlen;
2628 CURVNET_SET(so->so_vnet);
2629 error = sctp_lower_sosend(so, to, &auio, (struct mbuf *)NULL,
2630 (struct mbuf *)NULL, uap->flags, u_sinfo, td);
2633 if (auio.uio_resid != len && (error == ERESTART ||
2634 error == EINTR || error == EWOULDBLOCK))
2636 /* Generation of SIGPIPE can be controlled per socket. */
2637 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2638 !(uap->flags & MSG_NOSIGNAL)) {
2639 PROC_LOCK(td->td_proc);
2640 tdsignal(td, SIGPIPE);
2641 PROC_UNLOCK(td->td_proc);
2645 td->td_retval[0] = len - auio.uio_resid;
2647 if (ktruio != NULL) {
2648 ktruio->uio_resid = td->td_retval[0];
2649 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2659 return (EOPNOTSUPP);
2664 sys_sctp_generic_sendmsg_iov(td, uap)
2666 struct sctp_generic_sendmsg_iov_args /* {
2672 struct sctp_sndrcvinfo *sinfo,
2676 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2677 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2679 struct file *fp = NULL;
2680 struct sockaddr *to = NULL;
2682 struct uio *ktruio = NULL;
2685 struct iovec *iov, *tiov;
2686 cap_rights_t rights;
2690 if (uap->sinfo != NULL) {
2691 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2696 cap_rights_init(&rights, CAP_SEND);
2697 if (uap->tolen != 0) {
2698 error = getsockaddr(&to, uap->to, uap->tolen);
2703 cap_rights_set(&rights, CAP_CONNECT);
2706 AUDIT_ARG_FD(uap->sd);
2707 error = getsock_cap(td->td_proc->p_fd, uap->sd, &rights, &fp, NULL);
2711 #ifdef COMPAT_FREEBSD32
2712 if (SV_CURPROC_FLAG(SV_ILP32))
2713 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2714 uap->iovlen, &iov, EMSGSIZE);
2717 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2721 if (to && (KTRPOINT(td, KTR_STRUCT)))
2725 so = (struct socket *)fp->f_data;
2726 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2731 error = mac_socket_check_send(td->td_ucred, so);
2737 auio.uio_iovcnt = uap->iovlen;
2738 auio.uio_segflg = UIO_USERSPACE;
2739 auio.uio_rw = UIO_WRITE;
2741 auio.uio_offset = 0; /* XXX */
2744 for (i = 0; i <uap->iovlen; i++, tiov++) {
2745 if ((auio.uio_resid += tiov->iov_len) < 0) {
2750 len = auio.uio_resid;
2751 CURVNET_SET(so->so_vnet);
2752 error = sctp_lower_sosend(so, to, &auio,
2753 (struct mbuf *)NULL, (struct mbuf *)NULL,
2754 uap->flags, u_sinfo, td);
2757 if (auio.uio_resid != len && (error == ERESTART ||
2758 error == EINTR || error == EWOULDBLOCK))
2760 /* Generation of SIGPIPE can be controlled per socket */
2761 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2762 !(uap->flags & MSG_NOSIGNAL)) {
2763 PROC_LOCK(td->td_proc);
2764 tdsignal(td, SIGPIPE);
2765 PROC_UNLOCK(td->td_proc);
2769 td->td_retval[0] = len - auio.uio_resid;
2771 if (ktruio != NULL) {
2772 ktruio->uio_resid = td->td_retval[0];
2773 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2785 return (EOPNOTSUPP);
2790 sys_sctp_generic_recvmsg(td, uap)
2792 struct sctp_generic_recvmsg_args /* {
2796 struct sockaddr *from,
2797 __socklen_t *fromlenaddr,
2798 struct sctp_sndrcvinfo *sinfo,
2802 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2803 uint8_t sockbufstore[256];
2805 struct iovec *iov, *tiov;
2806 struct sctp_sndrcvinfo sinfo;
2808 struct file *fp = NULL;
2809 struct sockaddr *fromsa;
2810 cap_rights_t rights;
2812 struct uio *ktruio = NULL;
2815 int error, fromlen, i, msg_flags;
2817 AUDIT_ARG_FD(uap->sd);
2818 error = getsock_cap(td->td_proc->p_fd, uap->sd,
2819 cap_rights_init(&rights, CAP_RECV), &fp, NULL);
2822 #ifdef COMPAT_FREEBSD32
2823 if (SV_CURPROC_FLAG(SV_ILP32))
2824 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2825 uap->iovlen, &iov, EMSGSIZE);
2828 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2833 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2838 error = mac_socket_check_receive(td->td_ucred, so);
2843 if (uap->fromlenaddr != NULL) {
2844 error = copyin(uap->fromlenaddr, &fromlen, sizeof (fromlen));
2850 if (uap->msg_flags) {
2851 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2858 auio.uio_iovcnt = uap->iovlen;
2859 auio.uio_segflg = UIO_USERSPACE;
2860 auio.uio_rw = UIO_READ;
2862 auio.uio_offset = 0; /* XXX */
2865 for (i = 0; i <uap->iovlen; i++, tiov++) {
2866 if ((auio.uio_resid += tiov->iov_len) < 0) {
2871 len = auio.uio_resid;
2872 fromsa = (struct sockaddr *)sockbufstore;
2875 if (KTRPOINT(td, KTR_GENIO))
2876 ktruio = cloneuio(&auio);
2878 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
2879 CURVNET_SET(so->so_vnet);
2880 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2881 fromsa, fromlen, &msg_flags,
2882 (struct sctp_sndrcvinfo *)&sinfo, 1);
2885 if (auio.uio_resid != len && (error == ERESTART ||
2886 error == EINTR || error == EWOULDBLOCK))
2890 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2893 if (ktruio != NULL) {
2894 ktruio->uio_resid = len - auio.uio_resid;
2895 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2900 td->td_retval[0] = len - auio.uio_resid;
2902 if (fromlen && uap->from) {
2904 if (len <= 0 || fromsa == 0)
2907 len = MIN(len, fromsa->sa_len);
2908 error = copyout(fromsa, uap->from, (size_t)len);
2912 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2917 if (KTRPOINT(td, KTR_STRUCT))
2918 ktrsockaddr(fromsa);
2920 if (uap->msg_flags) {
2921 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2933 return (EOPNOTSUPP);