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