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>
62 #include <sys/unpcb.h>
64 #include <sys/ktrace.h>
66 #ifdef COMPAT_FREEBSD32
67 #include <compat/freebsd32/freebsd32_util.h>
72 #include <security/audit/audit.h>
73 #include <security/mac/mac_framework.h>
75 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
76 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
78 static int accept1(struct thread *td, int s, struct sockaddr *uname,
79 socklen_t *anamelen, int flags);
80 static int getsockname1(struct thread *td, struct getsockname_args *uap,
82 static int getpeername1(struct thread *td, struct getpeername_args *uap,
84 static int sockargs(struct mbuf **, char *, socklen_t, int);
87 * Convert a user file descriptor to a kernel file entry and check if required
88 * capability rights are present.
89 * If required copy of current set of capability rights is returned.
90 * A reference on the file entry is held upon returning.
93 getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
94 struct file **fpp, u_int *fflagp, struct filecaps *havecapsp)
99 error = fget_cap(td, fd, rightsp, &fp, havecapsp);
102 if (fp->f_type != DTYPE_SOCKET) {
104 if (havecapsp != NULL)
105 filecaps_free(havecapsp);
109 *fflagp = fp->f_flag;
115 * System call interface to the socket abstraction.
117 #if defined(COMPAT_43)
118 #define COMPAT_OLDSOCK
122 sys_socket(struct thread *td, struct socket_args *uap)
125 return (kern_socket(td, uap->domain, uap->type, uap->protocol));
129 kern_socket(struct thread *td, int domain, int type, int protocol)
133 int fd, error, oflag, fflag;
135 AUDIT_ARG_SOCKET(domain, type, protocol);
139 if ((type & SOCK_CLOEXEC) != 0) {
140 type &= ~SOCK_CLOEXEC;
143 if ((type & SOCK_NONBLOCK) != 0) {
144 type &= ~SOCK_NONBLOCK;
149 error = mac_socket_check_create(td->td_ucred, domain, type, protocol);
153 error = falloc(td, &fp, &fd, oflag);
156 /* An extra reference on `fp' has been held for us by falloc(). */
157 error = socreate(domain, &so, type, protocol, td->td_ucred, td);
161 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
162 if ((fflag & FNONBLOCK) != 0)
163 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
164 td->td_retval[0] = fd;
171 sys_bind(struct thread *td, struct bind_args *uap)
176 error = getsockaddr(&sa, uap->name, uap->namelen);
178 error = kern_bindat(td, AT_FDCWD, uap->s, sa);
185 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
191 #ifdef CAPABILITY_MODE
192 if (IN_CAPABILITY_MODE(td) && (dirfd == AT_FDCWD))
197 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
198 error = getsock_cap(td, fd, &cap_bind_rights,
204 if (KTRPOINT(td, KTR_STRUCT))
208 error = mac_socket_check_bind(td->td_ucred, so, sa);
211 if (dirfd == AT_FDCWD)
212 error = sobind(so, sa, td);
214 error = sobindat(dirfd, so, sa, td);
223 sys_bindat(struct thread *td, struct bindat_args *uap)
228 error = getsockaddr(&sa, uap->name, uap->namelen);
230 error = kern_bindat(td, uap->fd, uap->s, sa);
237 sys_listen(struct thread *td, struct listen_args *uap)
240 return (kern_listen(td, uap->s, uap->backlog));
244 kern_listen(struct thread *td, int s, int backlog)
251 error = getsock_cap(td, s, &cap_listen_rights,
256 error = mac_socket_check_listen(td->td_ucred, so);
259 error = solisten(so, backlog, td);
269 accept1(td, s, uname, anamelen, flags)
272 struct sockaddr *uname;
276 struct sockaddr *name;
282 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
284 error = copyin(anamelen, &namelen, sizeof (namelen));
288 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
293 if (error == 0 && uname != NULL) {
294 #ifdef COMPAT_OLDSOCK
295 if (flags & ACCEPT4_COMPAT)
296 ((struct osockaddr *)name)->sa_family =
299 error = copyout(name, uname, namelen);
302 error = copyout(&namelen, anamelen,
305 fdclose(td, fp, td->td_retval[0]);
307 free(name, M_SONAME);
312 kern_accept(struct thread *td, int s, struct sockaddr **name,
313 socklen_t *namelen, struct file **fp)
315 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
319 kern_accept4(struct thread *td, int s, struct sockaddr **name,
320 socklen_t *namelen, int flags, struct file **fp)
322 struct file *headfp, *nfp = NULL;
323 struct sockaddr *sa = NULL;
324 struct socket *head, *so;
325 struct filecaps fcaps;
334 error = getsock_cap(td, s, &cap_accept_rights,
335 &headfp, &fflag, &fcaps);
338 head = headfp->f_data;
339 if ((head->so_options & SO_ACCEPTCONN) == 0) {
344 error = mac_socket_check_accept(td->td_ucred, head);
348 error = falloc_caps(td, &nfp, &fd,
349 (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0, &fcaps);
353 if (!SOLISTENING(head)) {
359 error = solisten_dequeue(head, &so, flags);
363 /* An extra reference on `nfp' has been held for us by falloc(). */
364 td->td_retval[0] = fd;
366 /* Connection has been removed from the listen queue. */
367 KNOTE_UNLOCKED(&head->so_rdsel.si_note, 0);
369 if (flags & ACCEPT4_INHERIT) {
370 pgid = fgetown(&head->so_sigio);
372 fsetown(pgid, &so->so_sigio);
374 fflag &= ~(FNONBLOCK | FASYNC);
375 if (flags & SOCK_NONBLOCK)
379 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
380 /* Sync socket nonblocking/async state with file flags */
381 tmp = fflag & FNONBLOCK;
382 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
383 tmp = fflag & FASYNC;
384 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
385 error = soaccept(so, &sa);
393 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
395 /* check sa_len before it is destroyed */
396 if (*namelen > sa->sa_len)
397 *namelen = sa->sa_len;
399 if (KTRPOINT(td, KTR_STRUCT))
409 * close the new descriptor, assuming someone hasn't ripped it
413 fdclose(td, nfp, fd);
416 * Release explicitly held references before returning. We return
417 * a reference on nfp to the caller on success if they request it.
421 filecaps_free(&fcaps);
438 struct accept_args *uap;
441 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
447 struct accept4_args *uap;
450 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
453 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
456 #ifdef COMPAT_OLDSOCK
458 oaccept(struct thread *td, struct oaccept_args *uap)
461 return (accept1(td, uap->s, uap->name, uap->anamelen,
462 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
464 #endif /* COMPAT_OLDSOCK */
467 sys_connect(struct thread *td, struct connect_args *uap)
472 error = getsockaddr(&sa, uap->name, uap->namelen);
474 error = kern_connectat(td, AT_FDCWD, uap->s, sa);
481 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
485 int error, interrupted = 0;
487 #ifdef CAPABILITY_MODE
488 if (IN_CAPABILITY_MODE(td) && (dirfd == AT_FDCWD))
493 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
494 error = getsock_cap(td, fd, &cap_connect_rights,
499 if (so->so_state & SS_ISCONNECTING) {
504 if (KTRPOINT(td, KTR_STRUCT))
508 error = mac_socket_check_connect(td->td_ucred, so, sa);
512 if (dirfd == AT_FDCWD)
513 error = soconnect(so, sa, td);
515 error = soconnectat(dirfd, so, sa, td);
518 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
523 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
524 error = msleep(&so->so_timeo, &so->so_lock, PSOCK | PCATCH,
527 if (error == EINTR || error == ERESTART)
533 error = so->so_error;
539 so->so_state &= ~SS_ISCONNECTING;
540 if (error == ERESTART)
548 sys_connectat(struct thread *td, struct connectat_args *uap)
553 error = getsockaddr(&sa, uap->name, uap->namelen);
555 error = kern_connectat(td, uap->fd, uap->s, sa);
562 kern_socketpair(struct thread *td, int domain, int type, int protocol,
565 struct file *fp1, *fp2;
566 struct socket *so1, *so2;
567 int fd, error, oflag, fflag;
569 AUDIT_ARG_SOCKET(domain, type, protocol);
573 if ((type & SOCK_CLOEXEC) != 0) {
574 type &= ~SOCK_CLOEXEC;
577 if ((type & SOCK_NONBLOCK) != 0) {
578 type &= ~SOCK_NONBLOCK;
582 /* We might want to have a separate check for socket pairs. */
583 error = mac_socket_check_create(td->td_ucred, domain, type,
588 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
591 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
594 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
595 error = falloc(td, &fp1, &fd, oflag);
599 fp1->f_data = so1; /* so1 already has ref count */
600 error = falloc(td, &fp2, &fd, oflag);
603 fp2->f_data = so2; /* so2 already has ref count */
605 error = soconnect2(so1, so2);
608 if (type == SOCK_DGRAM) {
610 * Datagram socket connection is asymmetric.
612 error = soconnect2(so2, so1);
615 } else if (so1->so_proto->pr_flags & PR_CONNREQUIRED) {
616 struct unpcb *unp, *unp2;
617 unp = sotounpcb(so1);
618 unp2 = sotounpcb(so2);
620 * No need to lock the unps, because the sockets are brand-new.
621 * No other threads can be using them yet
623 unp_copy_peercred(td, unp, unp2, unp);
625 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
627 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
629 if ((fflag & FNONBLOCK) != 0) {
630 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
631 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
637 fdclose(td, fp2, rsv[1]);
640 fdclose(td, fp1, rsv[0]);
652 sys_socketpair(struct thread *td, struct socketpair_args *uap)
656 error = kern_socketpair(td, uap->domain, uap->type,
660 error = copyout(sv, uap->rsv, 2 * sizeof(int));
662 (void)kern_close(td, sv[0]);
663 (void)kern_close(td, sv[1]);
669 sendit(struct thread *td, int s, struct msghdr *mp, int flags)
671 struct mbuf *control;
675 #ifdef CAPABILITY_MODE
676 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
680 if (mp->msg_name != NULL) {
681 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
691 if (mp->msg_control) {
692 if (mp->msg_controllen < sizeof(struct cmsghdr)
693 #ifdef COMPAT_OLDSOCK
694 && mp->msg_flags != MSG_COMPAT
700 error = sockargs(&control, mp->msg_control,
701 mp->msg_controllen, MT_CONTROL);
704 #ifdef COMPAT_OLDSOCK
705 if (mp->msg_flags == MSG_COMPAT) {
708 M_PREPEND(control, sizeof(*cm), M_WAITOK);
709 cm = mtod(control, struct cmsghdr *);
710 cm->cmsg_len = control->m_len;
711 cm->cmsg_level = SOL_SOCKET;
712 cm->cmsg_type = SCM_RIGHTS;
719 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
727 kern_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
728 struct mbuf *control, enum uio_seg segflg)
734 cap_rights_t *rights;
736 struct uio *ktruio = NULL;
742 rights = &cap_send_rights;
743 if (mp->msg_name != NULL) {
744 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
745 rights = &cap_send_connect_rights;
747 error = getsock_cap(td, s, rights, &fp, NULL, NULL);
752 so = (struct socket *)fp->f_data;
755 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
756 ktrsockaddr(mp->msg_name);
759 if (mp->msg_name != NULL) {
760 error = mac_socket_check_connect(td->td_ucred, so,
767 error = mac_socket_check_send(td->td_ucred, so);
774 auio.uio_iov = mp->msg_iov;
775 auio.uio_iovcnt = mp->msg_iovlen;
776 auio.uio_segflg = segflg;
777 auio.uio_rw = UIO_WRITE;
779 auio.uio_offset = 0; /* XXX */
782 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
783 if ((auio.uio_resid += iov->iov_len) < 0) {
790 if (KTRPOINT(td, KTR_GENIO))
791 ktruio = cloneuio(&auio);
793 len = auio.uio_resid;
794 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
796 if (auio.uio_resid != len && (error == ERESTART ||
797 error == EINTR || error == EWOULDBLOCK))
799 /* Generation of SIGPIPE can be controlled per socket */
800 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
801 !(flags & MSG_NOSIGNAL)) {
802 PROC_LOCK(td->td_proc);
803 tdsignal(td, SIGPIPE);
804 PROC_UNLOCK(td->td_proc);
808 td->td_retval[0] = len - auio.uio_resid;
810 if (ktruio != NULL) {
811 ktruio->uio_resid = td->td_retval[0];
812 ktrgenio(s, UIO_WRITE, ktruio, error);
821 sys_sendto(struct thread *td, struct sendto_args *uap)
826 msg.msg_name = __DECONST(void *, uap->to);
827 msg.msg_namelen = uap->tolen;
831 #ifdef COMPAT_OLDSOCK
834 aiov.iov_base = __DECONST(void *, uap->buf);
835 aiov.iov_len = uap->len;
836 return (sendit(td, uap->s, &msg, uap->flags));
839 #ifdef COMPAT_OLDSOCK
841 osend(struct thread *td, struct osend_args *uap)
850 aiov.iov_base = __DECONST(void *, uap->buf);
851 aiov.iov_len = uap->len;
854 return (sendit(td, uap->s, &msg, uap->flags));
858 osendmsg(struct thread *td, struct osendmsg_args *uap)
864 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
867 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
871 msg.msg_flags = MSG_COMPAT;
872 error = sendit(td, uap->s, &msg, uap->flags);
879 sys_sendmsg(struct thread *td, struct sendmsg_args *uap)
885 error = copyin(uap->msg, &msg, sizeof (msg));
888 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
892 #ifdef COMPAT_OLDSOCK
895 error = sendit(td, uap->s, &msg, uap->flags);
901 kern_recvit(struct thread *td, int s, struct msghdr *mp, enum uio_seg fromseg,
902 struct mbuf **controlp)
906 struct mbuf *control, *m;
910 struct sockaddr *fromsa = NULL;
912 struct uio *ktruio = NULL;
917 if (controlp != NULL)
921 error = getsock_cap(td, s, &cap_recv_rights,
928 error = mac_socket_check_receive(td->td_ucred, so);
935 auio.uio_iov = mp->msg_iov;
936 auio.uio_iovcnt = mp->msg_iovlen;
937 auio.uio_segflg = UIO_USERSPACE;
938 auio.uio_rw = UIO_READ;
940 auio.uio_offset = 0; /* XXX */
943 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
944 if ((auio.uio_resid += iov->iov_len) < 0) {
950 if (KTRPOINT(td, KTR_GENIO))
951 ktruio = cloneuio(&auio);
954 len = auio.uio_resid;
955 error = soreceive(so, &fromsa, &auio, NULL,
956 (mp->msg_control || controlp) ? &control : NULL,
959 if (auio.uio_resid != len && (error == ERESTART ||
960 error == EINTR || error == EWOULDBLOCK))
964 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
966 if (ktruio != NULL) {
967 ktruio->uio_resid = len - auio.uio_resid;
968 ktrgenio(s, UIO_READ, ktruio, error);
973 td->td_retval[0] = len - auio.uio_resid;
975 len = mp->msg_namelen;
976 if (len <= 0 || fromsa == NULL)
979 /* save sa_len before it is destroyed by MSG_COMPAT */
980 len = MIN(len, fromsa->sa_len);
981 #ifdef COMPAT_OLDSOCK
982 if (mp->msg_flags & MSG_COMPAT)
983 ((struct osockaddr *)fromsa)->sa_family =
986 if (fromseg == UIO_USERSPACE) {
987 error = copyout(fromsa, mp->msg_name,
992 bcopy(fromsa, mp->msg_name, len);
994 mp->msg_namelen = len;
996 if (mp->msg_control && controlp == NULL) {
997 #ifdef COMPAT_OLDSOCK
999 * We assume that old recvmsg calls won't receive access
1000 * rights and other control info, esp. as control info
1001 * is always optional and those options didn't exist in 4.3.
1002 * If we receive rights, trim the cmsghdr; anything else
1005 if (control && mp->msg_flags & MSG_COMPAT) {
1006 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1008 mtod(control, struct cmsghdr *)->cmsg_type !=
1010 mp->msg_controllen = 0;
1013 control->m_len -= sizeof (struct cmsghdr);
1014 control->m_data += sizeof (struct cmsghdr);
1017 ctlbuf = mp->msg_control;
1018 len = mp->msg_controllen;
1019 mp->msg_controllen = 0;
1020 for (m = control; m != NULL && len >= m->m_len; m = m->m_next) {
1021 if ((error = copyout(mtod(m, caddr_t), ctlbuf,
1027 mp->msg_controllen += m->m_len;
1030 mp->msg_flags |= MSG_CTRUNC;
1031 m_dispose_extcontrolm(m);
1037 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1038 ktrsockaddr(fromsa);
1040 free(fromsa, M_SONAME);
1042 if (error == 0 && controlp != NULL)
1043 *controlp = control;
1044 else if (control != NULL) {
1046 m_dispose_extcontrolm(control);
1054 recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp)
1058 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1061 if (namelenp != NULL) {
1062 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1063 #ifdef COMPAT_OLDSOCK
1064 if (mp->msg_flags & MSG_COMPAT)
1065 error = 0; /* old recvfrom didn't check */
1072 sys_recvfrom(struct thread *td, struct recvfrom_args *uap)
1078 if (uap->fromlenaddr) {
1079 error = copyin(uap->fromlenaddr,
1080 &msg.msg_namelen, sizeof (msg.msg_namelen));
1084 msg.msg_namelen = 0;
1086 msg.msg_name = uap->from;
1087 msg.msg_iov = &aiov;
1089 aiov.iov_base = uap->buf;
1090 aiov.iov_len = uap->len;
1091 msg.msg_control = 0;
1092 msg.msg_flags = uap->flags;
1093 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1098 #ifdef COMPAT_OLDSOCK
1100 orecvfrom(struct thread *td, struct recvfrom_args *uap)
1103 uap->flags |= MSG_COMPAT;
1104 return (sys_recvfrom(td, uap));
1108 #ifdef COMPAT_OLDSOCK
1110 orecv(struct thread *td, struct orecv_args *uap)
1116 msg.msg_namelen = 0;
1117 msg.msg_iov = &aiov;
1119 aiov.iov_base = uap->buf;
1120 aiov.iov_len = uap->len;
1121 msg.msg_control = 0;
1122 msg.msg_flags = uap->flags;
1123 return (recvit(td, uap->s, &msg, NULL));
1127 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1128 * overlays the new one, missing only the flags, and with the (old) access
1129 * rights where the control fields are now.
1132 orecvmsg(struct thread *td, struct orecvmsg_args *uap)
1138 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1141 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1144 msg.msg_flags = uap->flags | MSG_COMPAT;
1146 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1147 if (msg.msg_controllen && error == 0)
1148 error = copyout(&msg.msg_controllen,
1149 &uap->msg->msg_accrightslen, sizeof (int));
1156 sys_recvmsg(struct thread *td, struct recvmsg_args *uap)
1159 struct iovec *uiov, *iov;
1162 error = copyin(uap->msg, &msg, sizeof (msg));
1165 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1168 msg.msg_flags = uap->flags;
1169 #ifdef COMPAT_OLDSOCK
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_cap(td, s, &cap_shutdown_rights,
1202 error = soshutdown(so, how);
1204 * Previous versions did not return ENOTCONN, but 0 in
1205 * case the socket was not connected. Some important
1206 * programs like syslogd up to r279016, 2015-02-19,
1207 * still depend on this behavior.
1209 if (error == ENOTCONN &&
1210 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1218 sys_setsockopt(struct thread *td, struct setsockopt_args *uap)
1221 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1222 uap->val, UIO_USERSPACE, uap->valsize));
1226 kern_setsockopt(struct thread *td, int s, int level, int name, const void *val,
1227 enum uio_seg valseg, socklen_t valsize)
1231 struct sockopt sopt;
1234 if (val == NULL && valsize != 0)
1236 if ((int)valsize < 0)
1239 sopt.sopt_dir = SOPT_SET;
1240 sopt.sopt_level = level;
1241 sopt.sopt_name = name;
1242 sopt.sopt_val = __DECONST(void *, val);
1243 sopt.sopt_valsize = valsize;
1249 sopt.sopt_td = NULL;
1252 panic("kern_setsockopt called with bad valseg");
1256 error = getsock_cap(td, s, &cap_setsockopt_rights,
1260 error = sosetopt(so, &sopt);
1267 sys_getsockopt(struct thread *td, struct getsockopt_args *uap)
1273 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1278 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1279 uap->val, UIO_USERSPACE, &valsize);
1282 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1287 * Kernel version of getsockopt.
1288 * optval can be a userland or userspace. optlen is always a kernel pointer.
1291 kern_getsockopt(struct thread *td, int s, int level, int name, void *val,
1292 enum uio_seg valseg, socklen_t *valsize)
1296 struct sockopt sopt;
1301 if ((int)*valsize < 0)
1304 sopt.sopt_dir = SOPT_GET;
1305 sopt.sopt_level = level;
1306 sopt.sopt_name = name;
1307 sopt.sopt_val = val;
1308 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1314 sopt.sopt_td = NULL;
1317 panic("kern_getsockopt called with bad valseg");
1321 error = getsock_cap(td, s, &cap_getsockopt_rights,
1325 error = sogetopt(so, &sopt);
1326 *valsize = sopt.sopt_valsize;
1333 * getsockname1() - Get socket name.
1336 getsockname1(struct thread *td, struct getsockname_args *uap, int compat)
1338 struct sockaddr *sa;
1342 error = copyin(uap->alen, &len, sizeof(len));
1346 error = kern_getsockname(td, uap->fdes, &sa, &len);
1351 #ifdef COMPAT_OLDSOCK
1353 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1355 error = copyout(sa, uap->asa, (u_int)len);
1359 error = copyout(&len, uap->alen, sizeof(len));
1364 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1373 error = getsock_cap(td, fd, &cap_getsockname_rights,
1379 CURVNET_SET(so->so_vnet);
1380 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1387 len = MIN(*alen, (*sa)->sa_len);
1390 if (KTRPOINT(td, KTR_STRUCT))
1395 if (error != 0 && *sa != NULL) {
1396 free(*sa, M_SONAME);
1403 sys_getsockname(struct thread *td, struct getsockname_args *uap)
1406 return (getsockname1(td, uap, 0));
1409 #ifdef COMPAT_OLDSOCK
1411 ogetsockname(struct thread *td, struct getsockname_args *uap)
1414 return (getsockname1(td, uap, 1));
1416 #endif /* COMPAT_OLDSOCK */
1419 * getpeername1() - Get name of peer for connected socket.
1422 getpeername1(struct thread *td, struct getpeername_args *uap, int compat)
1424 struct sockaddr *sa;
1428 error = copyin(uap->alen, &len, sizeof (len));
1432 error = kern_getpeername(td, uap->fdes, &sa, &len);
1437 #ifdef COMPAT_OLDSOCK
1439 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1441 error = copyout(sa, uap->asa, (u_int)len);
1445 error = copyout(&len, uap->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)
1497 return (getpeername1(td, uap, 0));
1500 #ifdef COMPAT_OLDSOCK
1502 ogetpeername(struct thread *td, struct ogetpeername_args *uap)
1505 /* XXX uap should have type `getpeername_args *' to begin with. */
1506 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1508 #endif /* COMPAT_OLDSOCK */
1511 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1513 struct sockaddr *sa;
1517 if (buflen > MLEN) {
1518 #ifdef COMPAT_OLDSOCK
1519 if (type == MT_SONAME && buflen <= 112)
1520 buflen = MLEN; /* unix domain compat. hack */
1523 if (buflen > MCLBYTES)
1526 m = m_get2(buflen, M_WAITOK, type, 0);
1528 error = copyin(buf, mtod(m, void *), buflen);
1533 if (type == MT_SONAME) {
1534 sa = mtod(m, struct sockaddr *);
1536 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1537 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1538 sa->sa_family = sa->sa_len;
1540 sa->sa_len = buflen;
1547 getsockaddr(struct sockaddr **namp, const struct sockaddr *uaddr, size_t len)
1549 struct sockaddr *sa;
1552 if (len > SOCK_MAXADDRLEN)
1553 return (ENAMETOOLONG);
1554 if (len < offsetof(struct sockaddr, sa_data[0]))
1556 sa = malloc(len, M_SONAME, M_WAITOK);
1557 error = copyin(uaddr, sa, len);
1561 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1562 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
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);