1 | /*
|
---|
2 | * Copyright (c) 2024 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 ethip
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <adt/list.h>
|
---|
38 | #include <async.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <fibril_synch.h>
|
---|
41 | #include <inet/eth_addr.h>
|
---|
42 | #include <inet/iplink_srv.h>
|
---|
43 | #include <io/log.h>
|
---|
44 | #include <loc.h>
|
---|
45 | #include <mem.h>
|
---|
46 | #include <nic_iface.h>
|
---|
47 | #include <stdbool.h>
|
---|
48 | #include <stdlib.h>
|
---|
49 | #include <str_error.h>
|
---|
50 | #include "ethip.h"
|
---|
51 | #include "ethip_nic.h"
|
---|
52 | #include "pdu.h"
|
---|
53 |
|
---|
54 | static errno_t ethip_nic_open(service_id_t sid);
|
---|
55 | static void ethip_nic_cb_conn(ipc_call_t *icall, void *arg);
|
---|
56 |
|
---|
57 | static LIST_INITIALIZE(ethip_nic_list);
|
---|
58 | static FIBRIL_MUTEX_INITIALIZE(ethip_discovery_lock);
|
---|
59 |
|
---|
60 | static errno_t ethip_nic_check_new(void)
|
---|
61 | {
|
---|
62 | bool already_known;
|
---|
63 | category_id_t iplink_cat;
|
---|
64 | service_id_t *svcs;
|
---|
65 | size_t count, i;
|
---|
66 | errno_t rc;
|
---|
67 |
|
---|
68 | fibril_mutex_lock(ðip_discovery_lock);
|
---|
69 |
|
---|
70 | rc = loc_category_get_id("nic", &iplink_cat, IPC_FLAG_BLOCKING);
|
---|
71 | if (rc != EOK) {
|
---|
72 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'nic'.");
|
---|
73 | fibril_mutex_unlock(ðip_discovery_lock);
|
---|
74 | return ENOENT;
|
---|
75 | }
|
---|
76 |
|
---|
77 | rc = loc_category_get_svcs(iplink_cat, &svcs, &count);
|
---|
78 | if (rc != EOK) {
|
---|
79 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting list of IP links.");
|
---|
80 | fibril_mutex_unlock(ðip_discovery_lock);
|
---|
81 | return EIO;
|
---|
82 | }
|
---|
83 |
|
---|
84 | for (i = 0; i < count; i++) {
|
---|
85 | already_known = false;
|
---|
86 |
|
---|
87 | list_foreach(ethip_nic_list, link, ethip_nic_t, nic) {
|
---|
88 | if (nic->svc_id == svcs[i]) {
|
---|
89 | already_known = true;
|
---|
90 | break;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (!already_known) {
|
---|
95 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Found NIC '%lu'",
|
---|
96 | (unsigned long) svcs[i]);
|
---|
97 | rc = ethip_nic_open(svcs[i]);
|
---|
98 | if (rc != EOK)
|
---|
99 | log_msg(LOG_DEFAULT, LVL_ERROR, "Could not open NIC.");
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | fibril_mutex_unlock(ðip_discovery_lock);
|
---|
104 | return EOK;
|
---|
105 | }
|
---|
106 |
|
---|
107 | static ethip_nic_t *ethip_nic_new(void)
|
---|
108 | {
|
---|
109 | ethip_nic_t *nic = calloc(1, sizeof(ethip_nic_t));
|
---|
110 | if (nic == NULL) {
|
---|
111 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating NIC structure. "
|
---|
112 | "Out of memory.");
|
---|
113 | return NULL;
|
---|
114 | }
|
---|
115 |
|
---|
116 | link_initialize(&nic->link);
|
---|
117 | list_initialize(&nic->addr_list);
|
---|
118 |
|
---|
119 | return nic;
|
---|
120 | }
|
---|
121 |
|
---|
122 | static ethip_link_addr_t *ethip_nic_addr_new(inet_addr_t *addr)
|
---|
123 | {
|
---|
124 | ethip_link_addr_t *laddr = calloc(1, sizeof(ethip_link_addr_t));
|
---|
125 | if (laddr == NULL) {
|
---|
126 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating NIC address structure. "
|
---|
127 | "Out of memory.");
|
---|
128 | return NULL;
|
---|
129 | }
|
---|
130 |
|
---|
131 | link_initialize(&laddr->link);
|
---|
132 | laddr->addr = *addr;
|
---|
133 |
|
---|
134 | return laddr;
|
---|
135 | }
|
---|
136 |
|
---|
137 | static void ethip_nic_delete(ethip_nic_t *nic)
|
---|
138 | {
|
---|
139 | if (nic->svc_name != NULL)
|
---|
140 | free(nic->svc_name);
|
---|
141 |
|
---|
142 | free(nic);
|
---|
143 | }
|
---|
144 |
|
---|
145 | static void ethip_link_addr_delete(ethip_link_addr_t *laddr)
|
---|
146 | {
|
---|
147 | free(laddr);
|
---|
148 | }
|
---|
149 |
|
---|
150 | static errno_t ethip_nic_open(service_id_t sid)
|
---|
151 | {
|
---|
152 | bool in_list = false;
|
---|
153 | nic_address_t nic_address;
|
---|
154 |
|
---|
155 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_open()");
|
---|
156 | ethip_nic_t *nic = ethip_nic_new();
|
---|
157 | if (nic == NULL)
|
---|
158 | return ENOMEM;
|
---|
159 |
|
---|
160 | errno_t rc = loc_service_get_name(sid, &nic->svc_name);
|
---|
161 | if (rc != EOK) {
|
---|
162 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
|
---|
163 | goto error;
|
---|
164 | }
|
---|
165 |
|
---|
166 | nic->sess = loc_service_connect(sid, INTERFACE_DDF, 0);
|
---|
167 | if (nic->sess == NULL) {
|
---|
168 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting '%s'", nic->svc_name);
|
---|
169 | goto error;
|
---|
170 | }
|
---|
171 |
|
---|
172 | nic->svc_id = sid;
|
---|
173 |
|
---|
174 | rc = nic_callback_create(nic->sess, ethip_nic_cb_conn, nic);
|
---|
175 | if (rc != EOK) {
|
---|
176 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed creating callback connection "
|
---|
177 | "from '%s'", nic->svc_name);
|
---|
178 | goto error;
|
---|
179 | }
|
---|
180 |
|
---|
181 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Opened NIC '%s'", nic->svc_name);
|
---|
182 | list_append(&nic->link, ðip_nic_list);
|
---|
183 | in_list = true;
|
---|
184 |
|
---|
185 | rc = ethip_iplink_init(nic);
|
---|
186 | if (rc != EOK)
|
---|
187 | goto error;
|
---|
188 |
|
---|
189 | rc = nic_get_address(nic->sess, &nic_address);
|
---|
190 | if (rc != EOK) {
|
---|
191 | log_msg(LOG_DEFAULT, LVL_ERROR, "Error getting MAC address of NIC '%s'.",
|
---|
192 | nic->svc_name);
|
---|
193 | goto error;
|
---|
194 | }
|
---|
195 |
|
---|
196 | eth_addr_decode(nic_address.address, &nic->mac_addr);
|
---|
197 |
|
---|
198 | rc = nic_set_state(nic->sess, NIC_STATE_ACTIVE);
|
---|
199 | if (rc != EOK) {
|
---|
200 | log_msg(LOG_DEFAULT, LVL_ERROR, "Error activating NIC '%s'.",
|
---|
201 | nic->svc_name);
|
---|
202 | goto error;
|
---|
203 | }
|
---|
204 |
|
---|
205 | rc = nic_broadcast_set_mode(nic->sess, NIC_BROADCAST_ACCEPTED);
|
---|
206 | if (rc != EOK) {
|
---|
207 | log_msg(LOG_DEFAULT, LVL_ERROR, "Error enabling "
|
---|
208 | "reception of broadcast frames on '%s'.", nic->svc_name);
|
---|
209 | goto error;
|
---|
210 | }
|
---|
211 |
|
---|
212 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Initialized IP link service,");
|
---|
213 |
|
---|
214 | return EOK;
|
---|
215 |
|
---|
216 | error:
|
---|
217 | if (in_list)
|
---|
218 | list_remove(&nic->link);
|
---|
219 |
|
---|
220 | if (nic->sess != NULL)
|
---|
221 | async_hangup(nic->sess);
|
---|
222 |
|
---|
223 | ethip_nic_delete(nic);
|
---|
224 | return rc;
|
---|
225 | }
|
---|
226 |
|
---|
227 | static void ethip_nic_cat_change_cb(void *arg)
|
---|
228 | {
|
---|
229 | (void) ethip_nic_check_new();
|
---|
230 | }
|
---|
231 |
|
---|
232 | static void ethip_nic_addr_changed(ethip_nic_t *nic, ipc_call_t *call)
|
---|
233 | {
|
---|
234 | uint8_t *addr;
|
---|
235 | size_t size;
|
---|
236 | eth_addr_str_t saddr;
|
---|
237 | errno_t rc;
|
---|
238 |
|
---|
239 | rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &size);
|
---|
240 | if (rc != EOK) {
|
---|
241 | log_msg(LOG_DEFAULT, LVL_DEBUG, "data_write_accept() failed");
|
---|
242 | return;
|
---|
243 | }
|
---|
244 |
|
---|
245 | eth_addr_decode(addr, &nic->mac_addr);
|
---|
246 | eth_addr_format(&nic->mac_addr, &saddr);
|
---|
247 |
|
---|
248 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_changed(): "
|
---|
249 | "new addr=%s", saddr.str);
|
---|
250 |
|
---|
251 | rc = iplink_ev_change_addr(&nic->iplink, &nic->mac_addr);
|
---|
252 | if (rc != EOK) {
|
---|
253 | log_msg(LOG_DEFAULT, LVL_DEBUG, "iplink_ev_change_addr() failed");
|
---|
254 | return;
|
---|
255 | }
|
---|
256 |
|
---|
257 | free(addr);
|
---|
258 | async_answer_0(call, EOK);
|
---|
259 | }
|
---|
260 |
|
---|
261 | static void ethip_nic_received(ethip_nic_t *nic, ipc_call_t *call)
|
---|
262 | {
|
---|
263 | errno_t rc;
|
---|
264 | void *data;
|
---|
265 | size_t size;
|
---|
266 |
|
---|
267 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_received() nic=%p", nic);
|
---|
268 |
|
---|
269 | rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
|
---|
270 | if (rc != EOK) {
|
---|
271 | log_msg(LOG_DEFAULT, LVL_DEBUG, "data_write_accept() failed");
|
---|
272 | return;
|
---|
273 | }
|
---|
274 |
|
---|
275 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Ethernet PDU contents (%zu bytes)",
|
---|
276 | size);
|
---|
277 |
|
---|
278 | log_msg(LOG_DEFAULT, LVL_DEBUG, "call ethip_received");
|
---|
279 | rc = ethip_received(&nic->iplink, data, size);
|
---|
280 | log_msg(LOG_DEFAULT, LVL_DEBUG, "free data");
|
---|
281 | free(data);
|
---|
282 |
|
---|
283 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_received() done, rc=%s", str_error_name(rc));
|
---|
284 | async_answer_0(call, rc);
|
---|
285 | }
|
---|
286 |
|
---|
287 | static void ethip_nic_device_state(ethip_nic_t *nic, ipc_call_t *call)
|
---|
288 | {
|
---|
289 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_device_state()");
|
---|
290 | async_answer_0(call, ENOTSUP);
|
---|
291 | }
|
---|
292 |
|
---|
293 | static void ethip_nic_cb_conn(ipc_call_t *icall, void *arg)
|
---|
294 | {
|
---|
295 | ethip_nic_t *nic = (ethip_nic_t *)arg;
|
---|
296 |
|
---|
297 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethnip_nic_cb_conn()");
|
---|
298 |
|
---|
299 | while (true) {
|
---|
300 | ipc_call_t call;
|
---|
301 | async_get_call(&call);
|
---|
302 |
|
---|
303 | if (!ipc_get_imethod(&call)) {
|
---|
304 | async_answer_0(&call, EOK);
|
---|
305 | return;
|
---|
306 | }
|
---|
307 |
|
---|
308 | switch (ipc_get_imethod(&call)) {
|
---|
309 | case NIC_EV_ADDR_CHANGED:
|
---|
310 | ethip_nic_addr_changed(nic, &call);
|
---|
311 | break;
|
---|
312 | case NIC_EV_RECEIVED:
|
---|
313 | ethip_nic_received(nic, &call);
|
---|
314 | break;
|
---|
315 | case NIC_EV_DEVICE_STATE:
|
---|
316 | ethip_nic_device_state(nic, &call);
|
---|
317 | break;
|
---|
318 | default:
|
---|
319 | log_msg(LOG_DEFAULT, LVL_DEBUG, "unknown IPC method: %" PRIun, ipc_get_imethod(&call));
|
---|
320 | async_answer_0(&call, ENOTSUP);
|
---|
321 | }
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | errno_t ethip_nic_discovery_start(void)
|
---|
326 | {
|
---|
327 | errno_t rc = loc_register_cat_change_cb(ethip_nic_cat_change_cb, NULL);
|
---|
328 | if (rc != EOK) {
|
---|
329 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback for NIC "
|
---|
330 | "discovery: %s.", str_error(rc));
|
---|
331 | return rc;
|
---|
332 | }
|
---|
333 |
|
---|
334 | return ethip_nic_check_new();
|
---|
335 | }
|
---|
336 |
|
---|
337 | ethip_nic_t *ethip_nic_find_by_iplink_sid(service_id_t iplink_sid)
|
---|
338 | {
|
---|
339 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid(%u)",
|
---|
340 | (unsigned) iplink_sid);
|
---|
341 |
|
---|
342 | list_foreach(ethip_nic_list, link, ethip_nic_t, nic) {
|
---|
343 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - element");
|
---|
344 | if (nic->iplink_sid == iplink_sid) {
|
---|
345 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - found %p", nic);
|
---|
346 | return nic;
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - not found");
|
---|
351 | return NULL;
|
---|
352 | }
|
---|
353 |
|
---|
354 | errno_t ethip_nic_send(ethip_nic_t *nic, void *data, size_t size)
|
---|
355 | {
|
---|
356 | errno_t rc;
|
---|
357 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_send(size=%zu)", size);
|
---|
358 | rc = nic_send_frame(nic->sess, data, size);
|
---|
359 | log_msg(LOG_DEFAULT, LVL_DEBUG, "nic_send_frame -> %s", str_error_name(rc));
|
---|
360 | return rc;
|
---|
361 | }
|
---|
362 |
|
---|
363 | /** Setup accepted multicast addresses
|
---|
364 | *
|
---|
365 | * Currently the set of accepted multicast addresses is
|
---|
366 | * determined only based on IPv6 addresses.
|
---|
367 | *
|
---|
368 | */
|
---|
369 | static errno_t ethip_nic_setup_multicast(ethip_nic_t *nic)
|
---|
370 | {
|
---|
371 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_setup_multicast()");
|
---|
372 |
|
---|
373 | /* Count the number of multicast addresses */
|
---|
374 |
|
---|
375 | size_t count = 0;
|
---|
376 |
|
---|
377 | list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
|
---|
378 | ip_ver_t ver = inet_addr_get(&laddr->addr, NULL, NULL);
|
---|
379 | if (ver == ip_v6)
|
---|
380 | count++;
|
---|
381 | }
|
---|
382 |
|
---|
383 | if (count == 0)
|
---|
384 | return nic_multicast_set_mode(nic->sess, NIC_MULTICAST_BLOCKED,
|
---|
385 | NULL, 0);
|
---|
386 |
|
---|
387 | nic_address_t *mac_list = calloc(count, sizeof(nic_address_t));
|
---|
388 | if (mac_list == NULL)
|
---|
389 | return ENOMEM;
|
---|
390 |
|
---|
391 | /* Create the multicast MAC list */
|
---|
392 |
|
---|
393 | size_t i = 0;
|
---|
394 |
|
---|
395 | list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
|
---|
396 | addr128_t v6;
|
---|
397 | ip_ver_t ver = inet_addr_get(&laddr->addr, NULL, &v6);
|
---|
398 | if (ver != ip_v6)
|
---|
399 | continue;
|
---|
400 |
|
---|
401 | assert(i < count);
|
---|
402 |
|
---|
403 | eth_addr_t mac;
|
---|
404 | eth_addr_solicited_node(v6, &mac);
|
---|
405 |
|
---|
406 | /* Avoid duplicate addresses in the list */
|
---|
407 |
|
---|
408 | bool found = false;
|
---|
409 |
|
---|
410 | for (size_t j = 0; j < i; j++) {
|
---|
411 | eth_addr_t mac_entry;
|
---|
412 | eth_addr_decode(mac_list[j].address, &mac_entry);
|
---|
413 | if (eth_addr_compare(&mac_entry, &mac)) {
|
---|
414 | found = true;
|
---|
415 | break;
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | if (!found) {
|
---|
420 | eth_addr_encode(&mac, mac_list[i].address);
|
---|
421 | i++;
|
---|
422 | } else {
|
---|
423 | count--;
|
---|
424 | }
|
---|
425 | }
|
---|
426 |
|
---|
427 | /* Setup the multicast MAC list */
|
---|
428 |
|
---|
429 | errno_t rc = nic_multicast_set_mode(nic->sess, NIC_MULTICAST_LIST,
|
---|
430 | mac_list, count);
|
---|
431 |
|
---|
432 | free(mac_list);
|
---|
433 | return rc;
|
---|
434 | }
|
---|
435 |
|
---|
436 | errno_t ethip_nic_addr_add(ethip_nic_t *nic, inet_addr_t *addr)
|
---|
437 | {
|
---|
438 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_add()");
|
---|
439 |
|
---|
440 | ethip_link_addr_t *laddr = ethip_nic_addr_new(addr);
|
---|
441 | if (laddr == NULL)
|
---|
442 | return ENOMEM;
|
---|
443 |
|
---|
444 | list_append(&laddr->link, &nic->addr_list);
|
---|
445 |
|
---|
446 | return ethip_nic_setup_multicast(nic);
|
---|
447 | }
|
---|
448 |
|
---|
449 | errno_t ethip_nic_addr_remove(ethip_nic_t *nic, inet_addr_t *addr)
|
---|
450 | {
|
---|
451 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_remove()");
|
---|
452 |
|
---|
453 | ethip_link_addr_t *laddr = ethip_nic_addr_find(nic, addr);
|
---|
454 | if (laddr == NULL)
|
---|
455 | return ENOENT;
|
---|
456 |
|
---|
457 | list_remove(&laddr->link);
|
---|
458 | ethip_link_addr_delete(laddr);
|
---|
459 |
|
---|
460 | return ethip_nic_setup_multicast(nic);
|
---|
461 | }
|
---|
462 |
|
---|
463 | ethip_link_addr_t *ethip_nic_addr_find(ethip_nic_t *nic,
|
---|
464 | inet_addr_t *addr)
|
---|
465 | {
|
---|
466 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_find()");
|
---|
467 |
|
---|
468 | list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
|
---|
469 | if (inet_addr_compare(addr, &laddr->addr))
|
---|
470 | return laddr;
|
---|
471 | }
|
---|
472 |
|
---|
473 | return NULL;
|
---|
474 | }
|
---|
475 |
|
---|
476 | /** @}
|
---|
477 | */
|
---|