ipv6: Pull IPv6 GSO registration out of the module
[linux/fpc-iii.git] / net / core / dev_addr_lists.c
blob87cc17db2d566e5846601d6eb03ba38ce64b2ddb
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 (ha->addr == dev->dev_addr && 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);
730 #ifdef CONFIG_PROC_FS
731 #include <linux/seq_file.h>
733 static int dev_mc_seq_show(struct seq_file *seq, void *v)
735 struct netdev_hw_addr *ha;
736 struct net_device *dev = v;
738 if (v == SEQ_START_TOKEN)
739 return 0;
741 netif_addr_lock_bh(dev);
742 netdev_for_each_mc_addr(ha, dev) {
743 int i;
745 seq_printf(seq, "%-4d %-15s %-5d %-5d ", dev->ifindex,
746 dev->name, ha->refcount, ha->global_use);
748 for (i = 0; i < dev->addr_len; i++)
749 seq_printf(seq, "%02x", ha->addr[i]);
751 seq_putc(seq, '\n');
753 netif_addr_unlock_bh(dev);
754 return 0;
757 static const struct seq_operations dev_mc_seq_ops = {
758 .start = dev_seq_start,
759 .next = dev_seq_next,
760 .stop = dev_seq_stop,
761 .show = dev_mc_seq_show,
764 static int dev_mc_seq_open(struct inode *inode, struct file *file)
766 return seq_open_net(inode, file, &dev_mc_seq_ops,
767 sizeof(struct seq_net_private));
770 static const struct file_operations dev_mc_seq_fops = {
771 .owner = THIS_MODULE,
772 .open = dev_mc_seq_open,
773 .read = seq_read,
774 .llseek = seq_lseek,
775 .release = seq_release_net,
778 #endif
780 static int __net_init dev_mc_net_init(struct net *net)
782 if (!proc_net_fops_create(net, "dev_mcast", 0, &dev_mc_seq_fops))
783 return -ENOMEM;
784 return 0;
787 static void __net_exit dev_mc_net_exit(struct net *net)
789 proc_net_remove(net, "dev_mcast");
792 static struct pernet_operations __net_initdata dev_mc_net_ops = {
793 .init = dev_mc_net_init,
794 .exit = dev_mc_net_exit,
797 void __init dev_mcast_init(void)
799 register_pernet_subsys(&dev_mc_net_ops);