source: mainline/uspace/srv/net/inetsrv/inetsrv.c@ 3233adb

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

unify interface API

  • introduce new interfaces
  • unify location service clients to always expect service ID as the second argument
  • remove obsolete methods that take explicit exchange management arguments (first phase)
  • use interfaces in device drivers, devman, location service, logger, inet
  • Property mode set to 100644
File size: 12.9 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 <task.h>
49#include "addrobj.h"
50#include "icmp.h"
51#include "icmp_std.h"
52#include "icmpv6.h"
53#include "icmpv6_std.h"
54#include "inetsrv.h"
55#include "inetcfg.h"
56#include "inetping.h"
57#include "inet_link.h"
58#include "reass.h"
59#include "sroute.h"
60
61#define NAME "inetsrv"
62
63static inet_naddr_t solicited_node_mask = {
64 .version = ip_v6,
65 .addr6 = {0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0xff, 0, 0, 0},
66 .prefix = 104
67};
68
69static inet_addr_t broadcast4_all_hosts = {
70 .version = ip_v4,
71 .addr = 0xffffffff
72};
73
74static inet_addr_t multicast_all_nodes = {
75 .version = ip_v6,
76 .addr6 = {0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
77};
78
79static FIBRIL_MUTEX_INITIALIZE(client_list_lock);
80static LIST_INITIALIZE(client_list);
81
82static void inet_default_conn(ipc_callid_t, ipc_call_t *, void *);
83
84static int inet_init(void)
85{
86 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_init()");
87
88 port_id_t port;
89 int rc = async_create_port(INTERFACE_INET,
90 inet_default_conn, NULL, &port);
91 if (rc != EOK)
92 return rc;
93
94 rc = async_create_port(INTERFACE_INETCFG,
95 inet_cfg_conn, NULL, &port);
96 if (rc != EOK)
97 return rc;
98
99 rc = async_create_port(INTERFACE_INETPING,
100 inetping_conn, NULL, &port);
101 if (rc != EOK)
102 return rc;
103
104 rc = loc_server_register(NAME);
105 if (rc != EOK) {
106 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server (%d).", rc);
107 return EEXIST;
108 }
109
110 service_id_t sid;
111 rc = loc_service_register(SERVICE_NAME_INET, &sid);
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 inet_client_t *inet_client_find(uint8_t proto)
431{
432 fibril_mutex_lock(&client_list_lock);
433
434 list_foreach(client_list, client_list, inet_client_t, client) {
435 if (client->protocol == proto) {
436 fibril_mutex_unlock(&client_list_lock);
437 return client;
438 }
439 }
440
441 fibril_mutex_unlock(&client_list_lock);
442 return NULL;
443}
444
445int inet_ev_recv(inet_client_t *client, inet_dgram_t *dgram)
446{
447 async_exch_t *exch = async_exchange_begin(client->sess);
448
449 ipc_call_t answer;
450
451 log_msg(LOG_DEFAULT, LVL_NOTE, "inet_ev_recv: iplink=%zu",
452 dgram->iplink);
453
454 aid_t req = async_send_2(exch, INET_EV_RECV, dgram->tos,
455 dgram->iplink, &answer);
456
457 int rc = async_data_write_start(exch, &dgram->src, sizeof(inet_addr_t));
458 if (rc != EOK) {
459 async_exchange_end(exch);
460 async_forget(req);
461 return rc;
462 }
463
464 rc = async_data_write_start(exch, &dgram->dest, sizeof(inet_addr_t));
465 if (rc != EOK) {
466 async_exchange_end(exch);
467 async_forget(req);
468 return rc;
469 }
470
471 rc = async_data_write_start(exch, dgram->data, dgram->size);
472
473 async_exchange_end(exch);
474
475 if (rc != EOK) {
476 async_forget(req);
477 return rc;
478 }
479
480 sysarg_t retval;
481 async_wait_for(req, &retval);
482
483 return (int) retval;
484}
485
486int inet_recv_dgram_local(inet_dgram_t *dgram, uint8_t proto)
487{
488 inet_client_t *client;
489
490 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_recv_dgram_local()");
491
492 /* ICMP and ICMPv6 messages are handled internally */
493 if (proto == IP_PROTO_ICMP)
494 return icmp_recv(dgram);
495
496 if (proto == IP_PROTO_ICMPV6)
497 return icmpv6_recv(dgram);
498
499 client = inet_client_find(proto);
500 if (client == NULL) {
501 log_msg(LOG_DEFAULT, LVL_DEBUG, "No client found for protocol 0x%" PRIx8,
502 proto);
503 return ENOENT;
504 }
505
506 return inet_ev_recv(client, dgram);
507}
508
509int inet_recv_packet(inet_packet_t *packet)
510{
511 inet_addrobj_t *addr;
512 inet_dgram_t dgram;
513
514 addr = inet_addrobj_find(&packet->dest, iaf_addr);
515 if ((addr != NULL) ||
516 (inet_naddr_compare_mask(&solicited_node_mask, &packet->dest)) ||
517 (inet_addr_compare(&multicast_all_nodes, &packet->dest)) ||
518 (inet_addr_compare(&broadcast4_all_hosts, &packet->dest))) {
519 /* Destined for one of the local addresses */
520
521 /* Check if packet is a complete datagram */
522 if (packet->offs == 0 && !packet->mf) {
523 /* It is complete deliver it immediately */
524 dgram.iplink = packet->link_id;
525 dgram.src = packet->src;
526 dgram.dest = packet->dest;
527 dgram.tos = packet->tos;
528 dgram.data = packet->data;
529 dgram.size = packet->size;
530
531 return inet_recv_dgram_local(&dgram, packet->proto);
532 } else {
533 /* It is a fragment, queue it for reassembly */
534 inet_reass_queue_packet(packet);
535 }
536 }
537
538 return ENOENT;
539}
540
541int main(int argc, char *argv[])
542{
543 int rc;
544
545 printf(NAME ": HelenOS Internet Protocol service\n");
546
547 if (log_init(NAME) != EOK) {
548 printf(NAME ": Failed to initialize logging.\n");
549 return 1;
550 }
551
552 rc = inet_init();
553 if (rc != EOK)
554 return 1;
555
556 printf(NAME ": Accepting connections.\n");
557 task_retval(0);
558 async_manager();
559
560 /* Not reached */
561 return 0;
562}
563
564/** @}
565 */
Note: See TracBrowser for help on using the repository browser.