[08ed137] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Antonin Steinhauser
|
---|
| 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 inet
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * @brief
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <adt/list.h>
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <fibril_synch.h>
|
---|
| 40 | #include <inet/iplink_srv.h>
|
---|
| 41 | #include <stdlib.h>
|
---|
| 42 | #include "ntrans.h"
|
---|
| 43 |
|
---|
| 44 | /** Address translation list (of inet_ntrans_t) */
|
---|
| 45 | static FIBRIL_MUTEX_INITIALIZE(ntrans_list_lock);
|
---|
| 46 | static LIST_INITIALIZE(ntrans_list);
|
---|
| 47 | static FIBRIL_CONDVAR_INITIALIZE(ntrans_cv);
|
---|
| 48 |
|
---|
[fc4bf2a] | 49 | /** Look for address in translation table
|
---|
| 50 | *
|
---|
| 51 | * @param ip_addr IPv6 address
|
---|
| 52 | *
|
---|
| 53 | * @return inet_ntrans_t with the address on success
|
---|
| 54 | * @return NULL if nothing found
|
---|
| 55 | */
|
---|
[08ed137] | 56 | static inet_ntrans_t *ntrans_find(addr128_t ip_addr)
|
---|
| 57 | {
|
---|
[feeac0d] | 58 | list_foreach(ntrans_list, ntrans_list, inet_ntrans_t, ntrans) {
|
---|
[08ed137] | 59 | if (addr128_compare(ntrans->ip_addr, ip_addr))
|
---|
| 60 | return ntrans;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | return NULL;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[fc4bf2a] | 66 | /** Add entry to translation table
|
---|
| 67 | *
|
---|
| 68 | * @param ip_addr IPv6 address of the new entry
|
---|
| 69 | * @param mac_addr MAC address of the new entry
|
---|
| 70 | *
|
---|
| 71 | * @return EOK on success
|
---|
| 72 | * @return ENOMEM if not enough memory
|
---|
| 73 | *
|
---|
| 74 | */
|
---|
[f05edcb] | 75 | errno_t ntrans_add(addr128_t ip_addr, addr48_t *mac_addr)
|
---|
[08ed137] | 76 | {
|
---|
| 77 | inet_ntrans_t *ntrans;
|
---|
| 78 | inet_ntrans_t *prev;
|
---|
| 79 |
|
---|
| 80 | ntrans = calloc(1, sizeof(inet_ntrans_t));
|
---|
| 81 | if (ntrans == NULL)
|
---|
| 82 | return ENOMEM;
|
---|
| 83 |
|
---|
| 84 | addr128(ip_addr, ntrans->ip_addr);
|
---|
[d5ed54b] | 85 | ntrans->mac_addr = *mac_addr;
|
---|
[08ed137] | 86 |
|
---|
| 87 | fibril_mutex_lock(&ntrans_list_lock);
|
---|
| 88 | prev = ntrans_find(ip_addr);
|
---|
| 89 | if (prev != NULL) {
|
---|
| 90 | list_remove(&prev->ntrans_list);
|
---|
| 91 | free(prev);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | list_append(&ntrans->ntrans_list, &ntrans_list);
|
---|
| 95 | fibril_mutex_unlock(&ntrans_list_lock);
|
---|
| 96 | fibril_condvar_broadcast(&ntrans_cv);
|
---|
| 97 |
|
---|
| 98 | return EOK;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[fc4bf2a] | 101 | /** Remove entry from translation table
|
---|
| 102 | *
|
---|
| 103 | * @param ip_addr IPv6 address of the entry to be removed
|
---|
| 104 | *
|
---|
| 105 | * @return EOK on success
|
---|
| 106 | * @return ENOENT when no such address found
|
---|
| 107 | *
|
---|
| 108 | */
|
---|
[b7fd2a0] | 109 | errno_t ntrans_remove(addr128_t ip_addr)
|
---|
[08ed137] | 110 | {
|
---|
| 111 | inet_ntrans_t *ntrans;
|
---|
| 112 |
|
---|
| 113 | fibril_mutex_lock(&ntrans_list_lock);
|
---|
| 114 | ntrans = ntrans_find(ip_addr);
|
---|
| 115 | if (ntrans == NULL) {
|
---|
| 116 | fibril_mutex_unlock(&ntrans_list_lock);
|
---|
| 117 | return ENOENT;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | list_remove(&ntrans->ntrans_list);
|
---|
| 121 | fibril_mutex_unlock(&ntrans_list_lock);
|
---|
| 122 | free(ntrans);
|
---|
| 123 |
|
---|
| 124 | return EOK;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[fc4bf2a] | 127 | /** Translate IPv6 address to MAC address using the translation table
|
---|
| 128 | *
|
---|
| 129 | * @param ip_addr IPv6 address to be translated
|
---|
| 130 | * @param mac_addr MAC address to be assigned
|
---|
| 131 | *
|
---|
| 132 | * @return EOK on success
|
---|
| 133 | * @return ENOENT when no such address found
|
---|
| 134 | *
|
---|
| 135 | */
|
---|
[f05edcb] | 136 | errno_t ntrans_lookup(addr128_t ip_addr, addr48_t *mac_addr)
|
---|
[08ed137] | 137 | {
|
---|
| 138 | fibril_mutex_lock(&ntrans_list_lock);
|
---|
| 139 | inet_ntrans_t *ntrans = ntrans_find(ip_addr);
|
---|
| 140 | if (ntrans == NULL) {
|
---|
| 141 | fibril_mutex_unlock(&ntrans_list_lock);
|
---|
| 142 | return ENOENT;
|
---|
| 143 | }
|
---|
[a35b458] | 144 |
|
---|
[08ed137] | 145 | fibril_mutex_unlock(&ntrans_list_lock);
|
---|
[d5ed54b] | 146 | *mac_addr = ntrans->mac_addr;
|
---|
[08ed137] | 147 | return EOK;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[fc4bf2a] | 150 | /** Wait on translation table CV for some time
|
---|
| 151 | *
|
---|
| 152 | * @param timeout Timeout in microseconds
|
---|
| 153 | *
|
---|
| 154 | * @return EOK if woken up by another fibril
|
---|
[3c7702c0] | 155 | * @return ETIMEOUT if timed out
|
---|
[fc4bf2a] | 156 | *
|
---|
| 157 | */
|
---|
[bd41ac52] | 158 | errno_t ntrans_wait_timeout(usec_t timeout)
|
---|
[08ed137] | 159 | {
|
---|
| 160 | fibril_mutex_lock(&ntrans_list_lock);
|
---|
[b7fd2a0] | 161 | errno_t rc = fibril_condvar_wait_timeout(&ntrans_cv, &ntrans_list_lock,
|
---|
[08ed137] | 162 | timeout);
|
---|
| 163 | fibril_mutex_unlock(&ntrans_list_lock);
|
---|
[a35b458] | 164 |
|
---|
[08ed137] | 165 | return rc;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | /** @}
|
---|
| 169 | */
|
---|