1 | /*
|
---|
2 | * Copyright (c) 2013 Martin Decky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup inet
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <async.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <fibril_synch.h>
|
---|
40 | #include <io/log.h>
|
---|
41 | #include <ipc/inet.h>
|
---|
42 | #include <loc.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <sys/types.h>
|
---|
45 | #include <types/inetping6.h>
|
---|
46 | #include <net/socket_codes.h>
|
---|
47 | #include "icmpv6.h"
|
---|
48 | #include "icmpv6_std.h"
|
---|
49 | #include "inetsrv.h"
|
---|
50 | #include "inetping6.h"
|
---|
51 |
|
---|
52 | static FIBRIL_MUTEX_INITIALIZE(client_list_lock);
|
---|
53 | static LIST_INITIALIZE(client_list);
|
---|
54 |
|
---|
55 | /** Last used session identifier. Protected by @c client_list_lock */
|
---|
56 | static uint16_t inetping6_ident = 0;
|
---|
57 |
|
---|
58 | static int inetping6_send(inetping6_client_t *client, inetping6_sdu_t *sdu)
|
---|
59 | {
|
---|
60 | return icmpv6_ping_send(client->ident, sdu);
|
---|
61 | }
|
---|
62 |
|
---|
63 | static int inetping6_get_srcaddr(inetping6_client_t *client, addr128_t remote,
|
---|
64 | addr128_t *local)
|
---|
65 | {
|
---|
66 | inet_addr_t remote_addr;
|
---|
67 | inet_addr_set6(remote, &remote_addr);
|
---|
68 |
|
---|
69 | inet_addr_t local_addr;
|
---|
70 | int rc = inet_get_srcaddr(&remote_addr, ICMPV6_TOS, &local_addr);
|
---|
71 | if (rc != EOK)
|
---|
72 | return rc;
|
---|
73 |
|
---|
74 | uint16_t family = inet_addr_get(&local_addr, NULL, local);
|
---|
75 | if (family != AF_INET6)
|
---|
76 | return EINVAL;
|
---|
77 |
|
---|
78 | return EOK;
|
---|
79 | }
|
---|
80 |
|
---|
81 | static inetping6_client_t *inetping6_client_find(uint16_t ident)
|
---|
82 | {
|
---|
83 | fibril_mutex_lock(&client_list_lock);
|
---|
84 |
|
---|
85 | list_foreach(client_list, client_list, inetping6_client_t, client) {
|
---|
86 | if (client->ident == ident) {
|
---|
87 | fibril_mutex_unlock(&client_list_lock);
|
---|
88 | return client;
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | fibril_mutex_unlock(&client_list_lock);
|
---|
93 | return NULL;
|
---|
94 | }
|
---|
95 |
|
---|
96 | int inetping6_recv(uint16_t ident, inetping6_sdu_t *sdu)
|
---|
97 | {
|
---|
98 | inetping6_client_t *client = inetping6_client_find(ident);
|
---|
99 | if (client == NULL) {
|
---|
100 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Unknown ICMP ident. Dropping.");
|
---|
101 | return ENOENT;
|
---|
102 | }
|
---|
103 |
|
---|
104 | async_exch_t *exch = async_exchange_begin(client->sess);
|
---|
105 |
|
---|
106 | ipc_call_t answer;
|
---|
107 | aid_t req = async_send_1(exch, INETPING6_EV_RECV, sdu->seq_no, &answer);
|
---|
108 |
|
---|
109 | int rc = async_data_write_start(exch, sdu->src, sizeof(addr128_t));
|
---|
110 | if (rc != EOK) {
|
---|
111 | async_exchange_end(exch);
|
---|
112 | async_forget(req);
|
---|
113 | return rc;
|
---|
114 | }
|
---|
115 |
|
---|
116 | rc = async_data_write_start(exch, sdu->dest, sizeof(addr128_t));
|
---|
117 | if (rc != EOK) {
|
---|
118 | async_exchange_end(exch);
|
---|
119 | async_forget(req);
|
---|
120 | return rc;
|
---|
121 | }
|
---|
122 |
|
---|
123 | rc = async_data_write_start(exch, sdu->data, sdu->size);
|
---|
124 |
|
---|
125 | async_exchange_end(exch);
|
---|
126 |
|
---|
127 | if (rc != EOK) {
|
---|
128 | async_forget(req);
|
---|
129 | return rc;
|
---|
130 | }
|
---|
131 |
|
---|
132 | sysarg_t retval;
|
---|
133 | async_wait_for(req, &retval);
|
---|
134 |
|
---|
135 | return (int) retval;
|
---|
136 | }
|
---|
137 |
|
---|
138 | static void inetping6_send_srv(inetping6_client_t *client, ipc_callid_t iid,
|
---|
139 | ipc_call_t *icall)
|
---|
140 | {
|
---|
141 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping6_send_srv()");
|
---|
142 |
|
---|
143 | inetping6_sdu_t sdu;
|
---|
144 |
|
---|
145 | sdu.seq_no = IPC_GET_ARG1(*icall);
|
---|
146 |
|
---|
147 | ipc_callid_t callid;
|
---|
148 | size_t size;
|
---|
149 | if (!async_data_write_receive(&callid, &size)) {
|
---|
150 | async_answer_0(callid, EREFUSED);
|
---|
151 | async_answer_0(iid, EREFUSED);
|
---|
152 | return;
|
---|
153 | }
|
---|
154 |
|
---|
155 | if (size != sizeof(addr128_t)) {
|
---|
156 | async_answer_0(callid, EINVAL);
|
---|
157 | async_answer_0(iid, EINVAL);
|
---|
158 | return;
|
---|
159 | }
|
---|
160 |
|
---|
161 | int rc = async_data_write_finalize(callid, &sdu.src, size);
|
---|
162 | if (rc != EOK) {
|
---|
163 | async_answer_0(callid, rc);
|
---|
164 | async_answer_0(iid, rc);
|
---|
165 | return;
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (!async_data_write_receive(&callid, &size)) {
|
---|
169 | async_answer_0(callid, EREFUSED);
|
---|
170 | async_answer_0(iid, EREFUSED);
|
---|
171 | return;
|
---|
172 | }
|
---|
173 |
|
---|
174 | if (size != sizeof(addr128_t)) {
|
---|
175 | async_answer_0(callid, EINVAL);
|
---|
176 | async_answer_0(iid, EINVAL);
|
---|
177 | return;
|
---|
178 | }
|
---|
179 |
|
---|
180 | rc = async_data_write_finalize(callid, &sdu.dest, size);
|
---|
181 | if (rc != EOK) {
|
---|
182 | async_answer_0(callid, rc);
|
---|
183 | async_answer_0(iid, rc);
|
---|
184 | return;
|
---|
185 | }
|
---|
186 |
|
---|
187 | rc = async_data_write_accept((void **) &sdu.data, false, 0, 0, 0,
|
---|
188 | &sdu.size);
|
---|
189 | if (rc != EOK) {
|
---|
190 | async_answer_0(iid, rc);
|
---|
191 | return;
|
---|
192 | }
|
---|
193 |
|
---|
194 | rc = inetping6_send(client, &sdu);
|
---|
195 | free(sdu.data);
|
---|
196 |
|
---|
197 | async_answer_0(iid, rc);
|
---|
198 | }
|
---|
199 |
|
---|
200 | static void inetping6_get_srcaddr_srv(inetping6_client_t *client,
|
---|
201 | ipc_callid_t iid, ipc_call_t *icall)
|
---|
202 | {
|
---|
203 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping6_get_srcaddr_srv()");
|
---|
204 |
|
---|
205 | ipc_callid_t callid;
|
---|
206 | size_t size;
|
---|
207 | if (!async_data_write_receive(&callid, &size)) {
|
---|
208 | async_answer_0(callid, EREFUSED);
|
---|
209 | async_answer_0(iid, EREFUSED);
|
---|
210 | return;
|
---|
211 | }
|
---|
212 |
|
---|
213 | if (size != sizeof(addr128_t)) {
|
---|
214 | async_answer_0(callid, EINVAL);
|
---|
215 | async_answer_0(iid, EINVAL);
|
---|
216 | return;
|
---|
217 | }
|
---|
218 |
|
---|
219 | addr128_t remote;
|
---|
220 | int rc = async_data_write_finalize(callid, &remote, size);
|
---|
221 | if (rc != EOK) {
|
---|
222 | async_answer_0(callid, rc);
|
---|
223 | async_answer_0(iid, rc);
|
---|
224 | return;
|
---|
225 | }
|
---|
226 |
|
---|
227 | addr128_t local;
|
---|
228 | rc = inetping6_get_srcaddr(client, remote, &local);
|
---|
229 | if (rc != EOK) {
|
---|
230 | async_answer_0(iid, rc);
|
---|
231 | return;
|
---|
232 | }
|
---|
233 |
|
---|
234 | if (!async_data_read_receive(&callid, &size)) {
|
---|
235 | async_answer_0(callid, EREFUSED);
|
---|
236 | async_answer_0(iid, EREFUSED);
|
---|
237 | return;
|
---|
238 | }
|
---|
239 |
|
---|
240 | if (size != sizeof(addr128_t)) {
|
---|
241 | async_answer_0(callid, EINVAL);
|
---|
242 | async_answer_0(iid, EINVAL);
|
---|
243 | return;
|
---|
244 | }
|
---|
245 |
|
---|
246 | rc = async_data_read_finalize(callid, &local, size);
|
---|
247 | if (rc != EOK)
|
---|
248 | async_answer_0(callid, rc);
|
---|
249 |
|
---|
250 | async_answer_0(iid, rc);
|
---|
251 | }
|
---|
252 |
|
---|
253 | static int inetping6_client_init(inetping6_client_t *client)
|
---|
254 | {
|
---|
255 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
256 | if (sess == NULL)
|
---|
257 | return ENOMEM;
|
---|
258 |
|
---|
259 | client->sess = sess;
|
---|
260 | link_initialize(&client->client_list);
|
---|
261 |
|
---|
262 | fibril_mutex_lock(&client_list_lock);
|
---|
263 | client->ident = ++inetping6_ident;
|
---|
264 | list_append(&client->client_list, &client_list);
|
---|
265 | fibril_mutex_unlock(&client_list_lock);
|
---|
266 |
|
---|
267 | return EOK;
|
---|
268 | }
|
---|
269 |
|
---|
270 | static void inetping6_client_fini(inetping6_client_t *client)
|
---|
271 | {
|
---|
272 | async_hangup(client->sess);
|
---|
273 | client->sess = NULL;
|
---|
274 |
|
---|
275 | fibril_mutex_lock(&client_list_lock);
|
---|
276 | list_remove(&client->client_list);
|
---|
277 | fibril_mutex_unlock(&client_list_lock);
|
---|
278 | }
|
---|
279 |
|
---|
280 | void inetping6_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
281 | {
|
---|
282 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping6_conn()");
|
---|
283 |
|
---|
284 | /* Accept the connection */
|
---|
285 | async_answer_0(iid, EOK);
|
---|
286 |
|
---|
287 | inetping6_client_t client;
|
---|
288 | int rc = inetping6_client_init(&client);
|
---|
289 | if (rc != EOK)
|
---|
290 | return;
|
---|
291 |
|
---|
292 | while (true) {
|
---|
293 | ipc_call_t call;
|
---|
294 | ipc_callid_t callid = async_get_call(&call);
|
---|
295 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
296 |
|
---|
297 | if (!method) {
|
---|
298 | /* The other side has hung up */
|
---|
299 | async_answer_0(callid, EOK);
|
---|
300 | break;
|
---|
301 | }
|
---|
302 |
|
---|
303 | switch (method) {
|
---|
304 | case INETPING6_SEND:
|
---|
305 | inetping6_send_srv(&client, callid, &call);
|
---|
306 | break;
|
---|
307 | case INETPING6_GET_SRCADDR:
|
---|
308 | inetping6_get_srcaddr_srv(&client, callid, &call);
|
---|
309 | break;
|
---|
310 | default:
|
---|
311 | async_answer_0(callid, EINVAL);
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | inetping6_client_fini(&client);
|
---|
316 | }
|
---|
317 |
|
---|
318 | /** @}
|
---|
319 | */
|
---|