dev_mcast.c 6.5 KB

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