staging:vt6655:srom: Whitespace cleanups
[linux-2.6/btrfs-unstable.git] / net / core / dev_addr_lists.c
blobbd2eb9d3e369c3ea0cdeac568600fd929c0b0d48
1 /*
2 * net/core/dev_addr_lists.c - Functions for handling net device lists
3 * Copyright (c) 2010 Jiri Pirko <jpirko@redhat.com>
5 * This file contains functions for working with unicast, multicast and device
6 * addresses lists.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/netdevice.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/export.h>
17 #include <linux/list.h>
20 * General list handling functions
23 static int __hw_addr_create_ex(struct netdev_hw_addr_list *list,
24 const unsigned char *addr, int addr_len,
25 unsigned char addr_type, bool global)
27 struct netdev_hw_addr *ha;
28 int alloc_size;
30 alloc_size = sizeof(*ha);
31 if (alloc_size < L1_CACHE_BYTES)
32 alloc_size = L1_CACHE_BYTES;
33 ha = kmalloc(alloc_size, GFP_ATOMIC);
34 if (!ha)
35 return -ENOMEM;
36 memcpy(ha->addr, addr, addr_len);
37 ha->type = addr_type;
38 ha->refcount = 1;
39 ha->global_use = global;
40 ha->synced = false;
41 list_add_tail_rcu(&ha->list, &list->list);
42 list->count++;
44 return 0;
47 static int __hw_addr_add_ex(struct netdev_hw_addr_list *list,
48 const unsigned char *addr, int addr_len,
49 unsigned char addr_type, bool global)
51 struct netdev_hw_addr *ha;
53 if (addr_len > MAX_ADDR_LEN)
54 return -EINVAL;
56 list_for_each_entry(ha, &list->list, list) {
57 if (!memcmp(ha->addr, addr, addr_len) &&
58 ha->type == addr_type) {
59 if (global) {
60 /* check if addr is already used as global */
61 if (ha->global_use)
62 return 0;
63 else
64 ha->global_use = true;
66 ha->refcount++;
67 return 0;
71 return __hw_addr_create_ex(list, addr, addr_len, addr_type, global);
74 static int __hw_addr_add(struct netdev_hw_addr_list *list,
75 const unsigned char *addr, int addr_len,
76 unsigned char addr_type)
78 return __hw_addr_add_ex(list, addr, addr_len, addr_type, false);
81 static int __hw_addr_del_ex(struct netdev_hw_addr_list *list,
82 const unsigned char *addr, int addr_len,
83 unsigned char addr_type, bool global)
85 struct netdev_hw_addr *ha;
87 list_for_each_entry(ha, &list->list, list) {
88 if (!memcmp(ha->addr, addr, addr_len) &&
89 (ha->type == addr_type || !addr_type)) {
90 if (global) {
91 if (!ha->global_use)
92 break;
93 else
94 ha->global_use = false;
96 if (--ha->refcount)
97 return 0;
98 list_del_rcu(&ha->list);
99 kfree_rcu(ha, rcu_head);
100 list->count--;
101 return 0;
104 return -ENOENT;
107 static int __hw_addr_del(struct netdev_hw_addr_list *list,
108 const unsigned char *addr, int addr_len,
109 unsigned char addr_type)
111 return __hw_addr_del_ex(list, addr, addr_len, addr_type, false);
114 int __hw_addr_add_multiple(struct netdev_hw_addr_list *to_list,
115 struct netdev_hw_addr_list *from_list,
116 int addr_len, unsigned char addr_type)
118 int err;
119 struct netdev_hw_addr *ha, *ha2;
120 unsigned char type;
122 list_for_each_entry(ha, &from_list->list, list) {
123 type = addr_type ? addr_type : ha->type;
124 err = __hw_addr_add(to_list, ha->addr, addr_len, type);
125 if (err)
126 goto unroll;
128 return 0;
130 unroll:
131 list_for_each_entry(ha2, &from_list->list, list) {
132 if (ha2 == ha)
133 break;
134 type = addr_type ? addr_type : ha2->type;
135 __hw_addr_del(to_list, ha2->addr, addr_len, type);
137 return err;
139 EXPORT_SYMBOL(__hw_addr_add_multiple);
141 void __hw_addr_del_multiple(struct netdev_hw_addr_list *to_list,
142 struct netdev_hw_addr_list *from_list,
143 int addr_len, unsigned char addr_type)
145 struct netdev_hw_addr *ha;
146 unsigned char type;
148 list_for_each_entry(ha, &from_list->list, list) {
149 type = addr_type ? addr_type : ha->type;
150 __hw_addr_del(to_list, ha->addr, addr_len, type);
153 EXPORT_SYMBOL(__hw_addr_del_multiple);
155 int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
156 struct netdev_hw_addr_list *from_list,
157 int addr_len)
159 int err = 0;
160 struct netdev_hw_addr *ha, *tmp;
162 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
163 if (!ha->synced) {
164 err = __hw_addr_add(to_list, ha->addr,
165 addr_len, ha->type);
166 if (err)
167 break;
168 ha->synced = true;
169 ha->refcount++;
170 } else if (ha->refcount == 1) {
171 __hw_addr_del(to_list, ha->addr, addr_len, ha->type);
172 __hw_addr_del(from_list, ha->addr, addr_len, ha->type);
175 return err;
177 EXPORT_SYMBOL(__hw_addr_sync);
179 void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
180 struct netdev_hw_addr_list *from_list,
181 int addr_len)
183 struct netdev_hw_addr *ha, *tmp;
185 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
186 if (ha->synced) {
187 __hw_addr_del(to_list, ha->addr,
188 addr_len, ha->type);
189 ha->synced = false;
190 __hw_addr_del(from_list, ha->addr,
191 addr_len, ha->type);
195 EXPORT_SYMBOL(__hw_addr_unsync);
197 void __hw_addr_flush(struct netdev_hw_addr_list *list)
199 struct netdev_hw_addr *ha, *tmp;
201 list_for_each_entry_safe(ha, tmp, &list->list, list) {
202 list_del_rcu(&ha->list);
203 kfree_rcu(ha, rcu_head);
205 list->count = 0;
207 EXPORT_SYMBOL(__hw_addr_flush);
209 void __hw_addr_init(struct netdev_hw_addr_list *list)
211 INIT_LIST_HEAD(&list->list);
212 list->count = 0;
214 EXPORT_SYMBOL(__hw_addr_init);
217 * Device addresses handling functions
221 * dev_addr_flush - Flush device address list
222 * @dev: device
224 * Flush device address list and reset ->dev_addr.
226 * The caller must hold the rtnl_mutex.
228 void dev_addr_flush(struct net_device *dev)
230 /* rtnl_mutex must be held here */
232 __hw_addr_flush(&dev->dev_addrs);
233 dev->dev_addr = NULL;
235 EXPORT_SYMBOL(dev_addr_flush);
238 * dev_addr_init - Init device address list
239 * @dev: device
241 * Init device address list and create the first element,
242 * used by ->dev_addr.
244 * The caller must hold the rtnl_mutex.
246 int dev_addr_init(struct net_device *dev)
248 unsigned char addr[MAX_ADDR_LEN];
249 struct netdev_hw_addr *ha;
250 int err;
252 /* rtnl_mutex must be held here */
254 __hw_addr_init(&dev->dev_addrs);
255 memset(addr, 0, sizeof(addr));
256 err = __hw_addr_add(&dev->dev_addrs, addr, sizeof(addr),
257 NETDEV_HW_ADDR_T_LAN);
258 if (!err) {
260 * Get the first (previously created) address from the list
261 * and set dev_addr pointer to this location.
263 ha = list_first_entry(&dev->dev_addrs.list,
264 struct netdev_hw_addr, list);
265 dev->dev_addr = ha->addr;
267 return err;
269 EXPORT_SYMBOL(dev_addr_init);
272 * dev_addr_add - Add a device address
273 * @dev: device
274 * @addr: address to add
275 * @addr_type: address type
277 * Add a device address to the device or increase the reference count if
278 * it already exists.
280 * The caller must hold the rtnl_mutex.
282 int dev_addr_add(struct net_device *dev, const unsigned char *addr,
283 unsigned char addr_type)
285 int err;
287 ASSERT_RTNL();
289 err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type);
290 if (!err)
291 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
292 return err;
294 EXPORT_SYMBOL(dev_addr_add);
297 * dev_addr_del - Release a device address.
298 * @dev: device
299 * @addr: address to delete
300 * @addr_type: address type
302 * Release reference to a device address and remove it from the device
303 * if the reference count drops to zero.
305 * The caller must hold the rtnl_mutex.
307 int dev_addr_del(struct net_device *dev, const unsigned char *addr,
308 unsigned char addr_type)
310 int err;
311 struct netdev_hw_addr *ha;
313 ASSERT_RTNL();
316 * We can not remove the first address from the list because
317 * dev->dev_addr points to that.
319 ha = list_first_entry(&dev->dev_addrs.list,
320 struct netdev_hw_addr, list);
321 if (!memcmp(ha->addr, addr, dev->addr_len) &&
322 ha->type == addr_type && ha->refcount == 1)
323 return -ENOENT;
325 err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len,
326 addr_type);
327 if (!err)
328 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
329 return err;
331 EXPORT_SYMBOL(dev_addr_del);
334 * dev_addr_add_multiple - Add device addresses from another device
335 * @to_dev: device to which addresses will be added
336 * @from_dev: device from which addresses will be added
337 * @addr_type: address type - 0 means type will be used from from_dev
339 * Add device addresses of the one device to another.
341 * The caller must hold the rtnl_mutex.
343 int dev_addr_add_multiple(struct net_device *to_dev,
344 struct net_device *from_dev,
345 unsigned char addr_type)
347 int err;
349 ASSERT_RTNL();
351 if (from_dev->addr_len != to_dev->addr_len)
352 return -EINVAL;
353 err = __hw_addr_add_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
354 to_dev->addr_len, addr_type);
355 if (!err)
356 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
357 return err;
359 EXPORT_SYMBOL(dev_addr_add_multiple);
362 * dev_addr_del_multiple - Delete device addresses by another device
363 * @to_dev: device where the addresses will be deleted
364 * @from_dev: device supplying the addresses to be deleted
365 * @addr_type: address type - 0 means type will be used from from_dev
367 * Deletes addresses in to device by the list of addresses in from device.
369 * The caller must hold the rtnl_mutex.
371 int dev_addr_del_multiple(struct net_device *to_dev,
372 struct net_device *from_dev,
373 unsigned char addr_type)
375 ASSERT_RTNL();
377 if (from_dev->addr_len != to_dev->addr_len)
378 return -EINVAL;
379 __hw_addr_del_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
380 to_dev->addr_len, addr_type);
381 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
382 return 0;
384 EXPORT_SYMBOL(dev_addr_del_multiple);
387 * Unicast list handling functions
391 * dev_uc_add_excl - Add a global secondary unicast address
392 * @dev: device
393 * @addr: address to add
395 int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr)
397 struct netdev_hw_addr *ha;
398 int err;
400 netif_addr_lock_bh(dev);
401 list_for_each_entry(ha, &dev->uc.list, list) {
402 if (!memcmp(ha->addr, addr, dev->addr_len) &&
403 ha->type == NETDEV_HW_ADDR_T_UNICAST) {
404 err = -EEXIST;
405 goto out;
408 err = __hw_addr_create_ex(&dev->uc, addr, dev->addr_len,
409 NETDEV_HW_ADDR_T_UNICAST, true);
410 if (!err)
411 __dev_set_rx_mode(dev);
412 out:
413 netif_addr_unlock_bh(dev);
414 return err;
416 EXPORT_SYMBOL(dev_uc_add_excl);
419 * dev_uc_add - Add a secondary unicast address
420 * @dev: device
421 * @addr: address to add
423 * Add a secondary unicast address to the device or increase
424 * the reference count if it already exists.
426 int dev_uc_add(struct net_device *dev, const unsigned char *addr)
428 int err;
430 netif_addr_lock_bh(dev);
431 err = __hw_addr_add(&dev->uc, addr, dev->addr_len,
432 NETDEV_HW_ADDR_T_UNICAST);
433 if (!err)
434 __dev_set_rx_mode(dev);
435 netif_addr_unlock_bh(dev);
436 return err;
438 EXPORT_SYMBOL(dev_uc_add);
441 * dev_uc_del - Release secondary unicast address.
442 * @dev: device
443 * @addr: address to delete
445 * Release reference to a secondary unicast address and remove it
446 * from the device if the reference count drops to zero.
448 int dev_uc_del(struct net_device *dev, const unsigned char *addr)
450 int err;
452 netif_addr_lock_bh(dev);
453 err = __hw_addr_del(&dev->uc, addr, dev->addr_len,
454 NETDEV_HW_ADDR_T_UNICAST);
455 if (!err)
456 __dev_set_rx_mode(dev);
457 netif_addr_unlock_bh(dev);
458 return err;
460 EXPORT_SYMBOL(dev_uc_del);
463 * dev_uc_sync - Synchronize device's unicast list to another device
464 * @to: destination device
465 * @from: source device
467 * Add newly added addresses to the destination device and release
468 * addresses that have no users left. The source device must be
469 * locked by netif_addr_lock_bh.
471 * This function is intended to be called from the dev->set_rx_mode
472 * function of layered software devices.
474 int dev_uc_sync(struct net_device *to, struct net_device *from)
476 int err = 0;
478 if (to->addr_len != from->addr_len)
479 return -EINVAL;
481 netif_addr_lock_nested(to);
482 err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
483 if (!err)
484 __dev_set_rx_mode(to);
485 netif_addr_unlock(to);
486 return err;
488 EXPORT_SYMBOL(dev_uc_sync);
491 * dev_uc_unsync - Remove synchronized addresses from the destination device
492 * @to: destination device
493 * @from: source device
495 * Remove all addresses that were added to the destination device by
496 * dev_uc_sync(). This function is intended to be called from the
497 * dev->stop function of layered software devices.
499 void dev_uc_unsync(struct net_device *to, struct net_device *from)
501 if (to->addr_len != from->addr_len)
502 return;
504 netif_addr_lock_bh(from);
505 netif_addr_lock_nested(to);
506 __hw_addr_unsync(&to->uc, &from->uc, to->addr_len);
507 __dev_set_rx_mode(to);
508 netif_addr_unlock(to);
509 netif_addr_unlock_bh(from);
511 EXPORT_SYMBOL(dev_uc_unsync);
514 * dev_uc_flush - Flush unicast addresses
515 * @dev: device
517 * Flush unicast addresses.
519 void dev_uc_flush(struct net_device *dev)
521 netif_addr_lock_bh(dev);
522 __hw_addr_flush(&dev->uc);
523 netif_addr_unlock_bh(dev);
525 EXPORT_SYMBOL(dev_uc_flush);
528 * dev_uc_flush - Init unicast address list
529 * @dev: device
531 * Init unicast address list.
533 void dev_uc_init(struct net_device *dev)
535 __hw_addr_init(&dev->uc);
537 EXPORT_SYMBOL(dev_uc_init);
540 * Multicast list handling functions
544 * dev_mc_add_excl - Add a global secondary multicast address
545 * @dev: device
546 * @addr: address to add
548 int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr)
550 struct netdev_hw_addr *ha;
551 int err;
553 netif_addr_lock_bh(dev);
554 list_for_each_entry(ha, &dev->mc.list, list) {
555 if (!memcmp(ha->addr, addr, dev->addr_len) &&
556 ha->type == NETDEV_HW_ADDR_T_MULTICAST) {
557 err = -EEXIST;
558 goto out;
561 err = __hw_addr_create_ex(&dev->mc, addr, dev->addr_len,
562 NETDEV_HW_ADDR_T_MULTICAST, true);
563 if (!err)
564 __dev_set_rx_mode(dev);
565 out:
566 netif_addr_unlock_bh(dev);
567 return err;
569 EXPORT_SYMBOL(dev_mc_add_excl);
571 static int __dev_mc_add(struct net_device *dev, const unsigned char *addr,
572 bool global)
574 int err;
576 netif_addr_lock_bh(dev);
577 err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
578 NETDEV_HW_ADDR_T_MULTICAST, global);
579 if (!err)
580 __dev_set_rx_mode(dev);
581 netif_addr_unlock_bh(dev);
582 return err;
585 * dev_mc_add - Add a multicast address
586 * @dev: device
587 * @addr: address to add
589 * Add a multicast address to the device or increase
590 * the reference count if it already exists.
592 int dev_mc_add(struct net_device *dev, const unsigned char *addr)
594 return __dev_mc_add(dev, addr, false);
596 EXPORT_SYMBOL(dev_mc_add);
599 * dev_mc_add_global - Add a global multicast address
600 * @dev: device
601 * @addr: address to add
603 * Add a global multicast address to the device.
605 int dev_mc_add_global(struct net_device *dev, const unsigned char *addr)
607 return __dev_mc_add(dev, addr, true);
609 EXPORT_SYMBOL(dev_mc_add_global);
611 static int __dev_mc_del(struct net_device *dev, const unsigned char *addr,
612 bool global)
614 int err;
616 netif_addr_lock_bh(dev);
617 err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
618 NETDEV_HW_ADDR_T_MULTICAST, global);
619 if (!err)
620 __dev_set_rx_mode(dev);
621 netif_addr_unlock_bh(dev);
622 return err;
626 * dev_mc_del - Delete a multicast address.
627 * @dev: device
628 * @addr: address to delete
630 * Release reference to a multicast address and remove it
631 * from the device if the reference count drops to zero.
633 int dev_mc_del(struct net_device *dev, const unsigned char *addr)
635 return __dev_mc_del(dev, addr, false);
637 EXPORT_SYMBOL(dev_mc_del);
640 * dev_mc_del_global - Delete a global multicast address.
641 * @dev: device
642 * @addr: address to delete
644 * Release reference to a multicast address and remove it
645 * from the device if the reference count drops to zero.
647 int dev_mc_del_global(struct net_device *dev, const unsigned char *addr)
649 return __dev_mc_del(dev, addr, true);
651 EXPORT_SYMBOL(dev_mc_del_global);
654 * dev_mc_sync - Synchronize device's unicast list to another device
655 * @to: destination device
656 * @from: source device
658 * Add newly added addresses to the destination device and release
659 * addresses that have no users left. The source device must be
660 * locked by netif_addr_lock_bh.
662 * This function is intended to be called from the ndo_set_rx_mode
663 * function of layered software devices.
665 int dev_mc_sync(struct net_device *to, struct net_device *from)
667 int err = 0;
669 if (to->addr_len != from->addr_len)
670 return -EINVAL;
672 netif_addr_lock_nested(to);
673 err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
674 if (!err)
675 __dev_set_rx_mode(to);
676 netif_addr_unlock(to);
677 return err;
679 EXPORT_SYMBOL(dev_mc_sync);
682 * dev_mc_unsync - Remove synchronized addresses from the destination device
683 * @to: destination device
684 * @from: source device
686 * Remove all addresses that were added to the destination device by
687 * dev_mc_sync(). This function is intended to be called from the
688 * dev->stop function of layered software devices.
690 void dev_mc_unsync(struct net_device *to, struct net_device *from)
692 if (to->addr_len != from->addr_len)
693 return;
695 netif_addr_lock_bh(from);
696 netif_addr_lock_nested(to);
697 __hw_addr_unsync(&to->mc, &from->mc, to->addr_len);
698 __dev_set_rx_mode(to);
699 netif_addr_unlock(to);
700 netif_addr_unlock_bh(from);
702 EXPORT_SYMBOL(dev_mc_unsync);
705 * dev_mc_flush - Flush multicast addresses
706 * @dev: device
708 * Flush multicast addresses.
710 void dev_mc_flush(struct net_device *dev)
712 netif_addr_lock_bh(dev);
713 __hw_addr_flush(&dev->mc);
714 netif_addr_unlock_bh(dev);
716 EXPORT_SYMBOL(dev_mc_flush);
719 * dev_mc_flush - Init multicast address list
720 * @dev: device
722 * Init multicast address list.
724 void dev_mc_init(struct net_device *dev)
726 __hw_addr_init(&dev->mc);
728 EXPORT_SYMBOL(dev_mc_init);