source: mainline/uspace/srv/net/ethip/ethip_nic.c@ 1543d4c

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1543d4c was b4edc96, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Rename and move addr48_t to eth_addr_t

  • Property mode set to 100644
File size: 11.6 KB
Line 
1/*
2 * Copyright (c) 2021 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
54static errno_t ethip_nic_open(service_id_t sid);
55static void ethip_nic_cb_conn(ipc_call_t *icall, void *arg);
56
57static LIST_INITIALIZE(ethip_nic_list);
58static FIBRIL_MUTEX_INITIALIZE(ethip_discovery_lock);
59
60static 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(&ethip_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(&ethip_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(&ethip_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(&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) {
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
122static 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
137static 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
145static void ethip_link_addr_delete(ethip_link_addr_t *laddr)
146{
147 free(laddr);
148}
149
150static 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, &ethip_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
216error:
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
227static void ethip_nic_cat_change_cb(void *arg)
228{
229 (void) ethip_nic_check_new();
230}
231
232static void ethip_nic_addr_changed(ethip_nic_t *nic, ipc_call_t *call)
233{
234 uint8_t *addr;
235 size_t size;
236 errno_t rc;
237
238 rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &size);
239 if (rc != EOK) {
240 log_msg(LOG_DEFAULT, LVL_DEBUG, "data_write_accept() failed");
241 return;
242 }
243
244 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_changed(): "
245 "new addr=%02x:%02x:%02x:%02x:%02x:%02x",
246 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
247
248 memcpy(&nic->mac_addr, addr, sizeof(nic->mac_addr));
249
250 rc = iplink_ev_change_addr(&nic->iplink, &nic->mac_addr);
251 if (rc != EOK) {
252 log_msg(LOG_DEFAULT, LVL_DEBUG, "iplink_ev_change_addr() failed");
253 return;
254 }
255
256 free(addr);
257 async_answer_0(call, EOK);
258}
259
260static void ethip_nic_received(ethip_nic_t *nic, ipc_call_t *call)
261{
262 errno_t rc;
263 void *data;
264 size_t size;
265
266 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_received() nic=%p", nic);
267
268 rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
269 if (rc != EOK) {
270 log_msg(LOG_DEFAULT, LVL_DEBUG, "data_write_accept() failed");
271 return;
272 }
273
274 log_msg(LOG_DEFAULT, LVL_DEBUG, "Ethernet PDU contents (%zu bytes)",
275 size);
276
277 log_msg(LOG_DEFAULT, LVL_DEBUG, "call ethip_received");
278 rc = ethip_received(&nic->iplink, data, size);
279 log_msg(LOG_DEFAULT, LVL_DEBUG, "free data");
280 free(data);
281
282 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_received() done, rc=%s", str_error_name(rc));
283 async_answer_0(call, rc);
284}
285
286static void ethip_nic_device_state(ethip_nic_t *nic, ipc_call_t *call)
287{
288 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_device_state()");
289 async_answer_0(call, ENOTSUP);
290}
291
292static void ethip_nic_cb_conn(ipc_call_t *icall, void *arg)
293{
294 ethip_nic_t *nic = (ethip_nic_t *)arg;
295
296 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethnip_nic_cb_conn()");
297
298 while (true) {
299 ipc_call_t call;
300 async_get_call(&call);
301
302 if (!ipc_get_imethod(&call)) {
303 async_answer_0(&call, EOK);
304 return;
305 }
306
307 switch (ipc_get_imethod(&call)) {
308 case NIC_EV_ADDR_CHANGED:
309 ethip_nic_addr_changed(nic, &call);
310 break;
311 case NIC_EV_RECEIVED:
312 ethip_nic_received(nic, &call);
313 break;
314 case NIC_EV_DEVICE_STATE:
315 ethip_nic_device_state(nic, &call);
316 break;
317 default:
318 log_msg(LOG_DEFAULT, LVL_DEBUG, "unknown IPC method: %" PRIun, ipc_get_imethod(&call));
319 async_answer_0(&call, ENOTSUP);
320 }
321 }
322}
323
324errno_t ethip_nic_discovery_start(void)
325{
326 errno_t rc = loc_register_cat_change_cb(ethip_nic_cat_change_cb, NULL);
327 if (rc != EOK) {
328 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback for NIC "
329 "discovery: %s.", str_error(rc));
330 return rc;
331 }
332
333 return ethip_nic_check_new();
334}
335
336ethip_nic_t *ethip_nic_find_by_iplink_sid(service_id_t iplink_sid)
337{
338 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid(%u)",
339 (unsigned) iplink_sid);
340
341 list_foreach(ethip_nic_list, link, ethip_nic_t, nic) {
342 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - element");
343 if (nic->iplink_sid == iplink_sid) {
344 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - found %p", nic);
345 return nic;
346 }
347 }
348
349 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - not found");
350 return NULL;
351}
352
353errno_t ethip_nic_send(ethip_nic_t *nic, void *data, size_t size)
354{
355 errno_t rc;
356 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_send(size=%zu)", size);
357 rc = nic_send_frame(nic->sess, data, size);
358 log_msg(LOG_DEFAULT, LVL_DEBUG, "nic_send_frame -> %s", str_error_name(rc));
359 return rc;
360}
361
362/** Setup accepted multicast addresses
363 *
364 * Currently the set of accepted multicast addresses is
365 * determined only based on IPv6 addresses.
366 *
367 */
368static errno_t ethip_nic_setup_multicast(ethip_nic_t *nic)
369{
370 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_setup_multicast()");
371
372 /* Count the number of multicast addresses */
373
374 size_t count = 0;
375
376 list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
377 ip_ver_t ver = inet_addr_get(&laddr->addr, NULL, NULL);
378 if (ver == ip_v6)
379 count++;
380 }
381
382 if (count == 0)
383 return nic_multicast_set_mode(nic->sess, NIC_MULTICAST_BLOCKED,
384 NULL, 0);
385
386 nic_address_t *mac_list = calloc(count, sizeof(nic_address_t));
387 if (mac_list == NULL)
388 return ENOMEM;
389
390 /* Create the multicast MAC list */
391
392 size_t i = 0;
393
394 list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
395 addr128_t v6;
396 ip_ver_t ver = inet_addr_get(&laddr->addr, NULL, &v6);
397 if (ver != ip_v6)
398 continue;
399
400 assert(i < count);
401
402 eth_addr_t mac;
403 eth_addr_solicited_node(v6, &mac);
404
405 /* Avoid duplicate addresses in the list */
406
407 bool found = false;
408
409 for (size_t j = 0; j < i; j++) {
410 eth_addr_t mac_entry;
411 eth_addr_decode(mac_list[j].address, &mac_entry);
412 if (eth_addr_compare(&mac_entry, &mac)) {
413 found = true;
414 break;
415 }
416 }
417
418 if (!found) {
419 eth_addr_encode(&mac, mac_list[i].address);
420 i++;
421 } else {
422 count--;
423 }
424 }
425
426 /* Setup the multicast MAC list */
427
428 errno_t rc = nic_multicast_set_mode(nic->sess, NIC_MULTICAST_LIST,
429 mac_list, count);
430
431 free(mac_list);
432 return rc;
433}
434
435errno_t ethip_nic_addr_add(ethip_nic_t *nic, inet_addr_t *addr)
436{
437 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_add()");
438
439 ethip_link_addr_t *laddr = ethip_nic_addr_new(addr);
440 if (laddr == NULL)
441 return ENOMEM;
442
443 list_append(&laddr->link, &nic->addr_list);
444
445 return ethip_nic_setup_multicast(nic);
446}
447
448errno_t ethip_nic_addr_remove(ethip_nic_t *nic, inet_addr_t *addr)
449{
450 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_remove()");
451
452 ethip_link_addr_t *laddr = ethip_nic_addr_find(nic, addr);
453 if (laddr == NULL)
454 return ENOENT;
455
456 list_remove(&laddr->link);
457 ethip_link_addr_delete(laddr);
458
459 return ethip_nic_setup_multicast(nic);
460}
461
462ethip_link_addr_t *ethip_nic_addr_find(ethip_nic_t *nic,
463 inet_addr_t *addr)
464{
465 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_find()");
466
467 list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
468 if (inet_addr_compare(addr, &laddr->addr))
469 return laddr;
470 }
471
472 return NULL;
473}
474
475/** @}
476 */
Note: See TracBrowser for help on using the repository browser.