source: mainline/uspace/srv/inet/inet.c@ 637a3b4

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

ICMP echo replying.

  • Property mode set to 100644
File size: 9.0 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
49#include "addrobj.h"
50#include "icmp.h"
51#include "icmp_std.h"
52#include "inet.h"
53#include "inetcfg.h"
54#include "inet_link.h"
55
56#define NAME "inet"
57
58static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
59
60static FIBRIL_MUTEX_INITIALIZE(client_list_lock);
61static LIST_INITIALIZE(client_list);
62
63static int inet_init(void)
64{
65 service_id_t sid;
66 int rc;
67
68 log_msg(LVL_DEBUG, "inet_init()");
69
70 async_set_client_connection(inet_client_conn);
71
72 rc = loc_server_register(NAME);
73 if (rc != EOK) {
74 log_msg(LVL_ERROR, "Failed registering server (%d).", rc);
75 return EEXIST;
76 }
77
78 rc = loc_service_register_with_iface(SERVICE_NAME_INET, &sid,
79 INET_PORT_DEFAULT);
80 if (rc != EOK) {
81 log_msg(LVL_ERROR, "Failed registering service (%d).", rc);
82 return EEXIST;
83 }
84
85 rc = loc_service_register_with_iface(SERVICE_NAME_INETCFG, &sid,
86 INET_PORT_CFG);
87 if (rc != EOK) {
88 log_msg(LVL_ERROR, "Failed registering service (%d).", rc);
89 return EEXIST;
90 }
91
92 rc = inet_link_discovery_start();
93 if (rc != EOK)
94 return EEXIST;
95
96 return EOK;
97}
98
99static void inet_callback_create_srv(inet_client_t *client, ipc_callid_t callid,
100 ipc_call_t *call)
101{
102 log_msg(LVL_DEBUG, "inet_callback_create_srv()");
103
104 async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
105 if (sess == NULL) {
106 async_answer_0(callid, ENOMEM);
107 return;
108 }
109
110 client->sess = sess;
111 async_answer_0(callid, EOK);
112}
113
114int inet_route_packet(inet_dgram_t *dgram, uint8_t proto, uint8_t ttl,
115 int df)
116{
117 inet_addrobj_t *addr;
118
119 addr = inet_addrobj_find(&dgram->dest, iaf_net);
120 if (addr != NULL) {
121 /* Destination is directly accessible */
122 return inet_addrobj_send_dgram(addr, dgram, proto, ttl, df);
123 }
124
125 /* TODO: Gateways */
126 log_msg(LVL_DEBUG, "inet_send: No route to destination.");
127 return ENOENT;
128}
129
130static int inet_send(inet_client_t *client, inet_dgram_t *dgram,
131 uint8_t proto, uint8_t ttl, int df)
132{
133 return inet_route_packet(dgram, proto, ttl, df);
134}
135
136static int inet_get_srcaddr(inet_client_t *client, inet_addr_t *remote,
137 uint8_t tos, inet_addr_t *local)
138{
139 inet_addrobj_t *addr;
140
141 addr = inet_addrobj_find(remote, iaf_net);
142 if (addr != NULL) {
143 /* Destination is directly accessible */
144 local->ipv4 = addr->naddr.ipv4;
145 return EOK;
146 }
147
148 return ENOENT;
149}
150
151static void inet_get_srcaddr_srv(inet_client_t *client, ipc_callid_t callid,
152 ipc_call_t *call)
153{
154 inet_addr_t remote;
155 uint8_t tos;
156 inet_addr_t local;
157 int rc;
158
159 log_msg(LVL_DEBUG, "inet_get_srcaddr_srv()");
160
161 remote.ipv4 = IPC_GET_ARG1(*call);
162 tos = IPC_GET_ARG2(*call);
163 local.ipv4 = 0;
164
165 rc = inet_get_srcaddr(client, &remote, tos, &local);
166 async_answer_1(callid, rc, local.ipv4);
167}
168
169static void inet_send_srv(inet_client_t *client, ipc_callid_t callid,
170 ipc_call_t *call)
171{
172 inet_dgram_t dgram;
173 uint8_t ttl;
174 int df;
175 int rc;
176
177 log_msg(LVL_DEBUG, "inet_send_srv()");
178
179 dgram.src.ipv4 = IPC_GET_ARG1(*call);
180 dgram.dest.ipv4 = IPC_GET_ARG2(*call);
181 dgram.tos = IPC_GET_ARG3(*call);
182 ttl = IPC_GET_ARG4(*call);
183 df = IPC_GET_ARG5(*call);
184
185 rc = async_data_write_accept(&dgram.data, false, 0, 0, 0, &dgram.size);
186 if (rc != EOK) {
187 async_answer_0(callid, rc);
188 return;
189 }
190
191 rc = inet_send(client, &dgram, client->protocol, ttl, df);
192
193 free(dgram.data);
194 async_answer_0(callid, rc);
195}
196
197static void inet_set_proto_srv(inet_client_t *client, ipc_callid_t callid,
198 ipc_call_t *call)
199{
200 sysarg_t proto;
201
202 proto = IPC_GET_ARG1(*call);
203 log_msg(LVL_DEBUG, "inet_set_proto_srv(%lu)", (unsigned long) proto);
204
205 if (proto > UINT8_MAX) {
206 async_answer_0(callid, EINVAL);
207 return;
208 }
209
210 client->protocol = proto;
211 async_answer_0(callid, EOK);
212}
213
214static void inet_client_init(inet_client_t *client)
215{
216 client->sess = NULL;
217
218 fibril_mutex_lock(&client_list_lock);
219 list_append(&client->client_list, &client_list);
220 fibril_mutex_unlock(&client_list_lock);
221}
222
223static void inet_client_fini(inet_client_t *client)
224{
225 async_hangup(client->sess);
226 client->sess = NULL;
227
228 fibril_mutex_lock(&client_list_lock);
229 list_remove(&client->client_list);
230 fibril_mutex_unlock(&client_list_lock);
231}
232
233static void inet_default_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
234{
235 inet_client_t client;
236
237 log_msg(LVL_DEBUG, "inet_default_conn()");
238
239 /* Accept the connection */
240 async_answer_0(iid, EOK);
241
242 inet_client_init(&client);
243
244 while (true) {
245 ipc_call_t call;
246 ipc_callid_t callid = async_get_call(&call);
247 sysarg_t method = IPC_GET_IMETHOD(call);
248
249 if (!method) {
250 /* The other side has hung up */
251 async_answer_0(callid, EOK);
252 return;
253 }
254
255 switch (method) {
256 case INET_CALLBACK_CREATE:
257 inet_callback_create_srv(&client, callid, &call);
258 break;
259 case INET_GET_SRCADDR:
260 inet_get_srcaddr_srv(&client, callid, &call);
261 break;
262 case INET_SEND:
263 inet_send_srv(&client, callid, &call);
264 break;
265 case INET_SET_PROTO:
266 inet_set_proto_srv(&client, callid, &call);
267 break;
268 default:
269 async_answer_0(callid, EINVAL);
270 }
271 }
272
273 inet_client_fini(&client);
274}
275
276static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
277{
278 sysarg_t port;
279
280 port = IPC_GET_ARG1(*icall);
281
282 switch (port) {
283 case INET_PORT_DEFAULT:
284 inet_default_conn(iid, icall, arg);
285 break;
286 case INET_PORT_CFG:
287 inet_cfg_conn(iid, icall, arg);
288 break;
289 default:
290 async_answer_0(iid, ENOTSUP);
291 break;
292 }
293}
294
295static inet_client_t *inet_client_find(uint8_t proto)
296{
297 fibril_mutex_lock(&client_list_lock);
298
299 list_foreach(client_list, link) {
300 inet_client_t *client = list_get_instance(link, inet_client_t,
301 client_list);
302
303 if (client->protocol == proto) {
304 fibril_mutex_unlock(&client_list_lock);
305 return client;
306 }
307 }
308
309 fibril_mutex_unlock(&client_list_lock);
310 return NULL;
311}
312
313int inet_ev_recv(inet_client_t *client, inet_dgram_t *dgram)
314{
315 async_exch_t *exch = async_exchange_begin(client->sess);
316
317 ipc_call_t answer;
318 aid_t req = async_send_3(exch, INET_EV_RECV, dgram->src.ipv4,
319 dgram->dest.ipv4, dgram->tos, &answer);
320 int rc = async_data_write_start(exch, dgram->data, dgram->size);
321 async_exchange_end(exch);
322
323 if (rc != EOK) {
324 async_wait_for(req, NULL);
325 return rc;
326 }
327
328 sysarg_t retval;
329 async_wait_for(req, &retval);
330 if (retval != EOK)
331 return retval;
332
333 return EOK;
334}
335
336static int inet_recv_dgram_local(inet_dgram_t *dgram, uint8_t proto)
337{
338 inet_client_t *client;
339
340 log_msg(LVL_DEBUG, "inet_recv_dgram_local()");
341
342 /* ICMP messages are handled internally */
343 if (proto == IP_PROTO_ICMP)
344 return icmp_recv(dgram);
345
346 client = inet_client_find(proto);
347 if (client == NULL) {
348 log_msg(LVL_DEBUG, "No client found for protocol 0x%" PRIx8,
349 proto);
350 return ENOENT;
351 }
352
353 return inet_ev_recv(client, dgram);
354}
355
356int inet_recv_packet(inet_packet_t *packet)
357{
358 inet_addrobj_t *addr;
359 inet_dgram_t dgram;
360
361 addr = inet_addrobj_find(&packet->dest, iaf_addr);
362 if (addr != NULL) {
363 /* Destined for one of the local addresses */
364
365 /* XXX Reassemble packets */
366 dgram.src = packet->src;
367 dgram.dest = packet->dest;
368 dgram.tos = packet->tos;
369 dgram.data = packet->data;
370 dgram.size = packet->size;
371
372 return inet_recv_dgram_local(&dgram, packet->proto);
373 }
374
375 return ENOENT;
376}
377
378int main(int argc, char *argv[])
379{
380 int rc;
381
382 printf(NAME ": HelenOS Internet Protocol service\n");
383
384 if (log_init(NAME, LVL_DEBUG) != EOK) {
385 printf(NAME ": Failed to initialize logging.\n");
386 return 1;
387 }
388
389 rc = inet_init();
390 if (rc != EOK)
391 return 1;
392
393 printf(NAME ": Accepting connections.\n");
394 task_retval(0);
395 async_manager();
396
397 /* Not reached */
398 return 0;
399}
400
401/** @}
402 */
Note: See TracBrowser for help on using the repository browser.