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 <bool.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 |
|
---|
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 sid);
|
---|
52 | static int inet_iplink_recv(iplink_t *ilink, iplink_sdu_t *sdu);
|
---|
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_sdu_t *sdu)
|
---|
62 | {
|
---|
63 | inet_packet_t packet;
|
---|
64 | int rc;
|
---|
65 |
|
---|
66 | log_msg(LVL_DEBUG, "inet_iplink_recv()");
|
---|
67 | rc = inet_pdu_decode(sdu->data, sdu->size, &packet);
|
---|
68 | if (rc != EOK) {
|
---|
69 | log_msg(LVL_DEBUG, "failed decoding PDU");
|
---|
70 | return rc;
|
---|
71 | }
|
---|
72 |
|
---|
73 | log_msg(LVL_DEBUG, "call inet_recv_packet()");
|
---|
74 | rc = inet_recv_packet(&packet);
|
---|
75 | log_msg(LVL_DEBUG, "call inet_recv_packet -> %d", rc);
|
---|
76 | free(packet.data);
|
---|
77 |
|
---|
78 | return rc;
|
---|
79 | }
|
---|
80 |
|
---|
81 | static int inet_link_check_new(void)
|
---|
82 | {
|
---|
83 | bool already_known;
|
---|
84 | category_id_t iplink_cat;
|
---|
85 | service_id_t *svcs;
|
---|
86 | size_t count, i;
|
---|
87 | int rc;
|
---|
88 |
|
---|
89 | fibril_mutex_lock(&inet_discovery_lock);
|
---|
90 |
|
---|
91 | rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
|
---|
92 | if (rc != EOK) {
|
---|
93 | log_msg(LVL_ERROR, "Failed resolving category 'iplink'.");
|
---|
94 | fibril_mutex_unlock(&inet_discovery_lock);
|
---|
95 | return ENOENT;
|
---|
96 | }
|
---|
97 |
|
---|
98 | rc = loc_category_get_svcs(iplink_cat, &svcs, &count);
|
---|
99 | if (rc != EOK) {
|
---|
100 | log_msg(LVL_ERROR, "Failed getting list of IP links.");
|
---|
101 | fibril_mutex_unlock(&inet_discovery_lock);
|
---|
102 | return EIO;
|
---|
103 | }
|
---|
104 |
|
---|
105 | for (i = 0; i < count; i++) {
|
---|
106 | already_known = false;
|
---|
107 |
|
---|
108 | list_foreach(inet_link_list, ilink_link) {
|
---|
109 | inet_link_t *ilink = list_get_instance(ilink_link,
|
---|
110 | inet_link_t, link_list);
|
---|
111 | if (ilink->svc_id == svcs[i]) {
|
---|
112 | already_known = true;
|
---|
113 | break;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | if (!already_known) {
|
---|
118 | log_msg(LVL_DEBUG, "Found IP link '%lu'",
|
---|
119 | (unsigned long) svcs[i]);
|
---|
120 | rc = inet_link_open(svcs[i]);
|
---|
121 | if (rc != EOK)
|
---|
122 | log_msg(LVL_ERROR, "Could not open IP link.");
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | fibril_mutex_unlock(&inet_discovery_lock);
|
---|
127 | return EOK;
|
---|
128 | }
|
---|
129 |
|
---|
130 | static inet_link_t *inet_link_new(void)
|
---|
131 | {
|
---|
132 | inet_link_t *ilink = calloc(1, sizeof(inet_link_t));
|
---|
133 |
|
---|
134 | if (ilink == NULL) {
|
---|
135 | log_msg(LVL_ERROR, "Failed allocating link structure. "
|
---|
136 | "Out of memory.");
|
---|
137 | return NULL;
|
---|
138 | }
|
---|
139 |
|
---|
140 | link_initialize(&ilink->link_list);
|
---|
141 |
|
---|
142 | return ilink;
|
---|
143 | }
|
---|
144 |
|
---|
145 | static void inet_link_delete(inet_link_t *ilink)
|
---|
146 | {
|
---|
147 | if (ilink->svc_name != NULL)
|
---|
148 | free(ilink->svc_name);
|
---|
149 | free(ilink);
|
---|
150 | }
|
---|
151 |
|
---|
152 | static int inet_link_open(service_id_t sid)
|
---|
153 | {
|
---|
154 | inet_link_t *ilink;
|
---|
155 | iplink_addr_t iaddr;
|
---|
156 | int rc;
|
---|
157 |
|
---|
158 | log_msg(LVL_DEBUG, "inet_link_open()");
|
---|
159 | ilink = inet_link_new();
|
---|
160 | if (ilink == NULL)
|
---|
161 | return ENOMEM;
|
---|
162 |
|
---|
163 | ilink->svc_id = sid;
|
---|
164 | ilink->iplink = NULL;
|
---|
165 |
|
---|
166 | rc = loc_service_get_name(sid, &ilink->svc_name);
|
---|
167 | if (rc != EOK) {
|
---|
168 | log_msg(LVL_ERROR, "Failed getting service name.");
|
---|
169 | goto error;
|
---|
170 | }
|
---|
171 |
|
---|
172 | ilink->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
|
---|
173 | if (ilink->sess == NULL) {
|
---|
174 | log_msg(LVL_ERROR, "Failed connecting '%s'", ilink->svc_name);
|
---|
175 | goto error;
|
---|
176 | }
|
---|
177 |
|
---|
178 | rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, &ilink->iplink);
|
---|
179 | if (rc != EOK) {
|
---|
180 | log_msg(LVL_ERROR, "Failed opening IP link '%s'",
|
---|
181 | ilink->svc_name);
|
---|
182 | goto error;
|
---|
183 | }
|
---|
184 |
|
---|
185 | rc = iplink_get_mtu(ilink->iplink, &ilink->def_mtu);
|
---|
186 | if (rc != EOK) {
|
---|
187 | log_msg(LVL_ERROR, "Failed determinning MTU of link '%s'",
|
---|
188 | ilink->svc_name);
|
---|
189 | goto error;
|
---|
190 | }
|
---|
191 |
|
---|
192 | log_msg(LVL_DEBUG, "Opened IP link '%s'", ilink->svc_name);
|
---|
193 | list_append(&ilink->link_list, &inet_link_list);
|
---|
194 |
|
---|
195 | inet_addrobj_t *addr;
|
---|
196 |
|
---|
197 | static int first = 1;
|
---|
198 | /* XXX For testing: set static IP address 192.168.0.4/24 */
|
---|
199 | addr = inet_addrobj_new();
|
---|
200 | if (first) {
|
---|
201 | addr->naddr.ipv4 = (127 << 24) + (0 << 16) + (0 << 8) + 1;
|
---|
202 | first = 0;
|
---|
203 | } else {
|
---|
204 | addr->naddr.ipv4 = (192 << 24) + (168 << 16) + (0 << 8) + 4;
|
---|
205 | }
|
---|
206 | addr->naddr.bits = 24;
|
---|
207 | addr->ilink = ilink;
|
---|
208 | addr->name = str_dup("v4a");
|
---|
209 | inet_addrobj_add(addr);
|
---|
210 |
|
---|
211 | iaddr.ipv4 = addr->naddr.ipv4;
|
---|
212 | rc = iplink_addr_add(ilink->iplink, &iaddr);
|
---|
213 | if (rc != EOK) {
|
---|
214 | log_msg(LVL_ERROR, "Failed setting IP address on internet link.");
|
---|
215 | /* XXX Roll back */
|
---|
216 | return rc;
|
---|
217 | }
|
---|
218 |
|
---|
219 | return EOK;
|
---|
220 |
|
---|
221 | error:
|
---|
222 | if (ilink->iplink != NULL)
|
---|
223 | iplink_close(ilink->iplink);
|
---|
224 | inet_link_delete(ilink);
|
---|
225 | return rc;
|
---|
226 | }
|
---|
227 |
|
---|
228 | static void inet_link_cat_change_cb(void)
|
---|
229 | {
|
---|
230 | (void) inet_link_check_new();
|
---|
231 | }
|
---|
232 |
|
---|
233 | int inet_link_discovery_start(void)
|
---|
234 | {
|
---|
235 | int rc;
|
---|
236 |
|
---|
237 | rc = loc_register_cat_change_cb(inet_link_cat_change_cb);
|
---|
238 | if (rc != EOK) {
|
---|
239 | log_msg(LVL_ERROR, "Failed registering callback for IP link "
|
---|
240 | "discovery (%d).", rc);
|
---|
241 | return rc;
|
---|
242 | }
|
---|
243 |
|
---|
244 | return inet_link_check_new();
|
---|
245 | }
|
---|
246 |
|
---|
247 | /** Send datagram over Internet link */
|
---|
248 | int inet_link_send_dgram(inet_link_t *ilink, inet_addr_t *lsrc,
|
---|
249 | inet_addr_t *ldest, inet_dgram_t *dgram, uint8_t proto, uint8_t ttl, int df)
|
---|
250 | {
|
---|
251 | iplink_sdu_t sdu;
|
---|
252 | inet_packet_t packet;
|
---|
253 | int rc;
|
---|
254 | size_t offs, roffs;
|
---|
255 |
|
---|
256 | /*
|
---|
257 | * Fill packet structure. Fragmentation is performed by
|
---|
258 | * inet_pdu_encode().
|
---|
259 | */
|
---|
260 | packet.src = dgram->src;
|
---|
261 | packet.dest = dgram->dest;
|
---|
262 | packet.tos = dgram->tos;
|
---|
263 | packet.proto = proto;
|
---|
264 | packet.ttl = ttl;
|
---|
265 | packet.df = df;
|
---|
266 | packet.data = dgram->data;
|
---|
267 | packet.size = dgram->size;
|
---|
268 |
|
---|
269 | sdu.lsrc.ipv4 = lsrc->ipv4;
|
---|
270 | sdu.ldest.ipv4 = ldest->ipv4;
|
---|
271 |
|
---|
272 | offs = 0;
|
---|
273 | do {
|
---|
274 | /* Encode one fragment */
|
---|
275 | rc = inet_pdu_encode(&packet, offs, ilink->def_mtu, &sdu.data,
|
---|
276 | &sdu.size, &roffs);
|
---|
277 | if (rc != EOK)
|
---|
278 | return rc;
|
---|
279 |
|
---|
280 | /* Send the PDU */
|
---|
281 | rc = iplink_send(ilink->iplink, &sdu);
|
---|
282 | free(sdu.data);
|
---|
283 |
|
---|
284 | offs = roffs;
|
---|
285 | } while (offs < packet.size);
|
---|
286 |
|
---|
287 | return rc;
|
---|
288 | }
|
---|
289 |
|
---|
290 | inet_link_t *inet_link_get_by_id(sysarg_t link_id)
|
---|
291 | {
|
---|
292 | fibril_mutex_lock(&inet_discovery_lock);
|
---|
293 |
|
---|
294 | list_foreach(inet_link_list, elem) {
|
---|
295 | inet_link_t *ilink = list_get_instance(elem, inet_link_t,
|
---|
296 | link_list);
|
---|
297 |
|
---|
298 | if (ilink->svc_id == link_id) {
|
---|
299 | fibril_mutex_unlock(&inet_discovery_lock);
|
---|
300 | return ilink;
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | fibril_mutex_unlock(&inet_discovery_lock);
|
---|
305 | return NULL;
|
---|
306 | }
|
---|
307 |
|
---|
308 | /** @}
|
---|
309 | */
|
---|