2 * net/core/dev_addr_lists.c - Functions for handling net device lists
3 * Copyright (c) 2010 Jiri Pirko <jpirko@redhat.com>
5 * This file contains functions for working with unicast, multicast and device
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.
14 #include <linux/netdevice.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/export.h>
17 #include <linux/list.h>
20 * General list handling functions
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,
28 struct netdev_hw_addr *ha;
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);
37 memcpy(ha->addr, addr, addr_len);
40 ha->global_use = global;
42 list_add_tail_rcu(&ha->list, &list->list);
48 static int __hw_addr_add_ex(struct netdev_hw_addr_list *list,
49 const unsigned char *addr, int addr_len,
50 unsigned char addr_type, bool global, bool sync)
52 struct netdev_hw_addr *ha;
54 if (addr_len > MAX_ADDR_LEN)
57 list_for_each_entry(ha, &list->list, list) {
58 if (!memcmp(ha->addr, addr, addr_len) &&
59 ha->type == addr_type) {
61 /* check if addr is already used as global */
65 ha->global_use = true;
78 return __hw_addr_create_ex(list, addr, addr_len, addr_type, global,
82 static int __hw_addr_add(struct netdev_hw_addr_list *list,
83 const unsigned char *addr, int addr_len,
84 unsigned char addr_type)
86 return __hw_addr_add_ex(list, addr, addr_len, addr_type, false, false);
89 static int __hw_addr_del_entry(struct netdev_hw_addr_list *list,
90 struct netdev_hw_addr *ha, bool global,
93 if (global && !ha->global_use)
96 if (sync && !ha->synced)
100 ha->global_use = false;
107 list_del_rcu(&ha->list);
108 kfree_rcu(ha, rcu_head);
113 static int __hw_addr_del_ex(struct netdev_hw_addr_list *list,
114 const unsigned char *addr, int addr_len,
115 unsigned char addr_type, bool global, bool sync)
117 struct netdev_hw_addr *ha;
119 list_for_each_entry(ha, &list->list, list) {
120 if (!memcmp(ha->addr, addr, addr_len) &&
121 (ha->type == addr_type || !addr_type))
122 return __hw_addr_del_entry(list, ha, global, sync);
127 static int __hw_addr_del(struct netdev_hw_addr_list *list,
128 const unsigned char *addr, int addr_len,
129 unsigned char addr_type)
131 return __hw_addr_del_ex(list, addr, addr_len, addr_type, false, false);
134 static int __hw_addr_sync_one(struct netdev_hw_addr_list *to_list,
135 struct netdev_hw_addr *ha,
140 err = __hw_addr_add_ex(to_list, ha->addr, addr_len, ha->type,
150 static void __hw_addr_unsync_one(struct netdev_hw_addr_list *to_list,
151 struct netdev_hw_addr_list *from_list,
152 struct netdev_hw_addr *ha,
157 err = __hw_addr_del_ex(to_list, ha->addr, addr_len, ha->type,
162 __hw_addr_del_entry(from_list, ha, false, true);
165 static int __hw_addr_sync_multiple(struct netdev_hw_addr_list *to_list,
166 struct netdev_hw_addr_list *from_list,
170 struct netdev_hw_addr *ha, *tmp;
172 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
173 if (ha->sync_cnt == ha->refcount) {
174 __hw_addr_unsync_one(to_list, from_list, ha, addr_len);
176 err = __hw_addr_sync_one(to_list, ha, addr_len);
184 int __hw_addr_add_multiple(struct netdev_hw_addr_list *to_list,
185 struct netdev_hw_addr_list *from_list,
186 int addr_len, unsigned char addr_type)
189 struct netdev_hw_addr *ha, *ha2;
192 list_for_each_entry(ha, &from_list->list, list) {
193 type = addr_type ? addr_type : ha->type;
194 err = __hw_addr_add(to_list, ha->addr, addr_len, type);
201 list_for_each_entry(ha2, &from_list->list, list) {
204 type = addr_type ? addr_type : ha2->type;
205 __hw_addr_del(to_list, ha2->addr, addr_len, type);
209 EXPORT_SYMBOL(__hw_addr_add_multiple);
211 void __hw_addr_del_multiple(struct netdev_hw_addr_list *to_list,
212 struct netdev_hw_addr_list *from_list,
213 int addr_len, unsigned char addr_type)
215 struct netdev_hw_addr *ha;
218 list_for_each_entry(ha, &from_list->list, list) {
219 type = addr_type ? addr_type : ha->type;
220 __hw_addr_del(to_list, ha->addr, addr_len, type);
223 EXPORT_SYMBOL(__hw_addr_del_multiple);
225 /* This function only works where there is a strict 1-1 relationship
226 * between source and destionation of they synch. If you ever need to
227 * sync addresses to more then 1 destination, you need to use
228 * __hw_addr_sync_multiple().
230 int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
231 struct netdev_hw_addr_list *from_list,
235 struct netdev_hw_addr *ha, *tmp;
237 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
239 err = __hw_addr_sync_one(to_list, ha, addr_len);
242 } else if (ha->refcount == 1)
243 __hw_addr_unsync_one(to_list, from_list, ha, addr_len);
247 EXPORT_SYMBOL(__hw_addr_sync);
249 void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
250 struct netdev_hw_addr_list *from_list,
253 struct netdev_hw_addr *ha, *tmp;
255 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
257 __hw_addr_unsync_one(to_list, from_list, ha, addr_len);
260 EXPORT_SYMBOL(__hw_addr_unsync);
262 void __hw_addr_flush(struct netdev_hw_addr_list *list)
264 struct netdev_hw_addr *ha, *tmp;
266 list_for_each_entry_safe(ha, tmp, &list->list, list) {
267 list_del_rcu(&ha->list);
268 kfree_rcu(ha, rcu_head);
272 EXPORT_SYMBOL(__hw_addr_flush);
274 void __hw_addr_init(struct netdev_hw_addr_list *list)
276 INIT_LIST_HEAD(&list->list);
279 EXPORT_SYMBOL(__hw_addr_init);
282 * Device addresses handling functions
286 * dev_addr_flush - Flush device address list
289 * Flush device address list and reset ->dev_addr.
291 * The caller must hold the rtnl_mutex.
293 void dev_addr_flush(struct net_device *dev)
295 /* rtnl_mutex must be held here */
297 __hw_addr_flush(&dev->dev_addrs);
298 dev->dev_addr = NULL;
300 EXPORT_SYMBOL(dev_addr_flush);
303 * dev_addr_init - Init device address list
306 * Init device address list and create the first element,
307 * used by ->dev_addr.
309 * The caller must hold the rtnl_mutex.
311 int dev_addr_init(struct net_device *dev)
313 unsigned char addr[MAX_ADDR_LEN];
314 struct netdev_hw_addr *ha;
317 /* rtnl_mutex must be held here */
319 __hw_addr_init(&dev->dev_addrs);
320 memset(addr, 0, sizeof(addr));
321 err = __hw_addr_add(&dev->dev_addrs, addr, sizeof(addr),
322 NETDEV_HW_ADDR_T_LAN);
325 * Get the first (previously created) address from the list
326 * and set dev_addr pointer to this location.
328 ha = list_first_entry(&dev->dev_addrs.list,
329 struct netdev_hw_addr, list);
330 dev->dev_addr = ha->addr;
334 EXPORT_SYMBOL(dev_addr_init);
337 * dev_addr_add - Add a device address
339 * @addr: address to add
340 * @addr_type: address type
342 * Add a device address to the device or increase the reference count if
345 * The caller must hold the rtnl_mutex.
347 int dev_addr_add(struct net_device *dev, const unsigned char *addr,
348 unsigned char addr_type)
354 err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type);
356 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
359 EXPORT_SYMBOL(dev_addr_add);
362 * dev_addr_del - Release a device address.
364 * @addr: address to delete
365 * @addr_type: address type
367 * Release reference to a device address and remove it from the device
368 * if the reference count drops to zero.
370 * The caller must hold the rtnl_mutex.
372 int dev_addr_del(struct net_device *dev, const unsigned char *addr,
373 unsigned char addr_type)
376 struct netdev_hw_addr *ha;
381 * We can not remove the first address from the list because
382 * dev->dev_addr points to that.
384 ha = list_first_entry(&dev->dev_addrs.list,
385 struct netdev_hw_addr, list);
386 if (!memcmp(ha->addr, addr, dev->addr_len) &&
387 ha->type == addr_type && ha->refcount == 1)
390 err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len,
393 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
396 EXPORT_SYMBOL(dev_addr_del);
399 * dev_addr_add_multiple - Add device addresses from another device
400 * @to_dev: device to which addresses will be added
401 * @from_dev: device from which addresses will be added
402 * @addr_type: address type - 0 means type will be used from from_dev
404 * Add device addresses of the one device to another.
406 * The caller must hold the rtnl_mutex.
408 int dev_addr_add_multiple(struct net_device *to_dev,
409 struct net_device *from_dev,
410 unsigned char addr_type)
416 if (from_dev->addr_len != to_dev->addr_len)
418 err = __hw_addr_add_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
419 to_dev->addr_len, addr_type);
421 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
424 EXPORT_SYMBOL(dev_addr_add_multiple);
427 * dev_addr_del_multiple - Delete device addresses by another device
428 * @to_dev: device where the addresses will be deleted
429 * @from_dev: device supplying the addresses to be deleted
430 * @addr_type: address type - 0 means type will be used from from_dev
432 * Deletes addresses in to device by the list of addresses in from device.
434 * The caller must hold the rtnl_mutex.
436 int dev_addr_del_multiple(struct net_device *to_dev,
437 struct net_device *from_dev,
438 unsigned char addr_type)
442 if (from_dev->addr_len != to_dev->addr_len)
444 __hw_addr_del_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
445 to_dev->addr_len, addr_type);
446 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
449 EXPORT_SYMBOL(dev_addr_del_multiple);
452 * Unicast list handling functions
456 * dev_uc_add_excl - Add a global secondary unicast address
458 * @addr: address to add
460 int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr)
462 struct netdev_hw_addr *ha;
465 netif_addr_lock_bh(dev);
466 list_for_each_entry(ha, &dev->uc.list, list) {
467 if (!memcmp(ha->addr, addr, dev->addr_len) &&
468 ha->type == NETDEV_HW_ADDR_T_UNICAST) {
473 err = __hw_addr_create_ex(&dev->uc, addr, dev->addr_len,
474 NETDEV_HW_ADDR_T_UNICAST, true, false);
476 __dev_set_rx_mode(dev);
478 netif_addr_unlock_bh(dev);
481 EXPORT_SYMBOL(dev_uc_add_excl);
484 * dev_uc_add - Add a secondary unicast address
486 * @addr: address to add
488 * Add a secondary unicast address to the device or increase
489 * the reference count if it already exists.
491 int dev_uc_add(struct net_device *dev, const unsigned char *addr)
495 netif_addr_lock_bh(dev);
496 err = __hw_addr_add(&dev->uc, addr, dev->addr_len,
497 NETDEV_HW_ADDR_T_UNICAST);
499 __dev_set_rx_mode(dev);
500 netif_addr_unlock_bh(dev);
503 EXPORT_SYMBOL(dev_uc_add);
506 * dev_uc_del - Release secondary unicast address.
508 * @addr: address to delete
510 * Release reference to a secondary unicast address and remove it
511 * from the device if the reference count drops to zero.
513 int dev_uc_del(struct net_device *dev, const unsigned char *addr)
517 netif_addr_lock_bh(dev);
518 err = __hw_addr_del(&dev->uc, addr, dev->addr_len,
519 NETDEV_HW_ADDR_T_UNICAST);
521 __dev_set_rx_mode(dev);
522 netif_addr_unlock_bh(dev);
525 EXPORT_SYMBOL(dev_uc_del);
528 * dev_uc_sync - Synchronize device's unicast list to another device
529 * @to: destination device
530 * @from: source device
532 * Add newly added addresses to the destination device and release
533 * addresses that have no users left. The source device must be
534 * locked by netif_addr_lock_bh.
536 * This function is intended to be called from the dev->set_rx_mode
537 * function of layered software devices. This function assumes that
538 * addresses will only ever be synced to the @to devices and no other.
540 int dev_uc_sync(struct net_device *to, struct net_device *from)
544 if (to->addr_len != from->addr_len)
547 netif_addr_lock_nested(to);
548 err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
550 __dev_set_rx_mode(to);
551 netif_addr_unlock(to);
554 EXPORT_SYMBOL(dev_uc_sync);
557 * dev_uc_sync_multiple - Synchronize device's unicast list to another
558 * device, but allow for multiple calls to sync to multiple devices.
559 * @to: destination device
560 * @from: source device
562 * Add newly added addresses to the destination device and release
563 * addresses that have been deleted from the source. The source device
564 * must be locked by netif_addr_lock_bh.
566 * This function is intended to be called from the dev->set_rx_mode
567 * function of layered software devices. It allows for a single source
568 * device to be synced to multiple destination devices.
570 int dev_uc_sync_multiple(struct net_device *to, struct net_device *from)
574 if (to->addr_len != from->addr_len)
577 netif_addr_lock_nested(to);
578 err = __hw_addr_sync_multiple(&to->uc, &from->uc, to->addr_len);
580 __dev_set_rx_mode(to);
581 netif_addr_unlock(to);
584 EXPORT_SYMBOL(dev_uc_sync_multiple);
587 * dev_uc_unsync - Remove synchronized addresses from the destination device
588 * @to: destination device
589 * @from: source device
591 * Remove all addresses that were added to the destination device by
592 * dev_uc_sync(). This function is intended to be called from the
593 * dev->stop function of layered software devices.
595 void dev_uc_unsync(struct net_device *to, struct net_device *from)
597 if (to->addr_len != from->addr_len)
600 netif_addr_lock_bh(from);
601 netif_addr_lock_nested(to);
602 __hw_addr_unsync(&to->uc, &from->uc, to->addr_len);
603 __dev_set_rx_mode(to);
604 netif_addr_unlock(to);
605 netif_addr_unlock_bh(from);
607 EXPORT_SYMBOL(dev_uc_unsync);
610 * dev_uc_flush - Flush unicast addresses
613 * Flush unicast addresses.
615 void dev_uc_flush(struct net_device *dev)
617 netif_addr_lock_bh(dev);
618 __hw_addr_flush(&dev->uc);
619 netif_addr_unlock_bh(dev);
621 EXPORT_SYMBOL(dev_uc_flush);
624 * dev_uc_flush - Init unicast address list
627 * Init unicast address list.
629 void dev_uc_init(struct net_device *dev)
631 __hw_addr_init(&dev->uc);
633 EXPORT_SYMBOL(dev_uc_init);
636 * Multicast list handling functions
640 * dev_mc_add_excl - Add a global secondary multicast address
642 * @addr: address to add
644 int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr)
646 struct netdev_hw_addr *ha;
649 netif_addr_lock_bh(dev);
650 list_for_each_entry(ha, &dev->mc.list, list) {
651 if (!memcmp(ha->addr, addr, dev->addr_len) &&
652 ha->type == NETDEV_HW_ADDR_T_MULTICAST) {
657 err = __hw_addr_create_ex(&dev->mc, addr, dev->addr_len,
658 NETDEV_HW_ADDR_T_MULTICAST, true, false);
660 __dev_set_rx_mode(dev);
662 netif_addr_unlock_bh(dev);
665 EXPORT_SYMBOL(dev_mc_add_excl);
667 static int __dev_mc_add(struct net_device *dev, const unsigned char *addr,
672 netif_addr_lock_bh(dev);
673 err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
674 NETDEV_HW_ADDR_T_MULTICAST, global, false);
676 __dev_set_rx_mode(dev);
677 netif_addr_unlock_bh(dev);
681 * dev_mc_add - Add a multicast address
683 * @addr: address to add
685 * Add a multicast address to the device or increase
686 * the reference count if it already exists.
688 int dev_mc_add(struct net_device *dev, const unsigned char *addr)
690 return __dev_mc_add(dev, addr, false);
692 EXPORT_SYMBOL(dev_mc_add);
695 * dev_mc_add_global - Add a global multicast address
697 * @addr: address to add
699 * Add a global multicast address to the device.
701 int dev_mc_add_global(struct net_device *dev, const unsigned char *addr)
703 return __dev_mc_add(dev, addr, true);
705 EXPORT_SYMBOL(dev_mc_add_global);
707 static int __dev_mc_del(struct net_device *dev, const unsigned char *addr,
712 netif_addr_lock_bh(dev);
713 err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
714 NETDEV_HW_ADDR_T_MULTICAST, global, false);
716 __dev_set_rx_mode(dev);
717 netif_addr_unlock_bh(dev);
722 * dev_mc_del - Delete a multicast address.
724 * @addr: address to delete
726 * Release reference to a multicast address and remove it
727 * from the device if the reference count drops to zero.
729 int dev_mc_del(struct net_device *dev, const unsigned char *addr)
731 return __dev_mc_del(dev, addr, false);
733 EXPORT_SYMBOL(dev_mc_del);
736 * dev_mc_del_global - Delete a global multicast address.
738 * @addr: address to delete
740 * Release reference to a multicast address and remove it
741 * from the device if the reference count drops to zero.
743 int dev_mc_del_global(struct net_device *dev, const unsigned char *addr)
745 return __dev_mc_del(dev, addr, true);
747 EXPORT_SYMBOL(dev_mc_del_global);
750 * dev_mc_sync - Synchronize device's unicast list to another device
751 * @to: destination device
752 * @from: source device
754 * Add newly added addresses to the destination device and release
755 * addresses that have no users left. The source device must be
756 * locked by netif_addr_lock_bh.
758 * This function is intended to be called from the ndo_set_rx_mode
759 * function of layered software devices.
761 int dev_mc_sync(struct net_device *to, struct net_device *from)
765 if (to->addr_len != from->addr_len)
768 netif_addr_lock_nested(to);
769 err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
771 __dev_set_rx_mode(to);
772 netif_addr_unlock(to);
775 EXPORT_SYMBOL(dev_mc_sync);
778 * dev_mc_sync_multiple - Synchronize device's unicast list to another
779 * device, but allow for multiple calls to sync to multiple devices.
780 * @to: destination device
781 * @from: source device
783 * Add newly added addresses to the destination device and release
784 * addresses that have no users left. The source device must be
785 * locked by netif_addr_lock_bh.
787 * This function is intended to be called from the ndo_set_rx_mode
788 * function of layered software devices. It allows for a single
789 * source device to be synced to multiple destination devices.
791 int dev_mc_sync_multiple(struct net_device *to, struct net_device *from)
795 if (to->addr_len != from->addr_len)
798 netif_addr_lock_nested(to);
799 err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
801 __dev_set_rx_mode(to);
802 netif_addr_unlock(to);
805 EXPORT_SYMBOL(dev_mc_sync_multiple);
808 * dev_mc_unsync - Remove synchronized addresses from the destination device
809 * @to: destination device
810 * @from: source device
812 * Remove all addresses that were added to the destination device by
813 * dev_mc_sync(). This function is intended to be called from the
814 * dev->stop function of layered software devices.
816 void dev_mc_unsync(struct net_device *to, struct net_device *from)
818 if (to->addr_len != from->addr_len)
821 netif_addr_lock_bh(from);
822 netif_addr_lock_nested(to);
823 __hw_addr_unsync(&to->mc, &from->mc, to->addr_len);
824 __dev_set_rx_mode(to);
825 netif_addr_unlock(to);
826 netif_addr_unlock_bh(from);
828 EXPORT_SYMBOL(dev_mc_unsync);
831 * dev_mc_flush - Flush multicast addresses
834 * Flush multicast addresses.
836 void dev_mc_flush(struct net_device *dev)
838 netif_addr_lock_bh(dev);
839 __hw_addr_flush(&dev->mc);
840 netif_addr_unlock_bh(dev);
842 EXPORT_SYMBOL(dev_mc_flush);
845 * dev_mc_flush - Init multicast address list
848 * Init multicast address list.
850 void dev_mc_init(struct net_device *dev)
852 __hw_addr_init(&dev->mc);
854 EXPORT_SYMBOL(dev_mc_init);