[f9d3dd4] | 1 | /*
|
---|
[f05edcb] | 2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
[f9d3dd4] | 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>
|
---|
[b4edc96] | 40 | #include <inet/eth_addr.h>
|
---|
[f9d3dd4] | 41 | #include <inet/iplink_srv.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
| 43 |
|
---|
| 44 | #include "atrans.h"
|
---|
| 45 | #include "ethip.h"
|
---|
| 46 |
|
---|
| 47 | /** Address translation list (of ethip_atrans_t) */
|
---|
| 48 | static FIBRIL_MUTEX_INITIALIZE(atrans_list_lock);
|
---|
| 49 | static LIST_INITIALIZE(atrans_list);
|
---|
[3d016ac] | 50 | static FIBRIL_CONDVAR_INITIALIZE(atrans_cv);
|
---|
[f9d3dd4] | 51 |
|
---|
[d8b47eca] | 52 | static ethip_atrans_t *atrans_find(addr32_t ip_addr)
|
---|
[f9d3dd4] | 53 | {
|
---|
[feeac0d] | 54 | list_foreach(atrans_list, atrans_list, ethip_atrans_t, atrans) {
|
---|
[a2e3ee6] | 55 | if (atrans->ip_addr == ip_addr)
|
---|
[f9d3dd4] | 56 | return atrans;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | return NULL;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[b4edc96] | 62 | errno_t atrans_add(addr32_t ip_addr, eth_addr_t *mac_addr)
|
---|
[f9d3dd4] | 63 | {
|
---|
| 64 | ethip_atrans_t *atrans;
|
---|
| 65 | ethip_atrans_t *prev;
|
---|
| 66 |
|
---|
| 67 | atrans = calloc(1, sizeof(ethip_atrans_t));
|
---|
| 68 | if (atrans == NULL)
|
---|
| 69 | return ENOMEM;
|
---|
| 70 |
|
---|
[a2e3ee6] | 71 | atrans->ip_addr = ip_addr;
|
---|
[d5ed54b] | 72 | atrans->mac_addr = *mac_addr;
|
---|
[f9d3dd4] | 73 |
|
---|
| 74 | fibril_mutex_lock(&atrans_list_lock);
|
---|
| 75 | prev = atrans_find(ip_addr);
|
---|
| 76 | if (prev != NULL) {
|
---|
| 77 | list_remove(&prev->atrans_list);
|
---|
| 78 | free(prev);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | list_append(&atrans->atrans_list, &atrans_list);
|
---|
| 82 | fibril_mutex_unlock(&atrans_list_lock);
|
---|
[3d016ac] | 83 | fibril_condvar_broadcast(&atrans_cv);
|
---|
[f9d3dd4] | 84 |
|
---|
| 85 | return EOK;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[b7fd2a0] | 88 | errno_t atrans_remove(addr32_t ip_addr)
|
---|
[f9d3dd4] | 89 | {
|
---|
| 90 | ethip_atrans_t *atrans;
|
---|
| 91 |
|
---|
| 92 | fibril_mutex_lock(&atrans_list_lock);
|
---|
| 93 | atrans = atrans_find(ip_addr);
|
---|
| 94 | if (atrans == NULL) {
|
---|
| 95 | fibril_mutex_unlock(&atrans_list_lock);
|
---|
| 96 | return ENOENT;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | list_remove(&atrans->atrans_list);
|
---|
| 100 | fibril_mutex_unlock(&atrans_list_lock);
|
---|
| 101 | free(atrans);
|
---|
| 102 |
|
---|
| 103 | return EOK;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[b4edc96] | 106 | static errno_t atrans_lookup_locked(addr32_t ip_addr, eth_addr_t *mac_addr)
|
---|
[f9d3dd4] | 107 | {
|
---|
[02a09ed] | 108 | ethip_atrans_t *atrans = atrans_find(ip_addr);
|
---|
[f303f2cf] | 109 | if (atrans == NULL)
|
---|
[f9d3dd4] | 110 | return ENOENT;
|
---|
[f303f2cf] | 111 |
|
---|
[d5ed54b] | 112 | *mac_addr = atrans->mac_addr;
|
---|
[f9d3dd4] | 113 | return EOK;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[b4edc96] | 116 | errno_t atrans_lookup(addr32_t ip_addr, eth_addr_t *mac_addr)
|
---|
[f303f2cf] | 117 | {
|
---|
[b7fd2a0] | 118 | errno_t rc;
|
---|
[f303f2cf] | 119 |
|
---|
| 120 | fibril_mutex_lock(&atrans_list_lock);
|
---|
| 121 | rc = atrans_lookup_locked(ip_addr, mac_addr);
|
---|
| 122 | fibril_mutex_unlock(&atrans_list_lock);
|
---|
| 123 |
|
---|
| 124 | return rc;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | static void atrans_lookup_timeout_handler(void *arg)
|
---|
| 128 | {
|
---|
| 129 | bool *timedout = (bool *)arg;
|
---|
| 130 |
|
---|
| 131 | fibril_mutex_lock(&atrans_list_lock);
|
---|
| 132 | *timedout = true;
|
---|
| 133 | fibril_mutex_unlock(&atrans_list_lock);
|
---|
| 134 | fibril_condvar_broadcast(&atrans_cv);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[bd41ac52] | 137 | errno_t atrans_lookup_timeout(addr32_t ip_addr, usec_t timeout,
|
---|
[b4edc96] | 138 | eth_addr_t *mac_addr)
|
---|
[3d016ac] | 139 | {
|
---|
[f303f2cf] | 140 | fibril_timer_t *t;
|
---|
| 141 | bool timedout;
|
---|
[b7fd2a0] | 142 | errno_t rc;
|
---|
[f303f2cf] | 143 |
|
---|
| 144 | t = fibril_timer_create(NULL);
|
---|
| 145 | if (t == NULL)
|
---|
| 146 | return ENOMEM;
|
---|
| 147 |
|
---|
| 148 | timedout = false;
|
---|
| 149 | fibril_timer_set(t, timeout, atrans_lookup_timeout_handler, &timedout);
|
---|
| 150 |
|
---|
[3d016ac] | 151 | fibril_mutex_lock(&atrans_list_lock);
|
---|
[f303f2cf] | 152 |
|
---|
| 153 | while ((rc = atrans_lookup_locked(ip_addr, mac_addr)) == ENOENT &&
|
---|
| 154 | !timedout) {
|
---|
| 155 | fibril_condvar_wait(&atrans_cv, &atrans_list_lock);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[3d016ac] | 158 | fibril_mutex_unlock(&atrans_list_lock);
|
---|
[f303f2cf] | 159 | (void) fibril_timer_clear(t);
|
---|
[5c2e8d0] | 160 | fibril_timer_destroy(t);
|
---|
[f303f2cf] | 161 |
|
---|
[3d016ac] | 162 | return rc;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[f9d3dd4] | 165 | /** @}
|
---|
| 166 | */
|
---|