source: mainline/uspace/srv/net/inetsrv/inetsrv.c@ 02a09ed

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

add basic infrastructure for IPv6 (inactive)
make inet_addr_t a universal address type

  • Property mode set to 100644
File size: 11.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"
[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
[02a09ed]204 addr32_t remote_v4 = IPC_GET_ARG1(*call);
[a2e3ee6]205 uint8_t tos = IPC_GET_ARG2(*call);
206
[02a09ed]207 inet_addr_t remote;
208 inet_addr_set(remote_v4, &remote);
209
[bd8bfc5a]210 inet_addr_t local;
[a2e3ee6]211 int rc = inet_get_srcaddr(&remote, tos, &local);
212 if (rc != EOK) {
213 async_answer_0(callid, rc);
214 return;
215 }
216
[02a09ed]217 addr32_t local_v4;
218 uint16_t family = inet_addr_get(&local, &local_v4, NULL);
219 if (family != AF_INET) {
220 async_answer_0(callid, EINVAL);
[a2e3ee6]221 return;
222 }
223
[02a09ed]224 async_answer_1(callid, rc, (sysarg_t) local_v4);
[ecff3d9]225}
226
[ceba4bed]227static void inet_send_srv(inet_client_t *client, ipc_callid_t callid,
[c76e926]228 ipc_call_t *call)
229{
[a1a101d]230 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_send_srv()");
[a2e3ee6]231
[02a09ed]232 addr32_t src_v4 = IPC_GET_ARG1(*call);
233 addr32_t dest_v4 = IPC_GET_ARG2(*call);
234
[a2e3ee6]235 inet_dgram_t dgram;
236
[02a09ed]237 inet_addr_set(src_v4, &dgram.src);
238 inet_addr_set(dest_v4, &dgram.dest);
[59157eb]239 dgram.tos = IPC_GET_ARG3(*call);
[a2e3ee6]240
241 uint8_t ttl = IPC_GET_ARG4(*call);
242 int df = IPC_GET_ARG5(*call);
243
244 int rc = async_data_write_accept(&dgram.data, false, 0, 0, 0,
245 &dgram.size);
[ecff3d9]246 if (rc != EOK) {
247 async_answer_0(callid, rc);
248 return;
249 }
[a2e3ee6]250
[2ff150e]251 rc = inet_send(client, &dgram, client->protocol, ttl, df);
[a2e3ee6]252
[59157eb]253 free(dgram.data);
[ceba4bed]254 async_answer_0(callid, rc);
[c76e926]255}
256
[ceba4bed]257static void inet_set_proto_srv(inet_client_t *client, ipc_callid_t callid,
[c76e926]258 ipc_call_t *call)
259{
260 sysarg_t proto;
261
262 proto = IPC_GET_ARG1(*call);
[a1a101d]263 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_set_proto_srv(%lu)", (unsigned long) proto);
[c76e926]264
265 if (proto > UINT8_MAX) {
266 async_answer_0(callid, EINVAL);
267 return;
268 }
269
270 client->protocol = proto;
271 async_answer_0(callid, EOK);
272}
273
274static void inet_client_init(inet_client_t *client)
275{
276 client->sess = NULL;
277
278 fibril_mutex_lock(&client_list_lock);
279 list_append(&client->client_list, &client_list);
280 fibril_mutex_unlock(&client_list_lock);
281}
282
283static void inet_client_fini(inet_client_t *client)
284{
285 async_hangup(client->sess);
286 client->sess = NULL;
287
288 fibril_mutex_lock(&client_list_lock);
289 list_remove(&client->client_list);
290 fibril_mutex_unlock(&client_list_lock);
291}
292
[0e25780]293static void inet_default_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[c76e926]294{
295 inet_client_t client;
296
[a1a101d]297 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_default_conn()");
[c76e926]298
299 /* Accept the connection */
300 async_answer_0(iid, EOK);
301
302 inet_client_init(&client);
303
304 while (true) {
305 ipc_call_t call;
306 ipc_callid_t callid = async_get_call(&call);
307 sysarg_t method = IPC_GET_IMETHOD(call);
308
309 if (!method) {
310 /* The other side has hung up */
311 async_answer_0(callid, EOK);
312 return;
313 }
314
315 switch (method) {
316 case INET_CALLBACK_CREATE:
[ceba4bed]317 inet_callback_create_srv(&client, callid, &call);
[c76e926]318 break;
[ecff3d9]319 case INET_GET_SRCADDR:
[ceba4bed]320 inet_get_srcaddr_srv(&client, callid, &call);
[ecff3d9]321 break;
[c76e926]322 case INET_SEND:
[ceba4bed]323 inet_send_srv(&client, callid, &call);
[c76e926]324 break;
325 case INET_SET_PROTO:
[ceba4bed]326 inet_set_proto_srv(&client, callid, &call);
[c76e926]327 break;
328 default:
329 async_answer_0(callid, EINVAL);
330 }
331 }
332
333 inet_client_fini(&client);
334}
335
[0e25780]336static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
337{
338 sysarg_t port;
339
340 port = IPC_GET_ARG1(*icall);
341
342 switch (port) {
343 case INET_PORT_DEFAULT:
344 inet_default_conn(iid, icall, arg);
345 break;
346 case INET_PORT_CFG:
347 inet_cfg_conn(iid, icall, arg);
348 break;
[6428115]349 case INET_PORT_PING:
350 inetping_conn(iid, icall, arg);
351 break;
[0e25780]352 default:
353 async_answer_0(iid, ENOTSUP);
354 break;
355 }
356}
357
[fe4310f]358static inet_client_t *inet_client_find(uint8_t proto)
359{
360 fibril_mutex_lock(&client_list_lock);
361
362 list_foreach(client_list, link) {
363 inet_client_t *client = list_get_instance(link, inet_client_t,
364 client_list);
365
366 if (client->protocol == proto) {
367 fibril_mutex_unlock(&client_list_lock);
368 return client;
369 }
370 }
371
372 fibril_mutex_unlock(&client_list_lock);
373 return NULL;
374}
375
[59157eb]376int inet_ev_recv(inet_client_t *client, inet_dgram_t *dgram)
377{
[02a09ed]378 async_exch_t *exch = async_exchange_begin(client->sess);
[a2e3ee6]379
[02a09ed]380 ipc_call_t answer;
381 aid_t req = async_send_1(exch, INET_EV_RECV, dgram->tos, &answer);
382
383 int rc = async_data_write_start(exch, &dgram->src, sizeof(inet_addr_t));
384 if (rc != EOK) {
385 async_exchange_end(exch);
386 async_forget(req);
[a2e3ee6]387 return rc;
[02a09ed]388 }
[a2e3ee6]389
[02a09ed]390 rc = async_data_write_start(exch, &dgram->dest, sizeof(inet_addr_t));
391 if (rc != EOK) {
392 async_exchange_end(exch);
393 async_forget(req);
394 return rc;
395 }
[a2e3ee6]396
397 rc = async_data_write_start(exch, dgram->data, dgram->size);
[02a09ed]398
[59157eb]399 async_exchange_end(exch);
[a2e3ee6]400
[59157eb]401 if (rc != EOK) {
[50b581d]402 async_forget(req);
[59157eb]403 return rc;
404 }
[a2e3ee6]405
[59157eb]406 sysarg_t retval;
407 async_wait_for(req, &retval);
[a2e3ee6]408
[02a09ed]409 return (int) retval;
[59157eb]410}
411
[7f95c904]412int inet_recv_dgram_local(inet_dgram_t *dgram, uint8_t proto)
[e767dbf]413{
[fe4310f]414 inet_client_t *client;
415
[a1a101d]416 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_recv_dgram_local()");
[fe4310f]417
[637a3b4]418 /* ICMP messages are handled internally */
419 if (proto == IP_PROTO_ICMP)
420 return icmp_recv(dgram);
421
[fe4310f]422 client = inet_client_find(proto);
423 if (client == NULL) {
[a1a101d]424 log_msg(LOG_DEFAULT, LVL_DEBUG, "No client found for protocol 0x%" PRIx8,
[fe4310f]425 proto);
426 return ENOENT;
427 }
428
429 return inet_ev_recv(client, dgram);
430}
431
432int inet_recv_packet(inet_packet_t *packet)
433{
434 inet_addrobj_t *addr;
435 inet_dgram_t dgram;
436
437 addr = inet_addrobj_find(&packet->dest, iaf_addr);
438 if (addr != NULL) {
439 /* Destined for one of the local addresses */
440
[7f95c904]441 /* Check if packet is a complete datagram */
442 if (packet->offs == 0 && !packet->mf) {
443 /* It is complete deliver it immediately */
444 dgram.src = packet->src;
445 dgram.dest = packet->dest;
446 dgram.tos = packet->tos;
447 dgram.data = packet->data;
448 dgram.size = packet->size;
449
450 return inet_recv_dgram_local(&dgram, packet->proto);
451 } else {
452 /* It is a fragment, queue it for reassembly */
453 inet_reass_queue_packet(packet);
454 }
[fe4310f]455 }
456
457 return ENOENT;
[e767dbf]458}
459
[c76e926]460int main(int argc, char *argv[])
461{
462 int rc;
463
[e2e56e67]464 printf(NAME ": HelenOS Internet Protocol service\n");
[c76e926]465
[267f235]466 if (log_init(NAME) != EOK) {
[e2e56e67]467 printf(NAME ": Failed to initialize logging.\n");
[c76e926]468 return 1;
469 }
470
471 rc = inet_init();
472 if (rc != EOK)
473 return 1;
474
[ecff3d9]475 printf(NAME ": Accepting connections.\n");
[c76e926]476 task_retval(0);
477 async_manager();
478
479 /* Not reached */
480 return 0;
481}
482
483/** @}
484 */
Note: See TracBrowser for help on using the repository browser.