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 <net/socket_codes.h>
|
---|
49 | #include "addrobj.h"
|
---|
50 | #include "icmp.h"
|
---|
51 | #include "icmp_std.h"
|
---|
52 | #include "inetsrv.h"
|
---|
53 | #include "inetcfg.h"
|
---|
54 | #include "inetping.h"
|
---|
55 | #include "inet_link.h"
|
---|
56 | #include "reass.h"
|
---|
57 | #include "sroute.h"
|
---|
58 |
|
---|
59 | #define NAME "inetsrv"
|
---|
60 |
|
---|
61 | static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
---|
62 |
|
---|
63 | static FIBRIL_MUTEX_INITIALIZE(client_list_lock);
|
---|
64 | static LIST_INITIALIZE(client_list);
|
---|
65 |
|
---|
66 | static int inet_init(void)
|
---|
67 | {
|
---|
68 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_init()");
|
---|
69 |
|
---|
70 | async_set_client_connection(inet_client_conn);
|
---|
71 |
|
---|
72 | int rc = loc_server_register(NAME);
|
---|
73 | if (rc != EOK) {
|
---|
74 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server (%d).", rc);
|
---|
75 | return EEXIST;
|
---|
76 | }
|
---|
77 |
|
---|
78 | service_id_t sid;
|
---|
79 | rc = loc_service_register_with_iface(SERVICE_NAME_INET, &sid,
|
---|
80 | INET_PORT_DEFAULT);
|
---|
81 | if (rc != EOK) {
|
---|
82 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service (%d).", rc);
|
---|
83 | return EEXIST;
|
---|
84 | }
|
---|
85 |
|
---|
86 | rc = loc_service_register_with_iface(SERVICE_NAME_INETCFG, &sid,
|
---|
87 | INET_PORT_CFG);
|
---|
88 | if (rc != EOK) {
|
---|
89 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service (%d).", rc);
|
---|
90 | return EEXIST;
|
---|
91 | }
|
---|
92 |
|
---|
93 | rc = loc_service_register_with_iface(SERVICE_NAME_INETPING, &sid,
|
---|
94 | INET_PORT_PING);
|
---|
95 | if (rc != EOK) {
|
---|
96 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service (%d).", rc);
|
---|
97 | return EEXIST;
|
---|
98 | }
|
---|
99 |
|
---|
100 | inet_sroute_t *sroute = inet_sroute_new();
|
---|
101 | if (sroute == NULL) {
|
---|
102 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed creating default route (%d).", rc);
|
---|
103 | return ENOMEM;
|
---|
104 | }
|
---|
105 |
|
---|
106 | inet_naddr(&sroute->dest, 0, 0, 0, 0, 0);
|
---|
107 | inet_addr(&sroute->router, 10, 0, 2, 2);
|
---|
108 | sroute->name = str_dup("default");
|
---|
109 | inet_sroute_add(sroute);
|
---|
110 |
|
---|
111 | rc = inet_link_discovery_start();
|
---|
112 | if (rc != EOK)
|
---|
113 | return EEXIST;
|
---|
114 |
|
---|
115 | return EOK;
|
---|
116 | }
|
---|
117 |
|
---|
118 | static void inet_callback_create_srv(inet_client_t *client, ipc_callid_t callid,
|
---|
119 | ipc_call_t *call)
|
---|
120 | {
|
---|
121 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_callback_create_srv()");
|
---|
122 |
|
---|
123 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
124 | if (sess == NULL) {
|
---|
125 | async_answer_0(callid, ENOMEM);
|
---|
126 | return;
|
---|
127 | }
|
---|
128 |
|
---|
129 | client->sess = sess;
|
---|
130 | async_answer_0(callid, EOK);
|
---|
131 | }
|
---|
132 |
|
---|
133 | static int inet_find_dir(inet_addr_t *src, inet_addr_t *dest, uint8_t tos,
|
---|
134 | inet_dir_t *dir)
|
---|
135 | {
|
---|
136 | inet_sroute_t *sr;
|
---|
137 |
|
---|
138 | /* XXX Handle case where source address is specified */
|
---|
139 | (void) src;
|
---|
140 |
|
---|
141 | dir->aobj = inet_addrobj_find(dest, iaf_net);
|
---|
142 | if (dir->aobj != NULL) {
|
---|
143 | dir->ldest = *dest;
|
---|
144 | dir->dtype = dt_direct;
|
---|
145 | } else {
|
---|
146 | /* No direct path, try using a static route */
|
---|
147 | sr = inet_sroute_find(dest);
|
---|
148 | if (sr != NULL) {
|
---|
149 | dir->aobj = inet_addrobj_find(&sr->router, iaf_net);
|
---|
150 | dir->ldest = sr->router;
|
---|
151 | dir->dtype = dt_router;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | if (dir->aobj == NULL) {
|
---|
156 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_send: No route to destination.");
|
---|
157 | return ENOENT;
|
---|
158 | }
|
---|
159 |
|
---|
160 | return EOK;
|
---|
161 | }
|
---|
162 |
|
---|
163 | int inet_route_packet(inet_dgram_t *dgram, uint8_t proto, uint8_t ttl,
|
---|
164 | int df)
|
---|
165 | {
|
---|
166 | inet_dir_t dir;
|
---|
167 | int rc;
|
---|
168 |
|
---|
169 | rc = inet_find_dir(&dgram->src, &dgram->dest, dgram->tos, &dir);
|
---|
170 | if (rc != EOK)
|
---|
171 | return rc;
|
---|
172 |
|
---|
173 | return inet_addrobj_send_dgram(dir.aobj, &dir.ldest, dgram,
|
---|
174 | proto, ttl, df);
|
---|
175 | }
|
---|
176 |
|
---|
177 | static int inet_send(inet_client_t *client, inet_dgram_t *dgram,
|
---|
178 | uint8_t proto, uint8_t ttl, int df)
|
---|
179 | {
|
---|
180 | return inet_route_packet(dgram, proto, ttl, df);
|
---|
181 | }
|
---|
182 |
|
---|
183 | int inet_get_srcaddr(inet_addr_t *remote, uint8_t tos, inet_addr_t *local)
|
---|
184 | {
|
---|
185 | inet_dir_t dir;
|
---|
186 | int rc;
|
---|
187 |
|
---|
188 | rc = inet_find_dir(NULL, remote, tos, &dir);
|
---|
189 | if (rc != EOK)
|
---|
190 | return rc;
|
---|
191 |
|
---|
192 | /* XXX dt_local? */
|
---|
193 |
|
---|
194 | /* Take source address from the address object */
|
---|
195 | inet_naddr_addr(&dir.aobj->naddr, local);
|
---|
196 | return EOK;
|
---|
197 | }
|
---|
198 |
|
---|
199 | static void inet_get_srcaddr_srv(inet_client_t *client, ipc_callid_t callid,
|
---|
200 | ipc_call_t *call)
|
---|
201 | {
|
---|
202 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srcaddr_srv()");
|
---|
203 |
|
---|
204 | addr32_t remote_v4 = IPC_GET_ARG1(*call);
|
---|
205 | uint8_t tos = IPC_GET_ARG2(*call);
|
---|
206 |
|
---|
207 | inet_addr_t remote;
|
---|
208 | inet_addr_set(remote_v4, &remote);
|
---|
209 |
|
---|
210 | inet_addr_t local;
|
---|
211 | int rc = inet_get_srcaddr(&remote, tos, &local);
|
---|
212 | if (rc != EOK) {
|
---|
213 | async_answer_0(callid, rc);
|
---|
214 | return;
|
---|
215 | }
|
---|
216 |
|
---|
217 | addr32_t local_v4;
|
---|
218 | uint16_t family = inet_addr_get(&local, &local_v4, NULL);
|
---|
219 | if (family != AF_INET) {
|
---|
220 | async_answer_0(callid, EINVAL);
|
---|
221 | return;
|
---|
222 | }
|
---|
223 |
|
---|
224 | async_answer_1(callid, rc, (sysarg_t) local_v4);
|
---|
225 | }
|
---|
226 |
|
---|
227 | static void inet_send_srv(inet_client_t *client, ipc_callid_t callid,
|
---|
228 | ipc_call_t *call)
|
---|
229 | {
|
---|
230 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_send_srv()");
|
---|
231 |
|
---|
232 | addr32_t src_v4 = IPC_GET_ARG1(*call);
|
---|
233 | addr32_t dest_v4 = IPC_GET_ARG2(*call);
|
---|
234 |
|
---|
235 | inet_dgram_t dgram;
|
---|
236 |
|
---|
237 | inet_addr_set(src_v4, &dgram.src);
|
---|
238 | inet_addr_set(dest_v4, &dgram.dest);
|
---|
239 | dgram.tos = IPC_GET_ARG3(*call);
|
---|
240 |
|
---|
241 | uint8_t ttl = IPC_GET_ARG4(*call);
|
---|
242 | int df = IPC_GET_ARG5(*call);
|
---|
243 |
|
---|
244 | int rc = async_data_write_accept(&dgram.data, false, 0, 0, 0,
|
---|
245 | &dgram.size);
|
---|
246 | if (rc != EOK) {
|
---|
247 | async_answer_0(callid, rc);
|
---|
248 | return;
|
---|
249 | }
|
---|
250 |
|
---|
251 | rc = inet_send(client, &dgram, client->protocol, ttl, df);
|
---|
252 |
|
---|
253 | free(dgram.data);
|
---|
254 | async_answer_0(callid, rc);
|
---|
255 | }
|
---|
256 |
|
---|
257 | static void inet_set_proto_srv(inet_client_t *client, ipc_callid_t callid,
|
---|
258 | ipc_call_t *call)
|
---|
259 | {
|
---|
260 | sysarg_t proto;
|
---|
261 |
|
---|
262 | proto = IPC_GET_ARG1(*call);
|
---|
263 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_set_proto_srv(%lu)", (unsigned long) proto);
|
---|
264 |
|
---|
265 | if (proto > UINT8_MAX) {
|
---|
266 | async_answer_0(callid, EINVAL);
|
---|
267 | return;
|
---|
268 | }
|
---|
269 |
|
---|
270 | client->protocol = proto;
|
---|
271 | async_answer_0(callid, EOK);
|
---|
272 | }
|
---|
273 |
|
---|
274 | static void inet_client_init(inet_client_t *client)
|
---|
275 | {
|
---|
276 | client->sess = NULL;
|
---|
277 |
|
---|
278 | fibril_mutex_lock(&client_list_lock);
|
---|
279 | list_append(&client->client_list, &client_list);
|
---|
280 | fibril_mutex_unlock(&client_list_lock);
|
---|
281 | }
|
---|
282 |
|
---|
283 | static void inet_client_fini(inet_client_t *client)
|
---|
284 | {
|
---|
285 | async_hangup(client->sess);
|
---|
286 | client->sess = NULL;
|
---|
287 |
|
---|
288 | fibril_mutex_lock(&client_list_lock);
|
---|
289 | list_remove(&client->client_list);
|
---|
290 | fibril_mutex_unlock(&client_list_lock);
|
---|
291 | }
|
---|
292 |
|
---|
293 | static void inet_default_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
294 | {
|
---|
295 | inet_client_t client;
|
---|
296 |
|
---|
297 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_default_conn()");
|
---|
298 |
|
---|
299 | /* Accept the connection */
|
---|
300 | async_answer_0(iid, EOK);
|
---|
301 |
|
---|
302 | inet_client_init(&client);
|
---|
303 |
|
---|
304 | while (true) {
|
---|
305 | ipc_call_t call;
|
---|
306 | ipc_callid_t callid = async_get_call(&call);
|
---|
307 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
308 |
|
---|
309 | if (!method) {
|
---|
310 | /* The other side has hung up */
|
---|
311 | async_answer_0(callid, EOK);
|
---|
312 | return;
|
---|
313 | }
|
---|
314 |
|
---|
315 | switch (method) {
|
---|
316 | case INET_CALLBACK_CREATE:
|
---|
317 | inet_callback_create_srv(&client, callid, &call);
|
---|
318 | break;
|
---|
319 | case INET_GET_SRCADDR:
|
---|
320 | inet_get_srcaddr_srv(&client, callid, &call);
|
---|
321 | break;
|
---|
322 | case INET_SEND:
|
---|
323 | inet_send_srv(&client, callid, &call);
|
---|
324 | break;
|
---|
325 | case INET_SET_PROTO:
|
---|
326 | inet_set_proto_srv(&client, callid, &call);
|
---|
327 | break;
|
---|
328 | default:
|
---|
329 | async_answer_0(callid, EINVAL);
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | inet_client_fini(&client);
|
---|
334 | }
|
---|
335 |
|
---|
336 | static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
337 | {
|
---|
338 | sysarg_t port;
|
---|
339 |
|
---|
340 | port = IPC_GET_ARG1(*icall);
|
---|
341 |
|
---|
342 | switch (port) {
|
---|
343 | case INET_PORT_DEFAULT:
|
---|
344 | inet_default_conn(iid, icall, arg);
|
---|
345 | break;
|
---|
346 | case INET_PORT_CFG:
|
---|
347 | inet_cfg_conn(iid, icall, arg);
|
---|
348 | break;
|
---|
349 | case INET_PORT_PING:
|
---|
350 | inetping_conn(iid, icall, arg);
|
---|
351 | break;
|
---|
352 | default:
|
---|
353 | async_answer_0(iid, ENOTSUP);
|
---|
354 | break;
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | static inet_client_t *inet_client_find(uint8_t proto)
|
---|
359 | {
|
---|
360 | fibril_mutex_lock(&client_list_lock);
|
---|
361 |
|
---|
362 | list_foreach(client_list, link) {
|
---|
363 | inet_client_t *client = list_get_instance(link, inet_client_t,
|
---|
364 | client_list);
|
---|
365 |
|
---|
366 | if (client->protocol == proto) {
|
---|
367 | fibril_mutex_unlock(&client_list_lock);
|
---|
368 | return client;
|
---|
369 | }
|
---|
370 | }
|
---|
371 |
|
---|
372 | fibril_mutex_unlock(&client_list_lock);
|
---|
373 | return NULL;
|
---|
374 | }
|
---|
375 |
|
---|
376 | int inet_ev_recv(inet_client_t *client, inet_dgram_t *dgram)
|
---|
377 | {
|
---|
378 | async_exch_t *exch = async_exchange_begin(client->sess);
|
---|
379 |
|
---|
380 | ipc_call_t answer;
|
---|
381 | aid_t req = async_send_1(exch, INET_EV_RECV, dgram->tos, &answer);
|
---|
382 |
|
---|
383 | int rc = async_data_write_start(exch, &dgram->src, sizeof(inet_addr_t));
|
---|
384 | if (rc != EOK) {
|
---|
385 | async_exchange_end(exch);
|
---|
386 | async_forget(req);
|
---|
387 | return rc;
|
---|
388 | }
|
---|
389 |
|
---|
390 | rc = async_data_write_start(exch, &dgram->dest, sizeof(inet_addr_t));
|
---|
391 | if (rc != EOK) {
|
---|
392 | async_exchange_end(exch);
|
---|
393 | async_forget(req);
|
---|
394 | return rc;
|
---|
395 | }
|
---|
396 |
|
---|
397 | rc = async_data_write_start(exch, dgram->data, dgram->size);
|
---|
398 |
|
---|
399 | async_exchange_end(exch);
|
---|
400 |
|
---|
401 | if (rc != EOK) {
|
---|
402 | async_forget(req);
|
---|
403 | return rc;
|
---|
404 | }
|
---|
405 |
|
---|
406 | sysarg_t retval;
|
---|
407 | async_wait_for(req, &retval);
|
---|
408 |
|
---|
409 | return (int) retval;
|
---|
410 | }
|
---|
411 |
|
---|
412 | int inet_recv_dgram_local(inet_dgram_t *dgram, uint8_t proto)
|
---|
413 | {
|
---|
414 | inet_client_t *client;
|
---|
415 |
|
---|
416 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_recv_dgram_local()");
|
---|
417 |
|
---|
418 | /* ICMP messages are handled internally */
|
---|
419 | if (proto == IP_PROTO_ICMP)
|
---|
420 | return icmp_recv(dgram);
|
---|
421 |
|
---|
422 | client = inet_client_find(proto);
|
---|
423 | if (client == NULL) {
|
---|
424 | log_msg(LOG_DEFAULT, LVL_DEBUG, "No client found for protocol 0x%" PRIx8,
|
---|
425 | proto);
|
---|
426 | return ENOENT;
|
---|
427 | }
|
---|
428 |
|
---|
429 | return inet_ev_recv(client, dgram);
|
---|
430 | }
|
---|
431 |
|
---|
432 | int inet_recv_packet(inet_packet_t *packet)
|
---|
433 | {
|
---|
434 | inet_addrobj_t *addr;
|
---|
435 | inet_dgram_t dgram;
|
---|
436 |
|
---|
437 | addr = inet_addrobj_find(&packet->dest, iaf_addr);
|
---|
438 | if (addr != NULL) {
|
---|
439 | /* Destined for one of the local addresses */
|
---|
440 |
|
---|
441 | /* Check if packet is a complete datagram */
|
---|
442 | if (packet->offs == 0 && !packet->mf) {
|
---|
443 | /* It is complete deliver it immediately */
|
---|
444 | dgram.src = packet->src;
|
---|
445 | dgram.dest = packet->dest;
|
---|
446 | dgram.tos = packet->tos;
|
---|
447 | dgram.data = packet->data;
|
---|
448 | dgram.size = packet->size;
|
---|
449 |
|
---|
450 | return inet_recv_dgram_local(&dgram, packet->proto);
|
---|
451 | } else {
|
---|
452 | /* It is a fragment, queue it for reassembly */
|
---|
453 | inet_reass_queue_packet(packet);
|
---|
454 | }
|
---|
455 | }
|
---|
456 |
|
---|
457 | return ENOENT;
|
---|
458 | }
|
---|
459 |
|
---|
460 | int main(int argc, char *argv[])
|
---|
461 | {
|
---|
462 | int rc;
|
---|
463 |
|
---|
464 | printf(NAME ": HelenOS Internet Protocol service\n");
|
---|
465 |
|
---|
466 | if (log_init(NAME) != EOK) {
|
---|
467 | printf(NAME ": Failed to initialize logging.\n");
|
---|
468 | return 1;
|
---|
469 | }
|
---|
470 |
|
---|
471 | rc = inet_init();
|
---|
472 | if (rc != EOK)
|
---|
473 | return 1;
|
---|
474 |
|
---|
475 | printf(NAME ": Accepting connections.\n");
|
---|
476 | task_retval(0);
|
---|
477 | async_manager();
|
---|
478 |
|
---|
479 | /* Not reached */
|
---|
480 | return 0;
|
---|
481 | }
|
---|
482 |
|
---|
483 | /** @}
|
---|
484 | */
|
---|