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