netdev: bfin_mac: clean up printk messages
[linux-2.6.git] / net / core / dev_addr_lists.c
blob508f9c18992f0a0717ca31926fc2b2afcb07d9a1
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/list.h>
17 #include <linux/proc_fs.h>
20 * General list handling functions
23 static int __hw_addr_add_ex(struct netdev_hw_addr_list *list,
24 unsigned char *addr, int addr_len,
25 unsigned char addr_type, bool global)
27 struct netdev_hw_addr *ha;
28 int alloc_size;
30 if (addr_len > MAX_ADDR_LEN)
31 return -EINVAL;
33 list_for_each_entry(ha, &list->list, list) {
34 if (!memcmp(ha->addr, addr, addr_len) &&
35 ha->type == addr_type) {
36 if (global) {
37 /* check if addr is already used as global */
38 if (ha->global_use)
39 return 0;
40 else
41 ha->global_use = true;
43 ha->refcount++;
44 return 0;
49 alloc_size = sizeof(*ha);
50 if (alloc_size < L1_CACHE_BYTES)
51 alloc_size = L1_CACHE_BYTES;
52 ha = kmalloc(alloc_size, GFP_ATOMIC);
53 if (!ha)
54 return -ENOMEM;
55 memcpy(ha->addr, addr, addr_len);
56 ha->type = addr_type;
57 ha->refcount = 1;
58 ha->global_use = global;
59 ha->synced = false;
60 list_add_tail_rcu(&ha->list, &list->list);
61 list->count++;
62 return 0;
65 static int __hw_addr_add(struct netdev_hw_addr_list *list, unsigned char *addr,
66 int addr_len, unsigned char addr_type)
68 return __hw_addr_add_ex(list, addr, addr_len, addr_type, false);
71 static void ha_rcu_free(struct rcu_head *head)
73 struct netdev_hw_addr *ha;
75 ha = container_of(head, struct netdev_hw_addr, rcu_head);
76 kfree(ha);
79 static int __hw_addr_del_ex(struct netdev_hw_addr_list *list,
80 unsigned char *addr, int addr_len,
81 unsigned char addr_type, bool global)
83 struct netdev_hw_addr *ha;
85 list_for_each_entry(ha, &list->list, list) {
86 if (!memcmp(ha->addr, addr, addr_len) &&
87 (ha->type == addr_type || !addr_type)) {
88 if (global) {
89 if (!ha->global_use)
90 break;
91 else
92 ha->global_use = false;
94 if (--ha->refcount)
95 return 0;
96 list_del_rcu(&ha->list);
97 call_rcu(&ha->rcu_head, ha_rcu_free);
98 list->count--;
99 return 0;
102 return -ENOENT;
105 static int __hw_addr_del(struct netdev_hw_addr_list *list, unsigned char *addr,
106 int addr_len, unsigned char addr_type)
108 return __hw_addr_del_ex(list, addr, addr_len, addr_type, false);
111 int __hw_addr_add_multiple(struct netdev_hw_addr_list *to_list,
112 struct netdev_hw_addr_list *from_list,
113 int addr_len, unsigned char addr_type)
115 int err;
116 struct netdev_hw_addr *ha, *ha2;
117 unsigned char type;
119 list_for_each_entry(ha, &from_list->list, list) {
120 type = addr_type ? addr_type : ha->type;
121 err = __hw_addr_add(to_list, ha->addr, addr_len, type);
122 if (err)
123 goto unroll;
125 return 0;
127 unroll:
128 list_for_each_entry(ha2, &from_list->list, list) {
129 if (ha2 == ha)
130 break;
131 type = addr_type ? addr_type : ha2->type;
132 __hw_addr_del(to_list, ha2->addr, addr_len, type);
134 return err;
136 EXPORT_SYMBOL(__hw_addr_add_multiple);
138 void __hw_addr_del_multiple(struct netdev_hw_addr_list *to_list,
139 struct netdev_hw_addr_list *from_list,
140 int addr_len, unsigned char addr_type)
142 struct netdev_hw_addr *ha;
143 unsigned char type;
145 list_for_each_entry(ha, &from_list->list, list) {
146 type = addr_type ? addr_type : ha->type;
147 __hw_addr_del(to_list, ha->addr, addr_len, addr_type);
150 EXPORT_SYMBOL(__hw_addr_del_multiple);
152 int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
153 struct netdev_hw_addr_list *from_list,
154 int addr_len)
156 int err = 0;
157 struct netdev_hw_addr *ha, *tmp;
159 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
160 if (!ha->synced) {
161 err = __hw_addr_add(to_list, ha->addr,
162 addr_len, ha->type);
163 if (err)
164 break;
165 ha->synced = true;
166 ha->refcount++;
167 } else if (ha->refcount == 1) {
168 __hw_addr_del(to_list, ha->addr, addr_len, ha->type);
169 __hw_addr_del(from_list, ha->addr, addr_len, ha->type);
172 return err;
174 EXPORT_SYMBOL(__hw_addr_sync);
176 void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
177 struct netdev_hw_addr_list *from_list,
178 int addr_len)
180 struct netdev_hw_addr *ha, *tmp;
182 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
183 if (ha->synced) {
184 __hw_addr_del(to_list, ha->addr,
185 addr_len, ha->type);
186 ha->synced = false;
187 __hw_addr_del(from_list, ha->addr,
188 addr_len, ha->type);
192 EXPORT_SYMBOL(__hw_addr_unsync);
194 void __hw_addr_flush(struct netdev_hw_addr_list *list)
196 struct netdev_hw_addr *ha, *tmp;
198 list_for_each_entry_safe(ha, tmp, &list->list, list) {
199 list_del_rcu(&ha->list);
200 call_rcu(&ha->rcu_head, ha_rcu_free);
202 list->count = 0;
204 EXPORT_SYMBOL(__hw_addr_flush);
206 void __hw_addr_init(struct netdev_hw_addr_list *list)
208 INIT_LIST_HEAD(&list->list);
209 list->count = 0;
211 EXPORT_SYMBOL(__hw_addr_init);
214 * Device addresses handling functions
218 * dev_addr_flush - Flush device address list
219 * @dev: device
221 * Flush device address list and reset ->dev_addr.
223 * The caller must hold the rtnl_mutex.
225 void dev_addr_flush(struct net_device *dev)
227 /* rtnl_mutex must be held here */
229 __hw_addr_flush(&dev->dev_addrs);
230 dev->dev_addr = NULL;
232 EXPORT_SYMBOL(dev_addr_flush);
235 * dev_addr_init - Init device address list
236 * @dev: device
238 * Init device address list and create the first element,
239 * used by ->dev_addr.
241 * The caller must hold the rtnl_mutex.
243 int dev_addr_init(struct net_device *dev)
245 unsigned char addr[MAX_ADDR_LEN];
246 struct netdev_hw_addr *ha;
247 int err;
249 /* rtnl_mutex must be held here */
251 __hw_addr_init(&dev->dev_addrs);
252 memset(addr, 0, sizeof(addr));
253 err = __hw_addr_add(&dev->dev_addrs, addr, sizeof(addr),
254 NETDEV_HW_ADDR_T_LAN);
255 if (!err) {
257 * Get the first (previously created) address from the list
258 * and set dev_addr pointer to this location.
260 ha = list_first_entry(&dev->dev_addrs.list,
261 struct netdev_hw_addr, list);
262 dev->dev_addr = ha->addr;
264 return err;
266 EXPORT_SYMBOL(dev_addr_init);
269 * dev_addr_add - Add a device address
270 * @dev: device
271 * @addr: address to add
272 * @addr_type: address type
274 * Add a device address to the device or increase the reference count if
275 * it already exists.
277 * The caller must hold the rtnl_mutex.
279 int dev_addr_add(struct net_device *dev, unsigned char *addr,
280 unsigned char addr_type)
282 int err;
284 ASSERT_RTNL();
286 err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type);
287 if (!err)
288 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
289 return err;
291 EXPORT_SYMBOL(dev_addr_add);
294 * dev_addr_del - Release a device address.
295 * @dev: device
296 * @addr: address to delete
297 * @addr_type: address type
299 * Release reference to a device address and remove it from the device
300 * if the reference count drops to zero.
302 * The caller must hold the rtnl_mutex.
304 int dev_addr_del(struct net_device *dev, unsigned char *addr,
305 unsigned char addr_type)
307 int err;
308 struct netdev_hw_addr *ha;
310 ASSERT_RTNL();
313 * We can not remove the first address from the list because
314 * dev->dev_addr points to that.
316 ha = list_first_entry(&dev->dev_addrs.list,
317 struct netdev_hw_addr, list);
318 if (ha->addr == dev->dev_addr && ha->refcount == 1)
319 return -ENOENT;
321 err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len,
322 addr_type);
323 if (!err)
324 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
325 return err;
327 EXPORT_SYMBOL(dev_addr_del);
330 * dev_addr_add_multiple - Add device addresses from another device
331 * @to_dev: device to which addresses will be added
332 * @from_dev: device from which addresses will be added
333 * @addr_type: address type - 0 means type will be used from from_dev
335 * Add device addresses of the one device to another.
337 * The caller must hold the rtnl_mutex.
339 int dev_addr_add_multiple(struct net_device *to_dev,
340 struct net_device *from_dev,
341 unsigned char addr_type)
343 int err;
345 ASSERT_RTNL();
347 if (from_dev->addr_len != to_dev->addr_len)
348 return -EINVAL;
349 err = __hw_addr_add_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
350 to_dev->addr_len, addr_type);
351 if (!err)
352 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
353 return err;
355 EXPORT_SYMBOL(dev_addr_add_multiple);
358 * dev_addr_del_multiple - Delete device addresses by another device
359 * @to_dev: device where the addresses will be deleted
360 * @from_dev: device by which addresses the addresses will be deleted
361 * @addr_type: address type - 0 means type will used from from_dev
363 * Deletes addresses in to device by the list of addresses in from device.
365 * The caller must hold the rtnl_mutex.
367 int dev_addr_del_multiple(struct net_device *to_dev,
368 struct net_device *from_dev,
369 unsigned char addr_type)
371 ASSERT_RTNL();
373 if (from_dev->addr_len != to_dev->addr_len)
374 return -EINVAL;
375 __hw_addr_del_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
376 to_dev->addr_len, addr_type);
377 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
378 return 0;
380 EXPORT_SYMBOL(dev_addr_del_multiple);
383 * Unicast list handling functions
387 * dev_uc_add - Add a secondary unicast address
388 * @dev: device
389 * @addr: address to add
391 * Add a secondary unicast address to the device or increase
392 * the reference count if it already exists.
394 int dev_uc_add(struct net_device *dev, unsigned char *addr)
396 int err;
398 netif_addr_lock_bh(dev);
399 err = __hw_addr_add(&dev->uc, addr, dev->addr_len,
400 NETDEV_HW_ADDR_T_UNICAST);
401 if (!err)
402 __dev_set_rx_mode(dev);
403 netif_addr_unlock_bh(dev);
404 return err;
406 EXPORT_SYMBOL(dev_uc_add);
409 * dev_uc_del - Release secondary unicast address.
410 * @dev: device
411 * @addr: address to delete
413 * Release reference to a secondary unicast address and remove it
414 * from the device if the reference count drops to zero.
416 int dev_uc_del(struct net_device *dev, unsigned char *addr)
418 int err;
420 netif_addr_lock_bh(dev);
421 err = __hw_addr_del(&dev->uc, addr, dev->addr_len,
422 NETDEV_HW_ADDR_T_UNICAST);
423 if (!err)
424 __dev_set_rx_mode(dev);
425 netif_addr_unlock_bh(dev);
426 return err;
428 EXPORT_SYMBOL(dev_uc_del);
431 * dev_uc_sync - Synchronize device's unicast list to another device
432 * @to: destination device
433 * @from: source device
435 * Add newly added addresses to the destination device and release
436 * addresses that have no users left. The source device must be
437 * locked by netif_tx_lock_bh.
439 * This function is intended to be called from the dev->set_rx_mode
440 * function of layered software devices.
442 int dev_uc_sync(struct net_device *to, struct net_device *from)
444 int err = 0;
446 if (to->addr_len != from->addr_len)
447 return -EINVAL;
449 netif_addr_lock_bh(to);
450 err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
451 if (!err)
452 __dev_set_rx_mode(to);
453 netif_addr_unlock_bh(to);
454 return err;
456 EXPORT_SYMBOL(dev_uc_sync);
459 * dev_uc_unsync - Remove synchronized addresses from the destination device
460 * @to: destination device
461 * @from: source device
463 * Remove all addresses that were added to the destination device by
464 * dev_uc_sync(). This function is intended to be called from the
465 * dev->stop function of layered software devices.
467 void dev_uc_unsync(struct net_device *to, struct net_device *from)
469 if (to->addr_len != from->addr_len)
470 return;
472 netif_addr_lock_bh(from);
473 netif_addr_lock(to);
474 __hw_addr_unsync(&to->uc, &from->uc, to->addr_len);
475 __dev_set_rx_mode(to);
476 netif_addr_unlock(to);
477 netif_addr_unlock_bh(from);
479 EXPORT_SYMBOL(dev_uc_unsync);
482 * dev_uc_flush - Flush unicast addresses
483 * @dev: device
485 * Flush unicast addresses.
487 void dev_uc_flush(struct net_device *dev)
489 netif_addr_lock_bh(dev);
490 __hw_addr_flush(&dev->uc);
491 netif_addr_unlock_bh(dev);
493 EXPORT_SYMBOL(dev_uc_flush);
496 * dev_uc_flush - Init unicast address list
497 * @dev: device
499 * Init unicast address list.
501 void dev_uc_init(struct net_device *dev)
503 __hw_addr_init(&dev->uc);
505 EXPORT_SYMBOL(dev_uc_init);
508 * Multicast list handling functions
511 static int __dev_mc_add(struct net_device *dev, unsigned char *addr,
512 bool global)
514 int err;
516 netif_addr_lock_bh(dev);
517 err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
518 NETDEV_HW_ADDR_T_MULTICAST, global);
519 if (!err)
520 __dev_set_rx_mode(dev);
521 netif_addr_unlock_bh(dev);
522 return err;
525 * dev_mc_add - Add a multicast address
526 * @dev: device
527 * @addr: address to add
529 * Add a multicast address to the device or increase
530 * the reference count if it already exists.
532 int dev_mc_add(struct net_device *dev, unsigned char *addr)
534 return __dev_mc_add(dev, addr, false);
536 EXPORT_SYMBOL(dev_mc_add);
539 * dev_mc_add_global - Add a global multicast address
540 * @dev: device
541 * @addr: address to add
543 * Add a global multicast address to the device.
545 int dev_mc_add_global(struct net_device *dev, unsigned char *addr)
547 return __dev_mc_add(dev, addr, true);
549 EXPORT_SYMBOL(dev_mc_add_global);
551 static int __dev_mc_del(struct net_device *dev, unsigned char *addr,
552 bool global)
554 int err;
556 netif_addr_lock_bh(dev);
557 err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
558 NETDEV_HW_ADDR_T_MULTICAST, global);
559 if (!err)
560 __dev_set_rx_mode(dev);
561 netif_addr_unlock_bh(dev);
562 return err;
566 * dev_mc_del - Delete a multicast address.
567 * @dev: device
568 * @addr: address to delete
570 * Release reference to a multicast address and remove it
571 * from the device if the reference count drops to zero.
573 int dev_mc_del(struct net_device *dev, unsigned char *addr)
575 return __dev_mc_del(dev, addr, false);
577 EXPORT_SYMBOL(dev_mc_del);
580 * dev_mc_del_global - Delete a global multicast address.
581 * @dev: device
582 * @addr: address to delete
584 * Release reference to a multicast address and remove it
585 * from the device if the reference count drops to zero.
587 int dev_mc_del_global(struct net_device *dev, unsigned char *addr)
589 return __dev_mc_del(dev, addr, true);
591 EXPORT_SYMBOL(dev_mc_del_global);
594 * dev_mc_sync - Synchronize device's unicast list to another device
595 * @to: destination device
596 * @from: source device
598 * Add newly added addresses to the destination device and release
599 * addresses that have no users left. The source device must be
600 * locked by netif_tx_lock_bh.
602 * This function is intended to be called from the dev->set_multicast_list
603 * or dev->set_rx_mode function of layered software devices.
605 int dev_mc_sync(struct net_device *to, struct net_device *from)
607 int err = 0;
609 if (to->addr_len != from->addr_len)
610 return -EINVAL;
612 netif_addr_lock_bh(to);
613 err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
614 if (!err)
615 __dev_set_rx_mode(to);
616 netif_addr_unlock_bh(to);
617 return err;
619 EXPORT_SYMBOL(dev_mc_sync);
622 * dev_mc_unsync - Remove synchronized addresses from the destination device
623 * @to: destination device
624 * @from: source device
626 * Remove all addresses that were added to the destination device by
627 * dev_mc_sync(). This function is intended to be called from the
628 * dev->stop function of layered software devices.
630 void dev_mc_unsync(struct net_device *to, struct net_device *from)
632 if (to->addr_len != from->addr_len)
633 return;
635 netif_addr_lock_bh(from);
636 netif_addr_lock(to);
637 __hw_addr_unsync(&to->mc, &from->mc, to->addr_len);
638 __dev_set_rx_mode(to);
639 netif_addr_unlock(to);
640 netif_addr_unlock_bh(from);
642 EXPORT_SYMBOL(dev_mc_unsync);
645 * dev_mc_flush - Flush multicast addresses
646 * @dev: device
648 * Flush multicast addresses.
650 void dev_mc_flush(struct net_device *dev)
652 netif_addr_lock_bh(dev);
653 __hw_addr_flush(&dev->mc);
654 netif_addr_unlock_bh(dev);
656 EXPORT_SYMBOL(dev_mc_flush);
659 * dev_mc_flush - Init multicast address list
660 * @dev: device
662 * Init multicast address list.
664 void dev_mc_init(struct net_device *dev)
666 __hw_addr_init(&dev->mc);
668 EXPORT_SYMBOL(dev_mc_init);
670 #ifdef CONFIG_PROC_FS
671 #include <linux/seq_file.h>
673 static int dev_mc_seq_show(struct seq_file *seq, void *v)
675 struct netdev_hw_addr *ha;
676 struct net_device *dev = v;
678 if (v == SEQ_START_TOKEN)
679 return 0;
681 netif_addr_lock_bh(dev);
682 netdev_for_each_mc_addr(ha, dev) {
683 int i;
685 seq_printf(seq, "%-4d %-15s %-5d %-5d ", dev->ifindex,
686 dev->name, ha->refcount, ha->global_use);
688 for (i = 0; i < dev->addr_len; i++)
689 seq_printf(seq, "%02x", ha->addr[i]);
691 seq_putc(seq, '\n');
693 netif_addr_unlock_bh(dev);
694 return 0;
697 static const struct seq_operations dev_mc_seq_ops = {
698 .start = dev_seq_start,
699 .next = dev_seq_next,
700 .stop = dev_seq_stop,
701 .show = dev_mc_seq_show,
704 static int dev_mc_seq_open(struct inode *inode, struct file *file)
706 return seq_open_net(inode, file, &dev_mc_seq_ops,
707 sizeof(struct seq_net_private));
710 static const struct file_operations dev_mc_seq_fops = {
711 .owner = THIS_MODULE,
712 .open = dev_mc_seq_open,
713 .read = seq_read,
714 .llseek = seq_lseek,
715 .release = seq_release_net,
718 #endif
720 static int __net_init dev_mc_net_init(struct net *net)
722 if (!proc_net_fops_create(net, "dev_mcast", 0, &dev_mc_seq_fops))
723 return -ENOMEM;
724 return 0;
727 static void __net_exit dev_mc_net_exit(struct net *net)
729 proc_net_remove(net, "dev_mcast");
732 static struct pernet_operations __net_initdata dev_mc_net_ops = {
733 .init = dev_mc_net_init,
734 .exit = dev_mc_net_exit,
737 void __init dev_mcast_init(void)
739 register_pernet_subsys(&dev_mc_net_ops);