1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/core/dev_addr_lists.c - Functions for handling net device lists
4 * Copyright (c) 2010 Jiri Pirko <jpirko@redhat.com>
6 * This file contains functions for working with unicast, multicast and device
10 #include <linux/netdevice.h>
11 #include <linux/rtnetlink.h>
12 #include <linux/export.h>
13 #include <linux/list.h>
18 * General list handling functions
21 static int __hw_addr_insert(struct netdev_hw_addr_list
*list
,
22 struct netdev_hw_addr
*new, int addr_len
)
24 struct rb_node
**ins_point
= &list
->tree
.rb_node
, *parent
= NULL
;
25 struct netdev_hw_addr
*ha
;
30 ha
= rb_entry(*ins_point
, struct netdev_hw_addr
, node
);
31 diff
= memcmp(new->addr
, ha
->addr
, addr_len
);
33 diff
= memcmp(&new->type
, &ha
->type
, sizeof(new->type
));
37 ins_point
= &parent
->rb_left
;
39 ins_point
= &parent
->rb_right
;
44 rb_link_node_rcu(&new->node
, parent
, ins_point
);
45 rb_insert_color(&new->node
, &list
->tree
);
50 static struct netdev_hw_addr
*
51 __hw_addr_create(const unsigned char *addr
, int addr_len
,
52 unsigned char addr_type
, bool global
, bool sync
)
54 struct netdev_hw_addr
*ha
;
57 alloc_size
= sizeof(*ha
);
58 if (alloc_size
< L1_CACHE_BYTES
)
59 alloc_size
= L1_CACHE_BYTES
;
60 ha
= kmalloc(alloc_size
, GFP_ATOMIC
);
63 memcpy(ha
->addr
, addr
, addr_len
);
66 ha
->global_use
= global
;
67 ha
->synced
= sync
? 1 : 0;
73 static int __hw_addr_add_ex(struct netdev_hw_addr_list
*list
,
74 const unsigned char *addr
, int addr_len
,
75 unsigned char addr_type
, bool global
, bool sync
,
76 int sync_count
, bool exclusive
)
78 struct rb_node
**ins_point
= &list
->tree
.rb_node
, *parent
= NULL
;
79 struct netdev_hw_addr
*ha
;
81 if (addr_len
> MAX_ADDR_LEN
)
87 ha
= rb_entry(*ins_point
, struct netdev_hw_addr
, node
);
88 diff
= memcmp(addr
, ha
->addr
, addr_len
);
90 diff
= memcmp(&addr_type
, &ha
->type
, sizeof(addr_type
));
94 ins_point
= &parent
->rb_left
;
95 } else if (diff
> 0) {
96 ins_point
= &parent
->rb_right
;
101 /* check if addr is already used as global */
105 ha
->global_use
= true;
108 if (ha
->synced
&& sync_count
)
118 ha
= __hw_addr_create(addr
, addr_len
, addr_type
, global
, sync
);
122 rb_link_node(&ha
->node
, parent
, ins_point
);
123 rb_insert_color(&ha
->node
, &list
->tree
);
125 list_add_tail_rcu(&ha
->list
, &list
->list
);
131 static int __hw_addr_add(struct netdev_hw_addr_list
*list
,
132 const unsigned char *addr
, int addr_len
,
133 unsigned char addr_type
)
135 return __hw_addr_add_ex(list
, addr
, addr_len
, addr_type
, false, false,
139 static int __hw_addr_del_entry(struct netdev_hw_addr_list
*list
,
140 struct netdev_hw_addr
*ha
, bool global
,
143 if (global
&& !ha
->global_use
)
146 if (sync
&& !ha
->synced
)
150 ha
->global_use
= false;
158 rb_erase(&ha
->node
, &list
->tree
);
160 list_del_rcu(&ha
->list
);
161 kfree_rcu(ha
, rcu_head
);
166 static struct netdev_hw_addr
*__hw_addr_lookup(struct netdev_hw_addr_list
*list
,
167 const unsigned char *addr
, int addr_len
,
168 unsigned char addr_type
)
170 struct rb_node
*node
;
172 node
= list
->tree
.rb_node
;
175 struct netdev_hw_addr
*ha
= rb_entry(node
, struct netdev_hw_addr
, node
);
176 int diff
= memcmp(addr
, ha
->addr
, addr_len
);
178 if (diff
== 0 && addr_type
)
179 diff
= memcmp(&addr_type
, &ha
->type
, sizeof(addr_type
));
182 node
= node
->rb_left
;
184 node
= node
->rb_right
;
192 static int __hw_addr_del_ex(struct netdev_hw_addr_list
*list
,
193 const unsigned char *addr
, int addr_len
,
194 unsigned char addr_type
, bool global
, bool sync
)
196 struct netdev_hw_addr
*ha
= __hw_addr_lookup(list
, addr
, addr_len
, addr_type
);
200 return __hw_addr_del_entry(list
, ha
, global
, sync
);
203 static int __hw_addr_del(struct netdev_hw_addr_list
*list
,
204 const unsigned char *addr
, int addr_len
,
205 unsigned char addr_type
)
207 return __hw_addr_del_ex(list
, addr
, addr_len
, addr_type
, false, false);
210 static int __hw_addr_sync_one(struct netdev_hw_addr_list
*to_list
,
211 struct netdev_hw_addr
*ha
,
216 err
= __hw_addr_add_ex(to_list
, ha
->addr
, addr_len
, ha
->type
,
217 false, true, ha
->sync_cnt
, false);
218 if (err
&& err
!= -EEXIST
)
229 static void __hw_addr_unsync_one(struct netdev_hw_addr_list
*to_list
,
230 struct netdev_hw_addr_list
*from_list
,
231 struct netdev_hw_addr
*ha
,
236 err
= __hw_addr_del_ex(to_list
, ha
->addr
, addr_len
, ha
->type
,
241 /* address on from list is not marked synced */
242 __hw_addr_del_entry(from_list
, ha
, false, false);
245 int __hw_addr_sync_multiple(struct netdev_hw_addr_list
*to_list
,
246 struct netdev_hw_addr_list
*from_list
,
250 struct netdev_hw_addr
*ha
, *tmp
;
252 list_for_each_entry_safe(ha
, tmp
, &from_list
->list
, list
) {
253 if (ha
->sync_cnt
== ha
->refcount
) {
254 __hw_addr_unsync_one(to_list
, from_list
, ha
, addr_len
);
256 err
= __hw_addr_sync_one(to_list
, ha
, addr_len
);
263 EXPORT_SYMBOL(__hw_addr_sync_multiple
);
265 /* This function only works where there is a strict 1-1 relationship
266 * between source and destination of they synch. If you ever need to
267 * sync addresses to more then 1 destination, you need to use
268 * __hw_addr_sync_multiple().
270 int __hw_addr_sync(struct netdev_hw_addr_list
*to_list
,
271 struct netdev_hw_addr_list
*from_list
,
275 struct netdev_hw_addr
*ha
, *tmp
;
277 list_for_each_entry_safe(ha
, tmp
, &from_list
->list
, list
) {
279 err
= __hw_addr_sync_one(to_list
, ha
, addr_len
);
282 } else if (ha
->refcount
== 1)
283 __hw_addr_unsync_one(to_list
, from_list
, ha
, addr_len
);
287 EXPORT_SYMBOL(__hw_addr_sync
);
289 void __hw_addr_unsync(struct netdev_hw_addr_list
*to_list
,
290 struct netdev_hw_addr_list
*from_list
,
293 struct netdev_hw_addr
*ha
, *tmp
;
295 list_for_each_entry_safe(ha
, tmp
, &from_list
->list
, list
) {
297 __hw_addr_unsync_one(to_list
, from_list
, ha
, addr_len
);
300 EXPORT_SYMBOL(__hw_addr_unsync
);
303 * __hw_addr_sync_dev - Synchronize device's multicast list
304 * @list: address list to synchronize
305 * @dev: device to sync
306 * @sync: function to call if address should be added
307 * @unsync: function to call if address should be removed
309 * This function is intended to be called from the ndo_set_rx_mode
310 * function of devices that require explicit address add/remove
311 * notifications. The unsync function may be NULL in which case
312 * the addresses requiring removal will simply be removed without
313 * any notification to the device.
315 int __hw_addr_sync_dev(struct netdev_hw_addr_list
*list
,
316 struct net_device
*dev
,
317 int (*sync
)(struct net_device
*, const unsigned char *),
318 int (*unsync
)(struct net_device
*,
319 const unsigned char *))
321 struct netdev_hw_addr
*ha
, *tmp
;
324 /* first go through and flush out any stale entries */
325 list_for_each_entry_safe(ha
, tmp
, &list
->list
, list
) {
326 if (!ha
->sync_cnt
|| ha
->refcount
!= 1)
329 /* if unsync is defined and fails defer unsyncing address */
330 if (unsync
&& unsync(dev
, ha
->addr
))
334 __hw_addr_del_entry(list
, ha
, false, false);
337 /* go through and sync new entries to the list */
338 list_for_each_entry_safe(ha
, tmp
, &list
->list
, list
) {
342 err
= sync(dev
, ha
->addr
);
352 EXPORT_SYMBOL(__hw_addr_sync_dev
);
355 * __hw_addr_ref_sync_dev - Synchronize device's multicast address list taking
356 * into account references
357 * @list: address list to synchronize
358 * @dev: device to sync
359 * @sync: function to call if address or reference on it should be added
360 * @unsync: function to call if address or some reference on it should removed
362 * This function is intended to be called from the ndo_set_rx_mode
363 * function of devices that require explicit address or references on it
364 * add/remove notifications. The unsync function may be NULL in which case
365 * the addresses or references on it requiring removal will simply be
366 * removed without any notification to the device. That is responsibility of
367 * the driver to identify and distribute address or references on it between
368 * internal address tables.
370 int __hw_addr_ref_sync_dev(struct netdev_hw_addr_list
*list
,
371 struct net_device
*dev
,
372 int (*sync
)(struct net_device
*,
373 const unsigned char *, int),
374 int (*unsync
)(struct net_device
*,
375 const unsigned char *, int))
377 struct netdev_hw_addr
*ha
, *tmp
;
380 /* first go through and flush out any unsynced/stale entries */
381 list_for_each_entry_safe(ha
, tmp
, &list
->list
, list
) {
382 /* sync if address is not used */
383 if ((ha
->sync_cnt
<< 1) <= ha
->refcount
)
386 /* if fails defer unsyncing address */
387 ref_cnt
= ha
->refcount
- ha
->sync_cnt
;
388 if (unsync
&& unsync(dev
, ha
->addr
, ref_cnt
))
391 ha
->refcount
= (ref_cnt
<< 1) + 1;
392 ha
->sync_cnt
= ref_cnt
;
393 __hw_addr_del_entry(list
, ha
, false, false);
396 /* go through and sync updated/new entries to the list */
397 list_for_each_entry_safe(ha
, tmp
, &list
->list
, list
) {
398 /* sync if address added or reused */
399 if ((ha
->sync_cnt
<< 1) >= ha
->refcount
)
402 ref_cnt
= ha
->refcount
- ha
->sync_cnt
;
403 err
= sync(dev
, ha
->addr
, ref_cnt
);
407 ha
->refcount
= ref_cnt
<< 1;
408 ha
->sync_cnt
= ref_cnt
;
413 EXPORT_SYMBOL(__hw_addr_ref_sync_dev
);
416 * __hw_addr_ref_unsync_dev - Remove synchronized addresses and references on
418 * @list: address list to remove synchronized addresses (references on it) from
419 * @dev: device to sync
420 * @unsync: function to call if address and references on it should be removed
422 * Remove all addresses that were added to the device by
423 * __hw_addr_ref_sync_dev(). This function is intended to be called from the
424 * ndo_stop or ndo_open functions on devices that require explicit address (or
425 * references on it) add/remove notifications. If the unsync function pointer
426 * is NULL then this function can be used to just reset the sync_cnt for the
427 * addresses in the list.
429 void __hw_addr_ref_unsync_dev(struct netdev_hw_addr_list
*list
,
430 struct net_device
*dev
,
431 int (*unsync
)(struct net_device
*,
432 const unsigned char *, int))
434 struct netdev_hw_addr
*ha
, *tmp
;
436 list_for_each_entry_safe(ha
, tmp
, &list
->list
, list
) {
440 /* if fails defer unsyncing address */
441 if (unsync
&& unsync(dev
, ha
->addr
, ha
->sync_cnt
))
444 ha
->refcount
-= ha
->sync_cnt
- 1;
446 __hw_addr_del_entry(list
, ha
, false, false);
449 EXPORT_SYMBOL(__hw_addr_ref_unsync_dev
);
452 * __hw_addr_unsync_dev - Remove synchronized addresses from device
453 * @list: address list to remove synchronized addresses from
454 * @dev: device to sync
455 * @unsync: function to call if address should be removed
457 * Remove all addresses that were added to the device by __hw_addr_sync_dev().
458 * This function is intended to be called from the ndo_stop or ndo_open
459 * functions on devices that require explicit address add/remove
460 * notifications. If the unsync function pointer is NULL then this function
461 * can be used to just reset the sync_cnt for the addresses in the list.
463 void __hw_addr_unsync_dev(struct netdev_hw_addr_list
*list
,
464 struct net_device
*dev
,
465 int (*unsync
)(struct net_device
*,
466 const unsigned char *))
468 struct netdev_hw_addr
*ha
, *tmp
;
470 list_for_each_entry_safe(ha
, tmp
, &list
->list
, list
) {
474 /* if unsync is defined and fails defer unsyncing address */
475 if (unsync
&& unsync(dev
, ha
->addr
))
479 __hw_addr_del_entry(list
, ha
, false, false);
482 EXPORT_SYMBOL(__hw_addr_unsync_dev
);
484 static void __hw_addr_flush(struct netdev_hw_addr_list
*list
)
486 struct netdev_hw_addr
*ha
, *tmp
;
488 list
->tree
= RB_ROOT
;
489 list_for_each_entry_safe(ha
, tmp
, &list
->list
, list
) {
490 list_del_rcu(&ha
->list
);
491 kfree_rcu(ha
, rcu_head
);
496 void __hw_addr_init(struct netdev_hw_addr_list
*list
)
498 INIT_LIST_HEAD(&list
->list
);
500 list
->tree
= RB_ROOT
;
502 EXPORT_SYMBOL(__hw_addr_init
);
505 * Device addresses handling functions
508 /* Check that netdev->dev_addr is not written to directly as this would
509 * break the rbtree layout. All changes should go thru dev_addr_set() and co.
510 * Remove this check in mid-2024.
512 void dev_addr_check(struct net_device
*dev
)
514 if (!memcmp(dev
->dev_addr
, dev
->dev_addr_shadow
, MAX_ADDR_LEN
))
517 netdev_warn(dev
, "Current addr: %*ph\n", MAX_ADDR_LEN
, dev
->dev_addr
);
518 netdev_warn(dev
, "Expected addr: %*ph\n",
519 MAX_ADDR_LEN
, dev
->dev_addr_shadow
);
520 netdev_WARN(dev
, "Incorrect netdev->dev_addr\n");
524 * dev_addr_flush - Flush device address list
527 * Flush device address list and reset ->dev_addr.
529 * The caller must hold the rtnl_mutex.
531 void dev_addr_flush(struct net_device
*dev
)
533 /* rtnl_mutex must be held here */
536 __hw_addr_flush(&dev
->dev_addrs
);
537 dev
->dev_addr
= NULL
;
541 * dev_addr_init - Init device address list
544 * Init device address list and create the first element,
545 * used by ->dev_addr.
547 * The caller must hold the rtnl_mutex.
549 int dev_addr_init(struct net_device
*dev
)
551 unsigned char addr
[MAX_ADDR_LEN
];
552 struct netdev_hw_addr
*ha
;
555 /* rtnl_mutex must be held here */
557 __hw_addr_init(&dev
->dev_addrs
);
558 memset(addr
, 0, sizeof(addr
));
559 err
= __hw_addr_add(&dev
->dev_addrs
, addr
, sizeof(addr
),
560 NETDEV_HW_ADDR_T_LAN
);
563 * Get the first (previously created) address from the list
564 * and set dev_addr pointer to this location.
566 ha
= list_first_entry(&dev
->dev_addrs
.list
,
567 struct netdev_hw_addr
, list
);
568 dev
->dev_addr
= ha
->addr
;
573 void dev_addr_mod(struct net_device
*dev
, unsigned int offset
,
574 const void *addr
, size_t len
)
576 struct netdev_hw_addr
*ha
;
580 ha
= container_of(dev
->dev_addr
, struct netdev_hw_addr
, addr
[0]);
581 rb_erase(&ha
->node
, &dev
->dev_addrs
.tree
);
582 memcpy(&ha
->addr
[offset
], addr
, len
);
583 memcpy(&dev
->dev_addr_shadow
[offset
], addr
, len
);
584 WARN_ON(__hw_addr_insert(&dev
->dev_addrs
, ha
, dev
->addr_len
));
586 EXPORT_SYMBOL(dev_addr_mod
);
589 * dev_addr_add - Add a device address
591 * @addr: address to add
592 * @addr_type: address type
594 * Add a device address to the device or increase the reference count if
597 * The caller must hold the rtnl_mutex.
599 int dev_addr_add(struct net_device
*dev
, const unsigned char *addr
,
600 unsigned char addr_type
)
606 err
= dev_pre_changeaddr_notify(dev
, addr
, NULL
);
609 err
= __hw_addr_add(&dev
->dev_addrs
, addr
, dev
->addr_len
, addr_type
);
611 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
614 EXPORT_SYMBOL(dev_addr_add
);
617 * dev_addr_del - Release a device address.
619 * @addr: address to delete
620 * @addr_type: address type
622 * Release reference to a device address and remove it from the device
623 * if the reference count drops to zero.
625 * The caller must hold the rtnl_mutex.
627 int dev_addr_del(struct net_device
*dev
, const unsigned char *addr
,
628 unsigned char addr_type
)
631 struct netdev_hw_addr
*ha
;
636 * We can not remove the first address from the list because
637 * dev->dev_addr points to that.
639 ha
= list_first_entry(&dev
->dev_addrs
.list
,
640 struct netdev_hw_addr
, list
);
641 if (!memcmp(ha
->addr
, addr
, dev
->addr_len
) &&
642 ha
->type
== addr_type
&& ha
->refcount
== 1)
645 err
= __hw_addr_del(&dev
->dev_addrs
, addr
, dev
->addr_len
,
648 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
651 EXPORT_SYMBOL(dev_addr_del
);
654 * Unicast list handling functions
658 * dev_uc_add_excl - Add a global secondary unicast address
660 * @addr: address to add
662 int dev_uc_add_excl(struct net_device
*dev
, const unsigned char *addr
)
666 netif_addr_lock_bh(dev
);
667 err
= __hw_addr_add_ex(&dev
->uc
, addr
, dev
->addr_len
,
668 NETDEV_HW_ADDR_T_UNICAST
, true, false,
671 __dev_set_rx_mode(dev
);
672 netif_addr_unlock_bh(dev
);
675 EXPORT_SYMBOL(dev_uc_add_excl
);
678 * dev_uc_add - Add a secondary unicast address
680 * @addr: address to add
682 * Add a secondary unicast address to the device or increase
683 * the reference count if it already exists.
685 int dev_uc_add(struct net_device
*dev
, const unsigned char *addr
)
689 netif_addr_lock_bh(dev
);
690 err
= __hw_addr_add(&dev
->uc
, addr
, dev
->addr_len
,
691 NETDEV_HW_ADDR_T_UNICAST
);
693 __dev_set_rx_mode(dev
);
694 netif_addr_unlock_bh(dev
);
697 EXPORT_SYMBOL(dev_uc_add
);
700 * dev_uc_del - Release secondary unicast address.
702 * @addr: address to delete
704 * Release reference to a secondary unicast address and remove it
705 * from the device if the reference count drops to zero.
707 int dev_uc_del(struct net_device
*dev
, const unsigned char *addr
)
711 netif_addr_lock_bh(dev
);
712 err
= __hw_addr_del(&dev
->uc
, addr
, dev
->addr_len
,
713 NETDEV_HW_ADDR_T_UNICAST
);
715 __dev_set_rx_mode(dev
);
716 netif_addr_unlock_bh(dev
);
719 EXPORT_SYMBOL(dev_uc_del
);
722 * dev_uc_sync - Synchronize device's unicast list to another device
723 * @to: destination device
724 * @from: source device
726 * Add newly added addresses to the destination device and release
727 * addresses that have no users left. The source device must be
728 * locked by netif_addr_lock_bh.
730 * This function is intended to be called from the dev->set_rx_mode
731 * function of layered software devices. This function assumes that
732 * addresses will only ever be synced to the @to devices and no other.
734 int dev_uc_sync(struct net_device
*to
, struct net_device
*from
)
738 if (to
->addr_len
!= from
->addr_len
)
742 err
= __hw_addr_sync(&to
->uc
, &from
->uc
, to
->addr_len
);
744 __dev_set_rx_mode(to
);
745 netif_addr_unlock(to
);
748 EXPORT_SYMBOL(dev_uc_sync
);
751 * dev_uc_sync_multiple - Synchronize device's unicast list to another
752 * device, but allow for multiple calls to sync to multiple devices.
753 * @to: destination device
754 * @from: source device
756 * Add newly added addresses to the destination device and release
757 * addresses that have been deleted from the source. The source device
758 * must be locked by netif_addr_lock_bh.
760 * This function is intended to be called from the dev->set_rx_mode
761 * function of layered software devices. It allows for a single source
762 * device to be synced to multiple destination devices.
764 int dev_uc_sync_multiple(struct net_device
*to
, struct net_device
*from
)
768 if (to
->addr_len
!= from
->addr_len
)
772 err
= __hw_addr_sync_multiple(&to
->uc
, &from
->uc
, to
->addr_len
);
774 __dev_set_rx_mode(to
);
775 netif_addr_unlock(to
);
778 EXPORT_SYMBOL(dev_uc_sync_multiple
);
781 * dev_uc_unsync - Remove synchronized addresses from the destination device
782 * @to: destination device
783 * @from: source device
785 * Remove all addresses that were added to the destination device by
786 * dev_uc_sync(). This function is intended to be called from the
787 * dev->stop function of layered software devices.
789 void dev_uc_unsync(struct net_device
*to
, struct net_device
*from
)
791 if (to
->addr_len
!= from
->addr_len
)
794 /* netif_addr_lock_bh() uses lockdep subclass 0, this is okay for two
796 * 1) This is always called without any addr_list_lock, so as the
797 * outermost one here, it must be 0.
798 * 2) This is called by some callers after unlinking the upper device,
799 * so the dev->lower_level becomes 1 again.
800 * Therefore, the subclass for 'from' is 0, for 'to' is either 1 or
803 netif_addr_lock_bh(from
);
805 __hw_addr_unsync(&to
->uc
, &from
->uc
, to
->addr_len
);
806 __dev_set_rx_mode(to
);
807 netif_addr_unlock(to
);
808 netif_addr_unlock_bh(from
);
810 EXPORT_SYMBOL(dev_uc_unsync
);
813 * dev_uc_flush - Flush unicast addresses
816 * Flush unicast addresses.
818 void dev_uc_flush(struct net_device
*dev
)
820 netif_addr_lock_bh(dev
);
821 __hw_addr_flush(&dev
->uc
);
822 netif_addr_unlock_bh(dev
);
824 EXPORT_SYMBOL(dev_uc_flush
);
827 * dev_uc_init - Init unicast address list
830 * Init unicast address list.
832 void dev_uc_init(struct net_device
*dev
)
834 __hw_addr_init(&dev
->uc
);
836 EXPORT_SYMBOL(dev_uc_init
);
839 * Multicast list handling functions
843 * dev_mc_add_excl - Add a global secondary multicast address
845 * @addr: address to add
847 int dev_mc_add_excl(struct net_device
*dev
, const unsigned char *addr
)
851 netif_addr_lock_bh(dev
);
852 err
= __hw_addr_add_ex(&dev
->mc
, addr
, dev
->addr_len
,
853 NETDEV_HW_ADDR_T_MULTICAST
, true, false,
856 __dev_set_rx_mode(dev
);
857 netif_addr_unlock_bh(dev
);
860 EXPORT_SYMBOL(dev_mc_add_excl
);
862 static int __dev_mc_add(struct net_device
*dev
, const unsigned char *addr
,
867 netif_addr_lock_bh(dev
);
868 err
= __hw_addr_add_ex(&dev
->mc
, addr
, dev
->addr_len
,
869 NETDEV_HW_ADDR_T_MULTICAST
, global
, false,
872 __dev_set_rx_mode(dev
);
873 netif_addr_unlock_bh(dev
);
877 * dev_mc_add - Add a multicast address
879 * @addr: address to add
881 * Add a multicast address to the device or increase
882 * the reference count if it already exists.
884 int dev_mc_add(struct net_device
*dev
, const unsigned char *addr
)
886 return __dev_mc_add(dev
, addr
, false);
888 EXPORT_SYMBOL(dev_mc_add
);
891 * dev_mc_add_global - Add a global multicast address
893 * @addr: address to add
895 * Add a global multicast address to the device.
897 int dev_mc_add_global(struct net_device
*dev
, const unsigned char *addr
)
899 return __dev_mc_add(dev
, addr
, true);
901 EXPORT_SYMBOL(dev_mc_add_global
);
903 static int __dev_mc_del(struct net_device
*dev
, const unsigned char *addr
,
908 netif_addr_lock_bh(dev
);
909 err
= __hw_addr_del_ex(&dev
->mc
, addr
, dev
->addr_len
,
910 NETDEV_HW_ADDR_T_MULTICAST
, global
, false);
912 __dev_set_rx_mode(dev
);
913 netif_addr_unlock_bh(dev
);
918 * dev_mc_del - Delete a multicast address.
920 * @addr: address to delete
922 * Release reference to a multicast address and remove it
923 * from the device if the reference count drops to zero.
925 int dev_mc_del(struct net_device
*dev
, const unsigned char *addr
)
927 return __dev_mc_del(dev
, addr
, false);
929 EXPORT_SYMBOL(dev_mc_del
);
932 * dev_mc_del_global - Delete a global multicast address.
934 * @addr: address to delete
936 * Release reference to a multicast address and remove it
937 * from the device if the reference count drops to zero.
939 int dev_mc_del_global(struct net_device
*dev
, const unsigned char *addr
)
941 return __dev_mc_del(dev
, addr
, true);
943 EXPORT_SYMBOL(dev_mc_del_global
);
946 * dev_mc_sync - Synchronize device's multicast list to another device
947 * @to: destination device
948 * @from: source device
950 * Add newly added addresses to the destination device and release
951 * addresses that have no users left. The source device must be
952 * locked by netif_addr_lock_bh.
954 * This function is intended to be called from the ndo_set_rx_mode
955 * function of layered software devices.
957 int dev_mc_sync(struct net_device
*to
, struct net_device
*from
)
961 if (to
->addr_len
!= from
->addr_len
)
965 err
= __hw_addr_sync(&to
->mc
, &from
->mc
, to
->addr_len
);
967 __dev_set_rx_mode(to
);
968 netif_addr_unlock(to
);
971 EXPORT_SYMBOL(dev_mc_sync
);
974 * dev_mc_sync_multiple - Synchronize device's multicast list to another
975 * device, but allow for multiple calls to sync to multiple devices.
976 * @to: destination device
977 * @from: source device
979 * Add newly added addresses to the destination device and release
980 * addresses that have no users left. The source device must be
981 * locked by netif_addr_lock_bh.
983 * This function is intended to be called from the ndo_set_rx_mode
984 * function of layered software devices. It allows for a single
985 * source device to be synced to multiple destination devices.
987 int dev_mc_sync_multiple(struct net_device
*to
, struct net_device
*from
)
991 if (to
->addr_len
!= from
->addr_len
)
995 err
= __hw_addr_sync_multiple(&to
->mc
, &from
->mc
, to
->addr_len
);
997 __dev_set_rx_mode(to
);
998 netif_addr_unlock(to
);
1001 EXPORT_SYMBOL(dev_mc_sync_multiple
);
1004 * dev_mc_unsync - Remove synchronized addresses from the destination device
1005 * @to: destination device
1006 * @from: source device
1008 * Remove all addresses that were added to the destination device by
1009 * dev_mc_sync(). This function is intended to be called from the
1010 * dev->stop function of layered software devices.
1012 void dev_mc_unsync(struct net_device
*to
, struct net_device
*from
)
1014 if (to
->addr_len
!= from
->addr_len
)
1017 /* See the above comments inside dev_uc_unsync(). */
1018 netif_addr_lock_bh(from
);
1019 netif_addr_lock(to
);
1020 __hw_addr_unsync(&to
->mc
, &from
->mc
, to
->addr_len
);
1021 __dev_set_rx_mode(to
);
1022 netif_addr_unlock(to
);
1023 netif_addr_unlock_bh(from
);
1025 EXPORT_SYMBOL(dev_mc_unsync
);
1028 * dev_mc_flush - Flush multicast addresses
1031 * Flush multicast addresses.
1033 void dev_mc_flush(struct net_device
*dev
)
1035 netif_addr_lock_bh(dev
);
1036 __hw_addr_flush(&dev
->mc
);
1037 netif_addr_unlock_bh(dev
);
1039 EXPORT_SYMBOL(dev_mc_flush
);
1042 * dev_mc_init - Init multicast address list
1045 * Init multicast address list.
1047 void dev_mc_init(struct net_device
*dev
)
1049 __hw_addr_init(&dev
->mc
);
1051 EXPORT_SYMBOL(dev_mc_init
);