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
RevLine 
[c76e926]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>
[ecff3d9]46#include <stdlib.h>
[c76e926]47#include <sys/types.h>
[02a09ed]48#include <net/socket_codes.h>
[ceba4bed]49#include "addrobj.h"
[637a3b4]50#include "icmp.h"
51#include "icmp_std.h"
[1d24ad3]52#include "icmpv6.h"
53#include "icmpv6_std.h"
[b4ec1ea]54#include "inetsrv.h"
[0e25780]55#include "inetcfg.h"
[6428115]56#include "inetping.h"
[1d24ad3]57#include "inetping6.h"
[e2e56e67]58#include "inet_link.h"
[7f95c904]59#include "reass.h"
[8bf672d]60#include "sroute.h"
[c76e926]61
[b4ec1ea]62#define NAME "inetsrv"
[c76e926]63
[9ae6fc7]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
[c76e926]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{
[a1a101d]82 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_init()");
[1038a9c]83
[c76e926]84 async_set_client_connection(inet_client_conn);
[1038a9c]85
86 int rc = loc_server_register(NAME);
[c76e926]87 if (rc != EOK) {
[a1a101d]88 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server (%d).", rc);
[c76e926]89 return EEXIST;
90 }
[1038a9c]91
92 service_id_t sid;
[0e25780]93 rc = loc_service_register_with_iface(SERVICE_NAME_INET, &sid,
94 INET_PORT_DEFAULT);
95 if (rc != EOK) {
[a1a101d]96 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service (%d).", rc);
[0e25780]97 return EEXIST;
98 }
[1038a9c]99
[0e25780]100 rc = loc_service_register_with_iface(SERVICE_NAME_INETCFG, &sid,
101 INET_PORT_CFG);
[c76e926]102 if (rc != EOK) {
[a1a101d]103 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service (%d).", rc);
[c76e926]104 return EEXIST;
105 }
[1038a9c]106
[6428115]107 rc = loc_service_register_with_iface(SERVICE_NAME_INETPING, &sid,
108 INET_PORT_PING);
109 if (rc != EOK) {
[a1a101d]110 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service (%d).", rc);
[6428115]111 return EEXIST;
112 }
[1038a9c]113
[1d24ad3]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
[25eec4ef]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
[a2e3ee6]127 inet_naddr(&sroute->dest, 0, 0, 0, 0, 0);
128 inet_addr(&sroute->router, 10, 0, 2, 2);
[25eec4ef]129 sroute->name = str_dup("default");
130 inet_sroute_add(sroute);
131
[a2e3ee6]132 rc = inet_link_discovery_start();
[e2e56e67]133 if (rc != EOK)
134 return EEXIST;
[1038a9c]135
[c76e926]136 return EOK;
137}
138
[ceba4bed]139static void inet_callback_create_srv(inet_client_t *client, ipc_callid_t callid,
[c76e926]140 ipc_call_t *call)
141{
[a1a101d]142 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_callback_create_srv()");
[c76e926]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
[8bf672d]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) {
[a1a101d]177 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_send: No route to destination.");
[8bf672d]178 return ENOENT;
179 }
180
181 return EOK;
182}
183
[637a3b4]184int inet_route_packet(inet_dgram_t *dgram, uint8_t proto, uint8_t ttl,
[2ff150e]185 int df)
[ceba4bed]186{
[8bf672d]187 inet_dir_t dir;
188 int rc;
[ceba4bed]189
[8bf672d]190 rc = inet_find_dir(&dgram->src, &dgram->dest, dgram->tos, &dir);
191 if (rc != EOK)
192 return rc;
[ceba4bed]193
[8bf672d]194 return inet_addrobj_send_dgram(dir.aobj, &dir.ldest, dgram,
195 proto, ttl, df);
[ceba4bed]196}
197
[e767dbf]198static int inet_send(inet_client_t *client, inet_dgram_t *dgram,
[2ff150e]199 uint8_t proto, uint8_t ttl, int df)
[e767dbf]200{
[2ff150e]201 return inet_route_packet(dgram, proto, ttl, df);
[e767dbf]202}
203
[6428115]204int inet_get_srcaddr(inet_addr_t *remote, uint8_t tos, inet_addr_t *local)
[bd8bfc5a]205{
[8bf672d]206 inet_dir_t dir;
207 int rc;
[bd8bfc5a]208
[8bf672d]209 rc = inet_find_dir(NULL, remote, tos, &dir);
210 if (rc != EOK)
211 return rc;
[bd8bfc5a]212
[8bf672d]213 /* XXX dt_local? */
214
215 /* Take source address from the address object */
[a2e3ee6]216 inet_naddr_addr(&dir.aobj->naddr, local);
[8bf672d]217 return EOK;
[bd8bfc5a]218}
219
[d8b47eca]220static void inet_get_srcaddr_srv(inet_client_t *client, ipc_callid_t iid,
221 ipc_call_t *icall)
[ecff3d9]222{
[a2e3ee6]223 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srcaddr_srv()");
224
[d8b47eca]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 }
[a2e3ee6]240
[02a09ed]241 inet_addr_t remote;
[d8b47eca]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 }
[02a09ed]247
[bd8bfc5a]248 inet_addr_t local;
[d8b47eca]249 rc = inet_get_srcaddr(&remote, tos, &local);
[a2e3ee6]250 if (rc != EOK) {
[d8b47eca]251 async_answer_0(iid, rc);
[a2e3ee6]252 return;
253 }
254
[d8b47eca]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)) {
[02a09ed]262 async_answer_0(callid, EINVAL);
[d8b47eca]263 async_answer_0(iid, EINVAL);
[a2e3ee6]264 return;
265 }
266
[d8b47eca]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);
[ecff3d9]275}
276
[d8b47eca]277static void inet_send_srv(inet_client_t *client, ipc_callid_t iid,
278 ipc_call_t *icall)
[c76e926]279{
[a1a101d]280 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_send_srv()");
[a2e3ee6]281
282 inet_dgram_t dgram;
283
[d8b47eca]284 dgram.tos = IPC_GET_ARG1(*icall);
[a2e3ee6]285
[d8b47eca]286 uint8_t ttl = IPC_GET_ARG2(*icall);
287 int df = IPC_GET_ARG3(*icall);
[a2e3ee6]288
[d8b47eca]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);
[ecff3d9]322 if (rc != EOK) {
323 async_answer_0(callid, rc);
[d8b47eca]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);
[ecff3d9]331 return;
332 }
[a2e3ee6]333
[2ff150e]334 rc = inet_send(client, &dgram, client->protocol, ttl, df);
[a2e3ee6]335
[59157eb]336 free(dgram.data);
[d8b47eca]337 async_answer_0(iid, rc);
[c76e926]338}
339
[ceba4bed]340static void inet_set_proto_srv(inet_client_t *client, ipc_callid_t callid,
[c76e926]341 ipc_call_t *call)
342{
343 sysarg_t proto;
344
345 proto = IPC_GET_ARG1(*call);
[a1a101d]346 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_set_proto_srv(%lu)", (unsigned long) proto);
[c76e926]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
[0e25780]376static void inet_default_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[c76e926]377{
378 inet_client_t client;
379
[a1a101d]380 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_default_conn()");
[c76e926]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:
[ceba4bed]400 inet_callback_create_srv(&client, callid, &call);
[c76e926]401 break;
[ecff3d9]402 case INET_GET_SRCADDR:
[ceba4bed]403 inet_get_srcaddr_srv(&client, callid, &call);
[ecff3d9]404 break;
[c76e926]405 case INET_SEND:
[ceba4bed]406 inet_send_srv(&client, callid, &call);
[c76e926]407 break;
408 case INET_SET_PROTO:
[ceba4bed]409 inet_set_proto_srv(&client, callid, &call);
[c76e926]410 break;
411 default:
412 async_answer_0(callid, EINVAL);
413 }
414 }
415
416 inet_client_fini(&client);
417}
418
[0e25780]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;
[6428115]432 case INET_PORT_PING:
433 inetping_conn(iid, icall, arg);
434 break;
[1d24ad3]435 case INET_PORT_PING6:
436 inetping6_conn(iid, icall, arg);
437 break;
[0e25780]438 default:
439 async_answer_0(iid, ENOTSUP);
440 break;
441 }
442}
443
[fe4310f]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
[59157eb]462int inet_ev_recv(inet_client_t *client, inet_dgram_t *dgram)
463{
[02a09ed]464 async_exch_t *exch = async_exchange_begin(client->sess);
[a2e3ee6]465
[02a09ed]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);
[a2e3ee6]473 return rc;
[02a09ed]474 }
[a2e3ee6]475
[02a09ed]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 }
[a2e3ee6]482
483 rc = async_data_write_start(exch, dgram->data, dgram->size);
[02a09ed]484
[59157eb]485 async_exchange_end(exch);
[a2e3ee6]486
[59157eb]487 if (rc != EOK) {
[50b581d]488 async_forget(req);
[59157eb]489 return rc;
490 }
[a2e3ee6]491
[59157eb]492 sysarg_t retval;
493 async_wait_for(req, &retval);
[a2e3ee6]494
[02a09ed]495 return (int) retval;
[59157eb]496}
497
[7f95c904]498int inet_recv_dgram_local(inet_dgram_t *dgram, uint8_t proto)
[e767dbf]499{
[fe4310f]500 inet_client_t *client;
501
[a1a101d]502 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_recv_dgram_local()");
[fe4310f]503
[1d24ad3]504 /* ICMP and ICMPv6 messages are handled internally */
[637a3b4]505 if (proto == IP_PROTO_ICMP)
506 return icmp_recv(dgram);
[1d24ad3]507
508 if (proto == IP_PROTO_ICMPV6)
509 return icmpv6_recv(dgram);
[637a3b4]510
[fe4310f]511 client = inet_client_find(proto);
512 if (client == NULL) {
[a1a101d]513 log_msg(LOG_DEFAULT, LVL_DEBUG, "No client found for protocol 0x%" PRIx8,
[fe4310f]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);
[9ae6fc7]527 if ((addr != NULL) ||
528 (inet_naddr_compare_mask(&solicited_node_mask, &packet->dest)) ||
529 (inet_addr_compare(&multicast_all_nodes, &packet->dest))) {
[fe4310f]530 /* Destined for one of the local addresses */
531
[7f95c904]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 }
[fe4310f]546 }
547
548 return ENOENT;
[e767dbf]549}
550
[c76e926]551int main(int argc, char *argv[])
552{
553 int rc;
554
[e2e56e67]555 printf(NAME ": HelenOS Internet Protocol service\n");
[c76e926]556
[267f235]557 if (log_init(NAME) != EOK) {
[e2e56e67]558 printf(NAME ": Failed to initialize logging.\n");
[c76e926]559 return 1;
560 }
561
562 rc = inet_init();
563 if (rc != EOK)
564 return 1;
565
[ecff3d9]566 printf(NAME ": Accepting connections.\n");
[c76e926]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.