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