2 * Linux NET3: Multicast List maintenance.
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>
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.
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.
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <asm/uaccess.h>
27 #include <asm/system.h>
28 #include <linux/bitops.h>
29 #include <linux/types.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/string.h>
34 #include <linux/socket.h>
35 #include <linux/sockios.h>
37 #include <linux/errno.h>
38 #include <linux/interrupt.h>
39 #include <linux/if_ether.h>
40 #include <linux/inet.h>
41 #include <linux/netdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/proc_fs.h>
44 #include <linux/seq_file.h>
45 #include <linux/init.h>
47 #include <net/route.h>
48 #include <linux/skbuff.h>
54 * Device multicast list maintenance.
56 * This is used both by IP and by the user level maintenance functions.
57 * Unlike BSD we maintain a usage count on a given multicast address so
58 * that a casual user application can add/delete multicasts used by
59 * protocols without doing damage to the protocols when it deletes the
60 * entries. It also helps IP as it tracks overlapping maps.
62 * Device mc lists are changed by bh at least if IPv6 is enabled,
63 * so that it must be bh protected.
65 * We block accesses to device mc filters with dev->xmit_lock.
69 * Update the multicast list into the physical NIC controller.
72 static void __dev_mc_upload(struct net_device
*dev
)
74 /* Don't do anything till we up the interface
75 * [dev_open will call this function so the list will
79 if (!(dev
->flags
&IFF_UP
))
83 * Devices with no set multicast or which have been
84 * detached don't get set.
87 if (dev
->set_multicast_list
== NULL
||
88 !netif_device_present(dev
))
91 dev
->set_multicast_list(dev
);
94 void dev_mc_upload(struct net_device
*dev
)
96 spin_lock_bh(&dev
->xmit_lock
);
98 spin_unlock_bh(&dev
->xmit_lock
);
102 * Delete a device level multicast
105 int dev_mc_delete(struct net_device
*dev
, void *addr
, int alen
, int glbl
)
108 struct dev_mc_list
*dmi
, **dmip
;
110 spin_lock_bh(&dev
->xmit_lock
);
112 for (dmip
= &dev
->mc_list
; (dmi
= *dmip
) != NULL
; dmip
= &dmi
->next
) {
114 * Find the entry we want to delete. The device could
115 * have variable length entries so check these too.
117 if (memcmp(dmi
->dmi_addr
, addr
, dmi
->dmi_addrlen
) == 0 &&
118 alen
== dmi
->dmi_addrlen
) {
120 int old_glbl
= dmi
->dmi_gusers
;
125 if (--dmi
->dmi_users
)
129 * Last user. So delete the entry.
137 * We have altered the list, so the card
138 * loaded filter is now wrong. Fix it
140 __dev_mc_upload(dev
);
142 spin_unlock_bh(&dev
->xmit_lock
);
148 spin_unlock_bh(&dev
->xmit_lock
);
153 * Add a device level multicast
156 int dev_mc_add(struct net_device
*dev
, void *addr
, int alen
, int glbl
)
159 struct dev_mc_list
*dmi
, *dmi1
;
161 dmi1
= (struct dev_mc_list
*)kmalloc(sizeof(*dmi
), GFP_ATOMIC
);
163 spin_lock_bh(&dev
->xmit_lock
);
164 for (dmi
= dev
->mc_list
; dmi
!= NULL
; dmi
= dmi
->next
) {
165 if (memcmp(dmi
->dmi_addr
, addr
, dmi
->dmi_addrlen
) == 0 &&
166 dmi
->dmi_addrlen
== alen
) {
168 int old_glbl
= dmi
->dmi_gusers
;
178 if ((dmi
= dmi1
) == NULL
) {
179 spin_unlock_bh(&dev
->xmit_lock
);
182 memcpy(dmi
->dmi_addr
, addr
, alen
);
183 dmi
->dmi_addrlen
= alen
;
184 dmi
->next
= dev
->mc_list
;
186 dmi
->dmi_gusers
= glbl
? 1 : 0;
190 __dev_mc_upload(dev
);
192 spin_unlock_bh(&dev
->xmit_lock
);
196 spin_unlock_bh(&dev
->xmit_lock
);
203 * Discard multicast list when a device is downed
206 void dev_mc_discard(struct net_device
*dev
)
208 spin_lock_bh(&dev
->xmit_lock
);
210 while (dev
->mc_list
!= NULL
) {
211 struct dev_mc_list
*tmp
= dev
->mc_list
;
212 dev
->mc_list
= tmp
->next
;
213 if (tmp
->dmi_users
> tmp
->dmi_gusers
)
214 printk("dev_mc_discard: multicast leakage! dmi_users=%d\n", tmp
->dmi_users
);
219 spin_unlock_bh(&dev
->xmit_lock
);
222 #ifdef CONFIG_PROC_FS
223 static void *dev_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
225 struct net_device
*dev
;
228 read_lock(&dev_base_lock
);
229 for (dev
= dev_base
; dev
; dev
= dev
->next
) {
236 static void *dev_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
238 struct net_device
*dev
= v
;
243 static void dev_mc_seq_stop(struct seq_file
*seq
, void *v
)
245 read_unlock(&dev_base_lock
);
249 static int dev_mc_seq_show(struct seq_file
*seq
, void *v
)
251 struct dev_mc_list
*m
;
252 struct net_device
*dev
= v
;
254 spin_lock_bh(&dev
->xmit_lock
);
255 for (m
= dev
->mc_list
; m
; m
= m
->next
) {
258 seq_printf(seq
, "%-4d %-15s %-5d %-5d ", dev
->ifindex
,
259 dev
->name
, m
->dmi_users
, m
->dmi_gusers
);
261 for (i
= 0; i
< m
->dmi_addrlen
; i
++)
262 seq_printf(seq
, "%02x", m
->dmi_addr
[i
]);
266 spin_unlock_bh(&dev
->xmit_lock
);
270 static struct seq_operations dev_mc_seq_ops
= {
271 .start
= dev_mc_seq_start
,
272 .next
= dev_mc_seq_next
,
273 .stop
= dev_mc_seq_stop
,
274 .show
= dev_mc_seq_show
,
277 static int dev_mc_seq_open(struct inode
*inode
, struct file
*file
)
279 return seq_open(file
, &dev_mc_seq_ops
);
282 static struct file_operations dev_mc_seq_fops
= {
283 .owner
= THIS_MODULE
,
284 .open
= dev_mc_seq_open
,
287 .release
= seq_release
,
292 void __init
dev_mcast_init(void)
294 proc_net_fops_create("dev_mcast", 0, &dev_mc_seq_fops
);
297 EXPORT_SYMBOL(dev_mc_add
);
298 EXPORT_SYMBOL(dev_mc_delete
);
299 EXPORT_SYMBOL(dev_mc_upload
);