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

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

Retransmit DHCP discover and request messages on timeout. Introduce DHCP link states.

  • Property mode set to 100644
File size: 11.4 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
[313824a]54static FIBRIL_MUTEX_INITIALIZE(ip_ident_lock);
55static uint16_t ip_ident = 0;
56
[02a09ed]57static int inet_iplink_recv(iplink_t *, iplink_recv_sdu_t *, uint16_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 = {
61 .recv = inet_iplink_recv
62};
63
[7af0cc5]64static LIST_INITIALIZE(inet_links);
65static FIBRIL_MUTEX_INITIALIZE(inet_links_lock);
[e2e56e67]66
[3aece36]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
[02a09ed]83static int inet_iplink_recv(iplink_t *iplink, iplink_recv_sdu_t *sdu, uint16_t af)
[e2e56e67]84{
[a1a101d]85 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_iplink_recv()");
[02a09ed]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:
[1d24ad3]95 rc = inet_pdu_decode6(sdu->data, sdu->size, &packet);
96 break;
[02a09ed]97 default:
98 log_msg(LOG_DEFAULT, LVL_DEBUG, "invalid address family");
99 return EINVAL;
100 }
101
[fe4310f]102 if (rc != EOK) {
[a1a101d]103 log_msg(LOG_DEFAULT, LVL_DEBUG, "failed decoding PDU");
[e767dbf]104 return rc;
[fe4310f]105 }
[257feec]106
[a1a101d]107 log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet()");
[fe4310f]108 rc = inet_recv_packet(&packet);
[a1a101d]109 log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet -> %d", rc);
[7f95c904]110 free(packet.data);
[257feec]111
[fe4310f]112 return rc;
[e2e56e67]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) {
[a1a101d]120 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating link structure. "
[e2e56e67]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);
[3aece36]134
[e2e56e67]135 free(ilink);
136}
137
[7af0cc5]138int inet_link_open(service_id_t sid)
[e2e56e67]139{
140 inet_link_t *ilink;
[a2e3ee6]141 inet_addr_t iaddr;
[e2e56e67]142 int rc;
143
[a1a101d]144 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_link_open()");
[e2e56e67]145 ilink = inet_link_new();
146 if (ilink == NULL)
147 return ENOMEM;
148
[45aa22c]149 ilink->svc_id = sid;
[347768d]150 ilink->iplink = NULL;
[45aa22c]151
[e2e56e67]152 rc = loc_service_get_name(sid, &ilink->svc_name);
153 if (rc != EOK) {
[a1a101d]154 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
[e2e56e67]155 goto error;
156 }
157
158 ilink->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
159 if (ilink->sess == NULL) {
[a1a101d]160 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting '%s'", ilink->svc_name);
[e2e56e67]161 goto error;
162 }
163
164 rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, &ilink->iplink);
165 if (rc != EOK) {
[a1a101d]166 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed opening IP link '%s'",
[e2e56e67]167 ilink->svc_name);
168 goto error;
169 }
170
[347768d]171 rc = iplink_get_mtu(ilink->iplink, &ilink->def_mtu);
172 if (rc != EOK) {
[a1a101d]173 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed determinning MTU of link '%s'",
[347768d]174 ilink->svc_name);
175 goto error;
176 }
[a17356fd]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);
[347768d]184
[a1a101d]185 log_msg(LOG_DEFAULT, LVL_DEBUG, "Opened IP link '%s'", ilink->svc_name);
[b417559]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
[7af0cc5]197 list_append(&ilink->link_list, &inet_links);
[b417559]198 fibril_mutex_unlock(&inet_links_lock);
[e2e56e67]199
[3aece36]200 inet_addrobj_t *addr = NULL;
[a2e3ee6]201
[695b6ff]202 /* XXX FIXME Cannot rely on loopback being the first IP link service!! */
[3aece36]203 if (first_link) {
204 addr = inet_addrobj_new();
205
[a2e3ee6]206 inet_naddr(&addr->naddr, 127, 0, 0, 1, 24);
[3aece36]207 first_link = false;
[081971b]208 }
[a2e3ee6]209
[3aece36]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 }
[bf9e6fc]228 }
[1d24ad3]229
[3aece36]230 inet_addrobj_t *addr6 = NULL;
[1d24ad3]231
[3aece36]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);
[962f03b]244 }
[1d24ad3]245
[3aece36]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 }
[1d24ad3]264 }
265
[bd88bee]266 log_msg(LOG_DEFAULT, LVL_DEBUG, "Configured link '%s'.", ilink->svc_name);
[e2e56e67]267 return EOK;
[02a09ed]268
[e2e56e67]269error:
[347768d]270 if (ilink->iplink != NULL)
271 iplink_close(ilink->iplink);
[257feec]272
[e2e56e67]273 inet_link_delete(ilink);
274 return rc;
275}
276
[1f97352]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 */
[a17356fd]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)
[ceba4bed]294{
[a17356fd]295 addr32_t src_v4;
296 uint16_t src_af = inet_addr_get(&dgram->src, &src_v4, NULL);
297 if (src_af != AF_INET)
298 return EINVAL;
299
300 addr32_t dest_v4;
301 uint16_t dest_af = inet_addr_get(&dgram->dest, &dest_v4, NULL);
302 if (dest_af != AF_INET)
303 return EINVAL;
304
[347768d]305 /*
306 * Fill packet structure. Fragmentation is performed by
307 * inet_pdu_encode().
308 */
[257feec]309
[a17356fd]310 iplink_sdu_t sdu;
311
312 sdu.src = lsrc;
313 sdu.dest = ldest;
314
[a2e3ee6]315 inet_packet_t packet;
316
[fe4310f]317 packet.src = dgram->src;
318 packet.dest = dgram->dest;
319 packet.tos = dgram->tos;
[2ff150e]320 packet.proto = proto;
[fe4310f]321 packet.ttl = ttl;
[313824a]322
323 /* Allocate identifier */
324 fibril_mutex_lock(&ip_ident_lock);
325 packet.ident = ++ip_ident;
326 fibril_mutex_unlock(&ip_ident_lock);
327
[fe4310f]328 packet.df = df;
329 packet.data = dgram->data;
330 packet.size = dgram->size;
[a2e3ee6]331
[02a09ed]332 int rc;
[313824a]333 size_t offs = 0;
[a2e3ee6]334
[347768d]335 do {
336 /* Encode one fragment */
[a17356fd]337
[02a09ed]338 size_t roffs;
[a17356fd]339 rc = inet_pdu_encode(&packet, src_v4, dest_v4, offs, ilink->def_mtu,
340 &sdu.data, &sdu.size, &roffs);
[347768d]341 if (rc != EOK)
342 return rc;
[a2e3ee6]343
[347768d]344 /* Send the PDU */
345 rc = iplink_send(ilink->iplink, &sdu);
[a17356fd]346
[347768d]347 free(sdu.data);
[a17356fd]348 offs = roffs;
349 } while (offs < packet.size);
350
351 return rc;
352}
353
[1f97352]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 */
[a17356fd]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 uint16_t src_af = inet_addr_get(&dgram->src, NULL, &src_v6);
372 if (src_af != AF_INET6)
373 return EINVAL;
374
375 addr128_t dest_v6;
376 uint16_t dest_af = inet_addr_get(&dgram->dest, NULL, &dest_v6);
377 if (dest_af != AF_INET6)
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;
[313824a]395
396 /* Allocate identifier */
397 fibril_mutex_lock(&ip_ident_lock);
398 packet.ident = ++ip_ident;
399 fibril_mutex_unlock(&ip_ident_lock);
400
[a17356fd]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);
[a2e3ee6]419
[a17356fd]420 free(sdu6.data);
[347768d]421 offs = roffs;
422 } while (offs < packet.size);
[a2e3ee6]423
[ceba4bed]424 return rc;
425}
426
[b417559]427static inet_link_t *inet_link_get_by_id_locked(sysarg_t link_id)
[45aa22c]428{
[b417559]429 assert(fibril_mutex_is_locked(&inet_links_lock));
[45aa22c]430
[7af0cc5]431 list_foreach(inet_links, link_list, inet_link_t, ilink) {
[b417559]432 if (ilink->svc_id == link_id)
[45aa22c]433 return ilink;
434 }
435
436 return NULL;
437}
[ceba4bed]438
[b417559]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
[b8b1adb1]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
[7af0cc5]456 fibril_mutex_lock(&inet_links_lock);
457 count = list_count(&inet_links);
[b8b1adb1]458
459 id_list = calloc(count, sizeof(sysarg_t));
460 if (id_list == NULL) {
[7af0cc5]461 fibril_mutex_unlock(&inet_links_lock);
[b8b1adb1]462 return ENOMEM;
463 }
464
465 i = 0;
[7af0cc5]466 list_foreach(inet_links, link_list, inet_link_t, ilink) {
[b8b1adb1]467 id_list[i++] = ilink->svc_id;
468 log_msg(LOG_DEFAULT, LVL_NOTE, "add link to list");
469 }
470
[7af0cc5]471 fibril_mutex_unlock(&inet_links_lock);
[b8b1adb1]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
[e2e56e67]480/** @}
481 */
Note: See TracBrowser for help on using the repository browser.