qlcnic: helper routine to handle async events
[linux-2.6/cjktty.git] / net / core / dev_addr_lists.c
blobb079c7bbc157a3c047db12e7a39595da380071e8
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>
18 #include <linux/proc_fs.h>
21 * General list handling functions
24 static int __hw_addr_create_ex(struct netdev_hw_addr_list *list,
25 const unsigned char *addr, int addr_len,
26 unsigned char addr_type, bool global)
28 struct netdev_hw_addr *ha;
29 int alloc_size;
31 alloc_size = sizeof(*ha);
32 if (alloc_size < L1_CACHE_BYTES)
33 alloc_size = L1_CACHE_BYTES;
34 ha = kmalloc(alloc_size, GFP_ATOMIC);
35 if (!ha)
36 return -ENOMEM;
37 memcpy(ha->addr, addr, addr_len);
38 ha->type = addr_type;
39 ha->refcount = 1;
40 ha->global_use = global;
41 ha->synced = false;
42 list_add_tail_rcu(&ha->list, &list->list);
43 list->count++;
45 return 0;
48 static int __hw_addr_add_ex(struct netdev_hw_addr_list *list,
49 const unsigned char *addr, int addr_len,
50 unsigned char addr_type, bool global)
52 struct netdev_hw_addr *ha;
54 if (addr_len > MAX_ADDR_LEN)
55 return -EINVAL;
57 list_for_each_entry(ha, &list->list, list) {
58 if (!memcmp(ha->addr, addr, addr_len) &&
59 ha->type == addr_type) {
60 if (global) {
61 /* check if addr is already used as global */
62 if (ha->global_use)
63 return 0;
64 else
65 ha->global_use = true;
67 ha->refcount++;
68 return 0;
72 return __hw_addr_create_ex(list, addr, addr_len, addr_type, global);
75 static int __hw_addr_add(struct netdev_hw_addr_list *list,
76 const unsigned char *addr, int addr_len,
77 unsigned char addr_type)
79 return __hw_addr_add_ex(list, addr, addr_len, addr_type, false);
82 static int __hw_addr_del_ex(struct netdev_hw_addr_list *list,
83 const unsigned char *addr, int addr_len,
84 unsigned char addr_type, bool global)
86 struct netdev_hw_addr *ha;
88 list_for_each_entry(ha, &list->list, list) {
89 if (!memcmp(ha->addr, addr, addr_len) &&
90 (ha->type == addr_type || !addr_type)) {
91 if (global) {
92 if (!ha->global_use)
93 break;
94 else
95 ha->global_use = false;
97 if (--ha->refcount)
98 return 0;
99 list_del_rcu(&ha->list);
100 kfree_rcu(ha, rcu_head);
101 list->count--;
102 return 0;
105 return -ENOENT;
108 static int __hw_addr_del(struct netdev_hw_addr_list *list,
109 const unsigned char *addr, int addr_len,
110 unsigned char addr_type)
112 return __hw_addr_del_ex(list, addr, addr_len, addr_type, false);
115 int __hw_addr_add_multiple(struct netdev_hw_addr_list *to_list,
116 struct netdev_hw_addr_list *from_list,
117 int addr_len, unsigned char addr_type)
119 int err;
120 struct netdev_hw_addr *ha, *ha2;
121 unsigned char type;
123 list_for_each_entry(ha, &from_list->list, list) {
124 type = addr_type ? addr_type : ha->type;
125 err = __hw_addr_add(to_list, ha->addr, addr_len, type);
126 if (err)
127 goto unroll;
129 return 0;
131 unroll:
132 list_for_each_entry(ha2, &from_list->list, list) {
133 if (ha2 == ha)
134 break;
135 type = addr_type ? addr_type : ha2->type;
136 __hw_addr_del(to_list, ha2->addr, addr_len, type);
138 return err;
140 EXPORT_SYMBOL(__hw_addr_add_multiple);
142 void __hw_addr_del_multiple(struct netdev_hw_addr_list *to_list,
143 struct netdev_hw_addr_list *from_list,
144 int addr_len, unsigned char addr_type)
146 struct netdev_hw_addr *ha;
147 unsigned char type;
149 list_for_each_entry(ha, &from_list->list, list) {
150 type = addr_type ? addr_type : ha->type;
151 __hw_addr_del(to_list, ha->addr, addr_len, type);
154 EXPORT_SYMBOL(__hw_addr_del_multiple);
156 int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
157 struct netdev_hw_addr_list *from_list,
158 int addr_len)
160 int err = 0;
161 struct netdev_hw_addr *ha, *tmp;
163 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
164 if (!ha->synced) {
165 err = __hw_addr_add(to_list, ha->addr,
166 addr_len, ha->type);
167 if (err)
168 break;
169 ha->synced = true;
170 ha->refcount++;
171 } else if (ha->refcount == 1) {
172 __hw_addr_del(to_list, ha->addr, addr_len, ha->type);
173 __hw_addr_del(from_list, ha->addr, addr_len, ha->type);
176 return err;
178 EXPORT_SYMBOL(__hw_addr_sync);
180 void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
181 struct netdev_hw_addr_list *from_list,
182 int addr_len)
184 struct netdev_hw_addr *ha, *tmp;
186 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
187 if (ha->synced) {
188 __hw_addr_del(to_list, ha->addr,
189 addr_len, ha->type);
190 ha->synced = false;
191 __hw_addr_del(from_list, ha->addr,
192 addr_len, ha->type);
196 EXPORT_SYMBOL(__hw_addr_unsync);
198 void __hw_addr_flush(struct netdev_hw_addr_list *list)
200 struct netdev_hw_addr *ha, *tmp;
202 list_for_each_entry_safe(ha, tmp, &list->list, list) {
203 list_del_rcu(&ha->list);
204 kfree_rcu(ha, rcu_head);
206 list->count = 0;
208 EXPORT_SYMBOL(__hw_addr_flush);
210 void __hw_addr_init(struct netdev_hw_addr_list *list)
212 INIT_LIST_HEAD(&list->list);
213 list->count = 0;
215 EXPORT_SYMBOL(__hw_addr_init);
218 * Device addresses handling functions
222 * dev_addr_flush - Flush device address list
223 * @dev: device
225 * Flush device address list and reset ->dev_addr.
227 * The caller must hold the rtnl_mutex.
229 void dev_addr_flush(struct net_device *dev)
231 /* rtnl_mutex must be held here */
233 __hw_addr_flush(&dev->dev_addrs);
234 dev->dev_addr = NULL;
236 EXPORT_SYMBOL(dev_addr_flush);
239 * dev_addr_init - Init device address list
240 * @dev: device
242 * Init device address list and create the first element,
243 * used by ->dev_addr.
245 * The caller must hold the rtnl_mutex.
247 int dev_addr_init(struct net_device *dev)
249 unsigned char addr[MAX_ADDR_LEN];
250 struct netdev_hw_addr *ha;
251 int err;
253 /* rtnl_mutex must be held here */
255 __hw_addr_init(&dev->dev_addrs);
256 memset(addr, 0, sizeof(addr));
257 err = __hw_addr_add(&dev->dev_addrs, addr, sizeof(addr),
258 NETDEV_HW_ADDR_T_LAN);
259 if (!err) {
261 * Get the first (previously created) address from the list
262 * and set dev_addr pointer to this location.
264 ha = list_first_entry(&dev->dev_addrs.list,
265 struct netdev_hw_addr, list);
266 dev->dev_addr = ha->addr;
268 return err;
270 EXPORT_SYMBOL(dev_addr_init);
273 * dev_addr_add - Add a device address
274 * @dev: device
275 * @addr: address to add
276 * @addr_type: address type
278 * Add a device address to the device or increase the reference count if
279 * it already exists.
281 * The caller must hold the rtnl_mutex.
283 int dev_addr_add(struct net_device *dev, const unsigned char *addr,
284 unsigned char addr_type)
286 int err;
288 ASSERT_RTNL();
290 err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type);
291 if (!err)
292 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
293 return err;
295 EXPORT_SYMBOL(dev_addr_add);
298 * dev_addr_del - Release a device address.
299 * @dev: device
300 * @addr: address to delete
301 * @addr_type: address type
303 * Release reference to a device address and remove it from the device
304 * if the reference count drops to zero.
306 * The caller must hold the rtnl_mutex.
308 int dev_addr_del(struct net_device *dev, const unsigned char *addr,
309 unsigned char addr_type)
311 int err;
312 struct netdev_hw_addr *ha;
314 ASSERT_RTNL();
317 * We can not remove the first address from the list because
318 * dev->dev_addr points to that.
320 ha = list_first_entry(&dev->dev_addrs.list,
321 struct netdev_hw_addr, list);
322 if (!memcmp(ha->addr, addr, dev->addr_len) &&
323 ha->type == addr_type && ha->refcount == 1)
324 return -ENOENT;
326 err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len,
327 addr_type);
328 if (!err)
329 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
330 return err;
332 EXPORT_SYMBOL(dev_addr_del);
335 * dev_addr_add_multiple - Add device addresses from another device
336 * @to_dev: device to which addresses will be added
337 * @from_dev: device from which addresses will be added
338 * @addr_type: address type - 0 means type will be used from from_dev
340 * Add device addresses of the one device to another.
342 * The caller must hold the rtnl_mutex.
344 int dev_addr_add_multiple(struct net_device *to_dev,
345 struct net_device *from_dev,
346 unsigned char addr_type)
348 int err;
350 ASSERT_RTNL();
352 if (from_dev->addr_len != to_dev->addr_len)
353 return -EINVAL;
354 err = __hw_addr_add_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
355 to_dev->addr_len, addr_type);
356 if (!err)
357 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
358 return err;
360 EXPORT_SYMBOL(dev_addr_add_multiple);
363 * dev_addr_del_multiple - Delete device addresses by another device
364 * @to_dev: device where the addresses will be deleted
365 * @from_dev: device supplying the addresses to be deleted
366 * @addr_type: address type - 0 means type will be used from from_dev
368 * Deletes addresses in to device by the list of addresses in from device.
370 * The caller must hold the rtnl_mutex.
372 int dev_addr_del_multiple(struct net_device *to_dev,
373 struct net_device *from_dev,
374 unsigned char addr_type)
376 ASSERT_RTNL();
378 if (from_dev->addr_len != to_dev->addr_len)
379 return -EINVAL;
380 __hw_addr_del_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
381 to_dev->addr_len, addr_type);
382 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
383 return 0;
385 EXPORT_SYMBOL(dev_addr_del_multiple);
388 * Unicast list handling functions
392 * dev_uc_add_excl - Add a global secondary unicast address
393 * @dev: device
394 * @addr: address to add
396 int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr)
398 struct netdev_hw_addr *ha;
399 int err;
401 netif_addr_lock_bh(dev);
402 list_for_each_entry(ha, &dev->uc.list, list) {
403 if (!memcmp(ha->addr, addr, dev->addr_len) &&
404 ha->type == NETDEV_HW_ADDR_T_UNICAST) {
405 err = -EEXIST;
406 goto out;
409 err = __hw_addr_create_ex(&dev->uc, addr, dev->addr_len,
410 NETDEV_HW_ADDR_T_UNICAST, true);
411 if (!err)
412 __dev_set_rx_mode(dev);
413 out:
414 netif_addr_unlock_bh(dev);
415 return err;
417 EXPORT_SYMBOL(dev_uc_add_excl);
420 * dev_uc_add - Add a secondary unicast address
421 * @dev: device
422 * @addr: address to add
424 * Add a secondary unicast address to the device or increase
425 * the reference count if it already exists.
427 int dev_uc_add(struct net_device *dev, const unsigned char *addr)
429 int err;
431 netif_addr_lock_bh(dev);
432 err = __hw_addr_add(&dev->uc, addr, dev->addr_len,
433 NETDEV_HW_ADDR_T_UNICAST);
434 if (!err)
435 __dev_set_rx_mode(dev);
436 netif_addr_unlock_bh(dev);
437 return err;
439 EXPORT_SYMBOL(dev_uc_add);
442 * dev_uc_del - Release secondary unicast address.
443 * @dev: device
444 * @addr: address to delete
446 * Release reference to a secondary unicast address and remove it
447 * from the device if the reference count drops to zero.
449 int dev_uc_del(struct net_device *dev, const unsigned char *addr)
451 int err;
453 netif_addr_lock_bh(dev);
454 err = __hw_addr_del(&dev->uc, addr, dev->addr_len,
455 NETDEV_HW_ADDR_T_UNICAST);
456 if (!err)
457 __dev_set_rx_mode(dev);
458 netif_addr_unlock_bh(dev);
459 return err;
461 EXPORT_SYMBOL(dev_uc_del);
464 * dev_uc_sync - Synchronize device's unicast list to another device
465 * @to: destination device
466 * @from: source device
468 * Add newly added addresses to the destination device and release
469 * addresses that have no users left. The source device must be
470 * locked by netif_addr_lock_bh.
472 * This function is intended to be called from the dev->set_rx_mode
473 * function of layered software devices.
475 int dev_uc_sync(struct net_device *to, struct net_device *from)
477 int err = 0;
479 if (to->addr_len != from->addr_len)
480 return -EINVAL;
482 netif_addr_lock_nested(to);
483 err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
484 if (!err)
485 __dev_set_rx_mode(to);
486 netif_addr_unlock(to);
487 return err;
489 EXPORT_SYMBOL(dev_uc_sync);
492 * dev_uc_unsync - Remove synchronized addresses from the destination device
493 * @to: destination device
494 * @from: source device
496 * Remove all addresses that were added to the destination device by
497 * dev_uc_sync(). This function is intended to be called from the
498 * dev->stop function of layered software devices.
500 void dev_uc_unsync(struct net_device *to, struct net_device *from)
502 if (to->addr_len != from->addr_len)
503 return;
505 netif_addr_lock_bh(from);
506 netif_addr_lock_nested(to);
507 __hw_addr_unsync(&to->uc, &from->uc, to->addr_len);
508 __dev_set_rx_mode(to);
509 netif_addr_unlock(to);
510 netif_addr_unlock_bh(from);
512 EXPORT_SYMBOL(dev_uc_unsync);
515 * dev_uc_flush - Flush unicast addresses
516 * @dev: device
518 * Flush unicast addresses.
520 void dev_uc_flush(struct net_device *dev)
522 netif_addr_lock_bh(dev);
523 __hw_addr_flush(&dev->uc);
524 netif_addr_unlock_bh(dev);
526 EXPORT_SYMBOL(dev_uc_flush);
529 * dev_uc_flush - Init unicast address list
530 * @dev: device
532 * Init unicast address list.
534 void dev_uc_init(struct net_device *dev)
536 __hw_addr_init(&dev->uc);
538 EXPORT_SYMBOL(dev_uc_init);
541 * Multicast list handling functions
545 * dev_mc_add_excl - Add a global secondary multicast address
546 * @dev: device
547 * @addr: address to add
549 int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr)
551 struct netdev_hw_addr *ha;
552 int err;
554 netif_addr_lock_bh(dev);
555 list_for_each_entry(ha, &dev->mc.list, list) {
556 if (!memcmp(ha->addr, addr, dev->addr_len) &&
557 ha->type == NETDEV_HW_ADDR_T_MULTICAST) {
558 err = -EEXIST;
559 goto out;
562 err = __hw_addr_create_ex(&dev->mc, addr, dev->addr_len,
563 NETDEV_HW_ADDR_T_MULTICAST, true);
564 if (!err)
565 __dev_set_rx_mode(dev);
566 out:
567 netif_addr_unlock_bh(dev);
568 return err;
570 EXPORT_SYMBOL(dev_mc_add_excl);
572 static int __dev_mc_add(struct net_device *dev, const unsigned char *addr,
573 bool global)
575 int err;
577 netif_addr_lock_bh(dev);
578 err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
579 NETDEV_HW_ADDR_T_MULTICAST, global);
580 if (!err)
581 __dev_set_rx_mode(dev);
582 netif_addr_unlock_bh(dev);
583 return err;
586 * dev_mc_add - Add a multicast address
587 * @dev: device
588 * @addr: address to add
590 * Add a multicast address to the device or increase
591 * the reference count if it already exists.
593 int dev_mc_add(struct net_device *dev, const unsigned char *addr)
595 return __dev_mc_add(dev, addr, false);
597 EXPORT_SYMBOL(dev_mc_add);
600 * dev_mc_add_global - Add a global multicast address
601 * @dev: device
602 * @addr: address to add
604 * Add a global multicast address to the device.
606 int dev_mc_add_global(struct net_device *dev, const unsigned char *addr)
608 return __dev_mc_add(dev, addr, true);
610 EXPORT_SYMBOL(dev_mc_add_global);
612 static int __dev_mc_del(struct net_device *dev, const unsigned char *addr,
613 bool global)
615 int err;
617 netif_addr_lock_bh(dev);
618 err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
619 NETDEV_HW_ADDR_T_MULTICAST, global);
620 if (!err)
621 __dev_set_rx_mode(dev);
622 netif_addr_unlock_bh(dev);
623 return err;
627 * dev_mc_del - Delete a multicast address.
628 * @dev: device
629 * @addr: address to delete
631 * Release reference to a multicast address and remove it
632 * from the device if the reference count drops to zero.
634 int dev_mc_del(struct net_device *dev, const unsigned char *addr)
636 return __dev_mc_del(dev, addr, false);
638 EXPORT_SYMBOL(dev_mc_del);
641 * dev_mc_del_global - Delete a global multicast address.
642 * @dev: device
643 * @addr: address to delete
645 * Release reference to a multicast address and remove it
646 * from the device if the reference count drops to zero.
648 int dev_mc_del_global(struct net_device *dev, const unsigned char *addr)
650 return __dev_mc_del(dev, addr, true);
652 EXPORT_SYMBOL(dev_mc_del_global);
655 * dev_mc_sync - Synchronize device's unicast list to another device
656 * @to: destination device
657 * @from: source device
659 * Add newly added addresses to the destination device and release
660 * addresses that have no users left. The source device must be
661 * locked by netif_addr_lock_bh.
663 * This function is intended to be called from the ndo_set_rx_mode
664 * function of layered software devices.
666 int dev_mc_sync(struct net_device *to, struct net_device *from)
668 int err = 0;
670 if (to->addr_len != from->addr_len)
671 return -EINVAL;
673 netif_addr_lock_nested(to);
674 err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
675 if (!err)
676 __dev_set_rx_mode(to);
677 netif_addr_unlock(to);
678 return err;
680 EXPORT_SYMBOL(dev_mc_sync);
683 * dev_mc_unsync - Remove synchronized addresses from the destination device
684 * @to: destination device
685 * @from: source device
687 * Remove all addresses that were added to the destination device by
688 * dev_mc_sync(). This function is intended to be called from the
689 * dev->stop function of layered software devices.
691 void dev_mc_unsync(struct net_device *to, struct net_device *from)
693 if (to->addr_len != from->addr_len)
694 return;
696 netif_addr_lock_bh(from);
697 netif_addr_lock_nested(to);
698 __hw_addr_unsync(&to->mc, &from->mc, to->addr_len);
699 __dev_set_rx_mode(to);
700 netif_addr_unlock(to);
701 netif_addr_unlock_bh(from);
703 EXPORT_SYMBOL(dev_mc_unsync);
706 * dev_mc_flush - Flush multicast addresses
707 * @dev: device
709 * Flush multicast addresses.
711 void dev_mc_flush(struct net_device *dev)
713 netif_addr_lock_bh(dev);
714 __hw_addr_flush(&dev->mc);
715 netif_addr_unlock_bh(dev);
717 EXPORT_SYMBOL(dev_mc_flush);
720 * dev_mc_flush - Init multicast address list
721 * @dev: device
723 * Init multicast address list.
725 void dev_mc_init(struct net_device *dev)
727 __hw_addr_init(&dev->mc);
729 EXPORT_SYMBOL(dev_mc_init);
731 #ifdef CONFIG_PROC_FS
732 #include <linux/seq_file.h>
734 static int dev_mc_seq_show(struct seq_file *seq, void *v)
736 struct netdev_hw_addr *ha;
737 struct net_device *dev = v;
739 if (v == SEQ_START_TOKEN)
740 return 0;
742 netif_addr_lock_bh(dev);
743 netdev_for_each_mc_addr(ha, dev) {
744 int i;
746 seq_printf(seq, "%-4d %-15s %-5d %-5d ", dev->ifindex,
747 dev->name, ha->refcount, ha->global_use);
749 for (i = 0; i < dev->addr_len; i++)
750 seq_printf(seq, "%02x", ha->addr[i]);
752 seq_putc(seq, '\n');
754 netif_addr_unlock_bh(dev);
755 return 0;
758 static const struct seq_operations dev_mc_seq_ops = {
759 .start = dev_seq_start,
760 .next = dev_seq_next,
761 .stop = dev_seq_stop,
762 .show = dev_mc_seq_show,
765 static int dev_mc_seq_open(struct inode *inode, struct file *file)
767 return seq_open_net(inode, file, &dev_mc_seq_ops,
768 sizeof(struct seq_net_private));
771 static const struct file_operations dev_mc_seq_fops = {
772 .owner = THIS_MODULE,
773 .open = dev_mc_seq_open,
774 .read = seq_read,
775 .llseek = seq_lseek,
776 .release = seq_release_net,
779 #endif
781 static int __net_init dev_mc_net_init(struct net *net)
783 if (!proc_net_fops_create(net, "dev_mcast", 0, &dev_mc_seq_fops))
784 return -ENOMEM;
785 return 0;
788 static void __net_exit dev_mc_net_exit(struct net *net)
790 proc_net_remove(net, "dev_mcast");
793 static struct pernet_operations __net_initdata dev_mc_net_ops = {
794 .init = dev_mc_net_init,
795 .exit = dev_mc_net_exit,
798 void __init dev_mcast_init(void)
800 register_pernet_subsys(&dev_mc_net_ops);