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;
43 list_add_tail_rcu(&ha->list, &list->list);
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)
53 struct netdev_hw_addr *ha;
55 if (addr_len > MAX_ADDR_LEN)
58 list_for_each_entry(ha, &list->list, list) {
59 if (!memcmp(ha->addr, addr, addr_len) &&
60 ha->type == addr_type) {
62 /* check if addr is already used as global */
66 ha->global_use = true;
79 return __hw_addr_create_ex(list, addr, addr_len, addr_type, global,
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)
87 return __hw_addr_add_ex(list, addr, addr_len, addr_type, false, false);
90 static int __hw_addr_del_entry(struct netdev_hw_addr_list *list,
91 struct netdev_hw_addr *ha, bool global,
94 if (global && !ha->global_use)
97 if (sync && !ha->synced)
101 ha->global_use = false;
108 list_del_rcu(&ha->list);
109 kfree_rcu(ha, rcu_head);
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)
118 struct netdev_hw_addr *ha;
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);
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)
132 return __hw_addr_del_ex(list, addr, addr_len, addr_type, false, false);
135 static int __hw_addr_sync_one(struct netdev_hw_addr_list *to_list,
136 struct netdev_hw_addr *ha,
141 err = __hw_addr_add_ex(to_list, ha->addr, addr_len, ha->type,
143 if (err && err != -EEXIST)
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,
161 err = __hw_addr_del_ex(to_list, ha->addr, addr_len, ha->type,
166 /* address on from list is not marked synced */
167 __hw_addr_del_entry(from_list, ha, false, false);
170 static int __hw_addr_sync_multiple(struct netdev_hw_addr_list *to_list,
171 struct netdev_hw_addr_list *from_list,
175 struct netdev_hw_addr *ha, *tmp;
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);
181 err = __hw_addr_sync_one(to_list, ha, addr_len);
189 int __hw_addr_add_multiple(struct netdev_hw_addr_list *to_list,
190 struct netdev_hw_addr_list *from_list,
191 int addr_len, unsigned char addr_type)
194 struct netdev_hw_addr *ha, *ha2;
197 list_for_each_entry(ha, &from_list->list, list) {
198 type = addr_type ? addr_type : ha->type;
199 err = __hw_addr_add(to_list, ha->addr, addr_len, type);
206 list_for_each_entry(ha2, &from_list->list, list) {
209 type = addr_type ? addr_type : ha2->type;
210 __hw_addr_del(to_list, ha2->addr, addr_len, type);
214 EXPORT_SYMBOL(__hw_addr_add_multiple);
216 void __hw_addr_del_multiple(struct netdev_hw_addr_list *to_list,
217 struct netdev_hw_addr_list *from_list,
218 int addr_len, unsigned char addr_type)
220 struct netdev_hw_addr *ha;
223 list_for_each_entry(ha, &from_list->list, list) {
224 type = addr_type ? addr_type : ha->type;
225 __hw_addr_del(to_list, ha->addr, addr_len, type);
228 EXPORT_SYMBOL(__hw_addr_del_multiple);
230 /* This function only works where there is a strict 1-1 relationship
231 * between source and destionation of they synch. If you ever need to
232 * sync addresses to more then 1 destination, you need to use
233 * __hw_addr_sync_multiple().
235 int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
236 struct netdev_hw_addr_list *from_list,
240 struct netdev_hw_addr *ha, *tmp;
242 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
244 err = __hw_addr_sync_one(to_list, ha, addr_len);
247 } else if (ha->refcount == 1)
248 __hw_addr_unsync_one(to_list, from_list, ha, addr_len);
252 EXPORT_SYMBOL(__hw_addr_sync);
254 void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
255 struct netdev_hw_addr_list *from_list,
258 struct netdev_hw_addr *ha, *tmp;
260 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
262 __hw_addr_unsync_one(to_list, from_list, ha, addr_len);
265 EXPORT_SYMBOL(__hw_addr_unsync);
267 void __hw_addr_flush(struct netdev_hw_addr_list *list)
269 struct netdev_hw_addr *ha, *tmp;
271 list_for_each_entry_safe(ha, tmp, &list->list, list) {
272 list_del_rcu(&ha->list);
273 kfree_rcu(ha, rcu_head);
277 EXPORT_SYMBOL(__hw_addr_flush);
279 void __hw_addr_init(struct netdev_hw_addr_list *list)
281 INIT_LIST_HEAD(&list->list);
284 EXPORT_SYMBOL(__hw_addr_init);
287 * Device addresses handling functions
291 * dev_addr_flush - Flush device address list
294 * Flush device address list and reset ->dev_addr.
296 * The caller must hold the rtnl_mutex.
298 void dev_addr_flush(struct net_device *dev)
300 /* rtnl_mutex must be held here */
302 __hw_addr_flush(&dev->dev_addrs);
303 dev->dev_addr = NULL;
305 EXPORT_SYMBOL(dev_addr_flush);
308 * dev_addr_init - Init device address list
311 * Init device address list and create the first element,
312 * used by ->dev_addr.
314 * The caller must hold the rtnl_mutex.
316 int dev_addr_init(struct net_device *dev)
318 unsigned char addr[MAX_ADDR_LEN];
319 struct netdev_hw_addr *ha;
322 /* rtnl_mutex must be held here */
324 __hw_addr_init(&dev->dev_addrs);
325 memset(addr, 0, sizeof(addr));
326 err = __hw_addr_add(&dev->dev_addrs, addr, sizeof(addr),
327 NETDEV_HW_ADDR_T_LAN);
330 * Get the first (previously created) address from the list
331 * and set dev_addr pointer to this location.
333 ha = list_first_entry(&dev->dev_addrs.list,
334 struct netdev_hw_addr, list);
335 dev->dev_addr = ha->addr;
339 EXPORT_SYMBOL(dev_addr_init);
342 * dev_addr_add - Add a device address
344 * @addr: address to add
345 * @addr_type: address type
347 * Add a device address to the device or increase the reference count if
350 * The caller must hold the rtnl_mutex.
352 int dev_addr_add(struct net_device *dev, const unsigned char *addr,
353 unsigned char addr_type)
359 err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type);
361 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
364 EXPORT_SYMBOL(dev_addr_add);
367 * dev_addr_del - Release a device address.
369 * @addr: address to delete
370 * @addr_type: address type
372 * Release reference to a device address and remove it from the device
373 * if the reference count drops to zero.
375 * The caller must hold the rtnl_mutex.
377 int dev_addr_del(struct net_device *dev, const unsigned char *addr,
378 unsigned char addr_type)
381 struct netdev_hw_addr *ha;
386 * We can not remove the first address from the list because
387 * dev->dev_addr points to that.
389 ha = list_first_entry(&dev->dev_addrs.list,
390 struct netdev_hw_addr, list);
391 if (!memcmp(ha->addr, addr, dev->addr_len) &&
392 ha->type == addr_type && ha->refcount == 1)
395 err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len,
398 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
401 EXPORT_SYMBOL(dev_addr_del);
404 * dev_addr_add_multiple - Add device addresses from another device
405 * @to_dev: device to which addresses will be added
406 * @from_dev: device from which addresses will be added
407 * @addr_type: address type - 0 means type will be used from from_dev
409 * Add device addresses of the one device to another.
411 * The caller must hold the rtnl_mutex.
413 int dev_addr_add_multiple(struct net_device *to_dev,
414 struct net_device *from_dev,
415 unsigned char addr_type)
421 if (from_dev->addr_len != to_dev->addr_len)
423 err = __hw_addr_add_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
424 to_dev->addr_len, addr_type);
426 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
429 EXPORT_SYMBOL(dev_addr_add_multiple);
432 * dev_addr_del_multiple - Delete device addresses by another device
433 * @to_dev: device where the addresses will be deleted
434 * @from_dev: device supplying the addresses to be deleted
435 * @addr_type: address type - 0 means type will be used from from_dev
437 * Deletes addresses in to device by the list of addresses in from device.
439 * The caller must hold the rtnl_mutex.
441 int dev_addr_del_multiple(struct net_device *to_dev,
442 struct net_device *from_dev,
443 unsigned char addr_type)
447 if (from_dev->addr_len != to_dev->addr_len)
449 __hw_addr_del_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
450 to_dev->addr_len, addr_type);
451 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
454 EXPORT_SYMBOL(dev_addr_del_multiple);
457 * Unicast list handling functions
461 * dev_uc_add_excl - Add a global secondary unicast address
463 * @addr: address to add
465 int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr)
467 struct netdev_hw_addr *ha;
470 netif_addr_lock_bh(dev);
471 list_for_each_entry(ha, &dev->uc.list, list) {
472 if (!memcmp(ha->addr, addr, dev->addr_len) &&
473 ha->type == NETDEV_HW_ADDR_T_UNICAST) {
478 err = __hw_addr_create_ex(&dev->uc, addr, dev->addr_len,
479 NETDEV_HW_ADDR_T_UNICAST, true, false);
481 __dev_set_rx_mode(dev);
483 netif_addr_unlock_bh(dev);
486 EXPORT_SYMBOL(dev_uc_add_excl);
489 * dev_uc_add - Add a secondary unicast address
491 * @addr: address to add
493 * Add a secondary unicast address to the device or increase
494 * the reference count if it already exists.
496 int dev_uc_add(struct net_device *dev, const unsigned char *addr)
500 netif_addr_lock_bh(dev);
501 err = __hw_addr_add(&dev->uc, addr, dev->addr_len,
502 NETDEV_HW_ADDR_T_UNICAST);
504 __dev_set_rx_mode(dev);
505 netif_addr_unlock_bh(dev);
508 EXPORT_SYMBOL(dev_uc_add);
511 * dev_uc_del - Release secondary unicast address.
513 * @addr: address to delete
515 * Release reference to a secondary unicast address and remove it
516 * from the device if the reference count drops to zero.
518 int dev_uc_del(struct net_device *dev, const unsigned char *addr)
522 netif_addr_lock_bh(dev);
523 err = __hw_addr_del(&dev->uc, addr, dev->addr_len,
524 NETDEV_HW_ADDR_T_UNICAST);
526 __dev_set_rx_mode(dev);
527 netif_addr_unlock_bh(dev);
530 EXPORT_SYMBOL(dev_uc_del);
533 * dev_uc_sync - Synchronize device's unicast list to another device
534 * @to: destination device
535 * @from: source device
537 * Add newly added addresses to the destination device and release
538 * addresses that have no users left. The source device must be
539 * locked by netif_addr_lock_bh.
541 * This function is intended to be called from the dev->set_rx_mode
542 * function of layered software devices. This function assumes that
543 * addresses will only ever be synced to the @to devices and no other.
545 int dev_uc_sync(struct net_device *to, struct net_device *from)
549 if (to->addr_len != from->addr_len)
552 netif_addr_lock_nested(to);
553 err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
555 __dev_set_rx_mode(to);
556 netif_addr_unlock(to);
559 EXPORT_SYMBOL(dev_uc_sync);
562 * dev_uc_sync_multiple - Synchronize device's unicast list to another
563 * device, but allow for multiple calls to sync to multiple devices.
564 * @to: destination device
565 * @from: source device
567 * Add newly added addresses to the destination device and release
568 * addresses that have been deleted from the source. The source device
569 * must be locked by netif_addr_lock_bh.
571 * This function is intended to be called from the dev->set_rx_mode
572 * function of layered software devices. It allows for a single source
573 * device to be synced to multiple destination devices.
575 int dev_uc_sync_multiple(struct net_device *to, struct net_device *from)
579 if (to->addr_len != from->addr_len)
582 netif_addr_lock_nested(to);
583 err = __hw_addr_sync_multiple(&to->uc, &from->uc, to->addr_len);
585 __dev_set_rx_mode(to);
586 netif_addr_unlock(to);
589 EXPORT_SYMBOL(dev_uc_sync_multiple);
592 * dev_uc_unsync - Remove synchronized addresses from the destination device
593 * @to: destination device
594 * @from: source device
596 * Remove all addresses that were added to the destination device by
597 * dev_uc_sync(). This function is intended to be called from the
598 * dev->stop function of layered software devices.
600 void dev_uc_unsync(struct net_device *to, struct net_device *from)
602 if (to->addr_len != from->addr_len)
605 netif_addr_lock_bh(from);
606 netif_addr_lock_nested(to);
607 __hw_addr_unsync(&to->uc, &from->uc, to->addr_len);
608 __dev_set_rx_mode(to);
609 netif_addr_unlock(to);
610 netif_addr_unlock_bh(from);
612 EXPORT_SYMBOL(dev_uc_unsync);
615 * dev_uc_flush - Flush unicast addresses
618 * Flush unicast addresses.
620 void dev_uc_flush(struct net_device *dev)
622 netif_addr_lock_bh(dev);
623 __hw_addr_flush(&dev->uc);
624 netif_addr_unlock_bh(dev);
626 EXPORT_SYMBOL(dev_uc_flush);
629 * dev_uc_flush - Init unicast address list
632 * Init unicast address list.
634 void dev_uc_init(struct net_device *dev)
636 __hw_addr_init(&dev->uc);
638 EXPORT_SYMBOL(dev_uc_init);
641 * Multicast list handling functions
645 * dev_mc_add_excl - Add a global secondary multicast address
647 * @addr: address to add
649 int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr)
651 struct netdev_hw_addr *ha;
654 netif_addr_lock_bh(dev);
655 list_for_each_entry(ha, &dev->mc.list, list) {
656 if (!memcmp(ha->addr, addr, dev->addr_len) &&
657 ha->type == NETDEV_HW_ADDR_T_MULTICAST) {
662 err = __hw_addr_create_ex(&dev->mc, addr, dev->addr_len,
663 NETDEV_HW_ADDR_T_MULTICAST, true, false);
665 __dev_set_rx_mode(dev);
667 netif_addr_unlock_bh(dev);
670 EXPORT_SYMBOL(dev_mc_add_excl);
672 static int __dev_mc_add(struct net_device *dev, const unsigned char *addr,
677 netif_addr_lock_bh(dev);
678 err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
679 NETDEV_HW_ADDR_T_MULTICAST, global, false);
681 __dev_set_rx_mode(dev);
682 netif_addr_unlock_bh(dev);
686 * dev_mc_add - Add a multicast address
688 * @addr: address to add
690 * Add a multicast address to the device or increase
691 * the reference count if it already exists.
693 int dev_mc_add(struct net_device *dev, const unsigned char *addr)
695 return __dev_mc_add(dev, addr, false);
697 EXPORT_SYMBOL(dev_mc_add);
700 * dev_mc_add_global - Add a global multicast address
702 * @addr: address to add
704 * Add a global multicast address to the device.
706 int dev_mc_add_global(struct net_device *dev, const unsigned char *addr)
708 return __dev_mc_add(dev, addr, true);
710 EXPORT_SYMBOL(dev_mc_add_global);
712 static int __dev_mc_del(struct net_device *dev, const unsigned char *addr,
717 netif_addr_lock_bh(dev);
718 err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
719 NETDEV_HW_ADDR_T_MULTICAST, global, false);
721 __dev_set_rx_mode(dev);
722 netif_addr_unlock_bh(dev);
727 * dev_mc_del - Delete a multicast address.
729 * @addr: address to delete
731 * Release reference to a multicast address and remove it
732 * from the device if the reference count drops to zero.
734 int dev_mc_del(struct net_device *dev, const unsigned char *addr)
736 return __dev_mc_del(dev, addr, false);
738 EXPORT_SYMBOL(dev_mc_del);
741 * dev_mc_del_global - Delete a global multicast address.
743 * @addr: address to delete
745 * Release reference to a multicast address and remove it
746 * from the device if the reference count drops to zero.
748 int dev_mc_del_global(struct net_device *dev, const unsigned char *addr)
750 return __dev_mc_del(dev, addr, true);
752 EXPORT_SYMBOL(dev_mc_del_global);
755 * dev_mc_sync - Synchronize device's unicast list to another device
756 * @to: destination device
757 * @from: source device
759 * Add newly added addresses to the destination device and release
760 * addresses that have no users left. The source device must be
761 * locked by netif_addr_lock_bh.
763 * This function is intended to be called from the ndo_set_rx_mode
764 * function of layered software devices.
766 int dev_mc_sync(struct net_device *to, struct net_device *from)
770 if (to->addr_len != from->addr_len)
773 netif_addr_lock_nested(to);
774 err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
776 __dev_set_rx_mode(to);
777 netif_addr_unlock(to);
780 EXPORT_SYMBOL(dev_mc_sync);
783 * dev_mc_sync_multiple - Synchronize device's unicast list to another
784 * device, but allow for multiple calls to sync to multiple devices.
785 * @to: destination device
786 * @from: source device
788 * Add newly added addresses to the destination device and release
789 * addresses that have no users left. The source device must be
790 * locked by netif_addr_lock_bh.
792 * This function is intended to be called from the ndo_set_rx_mode
793 * function of layered software devices. It allows for a single
794 * source device to be synced to multiple destination devices.
796 int dev_mc_sync_multiple(struct net_device *to, struct net_device *from)
800 if (to->addr_len != from->addr_len)
803 netif_addr_lock_nested(to);
804 err = __hw_addr_sync_multiple(&to->mc, &from->mc, to->addr_len);
806 __dev_set_rx_mode(to);
807 netif_addr_unlock(to);
810 EXPORT_SYMBOL(dev_mc_sync_multiple);
813 * dev_mc_unsync - Remove synchronized addresses from the destination device
814 * @to: destination device
815 * @from: source device
817 * Remove all addresses that were added to the destination device by
818 * dev_mc_sync(). This function is intended to be called from the
819 * dev->stop function of layered software devices.
821 void dev_mc_unsync(struct net_device *to, struct net_device *from)
823 if (to->addr_len != from->addr_len)
826 netif_addr_lock_bh(from);
827 netif_addr_lock_nested(to);
828 __hw_addr_unsync(&to->mc, &from->mc, to->addr_len);
829 __dev_set_rx_mode(to);
830 netif_addr_unlock(to);
831 netif_addr_unlock_bh(from);
833 EXPORT_SYMBOL(dev_mc_unsync);
836 * dev_mc_flush - Flush multicast addresses
839 * Flush multicast addresses.
841 void dev_mc_flush(struct net_device *dev)
843 netif_addr_lock_bh(dev);
844 __hw_addr_flush(&dev->mc);
845 netif_addr_unlock_bh(dev);
847 EXPORT_SYMBOL(dev_mc_flush);
850 * dev_mc_flush - Init multicast address list
853 * Init multicast address list.
855 void dev_mc_init(struct net_device *dev)
857 __hw_addr_init(&dev->mc);
859 EXPORT_SYMBOL(dev_mc_init);