| 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, ilink_link) {
|
|---|
| 143 | inet_link_t *ilink = list_get_instance(ilink_link,
|
|---|
| 144 | inet_link_t, link_list);
|
|---|
| 145 | if (ilink->svc_id == svcs[i]) {
|
|---|
| 146 | already_known = true;
|
|---|
| 147 | break;
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | if (!already_known) {
|
|---|
| 152 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Found IP link '%lu'",
|
|---|
| 153 | (unsigned long) svcs[i]);
|
|---|
| 154 | rc = inet_link_open(svcs[i]);
|
|---|
| 155 | if (rc != EOK)
|
|---|
| 156 | log_msg(LOG_DEFAULT, LVL_ERROR, "Could not open IP link.");
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | fibril_mutex_unlock(&inet_discovery_lock);
|
|---|
| 161 | return EOK;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | static inet_link_t *inet_link_new(void)
|
|---|
| 165 | {
|
|---|
| 166 | inet_link_t *ilink = calloc(1, sizeof(inet_link_t));
|
|---|
| 167 |
|
|---|
| 168 | if (ilink == NULL) {
|
|---|
| 169 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating link structure. "
|
|---|
| 170 | "Out of memory.");
|
|---|
| 171 | return NULL;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | link_initialize(&ilink->link_list);
|
|---|
| 175 |
|
|---|
| 176 | return ilink;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | static void inet_link_delete(inet_link_t *ilink)
|
|---|
| 180 | {
|
|---|
| 181 | if (ilink->svc_name != NULL)
|
|---|
| 182 | free(ilink->svc_name);
|
|---|
| 183 |
|
|---|
| 184 | free(ilink);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | static int inet_link_open(service_id_t sid)
|
|---|
| 188 | {
|
|---|
| 189 | inet_link_t *ilink;
|
|---|
| 190 | inet_addr_t iaddr;
|
|---|
| 191 | int rc;
|
|---|
| 192 |
|
|---|
| 193 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_link_open()");
|
|---|
| 194 | ilink = inet_link_new();
|
|---|
| 195 | if (ilink == NULL)
|
|---|
| 196 | return ENOMEM;
|
|---|
| 197 |
|
|---|
| 198 | ilink->svc_id = sid;
|
|---|
| 199 | ilink->iplink = NULL;
|
|---|
| 200 |
|
|---|
| 201 | rc = loc_service_get_name(sid, &ilink->svc_name);
|
|---|
| 202 | if (rc != EOK) {
|
|---|
| 203 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
|
|---|
| 204 | goto error;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | ilink->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
|
|---|
| 208 | if (ilink->sess == NULL) {
|
|---|
| 209 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting '%s'", ilink->svc_name);
|
|---|
| 210 | goto error;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, &ilink->iplink);
|
|---|
| 214 | if (rc != EOK) {
|
|---|
| 215 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed opening IP link '%s'",
|
|---|
| 216 | ilink->svc_name);
|
|---|
| 217 | goto error;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | rc = iplink_get_mtu(ilink->iplink, &ilink->def_mtu);
|
|---|
| 221 | if (rc != EOK) {
|
|---|
| 222 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed determinning MTU of link '%s'",
|
|---|
| 223 | ilink->svc_name);
|
|---|
| 224 | goto error;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | /*
|
|---|
| 228 | * Get the MAC address of the link. If the link has a MAC
|
|---|
| 229 | * address, we assume that it supports NDP.
|
|---|
| 230 | */
|
|---|
| 231 | rc = iplink_get_mac48(ilink->iplink, &ilink->mac);
|
|---|
| 232 | ilink->mac_valid = (rc == EOK);
|
|---|
| 233 |
|
|---|
| 234 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Opened IP link '%s'", ilink->svc_name);
|
|---|
| 235 | list_append(&ilink->link_list, &inet_link_list);
|
|---|
| 236 |
|
|---|
| 237 | inet_addrobj_t *addr = NULL;
|
|---|
| 238 |
|
|---|
| 239 | if (first_link) {
|
|---|
| 240 | addr = inet_addrobj_new();
|
|---|
| 241 |
|
|---|
| 242 | inet_naddr(&addr->naddr, 127, 0, 0, 1, 24);
|
|---|
| 243 | first_link = false;
|
|---|
| 244 | } else {
|
|---|
| 245 | /*
|
|---|
| 246 | * FIXME
|
|---|
| 247 | * Setting static IPv4 address for testing purposes:
|
|---|
| 248 | * 10.0.2.15/24
|
|---|
| 249 | */
|
|---|
| 250 | addr = inet_addrobj_new();
|
|---|
| 251 |
|
|---|
| 252 | inet_naddr(&addr->naddr, 10, 0, 2, 15, 24);
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | if (addr != NULL) {
|
|---|
| 256 | addr->ilink = ilink;
|
|---|
| 257 | addr->name = str_dup("v4a");
|
|---|
| 258 |
|
|---|
| 259 | rc = inet_addrobj_add(addr);
|
|---|
| 260 | if (rc == EOK) {
|
|---|
| 261 | inet_naddr_addr(&addr->naddr, &iaddr);
|
|---|
| 262 | rc = iplink_addr_add(ilink->iplink, &iaddr);
|
|---|
| 263 | if (rc != EOK) {
|
|---|
| 264 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| 265 | "Failed setting IPv4 address on internet link.");
|
|---|
| 266 | inet_addrobj_remove(addr);
|
|---|
| 267 | inet_addrobj_delete(addr);
|
|---|
| 268 | }
|
|---|
| 269 | } else {
|
|---|
| 270 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding IPv4 address.");
|
|---|
| 271 | inet_addrobj_delete(addr);
|
|---|
| 272 | }
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | inet_addrobj_t *addr6 = NULL;
|
|---|
| 276 |
|
|---|
| 277 | if (first_link6) {
|
|---|
| 278 | addr6 = inet_addrobj_new();
|
|---|
| 279 |
|
|---|
| 280 | inet_naddr6(&addr6->naddr, 0, 0, 0, 0, 0, 0, 0, 1, 128);
|
|---|
| 281 | first_link6 = false;
|
|---|
| 282 | } else if (ilink->mac_valid) {
|
|---|
| 283 | addr6 = inet_addrobj_new();
|
|---|
| 284 |
|
|---|
| 285 | addr128_t link_local;
|
|---|
| 286 | inet_link_local_node_ip(ilink->mac, link_local);
|
|---|
| 287 |
|
|---|
| 288 | inet_naddr_set6(link_local, 64, &addr6->naddr);
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | if (addr6 != NULL) {
|
|---|
| 292 | addr6->ilink = ilink;
|
|---|
| 293 | addr6->name = str_dup("v6a");
|
|---|
| 294 |
|
|---|
| 295 | rc = inet_addrobj_add(addr6);
|
|---|
| 296 | if (rc == EOK) {
|
|---|
| 297 | inet_naddr_addr(&addr6->naddr, &iaddr);
|
|---|
| 298 | rc = iplink_addr_add(ilink->iplink, &iaddr);
|
|---|
| 299 | if (rc != EOK) {
|
|---|
| 300 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| 301 | "Failed setting IPv6 address on internet link.");
|
|---|
| 302 | inet_addrobj_remove(addr6);
|
|---|
| 303 | inet_addrobj_delete(addr6);
|
|---|
| 304 | }
|
|---|
| 305 | } else {
|
|---|
| 306 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding IPv6 address.");
|
|---|
| 307 | inet_addrobj_delete(addr6);
|
|---|
| 308 | }
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | return EOK;
|
|---|
| 312 |
|
|---|
| 313 | error:
|
|---|
| 314 | if (ilink->iplink != NULL)
|
|---|
| 315 | iplink_close(ilink->iplink);
|
|---|
| 316 |
|
|---|
| 317 | inet_link_delete(ilink);
|
|---|
| 318 | return rc;
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | static void inet_link_cat_change_cb(void)
|
|---|
| 322 | {
|
|---|
| 323 | (void) inet_link_check_new();
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | int inet_link_discovery_start(void)
|
|---|
| 327 | {
|
|---|
| 328 | int rc;
|
|---|
| 329 |
|
|---|
| 330 | rc = loc_register_cat_change_cb(inet_link_cat_change_cb);
|
|---|
| 331 | if (rc != EOK) {
|
|---|
| 332 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback for IP link "
|
|---|
| 333 | "discovery (%d).", rc);
|
|---|
| 334 | return rc;
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | return inet_link_check_new();
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | /** Send IPv4 datagram over Internet link
|
|---|
| 341 | *
|
|---|
| 342 | * @param ilink Internet link
|
|---|
| 343 | * @param lsrc Source IPv4 address
|
|---|
| 344 | * @param ldest Destination IPv4 address
|
|---|
| 345 | * @param dgram IPv4 datagram body
|
|---|
| 346 | * @param proto Protocol
|
|---|
| 347 | * @param ttl Time-to-live
|
|---|
| 348 | * @param df Do-not-Fragment flag
|
|---|
| 349 | *
|
|---|
| 350 | * @return EOK on success
|
|---|
| 351 | * @return ENOMEM when not enough memory to create the datagram
|
|---|
| 352 | * @return ENOTSUP if networking mode is not supported
|
|---|
| 353 | *
|
|---|
| 354 | */
|
|---|
| 355 | int inet_link_send_dgram(inet_link_t *ilink, addr32_t lsrc, addr32_t ldest,
|
|---|
| 356 | inet_dgram_t *dgram, uint8_t proto, uint8_t ttl, int df)
|
|---|
| 357 | {
|
|---|
| 358 | addr32_t src_v4;
|
|---|
| 359 | uint16_t src_af = inet_addr_get(&dgram->src, &src_v4, NULL);
|
|---|
| 360 | if (src_af != AF_INET)
|
|---|
| 361 | return EINVAL;
|
|---|
| 362 |
|
|---|
| 363 | addr32_t dest_v4;
|
|---|
| 364 | uint16_t dest_af = inet_addr_get(&dgram->dest, &dest_v4, NULL);
|
|---|
| 365 | if (dest_af != AF_INET)
|
|---|
| 366 | return EINVAL;
|
|---|
| 367 |
|
|---|
| 368 | /*
|
|---|
| 369 | * Fill packet structure. Fragmentation is performed by
|
|---|
| 370 | * inet_pdu_encode().
|
|---|
| 371 | */
|
|---|
| 372 |
|
|---|
| 373 | iplink_sdu_t sdu;
|
|---|
| 374 |
|
|---|
| 375 | sdu.src = lsrc;
|
|---|
| 376 | sdu.dest = ldest;
|
|---|
| 377 |
|
|---|
| 378 | inet_packet_t packet;
|
|---|
| 379 |
|
|---|
| 380 | packet.src = dgram->src;
|
|---|
| 381 | packet.dest = dgram->dest;
|
|---|
| 382 | packet.tos = dgram->tos;
|
|---|
| 383 | packet.proto = proto;
|
|---|
| 384 | packet.ttl = ttl;
|
|---|
| 385 |
|
|---|
| 386 | /* Allocate identifier */
|
|---|
| 387 | fibril_mutex_lock(&ip_ident_lock);
|
|---|
| 388 | packet.ident = ++ip_ident;
|
|---|
| 389 | fibril_mutex_unlock(&ip_ident_lock);
|
|---|
| 390 |
|
|---|
| 391 | packet.df = df;
|
|---|
| 392 | packet.data = dgram->data;
|
|---|
| 393 | packet.size = dgram->size;
|
|---|
| 394 |
|
|---|
| 395 | int rc;
|
|---|
| 396 | size_t offs = 0;
|
|---|
| 397 |
|
|---|
| 398 | do {
|
|---|
| 399 | /* Encode one fragment */
|
|---|
| 400 |
|
|---|
| 401 | size_t roffs;
|
|---|
| 402 | rc = inet_pdu_encode(&packet, src_v4, dest_v4, offs, ilink->def_mtu,
|
|---|
| 403 | &sdu.data, &sdu.size, &roffs);
|
|---|
| 404 | if (rc != EOK)
|
|---|
| 405 | return rc;
|
|---|
| 406 |
|
|---|
| 407 | /* Send the PDU */
|
|---|
| 408 | rc = iplink_send(ilink->iplink, &sdu);
|
|---|
| 409 |
|
|---|
| 410 | free(sdu.data);
|
|---|
| 411 | offs = roffs;
|
|---|
| 412 | } while (offs < packet.size);
|
|---|
| 413 |
|
|---|
| 414 | return rc;
|
|---|
| 415 | }
|
|---|
| 416 |
|
|---|
| 417 | /** Send IPv6 datagram over Internet link
|
|---|
| 418 | *
|
|---|
| 419 | * @param ilink Internet link
|
|---|
| 420 | * @param ldest Destination MAC address
|
|---|
| 421 | * @param dgram IPv6 datagram body
|
|---|
| 422 | * @param proto Next header
|
|---|
| 423 | * @param ttl Hop limit
|
|---|
| 424 | * @param df Do-not-Fragment flag (unused)
|
|---|
| 425 | *
|
|---|
| 426 | * @return EOK on success
|
|---|
| 427 | * @return ENOMEM when not enough memory to create the datagram
|
|---|
| 428 | *
|
|---|
| 429 | */
|
|---|
| 430 | int inet_link_send_dgram6(inet_link_t *ilink, addr48_t ldest,
|
|---|
| 431 | inet_dgram_t *dgram, uint8_t proto, uint8_t ttl, int df)
|
|---|
| 432 | {
|
|---|
| 433 | addr128_t src_v6;
|
|---|
| 434 | uint16_t src_af = inet_addr_get(&dgram->src, NULL, &src_v6);
|
|---|
| 435 | if (src_af != AF_INET6)
|
|---|
| 436 | return EINVAL;
|
|---|
| 437 |
|
|---|
| 438 | addr128_t dest_v6;
|
|---|
| 439 | uint16_t dest_af = inet_addr_get(&dgram->dest, NULL, &dest_v6);
|
|---|
| 440 | if (dest_af != AF_INET6)
|
|---|
| 441 | return EINVAL;
|
|---|
| 442 |
|
|---|
| 443 | iplink_sdu6_t sdu6;
|
|---|
| 444 | addr48(ldest, sdu6.dest);
|
|---|
| 445 |
|
|---|
| 446 | /*
|
|---|
| 447 | * Fill packet structure. Fragmentation is performed by
|
|---|
| 448 | * inet_pdu_encode6().
|
|---|
| 449 | */
|
|---|
| 450 |
|
|---|
| 451 | inet_packet_t packet;
|
|---|
| 452 |
|
|---|
| 453 | packet.src = dgram->src;
|
|---|
| 454 | packet.dest = dgram->dest;
|
|---|
| 455 | packet.tos = dgram->tos;
|
|---|
| 456 | packet.proto = proto;
|
|---|
| 457 | packet.ttl = ttl;
|
|---|
| 458 |
|
|---|
| 459 | /* Allocate identifier */
|
|---|
| 460 | fibril_mutex_lock(&ip_ident_lock);
|
|---|
| 461 | packet.ident = ++ip_ident;
|
|---|
| 462 | fibril_mutex_unlock(&ip_ident_lock);
|
|---|
| 463 |
|
|---|
| 464 | packet.df = df;
|
|---|
| 465 | packet.data = dgram->data;
|
|---|
| 466 | packet.size = dgram->size;
|
|---|
| 467 |
|
|---|
| 468 | int rc;
|
|---|
| 469 | size_t offs = 0;
|
|---|
| 470 |
|
|---|
| 471 | do {
|
|---|
| 472 | /* Encode one fragment */
|
|---|
| 473 |
|
|---|
| 474 | size_t roffs;
|
|---|
| 475 | rc = inet_pdu_encode6(&packet, src_v6, dest_v6, offs, ilink->def_mtu,
|
|---|
| 476 | &sdu6.data, &sdu6.size, &roffs);
|
|---|
| 477 | if (rc != EOK)
|
|---|
| 478 | return rc;
|
|---|
| 479 |
|
|---|
| 480 | /* Send the PDU */
|
|---|
| 481 | rc = iplink_send6(ilink->iplink, &sdu6);
|
|---|
| 482 |
|
|---|
| 483 | free(sdu6.data);
|
|---|
| 484 | offs = roffs;
|
|---|
| 485 | } while (offs < packet.size);
|
|---|
| 486 |
|
|---|
| 487 | return rc;
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | inet_link_t *inet_link_get_by_id(sysarg_t link_id)
|
|---|
| 491 | {
|
|---|
| 492 | fibril_mutex_lock(&inet_discovery_lock);
|
|---|
| 493 |
|
|---|
| 494 | list_foreach(inet_link_list, elem) {
|
|---|
| 495 | inet_link_t *ilink = list_get_instance(elem, inet_link_t,
|
|---|
| 496 | link_list);
|
|---|
| 497 |
|
|---|
| 498 | if (ilink->svc_id == link_id) {
|
|---|
| 499 | fibril_mutex_unlock(&inet_discovery_lock);
|
|---|
| 500 | return ilink;
|
|---|
| 501 | }
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 | fibril_mutex_unlock(&inet_discovery_lock);
|
|---|
| 505 | return NULL;
|
|---|
| 506 | }
|
|---|
| 507 |
|
|---|
| 508 | /** @}
|
|---|
| 509 | */
|
|---|