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
Line 
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
37#include <stdbool.h>
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>
44#include <str.h>
45#include "addrobj.h"
46#include "inetsrv.h"
47#include "inet_link.h"
48#include "pdu.h"
49
50static bool first_link = true;
51static bool first_link6 = true;
52
53static FIBRIL_MUTEX_INITIALIZE(ip_ident_lock);
54static uint16_t ip_ident = 0;
55
56static int inet_iplink_recv(iplink_t *, iplink_recv_sdu_t *, ip_ver_t);
57static int inet_iplink_change_addr(iplink_t *, addr48_t);
58static inet_link_t *inet_link_get_by_id_locked(sysarg_t);
59
60static iplink_ev_ops_t inet_iplink_ev_ops = {
61 .recv = inet_iplink_recv,
62 .change_addr = inet_iplink_change_addr,
63};
64
65static LIST_INITIALIZE(inet_links);
66static FIBRIL_MUTEX_INITIALIZE(inet_links_lock);
67
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
84static int inet_iplink_recv(iplink_t *iplink, iplink_recv_sdu_t *sdu, ip_ver_t ver)
85{
86 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_iplink_recv()");
87
88 int rc;
89 inet_packet_t packet;
90
91 switch (ver) {
92 case ip_v4:
93 rc = inet_pdu_decode(sdu->data, sdu->size, &packet);
94 break;
95 case ip_v6:
96 rc = inet_pdu_decode6(sdu->data, sdu->size, &packet);
97 break;
98 default:
99 log_msg(LOG_DEFAULT, LVL_DEBUG, "invalid IP version");
100 return EINVAL;
101 }
102
103 if (rc != EOK) {
104 log_msg(LOG_DEFAULT, LVL_DEBUG, "failed decoding PDU");
105 return rc;
106 }
107
108 log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet()");
109 rc = inet_recv_packet(&packet);
110 log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet -> %d", rc);
111 free(packet.data);
112
113 return rc;
114}
115
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
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) {
135 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating link structure. "
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);
149
150 free(ilink);
151}
152
153int inet_link_open(service_id_t sid)
154{
155 inet_link_t *ilink;
156 inet_addr_t iaddr;
157 int rc;
158
159 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_link_open()");
160 ilink = inet_link_new();
161 if (ilink == NULL)
162 return ENOMEM;
163
164 ilink->svc_id = sid;
165 ilink->iplink = NULL;
166
167 rc = loc_service_get_name(sid, &ilink->svc_name);
168 if (rc != EOK) {
169 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
170 goto error;
171 }
172
173 ilink->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
174 if (ilink->sess == NULL) {
175 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting '%s'", ilink->svc_name);
176 goto error;
177 }
178
179 rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, &ilink->iplink);
180 if (rc != EOK) {
181 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed opening IP link '%s'",
182 ilink->svc_name);
183 goto error;
184 }
185
186 rc = iplink_get_mtu(ilink->iplink, &ilink->def_mtu);
187 if (rc != EOK) {
188 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed determinning MTU of link '%s'",
189 ilink->svc_name);
190 goto error;
191 }
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);
199
200 log_msg(LOG_DEFAULT, LVL_DEBUG, "Opened IP link '%s'", ilink->svc_name);
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
212 list_append(&ilink->link_list, &inet_links);
213 fibril_mutex_unlock(&inet_links_lock);
214
215 inet_addrobj_t *addr = NULL;
216
217 /* XXX FIXME Cannot rely on loopback being the first IP link service!! */
218 if (first_link) {
219 addr = inet_addrobj_new();
220
221 inet_naddr(&addr->naddr, 127, 0, 0, 1, 24);
222 first_link = false;
223 }
224
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 }
243 }
244
245 inet_addrobj_t *addr6 = NULL;
246
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);
259 }
260
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 }
279 }
280
281 log_msg(LOG_DEFAULT, LVL_DEBUG, "Configured link '%s'.", ilink->svc_name);
282 return EOK;
283
284error:
285 if (ilink->iplink != NULL)
286 iplink_close(ilink->iplink);
287
288 inet_link_delete(ilink);
289 return rc;
290}
291
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 */
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)
309{
310 addr32_t src_v4;
311 ip_ver_t src_ver = inet_addr_get(&dgram->src, &src_v4, NULL);
312 if (src_ver != ip_v4)
313 return EINVAL;
314
315 addr32_t dest_v4;
316 ip_ver_t dest_ver = inet_addr_get(&dgram->dest, &dest_v4, NULL);
317 if (dest_ver != ip_v4)
318 return EINVAL;
319
320 /*
321 * Fill packet structure. Fragmentation is performed by
322 * inet_pdu_encode().
323 */
324
325 iplink_sdu_t sdu;
326
327 sdu.src = lsrc;
328 sdu.dest = ldest;
329
330 inet_packet_t packet;
331
332 packet.src = dgram->src;
333 packet.dest = dgram->dest;
334 packet.tos = dgram->tos;
335 packet.proto = proto;
336 packet.ttl = ttl;
337
338 /* Allocate identifier */
339 fibril_mutex_lock(&ip_ident_lock);
340 packet.ident = ++ip_ident;
341 fibril_mutex_unlock(&ip_ident_lock);
342
343 packet.df = df;
344 packet.data = dgram->data;
345 packet.size = dgram->size;
346
347 int rc;
348 size_t offs = 0;
349
350 do {
351 /* Encode one fragment */
352
353 size_t roffs;
354 rc = inet_pdu_encode(&packet, src_v4, dest_v4, offs, ilink->def_mtu,
355 &sdu.data, &sdu.size, &roffs);
356 if (rc != EOK)
357 return rc;
358
359 /* Send the PDU */
360 rc = iplink_send(ilink->iplink, &sdu);
361
362 free(sdu.data);
363 offs = roffs;
364 } while (offs < packet.size);
365
366 return rc;
367}
368
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 */
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;
386 ip_ver_t src_ver = inet_addr_get(&dgram->src, NULL, &src_v6);
387 if (src_ver != ip_v6)
388 return EINVAL;
389
390 addr128_t dest_v6;
391 ip_ver_t dest_ver = inet_addr_get(&dgram->dest, NULL, &dest_v6);
392 if (dest_ver != ip_v6)
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;
410
411 /* Allocate identifier */
412 fibril_mutex_lock(&ip_ident_lock);
413 packet.ident = ++ip_ident;
414 fibril_mutex_unlock(&ip_ident_lock);
415
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);
434
435 free(sdu6.data);
436 offs = roffs;
437 } while (offs < packet.size);
438
439 return rc;
440}
441
442static inet_link_t *inet_link_get_by_id_locked(sysarg_t link_id)
443{
444 assert(fibril_mutex_is_locked(&inet_links_lock));
445
446 list_foreach(inet_links, link_list, inet_link_t, ilink) {
447 if (ilink->svc_id == link_id)
448 return ilink;
449 }
450
451 return NULL;
452}
453
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
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
471 fibril_mutex_lock(&inet_links_lock);
472 count = list_count(&inet_links);
473
474 id_list = calloc(count, sizeof(sysarg_t));
475 if (id_list == NULL) {
476 fibril_mutex_unlock(&inet_links_lock);
477 return ENOMEM;
478 }
479
480 i = 0;
481 list_foreach(inet_links, link_list, inet_link_t, ilink) {
482 id_list[i++] = ilink->svc_id;
483 }
484
485 fibril_mutex_unlock(&inet_links_lock);
486
487 *rid_list = id_list;
488 *rcount = count;
489
490 return EOK;
491}
492
493/** @}
494 */
Note: See TracBrowser for help on using the repository browser.