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 <contrib/cloudabi/cloudabi32_types.h>
39 
40 #include <compat/cloudabi/cloudabi_util.h>
41 
42 #include <compat/cloudabi32/cloudabi32_proto.h>
43 #include <compat/cloudabi32/cloudabi32_util.h>
44 
45 static MALLOC_DEFINE(M_SOCKET, "socket", "CloudABI socket");
46 
47 int
48 cloudabi32_sys_sock_recv(struct thread *td,
49     struct cloudabi32_sys_sock_recv_args *uap)
50 {
51 	struct sockaddr_storage ss;
52 	cloudabi32_recv_in_t ri;
53 	cloudabi32_recv_out_t ro = {};
54 	cloudabi32_iovec_t iovobj;
55 	struct msghdr msghdr = {};
56 	const cloudabi32_iovec_t *user_iov;
57 	size_t i;
58 	int error;
59 
60 	error = copyin(uap->in, &ri, sizeof(ri));
61 	if (error != 0)
62 		return (error);
63 
64 	/* Convert results in cloudabi_recv_in_t to struct msghdr. */
65 	if (ri.ri_datalen > UIO_MAXIOV)
66 		return (EINVAL);
67 	msghdr.msg_iovlen = ri.ri_datalen;
68 	msghdr.msg_iov = malloc(msghdr.msg_iovlen * sizeof(struct iovec),
69 	    M_SOCKET, M_WAITOK);
70 	user_iov = TO_PTR(ri.ri_data);
71 	for (i = 0; i < msghdr.msg_iovlen; i++) {
72 		error = copyin(&user_iov[i], &iovobj, sizeof(iovobj));
73 		if (error != 0) {
74 			free(msghdr.msg_iov, M_SOCKET);
75 			return (error);
76 		}
77 		msghdr.msg_iov[i].iov_base = TO_PTR(iovobj.iov_base);
78 		msghdr.msg_iov[i].iov_len = iovobj.iov_len;
79 	}
80 	msghdr.msg_name = &ss;
81 	msghdr.msg_namelen = sizeof(ss);
82 	if (ri.ri_flags & CLOUDABI_MSG_PEEK)
83 		msghdr.msg_flags |= MSG_PEEK;
84 	if (ri.ri_flags & CLOUDABI_MSG_WAITALL)
85 		msghdr.msg_flags |= MSG_WAITALL;
86 
87 	/* TODO(ed): Add file descriptor passing. */
88 	error = kern_recvit(td, uap->sock, &msghdr, UIO_SYSSPACE, NULL);
89 	free(msghdr.msg_iov, M_SOCKET);
90 	if (error != 0)
91 		return (error);
92 
93 	/* Convert results in msghdr to cloudabi_recv_out_t. */
94 	ro.ro_datalen = td->td_retval[0];
95 	cloudabi_convert_sockaddr((struct sockaddr *)&ss,
96 	    MIN(msghdr.msg_namelen, sizeof(ss)), &ro.ro_peername);
97 	td->td_retval[0] = 0;
98 	return (copyout(&ro, uap->out, sizeof(ro)));
99 }
100 
101 int
102 cloudabi32_sys_sock_send(struct thread *td,
103     struct cloudabi32_sys_sock_send_args *uap)
104 {
105 	cloudabi32_send_in_t si;
106 	cloudabi32_send_out_t so = {};
107 	cloudabi32_ciovec_t iovobj;
108 	struct msghdr msghdr = {};
109 	const cloudabi32_ciovec_t *user_iov;
110 	size_t i;
111 	int error, flags;
112 
113 	error = copyin(uap->in, &si, sizeof(si));
114 	if (error != 0)
115 		return (error);
116 
117 	/* Convert results in cloudabi_send_in_t to struct msghdr. */
118 	if (si.si_datalen > UIO_MAXIOV)
119 		return (EINVAL);
120 	msghdr.msg_iovlen = si.si_datalen;
121 	msghdr.msg_iov = malloc(msghdr.msg_iovlen * sizeof(struct iovec),
122 	    M_SOCKET, M_WAITOK);
123 	user_iov = TO_PTR(si.si_data);
124 	for (i = 0; i < msghdr.msg_iovlen; i++) {
125 		error = copyin(&user_iov[i], &iovobj, sizeof(iovobj));
126 		if (error != 0) {
127 			free(msghdr.msg_iov, M_SOCKET);
128 			return (error);
129 		}
130 		msghdr.msg_iov[i].iov_base = TO_PTR(iovobj.iov_base);
131 		msghdr.msg_iov[i].iov_len = iovobj.iov_len;
132 	}
133 
134 	flags = MSG_NOSIGNAL;
135 	if (si.si_flags & CLOUDABI_MSG_EOR)
136 		flags |= MSG_EOR;
137 
138 	/* TODO(ed): Add file descriptor passing. */
139 	error = kern_sendit(td, uap->sock, &msghdr, flags, NULL, UIO_USERSPACE);
140 	free(msghdr.msg_iov, M_SOCKET);
141 	if (error != 0)
142 		return (error);
143 
144 	/* Convert results in msghdr to cloudabi_send_out_t. */
145 	so.so_datalen = td->td_retval[0];
146 	td->td_retval[0] = 0;
147 	return (copyout(&so, uap->out, sizeof(so)));
148 }
149