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