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