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

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

Find the association to deliver the datagram using amap.

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