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