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: stable/12/sys/compat/cloudabi32/cloudabi32_sock.c 327684 2018-01-07 22:38:45Z ed $");
28 
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/malloc.h>
32 #include <sys/systm.h>
33 #include <sys/uio.h>
34 
35 #include <contrib/cloudabi/cloudabi32_types.h>
36 
37 #include <compat/cloudabi/cloudabi_util.h>
38 
39 #include <compat/cloudabi32/cloudabi32_proto.h>
40 #include <compat/cloudabi32/cloudabi32_util.h>
41 
42 static MALLOC_DEFINE(M_SOCKET, "socket", "CloudABI socket");
43 
44 int
cloudabi32_sys_sock_recv(struct thread * td,struct cloudabi32_sys_sock_recv_args * uap)45 cloudabi32_sys_sock_recv(struct thread *td,
46     struct cloudabi32_sys_sock_recv_args *uap)
47 {
48 	cloudabi32_recv_in_t ri;
49 	cloudabi32_recv_out_t ro = {};
50 	cloudabi32_iovec_t iovobj;
51 	struct iovec *iov;
52 	const cloudabi32_iovec_t *user_iov;
53 	size_t i, rdatalen, rfdslen;
54 	int error;
55 
56 	error = copyin(uap->in, &ri, sizeof(ri));
57 	if (error != 0)
58 		return (error);
59 
60 	/* Convert iovecs to native format. */
61 	if (ri.ri_data_len > UIO_MAXIOV)
62 		return (EINVAL);
63 	iov = mallocarray(ri.ri_data_len, sizeof(struct iovec),
64 	    M_SOCKET, M_WAITOK);
65 	user_iov = TO_PTR(ri.ri_data);
66 	for (i = 0; i < ri.ri_data_len; i++) {
67 		error = copyin(&user_iov[i], &iovobj, sizeof(iovobj));
68 		if (error != 0) {
69 			free(iov, M_SOCKET);
70 			return (error);
71 		}
72 		iov[i].iov_base = TO_PTR(iovobj.buf);
73 		iov[i].iov_len = iovobj.buf_len;
74 	}
75 
76 	error = cloudabi_sock_recv(td, uap->sock, iov, ri.ri_data_len,
77 	    TO_PTR(ri.ri_fds), ri.ri_fds_len, ri.ri_flags, &rdatalen,
78 	    &rfdslen, &ro.ro_flags);
79 	free(iov, M_SOCKET);
80 	if (error != 0)
81 		return (error);
82 
83 	ro.ro_datalen = rdatalen;
84 	ro.ro_fdslen = rfdslen;
85 	return (copyout(&ro, uap->out, sizeof(ro)));
86 }
87 
88 int
cloudabi32_sys_sock_send(struct thread * td,struct cloudabi32_sys_sock_send_args * uap)89 cloudabi32_sys_sock_send(struct thread *td,
90     struct cloudabi32_sys_sock_send_args *uap)
91 {
92 	cloudabi32_send_in_t si;
93 	cloudabi32_send_out_t so = {};
94 	cloudabi32_ciovec_t iovobj;
95 	struct iovec *iov;
96 	const cloudabi32_ciovec_t *user_iov;
97 	size_t datalen, i;
98 	int error;
99 
100 	error = copyin(uap->in, &si, sizeof(si));
101 	if (error != 0)
102 		return (error);
103 
104 	/* Convert iovecs to native format. */
105 	if (si.si_data_len > UIO_MAXIOV)
106 		return (EINVAL);
107 	iov = mallocarray(si.si_data_len, sizeof(struct iovec),
108 	    M_SOCKET, M_WAITOK);
109 	user_iov = TO_PTR(si.si_data);
110 	for (i = 0; i < si.si_data_len; i++) {
111 		error = copyin(&user_iov[i], &iovobj, sizeof(iovobj));
112 		if (error != 0) {
113 			free(iov, M_SOCKET);
114 			return (error);
115 		}
116 		iov[i].iov_base = TO_PTR(iovobj.buf);
117 		iov[i].iov_len = iovobj.buf_len;
118 	}
119 
120 	error = cloudabi_sock_send(td, uap->sock, iov, si.si_data_len,
121 	    TO_PTR(si.si_fds), si.si_fds_len, &datalen);
122 	free(iov, M_SOCKET);
123 	if (error != 0)
124 		return (error);
125 
126 	so.so_datalen = datalen;
127 	return (copyout(&so, uap->out, sizeof(so)));
128 }
129