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/unpcb.h>
62 #include <sys/ktrace.h>
64 #ifdef COMPAT_FREEBSD32
65 #include <compat/freebsd32/freebsd32_util.h>
70 #include <security/audit/audit.h>
71 #include <security/mac/mac_framework.h>
74 * Flags for accept1() and kern_accept4(), in addition to SOCK_CLOEXEC
77 #define ACCEPT4_INHERIT 0x1
78 #define ACCEPT4_COMPAT 0x2
80 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
81 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
83 static int accept1(struct thread *td, int s, struct sockaddr *uname,
84 socklen_t *anamelen, int flags);
85 static int getsockname1(struct thread *td, struct getsockname_args *uap,
87 static int getpeername1(struct thread *td, struct getpeername_args *uap,
89 static int sockargs(struct mbuf **, char *, socklen_t, int);
92 * Convert a user file descriptor to a kernel file entry and check if required
93 * capability rights are present.
94 * If required copy of current set of capability rights is returned.
95 * A reference on the file entry is held upon returning.
98 getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
99 struct file **fpp, u_int *fflagp, struct filecaps *havecapsp)
104 error = fget_cap(td, fd, rightsp, &fp, havecapsp);
107 if (fp->f_type != DTYPE_SOCKET) {
109 if (havecapsp != NULL)
110 filecaps_free(havecapsp);
114 *fflagp = fp->f_flag;
120 * System call interface to the socket abstraction.
122 #if defined(COMPAT_43)
123 #define COMPAT_OLDSOCK
129 struct socket_args /* {
136 return (kern_socket(td, uap->domain, uap->type, uap->protocol));
140 kern_socket(struct thread *td, int domain, int type, int protocol)
144 int fd, error, oflag, fflag;
146 AUDIT_ARG_SOCKET(domain, type, protocol);
150 if ((type & SOCK_CLOEXEC) != 0) {
151 type &= ~SOCK_CLOEXEC;
154 if ((type & SOCK_NONBLOCK) != 0) {
155 type &= ~SOCK_NONBLOCK;
160 error = mac_socket_check_create(td->td_ucred, domain, type, protocol);
164 error = falloc(td, &fp, &fd, oflag);
167 /* An extra reference on `fp' has been held for us by falloc(). */
168 error = socreate(domain, &so, type, protocol, td->td_ucred, td);
172 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
173 if ((fflag & FNONBLOCK) != 0)
174 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
175 td->td_retval[0] = fd;
185 struct bind_args /* {
194 error = getsockaddr(&sa, uap->name, uap->namelen);
196 error = kern_bindat(td, AT_FDCWD, uap->s, sa);
203 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
211 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
212 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_BIND),
218 if (KTRPOINT(td, KTR_STRUCT))
222 error = mac_socket_check_bind(td->td_ucred, so, sa);
225 if (dirfd == AT_FDCWD)
226 error = sobind(so, sa, td);
228 error = sobindat(dirfd, so, sa, td);
240 struct bindat_args /* {
250 error = getsockaddr(&sa, uap->name, uap->namelen);
252 error = kern_bindat(td, uap->fd, uap->s, sa);
262 struct listen_args /* {
268 return (kern_listen(td, uap->s, uap->backlog));
272 kern_listen(struct thread *td, int s, int backlog)
280 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_LISTEN),
285 error = mac_socket_check_listen(td->td_ucred, so);
288 error = solisten(so, backlog, td);
298 accept1(td, s, uname, anamelen, flags)
301 struct sockaddr *uname;
305 struct sockaddr *name;
311 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
313 error = copyin(anamelen, &namelen, sizeof (namelen));
317 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
322 if (error == 0 && uname != NULL) {
323 #ifdef COMPAT_OLDSOCK
324 if (flags & ACCEPT4_COMPAT)
325 ((struct osockaddr *)name)->sa_family =
328 error = copyout(name, uname, namelen);
331 error = copyout(&namelen, anamelen,
334 fdclose(td, fp, td->td_retval[0]);
336 free(name, M_SONAME);
341 kern_accept(struct thread *td, int s, struct sockaddr **name,
342 socklen_t *namelen, struct file **fp)
344 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
348 kern_accept4(struct thread *td, int s, struct sockaddr **name,
349 socklen_t *namelen, int flags, struct file **fp)
351 struct file *headfp, *nfp = NULL;
352 struct sockaddr *sa = NULL;
353 struct socket *head, *so;
354 struct filecaps fcaps;
364 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_ACCEPT),
365 &headfp, &fflag, &fcaps);
368 head = headfp->f_data;
369 if ((head->so_options & SO_ACCEPTCONN) == 0) {
374 error = mac_socket_check_accept(td->td_ucred, head);
378 error = falloc_caps(td, &nfp, &fd,
379 (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0, &fcaps);
383 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
388 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
389 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
390 head->so_error = ECONNABORTED;
393 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
400 if (head->so_error) {
401 error = head->so_error;
406 so = TAILQ_FIRST(&head->so_comp);
407 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
408 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
411 * Before changing the flags on the socket, we have to bump the
412 * reference count. Otherwise, if the protocol calls sofree(),
413 * the socket will be released due to a zero refcount.
415 SOCK_LOCK(so); /* soref() and so_state update */
416 soref(so); /* file descriptor reference */
418 TAILQ_REMOVE(&head->so_comp, so, so_list);
420 if (flags & ACCEPT4_INHERIT)
421 so->so_state |= (head->so_state & SS_NBIO);
423 so->so_state |= (flags & SOCK_NONBLOCK) ? SS_NBIO : 0;
424 so->so_qstate &= ~SQ_COMP;
430 /* An extra reference on `nfp' has been held for us by falloc(). */
431 td->td_retval[0] = fd;
433 /* connection has been removed from the listen queue */
434 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
436 if (flags & ACCEPT4_INHERIT) {
437 pgid = fgetown(&head->so_sigio);
439 fsetown(pgid, &so->so_sigio);
441 fflag &= ~(FNONBLOCK | FASYNC);
442 if (flags & SOCK_NONBLOCK)
446 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
447 /* Sync socket nonblocking/async state with file flags */
448 tmp = fflag & FNONBLOCK;
449 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
450 tmp = fflag & FASYNC;
451 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
453 error = soaccept(so, &sa);
461 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
463 /* check sa_len before it is destroyed */
464 if (*namelen > sa->sa_len)
465 *namelen = sa->sa_len;
467 if (KTRPOINT(td, KTR_STRUCT))
477 * close the new descriptor, assuming someone hasn't ripped it
481 fdclose(td, nfp, fd);
484 * Release explicitly held references before returning. We return
485 * a reference on nfp to the caller on success if they request it.
489 filecaps_free(&fcaps);
506 struct accept_args *uap;
509 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
515 struct accept4_args *uap;
518 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
521 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
524 #ifdef COMPAT_OLDSOCK
528 struct accept_args *uap;
531 return (accept1(td, uap->s, uap->name, uap->anamelen,
532 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
534 #endif /* COMPAT_OLDSOCK */
540 struct connect_args /* {
549 error = getsockaddr(&sa, uap->name, uap->namelen);
551 error = kern_connectat(td, AT_FDCWD, uap->s, sa);
558 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
563 int error, interrupted = 0;
566 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
567 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_CONNECT),
572 if (so->so_state & SS_ISCONNECTING) {
577 if (KTRPOINT(td, KTR_STRUCT))
581 error = mac_socket_check_connect(td->td_ucred, so, sa);
585 if (dirfd == AT_FDCWD)
586 error = soconnect(so, sa, td);
588 error = soconnectat(dirfd, so, sa, td);
591 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
596 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
597 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
600 if (error == EINTR || error == ERESTART)
606 error = so->so_error;
612 so->so_state &= ~SS_ISCONNECTING;
613 if (error == ERESTART)
622 sys_connectat(td, uap)
624 struct connectat_args /* {
634 error = getsockaddr(&sa, uap->name, uap->namelen);
636 error = kern_connectat(td, uap->fd, uap->s, sa);
643 kern_socketpair(struct thread *td, int domain, int type, int protocol,
646 struct file *fp1, *fp2;
647 struct socket *so1, *so2;
648 int fd, error, oflag, fflag;
650 AUDIT_ARG_SOCKET(domain, type, protocol);
654 if ((type & SOCK_CLOEXEC) != 0) {
655 type &= ~SOCK_CLOEXEC;
658 if ((type & SOCK_NONBLOCK) != 0) {
659 type &= ~SOCK_NONBLOCK;
663 /* We might want to have a separate check for socket pairs. */
664 error = mac_socket_check_create(td->td_ucred, domain, type,
669 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
672 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
675 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
676 error = falloc(td, &fp1, &fd, oflag);
680 fp1->f_data = so1; /* so1 already has ref count */
681 error = falloc(td, &fp2, &fd, oflag);
684 fp2->f_data = so2; /* so2 already has ref count */
686 error = soconnect2(so1, so2);
689 if (type == SOCK_DGRAM) {
691 * Datagram socket connection is asymmetric.
693 error = soconnect2(so2, so1);
696 } else if (so1->so_proto->pr_flags & PR_CONNREQUIRED) {
697 struct unpcb *unp, *unp2;
698 unp = sotounpcb(so1);
699 unp2 = sotounpcb(so2);
701 * No need to lock the unps, because the sockets are brand-new.
702 * No other threads can be using them yet
704 unp_copy_peercred(td, unp, unp2, unp);
706 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
708 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
710 if ((fflag & FNONBLOCK) != 0) {
711 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
712 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
718 fdclose(td, fp2, rsv[1]);
721 fdclose(td, fp1, rsv[0]);
733 sys_socketpair(struct thread *td, struct socketpair_args *uap)
737 error = kern_socketpair(td, uap->domain, uap->type,
741 error = copyout(sv, uap->rsv, 2 * sizeof(int));
743 (void)kern_close(td, sv[0]);
744 (void)kern_close(td, sv[1]);
750 sendit(td, s, mp, flags)
756 struct mbuf *control;
760 #ifdef CAPABILITY_MODE
761 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
765 if (mp->msg_name != NULL) {
766 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
776 if (mp->msg_control) {
777 if (mp->msg_controllen < sizeof(struct cmsghdr)
778 #ifdef COMPAT_OLDSOCK
779 && mp->msg_flags != MSG_COMPAT
785 error = sockargs(&control, mp->msg_control,
786 mp->msg_controllen, MT_CONTROL);
789 #ifdef COMPAT_OLDSOCK
790 if (mp->msg_flags == MSG_COMPAT) {
793 M_PREPEND(control, sizeof(*cm), M_WAITOK);
794 cm = mtod(control, struct cmsghdr *);
795 cm->cmsg_len = control->m_len;
796 cm->cmsg_level = SOL_SOCKET;
797 cm->cmsg_type = SCM_RIGHTS;
804 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
812 kern_sendit(td, s, mp, flags, control, segflg)
817 struct mbuf *control;
826 struct uio *ktruio = NULL;
832 cap_rights_init(&rights, CAP_SEND);
833 if (mp->msg_name != NULL) {
834 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
835 cap_rights_set(&rights, CAP_CONNECT);
837 error = getsock_cap(td, s, &rights, &fp, NULL, NULL);
842 so = (struct socket *)fp->f_data;
845 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
846 ktrsockaddr(mp->msg_name);
849 if (mp->msg_name != NULL) {
850 error = mac_socket_check_connect(td->td_ucred, so,
857 error = mac_socket_check_send(td->td_ucred, so);
864 auio.uio_iov = mp->msg_iov;
865 auio.uio_iovcnt = mp->msg_iovlen;
866 auio.uio_segflg = segflg;
867 auio.uio_rw = UIO_WRITE;
869 auio.uio_offset = 0; /* XXX */
872 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
873 if ((auio.uio_resid += iov->iov_len) < 0) {
880 if (KTRPOINT(td, KTR_GENIO))
881 ktruio = cloneuio(&auio);
883 len = auio.uio_resid;
884 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
886 if (auio.uio_resid != len && (error == ERESTART ||
887 error == EINTR || error == EWOULDBLOCK))
889 /* Generation of SIGPIPE can be controlled per socket */
890 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
891 !(flags & MSG_NOSIGNAL)) {
892 PROC_LOCK(td->td_proc);
893 tdsignal(td, SIGPIPE);
894 PROC_UNLOCK(td->td_proc);
898 td->td_retval[0] = len - auio.uio_resid;
900 if (ktruio != NULL) {
901 ktruio->uio_resid = td->td_retval[0];
902 ktrgenio(s, UIO_WRITE, ktruio, error);
913 struct sendto_args /* {
925 msg.msg_name = uap->to;
926 msg.msg_namelen = uap->tolen;
930 #ifdef COMPAT_OLDSOCK
933 aiov.iov_base = uap->buf;
934 aiov.iov_len = uap->len;
935 return (sendit(td, uap->s, &msg, uap->flags));
938 #ifdef COMPAT_OLDSOCK
942 struct osend_args /* {
956 aiov.iov_base = uap->buf;
957 aiov.iov_len = uap->len;
960 return (sendit(td, uap->s, &msg, uap->flags));
966 struct osendmsg_args /* {
976 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
979 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
983 msg.msg_flags = MSG_COMPAT;
984 error = sendit(td, uap->s, &msg, uap->flags);
993 struct sendmsg_args /* {
1003 error = copyin(uap->msg, &msg, sizeof (msg));
1006 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1010 #ifdef COMPAT_OLDSOCK
1013 error = sendit(td, uap->s, &msg, uap->flags);
1019 kern_recvit(td, s, mp, fromseg, controlp)
1023 enum uio_seg fromseg;
1024 struct mbuf **controlp;
1028 struct mbuf *control, *m;
1032 struct sockaddr *fromsa = NULL;
1033 cap_rights_t rights;
1035 struct uio *ktruio = NULL;
1040 if (controlp != NULL)
1044 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_RECV),
1051 error = mac_socket_check_receive(td->td_ucred, so);
1058 auio.uio_iov = mp->msg_iov;
1059 auio.uio_iovcnt = mp->msg_iovlen;
1060 auio.uio_segflg = UIO_USERSPACE;
1061 auio.uio_rw = UIO_READ;
1063 auio.uio_offset = 0; /* XXX */
1066 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
1067 if ((auio.uio_resid += iov->iov_len) < 0) {
1073 if (KTRPOINT(td, KTR_GENIO))
1074 ktruio = cloneuio(&auio);
1077 len = auio.uio_resid;
1078 error = soreceive(so, &fromsa, &auio, NULL,
1079 (mp->msg_control || controlp) ? &control : NULL,
1082 if (auio.uio_resid != len && (error == ERESTART ||
1083 error == EINTR || error == EWOULDBLOCK))
1087 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
1089 if (ktruio != NULL) {
1090 ktruio->uio_resid = len - auio.uio_resid;
1091 ktrgenio(s, UIO_READ, ktruio, error);
1096 td->td_retval[0] = len - auio.uio_resid;
1098 len = mp->msg_namelen;
1099 if (len <= 0 || fromsa == NULL)
1102 /* save sa_len before it is destroyed by MSG_COMPAT */
1103 len = MIN(len, fromsa->sa_len);
1104 #ifdef COMPAT_OLDSOCK
1105 if (mp->msg_flags & MSG_COMPAT)
1106 ((struct osockaddr *)fromsa)->sa_family =
1109 if (fromseg == UIO_USERSPACE) {
1110 error = copyout(fromsa, mp->msg_name,
1115 bcopy(fromsa, mp->msg_name, len);
1117 mp->msg_namelen = len;
1119 if (mp->msg_control && controlp == NULL) {
1120 #ifdef COMPAT_OLDSOCK
1122 * We assume that old recvmsg calls won't receive access
1123 * rights and other control info, esp. as control info
1124 * is always optional and those options didn't exist in 4.3.
1125 * If we receive rights, trim the cmsghdr; anything else
1128 if (control && mp->msg_flags & MSG_COMPAT) {
1129 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1131 mtod(control, struct cmsghdr *)->cmsg_type !=
1133 mp->msg_controllen = 0;
1136 control->m_len -= sizeof (struct cmsghdr);
1137 control->m_data += sizeof (struct cmsghdr);
1140 ctlbuf = mp->msg_control;
1141 len = mp->msg_controllen;
1142 mp->msg_controllen = 0;
1143 for (m = control; m != NULL && len >= m->m_len; m = m->m_next) {
1144 if ((error = copyout(mtod(m, caddr_t), ctlbuf,
1150 mp->msg_controllen += m->m_len;
1153 mp->msg_flags |= MSG_CTRUNC;
1154 m_dispose_extcontrolm(m);
1160 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1161 ktrsockaddr(fromsa);
1163 free(fromsa, M_SONAME);
1165 if (error == 0 && controlp != NULL)
1166 *controlp = control;
1167 else if (control != NULL) {
1169 m_dispose_extcontrolm(control);
1177 recvit(td, s, mp, namelenp)
1185 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1188 if (namelenp != NULL) {
1189 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1190 #ifdef COMPAT_OLDSOCK
1191 if (mp->msg_flags & MSG_COMPAT)
1192 error = 0; /* old recvfrom didn't check */
1199 sys_recvfrom(td, uap)
1201 struct recvfrom_args /* {
1206 struct sockaddr * __restrict from;
1207 socklen_t * __restrict fromlenaddr;
1214 if (uap->fromlenaddr) {
1215 error = copyin(uap->fromlenaddr,
1216 &msg.msg_namelen, sizeof (msg.msg_namelen));
1220 msg.msg_namelen = 0;
1222 msg.msg_name = uap->from;
1223 msg.msg_iov = &aiov;
1225 aiov.iov_base = uap->buf;
1226 aiov.iov_len = uap->len;
1227 msg.msg_control = 0;
1228 msg.msg_flags = uap->flags;
1229 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1234 #ifdef COMPAT_OLDSOCK
1238 struct recvfrom_args *uap;
1241 uap->flags |= MSG_COMPAT;
1242 return (sys_recvfrom(td, uap));
1246 #ifdef COMPAT_OLDSOCK
1250 struct orecv_args /* {
1261 msg.msg_namelen = 0;
1262 msg.msg_iov = &aiov;
1264 aiov.iov_base = uap->buf;
1265 aiov.iov_len = uap->len;
1266 msg.msg_control = 0;
1267 msg.msg_flags = uap->flags;
1268 return (recvit(td, uap->s, &msg, NULL));
1272 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1273 * overlays the new one, missing only the flags, and with the (old) access
1274 * rights where the control fields are now.
1279 struct orecvmsg_args /* {
1281 struct omsghdr *msg;
1289 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1292 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1295 msg.msg_flags = uap->flags | MSG_COMPAT;
1297 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1298 if (msg.msg_controllen && error == 0)
1299 error = copyout(&msg.msg_controllen,
1300 &uap->msg->msg_accrightslen, sizeof (int));
1307 sys_recvmsg(td, uap)
1309 struct recvmsg_args /* {
1316 struct iovec *uiov, *iov;
1319 error = copyin(uap->msg, &msg, sizeof (msg));
1322 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1325 msg.msg_flags = uap->flags;
1326 #ifdef COMPAT_OLDSOCK
1327 msg.msg_flags &= ~MSG_COMPAT;
1331 error = recvit(td, uap->s, &msg, NULL);
1334 error = copyout(&msg, uap->msg, sizeof(msg));
1342 sys_shutdown(td, uap)
1344 struct shutdown_args /* {
1350 return (kern_shutdown(td, uap->s, uap->how));
1354 kern_shutdown(struct thread *td, int s, int how)
1358 cap_rights_t rights;
1362 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SHUTDOWN),
1366 error = soshutdown(so, how);
1368 * Previous versions did not return ENOTCONN, but 0 in
1369 * case the socket was not connected. Some important
1370 * programs like syslogd up to r279016, 2015-02-19,
1371 * still depend on this behavior.
1373 if (error == ENOTCONN &&
1374 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1383 sys_setsockopt(td, uap)
1385 struct setsockopt_args /* {
1394 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1395 uap->val, UIO_USERSPACE, uap->valsize));
1399 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1405 enum uio_seg valseg;
1410 struct sockopt sopt;
1411 cap_rights_t rights;
1414 if (val == NULL && valsize != 0)
1416 if ((int)valsize < 0)
1419 sopt.sopt_dir = SOPT_SET;
1420 sopt.sopt_level = level;
1421 sopt.sopt_name = name;
1422 sopt.sopt_val = val;
1423 sopt.sopt_valsize = valsize;
1429 sopt.sopt_td = NULL;
1432 panic("kern_setsockopt called with bad valseg");
1436 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SETSOCKOPT),
1440 error = sosetopt(so, &sopt);
1448 sys_getsockopt(td, uap)
1450 struct getsockopt_args /* {
1454 void * __restrict val;
1455 socklen_t * __restrict avalsize;
1462 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1467 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1468 uap->val, UIO_USERSPACE, &valsize);
1471 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1476 * Kernel version of getsockopt.
1477 * optval can be a userland or userspace. optlen is always a kernel pointer.
1480 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1486 enum uio_seg valseg;
1491 struct sockopt sopt;
1492 cap_rights_t rights;
1497 if ((int)*valsize < 0)
1500 sopt.sopt_dir = SOPT_GET;
1501 sopt.sopt_level = level;
1502 sopt.sopt_name = name;
1503 sopt.sopt_val = val;
1504 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1510 sopt.sopt_td = NULL;
1513 panic("kern_getsockopt called with bad valseg");
1517 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_GETSOCKOPT),
1521 error = sogetopt(so, &sopt);
1522 *valsize = sopt.sopt_valsize;
1529 * getsockname1() - Get socket name.
1533 getsockname1(td, uap, compat)
1535 struct getsockname_args /* {
1537 struct sockaddr * __restrict asa;
1538 socklen_t * __restrict alen;
1542 struct sockaddr *sa;
1546 error = copyin(uap->alen, &len, sizeof(len));
1550 error = kern_getsockname(td, uap->fdes, &sa, &len);
1555 #ifdef COMPAT_OLDSOCK
1557 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1559 error = copyout(sa, uap->asa, (u_int)len);
1563 error = copyout(&len, uap->alen, sizeof(len));
1568 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1573 cap_rights_t rights;
1578 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETSOCKNAME),
1584 CURVNET_SET(so->so_vnet);
1585 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1592 len = MIN(*alen, (*sa)->sa_len);
1595 if (KTRPOINT(td, KTR_STRUCT))
1600 if (error != 0 && *sa != NULL) {
1601 free(*sa, M_SONAME);
1608 sys_getsockname(td, uap)
1610 struct getsockname_args *uap;
1613 return (getsockname1(td, uap, 0));
1616 #ifdef COMPAT_OLDSOCK
1618 ogetsockname(td, uap)
1620 struct getsockname_args *uap;
1623 return (getsockname1(td, uap, 1));
1625 #endif /* COMPAT_OLDSOCK */
1628 * getpeername1() - Get name of peer for connected socket.
1632 getpeername1(td, uap, compat)
1634 struct getpeername_args /* {
1636 struct sockaddr * __restrict asa;
1637 socklen_t * __restrict alen;
1641 struct sockaddr *sa;
1645 error = copyin(uap->alen, &len, sizeof (len));
1649 error = kern_getpeername(td, uap->fdes, &sa, &len);
1654 #ifdef COMPAT_OLDSOCK
1656 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1658 error = copyout(sa, uap->asa, (u_int)len);
1662 error = copyout(&len, uap->alen, sizeof(len));
1667 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1672 cap_rights_t rights;
1677 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETPEERNAME),
1682 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1687 CURVNET_SET(so->so_vnet);
1688 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1695 len = MIN(*alen, (*sa)->sa_len);
1698 if (KTRPOINT(td, KTR_STRUCT))
1702 if (error != 0 && *sa != NULL) {
1703 free(*sa, M_SONAME);
1712 sys_getpeername(td, uap)
1714 struct getpeername_args *uap;
1717 return (getpeername1(td, uap, 0));
1720 #ifdef COMPAT_OLDSOCK
1722 ogetpeername(td, uap)
1724 struct ogetpeername_args *uap;
1727 /* XXX uap should have type `getpeername_args *' to begin with. */
1728 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1730 #endif /* COMPAT_OLDSOCK */
1733 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1735 struct sockaddr *sa;
1739 if (buflen > MLEN) {
1740 #ifdef COMPAT_OLDSOCK
1741 if (type == MT_SONAME && buflen <= 112)
1742 buflen = MLEN; /* unix domain compat. hack */
1745 if (buflen > MCLBYTES)
1748 m = m_get2(buflen, M_WAITOK, type, 0);
1750 error = copyin(buf, mtod(m, void *), buflen);
1755 if (type == MT_SONAME) {
1756 sa = mtod(m, struct sockaddr *);
1758 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1759 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1760 sa->sa_family = sa->sa_len;
1762 sa->sa_len = buflen;
1769 getsockaddr(namp, uaddr, len)
1770 struct sockaddr **namp;
1774 struct sockaddr *sa;
1777 if (len > SOCK_MAXADDRLEN)
1778 return (ENAMETOOLONG);
1779 if (len < offsetof(struct sockaddr, sa_data[0]))
1781 sa = malloc(len, M_SONAME, M_WAITOK);
1782 error = copyin(uaddr, sa, len);
1786 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1787 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1788 sa->sa_family = sa->sa_len;
1797 * Dispose of externalized rights from an SCM_RIGHTS message. This function
1798 * should be used in error or truncation cases to avoid leaking file descriptors
1799 * into the recipient's (the current thread's) table.
1802 m_dispose_extcontrolm(struct mbuf *m)
1807 cap_rights_t rights;
1808 socklen_t clen, datalen;
1809 int error, fd, *fds, nfd;
1812 for (; m != NULL; m = m->m_next) {
1813 if (m->m_type != MT_EXTCONTROL)
1815 cm = mtod(m, struct cmsghdr *);
1818 if (clen < sizeof(*cm))
1819 panic("%s: truncated mbuf %p", __func__, m);
1820 datalen = CMSG_SPACE(cm->cmsg_len - CMSG_SPACE(0));
1822 panic("%s: truncated mbuf %p", __func__, m);
1824 if (cm->cmsg_level == SOL_SOCKET &&
1825 cm->cmsg_type == SCM_RIGHTS) {
1826 fds = (int *)CMSG_DATA(cm);
1827 nfd = (cm->cmsg_len - CMSG_SPACE(0)) /
1832 error = fget(td, fd,
1833 cap_rights_init(&rights), &fp);
1835 fdclose(td, fp, fd);
1839 cm = (struct cmsghdr *)((uint8_t *)cm + datalen);
1841 m_chtype(m, MT_CONTROL);