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
31 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_capsicum.h"
39 #include "opt_inet6.h"
40 #include "opt_ktrace.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/capsicum.h>
45 #include <sys/kernel.h>
47 #include <sys/mutex.h>
48 #include <sys/sysproto.h>
49 #include <sys/malloc.h>
50 #include <sys/filedesc.h>
52 #include <sys/filio.h>
55 #include <sys/protosw.h>
56 #include <sys/rwlock.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/syscallsubr.h>
61 #include <sys/sysent.h>
65 #include <sys/unpcb.h>
67 #include <sys/ktrace.h>
69 #ifdef COMPAT_FREEBSD32
70 #include <compat/freebsd32/freebsd32_util.h>
75 #include <security/audit/audit.h>
76 #include <security/mac/mac_framework.h>
78 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
79 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
81 static int accept1(struct thread *td, int s, struct sockaddr *uname,
82 socklen_t *anamelen, int flags);
83 static int sockargs(struct mbuf **, char *, socklen_t, int);
86 * Convert a user file descriptor to a kernel file entry and check if required
87 * capability rights are present.
88 * If required copy of current set of capability rights is returned.
89 * A reference on the file entry is held upon returning.
92 getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
93 struct file **fpp, struct filecaps *havecapsp)
98 error = fget_cap(td, fd, rightsp, &fp, havecapsp);
99 if (__predict_false(error != 0))
101 if (__predict_false(fp->f_type != DTYPE_SOCKET)) {
103 if (havecapsp != NULL)
104 filecaps_free(havecapsp);
112 getsock(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
117 error = fget_unlocked(td, fd, rightsp, &fp);
118 if (__predict_false(error != 0))
120 if (__predict_false(fp->f_type != DTYPE_SOCKET)) {
129 * System call interface to the socket abstraction.
131 #if defined(COMPAT_43)
132 #define COMPAT_OLDSOCK
136 sys_socket(struct thread *td, struct socket_args *uap)
139 return (kern_socket(td, uap->domain, uap->type, uap->protocol));
143 kern_socket(struct thread *td, int domain, int type, int protocol)
147 int fd, error, oflag, fflag;
149 AUDIT_ARG_SOCKET(domain, type, protocol);
153 if ((type & SOCK_CLOEXEC) != 0) {
154 type &= ~SOCK_CLOEXEC;
157 if ((type & SOCK_NONBLOCK) != 0) {
158 type &= ~SOCK_NONBLOCK;
163 error = mac_socket_check_create(td->td_ucred, domain, type, protocol);
167 error = falloc(td, &fp, &fd, oflag);
170 /* An extra reference on `fp' has been held for us by falloc(). */
171 error = socreate(domain, &so, type, protocol, td->td_ucred, td);
175 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
176 if ((fflag & FNONBLOCK) != 0)
177 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
178 td->td_retval[0] = fd;
185 sys_bind(struct thread *td, struct bind_args *uap)
190 error = getsockaddr(&sa, uap->name, uap->namelen);
192 error = kern_bindat(td, AT_FDCWD, uap->s, sa);
199 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
205 #ifdef CAPABILITY_MODE
206 if (IN_CAPABILITY_MODE(td) && (dirfd == AT_FDCWD))
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 *name;
290 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
292 error = copyin(anamelen, &namelen, sizeof (namelen));
296 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
301 if (error == 0 && uname != NULL) {
302 #ifdef COMPAT_OLDSOCK
303 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) &&
304 (flags & ACCEPT4_COMPAT) != 0)
305 ((struct osockaddr *)name)->sa_family =
308 error = copyout(name, uname, namelen);
311 error = copyout(&namelen, anamelen,
314 fdclose(td, fp, td->td_retval[0]);
316 free(name, M_SONAME);
321 kern_accept(struct thread *td, int s, struct sockaddr **name,
322 socklen_t *namelen, struct file **fp)
324 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
328 kern_accept4(struct thread *td, int s, struct sockaddr **name,
329 socklen_t *namelen, int flags, struct file **fp)
331 struct file *headfp, *nfp = NULL;
332 struct sockaddr *sa = NULL;
333 struct socket *head, *so;
334 struct filecaps fcaps;
343 error = getsock_cap(td, s, &cap_accept_rights,
347 fflag = atomic_load_int(&headfp->f_flag);
348 head = headfp->f_data;
349 if (!SOLISTENING(head)) {
354 error = mac_socket_check_accept(td->td_ucred, head);
358 error = falloc_caps(td, &nfp, &fd,
359 (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0, &fcaps);
363 if (!SOLISTENING(head)) {
369 error = solisten_dequeue(head, &so, flags);
373 /* An extra reference on `nfp' has been held for us by falloc(). */
374 td->td_retval[0] = fd;
376 /* Connection has been removed from the listen queue. */
377 KNOTE_UNLOCKED(&head->so_rdsel.si_note, 0);
379 if (flags & ACCEPT4_INHERIT) {
380 pgid = fgetown(&head->so_sigio);
382 fsetown(pgid, &so->so_sigio);
384 fflag &= ~(FNONBLOCK | FASYNC);
385 if (flags & SOCK_NONBLOCK)
389 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
390 /* Sync socket nonblocking/async state with file flags */
391 tmp = fflag & FNONBLOCK;
392 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
393 tmp = fflag & FASYNC;
394 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
395 error = soaccept(so, &sa);
403 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
405 /* check sa_len before it is destroyed */
406 if (*namelen > sa->sa_len)
407 *namelen = sa->sa_len;
409 if (KTRPOINT(td, KTR_STRUCT))
419 * close the new descriptor, assuming someone hasn't ripped it
423 fdclose(td, nfp, fd);
426 * Release explicitly held references before returning. We return
427 * a reference on nfp to the caller on success if they request it.
431 filecaps_free(&fcaps);
446 sys_accept(struct thread *td, struct accept_args *uap)
449 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
453 sys_accept4(struct thread *td, struct accept4_args *uap)
456 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
459 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
462 #ifdef COMPAT_OLDSOCK
464 oaccept(struct thread *td, struct oaccept_args *uap)
467 return (accept1(td, uap->s, uap->name, uap->anamelen,
468 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
470 #endif /* COMPAT_OLDSOCK */
473 sys_connect(struct thread *td, struct connect_args *uap)
478 error = getsockaddr(&sa, uap->name, uap->namelen);
480 error = kern_connectat(td, AT_FDCWD, uap->s, sa);
487 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
493 #ifdef CAPABILITY_MODE
494 if (IN_CAPABILITY_MODE(td) && (dirfd == AT_FDCWD))
499 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
500 error = getsock(td, fd, &cap_connect_rights, &fp);
504 if (so->so_state & SS_ISCONNECTING) {
509 if (KTRPOINT(td, KTR_STRUCT))
513 error = mac_socket_check_connect(td->td_ucred, so, sa);
517 error = soconnectat(dirfd, so, sa, td);
520 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
525 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
526 error = msleep(&so->so_timeo, &so->so_lock, PSOCK | PCATCH,
532 error = so->so_error;
537 if (error == ERESTART)
545 sys_connectat(struct thread *td, struct connectat_args *uap)
550 error = getsockaddr(&sa, uap->name, uap->namelen);
552 error = kern_connectat(td, uap->fd, uap->s, sa);
559 kern_socketpair(struct thread *td, int domain, int type, int protocol,
562 struct file *fp1, *fp2;
563 struct socket *so1, *so2;
564 int fd, error, oflag, fflag;
566 AUDIT_ARG_SOCKET(domain, type, protocol);
570 if ((type & SOCK_CLOEXEC) != 0) {
571 type &= ~SOCK_CLOEXEC;
574 if ((type & SOCK_NONBLOCK) != 0) {
575 type &= ~SOCK_NONBLOCK;
579 /* We might want to have a separate check for socket pairs. */
580 error = mac_socket_check_create(td->td_ucred, domain, type,
585 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
588 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
591 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
592 error = falloc(td, &fp1, &fd, oflag);
596 fp1->f_data = so1; /* so1 already has ref count */
597 error = falloc(td, &fp2, &fd, oflag);
600 fp2->f_data = so2; /* so2 already has ref count */
602 error = soconnect2(so1, so2);
605 if (type == SOCK_DGRAM) {
607 * Datagram socket connection is asymmetric.
609 error = soconnect2(so2, so1);
612 } else if (so1->so_proto->pr_flags & PR_CONNREQUIRED) {
613 struct unpcb *unp, *unp2;
614 unp = sotounpcb(so1);
615 unp2 = sotounpcb(so2);
617 * No need to lock the unps, because the sockets are brand-new.
618 * No other threads can be using them yet
620 unp_copy_peercred(td, unp, unp2, unp);
622 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
624 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
626 if ((fflag & FNONBLOCK) != 0) {
627 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
628 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
634 fdclose(td, fp2, rsv[1]);
637 fdclose(td, fp1, rsv[0]);
649 sys_socketpair(struct thread *td, struct socketpair_args *uap)
653 error = kern_socketpair(td, uap->domain, uap->type,
657 error = copyout(sv, uap->rsv, 2 * sizeof(int));
659 (void)kern_close(td, sv[0]);
660 (void)kern_close(td, sv[1]);
666 sendit(struct thread *td, int s, struct msghdr *mp, int flags)
668 struct mbuf *control;
672 #ifdef CAPABILITY_MODE
673 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
677 if (mp->msg_name != NULL) {
678 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
688 if (mp->msg_control) {
689 if (mp->msg_controllen < sizeof(struct cmsghdr)
690 #ifdef COMPAT_OLDSOCK
691 && (mp->msg_flags != MSG_COMPAT ||
692 !SV_PROC_FLAG(td->td_proc, SV_AOUT))
698 error = sockargs(&control, mp->msg_control,
699 mp->msg_controllen, MT_CONTROL);
702 #ifdef COMPAT_OLDSOCK
703 if (mp->msg_flags == MSG_COMPAT &&
704 SV_PROC_FLAG(td->td_proc, SV_AOUT)) {
707 M_PREPEND(control, sizeof(*cm), M_WAITOK);
708 cm = mtod(control, struct cmsghdr *);
709 cm->cmsg_len = control->m_len;
710 cm->cmsg_level = SOL_SOCKET;
711 cm->cmsg_type = SCM_RIGHTS;
718 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
726 kern_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
727 struct mbuf *control, enum uio_seg segflg)
733 cap_rights_t *rights;
735 struct uio *ktruio = NULL;
741 rights = &cap_send_rights;
742 if (mp->msg_name != NULL) {
743 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
744 rights = &cap_send_connect_rights;
746 error = getsock(td, s, rights, &fp);
751 so = (struct socket *)fp->f_data;
754 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
755 ktrsockaddr(mp->msg_name);
758 if (mp->msg_name != NULL) {
759 error = mac_socket_check_connect(td->td_ucred, so,
766 error = mac_socket_check_send(td->td_ucred, so);
773 auio.uio_iov = mp->msg_iov;
774 auio.uio_iovcnt = mp->msg_iovlen;
775 auio.uio_segflg = segflg;
776 auio.uio_rw = UIO_WRITE;
778 auio.uio_offset = 0; /* XXX */
781 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
782 if ((auio.uio_resid += iov->iov_len) < 0) {
789 if (KTRPOINT(td, KTR_GENIO))
790 ktruio = cloneuio(&auio);
792 len = auio.uio_resid;
793 error = sousrsend(so, mp->msg_name, &auio, control, flags, NULL);
795 td->td_retval[0] = len - auio.uio_resid;
797 if (ktruio != NULL) {
798 ktruio->uio_resid = td->td_retval[0];
799 ktrgenio(s, UIO_WRITE, ktruio, error);
808 sys_sendto(struct thread *td, struct sendto_args *uap)
813 msg.msg_name = __DECONST(void *, uap->to);
814 msg.msg_namelen = uap->tolen;
818 #ifdef COMPAT_OLDSOCK
819 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
822 aiov.iov_base = __DECONST(void *, uap->buf);
823 aiov.iov_len = uap->len;
824 return (sendit(td, uap->s, &msg, uap->flags));
827 #ifdef COMPAT_OLDSOCK
829 osend(struct thread *td, struct osend_args *uap)
838 aiov.iov_base = __DECONST(void *, uap->buf);
839 aiov.iov_len = uap->len;
842 return (sendit(td, uap->s, &msg, uap->flags));
846 osendmsg(struct thread *td, struct osendmsg_args *uap)
852 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
855 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
859 msg.msg_flags = MSG_COMPAT;
860 error = sendit(td, uap->s, &msg, uap->flags);
867 sys_sendmsg(struct thread *td, struct sendmsg_args *uap)
873 error = copyin(uap->msg, &msg, sizeof (msg));
876 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
880 #ifdef COMPAT_OLDSOCK
881 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
884 error = sendit(td, uap->s, &msg, uap->flags);
890 kern_recvit(struct thread *td, int s, struct msghdr *mp, enum uio_seg fromseg,
891 struct mbuf **controlp)
895 struct mbuf *control, *m;
899 struct sockaddr *fromsa = NULL;
901 struct uio *ktruio = NULL;
906 if (controlp != NULL)
910 error = getsock(td, s, &cap_recv_rights, &fp);
916 error = mac_socket_check_receive(td->td_ucred, so);
923 auio.uio_iov = mp->msg_iov;
924 auio.uio_iovcnt = mp->msg_iovlen;
925 auio.uio_segflg = UIO_USERSPACE;
926 auio.uio_rw = UIO_READ;
928 auio.uio_offset = 0; /* XXX */
931 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
932 if ((auio.uio_resid += iov->iov_len) < 0) {
938 if (KTRPOINT(td, KTR_GENIO))
939 ktruio = cloneuio(&auio);
942 len = auio.uio_resid;
943 error = soreceive(so, &fromsa, &auio, NULL,
944 (mp->msg_control || controlp) ? &control : NULL,
947 if (auio.uio_resid != len && (error == ERESTART ||
948 error == EINTR || error == EWOULDBLOCK))
952 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
954 if (ktruio != NULL) {
955 ktruio->uio_resid = len - auio.uio_resid;
956 ktrgenio(s, UIO_READ, ktruio, error);
961 td->td_retval[0] = len - auio.uio_resid;
963 len = mp->msg_namelen;
964 if (len <= 0 || fromsa == NULL)
967 /* save sa_len before it is destroyed by MSG_COMPAT */
968 len = MIN(len, fromsa->sa_len);
969 #ifdef COMPAT_OLDSOCK
970 if ((mp->msg_flags & MSG_COMPAT) != 0 &&
971 SV_PROC_FLAG(td->td_proc, SV_AOUT))
972 ((struct osockaddr *)fromsa)->sa_family =
975 if (fromseg == UIO_USERSPACE) {
976 error = copyout(fromsa, mp->msg_name,
981 bcopy(fromsa, mp->msg_name, len);
983 mp->msg_namelen = len;
985 if (mp->msg_control && controlp == NULL) {
986 #ifdef COMPAT_OLDSOCK
988 * We assume that old recvmsg calls won't receive access
989 * rights and other control info, esp. as control info
990 * is always optional and those options didn't exist in 4.3.
991 * If we receive rights, trim the cmsghdr; anything else
994 if (control && (mp->msg_flags & MSG_COMPAT) != 0 &&
995 SV_PROC_FLAG(td->td_proc, SV_AOUT)) {
996 if (mtod(control, struct cmsghdr *)->cmsg_level !=
998 mtod(control, struct cmsghdr *)->cmsg_type !=
1000 mp->msg_controllen = 0;
1003 control->m_len -= sizeof (struct cmsghdr);
1004 control->m_data += sizeof (struct cmsghdr);
1007 ctlbuf = mp->msg_control;
1008 len = mp->msg_controllen;
1009 mp->msg_controllen = 0;
1010 for (m = control; m != NULL && len >= m->m_len; m = m->m_next) {
1011 if ((error = copyout(mtod(m, caddr_t), ctlbuf,
1017 mp->msg_controllen += m->m_len;
1020 mp->msg_flags |= MSG_CTRUNC;
1021 m_dispose_extcontrolm(m);
1027 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1028 ktrsockaddr(fromsa);
1030 free(fromsa, M_SONAME);
1032 if (error == 0 && controlp != NULL)
1033 *controlp = control;
1034 else if (control != NULL) {
1036 m_dispose_extcontrolm(control);
1044 recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp)
1048 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1051 if (namelenp != NULL) {
1052 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1053 #ifdef COMPAT_OLDSOCK
1054 if ((mp->msg_flags & MSG_COMPAT) != 0 &&
1055 SV_PROC_FLAG(td->td_proc, SV_AOUT))
1056 error = 0; /* old recvfrom didn't check */
1063 kern_recvfrom(struct thread *td, int s, void *buf, size_t len, int flags,
1064 struct sockaddr *from, socklen_t *fromlenaddr)
1070 if (fromlenaddr != NULL) {
1071 error = copyin(fromlenaddr, &msg.msg_namelen,
1072 sizeof (msg.msg_namelen));
1076 msg.msg_namelen = 0;
1078 msg.msg_name = from;
1079 msg.msg_iov = &aiov;
1081 aiov.iov_base = buf;
1083 msg.msg_control = 0;
1084 msg.msg_flags = flags;
1085 error = recvit(td, s, &msg, fromlenaddr);
1091 sys_recvfrom(struct thread *td, struct recvfrom_args *uap)
1093 return (kern_recvfrom(td, uap->s, uap->buf, uap->len,
1094 uap->flags, uap->from, uap->fromlenaddr));
1098 #ifdef COMPAT_OLDSOCK
1100 orecvfrom(struct thread *td, struct orecvfrom_args *uap)
1102 return (kern_recvfrom(td, uap->s, uap->buf, uap->len,
1103 uap->flags | MSG_COMPAT, uap->from, uap->fromlenaddr));
1107 #ifdef COMPAT_OLDSOCK
1109 orecv(struct thread *td, struct orecv_args *uap)
1115 msg.msg_namelen = 0;
1116 msg.msg_iov = &aiov;
1118 aiov.iov_base = uap->buf;
1119 aiov.iov_len = uap->len;
1120 msg.msg_control = 0;
1121 msg.msg_flags = uap->flags;
1122 return (recvit(td, uap->s, &msg, NULL));
1126 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1127 * overlays the new one, missing only the flags, and with the (old) access
1128 * rights where the control fields are now.
1131 orecvmsg(struct thread *td, struct orecvmsg_args *uap)
1137 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1140 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1143 msg.msg_flags = uap->flags | MSG_COMPAT;
1145 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1146 if (msg.msg_controllen && error == 0)
1147 error = copyout(&msg.msg_controllen,
1148 &uap->msg->msg_accrightslen, sizeof (int));
1155 sys_recvmsg(struct thread *td, struct recvmsg_args *uap)
1158 struct iovec *uiov, *iov;
1161 error = copyin(uap->msg, &msg, sizeof (msg));
1164 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1167 msg.msg_flags = uap->flags;
1168 #ifdef COMPAT_OLDSOCK
1169 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
1170 msg.msg_flags &= ~MSG_COMPAT;
1174 error = recvit(td, uap->s, &msg, NULL);
1177 error = copyout(&msg, uap->msg, sizeof(msg));
1184 sys_shutdown(struct thread *td, struct shutdown_args *uap)
1187 return (kern_shutdown(td, uap->s, uap->how));
1191 kern_shutdown(struct thread *td, int s, int how)
1198 error = getsock(td, s, &cap_shutdown_rights, &fp);
1201 error = soshutdown(so, how);
1203 * Previous versions did not return ENOTCONN, but 0 in
1204 * case the socket was not connected. Some important
1205 * programs like syslogd up to r279016, 2015-02-19,
1206 * still depend on this behavior.
1208 if (error == ENOTCONN &&
1209 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1217 sys_setsockopt(struct thread *td, struct setsockopt_args *uap)
1220 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1221 uap->val, UIO_USERSPACE, uap->valsize));
1225 kern_setsockopt(struct thread *td, int s, int level, int name, const void *val,
1226 enum uio_seg valseg, socklen_t valsize)
1230 struct sockopt sopt;
1233 if (val == NULL && valsize != 0)
1235 if ((int)valsize < 0)
1238 sopt.sopt_dir = SOPT_SET;
1239 sopt.sopt_level = level;
1240 sopt.sopt_name = name;
1241 sopt.sopt_val = __DECONST(void *, val);
1242 sopt.sopt_valsize = valsize;
1248 sopt.sopt_td = NULL;
1251 panic("kern_setsockopt called with bad valseg");
1255 error = getsock(td, s, &cap_setsockopt_rights, &fp);
1258 error = sosetopt(so, &sopt);
1265 sys_getsockopt(struct thread *td, struct getsockopt_args *uap)
1271 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1276 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1277 uap->val, UIO_USERSPACE, &valsize);
1280 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1285 * Kernel version of getsockopt.
1286 * optval can be a userland or userspace. optlen is always a kernel pointer.
1289 kern_getsockopt(struct thread *td, int s, int level, int name, void *val,
1290 enum uio_seg valseg, socklen_t *valsize)
1294 struct sockopt sopt;
1299 if ((int)*valsize < 0)
1302 sopt.sopt_dir = SOPT_GET;
1303 sopt.sopt_level = level;
1304 sopt.sopt_name = name;
1305 sopt.sopt_val = val;
1306 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1312 sopt.sopt_td = NULL;
1315 panic("kern_getsockopt called with bad valseg");
1319 error = getsock(td, s, &cap_getsockopt_rights, &fp);
1322 error = sogetopt(so, &sopt);
1323 *valsize = sopt.sopt_valsize;
1330 user_getsockname(struct thread *td, int fdes, struct sockaddr *asa,
1331 socklen_t *alen, bool compat)
1333 struct sockaddr *sa;
1337 error = copyin(alen, &len, sizeof(len));
1341 error = kern_getsockname(td, fdes, &sa, &len);
1346 #ifdef COMPAT_OLDSOCK
1347 if (compat && SV_PROC_FLAG(td->td_proc, SV_AOUT))
1348 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1350 error = copyout(sa, asa, len);
1354 error = copyout(&len, alen, sizeof(len));
1359 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1368 error = getsock(td, fd, &cap_getsockname_rights, &fp);
1373 CURVNET_SET(so->so_vnet);
1374 error = so->so_proto->pr_sockaddr(so, sa);
1381 len = MIN(*alen, (*sa)->sa_len);
1384 if (KTRPOINT(td, KTR_STRUCT))
1389 if (error != 0 && *sa != NULL) {
1390 free(*sa, M_SONAME);
1397 sys_getsockname(struct thread *td, struct getsockname_args *uap)
1399 return (user_getsockname(td, uap->fdes, uap->asa, uap->alen, false));
1402 #ifdef COMPAT_OLDSOCK
1404 ogetsockname(struct thread *td, struct ogetsockname_args *uap)
1406 return (user_getsockname(td, uap->fdes, uap->asa, uap->alen, true));
1408 #endif /* COMPAT_OLDSOCK */
1411 user_getpeername(struct thread *td, int fdes, struct sockaddr *asa,
1412 socklen_t *alen, bool compat)
1414 struct sockaddr *sa;
1418 error = copyin(alen, &len, sizeof (len));
1422 error = kern_getpeername(td, fdes, &sa, &len);
1427 #ifdef COMPAT_OLDSOCK
1428 if (compat && SV_PROC_FLAG(td->td_proc, SV_AOUT))
1429 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1431 error = copyout(sa, asa, len);
1435 error = copyout(&len, alen, sizeof(len));
1440 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1449 error = getsock(td, fd, &cap_getpeername_rights, &fp);
1453 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1458 CURVNET_SET(so->so_vnet);
1459 error = so->so_proto->pr_peeraddr(so, sa);
1466 len = MIN(*alen, (*sa)->sa_len);
1469 if (KTRPOINT(td, KTR_STRUCT))
1473 if (error != 0 && *sa != NULL) {
1474 free(*sa, M_SONAME);
1483 sys_getpeername(struct thread *td, struct getpeername_args *uap)
1485 return (user_getpeername(td, uap->fdes, uap->asa, uap->alen, false));
1488 #ifdef COMPAT_OLDSOCK
1490 ogetpeername(struct thread *td, struct ogetpeername_args *uap)
1492 return (user_getpeername(td, uap->fdes, uap->asa, uap->alen, true));
1494 #endif /* COMPAT_OLDSOCK */
1497 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1499 struct sockaddr *sa;
1503 if (buflen > MLEN) {
1504 #ifdef COMPAT_OLDSOCK
1505 if (type == MT_SONAME && buflen <= 112 &&
1506 SV_CURPROC_FLAG(SV_AOUT))
1507 buflen = MLEN; /* unix domain compat. hack */
1510 if (buflen > MCLBYTES)
1513 m = m_get2(buflen, M_WAITOK, type, 0);
1515 error = copyin(buf, mtod(m, void *), buflen);
1520 if (type == MT_SONAME) {
1521 sa = mtod(m, struct sockaddr *);
1523 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1524 if (sa->sa_family == 0 && sa->sa_len < AF_MAX &&
1525 SV_CURPROC_FLAG(SV_AOUT))
1526 sa->sa_family = sa->sa_len;
1528 sa->sa_len = buflen;
1535 getsockaddr(struct sockaddr **namp, const struct sockaddr *uaddr, size_t len)
1537 struct sockaddr *sa;
1540 if (len > SOCK_MAXADDRLEN)
1541 return (ENAMETOOLONG);
1542 if (len < offsetof(struct sockaddr, sa_data[0]))
1544 sa = malloc(len, M_SONAME, M_WAITOK);
1545 error = copyin(uaddr, sa, len);
1549 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1550 if (sa->sa_family == 0 && sa->sa_len < AF_MAX &&
1551 SV_CURPROC_FLAG(SV_AOUT))
1552 sa->sa_family = sa->sa_len;
1561 * Dispose of externalized rights from an SCM_RIGHTS message. This function
1562 * should be used in error or truncation cases to avoid leaking file descriptors
1563 * into the recipient's (the current thread's) table.
1566 m_dispose_extcontrolm(struct mbuf *m)
1571 socklen_t clen, datalen;
1572 int error, fd, *fds, nfd;
1575 for (; m != NULL; m = m->m_next) {
1576 if (m->m_type != MT_EXTCONTROL)
1578 cm = mtod(m, struct cmsghdr *);
1581 if (clen < sizeof(*cm))
1582 panic("%s: truncated mbuf %p", __func__, m);
1583 datalen = CMSG_SPACE(cm->cmsg_len - CMSG_SPACE(0));
1585 panic("%s: truncated mbuf %p", __func__, m);
1587 if (cm->cmsg_level == SOL_SOCKET &&
1588 cm->cmsg_type == SCM_RIGHTS) {
1589 fds = (int *)CMSG_DATA(cm);
1590 nfd = (cm->cmsg_len - CMSG_SPACE(0)) /
1595 error = fget(td, fd, &cap_no_rights,
1598 fdclose(td, fp, fd);
1604 cm = (struct cmsghdr *)((uint8_t *)cm + datalen);
1606 m_chtype(m, MT_CONTROL);