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_param.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_pageout.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_extern.h>
92 #if defined(INET) || defined(INET6)
94 #include <netinet/sctp.h>
95 #include <netinet/sctp_peeloff.h>
97 #endif /* INET || INET6 */
99 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
100 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
102 static int accept1(struct thread *td, struct accept_args *uap, int compat);
103 static int do_sendfile(struct thread *td, struct sendfile_args *uap, int compat);
104 static int getsockname1(struct thread *td, struct getsockname_args *uap,
106 static int getpeername1(struct thread *td, struct getpeername_args *uap,
110 * NSFBUFS-related variables and associated sysctls
116 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
117 "Maximum number of sendfile(2) sf_bufs available");
118 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
119 "Number of sendfile(2) sf_bufs at peak usage");
120 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
121 "Number of sendfile(2) sf_bufs in use");
124 * Convert a user file descriptor to a kernel file entry and check that, if
125 * it is a capability, the right rights are present. A reference on the file
126 * entry is held upon returning.
129 getsock_cap(struct filedesc *fdp, int fd, cap_rights_t rights,
130 struct file **fpp, u_int *fflagp)
134 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 /* {
186 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
188 error = mac_socket_check_create(td->td_ucred, uap->domain, uap->type,
193 error = falloc(td, &fp, &fd, 0);
196 /* An extra reference on `fp' has been held for us by falloc(). */
197 error = socreate(uap->domain, &so, uap->type, uap->protocol,
200 fdclose(td->td_proc->p_fd, fp, fd, td);
202 finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &socketops);
203 td->td_retval[0] = fd;
213 struct bind_args /* {
222 if ((error = getsockaddr(&sa, uap->name, uap->namelen)) != 0)
225 error = kern_bind(td, uap->s, sa);
231 kern_bind(td, fd, sa)
241 error = getsock_cap(td->td_proc->p_fd, fd, CAP_BIND, &fp, NULL);
246 if (KTRPOINT(td, KTR_STRUCT))
250 error = mac_socket_check_bind(td->td_ucred, so, sa);
253 error = sobind(so, sa, td);
262 struct listen_args /* {
271 AUDIT_ARG_FD(uap->s);
272 error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_LISTEN, &fp, NULL);
276 error = mac_socket_check_listen(td->td_ucred, so);
279 error = solisten(so, uap->backlog, td);
289 accept1(td, uap, compat)
291 struct accept_args /* {
293 struct sockaddr * __restrict name;
294 socklen_t * __restrict anamelen;
298 struct sockaddr *name;
303 if (uap->name == NULL)
304 return (kern_accept(td, uap->s, NULL, NULL, NULL));
306 error = copyin(uap->anamelen, &namelen, sizeof (namelen));
310 error = kern_accept(td, uap->s, &name, &namelen, &fp);
313 * return a namelen of zero for older code which might
314 * ignore the return value from accept.
317 (void) copyout(&namelen,
318 uap->anamelen, sizeof(*uap->anamelen));
322 if (error == 0 && name != NULL) {
323 #ifdef COMPAT_OLDSOCK
325 ((struct osockaddr *)name)->sa_family =
328 error = copyout(name, uap->name, namelen);
331 error = copyout(&namelen, uap->anamelen,
334 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
336 free(name, M_SONAME);
341 kern_accept(struct thread *td, int s, struct sockaddr **name,
342 socklen_t *namelen, struct file **fp)
344 struct filedesc *fdp;
345 struct file *headfp, *nfp = NULL;
346 struct sockaddr *sa = NULL;
348 struct socket *head, *so;
361 fdp = td->td_proc->p_fd;
362 error = getsock_cap(fdp, s, CAP_ACCEPT, &headfp, &fflag);
365 head = headfp->f_data;
366 if ((head->so_options & SO_ACCEPTCONN) == 0) {
371 error = mac_socket_check_accept(td->td_ucred, head);
375 error = falloc(td, &nfp, &fd, 0);
379 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
384 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
385 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
386 head->so_error = ECONNABORTED;
389 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
396 if (head->so_error) {
397 error = head->so_error;
402 so = TAILQ_FIRST(&head->so_comp);
403 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
404 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
407 * Before changing the flags on the socket, we have to bump the
408 * reference count. Otherwise, if the protocol calls sofree(),
409 * the socket will be released due to a zero refcount.
411 SOCK_LOCK(so); /* soref() and so_state update */
412 soref(so); /* file descriptor reference */
414 TAILQ_REMOVE(&head->so_comp, so, so_list);
416 so->so_state |= (head->so_state & SS_NBIO);
417 so->so_qstate &= ~SQ_COMP;
423 /* An extra reference on `nfp' has been held for us by falloc(). */
424 td->td_retval[0] = fd;
426 /* connection has been removed from the listen queue */
427 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
429 pgid = fgetown(&head->so_sigio);
431 fsetown(pgid, &so->so_sigio);
433 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
434 /* Sync socket nonblocking/async state with file flags */
435 tmp = fflag & FNONBLOCK;
436 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
437 tmp = fflag & FASYNC;
438 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
440 error = soaccept(so, &sa);
443 * return a namelen of zero for older code which might
444 * ignore the return value from accept.
456 /* check sa_len before it is destroyed */
457 if (*namelen > sa->sa_len)
458 *namelen = sa->sa_len;
460 if (KTRPOINT(td, KTR_STRUCT))
471 * close the new descriptor, assuming someone hasn't ripped it
475 fdclose(fdp, nfp, fd, td);
478 * Release explicitly held references before returning. We return
479 * a reference on nfp to the caller on success if they request it.
498 struct accept_args *uap;
501 return (accept1(td, uap, 0));
504 #ifdef COMPAT_OLDSOCK
508 struct accept_args *uap;
511 return (accept1(td, uap, 1));
513 #endif /* COMPAT_OLDSOCK */
519 struct connect_args /* {
528 error = getsockaddr(&sa, uap->name, uap->namelen);
532 error = kern_connect(td, uap->s, sa);
539 kern_connect(td, fd, sa)
550 error = getsock_cap(td->td_proc->p_fd, fd, CAP_CONNECT, &fp, NULL);
554 if (so->so_state & SS_ISCONNECTING) {
559 if (KTRPOINT(td, KTR_STRUCT))
563 error = mac_socket_check_connect(td->td_ucred, so, sa);
567 error = soconnect(so, sa, td);
570 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
575 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
576 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
579 if (error == EINTR || error == ERESTART)
585 error = so->so_error;
591 so->so_state &= ~SS_ISCONNECTING;
592 if (error == ERESTART)
600 kern_socketpair(struct thread *td, int domain, int type, int protocol,
603 struct filedesc *fdp = td->td_proc->p_fd;
604 struct file *fp1, *fp2;
605 struct socket *so1, *so2;
608 AUDIT_ARG_SOCKET(domain, type, protocol);
610 /* We might want to have a separate check for socket pairs. */
611 error = mac_socket_check_create(td->td_ucred, domain, type,
616 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
619 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
622 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
623 error = falloc(td, &fp1, &fd, 0);
627 fp1->f_data = so1; /* so1 already has ref count */
628 error = falloc(td, &fp2, &fd, 0);
631 fp2->f_data = so2; /* so2 already has ref count */
633 error = soconnect2(so1, so2);
636 if (type == SOCK_DGRAM) {
638 * Datagram socket connection is asymmetric.
640 error = soconnect2(so2, so1);
644 finit(fp1, FREAD | FWRITE, DTYPE_SOCKET, fp1->f_data, &socketops);
645 finit(fp2, FREAD | FWRITE, DTYPE_SOCKET, fp2->f_data, &socketops);
650 fdclose(fdp, fp2, rsv[1], td);
653 fdclose(fdp, fp1, rsv[0], td);
665 sys_socketpair(struct thread *td, struct socketpair_args *uap)
669 error = kern_socketpair(td, uap->domain, uap->type,
673 error = copyout(sv, uap->rsv, 2 * sizeof(int));
675 (void)kern_close(td, sv[0]);
676 (void)kern_close(td, sv[1]);
682 sendit(td, s, mp, flags)
688 struct mbuf *control;
692 #ifdef CAPABILITY_MODE
693 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
697 if (mp->msg_name != NULL) {
698 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
708 if (mp->msg_control) {
709 if (mp->msg_controllen < sizeof(struct cmsghdr)
710 #ifdef COMPAT_OLDSOCK
711 && mp->msg_flags != MSG_COMPAT
717 error = sockargs(&control, mp->msg_control,
718 mp->msg_controllen, MT_CONTROL);
721 #ifdef COMPAT_OLDSOCK
722 if (mp->msg_flags == MSG_COMPAT) {
725 M_PREPEND(control, sizeof(*cm), M_WAIT);
726 cm = mtod(control, struct cmsghdr *);
727 cm->cmsg_len = control->m_len;
728 cm->cmsg_level = SOL_SOCKET;
729 cm->cmsg_type = SCM_RIGHTS;
736 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
745 kern_sendit(td, s, mp, flags, control, segflg)
750 struct mbuf *control;
761 struct uio *ktruio = NULL;
766 if (mp->msg_name != NULL)
767 rights |= CAP_CONNECT;
768 error = getsock_cap(td->td_proc->p_fd, s, rights, &fp, NULL);
771 so = (struct socket *)fp->f_data;
774 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
775 ktrsockaddr(mp->msg_name);
778 if (mp->msg_name != NULL) {
779 error = mac_socket_check_connect(td->td_ucred, so,
784 error = mac_socket_check_send(td->td_ucred, so);
789 auio.uio_iov = mp->msg_iov;
790 auio.uio_iovcnt = mp->msg_iovlen;
791 auio.uio_segflg = segflg;
792 auio.uio_rw = UIO_WRITE;
794 auio.uio_offset = 0; /* XXX */
797 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
798 if ((auio.uio_resid += iov->iov_len) < 0) {
804 if (KTRPOINT(td, KTR_GENIO))
805 ktruio = cloneuio(&auio);
807 len = auio.uio_resid;
808 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
810 if (auio.uio_resid != len && (error == ERESTART ||
811 error == EINTR || error == EWOULDBLOCK))
813 /* Generation of SIGPIPE can be controlled per socket */
814 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
815 !(flags & MSG_NOSIGNAL)) {
816 PROC_LOCK(td->td_proc);
817 tdsignal(td, SIGPIPE);
818 PROC_UNLOCK(td->td_proc);
822 td->td_retval[0] = len - auio.uio_resid;
824 if (ktruio != NULL) {
825 ktruio->uio_resid = td->td_retval[0];
826 ktrgenio(s, UIO_WRITE, ktruio, error);
837 struct sendto_args /* {
850 msg.msg_name = uap->to;
851 msg.msg_namelen = uap->tolen;
855 #ifdef COMPAT_OLDSOCK
858 aiov.iov_base = uap->buf;
859 aiov.iov_len = uap->len;
860 error = sendit(td, uap->s, &msg, uap->flags);
864 #ifdef COMPAT_OLDSOCK
868 struct osend_args /* {
883 aiov.iov_base = uap->buf;
884 aiov.iov_len = uap->len;
887 error = sendit(td, uap->s, &msg, uap->flags);
894 struct osendmsg_args /* {
904 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
907 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
911 msg.msg_flags = MSG_COMPAT;
912 error = sendit(td, uap->s, &msg, uap->flags);
921 struct sendmsg_args /* {
931 error = copyin(uap->msg, &msg, sizeof (msg));
934 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
938 #ifdef COMPAT_OLDSOCK
941 error = sendit(td, uap->s, &msg, uap->flags);
947 kern_recvit(td, s, mp, fromseg, controlp)
951 enum uio_seg fromseg;
952 struct mbuf **controlp;
959 struct mbuf *m, *control = 0;
963 struct sockaddr *fromsa = 0;
965 struct uio *ktruio = NULL;
968 if (controlp != NULL)
972 error = getsock_cap(td->td_proc->p_fd, s, CAP_READ, &fp, NULL);
978 error = mac_socket_check_receive(td->td_ucred, so);
985 auio.uio_iov = mp->msg_iov;
986 auio.uio_iovcnt = mp->msg_iovlen;
987 auio.uio_segflg = UIO_USERSPACE;
988 auio.uio_rw = UIO_READ;
990 auio.uio_offset = 0; /* XXX */
993 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
994 if ((auio.uio_resid += iov->iov_len) < 0) {
1000 if (KTRPOINT(td, KTR_GENIO))
1001 ktruio = cloneuio(&auio);
1003 len = auio.uio_resid;
1004 error = soreceive(so, &fromsa, &auio, (struct mbuf **)0,
1005 (mp->msg_control || controlp) ? &control : (struct mbuf **)0,
1008 if (auio.uio_resid != len && (error == ERESTART ||
1009 error == EINTR || error == EWOULDBLOCK))
1013 if (ktruio != NULL) {
1014 ktruio->uio_resid = len - auio.uio_resid;
1015 ktrgenio(s, UIO_READ, ktruio, error);
1020 td->td_retval[0] = len - auio.uio_resid;
1022 len = mp->msg_namelen;
1023 if (len <= 0 || fromsa == 0)
1026 /* save sa_len before it is destroyed by MSG_COMPAT */
1027 len = MIN(len, fromsa->sa_len);
1028 #ifdef COMPAT_OLDSOCK
1029 if (mp->msg_flags & MSG_COMPAT)
1030 ((struct osockaddr *)fromsa)->sa_family =
1033 if (fromseg == UIO_USERSPACE) {
1034 error = copyout(fromsa, mp->msg_name,
1039 bcopy(fromsa, mp->msg_name, len);
1041 mp->msg_namelen = len;
1043 if (mp->msg_control && controlp == NULL) {
1044 #ifdef COMPAT_OLDSOCK
1046 * We assume that old recvmsg calls won't receive access
1047 * rights and other control info, esp. as control info
1048 * is always optional and those options didn't exist in 4.3.
1049 * If we receive rights, trim the cmsghdr; anything else
1052 if (control && mp->msg_flags & MSG_COMPAT) {
1053 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1055 mtod(control, struct cmsghdr *)->cmsg_type !=
1057 mp->msg_controllen = 0;
1060 control->m_len -= sizeof (struct cmsghdr);
1061 control->m_data += sizeof (struct cmsghdr);
1064 len = mp->msg_controllen;
1066 mp->msg_controllen = 0;
1067 ctlbuf = mp->msg_control;
1069 while (m && len > 0) {
1070 unsigned int tocopy;
1072 if (len >= m->m_len)
1075 mp->msg_flags |= MSG_CTRUNC;
1079 if ((error = copyout(mtod(m, caddr_t),
1080 ctlbuf, tocopy)) != 0)
1087 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1092 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1093 ktrsockaddr(fromsa);
1096 free(fromsa, M_SONAME);
1098 if (error == 0 && controlp != NULL)
1099 *controlp = control;
1107 recvit(td, s, mp, namelenp)
1115 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1119 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1120 #ifdef COMPAT_OLDSOCK
1121 if (mp->msg_flags & MSG_COMPAT)
1122 error = 0; /* old recvfrom didn't check */
1129 sys_recvfrom(td, uap)
1131 struct recvfrom_args /* {
1136 struct sockaddr * __restrict from;
1137 socklen_t * __restrict fromlenaddr;
1144 if (uap->fromlenaddr) {
1145 error = copyin(uap->fromlenaddr,
1146 &msg.msg_namelen, sizeof (msg.msg_namelen));
1150 msg.msg_namelen = 0;
1152 msg.msg_name = uap->from;
1153 msg.msg_iov = &aiov;
1155 aiov.iov_base = uap->buf;
1156 aiov.iov_len = uap->len;
1157 msg.msg_control = 0;
1158 msg.msg_flags = uap->flags;
1159 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1164 #ifdef COMPAT_OLDSOCK
1168 struct recvfrom_args *uap;
1171 uap->flags |= MSG_COMPAT;
1172 return (sys_recvfrom(td, uap));
1176 #ifdef COMPAT_OLDSOCK
1180 struct orecv_args /* {
1192 msg.msg_namelen = 0;
1193 msg.msg_iov = &aiov;
1195 aiov.iov_base = uap->buf;
1196 aiov.iov_len = uap->len;
1197 msg.msg_control = 0;
1198 msg.msg_flags = uap->flags;
1199 error = recvit(td, uap->s, &msg, NULL);
1204 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1205 * overlays the new one, missing only the flags, and with the (old) access
1206 * rights where the control fields are now.
1211 struct orecvmsg_args /* {
1213 struct omsghdr *msg;
1221 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1224 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1227 msg.msg_flags = uap->flags | MSG_COMPAT;
1229 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1230 if (msg.msg_controllen && error == 0)
1231 error = copyout(&msg.msg_controllen,
1232 &uap->msg->msg_accrightslen, sizeof (int));
1239 sys_recvmsg(td, uap)
1241 struct recvmsg_args /* {
1248 struct iovec *uiov, *iov;
1251 error = copyin(uap->msg, &msg, sizeof (msg));
1254 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1257 msg.msg_flags = uap->flags;
1258 #ifdef COMPAT_OLDSOCK
1259 msg.msg_flags &= ~MSG_COMPAT;
1263 error = recvit(td, uap->s, &msg, NULL);
1266 error = copyout(&msg, uap->msg, sizeof(msg));
1274 sys_shutdown(td, uap)
1276 struct shutdown_args /* {
1285 AUDIT_ARG_FD(uap->s);
1286 error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_SHUTDOWN, &fp,
1290 error = soshutdown(so, uap->how);
1298 sys_setsockopt(td, uap)
1300 struct setsockopt_args /* {
1309 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1310 uap->val, UIO_USERSPACE, uap->valsize));
1314 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1320 enum uio_seg valseg;
1326 struct sockopt sopt;
1328 if (val == NULL && valsize != 0)
1330 if ((int)valsize < 0)
1333 sopt.sopt_dir = SOPT_SET;
1334 sopt.sopt_level = level;
1335 sopt.sopt_name = name;
1336 sopt.sopt_val = val;
1337 sopt.sopt_valsize = valsize;
1343 sopt.sopt_td = NULL;
1346 panic("kern_setsockopt called with bad valseg");
1350 error = getsock_cap(td->td_proc->p_fd, s, CAP_SETSOCKOPT, &fp, NULL);
1353 error = sosetopt(so, &sopt);
1361 sys_getsockopt(td, uap)
1363 struct getsockopt_args /* {
1367 void * __restrict val;
1368 socklen_t * __restrict avalsize;
1375 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1380 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1381 uap->val, UIO_USERSPACE, &valsize);
1384 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1389 * Kernel version of getsockopt.
1390 * optval can be a userland or userspace. optlen is always a kernel pointer.
1393 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1399 enum uio_seg valseg;
1405 struct sockopt sopt;
1409 if ((int)*valsize < 0)
1412 sopt.sopt_dir = SOPT_GET;
1413 sopt.sopt_level = level;
1414 sopt.sopt_name = name;
1415 sopt.sopt_val = val;
1416 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1422 sopt.sopt_td = NULL;
1425 panic("kern_getsockopt called with bad valseg");
1429 error = getsock_cap(td->td_proc->p_fd, s, CAP_GETSOCKOPT, &fp, NULL);
1432 error = sogetopt(so, &sopt);
1433 *valsize = sopt.sopt_valsize;
1440 * getsockname1() - Get socket name.
1444 getsockname1(td, uap, compat)
1446 struct getsockname_args /* {
1448 struct sockaddr * __restrict asa;
1449 socklen_t * __restrict alen;
1453 struct sockaddr *sa;
1457 error = copyin(uap->alen, &len, sizeof(len));
1461 error = kern_getsockname(td, uap->fdes, &sa, &len);
1466 #ifdef COMPAT_OLDSOCK
1468 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1470 error = copyout(sa, uap->asa, (u_int)len);
1474 error = copyout(&len, uap->alen, sizeof(len));
1479 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1491 error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETSOCKNAME, &fp, NULL);
1496 CURVNET_SET(so->so_vnet);
1497 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1504 len = MIN(*alen, (*sa)->sa_len);
1507 if (KTRPOINT(td, KTR_STRUCT))
1513 free(*sa, M_SONAME);
1520 sys_getsockname(td, uap)
1522 struct getsockname_args *uap;
1525 return (getsockname1(td, uap, 0));
1528 #ifdef COMPAT_OLDSOCK
1530 ogetsockname(td, uap)
1532 struct getsockname_args *uap;
1535 return (getsockname1(td, uap, 1));
1537 #endif /* COMPAT_OLDSOCK */
1540 * getpeername1() - Get name of peer for connected socket.
1544 getpeername1(td, uap, compat)
1546 struct getpeername_args /* {
1548 struct sockaddr * __restrict asa;
1549 socklen_t * __restrict alen;
1553 struct sockaddr *sa;
1557 error = copyin(uap->alen, &len, sizeof (len));
1561 error = kern_getpeername(td, uap->fdes, &sa, &len);
1566 #ifdef COMPAT_OLDSOCK
1568 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1570 error = copyout(sa, uap->asa, (u_int)len);
1574 error = copyout(&len, uap->alen, sizeof(len));
1579 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1591 error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETPEERNAME, &fp, NULL);
1595 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1600 CURVNET_SET(so->so_vnet);
1601 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1608 len = MIN(*alen, (*sa)->sa_len);
1611 if (KTRPOINT(td, KTR_STRUCT))
1616 free(*sa, M_SONAME);
1625 sys_getpeername(td, uap)
1627 struct getpeername_args *uap;
1630 return (getpeername1(td, uap, 0));
1633 #ifdef COMPAT_OLDSOCK
1635 ogetpeername(td, uap)
1637 struct ogetpeername_args *uap;
1640 /* XXX uap should have type `getpeername_args *' to begin with. */
1641 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1643 #endif /* COMPAT_OLDSOCK */
1646 sockargs(mp, buf, buflen, type)
1651 struct sockaddr *sa;
1655 if ((u_int)buflen > MLEN) {
1656 #ifdef COMPAT_OLDSOCK
1657 if (type == MT_SONAME && (u_int)buflen <= 112)
1658 buflen = MLEN; /* unix domain compat. hack */
1661 if ((u_int)buflen > MCLBYTES)
1664 m = m_get(M_WAIT, type);
1665 if ((u_int)buflen > MLEN)
1668 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1673 if (type == MT_SONAME) {
1674 sa = mtod(m, struct sockaddr *);
1676 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1677 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1678 sa->sa_family = sa->sa_len;
1680 sa->sa_len = buflen;
1687 getsockaddr(namp, uaddr, len)
1688 struct sockaddr **namp;
1692 struct sockaddr *sa;
1695 if (len > SOCK_MAXADDRLEN)
1696 return (ENAMETOOLONG);
1697 if (len < offsetof(struct sockaddr, sa_data[0]))
1699 sa = malloc(len, M_SONAME, M_WAITOK);
1700 error = copyin(uaddr, sa, len);
1704 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1705 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1706 sa->sa_family = sa->sa_len;
1714 #include <sys/condvar.h>
1716 struct sendfile_sync {
1723 * Detach mapped page and release resources back to the system.
1726 sf_buf_mext(void *addr, void *args)
1729 struct sendfile_sync *sfs;
1731 m = sf_buf_page(args);
1734 vm_page_unwire(m, 0);
1736 * Check for the object going away on us. This can
1737 * happen since we don't hold a reference to it.
1738 * If so, we're responsible for freeing the page.
1740 if (m->wire_count == 0 && m->object == NULL)
1746 mtx_lock(&sfs->mtx);
1747 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1748 if (--sfs->count == 0)
1749 cv_signal(&sfs->cv);
1750 mtx_unlock(&sfs->mtx);
1756 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1757 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1759 * Send a file specified by 'fd' and starting at 'offset' to a socket
1760 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1761 * 0. Optionally add a header and/or trailer to the socket output. If
1762 * specified, write the total number of bytes sent into *sbytes.
1765 sys_sendfile(struct thread *td, struct sendfile_args *uap)
1768 return (do_sendfile(td, uap, 0));
1772 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1774 struct sf_hdtr hdtr;
1775 struct uio *hdr_uio, *trl_uio;
1778 hdr_uio = trl_uio = NULL;
1780 if (uap->hdtr != NULL) {
1781 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1784 if (hdtr.headers != NULL) {
1785 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1789 if (hdtr.trailers != NULL) {
1790 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1797 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1800 free(hdr_uio, M_IOV);
1802 free(trl_uio, M_IOV);
1806 #ifdef COMPAT_FREEBSD4
1808 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1810 struct sendfile_args args;
1814 args.offset = uap->offset;
1815 args.nbytes = uap->nbytes;
1816 args.hdtr = uap->hdtr;
1817 args.sbytes = uap->sbytes;
1818 args.flags = uap->flags;
1820 return (do_sendfile(td, &args, 1));
1822 #endif /* COMPAT_FREEBSD4 */
1825 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1826 struct uio *hdr_uio, struct uio *trl_uio, int compat)
1828 struct file *sock_fp;
1830 struct vm_object *obj = NULL;
1831 struct socket *so = NULL;
1832 struct mbuf *m = NULL;
1835 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1836 int error, hdrlen = 0, mnw = 0;
1838 struct sendfile_sync *sfs = NULL;
1841 * The file descriptor must be a regular file and have a
1842 * backing VM object.
1843 * File offset must be positive. If it goes beyond EOF
1844 * we send only the header/trailer and no payload data.
1846 AUDIT_ARG_FD(uap->fd);
1847 if ((error = fgetvp_read(td, uap->fd, CAP_READ, &vp)) != 0)
1849 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1850 vn_lock(vp, LK_SHARED | LK_RETRY);
1851 if (vp->v_type == VREG) {
1855 * Temporarily increase the backing VM
1856 * object's reference count so that a forced
1857 * reclamation of its vnode does not
1858 * immediately destroy it.
1860 VM_OBJECT_LOCK(obj);
1861 if ((obj->flags & OBJ_DEAD) == 0) {
1862 vm_object_reference_locked(obj);
1863 VM_OBJECT_UNLOCK(obj);
1865 VM_OBJECT_UNLOCK(obj);
1871 VFS_UNLOCK_GIANT(vfslocked);
1876 if (uap->offset < 0) {
1882 * The socket must be a stream socket and connected.
1883 * Remember if it a blocking or non-blocking socket.
1885 if ((error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_WRITE,
1886 &sock_fp, NULL)) != 0)
1888 so = sock_fp->f_data;
1889 if (so->so_type != SOCK_STREAM) {
1893 if ((so->so_state & SS_ISCONNECTED) == 0) {
1898 * Do not wait on memory allocations but return ENOMEM for
1899 * caller to retry later.
1900 * XXX: Experimental.
1902 if (uap->flags & SF_MNOWAIT)
1905 if (uap->flags & SF_SYNC) {
1906 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO);
1907 mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
1908 cv_init(&sfs->cv, "sendfile");
1912 error = mac_socket_check_send(td->td_ucred, so);
1917 /* If headers are specified copy them into mbufs. */
1918 if (hdr_uio != NULL) {
1919 hdr_uio->uio_td = td;
1920 hdr_uio->uio_rw = UIO_WRITE;
1921 if (hdr_uio->uio_resid > 0) {
1923 * In FBSD < 5.0 the nbytes to send also included
1924 * the header. If compat is specified subtract the
1925 * header size from nbytes.
1928 if (uap->nbytes > hdr_uio->uio_resid)
1929 uap->nbytes -= hdr_uio->uio_resid;
1933 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
1936 error = mnw ? EAGAIN : ENOBUFS;
1939 hdrlen = m_length(m, NULL);
1944 * Protect against multiple writers to the socket.
1946 * XXXRW: Historically this has assumed non-interruptibility, so now
1947 * we implement that, but possibly shouldn't.
1949 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
1952 * Loop through the pages of the file, starting with the requested
1953 * offset. Get a file page (do I/O if necessary), map the file page
1954 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1956 * This is done in two loops. The inner loop turns as many pages
1957 * as it can, up to available socket buffer space, without blocking
1958 * into mbufs to have it bulk delivered into the socket send buffer.
1959 * The outer loop checks the state and available space of the socket
1960 * and takes care of the overall progress.
1962 for (off = uap->offset, rem = uap->nbytes; ; ) {
1963 struct mbuf *mtail = NULL;
1969 * Check the socket state for ongoing connection,
1970 * no errors and space in socket buffer.
1971 * If space is low allow for the remainder of the
1972 * file to be processed if it fits the socket buffer.
1973 * Otherwise block in waiting for sufficient space
1974 * to proceed, or if the socket is nonblocking, return
1975 * to userland with EAGAIN while reporting how far
1977 * We wait until the socket buffer has significant free
1978 * space to do bulk sends. This makes good use of file
1979 * system read ahead and allows packet segmentation
1980 * offloading hardware to take over lots of work. If
1981 * we were not careful here we would send off only one
1984 SOCKBUF_LOCK(&so->so_snd);
1985 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
1986 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
1988 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1990 SOCKBUF_UNLOCK(&so->so_snd);
1992 } else if (so->so_error) {
1993 error = so->so_error;
1995 SOCKBUF_UNLOCK(&so->so_snd);
1998 space = sbspace(&so->so_snd);
2001 space < so->so_snd.sb_lowat)) {
2002 if (so->so_state & SS_NBIO) {
2003 SOCKBUF_UNLOCK(&so->so_snd);
2008 * sbwait drops the lock while sleeping.
2009 * When we loop back to retry_space the
2010 * state may have changed and we retest
2013 error = sbwait(&so->so_snd);
2015 * An error from sbwait usually indicates that we've
2016 * been interrupted by a signal. If we've sent anything
2017 * then return bytes sent, otherwise return the error.
2020 SOCKBUF_UNLOCK(&so->so_snd);
2025 SOCKBUF_UNLOCK(&so->so_snd);
2028 * Reduce space in the socket buffer by the size of
2029 * the header mbuf chain.
2030 * hdrlen is set to 0 after the first loop.
2035 * Loop and construct maximum sized mbuf chain to be bulk
2036 * dumped into socket buffer.
2038 while (space > loopbytes) {
2043 VM_OBJECT_LOCK(obj);
2045 * Calculate the amount to transfer.
2046 * Not to exceed a page, the EOF,
2047 * or the passed in nbytes.
2049 pgoff = (vm_offset_t)(off & PAGE_MASK);
2050 xfsize = omin(PAGE_SIZE - pgoff,
2051 obj->un_pager.vnp.vnp_size - uap->offset -
2052 fsbytes - loopbytes);
2054 rem = (uap->nbytes - fsbytes - loopbytes);
2056 rem = obj->un_pager.vnp.vnp_size -
2057 uap->offset - fsbytes - loopbytes;
2058 xfsize = omin(rem, xfsize);
2059 xfsize = omin(space - loopbytes, xfsize);
2061 VM_OBJECT_UNLOCK(obj);
2062 done = 1; /* all data sent */
2067 * Attempt to look up the page. Allocate
2068 * if not found or wait and loop if busy.
2070 pindex = OFF_TO_IDX(off);
2071 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2072 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY);
2075 * Check if page is valid for what we need,
2076 * otherwise initiate I/O.
2077 * If we already turned some pages into mbufs,
2078 * send them off before we come here again and
2081 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2082 VM_OBJECT_UNLOCK(obj);
2084 error = EAGAIN; /* send what we already got */
2085 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. */
2186 m_last(m)->m_next = m0;
2191 /* Keep track of bits processed. */
2192 loopbytes += xfsize;
2196 mtx_lock(&sfs->mtx);
2198 mtx_unlock(&sfs->mtx);
2202 /* Add the buffer chain to the socket buffer. */
2206 mlen = m_length(m, NULL);
2207 SOCKBUF_LOCK(&so->so_snd);
2208 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2210 SOCKBUF_UNLOCK(&so->so_snd);
2213 SOCKBUF_UNLOCK(&so->so_snd);
2214 CURVNET_SET(so->so_vnet);
2215 /* Avoid error aliasing. */
2216 err = (*so->so_proto->pr_usrreqs->pru_send)
2217 (so, 0, m, NULL, NULL, td);
2221 * We need two counters to get the
2222 * file offset and nbytes to send
2224 * - sbytes contains the total amount
2225 * of bytes sent, including headers.
2226 * - fsbytes contains the total amount
2227 * of bytes sent from the file.
2235 } else if (error == 0)
2237 m = NULL; /* pru_send always consumes */
2240 /* Quit outer loop on error or when we're done. */
2248 * Send trailers. Wimp out and use writev(2).
2250 if (trl_uio != NULL) {
2251 sbunlock(&so->so_snd);
2252 error = kern_writev(td, uap->s, trl_uio);
2254 sbytes += td->td_retval[0];
2259 sbunlock(&so->so_snd);
2262 * If there was no error we have to clear td->td_retval[0]
2263 * because it may have been set by writev.
2266 td->td_retval[0] = 0;
2268 if (uap->sbytes != NULL) {
2269 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2272 vm_object_deallocate(obj);
2274 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2276 VFS_UNLOCK_GIANT(vfslocked);
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);