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 getsockname1(struct thread *td, struct getsockname_args *uap,
83 static int getpeername1(struct thread *td, struct getpeername_args *uap,
85 static int sockargs(struct mbuf **, char *, socklen_t, int);
88 * Convert a user file descriptor to a kernel file entry and check if required
89 * capability rights are present.
90 * If required copy of current set of capability rights is returned.
91 * A reference on the file entry is held upon returning.
94 getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
95 struct file **fpp, u_int *fflagp, struct filecaps *havecapsp)
100 error = fget_cap(td, fd, rightsp, &fp, havecapsp);
103 if (fp->f_type != DTYPE_SOCKET) {
105 if (havecapsp != NULL)
106 filecaps_free(havecapsp);
110 *fflagp = fp->f_flag;
116 * System call interface to the socket abstraction.
118 #if defined(COMPAT_43)
119 #define COMPAT_OLDSOCK
123 sys_socket(struct thread *td, struct socket_args *uap)
126 return (kern_socket(td, uap->domain, uap->type, uap->protocol));
130 kern_socket(struct thread *td, int domain, int type, int protocol)
134 int fd, error, oflag, fflag;
136 AUDIT_ARG_SOCKET(domain, type, protocol);
140 if ((type & SOCK_CLOEXEC) != 0) {
141 type &= ~SOCK_CLOEXEC;
144 if ((type & SOCK_NONBLOCK) != 0) {
145 type &= ~SOCK_NONBLOCK;
150 error = mac_socket_check_create(td->td_ucred, domain, type, protocol);
154 error = falloc(td, &fp, &fd, oflag);
157 /* An extra reference on `fp' has been held for us by falloc(). */
158 error = socreate(domain, &so, type, protocol, td->td_ucred, td);
162 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
163 if ((fflag & FNONBLOCK) != 0)
164 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
165 td->td_retval[0] = fd;
172 sys_bind(struct thread *td, struct bind_args *uap)
177 error = getsockaddr(&sa, uap->name, uap->namelen);
179 error = kern_bindat(td, AT_FDCWD, uap->s, sa);
186 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
192 #ifdef CAPABILITY_MODE
193 if (IN_CAPABILITY_MODE(td) && (dirfd == AT_FDCWD))
198 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
199 error = getsock_cap(td, fd, &cap_bind_rights,
205 if (KTRPOINT(td, KTR_STRUCT))
209 error = mac_socket_check_bind(td->td_ucred, so, sa);
212 if (dirfd == AT_FDCWD)
213 error = sobind(so, sa, td);
215 error = sobindat(dirfd, so, sa, td);
224 sys_bindat(struct thread *td, struct bindat_args *uap)
229 error = getsockaddr(&sa, uap->name, uap->namelen);
231 error = kern_bindat(td, uap->fd, uap->s, sa);
238 sys_listen(struct thread *td, struct listen_args *uap)
241 return (kern_listen(td, uap->s, uap->backlog));
245 kern_listen(struct thread *td, int s, int backlog)
252 error = getsock_cap(td, s, &cap_listen_rights,
257 error = mac_socket_check_listen(td->td_ucred, so);
260 error = solisten(so, backlog, td);
270 accept1(td, s, uname, anamelen, flags)
273 struct sockaddr *uname;
277 struct sockaddr *name;
283 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
285 error = copyin(anamelen, &namelen, sizeof (namelen));
289 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
294 if (error == 0 && uname != NULL) {
295 #ifdef COMPAT_OLDSOCK
296 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) &&
297 (flags & ACCEPT4_COMPAT) != 0)
298 ((struct osockaddr *)name)->sa_family =
301 error = copyout(name, uname, namelen);
304 error = copyout(&namelen, anamelen,
307 fdclose(td, fp, td->td_retval[0]);
309 free(name, M_SONAME);
314 kern_accept(struct thread *td, int s, struct sockaddr **name,
315 socklen_t *namelen, struct file **fp)
317 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
321 kern_accept4(struct thread *td, int s, struct sockaddr **name,
322 socklen_t *namelen, int flags, struct file **fp)
324 struct file *headfp, *nfp = NULL;
325 struct sockaddr *sa = NULL;
326 struct socket *head, *so;
327 struct filecaps fcaps;
336 error = getsock_cap(td, s, &cap_accept_rights,
337 &headfp, &fflag, &fcaps);
340 head = headfp->f_data;
341 if ((head->so_options & SO_ACCEPTCONN) == 0) {
346 error = mac_socket_check_accept(td->td_ucred, head);
350 error = falloc_caps(td, &nfp, &fd,
351 (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0, &fcaps);
355 if (!SOLISTENING(head)) {
361 error = solisten_dequeue(head, &so, flags);
365 /* An extra reference on `nfp' has been held for us by falloc(). */
366 td->td_retval[0] = fd;
368 /* Connection has been removed from the listen queue. */
369 KNOTE_UNLOCKED(&head->so_rdsel.si_note, 0);
371 if (flags & ACCEPT4_INHERIT) {
372 pgid = fgetown(&head->so_sigio);
374 fsetown(pgid, &so->so_sigio);
376 fflag &= ~(FNONBLOCK | FASYNC);
377 if (flags & SOCK_NONBLOCK)
381 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
382 /* Sync socket nonblocking/async state with file flags */
383 tmp = fflag & FNONBLOCK;
384 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
385 tmp = fflag & FASYNC;
386 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
387 error = soaccept(so, &sa);
395 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
397 /* check sa_len before it is destroyed */
398 if (*namelen > sa->sa_len)
399 *namelen = sa->sa_len;
401 if (KTRPOINT(td, KTR_STRUCT))
411 * close the new descriptor, assuming someone hasn't ripped it
415 fdclose(td, nfp, fd);
418 * Release explicitly held references before returning. We return
419 * a reference on nfp to the caller on success if they request it.
423 filecaps_free(&fcaps);
440 struct accept_args *uap;
443 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
449 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
462 struct accept_args *uap;
465 return (accept1(td, uap->s, uap->name, uap->anamelen,
466 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
468 #endif /* COMPAT_OLDSOCK */
471 sys_connect(struct thread *td, struct connect_args *uap)
476 error = getsockaddr(&sa, uap->name, uap->namelen);
478 error = kern_connectat(td, AT_FDCWD, uap->s, sa);
485 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
489 int error, interrupted = 0;
491 #ifdef CAPABILITY_MODE
492 if (IN_CAPABILITY_MODE(td) && (dirfd == AT_FDCWD))
497 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
498 error = getsock_cap(td, fd, &cap_connect_rights,
503 if (so->so_state & SS_ISCONNECTING) {
508 if (KTRPOINT(td, KTR_STRUCT))
512 error = mac_socket_check_connect(td->td_ucred, so, sa);
516 if (dirfd == AT_FDCWD)
517 error = soconnect(so, sa, td);
519 error = soconnectat(dirfd, so, sa, td);
522 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
527 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
528 error = msleep(&so->so_timeo, &so->so_lock, PSOCK | PCATCH,
531 if (error == EINTR || error == ERESTART)
537 error = so->so_error;
543 so->so_state &= ~SS_ISCONNECTING;
544 if (error == ERESTART)
552 sys_connectat(struct thread *td, struct connectat_args *uap)
557 error = getsockaddr(&sa, uap->name, uap->namelen);
559 error = kern_connectat(td, uap->fd, uap->s, sa);
566 kern_socketpair(struct thread *td, int domain, int type, int protocol,
569 struct file *fp1, *fp2;
570 struct socket *so1, *so2;
571 int fd, error, oflag, fflag;
573 AUDIT_ARG_SOCKET(domain, type, protocol);
577 if ((type & SOCK_CLOEXEC) != 0) {
578 type &= ~SOCK_CLOEXEC;
581 if ((type & SOCK_NONBLOCK) != 0) {
582 type &= ~SOCK_NONBLOCK;
586 /* We might want to have a separate check for socket pairs. */
587 error = mac_socket_check_create(td->td_ucred, domain, type,
592 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
595 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
598 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
599 error = falloc(td, &fp1, &fd, oflag);
603 fp1->f_data = so1; /* so1 already has ref count */
604 error = falloc(td, &fp2, &fd, oflag);
607 fp2->f_data = so2; /* so2 already has ref count */
609 error = soconnect2(so1, so2);
612 if (type == SOCK_DGRAM) {
614 * Datagram socket connection is asymmetric.
616 error = soconnect2(so2, so1);
619 } else if (so1->so_proto->pr_flags & PR_CONNREQUIRED) {
620 struct unpcb *unp, *unp2;
621 unp = sotounpcb(so1);
622 unp2 = sotounpcb(so2);
624 * No need to lock the unps, because the sockets are brand-new.
625 * No other threads can be using them yet
627 unp_copy_peercred(td, unp, unp2, unp);
629 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
631 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
633 if ((fflag & FNONBLOCK) != 0) {
634 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
635 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
641 fdclose(td, fp2, rsv[1]);
644 fdclose(td, fp1, rsv[0]);
656 sys_socketpair(struct thread *td, struct socketpair_args *uap)
660 error = kern_socketpair(td, uap->domain, uap->type,
664 error = copyout(sv, uap->rsv, 2 * sizeof(int));
666 (void)kern_close(td, sv[0]);
667 (void)kern_close(td, sv[1]);
673 sendit(struct thread *td, int s, struct msghdr *mp, int flags)
675 struct mbuf *control;
679 #ifdef CAPABILITY_MODE
680 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
684 if (mp->msg_name != NULL) {
685 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
695 if (mp->msg_control) {
696 if (mp->msg_controllen < sizeof(struct cmsghdr)
697 #ifdef COMPAT_OLDSOCK
698 && (mp->msg_flags != MSG_COMPAT ||
699 !SV_PROC_FLAG(td->td_proc, SV_AOUT))
705 error = sockargs(&control, mp->msg_control,
706 mp->msg_controllen, MT_CONTROL);
709 #ifdef COMPAT_OLDSOCK
710 if (mp->msg_flags == MSG_COMPAT &&
711 SV_PROC_FLAG(td->td_proc, SV_AOUT)) {
714 M_PREPEND(control, sizeof(*cm), M_WAITOK);
715 cm = mtod(control, struct cmsghdr *);
716 cm->cmsg_len = control->m_len;
717 cm->cmsg_level = SOL_SOCKET;
718 cm->cmsg_type = SCM_RIGHTS;
725 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
733 kern_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
734 struct mbuf *control, enum uio_seg segflg)
740 cap_rights_t *rights;
742 struct uio *ktruio = NULL;
748 rights = &cap_send_rights;
749 if (mp->msg_name != NULL) {
750 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
751 rights = &cap_send_connect_rights;
753 error = getsock_cap(td, s, rights, &fp, NULL, NULL);
758 so = (struct socket *)fp->f_data;
761 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
762 ktrsockaddr(mp->msg_name);
765 if (mp->msg_name != NULL) {
766 error = mac_socket_check_connect(td->td_ucred, so,
773 error = mac_socket_check_send(td->td_ucred, so);
780 auio.uio_iov = mp->msg_iov;
781 auio.uio_iovcnt = mp->msg_iovlen;
782 auio.uio_segflg = segflg;
783 auio.uio_rw = UIO_WRITE;
785 auio.uio_offset = 0; /* XXX */
788 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
789 if ((auio.uio_resid += iov->iov_len) < 0) {
796 if (KTRPOINT(td, KTR_GENIO))
797 ktruio = cloneuio(&auio);
799 len = auio.uio_resid;
800 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
802 if (auio.uio_resid != len && (error == ERESTART ||
803 error == EINTR || error == EWOULDBLOCK))
805 /* Generation of SIGPIPE can be controlled per socket */
806 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
807 !(flags & MSG_NOSIGNAL)) {
808 PROC_LOCK(td->td_proc);
809 tdsignal(td, SIGPIPE);
810 PROC_UNLOCK(td->td_proc);
814 td->td_retval[0] = len - auio.uio_resid;
816 if (ktruio != NULL) {
817 ktruio->uio_resid = td->td_retval[0];
818 ktrgenio(s, UIO_WRITE, ktruio, error);
827 sys_sendto(struct thread *td, struct sendto_args *uap)
832 msg.msg_name = uap->to;
833 msg.msg_namelen = uap->tolen;
837 #ifdef COMPAT_OLDSOCK
838 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
841 aiov.iov_base = uap->buf;
842 aiov.iov_len = uap->len;
843 return (sendit(td, uap->s, &msg, uap->flags));
846 #ifdef COMPAT_OLDSOCK
848 osend(struct thread *td, struct osend_args *uap)
857 aiov.iov_base = uap->buf;
858 aiov.iov_len = uap->len;
861 return (sendit(td, uap->s, &msg, uap->flags));
865 osendmsg(struct thread *td, struct osendmsg_args *uap)
871 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
874 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
878 msg.msg_flags = MSG_COMPAT;
879 error = sendit(td, uap->s, &msg, uap->flags);
886 sys_sendmsg(struct thread *td, struct sendmsg_args *uap)
892 error = copyin(uap->msg, &msg, sizeof (msg));
895 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
899 #ifdef COMPAT_OLDSOCK
900 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
903 error = sendit(td, uap->s, &msg, uap->flags);
909 kern_recvit(struct thread *td, int s, struct msghdr *mp, enum uio_seg fromseg,
910 struct mbuf **controlp)
914 struct mbuf *control, *m;
918 struct sockaddr *fromsa = NULL;
920 struct uio *ktruio = NULL;
925 if (controlp != NULL)
929 error = getsock_cap(td, s, &cap_recv_rights,
936 error = mac_socket_check_receive(td->td_ucred, so);
943 auio.uio_iov = mp->msg_iov;
944 auio.uio_iovcnt = mp->msg_iovlen;
945 auio.uio_segflg = UIO_USERSPACE;
946 auio.uio_rw = UIO_READ;
948 auio.uio_offset = 0; /* XXX */
951 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
952 if ((auio.uio_resid += iov->iov_len) < 0) {
958 if (KTRPOINT(td, KTR_GENIO))
959 ktruio = cloneuio(&auio);
962 len = auio.uio_resid;
963 error = soreceive(so, &fromsa, &auio, NULL,
964 (mp->msg_control || controlp) ? &control : NULL,
967 if (auio.uio_resid != len && (error == ERESTART ||
968 error == EINTR || error == EWOULDBLOCK))
972 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
974 if (ktruio != NULL) {
975 ktruio->uio_resid = len - auio.uio_resid;
976 ktrgenio(s, UIO_READ, ktruio, error);
981 td->td_retval[0] = len - auio.uio_resid;
983 len = mp->msg_namelen;
984 if (len <= 0 || fromsa == NULL)
987 /* save sa_len before it is destroyed by MSG_COMPAT */
988 len = MIN(len, fromsa->sa_len);
989 #ifdef COMPAT_OLDSOCK
990 if ((mp->msg_flags & MSG_COMPAT) != 0 &&
991 SV_PROC_FLAG(td->td_proc, SV_AOUT))
992 ((struct osockaddr *)fromsa)->sa_family =
995 if (fromseg == UIO_USERSPACE) {
996 error = copyout(fromsa, mp->msg_name,
1001 bcopy(fromsa, mp->msg_name, len);
1003 mp->msg_namelen = len;
1005 if (mp->msg_control && controlp == NULL) {
1006 #ifdef COMPAT_OLDSOCK
1008 * We assume that old recvmsg calls won't receive access
1009 * rights and other control info, esp. as control info
1010 * is always optional and those options didn't exist in 4.3.
1011 * If we receive rights, trim the cmsghdr; anything else
1014 if (control && (mp->msg_flags & MSG_COMPAT) != 0 &&
1015 SV_PROC_FLAG(td->td_proc, SV_AOUT)) {
1016 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1018 mtod(control, struct cmsghdr *)->cmsg_type !=
1020 mp->msg_controllen = 0;
1023 control->m_len -= sizeof (struct cmsghdr);
1024 control->m_data += sizeof (struct cmsghdr);
1027 ctlbuf = mp->msg_control;
1028 len = mp->msg_controllen;
1029 mp->msg_controllen = 0;
1030 for (m = control; m != NULL && len >= m->m_len; m = m->m_next) {
1031 if ((error = copyout(mtod(m, caddr_t), ctlbuf,
1037 mp->msg_controllen += m->m_len;
1040 mp->msg_flags |= MSG_CTRUNC;
1041 m_dispose_extcontrolm(m);
1047 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1048 ktrsockaddr(fromsa);
1050 free(fromsa, M_SONAME);
1052 if (error == 0 && controlp != NULL)
1053 *controlp = control;
1054 else if (control != NULL) {
1056 m_dispose_extcontrolm(control);
1064 recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp)
1068 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1071 if (namelenp != NULL) {
1072 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1073 #ifdef COMPAT_OLDSOCK
1074 if ((mp->msg_flags & MSG_COMPAT) != 0 &&
1075 SV_PROC_FLAG(td->td_proc, SV_AOUT))
1076 error = 0; /* old recvfrom didn't check */
1083 sys_recvfrom(struct thread *td, struct recvfrom_args *uap)
1089 if (uap->fromlenaddr) {
1090 error = copyin(uap->fromlenaddr,
1091 &msg.msg_namelen, sizeof (msg.msg_namelen));
1095 msg.msg_namelen = 0;
1097 msg.msg_name = uap->from;
1098 msg.msg_iov = &aiov;
1100 aiov.iov_base = uap->buf;
1101 aiov.iov_len = uap->len;
1102 msg.msg_control = 0;
1103 msg.msg_flags = uap->flags;
1104 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1109 #ifdef COMPAT_OLDSOCK
1111 orecvfrom(struct thread *td, struct recvfrom_args *uap)
1114 uap->flags |= MSG_COMPAT;
1115 return (sys_recvfrom(td, uap));
1119 #ifdef COMPAT_OLDSOCK
1121 orecv(struct thread *td, struct orecv_args *uap)
1127 msg.msg_namelen = 0;
1128 msg.msg_iov = &aiov;
1130 aiov.iov_base = uap->buf;
1131 aiov.iov_len = uap->len;
1132 msg.msg_control = 0;
1133 msg.msg_flags = uap->flags;
1134 return (recvit(td, uap->s, &msg, NULL));
1138 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1139 * overlays the new one, missing only the flags, and with the (old) access
1140 * rights where the control fields are now.
1143 orecvmsg(struct thread *td, struct orecvmsg_args *uap)
1149 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1152 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1155 msg.msg_flags = uap->flags | MSG_COMPAT;
1157 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1158 if (msg.msg_controllen && error == 0)
1159 error = copyout(&msg.msg_controllen,
1160 &uap->msg->msg_accrightslen, sizeof (int));
1167 sys_recvmsg(struct thread *td, struct recvmsg_args *uap)
1170 struct iovec *uiov, *iov;
1173 error = copyin(uap->msg, &msg, sizeof (msg));
1176 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1179 msg.msg_flags = uap->flags;
1180 #ifdef COMPAT_OLDSOCK
1181 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
1182 msg.msg_flags &= ~MSG_COMPAT;
1186 error = recvit(td, uap->s, &msg, NULL);
1189 error = copyout(&msg, uap->msg, sizeof(msg));
1196 sys_shutdown(struct thread *td, struct shutdown_args *uap)
1199 return (kern_shutdown(td, uap->s, uap->how));
1203 kern_shutdown(struct thread *td, int s, int how)
1210 error = getsock_cap(td, s, &cap_shutdown_rights,
1214 error = soshutdown(so, how);
1216 * Previous versions did not return ENOTCONN, but 0 in
1217 * case the socket was not connected. Some important
1218 * programs like syslogd up to r279016, 2015-02-19,
1219 * still depend on this behavior.
1221 if (error == ENOTCONN &&
1222 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1230 sys_setsockopt(struct thread *td, struct setsockopt_args *uap)
1233 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1234 uap->val, UIO_USERSPACE, uap->valsize));
1238 kern_setsockopt(struct thread *td, int s, int level, int name, void *val,
1239 enum uio_seg valseg, socklen_t valsize)
1243 struct sockopt sopt;
1246 if (val == NULL && valsize != 0)
1248 if ((int)valsize < 0)
1251 sopt.sopt_dir = SOPT_SET;
1252 sopt.sopt_level = level;
1253 sopt.sopt_name = name;
1254 sopt.sopt_val = val;
1255 sopt.sopt_valsize = valsize;
1261 sopt.sopt_td = NULL;
1264 panic("kern_setsockopt called with bad valseg");
1268 error = getsock_cap(td, s, &cap_setsockopt_rights,
1272 error = sosetopt(so, &sopt);
1279 sys_getsockopt(struct thread *td, struct getsockopt_args *uap)
1285 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1290 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1291 uap->val, UIO_USERSPACE, &valsize);
1294 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1299 * Kernel version of getsockopt.
1300 * optval can be a userland or userspace. optlen is always a kernel pointer.
1303 kern_getsockopt(struct thread *td, int s, int level, int name, void *val,
1304 enum uio_seg valseg, socklen_t *valsize)
1308 struct sockopt sopt;
1313 if ((int)*valsize < 0)
1316 sopt.sopt_dir = SOPT_GET;
1317 sopt.sopt_level = level;
1318 sopt.sopt_name = name;
1319 sopt.sopt_val = val;
1320 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1326 sopt.sopt_td = NULL;
1329 panic("kern_getsockopt called with bad valseg");
1333 error = getsock_cap(td, s, &cap_getsockopt_rights,
1337 error = sogetopt(so, &sopt);
1338 *valsize = sopt.sopt_valsize;
1345 * getsockname1() - Get socket name.
1348 getsockname1(struct thread *td, struct getsockname_args *uap, int compat)
1350 struct sockaddr *sa;
1354 error = copyin(uap->alen, &len, sizeof(len));
1358 error = kern_getsockname(td, uap->fdes, &sa, &len);
1363 #ifdef COMPAT_OLDSOCK
1364 if (compat && SV_PROC_FLAG(td->td_proc, SV_AOUT))
1365 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1367 error = copyout(sa, uap->asa, (u_int)len);
1371 error = copyout(&len, uap->alen, sizeof(len));
1376 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1385 error = getsock_cap(td, fd, &cap_getsockname_rights,
1391 CURVNET_SET(so->so_vnet);
1392 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1399 len = MIN(*alen, (*sa)->sa_len);
1402 if (KTRPOINT(td, KTR_STRUCT))
1407 if (error != 0 && *sa != NULL) {
1408 free(*sa, M_SONAME);
1415 sys_getsockname(struct thread *td, struct getsockname_args *uap)
1418 return (getsockname1(td, uap, 0));
1421 #ifdef COMPAT_OLDSOCK
1423 ogetsockname(struct thread *td, struct getsockname_args *uap)
1426 return (getsockname1(td, uap, 1));
1428 #endif /* COMPAT_OLDSOCK */
1431 * getpeername1() - Get name of peer for connected socket.
1434 getpeername1(struct thread *td, struct getpeername_args *uap, int compat)
1436 struct sockaddr *sa;
1440 error = copyin(uap->alen, &len, sizeof (len));
1444 error = kern_getpeername(td, uap->fdes, &sa, &len);
1449 #ifdef COMPAT_OLDSOCK
1450 if (compat && SV_PROC_FLAG(td->td_proc, SV_AOUT))
1451 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1453 error = copyout(sa, uap->asa, (u_int)len);
1457 error = copyout(&len, uap->alen, sizeof(len));
1462 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1471 error = getsock_cap(td, fd, &cap_getpeername_rights,
1476 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1481 CURVNET_SET(so->so_vnet);
1482 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1489 len = MIN(*alen, (*sa)->sa_len);
1492 if (KTRPOINT(td, KTR_STRUCT))
1496 if (error != 0 && *sa != NULL) {
1497 free(*sa, M_SONAME);
1506 sys_getpeername(struct thread *td, struct getpeername_args *uap)
1509 return (getpeername1(td, uap, 0));
1512 #ifdef COMPAT_OLDSOCK
1514 ogetpeername(struct thread *td, struct ogetpeername_args *uap)
1517 /* XXX uap should have type `getpeername_args *' to begin with. */
1518 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1520 #endif /* COMPAT_OLDSOCK */
1523 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1525 struct sockaddr *sa;
1529 if (buflen > MLEN) {
1530 #ifdef COMPAT_OLDSOCK
1531 if (type == MT_SONAME && buflen <= 112 &&
1532 SV_CURPROC_FLAG(SV_AOUT))
1533 buflen = MLEN; /* unix domain compat. hack */
1536 if (buflen > MCLBYTES)
1539 m = m_get2(buflen, M_WAITOK, type, 0);
1541 error = copyin(buf, mtod(m, void *), buflen);
1546 if (type == MT_SONAME) {
1547 sa = mtod(m, struct sockaddr *);
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;
1554 sa->sa_len = buflen;
1561 getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
1563 struct sockaddr *sa;
1566 if (len > SOCK_MAXADDRLEN)
1567 return (ENAMETOOLONG);
1568 if (len < offsetof(struct sockaddr, sa_data[0]))
1570 sa = malloc(len, M_SONAME, M_WAITOK);
1571 error = copyin(uaddr, sa, len);
1575 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1576 if (sa->sa_family == 0 && sa->sa_len < AF_MAX &&
1577 SV_CURPROC_FLAG(SV_AOUT))
1578 sa->sa_family = sa->sa_len;
1587 * Dispose of externalized rights from an SCM_RIGHTS message. This function
1588 * should be used in error or truncation cases to avoid leaking file descriptors
1589 * into the recipient's (the current thread's) table.
1592 m_dispose_extcontrolm(struct mbuf *m)
1597 socklen_t clen, datalen;
1598 int error, fd, *fds, nfd;
1601 for (; m != NULL; m = m->m_next) {
1602 if (m->m_type != MT_EXTCONTROL)
1604 cm = mtod(m, struct cmsghdr *);
1607 if (clen < sizeof(*cm))
1608 panic("%s: truncated mbuf %p", __func__, m);
1609 datalen = CMSG_SPACE(cm->cmsg_len - CMSG_SPACE(0));
1611 panic("%s: truncated mbuf %p", __func__, m);
1613 if (cm->cmsg_level == SOL_SOCKET &&
1614 cm->cmsg_type == SCM_RIGHTS) {
1615 fds = (int *)CMSG_DATA(cm);
1616 nfd = (cm->cmsg_len - CMSG_SPACE(0)) /
1621 error = fget(td, fd, &cap_no_rights,
1624 fdclose(td, fp, fd);
1630 cm = (struct cmsghdr *)((uint8_t *)cm + datalen);
1632 m_chtype(m, MT_CONTROL);