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