[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>
|
---|
| 45 | #include <sys/types.h>
|
---|
[13be2583] | 46 | #include <types/inetping.h>
|
---|
[6428115] | 47 | #include "icmp.h"
|
---|
[9749e47] | 48 | #include "icmpv6.h"
|
---|
[6428115] | 49 | #include "icmp_std.h"
|
---|
[b4ec1ea] | 50 | #include "inetsrv.h"
|
---|
[6428115] | 51 | #include "inetping.h"
|
---|
| 52 |
|
---|
| 53 | static FIBRIL_MUTEX_INITIALIZE(client_list_lock);
|
---|
| 54 | static LIST_INITIALIZE(client_list);
|
---|
| 55 |
|
---|
| 56 | /** Last used session identifier. Protected by @c client_list_lock */
|
---|
| 57 | static uint16_t inetping_ident = 0;
|
---|
| 58 |
|
---|
| 59 | static int inetping_send(inetping_client_t *client, inetping_sdu_t *sdu)
|
---|
| 60 | {
|
---|
[9749e47] | 61 | if (sdu->src.version != sdu->dest.version)
|
---|
| 62 | return EINVAL;
|
---|
| 63 |
|
---|
| 64 | switch (sdu->src.version) {
|
---|
| 65 | case ip_v4:
|
---|
| 66 | return icmp_ping_send(client->ident, sdu);
|
---|
| 67 | case ip_v6:
|
---|
| 68 | return icmpv6_ping_send(client->ident, sdu);
|
---|
| 69 | default:
|
---|
| 70 | return EINVAL;
|
---|
| 71 | }
|
---|
[6428115] | 72 | }
|
---|
| 73 |
|
---|
[9749e47] | 74 | static int inetping_get_srcaddr(inetping_client_t *client,
|
---|
| 75 | inet_addr_t *remote, inet_addr_t *local)
|
---|
[6428115] | 76 | {
|
---|
[9749e47] | 77 | return inet_get_srcaddr(remote, ICMP_TOS, local);
|
---|
[6428115] | 78 | }
|
---|
| 79 |
|
---|
[02a09ed] | 80 | static inetping_client_t *inetping_client_find(uint16_t ident)
|
---|
[6428115] | 81 | {
|
---|
[02a09ed] | 82 | fibril_mutex_lock(&client_list_lock);
|
---|
[9749e47] | 83 |
|
---|
[feeac0d] | 84 | list_foreach(client_list, client_list, inetping_client_t, client) {
|
---|
[02a09ed] | 85 | if (client->ident == ident) {
|
---|
| 86 | fibril_mutex_unlock(&client_list_lock);
|
---|
| 87 | return client;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
[9749e47] | 90 |
|
---|
[02a09ed] | 91 | fibril_mutex_unlock(&client_list_lock);
|
---|
| 92 | return NULL;
|
---|
| 93 | }
|
---|
[6428115] | 94 |
|
---|
[02a09ed] | 95 | int inetping_recv(uint16_t ident, inetping_sdu_t *sdu)
|
---|
| 96 | {
|
---|
| 97 | inetping_client_t *client = inetping_client_find(ident);
|
---|
[6428115] | 98 | if (client == NULL) {
|
---|
[a1a101d] | 99 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Unknown ICMP ident. Dropping.");
|
---|
[6428115] | 100 | return ENOENT;
|
---|
| 101 | }
|
---|
[9749e47] | 102 |
|
---|
[02a09ed] | 103 | async_exch_t *exch = async_exchange_begin(client->sess);
|
---|
[9749e47] | 104 |
|
---|
[02a09ed] | 105 | ipc_call_t answer;
|
---|
[9749e47] | 106 | aid_t req = async_send_1(exch, INETPING_EV_RECV, sdu->seq_no, &answer);
|
---|
| 107 |
|
---|
| 108 | int rc = async_data_write_start(exch, &sdu->src, sizeof(sdu->src));
|
---|
| 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(sdu->dest));
|
---|
| 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 |
|
---|
[6428115] | 124 | async_exchange_end(exch);
|
---|
[9749e47] | 125 |
|
---|
[6428115] | 126 | if (rc != EOK) {
|
---|
[50b581d] | 127 | async_forget(req);
|
---|
[6428115] | 128 | return rc;
|
---|
| 129 | }
|
---|
[9749e47] | 130 |
|
---|
[6428115] | 131 | sysarg_t retval;
|
---|
| 132 | async_wait_for(req, &retval);
|
---|
[9749e47] | 133 |
|
---|
[257feec] | 134 | return (int) retval;
|
---|
[6428115] | 135 | }
|
---|
| 136 |
|
---|
[9749e47] | 137 | static void inetping_send_srv(inetping_client_t *client, ipc_callid_t iid,
|
---|
| 138 | ipc_call_t *icall)
|
---|
[6428115] | 139 | {
|
---|
[9749e47] | 140 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping_send_srv()");
|
---|
| 141 |
|
---|
[6428115] | 142 | inetping_sdu_t sdu;
|
---|
| 143 | int rc;
|
---|
| 144 |
|
---|
[9749e47] | 145 | sdu.seq_no = IPC_GET_ARG1(*icall);
|
---|
[6428115] | 146 |
|
---|
[9749e47] | 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(sdu.src)) {
|
---|
| 156 | async_answer_0(callid, EINVAL);
|
---|
| 157 | async_answer_0(iid, EINVAL);
|
---|
| 158 | return;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | rc = async_data_write_finalize(callid, &sdu.src, size);
|
---|
[6428115] | 162 | if (rc != EOK) {
|
---|
| 163 | async_answer_0(callid, rc);
|
---|
[9749e47] | 164 | async_answer_0(iid, rc);
|
---|
[6428115] | 165 | return;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[9749e47] | 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(sdu.dest)) {
|
---|
| 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 | }
|
---|
[6428115] | 193 |
|
---|
| 194 | rc = inetping_send(client, &sdu);
|
---|
| 195 | free(sdu.data);
|
---|
| 196 |
|
---|
[9749e47] | 197 | async_answer_0(iid, rc);
|
---|
[6428115] | 198 | }
|
---|
| 199 |
|
---|
| 200 | static void inetping_get_srcaddr_srv(inetping_client_t *client,
|
---|
[9749e47] | 201 | ipc_callid_t iid, ipc_call_t *icall)
|
---|
[6428115] | 202 | {
|
---|
[a1a101d] | 203 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping_get_srcaddr_srv()");
|
---|
[9749e47] | 204 |
|
---|
| 205 | ipc_callid_t callid;
|
---|
| 206 | size_t size;
|
---|
| 207 |
|
---|
| 208 | inet_addr_t local;
|
---|
| 209 | inet_addr_t remote;
|
---|
| 210 |
|
---|
| 211 | if (!async_data_write_receive(&callid, &size)) {
|
---|
| 212 | async_answer_0(callid, EREFUSED);
|
---|
| 213 | async_answer_0(iid, EREFUSED);
|
---|
| 214 | return;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | if (size != sizeof(remote)) {
|
---|
| 218 | async_answer_0(callid, EINVAL);
|
---|
| 219 | async_answer_0(iid, EINVAL);
|
---|
| 220 | return;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | int rc = async_data_write_finalize(callid, &remote, size);
|
---|
| 224 | if (rc != EOK) {
|
---|
| 225 | async_answer_0(callid, rc);
|
---|
| 226 | async_answer_0(iid, rc);
|
---|
| 227 | return;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | rc = inetping_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(local)) {
|
---|
| 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);
|
---|
[6428115] | 253 | }
|
---|
| 254 |
|
---|
| 255 | static int inetping_client_init(inetping_client_t *client)
|
---|
| 256 | {
|
---|
| 257 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
| 258 | if (sess == NULL)
|
---|
| 259 | return ENOMEM;
|
---|
[9749e47] | 260 |
|
---|
[6428115] | 261 | client->sess = sess;
|
---|
| 262 | link_initialize(&client->client_list);
|
---|
[9749e47] | 263 |
|
---|
[6428115] | 264 | fibril_mutex_lock(&client_list_lock);
|
---|
| 265 | client->ident = ++inetping_ident;
|
---|
| 266 | list_append(&client->client_list, &client_list);
|
---|
| 267 | fibril_mutex_unlock(&client_list_lock);
|
---|
[9749e47] | 268 |
|
---|
[6428115] | 269 | return EOK;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | static void inetping_client_fini(inetping_client_t *client)
|
---|
| 273 | {
|
---|
| 274 | async_hangup(client->sess);
|
---|
| 275 | client->sess = NULL;
|
---|
[9749e47] | 276 |
|
---|
[6428115] | 277 | fibril_mutex_lock(&client_list_lock);
|
---|
| 278 | list_remove(&client->client_list);
|
---|
| 279 | fibril_mutex_unlock(&client_list_lock);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | void inetping_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 283 | {
|
---|
[a1a101d] | 284 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping_conn()");
|
---|
[9749e47] | 285 |
|
---|
[6428115] | 286 | /* Accept the connection */
|
---|
| 287 | async_answer_0(iid, EOK);
|
---|
[9749e47] | 288 |
|
---|
[02a09ed] | 289 | inetping_client_t client;
|
---|
| 290 | int rc = inetping_client_init(&client);
|
---|
[6428115] | 291 | if (rc != EOK)
|
---|
| 292 | return;
|
---|
[9749e47] | 293 |
|
---|
[6428115] | 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);
|
---|
[9749e47] | 298 |
|
---|
[6428115] | 299 | if (!method) {
|
---|
| 300 | /* The other side has hung up */
|
---|
| 301 | async_answer_0(callid, EOK);
|
---|
| 302 | break;
|
---|
| 303 | }
|
---|
[9749e47] | 304 |
|
---|
[6428115] | 305 | switch (method) {
|
---|
| 306 | case INETPING_SEND:
|
---|
| 307 | inetping_send_srv(&client, callid, &call);
|
---|
| 308 | break;
|
---|
| 309 | case INETPING_GET_SRCADDR:
|
---|
| 310 | inetping_get_srcaddr_srv(&client, callid, &call);
|
---|
| 311 | break;
|
---|
| 312 | default:
|
---|
| 313 | async_answer_0(callid, EINVAL);
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
[9749e47] | 316 |
|
---|
[6428115] | 317 | inetping_client_fini(&client);
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | /** @}
|
---|
| 321 | */
|
---|