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 #include "opt_capsicum.h"
37 #include "opt_inet6.h"
38 #include "opt_ktrace.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/capsicum.h>
43 #include <sys/kernel.h>
45 #include <sys/mutex.h>
46 #include <sys/sysproto.h>
47 #include <sys/malloc.h>
48 #include <sys/filedesc.h>
50 #include <sys/filio.h>
53 #include <sys/protosw.h>
54 #include <sys/rwlock.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/syscallsubr.h>
59 #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, struct filecaps *havecapsp)
96 error = fget_cap(td, fd, rightsp, &fp, havecapsp);
97 if (__predict_false(error != 0))
99 if (__predict_false(fp->f_type != DTYPE_SOCKET)) {
101 if (havecapsp != NULL)
102 filecaps_free(havecapsp);
110 getsock(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
115 error = fget_unlocked(td, fd, rightsp, &fp);
116 if (__predict_false(error != 0))
118 if (__predict_false(fp->f_type != DTYPE_SOCKET)) {
127 * System call interface to the socket abstraction.
129 #if defined(COMPAT_43)
130 #define COMPAT_OLDSOCK
134 sys_socket(struct thread *td, struct socket_args *uap)
137 return (kern_socket(td, uap->domain, uap->type, uap->protocol));
141 kern_socket(struct thread *td, int domain, int type, int protocol)
145 int fd, error, oflag, fflag;
147 AUDIT_ARG_SOCKET(domain, type, protocol);
151 if ((type & SOCK_CLOEXEC) != 0) {
152 type &= ~SOCK_CLOEXEC;
155 if ((type & SOCK_NONBLOCK) != 0) {
156 type &= ~SOCK_NONBLOCK;
161 error = mac_socket_check_create(td->td_ucred, domain, type, protocol);
165 error = falloc(td, &fp, &fd, oflag);
168 /* An extra reference on `fp' has been held for us by falloc(). */
169 error = socreate(domain, &so, type, protocol, td->td_ucred, td);
173 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
174 if ((fflag & FNONBLOCK) != 0)
175 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
176 td->td_retval[0] = fd;
183 sys_bind(struct thread *td, struct bind_args *uap)
188 error = getsockaddr(&sa, uap->name, uap->namelen);
190 error = kern_bindat(td, AT_FDCWD, uap->s, sa);
197 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
203 #ifdef CAPABILITY_MODE
204 if (IN_CAPABILITY_MODE(td) && (dirfd == AT_FDCWD))
209 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
210 error = getsock(td, fd, &cap_bind_rights, &fp);
215 if (KTRPOINT(td, KTR_STRUCT))
219 error = mac_socket_check_bind(td->td_ucred, so, sa);
222 if (dirfd == AT_FDCWD)
223 error = sobind(so, sa, td);
225 error = sobindat(dirfd, so, sa, td);
234 sys_bindat(struct thread *td, struct bindat_args *uap)
239 error = getsockaddr(&sa, uap->name, uap->namelen);
241 error = kern_bindat(td, uap->fd, uap->s, sa);
248 sys_listen(struct thread *td, struct listen_args *uap)
251 return (kern_listen(td, uap->s, uap->backlog));
255 kern_listen(struct thread *td, int s, int backlog)
262 error = getsock(td, s, &cap_listen_rights, &fp);
266 error = mac_socket_check_listen(td->td_ucred, so);
269 error = solisten(so, backlog, td);
279 accept1(struct thread *td, int s, struct sockaddr *uname, socklen_t *anamelen,
282 struct sockaddr *name;
288 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
290 error = copyin(anamelen, &namelen, sizeof (namelen));
294 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
299 #ifdef COMPAT_OLDSOCK
300 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) &&
301 (flags & ACCEPT4_COMPAT) != 0)
302 ((struct osockaddr *)name)->sa_family =
305 error = copyout(name, uname, namelen);
307 error = copyout(&namelen, anamelen,
310 fdclose(td, fp, td->td_retval[0]);
312 free(name, M_SONAME);
317 kern_accept(struct thread *td, int s, struct sockaddr **name,
318 socklen_t *namelen, struct file **fp)
320 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
324 kern_accept4(struct thread *td, int s, struct sockaddr **name,
325 socklen_t *namelen, int flags, struct file **fp)
327 struct file *headfp, *nfp = NULL;
328 struct sockaddr *sa = NULL;
329 struct socket *head, *so;
330 struct filecaps fcaps;
339 error = getsock_cap(td, s, &cap_accept_rights,
343 fflag = atomic_load_int(&headfp->f_flag);
344 head = headfp->f_data;
345 if (!SOLISTENING(head)) {
350 error = mac_socket_check_accept(td->td_ucred, head);
354 error = falloc_caps(td, &nfp, &fd,
355 (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0, &fcaps);
359 if (!SOLISTENING(head)) {
365 error = solisten_dequeue(head, &so, flags);
369 /* An extra reference on `nfp' has been held for us by falloc(). */
370 td->td_retval[0] = fd;
372 /* Connection has been removed from the listen queue. */
373 KNOTE_UNLOCKED(&head->so_rdsel.si_note, 0);
375 if (flags & ACCEPT4_INHERIT) {
376 pgid = fgetown(&head->so_sigio);
378 fsetown(pgid, &so->so_sigio);
380 fflag &= ~(FNONBLOCK | FASYNC);
381 if (flags & SOCK_NONBLOCK)
385 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
386 /* Sync socket nonblocking/async state with file flags */
387 tmp = fflag & FNONBLOCK;
388 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
389 tmp = fflag & FASYNC;
390 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
391 error = soaccept(so, &sa);
399 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
401 /* check sa_len before it is destroyed */
402 if (*namelen > sa->sa_len)
403 *namelen = sa->sa_len;
405 if (KTRPOINT(td, KTR_STRUCT))
415 * close the new descriptor, assuming someone hasn't ripped it
419 fdclose(td, nfp, fd);
422 * Release explicitly held references before returning. We return
423 * a reference on nfp to the caller on success if they request it.
427 filecaps_free(&fcaps);
442 sys_accept(struct thread *td, struct accept_args *uap)
445 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
449 sys_accept4(struct thread *td, struct accept4_args *uap)
452 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
455 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
458 #ifdef COMPAT_OLDSOCK
460 oaccept(struct thread *td, struct oaccept_args *uap)
463 return (accept1(td, uap->s, uap->name, uap->anamelen,
464 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
466 #endif /* COMPAT_OLDSOCK */
469 sys_connect(struct thread *td, struct connect_args *uap)
474 error = getsockaddr(&sa, uap->name, uap->namelen);
476 error = kern_connectat(td, AT_FDCWD, uap->s, sa);
483 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
489 #ifdef CAPABILITY_MODE
490 if (IN_CAPABILITY_MODE(td) && (dirfd == AT_FDCWD))
495 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
496 error = getsock(td, fd, &cap_connect_rights, &fp);
500 if (so->so_state & SS_ISCONNECTING) {
505 if (KTRPOINT(td, KTR_STRUCT))
509 error = mac_socket_check_connect(td->td_ucred, so, sa);
513 error = soconnectat(dirfd, so, sa, td);
516 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
521 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
522 error = msleep(&so->so_timeo, &so->so_lock, PSOCK | PCATCH,
528 error = so->so_error;
533 if (error == ERESTART)
541 sys_connectat(struct thread *td, struct connectat_args *uap)
546 error = getsockaddr(&sa, uap->name, uap->namelen);
548 error = kern_connectat(td, uap->fd, uap->s, sa);
555 kern_socketpair(struct thread *td, int domain, int type, int protocol,
558 struct file *fp1, *fp2;
559 struct socket *so1, *so2;
560 int fd, error, oflag, fflag;
562 AUDIT_ARG_SOCKET(domain, type, protocol);
566 if ((type & SOCK_CLOEXEC) != 0) {
567 type &= ~SOCK_CLOEXEC;
570 if ((type & SOCK_NONBLOCK) != 0) {
571 type &= ~SOCK_NONBLOCK;
575 /* We might want to have a separate check for socket pairs. */
576 error = mac_socket_check_create(td->td_ucred, domain, type,
581 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
584 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
587 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
588 error = falloc(td, &fp1, &fd, oflag);
592 fp1->f_data = so1; /* so1 already has ref count */
593 error = falloc(td, &fp2, &fd, oflag);
596 fp2->f_data = so2; /* so2 already has ref count */
598 error = soconnect2(so1, so2);
601 if (type == SOCK_DGRAM) {
603 * Datagram socket connection is asymmetric.
605 error = soconnect2(so2, so1);
608 } else if (so1->so_proto->pr_flags & PR_CONNREQUIRED) {
609 struct unpcb *unp, *unp2;
610 unp = sotounpcb(so1);
611 unp2 = sotounpcb(so2);
613 * No need to lock the unps, because the sockets are brand-new.
614 * No other threads can be using them yet
616 unp_copy_peercred(td, unp, unp2, unp);
618 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
620 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
622 if ((fflag & FNONBLOCK) != 0) {
623 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
624 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
630 fdclose(td, fp2, rsv[1]);
633 fdclose(td, fp1, rsv[0]);
645 sys_socketpair(struct thread *td, struct socketpair_args *uap)
649 error = kern_socketpair(td, uap->domain, uap->type,
653 error = copyout(sv, uap->rsv, 2 * sizeof(int));
655 (void)kern_close(td, sv[0]);
656 (void)kern_close(td, sv[1]);
662 sendit(struct thread *td, int s, struct msghdr *mp, int flags)
664 struct mbuf *control;
668 #ifdef CAPABILITY_MODE
669 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
673 if (mp->msg_name != NULL) {
674 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
684 if (mp->msg_control) {
685 if (mp->msg_controllen < sizeof(struct cmsghdr)
686 #ifdef COMPAT_OLDSOCK
687 && (mp->msg_flags != MSG_COMPAT ||
688 !SV_PROC_FLAG(td->td_proc, SV_AOUT))
694 error = sockargs(&control, mp->msg_control,
695 mp->msg_controllen, MT_CONTROL);
698 #ifdef COMPAT_OLDSOCK
699 if (mp->msg_flags == MSG_COMPAT &&
700 SV_PROC_FLAG(td->td_proc, SV_AOUT)) {
703 M_PREPEND(control, sizeof(*cm), M_WAITOK);
704 cm = mtod(control, struct cmsghdr *);
705 cm->cmsg_len = control->m_len;
706 cm->cmsg_level = SOL_SOCKET;
707 cm->cmsg_type = SCM_RIGHTS;
714 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
722 kern_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
723 struct mbuf *control, enum uio_seg segflg)
729 cap_rights_t *rights;
731 struct uio *ktruio = NULL;
737 rights = &cap_send_rights;
738 if (mp->msg_name != NULL) {
739 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
740 rights = &cap_send_connect_rights;
742 error = getsock(td, s, rights, &fp);
747 so = (struct socket *)fp->f_data;
750 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
751 ktrsockaddr(mp->msg_name);
754 if (mp->msg_name != NULL) {
755 error = mac_socket_check_connect(td->td_ucred, so,
762 error = mac_socket_check_send(td->td_ucred, so);
769 auio.uio_iov = mp->msg_iov;
770 auio.uio_iovcnt = mp->msg_iovlen;
771 auio.uio_segflg = segflg;
772 auio.uio_rw = UIO_WRITE;
774 auio.uio_offset = 0; /* XXX */
777 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
778 if ((auio.uio_resid += iov->iov_len) < 0) {
785 if (KTRPOINT(td, KTR_GENIO))
786 ktruio = cloneuio(&auio);
788 len = auio.uio_resid;
789 error = sousrsend(so, mp->msg_name, &auio, control, flags, NULL);
791 td->td_retval[0] = len - auio.uio_resid;
793 if (ktruio != NULL) {
794 ktruio->uio_resid = td->td_retval[0];
795 ktrgenio(s, UIO_WRITE, ktruio, error);
804 sys_sendto(struct thread *td, struct sendto_args *uap)
809 msg.msg_name = __DECONST(void *, uap->to);
810 msg.msg_namelen = uap->tolen;
814 #ifdef COMPAT_OLDSOCK
815 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
818 aiov.iov_base = __DECONST(void *, uap->buf);
819 aiov.iov_len = uap->len;
820 return (sendit(td, uap->s, &msg, uap->flags));
823 #ifdef COMPAT_OLDSOCK
825 osend(struct thread *td, struct osend_args *uap)
834 aiov.iov_base = __DECONST(void *, uap->buf);
835 aiov.iov_len = uap->len;
838 return (sendit(td, uap->s, &msg, uap->flags));
842 osendmsg(struct thread *td, struct osendmsg_args *uap)
848 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
851 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
855 msg.msg_flags = MSG_COMPAT;
856 error = sendit(td, uap->s, &msg, uap->flags);
863 sys_sendmsg(struct thread *td, struct sendmsg_args *uap)
869 error = copyin(uap->msg, &msg, sizeof (msg));
872 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
876 #ifdef COMPAT_OLDSOCK
877 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
880 error = sendit(td, uap->s, &msg, uap->flags);
886 kern_recvit(struct thread *td, int s, struct msghdr *mp, enum uio_seg fromseg,
887 struct mbuf **controlp)
891 struct mbuf *control, *m;
895 struct sockaddr *fromsa = NULL;
897 struct uio *ktruio = NULL;
902 if (controlp != NULL)
906 error = getsock(td, s, &cap_recv_rights, &fp);
912 error = mac_socket_check_receive(td->td_ucred, so);
919 auio.uio_iov = mp->msg_iov;
920 auio.uio_iovcnt = mp->msg_iovlen;
921 auio.uio_segflg = UIO_USERSPACE;
922 auio.uio_rw = UIO_READ;
924 auio.uio_offset = 0; /* XXX */
927 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
928 if ((auio.uio_resid += iov->iov_len) < 0) {
934 if (KTRPOINT(td, KTR_GENIO))
935 ktruio = cloneuio(&auio);
938 len = auio.uio_resid;
939 error = soreceive(so, &fromsa, &auio, NULL,
940 (mp->msg_control || controlp) ? &control : NULL,
943 if (auio.uio_resid != len && (error == ERESTART ||
944 error == EINTR || error == EWOULDBLOCK))
948 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
950 if (ktruio != NULL) {
951 ktruio->uio_resid = len - auio.uio_resid;
952 ktrgenio(s, UIO_READ, ktruio, error);
957 td->td_retval[0] = len - auio.uio_resid;
959 len = mp->msg_namelen;
960 if (len <= 0 || fromsa == NULL)
963 /* save sa_len before it is destroyed by MSG_COMPAT */
964 len = MIN(len, fromsa->sa_len);
965 #ifdef COMPAT_OLDSOCK
966 if ((mp->msg_flags & MSG_COMPAT) != 0 &&
967 SV_PROC_FLAG(td->td_proc, SV_AOUT))
968 ((struct osockaddr *)fromsa)->sa_family =
971 if (fromseg == UIO_USERSPACE) {
972 error = copyout(fromsa, mp->msg_name,
977 bcopy(fromsa, mp->msg_name, len);
979 mp->msg_namelen = len;
981 if (mp->msg_control && controlp == NULL) {
982 #ifdef COMPAT_OLDSOCK
984 * We assume that old recvmsg calls won't receive access
985 * rights and other control info, esp. as control info
986 * is always optional and those options didn't exist in 4.3.
987 * If we receive rights, trim the cmsghdr; anything else
990 if (control && (mp->msg_flags & MSG_COMPAT) != 0 &&
991 SV_PROC_FLAG(td->td_proc, SV_AOUT)) {
992 if (mtod(control, struct cmsghdr *)->cmsg_level !=
994 mtod(control, struct cmsghdr *)->cmsg_type !=
996 mp->msg_controllen = 0;
999 control->m_len -= sizeof (struct cmsghdr);
1000 control->m_data += sizeof (struct cmsghdr);
1003 ctlbuf = mp->msg_control;
1004 len = mp->msg_controllen;
1005 mp->msg_controllen = 0;
1006 for (m = control; m != NULL && len >= m->m_len; m = m->m_next) {
1007 if ((error = copyout(mtod(m, caddr_t), ctlbuf,
1013 mp->msg_controllen += m->m_len;
1016 mp->msg_flags |= MSG_CTRUNC;
1017 m_dispose_extcontrolm(m);
1023 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1024 ktrsockaddr(fromsa);
1026 free(fromsa, M_SONAME);
1028 if (error == 0 && controlp != NULL)
1029 *controlp = control;
1030 else if (control != NULL) {
1032 m_dispose_extcontrolm(control);
1040 recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp)
1044 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1047 if (namelenp != NULL) {
1048 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1049 #ifdef COMPAT_OLDSOCK
1050 if ((mp->msg_flags & MSG_COMPAT) != 0 &&
1051 SV_PROC_FLAG(td->td_proc, SV_AOUT))
1052 error = 0; /* old recvfrom didn't check */
1059 kern_recvfrom(struct thread *td, int s, void *buf, size_t len, int flags,
1060 struct sockaddr *from, socklen_t *fromlenaddr)
1066 if (fromlenaddr != NULL) {
1067 error = copyin(fromlenaddr, &msg.msg_namelen,
1068 sizeof (msg.msg_namelen));
1072 msg.msg_namelen = 0;
1074 msg.msg_name = from;
1075 msg.msg_iov = &aiov;
1077 aiov.iov_base = buf;
1079 msg.msg_control = 0;
1080 msg.msg_flags = flags;
1081 error = recvit(td, s, &msg, fromlenaddr);
1087 sys_recvfrom(struct thread *td, struct recvfrom_args *uap)
1089 return (kern_recvfrom(td, uap->s, uap->buf, uap->len,
1090 uap->flags, uap->from, uap->fromlenaddr));
1094 #ifdef COMPAT_OLDSOCK
1096 orecvfrom(struct thread *td, struct orecvfrom_args *uap)
1098 return (kern_recvfrom(td, uap->s, uap->buf, uap->len,
1099 uap->flags | MSG_COMPAT, uap->from, uap->fromlenaddr));
1103 #ifdef COMPAT_OLDSOCK
1105 orecv(struct thread *td, struct orecv_args *uap)
1111 msg.msg_namelen = 0;
1112 msg.msg_iov = &aiov;
1114 aiov.iov_base = uap->buf;
1115 aiov.iov_len = uap->len;
1116 msg.msg_control = 0;
1117 msg.msg_flags = uap->flags;
1118 return (recvit(td, uap->s, &msg, NULL));
1122 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1123 * overlays the new one, missing only the flags, and with the (old) access
1124 * rights where the control fields are now.
1127 orecvmsg(struct thread *td, struct orecvmsg_args *uap)
1133 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1136 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1139 msg.msg_flags = uap->flags | MSG_COMPAT;
1141 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1142 if (msg.msg_controllen && error == 0)
1143 error = copyout(&msg.msg_controllen,
1144 &uap->msg->msg_accrightslen, sizeof (int));
1151 sys_recvmsg(struct thread *td, struct recvmsg_args *uap)
1154 struct iovec *uiov, *iov;
1157 error = copyin(uap->msg, &msg, sizeof (msg));
1160 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1163 msg.msg_flags = uap->flags;
1164 #ifdef COMPAT_OLDSOCK
1165 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
1166 msg.msg_flags &= ~MSG_COMPAT;
1170 error = recvit(td, uap->s, &msg, NULL);
1173 error = copyout(&msg, uap->msg, sizeof(msg));
1180 sys_shutdown(struct thread *td, struct shutdown_args *uap)
1183 return (kern_shutdown(td, uap->s, uap->how));
1187 kern_shutdown(struct thread *td, int s, int how)
1194 error = getsock(td, s, &cap_shutdown_rights, &fp);
1197 error = soshutdown(so, how);
1199 * Previous versions did not return ENOTCONN, but 0 in
1200 * case the socket was not connected. Some important
1201 * programs like syslogd up to r279016, 2015-02-19,
1202 * still depend on this behavior.
1204 if (error == ENOTCONN &&
1205 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1213 sys_setsockopt(struct thread *td, struct setsockopt_args *uap)
1216 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1217 uap->val, UIO_USERSPACE, uap->valsize));
1221 kern_setsockopt(struct thread *td, int s, int level, int name, const void *val,
1222 enum uio_seg valseg, socklen_t valsize)
1226 struct sockopt sopt;
1229 if (val == NULL && valsize != 0)
1231 if ((int)valsize < 0)
1234 sopt.sopt_dir = SOPT_SET;
1235 sopt.sopt_level = level;
1236 sopt.sopt_name = name;
1237 sopt.sopt_val = __DECONST(void *, val);
1238 sopt.sopt_valsize = valsize;
1244 sopt.sopt_td = NULL;
1247 panic("kern_setsockopt called with bad valseg");
1251 error = getsock(td, s, &cap_setsockopt_rights, &fp);
1254 error = sosetopt(so, &sopt);
1261 sys_getsockopt(struct thread *td, struct getsockopt_args *uap)
1267 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1272 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1273 uap->val, UIO_USERSPACE, &valsize);
1276 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1281 * Kernel version of getsockopt.
1282 * optval can be a userland or userspace. optlen is always a kernel pointer.
1285 kern_getsockopt(struct thread *td, int s, int level, int name, void *val,
1286 enum uio_seg valseg, socklen_t *valsize)
1290 struct sockopt sopt;
1295 if ((int)*valsize < 0)
1298 sopt.sopt_dir = SOPT_GET;
1299 sopt.sopt_level = level;
1300 sopt.sopt_name = name;
1301 sopt.sopt_val = val;
1302 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1308 sopt.sopt_td = NULL;
1311 panic("kern_getsockopt called with bad valseg");
1315 error = getsock(td, s, &cap_getsockopt_rights, &fp);
1318 error = sogetopt(so, &sopt);
1319 *valsize = sopt.sopt_valsize;
1326 user_getsockname(struct thread *td, int fdes, struct sockaddr *asa,
1327 socklen_t *alen, bool compat)
1329 struct sockaddr *sa;
1333 error = copyin(alen, &len, sizeof(len));
1337 error = kern_getsockname(td, fdes, &sa, &len);
1342 #ifdef COMPAT_OLDSOCK
1343 if (compat && SV_PROC_FLAG(td->td_proc, SV_AOUT))
1344 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1346 error = copyout(sa, asa, len);
1350 error = copyout(&len, alen, sizeof(len));
1355 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1364 error = getsock(td, fd, &cap_getsockname_rights, &fp);
1369 CURVNET_SET(so->so_vnet);
1370 error = so->so_proto->pr_sockaddr(so, sa);
1377 len = MIN(*alen, (*sa)->sa_len);
1380 if (KTRPOINT(td, KTR_STRUCT))
1385 if (error != 0 && *sa != NULL) {
1386 free(*sa, M_SONAME);
1393 sys_getsockname(struct thread *td, struct getsockname_args *uap)
1395 return (user_getsockname(td, uap->fdes, uap->asa, uap->alen, false));
1398 #ifdef COMPAT_OLDSOCK
1400 ogetsockname(struct thread *td, struct ogetsockname_args *uap)
1402 return (user_getsockname(td, uap->fdes, uap->asa, uap->alen, true));
1404 #endif /* COMPAT_OLDSOCK */
1407 user_getpeername(struct thread *td, int fdes, struct sockaddr *asa,
1408 socklen_t *alen, bool compat)
1410 struct sockaddr *sa;
1414 error = copyin(alen, &len, sizeof (len));
1418 error = kern_getpeername(td, fdes, &sa, &len);
1423 #ifdef COMPAT_OLDSOCK
1424 if (compat && SV_PROC_FLAG(td->td_proc, SV_AOUT))
1425 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1427 error = copyout(sa, asa, len);
1431 error = copyout(&len, alen, sizeof(len));
1436 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1445 error = getsock(td, fd, &cap_getpeername_rights, &fp);
1449 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1454 CURVNET_SET(so->so_vnet);
1455 error = so->so_proto->pr_peeraddr(so, sa);
1462 len = MIN(*alen, (*sa)->sa_len);
1465 if (KTRPOINT(td, KTR_STRUCT))
1469 if (error != 0 && *sa != NULL) {
1470 free(*sa, M_SONAME);
1479 sys_getpeername(struct thread *td, struct getpeername_args *uap)
1481 return (user_getpeername(td, uap->fdes, uap->asa, uap->alen, false));
1484 #ifdef COMPAT_OLDSOCK
1486 ogetpeername(struct thread *td, struct ogetpeername_args *uap)
1488 return (user_getpeername(td, uap->fdes, uap->asa, uap->alen, true));
1490 #endif /* COMPAT_OLDSOCK */
1493 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1495 struct sockaddr *sa;
1499 if (buflen > MLEN) {
1500 #ifdef COMPAT_OLDSOCK
1501 if (type == MT_SONAME && buflen <= 112 &&
1502 SV_CURPROC_FLAG(SV_AOUT))
1503 buflen = MLEN; /* unix domain compat. hack */
1506 if (buflen > MCLBYTES)
1509 m = m_get2(buflen, M_WAITOK, type, 0);
1511 error = copyin(buf, mtod(m, void *), buflen);
1516 if (type == MT_SONAME) {
1517 sa = mtod(m, struct sockaddr *);
1519 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1520 if (sa->sa_family == 0 && sa->sa_len < AF_MAX &&
1521 SV_CURPROC_FLAG(SV_AOUT))
1522 sa->sa_family = sa->sa_len;
1524 sa->sa_len = buflen;
1531 getsockaddr(struct sockaddr **namp, const struct sockaddr *uaddr, size_t len)
1533 struct sockaddr *sa;
1536 if (len > SOCK_MAXADDRLEN)
1537 return (ENAMETOOLONG);
1538 if (len < offsetof(struct sockaddr, sa_data[0]))
1540 sa = malloc(len, M_SONAME, M_WAITOK);
1541 error = copyin(uaddr, sa, len);
1545 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1546 if (sa->sa_family == 0 && sa->sa_len < AF_MAX &&
1547 SV_CURPROC_FLAG(SV_AOUT))
1548 sa->sa_family = sa->sa_len;
1557 * Dispose of externalized rights from an SCM_RIGHTS message. This function
1558 * should be used in error or truncation cases to avoid leaking file descriptors
1559 * into the recipient's (the current thread's) table.
1562 m_dispose_extcontrolm(struct mbuf *m)
1567 socklen_t clen, datalen;
1568 int error, fd, *fds, nfd;
1571 for (; m != NULL; m = m->m_next) {
1572 if (m->m_type != MT_EXTCONTROL)
1574 cm = mtod(m, struct cmsghdr *);
1577 if (clen < sizeof(*cm))
1578 panic("%s: truncated mbuf %p", __func__, m);
1579 datalen = CMSG_SPACE(cm->cmsg_len - CMSG_SPACE(0));
1581 panic("%s: truncated mbuf %p", __func__, m);
1583 if (cm->cmsg_level == SOL_SOCKET &&
1584 cm->cmsg_type == SCM_RIGHTS) {
1585 fds = (int *)CMSG_DATA(cm);
1586 nfd = (cm->cmsg_len - CMSG_SPACE(0)) /
1591 error = fget(td, fd, &cap_no_rights,
1594 fdclose(td, fp, fd);
1600 cm = (struct cmsghdr *)((uint8_t *)cm + datalen);
1602 m_chtype(m, MT_CONTROL);