| 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 inet
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <stdbool.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <fibril_synch.h>
|
|---|
| 40 | #include <inet/iplink.h>
|
|---|
| 41 | #include <io/log.h>
|
|---|
| 42 | #include <loc.h>
|
|---|
| 43 | #include <stdlib.h>
|
|---|
| 44 | #include <str.h>
|
|---|
| 45 | #include <net/socket_codes.h>
|
|---|
| 46 | #include "addrobj.h"
|
|---|
| 47 | #include "inetsrv.h"
|
|---|
| 48 | #include "inet_link.h"
|
|---|
| 49 | #include "pdu.h"
|
|---|
| 50 |
|
|---|
| 51 | static bool first_link = true;
|
|---|
| 52 | static bool first_link6 = true;
|
|---|
| 53 |
|
|---|
| 54 | static FIBRIL_MUTEX_INITIALIZE(ip_ident_lock);
|
|---|
| 55 | static uint16_t ip_ident = 0;
|
|---|
| 56 |
|
|---|
| 57 | static int inet_link_open(service_id_t);
|
|---|
| 58 | static int inet_iplink_recv(iplink_t *, iplink_recv_sdu_t *, uint16_t);
|
|---|
| 59 |
|
|---|
| 60 | static iplink_ev_ops_t inet_iplink_ev_ops = {
|
|---|
| 61 | .recv = inet_iplink_recv
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| 64 | static LIST_INITIALIZE(inet_link_list);
|
|---|
| 65 | static FIBRIL_MUTEX_INITIALIZE(inet_discovery_lock);
|
|---|
| 66 |
|
|---|
| 67 | static addr128_t link_local_node_ip =
|
|---|
| 68 | {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xfe, 0, 0, 0};
|
|---|
| 69 |
|
|---|
| 70 | static void inet_link_local_node_ip(addr48_t mac_addr,
|
|---|
| 71 | addr128_t ip_addr)
|
|---|
| 72 | {
|
|---|
| 73 | memcpy(ip_addr, link_local_node_ip, 16);
|
|---|
| 74 |
|
|---|
| 75 | ip_addr[8] = mac_addr[0] ^ 0x02;
|
|---|
| 76 | ip_addr[9] = mac_addr[1];
|
|---|
| 77 | ip_addr[10] = mac_addr[2];
|
|---|
| 78 | ip_addr[13] = mac_addr[3];
|
|---|
| 79 | ip_addr[14] = mac_addr[4];
|
|---|
| 80 | ip_addr[15] = mac_addr[5];
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | static int inet_iplink_recv(iplink_t *iplink, iplink_recv_sdu_t *sdu, uint16_t af)
|
|---|
| 84 | {
|
|---|
| 85 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_iplink_recv()");
|
|---|
| 86 |
|
|---|
| 87 | int rc;
|
|---|
| 88 | inet_packet_t packet;
|
|---|
| 89 |
|
|---|
| 90 | switch (af) {
|
|---|
| 91 | case AF_INET:
|
|---|
| 92 | rc = inet_pdu_decode(sdu->data, sdu->size, &packet);
|
|---|
| 93 | break;
|
|---|
| 94 | case AF_INET6:
|
|---|
| 95 | rc = inet_pdu_decode6(sdu->data, sdu->size, &packet);
|
|---|
| 96 | break;
|
|---|
| 97 | default:
|
|---|
| 98 | log_msg(LOG_DEFAULT, LVL_DEBUG, "invalid address family");
|
|---|
| 99 | return EINVAL;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | if (rc != EOK) {
|
|---|
| 103 | log_msg(LOG_DEFAULT, LVL_DEBUG, "failed decoding PDU");
|
|---|
| 104 | return rc;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet()");
|
|---|
| 108 | rc = inet_recv_packet(&packet);
|
|---|
| 109 | log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet -> %d", rc);
|
|---|
| 110 | free(packet.data);
|
|---|
| 111 |
|
|---|
| 112 | return rc;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | static int inet_link_check_new(void)
|
|---|
| 116 | {
|
|---|
| 117 | bool already_known;
|
|---|
| 118 | category_id_t iplink_cat;
|
|---|
| 119 | service_id_t *svcs;
|
|---|
| 120 | size_t count, i;
|
|---|
| 121 | int rc;
|
|---|
| 122 |
|
|---|
| 123 | fibril_mutex_lock(&inet_discovery_lock);
|
|---|
| 124 |
|
|---|
| 125 | rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
|
|---|
| 126 | if (rc != EOK) {
|
|---|
| 127 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
|
|---|
| 128 | fibril_mutex_unlock(&inet_discovery_lock);
|
|---|
| 129 | return ENOENT;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | rc = loc_category_get_svcs(iplink_cat, &svcs, &count);
|
|---|
| 133 | if (rc != EOK) {
|
|---|
| 134 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting list of IP links.");
|
|---|
| 135 | fibril_mutex_unlock(&inet_discovery_lock);
|
|---|
| 136 | return EIO;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | for (i = 0; i < count; i++) {
|
|---|
| 140 | already_known = false;
|
|---|
| 141 |
|
|---|
| 142 | list_foreach(inet_link_list, link_list, inet_link_t, ilink) {
|
|---|
| 143 | if (ilink->svc_id == svcs[i]) {
|
|---|
| 144 | already_known = true;
|
|---|
| 145 | break;
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | if (!already_known) {
|
|---|
| 150 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Found IP link '%lu'",
|
|---|
| 151 | (unsigned long) svcs[i]);
|
|---|
| 152 | rc = inet_link_open(svcs[i]);
|
|---|
| 153 | if (rc != EOK)
|
|---|
| 154 | log_msg(LOG_DEFAULT, LVL_ERROR, "Could not open IP link.");
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | fibril_mutex_unlock(&inet_discovery_lock);
|
|---|
| 159 | return EOK;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | static inet_link_t *inet_link_new(void)
|
|---|
| 163 | {
|
|---|
| 164 | inet_link_t *ilink = calloc(1, sizeof(inet_link_t));
|
|---|
| 165 |
|
|---|
| 166 | if (ilink == NULL) {
|
|---|
| 167 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating link structure. "
|
|---|
| 168 | "Out of memory.");
|
|---|
| 169 | return NULL;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | link_initialize(&ilink->link_list);
|
|---|
| 173 |
|
|---|
| 174 | return ilink;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | static void inet_link_delete(inet_link_t *ilink)
|
|---|
| 178 | {
|
|---|
| 179 | if (ilink->svc_name != NULL)
|
|---|
| 180 | free(ilink->svc_name);
|
|---|
| 181 |
|
|---|
| 182 | free(ilink);
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | static int inet_link_open(service_id_t sid)
|
|---|
| 186 | {
|
|---|
| 187 | inet_link_t *ilink;
|
|---|
| 188 | inet_addr_t iaddr;
|
|---|
| 189 | int rc;
|
|---|
| 190 |
|
|---|
| 191 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_link_open()");
|
|---|
| 192 | ilink = inet_link_new();
|
|---|
| 193 | if (ilink == NULL)
|
|---|
| 194 | return ENOMEM;
|
|---|
| 195 |
|
|---|
| 196 | ilink->svc_id = sid;
|
|---|
| 197 | ilink->iplink = NULL;
|
|---|
| 198 |
|
|---|
| 199 | rc = loc_service_get_name(sid, &ilink->svc_name);
|
|---|
| 200 | if (rc != EOK) {
|
|---|
| 201 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
|
|---|
| 202 | goto error;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | ilink->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
|
|---|
| 206 | if (ilink->sess == NULL) {
|
|---|
| 207 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting '%s'", ilink->svc_name);
|
|---|
| 208 | goto error;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, &ilink->iplink);
|
|---|
| 212 | if (rc != EOK) {
|
|---|
| 213 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed opening IP link '%s'",
|
|---|
| 214 | ilink->svc_name);
|
|---|
| 215 | goto error;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | rc = iplink_get_mtu(ilink->iplink, &ilink->def_mtu);
|
|---|
| 219 | if (rc != EOK) {
|
|---|
| 220 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed determinning MTU of link '%s'",
|
|---|
| 221 | ilink->svc_name);
|
|---|
| 222 | goto error;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | /*
|
|---|
| 226 | * Get the MAC address of the link. If the link has a MAC
|
|---|
| 227 | * address, we assume that it supports NDP.
|
|---|
| 228 | */
|
|---|
| 229 | rc = iplink_get_mac48(ilink->iplink, &ilink->mac);
|
|---|
| 230 | ilink->mac_valid = (rc == EOK);
|
|---|
| 231 |
|
|---|
| 232 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Opened IP link '%s'", ilink->svc_name);
|
|---|
| 233 | list_append(&ilink->link_list, &inet_link_list);
|
|---|
| 234 |
|
|---|
| 235 | inet_addrobj_t *addr = NULL;
|
|---|
| 236 |
|
|---|
| 237 | /* XXX FIXME Cannot rely on loopback being the first IP link service!! */
|
|---|
| 238 | if (first_link) {
|
|---|
| 239 | addr = inet_addrobj_new();
|
|---|
| 240 |
|
|---|
| 241 | inet_naddr(&addr->naddr, 127, 0, 0, 1, 24);
|
|---|
| 242 | first_link = false;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | if (addr != NULL) {
|
|---|
| 246 | addr->ilink = ilink;
|
|---|
| 247 | addr->name = str_dup("v4a");
|
|---|
| 248 |
|
|---|
| 249 | rc = inet_addrobj_add(addr);
|
|---|
| 250 | if (rc == EOK) {
|
|---|
| 251 | inet_naddr_addr(&addr->naddr, &iaddr);
|
|---|
| 252 | rc = iplink_addr_add(ilink->iplink, &iaddr);
|
|---|
| 253 | if (rc != EOK) {
|
|---|
| 254 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| 255 | "Failed setting IPv4 address on internet link.");
|
|---|
| 256 | inet_addrobj_remove(addr);
|
|---|
| 257 | inet_addrobj_delete(addr);
|
|---|
| 258 | }
|
|---|
| 259 | } else {
|
|---|
| 260 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding IPv4 address.");
|
|---|
| 261 | inet_addrobj_delete(addr);
|
|---|
| 262 | }
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | inet_addrobj_t *addr6 = NULL;
|
|---|
| 266 |
|
|---|
| 267 | if (first_link6) {
|
|---|
| 268 | addr6 = inet_addrobj_new();
|
|---|
| 269 |
|
|---|
| 270 | inet_naddr6(&addr6->naddr, 0, 0, 0, 0, 0, 0, 0, 1, 128);
|
|---|
| 271 | first_link6 = false;
|
|---|
| 272 | } else if (ilink->mac_valid) {
|
|---|
| 273 | addr6 = inet_addrobj_new();
|
|---|
| 274 |
|
|---|
| 275 | addr128_t link_local;
|
|---|
| 276 | inet_link_local_node_ip(ilink->mac, link_local);
|
|---|
| 277 |
|
|---|
| 278 | inet_naddr_set6(link_local, 64, &addr6->naddr);
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | if (addr6 != NULL) {
|
|---|
| 282 | addr6->ilink = ilink;
|
|---|
| 283 | addr6->name = str_dup("v6a");
|
|---|
| 284 |
|
|---|
| 285 | rc = inet_addrobj_add(addr6);
|
|---|
| 286 | if (rc == EOK) {
|
|---|
| 287 | inet_naddr_addr(&addr6->naddr, &iaddr);
|
|---|
| 288 | rc = iplink_addr_add(ilink->iplink, &iaddr);
|
|---|
| 289 | if (rc != EOK) {
|
|---|
| 290 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| 291 | "Failed setting IPv6 address on internet link.");
|
|---|
| 292 | inet_addrobj_remove(addr6);
|
|---|
| 293 | inet_addrobj_delete(addr6);
|
|---|
| 294 | }
|
|---|
| 295 | } else {
|
|---|
| 296 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding IPv6 address.");
|
|---|
| 297 | inet_addrobj_delete(addr6);
|
|---|
| 298 | }
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | return EOK;
|
|---|
| 302 |
|
|---|
| 303 | error:
|
|---|
| 304 | if (ilink->iplink != NULL)
|
|---|
| 305 | iplink_close(ilink->iplink);
|
|---|
| 306 |
|
|---|
| 307 | inet_link_delete(ilink);
|
|---|
| 308 | return rc;
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | static void inet_link_cat_change_cb(void)
|
|---|
| 312 | {
|
|---|
| 313 | (void) inet_link_check_new();
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | int inet_link_discovery_start(void)
|
|---|
| 317 | {
|
|---|
| 318 | int rc;
|
|---|
| 319 |
|
|---|
| 320 | rc = loc_register_cat_change_cb(inet_link_cat_change_cb);
|
|---|
| 321 | if (rc != EOK) {
|
|---|
| 322 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback for IP link "
|
|---|
| 323 | "discovery (%d).", rc);
|
|---|
| 324 | return rc;
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | return inet_link_check_new();
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | /** Send IPv4 datagram over Internet link
|
|---|
| 331 | *
|
|---|
| 332 | * @param ilink Internet link
|
|---|
| 333 | * @param lsrc Source IPv4 address
|
|---|
| 334 | * @param ldest Destination IPv4 address
|
|---|
| 335 | * @param dgram IPv4 datagram body
|
|---|
| 336 | * @param proto Protocol
|
|---|
| 337 | * @param ttl Time-to-live
|
|---|
| 338 | * @param df Do-not-Fragment flag
|
|---|
| 339 | *
|
|---|
| 340 | * @return EOK on success
|
|---|
| 341 | * @return ENOMEM when not enough memory to create the datagram
|
|---|
| 342 | * @return ENOTSUP if networking mode is not supported
|
|---|
| 343 | *
|
|---|
| 344 | */
|
|---|
| 345 | int inet_link_send_dgram(inet_link_t *ilink, addr32_t lsrc, addr32_t ldest,
|
|---|
| 346 | inet_dgram_t *dgram, uint8_t proto, uint8_t ttl, int df)
|
|---|
| 347 | {
|
|---|
| 348 | addr32_t src_v4;
|
|---|
| 349 | uint16_t src_af = inet_addr_get(&dgram->src, &src_v4, NULL);
|
|---|
| 350 | if (src_af != AF_INET)
|
|---|
| 351 | return EINVAL;
|
|---|
| 352 |
|
|---|
| 353 | addr32_t dest_v4;
|
|---|
| 354 | uint16_t dest_af = inet_addr_get(&dgram->dest, &dest_v4, NULL);
|
|---|
| 355 | if (dest_af != AF_INET)
|
|---|
| 356 | return EINVAL;
|
|---|
| 357 |
|
|---|
| 358 | /*
|
|---|
| 359 | * Fill packet structure. Fragmentation is performed by
|
|---|
| 360 | * inet_pdu_encode().
|
|---|
| 361 | */
|
|---|
| 362 |
|
|---|
| 363 | iplink_sdu_t sdu;
|
|---|
| 364 |
|
|---|
| 365 | sdu.src = lsrc;
|
|---|
| 366 | sdu.dest = ldest;
|
|---|
| 367 |
|
|---|
| 368 | inet_packet_t packet;
|
|---|
| 369 |
|
|---|
| 370 | packet.src = dgram->src;
|
|---|
| 371 | packet.dest = dgram->dest;
|
|---|
| 372 | packet.tos = dgram->tos;
|
|---|
| 373 | packet.proto = proto;
|
|---|
| 374 | packet.ttl = ttl;
|
|---|
| 375 |
|
|---|
| 376 | /* Allocate identifier */
|
|---|
| 377 | fibril_mutex_lock(&ip_ident_lock);
|
|---|
| 378 | packet.ident = ++ip_ident;
|
|---|
| 379 | fibril_mutex_unlock(&ip_ident_lock);
|
|---|
| 380 |
|
|---|
| 381 | packet.df = df;
|
|---|
| 382 | packet.data = dgram->data;
|
|---|
| 383 | packet.size = dgram->size;
|
|---|
| 384 |
|
|---|
| 385 | int rc;
|
|---|
| 386 | size_t offs = 0;
|
|---|
| 387 |
|
|---|
| 388 | do {
|
|---|
| 389 | /* Encode one fragment */
|
|---|
| 390 |
|
|---|
| 391 | size_t roffs;
|
|---|
| 392 | rc = inet_pdu_encode(&packet, src_v4, dest_v4, offs, ilink->def_mtu,
|
|---|
| 393 | &sdu.data, &sdu.size, &roffs);
|
|---|
| 394 | if (rc != EOK)
|
|---|
| 395 | return rc;
|
|---|
| 396 |
|
|---|
| 397 | /* Send the PDU */
|
|---|
| 398 | rc = iplink_send(ilink->iplink, &sdu);
|
|---|
| 399 |
|
|---|
| 400 | free(sdu.data);
|
|---|
| 401 | offs = roffs;
|
|---|
| 402 | } while (offs < packet.size);
|
|---|
| 403 |
|
|---|
| 404 | return rc;
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | /** Send IPv6 datagram over Internet link
|
|---|
| 408 | *
|
|---|
| 409 | * @param ilink Internet link
|
|---|
| 410 | * @param ldest Destination MAC address
|
|---|
| 411 | * @param dgram IPv6 datagram body
|
|---|
| 412 | * @param proto Next header
|
|---|
| 413 | * @param ttl Hop limit
|
|---|
| 414 | * @param df Do-not-Fragment flag (unused)
|
|---|
| 415 | *
|
|---|
| 416 | * @return EOK on success
|
|---|
| 417 | * @return ENOMEM when not enough memory to create the datagram
|
|---|
| 418 | *
|
|---|
| 419 | */
|
|---|
| 420 | int inet_link_send_dgram6(inet_link_t *ilink, addr48_t ldest,
|
|---|
| 421 | inet_dgram_t *dgram, uint8_t proto, uint8_t ttl, int df)
|
|---|
| 422 | {
|
|---|
| 423 | addr128_t src_v6;
|
|---|
| 424 | uint16_t src_af = inet_addr_get(&dgram->src, NULL, &src_v6);
|
|---|
| 425 | if (src_af != AF_INET6)
|
|---|
| 426 | return EINVAL;
|
|---|
| 427 |
|
|---|
| 428 | addr128_t dest_v6;
|
|---|
| 429 | uint16_t dest_af = inet_addr_get(&dgram->dest, NULL, &dest_v6);
|
|---|
| 430 | if (dest_af != AF_INET6)
|
|---|
| 431 | return EINVAL;
|
|---|
| 432 |
|
|---|
| 433 | iplink_sdu6_t sdu6;
|
|---|
| 434 | addr48(ldest, sdu6.dest);
|
|---|
| 435 |
|
|---|
| 436 | /*
|
|---|
| 437 | * Fill packet structure. Fragmentation is performed by
|
|---|
| 438 | * inet_pdu_encode6().
|
|---|
| 439 | */
|
|---|
| 440 |
|
|---|
| 441 | inet_packet_t packet;
|
|---|
| 442 |
|
|---|
| 443 | packet.src = dgram->src;
|
|---|
| 444 | packet.dest = dgram->dest;
|
|---|
| 445 | packet.tos = dgram->tos;
|
|---|
| 446 | packet.proto = proto;
|
|---|
| 447 | packet.ttl = ttl;
|
|---|
| 448 |
|
|---|
| 449 | /* Allocate identifier */
|
|---|
| 450 | fibril_mutex_lock(&ip_ident_lock);
|
|---|
| 451 | packet.ident = ++ip_ident;
|
|---|
| 452 | fibril_mutex_unlock(&ip_ident_lock);
|
|---|
| 453 |
|
|---|
| 454 | packet.df = df;
|
|---|
| 455 | packet.data = dgram->data;
|
|---|
| 456 | packet.size = dgram->size;
|
|---|
| 457 |
|
|---|
| 458 | int rc;
|
|---|
| 459 | size_t offs = 0;
|
|---|
| 460 |
|
|---|
| 461 | do {
|
|---|
| 462 | /* Encode one fragment */
|
|---|
| 463 |
|
|---|
| 464 | size_t roffs;
|
|---|
| 465 | rc = inet_pdu_encode6(&packet, src_v6, dest_v6, offs, ilink->def_mtu,
|
|---|
| 466 | &sdu6.data, &sdu6.size, &roffs);
|
|---|
| 467 | if (rc != EOK)
|
|---|
| 468 | return rc;
|
|---|
| 469 |
|
|---|
| 470 | /* Send the PDU */
|
|---|
| 471 | rc = iplink_send6(ilink->iplink, &sdu6);
|
|---|
| 472 |
|
|---|
| 473 | free(sdu6.data);
|
|---|
| 474 | offs = roffs;
|
|---|
| 475 | } while (offs < packet.size);
|
|---|
| 476 |
|
|---|
| 477 | return rc;
|
|---|
| 478 | }
|
|---|
| 479 |
|
|---|
| 480 | inet_link_t *inet_link_get_by_id(sysarg_t link_id)
|
|---|
| 481 | {
|
|---|
| 482 | fibril_mutex_lock(&inet_discovery_lock);
|
|---|
| 483 |
|
|---|
| 484 | list_foreach(inet_link_list, link_list, inet_link_t, ilink) {
|
|---|
| 485 | if (ilink->svc_id == link_id) {
|
|---|
| 486 | fibril_mutex_unlock(&inet_discovery_lock);
|
|---|
| 487 | return ilink;
|
|---|
| 488 | }
|
|---|
| 489 | }
|
|---|
| 490 |
|
|---|
| 491 | fibril_mutex_unlock(&inet_discovery_lock);
|
|---|
| 492 | return NULL;
|
|---|
| 493 | }
|
|---|
| 494 |
|
|---|
| 495 | /** Get IDs of all links. */
|
|---|
| 496 | int inet_link_get_id_list(sysarg_t **rid_list, size_t *rcount)
|
|---|
| 497 | {
|
|---|
| 498 | sysarg_t *id_list;
|
|---|
| 499 | size_t count, i;
|
|---|
| 500 |
|
|---|
| 501 | fibril_mutex_lock(&inet_discovery_lock);
|
|---|
| 502 | count = list_count(&inet_link_list);
|
|---|
| 503 |
|
|---|
| 504 | id_list = calloc(count, sizeof(sysarg_t));
|
|---|
| 505 | if (id_list == NULL) {
|
|---|
| 506 | fibril_mutex_unlock(&inet_discovery_lock);
|
|---|
| 507 | return ENOMEM;
|
|---|
| 508 | }
|
|---|
| 509 |
|
|---|
| 510 | i = 0;
|
|---|
| 511 | list_foreach(inet_link_list, link_list, inet_link_t, ilink) {
|
|---|
| 512 | id_list[i++] = ilink->svc_id;
|
|---|
| 513 | log_msg(LOG_DEFAULT, LVL_NOTE, "add link to list");
|
|---|
| 514 | }
|
|---|
| 515 |
|
|---|
| 516 | fibril_mutex_unlock(&inet_discovery_lock);
|
|---|
| 517 |
|
|---|
| 518 | log_msg(LOG_DEFAULT, LVL_NOTE, "return %zu links", count);
|
|---|
| 519 | *rid_list = id_list;
|
|---|
| 520 | *rcount = count;
|
|---|
| 521 |
|
|---|
| 522 | return EOK;
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 | /** @}
|
|---|
| 526 | */
|
|---|