source: mainline/uspace/srv/net/il/arp/arp.c@ 765678f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 765678f was 126daa2, checked in by Martin Decky <martin@…>, 14 years ago

cstyle

  • Property mode set to 100644
File size: 24.3 KB
Line 
1/*
2 * Copyright (c) 2009 Lukas Mejdrech
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 arp
30 * @{
31 */
32
33/** @file
34 * ARP module implementation.
35 * @see arp.h
36 */
37
38#include <async.h>
39#include <malloc.h>
40#include <mem.h>
41#include <fibril_synch.h>
42#include <assert.h>
43#include <stdio.h>
44#include <str.h>
45#include <task.h>
46#include <adt/measured_strings.h>
47#include <ipc/services.h>
48#include <ipc/net.h>
49#include <ipc/arp.h>
50#include <ipc/il.h>
51#include <ipc/nil.h>
52#include <byteorder.h>
53#include <errno.h>
54#include <net/modules.h>
55#include <net/device.h>
56#include <net/packet.h>
57#include <nil_remote.h>
58#include <protocol_map.h>
59#include <packet_client.h>
60#include <packet_remote.h>
61#include <il_remote.h>
62#include <il_skel.h>
63#include "arp.h"
64
65/** ARP module name. */
66#define NAME "arp"
67
68/** Number of microseconds to wait for an ARP reply. */
69#define ARP_TRANS_WAIT 1000000
70
71/** @name ARP operation codes definitions */
72/*@{*/
73
74/** REQUEST operation code. */
75#define ARPOP_REQUEST 1
76
77/** REPLY operation code. */
78#define ARPOP_REPLY 2
79
80/*@}*/
81
82/** Type definition of an ARP protocol header.
83 * @see arp_header
84 */
85typedef struct arp_header arp_header_t;
86
87/** ARP protocol header. */
88struct arp_header {
89 /**
90 * Hardware type identifier.
91 * @see hardware.h
92 */
93 uint16_t hardware;
94
95 /** Protocol identifier. */
96 uint16_t protocol;
97 /** Hardware address length in bytes. */
98 uint8_t hardware_length;
99 /** Protocol address length in bytes. */
100 uint8_t protocol_length;
101
102 /**
103 * ARP packet type.
104 * @see arp_oc.h
105 */
106 uint16_t operation;
107} __attribute__ ((packed));
108
109/** ARP global data. */
110arp_globals_t arp_globals;
111
112DEVICE_MAP_IMPLEMENT(arp_cache, arp_device_t);
113INT_MAP_IMPLEMENT(arp_protos, arp_proto_t);
114GENERIC_CHAR_MAP_IMPLEMENT(arp_addr, arp_trans_t);
115
116static void arp_clear_trans(arp_trans_t *trans)
117{
118 if (trans->hw_addr) {
119 free(trans->hw_addr);
120 trans->hw_addr = NULL;
121 }
122
123 fibril_condvar_broadcast(&trans->cv);
124}
125
126static void arp_clear_addr(arp_addr_t *addresses)
127{
128 int count;
129
130 for (count = arp_addr_count(addresses) - 1; count >= 0; count--) {
131 arp_trans_t *trans = arp_addr_items_get_index(&addresses->values,
132 count);
133 if (trans)
134 arp_clear_trans(trans);
135 }
136}
137
138/** Clear the device specific data.
139 *
140 * @param[in] device Device specific data.
141 */
142static void arp_clear_device(arp_device_t *device)
143{
144 int count;
145
146 for (count = arp_protos_count(&device->protos) - 1; count >= 0;
147 count--) {
148 arp_proto_t *proto = arp_protos_get_index(&device->protos,
149 count);
150
151 if (proto) {
152 if (proto->addr)
153 free(proto->addr);
154
155 if (proto->addr_data)
156 free(proto->addr_data);
157
158 arp_clear_addr(&proto->addresses);
159 arp_addr_destroy(&proto->addresses, free);
160 }
161 }
162
163 arp_protos_clear(&device->protos, free);
164}
165
166static int arp_clean_cache_req(void)
167{
168 int count;
169
170 fibril_mutex_lock(&arp_globals.lock);
171 for (count = arp_cache_count(&arp_globals.cache) - 1; count >= 0;
172 count--) {
173 arp_device_t *device = arp_cache_get_index(&arp_globals.cache,
174 count);
175
176 if (device)
177 arp_clear_device(device);
178 }
179
180 arp_cache_clear(&arp_globals.cache, free);
181 fibril_mutex_unlock(&arp_globals.lock);
182
183 return EOK;
184}
185
186static int arp_clear_address_req(nic_device_id_t device_id,
187 services_t protocol, measured_string_t *address)
188{
189 fibril_mutex_lock(&arp_globals.lock);
190
191 arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id);
192 if (!device) {
193 fibril_mutex_unlock(&arp_globals.lock);
194 return ENOENT;
195 }
196
197 arp_proto_t *proto = arp_protos_find(&device->protos, protocol);
198 if (!proto) {
199 fibril_mutex_unlock(&arp_globals.lock);
200 return ENOENT;
201 }
202
203 arp_trans_t *trans = arp_addr_find(&proto->addresses, address->value,
204 address->length);
205 if (trans)
206 arp_clear_trans(trans);
207
208 arp_addr_exclude(&proto->addresses, address->value, address->length, free);
209
210 fibril_mutex_unlock(&arp_globals.lock);
211 return EOK;
212}
213
214static int arp_clear_device_req(nic_device_id_t device_id)
215{
216 fibril_mutex_lock(&arp_globals.lock);
217
218 arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id);
219 if (!device) {
220 fibril_mutex_unlock(&arp_globals.lock);
221 return ENOENT;
222 }
223
224 arp_clear_device(device);
225
226 fibril_mutex_unlock(&arp_globals.lock);
227 return EOK;
228}
229
230/** Create new protocol specific data.
231 *
232 * Allocate and return the needed memory block as the proto parameter.
233 *
234 * @param[out] proto Allocated protocol specific data.
235 * @param[in] service Protocol module service.
236 * @param[in] address Actual protocol device address.
237 *
238 * @return EOK on success.
239 * @return ENOMEM if there is not enough memory left.
240 *
241 */
242static int arp_proto_create(arp_proto_t **proto, services_t service,
243 measured_string_t *address)
244{
245 *proto = (arp_proto_t *) malloc(sizeof(arp_proto_t));
246 if (!*proto)
247 return ENOMEM;
248
249 (*proto)->service = service;
250 (*proto)->addr = address;
251 (*proto)->addr_data = address->value;
252
253 int rc = arp_addr_initialize(&(*proto)->addresses);
254 if (rc != EOK) {
255 free(*proto);
256 return rc;
257 }
258
259 return EOK;
260}
261
262/** Process the received ARP packet.
263 *
264 * Update the source hardware address if the source entry exists or the packet
265 * is targeted to my protocol address.
266 *
267 * Respond to the ARP request if the packet is the ARP request and is
268 * targeted to my address.
269 *
270 * @param[in] device_id Source device identifier.
271 * @param[in,out] packet Received packet.
272 *
273 * @return EOK on success and the packet is no longer needed.
274 * @return One on success and the packet has been reused.
275 * @return EINVAL if the packet is too small to carry an ARP
276 * packet.
277 * @return EINVAL if the received address lengths differs from
278 * the registered values.
279 * @return ENOENT if the device is not found in the cache.
280 * @return ENOENT if the protocol for the device is not found in
281 * the cache.
282 * @return ENOMEM if there is not enough memory left.
283 *
284 */
285static int arp_receive_message(nic_device_id_t device_id, packet_t *packet)
286{
287 int rc;
288
289 size_t length = packet_get_data_length(packet);
290 if (length <= sizeof(arp_header_t))
291 return EINVAL;
292
293 arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id);
294 if (!device)
295 return ENOENT;
296
297 arp_header_t *header = (arp_header_t *) packet_get_data(packet);
298 if ((ntohs(header->hardware) != device->hardware) ||
299 (length < sizeof(arp_header_t) + header->hardware_length * 2U +
300 header->protocol_length * 2U)) {
301 return EINVAL;
302 }
303
304 arp_proto_t *proto = arp_protos_find(&device->protos,
305 protocol_unmap(device->service, ntohs(header->protocol)));
306 if (!proto)
307 return ENOENT;
308
309 uint8_t *src_hw = ((uint8_t *) header) + sizeof(arp_header_t);
310 uint8_t *src_proto = src_hw + header->hardware_length;
311 uint8_t *des_hw = src_proto + header->protocol_length;
312 uint8_t *des_proto = des_hw + header->hardware_length;
313
314 arp_trans_t *trans = arp_addr_find(&proto->addresses, src_proto,
315 header->protocol_length);
316
317 if ((trans) && (trans->hw_addr)) {
318 /* Translation exists */
319 if (trans->hw_addr->length != header->hardware_length)
320 return EINVAL;
321
322 memcpy(trans->hw_addr->value, src_hw, trans->hw_addr->length);
323 }
324
325 /* Is my protocol address? */
326 if (proto->addr->length != header->protocol_length)
327 return EINVAL;
328
329 if (!bcmp(proto->addr->value, des_proto, proto->addr->length)) {
330 if (!trans) {
331 /* Update the translation */
332 trans = (arp_trans_t *) malloc(sizeof(arp_trans_t));
333 if (!trans)
334 return ENOMEM;
335
336 trans->hw_addr = NULL;
337 fibril_condvar_initialize(&trans->cv);
338 rc = arp_addr_add(&proto->addresses, src_proto,
339 header->protocol_length, trans);
340 if (rc != EOK) {
341 free(trans);
342 return rc;
343 }
344 }
345
346 if (!trans->hw_addr) {
347 trans->hw_addr = measured_string_create_bulk(src_hw,
348 header->hardware_length);
349 if (!trans->hw_addr)
350 return ENOMEM;
351
352 /* Notify the fibrils that wait for the translation. */
353 fibril_condvar_broadcast(&trans->cv);
354 }
355
356 if (ntohs(header->operation) == ARPOP_REQUEST) {
357 header->operation = htons(ARPOP_REPLY);
358 memcpy(des_proto, src_proto, header->protocol_length);
359 memcpy(src_proto, proto->addr->value,
360 header->protocol_length);
361 memcpy(src_hw, device->addr,
362 device->packet_dimension.addr_len);
363 memcpy(des_hw, trans->hw_addr->value,
364 header->hardware_length);
365
366 rc = packet_set_addr(packet, src_hw, des_hw,
367 header->hardware_length);
368 if (rc != EOK)
369 return rc;
370
371 nil_send_msg(device->sess, device_id, packet,
372 SERVICE_ARP);
373 return 1;
374 }
375 }
376
377 return EOK;
378}
379
380/** Update the device content length according to the new MTU value.
381 *
382 * @param[in] device_id Device identifier.
383 * @param[in] mtu New MTU value.
384 *
385 * @return ENOENT if device is not found.
386 * @return EOK on success.
387 *
388 */
389static int arp_mtu_changed_message(nic_device_id_t device_id, size_t mtu)
390{
391 fibril_mutex_lock(&arp_globals.lock);
392
393 arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id);
394 if (!device) {
395 fibril_mutex_unlock(&arp_globals.lock);
396 return ENOENT;
397 }
398
399 device->packet_dimension.content = mtu;
400
401 fibril_mutex_unlock(&arp_globals.lock);
402
403 printf("%s: Device %d changed MTU to %zu\n", NAME, device_id, mtu);
404
405 return EOK;
406}
407
408static int arp_addr_changed_message(nic_device_id_t device_id)
409{
410 uint8_t addr_buffer[NIC_MAX_ADDRESS_LENGTH];
411 size_t length;
412 ipc_callid_t data_callid;
413 if (!async_data_write_receive(&data_callid, &length)) {
414 async_answer_0(data_callid, EINVAL);
415 return EINVAL;
416 }
417 if (length > NIC_MAX_ADDRESS_LENGTH) {
418 async_answer_0(data_callid, ELIMIT);
419 return ELIMIT;
420 }
421 if (async_data_write_finalize(data_callid, addr_buffer, length) != EOK) {
422 return EINVAL;
423 }
424
425 fibril_mutex_lock(&arp_globals.lock);
426
427 arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id);
428 if (!device) {
429 fibril_mutex_unlock(&arp_globals.lock);
430 return ENOENT;
431 }
432
433 memcpy(device->addr, addr_buffer, length);
434 device->addr_len = length;
435
436 fibril_mutex_unlock(&arp_globals.lock);
437 return EOK;
438}
439
440/** Process IPC messages from the registered device driver modules
441 *
442 * @param[in] iid Message identifier.
443 * @param[in,out] icall Message parameters.
444 * @param[in] arg Local argument.
445 *
446 */
447static void arp_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg)
448{
449 packet_t *packet;
450 int rc;
451
452 while (true) {
453 switch (IPC_GET_IMETHOD(*icall)) {
454 case NET_IL_DEVICE_STATE:
455 /* Do nothing - keep the cache */
456 async_answer_0(iid, (sysarg_t) EOK);
457 break;
458
459 case NET_IL_RECEIVED:
460 rc = packet_translate_remote(arp_globals.net_sess, &packet,
461 IPC_GET_PACKET(*icall));
462 if (rc == EOK) {
463 fibril_mutex_lock(&arp_globals.lock);
464 do {
465 packet_t *next = pq_detach(packet);
466 rc = arp_receive_message(IPC_GET_DEVICE(*icall), packet);
467 if (rc != 1) {
468 pq_release_remote(arp_globals.net_sess,
469 packet_get_id(packet));
470 }
471
472 packet = next;
473 } while (packet);
474 fibril_mutex_unlock(&arp_globals.lock);
475 }
476 async_answer_0(iid, (sysarg_t) rc);
477 break;
478
479 case NET_IL_MTU_CHANGED:
480 rc = arp_mtu_changed_message(IPC_GET_DEVICE(*icall),
481 IPC_GET_MTU(*icall));
482 async_answer_0(iid, (sysarg_t) rc);
483 break;
484 case NET_IL_ADDR_CHANGED:
485 rc = arp_addr_changed_message(IPC_GET_DEVICE(*icall));
486 async_answer_0(iid, (sysarg_t) rc);
487
488 default:
489 async_answer_0(iid, (sysarg_t) ENOTSUP);
490 }
491
492 iid = async_get_call(icall);
493 }
494}
495
496/** Register the device.
497 *
498 * Create new device entry in the cache or update the protocol address if the
499 * device with the device identifier and the driver service exists.
500 *
501 * @param[in] device_id Device identifier.
502 * @param[in] service Device driver service.
503 * @param[in] protocol Protocol service.
504 * @param[in] address Actual device protocol address.
505 *
506 * @return EOK on success.
507 * @return EEXIST if another device with the same device identifier
508 * and different driver service exists.
509 * @return ENOMEM if there is not enough memory left.
510 * @return Other error codes as defined for the
511 * measured_strings_return() function.
512 *
513 */
514static int arp_device_message(nic_device_id_t device_id, services_t service,
515 services_t protocol, measured_string_t *address)
516{
517 int index;
518 int rc;
519
520 fibril_mutex_lock(&arp_globals.lock);
521
522 /* An existing device? */
523 arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id);
524 if (device) {
525 if (device->service != service) {
526 printf("%s: Device %d already exists\n", NAME,
527 device->device_id);
528 fibril_mutex_unlock(&arp_globals.lock);
529 return EEXIST;
530 }
531
532 arp_proto_t *proto = arp_protos_find(&device->protos, protocol);
533 if (proto) {
534 free(proto->addr);
535 free(proto->addr_data);
536 proto->addr = address;
537 proto->addr_data = address->value;
538 } else {
539 rc = arp_proto_create(&proto, protocol, address);
540 if (rc != EOK) {
541 fibril_mutex_unlock(&arp_globals.lock);
542 return rc;
543 }
544
545 index = arp_protos_add(&device->protos, proto->service,
546 proto);
547 if (index < 0) {
548 fibril_mutex_unlock(&arp_globals.lock);
549 free(proto);
550 return index;
551 }
552
553 printf("%s: New protocol added (id: %d, proto: %d)\n", NAME,
554 device_id, protocol);
555 }
556 } else {
557 hw_type_t hardware = hardware_map(service);
558 if (!hardware)
559 return ENOENT;
560
561 /* Create new device */
562 device = (arp_device_t *) malloc(sizeof(arp_device_t));
563 if (!device) {
564 fibril_mutex_unlock(&arp_globals.lock);
565 return ENOMEM;
566 }
567
568 device->hardware = hardware;
569 device->device_id = device_id;
570 rc = arp_protos_initialize(&device->protos);
571 if (rc != EOK) {
572 fibril_mutex_unlock(&arp_globals.lock);
573 free(device);
574 return rc;
575 }
576
577 arp_proto_t *proto;
578 rc = arp_proto_create(&proto, protocol, address);
579 if (rc != EOK) {
580 fibril_mutex_unlock(&arp_globals.lock);
581 free(device);
582 return rc;
583 }
584
585 index = arp_protos_add(&device->protos, proto->service, proto);
586 if (index < 0) {
587 fibril_mutex_unlock(&arp_globals.lock);
588 arp_protos_destroy(&device->protos, free);
589 free(device);
590 return index;
591 }
592
593 device->service = service;
594
595 /* Bind */
596 device->sess = nil_bind_service(device->service,
597 (sysarg_t) device->device_id, SERVICE_ARP,
598 arp_receiver);
599 if (device->sess == NULL) {
600 fibril_mutex_unlock(&arp_globals.lock);
601 arp_protos_destroy(&device->protos, free);
602 free(device);
603 return EREFUSED;
604 }
605
606 /* Get packet dimensions */
607 rc = nil_packet_size_req(device->sess, device_id,
608 &device->packet_dimension);
609 if (rc != EOK) {
610 fibril_mutex_unlock(&arp_globals.lock);
611 arp_protos_destroy(&device->protos, free);
612 free(device);
613 return rc;
614 }
615
616 /* Get hardware address */
617 int len = nil_get_addr_req(device->sess, device_id, device->addr,
618 NIC_MAX_ADDRESS_LENGTH);
619 if (len < 0) {
620 fibril_mutex_unlock(&arp_globals.lock);
621 arp_protos_destroy(&device->protos, free);
622 free(device);
623 return len;
624 }
625
626 device->addr_len = len;
627
628 /* Get broadcast address */
629 len = nil_get_broadcast_addr_req(device->sess, device_id,
630 device->broadcast_addr, NIC_MAX_ADDRESS_LENGTH);
631 if (len < 0) {
632 fibril_mutex_unlock(&arp_globals.lock);
633 arp_protos_destroy(&device->protos, free);
634 free(device);
635 return len;
636 }
637
638 device->broadcast_addr_len = len;
639
640 rc = arp_cache_add(&arp_globals.cache, device->device_id,
641 device);
642 if (rc != EOK) {
643 fibril_mutex_unlock(&arp_globals.lock);
644 arp_protos_destroy(&device->protos, free);
645 free(device);
646 return rc;
647 }
648 printf("%s: Device registered (id: %d, type: 0x%x, service: %d,"
649 " proto: %d)\n", NAME, device->device_id, device->hardware,
650 device->service, protocol);
651 }
652
653 fibril_mutex_unlock(&arp_globals.lock);
654 return EOK;
655}
656
657int il_initialize(async_sess_t *net_sess)
658{
659 fibril_mutex_initialize(&arp_globals.lock);
660
661 fibril_mutex_lock(&arp_globals.lock);
662 arp_globals.net_sess = net_sess;
663 int rc = arp_cache_initialize(&arp_globals.cache);
664 fibril_mutex_unlock(&arp_globals.lock);
665
666 return rc;
667}
668
669static int arp_send_request(nic_device_id_t device_id, services_t protocol,
670 measured_string_t *target, arp_device_t *device, arp_proto_t *proto)
671{
672 /* ARP packet content size = header + (address + translation) * 2 */
673 size_t length = 8 + 2 * (proto->addr->length + device->addr_len);
674 if (length > device->packet_dimension.content)
675 return ELIMIT;
676
677 packet_t *packet = packet_get_4_remote(arp_globals.net_sess,
678 device->packet_dimension.addr_len, device->packet_dimension.prefix,
679 length, device->packet_dimension.suffix);
680 if (!packet)
681 return ENOMEM;
682
683 arp_header_t *header = (arp_header_t *) packet_suffix(packet, length);
684 if (!header) {
685 pq_release_remote(arp_globals.net_sess, packet_get_id(packet));
686 return ENOMEM;
687 }
688
689 header->hardware = htons(device->hardware);
690 header->hardware_length = (uint8_t) device->addr_len;
691 header->protocol = htons(protocol_map(device->service, protocol));
692 header->protocol_length = (uint8_t) proto->addr->length;
693 header->operation = htons(ARPOP_REQUEST);
694
695 length = sizeof(arp_header_t);
696 memcpy(((uint8_t *) header) + length, device->addr,
697 device->addr_len);
698 length += device->addr_len;
699 memcpy(((uint8_t *) header) + length, proto->addr->value,
700 proto->addr->length);
701 length += proto->addr->length;
702 bzero(((uint8_t *) header) + length, device->addr_len);
703 length += device->addr_len;
704 memcpy(((uint8_t *) header) + length, target->value, target->length);
705
706 int rc = packet_set_addr(packet, device->addr, device->broadcast_addr,
707 device->addr_len);
708 if (rc != EOK) {
709 pq_release_remote(arp_globals.net_sess, packet_get_id(packet));
710 return rc;
711 }
712
713 nil_send_msg(device->sess, device_id, packet, SERVICE_ARP);
714 return EOK;
715}
716
717/** Return the hardware address for the given protocol address.
718 *
719 * Send the ARP request packet if the hardware address is not found in the
720 * cache.
721 *
722 * @param[in] device_id Device identifier.
723 * @param[in] protocol Protocol service.
724 * @param[in] target Target protocol address.
725 * @param[out] translation Where the hardware address of the target is stored.
726 *
727 * @return EOK on success.
728 * @return EAGAIN if the caller should try again.
729 * @return Other error codes in case of error.
730 *
731 */
732static int arp_translate_message(nic_device_id_t device_id, services_t protocol,
733 measured_string_t *target, measured_string_t **translation)
734{
735 bool retry = false;
736 int rc;
737
738 assert(fibril_mutex_is_locked(&arp_globals.lock));
739
740restart:
741 if ((!target) || (!translation))
742 return EBADMEM;
743
744 arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id);
745 if (!device)
746 return ENOENT;
747
748 arp_proto_t *proto = arp_protos_find(&device->protos, protocol);
749 if ((!proto) || (proto->addr->length != target->length))
750 return ENOENT;
751
752 arp_trans_t *trans = arp_addr_find(&proto->addresses, target->value,
753 target->length);
754 if (trans) {
755 if (trans->hw_addr) {
756 /* The translation is in place. */
757 *translation = trans->hw_addr;
758 return EOK;
759 }
760
761 if (retry) {
762 /*
763 * We may get here as a result of being signalled for
764 * some reason while waiting for the translation (e.g.
765 * translation becoming available, record being removed
766 * from the table) and then losing the race for
767 * the arp_globals.lock with someone else who modified
768 * the table.
769 *
770 * Remove the incomplete record so that it is possible
771 * to make new ARP requests.
772 */
773 arp_clear_trans(trans);
774 arp_addr_exclude(&proto->addresses, target->value,
775 target->length, free);
776 return EAGAIN;
777 }
778
779 /*
780 * We are a random passer-by who merely joins an already waiting
781 * fibril in waiting for the translation.
782 */
783 rc = fibril_condvar_wait_timeout(&trans->cv, &arp_globals.lock,
784 ARP_TRANS_WAIT);
785 if (rc == ETIMEOUT)
786 return ENOENT;
787
788 /*
789 * Need to recheck because we did not hold the lock while
790 * sleeping on the condition variable.
791 */
792 retry = true;
793 goto restart;
794 }
795
796 if (retry)
797 return EAGAIN;
798
799 /*
800 * We are under the protection of arp_globals.lock, so we can afford to
801 * first send the ARP request and then insert an incomplete ARP record.
802 * The incomplete record is used to tell any other potential waiter
803 * that this fibril has already sent the request and that it is waiting
804 * for the answer. Lastly, any fibril which sees the incomplete request
805 * can perform a timed wait on its condition variable to wait for the
806 * ARP reply to arrive.
807 */
808
809 rc = arp_send_request(device_id, protocol, target, device, proto);
810 if (rc != EOK)
811 return rc;
812
813 trans = (arp_trans_t *) malloc(sizeof(arp_trans_t));
814 if (!trans)
815 return ENOMEM;
816
817 trans->hw_addr = NULL;
818 fibril_condvar_initialize(&trans->cv);
819
820 rc = arp_addr_add(&proto->addresses, target->value, target->length,
821 trans);
822 if (rc != EOK) {
823 free(trans);
824 return rc;
825 }
826
827 rc = fibril_condvar_wait_timeout(&trans->cv, &arp_globals.lock,
828 ARP_TRANS_WAIT);
829 if (rc == ETIMEOUT) {
830 /*
831 * Remove the incomplete record so that it is possible to make
832 * new ARP requests.
833 */
834 arp_clear_trans(trans);
835 arp_addr_exclude(&proto->addresses, target->value,
836 target->length, free);
837 return ENOENT;
838 }
839
840 /*
841 * We need to recheck that the translation has indeed become available,
842 * because we dropped the arp_globals.lock while sleeping on the
843 * condition variable and someone else might have e.g. removed the
844 * translation before we managed to lock arp_globals.lock again.
845 */
846
847 retry = true;
848 goto restart;
849}
850
851/** Process the ARP message.
852 *
853 * @param[in] callid Message identifier.
854 * @param[in] call Message parameters.
855 * @param[out] answer Answer.
856 * @param[out] count Number of arguments of the answer.
857 *
858 * @return EOK on success.
859 * @return ENOTSUP if the message is not known.
860 *
861 * @see arp_interface.h
862 * @see IS_NET_ARP_MESSAGE()
863 *
864 */
865int il_module_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer,
866 size_t *count)
867{
868 measured_string_t *address;
869 measured_string_t *translation;
870 uint8_t *data;
871 int rc;
872
873 *count = 0;
874
875 if (!IPC_GET_IMETHOD(*call))
876 return EOK;
877
878 switch (IPC_GET_IMETHOD(*call)) {
879 case NET_ARP_DEVICE:
880 rc = measured_strings_receive(&address, &data, 1);
881 if (rc != EOK)
882 return rc;
883
884 rc = arp_device_message(IPC_GET_DEVICE(*call),
885 IPC_GET_SERVICE(*call), ARP_GET_NETIF(*call), address);
886 if (rc != EOK) {
887 free(address);
888 free(data);
889 }
890
891 return rc;
892
893 case NET_ARP_TRANSLATE:
894 rc = measured_strings_receive(&address, &data, 1);
895 if (rc != EOK)
896 return rc;
897
898 fibril_mutex_lock(&arp_globals.lock);
899 rc = arp_translate_message(IPC_GET_DEVICE(*call),
900 IPC_GET_SERVICE(*call), address, &translation);
901 free(address);
902 free(data);
903
904 if (rc != EOK) {
905 fibril_mutex_unlock(&arp_globals.lock);
906 return rc;
907 }
908
909 if (!translation) {
910 fibril_mutex_unlock(&arp_globals.lock);
911 return ENOENT;
912 }
913
914 rc = measured_strings_reply(translation, 1);
915 fibril_mutex_unlock(&arp_globals.lock);
916 return rc;
917
918 case NET_ARP_CLEAR_DEVICE:
919 return arp_clear_device_req(IPC_GET_DEVICE(*call));
920
921 case NET_ARP_CLEAR_ADDRESS:
922 rc = measured_strings_receive(&address, &data, 1);
923 if (rc != EOK)
924 return rc;
925
926 arp_clear_address_req(IPC_GET_DEVICE(*call),
927 IPC_GET_SERVICE(*call), address);
928 free(address);
929 free(data);
930 return EOK;
931
932 case NET_ARP_CLEAN_CACHE:
933 return arp_clean_cache_req();
934 }
935
936 return ENOTSUP;
937}
938
939int main(int argc, char *argv[])
940{
941 /* Start the module */
942 return il_module_start(SERVICE_ARP);
943}
944
945/** @}
946 */
Note: See TracBrowser for help on using the repository browser.