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 != len && (error == ERESTART ||
1011 error == EINTR || error == EWOULDBLOCK))
1015 if (ktruio != NULL) {
1016 ktruio->uio_resid = len - auio.uio_resid;
1017 ktrgenio(s, UIO_READ, ktruio, error);
1022 td->td_retval[0] = 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; ; ) {
1965 struct mbuf *mtail = NULL;
1971 * Check the socket state for ongoing connection,
1972 * no errors and space in socket buffer.
1973 * If space is low allow for the remainder of the
1974 * file to be processed if it fits the socket buffer.
1975 * Otherwise block in waiting for sufficient space
1976 * to proceed, or if the socket is nonblocking, return
1977 * to userland with EAGAIN while reporting how far
1979 * We wait until the socket buffer has significant free
1980 * space to do bulk sends. This makes good use of file
1981 * system read ahead and allows packet segmentation
1982 * offloading hardware to take over lots of work. If
1983 * we were not careful here we would send off only one
1986 SOCKBUF_LOCK(&so->so_snd);
1987 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
1988 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
1990 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1992 SOCKBUF_UNLOCK(&so->so_snd);
1994 } else if (so->so_error) {
1995 error = so->so_error;
1997 SOCKBUF_UNLOCK(&so->so_snd);
2000 space = sbspace(&so->so_snd);
2003 space < so->so_snd.sb_lowat)) {
2004 if (so->so_state & SS_NBIO) {
2005 SOCKBUF_UNLOCK(&so->so_snd);
2010 * sbwait drops the lock while sleeping.
2011 * When we loop back to retry_space the
2012 * state may have changed and we retest
2015 error = sbwait(&so->so_snd);
2017 * An error from sbwait usually indicates that we've
2018 * been interrupted by a signal. If we've sent anything
2019 * then return bytes sent, otherwise return the error.
2022 SOCKBUF_UNLOCK(&so->so_snd);
2027 SOCKBUF_UNLOCK(&so->so_snd);
2030 * Reduce space in the socket buffer by the size of
2031 * the header mbuf chain.
2032 * hdrlen is set to 0 after the first loop.
2037 * Loop and construct maximum sized mbuf chain to be bulk
2038 * dumped into socket buffer.
2040 while (space > loopbytes) {
2045 VM_OBJECT_LOCK(obj);
2047 * Calculate the amount to transfer.
2048 * Not to exceed a page, the EOF,
2049 * or the passed in nbytes.
2051 pgoff = (vm_offset_t)(off & PAGE_MASK);
2052 xfsize = omin(PAGE_SIZE - pgoff,
2053 obj->un_pager.vnp.vnp_size - uap->offset -
2054 fsbytes - loopbytes);
2056 rem = (uap->nbytes - fsbytes - loopbytes);
2058 rem = obj->un_pager.vnp.vnp_size -
2059 uap->offset - fsbytes - loopbytes;
2060 xfsize = omin(rem, xfsize);
2061 xfsize = omin(space - loopbytes, xfsize);
2063 VM_OBJECT_UNLOCK(obj);
2064 done = 1; /* all data sent */
2069 * Attempt to look up the page. Allocate
2070 * if not found or wait and loop if busy.
2072 pindex = OFF_TO_IDX(off);
2073 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2074 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY);
2077 * Check if page is valid for what we need,
2078 * otherwise initiate I/O.
2079 * If we already turned some pages into mbufs,
2080 * send them off before we come here again and
2083 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2084 VM_OBJECT_UNLOCK(obj);
2086 error = EAGAIN; /* send what we already got */
2087 else if (uap->flags & SF_NODISKIO)
2094 * Ensure that our page is still around
2095 * when the I/O completes.
2097 vm_page_io_start(pg);
2098 VM_OBJECT_UNLOCK(obj);
2101 * Get the page from backing store.
2103 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2104 error = vn_lock(vp, LK_SHARED);
2107 bsize = vp->v_mount->mnt_stat.f_iosize;
2110 * XXXMAC: Because we don't have fp->f_cred
2111 * here, we pass in NOCRED. This is probably
2112 * wrong, but is consistent with our original
2115 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
2116 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2117 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
2118 td->td_ucred, NOCRED, &resid, td);
2121 VFS_UNLOCK_GIANT(vfslocked);
2122 VM_OBJECT_LOCK(obj);
2123 vm_page_io_finish(pg);
2125 VM_OBJECT_UNLOCK(obj);
2130 vm_page_unwire(pg, 0);
2132 * See if anyone else might know about
2133 * this page. If not and it is not valid,
2136 if (pg->wire_count == 0 && pg->valid == 0 &&
2137 pg->busy == 0 && !(pg->oflags & VPO_BUSY))
2140 VM_OBJECT_UNLOCK(obj);
2141 if (error == EAGAIN)
2142 error = 0; /* not a real error */
2147 * Get a sendfile buf. When allocating the
2148 * first buffer for mbuf chain, we usually
2149 * wait as long as necessary, but this wait
2150 * can be interrupted. For consequent
2151 * buffers, do not sleep, since several
2152 * threads might exhaust the buffers and then
2155 sf = sf_buf_alloc(pg, (mnw || m != NULL) ? SFB_NOWAIT :
2158 mbstat.sf_allocfail++;
2160 vm_page_unwire(pg, 0);
2161 KASSERT(pg->object != NULL,
2162 ("kern_sendfile: object disappeared"));
2165 error = (mnw ? EAGAIN : EINTR);
2170 * Get an mbuf and set it up as having
2173 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2175 error = (mnw ? EAGAIN : ENOBUFS);
2176 sf_buf_mext((void *)sf_buf_kva(sf), sf);
2179 MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext,
2180 sfs, sf, M_RDONLY, EXT_SFBUF);
2181 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2184 /* Append to mbuf chain. */
2188 m_last(m)->m_next = m0;
2193 /* Keep track of bits processed. */
2194 loopbytes += xfsize;
2198 mtx_lock(&sfs->mtx);
2200 mtx_unlock(&sfs->mtx);
2204 /* Add the buffer chain to the socket buffer. */
2208 mlen = m_length(m, NULL);
2209 SOCKBUF_LOCK(&so->so_snd);
2210 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2212 SOCKBUF_UNLOCK(&so->so_snd);
2215 SOCKBUF_UNLOCK(&so->so_snd);
2216 CURVNET_SET(so->so_vnet);
2217 /* Avoid error aliasing. */
2218 err = (*so->so_proto->pr_usrreqs->pru_send)
2219 (so, 0, m, NULL, NULL, td);
2223 * We need two counters to get the
2224 * file offset and nbytes to send
2226 * - sbytes contains the total amount
2227 * of bytes sent, including headers.
2228 * - fsbytes contains the total amount
2229 * of bytes sent from the file.
2237 } else if (error == 0)
2239 m = NULL; /* pru_send always consumes */
2242 /* Quit outer loop on error or when we're done. */
2250 * Send trailers. Wimp out and use writev(2).
2252 if (trl_uio != NULL) {
2253 sbunlock(&so->so_snd);
2254 error = kern_writev(td, uap->s, trl_uio);
2256 sbytes += td->td_retval[0];
2261 sbunlock(&so->so_snd);
2264 * If there was no error we have to clear td->td_retval[0]
2265 * because it may have been set by writev.
2268 td->td_retval[0] = 0;
2270 if (uap->sbytes != NULL) {
2271 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2274 vm_object_deallocate(obj);
2276 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2278 VFS_UNLOCK_GIANT(vfslocked);
2286 mtx_lock(&sfs->mtx);
2287 if (sfs->count != 0)
2288 cv_wait(&sfs->cv, &sfs->mtx);
2289 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2290 cv_destroy(&sfs->cv);
2291 mtx_destroy(&sfs->mtx);
2295 if (error == ERESTART)
2303 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2304 * otherwise all return EOPNOTSUPP.
2305 * XXX: We should make this loadable one day.
2308 sys_sctp_peeloff(td, uap)
2310 struct sctp_peeloff_args /* {
2315 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2316 struct filedesc *fdp;
2317 struct file *nfp = NULL;
2319 struct socket *head, *so;
2323 fdp = td->td_proc->p_fd;
2324 AUDIT_ARG_FD(uap->sd);
2325 error = fgetsock(td, uap->sd, CAP_PEELOFF, &head, &fflag);
2328 if (head->so_proto->pr_protocol != IPPROTO_SCTP) {
2332 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2336 * At this point we know we do have a assoc to pull
2337 * we proceed to get the fd setup. This may block
2341 error = falloc(td, &nfp, &fd, 0);
2344 td->td_retval[0] = fd;
2346 CURVNET_SET(head->so_vnet);
2347 so = sonewconn(head, SS_ISCONNECTED);
2351 * Before changing the flags on the socket, we have to bump the
2352 * reference count. Otherwise, if the protocol calls sofree(),
2353 * the socket will be released due to a zero refcount.
2356 soref(so); /* file descriptor reference */
2361 TAILQ_REMOVE(&head->so_comp, so, so_list);
2363 so->so_state |= (head->so_state & SS_NBIO);
2364 so->so_state &= ~SS_NOFDREF;
2365 so->so_qstate &= ~SQ_COMP;
2368 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2369 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2372 if (head->so_sigio != NULL)
2373 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2377 * close the new descriptor, assuming someone hasn't ripped it
2378 * out from under us.
2381 fdclose(fdp, nfp, fd, td);
2384 * Release explicitly held references before returning.
2394 return (EOPNOTSUPP);
2399 sys_sctp_generic_sendmsg (td, uap)
2401 struct sctp_generic_sendmsg_args /* {
2407 struct sctp_sndrcvinfo *sinfo,
2411 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2412 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2414 struct file *fp = NULL;
2416 struct sockaddr *to = NULL;
2418 struct uio *ktruio = NULL;
2421 struct iovec iov[1];
2422 cap_rights_t rights;
2425 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2433 error = getsockaddr(&to, uap->to, uap->tolen);
2438 rights |= CAP_CONNECT;
2441 AUDIT_ARG_FD(uap->sd);
2442 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2446 if (to && (KTRPOINT(td, KTR_STRUCT)))
2450 iov[0].iov_base = uap->msg;
2451 iov[0].iov_len = uap->mlen;
2453 so = (struct socket *)fp->f_data;
2454 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2459 error = mac_socket_check_send(td->td_ucred, so);
2465 auio.uio_iovcnt = 1;
2466 auio.uio_segflg = UIO_USERSPACE;
2467 auio.uio_rw = UIO_WRITE;
2469 auio.uio_offset = 0; /* XXX */
2471 len = auio.uio_resid = uap->mlen;
2472 CURVNET_SET(so->so_vnet);
2473 error = sctp_lower_sosend(so, to, &auio,
2474 (struct mbuf *)NULL, (struct mbuf *)NULL,
2475 uap->flags, u_sinfo, td);
2478 if (auio.uio_resid != len && (error == ERESTART ||
2479 error == EINTR || error == EWOULDBLOCK))
2481 /* Generation of SIGPIPE can be controlled per socket. */
2482 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2483 !(uap->flags & MSG_NOSIGNAL)) {
2484 PROC_LOCK(td->td_proc);
2485 tdsignal(td, SIGPIPE);
2486 PROC_UNLOCK(td->td_proc);
2490 td->td_retval[0] = len - auio.uio_resid;
2492 if (ktruio != NULL) {
2493 ktruio->uio_resid = td->td_retval[0];
2494 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2505 return (EOPNOTSUPP);
2510 sys_sctp_generic_sendmsg_iov(td, uap)
2512 struct sctp_generic_sendmsg_iov_args /* {
2518 struct sctp_sndrcvinfo *sinfo,
2522 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2523 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2525 struct file *fp = NULL;
2528 struct sockaddr *to = NULL;
2530 struct uio *ktruio = NULL;
2533 struct iovec *iov, *tiov;
2534 cap_rights_t rights;
2537 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2544 error = getsockaddr(&to, uap->to, uap->tolen);
2549 rights |= CAP_CONNECT;
2552 AUDIT_ARG_FD(uap->sd);
2553 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2557 #ifdef COMPAT_FREEBSD32
2558 if (SV_CURPROC_FLAG(SV_ILP32))
2559 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2560 uap->iovlen, &iov, EMSGSIZE);
2563 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2567 if (to && (KTRPOINT(td, KTR_STRUCT)))
2571 so = (struct socket *)fp->f_data;
2572 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2577 error = mac_socket_check_send(td->td_ucred, so);
2583 auio.uio_iovcnt = uap->iovlen;
2584 auio.uio_segflg = UIO_USERSPACE;
2585 auio.uio_rw = UIO_WRITE;
2587 auio.uio_offset = 0; /* XXX */
2590 for (i = 0; i <uap->iovlen; i++, tiov++) {
2591 if ((auio.uio_resid += tiov->iov_len) < 0) {
2596 len = auio.uio_resid;
2597 CURVNET_SET(so->so_vnet);
2598 error = sctp_lower_sosend(so, to, &auio,
2599 (struct mbuf *)NULL, (struct mbuf *)NULL,
2600 uap->flags, u_sinfo, td);
2603 if (auio.uio_resid != len && (error == ERESTART ||
2604 error == EINTR || error == EWOULDBLOCK))
2606 /* Generation of SIGPIPE can be controlled per socket */
2607 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2608 !(uap->flags & MSG_NOSIGNAL)) {
2609 PROC_LOCK(td->td_proc);
2610 tdsignal(td, SIGPIPE);
2611 PROC_UNLOCK(td->td_proc);
2615 td->td_retval[0] = len - auio.uio_resid;
2617 if (ktruio != NULL) {
2618 ktruio->uio_resid = td->td_retval[0];
2619 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2632 return (EOPNOTSUPP);
2637 sys_sctp_generic_recvmsg(td, uap)
2639 struct sctp_generic_recvmsg_args /* {
2643 struct sockaddr *from,
2644 __socklen_t *fromlenaddr,
2645 struct sctp_sndrcvinfo *sinfo,
2649 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2650 uint8_t sockbufstore[256];
2652 struct iovec *iov, *tiov;
2653 struct sctp_sndrcvinfo sinfo;
2655 struct file *fp = NULL;
2656 struct sockaddr *fromsa;
2662 struct uio *ktruio = NULL;
2665 AUDIT_ARG_FD(uap->sd);
2666 error = getsock_cap(td->td_proc->p_fd, uap->sd, CAP_READ, &fp, NULL);
2670 #ifdef COMPAT_FREEBSD32
2671 if (SV_CURPROC_FLAG(SV_ILP32))
2672 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2673 uap->iovlen, &iov, EMSGSIZE);
2676 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2681 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2686 error = mac_socket_check_receive(td->td_ucred, so);
2692 if (uap->fromlenaddr) {
2693 error = copyin(uap->fromlenaddr,
2694 &fromlen, sizeof (fromlen));
2701 if (uap->msg_flags) {
2702 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2710 auio.uio_iovcnt = uap->iovlen;
2711 auio.uio_segflg = UIO_USERSPACE;
2712 auio.uio_rw = UIO_READ;
2714 auio.uio_offset = 0; /* XXX */
2717 for (i = 0; i <uap->iovlen; i++, tiov++) {
2718 if ((auio.uio_resid += tiov->iov_len) < 0) {
2723 len = auio.uio_resid;
2724 fromsa = (struct sockaddr *)sockbufstore;
2727 if (KTRPOINT(td, KTR_GENIO))
2728 ktruio = cloneuio(&auio);
2730 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
2731 CURVNET_SET(so->so_vnet);
2732 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2733 fromsa, fromlen, &msg_flags,
2734 (struct sctp_sndrcvinfo *)&sinfo, 1);
2737 if (auio.uio_resid != len && (error == ERESTART ||
2738 error == EINTR || error == EWOULDBLOCK))
2742 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2745 if (ktruio != NULL) {
2746 ktruio->uio_resid = len - auio.uio_resid;
2747 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2752 td->td_retval[0] = len - auio.uio_resid;
2754 if (fromlen && uap->from) {
2756 if (len <= 0 || fromsa == 0)
2759 len = MIN(len, fromsa->sa_len);
2760 error = copyout(fromsa, uap->from, (size_t)len);
2764 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2770 if (KTRPOINT(td, KTR_STRUCT))
2771 ktrsockaddr(fromsa);
2773 if (uap->msg_flags) {
2774 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2787 return (EOPNOTSUPP);