source: mainline/uspace/srv/net/inetsrv/inetsrv.c@ 10005fd

Last change on this file since 10005fd was 1bbc6dc, checked in by Jiri Svoboda <jiri@…>, 13 months ago

Network configuration persistence.

nconfsrv is folded into inetsrv
DHCP is disabled when a static address is configured on a link

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