source: mainline/uspace/srv/net/ethip/ethip_nic.c@ 1f1fa64

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1f1fa64 was 1f1fa64, checked in by Agnieszka Tabaka <nufcia@…>, 11 years ago

Make MAC address change possible on ethip side. Merge mainline changes.

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