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>
71 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
72 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
74 static int accept1(struct thread *td, int s, struct sockaddr *uname,
75 socklen_t *anamelen, int flags);
76 static int getsockname1(struct thread *td, struct getsockname_args *uap,
78 static int getpeername1(struct thread *td, struct getpeername_args *uap,
80 static int sockargs(struct mbuf **, char *, socklen_t, int);
83 * Convert a user file descriptor to a kernel file entry and check if required
84 * capability rights are present.
85 * If required copy of current set of capability rights is returned.
86 * A reference on the file entry is held upon returning.
89 getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
90 struct file **fpp, u_int *fflagp, struct filecaps *havecapsp)
95 error = fget_cap(td, fd, rightsp, &fp, havecapsp);
98 if (fp->f_type != DTYPE_SOCKET) {
100 if (havecapsp != NULL)
101 filecaps_free(havecapsp);
105 *fflagp = fp->f_flag;
111 * System call interface to the socket abstraction.
113 #if defined(COMPAT_43)
114 #define COMPAT_OLDSOCK
118 sys_socket(struct thread *td, struct socket_args *uap)
121 return (kern_socket(td, uap->domain, uap->type, uap->protocol));
125 kern_socket(struct thread *td, int domain, int type, int protocol)
129 int fd, error, oflag, fflag;
131 AUDIT_ARG_SOCKET(domain, type, protocol);
135 if ((type & SOCK_CLOEXEC) != 0) {
136 type &= ~SOCK_CLOEXEC;
139 if ((type & SOCK_NONBLOCK) != 0) {
140 type &= ~SOCK_NONBLOCK;
145 error = mac_socket_check_create(td->td_ucred, domain, type, protocol);
149 error = falloc(td, &fp, &fd, oflag);
152 /* An extra reference on `fp' has been held for us by falloc(). */
153 error = socreate(domain, &so, type, protocol, td->td_ucred, td);
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)
232 return (kern_listen(td, uap->s, uap->backlog));
236 kern_listen(struct thread *td, int s, int backlog)
244 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_LISTEN),
249 error = mac_socket_check_listen(td->td_ucred, so);
252 error = solisten(so, backlog, td);
262 accept1(td, s, uname, anamelen, flags)
265 struct sockaddr *uname;
269 struct sockaddr *name;
275 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
277 error = copyin(anamelen, &namelen, sizeof (namelen));
281 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
286 if (error == 0 && uname != NULL) {
287 #ifdef COMPAT_OLDSOCK
288 if (flags & ACCEPT4_COMPAT)
289 ((struct osockaddr *)name)->sa_family =
292 error = copyout(name, uname, namelen);
295 error = copyout(&namelen, anamelen,
298 fdclose(td, fp, td->td_retval[0]);
300 free(name, M_SONAME);
305 kern_accept(struct thread *td, int s, struct sockaddr **name,
306 socklen_t *namelen, struct file **fp)
308 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
312 kern_accept4(struct thread *td, int s, struct sockaddr **name,
313 socklen_t *namelen, int flags, struct file **fp)
315 struct file *headfp, *nfp = NULL;
316 struct sockaddr *sa = NULL;
317 struct socket *head, *so;
318 struct filecaps fcaps;
328 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_ACCEPT),
329 &headfp, &fflag, &fcaps);
332 head = headfp->f_data;
333 if ((head->so_options & SO_ACCEPTCONN) == 0) {
338 error = mac_socket_check_accept(td->td_ucred, head);
342 error = falloc_caps(td, &nfp, &fd,
343 (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0, &fcaps);
347 if (!SOLISTENING(head)) {
353 error = solisten_dequeue(head, &so, flags);
357 /* An extra reference on `nfp' has been held for us by falloc(). */
358 td->td_retval[0] = fd;
360 /* Connection has been removed from the listen queue. */
361 KNOTE_UNLOCKED(&head->so_rdsel.si_note, 0);
363 if (flags & ACCEPT4_INHERIT) {
364 pgid = fgetown(&head->so_sigio);
366 fsetown(pgid, &so->so_sigio);
368 fflag &= ~(FNONBLOCK | FASYNC);
369 if (flags & SOCK_NONBLOCK)
373 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
374 /* Sync socket nonblocking/async state with file flags */
375 tmp = fflag & FNONBLOCK;
376 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
377 tmp = fflag & FASYNC;
378 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
379 error = soaccept(so, &sa);
387 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
389 /* check sa_len before it is destroyed */
390 if (*namelen > sa->sa_len)
391 *namelen = sa->sa_len;
393 if (KTRPOINT(td, KTR_STRUCT))
403 * close the new descriptor, assuming someone hasn't ripped it
407 fdclose(td, nfp, fd);
410 * Release explicitly held references before returning. We return
411 * a reference on nfp to the caller on success if they request it.
415 filecaps_free(&fcaps);
432 struct accept_args *uap;
435 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
441 struct accept4_args *uap;
444 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
447 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
450 #ifdef COMPAT_OLDSOCK
454 struct accept_args *uap;
457 return (accept1(td, uap->s, uap->name, uap->anamelen,
458 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
460 #endif /* COMPAT_OLDSOCK */
463 sys_connect(struct thread *td, struct connect_args *uap)
468 error = getsockaddr(&sa, uap->name, uap->namelen);
470 error = kern_connectat(td, AT_FDCWD, uap->s, sa);
477 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
482 int error, interrupted = 0;
485 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
486 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_CONNECT),
491 if (so->so_state & SS_ISCONNECTING) {
496 if (KTRPOINT(td, KTR_STRUCT))
500 error = mac_socket_check_connect(td->td_ucred, so, sa);
504 if (dirfd == AT_FDCWD)
505 error = soconnect(so, sa, td);
507 error = soconnectat(dirfd, so, sa, td);
510 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
515 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
516 error = msleep(&so->so_timeo, &so->so_lock, PSOCK | PCATCH,
519 if (error == EINTR || error == ERESTART)
525 error = so->so_error;
531 so->so_state &= ~SS_ISCONNECTING;
532 if (error == ERESTART)
540 sys_connectat(struct thread *td, struct connectat_args *uap)
545 error = getsockaddr(&sa, uap->name, uap->namelen);
547 error = kern_connectat(td, uap->fd, uap->s, sa);
554 kern_socketpair(struct thread *td, int domain, int type, int protocol,
557 struct file *fp1, *fp2;
558 struct socket *so1, *so2;
559 int fd, error, oflag, fflag;
561 AUDIT_ARG_SOCKET(domain, type, protocol);
565 if ((type & SOCK_CLOEXEC) != 0) {
566 type &= ~SOCK_CLOEXEC;
569 if ((type & SOCK_NONBLOCK) != 0) {
570 type &= ~SOCK_NONBLOCK;
574 /* We might want to have a separate check for socket pairs. */
575 error = mac_socket_check_create(td->td_ucred, domain, type,
580 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
583 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
586 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
587 error = falloc(td, &fp1, &fd, oflag);
591 fp1->f_data = so1; /* so1 already has ref count */
592 error = falloc(td, &fp2, &fd, oflag);
595 fp2->f_data = so2; /* so2 already has ref count */
597 error = soconnect2(so1, so2);
600 if (type == SOCK_DGRAM) {
602 * Datagram socket connection is asymmetric.
604 error = soconnect2(so2, so1);
608 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
610 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
612 if ((fflag & FNONBLOCK) != 0) {
613 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
614 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
620 fdclose(td, fp2, rsv[1]);
623 fdclose(td, fp1, rsv[0]);
635 sys_socketpair(struct thread *td, struct socketpair_args *uap)
639 error = kern_socketpair(td, uap->domain, uap->type,
643 error = copyout(sv, uap->rsv, 2 * sizeof(int));
645 (void)kern_close(td, sv[0]);
646 (void)kern_close(td, sv[1]);
652 sendit(struct thread *td, int s, struct msghdr *mp, int flags)
654 struct mbuf *control;
658 #ifdef CAPABILITY_MODE
659 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
663 if (mp->msg_name != NULL) {
664 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
674 if (mp->msg_control) {
675 if (mp->msg_controllen < sizeof(struct cmsghdr)
676 #ifdef COMPAT_OLDSOCK
677 && mp->msg_flags != MSG_COMPAT
683 error = sockargs(&control, mp->msg_control,
684 mp->msg_controllen, MT_CONTROL);
687 #ifdef COMPAT_OLDSOCK
688 if (mp->msg_flags == MSG_COMPAT) {
691 M_PREPEND(control, sizeof(*cm), M_WAITOK);
692 cm = mtod(control, struct cmsghdr *);
693 cm->cmsg_len = control->m_len;
694 cm->cmsg_level = SOL_SOCKET;
695 cm->cmsg_type = SCM_RIGHTS;
702 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
710 kern_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
711 struct mbuf *control, enum uio_seg segflg)
719 struct uio *ktruio = NULL;
725 cap_rights_init(&rights, CAP_SEND);
726 if (mp->msg_name != NULL) {
727 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
728 cap_rights_set(&rights, CAP_CONNECT);
730 error = getsock_cap(td, s, &rights, &fp, NULL, NULL);
735 so = (struct socket *)fp->f_data;
738 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
739 ktrsockaddr(mp->msg_name);
742 if (mp->msg_name != NULL) {
743 error = mac_socket_check_connect(td->td_ucred, so,
750 error = mac_socket_check_send(td->td_ucred, so);
757 auio.uio_iov = mp->msg_iov;
758 auio.uio_iovcnt = mp->msg_iovlen;
759 auio.uio_segflg = segflg;
760 auio.uio_rw = UIO_WRITE;
762 auio.uio_offset = 0; /* XXX */
765 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
766 if ((auio.uio_resid += iov->iov_len) < 0) {
773 if (KTRPOINT(td, KTR_GENIO))
774 ktruio = cloneuio(&auio);
776 len = auio.uio_resid;
777 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
779 if (auio.uio_resid != len && (error == ERESTART ||
780 error == EINTR || error == EWOULDBLOCK))
782 /* Generation of SIGPIPE can be controlled per socket */
783 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
784 !(flags & MSG_NOSIGNAL)) {
785 PROC_LOCK(td->td_proc);
786 tdsignal(td, SIGPIPE);
787 PROC_UNLOCK(td->td_proc);
791 td->td_retval[0] = len - auio.uio_resid;
793 if (ktruio != NULL) {
794 ktruio->uio_resid = td->td_retval[0];
795 ktrgenio(s, UIO_WRITE, ktruio, error);
804 sys_sendto(struct thread *td, struct sendto_args *uap)
809 msg.msg_name = uap->to;
810 msg.msg_namelen = uap->tolen;
814 #ifdef COMPAT_OLDSOCK
817 aiov.iov_base = uap->buf;
818 aiov.iov_len = uap->len;
819 return (sendit(td, uap->s, &msg, uap->flags));
822 #ifdef COMPAT_OLDSOCK
824 osend(struct thread *td, struct osend_args *uap)
833 aiov.iov_base = uap->buf;
834 aiov.iov_len = uap->len;
837 return (sendit(td, uap->s, &msg, uap->flags));
841 osendmsg(struct thread *td, struct osendmsg_args *uap)
847 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
850 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
854 msg.msg_flags = MSG_COMPAT;
855 error = sendit(td, uap->s, &msg, uap->flags);
862 sys_sendmsg(struct thread *td, struct sendmsg_args *uap)
868 error = copyin(uap->msg, &msg, sizeof (msg));
871 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
875 #ifdef COMPAT_OLDSOCK
878 error = sendit(td, uap->s, &msg, uap->flags);
884 kern_recvit(struct thread *td, int s, struct msghdr *mp, enum uio_seg fromseg,
885 struct mbuf **controlp)
889 struct mbuf *m, *control = NULL;
893 struct sockaddr *fromsa = NULL;
896 struct uio *ktruio = NULL;
901 if (controlp != NULL)
905 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_RECV),
912 error = mac_socket_check_receive(td->td_ucred, so);
919 auio.uio_iov = mp->msg_iov;
920 auio.uio_iovcnt = mp->msg_iovlen;
921 auio.uio_segflg = UIO_USERSPACE;
922 auio.uio_rw = UIO_READ;
924 auio.uio_offset = 0; /* XXX */
927 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
928 if ((auio.uio_resid += iov->iov_len) < 0) {
934 if (KTRPOINT(td, KTR_GENIO))
935 ktruio = cloneuio(&auio);
937 len = auio.uio_resid;
938 error = soreceive(so, &fromsa, &auio, NULL,
939 (mp->msg_control || controlp) ? &control : NULL,
942 if (auio.uio_resid != len && (error == ERESTART ||
943 error == EINTR || error == EWOULDBLOCK))
947 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
949 if (ktruio != NULL) {
950 ktruio->uio_resid = len - auio.uio_resid;
951 ktrgenio(s, UIO_READ, ktruio, error);
956 td->td_retval[0] = len - auio.uio_resid;
958 len = mp->msg_namelen;
959 if (len <= 0 || fromsa == NULL)
962 /* save sa_len before it is destroyed by MSG_COMPAT */
963 len = MIN(len, fromsa->sa_len);
964 #ifdef COMPAT_OLDSOCK
965 if (mp->msg_flags & MSG_COMPAT)
966 ((struct osockaddr *)fromsa)->sa_family =
969 if (fromseg == UIO_USERSPACE) {
970 error = copyout(fromsa, mp->msg_name,
975 bcopy(fromsa, mp->msg_name, len);
977 mp->msg_namelen = len;
979 if (mp->msg_control && controlp == NULL) {
980 #ifdef COMPAT_OLDSOCK
982 * We assume that old recvmsg calls won't receive access
983 * rights and other control info, esp. as control info
984 * is always optional and those options didn't exist in 4.3.
985 * If we receive rights, trim the cmsghdr; anything else
988 if (control && mp->msg_flags & MSG_COMPAT) {
989 if (mtod(control, struct cmsghdr *)->cmsg_level !=
991 mtod(control, struct cmsghdr *)->cmsg_type !=
993 mp->msg_controllen = 0;
996 control->m_len -= sizeof (struct cmsghdr);
997 control->m_data += sizeof (struct cmsghdr);
1000 len = mp->msg_controllen;
1002 mp->msg_controllen = 0;
1003 ctlbuf = mp->msg_control;
1005 while (m && len > 0) {
1006 unsigned int tocopy;
1008 if (len >= m->m_len)
1011 mp->msg_flags |= MSG_CTRUNC;
1015 if ((error = copyout(mtod(m, caddr_t),
1016 ctlbuf, tocopy)) != 0)
1023 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1028 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1029 ktrsockaddr(fromsa);
1031 free(fromsa, M_SONAME);
1033 if (error == 0 && controlp != NULL)
1034 *controlp = control;
1042 recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp)
1046 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1049 if (namelenp != NULL) {
1050 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1051 #ifdef COMPAT_OLDSOCK
1052 if (mp->msg_flags & MSG_COMPAT)
1053 error = 0; /* old recvfrom didn't check */
1060 sys_recvfrom(struct thread *td, struct recvfrom_args *uap)
1066 if (uap->fromlenaddr) {
1067 error = copyin(uap->fromlenaddr,
1068 &msg.msg_namelen, sizeof (msg.msg_namelen));
1072 msg.msg_namelen = 0;
1074 msg.msg_name = uap->from;
1075 msg.msg_iov = &aiov;
1077 aiov.iov_base = uap->buf;
1078 aiov.iov_len = uap->len;
1079 msg.msg_control = 0;
1080 msg.msg_flags = uap->flags;
1081 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1086 #ifdef COMPAT_OLDSOCK
1088 orecvfrom(struct thread *td, struct recvfrom_args *uap)
1091 uap->flags |= MSG_COMPAT;
1092 return (sys_recvfrom(td, uap));
1096 #ifdef COMPAT_OLDSOCK
1098 orecv(struct thread *td, struct orecv_args *uap)
1104 msg.msg_namelen = 0;
1105 msg.msg_iov = &aiov;
1107 aiov.iov_base = uap->buf;
1108 aiov.iov_len = uap->len;
1109 msg.msg_control = 0;
1110 msg.msg_flags = uap->flags;
1111 return (recvit(td, uap->s, &msg, NULL));
1115 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1116 * overlays the new one, missing only the flags, and with the (old) access
1117 * rights where the control fields are now.
1120 orecvmsg(struct thread *td, struct orecvmsg_args *uap)
1126 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1129 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1132 msg.msg_flags = uap->flags | MSG_COMPAT;
1134 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1135 if (msg.msg_controllen && error == 0)
1136 error = copyout(&msg.msg_controllen,
1137 &uap->msg->msg_accrightslen, sizeof (int));
1144 sys_recvmsg(struct thread *td, struct recvmsg_args *uap)
1147 struct iovec *uiov, *iov;
1150 error = copyin(uap->msg, &msg, sizeof (msg));
1153 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1156 msg.msg_flags = uap->flags;
1157 #ifdef COMPAT_OLDSOCK
1158 msg.msg_flags &= ~MSG_COMPAT;
1162 error = recvit(td, uap->s, &msg, NULL);
1165 error = copyout(&msg, uap->msg, sizeof(msg));
1172 sys_shutdown(struct thread *td, struct shutdown_args *uap)
1175 return (kern_shutdown(td, uap->s, uap->how));
1179 kern_shutdown(struct thread *td, int s, int how)
1183 cap_rights_t rights;
1187 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SHUTDOWN),
1191 error = soshutdown(so, how);
1193 * Previous versions did not return ENOTCONN, but 0 in
1194 * case the socket was not connected. Some important
1195 * programs like syslogd up to r279016, 2015-02-19,
1196 * still depend on this behavior.
1198 if (error == ENOTCONN &&
1199 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1207 sys_setsockopt(struct thread *td, struct setsockopt_args *uap)
1210 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1211 uap->val, UIO_USERSPACE, uap->valsize));
1215 kern_setsockopt(struct thread *td, int s, int level, int name, void *val,
1216 enum uio_seg valseg, socklen_t valsize)
1220 struct sockopt sopt;
1221 cap_rights_t rights;
1224 if (val == NULL && valsize != 0)
1226 if ((int)valsize < 0)
1229 sopt.sopt_dir = SOPT_SET;
1230 sopt.sopt_level = level;
1231 sopt.sopt_name = name;
1232 sopt.sopt_val = val;
1233 sopt.sopt_valsize = valsize;
1239 sopt.sopt_td = NULL;
1242 panic("kern_setsockopt called with bad valseg");
1246 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SETSOCKOPT),
1250 error = sosetopt(so, &sopt);
1257 sys_getsockopt(struct thread *td, struct getsockopt_args *uap)
1263 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1268 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1269 uap->val, UIO_USERSPACE, &valsize);
1272 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1277 * Kernel version of getsockopt.
1278 * optval can be a userland or userspace. optlen is always a kernel pointer.
1281 kern_getsockopt(struct thread *td, int s, int level, int name, void *val,
1282 enum uio_seg valseg, socklen_t *valsize)
1286 struct sockopt sopt;
1287 cap_rights_t rights;
1292 if ((int)*valsize < 0)
1295 sopt.sopt_dir = SOPT_GET;
1296 sopt.sopt_level = level;
1297 sopt.sopt_name = name;
1298 sopt.sopt_val = val;
1299 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1305 sopt.sopt_td = NULL;
1308 panic("kern_getsockopt called with bad valseg");
1312 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_GETSOCKOPT),
1316 error = sogetopt(so, &sopt);
1317 *valsize = sopt.sopt_valsize;
1324 * getsockname1() - Get socket name.
1327 getsockname1(struct thread *td, struct getsockname_args *uap, int compat)
1329 struct sockaddr *sa;
1333 error = copyin(uap->alen, &len, sizeof(len));
1337 error = kern_getsockname(td, uap->fdes, &sa, &len);
1342 #ifdef COMPAT_OLDSOCK
1344 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1346 error = copyout(sa, uap->asa, (u_int)len);
1350 error = copyout(&len, uap->alen, sizeof(len));
1355 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1360 cap_rights_t rights;
1365 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETSOCKNAME),
1371 CURVNET_SET(so->so_vnet);
1372 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1379 len = MIN(*alen, (*sa)->sa_len);
1382 if (KTRPOINT(td, KTR_STRUCT))
1387 if (error != 0 && *sa != NULL) {
1388 free(*sa, M_SONAME);
1395 sys_getsockname(struct thread *td, struct getsockname_args *uap)
1398 return (getsockname1(td, uap, 0));
1401 #ifdef COMPAT_OLDSOCK
1403 ogetsockname(struct thread *td, struct getsockname_args *uap)
1406 return (getsockname1(td, uap, 1));
1408 #endif /* COMPAT_OLDSOCK */
1411 * getpeername1() - Get name of peer for connected socket.
1414 getpeername1(struct thread *td, struct getpeername_args *uap, int compat)
1416 struct sockaddr *sa;
1420 error = copyin(uap->alen, &len, sizeof (len));
1424 error = kern_getpeername(td, uap->fdes, &sa, &len);
1429 #ifdef COMPAT_OLDSOCK
1431 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1433 error = copyout(sa, uap->asa, (u_int)len);
1437 error = copyout(&len, uap->alen, sizeof(len));
1442 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1447 cap_rights_t rights;
1452 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETPEERNAME),
1457 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1462 CURVNET_SET(so->so_vnet);
1463 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1470 len = MIN(*alen, (*sa)->sa_len);
1473 if (KTRPOINT(td, KTR_STRUCT))
1477 if (error != 0 && *sa != NULL) {
1478 free(*sa, M_SONAME);
1487 sys_getpeername(struct thread *td, struct getpeername_args *uap)
1490 return (getpeername1(td, uap, 0));
1493 #ifdef COMPAT_OLDSOCK
1495 ogetpeername(struct thread *td, struct ogetpeername_args *uap)
1498 /* XXX uap should have type `getpeername_args *' to begin with. */
1499 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1501 #endif /* COMPAT_OLDSOCK */
1504 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1506 struct sockaddr *sa;
1510 if (buflen > MLEN) {
1511 #ifdef COMPAT_OLDSOCK
1512 if (type == MT_SONAME && buflen <= 112)
1513 buflen = MLEN; /* unix domain compat. hack */
1516 if (buflen > MCLBYTES)
1519 m = m_get2(buflen, M_WAITOK, type, 0);
1521 error = copyin(buf, mtod(m, void *), buflen);
1526 if (type == MT_SONAME) {
1527 sa = mtod(m, struct sockaddr *);
1529 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1530 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1531 sa->sa_family = sa->sa_len;
1533 sa->sa_len = buflen;
1540 getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
1542 struct sockaddr *sa;
1545 if (len > SOCK_MAXADDRLEN)
1546 return (ENAMETOOLONG);
1547 if (len < offsetof(struct sockaddr, sa_data[0]))
1549 sa = malloc(len, M_SONAME, M_WAITOK);
1550 error = copyin(uaddr, sa, len);
1554 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1555 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1556 sa->sa_family = sa->sa_len;