Fix decoding of changed MAC address.
[helenos.git] / uspace / srv / net / ethip / ethip_nic.c
blobd6bc9b70aa837673a7dd5d67607ec6120bc2d617
1 /*
2 * Copyright (c) 2024 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup ethip
30 * @{
32 /**
33 * @file
34 * @brief
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"
54 static errno_t ethip_nic_open(service_id_t sid);
55 static void ethip_nic_cb_conn(ipc_call_t *icall, void *arg);
57 static LIST_INITIALIZE(ethip_nic_list);
58 static FIBRIL_MUTEX_INITIALIZE(ethip_discovery_lock);
60 static errno_t ethip_nic_check_new(void)
62 bool already_known;
63 category_id_t iplink_cat;
64 service_id_t *svcs;
65 size_t count, i;
66 errno_t rc;
68 fibril_mutex_lock(&ethip_discovery_lock);
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(&ethip_discovery_lock);
74 return ENOENT;
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(&ethip_discovery_lock);
81 return EIO;
84 for (i = 0; i < count; i++) {
85 already_known = false;
87 list_foreach(ethip_nic_list, link, ethip_nic_t, nic) {
88 if (nic->svc_id == svcs[i]) {
89 already_known = true;
90 break;
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.");
103 fibril_mutex_unlock(&ethip_discovery_lock);
104 return EOK;
107 static ethip_nic_t *ethip_nic_new(void)
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;
116 link_initialize(&nic->link);
117 list_initialize(&nic->addr_list);
119 return nic;
122 static ethip_link_addr_t *ethip_nic_addr_new(inet_addr_t *addr)
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;
131 link_initialize(&laddr->link);
132 laddr->addr = *addr;
134 return laddr;
137 static void ethip_nic_delete(ethip_nic_t *nic)
139 if (nic->svc_name != NULL)
140 free(nic->svc_name);
142 free(nic);
145 static void ethip_link_addr_delete(ethip_link_addr_t *laddr)
147 free(laddr);
150 static errno_t ethip_nic_open(service_id_t sid)
152 bool in_list = false;
153 nic_address_t nic_address;
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;
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;
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;
172 nic->svc_id = sid;
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;
181 log_msg(LOG_DEFAULT, LVL_DEBUG, "Opened NIC '%s'", nic->svc_name);
182 list_append(&nic->link, &ethip_nic_list);
183 in_list = true;
185 rc = ethip_iplink_init(nic);
186 if (rc != EOK)
187 goto error;
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;
196 eth_addr_decode(nic_address.address, &nic->mac_addr);
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;
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;
212 log_msg(LOG_DEFAULT, LVL_DEBUG, "Initialized IP link service,");
214 return EOK;
216 error:
217 if (in_list)
218 list_remove(&nic->link);
220 if (nic->sess != NULL)
221 async_hangup(nic->sess);
223 ethip_nic_delete(nic);
224 return rc;
227 static void ethip_nic_cat_change_cb(void *arg)
229 (void) ethip_nic_check_new();
232 static void ethip_nic_addr_changed(ethip_nic_t *nic, ipc_call_t *call)
234 uint8_t *addr;
235 size_t size;
236 eth_addr_str_t saddr;
237 errno_t rc;
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;
245 eth_addr_decode(addr, &nic->mac_addr);
246 eth_addr_format(&nic->mac_addr, &saddr);
248 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_changed(): "
249 "new addr=%s", saddr.str);
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;
257 free(addr);
258 async_answer_0(call, EOK);
261 static void ethip_nic_received(ethip_nic_t *nic, ipc_call_t *call)
263 errno_t rc;
264 void *data;
265 size_t size;
267 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_received() nic=%p", nic);
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;
275 log_msg(LOG_DEFAULT, LVL_DEBUG, "Ethernet PDU contents (%zu bytes)",
276 size);
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);
283 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_received() done, rc=%s", str_error_name(rc));
284 async_answer_0(call, rc);
287 static void ethip_nic_device_state(ethip_nic_t *nic, ipc_call_t *call)
289 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_device_state()");
290 async_answer_0(call, ENOTSUP);
293 static void ethip_nic_cb_conn(ipc_call_t *icall, void *arg)
295 ethip_nic_t *nic = (ethip_nic_t *)arg;
297 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethnip_nic_cb_conn()");
299 while (true) {
300 ipc_call_t call;
301 async_get_call(&call);
303 if (!ipc_get_imethod(&call)) {
304 async_answer_0(&call, EOK);
305 return;
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);
325 errno_t ethip_nic_discovery_start(void)
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;
334 return ethip_nic_check_new();
337 ethip_nic_t *ethip_nic_find_by_iplink_sid(service_id_t iplink_sid)
339 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid(%u)",
340 (unsigned) iplink_sid);
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;
350 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - not found");
351 return NULL;
354 errno_t ethip_nic_send(ethip_nic_t *nic, void *data, size_t size)
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;
363 /** Setup accepted multicast addresses
365 * Currently the set of accepted multicast addresses is
366 * determined only based on IPv6 addresses.
369 static errno_t ethip_nic_setup_multicast(ethip_nic_t *nic)
371 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_setup_multicast()");
373 /* Count the number of multicast addresses */
375 size_t count = 0;
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++;
383 if (count == 0)
384 return nic_multicast_set_mode(nic->sess, NIC_MULTICAST_BLOCKED,
385 NULL, 0);
387 nic_address_t *mac_list = calloc(count, sizeof(nic_address_t));
388 if (mac_list == NULL)
389 return ENOMEM;
391 /* Create the multicast MAC list */
393 size_t i = 0;
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;
401 assert(i < count);
403 eth_addr_t mac;
404 eth_addr_solicited_node(v6, &mac);
406 /* Avoid duplicate addresses in the list */
408 bool found = false;
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;
419 if (!found) {
420 eth_addr_encode(&mac, mac_list[i].address);
421 i++;
422 } else {
423 count--;
427 /* Setup the multicast MAC list */
429 errno_t rc = nic_multicast_set_mode(nic->sess, NIC_MULTICAST_LIST,
430 mac_list, count);
432 free(mac_list);
433 return rc;
436 errno_t ethip_nic_addr_add(ethip_nic_t *nic, inet_addr_t *addr)
438 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_add()");
440 ethip_link_addr_t *laddr = ethip_nic_addr_new(addr);
441 if (laddr == NULL)
442 return ENOMEM;
444 list_append(&laddr->link, &nic->addr_list);
446 return ethip_nic_setup_multicast(nic);
449 errno_t ethip_nic_addr_remove(ethip_nic_t *nic, inet_addr_t *addr)
451 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_remove()");
453 ethip_link_addr_t *laddr = ethip_nic_addr_find(nic, addr);
454 if (laddr == NULL)
455 return ENOENT;
457 list_remove(&laddr->link);
458 ethip_link_addr_delete(laddr);
460 return ethip_nic_setup_multicast(nic);
463 ethip_link_addr_t *ethip_nic_addr_find(ethip_nic_t *nic,
464 inet_addr_t *addr)
466 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_find()");
468 list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
469 if (inet_addr_compare(addr, &laddr->addr))
470 return laddr;
473 return NULL;
476 /** @}