| 1 | |
| 2 | |
| 3 | * net/core/dev_addr_lists.c - Functions for handling net device lists
|
| 4 | * Copyright (c) 2010 Jiri Pirko <[email protected]>
|
| 5 | *
|
| 6 | * This file contains functions for working with unicast, multicast and device
|
| 7 | * addresses lists.
|
| 8 |
|
| 9 |
|
| 10 | #include <linux/netdevice.h>
|
| 11 | #include <linux/rtnetlink.h>
|
| 12 | #include <linux/export.h>
|
| 13 | #include <linux/list.h>
|
| 14 | #include <linux/spinlock.h>
|
| 15 | #include <kunit/visibility.h>
|
| 16 |
|
| 17 | #include "dev.h"
|
| 18 |
|
| 19 | |
| 20 | * General list handling functions
|
| 21 |
|
| 22 |
|
| 23 | static int __hw_addr_insert(struct netdev_hw_addr_list *list,
|
| 24 | struct netdev_hw_addr *new, int addr_len)
|
| 25 | {
|
| 26 | struct rb_node **ins_point = &list->tree.rb_node, *parent = NULL;
|
| 27 | struct netdev_hw_addr *ha;
|
| 28 |
|
| 29 | while (*ins_point) {
|
| 30 | int diff;
|
| 31 |
|
| 32 | ha = rb_entry(*ins_point, struct netdev_hw_addr, node);
|
| 33 | diff = memcmp(new->addr, ha->addr, addr_len);
|
| 34 | if (diff == 0)
|
| 35 | diff = memcmp(&new->type, &ha->type, sizeof(new->type));
|
| 36 |
|
| 37 | parent = *ins_point;
|
| 38 | if (diff < 0)
|
| 39 | ins_point = &parent->rb_left;
|
| 40 | else if (diff > 0)
|
| 41 | ins_point = &parent->rb_right;
|
| 42 | else
|
| 43 | return -EEXIST;
|
| 44 | }
|
| 45 |
|
| 46 | rb_link_node_rcu(&new->node, parent, ins_point);
|
| 47 | rb_insert_color(&new->node, &list->tree);
|
| 48 |
|
| 49 | return 0;
|
| 50 | }
|
| 51 |
|
| 52 | static struct netdev_hw_addr*
|
| 53 | __hw_addr_create(const unsigned char *addr, int addr_len,
|
| 54 | unsigned char addr_type, bool global, bool sync)
|
| 55 | {
|
| 56 | struct netdev_hw_addr *ha;
|
| 57 | int alloc_size;
|
| 58 |
|
| 59 | alloc_size = sizeof(*ha);
|
| 60 | if (alloc_size < L1_CACHE_BYTES)
|
| 61 | alloc_size = L1_CACHE_BYTES;
|
| 62 | ha = kmalloc(alloc_size, GFP_ATOMIC);
|
| 63 | if (!ha)
|
| 64 | return NULL;
|
| 65 | memcpy(ha->addr, addr, addr_len);
|
| 66 | ha->type = addr_type;
|
| 67 | ha->refcount = 1;
|
| 68 | ha->global_use = global;
|
| 69 | ha->synced = sync ? 1 : 0;
|
| 70 | ha->sync_cnt = 0;
|
| 71 |
|
| 72 | return ha;
|
| 73 | }
|
| 74 |
|
| 75 | static int __hw_addr_add_ex(struct netdev_hw_addr_list *list,
|
| 76 | const unsigned char *addr, int addr_len,
|
| 77 | unsigned char addr_type, bool global, bool sync,
|
| 78 | int sync_count, bool exclusive)
|
| 79 | {
|
| 80 | struct rb_node **ins_point = &list->tree.rb_node, *parent = NULL;
|
| 81 | struct netdev_hw_addr *ha;
|
| 82 |
|
| 83 | if (addr_len > MAX_ADDR_LEN)
|
| 84 | return -EINVAL;
|
| 85 |
|
| 86 | while (*ins_point) {
|
| 87 | int diff;
|
| 88 |
|
| 89 | ha = rb_entry(*ins_point, struct netdev_hw_addr, node);
|
| 90 | diff = memcmp(addr, ha->addr, addr_len);
|
| 91 | if (diff == 0)
|
| 92 | diff = memcmp(&addr_type, &ha->type, sizeof(addr_type));
|
| 93 |
|
| 94 | parent = *ins_point;
|
| 95 | if (diff < 0) {
|
| 96 | ins_point = &parent->rb_left;
|
| 97 | } else if (diff > 0) {
|
| 98 | ins_point = &parent->rb_right;
|
| 99 | } else {
|
| 100 | if (exclusive)
|
| 101 | return -EEXIST;
|
| 102 | if (global) {
|
| 103 |
|
| 104 | if (ha->global_use)
|
| 105 | return 0;
|
| 106 | else
|
| 107 | ha->global_use = true;
|
| 108 | }
|
| 109 | if (sync) {
|
| 110 | if (ha->synced && sync_count)
|
| 111 | return -EEXIST;
|
| 112 | else
|
| 113 | ha->synced++;
|
| 114 | }
|
| 115 | ha->refcount++;
|
| 116 | return 0;
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | ha = __hw_addr_create(addr, addr_len, addr_type, global, sync);
|
| 121 | if (!ha)
|
| 122 | return -ENOMEM;
|
| 123 |
|
| 124 | rb_link_node(&ha->node, parent, ins_point);
|
| 125 | rb_insert_color(&ha->node, &list->tree);
|
| 126 |
|
| 127 | list_add_tail_rcu(&ha->list, &list->list);
|
| 128 | list->count++;
|
| 129 |
|
| 130 | return 0;
|
| 131 | }
|
| 132 |
|
| 133 | static int __hw_addr_add(struct netdev_hw_addr_list *list,
|
| 134 | const unsigned char *addr, int addr_len,
|
| 135 | unsigned char addr_type)
|
| 136 | {
|
| 137 | return __hw_addr_add_ex(list, addr, addr_len, addr_type, false, false,
|
| 138 | 0, false);
|
| 139 | }
|
| 140 |
|
| 141 | static int __hw_addr_del_entry(struct netdev_hw_addr_list *list,
|
| 142 | struct netdev_hw_addr *ha, bool global,
|
| 143 | bool sync)
|
| 144 | {
|
| 145 | if (global && !ha->global_use)
|
| 146 | return -ENOENT;
|
| 147 |
|
| 148 | if (sync && !ha->synced)
|
| 149 | return -ENOENT;
|
| 150 |
|
| 151 | if (global)
|
| 152 | ha->global_use = false;
|
| 153 |
|
| 154 | if (sync)
|
| 155 | ha->synced--;
|
| 156 |
|
| 157 | if (--ha->refcount)
|
| 158 | return 0;
|
| 159 |
|
| 160 | rb_erase(&ha->node, &list->tree);
|
| 161 |
|
| 162 | list_del_rcu(&ha->list);
|
| 163 | kfree_rcu(ha, rcu_head);
|
| 164 | list->count--;
|
| 165 | return 0;
|
| 166 | }
|
| 167 |
|
| 168 | static struct netdev_hw_addr *__hw_addr_lookup(struct netdev_hw_addr_list *list,
|
| 169 | const unsigned char *addr, int addr_len,
|
| 170 | unsigned char addr_type)
|
| 171 | {
|
| 172 | struct rb_node *node;
|
| 173 |
|
| 174 | node = list->tree.rb_node;
|
| 175 |
|
| 176 | while (node) {
|
| 177 | struct netdev_hw_addr *ha = rb_entry(node, struct netdev_hw_addr, node);
|
| 178 | int diff = memcmp(addr, ha->addr, addr_len);
|
| 179 |
|
| 180 | if (diff == 0 && addr_type)
|
| 181 | diff = memcmp(&addr_type, &ha->type, sizeof(addr_type));
|
| 182 |
|
| 183 | if (diff < 0)
|
| 184 | node = node->rb_left;
|
| 185 | else if (diff > 0)
|
| 186 | node = node->rb_right;
|
| 187 | else
|
| 188 | return ha;
|
| 189 | }
|
| 190 |
|
| 191 | return NULL;
|
| 192 | }
|
| 193 |
|
| 194 | static int __hw_addr_del_ex(struct netdev_hw_addr_list *list,
|
| 195 | const unsigned char *addr, int addr_len,
|
| 196 | unsigned char addr_type, bool global, bool sync)
|
| 197 | {
|
| 198 | struct netdev_hw_addr *ha = __hw_addr_lookup(list, addr, addr_len, addr_type);
|
| 199 |
|
| 200 | if (!ha)
|
| 201 | return -ENOENT;
|
| 202 | return __hw_addr_del_entry(list, ha, global, sync);
|
| 203 | }
|
| 204 |
|
| 205 | static int __hw_addr_del(struct netdev_hw_addr_list *list,
|
| 206 | const unsigned char *addr, int addr_len,
|
| 207 | unsigned char addr_type)
|
| 208 | {
|
| 209 | return __hw_addr_del_ex(list, addr, addr_len, addr_type, false, false);
|
| 210 | }
|
| 211 |
|
| 212 | static int __hw_addr_sync_one(struct netdev_hw_addr_list *to_list,
|
| 213 | struct netdev_hw_addr *ha,
|
| 214 | int addr_len)
|
| 215 | {
|
| 216 | int err;
|
| 217 |
|
| 218 | err = __hw_addr_add_ex(to_list, ha->addr, addr_len, ha->type,
|
| 219 | false, true, ha->sync_cnt, false);
|
| 220 | if (err && err != -EEXIST)
|
| 221 | return err;
|
| 222 |
|
| 223 | if (!err) {
|
| 224 | ha->sync_cnt++;
|
| 225 | ha->refcount++;
|
| 226 | }
|
| 227 |
|
| 228 | return 0;
|
| 229 | }
|
| 230 |
|
| 231 | static void __hw_addr_unsync_one(struct netdev_hw_addr_list *to_list,
|
| 232 | struct netdev_hw_addr_list *from_list,
|
| 233 | struct netdev_hw_addr *ha,
|
| 234 | int addr_len)
|
| 235 | {
|
| 236 | int err;
|
| 237 |
|
| 238 | err = __hw_addr_del_ex(to_list, ha->addr, addr_len, ha->type,
|
| 239 | false, true);
|
| 240 | if (err)
|
| 241 | return;
|
| 242 | ha->sync_cnt--;
|
| 243 |
|
| 244 | __hw_addr_del_entry(from_list, ha, false, false);
|
| 245 | }
|
| 246 |
|
| 247 | int __hw_addr_sync_multiple(struct netdev_hw_addr_list *to_list,
|
| 248 | struct netdev_hw_addr_list *from_list,
|
| 249 | int addr_len)
|
| 250 | {
|
| 251 | int err = 0;
|
| 252 | struct netdev_hw_addr *ha, *tmp;
|
| 253 |
|
| 254 | list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
|
| 255 | if (ha->sync_cnt == ha->refcount) {
|
| 256 | __hw_addr_unsync_one(to_list, from_list, ha, addr_len);
|
| 257 | } else {
|
| 258 | err = __hw_addr_sync_one(to_list, ha, addr_len);
|
| 259 | if (err)
|
| 260 | break;
|
| 261 | }
|
| 262 | }
|
| 263 | return err;
|
| 264 | }
|
| 265 | EXPORT_SYMBOL(__hw_addr_sync_multiple);
|
| 266 |
|
| 267 | |
| 268 | * between source and destination of they synch. If you ever need to
|
| 269 | * sync addresses to more then 1 destination, you need to use
|
| 270 | * __hw_addr_sync_multiple().
|
| 271 |
|
| 272 | int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
|
| 273 | struct netdev_hw_addr_list *from_list,
|
| 274 | int addr_len)
|
| 275 | {
|
| 276 | int err = 0;
|
| 277 | struct netdev_hw_addr *ha, *tmp;
|
| 278 |
|
| 279 | list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
|
| 280 | if (!ha->sync_cnt) {
|
| 281 | err = __hw_addr_sync_one(to_list, ha, addr_len);
|
| 282 | if (err)
|
| 283 | break;
|
| 284 | } else if (ha->refcount == 1)
|
| 285 | __hw_addr_unsync_one(to_list, from_list, ha, addr_len);
|
| 286 | }
|
| 287 | return err;
|
| 288 | }
|
| 289 | EXPORT_SYMBOL(__hw_addr_sync);
|
| 290 |
|
| 291 | void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
|
| 292 | struct netdev_hw_addr_list *from_list,
|
| 293 | int addr_len)
|
| 294 | {
|
| 295 | struct netdev_hw_addr *ha, *tmp;
|
| 296 |
|
| 297 | list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
|
| 298 | if (ha->sync_cnt)
|
| 299 | __hw_addr_unsync_one(to_list, from_list, ha, addr_len);
|
| 300 | }
|
| 301 | }
|
| 302 | EXPORT_SYMBOL(__hw_addr_unsync);
|
| 303 |
|
| 304 | |
| 305 | * __hw_addr_sync_dev - Synchronize device's multicast list
|
| 306 | * @list: address list to synchronize
|
| 307 | * @dev: device to sync
|
| 308 | * @sync: function to call if address should be added
|
| 309 | * @unsync: function to call if address should be removed
|
| 310 | *
|
| 311 | * This function is intended to be called from the ndo_set_rx_mode
|
| 312 | * function of devices that require explicit address add/remove
|
| 313 | * notifications. The unsync function may be NULL in which case
|
| 314 | * the addresses requiring removal will simply be removed without
|
| 315 | * any notification to the device.
|
| 316 | *
|
| 317 | int __hw_addr_sync_dev(struct netdev_hw_addr_list *list,
|
| 318 | struct net_device *dev,
|
| 319 | int (*sync)(struct net_device *, const unsigned char *),
|
| 320 | int (*unsync)(struct net_device *,
|
| 321 | const unsigned char *))
|
| 322 | {
|
| 323 | struct netdev_hw_addr *ha, *tmp;
|
| 324 | int err;
|
| 325 |
|
| 326 |
|
| 327 | list_for_each_entry_safe(ha, tmp, &list->list, list) {
|
| 328 | if (!ha->sync_cnt || ha->refcount != 1)
|
| 329 | continue;
|
| 330 |
|
| 331 |
|
| 332 | if (unsync && unsync(dev, ha->addr))
|
| 333 | continue;
|
| 334 |
|
| 335 | ha->sync_cnt--;
|
| 336 | __hw_addr_del_entry(list, ha, false, false);
|
| 337 | }
|
| 338 |
|
| 339 |
|
| 340 | list_for_each_entry_safe(ha, tmp, &list->list, list) {
|
| 341 | if (ha->sync_cnt)
|
| 342 | continue;
|
| 343 |
|
| 344 | err = sync(dev, ha->addr);
|
| 345 | if (err)
|
| 346 | return err;
|
| 347 |
|
| 348 | ha->sync_cnt++;
|
| 349 | ha->refcount++;
|
| 350 | }
|
| 351 |
|
| 352 | return 0;
|
| 353 | }
|
| 354 | EXPORT_SYMBOL(__hw_addr_sync_dev);
|
| 355 |
|
| 356 | |
| 357 | * __hw_addr_ref_sync_dev - Synchronize device's multicast address list taking
|
| 358 | * into account references
|
| 359 | * @list: address list to synchronize
|
| 360 | * @dev: device to sync
|
| 361 | * @sync: function to call if address or reference on it should be added
|
| 362 | * @unsync: function to call if address or some reference on it should removed
|
| 363 | *
|
| 364 | * This function is intended to be called from the ndo_set_rx_mode
|
| 365 | * function of devices that require explicit address or references on it
|
| 366 | * add/remove notifications. The unsync function may be NULL in which case
|
| 367 | * the addresses or references on it requiring removal will simply be
|
| 368 | * removed without any notification to the device. That is responsibility of
|
| 369 | * the driver to identify and distribute address or references on it between
|
| 370 | * internal address tables.
|
| 371 | *
|
| 372 | int __hw_addr_ref_sync_dev(struct netdev_hw_addr_list *list,
|
| 373 | struct net_device *dev,
|
| 374 | int (*sync)(struct net_device *,
|
| 375 | const unsigned char *, int),
|
| 376 | int (*unsync)(struct net_device *,
|
| 377 | const unsigned char *, int))
|
| 378 | {
|
| 379 | struct netdev_hw_addr *ha, *tmp;
|
| 380 | int err, ref_cnt;
|
| 381 |
|
| 382 |
|
| 383 | list_for_each_entry_safe(ha, tmp, &list->list, list) {
|
| 384 |
|
| 385 | if ((ha->sync_cnt << 1) <= ha->refcount)
|
| 386 | continue;
|
| 387 |
|
| 388 |
|
| 389 | ref_cnt = ha->refcount - ha->sync_cnt;
|
| 390 | if (unsync && unsync(dev, ha->addr, ref_cnt))
|
| 391 | continue;
|
| 392 |
|
| 393 | ha->refcount = (ref_cnt << 1) + 1;
|
| 394 | ha->sync_cnt = ref_cnt;
|
| 395 | __hw_addr_del_entry(list, ha, false, false);
|
| 396 | }
|
| 397 |
|
| 398 |
|
| 399 | list_for_each_entry_safe(ha, tmp, &list->list, list) {
|
| 400 |
|
| 401 | if ((ha->sync_cnt << 1) >= ha->refcount)
|
| 402 | continue;
|
| 403 |
|
| 404 | ref_cnt = ha->refcount - ha->sync_cnt;
|
| 405 | err = sync(dev, ha->addr, ref_cnt);
|
| 406 | if (err)
|
| 407 | return err;
|
| 408 |
|
| 409 | ha->refcount = ref_cnt << 1;
|
| 410 | ha->sync_cnt = ref_cnt;
|
| 411 | }
|
| 412 |
|
| 413 | return 0;
|
| 414 | }
|
| 415 | EXPORT_SYMBOL(__hw_addr_ref_sync_dev);
|
| 416 |
|
| 417 | |
| 418 | * __hw_addr_ref_unsync_dev - Remove synchronized addresses and references on
|
| 419 | * it from device
|
| 420 | * @list: address list to remove synchronized addresses (references on it) from
|
| 421 | * @dev: device to sync
|
| 422 | * @unsync: function to call if address and references on it should be removed
|
| 423 | *
|
| 424 | * Remove all addresses that were added to the device by
|
| 425 | * __hw_addr_ref_sync_dev(). This function is intended to be called from the
|
| 426 | * ndo_stop or ndo_open functions on devices that require explicit address (or
|
| 427 | * references on it) add/remove notifications. If the unsync function pointer
|
| 428 | * is NULL then this function can be used to just reset the sync_cnt for the
|
| 429 | * addresses in the list.
|
| 430 | *
|
| 431 | void __hw_addr_ref_unsync_dev(struct netdev_hw_addr_list *list,
|
| 432 | struct net_device *dev,
|
| 433 | int (*unsync)(struct net_device *,
|
| 434 | const unsigned char *, int))
|
| 435 | {
|
| 436 | struct netdev_hw_addr *ha, *tmp;
|
| 437 |
|
| 438 | list_for_each_entry_safe(ha, tmp, &list->list, list) {
|
| 439 | if (!ha->sync_cnt)
|
| 440 | continue;
|
| 441 |
|
| 442 |
|
| 443 | if (unsync && unsync(dev, ha->addr, ha->sync_cnt))
|
| 444 | continue;
|
| 445 |
|
| 446 | ha->refcount -= ha->sync_cnt - 1;
|
| 447 | ha->sync_cnt = 0;
|
| 448 | __hw_addr_del_entry(list, ha, false, false);
|
| 449 | }
|
| 450 | }
|
| 451 | EXPORT_SYMBOL(__hw_addr_ref_unsync_dev);
|
| 452 |
|
| 453 | |
| 454 | * __hw_addr_unsync_dev - Remove synchronized addresses from device
|
| 455 | * @list: address list to remove synchronized addresses from
|
| 456 | * @dev: device to sync
|
| 457 | * @unsync: function to call if address should be removed
|
| 458 | *
|
| 459 | * Remove all addresses that were added to the device by __hw_addr_sync_dev().
|
| 460 | * This function is intended to be called from the ndo_stop or ndo_open
|
| 461 | * functions on devices that require explicit address add/remove
|
| 462 | * notifications. If the unsync function pointer is NULL then this function
|
| 463 | * can be used to just reset the sync_cnt for the addresses in the list.
|
| 464 | *
|
| 465 | void __hw_addr_unsync_dev(struct netdev_hw_addr_list *list,
|
| 466 | struct net_device *dev,
|
| 467 | int (*unsync)(struct net_device *,
|
| 468 | const unsigned char *))
|
| 469 | {
|
| 470 | struct netdev_hw_addr *ha, *tmp;
|
| 471 |
|
| 472 | list_for_each_entry_safe(ha, tmp, &list->list, list) {
|
| 473 | if (!ha->sync_cnt)
|
| 474 | continue;
|
| 475 |
|
| 476 |
|
| 477 | if (unsync && unsync(dev, ha->addr))
|
| 478 | continue;
|
| 479 |
|
| 480 | ha->sync_cnt--;
|
| 481 | __hw_addr_del_entry(list, ha, false, false);
|
| 482 | }
|
| 483 | }
|
| 484 | EXPORT_SYMBOL(__hw_addr_unsync_dev);
|
| 485 |
|
| 486 | void __hw_addr_flush(struct netdev_hw_addr_list *list)
|
| 487 | {
|
| 488 | struct netdev_hw_addr *ha, *tmp;
|
| 489 |
|
| 490 | list->tree = RB_ROOT;
|
| 491 | list_for_each_entry_safe(ha, tmp, &list->list, list) {
|
| 492 | list_del_rcu(&ha->list);
|
| 493 | kfree_rcu(ha, rcu_head);
|
| 494 | }
|
| 495 | list->count = 0;
|
| 496 | }
|
| 497 | EXPORT_SYMBOL_IF_KUNIT(__hw_addr_flush);
|
| 498 |
|
| 499 | void __hw_addr_init(struct netdev_hw_addr_list *list)
|
| 500 | {
|
| 501 | INIT_LIST_HEAD(&list->list);
|
| 502 | list->count = 0;
|
| 503 | list->tree = RB_ROOT;
|
| 504 | }
|
| 505 | EXPORT_SYMBOL(__hw_addr_init);
|
| 506 |
|
| 507 | static void __hw_addr_splice(struct netdev_hw_addr_list *dst,
|
| 508 | struct netdev_hw_addr_list *src)
|
| 509 | {
|
| 510 | src->tree = RB_ROOT;
|
| 511 | list_splice_init(&src->list, &dst->list);
|
| 512 | dst->count += src->count;
|
| 513 | src->count = 0;
|
| 514 | }
|
| 515 |
|
| 516 | |
| 517 | * __hw_addr_list_snapshot - create a snapshot copy of an address list
|
| 518 | * @snap: destination snapshot list (needs to be __hw_addr_init-initialized)
|
| 519 | * @list: source address list to snapshot
|
| 520 | * @addr_len: length of addresses
|
| 521 | * @cache: entry cache to reuse entries from; falls back to GFP_ATOMIC
|
| 522 | *
|
| 523 | * Creates a copy of @list reusing entries from @cache when available.
|
| 524 | * Must be called under a spinlock.
|
| 525 | *
|
| 526 | * Return: 0 on success, -errno on failure.
|
| 527 |
|
| 528 | int __hw_addr_list_snapshot(struct netdev_hw_addr_list *snap,
|
| 529 | const struct netdev_hw_addr_list *list,
|
| 530 | int addr_len, struct netdev_hw_addr_list *cache)
|
| 531 | {
|
| 532 | struct netdev_hw_addr *ha, *entry;
|
| 533 |
|
| 534 | list_for_each_entry(ha, &list->list, list) {
|
| 535 | if (cache->count) {
|
| 536 | entry = list_first_entry(&cache->list,
|
| 537 | struct netdev_hw_addr, list);
|
| 538 | list_del(&entry->list);
|
| 539 | cache->count--;
|
| 540 | memcpy(entry->addr, ha->addr, addr_len);
|
| 541 | entry->type = ha->type;
|
| 542 | entry->global_use = false;
|
| 543 | entry->synced = 0;
|
| 544 | } else {
|
| 545 | entry = __hw_addr_create(ha->addr, addr_len, ha->type,
|
| 546 | false, false);
|
| 547 | if (!entry) {
|
| 548 | __hw_addr_flush(snap);
|
| 549 | return -ENOMEM;
|
| 550 | }
|
| 551 | }
|
| 552 | entry->sync_cnt = ha->sync_cnt;
|
| 553 | entry->refcount = ha->refcount;
|
| 554 |
|
| 555 | list_add_tail(&entry->list, &snap->list);
|
| 556 | __hw_addr_insert(snap, entry, addr_len);
|
| 557 | snap->count++;
|
| 558 | }
|
| 559 |
|
| 560 | return 0;
|
| 561 | }
|
| 562 | EXPORT_SYMBOL_IF_KUNIT(__hw_addr_list_snapshot);
|
| 563 |
|
| 564 | |
| 565 | * __hw_addr_list_reconcile - sync snapshot changes back and free snapshots
|
| 566 | * @real_list: the real address list to update
|
| 567 | * @work: the working snapshot (modified by driver via __hw_addr_sync_dev)
|
| 568 | * @ref: the reference snapshot (untouched copy of original state)
|
| 569 | * @addr_len: length of addresses
|
| 570 | * @cache: entry cache to return snapshot entries to for reuse
|
| 571 | *
|
| 572 | * Walks the reference snapshot and compares each entry against the work
|
| 573 | * snapshot to compute sync_cnt deltas. Applies those deltas to @real_list.
|
| 574 | * Returns snapshot entries to @cache for reuse; frees both snapshots.
|
| 575 | * Caller must hold netif_addr_lock_bh.
|
| 576 |
|
| 577 | void __hw_addr_list_reconcile(struct netdev_hw_addr_list *real_list,
|
| 578 | struct netdev_hw_addr_list *work,
|
| 579 | struct netdev_hw_addr_list *ref, int addr_len,
|
| 580 | struct netdev_hw_addr_list *cache)
|
| 581 | {
|
| 582 | struct netdev_hw_addr *ref_ha, *tmp, *work_ha, *real_ha;
|
| 583 | int delta;
|
| 584 |
|
| 585 | list_for_each_entry_safe(ref_ha, tmp, &ref->list, list) {
|
| 586 | work_ha = __hw_addr_lookup(work, ref_ha->addr, addr_len,
|
| 587 | ref_ha->type);
|
| 588 | if (work_ha)
|
| 589 | delta = work_ha->sync_cnt - ref_ha->sync_cnt;
|
| 590 | else
|
| 591 | delta = -1;
|
| 592 |
|
| 593 | if (delta == 0)
|
| 594 | continue;
|
| 595 |
|
| 596 | real_ha = __hw_addr_lookup(real_list, ref_ha->addr, addr_len,
|
| 597 | ref_ha->type);
|
| 598 | if (!real_ha) {
|
| 599 | |
| 600 | * driver synced this addr to hardware (delta > 0),
|
| 601 | * re-insert it as a stale entry so the next work
|
| 602 | * run unsyncs it from hardware.
|
| 603 |
|
| 604 | if (delta > 0) {
|
| 605 | rb_erase(&ref_ha->node, &ref->tree);
|
| 606 | list_del(&ref_ha->list);
|
| 607 | ref->count--;
|
| 608 | ref_ha->sync_cnt = delta;
|
| 609 | ref_ha->refcount = delta;
|
| 610 | list_add_tail_rcu(&ref_ha->list,
|
| 611 | &real_list->list);
|
| 612 | __hw_addr_insert(real_list, ref_ha,
|
| 613 | addr_len);
|
| 614 | real_list->count++;
|
| 615 | }
|
| 616 | continue;
|
| 617 | }
|
| 618 |
|
| 619 | real_ha->sync_cnt += delta;
|
| 620 | real_ha->refcount += delta;
|
| 621 | if (!real_ha->refcount) {
|
| 622 | rb_erase(&real_ha->node, &real_list->tree);
|
| 623 | list_del_rcu(&real_ha->list);
|
| 624 | kfree_rcu(real_ha, rcu_head);
|
| 625 | real_list->count--;
|
| 626 | }
|
| 627 | }
|
| 628 |
|
| 629 | __hw_addr_splice(cache, work);
|
| 630 | __hw_addr_splice(cache, ref);
|
| 631 | }
|
| 632 | EXPORT_SYMBOL_IF_KUNIT(__hw_addr_list_reconcile);
|
| 633 |
|
| 634 | |
| 635 | * Device addresses handling functions
|
| 636 |
|
| 637 |
|
| 638 | |
| 639 | * break the rbtree layout. All changes should go thru dev_addr_set() and co.
|
| 640 | * Remove this check in mid-2024.
|
| 641 |
|
| 642 | void dev_addr_check(struct net_device *dev)
|
| 643 | {
|
| 644 | if (!memcmp(dev->dev_addr, dev->dev_addr_shadow, MAX_ADDR_LEN))
|
| 645 | return;
|
| 646 |
|
| 647 | netdev_warn(dev, "Current addr: %*ph\n", MAX_ADDR_LEN, dev->dev_addr);
|
| 648 | netdev_warn(dev, "Expected addr: %*ph\n",
|
| 649 | MAX_ADDR_LEN, dev->dev_addr_shadow);
|
| 650 | netdev_WARN(dev, "Incorrect netdev->dev_addr\n");
|
| 651 | }
|
| 652 |
|
| 653 | |
| 654 | * dev_addr_flush - Flush device address list
|
| 655 | * @dev: device
|
| 656 | *
|
| 657 | * Flush device address list and reset ->dev_addr.
|
| 658 | *
|
| 659 | * The caller must hold the rtnl_mutex.
|
| 660 |
|
| 661 | void dev_addr_flush(struct net_device *dev)
|
| 662 | {
|
| 663 |
|
| 664 | dev_addr_check(dev);
|
| 665 |
|
| 666 | __hw_addr_flush(&dev->dev_addrs);
|
| 667 | dev->dev_addr = NULL;
|
| 668 | }
|
| 669 |
|
| 670 | |
| 671 | * dev_addr_init - Init device address list
|
| 672 | * @dev: device
|
| 673 | *
|
| 674 | * Init device address list and create the first element,
|
| 675 | * used by ->dev_addr.
|
| 676 | *
|
| 677 | * The caller must hold the rtnl_mutex.
|
| 678 |
|
| 679 | int dev_addr_init(struct net_device *dev)
|
| 680 | {
|
| 681 | unsigned char addr[MAX_ADDR_LEN];
|
| 682 | struct netdev_hw_addr *ha;
|
| 683 | int err;
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 | __hw_addr_init(&dev->dev_addrs);
|
| 688 | memset(addr, 0, sizeof(addr));
|
| 689 | err = __hw_addr_add(&dev->dev_addrs, addr, sizeof(addr),
|
| 690 | NETDEV_HW_ADDR_T_LAN);
|
| 691 | if (!err) {
|
| 692 | |
| 693 | * Get the first (previously created) address from the list
|
| 694 | * and set dev_addr pointer to this location.
|
| 695 |
|
| 696 | ha = list_first_entry(&dev->dev_addrs.list,
|
| 697 | struct netdev_hw_addr, list);
|
| 698 | dev->dev_addr = ha->addr;
|
| 699 | }
|
| 700 | return err;
|
| 701 | }
|
| 702 |
|
| 703 | void dev_addr_mod(struct net_device *dev, unsigned int offset,
|
| 704 | const void *addr, size_t len)
|
| 705 | {
|
| 706 | struct netdev_hw_addr *ha;
|
| 707 |
|
| 708 | dev_addr_check(dev);
|
| 709 |
|
| 710 | ha = container_of(dev->dev_addr, struct netdev_hw_addr, addr[0]);
|
| 711 | rb_erase(&ha->node, &dev->dev_addrs.tree);
|
| 712 | memcpy(&ha->addr[offset], addr, len);
|
| 713 | memcpy(&dev->dev_addr_shadow[offset], addr, len);
|
| 714 | WARN_ON(__hw_addr_insert(&dev->dev_addrs, ha, dev->addr_len));
|
| 715 | }
|
| 716 | EXPORT_SYMBOL(dev_addr_mod);
|
| 717 |
|
| 718 | |
| 719 | * dev_addr_add - Add a device address
|
| 720 | * @dev: device
|
| 721 | * @addr: address to add
|
| 722 | * @addr_type: address type
|
| 723 | *
|
| 724 | * Add a device address to the device or increase the reference count if
|
| 725 | * it already exists.
|
| 726 | *
|
| 727 | * The caller must hold the rtnl_mutex.
|
| 728 |
|
| 729 | int dev_addr_add(struct net_device *dev, const unsigned char *addr,
|
| 730 | unsigned char addr_type)
|
| 731 | {
|
| 732 | int err;
|
| 733 |
|
| 734 | ASSERT_RTNL();
|
| 735 |
|
| 736 | err = netif_pre_changeaddr_notify(dev, addr, NULL);
|
| 737 | if (err)
|
| 738 | return err;
|
| 739 | err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type);
|
| 740 | if (!err)
|
| 741 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
|
| 742 | return err;
|
| 743 | }
|
| 744 | EXPORT_SYMBOL(dev_addr_add);
|
| 745 |
|
| 746 | |
| 747 | * dev_addr_del - Release a device address.
|
| 748 | * @dev: device
|
| 749 | * @addr: address to delete
|
| 750 | * @addr_type: address type
|
| 751 | *
|
| 752 | * Release reference to a device address and remove it from the device
|
| 753 | * if the reference count drops to zero.
|
| 754 | *
|
| 755 | * The caller must hold the rtnl_mutex.
|
| 756 |
|
| 757 | int dev_addr_del(struct net_device *dev, const unsigned char *addr,
|
| 758 | unsigned char addr_type)
|
| 759 | {
|
| 760 | int err;
|
| 761 | struct netdev_hw_addr *ha;
|
| 762 |
|
| 763 | ASSERT_RTNL();
|
| 764 |
|
| 765 | |
| 766 | * We can not remove the first address from the list because
|
| 767 | * dev->dev_addr points to that.
|
| 768 |
|
| 769 | ha = list_first_entry(&dev->dev_addrs.list,
|
| 770 | struct netdev_hw_addr, list);
|
| 771 | if (!memcmp(ha->addr, addr, dev->addr_len) &&
|
| 772 | ha->type == addr_type && ha->refcount == 1)
|
| 773 | return -ENOENT;
|
| 774 |
|
| 775 | err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len,
|
| 776 | addr_type);
|
| 777 | if (!err)
|
| 778 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
|
| 779 | return err;
|
| 780 | }
|
| 781 | EXPORT_SYMBOL(dev_addr_del);
|
| 782 |
|
| 783 | |
| 784 | * Unicast list handling functions
|
| 785 |
|
| 786 |
|
| 787 | |
| 788 | * dev_uc_add_excl - Add a global secondary unicast address
|
| 789 | * @dev: device
|
| 790 | * @addr: address to add
|
| 791 |
|
| 792 | int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr)
|
| 793 | {
|
| 794 | int err;
|
| 795 |
|
| 796 | netif_addr_lock_bh(dev);
|
| 797 | err = __hw_addr_add_ex(&dev->uc, addr, dev->addr_len,
|
| 798 | NETDEV_HW_ADDR_T_UNICAST, true, false,
|
| 799 | 0, true);
|
| 800 | if (!err)
|
| 801 | __dev_set_rx_mode(dev);
|
| 802 | netif_addr_unlock_bh(dev);
|
| 803 | return err;
|
| 804 | }
|
| 805 | EXPORT_SYMBOL(dev_uc_add_excl);
|
| 806 |
|
| 807 | |
| 808 | * dev_uc_add - Add a secondary unicast address
|
| 809 | * @dev: device
|
| 810 | * @addr: address to add
|
| 811 | *
|
| 812 | * Add a secondary unicast address to the device or increase
|
| 813 | * the reference count if it already exists.
|
| 814 |
|
| 815 | int dev_uc_add(struct net_device *dev, const unsigned char *addr)
|
| 816 | {
|
| 817 | int err;
|
| 818 |
|
| 819 | netif_addr_lock_bh(dev);
|
| 820 | err = __hw_addr_add(&dev->uc, addr, dev->addr_len,
|
| 821 | NETDEV_HW_ADDR_T_UNICAST);
|
| 822 | if (!err)
|
| 823 | __dev_set_rx_mode(dev);
|
| 824 | netif_addr_unlock_bh(dev);
|
| 825 | return err;
|
| 826 | }
|
| 827 | EXPORT_SYMBOL(dev_uc_add);
|
| 828 |
|
| 829 | |
| 830 | * dev_uc_del - Release secondary unicast address.
|
| 831 | * @dev: device
|
| 832 | * @addr: address to delete
|
| 833 | *
|
| 834 | * Release reference to a secondary unicast address and remove it
|
| 835 | * from the device if the reference count drops to zero.
|
| 836 |
|
| 837 | int dev_uc_del(struct net_device *dev, const unsigned char *addr)
|
| 838 | {
|
| 839 | int err;
|
| 840 |
|
| 841 | netif_addr_lock_bh(dev);
|
| 842 | err = __hw_addr_del(&dev->uc, addr, dev->addr_len,
|
| 843 | NETDEV_HW_ADDR_T_UNICAST);
|
| 844 | if (!err)
|
| 845 | __dev_set_rx_mode(dev);
|
| 846 | netif_addr_unlock_bh(dev);
|
| 847 | return err;
|
| 848 | }
|
| 849 | EXPORT_SYMBOL(dev_uc_del);
|
| 850 |
|
| 851 | |
| 852 | * dev_uc_sync - Synchronize device's unicast list to another device
|
| 853 | * @to: destination device
|
| 854 | * @from: source device
|
| 855 | *
|
| 856 | * Add newly added addresses to the destination device and release
|
| 857 | * addresses that have no users left. The source device must be
|
| 858 | * locked by netif_addr_lock_bh.
|
| 859 | *
|
| 860 | * This function is intended to be called from the dev->set_rx_mode
|
| 861 | * function of layered software devices. This function assumes that
|
| 862 | * addresses will only ever be synced to the @to devices and no other.
|
| 863 |
|
| 864 | int dev_uc_sync(struct net_device *to, struct net_device *from)
|
| 865 | {
|
| 866 | int err = 0;
|
| 867 |
|
| 868 | if (to->addr_len != from->addr_len)
|
| 869 | return -EINVAL;
|
| 870 |
|
| 871 | netif_addr_lock(to);
|
| 872 | err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
|
| 873 | if (!err)
|
| 874 | __dev_set_rx_mode(to);
|
| 875 | netif_addr_unlock(to);
|
| 876 | return err;
|
| 877 | }
|
| 878 | EXPORT_SYMBOL(dev_uc_sync);
|
| 879 |
|
| 880 | |
| 881 | * dev_uc_sync_multiple - Synchronize device's unicast list to another
|
| 882 | * device, but allow for multiple calls to sync to multiple devices.
|
| 883 | * @to: destination device
|
| 884 | * @from: source device
|
| 885 | *
|
| 886 | * Add newly added addresses to the destination device and release
|
| 887 | * addresses that have been deleted from the source. The source device
|
| 888 | * must be locked by netif_addr_lock_bh.
|
| 889 | *
|
| 890 | * This function is intended to be called from the dev->set_rx_mode
|
| 891 | * function of layered software devices. It allows for a single source
|
| 892 | * device to be synced to multiple destination devices.
|
| 893 |
|
| 894 | int dev_uc_sync_multiple(struct net_device *to, struct net_device *from)
|
| 895 | {
|
| 896 | int err = 0;
|
| 897 |
|
| 898 | if (to->addr_len != from->addr_len)
|
| 899 | return -EINVAL;
|
| 900 |
|
| 901 | netif_addr_lock(to);
|
| 902 | err = __hw_addr_sync_multiple(&to->uc, &from->uc, to->addr_len);
|
| 903 | if (!err)
|
| 904 | __dev_set_rx_mode(to);
|
| 905 | netif_addr_unlock(to);
|
| 906 | return err;
|
| 907 | }
|
| 908 | EXPORT_SYMBOL(dev_uc_sync_multiple);
|
| 909 |
|
| 910 | |
| 911 | * dev_uc_unsync - Remove synchronized addresses from the destination device
|
| 912 | * @to: destination device
|
| 913 | * @from: source device
|
| 914 | *
|
| 915 | * Remove all addresses that were added to the destination device by
|
| 916 | * dev_uc_sync(). This function is intended to be called from the
|
| 917 | * dev->stop function of layered software devices.
|
| 918 |
|
| 919 | void dev_uc_unsync(struct net_device *to, struct net_device *from)
|
| 920 | {
|
| 921 | if (to->addr_len != from->addr_len)
|
| 922 | return;
|
| 923 |
|
| 924 | |
| 925 | * reasons:
|
| 926 | * 1) This is always called without any addr_list_lock, so as the
|
| 927 | * outermost one here, it must be 0.
|
| 928 | * 2) This is called by some callers after unlinking the upper device,
|
| 929 | * so the dev->lower_level becomes 1 again.
|
| 930 | * Therefore, the subclass for 'from' is 0, for 'to' is either 1 or
|
| 931 | * larger.
|
| 932 |
|
| 933 | netif_addr_lock_bh(from);
|
| 934 | netif_addr_lock(to);
|
| 935 | __hw_addr_unsync(&to->uc, &from->uc, to->addr_len);
|
| 936 | __dev_set_rx_mode(to);
|
| 937 | netif_addr_unlock(to);
|
| 938 | netif_addr_unlock_bh(from);
|
| 939 | }
|
| 940 | EXPORT_SYMBOL(dev_uc_unsync);
|
| 941 |
|
| 942 | |
| 943 | * dev_uc_flush - Flush unicast addresses
|
| 944 | * @dev: device
|
| 945 | *
|
| 946 | * Flush unicast addresses.
|
| 947 |
|
| 948 | void dev_uc_flush(struct net_device *dev)
|
| 949 | {
|
| 950 | netif_addr_lock_bh(dev);
|
| 951 | __hw_addr_flush(&dev->uc);
|
| 952 | netif_addr_unlock_bh(dev);
|
| 953 | }
|
| 954 | EXPORT_SYMBOL(dev_uc_flush);
|
| 955 |
|
| 956 | |
| 957 | * dev_uc_init - Init unicast address list
|
| 958 | * @dev: device
|
| 959 | *
|
| 960 | * Init unicast address list.
|
| 961 |
|
| 962 | void dev_uc_init(struct net_device *dev)
|
| 963 | {
|
| 964 | __hw_addr_init(&dev->uc);
|
| 965 | }
|
| 966 | EXPORT_SYMBOL(dev_uc_init);
|
| 967 |
|
| 968 | |
| 969 | * Multicast list handling functions
|
| 970 |
|
| 971 |
|
| 972 | |
| 973 | * dev_mc_add_excl - Add a global secondary multicast address
|
| 974 | * @dev: device
|
| 975 | * @addr: address to add
|
| 976 |
|
| 977 | int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr)
|
| 978 | {
|
| 979 | int err;
|
| 980 |
|
| 981 | netif_addr_lock_bh(dev);
|
| 982 | err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
|
| 983 | NETDEV_HW_ADDR_T_MULTICAST, true, false,
|
| 984 | 0, true);
|
| 985 | if (!err)
|
| 986 | __dev_set_rx_mode(dev);
|
| 987 | netif_addr_unlock_bh(dev);
|
| 988 | return err;
|
| 989 | }
|
| 990 | EXPORT_SYMBOL(dev_mc_add_excl);
|
| 991 |
|
| 992 | static int __dev_mc_add(struct net_device *dev, const unsigned char *addr,
|
| 993 | bool global)
|
| 994 | {
|
| 995 | int err;
|
| 996 |
|
| 997 | netif_addr_lock_bh(dev);
|
| 998 | err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
|
| 999 | NETDEV_HW_ADDR_T_MULTICAST, global, false,
|
| 1000 | 0, false);
|
| 1001 | if (!err)
|
| 1002 | __dev_set_rx_mode(dev);
|
| 1003 | netif_addr_unlock_bh(dev);
|
| 1004 | return err;
|
| 1005 | }
|
| 1006 | |
| 1007 | * dev_mc_add - Add a multicast address
|
| 1008 | * @dev: device
|
| 1009 | * @addr: address to add
|
| 1010 | *
|
| 1011 | * Add a multicast address to the device or increase
|
| 1012 | * the reference count if it already exists.
|
| 1013 |
|
| 1014 | int dev_mc_add(struct net_device *dev, const unsigned char *addr)
|
| 1015 | {
|
| 1016 | return __dev_mc_add(dev, addr, false);
|
| 1017 | }
|
| 1018 | EXPORT_SYMBOL(dev_mc_add);
|
| 1019 |
|
| 1020 | |
| 1021 | * dev_mc_add_global - Add a global multicast address
|
| 1022 | * @dev: device
|
| 1023 | * @addr: address to add
|
| 1024 | *
|
| 1025 | * Add a global multicast address to the device.
|
| 1026 |
|
| 1027 | int dev_mc_add_global(struct net_device *dev, const unsigned char *addr)
|
| 1028 | {
|
| 1029 | return __dev_mc_add(dev, addr, true);
|
| 1030 | }
|
| 1031 | EXPORT_SYMBOL(dev_mc_add_global);
|
| 1032 |
|
| 1033 | static int __dev_mc_del(struct net_device *dev, const unsigned char *addr,
|
| 1034 | bool global)
|
| 1035 | {
|
| 1036 | int err;
|
| 1037 |
|
| 1038 | netif_addr_lock_bh(dev);
|
| 1039 | err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
|
| 1040 | NETDEV_HW_ADDR_T_MULTICAST, global, false);
|
| 1041 | if (!err)
|
| 1042 | __dev_set_rx_mode(dev);
|
| 1043 | netif_addr_unlock_bh(dev);
|
| 1044 | return err;
|
| 1045 | }
|
| 1046 |
|
| 1047 | |
| 1048 | * dev_mc_del - Delete a multicast address.
|
| 1049 | * @dev: device
|
| 1050 | * @addr: address to delete
|
| 1051 | *
|
| 1052 | * Release reference to a multicast address and remove it
|
| 1053 | * from the device if the reference count drops to zero.
|
| 1054 |
|
| 1055 | int dev_mc_del(struct net_device *dev, const unsigned char *addr)
|
| 1056 | {
|
| 1057 | return __dev_mc_del(dev, addr, false);
|
| 1058 | }
|
| 1059 | EXPORT_SYMBOL(dev_mc_del);
|
| 1060 |
|
| 1061 | |
| 1062 | * dev_mc_del_global - Delete a global multicast address.
|
| 1063 | * @dev: device
|
| 1064 | * @addr: address to delete
|
| 1065 | *
|
| 1066 | * Release reference to a multicast address and remove it
|
| 1067 | * from the device if the reference count drops to zero.
|
| 1068 |
|
| 1069 | int dev_mc_del_global(struct net_device *dev, const unsigned char *addr)
|
| 1070 | {
|
| 1071 | return __dev_mc_del(dev, addr, true);
|
| 1072 | }
|
| 1073 | EXPORT_SYMBOL(dev_mc_del_global);
|
| 1074 |
|
| 1075 | |
| 1076 | * dev_mc_sync - Synchronize device's multicast list to another device
|
| 1077 | * @to: destination device
|
| 1078 | * @from: source device
|
| 1079 | *
|
| 1080 | * Add newly added addresses to the destination device and release
|
| 1081 | * addresses that have no users left. The source device must be
|
| 1082 | * locked by netif_addr_lock_bh.
|
| 1083 | *
|
| 1084 | * This function is intended to be called from the ndo_set_rx_mode
|
| 1085 | * function of layered software devices.
|
| 1086 |
|
| 1087 | int dev_mc_sync(struct net_device *to, struct net_device *from)
|
| 1088 | {
|
| 1089 | int err = 0;
|
| 1090 |
|
| 1091 | if (to->addr_len != from->addr_len)
|
| 1092 | return -EINVAL;
|
| 1093 |
|
| 1094 | netif_addr_lock(to);
|
| 1095 | err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
|
| 1096 | if (!err)
|
| 1097 | __dev_set_rx_mode(to);
|
| 1098 | netif_addr_unlock(to);
|
| 1099 | return err;
|
| 1100 | }
|
| 1101 | EXPORT_SYMBOL(dev_mc_sync);
|
| 1102 |
|
| 1103 | |
| 1104 | * dev_mc_sync_multiple - Synchronize device's multicast list to another
|
| 1105 | * device, but allow for multiple calls to sync to multiple devices.
|
| 1106 | * @to: destination device
|
| 1107 | * @from: source device
|
| 1108 | *
|
| 1109 | * Add newly added addresses to the destination device and release
|
| 1110 | * addresses that have no users left. The source device must be
|
| 1111 | * locked by netif_addr_lock_bh.
|
| 1112 | *
|
| 1113 | * This function is intended to be called from the ndo_set_rx_mode
|
| 1114 | * function of layered software devices. It allows for a single
|
| 1115 | * source device to be synced to multiple destination devices.
|
| 1116 |
|
| 1117 | int dev_mc_sync_multiple(struct net_device *to, struct net_device *from)
|
| 1118 | {
|
| 1119 | int err = 0;
|
| 1120 |
|
| 1121 | if (to->addr_len != from->addr_len)
|
| 1122 | return -EINVAL;
|
| 1123 |
|
| 1124 | netif_addr_lock(to);
|
| 1125 | err = __hw_addr_sync_multiple(&to->mc, &from->mc, to->addr_len);
|
| 1126 | if (!err)
|
| 1127 | __dev_set_rx_mode(to);
|
| 1128 | netif_addr_unlock(to);
|
| 1129 | return err;
|
| 1130 | }
|
| 1131 | EXPORT_SYMBOL(dev_mc_sync_multiple);
|
| 1132 |
|
| 1133 | |
| 1134 | * dev_mc_unsync - Remove synchronized addresses from the destination device
|
| 1135 | * @to: destination device
|
| 1136 | * @from: source device
|
| 1137 | *
|
| 1138 | * Remove all addresses that were added to the destination device by
|
| 1139 | * dev_mc_sync(). This function is intended to be called from the
|
| 1140 | * dev->stop function of layered software devices.
|
| 1141 |
|
| 1142 | void dev_mc_unsync(struct net_device *to, struct net_device *from)
|
| 1143 | {
|
| 1144 | if (to->addr_len != from->addr_len)
|
| 1145 | return;
|
| 1146 |
|
| 1147 |
|
| 1148 | netif_addr_lock_bh(from);
|
| 1149 | netif_addr_lock(to);
|
| 1150 | __hw_addr_unsync(&to->mc, &from->mc, to->addr_len);
|
| 1151 | __dev_set_rx_mode(to);
|
| 1152 | netif_addr_unlock(to);
|
| 1153 | netif_addr_unlock_bh(from);
|
| 1154 | }
|
| 1155 | EXPORT_SYMBOL(dev_mc_unsync);
|
| 1156 |
|
| 1157 | |
| 1158 | * dev_mc_flush - Flush multicast addresses
|
| 1159 | * @dev: device
|
| 1160 | *
|
| 1161 | * Flush multicast addresses.
|
| 1162 |
|
| 1163 | void dev_mc_flush(struct net_device *dev)
|
| 1164 | {
|
| 1165 | netif_addr_lock_bh(dev);
|
| 1166 | __hw_addr_flush(&dev->mc);
|
| 1167 | netif_addr_unlock_bh(dev);
|
| 1168 | }
|
| 1169 | EXPORT_SYMBOL(dev_mc_flush);
|
| 1170 |
|
| 1171 | |
| 1172 | * dev_mc_init - Init multicast address list
|
| 1173 | * @dev: device
|
| 1174 | *
|
| 1175 | * Init multicast address list.
|
| 1176 |
|
| 1177 | void dev_mc_init(struct net_device *dev)
|
| 1178 | {
|
| 1179 | __hw_addr_init(&dev->mc);
|
| 1180 | }
|
| 1181 | EXPORT_SYMBOL(dev_mc_init);
|
| 1182 |
|
| 1183 | static int netif_addr_lists_snapshot(struct net_device *dev,
|
| 1184 | struct netdev_hw_addr_list *uc_snap,
|
| 1185 | struct netdev_hw_addr_list *mc_snap,
|
| 1186 | struct netdev_hw_addr_list *uc_ref,
|
| 1187 | struct netdev_hw_addr_list *mc_ref)
|
| 1188 | {
|
| 1189 | int err;
|
| 1190 |
|
| 1191 | err = __hw_addr_list_snapshot(uc_snap, &dev->uc, dev->addr_len,
|
| 1192 | &dev->rx_mode_addr_cache);
|
| 1193 | if (!err)
|
| 1194 | err = __hw_addr_list_snapshot(uc_ref, &dev->uc, dev->addr_len,
|
| 1195 | &dev->rx_mode_addr_cache);
|
| 1196 | if (!err)
|
| 1197 | err = __hw_addr_list_snapshot(mc_snap, &dev->mc,
|
| 1198 | dev->addr_len,
|
| 1199 | &dev->rx_mode_addr_cache);
|
| 1200 | if (!err)
|
| 1201 | err = __hw_addr_list_snapshot(mc_ref, &dev->mc, dev->addr_len,
|
| 1202 | &dev->rx_mode_addr_cache);
|
| 1203 |
|
| 1204 | if (err) {
|
| 1205 | __hw_addr_flush(uc_snap);
|
| 1206 | __hw_addr_flush(uc_ref);
|
| 1207 | __hw_addr_flush(mc_snap);
|
| 1208 | }
|
| 1209 |
|
| 1210 | return err;
|
| 1211 | }
|
| 1212 |
|
| 1213 | static void netif_addr_lists_reconcile(struct net_device *dev,
|
| 1214 | struct netdev_hw_addr_list *uc_snap,
|
| 1215 | struct netdev_hw_addr_list *mc_snap,
|
| 1216 | struct netdev_hw_addr_list *uc_ref,
|
| 1217 | struct netdev_hw_addr_list *mc_ref)
|
| 1218 | {
|
| 1219 | __hw_addr_list_reconcile(&dev->uc, uc_snap, uc_ref, dev->addr_len,
|
| 1220 | &dev->rx_mode_addr_cache);
|
| 1221 | __hw_addr_list_reconcile(&dev->mc, mc_snap, mc_ref, dev->addr_len,
|
| 1222 | &dev->rx_mode_addr_cache);
|
| 1223 | }
|
| 1224 |
|
| 1225 | |
| 1226 | * netif_uc_promisc_update() - evaluate whether uc_promisc should be toggled.
|
| 1227 | * @dev: device
|
| 1228 | *
|
| 1229 | * Must be called under netif_addr_lock_bh.
|
| 1230 | * Return: +1 to enter promisc, -1 to leave, 0 for no change.
|
| 1231 |
|
| 1232 | static int netif_uc_promisc_update(struct net_device *dev)
|
| 1233 | {
|
| 1234 | if (dev->priv_flags & IFF_UNICAST_FLT)
|
| 1235 | return 0;
|
| 1236 |
|
| 1237 | if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
|
| 1238 | dev->uc_promisc = true;
|
| 1239 | return 1;
|
| 1240 | }
|
| 1241 | if (netdev_uc_empty(dev) && dev->uc_promisc) {
|
| 1242 | dev->uc_promisc = false;
|
| 1243 | return -1;
|
| 1244 | }
|
| 1245 | return 0;
|
| 1246 | }
|
| 1247 |
|
| 1248 |
|
| 1249 | #define NETIF_RX_MODE_RETRY_MAX 4
|
| 1250 |
|
| 1251 | void netif_rx_mode_schedule_retry(struct net_device *dev)
|
| 1252 | {
|
| 1253 | unsigned long delay;
|
| 1254 |
|
| 1255 | netdev_assert_locked_ops_compat(dev);
|
| 1256 |
|
| 1257 | if (dev->rx_mode_retry_count >= NETIF_RX_MODE_RETRY_MAX) {
|
| 1258 | netdev_err(dev, "rx_mode retry limit reached, giving up\n");
|
| 1259 | return;
|
| 1260 | }
|
| 1261 |
|
| 1262 | delay = HZ << dev->rx_mode_retry_count;
|
| 1263 | if (mod_timer(&dev->rx_mode_retry_timer, jiffies + delay))
|
| 1264 | return;
|
| 1265 | if (!dev->rx_mode_retry_count)
|
| 1266 | netdev_info(dev, "rx_mode install failed, retrying with backoff\n");
|
| 1267 | dev->rx_mode_retry_count++;
|
| 1268 | }
|
| 1269 | EXPORT_SYMBOL_GPL(netif_rx_mode_schedule_retry);
|
| 1270 |
|
| 1271 | void netif_rx_mode_cancel_retry(struct net_device *dev)
|
| 1272 | {
|
| 1273 | timer_delete_sync(&dev->rx_mode_retry_timer);
|
| 1274 | dev->rx_mode_retry_count = 0;
|
| 1275 | }
|
| 1276 |
|
| 1277 | void netif_rx_mode_run(struct net_device *dev)
|
| 1278 | {
|
| 1279 | struct netdev_hw_addr_list uc_snap, mc_snap, uc_ref, mc_ref;
|
| 1280 | const struct net_device_ops *ops = dev->netdev_ops;
|
| 1281 | int promisc_inc;
|
| 1282 | int err;
|
| 1283 |
|
| 1284 | might_sleep();
|
| 1285 | netdev_assert_locked_ops_compat(dev);
|
| 1286 |
|
| 1287 | __hw_addr_init(&uc_snap);
|
| 1288 | __hw_addr_init(&mc_snap);
|
| 1289 | __hw_addr_init(&uc_ref);
|
| 1290 | __hw_addr_init(&mc_ref);
|
| 1291 |
|
| 1292 | if (!(dev->flags & IFF_UP) || !netif_device_present(dev))
|
| 1293 | return;
|
| 1294 |
|
| 1295 | if (ops->ndo_set_rx_mode_async) {
|
| 1296 | netif_addr_lock_bh(dev);
|
| 1297 | err = netif_addr_lists_snapshot(dev, &uc_snap, &mc_snap,
|
| 1298 | &uc_ref, &mc_ref);
|
| 1299 | if (err) {
|
| 1300 | netif_addr_unlock_bh(dev);
|
| 1301 | netif_rx_mode_schedule_retry(dev);
|
| 1302 | return;
|
| 1303 | }
|
| 1304 |
|
| 1305 | promisc_inc = netif_uc_promisc_update(dev);
|
| 1306 | netif_addr_unlock_bh(dev);
|
| 1307 | } else {
|
| 1308 | netif_addr_lock_bh(dev);
|
| 1309 | promisc_inc = netif_uc_promisc_update(dev);
|
| 1310 | netif_addr_unlock_bh(dev);
|
| 1311 | }
|
| 1312 |
|
| 1313 | if (promisc_inc)
|
| 1314 | __dev_set_promiscuity(dev, promisc_inc, false);
|
| 1315 |
|
| 1316 | if (ops->ndo_set_rx_mode_async) {
|
| 1317 | err = ops->ndo_set_rx_mode_async(dev, &uc_snap, &mc_snap);
|
| 1318 |
|
| 1319 | netif_addr_lock_bh(dev);
|
| 1320 | netif_addr_lists_reconcile(dev, &uc_snap, &mc_snap,
|
| 1321 | &uc_ref, &mc_ref);
|
| 1322 | netif_addr_unlock_bh(dev);
|
| 1323 |
|
| 1324 | if (err)
|
| 1325 | netif_rx_mode_schedule_retry(dev);
|
| 1326 | else
|
| 1327 | dev->rx_mode_retry_count = 0;
|
| 1328 | } else if (ops->ndo_set_rx_mode) {
|
| 1329 | netif_addr_lock_bh(dev);
|
| 1330 | ops->ndo_set_rx_mode(dev);
|
| 1331 | netif_addr_unlock_bh(dev);
|
| 1332 | }
|
| 1333 | }
|
| 1334 |
|
| 1335 | static void netif_rx_mode_queue(struct net_device *dev)
|
| 1336 | {
|
| 1337 | __netdev_work_core_sched(dev, NETDEV_WORK_RX_MODE);
|
| 1338 | }
|
| 1339 |
|
| 1340 | static void netif_rx_mode_retry(struct timer_list *t)
|
| 1341 | {
|
| 1342 | struct net_device *dev =
|
| 1343 | timer_container_of(dev, t, rx_mode_retry_timer);
|
| 1344 |
|
| 1345 | netif_rx_mode_queue(dev);
|
| 1346 | }
|
| 1347 |
|
| 1348 | void netif_rx_mode_init(struct net_device *dev)
|
| 1349 | {
|
| 1350 | __hw_addr_init(&dev->rx_mode_addr_cache);
|
| 1351 | timer_setup(&dev->rx_mode_retry_timer, netif_rx_mode_retry, 0);
|
| 1352 | }
|
| 1353 |
|
| 1354 | |
| 1355 | * __dev_set_rx_mode() - upload unicast and multicast address lists to device
|
| 1356 | * and configure RX filtering.
|
| 1357 | * @dev: device
|
| 1358 | *
|
| 1359 | * When the device doesn't support unicast filtering it is put in promiscuous
|
| 1360 | * mode while unicast addresses are present.
|
| 1361 |
|
| 1362 | void __dev_set_rx_mode(struct net_device *dev)
|
| 1363 | {
|
| 1364 | const struct net_device_ops *ops = dev->netdev_ops;
|
| 1365 | int promisc_inc;
|
| 1366 |
|
| 1367 |
|
| 1368 | if (!(dev->flags & IFF_UP))
|
| 1369 | return;
|
| 1370 |
|
| 1371 | if (!netif_device_present(dev))
|
| 1372 | return;
|
| 1373 |
|
| 1374 | if (ops->ndo_set_rx_mode_async || ops->ndo_change_rx_flags ||
|
| 1375 | netdev_need_ops_lock(dev)) {
|
| 1376 | netif_rx_mode_queue(dev);
|
| 1377 | return;
|
| 1378 | }
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 | promisc_inc = netif_uc_promisc_update(dev);
|
| 1383 | if (promisc_inc)
|
| 1384 | __dev_set_promiscuity(dev, promisc_inc, false);
|
| 1385 |
|
| 1386 | if (ops->ndo_set_rx_mode)
|
| 1387 | ops->ndo_set_rx_mode(dev);
|
| 1388 | }
|
| 1389 |
|
| 1390 | void dev_set_rx_mode(struct net_device *dev)
|
| 1391 | {
|
| 1392 | netif_addr_lock_bh(dev);
|
| 1393 | __dev_set_rx_mode(dev);
|
| 1394 | netif_addr_unlock_bh(dev);
|
| 1395 | }
|
| 1396 |
|
| 1397 | |
| 1398 | * netif_rx_mode_sync() - sync rx mode inline
|
| 1399 | * @dev: network device
|
| 1400 | *
|
| 1401 | * Drivers implementing ndo_set_rx_mode_async() have their rx mode callback
|
| 1402 | * executed from a workqueue. This allows the callback to sleep, but means
|
| 1403 | * the hardware update is deferred and may not be visible to userspace
|
| 1404 | * by the time the initiating syscall returns. netif_rx_mode_sync() steals
|
| 1405 | * workqueue update and executes it inline. This preserves the atomicity of
|
| 1406 | * operations to the userspace.
|
| 1407 |
|
| 1408 | void netif_rx_mode_sync(struct net_device *dev)
|
| 1409 | {
|
| 1410 | if (__netdev_work_core_cancel(dev, NETDEV_WORK_RX_MODE))
|
| 1411 | netif_rx_mode_run(dev);
|
| 1412 | }
|