dev_mcast.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Linux NET3: Multicast List maintenance.
  3. *
  4. * Authors:
  5. * Tim Kordas <tjk@nostromo.eeap.cwru.edu>
  6. * Richard Underwood <richard@wuzz.demon.co.uk>
  7. *
  8. * Stir fried together from the IP multicast and CAP patches above
  9. * Alan Cox <Alan.Cox@linux.org>
  10. *
  11. * Fixes:
  12. * Alan Cox : Update the device on a real delete
  13. * rather than any time but...
  14. * Alan Cox : IFF_ALLMULTI support.
  15. * Alan Cox : New format set_multicast_list() calls.
  16. * Gleb Natapov : Remove dev_mc_lock.
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version
  21. * 2 of the License, or (at your option) any later version.
  22. */
  23. #include <linux/module.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/system.h>
  26. #include <linux/bitops.h>
  27. #include <linux/types.h>
  28. #include <linux/kernel.h>
  29. #include <linux/string.h>
  30. #include <linux/mm.h>
  31. #include <linux/socket.h>
  32. #include <linux/sockios.h>
  33. #include <linux/in.h>
  34. #include <linux/errno.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/if_ether.h>
  37. #include <linux/inet.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/etherdevice.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/seq_file.h>
  42. #include <linux/init.h>
  43. #include <net/ip.h>
  44. #include <net/route.h>
  45. #include <linux/skbuff.h>
  46. #include <net/sock.h>
  47. #include <net/arp.h>
  48. /*
  49. * Device multicast list maintenance.
  50. *
  51. * This is used both by IP and by the user level maintenance functions.
  52. * Unlike BSD we maintain a usage count on a given multicast address so
  53. * that a casual user application can add/delete multicasts used by
  54. * protocols without doing damage to the protocols when it deletes the
  55. * entries. It also helps IP as it tracks overlapping maps.
  56. *
  57. * Device mc lists are changed by bh at least if IPv6 is enabled,
  58. * so that it must be bh protected.
  59. *
  60. * We block accesses to device mc filters with netif_tx_lock.
  61. */
  62. /*
  63. * Delete a device level multicast
  64. */
  65. int dev_mc_delete(struct net_device *dev, void *addr, int alen, int glbl)
  66. {
  67. int err;
  68. netif_tx_lock_bh(dev);
  69. err = __dev_addr_delete(&dev->mc_list, &dev->mc_count,
  70. addr, alen, glbl);
  71. if (!err) {
  72. /*
  73. * We have altered the list, so the card
  74. * loaded filter is now wrong. Fix it
  75. */
  76. __dev_set_rx_mode(dev);
  77. }
  78. netif_tx_unlock_bh(dev);
  79. return err;
  80. }
  81. /*
  82. * Add a device level multicast
  83. */
  84. int dev_mc_add(struct net_device *dev, void *addr, int alen, int glbl)
  85. {
  86. int err;
  87. netif_tx_lock_bh(dev);
  88. err = __dev_addr_add(&dev->mc_list, &dev->mc_count, addr, alen, glbl);
  89. if (!err)
  90. __dev_set_rx_mode(dev);
  91. netif_tx_unlock_bh(dev);
  92. return err;
  93. }
  94. /**
  95. * dev_mc_sync - Synchronize device's multicast list to another device
  96. * @to: destination device
  97. * @from: source device
  98. *
  99. * Add newly added addresses to the destination device and release
  100. * addresses that have no users left. The source device must be
  101. * locked by netif_tx_lock_bh.
  102. *
  103. * This function is intended to be called from the dev->set_multicast_list
  104. * function of layered software devices.
  105. */
  106. int dev_mc_sync(struct net_device *to, struct net_device *from)
  107. {
  108. struct dev_addr_list *da, *next;
  109. int err = 0;
  110. netif_tx_lock_bh(to);
  111. da = from->mc_list;
  112. while (da != NULL) {
  113. next = da->next;
  114. if (!da->da_synced) {
  115. err = __dev_addr_add(&to->mc_list, &to->mc_count,
  116. da->da_addr, da->da_addrlen, 0);
  117. if (err < 0)
  118. break;
  119. da->da_synced = 1;
  120. da->da_users++;
  121. } else if (da->da_users == 1) {
  122. __dev_addr_delete(&to->mc_list, &to->mc_count,
  123. da->da_addr, da->da_addrlen, 0);
  124. __dev_addr_delete(&from->mc_list, &from->mc_count,
  125. da->da_addr, da->da_addrlen, 0);
  126. }
  127. da = next;
  128. }
  129. if (!err)
  130. __dev_set_rx_mode(to);
  131. netif_tx_unlock_bh(to);
  132. return err;
  133. }
  134. EXPORT_SYMBOL(dev_mc_sync);
  135. /**
  136. * dev_mc_unsync - Remove synchronized addresses from the destination
  137. * device
  138. * @to: destination device
  139. * @from: source device
  140. *
  141. * Remove all addresses that were added to the destination device by
  142. * dev_mc_sync(). This function is intended to be called from the
  143. * dev->stop function of layered software devices.
  144. */
  145. void dev_mc_unsync(struct net_device *to, struct net_device *from)
  146. {
  147. struct dev_addr_list *da, *next;
  148. netif_tx_lock_bh(from);
  149. netif_tx_lock_bh(to);
  150. da = from->mc_list;
  151. while (da != NULL) {
  152. next = da->next;
  153. if (!da->da_synced)
  154. continue;
  155. __dev_addr_delete(&to->mc_list, &to->mc_count,
  156. da->da_addr, da->da_addrlen, 0);
  157. da->da_synced = 0;
  158. __dev_addr_delete(&from->mc_list, &from->mc_count,
  159. da->da_addr, da->da_addrlen, 0);
  160. da = next;
  161. }
  162. __dev_set_rx_mode(to);
  163. netif_tx_unlock_bh(to);
  164. netif_tx_unlock_bh(from);
  165. }
  166. EXPORT_SYMBOL(dev_mc_unsync);
  167. #ifdef CONFIG_PROC_FS
  168. static void *dev_mc_seq_start(struct seq_file *seq, loff_t *pos)
  169. {
  170. struct net_device *dev;
  171. loff_t off = 0;
  172. read_lock(&dev_base_lock);
  173. for_each_netdev(dev) {
  174. if (off++ == *pos)
  175. return dev;
  176. }
  177. return NULL;
  178. }
  179. static void *dev_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  180. {
  181. ++*pos;
  182. return next_net_device((struct net_device *)v);
  183. }
  184. static void dev_mc_seq_stop(struct seq_file *seq, void *v)
  185. {
  186. read_unlock(&dev_base_lock);
  187. }
  188. static int dev_mc_seq_show(struct seq_file *seq, void *v)
  189. {
  190. struct dev_addr_list *m;
  191. struct net_device *dev = v;
  192. netif_tx_lock_bh(dev);
  193. for (m = dev->mc_list; m; m = m->next) {
  194. int i;
  195. seq_printf(seq, "%-4d %-15s %-5d %-5d ", dev->ifindex,
  196. dev->name, m->dmi_users, m->dmi_gusers);
  197. for (i = 0; i < m->dmi_addrlen; i++)
  198. seq_printf(seq, "%02x", m->dmi_addr[i]);
  199. seq_putc(seq, '\n');
  200. }
  201. netif_tx_unlock_bh(dev);
  202. return 0;
  203. }
  204. static const struct seq_operations dev_mc_seq_ops = {
  205. .start = dev_mc_seq_start,
  206. .next = dev_mc_seq_next,
  207. .stop = dev_mc_seq_stop,
  208. .show = dev_mc_seq_show,
  209. };
  210. static int dev_mc_seq_open(struct inode *inode, struct file *file)
  211. {
  212. return seq_open(file, &dev_mc_seq_ops);
  213. }
  214. static const struct file_operations dev_mc_seq_fops = {
  215. .owner = THIS_MODULE,
  216. .open = dev_mc_seq_open,
  217. .read = seq_read,
  218. .llseek = seq_lseek,
  219. .release = seq_release,
  220. };
  221. #endif
  222. void __init dev_mcast_init(void)
  223. {
  224. proc_net_fops_create("dev_mcast", 0, &dev_mc_seq_fops);
  225. }
  226. EXPORT_SYMBOL(dev_mc_add);
  227. EXPORT_SYMBOL(dev_mc_delete);