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>
18 #include <linux/proc_fs.h>
21 * General list handling functions
24 static int __hw_addr_create_ex(struct netdev_hw_addr_list *list,
25 const unsigned char *addr, int addr_len,
26 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)
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;
72 return __hw_addr_create_ex(list, addr, addr_len, addr_type, global);
75 static int __hw_addr_add(struct netdev_hw_addr_list *list,
76 const unsigned char *addr, int addr_len,
77 unsigned char addr_type)
79 return __hw_addr_add_ex(list, addr, addr_len, addr_type, false);
82 static int __hw_addr_del_ex(struct netdev_hw_addr_list *list,
83 const unsigned char *addr, int addr_len,
84 unsigned char addr_type, bool global)
86 struct netdev_hw_addr *ha;
88 list_for_each_entry(ha, &list->list, list) {
89 if (!memcmp(ha->addr, addr, addr_len) &&
90 (ha->type == addr_type || !addr_type)) {
95 ha->global_use = false;
99 list_del_rcu(&ha->list);
100 kfree_rcu(ha, rcu_head);
108 static int __hw_addr_del(struct netdev_hw_addr_list *list,
109 const unsigned char *addr, int addr_len,
110 unsigned char addr_type)
112 return __hw_addr_del_ex(list, addr, addr_len, addr_type, false);
115 int __hw_addr_add_multiple(struct netdev_hw_addr_list *to_list,
116 struct netdev_hw_addr_list *from_list,
117 int addr_len, unsigned char addr_type)
120 struct netdev_hw_addr *ha, *ha2;
123 list_for_each_entry(ha, &from_list->list, list) {
124 type = addr_type ? addr_type : ha->type;
125 err = __hw_addr_add(to_list, ha->addr, addr_len, type);
132 list_for_each_entry(ha2, &from_list->list, list) {
135 type = addr_type ? addr_type : ha2->type;
136 __hw_addr_del(to_list, ha2->addr, addr_len, type);
140 EXPORT_SYMBOL(__hw_addr_add_multiple);
142 void __hw_addr_del_multiple(struct netdev_hw_addr_list *to_list,
143 struct netdev_hw_addr_list *from_list,
144 int addr_len, unsigned char addr_type)
146 struct netdev_hw_addr *ha;
149 list_for_each_entry(ha, &from_list->list, list) {
150 type = addr_type ? addr_type : ha->type;
151 __hw_addr_del(to_list, ha->addr, addr_len, type);
154 EXPORT_SYMBOL(__hw_addr_del_multiple);
156 int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
157 struct netdev_hw_addr_list *from_list,
161 struct netdev_hw_addr *ha, *tmp;
163 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
165 err = __hw_addr_add(to_list, ha->addr,
171 } else if (ha->refcount == 1) {
172 __hw_addr_del(to_list, ha->addr, addr_len, ha->type);
173 __hw_addr_del(from_list, ha->addr, addr_len, ha->type);
178 EXPORT_SYMBOL(__hw_addr_sync);
180 void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
181 struct netdev_hw_addr_list *from_list,
184 struct netdev_hw_addr *ha, *tmp;
186 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
188 __hw_addr_del(to_list, ha->addr,
191 __hw_addr_del(from_list, ha->addr,
196 EXPORT_SYMBOL(__hw_addr_unsync);
198 void __hw_addr_flush(struct netdev_hw_addr_list *list)
200 struct netdev_hw_addr *ha, *tmp;
202 list_for_each_entry_safe(ha, tmp, &list->list, list) {
203 list_del_rcu(&ha->list);
204 kfree_rcu(ha, rcu_head);
208 EXPORT_SYMBOL(__hw_addr_flush);
210 void __hw_addr_init(struct netdev_hw_addr_list *list)
212 INIT_LIST_HEAD(&list->list);
215 EXPORT_SYMBOL(__hw_addr_init);
218 * Device addresses handling functions
222 * dev_addr_flush - Flush device address list
225 * Flush device address list and reset ->dev_addr.
227 * The caller must hold the rtnl_mutex.
229 void dev_addr_flush(struct net_device *dev)
231 /* rtnl_mutex must be held here */
233 __hw_addr_flush(&dev->dev_addrs);
234 dev->dev_addr = NULL;
236 EXPORT_SYMBOL(dev_addr_flush);
239 * dev_addr_init - Init device address list
242 * Init device address list and create the first element,
243 * used by ->dev_addr.
245 * The caller must hold the rtnl_mutex.
247 int dev_addr_init(struct net_device *dev)
249 unsigned char addr[MAX_ADDR_LEN];
250 struct netdev_hw_addr *ha;
253 /* rtnl_mutex must be held here */
255 __hw_addr_init(&dev->dev_addrs);
256 memset(addr, 0, sizeof(addr));
257 err = __hw_addr_add(&dev->dev_addrs, addr, sizeof(addr),
258 NETDEV_HW_ADDR_T_LAN);
261 * Get the first (previously created) address from the list
262 * and set dev_addr pointer to this location.
264 ha = list_first_entry(&dev->dev_addrs.list,
265 struct netdev_hw_addr, list);
266 dev->dev_addr = ha->addr;
270 EXPORT_SYMBOL(dev_addr_init);
273 * dev_addr_add - Add a device address
275 * @addr: address to add
276 * @addr_type: address type
278 * Add a device address to the device or increase the reference count if
281 * The caller must hold the rtnl_mutex.
283 int dev_addr_add(struct net_device *dev, const unsigned char *addr,
284 unsigned char addr_type)
290 err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type);
292 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
295 EXPORT_SYMBOL(dev_addr_add);
298 * dev_addr_del - Release a device address.
300 * @addr: address to delete
301 * @addr_type: address type
303 * Release reference to a device address and remove it from the device
304 * if the reference count drops to zero.
306 * The caller must hold the rtnl_mutex.
308 int dev_addr_del(struct net_device *dev, const unsigned char *addr,
309 unsigned char addr_type)
312 struct netdev_hw_addr *ha;
317 * We can not remove the first address from the list because
318 * dev->dev_addr points to that.
320 ha = list_first_entry(&dev->dev_addrs.list,
321 struct netdev_hw_addr, list);
322 if (!memcmp(ha->addr, addr, dev->addr_len) &&
323 ha->type == addr_type && ha->refcount == 1)
326 err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len,
329 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
332 EXPORT_SYMBOL(dev_addr_del);
335 * dev_addr_add_multiple - Add device addresses from another device
336 * @to_dev: device to which addresses will be added
337 * @from_dev: device from which addresses will be added
338 * @addr_type: address type - 0 means type will be used from from_dev
340 * Add device addresses of the one device to another.
342 * The caller must hold the rtnl_mutex.
344 int dev_addr_add_multiple(struct net_device *to_dev,
345 struct net_device *from_dev,
346 unsigned char addr_type)
352 if (from_dev->addr_len != to_dev->addr_len)
354 err = __hw_addr_add_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
355 to_dev->addr_len, addr_type);
357 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
360 EXPORT_SYMBOL(dev_addr_add_multiple);
363 * dev_addr_del_multiple - Delete device addresses by another device
364 * @to_dev: device where the addresses will be deleted
365 * @from_dev: device supplying the addresses to be deleted
366 * @addr_type: address type - 0 means type will be used from from_dev
368 * Deletes addresses in to device by the list of addresses in from device.
370 * The caller must hold the rtnl_mutex.
372 int dev_addr_del_multiple(struct net_device *to_dev,
373 struct net_device *from_dev,
374 unsigned char addr_type)
378 if (from_dev->addr_len != to_dev->addr_len)
380 __hw_addr_del_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
381 to_dev->addr_len, addr_type);
382 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
385 EXPORT_SYMBOL(dev_addr_del_multiple);
388 * Unicast list handling functions
392 * dev_uc_add_excl - Add a global secondary unicast address
394 * @addr: address to add
396 int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr)
398 struct netdev_hw_addr *ha;
401 netif_addr_lock_bh(dev);
402 list_for_each_entry(ha, &dev->uc.list, list) {
403 if (!memcmp(ha->addr, addr, dev->addr_len) &&
404 ha->type == NETDEV_HW_ADDR_T_UNICAST) {
409 err = __hw_addr_create_ex(&dev->uc, addr, dev->addr_len,
410 NETDEV_HW_ADDR_T_UNICAST, true);
412 __dev_set_rx_mode(dev);
414 netif_addr_unlock_bh(dev);
417 EXPORT_SYMBOL(dev_uc_add_excl);
420 * dev_uc_add - Add a secondary unicast address
422 * @addr: address to add
424 * Add a secondary unicast address to the device or increase
425 * the reference count if it already exists.
427 int dev_uc_add(struct net_device *dev, const unsigned char *addr)
431 netif_addr_lock_bh(dev);
432 err = __hw_addr_add(&dev->uc, addr, dev->addr_len,
433 NETDEV_HW_ADDR_T_UNICAST);
435 __dev_set_rx_mode(dev);
436 netif_addr_unlock_bh(dev);
439 EXPORT_SYMBOL(dev_uc_add);
442 * dev_uc_del - Release secondary unicast address.
444 * @addr: address to delete
446 * Release reference to a secondary unicast address and remove it
447 * from the device if the reference count drops to zero.
449 int dev_uc_del(struct net_device *dev, const unsigned char *addr)
453 netif_addr_lock_bh(dev);
454 err = __hw_addr_del(&dev->uc, addr, dev->addr_len,
455 NETDEV_HW_ADDR_T_UNICAST);
457 __dev_set_rx_mode(dev);
458 netif_addr_unlock_bh(dev);
461 EXPORT_SYMBOL(dev_uc_del);
464 * dev_uc_sync - Synchronize device's unicast list to another device
465 * @to: destination device
466 * @from: source device
468 * Add newly added addresses to the destination device and release
469 * addresses that have no users left. The source device must be
470 * locked by netif_addr_lock_bh.
472 * This function is intended to be called from the dev->set_rx_mode
473 * function of layered software devices.
475 int dev_uc_sync(struct net_device *to, struct net_device *from)
479 if (to->addr_len != from->addr_len)
482 netif_addr_lock_nested(to);
483 err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
485 __dev_set_rx_mode(to);
486 netif_addr_unlock(to);
489 EXPORT_SYMBOL(dev_uc_sync);
492 * dev_uc_unsync - Remove synchronized addresses from the destination device
493 * @to: destination device
494 * @from: source device
496 * Remove all addresses that were added to the destination device by
497 * dev_uc_sync(). This function is intended to be called from the
498 * dev->stop function of layered software devices.
500 void dev_uc_unsync(struct net_device *to, struct net_device *from)
502 if (to->addr_len != from->addr_len)
505 netif_addr_lock_bh(from);
506 netif_addr_lock_nested(to);
507 __hw_addr_unsync(&to->uc, &from->uc, to->addr_len);
508 __dev_set_rx_mode(to);
509 netif_addr_unlock(to);
510 netif_addr_unlock_bh(from);
512 EXPORT_SYMBOL(dev_uc_unsync);
515 * dev_uc_flush - Flush unicast addresses
518 * Flush unicast addresses.
520 void dev_uc_flush(struct net_device *dev)
522 netif_addr_lock_bh(dev);
523 __hw_addr_flush(&dev->uc);
524 netif_addr_unlock_bh(dev);
526 EXPORT_SYMBOL(dev_uc_flush);
529 * dev_uc_flush - Init unicast address list
532 * Init unicast address list.
534 void dev_uc_init(struct net_device *dev)
536 __hw_addr_init(&dev->uc);
538 EXPORT_SYMBOL(dev_uc_init);
541 * Multicast list handling functions
545 * dev_mc_add_excl - Add a global secondary multicast address
547 * @addr: address to add
549 int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr)
551 struct netdev_hw_addr *ha;
554 netif_addr_lock_bh(dev);
555 list_for_each_entry(ha, &dev->mc.list, list) {
556 if (!memcmp(ha->addr, addr, dev->addr_len) &&
557 ha->type == NETDEV_HW_ADDR_T_MULTICAST) {
562 err = __hw_addr_create_ex(&dev->mc, addr, dev->addr_len,
563 NETDEV_HW_ADDR_T_MULTICAST, true);
565 __dev_set_rx_mode(dev);
567 netif_addr_unlock_bh(dev);
570 EXPORT_SYMBOL(dev_mc_add_excl);
572 static int __dev_mc_add(struct net_device *dev, const unsigned char *addr,
577 netif_addr_lock_bh(dev);
578 err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
579 NETDEV_HW_ADDR_T_MULTICAST, global);
581 __dev_set_rx_mode(dev);
582 netif_addr_unlock_bh(dev);
586 * dev_mc_add - Add a multicast address
588 * @addr: address to add
590 * Add a multicast address to the device or increase
591 * the reference count if it already exists.
593 int dev_mc_add(struct net_device *dev, const unsigned char *addr)
595 return __dev_mc_add(dev, addr, false);
597 EXPORT_SYMBOL(dev_mc_add);
600 * dev_mc_add_global - Add a global multicast address
602 * @addr: address to add
604 * Add a global multicast address to the device.
606 int dev_mc_add_global(struct net_device *dev, const unsigned char *addr)
608 return __dev_mc_add(dev, addr, true);
610 EXPORT_SYMBOL(dev_mc_add_global);
612 static int __dev_mc_del(struct net_device *dev, const unsigned char *addr,
617 netif_addr_lock_bh(dev);
618 err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
619 NETDEV_HW_ADDR_T_MULTICAST, global);
621 __dev_set_rx_mode(dev);
622 netif_addr_unlock_bh(dev);
627 * dev_mc_del - Delete a multicast address.
629 * @addr: address to delete
631 * Release reference to a multicast address and remove it
632 * from the device if the reference count drops to zero.
634 int dev_mc_del(struct net_device *dev, const unsigned char *addr)
636 return __dev_mc_del(dev, addr, false);
638 EXPORT_SYMBOL(dev_mc_del);
641 * dev_mc_del_global - Delete a global multicast address.
643 * @addr: address to delete
645 * Release reference to a multicast address and remove it
646 * from the device if the reference count drops to zero.
648 int dev_mc_del_global(struct net_device *dev, const unsigned char *addr)
650 return __dev_mc_del(dev, addr, true);
652 EXPORT_SYMBOL(dev_mc_del_global);
655 * dev_mc_sync - Synchronize device's unicast list to another device
656 * @to: destination device
657 * @from: source device
659 * Add newly added addresses to the destination device and release
660 * addresses that have no users left. The source device must be
661 * locked by netif_addr_lock_bh.
663 * This function is intended to be called from the ndo_set_rx_mode
664 * function of layered software devices.
666 int dev_mc_sync(struct net_device *to, struct net_device *from)
670 if (to->addr_len != from->addr_len)
673 netif_addr_lock_nested(to);
674 err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
676 __dev_set_rx_mode(to);
677 netif_addr_unlock(to);
680 EXPORT_SYMBOL(dev_mc_sync);
683 * dev_mc_unsync - Remove synchronized addresses from the destination device
684 * @to: destination device
685 * @from: source device
687 * Remove all addresses that were added to the destination device by
688 * dev_mc_sync(). This function is intended to be called from the
689 * dev->stop function of layered software devices.
691 void dev_mc_unsync(struct net_device *to, struct net_device *from)
693 if (to->addr_len != from->addr_len)
696 netif_addr_lock_bh(from);
697 netif_addr_lock_nested(to);
698 __hw_addr_unsync(&to->mc, &from->mc, to->addr_len);
699 __dev_set_rx_mode(to);
700 netif_addr_unlock(to);
701 netif_addr_unlock_bh(from);
703 EXPORT_SYMBOL(dev_mc_unsync);
706 * dev_mc_flush - Flush multicast addresses
709 * Flush multicast addresses.
711 void dev_mc_flush(struct net_device *dev)
713 netif_addr_lock_bh(dev);
714 __hw_addr_flush(&dev->mc);
715 netif_addr_unlock_bh(dev);
717 EXPORT_SYMBOL(dev_mc_flush);
720 * dev_mc_flush - Init multicast address list
723 * Init multicast address list.
725 void dev_mc_init(struct net_device *dev)
727 __hw_addr_init(&dev->mc);
729 EXPORT_SYMBOL(dev_mc_init);
731 #ifdef CONFIG_PROC_FS
732 #include <linux/seq_file.h>
734 static int dev_mc_seq_show(struct seq_file *seq, void *v)
736 struct netdev_hw_addr *ha;
737 struct net_device *dev = v;
739 if (v == SEQ_START_TOKEN)
742 netif_addr_lock_bh(dev);
743 netdev_for_each_mc_addr(ha, dev) {
746 seq_printf(seq, "%-4d %-15s %-5d %-5d ", dev->ifindex,
747 dev->name, ha->refcount, ha->global_use);
749 for (i = 0; i < dev->addr_len; i++)
750 seq_printf(seq, "%02x", ha->addr[i]);
754 netif_addr_unlock_bh(dev);
758 static const struct seq_operations dev_mc_seq_ops = {
759 .start = dev_seq_start,
760 .next = dev_seq_next,
761 .stop = dev_seq_stop,
762 .show = dev_mc_seq_show,
765 static int dev_mc_seq_open(struct inode *inode, struct file *file)
767 return seq_open_net(inode, file, &dev_mc_seq_ops,
768 sizeof(struct seq_net_private));
771 static const struct file_operations dev_mc_seq_fops = {
772 .owner = THIS_MODULE,
773 .open = dev_mc_seq_open,
776 .release = seq_release_net,
781 static int __net_init dev_mc_net_init(struct net *net)
783 if (!proc_net_fops_create(net, "dev_mcast", 0, &dev_mc_seq_fops))
788 static void __net_exit dev_mc_net_exit(struct net *net)
790 proc_net_remove(net, "dev_mcast");
793 static struct pernet_operations __net_initdata dev_mc_net_ops = {
794 .init = dev_mc_net_init,
795 .exit = dev_mc_net_exit,
798 void __init dev_mcast_init(void)
800 register_pernet_subsys(&dev_mc_net_ops);