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

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

HelenOS internet address version should not be based on BSD sockets definition.

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