at v5.15-rc3 26 kB view raw
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/list.h> 14 15/* 16 * General list handling functions 17 */ 18 19static struct netdev_hw_addr* 20__hw_addr_create(const unsigned char *addr, int addr_len, 21 unsigned char addr_type, bool global, bool sync) 22{ 23 struct netdev_hw_addr *ha; 24 int alloc_size; 25 26 alloc_size = sizeof(*ha); 27 if (alloc_size < L1_CACHE_BYTES) 28 alloc_size = L1_CACHE_BYTES; 29 ha = kmalloc(alloc_size, GFP_ATOMIC); 30 if (!ha) 31 return NULL; 32 memcpy(ha->addr, addr, addr_len); 33 ha->type = addr_type; 34 ha->refcount = 1; 35 ha->global_use = global; 36 ha->synced = sync ? 1 : 0; 37 ha->sync_cnt = 0; 38 39 return ha; 40} 41 42static int __hw_addr_add_ex(struct netdev_hw_addr_list *list, 43 const unsigned char *addr, int addr_len, 44 unsigned char addr_type, bool global, bool sync, 45 int sync_count, bool exclusive) 46{ 47 struct rb_node **ins_point = &list->tree.rb_node, *parent = NULL; 48 struct netdev_hw_addr *ha; 49 50 if (addr_len > MAX_ADDR_LEN) 51 return -EINVAL; 52 53 while (*ins_point) { 54 int diff; 55 56 ha = rb_entry(*ins_point, struct netdev_hw_addr, node); 57 diff = memcmp(addr, ha->addr, addr_len); 58 if (diff == 0) 59 diff = memcmp(&addr_type, &ha->type, sizeof(addr_type)); 60 61 parent = *ins_point; 62 if (diff < 0) { 63 ins_point = &parent->rb_left; 64 } else if (diff > 0) { 65 ins_point = &parent->rb_right; 66 } else { 67 if (exclusive) 68 return -EEXIST; 69 if (global) { 70 /* check if addr is already used as global */ 71 if (ha->global_use) 72 return 0; 73 else 74 ha->global_use = true; 75 } 76 if (sync) { 77 if (ha->synced && sync_count) 78 return -EEXIST; 79 else 80 ha->synced++; 81 } 82 ha->refcount++; 83 return 0; 84 } 85 } 86 87 ha = __hw_addr_create(addr, addr_len, addr_type, global, sync); 88 if (!ha) 89 return -ENOMEM; 90 91 /* The first address in dev->dev_addrs is pointed to by dev->dev_addr 92 * and mutated freely by device drivers and netdev ops, so if we insert 93 * it into the tree we'll end up with an invalid rbtree. 94 */ 95 if (list->count > 0) { 96 rb_link_node(&ha->node, parent, ins_point); 97 rb_insert_color(&ha->node, &list->tree); 98 } else { 99 RB_CLEAR_NODE(&ha->node); 100 } 101 102 list_add_tail_rcu(&ha->list, &list->list); 103 list->count++; 104 105 return 0; 106} 107 108static int __hw_addr_add(struct netdev_hw_addr_list *list, 109 const unsigned char *addr, int addr_len, 110 unsigned char addr_type) 111{ 112 return __hw_addr_add_ex(list, addr, addr_len, addr_type, false, false, 113 0, false); 114} 115 116static int __hw_addr_del_entry(struct netdev_hw_addr_list *list, 117 struct netdev_hw_addr *ha, bool global, 118 bool sync) 119{ 120 if (global && !ha->global_use) 121 return -ENOENT; 122 123 if (sync && !ha->synced) 124 return -ENOENT; 125 126 if (global) 127 ha->global_use = false; 128 129 if (sync) 130 ha->synced--; 131 132 if (--ha->refcount) 133 return 0; 134 135 if (!RB_EMPTY_NODE(&ha->node)) 136 rb_erase(&ha->node, &list->tree); 137 138 list_del_rcu(&ha->list); 139 kfree_rcu(ha, rcu_head); 140 list->count--; 141 return 0; 142} 143 144static struct netdev_hw_addr *__hw_addr_lookup(struct netdev_hw_addr_list *list, 145 const unsigned char *addr, int addr_len, 146 unsigned char addr_type) 147{ 148 struct netdev_hw_addr *ha; 149 struct rb_node *node; 150 151 /* The first address isn't inserted into the tree because in the dev->dev_addrs 152 * list it's the address pointed to by dev->dev_addr which is freely mutated 153 * in place, so we need to check it separately. 154 */ 155 ha = list_first_entry(&list->list, struct netdev_hw_addr, list); 156 if (ha && !memcmp(addr, ha->addr, addr_len) && 157 (!addr_type || addr_type == ha->type)) 158 return ha; 159 160 node = list->tree.rb_node; 161 162 while (node) { 163 struct netdev_hw_addr *ha = rb_entry(node, struct netdev_hw_addr, node); 164 int diff = memcmp(addr, ha->addr, addr_len); 165 166 if (diff == 0 && addr_type) 167 diff = memcmp(&addr_type, &ha->type, sizeof(addr_type)); 168 169 if (diff < 0) 170 node = node->rb_left; 171 else if (diff > 0) 172 node = node->rb_right; 173 else 174 return ha; 175 } 176 177 return NULL; 178} 179 180static int __hw_addr_del_ex(struct netdev_hw_addr_list *list, 181 const unsigned char *addr, int addr_len, 182 unsigned char addr_type, bool global, bool sync) 183{ 184 struct netdev_hw_addr *ha = __hw_addr_lookup(list, addr, addr_len, addr_type); 185 186 if (!ha) 187 return -ENOENT; 188 return __hw_addr_del_entry(list, ha, global, sync); 189} 190 191static int __hw_addr_del(struct netdev_hw_addr_list *list, 192 const unsigned char *addr, int addr_len, 193 unsigned char addr_type) 194{ 195 return __hw_addr_del_ex(list, addr, addr_len, addr_type, false, false); 196} 197 198static int __hw_addr_sync_one(struct netdev_hw_addr_list *to_list, 199 struct netdev_hw_addr *ha, 200 int addr_len) 201{ 202 int err; 203 204 err = __hw_addr_add_ex(to_list, ha->addr, addr_len, ha->type, 205 false, true, ha->sync_cnt, false); 206 if (err && err != -EEXIST) 207 return err; 208 209 if (!err) { 210 ha->sync_cnt++; 211 ha->refcount++; 212 } 213 214 return 0; 215} 216 217static void __hw_addr_unsync_one(struct netdev_hw_addr_list *to_list, 218 struct netdev_hw_addr_list *from_list, 219 struct netdev_hw_addr *ha, 220 int addr_len) 221{ 222 int err; 223 224 err = __hw_addr_del_ex(to_list, ha->addr, addr_len, ha->type, 225 false, true); 226 if (err) 227 return; 228 ha->sync_cnt--; 229 /* address on from list is not marked synced */ 230 __hw_addr_del_entry(from_list, ha, false, false); 231} 232 233static int __hw_addr_sync_multiple(struct netdev_hw_addr_list *to_list, 234 struct netdev_hw_addr_list *from_list, 235 int addr_len) 236{ 237 int err = 0; 238 struct netdev_hw_addr *ha, *tmp; 239 240 list_for_each_entry_safe(ha, tmp, &from_list->list, list) { 241 if (ha->sync_cnt == ha->refcount) { 242 __hw_addr_unsync_one(to_list, from_list, ha, addr_len); 243 } else { 244 err = __hw_addr_sync_one(to_list, ha, addr_len); 245 if (err) 246 break; 247 } 248 } 249 return err; 250} 251 252/* This function only works where there is a strict 1-1 relationship 253 * between source and destionation of they synch. If you ever need to 254 * sync addresses to more then 1 destination, you need to use 255 * __hw_addr_sync_multiple(). 256 */ 257int __hw_addr_sync(struct netdev_hw_addr_list *to_list, 258 struct netdev_hw_addr_list *from_list, 259 int addr_len) 260{ 261 int err = 0; 262 struct netdev_hw_addr *ha, *tmp; 263 264 list_for_each_entry_safe(ha, tmp, &from_list->list, list) { 265 if (!ha->sync_cnt) { 266 err = __hw_addr_sync_one(to_list, ha, addr_len); 267 if (err) 268 break; 269 } else if (ha->refcount == 1) 270 __hw_addr_unsync_one(to_list, from_list, ha, addr_len); 271 } 272 return err; 273} 274EXPORT_SYMBOL(__hw_addr_sync); 275 276void __hw_addr_unsync(struct netdev_hw_addr_list *to_list, 277 struct netdev_hw_addr_list *from_list, 278 int addr_len) 279{ 280 struct netdev_hw_addr *ha, *tmp; 281 282 list_for_each_entry_safe(ha, tmp, &from_list->list, list) { 283 if (ha->sync_cnt) 284 __hw_addr_unsync_one(to_list, from_list, ha, addr_len); 285 } 286} 287EXPORT_SYMBOL(__hw_addr_unsync); 288 289/** 290 * __hw_addr_sync_dev - Synchonize device's multicast list 291 * @list: address list to syncronize 292 * @dev: device to sync 293 * @sync: function to call if address should be added 294 * @unsync: function to call if address should be removed 295 * 296 * This function is intended to be called from the ndo_set_rx_mode 297 * function of devices that require explicit address add/remove 298 * notifications. The unsync function may be NULL in which case 299 * the addresses requiring removal will simply be removed without 300 * any notification to the device. 301 **/ 302int __hw_addr_sync_dev(struct netdev_hw_addr_list *list, 303 struct net_device *dev, 304 int (*sync)(struct net_device *, const unsigned char *), 305 int (*unsync)(struct net_device *, 306 const unsigned char *)) 307{ 308 struct netdev_hw_addr *ha, *tmp; 309 int err; 310 311 /* first go through and flush out any stale entries */ 312 list_for_each_entry_safe(ha, tmp, &list->list, list) { 313 if (!ha->sync_cnt || ha->refcount != 1) 314 continue; 315 316 /* if unsync is defined and fails defer unsyncing address */ 317 if (unsync && unsync(dev, ha->addr)) 318 continue; 319 320 ha->sync_cnt--; 321 __hw_addr_del_entry(list, ha, false, false); 322 } 323 324 /* go through and sync new entries to the list */ 325 list_for_each_entry_safe(ha, tmp, &list->list, list) { 326 if (ha->sync_cnt) 327 continue; 328 329 err = sync(dev, ha->addr); 330 if (err) 331 return err; 332 333 ha->sync_cnt++; 334 ha->refcount++; 335 } 336 337 return 0; 338} 339EXPORT_SYMBOL(__hw_addr_sync_dev); 340 341/** 342 * __hw_addr_ref_sync_dev - Synchronize device's multicast address list taking 343 * into account references 344 * @list: address list to synchronize 345 * @dev: device to sync 346 * @sync: function to call if address or reference on it should be added 347 * @unsync: function to call if address or some reference on it should removed 348 * 349 * This function is intended to be called from the ndo_set_rx_mode 350 * function of devices that require explicit address or references on it 351 * add/remove notifications. The unsync function may be NULL in which case 352 * the addresses or references on it requiring removal will simply be 353 * removed without any notification to the device. That is responsibility of 354 * the driver to identify and distribute address or references on it between 355 * internal address tables. 356 **/ 357int __hw_addr_ref_sync_dev(struct netdev_hw_addr_list *list, 358 struct net_device *dev, 359 int (*sync)(struct net_device *, 360 const unsigned char *, int), 361 int (*unsync)(struct net_device *, 362 const unsigned char *, int)) 363{ 364 struct netdev_hw_addr *ha, *tmp; 365 int err, ref_cnt; 366 367 /* first go through and flush out any unsynced/stale entries */ 368 list_for_each_entry_safe(ha, tmp, &list->list, list) { 369 /* sync if address is not used */ 370 if ((ha->sync_cnt << 1) <= ha->refcount) 371 continue; 372 373 /* if fails defer unsyncing address */ 374 ref_cnt = ha->refcount - ha->sync_cnt; 375 if (unsync && unsync(dev, ha->addr, ref_cnt)) 376 continue; 377 378 ha->refcount = (ref_cnt << 1) + 1; 379 ha->sync_cnt = ref_cnt; 380 __hw_addr_del_entry(list, ha, false, false); 381 } 382 383 /* go through and sync updated/new entries to the list */ 384 list_for_each_entry_safe(ha, tmp, &list->list, list) { 385 /* sync if address added or reused */ 386 if ((ha->sync_cnt << 1) >= ha->refcount) 387 continue; 388 389 ref_cnt = ha->refcount - ha->sync_cnt; 390 err = sync(dev, ha->addr, ref_cnt); 391 if (err) 392 return err; 393 394 ha->refcount = ref_cnt << 1; 395 ha->sync_cnt = ref_cnt; 396 } 397 398 return 0; 399} 400EXPORT_SYMBOL(__hw_addr_ref_sync_dev); 401 402/** 403 * __hw_addr_ref_unsync_dev - Remove synchronized addresses and references on 404 * it from device 405 * @list: address list to remove synchronized addresses (references on it) from 406 * @dev: device to sync 407 * @unsync: function to call if address and references on it should be removed 408 * 409 * Remove all addresses that were added to the device by 410 * __hw_addr_ref_sync_dev(). This function is intended to be called from the 411 * ndo_stop or ndo_open functions on devices that require explicit address (or 412 * references on it) add/remove notifications. If the unsync function pointer 413 * is NULL then this function can be used to just reset the sync_cnt for the 414 * addresses in the list. 415 **/ 416void __hw_addr_ref_unsync_dev(struct netdev_hw_addr_list *list, 417 struct net_device *dev, 418 int (*unsync)(struct net_device *, 419 const unsigned char *, int)) 420{ 421 struct netdev_hw_addr *ha, *tmp; 422 423 list_for_each_entry_safe(ha, tmp, &list->list, list) { 424 if (!ha->sync_cnt) 425 continue; 426 427 /* if fails defer unsyncing address */ 428 if (unsync && unsync(dev, ha->addr, ha->sync_cnt)) 429 continue; 430 431 ha->refcount -= ha->sync_cnt - 1; 432 ha->sync_cnt = 0; 433 __hw_addr_del_entry(list, ha, false, false); 434 } 435} 436EXPORT_SYMBOL(__hw_addr_ref_unsync_dev); 437 438/** 439 * __hw_addr_unsync_dev - Remove synchronized addresses from device 440 * @list: address list to remove synchronized addresses from 441 * @dev: device to sync 442 * @unsync: function to call if address should be removed 443 * 444 * Remove all addresses that were added to the device by __hw_addr_sync_dev(). 445 * This function is intended to be called from the ndo_stop or ndo_open 446 * functions on devices that require explicit address add/remove 447 * notifications. If the unsync function pointer is NULL then this function 448 * can be used to just reset the sync_cnt for the addresses in the list. 449 **/ 450void __hw_addr_unsync_dev(struct netdev_hw_addr_list *list, 451 struct net_device *dev, 452 int (*unsync)(struct net_device *, 453 const unsigned char *)) 454{ 455 struct netdev_hw_addr *ha, *tmp; 456 457 list_for_each_entry_safe(ha, tmp, &list->list, list) { 458 if (!ha->sync_cnt) 459 continue; 460 461 /* if unsync is defined and fails defer unsyncing address */ 462 if (unsync && unsync(dev, ha->addr)) 463 continue; 464 465 ha->sync_cnt--; 466 __hw_addr_del_entry(list, ha, false, false); 467 } 468} 469EXPORT_SYMBOL(__hw_addr_unsync_dev); 470 471static void __hw_addr_flush(struct netdev_hw_addr_list *list) 472{ 473 struct netdev_hw_addr *ha, *tmp; 474 475 list->tree = RB_ROOT; 476 list_for_each_entry_safe(ha, tmp, &list->list, list) { 477 list_del_rcu(&ha->list); 478 kfree_rcu(ha, rcu_head); 479 } 480 list->count = 0; 481} 482 483void __hw_addr_init(struct netdev_hw_addr_list *list) 484{ 485 INIT_LIST_HEAD(&list->list); 486 list->count = 0; 487 list->tree = RB_ROOT; 488} 489EXPORT_SYMBOL(__hw_addr_init); 490 491/* 492 * Device addresses handling functions 493 */ 494 495/** 496 * dev_addr_flush - Flush device address list 497 * @dev: device 498 * 499 * Flush device address list and reset ->dev_addr. 500 * 501 * The caller must hold the rtnl_mutex. 502 */ 503void dev_addr_flush(struct net_device *dev) 504{ 505 /* rtnl_mutex must be held here */ 506 507 __hw_addr_flush(&dev->dev_addrs); 508 dev->dev_addr = NULL; 509} 510EXPORT_SYMBOL(dev_addr_flush); 511 512/** 513 * dev_addr_init - Init device address list 514 * @dev: device 515 * 516 * Init device address list and create the first element, 517 * used by ->dev_addr. 518 * 519 * The caller must hold the rtnl_mutex. 520 */ 521int dev_addr_init(struct net_device *dev) 522{ 523 unsigned char addr[MAX_ADDR_LEN]; 524 struct netdev_hw_addr *ha; 525 int err; 526 527 /* rtnl_mutex must be held here */ 528 529 __hw_addr_init(&dev->dev_addrs); 530 memset(addr, 0, sizeof(addr)); 531 err = __hw_addr_add(&dev->dev_addrs, addr, sizeof(addr), 532 NETDEV_HW_ADDR_T_LAN); 533 if (!err) { 534 /* 535 * Get the first (previously created) address from the list 536 * and set dev_addr pointer to this location. 537 */ 538 ha = list_first_entry(&dev->dev_addrs.list, 539 struct netdev_hw_addr, list); 540 dev->dev_addr = ha->addr; 541 } 542 return err; 543} 544EXPORT_SYMBOL(dev_addr_init); 545 546/** 547 * dev_addr_add - Add a device address 548 * @dev: device 549 * @addr: address to add 550 * @addr_type: address type 551 * 552 * Add a device address to the device or increase the reference count if 553 * it already exists. 554 * 555 * The caller must hold the rtnl_mutex. 556 */ 557int dev_addr_add(struct net_device *dev, const unsigned char *addr, 558 unsigned char addr_type) 559{ 560 int err; 561 562 ASSERT_RTNL(); 563 564 err = dev_pre_changeaddr_notify(dev, addr, NULL); 565 if (err) 566 return err; 567 err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type); 568 if (!err) 569 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 570 return err; 571} 572EXPORT_SYMBOL(dev_addr_add); 573 574/** 575 * dev_addr_del - Release a device address. 576 * @dev: device 577 * @addr: address to delete 578 * @addr_type: address type 579 * 580 * Release reference to a device address and remove it from the device 581 * if the reference count drops to zero. 582 * 583 * The caller must hold the rtnl_mutex. 584 */ 585int dev_addr_del(struct net_device *dev, const unsigned char *addr, 586 unsigned char addr_type) 587{ 588 int err; 589 struct netdev_hw_addr *ha; 590 591 ASSERT_RTNL(); 592 593 /* 594 * We can not remove the first address from the list because 595 * dev->dev_addr points to that. 596 */ 597 ha = list_first_entry(&dev->dev_addrs.list, 598 struct netdev_hw_addr, list); 599 if (!memcmp(ha->addr, addr, dev->addr_len) && 600 ha->type == addr_type && ha->refcount == 1) 601 return -ENOENT; 602 603 err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len, 604 addr_type); 605 if (!err) 606 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 607 return err; 608} 609EXPORT_SYMBOL(dev_addr_del); 610 611/* 612 * Unicast list handling functions 613 */ 614 615/** 616 * dev_uc_add_excl - Add a global secondary unicast address 617 * @dev: device 618 * @addr: address to add 619 */ 620int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr) 621{ 622 int err; 623 624 netif_addr_lock_bh(dev); 625 err = __hw_addr_add_ex(&dev->uc, addr, dev->addr_len, 626 NETDEV_HW_ADDR_T_UNICAST, true, false, 627 0, true); 628 if (!err) 629 __dev_set_rx_mode(dev); 630 netif_addr_unlock_bh(dev); 631 return err; 632} 633EXPORT_SYMBOL(dev_uc_add_excl); 634 635/** 636 * dev_uc_add - Add a secondary unicast address 637 * @dev: device 638 * @addr: address to add 639 * 640 * Add a secondary unicast address to the device or increase 641 * the reference count if it already exists. 642 */ 643int dev_uc_add(struct net_device *dev, const unsigned char *addr) 644{ 645 int err; 646 647 netif_addr_lock_bh(dev); 648 err = __hw_addr_add(&dev->uc, addr, dev->addr_len, 649 NETDEV_HW_ADDR_T_UNICAST); 650 if (!err) 651 __dev_set_rx_mode(dev); 652 netif_addr_unlock_bh(dev); 653 return err; 654} 655EXPORT_SYMBOL(dev_uc_add); 656 657/** 658 * dev_uc_del - Release secondary unicast address. 659 * @dev: device 660 * @addr: address to delete 661 * 662 * Release reference to a secondary unicast address and remove it 663 * from the device if the reference count drops to zero. 664 */ 665int dev_uc_del(struct net_device *dev, const unsigned char *addr) 666{ 667 int err; 668 669 netif_addr_lock_bh(dev); 670 err = __hw_addr_del(&dev->uc, addr, dev->addr_len, 671 NETDEV_HW_ADDR_T_UNICAST); 672 if (!err) 673 __dev_set_rx_mode(dev); 674 netif_addr_unlock_bh(dev); 675 return err; 676} 677EXPORT_SYMBOL(dev_uc_del); 678 679/** 680 * dev_uc_sync - Synchronize device's unicast list to another device 681 * @to: destination device 682 * @from: source device 683 * 684 * Add newly added addresses to the destination device and release 685 * addresses that have no users left. The source device must be 686 * locked by netif_addr_lock_bh. 687 * 688 * This function is intended to be called from the dev->set_rx_mode 689 * function of layered software devices. This function assumes that 690 * addresses will only ever be synced to the @to devices and no other. 691 */ 692int dev_uc_sync(struct net_device *to, struct net_device *from) 693{ 694 int err = 0; 695 696 if (to->addr_len != from->addr_len) 697 return -EINVAL; 698 699 netif_addr_lock(to); 700 err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len); 701 if (!err) 702 __dev_set_rx_mode(to); 703 netif_addr_unlock(to); 704 return err; 705} 706EXPORT_SYMBOL(dev_uc_sync); 707 708/** 709 * dev_uc_sync_multiple - Synchronize device's unicast list to another 710 * device, but allow for multiple calls to sync to multiple devices. 711 * @to: destination device 712 * @from: source device 713 * 714 * Add newly added addresses to the destination device and release 715 * addresses that have been deleted from the source. The source device 716 * must be locked by netif_addr_lock_bh. 717 * 718 * This function is intended to be called from the dev->set_rx_mode 719 * function of layered software devices. It allows for a single source 720 * device to be synced to multiple destination devices. 721 */ 722int dev_uc_sync_multiple(struct net_device *to, struct net_device *from) 723{ 724 int err = 0; 725 726 if (to->addr_len != from->addr_len) 727 return -EINVAL; 728 729 netif_addr_lock(to); 730 err = __hw_addr_sync_multiple(&to->uc, &from->uc, to->addr_len); 731 if (!err) 732 __dev_set_rx_mode(to); 733 netif_addr_unlock(to); 734 return err; 735} 736EXPORT_SYMBOL(dev_uc_sync_multiple); 737 738/** 739 * dev_uc_unsync - Remove synchronized addresses from the destination device 740 * @to: destination device 741 * @from: source device 742 * 743 * Remove all addresses that were added to the destination device by 744 * dev_uc_sync(). This function is intended to be called from the 745 * dev->stop function of layered software devices. 746 */ 747void dev_uc_unsync(struct net_device *to, struct net_device *from) 748{ 749 if (to->addr_len != from->addr_len) 750 return; 751 752 /* netif_addr_lock_bh() uses lockdep subclass 0, this is okay for two 753 * reasons: 754 * 1) This is always called without any addr_list_lock, so as the 755 * outermost one here, it must be 0. 756 * 2) This is called by some callers after unlinking the upper device, 757 * so the dev->lower_level becomes 1 again. 758 * Therefore, the subclass for 'from' is 0, for 'to' is either 1 or 759 * larger. 760 */ 761 netif_addr_lock_bh(from); 762 netif_addr_lock(to); 763 __hw_addr_unsync(&to->uc, &from->uc, to->addr_len); 764 __dev_set_rx_mode(to); 765 netif_addr_unlock(to); 766 netif_addr_unlock_bh(from); 767} 768EXPORT_SYMBOL(dev_uc_unsync); 769 770/** 771 * dev_uc_flush - Flush unicast addresses 772 * @dev: device 773 * 774 * Flush unicast addresses. 775 */ 776void dev_uc_flush(struct net_device *dev) 777{ 778 netif_addr_lock_bh(dev); 779 __hw_addr_flush(&dev->uc); 780 netif_addr_unlock_bh(dev); 781} 782EXPORT_SYMBOL(dev_uc_flush); 783 784/** 785 * dev_uc_init - Init unicast address list 786 * @dev: device 787 * 788 * Init unicast address list. 789 */ 790void dev_uc_init(struct net_device *dev) 791{ 792 __hw_addr_init(&dev->uc); 793} 794EXPORT_SYMBOL(dev_uc_init); 795 796/* 797 * Multicast list handling functions 798 */ 799 800/** 801 * dev_mc_add_excl - Add a global secondary multicast address 802 * @dev: device 803 * @addr: address to add 804 */ 805int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr) 806{ 807 int err; 808 809 netif_addr_lock_bh(dev); 810 err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len, 811 NETDEV_HW_ADDR_T_MULTICAST, true, false, 812 0, true); 813 if (!err) 814 __dev_set_rx_mode(dev); 815 netif_addr_unlock_bh(dev); 816 return err; 817} 818EXPORT_SYMBOL(dev_mc_add_excl); 819 820static int __dev_mc_add(struct net_device *dev, const unsigned char *addr, 821 bool global) 822{ 823 int err; 824 825 netif_addr_lock_bh(dev); 826 err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len, 827 NETDEV_HW_ADDR_T_MULTICAST, global, false, 828 0, false); 829 if (!err) 830 __dev_set_rx_mode(dev); 831 netif_addr_unlock_bh(dev); 832 return err; 833} 834/** 835 * dev_mc_add - Add a multicast address 836 * @dev: device 837 * @addr: address to add 838 * 839 * Add a multicast address to the device or increase 840 * the reference count if it already exists. 841 */ 842int dev_mc_add(struct net_device *dev, const unsigned char *addr) 843{ 844 return __dev_mc_add(dev, addr, false); 845} 846EXPORT_SYMBOL(dev_mc_add); 847 848/** 849 * dev_mc_add_global - Add a global multicast address 850 * @dev: device 851 * @addr: address to add 852 * 853 * Add a global multicast address to the device. 854 */ 855int dev_mc_add_global(struct net_device *dev, const unsigned char *addr) 856{ 857 return __dev_mc_add(dev, addr, true); 858} 859EXPORT_SYMBOL(dev_mc_add_global); 860 861static int __dev_mc_del(struct net_device *dev, const unsigned char *addr, 862 bool global) 863{ 864 int err; 865 866 netif_addr_lock_bh(dev); 867 err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len, 868 NETDEV_HW_ADDR_T_MULTICAST, global, false); 869 if (!err) 870 __dev_set_rx_mode(dev); 871 netif_addr_unlock_bh(dev); 872 return err; 873} 874 875/** 876 * dev_mc_del - Delete a multicast address. 877 * @dev: device 878 * @addr: address to delete 879 * 880 * Release reference to a multicast address and remove it 881 * from the device if the reference count drops to zero. 882 */ 883int dev_mc_del(struct net_device *dev, const unsigned char *addr) 884{ 885 return __dev_mc_del(dev, addr, false); 886} 887EXPORT_SYMBOL(dev_mc_del); 888 889/** 890 * dev_mc_del_global - Delete a global multicast address. 891 * @dev: device 892 * @addr: address to delete 893 * 894 * Release reference to a multicast address and remove it 895 * from the device if the reference count drops to zero. 896 */ 897int dev_mc_del_global(struct net_device *dev, const unsigned char *addr) 898{ 899 return __dev_mc_del(dev, addr, true); 900} 901EXPORT_SYMBOL(dev_mc_del_global); 902 903/** 904 * dev_mc_sync - Synchronize device's multicast list to another device 905 * @to: destination device 906 * @from: source device 907 * 908 * Add newly added addresses to the destination device and release 909 * addresses that have no users left. The source device must be 910 * locked by netif_addr_lock_bh. 911 * 912 * This function is intended to be called from the ndo_set_rx_mode 913 * function of layered software devices. 914 */ 915int dev_mc_sync(struct net_device *to, struct net_device *from) 916{ 917 int err = 0; 918 919 if (to->addr_len != from->addr_len) 920 return -EINVAL; 921 922 netif_addr_lock(to); 923 err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len); 924 if (!err) 925 __dev_set_rx_mode(to); 926 netif_addr_unlock(to); 927 return err; 928} 929EXPORT_SYMBOL(dev_mc_sync); 930 931/** 932 * dev_mc_sync_multiple - Synchronize device's multicast list to another 933 * device, but allow for multiple calls to sync to multiple devices. 934 * @to: destination device 935 * @from: source device 936 * 937 * Add newly added addresses to the destination device and release 938 * addresses that have no users left. The source device must be 939 * locked by netif_addr_lock_bh. 940 * 941 * This function is intended to be called from the ndo_set_rx_mode 942 * function of layered software devices. It allows for a single 943 * source device to be synced to multiple destination devices. 944 */ 945int dev_mc_sync_multiple(struct net_device *to, struct net_device *from) 946{ 947 int err = 0; 948 949 if (to->addr_len != from->addr_len) 950 return -EINVAL; 951 952 netif_addr_lock(to); 953 err = __hw_addr_sync_multiple(&to->mc, &from->mc, to->addr_len); 954 if (!err) 955 __dev_set_rx_mode(to); 956 netif_addr_unlock(to); 957 return err; 958} 959EXPORT_SYMBOL(dev_mc_sync_multiple); 960 961/** 962 * dev_mc_unsync - Remove synchronized addresses from the destination device 963 * @to: destination device 964 * @from: source device 965 * 966 * Remove all addresses that were added to the destination device by 967 * dev_mc_sync(). This function is intended to be called from the 968 * dev->stop function of layered software devices. 969 */ 970void dev_mc_unsync(struct net_device *to, struct net_device *from) 971{ 972 if (to->addr_len != from->addr_len) 973 return; 974 975 /* See the above comments inside dev_uc_unsync(). */ 976 netif_addr_lock_bh(from); 977 netif_addr_lock(to); 978 __hw_addr_unsync(&to->mc, &from->mc, to->addr_len); 979 __dev_set_rx_mode(to); 980 netif_addr_unlock(to); 981 netif_addr_unlock_bh(from); 982} 983EXPORT_SYMBOL(dev_mc_unsync); 984 985/** 986 * dev_mc_flush - Flush multicast addresses 987 * @dev: device 988 * 989 * Flush multicast addresses. 990 */ 991void dev_mc_flush(struct net_device *dev) 992{ 993 netif_addr_lock_bh(dev); 994 __hw_addr_flush(&dev->mc); 995 netif_addr_unlock_bh(dev); 996} 997EXPORT_SYMBOL(dev_mc_flush); 998 999/** 1000 * dev_mc_init - Init multicast address list 1001 * @dev: device 1002 * 1003 * Init multicast address list. 1004 */ 1005void dev_mc_init(struct net_device *dev) 1006{ 1007 __hw_addr_init(&dev->mc); 1008} 1009EXPORT_SYMBOL(dev_mc_init);