dev_mcast.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/net_namespace.h>
  44. #include <net/ip.h>
  45. #include <net/route.h>
  46. #include <linux/skbuff.h>
  47. #include <net/sock.h>
  48. #include <net/arp.h>
  49. /*
  50. * Device multicast list maintenance.
  51. *
  52. * This is used both by IP and by the user level maintenance functions.
  53. * Unlike BSD we maintain a usage count on a given multicast address so
  54. * that a casual user application can add/delete multicasts used by
  55. * protocols without doing damage to the protocols when it deletes the
  56. * entries. It also helps IP as it tracks overlapping maps.
  57. *
  58. * Device mc lists are changed by bh at least if IPv6 is enabled,
  59. * so that it must be bh protected.
  60. *
  61. * We block accesses to device mc filters with netif_tx_lock.
  62. */
  63. /*
  64. * Delete a device level multicast
  65. */
  66. int dev_mc_delete(struct net_device *dev, void *addr, int alen, int glbl)
  67. {
  68. int err;
  69. netif_tx_lock_bh(dev);
  70. err = __dev_addr_delete(&dev->mc_list, &dev->mc_count,
  71. addr, alen, glbl);
  72. if (!err) {
  73. /*
  74. * We have altered the list, so the card
  75. * loaded filter is now wrong. Fix it
  76. */
  77. __dev_set_rx_mode(dev);
  78. }
  79. netif_tx_unlock_bh(dev);
  80. return err;
  81. }
  82. /*
  83. * Add a device level multicast
  84. */
  85. int dev_mc_add(struct net_device *dev, void *addr, int alen, int glbl)
  86. {
  87. int err;
  88. netif_tx_lock_bh(dev);
  89. err = __dev_addr_add(&dev->mc_list, &dev->mc_count, addr, alen, glbl);
  90. if (!err)
  91. __dev_set_rx_mode(dev);
  92. netif_tx_unlock_bh(dev);
  93. return err;
  94. }
  95. /**
  96. * dev_mc_sync - Synchronize device's multicast list to another device
  97. * @to: destination device
  98. * @from: source device
  99. *
  100. * Add newly added addresses to the destination device and release
  101. * addresses that have no users left. The source device must be
  102. * locked by netif_tx_lock_bh.
  103. *
  104. * This function is intended to be called from the dev->set_multicast_list
  105. * function of layered software devices.
  106. */
  107. int dev_mc_sync(struct net_device *to, struct net_device *from)
  108. {
  109. struct dev_addr_list *da, *next;
  110. int err = 0;
  111. netif_tx_lock_bh(to);
  112. da = from->mc_list;
  113. while (da != NULL) {
  114. next = da->next;
  115. if (!da->da_synced) {
  116. err = __dev_addr_add(&to->mc_list, &to->mc_count,
  117. da->da_addr, da->da_addrlen, 0);
  118. if (err < 0)
  119. break;
  120. da->da_synced = 1;
  121. da->da_users++;
  122. } else if (da->da_users == 1) {
  123. __dev_addr_delete(&to->mc_list, &to->mc_count,
  124. da->da_addr, da->da_addrlen, 0);
  125. __dev_addr_delete(&from->mc_list, &from->mc_count,
  126. da->da_addr, da->da_addrlen, 0);
  127. }
  128. da = next;
  129. }
  130. if (!err)
  131. __dev_set_rx_mode(to);
  132. netif_tx_unlock_bh(to);
  133. return err;
  134. }
  135. EXPORT_SYMBOL(dev_mc_sync);
  136. /**
  137. * dev_mc_unsync - Remove synchronized addresses from the destination
  138. * device
  139. * @to: destination device
  140. * @from: source device
  141. *
  142. * Remove all addresses that were added to the destination device by
  143. * dev_mc_sync(). This function is intended to be called from the
  144. * dev->stop function of layered software devices.
  145. */
  146. void dev_mc_unsync(struct net_device *to, struct net_device *from)
  147. {
  148. struct dev_addr_list *da, *next;
  149. netif_tx_lock_bh(from);
  150. netif_tx_lock_bh(to);
  151. da = from->mc_list;
  152. while (da != NULL) {
  153. next = da->next;
  154. if (!da->da_synced)
  155. continue;
  156. __dev_addr_delete(&to->mc_list, &to->mc_count,
  157. da->da_addr, da->da_addrlen, 0);
  158. da->da_synced = 0;
  159. __dev_addr_delete(&from->mc_list, &from->mc_count,
  160. da->da_addr, da->da_addrlen, 0);
  161. da = next;
  162. }
  163. __dev_set_rx_mode(to);
  164. netif_tx_unlock_bh(to);
  165. netif_tx_unlock_bh(from);
  166. }
  167. EXPORT_SYMBOL(dev_mc_unsync);
  168. #ifdef CONFIG_PROC_FS
  169. static void *dev_mc_seq_start(struct seq_file *seq, loff_t *pos)
  170. {
  171. struct net *net = seq->private;
  172. struct net_device *dev;
  173. loff_t off = 0;
  174. read_lock(&dev_base_lock);
  175. for_each_netdev(net, dev) {
  176. if (off++ == *pos)
  177. return dev;
  178. }
  179. return NULL;
  180. }
  181. static void *dev_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  182. {
  183. ++*pos;
  184. return next_net_device((struct net_device *)v);
  185. }
  186. static void dev_mc_seq_stop(struct seq_file *seq, void *v)
  187. {
  188. read_unlock(&dev_base_lock);
  189. }
  190. static int dev_mc_seq_show(struct seq_file *seq, void *v)
  191. {
  192. struct dev_addr_list *m;
  193. struct net_device *dev = v;
  194. netif_tx_lock_bh(dev);
  195. for (m = dev->mc_list; m; m = m->next) {
  196. int i;
  197. seq_printf(seq, "%-4d %-15s %-5d %-5d ", dev->ifindex,
  198. dev->name, m->dmi_users, m->dmi_gusers);
  199. for (i = 0; i < m->dmi_addrlen; i++)
  200. seq_printf(seq, "%02x", m->dmi_addr[i]);
  201. seq_putc(seq, '\n');
  202. }
  203. netif_tx_unlock_bh(dev);
  204. return 0;
  205. }
  206. static const struct seq_operations dev_mc_seq_ops = {
  207. .start = dev_mc_seq_start,
  208. .next = dev_mc_seq_next,
  209. .stop = dev_mc_seq_stop,
  210. .show = dev_mc_seq_show,
  211. };
  212. static int dev_mc_seq_open(struct inode *inode, struct file *file)
  213. {
  214. struct seq_file *seq;
  215. int res;
  216. res = seq_open(file, &dev_mc_seq_ops);
  217. if (!res) {
  218. seq = file->private_data;
  219. seq->private = get_proc_net(inode);
  220. if (!seq->private) {
  221. seq_release(inode, file);
  222. res = -ENXIO;
  223. }
  224. }
  225. return res;
  226. }
  227. static int dev_mc_seq_release(struct inode *inode, struct file *file)
  228. {
  229. struct seq_file *seq = file->private_data;
  230. struct net *net = seq->private;
  231. put_net(net);
  232. return seq_release(inode, file);
  233. }
  234. static const struct file_operations dev_mc_seq_fops = {
  235. .owner = THIS_MODULE,
  236. .open = dev_mc_seq_open,
  237. .read = seq_read,
  238. .llseek = seq_lseek,
  239. .release = dev_mc_seq_release,
  240. };
  241. #endif
  242. static int dev_mc_net_init(struct net *net)
  243. {
  244. if (!proc_net_fops_create(net, "dev_mcast", 0, &dev_mc_seq_fops))
  245. return -ENOMEM;
  246. return 0;
  247. }
  248. static void dev_mc_net_exit(struct net *net)
  249. {
  250. proc_net_remove(net, "dev_mcast");
  251. }
  252. static struct pernet_operations dev_mc_net_ops = {
  253. .init = dev_mc_net_init,
  254. .exit = dev_mc_net_exit,
  255. };
  256. void __init dev_mcast_init(void)
  257. {
  258. register_pernet_subsys(&dev_mc_net_ops);
  259. }
  260. EXPORT_SYMBOL(dev_mc_add);
  261. EXPORT_SYMBOL(dev_mc_delete);