source: mainline/uspace/srv/net/ethip/ethip_nic.c

Last change on this file was e82b37e, checked in by Jiri Svoboda <jiri@…>, 11 months ago

Fix decoding of changed MAC address.

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