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
Line 
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
29/** @addtogroup ethip
30 * @{
31 */
32/**
33 * @file
34 * @brief
35 */
36
37#include <adt/list.h>
38#include <async.h>
39#include <stdbool.h>
40#include <errno.h>
41#include <fibril_synch.h>
42#include <inet/iplink_srv.h>
43#include <io/log.h>
44#include <loc.h>
45#include <nic_iface.h>
46#include <stdlib.h>
47#include <mem.h>
48#include "ethip.h"
49#include "ethip_nic.h"
50#include "pdu.h"
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) {
70 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'nic'.");
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) {
77 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting list of IP links.");
78 fibril_mutex_unlock(&ethip_discovery_lock);
79 return EIO;
80 }
81
82 for (i = 0; i < count; i++) {
83 already_known = false;
84
85 list_foreach(ethip_nic_list, link, ethip_nic_t, nic) {
86 if (nic->svc_id == svcs[i]) {
87 already_known = true;
88 break;
89 }
90 }
91
92 if (!already_known) {
93 log_msg(LOG_DEFAULT, LVL_DEBUG, "Found NIC '%lu'",
94 (unsigned long) svcs[i]);
95 rc = ethip_nic_open(svcs[i]);
96 if (rc != EOK)
97 log_msg(LOG_DEFAULT, LVL_ERROR, "Could not open NIC.");
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) {
109 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating NIC structure. "
110 "Out of memory.");
111 return NULL;
112 }
113
114 link_initialize(&nic->link);
115 list_initialize(&nic->addr_list);
116
117 return nic;
118}
119
120static ethip_link_addr_t *ethip_nic_addr_new(inet_addr_t *addr)
121{
122 ethip_link_addr_t *laddr = calloc(1, sizeof(ethip_link_addr_t));
123 if (laddr == NULL) {
124 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating NIC address structure. "
125 "Out of memory.");
126 return NULL;
127 }
128
129 link_initialize(&laddr->link);
130 laddr->addr = *addr;
131
132 return laddr;
133}
134
135static void ethip_nic_delete(ethip_nic_t *nic)
136{
137 if (nic->svc_name != NULL)
138 free(nic->svc_name);
139
140 free(nic);
141}
142
143static void ethip_link_addr_delete(ethip_link_addr_t *laddr)
144{
145 free(laddr);
146}
147
148static int ethip_nic_open(service_id_t sid)
149{
150 bool in_list = false;
151 nic_address_t nic_address;
152
153 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_open()");
154 ethip_nic_t *nic = ethip_nic_new();
155 if (nic == NULL)
156 return ENOMEM;
157
158 int rc = loc_service_get_name(sid, &nic->svc_name);
159 if (rc != EOK) {
160 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
161 goto error;
162 }
163
164 nic->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
165 if (nic->sess == NULL) {
166 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting '%s'", nic->svc_name);
167 goto error;
168 }
169
170 nic->svc_id = sid;
171
172 rc = nic_callback_create(nic->sess, ethip_nic_cb_conn, nic);
173 if (rc != EOK) {
174 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed creating callback connection "
175 "from '%s'", nic->svc_name);
176 goto error;
177 }
178
179 log_msg(LOG_DEFAULT, LVL_DEBUG, "Opened NIC '%s'", nic->svc_name);
180 list_append(&nic->link, &ethip_nic_list);
181 in_list = true;
182
183 rc = ethip_iplink_init(nic);
184 if (rc != EOK)
185 goto error;
186
187 rc = nic_get_address(nic->sess, &nic_address);
188 if (rc != EOK) {
189 log_msg(LOG_DEFAULT, LVL_ERROR, "Error getting MAC address of NIC '%s'.",
190 nic->svc_name);
191 goto error;
192 }
193
194 addr48(nic_address.address, nic->mac_addr);
195
196 rc = nic_set_state(nic->sess, NIC_STATE_ACTIVE);
197 if (rc != EOK) {
198 log_msg(LOG_DEFAULT, LVL_ERROR, "Error activating NIC '%s'.",
199 nic->svc_name);
200 goto error;
201 }
202
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
210 log_msg(LOG_DEFAULT, LVL_DEBUG, "Initialized IP link service,");
211
212 return EOK;
213
214error:
215 if (in_list)
216 list_remove(&nic->link);
217
218 if (nic->sess != NULL)
219 async_hangup(nic->sess);
220
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{
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);
249}
250
251static void ethip_nic_received(ethip_nic_t *nic, ipc_callid_t callid,
252 ipc_call_t *call)
253{
254 int rc;
255 void *data;
256 size_t size;
257
258 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_received() nic=%p", nic);
259
260 rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
261 if (rc != EOK) {
262 log_msg(LOG_DEFAULT, LVL_DEBUG, "data_write_accept() failed");
263 return;
264 }
265
266 log_msg(LOG_DEFAULT, LVL_DEBUG, "Ethernet PDU contents (%zu bytes)",
267 size);
268
269 log_msg(LOG_DEFAULT, LVL_DEBUG, "call ethip_received");
270 rc = ethip_received(&nic->iplink, data, size);
271 log_msg(LOG_DEFAULT, LVL_DEBUG, "free data");
272 free(data);
273
274 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_received() done, rc=%d", rc);
275 async_answer_0(callid, rc);
276}
277
278static void ethip_nic_device_state(ethip_nic_t *nic, ipc_callid_t callid,
279 ipc_call_t *call)
280{
281 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_device_state()");
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{
287 ethip_nic_t *nic = (ethip_nic_t *)arg;
288
289 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethnip_nic_cb_conn()");
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:
311 log_msg(LOG_DEFAULT, LVL_DEBUG, "unknown IPC method: %d", IPC_GET_IMETHOD(call));
312 async_answer_0(callid, ENOTSUP);
313 }
314 }
315}
316
317int ethip_nic_discovery_start(void)
318{
319 int rc = loc_register_cat_change_cb(ethip_nic_cat_change_cb);
320 if (rc != EOK) {
321 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback for NIC "
322 "discovery (%d).", rc);
323 return rc;
324 }
325
326 return ethip_nic_check_new();
327}
328
329ethip_nic_t *ethip_nic_find_by_iplink_sid(service_id_t iplink_sid)
330{
331 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid(%u)",
332 (unsigned) iplink_sid);
333
334 list_foreach(ethip_nic_list, link, ethip_nic_t, nic) {
335 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - element");
336 if (nic->iplink_sid == iplink_sid) {
337 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - found %p", nic);
338 return nic;
339 }
340 }
341
342 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - not found");
343 return NULL;
344}
345
346int ethip_nic_send(ethip_nic_t *nic, void *data, size_t size)
347{
348 int rc;
349 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_send(size=%zu)", size);
350 rc = nic_send_frame(nic->sess, data, size);
351 log_msg(LOG_DEFAULT, LVL_DEBUG, "nic_send_frame -> %d", rc);
352 return rc;
353}
354
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
369 list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
370 ip_ver_t ver = inet_addr_get(&laddr->addr, NULL, NULL);
371 if (ver == ip_v6)
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
387 list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
388 addr128_t v6;
389 ip_ver_t ver = inet_addr_get(&laddr->addr, NULL, &v6);
390 if (ver != ip_v6)
391 continue;
392
393 assert(i < count);
394
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
425int ethip_nic_addr_add(ethip_nic_t *nic, inet_addr_t *addr)
426{
427 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_add()");
428
429 ethip_link_addr_t *laddr = ethip_nic_addr_new(addr);
430 if (laddr == NULL)
431 return ENOMEM;
432
433 list_append(&laddr->link, &nic->addr_list);
434
435 return ethip_nic_setup_multicast(nic);
436}
437
438int ethip_nic_addr_remove(ethip_nic_t *nic, inet_addr_t *addr)
439{
440 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_remove()");
441
442 ethip_link_addr_t *laddr = ethip_nic_addr_find(nic, addr);
443 if (laddr == NULL)
444 return ENOENT;
445
446 list_remove(&laddr->link);
447 ethip_link_addr_delete(laddr);
448
449 return ethip_nic_setup_multicast(nic);
450}
451
452ethip_link_addr_t *ethip_nic_addr_find(ethip_nic_t *nic,
453 inet_addr_t *addr)
454{
455 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_find()");
456
457 list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
458 if (inet_addr_compare(addr, &laddr->addr))
459 return laddr;
460 }
461
462 return NULL;
463}
464
465/** @}
466 */
Note: See TracBrowser for help on using the repository browser.