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.
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>
32 #include <linux/socket.h>
33 #include <linux/sockios.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>
44 #include <net/route.h>
45 #include <linux/skbuff.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
78 if (!(dev
->flags
&IFF_UP
))
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
))
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
)
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
) {
117 int old_glbl
= dmi
->dmi_gusers
;
122 if (--dmi
->dmi_users
)
126 * Last user. So delete the entry.
130 write_unlock_bh(&dev_mc_lock
);
135 * We have altered the list, so the card
136 * loaded filter is now wrong. Fix it
144 write_unlock_bh(&dev_mc_lock
);
149 * Add a device level multicast
152 int dev_mc_add(struct net_device
*dev
, void *addr
, int alen
, int glbl
)
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
) {
164 int old_glbl
= dmi
->dmi_gusers
;
174 if ((dmi
= dmi1
) == NULL
) {
175 write_unlock_bh(&dev_mc_lock
);
178 memcpy(dmi
->dmi_addr
, addr
, alen
);
179 dmi
->dmi_addrlen
= alen
;
180 dmi
->next
= dev
->mc_list
;
182 dmi
->dmi_gusers
= glbl
? 1 : 0;
185 write_unlock_bh(&dev_mc_lock
);
190 write_unlock_bh(&dev_mc_lock
);
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
);
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
;
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
) {
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");
242 if (pos
> offset
+ length
) {
243 read_unlock_bh(&dev_mc_lock
);
247 read_unlock_bh(&dev_mc_lock
);
252 read_unlock(&dev_base_lock
);
253 *start
= buffer
+ (offset
- begin
);
254 len
-= (offset
- begin
);
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
);