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/list.h>
19 * General list handling functions
22 static int __hw_addr_add_ex(struct netdev_hw_addr_list *list,
23 unsigned char *addr, int addr_len,
24 unsigned char addr_type, bool global)
26 struct netdev_hw_addr *ha;
29 if (addr_len > MAX_ADDR_LEN)
32 list_for_each_entry(ha, &list->list, list) {
33 if (!memcmp(ha->addr, addr, addr_len) &&
34 ha->type == addr_type) {
36 /* check if addr is already used as global */
40 ha->global_use = true;
48 alloc_size = sizeof(*ha);
49 if (alloc_size < L1_CACHE_BYTES)
50 alloc_size = L1_CACHE_BYTES;
51 ha = kmalloc(alloc_size, GFP_ATOMIC);
54 memcpy(ha->addr, addr, addr_len);
57 ha->global_use = global;
59 list_add_tail_rcu(&ha->list, &list->list);
64 static int __hw_addr_add(struct netdev_hw_addr_list *list, unsigned char *addr,
65 int addr_len, unsigned char addr_type)
67 return __hw_addr_add_ex(list, addr, addr_len, addr_type, false);
70 static void ha_rcu_free(struct rcu_head *head)
72 struct netdev_hw_addr *ha;
74 ha = container_of(head, struct netdev_hw_addr, rcu_head);
78 static int __hw_addr_del_ex(struct netdev_hw_addr_list *list,
79 unsigned char *addr, int addr_len,
80 unsigned char addr_type, bool global)
82 struct netdev_hw_addr *ha;
84 list_for_each_entry(ha, &list->list, list) {
85 if (!memcmp(ha->addr, addr, addr_len) &&
86 (ha->type == addr_type || !addr_type)) {
91 ha->global_use = false;
95 list_del_rcu(&ha->list);
96 call_rcu(&ha->rcu_head, ha_rcu_free);
104 static int __hw_addr_del(struct netdev_hw_addr_list *list, unsigned char *addr,
105 int addr_len, unsigned char addr_type)
107 return __hw_addr_del_ex(list, addr, addr_len, addr_type, false);
110 int __hw_addr_add_multiple(struct netdev_hw_addr_list *to_list,
111 struct netdev_hw_addr_list *from_list,
112 int addr_len, unsigned char addr_type)
115 struct netdev_hw_addr *ha, *ha2;
118 list_for_each_entry(ha, &from_list->list, list) {
119 type = addr_type ? addr_type : ha->type;
120 err = __hw_addr_add(to_list, ha->addr, addr_len, type);
127 list_for_each_entry(ha2, &from_list->list, list) {
130 type = addr_type ? addr_type : ha2->type;
131 __hw_addr_del(to_list, ha2->addr, addr_len, type);
135 EXPORT_SYMBOL(__hw_addr_add_multiple);
137 void __hw_addr_del_multiple(struct netdev_hw_addr_list *to_list,
138 struct netdev_hw_addr_list *from_list,
139 int addr_len, unsigned char addr_type)
141 struct netdev_hw_addr *ha;
144 list_for_each_entry(ha, &from_list->list, list) {
145 type = addr_type ? addr_type : ha->type;
146 __hw_addr_del(to_list, ha->addr, addr_len, addr_type);
149 EXPORT_SYMBOL(__hw_addr_del_multiple);
151 int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
152 struct netdev_hw_addr_list *from_list,
156 struct netdev_hw_addr *ha, *tmp;
158 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
160 err = __hw_addr_add(to_list, ha->addr,
166 } else if (ha->refcount == 1) {
167 __hw_addr_del(to_list, ha->addr, addr_len, ha->type);
168 __hw_addr_del(from_list, ha->addr, addr_len, ha->type);
173 EXPORT_SYMBOL(__hw_addr_sync);
175 void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
176 struct netdev_hw_addr_list *from_list,
179 struct netdev_hw_addr *ha, *tmp;
181 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
183 __hw_addr_del(to_list, ha->addr,
186 __hw_addr_del(from_list, ha->addr,
191 EXPORT_SYMBOL(__hw_addr_unsync);
193 void __hw_addr_flush(struct netdev_hw_addr_list *list)
195 struct netdev_hw_addr *ha, *tmp;
197 list_for_each_entry_safe(ha, tmp, &list->list, list) {
198 list_del_rcu(&ha->list);
199 call_rcu(&ha->rcu_head, ha_rcu_free);
203 EXPORT_SYMBOL(__hw_addr_flush);
205 void __hw_addr_init(struct netdev_hw_addr_list *list)
207 INIT_LIST_HEAD(&list->list);
210 EXPORT_SYMBOL(__hw_addr_init);
213 * Device addresses handling functions
217 * dev_addr_flush - Flush device address list
220 * Flush device address list and reset ->dev_addr.
222 * The caller must hold the rtnl_mutex.
224 void dev_addr_flush(struct net_device *dev)
226 /* rtnl_mutex must be held here */
228 __hw_addr_flush(&dev->dev_addrs);
229 dev->dev_addr = NULL;
231 EXPORT_SYMBOL(dev_addr_flush);
234 * dev_addr_init - Init device address list
237 * Init device address list and create the first element,
238 * used by ->dev_addr.
240 * The caller must hold the rtnl_mutex.
242 int dev_addr_init(struct net_device *dev)
244 unsigned char addr[MAX_ADDR_LEN];
245 struct netdev_hw_addr *ha;
248 /* rtnl_mutex must be held here */
250 __hw_addr_init(&dev->dev_addrs);
251 memset(addr, 0, sizeof(addr));
252 err = __hw_addr_add(&dev->dev_addrs, addr, sizeof(addr),
253 NETDEV_HW_ADDR_T_LAN);
256 * Get the first (previously created) address from the list
257 * and set dev_addr pointer to this location.
259 ha = list_first_entry(&dev->dev_addrs.list,
260 struct netdev_hw_addr, list);
261 dev->dev_addr = ha->addr;
265 EXPORT_SYMBOL(dev_addr_init);
268 * dev_addr_add - Add a device address
270 * @addr: address to add
271 * @addr_type: address type
273 * Add a device address to the device or increase the reference count if
276 * The caller must hold the rtnl_mutex.
278 int dev_addr_add(struct net_device *dev, unsigned char *addr,
279 unsigned char addr_type)
285 err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type);
287 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
290 EXPORT_SYMBOL(dev_addr_add);
293 * dev_addr_del - Release a device address.
295 * @addr: address to delete
296 * @addr_type: address type
298 * Release reference to a device address and remove it from the device
299 * if the reference count drops to zero.
301 * The caller must hold the rtnl_mutex.
303 int dev_addr_del(struct net_device *dev, unsigned char *addr,
304 unsigned char addr_type)
307 struct netdev_hw_addr *ha;
312 * We can not remove the first address from the list because
313 * dev->dev_addr points to that.
315 ha = list_first_entry(&dev->dev_addrs.list,
316 struct netdev_hw_addr, list);
317 if (ha->addr == dev->dev_addr && ha->refcount == 1)
320 err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len,
323 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
326 EXPORT_SYMBOL(dev_addr_del);
329 * dev_addr_add_multiple - Add device addresses from another device
330 * @to_dev: device to which addresses will be added
331 * @from_dev: device from which addresses will be added
332 * @addr_type: address type - 0 means type will be used from from_dev
334 * Add device addresses of the one device to another.
336 * The caller must hold the rtnl_mutex.
338 int dev_addr_add_multiple(struct net_device *to_dev,
339 struct net_device *from_dev,
340 unsigned char addr_type)
346 if (from_dev->addr_len != to_dev->addr_len)
348 err = __hw_addr_add_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
349 to_dev->addr_len, addr_type);
351 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
354 EXPORT_SYMBOL(dev_addr_add_multiple);
357 * dev_addr_del_multiple - Delete device addresses by another device
358 * @to_dev: device where the addresses will be deleted
359 * @from_dev: device by which addresses the addresses will be deleted
360 * @addr_type: address type - 0 means type will used from from_dev
362 * Deletes addresses in to device by the list of addresses in from device.
364 * The caller must hold the rtnl_mutex.
366 int dev_addr_del_multiple(struct net_device *to_dev,
367 struct net_device *from_dev,
368 unsigned char addr_type)
372 if (from_dev->addr_len != to_dev->addr_len)
374 __hw_addr_del_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
375 to_dev->addr_len, addr_type);
376 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
379 EXPORT_SYMBOL(dev_addr_del_multiple);
382 * Unicast list handling functions
386 * dev_uc_add - Add a secondary unicast address
388 * @addr: address to add
390 * Add a secondary unicast address to the device or increase
391 * the reference count if it already exists.
393 int dev_uc_add(struct net_device *dev, unsigned char *addr)
397 netif_addr_lock_bh(dev);
398 err = __hw_addr_add(&dev->uc, addr, dev->addr_len,
399 NETDEV_HW_ADDR_T_UNICAST);
401 __dev_set_rx_mode(dev);
402 netif_addr_unlock_bh(dev);
405 EXPORT_SYMBOL(dev_uc_add);
408 * dev_uc_del - Release secondary unicast address.
410 * @addr: address to delete
412 * Release reference to a secondary unicast address and remove it
413 * from the device if the reference count drops to zero.
415 int dev_uc_del(struct net_device *dev, unsigned char *addr)
419 netif_addr_lock_bh(dev);
420 err = __hw_addr_del(&dev->uc, addr, dev->addr_len,
421 NETDEV_HW_ADDR_T_UNICAST);
423 __dev_set_rx_mode(dev);
424 netif_addr_unlock_bh(dev);
427 EXPORT_SYMBOL(dev_uc_del);
430 * dev_uc_sync - Synchronize device's unicast list to another device
431 * @to: destination device
432 * @from: source device
434 * Add newly added addresses to the destination device and release
435 * addresses that have no users left. The source device must be
436 * locked by netif_tx_lock_bh.
438 * This function is intended to be called from the dev->set_rx_mode
439 * function of layered software devices.
441 int dev_uc_sync(struct net_device *to, struct net_device *from)
445 if (to->addr_len != from->addr_len)
448 netif_addr_lock_bh(to);
449 err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
451 __dev_set_rx_mode(to);
452 netif_addr_unlock_bh(to);
455 EXPORT_SYMBOL(dev_uc_sync);
458 * dev_uc_unsync - Remove synchronized addresses from the destination device
459 * @to: destination device
460 * @from: source device
462 * Remove all addresses that were added to the destination device by
463 * dev_uc_sync(). This function is intended to be called from the
464 * dev->stop function of layered software devices.
466 void dev_uc_unsync(struct net_device *to, struct net_device *from)
468 if (to->addr_len != from->addr_len)
471 netif_addr_lock_bh(from);
473 __hw_addr_unsync(&to->uc, &from->uc, to->addr_len);
474 __dev_set_rx_mode(to);
475 netif_addr_unlock(to);
476 netif_addr_unlock_bh(from);
478 EXPORT_SYMBOL(dev_uc_unsync);
481 * dev_uc_flush - Flush unicast addresses
484 * Flush unicast addresses.
486 void dev_uc_flush(struct net_device *dev)
488 netif_addr_lock_bh(dev);
489 __hw_addr_flush(&dev->uc);
490 netif_addr_unlock_bh(dev);
492 EXPORT_SYMBOL(dev_uc_flush);
495 * dev_uc_flush - Init unicast address list
498 * Init unicast address list.
500 void dev_uc_init(struct net_device *dev)
502 __hw_addr_init(&dev->uc);
504 EXPORT_SYMBOL(dev_uc_init);
507 * Multicast list handling functions
510 static int __dev_mc_add(struct net_device *dev, unsigned char *addr,
515 netif_addr_lock_bh(dev);
516 err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
517 NETDEV_HW_ADDR_T_MULTICAST, global);
519 __dev_set_rx_mode(dev);
520 netif_addr_unlock_bh(dev);
524 * dev_mc_add - Add a multicast address
526 * @addr: address to add
528 * Add a multicast address to the device or increase
529 * the reference count if it already exists.
531 int dev_mc_add(struct net_device *dev, unsigned char *addr)
533 return __dev_mc_add(dev, addr, false);
535 EXPORT_SYMBOL(dev_mc_add);
538 * dev_mc_add_global - Add a global multicast address
540 * @addr: address to add
542 * Add a global multicast address to the device.
544 int dev_mc_add_global(struct net_device *dev, unsigned char *addr)
546 return __dev_mc_add(dev, addr, true);
548 EXPORT_SYMBOL(dev_mc_add_global);
550 static int __dev_mc_del(struct net_device *dev, unsigned char *addr,
555 netif_addr_lock_bh(dev);
556 err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
557 NETDEV_HW_ADDR_T_MULTICAST, global);
559 __dev_set_rx_mode(dev);
560 netif_addr_unlock_bh(dev);
565 * dev_mc_del - Delete a multicast address.
567 * @addr: address to delete
569 * Release reference to a multicast address and remove it
570 * from the device if the reference count drops to zero.
572 int dev_mc_del(struct net_device *dev, unsigned char *addr)
574 return __dev_mc_del(dev, addr, false);
576 EXPORT_SYMBOL(dev_mc_del);
579 * dev_mc_del_global - Delete a global multicast address.
581 * @addr: address to delete
583 * Release reference to a multicast address and remove it
584 * from the device if the reference count drops to zero.
586 int dev_mc_del_global(struct net_device *dev, unsigned char *addr)
588 return __dev_mc_del(dev, addr, true);
590 EXPORT_SYMBOL(dev_mc_del_global);
593 * dev_mc_sync - Synchronize device's unicast list to another device
594 * @to: destination device
595 * @from: source device
597 * Add newly added addresses to the destination device and release
598 * addresses that have no users left. The source device must be
599 * locked by netif_tx_lock_bh.
601 * This function is intended to be called from the dev->set_multicast_list
602 * or dev->set_rx_mode function of layered software devices.
604 int dev_mc_sync(struct net_device *to, struct net_device *from)
608 if (to->addr_len != from->addr_len)
611 netif_addr_lock_bh(to);
612 err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
614 __dev_set_rx_mode(to);
615 netif_addr_unlock_bh(to);
618 EXPORT_SYMBOL(dev_mc_sync);
621 * dev_mc_unsync - Remove synchronized addresses from the destination device
622 * @to: destination device
623 * @from: source device
625 * Remove all addresses that were added to the destination device by
626 * dev_mc_sync(). This function is intended to be called from the
627 * dev->stop function of layered software devices.
629 void dev_mc_unsync(struct net_device *to, struct net_device *from)
631 if (to->addr_len != from->addr_len)
634 netif_addr_lock_bh(from);
636 __hw_addr_unsync(&to->mc, &from->mc, to->addr_len);
637 __dev_set_rx_mode(to);
638 netif_addr_unlock(to);
639 netif_addr_unlock_bh(from);
641 EXPORT_SYMBOL(dev_mc_unsync);
644 * dev_mc_flush - Flush multicast addresses
647 * Flush multicast addresses.
649 void dev_mc_flush(struct net_device *dev)
651 netif_addr_lock_bh(dev);
652 __hw_addr_flush(&dev->mc);
653 netif_addr_unlock_bh(dev);
655 EXPORT_SYMBOL(dev_mc_flush);
658 * dev_mc_flush - Init multicast address list
661 * Init multicast address list.
663 void dev_mc_init(struct net_device *dev)
665 __hw_addr_init(&dev->mc);
667 EXPORT_SYMBOL(dev_mc_init);
669 #ifdef CONFIG_PROC_FS
670 #include <linux/proc_fs.h>
671 #include <linux/seq_file.h>
673 static int dev_mc_seq_show(struct seq_file *seq, void *v)
675 struct netdev_hw_addr *ha;
676 struct net_device *dev = v;
678 if (v == SEQ_START_TOKEN)
681 netif_addr_lock_bh(dev);
682 netdev_for_each_mc_addr(ha, dev) {
685 seq_printf(seq, "%-4d %-15s %-5d %-5d ", dev->ifindex,
686 dev->name, ha->refcount, ha->global_use);
688 for (i = 0; i < dev->addr_len; i++)
689 seq_printf(seq, "%02x", ha->addr[i]);
693 netif_addr_unlock_bh(dev);
697 static const struct seq_operations dev_mc_seq_ops = {
698 .start = dev_seq_start,
699 .next = dev_seq_next,
700 .stop = dev_seq_stop,
701 .show = dev_mc_seq_show,
704 static int dev_mc_seq_open(struct inode *inode, struct file *file)
706 return seq_open_net(inode, file, &dev_mc_seq_ops,
707 sizeof(struct seq_net_private));
710 static const struct file_operations dev_mc_seq_fops = {
711 .owner = THIS_MODULE,
712 .open = dev_mc_seq_open,
715 .release = seq_release_net,
720 static int __net_init dev_mc_net_init(struct net *net)
722 if (!proc_net_fops_create(net, "dev_mcast", 0, &dev_mc_seq_fops))
727 static void __net_exit dev_mc_net_exit(struct net *net)
729 proc_net_remove(net, "dev_mcast");
732 static struct pernet_operations __net_initdata dev_mc_net_ops = {
733 .init = dev_mc_net_init,
734 .exit = dev_mc_net_exit,
737 void __init dev_mcast_init(void)
739 register_pernet_subsys(&dev_mc_net_ops);