source: mainline/uspace/srv/net/inetsrv/inetsrv.c@ 1433ecda

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1433ecda was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

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