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 * 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
127 struct socket_args /* {
134 return (kern_socket(td, uap->domain, uap->type, uap->protocol));
138 kern_socket(struct thread *td, int domain, int type, int protocol)
142 int fd, error, oflag, fflag;
144 AUDIT_ARG_SOCKET(domain, type, protocol);
148 if ((type & SOCK_CLOEXEC) != 0) {
149 type &= ~SOCK_CLOEXEC;
152 if ((type & SOCK_NONBLOCK) != 0) {
153 type &= ~SOCK_NONBLOCK;
158 error = mac_socket_check_create(td->td_ucred, domain, type, protocol);
162 error = falloc(td, &fp, &fd, oflag);
165 /* An extra reference on `fp' has been held for us by falloc(). */
166 error = socreate(domain, &so, type, protocol, td->td_ucred, td);
170 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
171 if ((fflag & FNONBLOCK) != 0)
172 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
173 td->td_retval[0] = fd;
183 struct bind_args /* {
192 error = getsockaddr(&sa, uap->name, uap->namelen);
194 error = kern_bindat(td, AT_FDCWD, uap->s, sa);
201 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
209 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
210 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_BIND),
216 if (KTRPOINT(td, KTR_STRUCT))
220 error = mac_socket_check_bind(td->td_ucred, so, sa);
223 if (dirfd == AT_FDCWD)
224 error = sobind(so, sa, td);
226 error = sobindat(dirfd, so, sa, td);
238 struct bindat_args /* {
248 error = getsockaddr(&sa, uap->name, uap->namelen);
250 error = kern_bindat(td, uap->fd, uap->s, sa);
260 struct listen_args /* {
266 return (kern_listen(td, uap->s, uap->backlog));
270 kern_listen(struct thread *td, int s, int backlog)
278 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_LISTEN),
283 error = mac_socket_check_listen(td->td_ucred, so);
286 error = solisten(so, backlog, td);
296 accept1(td, s, uname, anamelen, flags)
299 struct sockaddr *uname;
303 struct sockaddr *name;
309 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
311 error = copyin(anamelen, &namelen, sizeof (namelen));
315 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
320 if (error == 0 && uname != NULL) {
321 #ifdef COMPAT_OLDSOCK
322 if (flags & ACCEPT4_COMPAT)
323 ((struct osockaddr *)name)->sa_family =
326 error = copyout(name, uname, namelen);
329 error = copyout(&namelen, anamelen,
332 fdclose(td, fp, td->td_retval[0]);
334 free(name, M_SONAME);
339 kern_accept(struct thread *td, int s, struct sockaddr **name,
340 socklen_t *namelen, struct file **fp)
342 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
346 kern_accept4(struct thread *td, int s, struct sockaddr **name,
347 socklen_t *namelen, int flags, struct file **fp)
349 struct file *headfp, *nfp = NULL;
350 struct sockaddr *sa = NULL;
351 struct socket *head, *so;
352 struct filecaps fcaps;
362 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_ACCEPT),
363 &headfp, &fflag, &fcaps);
366 head = headfp->f_data;
367 if ((head->so_options & SO_ACCEPTCONN) == 0) {
372 error = mac_socket_check_accept(td->td_ucred, head);
376 error = falloc_caps(td, &nfp, &fd,
377 (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0, &fcaps);
381 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
386 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
387 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
388 head->so_error = ECONNABORTED;
391 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
398 if (head->so_error) {
399 error = head->so_error;
404 so = TAILQ_FIRST(&head->so_comp);
405 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
406 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
409 * Before changing the flags on the socket, we have to bump the
410 * reference count. Otherwise, if the protocol calls sofree(),
411 * the socket will be released due to a zero refcount.
413 SOCK_LOCK(so); /* soref() and so_state update */
414 soref(so); /* file descriptor reference */
416 TAILQ_REMOVE(&head->so_comp, so, so_list);
418 if (flags & ACCEPT4_INHERIT)
419 so->so_state |= (head->so_state & SS_NBIO);
421 so->so_state |= (flags & SOCK_NONBLOCK) ? SS_NBIO : 0;
422 so->so_qstate &= ~SQ_COMP;
428 /* An extra reference on `nfp' has been held for us by falloc(). */
429 td->td_retval[0] = fd;
431 /* connection has been removed from the listen queue */
432 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
434 if (flags & ACCEPT4_INHERIT) {
435 pgid = fgetown(&head->so_sigio);
437 fsetown(pgid, &so->so_sigio);
439 fflag &= ~(FNONBLOCK | FASYNC);
440 if (flags & SOCK_NONBLOCK)
444 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
445 /* Sync socket nonblocking/async state with file flags */
446 tmp = fflag & FNONBLOCK;
447 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
448 tmp = fflag & FASYNC;
449 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
451 error = soaccept(so, &sa);
459 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
461 /* check sa_len before it is destroyed */
462 if (*namelen > sa->sa_len)
463 *namelen = sa->sa_len;
465 if (KTRPOINT(td, KTR_STRUCT))
475 * close the new descriptor, assuming someone hasn't ripped it
479 fdclose(td, nfp, fd);
482 * Release explicitly held references before returning. We return
483 * a reference on nfp to the caller on success if they request it.
487 filecaps_free(&fcaps);
504 struct accept_args *uap;
507 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
513 struct accept4_args *uap;
516 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
519 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
522 #ifdef COMPAT_OLDSOCK
526 struct accept_args *uap;
529 return (accept1(td, uap->s, uap->name, uap->anamelen,
530 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
532 #endif /* COMPAT_OLDSOCK */
538 struct connect_args /* {
547 error = getsockaddr(&sa, uap->name, uap->namelen);
549 error = kern_connectat(td, AT_FDCWD, uap->s, sa);
556 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
561 int error, interrupted = 0;
564 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
565 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_CONNECT),
570 if (so->so_state & SS_ISCONNECTING) {
575 if (KTRPOINT(td, KTR_STRUCT))
579 error = mac_socket_check_connect(td->td_ucred, so, sa);
583 if (dirfd == AT_FDCWD)
584 error = soconnect(so, sa, td);
586 error = soconnectat(dirfd, so, sa, td);
589 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
594 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
595 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
598 if (error == EINTR || error == ERESTART)
604 error = so->so_error;
610 so->so_state &= ~SS_ISCONNECTING;
611 if (error == ERESTART)
620 sys_connectat(td, uap)
622 struct connectat_args /* {
632 error = getsockaddr(&sa, uap->name, uap->namelen);
634 error = kern_connectat(td, uap->fd, uap->s, sa);
641 kern_socketpair(struct thread *td, int domain, int type, int protocol,
644 struct file *fp1, *fp2;
645 struct socket *so1, *so2;
646 int fd, error, oflag, fflag;
648 AUDIT_ARG_SOCKET(domain, type, protocol);
652 if ((type & SOCK_CLOEXEC) != 0) {
653 type &= ~SOCK_CLOEXEC;
656 if ((type & SOCK_NONBLOCK) != 0) {
657 type &= ~SOCK_NONBLOCK;
661 /* We might want to have a separate check for socket pairs. */
662 error = mac_socket_check_create(td->td_ucred, domain, type,
667 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
670 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
673 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
674 error = falloc(td, &fp1, &fd, oflag);
678 fp1->f_data = so1; /* so1 already has ref count */
679 error = falloc(td, &fp2, &fd, oflag);
682 fp2->f_data = so2; /* so2 already has ref count */
684 error = soconnect2(so1, so2);
687 if (type == SOCK_DGRAM) {
689 * Datagram socket connection is asymmetric.
691 error = soconnect2(so2, so1);
695 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
697 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
699 if ((fflag & FNONBLOCK) != 0) {
700 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
701 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
707 fdclose(td, fp2, rsv[1]);
710 fdclose(td, fp1, rsv[0]);
722 sys_socketpair(struct thread *td, struct socketpair_args *uap)
726 error = kern_socketpair(td, uap->domain, uap->type,
730 error = copyout(sv, uap->rsv, 2 * sizeof(int));
732 (void)kern_close(td, sv[0]);
733 (void)kern_close(td, sv[1]);
739 sendit(td, s, mp, flags)
745 struct mbuf *control;
749 #ifdef CAPABILITY_MODE
750 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
754 if (mp->msg_name != NULL) {
755 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
765 if (mp->msg_control) {
766 if (mp->msg_controllen < sizeof(struct cmsghdr)
767 #ifdef COMPAT_OLDSOCK
768 && mp->msg_flags != MSG_COMPAT
774 error = sockargs(&control, mp->msg_control,
775 mp->msg_controllen, MT_CONTROL);
778 #ifdef COMPAT_OLDSOCK
779 if (mp->msg_flags == MSG_COMPAT) {
782 M_PREPEND(control, sizeof(*cm), M_WAITOK);
783 cm = mtod(control, struct cmsghdr *);
784 cm->cmsg_len = control->m_len;
785 cm->cmsg_level = SOL_SOCKET;
786 cm->cmsg_type = SCM_RIGHTS;
793 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
801 kern_sendit(td, s, mp, flags, control, segflg)
806 struct mbuf *control;
815 struct uio *ktruio = NULL;
821 cap_rights_init(&rights, CAP_SEND);
822 if (mp->msg_name != NULL) {
823 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
824 cap_rights_set(&rights, CAP_CONNECT);
826 error = getsock_cap(td, s, &rights, &fp, NULL, NULL);
831 so = (struct socket *)fp->f_data;
834 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
835 ktrsockaddr(mp->msg_name);
838 if (mp->msg_name != NULL) {
839 error = mac_socket_check_connect(td->td_ucred, so,
846 error = mac_socket_check_send(td->td_ucred, so);
853 auio.uio_iov = mp->msg_iov;
854 auio.uio_iovcnt = mp->msg_iovlen;
855 auio.uio_segflg = segflg;
856 auio.uio_rw = UIO_WRITE;
858 auio.uio_offset = 0; /* XXX */
861 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
862 if ((auio.uio_resid += iov->iov_len) < 0) {
869 if (KTRPOINT(td, KTR_GENIO))
870 ktruio = cloneuio(&auio);
872 len = auio.uio_resid;
873 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
875 if (auio.uio_resid != len && (error == ERESTART ||
876 error == EINTR || error == EWOULDBLOCK))
878 /* Generation of SIGPIPE can be controlled per socket */
879 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
880 !(flags & MSG_NOSIGNAL)) {
881 PROC_LOCK(td->td_proc);
882 tdsignal(td, SIGPIPE);
883 PROC_UNLOCK(td->td_proc);
887 td->td_retval[0] = len - auio.uio_resid;
889 if (ktruio != NULL) {
890 ktruio->uio_resid = td->td_retval[0];
891 ktrgenio(s, UIO_WRITE, ktruio, error);
902 struct sendto_args /* {
914 msg.msg_name = uap->to;
915 msg.msg_namelen = uap->tolen;
919 #ifdef COMPAT_OLDSOCK
922 aiov.iov_base = uap->buf;
923 aiov.iov_len = uap->len;
924 return (sendit(td, uap->s, &msg, uap->flags));
927 #ifdef COMPAT_OLDSOCK
931 struct osend_args /* {
945 aiov.iov_base = uap->buf;
946 aiov.iov_len = uap->len;
949 return (sendit(td, uap->s, &msg, uap->flags));
955 struct osendmsg_args /* {
965 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
968 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
972 msg.msg_flags = MSG_COMPAT;
973 error = sendit(td, uap->s, &msg, uap->flags);
982 struct sendmsg_args /* {
992 error = copyin(uap->msg, &msg, sizeof (msg));
995 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
999 #ifdef COMPAT_OLDSOCK
1002 error = sendit(td, uap->s, &msg, uap->flags);
1008 kern_recvit(td, s, mp, fromseg, controlp)
1012 enum uio_seg fromseg;
1013 struct mbuf **controlp;
1017 struct mbuf *m, *control = NULL;
1021 struct sockaddr *fromsa = NULL;
1022 cap_rights_t rights;
1024 struct uio *ktruio = NULL;
1029 if (controlp != NULL)
1033 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_RECV),
1040 error = mac_socket_check_receive(td->td_ucred, so);
1047 auio.uio_iov = mp->msg_iov;
1048 auio.uio_iovcnt = mp->msg_iovlen;
1049 auio.uio_segflg = UIO_USERSPACE;
1050 auio.uio_rw = UIO_READ;
1052 auio.uio_offset = 0; /* XXX */
1055 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
1056 if ((auio.uio_resid += iov->iov_len) < 0) {
1062 if (KTRPOINT(td, KTR_GENIO))
1063 ktruio = cloneuio(&auio);
1065 len = auio.uio_resid;
1066 error = soreceive(so, &fromsa, &auio, NULL,
1067 (mp->msg_control || controlp) ? &control : NULL,
1070 if (auio.uio_resid != len && (error == ERESTART ||
1071 error == EINTR || error == EWOULDBLOCK))
1075 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
1077 if (ktruio != NULL) {
1078 ktruio->uio_resid = len - auio.uio_resid;
1079 ktrgenio(s, UIO_READ, ktruio, error);
1084 td->td_retval[0] = len - auio.uio_resid;
1086 len = mp->msg_namelen;
1087 if (len <= 0 || fromsa == NULL)
1090 /* save sa_len before it is destroyed by MSG_COMPAT */
1091 len = MIN(len, fromsa->sa_len);
1092 #ifdef COMPAT_OLDSOCK
1093 if (mp->msg_flags & MSG_COMPAT)
1094 ((struct osockaddr *)fromsa)->sa_family =
1097 if (fromseg == UIO_USERSPACE) {
1098 error = copyout(fromsa, mp->msg_name,
1103 bcopy(fromsa, mp->msg_name, len);
1105 mp->msg_namelen = len;
1107 if (mp->msg_control && controlp == NULL) {
1108 #ifdef COMPAT_OLDSOCK
1110 * We assume that old recvmsg calls won't receive access
1111 * rights and other control info, esp. as control info
1112 * is always optional and those options didn't exist in 4.3.
1113 * If we receive rights, trim the cmsghdr; anything else
1116 if (control && mp->msg_flags & MSG_COMPAT) {
1117 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1119 mtod(control, struct cmsghdr *)->cmsg_type !=
1121 mp->msg_controllen = 0;
1124 control->m_len -= sizeof (struct cmsghdr);
1125 control->m_data += sizeof (struct cmsghdr);
1128 len = mp->msg_controllen;
1130 mp->msg_controllen = 0;
1131 ctlbuf = mp->msg_control;
1133 while (m && len > 0) {
1134 unsigned int tocopy;
1136 if (len >= m->m_len)
1139 mp->msg_flags |= MSG_CTRUNC;
1143 if ((error = copyout(mtod(m, caddr_t),
1144 ctlbuf, tocopy)) != 0)
1151 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1156 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1157 ktrsockaddr(fromsa);
1159 free(fromsa, M_SONAME);
1161 if (error == 0 && controlp != NULL)
1162 *controlp = control;
1170 recvit(td, s, mp, namelenp)
1178 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1181 if (namelenp != NULL) {
1182 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1183 #ifdef COMPAT_OLDSOCK
1184 if (mp->msg_flags & MSG_COMPAT)
1185 error = 0; /* old recvfrom didn't check */
1192 sys_recvfrom(td, uap)
1194 struct recvfrom_args /* {
1199 struct sockaddr * __restrict from;
1200 socklen_t * __restrict fromlenaddr;
1207 if (uap->fromlenaddr) {
1208 error = copyin(uap->fromlenaddr,
1209 &msg.msg_namelen, sizeof (msg.msg_namelen));
1213 msg.msg_namelen = 0;
1215 msg.msg_name = uap->from;
1216 msg.msg_iov = &aiov;
1218 aiov.iov_base = uap->buf;
1219 aiov.iov_len = uap->len;
1220 msg.msg_control = 0;
1221 msg.msg_flags = uap->flags;
1222 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1227 #ifdef COMPAT_OLDSOCK
1231 struct recvfrom_args *uap;
1234 uap->flags |= MSG_COMPAT;
1235 return (sys_recvfrom(td, uap));
1239 #ifdef COMPAT_OLDSOCK
1243 struct orecv_args /* {
1254 msg.msg_namelen = 0;
1255 msg.msg_iov = &aiov;
1257 aiov.iov_base = uap->buf;
1258 aiov.iov_len = uap->len;
1259 msg.msg_control = 0;
1260 msg.msg_flags = uap->flags;
1261 return (recvit(td, uap->s, &msg, NULL));
1265 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1266 * overlays the new one, missing only the flags, and with the (old) access
1267 * rights where the control fields are now.
1272 struct orecvmsg_args /* {
1274 struct omsghdr *msg;
1282 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1285 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1288 msg.msg_flags = uap->flags | MSG_COMPAT;
1290 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1291 if (msg.msg_controllen && error == 0)
1292 error = copyout(&msg.msg_controllen,
1293 &uap->msg->msg_accrightslen, sizeof (int));
1300 sys_recvmsg(td, uap)
1302 struct recvmsg_args /* {
1309 struct iovec *uiov, *iov;
1312 error = copyin(uap->msg, &msg, sizeof (msg));
1315 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1318 msg.msg_flags = uap->flags;
1319 #ifdef COMPAT_OLDSOCK
1320 msg.msg_flags &= ~MSG_COMPAT;
1324 error = recvit(td, uap->s, &msg, NULL);
1327 error = copyout(&msg, uap->msg, sizeof(msg));
1335 sys_shutdown(td, uap)
1337 struct shutdown_args /* {
1343 return (kern_shutdown(td, uap->s, uap->how));
1347 kern_shutdown(struct thread *td, int s, int how)
1351 cap_rights_t rights;
1355 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SHUTDOWN),
1359 error = soshutdown(so, how);
1361 * Previous versions did not return ENOTCONN, but 0 in
1362 * case the socket was not connected. Some important
1363 * programs like syslogd up to r279016, 2015-02-19,
1364 * still depend on this behavior.
1366 if (error == ENOTCONN &&
1367 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1376 sys_setsockopt(td, uap)
1378 struct setsockopt_args /* {
1387 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1388 uap->val, UIO_USERSPACE, uap->valsize));
1392 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1398 enum uio_seg valseg;
1403 struct sockopt sopt;
1404 cap_rights_t rights;
1407 if (val == NULL && valsize != 0)
1409 if ((int)valsize < 0)
1412 sopt.sopt_dir = SOPT_SET;
1413 sopt.sopt_level = level;
1414 sopt.sopt_name = name;
1415 sopt.sopt_val = val;
1416 sopt.sopt_valsize = valsize;
1422 sopt.sopt_td = NULL;
1425 panic("kern_setsockopt called with bad valseg");
1429 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SETSOCKOPT),
1433 error = sosetopt(so, &sopt);
1441 sys_getsockopt(td, uap)
1443 struct getsockopt_args /* {
1447 void * __restrict val;
1448 socklen_t * __restrict avalsize;
1455 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1460 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1461 uap->val, UIO_USERSPACE, &valsize);
1464 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1469 * Kernel version of getsockopt.
1470 * optval can be a userland or userspace. optlen is always a kernel pointer.
1473 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1479 enum uio_seg valseg;
1484 struct sockopt sopt;
1485 cap_rights_t rights;
1490 if ((int)*valsize < 0)
1493 sopt.sopt_dir = SOPT_GET;
1494 sopt.sopt_level = level;
1495 sopt.sopt_name = name;
1496 sopt.sopt_val = val;
1497 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1503 sopt.sopt_td = NULL;
1506 panic("kern_getsockopt called with bad valseg");
1510 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_GETSOCKOPT),
1514 error = sogetopt(so, &sopt);
1515 *valsize = sopt.sopt_valsize;
1522 * getsockname1() - Get socket name.
1526 getsockname1(td, uap, compat)
1528 struct getsockname_args /* {
1530 struct sockaddr * __restrict asa;
1531 socklen_t * __restrict alen;
1535 struct sockaddr *sa;
1539 error = copyin(uap->alen, &len, sizeof(len));
1543 error = kern_getsockname(td, uap->fdes, &sa, &len);
1548 #ifdef COMPAT_OLDSOCK
1550 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1552 error = copyout(sa, uap->asa, (u_int)len);
1556 error = copyout(&len, uap->alen, sizeof(len));
1561 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1566 cap_rights_t rights;
1571 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETSOCKNAME),
1577 CURVNET_SET(so->so_vnet);
1578 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1585 len = MIN(*alen, (*sa)->sa_len);
1588 if (KTRPOINT(td, KTR_STRUCT))
1593 if (error != 0 && *sa != NULL) {
1594 free(*sa, M_SONAME);
1601 sys_getsockname(td, uap)
1603 struct getsockname_args *uap;
1606 return (getsockname1(td, uap, 0));
1609 #ifdef COMPAT_OLDSOCK
1611 ogetsockname(td, uap)
1613 struct getsockname_args *uap;
1616 return (getsockname1(td, uap, 1));
1618 #endif /* COMPAT_OLDSOCK */
1621 * getpeername1() - Get name of peer for connected socket.
1625 getpeername1(td, uap, compat)
1627 struct getpeername_args /* {
1629 struct sockaddr * __restrict asa;
1630 socklen_t * __restrict alen;
1634 struct sockaddr *sa;
1638 error = copyin(uap->alen, &len, sizeof (len));
1642 error = kern_getpeername(td, uap->fdes, &sa, &len);
1647 #ifdef COMPAT_OLDSOCK
1649 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1651 error = copyout(sa, uap->asa, (u_int)len);
1655 error = copyout(&len, uap->alen, sizeof(len));
1660 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1665 cap_rights_t rights;
1670 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETPEERNAME),
1675 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1680 CURVNET_SET(so->so_vnet);
1681 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1688 len = MIN(*alen, (*sa)->sa_len);
1691 if (KTRPOINT(td, KTR_STRUCT))
1695 if (error != 0 && *sa != NULL) {
1696 free(*sa, M_SONAME);
1705 sys_getpeername(td, uap)
1707 struct getpeername_args *uap;
1710 return (getpeername1(td, uap, 0));
1713 #ifdef COMPAT_OLDSOCK
1715 ogetpeername(td, uap)
1717 struct ogetpeername_args *uap;
1720 /* XXX uap should have type `getpeername_args *' to begin with. */
1721 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1723 #endif /* COMPAT_OLDSOCK */
1726 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1728 struct sockaddr *sa;
1732 if (buflen > MLEN) {
1733 #ifdef COMPAT_OLDSOCK
1734 if (type == MT_SONAME && buflen <= 112)
1735 buflen = MLEN; /* unix domain compat. hack */
1738 if (buflen > MCLBYTES)
1741 m = m_get2(buflen, M_WAITOK, type, 0);
1743 error = copyin(buf, mtod(m, void *), buflen);
1748 if (type == MT_SONAME) {
1749 sa = mtod(m, struct sockaddr *);
1751 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1752 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1753 sa->sa_family = sa->sa_len;
1755 sa->sa_len = buflen;
1762 getsockaddr(namp, uaddr, len)
1763 struct sockaddr **namp;
1767 struct sockaddr *sa;
1770 if (len > SOCK_MAXADDRLEN)
1771 return (ENAMETOOLONG);
1772 if (len < offsetof(struct sockaddr, sa_data[0]))
1774 sa = malloc(len, M_SONAME, M_WAITOK);
1775 error = copyin(uaddr, sa, len);
1779 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1780 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1781 sa->sa_family = sa->sa_len;