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

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

Add link listing to inet utility, showing MAC address.

  • Property mode set to 100644
File size: 12.3 KB
RevLine 
[e2e56e67]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
[3e6a98c5]37#include <stdbool.h>
[e2e56e67]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>
[0e94b979]44#include <str.h>
[02a09ed]45#include <net/socket_codes.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
[02a09ed]57static int inet_link_open(service_id_t);
58static int inet_iplink_recv(iplink_t *, iplink_recv_sdu_t *, uint16_t);
[e2e56e67]59
60static iplink_ev_ops_t inet_iplink_ev_ops = {
61 .recv = inet_iplink_recv
62};
63
64static LIST_INITIALIZE(inet_link_list);
65static FIBRIL_MUTEX_INITIALIZE(inet_discovery_lock);
66
[3aece36]67static addr128_t link_local_node_ip =
68 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xfe, 0, 0, 0};
69
70static 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
[02a09ed]83static int inet_iplink_recv(iplink_t *iplink, iplink_recv_sdu_t *sdu, uint16_t af)
[e2e56e67]84{
[a1a101d]85 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_iplink_recv()");
[02a09ed]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:
[1d24ad3]95 rc = inet_pdu_decode6(sdu->data, sdu->size, &packet);
96 break;
[02a09ed]97 default:
98 log_msg(LOG_DEFAULT, LVL_DEBUG, "invalid address family");
99 return EINVAL;
100 }
101
[fe4310f]102 if (rc != EOK) {
[a1a101d]103 log_msg(LOG_DEFAULT, LVL_DEBUG, "failed decoding PDU");
[e767dbf]104 return rc;
[fe4310f]105 }
[257feec]106
[a1a101d]107 log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet()");
[fe4310f]108 rc = inet_recv_packet(&packet);
[a1a101d]109 log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet -> %d", rc);
[7f95c904]110 free(packet.data);
[257feec]111
[fe4310f]112 return rc;
[e2e56e67]113}
114
115static 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) {
[a1a101d]127 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
[e2e56e67]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) {
[a1a101d]134 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting list of IP links.");
[e2e56e67]135 fibril_mutex_unlock(&inet_discovery_lock);
136 return EIO;
137 }
138
139 for (i = 0; i < count; i++) {
140 already_known = false;
141
[feeac0d]142 list_foreach(inet_link_list, link_list, inet_link_t, ilink) {
[e2e56e67]143 if (ilink->svc_id == svcs[i]) {
144 already_known = true;
145 break;
146 }
147 }
148
149 if (!already_known) {
[a1a101d]150 log_msg(LOG_DEFAULT, LVL_DEBUG, "Found IP link '%lu'",
[e2e56e67]151 (unsigned long) svcs[i]);
152 rc = inet_link_open(svcs[i]);
153 if (rc != EOK)
[a1a101d]154 log_msg(LOG_DEFAULT, LVL_ERROR, "Could not open IP link.");
[e2e56e67]155 }
156 }
157
158 fibril_mutex_unlock(&inet_discovery_lock);
159 return EOK;
160}
161
162static inet_link_t *inet_link_new(void)
163{
164 inet_link_t *ilink = calloc(1, sizeof(inet_link_t));
165
166 if (ilink == NULL) {
[a1a101d]167 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating link structure. "
[e2e56e67]168 "Out of memory.");
169 return NULL;
170 }
171
172 link_initialize(&ilink->link_list);
173
174 return ilink;
175}
176
177static void inet_link_delete(inet_link_t *ilink)
178{
179 if (ilink->svc_name != NULL)
180 free(ilink->svc_name);
[3aece36]181
[e2e56e67]182 free(ilink);
183}
184
185static int inet_link_open(service_id_t sid)
186{
187 inet_link_t *ilink;
[a2e3ee6]188 inet_addr_t iaddr;
[e2e56e67]189 int rc;
190
[a1a101d]191 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_link_open()");
[e2e56e67]192 ilink = inet_link_new();
193 if (ilink == NULL)
194 return ENOMEM;
195
[45aa22c]196 ilink->svc_id = sid;
[347768d]197 ilink->iplink = NULL;
[45aa22c]198
[e2e56e67]199 rc = loc_service_get_name(sid, &ilink->svc_name);
200 if (rc != EOK) {
[a1a101d]201 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
[e2e56e67]202 goto error;
203 }
204
205 ilink->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
206 if (ilink->sess == NULL) {
[a1a101d]207 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting '%s'", ilink->svc_name);
[e2e56e67]208 goto error;
209 }
210
211 rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, &ilink->iplink);
212 if (rc != EOK) {
[a1a101d]213 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed opening IP link '%s'",
[e2e56e67]214 ilink->svc_name);
215 goto error;
216 }
217
[347768d]218 rc = iplink_get_mtu(ilink->iplink, &ilink->def_mtu);
219 if (rc != EOK) {
[a1a101d]220 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed determinning MTU of link '%s'",
[347768d]221 ilink->svc_name);
222 goto error;
223 }
[a17356fd]224
225 /*
226 * Get the MAC address of the link. If the link has a MAC
227 * address, we assume that it supports NDP.
228 */
229 rc = iplink_get_mac48(ilink->iplink, &ilink->mac);
230 ilink->mac_valid = (rc == EOK);
[347768d]231
[a1a101d]232 log_msg(LOG_DEFAULT, LVL_DEBUG, "Opened IP link '%s'", ilink->svc_name);
[e2e56e67]233 list_append(&ilink->link_list, &inet_link_list);
234
[3aece36]235 inet_addrobj_t *addr = NULL;
[a2e3ee6]236
[695b6ff]237 /* XXX FIXME Cannot rely on loopback being the first IP link service!! */
[3aece36]238 if (first_link) {
239 addr = inet_addrobj_new();
240
[a2e3ee6]241 inet_naddr(&addr->naddr, 127, 0, 0, 1, 24);
[3aece36]242 first_link = false;
[081971b]243 }
[a2e3ee6]244
[3aece36]245 if (addr != NULL) {
246 addr->ilink = ilink;
247 addr->name = str_dup("v4a");
248
249 rc = inet_addrobj_add(addr);
250 if (rc == EOK) {
251 inet_naddr_addr(&addr->naddr, &iaddr);
252 rc = iplink_addr_add(ilink->iplink, &iaddr);
253 if (rc != EOK) {
254 log_msg(LOG_DEFAULT, LVL_ERROR,
255 "Failed setting IPv4 address on internet link.");
256 inet_addrobj_remove(addr);
257 inet_addrobj_delete(addr);
258 }
259 } else {
260 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding IPv4 address.");
261 inet_addrobj_delete(addr);
262 }
[bf9e6fc]263 }
[1d24ad3]264
[3aece36]265 inet_addrobj_t *addr6 = NULL;
[1d24ad3]266
[3aece36]267 if (first_link6) {
268 addr6 = inet_addrobj_new();
269
270 inet_naddr6(&addr6->naddr, 0, 0, 0, 0, 0, 0, 0, 1, 128);
271 first_link6 = false;
272 } else if (ilink->mac_valid) {
273 addr6 = inet_addrobj_new();
274
275 addr128_t link_local;
276 inet_link_local_node_ip(ilink->mac, link_local);
277
278 inet_naddr_set6(link_local, 64, &addr6->naddr);
[962f03b]279 }
[1d24ad3]280
[3aece36]281 if (addr6 != NULL) {
282 addr6->ilink = ilink;
283 addr6->name = str_dup("v6a");
284
285 rc = inet_addrobj_add(addr6);
286 if (rc == EOK) {
287 inet_naddr_addr(&addr6->naddr, &iaddr);
288 rc = iplink_addr_add(ilink->iplink, &iaddr);
289 if (rc != EOK) {
290 log_msg(LOG_DEFAULT, LVL_ERROR,
291 "Failed setting IPv6 address on internet link.");
292 inet_addrobj_remove(addr6);
293 inet_addrobj_delete(addr6);
294 }
295 } else {
296 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding IPv6 address.");
297 inet_addrobj_delete(addr6);
298 }
[1d24ad3]299 }
300
[e2e56e67]301 return EOK;
[02a09ed]302
[e2e56e67]303error:
[347768d]304 if (ilink->iplink != NULL)
305 iplink_close(ilink->iplink);
[257feec]306
[e2e56e67]307 inet_link_delete(ilink);
308 return rc;
309}
310
311static void inet_link_cat_change_cb(void)
312{
313 (void) inet_link_check_new();
314}
315
316int inet_link_discovery_start(void)
317{
318 int rc;
319
320 rc = loc_register_cat_change_cb(inet_link_cat_change_cb);
321 if (rc != EOK) {
[a1a101d]322 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback for IP link "
[e2e56e67]323 "discovery (%d).", rc);
324 return rc;
325 }
326
327 return inet_link_check_new();
328}
329
[1f97352]330/** Send IPv4 datagram over Internet link
331 *
332 * @param ilink Internet link
333 * @param lsrc Source IPv4 address
334 * @param ldest Destination IPv4 address
335 * @param dgram IPv4 datagram body
336 * @param proto Protocol
337 * @param ttl Time-to-live
338 * @param df Do-not-Fragment flag
339 *
340 * @return EOK on success
341 * @return ENOMEM when not enough memory to create the datagram
342 * @return ENOTSUP if networking mode is not supported
343 *
344 */
[a17356fd]345int inet_link_send_dgram(inet_link_t *ilink, addr32_t lsrc, addr32_t ldest,
346 inet_dgram_t *dgram, uint8_t proto, uint8_t ttl, int df)
[ceba4bed]347{
[a17356fd]348 addr32_t src_v4;
349 uint16_t src_af = inet_addr_get(&dgram->src, &src_v4, NULL);
350 if (src_af != AF_INET)
351 return EINVAL;
352
353 addr32_t dest_v4;
354 uint16_t dest_af = inet_addr_get(&dgram->dest, &dest_v4, NULL);
355 if (dest_af != AF_INET)
356 return EINVAL;
357
[347768d]358 /*
359 * Fill packet structure. Fragmentation is performed by
360 * inet_pdu_encode().
361 */
[257feec]362
[a17356fd]363 iplink_sdu_t sdu;
364
365 sdu.src = lsrc;
366 sdu.dest = ldest;
367
[a2e3ee6]368 inet_packet_t packet;
369
[fe4310f]370 packet.src = dgram->src;
371 packet.dest = dgram->dest;
372 packet.tos = dgram->tos;
[2ff150e]373 packet.proto = proto;
[fe4310f]374 packet.ttl = ttl;
[313824a]375
376 /* Allocate identifier */
377 fibril_mutex_lock(&ip_ident_lock);
378 packet.ident = ++ip_ident;
379 fibril_mutex_unlock(&ip_ident_lock);
380
[fe4310f]381 packet.df = df;
382 packet.data = dgram->data;
383 packet.size = dgram->size;
[a2e3ee6]384
[02a09ed]385 int rc;
[313824a]386 size_t offs = 0;
[a2e3ee6]387
[347768d]388 do {
389 /* Encode one fragment */
[a17356fd]390
[02a09ed]391 size_t roffs;
[a17356fd]392 rc = inet_pdu_encode(&packet, src_v4, dest_v4, offs, ilink->def_mtu,
393 &sdu.data, &sdu.size, &roffs);
[347768d]394 if (rc != EOK)
395 return rc;
[a2e3ee6]396
[347768d]397 /* Send the PDU */
398 rc = iplink_send(ilink->iplink, &sdu);
[a17356fd]399
[347768d]400 free(sdu.data);
[a17356fd]401 offs = roffs;
402 } while (offs < packet.size);
403
404 return rc;
405}
406
[1f97352]407/** Send IPv6 datagram over Internet link
408 *
409 * @param ilink Internet link
410 * @param ldest Destination MAC address
411 * @param dgram IPv6 datagram body
412 * @param proto Next header
413 * @param ttl Hop limit
414 * @param df Do-not-Fragment flag (unused)
415 *
416 * @return EOK on success
417 * @return ENOMEM when not enough memory to create the datagram
418 *
419 */
[a17356fd]420int inet_link_send_dgram6(inet_link_t *ilink, addr48_t ldest,
421 inet_dgram_t *dgram, uint8_t proto, uint8_t ttl, int df)
422{
423 addr128_t src_v6;
424 uint16_t src_af = inet_addr_get(&dgram->src, NULL, &src_v6);
425 if (src_af != AF_INET6)
426 return EINVAL;
427
428 addr128_t dest_v6;
429 uint16_t dest_af = inet_addr_get(&dgram->dest, NULL, &dest_v6);
430 if (dest_af != AF_INET6)
431 return EINVAL;
432
433 iplink_sdu6_t sdu6;
434 addr48(ldest, sdu6.dest);
435
436 /*
437 * Fill packet structure. Fragmentation is performed by
438 * inet_pdu_encode6().
439 */
440
441 inet_packet_t packet;
442
443 packet.src = dgram->src;
444 packet.dest = dgram->dest;
445 packet.tos = dgram->tos;
446 packet.proto = proto;
447 packet.ttl = ttl;
[313824a]448
449 /* Allocate identifier */
450 fibril_mutex_lock(&ip_ident_lock);
451 packet.ident = ++ip_ident;
452 fibril_mutex_unlock(&ip_ident_lock);
453
[a17356fd]454 packet.df = df;
455 packet.data = dgram->data;
456 packet.size = dgram->size;
457
458 int rc;
459 size_t offs = 0;
460
461 do {
462 /* Encode one fragment */
463
464 size_t roffs;
465 rc = inet_pdu_encode6(&packet, src_v6, dest_v6, offs, ilink->def_mtu,
466 &sdu6.data, &sdu6.size, &roffs);
467 if (rc != EOK)
468 return rc;
469
470 /* Send the PDU */
471 rc = iplink_send6(ilink->iplink, &sdu6);
[a2e3ee6]472
[a17356fd]473 free(sdu6.data);
[347768d]474 offs = roffs;
475 } while (offs < packet.size);
[a2e3ee6]476
[ceba4bed]477 return rc;
478}
479
[45aa22c]480inet_link_t *inet_link_get_by_id(sysarg_t link_id)
481{
482 fibril_mutex_lock(&inet_discovery_lock);
483
[feeac0d]484 list_foreach(inet_link_list, link_list, inet_link_t, ilink) {
[45aa22c]485 if (ilink->svc_id == link_id) {
486 fibril_mutex_unlock(&inet_discovery_lock);
487 return ilink;
488 }
489 }
490
491 fibril_mutex_unlock(&inet_discovery_lock);
492 return NULL;
493}
[ceba4bed]494
[b8b1adb1]495/** Get IDs of all links. */
496int inet_link_get_id_list(sysarg_t **rid_list, size_t *rcount)
497{
498 sysarg_t *id_list;
499 size_t count, i;
500
501 fibril_mutex_lock(&inet_discovery_lock);
502 count = list_count(&inet_link_list);
503
504 id_list = calloc(count, sizeof(sysarg_t));
505 if (id_list == NULL) {
506 fibril_mutex_unlock(&inet_discovery_lock);
507 return ENOMEM;
508 }
509
510 i = 0;
511 list_foreach(inet_link_list, link_list, inet_link_t, ilink) {
512 id_list[i++] = ilink->svc_id;
513 log_msg(LOG_DEFAULT, LVL_NOTE, "add link to list");
514 }
515
516 fibril_mutex_unlock(&inet_discovery_lock);
517
518 log_msg(LOG_DEFAULT, LVL_NOTE, "return %zu links", count);
519 *rid_list = id_list;
520 *rcount = count;
521
522 return EOK;
523}
524
[e2e56e67]525/** @}
526 */
Note: See TracBrowser for help on using the repository browser.