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 /* 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().
194 int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
195 struct netdev_hw_addr_list *from_list,
199 struct netdev_hw_addr *ha, *tmp;
201 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
203 err = __hw_addr_sync_one(to_list, ha, addr_len);
206 } else if (ha->refcount == 1)
207 __hw_addr_unsync_one(to_list, from_list, ha, addr_len);
211 EXPORT_SYMBOL(__hw_addr_sync);
213 void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
214 struct netdev_hw_addr_list *from_list,
217 struct netdev_hw_addr *ha, *tmp;
219 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
221 __hw_addr_unsync_one(to_list, from_list, ha, addr_len);
224 EXPORT_SYMBOL(__hw_addr_unsync);
226 static void __hw_addr_flush(struct netdev_hw_addr_list *list)
228 struct netdev_hw_addr *ha, *tmp;
230 list_for_each_entry_safe(ha, tmp, &list->list, list) {
231 list_del_rcu(&ha->list);
232 kfree_rcu(ha, rcu_head);
237 void __hw_addr_init(struct netdev_hw_addr_list *list)
239 INIT_LIST_HEAD(&list->list);
242 EXPORT_SYMBOL(__hw_addr_init);
245 * Device addresses handling functions
249 * dev_addr_flush - Flush device address list
252 * Flush device address list and reset ->dev_addr.
254 * The caller must hold the rtnl_mutex.
256 void dev_addr_flush(struct net_device *dev)
258 /* rtnl_mutex must be held here */
260 __hw_addr_flush(&dev->dev_addrs);
261 dev->dev_addr = NULL;
263 EXPORT_SYMBOL(dev_addr_flush);
266 * dev_addr_init - Init device address list
269 * Init device address list and create the first element,
270 * used by ->dev_addr.
272 * The caller must hold the rtnl_mutex.
274 int dev_addr_init(struct net_device *dev)
276 unsigned char addr[MAX_ADDR_LEN];
277 struct netdev_hw_addr *ha;
280 /* rtnl_mutex must be held here */
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);
288 * Get the first (previously created) address from the list
289 * and set dev_addr pointer to this location.
291 ha = list_first_entry(&dev->dev_addrs.list,
292 struct netdev_hw_addr, list);
293 dev->dev_addr = ha->addr;
297 EXPORT_SYMBOL(dev_addr_init);
300 * dev_addr_add - Add a device address
302 * @addr: address to add
303 * @addr_type: address type
305 * Add a device address to the device or increase the reference count if
308 * The caller must hold the rtnl_mutex.
310 int dev_addr_add(struct net_device *dev, const unsigned char *addr,
311 unsigned char addr_type)
317 err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type);
319 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
322 EXPORT_SYMBOL(dev_addr_add);
325 * dev_addr_del - Release a device address.
327 * @addr: address to delete
328 * @addr_type: address type
330 * Release reference to a device address and remove it from the device
331 * if the reference count drops to zero.
333 * The caller must hold the rtnl_mutex.
335 int dev_addr_del(struct net_device *dev, const unsigned char *addr,
336 unsigned char addr_type)
339 struct netdev_hw_addr *ha;
344 * We can not remove the first address from the list because
345 * dev->dev_addr points to that.
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)
353 err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len,
356 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
359 EXPORT_SYMBOL(dev_addr_del);
362 * Unicast list handling functions
366 * dev_uc_add_excl - Add a global secondary unicast address
368 * @addr: address to add
370 int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr)
372 struct netdev_hw_addr *ha;
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) {
383 err = __hw_addr_create_ex(&dev->uc, addr, dev->addr_len,
384 NETDEV_HW_ADDR_T_UNICAST, true, false);
386 __dev_set_rx_mode(dev);
388 netif_addr_unlock_bh(dev);
391 EXPORT_SYMBOL(dev_uc_add_excl);
394 * dev_uc_add - Add a secondary unicast address
396 * @addr: address to add
398 * Add a secondary unicast address to the device or increase
399 * the reference count if it already exists.
401 int dev_uc_add(struct net_device *dev, const unsigned char *addr)
405 netif_addr_lock_bh(dev);
406 err = __hw_addr_add(&dev->uc, addr, dev->addr_len,
407 NETDEV_HW_ADDR_T_UNICAST);
409 __dev_set_rx_mode(dev);
410 netif_addr_unlock_bh(dev);
413 EXPORT_SYMBOL(dev_uc_add);
416 * dev_uc_del - Release secondary unicast address.
418 * @addr: address to delete
420 * Release reference to a secondary unicast address and remove it
421 * from the device if the reference count drops to zero.
423 int dev_uc_del(struct net_device *dev, const unsigned char *addr)
427 netif_addr_lock_bh(dev);
428 err = __hw_addr_del(&dev->uc, addr, dev->addr_len,
429 NETDEV_HW_ADDR_T_UNICAST);
431 __dev_set_rx_mode(dev);
432 netif_addr_unlock_bh(dev);
435 EXPORT_SYMBOL(dev_uc_del);
438 * dev_uc_sync - Synchronize device's unicast list to another device
439 * @to: destination device
440 * @from: source device
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.
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.
450 int dev_uc_sync(struct net_device *to, struct net_device *from)
454 if (to->addr_len != from->addr_len)
457 netif_addr_lock_nested(to);
458 err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
460 __dev_set_rx_mode(to);
461 netif_addr_unlock(to);
464 EXPORT_SYMBOL(dev_uc_sync);
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
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.
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.
480 int dev_uc_sync_multiple(struct net_device *to, struct net_device *from)
484 if (to->addr_len != from->addr_len)
487 netif_addr_lock_nested(to);
488 err = __hw_addr_sync_multiple(&to->uc, &from->uc, to->addr_len);
490 __dev_set_rx_mode(to);
491 netif_addr_unlock(to);
494 EXPORT_SYMBOL(dev_uc_sync_multiple);
497 * dev_uc_unsync - Remove synchronized addresses from the destination device
498 * @to: destination device
499 * @from: source device
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.
505 void dev_uc_unsync(struct net_device *to, struct net_device *from)
507 if (to->addr_len != from->addr_len)
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);
517 EXPORT_SYMBOL(dev_uc_unsync);
520 * dev_uc_flush - Flush unicast addresses
523 * Flush unicast addresses.
525 void dev_uc_flush(struct net_device *dev)
527 netif_addr_lock_bh(dev);
528 __hw_addr_flush(&dev->uc);
529 netif_addr_unlock_bh(dev);
531 EXPORT_SYMBOL(dev_uc_flush);
534 * dev_uc_flush - Init unicast address list
537 * Init unicast address list.
539 void dev_uc_init(struct net_device *dev)
541 __hw_addr_init(&dev->uc);
543 EXPORT_SYMBOL(dev_uc_init);
546 * Multicast list handling functions
550 * dev_mc_add_excl - Add a global secondary multicast address
552 * @addr: address to add
554 int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr)
556 struct netdev_hw_addr *ha;
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) {
567 err = __hw_addr_create_ex(&dev->mc, addr, dev->addr_len,
568 NETDEV_HW_ADDR_T_MULTICAST, true, false);
570 __dev_set_rx_mode(dev);
572 netif_addr_unlock_bh(dev);
575 EXPORT_SYMBOL(dev_mc_add_excl);
577 static int __dev_mc_add(struct net_device *dev, const unsigned char *addr,
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);
586 __dev_set_rx_mode(dev);
587 netif_addr_unlock_bh(dev);
591 * dev_mc_add - Add a multicast address
593 * @addr: address to add
595 * Add a multicast address to the device or increase
596 * the reference count if it already exists.
598 int dev_mc_add(struct net_device *dev, const unsigned char *addr)
600 return __dev_mc_add(dev, addr, false);
602 EXPORT_SYMBOL(dev_mc_add);
605 * dev_mc_add_global - Add a global multicast address
607 * @addr: address to add
609 * Add a global multicast address to the device.
611 int dev_mc_add_global(struct net_device *dev, const unsigned char *addr)
613 return __dev_mc_add(dev, addr, true);
615 EXPORT_SYMBOL(dev_mc_add_global);
617 static int __dev_mc_del(struct net_device *dev, const unsigned char *addr,
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);
626 __dev_set_rx_mode(dev);
627 netif_addr_unlock_bh(dev);
632 * dev_mc_del - Delete a multicast address.
634 * @addr: address to delete
636 * Release reference to a multicast address and remove it
637 * from the device if the reference count drops to zero.
639 int dev_mc_del(struct net_device *dev, const unsigned char *addr)
641 return __dev_mc_del(dev, addr, false);
643 EXPORT_SYMBOL(dev_mc_del);
646 * dev_mc_del_global - Delete a global multicast address.
648 * @addr: address to delete
650 * Release reference to a multicast address and remove it
651 * from the device if the reference count drops to zero.
653 int dev_mc_del_global(struct net_device *dev, const unsigned char *addr)
655 return __dev_mc_del(dev, addr, true);
657 EXPORT_SYMBOL(dev_mc_del_global);
660 * dev_mc_sync - Synchronize device's multicast list to another device
661 * @to: destination device
662 * @from: source device
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.
668 * This function is intended to be called from the ndo_set_rx_mode
669 * function of layered software devices.
671 int dev_mc_sync(struct net_device *to, struct net_device *from)
675 if (to->addr_len != from->addr_len)
678 netif_addr_lock_nested(to);
679 err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
681 __dev_set_rx_mode(to);
682 netif_addr_unlock(to);
685 EXPORT_SYMBOL(dev_mc_sync);
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
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.
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.
701 int dev_mc_sync_multiple(struct net_device *to, struct net_device *from)
705 if (to->addr_len != from->addr_len)
708 netif_addr_lock_nested(to);
709 err = __hw_addr_sync_multiple(&to->mc, &from->mc, to->addr_len);
711 __dev_set_rx_mode(to);
712 netif_addr_unlock(to);
715 EXPORT_SYMBOL(dev_mc_sync_multiple);
718 * dev_mc_unsync - Remove synchronized addresses from the destination device
719 * @to: destination device
720 * @from: source device
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.
726 void dev_mc_unsync(struct net_device *to, struct net_device *from)
728 if (to->addr_len != from->addr_len)
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);
738 EXPORT_SYMBOL(dev_mc_unsync);
741 * dev_mc_flush - Flush multicast addresses
744 * Flush multicast addresses.
746 void dev_mc_flush(struct net_device *dev)
748 netif_addr_lock_bh(dev);
749 __hw_addr_flush(&dev->mc);
750 netif_addr_unlock_bh(dev);
752 EXPORT_SYMBOL(dev_mc_flush);
755 * dev_mc_flush - Init multicast address list
758 * Init multicast address list.
760 void dev_mc_init(struct net_device *dev)
762 __hw_addr_init(&dev->mc);
764 EXPORT_SYMBOL(dev_mc_init);