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

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

Make sure we wait the specified time before giving up on ARP translation.

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