[8bf672d] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Jiri Svoboda
|
---|
| 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 <bitops.h>
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <fibril_synch.h>
|
---|
| 40 | #include <io/log.h>
|
---|
| 41 | #include <ipc/loc.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
| 43 | #include <str.h>
|
---|
| 44 | #include "sroute.h"
|
---|
[b4ec1ea] | 45 | #include "inetsrv.h"
|
---|
[8bf672d] | 46 | #include "inet_link.h"
|
---|
| 47 |
|
---|
| 48 | static FIBRIL_MUTEX_INITIALIZE(sroute_list_lock);
|
---|
| 49 | static LIST_INITIALIZE(sroute_list);
|
---|
| 50 | static sysarg_t sroute_id = 0;
|
---|
| 51 |
|
---|
| 52 | inet_sroute_t *inet_sroute_new(void)
|
---|
| 53 | {
|
---|
| 54 | inet_sroute_t *sroute = calloc(1, sizeof(inet_sroute_t));
|
---|
| 55 |
|
---|
| 56 | if (sroute == NULL) {
|
---|
[a1a101d] | 57 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating static route object. "
|
---|
[8bf672d] | 58 | "Out of memory.");
|
---|
| 59 | return NULL;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | link_initialize(&sroute->sroute_list);
|
---|
| 63 | fibril_mutex_lock(&sroute_list_lock);
|
---|
| 64 | sroute->id = ++sroute_id;
|
---|
| 65 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
| 66 |
|
---|
| 67 | return sroute;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | void inet_sroute_delete(inet_sroute_t *sroute)
|
---|
| 71 | {
|
---|
| 72 | if (sroute->name != NULL)
|
---|
| 73 | free(sroute->name);
|
---|
| 74 | free(sroute);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void inet_sroute_add(inet_sroute_t *sroute)
|
---|
| 78 | {
|
---|
| 79 | fibril_mutex_lock(&sroute_list_lock);
|
---|
| 80 | list_append(&sroute->sroute_list, &sroute_list);
|
---|
| 81 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | void inet_sroute_remove(inet_sroute_t *sroute)
|
---|
| 85 | {
|
---|
| 86 | fibril_mutex_lock(&sroute_list_lock);
|
---|
| 87 | list_remove(&sroute->sroute_list);
|
---|
| 88 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[feeac0d] | 91 | /** Find static route object matching address @a addr.
|
---|
[8bf672d] | 92 | *
|
---|
| 93 | * @param addr Address
|
---|
| 94 | */
|
---|
| 95 | inet_sroute_t *inet_sroute_find(inet_addr_t *addr)
|
---|
| 96 | {
|
---|
[f023251] | 97 | ip_ver_t addr_ver = inet_addr_get(addr, NULL, NULL);
|
---|
[a2e3ee6] | 98 |
|
---|
| 99 | inet_sroute_t *best = NULL;
|
---|
| 100 | uint8_t best_bits = 0;
|
---|
| 101 |
|
---|
[8bf672d] | 102 | fibril_mutex_lock(&sroute_list_lock);
|
---|
[a2e3ee6] | 103 |
|
---|
[feeac0d] | 104 | list_foreach(sroute_list, sroute_list, inet_sroute_t, sroute) {
|
---|
[a2e3ee6] | 105 | uint8_t dest_bits;
|
---|
[f023251] | 106 | ip_ver_t dest_ver = inet_naddr_get(&sroute->dest, NULL, NULL,
|
---|
[02a09ed] | 107 | &dest_bits);
|
---|
| 108 |
|
---|
| 109 | /* Skip comparison with different address family */
|
---|
[f023251] | 110 | if (addr_ver != dest_ver)
|
---|
[a2e3ee6] | 111 | continue;
|
---|
| 112 |
|
---|
[8bf672d] | 113 | /* Look for the most specific route */
|
---|
[a2e3ee6] | 114 | if ((best != NULL) && (best_bits >= dest_bits))
|
---|
[8bf672d] | 115 | continue;
|
---|
[a2e3ee6] | 116 |
|
---|
[02a09ed] | 117 | if (inet_naddr_compare_mask(&sroute->dest, addr)) {
|
---|
[a2e3ee6] | 118 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_sroute_find: found candidate %p",
|
---|
[8bf672d] | 119 | sroute);
|
---|
[a2e3ee6] | 120 |
|
---|
| 121 | best = sroute;
|
---|
| 122 | best_bits = dest_bits;
|
---|
[8bf672d] | 123 | }
|
---|
| 124 | }
|
---|
[a2e3ee6] | 125 |
|
---|
| 126 | if (best == NULL)
|
---|
| 127 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_sroute_find: Not found");
|
---|
| 128 |
|
---|
[8bf672d] | 129 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
[a2e3ee6] | 130 |
|
---|
| 131 | return best;
|
---|
[8bf672d] | 132 | }
|
---|
| 133 |
|
---|
| 134 | /** Find static route with a specific name.
|
---|
| 135 | *
|
---|
| 136 | * @param name Address object name
|
---|
| 137 | * @return Address object
|
---|
| 138 | */
|
---|
| 139 | inet_sroute_t *inet_sroute_find_by_name(const char *name)
|
---|
| 140 | {
|
---|
[a1a101d] | 141 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_sroute_find_by_name('%s')",
|
---|
[8bf672d] | 142 | name);
|
---|
| 143 |
|
---|
| 144 | fibril_mutex_lock(&sroute_list_lock);
|
---|
| 145 |
|
---|
[feeac0d] | 146 | list_foreach(sroute_list, sroute_list, inet_sroute_t, sroute) {
|
---|
[8bf672d] | 147 | if (str_cmp(sroute->name, name) == 0) {
|
---|
| 148 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
[a1a101d] | 149 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_sroute_find_by_name: found %p",
|
---|
[8bf672d] | 150 | sroute);
|
---|
| 151 | return sroute;
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[a1a101d] | 155 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_sroute_find_by_name: Not found");
|
---|
[8bf672d] | 156 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
| 157 |
|
---|
| 158 | return NULL;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /** Find static route with the given ID.
|
---|
| 162 | *
|
---|
| 163 | * @param id Address object ID
|
---|
| 164 | * @return Address object
|
---|
| 165 | */
|
---|
| 166 | inet_sroute_t *inet_sroute_get_by_id(sysarg_t id)
|
---|
| 167 | {
|
---|
[a1a101d] | 168 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_sroute_get_by_id(%zu)", (size_t)id);
|
---|
[8bf672d] | 169 |
|
---|
| 170 | fibril_mutex_lock(&sroute_list_lock);
|
---|
| 171 |
|
---|
[feeac0d] | 172 | list_foreach(sroute_list, sroute_list, inet_sroute_t, sroute) {
|
---|
[8bf672d] | 173 | if (sroute->id == id) {
|
---|
| 174 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
| 175 | return sroute;
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
| 180 |
|
---|
| 181 | return NULL;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | /** Get IDs of all static routes. */
|
---|
| 185 | int inet_sroute_get_id_list(sysarg_t **rid_list, size_t *rcount)
|
---|
| 186 | {
|
---|
| 187 | sysarg_t *id_list;
|
---|
| 188 | size_t count, i;
|
---|
| 189 |
|
---|
| 190 | fibril_mutex_lock(&sroute_list_lock);
|
---|
| 191 | count = list_count(&sroute_list);
|
---|
| 192 |
|
---|
| 193 | id_list = calloc(count, sizeof(sysarg_t));
|
---|
| 194 | if (id_list == NULL) {
|
---|
| 195 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
| 196 | return ENOMEM;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | i = 0;
|
---|
[feeac0d] | 200 | list_foreach(sroute_list, sroute_list, inet_sroute_t, sroute) {
|
---|
[8bf672d] | 201 | id_list[i++] = sroute->id;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
| 205 |
|
---|
| 206 | *rid_list = id_list;
|
---|
| 207 | *rcount = count;
|
---|
| 208 |
|
---|
| 209 | return EOK;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | /** @}
|
---|
| 213 | */
|
---|