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)
128 return (kern_socket(td, uap->domain, uap->type, uap->protocol));
132 kern_socket(struct thread *td, int domain, int type, int protocol)
136 int fd, error, oflag, fflag;
138 AUDIT_ARG_SOCKET(domain, type, protocol);
142 if ((type & SOCK_CLOEXEC) != 0) {
143 type &= ~SOCK_CLOEXEC;
146 if ((type & SOCK_NONBLOCK) != 0) {
147 type &= ~SOCK_NONBLOCK;
152 error = mac_socket_check_create(td->td_ucred, domain, type, protocol);
156 error = falloc(td, &fp, &fd, oflag);
159 /* An extra reference on `fp' has been held for us by falloc(). */
160 error = socreate(domain, &so, type, protocol, td->td_ucred, td);
164 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
165 if ((fflag & FNONBLOCK) != 0)
166 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
167 td->td_retval[0] = fd;
174 sys_bind(struct thread *td, struct bind_args *uap)
179 error = getsockaddr(&sa, uap->name, uap->namelen);
181 error = kern_bindat(td, AT_FDCWD, uap->s, sa);
188 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
196 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
197 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_BIND),
203 if (KTRPOINT(td, KTR_STRUCT))
207 error = mac_socket_check_bind(td->td_ucred, so, sa);
210 if (dirfd == AT_FDCWD)
211 error = sobind(so, sa, td);
213 error = sobindat(dirfd, so, sa, td);
222 sys_bindat(struct thread *td, struct bindat_args *uap)
227 error = getsockaddr(&sa, uap->name, uap->namelen);
229 error = kern_bindat(td, uap->fd, uap->s, sa);
236 sys_listen(struct thread *td, struct listen_args *uap)
239 return (kern_listen(td, uap->s, uap->backlog));
243 kern_listen(struct thread *td, int s, int backlog)
251 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_LISTEN),
256 error = mac_socket_check_listen(td->td_ucred, so);
259 error = solisten(so, backlog, td);
269 accept1(td, s, uname, anamelen, flags)
272 struct sockaddr *uname;
276 struct sockaddr *name;
282 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
284 error = copyin(anamelen, &namelen, sizeof (namelen));
288 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
293 if (error == 0 && uname != NULL) {
294 #ifdef COMPAT_OLDSOCK
295 if (flags & ACCEPT4_COMPAT)
296 ((struct osockaddr *)name)->sa_family =
299 error = copyout(name, uname, namelen);
302 error = copyout(&namelen, anamelen,
305 fdclose(td, fp, td->td_retval[0]);
307 free(name, M_SONAME);
312 kern_accept(struct thread *td, int s, struct sockaddr **name,
313 socklen_t *namelen, struct file **fp)
315 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
319 kern_accept4(struct thread *td, int s, struct sockaddr **name,
320 socklen_t *namelen, int flags, struct file **fp)
322 struct file *headfp, *nfp = NULL;
323 struct sockaddr *sa = NULL;
324 struct socket *head, *so;
325 struct filecaps fcaps;
335 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_ACCEPT),
336 &headfp, &fflag, &fcaps);
339 head = headfp->f_data;
340 if ((head->so_options & SO_ACCEPTCONN) == 0) {
345 error = mac_socket_check_accept(td->td_ucred, head);
349 error = falloc_caps(td, &nfp, &fd,
350 (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0, &fcaps);
354 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
359 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
360 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
361 head->so_error = ECONNABORTED;
364 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
371 if (head->so_error) {
372 error = head->so_error;
377 so = TAILQ_FIRST(&head->so_comp);
378 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
379 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
382 * Before changing the flags on the socket, we have to bump the
383 * reference count. Otherwise, if the protocol calls sofree(),
384 * the socket will be released due to a zero refcount.
386 SOCK_LOCK(so); /* soref() and so_state update */
387 soref(so); /* file descriptor reference */
389 TAILQ_REMOVE(&head->so_comp, so, so_list);
391 if (flags & ACCEPT4_INHERIT)
392 so->so_state |= (head->so_state & SS_NBIO);
394 so->so_state |= (flags & SOCK_NONBLOCK) ? SS_NBIO : 0;
395 so->so_qstate &= ~SQ_COMP;
401 /* An extra reference on `nfp' has been held for us by falloc(). */
402 td->td_retval[0] = fd;
404 /* connection has been removed from the listen queue */
405 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
407 if (flags & ACCEPT4_INHERIT) {
408 pgid = fgetown(&head->so_sigio);
410 fsetown(pgid, &so->so_sigio);
412 fflag &= ~(FNONBLOCK | FASYNC);
413 if (flags & SOCK_NONBLOCK)
417 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
418 /* Sync socket nonblocking/async state with file flags */
419 tmp = fflag & FNONBLOCK;
420 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
421 tmp = fflag & FASYNC;
422 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
424 error = soaccept(so, &sa);
432 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
434 /* check sa_len before it is destroyed */
435 if (*namelen > sa->sa_len)
436 *namelen = sa->sa_len;
438 if (KTRPOINT(td, KTR_STRUCT))
448 * close the new descriptor, assuming someone hasn't ripped it
452 fdclose(td, nfp, fd);
455 * Release explicitly held references before returning. We return
456 * a reference on nfp to the caller on success if they request it.
460 filecaps_free(&fcaps);
477 struct accept_args *uap;
480 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
486 struct accept4_args *uap;
489 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
492 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
495 #ifdef COMPAT_OLDSOCK
499 struct accept_args *uap;
502 return (accept1(td, uap->s, uap->name, uap->anamelen,
503 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
505 #endif /* COMPAT_OLDSOCK */
508 sys_connect(struct thread *td, struct connect_args *uap)
513 error = getsockaddr(&sa, uap->name, uap->namelen);
515 error = kern_connectat(td, AT_FDCWD, uap->s, sa);
522 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
527 int error, interrupted = 0;
530 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
531 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_CONNECT),
536 if (so->so_state & SS_ISCONNECTING) {
541 if (KTRPOINT(td, KTR_STRUCT))
545 error = mac_socket_check_connect(td->td_ucred, so, sa);
549 if (dirfd == AT_FDCWD)
550 error = soconnect(so, sa, td);
552 error = soconnectat(dirfd, so, sa, td);
555 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
560 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
561 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
564 if (error == EINTR || error == ERESTART)
570 error = so->so_error;
576 so->so_state &= ~SS_ISCONNECTING;
577 if (error == ERESTART)
585 sys_connectat(struct thread *td, struct connectat_args *uap)
590 error = getsockaddr(&sa, uap->name, uap->namelen);
592 error = kern_connectat(td, uap->fd, uap->s, sa);
599 kern_socketpair(struct thread *td, int domain, int type, int protocol,
602 struct file *fp1, *fp2;
603 struct socket *so1, *so2;
604 int fd, error, oflag, fflag;
606 AUDIT_ARG_SOCKET(domain, type, protocol);
610 if ((type & SOCK_CLOEXEC) != 0) {
611 type &= ~SOCK_CLOEXEC;
614 if ((type & SOCK_NONBLOCK) != 0) {
615 type &= ~SOCK_NONBLOCK;
619 /* We might want to have a separate check for socket pairs. */
620 error = mac_socket_check_create(td->td_ucred, domain, type,
625 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
628 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
631 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
632 error = falloc(td, &fp1, &fd, oflag);
636 fp1->f_data = so1; /* so1 already has ref count */
637 error = falloc(td, &fp2, &fd, oflag);
640 fp2->f_data = so2; /* so2 already has ref count */
642 error = soconnect2(so1, so2);
645 if (type == SOCK_DGRAM) {
647 * Datagram socket connection is asymmetric.
649 error = soconnect2(so2, so1);
653 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
655 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
657 if ((fflag & FNONBLOCK) != 0) {
658 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
659 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
665 fdclose(td, fp2, rsv[1]);
668 fdclose(td, fp1, rsv[0]);
680 sys_socketpair(struct thread *td, struct socketpair_args *uap)
684 error = kern_socketpair(td, uap->domain, uap->type,
688 error = copyout(sv, uap->rsv, 2 * sizeof(int));
690 (void)kern_close(td, sv[0]);
691 (void)kern_close(td, sv[1]);
697 sendit(struct thread *td, int s, struct msghdr *mp, int flags)
699 struct mbuf *control;
703 #ifdef CAPABILITY_MODE
704 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
708 if (mp->msg_name != NULL) {
709 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
719 if (mp->msg_control) {
720 if (mp->msg_controllen < sizeof(struct cmsghdr)
721 #ifdef COMPAT_OLDSOCK
722 && mp->msg_flags != MSG_COMPAT
728 error = sockargs(&control, mp->msg_control,
729 mp->msg_controllen, MT_CONTROL);
732 #ifdef COMPAT_OLDSOCK
733 if (mp->msg_flags == MSG_COMPAT) {
736 M_PREPEND(control, sizeof(*cm), M_WAITOK);
737 cm = mtod(control, struct cmsghdr *);
738 cm->cmsg_len = control->m_len;
739 cm->cmsg_level = SOL_SOCKET;
740 cm->cmsg_type = SCM_RIGHTS;
747 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
755 kern_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
756 struct mbuf *control, enum uio_seg segflg)
764 struct uio *ktruio = NULL;
770 cap_rights_init(&rights, CAP_SEND);
771 if (mp->msg_name != NULL) {
772 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
773 cap_rights_set(&rights, CAP_CONNECT);
775 error = getsock_cap(td, s, &rights, &fp, NULL, NULL);
780 so = (struct socket *)fp->f_data;
783 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
784 ktrsockaddr(mp->msg_name);
787 if (mp->msg_name != NULL) {
788 error = mac_socket_check_connect(td->td_ucred, so,
795 error = mac_socket_check_send(td->td_ucred, so);
802 auio.uio_iov = mp->msg_iov;
803 auio.uio_iovcnt = mp->msg_iovlen;
804 auio.uio_segflg = segflg;
805 auio.uio_rw = UIO_WRITE;
807 auio.uio_offset = 0; /* XXX */
810 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
811 if ((auio.uio_resid += iov->iov_len) < 0) {
818 if (KTRPOINT(td, KTR_GENIO))
819 ktruio = cloneuio(&auio);
821 len = auio.uio_resid;
822 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
824 if (auio.uio_resid != len && (error == ERESTART ||
825 error == EINTR || error == EWOULDBLOCK))
827 /* Generation of SIGPIPE can be controlled per socket */
828 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
829 !(flags & MSG_NOSIGNAL)) {
830 PROC_LOCK(td->td_proc);
831 tdsignal(td, SIGPIPE);
832 PROC_UNLOCK(td->td_proc);
836 td->td_retval[0] = len - auio.uio_resid;
838 if (ktruio != NULL) {
839 ktruio->uio_resid = td->td_retval[0];
840 ktrgenio(s, UIO_WRITE, ktruio, error);
849 sys_sendto(struct thread *td, struct sendto_args *uap)
854 msg.msg_name = uap->to;
855 msg.msg_namelen = uap->tolen;
859 #ifdef COMPAT_OLDSOCK
862 aiov.iov_base = uap->buf;
863 aiov.iov_len = uap->len;
864 return (sendit(td, uap->s, &msg, uap->flags));
867 #ifdef COMPAT_OLDSOCK
869 osend(struct thread *td, struct osend_args *uap)
878 aiov.iov_base = uap->buf;
879 aiov.iov_len = uap->len;
882 return (sendit(td, uap->s, &msg, uap->flags));
886 osendmsg(struct thread *td, struct osendmsg_args *uap)
892 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
895 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
899 msg.msg_flags = MSG_COMPAT;
900 error = sendit(td, uap->s, &msg, uap->flags);
907 sys_sendmsg(struct thread *td, struct sendmsg_args *uap)
913 error = copyin(uap->msg, &msg, sizeof (msg));
916 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
920 #ifdef COMPAT_OLDSOCK
923 error = sendit(td, uap->s, &msg, uap->flags);
929 kern_recvit(struct thread *td, int s, struct msghdr *mp, enum uio_seg fromseg,
930 struct mbuf **controlp)
934 struct mbuf *m, *control = NULL;
938 struct sockaddr *fromsa = NULL;
941 struct uio *ktruio = NULL;
946 if (controlp != NULL)
950 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_RECV),
957 error = mac_socket_check_receive(td->td_ucred, so);
964 auio.uio_iov = mp->msg_iov;
965 auio.uio_iovcnt = mp->msg_iovlen;
966 auio.uio_segflg = UIO_USERSPACE;
967 auio.uio_rw = UIO_READ;
969 auio.uio_offset = 0; /* XXX */
972 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
973 if ((auio.uio_resid += iov->iov_len) < 0) {
979 if (KTRPOINT(td, KTR_GENIO))
980 ktruio = cloneuio(&auio);
982 len = auio.uio_resid;
983 error = soreceive(so, &fromsa, &auio, NULL,
984 (mp->msg_control || controlp) ? &control : NULL,
987 if (auio.uio_resid != len && (error == ERESTART ||
988 error == EINTR || error == EWOULDBLOCK))
992 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
994 if (ktruio != NULL) {
995 ktruio->uio_resid = len - auio.uio_resid;
996 ktrgenio(s, UIO_READ, ktruio, error);
1001 td->td_retval[0] = len - auio.uio_resid;
1003 len = mp->msg_namelen;
1004 if (len <= 0 || fromsa == NULL)
1007 /* save sa_len before it is destroyed by MSG_COMPAT */
1008 len = MIN(len, fromsa->sa_len);
1009 #ifdef COMPAT_OLDSOCK
1010 if (mp->msg_flags & MSG_COMPAT)
1011 ((struct osockaddr *)fromsa)->sa_family =
1014 if (fromseg == UIO_USERSPACE) {
1015 error = copyout(fromsa, mp->msg_name,
1020 bcopy(fromsa, mp->msg_name, len);
1022 mp->msg_namelen = len;
1024 if (mp->msg_control && controlp == NULL) {
1025 #ifdef COMPAT_OLDSOCK
1027 * We assume that old recvmsg calls won't receive access
1028 * rights and other control info, esp. as control info
1029 * is always optional and those options didn't exist in 4.3.
1030 * If we receive rights, trim the cmsghdr; anything else
1033 if (control && mp->msg_flags & MSG_COMPAT) {
1034 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1036 mtod(control, struct cmsghdr *)->cmsg_type !=
1038 mp->msg_controllen = 0;
1041 control->m_len -= sizeof (struct cmsghdr);
1042 control->m_data += sizeof (struct cmsghdr);
1045 len = mp->msg_controllen;
1047 mp->msg_controllen = 0;
1048 ctlbuf = mp->msg_control;
1050 while (m && len > 0) {
1051 unsigned int tocopy;
1053 if (len >= m->m_len)
1056 mp->msg_flags |= MSG_CTRUNC;
1060 if ((error = copyout(mtod(m, caddr_t),
1061 ctlbuf, tocopy)) != 0)
1068 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1073 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1074 ktrsockaddr(fromsa);
1076 free(fromsa, M_SONAME);
1078 if (error == 0 && controlp != NULL)
1079 *controlp = control;
1087 recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp)
1091 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1094 if (namelenp != NULL) {
1095 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1096 #ifdef COMPAT_OLDSOCK
1097 if (mp->msg_flags & MSG_COMPAT)
1098 error = 0; /* old recvfrom didn't check */
1105 sys_recvfrom(struct thread *td, struct recvfrom_args *uap)
1111 if (uap->fromlenaddr) {
1112 error = copyin(uap->fromlenaddr,
1113 &msg.msg_namelen, sizeof (msg.msg_namelen));
1117 msg.msg_namelen = 0;
1119 msg.msg_name = uap->from;
1120 msg.msg_iov = &aiov;
1122 aiov.iov_base = uap->buf;
1123 aiov.iov_len = uap->len;
1124 msg.msg_control = 0;
1125 msg.msg_flags = uap->flags;
1126 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1131 #ifdef COMPAT_OLDSOCK
1133 orecvfrom(struct thread *td, struct recvfrom_args *uap)
1136 uap->flags |= MSG_COMPAT;
1137 return (sys_recvfrom(td, uap));
1141 #ifdef COMPAT_OLDSOCK
1143 orecv(struct thread *td, struct orecv_args *uap)
1149 msg.msg_namelen = 0;
1150 msg.msg_iov = &aiov;
1152 aiov.iov_base = uap->buf;
1153 aiov.iov_len = uap->len;
1154 msg.msg_control = 0;
1155 msg.msg_flags = uap->flags;
1156 return (recvit(td, uap->s, &msg, NULL));
1160 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1161 * overlays the new one, missing only the flags, and with the (old) access
1162 * rights where the control fields are now.
1165 orecvmsg(struct thread *td, struct orecvmsg_args *uap)
1171 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1174 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1177 msg.msg_flags = uap->flags | MSG_COMPAT;
1179 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1180 if (msg.msg_controllen && error == 0)
1181 error = copyout(&msg.msg_controllen,
1182 &uap->msg->msg_accrightslen, sizeof (int));
1189 sys_recvmsg(struct thread *td, struct recvmsg_args *uap)
1192 struct iovec *uiov, *iov;
1195 error = copyin(uap->msg, &msg, sizeof (msg));
1198 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1201 msg.msg_flags = uap->flags;
1202 #ifdef COMPAT_OLDSOCK
1203 msg.msg_flags &= ~MSG_COMPAT;
1207 error = recvit(td, uap->s, &msg, NULL);
1210 error = copyout(&msg, uap->msg, sizeof(msg));
1217 sys_shutdown(struct thread *td, struct shutdown_args *uap)
1220 return (kern_shutdown(td, uap->s, uap->how));
1224 kern_shutdown(struct thread *td, int s, int how)
1228 cap_rights_t rights;
1232 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SHUTDOWN),
1236 error = soshutdown(so, how);
1238 * Previous versions did not return ENOTCONN, but 0 in
1239 * case the socket was not connected. Some important
1240 * programs like syslogd up to r279016, 2015-02-19,
1241 * still depend on this behavior.
1243 if (error == ENOTCONN &&
1244 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1252 sys_setsockopt(struct thread *td, struct setsockopt_args *uap)
1255 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1256 uap->val, UIO_USERSPACE, uap->valsize));
1260 kern_setsockopt(struct thread *td, int s, int level, int name, void *val,
1261 enum uio_seg valseg, socklen_t valsize)
1265 struct sockopt sopt;
1266 cap_rights_t rights;
1269 if (val == NULL && valsize != 0)
1271 if ((int)valsize < 0)
1274 sopt.sopt_dir = SOPT_SET;
1275 sopt.sopt_level = level;
1276 sopt.sopt_name = name;
1277 sopt.sopt_val = val;
1278 sopt.sopt_valsize = valsize;
1284 sopt.sopt_td = NULL;
1287 panic("kern_setsockopt called with bad valseg");
1291 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SETSOCKOPT),
1295 error = sosetopt(so, &sopt);
1302 sys_getsockopt(struct thread *td, struct getsockopt_args *uap)
1308 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1313 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1314 uap->val, UIO_USERSPACE, &valsize);
1317 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1322 * Kernel version of getsockopt.
1323 * optval can be a userland or userspace. optlen is always a kernel pointer.
1326 kern_getsockopt(struct thread *td, int s, int level, int name, void *val,
1327 enum uio_seg valseg, socklen_t *valsize)
1331 struct sockopt sopt;
1332 cap_rights_t rights;
1337 if ((int)*valsize < 0)
1340 sopt.sopt_dir = SOPT_GET;
1341 sopt.sopt_level = level;
1342 sopt.sopt_name = name;
1343 sopt.sopt_val = val;
1344 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1350 sopt.sopt_td = NULL;
1353 panic("kern_getsockopt called with bad valseg");
1357 error = getsock_cap(td, s, cap_rights_init(&rights, CAP_GETSOCKOPT),
1361 error = sogetopt(so, &sopt);
1362 *valsize = sopt.sopt_valsize;
1369 * getsockname1() - Get socket name.
1372 getsockname1(struct thread *td, struct getsockname_args *uap, int compat)
1374 struct sockaddr *sa;
1378 error = copyin(uap->alen, &len, sizeof(len));
1382 error = kern_getsockname(td, uap->fdes, &sa, &len);
1387 #ifdef COMPAT_OLDSOCK
1389 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1391 error = copyout(sa, uap->asa, (u_int)len);
1395 error = copyout(&len, uap->alen, sizeof(len));
1400 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1405 cap_rights_t rights;
1410 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETSOCKNAME),
1416 CURVNET_SET(so->so_vnet);
1417 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1424 len = MIN(*alen, (*sa)->sa_len);
1427 if (KTRPOINT(td, KTR_STRUCT))
1432 if (error != 0 && *sa != NULL) {
1433 free(*sa, M_SONAME);
1440 sys_getsockname(struct thread *td, struct getsockname_args *uap)
1443 return (getsockname1(td, uap, 0));
1446 #ifdef COMPAT_OLDSOCK
1448 ogetsockname(struct thread *td, struct getsockname_args *uap)
1451 return (getsockname1(td, uap, 1));
1453 #endif /* COMPAT_OLDSOCK */
1456 * getpeername1() - Get name of peer for connected socket.
1459 getpeername1(struct thread *td, struct getpeername_args *uap, int compat)
1461 struct sockaddr *sa;
1465 error = copyin(uap->alen, &len, sizeof (len));
1469 error = kern_getpeername(td, uap->fdes, &sa, &len);
1474 #ifdef COMPAT_OLDSOCK
1476 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1478 error = copyout(sa, uap->asa, (u_int)len);
1482 error = copyout(&len, uap->alen, sizeof(len));
1487 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1492 cap_rights_t rights;
1497 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_GETPEERNAME),
1502 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1507 CURVNET_SET(so->so_vnet);
1508 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1515 len = MIN(*alen, (*sa)->sa_len);
1518 if (KTRPOINT(td, KTR_STRUCT))
1522 if (error != 0 && *sa != NULL) {
1523 free(*sa, M_SONAME);
1532 sys_getpeername(struct thread *td, struct getpeername_args *uap)
1535 return (getpeername1(td, uap, 0));
1538 #ifdef COMPAT_OLDSOCK
1540 ogetpeername(struct thread *td, struct ogetpeername_args *uap)
1543 /* XXX uap should have type `getpeername_args *' to begin with. */
1544 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1546 #endif /* COMPAT_OLDSOCK */
1549 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1551 struct sockaddr *sa;
1555 if (buflen > MLEN) {
1556 #ifdef COMPAT_OLDSOCK
1557 if (type == MT_SONAME && buflen <= 112)
1558 buflen = MLEN; /* unix domain compat. hack */
1561 if (buflen > MCLBYTES)
1564 m = m_get2(buflen, M_WAITOK, type, 0);
1566 error = copyin(buf, mtod(m, void *), buflen);
1571 if (type == MT_SONAME) {
1572 sa = mtod(m, struct sockaddr *);
1574 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1575 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1576 sa->sa_family = sa->sa_len;
1578 sa->sa_len = buflen;
1585 getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
1587 struct sockaddr *sa;
1590 if (len > SOCK_MAXADDRLEN)
1591 return (ENAMETOOLONG);
1592 if (len < offsetof(struct sockaddr, sa_data[0]))
1594 sa = malloc(len, M_SONAME, M_WAITOK);
1595 error = copyin(uaddr, sa, len);
1599 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1600 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1601 sa->sa_family = sa->sa_len;