2 * Copyright (c) 1982, 1986, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_capsicum.h"
37 #include "opt_inet6.h"
38 #include "opt_compat.h"
39 #include "opt_ktrace.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/capsicum.h>
44 #include <sys/kernel.h>
46 #include <sys/mutex.h>
47 #include <sys/sysproto.h>
48 #include <sys/malloc.h>
49 #include <sys/filedesc.h>
51 #include <sys/filio.h>
54 #include <sys/protosw.h>
55 #include <sys/rwlock.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/syscallsubr.h>
60 #include <sys/ktrace.h>
62 #ifdef COMPAT_FREEBSD32
63 #include <compat/freebsd32/freebsd32_util.h>
68 #include <security/audit/audit.h>
69 #include <security/mac/mac_framework.h>
72 * Flags for accept1() and kern_accept4(), in addition to SOCK_CLOEXEC
75 #define ACCEPT4_INHERIT 0x1
76 #define ACCEPT4_COMPAT 0x2
78 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
79 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
81 static int accept1(struct thread *td, int s, struct sockaddr *uname,
82 socklen_t *anamelen, int flags);
83 static int getsockname1(struct thread *td, struct getsockname_args *uap,
85 static int getpeername1(struct thread *td, struct getpeername_args *uap,
87 static int sockargs(struct mbuf **, char *, socklen_t, int);
90 * Convert a user file descriptor to a kernel file entry and check if required
91 * capability rights are present.
92 * A reference on the file entry is held upon returning.
95 getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
96 struct file **fpp, u_int *fflagp)
101 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
104 if (fp->f_type != DTYPE_SOCKET) {
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)
126 int fd, error, type, oflag, fflag;
128 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
133 if ((type & SOCK_CLOEXEC) != 0) {
134 type &= ~SOCK_CLOEXEC;
137 if ((type & SOCK_NONBLOCK) != 0) {
138 type &= ~SOCK_NONBLOCK;
143 error = mac_socket_check_create(td->td_ucred, uap->domain, type,
148 error = falloc(td, &fp, &fd, oflag);
151 /* An extra reference on `fp' has been held for us by falloc(). */
152 error = socreate(uap->domain, &so, type, uap->protocol,
157 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
158 if ((fflag & FNONBLOCK) != 0)
159 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
160 td->td_retval[0] = fd;
167 sys_bind(struct thread *td, struct bind_args *uap)
172 error = getsockaddr(&sa, uap->name, uap->namelen);
174 error = kern_bindat(td, AT_FDCWD, uap->s, sa);
181 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
189 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
190 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_BIND),
196 if (KTRPOINT(td, KTR_STRUCT))
200 error = mac_socket_check_bind(td->td_ucred, so, sa);
203 if (dirfd == AT_FDCWD)
204 error = sobind(so, sa, td);
206 error = sobindat(dirfd, so, sa, td);
215 sys_bindat(struct thread *td, struct bindat_args *uap)
220 error = getsockaddr(&sa, uap->name, uap->namelen);
222 error = kern_bindat(td, uap->fd, uap->s, sa);
229 sys_listen(struct thread *td, struct listen_args *uap)
236 AUDIT_ARG_FD(uap->s);
237 error = getsock_cap(td, uap->s, cap_rights_init(&rights, CAP_LISTEN),
242 error = mac_socket_check_listen(td->td_ucred, so);
245 error = solisten(so, uap->backlog, td);
255 accept1(td, s, uname, anamelen, flags)
258 struct sockaddr *uname;
262 struct sockaddr *name;
268 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
270 error = copyin(anamelen, &namelen, sizeof (namelen));
274 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
279 if (error == 0 && uname != NULL) {
280 #ifdef COMPAT_OLDSOCK
281 if (flags & ACCEPT4_COMPAT)
282 ((struct osockaddr *)name)->sa_family =
285 error = copyout(name, uname, namelen);
288 error = copyout(&namelen, anamelen,
291 fdclose(td, fp, td->td_retval[0]);
293 free(name, M_SONAME);
298 kern_accept(struct thread *td, int s, struct sockaddr **name,
299 socklen_t *namelen, struct file **fp)
301 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
305 kern_accept4(struct thread *td, int s, struct sockaddr **name,
306 socklen_t *namelen, int flags, struct file **fp)
308 struct file *headfp, *nfp = NULL;
309 struct sockaddr *sa = NULL;
310 struct socket *head, *so;
320 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_ACCEPT),
324 head = headfp->f_data;
325 if ((head->so_options & SO_ACCEPTCONN) == 0) {
330 error = mac_socket_check_accept(td->td_ucred, head);
334 error = falloc(td, &nfp, &fd, (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0);
338 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
343 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
344 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
345 head->so_error = ECONNABORTED;
348 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
355 if (head->so_error) {
356 error = head->so_error;
361 so = TAILQ_FIRST(&head->so_comp);
362 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
363 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
366 * Before changing the flags on the socket, we have to bump the
367 * reference count. Otherwise, if the protocol calls sofree(),
368 * the socket will be released due to a zero refcount.
370 SOCK_LOCK(so); /* soref() and so_state update */
371 soref(so); /* file descriptor reference */
373 TAILQ_REMOVE(&head->so_comp, so, so_list);
375 if (flags & ACCEPT4_INHERIT)
376 so->so_state |= (head->so_state & SS_NBIO);
378 so->so_state |= (flags & SOCK_NONBLOCK) ? SS_NBIO : 0;
379 so->so_qstate &= ~SQ_COMP;
385 /* An extra reference on `nfp' has been held for us by falloc(). */
386 td->td_retval[0] = fd;
388 /* connection has been removed from the listen queue */
389 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
391 if (flags & ACCEPT4_INHERIT) {
392 pgid = fgetown(&head->so_sigio);
394 fsetown(pgid, &so->so_sigio);
396 fflag &= ~(FNONBLOCK | FASYNC);
397 if (flags & SOCK_NONBLOCK)
401 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
402 /* Sync socket nonblocking/async state with file flags */
403 tmp = fflag & FNONBLOCK;
404 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
405 tmp = fflag & FASYNC;
406 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
408 error = soaccept(so, &sa);
416 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
418 /* check sa_len before it is destroyed */
419 if (*namelen > sa->sa_len)
420 *namelen = sa->sa_len;
422 if (KTRPOINT(td, KTR_STRUCT))
432 * close the new descriptor, assuming someone hasn't ripped it
436 fdclose(td, nfp, fd);
439 * Release explicitly held references before returning. We return
440 * a reference on nfp to the caller on success if they request it.
459 struct accept_args *uap;
462 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
468 struct accept4_args *uap;
471 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
474 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
477 #ifdef COMPAT_OLDSOCK
481 struct accept_args *uap;
484 return (accept1(td, uap->s, uap->name, uap->anamelen,
485 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
487 #endif /* COMPAT_OLDSOCK */
490 sys_connect(struct thread *td, struct connect_args *uap)
495 error = getsockaddr(&sa, uap->name, uap->namelen);
497 error = kern_connectat(td, AT_FDCWD, uap->s, sa);
504 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
509 int error, interrupted = 0;
512 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
513 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_CONNECT),
518 if (so->so_state & SS_ISCONNECTING) {
523 if (KTRPOINT(td, KTR_STRUCT))
527 error = mac_socket_check_connect(td->td_ucred, so, sa);
531 if (dirfd == AT_FDCWD)
532 error = soconnect(so, sa, td);
534 error = soconnectat(dirfd, so, sa, td);
537 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
542 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
543 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
546 if (error == EINTR || error == ERESTART)
552 error = so->so_error;
558 so->so_state &= ~SS_ISCONNECTING;
559 if (error == ERESTART)
567 sys_connectat(struct thread *td, struct connectat_args *uap)
572 error = getsockaddr(&sa, uap->name, uap->namelen);
574 error = kern_connectat(td, uap->fd, uap->s, sa);
581 kern_socketpair(struct thread *td, int domain, int type, int protocol,
584 struct file *fp1, *fp2;
585 struct socket *so1, *so2;
586 int fd, error, oflag, fflag;
588 AUDIT_ARG_SOCKET(domain, type, protocol);
592 if ((type & SOCK_CLOEXEC) != 0) {
593 type &= ~SOCK_CLOEXEC;
596 if ((type & SOCK_NONBLOCK) != 0) {
597 type &= ~SOCK_NONBLOCK;
601 /* We might want to have a separate check for socket pairs. */
602 error = mac_socket_check_create(td->td_ucred, domain, type,
607 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
610 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
613 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
614 error = falloc(td, &fp1, &fd, oflag);
618 fp1->f_data = so1; /* so1 already has ref count */
619 error = falloc(td, &fp2, &fd, oflag);
622 fp2->f_data = so2; /* so2 already has ref count */
624 error = soconnect2(so1, so2);
627 if (type == SOCK_DGRAM) {
629 * Datagram socket connection is asymmetric.
631 error = soconnect2(so2, so1);
635 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
637 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
639 if ((fflag & FNONBLOCK) != 0) {
640 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
641 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
647 fdclose(td, fp2, rsv[1]);
650 fdclose(td, fp1, rsv[0]);
662 sys_socketpair(struct thread *td, struct socketpair_args *uap)
666 error = kern_socketpair(td, uap->domain, uap->type,
670 error = copyout(sv, uap->rsv, 2 * sizeof(int));
672 (void)kern_close(td, sv[0]);
673 (void)kern_close(td, sv[1]);
679 sendit(struct thread *td, int s, struct msghdr *mp, int flags)
681 struct mbuf *control;
685 #ifdef CAPABILITY_MODE
686 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
690 if (mp->msg_name != NULL) {
691 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
701 if (mp->msg_control) {
702 if (mp->msg_controllen < sizeof(struct cmsghdr)
703 #ifdef COMPAT_OLDSOCK
704 && mp->msg_flags != MSG_COMPAT
710 error = sockargs(&control, mp->msg_control,
711 mp->msg_controllen, MT_CONTROL);
714 #ifdef COMPAT_OLDSOCK
715 if (mp->msg_flags == MSG_COMPAT) {
718 M_PREPEND(control, sizeof(*cm), M_WAITOK);
719 cm = mtod(control, struct cmsghdr *);
720 cm->cmsg_len = control->m_len;
721 cm->cmsg_level = SOL_SOCKET;
722 cm->cmsg_type = SCM_RIGHTS;
729 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
737 kern_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
738 struct mbuf *control, enum uio_seg segflg)
746 struct uio *ktruio = NULL;
752 cap_rights_init(&rights, CAP_SEND);
753 if (mp->msg_name != NULL) {
754 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
755 cap_rights_set(&rights, CAP_CONNECT);
757 error = getsock_cap(td, s, &rights, &fp, NULL);
760 so = (struct socket *)fp->f_data;
763 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
764 ktrsockaddr(mp->msg_name);
767 if (mp->msg_name != NULL) {
768 error = mac_socket_check_connect(td->td_ucred, so,
773 error = mac_socket_check_send(td->td_ucred, so);
778 auio.uio_iov = mp->msg_iov;
779 auio.uio_iovcnt = mp->msg_iovlen;
780 auio.uio_segflg = segflg;
781 auio.uio_rw = UIO_WRITE;
783 auio.uio_offset = 0; /* XXX */
786 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
787 if ((auio.uio_resid += iov->iov_len) < 0) {
793 if (KTRPOINT(td, KTR_GENIO))
794 ktruio = cloneuio(&auio);
796 len = auio.uio_resid;
797 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
799 if (auio.uio_resid != len && (error == ERESTART ||
800 error == EINTR || error == EWOULDBLOCK))
802 /* Generation of SIGPIPE can be controlled per socket */
803 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
804 !(flags & MSG_NOSIGNAL)) {
805 PROC_LOCK(td->td_proc);
806 tdsignal(td, SIGPIPE);
807 PROC_UNLOCK(td->td_proc);
811 td->td_retval[0] = len - auio.uio_resid;
813 if (ktruio != NULL) {
814 ktruio->uio_resid = td->td_retval[0];
815 ktrgenio(s, UIO_WRITE, ktruio, error);
824 sys_sendto(struct thread *td, struct sendto_args *uap)
829 msg.msg_name = uap->to;
830 msg.msg_namelen = uap->tolen;
834 #ifdef COMPAT_OLDSOCK
837 aiov.iov_base = uap->buf;
838 aiov.iov_len = uap->len;
839 return (sendit(td, uap->s, &msg, uap->flags));
842 #ifdef COMPAT_OLDSOCK
844 osend(struct thread *td, struct osend_args *uap)
853 aiov.iov_base = uap->buf;
854 aiov.iov_len = uap->len;
857 return (sendit(td, uap->s, &msg, uap->flags));
861 osendmsg(struct thread *td, struct osendmsg_args *uap)
867 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
870 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
874 msg.msg_flags = MSG_COMPAT;
875 error = sendit(td, uap->s, &msg, uap->flags);
882 sys_sendmsg(struct thread *td, struct sendmsg_args *uap)
888 error = copyin(uap->msg, &msg, sizeof (msg));
891 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
895 #ifdef COMPAT_OLDSOCK
898 error = sendit(td, uap->s, &msg, uap->flags);
904 kern_recvit(struct thread *td, int s, struct msghdr *mp, enum uio_seg fromseg,
905 struct mbuf **controlp)
909 struct mbuf *m, *control = NULL;
913 struct sockaddr *fromsa = NULL;
916 struct uio *ktruio = NULL;
921 if (controlp != NULL)
925 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_RECV),
932 error = mac_socket_check_receive(td->td_ucred, so);
939 auio.uio_iov = mp->msg_iov;
940 auio.uio_iovcnt = mp->msg_iovlen;
941 auio.uio_segflg = UIO_USERSPACE;
942 auio.uio_rw = UIO_READ;
944 auio.uio_offset = 0; /* XXX */
947 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
948 if ((auio.uio_resid += iov->iov_len) < 0) {
954 if (KTRPOINT(td, KTR_GENIO))
955 ktruio = cloneuio(&auio);
957 len = auio.uio_resid;
958 error = soreceive(so, &fromsa, &auio, NULL,
959 (mp->msg_control || controlp) ? &control : NULL,
962 if (auio.uio_resid != len && (error == ERESTART ||
963 error == EINTR || error == EWOULDBLOCK))
967 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
969 if (ktruio != NULL) {
970 ktruio->uio_resid = len - auio.uio_resid;
971 ktrgenio(s, UIO_READ, ktruio, error);
976 td->td_retval[0] = len - auio.uio_resid;
978 len = mp->msg_namelen;
979 if (len <= 0 || fromsa == NULL)
982 /* save sa_len before it is destroyed by MSG_COMPAT */
983 len = MIN(len, fromsa->sa_len);
984 #ifdef COMPAT_OLDSOCK
985 if (mp->msg_flags & MSG_COMPAT)
986 ((struct osockaddr *)fromsa)->sa_family =
989 if (fromseg == UIO_USERSPACE) {
990 error = copyout(fromsa, mp->msg_name,
995 bcopy(fromsa, mp->msg_name, len);
997 mp->msg_namelen = len;
999 if (mp->msg_control && controlp == NULL) {
1000 #ifdef COMPAT_OLDSOCK
1002 * We assume that old recvmsg calls won't receive access
1003 * rights and other control info, esp. as control info
1004 * is always optional and those options didn't exist in 4.3.
1005 * If we receive rights, trim the cmsghdr; anything else
1008 if (control && mp->msg_flags & MSG_COMPAT) {
1009 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1011 mtod(control, struct cmsghdr *)->cmsg_type !=
1013 mp->msg_controllen = 0;
1016 control->m_len -= sizeof (struct cmsghdr);
1017 control->m_data += sizeof (struct cmsghdr);
1020 len = mp->msg_controllen;
1022 mp->msg_controllen = 0;
1023 ctlbuf = mp->msg_control;
1025 while (m && len > 0) {
1026 unsigned int tocopy;
1028 if (len >= m->m_len)
1031 mp->msg_flags |= MSG_CTRUNC;
1035 if ((error = copyout(mtod(m, caddr_t),
1036 ctlbuf, tocopy)) != 0)
1043 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1048 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1049 ktrsockaddr(fromsa);
1051 free(fromsa, M_SONAME);
1053 if (error == 0 && controlp != NULL)
1054 *controlp = control;
1062 recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp)
1066 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1069 if (namelenp != NULL) {
1070 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1071 #ifdef COMPAT_OLDSOCK
1072 if (mp->msg_flags & MSG_COMPAT)
1073 error = 0; /* old recvfrom didn't check */
1080 sys_recvfrom(struct thread *td, struct recvfrom_args *uap)
1086 if (uap->fromlenaddr) {
1087 error = copyin(uap->fromlenaddr,
1088 &msg.msg_namelen, sizeof (msg.msg_namelen));
1092 msg.msg_namelen = 0;
1094 msg.msg_name = uap->from;
1095 msg.msg_iov = &aiov;
1097 aiov.iov_base = uap->buf;
1098 aiov.iov_len = uap->len;
1099 msg.msg_control = 0;
1100 msg.msg_flags = uap->flags;
1101 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1106 #ifdef COMPAT_OLDSOCK
1108 orecvfrom(struct thread *td, struct recvfrom_args *uap)
1111 uap->flags |= MSG_COMPAT;
1112 return (sys_recvfrom(td, uap));
1116 #ifdef COMPAT_OLDSOCK
1118 orecv(struct thread *td, struct orecv_args *uap)
1124 msg.msg_namelen = 0;
1125 msg.msg_iov = &aiov;
1127 aiov.iov_base = uap->buf;
1128 aiov.iov_len = uap->len;
1129 msg.msg_control = 0;
1130 msg.msg_flags = uap->flags;
1131 return (recvit(td, uap->s, &msg, NULL));
1135 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1136 * overlays the new one, missing only the flags, and with the (old) access
1137 * rights where the control fields are now.
1140 orecvmsg(struct thread *td, struct orecvmsg_args *uap)
1146 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1149 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1152 msg.msg_flags = uap->flags | MSG_COMPAT;
1154 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1155 if (msg.msg_controllen && error == 0)
1156 error = copyout(&msg.msg_controllen,
1157 &uap->msg->msg_accrightslen, sizeof (int));
1164 sys_recvmsg(struct thread *td, struct recvmsg_args *uap)
1167 struct iovec *uiov, *iov;
1170 error = copyin(uap->msg, &msg, sizeof (msg));
1173 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1176 msg.msg_flags = uap->flags;
1177 #ifdef COMPAT_OLDSOCK
1178 msg.msg_flags &= ~MSG_COMPAT;
1182 error = recvit(td, uap->s, &msg, NULL);
1185 error = copyout(&msg, uap->msg, sizeof(msg));
1192 sys_shutdown(struct thread *td, struct shutdown_args *uap)
1196 cap_rights_t rights;
1199 AUDIT_ARG_FD(uap->s);
1200 error = getsock_cap(td, uap->s, cap_rights_init(&rights, CAP_SHUTDOWN),
1204 error = soshutdown(so, uap->how);
1206 * Previous versions did not return ENOTCONN, but 0 in
1207 * case the socket was not connected. Some important
1208 * programs like syslogd up to r279016, 2015-02-19,
1209 * still depend on this behavior.
1211 if (error == ENOTCONN &&
1212 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1220 sys_setsockopt(struct thread *td, struct setsockopt_args *uap)
1223 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1224 uap->val, UIO_USERSPACE, uap->valsize));
1228 kern_setsockopt(struct thread *td, int s, int level, int name, void *val,
1229 enum uio_seg valseg, socklen_t valsize)
1233 struct sockopt sopt;
1234 cap_rights_t rights;
1237 if (val == NULL && valsize != 0)
1239 if ((int)valsize < 0)
1242 sopt.sopt_dir = SOPT_SET;
1243 sopt.sopt_level = level;
1244 sopt.sopt_name = name;
1245 sopt.sopt_val = val;
1246 sopt.sopt_valsize = valsize;
1252 sopt.sopt_td = NULL;
1255 panic("kern_setsockopt called with bad valseg");
1259 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SETSOCKOPT),
1263 error = sosetopt(so, &sopt);
1270 sys_getsockopt(struct thread *td, struct getsockopt_args *uap)
1276 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1281 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1282 uap->val, UIO_USERSPACE, &valsize);
1285 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1290 * Kernel version of getsockopt.
1291 * optval can be a userland or userspace. optlen is always a kernel pointer.
1294 kern_getsockopt(struct thread *td, int s, int level, int name, void *val,
1295 enum uio_seg valseg, socklen_t *valsize)
1299 struct sockopt sopt;
1300 cap_rights_t rights;
1305 if ((int)*valsize < 0)
1308 sopt.sopt_dir = SOPT_GET;
1309 sopt.sopt_level = level;
1310 sopt.sopt_name = name;
1311 sopt.sopt_val = val;
1312 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1318 sopt.sopt_td = NULL;
1321 panic("kern_getsockopt called with bad valseg");
1325 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_GETSOCKOPT),
1329 error = sogetopt(so, &sopt);
1330 *valsize = sopt.sopt_valsize;
1337 * getsockname1() - Get socket name.
1340 getsockname1(struct thread *td, struct getsockname_args *uap, int compat)
1342 struct sockaddr *sa;
1346 error = copyin(uap->alen, &len, sizeof(len));
1350 error = kern_getsockname(td, uap->fdes, &sa, &len);
1355 #ifdef COMPAT_OLDSOCK
1357 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1359 error = copyout(sa, uap->asa, (u_int)len);
1363 error = copyout(&len, uap->alen, sizeof(len));
1368 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1373 cap_rights_t rights;
1378 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETSOCKNAME),
1384 CURVNET_SET(so->so_vnet);
1385 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1392 len = MIN(*alen, (*sa)->sa_len);
1395 if (KTRPOINT(td, KTR_STRUCT))
1400 if (error != 0 && *sa != NULL) {
1401 free(*sa, M_SONAME);
1408 sys_getsockname(struct thread *td, struct getsockname_args *uap)
1411 return (getsockname1(td, uap, 0));
1414 #ifdef COMPAT_OLDSOCK
1416 ogetsockname(struct thread *td, struct getsockname_args *uap)
1419 return (getsockname1(td, uap, 1));
1421 #endif /* COMPAT_OLDSOCK */
1424 * getpeername1() - Get name of peer for connected socket.
1427 getpeername1(struct thread *td, struct getpeername_args *uap, int compat)
1429 struct sockaddr *sa;
1433 error = copyin(uap->alen, &len, sizeof (len));
1437 error = kern_getpeername(td, uap->fdes, &sa, &len);
1442 #ifdef COMPAT_OLDSOCK
1444 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1446 error = copyout(sa, uap->asa, (u_int)len);
1450 error = copyout(&len, uap->alen, sizeof(len));
1455 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1460 cap_rights_t rights;
1465 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETPEERNAME),
1470 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1475 CURVNET_SET(so->so_vnet);
1476 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1483 len = MIN(*alen, (*sa)->sa_len);
1486 if (KTRPOINT(td, KTR_STRUCT))
1490 if (error != 0 && *sa != NULL) {
1491 free(*sa, M_SONAME);
1500 sys_getpeername(struct thread *td, struct getpeername_args *uap)
1503 return (getpeername1(td, uap, 0));
1506 #ifdef COMPAT_OLDSOCK
1508 ogetpeername(struct thread *td, struct ogetpeername_args *uap)
1511 /* XXX uap should have type `getpeername_args *' to begin with. */
1512 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1514 #endif /* COMPAT_OLDSOCK */
1517 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1519 struct sockaddr *sa;
1523 if (buflen > MLEN) {
1524 #ifdef COMPAT_OLDSOCK
1525 if (type == MT_SONAME && buflen <= 112)
1526 buflen = MLEN; /* unix domain compat. hack */
1529 if (buflen > MCLBYTES)
1532 m = m_get2(buflen, M_WAITOK, type, 0);
1534 error = copyin(buf, mtod(m, void *), buflen);
1539 if (type == MT_SONAME) {
1540 sa = mtod(m, struct sockaddr *);
1542 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1543 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1544 sa->sa_family = sa->sa_len;
1546 sa->sa_len = buflen;
1553 getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
1555 struct sockaddr *sa;
1558 if (len > SOCK_MAXADDRLEN)
1559 return (ENAMETOOLONG);
1560 if (len < offsetof(struct sockaddr, sa_data[0]))
1562 sa = malloc(len, M_SONAME, M_WAITOK);
1563 error = copyin(uaddr, sa, len);
1567 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1568 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1569 sa->sa_family = sa->sa_len;