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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c3b25985 was c3b25985, checked in by Agnieszka Tabaka <nufcia@…>, 11 years ago

Add support for actual MAC change in networking stack.

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