Import 2.2.5pre2
[davej-history.git] / net / core / dev_mcast.c
blobbce3f4a4a2659b8fcd0f2c7e9ae5fb2fb4692e4a
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.
64 * Update the multicast list into the physical NIC controller.
67 void dev_mc_upload(struct device *dev)
69 /* Don't do anything till we up the interface
70 [dev_open will call this function so the list will
71 stay sane] */
73 if(!(dev->flags&IFF_UP))
74 return;
77 * Devices with no set multicast don't get set
80 if(dev->set_multicast_list==NULL)
81 return;
83 start_bh_atomic();
84 dev->set_multicast_list(dev);
85 end_bh_atomic();
89 * Delete a device level multicast
92 int dev_mc_delete(struct device *dev, void *addr, int alen, int glbl)
94 int err = 0;
95 struct dev_mc_list *dmi, **dmip;
97 start_bh_atomic();
98 for (dmip=&dev->mc_list; (dmi=*dmip)!=NULL; dmip=&dmi->next) {
100 * Find the entry we want to delete. The device could
101 * have variable length entries so check these too.
103 if (memcmp(dmi->dmi_addr,addr,dmi->dmi_addrlen)==0 && 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;
110 if(--dmi->dmi_users)
111 goto done;
114 * Last user. So delete the entry.
116 *dmip = dmi->next;
117 dev->mc_count--;
118 kfree_s(dmi,sizeof(*dmi));
120 * We have altered the list, so the card
121 * loaded filter is now wrong. Fix it
123 end_bh_atomic();
124 dev_mc_upload(dev);
125 return 0;
128 err = -ENOENT;
129 done:
130 end_bh_atomic();
131 return err;
135 * Add a device level multicast
138 int dev_mc_add(struct device *dev, void *addr, int alen, int glbl)
140 int err = 0;
141 struct dev_mc_list *dmi, *dmi1;
143 dmi1 = (struct dev_mc_list *)kmalloc(sizeof(*dmi), gfp_any());
145 start_bh_atomic();
146 for(dmi=dev->mc_list; dmi!=NULL; dmi=dmi->next) {
147 if (memcmp(dmi->dmi_addr,addr,dmi->dmi_addrlen)==0 && dmi->dmi_addrlen==alen) {
148 if (glbl) {
149 int old_glbl = dmi->dmi_gusers;
150 dmi->dmi_gusers = 1;
151 if (old_glbl)
152 goto done;
154 dmi->dmi_users++;
155 goto done;
159 if ((dmi=dmi1)==NULL)
160 return -ENOMEM;
161 memcpy(dmi->dmi_addr, addr, alen);
162 dmi->dmi_addrlen=alen;
163 dmi->next=dev->mc_list;
164 dmi->dmi_users=1;
165 dmi->dmi_gusers=glbl ? 1 : 0;
166 dev->mc_list=dmi;
167 dev->mc_count++;
168 end_bh_atomic();
169 dev_mc_upload(dev);
170 return 0;
172 done:
173 end_bh_atomic();
174 if (dmi1)
175 kfree(dmi1);
176 return err;
180 * Discard multicast list when a device is downed
183 void dev_mc_discard(struct device *dev)
185 start_bh_atomic();
186 while (dev->mc_list!=NULL) {
187 struct dev_mc_list *tmp=dev->mc_list;
188 dev->mc_list=tmp->next;
189 if (tmp->dmi_users > tmp->dmi_gusers)
190 printk("dev_mc_discard: multicast leakage! dmi_users=%d\n", tmp->dmi_users);
191 kfree_s(tmp,sizeof(*tmp));
193 dev->mc_count=0;
194 end_bh_atomic();
197 #ifdef CONFIG_PROC_FS
198 static int dev_mc_read_proc(char *buffer, char **start, off_t offset,
199 int length, int *eof, void *data)
201 off_t pos=0, begin=0;
202 struct dev_mc_list *m;
203 int len=0;
204 struct device *dev;
206 start_bh_atomic();
208 for (dev = dev_base; dev; dev = dev->next) {
209 for (m = dev->mc_list; m; m = m->next) {
210 int i;
212 len += sprintf(buffer+len,"%-4d %-15s %-5d %-5d ", dev->ifindex, dev->name,
213 m->dmi_users, m->dmi_gusers);
215 for (i=0; i<m->dmi_addrlen; i++)
216 len += sprintf(buffer+len, "%02x", m->dmi_addr[i]);
218 len+=sprintf(buffer+len, "\n");
220 pos=begin+len;
221 if (pos < offset) {
222 len=0;
223 begin=pos;
225 if (pos > offset+length)
226 goto done;
229 *eof = 1;
231 done:
232 end_bh_atomic();
233 *start=buffer+(offset-begin);
234 len-=(offset-begin);
235 if(len>length)
236 len=length;
237 if(len<0)
238 len=0;
239 return len;
241 #endif
243 __initfunc(void dev_mcast_init(void))
245 #ifdef CONFIG_PROC_FS
246 struct proc_dir_entry *ent;
248 ent = create_proc_entry("net/dev_mcast", 0, 0);
249 ent->read_proc = dev_mc_read_proc;
250 #endif