[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 | {
|
---|
| 58 | list_foreach(ntrans_list, link) {
|
---|
| 59 | inet_ntrans_t *ntrans = list_get_instance(link,
|
---|
| 60 | inet_ntrans_t, ntrans_list);
|
---|
| 61 |
|
---|
| 62 | if (addr128_compare(ntrans->ip_addr, ip_addr))
|
---|
| 63 | return ntrans;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | return NULL;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[fc4bf2a] | 69 | /** Add entry to translation table
|
---|
| 70 | *
|
---|
| 71 | * @param ip_addr IPv6 address of the new entry
|
---|
| 72 | * @param mac_addr MAC address of the new entry
|
---|
| 73 | *
|
---|
| 74 | * @return EOK on success
|
---|
| 75 | * @return ENOMEM if not enough memory
|
---|
| 76 | *
|
---|
| 77 | */
|
---|
[08ed137] | 78 | int ntrans_add(addr128_t ip_addr, addr48_t mac_addr)
|
---|
| 79 | {
|
---|
| 80 | inet_ntrans_t *ntrans;
|
---|
| 81 | inet_ntrans_t *prev;
|
---|
| 82 |
|
---|
| 83 | ntrans = calloc(1, sizeof(inet_ntrans_t));
|
---|
| 84 | if (ntrans == NULL)
|
---|
| 85 | return ENOMEM;
|
---|
| 86 |
|
---|
| 87 | addr128(ip_addr, ntrans->ip_addr);
|
---|
| 88 | addr48(mac_addr, ntrans->mac_addr);
|
---|
| 89 |
|
---|
| 90 | fibril_mutex_lock(&ntrans_list_lock);
|
---|
| 91 | prev = ntrans_find(ip_addr);
|
---|
| 92 | if (prev != NULL) {
|
---|
| 93 | list_remove(&prev->ntrans_list);
|
---|
| 94 | free(prev);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | list_append(&ntrans->ntrans_list, &ntrans_list);
|
---|
| 98 | fibril_mutex_unlock(&ntrans_list_lock);
|
---|
| 99 | fibril_condvar_broadcast(&ntrans_cv);
|
---|
| 100 |
|
---|
| 101 | return EOK;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[fc4bf2a] | 104 | /** Remove entry from translation table
|
---|
| 105 | *
|
---|
| 106 | * @param ip_addr IPv6 address of the entry to be removed
|
---|
| 107 | *
|
---|
| 108 | * @return EOK on success
|
---|
| 109 | * @return ENOENT when no such address found
|
---|
| 110 | *
|
---|
| 111 | */
|
---|
[08ed137] | 112 | int ntrans_remove(addr128_t ip_addr)
|
---|
| 113 | {
|
---|
| 114 | inet_ntrans_t *ntrans;
|
---|
| 115 |
|
---|
| 116 | fibril_mutex_lock(&ntrans_list_lock);
|
---|
| 117 | ntrans = ntrans_find(ip_addr);
|
---|
| 118 | if (ntrans == NULL) {
|
---|
| 119 | fibril_mutex_unlock(&ntrans_list_lock);
|
---|
| 120 | return ENOENT;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | list_remove(&ntrans->ntrans_list);
|
---|
| 124 | fibril_mutex_unlock(&ntrans_list_lock);
|
---|
| 125 | free(ntrans);
|
---|
| 126 |
|
---|
| 127 | return EOK;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[fc4bf2a] | 130 | /** Translate IPv6 address to MAC address using the translation table
|
---|
| 131 | *
|
---|
| 132 | * @param ip_addr IPv6 address to be translated
|
---|
| 133 | * @param mac_addr MAC address to be assigned
|
---|
| 134 | *
|
---|
| 135 | * @return EOK on success
|
---|
| 136 | * @return ENOENT when no such address found
|
---|
| 137 | *
|
---|
| 138 | */
|
---|
[08ed137] | 139 | int ntrans_lookup(addr128_t ip_addr, addr48_t mac_addr)
|
---|
| 140 | {
|
---|
| 141 | fibril_mutex_lock(&ntrans_list_lock);
|
---|
| 142 | inet_ntrans_t *ntrans = ntrans_find(ip_addr);
|
---|
| 143 | if (ntrans == NULL) {
|
---|
| 144 | fibril_mutex_unlock(&ntrans_list_lock);
|
---|
| 145 | return ENOENT;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | fibril_mutex_unlock(&ntrans_list_lock);
|
---|
| 149 | addr48(ntrans->mac_addr, mac_addr);
|
---|
| 150 | return EOK;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[fc4bf2a] | 153 | /** Wait on translation table CV for some time
|
---|
| 154 | *
|
---|
| 155 | * @param timeout Timeout in microseconds
|
---|
| 156 | *
|
---|
| 157 | * @return EOK if woken up by another fibril
|
---|
| 158 | * @return ETIMEDOUT if timed out
|
---|
| 159 | *
|
---|
| 160 | */
|
---|
[08ed137] | 161 | int ntrans_wait_timeout(suseconds_t timeout)
|
---|
| 162 | {
|
---|
| 163 | fibril_mutex_lock(&ntrans_list_lock);
|
---|
| 164 | int rc = fibril_condvar_wait_timeout(&ntrans_cv, &ntrans_list_lock,
|
---|
| 165 | timeout);
|
---|
| 166 | fibril_mutex_unlock(&ntrans_list_lock);
|
---|
| 167 |
|
---|
| 168 | return rc;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /** @}
|
---|
| 172 | */
|
---|