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, int compat);
113 static int getsockname1(struct thread *td, struct getsockname_args *uap,
115 static int getpeername1(struct thread *td, struct getpeername_args *uap,
119 * NSFBUFS-related variables and associated sysctls
125 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
126 "Maximum number of sendfile(2) sf_bufs available");
127 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
128 "Number of sendfile(2) sf_bufs at peak usage");
129 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
130 "Number of sendfile(2) sf_bufs in use");
133 * Convert a user file descriptor to a kernel file entry and check if required
134 * capability rights are present.
135 * A reference on the file entry is held upon returning.
138 getsock_cap(struct filedesc *fdp, int fd, cap_rights_t rights,
139 struct file **fpp, u_int *fflagp)
144 error = fget_unlocked(fdp, fd, rights, 0, &fp, NULL);
147 if (fp->f_type != DTYPE_SOCKET) {
148 fdrop(fp, curthread);
152 *fflagp = fp->f_flag;
158 * System call interface to the socket abstraction.
160 #if defined(COMPAT_43)
161 #define COMPAT_OLDSOCK
167 struct socket_args /* {
175 int fd, error, type, oflag, fflag;
177 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
182 if ((type & SOCK_CLOEXEC) != 0) {
183 type &= ~SOCK_CLOEXEC;
186 if ((type & SOCK_NONBLOCK) != 0) {
187 type &= ~SOCK_NONBLOCK;
192 error = mac_socket_check_create(td->td_ucred, uap->domain, type,
197 error = falloc(td, &fp, &fd, oflag);
200 /* An extra reference on `fp' has been held for us by falloc(). */
201 error = socreate(uap->domain, &so, type, uap->protocol,
204 fdclose(td->td_proc->p_fd, fp, fd, td);
206 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
207 if ((fflag & FNONBLOCK) != 0)
208 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
209 td->td_retval[0] = fd;
219 struct bind_args /* {
228 error = getsockaddr(&sa, uap->name, uap->namelen);
230 error = kern_bind(td, uap->s, sa);
237 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
244 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
245 error = getsock_cap(td->td_proc->p_fd, fd, CAP_BIND, &fp, NULL);
250 if (KTRPOINT(td, KTR_STRUCT))
254 error = mac_socket_check_bind(td->td_ucred, so, sa);
257 if (dirfd == AT_FDCWD)
258 error = sobind(so, sa, td);
260 error = sobindat(dirfd, so, sa, td);
269 kern_bind(struct thread *td, int fd, struct sockaddr *sa)
272 return (kern_bindat(td, AT_FDCWD, fd, sa));
279 struct bindat_args /* {
289 error = getsockaddr(&sa, uap->name, uap->namelen);
291 error = kern_bindat(td, uap->fd, uap->s, sa);
301 struct listen_args /* {
310 AUDIT_ARG_FD(uap->s);
311 error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_LISTEN, &fp, NULL);
315 error = mac_socket_check_listen(td->td_ucred, so);
318 error = solisten(so, uap->backlog, td);
328 accept1(td, s, uname, anamelen, flags)
331 struct sockaddr *uname;
335 struct sockaddr *name;
341 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
343 error = copyin(anamelen, &namelen, sizeof (namelen));
347 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
350 * return a namelen of zero for older code which might
351 * ignore the return value from accept.
354 (void) copyout(&namelen, anamelen, sizeof(*anamelen));
358 if (error == 0 && uname != NULL) {
359 #ifdef COMPAT_OLDSOCK
360 if (flags & ACCEPT4_COMPAT)
361 ((struct osockaddr *)name)->sa_family =
364 error = copyout(name, uname, namelen);
367 error = copyout(&namelen, anamelen,
370 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
372 free(name, M_SONAME);
377 kern_accept(struct thread *td, int s, struct sockaddr **name,
378 socklen_t *namelen, struct file **fp)
380 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
384 kern_accept4(struct thread *td, int s, struct sockaddr **name,
385 socklen_t *namelen, int flags, struct file **fp)
387 struct filedesc *fdp;
388 struct file *headfp, *nfp = NULL;
389 struct sockaddr *sa = NULL;
391 struct socket *head, *so;
401 fdp = td->td_proc->p_fd;
402 error = getsock_cap(fdp, s, CAP_ACCEPT, &headfp, &fflag);
405 head = headfp->f_data;
406 if ((head->so_options & SO_ACCEPTCONN) == 0) {
411 error = mac_socket_check_accept(td->td_ucred, head);
415 error = falloc(td, &nfp, &fd, (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0);
419 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
424 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
425 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
426 head->so_error = ECONNABORTED;
429 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
436 if (head->so_error) {
437 error = head->so_error;
442 so = TAILQ_FIRST(&head->so_comp);
443 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
444 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
447 * Before changing the flags on the socket, we have to bump the
448 * reference count. Otherwise, if the protocol calls sofree(),
449 * the socket will be released due to a zero refcount.
451 SOCK_LOCK(so); /* soref() and so_state update */
452 soref(so); /* file descriptor reference */
454 TAILQ_REMOVE(&head->so_comp, so, so_list);
456 if (flags & ACCEPT4_INHERIT)
457 so->so_state |= (head->so_state & SS_NBIO);
459 so->so_state |= (flags & SOCK_NONBLOCK) ? SS_NBIO : 0;
460 so->so_qstate &= ~SQ_COMP;
466 /* An extra reference on `nfp' has been held for us by falloc(). */
467 td->td_retval[0] = fd;
469 /* connection has been removed from the listen queue */
470 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
472 if (flags & ACCEPT4_INHERIT) {
473 pgid = fgetown(&head->so_sigio);
475 fsetown(pgid, &so->so_sigio);
477 fflag &= ~(FNONBLOCK | FASYNC);
478 if (flags & SOCK_NONBLOCK)
482 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
483 /* Sync socket nonblocking/async state with file flags */
484 tmp = fflag & FNONBLOCK;
485 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
486 tmp = fflag & FASYNC;
487 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
489 error = soaccept(so, &sa);
492 * return a namelen of zero for older code which might
493 * ignore the return value from accept.
504 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
506 /* check sa_len before it is destroyed */
507 if (*namelen > sa->sa_len)
508 *namelen = sa->sa_len;
510 if (KTRPOINT(td, KTR_STRUCT))
521 * close the new descriptor, assuming someone hasn't ripped it
525 fdclose(fdp, nfp, fd, td);
528 * Release explicitly held references before returning. We return
529 * a reference on nfp to the caller on success if they request it.
548 struct accept_args *uap;
551 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
557 struct accept4_args *uap;
559 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
562 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
565 #ifdef COMPAT_OLDSOCK
569 struct accept_args *uap;
572 return (accept1(td, uap->s, uap->name, uap->anamelen,
573 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
575 #endif /* COMPAT_OLDSOCK */
581 struct connect_args /* {
590 error = getsockaddr(&sa, uap->name, uap->namelen);
592 error = kern_connect(td, uap->s, sa);
599 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
607 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
608 error = getsock_cap(td->td_proc->p_fd, fd, CAP_CONNECT, &fp, NULL);
612 if (so->so_state & SS_ISCONNECTING) {
617 if (KTRPOINT(td, KTR_STRUCT))
621 error = mac_socket_check_connect(td->td_ucred, so, sa);
625 if (dirfd == AT_FDCWD)
626 error = soconnect(so, sa, td);
628 error = soconnectat(dirfd, so, sa, td);
631 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
636 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
637 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
640 if (error == EINTR || error == ERESTART)
646 error = so->so_error;
652 so->so_state &= ~SS_ISCONNECTING;
653 if (error == ERESTART)
661 kern_connect(struct thread *td, int fd, struct sockaddr *sa)
664 return (kern_connectat(td, AT_FDCWD, fd, sa));
669 sys_connectat(td, uap)
671 struct connectat_args /* {
681 error = getsockaddr(&sa, uap->name, uap->namelen);
683 error = kern_connectat(td, uap->fd, uap->s, sa);
690 kern_socketpair(struct thread *td, int domain, int type, int protocol,
693 struct filedesc *fdp = td->td_proc->p_fd;
694 struct file *fp1, *fp2;
695 struct socket *so1, *so2;
696 int fd, error, oflag, fflag;
698 AUDIT_ARG_SOCKET(domain, type, protocol);
702 if ((type & SOCK_CLOEXEC) != 0) {
703 type &= ~SOCK_CLOEXEC;
706 if ((type & SOCK_NONBLOCK) != 0) {
707 type &= ~SOCK_NONBLOCK;
711 /* We might want to have a separate check for socket pairs. */
712 error = mac_socket_check_create(td->td_ucred, domain, type,
717 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
720 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
723 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
724 error = falloc(td, &fp1, &fd, oflag);
728 fp1->f_data = so1; /* so1 already has ref count */
729 error = falloc(td, &fp2, &fd, oflag);
732 fp2->f_data = so2; /* so2 already has ref count */
734 error = soconnect2(so1, so2);
737 if (type == SOCK_DGRAM) {
739 * Datagram socket connection is asymmetric.
741 error = soconnect2(so2, so1);
745 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
747 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
749 if ((fflag & FNONBLOCK) != 0) {
750 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
751 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
757 fdclose(fdp, fp2, rsv[1], td);
760 fdclose(fdp, fp1, rsv[0], td);
772 sys_socketpair(struct thread *td, struct socketpair_args *uap)
776 error = kern_socketpair(td, uap->domain, uap->type,
780 error = copyout(sv, uap->rsv, 2 * sizeof(int));
782 (void)kern_close(td, sv[0]);
783 (void)kern_close(td, sv[1]);
789 sendit(td, s, mp, flags)
795 struct mbuf *control;
799 #ifdef CAPABILITY_MODE
800 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
804 if (mp->msg_name != NULL) {
805 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
815 if (mp->msg_control) {
816 if (mp->msg_controllen < sizeof(struct cmsghdr)
817 #ifdef COMPAT_OLDSOCK
818 && mp->msg_flags != MSG_COMPAT
824 error = sockargs(&control, mp->msg_control,
825 mp->msg_controllen, MT_CONTROL);
828 #ifdef COMPAT_OLDSOCK
829 if (mp->msg_flags == MSG_COMPAT) {
832 M_PREPEND(control, sizeof(*cm), M_WAITOK);
833 cm = mtod(control, struct cmsghdr *);
834 cm->cmsg_len = control->m_len;
835 cm->cmsg_level = SOL_SOCKET;
836 cm->cmsg_type = SCM_RIGHTS;
843 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
852 kern_sendit(td, s, mp, flags, control, segflg)
857 struct mbuf *control;
868 struct uio *ktruio = NULL;
873 if (mp->msg_name != NULL) {
874 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
875 rights |= CAP_CONNECT;
877 error = getsock_cap(td->td_proc->p_fd, s, rights, &fp, NULL);
880 so = (struct socket *)fp->f_data;
883 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
884 ktrsockaddr(mp->msg_name);
887 if (mp->msg_name != NULL) {
888 error = mac_socket_check_connect(td->td_ucred, so,
893 error = mac_socket_check_send(td->td_ucred, so);
898 auio.uio_iov = mp->msg_iov;
899 auio.uio_iovcnt = mp->msg_iovlen;
900 auio.uio_segflg = segflg;
901 auio.uio_rw = UIO_WRITE;
903 auio.uio_offset = 0; /* XXX */
906 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
907 if ((auio.uio_resid += iov->iov_len) < 0) {
913 if (KTRPOINT(td, KTR_GENIO))
914 ktruio = cloneuio(&auio);
916 len = auio.uio_resid;
917 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
919 if (auio.uio_resid != len && (error == ERESTART ||
920 error == EINTR || error == EWOULDBLOCK))
922 /* Generation of SIGPIPE can be controlled per socket */
923 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
924 !(flags & MSG_NOSIGNAL)) {
925 PROC_LOCK(td->td_proc);
926 tdsignal(td, SIGPIPE);
927 PROC_UNLOCK(td->td_proc);
931 td->td_retval[0] = len - auio.uio_resid;
933 if (ktruio != NULL) {
934 ktruio->uio_resid = td->td_retval[0];
935 ktrgenio(s, UIO_WRITE, ktruio, error);
946 struct sendto_args /* {
959 msg.msg_name = uap->to;
960 msg.msg_namelen = uap->tolen;
964 #ifdef COMPAT_OLDSOCK
967 aiov.iov_base = uap->buf;
968 aiov.iov_len = uap->len;
969 error = sendit(td, uap->s, &msg, uap->flags);
973 #ifdef COMPAT_OLDSOCK
977 struct osend_args /* {
992 aiov.iov_base = uap->buf;
993 aiov.iov_len = uap->len;
996 error = sendit(td, uap->s, &msg, uap->flags);
1003 struct osendmsg_args /* {
1013 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1016 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1020 msg.msg_flags = MSG_COMPAT;
1021 error = sendit(td, uap->s, &msg, uap->flags);
1028 sys_sendmsg(td, uap)
1030 struct sendmsg_args /* {
1040 error = copyin(uap->msg, &msg, sizeof (msg));
1043 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1047 #ifdef COMPAT_OLDSOCK
1050 error = sendit(td, uap->s, &msg, uap->flags);
1056 kern_recvit(td, s, mp, fromseg, controlp)
1060 enum uio_seg fromseg;
1061 struct mbuf **controlp;
1068 struct mbuf *m, *control = NULL;
1072 struct sockaddr *fromsa = NULL;
1074 struct uio *ktruio = NULL;
1077 if (controlp != NULL)
1081 error = getsock_cap(td->td_proc->p_fd, s, CAP_RECV, &fp, NULL);
1087 error = mac_socket_check_receive(td->td_ucred, so);
1094 auio.uio_iov = mp->msg_iov;
1095 auio.uio_iovcnt = mp->msg_iovlen;
1096 auio.uio_segflg = UIO_USERSPACE;
1097 auio.uio_rw = UIO_READ;
1099 auio.uio_offset = 0; /* XXX */
1102 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
1103 if ((auio.uio_resid += iov->iov_len) < 0) {
1109 if (KTRPOINT(td, KTR_GENIO))
1110 ktruio = cloneuio(&auio);
1112 len = auio.uio_resid;
1113 error = soreceive(so, &fromsa, &auio, NULL,
1114 (mp->msg_control || controlp) ? &control : NULL,
1117 if (auio.uio_resid != len && (error == ERESTART ||
1118 error == EINTR || error == EWOULDBLOCK))
1122 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
1124 if (ktruio != NULL) {
1125 ktruio->uio_resid = len - auio.uio_resid;
1126 ktrgenio(s, UIO_READ, ktruio, error);
1131 td->td_retval[0] = len - auio.uio_resid;
1133 len = mp->msg_namelen;
1134 if (len <= 0 || fromsa == NULL)
1137 /* save sa_len before it is destroyed by MSG_COMPAT */
1138 len = MIN(len, fromsa->sa_len);
1139 #ifdef COMPAT_OLDSOCK
1140 if (mp->msg_flags & MSG_COMPAT)
1141 ((struct osockaddr *)fromsa)->sa_family =
1144 if (fromseg == UIO_USERSPACE) {
1145 error = copyout(fromsa, mp->msg_name,
1150 bcopy(fromsa, mp->msg_name, len);
1152 mp->msg_namelen = len;
1154 if (mp->msg_control && controlp == NULL) {
1155 #ifdef COMPAT_OLDSOCK
1157 * We assume that old recvmsg calls won't receive access
1158 * rights and other control info, esp. as control info
1159 * is always optional and those options didn't exist in 4.3.
1160 * If we receive rights, trim the cmsghdr; anything else
1163 if (control && mp->msg_flags & MSG_COMPAT) {
1164 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1166 mtod(control, struct cmsghdr *)->cmsg_type !=
1168 mp->msg_controllen = 0;
1171 control->m_len -= sizeof (struct cmsghdr);
1172 control->m_data += sizeof (struct cmsghdr);
1175 len = mp->msg_controllen;
1177 mp->msg_controllen = 0;
1178 ctlbuf = mp->msg_control;
1180 while (m && len > 0) {
1181 unsigned int tocopy;
1183 if (len >= m->m_len)
1186 mp->msg_flags |= MSG_CTRUNC;
1190 if ((error = copyout(mtod(m, caddr_t),
1191 ctlbuf, tocopy)) != 0)
1198 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1203 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1204 ktrsockaddr(fromsa);
1207 free(fromsa, M_SONAME);
1209 if (error == 0 && controlp != NULL)
1210 *controlp = control;
1218 recvit(td, s, mp, namelenp)
1226 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1230 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1231 #ifdef COMPAT_OLDSOCK
1232 if (mp->msg_flags & MSG_COMPAT)
1233 error = 0; /* old recvfrom didn't check */
1240 sys_recvfrom(td, uap)
1242 struct recvfrom_args /* {
1247 struct sockaddr * __restrict from;
1248 socklen_t * __restrict fromlenaddr;
1255 if (uap->fromlenaddr) {
1256 error = copyin(uap->fromlenaddr,
1257 &msg.msg_namelen, sizeof (msg.msg_namelen));
1261 msg.msg_namelen = 0;
1263 msg.msg_name = uap->from;
1264 msg.msg_iov = &aiov;
1266 aiov.iov_base = uap->buf;
1267 aiov.iov_len = uap->len;
1268 msg.msg_control = 0;
1269 msg.msg_flags = uap->flags;
1270 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1275 #ifdef COMPAT_OLDSOCK
1279 struct recvfrom_args *uap;
1282 uap->flags |= MSG_COMPAT;
1283 return (sys_recvfrom(td, uap));
1287 #ifdef COMPAT_OLDSOCK
1291 struct orecv_args /* {
1303 msg.msg_namelen = 0;
1304 msg.msg_iov = &aiov;
1306 aiov.iov_base = uap->buf;
1307 aiov.iov_len = uap->len;
1308 msg.msg_control = 0;
1309 msg.msg_flags = uap->flags;
1310 error = recvit(td, uap->s, &msg, NULL);
1315 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1316 * overlays the new one, missing only the flags, and with the (old) access
1317 * rights where the control fields are now.
1322 struct orecvmsg_args /* {
1324 struct omsghdr *msg;
1332 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1335 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1338 msg.msg_flags = uap->flags | MSG_COMPAT;
1340 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1341 if (msg.msg_controllen && error == 0)
1342 error = copyout(&msg.msg_controllen,
1343 &uap->msg->msg_accrightslen, sizeof (int));
1350 sys_recvmsg(td, uap)
1352 struct recvmsg_args /* {
1359 struct iovec *uiov, *iov;
1362 error = copyin(uap->msg, &msg, sizeof (msg));
1365 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1368 msg.msg_flags = uap->flags;
1369 #ifdef COMPAT_OLDSOCK
1370 msg.msg_flags &= ~MSG_COMPAT;
1374 error = recvit(td, uap->s, &msg, NULL);
1377 error = copyout(&msg, uap->msg, sizeof(msg));
1385 sys_shutdown(td, uap)
1387 struct shutdown_args /* {
1396 AUDIT_ARG_FD(uap->s);
1397 error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_SHUTDOWN, &fp,
1401 error = soshutdown(so, uap->how);
1409 sys_setsockopt(td, uap)
1411 struct setsockopt_args /* {
1420 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1421 uap->val, UIO_USERSPACE, uap->valsize));
1425 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1431 enum uio_seg valseg;
1437 struct sockopt sopt;
1439 if (val == NULL && valsize != 0)
1441 if ((int)valsize < 0)
1444 sopt.sopt_dir = SOPT_SET;
1445 sopt.sopt_level = level;
1446 sopt.sopt_name = name;
1447 sopt.sopt_val = val;
1448 sopt.sopt_valsize = valsize;
1454 sopt.sopt_td = NULL;
1457 panic("kern_setsockopt called with bad valseg");
1461 error = getsock_cap(td->td_proc->p_fd, s, CAP_SETSOCKOPT, &fp, NULL);
1464 error = sosetopt(so, &sopt);
1472 sys_getsockopt(td, uap)
1474 struct getsockopt_args /* {
1478 void * __restrict val;
1479 socklen_t * __restrict avalsize;
1486 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1491 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1492 uap->val, UIO_USERSPACE, &valsize);
1495 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1500 * Kernel version of getsockopt.
1501 * optval can be a userland or userspace. optlen is always a kernel pointer.
1504 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1510 enum uio_seg valseg;
1516 struct sockopt sopt;
1520 if ((int)*valsize < 0)
1523 sopt.sopt_dir = SOPT_GET;
1524 sopt.sopt_level = level;
1525 sopt.sopt_name = name;
1526 sopt.sopt_val = val;
1527 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1533 sopt.sopt_td = NULL;
1536 panic("kern_getsockopt called with bad valseg");
1540 error = getsock_cap(td->td_proc->p_fd, s, CAP_GETSOCKOPT, &fp, NULL);
1543 error = sogetopt(so, &sopt);
1544 *valsize = sopt.sopt_valsize;
1551 * getsockname1() - Get socket name.
1555 getsockname1(td, uap, compat)
1557 struct getsockname_args /* {
1559 struct sockaddr * __restrict asa;
1560 socklen_t * __restrict alen;
1564 struct sockaddr *sa;
1568 error = copyin(uap->alen, &len, sizeof(len));
1572 error = kern_getsockname(td, uap->fdes, &sa, &len);
1577 #ifdef COMPAT_OLDSOCK
1579 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1581 error = copyout(sa, uap->asa, (u_int)len);
1585 error = copyout(&len, uap->alen, sizeof(len));
1590 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1599 error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETSOCKNAME, &fp, NULL);
1604 CURVNET_SET(so->so_vnet);
1605 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1612 len = MIN(*alen, (*sa)->sa_len);
1615 if (KTRPOINT(td, KTR_STRUCT))
1621 free(*sa, M_SONAME);
1628 sys_getsockname(td, uap)
1630 struct getsockname_args *uap;
1633 return (getsockname1(td, uap, 0));
1636 #ifdef COMPAT_OLDSOCK
1638 ogetsockname(td, uap)
1640 struct getsockname_args *uap;
1643 return (getsockname1(td, uap, 1));
1645 #endif /* COMPAT_OLDSOCK */
1648 * getpeername1() - Get name of peer for connected socket.
1652 getpeername1(td, uap, compat)
1654 struct getpeername_args /* {
1656 struct sockaddr * __restrict asa;
1657 socklen_t * __restrict alen;
1661 struct sockaddr *sa;
1665 error = copyin(uap->alen, &len, sizeof (len));
1669 error = kern_getpeername(td, uap->fdes, &sa, &len);
1674 #ifdef COMPAT_OLDSOCK
1676 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1678 error = copyout(sa, uap->asa, (u_int)len);
1682 error = copyout(&len, uap->alen, sizeof(len));
1687 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1696 error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETPEERNAME, &fp, NULL);
1700 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1705 CURVNET_SET(so->so_vnet);
1706 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1713 len = MIN(*alen, (*sa)->sa_len);
1716 if (KTRPOINT(td, KTR_STRUCT))
1721 free(*sa, M_SONAME);
1730 sys_getpeername(td, uap)
1732 struct getpeername_args *uap;
1735 return (getpeername1(td, uap, 0));
1738 #ifdef COMPAT_OLDSOCK
1740 ogetpeername(td, uap)
1742 struct ogetpeername_args *uap;
1745 /* XXX uap should have type `getpeername_args *' to begin with. */
1746 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1748 #endif /* COMPAT_OLDSOCK */
1751 sockargs(mp, buf, buflen, type)
1756 struct sockaddr *sa;
1760 if (buflen > MLEN) {
1761 #ifdef COMPAT_OLDSOCK
1762 if (type == MT_SONAME && buflen <= 112)
1763 buflen = MLEN; /* unix domain compat. hack */
1766 if (buflen > MCLBYTES)
1769 m = m_get2(buflen, M_WAITOK, type, 0);
1771 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1776 if (type == MT_SONAME) {
1777 sa = mtod(m, struct sockaddr *);
1779 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1780 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1781 sa->sa_family = sa->sa_len;
1783 sa->sa_len = buflen;
1790 getsockaddr(namp, uaddr, len)
1791 struct sockaddr **namp;
1795 struct sockaddr *sa;
1798 if (len > SOCK_MAXADDRLEN)
1799 return (ENAMETOOLONG);
1800 if (len < offsetof(struct sockaddr, sa_data[0]))
1802 sa = malloc(len, M_SONAME, M_WAITOK);
1803 error = copyin(uaddr, sa, len);
1807 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1808 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1809 sa->sa_family = sa->sa_len;
1817 #include <sys/condvar.h>
1819 struct sendfile_sync {
1826 * Detach mapped page and release resources back to the system.
1829 sf_buf_mext(void *addr, void *args)
1832 struct sendfile_sync *sfs;
1834 m = sf_buf_page(args);
1837 vm_page_unwire(m, 0);
1839 * Check for the object going away on us. This can
1840 * happen since we don't hold a reference to it.
1841 * If so, we're responsible for freeing the page.
1843 if (m->wire_count == 0 && m->object == NULL)
1849 mtx_lock(&sfs->mtx);
1850 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1851 if (--sfs->count == 0)
1852 cv_signal(&sfs->cv);
1853 mtx_unlock(&sfs->mtx);
1859 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1860 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1862 * Send a file specified by 'fd' and starting at 'offset' to a socket
1863 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1864 * 0. Optionally add a header and/or trailer to the socket output. If
1865 * specified, write the total number of bytes sent into *sbytes.
1868 sys_sendfile(struct thread *td, struct sendfile_args *uap)
1871 return (do_sendfile(td, uap, 0));
1875 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1877 struct sf_hdtr hdtr;
1878 struct uio *hdr_uio, *trl_uio;
1881 hdr_uio = trl_uio = NULL;
1883 if (uap->hdtr != NULL) {
1884 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1887 if (hdtr.headers != NULL) {
1888 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1892 if (hdtr.trailers != NULL) {
1893 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1900 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1903 free(hdr_uio, M_IOV);
1905 free(trl_uio, M_IOV);
1909 #ifdef COMPAT_FREEBSD4
1911 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1913 struct sendfile_args args;
1917 args.offset = uap->offset;
1918 args.nbytes = uap->nbytes;
1919 args.hdtr = uap->hdtr;
1920 args.sbytes = uap->sbytes;
1921 args.flags = uap->flags;
1923 return (do_sendfile(td, &args, 1));
1925 #endif /* COMPAT_FREEBSD4 */
1928 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1929 struct uio *hdr_uio, struct uio *trl_uio, int compat)
1931 struct file *sock_fp;
1933 struct vm_object *obj = NULL;
1934 struct socket *so = NULL;
1935 struct mbuf *m = NULL;
1939 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1940 int error, hdrlen = 0, mnw = 0;
1942 struct sendfile_sync *sfs = NULL;
1945 * The file descriptor must be a regular file and have a
1946 * backing VM object.
1947 * File offset must be positive. If it goes beyond EOF
1948 * we send only the header/trailer and no payload data.
1950 AUDIT_ARG_FD(uap->fd);
1952 * sendfile(2) can start at any offset within a file so we require
1953 * CAP_READ+CAP_SEEK = CAP_PREAD.
1955 if ((error = fgetvp_read(td, uap->fd, CAP_PREAD, &vp)) != 0)
1957 vn_lock(vp, LK_SHARED | LK_RETRY);
1958 if (vp->v_type == VREG) {
1959 bsize = vp->v_mount->mnt_stat.f_iosize;
1960 if (uap->nbytes == 0) {
1961 error = VOP_GETATTR(vp, &va, td->td_ucred);
1973 * Temporarily increase the backing VM
1974 * object's reference count so that a forced
1975 * reclamation of its vnode does not
1976 * immediately destroy it.
1978 VM_OBJECT_WLOCK(obj);
1979 if ((obj->flags & OBJ_DEAD) == 0) {
1980 vm_object_reference_locked(obj);
1981 VM_OBJECT_WUNLOCK(obj);
1983 VM_OBJECT_WUNLOCK(obj);
1988 bsize = 0; /* silence gcc */
1994 if (uap->offset < 0) {
2000 * The socket must be a stream socket and connected.
2001 * Remember if it a blocking or non-blocking socket.
2003 if ((error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_SEND,
2004 &sock_fp, NULL)) != 0)
2006 so = sock_fp->f_data;
2007 if (so->so_type != SOCK_STREAM) {
2011 if ((so->so_state & SS_ISCONNECTED) == 0) {
2016 * Do not wait on memory allocations but return ENOMEM for
2017 * caller to retry later.
2018 * XXX: Experimental.
2020 if (uap->flags & SF_MNOWAIT)
2023 if (uap->flags & SF_SYNC) {
2024 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO);
2025 mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
2026 cv_init(&sfs->cv, "sendfile");
2030 error = mac_socket_check_send(td->td_ucred, so);
2035 /* If headers are specified copy them into mbufs. */
2036 if (hdr_uio != NULL) {
2037 hdr_uio->uio_td = td;
2038 hdr_uio->uio_rw = UIO_WRITE;
2039 if (hdr_uio->uio_resid > 0) {
2041 * In FBSD < 5.0 the nbytes to send also included
2042 * the header. If compat is specified subtract the
2043 * header size from nbytes.
2046 if (uap->nbytes > hdr_uio->uio_resid)
2047 uap->nbytes -= hdr_uio->uio_resid;
2051 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
2054 error = mnw ? EAGAIN : ENOBUFS;
2057 hdrlen = m_length(m, NULL);
2062 * Protect against multiple writers to the socket.
2064 * XXXRW: Historically this has assumed non-interruptibility, so now
2065 * we implement that, but possibly shouldn't.
2067 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
2070 * Loop through the pages of the file, starting with the requested
2071 * offset. Get a file page (do I/O if necessary), map the file page
2072 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
2074 * This is done in two loops. The inner loop turns as many pages
2075 * as it can, up to available socket buffer space, without blocking
2076 * into mbufs to have it bulk delivered into the socket send buffer.
2077 * The outer loop checks the state and available space of the socket
2078 * and takes care of the overall progress.
2080 for (off = uap->offset; ; ) {
2086 if ((uap->nbytes != 0 && uap->nbytes == fsbytes) ||
2087 (uap->nbytes == 0 && va.va_size == fsbytes))
2096 * Check the socket state for ongoing connection,
2097 * no errors and space in socket buffer.
2098 * If space is low allow for the remainder of the
2099 * file to be processed if it fits the socket buffer.
2100 * Otherwise block in waiting for sufficient space
2101 * to proceed, or if the socket is nonblocking, return
2102 * to userland with EAGAIN while reporting how far
2104 * We wait until the socket buffer has significant free
2105 * space to do bulk sends. This makes good use of file
2106 * system read ahead and allows packet segmentation
2107 * offloading hardware to take over lots of work. If
2108 * we were not careful here we would send off only one
2111 SOCKBUF_LOCK(&so->so_snd);
2112 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
2113 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
2115 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2117 SOCKBUF_UNLOCK(&so->so_snd);
2119 } else if (so->so_error) {
2120 error = so->so_error;
2122 SOCKBUF_UNLOCK(&so->so_snd);
2125 space = sbspace(&so->so_snd);
2128 space < so->so_snd.sb_lowat)) {
2129 if (so->so_state & SS_NBIO) {
2130 SOCKBUF_UNLOCK(&so->so_snd);
2135 * sbwait drops the lock while sleeping.
2136 * When we loop back to retry_space the
2137 * state may have changed and we retest
2140 error = sbwait(&so->so_snd);
2142 * An error from sbwait usually indicates that we've
2143 * been interrupted by a signal. If we've sent anything
2144 * then return bytes sent, otherwise return the error.
2147 SOCKBUF_UNLOCK(&so->so_snd);
2152 SOCKBUF_UNLOCK(&so->so_snd);
2155 * Reduce space in the socket buffer by the size of
2156 * the header mbuf chain.
2157 * hdrlen is set to 0 after the first loop.
2161 error = vn_lock(vp, LK_SHARED);
2164 error = VOP_GETATTR(vp, &va, td->td_ucred);
2165 if (error != 0 || off >= va.va_size) {
2171 * Loop and construct maximum sized mbuf chain to be bulk
2172 * dumped into socket buffer.
2174 while (space > loopbytes) {
2180 * Calculate the amount to transfer.
2181 * Not to exceed a page, the EOF,
2182 * or the passed in nbytes.
2184 pgoff = (vm_offset_t)(off & PAGE_MASK);
2186 rem = (uap->nbytes - fsbytes - loopbytes);
2189 uap->offset - fsbytes - loopbytes;
2190 xfsize = omin(PAGE_SIZE - pgoff, rem);
2191 xfsize = omin(space - loopbytes, xfsize);
2193 done = 1; /* all data sent */
2198 * Attempt to look up the page. Allocate
2199 * if not found or wait and loop if busy.
2201 pindex = OFF_TO_IDX(off);
2202 VM_OBJECT_WLOCK(obj);
2203 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2204 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY);
2207 * Check if page is valid for what we need,
2208 * otherwise initiate I/O.
2209 * If we already turned some pages into mbufs,
2210 * send them off before we come here again and
2213 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2214 VM_OBJECT_WUNLOCK(obj);
2216 error = EAGAIN; /* send what we already got */
2217 else if (uap->flags & SF_NODISKIO)
2223 * Ensure that our page is still around
2224 * when the I/O completes.
2226 vm_page_io_start(pg);
2227 VM_OBJECT_WUNLOCK(obj);
2230 * Get the page from backing store.
2231 * XXXMAC: Because we don't have fp->f_cred
2232 * here, we pass in NOCRED. This is probably
2233 * wrong, but is consistent with our original
2236 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
2237 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2238 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
2239 td->td_ucred, NOCRED, &resid, td);
2240 VM_OBJECT_WLOCK(obj);
2241 vm_page_io_finish(pg);
2243 VM_OBJECT_WUNLOCK(obj);
2248 vm_page_unwire(pg, 0);
2250 * See if anyone else might know about
2251 * this page. If not and it is not valid,
2254 if (pg->wire_count == 0 && pg->valid == 0 &&
2255 pg->busy == 0 && !(pg->oflags & VPO_BUSY))
2258 VM_OBJECT_WUNLOCK(obj);
2259 if (error == EAGAIN)
2260 error = 0; /* not a real error */
2265 * Get a sendfile buf. When allocating the
2266 * first buffer for mbuf chain, we usually
2267 * wait as long as necessary, but this wait
2268 * can be interrupted. For consequent
2269 * buffers, do not sleep, since several
2270 * threads might exhaust the buffers and then
2273 sf = sf_buf_alloc(pg, (mnw || m != NULL) ? SFB_NOWAIT :
2276 mbstat.sf_allocfail++;
2278 vm_page_unwire(pg, 0);
2279 KASSERT(pg->object != NULL,
2280 ("kern_sendfile: object disappeared"));
2283 error = (mnw ? EAGAIN : EINTR);
2288 * Get an mbuf and set it up as having
2291 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2293 error = (mnw ? EAGAIN : ENOBUFS);
2294 sf_buf_mext(NULL, sf);
2297 if (m_extadd(m0, (caddr_t )sf_buf_kva(sf), PAGE_SIZE,
2298 sf_buf_mext, sfs, sf, M_RDONLY, EXT_SFBUF,
2299 (mnw ? M_NOWAIT : M_WAITOK)) != 0) {
2300 error = (mnw ? EAGAIN : ENOBUFS);
2301 sf_buf_mext(NULL, sf);
2305 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2308 /* Append to mbuf chain. */
2312 m_last(m)->m_next = m0;
2317 /* Keep track of bits processed. */
2318 loopbytes += xfsize;
2322 mtx_lock(&sfs->mtx);
2324 mtx_unlock(&sfs->mtx);
2330 /* Add the buffer chain to the socket buffer. */
2334 mlen = m_length(m, NULL);
2335 SOCKBUF_LOCK(&so->so_snd);
2336 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2338 SOCKBUF_UNLOCK(&so->so_snd);
2341 SOCKBUF_UNLOCK(&so->so_snd);
2342 CURVNET_SET(so->so_vnet);
2343 /* Avoid error aliasing. */
2344 err = (*so->so_proto->pr_usrreqs->pru_send)
2345 (so, 0, m, NULL, NULL, td);
2349 * We need two counters to get the
2350 * file offset and nbytes to send
2352 * - sbytes contains the total amount
2353 * of bytes sent, including headers.
2354 * - fsbytes contains the total amount
2355 * of bytes sent from the file.
2363 } else if (error == 0)
2365 m = NULL; /* pru_send always consumes */
2368 /* Quit outer loop on error or when we're done. */
2376 * Send trailers. Wimp out and use writev(2).
2378 if (trl_uio != NULL) {
2379 sbunlock(&so->so_snd);
2380 error = kern_writev(td, uap->s, trl_uio);
2382 sbytes += td->td_retval[0];
2387 sbunlock(&so->so_snd);
2390 * If there was no error we have to clear td->td_retval[0]
2391 * because it may have been set by writev.
2394 td->td_retval[0] = 0;
2396 if (uap->sbytes != NULL) {
2397 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2400 vm_object_deallocate(obj);
2409 mtx_lock(&sfs->mtx);
2410 if (sfs->count != 0)
2411 cv_wait(&sfs->cv, &sfs->mtx);
2412 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2413 cv_destroy(&sfs->cv);
2414 mtx_destroy(&sfs->mtx);
2418 if (error == ERESTART)
2426 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2427 * otherwise all return EOPNOTSUPP.
2428 * XXX: We should make this loadable one day.
2431 sys_sctp_peeloff(td, uap)
2433 struct sctp_peeloff_args /* {
2438 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2439 struct file *nfp = NULL;
2441 struct socket *head, *so;
2445 AUDIT_ARG_FD(uap->sd);
2446 error = fgetsock(td, uap->sd, CAP_PEELOFF, &head, &fflag);
2449 if (head->so_proto->pr_protocol != IPPROTO_SCTP) {
2453 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2457 * At this point we know we do have a assoc to pull
2458 * we proceed to get the fd setup. This may block
2462 error = falloc(td, &nfp, &fd, 0);
2465 td->td_retval[0] = fd;
2467 CURVNET_SET(head->so_vnet);
2468 so = sonewconn(head, SS_ISCONNECTED);
2474 * Before changing the flags on the socket, we have to bump the
2475 * reference count. Otherwise, if the protocol calls sofree(),
2476 * the socket will be released due to a zero refcount.
2479 soref(so); /* file descriptor reference */
2484 TAILQ_REMOVE(&head->so_comp, so, so_list);
2486 so->so_state |= (head->so_state & SS_NBIO);
2487 so->so_state &= ~SS_NOFDREF;
2488 so->so_qstate &= ~SQ_COMP;
2491 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2492 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2495 if (head->so_sigio != NULL)
2496 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2500 * close the new descriptor, assuming someone hasn't ripped it
2501 * out from under us.
2504 fdclose(td->td_proc->p_fd, nfp, fd, td);
2507 * Release explicitly held references before returning.
2517 return (EOPNOTSUPP);
2522 sys_sctp_generic_sendmsg (td, uap)
2524 struct sctp_generic_sendmsg_args /* {
2530 struct sctp_sndrcvinfo *sinfo,
2534 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2535 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2537 struct file *fp = NULL;
2539 struct sockaddr *to = NULL;
2541 struct uio *ktruio = NULL;
2544 struct iovec iov[1];
2545 cap_rights_t rights;
2548 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2556 error = getsockaddr(&to, uap->to, uap->tolen);
2561 rights |= CAP_CONNECT;
2564 AUDIT_ARG_FD(uap->sd);
2565 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2569 if (to && (KTRPOINT(td, KTR_STRUCT)))
2573 iov[0].iov_base = uap->msg;
2574 iov[0].iov_len = uap->mlen;
2576 so = (struct socket *)fp->f_data;
2577 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2582 error = mac_socket_check_send(td->td_ucred, so);
2588 auio.uio_iovcnt = 1;
2589 auio.uio_segflg = UIO_USERSPACE;
2590 auio.uio_rw = UIO_WRITE;
2592 auio.uio_offset = 0; /* XXX */
2594 len = auio.uio_resid = uap->mlen;
2595 CURVNET_SET(so->so_vnet);
2596 error = sctp_lower_sosend(so, to, &auio,
2597 (struct mbuf *)NULL, (struct mbuf *)NULL,
2598 uap->flags, u_sinfo, td);
2601 if (auio.uio_resid != len && (error == ERESTART ||
2602 error == EINTR || error == EWOULDBLOCK))
2604 /* Generation of SIGPIPE can be controlled per socket. */
2605 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2606 !(uap->flags & MSG_NOSIGNAL)) {
2607 PROC_LOCK(td->td_proc);
2608 tdsignal(td, SIGPIPE);
2609 PROC_UNLOCK(td->td_proc);
2613 td->td_retval[0] = len - auio.uio_resid;
2615 if (ktruio != NULL) {
2616 ktruio->uio_resid = td->td_retval[0];
2617 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2628 return (EOPNOTSUPP);
2633 sys_sctp_generic_sendmsg_iov(td, uap)
2635 struct sctp_generic_sendmsg_iov_args /* {
2641 struct sctp_sndrcvinfo *sinfo,
2645 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2646 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2648 struct file *fp = NULL;
2651 struct sockaddr *to = NULL;
2653 struct uio *ktruio = NULL;
2656 struct iovec *iov, *tiov;
2657 cap_rights_t rights;
2660 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2667 error = getsockaddr(&to, uap->to, uap->tolen);
2672 rights |= CAP_CONNECT;
2675 AUDIT_ARG_FD(uap->sd);
2676 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2680 #ifdef COMPAT_FREEBSD32
2681 if (SV_CURPROC_FLAG(SV_ILP32))
2682 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2683 uap->iovlen, &iov, EMSGSIZE);
2686 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2690 if (to && (KTRPOINT(td, KTR_STRUCT)))
2694 so = (struct socket *)fp->f_data;
2695 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2700 error = mac_socket_check_send(td->td_ucred, so);
2706 auio.uio_iovcnt = uap->iovlen;
2707 auio.uio_segflg = UIO_USERSPACE;
2708 auio.uio_rw = UIO_WRITE;
2710 auio.uio_offset = 0; /* XXX */
2713 for (i = 0; i <uap->iovlen; i++, tiov++) {
2714 if ((auio.uio_resid += tiov->iov_len) < 0) {
2719 len = auio.uio_resid;
2720 CURVNET_SET(so->so_vnet);
2721 error = sctp_lower_sosend(so, to, &auio,
2722 (struct mbuf *)NULL, (struct mbuf *)NULL,
2723 uap->flags, u_sinfo, td);
2726 if (auio.uio_resid != len && (error == ERESTART ||
2727 error == EINTR || error == EWOULDBLOCK))
2729 /* Generation of SIGPIPE can be controlled per socket */
2730 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2731 !(uap->flags & MSG_NOSIGNAL)) {
2732 PROC_LOCK(td->td_proc);
2733 tdsignal(td, SIGPIPE);
2734 PROC_UNLOCK(td->td_proc);
2738 td->td_retval[0] = len - auio.uio_resid;
2740 if (ktruio != NULL) {
2741 ktruio->uio_resid = td->td_retval[0];
2742 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2755 return (EOPNOTSUPP);
2760 sys_sctp_generic_recvmsg(td, uap)
2762 struct sctp_generic_recvmsg_args /* {
2766 struct sockaddr *from,
2767 __socklen_t *fromlenaddr,
2768 struct sctp_sndrcvinfo *sinfo,
2772 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2773 uint8_t sockbufstore[256];
2775 struct iovec *iov, *tiov;
2776 struct sctp_sndrcvinfo sinfo;
2778 struct file *fp = NULL;
2779 struct sockaddr *fromsa;
2785 struct uio *ktruio = NULL;
2788 AUDIT_ARG_FD(uap->sd);
2789 error = getsock_cap(td->td_proc->p_fd, uap->sd, CAP_RECV, &fp, NULL);
2793 #ifdef COMPAT_FREEBSD32
2794 if (SV_CURPROC_FLAG(SV_ILP32))
2795 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2796 uap->iovlen, &iov, EMSGSIZE);
2799 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2804 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2809 error = mac_socket_check_receive(td->td_ucred, so);
2815 if (uap->fromlenaddr) {
2816 error = copyin(uap->fromlenaddr,
2817 &fromlen, sizeof (fromlen));
2824 if (uap->msg_flags) {
2825 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2833 auio.uio_iovcnt = uap->iovlen;
2834 auio.uio_segflg = UIO_USERSPACE;
2835 auio.uio_rw = UIO_READ;
2837 auio.uio_offset = 0; /* XXX */
2840 for (i = 0; i <uap->iovlen; i++, tiov++) {
2841 if ((auio.uio_resid += tiov->iov_len) < 0) {
2846 len = auio.uio_resid;
2847 fromsa = (struct sockaddr *)sockbufstore;
2850 if (KTRPOINT(td, KTR_GENIO))
2851 ktruio = cloneuio(&auio);
2853 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
2854 CURVNET_SET(so->so_vnet);
2855 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2856 fromsa, fromlen, &msg_flags,
2857 (struct sctp_sndrcvinfo *)&sinfo, 1);
2860 if (auio.uio_resid != len && (error == ERESTART ||
2861 error == EINTR || error == EWOULDBLOCK))
2865 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2868 if (ktruio != NULL) {
2869 ktruio->uio_resid = len - auio.uio_resid;
2870 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2875 td->td_retval[0] = len - auio.uio_resid;
2877 if (fromlen && uap->from) {
2879 if (len <= 0 || fromsa == 0)
2882 len = MIN(len, fromsa->sa_len);
2883 error = copyout(fromsa, uap->from, (size_t)len);
2887 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2893 if (KTRPOINT(td, KTR_STRUCT))
2894 ktrsockaddr(fromsa);
2896 if (uap->msg_flags) {
2897 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2910 return (EOPNOTSUPP);