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 (dirfd == AT_FDCWD) {
204 ktrcapfail(CAPFAIL_NAMEI, "AT_FDCWD");
205 if (IN_CAPABILITY_MODE(td))
211 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
212 error = getsock(td, fd, &cap_bind_rights, &fp);
217 if (KTRPOINT(td, KTR_STRUCT))
221 error = mac_socket_check_bind(td->td_ucred, so, sa);
224 if (dirfd == AT_FDCWD)
225 error = sobind(so, sa, td);
227 error = sobindat(dirfd, so, sa, td);
236 sys_bindat(struct thread *td, struct bindat_args *uap)
241 error = getsockaddr(&sa, uap->name, uap->namelen);
243 error = kern_bindat(td, uap->fd, uap->s, sa);
250 sys_listen(struct thread *td, struct listen_args *uap)
253 return (kern_listen(td, uap->s, uap->backlog));
257 kern_listen(struct thread *td, int s, int backlog)
264 error = getsock(td, s, &cap_listen_rights, &fp);
268 error = mac_socket_check_listen(td->td_ucred, so);
271 error = solisten(so, backlog, td);
281 accept1(struct thread *td, int s, struct sockaddr *uname, socklen_t *anamelen,
284 struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
290 error = copyin(anamelen, &addrlen, sizeof(addrlen));
295 error = kern_accept4(td, s, (struct sockaddr *)&ss, flags, &fp);
300 #ifdef COMPAT_OLDSOCK
301 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) &&
302 (flags & ACCEPT4_COMPAT) != 0)
303 ((struct osockaddr *)&ss)->sa_family = ss.ss_family;
306 addrlen = min(ss.ss_len, addrlen);
307 error = copyout(&ss, uname, addrlen);
310 error = copyout(&addrlen, anamelen, sizeof(addrlen));
314 fdclose(td, fp, td->td_retval[0]);
321 kern_accept(struct thread *td, int s, struct sockaddr *sa, struct file **fp)
323 return (kern_accept4(td, s, sa, ACCEPT4_INHERIT, fp));
327 kern_accept4(struct thread *td, int s, struct sockaddr *sa, int flags,
330 struct file *headfp, *nfp = NULL;
331 struct socket *head, *so;
332 struct filecaps fcaps;
338 error = getsock_cap(td, s, &cap_accept_rights,
342 fflag = atomic_load_int(&headfp->f_flag);
343 head = headfp->f_data;
344 if (!SOLISTENING(head)) {
349 error = mac_socket_check_accept(td->td_ucred, head);
353 error = falloc_caps(td, &nfp, &fd,
354 (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0, &fcaps);
358 if (!SOLISTENING(head)) {
364 error = solisten_dequeue(head, &so, flags);
368 /* An extra reference on `nfp' has been held for us by falloc(). */
369 td->td_retval[0] = fd;
371 /* Connection has been removed from the listen queue. */
372 KNOTE_UNLOCKED(&head->so_rdsel.si_note, 0);
374 if (flags & ACCEPT4_INHERIT) {
375 pgid = fgetown(&head->so_sigio);
377 fsetown(pgid, &so->so_sigio);
379 fflag &= ~(FNONBLOCK | FASYNC);
380 if (flags & SOCK_NONBLOCK)
384 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
385 /* Sync socket nonblocking/async state with file flags */
386 tmp = fflag & FNONBLOCK;
387 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
388 tmp = fflag & FASYNC;
389 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
391 if ((error = soaccept(so, sa)) == 0) {
392 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
394 if (KTRPOINT(td, KTR_STRUCT))
400 * close the new descriptor, assuming someone hasn't ripped it
404 fdclose(td, nfp, fd);
407 * Release explicitly held references before returning. We return
408 * a reference on nfp to the caller on success if they request it.
412 filecaps_free(&fcaps);
427 sys_accept(struct thread *td, struct accept_args *uap)
430 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
434 sys_accept4(struct thread *td, struct accept4_args *uap)
437 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
440 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
443 #ifdef COMPAT_OLDSOCK
445 oaccept(struct thread *td, struct oaccept_args *uap)
448 return (accept1(td, uap->s, uap->name, uap->anamelen,
449 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
451 #endif /* COMPAT_OLDSOCK */
454 sys_connect(struct thread *td, struct connect_args *uap)
459 error = getsockaddr(&sa, uap->name, uap->namelen);
461 error = kern_connectat(td, AT_FDCWD, uap->s, sa);
468 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
474 #ifdef CAPABILITY_MODE
475 if (dirfd == AT_FDCWD) {
477 ktrcapfail(CAPFAIL_NAMEI, "AT_FDCWD");
478 if (IN_CAPABILITY_MODE(td))
484 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
485 error = getsock(td, fd, &cap_connect_rights, &fp);
489 if (so->so_state & SS_ISCONNECTING) {
494 if (KTRPOINT(td, KTR_STRUCT))
498 error = mac_socket_check_connect(td->td_ucred, so, sa);
502 error = soconnectat(dirfd, so, sa, td);
505 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
510 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
511 error = msleep(&so->so_timeo, &so->so_lock, PSOCK | PCATCH,
517 error = so->so_error;
522 if (error == ERESTART)
530 sys_connectat(struct thread *td, struct connectat_args *uap)
535 error = getsockaddr(&sa, uap->name, uap->namelen);
537 error = kern_connectat(td, uap->fd, uap->s, sa);
544 kern_socketpair(struct thread *td, int domain, int type, int protocol,
547 struct file *fp1, *fp2;
548 struct socket *so1, *so2;
549 int fd, error, oflag, fflag;
551 AUDIT_ARG_SOCKET(domain, type, protocol);
555 if ((type & SOCK_CLOEXEC) != 0) {
556 type &= ~SOCK_CLOEXEC;
559 if ((type & SOCK_NONBLOCK) != 0) {
560 type &= ~SOCK_NONBLOCK;
564 /* We might want to have a separate check for socket pairs. */
565 error = mac_socket_check_create(td->td_ucred, domain, type,
570 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
573 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
576 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
577 error = falloc(td, &fp1, &fd, oflag);
581 fp1->f_data = so1; /* so1 already has ref count */
582 error = falloc(td, &fp2, &fd, oflag);
585 fp2->f_data = so2; /* so2 already has ref count */
587 error = soconnect2(so1, so2);
590 if (type == SOCK_DGRAM) {
592 * Datagram socket connection is asymmetric.
594 error = soconnect2(so2, so1);
597 } else if (so1->so_proto->pr_flags & PR_CONNREQUIRED) {
598 struct unpcb *unp, *unp2;
599 unp = sotounpcb(so1);
600 unp2 = sotounpcb(so2);
602 * No need to lock the unps, because the sockets are brand-new.
603 * No other threads can be using them yet
605 unp_copy_peercred(td, unp, unp2, unp);
607 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
609 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
611 if ((fflag & FNONBLOCK) != 0) {
612 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
613 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
619 fdclose(td, fp2, rsv[1]);
622 fdclose(td, fp1, rsv[0]);
634 sys_socketpair(struct thread *td, struct socketpair_args *uap)
638 error = kern_socketpair(td, uap->domain, uap->type,
642 error = copyout(sv, uap->rsv, 2 * sizeof(int));
644 (void)kern_close(td, sv[0]);
645 (void)kern_close(td, sv[1]);
651 sendit(struct thread *td, int s, struct msghdr *mp, int flags)
653 struct mbuf *control;
657 if (mp->msg_name != NULL) {
658 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
664 #ifdef CAPABILITY_MODE
666 ktrcapfail(CAPFAIL_SOCKADDR, mp->msg_name);
667 if (IN_CAPABILITY_MODE(td)) {
676 if (mp->msg_control) {
677 if (mp->msg_controllen < sizeof(struct cmsghdr)
678 #ifdef COMPAT_OLDSOCK
679 && (mp->msg_flags != MSG_COMPAT ||
680 !SV_PROC_FLAG(td->td_proc, SV_AOUT))
686 error = sockargs(&control, mp->msg_control,
687 mp->msg_controllen, MT_CONTROL);
690 #ifdef COMPAT_OLDSOCK
691 if (mp->msg_flags == MSG_COMPAT &&
692 SV_PROC_FLAG(td->td_proc, SV_AOUT)) {
695 M_PREPEND(control, sizeof(*cm), M_WAITOK);
696 cm = mtod(control, struct cmsghdr *);
697 cm->cmsg_len = control->m_len;
698 cm->cmsg_level = SOL_SOCKET;
699 cm->cmsg_type = SCM_RIGHTS;
706 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
714 kern_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
715 struct mbuf *control, enum uio_seg segflg)
721 cap_rights_t *rights;
723 struct uio *ktruio = NULL;
729 rights = &cap_send_rights;
730 if (mp->msg_name != NULL) {
731 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
732 rights = &cap_send_connect_rights;
734 error = getsock(td, s, rights, &fp);
739 so = (struct socket *)fp->f_data;
742 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
743 ktrsockaddr(mp->msg_name);
746 if (mp->msg_name != NULL) {
747 error = mac_socket_check_connect(td->td_ucred, so,
754 error = mac_socket_check_send(td->td_ucred, so);
761 auio.uio_iov = mp->msg_iov;
762 auio.uio_iovcnt = mp->msg_iovlen;
763 auio.uio_segflg = segflg;
764 auio.uio_rw = UIO_WRITE;
766 auio.uio_offset = 0; /* XXX */
769 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
770 if ((auio.uio_resid += iov->iov_len) < 0) {
777 if (KTRPOINT(td, KTR_GENIO))
778 ktruio = cloneuio(&auio);
780 len = auio.uio_resid;
781 error = sousrsend(so, mp->msg_name, &auio, control, flags, NULL);
783 td->td_retval[0] = len - auio.uio_resid;
785 if (ktruio != NULL) {
787 ktruio->uio_resid = td->td_retval[0];
788 ktrgenio(s, UIO_WRITE, ktruio, error);
797 sys_sendto(struct thread *td, struct sendto_args *uap)
802 msg.msg_name = __DECONST(void *, uap->to);
803 msg.msg_namelen = uap->tolen;
807 #ifdef COMPAT_OLDSOCK
808 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
811 aiov.iov_base = __DECONST(void *, uap->buf);
812 aiov.iov_len = uap->len;
813 return (sendit(td, uap->s, &msg, uap->flags));
816 #ifdef COMPAT_OLDSOCK
818 osend(struct thread *td, struct osend_args *uap)
827 aiov.iov_base = __DECONST(void *, uap->buf);
828 aiov.iov_len = uap->len;
831 return (sendit(td, uap->s, &msg, uap->flags));
835 osendmsg(struct thread *td, struct osendmsg_args *uap)
841 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
844 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
848 msg.msg_flags = MSG_COMPAT;
849 error = sendit(td, uap->s, &msg, uap->flags);
856 sys_sendmsg(struct thread *td, struct sendmsg_args *uap)
862 error = copyin(uap->msg, &msg, sizeof (msg));
865 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
869 #ifdef COMPAT_OLDSOCK
870 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
873 error = sendit(td, uap->s, &msg, uap->flags);
879 kern_recvit(struct thread *td, int s, struct msghdr *mp, enum uio_seg fromseg,
880 struct mbuf **controlp)
884 struct mbuf *control, *m;
888 struct sockaddr *fromsa = NULL;
890 struct uio *ktruio = NULL;
895 if (controlp != NULL)
899 error = getsock(td, s, &cap_recv_rights, &fp);
905 error = mac_socket_check_receive(td->td_ucred, so);
912 auio.uio_iov = mp->msg_iov;
913 auio.uio_iovcnt = mp->msg_iovlen;
914 auio.uio_segflg = UIO_USERSPACE;
915 auio.uio_rw = UIO_READ;
917 auio.uio_offset = 0; /* XXX */
920 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
921 if ((auio.uio_resid += iov->iov_len) < 0) {
927 if (KTRPOINT(td, KTR_GENIO))
928 ktruio = cloneuio(&auio);
931 len = auio.uio_resid;
932 error = soreceive(so, &fromsa, &auio, NULL,
933 (mp->msg_control || controlp) ? &control : NULL,
936 if (auio.uio_resid != len && (error == ERESTART ||
937 error == EINTR || error == EWOULDBLOCK))
941 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
943 if (ktruio != NULL) {
944 /* MSG_TRUNC can trigger underflow of uio_resid. */
945 ktruio->uio_resid = MIN(len - auio.uio_resid, len);
946 ktrgenio(s, UIO_READ, ktruio, error);
951 td->td_retval[0] = len - auio.uio_resid;
953 len = mp->msg_namelen;
954 if (len <= 0 || fromsa == NULL)
957 /* save sa_len before it is destroyed by MSG_COMPAT */
958 len = MIN(len, fromsa->sa_len);
959 #ifdef COMPAT_OLDSOCK
960 if ((mp->msg_flags & MSG_COMPAT) != 0 &&
961 SV_PROC_FLAG(td->td_proc, SV_AOUT))
962 ((struct osockaddr *)fromsa)->sa_family =
965 if (fromseg == UIO_USERSPACE) {
966 error = copyout(fromsa, mp->msg_name,
971 bcopy(fromsa, mp->msg_name, len);
973 mp->msg_namelen = len;
975 if (mp->msg_control && controlp == NULL) {
976 #ifdef COMPAT_OLDSOCK
978 * We assume that old recvmsg calls won't receive access
979 * rights and other control info, esp. as control info
980 * is always optional and those options didn't exist in 4.3.
981 * If we receive rights, trim the cmsghdr; anything else
984 if (control && (mp->msg_flags & MSG_COMPAT) != 0 &&
985 SV_PROC_FLAG(td->td_proc, SV_AOUT)) {
986 if (mtod(control, struct cmsghdr *)->cmsg_level !=
988 mtod(control, struct cmsghdr *)->cmsg_type !=
990 mp->msg_controllen = 0;
993 control->m_len -= sizeof (struct cmsghdr);
994 control->m_data += sizeof (struct cmsghdr);
997 ctlbuf = mp->msg_control;
998 len = mp->msg_controllen;
999 mp->msg_controllen = 0;
1000 for (m = control; m != NULL && len >= m->m_len; m = m->m_next) {
1001 if ((error = copyout(mtod(m, caddr_t), ctlbuf,
1007 mp->msg_controllen += m->m_len;
1010 mp->msg_flags |= MSG_CTRUNC;
1011 m_dispose_extcontrolm(m);
1017 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1018 ktrsockaddr(fromsa);
1020 free(fromsa, M_SONAME);
1022 if (error == 0 && controlp != NULL)
1023 *controlp = control;
1024 else if (control != NULL) {
1026 m_dispose_extcontrolm(control);
1034 recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp)
1038 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1041 if (namelenp != NULL) {
1042 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1043 #ifdef COMPAT_OLDSOCK
1044 if ((mp->msg_flags & MSG_COMPAT) != 0 &&
1045 SV_PROC_FLAG(td->td_proc, SV_AOUT))
1046 error = 0; /* old recvfrom didn't check */
1053 kern_recvfrom(struct thread *td, int s, void *buf, size_t len, int flags,
1054 struct sockaddr *from, socklen_t *fromlenaddr)
1060 if (fromlenaddr != NULL) {
1061 error = copyin(fromlenaddr, &msg.msg_namelen,
1062 sizeof (msg.msg_namelen));
1066 msg.msg_namelen = 0;
1068 msg.msg_name = from;
1069 msg.msg_iov = &aiov;
1071 aiov.iov_base = buf;
1073 msg.msg_control = 0;
1074 msg.msg_flags = flags;
1075 error = recvit(td, s, &msg, fromlenaddr);
1081 sys_recvfrom(struct thread *td, struct recvfrom_args *uap)
1083 return (kern_recvfrom(td, uap->s, uap->buf, uap->len,
1084 uap->flags, uap->from, uap->fromlenaddr));
1088 #ifdef COMPAT_OLDSOCK
1090 orecvfrom(struct thread *td, struct orecvfrom_args *uap)
1092 return (kern_recvfrom(td, uap->s, uap->buf, uap->len,
1093 uap->flags | MSG_COMPAT, uap->from, uap->fromlenaddr));
1097 #ifdef COMPAT_OLDSOCK
1099 orecv(struct thread *td, struct orecv_args *uap)
1105 msg.msg_namelen = 0;
1106 msg.msg_iov = &aiov;
1108 aiov.iov_base = uap->buf;
1109 aiov.iov_len = uap->len;
1110 msg.msg_control = 0;
1111 msg.msg_flags = uap->flags;
1112 return (recvit(td, uap->s, &msg, NULL));
1116 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1117 * overlays the new one, missing only the flags, and with the (old) access
1118 * rights where the control fields are now.
1121 orecvmsg(struct thread *td, struct orecvmsg_args *uap)
1127 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1130 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1133 msg.msg_flags = uap->flags | MSG_COMPAT;
1135 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1136 if (msg.msg_controllen && error == 0)
1137 error = copyout(&msg.msg_controllen,
1138 &uap->msg->msg_accrightslen, sizeof (int));
1145 sys_recvmsg(struct thread *td, struct recvmsg_args *uap)
1148 struct iovec *uiov, *iov;
1151 error = copyin(uap->msg, &msg, sizeof (msg));
1154 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1157 msg.msg_flags = uap->flags;
1158 #ifdef COMPAT_OLDSOCK
1159 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
1160 msg.msg_flags &= ~MSG_COMPAT;
1164 error = recvit(td, uap->s, &msg, NULL);
1167 error = copyout(&msg, uap->msg, sizeof(msg));
1174 sys_shutdown(struct thread *td, struct shutdown_args *uap)
1177 return (kern_shutdown(td, uap->s, uap->how));
1181 kern_shutdown(struct thread *td, int s, int how)
1187 if (__predict_false(how < SHUT_RD || how > SHUT_RDWR))
1191 error = getsock(td, s, &cap_shutdown_rights, &fp);
1194 error = soshutdown(so, how);
1196 * Previous versions did not return ENOTCONN, but 0 in
1197 * case the socket was not connected. Some important
1198 * programs like syslogd up to r279016, 2015-02-19,
1199 * still depend on this behavior.
1201 if (error == ENOTCONN &&
1202 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1210 sys_setsockopt(struct thread *td, struct setsockopt_args *uap)
1213 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1214 uap->val, UIO_USERSPACE, uap->valsize));
1218 kern_setsockopt(struct thread *td, int s, int level, int name, const void *val,
1219 enum uio_seg valseg, socklen_t valsize)
1223 struct sockopt sopt;
1226 if (val == NULL && valsize != 0)
1228 if ((int)valsize < 0)
1231 sopt.sopt_dir = SOPT_SET;
1232 sopt.sopt_level = level;
1233 sopt.sopt_name = name;
1234 sopt.sopt_val = __DECONST(void *, val);
1235 sopt.sopt_valsize = valsize;
1241 sopt.sopt_td = NULL;
1244 panic("kern_setsockopt called with bad valseg");
1248 error = getsock(td, s, &cap_setsockopt_rights, &fp);
1251 error = sosetopt(so, &sopt);
1258 sys_getsockopt(struct thread *td, struct getsockopt_args *uap)
1264 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1269 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1270 uap->val, UIO_USERSPACE, &valsize);
1273 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1278 * Kernel version of getsockopt.
1279 * optval can be a userland or userspace. optlen is always a kernel pointer.
1282 kern_getsockopt(struct thread *td, int s, int level, int name, void *val,
1283 enum uio_seg valseg, socklen_t *valsize)
1287 struct sockopt sopt;
1292 if ((int)*valsize < 0)
1295 sopt.sopt_dir = SOPT_GET;
1296 sopt.sopt_level = level;
1297 sopt.sopt_name = name;
1298 sopt.sopt_val = val;
1299 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1305 sopt.sopt_td = NULL;
1308 panic("kern_getsockopt called with bad valseg");
1312 error = getsock(td, s, &cap_getsockopt_rights, &fp);
1315 error = sogetopt(so, &sopt);
1316 *valsize = sopt.sopt_valsize;
1323 user_getsockname(struct thread *td, int fdes, struct sockaddr *asa,
1324 socklen_t *alen, bool compat)
1326 struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
1330 error = copyin(alen, &len, sizeof(len));
1334 error = kern_getsockname(td, fdes, (struct sockaddr *)&ss);
1338 #ifdef COMPAT_OLDSOCK
1339 if (compat && SV_PROC_FLAG(td->td_proc, SV_AOUT))
1340 ((struct osockaddr *)&ss)->sa_family = ss.ss_family;
1342 len = min(ss.ss_len, len);
1343 error = copyout(&ss, asa, len);
1346 error = copyout(&len, alen, sizeof(len));
1352 kern_getsockname(struct thread *td, int fd, struct sockaddr *sa)
1359 error = getsock(td, fd, &cap_getsockname_rights, &fp);
1363 error = sosockaddr(so, sa);
1365 if (error == 0 && KTRPOINT(td, KTR_STRUCT))
1373 sys_getsockname(struct thread *td, struct getsockname_args *uap)
1375 return (user_getsockname(td, uap->fdes, uap->asa, uap->alen, false));
1378 #ifdef COMPAT_OLDSOCK
1380 ogetsockname(struct thread *td, struct ogetsockname_args *uap)
1382 return (user_getsockname(td, uap->fdes, uap->asa, uap->alen, true));
1384 #endif /* COMPAT_OLDSOCK */
1387 user_getpeername(struct thread *td, int fdes, struct sockaddr *asa,
1388 socklen_t *alen, bool compat)
1390 struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
1394 error = copyin(alen, &len, sizeof (len));
1398 error = kern_getpeername(td, fdes, (struct sockaddr *)&ss);
1402 #ifdef COMPAT_OLDSOCK
1403 if (compat && SV_PROC_FLAG(td->td_proc, SV_AOUT))
1404 ((struct osockaddr *)&ss)->sa_family = ss.ss_family;
1406 len = min(ss.ss_len, len);
1407 error = copyout(&ss, asa, len);
1410 error = copyout(&len, alen, sizeof(len));
1416 kern_getpeername(struct thread *td, int fd, struct sockaddr *sa)
1423 error = getsock(td, fd, &cap_getpeername_rights, &fp);
1427 if ((so->so_state & SS_ISCONNECTED) == 0) {
1431 error = sopeeraddr(so, sa);
1433 if (error == 0 && KTRPOINT(td, KTR_STRUCT))
1442 sys_getpeername(struct thread *td, struct getpeername_args *uap)
1444 return (user_getpeername(td, uap->fdes, uap->asa, uap->alen, false));
1447 #ifdef COMPAT_OLDSOCK
1449 ogetpeername(struct thread *td, struct ogetpeername_args *uap)
1451 return (user_getpeername(td, uap->fdes, uap->asa, uap->alen, true));
1453 #endif /* COMPAT_OLDSOCK */
1456 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1458 struct sockaddr *sa;
1462 if (buflen > MLEN) {
1463 #ifdef COMPAT_OLDSOCK
1464 if (type == MT_SONAME && buflen <= 112 &&
1465 SV_CURPROC_FLAG(SV_AOUT))
1466 buflen = MLEN; /* unix domain compat. hack */
1469 if (buflen > MCLBYTES)
1472 m = m_get2(buflen, M_WAITOK, type, 0);
1474 error = copyin(buf, mtod(m, void *), buflen);
1479 if (type == MT_SONAME) {
1480 sa = mtod(m, struct sockaddr *);
1482 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1483 if (sa->sa_family == 0 && sa->sa_len < AF_MAX &&
1484 SV_CURPROC_FLAG(SV_AOUT))
1485 sa->sa_family = sa->sa_len;
1487 sa->sa_len = buflen;
1494 getsockaddr(struct sockaddr **namp, const struct sockaddr *uaddr, size_t len)
1496 struct sockaddr *sa;
1499 if (len > SOCK_MAXADDRLEN)
1500 return (ENAMETOOLONG);
1501 if (len < offsetof(struct sockaddr, sa_data[0]))
1503 sa = malloc(len, M_SONAME, M_WAITOK);
1504 error = copyin(uaddr, sa, len);
1508 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1509 if (sa->sa_family == 0 && sa->sa_len < AF_MAX &&
1510 SV_CURPROC_FLAG(SV_AOUT))
1511 sa->sa_family = sa->sa_len;
1520 * Dispose of externalized rights from an SCM_RIGHTS message. This function
1521 * should be used in error or truncation cases to avoid leaking file descriptors
1522 * into the recipient's (the current thread's) table.
1525 m_dispose_extcontrolm(struct mbuf *m)
1530 socklen_t clen, datalen;
1531 int error, fd, *fds, nfd;
1534 for (; m != NULL; m = m->m_next) {
1535 if (m->m_type != MT_EXTCONTROL)
1537 cm = mtod(m, struct cmsghdr *);
1540 if (clen < sizeof(*cm))
1541 panic("%s: truncated mbuf %p", __func__, m);
1542 datalen = CMSG_SPACE(cm->cmsg_len - CMSG_SPACE(0));
1544 panic("%s: truncated mbuf %p", __func__, m);
1546 if (cm->cmsg_level == SOL_SOCKET &&
1547 cm->cmsg_type == SCM_RIGHTS) {
1548 fds = (int *)CMSG_DATA(cm);
1549 nfd = (cm->cmsg_len - CMSG_SPACE(0)) /
1554 error = fget(td, fd, &cap_no_rights,
1557 fdclose(td, fp, fd);
1563 cm = (struct cmsghdr *)((uint8_t *)cm + datalen);
1565 m_chtype(m, MT_CONTROL);