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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 12df1f1 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
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
[02a09ed]54static int inet_link_open(service_id_t);
55static int inet_iplink_recv(iplink_t *, iplink_recv_sdu_t *, uint16_t);
[e2e56e67]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
[3aece36]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
[02a09ed]80static int inet_iplink_recv(iplink_t *iplink, iplink_recv_sdu_t *sdu, uint16_t af)
[e2e56e67]81{
[a1a101d]82 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_iplink_recv()");
[02a09ed]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:
[1d24ad3]92 rc = inet_pdu_decode6(sdu->data, sdu->size, &packet);
93 break;
[02a09ed]94 default:
95 log_msg(LOG_DEFAULT, LVL_DEBUG, "invalid address family");
96 return EINVAL;
97 }
98
[fe4310f]99 if (rc != EOK) {
[a1a101d]100 log_msg(LOG_DEFAULT, LVL_DEBUG, "failed decoding PDU");
[e767dbf]101 return rc;
[fe4310f]102 }
[257feec]103
[a1a101d]104 log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet()");
[fe4310f]105 rc = inet_recv_packet(&packet);
[a1a101d]106 log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet -> %d", rc);
[7f95c904]107 free(packet.data);
[257feec]108
[fe4310f]109 return rc;
[e2e56e67]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) {
[a1a101d]124 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
[e2e56e67]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) {
[a1a101d]131 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting list of IP links.");
[e2e56e67]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) {
[a1a101d]149 log_msg(LOG_DEFAULT, LVL_DEBUG, "Found IP link '%lu'",
[e2e56e67]150 (unsigned long) svcs[i]);
151 rc = inet_link_open(svcs[i]);
152 if (rc != EOK)
[a1a101d]153 log_msg(LOG_DEFAULT, LVL_ERROR, "Could not open IP link.");
[e2e56e67]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) {
[a1a101d]166 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating link structure. "
[e2e56e67]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);
[3aece36]180
[e2e56e67]181 free(ilink);
182}
183
184static int inet_link_open(service_id_t sid)
185{
186 inet_link_t *ilink;
[a2e3ee6]187 inet_addr_t iaddr;
[e2e56e67]188 int rc;
189
[a1a101d]190 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_link_open()");
[e2e56e67]191 ilink = inet_link_new();
192 if (ilink == NULL)
193 return ENOMEM;
194
[45aa22c]195 ilink->svc_id = sid;
[347768d]196 ilink->iplink = NULL;
[45aa22c]197
[e2e56e67]198 rc = loc_service_get_name(sid, &ilink->svc_name);
199 if (rc != EOK) {
[a1a101d]200 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
[e2e56e67]201 goto error;
202 }
203
204 ilink->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
205 if (ilink->sess == NULL) {
[a1a101d]206 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting '%s'", ilink->svc_name);
[e2e56e67]207 goto error;
208 }
209
210 rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, &ilink->iplink);
211 if (rc != EOK) {
[a1a101d]212 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed opening IP link '%s'",
[e2e56e67]213 ilink->svc_name);
214 goto error;
215 }
216
[347768d]217 rc = iplink_get_mtu(ilink->iplink, &ilink->def_mtu);
218 if (rc != EOK) {
[a1a101d]219 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed determinning MTU of link '%s'",
[347768d]220 ilink->svc_name);
221 goto error;
222 }
[a17356fd]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);
[347768d]230
[a1a101d]231 log_msg(LOG_DEFAULT, LVL_DEBUG, "Opened IP link '%s'", ilink->svc_name);
[e2e56e67]232 list_append(&ilink->link_list, &inet_link_list);
233
[3aece36]234 inet_addrobj_t *addr = NULL;
[a2e3ee6]235
[3aece36]236 if (first_link) {
237 addr = inet_addrobj_new();
238
[a2e3ee6]239 inet_naddr(&addr->naddr, 127, 0, 0, 1, 24);
[3aece36]240 first_link = false;
[081971b]241 } else {
[1d24ad3]242 /*
243 * FIXME
[3aece36]244 * Setting static IPv4 address for testing purposes:
[1d24ad3]245 * 10.0.2.15/24
246 */
[3aece36]247 addr = inet_addrobj_new();
248
[a2e3ee6]249 inet_naddr(&addr->naddr, 10, 0, 2, 15, 24);
[081971b]250 }
[a2e3ee6]251
[3aece36]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 }
[bf9e6fc]270 }
[1d24ad3]271
[3aece36]272 inet_addrobj_t *addr6 = NULL;
[1d24ad3]273
[3aece36]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);
[962f03b]286 }
[1d24ad3]287
[3aece36]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 }
[1d24ad3]306 }
307
[e2e56e67]308 return EOK;
[02a09ed]309
[e2e56e67]310error:
[347768d]311 if (ilink->iplink != NULL)
312 iplink_close(ilink->iplink);
[257feec]313
[e2e56e67]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) {
[a1a101d]329 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback for IP link "
[e2e56e67]330 "discovery (%d).", rc);
331 return rc;
332 }
333
334 return inet_link_check_new();
335}
336
[a17356fd]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)
[ceba4bed]340{
[a17356fd]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
[347768d]351 /*
352 * Fill packet structure. Fragmentation is performed by
353 * inet_pdu_encode().
354 */
[257feec]355
[a17356fd]356 iplink_sdu_t sdu;
357
358 sdu.src = lsrc;
359 sdu.dest = ldest;
360
[a2e3ee6]361 inet_packet_t packet;
362
[fe4310f]363 packet.src = dgram->src;
364 packet.dest = dgram->dest;
365 packet.tos = dgram->tos;
[2ff150e]366 packet.proto = proto;
[fe4310f]367 packet.ttl = ttl;
368 packet.df = df;
369 packet.data = dgram->data;
370 packet.size = dgram->size;
[a2e3ee6]371
[02a09ed]372 size_t offs = 0;
373 int rc;
[a2e3ee6]374
[347768d]375 do {
376 /* Encode one fragment */
[a17356fd]377
[02a09ed]378 size_t roffs;
[a17356fd]379 rc = inet_pdu_encode(&packet, src_v4, dest_v4, offs, ilink->def_mtu,
380 &sdu.data, &sdu.size, &roffs);
[347768d]381 if (rc != EOK)
382 return rc;
[a2e3ee6]383
[347768d]384 /* Send the PDU */
385 rc = iplink_send(ilink->iplink, &sdu);
[a17356fd]386
[347768d]387 free(sdu.data);
[a17356fd]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);
[a2e3ee6]441
[a17356fd]442 free(sdu6.data);
[347768d]443 offs = roffs;
444 } while (offs < packet.size);
[a2e3ee6]445
[ceba4bed]446 return rc;
447}
448
[45aa22c]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}
[ceba4bed]466
[e2e56e67]467/** @}
468 */
Note: See TracBrowser for help on using the repository browser.