dev_mcast.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. * Update the multicast list into the physical NIC controller.
  64. */
  65. static void __dev_mc_upload(struct net_device *dev)
  66. {
  67. /* Don't do anything till we up the interface
  68. * [dev_open will call this function so the list will
  69. * stay sane]
  70. */
  71. if (!(dev->flags&IFF_UP))
  72. return;
  73. /*
  74. * Devices with no set multicast or which have been
  75. * detached don't get set.
  76. */
  77. if (dev->set_multicast_list == NULL ||
  78. !netif_device_present(dev))
  79. return;
  80. dev->set_multicast_list(dev);
  81. }
  82. void dev_mc_upload(struct net_device *dev)
  83. {
  84. netif_tx_lock_bh(dev);
  85. __dev_mc_upload(dev);
  86. netif_tx_unlock_bh(dev);
  87. }
  88. /*
  89. * Delete a device level multicast
  90. */
  91. int dev_mc_delete(struct net_device *dev, void *addr, int alen, int glbl)
  92. {
  93. int err = 0;
  94. struct dev_mc_list *dmi, **dmip;
  95. netif_tx_lock_bh(dev);
  96. for (dmip = &dev->mc_list; (dmi = *dmip) != NULL; dmip = &dmi->next) {
  97. /*
  98. * Find the entry we want to delete. The device could
  99. * have variable length entries so check these too.
  100. */
  101. if (memcmp(dmi->dmi_addr, addr, dmi->dmi_addrlen) == 0 &&
  102. alen == dmi->dmi_addrlen) {
  103. if (glbl) {
  104. int old_glbl = dmi->dmi_gusers;
  105. dmi->dmi_gusers = 0;
  106. if (old_glbl == 0)
  107. break;
  108. }
  109. if (--dmi->dmi_users)
  110. goto done;
  111. /*
  112. * Last user. So delete the entry.
  113. */
  114. *dmip = dmi->next;
  115. dev->mc_count--;
  116. kfree(dmi);
  117. /*
  118. * We have altered the list, so the card
  119. * loaded filter is now wrong. Fix it
  120. */
  121. __dev_mc_upload(dev);
  122. netif_tx_unlock_bh(dev);
  123. return 0;
  124. }
  125. }
  126. err = -ENOENT;
  127. done:
  128. netif_tx_unlock_bh(dev);
  129. return err;
  130. }
  131. /*
  132. * Add a device level multicast
  133. */
  134. int dev_mc_add(struct net_device *dev, void *addr, int alen, int glbl)
  135. {
  136. int err = 0;
  137. struct dev_mc_list *dmi, *dmi1;
  138. dmi1 = kmalloc(sizeof(*dmi), GFP_ATOMIC);
  139. netif_tx_lock_bh(dev);
  140. for (dmi = dev->mc_list; dmi != NULL; dmi = dmi->next) {
  141. if (memcmp(dmi->dmi_addr, addr, dmi->dmi_addrlen) == 0 &&
  142. dmi->dmi_addrlen == alen) {
  143. if (glbl) {
  144. int old_glbl = dmi->dmi_gusers;
  145. dmi->dmi_gusers = 1;
  146. if (old_glbl)
  147. goto done;
  148. }
  149. dmi->dmi_users++;
  150. goto done;
  151. }
  152. }
  153. if ((dmi = dmi1) == NULL) {
  154. netif_tx_unlock_bh(dev);
  155. return -ENOMEM;
  156. }
  157. memcpy(dmi->dmi_addr, addr, alen);
  158. dmi->dmi_addrlen = alen;
  159. dmi->next = dev->mc_list;
  160. dmi->dmi_users = 1;
  161. dmi->dmi_gusers = glbl ? 1 : 0;
  162. dev->mc_list = dmi;
  163. dev->mc_count++;
  164. __dev_mc_upload(dev);
  165. netif_tx_unlock_bh(dev);
  166. return 0;
  167. done:
  168. netif_tx_unlock_bh(dev);
  169. kfree(dmi1);
  170. return err;
  171. }
  172. /*
  173. * Discard multicast list when a device is downed
  174. */
  175. void dev_mc_discard(struct net_device *dev)
  176. {
  177. netif_tx_lock_bh(dev);
  178. while (dev->mc_list != NULL) {
  179. struct dev_mc_list *tmp = dev->mc_list;
  180. dev->mc_list = tmp->next;
  181. if (tmp->dmi_users > tmp->dmi_gusers)
  182. printk("dev_mc_discard: multicast leakage! dmi_users=%d\n", tmp->dmi_users);
  183. kfree(tmp);
  184. }
  185. dev->mc_count = 0;
  186. netif_tx_unlock_bh(dev);
  187. }
  188. #ifdef CONFIG_PROC_FS
  189. static void *dev_mc_seq_start(struct seq_file *seq, loff_t *pos)
  190. {
  191. struct net_device *dev;
  192. loff_t off = 0;
  193. read_lock(&dev_base_lock);
  194. for (dev = dev_base; dev; dev = dev->next) {
  195. if (off++ == *pos)
  196. return dev;
  197. }
  198. return NULL;
  199. }
  200. static void *dev_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  201. {
  202. struct net_device *dev = v;
  203. ++*pos;
  204. return dev->next;
  205. }
  206. static void dev_mc_seq_stop(struct seq_file *seq, void *v)
  207. {
  208. read_unlock(&dev_base_lock);
  209. }
  210. static int dev_mc_seq_show(struct seq_file *seq, void *v)
  211. {
  212. struct dev_mc_list *m;
  213. struct net_device *dev = v;
  214. netif_tx_lock_bh(dev);
  215. for (m = dev->mc_list; m; m = m->next) {
  216. int i;
  217. seq_printf(seq, "%-4d %-15s %-5d %-5d ", dev->ifindex,
  218. dev->name, m->dmi_users, m->dmi_gusers);
  219. for (i = 0; i < m->dmi_addrlen; i++)
  220. seq_printf(seq, "%02x", m->dmi_addr[i]);
  221. seq_putc(seq, '\n');
  222. }
  223. netif_tx_unlock_bh(dev);
  224. return 0;
  225. }
  226. static struct seq_operations dev_mc_seq_ops = {
  227. .start = dev_mc_seq_start,
  228. .next = dev_mc_seq_next,
  229. .stop = dev_mc_seq_stop,
  230. .show = dev_mc_seq_show,
  231. };
  232. static int dev_mc_seq_open(struct inode *inode, struct file *file)
  233. {
  234. return seq_open(file, &dev_mc_seq_ops);
  235. }
  236. static const struct file_operations dev_mc_seq_fops = {
  237. .owner = THIS_MODULE,
  238. .open = dev_mc_seq_open,
  239. .read = seq_read,
  240. .llseek = seq_lseek,
  241. .release = seq_release,
  242. };
  243. #endif
  244. void __init dev_mcast_init(void)
  245. {
  246. proc_net_fops_create("dev_mcast", 0, &dev_mc_seq_fops);
  247. }
  248. EXPORT_SYMBOL(dev_mc_add);
  249. EXPORT_SYMBOL(dev_mc_delete);
  250. EXPORT_SYMBOL(dev_mc_upload);