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