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 <net/socket_codes.h>
|
---|
46 | #include "icmpv6.h"
|
---|
47 | #include "icmpv6_std.h"
|
---|
48 | #include "inetsrv.h"
|
---|
49 | #include "inetping6.h"
|
---|
50 |
|
---|
51 | static FIBRIL_MUTEX_INITIALIZE(client_list_lock);
|
---|
52 | static LIST_INITIALIZE(client_list);
|
---|
53 |
|
---|
54 | /** Last used session identifier. Protected by @c client_list_lock */
|
---|
55 | static uint16_t inetping6_ident = 0;
|
---|
56 |
|
---|
57 | static int inetping6_send(inetping6_client_t *client, inetping6_sdu_t *sdu)
|
---|
58 | {
|
---|
59 | return icmpv6_ping_send(client->ident, sdu);
|
---|
60 | }
|
---|
61 |
|
---|
62 | static int inetping6_get_srcaddr(inetping6_client_t *client, addr128_t remote,
|
---|
63 | addr128_t *local)
|
---|
64 | {
|
---|
65 | inet_addr_t remote_addr;
|
---|
66 | inet_addr_set6(remote, &remote_addr);
|
---|
67 |
|
---|
68 | inet_addr_t local_addr;
|
---|
69 | int rc = inet_get_srcaddr(&remote_addr, ICMPV6_TOS, &local_addr);
|
---|
70 | if (rc != EOK)
|
---|
71 | return rc;
|
---|
72 |
|
---|
73 | uint16_t family = inet_addr_get(&local_addr, NULL, local);
|
---|
74 | if (family != AF_INET6)
|
---|
75 | return EINVAL;
|
---|
76 |
|
---|
77 | return EOK;
|
---|
78 | }
|
---|
79 |
|
---|
80 | static inetping6_client_t *inetping6_client_find(uint16_t ident)
|
---|
81 | {
|
---|
82 | fibril_mutex_lock(&client_list_lock);
|
---|
83 |
|
---|
84 | list_foreach(client_list, client_list, inetping6_client_t, client) {
|
---|
85 | if (client->ident == ident) {
|
---|
86 | fibril_mutex_unlock(&client_list_lock);
|
---|
87 | return client;
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | fibril_mutex_unlock(&client_list_lock);
|
---|
92 | return NULL;
|
---|
93 | }
|
---|
94 |
|
---|
95 | int inetping6_recv(uint16_t ident, inetping6_sdu_t *sdu)
|
---|
96 | {
|
---|
97 | inetping6_client_t *client = inetping6_client_find(ident);
|
---|
98 | if (client == NULL) {
|
---|
99 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Unknown ICMP ident. Dropping.");
|
---|
100 | return ENOENT;
|
---|
101 | }
|
---|
102 |
|
---|
103 | async_exch_t *exch = async_exchange_begin(client->sess);
|
---|
104 |
|
---|
105 | ipc_call_t answer;
|
---|
106 | aid_t req = async_send_1(exch, INETPING6_EV_RECV, sdu->seq_no, &answer);
|
---|
107 |
|
---|
108 | int rc = async_data_write_start(exch, sdu->src, sizeof(addr128_t));
|
---|
109 | if (rc != EOK) {
|
---|
110 | async_exchange_end(exch);
|
---|
111 | async_forget(req);
|
---|
112 | return rc;
|
---|
113 | }
|
---|
114 |
|
---|
115 | rc = async_data_write_start(exch, sdu->dest, sizeof(addr128_t));
|
---|
116 | if (rc != EOK) {
|
---|
117 | async_exchange_end(exch);
|
---|
118 | async_forget(req);
|
---|
119 | return rc;
|
---|
120 | }
|
---|
121 |
|
---|
122 | rc = async_data_write_start(exch, sdu->data, sdu->size);
|
---|
123 |
|
---|
124 | async_exchange_end(exch);
|
---|
125 |
|
---|
126 | if (rc != EOK) {
|
---|
127 | async_forget(req);
|
---|
128 | return rc;
|
---|
129 | }
|
---|
130 |
|
---|
131 | sysarg_t retval;
|
---|
132 | async_wait_for(req, &retval);
|
---|
133 |
|
---|
134 | return (int) retval;
|
---|
135 | }
|
---|
136 |
|
---|
137 | static void inetping6_send_srv(inetping6_client_t *client, ipc_callid_t iid,
|
---|
138 | ipc_call_t *icall)
|
---|
139 | {
|
---|
140 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping6_send_srv()");
|
---|
141 |
|
---|
142 | inetping6_sdu_t sdu;
|
---|
143 |
|
---|
144 | sdu.seq_no = IPC_GET_ARG1(*icall);
|
---|
145 |
|
---|
146 | ipc_callid_t callid;
|
---|
147 | size_t size;
|
---|
148 | if (!async_data_write_receive(&callid, &size)) {
|
---|
149 | async_answer_0(callid, EREFUSED);
|
---|
150 | async_answer_0(iid, EREFUSED);
|
---|
151 | return;
|
---|
152 | }
|
---|
153 |
|
---|
154 | if (size != sizeof(addr128_t)) {
|
---|
155 | async_answer_0(callid, EINVAL);
|
---|
156 | async_answer_0(iid, EINVAL);
|
---|
157 | return;
|
---|
158 | }
|
---|
159 |
|
---|
160 | int rc = async_data_write_finalize(callid, &sdu.src, size);
|
---|
161 | if (rc != EOK) {
|
---|
162 | async_answer_0(callid, rc);
|
---|
163 | async_answer_0(iid, rc);
|
---|
164 | return;
|
---|
165 | }
|
---|
166 |
|
---|
167 | if (!async_data_write_receive(&callid, &size)) {
|
---|
168 | async_answer_0(callid, EREFUSED);
|
---|
169 | async_answer_0(iid, EREFUSED);
|
---|
170 | return;
|
---|
171 | }
|
---|
172 |
|
---|
173 | if (size != sizeof(addr128_t)) {
|
---|
174 | async_answer_0(callid, EINVAL);
|
---|
175 | async_answer_0(iid, EINVAL);
|
---|
176 | return;
|
---|
177 | }
|
---|
178 |
|
---|
179 | rc = async_data_write_finalize(callid, &sdu.dest, size);
|
---|
180 | if (rc != EOK) {
|
---|
181 | async_answer_0(callid, rc);
|
---|
182 | async_answer_0(iid, rc);
|
---|
183 | return;
|
---|
184 | }
|
---|
185 |
|
---|
186 | rc = async_data_write_accept((void **) &sdu.data, false, 0, 0, 0,
|
---|
187 | &sdu.size);
|
---|
188 | if (rc != EOK) {
|
---|
189 | async_answer_0(iid, rc);
|
---|
190 | return;
|
---|
191 | }
|
---|
192 |
|
---|
193 | rc = inetping6_send(client, &sdu);
|
---|
194 | free(sdu.data);
|
---|
195 |
|
---|
196 | async_answer_0(iid, rc);
|
---|
197 | }
|
---|
198 |
|
---|
199 | static void inetping6_get_srcaddr_srv(inetping6_client_t *client,
|
---|
200 | ipc_callid_t iid, ipc_call_t *icall)
|
---|
201 | {
|
---|
202 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping6_get_srcaddr_srv()");
|
---|
203 |
|
---|
204 | ipc_callid_t callid;
|
---|
205 | size_t size;
|
---|
206 | if (!async_data_write_receive(&callid, &size)) {
|
---|
207 | async_answer_0(callid, EREFUSED);
|
---|
208 | async_answer_0(iid, EREFUSED);
|
---|
209 | return;
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (size != sizeof(addr128_t)) {
|
---|
213 | async_answer_0(callid, EINVAL);
|
---|
214 | async_answer_0(iid, EINVAL);
|
---|
215 | return;
|
---|
216 | }
|
---|
217 |
|
---|
218 | addr128_t remote;
|
---|
219 | int rc = async_data_write_finalize(callid, &remote, size);
|
---|
220 | if (rc != EOK) {
|
---|
221 | async_answer_0(callid, rc);
|
---|
222 | async_answer_0(iid, rc);
|
---|
223 | return;
|
---|
224 | }
|
---|
225 |
|
---|
226 | addr128_t local;
|
---|
227 | rc = inetping6_get_srcaddr(client, remote, &local);
|
---|
228 | if (rc != EOK) {
|
---|
229 | async_answer_0(iid, rc);
|
---|
230 | return;
|
---|
231 | }
|
---|
232 |
|
---|
233 | if (!async_data_read_receive(&callid, &size)) {
|
---|
234 | async_answer_0(callid, EREFUSED);
|
---|
235 | async_answer_0(iid, EREFUSED);
|
---|
236 | return;
|
---|
237 | }
|
---|
238 |
|
---|
239 | if (size != sizeof(addr128_t)) {
|
---|
240 | async_answer_0(callid, EINVAL);
|
---|
241 | async_answer_0(iid, EINVAL);
|
---|
242 | return;
|
---|
243 | }
|
---|
244 |
|
---|
245 | rc = async_data_read_finalize(callid, &local, size);
|
---|
246 | if (rc != EOK)
|
---|
247 | async_answer_0(callid, rc);
|
---|
248 |
|
---|
249 | async_answer_0(iid, rc);
|
---|
250 | }
|
---|
251 |
|
---|
252 | static int inetping6_client_init(inetping6_client_t *client)
|
---|
253 | {
|
---|
254 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
255 | if (sess == NULL)
|
---|
256 | return ENOMEM;
|
---|
257 |
|
---|
258 | client->sess = sess;
|
---|
259 | link_initialize(&client->client_list);
|
---|
260 |
|
---|
261 | fibril_mutex_lock(&client_list_lock);
|
---|
262 | client->ident = ++inetping6_ident;
|
---|
263 | list_append(&client->client_list, &client_list);
|
---|
264 | fibril_mutex_unlock(&client_list_lock);
|
---|
265 |
|
---|
266 | return EOK;
|
---|
267 | }
|
---|
268 |
|
---|
269 | static void inetping6_client_fini(inetping6_client_t *client)
|
---|
270 | {
|
---|
271 | async_hangup(client->sess);
|
---|
272 | client->sess = NULL;
|
---|
273 |
|
---|
274 | fibril_mutex_lock(&client_list_lock);
|
---|
275 | list_remove(&client->client_list);
|
---|
276 | fibril_mutex_unlock(&client_list_lock);
|
---|
277 | }
|
---|
278 |
|
---|
279 | void inetping6_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
280 | {
|
---|
281 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping6_conn()");
|
---|
282 |
|
---|
283 | /* Accept the connection */
|
---|
284 | async_answer_0(iid, EOK);
|
---|
285 |
|
---|
286 | inetping6_client_t client;
|
---|
287 | int rc = inetping6_client_init(&client);
|
---|
288 | if (rc != EOK)
|
---|
289 | return;
|
---|
290 |
|
---|
291 | while (true) {
|
---|
292 | ipc_call_t call;
|
---|
293 | ipc_callid_t callid = async_get_call(&call);
|
---|
294 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
295 |
|
---|
296 | if (!method) {
|
---|
297 | /* The other side has hung up */
|
---|
298 | async_answer_0(callid, EOK);
|
---|
299 | break;
|
---|
300 | }
|
---|
301 |
|
---|
302 | switch (method) {
|
---|
303 | case INETPING6_SEND:
|
---|
304 | inetping6_send_srv(&client, callid, &call);
|
---|
305 | break;
|
---|
306 | case INETPING6_GET_SRCADDR:
|
---|
307 | inetping6_get_srcaddr_srv(&client, callid, &call);
|
---|
308 | break;
|
---|
309 | default:
|
---|
310 | async_answer_0(callid, EINVAL);
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | inetping6_client_fini(&client);
|
---|
315 | }
|
---|
316 |
|
---|
317 | /** @}
|
---|
318 | */
|
---|