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 */
100 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
101 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
103 static int accept1(struct thread *td, struct accept_args *uap, int compat);
104 static int do_sendfile(struct thread *td, struct sendfile_args *uap, int compat);
105 static int getsockname1(struct thread *td, struct getsockname_args *uap,
107 static int getpeername1(struct thread *td, struct getpeername_args *uap,
111 * NSFBUFS-related variables and associated sysctls
117 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
118 "Maximum number of sendfile(2) sf_bufs available");
119 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
120 "Number of sendfile(2) sf_bufs at peak usage");
121 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
122 "Number of sendfile(2) sf_bufs in use");
125 * Convert a user file descriptor to a kernel file entry and check that, if
126 * it is a capability, the right rights are present. A reference on the file
127 * entry is held upon returning.
130 getsock_cap(struct filedesc *fdp, int fd, cap_rights_t rights,
131 struct file **fpp, u_int *fflagp)
135 struct file *fp_fromcap;
139 if (fdp == NULL || (fp = fget_unlocked(fdp, fd)) == NULL)
143 * If the file descriptor is for a capability, test rights and use
144 * the file descriptor referenced by the capability.
146 error = cap_funwrap(fp, rights, &fp_fromcap);
148 fdrop(fp, curthread);
151 if (fp != fp_fromcap) {
153 fdrop(fp, curthread);
156 #endif /* CAPABILITIES */
157 if (fp->f_type != DTYPE_SOCKET) {
158 fdrop(fp, curthread);
162 *fflagp = fp->f_flag;
168 * System call interface to the socket abstraction.
170 #if defined(COMPAT_43)
171 #define COMPAT_OLDSOCK
177 struct socket_args /* {
187 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
189 error = mac_socket_check_create(td->td_ucred, uap->domain, uap->type,
194 error = falloc(td, &fp, &fd, 0);
197 /* An extra reference on `fp' has been held for us by falloc(). */
198 error = socreate(uap->domain, &so, uap->type, uap->protocol,
201 fdclose(td->td_proc->p_fd, fp, fd, td);
203 finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &socketops);
204 td->td_retval[0] = fd;
214 struct bind_args /* {
223 if ((error = getsockaddr(&sa, uap->name, uap->namelen)) != 0)
226 error = kern_bind(td, uap->s, sa);
232 kern_bind(td, fd, sa)
242 AUDIT_ARG_SOCKADDR(td, sa);
243 error = getsock_cap(td->td_proc->p_fd, fd, CAP_BIND, &fp, NULL);
248 if (KTRPOINT(td, KTR_STRUCT))
252 error = mac_socket_check_bind(td->td_ucred, so, sa);
255 error = sobind(so, sa, td);
264 struct listen_args /* {
273 AUDIT_ARG_FD(uap->s);
274 error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_LISTEN, &fp, NULL);
278 error = mac_socket_check_listen(td->td_ucred, so);
281 error = solisten(so, uap->backlog, td);
291 accept1(td, uap, compat)
293 struct accept_args /* {
295 struct sockaddr * __restrict name;
296 socklen_t * __restrict anamelen;
300 struct sockaddr *name;
305 if (uap->name == NULL)
306 return (kern_accept(td, uap->s, NULL, NULL, NULL));
308 error = copyin(uap->anamelen, &namelen, sizeof (namelen));
312 error = kern_accept(td, uap->s, &name, &namelen, &fp);
315 * return a namelen of zero for older code which might
316 * ignore the return value from accept.
319 (void) copyout(&namelen,
320 uap->anamelen, sizeof(*uap->anamelen));
324 if (error == 0 && name != NULL) {
325 #ifdef COMPAT_OLDSOCK
327 ((struct osockaddr *)name)->sa_family =
330 error = copyout(name, uap->name, namelen);
333 error = copyout(&namelen, uap->anamelen,
336 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
338 free(name, M_SONAME);
343 kern_accept(struct thread *td, int s, struct sockaddr **name,
344 socklen_t *namelen, struct file **fp)
346 struct filedesc *fdp;
347 struct file *headfp, *nfp = NULL;
348 struct sockaddr *sa = NULL;
350 struct socket *head, *so;
363 fdp = td->td_proc->p_fd;
364 error = getsock_cap(fdp, s, CAP_ACCEPT, &headfp, &fflag);
367 head = headfp->f_data;
368 if ((head->so_options & SO_ACCEPTCONN) == 0) {
373 error = mac_socket_check_accept(td->td_ucred, head);
377 error = falloc(td, &nfp, &fd, 0);
381 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
386 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
387 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
388 head->so_error = ECONNABORTED;
391 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
398 if (head->so_error) {
399 error = head->so_error;
404 so = TAILQ_FIRST(&head->so_comp);
405 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
406 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
409 * Before changing the flags on the socket, we have to bump the
410 * reference count. Otherwise, if the protocol calls sofree(),
411 * the socket will be released due to a zero refcount.
413 SOCK_LOCK(so); /* soref() and so_state update */
414 soref(so); /* file descriptor reference */
416 TAILQ_REMOVE(&head->so_comp, so, so_list);
418 so->so_state |= (head->so_state & SS_NBIO);
419 so->so_qstate &= ~SQ_COMP;
425 /* An extra reference on `nfp' has been held for us by falloc(). */
426 td->td_retval[0] = fd;
428 /* connection has been removed from the listen queue */
429 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
431 pgid = fgetown(&head->so_sigio);
433 fsetown(pgid, &so->so_sigio);
435 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
436 /* Sync socket nonblocking/async state with file flags */
437 tmp = fflag & FNONBLOCK;
438 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
439 tmp = fflag & FASYNC;
440 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
442 error = soaccept(so, &sa);
445 * return a namelen of zero for older code which might
446 * ignore the return value from accept.
457 AUDIT_ARG_SOCKADDR(td, sa);
459 /* check sa_len before it is destroyed */
460 if (*namelen > sa->sa_len)
461 *namelen = sa->sa_len;
463 if (KTRPOINT(td, KTR_STRUCT))
474 * close the new descriptor, assuming someone hasn't ripped it
478 fdclose(fdp, nfp, fd, td);
481 * Release explicitly held references before returning. We return
482 * a reference on nfp to the caller on success if they request it.
501 struct accept_args *uap;
504 return (accept1(td, uap, 0));
507 #ifdef COMPAT_OLDSOCK
511 struct accept_args *uap;
514 return (accept1(td, uap, 1));
516 #endif /* COMPAT_OLDSOCK */
522 struct connect_args /* {
531 error = getsockaddr(&sa, uap->name, uap->namelen);
535 error = kern_connect(td, uap->s, sa);
542 kern_connect(td, fd, sa)
553 AUDIT_ARG_SOCKADDR(td, sa);
554 error = getsock_cap(td->td_proc->p_fd, fd, CAP_CONNECT, &fp, NULL);
558 if (so->so_state & SS_ISCONNECTING) {
563 if (KTRPOINT(td, KTR_STRUCT))
567 error = mac_socket_check_connect(td->td_ucred, so, sa);
571 error = soconnect(so, sa, td);
574 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
579 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
580 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
583 if (error == EINTR || error == ERESTART)
589 error = so->so_error;
595 so->so_state &= ~SS_ISCONNECTING;
596 if (error == ERESTART)
604 kern_socketpair(struct thread *td, int domain, int type, int protocol,
607 struct filedesc *fdp = td->td_proc->p_fd;
608 struct file *fp1, *fp2;
609 struct socket *so1, *so2;
612 AUDIT_ARG_SOCKET(domain, type, protocol);
614 /* We might want to have a separate check for socket pairs. */
615 error = mac_socket_check_create(td->td_ucred, domain, type,
620 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
623 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
626 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
627 error = falloc(td, &fp1, &fd, 0);
631 fp1->f_data = so1; /* so1 already has ref count */
632 error = falloc(td, &fp2, &fd, 0);
635 fp2->f_data = so2; /* so2 already has ref count */
637 error = soconnect2(so1, so2);
640 if (type == SOCK_DGRAM) {
642 * Datagram socket connection is asymmetric.
644 error = soconnect2(so2, so1);
648 finit(fp1, FREAD | FWRITE, DTYPE_SOCKET, fp1->f_data, &socketops);
649 finit(fp2, FREAD | FWRITE, DTYPE_SOCKET, fp2->f_data, &socketops);
654 fdclose(fdp, fp2, rsv[1], td);
657 fdclose(fdp, fp1, rsv[0], td);
669 sys_socketpair(struct thread *td, struct socketpair_args *uap)
673 error = kern_socketpair(td, uap->domain, uap->type,
677 error = copyout(sv, uap->rsv, 2 * sizeof(int));
679 (void)kern_close(td, sv[0]);
680 (void)kern_close(td, sv[1]);
686 sendit(td, s, mp, flags)
692 struct mbuf *control;
696 #ifdef CAPABILITY_MODE
697 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
701 if (mp->msg_name != NULL) {
702 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
712 if (mp->msg_control) {
713 if (mp->msg_controllen < sizeof(struct cmsghdr)
714 #ifdef COMPAT_OLDSOCK
715 && mp->msg_flags != MSG_COMPAT
721 error = sockargs(&control, mp->msg_control,
722 mp->msg_controllen, MT_CONTROL);
725 #ifdef COMPAT_OLDSOCK
726 if (mp->msg_flags == MSG_COMPAT) {
729 M_PREPEND(control, sizeof(*cm), M_WAITOK);
730 cm = mtod(control, struct cmsghdr *);
731 cm->cmsg_len = control->m_len;
732 cm->cmsg_level = SOL_SOCKET;
733 cm->cmsg_type = SCM_RIGHTS;
740 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
749 kern_sendit(td, s, mp, flags, control, segflg)
754 struct mbuf *control;
765 struct uio *ktruio = NULL;
770 if (mp->msg_name != NULL) {
771 AUDIT_ARG_SOCKADDR(td, mp->msg_name);
772 rights |= CAP_CONNECT;
774 error = getsock_cap(td->td_proc->p_fd, s, rights, &fp, NULL);
777 so = (struct socket *)fp->f_data;
780 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
781 ktrsockaddr(mp->msg_name);
784 if (mp->msg_name != NULL) {
785 error = mac_socket_check_connect(td->td_ucred, so,
790 error = mac_socket_check_send(td->td_ucred, so);
795 auio.uio_iov = mp->msg_iov;
796 auio.uio_iovcnt = mp->msg_iovlen;
797 auio.uio_segflg = segflg;
798 auio.uio_rw = UIO_WRITE;
800 auio.uio_offset = 0; /* XXX */
803 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
804 if ((auio.uio_resid += iov->iov_len) < 0) {
810 if (KTRPOINT(td, KTR_GENIO))
811 ktruio = cloneuio(&auio);
813 len = auio.uio_resid;
814 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
816 if (auio.uio_resid != len && (error == ERESTART ||
817 error == EINTR || error == EWOULDBLOCK))
819 /* Generation of SIGPIPE can be controlled per socket */
820 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
821 !(flags & MSG_NOSIGNAL)) {
822 PROC_LOCK(td->td_proc);
823 tdsignal(td, SIGPIPE);
824 PROC_UNLOCK(td->td_proc);
828 td->td_retval[0] = len - auio.uio_resid;
830 if (ktruio != NULL) {
831 ktruio->uio_resid = td->td_retval[0];
832 ktrgenio(s, UIO_WRITE, ktruio, error);
843 struct sendto_args /* {
856 msg.msg_name = uap->to;
857 msg.msg_namelen = uap->tolen;
861 #ifdef COMPAT_OLDSOCK
864 aiov.iov_base = uap->buf;
865 aiov.iov_len = uap->len;
866 error = sendit(td, uap->s, &msg, uap->flags);
870 #ifdef COMPAT_OLDSOCK
874 struct osend_args /* {
889 aiov.iov_base = uap->buf;
890 aiov.iov_len = uap->len;
893 error = sendit(td, uap->s, &msg, uap->flags);
900 struct osendmsg_args /* {
910 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
913 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
917 msg.msg_flags = MSG_COMPAT;
918 error = sendit(td, uap->s, &msg, uap->flags);
927 struct sendmsg_args /* {
937 error = copyin(uap->msg, &msg, sizeof (msg));
940 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
944 #ifdef COMPAT_OLDSOCK
947 error = sendit(td, uap->s, &msg, uap->flags);
953 kern_recvit(td, s, mp, fromseg, controlp)
957 enum uio_seg fromseg;
958 struct mbuf **controlp;
965 struct mbuf *m, *control = NULL;
969 struct sockaddr *fromsa = NULL;
971 struct uio *ktruio = NULL;
974 if (controlp != NULL)
978 error = getsock_cap(td->td_proc->p_fd, s, CAP_READ, &fp, NULL);
984 error = mac_socket_check_receive(td->td_ucred, so);
991 auio.uio_iov = mp->msg_iov;
992 auio.uio_iovcnt = mp->msg_iovlen;
993 auio.uio_segflg = UIO_USERSPACE;
994 auio.uio_rw = UIO_READ;
996 auio.uio_offset = 0; /* XXX */
999 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
1000 if ((auio.uio_resid += iov->iov_len) < 0) {
1006 if (KTRPOINT(td, KTR_GENIO))
1007 ktruio = cloneuio(&auio);
1009 len = auio.uio_resid;
1010 error = soreceive(so, &fromsa, &auio, NULL,
1011 (mp->msg_control || controlp) ? &control : NULL,
1014 if (auio.uio_resid != len && (error == ERESTART ||
1015 error == EINTR || error == EWOULDBLOCK))
1019 AUDIT_ARG_SOCKADDR(td, fromsa);
1021 if (ktruio != NULL) {
1022 ktruio->uio_resid = len - auio.uio_resid;
1023 ktrgenio(s, UIO_READ, ktruio, error);
1028 td->td_retval[0] = len - auio.uio_resid;
1030 len = mp->msg_namelen;
1031 if (len <= 0 || fromsa == NULL)
1034 /* save sa_len before it is destroyed by MSG_COMPAT */
1035 len = MIN(len, fromsa->sa_len);
1036 #ifdef COMPAT_OLDSOCK
1037 if (mp->msg_flags & MSG_COMPAT)
1038 ((struct osockaddr *)fromsa)->sa_family =
1041 if (fromseg == UIO_USERSPACE) {
1042 error = copyout(fromsa, mp->msg_name,
1047 bcopy(fromsa, mp->msg_name, len);
1049 mp->msg_namelen = len;
1051 if (mp->msg_control && controlp == NULL) {
1052 #ifdef COMPAT_OLDSOCK
1054 * We assume that old recvmsg calls won't receive access
1055 * rights and other control info, esp. as control info
1056 * is always optional and those options didn't exist in 4.3.
1057 * If we receive rights, trim the cmsghdr; anything else
1060 if (control && mp->msg_flags & MSG_COMPAT) {
1061 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1063 mtod(control, struct cmsghdr *)->cmsg_type !=
1065 mp->msg_controllen = 0;
1068 control->m_len -= sizeof (struct cmsghdr);
1069 control->m_data += sizeof (struct cmsghdr);
1072 len = mp->msg_controllen;
1074 mp->msg_controllen = 0;
1075 ctlbuf = mp->msg_control;
1077 while (m && len > 0) {
1078 unsigned int tocopy;
1080 if (len >= m->m_len)
1083 mp->msg_flags |= MSG_CTRUNC;
1087 if ((error = copyout(mtod(m, caddr_t),
1088 ctlbuf, tocopy)) != 0)
1095 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1100 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1101 ktrsockaddr(fromsa);
1104 free(fromsa, M_SONAME);
1106 if (error == 0 && controlp != NULL)
1107 *controlp = control;
1115 recvit(td, s, mp, namelenp)
1123 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1127 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1128 #ifdef COMPAT_OLDSOCK
1129 if (mp->msg_flags & MSG_COMPAT)
1130 error = 0; /* old recvfrom didn't check */
1137 sys_recvfrom(td, uap)
1139 struct recvfrom_args /* {
1144 struct sockaddr * __restrict from;
1145 socklen_t * __restrict fromlenaddr;
1152 if (uap->fromlenaddr) {
1153 error = copyin(uap->fromlenaddr,
1154 &msg.msg_namelen, sizeof (msg.msg_namelen));
1158 msg.msg_namelen = 0;
1160 msg.msg_name = uap->from;
1161 msg.msg_iov = &aiov;
1163 aiov.iov_base = uap->buf;
1164 aiov.iov_len = uap->len;
1165 msg.msg_control = 0;
1166 msg.msg_flags = uap->flags;
1167 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1172 #ifdef COMPAT_OLDSOCK
1176 struct recvfrom_args *uap;
1179 uap->flags |= MSG_COMPAT;
1180 return (sys_recvfrom(td, uap));
1184 #ifdef COMPAT_OLDSOCK
1188 struct orecv_args /* {
1200 msg.msg_namelen = 0;
1201 msg.msg_iov = &aiov;
1203 aiov.iov_base = uap->buf;
1204 aiov.iov_len = uap->len;
1205 msg.msg_control = 0;
1206 msg.msg_flags = uap->flags;
1207 error = recvit(td, uap->s, &msg, NULL);
1212 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1213 * overlays the new one, missing only the flags, and with the (old) access
1214 * rights where the control fields are now.
1219 struct orecvmsg_args /* {
1221 struct omsghdr *msg;
1229 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1232 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1235 msg.msg_flags = uap->flags | MSG_COMPAT;
1237 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1238 if (msg.msg_controllen && error == 0)
1239 error = copyout(&msg.msg_controllen,
1240 &uap->msg->msg_accrightslen, sizeof (int));
1247 sys_recvmsg(td, uap)
1249 struct recvmsg_args /* {
1256 struct iovec *uiov, *iov;
1259 error = copyin(uap->msg, &msg, sizeof (msg));
1262 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1265 msg.msg_flags = uap->flags;
1266 #ifdef COMPAT_OLDSOCK
1267 msg.msg_flags &= ~MSG_COMPAT;
1271 error = recvit(td, uap->s, &msg, NULL);
1274 error = copyout(&msg, uap->msg, sizeof(msg));
1282 sys_shutdown(td, uap)
1284 struct shutdown_args /* {
1293 AUDIT_ARG_FD(uap->s);
1294 error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_SHUTDOWN, &fp,
1298 error = soshutdown(so, uap->how);
1306 sys_setsockopt(td, uap)
1308 struct setsockopt_args /* {
1317 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1318 uap->val, UIO_USERSPACE, uap->valsize));
1322 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1328 enum uio_seg valseg;
1334 struct sockopt sopt;
1336 if (val == NULL && valsize != 0)
1338 if ((int)valsize < 0)
1341 sopt.sopt_dir = SOPT_SET;
1342 sopt.sopt_level = level;
1343 sopt.sopt_name = name;
1344 sopt.sopt_val = val;
1345 sopt.sopt_valsize = valsize;
1351 sopt.sopt_td = NULL;
1354 panic("kern_setsockopt called with bad valseg");
1358 error = getsock_cap(td->td_proc->p_fd, s, CAP_SETSOCKOPT, &fp, NULL);
1361 error = sosetopt(so, &sopt);
1369 sys_getsockopt(td, uap)
1371 struct getsockopt_args /* {
1375 void * __restrict val;
1376 socklen_t * __restrict avalsize;
1383 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1388 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1389 uap->val, UIO_USERSPACE, &valsize);
1392 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1397 * Kernel version of getsockopt.
1398 * optval can be a userland or userspace. optlen is always a kernel pointer.
1401 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1407 enum uio_seg valseg;
1413 struct sockopt sopt;
1417 if ((int)*valsize < 0)
1420 sopt.sopt_dir = SOPT_GET;
1421 sopt.sopt_level = level;
1422 sopt.sopt_name = name;
1423 sopt.sopt_val = val;
1424 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1430 sopt.sopt_td = NULL;
1433 panic("kern_getsockopt called with bad valseg");
1437 error = getsock_cap(td->td_proc->p_fd, s, CAP_GETSOCKOPT, &fp, NULL);
1440 error = sogetopt(so, &sopt);
1441 *valsize = sopt.sopt_valsize;
1448 * getsockname1() - Get socket name.
1452 getsockname1(td, uap, compat)
1454 struct getsockname_args /* {
1456 struct sockaddr * __restrict asa;
1457 socklen_t * __restrict alen;
1461 struct sockaddr *sa;
1465 error = copyin(uap->alen, &len, sizeof(len));
1469 error = kern_getsockname(td, uap->fdes, &sa, &len);
1474 #ifdef COMPAT_OLDSOCK
1476 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1478 error = copyout(sa, uap->asa, (u_int)len);
1482 error = copyout(&len, uap->alen, sizeof(len));
1487 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1499 error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETSOCKNAME, &fp, NULL);
1504 CURVNET_SET(so->so_vnet);
1505 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1512 len = MIN(*alen, (*sa)->sa_len);
1515 if (KTRPOINT(td, KTR_STRUCT))
1521 free(*sa, M_SONAME);
1528 sys_getsockname(td, uap)
1530 struct getsockname_args *uap;
1533 return (getsockname1(td, uap, 0));
1536 #ifdef COMPAT_OLDSOCK
1538 ogetsockname(td, uap)
1540 struct getsockname_args *uap;
1543 return (getsockname1(td, uap, 1));
1545 #endif /* COMPAT_OLDSOCK */
1548 * getpeername1() - Get name of peer for connected socket.
1552 getpeername1(td, uap, compat)
1554 struct getpeername_args /* {
1556 struct sockaddr * __restrict asa;
1557 socklen_t * __restrict alen;
1561 struct sockaddr *sa;
1565 error = copyin(uap->alen, &len, sizeof (len));
1569 error = kern_getpeername(td, uap->fdes, &sa, &len);
1574 #ifdef COMPAT_OLDSOCK
1576 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1578 error = copyout(sa, uap->asa, (u_int)len);
1582 error = copyout(&len, uap->alen, sizeof(len));
1587 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1599 error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETPEERNAME, &fp, NULL);
1603 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1608 CURVNET_SET(so->so_vnet);
1609 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1616 len = MIN(*alen, (*sa)->sa_len);
1619 if (KTRPOINT(td, KTR_STRUCT))
1624 free(*sa, M_SONAME);
1633 sys_getpeername(td, uap)
1635 struct getpeername_args *uap;
1638 return (getpeername1(td, uap, 0));
1641 #ifdef COMPAT_OLDSOCK
1643 ogetpeername(td, uap)
1645 struct ogetpeername_args *uap;
1648 /* XXX uap should have type `getpeername_args *' to begin with. */
1649 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1651 #endif /* COMPAT_OLDSOCK */
1654 sockargs(mp, buf, buflen, type)
1659 struct sockaddr *sa;
1663 if ((u_int)buflen > MLEN) {
1664 #ifdef COMPAT_OLDSOCK
1665 if (type == MT_SONAME && (u_int)buflen <= 112)
1666 buflen = MLEN; /* unix domain compat. hack */
1669 if ((u_int)buflen > MCLBYTES)
1672 m = m_get(M_WAITOK, type);
1673 if ((u_int)buflen > MLEN)
1674 MCLGET(m, M_WAITOK);
1676 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1681 if (type == MT_SONAME) {
1682 sa = mtod(m, struct sockaddr *);
1684 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1685 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1686 sa->sa_family = sa->sa_len;
1688 sa->sa_len = buflen;
1695 getsockaddr(namp, uaddr, len)
1696 struct sockaddr **namp;
1700 struct sockaddr *sa;
1703 if (len > SOCK_MAXADDRLEN)
1704 return (ENAMETOOLONG);
1705 if (len < offsetof(struct sockaddr, sa_data[0]))
1707 sa = malloc(len, M_SONAME, M_WAITOK);
1708 error = copyin(uaddr, sa, len);
1712 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1713 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1714 sa->sa_family = sa->sa_len;
1722 #include <sys/condvar.h>
1724 struct sendfile_sync {
1731 * Detach mapped page and release resources back to the system.
1734 sf_buf_mext(void *addr, void *args)
1737 struct sendfile_sync *sfs;
1739 m = sf_buf_page(args);
1742 vm_page_unwire(m, 0);
1744 * Check for the object going away on us. This can
1745 * happen since we don't hold a reference to it.
1746 * If so, we're responsible for freeing the page.
1748 if (m->wire_count == 0 && m->object == NULL)
1754 mtx_lock(&sfs->mtx);
1755 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1756 if (--sfs->count == 0)
1757 cv_signal(&sfs->cv);
1758 mtx_unlock(&sfs->mtx);
1764 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1765 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1767 * Send a file specified by 'fd' and starting at 'offset' to a socket
1768 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1769 * 0. Optionally add a header and/or trailer to the socket output. If
1770 * specified, write the total number of bytes sent into *sbytes.
1773 sys_sendfile(struct thread *td, struct sendfile_args *uap)
1776 return (do_sendfile(td, uap, 0));
1780 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1782 struct sf_hdtr hdtr;
1783 struct uio *hdr_uio, *trl_uio;
1786 hdr_uio = trl_uio = NULL;
1788 if (uap->hdtr != NULL) {
1789 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1792 if (hdtr.headers != NULL) {
1793 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1797 if (hdtr.trailers != NULL) {
1798 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1805 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1808 free(hdr_uio, M_IOV);
1810 free(trl_uio, M_IOV);
1814 #ifdef COMPAT_FREEBSD4
1816 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1818 struct sendfile_args args;
1822 args.offset = uap->offset;
1823 args.nbytes = uap->nbytes;
1824 args.hdtr = uap->hdtr;
1825 args.sbytes = uap->sbytes;
1826 args.flags = uap->flags;
1828 return (do_sendfile(td, &args, 1));
1830 #endif /* COMPAT_FREEBSD4 */
1833 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1834 struct uio *hdr_uio, struct uio *trl_uio, int compat)
1836 struct file *sock_fp;
1838 struct vm_object *obj = NULL;
1839 struct socket *so = NULL;
1840 struct mbuf *m = NULL;
1843 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1844 int error, hdrlen = 0, mnw = 0;
1845 struct sendfile_sync *sfs = NULL;
1848 * The file descriptor must be a regular file and have a
1849 * backing VM object.
1850 * File offset must be positive. If it goes beyond EOF
1851 * we send only the header/trailer and no payload data.
1853 AUDIT_ARG_FD(uap->fd);
1854 if ((error = fgetvp_read(td, uap->fd, CAP_READ, &vp)) != 0)
1856 vn_lock(vp, LK_SHARED | LK_RETRY);
1857 if (vp->v_type == VREG) {
1861 * Temporarily increase the backing VM
1862 * object's reference count so that a forced
1863 * reclamation of its vnode does not
1864 * immediately destroy it.
1866 VM_OBJECT_LOCK(obj);
1867 if ((obj->flags & OBJ_DEAD) == 0) {
1868 vm_object_reference_locked(obj);
1869 VM_OBJECT_UNLOCK(obj);
1871 VM_OBJECT_UNLOCK(obj);
1881 if (uap->offset < 0) {
1887 * The socket must be a stream socket and connected.
1888 * Remember if it a blocking or non-blocking socket.
1890 if ((error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_WRITE,
1891 &sock_fp, NULL)) != 0)
1893 so = sock_fp->f_data;
1894 if (so->so_type != SOCK_STREAM) {
1898 if ((so->so_state & SS_ISCONNECTED) == 0) {
1903 * Do not wait on memory allocations but return ENOMEM for
1904 * caller to retry later.
1905 * XXX: Experimental.
1907 if (uap->flags & SF_MNOWAIT)
1910 if (uap->flags & SF_SYNC) {
1911 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO);
1912 mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
1913 cv_init(&sfs->cv, "sendfile");
1917 error = mac_socket_check_send(td->td_ucred, so);
1922 /* If headers are specified copy them into mbufs. */
1923 if (hdr_uio != NULL) {
1924 hdr_uio->uio_td = td;
1925 hdr_uio->uio_rw = UIO_WRITE;
1926 if (hdr_uio->uio_resid > 0) {
1928 * In FBSD < 5.0 the nbytes to send also included
1929 * the header. If compat is specified subtract the
1930 * header size from nbytes.
1933 if (uap->nbytes > hdr_uio->uio_resid)
1934 uap->nbytes -= hdr_uio->uio_resid;
1938 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
1941 error = mnw ? EAGAIN : ENOBUFS;
1944 hdrlen = m_length(m, NULL);
1949 * Protect against multiple writers to the socket.
1951 * XXXRW: Historically this has assumed non-interruptibility, so now
1952 * we implement that, but possibly shouldn't.
1954 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
1957 * Loop through the pages of the file, starting with the requested
1958 * offset. Get a file page (do I/O if necessary), map the file page
1959 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1961 * This is done in two loops. The inner loop turns as many pages
1962 * as it can, up to available socket buffer space, without blocking
1963 * into mbufs to have it bulk delivered into the socket send buffer.
1964 * The outer loop checks the state and available space of the socket
1965 * and takes care of the overall progress.
1967 for (off = uap->offset, rem = uap->nbytes; ; ) {
1968 struct mbuf *mtail = NULL;
1974 * Check the socket state for ongoing connection,
1975 * no errors and space in socket buffer.
1976 * If space is low allow for the remainder of the
1977 * file to be processed if it fits the socket buffer.
1978 * Otherwise block in waiting for sufficient space
1979 * to proceed, or if the socket is nonblocking, return
1980 * to userland with EAGAIN while reporting how far
1982 * We wait until the socket buffer has significant free
1983 * space to do bulk sends. This makes good use of file
1984 * system read ahead and allows packet segmentation
1985 * offloading hardware to take over lots of work. If
1986 * we were not careful here we would send off only one
1989 SOCKBUF_LOCK(&so->so_snd);
1990 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
1991 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
1993 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1995 SOCKBUF_UNLOCK(&so->so_snd);
1997 } else if (so->so_error) {
1998 error = so->so_error;
2000 SOCKBUF_UNLOCK(&so->so_snd);
2003 space = sbspace(&so->so_snd);
2006 space < so->so_snd.sb_lowat)) {
2007 if (so->so_state & SS_NBIO) {
2008 SOCKBUF_UNLOCK(&so->so_snd);
2013 * sbwait drops the lock while sleeping.
2014 * When we loop back to retry_space the
2015 * state may have changed and we retest
2018 error = sbwait(&so->so_snd);
2020 * An error from sbwait usually indicates that we've
2021 * been interrupted by a signal. If we've sent anything
2022 * then return bytes sent, otherwise return the error.
2025 SOCKBUF_UNLOCK(&so->so_snd);
2030 SOCKBUF_UNLOCK(&so->so_snd);
2033 * Reduce space in the socket buffer by the size of
2034 * the header mbuf chain.
2035 * hdrlen is set to 0 after the first loop.
2040 * Loop and construct maximum sized mbuf chain to be bulk
2041 * dumped into socket buffer.
2043 while (space > loopbytes) {
2048 VM_OBJECT_LOCK(obj);
2050 * Calculate the amount to transfer.
2051 * Not to exceed a page, the EOF,
2052 * or the passed in nbytes.
2054 pgoff = (vm_offset_t)(off & PAGE_MASK);
2055 xfsize = omin(PAGE_SIZE - pgoff,
2056 obj->un_pager.vnp.vnp_size - uap->offset -
2057 fsbytes - loopbytes);
2059 rem = (uap->nbytes - fsbytes - loopbytes);
2061 rem = obj->un_pager.vnp.vnp_size -
2062 uap->offset - fsbytes - loopbytes;
2063 xfsize = omin(rem, xfsize);
2064 xfsize = omin(space - loopbytes, xfsize);
2066 VM_OBJECT_UNLOCK(obj);
2067 done = 1; /* all data sent */
2072 * Attempt to look up the page. Allocate
2073 * if not found or wait and loop if busy.
2075 pindex = OFF_TO_IDX(off);
2076 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2077 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY);
2080 * Check if page is valid for what we need,
2081 * otherwise initiate I/O.
2082 * If we already turned some pages into mbufs,
2083 * send them off before we come here again and
2086 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2087 VM_OBJECT_UNLOCK(obj);
2089 error = EAGAIN; /* send what we already got */
2090 else if (uap->flags & SF_NODISKIO)
2097 * Ensure that our page is still around
2098 * when the I/O completes.
2100 vm_page_io_start(pg);
2101 VM_OBJECT_UNLOCK(obj);
2104 * Get the page from backing store.
2106 error = vn_lock(vp, LK_SHARED);
2109 bsize = vp->v_mount->mnt_stat.f_iosize;
2112 * XXXMAC: Because we don't have fp->f_cred
2113 * here, we pass in NOCRED. This is probably
2114 * wrong, but is consistent with our original
2117 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
2118 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2119 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
2120 td->td_ucred, NOCRED, &resid, td);
2123 VM_OBJECT_LOCK(obj);
2124 vm_page_io_finish(pg);
2126 VM_OBJECT_UNLOCK(obj);
2131 vm_page_unwire(pg, 0);
2133 * See if anyone else might know about
2134 * this page. If not and it is not valid,
2137 if (pg->wire_count == 0 && pg->valid == 0 &&
2138 pg->busy == 0 && !(pg->oflags & VPO_BUSY))
2141 VM_OBJECT_UNLOCK(obj);
2142 if (error == EAGAIN)
2143 error = 0; /* not a real error */
2148 * Get a sendfile buf. When allocating the
2149 * first buffer for mbuf chain, we usually
2150 * wait as long as necessary, but this wait
2151 * can be interrupted. For consequent
2152 * buffers, do not sleep, since several
2153 * threads might exhaust the buffers and then
2156 sf = sf_buf_alloc(pg, (mnw || m != NULL) ? SFB_NOWAIT :
2159 mbstat.sf_allocfail++;
2161 vm_page_unwire(pg, 0);
2162 KASSERT(pg->object != NULL,
2163 ("kern_sendfile: object disappeared"));
2166 error = (mnw ? EAGAIN : EINTR);
2171 * Get an mbuf and set it up as having
2174 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2176 error = (mnw ? EAGAIN : ENOBUFS);
2177 sf_buf_mext((void *)sf_buf_kva(sf), sf);
2180 MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext,
2181 sfs, sf, M_RDONLY, EXT_SFBUF);
2182 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2185 /* Append to mbuf chain. */
2189 m_last(m)->m_next = m0;
2194 /* Keep track of bits processed. */
2195 loopbytes += xfsize;
2199 mtx_lock(&sfs->mtx);
2201 mtx_unlock(&sfs->mtx);
2205 /* Add the buffer chain to the socket buffer. */
2209 mlen = m_length(m, NULL);
2210 SOCKBUF_LOCK(&so->so_snd);
2211 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2213 SOCKBUF_UNLOCK(&so->so_snd);
2216 SOCKBUF_UNLOCK(&so->so_snd);
2217 CURVNET_SET(so->so_vnet);
2218 /* Avoid error aliasing. */
2219 err = (*so->so_proto->pr_usrreqs->pru_send)
2220 (so, 0, m, NULL, NULL, td);
2224 * We need two counters to get the
2225 * file offset and nbytes to send
2227 * - sbytes contains the total amount
2228 * of bytes sent, including headers.
2229 * - fsbytes contains the total amount
2230 * of bytes sent from the file.
2238 } else if (error == 0)
2240 m = NULL; /* pru_send always consumes */
2243 /* Quit outer loop on error or when we're done. */
2251 * Send trailers. Wimp out and use writev(2).
2253 if (trl_uio != NULL) {
2254 sbunlock(&so->so_snd);
2255 error = kern_writev(td, uap->s, trl_uio);
2257 sbytes += td->td_retval[0];
2262 sbunlock(&so->so_snd);
2265 * If there was no error we have to clear td->td_retval[0]
2266 * because it may have been set by writev.
2269 td->td_retval[0] = 0;
2271 if (uap->sbytes != NULL) {
2272 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2275 vm_object_deallocate(obj);
2284 mtx_lock(&sfs->mtx);
2285 if (sfs->count != 0)
2286 cv_wait(&sfs->cv, &sfs->mtx);
2287 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2288 cv_destroy(&sfs->cv);
2289 mtx_destroy(&sfs->mtx);
2293 if (error == ERESTART)
2301 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2302 * otherwise all return EOPNOTSUPP.
2303 * XXX: We should make this loadable one day.
2306 sys_sctp_peeloff(td, uap)
2308 struct sctp_peeloff_args /* {
2313 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2314 struct file *nfp = NULL;
2316 struct socket *head, *so;
2320 AUDIT_ARG_FD(uap->sd);
2321 error = fgetsock(td, uap->sd, CAP_PEELOFF, &head, &fflag);
2324 if (head->so_proto->pr_protocol != IPPROTO_SCTP) {
2328 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2332 * At this point we know we do have a assoc to pull
2333 * we proceed to get the fd setup. This may block
2337 error = falloc(td, &nfp, &fd, 0);
2340 td->td_retval[0] = fd;
2342 CURVNET_SET(head->so_vnet);
2343 so = sonewconn(head, SS_ISCONNECTED);
2347 * Before changing the flags on the socket, we have to bump the
2348 * reference count. Otherwise, if the protocol calls sofree(),
2349 * the socket will be released due to a zero refcount.
2352 soref(so); /* file descriptor reference */
2357 TAILQ_REMOVE(&head->so_comp, so, so_list);
2359 so->so_state |= (head->so_state & SS_NBIO);
2360 so->so_state &= ~SS_NOFDREF;
2361 so->so_qstate &= ~SQ_COMP;
2364 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2365 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2368 if (head->so_sigio != NULL)
2369 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2373 * close the new descriptor, assuming someone hasn't ripped it
2374 * out from under us.
2377 fdclose(td->td_proc->p_fd, nfp, fd, td);
2380 * Release explicitly held references before returning.
2390 return (EOPNOTSUPP);
2395 sys_sctp_generic_sendmsg (td, uap)
2397 struct sctp_generic_sendmsg_args /* {
2403 struct sctp_sndrcvinfo *sinfo,
2407 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2408 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2410 struct file *fp = NULL;
2412 struct sockaddr *to = NULL;
2414 struct uio *ktruio = NULL;
2417 struct iovec iov[1];
2418 cap_rights_t rights;
2421 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2429 error = getsockaddr(&to, uap->to, uap->tolen);
2434 rights |= CAP_CONNECT;
2437 AUDIT_ARG_FD(uap->sd);
2438 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2442 if (to && (KTRPOINT(td, KTR_STRUCT)))
2446 iov[0].iov_base = uap->msg;
2447 iov[0].iov_len = uap->mlen;
2449 so = (struct socket *)fp->f_data;
2450 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2455 error = mac_socket_check_send(td->td_ucred, so);
2461 auio.uio_iovcnt = 1;
2462 auio.uio_segflg = UIO_USERSPACE;
2463 auio.uio_rw = UIO_WRITE;
2465 auio.uio_offset = 0; /* XXX */
2467 len = auio.uio_resid = uap->mlen;
2468 CURVNET_SET(so->so_vnet);
2469 error = sctp_lower_sosend(so, to, &auio,
2470 (struct mbuf *)NULL, (struct mbuf *)NULL,
2471 uap->flags, u_sinfo, td);
2474 if (auio.uio_resid != len && (error == ERESTART ||
2475 error == EINTR || error == EWOULDBLOCK))
2477 /* Generation of SIGPIPE can be controlled per socket. */
2478 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2479 !(uap->flags & MSG_NOSIGNAL)) {
2480 PROC_LOCK(td->td_proc);
2481 tdsignal(td, SIGPIPE);
2482 PROC_UNLOCK(td->td_proc);
2486 td->td_retval[0] = len - auio.uio_resid;
2488 if (ktruio != NULL) {
2489 ktruio->uio_resid = td->td_retval[0];
2490 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2501 return (EOPNOTSUPP);
2506 sys_sctp_generic_sendmsg_iov(td, uap)
2508 struct sctp_generic_sendmsg_iov_args /* {
2514 struct sctp_sndrcvinfo *sinfo,
2518 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2519 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2521 struct file *fp = NULL;
2524 struct sockaddr *to = NULL;
2526 struct uio *ktruio = NULL;
2529 struct iovec *iov, *tiov;
2530 cap_rights_t rights;
2533 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2540 error = getsockaddr(&to, uap->to, uap->tolen);
2545 rights |= CAP_CONNECT;
2548 AUDIT_ARG_FD(uap->sd);
2549 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2553 #ifdef COMPAT_FREEBSD32
2554 if (SV_CURPROC_FLAG(SV_ILP32))
2555 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2556 uap->iovlen, &iov, EMSGSIZE);
2559 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2563 if (to && (KTRPOINT(td, KTR_STRUCT)))
2567 so = (struct socket *)fp->f_data;
2568 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2573 error = mac_socket_check_send(td->td_ucred, so);
2579 auio.uio_iovcnt = uap->iovlen;
2580 auio.uio_segflg = UIO_USERSPACE;
2581 auio.uio_rw = UIO_WRITE;
2583 auio.uio_offset = 0; /* XXX */
2586 for (i = 0; i <uap->iovlen; i++, tiov++) {
2587 if ((auio.uio_resid += tiov->iov_len) < 0) {
2592 len = auio.uio_resid;
2593 CURVNET_SET(so->so_vnet);
2594 error = sctp_lower_sosend(so, to, &auio,
2595 (struct mbuf *)NULL, (struct mbuf *)NULL,
2596 uap->flags, u_sinfo, td);
2599 if (auio.uio_resid != len && (error == ERESTART ||
2600 error == EINTR || error == EWOULDBLOCK))
2602 /* Generation of SIGPIPE can be controlled per socket */
2603 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2604 !(uap->flags & MSG_NOSIGNAL)) {
2605 PROC_LOCK(td->td_proc);
2606 tdsignal(td, SIGPIPE);
2607 PROC_UNLOCK(td->td_proc);
2611 td->td_retval[0] = len - auio.uio_resid;
2613 if (ktruio != NULL) {
2614 ktruio->uio_resid = td->td_retval[0];
2615 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2628 return (EOPNOTSUPP);
2633 sys_sctp_generic_recvmsg(td, uap)
2635 struct sctp_generic_recvmsg_args /* {
2639 struct sockaddr *from,
2640 __socklen_t *fromlenaddr,
2641 struct sctp_sndrcvinfo *sinfo,
2645 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2646 uint8_t sockbufstore[256];
2648 struct iovec *iov, *tiov;
2649 struct sctp_sndrcvinfo sinfo;
2651 struct file *fp = NULL;
2652 struct sockaddr *fromsa;
2658 struct uio *ktruio = NULL;
2661 AUDIT_ARG_FD(uap->sd);
2662 error = getsock_cap(td->td_proc->p_fd, uap->sd, CAP_READ, &fp, NULL);
2666 #ifdef COMPAT_FREEBSD32
2667 if (SV_CURPROC_FLAG(SV_ILP32))
2668 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2669 uap->iovlen, &iov, EMSGSIZE);
2672 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2677 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2682 error = mac_socket_check_receive(td->td_ucred, so);
2688 if (uap->fromlenaddr) {
2689 error = copyin(uap->fromlenaddr,
2690 &fromlen, sizeof (fromlen));
2697 if (uap->msg_flags) {
2698 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2706 auio.uio_iovcnt = uap->iovlen;
2707 auio.uio_segflg = UIO_USERSPACE;
2708 auio.uio_rw = UIO_READ;
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 fromsa = (struct sockaddr *)sockbufstore;
2723 if (KTRPOINT(td, KTR_GENIO))
2724 ktruio = cloneuio(&auio);
2726 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
2727 CURVNET_SET(so->so_vnet);
2728 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2729 fromsa, fromlen, &msg_flags,
2730 (struct sctp_sndrcvinfo *)&sinfo, 1);
2733 if (auio.uio_resid != len && (error == ERESTART ||
2734 error == EINTR || error == EWOULDBLOCK))
2738 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2741 if (ktruio != NULL) {
2742 ktruio->uio_resid = len - auio.uio_resid;
2743 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2748 td->td_retval[0] = len - auio.uio_resid;
2750 if (fromlen && uap->from) {
2752 if (len <= 0 || fromsa == 0)
2755 len = MIN(len, fromsa->sa_len);
2756 error = copyout(fromsa, uap->from, (size_t)len);
2760 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2766 if (KTRPOINT(td, KTR_STRUCT))
2767 ktrsockaddr(fromsa);
2769 if (uap->msg_flags) {
2770 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2783 return (EOPNOTSUPP);