]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/cloudabi/cloudabi_sock.c
gnu/dts: Update our copy of arm dts from Linux 4.16
[FreeBSD/FreeBSD.git] / sys / compat / cloudabi / cloudabi_sock.c
1 /*-
2  * Copyright (c) 2015-2017 Nuxi, https://nuxi.nl/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/capsicum.h>
31 #include <sys/lock.h>
32 #include <sys/malloc.h>
33 #include <sys/mbuf.h>
34 #include <sys/mutex.h>
35 #include <sys/proc.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>
41
42 #include <net/vnet.h>
43
44 #include <netinet/in.h>
45
46 #include <contrib/cloudabi/cloudabi_types_common.h>
47
48 #include <compat/cloudabi/cloudabi_proto.h>
49 #include <compat/cloudabi/cloudabi_util.h>
50
51 int
52 cloudabi_sys_sock_shutdown(struct thread *td,
53     struct cloudabi_sys_sock_shutdown_args *uap)
54 {
55         int how;
56
57         switch (uap->how) {
58         case CLOUDABI_SHUT_RD:
59                 how = SHUT_RD;
60                 break;
61         case CLOUDABI_SHUT_WR:
62                 how = SHUT_WR;
63                 break;
64         case CLOUDABI_SHUT_RD | CLOUDABI_SHUT_WR:
65                 how = SHUT_RDWR;
66                 break;
67         default:
68                 return (EINVAL);
69         }
70
71         return (kern_shutdown(td, uap->sock, how));
72 }
73
74 int
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)
79 {
80         struct msghdr hdr = {
81                 .msg_iov = data,
82                 .msg_iovlen = datalen,
83         };
84         struct mbuf *control;
85         int error;
86
87         /* Convert flags. */
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;
92
93         control = NULL;
94         error = kern_recvit(td, fd, &hdr, UIO_SYSSPACE,
95             fdslen > 0 ? &control : NULL);
96         if (error != 0)
97                 return (error);
98
99         /* Convert return values. */
100         *rdatalen = td->td_retval[0];
101         td->td_retval[0] = 0;
102         *rfdslen = 0;
103         *rflags = 0;
104         if (hdr.msg_flags & MSG_TRUNC)
105                 *rflags |= CLOUDABI_SOCK_RECV_DATA_TRUNCATED;
106
107         /* Extract file descriptors from SCM_RIGHTS messages. */
108         if (control != NULL) {
109                 struct cmsghdr *chdr;
110
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) {
117                                 size_t nfds;
118
119                                 nfds = (chdr->cmsg_len - CMSG_LEN(0)) /
120                                     sizeof(int);
121                                 if (nfds > fdslen) {
122                                         /* Unable to store file descriptors. */
123                                         nfds = fdslen;
124                                         *rflags |=
125                                             CLOUDABI_SOCK_RECV_FDS_TRUNCATED;
126                                 }
127                                 error = copyout(CMSG_DATA(chdr), fds,
128                                     nfds * sizeof(int));
129                                 if (error != 0) {
130                                         m_free(control);
131                                         return (error);
132                                 }
133                                 fds += nfds;
134                                 fdslen -= nfds;
135                                 *rfdslen += nfds;
136                         }
137                 }
138                 m_free(control);
139         }
140         return (0);
141 }
142
143 int
144 cloudabi_sock_send(struct thread *td, cloudabi_fd_t fd, struct iovec *data,
145     size_t datalen, const cloudabi_fd_t *fds, size_t fdslen, size_t *rdatalen)
146 {
147         struct msghdr hdr = {
148                 .msg_iov = data,
149                 .msg_iovlen = datalen,
150         };
151         struct mbuf *control;
152         int error;
153
154         /* Convert file descriptor array to an SCM_RIGHTS message. */
155         if (fdslen > MCLBYTES || CMSG_SPACE(fdslen * sizeof(int)) > MCLBYTES) {
156                 return (EINVAL);
157         } else if (fdslen > 0) {
158                 struct cmsghdr *chdr;
159
160                 control = m_get2(CMSG_SPACE(fdslen * sizeof(int)),
161                     M_WAITOK, MT_CONTROL, 0);
162                 control->m_len = CMSG_SPACE(fdslen * sizeof(int));
163
164                 chdr = mtod(control, struct cmsghdr *);
165                 chdr->cmsg_len = CMSG_LEN(fdslen * sizeof(int));
166                 chdr->cmsg_level = SOL_SOCKET;
167                 chdr->cmsg_type = SCM_RIGHTS;
168                 error = copyin(fds, CMSG_DATA(chdr), fdslen * sizeof(int));
169                 if (error != 0) {
170                         m_free(control);
171                         return (error);
172                 }
173         } else {
174                 control = NULL;
175         }
176
177         error = kern_sendit(td, fd, &hdr, MSG_NOSIGNAL, control, UIO_USERSPACE);
178         if (error != 0)
179                 return (error);
180         *rdatalen = td->td_retval[0];
181         td->td_retval[0] = 0;
182         return (0);
183 }