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>
60 #include <sys/sysent.h>
63 #include <sys/unpcb.h>
65 #include <sys/ktrace.h>
67 #ifdef COMPAT_FREEBSD32
68 #include <compat/freebsd32/freebsd32_util.h>
73 #include <security/audit/audit.h>
74 #include <security/mac/mac_framework.h>
76 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
77 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
79 static int accept1(struct thread *td, int s, struct sockaddr *uname,
80 socklen_t *anamelen, int flags);
81 static int sockargs(struct mbuf **, char *, socklen_t, int);
84 * Convert a user file descriptor to a kernel file entry and check if required
85 * capability rights are present.
86 * If required copy of current set of capability rights is returned.
87 * A reference on the file entry is held upon returning.
90 getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
91 struct file **fpp, u_int *fflagp, struct filecaps *havecapsp)
96 error = fget_cap(td, fd, rightsp, &fp, havecapsp);
99 if (fp->f_type != DTYPE_SOCKET) {
101 if (havecapsp != NULL)
102 filecaps_free(havecapsp);
106 *fflagp = fp->f_flag;
112 * System call interface to the socket abstraction.
114 #if defined(COMPAT_43)
115 #define COMPAT_OLDSOCK
119 sys_socket(struct thread *td, struct socket_args *uap)
122 return (kern_socket(td, uap->domain, uap->type, uap->protocol));
126 kern_socket(struct thread *td, int domain, int type, int protocol)
130 int fd, error, oflag, fflag;
132 AUDIT_ARG_SOCKET(domain, type, protocol);
136 if ((type & SOCK_CLOEXEC) != 0) {
137 type &= ~SOCK_CLOEXEC;
140 if ((type & SOCK_NONBLOCK) != 0) {
141 type &= ~SOCK_NONBLOCK;
146 error = mac_socket_check_create(td->td_ucred, domain, type, protocol);
150 error = falloc(td, &fp, &fd, oflag);
153 /* An extra reference on `fp' has been held for us by falloc(). */
154 error = socreate(domain, &so, type, protocol, td->td_ucred, td);
158 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
159 if ((fflag & FNONBLOCK) != 0)
160 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
161 td->td_retval[0] = fd;
168 sys_bind(struct thread *td, struct bind_args *uap)
173 error = getsockaddr(&sa, uap->name, uap->namelen);
175 error = kern_bindat(td, AT_FDCWD, uap->s, sa);
182 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
188 #ifdef CAPABILITY_MODE
189 if (IN_CAPABILITY_MODE(td) && (dirfd == AT_FDCWD))
194 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
195 error = getsock_cap(td, fd, &cap_bind_rights,
201 if (KTRPOINT(td, KTR_STRUCT))
205 error = mac_socket_check_bind(td->td_ucred, so, sa);
208 if (dirfd == AT_FDCWD)
209 error = sobind(so, sa, td);
211 error = sobindat(dirfd, so, sa, td);
220 sys_bindat(struct thread *td, struct bindat_args *uap)
225 error = getsockaddr(&sa, uap->name, uap->namelen);
227 error = kern_bindat(td, uap->fd, uap->s, sa);
234 sys_listen(struct thread *td, struct listen_args *uap)
237 return (kern_listen(td, uap->s, uap->backlog));
241 kern_listen(struct thread *td, int s, int backlog)
248 error = getsock_cap(td, s, &cap_listen_rights,
253 error = mac_socket_check_listen(td->td_ucred, so);
256 error = solisten(so, backlog, td);
266 accept1(td, s, uname, anamelen, flags)
269 struct sockaddr *uname;
273 struct sockaddr *name;
279 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
281 error = copyin(anamelen, &namelen, sizeof (namelen));
285 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
290 if (error == 0 && uname != NULL) {
291 #ifdef COMPAT_OLDSOCK
292 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) &&
293 (flags & ACCEPT4_COMPAT) != 0)
294 ((struct osockaddr *)name)->sa_family =
297 error = copyout(name, uname, namelen);
300 error = copyout(&namelen, anamelen,
303 fdclose(td, fp, td->td_retval[0]);
305 free(name, M_SONAME);
310 kern_accept(struct thread *td, int s, struct sockaddr **name,
311 socklen_t *namelen, struct file **fp)
313 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
317 kern_accept4(struct thread *td, int s, struct sockaddr **name,
318 socklen_t *namelen, int flags, struct file **fp)
320 struct file *headfp, *nfp = NULL;
321 struct sockaddr *sa = NULL;
322 struct socket *head, *so;
323 struct filecaps fcaps;
332 error = getsock_cap(td, s, &cap_accept_rights,
333 &headfp, &fflag, &fcaps);
336 head = headfp->f_data;
337 if (!SOLISTENING(head)) {
342 error = mac_socket_check_accept(td->td_ucred, head);
346 error = falloc_caps(td, &nfp, &fd,
347 (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0, &fcaps);
351 if (!SOLISTENING(head)) {
357 error = solisten_dequeue(head, &so, flags);
361 /* An extra reference on `nfp' has been held for us by falloc(). */
362 td->td_retval[0] = fd;
364 /* Connection has been removed from the listen queue. */
365 KNOTE_UNLOCKED(&head->so_rdsel.si_note, 0);
367 if (flags & ACCEPT4_INHERIT) {
368 pgid = fgetown(&head->so_sigio);
370 fsetown(pgid, &so->so_sigio);
372 fflag &= ~(FNONBLOCK | FASYNC);
373 if (flags & SOCK_NONBLOCK)
377 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
378 /* Sync socket nonblocking/async state with file flags */
379 tmp = fflag & FNONBLOCK;
380 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
381 tmp = fflag & FASYNC;
382 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
383 error = soaccept(so, &sa);
391 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
393 /* check sa_len before it is destroyed */
394 if (*namelen > sa->sa_len)
395 *namelen = sa->sa_len;
397 if (KTRPOINT(td, KTR_STRUCT))
407 * close the new descriptor, assuming someone hasn't ripped it
411 fdclose(td, nfp, fd);
414 * Release explicitly held references before returning. We return
415 * a reference on nfp to the caller on success if they request it.
419 filecaps_free(&fcaps);
436 struct accept_args *uap;
439 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
445 struct accept4_args *uap;
448 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
451 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
454 #ifdef COMPAT_OLDSOCK
456 oaccept(struct thread *td, struct oaccept_args *uap)
459 return (accept1(td, uap->s, uap->name, uap->anamelen,
460 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
462 #endif /* COMPAT_OLDSOCK */
465 sys_connect(struct thread *td, struct connect_args *uap)
470 error = getsockaddr(&sa, uap->name, uap->namelen);
472 error = kern_connectat(td, AT_FDCWD, uap->s, sa);
479 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
485 #ifdef CAPABILITY_MODE
486 if (IN_CAPABILITY_MODE(td) && (dirfd == AT_FDCWD))
491 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
492 error = getsock_cap(td, fd, &cap_connect_rights,
497 if (so->so_state & SS_ISCONNECTING) {
502 if (KTRPOINT(td, KTR_STRUCT))
506 error = mac_socket_check_connect(td->td_ucred, so, sa);
510 error = soconnectat(dirfd, so, sa, td);
513 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
518 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
519 error = msleep(&so->so_timeo, &so->so_lock, PSOCK | PCATCH,
525 error = so->so_error;
530 if (error == ERESTART)
538 sys_connectat(struct thread *td, struct connectat_args *uap)
543 error = getsockaddr(&sa, uap->name, uap->namelen);
545 error = kern_connectat(td, uap->fd, uap->s, sa);
552 kern_socketpair(struct thread *td, int domain, int type, int protocol,
555 struct file *fp1, *fp2;
556 struct socket *so1, *so2;
557 int fd, error, oflag, fflag;
559 AUDIT_ARG_SOCKET(domain, type, protocol);
563 if ((type & SOCK_CLOEXEC) != 0) {
564 type &= ~SOCK_CLOEXEC;
567 if ((type & SOCK_NONBLOCK) != 0) {
568 type &= ~SOCK_NONBLOCK;
572 /* We might want to have a separate check for socket pairs. */
573 error = mac_socket_check_create(td->td_ucred, domain, type,
578 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
581 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
584 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
585 error = falloc(td, &fp1, &fd, oflag);
589 fp1->f_data = so1; /* so1 already has ref count */
590 error = falloc(td, &fp2, &fd, oflag);
593 fp2->f_data = so2; /* so2 already has ref count */
595 error = soconnect2(so1, so2);
598 if (type == SOCK_DGRAM) {
600 * Datagram socket connection is asymmetric.
602 error = soconnect2(so2, so1);
605 } else if (so1->so_proto->pr_flags & PR_CONNREQUIRED) {
606 struct unpcb *unp, *unp2;
607 unp = sotounpcb(so1);
608 unp2 = sotounpcb(so2);
610 * No need to lock the unps, because the sockets are brand-new.
611 * No other threads can be using them yet
613 unp_copy_peercred(td, unp, unp2, unp);
615 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
617 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
619 if ((fflag & FNONBLOCK) != 0) {
620 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
621 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
627 fdclose(td, fp2, rsv[1]);
630 fdclose(td, fp1, rsv[0]);
642 sys_socketpair(struct thread *td, struct socketpair_args *uap)
646 error = kern_socketpair(td, uap->domain, uap->type,
650 error = copyout(sv, uap->rsv, 2 * sizeof(int));
652 (void)kern_close(td, sv[0]);
653 (void)kern_close(td, sv[1]);
659 sendit(struct thread *td, int s, struct msghdr *mp, int flags)
661 struct mbuf *control;
665 #ifdef CAPABILITY_MODE
666 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
670 if (mp->msg_name != NULL) {
671 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
681 if (mp->msg_control) {
682 if (mp->msg_controllen < sizeof(struct cmsghdr)
683 #ifdef COMPAT_OLDSOCK
684 && (mp->msg_flags != MSG_COMPAT ||
685 !SV_PROC_FLAG(td->td_proc, SV_AOUT))
691 error = sockargs(&control, mp->msg_control,
692 mp->msg_controllen, MT_CONTROL);
695 #ifdef COMPAT_OLDSOCK
696 if (mp->msg_flags == MSG_COMPAT &&
697 SV_PROC_FLAG(td->td_proc, SV_AOUT)) {
700 M_PREPEND(control, sizeof(*cm), M_WAITOK);
701 cm = mtod(control, struct cmsghdr *);
702 cm->cmsg_len = control->m_len;
703 cm->cmsg_level = SOL_SOCKET;
704 cm->cmsg_type = SCM_RIGHTS;
711 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
719 kern_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
720 struct mbuf *control, enum uio_seg segflg)
726 cap_rights_t *rights;
728 struct uio *ktruio = NULL;
734 rights = &cap_send_rights;
735 if (mp->msg_name != NULL) {
736 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
737 rights = &cap_send_connect_rights;
739 error = getsock_cap(td, s, rights, &fp, NULL, NULL);
744 so = (struct socket *)fp->f_data;
747 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
748 ktrsockaddr(mp->msg_name);
751 if (mp->msg_name != NULL) {
752 error = mac_socket_check_connect(td->td_ucred, so,
759 error = mac_socket_check_send(td->td_ucred, so);
766 auio.uio_iov = mp->msg_iov;
767 auio.uio_iovcnt = mp->msg_iovlen;
768 auio.uio_segflg = segflg;
769 auio.uio_rw = UIO_WRITE;
771 auio.uio_offset = 0; /* XXX */
774 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
775 if ((auio.uio_resid += iov->iov_len) < 0) {
782 if (KTRPOINT(td, KTR_GENIO))
783 ktruio = cloneuio(&auio);
785 len = auio.uio_resid;
786 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
788 if (auio.uio_resid != len && (error == ERESTART ||
789 error == EINTR || error == EWOULDBLOCK))
791 /* Generation of SIGPIPE can be controlled per socket */
792 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
793 !(flags & MSG_NOSIGNAL)) {
794 PROC_LOCK(td->td_proc);
795 tdsignal(td, SIGPIPE);
796 PROC_UNLOCK(td->td_proc);
800 td->td_retval[0] = len - auio.uio_resid;
802 if (ktruio != NULL) {
803 ktruio->uio_resid = td->td_retval[0];
804 ktrgenio(s, UIO_WRITE, ktruio, error);
813 sys_sendto(struct thread *td, struct sendto_args *uap)
818 msg.msg_name = __DECONST(void *, uap->to);
819 msg.msg_namelen = uap->tolen;
823 #ifdef COMPAT_OLDSOCK
824 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
827 aiov.iov_base = __DECONST(void *, uap->buf);
828 aiov.iov_len = uap->len;
829 return (sendit(td, uap->s, &msg, uap->flags));
832 #ifdef COMPAT_OLDSOCK
834 osend(struct thread *td, struct osend_args *uap)
843 aiov.iov_base = __DECONST(void *, uap->buf);
844 aiov.iov_len = uap->len;
847 return (sendit(td, uap->s, &msg, uap->flags));
851 osendmsg(struct thread *td, struct osendmsg_args *uap)
857 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
860 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
864 msg.msg_flags = MSG_COMPAT;
865 error = sendit(td, uap->s, &msg, uap->flags);
872 sys_sendmsg(struct thread *td, struct sendmsg_args *uap)
878 error = copyin(uap->msg, &msg, sizeof (msg));
881 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
885 #ifdef COMPAT_OLDSOCK
886 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
889 error = sendit(td, uap->s, &msg, uap->flags);
895 kern_recvit(struct thread *td, int s, struct msghdr *mp, enum uio_seg fromseg,
896 struct mbuf **controlp)
900 struct mbuf *control, *m;
904 struct sockaddr *fromsa = NULL;
906 struct uio *ktruio = NULL;
911 if (controlp != NULL)
915 error = getsock_cap(td, s, &cap_recv_rights,
922 error = mac_socket_check_receive(td->td_ucred, so);
929 auio.uio_iov = mp->msg_iov;
930 auio.uio_iovcnt = mp->msg_iovlen;
931 auio.uio_segflg = UIO_USERSPACE;
932 auio.uio_rw = UIO_READ;
934 auio.uio_offset = 0; /* XXX */
937 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
938 if ((auio.uio_resid += iov->iov_len) < 0) {
944 if (KTRPOINT(td, KTR_GENIO))
945 ktruio = cloneuio(&auio);
948 len = auio.uio_resid;
949 error = soreceive(so, &fromsa, &auio, NULL,
950 (mp->msg_control || controlp) ? &control : NULL,
953 if (auio.uio_resid != len && (error == ERESTART ||
954 error == EINTR || error == EWOULDBLOCK))
958 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
960 if (ktruio != NULL) {
961 ktruio->uio_resid = len - auio.uio_resid;
962 ktrgenio(s, UIO_READ, ktruio, error);
967 td->td_retval[0] = len - auio.uio_resid;
969 len = mp->msg_namelen;
970 if (len <= 0 || fromsa == NULL)
973 /* save sa_len before it is destroyed by MSG_COMPAT */
974 len = MIN(len, fromsa->sa_len);
975 #ifdef COMPAT_OLDSOCK
976 if ((mp->msg_flags & MSG_COMPAT) != 0 &&
977 SV_PROC_FLAG(td->td_proc, SV_AOUT))
978 ((struct osockaddr *)fromsa)->sa_family =
981 if (fromseg == UIO_USERSPACE) {
982 error = copyout(fromsa, mp->msg_name,
987 bcopy(fromsa, mp->msg_name, len);
989 mp->msg_namelen = len;
991 if (mp->msg_control && controlp == NULL) {
992 #ifdef COMPAT_OLDSOCK
994 * We assume that old recvmsg calls won't receive access
995 * rights and other control info, esp. as control info
996 * is always optional and those options didn't exist in 4.3.
997 * If we receive rights, trim the cmsghdr; anything else
1000 if (control && (mp->msg_flags & MSG_COMPAT) != 0 &&
1001 SV_PROC_FLAG(td->td_proc, SV_AOUT)) {
1002 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1004 mtod(control, struct cmsghdr *)->cmsg_type !=
1006 mp->msg_controllen = 0;
1009 control->m_len -= sizeof (struct cmsghdr);
1010 control->m_data += sizeof (struct cmsghdr);
1013 ctlbuf = mp->msg_control;
1014 len = mp->msg_controllen;
1015 mp->msg_controllen = 0;
1016 for (m = control; m != NULL && len >= m->m_len; m = m->m_next) {
1017 if ((error = copyout(mtod(m, caddr_t), ctlbuf,
1023 mp->msg_controllen += m->m_len;
1026 mp->msg_flags |= MSG_CTRUNC;
1027 m_dispose_extcontrolm(m);
1033 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1034 ktrsockaddr(fromsa);
1036 free(fromsa, M_SONAME);
1038 if (error == 0 && controlp != NULL)
1039 *controlp = control;
1040 else if (control != NULL) {
1042 m_dispose_extcontrolm(control);
1050 recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp)
1054 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1057 if (namelenp != NULL) {
1058 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1059 #ifdef COMPAT_OLDSOCK
1060 if ((mp->msg_flags & MSG_COMPAT) != 0 &&
1061 SV_PROC_FLAG(td->td_proc, SV_AOUT))
1062 error = 0; /* old recvfrom didn't check */
1069 kern_recvfrom(struct thread *td, int s, void *buf, size_t len, int flags,
1070 struct sockaddr *from, socklen_t *fromlenaddr)
1076 if (fromlenaddr != NULL) {
1077 error = copyin(fromlenaddr, &msg.msg_namelen,
1078 sizeof (msg.msg_namelen));
1082 msg.msg_namelen = 0;
1084 msg.msg_name = from;
1085 msg.msg_iov = &aiov;
1087 aiov.iov_base = buf;
1089 msg.msg_control = 0;
1090 msg.msg_flags = flags;
1091 error = recvit(td, s, &msg, fromlenaddr);
1097 sys_recvfrom(struct thread *td, struct recvfrom_args *uap)
1099 return (kern_recvfrom(td, uap->s, uap->buf, uap->len,
1100 uap->flags, uap->from, uap->fromlenaddr));
1104 #ifdef COMPAT_OLDSOCK
1106 orecvfrom(struct thread *td, struct recvfrom_args *uap)
1108 return (kern_recvfrom(td, uap->s, uap->buf, uap->len,
1109 uap->flags | MSG_COMPAT, uap->from, uap->fromlenaddr));
1113 #ifdef COMPAT_OLDSOCK
1115 orecv(struct thread *td, struct orecv_args *uap)
1121 msg.msg_namelen = 0;
1122 msg.msg_iov = &aiov;
1124 aiov.iov_base = uap->buf;
1125 aiov.iov_len = uap->len;
1126 msg.msg_control = 0;
1127 msg.msg_flags = uap->flags;
1128 return (recvit(td, uap->s, &msg, NULL));
1132 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1133 * overlays the new one, missing only the flags, and with the (old) access
1134 * rights where the control fields are now.
1137 orecvmsg(struct thread *td, struct orecvmsg_args *uap)
1143 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1146 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1149 msg.msg_flags = uap->flags | MSG_COMPAT;
1151 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1152 if (msg.msg_controllen && error == 0)
1153 error = copyout(&msg.msg_controllen,
1154 &uap->msg->msg_accrightslen, sizeof (int));
1161 sys_recvmsg(struct thread *td, struct recvmsg_args *uap)
1164 struct iovec *uiov, *iov;
1167 error = copyin(uap->msg, &msg, sizeof (msg));
1170 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1173 msg.msg_flags = uap->flags;
1174 #ifdef COMPAT_OLDSOCK
1175 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
1176 msg.msg_flags &= ~MSG_COMPAT;
1180 error = recvit(td, uap->s, &msg, NULL);
1183 error = copyout(&msg, uap->msg, sizeof(msg));
1190 sys_shutdown(struct thread *td, struct shutdown_args *uap)
1193 return (kern_shutdown(td, uap->s, uap->how));
1197 kern_shutdown(struct thread *td, int s, int how)
1204 error = getsock_cap(td, s, &cap_shutdown_rights,
1208 error = soshutdown(so, how);
1210 * Previous versions did not return ENOTCONN, but 0 in
1211 * case the socket was not connected. Some important
1212 * programs like syslogd up to r279016, 2015-02-19,
1213 * still depend on this behavior.
1215 if (error == ENOTCONN &&
1216 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1224 sys_setsockopt(struct thread *td, struct setsockopt_args *uap)
1227 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1228 uap->val, UIO_USERSPACE, uap->valsize));
1232 kern_setsockopt(struct thread *td, int s, int level, int name, const void *val,
1233 enum uio_seg valseg, socklen_t valsize)
1237 struct sockopt sopt;
1240 if (val == NULL && valsize != 0)
1242 if ((int)valsize < 0)
1245 sopt.sopt_dir = SOPT_SET;
1246 sopt.sopt_level = level;
1247 sopt.sopt_name = name;
1248 sopt.sopt_val = __DECONST(void *, val);
1249 sopt.sopt_valsize = valsize;
1255 sopt.sopt_td = NULL;
1258 panic("kern_setsockopt called with bad valseg");
1262 error = getsock_cap(td, s, &cap_setsockopt_rights,
1266 error = sosetopt(so, &sopt);
1273 sys_getsockopt(struct thread *td, struct getsockopt_args *uap)
1279 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1284 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1285 uap->val, UIO_USERSPACE, &valsize);
1288 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1293 * Kernel version of getsockopt.
1294 * optval can be a userland or userspace. optlen is always a kernel pointer.
1297 kern_getsockopt(struct thread *td, int s, int level, int name, void *val,
1298 enum uio_seg valseg, socklen_t *valsize)
1302 struct sockopt sopt;
1307 if ((int)*valsize < 0)
1310 sopt.sopt_dir = SOPT_GET;
1311 sopt.sopt_level = level;
1312 sopt.sopt_name = name;
1313 sopt.sopt_val = val;
1314 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1320 sopt.sopt_td = NULL;
1323 panic("kern_getsockopt called with bad valseg");
1327 error = getsock_cap(td, s, &cap_getsockopt_rights,
1331 error = sogetopt(so, &sopt);
1332 *valsize = sopt.sopt_valsize;
1339 user_getsockname(struct thread *td, int fdes, struct sockaddr *asa,
1340 socklen_t *alen, bool compat)
1342 struct sockaddr *sa;
1346 error = copyin(alen, &len, sizeof(len));
1350 error = kern_getsockname(td, fdes, &sa, &len);
1355 #ifdef COMPAT_OLDSOCK
1356 if (compat && SV_PROC_FLAG(td->td_proc, SV_AOUT))
1357 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1359 error = copyout(sa, asa, len);
1363 error = copyout(&len, alen, sizeof(len));
1368 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1377 error = getsock_cap(td, fd, &cap_getsockname_rights,
1383 CURVNET_SET(so->so_vnet);
1384 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1391 len = MIN(*alen, (*sa)->sa_len);
1394 if (KTRPOINT(td, KTR_STRUCT))
1399 if (error != 0 && *sa != NULL) {
1400 free(*sa, M_SONAME);
1407 sys_getsockname(struct thread *td, struct getsockname_args *uap)
1409 return (user_getsockname(td, uap->fdes, uap->asa, uap->alen, false));
1412 #ifdef COMPAT_OLDSOCK
1414 ogetsockname(struct thread *td, struct getsockname_args *uap)
1416 return (user_getsockname(td, uap->fdes, uap->asa, uap->alen, true));
1418 #endif /* COMPAT_OLDSOCK */
1421 user_getpeername(struct thread *td, int fdes, struct sockaddr *asa,
1422 socklen_t *alen, int compat)
1424 struct sockaddr *sa;
1428 error = copyin(alen, &len, sizeof (len));
1432 error = kern_getpeername(td, fdes, &sa, &len);
1437 #ifdef COMPAT_OLDSOCK
1438 if (compat && SV_PROC_FLAG(td->td_proc, SV_AOUT))
1439 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1441 error = copyout(sa, asa, len);
1445 error = copyout(&len, alen, sizeof(len));
1450 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1459 error = getsock_cap(td, fd, &cap_getpeername_rights,
1464 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1469 CURVNET_SET(so->so_vnet);
1470 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1477 len = MIN(*alen, (*sa)->sa_len);
1480 if (KTRPOINT(td, KTR_STRUCT))
1484 if (error != 0 && *sa != NULL) {
1485 free(*sa, M_SONAME);
1494 sys_getpeername(struct thread *td, struct getpeername_args *uap)
1496 return (user_getpeername(td, uap->fdes, uap->asa, uap->alen, 0));
1499 #ifdef COMPAT_OLDSOCK
1501 ogetpeername(struct thread *td, struct ogetpeername_args *uap)
1503 return (user_getpeername(td, uap->fdes, uap->asa, uap->alen, 1));
1505 #endif /* COMPAT_OLDSOCK */
1508 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1510 struct sockaddr *sa;
1514 if (buflen > MLEN) {
1515 #ifdef COMPAT_OLDSOCK
1516 if (type == MT_SONAME && buflen <= 112 &&
1517 SV_CURPROC_FLAG(SV_AOUT))
1518 buflen = MLEN; /* unix domain compat. hack */
1521 if (buflen > MCLBYTES)
1524 m = m_get2(buflen, M_WAITOK, type, 0);
1526 error = copyin(buf, mtod(m, void *), buflen);
1531 if (type == MT_SONAME) {
1532 sa = mtod(m, struct sockaddr *);
1534 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1535 if (sa->sa_family == 0 && sa->sa_len < AF_MAX &&
1536 SV_CURPROC_FLAG(SV_AOUT))
1537 sa->sa_family = sa->sa_len;
1539 sa->sa_len = buflen;
1546 getsockaddr(struct sockaddr **namp, const struct sockaddr *uaddr, size_t len)
1548 struct sockaddr *sa;
1551 if (len > SOCK_MAXADDRLEN)
1552 return (ENAMETOOLONG);
1553 if (len < offsetof(struct sockaddr, sa_data[0]))
1555 sa = malloc(len, M_SONAME, M_WAITOK);
1556 error = copyin(uaddr, sa, len);
1560 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1561 if (sa->sa_family == 0 && sa->sa_len < AF_MAX &&
1562 SV_CURPROC_FLAG(SV_AOUT))
1563 sa->sa_family = sa->sa_len;
1572 * Dispose of externalized rights from an SCM_RIGHTS message. This function
1573 * should be used in error or truncation cases to avoid leaking file descriptors
1574 * into the recipient's (the current thread's) table.
1577 m_dispose_extcontrolm(struct mbuf *m)
1582 socklen_t clen, datalen;
1583 int error, fd, *fds, nfd;
1586 for (; m != NULL; m = m->m_next) {
1587 if (m->m_type != MT_EXTCONTROL)
1589 cm = mtod(m, struct cmsghdr *);
1592 if (clen < sizeof(*cm))
1593 panic("%s: truncated mbuf %p", __func__, m);
1594 datalen = CMSG_SPACE(cm->cmsg_len - CMSG_SPACE(0));
1596 panic("%s: truncated mbuf %p", __func__, m);
1598 if (cm->cmsg_level == SOL_SOCKET &&
1599 cm->cmsg_type == SCM_RIGHTS) {
1600 fds = (int *)CMSG_DATA(cm);
1601 nfd = (cm->cmsg_len - CMSG_SPACE(0)) /
1606 error = fget(td, fd, &cap_no_rights,
1609 fdclose(td, fp, fd);
1615 cm = (struct cmsghdr *)((uint8_t *)cm + datalen);
1617 m_chtype(m, MT_CONTROL);