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