2 * net/core/dev_addr_lists.c - Functions for handling net device lists
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 (ha->addr == dev->dev_addr && ha->refcount == 1)
325 err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len,
328 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
331 EXPORT_SYMBOL(dev_addr_del);
334 * dev_addr_add_multiple - Add device addresses from another device
335 * @to_dev: device to which addresses will be added
336 * @from_dev: device from which addresses will be added
337 * @addr_type: address type - 0 means type will be used from from_dev
339 * Add device addresses of the one device to another.
341 * The caller must hold the rtnl_mutex.
343 int dev_addr_add_multiple(struct net_device *to_dev,
344 struct net_device *from_dev,
345 unsigned char addr_type)
351 if (from_dev->addr_len != to_dev->addr_len)
353 err = __hw_addr_add_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
354 to_dev->addr_len, addr_type);
356 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
359 EXPORT_SYMBOL(dev_addr_add_multiple);
362 * dev_addr_del_multiple - Delete device addresses by another device
363 * @to_dev: device where the addresses will be deleted
364 * @from_dev: device supplying the addresses to be deleted
365 * @addr_type: address type - 0 means type will be used from from_dev
367 * Deletes addresses in to device by the list of addresses in from device.
369 * The caller must hold the rtnl_mutex.
371 int dev_addr_del_multiple(struct net_device *to_dev,
372 struct net_device *from_dev,
373 unsigned char addr_type)
377 if (from_dev->addr_len != to_dev->addr_len)
379 __hw_addr_del_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
380 to_dev->addr_len, addr_type);
381 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
384 EXPORT_SYMBOL(dev_addr_del_multiple);
387 * Unicast list handling functions
391 * dev_uc_add_excl - Add a global secondary unicast address
393 * @addr: address to add
395 int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr)
397 struct netdev_hw_addr *ha;
400 netif_addr_lock_bh(dev);
401 list_for_each_entry(ha, &dev->uc.list, list) {
402 if (!memcmp(ha->addr, addr, dev->addr_len) &&
403 ha->type == NETDEV_HW_ADDR_T_UNICAST) {
408 err = __hw_addr_create_ex(&dev->uc, addr, dev->addr_len,
409 NETDEV_HW_ADDR_T_UNICAST, true);
411 __dev_set_rx_mode(dev);
413 netif_addr_unlock_bh(dev);
416 EXPORT_SYMBOL(dev_uc_add_excl);
419 * dev_uc_add - Add a secondary unicast address
421 * @addr: address to add
423 * Add a secondary unicast address to the device or increase
424 * the reference count if it already exists.
426 int dev_uc_add(struct net_device *dev, const unsigned char *addr)
430 netif_addr_lock_bh(dev);
431 err = __hw_addr_add(&dev->uc, addr, dev->addr_len,
432 NETDEV_HW_ADDR_T_UNICAST);
434 __dev_set_rx_mode(dev);
435 netif_addr_unlock_bh(dev);
438 EXPORT_SYMBOL(dev_uc_add);
441 * dev_uc_del - Release secondary unicast address.
443 * @addr: address to delete
445 * Release reference to a secondary unicast address and remove it
446 * from the device if the reference count drops to zero.
448 int dev_uc_del(struct net_device *dev, const unsigned char *addr)
452 netif_addr_lock_bh(dev);
453 err = __hw_addr_del(&dev->uc, addr, dev->addr_len,
454 NETDEV_HW_ADDR_T_UNICAST);
456 __dev_set_rx_mode(dev);
457 netif_addr_unlock_bh(dev);
460 EXPORT_SYMBOL(dev_uc_del);
463 * dev_uc_sync - Synchronize device's unicast list to another device
464 * @to: destination device
465 * @from: source device
467 * Add newly added addresses to the destination device and release
468 * addresses that have no users left. The source device must be
469 * locked by netif_addr_lock_bh.
471 * This function is intended to be called from the dev->set_rx_mode
472 * function of layered software devices.
474 int dev_uc_sync(struct net_device *to, struct net_device *from)
478 if (to->addr_len != from->addr_len)
481 netif_addr_lock_nested(to);
482 err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
484 __dev_set_rx_mode(to);
485 netif_addr_unlock(to);
488 EXPORT_SYMBOL(dev_uc_sync);
491 * dev_uc_unsync - Remove synchronized addresses from the destination device
492 * @to: destination device
493 * @from: source device
495 * Remove all addresses that were added to the destination device by
496 * dev_uc_sync(). This function is intended to be called from the
497 * dev->stop function of layered software devices.
499 void dev_uc_unsync(struct net_device *to, struct net_device *from)
501 if (to->addr_len != from->addr_len)
504 netif_addr_lock_bh(from);
505 netif_addr_lock_nested(to);
506 __hw_addr_unsync(&to->uc, &from->uc, to->addr_len);
507 __dev_set_rx_mode(to);
508 netif_addr_unlock(to);
509 netif_addr_unlock_bh(from);
511 EXPORT_SYMBOL(dev_uc_unsync);
514 * dev_uc_flush - Flush unicast addresses
517 * Flush unicast addresses.
519 void dev_uc_flush(struct net_device *dev)
521 netif_addr_lock_bh(dev);
522 __hw_addr_flush(&dev->uc);
523 netif_addr_unlock_bh(dev);
525 EXPORT_SYMBOL(dev_uc_flush);
528 * dev_uc_flush - Init unicast address list
531 * Init unicast address list.
533 void dev_uc_init(struct net_device *dev)
535 __hw_addr_init(&dev->uc);
537 EXPORT_SYMBOL(dev_uc_init);
540 * Multicast list handling functions
544 * dev_mc_add_excl - Add a global secondary multicast address
546 * @addr: address to add
548 int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr)
550 struct netdev_hw_addr *ha;
553 netif_addr_lock_bh(dev);
554 list_for_each_entry(ha, &dev->mc.list, list) {
555 if (!memcmp(ha->addr, addr, dev->addr_len) &&
556 ha->type == NETDEV_HW_ADDR_T_MULTICAST) {
561 err = __hw_addr_create_ex(&dev->mc, addr, dev->addr_len,
562 NETDEV_HW_ADDR_T_MULTICAST, true);
564 __dev_set_rx_mode(dev);
566 netif_addr_unlock_bh(dev);
569 EXPORT_SYMBOL(dev_mc_add_excl);
571 static int __dev_mc_add(struct net_device *dev, const unsigned char *addr,
576 netif_addr_lock_bh(dev);
577 err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
578 NETDEV_HW_ADDR_T_MULTICAST, global);
580 __dev_set_rx_mode(dev);
581 netif_addr_unlock_bh(dev);
585 * dev_mc_add - Add a multicast address
587 * @addr: address to add
589 * Add a multicast address to the device or increase
590 * the reference count if it already exists.
592 int dev_mc_add(struct net_device *dev, const unsigned char *addr)
594 return __dev_mc_add(dev, addr, false);
596 EXPORT_SYMBOL(dev_mc_add);
599 * dev_mc_add_global - Add a global multicast address
601 * @addr: address to add
603 * Add a global multicast address to the device.
605 int dev_mc_add_global(struct net_device *dev, const unsigned char *addr)
607 return __dev_mc_add(dev, addr, true);
609 EXPORT_SYMBOL(dev_mc_add_global);
611 static int __dev_mc_del(struct net_device *dev, const unsigned char *addr,
616 netif_addr_lock_bh(dev);
617 err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
618 NETDEV_HW_ADDR_T_MULTICAST, global);
620 __dev_set_rx_mode(dev);
621 netif_addr_unlock_bh(dev);
626 * dev_mc_del - Delete a multicast address.
628 * @addr: address to delete
630 * Release reference to a multicast address and remove it
631 * from the device if the reference count drops to zero.
633 int dev_mc_del(struct net_device *dev, const unsigned char *addr)
635 return __dev_mc_del(dev, addr, false);
637 EXPORT_SYMBOL(dev_mc_del);
640 * dev_mc_del_global - Delete a global multicast address.
642 * @addr: address to delete
644 * Release reference to a multicast address and remove it
645 * from the device if the reference count drops to zero.
647 int dev_mc_del_global(struct net_device *dev, const unsigned char *addr)
649 return __dev_mc_del(dev, addr, true);
651 EXPORT_SYMBOL(dev_mc_del_global);
654 * dev_mc_sync - Synchronize device's unicast list to another device
655 * @to: destination device
656 * @from: source device
658 * Add newly added addresses to the destination device and release
659 * addresses that have no users left. The source device must be
660 * locked by netif_addr_lock_bh.
662 * This function is intended to be called from the ndo_set_rx_mode
663 * function of layered software devices.
665 int dev_mc_sync(struct net_device *to, struct net_device *from)
669 if (to->addr_len != from->addr_len)
672 netif_addr_lock_nested(to);
673 err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
675 __dev_set_rx_mode(to);
676 netif_addr_unlock(to);
679 EXPORT_SYMBOL(dev_mc_sync);
682 * dev_mc_unsync - Remove synchronized addresses from the destination device
683 * @to: destination device
684 * @from: source device
686 * Remove all addresses that were added to the destination device by
687 * dev_mc_sync(). This function is intended to be called from the
688 * dev->stop function of layered software devices.
690 void dev_mc_unsync(struct net_device *to, struct net_device *from)
692 if (to->addr_len != from->addr_len)
695 netif_addr_lock_bh(from);
696 netif_addr_lock_nested(to);
697 __hw_addr_unsync(&to->mc, &from->mc, to->addr_len);
698 __dev_set_rx_mode(to);
699 netif_addr_unlock(to);
700 netif_addr_unlock_bh(from);
702 EXPORT_SYMBOL(dev_mc_unsync);
705 * dev_mc_flush - Flush multicast addresses
708 * Flush multicast addresses.
710 void dev_mc_flush(struct net_device *dev)
712 netif_addr_lock_bh(dev);
713 __hw_addr_flush(&dev->mc);
714 netif_addr_unlock_bh(dev);
716 EXPORT_SYMBOL(dev_mc_flush);
719 * dev_mc_flush - Init multicast address list
722 * Init multicast address list.
724 void dev_mc_init(struct net_device *dev)
726 __hw_addr_init(&dev->mc);
728 EXPORT_SYMBOL(dev_mc_init);
730 #ifdef CONFIG_PROC_FS
731 #include <linux/seq_file.h>
733 static int dev_mc_seq_show(struct seq_file *seq, void *v)
735 struct netdev_hw_addr *ha;
736 struct net_device *dev = v;
738 if (v == SEQ_START_TOKEN)
741 netif_addr_lock_bh(dev);
742 netdev_for_each_mc_addr(ha, dev) {
745 seq_printf(seq, "%-4d %-15s %-5d %-5d ", dev->ifindex,
746 dev->name, ha->refcount, ha->global_use);
748 for (i = 0; i < dev->addr_len; i++)
749 seq_printf(seq, "%02x", ha->addr[i]);
753 netif_addr_unlock_bh(dev);
757 static const struct seq_operations dev_mc_seq_ops = {
758 .start = dev_seq_start,
759 .next = dev_seq_next,
760 .stop = dev_seq_stop,
761 .show = dev_mc_seq_show,
764 static int dev_mc_seq_open(struct inode *inode, struct file *file)
766 return seq_open_net(inode, file, &dev_mc_seq_ops,
767 sizeof(struct seq_net_private));
770 static const struct file_operations dev_mc_seq_fops = {
771 .owner = THIS_MODULE,
772 .open = dev_mc_seq_open,
775 .release = seq_release_net,
780 static int __net_init dev_mc_net_init(struct net *net)
782 if (!proc_net_fops_create(net, "dev_mcast", 0, &dev_mc_seq_fops))
787 static void __net_exit dev_mc_net_exit(struct net *net)
789 proc_net_remove(net, "dev_mcast");
792 static struct pernet_operations __net_initdata dev_mc_net_ops = {
793 .init = dev_mc_net_init,
794 .exit = dev_mc_net_exit,
797 void __init dev_mcast_init(void)
799 register_pernet_subsys(&dev_mc_net_ops);