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