source: mainline/uspace/srv/net/inetsrv/inetsrv.c@ 7901ac8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7901ac8 was a2e3ee6, checked in by Martin Decky <martin@…>, 13 years ago

use new network address infrastructure (towards IPv6 support)

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