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,
151 static void __hw_addr_unsync_one(struct netdev_hw_addr_list *to_list,
152 struct netdev_hw_addr_list *from_list,
153 struct netdev_hw_addr *ha,
158 err = __hw_addr_del_ex(to_list, ha->addr, addr_len, ha->type,
163 /* address on from list is not marked synced */
164 __hw_addr_del_entry(from_list, ha, false, false);
167 static int __hw_addr_sync_multiple(struct netdev_hw_addr_list *to_list,
168 struct netdev_hw_addr_list *from_list,
172 struct netdev_hw_addr *ha, *tmp;
174 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
175 if (ha->sync_cnt == ha->refcount) {
176 __hw_addr_unsync_one(to_list, from_list, ha, addr_len);
178 err = __hw_addr_sync_one(to_list, ha, addr_len);
186 int __hw_addr_add_multiple(struct netdev_hw_addr_list *to_list,
187 struct netdev_hw_addr_list *from_list,
188 int addr_len, unsigned char addr_type)
191 struct netdev_hw_addr *ha, *ha2;
194 list_for_each_entry(ha, &from_list->list, list) {
195 type = addr_type ? addr_type : ha->type;
196 err = __hw_addr_add(to_list, ha->addr, addr_len, type);
203 list_for_each_entry(ha2, &from_list->list, list) {
206 type = addr_type ? addr_type : ha2->type;
207 __hw_addr_del(to_list, ha2->addr, addr_len, type);
211 EXPORT_SYMBOL(__hw_addr_add_multiple);
213 void __hw_addr_del_multiple(struct netdev_hw_addr_list *to_list,
214 struct netdev_hw_addr_list *from_list,
215 int addr_len, unsigned char addr_type)
217 struct netdev_hw_addr *ha;
220 list_for_each_entry(ha, &from_list->list, list) {
221 type = addr_type ? addr_type : ha->type;
222 __hw_addr_del(to_list, ha->addr, addr_len, type);
225 EXPORT_SYMBOL(__hw_addr_del_multiple);
227 /* This function only works where there is a strict 1-1 relationship
228 * between source and destionation of they synch. If you ever need to
229 * sync addresses to more then 1 destination, you need to use
230 * __hw_addr_sync_multiple().
232 int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
233 struct netdev_hw_addr_list *from_list,
237 struct netdev_hw_addr *ha, *tmp;
239 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
241 err = __hw_addr_sync_one(to_list, ha, addr_len);
244 } else if (ha->refcount == 1)
245 __hw_addr_unsync_one(to_list, from_list, ha, addr_len);
249 EXPORT_SYMBOL(__hw_addr_sync);
251 void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
252 struct netdev_hw_addr_list *from_list,
255 struct netdev_hw_addr *ha, *tmp;
257 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
259 __hw_addr_unsync_one(to_list, from_list, ha, addr_len);
262 EXPORT_SYMBOL(__hw_addr_unsync);
264 void __hw_addr_flush(struct netdev_hw_addr_list *list)
266 struct netdev_hw_addr *ha, *tmp;
268 list_for_each_entry_safe(ha, tmp, &list->list, list) {
269 list_del_rcu(&ha->list);
270 kfree_rcu(ha, rcu_head);
274 EXPORT_SYMBOL(__hw_addr_flush);
276 void __hw_addr_init(struct netdev_hw_addr_list *list)
278 INIT_LIST_HEAD(&list->list);
281 EXPORT_SYMBOL(__hw_addr_init);
284 * Device addresses handling functions
288 * dev_addr_flush - Flush device address list
291 * Flush device address list and reset ->dev_addr.
293 * The caller must hold the rtnl_mutex.
295 void dev_addr_flush(struct net_device *dev)
297 /* rtnl_mutex must be held here */
299 __hw_addr_flush(&dev->dev_addrs);
300 dev->dev_addr = NULL;
302 EXPORT_SYMBOL(dev_addr_flush);
305 * dev_addr_init - Init device address list
308 * Init device address list and create the first element,
309 * used by ->dev_addr.
311 * The caller must hold the rtnl_mutex.
313 int dev_addr_init(struct net_device *dev)
315 unsigned char addr[MAX_ADDR_LEN];
316 struct netdev_hw_addr *ha;
319 /* rtnl_mutex must be held here */
321 __hw_addr_init(&dev->dev_addrs);
322 memset(addr, 0, sizeof(addr));
323 err = __hw_addr_add(&dev->dev_addrs, addr, sizeof(addr),
324 NETDEV_HW_ADDR_T_LAN);
327 * Get the first (previously created) address from the list
328 * and set dev_addr pointer to this location.
330 ha = list_first_entry(&dev->dev_addrs.list,
331 struct netdev_hw_addr, list);
332 dev->dev_addr = ha->addr;
336 EXPORT_SYMBOL(dev_addr_init);
339 * dev_addr_add - Add a device address
341 * @addr: address to add
342 * @addr_type: address type
344 * Add a device address to the device or increase the reference count if
347 * The caller must hold the rtnl_mutex.
349 int dev_addr_add(struct net_device *dev, const unsigned char *addr,
350 unsigned char addr_type)
356 err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type);
358 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
361 EXPORT_SYMBOL(dev_addr_add);
364 * dev_addr_del - Release a device address.
366 * @addr: address to delete
367 * @addr_type: address type
369 * Release reference to a device address and remove it from the device
370 * if the reference count drops to zero.
372 * The caller must hold the rtnl_mutex.
374 int dev_addr_del(struct net_device *dev, const unsigned char *addr,
375 unsigned char addr_type)
378 struct netdev_hw_addr *ha;
383 * We can not remove the first address from the list because
384 * dev->dev_addr points to that.
386 ha = list_first_entry(&dev->dev_addrs.list,
387 struct netdev_hw_addr, list);
388 if (!memcmp(ha->addr, addr, dev->addr_len) &&
389 ha->type == addr_type && ha->refcount == 1)
392 err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len,
395 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
398 EXPORT_SYMBOL(dev_addr_del);
401 * dev_addr_add_multiple - Add device addresses from another device
402 * @to_dev: device to which addresses will be added
403 * @from_dev: device from which addresses will be added
404 * @addr_type: address type - 0 means type will be used from from_dev
406 * Add device addresses of the one device to another.
408 * The caller must hold the rtnl_mutex.
410 int dev_addr_add_multiple(struct net_device *to_dev,
411 struct net_device *from_dev,
412 unsigned char addr_type)
418 if (from_dev->addr_len != to_dev->addr_len)
420 err = __hw_addr_add_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
421 to_dev->addr_len, addr_type);
423 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
426 EXPORT_SYMBOL(dev_addr_add_multiple);
429 * dev_addr_del_multiple - Delete device addresses by another device
430 * @to_dev: device where the addresses will be deleted
431 * @from_dev: device supplying the addresses to be deleted
432 * @addr_type: address type - 0 means type will be used from from_dev
434 * Deletes addresses in to device by the list of addresses in from device.
436 * The caller must hold the rtnl_mutex.
438 int dev_addr_del_multiple(struct net_device *to_dev,
439 struct net_device *from_dev,
440 unsigned char addr_type)
444 if (from_dev->addr_len != to_dev->addr_len)
446 __hw_addr_del_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
447 to_dev->addr_len, addr_type);
448 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
451 EXPORT_SYMBOL(dev_addr_del_multiple);
454 * Unicast list handling functions
458 * dev_uc_add_excl - Add a global secondary unicast address
460 * @addr: address to add
462 int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr)
464 struct netdev_hw_addr *ha;
467 netif_addr_lock_bh(dev);
468 list_for_each_entry(ha, &dev->uc.list, list) {
469 if (!memcmp(ha->addr, addr, dev->addr_len) &&
470 ha->type == NETDEV_HW_ADDR_T_UNICAST) {
475 err = __hw_addr_create_ex(&dev->uc, addr, dev->addr_len,
476 NETDEV_HW_ADDR_T_UNICAST, true, false);
478 __dev_set_rx_mode(dev);
480 netif_addr_unlock_bh(dev);
483 EXPORT_SYMBOL(dev_uc_add_excl);
486 * dev_uc_add - Add a secondary unicast address
488 * @addr: address to add
490 * Add a secondary unicast address to the device or increase
491 * the reference count if it already exists.
493 int dev_uc_add(struct net_device *dev, const unsigned char *addr)
497 netif_addr_lock_bh(dev);
498 err = __hw_addr_add(&dev->uc, addr, dev->addr_len,
499 NETDEV_HW_ADDR_T_UNICAST);
501 __dev_set_rx_mode(dev);
502 netif_addr_unlock_bh(dev);
505 EXPORT_SYMBOL(dev_uc_add);
508 * dev_uc_del - Release secondary unicast address.
510 * @addr: address to delete
512 * Release reference to a secondary unicast address and remove it
513 * from the device if the reference count drops to zero.
515 int dev_uc_del(struct net_device *dev, const unsigned char *addr)
519 netif_addr_lock_bh(dev);
520 err = __hw_addr_del(&dev->uc, addr, dev->addr_len,
521 NETDEV_HW_ADDR_T_UNICAST);
523 __dev_set_rx_mode(dev);
524 netif_addr_unlock_bh(dev);
527 EXPORT_SYMBOL(dev_uc_del);
530 * dev_uc_sync - Synchronize device's unicast list to another device
531 * @to: destination device
532 * @from: source device
534 * Add newly added addresses to the destination device and release
535 * addresses that have no users left. The source device must be
536 * locked by netif_addr_lock_bh.
538 * This function is intended to be called from the dev->set_rx_mode
539 * function of layered software devices. This function assumes that
540 * addresses will only ever be synced to the @to devices and no other.
542 int dev_uc_sync(struct net_device *to, struct net_device *from)
546 if (to->addr_len != from->addr_len)
549 netif_addr_lock_nested(to);
550 err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
552 __dev_set_rx_mode(to);
553 netif_addr_unlock(to);
556 EXPORT_SYMBOL(dev_uc_sync);
559 * dev_uc_sync_multiple - Synchronize device's unicast list to another
560 * device, but allow for multiple calls to sync to multiple devices.
561 * @to: destination device
562 * @from: source device
564 * Add newly added addresses to the destination device and release
565 * addresses that have been deleted from the source. The source device
566 * must be locked by netif_addr_lock_bh.
568 * This function is intended to be called from the dev->set_rx_mode
569 * function of layered software devices. It allows for a single source
570 * device to be synced to multiple destination devices.
572 int dev_uc_sync_multiple(struct net_device *to, struct net_device *from)
576 if (to->addr_len != from->addr_len)
579 netif_addr_lock_nested(to);
580 err = __hw_addr_sync_multiple(&to->uc, &from->uc, to->addr_len);
582 __dev_set_rx_mode(to);
583 netif_addr_unlock(to);
586 EXPORT_SYMBOL(dev_uc_sync_multiple);
589 * dev_uc_unsync - Remove synchronized addresses from the destination device
590 * @to: destination device
591 * @from: source device
593 * Remove all addresses that were added to the destination device by
594 * dev_uc_sync(). This function is intended to be called from the
595 * dev->stop function of layered software devices.
597 void dev_uc_unsync(struct net_device *to, struct net_device *from)
599 if (to->addr_len != from->addr_len)
602 netif_addr_lock_bh(from);
603 netif_addr_lock_nested(to);
604 __hw_addr_unsync(&to->uc, &from->uc, to->addr_len);
605 __dev_set_rx_mode(to);
606 netif_addr_unlock(to);
607 netif_addr_unlock_bh(from);
609 EXPORT_SYMBOL(dev_uc_unsync);
612 * dev_uc_flush - Flush unicast addresses
615 * Flush unicast addresses.
617 void dev_uc_flush(struct net_device *dev)
619 netif_addr_lock_bh(dev);
620 __hw_addr_flush(&dev->uc);
621 netif_addr_unlock_bh(dev);
623 EXPORT_SYMBOL(dev_uc_flush);
626 * dev_uc_flush - Init unicast address list
629 * Init unicast address list.
631 void dev_uc_init(struct net_device *dev)
633 __hw_addr_init(&dev->uc);
635 EXPORT_SYMBOL(dev_uc_init);
638 * Multicast list handling functions
642 * dev_mc_add_excl - Add a global secondary multicast address
644 * @addr: address to add
646 int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr)
648 struct netdev_hw_addr *ha;
651 netif_addr_lock_bh(dev);
652 list_for_each_entry(ha, &dev->mc.list, list) {
653 if (!memcmp(ha->addr, addr, dev->addr_len) &&
654 ha->type == NETDEV_HW_ADDR_T_MULTICAST) {
659 err = __hw_addr_create_ex(&dev->mc, addr, dev->addr_len,
660 NETDEV_HW_ADDR_T_MULTICAST, true, false);
662 __dev_set_rx_mode(dev);
664 netif_addr_unlock_bh(dev);
667 EXPORT_SYMBOL(dev_mc_add_excl);
669 static int __dev_mc_add(struct net_device *dev, const unsigned char *addr,
674 netif_addr_lock_bh(dev);
675 err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
676 NETDEV_HW_ADDR_T_MULTICAST, global, false);
678 __dev_set_rx_mode(dev);
679 netif_addr_unlock_bh(dev);
683 * dev_mc_add - Add a multicast address
685 * @addr: address to add
687 * Add a multicast address to the device or increase
688 * the reference count if it already exists.
690 int dev_mc_add(struct net_device *dev, const unsigned char *addr)
692 return __dev_mc_add(dev, addr, false);
694 EXPORT_SYMBOL(dev_mc_add);
697 * dev_mc_add_global - Add a global multicast address
699 * @addr: address to add
701 * Add a global multicast address to the device.
703 int dev_mc_add_global(struct net_device *dev, const unsigned char *addr)
705 return __dev_mc_add(dev, addr, true);
707 EXPORT_SYMBOL(dev_mc_add_global);
709 static int __dev_mc_del(struct net_device *dev, const unsigned char *addr,
714 netif_addr_lock_bh(dev);
715 err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
716 NETDEV_HW_ADDR_T_MULTICAST, global, false);
718 __dev_set_rx_mode(dev);
719 netif_addr_unlock_bh(dev);
724 * dev_mc_del - Delete a multicast address.
726 * @addr: address to delete
728 * Release reference to a multicast address and remove it
729 * from the device if the reference count drops to zero.
731 int dev_mc_del(struct net_device *dev, const unsigned char *addr)
733 return __dev_mc_del(dev, addr, false);
735 EXPORT_SYMBOL(dev_mc_del);
738 * dev_mc_del_global - Delete a global multicast address.
740 * @addr: address to delete
742 * Release reference to a multicast address and remove it
743 * from the device if the reference count drops to zero.
745 int dev_mc_del_global(struct net_device *dev, const unsigned char *addr)
747 return __dev_mc_del(dev, addr, true);
749 EXPORT_SYMBOL(dev_mc_del_global);
752 * dev_mc_sync - Synchronize device's unicast list to another device
753 * @to: destination device
754 * @from: source device
756 * Add newly added addresses to the destination device and release
757 * addresses that have no users left. The source device must be
758 * locked by netif_addr_lock_bh.
760 * This function is intended to be called from the ndo_set_rx_mode
761 * function of layered software devices.
763 int dev_mc_sync(struct net_device *to, struct net_device *from)
767 if (to->addr_len != from->addr_len)
770 netif_addr_lock_nested(to);
771 err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
773 __dev_set_rx_mode(to);
774 netif_addr_unlock(to);
777 EXPORT_SYMBOL(dev_mc_sync);
780 * dev_mc_sync_multiple - Synchronize device's unicast list to another
781 * device, but allow for multiple calls to sync to multiple devices.
782 * @to: destination device
783 * @from: source device
785 * Add newly added addresses to the destination device and release
786 * addresses that have no users left. The source device must be
787 * locked by netif_addr_lock_bh.
789 * This function is intended to be called from the ndo_set_rx_mode
790 * function of layered software devices. It allows for a single
791 * source device to be synced to multiple destination devices.
793 int dev_mc_sync_multiple(struct net_device *to, struct net_device *from)
797 if (to->addr_len != from->addr_len)
800 netif_addr_lock_nested(to);
801 err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
803 __dev_set_rx_mode(to);
804 netif_addr_unlock(to);
807 EXPORT_SYMBOL(dev_mc_sync_multiple);
810 * dev_mc_unsync - Remove synchronized addresses from the destination device
811 * @to: destination device
812 * @from: source device
814 * Remove all addresses that were added to the destination device by
815 * dev_mc_sync(). This function is intended to be called from the
816 * dev->stop function of layered software devices.
818 void dev_mc_unsync(struct net_device *to, struct net_device *from)
820 if (to->addr_len != from->addr_len)
823 netif_addr_lock_bh(from);
824 netif_addr_lock_nested(to);
825 __hw_addr_unsync(&to->mc, &from->mc, to->addr_len);
826 __dev_set_rx_mode(to);
827 netif_addr_unlock(to);
828 netif_addr_unlock_bh(from);
830 EXPORT_SYMBOL(dev_mc_unsync);
833 * dev_mc_flush - Flush multicast addresses
836 * Flush multicast addresses.
838 void dev_mc_flush(struct net_device *dev)
840 netif_addr_lock_bh(dev);
841 __hw_addr_flush(&dev->mc);
842 netif_addr_unlock_bh(dev);
844 EXPORT_SYMBOL(dev_mc_flush);
847 * dev_mc_flush - Init multicast address list
850 * Init multicast address list.
852 void dev_mc_init(struct net_device *dev)
854 __hw_addr_init(&dev->mc);
856 EXPORT_SYMBOL(dev_mc_init);