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

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

Fix incorrect Dont Fragment flag.

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