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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c0f3460 was 3aece36, checked in by Martin Decky <martin@…>, 12 years ago

configure a link-local IPv6 address instead of a static IPv6 address

  • Property mode set to 100644
File size: 10.9 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 <net/socket_codes.h>
46#include "addrobj.h"
47#include "inetsrv.h"
48#include "inet_link.h"
49#include "pdu.h"
50
51static bool first_link = true;
52static bool first_link6 = true;
53
54static int inet_link_open(service_id_t);
55static int inet_iplink_recv(iplink_t *, iplink_recv_sdu_t *, uint16_t);
56
57static iplink_ev_ops_t inet_iplink_ev_ops = {
58 .recv = inet_iplink_recv
59};
60
61static LIST_INITIALIZE(inet_link_list);
62static FIBRIL_MUTEX_INITIALIZE(inet_discovery_lock);
63
64static addr128_t link_local_node_ip =
65 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xfe, 0, 0, 0};
66
67static void inet_link_local_node_ip(addr48_t mac_addr,
68 addr128_t ip_addr)
69{
70 memcpy(ip_addr, link_local_node_ip, 16);
71
72 ip_addr[8] = mac_addr[0] ^ 0x02;
73 ip_addr[9] = mac_addr[1];
74 ip_addr[10] = mac_addr[2];
75 ip_addr[13] = mac_addr[3];
76 ip_addr[14] = mac_addr[4];
77 ip_addr[15] = mac_addr[5];
78}
79
80static int inet_iplink_recv(iplink_t *iplink, iplink_recv_sdu_t *sdu, uint16_t af)
81{
82 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_iplink_recv()");
83
84 int rc;
85 inet_packet_t packet;
86
87 switch (af) {
88 case AF_INET:
89 rc = inet_pdu_decode(sdu->data, sdu->size, &packet);
90 break;
91 case AF_INET6:
92 rc = inet_pdu_decode6(sdu->data, sdu->size, &packet);
93 break;
94 default:
95 log_msg(LOG_DEFAULT, LVL_DEBUG, "invalid address family");
96 return EINVAL;
97 }
98
99 if (rc != EOK) {
100 log_msg(LOG_DEFAULT, LVL_DEBUG, "failed decoding PDU");
101 return rc;
102 }
103
104 log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet()");
105 rc = inet_recv_packet(&packet);
106 log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet -> %d", rc);
107 free(packet.data);
108
109 return rc;
110}
111
112static int inet_link_check_new(void)
113{
114 bool already_known;
115 category_id_t iplink_cat;
116 service_id_t *svcs;
117 size_t count, i;
118 int rc;
119
120 fibril_mutex_lock(&inet_discovery_lock);
121
122 rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
123 if (rc != EOK) {
124 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
125 fibril_mutex_unlock(&inet_discovery_lock);
126 return ENOENT;
127 }
128
129 rc = loc_category_get_svcs(iplink_cat, &svcs, &count);
130 if (rc != EOK) {
131 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting list of IP links.");
132 fibril_mutex_unlock(&inet_discovery_lock);
133 return EIO;
134 }
135
136 for (i = 0; i < count; i++) {
137 already_known = false;
138
139 list_foreach(inet_link_list, ilink_link) {
140 inet_link_t *ilink = list_get_instance(ilink_link,
141 inet_link_t, link_list);
142 if (ilink->svc_id == svcs[i]) {
143 already_known = true;
144 break;
145 }
146 }
147
148 if (!already_known) {
149 log_msg(LOG_DEFAULT, LVL_DEBUG, "Found IP link '%lu'",
150 (unsigned long) svcs[i]);
151 rc = inet_link_open(svcs[i]);
152 if (rc != EOK)
153 log_msg(LOG_DEFAULT, LVL_ERROR, "Could not open IP link.");
154 }
155 }
156
157 fibril_mutex_unlock(&inet_discovery_lock);
158 return EOK;
159}
160
161static inet_link_t *inet_link_new(void)
162{
163 inet_link_t *ilink = calloc(1, sizeof(inet_link_t));
164
165 if (ilink == NULL) {
166 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating link structure. "
167 "Out of memory.");
168 return NULL;
169 }
170
171 link_initialize(&ilink->link_list);
172
173 return ilink;
174}
175
176static void inet_link_delete(inet_link_t *ilink)
177{
178 if (ilink->svc_name != NULL)
179 free(ilink->svc_name);
180
181 free(ilink);
182}
183
184static int inet_link_open(service_id_t sid)
185{
186 inet_link_t *ilink;
187 inet_addr_t iaddr;
188 int rc;
189
190 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_link_open()");
191 ilink = inet_link_new();
192 if (ilink == NULL)
193 return ENOMEM;
194
195 ilink->svc_id = sid;
196 ilink->iplink = NULL;
197
198 rc = loc_service_get_name(sid, &ilink->svc_name);
199 if (rc != EOK) {
200 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
201 goto error;
202 }
203
204 ilink->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
205 if (ilink->sess == NULL) {
206 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting '%s'", ilink->svc_name);
207 goto error;
208 }
209
210 rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, &ilink->iplink);
211 if (rc != EOK) {
212 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed opening IP link '%s'",
213 ilink->svc_name);
214 goto error;
215 }
216
217 rc = iplink_get_mtu(ilink->iplink, &ilink->def_mtu);
218 if (rc != EOK) {
219 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed determinning MTU of link '%s'",
220 ilink->svc_name);
221 goto error;
222 }
223
224 /*
225 * Get the MAC address of the link. If the link has a MAC
226 * address, we assume that it supports NDP.
227 */
228 rc = iplink_get_mac48(ilink->iplink, &ilink->mac);
229 ilink->mac_valid = (rc == EOK);
230
231 log_msg(LOG_DEFAULT, LVL_DEBUG, "Opened IP link '%s'", ilink->svc_name);
232 list_append(&ilink->link_list, &inet_link_list);
233
234 inet_addrobj_t *addr = NULL;
235
236 if (first_link) {
237 addr = inet_addrobj_new();
238
239 inet_naddr(&addr->naddr, 127, 0, 0, 1, 24);
240 first_link = false;
241 } else {
242 /*
243 * FIXME
244 * Setting static IPv4 address for testing purposes:
245 * 10.0.2.15/24
246 */
247 addr = inet_addrobj_new();
248
249 inet_naddr(&addr->naddr, 10, 0, 2, 15, 24);
250 }
251
252 if (addr != NULL) {
253 addr->ilink = ilink;
254 addr->name = str_dup("v4a");
255
256 rc = inet_addrobj_add(addr);
257 if (rc == EOK) {
258 inet_naddr_addr(&addr->naddr, &iaddr);
259 rc = iplink_addr_add(ilink->iplink, &iaddr);
260 if (rc != EOK) {
261 log_msg(LOG_DEFAULT, LVL_ERROR,
262 "Failed setting IPv4 address on internet link.");
263 inet_addrobj_remove(addr);
264 inet_addrobj_delete(addr);
265 }
266 } else {
267 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding IPv4 address.");
268 inet_addrobj_delete(addr);
269 }
270 }
271
272 inet_addrobj_t *addr6 = NULL;
273
274 if (first_link6) {
275 addr6 = inet_addrobj_new();
276
277 inet_naddr6(&addr6->naddr, 0, 0, 0, 0, 0, 0, 0, 1, 128);
278 first_link6 = false;
279 } else if (ilink->mac_valid) {
280 addr6 = inet_addrobj_new();
281
282 addr128_t link_local;
283 inet_link_local_node_ip(ilink->mac, link_local);
284
285 inet_naddr_set6(link_local, 64, &addr6->naddr);
286 }
287
288 if (addr6 != NULL) {
289 addr6->ilink = ilink;
290 addr6->name = str_dup("v6a");
291
292 rc = inet_addrobj_add(addr6);
293 if (rc == EOK) {
294 inet_naddr_addr(&addr6->naddr, &iaddr);
295 rc = iplink_addr_add(ilink->iplink, &iaddr);
296 if (rc != EOK) {
297 log_msg(LOG_DEFAULT, LVL_ERROR,
298 "Failed setting IPv6 address on internet link.");
299 inet_addrobj_remove(addr6);
300 inet_addrobj_delete(addr6);
301 }
302 } else {
303 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding IPv6 address.");
304 inet_addrobj_delete(addr6);
305 }
306 }
307
308 return EOK;
309
310error:
311 if (ilink->iplink != NULL)
312 iplink_close(ilink->iplink);
313
314 inet_link_delete(ilink);
315 return rc;
316}
317
318static void inet_link_cat_change_cb(void)
319{
320 (void) inet_link_check_new();
321}
322
323int inet_link_discovery_start(void)
324{
325 int rc;
326
327 rc = loc_register_cat_change_cb(inet_link_cat_change_cb);
328 if (rc != EOK) {
329 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback for IP link "
330 "discovery (%d).", rc);
331 return rc;
332 }
333
334 return inet_link_check_new();
335}
336
337/** Send IPv4 datagram over Internet link */
338int inet_link_send_dgram(inet_link_t *ilink, addr32_t lsrc, addr32_t ldest,
339 inet_dgram_t *dgram, uint8_t proto, uint8_t ttl, int df)
340{
341 addr32_t src_v4;
342 uint16_t src_af = inet_addr_get(&dgram->src, &src_v4, NULL);
343 if (src_af != AF_INET)
344 return EINVAL;
345
346 addr32_t dest_v4;
347 uint16_t dest_af = inet_addr_get(&dgram->dest, &dest_v4, NULL);
348 if (dest_af != AF_INET)
349 return EINVAL;
350
351 /*
352 * Fill packet structure. Fragmentation is performed by
353 * inet_pdu_encode().
354 */
355
356 iplink_sdu_t sdu;
357
358 sdu.src = lsrc;
359 sdu.dest = ldest;
360
361 inet_packet_t packet;
362
363 packet.src = dgram->src;
364 packet.dest = dgram->dest;
365 packet.tos = dgram->tos;
366 packet.proto = proto;
367 packet.ttl = ttl;
368 packet.df = df;
369 packet.data = dgram->data;
370 packet.size = dgram->size;
371
372 size_t offs = 0;
373 int rc;
374
375 do {
376 /* Encode one fragment */
377
378 size_t roffs;
379 rc = inet_pdu_encode(&packet, src_v4, dest_v4, offs, ilink->def_mtu,
380 &sdu.data, &sdu.size, &roffs);
381 if (rc != EOK)
382 return rc;
383
384 /* Send the PDU */
385 rc = iplink_send(ilink->iplink, &sdu);
386
387 free(sdu.data);
388 offs = roffs;
389 } while (offs < packet.size);
390
391 return rc;
392}
393
394/** Send IPv6 datagram over Internet link */
395int inet_link_send_dgram6(inet_link_t *ilink, addr48_t ldest,
396 inet_dgram_t *dgram, uint8_t proto, uint8_t ttl, int df)
397{
398 addr128_t src_v6;
399 uint16_t src_af = inet_addr_get(&dgram->src, NULL, &src_v6);
400 if (src_af != AF_INET6)
401 return EINVAL;
402
403 addr128_t dest_v6;
404 uint16_t dest_af = inet_addr_get(&dgram->dest, NULL, &dest_v6);
405 if (dest_af != AF_INET6)
406 return EINVAL;
407
408 iplink_sdu6_t sdu6;
409 addr48(ldest, sdu6.dest);
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 packet.df = df;
424 packet.data = dgram->data;
425 packet.size = dgram->size;
426
427 int rc;
428 size_t offs = 0;
429
430 do {
431 /* Encode one fragment */
432
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;
438
439 /* Send the PDU */
440 rc = iplink_send6(ilink->iplink, &sdu6);
441
442 free(sdu6.data);
443 offs = roffs;
444 } while (offs < packet.size);
445
446 return rc;
447}
448
449inet_link_t *inet_link_get_by_id(sysarg_t link_id)
450{
451 fibril_mutex_lock(&inet_discovery_lock);
452
453 list_foreach(inet_link_list, elem) {
454 inet_link_t *ilink = list_get_instance(elem, inet_link_t,
455 link_list);
456
457 if (ilink->svc_id == link_id) {
458 fibril_mutex_unlock(&inet_discovery_lock);
459 return ilink;
460 }
461 }
462
463 fibril_mutex_unlock(&inet_discovery_lock);
464 return NULL;
465}
466
467/** @}
468 */
Note: See TracBrowser for help on using the repository browser.