xref: /linux-6.15/net/core/dev_addr_lists.c (revision adeef3e3)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/core/dev_addr_lists.c - Functions for handling net device lists
4  * Copyright (c) 2010 Jiri Pirko <[email protected]>
5  *
6  * This file contains functions for working with unicast, multicast and device
7  * addresses lists.
8  */
9 
10 #include <linux/netdevice.h>
11 #include <linux/rtnetlink.h>
12 #include <linux/export.h>
13 #include <linux/list.h>
14 
15 /*
16  * General list handling functions
17  */
18 
19 static struct netdev_hw_addr*
20 __hw_addr_create(const unsigned char *addr, int addr_len,
21 		 unsigned char addr_type, bool global, bool sync)
22 {
23 	struct netdev_hw_addr *ha;
24 	int alloc_size;
25 
26 	alloc_size = sizeof(*ha);
27 	if (alloc_size < L1_CACHE_BYTES)
28 		alloc_size = L1_CACHE_BYTES;
29 	ha = kmalloc(alloc_size, GFP_ATOMIC);
30 	if (!ha)
31 		return NULL;
32 	memcpy(ha->addr, addr, addr_len);
33 	ha->type = addr_type;
34 	ha->refcount = 1;
35 	ha->global_use = global;
36 	ha->synced = sync ? 1 : 0;
37 	ha->sync_cnt = 0;
38 
39 	return ha;
40 }
41 
42 static int __hw_addr_add_ex(struct netdev_hw_addr_list *list,
43 			    const unsigned char *addr, int addr_len,
44 			    unsigned char addr_type, bool global, bool sync,
45 			    int sync_count, bool exclusive)
46 {
47 	struct rb_node **ins_point = &list->tree.rb_node, *parent = NULL;
48 	struct netdev_hw_addr *ha;
49 
50 	if (addr_len > MAX_ADDR_LEN)
51 		return -EINVAL;
52 
53 	ha = list_first_entry(&list->list, struct netdev_hw_addr, list);
54 	if (ha && !memcmp(addr, ha->addr, addr_len) &&
55 	    (!addr_type || addr_type == ha->type))
56 		goto found_it;
57 
58 	while (*ins_point) {
59 		int diff;
60 
61 		ha = rb_entry(*ins_point, struct netdev_hw_addr, node);
62 		diff = memcmp(addr, ha->addr, addr_len);
63 		if (diff == 0)
64 			diff = memcmp(&addr_type, &ha->type, sizeof(addr_type));
65 
66 		parent = *ins_point;
67 		if (diff < 0) {
68 			ins_point = &parent->rb_left;
69 		} else if (diff > 0) {
70 			ins_point = &parent->rb_right;
71 		} else {
72 found_it:
73 			if (exclusive)
74 				return -EEXIST;
75 			if (global) {
76 				/* check if addr is already used as global */
77 				if (ha->global_use)
78 					return 0;
79 				else
80 					ha->global_use = true;
81 			}
82 			if (sync) {
83 				if (ha->synced && sync_count)
84 					return -EEXIST;
85 				else
86 					ha->synced++;
87 			}
88 			ha->refcount++;
89 			return 0;
90 		}
91 	}
92 
93 	ha = __hw_addr_create(addr, addr_len, addr_type, global, sync);
94 	if (!ha)
95 		return -ENOMEM;
96 
97 	/* The first address in dev->dev_addrs is pointed to by dev->dev_addr
98 	 * and mutated freely by device drivers and netdev ops, so if we insert
99 	 * it into the tree we'll end up with an invalid rbtree.
100 	 */
101 	if (list->count > 0) {
102 		rb_link_node(&ha->node, parent, ins_point);
103 		rb_insert_color(&ha->node, &list->tree);
104 	} else {
105 		RB_CLEAR_NODE(&ha->node);
106 	}
107 
108 	list_add_tail_rcu(&ha->list, &list->list);
109 	list->count++;
110 
111 	return 0;
112 }
113 
114 static int __hw_addr_add(struct netdev_hw_addr_list *list,
115 			 const unsigned char *addr, int addr_len,
116 			 unsigned char addr_type)
117 {
118 	return __hw_addr_add_ex(list, addr, addr_len, addr_type, false, false,
119 				0, false);
120 }
121 
122 static int __hw_addr_del_entry(struct netdev_hw_addr_list *list,
123 			       struct netdev_hw_addr *ha, bool global,
124 			       bool sync)
125 {
126 	if (global && !ha->global_use)
127 		return -ENOENT;
128 
129 	if (sync && !ha->synced)
130 		return -ENOENT;
131 
132 	if (global)
133 		ha->global_use = false;
134 
135 	if (sync)
136 		ha->synced--;
137 
138 	if (--ha->refcount)
139 		return 0;
140 
141 	if (!RB_EMPTY_NODE(&ha->node))
142 		rb_erase(&ha->node, &list->tree);
143 
144 	list_del_rcu(&ha->list);
145 	kfree_rcu(ha, rcu_head);
146 	list->count--;
147 	return 0;
148 }
149 
150 static struct netdev_hw_addr *__hw_addr_lookup(struct netdev_hw_addr_list *list,
151 					       const unsigned char *addr, int addr_len,
152 					       unsigned char addr_type)
153 {
154 	struct netdev_hw_addr *ha;
155 	struct rb_node *node;
156 
157 	/* The first address isn't inserted into the tree because in the dev->dev_addrs
158 	 * list it's the address pointed to by dev->dev_addr which is freely mutated
159 	 * in place, so we need to check it separately.
160 	 */
161 	ha = list_first_entry(&list->list, struct netdev_hw_addr, list);
162 	if (ha && !memcmp(addr, ha->addr, addr_len) &&
163 	    (!addr_type || addr_type == ha->type))
164 		return ha;
165 
166 	node = list->tree.rb_node;
167 
168 	while (node) {
169 		struct netdev_hw_addr *ha = rb_entry(node, struct netdev_hw_addr, node);
170 		int diff = memcmp(addr, ha->addr, addr_len);
171 
172 		if (diff == 0 && addr_type)
173 			diff = memcmp(&addr_type, &ha->type, sizeof(addr_type));
174 
175 		if (diff < 0)
176 			node = node->rb_left;
177 		else if (diff > 0)
178 			node = node->rb_right;
179 		else
180 			return ha;
181 	}
182 
183 	return NULL;
184 }
185 
186 static int __hw_addr_del_ex(struct netdev_hw_addr_list *list,
187 			    const unsigned char *addr, int addr_len,
188 			    unsigned char addr_type, bool global, bool sync)
189 {
190 	struct netdev_hw_addr *ha = __hw_addr_lookup(list, addr, addr_len, addr_type);
191 
192 	if (!ha)
193 		return -ENOENT;
194 	return __hw_addr_del_entry(list, ha, global, sync);
195 }
196 
197 static int __hw_addr_del(struct netdev_hw_addr_list *list,
198 			 const unsigned char *addr, int addr_len,
199 			 unsigned char addr_type)
200 {
201 	return __hw_addr_del_ex(list, addr, addr_len, addr_type, false, false);
202 }
203 
204 static int __hw_addr_sync_one(struct netdev_hw_addr_list *to_list,
205 			       struct netdev_hw_addr *ha,
206 			       int addr_len)
207 {
208 	int err;
209 
210 	err = __hw_addr_add_ex(to_list, ha->addr, addr_len, ha->type,
211 			       false, true, ha->sync_cnt, false);
212 	if (err && err != -EEXIST)
213 		return err;
214 
215 	if (!err) {
216 		ha->sync_cnt++;
217 		ha->refcount++;
218 	}
219 
220 	return 0;
221 }
222 
223 static void __hw_addr_unsync_one(struct netdev_hw_addr_list *to_list,
224 				 struct netdev_hw_addr_list *from_list,
225 				 struct netdev_hw_addr *ha,
226 				 int addr_len)
227 {
228 	int err;
229 
230 	err = __hw_addr_del_ex(to_list, ha->addr, addr_len, ha->type,
231 			       false, true);
232 	if (err)
233 		return;
234 	ha->sync_cnt--;
235 	/* address on from list is not marked synced */
236 	__hw_addr_del_entry(from_list, ha, false, false);
237 }
238 
239 static int __hw_addr_sync_multiple(struct netdev_hw_addr_list *to_list,
240 				   struct netdev_hw_addr_list *from_list,
241 				   int addr_len)
242 {
243 	int err = 0;
244 	struct netdev_hw_addr *ha, *tmp;
245 
246 	list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
247 		if (ha->sync_cnt == ha->refcount) {
248 			__hw_addr_unsync_one(to_list, from_list, ha, addr_len);
249 		} else {
250 			err = __hw_addr_sync_one(to_list, ha, addr_len);
251 			if (err)
252 				break;
253 		}
254 	}
255 	return err;
256 }
257 
258 /* This function only works where there is a strict 1-1 relationship
259  * between source and destionation of they synch. If you ever need to
260  * sync addresses to more then 1 destination, you need to use
261  * __hw_addr_sync_multiple().
262  */
263 int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
264 		   struct netdev_hw_addr_list *from_list,
265 		   int addr_len)
266 {
267 	int err = 0;
268 	struct netdev_hw_addr *ha, *tmp;
269 
270 	list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
271 		if (!ha->sync_cnt) {
272 			err = __hw_addr_sync_one(to_list, ha, addr_len);
273 			if (err)
274 				break;
275 		} else if (ha->refcount == 1)
276 			__hw_addr_unsync_one(to_list, from_list, ha, addr_len);
277 	}
278 	return err;
279 }
280 EXPORT_SYMBOL(__hw_addr_sync);
281 
282 void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
283 		      struct netdev_hw_addr_list *from_list,
284 		      int addr_len)
285 {
286 	struct netdev_hw_addr *ha, *tmp;
287 
288 	list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
289 		if (ha->sync_cnt)
290 			__hw_addr_unsync_one(to_list, from_list, ha, addr_len);
291 	}
292 }
293 EXPORT_SYMBOL(__hw_addr_unsync);
294 
295 /**
296  *  __hw_addr_sync_dev - Synchonize device's multicast list
297  *  @list: address list to syncronize
298  *  @dev:  device to sync
299  *  @sync: function to call if address should be added
300  *  @unsync: function to call if address should be removed
301  *
302  *  This function is intended to be called from the ndo_set_rx_mode
303  *  function of devices that require explicit address add/remove
304  *  notifications.  The unsync function may be NULL in which case
305  *  the addresses requiring removal will simply be removed without
306  *  any notification to the device.
307  **/
308 int __hw_addr_sync_dev(struct netdev_hw_addr_list *list,
309 		       struct net_device *dev,
310 		       int (*sync)(struct net_device *, const unsigned char *),
311 		       int (*unsync)(struct net_device *,
312 				     const unsigned char *))
313 {
314 	struct netdev_hw_addr *ha, *tmp;
315 	int err;
316 
317 	/* first go through and flush out any stale entries */
318 	list_for_each_entry_safe(ha, tmp, &list->list, list) {
319 		if (!ha->sync_cnt || ha->refcount != 1)
320 			continue;
321 
322 		/* if unsync is defined and fails defer unsyncing address */
323 		if (unsync && unsync(dev, ha->addr))
324 			continue;
325 
326 		ha->sync_cnt--;
327 		__hw_addr_del_entry(list, ha, false, false);
328 	}
329 
330 	/* go through and sync new entries to the list */
331 	list_for_each_entry_safe(ha, tmp, &list->list, list) {
332 		if (ha->sync_cnt)
333 			continue;
334 
335 		err = sync(dev, ha->addr);
336 		if (err)
337 			return err;
338 
339 		ha->sync_cnt++;
340 		ha->refcount++;
341 	}
342 
343 	return 0;
344 }
345 EXPORT_SYMBOL(__hw_addr_sync_dev);
346 
347 /**
348  *  __hw_addr_ref_sync_dev - Synchronize device's multicast address list taking
349  *  into account references
350  *  @list: address list to synchronize
351  *  @dev:  device to sync
352  *  @sync: function to call if address or reference on it should be added
353  *  @unsync: function to call if address or some reference on it should removed
354  *
355  *  This function is intended to be called from the ndo_set_rx_mode
356  *  function of devices that require explicit address or references on it
357  *  add/remove notifications. The unsync function may be NULL in which case
358  *  the addresses or references on it requiring removal will simply be
359  *  removed without any notification to the device. That is responsibility of
360  *  the driver to identify and distribute address or references on it between
361  *  internal address tables.
362  **/
363 int __hw_addr_ref_sync_dev(struct netdev_hw_addr_list *list,
364 			   struct net_device *dev,
365 			   int (*sync)(struct net_device *,
366 				       const unsigned char *, int),
367 			   int (*unsync)(struct net_device *,
368 					 const unsigned char *, int))
369 {
370 	struct netdev_hw_addr *ha, *tmp;
371 	int err, ref_cnt;
372 
373 	/* first go through and flush out any unsynced/stale entries */
374 	list_for_each_entry_safe(ha, tmp, &list->list, list) {
375 		/* sync if address is not used */
376 		if ((ha->sync_cnt << 1) <= ha->refcount)
377 			continue;
378 
379 		/* if fails defer unsyncing address */
380 		ref_cnt = ha->refcount - ha->sync_cnt;
381 		if (unsync && unsync(dev, ha->addr, ref_cnt))
382 			continue;
383 
384 		ha->refcount = (ref_cnt << 1) + 1;
385 		ha->sync_cnt = ref_cnt;
386 		__hw_addr_del_entry(list, ha, false, false);
387 	}
388 
389 	/* go through and sync updated/new entries to the list */
390 	list_for_each_entry_safe(ha, tmp, &list->list, list) {
391 		/* sync if address added or reused */
392 		if ((ha->sync_cnt << 1) >= ha->refcount)
393 			continue;
394 
395 		ref_cnt = ha->refcount - ha->sync_cnt;
396 		err = sync(dev, ha->addr, ref_cnt);
397 		if (err)
398 			return err;
399 
400 		ha->refcount = ref_cnt << 1;
401 		ha->sync_cnt = ref_cnt;
402 	}
403 
404 	return 0;
405 }
406 EXPORT_SYMBOL(__hw_addr_ref_sync_dev);
407 
408 /**
409  *  __hw_addr_ref_unsync_dev - Remove synchronized addresses and references on
410  *  it from device
411  *  @list: address list to remove synchronized addresses (references on it) from
412  *  @dev:  device to sync
413  *  @unsync: function to call if address and references on it should be removed
414  *
415  *  Remove all addresses that were added to the device by
416  *  __hw_addr_ref_sync_dev(). This function is intended to be called from the
417  *  ndo_stop or ndo_open functions on devices that require explicit address (or
418  *  references on it) add/remove notifications. If the unsync function pointer
419  *  is NULL then this function can be used to just reset the sync_cnt for the
420  *  addresses in the list.
421  **/
422 void __hw_addr_ref_unsync_dev(struct netdev_hw_addr_list *list,
423 			      struct net_device *dev,
424 			      int (*unsync)(struct net_device *,
425 					    const unsigned char *, int))
426 {
427 	struct netdev_hw_addr *ha, *tmp;
428 
429 	list_for_each_entry_safe(ha, tmp, &list->list, list) {
430 		if (!ha->sync_cnt)
431 			continue;
432 
433 		/* if fails defer unsyncing address */
434 		if (unsync && unsync(dev, ha->addr, ha->sync_cnt))
435 			continue;
436 
437 		ha->refcount -= ha->sync_cnt - 1;
438 		ha->sync_cnt = 0;
439 		__hw_addr_del_entry(list, ha, false, false);
440 	}
441 }
442 EXPORT_SYMBOL(__hw_addr_ref_unsync_dev);
443 
444 /**
445  *  __hw_addr_unsync_dev - Remove synchronized addresses from device
446  *  @list: address list to remove synchronized addresses from
447  *  @dev:  device to sync
448  *  @unsync: function to call if address should be removed
449  *
450  *  Remove all addresses that were added to the device by __hw_addr_sync_dev().
451  *  This function is intended to be called from the ndo_stop or ndo_open
452  *  functions on devices that require explicit address add/remove
453  *  notifications.  If the unsync function pointer is NULL then this function
454  *  can be used to just reset the sync_cnt for the addresses in the list.
455  **/
456 void __hw_addr_unsync_dev(struct netdev_hw_addr_list *list,
457 			  struct net_device *dev,
458 			  int (*unsync)(struct net_device *,
459 					const unsigned char *))
460 {
461 	struct netdev_hw_addr *ha, *tmp;
462 
463 	list_for_each_entry_safe(ha, tmp, &list->list, list) {
464 		if (!ha->sync_cnt)
465 			continue;
466 
467 		/* if unsync is defined and fails defer unsyncing address */
468 		if (unsync && unsync(dev, ha->addr))
469 			continue;
470 
471 		ha->sync_cnt--;
472 		__hw_addr_del_entry(list, ha, false, false);
473 	}
474 }
475 EXPORT_SYMBOL(__hw_addr_unsync_dev);
476 
477 static void __hw_addr_flush(struct netdev_hw_addr_list *list)
478 {
479 	struct netdev_hw_addr *ha, *tmp;
480 
481 	list->tree = RB_ROOT;
482 	list_for_each_entry_safe(ha, tmp, &list->list, list) {
483 		list_del_rcu(&ha->list);
484 		kfree_rcu(ha, rcu_head);
485 	}
486 	list->count = 0;
487 }
488 
489 void __hw_addr_init(struct netdev_hw_addr_list *list)
490 {
491 	INIT_LIST_HEAD(&list->list);
492 	list->count = 0;
493 	list->tree = RB_ROOT;
494 }
495 EXPORT_SYMBOL(__hw_addr_init);
496 
497 /*
498  * Device addresses handling functions
499  */
500 
501 /**
502  *	dev_addr_flush - Flush device address list
503  *	@dev: device
504  *
505  *	Flush device address list and reset ->dev_addr.
506  *
507  *	The caller must hold the rtnl_mutex.
508  */
509 void dev_addr_flush(struct net_device *dev)
510 {
511 	/* rtnl_mutex must be held here */
512 
513 	__hw_addr_flush(&dev->dev_addrs);
514 	dev->dev_addr = NULL;
515 }
516 EXPORT_SYMBOL(dev_addr_flush);
517 
518 /**
519  *	dev_addr_init - Init device address list
520  *	@dev: device
521  *
522  *	Init device address list and create the first element,
523  *	used by ->dev_addr.
524  *
525  *	The caller must hold the rtnl_mutex.
526  */
527 int dev_addr_init(struct net_device *dev)
528 {
529 	unsigned char addr[MAX_ADDR_LEN];
530 	struct netdev_hw_addr *ha;
531 	int err;
532 
533 	/* rtnl_mutex must be held here */
534 
535 	__hw_addr_init(&dev->dev_addrs);
536 	memset(addr, 0, sizeof(addr));
537 	err = __hw_addr_add(&dev->dev_addrs, addr, sizeof(addr),
538 			    NETDEV_HW_ADDR_T_LAN);
539 	if (!err) {
540 		/*
541 		 * Get the first (previously created) address from the list
542 		 * and set dev_addr pointer to this location.
543 		 */
544 		ha = list_first_entry(&dev->dev_addrs.list,
545 				      struct netdev_hw_addr, list);
546 		dev->dev_addr = ha->addr;
547 	}
548 	return err;
549 }
550 EXPORT_SYMBOL(dev_addr_init);
551 
552 void dev_addr_mod(struct net_device *dev, unsigned int offset,
553 		  const void *addr, size_t len)
554 {
555 	struct netdev_hw_addr *ha;
556 
557 	ha = container_of(dev->dev_addr, struct netdev_hw_addr, addr[0]);
558 	memcpy(&ha->addr[offset], addr, len);
559 }
560 EXPORT_SYMBOL(dev_addr_mod);
561 
562 /**
563  *	dev_addr_add - Add a device address
564  *	@dev: device
565  *	@addr: address to add
566  *	@addr_type: address type
567  *
568  *	Add a device address to the device or increase the reference count if
569  *	it already exists.
570  *
571  *	The caller must hold the rtnl_mutex.
572  */
573 int dev_addr_add(struct net_device *dev, const unsigned char *addr,
574 		 unsigned char addr_type)
575 {
576 	int err;
577 
578 	ASSERT_RTNL();
579 
580 	err = dev_pre_changeaddr_notify(dev, addr, NULL);
581 	if (err)
582 		return err;
583 	err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type);
584 	if (!err)
585 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
586 	return err;
587 }
588 EXPORT_SYMBOL(dev_addr_add);
589 
590 /**
591  *	dev_addr_del - Release a device address.
592  *	@dev: device
593  *	@addr: address to delete
594  *	@addr_type: address type
595  *
596  *	Release reference to a device address and remove it from the device
597  *	if the reference count drops to zero.
598  *
599  *	The caller must hold the rtnl_mutex.
600  */
601 int dev_addr_del(struct net_device *dev, const unsigned char *addr,
602 		 unsigned char addr_type)
603 {
604 	int err;
605 	struct netdev_hw_addr *ha;
606 
607 	ASSERT_RTNL();
608 
609 	/*
610 	 * We can not remove the first address from the list because
611 	 * dev->dev_addr points to that.
612 	 */
613 	ha = list_first_entry(&dev->dev_addrs.list,
614 			      struct netdev_hw_addr, list);
615 	if (!memcmp(ha->addr, addr, dev->addr_len) &&
616 	    ha->type == addr_type && ha->refcount == 1)
617 		return -ENOENT;
618 
619 	err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len,
620 			    addr_type);
621 	if (!err)
622 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
623 	return err;
624 }
625 EXPORT_SYMBOL(dev_addr_del);
626 
627 /*
628  * Unicast list handling functions
629  */
630 
631 /**
632  *	dev_uc_add_excl - Add a global secondary unicast address
633  *	@dev: device
634  *	@addr: address to add
635  */
636 int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr)
637 {
638 	int err;
639 
640 	netif_addr_lock_bh(dev);
641 	err = __hw_addr_add_ex(&dev->uc, addr, dev->addr_len,
642 			       NETDEV_HW_ADDR_T_UNICAST, true, false,
643 			       0, true);
644 	if (!err)
645 		__dev_set_rx_mode(dev);
646 	netif_addr_unlock_bh(dev);
647 	return err;
648 }
649 EXPORT_SYMBOL(dev_uc_add_excl);
650 
651 /**
652  *	dev_uc_add - Add a secondary unicast address
653  *	@dev: device
654  *	@addr: address to add
655  *
656  *	Add a secondary unicast address to the device or increase
657  *	the reference count if it already exists.
658  */
659 int dev_uc_add(struct net_device *dev, const unsigned char *addr)
660 {
661 	int err;
662 
663 	netif_addr_lock_bh(dev);
664 	err = __hw_addr_add(&dev->uc, addr, dev->addr_len,
665 			    NETDEV_HW_ADDR_T_UNICAST);
666 	if (!err)
667 		__dev_set_rx_mode(dev);
668 	netif_addr_unlock_bh(dev);
669 	return err;
670 }
671 EXPORT_SYMBOL(dev_uc_add);
672 
673 /**
674  *	dev_uc_del - Release secondary unicast address.
675  *	@dev: device
676  *	@addr: address to delete
677  *
678  *	Release reference to a secondary unicast address and remove it
679  *	from the device if the reference count drops to zero.
680  */
681 int dev_uc_del(struct net_device *dev, const unsigned char *addr)
682 {
683 	int err;
684 
685 	netif_addr_lock_bh(dev);
686 	err = __hw_addr_del(&dev->uc, addr, dev->addr_len,
687 			    NETDEV_HW_ADDR_T_UNICAST);
688 	if (!err)
689 		__dev_set_rx_mode(dev);
690 	netif_addr_unlock_bh(dev);
691 	return err;
692 }
693 EXPORT_SYMBOL(dev_uc_del);
694 
695 /**
696  *	dev_uc_sync - Synchronize device's unicast list to another device
697  *	@to: destination device
698  *	@from: source device
699  *
700  *	Add newly added addresses to the destination device and release
701  *	addresses that have no users left. The source device must be
702  *	locked by netif_addr_lock_bh.
703  *
704  *	This function is intended to be called from the dev->set_rx_mode
705  *	function of layered software devices.  This function assumes that
706  *	addresses will only ever be synced to the @to devices and no other.
707  */
708 int dev_uc_sync(struct net_device *to, struct net_device *from)
709 {
710 	int err = 0;
711 
712 	if (to->addr_len != from->addr_len)
713 		return -EINVAL;
714 
715 	netif_addr_lock(to);
716 	err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
717 	if (!err)
718 		__dev_set_rx_mode(to);
719 	netif_addr_unlock(to);
720 	return err;
721 }
722 EXPORT_SYMBOL(dev_uc_sync);
723 
724 /**
725  *	dev_uc_sync_multiple - Synchronize device's unicast list to another
726  *	device, but allow for multiple calls to sync to multiple devices.
727  *	@to: destination device
728  *	@from: source device
729  *
730  *	Add newly added addresses to the destination device and release
731  *	addresses that have been deleted from the source. The source device
732  *	must be locked by netif_addr_lock_bh.
733  *
734  *	This function is intended to be called from the dev->set_rx_mode
735  *	function of layered software devices.  It allows for a single source
736  *	device to be synced to multiple destination devices.
737  */
738 int dev_uc_sync_multiple(struct net_device *to, struct net_device *from)
739 {
740 	int err = 0;
741 
742 	if (to->addr_len != from->addr_len)
743 		return -EINVAL;
744 
745 	netif_addr_lock(to);
746 	err = __hw_addr_sync_multiple(&to->uc, &from->uc, to->addr_len);
747 	if (!err)
748 		__dev_set_rx_mode(to);
749 	netif_addr_unlock(to);
750 	return err;
751 }
752 EXPORT_SYMBOL(dev_uc_sync_multiple);
753 
754 /**
755  *	dev_uc_unsync - Remove synchronized addresses from the destination device
756  *	@to: destination device
757  *	@from: source device
758  *
759  *	Remove all addresses that were added to the destination device by
760  *	dev_uc_sync(). This function is intended to be called from the
761  *	dev->stop function of layered software devices.
762  */
763 void dev_uc_unsync(struct net_device *to, struct net_device *from)
764 {
765 	if (to->addr_len != from->addr_len)
766 		return;
767 
768 	/* netif_addr_lock_bh() uses lockdep subclass 0, this is okay for two
769 	 * reasons:
770 	 * 1) This is always called without any addr_list_lock, so as the
771 	 *    outermost one here, it must be 0.
772 	 * 2) This is called by some callers after unlinking the upper device,
773 	 *    so the dev->lower_level becomes 1 again.
774 	 * Therefore, the subclass for 'from' is 0, for 'to' is either 1 or
775 	 * larger.
776 	 */
777 	netif_addr_lock_bh(from);
778 	netif_addr_lock(to);
779 	__hw_addr_unsync(&to->uc, &from->uc, to->addr_len);
780 	__dev_set_rx_mode(to);
781 	netif_addr_unlock(to);
782 	netif_addr_unlock_bh(from);
783 }
784 EXPORT_SYMBOL(dev_uc_unsync);
785 
786 /**
787  *	dev_uc_flush - Flush unicast addresses
788  *	@dev: device
789  *
790  *	Flush unicast addresses.
791  */
792 void dev_uc_flush(struct net_device *dev)
793 {
794 	netif_addr_lock_bh(dev);
795 	__hw_addr_flush(&dev->uc);
796 	netif_addr_unlock_bh(dev);
797 }
798 EXPORT_SYMBOL(dev_uc_flush);
799 
800 /**
801  *	dev_uc_init - Init unicast address list
802  *	@dev: device
803  *
804  *	Init unicast address list.
805  */
806 void dev_uc_init(struct net_device *dev)
807 {
808 	__hw_addr_init(&dev->uc);
809 }
810 EXPORT_SYMBOL(dev_uc_init);
811 
812 /*
813  * Multicast list handling functions
814  */
815 
816 /**
817  *	dev_mc_add_excl - Add a global secondary multicast address
818  *	@dev: device
819  *	@addr: address to add
820  */
821 int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr)
822 {
823 	int err;
824 
825 	netif_addr_lock_bh(dev);
826 	err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
827 			       NETDEV_HW_ADDR_T_MULTICAST, true, false,
828 			       0, true);
829 	if (!err)
830 		__dev_set_rx_mode(dev);
831 	netif_addr_unlock_bh(dev);
832 	return err;
833 }
834 EXPORT_SYMBOL(dev_mc_add_excl);
835 
836 static int __dev_mc_add(struct net_device *dev, const unsigned char *addr,
837 			bool global)
838 {
839 	int err;
840 
841 	netif_addr_lock_bh(dev);
842 	err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
843 			       NETDEV_HW_ADDR_T_MULTICAST, global, false,
844 			       0, false);
845 	if (!err)
846 		__dev_set_rx_mode(dev);
847 	netif_addr_unlock_bh(dev);
848 	return err;
849 }
850 /**
851  *	dev_mc_add - Add a multicast address
852  *	@dev: device
853  *	@addr: address to add
854  *
855  *	Add a multicast address to the device or increase
856  *	the reference count if it already exists.
857  */
858 int dev_mc_add(struct net_device *dev, const unsigned char *addr)
859 {
860 	return __dev_mc_add(dev, addr, false);
861 }
862 EXPORT_SYMBOL(dev_mc_add);
863 
864 /**
865  *	dev_mc_add_global - Add a global multicast address
866  *	@dev: device
867  *	@addr: address to add
868  *
869  *	Add a global multicast address to the device.
870  */
871 int dev_mc_add_global(struct net_device *dev, const unsigned char *addr)
872 {
873 	return __dev_mc_add(dev, addr, true);
874 }
875 EXPORT_SYMBOL(dev_mc_add_global);
876 
877 static int __dev_mc_del(struct net_device *dev, const unsigned char *addr,
878 			bool global)
879 {
880 	int err;
881 
882 	netif_addr_lock_bh(dev);
883 	err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
884 			       NETDEV_HW_ADDR_T_MULTICAST, global, false);
885 	if (!err)
886 		__dev_set_rx_mode(dev);
887 	netif_addr_unlock_bh(dev);
888 	return err;
889 }
890 
891 /**
892  *	dev_mc_del - Delete a multicast address.
893  *	@dev: device
894  *	@addr: address to delete
895  *
896  *	Release reference to a multicast address and remove it
897  *	from the device if the reference count drops to zero.
898  */
899 int dev_mc_del(struct net_device *dev, const unsigned char *addr)
900 {
901 	return __dev_mc_del(dev, addr, false);
902 }
903 EXPORT_SYMBOL(dev_mc_del);
904 
905 /**
906  *	dev_mc_del_global - Delete a global multicast address.
907  *	@dev: device
908  *	@addr: address to delete
909  *
910  *	Release reference to a multicast address and remove it
911  *	from the device if the reference count drops to zero.
912  */
913 int dev_mc_del_global(struct net_device *dev, const unsigned char *addr)
914 {
915 	return __dev_mc_del(dev, addr, true);
916 }
917 EXPORT_SYMBOL(dev_mc_del_global);
918 
919 /**
920  *	dev_mc_sync - Synchronize device's multicast list to another device
921  *	@to: destination device
922  *	@from: source device
923  *
924  *	Add newly added addresses to the destination device and release
925  *	addresses that have no users left. The source device must be
926  *	locked by netif_addr_lock_bh.
927  *
928  *	This function is intended to be called from the ndo_set_rx_mode
929  *	function of layered software devices.
930  */
931 int dev_mc_sync(struct net_device *to, struct net_device *from)
932 {
933 	int err = 0;
934 
935 	if (to->addr_len != from->addr_len)
936 		return -EINVAL;
937 
938 	netif_addr_lock(to);
939 	err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
940 	if (!err)
941 		__dev_set_rx_mode(to);
942 	netif_addr_unlock(to);
943 	return err;
944 }
945 EXPORT_SYMBOL(dev_mc_sync);
946 
947 /**
948  *	dev_mc_sync_multiple - Synchronize device's multicast list to another
949  *	device, but allow for multiple calls to sync to multiple devices.
950  *	@to: destination device
951  *	@from: source device
952  *
953  *	Add newly added addresses to the destination device and release
954  *	addresses that have no users left. The source device must be
955  *	locked by netif_addr_lock_bh.
956  *
957  *	This function is intended to be called from the ndo_set_rx_mode
958  *	function of layered software devices.  It allows for a single
959  *	source device to be synced to multiple destination devices.
960  */
961 int dev_mc_sync_multiple(struct net_device *to, struct net_device *from)
962 {
963 	int err = 0;
964 
965 	if (to->addr_len != from->addr_len)
966 		return -EINVAL;
967 
968 	netif_addr_lock(to);
969 	err = __hw_addr_sync_multiple(&to->mc, &from->mc, to->addr_len);
970 	if (!err)
971 		__dev_set_rx_mode(to);
972 	netif_addr_unlock(to);
973 	return err;
974 }
975 EXPORT_SYMBOL(dev_mc_sync_multiple);
976 
977 /**
978  *	dev_mc_unsync - Remove synchronized addresses from the destination device
979  *	@to: destination device
980  *	@from: source device
981  *
982  *	Remove all addresses that were added to the destination device by
983  *	dev_mc_sync(). This function is intended to be called from the
984  *	dev->stop function of layered software devices.
985  */
986 void dev_mc_unsync(struct net_device *to, struct net_device *from)
987 {
988 	if (to->addr_len != from->addr_len)
989 		return;
990 
991 	/* See the above comments inside dev_uc_unsync(). */
992 	netif_addr_lock_bh(from);
993 	netif_addr_lock(to);
994 	__hw_addr_unsync(&to->mc, &from->mc, to->addr_len);
995 	__dev_set_rx_mode(to);
996 	netif_addr_unlock(to);
997 	netif_addr_unlock_bh(from);
998 }
999 EXPORT_SYMBOL(dev_mc_unsync);
1000 
1001 /**
1002  *	dev_mc_flush - Flush multicast addresses
1003  *	@dev: device
1004  *
1005  *	Flush multicast addresses.
1006  */
1007 void dev_mc_flush(struct net_device *dev)
1008 {
1009 	netif_addr_lock_bh(dev);
1010 	__hw_addr_flush(&dev->mc);
1011 	netif_addr_unlock_bh(dev);
1012 }
1013 EXPORT_SYMBOL(dev_mc_flush);
1014 
1015 /**
1016  *	dev_mc_init - Init multicast address list
1017  *	@dev: device
1018  *
1019  *	Init multicast address list.
1020  */
1021 void dev_mc_init(struct net_device *dev)
1022 {
1023 	__hw_addr_init(&dev->mc);
1024 }
1025 EXPORT_SYMBOL(dev_mc_init);
1026