Merge with Linux 2.4.0-test5-pre3.
[linux-2.6/linux-mips.git] / net / core / dev_mcast.c
blobcf590db040370cafd6d9074312de1891d2ca57f0
1 /*
2 * Linux NET3: Multicast List maintenance.
4 * Authors:
5 * Tim Kordas <tjk@nostromo.eeap.cwru.edu>
6 * Richard Underwood <richard@wuzz.demon.co.uk>
8 * Stir fried together from the IP multicast and CAP patches above
9 * Alan Cox <Alan.Cox@linux.org>
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.
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
23 #include <linux/config.h>
24 #include <asm/uaccess.h>
25 #include <asm/system.h>
26 #include <asm/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/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>
51 * Device multicast list maintenance.
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.
59 * Device mc lists are changed by bh at least if IPv6 is enabled,
60 * so that it must be bh protected.
62 * We protect all mc lists with global rw lock
63 * and block accesses to device mc filters with dev->xmit_lock.
65 static rwlock_t dev_mc_lock = RW_LOCK_UNLOCKED;
68 * Update the multicast list into the physical NIC controller.
71 void dev_mc_upload(struct net_device *dev)
73 /* Don't do anything till we up the interface
74 * [dev_open will call this function so the list will
75 * stay sane]
78 if (!(dev->flags&IFF_UP))
79 return;
82 * Devices with no set multicast or which have been
83 * detached don't get set.
86 if (dev->set_multicast_list == NULL ||
87 !netif_device_present(dev))
88 return;
90 read_lock_bh(&dev_mc_lock);
91 spin_lock(&dev->xmit_lock);
92 dev->xmit_lock_owner = smp_processor_id();
93 dev->set_multicast_list(dev);
94 dev->xmit_lock_owner = -1;
95 spin_unlock(&dev->xmit_lock);
96 read_unlock_bh(&dev_mc_lock);
100 * Delete a device level multicast
103 int dev_mc_delete(struct net_device *dev, void *addr, int alen, int glbl)
105 int err = 0;
106 struct dev_mc_list *dmi, **dmip;
108 write_lock_bh(&dev_mc_lock);
109 for (dmip = &dev->mc_list; (dmi = *dmip) != NULL; dmip = &dmi->next) {
111 * Find the entry we want to delete. The device could
112 * have variable length entries so check these too.
114 if (memcmp(dmi->dmi_addr, addr, dmi->dmi_addrlen) == 0 &&
115 alen == dmi->dmi_addrlen) {
116 if (glbl) {
117 int old_glbl = dmi->dmi_gusers;
118 dmi->dmi_gusers = 0;
119 if (old_glbl == 0)
120 break;
122 if (--dmi->dmi_users)
123 goto done;
126 * Last user. So delete the entry.
128 *dmip = dmi->next;
129 dev->mc_count--;
130 write_unlock_bh(&dev_mc_lock);
132 kfree(dmi);
135 * We have altered the list, so the card
136 * loaded filter is now wrong. Fix it
138 dev_mc_upload(dev);
139 return 0;
142 err = -ENOENT;
143 done:
144 write_unlock_bh(&dev_mc_lock);
145 return err;
149 * Add a device level multicast
152 int dev_mc_add(struct net_device *dev, void *addr, int alen, int glbl)
154 int err = 0;
155 struct dev_mc_list *dmi, *dmi1;
157 dmi1 = (struct dev_mc_list *)kmalloc(sizeof(*dmi), GFP_ATOMIC);
159 write_lock_bh(&dev_mc_lock);
160 for (dmi = dev->mc_list; dmi != NULL; dmi = dmi->next) {
161 if (memcmp(dmi->dmi_addr, addr, dmi->dmi_addrlen) == 0 &&
162 dmi->dmi_addrlen == alen) {
163 if (glbl) {
164 int old_glbl = dmi->dmi_gusers;
165 dmi->dmi_gusers = 1;
166 if (old_glbl)
167 goto done;
169 dmi->dmi_users++;
170 goto done;
174 if ((dmi = dmi1) == NULL) {
175 write_unlock_bh(&dev_mc_lock);
176 return -ENOMEM;
178 memcpy(dmi->dmi_addr, addr, alen);
179 dmi->dmi_addrlen = alen;
180 dmi->next = dev->mc_list;
181 dmi->dmi_users = 1;
182 dmi->dmi_gusers = glbl ? 1 : 0;
183 dev->mc_list = dmi;
184 dev->mc_count++;
185 write_unlock_bh(&dev_mc_lock);
186 dev_mc_upload(dev);
187 return 0;
189 done:
190 write_unlock_bh(&dev_mc_lock);
191 if (dmi1)
192 kfree(dmi1);
193 return err;
197 * Discard multicast list when a device is downed
200 void dev_mc_discard(struct net_device *dev)
202 write_lock_bh(&dev_mc_lock);
203 while (dev->mc_list != NULL) {
204 struct dev_mc_list *tmp = dev->mc_list;
205 dev->mc_list = tmp->next;
206 if (tmp->dmi_users > tmp->dmi_gusers)
207 printk("dev_mc_discard: multicast leakage! dmi_users=%d\n", tmp->dmi_users);
208 kfree(tmp);
210 dev->mc_count = 0;
211 write_unlock_bh(&dev_mc_lock);
214 #ifdef CONFIG_PROC_FS
215 static int dev_mc_read_proc(char *buffer, char **start, off_t offset,
216 int length, int *eof, void *data)
218 off_t pos = 0, begin = 0;
219 struct dev_mc_list *m;
220 int len = 0;
221 struct net_device *dev;
223 read_lock(&dev_base_lock);
224 for (dev = dev_base; dev; dev = dev->next) {
225 read_lock_bh(&dev_mc_lock);
226 for (m = dev->mc_list; m; m = m->next) {
227 int i;
229 len += sprintf(buffer+len,"%-4d %-15s %-5d %-5d ", dev->ifindex,
230 dev->name, m->dmi_users, m->dmi_gusers);
232 for (i = 0; i < m->dmi_addrlen; i++)
233 len += sprintf(buffer+len, "%02x", m->dmi_addr[i]);
235 len += sprintf(buffer+len, "\n");
237 pos = begin + len;
238 if (pos < offset) {
239 len = 0;
240 begin = pos;
242 if (pos > offset + length) {
243 read_unlock_bh(&dev_mc_lock);
244 goto done;
247 read_unlock_bh(&dev_mc_lock);
249 *eof = 1;
251 done:
252 read_unlock(&dev_base_lock);
253 *start = buffer + (offset - begin);
254 len -= (offset - begin);
255 if (len > length)
256 len = length;
257 if (len < 0)
258 len = 0;
259 return len;
261 #endif
263 void __init dev_mcast_init(void)
265 #ifdef CONFIG_PROC_FS
266 create_proc_read_entry("net/dev_mcast", 0, 0, dev_mc_read_proc, NULL);
267 #endif