1 /* 2 * net/core/dev_addr_lists.c - Functions for handling net device lists 3 * Copyright (c) 2010 Jiri Pirko <[email protected]> 4 * 5 * This file contains functions for working with unicast, multicast and device 6 * addresses lists. 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 */ 13 14 #include <linux/netdevice.h> 15 #include <linux/rtnetlink.h> 16 #include <linux/export.h> 17 #include <linux/list.h> 18 19 /* 20 * General list handling functions 21 */ 22 23 static int __hw_addr_create_ex(struct netdev_hw_addr_list *list, 24 const unsigned char *addr, int addr_len, 25 unsigned char addr_type, bool global, 26 bool sync) 27 { 28 struct netdev_hw_addr *ha; 29 int alloc_size; 30 31 alloc_size = sizeof(*ha); 32 if (alloc_size < L1_CACHE_BYTES) 33 alloc_size = L1_CACHE_BYTES; 34 ha = kmalloc(alloc_size, GFP_ATOMIC); 35 if (!ha) 36 return -ENOMEM; 37 memcpy(ha->addr, addr, addr_len); 38 ha->type = addr_type; 39 ha->refcount = 1; 40 ha->global_use = global; 41 ha->synced = sync; 42 ha->sync_cnt = 0; 43 list_add_tail_rcu(&ha->list, &list->list); 44 list->count++; 45 46 return 0; 47 } 48 49 static int __hw_addr_add_ex(struct netdev_hw_addr_list *list, 50 const unsigned char *addr, int addr_len, 51 unsigned char addr_type, bool global, bool sync) 52 { 53 struct netdev_hw_addr *ha; 54 55 if (addr_len > MAX_ADDR_LEN) 56 return -EINVAL; 57 58 list_for_each_entry(ha, &list->list, list) { 59 if (!memcmp(ha->addr, addr, addr_len) && 60 ha->type == addr_type) { 61 if (global) { 62 /* check if addr is already used as global */ 63 if (ha->global_use) 64 return 0; 65 else 66 ha->global_use = true; 67 } 68 if (sync) { 69 if (ha->synced) 70 return -EEXIST; 71 else 72 ha->synced = true; 73 } 74 ha->refcount++; 75 return 0; 76 } 77 } 78 79 return __hw_addr_create_ex(list, addr, addr_len, addr_type, global, 80 sync); 81 } 82 83 static int __hw_addr_add(struct netdev_hw_addr_list *list, 84 const unsigned char *addr, int addr_len, 85 unsigned char addr_type) 86 { 87 return __hw_addr_add_ex(list, addr, addr_len, addr_type, false, false); 88 } 89 90 static int __hw_addr_del_entry(struct netdev_hw_addr_list *list, 91 struct netdev_hw_addr *ha, bool global, 92 bool sync) 93 { 94 if (global && !ha->global_use) 95 return -ENOENT; 96 97 if (sync && !ha->synced) 98 return -ENOENT; 99 100 if (global) 101 ha->global_use = false; 102 103 if (sync) 104 ha->synced = false; 105 106 if (--ha->refcount) 107 return 0; 108 list_del_rcu(&ha->list); 109 kfree_rcu(ha, rcu_head); 110 list->count--; 111 return 0; 112 } 113 114 static int __hw_addr_del_ex(struct netdev_hw_addr_list *list, 115 const unsigned char *addr, int addr_len, 116 unsigned char addr_type, bool global, bool sync) 117 { 118 struct netdev_hw_addr *ha; 119 120 list_for_each_entry(ha, &list->list, list) { 121 if (!memcmp(ha->addr, addr, addr_len) && 122 (ha->type == addr_type || !addr_type)) 123 return __hw_addr_del_entry(list, ha, global, sync); 124 } 125 return -ENOENT; 126 } 127 128 static int __hw_addr_del(struct netdev_hw_addr_list *list, 129 const unsigned char *addr, int addr_len, 130 unsigned char addr_type) 131 { 132 return __hw_addr_del_ex(list, addr, addr_len, addr_type, false, false); 133 } 134 135 static int __hw_addr_sync_one(struct netdev_hw_addr_list *to_list, 136 struct netdev_hw_addr *ha, 137 int addr_len) 138 { 139 int err; 140 141 err = __hw_addr_add_ex(to_list, ha->addr, addr_len, ha->type, 142 false, true); 143 if (err && err != -EEXIST) 144 return err; 145 146 if (!err) { 147 ha->sync_cnt++; 148 ha->refcount++; 149 } 150 151 return 0; 152 } 153 154 static void __hw_addr_unsync_one(struct netdev_hw_addr_list *to_list, 155 struct netdev_hw_addr_list *from_list, 156 struct netdev_hw_addr *ha, 157 int addr_len) 158 { 159 int err; 160 161 err = __hw_addr_del_ex(to_list, ha->addr, addr_len, ha->type, 162 false, true); 163 if (err) 164 return; 165 ha->sync_cnt--; 166 /* address on from list is not marked synced */ 167 __hw_addr_del_entry(from_list, ha, false, false); 168 } 169 170 static int __hw_addr_sync_multiple(struct netdev_hw_addr_list *to_list, 171 struct netdev_hw_addr_list *from_list, 172 int addr_len) 173 { 174 int err = 0; 175 struct netdev_hw_addr *ha, *tmp; 176 177 list_for_each_entry_safe(ha, tmp, &from_list->list, list) { 178 if (ha->sync_cnt == ha->refcount) { 179 __hw_addr_unsync_one(to_list, from_list, ha, addr_len); 180 } else { 181 err = __hw_addr_sync_one(to_list, ha, addr_len); 182 if (err) 183 break; 184 } 185 } 186 return err; 187 } 188 189 /* This function only works where there is a strict 1-1 relationship 190 * between source and destionation of they synch. If you ever need to 191 * sync addresses to more then 1 destination, you need to use 192 * __hw_addr_sync_multiple(). 193 */ 194 int __hw_addr_sync(struct netdev_hw_addr_list *to_list, 195 struct netdev_hw_addr_list *from_list, 196 int addr_len) 197 { 198 int err = 0; 199 struct netdev_hw_addr *ha, *tmp; 200 201 list_for_each_entry_safe(ha, tmp, &from_list->list, list) { 202 if (!ha->sync_cnt) { 203 err = __hw_addr_sync_one(to_list, ha, addr_len); 204 if (err) 205 break; 206 } else if (ha->refcount == 1) 207 __hw_addr_unsync_one(to_list, from_list, ha, addr_len); 208 } 209 return err; 210 } 211 EXPORT_SYMBOL(__hw_addr_sync); 212 213 void __hw_addr_unsync(struct netdev_hw_addr_list *to_list, 214 struct netdev_hw_addr_list *from_list, 215 int addr_len) 216 { 217 struct netdev_hw_addr *ha, *tmp; 218 219 list_for_each_entry_safe(ha, tmp, &from_list->list, list) { 220 if (ha->sync_cnt) 221 __hw_addr_unsync_one(to_list, from_list, ha, addr_len); 222 } 223 } 224 EXPORT_SYMBOL(__hw_addr_unsync); 225 226 static void __hw_addr_flush(struct netdev_hw_addr_list *list) 227 { 228 struct netdev_hw_addr *ha, *tmp; 229 230 list_for_each_entry_safe(ha, tmp, &list->list, list) { 231 list_del_rcu(&ha->list); 232 kfree_rcu(ha, rcu_head); 233 } 234 list->count = 0; 235 } 236 237 void __hw_addr_init(struct netdev_hw_addr_list *list) 238 { 239 INIT_LIST_HEAD(&list->list); 240 list->count = 0; 241 } 242 EXPORT_SYMBOL(__hw_addr_init); 243 244 /* 245 * Device addresses handling functions 246 */ 247 248 /** 249 * dev_addr_flush - Flush device address list 250 * @dev: device 251 * 252 * Flush device address list and reset ->dev_addr. 253 * 254 * The caller must hold the rtnl_mutex. 255 */ 256 void dev_addr_flush(struct net_device *dev) 257 { 258 /* rtnl_mutex must be held here */ 259 260 __hw_addr_flush(&dev->dev_addrs); 261 dev->dev_addr = NULL; 262 } 263 EXPORT_SYMBOL(dev_addr_flush); 264 265 /** 266 * dev_addr_init - Init device address list 267 * @dev: device 268 * 269 * Init device address list and create the first element, 270 * used by ->dev_addr. 271 * 272 * The caller must hold the rtnl_mutex. 273 */ 274 int dev_addr_init(struct net_device *dev) 275 { 276 unsigned char addr[MAX_ADDR_LEN]; 277 struct netdev_hw_addr *ha; 278 int err; 279 280 /* rtnl_mutex must be held here */ 281 282 __hw_addr_init(&dev->dev_addrs); 283 memset(addr, 0, sizeof(addr)); 284 err = __hw_addr_add(&dev->dev_addrs, addr, sizeof(addr), 285 NETDEV_HW_ADDR_T_LAN); 286 if (!err) { 287 /* 288 * Get the first (previously created) address from the list 289 * and set dev_addr pointer to this location. 290 */ 291 ha = list_first_entry(&dev->dev_addrs.list, 292 struct netdev_hw_addr, list); 293 dev->dev_addr = ha->addr; 294 } 295 return err; 296 } 297 EXPORT_SYMBOL(dev_addr_init); 298 299 /** 300 * dev_addr_add - Add a device address 301 * @dev: device 302 * @addr: address to add 303 * @addr_type: address type 304 * 305 * Add a device address to the device or increase the reference count if 306 * it already exists. 307 * 308 * The caller must hold the rtnl_mutex. 309 */ 310 int dev_addr_add(struct net_device *dev, const unsigned char *addr, 311 unsigned char addr_type) 312 { 313 int err; 314 315 ASSERT_RTNL(); 316 317 err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type); 318 if (!err) 319 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 320 return err; 321 } 322 EXPORT_SYMBOL(dev_addr_add); 323 324 /** 325 * dev_addr_del - Release a device address. 326 * @dev: device 327 * @addr: address to delete 328 * @addr_type: address type 329 * 330 * Release reference to a device address and remove it from the device 331 * if the reference count drops to zero. 332 * 333 * The caller must hold the rtnl_mutex. 334 */ 335 int dev_addr_del(struct net_device *dev, const unsigned char *addr, 336 unsigned char addr_type) 337 { 338 int err; 339 struct netdev_hw_addr *ha; 340 341 ASSERT_RTNL(); 342 343 /* 344 * We can not remove the first address from the list because 345 * dev->dev_addr points to that. 346 */ 347 ha = list_first_entry(&dev->dev_addrs.list, 348 struct netdev_hw_addr, list); 349 if (!memcmp(ha->addr, addr, dev->addr_len) && 350 ha->type == addr_type && ha->refcount == 1) 351 return -ENOENT; 352 353 err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len, 354 addr_type); 355 if (!err) 356 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 357 return err; 358 } 359 EXPORT_SYMBOL(dev_addr_del); 360 361 /* 362 * Unicast list handling functions 363 */ 364 365 /** 366 * dev_uc_add_excl - Add a global secondary unicast address 367 * @dev: device 368 * @addr: address to add 369 */ 370 int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr) 371 { 372 struct netdev_hw_addr *ha; 373 int err; 374 375 netif_addr_lock_bh(dev); 376 list_for_each_entry(ha, &dev->uc.list, list) { 377 if (!memcmp(ha->addr, addr, dev->addr_len) && 378 ha->type == NETDEV_HW_ADDR_T_UNICAST) { 379 err = -EEXIST; 380 goto out; 381 } 382 } 383 err = __hw_addr_create_ex(&dev->uc, addr, dev->addr_len, 384 NETDEV_HW_ADDR_T_UNICAST, true, false); 385 if (!err) 386 __dev_set_rx_mode(dev); 387 out: 388 netif_addr_unlock_bh(dev); 389 return err; 390 } 391 EXPORT_SYMBOL(dev_uc_add_excl); 392 393 /** 394 * dev_uc_add - Add a secondary unicast address 395 * @dev: device 396 * @addr: address to add 397 * 398 * Add a secondary unicast address to the device or increase 399 * the reference count if it already exists. 400 */ 401 int dev_uc_add(struct net_device *dev, const unsigned char *addr) 402 { 403 int err; 404 405 netif_addr_lock_bh(dev); 406 err = __hw_addr_add(&dev->uc, addr, dev->addr_len, 407 NETDEV_HW_ADDR_T_UNICAST); 408 if (!err) 409 __dev_set_rx_mode(dev); 410 netif_addr_unlock_bh(dev); 411 return err; 412 } 413 EXPORT_SYMBOL(dev_uc_add); 414 415 /** 416 * dev_uc_del - Release secondary unicast address. 417 * @dev: device 418 * @addr: address to delete 419 * 420 * Release reference to a secondary unicast address and remove it 421 * from the device if the reference count drops to zero. 422 */ 423 int dev_uc_del(struct net_device *dev, const unsigned char *addr) 424 { 425 int err; 426 427 netif_addr_lock_bh(dev); 428 err = __hw_addr_del(&dev->uc, addr, dev->addr_len, 429 NETDEV_HW_ADDR_T_UNICAST); 430 if (!err) 431 __dev_set_rx_mode(dev); 432 netif_addr_unlock_bh(dev); 433 return err; 434 } 435 EXPORT_SYMBOL(dev_uc_del); 436 437 /** 438 * dev_uc_sync - Synchronize device's unicast list to another device 439 * @to: destination device 440 * @from: source device 441 * 442 * Add newly added addresses to the destination device and release 443 * addresses that have no users left. The source device must be 444 * locked by netif_addr_lock_bh. 445 * 446 * This function is intended to be called from the dev->set_rx_mode 447 * function of layered software devices. This function assumes that 448 * addresses will only ever be synced to the @to devices and no other. 449 */ 450 int dev_uc_sync(struct net_device *to, struct net_device *from) 451 { 452 int err = 0; 453 454 if (to->addr_len != from->addr_len) 455 return -EINVAL; 456 457 netif_addr_lock_nested(to); 458 err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len); 459 if (!err) 460 __dev_set_rx_mode(to); 461 netif_addr_unlock(to); 462 return err; 463 } 464 EXPORT_SYMBOL(dev_uc_sync); 465 466 /** 467 * dev_uc_sync_multiple - Synchronize device's unicast list to another 468 * device, but allow for multiple calls to sync to multiple devices. 469 * @to: destination device 470 * @from: source device 471 * 472 * Add newly added addresses to the destination device and release 473 * addresses that have been deleted from the source. The source device 474 * must be locked by netif_addr_lock_bh. 475 * 476 * This function is intended to be called from the dev->set_rx_mode 477 * function of layered software devices. It allows for a single source 478 * device to be synced to multiple destination devices. 479 */ 480 int dev_uc_sync_multiple(struct net_device *to, struct net_device *from) 481 { 482 int err = 0; 483 484 if (to->addr_len != from->addr_len) 485 return -EINVAL; 486 487 netif_addr_lock_nested(to); 488 err = __hw_addr_sync_multiple(&to->uc, &from->uc, to->addr_len); 489 if (!err) 490 __dev_set_rx_mode(to); 491 netif_addr_unlock(to); 492 return err; 493 } 494 EXPORT_SYMBOL(dev_uc_sync_multiple); 495 496 /** 497 * dev_uc_unsync - Remove synchronized addresses from the destination device 498 * @to: destination device 499 * @from: source device 500 * 501 * Remove all addresses that were added to the destination device by 502 * dev_uc_sync(). This function is intended to be called from the 503 * dev->stop function of layered software devices. 504 */ 505 void dev_uc_unsync(struct net_device *to, struct net_device *from) 506 { 507 if (to->addr_len != from->addr_len) 508 return; 509 510 netif_addr_lock_bh(from); 511 netif_addr_lock_nested(to); 512 __hw_addr_unsync(&to->uc, &from->uc, to->addr_len); 513 __dev_set_rx_mode(to); 514 netif_addr_unlock(to); 515 netif_addr_unlock_bh(from); 516 } 517 EXPORT_SYMBOL(dev_uc_unsync); 518 519 /** 520 * dev_uc_flush - Flush unicast addresses 521 * @dev: device 522 * 523 * Flush unicast addresses. 524 */ 525 void dev_uc_flush(struct net_device *dev) 526 { 527 netif_addr_lock_bh(dev); 528 __hw_addr_flush(&dev->uc); 529 netif_addr_unlock_bh(dev); 530 } 531 EXPORT_SYMBOL(dev_uc_flush); 532 533 /** 534 * dev_uc_flush - Init unicast address list 535 * @dev: device 536 * 537 * Init unicast address list. 538 */ 539 void dev_uc_init(struct net_device *dev) 540 { 541 __hw_addr_init(&dev->uc); 542 } 543 EXPORT_SYMBOL(dev_uc_init); 544 545 /* 546 * Multicast list handling functions 547 */ 548 549 /** 550 * dev_mc_add_excl - Add a global secondary multicast address 551 * @dev: device 552 * @addr: address to add 553 */ 554 int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr) 555 { 556 struct netdev_hw_addr *ha; 557 int err; 558 559 netif_addr_lock_bh(dev); 560 list_for_each_entry(ha, &dev->mc.list, list) { 561 if (!memcmp(ha->addr, addr, dev->addr_len) && 562 ha->type == NETDEV_HW_ADDR_T_MULTICAST) { 563 err = -EEXIST; 564 goto out; 565 } 566 } 567 err = __hw_addr_create_ex(&dev->mc, addr, dev->addr_len, 568 NETDEV_HW_ADDR_T_MULTICAST, true, false); 569 if (!err) 570 __dev_set_rx_mode(dev); 571 out: 572 netif_addr_unlock_bh(dev); 573 return err; 574 } 575 EXPORT_SYMBOL(dev_mc_add_excl); 576 577 static int __dev_mc_add(struct net_device *dev, const unsigned char *addr, 578 bool global) 579 { 580 int err; 581 582 netif_addr_lock_bh(dev); 583 err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len, 584 NETDEV_HW_ADDR_T_MULTICAST, global, false); 585 if (!err) 586 __dev_set_rx_mode(dev); 587 netif_addr_unlock_bh(dev); 588 return err; 589 } 590 /** 591 * dev_mc_add - Add a multicast address 592 * @dev: device 593 * @addr: address to add 594 * 595 * Add a multicast address to the device or increase 596 * the reference count if it already exists. 597 */ 598 int dev_mc_add(struct net_device *dev, const unsigned char *addr) 599 { 600 return __dev_mc_add(dev, addr, false); 601 } 602 EXPORT_SYMBOL(dev_mc_add); 603 604 /** 605 * dev_mc_add_global - Add a global multicast address 606 * @dev: device 607 * @addr: address to add 608 * 609 * Add a global multicast address to the device. 610 */ 611 int dev_mc_add_global(struct net_device *dev, const unsigned char *addr) 612 { 613 return __dev_mc_add(dev, addr, true); 614 } 615 EXPORT_SYMBOL(dev_mc_add_global); 616 617 static int __dev_mc_del(struct net_device *dev, const unsigned char *addr, 618 bool global) 619 { 620 int err; 621 622 netif_addr_lock_bh(dev); 623 err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len, 624 NETDEV_HW_ADDR_T_MULTICAST, global, false); 625 if (!err) 626 __dev_set_rx_mode(dev); 627 netif_addr_unlock_bh(dev); 628 return err; 629 } 630 631 /** 632 * dev_mc_del - Delete a multicast address. 633 * @dev: device 634 * @addr: address to delete 635 * 636 * Release reference to a multicast address and remove it 637 * from the device if the reference count drops to zero. 638 */ 639 int dev_mc_del(struct net_device *dev, const unsigned char *addr) 640 { 641 return __dev_mc_del(dev, addr, false); 642 } 643 EXPORT_SYMBOL(dev_mc_del); 644 645 /** 646 * dev_mc_del_global - Delete a global multicast address. 647 * @dev: device 648 * @addr: address to delete 649 * 650 * Release reference to a multicast address and remove it 651 * from the device if the reference count drops to zero. 652 */ 653 int dev_mc_del_global(struct net_device *dev, const unsigned char *addr) 654 { 655 return __dev_mc_del(dev, addr, true); 656 } 657 EXPORT_SYMBOL(dev_mc_del_global); 658 659 /** 660 * dev_mc_sync - Synchronize device's multicast list to another device 661 * @to: destination device 662 * @from: source device 663 * 664 * Add newly added addresses to the destination device and release 665 * addresses that have no users left. The source device must be 666 * locked by netif_addr_lock_bh. 667 * 668 * This function is intended to be called from the ndo_set_rx_mode 669 * function of layered software devices. 670 */ 671 int dev_mc_sync(struct net_device *to, struct net_device *from) 672 { 673 int err = 0; 674 675 if (to->addr_len != from->addr_len) 676 return -EINVAL; 677 678 netif_addr_lock_nested(to); 679 err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len); 680 if (!err) 681 __dev_set_rx_mode(to); 682 netif_addr_unlock(to); 683 return err; 684 } 685 EXPORT_SYMBOL(dev_mc_sync); 686 687 /** 688 * dev_mc_sync_multiple - Synchronize device's multicast list to another 689 * device, but allow for multiple calls to sync to multiple devices. 690 * @to: destination device 691 * @from: source device 692 * 693 * Add newly added addresses to the destination device and release 694 * addresses that have no users left. The source device must be 695 * locked by netif_addr_lock_bh. 696 * 697 * This function is intended to be called from the ndo_set_rx_mode 698 * function of layered software devices. It allows for a single 699 * source device to be synced to multiple destination devices. 700 */ 701 int dev_mc_sync_multiple(struct net_device *to, struct net_device *from) 702 { 703 int err = 0; 704 705 if (to->addr_len != from->addr_len) 706 return -EINVAL; 707 708 netif_addr_lock_nested(to); 709 err = __hw_addr_sync_multiple(&to->mc, &from->mc, to->addr_len); 710 if (!err) 711 __dev_set_rx_mode(to); 712 netif_addr_unlock(to); 713 return err; 714 } 715 EXPORT_SYMBOL(dev_mc_sync_multiple); 716 717 /** 718 * dev_mc_unsync - Remove synchronized addresses from the destination device 719 * @to: destination device 720 * @from: source device 721 * 722 * Remove all addresses that were added to the destination device by 723 * dev_mc_sync(). This function is intended to be called from the 724 * dev->stop function of layered software devices. 725 */ 726 void dev_mc_unsync(struct net_device *to, struct net_device *from) 727 { 728 if (to->addr_len != from->addr_len) 729 return; 730 731 netif_addr_lock_bh(from); 732 netif_addr_lock_nested(to); 733 __hw_addr_unsync(&to->mc, &from->mc, to->addr_len); 734 __dev_set_rx_mode(to); 735 netif_addr_unlock(to); 736 netif_addr_unlock_bh(from); 737 } 738 EXPORT_SYMBOL(dev_mc_unsync); 739 740 /** 741 * dev_mc_flush - Flush multicast addresses 742 * @dev: device 743 * 744 * Flush multicast addresses. 745 */ 746 void dev_mc_flush(struct net_device *dev) 747 { 748 netif_addr_lock_bh(dev); 749 __hw_addr_flush(&dev->mc); 750 netif_addr_unlock_bh(dev); 751 } 752 EXPORT_SYMBOL(dev_mc_flush); 753 754 /** 755 * dev_mc_flush - Init multicast address list 756 * @dev: device 757 * 758 * Init multicast address list. 759 */ 760 void dev_mc_init(struct net_device *dev) 761 { 762 __hw_addr_init(&dev->mc); 763 } 764 EXPORT_SYMBOL(dev_mc_init); 765