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