xref: /linux/net/core/dev_addr_lists.c (revision ebff14be20c3f2c0fbfa2da3321e3043a0ff8650)
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 <jpirko@redhat.com>
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/if_addr.h>
14 #include <linux/list.h>
15 #include <linux/spinlock.h>
16 #include <net/netlink.h>
17 #include <net/rtnetlink.h>
18 #include <net/sock.h>
19 #include <kunit/visibility.h>
20 
21 #include "dev.h"
22 
23 /* RTM_GETMULTICAST dumps use the netns generation counter to detect
24  * changes of dev->mc between dump rounds.
25  */
26 static void __hw_addr_changed(struct netdev_hw_addr_list *list)
27 {
28 	if (list->mc_dev)
29 		atomic_inc(&dev_net(list->mc_dev)->dev_mc_genid);
30 }
31 
32 static void __hw_addr_count_add(struct netdev_hw_addr_list *list, int value)
33 {
34 	list->_count += value;
35 	__hw_addr_changed(list);
36 }
37 
38 static void __hw_addr_count_inc(struct netdev_hw_addr_list *list)
39 {
40 	__hw_addr_count_add(list, 1);
41 }
42 
43 static void __hw_addr_count_dec(struct netdev_hw_addr_list *list)
44 {
45 	__hw_addr_count_add(list, -1);
46 }
47 
48 static void __hw_addr_count_reset(struct netdev_hw_addr_list *list)
49 {
50 	if (!list->_count)
51 		return;
52 	list->_count = 0;
53 	__hw_addr_changed(list);
54 }
55 
56 /*
57  * General list handling functions
58  */
59 
60 static int __hw_addr_insert(struct netdev_hw_addr_list *list,
61 			    struct netdev_hw_addr *new, int addr_len)
62 {
63 	struct rb_node **ins_point = &list->tree.rb_node, *parent = NULL;
64 	struct netdev_hw_addr *ha;
65 
66 	while (*ins_point) {
67 		int diff;
68 
69 		ha = rb_entry(*ins_point, struct netdev_hw_addr, node);
70 		diff = memcmp(new->addr, ha->addr, addr_len);
71 		if (diff == 0)
72 			diff = memcmp(&new->type, &ha->type, sizeof(new->type));
73 
74 		parent = *ins_point;
75 		if (diff < 0)
76 			ins_point = &parent->rb_left;
77 		else if (diff > 0)
78 			ins_point = &parent->rb_right;
79 		else
80 			return -EEXIST;
81 	}
82 
83 	rb_link_node_rcu(&new->node, parent, ins_point);
84 	rb_insert_color(&new->node, &list->tree);
85 
86 	return 0;
87 }
88 
89 static struct netdev_hw_addr*
90 __hw_addr_create(const unsigned char *addr, int addr_len,
91 		 unsigned char addr_type, bool global, bool sync)
92 {
93 	struct netdev_hw_addr *ha;
94 	int alloc_size;
95 
96 	alloc_size = sizeof(*ha);
97 	if (alloc_size < L1_CACHE_BYTES)
98 		alloc_size = L1_CACHE_BYTES;
99 	ha = kmalloc(alloc_size, GFP_ATOMIC);
100 	if (!ha)
101 		return NULL;
102 	memcpy(ha->addr, addr, addr_len);
103 	ha->type = addr_type;
104 	ha->refcount = 1;
105 	ha->global_use = global;
106 	ha->synced = sync ? 1 : 0;
107 	ha->sync_cnt = 0;
108 
109 	return ha;
110 }
111 
112 static int __hw_addr_add_ex(struct netdev_hw_addr_list *list,
113 			    const unsigned char *addr, int addr_len,
114 			    unsigned char addr_type, bool global, bool sync,
115 			    int sync_count, bool exclusive)
116 {
117 	struct rb_node **ins_point = &list->tree.rb_node, *parent = NULL;
118 	struct netdev_hw_addr *ha;
119 
120 	if (addr_len > MAX_ADDR_LEN)
121 		return -EINVAL;
122 
123 	while (*ins_point) {
124 		int diff;
125 
126 		ha = rb_entry(*ins_point, struct netdev_hw_addr, node);
127 		diff = memcmp(addr, ha->addr, addr_len);
128 		if (diff == 0)
129 			diff = memcmp(&addr_type, &ha->type, sizeof(addr_type));
130 
131 		parent = *ins_point;
132 		if (diff < 0) {
133 			ins_point = &parent->rb_left;
134 		} else if (diff > 0) {
135 			ins_point = &parent->rb_right;
136 		} else {
137 			if (exclusive)
138 				return -EEXIST;
139 			if (global) {
140 				/* check if addr is already used as global */
141 				if (ha->global_use)
142 					return 0;
143 				else
144 					ha->global_use = true;
145 			}
146 			if (sync) {
147 				if (ha->synced && sync_count)
148 					return -EEXIST;
149 				else
150 					ha->synced++;
151 			}
152 			ha->refcount++;
153 			return 0;
154 		}
155 	}
156 
157 	ha = __hw_addr_create(addr, addr_len, addr_type, global, sync);
158 	if (!ha)
159 		return -ENOMEM;
160 
161 	rb_link_node(&ha->node, parent, ins_point);
162 	rb_insert_color(&ha->node, &list->tree);
163 
164 	list_add_tail_rcu(&ha->list, &list->list);
165 	__hw_addr_count_inc(list);
166 
167 	return 0;
168 }
169 
170 static int __hw_addr_add(struct netdev_hw_addr_list *list,
171 			 const unsigned char *addr, int addr_len,
172 			 unsigned char addr_type)
173 {
174 	return __hw_addr_add_ex(list, addr, addr_len, addr_type, false, false,
175 				0, false);
176 }
177 
178 static int __hw_addr_del_entry(struct netdev_hw_addr_list *list,
179 			       struct netdev_hw_addr *ha, bool global,
180 			       bool sync)
181 {
182 	if (global && !ha->global_use)
183 		return -ENOENT;
184 
185 	if (sync && !ha->synced)
186 		return -ENOENT;
187 
188 	if (global)
189 		ha->global_use = false;
190 
191 	if (sync)
192 		ha->synced--;
193 
194 	if (--ha->refcount)
195 		return 0;
196 
197 	rb_erase(&ha->node, &list->tree);
198 
199 	list_del_rcu(&ha->list);
200 	kfree_rcu(ha, rcu_head);
201 	__hw_addr_count_dec(list);
202 	return 0;
203 }
204 
205 static struct netdev_hw_addr *__hw_addr_lookup(struct netdev_hw_addr_list *list,
206 					       const unsigned char *addr, int addr_len,
207 					       unsigned char addr_type)
208 {
209 	struct rb_node *node;
210 
211 	node = list->tree.rb_node;
212 
213 	while (node) {
214 		struct netdev_hw_addr *ha = rb_entry(node, struct netdev_hw_addr, node);
215 		int diff = memcmp(addr, ha->addr, addr_len);
216 
217 		if (diff == 0 && addr_type)
218 			diff = memcmp(&addr_type, &ha->type, sizeof(addr_type));
219 
220 		if (diff < 0)
221 			node = node->rb_left;
222 		else if (diff > 0)
223 			node = node->rb_right;
224 		else
225 			return ha;
226 	}
227 
228 	return NULL;
229 }
230 
231 static int __hw_addr_del_ex(struct netdev_hw_addr_list *list,
232 			    const unsigned char *addr, int addr_len,
233 			    unsigned char addr_type, bool global, bool sync)
234 {
235 	struct netdev_hw_addr *ha = __hw_addr_lookup(list, addr, addr_len, addr_type);
236 
237 	if (!ha)
238 		return -ENOENT;
239 	return __hw_addr_del_entry(list, ha, global, sync);
240 }
241 
242 static int __hw_addr_del(struct netdev_hw_addr_list *list,
243 			 const unsigned char *addr, int addr_len,
244 			 unsigned char addr_type)
245 {
246 	return __hw_addr_del_ex(list, addr, addr_len, addr_type, false, false);
247 }
248 
249 static int __hw_addr_sync_one(struct netdev_hw_addr_list *to_list,
250 			       struct netdev_hw_addr *ha,
251 			       int addr_len)
252 {
253 	int err;
254 
255 	err = __hw_addr_add_ex(to_list, ha->addr, addr_len, ha->type,
256 			       false, true, ha->sync_cnt, false);
257 	if (err && err != -EEXIST)
258 		return err;
259 
260 	if (!err) {
261 		ha->sync_cnt++;
262 		ha->refcount++;
263 	}
264 
265 	return 0;
266 }
267 
268 static void __hw_addr_unsync_one(struct netdev_hw_addr_list *to_list,
269 				 struct netdev_hw_addr_list *from_list,
270 				 struct netdev_hw_addr *ha,
271 				 int addr_len)
272 {
273 	int err;
274 
275 	err = __hw_addr_del_ex(to_list, ha->addr, addr_len, ha->type,
276 			       false, true);
277 	if (err)
278 		return;
279 	ha->sync_cnt--;
280 	/* address on from list is not marked synced */
281 	__hw_addr_del_entry(from_list, ha, false, false);
282 }
283 
284 int __hw_addr_sync_multiple(struct netdev_hw_addr_list *to_list,
285 			    struct netdev_hw_addr_list *from_list,
286 			    int addr_len)
287 {
288 	int err = 0;
289 	struct netdev_hw_addr *ha, *tmp;
290 
291 	list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
292 		if (ha->sync_cnt == ha->refcount) {
293 			__hw_addr_unsync_one(to_list, from_list, ha, addr_len);
294 		} else {
295 			err = __hw_addr_sync_one(to_list, ha, addr_len);
296 			if (err)
297 				break;
298 		}
299 	}
300 	return err;
301 }
302 EXPORT_SYMBOL(__hw_addr_sync_multiple);
303 
304 /* This function only works where there is a strict 1-1 relationship
305  * between source and destination of they synch. If you ever need to
306  * sync addresses to more then 1 destination, you need to use
307  * __hw_addr_sync_multiple().
308  */
309 int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
310 		   struct netdev_hw_addr_list *from_list,
311 		   int addr_len)
312 {
313 	int err = 0;
314 	struct netdev_hw_addr *ha, *tmp;
315 
316 	list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
317 		if (!ha->sync_cnt) {
318 			err = __hw_addr_sync_one(to_list, ha, addr_len);
319 			if (err)
320 				break;
321 		} else if (ha->refcount == 1)
322 			__hw_addr_unsync_one(to_list, from_list, ha, addr_len);
323 	}
324 	return err;
325 }
326 EXPORT_SYMBOL(__hw_addr_sync);
327 
328 void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
329 		      struct netdev_hw_addr_list *from_list,
330 		      int addr_len)
331 {
332 	struct netdev_hw_addr *ha, *tmp;
333 
334 	list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
335 		if (ha->sync_cnt)
336 			__hw_addr_unsync_one(to_list, from_list, ha, addr_len);
337 	}
338 }
339 EXPORT_SYMBOL(__hw_addr_unsync);
340 
341 /**
342  *  __hw_addr_sync_dev - Synchronize device's multicast list
343  *  @list: address list to synchronize
344  *  @dev:  device to sync
345  *  @sync: function to call if address should be added
346  *  @unsync: function to call if address should be removed
347  *
348  *  This function is intended to be called from the ndo_set_rx_mode
349  *  function of devices that require explicit address add/remove
350  *  notifications.  The unsync function may be NULL in which case
351  *  the addresses requiring removal will simply be removed without
352  *  any notification to the device.
353  **/
354 int __hw_addr_sync_dev(struct netdev_hw_addr_list *list,
355 		       struct net_device *dev,
356 		       int (*sync)(struct net_device *, const unsigned char *),
357 		       int (*unsync)(struct net_device *,
358 				     const unsigned char *))
359 {
360 	struct netdev_hw_addr *ha, *tmp;
361 	int err;
362 
363 	/* first go through and flush out any stale entries */
364 	list_for_each_entry_safe(ha, tmp, &list->list, list) {
365 		if (!ha->sync_cnt || ha->refcount != 1)
366 			continue;
367 
368 		/* if unsync is defined and fails defer unsyncing address */
369 		if (unsync && unsync(dev, ha->addr))
370 			continue;
371 
372 		ha->sync_cnt--;
373 		__hw_addr_del_entry(list, ha, false, false);
374 	}
375 
376 	/* go through and sync new entries to the list */
377 	list_for_each_entry_safe(ha, tmp, &list->list, list) {
378 		if (ha->sync_cnt)
379 			continue;
380 
381 		err = sync(dev, ha->addr);
382 		if (err)
383 			return err;
384 
385 		ha->sync_cnt++;
386 		ha->refcount++;
387 	}
388 
389 	return 0;
390 }
391 EXPORT_SYMBOL(__hw_addr_sync_dev);
392 
393 /**
394  *  __hw_addr_ref_sync_dev - Synchronize device's multicast address list taking
395  *  into account references
396  *  @list: address list to synchronize
397  *  @dev:  device to sync
398  *  @sync: function to call if address or reference on it should be added
399  *  @unsync: function to call if address or some reference on it should removed
400  *
401  *  This function is intended to be called from the ndo_set_rx_mode
402  *  function of devices that require explicit address or references on it
403  *  add/remove notifications. The unsync function may be NULL in which case
404  *  the addresses or references on it requiring removal will simply be
405  *  removed without any notification to the device. That is responsibility of
406  *  the driver to identify and distribute address or references on it between
407  *  internal address tables.
408  **/
409 int __hw_addr_ref_sync_dev(struct netdev_hw_addr_list *list,
410 			   struct net_device *dev,
411 			   int (*sync)(struct net_device *,
412 				       const unsigned char *, int),
413 			   int (*unsync)(struct net_device *,
414 					 const unsigned char *, int))
415 {
416 	struct netdev_hw_addr *ha, *tmp;
417 	int err, ref_cnt;
418 
419 	/* first go through and flush out any unsynced/stale entries */
420 	list_for_each_entry_safe(ha, tmp, &list->list, list) {
421 		/* sync if address is not used */
422 		if ((ha->sync_cnt << 1) <= ha->refcount)
423 			continue;
424 
425 		/* if fails defer unsyncing address */
426 		ref_cnt = ha->refcount - ha->sync_cnt;
427 		if (unsync && unsync(dev, ha->addr, ref_cnt))
428 			continue;
429 
430 		ha->refcount = (ref_cnt << 1) + 1;
431 		ha->sync_cnt = ref_cnt;
432 		__hw_addr_del_entry(list, ha, false, false);
433 	}
434 
435 	/* go through and sync updated/new entries to the list */
436 	list_for_each_entry_safe(ha, tmp, &list->list, list) {
437 		/* sync if address added or reused */
438 		if ((ha->sync_cnt << 1) >= ha->refcount)
439 			continue;
440 
441 		ref_cnt = ha->refcount - ha->sync_cnt;
442 		err = sync(dev, ha->addr, ref_cnt);
443 		if (err)
444 			return err;
445 
446 		ha->refcount = ref_cnt << 1;
447 		ha->sync_cnt = ref_cnt;
448 	}
449 
450 	return 0;
451 }
452 EXPORT_SYMBOL(__hw_addr_ref_sync_dev);
453 
454 /**
455  *  __hw_addr_ref_unsync_dev - Remove synchronized addresses and references on
456  *  it from device
457  *  @list: address list to remove synchronized addresses (references on it) from
458  *  @dev:  device to sync
459  *  @unsync: function to call if address and references on it should be removed
460  *
461  *  Remove all addresses that were added to the device by
462  *  __hw_addr_ref_sync_dev(). This function is intended to be called from the
463  *  ndo_stop or ndo_open functions on devices that require explicit address (or
464  *  references on it) add/remove notifications. If the unsync function pointer
465  *  is NULL then this function can be used to just reset the sync_cnt for the
466  *  addresses in the list.
467  **/
468 void __hw_addr_ref_unsync_dev(struct netdev_hw_addr_list *list,
469 			      struct net_device *dev,
470 			      int (*unsync)(struct net_device *,
471 					    const unsigned char *, int))
472 {
473 	struct netdev_hw_addr *ha, *tmp;
474 
475 	list_for_each_entry_safe(ha, tmp, &list->list, list) {
476 		if (!ha->sync_cnt)
477 			continue;
478 
479 		/* if fails defer unsyncing address */
480 		if (unsync && unsync(dev, ha->addr, ha->sync_cnt))
481 			continue;
482 
483 		ha->refcount -= ha->sync_cnt - 1;
484 		ha->sync_cnt = 0;
485 		__hw_addr_del_entry(list, ha, false, false);
486 	}
487 }
488 EXPORT_SYMBOL(__hw_addr_ref_unsync_dev);
489 
490 /**
491  *  __hw_addr_unsync_dev - Remove synchronized addresses from device
492  *  @list: address list to remove synchronized addresses from
493  *  @dev:  device to sync
494  *  @unsync: function to call if address should be removed
495  *
496  *  Remove all addresses that were added to the device by __hw_addr_sync_dev().
497  *  This function is intended to be called from the ndo_stop or ndo_open
498  *  functions on devices that require explicit address add/remove
499  *  notifications.  If the unsync function pointer is NULL then this function
500  *  can be used to just reset the sync_cnt for the addresses in the list.
501  **/
502 void __hw_addr_unsync_dev(struct netdev_hw_addr_list *list,
503 			  struct net_device *dev,
504 			  int (*unsync)(struct net_device *,
505 					const unsigned char *))
506 {
507 	struct netdev_hw_addr *ha, *tmp;
508 
509 	list_for_each_entry_safe(ha, tmp, &list->list, list) {
510 		if (!ha->sync_cnt)
511 			continue;
512 
513 		/* if unsync is defined and fails defer unsyncing address */
514 		if (unsync && unsync(dev, ha->addr))
515 			continue;
516 
517 		ha->sync_cnt--;
518 		__hw_addr_del_entry(list, ha, false, false);
519 	}
520 }
521 EXPORT_SYMBOL(__hw_addr_unsync_dev);
522 
523 void __hw_addr_flush(struct netdev_hw_addr_list *list)
524 {
525 	struct netdev_hw_addr *ha, *tmp;
526 
527 	list->tree = RB_ROOT;
528 	list_for_each_entry_safe(ha, tmp, &list->list, list) {
529 		list_del_rcu(&ha->list);
530 		kfree_rcu(ha, rcu_head);
531 	}
532 	__hw_addr_count_reset(list);
533 }
534 EXPORT_SYMBOL_IF_KUNIT(__hw_addr_flush);
535 
536 void __hw_addr_init(struct netdev_hw_addr_list *list)
537 {
538 	INIT_LIST_HEAD(&list->list);
539 	list->_count = 0;
540 	list->tree = RB_ROOT;
541 	list->mc_dev = NULL;
542 }
543 EXPORT_SYMBOL(__hw_addr_init);
544 
545 static void __hw_addr_splice(struct netdev_hw_addr_list *dst,
546 			     struct netdev_hw_addr_list *src)
547 {
548 	src->tree = RB_ROOT;
549 	list_splice_init(&src->list, &dst->list);
550 	__hw_addr_count_add(dst, netdev_hw_addr_list_count(src));
551 	__hw_addr_count_reset(src);
552 }
553 
554 /**
555  *  __hw_addr_list_snapshot - create a snapshot copy of an address list
556  *  @snap: destination snapshot list (needs to be __hw_addr_init-initialized)
557  *  @list: source address list to snapshot
558  *  @addr_len: length of addresses
559  *  @cache: entry cache to reuse entries from; falls back to GFP_ATOMIC
560  *
561  *  Creates a copy of @list reusing entries from @cache when available.
562  *  Must be called under a spinlock.
563  *
564  *  Return: 0 on success, -errno on failure.
565  */
566 int __hw_addr_list_snapshot(struct netdev_hw_addr_list *snap,
567 			    const struct netdev_hw_addr_list *list,
568 			    int addr_len, struct netdev_hw_addr_list *cache)
569 {
570 	struct netdev_hw_addr *ha, *entry;
571 
572 	list_for_each_entry(ha, &list->list, list) {
573 		if (netdev_hw_addr_list_count(cache)) {
574 			entry = list_first_entry(&cache->list,
575 						 struct netdev_hw_addr, list);
576 			list_del(&entry->list);
577 			__hw_addr_count_dec(cache);
578 			memcpy(entry->addr, ha->addr, addr_len);
579 			entry->type = ha->type;
580 			entry->global_use = false;
581 			entry->synced = 0;
582 		} else {
583 			entry = __hw_addr_create(ha->addr, addr_len, ha->type,
584 						 false, false);
585 			if (!entry) {
586 				__hw_addr_flush(snap);
587 				return -ENOMEM;
588 			}
589 		}
590 		entry->sync_cnt = ha->sync_cnt;
591 		entry->refcount = ha->refcount;
592 
593 		list_add_tail(&entry->list, &snap->list);
594 		__hw_addr_insert(snap, entry, addr_len);
595 		__hw_addr_count_inc(snap);
596 	}
597 
598 	return 0;
599 }
600 EXPORT_SYMBOL_IF_KUNIT(__hw_addr_list_snapshot);
601 
602 /**
603  *  __hw_addr_list_reconcile - sync snapshot changes back and free snapshots
604  *  @real_list: the real address list to update
605  *  @work: the working snapshot (modified by driver via __hw_addr_sync_dev)
606  *  @ref: the reference snapshot (untouched copy of original state)
607  *  @addr_len: length of addresses
608  *  @cache: entry cache to return snapshot entries to for reuse
609  *
610  *  Walks the reference snapshot and compares each entry against the work
611  *  snapshot to compute sync_cnt deltas. Applies those deltas to @real_list.
612  *  Returns snapshot entries to @cache for reuse; frees both snapshots.
613  *  Caller must hold netif_addr_lock_bh.
614  */
615 void __hw_addr_list_reconcile(struct netdev_hw_addr_list *real_list,
616 			      struct netdev_hw_addr_list *work,
617 			      struct netdev_hw_addr_list *ref, int addr_len,
618 			      struct netdev_hw_addr_list *cache)
619 {
620 	struct netdev_hw_addr *ref_ha, *tmp, *work_ha, *real_ha;
621 	int delta;
622 
623 	list_for_each_entry_safe(ref_ha, tmp, &ref->list, list) {
624 		work_ha = __hw_addr_lookup(work, ref_ha->addr, addr_len,
625 					   ref_ha->type);
626 		if (work_ha)
627 			delta = work_ha->sync_cnt - ref_ha->sync_cnt;
628 		else
629 			delta = -1;
630 
631 		if (delta == 0)
632 			continue;
633 
634 		real_ha = __hw_addr_lookup(real_list, ref_ha->addr, addr_len,
635 					   ref_ha->type);
636 		if (!real_ha) {
637 			/* The real entry was concurrently removed. If the
638 			 * driver synced this addr to hardware (delta > 0),
639 			 * re-insert it as a stale entry so the next work
640 			 * run unsyncs it from hardware.
641 			 */
642 			if (delta > 0) {
643 				rb_erase(&ref_ha->node, &ref->tree);
644 				list_del(&ref_ha->list);
645 				__hw_addr_count_dec(ref);
646 				ref_ha->sync_cnt = delta;
647 				ref_ha->refcount = delta;
648 				list_add_tail_rcu(&ref_ha->list,
649 						  &real_list->list);
650 				__hw_addr_insert(real_list, ref_ha,
651 						 addr_len);
652 				__hw_addr_count_inc(real_list);
653 			}
654 			continue;
655 		}
656 
657 		real_ha->sync_cnt += delta;
658 		real_ha->refcount += delta;
659 		if (!real_ha->refcount) {
660 			rb_erase(&real_ha->node, &real_list->tree);
661 			list_del_rcu(&real_ha->list);
662 			kfree_rcu(real_ha, rcu_head);
663 			__hw_addr_count_dec(real_list);
664 		}
665 	}
666 
667 	__hw_addr_splice(cache, work);
668 	__hw_addr_splice(cache, ref);
669 }
670 EXPORT_SYMBOL_IF_KUNIT(__hw_addr_list_reconcile);
671 
672 /*
673  * Device addresses handling functions
674  */
675 
676 /* Check that netdev->dev_addr is not written to directly as this would
677  * break the rbtree layout. All changes should go thru dev_addr_set() and co.
678  * Remove this check in mid-2024.
679  */
680 void dev_addr_check(struct net_device *dev)
681 {
682 	if (!memcmp(dev->dev_addr, dev->dev_addr_shadow, MAX_ADDR_LEN))
683 		return;
684 
685 	netdev_warn(dev, "Current addr:  %*ph\n", MAX_ADDR_LEN, dev->dev_addr);
686 	netdev_warn(dev, "Expected addr: %*ph\n",
687 		    MAX_ADDR_LEN, dev->dev_addr_shadow);
688 	netdev_WARN(dev, "Incorrect netdev->dev_addr\n");
689 }
690 
691 /**
692  *	dev_addr_flush - Flush device address list
693  *	@dev: device
694  *
695  *	Flush device address list and reset ->dev_addr.
696  *
697  *	The caller must hold the rtnl_mutex.
698  */
699 void dev_addr_flush(struct net_device *dev)
700 {
701 	/* rtnl_mutex must be held here */
702 	dev_addr_check(dev);
703 
704 	__hw_addr_flush(&dev->dev_addrs);
705 	dev->dev_addr = NULL;
706 }
707 
708 /**
709  *	dev_addr_init - Init device address list
710  *	@dev: device
711  *
712  *	Init device address list and create the first element,
713  *	used by ->dev_addr.
714  *
715  *	The caller must hold the rtnl_mutex.
716  */
717 int dev_addr_init(struct net_device *dev)
718 {
719 	unsigned char addr[MAX_ADDR_LEN];
720 	struct netdev_hw_addr *ha;
721 	int err;
722 
723 	/* rtnl_mutex must be held here */
724 
725 	__hw_addr_init(&dev->dev_addrs);
726 	memset(addr, 0, sizeof(addr));
727 	err = __hw_addr_add(&dev->dev_addrs, addr, sizeof(addr),
728 			    NETDEV_HW_ADDR_T_LAN);
729 	if (!err) {
730 		/*
731 		 * Get the first (previously created) address from the list
732 		 * and set dev_addr pointer to this location.
733 		 */
734 		ha = list_first_entry(&dev->dev_addrs.list,
735 				      struct netdev_hw_addr, list);
736 		dev->dev_addr = ha->addr;
737 	}
738 	return err;
739 }
740 
741 void dev_addr_mod(struct net_device *dev, unsigned int offset,
742 		  const void *addr, size_t len)
743 {
744 	struct netdev_hw_addr *ha;
745 
746 	dev_addr_check(dev);
747 
748 	ha = container_of(dev->dev_addr, struct netdev_hw_addr, addr[0]);
749 	rb_erase(&ha->node, &dev->dev_addrs.tree);
750 	memcpy(&ha->addr[offset], addr, len);
751 	memcpy(&dev->dev_addr_shadow[offset], addr, len);
752 	WARN_ON(__hw_addr_insert(&dev->dev_addrs, ha, dev->addr_len));
753 }
754 EXPORT_SYMBOL(dev_addr_mod);
755 
756 /**
757  *	dev_addr_add - Add a device address
758  *	@dev: device
759  *	@addr: address to add
760  *	@addr_type: address type
761  *
762  *	Add a device address to the device or increase the reference count if
763  *	it already exists.
764  *
765  *	The caller must hold the rtnl_mutex.
766  */
767 int dev_addr_add(struct net_device *dev, const unsigned char *addr,
768 		 unsigned char addr_type)
769 {
770 	int err;
771 
772 	ASSERT_RTNL();
773 
774 	err = netif_pre_changeaddr_notify(dev, addr, NULL);
775 	if (err)
776 		return err;
777 	err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type);
778 	if (!err)
779 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
780 	return err;
781 }
782 EXPORT_SYMBOL(dev_addr_add);
783 
784 /**
785  *	dev_addr_del - Release a device address.
786  *	@dev: device
787  *	@addr: address to delete
788  *	@addr_type: address type
789  *
790  *	Release reference to a device address and remove it from the device
791  *	if the reference count drops to zero.
792  *
793  *	The caller must hold the rtnl_mutex.
794  */
795 int dev_addr_del(struct net_device *dev, const unsigned char *addr,
796 		 unsigned char addr_type)
797 {
798 	int err;
799 	struct netdev_hw_addr *ha;
800 
801 	ASSERT_RTNL();
802 
803 	/*
804 	 * We can not remove the first address from the list because
805 	 * dev->dev_addr points to that.
806 	 */
807 	ha = list_first_entry(&dev->dev_addrs.list,
808 			      struct netdev_hw_addr, list);
809 	if (!memcmp(ha->addr, addr, dev->addr_len) &&
810 	    ha->type == addr_type && ha->refcount == 1)
811 		return -ENOENT;
812 
813 	err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len,
814 			    addr_type);
815 	if (!err)
816 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
817 	return err;
818 }
819 EXPORT_SYMBOL(dev_addr_del);
820 
821 /*
822  * Unicast list handling functions
823  */
824 
825 /**
826  *	dev_uc_add_excl - Add a global secondary unicast address
827  *	@dev: device
828  *	@addr: address to add
829  */
830 int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr)
831 {
832 	int err;
833 
834 	netif_addr_lock_bh(dev);
835 	err = __hw_addr_add_ex(&dev->uc, addr, dev->addr_len,
836 			       NETDEV_HW_ADDR_T_UNICAST, true, false,
837 			       0, true);
838 	if (!err)
839 		__dev_set_rx_mode(dev);
840 	netif_addr_unlock_bh(dev);
841 	return err;
842 }
843 EXPORT_SYMBOL(dev_uc_add_excl);
844 
845 /**
846  *	dev_uc_add - Add a secondary unicast address
847  *	@dev: device
848  *	@addr: address to add
849  *
850  *	Add a secondary unicast address to the device or increase
851  *	the reference count if it already exists.
852  */
853 int dev_uc_add(struct net_device *dev, const unsigned char *addr)
854 {
855 	int err;
856 
857 	netif_addr_lock_bh(dev);
858 	err = __hw_addr_add(&dev->uc, addr, dev->addr_len,
859 			    NETDEV_HW_ADDR_T_UNICAST);
860 	if (!err)
861 		__dev_set_rx_mode(dev);
862 	netif_addr_unlock_bh(dev);
863 	return err;
864 }
865 EXPORT_SYMBOL(dev_uc_add);
866 
867 /**
868  *	dev_uc_del - Release secondary unicast address.
869  *	@dev: device
870  *	@addr: address to delete
871  *
872  *	Release reference to a secondary unicast address and remove it
873  *	from the device if the reference count drops to zero.
874  */
875 int dev_uc_del(struct net_device *dev, const unsigned char *addr)
876 {
877 	int err;
878 
879 	netif_addr_lock_bh(dev);
880 	err = __hw_addr_del(&dev->uc, addr, dev->addr_len,
881 			    NETDEV_HW_ADDR_T_UNICAST);
882 	if (!err)
883 		__dev_set_rx_mode(dev);
884 	netif_addr_unlock_bh(dev);
885 	return err;
886 }
887 EXPORT_SYMBOL(dev_uc_del);
888 
889 /**
890  *	dev_uc_sync - Synchronize device's unicast list to another device
891  *	@to: destination device
892  *	@from: source device
893  *
894  *	Add newly added addresses to the destination device and release
895  *	addresses that have no users left. The source device must be
896  *	locked by netif_addr_lock_bh.
897  *
898  *	This function is intended to be called from the dev->set_rx_mode
899  *	function of layered software devices.  This function assumes that
900  *	addresses will only ever be synced to the @to devices and no other.
901  */
902 int dev_uc_sync(struct net_device *to, struct net_device *from)
903 {
904 	int err = 0;
905 
906 	if (to->addr_len != from->addr_len)
907 		return -EINVAL;
908 
909 	netif_addr_lock(to);
910 	err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
911 	if (!err)
912 		__dev_set_rx_mode(to);
913 	netif_addr_unlock(to);
914 	return err;
915 }
916 EXPORT_SYMBOL(dev_uc_sync);
917 
918 /**
919  *	dev_uc_sync_multiple - Synchronize device's unicast list to another
920  *	device, but allow for multiple calls to sync to multiple devices.
921  *	@to: destination device
922  *	@from: source device
923  *
924  *	Add newly added addresses to the destination device and release
925  *	addresses that have been deleted from the source. The source device
926  *	must be locked by netif_addr_lock_bh.
927  *
928  *	This function is intended to be called from the dev->set_rx_mode
929  *	function of layered software devices.  It allows for a single source
930  *	device to be synced to multiple destination devices.
931  */
932 int dev_uc_sync_multiple(struct net_device *to, struct net_device *from)
933 {
934 	int err = 0;
935 
936 	if (to->addr_len != from->addr_len)
937 		return -EINVAL;
938 
939 	netif_addr_lock(to);
940 	err = __hw_addr_sync_multiple(&to->uc, &from->uc, to->addr_len);
941 	if (!err)
942 		__dev_set_rx_mode(to);
943 	netif_addr_unlock(to);
944 	return err;
945 }
946 EXPORT_SYMBOL(dev_uc_sync_multiple);
947 
948 /**
949  *	dev_uc_unsync - Remove synchronized addresses from the destination device
950  *	@to: destination device
951  *	@from: source device
952  *
953  *	Remove all addresses that were added to the destination device by
954  *	dev_uc_sync(). This function is intended to be called from the
955  *	dev->stop function of layered software devices.
956  */
957 void dev_uc_unsync(struct net_device *to, struct net_device *from)
958 {
959 	if (to->addr_len != from->addr_len)
960 		return;
961 
962 	/* netif_addr_lock_bh() uses lockdep subclass 0, this is okay for two
963 	 * reasons:
964 	 * 1) This is always called without any addr_list_lock, so as the
965 	 *    outermost one here, it must be 0.
966 	 * 2) This is called by some callers after unlinking the upper device,
967 	 *    so the dev->lower_level becomes 1 again.
968 	 * Therefore, the subclass for 'from' is 0, for 'to' is either 1 or
969 	 * larger.
970 	 */
971 	netif_addr_lock_bh(from);
972 	netif_addr_lock(to);
973 	__hw_addr_unsync(&to->uc, &from->uc, to->addr_len);
974 	__dev_set_rx_mode(to);
975 	netif_addr_unlock(to);
976 	netif_addr_unlock_bh(from);
977 }
978 EXPORT_SYMBOL(dev_uc_unsync);
979 
980 /**
981  *	dev_uc_flush - Flush unicast addresses
982  *	@dev: device
983  *
984  *	Flush unicast addresses.
985  */
986 void dev_uc_flush(struct net_device *dev)
987 {
988 	netif_addr_lock_bh(dev);
989 	__hw_addr_flush(&dev->uc);
990 	netif_addr_unlock_bh(dev);
991 }
992 EXPORT_SYMBOL(dev_uc_flush);
993 
994 /**
995  *	dev_uc_init - Init unicast address list
996  *	@dev: device
997  *
998  *	Init unicast address list.
999  */
1000 void dev_uc_init(struct net_device *dev)
1001 {
1002 	__hw_addr_init(&dev->uc);
1003 }
1004 EXPORT_SYMBOL(dev_uc_init);
1005 
1006 /*
1007  * Multicast list handling functions
1008  */
1009 
1010 /**
1011  *	dev_mc_add_excl - Add a global secondary multicast address
1012  *	@dev: device
1013  *	@addr: address to add
1014  */
1015 int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr)
1016 {
1017 	int err;
1018 
1019 	netif_addr_lock_bh(dev);
1020 	err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
1021 			       NETDEV_HW_ADDR_T_MULTICAST, true, false,
1022 			       0, true);
1023 	if (!err)
1024 		__dev_set_rx_mode(dev);
1025 	netif_addr_unlock_bh(dev);
1026 	return err;
1027 }
1028 EXPORT_SYMBOL(dev_mc_add_excl);
1029 
1030 static int __dev_mc_add(struct net_device *dev, const unsigned char *addr,
1031 			bool global)
1032 {
1033 	int err;
1034 
1035 	netif_addr_lock_bh(dev);
1036 	err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
1037 			       NETDEV_HW_ADDR_T_MULTICAST, global, false,
1038 			       0, false);
1039 	if (!err)
1040 		__dev_set_rx_mode(dev);
1041 	netif_addr_unlock_bh(dev);
1042 	return err;
1043 }
1044 /**
1045  *	dev_mc_add - Add a multicast address
1046  *	@dev: device
1047  *	@addr: address to add
1048  *
1049  *	Add a multicast address to the device or increase
1050  *	the reference count if it already exists.
1051  */
1052 int dev_mc_add(struct net_device *dev, const unsigned char *addr)
1053 {
1054 	return __dev_mc_add(dev, addr, false);
1055 }
1056 EXPORT_SYMBOL(dev_mc_add);
1057 
1058 /**
1059  *	dev_mc_add_global - Add a global multicast address
1060  *	@dev: device
1061  *	@addr: address to add
1062  *
1063  *	Add a global multicast address to the device.
1064  */
1065 int dev_mc_add_global(struct net_device *dev, const unsigned char *addr)
1066 {
1067 	return __dev_mc_add(dev, addr, true);
1068 }
1069 EXPORT_SYMBOL(dev_mc_add_global);
1070 
1071 static int __dev_mc_del(struct net_device *dev, const unsigned char *addr,
1072 			bool global)
1073 {
1074 	int err;
1075 
1076 	netif_addr_lock_bh(dev);
1077 	err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
1078 			       NETDEV_HW_ADDR_T_MULTICAST, global, false);
1079 	if (!err)
1080 		__dev_set_rx_mode(dev);
1081 	netif_addr_unlock_bh(dev);
1082 	return err;
1083 }
1084 
1085 /**
1086  *	dev_mc_del - Delete a multicast address.
1087  *	@dev: device
1088  *	@addr: address to delete
1089  *
1090  *	Release reference to a multicast address and remove it
1091  *	from the device if the reference count drops to zero.
1092  */
1093 int dev_mc_del(struct net_device *dev, const unsigned char *addr)
1094 {
1095 	return __dev_mc_del(dev, addr, false);
1096 }
1097 EXPORT_SYMBOL(dev_mc_del);
1098 
1099 /**
1100  *	dev_mc_del_global - Delete a global multicast address.
1101  *	@dev: device
1102  *	@addr: address to delete
1103  *
1104  *	Release reference to a multicast address and remove it
1105  *	from the device if the reference count drops to zero.
1106  */
1107 int dev_mc_del_global(struct net_device *dev, const unsigned char *addr)
1108 {
1109 	return __dev_mc_del(dev, addr, true);
1110 }
1111 EXPORT_SYMBOL(dev_mc_del_global);
1112 
1113 /**
1114  *	dev_mc_sync - Synchronize device's multicast list to another device
1115  *	@to: destination device
1116  *	@from: source device
1117  *
1118  *	Add newly added addresses to the destination device and release
1119  *	addresses that have no users left. The source device must be
1120  *	locked by netif_addr_lock_bh.
1121  *
1122  *	This function is intended to be called from the ndo_set_rx_mode
1123  *	function of layered software devices.
1124  */
1125 int dev_mc_sync(struct net_device *to, struct net_device *from)
1126 {
1127 	int err = 0;
1128 
1129 	if (to->addr_len != from->addr_len)
1130 		return -EINVAL;
1131 
1132 	netif_addr_lock(to);
1133 	err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
1134 	if (!err)
1135 		__dev_set_rx_mode(to);
1136 	netif_addr_unlock(to);
1137 	return err;
1138 }
1139 EXPORT_SYMBOL(dev_mc_sync);
1140 
1141 /**
1142  *	dev_mc_sync_multiple - Synchronize device's multicast list to another
1143  *	device, but allow for multiple calls to sync to multiple devices.
1144  *	@to: destination device
1145  *	@from: source device
1146  *
1147  *	Add newly added addresses to the destination device and release
1148  *	addresses that have no users left. The source device must be
1149  *	locked by netif_addr_lock_bh.
1150  *
1151  *	This function is intended to be called from the ndo_set_rx_mode
1152  *	function of layered software devices.  It allows for a single
1153  *	source device to be synced to multiple destination devices.
1154  */
1155 int dev_mc_sync_multiple(struct net_device *to, struct net_device *from)
1156 {
1157 	int err = 0;
1158 
1159 	if (to->addr_len != from->addr_len)
1160 		return -EINVAL;
1161 
1162 	netif_addr_lock(to);
1163 	err = __hw_addr_sync_multiple(&to->mc, &from->mc, to->addr_len);
1164 	if (!err)
1165 		__dev_set_rx_mode(to);
1166 	netif_addr_unlock(to);
1167 	return err;
1168 }
1169 EXPORT_SYMBOL(dev_mc_sync_multiple);
1170 
1171 /**
1172  *	dev_mc_unsync - Remove synchronized addresses from the destination device
1173  *	@to: destination device
1174  *	@from: source device
1175  *
1176  *	Remove all addresses that were added to the destination device by
1177  *	dev_mc_sync(). This function is intended to be called from the
1178  *	dev->stop function of layered software devices.
1179  */
1180 void dev_mc_unsync(struct net_device *to, struct net_device *from)
1181 {
1182 	if (to->addr_len != from->addr_len)
1183 		return;
1184 
1185 	/* See the above comments inside dev_uc_unsync(). */
1186 	netif_addr_lock_bh(from);
1187 	netif_addr_lock(to);
1188 	__hw_addr_unsync(&to->mc, &from->mc, to->addr_len);
1189 	__dev_set_rx_mode(to);
1190 	netif_addr_unlock(to);
1191 	netif_addr_unlock_bh(from);
1192 }
1193 EXPORT_SYMBOL(dev_mc_unsync);
1194 
1195 /**
1196  *	dev_mc_flush - Flush multicast addresses
1197  *	@dev: device
1198  *
1199  *	Flush multicast addresses.
1200  */
1201 void dev_mc_flush(struct net_device *dev)
1202 {
1203 	netif_addr_lock_bh(dev);
1204 	__hw_addr_flush(&dev->mc);
1205 	netif_addr_unlock_bh(dev);
1206 }
1207 EXPORT_SYMBOL(dev_mc_flush);
1208 
1209 /**
1210  *	dev_mc_init - Init multicast address list
1211  *	@dev: device
1212  *
1213  *	Init multicast address list.
1214  */
1215 void dev_mc_init(struct net_device *dev)
1216 {
1217 	__hw_addr_init(&dev->mc);
1218 	dev->mc.mc_dev = dev;
1219 }
1220 EXPORT_SYMBOL(dev_mc_init);
1221 
1222 static int dev_mc_fill_addr(struct sk_buff *skb, const struct net_device *dev,
1223 			    const struct netdev_hw_addr *ha, u32 portid,
1224 			    u32 seq, unsigned int flags, int netnsid)
1225 {
1226 	struct ifaddrmsg *ifm;
1227 	struct nlmsghdr *nlh;
1228 
1229 	nlh = nlmsg_put(skb, portid, seq, RTM_GETMULTICAST, sizeof(*ifm),
1230 			flags);
1231 	if (!nlh)
1232 		return -EMSGSIZE;
1233 
1234 	ifm = nlmsg_data(nlh);
1235 	ifm->ifa_family = AF_PACKET;
1236 	ifm->ifa_prefixlen = 0;
1237 	ifm->ifa_flags = 0;
1238 	ifm->ifa_scope = RT_SCOPE_LINK;
1239 	ifm->ifa_index = dev->ifindex;
1240 
1241 	if ((netnsid >= 0 &&
1242 	     nla_put_s32(skb, IFA_TARGET_NETNSID, netnsid)) ||
1243 	    nla_put(skb, IFA_MULTICAST, dev->addr_len, ha->addr) ||
1244 	    nla_put_u32(skb, IFA_MC_USERS, ha->refcount) ||
1245 	    nla_put_u32(skb, IFA_FLAGS, ha->global_use ? IFA_F_GLOBAL : 0)) {
1246 		nlmsg_cancel(skb, nlh);
1247 		return -EMSGSIZE;
1248 	}
1249 
1250 	nlmsg_end(skb, nlh);
1251 	return 0;
1252 }
1253 
1254 /* Combine dev_mc_genid and dev_base_seq to detect changes, like
1255  * inet_base_seq().
1256  */
1257 static u32 dev_mc_base_seq(const struct net *net)
1258 {
1259 	u32 res = atomic_read(&net->dev_mc_genid) +
1260 		  READ_ONCE(net->dev_base_seq);
1261 
1262 	/* Must not return 0 (see nl_dump_check_consistent()). */
1263 	if (!res)
1264 		res = 0x80000000;
1265 	return res;
1266 }
1267 
1268 static int dev_mc_dump_dev(struct net_device *dev, struct sk_buff *skb,
1269 			   struct netlink_callback *cb, int *s_addr_idx,
1270 			   unsigned int flags, int netnsid)
1271 {
1272 	struct netdev_hw_addr *ha;
1273 	int addr_idx = 0;
1274 	int err = 0;
1275 
1276 	netif_addr_lock_bh(dev);
1277 	netdev_for_each_mc_addr(ha, dev) {
1278 		if (addr_idx < *s_addr_idx) {
1279 			addr_idx++;
1280 			continue;
1281 		}
1282 		err = dev_mc_fill_addr(skb, dev, ha, NETLINK_CB(cb->skb).portid,
1283 				       cb->nlh->nlmsg_seq, flags, netnsid);
1284 		if (err < 0)
1285 			break;
1286 		addr_idx++;
1287 	}
1288 	netif_addr_unlock_bh(dev);
1289 
1290 	*s_addr_idx = err < 0 ? addr_idx : 0;
1291 
1292 	return err;
1293 }
1294 
1295 struct dev_mc_dump_filter {
1296 	struct net *tgt_net;
1297 	netns_tracker ns_tracker;
1298 	int netnsid;
1299 	int ifindex;
1300 };
1301 
1302 static const struct nla_policy dev_mc_dump_policy[IFA_MAX + 1] = {
1303 	[IFA_TARGET_NETNSID]	= { .type = NLA_S32 },
1304 };
1305 
1306 static int dev_mc_valid_dump_req(const struct nlmsghdr *nlh, struct sock *sk,
1307 				 struct dev_mc_dump_filter *filter,
1308 				 struct netlink_ext_ack *extack)
1309 {
1310 	struct nlattr *tb[IFA_MAX + 1];
1311 	struct ifaddrmsg *ifm;
1312 	int err;
1313 
1314 	ifm = nlmsg_payload(nlh, sizeof(*ifm));
1315 	if (!ifm) {
1316 		NL_SET_ERR_MSG(extack,
1317 			       "Invalid header for multicast dump request");
1318 		return -EINVAL;
1319 	}
1320 
1321 	if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
1322 		NL_SET_ERR_MSG(extack,
1323 			       "Invalid values in multicast dump header");
1324 		return -EINVAL;
1325 	}
1326 
1327 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX,
1328 			  dev_mc_dump_policy, extack);
1329 	if (err < 0)
1330 		return err;
1331 
1332 	if (tb[IFA_TARGET_NETNSID]) {
1333 		struct net *net;
1334 
1335 		filter->netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
1336 		net = rtnl_get_net_ns_capable(sk, filter->netnsid);
1337 		if (IS_ERR(net)) {
1338 			NL_SET_ERR_MSG(extack,
1339 				       "Invalid target network namespace id");
1340 			return PTR_ERR(net);
1341 		}
1342 		netns_tracker_alloc(net, &filter->ns_tracker, GFP_KERNEL);
1343 		filter->tgt_net = net;
1344 	}
1345 
1346 	filter->ifindex = ifm->ifa_index;
1347 
1348 	return 0;
1349 }
1350 
1351 int dev_mc_dump(struct sk_buff *skb, struct netlink_callback *cb)
1352 {
1353 	struct dev_mc_dump_filter filter = {
1354 		.tgt_net = sock_net(skb->sk),
1355 		.netnsid = -1,
1356 	};
1357 	unsigned int flags = NLM_F_MULTI;
1358 	struct {
1359 		unsigned long ifindex;
1360 		int addr_idx;
1361 	} *ctx = (void *)cb->ctx;
1362 	unsigned long s_ifindex;
1363 	struct net_device *dev;
1364 	int err;
1365 
1366 	err = dev_mc_valid_dump_req(cb->nlh, skb->sk, &filter, cb->extack);
1367 	if (err < 0)
1368 		return err;
1369 
1370 	rcu_read_lock();
1371 
1372 	cb->seq = dev_mc_base_seq(filter.tgt_net);
1373 	if (filter.ifindex) {
1374 		cb->answer_flags |= NLM_F_DUMP_FILTERED;
1375 		flags |= NLM_F_DUMP_FILTERED;
1376 		dev = dev_get_by_index_rcu(filter.tgt_net, filter.ifindex);
1377 		if (dev)
1378 			err = dev_mc_dump_dev(dev, skb, cb, &ctx->addr_idx,
1379 					      flags, filter.netnsid);
1380 		else
1381 			err = -ENODEV;
1382 	} else {
1383 		s_ifindex = ctx->ifindex;
1384 		for_each_netdev_dump(filter.tgt_net, dev, ctx->ifindex) {
1385 			/* The device the dump stopped at is gone, do not
1386 			 * skip entries of the next one.
1387 			 */
1388 			if (dev->ifindex != s_ifindex)
1389 				ctx->addr_idx = 0;
1390 			err = dev_mc_dump_dev(dev, skb, cb, &ctx->addr_idx,
1391 					      flags, filter.netnsid);
1392 			if (err < 0)
1393 				break;
1394 		}
1395 	}
1396 	/* A round without a message has nothing to flag, the NLMSG_DONE
1397 	 * check then compares against the previous round.
1398 	 */
1399 	if (skb->len)
1400 		nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1401 	/* Let the NLMSG_DONE check see changes made during the last round */
1402 	cb->seq = dev_mc_base_seq(filter.tgt_net);
1403 
1404 	rcu_read_unlock();
1405 	if (filter.netnsid >= 0)
1406 		put_net_track(filter.tgt_net, &filter.ns_tracker);
1407 	return err;
1408 }
1409 
1410 static int netif_addr_lists_snapshot(struct net_device *dev,
1411 				     struct netdev_hw_addr_list *uc_snap,
1412 				     struct netdev_hw_addr_list *mc_snap,
1413 				     struct netdev_hw_addr_list *uc_ref,
1414 				     struct netdev_hw_addr_list *mc_ref)
1415 {
1416 	int err;
1417 
1418 	err = __hw_addr_list_snapshot(uc_snap, &dev->uc, dev->addr_len,
1419 				      &dev->rx_mode_addr_cache);
1420 	if (!err)
1421 		err = __hw_addr_list_snapshot(uc_ref, &dev->uc, dev->addr_len,
1422 					      &dev->rx_mode_addr_cache);
1423 	if (!err)
1424 		err = __hw_addr_list_snapshot(mc_snap, &dev->mc,
1425 					      dev->addr_len,
1426 					      &dev->rx_mode_addr_cache);
1427 	if (!err)
1428 		err = __hw_addr_list_snapshot(mc_ref, &dev->mc, dev->addr_len,
1429 					      &dev->rx_mode_addr_cache);
1430 
1431 	if (err) {
1432 		__hw_addr_flush(uc_snap);
1433 		__hw_addr_flush(uc_ref);
1434 		__hw_addr_flush(mc_snap);
1435 	}
1436 
1437 	return err;
1438 }
1439 
1440 static void netif_addr_lists_reconcile(struct net_device *dev,
1441 				       struct netdev_hw_addr_list *uc_snap,
1442 				       struct netdev_hw_addr_list *mc_snap,
1443 				       struct netdev_hw_addr_list *uc_ref,
1444 				       struct netdev_hw_addr_list *mc_ref)
1445 {
1446 	__hw_addr_list_reconcile(&dev->uc, uc_snap, uc_ref, dev->addr_len,
1447 				 &dev->rx_mode_addr_cache);
1448 	__hw_addr_list_reconcile(&dev->mc, mc_snap, mc_ref, dev->addr_len,
1449 				 &dev->rx_mode_addr_cache);
1450 }
1451 
1452 /**
1453  * netif_uc_promisc_update() - evaluate whether uc_promisc should be toggled.
1454  * @dev: device
1455  *
1456  * Must be called under netif_addr_lock_bh.
1457  * Return: +1 to enter promisc, -1 to leave, 0 for no change.
1458  */
1459 static int netif_uc_promisc_update(struct net_device *dev)
1460 {
1461 	if (dev->priv_flags & IFF_UNICAST_FLT)
1462 		return 0;
1463 
1464 	if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
1465 		dev->uc_promisc = true;
1466 		return 1;
1467 	}
1468 	if (netdev_uc_empty(dev) && dev->uc_promisc) {
1469 		dev->uc_promisc = false;
1470 		return -1;
1471 	}
1472 	return 0;
1473 }
1474 
1475 /* Total retry budget (4): 1+2+4+8 = 15 seconds */
1476 #define NETIF_RX_MODE_RETRY_MAX	4
1477 
1478 void netif_rx_mode_schedule_retry(struct net_device *dev)
1479 {
1480 	unsigned long delay;
1481 
1482 	netdev_assert_locked_ops_compat(dev);
1483 
1484 	if (dev->rx_mode_retry_count >= NETIF_RX_MODE_RETRY_MAX) {
1485 		netdev_err(dev, "rx_mode retry limit reached, giving up\n");
1486 		return;
1487 	}
1488 
1489 	delay = HZ << dev->rx_mode_retry_count;
1490 	if (mod_timer(&dev->rx_mode_retry_timer, jiffies + delay))
1491 		return;
1492 	if (!dev->rx_mode_retry_count)
1493 		netdev_info(dev, "rx_mode install failed, retrying with backoff\n");
1494 	dev->rx_mode_retry_count++;
1495 }
1496 EXPORT_SYMBOL_GPL(netif_rx_mode_schedule_retry);
1497 
1498 void netif_rx_mode_cancel_retry(struct net_device *dev)
1499 {
1500 	timer_delete_sync(&dev->rx_mode_retry_timer);
1501 	dev->rx_mode_retry_count = 0;
1502 }
1503 
1504 void netif_rx_mode_run(struct net_device *dev)
1505 {
1506 	struct netdev_hw_addr_list uc_snap, mc_snap, uc_ref, mc_ref;
1507 	const struct net_device_ops *ops = dev->netdev_ops;
1508 	int promisc_inc;
1509 	int err;
1510 
1511 	might_sleep();
1512 	netdev_assert_locked_ops_compat(dev);
1513 
1514 	__hw_addr_init(&uc_snap);
1515 	__hw_addr_init(&mc_snap);
1516 	__hw_addr_init(&uc_ref);
1517 	__hw_addr_init(&mc_ref);
1518 
1519 	if (!(dev->flags & IFF_UP) || !netif_device_present(dev))
1520 		return;
1521 
1522 	if (ops->ndo_set_rx_mode_async) {
1523 		netif_addr_lock_bh(dev);
1524 		err = netif_addr_lists_snapshot(dev, &uc_snap, &mc_snap,
1525 						&uc_ref, &mc_ref);
1526 		if (err) {
1527 			netif_addr_unlock_bh(dev);
1528 			netif_rx_mode_schedule_retry(dev);
1529 			return;
1530 		}
1531 
1532 		promisc_inc = netif_uc_promisc_update(dev);
1533 		netif_addr_unlock_bh(dev);
1534 	} else {
1535 		netif_addr_lock_bh(dev);
1536 		promisc_inc = netif_uc_promisc_update(dev);
1537 		netif_addr_unlock_bh(dev);
1538 	}
1539 
1540 	if (promisc_inc)
1541 		__dev_set_promiscuity(dev, promisc_inc, false);
1542 
1543 	if (ops->ndo_set_rx_mode_async) {
1544 		err = ops->ndo_set_rx_mode_async(dev, &uc_snap, &mc_snap);
1545 
1546 		netif_addr_lock_bh(dev);
1547 		netif_addr_lists_reconcile(dev, &uc_snap, &mc_snap,
1548 					   &uc_ref, &mc_ref);
1549 		netif_addr_unlock_bh(dev);
1550 
1551 		if (err)
1552 			netif_rx_mode_schedule_retry(dev);
1553 		else
1554 			dev->rx_mode_retry_count = 0;
1555 	} else if (ops->ndo_set_rx_mode) {
1556 		netif_addr_lock_bh(dev);
1557 		ops->ndo_set_rx_mode(dev);
1558 		netif_addr_unlock_bh(dev);
1559 	}
1560 }
1561 
1562 static void netif_rx_mode_queue(struct net_device *dev)
1563 {
1564 	__netdev_work_core_sched(dev, NETDEV_WORK_RX_MODE);
1565 }
1566 
1567 static void netif_rx_mode_retry(struct timer_list *t)
1568 {
1569 	struct net_device *dev =
1570 		timer_container_of(dev, t, rx_mode_retry_timer);
1571 
1572 	netif_rx_mode_queue(dev);
1573 }
1574 
1575 void netif_rx_mode_init(struct net_device *dev)
1576 {
1577 	__hw_addr_init(&dev->rx_mode_addr_cache);
1578 	timer_setup(&dev->rx_mode_retry_timer, netif_rx_mode_retry, 0);
1579 }
1580 
1581 /**
1582  * __dev_set_rx_mode() - upload unicast and multicast address lists to device
1583  * and configure RX filtering.
1584  * @dev: device
1585  *
1586  * When the device doesn't support unicast filtering it is put in promiscuous
1587  * mode while unicast addresses are present.
1588  */
1589 void __dev_set_rx_mode(struct net_device *dev)
1590 {
1591 	const struct net_device_ops *ops = dev->netdev_ops;
1592 	int promisc_inc;
1593 
1594 	/* dev_open will call this function so the list will stay sane. */
1595 	if (!(dev->flags & IFF_UP))
1596 		return;
1597 
1598 	if (!netif_device_present(dev))
1599 		return;
1600 
1601 	if (ops->ndo_set_rx_mode_async || ops->ndo_change_rx_flags ||
1602 	    netdev_need_ops_lock(dev)) {
1603 		netif_rx_mode_queue(dev);
1604 		return;
1605 	}
1606 
1607 	/* Legacy path for non-ops-locked HW devices. */
1608 
1609 	promisc_inc = netif_uc_promisc_update(dev);
1610 	if (promisc_inc)
1611 		__dev_set_promiscuity(dev, promisc_inc, false);
1612 
1613 	if (ops->ndo_set_rx_mode)
1614 		ops->ndo_set_rx_mode(dev);
1615 }
1616 
1617 void dev_set_rx_mode(struct net_device *dev)
1618 {
1619 	netif_addr_lock_bh(dev);
1620 	__dev_set_rx_mode(dev);
1621 	netif_addr_unlock_bh(dev);
1622 }
1623 
1624 /**
1625  * netif_rx_mode_sync() - sync rx mode inline
1626  * @dev: network device
1627  *
1628  * Drivers implementing ndo_set_rx_mode_async() have their rx mode callback
1629  * executed from a workqueue. This allows the callback to sleep, but means
1630  * the hardware update is deferred and may not be visible to userspace
1631  * by the time the initiating syscall returns. netif_rx_mode_sync() steals
1632  * workqueue update and executes it inline. This preserves the atomicity of
1633  * operations to the userspace.
1634  */
1635 void netif_rx_mode_sync(struct net_device *dev)
1636 {
1637 	if (__netdev_work_core_cancel(dev, NETDEV_WORK_RX_MODE))
1638 		netif_rx_mode_run(dev);
1639 }
1640