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

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

Represent Ethernet address as a number instead of an array

Carefully document the design since this breaks the principle of least
surprise. Also add unit tests.

  • Property mode set to 100644
File size: 11.9 KB
Line 
1/*
2 * Copyright (c) 2021 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 <errno.h>
38#include <fibril_synch.h>
39#include <inet/eth_addr.h>
40#include <inet/iplink.h>
41#include <io/log.h>
42#include <loc.h>
43#include <stdbool.h>
44#include <stdlib.h>
45#include <str.h>
46#include <str_error.h>
47#include "addrobj.h"
48#include "inetsrv.h"
49#include "inet_link.h"
50#include "pdu.h"
51
52static bool first_link = true;
53static bool first_link6 = true;
54
55static FIBRIL_MUTEX_INITIALIZE(ip_ident_lock);
56static uint16_t ip_ident = 0;
57
58static errno_t inet_iplink_recv(iplink_t *, iplink_recv_sdu_t *, ip_ver_t);
59static errno_t inet_iplink_change_addr(iplink_t *, eth_addr_t *);
60static inet_link_t *inet_link_get_by_id_locked(sysarg_t);
61
62static iplink_ev_ops_t inet_iplink_ev_ops = {
63 .recv = inet_iplink_recv,
64 .change_addr = inet_iplink_change_addr,
65};
66
67static LIST_INITIALIZE(inet_links);
68static FIBRIL_MUTEX_INITIALIZE(inet_links_lock);
69
70static addr128_t link_local_node_ip =
71 { 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xfe, 0, 0, 0 };
72
73static void inet_link_local_node_ip(eth_addr_t *mac_addr,
74 addr128_t ip_addr)
75{
76 uint8_t b[ETH_ADDR_SIZE];
77
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];
87}
88
89static errno_t inet_iplink_recv(iplink_t *iplink, iplink_recv_sdu_t *sdu, ip_ver_t ver)
90{
91 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_iplink_recv()");
92
93 errno_t rc;
94 inet_packet_t packet;
95 inet_link_t *ilink;
96
97 ilink = (inet_link_t *)iplink_get_userptr(iplink);
98
99 switch (ver) {
100 case ip_v4:
101 rc = inet_pdu_decode(sdu->data, sdu->size, ilink->svc_id,
102 &packet);
103 break;
104 case ip_v6:
105 rc = inet_pdu_decode6(sdu->data, sdu->size, ilink->svc_id,
106 &packet);
107 break;
108 default:
109 log_msg(LOG_DEFAULT, LVL_DEBUG, "invalid IP version");
110 return EINVAL;
111 }
112
113 if (rc != EOK) {
114 log_msg(LOG_DEFAULT, LVL_DEBUG, "failed decoding PDU");
115 return rc;
116 }
117
118 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_iplink_recv: link_id=%zu", packet.link_id);
119 log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet()");
120 rc = inet_recv_packet(&packet);
121 log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet -> %s", str_error_name(rc));
122 free(packet.data);
123
124 return rc;
125}
126
127static errno_t inet_iplink_change_addr(iplink_t *iplink, eth_addr_t *mac)
128{
129 eth_addr_str_t saddr;
130
131 eth_addr_format(mac, &saddr);
132 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_iplink_change_addr(): "
133 "new addr=%s", saddr.str);
134
135 list_foreach(inet_links, link_list, inet_link_t, ilink) {
136 if (ilink->sess == iplink->sess)
137 ilink->mac = *mac;
138 }
139
140 return EOK;
141}
142
143static inet_link_t *inet_link_new(void)
144{
145 inet_link_t *ilink = calloc(1, sizeof(inet_link_t));
146
147 if (ilink == NULL) {
148 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating link structure. "
149 "Out of memory.");
150 return NULL;
151 }
152
153 link_initialize(&ilink->link_list);
154
155 return ilink;
156}
157
158static void inet_link_delete(inet_link_t *ilink)
159{
160 if (ilink->svc_name != NULL)
161 free(ilink->svc_name);
162
163 free(ilink);
164}
165
166errno_t inet_link_open(service_id_t sid)
167{
168 inet_link_t *ilink;
169 inet_addr_t iaddr;
170 errno_t rc;
171
172 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_link_open()");
173 ilink = inet_link_new();
174 if (ilink == NULL)
175 return ENOMEM;
176
177 ilink->svc_id = sid;
178 ilink->iplink = NULL;
179
180 rc = loc_service_get_name(sid, &ilink->svc_name);
181 if (rc != EOK) {
182 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
183 goto error;
184 }
185
186 ilink->sess = loc_service_connect(sid, INTERFACE_IPLINK, 0);
187 if (ilink->sess == NULL) {
188 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting '%s'", ilink->svc_name);
189 goto error;
190 }
191
192 rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, ilink, &ilink->iplink);
193 if (rc != EOK) {
194 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed opening IP link '%s'",
195 ilink->svc_name);
196 goto error;
197 }
198
199 rc = iplink_get_mtu(ilink->iplink, &ilink->def_mtu);
200 if (rc != EOK) {
201 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed determinning MTU of link '%s'",
202 ilink->svc_name);
203 goto error;
204 }
205
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);
212
213 log_msg(LOG_DEFAULT, LVL_DEBUG, "Opened IP link '%s'", ilink->svc_name);
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
225 list_append(&ilink->link_list, &inet_links);
226 fibril_mutex_unlock(&inet_links_lock);
227
228 inet_addrobj_t *addr = NULL;
229
230 /* XXX FIXME Cannot rely on loopback being the first IP link service!! */
231 if (first_link) {
232 addr = inet_addrobj_new();
233
234 inet_naddr(&addr->naddr, 127, 0, 0, 1, 24);
235 first_link = false;
236 }
237
238 if (addr != NULL) {
239 addr->ilink = ilink;
240 addr->name = str_dup("v4a");
241
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 }
256 }
257
258 inet_addrobj_t *addr6 = NULL;
259
260 if (first_link6) {
261 addr6 = inet_addrobj_new();
262
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();
267
268 addr128_t link_local;
269 inet_link_local_node_ip(&ilink->mac, link_local);
270
271 inet_naddr_set6(link_local, 64, &addr6->naddr);
272 }
273
274 if (addr6 != NULL) {
275 addr6->ilink = ilink;
276 addr6->name = str_dup("v6a");
277
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 }
292 }
293
294 log_msg(LOG_DEFAULT, LVL_DEBUG, "Configured link '%s'.", ilink->svc_name);
295 return EOK;
296
297error:
298 if (ilink->iplink != NULL)
299 iplink_close(ilink->iplink);
300
301 inet_link_delete(ilink);
302 return rc;
303}
304
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 */
320errno_t inet_link_send_dgram(inet_link_t *ilink, addr32_t lsrc, addr32_t ldest,
321 inet_dgram_t *dgram, uint8_t proto, uint8_t ttl, int df)
322{
323 addr32_t src_v4;
324 ip_ver_t src_ver = inet_addr_get(&dgram->src, &src_v4, NULL);
325 if (src_ver != ip_v4)
326 return EINVAL;
327
328 addr32_t dest_v4;
329 ip_ver_t dest_ver = inet_addr_get(&dgram->dest, &dest_v4, NULL);
330 if (dest_ver != ip_v4)
331 return EINVAL;
332
333 /*
334 * Fill packet structure. Fragmentation is performed by
335 * inet_pdu_encode().
336 */
337
338 iplink_sdu_t sdu;
339
340 sdu.src = lsrc;
341 sdu.dest = ldest;
342
343 inet_packet_t packet;
344
345 packet.src = dgram->src;
346 packet.dest = dgram->dest;
347 packet.tos = dgram->tos;
348 packet.proto = proto;
349 packet.ttl = ttl;
350
351 /* Allocate identifier */
352 fibril_mutex_lock(&ip_ident_lock);
353 packet.ident = ++ip_ident;
354 fibril_mutex_unlock(&ip_ident_lock);
355
356 packet.df = df;
357 packet.data = dgram->data;
358 packet.size = dgram->size;
359
360 errno_t rc;
361 size_t offs = 0;
362
363 do {
364 /* Encode one fragment */
365
366 size_t roffs;
367 rc = inet_pdu_encode(&packet, src_v4, dest_v4, offs, ilink->def_mtu,
368 &sdu.data, &sdu.size, &roffs);
369 if (rc != EOK)
370 return rc;
371
372 /* Send the PDU */
373 rc = iplink_send(ilink->iplink, &sdu);
374
375 free(sdu.data);
376 offs = roffs;
377 } while (offs < packet.size);
378
379 return rc;
380}
381
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 */
395errno_t inet_link_send_dgram6(inet_link_t *ilink, eth_addr_t *ldest,
396 inet_dgram_t *dgram, uint8_t proto, uint8_t ttl, int df)
397{
398 addr128_t src_v6;
399 ip_ver_t src_ver = inet_addr_get(&dgram->src, NULL, &src_v6);
400 if (src_ver != ip_v6)
401 return EINVAL;
402
403 addr128_t dest_v6;
404 ip_ver_t dest_ver = inet_addr_get(&dgram->dest, NULL, &dest_v6);
405 if (dest_ver != ip_v6)
406 return EINVAL;
407
408 iplink_sdu6_t sdu6;
409 sdu6.dest = *ldest;
410
411 /*
412 * Fill packet structure. Fragmentation is performed by
413 * inet_pdu_encode6().
414 */
415
416 inet_packet_t packet;
417
418 packet.src = dgram->src;
419 packet.dest = dgram->dest;
420 packet.tos = dgram->tos;
421 packet.proto = proto;
422 packet.ttl = ttl;
423
424 /* Allocate identifier */
425 fibril_mutex_lock(&ip_ident_lock);
426 packet.ident = ++ip_ident;
427 fibril_mutex_unlock(&ip_ident_lock);
428
429 packet.df = df;
430 packet.data = dgram->data;
431 packet.size = dgram->size;
432
433 errno_t rc;
434 size_t offs = 0;
435
436 do {
437 /* Encode one fragment */
438
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;
444
445 /* Send the PDU */
446 rc = iplink_send6(ilink->iplink, &sdu6);
447
448 free(sdu6.data);
449 offs = roffs;
450 } while (offs < packet.size);
451
452 return rc;
453}
454
455static inet_link_t *inet_link_get_by_id_locked(sysarg_t link_id)
456{
457 assert(fibril_mutex_is_locked(&inet_links_lock));
458
459 list_foreach(inet_links, link_list, inet_link_t, ilink) {
460 if (ilink->svc_id == link_id)
461 return ilink;
462 }
463
464 return NULL;
465}
466
467inet_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
478/** Get IDs of all links. */
479errno_t inet_link_get_id_list(sysarg_t **rid_list, size_t *rcount)
480{
481 sysarg_t *id_list;
482 size_t count, i;
483
484 fibril_mutex_lock(&inet_links_lock);
485 count = list_count(&inet_links);
486
487 id_list = calloc(count, sizeof(sysarg_t));
488 if (id_list == NULL) {
489 fibril_mutex_unlock(&inet_links_lock);
490 return ENOMEM;
491 }
492
493 i = 0;
494 list_foreach(inet_links, link_list, inet_link_t, ilink) {
495 id_list[i++] = ilink->svc_id;
496 }
497
498 fibril_mutex_unlock(&inet_links_lock);
499
500 *rid_list = id_list;
501 *rcount = count;
502
503 return EOK;
504}
505
506/** @}
507 */
Note: See TracBrowser for help on using the repository browser.