• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 /*
16  * General list handling functions
17  */
18 
19 static struct netdev_hw_addr*
__hw_addr_create(const unsigned char * addr,int addr_len,unsigned char addr_type,bool global,bool sync)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 
__hw_addr_add_ex(struct netdev_hw_addr_list * list,const unsigned char * addr,int addr_len,unsigned char addr_type,bool global,bool sync,int sync_count,bool exclusive)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 
__hw_addr_add(struct netdev_hw_addr_list * list,const unsigned char * addr,int addr_len,unsigned char addr_type)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 
__hw_addr_del_entry(struct netdev_hw_addr_list * list,struct netdev_hw_addr * ha,bool global,bool sync)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 
__hw_addr_lookup(struct netdev_hw_addr_list * list,const unsigned char * addr,int addr_len,unsigned char addr_type)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 
__hw_addr_del_ex(struct netdev_hw_addr_list * list,const unsigned char * addr,int addr_len,unsigned char addr_type,bool global,bool sync)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 
__hw_addr_del(struct netdev_hw_addr_list * list,const unsigned char * addr,int addr_len,unsigned char addr_type)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 
__hw_addr_sync_one(struct netdev_hw_addr_list * to_list,struct netdev_hw_addr * ha,int addr_len)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 
__hw_addr_unsync_one(struct netdev_hw_addr_list * to_list,struct netdev_hw_addr_list * from_list,struct netdev_hw_addr * ha,int addr_len)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 
__hw_addr_sync_multiple(struct netdev_hw_addr_list * to_list,struct netdev_hw_addr_list * from_list,int addr_len)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  */
__hw_addr_sync(struct netdev_hw_addr_list * to_list,struct netdev_hw_addr_list * from_list,int addr_len)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 
__hw_addr_unsync(struct netdev_hw_addr_list * to_list,struct netdev_hw_addr_list * from_list,int addr_len)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  **/
__hw_addr_sync_dev(struct netdev_hw_addr_list * list,struct net_device * dev,int (* sync)(struct net_device *,const unsigned char *),int (* unsync)(struct net_device *,const unsigned char *))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  **/
__hw_addr_ref_sync_dev(struct netdev_hw_addr_list * list,struct net_device * dev,int (* sync)(struct net_device *,const unsigned char *,int),int (* unsync)(struct net_device *,const unsigned char *,int))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  **/
__hw_addr_ref_unsync_dev(struct netdev_hw_addr_list * list,struct net_device * dev,int (* unsync)(struct net_device *,const unsigned char *,int))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  **/
__hw_addr_unsync_dev(struct netdev_hw_addr_list * list,struct net_device * dev,int (* unsync)(struct net_device *,const unsigned char *))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 
__hw_addr_flush(struct netdev_hw_addr_list * list)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 
__hw_addr_init(struct netdev_hw_addr_list * list)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  */
dev_addr_flush(struct net_device * dev)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  */
dev_addr_init(struct net_device * dev)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 /**
553  *	dev_addr_add - Add a device address
554  *	@dev: device
555  *	@addr: address to add
556  *	@addr_type: address type
557  *
558  *	Add a device address to the device or increase the reference count if
559  *	it already exists.
560  *
561  *	The caller must hold the rtnl_mutex.
562  */
dev_addr_add(struct net_device * dev,const unsigned char * addr,unsigned char addr_type)563 int dev_addr_add(struct net_device *dev, const unsigned char *addr,
564 		 unsigned char addr_type)
565 {
566 	int err;
567 
568 	ASSERT_RTNL();
569 
570 	err = dev_pre_changeaddr_notify(dev, addr, NULL);
571 	if (err)
572 		return err;
573 	err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type);
574 	if (!err)
575 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
576 	return err;
577 }
578 EXPORT_SYMBOL(dev_addr_add);
579 
580 /**
581  *	dev_addr_del - Release a device address.
582  *	@dev: device
583  *	@addr: address to delete
584  *	@addr_type: address type
585  *
586  *	Release reference to a device address and remove it from the device
587  *	if the reference count drops to zero.
588  *
589  *	The caller must hold the rtnl_mutex.
590  */
dev_addr_del(struct net_device * dev,const unsigned char * addr,unsigned char addr_type)591 int dev_addr_del(struct net_device *dev, const unsigned char *addr,
592 		 unsigned char addr_type)
593 {
594 	int err;
595 	struct netdev_hw_addr *ha;
596 
597 	ASSERT_RTNL();
598 
599 	/*
600 	 * We can not remove the first address from the list because
601 	 * dev->dev_addr points to that.
602 	 */
603 	ha = list_first_entry(&dev->dev_addrs.list,
604 			      struct netdev_hw_addr, list);
605 	if (!memcmp(ha->addr, addr, dev->addr_len) &&
606 	    ha->type == addr_type && ha->refcount == 1)
607 		return -ENOENT;
608 
609 	err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len,
610 			    addr_type);
611 	if (!err)
612 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
613 	return err;
614 }
615 EXPORT_SYMBOL(dev_addr_del);
616 
617 /*
618  * Unicast list handling functions
619  */
620 
621 /**
622  *	dev_uc_add_excl - Add a global secondary unicast address
623  *	@dev: device
624  *	@addr: address to add
625  */
dev_uc_add_excl(struct net_device * dev,const unsigned char * addr)626 int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr)
627 {
628 	int err;
629 
630 	netif_addr_lock_bh(dev);
631 	err = __hw_addr_add_ex(&dev->uc, addr, dev->addr_len,
632 			       NETDEV_HW_ADDR_T_UNICAST, true, false,
633 			       0, true);
634 	if (!err)
635 		__dev_set_rx_mode(dev);
636 	netif_addr_unlock_bh(dev);
637 	return err;
638 }
639 EXPORT_SYMBOL(dev_uc_add_excl);
640 
641 /**
642  *	dev_uc_add - Add a secondary unicast address
643  *	@dev: device
644  *	@addr: address to add
645  *
646  *	Add a secondary unicast address to the device or increase
647  *	the reference count if it already exists.
648  */
dev_uc_add(struct net_device * dev,const unsigned char * addr)649 int dev_uc_add(struct net_device *dev, const unsigned char *addr)
650 {
651 	int err;
652 
653 	netif_addr_lock_bh(dev);
654 	err = __hw_addr_add(&dev->uc, addr, dev->addr_len,
655 			    NETDEV_HW_ADDR_T_UNICAST);
656 	if (!err)
657 		__dev_set_rx_mode(dev);
658 	netif_addr_unlock_bh(dev);
659 	return err;
660 }
661 EXPORT_SYMBOL(dev_uc_add);
662 
663 /**
664  *	dev_uc_del - Release secondary unicast address.
665  *	@dev: device
666  *	@addr: address to delete
667  *
668  *	Release reference to a secondary unicast address and remove it
669  *	from the device if the reference count drops to zero.
670  */
dev_uc_del(struct net_device * dev,const unsigned char * addr)671 int dev_uc_del(struct net_device *dev, const unsigned char *addr)
672 {
673 	int err;
674 
675 	netif_addr_lock_bh(dev);
676 	err = __hw_addr_del(&dev->uc, addr, dev->addr_len,
677 			    NETDEV_HW_ADDR_T_UNICAST);
678 	if (!err)
679 		__dev_set_rx_mode(dev);
680 	netif_addr_unlock_bh(dev);
681 	return err;
682 }
683 EXPORT_SYMBOL(dev_uc_del);
684 
685 /**
686  *	dev_uc_sync - Synchronize device's unicast list to another device
687  *	@to: destination device
688  *	@from: source device
689  *
690  *	Add newly added addresses to the destination device and release
691  *	addresses that have no users left. The source device must be
692  *	locked by netif_addr_lock_bh.
693  *
694  *	This function is intended to be called from the dev->set_rx_mode
695  *	function of layered software devices.  This function assumes that
696  *	addresses will only ever be synced to the @to devices and no other.
697  */
dev_uc_sync(struct net_device * to,struct net_device * from)698 int dev_uc_sync(struct net_device *to, struct net_device *from)
699 {
700 	int err = 0;
701 
702 	if (to->addr_len != from->addr_len)
703 		return -EINVAL;
704 
705 	netif_addr_lock(to);
706 	err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
707 	if (!err)
708 		__dev_set_rx_mode(to);
709 	netif_addr_unlock(to);
710 	return err;
711 }
712 EXPORT_SYMBOL(dev_uc_sync);
713 
714 /**
715  *	dev_uc_sync_multiple - Synchronize device's unicast list to another
716  *	device, but allow for multiple calls to sync to multiple devices.
717  *	@to: destination device
718  *	@from: source device
719  *
720  *	Add newly added addresses to the destination device and release
721  *	addresses that have been deleted from the source. The source device
722  *	must be locked by netif_addr_lock_bh.
723  *
724  *	This function is intended to be called from the dev->set_rx_mode
725  *	function of layered software devices.  It allows for a single source
726  *	device to be synced to multiple destination devices.
727  */
dev_uc_sync_multiple(struct net_device * to,struct net_device * from)728 int dev_uc_sync_multiple(struct net_device *to, struct net_device *from)
729 {
730 	int err = 0;
731 
732 	if (to->addr_len != from->addr_len)
733 		return -EINVAL;
734 
735 	netif_addr_lock(to);
736 	err = __hw_addr_sync_multiple(&to->uc, &from->uc, to->addr_len);
737 	if (!err)
738 		__dev_set_rx_mode(to);
739 	netif_addr_unlock(to);
740 	return err;
741 }
742 EXPORT_SYMBOL(dev_uc_sync_multiple);
743 
744 /**
745  *	dev_uc_unsync - Remove synchronized addresses from the destination device
746  *	@to: destination device
747  *	@from: source device
748  *
749  *	Remove all addresses that were added to the destination device by
750  *	dev_uc_sync(). This function is intended to be called from the
751  *	dev->stop function of layered software devices.
752  */
dev_uc_unsync(struct net_device * to,struct net_device * from)753 void dev_uc_unsync(struct net_device *to, struct net_device *from)
754 {
755 	if (to->addr_len != from->addr_len)
756 		return;
757 
758 	/* netif_addr_lock_bh() uses lockdep subclass 0, this is okay for two
759 	 * reasons:
760 	 * 1) This is always called without any addr_list_lock, so as the
761 	 *    outermost one here, it must be 0.
762 	 * 2) This is called by some callers after unlinking the upper device,
763 	 *    so the dev->lower_level becomes 1 again.
764 	 * Therefore, the subclass for 'from' is 0, for 'to' is either 1 or
765 	 * larger.
766 	 */
767 	netif_addr_lock_bh(from);
768 	netif_addr_lock(to);
769 	__hw_addr_unsync(&to->uc, &from->uc, to->addr_len);
770 	__dev_set_rx_mode(to);
771 	netif_addr_unlock(to);
772 	netif_addr_unlock_bh(from);
773 }
774 EXPORT_SYMBOL(dev_uc_unsync);
775 
776 /**
777  *	dev_uc_flush - Flush unicast addresses
778  *	@dev: device
779  *
780  *	Flush unicast addresses.
781  */
dev_uc_flush(struct net_device * dev)782 void dev_uc_flush(struct net_device *dev)
783 {
784 	netif_addr_lock_bh(dev);
785 	__hw_addr_flush(&dev->uc);
786 	netif_addr_unlock_bh(dev);
787 }
788 EXPORT_SYMBOL(dev_uc_flush);
789 
790 /**
791  *	dev_uc_init - Init unicast address list
792  *	@dev: device
793  *
794  *	Init unicast address list.
795  */
dev_uc_init(struct net_device * dev)796 void dev_uc_init(struct net_device *dev)
797 {
798 	__hw_addr_init(&dev->uc);
799 }
800 EXPORT_SYMBOL(dev_uc_init);
801 
802 /*
803  * Multicast list handling functions
804  */
805 
806 /**
807  *	dev_mc_add_excl - Add a global secondary multicast address
808  *	@dev: device
809  *	@addr: address to add
810  */
dev_mc_add_excl(struct net_device * dev,const unsigned char * addr)811 int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr)
812 {
813 	int err;
814 
815 	netif_addr_lock_bh(dev);
816 	err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
817 			       NETDEV_HW_ADDR_T_MULTICAST, true, false,
818 			       0, true);
819 	if (!err)
820 		__dev_set_rx_mode(dev);
821 	netif_addr_unlock_bh(dev);
822 	return err;
823 }
824 EXPORT_SYMBOL(dev_mc_add_excl);
825 
__dev_mc_add(struct net_device * dev,const unsigned char * addr,bool global)826 static int __dev_mc_add(struct net_device *dev, const unsigned char *addr,
827 			bool global)
828 {
829 	int err;
830 
831 	netif_addr_lock_bh(dev);
832 	err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
833 			       NETDEV_HW_ADDR_T_MULTICAST, global, false,
834 			       0, false);
835 	if (!err)
836 		__dev_set_rx_mode(dev);
837 	netif_addr_unlock_bh(dev);
838 	return err;
839 }
840 /**
841  *	dev_mc_add - Add a multicast address
842  *	@dev: device
843  *	@addr: address to add
844  *
845  *	Add a multicast address to the device or increase
846  *	the reference count if it already exists.
847  */
dev_mc_add(struct net_device * dev,const unsigned char * addr)848 int dev_mc_add(struct net_device *dev, const unsigned char *addr)
849 {
850 	return __dev_mc_add(dev, addr, false);
851 }
852 EXPORT_SYMBOL(dev_mc_add);
853 
854 /**
855  *	dev_mc_add_global - Add a global multicast address
856  *	@dev: device
857  *	@addr: address to add
858  *
859  *	Add a global multicast address to the device.
860  */
dev_mc_add_global(struct net_device * dev,const unsigned char * addr)861 int dev_mc_add_global(struct net_device *dev, const unsigned char *addr)
862 {
863 	return __dev_mc_add(dev, addr, true);
864 }
865 EXPORT_SYMBOL(dev_mc_add_global);
866 
__dev_mc_del(struct net_device * dev,const unsigned char * addr,bool global)867 static int __dev_mc_del(struct net_device *dev, const unsigned char *addr,
868 			bool global)
869 {
870 	int err;
871 
872 	netif_addr_lock_bh(dev);
873 	err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
874 			       NETDEV_HW_ADDR_T_MULTICAST, global, false);
875 	if (!err)
876 		__dev_set_rx_mode(dev);
877 	netif_addr_unlock_bh(dev);
878 	return err;
879 }
880 
881 /**
882  *	dev_mc_del - Delete a multicast address.
883  *	@dev: device
884  *	@addr: address to delete
885  *
886  *	Release reference to a multicast address and remove it
887  *	from the device if the reference count drops to zero.
888  */
dev_mc_del(struct net_device * dev,const unsigned char * addr)889 int dev_mc_del(struct net_device *dev, const unsigned char *addr)
890 {
891 	return __dev_mc_del(dev, addr, false);
892 }
893 EXPORT_SYMBOL(dev_mc_del);
894 
895 /**
896  *	dev_mc_del_global - Delete a global multicast address.
897  *	@dev: device
898  *	@addr: address to delete
899  *
900  *	Release reference to a multicast address and remove it
901  *	from the device if the reference count drops to zero.
902  */
dev_mc_del_global(struct net_device * dev,const unsigned char * addr)903 int dev_mc_del_global(struct net_device *dev, const unsigned char *addr)
904 {
905 	return __dev_mc_del(dev, addr, true);
906 }
907 EXPORT_SYMBOL(dev_mc_del_global);
908 
909 /**
910  *	dev_mc_sync - Synchronize device's multicast list to another device
911  *	@to: destination device
912  *	@from: source device
913  *
914  *	Add newly added addresses to the destination device and release
915  *	addresses that have no users left. The source device must be
916  *	locked by netif_addr_lock_bh.
917  *
918  *	This function is intended to be called from the ndo_set_rx_mode
919  *	function of layered software devices.
920  */
dev_mc_sync(struct net_device * to,struct net_device * from)921 int dev_mc_sync(struct net_device *to, struct net_device *from)
922 {
923 	int err = 0;
924 
925 	if (to->addr_len != from->addr_len)
926 		return -EINVAL;
927 
928 	netif_addr_lock(to);
929 	err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
930 	if (!err)
931 		__dev_set_rx_mode(to);
932 	netif_addr_unlock(to);
933 	return err;
934 }
935 EXPORT_SYMBOL(dev_mc_sync);
936 
937 /**
938  *	dev_mc_sync_multiple - Synchronize device's multicast list to another
939  *	device, but allow for multiple calls to sync to multiple devices.
940  *	@to: destination device
941  *	@from: source device
942  *
943  *	Add newly added addresses to the destination device and release
944  *	addresses that have no users left. The source device must be
945  *	locked by netif_addr_lock_bh.
946  *
947  *	This function is intended to be called from the ndo_set_rx_mode
948  *	function of layered software devices.  It allows for a single
949  *	source device to be synced to multiple destination devices.
950  */
dev_mc_sync_multiple(struct net_device * to,struct net_device * from)951 int dev_mc_sync_multiple(struct net_device *to, struct net_device *from)
952 {
953 	int err = 0;
954 
955 	if (to->addr_len != from->addr_len)
956 		return -EINVAL;
957 
958 	netif_addr_lock(to);
959 	err = __hw_addr_sync_multiple(&to->mc, &from->mc, to->addr_len);
960 	if (!err)
961 		__dev_set_rx_mode(to);
962 	netif_addr_unlock(to);
963 	return err;
964 }
965 EXPORT_SYMBOL(dev_mc_sync_multiple);
966 
967 /**
968  *	dev_mc_unsync - Remove synchronized addresses from the destination device
969  *	@to: destination device
970  *	@from: source device
971  *
972  *	Remove all addresses that were added to the destination device by
973  *	dev_mc_sync(). This function is intended to be called from the
974  *	dev->stop function of layered software devices.
975  */
dev_mc_unsync(struct net_device * to,struct net_device * from)976 void dev_mc_unsync(struct net_device *to, struct net_device *from)
977 {
978 	if (to->addr_len != from->addr_len)
979 		return;
980 
981 	/* See the above comments inside dev_uc_unsync(). */
982 	netif_addr_lock_bh(from);
983 	netif_addr_lock(to);
984 	__hw_addr_unsync(&to->mc, &from->mc, to->addr_len);
985 	__dev_set_rx_mode(to);
986 	netif_addr_unlock(to);
987 	netif_addr_unlock_bh(from);
988 }
989 EXPORT_SYMBOL(dev_mc_unsync);
990 
991 /**
992  *	dev_mc_flush - Flush multicast addresses
993  *	@dev: device
994  *
995  *	Flush multicast addresses.
996  */
dev_mc_flush(struct net_device * dev)997 void dev_mc_flush(struct net_device *dev)
998 {
999 	netif_addr_lock_bh(dev);
1000 	__hw_addr_flush(&dev->mc);
1001 	netif_addr_unlock_bh(dev);
1002 }
1003 EXPORT_SYMBOL(dev_mc_flush);
1004 
1005 /**
1006  *	dev_mc_init - Init multicast address list
1007  *	@dev: device
1008  *
1009  *	Init multicast address list.
1010  */
dev_mc_init(struct net_device * dev)1011 void dev_mc_init(struct net_device *dev)
1012 {
1013 	__hw_addr_init(&dev->mc);
1014 }
1015 EXPORT_SYMBOL(dev_mc_init);
1016