source: mainline/uspace/srv/net/inetsrv/inetsrv.c

Last change on this file was ca48672, checked in by Jiri Svoboda <jiri@…>, 8 days ago

loc_service_register() needs to take a port ID argument.

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