2 * Copyright (c) 2015-2017 Nuxi, https://nuxi.nl/
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/capsicum.h>
32 #include <sys/malloc.h>
34 #include <sys/mutex.h>
36 #include <sys/protosw.h>
37 #include <sys/socket.h>
38 #include <sys/socketvar.h>
39 #include <sys/syscallsubr.h>
40 #include <sys/systm.h>
44 #include <netinet/in.h>
46 #include <contrib/cloudabi/cloudabi_types_common.h>
48 #include <compat/cloudabi/cloudabi_proto.h>
49 #include <compat/cloudabi/cloudabi_util.h>
52 cloudabi_sys_sock_shutdown(struct thread *td,
53 struct cloudabi_sys_sock_shutdown_args *uap)
58 case CLOUDABI_SHUT_RD:
61 case CLOUDABI_SHUT_WR:
64 case CLOUDABI_SHUT_RD | CLOUDABI_SHUT_WR:
71 return (kern_shutdown(td, uap->sock, how));
75 cloudabi_sock_recv(struct thread *td, cloudabi_fd_t fd, struct iovec *data,
76 size_t datalen, cloudabi_fd_t *fds, size_t fdslen,
77 cloudabi_riflags_t flags, size_t *rdatalen, size_t *rfdslen,
78 cloudabi_roflags_t *rflags)
82 .msg_iovlen = datalen,
88 if (flags & CLOUDABI_SOCK_RECV_PEEK)
89 hdr.msg_flags |= MSG_PEEK;
90 if (flags & CLOUDABI_SOCK_RECV_WAITALL)
91 hdr.msg_flags |= MSG_WAITALL;
94 error = kern_recvit(td, fd, &hdr, UIO_SYSSPACE,
95 fdslen > 0 ? &control : NULL);
99 /* Convert return values. */
100 *rdatalen = td->td_retval[0];
101 td->td_retval[0] = 0;
104 if (hdr.msg_flags & MSG_TRUNC)
105 *rflags |= CLOUDABI_SOCK_RECV_DATA_TRUNCATED;
107 /* Extract file descriptors from SCM_RIGHTS messages. */
108 if (control != NULL) {
109 struct cmsghdr *chdr;
111 hdr.msg_control = mtod(control, void *);
112 hdr.msg_controllen = control->m_len;
113 for (chdr = CMSG_FIRSTHDR(&hdr); chdr != NULL;
114 chdr = CMSG_NXTHDR(&hdr, chdr)) {
115 if (chdr->cmsg_level == SOL_SOCKET &&
116 chdr->cmsg_type == SCM_RIGHTS) {
119 nfds = (chdr->cmsg_len - CMSG_LEN(0)) /
122 /* Unable to store file descriptors. */
124 CLOUDABI_SOCK_RECV_FDS_TRUNCATED;
125 m_dispose_extcontrolm(control);
128 error = copyout(CMSG_DATA(chdr), fds,
137 if (control != NULL) {
139 m_dispose_extcontrolm(control);
147 cloudabi_sock_send(struct thread *td, cloudabi_fd_t fd, struct iovec *data,
148 size_t datalen, const cloudabi_fd_t *fds, size_t fdslen, size_t *rdatalen)
150 struct msghdr hdr = {
152 .msg_iovlen = datalen,
154 struct mbuf *control;
157 /* Convert file descriptor array to an SCM_RIGHTS message. */
158 if (fdslen > MCLBYTES || CMSG_SPACE(fdslen * sizeof(int)) > MCLBYTES) {
160 } else if (fdslen > 0) {
161 struct cmsghdr *chdr;
163 control = m_get2(CMSG_SPACE(fdslen * sizeof(int)),
164 M_WAITOK, MT_CONTROL, 0);
165 control->m_len = CMSG_SPACE(fdslen * sizeof(int));
167 chdr = mtod(control, struct cmsghdr *);
168 chdr->cmsg_len = CMSG_LEN(fdslen * sizeof(int));
169 chdr->cmsg_level = SOL_SOCKET;
170 chdr->cmsg_type = SCM_RIGHTS;
171 error = copyin(fds, CMSG_DATA(chdr), fdslen * sizeof(int));
180 error = kern_sendit(td, fd, &hdr, MSG_NOSIGNAL, control, UIO_USERSPACE);
183 *rdatalen = td->td_retval[0];
184 td->td_retval[0] = 0;