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;
137 if (fdp == NULL || (fp = fget_unlocked(fdp, fd)) == NULL)
141 * If the file descriptor is for a capability, test rights and use
142 * the file descriptor referenced by the capability.
144 error = cap_funwrap(fp, rights, &fp_fromcap);
146 fdrop(fp, curthread);
149 if (fp != fp_fromcap) {
151 fdrop(fp, curthread);
154 #endif /* CAPABILITIES */
155 if (fp->f_type != DTYPE_SOCKET) {
156 fdrop(fp, curthread);
160 *fflagp = fp->f_flag;
166 * System call interface to the socket abstraction.
168 #if defined(COMPAT_43)
169 #define COMPAT_OLDSOCK
175 struct socket_args /* {
185 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
187 error = mac_socket_check_create(td->td_ucred, uap->domain, uap->type,
192 error = falloc(td, &fp, &fd, 0);
195 /* An extra reference on `fp' has been held for us by falloc(). */
196 error = socreate(uap->domain, &so, uap->type, uap->protocol,
199 fdclose(td->td_proc->p_fd, fp, fd, td);
201 finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &socketops);
202 td->td_retval[0] = fd;
212 struct bind_args /* {
221 if ((error = getsockaddr(&sa, uap->name, uap->namelen)) != 0)
224 error = kern_bind(td, uap->s, sa);
230 kern_bind(td, fd, sa)
240 error = getsock_cap(td->td_proc->p_fd, fd, CAP_BIND, &fp, NULL);
245 if (KTRPOINT(td, KTR_STRUCT))
249 error = mac_socket_check_bind(td->td_ucred, so, sa);
252 error = sobind(so, sa, td);
261 struct listen_args /* {
270 AUDIT_ARG_FD(uap->s);
271 error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_LISTEN, &fp, NULL);
275 error = mac_socket_check_listen(td->td_ucred, so);
278 error = solisten(so, uap->backlog, td);
288 accept1(td, uap, compat)
290 struct accept_args /* {
292 struct sockaddr * __restrict name;
293 socklen_t * __restrict anamelen;
297 struct sockaddr *name;
302 if (uap->name == NULL)
303 return (kern_accept(td, uap->s, NULL, NULL, NULL));
305 error = copyin(uap->anamelen, &namelen, sizeof (namelen));
309 error = kern_accept(td, uap->s, &name, &namelen, &fp);
312 * return a namelen of zero for older code which might
313 * ignore the return value from accept.
316 (void) copyout(&namelen,
317 uap->anamelen, sizeof(*uap->anamelen));
321 if (error == 0 && name != NULL) {
322 #ifdef COMPAT_OLDSOCK
324 ((struct osockaddr *)name)->sa_family =
327 error = copyout(name, uap->name, namelen);
330 error = copyout(&namelen, uap->anamelen,
333 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
335 free(name, M_SONAME);
340 kern_accept(struct thread *td, int s, struct sockaddr **name,
341 socklen_t *namelen, struct file **fp)
343 struct filedesc *fdp;
344 struct file *headfp, *nfp = NULL;
345 struct sockaddr *sa = NULL;
347 struct socket *head, *so;
360 fdp = td->td_proc->p_fd;
361 error = getsock_cap(fdp, s, CAP_ACCEPT, &headfp, &fflag);
364 head = headfp->f_data;
365 if ((head->so_options & SO_ACCEPTCONN) == 0) {
370 error = mac_socket_check_accept(td->td_ucred, head);
374 error = falloc(td, &nfp, &fd, 0);
378 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
383 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
384 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
385 head->so_error = ECONNABORTED;
388 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
395 if (head->so_error) {
396 error = head->so_error;
401 so = TAILQ_FIRST(&head->so_comp);
402 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
403 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
406 * Before changing the flags on the socket, we have to bump the
407 * reference count. Otherwise, if the protocol calls sofree(),
408 * the socket will be released due to a zero refcount.
410 SOCK_LOCK(so); /* soref() and so_state update */
411 soref(so); /* file descriptor reference */
413 TAILQ_REMOVE(&head->so_comp, so, so_list);
415 so->so_state |= (head->so_state & SS_NBIO);
416 so->so_qstate &= ~SQ_COMP;
422 /* An extra reference on `nfp' has been held for us by falloc(). */
423 td->td_retval[0] = fd;
425 /* connection has been removed from the listen queue */
426 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
428 pgid = fgetown(&head->so_sigio);
430 fsetown(pgid, &so->so_sigio);
432 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
433 /* Sync socket nonblocking/async state with file flags */
434 tmp = fflag & FNONBLOCK;
435 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
436 tmp = fflag & FASYNC;
437 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
439 error = soaccept(so, &sa);
442 * return a namelen of zero for older code which might
443 * ignore the return value from accept.
455 /* check sa_len before it is destroyed */
456 if (*namelen > sa->sa_len)
457 *namelen = sa->sa_len;
459 if (KTRPOINT(td, KTR_STRUCT))
470 * close the new descriptor, assuming someone hasn't ripped it
474 fdclose(fdp, nfp, fd, td);
477 * Release explicitly held references before returning. We return
478 * a reference on nfp to the caller on success if they request it.
497 struct accept_args *uap;
500 return (accept1(td, uap, 0));
503 #ifdef COMPAT_OLDSOCK
507 struct accept_args *uap;
510 return (accept1(td, uap, 1));
512 #endif /* COMPAT_OLDSOCK */
518 struct connect_args /* {
527 error = getsockaddr(&sa, uap->name, uap->namelen);
531 error = kern_connect(td, uap->s, sa);
538 kern_connect(td, fd, sa)
549 error = getsock_cap(td->td_proc->p_fd, fd, CAP_CONNECT, &fp, NULL);
553 if (so->so_state & SS_ISCONNECTING) {
558 if (KTRPOINT(td, KTR_STRUCT))
562 error = mac_socket_check_connect(td->td_ucred, so, sa);
566 error = soconnect(so, sa, td);
569 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
574 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
575 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
578 if (error == EINTR || error == ERESTART)
584 error = so->so_error;
590 so->so_state &= ~SS_ISCONNECTING;
591 if (error == ERESTART)
599 kern_socketpair(struct thread *td, int domain, int type, int protocol,
602 struct filedesc *fdp = td->td_proc->p_fd;
603 struct file *fp1, *fp2;
604 struct socket *so1, *so2;
607 AUDIT_ARG_SOCKET(domain, type, protocol);
609 /* We might want to have a separate check for socket pairs. */
610 error = mac_socket_check_create(td->td_ucred, domain, type,
615 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
618 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
621 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
622 error = falloc(td, &fp1, &fd, 0);
626 fp1->f_data = so1; /* so1 already has ref count */
627 error = falloc(td, &fp2, &fd, 0);
630 fp2->f_data = so2; /* so2 already has ref count */
632 error = soconnect2(so1, so2);
635 if (type == SOCK_DGRAM) {
637 * Datagram socket connection is asymmetric.
639 error = soconnect2(so2, so1);
643 finit(fp1, FREAD | FWRITE, DTYPE_SOCKET, fp1->f_data, &socketops);
644 finit(fp2, FREAD | FWRITE, DTYPE_SOCKET, fp2->f_data, &socketops);
649 fdclose(fdp, fp2, rsv[1], td);
652 fdclose(fdp, fp1, rsv[0], td);
664 sys_socketpair(struct thread *td, struct socketpair_args *uap)
668 error = kern_socketpair(td, uap->domain, uap->type,
672 error = copyout(sv, uap->rsv, 2 * sizeof(int));
674 (void)kern_close(td, sv[0]);
675 (void)kern_close(td, sv[1]);
681 sendit(td, s, mp, flags)
687 struct mbuf *control;
691 #ifdef CAPABILITY_MODE
692 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
696 if (mp->msg_name != NULL) {
697 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
707 if (mp->msg_control) {
708 if (mp->msg_controllen < sizeof(struct cmsghdr)
709 #ifdef COMPAT_OLDSOCK
710 && mp->msg_flags != MSG_COMPAT
716 error = sockargs(&control, mp->msg_control,
717 mp->msg_controllen, MT_CONTROL);
720 #ifdef COMPAT_OLDSOCK
721 if (mp->msg_flags == MSG_COMPAT) {
724 M_PREPEND(control, sizeof(*cm), M_WAIT);
725 cm = mtod(control, struct cmsghdr *);
726 cm->cmsg_len = control->m_len;
727 cm->cmsg_level = SOL_SOCKET;
728 cm->cmsg_type = SCM_RIGHTS;
735 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
744 kern_sendit(td, s, mp, flags, control, segflg)
749 struct mbuf *control;
760 struct uio *ktruio = NULL;
765 if (mp->msg_name != NULL)
766 rights |= CAP_CONNECT;
767 error = getsock_cap(td->td_proc->p_fd, s, rights, &fp, NULL);
770 so = (struct socket *)fp->f_data;
773 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
774 ktrsockaddr(mp->msg_name);
777 if (mp->msg_name != NULL) {
778 error = mac_socket_check_connect(td->td_ucred, so,
783 error = mac_socket_check_send(td->td_ucred, so);
788 auio.uio_iov = mp->msg_iov;
789 auio.uio_iovcnt = mp->msg_iovlen;
790 auio.uio_segflg = segflg;
791 auio.uio_rw = UIO_WRITE;
793 auio.uio_offset = 0; /* XXX */
796 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
797 if ((auio.uio_resid += iov->iov_len) < 0) {
803 if (KTRPOINT(td, KTR_GENIO))
804 ktruio = cloneuio(&auio);
806 len = auio.uio_resid;
807 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
809 if (auio.uio_resid != len && (error == ERESTART ||
810 error == EINTR || error == EWOULDBLOCK))
812 /* Generation of SIGPIPE can be controlled per socket */
813 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
814 !(flags & MSG_NOSIGNAL)) {
815 PROC_LOCK(td->td_proc);
816 tdsignal(td, SIGPIPE);
817 PROC_UNLOCK(td->td_proc);
821 td->td_retval[0] = len - auio.uio_resid;
823 if (ktruio != NULL) {
824 ktruio->uio_resid = td->td_retval[0];
825 ktrgenio(s, UIO_WRITE, ktruio, error);
836 struct sendto_args /* {
849 msg.msg_name = uap->to;
850 msg.msg_namelen = uap->tolen;
854 #ifdef COMPAT_OLDSOCK
857 aiov.iov_base = uap->buf;
858 aiov.iov_len = uap->len;
859 error = sendit(td, uap->s, &msg, uap->flags);
863 #ifdef COMPAT_OLDSOCK
867 struct osend_args /* {
882 aiov.iov_base = uap->buf;
883 aiov.iov_len = uap->len;
886 error = sendit(td, uap->s, &msg, uap->flags);
893 struct osendmsg_args /* {
903 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
906 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
910 msg.msg_flags = MSG_COMPAT;
911 error = sendit(td, uap->s, &msg, uap->flags);
920 struct sendmsg_args /* {
930 error = copyin(uap->msg, &msg, sizeof (msg));
933 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
937 #ifdef COMPAT_OLDSOCK
940 error = sendit(td, uap->s, &msg, uap->flags);
946 kern_recvit(td, s, mp, fromseg, controlp)
950 enum uio_seg fromseg;
951 struct mbuf **controlp;
958 struct mbuf *m, *control = 0;
962 struct sockaddr *fromsa = 0;
964 struct uio *ktruio = NULL;
967 if (controlp != NULL)
971 error = getsock_cap(td->td_proc->p_fd, s, CAP_READ, &fp, NULL);
977 error = mac_socket_check_receive(td->td_ucred, so);
984 auio.uio_iov = mp->msg_iov;
985 auio.uio_iovcnt = mp->msg_iovlen;
986 auio.uio_segflg = UIO_USERSPACE;
987 auio.uio_rw = UIO_READ;
989 auio.uio_offset = 0; /* XXX */
992 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
993 if ((auio.uio_resid += iov->iov_len) < 0) {
999 if (KTRPOINT(td, KTR_GENIO))
1000 ktruio = cloneuio(&auio);
1002 len = auio.uio_resid;
1003 error = soreceive(so, &fromsa, &auio, (struct mbuf **)0,
1004 (mp->msg_control || controlp) ? &control : (struct mbuf **)0,
1007 if (auio.uio_resid != len && (error == ERESTART ||
1008 error == EINTR || error == EWOULDBLOCK))
1012 if (ktruio != NULL) {
1013 ktruio->uio_resid = len - auio.uio_resid;
1014 ktrgenio(s, UIO_READ, ktruio, error);
1019 td->td_retval[0] = len - auio.uio_resid;
1021 len = mp->msg_namelen;
1022 if (len <= 0 || fromsa == 0)
1025 /* save sa_len before it is destroyed by MSG_COMPAT */
1026 len = MIN(len, fromsa->sa_len);
1027 #ifdef COMPAT_OLDSOCK
1028 if (mp->msg_flags & MSG_COMPAT)
1029 ((struct osockaddr *)fromsa)->sa_family =
1032 if (fromseg == UIO_USERSPACE) {
1033 error = copyout(fromsa, mp->msg_name,
1038 bcopy(fromsa, mp->msg_name, len);
1040 mp->msg_namelen = len;
1042 if (mp->msg_control && controlp == NULL) {
1043 #ifdef COMPAT_OLDSOCK
1045 * We assume that old recvmsg calls won't receive access
1046 * rights and other control info, esp. as control info
1047 * is always optional and those options didn't exist in 4.3.
1048 * If we receive rights, trim the cmsghdr; anything else
1051 if (control && mp->msg_flags & MSG_COMPAT) {
1052 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1054 mtod(control, struct cmsghdr *)->cmsg_type !=
1056 mp->msg_controllen = 0;
1059 control->m_len -= sizeof (struct cmsghdr);
1060 control->m_data += sizeof (struct cmsghdr);
1063 len = mp->msg_controllen;
1065 mp->msg_controllen = 0;
1066 ctlbuf = mp->msg_control;
1068 while (m && len > 0) {
1069 unsigned int tocopy;
1071 if (len >= m->m_len)
1074 mp->msg_flags |= MSG_CTRUNC;
1078 if ((error = copyout(mtod(m, caddr_t),
1079 ctlbuf, tocopy)) != 0)
1086 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1091 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1092 ktrsockaddr(fromsa);
1095 free(fromsa, M_SONAME);
1097 if (error == 0 && controlp != NULL)
1098 *controlp = control;
1106 recvit(td, s, mp, namelenp)
1114 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1118 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1119 #ifdef COMPAT_OLDSOCK
1120 if (mp->msg_flags & MSG_COMPAT)
1121 error = 0; /* old recvfrom didn't check */
1128 sys_recvfrom(td, uap)
1130 struct recvfrom_args /* {
1135 struct sockaddr * __restrict from;
1136 socklen_t * __restrict fromlenaddr;
1143 if (uap->fromlenaddr) {
1144 error = copyin(uap->fromlenaddr,
1145 &msg.msg_namelen, sizeof (msg.msg_namelen));
1149 msg.msg_namelen = 0;
1151 msg.msg_name = uap->from;
1152 msg.msg_iov = &aiov;
1154 aiov.iov_base = uap->buf;
1155 aiov.iov_len = uap->len;
1156 msg.msg_control = 0;
1157 msg.msg_flags = uap->flags;
1158 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1163 #ifdef COMPAT_OLDSOCK
1167 struct recvfrom_args *uap;
1170 uap->flags |= MSG_COMPAT;
1171 return (sys_recvfrom(td, uap));
1175 #ifdef COMPAT_OLDSOCK
1179 struct orecv_args /* {
1191 msg.msg_namelen = 0;
1192 msg.msg_iov = &aiov;
1194 aiov.iov_base = uap->buf;
1195 aiov.iov_len = uap->len;
1196 msg.msg_control = 0;
1197 msg.msg_flags = uap->flags;
1198 error = recvit(td, uap->s, &msg, NULL);
1203 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1204 * overlays the new one, missing only the flags, and with the (old) access
1205 * rights where the control fields are now.
1210 struct orecvmsg_args /* {
1212 struct omsghdr *msg;
1220 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1223 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1226 msg.msg_flags = uap->flags | MSG_COMPAT;
1228 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1229 if (msg.msg_controllen && error == 0)
1230 error = copyout(&msg.msg_controllen,
1231 &uap->msg->msg_accrightslen, sizeof (int));
1238 sys_recvmsg(td, uap)
1240 struct recvmsg_args /* {
1247 struct iovec *uiov, *iov;
1250 error = copyin(uap->msg, &msg, sizeof (msg));
1253 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1256 msg.msg_flags = uap->flags;
1257 #ifdef COMPAT_OLDSOCK
1258 msg.msg_flags &= ~MSG_COMPAT;
1262 error = recvit(td, uap->s, &msg, NULL);
1265 error = copyout(&msg, uap->msg, sizeof(msg));
1273 sys_shutdown(td, uap)
1275 struct shutdown_args /* {
1284 AUDIT_ARG_FD(uap->s);
1285 error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_SHUTDOWN, &fp,
1289 error = soshutdown(so, uap->how);
1297 sys_setsockopt(td, uap)
1299 struct setsockopt_args /* {
1308 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1309 uap->val, UIO_USERSPACE, uap->valsize));
1313 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1319 enum uio_seg valseg;
1325 struct sockopt sopt;
1327 if (val == NULL && valsize != 0)
1329 if ((int)valsize < 0)
1332 sopt.sopt_dir = SOPT_SET;
1333 sopt.sopt_level = level;
1334 sopt.sopt_name = name;
1335 sopt.sopt_val = val;
1336 sopt.sopt_valsize = valsize;
1342 sopt.sopt_td = NULL;
1345 panic("kern_setsockopt called with bad valseg");
1349 error = getsock_cap(td->td_proc->p_fd, s, CAP_SETSOCKOPT, &fp, NULL);
1352 error = sosetopt(so, &sopt);
1360 sys_getsockopt(td, uap)
1362 struct getsockopt_args /* {
1366 void * __restrict val;
1367 socklen_t * __restrict avalsize;
1374 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1379 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1380 uap->val, UIO_USERSPACE, &valsize);
1383 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1388 * Kernel version of getsockopt.
1389 * optval can be a userland or userspace. optlen is always a kernel pointer.
1392 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1398 enum uio_seg valseg;
1404 struct sockopt sopt;
1408 if ((int)*valsize < 0)
1411 sopt.sopt_dir = SOPT_GET;
1412 sopt.sopt_level = level;
1413 sopt.sopt_name = name;
1414 sopt.sopt_val = val;
1415 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1421 sopt.sopt_td = NULL;
1424 panic("kern_getsockopt called with bad valseg");
1428 error = getsock_cap(td->td_proc->p_fd, s, CAP_GETSOCKOPT, &fp, NULL);
1431 error = sogetopt(so, &sopt);
1432 *valsize = sopt.sopt_valsize;
1439 * getsockname1() - Get socket name.
1443 getsockname1(td, uap, compat)
1445 struct getsockname_args /* {
1447 struct sockaddr * __restrict asa;
1448 socklen_t * __restrict alen;
1452 struct sockaddr *sa;
1456 error = copyin(uap->alen, &len, sizeof(len));
1460 error = kern_getsockname(td, uap->fdes, &sa, &len);
1465 #ifdef COMPAT_OLDSOCK
1467 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1469 error = copyout(sa, uap->asa, (u_int)len);
1473 error = copyout(&len, uap->alen, sizeof(len));
1478 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1490 error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETSOCKNAME, &fp, NULL);
1495 CURVNET_SET(so->so_vnet);
1496 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1503 len = MIN(*alen, (*sa)->sa_len);
1506 if (KTRPOINT(td, KTR_STRUCT))
1512 free(*sa, M_SONAME);
1519 sys_getsockname(td, uap)
1521 struct getsockname_args *uap;
1524 return (getsockname1(td, uap, 0));
1527 #ifdef COMPAT_OLDSOCK
1529 ogetsockname(td, uap)
1531 struct getsockname_args *uap;
1534 return (getsockname1(td, uap, 1));
1536 #endif /* COMPAT_OLDSOCK */
1539 * getpeername1() - Get name of peer for connected socket.
1543 getpeername1(td, uap, compat)
1545 struct getpeername_args /* {
1547 struct sockaddr * __restrict asa;
1548 socklen_t * __restrict alen;
1552 struct sockaddr *sa;
1556 error = copyin(uap->alen, &len, sizeof (len));
1560 error = kern_getpeername(td, uap->fdes, &sa, &len);
1565 #ifdef COMPAT_OLDSOCK
1567 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1569 error = copyout(sa, uap->asa, (u_int)len);
1573 error = copyout(&len, uap->alen, sizeof(len));
1578 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1590 error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETPEERNAME, &fp, NULL);
1594 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1599 CURVNET_SET(so->so_vnet);
1600 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1607 len = MIN(*alen, (*sa)->sa_len);
1610 if (KTRPOINT(td, KTR_STRUCT))
1615 free(*sa, M_SONAME);
1624 sys_getpeername(td, uap)
1626 struct getpeername_args *uap;
1629 return (getpeername1(td, uap, 0));
1632 #ifdef COMPAT_OLDSOCK
1634 ogetpeername(td, uap)
1636 struct ogetpeername_args *uap;
1639 /* XXX uap should have type `getpeername_args *' to begin with. */
1640 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1642 #endif /* COMPAT_OLDSOCK */
1645 sockargs(mp, buf, buflen, type)
1650 struct sockaddr *sa;
1654 if ((u_int)buflen > MLEN) {
1655 #ifdef COMPAT_OLDSOCK
1656 if (type == MT_SONAME && (u_int)buflen <= 112)
1657 buflen = MLEN; /* unix domain compat. hack */
1660 if ((u_int)buflen > MCLBYTES)
1663 m = m_get(M_WAIT, type);
1664 if ((u_int)buflen > MLEN)
1667 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1672 if (type == MT_SONAME) {
1673 sa = mtod(m, struct sockaddr *);
1675 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1676 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1677 sa->sa_family = sa->sa_len;
1679 sa->sa_len = buflen;
1686 getsockaddr(namp, uaddr, len)
1687 struct sockaddr **namp;
1691 struct sockaddr *sa;
1694 if (len > SOCK_MAXADDRLEN)
1695 return (ENAMETOOLONG);
1696 if (len < offsetof(struct sockaddr, sa_data[0]))
1698 sa = malloc(len, M_SONAME, M_WAITOK);
1699 error = copyin(uaddr, sa, len);
1703 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1704 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1705 sa->sa_family = sa->sa_len;
1713 #include <sys/condvar.h>
1715 struct sendfile_sync {
1722 * Detach mapped page and release resources back to the system.
1725 sf_buf_mext(void *addr, void *args)
1728 struct sendfile_sync *sfs;
1730 m = sf_buf_page(args);
1733 vm_page_unwire(m, 0);
1735 * Check for the object going away on us. This can
1736 * happen since we don't hold a reference to it.
1737 * If so, we're responsible for freeing the page.
1739 if (m->wire_count == 0 && m->object == NULL)
1745 mtx_lock(&sfs->mtx);
1746 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1747 if (--sfs->count == 0)
1748 cv_signal(&sfs->cv);
1749 mtx_unlock(&sfs->mtx);
1755 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1756 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1758 * Send a file specified by 'fd' and starting at 'offset' to a socket
1759 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1760 * 0. Optionally add a header and/or trailer to the socket output. If
1761 * specified, write the total number of bytes sent into *sbytes.
1764 sys_sendfile(struct thread *td, struct sendfile_args *uap)
1767 return (do_sendfile(td, uap, 0));
1771 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1773 struct sf_hdtr hdtr;
1774 struct uio *hdr_uio, *trl_uio;
1777 hdr_uio = trl_uio = NULL;
1779 if (uap->hdtr != NULL) {
1780 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1783 if (hdtr.headers != NULL) {
1784 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1788 if (hdtr.trailers != NULL) {
1789 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1796 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1799 free(hdr_uio, M_IOV);
1801 free(trl_uio, M_IOV);
1805 #ifdef COMPAT_FREEBSD4
1807 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1809 struct sendfile_args args;
1813 args.offset = uap->offset;
1814 args.nbytes = uap->nbytes;
1815 args.hdtr = uap->hdtr;
1816 args.sbytes = uap->sbytes;
1817 args.flags = uap->flags;
1819 return (do_sendfile(td, &args, 1));
1821 #endif /* COMPAT_FREEBSD4 */
1824 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1825 struct uio *hdr_uio, struct uio *trl_uio, int compat)
1827 struct file *sock_fp;
1829 struct vm_object *obj = NULL;
1830 struct socket *so = NULL;
1831 struct mbuf *m = NULL;
1834 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1835 int error, hdrlen = 0, mnw = 0;
1837 struct sendfile_sync *sfs = NULL;
1840 * The file descriptor must be a regular file and have a
1841 * backing VM object.
1842 * File offset must be positive. If it goes beyond EOF
1843 * we send only the header/trailer and no payload data.
1845 AUDIT_ARG_FD(uap->fd);
1846 if ((error = fgetvp_read(td, uap->fd, CAP_READ, &vp)) != 0)
1848 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1849 vn_lock(vp, LK_SHARED | LK_RETRY);
1850 if (vp->v_type == VREG) {
1854 * Temporarily increase the backing VM
1855 * object's reference count so that a forced
1856 * reclamation of its vnode does not
1857 * immediately destroy it.
1859 VM_OBJECT_LOCK(obj);
1860 if ((obj->flags & OBJ_DEAD) == 0) {
1861 vm_object_reference_locked(obj);
1862 VM_OBJECT_UNLOCK(obj);
1864 VM_OBJECT_UNLOCK(obj);
1870 VFS_UNLOCK_GIANT(vfslocked);
1875 if (uap->offset < 0) {
1881 * The socket must be a stream socket and connected.
1882 * Remember if it a blocking or non-blocking socket.
1884 if ((error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_WRITE,
1885 &sock_fp, NULL)) != 0)
1887 so = sock_fp->f_data;
1888 if (so->so_type != SOCK_STREAM) {
1892 if ((so->so_state & SS_ISCONNECTED) == 0) {
1897 * Do not wait on memory allocations but return ENOMEM for
1898 * caller to retry later.
1899 * XXX: Experimental.
1901 if (uap->flags & SF_MNOWAIT)
1904 if (uap->flags & SF_SYNC) {
1905 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO);
1906 mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
1907 cv_init(&sfs->cv, "sendfile");
1911 error = mac_socket_check_send(td->td_ucred, so);
1916 /* If headers are specified copy them into mbufs. */
1917 if (hdr_uio != NULL) {
1918 hdr_uio->uio_td = td;
1919 hdr_uio->uio_rw = UIO_WRITE;
1920 if (hdr_uio->uio_resid > 0) {
1922 * In FBSD < 5.0 the nbytes to send also included
1923 * the header. If compat is specified subtract the
1924 * header size from nbytes.
1927 if (uap->nbytes > hdr_uio->uio_resid)
1928 uap->nbytes -= hdr_uio->uio_resid;
1932 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
1935 error = mnw ? EAGAIN : ENOBUFS;
1938 hdrlen = m_length(m, NULL);
1943 * Protect against multiple writers to the socket.
1945 * XXXRW: Historically this has assumed non-interruptibility, so now
1946 * we implement that, but possibly shouldn't.
1948 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
1951 * Loop through the pages of the file, starting with the requested
1952 * offset. Get a file page (do I/O if necessary), map the file page
1953 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1955 * This is done in two loops. The inner loop turns as many pages
1956 * as it can, up to available socket buffer space, without blocking
1957 * into mbufs to have it bulk delivered into the socket send buffer.
1958 * The outer loop checks the state and available space of the socket
1959 * and takes care of the overall progress.
1961 for (off = uap->offset, rem = uap->nbytes; ; ) {
1962 struct mbuf *mtail = NULL;
1968 * Check the socket state for ongoing connection,
1969 * no errors and space in socket buffer.
1970 * If space is low allow for the remainder of the
1971 * file to be processed if it fits the socket buffer.
1972 * Otherwise block in waiting for sufficient space
1973 * to proceed, or if the socket is nonblocking, return
1974 * to userland with EAGAIN while reporting how far
1976 * We wait until the socket buffer has significant free
1977 * space to do bulk sends. This makes good use of file
1978 * system read ahead and allows packet segmentation
1979 * offloading hardware to take over lots of work. If
1980 * we were not careful here we would send off only one
1983 SOCKBUF_LOCK(&so->so_snd);
1984 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
1985 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
1987 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1989 SOCKBUF_UNLOCK(&so->so_snd);
1991 } else if (so->so_error) {
1992 error = so->so_error;
1994 SOCKBUF_UNLOCK(&so->so_snd);
1997 space = sbspace(&so->so_snd);
2000 space < so->so_snd.sb_lowat)) {
2001 if (so->so_state & SS_NBIO) {
2002 SOCKBUF_UNLOCK(&so->so_snd);
2007 * sbwait drops the lock while sleeping.
2008 * When we loop back to retry_space the
2009 * state may have changed and we retest
2012 error = sbwait(&so->so_snd);
2014 * An error from sbwait usually indicates that we've
2015 * been interrupted by a signal. If we've sent anything
2016 * then return bytes sent, otherwise return the error.
2019 SOCKBUF_UNLOCK(&so->so_snd);
2024 SOCKBUF_UNLOCK(&so->so_snd);
2027 * Reduce space in the socket buffer by the size of
2028 * the header mbuf chain.
2029 * hdrlen is set to 0 after the first loop.
2034 * Loop and construct maximum sized mbuf chain to be bulk
2035 * dumped into socket buffer.
2037 while (space > loopbytes) {
2042 VM_OBJECT_LOCK(obj);
2044 * Calculate the amount to transfer.
2045 * Not to exceed a page, the EOF,
2046 * or the passed in nbytes.
2048 pgoff = (vm_offset_t)(off & PAGE_MASK);
2049 xfsize = omin(PAGE_SIZE - pgoff,
2050 obj->un_pager.vnp.vnp_size - uap->offset -
2051 fsbytes - loopbytes);
2053 rem = (uap->nbytes - fsbytes - loopbytes);
2055 rem = obj->un_pager.vnp.vnp_size -
2056 uap->offset - fsbytes - loopbytes;
2057 xfsize = omin(rem, xfsize);
2058 xfsize = omin(space - loopbytes, xfsize);
2060 VM_OBJECT_UNLOCK(obj);
2061 done = 1; /* all data sent */
2066 * Attempt to look up the page. Allocate
2067 * if not found or wait and loop if busy.
2069 pindex = OFF_TO_IDX(off);
2070 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2071 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY);
2074 * Check if page is valid for what we need,
2075 * otherwise initiate I/O.
2076 * If we already turned some pages into mbufs,
2077 * send them off before we come here again and
2080 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2081 VM_OBJECT_UNLOCK(obj);
2083 error = EAGAIN; /* send what we already got */
2084 else if (uap->flags & SF_NODISKIO)
2091 * Ensure that our page is still around
2092 * when the I/O completes.
2094 vm_page_io_start(pg);
2095 VM_OBJECT_UNLOCK(obj);
2098 * Get the page from backing store.
2100 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2101 error = vn_lock(vp, LK_SHARED);
2104 bsize = vp->v_mount->mnt_stat.f_iosize;
2107 * XXXMAC: Because we don't have fp->f_cred
2108 * here, we pass in NOCRED. This is probably
2109 * wrong, but is consistent with our original
2112 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
2113 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2114 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
2115 td->td_ucred, NOCRED, &resid, td);
2118 VFS_UNLOCK_GIANT(vfslocked);
2119 VM_OBJECT_LOCK(obj);
2120 vm_page_io_finish(pg);
2122 VM_OBJECT_UNLOCK(obj);
2127 vm_page_unwire(pg, 0);
2129 * See if anyone else might know about
2130 * this page. If not and it is not valid,
2133 if (pg->wire_count == 0 && pg->valid == 0 &&
2134 pg->busy == 0 && !(pg->oflags & VPO_BUSY))
2137 VM_OBJECT_UNLOCK(obj);
2138 if (error == EAGAIN)
2139 error = 0; /* not a real error */
2144 * Get a sendfile buf. When allocating the
2145 * first buffer for mbuf chain, we usually
2146 * wait as long as necessary, but this wait
2147 * can be interrupted. For consequent
2148 * buffers, do not sleep, since several
2149 * threads might exhaust the buffers and then
2152 sf = sf_buf_alloc(pg, (mnw || m != NULL) ? SFB_NOWAIT :
2155 mbstat.sf_allocfail++;
2157 vm_page_unwire(pg, 0);
2158 KASSERT(pg->object != NULL,
2159 ("kern_sendfile: object disappeared"));
2162 error = (mnw ? EAGAIN : EINTR);
2167 * Get an mbuf and set it up as having
2170 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2172 error = (mnw ? EAGAIN : ENOBUFS);
2173 sf_buf_mext((void *)sf_buf_kva(sf), sf);
2176 MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext,
2177 sfs, sf, M_RDONLY, EXT_SFBUF);
2178 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2181 /* Append to mbuf chain. */
2185 m_last(m)->m_next = m0;
2190 /* Keep track of bits processed. */
2191 loopbytes += xfsize;
2195 mtx_lock(&sfs->mtx);
2197 mtx_unlock(&sfs->mtx);
2201 /* Add the buffer chain to the socket buffer. */
2205 mlen = m_length(m, NULL);
2206 SOCKBUF_LOCK(&so->so_snd);
2207 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2209 SOCKBUF_UNLOCK(&so->so_snd);
2212 SOCKBUF_UNLOCK(&so->so_snd);
2213 CURVNET_SET(so->so_vnet);
2214 /* Avoid error aliasing. */
2215 err = (*so->so_proto->pr_usrreqs->pru_send)
2216 (so, 0, m, NULL, NULL, td);
2220 * We need two counters to get the
2221 * file offset and nbytes to send
2223 * - sbytes contains the total amount
2224 * of bytes sent, including headers.
2225 * - fsbytes contains the total amount
2226 * of bytes sent from the file.
2234 } else if (error == 0)
2236 m = NULL; /* pru_send always consumes */
2239 /* Quit outer loop on error or when we're done. */
2247 * Send trailers. Wimp out and use writev(2).
2249 if (trl_uio != NULL) {
2250 sbunlock(&so->so_snd);
2251 error = kern_writev(td, uap->s, trl_uio);
2253 sbytes += td->td_retval[0];
2258 sbunlock(&so->so_snd);
2261 * If there was no error we have to clear td->td_retval[0]
2262 * because it may have been set by writev.
2265 td->td_retval[0] = 0;
2267 if (uap->sbytes != NULL) {
2268 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2271 vm_object_deallocate(obj);
2273 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2275 VFS_UNLOCK_GIANT(vfslocked);
2283 mtx_lock(&sfs->mtx);
2284 if (sfs->count != 0)
2285 cv_wait(&sfs->cv, &sfs->mtx);
2286 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2287 cv_destroy(&sfs->cv);
2288 mtx_destroy(&sfs->mtx);
2292 if (error == ERESTART)
2300 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2301 * otherwise all return EOPNOTSUPP.
2302 * XXX: We should make this loadable one day.
2305 sys_sctp_peeloff(td, uap)
2307 struct sctp_peeloff_args /* {
2312 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2313 struct file *nfp = NULL;
2315 struct socket *head, *so;
2319 AUDIT_ARG_FD(uap->sd);
2320 error = fgetsock(td, uap->sd, CAP_PEELOFF, &head, &fflag);
2323 if (head->so_proto->pr_protocol != IPPROTO_SCTP) {
2327 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2331 * At this point we know we do have a assoc to pull
2332 * we proceed to get the fd setup. This may block
2336 error = falloc(td, &nfp, &fd, 0);
2339 td->td_retval[0] = fd;
2341 CURVNET_SET(head->so_vnet);
2342 so = sonewconn(head, SS_ISCONNECTED);
2346 * Before changing the flags on the socket, we have to bump the
2347 * reference count. Otherwise, if the protocol calls sofree(),
2348 * the socket will be released due to a zero refcount.
2351 soref(so); /* file descriptor reference */
2356 TAILQ_REMOVE(&head->so_comp, so, so_list);
2358 so->so_state |= (head->so_state & SS_NBIO);
2359 so->so_state &= ~SS_NOFDREF;
2360 so->so_qstate &= ~SQ_COMP;
2363 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2364 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2367 if (head->so_sigio != NULL)
2368 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2372 * close the new descriptor, assuming someone hasn't ripped it
2373 * out from under us.
2376 fdclose(td->td_proc->p_fd, nfp, fd, td);
2379 * Release explicitly held references before returning.
2389 return (EOPNOTSUPP);
2394 sys_sctp_generic_sendmsg (td, uap)
2396 struct sctp_generic_sendmsg_args /* {
2402 struct sctp_sndrcvinfo *sinfo,
2406 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2407 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2409 struct file *fp = NULL;
2411 struct sockaddr *to = NULL;
2413 struct uio *ktruio = NULL;
2416 struct iovec iov[1];
2417 cap_rights_t rights;
2420 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2428 error = getsockaddr(&to, uap->to, uap->tolen);
2433 rights |= CAP_CONNECT;
2436 AUDIT_ARG_FD(uap->sd);
2437 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2441 if (to && (KTRPOINT(td, KTR_STRUCT)))
2445 iov[0].iov_base = uap->msg;
2446 iov[0].iov_len = uap->mlen;
2448 so = (struct socket *)fp->f_data;
2449 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2454 error = mac_socket_check_send(td->td_ucred, so);
2460 auio.uio_iovcnt = 1;
2461 auio.uio_segflg = UIO_USERSPACE;
2462 auio.uio_rw = UIO_WRITE;
2464 auio.uio_offset = 0; /* XXX */
2466 len = auio.uio_resid = uap->mlen;
2467 CURVNET_SET(so->so_vnet);
2468 error = sctp_lower_sosend(so, to, &auio,
2469 (struct mbuf *)NULL, (struct mbuf *)NULL,
2470 uap->flags, u_sinfo, td);
2473 if (auio.uio_resid != len && (error == ERESTART ||
2474 error == EINTR || error == EWOULDBLOCK))
2476 /* Generation of SIGPIPE can be controlled per socket. */
2477 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2478 !(uap->flags & MSG_NOSIGNAL)) {
2479 PROC_LOCK(td->td_proc);
2480 tdsignal(td, SIGPIPE);
2481 PROC_UNLOCK(td->td_proc);
2485 td->td_retval[0] = len - auio.uio_resid;
2487 if (ktruio != NULL) {
2488 ktruio->uio_resid = td->td_retval[0];
2489 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2500 return (EOPNOTSUPP);
2505 sys_sctp_generic_sendmsg_iov(td, uap)
2507 struct sctp_generic_sendmsg_iov_args /* {
2513 struct sctp_sndrcvinfo *sinfo,
2517 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2518 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2520 struct file *fp = NULL;
2523 struct sockaddr *to = NULL;
2525 struct uio *ktruio = NULL;
2528 struct iovec *iov, *tiov;
2529 cap_rights_t rights;
2532 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2539 error = getsockaddr(&to, uap->to, uap->tolen);
2544 rights |= CAP_CONNECT;
2547 AUDIT_ARG_FD(uap->sd);
2548 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2552 #ifdef COMPAT_FREEBSD32
2553 if (SV_CURPROC_FLAG(SV_ILP32))
2554 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2555 uap->iovlen, &iov, EMSGSIZE);
2558 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2562 if (to && (KTRPOINT(td, KTR_STRUCT)))
2566 so = (struct socket *)fp->f_data;
2567 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2572 error = mac_socket_check_send(td->td_ucred, so);
2578 auio.uio_iovcnt = uap->iovlen;
2579 auio.uio_segflg = UIO_USERSPACE;
2580 auio.uio_rw = UIO_WRITE;
2582 auio.uio_offset = 0; /* XXX */
2585 for (i = 0; i <uap->iovlen; i++, tiov++) {
2586 if ((auio.uio_resid += tiov->iov_len) < 0) {
2591 len = auio.uio_resid;
2592 CURVNET_SET(so->so_vnet);
2593 error = sctp_lower_sosend(so, to, &auio,
2594 (struct mbuf *)NULL, (struct mbuf *)NULL,
2595 uap->flags, u_sinfo, td);
2598 if (auio.uio_resid != len && (error == ERESTART ||
2599 error == EINTR || error == EWOULDBLOCK))
2601 /* Generation of SIGPIPE can be controlled per socket */
2602 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2603 !(uap->flags & MSG_NOSIGNAL)) {
2604 PROC_LOCK(td->td_proc);
2605 tdsignal(td, SIGPIPE);
2606 PROC_UNLOCK(td->td_proc);
2610 td->td_retval[0] = len - auio.uio_resid;
2612 if (ktruio != NULL) {
2613 ktruio->uio_resid = td->td_retval[0];
2614 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2627 return (EOPNOTSUPP);
2632 sys_sctp_generic_recvmsg(td, uap)
2634 struct sctp_generic_recvmsg_args /* {
2638 struct sockaddr *from,
2639 __socklen_t *fromlenaddr,
2640 struct sctp_sndrcvinfo *sinfo,
2644 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2645 uint8_t sockbufstore[256];
2647 struct iovec *iov, *tiov;
2648 struct sctp_sndrcvinfo sinfo;
2650 struct file *fp = NULL;
2651 struct sockaddr *fromsa;
2657 struct uio *ktruio = NULL;
2660 AUDIT_ARG_FD(uap->sd);
2661 error = getsock_cap(td->td_proc->p_fd, uap->sd, CAP_READ, &fp, NULL);
2665 #ifdef COMPAT_FREEBSD32
2666 if (SV_CURPROC_FLAG(SV_ILP32))
2667 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2668 uap->iovlen, &iov, EMSGSIZE);
2671 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2676 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2681 error = mac_socket_check_receive(td->td_ucred, so);
2687 if (uap->fromlenaddr) {
2688 error = copyin(uap->fromlenaddr,
2689 &fromlen, sizeof (fromlen));
2696 if (uap->msg_flags) {
2697 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2705 auio.uio_iovcnt = uap->iovlen;
2706 auio.uio_segflg = UIO_USERSPACE;
2707 auio.uio_rw = UIO_READ;
2709 auio.uio_offset = 0; /* XXX */
2712 for (i = 0; i <uap->iovlen; i++, tiov++) {
2713 if ((auio.uio_resid += tiov->iov_len) < 0) {
2718 len = auio.uio_resid;
2719 fromsa = (struct sockaddr *)sockbufstore;
2722 if (KTRPOINT(td, KTR_GENIO))
2723 ktruio = cloneuio(&auio);
2725 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
2726 CURVNET_SET(so->so_vnet);
2727 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2728 fromsa, fromlen, &msg_flags,
2729 (struct sctp_sndrcvinfo *)&sinfo, 1);
2732 if (auio.uio_resid != len && (error == ERESTART ||
2733 error == EINTR || error == EWOULDBLOCK))
2737 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2740 if (ktruio != NULL) {
2741 ktruio->uio_resid = len - auio.uio_resid;
2742 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2747 td->td_retval[0] = len - auio.uio_resid;
2749 if (fromlen && uap->from) {
2751 if (len <= 0 || fromsa == 0)
2754 len = MIN(len, fromsa->sa_len);
2755 error = copyout(fromsa, uap->from, (size_t)len);
2759 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2765 if (KTRPOINT(td, KTR_STRUCT))
2766 ktrsockaddr(fromsa);
2768 if (uap->msg_flags) {
2769 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2782 return (EOPNOTSUPP);