2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 #include "opt_capsicum.h"
35 #include "opt_inet6.h"
36 #include "opt_ktrace.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/capsicum.h>
41 #include <sys/kernel.h>
43 #include <sys/mutex.h>
44 #include <sys/sysproto.h>
45 #include <sys/malloc.h>
46 #include <sys/filedesc.h>
48 #include <sys/filio.h>
51 #include <sys/protosw.h>
52 #include <sys/rwlock.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/syscallsubr.h>
57 #include <sys/sysent.h>
61 #include <sys/unpcb.h>
63 #include <sys/ktrace.h>
65 #ifdef COMPAT_FREEBSD32
66 #include <compat/freebsd32/freebsd32_util.h>
71 #include <security/audit/audit.h>
72 #include <security/mac/mac_framework.h>
74 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
75 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
77 static int accept1(struct thread *td, int s, struct sockaddr *uname,
78 socklen_t *anamelen, int flags);
79 static int sockargs(struct mbuf **, char *, socklen_t, int);
82 * Convert a user file descriptor to a kernel file entry and check if required
83 * capability rights are present.
84 * If required copy of current set of capability rights is returned.
85 * A reference on the file entry is held upon returning.
88 getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
89 struct file **fpp, struct filecaps *havecapsp)
94 error = fget_cap(td, fd, rightsp, &fp, havecapsp);
95 if (__predict_false(error != 0))
97 if (__predict_false(fp->f_type != DTYPE_SOCKET)) {
99 if (havecapsp != NULL)
100 filecaps_free(havecapsp);
108 getsock(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
113 error = fget_unlocked(td, fd, rightsp, &fp);
114 if (__predict_false(error != 0))
116 if (__predict_false(fp->f_type != DTYPE_SOCKET)) {
125 * System call interface to the socket abstraction.
127 #if defined(COMPAT_43)
128 #define COMPAT_OLDSOCK
132 sys_socket(struct thread *td, struct socket_args *uap)
135 return (kern_socket(td, uap->domain, uap->type, uap->protocol));
139 kern_socket(struct thread *td, int domain, int type, int protocol)
143 int fd, error, oflag, fflag;
145 AUDIT_ARG_SOCKET(domain, type, protocol);
149 if ((type & SOCK_CLOEXEC) != 0) {
150 type &= ~SOCK_CLOEXEC;
153 if ((type & SOCK_NONBLOCK) != 0) {
154 type &= ~SOCK_NONBLOCK;
159 error = mac_socket_check_create(td->td_ucred, domain, type, protocol);
163 error = falloc(td, &fp, &fd, oflag);
166 /* An extra reference on `fp' has been held for us by falloc(). */
167 error = socreate(domain, &so, type, protocol, td->td_ucred, td);
171 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
172 if ((fflag & FNONBLOCK) != 0)
173 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
174 td->td_retval[0] = fd;
181 sys_bind(struct thread *td, struct bind_args *uap)
186 error = getsockaddr(&sa, uap->name, uap->namelen);
188 error = kern_bindat(td, AT_FDCWD, uap->s, sa);
195 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
201 #ifdef CAPABILITY_MODE
202 if (IN_CAPABILITY_MODE(td) && (dirfd == AT_FDCWD))
207 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
208 error = getsock(td, fd, &cap_bind_rights, &fp);
213 if (KTRPOINT(td, KTR_STRUCT))
217 error = mac_socket_check_bind(td->td_ucred, so, sa);
220 if (dirfd == AT_FDCWD)
221 error = sobind(so, sa, td);
223 error = sobindat(dirfd, so, sa, td);
232 sys_bindat(struct thread *td, struct bindat_args *uap)
237 error = getsockaddr(&sa, uap->name, uap->namelen);
239 error = kern_bindat(td, uap->fd, uap->s, sa);
246 sys_listen(struct thread *td, struct listen_args *uap)
249 return (kern_listen(td, uap->s, uap->backlog));
253 kern_listen(struct thread *td, int s, int backlog)
260 error = getsock(td, s, &cap_listen_rights, &fp);
264 error = mac_socket_check_listen(td->td_ucred, so);
267 error = solisten(so, backlog, td);
277 accept1(struct thread *td, int s, struct sockaddr *uname, socklen_t *anamelen,
280 struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
286 error = copyin(anamelen, &addrlen, sizeof(addrlen));
291 error = kern_accept4(td, s, (struct sockaddr *)&ss, flags, &fp);
296 #ifdef COMPAT_OLDSOCK
297 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) &&
298 (flags & ACCEPT4_COMPAT) != 0)
299 ((struct osockaddr *)&ss)->sa_family = ss.ss_family;
302 addrlen = min(ss.ss_len, addrlen);
303 error = copyout(&ss, uname, addrlen);
306 error = copyout(&addrlen, anamelen, sizeof(addrlen));
310 fdclose(td, fp, td->td_retval[0]);
317 kern_accept(struct thread *td, int s, struct sockaddr *sa, struct file **fp)
319 return (kern_accept4(td, s, sa, ACCEPT4_INHERIT, fp));
323 kern_accept4(struct thread *td, int s, struct sockaddr *sa, int flags,
326 struct file *headfp, *nfp = NULL;
327 struct socket *head, *so;
328 struct filecaps fcaps;
334 error = getsock_cap(td, s, &cap_accept_rights,
338 fflag = atomic_load_int(&headfp->f_flag);
339 head = headfp->f_data;
340 if (!SOLISTENING(head)) {
345 error = mac_socket_check_accept(td->td_ucred, head);
349 error = falloc_caps(td, &nfp, &fd,
350 (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0, &fcaps);
354 if (!SOLISTENING(head)) {
360 error = solisten_dequeue(head, &so, flags);
364 /* An extra reference on `nfp' has been held for us by falloc(). */
365 td->td_retval[0] = fd;
367 /* Connection has been removed from the listen queue. */
368 KNOTE_UNLOCKED(&head->so_rdsel.si_note, 0);
370 if (flags & ACCEPT4_INHERIT) {
371 pgid = fgetown(&head->so_sigio);
373 fsetown(pgid, &so->so_sigio);
375 fflag &= ~(FNONBLOCK | FASYNC);
376 if (flags & SOCK_NONBLOCK)
380 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
381 /* Sync socket nonblocking/async state with file flags */
382 tmp = fflag & FNONBLOCK;
383 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
384 tmp = fflag & FASYNC;
385 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
387 if ((error = soaccept(so, sa)) == 0) {
388 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
390 if (KTRPOINT(td, KTR_STRUCT))
396 * close the new descriptor, assuming someone hasn't ripped it
400 fdclose(td, nfp, fd);
403 * Release explicitly held references before returning. We return
404 * a reference on nfp to the caller on success if they request it.
408 filecaps_free(&fcaps);
423 sys_accept(struct thread *td, struct accept_args *uap)
426 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
430 sys_accept4(struct thread *td, struct accept4_args *uap)
433 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
436 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
439 #ifdef COMPAT_OLDSOCK
441 oaccept(struct thread *td, struct oaccept_args *uap)
444 return (accept1(td, uap->s, uap->name, uap->anamelen,
445 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
447 #endif /* COMPAT_OLDSOCK */
450 sys_connect(struct thread *td, struct connect_args *uap)
455 error = getsockaddr(&sa, uap->name, uap->namelen);
457 error = kern_connectat(td, AT_FDCWD, uap->s, sa);
464 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
470 #ifdef CAPABILITY_MODE
471 if (IN_CAPABILITY_MODE(td) && (dirfd == AT_FDCWD))
476 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
477 error = getsock(td, fd, &cap_connect_rights, &fp);
481 if (so->so_state & SS_ISCONNECTING) {
486 if (KTRPOINT(td, KTR_STRUCT))
490 error = mac_socket_check_connect(td->td_ucred, so, sa);
494 error = soconnectat(dirfd, so, sa, td);
497 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
502 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
503 error = msleep(&so->so_timeo, &so->so_lock, PSOCK | PCATCH,
509 error = so->so_error;
514 if (error == ERESTART)
522 sys_connectat(struct thread *td, struct connectat_args *uap)
527 error = getsockaddr(&sa, uap->name, uap->namelen);
529 error = kern_connectat(td, uap->fd, uap->s, sa);
536 kern_socketpair(struct thread *td, int domain, int type, int protocol,
539 struct file *fp1, *fp2;
540 struct socket *so1, *so2;
541 int fd, error, oflag, fflag;
543 AUDIT_ARG_SOCKET(domain, type, protocol);
547 if ((type & SOCK_CLOEXEC) != 0) {
548 type &= ~SOCK_CLOEXEC;
551 if ((type & SOCK_NONBLOCK) != 0) {
552 type &= ~SOCK_NONBLOCK;
556 /* We might want to have a separate check for socket pairs. */
557 error = mac_socket_check_create(td->td_ucred, domain, type,
562 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
565 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
568 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
569 error = falloc(td, &fp1, &fd, oflag);
573 fp1->f_data = so1; /* so1 already has ref count */
574 error = falloc(td, &fp2, &fd, oflag);
577 fp2->f_data = so2; /* so2 already has ref count */
579 error = soconnect2(so1, so2);
582 if (type == SOCK_DGRAM) {
584 * Datagram socket connection is asymmetric.
586 error = soconnect2(so2, so1);
589 } else if (so1->so_proto->pr_flags & PR_CONNREQUIRED) {
590 struct unpcb *unp, *unp2;
591 unp = sotounpcb(so1);
592 unp2 = sotounpcb(so2);
594 * No need to lock the unps, because the sockets are brand-new.
595 * No other threads can be using them yet
597 unp_copy_peercred(td, unp, unp2, unp);
599 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
601 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
603 if ((fflag & FNONBLOCK) != 0) {
604 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
605 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
611 fdclose(td, fp2, rsv[1]);
614 fdclose(td, fp1, rsv[0]);
626 sys_socketpair(struct thread *td, struct socketpair_args *uap)
630 error = kern_socketpair(td, uap->domain, uap->type,
634 error = copyout(sv, uap->rsv, 2 * sizeof(int));
636 (void)kern_close(td, sv[0]);
637 (void)kern_close(td, sv[1]);
643 sendit(struct thread *td, int s, struct msghdr *mp, int flags)
645 struct mbuf *control;
649 #ifdef CAPABILITY_MODE
650 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
654 if (mp->msg_name != NULL) {
655 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
665 if (mp->msg_control) {
666 if (mp->msg_controllen < sizeof(struct cmsghdr)
667 #ifdef COMPAT_OLDSOCK
668 && (mp->msg_flags != MSG_COMPAT ||
669 !SV_PROC_FLAG(td->td_proc, SV_AOUT))
675 error = sockargs(&control, mp->msg_control,
676 mp->msg_controllen, MT_CONTROL);
679 #ifdef COMPAT_OLDSOCK
680 if (mp->msg_flags == MSG_COMPAT &&
681 SV_PROC_FLAG(td->td_proc, SV_AOUT)) {
684 M_PREPEND(control, sizeof(*cm), M_WAITOK);
685 cm = mtod(control, struct cmsghdr *);
686 cm->cmsg_len = control->m_len;
687 cm->cmsg_level = SOL_SOCKET;
688 cm->cmsg_type = SCM_RIGHTS;
695 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
703 kern_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
704 struct mbuf *control, enum uio_seg segflg)
710 cap_rights_t *rights;
712 struct uio *ktruio = NULL;
718 rights = &cap_send_rights;
719 if (mp->msg_name != NULL) {
720 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
721 rights = &cap_send_connect_rights;
723 error = getsock(td, s, rights, &fp);
728 so = (struct socket *)fp->f_data;
731 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
732 ktrsockaddr(mp->msg_name);
735 if (mp->msg_name != NULL) {
736 error = mac_socket_check_connect(td->td_ucred, so,
743 error = mac_socket_check_send(td->td_ucred, so);
750 auio.uio_iov = mp->msg_iov;
751 auio.uio_iovcnt = mp->msg_iovlen;
752 auio.uio_segflg = segflg;
753 auio.uio_rw = UIO_WRITE;
755 auio.uio_offset = 0; /* XXX */
758 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
759 if ((auio.uio_resid += iov->iov_len) < 0) {
766 if (KTRPOINT(td, KTR_GENIO))
767 ktruio = cloneuio(&auio);
769 len = auio.uio_resid;
770 error = sousrsend(so, mp->msg_name, &auio, control, flags, NULL);
772 td->td_retval[0] = len - auio.uio_resid;
774 if (ktruio != NULL) {
776 ktruio->uio_resid = td->td_retval[0];
777 ktrgenio(s, UIO_WRITE, ktruio, error);
786 sys_sendto(struct thread *td, struct sendto_args *uap)
791 msg.msg_name = __DECONST(void *, uap->to);
792 msg.msg_namelen = uap->tolen;
796 #ifdef COMPAT_OLDSOCK
797 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
800 aiov.iov_base = __DECONST(void *, uap->buf);
801 aiov.iov_len = uap->len;
802 return (sendit(td, uap->s, &msg, uap->flags));
805 #ifdef COMPAT_OLDSOCK
807 osend(struct thread *td, struct osend_args *uap)
816 aiov.iov_base = __DECONST(void *, uap->buf);
817 aiov.iov_len = uap->len;
820 return (sendit(td, uap->s, &msg, uap->flags));
824 osendmsg(struct thread *td, struct osendmsg_args *uap)
830 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
833 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
837 msg.msg_flags = MSG_COMPAT;
838 error = sendit(td, uap->s, &msg, uap->flags);
845 sys_sendmsg(struct thread *td, struct sendmsg_args *uap)
851 error = copyin(uap->msg, &msg, sizeof (msg));
854 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
858 #ifdef COMPAT_OLDSOCK
859 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
862 error = sendit(td, uap->s, &msg, uap->flags);
868 kern_recvit(struct thread *td, int s, struct msghdr *mp, enum uio_seg fromseg,
869 struct mbuf **controlp)
873 struct mbuf *control, *m;
877 struct sockaddr *fromsa = NULL;
879 struct uio *ktruio = NULL;
884 if (controlp != NULL)
888 error = getsock(td, s, &cap_recv_rights, &fp);
894 error = mac_socket_check_receive(td->td_ucred, so);
901 auio.uio_iov = mp->msg_iov;
902 auio.uio_iovcnt = mp->msg_iovlen;
903 auio.uio_segflg = UIO_USERSPACE;
904 auio.uio_rw = UIO_READ;
906 auio.uio_offset = 0; /* XXX */
909 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
910 if ((auio.uio_resid += iov->iov_len) < 0) {
916 if (KTRPOINT(td, KTR_GENIO))
917 ktruio = cloneuio(&auio);
920 len = auio.uio_resid;
921 error = soreceive(so, &fromsa, &auio, NULL,
922 (mp->msg_control || controlp) ? &control : NULL,
925 if (auio.uio_resid != len && (error == ERESTART ||
926 error == EINTR || error == EWOULDBLOCK))
930 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
932 if (ktruio != NULL) {
933 /* MSG_TRUNC can trigger underflow of uio_resid. */
934 ktruio->uio_resid = MIN(len - auio.uio_resid, len);
935 ktrgenio(s, UIO_READ, ktruio, error);
940 td->td_retval[0] = len - auio.uio_resid;
942 len = mp->msg_namelen;
943 if (len <= 0 || fromsa == NULL)
946 /* save sa_len before it is destroyed by MSG_COMPAT */
947 len = MIN(len, fromsa->sa_len);
948 #ifdef COMPAT_OLDSOCK
949 if ((mp->msg_flags & MSG_COMPAT) != 0 &&
950 SV_PROC_FLAG(td->td_proc, SV_AOUT))
951 ((struct osockaddr *)fromsa)->sa_family =
954 if (fromseg == UIO_USERSPACE) {
955 error = copyout(fromsa, mp->msg_name,
960 bcopy(fromsa, mp->msg_name, len);
962 mp->msg_namelen = len;
964 if (mp->msg_control && controlp == NULL) {
965 #ifdef COMPAT_OLDSOCK
967 * We assume that old recvmsg calls won't receive access
968 * rights and other control info, esp. as control info
969 * is always optional and those options didn't exist in 4.3.
970 * If we receive rights, trim the cmsghdr; anything else
973 if (control && (mp->msg_flags & MSG_COMPAT) != 0 &&
974 SV_PROC_FLAG(td->td_proc, SV_AOUT)) {
975 if (mtod(control, struct cmsghdr *)->cmsg_level !=
977 mtod(control, struct cmsghdr *)->cmsg_type !=
979 mp->msg_controllen = 0;
982 control->m_len -= sizeof (struct cmsghdr);
983 control->m_data += sizeof (struct cmsghdr);
986 ctlbuf = mp->msg_control;
987 len = mp->msg_controllen;
988 mp->msg_controllen = 0;
989 for (m = control; m != NULL && len >= m->m_len; m = m->m_next) {
990 if ((error = copyout(mtod(m, caddr_t), ctlbuf,
996 mp->msg_controllen += m->m_len;
999 mp->msg_flags |= MSG_CTRUNC;
1000 m_dispose_extcontrolm(m);
1006 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1007 ktrsockaddr(fromsa);
1009 free(fromsa, M_SONAME);
1011 if (error == 0 && controlp != NULL)
1012 *controlp = control;
1013 else if (control != NULL) {
1015 m_dispose_extcontrolm(control);
1023 recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp)
1027 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1030 if (namelenp != NULL) {
1031 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1032 #ifdef COMPAT_OLDSOCK
1033 if ((mp->msg_flags & MSG_COMPAT) != 0 &&
1034 SV_PROC_FLAG(td->td_proc, SV_AOUT))
1035 error = 0; /* old recvfrom didn't check */
1042 kern_recvfrom(struct thread *td, int s, void *buf, size_t len, int flags,
1043 struct sockaddr *from, socklen_t *fromlenaddr)
1049 if (fromlenaddr != NULL) {
1050 error = copyin(fromlenaddr, &msg.msg_namelen,
1051 sizeof (msg.msg_namelen));
1055 msg.msg_namelen = 0;
1057 msg.msg_name = from;
1058 msg.msg_iov = &aiov;
1060 aiov.iov_base = buf;
1062 msg.msg_control = 0;
1063 msg.msg_flags = flags;
1064 error = recvit(td, s, &msg, fromlenaddr);
1070 sys_recvfrom(struct thread *td, struct recvfrom_args *uap)
1072 return (kern_recvfrom(td, uap->s, uap->buf, uap->len,
1073 uap->flags, uap->from, uap->fromlenaddr));
1077 #ifdef COMPAT_OLDSOCK
1079 orecvfrom(struct thread *td, struct orecvfrom_args *uap)
1081 return (kern_recvfrom(td, uap->s, uap->buf, uap->len,
1082 uap->flags | MSG_COMPAT, uap->from, uap->fromlenaddr));
1086 #ifdef COMPAT_OLDSOCK
1088 orecv(struct thread *td, struct orecv_args *uap)
1094 msg.msg_namelen = 0;
1095 msg.msg_iov = &aiov;
1097 aiov.iov_base = uap->buf;
1098 aiov.iov_len = uap->len;
1099 msg.msg_control = 0;
1100 msg.msg_flags = uap->flags;
1101 return (recvit(td, uap->s, &msg, NULL));
1105 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1106 * overlays the new one, missing only the flags, and with the (old) access
1107 * rights where the control fields are now.
1110 orecvmsg(struct thread *td, struct orecvmsg_args *uap)
1116 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1119 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1122 msg.msg_flags = uap->flags | MSG_COMPAT;
1124 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1125 if (msg.msg_controllen && error == 0)
1126 error = copyout(&msg.msg_controllen,
1127 &uap->msg->msg_accrightslen, sizeof (int));
1134 sys_recvmsg(struct thread *td, struct recvmsg_args *uap)
1137 struct iovec *uiov, *iov;
1140 error = copyin(uap->msg, &msg, sizeof (msg));
1143 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1146 msg.msg_flags = uap->flags;
1147 #ifdef COMPAT_OLDSOCK
1148 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
1149 msg.msg_flags &= ~MSG_COMPAT;
1153 error = recvit(td, uap->s, &msg, NULL);
1156 error = copyout(&msg, uap->msg, sizeof(msg));
1163 sys_shutdown(struct thread *td, struct shutdown_args *uap)
1166 return (kern_shutdown(td, uap->s, uap->how));
1170 kern_shutdown(struct thread *td, int s, int how)
1176 if (__predict_false(how < SHUT_RD || how > SHUT_RDWR))
1180 error = getsock(td, s, &cap_shutdown_rights, &fp);
1183 error = soshutdown(so, how);
1185 * Previous versions did not return ENOTCONN, but 0 in
1186 * case the socket was not connected. Some important
1187 * programs like syslogd up to r279016, 2015-02-19,
1188 * still depend on this behavior.
1190 if (error == ENOTCONN &&
1191 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1199 sys_setsockopt(struct thread *td, struct setsockopt_args *uap)
1202 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1203 uap->val, UIO_USERSPACE, uap->valsize));
1207 kern_setsockopt(struct thread *td, int s, int level, int name, const void *val,
1208 enum uio_seg valseg, socklen_t valsize)
1212 struct sockopt sopt;
1215 if (val == NULL && valsize != 0)
1217 if ((int)valsize < 0)
1220 sopt.sopt_dir = SOPT_SET;
1221 sopt.sopt_level = level;
1222 sopt.sopt_name = name;
1223 sopt.sopt_val = __DECONST(void *, val);
1224 sopt.sopt_valsize = valsize;
1230 sopt.sopt_td = NULL;
1233 panic("kern_setsockopt called with bad valseg");
1237 error = getsock(td, s, &cap_setsockopt_rights, &fp);
1240 error = sosetopt(so, &sopt);
1247 sys_getsockopt(struct thread *td, struct getsockopt_args *uap)
1253 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1258 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1259 uap->val, UIO_USERSPACE, &valsize);
1262 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1267 * Kernel version of getsockopt.
1268 * optval can be a userland or userspace. optlen is always a kernel pointer.
1271 kern_getsockopt(struct thread *td, int s, int level, int name, void *val,
1272 enum uio_seg valseg, socklen_t *valsize)
1276 struct sockopt sopt;
1281 if ((int)*valsize < 0)
1284 sopt.sopt_dir = SOPT_GET;
1285 sopt.sopt_level = level;
1286 sopt.sopt_name = name;
1287 sopt.sopt_val = val;
1288 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1294 sopt.sopt_td = NULL;
1297 panic("kern_getsockopt called with bad valseg");
1301 error = getsock(td, s, &cap_getsockopt_rights, &fp);
1304 error = sogetopt(so, &sopt);
1305 *valsize = sopt.sopt_valsize;
1312 user_getsockname(struct thread *td, int fdes, struct sockaddr *asa,
1313 socklen_t *alen, bool compat)
1315 struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
1319 error = copyin(alen, &len, sizeof(len));
1323 error = kern_getsockname(td, fdes, (struct sockaddr *)&ss);
1327 #ifdef COMPAT_OLDSOCK
1328 if (compat && SV_PROC_FLAG(td->td_proc, SV_AOUT))
1329 ((struct osockaddr *)&ss)->sa_family = ss.ss_family;
1331 len = min(ss.ss_len, len);
1332 error = copyout(&ss, asa, len);
1335 error = copyout(&len, alen, sizeof(len));
1341 kern_getsockname(struct thread *td, int fd, struct sockaddr *sa)
1348 error = getsock(td, fd, &cap_getsockname_rights, &fp);
1352 error = sosockaddr(so, sa);
1354 if (error == 0 && KTRPOINT(td, KTR_STRUCT))
1362 sys_getsockname(struct thread *td, struct getsockname_args *uap)
1364 return (user_getsockname(td, uap->fdes, uap->asa, uap->alen, false));
1367 #ifdef COMPAT_OLDSOCK
1369 ogetsockname(struct thread *td, struct ogetsockname_args *uap)
1371 return (user_getsockname(td, uap->fdes, uap->asa, uap->alen, true));
1373 #endif /* COMPAT_OLDSOCK */
1376 user_getpeername(struct thread *td, int fdes, struct sockaddr *asa,
1377 socklen_t *alen, bool compat)
1379 struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
1383 error = copyin(alen, &len, sizeof (len));
1387 error = kern_getpeername(td, fdes, (struct sockaddr *)&ss);
1391 #ifdef COMPAT_OLDSOCK
1392 if (compat && SV_PROC_FLAG(td->td_proc, SV_AOUT))
1393 ((struct osockaddr *)&ss)->sa_family = ss.ss_family;
1395 len = min(ss.ss_len, len);
1396 error = copyout(&ss, asa, len);
1399 error = copyout(&len, alen, sizeof(len));
1405 kern_getpeername(struct thread *td, int fd, struct sockaddr *sa)
1412 error = getsock(td, fd, &cap_getpeername_rights, &fp);
1416 if ((so->so_state & SS_ISCONNECTED) == 0) {
1420 error = sopeeraddr(so, sa);
1422 if (error == 0 && KTRPOINT(td, KTR_STRUCT))
1431 sys_getpeername(struct thread *td, struct getpeername_args *uap)
1433 return (user_getpeername(td, uap->fdes, uap->asa, uap->alen, false));
1436 #ifdef COMPAT_OLDSOCK
1438 ogetpeername(struct thread *td, struct ogetpeername_args *uap)
1440 return (user_getpeername(td, uap->fdes, uap->asa, uap->alen, true));
1442 #endif /* COMPAT_OLDSOCK */
1445 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1447 struct sockaddr *sa;
1451 if (buflen > MLEN) {
1452 #ifdef COMPAT_OLDSOCK
1453 if (type == MT_SONAME && buflen <= 112 &&
1454 SV_CURPROC_FLAG(SV_AOUT))
1455 buflen = MLEN; /* unix domain compat. hack */
1458 if (buflen > MCLBYTES)
1461 m = m_get2(buflen, M_WAITOK, type, 0);
1463 error = copyin(buf, mtod(m, void *), buflen);
1468 if (type == MT_SONAME) {
1469 sa = mtod(m, struct sockaddr *);
1471 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1472 if (sa->sa_family == 0 && sa->sa_len < AF_MAX &&
1473 SV_CURPROC_FLAG(SV_AOUT))
1474 sa->sa_family = sa->sa_len;
1476 sa->sa_len = buflen;
1483 getsockaddr(struct sockaddr **namp, const struct sockaddr *uaddr, size_t len)
1485 struct sockaddr *sa;
1488 if (len > SOCK_MAXADDRLEN)
1489 return (ENAMETOOLONG);
1490 if (len < offsetof(struct sockaddr, sa_data[0]))
1492 sa = malloc(len, M_SONAME, M_WAITOK);
1493 error = copyin(uaddr, sa, len);
1497 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1498 if (sa->sa_family == 0 && sa->sa_len < AF_MAX &&
1499 SV_CURPROC_FLAG(SV_AOUT))
1500 sa->sa_family = sa->sa_len;
1509 * Dispose of externalized rights from an SCM_RIGHTS message. This function
1510 * should be used in error or truncation cases to avoid leaking file descriptors
1511 * into the recipient's (the current thread's) table.
1514 m_dispose_extcontrolm(struct mbuf *m)
1519 socklen_t clen, datalen;
1520 int error, fd, *fds, nfd;
1523 for (; m != NULL; m = m->m_next) {
1524 if (m->m_type != MT_EXTCONTROL)
1526 cm = mtod(m, struct cmsghdr *);
1529 if (clen < sizeof(*cm))
1530 panic("%s: truncated mbuf %p", __func__, m);
1531 datalen = CMSG_SPACE(cm->cmsg_len - CMSG_SPACE(0));
1533 panic("%s: truncated mbuf %p", __func__, m);
1535 if (cm->cmsg_level == SOL_SOCKET &&
1536 cm->cmsg_type == SCM_RIGHTS) {
1537 fds = (int *)CMSG_DATA(cm);
1538 nfd = (cm->cmsg_len - CMSG_SPACE(0)) /
1543 error = fget(td, fd, &cap_no_rights,
1546 fdclose(td, fp, fd);
1552 cm = (struct cmsghdr *)((uint8_t *)cm + datalen);
1554 m_chtype(m, MT_CONTROL);