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 * 4. 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
124 struct socket_args /* {
132 int fd, error, type, oflag, fflag;
134 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->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, uap->domain, type,
154 error = falloc(td, &fp, &fd, oflag);
157 /* An extra reference on `fp' has been held for us by falloc(). */
158 error = socreate(uap->domain, &so, type, uap->protocol,
163 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
164 if ((fflag & FNONBLOCK) != 0)
165 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
166 td->td_retval[0] = fd;
176 struct bind_args /* {
185 error = getsockaddr(&sa, uap->name, uap->namelen);
187 error = kern_bindat(td, AT_FDCWD, uap->s, sa);
194 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
202 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
203 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_BIND),
209 if (KTRPOINT(td, KTR_STRUCT))
213 error = mac_socket_check_bind(td->td_ucred, so, sa);
216 if (dirfd == AT_FDCWD)
217 error = sobind(so, sa, td);
219 error = sobindat(dirfd, so, sa, td);
231 struct bindat_args /* {
241 error = getsockaddr(&sa, uap->name, uap->namelen);
243 error = kern_bindat(td, uap->fd, uap->s, sa);
253 struct listen_args /* {
263 AUDIT_ARG_FD(uap->s);
264 error = getsock_cap(td, uap->s, cap_rights_init(&rights, CAP_LISTEN),
269 error = mac_socket_check_listen(td->td_ucred, so);
272 error = solisten(so, uap->backlog, td);
282 accept1(td, s, uname, anamelen, flags)
285 struct sockaddr *uname;
289 struct sockaddr *name;
295 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
297 error = copyin(anamelen, &namelen, sizeof (namelen));
301 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
306 if (error == 0 && uname != NULL) {
307 #ifdef COMPAT_OLDSOCK
308 if (flags & ACCEPT4_COMPAT)
309 ((struct osockaddr *)name)->sa_family =
312 error = copyout(name, uname, namelen);
315 error = copyout(&namelen, anamelen,
318 fdclose(td, fp, td->td_retval[0]);
320 free(name, M_SONAME);
325 kern_accept(struct thread *td, int s, struct sockaddr **name,
326 socklen_t *namelen, struct file **fp)
328 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
332 kern_accept4(struct thread *td, int s, struct sockaddr **name,
333 socklen_t *namelen, int flags, struct file **fp)
335 struct file *headfp, *nfp = NULL;
336 struct sockaddr *sa = NULL;
337 struct socket *head, *so;
347 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_ACCEPT),
351 head = headfp->f_data;
352 if ((head->so_options & SO_ACCEPTCONN) == 0) {
357 error = mac_socket_check_accept(td->td_ucred, head);
361 error = falloc(td, &nfp, &fd, (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0);
365 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
370 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
371 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
372 head->so_error = ECONNABORTED;
375 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
382 if (head->so_error) {
383 error = head->so_error;
388 so = TAILQ_FIRST(&head->so_comp);
389 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
390 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
393 * Before changing the flags on the socket, we have to bump the
394 * reference count. Otherwise, if the protocol calls sofree(),
395 * the socket will be released due to a zero refcount.
397 SOCK_LOCK(so); /* soref() and so_state update */
398 soref(so); /* file descriptor reference */
400 TAILQ_REMOVE(&head->so_comp, so, so_list);
402 if (flags & ACCEPT4_INHERIT)
403 so->so_state |= (head->so_state & SS_NBIO);
405 so->so_state |= (flags & SOCK_NONBLOCK) ? SS_NBIO : 0;
406 so->so_qstate &= ~SQ_COMP;
412 /* An extra reference on `nfp' has been held for us by falloc(). */
413 td->td_retval[0] = fd;
415 /* connection has been removed from the listen queue */
416 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
418 if (flags & ACCEPT4_INHERIT) {
419 pgid = fgetown(&head->so_sigio);
421 fsetown(pgid, &so->so_sigio);
423 fflag &= ~(FNONBLOCK | FASYNC);
424 if (flags & SOCK_NONBLOCK)
428 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
429 /* Sync socket nonblocking/async state with file flags */
430 tmp = fflag & FNONBLOCK;
431 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
432 tmp = fflag & FASYNC;
433 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
435 error = soaccept(so, &sa);
443 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
445 /* check sa_len before it is destroyed */
446 if (*namelen > sa->sa_len)
447 *namelen = sa->sa_len;
449 if (KTRPOINT(td, KTR_STRUCT))
459 * close the new descriptor, assuming someone hasn't ripped it
463 fdclose(td, nfp, fd);
466 * Release explicitly held references before returning. We return
467 * a reference on nfp to the caller on success if they request it.
486 struct accept_args *uap;
489 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
495 struct accept4_args *uap;
498 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
501 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
504 #ifdef COMPAT_OLDSOCK
508 struct accept_args *uap;
511 return (accept1(td, uap->s, uap->name, uap->anamelen,
512 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
514 #endif /* COMPAT_OLDSOCK */
520 struct connect_args /* {
529 error = getsockaddr(&sa, uap->name, uap->namelen);
531 error = kern_connectat(td, AT_FDCWD, uap->s, sa);
538 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
543 int error, interrupted = 0;
546 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
547 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_CONNECT),
552 if (so->so_state & SS_ISCONNECTING) {
557 if (KTRPOINT(td, KTR_STRUCT))
561 error = mac_socket_check_connect(td->td_ucred, so, sa);
565 if (dirfd == AT_FDCWD)
566 error = soconnect(so, sa, td);
568 error = soconnectat(dirfd, so, sa, td);
571 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
576 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
577 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
580 if (error == EINTR || error == ERESTART)
586 error = so->so_error;
592 so->so_state &= ~SS_ISCONNECTING;
593 if (error == ERESTART)
602 sys_connectat(td, uap)
604 struct connectat_args /* {
614 error = getsockaddr(&sa, uap->name, uap->namelen);
616 error = kern_connectat(td, uap->fd, uap->s, sa);
623 kern_socketpair(struct thread *td, int domain, int type, int protocol,
626 struct file *fp1, *fp2;
627 struct socket *so1, *so2;
628 int fd, error, oflag, fflag;
630 AUDIT_ARG_SOCKET(domain, type, protocol);
634 if ((type & SOCK_CLOEXEC) != 0) {
635 type &= ~SOCK_CLOEXEC;
638 if ((type & SOCK_NONBLOCK) != 0) {
639 type &= ~SOCK_NONBLOCK;
643 /* We might want to have a separate check for socket pairs. */
644 error = mac_socket_check_create(td->td_ucred, domain, type,
649 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
652 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
655 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
656 error = falloc(td, &fp1, &fd, oflag);
660 fp1->f_data = so1; /* so1 already has ref count */
661 error = falloc(td, &fp2, &fd, oflag);
664 fp2->f_data = so2; /* so2 already has ref count */
666 error = soconnect2(so1, so2);
669 if (type == SOCK_DGRAM) {
671 * Datagram socket connection is asymmetric.
673 error = soconnect2(so2, so1);
677 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
679 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
681 if ((fflag & FNONBLOCK) != 0) {
682 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
683 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
689 fdclose(td, fp2, rsv[1]);
692 fdclose(td, fp1, rsv[0]);
704 sys_socketpair(struct thread *td, struct socketpair_args *uap)
708 error = kern_socketpair(td, uap->domain, uap->type,
712 error = copyout(sv, uap->rsv, 2 * sizeof(int));
714 (void)kern_close(td, sv[0]);
715 (void)kern_close(td, sv[1]);
721 sendit(td, s, mp, flags)
727 struct mbuf *control;
731 #ifdef CAPABILITY_MODE
732 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
736 if (mp->msg_name != NULL) {
737 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
747 if (mp->msg_control) {
748 if (mp->msg_controllen < sizeof(struct cmsghdr)
749 #ifdef COMPAT_OLDSOCK
750 && mp->msg_flags != MSG_COMPAT
756 error = sockargs(&control, mp->msg_control,
757 mp->msg_controllen, MT_CONTROL);
760 #ifdef COMPAT_OLDSOCK
761 if (mp->msg_flags == MSG_COMPAT) {
764 M_PREPEND(control, sizeof(*cm), M_WAITOK);
765 cm = mtod(control, struct cmsghdr *);
766 cm->cmsg_len = control->m_len;
767 cm->cmsg_level = SOL_SOCKET;
768 cm->cmsg_type = SCM_RIGHTS;
775 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
783 kern_sendit(td, s, mp, flags, control, segflg)
788 struct mbuf *control;
797 struct uio *ktruio = NULL;
803 cap_rights_init(&rights, CAP_SEND);
804 if (mp->msg_name != NULL) {
805 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
806 cap_rights_set(&rights, CAP_CONNECT);
808 error = getsock_cap(td, s, &rights, &fp, NULL);
811 so = (struct socket *)fp->f_data;
814 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
815 ktrsockaddr(mp->msg_name);
818 if (mp->msg_name != NULL) {
819 error = mac_socket_check_connect(td->td_ucred, so,
824 error = mac_socket_check_send(td->td_ucred, so);
829 auio.uio_iov = mp->msg_iov;
830 auio.uio_iovcnt = mp->msg_iovlen;
831 auio.uio_segflg = segflg;
832 auio.uio_rw = UIO_WRITE;
834 auio.uio_offset = 0; /* XXX */
837 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
838 if ((auio.uio_resid += iov->iov_len) < 0) {
844 if (KTRPOINT(td, KTR_GENIO))
845 ktruio = cloneuio(&auio);
847 len = auio.uio_resid;
848 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
850 if (auio.uio_resid != len && (error == ERESTART ||
851 error == EINTR || error == EWOULDBLOCK))
853 /* Generation of SIGPIPE can be controlled per socket */
854 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
855 !(flags & MSG_NOSIGNAL)) {
856 PROC_LOCK(td->td_proc);
857 tdsignal(td, SIGPIPE);
858 PROC_UNLOCK(td->td_proc);
862 td->td_retval[0] = len - auio.uio_resid;
864 if (ktruio != NULL) {
865 ktruio->uio_resid = td->td_retval[0];
866 ktrgenio(s, UIO_WRITE, ktruio, error);
877 struct sendto_args /* {
889 msg.msg_name = uap->to;
890 msg.msg_namelen = uap->tolen;
894 #ifdef COMPAT_OLDSOCK
897 aiov.iov_base = uap->buf;
898 aiov.iov_len = uap->len;
899 return (sendit(td, uap->s, &msg, uap->flags));
902 #ifdef COMPAT_OLDSOCK
906 struct osend_args /* {
920 aiov.iov_base = uap->buf;
921 aiov.iov_len = uap->len;
924 return (sendit(td, uap->s, &msg, uap->flags));
930 struct osendmsg_args /* {
940 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
943 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
947 msg.msg_flags = MSG_COMPAT;
948 error = sendit(td, uap->s, &msg, uap->flags);
957 struct sendmsg_args /* {
967 error = copyin(uap->msg, &msg, sizeof (msg));
970 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
974 #ifdef COMPAT_OLDSOCK
977 error = sendit(td, uap->s, &msg, uap->flags);
983 kern_recvit(td, s, mp, fromseg, controlp)
987 enum uio_seg fromseg;
988 struct mbuf **controlp;
992 struct mbuf *m, *control = NULL;
996 struct sockaddr *fromsa = NULL;
999 struct uio *ktruio = NULL;
1004 if (controlp != NULL)
1008 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_RECV),
1015 error = mac_socket_check_receive(td->td_ucred, so);
1022 auio.uio_iov = mp->msg_iov;
1023 auio.uio_iovcnt = mp->msg_iovlen;
1024 auio.uio_segflg = UIO_USERSPACE;
1025 auio.uio_rw = UIO_READ;
1027 auio.uio_offset = 0; /* XXX */
1030 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
1031 if ((auio.uio_resid += iov->iov_len) < 0) {
1037 if (KTRPOINT(td, KTR_GENIO))
1038 ktruio = cloneuio(&auio);
1040 len = auio.uio_resid;
1041 error = soreceive(so, &fromsa, &auio, NULL,
1042 (mp->msg_control || controlp) ? &control : NULL,
1045 if (auio.uio_resid != len && (error == ERESTART ||
1046 error == EINTR || error == EWOULDBLOCK))
1050 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
1052 if (ktruio != NULL) {
1053 ktruio->uio_resid = len - auio.uio_resid;
1054 ktrgenio(s, UIO_READ, ktruio, error);
1059 td->td_retval[0] = len - auio.uio_resid;
1061 len = mp->msg_namelen;
1062 if (len <= 0 || fromsa == NULL)
1065 /* save sa_len before it is destroyed by MSG_COMPAT */
1066 len = MIN(len, fromsa->sa_len);
1067 #ifdef COMPAT_OLDSOCK
1068 if (mp->msg_flags & MSG_COMPAT)
1069 ((struct osockaddr *)fromsa)->sa_family =
1072 if (fromseg == UIO_USERSPACE) {
1073 error = copyout(fromsa, mp->msg_name,
1078 bcopy(fromsa, mp->msg_name, len);
1080 mp->msg_namelen = len;
1082 if (mp->msg_control && controlp == NULL) {
1083 #ifdef COMPAT_OLDSOCK
1085 * We assume that old recvmsg calls won't receive access
1086 * rights and other control info, esp. as control info
1087 * is always optional and those options didn't exist in 4.3.
1088 * If we receive rights, trim the cmsghdr; anything else
1091 if (control && mp->msg_flags & MSG_COMPAT) {
1092 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1094 mtod(control, struct cmsghdr *)->cmsg_type !=
1096 mp->msg_controllen = 0;
1099 control->m_len -= sizeof (struct cmsghdr);
1100 control->m_data += sizeof (struct cmsghdr);
1103 len = mp->msg_controllen;
1105 mp->msg_controllen = 0;
1106 ctlbuf = mp->msg_control;
1108 while (m && len > 0) {
1109 unsigned int tocopy;
1111 if (len >= m->m_len)
1114 mp->msg_flags |= MSG_CTRUNC;
1118 if ((error = copyout(mtod(m, caddr_t),
1119 ctlbuf, tocopy)) != 0)
1126 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1131 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1132 ktrsockaddr(fromsa);
1134 free(fromsa, M_SONAME);
1136 if (error == 0 && controlp != NULL)
1137 *controlp = control;
1145 recvit(td, s, mp, namelenp)
1153 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1156 if (namelenp != NULL) {
1157 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1158 #ifdef COMPAT_OLDSOCK
1159 if (mp->msg_flags & MSG_COMPAT)
1160 error = 0; /* old recvfrom didn't check */
1167 sys_recvfrom(td, uap)
1169 struct recvfrom_args /* {
1174 struct sockaddr * __restrict from;
1175 socklen_t * __restrict fromlenaddr;
1182 if (uap->fromlenaddr) {
1183 error = copyin(uap->fromlenaddr,
1184 &msg.msg_namelen, sizeof (msg.msg_namelen));
1188 msg.msg_namelen = 0;
1190 msg.msg_name = uap->from;
1191 msg.msg_iov = &aiov;
1193 aiov.iov_base = uap->buf;
1194 aiov.iov_len = uap->len;
1195 msg.msg_control = 0;
1196 msg.msg_flags = uap->flags;
1197 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1202 #ifdef COMPAT_OLDSOCK
1206 struct recvfrom_args *uap;
1209 uap->flags |= MSG_COMPAT;
1210 return (sys_recvfrom(td, uap));
1214 #ifdef COMPAT_OLDSOCK
1218 struct orecv_args /* {
1229 msg.msg_namelen = 0;
1230 msg.msg_iov = &aiov;
1232 aiov.iov_base = uap->buf;
1233 aiov.iov_len = uap->len;
1234 msg.msg_control = 0;
1235 msg.msg_flags = uap->flags;
1236 return (recvit(td, uap->s, &msg, NULL));
1240 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1241 * overlays the new one, missing only the flags, and with the (old) access
1242 * rights where the control fields are now.
1247 struct orecvmsg_args /* {
1249 struct omsghdr *msg;
1257 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1260 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1263 msg.msg_flags = uap->flags | MSG_COMPAT;
1265 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1266 if (msg.msg_controllen && error == 0)
1267 error = copyout(&msg.msg_controllen,
1268 &uap->msg->msg_accrightslen, sizeof (int));
1275 sys_recvmsg(td, uap)
1277 struct recvmsg_args /* {
1284 struct iovec *uiov, *iov;
1287 error = copyin(uap->msg, &msg, sizeof (msg));
1290 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1293 msg.msg_flags = uap->flags;
1294 #ifdef COMPAT_OLDSOCK
1295 msg.msg_flags &= ~MSG_COMPAT;
1299 error = recvit(td, uap->s, &msg, NULL);
1302 error = copyout(&msg, uap->msg, sizeof(msg));
1310 sys_shutdown(td, uap)
1312 struct shutdown_args /* {
1319 cap_rights_t rights;
1322 AUDIT_ARG_FD(uap->s);
1323 error = getsock_cap(td, uap->s, cap_rights_init(&rights, CAP_SHUTDOWN),
1327 error = soshutdown(so, uap->how);
1329 * Previous versions did not return ENOTCONN, but 0 in
1330 * case the socket was not connected. Some important
1331 * programs like syslogd up to r279016, 2015-02-19,
1332 * still depend on this behavior.
1334 if (error == ENOTCONN &&
1335 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1344 sys_setsockopt(td, uap)
1346 struct setsockopt_args /* {
1355 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1356 uap->val, UIO_USERSPACE, uap->valsize));
1360 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1366 enum uio_seg valseg;
1371 struct sockopt sopt;
1372 cap_rights_t rights;
1375 if (val == NULL && valsize != 0)
1377 if ((int)valsize < 0)
1380 sopt.sopt_dir = SOPT_SET;
1381 sopt.sopt_level = level;
1382 sopt.sopt_name = name;
1383 sopt.sopt_val = val;
1384 sopt.sopt_valsize = valsize;
1390 sopt.sopt_td = NULL;
1393 panic("kern_setsockopt called with bad valseg");
1397 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SETSOCKOPT),
1401 error = sosetopt(so, &sopt);
1409 sys_getsockopt(td, uap)
1411 struct getsockopt_args /* {
1415 void * __restrict val;
1416 socklen_t * __restrict avalsize;
1423 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1428 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1429 uap->val, UIO_USERSPACE, &valsize);
1432 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1437 * Kernel version of getsockopt.
1438 * optval can be a userland or userspace. optlen is always a kernel pointer.
1441 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1447 enum uio_seg valseg;
1452 struct sockopt sopt;
1453 cap_rights_t rights;
1458 if ((int)*valsize < 0)
1461 sopt.sopt_dir = SOPT_GET;
1462 sopt.sopt_level = level;
1463 sopt.sopt_name = name;
1464 sopt.sopt_val = val;
1465 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1471 sopt.sopt_td = NULL;
1474 panic("kern_getsockopt called with bad valseg");
1478 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_GETSOCKOPT),
1482 error = sogetopt(so, &sopt);
1483 *valsize = sopt.sopt_valsize;
1490 * getsockname1() - Get socket name.
1494 getsockname1(td, uap, compat)
1496 struct getsockname_args /* {
1498 struct sockaddr * __restrict asa;
1499 socklen_t * __restrict alen;
1503 struct sockaddr *sa;
1507 error = copyin(uap->alen, &len, sizeof(len));
1511 error = kern_getsockname(td, uap->fdes, &sa, &len);
1516 #ifdef COMPAT_OLDSOCK
1518 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1520 error = copyout(sa, uap->asa, (u_int)len);
1524 error = copyout(&len, uap->alen, sizeof(len));
1529 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1534 cap_rights_t rights;
1539 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETSOCKNAME),
1545 CURVNET_SET(so->so_vnet);
1546 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1553 len = MIN(*alen, (*sa)->sa_len);
1556 if (KTRPOINT(td, KTR_STRUCT))
1561 if (error != 0 && *sa != NULL) {
1562 free(*sa, M_SONAME);
1569 sys_getsockname(td, uap)
1571 struct getsockname_args *uap;
1574 return (getsockname1(td, uap, 0));
1577 #ifdef COMPAT_OLDSOCK
1579 ogetsockname(td, uap)
1581 struct getsockname_args *uap;
1584 return (getsockname1(td, uap, 1));
1586 #endif /* COMPAT_OLDSOCK */
1589 * getpeername1() - Get name of peer for connected socket.
1593 getpeername1(td, uap, compat)
1595 struct getpeername_args /* {
1597 struct sockaddr * __restrict asa;
1598 socklen_t * __restrict alen;
1602 struct sockaddr *sa;
1606 error = copyin(uap->alen, &len, sizeof (len));
1610 error = kern_getpeername(td, uap->fdes, &sa, &len);
1615 #ifdef COMPAT_OLDSOCK
1617 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1619 error = copyout(sa, uap->asa, (u_int)len);
1623 error = copyout(&len, uap->alen, sizeof(len));
1628 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1633 cap_rights_t rights;
1638 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETPEERNAME),
1643 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1648 CURVNET_SET(so->so_vnet);
1649 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1656 len = MIN(*alen, (*sa)->sa_len);
1659 if (KTRPOINT(td, KTR_STRUCT))
1663 if (error != 0 && *sa != NULL) {
1664 free(*sa, M_SONAME);
1673 sys_getpeername(td, uap)
1675 struct getpeername_args *uap;
1678 return (getpeername1(td, uap, 0));
1681 #ifdef COMPAT_OLDSOCK
1683 ogetpeername(td, uap)
1685 struct ogetpeername_args *uap;
1688 /* XXX uap should have type `getpeername_args *' to begin with. */
1689 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1691 #endif /* COMPAT_OLDSOCK */
1694 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1696 struct sockaddr *sa;
1700 if (buflen > MLEN) {
1701 #ifdef COMPAT_OLDSOCK
1702 if (type == MT_SONAME && buflen <= 112)
1703 buflen = MLEN; /* unix domain compat. hack */
1706 if (buflen > MCLBYTES)
1709 m = m_get2(buflen, M_WAITOK, type, 0);
1711 error = copyin(buf, mtod(m, void *), buflen);
1716 if (type == MT_SONAME) {
1717 sa = mtod(m, struct sockaddr *);
1719 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1720 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1721 sa->sa_family = sa->sa_len;
1723 sa->sa_len = buflen;
1730 getsockaddr(namp, uaddr, len)
1731 struct sockaddr **namp;
1735 struct sockaddr *sa;
1738 if (len > SOCK_MAXADDRLEN)
1739 return (ENAMETOOLONG);
1740 if (len < offsetof(struct sockaddr, sa_data[0]))
1742 sa = malloc(len, M_SONAME, M_WAITOK);
1743 error = copyin(uaddr, sa, len);
1747 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1748 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1749 sa->sa_family = sa->sa_len;