xref: /NextBSD/sys/compat/cloudabi64/cloudabi64_sock.c (revision c21ffb8d6aca32c9584cfa072f309a5890a21aea)
1 /*-
2  * Copyright (c) 2015 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/kernel.h>
31 #include <sys/malloc.h>
32 #include <sys/proc.h>
33 #include <sys/socket.h>
34 #include <sys/syscallsubr.h>
35 #include <sys/systm.h>
36 #include <sys/uio.h>
37 
38 #include <compat/cloudabi/cloudabi_util.h>
39 
40 #include <compat/cloudabi64/cloudabi64_syscalldefs.h>
41 #include <compat/cloudabi64/cloudabi64_proto.h>
42 
43 static MALLOC_DEFINE(M_SOCKET, "socket", "CloudABI socket");
44 
45 int
cloudabi64_sys_sock_recv(struct thread * td,struct cloudabi64_sys_sock_recv_args * uap)46 cloudabi64_sys_sock_recv(struct thread *td,
47     struct cloudabi64_sys_sock_recv_args *uap)
48 {
49 	struct sockaddr_storage ss;
50 	cloudabi64_recv_in_t ri;
51 	cloudabi64_recv_out_t ro = {};
52 	cloudabi64_iovec_t iovobj;
53 	struct msghdr msghdr = {};
54 	size_t i;
55 	int error;
56 
57 	error = copyin(uap->in, &ri, sizeof(ri));
58 	if (error != 0)
59 		return (error);
60 
61 	/* Convert results in cloudabi_recv_in_t to struct msghdr. */
62 	if (ri.ri_datalen > UIO_MAXIOV)
63 		return (EINVAL);
64 	msghdr.msg_iovlen = ri.ri_datalen;
65 	msghdr.msg_iov = malloc(msghdr.msg_iovlen * sizeof(struct iovec),
66 	    M_SOCKET, M_WAITOK);
67 	for (i = 0; i < msghdr.msg_iovlen; i++) {
68 		error = copyin(&((cloudabi64_iovec_t *)ri.ri_data)[i], &iovobj,
69 		    sizeof(iovobj));
70 		if (error != 0) {
71 			free(msghdr.msg_iov, M_SOCKET);
72 			return (error);
73 		}
74 		msghdr.msg_iov[i].iov_base = (void *)iovobj.iov_base;
75 		msghdr.msg_iov[i].iov_len = iovobj.iov_len;
76 	}
77 	msghdr.msg_name = &ss;
78 	msghdr.msg_namelen = sizeof(ss);
79 	if (ri.ri_flags & CLOUDABI_MSG_PEEK)
80 		msghdr.msg_flags |= MSG_PEEK;
81 	if (ri.ri_flags & CLOUDABI_MSG_WAITALL)
82 		msghdr.msg_flags |= MSG_WAITALL;
83 
84 	/* TODO(ed): Add file descriptor passing. */
85 	error = kern_recvit(td, uap->s, &msghdr, UIO_SYSSPACE, NULL);
86 	free(msghdr.msg_iov, M_SOCKET);
87 	if (error != 0)
88 		return (error);
89 
90 	/* Convert results in msghdr to cloudabi_recv_out_t. */
91 	ro.ro_datalen = td->td_retval[0];
92 	cloudabi_convert_sockaddr((struct sockaddr *)&ss,
93 	    MIN(msghdr.msg_namelen, sizeof(ss)), &ro.ro_peername);
94 	td->td_retval[0] = 0;
95 	return (copyout(&ro, uap->out, sizeof(ro)));
96 }
97 
98 int
cloudabi64_sys_sock_send(struct thread * td,struct cloudabi64_sys_sock_send_args * uap)99 cloudabi64_sys_sock_send(struct thread *td,
100     struct cloudabi64_sys_sock_send_args *uap)
101 {
102 	cloudabi64_send_in_t si;
103 	cloudabi64_send_out_t so = {};
104 	cloudabi64_ciovec_t iovobj;
105 	struct msghdr msghdr = {};
106 	size_t i;
107 	int error, flags;
108 
109 	error = copyin(uap->in, &si, sizeof(si));
110 	if (error != 0)
111 		return (error);
112 
113 	/* Convert results in cloudabi_send_in_t to struct msghdr. */
114 	if (si.si_datalen > UIO_MAXIOV)
115 		return (EINVAL);
116 	msghdr.msg_iovlen = si.si_datalen;
117 	msghdr.msg_iov = malloc(msghdr.msg_iovlen * sizeof(struct iovec),
118 	    M_SOCKET, M_WAITOK);
119 	for (i = 0; i < msghdr.msg_iovlen; i++) {
120 		error = copyin(&((cloudabi64_ciovec_t *)si.si_data)[i], &iovobj,
121 		    sizeof(iovobj));
122 		if (error != 0) {
123 			free(msghdr.msg_iov, M_SOCKET);
124 			return (error);
125 		}
126 		msghdr.msg_iov[i].iov_base = (void *)iovobj.iov_base;
127 		msghdr.msg_iov[i].iov_len = iovobj.iov_len;
128 	}
129 
130 	flags = MSG_NOSIGNAL;
131 	if (si.si_flags & CLOUDABI_MSG_EOR)
132 		flags |= MSG_EOR;
133 
134 	/* TODO(ed): Add file descriptor passing. */
135 	error = kern_sendit(td, uap->s, &msghdr, flags, NULL, UIO_USERSPACE);
136 	free(msghdr.msg_iov, M_SOCKET);
137 	if (error != 0)
138 		return (error);
139 
140 	/* Convert results in msghdr to cloudabi_send_out_t. */
141 	so.so_datalen = td->td_retval[0];
142 	td->td_retval[0] = 0;
143 	return (copyout(&so, uap->out, sizeof(so)));
144 }
145