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 Internet Protocol service
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <adt/list.h>
|
---|
38 | #include <async.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <fibril_synch.h>
|
---|
41 | #include <io/log.h>
|
---|
42 | #include <ipc/inet.h>
|
---|
43 | #include <ipc/services.h>
|
---|
44 | #include <loc.h>
|
---|
45 | #include <stdio.h>
|
---|
46 | #include <stdlib.h>
|
---|
47 | #include <sys/types.h>
|
---|
48 |
|
---|
49 | #include "addrobj.h"
|
---|
50 | #include "icmp.h"
|
---|
51 | #include "icmp_std.h"
|
---|
52 | #include "inetsrv.h"
|
---|
53 | #include "inetcfg.h"
|
---|
54 | #include "inetping.h"
|
---|
55 | #include "inet_link.h"
|
---|
56 | #include "reass.h"
|
---|
57 | #include "sroute.h"
|
---|
58 |
|
---|
59 | #define NAME "inetsrv"
|
---|
60 |
|
---|
61 | static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
---|
62 |
|
---|
63 | static FIBRIL_MUTEX_INITIALIZE(client_list_lock);
|
---|
64 | static LIST_INITIALIZE(client_list);
|
---|
65 |
|
---|
66 | static int inet_init(void)
|
---|
67 | {
|
---|
68 | service_id_t sid;
|
---|
69 | int rc;
|
---|
70 |
|
---|
71 | log_msg(LVL_DEBUG, "inet_init()");
|
---|
72 |
|
---|
73 | async_set_client_connection(inet_client_conn);
|
---|
74 |
|
---|
75 | rc = loc_server_register(NAME);
|
---|
76 | if (rc != EOK) {
|
---|
77 | log_msg(LVL_ERROR, "Failed registering server (%d).", rc);
|
---|
78 | return EEXIST;
|
---|
79 | }
|
---|
80 |
|
---|
81 | rc = loc_service_register_with_iface(SERVICE_NAME_INET, &sid,
|
---|
82 | INET_PORT_DEFAULT);
|
---|
83 | if (rc != EOK) {
|
---|
84 | log_msg(LVL_ERROR, "Failed registering service (%d).", rc);
|
---|
85 | return EEXIST;
|
---|
86 | }
|
---|
87 |
|
---|
88 | rc = loc_service_register_with_iface(SERVICE_NAME_INETCFG, &sid,
|
---|
89 | INET_PORT_CFG);
|
---|
90 | if (rc != EOK) {
|
---|
91 | log_msg(LVL_ERROR, "Failed registering service (%d).", rc);
|
---|
92 | return EEXIST;
|
---|
93 | }
|
---|
94 |
|
---|
95 | rc = loc_service_register_with_iface(SERVICE_NAME_INETPING, &sid,
|
---|
96 | INET_PORT_PING);
|
---|
97 | if (rc != EOK) {
|
---|
98 | log_msg(LVL_ERROR, "Failed registering service (%d).", rc);
|
---|
99 | return EEXIST;
|
---|
100 | }
|
---|
101 |
|
---|
102 | rc = inet_link_discovery_start();
|
---|
103 | if (rc != EOK)
|
---|
104 | return EEXIST;
|
---|
105 |
|
---|
106 | return EOK;
|
---|
107 | }
|
---|
108 |
|
---|
109 | static void inet_callback_create_srv(inet_client_t *client, ipc_callid_t callid,
|
---|
110 | ipc_call_t *call)
|
---|
111 | {
|
---|
112 | log_msg(LVL_DEBUG, "inet_callback_create_srv()");
|
---|
113 |
|
---|
114 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
115 | if (sess == NULL) {
|
---|
116 | async_answer_0(callid, ENOMEM);
|
---|
117 | return;
|
---|
118 | }
|
---|
119 |
|
---|
120 | client->sess = sess;
|
---|
121 | async_answer_0(callid, EOK);
|
---|
122 | }
|
---|
123 |
|
---|
124 | static int inet_find_dir(inet_addr_t *src, inet_addr_t *dest, uint8_t tos,
|
---|
125 | inet_dir_t *dir)
|
---|
126 | {
|
---|
127 | inet_sroute_t *sr;
|
---|
128 |
|
---|
129 | /* XXX Handle case where source address is specified */
|
---|
130 | (void) src;
|
---|
131 |
|
---|
132 | dir->aobj = inet_addrobj_find(dest, iaf_net);
|
---|
133 | if (dir->aobj != NULL) {
|
---|
134 | dir->ldest = *dest;
|
---|
135 | dir->dtype = dt_direct;
|
---|
136 | } else {
|
---|
137 | /* No direct path, try using a static route */
|
---|
138 | sr = inet_sroute_find(dest);
|
---|
139 | if (sr != NULL) {
|
---|
140 | dir->aobj = inet_addrobj_find(&sr->router, iaf_net);
|
---|
141 | dir->ldest = sr->router;
|
---|
142 | dir->dtype = dt_router;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | if (dir->aobj == NULL) {
|
---|
147 | log_msg(LVL_DEBUG, "inet_send: No route to destination.");
|
---|
148 | return ENOENT;
|
---|
149 | }
|
---|
150 |
|
---|
151 | return EOK;
|
---|
152 | }
|
---|
153 |
|
---|
154 | int inet_route_packet(inet_dgram_t *dgram, uint8_t proto, uint8_t ttl,
|
---|
155 | int df)
|
---|
156 | {
|
---|
157 | inet_dir_t dir;
|
---|
158 | int rc;
|
---|
159 |
|
---|
160 | rc = inet_find_dir(&dgram->src, &dgram->dest, dgram->tos, &dir);
|
---|
161 | if (rc != EOK)
|
---|
162 | return rc;
|
---|
163 |
|
---|
164 | return inet_addrobj_send_dgram(dir.aobj, &dir.ldest, dgram,
|
---|
165 | proto, ttl, df);
|
---|
166 | }
|
---|
167 |
|
---|
168 | static int inet_send(inet_client_t *client, inet_dgram_t *dgram,
|
---|
169 | uint8_t proto, uint8_t ttl, int df)
|
---|
170 | {
|
---|
171 | return inet_route_packet(dgram, proto, ttl, df);
|
---|
172 | }
|
---|
173 |
|
---|
174 | int inet_get_srcaddr(inet_addr_t *remote, uint8_t tos, inet_addr_t *local)
|
---|
175 | {
|
---|
176 | inet_dir_t dir;
|
---|
177 | int rc;
|
---|
178 |
|
---|
179 | rc = inet_find_dir(NULL, remote, tos, &dir);
|
---|
180 | if (rc != EOK)
|
---|
181 | return rc;
|
---|
182 |
|
---|
183 | /* XXX dt_local? */
|
---|
184 |
|
---|
185 | /* Take source address from the address object */
|
---|
186 | local->ipv4 = dir.aobj->naddr.ipv4;
|
---|
187 | return EOK;
|
---|
188 | }
|
---|
189 |
|
---|
190 | static void inet_get_srcaddr_srv(inet_client_t *client, ipc_callid_t callid,
|
---|
191 | ipc_call_t *call)
|
---|
192 | {
|
---|
193 | inet_addr_t remote;
|
---|
194 | uint8_t tos;
|
---|
195 | inet_addr_t local;
|
---|
196 | int rc;
|
---|
197 |
|
---|
198 | log_msg(LVL_DEBUG, "inet_get_srcaddr_srv()");
|
---|
199 |
|
---|
200 | remote.ipv4 = IPC_GET_ARG1(*call);
|
---|
201 | tos = IPC_GET_ARG2(*call);
|
---|
202 | local.ipv4 = 0;
|
---|
203 |
|
---|
204 | rc = inet_get_srcaddr(&remote, tos, &local);
|
---|
205 | async_answer_1(callid, rc, local.ipv4);
|
---|
206 | }
|
---|
207 |
|
---|
208 | static void inet_send_srv(inet_client_t *client, ipc_callid_t callid,
|
---|
209 | ipc_call_t *call)
|
---|
210 | {
|
---|
211 | inet_dgram_t dgram;
|
---|
212 | uint8_t ttl;
|
---|
213 | int df;
|
---|
214 | int rc;
|
---|
215 |
|
---|
216 | log_msg(LVL_DEBUG, "inet_send_srv()");
|
---|
217 |
|
---|
218 | dgram.src.ipv4 = IPC_GET_ARG1(*call);
|
---|
219 | dgram.dest.ipv4 = IPC_GET_ARG2(*call);
|
---|
220 | dgram.tos = IPC_GET_ARG3(*call);
|
---|
221 | ttl = IPC_GET_ARG4(*call);
|
---|
222 | df = IPC_GET_ARG5(*call);
|
---|
223 |
|
---|
224 | rc = async_data_write_accept(&dgram.data, false, 0, 0, 0, &dgram.size);
|
---|
225 | if (rc != EOK) {
|
---|
226 | async_answer_0(callid, rc);
|
---|
227 | return;
|
---|
228 | }
|
---|
229 |
|
---|
230 | rc = inet_send(client, &dgram, client->protocol, ttl, df);
|
---|
231 |
|
---|
232 | free(dgram.data);
|
---|
233 | async_answer_0(callid, rc);
|
---|
234 | }
|
---|
235 |
|
---|
236 | static void inet_set_proto_srv(inet_client_t *client, ipc_callid_t callid,
|
---|
237 | ipc_call_t *call)
|
---|
238 | {
|
---|
239 | sysarg_t proto;
|
---|
240 |
|
---|
241 | proto = IPC_GET_ARG1(*call);
|
---|
242 | log_msg(LVL_DEBUG, "inet_set_proto_srv(%lu)", (unsigned long) proto);
|
---|
243 |
|
---|
244 | if (proto > UINT8_MAX) {
|
---|
245 | async_answer_0(callid, EINVAL);
|
---|
246 | return;
|
---|
247 | }
|
---|
248 |
|
---|
249 | client->protocol = proto;
|
---|
250 | async_answer_0(callid, EOK);
|
---|
251 | }
|
---|
252 |
|
---|
253 | static void inet_client_init(inet_client_t *client)
|
---|
254 | {
|
---|
255 | client->sess = NULL;
|
---|
256 |
|
---|
257 | fibril_mutex_lock(&client_list_lock);
|
---|
258 | list_append(&client->client_list, &client_list);
|
---|
259 | fibril_mutex_unlock(&client_list_lock);
|
---|
260 | }
|
---|
261 |
|
---|
262 | static void inet_client_fini(inet_client_t *client)
|
---|
263 | {
|
---|
264 | async_hangup(client->sess);
|
---|
265 | client->sess = NULL;
|
---|
266 |
|
---|
267 | fibril_mutex_lock(&client_list_lock);
|
---|
268 | list_remove(&client->client_list);
|
---|
269 | fibril_mutex_unlock(&client_list_lock);
|
---|
270 | }
|
---|
271 |
|
---|
272 | static void inet_default_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
273 | {
|
---|
274 | inet_client_t client;
|
---|
275 |
|
---|
276 | log_msg(LVL_DEBUG, "inet_default_conn()");
|
---|
277 |
|
---|
278 | /* Accept the connection */
|
---|
279 | async_answer_0(iid, EOK);
|
---|
280 |
|
---|
281 | inet_client_init(&client);
|
---|
282 |
|
---|
283 | while (true) {
|
---|
284 | ipc_call_t call;
|
---|
285 | ipc_callid_t callid = async_get_call(&call);
|
---|
286 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
287 |
|
---|
288 | if (!method) {
|
---|
289 | /* The other side has hung up */
|
---|
290 | async_answer_0(callid, EOK);
|
---|
291 | return;
|
---|
292 | }
|
---|
293 |
|
---|
294 | switch (method) {
|
---|
295 | case INET_CALLBACK_CREATE:
|
---|
296 | inet_callback_create_srv(&client, callid, &call);
|
---|
297 | break;
|
---|
298 | case INET_GET_SRCADDR:
|
---|
299 | inet_get_srcaddr_srv(&client, callid, &call);
|
---|
300 | break;
|
---|
301 | case INET_SEND:
|
---|
302 | inet_send_srv(&client, callid, &call);
|
---|
303 | break;
|
---|
304 | case INET_SET_PROTO:
|
---|
305 | inet_set_proto_srv(&client, callid, &call);
|
---|
306 | break;
|
---|
307 | default:
|
---|
308 | async_answer_0(callid, EINVAL);
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | inet_client_fini(&client);
|
---|
313 | }
|
---|
314 |
|
---|
315 | static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
316 | {
|
---|
317 | sysarg_t port;
|
---|
318 |
|
---|
319 | port = IPC_GET_ARG1(*icall);
|
---|
320 |
|
---|
321 | switch (port) {
|
---|
322 | case INET_PORT_DEFAULT:
|
---|
323 | inet_default_conn(iid, icall, arg);
|
---|
324 | break;
|
---|
325 | case INET_PORT_CFG:
|
---|
326 | inet_cfg_conn(iid, icall, arg);
|
---|
327 | break;
|
---|
328 | case INET_PORT_PING:
|
---|
329 | inetping_conn(iid, icall, arg);
|
---|
330 | break;
|
---|
331 | default:
|
---|
332 | async_answer_0(iid, ENOTSUP);
|
---|
333 | break;
|
---|
334 | }
|
---|
335 | }
|
---|
336 |
|
---|
337 | static inet_client_t *inet_client_find(uint8_t proto)
|
---|
338 | {
|
---|
339 | fibril_mutex_lock(&client_list_lock);
|
---|
340 |
|
---|
341 | list_foreach(client_list, link) {
|
---|
342 | inet_client_t *client = list_get_instance(link, inet_client_t,
|
---|
343 | client_list);
|
---|
344 |
|
---|
345 | if (client->protocol == proto) {
|
---|
346 | fibril_mutex_unlock(&client_list_lock);
|
---|
347 | return client;
|
---|
348 | }
|
---|
349 | }
|
---|
350 |
|
---|
351 | fibril_mutex_unlock(&client_list_lock);
|
---|
352 | return NULL;
|
---|
353 | }
|
---|
354 |
|
---|
355 | int inet_ev_recv(inet_client_t *client, inet_dgram_t *dgram)
|
---|
356 | {
|
---|
357 | async_exch_t *exch = async_exchange_begin(client->sess);
|
---|
358 |
|
---|
359 | ipc_call_t answer;
|
---|
360 | aid_t req = async_send_3(exch, INET_EV_RECV, dgram->src.ipv4,
|
---|
361 | dgram->dest.ipv4, dgram->tos, &answer);
|
---|
362 | int rc = async_data_write_start(exch, dgram->data, dgram->size);
|
---|
363 | async_exchange_end(exch);
|
---|
364 |
|
---|
365 | if (rc != EOK) {
|
---|
366 | async_forget(req);
|
---|
367 | return rc;
|
---|
368 | }
|
---|
369 |
|
---|
370 | sysarg_t retval;
|
---|
371 | async_wait_for(req, &retval);
|
---|
372 | if (retval != EOK)
|
---|
373 | return retval;
|
---|
374 |
|
---|
375 | return EOK;
|
---|
376 | }
|
---|
377 |
|
---|
378 | int inet_recv_dgram_local(inet_dgram_t *dgram, uint8_t proto)
|
---|
379 | {
|
---|
380 | inet_client_t *client;
|
---|
381 |
|
---|
382 | log_msg(LVL_DEBUG, "inet_recv_dgram_local()");
|
---|
383 |
|
---|
384 | /* ICMP messages are handled internally */
|
---|
385 | if (proto == IP_PROTO_ICMP)
|
---|
386 | return icmp_recv(dgram);
|
---|
387 |
|
---|
388 | client = inet_client_find(proto);
|
---|
389 | if (client == NULL) {
|
---|
390 | log_msg(LVL_DEBUG, "No client found for protocol 0x%" PRIx8,
|
---|
391 | proto);
|
---|
392 | return ENOENT;
|
---|
393 | }
|
---|
394 |
|
---|
395 | return inet_ev_recv(client, dgram);
|
---|
396 | }
|
---|
397 |
|
---|
398 | int inet_recv_packet(inet_packet_t *packet)
|
---|
399 | {
|
---|
400 | inet_addrobj_t *addr;
|
---|
401 | inet_dgram_t dgram;
|
---|
402 |
|
---|
403 | addr = inet_addrobj_find(&packet->dest, iaf_addr);
|
---|
404 | if (addr != NULL) {
|
---|
405 | /* Destined for one of the local addresses */
|
---|
406 |
|
---|
407 | /* Check if packet is a complete datagram */
|
---|
408 | if (packet->offs == 0 && !packet->mf) {
|
---|
409 | /* It is complete deliver it immediately */
|
---|
410 | dgram.src = packet->src;
|
---|
411 | dgram.dest = packet->dest;
|
---|
412 | dgram.tos = packet->tos;
|
---|
413 | dgram.data = packet->data;
|
---|
414 | dgram.size = packet->size;
|
---|
415 |
|
---|
416 | return inet_recv_dgram_local(&dgram, packet->proto);
|
---|
417 | } else {
|
---|
418 | /* It is a fragment, queue it for reassembly */
|
---|
419 | inet_reass_queue_packet(packet);
|
---|
420 | }
|
---|
421 | }
|
---|
422 |
|
---|
423 | return ENOENT;
|
---|
424 | }
|
---|
425 |
|
---|
426 | int main(int argc, char *argv[])
|
---|
427 | {
|
---|
428 | int rc;
|
---|
429 |
|
---|
430 | printf(NAME ": HelenOS Internet Protocol service\n");
|
---|
431 |
|
---|
432 | if (log_init(NAME, LVL_WARN) != EOK) {
|
---|
433 | printf(NAME ": Failed to initialize logging.\n");
|
---|
434 | return 1;
|
---|
435 | }
|
---|
436 |
|
---|
437 | rc = inet_init();
|
---|
438 | if (rc != EOK)
|
---|
439 | return 1;
|
---|
440 |
|
---|
441 | printf(NAME ": Accepting connections.\n");
|
---|
442 | task_retval(0);
|
---|
443 | async_manager();
|
---|
444 |
|
---|
445 | /* Not reached */
|
---|
446 | return 0;
|
---|
447 | }
|
---|
448 |
|
---|
449 | /** @}
|
---|
450 | */
|
---|