| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Radim Vansa
|
|---|
| 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 | /**
|
|---|
| 30 | * @addtogroup libnic
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | * @brief Wake-on-LAN support
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include "nic_wol_virtues.h"
|
|---|
| 39 | #include "nic.h"
|
|---|
| 40 | #include <assert.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | /*
|
|---|
| 45 | * Hash table helper functions
|
|---|
| 46 | */
|
|---|
| 47 |
|
|---|
| 48 | static size_t nic_wv_key_hash(void *key)
|
|---|
| 49 | {
|
|---|
| 50 | return *(nic_wv_id_t*) key;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | static size_t nic_wv_hash(const ht_link_t *item)
|
|---|
| 54 | {
|
|---|
| 55 | nic_wol_virtue_t *virtue = (nic_wol_virtue_t *) item;
|
|---|
| 56 | return virtue->id;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | static bool nic_wv_key_equal(void *key, const ht_link_t *item)
|
|---|
| 60 | {
|
|---|
| 61 | nic_wol_virtue_t *virtue = (nic_wol_virtue_t *) item;
|
|---|
| 62 | return (virtue->id == *(nic_wv_id_t*) key);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Initializes the WOL virtues structure
|
|---|
| 67 | *
|
|---|
| 68 | * @param wvs
|
|---|
| 69 | *
|
|---|
| 70 | * @return EOK On success
|
|---|
| 71 | * @return ENOMEM On not enough memory
|
|---|
| 72 | */
|
|---|
| 73 | int nic_wol_virtues_init(nic_wol_virtues_t *wvs)
|
|---|
| 74 | {
|
|---|
| 75 | memset(wvs, 0, sizeof(nic_wol_virtues_t));
|
|---|
| 76 | wvs->table_operations.hash = nic_wv_hash;
|
|---|
| 77 | wvs->table_operations.key_hash = nic_wv_key_hash;
|
|---|
| 78 | wvs->table_operations.key_equal = nic_wv_key_equal;
|
|---|
| 79 | wvs->table_operations.equal = 0;
|
|---|
| 80 | wvs->table_operations.remove_callback = 0;
|
|---|
| 81 |
|
|---|
| 82 | if (!hash_table_create(&wvs->table, 0, 0, &wvs->table_operations)) {
|
|---|
| 83 | return ENOMEM;
|
|---|
| 84 | }
|
|---|
| 85 | size_t i;
|
|---|
| 86 | for (i = 0; i < NIC_WV_MAX; ++i) {
|
|---|
| 87 | wvs->caps_max[i] = -1;
|
|---|
| 88 | }
|
|---|
| 89 | wvs->next_id = 0;
|
|---|
| 90 | return EOK;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * Reinitializes the structure, destroying all virtues. The next_id is not
|
|---|
| 95 | * changed (some apps could still hold the filter IDs).
|
|---|
| 96 | *
|
|---|
| 97 | * @param wvs
|
|---|
| 98 | */
|
|---|
| 99 | void nic_wol_virtues_clear(nic_wol_virtues_t *wvs)
|
|---|
| 100 | {
|
|---|
| 101 | hash_table_clear(&wvs->table);
|
|---|
| 102 | nic_wv_type_t type;
|
|---|
| 103 | for (type = NIC_WV_NONE; type < NIC_WV_MAX; ++type) {
|
|---|
| 104 | nic_wol_virtue_t *virtue = wvs->lists[type];
|
|---|
| 105 | while (virtue != NULL) {
|
|---|
| 106 | nic_wol_virtue_t *next = virtue->next;
|
|---|
| 107 | free(virtue->data);
|
|---|
| 108 | free(virtue);
|
|---|
| 109 | virtue = next;
|
|---|
| 110 | }
|
|---|
| 111 | wvs->lists_sizes[type] = 0;
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * Verifies that the arguments for the WOL virtues are correct.
|
|---|
| 117 | *
|
|---|
| 118 | * @param type Type of the virtue
|
|---|
| 119 | * @param data Data argument for the virtue
|
|---|
| 120 | * @param length Length of the data
|
|---|
| 121 | *
|
|---|
| 122 | * @return EOK The arguments are correct
|
|---|
| 123 | * @return EINVAL The arguments are incorrect
|
|---|
| 124 | * @return ENOTSUP This type is unknown
|
|---|
| 125 | */
|
|---|
| 126 | int nic_wol_virtues_verify(nic_wv_type_t type, const void *data, size_t length)
|
|---|
| 127 | {
|
|---|
| 128 | switch (type) {
|
|---|
| 129 | case NIC_WV_ARP_REQUEST:
|
|---|
| 130 | case NIC_WV_BROADCAST:
|
|---|
| 131 | case NIC_WV_LINK_CHANGE:
|
|---|
| 132 | return EOK;
|
|---|
| 133 | case NIC_WV_DESTINATION:
|
|---|
| 134 | return length == sizeof (nic_address_t) ? EOK : EINVAL;
|
|---|
| 135 | case NIC_WV_DIRECTED_IPV4:
|
|---|
| 136 | return length == sizeof (nic_wv_ipv4_data_t) ? EOK : EINVAL;
|
|---|
| 137 | case NIC_WV_DIRECTED_IPV6:
|
|---|
| 138 | return length == sizeof (nic_wv_ipv6_data_t) ? EOK : EINVAL;
|
|---|
| 139 | case NIC_WV_FULL_MATCH:
|
|---|
| 140 | return length % 2 == 0 ? EOK : EINVAL;
|
|---|
| 141 | case NIC_WV_MAGIC_PACKET:
|
|---|
| 142 | return data == NULL || length == sizeof (nic_wv_magic_packet_data_t) ?
|
|---|
| 143 | EOK : EINVAL;
|
|---|
| 144 | default:
|
|---|
| 145 | return ENOTSUP;
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * Adds the virtue to the list of known virtues, activating it.
|
|---|
| 151 | *
|
|---|
| 152 | * @param wvs
|
|---|
| 153 | * @param virtue The virtue structure
|
|---|
| 154 | *
|
|---|
| 155 | * @return EOK On success
|
|---|
| 156 | * @return ENOTSUP If the virtue type is not supported
|
|---|
| 157 | * @return EINVAL If the virtue type is a single-filter and there's already
|
|---|
| 158 | * a virtue of this type defined, or there is something wrong
|
|---|
| 159 | * with the data
|
|---|
| 160 | * @return ENOMEM Not enough memory to activate the virtue
|
|---|
| 161 | */
|
|---|
| 162 | int nic_wol_virtues_add(nic_wol_virtues_t *wvs, nic_wol_virtue_t *virtue)
|
|---|
| 163 | {
|
|---|
| 164 | if (!nic_wv_is_multi(virtue->type) &&
|
|---|
| 165 | wvs->lists[virtue->type] != NULL) {
|
|---|
| 166 | return EINVAL;
|
|---|
| 167 | }
|
|---|
| 168 | do {
|
|---|
| 169 | virtue->id = wvs->next_id++;
|
|---|
| 170 | } while (NULL != hash_table_find(&wvs->table, &virtue->id));
|
|---|
| 171 | hash_table_insert(&wvs->table, &virtue->item);
|
|---|
| 172 | virtue->next = wvs->lists[virtue->type];
|
|---|
| 173 | wvs->lists[virtue->type] = virtue;
|
|---|
| 174 | wvs->lists_sizes[virtue->type]++;
|
|---|
| 175 | return EOK;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | /**
|
|---|
| 179 | * Removes the virtue from the list of virtues, but NOT deallocating the
|
|---|
| 180 | * nic_wol_virtue structure.
|
|---|
| 181 | *
|
|---|
| 182 | * @param wvs
|
|---|
| 183 | * @param id Identifier of the removed virtue
|
|---|
| 184 | *
|
|---|
| 185 | * @return Removed virtue structure or NULL if not found.
|
|---|
| 186 | */
|
|---|
| 187 | nic_wol_virtue_t *nic_wol_virtues_remove(nic_wol_virtues_t *wvs, nic_wv_id_t id)
|
|---|
| 188 | {
|
|---|
| 189 | nic_wol_virtue_t *virtue =
|
|---|
| 190 | (nic_wol_virtue_t *) hash_table_find(&wvs->table, &id);
|
|---|
| 191 | if (virtue == NULL) {
|
|---|
| 192 | return NULL;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /* Remove from filter_table */
|
|---|
| 196 | hash_table_remove_item(&wvs->table, &virtue->item);
|
|---|
| 197 |
|
|---|
| 198 | /* Remove from filter_types */
|
|---|
| 199 | assert(wvs->lists[virtue->type] != NULL);
|
|---|
| 200 | if (wvs->lists[virtue->type] == virtue) {
|
|---|
| 201 | wvs->lists[virtue->type] = virtue->next;
|
|---|
| 202 | } else {
|
|---|
| 203 | nic_wol_virtue_t *wv = wvs->lists[virtue->type];
|
|---|
| 204 | while (wv->next != virtue) {
|
|---|
| 205 | wv = wv->next;
|
|---|
| 206 | assert(wv != NULL);
|
|---|
| 207 | }
|
|---|
| 208 | wv->next = virtue->next;
|
|---|
| 209 | }
|
|---|
| 210 | wvs->lists_sizes[virtue->type]--;
|
|---|
| 211 |
|
|---|
| 212 | virtue->next = NULL;
|
|---|
| 213 | return virtue;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 | /**
|
|---|
| 218 | * Searches the filters table for a filter with specified ID
|
|---|
| 219 | *
|
|---|
| 220 | * @param wvs
|
|---|
| 221 | * @param id Identifier of the searched virtue
|
|---|
| 222 | *
|
|---|
| 223 | * @return Requested filter or NULL if not found.
|
|---|
| 224 | */
|
|---|
| 225 | const nic_wol_virtue_t *nic_wol_virtues_find(const nic_wol_virtues_t *wvs,
|
|---|
| 226 | nic_wv_id_t id)
|
|---|
| 227 | {
|
|---|
| 228 | /*
|
|---|
| 229 | * The hash_table_find cannot be const, because it would require the
|
|---|
| 230 | * returned link to be const as well. But in this case, when we're returning
|
|---|
| 231 | * constant virtue the retyping is correct.
|
|---|
| 232 | */
|
|---|
| 233 | ht_link_t *virtue = hash_table_find(&((nic_wol_virtues_t *) wvs)->table, &id);
|
|---|
| 234 | return (const nic_wol_virtue_t *) virtue;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | /**
|
|---|
| 238 | * Fill identifiers of current wol virtues of the specified type into the list.
|
|---|
| 239 | * If the type is set to NIC_WV_NONE, all virtues are used.
|
|---|
| 240 | *
|
|---|
| 241 | * @param wvs
|
|---|
| 242 | * @param[in] type Type of the virtues or NIC_WV_NONE
|
|---|
| 243 | * @param[out] id_list The new vector of filter IDs. Can be NULL.
|
|---|
| 244 | * @param[out] count Number of IDs in the filter_list. Can be NULL.
|
|---|
| 245 | *
|
|---|
| 246 | * @return EOK If it completes successfully
|
|---|
| 247 | * @return EINVAL If the filter type is invalid
|
|---|
| 248 | */
|
|---|
| 249 | int nic_wol_virtues_list(const nic_wol_virtues_t *wvs, nic_wv_type_t type,
|
|---|
| 250 | size_t max_count, nic_wv_id_t *id_list, size_t *id_count)
|
|---|
| 251 | {
|
|---|
| 252 | size_t count = 0;
|
|---|
| 253 | if (type == NIC_WV_NONE) {
|
|---|
| 254 | size_t i;
|
|---|
| 255 | for (i = NIC_WV_NONE; i < NIC_WV_MAX; ++i) {
|
|---|
| 256 | if (id_list != NULL) {
|
|---|
| 257 | nic_wol_virtue_t *virtue = wvs->lists[i];
|
|---|
| 258 | while (virtue != NULL) {
|
|---|
| 259 | if (count < max_count) {
|
|---|
| 260 | id_list[count] = virtue->id;
|
|---|
| 261 | }
|
|---|
| 262 | ++count;
|
|---|
| 263 | virtue = virtue->next;
|
|---|
| 264 | }
|
|---|
| 265 | } else {
|
|---|
| 266 | count += wvs->lists_sizes[i];
|
|---|
| 267 | }
|
|---|
| 268 | }
|
|---|
| 269 | } else if (type >= NIC_WV_MAX) {
|
|---|
| 270 | return EINVAL;
|
|---|
| 271 | } else {
|
|---|
| 272 | if (id_list != NULL) {
|
|---|
| 273 | nic_wol_virtue_t *virtue = wvs->lists[type];
|
|---|
| 274 | while (virtue != NULL) {
|
|---|
| 275 | if (count < max_count) {
|
|---|
| 276 | id_list[count] = virtue->id;
|
|---|
| 277 | }
|
|---|
| 278 | ++count;
|
|---|
| 279 | virtue = virtue->next;
|
|---|
| 280 | }
|
|---|
| 281 | } else {
|
|---|
| 282 | count = wvs->lists_sizes[type];
|
|---|
| 283 | }
|
|---|
| 284 | }
|
|---|
| 285 | if (id_count != NULL) {
|
|---|
| 286 | *id_count = count;
|
|---|
| 287 | }
|
|---|
| 288 | return EOK;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | /** @}
|
|---|
| 292 | */
|
|---|