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_compat.h"
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/capsicum.h>
46 #include <sys/kernel.h>
48 #include <sys/mutex.h>
49 #include <sys/sysproto.h>
50 #include <sys/malloc.h>
51 #include <sys/filedesc.h>
53 #include <sys/filio.h>
56 #include <sys/protosw.h>
57 #include <sys/rwlock.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
60 #include <sys/syscallsubr.h>
63 #include <sys/ktrace.h>
65 #ifdef COMPAT_FREEBSD32
66 #include <compat/freebsd32/freebsd32_util.h>
71 #include <security/audit/audit.h>
72 #include <security/mac/mac_framework.h>
74 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
75 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
77 static int accept1(struct thread *td, int s, struct sockaddr *uname,
78 socklen_t *anamelen, int flags);
79 static int getsockname1(struct thread *td, struct getsockname_args *uap,
81 static int getpeername1(struct thread *td, struct getpeername_args *uap,
83 static int sockargs(struct mbuf **, char *, socklen_t, int);
86 * Convert a user file descriptor to a kernel file entry and check if required
87 * capability rights are present.
88 * If required copy of current set of capability rights is returned.
89 * A reference on the file entry is held upon returning.
92 getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
93 struct file **fpp, u_int *fflagp, struct filecaps *havecapsp)
98 error = fget_cap(td, fd, rightsp, &fp, havecapsp);
101 if (fp->f_type != DTYPE_SOCKET) {
103 if (havecapsp != NULL)
104 filecaps_free(havecapsp);
108 *fflagp = fp->f_flag;
114 * System call interface to the socket abstraction.
116 #if defined(COMPAT_43)
117 #define COMPAT_OLDSOCK
121 sys_socket(struct thread *td, struct socket_args *uap)
124 return (kern_socket(td, uap->domain, uap->type, uap->protocol));
128 kern_socket(struct thread *td, int domain, int type, int protocol)
132 int fd, error, oflag, fflag;
134 AUDIT_ARG_SOCKET(domain, type, protocol);
138 if ((type & SOCK_CLOEXEC) != 0) {
139 type &= ~SOCK_CLOEXEC;
142 if ((type & SOCK_NONBLOCK) != 0) {
143 type &= ~SOCK_NONBLOCK;
148 error = mac_socket_check_create(td->td_ucred, domain, type, protocol);
152 error = falloc(td, &fp, &fd, oflag);
155 /* An extra reference on `fp' has been held for us by falloc(). */
156 error = socreate(domain, &so, type, protocol, td->td_ucred, td);
160 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
161 if ((fflag & FNONBLOCK) != 0)
162 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
163 td->td_retval[0] = fd;
170 sys_bind(struct thread *td, struct bind_args *uap)
175 error = getsockaddr(&sa, uap->name, uap->namelen);
177 error = kern_bindat(td, AT_FDCWD, uap->s, sa);
184 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
192 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
193 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_BIND),
199 if (KTRPOINT(td, KTR_STRUCT))
203 error = mac_socket_check_bind(td->td_ucred, so, sa);
206 if (dirfd == AT_FDCWD)
207 error = sobind(so, sa, td);
209 error = sobindat(dirfd, so, sa, td);
218 sys_bindat(struct thread *td, struct bindat_args *uap)
223 error = getsockaddr(&sa, uap->name, uap->namelen);
225 error = kern_bindat(td, uap->fd, uap->s, sa);
232 sys_listen(struct thread *td, struct listen_args *uap)
235 return (kern_listen(td, uap->s, uap->backlog));
239 kern_listen(struct thread *td, int s, int backlog)
247 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_LISTEN),
252 error = mac_socket_check_listen(td->td_ucred, so);
255 error = solisten(so, backlog, td);
265 accept1(td, s, uname, anamelen, flags)
268 struct sockaddr *uname;
272 struct sockaddr *name;
278 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
280 error = copyin(anamelen, &namelen, sizeof (namelen));
284 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
289 if (error == 0 && uname != NULL) {
290 #ifdef COMPAT_OLDSOCK
291 if (flags & ACCEPT4_COMPAT)
292 ((struct osockaddr *)name)->sa_family =
295 error = copyout(name, uname, namelen);
298 error = copyout(&namelen, anamelen,
301 fdclose(td, fp, td->td_retval[0]);
303 free(name, M_SONAME);
308 kern_accept(struct thread *td, int s, struct sockaddr **name,
309 socklen_t *namelen, struct file **fp)
311 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
315 kern_accept4(struct thread *td, int s, struct sockaddr **name,
316 socklen_t *namelen, int flags, struct file **fp)
318 struct file *headfp, *nfp = NULL;
319 struct sockaddr *sa = NULL;
320 struct socket *head, *so;
321 struct filecaps fcaps;
331 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_ACCEPT),
332 &headfp, &fflag, &fcaps);
335 head = headfp->f_data;
336 if ((head->so_options & SO_ACCEPTCONN) == 0) {
341 error = mac_socket_check_accept(td->td_ucred, head);
345 error = falloc_caps(td, &nfp, &fd,
346 (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0, &fcaps);
350 if (!SOLISTENING(head)) {
356 error = solisten_dequeue(head, &so, flags);
360 /* An extra reference on `nfp' has been held for us by falloc(). */
361 td->td_retval[0] = fd;
363 /* Connection has been removed from the listen queue. */
364 KNOTE_UNLOCKED(&head->so_rdsel.si_note, 0);
366 if (flags & ACCEPT4_INHERIT) {
367 pgid = fgetown(&head->so_sigio);
369 fsetown(pgid, &so->so_sigio);
371 fflag &= ~(FNONBLOCK | FASYNC);
372 if (flags & SOCK_NONBLOCK)
376 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
377 /* Sync socket nonblocking/async state with file flags */
378 tmp = fflag & FNONBLOCK;
379 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
380 tmp = fflag & FASYNC;
381 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
382 error = soaccept(so, &sa);
390 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
392 /* check sa_len before it is destroyed */
393 if (*namelen > sa->sa_len)
394 *namelen = sa->sa_len;
396 if (KTRPOINT(td, KTR_STRUCT))
406 * close the new descriptor, assuming someone hasn't ripped it
410 fdclose(td, nfp, fd);
413 * Release explicitly held references before returning. We return
414 * a reference on nfp to the caller on success if they request it.
418 filecaps_free(&fcaps);
435 struct accept_args *uap;
438 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
444 struct accept4_args *uap;
447 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
450 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
453 #ifdef COMPAT_OLDSOCK
457 struct accept_args *uap;
460 return (accept1(td, uap->s, uap->name, uap->anamelen,
461 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
463 #endif /* COMPAT_OLDSOCK */
466 sys_connect(struct thread *td, struct connect_args *uap)
471 error = getsockaddr(&sa, uap->name, uap->namelen);
473 error = kern_connectat(td, AT_FDCWD, uap->s, sa);
480 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
485 int error, interrupted = 0;
488 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
489 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_CONNECT),
494 if (so->so_state & SS_ISCONNECTING) {
499 if (KTRPOINT(td, KTR_STRUCT))
503 error = mac_socket_check_connect(td->td_ucred, so, sa);
507 if (dirfd == AT_FDCWD)
508 error = soconnect(so, sa, td);
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,
522 if (error == EINTR || error == ERESTART)
528 error = so->so_error;
534 so->so_state &= ~SS_ISCONNECTING;
535 if (error == ERESTART)
543 sys_connectat(struct thread *td, struct connectat_args *uap)
548 error = getsockaddr(&sa, uap->name, uap->namelen);
550 error = kern_connectat(td, uap->fd, uap->s, sa);
557 kern_socketpair(struct thread *td, int domain, int type, int protocol,
560 struct file *fp1, *fp2;
561 struct socket *so1, *so2;
562 int fd, error, oflag, fflag;
564 AUDIT_ARG_SOCKET(domain, type, protocol);
568 if ((type & SOCK_CLOEXEC) != 0) {
569 type &= ~SOCK_CLOEXEC;
572 if ((type & SOCK_NONBLOCK) != 0) {
573 type &= ~SOCK_NONBLOCK;
577 /* We might want to have a separate check for socket pairs. */
578 error = mac_socket_check_create(td->td_ucred, domain, type,
583 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
586 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
589 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
590 error = falloc(td, &fp1, &fd, oflag);
594 fp1->f_data = so1; /* so1 already has ref count */
595 error = falloc(td, &fp2, &fd, oflag);
598 fp2->f_data = so2; /* so2 already has ref count */
600 error = soconnect2(so1, so2);
603 if (type == SOCK_DGRAM) {
605 * Datagram socket connection is asymmetric.
607 error = soconnect2(so2, so1);
611 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
613 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
615 if ((fflag & FNONBLOCK) != 0) {
616 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
617 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
623 fdclose(td, fp2, rsv[1]);
626 fdclose(td, fp1, rsv[0]);
638 sys_socketpair(struct thread *td, struct socketpair_args *uap)
642 error = kern_socketpair(td, uap->domain, uap->type,
646 error = copyout(sv, uap->rsv, 2 * sizeof(int));
648 (void)kern_close(td, sv[0]);
649 (void)kern_close(td, sv[1]);
655 sendit(struct thread *td, int s, struct msghdr *mp, int flags)
657 struct mbuf *control;
661 #ifdef CAPABILITY_MODE
662 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
666 if (mp->msg_name != NULL) {
667 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
677 if (mp->msg_control) {
678 if (mp->msg_controllen < sizeof(struct cmsghdr)
679 #ifdef COMPAT_OLDSOCK
680 && mp->msg_flags != MSG_COMPAT
686 error = sockargs(&control, mp->msg_control,
687 mp->msg_controllen, MT_CONTROL);
690 #ifdef COMPAT_OLDSOCK
691 if (mp->msg_flags == MSG_COMPAT) {
694 M_PREPEND(control, sizeof(*cm), M_WAITOK);
695 cm = mtod(control, struct cmsghdr *);
696 cm->cmsg_len = control->m_len;
697 cm->cmsg_level = SOL_SOCKET;
698 cm->cmsg_type = SCM_RIGHTS;
705 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
713 kern_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
714 struct mbuf *control, enum uio_seg segflg)
722 struct uio *ktruio = NULL;
728 cap_rights_init(&rights, CAP_SEND);
729 if (mp->msg_name != NULL) {
730 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
731 cap_rights_set(&rights, CAP_CONNECT);
733 error = getsock_cap(td, s, &rights, &fp, NULL, NULL);
738 so = (struct socket *)fp->f_data;
741 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
742 ktrsockaddr(mp->msg_name);
745 if (mp->msg_name != NULL) {
746 error = mac_socket_check_connect(td->td_ucred, so,
753 error = mac_socket_check_send(td->td_ucred, so);
760 auio.uio_iov = mp->msg_iov;
761 auio.uio_iovcnt = mp->msg_iovlen;
762 auio.uio_segflg = segflg;
763 auio.uio_rw = UIO_WRITE;
765 auio.uio_offset = 0; /* XXX */
768 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
769 if ((auio.uio_resid += iov->iov_len) < 0) {
776 if (KTRPOINT(td, KTR_GENIO))
777 ktruio = cloneuio(&auio);
779 len = auio.uio_resid;
780 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
782 if (auio.uio_resid != len && (error == ERESTART ||
783 error == EINTR || error == EWOULDBLOCK))
785 /* Generation of SIGPIPE can be controlled per socket */
786 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
787 !(flags & MSG_NOSIGNAL)) {
788 PROC_LOCK(td->td_proc);
789 tdsignal(td, SIGPIPE);
790 PROC_UNLOCK(td->td_proc);
794 td->td_retval[0] = len - auio.uio_resid;
796 if (ktruio != NULL) {
797 ktruio->uio_resid = td->td_retval[0];
798 ktrgenio(s, UIO_WRITE, ktruio, error);
807 sys_sendto(struct thread *td, struct sendto_args *uap)
812 msg.msg_name = uap->to;
813 msg.msg_namelen = uap->tolen;
817 #ifdef COMPAT_OLDSOCK
820 aiov.iov_base = uap->buf;
821 aiov.iov_len = uap->len;
822 return (sendit(td, uap->s, &msg, uap->flags));
825 #ifdef COMPAT_OLDSOCK
827 osend(struct thread *td, struct osend_args *uap)
836 aiov.iov_base = uap->buf;
837 aiov.iov_len = uap->len;
840 return (sendit(td, uap->s, &msg, uap->flags));
844 osendmsg(struct thread *td, struct osendmsg_args *uap)
850 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
853 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
857 msg.msg_flags = MSG_COMPAT;
858 error = sendit(td, uap->s, &msg, uap->flags);
865 sys_sendmsg(struct thread *td, struct sendmsg_args *uap)
871 error = copyin(uap->msg, &msg, sizeof (msg));
874 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
878 #ifdef COMPAT_OLDSOCK
881 error = sendit(td, uap->s, &msg, uap->flags);
887 kern_recvit(struct thread *td, int s, struct msghdr *mp, enum uio_seg fromseg,
888 struct mbuf **controlp)
892 struct mbuf *m, *control = NULL;
896 struct sockaddr *fromsa = NULL;
899 struct uio *ktruio = NULL;
904 if (controlp != NULL)
908 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_RECV),
915 error = mac_socket_check_receive(td->td_ucred, so);
922 auio.uio_iov = mp->msg_iov;
923 auio.uio_iovcnt = mp->msg_iovlen;
924 auio.uio_segflg = UIO_USERSPACE;
925 auio.uio_rw = UIO_READ;
927 auio.uio_offset = 0; /* XXX */
930 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
931 if ((auio.uio_resid += iov->iov_len) < 0) {
937 if (KTRPOINT(td, KTR_GENIO))
938 ktruio = cloneuio(&auio);
940 len = auio.uio_resid;
941 error = soreceive(so, &fromsa, &auio, NULL,
942 (mp->msg_control || controlp) ? &control : NULL,
945 if (auio.uio_resid != len && (error == ERESTART ||
946 error == EINTR || error == EWOULDBLOCK))
950 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
952 if (ktruio != NULL) {
953 ktruio->uio_resid = len - auio.uio_resid;
954 ktrgenio(s, UIO_READ, ktruio, error);
959 td->td_retval[0] = len - auio.uio_resid;
961 len = mp->msg_namelen;
962 if (len <= 0 || fromsa == NULL)
965 /* save sa_len before it is destroyed by MSG_COMPAT */
966 len = MIN(len, fromsa->sa_len);
967 #ifdef COMPAT_OLDSOCK
968 if (mp->msg_flags & MSG_COMPAT)
969 ((struct osockaddr *)fromsa)->sa_family =
972 if (fromseg == UIO_USERSPACE) {
973 error = copyout(fromsa, mp->msg_name,
978 bcopy(fromsa, mp->msg_name, len);
980 mp->msg_namelen = len;
982 if (mp->msg_control && controlp == NULL) {
983 #ifdef COMPAT_OLDSOCK
985 * We assume that old recvmsg calls won't receive access
986 * rights and other control info, esp. as control info
987 * is always optional and those options didn't exist in 4.3.
988 * If we receive rights, trim the cmsghdr; anything else
991 if (control && mp->msg_flags & MSG_COMPAT) {
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 len = mp->msg_controllen;
1005 mp->msg_controllen = 0;
1006 ctlbuf = mp->msg_control;
1008 while (m && len > 0) {
1009 unsigned int tocopy;
1011 if (len >= m->m_len)
1014 mp->msg_flags |= MSG_CTRUNC;
1018 if ((error = copyout(mtod(m, caddr_t),
1019 ctlbuf, tocopy)) != 0)
1026 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1031 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1032 ktrsockaddr(fromsa);
1034 free(fromsa, M_SONAME);
1036 if (error == 0 && controlp != NULL)
1037 *controlp = control;
1045 recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp)
1049 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1052 if (namelenp != NULL) {
1053 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1054 #ifdef COMPAT_OLDSOCK
1055 if (mp->msg_flags & MSG_COMPAT)
1056 error = 0; /* old recvfrom didn't check */
1063 sys_recvfrom(struct thread *td, struct recvfrom_args *uap)
1069 if (uap->fromlenaddr) {
1070 error = copyin(uap->fromlenaddr,
1071 &msg.msg_namelen, sizeof (msg.msg_namelen));
1075 msg.msg_namelen = 0;
1077 msg.msg_name = uap->from;
1078 msg.msg_iov = &aiov;
1080 aiov.iov_base = uap->buf;
1081 aiov.iov_len = uap->len;
1082 msg.msg_control = 0;
1083 msg.msg_flags = uap->flags;
1084 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1089 #ifdef COMPAT_OLDSOCK
1091 orecvfrom(struct thread *td, struct recvfrom_args *uap)
1094 uap->flags |= MSG_COMPAT;
1095 return (sys_recvfrom(td, uap));
1099 #ifdef COMPAT_OLDSOCK
1101 orecv(struct thread *td, struct orecv_args *uap)
1107 msg.msg_namelen = 0;
1108 msg.msg_iov = &aiov;
1110 aiov.iov_base = uap->buf;
1111 aiov.iov_len = uap->len;
1112 msg.msg_control = 0;
1113 msg.msg_flags = uap->flags;
1114 return (recvit(td, uap->s, &msg, NULL));
1118 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1119 * overlays the new one, missing only the flags, and with the (old) access
1120 * rights where the control fields are now.
1123 orecvmsg(struct thread *td, struct orecvmsg_args *uap)
1129 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1132 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1135 msg.msg_flags = uap->flags | MSG_COMPAT;
1137 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1138 if (msg.msg_controllen && error == 0)
1139 error = copyout(&msg.msg_controllen,
1140 &uap->msg->msg_accrightslen, sizeof (int));
1147 sys_recvmsg(struct thread *td, struct recvmsg_args *uap)
1150 struct iovec *uiov, *iov;
1153 error = copyin(uap->msg, &msg, sizeof (msg));
1156 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1159 msg.msg_flags = uap->flags;
1160 #ifdef COMPAT_OLDSOCK
1161 msg.msg_flags &= ~MSG_COMPAT;
1165 error = recvit(td, uap->s, &msg, NULL);
1168 error = copyout(&msg, uap->msg, sizeof(msg));
1175 sys_shutdown(struct thread *td, struct shutdown_args *uap)
1178 return (kern_shutdown(td, uap->s, uap->how));
1182 kern_shutdown(struct thread *td, int s, int how)
1186 cap_rights_t rights;
1190 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SHUTDOWN),
1194 error = soshutdown(so, how);
1196 * Previous versions did not return ENOTCONN, but 0 in
1197 * case the socket was not connected. Some important
1198 * programs like syslogd up to r279016, 2015-02-19,
1199 * still depend on this behavior.
1201 if (error == ENOTCONN &&
1202 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1210 sys_setsockopt(struct thread *td, struct setsockopt_args *uap)
1213 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1214 uap->val, UIO_USERSPACE, uap->valsize));
1218 kern_setsockopt(struct thread *td, int s, int level, int name, void *val,
1219 enum uio_seg valseg, socklen_t valsize)
1223 struct sockopt sopt;
1224 cap_rights_t rights;
1227 if (val == NULL && valsize != 0)
1229 if ((int)valsize < 0)
1232 sopt.sopt_dir = SOPT_SET;
1233 sopt.sopt_level = level;
1234 sopt.sopt_name = name;
1235 sopt.sopt_val = val;
1236 sopt.sopt_valsize = valsize;
1242 sopt.sopt_td = NULL;
1245 panic("kern_setsockopt called with bad valseg");
1249 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SETSOCKOPT),
1253 error = sosetopt(so, &sopt);
1260 sys_getsockopt(struct thread *td, struct getsockopt_args *uap)
1266 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1271 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1272 uap->val, UIO_USERSPACE, &valsize);
1275 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1280 * Kernel version of getsockopt.
1281 * optval can be a userland or userspace. optlen is always a kernel pointer.
1284 kern_getsockopt(struct thread *td, int s, int level, int name, void *val,
1285 enum uio_seg valseg, socklen_t *valsize)
1289 struct sockopt sopt;
1290 cap_rights_t rights;
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_cap(td, s, cap_rights_init(&rights, CAP_GETSOCKOPT),
1319 error = sogetopt(so, &sopt);
1320 *valsize = sopt.sopt_valsize;
1327 * getsockname1() - Get socket name.
1330 getsockname1(struct thread *td, struct getsockname_args *uap, int compat)
1332 struct sockaddr *sa;
1336 error = copyin(uap->alen, &len, sizeof(len));
1340 error = kern_getsockname(td, uap->fdes, &sa, &len);
1345 #ifdef COMPAT_OLDSOCK
1347 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1349 error = copyout(sa, uap->asa, (u_int)len);
1353 error = copyout(&len, uap->alen, sizeof(len));
1358 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1363 cap_rights_t rights;
1368 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETSOCKNAME),
1374 CURVNET_SET(so->so_vnet);
1375 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1382 len = MIN(*alen, (*sa)->sa_len);
1385 if (KTRPOINT(td, KTR_STRUCT))
1390 if (error != 0 && *sa != NULL) {
1391 free(*sa, M_SONAME);
1398 sys_getsockname(struct thread *td, struct getsockname_args *uap)
1401 return (getsockname1(td, uap, 0));
1404 #ifdef COMPAT_OLDSOCK
1406 ogetsockname(struct thread *td, struct getsockname_args *uap)
1409 return (getsockname1(td, uap, 1));
1411 #endif /* COMPAT_OLDSOCK */
1414 * getpeername1() - Get name of peer for connected socket.
1417 getpeername1(struct thread *td, struct getpeername_args *uap, int compat)
1419 struct sockaddr *sa;
1423 error = copyin(uap->alen, &len, sizeof (len));
1427 error = kern_getpeername(td, uap->fdes, &sa, &len);
1432 #ifdef COMPAT_OLDSOCK
1434 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1436 error = copyout(sa, uap->asa, (u_int)len);
1440 error = copyout(&len, uap->alen, sizeof(len));
1445 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1450 cap_rights_t rights;
1455 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETPEERNAME),
1460 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1465 CURVNET_SET(so->so_vnet);
1466 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1473 len = MIN(*alen, (*sa)->sa_len);
1476 if (KTRPOINT(td, KTR_STRUCT))
1480 if (error != 0 && *sa != NULL) {
1481 free(*sa, M_SONAME);
1490 sys_getpeername(struct thread *td, struct getpeername_args *uap)
1493 return (getpeername1(td, uap, 0));
1496 #ifdef COMPAT_OLDSOCK
1498 ogetpeername(struct thread *td, struct ogetpeername_args *uap)
1501 /* XXX uap should have type `getpeername_args *' to begin with. */
1502 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1504 #endif /* COMPAT_OLDSOCK */
1507 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1509 struct sockaddr *sa;
1513 if (buflen > MLEN) {
1514 #ifdef COMPAT_OLDSOCK
1515 if (type == MT_SONAME && buflen <= 112)
1516 buflen = MLEN; /* unix domain compat. hack */
1519 if (buflen > MCLBYTES)
1522 m = m_get2(buflen, M_WAITOK, type, 0);
1524 error = copyin(buf, mtod(m, void *), buflen);
1529 if (type == MT_SONAME) {
1530 sa = mtod(m, struct sockaddr *);
1532 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1533 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1534 sa->sa_family = sa->sa_len;
1536 sa->sa_len = buflen;
1543 getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
1545 struct sockaddr *sa;
1548 if (len > SOCK_MAXADDRLEN)
1549 return (ENAMETOOLONG);
1550 if (len < offsetof(struct sockaddr, sa_data[0]))
1552 sa = malloc(len, M_SONAME, M_WAITOK);
1553 error = copyin(uaddr, sa, len);
1557 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1558 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1559 sa->sa_family = sa->sa_len;