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 |
|
---|
51 | static int inet_link_open(service_id_t);
|
---|
52 | static int inet_iplink_recv(iplink_t *, iplink_recv_sdu_t *, uint16_t);
|
---|
53 |
|
---|
54 | static iplink_ev_ops_t inet_iplink_ev_ops = {
|
---|
55 | .recv = inet_iplink_recv
|
---|
56 | };
|
---|
57 |
|
---|
58 | static LIST_INITIALIZE(inet_link_list);
|
---|
59 | static FIBRIL_MUTEX_INITIALIZE(inet_discovery_lock);
|
---|
60 |
|
---|
61 | static int inet_iplink_recv(iplink_t *iplink, iplink_recv_sdu_t *sdu, uint16_t af)
|
---|
62 | {
|
---|
63 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_iplink_recv()");
|
---|
64 |
|
---|
65 | int rc;
|
---|
66 | inet_packet_t packet;
|
---|
67 |
|
---|
68 | switch (af) {
|
---|
69 | case AF_INET:
|
---|
70 | rc = inet_pdu_decode(sdu->data, sdu->size, &packet);
|
---|
71 | break;
|
---|
72 | case AF_INET6:
|
---|
73 | rc = inet_pdu_decode6(sdu->data, sdu->size, &packet);
|
---|
74 | break;
|
---|
75 | default:
|
---|
76 | log_msg(LOG_DEFAULT, LVL_DEBUG, "invalid address family");
|
---|
77 | return EINVAL;
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (rc != EOK) {
|
---|
81 | log_msg(LOG_DEFAULT, LVL_DEBUG, "failed decoding PDU");
|
---|
82 | return rc;
|
---|
83 | }
|
---|
84 |
|
---|
85 | log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet()");
|
---|
86 | rc = inet_recv_packet(&packet);
|
---|
87 | log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet -> %d", rc);
|
---|
88 | free(packet.data);
|
---|
89 |
|
---|
90 | return rc;
|
---|
91 | }
|
---|
92 |
|
---|
93 | static int inet_link_check_new(void)
|
---|
94 | {
|
---|
95 | bool already_known;
|
---|
96 | category_id_t iplink_cat;
|
---|
97 | service_id_t *svcs;
|
---|
98 | size_t count, i;
|
---|
99 | int rc;
|
---|
100 |
|
---|
101 | fibril_mutex_lock(&inet_discovery_lock);
|
---|
102 |
|
---|
103 | rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
|
---|
104 | if (rc != EOK) {
|
---|
105 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
|
---|
106 | fibril_mutex_unlock(&inet_discovery_lock);
|
---|
107 | return ENOENT;
|
---|
108 | }
|
---|
109 |
|
---|
110 | rc = loc_category_get_svcs(iplink_cat, &svcs, &count);
|
---|
111 | if (rc != EOK) {
|
---|
112 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting list of IP links.");
|
---|
113 | fibril_mutex_unlock(&inet_discovery_lock);
|
---|
114 | return EIO;
|
---|
115 | }
|
---|
116 |
|
---|
117 | for (i = 0; i < count; i++) {
|
---|
118 | already_known = false;
|
---|
119 |
|
---|
120 | list_foreach(inet_link_list, ilink_link) {
|
---|
121 | inet_link_t *ilink = list_get_instance(ilink_link,
|
---|
122 | inet_link_t, link_list);
|
---|
123 | if (ilink->svc_id == svcs[i]) {
|
---|
124 | already_known = true;
|
---|
125 | break;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (!already_known) {
|
---|
130 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Found IP link '%lu'",
|
---|
131 | (unsigned long) svcs[i]);
|
---|
132 | rc = inet_link_open(svcs[i]);
|
---|
133 | if (rc != EOK)
|
---|
134 | log_msg(LOG_DEFAULT, LVL_ERROR, "Could not open IP link.");
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | fibril_mutex_unlock(&inet_discovery_lock);
|
---|
139 | return EOK;
|
---|
140 | }
|
---|
141 |
|
---|
142 | static inet_link_t *inet_link_new(void)
|
---|
143 | {
|
---|
144 | inet_link_t *ilink = calloc(1, sizeof(inet_link_t));
|
---|
145 |
|
---|
146 | if (ilink == NULL) {
|
---|
147 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating link structure. "
|
---|
148 | "Out of memory.");
|
---|
149 | return NULL;
|
---|
150 | }
|
---|
151 |
|
---|
152 | link_initialize(&ilink->link_list);
|
---|
153 |
|
---|
154 | return ilink;
|
---|
155 | }
|
---|
156 |
|
---|
157 | static void inet_link_delete(inet_link_t *ilink)
|
---|
158 | {
|
---|
159 | if (ilink->svc_name != NULL)
|
---|
160 | free(ilink->svc_name);
|
---|
161 | free(ilink);
|
---|
162 | }
|
---|
163 |
|
---|
164 | static int inet_link_open(service_id_t sid)
|
---|
165 | {
|
---|
166 | inet_link_t *ilink;
|
---|
167 | inet_addr_t iaddr;
|
---|
168 | int rc;
|
---|
169 |
|
---|
170 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_link_open()");
|
---|
171 | ilink = inet_link_new();
|
---|
172 | if (ilink == NULL)
|
---|
173 | return ENOMEM;
|
---|
174 |
|
---|
175 | ilink->svc_id = sid;
|
---|
176 | ilink->iplink = NULL;
|
---|
177 |
|
---|
178 | rc = loc_service_get_name(sid, &ilink->svc_name);
|
---|
179 | if (rc != EOK) {
|
---|
180 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
|
---|
181 | goto error;
|
---|
182 | }
|
---|
183 |
|
---|
184 | ilink->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
|
---|
185 | if (ilink->sess == NULL) {
|
---|
186 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting '%s'", ilink->svc_name);
|
---|
187 | goto error;
|
---|
188 | }
|
---|
189 |
|
---|
190 | rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, &ilink->iplink);
|
---|
191 | if (rc != EOK) {
|
---|
192 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed opening IP link '%s'",
|
---|
193 | ilink->svc_name);
|
---|
194 | goto error;
|
---|
195 | }
|
---|
196 |
|
---|
197 | rc = iplink_get_mtu(ilink->iplink, &ilink->def_mtu);
|
---|
198 | if (rc != EOK) {
|
---|
199 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed determinning MTU of link '%s'",
|
---|
200 | ilink->svc_name);
|
---|
201 | goto error;
|
---|
202 | }
|
---|
203 |
|
---|
204 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Opened IP link '%s'", ilink->svc_name);
|
---|
205 | list_append(&ilink->link_list, &inet_link_list);
|
---|
206 |
|
---|
207 | inet_addrobj_t *addr;
|
---|
208 | inet_addrobj_t *addr6;
|
---|
209 |
|
---|
210 | static int first = 1;
|
---|
211 |
|
---|
212 | addr = inet_addrobj_new();
|
---|
213 | addr6 = inet_addrobj_new();
|
---|
214 |
|
---|
215 | if (first) {
|
---|
216 | inet_naddr(&addr->naddr, 127, 0, 0, 1, 24);
|
---|
217 | inet_naddr6(&addr6->naddr, 0, 0, 0, 0, 0, 0, 0, 1, 128);
|
---|
218 | first = 0;
|
---|
219 | } else {
|
---|
220 | /*
|
---|
221 | * FIXME
|
---|
222 | * Setting static IP addresses for testing purposes
|
---|
223 | * 10.0.2.15/24
|
---|
224 | * fd19:1680::4/120
|
---|
225 | */
|
---|
226 | inet_naddr(&addr->naddr, 10, 0, 2, 15, 24);
|
---|
227 | inet_naddr6(&addr6->naddr, 0xfd19, 0x1680, 0, 0, 0, 0, 0, 4, 120);
|
---|
228 | }
|
---|
229 |
|
---|
230 | addr->ilink = ilink;
|
---|
231 | addr6->ilink = ilink;
|
---|
232 | addr->name = str_dup("v4a");
|
---|
233 | addr6->name = str_dup("v6a");
|
---|
234 |
|
---|
235 | rc = inet_addrobj_add(addr);
|
---|
236 | if (rc != EOK) {
|
---|
237 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding IPv4 address.");
|
---|
238 | inet_addrobj_delete(addr);
|
---|
239 | /* XXX Roll back */
|
---|
240 | return rc;
|
---|
241 | }
|
---|
242 |
|
---|
243 | rc = inet_addrobj_add(addr6);
|
---|
244 | if (rc != EOK) {
|
---|
245 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding IPv6 address.");
|
---|
246 | inet_addrobj_delete(addr6);
|
---|
247 | /* XXX Roll back */
|
---|
248 | return rc;
|
---|
249 | }
|
---|
250 |
|
---|
251 | inet_naddr_addr(&addr->naddr, &iaddr);
|
---|
252 | rc = iplink_addr_add(ilink->iplink, &iaddr);
|
---|
253 | if (rc != EOK) {
|
---|
254 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed setting IPv4 address on internet link.");
|
---|
255 | inet_addrobj_remove(addr);
|
---|
256 | inet_addrobj_delete(addr);
|
---|
257 | /* XXX Roll back */
|
---|
258 | return rc;
|
---|
259 | }
|
---|
260 |
|
---|
261 | inet_naddr_addr(&addr6->naddr, &iaddr);
|
---|
262 | rc = iplink_addr_add(ilink->iplink, &iaddr);
|
---|
263 | if (rc != EOK) {
|
---|
264 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed setting IPv6 address on internet link.");
|
---|
265 | inet_addrobj_remove(addr6);
|
---|
266 | inet_addrobj_delete(addr6);
|
---|
267 | /* XXX Roll back */
|
---|
268 | return rc;
|
---|
269 | }
|
---|
270 |
|
---|
271 | return EOK;
|
---|
272 |
|
---|
273 | error:
|
---|
274 | if (ilink->iplink != NULL)
|
---|
275 | iplink_close(ilink->iplink);
|
---|
276 |
|
---|
277 | inet_link_delete(ilink);
|
---|
278 | return rc;
|
---|
279 | }
|
---|
280 |
|
---|
281 | static void inet_link_cat_change_cb(void)
|
---|
282 | {
|
---|
283 | (void) inet_link_check_new();
|
---|
284 | }
|
---|
285 |
|
---|
286 | int inet_link_discovery_start(void)
|
---|
287 | {
|
---|
288 | int rc;
|
---|
289 |
|
---|
290 | rc = loc_register_cat_change_cb(inet_link_cat_change_cb);
|
---|
291 | if (rc != EOK) {
|
---|
292 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback for IP link "
|
---|
293 | "discovery (%d).", rc);
|
---|
294 | return rc;
|
---|
295 | }
|
---|
296 |
|
---|
297 | return inet_link_check_new();
|
---|
298 | }
|
---|
299 |
|
---|
300 | /** Send datagram over Internet link */
|
---|
301 | int inet_link_send_dgram(inet_link_t *ilink, inet_addr_t *lsrc,
|
---|
302 | inet_addr_t *ldest, inet_dgram_t *dgram, uint8_t proto, uint8_t ttl, int df)
|
---|
303 | {
|
---|
304 | /*
|
---|
305 | * Fill packet structure. Fragmentation is performed by
|
---|
306 | * inet_pdu_encode().
|
---|
307 | */
|
---|
308 |
|
---|
309 | inet_packet_t packet;
|
---|
310 |
|
---|
311 | packet.src = dgram->src;
|
---|
312 | packet.dest = dgram->dest;
|
---|
313 | packet.tos = dgram->tos;
|
---|
314 | packet.proto = proto;
|
---|
315 | packet.ttl = ttl;
|
---|
316 | packet.df = df;
|
---|
317 | packet.data = dgram->data;
|
---|
318 | packet.size = dgram->size;
|
---|
319 |
|
---|
320 | iplink_sdu_t sdu;
|
---|
321 | size_t offs = 0;
|
---|
322 | int rc;
|
---|
323 |
|
---|
324 | sdu.src = *lsrc;
|
---|
325 | sdu.dest = *ldest;
|
---|
326 |
|
---|
327 | do {
|
---|
328 | /* Encode one fragment */
|
---|
329 | size_t roffs;
|
---|
330 | rc = inet_pdu_encode(&packet, offs, ilink->def_mtu, &sdu.data,
|
---|
331 | &sdu.size, &roffs);
|
---|
332 | if (rc != EOK)
|
---|
333 | return rc;
|
---|
334 |
|
---|
335 | /* Send the PDU */
|
---|
336 | rc = iplink_send(ilink->iplink, &sdu);
|
---|
337 | free(sdu.data);
|
---|
338 |
|
---|
339 | offs = roffs;
|
---|
340 | } while (offs < packet.size);
|
---|
341 |
|
---|
342 | return rc;
|
---|
343 | }
|
---|
344 |
|
---|
345 | inet_link_t *inet_link_get_by_id(sysarg_t link_id)
|
---|
346 | {
|
---|
347 | fibril_mutex_lock(&inet_discovery_lock);
|
---|
348 |
|
---|
349 | list_foreach(inet_link_list, elem) {
|
---|
350 | inet_link_t *ilink = list_get_instance(elem, inet_link_t,
|
---|
351 | link_list);
|
---|
352 |
|
---|
353 | if (ilink->svc_id == link_id) {
|
---|
354 | fibril_mutex_unlock(&inet_discovery_lock);
|
---|
355 | return ilink;
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | fibril_mutex_unlock(&inet_discovery_lock);
|
---|
360 | return NULL;
|
---|
361 | }
|
---|
362 |
|
---|
363 | /** @}
|
---|
364 | */
|
---|