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>
17 #include <linux/proc_fs.h>
20 * General list handling functions
23 static int __hw_addr_add_ex(struct netdev_hw_addr_list
*list
,
24 unsigned char *addr
, int addr_len
,
25 unsigned char addr_type
, bool global
)
27 struct netdev_hw_addr
*ha
;
30 if (addr_len
> MAX_ADDR_LEN
)
33 list_for_each_entry(ha
, &list
->list
, list
) {
34 if (!memcmp(ha
->addr
, addr
, addr_len
) &&
35 ha
->type
== addr_type
) {
37 /* check if addr is already used as global */
41 ha
->global_use
= true;
49 alloc_size
= sizeof(*ha
);
50 if (alloc_size
< L1_CACHE_BYTES
)
51 alloc_size
= L1_CACHE_BYTES
;
52 ha
= kmalloc(alloc_size
, GFP_ATOMIC
);
55 memcpy(ha
->addr
, addr
, addr_len
);
58 ha
->global_use
= global
;
60 list_add_tail_rcu(&ha
->list
, &list
->list
);
65 static int __hw_addr_add(struct netdev_hw_addr_list
*list
, unsigned char *addr
,
66 int addr_len
, unsigned char addr_type
)
68 return __hw_addr_add_ex(list
, addr
, addr_len
, addr_type
, false);
71 static void ha_rcu_free(struct rcu_head
*head
)
73 struct netdev_hw_addr
*ha
;
75 ha
= container_of(head
, struct netdev_hw_addr
, rcu_head
);
79 static int __hw_addr_del_ex(struct netdev_hw_addr_list
*list
,
80 unsigned char *addr
, int addr_len
,
81 unsigned char addr_type
, bool global
)
83 struct netdev_hw_addr
*ha
;
85 list_for_each_entry(ha
, &list
->list
, list
) {
86 if (!memcmp(ha
->addr
, addr
, addr_len
) &&
87 (ha
->type
== addr_type
|| !addr_type
)) {
92 ha
->global_use
= false;
96 list_del_rcu(&ha
->list
);
97 call_rcu(&ha
->rcu_head
, ha_rcu_free
);
105 static int __hw_addr_del(struct netdev_hw_addr_list
*list
, unsigned char *addr
,
106 int addr_len
, unsigned char addr_type
)
108 return __hw_addr_del_ex(list
, addr
, addr_len
, addr_type
, false);
111 int __hw_addr_add_multiple(struct netdev_hw_addr_list
*to_list
,
112 struct netdev_hw_addr_list
*from_list
,
113 int addr_len
, unsigned char addr_type
)
116 struct netdev_hw_addr
*ha
, *ha2
;
119 list_for_each_entry(ha
, &from_list
->list
, list
) {
120 type
= addr_type
? addr_type
: ha
->type
;
121 err
= __hw_addr_add(to_list
, ha
->addr
, addr_len
, type
);
128 list_for_each_entry(ha2
, &from_list
->list
, list
) {
131 type
= addr_type
? addr_type
: ha2
->type
;
132 __hw_addr_del(to_list
, ha2
->addr
, addr_len
, type
);
136 EXPORT_SYMBOL(__hw_addr_add_multiple
);
138 void __hw_addr_del_multiple(struct netdev_hw_addr_list
*to_list
,
139 struct netdev_hw_addr_list
*from_list
,
140 int addr_len
, unsigned char addr_type
)
142 struct netdev_hw_addr
*ha
;
145 list_for_each_entry(ha
, &from_list
->list
, list
) {
146 type
= addr_type
? addr_type
: ha
->type
;
147 __hw_addr_del(to_list
, ha
->addr
, addr_len
, addr_type
);
150 EXPORT_SYMBOL(__hw_addr_del_multiple
);
152 int __hw_addr_sync(struct netdev_hw_addr_list
*to_list
,
153 struct netdev_hw_addr_list
*from_list
,
157 struct netdev_hw_addr
*ha
, *tmp
;
159 list_for_each_entry_safe(ha
, tmp
, &from_list
->list
, list
) {
161 err
= __hw_addr_add(to_list
, ha
->addr
,
167 } else if (ha
->refcount
== 1) {
168 __hw_addr_del(to_list
, ha
->addr
, addr_len
, ha
->type
);
169 __hw_addr_del(from_list
, ha
->addr
, addr_len
, ha
->type
);
174 EXPORT_SYMBOL(__hw_addr_sync
);
176 void __hw_addr_unsync(struct netdev_hw_addr_list
*to_list
,
177 struct netdev_hw_addr_list
*from_list
,
180 struct netdev_hw_addr
*ha
, *tmp
;
182 list_for_each_entry_safe(ha
, tmp
, &from_list
->list
, list
) {
184 __hw_addr_del(to_list
, ha
->addr
,
187 __hw_addr_del(from_list
, ha
->addr
,
192 EXPORT_SYMBOL(__hw_addr_unsync
);
194 void __hw_addr_flush(struct netdev_hw_addr_list
*list
)
196 struct netdev_hw_addr
*ha
, *tmp
;
198 list_for_each_entry_safe(ha
, tmp
, &list
->list
, list
) {
199 list_del_rcu(&ha
->list
);
200 call_rcu(&ha
->rcu_head
, ha_rcu_free
);
204 EXPORT_SYMBOL(__hw_addr_flush
);
206 void __hw_addr_init(struct netdev_hw_addr_list
*list
)
208 INIT_LIST_HEAD(&list
->list
);
211 EXPORT_SYMBOL(__hw_addr_init
);
214 * Device addresses handling functions
218 * dev_addr_flush - Flush device address list
221 * Flush device address list and reset ->dev_addr.
223 * The caller must hold the rtnl_mutex.
225 void dev_addr_flush(struct net_device
*dev
)
227 /* rtnl_mutex must be held here */
229 __hw_addr_flush(&dev
->dev_addrs
);
230 dev
->dev_addr
= NULL
;
232 EXPORT_SYMBOL(dev_addr_flush
);
235 * dev_addr_init - Init device address list
238 * Init device address list and create the first element,
239 * used by ->dev_addr.
241 * The caller must hold the rtnl_mutex.
243 int dev_addr_init(struct net_device
*dev
)
245 unsigned char addr
[MAX_ADDR_LEN
];
246 struct netdev_hw_addr
*ha
;
249 /* rtnl_mutex must be held here */
251 __hw_addr_init(&dev
->dev_addrs
);
252 memset(addr
, 0, sizeof(addr
));
253 err
= __hw_addr_add(&dev
->dev_addrs
, addr
, sizeof(addr
),
254 NETDEV_HW_ADDR_T_LAN
);
257 * Get the first (previously created) address from the list
258 * and set dev_addr pointer to this location.
260 ha
= list_first_entry(&dev
->dev_addrs
.list
,
261 struct netdev_hw_addr
, list
);
262 dev
->dev_addr
= ha
->addr
;
266 EXPORT_SYMBOL(dev_addr_init
);
269 * dev_addr_add - Add a device address
271 * @addr: address to add
272 * @addr_type: address type
274 * Add a device address to the device or increase the reference count if
277 * The caller must hold the rtnl_mutex.
279 int dev_addr_add(struct net_device
*dev
, unsigned char *addr
,
280 unsigned char addr_type
)
286 err
= __hw_addr_add(&dev
->dev_addrs
, addr
, dev
->addr_len
, addr_type
);
288 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
291 EXPORT_SYMBOL(dev_addr_add
);
294 * dev_addr_del - Release a device address.
296 * @addr: address to delete
297 * @addr_type: address type
299 * Release reference to a device address and remove it from the device
300 * if the reference count drops to zero.
302 * The caller must hold the rtnl_mutex.
304 int dev_addr_del(struct net_device
*dev
, unsigned char *addr
,
305 unsigned char addr_type
)
308 struct netdev_hw_addr
*ha
;
313 * We can not remove the first address from the list because
314 * dev->dev_addr points to that.
316 ha
= list_first_entry(&dev
->dev_addrs
.list
,
317 struct netdev_hw_addr
, list
);
318 if (ha
->addr
== dev
->dev_addr
&& ha
->refcount
== 1)
321 err
= __hw_addr_del(&dev
->dev_addrs
, addr
, dev
->addr_len
,
324 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
327 EXPORT_SYMBOL(dev_addr_del
);
330 * dev_addr_add_multiple - Add device addresses from another device
331 * @to_dev: device to which addresses will be added
332 * @from_dev: device from which addresses will be added
333 * @addr_type: address type - 0 means type will be used from from_dev
335 * Add device addresses of the one device to another.
337 * The caller must hold the rtnl_mutex.
339 int dev_addr_add_multiple(struct net_device
*to_dev
,
340 struct net_device
*from_dev
,
341 unsigned char addr_type
)
347 if (from_dev
->addr_len
!= to_dev
->addr_len
)
349 err
= __hw_addr_add_multiple(&to_dev
->dev_addrs
, &from_dev
->dev_addrs
,
350 to_dev
->addr_len
, addr_type
);
352 call_netdevice_notifiers(NETDEV_CHANGEADDR
, to_dev
);
355 EXPORT_SYMBOL(dev_addr_add_multiple
);
358 * dev_addr_del_multiple - Delete device addresses by another device
359 * @to_dev: device where the addresses will be deleted
360 * @from_dev: device by which addresses the addresses will be deleted
361 * @addr_type: address type - 0 means type will used from from_dev
363 * Deletes addresses in to device by the list of addresses in from device.
365 * The caller must hold the rtnl_mutex.
367 int dev_addr_del_multiple(struct net_device
*to_dev
,
368 struct net_device
*from_dev
,
369 unsigned char addr_type
)
373 if (from_dev
->addr_len
!= to_dev
->addr_len
)
375 __hw_addr_del_multiple(&to_dev
->dev_addrs
, &from_dev
->dev_addrs
,
376 to_dev
->addr_len
, addr_type
);
377 call_netdevice_notifiers(NETDEV_CHANGEADDR
, to_dev
);
380 EXPORT_SYMBOL(dev_addr_del_multiple
);
383 * Unicast list handling functions
387 * dev_uc_add - Add a secondary unicast address
389 * @addr: address to add
391 * Add a secondary unicast address to the device or increase
392 * the reference count if it already exists.
394 int dev_uc_add(struct net_device
*dev
, unsigned char *addr
)
398 netif_addr_lock_bh(dev
);
399 err
= __hw_addr_add(&dev
->uc
, addr
, dev
->addr_len
,
400 NETDEV_HW_ADDR_T_UNICAST
);
402 __dev_set_rx_mode(dev
);
403 netif_addr_unlock_bh(dev
);
406 EXPORT_SYMBOL(dev_uc_add
);
409 * dev_uc_del - Release secondary unicast address.
411 * @addr: address to delete
413 * Release reference to a secondary unicast address and remove it
414 * from the device if the reference count drops to zero.
416 int dev_uc_del(struct net_device
*dev
, unsigned char *addr
)
420 netif_addr_lock_bh(dev
);
421 err
= __hw_addr_del(&dev
->uc
, addr
, dev
->addr_len
,
422 NETDEV_HW_ADDR_T_UNICAST
);
424 __dev_set_rx_mode(dev
);
425 netif_addr_unlock_bh(dev
);
428 EXPORT_SYMBOL(dev_uc_del
);
431 * dev_uc_sync - Synchronize device's unicast list to another device
432 * @to: destination device
433 * @from: source device
435 * Add newly added addresses to the destination device and release
436 * addresses that have no users left. The source device must be
437 * locked by netif_tx_lock_bh.
439 * This function is intended to be called from the dev->set_rx_mode
440 * function of layered software devices.
442 int dev_uc_sync(struct net_device
*to
, struct net_device
*from
)
446 if (to
->addr_len
!= from
->addr_len
)
449 netif_addr_lock_bh(to
);
450 err
= __hw_addr_sync(&to
->uc
, &from
->uc
, to
->addr_len
);
452 __dev_set_rx_mode(to
);
453 netif_addr_unlock_bh(to
);
456 EXPORT_SYMBOL(dev_uc_sync
);
459 * dev_uc_unsync - Remove synchronized addresses from the destination device
460 * @to: destination device
461 * @from: source device
463 * Remove all addresses that were added to the destination device by
464 * dev_uc_sync(). This function is intended to be called from the
465 * dev->stop function of layered software devices.
467 void dev_uc_unsync(struct net_device
*to
, struct net_device
*from
)
469 if (to
->addr_len
!= from
->addr_len
)
472 netif_addr_lock_bh(from
);
474 __hw_addr_unsync(&to
->uc
, &from
->uc
, to
->addr_len
);
475 __dev_set_rx_mode(to
);
476 netif_addr_unlock(to
);
477 netif_addr_unlock_bh(from
);
479 EXPORT_SYMBOL(dev_uc_unsync
);
482 * dev_uc_flush - Flush unicast addresses
485 * Flush unicast addresses.
487 void dev_uc_flush(struct net_device
*dev
)
489 netif_addr_lock_bh(dev
);
490 __hw_addr_flush(&dev
->uc
);
491 netif_addr_unlock_bh(dev
);
493 EXPORT_SYMBOL(dev_uc_flush
);
496 * dev_uc_flush - Init unicast address list
499 * Init unicast address list.
501 void dev_uc_init(struct net_device
*dev
)
503 __hw_addr_init(&dev
->uc
);
505 EXPORT_SYMBOL(dev_uc_init
);
508 * Multicast list handling functions
511 static int __dev_mc_add(struct net_device
*dev
, unsigned char *addr
,
516 netif_addr_lock_bh(dev
);
517 err
= __hw_addr_add_ex(&dev
->mc
, addr
, dev
->addr_len
,
518 NETDEV_HW_ADDR_T_MULTICAST
, global
);
520 __dev_set_rx_mode(dev
);
521 netif_addr_unlock_bh(dev
);
525 * dev_mc_add - Add a multicast address
527 * @addr: address to add
529 * Add a multicast address to the device or increase
530 * the reference count if it already exists.
532 int dev_mc_add(struct net_device
*dev
, unsigned char *addr
)
534 return __dev_mc_add(dev
, addr
, false);
536 EXPORT_SYMBOL(dev_mc_add
);
539 * dev_mc_add_global - Add a global multicast address
541 * @addr: address to add
543 * Add a global multicast address to the device.
545 int dev_mc_add_global(struct net_device
*dev
, unsigned char *addr
)
547 return __dev_mc_add(dev
, addr
, true);
549 EXPORT_SYMBOL(dev_mc_add_global
);
551 static int __dev_mc_del(struct net_device
*dev
, unsigned char *addr
,
556 netif_addr_lock_bh(dev
);
557 err
= __hw_addr_del_ex(&dev
->mc
, addr
, dev
->addr_len
,
558 NETDEV_HW_ADDR_T_MULTICAST
, global
);
560 __dev_set_rx_mode(dev
);
561 netif_addr_unlock_bh(dev
);
566 * dev_mc_del - Delete a multicast address.
568 * @addr: address to delete
570 * Release reference to a multicast address and remove it
571 * from the device if the reference count drops to zero.
573 int dev_mc_del(struct net_device
*dev
, unsigned char *addr
)
575 return __dev_mc_del(dev
, addr
, false);
577 EXPORT_SYMBOL(dev_mc_del
);
580 * dev_mc_del_global - Delete a global multicast address.
582 * @addr: address to delete
584 * Release reference to a multicast address and remove it
585 * from the device if the reference count drops to zero.
587 int dev_mc_del_global(struct net_device
*dev
, unsigned char *addr
)
589 return __dev_mc_del(dev
, addr
, true);
591 EXPORT_SYMBOL(dev_mc_del_global
);
594 * dev_mc_sync - Synchronize device's unicast list to another device
595 * @to: destination device
596 * @from: source device
598 * Add newly added addresses to the destination device and release
599 * addresses that have no users left. The source device must be
600 * locked by netif_tx_lock_bh.
602 * This function is intended to be called from the dev->set_multicast_list
603 * or dev->set_rx_mode function of layered software devices.
605 int dev_mc_sync(struct net_device
*to
, struct net_device
*from
)
609 if (to
->addr_len
!= from
->addr_len
)
612 netif_addr_lock_bh(to
);
613 err
= __hw_addr_sync(&to
->mc
, &from
->mc
, to
->addr_len
);
615 __dev_set_rx_mode(to
);
616 netif_addr_unlock_bh(to
);
619 EXPORT_SYMBOL(dev_mc_sync
);
622 * dev_mc_unsync - Remove synchronized addresses from the destination device
623 * @to: destination device
624 * @from: source device
626 * Remove all addresses that were added to the destination device by
627 * dev_mc_sync(). This function is intended to be called from the
628 * dev->stop function of layered software devices.
630 void dev_mc_unsync(struct net_device
*to
, struct net_device
*from
)
632 if (to
->addr_len
!= from
->addr_len
)
635 netif_addr_lock_bh(from
);
637 __hw_addr_unsync(&to
->mc
, &from
->mc
, to
->addr_len
);
638 __dev_set_rx_mode(to
);
639 netif_addr_unlock(to
);
640 netif_addr_unlock_bh(from
);
642 EXPORT_SYMBOL(dev_mc_unsync
);
645 * dev_mc_flush - Flush multicast addresses
648 * Flush multicast addresses.
650 void dev_mc_flush(struct net_device
*dev
)
652 netif_addr_lock_bh(dev
);
653 __hw_addr_flush(&dev
->mc
);
654 netif_addr_unlock_bh(dev
);
656 EXPORT_SYMBOL(dev_mc_flush
);
659 * dev_mc_flush - Init multicast address list
662 * Init multicast address list.
664 void dev_mc_init(struct net_device
*dev
)
666 __hw_addr_init(&dev
->mc
);
668 EXPORT_SYMBOL(dev_mc_init
);
670 #ifdef CONFIG_PROC_FS
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
);