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 * If required copy of current set of capability rights is returned.
93 * A reference on the file entry is held upon returning.
96 getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
97 struct file **fpp, u_int *fflagp, struct filecaps *havecapsp)
102 error = fget_cap(td, fd, rightsp, &fp, havecapsp);
105 if (fp->f_type != DTYPE_SOCKET) {
107 if (havecapsp != NULL)
108 filecaps_free(havecapsp);
112 *fflagp = fp->f_flag;
118 * System call interface to the socket abstraction.
120 #if defined(COMPAT_43)
121 #define COMPAT_OLDSOCK
125 sys_socket(struct thread *td, struct socket_args *uap)
129 int fd, error, type, oflag, fflag;
131 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
136 if ((type & SOCK_CLOEXEC) != 0) {
137 type &= ~SOCK_CLOEXEC;
140 if ((type & SOCK_NONBLOCK) != 0) {
141 type &= ~SOCK_NONBLOCK;
146 error = mac_socket_check_create(td->td_ucred, uap->domain, type,
151 error = falloc(td, &fp, &fd, oflag);
154 /* An extra reference on `fp' has been held for us by falloc(). */
155 error = socreate(uap->domain, &so, type, uap->protocol,
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)
239 AUDIT_ARG_FD(uap->s);
240 error = getsock_cap(td, uap->s, cap_rights_init(&rights, CAP_LISTEN),
245 error = mac_socket_check_listen(td->td_ucred, so);
248 error = solisten(so, uap->backlog, td);
258 accept1(td, s, uname, anamelen, flags)
261 struct sockaddr *uname;
265 struct sockaddr *name;
271 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
273 error = copyin(anamelen, &namelen, sizeof (namelen));
277 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
282 if (error == 0 && uname != NULL) {
283 #ifdef COMPAT_OLDSOCK
284 if (flags & ACCEPT4_COMPAT)
285 ((struct osockaddr *)name)->sa_family =
288 error = copyout(name, uname, namelen);
291 error = copyout(&namelen, anamelen,
294 fdclose(td, fp, td->td_retval[0]);
296 free(name, M_SONAME);
301 kern_accept(struct thread *td, int s, struct sockaddr **name,
302 socklen_t *namelen, struct file **fp)
304 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
308 kern_accept4(struct thread *td, int s, struct sockaddr **name,
309 socklen_t *namelen, int flags, struct file **fp)
311 struct file *headfp, *nfp = NULL;
312 struct sockaddr *sa = NULL;
313 struct socket *head, *so;
314 struct filecaps fcaps;
324 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_ACCEPT),
325 &headfp, &fflag, &fcaps);
328 head = headfp->f_data;
329 if ((head->so_options & SO_ACCEPTCONN) == 0) {
334 error = mac_socket_check_accept(td->td_ucred, head);
338 error = falloc_caps(td, &nfp, &fd,
339 (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0, &fcaps);
343 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
348 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
349 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
350 head->so_error = ECONNABORTED;
353 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
360 if (head->so_error) {
361 error = head->so_error;
366 so = TAILQ_FIRST(&head->so_comp);
367 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
368 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
371 * Before changing the flags on the socket, we have to bump the
372 * reference count. Otherwise, if the protocol calls sofree(),
373 * the socket will be released due to a zero refcount.
375 SOCK_LOCK(so); /* soref() and so_state update */
376 soref(so); /* file descriptor reference */
378 TAILQ_REMOVE(&head->so_comp, so, so_list);
380 if (flags & ACCEPT4_INHERIT)
381 so->so_state |= (head->so_state & SS_NBIO);
383 so->so_state |= (flags & SOCK_NONBLOCK) ? SS_NBIO : 0;
384 so->so_qstate &= ~SQ_COMP;
390 /* An extra reference on `nfp' has been held for us by falloc(). */
391 td->td_retval[0] = fd;
393 /* connection has been removed from the listen queue */
394 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
396 if (flags & ACCEPT4_INHERIT) {
397 pgid = fgetown(&head->so_sigio);
399 fsetown(pgid, &so->so_sigio);
401 fflag &= ~(FNONBLOCK | FASYNC);
402 if (flags & SOCK_NONBLOCK)
406 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
407 /* Sync socket nonblocking/async state with file flags */
408 tmp = fflag & FNONBLOCK;
409 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
410 tmp = fflag & FASYNC;
411 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
413 error = soaccept(so, &sa);
421 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
423 /* check sa_len before it is destroyed */
424 if (*namelen > sa->sa_len)
425 *namelen = sa->sa_len;
427 if (KTRPOINT(td, KTR_STRUCT))
437 * close the new descriptor, assuming someone hasn't ripped it
441 fdclose(td, nfp, fd);
444 * Release explicitly held references before returning. We return
445 * a reference on nfp to the caller on success if they request it.
449 filecaps_free(&fcaps);
466 struct accept_args *uap;
469 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
475 struct accept4_args *uap;
478 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
481 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
484 #ifdef COMPAT_OLDSOCK
488 struct accept_args *uap;
491 return (accept1(td, uap->s, uap->name, uap->anamelen,
492 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
494 #endif /* COMPAT_OLDSOCK */
497 sys_connect(struct thread *td, struct connect_args *uap)
502 error = getsockaddr(&sa, uap->name, uap->namelen);
504 error = kern_connectat(td, AT_FDCWD, uap->s, sa);
511 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
516 int error, interrupted = 0;
519 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
520 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_CONNECT),
525 if (so->so_state & SS_ISCONNECTING) {
530 if (KTRPOINT(td, KTR_STRUCT))
534 error = mac_socket_check_connect(td->td_ucred, so, sa);
538 if (dirfd == AT_FDCWD)
539 error = soconnect(so, sa, td);
541 error = soconnectat(dirfd, so, sa, td);
544 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
549 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
550 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
553 if (error == EINTR || error == ERESTART)
559 error = so->so_error;
565 so->so_state &= ~SS_ISCONNECTING;
566 if (error == ERESTART)
574 sys_connectat(struct thread *td, struct connectat_args *uap)
579 error = getsockaddr(&sa, uap->name, uap->namelen);
581 error = kern_connectat(td, uap->fd, uap->s, sa);
588 kern_socketpair(struct thread *td, int domain, int type, int protocol,
591 struct file *fp1, *fp2;
592 struct socket *so1, *so2;
593 int fd, error, oflag, fflag;
595 AUDIT_ARG_SOCKET(domain, type, protocol);
599 if ((type & SOCK_CLOEXEC) != 0) {
600 type &= ~SOCK_CLOEXEC;
603 if ((type & SOCK_NONBLOCK) != 0) {
604 type &= ~SOCK_NONBLOCK;
608 /* We might want to have a separate check for socket pairs. */
609 error = mac_socket_check_create(td->td_ucred, domain, type,
614 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
617 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
620 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
621 error = falloc(td, &fp1, &fd, oflag);
625 fp1->f_data = so1; /* so1 already has ref count */
626 error = falloc(td, &fp2, &fd, oflag);
629 fp2->f_data = so2; /* so2 already has ref count */
631 error = soconnect2(so1, so2);
634 if (type == SOCK_DGRAM) {
636 * Datagram socket connection is asymmetric.
638 error = soconnect2(so2, so1);
642 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
644 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
646 if ((fflag & FNONBLOCK) != 0) {
647 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
648 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
654 fdclose(td, fp2, rsv[1]);
657 fdclose(td, fp1, rsv[0]);
669 sys_socketpair(struct thread *td, struct socketpair_args *uap)
673 error = kern_socketpair(td, uap->domain, uap->type,
677 error = copyout(sv, uap->rsv, 2 * sizeof(int));
679 (void)kern_close(td, sv[0]);
680 (void)kern_close(td, sv[1]);
686 sendit(struct thread *td, int s, struct msghdr *mp, int flags)
688 struct mbuf *control;
692 #ifdef CAPABILITY_MODE
693 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
697 if (mp->msg_name != NULL) {
698 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
708 if (mp->msg_control) {
709 if (mp->msg_controllen < sizeof(struct cmsghdr)
710 #ifdef COMPAT_OLDSOCK
711 && mp->msg_flags != MSG_COMPAT
717 error = sockargs(&control, mp->msg_control,
718 mp->msg_controllen, MT_CONTROL);
721 #ifdef COMPAT_OLDSOCK
722 if (mp->msg_flags == MSG_COMPAT) {
725 M_PREPEND(control, sizeof(*cm), M_WAITOK);
726 cm = mtod(control, struct cmsghdr *);
727 cm->cmsg_len = control->m_len;
728 cm->cmsg_level = SOL_SOCKET;
729 cm->cmsg_type = SCM_RIGHTS;
736 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
744 kern_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
745 struct mbuf *control, enum uio_seg segflg)
753 struct uio *ktruio = NULL;
759 cap_rights_init(&rights, CAP_SEND);
760 if (mp->msg_name != NULL) {
761 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
762 cap_rights_set(&rights, CAP_CONNECT);
764 error = getsock_cap(td, s, &rights, &fp, NULL, NULL);
769 so = (struct socket *)fp->f_data;
772 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
773 ktrsockaddr(mp->msg_name);
776 if (mp->msg_name != NULL) {
777 error = mac_socket_check_connect(td->td_ucred, so,
784 error = mac_socket_check_send(td->td_ucred, so);
791 auio.uio_iov = mp->msg_iov;
792 auio.uio_iovcnt = mp->msg_iovlen;
793 auio.uio_segflg = segflg;
794 auio.uio_rw = UIO_WRITE;
796 auio.uio_offset = 0; /* XXX */
799 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
800 if ((auio.uio_resid += iov->iov_len) < 0) {
807 if (KTRPOINT(td, KTR_GENIO))
808 ktruio = cloneuio(&auio);
810 len = auio.uio_resid;
811 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
813 if (auio.uio_resid != len && (error == ERESTART ||
814 error == EINTR || error == EWOULDBLOCK))
816 /* Generation of SIGPIPE can be controlled per socket */
817 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
818 !(flags & MSG_NOSIGNAL)) {
819 PROC_LOCK(td->td_proc);
820 tdsignal(td, SIGPIPE);
821 PROC_UNLOCK(td->td_proc);
825 td->td_retval[0] = len - auio.uio_resid;
827 if (ktruio != NULL) {
828 ktruio->uio_resid = td->td_retval[0];
829 ktrgenio(s, UIO_WRITE, ktruio, error);
838 sys_sendto(struct thread *td, struct sendto_args *uap)
843 msg.msg_name = uap->to;
844 msg.msg_namelen = uap->tolen;
848 #ifdef COMPAT_OLDSOCK
851 aiov.iov_base = uap->buf;
852 aiov.iov_len = uap->len;
853 return (sendit(td, uap->s, &msg, uap->flags));
856 #ifdef COMPAT_OLDSOCK
858 osend(struct thread *td, struct osend_args *uap)
867 aiov.iov_base = uap->buf;
868 aiov.iov_len = uap->len;
871 return (sendit(td, uap->s, &msg, uap->flags));
875 osendmsg(struct thread *td, struct osendmsg_args *uap)
881 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
884 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
888 msg.msg_flags = MSG_COMPAT;
889 error = sendit(td, uap->s, &msg, uap->flags);
896 sys_sendmsg(struct thread *td, struct sendmsg_args *uap)
902 error = copyin(uap->msg, &msg, sizeof (msg));
905 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
909 #ifdef COMPAT_OLDSOCK
912 error = sendit(td, uap->s, &msg, uap->flags);
918 kern_recvit(struct thread *td, int s, struct msghdr *mp, enum uio_seg fromseg,
919 struct mbuf **controlp)
923 struct mbuf *m, *control = NULL;
927 struct sockaddr *fromsa = NULL;
930 struct uio *ktruio = NULL;
935 if (controlp != NULL)
939 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_RECV),
946 error = mac_socket_check_receive(td->td_ucred, so);
953 auio.uio_iov = mp->msg_iov;
954 auio.uio_iovcnt = mp->msg_iovlen;
955 auio.uio_segflg = UIO_USERSPACE;
956 auio.uio_rw = UIO_READ;
958 auio.uio_offset = 0; /* XXX */
961 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
962 if ((auio.uio_resid += iov->iov_len) < 0) {
968 if (KTRPOINT(td, KTR_GENIO))
969 ktruio = cloneuio(&auio);
971 len = auio.uio_resid;
972 error = soreceive(so, &fromsa, &auio, NULL,
973 (mp->msg_control || controlp) ? &control : NULL,
976 if (auio.uio_resid != len && (error == ERESTART ||
977 error == EINTR || error == EWOULDBLOCK))
981 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
983 if (ktruio != NULL) {
984 ktruio->uio_resid = len - auio.uio_resid;
985 ktrgenio(s, UIO_READ, ktruio, error);
990 td->td_retval[0] = len - auio.uio_resid;
992 len = mp->msg_namelen;
993 if (len <= 0 || fromsa == NULL)
996 /* save sa_len before it is destroyed by MSG_COMPAT */
997 len = MIN(len, fromsa->sa_len);
998 #ifdef COMPAT_OLDSOCK
999 if (mp->msg_flags & MSG_COMPAT)
1000 ((struct osockaddr *)fromsa)->sa_family =
1003 if (fromseg == UIO_USERSPACE) {
1004 error = copyout(fromsa, mp->msg_name,
1009 bcopy(fromsa, mp->msg_name, len);
1011 mp->msg_namelen = len;
1013 if (mp->msg_control && controlp == NULL) {
1014 #ifdef COMPAT_OLDSOCK
1016 * We assume that old recvmsg calls won't receive access
1017 * rights and other control info, esp. as control info
1018 * is always optional and those options didn't exist in 4.3.
1019 * If we receive rights, trim the cmsghdr; anything else
1022 if (control && mp->msg_flags & MSG_COMPAT) {
1023 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1025 mtod(control, struct cmsghdr *)->cmsg_type !=
1027 mp->msg_controllen = 0;
1030 control->m_len -= sizeof (struct cmsghdr);
1031 control->m_data += sizeof (struct cmsghdr);
1034 len = mp->msg_controllen;
1036 mp->msg_controllen = 0;
1037 ctlbuf = mp->msg_control;
1039 while (m && len > 0) {
1040 unsigned int tocopy;
1042 if (len >= m->m_len)
1045 mp->msg_flags |= MSG_CTRUNC;
1049 if ((error = copyout(mtod(m, caddr_t),
1050 ctlbuf, tocopy)) != 0)
1057 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1062 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1063 ktrsockaddr(fromsa);
1065 free(fromsa, M_SONAME);
1067 if (error == 0 && controlp != NULL)
1068 *controlp = control;
1076 recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp)
1080 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1083 if (namelenp != NULL) {
1084 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1085 #ifdef COMPAT_OLDSOCK
1086 if (mp->msg_flags & MSG_COMPAT)
1087 error = 0; /* old recvfrom didn't check */
1094 sys_recvfrom(struct thread *td, struct recvfrom_args *uap)
1100 if (uap->fromlenaddr) {
1101 error = copyin(uap->fromlenaddr,
1102 &msg.msg_namelen, sizeof (msg.msg_namelen));
1106 msg.msg_namelen = 0;
1108 msg.msg_name = uap->from;
1109 msg.msg_iov = &aiov;
1111 aiov.iov_base = uap->buf;
1112 aiov.iov_len = uap->len;
1113 msg.msg_control = 0;
1114 msg.msg_flags = uap->flags;
1115 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1120 #ifdef COMPAT_OLDSOCK
1122 orecvfrom(struct thread *td, struct recvfrom_args *uap)
1125 uap->flags |= MSG_COMPAT;
1126 return (sys_recvfrom(td, uap));
1130 #ifdef COMPAT_OLDSOCK
1132 orecv(struct thread *td, struct orecv_args *uap)
1138 msg.msg_namelen = 0;
1139 msg.msg_iov = &aiov;
1141 aiov.iov_base = uap->buf;
1142 aiov.iov_len = uap->len;
1143 msg.msg_control = 0;
1144 msg.msg_flags = uap->flags;
1145 return (recvit(td, uap->s, &msg, NULL));
1149 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1150 * overlays the new one, missing only the flags, and with the (old) access
1151 * rights where the control fields are now.
1154 orecvmsg(struct thread *td, struct orecvmsg_args *uap)
1160 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1163 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1166 msg.msg_flags = uap->flags | MSG_COMPAT;
1168 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1169 if (msg.msg_controllen && error == 0)
1170 error = copyout(&msg.msg_controllen,
1171 &uap->msg->msg_accrightslen, sizeof (int));
1178 sys_recvmsg(struct thread *td, struct recvmsg_args *uap)
1181 struct iovec *uiov, *iov;
1184 error = copyin(uap->msg, &msg, sizeof (msg));
1187 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1190 msg.msg_flags = uap->flags;
1191 #ifdef COMPAT_OLDSOCK
1192 msg.msg_flags &= ~MSG_COMPAT;
1196 error = recvit(td, uap->s, &msg, NULL);
1199 error = copyout(&msg, uap->msg, sizeof(msg));
1206 sys_shutdown(struct thread *td, struct shutdown_args *uap)
1210 cap_rights_t rights;
1213 AUDIT_ARG_FD(uap->s);
1214 error = getsock_cap(td, uap->s, cap_rights_init(&rights, CAP_SHUTDOWN),
1218 error = soshutdown(so, uap->how);
1220 * Previous versions did not return ENOTCONN, but 0 in
1221 * case the socket was not connected. Some important
1222 * programs like syslogd up to r279016, 2015-02-19,
1223 * still depend on this behavior.
1225 if (error == ENOTCONN &&
1226 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1234 sys_setsockopt(struct thread *td, struct setsockopt_args *uap)
1237 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1238 uap->val, UIO_USERSPACE, uap->valsize));
1242 kern_setsockopt(struct thread *td, int s, int level, int name, void *val,
1243 enum uio_seg valseg, socklen_t valsize)
1247 struct sockopt sopt;
1248 cap_rights_t rights;
1251 if (val == NULL && valsize != 0)
1253 if ((int)valsize < 0)
1256 sopt.sopt_dir = SOPT_SET;
1257 sopt.sopt_level = level;
1258 sopt.sopt_name = name;
1259 sopt.sopt_val = val;
1260 sopt.sopt_valsize = valsize;
1266 sopt.sopt_td = NULL;
1269 panic("kern_setsockopt called with bad valseg");
1273 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SETSOCKOPT),
1277 error = sosetopt(so, &sopt);
1284 sys_getsockopt(struct thread *td, struct getsockopt_args *uap)
1290 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1295 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1296 uap->val, UIO_USERSPACE, &valsize);
1299 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1304 * Kernel version of getsockopt.
1305 * optval can be a userland or userspace. optlen is always a kernel pointer.
1308 kern_getsockopt(struct thread *td, int s, int level, int name, void *val,
1309 enum uio_seg valseg, socklen_t *valsize)
1313 struct sockopt sopt;
1314 cap_rights_t rights;
1319 if ((int)*valsize < 0)
1322 sopt.sopt_dir = SOPT_GET;
1323 sopt.sopt_level = level;
1324 sopt.sopt_name = name;
1325 sopt.sopt_val = val;
1326 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1332 sopt.sopt_td = NULL;
1335 panic("kern_getsockopt called with bad valseg");
1339 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_GETSOCKOPT),
1343 error = sogetopt(so, &sopt);
1344 *valsize = sopt.sopt_valsize;
1351 * getsockname1() - Get socket name.
1354 getsockname1(struct thread *td, struct getsockname_args *uap, int compat)
1356 struct sockaddr *sa;
1360 error = copyin(uap->alen, &len, sizeof(len));
1364 error = kern_getsockname(td, uap->fdes, &sa, &len);
1369 #ifdef COMPAT_OLDSOCK
1371 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1373 error = copyout(sa, uap->asa, (u_int)len);
1377 error = copyout(&len, uap->alen, sizeof(len));
1382 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1387 cap_rights_t rights;
1392 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETSOCKNAME),
1398 CURVNET_SET(so->so_vnet);
1399 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1406 len = MIN(*alen, (*sa)->sa_len);
1409 if (KTRPOINT(td, KTR_STRUCT))
1414 if (error != 0 && *sa != NULL) {
1415 free(*sa, M_SONAME);
1422 sys_getsockname(struct thread *td, struct getsockname_args *uap)
1425 return (getsockname1(td, uap, 0));
1428 #ifdef COMPAT_OLDSOCK
1430 ogetsockname(struct thread *td, struct getsockname_args *uap)
1433 return (getsockname1(td, uap, 1));
1435 #endif /* COMPAT_OLDSOCK */
1438 * getpeername1() - Get name of peer for connected socket.
1441 getpeername1(struct thread *td, struct getpeername_args *uap, int compat)
1443 struct sockaddr *sa;
1447 error = copyin(uap->alen, &len, sizeof (len));
1451 error = kern_getpeername(td, uap->fdes, &sa, &len);
1456 #ifdef COMPAT_OLDSOCK
1458 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1460 error = copyout(sa, uap->asa, (u_int)len);
1464 error = copyout(&len, uap->alen, sizeof(len));
1469 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1474 cap_rights_t rights;
1479 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETPEERNAME),
1484 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1489 CURVNET_SET(so->so_vnet);
1490 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1497 len = MIN(*alen, (*sa)->sa_len);
1500 if (KTRPOINT(td, KTR_STRUCT))
1504 if (error != 0 && *sa != NULL) {
1505 free(*sa, M_SONAME);
1514 sys_getpeername(struct thread *td, struct getpeername_args *uap)
1517 return (getpeername1(td, uap, 0));
1520 #ifdef COMPAT_OLDSOCK
1522 ogetpeername(struct thread *td, struct ogetpeername_args *uap)
1525 /* XXX uap should have type `getpeername_args *' to begin with. */
1526 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1528 #endif /* COMPAT_OLDSOCK */
1531 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1533 struct sockaddr *sa;
1537 if (buflen > MLEN) {
1538 #ifdef COMPAT_OLDSOCK
1539 if (type == MT_SONAME && buflen <= 112)
1540 buflen = MLEN; /* unix domain compat. hack */
1543 if (buflen > MCLBYTES)
1546 m = m_get2(buflen, M_WAITOK, type, 0);
1548 error = copyin(buf, mtod(m, void *), buflen);
1553 if (type == MT_SONAME) {
1554 sa = mtod(m, struct sockaddr *);
1556 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1557 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1558 sa->sa_family = sa->sa_len;
1560 sa->sa_len = buflen;
1567 getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
1569 struct sockaddr *sa;
1572 if (len > SOCK_MAXADDRLEN)
1573 return (ENAMETOOLONG);
1574 if (len < offsetof(struct sockaddr, sa_data[0]))
1576 sa = malloc(len, M_SONAME, M_WAITOK);
1577 error = copyin(uaddr, sa, len);
1581 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1582 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1583 sa->sa_family = sa->sa_len;