dev_mcast.c 6.4 KB

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