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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1d94e21 was 9ae6fc7, checked in by Martin Decky <martin@…>, 12 years ago

accept multicast datagrams (thx Antonin Steinhauser)

  • Property mode set to 100644
File size: 13.1 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 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#include <net/socket_codes.h>
49#include "addrobj.h"
50#include "icmp.h"
51#include "icmp_std.h"
52#include "icmpv6.h"
53#include "icmpv6_std.h"
54#include "inetsrv.h"
55#include "inetcfg.h"
56#include "inetping.h"
57#include "inetping6.h"
58#include "inet_link.h"
59#include "reass.h"
60#include "sroute.h"
61
62#define NAME "inetsrv"
63
64static inet_naddr_t solicited_node_mask = {
65 .family = AF_INET6,
66 .addr6 = {0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0xff, 0, 0, 0},
67 .prefix = 104
68};
69
70static inet_addr_t multicast_all_nodes = {
71 .family = AF_INET,
72 .addr6 = {0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
73};
74
75static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
76
77static FIBRIL_MUTEX_INITIALIZE(client_list_lock);
78static LIST_INITIALIZE(client_list);
79
80static int inet_init(void)
81{
82 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_init()");
83
84 async_set_client_connection(inet_client_conn);
85
86 int rc = loc_server_register(NAME);
87 if (rc != EOK) {
88 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server (%d).", rc);
89 return EEXIST;
90 }
91
92 service_id_t sid;
93 rc = loc_service_register_with_iface(SERVICE_NAME_INET, &sid,
94 INET_PORT_DEFAULT);
95 if (rc != EOK) {
96 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service (%d).", rc);
97 return EEXIST;
98 }
99
100 rc = loc_service_register_with_iface(SERVICE_NAME_INETCFG, &sid,
101 INET_PORT_CFG);
102 if (rc != EOK) {
103 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service (%d).", rc);
104 return EEXIST;
105 }
106
107 rc = loc_service_register_with_iface(SERVICE_NAME_INETPING, &sid,
108 INET_PORT_PING);
109 if (rc != EOK) {
110 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service (%d).", rc);
111 return EEXIST;
112 }
113
114 rc = loc_service_register_with_iface(SERVICE_NAME_INETPING6, &sid,
115 INET_PORT_PING6);
116 if (rc != EOK) {
117 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service (%d).", rc);
118 return EEXIST;
119 }
120
121 inet_sroute_t *sroute = inet_sroute_new();
122 if (sroute == NULL) {
123 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed creating default route (%d).", rc);
124 return ENOMEM;
125 }
126
127 inet_naddr(&sroute->dest, 0, 0, 0, 0, 0);
128 inet_addr(&sroute->router, 10, 0, 2, 2);
129 sroute->name = str_dup("default");
130 inet_sroute_add(sroute);
131
132 rc = inet_link_discovery_start();
133 if (rc != EOK)
134 return EEXIST;
135
136 return EOK;
137}
138
139static void inet_callback_create_srv(inet_client_t *client, ipc_callid_t callid,
140 ipc_call_t *call)
141{
142 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_callback_create_srv()");
143
144 async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
145 if (sess == NULL) {
146 async_answer_0(callid, ENOMEM);
147 return;
148 }
149
150 client->sess = sess;
151 async_answer_0(callid, EOK);
152}
153
154static int inet_find_dir(inet_addr_t *src, inet_addr_t *dest, uint8_t tos,
155 inet_dir_t *dir)
156{
157 inet_sroute_t *sr;
158
159 /* XXX Handle case where source address is specified */
160 (void) src;
161
162 dir->aobj = inet_addrobj_find(dest, iaf_net);
163 if (dir->aobj != NULL) {
164 dir->ldest = *dest;
165 dir->dtype = dt_direct;
166 } else {
167 /* No direct path, try using a static route */
168 sr = inet_sroute_find(dest);
169 if (sr != NULL) {
170 dir->aobj = inet_addrobj_find(&sr->router, iaf_net);
171 dir->ldest = sr->router;
172 dir->dtype = dt_router;
173 }
174 }
175
176 if (dir->aobj == NULL) {
177 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_send: No route to destination.");
178 return ENOENT;
179 }
180
181 return EOK;
182}
183
184int inet_route_packet(inet_dgram_t *dgram, uint8_t proto, uint8_t ttl,
185 int df)
186{
187 inet_dir_t dir;
188 int rc;
189
190 rc = inet_find_dir(&dgram->src, &dgram->dest, dgram->tos, &dir);
191 if (rc != EOK)
192 return rc;
193
194 return inet_addrobj_send_dgram(dir.aobj, &dir.ldest, dgram,
195 proto, ttl, df);
196}
197
198static int inet_send(inet_client_t *client, inet_dgram_t *dgram,
199 uint8_t proto, uint8_t ttl, int df)
200{
201 return inet_route_packet(dgram, proto, ttl, df);
202}
203
204int inet_get_srcaddr(inet_addr_t *remote, uint8_t tos, inet_addr_t *local)
205{
206 inet_dir_t dir;
207 int rc;
208
209 rc = inet_find_dir(NULL, remote, tos, &dir);
210 if (rc != EOK)
211 return rc;
212
213 /* XXX dt_local? */
214
215 /* Take source address from the address object */
216 inet_naddr_addr(&dir.aobj->naddr, local);
217 return EOK;
218}
219
220static void inet_get_srcaddr_srv(inet_client_t *client, ipc_callid_t iid,
221 ipc_call_t *icall)
222{
223 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srcaddr_srv()");
224
225 uint8_t tos = IPC_GET_ARG1(*icall);
226
227 ipc_callid_t callid;
228 size_t size;
229 if (!async_data_write_receive(&callid, &size)) {
230 async_answer_0(callid, EREFUSED);
231 async_answer_0(iid, EREFUSED);
232 return;
233 }
234
235 if (size != sizeof(inet_addr_t)) {
236 async_answer_0(callid, EINVAL);
237 async_answer_0(iid, EINVAL);
238 return;
239 }
240
241 inet_addr_t remote;
242 int rc = async_data_write_finalize(callid, &remote, size);
243 if (rc != EOK) {
244 async_answer_0(callid, rc);
245 async_answer_0(iid, rc);
246 }
247
248 inet_addr_t local;
249 rc = inet_get_srcaddr(&remote, tos, &local);
250 if (rc != EOK) {
251 async_answer_0(iid, rc);
252 return;
253 }
254
255 if (!async_data_read_receive(&callid, &size)) {
256 async_answer_0(callid, EREFUSED);
257 async_answer_0(iid, EREFUSED);
258 return;
259 }
260
261 if (size != sizeof(inet_addr_t)) {
262 async_answer_0(callid, EINVAL);
263 async_answer_0(iid, EINVAL);
264 return;
265 }
266
267 rc = async_data_read_finalize(callid, &local, size);
268 if (rc != EOK) {
269 async_answer_0(callid, rc);
270 async_answer_0(iid, rc);
271 return;
272 }
273
274 async_answer_0(iid, rc);
275}
276
277static void inet_send_srv(inet_client_t *client, ipc_callid_t iid,
278 ipc_call_t *icall)
279{
280 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_send_srv()");
281
282 inet_dgram_t dgram;
283
284 dgram.tos = IPC_GET_ARG1(*icall);
285
286 uint8_t ttl = IPC_GET_ARG2(*icall);
287 int df = IPC_GET_ARG3(*icall);
288
289 ipc_callid_t callid;
290 size_t size;
291 if (!async_data_write_receive(&callid, &size)) {
292 async_answer_0(callid, EREFUSED);
293 async_answer_0(iid, EREFUSED);
294 return;
295 }
296
297 if (size != sizeof(inet_addr_t)) {
298 async_answer_0(callid, EINVAL);
299 async_answer_0(iid, EINVAL);
300 return;
301 }
302
303 int rc = async_data_write_finalize(callid, &dgram.src, size);
304 if (rc != EOK) {
305 async_answer_0(callid, rc);
306 async_answer_0(iid, rc);
307 }
308
309 if (!async_data_write_receive(&callid, &size)) {
310 async_answer_0(callid, EREFUSED);
311 async_answer_0(iid, EREFUSED);
312 return;
313 }
314
315 if (size != sizeof(inet_addr_t)) {
316 async_answer_0(callid, EINVAL);
317 async_answer_0(iid, EINVAL);
318 return;
319 }
320
321 rc = async_data_write_finalize(callid, &dgram.dest, size);
322 if (rc != EOK) {
323 async_answer_0(callid, rc);
324 async_answer_0(iid, rc);
325 }
326
327 rc = async_data_write_accept(&dgram.data, false, 0, 0, 0,
328 &dgram.size);
329 if (rc != EOK) {
330 async_answer_0(iid, rc);
331 return;
332 }
333
334 rc = inet_send(client, &dgram, client->protocol, ttl, df);
335
336 free(dgram.data);
337 async_answer_0(iid, rc);
338}
339
340static void inet_set_proto_srv(inet_client_t *client, ipc_callid_t callid,
341 ipc_call_t *call)
342{
343 sysarg_t proto;
344
345 proto = IPC_GET_ARG1(*call);
346 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_set_proto_srv(%lu)", (unsigned long) proto);
347
348 if (proto > UINT8_MAX) {
349 async_answer_0(callid, EINVAL);
350 return;
351 }
352
353 client->protocol = proto;
354 async_answer_0(callid, EOK);
355}
356
357static void inet_client_init(inet_client_t *client)
358{
359 client->sess = NULL;
360
361 fibril_mutex_lock(&client_list_lock);
362 list_append(&client->client_list, &client_list);
363 fibril_mutex_unlock(&client_list_lock);
364}
365
366static void inet_client_fini(inet_client_t *client)
367{
368 async_hangup(client->sess);
369 client->sess = NULL;
370
371 fibril_mutex_lock(&client_list_lock);
372 list_remove(&client->client_list);
373 fibril_mutex_unlock(&client_list_lock);
374}
375
376static void inet_default_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
377{
378 inet_client_t client;
379
380 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_default_conn()");
381
382 /* Accept the connection */
383 async_answer_0(iid, EOK);
384
385 inet_client_init(&client);
386
387 while (true) {
388 ipc_call_t call;
389 ipc_callid_t callid = async_get_call(&call);
390 sysarg_t method = IPC_GET_IMETHOD(call);
391
392 if (!method) {
393 /* The other side has hung up */
394 async_answer_0(callid, EOK);
395 return;
396 }
397
398 switch (method) {
399 case INET_CALLBACK_CREATE:
400 inet_callback_create_srv(&client, callid, &call);
401 break;
402 case INET_GET_SRCADDR:
403 inet_get_srcaddr_srv(&client, callid, &call);
404 break;
405 case INET_SEND:
406 inet_send_srv(&client, callid, &call);
407 break;
408 case INET_SET_PROTO:
409 inet_set_proto_srv(&client, callid, &call);
410 break;
411 default:
412 async_answer_0(callid, EINVAL);
413 }
414 }
415
416 inet_client_fini(&client);
417}
418
419static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
420{
421 sysarg_t port;
422
423 port = IPC_GET_ARG1(*icall);
424
425 switch (port) {
426 case INET_PORT_DEFAULT:
427 inet_default_conn(iid, icall, arg);
428 break;
429 case INET_PORT_CFG:
430 inet_cfg_conn(iid, icall, arg);
431 break;
432 case INET_PORT_PING:
433 inetping_conn(iid, icall, arg);
434 break;
435 case INET_PORT_PING6:
436 inetping6_conn(iid, icall, arg);
437 break;
438 default:
439 async_answer_0(iid, ENOTSUP);
440 break;
441 }
442}
443
444static inet_client_t *inet_client_find(uint8_t proto)
445{
446 fibril_mutex_lock(&client_list_lock);
447
448 list_foreach(client_list, link) {
449 inet_client_t *client = list_get_instance(link, inet_client_t,
450 client_list);
451
452 if (client->protocol == proto) {
453 fibril_mutex_unlock(&client_list_lock);
454 return client;
455 }
456 }
457
458 fibril_mutex_unlock(&client_list_lock);
459 return NULL;
460}
461
462int inet_ev_recv(inet_client_t *client, inet_dgram_t *dgram)
463{
464 async_exch_t *exch = async_exchange_begin(client->sess);
465
466 ipc_call_t answer;
467 aid_t req = async_send_1(exch, INET_EV_RECV, dgram->tos, &answer);
468
469 int rc = async_data_write_start(exch, &dgram->src, sizeof(inet_addr_t));
470 if (rc != EOK) {
471 async_exchange_end(exch);
472 async_forget(req);
473 return rc;
474 }
475
476 rc = async_data_write_start(exch, &dgram->dest, sizeof(inet_addr_t));
477 if (rc != EOK) {
478 async_exchange_end(exch);
479 async_forget(req);
480 return rc;
481 }
482
483 rc = async_data_write_start(exch, dgram->data, dgram->size);
484
485 async_exchange_end(exch);
486
487 if (rc != EOK) {
488 async_forget(req);
489 return rc;
490 }
491
492 sysarg_t retval;
493 async_wait_for(req, &retval);
494
495 return (int) retval;
496}
497
498int inet_recv_dgram_local(inet_dgram_t *dgram, uint8_t proto)
499{
500 inet_client_t *client;
501
502 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_recv_dgram_local()");
503
504 /* ICMP and ICMPv6 messages are handled internally */
505 if (proto == IP_PROTO_ICMP)
506 return icmp_recv(dgram);
507
508 if (proto == IP_PROTO_ICMPV6)
509 return icmpv6_recv(dgram);
510
511 client = inet_client_find(proto);
512 if (client == NULL) {
513 log_msg(LOG_DEFAULT, LVL_DEBUG, "No client found for protocol 0x%" PRIx8,
514 proto);
515 return ENOENT;
516 }
517
518 return inet_ev_recv(client, dgram);
519}
520
521int inet_recv_packet(inet_packet_t *packet)
522{
523 inet_addrobj_t *addr;
524 inet_dgram_t dgram;
525
526 addr = inet_addrobj_find(&packet->dest, iaf_addr);
527 if ((addr != NULL) ||
528 (inet_naddr_compare_mask(&solicited_node_mask, &packet->dest)) ||
529 (inet_addr_compare(&multicast_all_nodes, &packet->dest))) {
530 /* Destined for one of the local addresses */
531
532 /* Check if packet is a complete datagram */
533 if (packet->offs == 0 && !packet->mf) {
534 /* It is complete deliver it immediately */
535 dgram.src = packet->src;
536 dgram.dest = packet->dest;
537 dgram.tos = packet->tos;
538 dgram.data = packet->data;
539 dgram.size = packet->size;
540
541 return inet_recv_dgram_local(&dgram, packet->proto);
542 } else {
543 /* It is a fragment, queue it for reassembly */
544 inet_reass_queue_packet(packet);
545 }
546 }
547
548 return ENOENT;
549}
550
551int main(int argc, char *argv[])
552{
553 int rc;
554
555 printf(NAME ": HelenOS Internet Protocol service\n");
556
557 if (log_init(NAME) != EOK) {
558 printf(NAME ": Failed to initialize logging.\n");
559 return 1;
560 }
561
562 rc = inet_init();
563 if (rc != EOK)
564 return 1;
565
566 printf(NAME ": Accepting connections.\n");
567 task_retval(0);
568 async_manager();
569
570 /* Not reached */
571 return 0;
572}
573
574/** @}
575 */
Note: See TracBrowser for help on using the repository browser.