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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a347a11 was 6b82009, checked in by Martin Decky <martin@…>, 15 years ago

networking stack: convert to the new async framework

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