source: mainline/uspace/srv/net/inetsrv/inet_link.c@ d5ed54b

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d5ed54b was d5ed54b, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Use assignment operator to copy Ethernet addresses

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