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/sf_buf.h>
64 #include <sys/sysent.h>
65 #include <sys/socket.h>
66 #include <sys/socketvar.h>
67 #include <sys/signalvar.h>
68 #include <sys/syscallsubr.h>
69 #include <sys/sysctl.h>
71 #include <sys/vnode.h>
73 #include <sys/ktrace.h>
75 #ifdef COMPAT_FREEBSD32
76 #include <compat/freebsd32/freebsd32_util.h>
81 #include <security/audit/audit.h>
82 #include <security/mac/mac_framework.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
87 #include <vm/vm_pageout.h>
88 #include <vm/vm_kern.h>
89 #include <vm/vm_extern.h>
91 #if defined(INET) || defined(INET6)
93 #include <netinet/sctp.h>
94 #include <netinet/sctp_peeloff.h>
96 #endif /* INET || INET6 */
98 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
99 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
101 static int accept1(struct thread *td, struct accept_args *uap, int compat);
102 static int do_sendfile(struct thread *td, struct sendfile_args *uap, int compat);
103 static int getsockname1(struct thread *td, struct getsockname_args *uap,
105 static int getpeername1(struct thread *td, struct getpeername_args *uap,
109 * NSFBUFS-related variables and associated sysctls
115 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
116 "Maximum number of sendfile(2) sf_bufs available");
117 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
118 "Number of sendfile(2) sf_bufs at peak usage");
119 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
120 "Number of sendfile(2) sf_bufs in use");
123 * Convert a user file descriptor to a kernel file entry and check that, if
124 * it is a capability, the right rights are present. A reference on the file
125 * entry is held upon returning.
128 getsock_cap(struct filedesc *fdp, int fd, cap_rights_t rights,
129 struct file **fpp, u_int *fflagp)
133 struct file *fp_fromcap;
138 if ((fdp == NULL) || ((fp = fget_unlocked(fdp, fd)) == NULL))
142 * If the file descriptor is for a capability, test rights and use
143 * the file descriptor referenced by the capability.
145 error = cap_funwrap(fp, rights, &fp_fromcap);
147 fdrop(fp, curthread);
150 if (fp != fp_fromcap) {
152 fdrop(fp, curthread);
155 #endif /* CAPABILITIES */
156 if (fp->f_type != DTYPE_SOCKET) {
157 fdrop(fp, curthread);
161 *fflagp = fp->f_flag;
167 * System call interface to the socket abstraction.
169 #if defined(COMPAT_43)
170 #define COMPAT_OLDSOCK
176 struct socket_args /* {
182 struct filedesc *fdp;
187 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
189 error = mac_socket_check_create(td->td_ucred, uap->domain, uap->type,
194 fdp = td->td_proc->p_fd;
195 error = falloc(td, &fp, &fd, 0);
198 /* An extra reference on `fp' has been held for us by falloc(). */
199 error = socreate(uap->domain, &so, uap->type, uap->protocol,
202 fdclose(fdp, fp, fd, td);
204 finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &socketops);
205 td->td_retval[0] = fd;
215 struct bind_args /* {
224 if ((error = getsockaddr(&sa, uap->name, uap->namelen)) != 0)
227 error = kern_bind(td, uap->s, sa);
233 kern_bind(td, fd, 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.
458 /* check sa_len before it is destroyed */
459 if (*namelen > sa->sa_len)
460 *namelen = sa->sa_len;
462 if (KTRPOINT(td, KTR_STRUCT))
473 * close the new descriptor, assuming someone hasn't ripped it
477 fdclose(fdp, nfp, fd, td);
480 * Release explicitly held references before returning. We return
481 * a reference on nfp to the caller on success if they request it.
500 struct accept_args *uap;
503 return (accept1(td, uap, 0));
506 #ifdef COMPAT_OLDSOCK
510 struct accept_args *uap;
513 return (accept1(td, uap, 1));
515 #endif /* COMPAT_OLDSOCK */
521 struct connect_args /* {
530 error = getsockaddr(&sa, uap->name, uap->namelen);
534 error = kern_connect(td, uap->s, sa);
541 kern_connect(td, fd, sa)
552 error = getsock_cap(td->td_proc->p_fd, fd, CAP_CONNECT, &fp, NULL);
556 if (so->so_state & SS_ISCONNECTING) {
561 if (KTRPOINT(td, KTR_STRUCT))
565 error = mac_socket_check_connect(td->td_ucred, so, sa);
569 error = soconnect(so, sa, td);
572 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
577 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
578 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
581 if (error == EINTR || error == ERESTART)
587 error = so->so_error;
593 so->so_state &= ~SS_ISCONNECTING;
594 if (error == ERESTART)
602 kern_socketpair(struct thread *td, int domain, int type, int protocol,
605 struct filedesc *fdp = td->td_proc->p_fd;
606 struct file *fp1, *fp2;
607 struct socket *so1, *so2;
610 AUDIT_ARG_SOCKET(domain, type, protocol);
612 /* We might want to have a separate check for socket pairs. */
613 error = mac_socket_check_create(td->td_ucred, domain, type,
618 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
621 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
624 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
625 error = falloc(td, &fp1, &fd, 0);
629 fp1->f_data = so1; /* so1 already has ref count */
630 error = falloc(td, &fp2, &fd, 0);
633 fp2->f_data = so2; /* so2 already has ref count */
635 error = soconnect2(so1, so2);
638 if (type == SOCK_DGRAM) {
640 * Datagram socket connection is asymmetric.
642 error = soconnect2(so2, so1);
646 finit(fp1, FREAD | FWRITE, DTYPE_SOCKET, fp1->f_data, &socketops);
647 finit(fp2, FREAD | FWRITE, DTYPE_SOCKET, fp2->f_data, &socketops);
652 fdclose(fdp, fp2, rsv[1], td);
655 fdclose(fdp, fp1, rsv[0], td);
667 sys_socketpair(struct thread *td, struct socketpair_args *uap)
671 error = kern_socketpair(td, uap->domain, uap->type,
675 error = copyout(sv, uap->rsv, 2 * sizeof(int));
677 (void)kern_close(td, sv[0]);
678 (void)kern_close(td, sv[1]);
684 sendit(td, s, mp, flags)
690 struct mbuf *control;
694 #ifdef CAPABILITY_MODE
695 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
699 if (mp->msg_name != NULL) {
700 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
710 if (mp->msg_control) {
711 if (mp->msg_controllen < sizeof(struct cmsghdr)
712 #ifdef COMPAT_OLDSOCK
713 && mp->msg_flags != MSG_COMPAT
719 error = sockargs(&control, mp->msg_control,
720 mp->msg_controllen, MT_CONTROL);
723 #ifdef COMPAT_OLDSOCK
724 if (mp->msg_flags == MSG_COMPAT) {
727 M_PREPEND(control, sizeof(*cm), M_WAIT);
728 cm = mtod(control, struct cmsghdr *);
729 cm->cmsg_len = control->m_len;
730 cm->cmsg_level = SOL_SOCKET;
731 cm->cmsg_type = SCM_RIGHTS;
738 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
747 kern_sendit(td, s, mp, flags, control, segflg)
752 struct mbuf *control;
763 struct uio *ktruio = NULL;
768 if (mp->msg_name != NULL)
769 rights |= CAP_CONNECT;
770 error = getsock_cap(td->td_proc->p_fd, s, rights, &fp, NULL);
773 so = (struct socket *)fp->f_data;
776 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
777 ktrsockaddr(mp->msg_name);
780 if (mp->msg_name != NULL) {
781 error = mac_socket_check_connect(td->td_ucred, so,
786 error = mac_socket_check_send(td->td_ucred, so);
791 auio.uio_iov = mp->msg_iov;
792 auio.uio_iovcnt = mp->msg_iovlen;
793 auio.uio_segflg = segflg;
794 auio.uio_rw = UIO_WRITE;
796 auio.uio_offset = 0; /* XXX */
799 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
800 if ((auio.uio_resid += iov->iov_len) < 0) {
806 if (KTRPOINT(td, KTR_GENIO))
807 ktruio = cloneuio(&auio);
809 len = auio.uio_resid;
810 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
812 if (auio.uio_resid != len && (error == ERESTART ||
813 error == EINTR || error == EWOULDBLOCK))
815 /* Generation of SIGPIPE can be controlled per socket */
816 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
817 !(flags & MSG_NOSIGNAL)) {
818 PROC_LOCK(td->td_proc);
819 tdsignal(td, SIGPIPE);
820 PROC_UNLOCK(td->td_proc);
824 td->td_retval[0] = len - auio.uio_resid;
826 if (ktruio != NULL) {
827 ktruio->uio_resid = td->td_retval[0];
828 ktrgenio(s, UIO_WRITE, ktruio, error);
839 struct sendto_args /* {
852 msg.msg_name = uap->to;
853 msg.msg_namelen = uap->tolen;
857 #ifdef COMPAT_OLDSOCK
860 aiov.iov_base = uap->buf;
861 aiov.iov_len = uap->len;
862 error = sendit(td, uap->s, &msg, uap->flags);
866 #ifdef COMPAT_OLDSOCK
870 struct osend_args /* {
885 aiov.iov_base = uap->buf;
886 aiov.iov_len = uap->len;
889 error = sendit(td, uap->s, &msg, uap->flags);
896 struct osendmsg_args /* {
906 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
909 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
913 msg.msg_flags = MSG_COMPAT;
914 error = sendit(td, uap->s, &msg, uap->flags);
923 struct sendmsg_args /* {
933 error = copyin(uap->msg, &msg, sizeof (msg));
936 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
940 #ifdef COMPAT_OLDSOCK
943 error = sendit(td, uap->s, &msg, uap->flags);
949 kern_recvit(td, s, mp, fromseg, controlp)
953 enum uio_seg fromseg;
954 struct mbuf **controlp;
961 struct mbuf *m, *control = 0;
965 struct sockaddr *fromsa = 0;
967 struct uio *ktruio = NULL;
970 if (controlp != NULL)
974 error = getsock_cap(td->td_proc->p_fd, s, CAP_READ, &fp, NULL);
980 error = mac_socket_check_receive(td->td_ucred, so);
987 auio.uio_iov = mp->msg_iov;
988 auio.uio_iovcnt = mp->msg_iovlen;
989 auio.uio_segflg = UIO_USERSPACE;
990 auio.uio_rw = UIO_READ;
992 auio.uio_offset = 0; /* XXX */
995 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
996 if ((auio.uio_resid += iov->iov_len) < 0) {
1002 if (KTRPOINT(td, KTR_GENIO))
1003 ktruio = cloneuio(&auio);
1005 len = auio.uio_resid;
1006 error = soreceive(so, &fromsa, &auio, (struct mbuf **)0,
1007 (mp->msg_control || controlp) ? &control : (struct mbuf **)0,
1010 if (auio.uio_resid != (int)len && (error == ERESTART ||
1011 error == EINTR || error == EWOULDBLOCK))
1015 if (ktruio != NULL) {
1016 ktruio->uio_resid = (int)len - auio.uio_resid;
1017 ktrgenio(s, UIO_READ, ktruio, error);
1022 td->td_retval[0] = (int)len - auio.uio_resid;
1024 len = mp->msg_namelen;
1025 if (len <= 0 || fromsa == 0)
1028 /* save sa_len before it is destroyed by MSG_COMPAT */
1029 len = MIN(len, fromsa->sa_len);
1030 #ifdef COMPAT_OLDSOCK
1031 if (mp->msg_flags & MSG_COMPAT)
1032 ((struct osockaddr *)fromsa)->sa_family =
1035 if (fromseg == UIO_USERSPACE) {
1036 error = copyout(fromsa, mp->msg_name,
1041 bcopy(fromsa, mp->msg_name, len);
1043 mp->msg_namelen = len;
1045 if (mp->msg_control && controlp == NULL) {
1046 #ifdef COMPAT_OLDSOCK
1048 * We assume that old recvmsg calls won't receive access
1049 * rights and other control info, esp. as control info
1050 * is always optional and those options didn't exist in 4.3.
1051 * If we receive rights, trim the cmsghdr; anything else
1054 if (control && mp->msg_flags & MSG_COMPAT) {
1055 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1057 mtod(control, struct cmsghdr *)->cmsg_type !=
1059 mp->msg_controllen = 0;
1062 control->m_len -= sizeof (struct cmsghdr);
1063 control->m_data += sizeof (struct cmsghdr);
1066 len = mp->msg_controllen;
1068 mp->msg_controllen = 0;
1069 ctlbuf = mp->msg_control;
1071 while (m && len > 0) {
1072 unsigned int tocopy;
1074 if (len >= m->m_len)
1077 mp->msg_flags |= MSG_CTRUNC;
1081 if ((error = copyout(mtod(m, caddr_t),
1082 ctlbuf, tocopy)) != 0)
1089 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1094 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1095 ktrsockaddr(fromsa);
1098 free(fromsa, M_SONAME);
1100 if (error == 0 && controlp != NULL)
1101 *controlp = control;
1109 recvit(td, s, mp, namelenp)
1117 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1121 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1122 #ifdef COMPAT_OLDSOCK
1123 if (mp->msg_flags & MSG_COMPAT)
1124 error = 0; /* old recvfrom didn't check */
1131 sys_recvfrom(td, uap)
1133 struct recvfrom_args /* {
1138 struct sockaddr * __restrict from;
1139 socklen_t * __restrict fromlenaddr;
1146 if (uap->fromlenaddr) {
1147 error = copyin(uap->fromlenaddr,
1148 &msg.msg_namelen, sizeof (msg.msg_namelen));
1152 msg.msg_namelen = 0;
1154 msg.msg_name = uap->from;
1155 msg.msg_iov = &aiov;
1157 aiov.iov_base = uap->buf;
1158 aiov.iov_len = uap->len;
1159 msg.msg_control = 0;
1160 msg.msg_flags = uap->flags;
1161 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1166 #ifdef COMPAT_OLDSOCK
1170 struct recvfrom_args *uap;
1173 uap->flags |= MSG_COMPAT;
1174 return (sys_recvfrom(td, uap));
1178 #ifdef COMPAT_OLDSOCK
1182 struct orecv_args /* {
1194 msg.msg_namelen = 0;
1195 msg.msg_iov = &aiov;
1197 aiov.iov_base = uap->buf;
1198 aiov.iov_len = uap->len;
1199 msg.msg_control = 0;
1200 msg.msg_flags = uap->flags;
1201 error = recvit(td, uap->s, &msg, NULL);
1206 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1207 * overlays the new one, missing only the flags, and with the (old) access
1208 * rights where the control fields are now.
1213 struct orecvmsg_args /* {
1215 struct omsghdr *msg;
1223 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1226 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1229 msg.msg_flags = uap->flags | MSG_COMPAT;
1231 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1232 if (msg.msg_controllen && error == 0)
1233 error = copyout(&msg.msg_controllen,
1234 &uap->msg->msg_accrightslen, sizeof (int));
1241 sys_recvmsg(td, uap)
1243 struct recvmsg_args /* {
1250 struct iovec *uiov, *iov;
1253 error = copyin(uap->msg, &msg, sizeof (msg));
1256 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1259 msg.msg_flags = uap->flags;
1260 #ifdef COMPAT_OLDSOCK
1261 msg.msg_flags &= ~MSG_COMPAT;
1265 error = recvit(td, uap->s, &msg, NULL);
1268 error = copyout(&msg, uap->msg, sizeof(msg));
1276 sys_shutdown(td, uap)
1278 struct shutdown_args /* {
1287 AUDIT_ARG_FD(uap->s);
1288 error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_SHUTDOWN, &fp,
1292 error = soshutdown(so, uap->how);
1300 sys_setsockopt(td, uap)
1302 struct setsockopt_args /* {
1311 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1312 uap->val, UIO_USERSPACE, uap->valsize));
1316 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1322 enum uio_seg valseg;
1328 struct sockopt sopt;
1330 if (val == NULL && valsize != 0)
1332 if ((int)valsize < 0)
1335 sopt.sopt_dir = SOPT_SET;
1336 sopt.sopt_level = level;
1337 sopt.sopt_name = name;
1338 sopt.sopt_val = val;
1339 sopt.sopt_valsize = valsize;
1345 sopt.sopt_td = NULL;
1348 panic("kern_setsockopt called with bad valseg");
1352 error = getsock_cap(td->td_proc->p_fd, s, CAP_SETSOCKOPT, &fp, NULL);
1355 error = sosetopt(so, &sopt);
1363 sys_getsockopt(td, uap)
1365 struct getsockopt_args /* {
1369 void * __restrict val;
1370 socklen_t * __restrict avalsize;
1377 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1382 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1383 uap->val, UIO_USERSPACE, &valsize);
1386 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1391 * Kernel version of getsockopt.
1392 * optval can be a userland or userspace. optlen is always a kernel pointer.
1395 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1401 enum uio_seg valseg;
1407 struct sockopt sopt;
1411 if ((int)*valsize < 0)
1414 sopt.sopt_dir = SOPT_GET;
1415 sopt.sopt_level = level;
1416 sopt.sopt_name = name;
1417 sopt.sopt_val = val;
1418 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1424 sopt.sopt_td = NULL;
1427 panic("kern_getsockopt called with bad valseg");
1431 error = getsock_cap(td->td_proc->p_fd, s, CAP_GETSOCKOPT, &fp, NULL);
1434 error = sogetopt(so, &sopt);
1435 *valsize = sopt.sopt_valsize;
1442 * getsockname1() - Get socket name.
1446 getsockname1(td, uap, compat)
1448 struct getsockname_args /* {
1450 struct sockaddr * __restrict asa;
1451 socklen_t * __restrict alen;
1455 struct sockaddr *sa;
1459 error = copyin(uap->alen, &len, sizeof(len));
1463 error = kern_getsockname(td, uap->fdes, &sa, &len);
1468 #ifdef COMPAT_OLDSOCK
1470 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1472 error = copyout(sa, uap->asa, (u_int)len);
1476 error = copyout(&len, uap->alen, sizeof(len));
1481 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1493 error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETSOCKNAME, &fp, NULL);
1498 CURVNET_SET(so->so_vnet);
1499 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1506 len = MIN(*alen, (*sa)->sa_len);
1509 if (KTRPOINT(td, KTR_STRUCT))
1515 free(*sa, M_SONAME);
1522 sys_getsockname(td, uap)
1524 struct getsockname_args *uap;
1527 return (getsockname1(td, uap, 0));
1530 #ifdef COMPAT_OLDSOCK
1532 ogetsockname(td, uap)
1534 struct getsockname_args *uap;
1537 return (getsockname1(td, uap, 1));
1539 #endif /* COMPAT_OLDSOCK */
1542 * getpeername1() - Get name of peer for connected socket.
1546 getpeername1(td, uap, compat)
1548 struct getpeername_args /* {
1550 struct sockaddr * __restrict asa;
1551 socklen_t * __restrict alen;
1555 struct sockaddr *sa;
1559 error = copyin(uap->alen, &len, sizeof (len));
1563 error = kern_getpeername(td, uap->fdes, &sa, &len);
1568 #ifdef COMPAT_OLDSOCK
1570 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1572 error = copyout(sa, uap->asa, (u_int)len);
1576 error = copyout(&len, uap->alen, sizeof(len));
1581 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1593 error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETPEERNAME, &fp, NULL);
1597 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1602 CURVNET_SET(so->so_vnet);
1603 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1610 len = MIN(*alen, (*sa)->sa_len);
1613 if (KTRPOINT(td, KTR_STRUCT))
1618 free(*sa, M_SONAME);
1627 sys_getpeername(td, uap)
1629 struct getpeername_args *uap;
1632 return (getpeername1(td, uap, 0));
1635 #ifdef COMPAT_OLDSOCK
1637 ogetpeername(td, uap)
1639 struct ogetpeername_args *uap;
1642 /* XXX uap should have type `getpeername_args *' to begin with. */
1643 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1645 #endif /* COMPAT_OLDSOCK */
1648 sockargs(mp, buf, buflen, type)
1653 struct sockaddr *sa;
1657 if ((u_int)buflen > MLEN) {
1658 #ifdef COMPAT_OLDSOCK
1659 if (type == MT_SONAME && (u_int)buflen <= 112)
1660 buflen = MLEN; /* unix domain compat. hack */
1663 if ((u_int)buflen > MCLBYTES)
1666 m = m_get(M_WAIT, type);
1667 if ((u_int)buflen > MLEN)
1670 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1675 if (type == MT_SONAME) {
1676 sa = mtod(m, struct sockaddr *);
1678 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1679 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1680 sa->sa_family = sa->sa_len;
1682 sa->sa_len = buflen;
1689 getsockaddr(namp, uaddr, len)
1690 struct sockaddr **namp;
1694 struct sockaddr *sa;
1697 if (len > SOCK_MAXADDRLEN)
1698 return (ENAMETOOLONG);
1699 if (len < offsetof(struct sockaddr, sa_data[0]))
1701 sa = malloc(len, M_SONAME, M_WAITOK);
1702 error = copyin(uaddr, sa, len);
1706 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1707 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1708 sa->sa_family = sa->sa_len;
1716 #include <sys/condvar.h>
1718 struct sendfile_sync {
1725 * Detach mapped page and release resources back to the system.
1728 sf_buf_mext(void *addr, void *args)
1731 struct sendfile_sync *sfs;
1733 m = sf_buf_page(args);
1736 vm_page_unwire(m, 0);
1738 * Check for the object going away on us. This can
1739 * happen since we don't hold a reference to it.
1740 * If so, we're responsible for freeing the page.
1742 if (m->wire_count == 0 && m->object == NULL)
1748 mtx_lock(&sfs->mtx);
1749 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1750 if (--sfs->count == 0)
1751 cv_signal(&sfs->cv);
1752 mtx_unlock(&sfs->mtx);
1758 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1759 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1761 * Send a file specified by 'fd' and starting at 'offset' to a socket
1762 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1763 * 0. Optionally add a header and/or trailer to the socket output. If
1764 * specified, write the total number of bytes sent into *sbytes.
1767 sys_sendfile(struct thread *td, struct sendfile_args *uap)
1770 return (do_sendfile(td, uap, 0));
1774 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1776 struct sf_hdtr hdtr;
1777 struct uio *hdr_uio, *trl_uio;
1780 hdr_uio = trl_uio = NULL;
1782 if (uap->hdtr != NULL) {
1783 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1786 if (hdtr.headers != NULL) {
1787 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1791 if (hdtr.trailers != NULL) {
1792 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1799 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1802 free(hdr_uio, M_IOV);
1804 free(trl_uio, M_IOV);
1808 #ifdef COMPAT_FREEBSD4
1810 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1812 struct sendfile_args args;
1816 args.offset = uap->offset;
1817 args.nbytes = uap->nbytes;
1818 args.hdtr = uap->hdtr;
1819 args.sbytes = uap->sbytes;
1820 args.flags = uap->flags;
1822 return (do_sendfile(td, &args, 1));
1824 #endif /* COMPAT_FREEBSD4 */
1827 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1828 struct uio *hdr_uio, struct uio *trl_uio, int compat)
1830 struct file *sock_fp;
1832 struct vm_object *obj = NULL;
1833 struct socket *so = NULL;
1834 struct mbuf *m = NULL;
1837 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1838 int error, hdrlen = 0, mnw = 0;
1840 struct sendfile_sync *sfs = NULL;
1843 * The file descriptor must be a regular file and have a
1844 * backing VM object.
1845 * File offset must be positive. If it goes beyond EOF
1846 * we send only the header/trailer and no payload data.
1848 AUDIT_ARG_FD(uap->fd);
1849 if ((error = fgetvp_read(td, uap->fd, CAP_READ, &vp)) != 0)
1851 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1852 vn_lock(vp, LK_SHARED | LK_RETRY);
1853 if (vp->v_type == VREG) {
1857 * Temporarily increase the backing VM
1858 * object's reference count so that a forced
1859 * reclamation of its vnode does not
1860 * immediately destroy it.
1862 VM_OBJECT_LOCK(obj);
1863 if ((obj->flags & OBJ_DEAD) == 0) {
1864 vm_object_reference_locked(obj);
1865 VM_OBJECT_UNLOCK(obj);
1867 VM_OBJECT_UNLOCK(obj);
1873 VFS_UNLOCK_GIANT(vfslocked);
1878 if (uap->offset < 0) {
1884 * The socket must be a stream socket and connected.
1885 * Remember if it a blocking or non-blocking socket.
1887 if ((error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_WRITE,
1888 &sock_fp, NULL)) != 0)
1890 so = sock_fp->f_data;
1891 if (so->so_type != SOCK_STREAM) {
1895 if ((so->so_state & SS_ISCONNECTED) == 0) {
1900 * Do not wait on memory allocations but return ENOMEM for
1901 * caller to retry later.
1902 * XXX: Experimental.
1904 if (uap->flags & SF_MNOWAIT)
1907 if (uap->flags & SF_SYNC) {
1908 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO);
1909 mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
1910 cv_init(&sfs->cv, "sendfile");
1914 error = mac_socket_check_send(td->td_ucred, so);
1919 /* If headers are specified copy them into mbufs. */
1920 if (hdr_uio != NULL) {
1921 hdr_uio->uio_td = td;
1922 hdr_uio->uio_rw = UIO_WRITE;
1923 if (hdr_uio->uio_resid > 0) {
1925 * In FBSD < 5.0 the nbytes to send also included
1926 * the header. If compat is specified subtract the
1927 * header size from nbytes.
1930 if (uap->nbytes > hdr_uio->uio_resid)
1931 uap->nbytes -= hdr_uio->uio_resid;
1935 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
1938 error = mnw ? EAGAIN : ENOBUFS;
1941 hdrlen = m_length(m, NULL);
1946 * Protect against multiple writers to the socket.
1948 * XXXRW: Historically this has assumed non-interruptibility, so now
1949 * we implement that, but possibly shouldn't.
1951 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
1954 * Loop through the pages of the file, starting with the requested
1955 * offset. Get a file page (do I/O if necessary), map the file page
1956 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1958 * This is done in two loops. The inner loop turns as many pages
1959 * as it can, up to available socket buffer space, without blocking
1960 * into mbufs to have it bulk delivered into the socket send buffer.
1961 * The outer loop checks the state and available space of the socket
1962 * and takes care of the overall progress.
1964 for (off = uap->offset, rem = uap->nbytes; ; ) {
1970 * Check the socket state for ongoing connection,
1971 * no errors and space in socket buffer.
1972 * If space is low allow for the remainder of the
1973 * file to be processed if it fits the socket buffer.
1974 * Otherwise block in waiting for sufficient space
1975 * to proceed, or if the socket is nonblocking, return
1976 * to userland with EAGAIN while reporting how far
1978 * We wait until the socket buffer has significant free
1979 * space to do bulk sends. This makes good use of file
1980 * system read ahead and allows packet segmentation
1981 * offloading hardware to take over lots of work. If
1982 * we were not careful here we would send off only one
1985 SOCKBUF_LOCK(&so->so_snd);
1986 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
1987 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
1989 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1991 SOCKBUF_UNLOCK(&so->so_snd);
1993 } else if (so->so_error) {
1994 error = so->so_error;
1996 SOCKBUF_UNLOCK(&so->so_snd);
1999 space = sbspace(&so->so_snd);
2002 space < so->so_snd.sb_lowat)) {
2003 if (so->so_state & SS_NBIO) {
2004 SOCKBUF_UNLOCK(&so->so_snd);
2009 * sbwait drops the lock while sleeping.
2010 * When we loop back to retry_space the
2011 * state may have changed and we retest
2014 error = sbwait(&so->so_snd);
2016 * An error from sbwait usually indicates that we've
2017 * been interrupted by a signal. If we've sent anything
2018 * then return bytes sent, otherwise return the error.
2021 SOCKBUF_UNLOCK(&so->so_snd);
2026 SOCKBUF_UNLOCK(&so->so_snd);
2029 * Reduce space in the socket buffer by the size of
2030 * the header mbuf chain.
2031 * hdrlen is set to 0 after the first loop.
2036 * Loop and construct maximum sized mbuf chain to be bulk
2037 * dumped into socket buffer.
2039 while (space > loopbytes) {
2044 VM_OBJECT_LOCK(obj);
2046 * Calculate the amount to transfer.
2047 * Not to exceed a page, the EOF,
2048 * or the passed in nbytes.
2050 pgoff = (vm_offset_t)(off & PAGE_MASK);
2051 xfsize = omin(PAGE_SIZE - pgoff,
2052 obj->un_pager.vnp.vnp_size - uap->offset -
2053 fsbytes - loopbytes);
2055 rem = (uap->nbytes - fsbytes - loopbytes);
2057 rem = obj->un_pager.vnp.vnp_size -
2058 uap->offset - fsbytes - loopbytes;
2059 xfsize = omin(rem, xfsize);
2060 xfsize = omin(space - loopbytes, xfsize);
2062 VM_OBJECT_UNLOCK(obj);
2063 done = 1; /* all data sent */
2068 * Attempt to look up the page. Allocate
2069 * if not found or wait and loop if busy.
2071 pindex = OFF_TO_IDX(off);
2072 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2073 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY);
2076 * Check if page is valid for what we need,
2077 * otherwise initiate I/O.
2078 * If we already turned some pages into mbufs,
2079 * send them off before we come here again and
2082 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2083 VM_OBJECT_UNLOCK(obj);
2085 error = EAGAIN; /* send what we already got */
2086 else if (uap->flags & SF_NODISKIO)
2092 * Ensure that our page is still around
2093 * when the I/O completes.
2095 vm_page_io_start(pg);
2096 VM_OBJECT_UNLOCK(obj);
2099 * Get the page from backing store.
2101 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2102 error = vn_lock(vp, LK_SHARED);
2105 bsize = vp->v_mount->mnt_stat.f_iosize;
2108 * XXXMAC: Because we don't have fp->f_cred
2109 * here, we pass in NOCRED. This is probably
2110 * wrong, but is consistent with our original
2113 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
2114 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2115 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
2116 td->td_ucred, NOCRED, &resid, td);
2119 VFS_UNLOCK_GIANT(vfslocked);
2120 VM_OBJECT_LOCK(obj);
2121 vm_page_io_finish(pg);
2123 VM_OBJECT_UNLOCK(obj);
2128 vm_page_unwire(pg, 0);
2130 * See if anyone else might know about
2131 * this page. If not and it is not valid,
2134 if (pg->wire_count == 0 && pg->valid == 0 &&
2135 pg->busy == 0 && !(pg->oflags & VPO_BUSY))
2138 VM_OBJECT_UNLOCK(obj);
2139 if (error == EAGAIN)
2140 error = 0; /* not a real error */
2145 * Get a sendfile buf. When allocating the
2146 * first buffer for mbuf chain, we usually
2147 * wait as long as necessary, but this wait
2148 * can be interrupted. For consequent
2149 * buffers, do not sleep, since several
2150 * threads might exhaust the buffers and then
2153 sf = sf_buf_alloc(pg, (mnw || m != NULL) ? SFB_NOWAIT :
2156 mbstat.sf_allocfail++;
2158 vm_page_unwire(pg, 0);
2159 KASSERT(pg->object != NULL,
2160 ("kern_sendfile: object disappeared"));
2163 error = (mnw ? EAGAIN : EINTR);
2168 * Get an mbuf and set it up as having
2171 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2173 error = (mnw ? EAGAIN : ENOBUFS);
2174 sf_buf_mext((void *)sf_buf_kva(sf), sf);
2177 MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext,
2178 sfs, sf, M_RDONLY, EXT_SFBUF);
2179 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2182 /* Append to mbuf chain. */
2188 /* Keep track of bits processed. */
2189 loopbytes += xfsize;
2193 mtx_lock(&sfs->mtx);
2195 mtx_unlock(&sfs->mtx);
2199 /* Add the buffer chain to the socket buffer. */
2203 mlen = m_length(m, NULL);
2204 SOCKBUF_LOCK(&so->so_snd);
2205 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2207 SOCKBUF_UNLOCK(&so->so_snd);
2210 SOCKBUF_UNLOCK(&so->so_snd);
2211 CURVNET_SET(so->so_vnet);
2212 /* Avoid error aliasing. */
2213 err = (*so->so_proto->pr_usrreqs->pru_send)
2214 (so, 0, m, NULL, NULL, td);
2218 * We need two counters to get the
2219 * file offset and nbytes to send
2221 * - sbytes contains the total amount
2222 * of bytes sent, including headers.
2223 * - fsbytes contains the total amount
2224 * of bytes sent from the file.
2232 } else if (error == 0)
2234 m = NULL; /* pru_send always consumes */
2237 /* Quit outer loop on error or when we're done. */
2245 * Send trailers. Wimp out and use writev(2).
2247 if (trl_uio != NULL) {
2248 sbunlock(&so->so_snd);
2249 error = kern_writev(td, uap->s, trl_uio);
2251 sbytes += td->td_retval[0];
2256 sbunlock(&so->so_snd);
2259 * If there was no error we have to clear td->td_retval[0]
2260 * because it may have been set by writev.
2263 td->td_retval[0] = 0;
2265 if (uap->sbytes != NULL) {
2266 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2269 vm_object_deallocate(obj);
2271 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2273 VFS_UNLOCK_GIANT(vfslocked);
2281 mtx_lock(&sfs->mtx);
2282 if (sfs->count != 0)
2283 cv_wait(&sfs->cv, &sfs->mtx);
2284 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2285 cv_destroy(&sfs->cv);
2286 mtx_destroy(&sfs->mtx);
2290 if (error == ERESTART)
2298 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2299 * otherwise all return EOPNOTSUPP.
2300 * XXX: We should make this loadable one day.
2303 sys_sctp_peeloff(td, uap)
2305 struct sctp_peeloff_args /* {
2310 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2311 struct filedesc *fdp;
2312 struct file *nfp = NULL;
2314 struct socket *head, *so;
2318 fdp = td->td_proc->p_fd;
2319 AUDIT_ARG_FD(uap->sd);
2320 error = fgetsock(td, uap->sd, CAP_PEELOFF, &head, &fflag);
2323 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2327 * At this point we know we do have a assoc to pull
2328 * we proceed to get the fd setup. This may block
2332 error = falloc(td, &nfp, &fd, 0);
2335 td->td_retval[0] = fd;
2337 CURVNET_SET(head->so_vnet);
2338 so = sonewconn(head, SS_ISCONNECTED);
2342 * Before changing the flags on the socket, we have to bump the
2343 * reference count. Otherwise, if the protocol calls sofree(),
2344 * the socket will be released due to a zero refcount.
2347 soref(so); /* file descriptor reference */
2352 TAILQ_REMOVE(&head->so_comp, so, so_list);
2354 so->so_state |= (head->so_state & SS_NBIO);
2355 so->so_state &= ~SS_NOFDREF;
2356 so->so_qstate &= ~SQ_COMP;
2359 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2360 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2363 if (head->so_sigio != NULL)
2364 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2368 * close the new descriptor, assuming someone hasn't ripped it
2369 * out from under us.
2372 fdclose(fdp, nfp, fd, td);
2375 * Release explicitly held references before returning.
2385 return (EOPNOTSUPP);
2390 sys_sctp_generic_sendmsg (td, uap)
2392 struct sctp_generic_sendmsg_args /* {
2398 struct sctp_sndrcvinfo *sinfo,
2402 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2403 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2405 struct file *fp = NULL;
2407 struct sockaddr *to = NULL;
2409 struct uio *ktruio = NULL;
2412 struct iovec iov[1];
2413 cap_rights_t rights;
2416 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2424 error = getsockaddr(&to, uap->to, uap->tolen);
2429 rights |= CAP_CONNECT;
2432 AUDIT_ARG_FD(uap->sd);
2433 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2437 if (to && (KTRPOINT(td, KTR_STRUCT)))
2441 iov[0].iov_base = uap->msg;
2442 iov[0].iov_len = uap->mlen;
2444 so = (struct socket *)fp->f_data;
2446 error = mac_socket_check_send(td->td_ucred, so);
2452 auio.uio_iovcnt = 1;
2453 auio.uio_segflg = UIO_USERSPACE;
2454 auio.uio_rw = UIO_WRITE;
2456 auio.uio_offset = 0; /* XXX */
2458 len = auio.uio_resid = uap->mlen;
2459 CURVNET_SET(so->so_vnet);
2460 error = sctp_lower_sosend(so, to, &auio,
2461 (struct mbuf *)NULL, (struct mbuf *)NULL,
2462 uap->flags, u_sinfo, td);
2465 if (auio.uio_resid != len && (error == ERESTART ||
2466 error == EINTR || error == EWOULDBLOCK))
2468 /* Generation of SIGPIPE can be controlled per socket. */
2469 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2470 !(uap->flags & MSG_NOSIGNAL)) {
2471 PROC_LOCK(td->td_proc);
2472 tdsignal(td, SIGPIPE);
2473 PROC_UNLOCK(td->td_proc);
2477 td->td_retval[0] = len - auio.uio_resid;
2479 if (ktruio != NULL) {
2480 ktruio->uio_resid = td->td_retval[0];
2481 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2492 return (EOPNOTSUPP);
2497 sys_sctp_generic_sendmsg_iov(td, uap)
2499 struct sctp_generic_sendmsg_iov_args /* {
2505 struct sctp_sndrcvinfo *sinfo,
2509 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2510 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2512 struct file *fp = NULL;
2513 int error=0, len, i;
2514 struct sockaddr *to = NULL;
2516 struct uio *ktruio = NULL;
2519 struct iovec *iov, *tiov;
2520 cap_rights_t rights;
2523 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2530 error = getsockaddr(&to, uap->to, uap->tolen);
2535 rights |= CAP_CONNECT;
2538 AUDIT_ARG_FD(uap->sd);
2539 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2543 #ifdef COMPAT_FREEBSD32
2544 if (SV_CURPROC_FLAG(SV_ILP32))
2545 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2546 uap->iovlen, &iov, EMSGSIZE);
2549 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2553 if (to && (KTRPOINT(td, KTR_STRUCT)))
2557 so = (struct socket *)fp->f_data;
2559 error = mac_socket_check_send(td->td_ucred, so);
2565 auio.uio_iovcnt = uap->iovlen;
2566 auio.uio_segflg = UIO_USERSPACE;
2567 auio.uio_rw = UIO_WRITE;
2569 auio.uio_offset = 0; /* XXX */
2572 for (i = 0; i <uap->iovlen; i++, tiov++) {
2573 if ((auio.uio_resid += tiov->iov_len) < 0) {
2578 len = auio.uio_resid;
2579 CURVNET_SET(so->so_vnet);
2580 error = sctp_lower_sosend(so, to, &auio,
2581 (struct mbuf *)NULL, (struct mbuf *)NULL,
2582 uap->flags, u_sinfo, td);
2585 if (auio.uio_resid != len && (error == ERESTART ||
2586 error == EINTR || error == EWOULDBLOCK))
2588 /* Generation of SIGPIPE can be controlled per socket */
2589 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2590 !(uap->flags & MSG_NOSIGNAL)) {
2591 PROC_LOCK(td->td_proc);
2592 tdsignal(td, SIGPIPE);
2593 PROC_UNLOCK(td->td_proc);
2597 td->td_retval[0] = len - auio.uio_resid;
2599 if (ktruio != NULL) {
2600 ktruio->uio_resid = td->td_retval[0];
2601 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2614 return (EOPNOTSUPP);
2619 sys_sctp_generic_recvmsg(td, uap)
2621 struct sctp_generic_recvmsg_args /* {
2625 struct sockaddr *from,
2626 __socklen_t *fromlenaddr,
2627 struct sctp_sndrcvinfo *sinfo,
2631 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2632 uint8_t sockbufstore[256];
2634 struct iovec *iov, *tiov;
2635 struct sctp_sndrcvinfo sinfo;
2637 struct file *fp = NULL;
2638 struct sockaddr *fromsa;
2640 int len, i, msg_flags;
2643 struct uio *ktruio = NULL;
2646 AUDIT_ARG_FD(uap->sd);
2647 error = getsock_cap(td->td_proc->p_fd, uap->sd, CAP_READ, &fp, NULL);
2651 #ifdef COMPAT_FREEBSD32
2652 if (SV_CURPROC_FLAG(SV_ILP32))
2653 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2654 uap->iovlen, &iov, EMSGSIZE);
2657 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2663 error = mac_socket_check_receive(td->td_ucred, so);
2669 if (uap->fromlenaddr) {
2670 error = copyin(uap->fromlenaddr,
2671 &fromlen, sizeof (fromlen));
2678 if (uap->msg_flags) {
2679 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2687 auio.uio_iovcnt = uap->iovlen;
2688 auio.uio_segflg = UIO_USERSPACE;
2689 auio.uio_rw = UIO_READ;
2691 auio.uio_offset = 0; /* XXX */
2694 for (i = 0; i <uap->iovlen; i++, tiov++) {
2695 if ((auio.uio_resid += tiov->iov_len) < 0) {
2700 len = auio.uio_resid;
2701 fromsa = (struct sockaddr *)sockbufstore;
2704 if (KTRPOINT(td, KTR_GENIO))
2705 ktruio = cloneuio(&auio);
2707 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
2708 CURVNET_SET(so->so_vnet);
2709 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2710 fromsa, fromlen, &msg_flags,
2711 (struct sctp_sndrcvinfo *)&sinfo, 1);
2714 if (auio.uio_resid != (int)len && (error == ERESTART ||
2715 error == EINTR || error == EWOULDBLOCK))
2719 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2722 if (ktruio != NULL) {
2723 ktruio->uio_resid = (int)len - auio.uio_resid;
2724 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2729 td->td_retval[0] = (int)len - auio.uio_resid;
2731 if (fromlen && uap->from) {
2733 if (len <= 0 || fromsa == 0)
2736 len = MIN(len, fromsa->sa_len);
2737 error = copyout(fromsa, uap->from, (unsigned)len);
2741 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2747 if (KTRPOINT(td, KTR_STRUCT))
2748 ktrsockaddr(fromsa);
2750 if (uap->msg_flags) {
2751 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2764 return (EOPNOTSUPP);