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 if required
125 * capability rights are present.
126 * A reference on the file entry is held upon returning.
129 getsock_cap(struct filedesc *fdp, int fd, cap_rights_t rights,
130 struct file **fpp, u_int *fflagp)
135 error = fget_unlocked(fdp, fd, rights, 0, &fp, NULL);
138 if (fp->f_type != DTYPE_SOCKET) {
139 fdrop(fp, curthread);
143 *fflagp = fp->f_flag;
149 * System call interface to the socket abstraction.
151 #if defined(COMPAT_43)
152 #define COMPAT_OLDSOCK
158 struct socket_args /* {
168 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
170 error = mac_socket_check_create(td->td_ucred, uap->domain, uap->type,
175 error = falloc(td, &fp, &fd, 0);
178 /* An extra reference on `fp' has been held for us by falloc(). */
179 error = socreate(uap->domain, &so, uap->type, uap->protocol,
182 fdclose(td->td_proc->p_fd, fp, fd, td);
184 finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &socketops);
185 td->td_retval[0] = fd;
195 struct bind_args /* {
204 error = getsockaddr(&sa, uap->name, uap->namelen);
206 error = kern_bind(td, uap->s, sa);
213 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
220 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
221 error = getsock_cap(td->td_proc->p_fd, fd, CAP_BIND, &fp, NULL);
226 if (KTRPOINT(td, KTR_STRUCT))
230 error = mac_socket_check_bind(td->td_ucred, so, sa);
233 if (dirfd == AT_FDCWD)
234 error = sobind(so, sa, td);
236 error = sobindat(dirfd, so, sa, td);
245 kern_bind(struct thread *td, int fd, struct sockaddr *sa)
248 return (kern_bindat(td, AT_FDCWD, fd, sa));
255 struct bindat_args /* {
265 error = getsockaddr(&sa, uap->name, uap->namelen);
267 error = kern_bindat(td, uap->fd, uap->s, sa);
277 struct listen_args /* {
286 AUDIT_ARG_FD(uap->s);
287 error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_LISTEN, &fp, NULL);
291 error = mac_socket_check_listen(td->td_ucred, so);
294 error = solisten(so, uap->backlog, td);
304 accept1(td, uap, compat)
306 struct accept_args /* {
308 struct sockaddr * __restrict name;
309 socklen_t * __restrict anamelen;
313 struct sockaddr *name;
318 if (uap->name == NULL)
319 return (kern_accept(td, uap->s, NULL, NULL, NULL));
321 error = copyin(uap->anamelen, &namelen, sizeof (namelen));
325 error = kern_accept(td, uap->s, &name, &namelen, &fp);
328 * return a namelen of zero for older code which might
329 * ignore the return value from accept.
332 (void) copyout(&namelen,
333 uap->anamelen, sizeof(*uap->anamelen));
337 if (error == 0 && name != NULL) {
338 #ifdef COMPAT_OLDSOCK
340 ((struct osockaddr *)name)->sa_family =
343 error = copyout(name, uap->name, namelen);
346 error = copyout(&namelen, uap->anamelen,
349 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
351 free(name, M_SONAME);
356 kern_accept(struct thread *td, int s, struct sockaddr **name,
357 socklen_t *namelen, struct file **fp)
359 struct filedesc *fdp;
360 struct file *headfp, *nfp = NULL;
361 struct sockaddr *sa = NULL;
363 struct socket *head, *so;
376 fdp = td->td_proc->p_fd;
377 error = getsock_cap(fdp, s, CAP_ACCEPT, &headfp, &fflag);
380 head = headfp->f_data;
381 if ((head->so_options & SO_ACCEPTCONN) == 0) {
386 error = mac_socket_check_accept(td->td_ucred, head);
390 error = falloc(td, &nfp, &fd, 0);
394 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
399 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
400 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
401 head->so_error = ECONNABORTED;
404 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
411 if (head->so_error) {
412 error = head->so_error;
417 so = TAILQ_FIRST(&head->so_comp);
418 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
419 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
422 * Before changing the flags on the socket, we have to bump the
423 * reference count. Otherwise, if the protocol calls sofree(),
424 * the socket will be released due to a zero refcount.
426 SOCK_LOCK(so); /* soref() and so_state update */
427 soref(so); /* file descriptor reference */
429 TAILQ_REMOVE(&head->so_comp, so, so_list);
431 so->so_state |= (head->so_state & SS_NBIO);
432 so->so_qstate &= ~SQ_COMP;
438 /* An extra reference on `nfp' has been held for us by falloc(). */
439 td->td_retval[0] = fd;
441 /* connection has been removed from the listen queue */
442 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
444 pgid = fgetown(&head->so_sigio);
446 fsetown(pgid, &so->so_sigio);
448 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
449 /* Sync socket nonblocking/async state with file flags */
450 tmp = fflag & FNONBLOCK;
451 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
452 tmp = fflag & FASYNC;
453 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
455 error = soaccept(so, &sa);
458 * return a namelen of zero for older code which might
459 * ignore the return value from accept.
470 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
472 /* check sa_len before it is destroyed */
473 if (*namelen > sa->sa_len)
474 *namelen = sa->sa_len;
476 if (KTRPOINT(td, KTR_STRUCT))
487 * close the new descriptor, assuming someone hasn't ripped it
491 fdclose(fdp, nfp, fd, td);
494 * Release explicitly held references before returning. We return
495 * a reference on nfp to the caller on success if they request it.
514 struct accept_args *uap;
517 return (accept1(td, uap, 0));
520 #ifdef COMPAT_OLDSOCK
524 struct accept_args *uap;
527 return (accept1(td, uap, 1));
529 #endif /* COMPAT_OLDSOCK */
535 struct connect_args /* {
544 error = getsockaddr(&sa, uap->name, uap->namelen);
546 error = kern_connect(td, uap->s, sa);
553 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
561 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
562 error = getsock_cap(td->td_proc->p_fd, fd, CAP_CONNECT, &fp, NULL);
566 if (so->so_state & SS_ISCONNECTING) {
571 if (KTRPOINT(td, KTR_STRUCT))
575 error = mac_socket_check_connect(td->td_ucred, so, sa);
579 if (dirfd == AT_FDCWD)
580 error = soconnect(so, sa, td);
582 error = soconnectat(dirfd, so, sa, td);
585 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
590 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
591 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
594 if (error == EINTR || error == ERESTART)
600 error = so->so_error;
606 so->so_state &= ~SS_ISCONNECTING;
607 if (error == ERESTART)
615 kern_connect(struct thread *td, int fd, struct sockaddr *sa)
618 return (kern_connectat(td, AT_FDCWD, fd, sa));
623 sys_connectat(td, uap)
625 struct connectat_args /* {
635 error = getsockaddr(&sa, uap->name, uap->namelen);
637 error = kern_connectat(td, uap->fd, uap->s, sa);
644 kern_socketpair(struct thread *td, int domain, int type, int protocol,
647 struct filedesc *fdp = td->td_proc->p_fd;
648 struct file *fp1, *fp2;
649 struct socket *so1, *so2;
652 AUDIT_ARG_SOCKET(domain, type, protocol);
654 /* We might want to have a separate check for socket pairs. */
655 error = mac_socket_check_create(td->td_ucred, domain, type,
660 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
663 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
666 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
667 error = falloc(td, &fp1, &fd, 0);
671 fp1->f_data = so1; /* so1 already has ref count */
672 error = falloc(td, &fp2, &fd, 0);
675 fp2->f_data = so2; /* so2 already has ref count */
677 error = soconnect2(so1, so2);
680 if (type == SOCK_DGRAM) {
682 * Datagram socket connection is asymmetric.
684 error = soconnect2(so2, so1);
688 finit(fp1, FREAD | FWRITE, DTYPE_SOCKET, fp1->f_data, &socketops);
689 finit(fp2, FREAD | FWRITE, DTYPE_SOCKET, fp2->f_data, &socketops);
694 fdclose(fdp, fp2, rsv[1], td);
697 fdclose(fdp, fp1, rsv[0], td);
709 sys_socketpair(struct thread *td, struct socketpair_args *uap)
713 error = kern_socketpair(td, uap->domain, uap->type,
717 error = copyout(sv, uap->rsv, 2 * sizeof(int));
719 (void)kern_close(td, sv[0]);
720 (void)kern_close(td, sv[1]);
726 sendit(td, s, mp, flags)
732 struct mbuf *control;
736 #ifdef CAPABILITY_MODE
737 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
741 if (mp->msg_name != NULL) {
742 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
752 if (mp->msg_control) {
753 if (mp->msg_controllen < sizeof(struct cmsghdr)
754 #ifdef COMPAT_OLDSOCK
755 && mp->msg_flags != MSG_COMPAT
761 error = sockargs(&control, mp->msg_control,
762 mp->msg_controllen, MT_CONTROL);
765 #ifdef COMPAT_OLDSOCK
766 if (mp->msg_flags == MSG_COMPAT) {
769 M_PREPEND(control, sizeof(*cm), M_WAITOK);
770 cm = mtod(control, struct cmsghdr *);
771 cm->cmsg_len = control->m_len;
772 cm->cmsg_level = SOL_SOCKET;
773 cm->cmsg_type = SCM_RIGHTS;
780 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
789 kern_sendit(td, s, mp, flags, control, segflg)
794 struct mbuf *control;
805 struct uio *ktruio = NULL;
810 if (mp->msg_name != NULL) {
811 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
812 rights |= CAP_CONNECT;
814 error = getsock_cap(td->td_proc->p_fd, s, rights, &fp, NULL);
817 so = (struct socket *)fp->f_data;
820 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
821 ktrsockaddr(mp->msg_name);
824 if (mp->msg_name != NULL) {
825 error = mac_socket_check_connect(td->td_ucred, so,
830 error = mac_socket_check_send(td->td_ucred, so);
835 auio.uio_iov = mp->msg_iov;
836 auio.uio_iovcnt = mp->msg_iovlen;
837 auio.uio_segflg = segflg;
838 auio.uio_rw = UIO_WRITE;
840 auio.uio_offset = 0; /* XXX */
843 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
844 if ((auio.uio_resid += iov->iov_len) < 0) {
850 if (KTRPOINT(td, KTR_GENIO))
851 ktruio = cloneuio(&auio);
853 len = auio.uio_resid;
854 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
856 if (auio.uio_resid != len && (error == ERESTART ||
857 error == EINTR || error == EWOULDBLOCK))
859 /* Generation of SIGPIPE can be controlled per socket */
860 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
861 !(flags & MSG_NOSIGNAL)) {
862 PROC_LOCK(td->td_proc);
863 tdsignal(td, SIGPIPE);
864 PROC_UNLOCK(td->td_proc);
868 td->td_retval[0] = len - auio.uio_resid;
870 if (ktruio != NULL) {
871 ktruio->uio_resid = td->td_retval[0];
872 ktrgenio(s, UIO_WRITE, ktruio, error);
883 struct sendto_args /* {
896 msg.msg_name = uap->to;
897 msg.msg_namelen = uap->tolen;
901 #ifdef COMPAT_OLDSOCK
904 aiov.iov_base = uap->buf;
905 aiov.iov_len = uap->len;
906 error = sendit(td, uap->s, &msg, uap->flags);
910 #ifdef COMPAT_OLDSOCK
914 struct osend_args /* {
929 aiov.iov_base = uap->buf;
930 aiov.iov_len = uap->len;
933 error = sendit(td, uap->s, &msg, uap->flags);
940 struct osendmsg_args /* {
950 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
953 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
957 msg.msg_flags = MSG_COMPAT;
958 error = sendit(td, uap->s, &msg, uap->flags);
967 struct sendmsg_args /* {
977 error = copyin(uap->msg, &msg, sizeof (msg));
980 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
984 #ifdef COMPAT_OLDSOCK
987 error = sendit(td, uap->s, &msg, uap->flags);
993 kern_recvit(td, s, mp, fromseg, controlp)
997 enum uio_seg fromseg;
998 struct mbuf **controlp;
1005 struct mbuf *m, *control = NULL;
1009 struct sockaddr *fromsa = NULL;
1011 struct uio *ktruio = NULL;
1014 if (controlp != NULL)
1018 error = getsock_cap(td->td_proc->p_fd, s, CAP_RECV, &fp, NULL);
1024 error = mac_socket_check_receive(td->td_ucred, so);
1031 auio.uio_iov = mp->msg_iov;
1032 auio.uio_iovcnt = mp->msg_iovlen;
1033 auio.uio_segflg = UIO_USERSPACE;
1034 auio.uio_rw = UIO_READ;
1036 auio.uio_offset = 0; /* XXX */
1039 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
1040 if ((auio.uio_resid += iov->iov_len) < 0) {
1046 if (KTRPOINT(td, KTR_GENIO))
1047 ktruio = cloneuio(&auio);
1049 len = auio.uio_resid;
1050 error = soreceive(so, &fromsa, &auio, NULL,
1051 (mp->msg_control || controlp) ? &control : NULL,
1054 if (auio.uio_resid != len && (error == ERESTART ||
1055 error == EINTR || error == EWOULDBLOCK))
1059 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
1061 if (ktruio != NULL) {
1062 ktruio->uio_resid = len - auio.uio_resid;
1063 ktrgenio(s, UIO_READ, ktruio, error);
1068 td->td_retval[0] = len - auio.uio_resid;
1070 len = mp->msg_namelen;
1071 if (len <= 0 || fromsa == NULL)
1074 /* save sa_len before it is destroyed by MSG_COMPAT */
1075 len = MIN(len, fromsa->sa_len);
1076 #ifdef COMPAT_OLDSOCK
1077 if (mp->msg_flags & MSG_COMPAT)
1078 ((struct osockaddr *)fromsa)->sa_family =
1081 if (fromseg == UIO_USERSPACE) {
1082 error = copyout(fromsa, mp->msg_name,
1087 bcopy(fromsa, mp->msg_name, len);
1089 mp->msg_namelen = len;
1091 if (mp->msg_control && controlp == NULL) {
1092 #ifdef COMPAT_OLDSOCK
1094 * We assume that old recvmsg calls won't receive access
1095 * rights and other control info, esp. as control info
1096 * is always optional and those options didn't exist in 4.3.
1097 * If we receive rights, trim the cmsghdr; anything else
1100 if (control && mp->msg_flags & MSG_COMPAT) {
1101 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1103 mtod(control, struct cmsghdr *)->cmsg_type !=
1105 mp->msg_controllen = 0;
1108 control->m_len -= sizeof (struct cmsghdr);
1109 control->m_data += sizeof (struct cmsghdr);
1112 len = mp->msg_controllen;
1114 mp->msg_controllen = 0;
1115 ctlbuf = mp->msg_control;
1117 while (m && len > 0) {
1118 unsigned int tocopy;
1120 if (len >= m->m_len)
1123 mp->msg_flags |= MSG_CTRUNC;
1127 if ((error = copyout(mtod(m, caddr_t),
1128 ctlbuf, tocopy)) != 0)
1135 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1140 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1141 ktrsockaddr(fromsa);
1144 free(fromsa, M_SONAME);
1146 if (error == 0 && controlp != NULL)
1147 *controlp = control;
1155 recvit(td, s, mp, namelenp)
1163 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1167 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1168 #ifdef COMPAT_OLDSOCK
1169 if (mp->msg_flags & MSG_COMPAT)
1170 error = 0; /* old recvfrom didn't check */
1177 sys_recvfrom(td, uap)
1179 struct recvfrom_args /* {
1184 struct sockaddr * __restrict from;
1185 socklen_t * __restrict fromlenaddr;
1192 if (uap->fromlenaddr) {
1193 error = copyin(uap->fromlenaddr,
1194 &msg.msg_namelen, sizeof (msg.msg_namelen));
1198 msg.msg_namelen = 0;
1200 msg.msg_name = uap->from;
1201 msg.msg_iov = &aiov;
1203 aiov.iov_base = uap->buf;
1204 aiov.iov_len = uap->len;
1205 msg.msg_control = 0;
1206 msg.msg_flags = uap->flags;
1207 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1212 #ifdef COMPAT_OLDSOCK
1216 struct recvfrom_args *uap;
1219 uap->flags |= MSG_COMPAT;
1220 return (sys_recvfrom(td, uap));
1224 #ifdef COMPAT_OLDSOCK
1228 struct orecv_args /* {
1240 msg.msg_namelen = 0;
1241 msg.msg_iov = &aiov;
1243 aiov.iov_base = uap->buf;
1244 aiov.iov_len = uap->len;
1245 msg.msg_control = 0;
1246 msg.msg_flags = uap->flags;
1247 error = recvit(td, uap->s, &msg, NULL);
1252 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1253 * overlays the new one, missing only the flags, and with the (old) access
1254 * rights where the control fields are now.
1259 struct orecvmsg_args /* {
1261 struct omsghdr *msg;
1269 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1272 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1275 msg.msg_flags = uap->flags | MSG_COMPAT;
1277 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1278 if (msg.msg_controllen && error == 0)
1279 error = copyout(&msg.msg_controllen,
1280 &uap->msg->msg_accrightslen, sizeof (int));
1287 sys_recvmsg(td, uap)
1289 struct recvmsg_args /* {
1296 struct iovec *uiov, *iov;
1299 error = copyin(uap->msg, &msg, sizeof (msg));
1302 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1305 msg.msg_flags = uap->flags;
1306 #ifdef COMPAT_OLDSOCK
1307 msg.msg_flags &= ~MSG_COMPAT;
1311 error = recvit(td, uap->s, &msg, NULL);
1314 error = copyout(&msg, uap->msg, sizeof(msg));
1322 sys_shutdown(td, uap)
1324 struct shutdown_args /* {
1333 AUDIT_ARG_FD(uap->s);
1334 error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_SHUTDOWN, &fp,
1338 error = soshutdown(so, uap->how);
1346 sys_setsockopt(td, uap)
1348 struct setsockopt_args /* {
1357 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1358 uap->val, UIO_USERSPACE, uap->valsize));
1362 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1368 enum uio_seg valseg;
1374 struct sockopt sopt;
1376 if (val == NULL && valsize != 0)
1378 if ((int)valsize < 0)
1381 sopt.sopt_dir = SOPT_SET;
1382 sopt.sopt_level = level;
1383 sopt.sopt_name = name;
1384 sopt.sopt_val = val;
1385 sopt.sopt_valsize = valsize;
1391 sopt.sopt_td = NULL;
1394 panic("kern_setsockopt called with bad valseg");
1398 error = getsock_cap(td->td_proc->p_fd, s, CAP_SETSOCKOPT, &fp, NULL);
1401 error = sosetopt(so, &sopt);
1409 sys_getsockopt(td, uap)
1411 struct getsockopt_args /* {
1415 void * __restrict val;
1416 socklen_t * __restrict avalsize;
1423 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1428 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1429 uap->val, UIO_USERSPACE, &valsize);
1432 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1437 * Kernel version of getsockopt.
1438 * optval can be a userland or userspace. optlen is always a kernel pointer.
1441 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1447 enum uio_seg valseg;
1453 struct sockopt sopt;
1457 if ((int)*valsize < 0)
1460 sopt.sopt_dir = SOPT_GET;
1461 sopt.sopt_level = level;
1462 sopt.sopt_name = name;
1463 sopt.sopt_val = val;
1464 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1470 sopt.sopt_td = NULL;
1473 panic("kern_getsockopt called with bad valseg");
1477 error = getsock_cap(td->td_proc->p_fd, s, CAP_GETSOCKOPT, &fp, NULL);
1480 error = sogetopt(so, &sopt);
1481 *valsize = sopt.sopt_valsize;
1488 * getsockname1() - Get socket name.
1492 getsockname1(td, uap, compat)
1494 struct getsockname_args /* {
1496 struct sockaddr * __restrict asa;
1497 socklen_t * __restrict alen;
1501 struct sockaddr *sa;
1505 error = copyin(uap->alen, &len, sizeof(len));
1509 error = kern_getsockname(td, uap->fdes, &sa, &len);
1514 #ifdef COMPAT_OLDSOCK
1516 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1518 error = copyout(sa, uap->asa, (u_int)len);
1522 error = copyout(&len, uap->alen, sizeof(len));
1527 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1539 error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETSOCKNAME, &fp, NULL);
1544 CURVNET_SET(so->so_vnet);
1545 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1552 len = MIN(*alen, (*sa)->sa_len);
1555 if (KTRPOINT(td, KTR_STRUCT))
1561 free(*sa, M_SONAME);
1568 sys_getsockname(td, uap)
1570 struct getsockname_args *uap;
1573 return (getsockname1(td, uap, 0));
1576 #ifdef COMPAT_OLDSOCK
1578 ogetsockname(td, uap)
1580 struct getsockname_args *uap;
1583 return (getsockname1(td, uap, 1));
1585 #endif /* COMPAT_OLDSOCK */
1588 * getpeername1() - Get name of peer for connected socket.
1592 getpeername1(td, uap, compat)
1594 struct getpeername_args /* {
1596 struct sockaddr * __restrict asa;
1597 socklen_t * __restrict alen;
1601 struct sockaddr *sa;
1605 error = copyin(uap->alen, &len, sizeof (len));
1609 error = kern_getpeername(td, uap->fdes, &sa, &len);
1614 #ifdef COMPAT_OLDSOCK
1616 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1618 error = copyout(sa, uap->asa, (u_int)len);
1622 error = copyout(&len, uap->alen, sizeof(len));
1627 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1639 error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETPEERNAME, &fp, NULL);
1643 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1648 CURVNET_SET(so->so_vnet);
1649 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1656 len = MIN(*alen, (*sa)->sa_len);
1659 if (KTRPOINT(td, KTR_STRUCT))
1664 free(*sa, M_SONAME);
1673 sys_getpeername(td, uap)
1675 struct getpeername_args *uap;
1678 return (getpeername1(td, uap, 0));
1681 #ifdef COMPAT_OLDSOCK
1683 ogetpeername(td, uap)
1685 struct ogetpeername_args *uap;
1688 /* XXX uap should have type `getpeername_args *' to begin with. */
1689 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1691 #endif /* COMPAT_OLDSOCK */
1694 sockargs(mp, buf, buflen, type)
1699 struct sockaddr *sa;
1703 if ((u_int)buflen > MLEN) {
1704 #ifdef COMPAT_OLDSOCK
1705 if (type == MT_SONAME && (u_int)buflen <= 112)
1706 buflen = MLEN; /* unix domain compat. hack */
1709 if ((u_int)buflen > MCLBYTES)
1712 m = m_get(M_WAITOK, type);
1713 if ((u_int)buflen > MLEN)
1714 MCLGET(m, M_WAITOK);
1716 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1721 if (type == MT_SONAME) {
1722 sa = mtod(m, struct sockaddr *);
1724 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1725 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1726 sa->sa_family = sa->sa_len;
1728 sa->sa_len = buflen;
1735 getsockaddr(namp, uaddr, len)
1736 struct sockaddr **namp;
1740 struct sockaddr *sa;
1743 if (len > SOCK_MAXADDRLEN)
1744 return (ENAMETOOLONG);
1745 if (len < offsetof(struct sockaddr, sa_data[0]))
1747 sa = malloc(len, M_SONAME, M_WAITOK);
1748 error = copyin(uaddr, sa, len);
1752 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1753 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1754 sa->sa_family = sa->sa_len;
1762 #include <sys/condvar.h>
1764 struct sendfile_sync {
1771 * Detach mapped page and release resources back to the system.
1774 sf_buf_mext(void *addr, void *args)
1777 struct sendfile_sync *sfs;
1779 m = sf_buf_page(args);
1782 vm_page_unwire(m, 0);
1784 * Check for the object going away on us. This can
1785 * happen since we don't hold a reference to it.
1786 * If so, we're responsible for freeing the page.
1788 if (m->wire_count == 0 && m->object == NULL)
1794 mtx_lock(&sfs->mtx);
1795 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1796 if (--sfs->count == 0)
1797 cv_signal(&sfs->cv);
1798 mtx_unlock(&sfs->mtx);
1804 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1805 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1807 * Send a file specified by 'fd' and starting at 'offset' to a socket
1808 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1809 * 0. Optionally add a header and/or trailer to the socket output. If
1810 * specified, write the total number of bytes sent into *sbytes.
1813 sys_sendfile(struct thread *td, struct sendfile_args *uap)
1816 return (do_sendfile(td, uap, 0));
1820 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1822 struct sf_hdtr hdtr;
1823 struct uio *hdr_uio, *trl_uio;
1826 hdr_uio = trl_uio = NULL;
1828 if (uap->hdtr != NULL) {
1829 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1832 if (hdtr.headers != NULL) {
1833 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1837 if (hdtr.trailers != NULL) {
1838 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1845 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1848 free(hdr_uio, M_IOV);
1850 free(trl_uio, M_IOV);
1854 #ifdef COMPAT_FREEBSD4
1856 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1858 struct sendfile_args args;
1862 args.offset = uap->offset;
1863 args.nbytes = uap->nbytes;
1864 args.hdtr = uap->hdtr;
1865 args.sbytes = uap->sbytes;
1866 args.flags = uap->flags;
1868 return (do_sendfile(td, &args, 1));
1870 #endif /* COMPAT_FREEBSD4 */
1873 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1874 struct uio *hdr_uio, struct uio *trl_uio, int compat)
1876 struct file *sock_fp;
1878 struct vm_object *obj = NULL;
1879 struct socket *so = NULL;
1880 struct mbuf *m = NULL;
1883 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1884 int error, hdrlen = 0, mnw = 0;
1885 struct sendfile_sync *sfs = NULL;
1888 * The file descriptor must be a regular file and have a
1889 * backing VM object.
1890 * File offset must be positive. If it goes beyond EOF
1891 * we send only the header/trailer and no payload data.
1893 AUDIT_ARG_FD(uap->fd);
1895 * sendfile(2) can start at any offset within a file so we require
1896 * CAP_READ+CAP_SEEK = CAP_PREAD.
1898 if ((error = fgetvp_read(td, uap->fd, CAP_PREAD, &vp)) != 0)
1900 vn_lock(vp, LK_SHARED | LK_RETRY);
1901 if (vp->v_type == VREG) {
1905 * Temporarily increase the backing VM
1906 * object's reference count so that a forced
1907 * reclamation of its vnode does not
1908 * immediately destroy it.
1910 VM_OBJECT_LOCK(obj);
1911 if ((obj->flags & OBJ_DEAD) == 0) {
1912 vm_object_reference_locked(obj);
1913 VM_OBJECT_UNLOCK(obj);
1915 VM_OBJECT_UNLOCK(obj);
1925 if (uap->offset < 0) {
1931 * The socket must be a stream socket and connected.
1932 * Remember if it a blocking or non-blocking socket.
1934 if ((error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_SEND,
1935 &sock_fp, NULL)) != 0)
1937 so = sock_fp->f_data;
1938 if (so->so_type != SOCK_STREAM) {
1942 if ((so->so_state & SS_ISCONNECTED) == 0) {
1947 * Do not wait on memory allocations but return ENOMEM for
1948 * caller to retry later.
1949 * XXX: Experimental.
1951 if (uap->flags & SF_MNOWAIT)
1954 if (uap->flags & SF_SYNC) {
1955 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO);
1956 mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
1957 cv_init(&sfs->cv, "sendfile");
1961 error = mac_socket_check_send(td->td_ucred, so);
1966 /* If headers are specified copy them into mbufs. */
1967 if (hdr_uio != NULL) {
1968 hdr_uio->uio_td = td;
1969 hdr_uio->uio_rw = UIO_WRITE;
1970 if (hdr_uio->uio_resid > 0) {
1972 * In FBSD < 5.0 the nbytes to send also included
1973 * the header. If compat is specified subtract the
1974 * header size from nbytes.
1977 if (uap->nbytes > hdr_uio->uio_resid)
1978 uap->nbytes -= hdr_uio->uio_resid;
1982 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
1985 error = mnw ? EAGAIN : ENOBUFS;
1988 hdrlen = m_length(m, NULL);
1993 * Protect against multiple writers to the socket.
1995 * XXXRW: Historically this has assumed non-interruptibility, so now
1996 * we implement that, but possibly shouldn't.
1998 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
2001 * Loop through the pages of the file, starting with the requested
2002 * offset. Get a file page (do I/O if necessary), map the file page
2003 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
2005 * This is done in two loops. The inner loop turns as many pages
2006 * as it can, up to available socket buffer space, without blocking
2007 * into mbufs to have it bulk delivered into the socket send buffer.
2008 * The outer loop checks the state and available space of the socket
2009 * and takes care of the overall progress.
2011 for (off = uap->offset, rem = uap->nbytes; ; ) {
2012 struct mbuf *mtail = NULL;
2018 * Check the socket state for ongoing connection,
2019 * no errors and space in socket buffer.
2020 * If space is low allow for the remainder of the
2021 * file to be processed if it fits the socket buffer.
2022 * Otherwise block in waiting for sufficient space
2023 * to proceed, or if the socket is nonblocking, return
2024 * to userland with EAGAIN while reporting how far
2026 * We wait until the socket buffer has significant free
2027 * space to do bulk sends. This makes good use of file
2028 * system read ahead and allows packet segmentation
2029 * offloading hardware to take over lots of work. If
2030 * we were not careful here we would send off only one
2033 SOCKBUF_LOCK(&so->so_snd);
2034 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
2035 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
2037 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2039 SOCKBUF_UNLOCK(&so->so_snd);
2041 } else if (so->so_error) {
2042 error = so->so_error;
2044 SOCKBUF_UNLOCK(&so->so_snd);
2047 space = sbspace(&so->so_snd);
2050 space < so->so_snd.sb_lowat)) {
2051 if (so->so_state & SS_NBIO) {
2052 SOCKBUF_UNLOCK(&so->so_snd);
2057 * sbwait drops the lock while sleeping.
2058 * When we loop back to retry_space the
2059 * state may have changed and we retest
2062 error = sbwait(&so->so_snd);
2064 * An error from sbwait usually indicates that we've
2065 * been interrupted by a signal. If we've sent anything
2066 * then return bytes sent, otherwise return the error.
2069 SOCKBUF_UNLOCK(&so->so_snd);
2074 SOCKBUF_UNLOCK(&so->so_snd);
2077 * Reduce space in the socket buffer by the size of
2078 * the header mbuf chain.
2079 * hdrlen is set to 0 after the first loop.
2084 * Loop and construct maximum sized mbuf chain to be bulk
2085 * dumped into socket buffer.
2087 while (space > loopbytes) {
2092 VM_OBJECT_LOCK(obj);
2094 * Calculate the amount to transfer.
2095 * Not to exceed a page, the EOF,
2096 * or the passed in nbytes.
2098 pgoff = (vm_offset_t)(off & PAGE_MASK);
2099 xfsize = omin(PAGE_SIZE - pgoff,
2100 obj->un_pager.vnp.vnp_size - uap->offset -
2101 fsbytes - loopbytes);
2103 rem = (uap->nbytes - fsbytes - loopbytes);
2105 rem = obj->un_pager.vnp.vnp_size -
2106 uap->offset - fsbytes - loopbytes;
2107 xfsize = omin(rem, xfsize);
2108 xfsize = omin(space - loopbytes, xfsize);
2110 VM_OBJECT_UNLOCK(obj);
2111 done = 1; /* all data sent */
2116 * Attempt to look up the page. Allocate
2117 * if not found or wait and loop if busy.
2119 pindex = OFF_TO_IDX(off);
2120 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2121 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY);
2124 * Check if page is valid for what we need,
2125 * otherwise initiate I/O.
2126 * If we already turned some pages into mbufs,
2127 * send them off before we come here again and
2130 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2131 VM_OBJECT_UNLOCK(obj);
2133 error = EAGAIN; /* send what we already got */
2134 else if (uap->flags & SF_NODISKIO)
2141 * Ensure that our page is still around
2142 * when the I/O completes.
2144 vm_page_io_start(pg);
2145 VM_OBJECT_UNLOCK(obj);
2148 * Get the page from backing store.
2150 error = vn_lock(vp, LK_SHARED);
2153 bsize = vp->v_mount->mnt_stat.f_iosize;
2156 * XXXMAC: Because we don't have fp->f_cred
2157 * here, we pass in NOCRED. This is probably
2158 * wrong, but is consistent with our original
2161 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
2162 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2163 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
2164 td->td_ucred, NOCRED, &resid, td);
2167 VM_OBJECT_LOCK(obj);
2168 vm_page_io_finish(pg);
2170 VM_OBJECT_UNLOCK(obj);
2175 vm_page_unwire(pg, 0);
2177 * See if anyone else might know about
2178 * this page. If not and it is not valid,
2181 if (pg->wire_count == 0 && pg->valid == 0 &&
2182 pg->busy == 0 && !(pg->oflags & VPO_BUSY))
2185 VM_OBJECT_UNLOCK(obj);
2186 if (error == EAGAIN)
2187 error = 0; /* not a real error */
2192 * Get a sendfile buf. When allocating the
2193 * first buffer for mbuf chain, we usually
2194 * wait as long as necessary, but this wait
2195 * can be interrupted. For consequent
2196 * buffers, do not sleep, since several
2197 * threads might exhaust the buffers and then
2200 sf = sf_buf_alloc(pg, (mnw || m != NULL) ? SFB_NOWAIT :
2203 mbstat.sf_allocfail++;
2205 vm_page_unwire(pg, 0);
2206 KASSERT(pg->object != NULL,
2207 ("kern_sendfile: object disappeared"));
2210 error = (mnw ? EAGAIN : EINTR);
2215 * Get an mbuf and set it up as having
2218 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2220 error = (mnw ? EAGAIN : ENOBUFS);
2221 sf_buf_mext((void *)sf_buf_kva(sf), sf);
2224 MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext,
2225 sfs, sf, M_RDONLY, EXT_SFBUF);
2226 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2229 /* Append to mbuf chain. */
2233 m_last(m)->m_next = m0;
2238 /* Keep track of bits processed. */
2239 loopbytes += xfsize;
2243 mtx_lock(&sfs->mtx);
2245 mtx_unlock(&sfs->mtx);
2249 /* Add the buffer chain to the socket buffer. */
2253 mlen = m_length(m, NULL);
2254 SOCKBUF_LOCK(&so->so_snd);
2255 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2257 SOCKBUF_UNLOCK(&so->so_snd);
2260 SOCKBUF_UNLOCK(&so->so_snd);
2261 CURVNET_SET(so->so_vnet);
2262 /* Avoid error aliasing. */
2263 err = (*so->so_proto->pr_usrreqs->pru_send)
2264 (so, 0, m, NULL, NULL, td);
2268 * We need two counters to get the
2269 * file offset and nbytes to send
2271 * - sbytes contains the total amount
2272 * of bytes sent, including headers.
2273 * - fsbytes contains the total amount
2274 * of bytes sent from the file.
2282 } else if (error == 0)
2284 m = NULL; /* pru_send always consumes */
2287 /* Quit outer loop on error or when we're done. */
2295 * Send trailers. Wimp out and use writev(2).
2297 if (trl_uio != NULL) {
2298 sbunlock(&so->so_snd);
2299 error = kern_writev(td, uap->s, trl_uio);
2301 sbytes += td->td_retval[0];
2306 sbunlock(&so->so_snd);
2309 * If there was no error we have to clear td->td_retval[0]
2310 * because it may have been set by writev.
2313 td->td_retval[0] = 0;
2315 if (uap->sbytes != NULL) {
2316 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2319 vm_object_deallocate(obj);
2328 mtx_lock(&sfs->mtx);
2329 if (sfs->count != 0)
2330 cv_wait(&sfs->cv, &sfs->mtx);
2331 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2332 cv_destroy(&sfs->cv);
2333 mtx_destroy(&sfs->mtx);
2337 if (error == ERESTART)
2345 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2346 * otherwise all return EOPNOTSUPP.
2347 * XXX: We should make this loadable one day.
2350 sys_sctp_peeloff(td, uap)
2352 struct sctp_peeloff_args /* {
2357 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2358 struct file *nfp = NULL;
2360 struct socket *head, *so;
2364 AUDIT_ARG_FD(uap->sd);
2365 error = fgetsock(td, uap->sd, CAP_PEELOFF, &head, &fflag);
2368 if (head->so_proto->pr_protocol != IPPROTO_SCTP) {
2372 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2376 * At this point we know we do have a assoc to pull
2377 * we proceed to get the fd setup. This may block
2381 error = falloc(td, &nfp, &fd, 0);
2384 td->td_retval[0] = fd;
2386 CURVNET_SET(head->so_vnet);
2387 so = sonewconn(head, SS_ISCONNECTED);
2391 * Before changing the flags on the socket, we have to bump the
2392 * reference count. Otherwise, if the protocol calls sofree(),
2393 * the socket will be released due to a zero refcount.
2396 soref(so); /* file descriptor reference */
2401 TAILQ_REMOVE(&head->so_comp, so, so_list);
2403 so->so_state |= (head->so_state & SS_NBIO);
2404 so->so_state &= ~SS_NOFDREF;
2405 so->so_qstate &= ~SQ_COMP;
2408 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2409 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2412 if (head->so_sigio != NULL)
2413 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2417 * close the new descriptor, assuming someone hasn't ripped it
2418 * out from under us.
2421 fdclose(td->td_proc->p_fd, nfp, fd, td);
2424 * Release explicitly held references before returning.
2434 return (EOPNOTSUPP);
2439 sys_sctp_generic_sendmsg (td, uap)
2441 struct sctp_generic_sendmsg_args /* {
2447 struct sctp_sndrcvinfo *sinfo,
2451 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2452 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2454 struct file *fp = NULL;
2456 struct sockaddr *to = NULL;
2458 struct uio *ktruio = NULL;
2461 struct iovec iov[1];
2462 cap_rights_t rights;
2465 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2473 error = getsockaddr(&to, uap->to, uap->tolen);
2478 rights |= CAP_CONNECT;
2481 AUDIT_ARG_FD(uap->sd);
2482 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2486 if (to && (KTRPOINT(td, KTR_STRUCT)))
2490 iov[0].iov_base = uap->msg;
2491 iov[0].iov_len = uap->mlen;
2493 so = (struct socket *)fp->f_data;
2494 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2499 error = mac_socket_check_send(td->td_ucred, so);
2505 auio.uio_iovcnt = 1;
2506 auio.uio_segflg = UIO_USERSPACE;
2507 auio.uio_rw = UIO_WRITE;
2509 auio.uio_offset = 0; /* XXX */
2511 len = auio.uio_resid = uap->mlen;
2512 CURVNET_SET(so->so_vnet);
2513 error = sctp_lower_sosend(so, to, &auio,
2514 (struct mbuf *)NULL, (struct mbuf *)NULL,
2515 uap->flags, u_sinfo, td);
2518 if (auio.uio_resid != len && (error == ERESTART ||
2519 error == EINTR || error == EWOULDBLOCK))
2521 /* Generation of SIGPIPE can be controlled per socket. */
2522 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2523 !(uap->flags & MSG_NOSIGNAL)) {
2524 PROC_LOCK(td->td_proc);
2525 tdsignal(td, SIGPIPE);
2526 PROC_UNLOCK(td->td_proc);
2530 td->td_retval[0] = len - auio.uio_resid;
2532 if (ktruio != NULL) {
2533 ktruio->uio_resid = td->td_retval[0];
2534 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2545 return (EOPNOTSUPP);
2550 sys_sctp_generic_sendmsg_iov(td, uap)
2552 struct sctp_generic_sendmsg_iov_args /* {
2558 struct sctp_sndrcvinfo *sinfo,
2562 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2563 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2565 struct file *fp = NULL;
2568 struct sockaddr *to = NULL;
2570 struct uio *ktruio = NULL;
2573 struct iovec *iov, *tiov;
2574 cap_rights_t rights;
2577 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2584 error = getsockaddr(&to, uap->to, uap->tolen);
2589 rights |= CAP_CONNECT;
2592 AUDIT_ARG_FD(uap->sd);
2593 error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2597 #ifdef COMPAT_FREEBSD32
2598 if (SV_CURPROC_FLAG(SV_ILP32))
2599 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2600 uap->iovlen, &iov, EMSGSIZE);
2603 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2607 if (to && (KTRPOINT(td, KTR_STRUCT)))
2611 so = (struct socket *)fp->f_data;
2612 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2617 error = mac_socket_check_send(td->td_ucred, so);
2623 auio.uio_iovcnt = uap->iovlen;
2624 auio.uio_segflg = UIO_USERSPACE;
2625 auio.uio_rw = UIO_WRITE;
2627 auio.uio_offset = 0; /* XXX */
2630 for (i = 0; i <uap->iovlen; i++, tiov++) {
2631 if ((auio.uio_resid += tiov->iov_len) < 0) {
2636 len = auio.uio_resid;
2637 CURVNET_SET(so->so_vnet);
2638 error = sctp_lower_sosend(so, to, &auio,
2639 (struct mbuf *)NULL, (struct mbuf *)NULL,
2640 uap->flags, u_sinfo, td);
2643 if (auio.uio_resid != len && (error == ERESTART ||
2644 error == EINTR || error == EWOULDBLOCK))
2646 /* Generation of SIGPIPE can be controlled per socket */
2647 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2648 !(uap->flags & MSG_NOSIGNAL)) {
2649 PROC_LOCK(td->td_proc);
2650 tdsignal(td, SIGPIPE);
2651 PROC_UNLOCK(td->td_proc);
2655 td->td_retval[0] = len - auio.uio_resid;
2657 if (ktruio != NULL) {
2658 ktruio->uio_resid = td->td_retval[0];
2659 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2672 return (EOPNOTSUPP);
2677 sys_sctp_generic_recvmsg(td, uap)
2679 struct sctp_generic_recvmsg_args /* {
2683 struct sockaddr *from,
2684 __socklen_t *fromlenaddr,
2685 struct sctp_sndrcvinfo *sinfo,
2689 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2690 uint8_t sockbufstore[256];
2692 struct iovec *iov, *tiov;
2693 struct sctp_sndrcvinfo sinfo;
2695 struct file *fp = NULL;
2696 struct sockaddr *fromsa;
2702 struct uio *ktruio = NULL;
2705 AUDIT_ARG_FD(uap->sd);
2706 error = getsock_cap(td->td_proc->p_fd, uap->sd, CAP_RECV, &fp, NULL);
2710 #ifdef COMPAT_FREEBSD32
2711 if (SV_CURPROC_FLAG(SV_ILP32))
2712 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2713 uap->iovlen, &iov, EMSGSIZE);
2716 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2721 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2726 error = mac_socket_check_receive(td->td_ucred, so);
2732 if (uap->fromlenaddr) {
2733 error = copyin(uap->fromlenaddr,
2734 &fromlen, sizeof (fromlen));
2741 if (uap->msg_flags) {
2742 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2750 auio.uio_iovcnt = uap->iovlen;
2751 auio.uio_segflg = UIO_USERSPACE;
2752 auio.uio_rw = UIO_READ;
2754 auio.uio_offset = 0; /* XXX */
2757 for (i = 0; i <uap->iovlen; i++, tiov++) {
2758 if ((auio.uio_resid += tiov->iov_len) < 0) {
2763 len = auio.uio_resid;
2764 fromsa = (struct sockaddr *)sockbufstore;
2767 if (KTRPOINT(td, KTR_GENIO))
2768 ktruio = cloneuio(&auio);
2770 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
2771 CURVNET_SET(so->so_vnet);
2772 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2773 fromsa, fromlen, &msg_flags,
2774 (struct sctp_sndrcvinfo *)&sinfo, 1);
2777 if (auio.uio_resid != len && (error == ERESTART ||
2778 error == EINTR || error == EWOULDBLOCK))
2782 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2785 if (ktruio != NULL) {
2786 ktruio->uio_resid = len - auio.uio_resid;
2787 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2792 td->td_retval[0] = len - auio.uio_resid;
2794 if (fromlen && uap->from) {
2796 if (len <= 0 || fromsa == 0)
2799 len = MIN(len, fromsa->sa_len);
2800 error = copyout(fromsa, uap->from, (size_t)len);
2804 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2810 if (KTRPOINT(td, KTR_STRUCT))
2811 ktrsockaddr(fromsa);
2813 if (uap->msg_flags) {
2814 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2827 return (EOPNOTSUPP);