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 libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief IP link server stub
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <ipc/iplink.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <sys/types.h>
|
---|
41 | #include <inet/addr.h>
|
---|
42 | #include <inet/iplink_srv.h>
|
---|
43 |
|
---|
44 | static void iplink_get_mtu_srv(iplink_srv_t *srv, ipc_callid_t callid,
|
---|
45 | ipc_call_t *call)
|
---|
46 | {
|
---|
47 | size_t mtu;
|
---|
48 | int rc = srv->ops->get_mtu(srv, &mtu);
|
---|
49 | async_answer_1(callid, rc, mtu);
|
---|
50 | }
|
---|
51 |
|
---|
52 | static void iplink_get_mac48_srv(iplink_srv_t *srv, ipc_callid_t iid,
|
---|
53 | ipc_call_t *icall)
|
---|
54 | {
|
---|
55 | addr48_t mac;
|
---|
56 | int rc = srv->ops->get_mac48(srv, &mac);
|
---|
57 | if (rc != EOK) {
|
---|
58 | async_answer_0(iid, rc);
|
---|
59 | return;
|
---|
60 | }
|
---|
61 |
|
---|
62 | ipc_callid_t callid;
|
---|
63 | size_t size;
|
---|
64 | if (!async_data_read_receive(&callid, &size)) {
|
---|
65 | async_answer_0(callid, EREFUSED);
|
---|
66 | async_answer_0(iid, EREFUSED);
|
---|
67 | return;
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (size != sizeof(addr48_t)) {
|
---|
71 | async_answer_0(callid, EINVAL);
|
---|
72 | async_answer_0(iid, EINVAL);
|
---|
73 | return;
|
---|
74 | }
|
---|
75 |
|
---|
76 | rc = async_data_read_finalize(callid, &mac, size);
|
---|
77 | if (rc != EOK)
|
---|
78 | async_answer_0(callid, rc);
|
---|
79 |
|
---|
80 | async_answer_0(iid, (sysarg_t) rc);
|
---|
81 | }
|
---|
82 |
|
---|
83 | static void iplink_set_mac48_srv(iplink_srv_t *srv, ipc_callid_t iid,
|
---|
84 | ipc_call_t *icall)
|
---|
85 | {
|
---|
86 | int rc;
|
---|
87 | size_t size;
|
---|
88 | addr48_t mac;
|
---|
89 | ipc_callid_t callid;
|
---|
90 |
|
---|
91 | rc = async_data_write_receive(&callid, &size);
|
---|
92 | if (rc != EOK) {
|
---|
93 | async_answer_0(callid, (sysarg_t) rc);
|
---|
94 | async_answer_0(iid, (sysarg_t) rc);
|
---|
95 | }
|
---|
96 |
|
---|
97 | rc = srv->ops->set_mac48(srv, &mac);
|
---|
98 | if (rc != EOK) {
|
---|
99 | async_answer_0(iid, rc);
|
---|
100 | return;
|
---|
101 | }
|
---|
102 |
|
---|
103 | rc = async_data_read_finalize(callid, &mac, sizeof(addr48_t));
|
---|
104 | if (rc != EOK)
|
---|
105 | async_answer_0(callid, rc);
|
---|
106 |
|
---|
107 | async_answer_0(iid, (sysarg_t) rc);
|
---|
108 | }
|
---|
109 |
|
---|
110 | static void iplink_addr_add_srv(iplink_srv_t *srv, ipc_callid_t iid,
|
---|
111 | ipc_call_t *icall)
|
---|
112 | {
|
---|
113 | ipc_callid_t callid;
|
---|
114 | size_t size;
|
---|
115 | if (!async_data_write_receive(&callid, &size)) {
|
---|
116 | async_answer_0(callid, EREFUSED);
|
---|
117 | async_answer_0(iid, EREFUSED);
|
---|
118 | return;
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (size != sizeof(inet_addr_t)) {
|
---|
122 | async_answer_0(callid, EINVAL);
|
---|
123 | async_answer_0(iid, EINVAL);
|
---|
124 | return;
|
---|
125 | }
|
---|
126 |
|
---|
127 | inet_addr_t addr;
|
---|
128 | int rc = async_data_write_finalize(callid, &addr, size);
|
---|
129 | if (rc != EOK) {
|
---|
130 | async_answer_0(callid, (sysarg_t) rc);
|
---|
131 | async_answer_0(iid, (sysarg_t) rc);
|
---|
132 | }
|
---|
133 |
|
---|
134 | rc = srv->ops->addr_add(srv, &addr);
|
---|
135 | async_answer_0(iid, (sysarg_t) rc);
|
---|
136 | }
|
---|
137 |
|
---|
138 | static void iplink_addr_remove_srv(iplink_srv_t *srv, ipc_callid_t iid,
|
---|
139 | ipc_call_t *icall)
|
---|
140 | {
|
---|
141 | ipc_callid_t callid;
|
---|
142 | size_t size;
|
---|
143 | if (!async_data_write_receive(&callid, &size)) {
|
---|
144 | async_answer_0(callid, EREFUSED);
|
---|
145 | async_answer_0(iid, EREFUSED);
|
---|
146 | return;
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (size != sizeof(inet_addr_t)) {
|
---|
150 | async_answer_0(callid, EINVAL);
|
---|
151 | async_answer_0(iid, EINVAL);
|
---|
152 | return;
|
---|
153 | }
|
---|
154 |
|
---|
155 | inet_addr_t addr;
|
---|
156 | int rc = async_data_write_finalize(callid, &addr, size);
|
---|
157 | if (rc != EOK) {
|
---|
158 | async_answer_0(callid, (sysarg_t) rc);
|
---|
159 | async_answer_0(iid, (sysarg_t) rc);
|
---|
160 | }
|
---|
161 |
|
---|
162 | rc = srv->ops->addr_remove(srv, &addr);
|
---|
163 | async_answer_0(iid, (sysarg_t) rc);
|
---|
164 | }
|
---|
165 |
|
---|
166 | static void iplink_send_srv(iplink_srv_t *srv, ipc_callid_t iid,
|
---|
167 | ipc_call_t *icall)
|
---|
168 | {
|
---|
169 | iplink_sdu_t sdu;
|
---|
170 |
|
---|
171 | sdu.src = IPC_GET_ARG1(*icall);
|
---|
172 | sdu.dest = IPC_GET_ARG2(*icall);
|
---|
173 |
|
---|
174 | int rc = async_data_write_accept(&sdu.data, false, 0, 0, 0,
|
---|
175 | &sdu.size);
|
---|
176 | if (rc != EOK) {
|
---|
177 | async_answer_0(iid, rc);
|
---|
178 | return;
|
---|
179 | }
|
---|
180 |
|
---|
181 | rc = srv->ops->send(srv, &sdu);
|
---|
182 | free(sdu.data);
|
---|
183 | async_answer_0(iid, rc);
|
---|
184 | }
|
---|
185 |
|
---|
186 | static void iplink_send6_srv(iplink_srv_t *srv, ipc_callid_t iid,
|
---|
187 | ipc_call_t *icall)
|
---|
188 | {
|
---|
189 | iplink_sdu6_t sdu;
|
---|
190 |
|
---|
191 | ipc_callid_t callid;
|
---|
192 | size_t size;
|
---|
193 | if (!async_data_write_receive(&callid, &size)) {
|
---|
194 | async_answer_0(callid, EREFUSED);
|
---|
195 | async_answer_0(iid, EREFUSED);
|
---|
196 | return;
|
---|
197 | }
|
---|
198 |
|
---|
199 | if (size != sizeof(addr48_t)) {
|
---|
200 | async_answer_0(callid, EINVAL);
|
---|
201 | async_answer_0(iid, EINVAL);
|
---|
202 | return;
|
---|
203 | }
|
---|
204 |
|
---|
205 | int rc = async_data_write_finalize(callid, &sdu.dest, size);
|
---|
206 | if (rc != EOK) {
|
---|
207 | async_answer_0(callid, (sysarg_t) rc);
|
---|
208 | async_answer_0(iid, (sysarg_t) rc);
|
---|
209 | }
|
---|
210 |
|
---|
211 | rc = async_data_write_accept(&sdu.data, false, 0, 0, 0,
|
---|
212 | &sdu.size);
|
---|
213 | if (rc != EOK) {
|
---|
214 | async_answer_0(iid, rc);
|
---|
215 | return;
|
---|
216 | }
|
---|
217 |
|
---|
218 | rc = srv->ops->send6(srv, &sdu);
|
---|
219 | free(sdu.data);
|
---|
220 | async_answer_0(iid, rc);
|
---|
221 | }
|
---|
222 |
|
---|
223 | void iplink_srv_init(iplink_srv_t *srv)
|
---|
224 | {
|
---|
225 | fibril_mutex_initialize(&srv->lock);
|
---|
226 | srv->connected = false;
|
---|
227 | srv->ops = NULL;
|
---|
228 | srv->arg = NULL;
|
---|
229 | srv->client_sess = NULL;
|
---|
230 | }
|
---|
231 |
|
---|
232 | int iplink_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
233 | {
|
---|
234 | iplink_srv_t *srv = (iplink_srv_t *) arg;
|
---|
235 | int rc;
|
---|
236 |
|
---|
237 | fibril_mutex_lock(&srv->lock);
|
---|
238 | if (srv->connected) {
|
---|
239 | fibril_mutex_unlock(&srv->lock);
|
---|
240 | async_answer_0(iid, EBUSY);
|
---|
241 | return EBUSY;
|
---|
242 | }
|
---|
243 |
|
---|
244 | srv->connected = true;
|
---|
245 | fibril_mutex_unlock(&srv->lock);
|
---|
246 |
|
---|
247 | /* Accept the connection */
|
---|
248 | async_answer_0(iid, EOK);
|
---|
249 |
|
---|
250 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
251 | if (sess == NULL)
|
---|
252 | return ENOMEM;
|
---|
253 |
|
---|
254 | srv->client_sess = sess;
|
---|
255 |
|
---|
256 | rc = srv->ops->open(srv);
|
---|
257 | if (rc != EOK)
|
---|
258 | return rc;
|
---|
259 |
|
---|
260 | while (true) {
|
---|
261 | ipc_call_t call;
|
---|
262 | ipc_callid_t callid = async_get_call(&call);
|
---|
263 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
264 |
|
---|
265 | if (!method) {
|
---|
266 | /* The other side has hung up */
|
---|
267 | fibril_mutex_lock(&srv->lock);
|
---|
268 | srv->connected = false;
|
---|
269 | fibril_mutex_unlock(&srv->lock);
|
---|
270 | async_answer_0(callid, EOK);
|
---|
271 | break;
|
---|
272 | }
|
---|
273 |
|
---|
274 | switch (method) {
|
---|
275 | case IPLINK_GET_MTU:
|
---|
276 | iplink_get_mtu_srv(srv, callid, &call);
|
---|
277 | break;
|
---|
278 | case IPLINK_GET_MAC48:
|
---|
279 | iplink_get_mac48_srv(srv, callid, &call);
|
---|
280 | break;
|
---|
281 | case IPLINK_SET_MAC48:
|
---|
282 | iplink_set_mac48_srv(srv, callid, &call);
|
---|
283 | break;
|
---|
284 | case IPLINK_SEND:
|
---|
285 | iplink_send_srv(srv, callid, &call);
|
---|
286 | break;
|
---|
287 | case IPLINK_SEND6:
|
---|
288 | iplink_send6_srv(srv, callid, &call);
|
---|
289 | break;
|
---|
290 | case IPLINK_ADDR_ADD:
|
---|
291 | iplink_addr_add_srv(srv, callid, &call);
|
---|
292 | break;
|
---|
293 | case IPLINK_ADDR_REMOVE:
|
---|
294 | iplink_addr_remove_srv(srv, callid, &call);
|
---|
295 | break;
|
---|
296 | default:
|
---|
297 | async_answer_0(callid, EINVAL);
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | return srv->ops->close(srv);
|
---|
302 | }
|
---|
303 |
|
---|
304 | /* XXX Version should be part of @a sdu */
|
---|
305 | int iplink_ev_recv(iplink_srv_t *srv, iplink_recv_sdu_t *sdu, ip_ver_t ver)
|
---|
306 | {
|
---|
307 | if (srv->client_sess == NULL)
|
---|
308 | return EIO;
|
---|
309 |
|
---|
310 | async_exch_t *exch = async_exchange_begin(srv->client_sess);
|
---|
311 |
|
---|
312 | ipc_call_t answer;
|
---|
313 | aid_t req = async_send_1(exch, IPLINK_EV_RECV, (sysarg_t)ver,
|
---|
314 | &answer);
|
---|
315 |
|
---|
316 | int rc = async_data_write_start(exch, sdu->data, sdu->size);
|
---|
317 | async_exchange_end(exch);
|
---|
318 |
|
---|
319 | if (rc != EOK) {
|
---|
320 | async_forget(req);
|
---|
321 | return rc;
|
---|
322 | }
|
---|
323 |
|
---|
324 | sysarg_t retval;
|
---|
325 | async_wait_for(req, &retval);
|
---|
326 | if (retval != EOK)
|
---|
327 | return retval;
|
---|
328 |
|
---|
329 | return EOK;
|
---|
330 | }
|
---|
331 |
|
---|
332 | int iplink_ev_change_addr(iplink_srv_t *srv, addr48_t *addr)
|
---|
333 | {
|
---|
334 | if (srv->client_sess == NULL)
|
---|
335 | return EIO;
|
---|
336 |
|
---|
337 | async_exch_t *exch = async_exchange_begin(srv->client_sess);
|
---|
338 |
|
---|
339 | ipc_call_t answer;
|
---|
340 | aid_t req = async_send_0(exch, IPLINK_EV_CHANGE_ADDR, &answer);
|
---|
341 |
|
---|
342 | int rc = async_data_write_start(exch, addr, sizeof(addr48_t));
|
---|
343 | async_exchange_end(exch);
|
---|
344 |
|
---|
345 | if (rc != EOK) {
|
---|
346 | async_forget(req);
|
---|
347 | return rc;
|
---|
348 | }
|
---|
349 |
|
---|
350 | sysarg_t retval;
|
---|
351 | async_wait_for(req, &retval);
|
---|
352 | if (retval != EOK)
|
---|
353 | return retval;
|
---|
354 |
|
---|
355 | return EOK;
|
---|
356 | }
|
---|
357 |
|
---|
358 | /** @}
|
---|
359 | */
|
---|