net: convert multicast list to list_head
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / core / dev_addr_lists.c
1 /*
2 * net/core/dev_addr_lists.c - Functions for handling net device lists
3 * Copyright (c) 2010 Jiri Pirko <jpirko@redhat.com>
4 *
5 * This file contains functions for working with unicast, multicast and device
6 * addresses lists.
7 *
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.
12 */
13
14 #include <linux/netdevice.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/list.h>
17
18 /*
19 * General list handling functions
20 */
21
22 static int __hw_addr_add_ex(struct netdev_hw_addr_list *list,
23 unsigned char *addr, int addr_len,
24 unsigned char addr_type, bool global)
25 {
26 struct netdev_hw_addr *ha;
27 int alloc_size;
28
29 if (addr_len > MAX_ADDR_LEN)
30 return -EINVAL;
31
32 list_for_each_entry(ha, &list->list, list) {
33 if (!memcmp(ha->addr, addr, addr_len) &&
34 ha->type == addr_type) {
35 if (global) {
36 /* check if addr is already used as global */
37 if (ha->global_use)
38 return 0;
39 else
40 ha->global_use = true;
41 }
42 ha->refcount++;
43 return 0;
44 }
45 }
46
47
48 alloc_size = sizeof(*ha);
49 if (alloc_size < L1_CACHE_BYTES)
50 alloc_size = L1_CACHE_BYTES;
51 ha = kmalloc(alloc_size, GFP_ATOMIC);
52 if (!ha)
53 return -ENOMEM;
54 memcpy(ha->addr, addr, addr_len);
55 ha->type = addr_type;
56 ha->refcount = 1;
57 ha->global_use = global;
58 ha->synced = false;
59 list_add_tail_rcu(&ha->list, &list->list);
60 list->count++;
61 return 0;
62 }
63
64 static int __hw_addr_add(struct netdev_hw_addr_list *list, unsigned char *addr,
65 int addr_len, unsigned char addr_type)
66 {
67 return __hw_addr_add_ex(list, addr, addr_len, addr_type, false);
68 }
69
70 static void ha_rcu_free(struct rcu_head *head)
71 {
72 struct netdev_hw_addr *ha;
73
74 ha = container_of(head, struct netdev_hw_addr, rcu_head);
75 kfree(ha);
76 }
77
78 static int __hw_addr_del_ex(struct netdev_hw_addr_list *list,
79 unsigned char *addr, int addr_len,
80 unsigned char addr_type, bool global)
81 {
82 struct netdev_hw_addr *ha;
83
84 list_for_each_entry(ha, &list->list, list) {
85 if (!memcmp(ha->addr, addr, addr_len) &&
86 (ha->type == addr_type || !addr_type)) {
87 if (global) {
88 if (!ha->global_use)
89 break;
90 else
91 ha->global_use = false;
92 }
93 if (--ha->refcount)
94 return 0;
95 list_del_rcu(&ha->list);
96 call_rcu(&ha->rcu_head, ha_rcu_free);
97 list->count--;
98 return 0;
99 }
100 }
101 return -ENOENT;
102 }
103
104 static int __hw_addr_del(struct netdev_hw_addr_list *list, unsigned char *addr,
105 int addr_len, unsigned char addr_type)
106 {
107 return __hw_addr_del_ex(list, addr, addr_len, addr_type, false);
108 }
109
110 int __hw_addr_add_multiple(struct netdev_hw_addr_list *to_list,
111 struct netdev_hw_addr_list *from_list,
112 int addr_len, unsigned char addr_type)
113 {
114 int err;
115 struct netdev_hw_addr *ha, *ha2;
116 unsigned char type;
117
118 list_for_each_entry(ha, &from_list->list, list) {
119 type = addr_type ? addr_type : ha->type;
120 err = __hw_addr_add(to_list, ha->addr, addr_len, type);
121 if (err)
122 goto unroll;
123 }
124 return 0;
125
126 unroll:
127 list_for_each_entry(ha2, &from_list->list, list) {
128 if (ha2 == ha)
129 break;
130 type = addr_type ? addr_type : ha2->type;
131 __hw_addr_del(to_list, ha2->addr, addr_len, type);
132 }
133 return err;
134 }
135 EXPORT_SYMBOL(__hw_addr_add_multiple);
136
137 void __hw_addr_del_multiple(struct netdev_hw_addr_list *to_list,
138 struct netdev_hw_addr_list *from_list,
139 int addr_len, unsigned char addr_type)
140 {
141 struct netdev_hw_addr *ha;
142 unsigned char type;
143
144 list_for_each_entry(ha, &from_list->list, list) {
145 type = addr_type ? addr_type : ha->type;
146 __hw_addr_del(to_list, ha->addr, addr_len, addr_type);
147 }
148 }
149 EXPORT_SYMBOL(__hw_addr_del_multiple);
150
151 int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
152 struct netdev_hw_addr_list *from_list,
153 int addr_len)
154 {
155 int err = 0;
156 struct netdev_hw_addr *ha, *tmp;
157
158 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
159 if (!ha->synced) {
160 err = __hw_addr_add(to_list, ha->addr,
161 addr_len, ha->type);
162 if (err)
163 break;
164 ha->synced = true;
165 ha->refcount++;
166 } else if (ha->refcount == 1) {
167 __hw_addr_del(to_list, ha->addr, addr_len, ha->type);
168 __hw_addr_del(from_list, ha->addr, addr_len, ha->type);
169 }
170 }
171 return err;
172 }
173 EXPORT_SYMBOL(__hw_addr_sync);
174
175 void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
176 struct netdev_hw_addr_list *from_list,
177 int addr_len)
178 {
179 struct netdev_hw_addr *ha, *tmp;
180
181 list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
182 if (ha->synced) {
183 __hw_addr_del(to_list, ha->addr,
184 addr_len, ha->type);
185 ha->synced = false;
186 __hw_addr_del(from_list, ha->addr,
187 addr_len, ha->type);
188 }
189 }
190 }
191 EXPORT_SYMBOL(__hw_addr_unsync);
192
193 void __hw_addr_flush(struct netdev_hw_addr_list *list)
194 {
195 struct netdev_hw_addr *ha, *tmp;
196
197 list_for_each_entry_safe(ha, tmp, &list->list, list) {
198 list_del_rcu(&ha->list);
199 call_rcu(&ha->rcu_head, ha_rcu_free);
200 }
201 list->count = 0;
202 }
203 EXPORT_SYMBOL(__hw_addr_flush);
204
205 void __hw_addr_init(struct netdev_hw_addr_list *list)
206 {
207 INIT_LIST_HEAD(&list->list);
208 list->count = 0;
209 }
210 EXPORT_SYMBOL(__hw_addr_init);
211
212 /*
213 * Device addresses handling functions
214 */
215
216 /**
217 * dev_addr_flush - Flush device address list
218 * @dev: device
219 *
220 * Flush device address list and reset ->dev_addr.
221 *
222 * The caller must hold the rtnl_mutex.
223 */
224 void dev_addr_flush(struct net_device *dev)
225 {
226 /* rtnl_mutex must be held here */
227
228 __hw_addr_flush(&dev->dev_addrs);
229 dev->dev_addr = NULL;
230 }
231 EXPORT_SYMBOL(dev_addr_flush);
232
233 /**
234 * dev_addr_init - Init device address list
235 * @dev: device
236 *
237 * Init device address list and create the first element,
238 * used by ->dev_addr.
239 *
240 * The caller must hold the rtnl_mutex.
241 */
242 int dev_addr_init(struct net_device *dev)
243 {
244 unsigned char addr[MAX_ADDR_LEN];
245 struct netdev_hw_addr *ha;
246 int err;
247
248 /* rtnl_mutex must be held here */
249
250 __hw_addr_init(&dev->dev_addrs);
251 memset(addr, 0, sizeof(addr));
252 err = __hw_addr_add(&dev->dev_addrs, addr, sizeof(addr),
253 NETDEV_HW_ADDR_T_LAN);
254 if (!err) {
255 /*
256 * Get the first (previously created) address from the list
257 * and set dev_addr pointer to this location.
258 */
259 ha = list_first_entry(&dev->dev_addrs.list,
260 struct netdev_hw_addr, list);
261 dev->dev_addr = ha->addr;
262 }
263 return err;
264 }
265 EXPORT_SYMBOL(dev_addr_init);
266
267 /**
268 * dev_addr_add - Add a device address
269 * @dev: device
270 * @addr: address to add
271 * @addr_type: address type
272 *
273 * Add a device address to the device or increase the reference count if
274 * it already exists.
275 *
276 * The caller must hold the rtnl_mutex.
277 */
278 int dev_addr_add(struct net_device *dev, unsigned char *addr,
279 unsigned char addr_type)
280 {
281 int err;
282
283 ASSERT_RTNL();
284
285 err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type);
286 if (!err)
287 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
288 return err;
289 }
290 EXPORT_SYMBOL(dev_addr_add);
291
292 /**
293 * dev_addr_del - Release a device address.
294 * @dev: device
295 * @addr: address to delete
296 * @addr_type: address type
297 *
298 * Release reference to a device address and remove it from the device
299 * if the reference count drops to zero.
300 *
301 * The caller must hold the rtnl_mutex.
302 */
303 int dev_addr_del(struct net_device *dev, unsigned char *addr,
304 unsigned char addr_type)
305 {
306 int err;
307 struct netdev_hw_addr *ha;
308
309 ASSERT_RTNL();
310
311 /*
312 * We can not remove the first address from the list because
313 * dev->dev_addr points to that.
314 */
315 ha = list_first_entry(&dev->dev_addrs.list,
316 struct netdev_hw_addr, list);
317 if (ha->addr == dev->dev_addr && ha->refcount == 1)
318 return -ENOENT;
319
320 err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len,
321 addr_type);
322 if (!err)
323 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
324 return err;
325 }
326 EXPORT_SYMBOL(dev_addr_del);
327
328 /**
329 * dev_addr_add_multiple - Add device addresses from another device
330 * @to_dev: device to which addresses will be added
331 * @from_dev: device from which addresses will be added
332 * @addr_type: address type - 0 means type will be used from from_dev
333 *
334 * Add device addresses of the one device to another.
335 **
336 * The caller must hold the rtnl_mutex.
337 */
338 int dev_addr_add_multiple(struct net_device *to_dev,
339 struct net_device *from_dev,
340 unsigned char addr_type)
341 {
342 int err;
343
344 ASSERT_RTNL();
345
346 if (from_dev->addr_len != to_dev->addr_len)
347 return -EINVAL;
348 err = __hw_addr_add_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
349 to_dev->addr_len, addr_type);
350 if (!err)
351 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
352 return err;
353 }
354 EXPORT_SYMBOL(dev_addr_add_multiple);
355
356 /**
357 * dev_addr_del_multiple - Delete device addresses by another device
358 * @to_dev: device where the addresses will be deleted
359 * @from_dev: device by which addresses the addresses will be deleted
360 * @addr_type: address type - 0 means type will used from from_dev
361 *
362 * Deletes addresses in to device by the list of addresses in from device.
363 *
364 * The caller must hold the rtnl_mutex.
365 */
366 int dev_addr_del_multiple(struct net_device *to_dev,
367 struct net_device *from_dev,
368 unsigned char addr_type)
369 {
370 ASSERT_RTNL();
371
372 if (from_dev->addr_len != to_dev->addr_len)
373 return -EINVAL;
374 __hw_addr_del_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
375 to_dev->addr_len, addr_type);
376 call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
377 return 0;
378 }
379 EXPORT_SYMBOL(dev_addr_del_multiple);
380
381 /*
382 * Unicast list handling functions
383 */
384
385 /**
386 * dev_uc_add - Add a secondary unicast address
387 * @dev: device
388 * @addr: address to add
389 *
390 * Add a secondary unicast address to the device or increase
391 * the reference count if it already exists.
392 */
393 int dev_uc_add(struct net_device *dev, unsigned char *addr)
394 {
395 int err;
396
397 netif_addr_lock_bh(dev);
398 err = __hw_addr_add(&dev->uc, addr, dev->addr_len,
399 NETDEV_HW_ADDR_T_UNICAST);
400 if (!err)
401 __dev_set_rx_mode(dev);
402 netif_addr_unlock_bh(dev);
403 return err;
404 }
405 EXPORT_SYMBOL(dev_uc_add);
406
407 /**
408 * dev_uc_del - Release secondary unicast address.
409 * @dev: device
410 * @addr: address to delete
411 *
412 * Release reference to a secondary unicast address and remove it
413 * from the device if the reference count drops to zero.
414 */
415 int dev_uc_del(struct net_device *dev, unsigned char *addr)
416 {
417 int err;
418
419 netif_addr_lock_bh(dev);
420 err = __hw_addr_del(&dev->uc, addr, dev->addr_len,
421 NETDEV_HW_ADDR_T_UNICAST);
422 if (!err)
423 __dev_set_rx_mode(dev);
424 netif_addr_unlock_bh(dev);
425 return err;
426 }
427 EXPORT_SYMBOL(dev_uc_del);
428
429 /**
430 * dev_uc_sync - Synchronize device's unicast list to another device
431 * @to: destination device
432 * @from: source device
433 *
434 * Add newly added addresses to the destination device and release
435 * addresses that have no users left. The source device must be
436 * locked by netif_tx_lock_bh.
437 *
438 * This function is intended to be called from the dev->set_rx_mode
439 * function of layered software devices.
440 */
441 int dev_uc_sync(struct net_device *to, struct net_device *from)
442 {
443 int err = 0;
444
445 if (to->addr_len != from->addr_len)
446 return -EINVAL;
447
448 netif_addr_lock_bh(to);
449 err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
450 if (!err)
451 __dev_set_rx_mode(to);
452 netif_addr_unlock_bh(to);
453 return err;
454 }
455 EXPORT_SYMBOL(dev_uc_sync);
456
457 /**
458 * dev_uc_unsync - Remove synchronized addresses from the destination device
459 * @to: destination device
460 * @from: source device
461 *
462 * Remove all addresses that were added to the destination device by
463 * dev_uc_sync(). This function is intended to be called from the
464 * dev->stop function of layered software devices.
465 */
466 void dev_uc_unsync(struct net_device *to, struct net_device *from)
467 {
468 if (to->addr_len != from->addr_len)
469 return;
470
471 netif_addr_lock_bh(from);
472 netif_addr_lock(to);
473 __hw_addr_unsync(&to->uc, &from->uc, to->addr_len);
474 __dev_set_rx_mode(to);
475 netif_addr_unlock(to);
476 netif_addr_unlock_bh(from);
477 }
478 EXPORT_SYMBOL(dev_uc_unsync);
479
480 /**
481 * dev_uc_flush - Flush unicast addresses
482 * @dev: device
483 *
484 * Flush unicast addresses.
485 */
486 void dev_uc_flush(struct net_device *dev)
487 {
488 netif_addr_lock_bh(dev);
489 __hw_addr_flush(&dev->uc);
490 netif_addr_unlock_bh(dev);
491 }
492 EXPORT_SYMBOL(dev_uc_flush);
493
494 /**
495 * dev_uc_flush - Init unicast address list
496 * @dev: device
497 *
498 * Init unicast address list.
499 */
500 void dev_uc_init(struct net_device *dev)
501 {
502 __hw_addr_init(&dev->uc);
503 }
504 EXPORT_SYMBOL(dev_uc_init);
505
506 /*
507 * Multicast list handling functions
508 */
509
510 static int __dev_mc_add(struct net_device *dev, unsigned char *addr,
511 bool global)
512 {
513 int err;
514
515 netif_addr_lock_bh(dev);
516 err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
517 NETDEV_HW_ADDR_T_MULTICAST, global);
518 if (!err)
519 __dev_set_rx_mode(dev);
520 netif_addr_unlock_bh(dev);
521 return err;
522 }
523 /**
524 * dev_mc_add - Add a multicast address
525 * @dev: device
526 * @addr: address to add
527 *
528 * Add a multicast address to the device or increase
529 * the reference count if it already exists.
530 */
531 int dev_mc_add(struct net_device *dev, unsigned char *addr)
532 {
533 return __dev_mc_add(dev, addr, false);
534 }
535 EXPORT_SYMBOL(dev_mc_add);
536
537 /**
538 * dev_mc_add_global - Add a global multicast address
539 * @dev: device
540 * @addr: address to add
541 *
542 * Add a global multicast address to the device.
543 */
544 int dev_mc_add_global(struct net_device *dev, unsigned char *addr)
545 {
546 return __dev_mc_add(dev, addr, true);
547 }
548 EXPORT_SYMBOL(dev_mc_add_global);
549
550 static int __dev_mc_del(struct net_device *dev, unsigned char *addr,
551 bool global)
552 {
553 int err;
554
555 netif_addr_lock_bh(dev);
556 err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
557 NETDEV_HW_ADDR_T_MULTICAST, global);
558 if (!err)
559 __dev_set_rx_mode(dev);
560 netif_addr_unlock_bh(dev);
561 return err;
562 }
563
564 /**
565 * dev_mc_del - Delete a multicast address.
566 * @dev: device
567 * @addr: address to delete
568 *
569 * Release reference to a multicast address and remove it
570 * from the device if the reference count drops to zero.
571 */
572 int dev_mc_del(struct net_device *dev, unsigned char *addr)
573 {
574 return __dev_mc_del(dev, addr, false);
575 }
576 EXPORT_SYMBOL(dev_mc_del);
577
578 /**
579 * dev_mc_del_global - Delete a global multicast address.
580 * @dev: device
581 * @addr: address to delete
582 *
583 * Release reference to a multicast address and remove it
584 * from the device if the reference count drops to zero.
585 */
586 int dev_mc_del_global(struct net_device *dev, unsigned char *addr)
587 {
588 return __dev_mc_del(dev, addr, true);
589 }
590 EXPORT_SYMBOL(dev_mc_del_global);
591
592 /**
593 * dev_mc_sync - Synchronize device's unicast list to another device
594 * @to: destination device
595 * @from: source device
596 *
597 * Add newly added addresses to the destination device and release
598 * addresses that have no users left. The source device must be
599 * locked by netif_tx_lock_bh.
600 *
601 * This function is intended to be called from the dev->set_multicast_list
602 * or dev->set_rx_mode function of layered software devices.
603 */
604 int dev_mc_sync(struct net_device *to, struct net_device *from)
605 {
606 int err = 0;
607
608 if (to->addr_len != from->addr_len)
609 return -EINVAL;
610
611 netif_addr_lock_bh(to);
612 err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
613 if (!err)
614 __dev_set_rx_mode(to);
615 netif_addr_unlock_bh(to);
616 return err;
617 }
618 EXPORT_SYMBOL(dev_mc_sync);
619
620 /**
621 * dev_mc_unsync - Remove synchronized addresses from the destination device
622 * @to: destination device
623 * @from: source device
624 *
625 * Remove all addresses that were added to the destination device by
626 * dev_mc_sync(). This function is intended to be called from the
627 * dev->stop function of layered software devices.
628 */
629 void dev_mc_unsync(struct net_device *to, struct net_device *from)
630 {
631 if (to->addr_len != from->addr_len)
632 return;
633
634 netif_addr_lock_bh(from);
635 netif_addr_lock(to);
636 __hw_addr_unsync(&to->mc, &from->mc, to->addr_len);
637 __dev_set_rx_mode(to);
638 netif_addr_unlock(to);
639 netif_addr_unlock_bh(from);
640 }
641 EXPORT_SYMBOL(dev_mc_unsync);
642
643 /**
644 * dev_mc_flush - Flush multicast addresses
645 * @dev: device
646 *
647 * Flush multicast addresses.
648 */
649 void dev_mc_flush(struct net_device *dev)
650 {
651 netif_addr_lock_bh(dev);
652 __hw_addr_flush(&dev->mc);
653 netif_addr_unlock_bh(dev);
654 }
655 EXPORT_SYMBOL(dev_mc_flush);
656
657 /**
658 * dev_mc_flush - Init multicast address list
659 * @dev: device
660 *
661 * Init multicast address list.
662 */
663 void dev_mc_init(struct net_device *dev)
664 {
665 __hw_addr_init(&dev->mc);
666 }
667 EXPORT_SYMBOL(dev_mc_init);
668
669 #ifdef CONFIG_PROC_FS
670 #include <linux/proc_fs.h>
671 #include <linux/seq_file.h>
672
673 static int dev_mc_seq_show(struct seq_file *seq, void *v)
674 {
675 struct netdev_hw_addr *ha;
676 struct net_device *dev = v;
677
678 if (v == SEQ_START_TOKEN)
679 return 0;
680
681 netif_addr_lock_bh(dev);
682 netdev_for_each_mc_addr(ha, dev) {
683 int i;
684
685 seq_printf(seq, "%-4d %-15s %-5d %-5d ", dev->ifindex,
686 dev->name, ha->refcount, ha->global_use);
687
688 for (i = 0; i < dev->addr_len; i++)
689 seq_printf(seq, "%02x", ha->addr[i]);
690
691 seq_putc(seq, '\n');
692 }
693 netif_addr_unlock_bh(dev);
694 return 0;
695 }
696
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,
702 };
703
704 static int dev_mc_seq_open(struct inode *inode, struct file *file)
705 {
706 return seq_open_net(inode, file, &dev_mc_seq_ops,
707 sizeof(struct seq_net_private));
708 }
709
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,
716 };
717
718 #endif
719
720 static int __net_init dev_mc_net_init(struct net *net)
721 {
722 if (!proc_net_fops_create(net, "dev_mcast", 0, &dev_mc_seq_fops))
723 return -ENOMEM;
724 return 0;
725 }
726
727 static void __net_exit dev_mc_net_exit(struct net *net)
728 {
729 proc_net_remove(net, "dev_mcast");
730 }
731
732 static struct pernet_operations __net_initdata dev_mc_net_ops = {
733 .init = dev_mc_net_init,
734 .exit = dev_mc_net_exit,
735 };
736
737 void __init dev_mcast_init(void)
738 {
739 register_pernet_subsys(&dev_mc_net_ops);
740 }
741