[f834f90d] | 1 | /*
|
---|
[cdd6fc9] | 2 | * Copyright (c) 2023 Jiri Svoboda
|
---|
[f834f90d] | 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 |
|
---|
[edeee9f] | 29 | /** @addtogroup libinet
|
---|
[f834f90d] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * @brief IP link server stub
|
---|
| 35 | */
|
---|
[02a09ed] | 36 |
|
---|
[f834f90d] | 37 | #include <errno.h>
|
---|
[b4edc96] | 38 | #include <inet/eth_addr.h>
|
---|
[f834f90d] | 39 | #include <ipc/iplink.h>
|
---|
| 40 | #include <stdlib.h>
|
---|
[8d2dd7f2] | 41 | #include <stddef.h>
|
---|
[02a09ed] | 42 | #include <inet/addr.h>
|
---|
[f834f90d] | 43 | #include <inet/iplink_srv.h>
|
---|
| 44 |
|
---|
[984a9ba] | 45 | static void iplink_get_mtu_srv(iplink_srv_t *srv, ipc_call_t *call)
|
---|
[f834f90d] | 46 | {
|
---|
| 47 | size_t mtu;
|
---|
[b7fd2a0] | 48 | errno_t rc = srv->ops->get_mtu(srv, &mtu);
|
---|
[984a9ba] | 49 | async_answer_1(call, rc, mtu);
|
---|
[f834f90d] | 50 | }
|
---|
| 51 |
|
---|
[984a9ba] | 52 | static void iplink_get_mac48_srv(iplink_srv_t *srv, ipc_call_t *icall)
|
---|
[a17356fd] | 53 | {
|
---|
[b4edc96] | 54 | eth_addr_t mac;
|
---|
[b7fd2a0] | 55 | errno_t rc = srv->ops->get_mac48(srv, &mac);
|
---|
[a17356fd] | 56 | if (rc != EOK) {
|
---|
[984a9ba] | 57 | async_answer_0(icall, rc);
|
---|
[a17356fd] | 58 | return;
|
---|
| 59 | }
|
---|
[a35b458] | 60 |
|
---|
[984a9ba] | 61 | ipc_call_t call;
|
---|
[a17356fd] | 62 | size_t size;
|
---|
[984a9ba] | 63 | if (!async_data_read_receive(&call, &size)) {
|
---|
| 64 | async_answer_0(&call, EREFUSED);
|
---|
| 65 | async_answer_0(icall, EREFUSED);
|
---|
[a17356fd] | 66 | return;
|
---|
| 67 | }
|
---|
[a35b458] | 68 |
|
---|
[b4edc96] | 69 | if (size != sizeof(eth_addr_t)) {
|
---|
[984a9ba] | 70 | async_answer_0(&call, EINVAL);
|
---|
| 71 | async_answer_0(icall, EINVAL);
|
---|
[a17356fd] | 72 | return;
|
---|
| 73 | }
|
---|
[a35b458] | 74 |
|
---|
[984a9ba] | 75 | rc = async_data_read_finalize(&call, &mac, size);
|
---|
[cdd6fc9] | 76 | if (rc != EOK) {
|
---|
[984a9ba] | 77 | async_answer_0(&call, rc);
|
---|
[cdd6fc9] | 78 | async_answer_0(icall, rc);
|
---|
| 79 | return;
|
---|
| 80 | }
|
---|
[a35b458] | 81 |
|
---|
[984a9ba] | 82 | async_answer_0(icall, rc);
|
---|
[a17356fd] | 83 | }
|
---|
| 84 |
|
---|
[984a9ba] | 85 | static void iplink_set_mac48_srv(iplink_srv_t *srv, ipc_call_t *icall)
|
---|
[c3b25985] | 86 | {
|
---|
[b7fd2a0] | 87 | errno_t rc;
|
---|
[c3b25985] | 88 | size_t size;
|
---|
[b4edc96] | 89 | eth_addr_t mac;
|
---|
[c3b25985] | 90 |
|
---|
[984a9ba] | 91 | ipc_call_t call;
|
---|
| 92 | if (!async_data_write_receive(&call, &size)) {
|
---|
| 93 | async_answer_0(&call, EREFUSED);
|
---|
| 94 | async_answer_0(icall, EREFUSED);
|
---|
[c3b25985] | 95 | }
|
---|
| 96 |
|
---|
| 97 | rc = srv->ops->set_mac48(srv, &mac);
|
---|
| 98 | if (rc != EOK) {
|
---|
[cdd6fc9] | 99 | async_answer_0(&call, rc);
|
---|
[984a9ba] | 100 | async_answer_0(icall, rc);
|
---|
[c3b25985] | 101 | return;
|
---|
| 102 | }
|
---|
[a35b458] | 103 |
|
---|
[b4edc96] | 104 | rc = async_data_read_finalize(&call, &mac, sizeof(eth_addr_t));
|
---|
[cdd6fc9] | 105 | if (rc != EOK) {
|
---|
[984a9ba] | 106 | async_answer_0(&call, rc);
|
---|
[cdd6fc9] | 107 | async_answer_0(icall, rc);
|
---|
| 108 | return;
|
---|
| 109 | }
|
---|
[a35b458] | 110 |
|
---|
[984a9ba] | 111 | async_answer_0(icall, rc);
|
---|
[c3b25985] | 112 | }
|
---|
| 113 |
|
---|
[984a9ba] | 114 | static void iplink_addr_add_srv(iplink_srv_t *srv, ipc_call_t *icall)
|
---|
[962f03b] | 115 | {
|
---|
[984a9ba] | 116 | ipc_call_t call;
|
---|
[02a09ed] | 117 | size_t size;
|
---|
[cdd6fc9] | 118 |
|
---|
[984a9ba] | 119 | if (!async_data_write_receive(&call, &size)) {
|
---|
| 120 | async_answer_0(&call, EREFUSED);
|
---|
| 121 | async_answer_0(icall, EREFUSED);
|
---|
[02a09ed] | 122 | return;
|
---|
| 123 | }
|
---|
[a35b458] | 124 |
|
---|
[02a09ed] | 125 | if (size != sizeof(inet_addr_t)) {
|
---|
[984a9ba] | 126 | async_answer_0(&call, EINVAL);
|
---|
| 127 | async_answer_0(icall, EINVAL);
|
---|
[02a09ed] | 128 | return;
|
---|
| 129 | }
|
---|
[a35b458] | 130 |
|
---|
[02a09ed] | 131 | inet_addr_t addr;
|
---|
[984a9ba] | 132 | errno_t rc = async_data_write_finalize(&call, &addr, size);
|
---|
[02a09ed] | 133 | if (rc != EOK) {
|
---|
[984a9ba] | 134 | async_answer_0(&call, rc);
|
---|
| 135 | async_answer_0(icall, rc);
|
---|
[cdd6fc9] | 136 | return;
|
---|
[02a09ed] | 137 | }
|
---|
[a35b458] | 138 |
|
---|
[02a09ed] | 139 | rc = srv->ops->addr_add(srv, &addr);
|
---|
[984a9ba] | 140 | async_answer_0(icall, rc);
|
---|
[962f03b] | 141 | }
|
---|
| 142 |
|
---|
[984a9ba] | 143 | static void iplink_addr_remove_srv(iplink_srv_t *srv, ipc_call_t *icall)
|
---|
[962f03b] | 144 | {
|
---|
[984a9ba] | 145 | ipc_call_t call;
|
---|
[02a09ed] | 146 | size_t size;
|
---|
[cdd6fc9] | 147 |
|
---|
[984a9ba] | 148 | if (!async_data_write_receive(&call, &size)) {
|
---|
| 149 | async_answer_0(&call, EREFUSED);
|
---|
| 150 | async_answer_0(icall, EREFUSED);
|
---|
[02a09ed] | 151 | return;
|
---|
| 152 | }
|
---|
[a35b458] | 153 |
|
---|
[02a09ed] | 154 | if (size != sizeof(inet_addr_t)) {
|
---|
[984a9ba] | 155 | async_answer_0(&call, EINVAL);
|
---|
| 156 | async_answer_0(icall, EINVAL);
|
---|
[02a09ed] | 157 | return;
|
---|
| 158 | }
|
---|
[a35b458] | 159 |
|
---|
[02a09ed] | 160 | inet_addr_t addr;
|
---|
[984a9ba] | 161 | errno_t rc = async_data_write_finalize(&call, &addr, size);
|
---|
[02a09ed] | 162 | if (rc != EOK) {
|
---|
[984a9ba] | 163 | async_answer_0(&call, rc);
|
---|
| 164 | async_answer_0(icall, rc);
|
---|
[cdd6fc9] | 165 | return;
|
---|
[02a09ed] | 166 | }
|
---|
[a35b458] | 167 |
|
---|
[02a09ed] | 168 | rc = srv->ops->addr_remove(srv, &addr);
|
---|
[984a9ba] | 169 | async_answer_0(icall, rc);
|
---|
[962f03b] | 170 | }
|
---|
| 171 |
|
---|
[984a9ba] | 172 | static void iplink_send_srv(iplink_srv_t *srv, ipc_call_t *icall)
|
---|
[f834f90d] | 173 | {
|
---|
[02a09ed] | 174 | iplink_sdu_t sdu;
|
---|
[a35b458] | 175 |
|
---|
[fafb8e5] | 176 | sdu.src = ipc_get_arg1(icall);
|
---|
| 177 | sdu.dest = ipc_get_arg2(icall);
|
---|
[a35b458] | 178 |
|
---|
[b7fd2a0] | 179 | errno_t rc = async_data_write_accept(&sdu.data, false, 0, 0, 0,
|
---|
[a17356fd] | 180 | &sdu.size);
|
---|
| 181 | if (rc != EOK) {
|
---|
[984a9ba] | 182 | async_answer_0(icall, rc);
|
---|
[02a09ed] | 183 | return;
|
---|
| 184 | }
|
---|
[a35b458] | 185 |
|
---|
[a17356fd] | 186 | rc = srv->ops->send(srv, &sdu);
|
---|
| 187 | free(sdu.data);
|
---|
[984a9ba] | 188 | async_answer_0(icall, rc);
|
---|
[a17356fd] | 189 | }
|
---|
| 190 |
|
---|
[984a9ba] | 191 | static void iplink_send6_srv(iplink_srv_t *srv, ipc_call_t *icall)
|
---|
[a17356fd] | 192 | {
|
---|
| 193 | iplink_sdu6_t sdu;
|
---|
[984a9ba] | 194 | ipc_call_t call;
|
---|
[a17356fd] | 195 | size_t size;
|
---|
[cdd6fc9] | 196 |
|
---|
[984a9ba] | 197 | if (!async_data_write_receive(&call, &size)) {
|
---|
| 198 | async_answer_0(&call, EREFUSED);
|
---|
| 199 | async_answer_0(icall, EREFUSED);
|
---|
[f834f90d] | 200 | return;
|
---|
| 201 | }
|
---|
[a35b458] | 202 |
|
---|
[b4edc96] | 203 | if (size != sizeof(eth_addr_t)) {
|
---|
[984a9ba] | 204 | async_answer_0(&call, EINVAL);
|
---|
| 205 | async_answer_0(icall, EINVAL);
|
---|
[02a09ed] | 206 | return;
|
---|
| 207 | }
|
---|
[a35b458] | 208 |
|
---|
[984a9ba] | 209 | errno_t rc = async_data_write_finalize(&call, &sdu.dest, size);
|
---|
[02a09ed] | 210 | if (rc != EOK) {
|
---|
[984a9ba] | 211 | async_answer_0(&call, rc);
|
---|
| 212 | async_answer_0(icall, rc);
|
---|
[02a09ed] | 213 | }
|
---|
[a35b458] | 214 |
|
---|
[02a09ed] | 215 | rc = async_data_write_accept(&sdu.data, false, 0, 0, 0,
|
---|
| 216 | &sdu.size);
|
---|
[a17356fd] | 217 | if (rc != EOK) {
|
---|
[984a9ba] | 218 | async_answer_0(icall, rc);
|
---|
[02a09ed] | 219 | return;
|
---|
[a17356fd] | 220 | }
|
---|
[a35b458] | 221 |
|
---|
[a17356fd] | 222 | rc = srv->ops->send6(srv, &sdu);
|
---|
[f834f90d] | 223 | free(sdu.data);
|
---|
[984a9ba] | 224 | async_answer_0(icall, rc);
|
---|
[f834f90d] | 225 | }
|
---|
| 226 |
|
---|
[4f64a523] | 227 | void iplink_srv_init(iplink_srv_t *srv)
|
---|
| 228 | {
|
---|
| 229 | fibril_mutex_initialize(&srv->lock);
|
---|
| 230 | srv->connected = false;
|
---|
| 231 | srv->ops = NULL;
|
---|
| 232 | srv->arg = NULL;
|
---|
| 233 | srv->client_sess = NULL;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[984a9ba] | 236 | errno_t iplink_conn(ipc_call_t *icall, void *arg)
|
---|
[f834f90d] | 237 | {
|
---|
[a17356fd] | 238 | iplink_srv_t *srv = (iplink_srv_t *) arg;
|
---|
[b7fd2a0] | 239 | errno_t rc;
|
---|
[a35b458] | 240 |
|
---|
[4f64a523] | 241 | fibril_mutex_lock(&srv->lock);
|
---|
| 242 | if (srv->connected) {
|
---|
| 243 | fibril_mutex_unlock(&srv->lock);
|
---|
[984a9ba] | 244 | async_answer_0(icall, EBUSY);
|
---|
[4f64a523] | 245 | return EBUSY;
|
---|
| 246 | }
|
---|
[a35b458] | 247 |
|
---|
[4f64a523] | 248 | srv->connected = true;
|
---|
| 249 | fibril_mutex_unlock(&srv->lock);
|
---|
[a35b458] | 250 |
|
---|
[f834f90d] | 251 | /* Accept the connection */
|
---|
[beb83c1] | 252 | async_accept_0(icall);
|
---|
[a35b458] | 253 |
|
---|
[f834f90d] | 254 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
| 255 | if (sess == NULL)
|
---|
| 256 | return ENOMEM;
|
---|
[a35b458] | 257 |
|
---|
[4f64a523] | 258 | srv->client_sess = sess;
|
---|
[a35b458] | 259 |
|
---|
[4f64a523] | 260 | rc = srv->ops->open(srv);
|
---|
[f834f90d] | 261 | if (rc != EOK)
|
---|
| 262 | return rc;
|
---|
[a35b458] | 263 |
|
---|
[f834f90d] | 264 | while (true) {
|
---|
| 265 | ipc_call_t call;
|
---|
[984a9ba] | 266 | async_get_call(&call);
|
---|
[fafb8e5] | 267 | sysarg_t method = ipc_get_imethod(&call);
|
---|
[a35b458] | 268 |
|
---|
[f834f90d] | 269 | if (!method) {
|
---|
| 270 | /* The other side has hung up */
|
---|
[a2e3ee6] | 271 | fibril_mutex_lock(&srv->lock);
|
---|
[4802dd7] | 272 | srv->connected = false;
|
---|
[a2e3ee6] | 273 | fibril_mutex_unlock(&srv->lock);
|
---|
[984a9ba] | 274 | async_answer_0(&call, EOK);
|
---|
[f834f90d] | 275 | break;
|
---|
| 276 | }
|
---|
[a35b458] | 277 |
|
---|
[f834f90d] | 278 | switch (method) {
|
---|
| 279 | case IPLINK_GET_MTU:
|
---|
[984a9ba] | 280 | iplink_get_mtu_srv(srv, &call);
|
---|
[f834f90d] | 281 | break;
|
---|
[a17356fd] | 282 | case IPLINK_GET_MAC48:
|
---|
[984a9ba] | 283 | iplink_get_mac48_srv(srv, &call);
|
---|
[a17356fd] | 284 | break;
|
---|
[c3b25985] | 285 | case IPLINK_SET_MAC48:
|
---|
[984a9ba] | 286 | iplink_set_mac48_srv(srv, &call);
|
---|
[c3b25985] | 287 | break;
|
---|
[f834f90d] | 288 | case IPLINK_SEND:
|
---|
[984a9ba] | 289 | iplink_send_srv(srv, &call);
|
---|
[f834f90d] | 290 | break;
|
---|
[a17356fd] | 291 | case IPLINK_SEND6:
|
---|
[984a9ba] | 292 | iplink_send6_srv(srv, &call);
|
---|
[a17356fd] | 293 | break;
|
---|
[962f03b] | 294 | case IPLINK_ADDR_ADD:
|
---|
[984a9ba] | 295 | iplink_addr_add_srv(srv, &call);
|
---|
[962f03b] | 296 | break;
|
---|
| 297 | case IPLINK_ADDR_REMOVE:
|
---|
[984a9ba] | 298 | iplink_addr_remove_srv(srv, &call);
|
---|
[962f03b] | 299 | break;
|
---|
[f834f90d] | 300 | default:
|
---|
[984a9ba] | 301 | async_answer_0(&call, EINVAL);
|
---|
[f834f90d] | 302 | }
|
---|
| 303 | }
|
---|
[a35b458] | 304 |
|
---|
[4f64a523] | 305 | return srv->ops->close(srv);
|
---|
[f834f90d] | 306 | }
|
---|
| 307 |
|
---|
[417a2ba1] | 308 | /* XXX Version should be part of @a sdu */
|
---|
[b7fd2a0] | 309 | errno_t iplink_ev_recv(iplink_srv_t *srv, iplink_recv_sdu_t *sdu, ip_ver_t ver)
|
---|
[f834f90d] | 310 | {
|
---|
[4f64a523] | 311 | if (srv->client_sess == NULL)
|
---|
| 312 | return EIO;
|
---|
[a35b458] | 313 |
|
---|
[4f64a523] | 314 | async_exch_t *exch = async_exchange_begin(srv->client_sess);
|
---|
[a35b458] | 315 |
|
---|
[f834f90d] | 316 | ipc_call_t answer;
|
---|
[417a2ba1] | 317 | aid_t req = async_send_1(exch, IPLINK_EV_RECV, (sysarg_t)ver,
|
---|
[02a09ed] | 318 | &answer);
|
---|
[a35b458] | 319 |
|
---|
[b7fd2a0] | 320 | errno_t rc = async_data_write_start(exch, sdu->data, sdu->size);
|
---|
[f834f90d] | 321 | async_exchange_end(exch);
|
---|
[a35b458] | 322 |
|
---|
[f834f90d] | 323 | if (rc != EOK) {
|
---|
[50b581d] | 324 | async_forget(req);
|
---|
[f834f90d] | 325 | return rc;
|
---|
| 326 | }
|
---|
[a35b458] | 327 |
|
---|
[b7fd2a0] | 328 | errno_t retval;
|
---|
[f834f90d] | 329 | async_wait_for(req, &retval);
|
---|
| 330 | if (retval != EOK)
|
---|
| 331 | return retval;
|
---|
[a35b458] | 332 |
|
---|
[f834f90d] | 333 | return EOK;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[b4edc96] | 336 | errno_t iplink_ev_change_addr(iplink_srv_t *srv, eth_addr_t *addr)
|
---|
[c3b25985] | 337 | {
|
---|
| 338 | if (srv->client_sess == NULL)
|
---|
| 339 | return EIO;
|
---|
[a35b458] | 340 |
|
---|
[c3b25985] | 341 | async_exch_t *exch = async_exchange_begin(srv->client_sess);
|
---|
[a35b458] | 342 |
|
---|
[c3b25985] | 343 | ipc_call_t answer;
|
---|
| 344 | aid_t req = async_send_0(exch, IPLINK_EV_CHANGE_ADDR, &answer);
|
---|
[a35b458] | 345 |
|
---|
[b4edc96] | 346 | errno_t rc = async_data_write_start(exch, addr, sizeof(eth_addr_t));
|
---|
[c3b25985] | 347 | async_exchange_end(exch);
|
---|
[a35b458] | 348 |
|
---|
[c3b25985] | 349 | if (rc != EOK) {
|
---|
| 350 | async_forget(req);
|
---|
| 351 | return rc;
|
---|
| 352 | }
|
---|
[a35b458] | 353 |
|
---|
[b7fd2a0] | 354 | errno_t retval;
|
---|
[c3b25985] | 355 | async_wait_for(req, &retval);
|
---|
| 356 | if (retval != EOK)
|
---|
| 357 | return retval;
|
---|
[a35b458] | 358 |
|
---|
[c3b25985] | 359 | return EOK;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
[f834f90d] | 362 | /** @}
|
---|
| 363 | */
|
---|