dev_mcast.c 4.9 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 *	Linux NET3:	Multicast List maintenance.
L
Linus Torvalds 已提交
3 4
 *
 *	Authors:
5
 *		Tim Kordas <tjk@nostromo.eeap.cwru.edu>
L
Linus Torvalds 已提交
6 7 8
 *		Richard Underwood <richard@wuzz.demon.co.uk>
 *
 *	Stir fried together from the IP multicast and CAP patches above
9
 *		Alan Cox <Alan.Cox@linux.org>
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *	Fixes:
 *		Alan Cox	:	Update the device on a real delete
 *					rather than any time but...
 *		Alan Cox	:	IFF_ALLMULTI support.
 *		Alan Cox	: 	New format set_multicast_list() calls.
 *		Gleb Natapov    :       Remove dev_mc_lock.
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 */

24
#include <linux/module.h>
L
Linus Torvalds 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <asm/uaccess.h>
#include <asm/system.h>
#include <linux/bitops.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/in.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/if_ether.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/init.h>
#include <net/ip.h>
#include <net/route.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <net/arp.h>


/*
52
 *	Device multicast list maintenance.
L
Linus Torvalds 已提交
53
 *
54 55 56
 *	This is used both by IP and by the user level maintenance functions.
 *	Unlike BSD we maintain a usage count on a given multicast address so
 *	that a casual user application can add/delete multicasts used by
L
Linus Torvalds 已提交
57 58 59 60 61 62
 *	protocols without doing damage to the protocols when it deletes the
 *	entries. It also helps IP as it tracks overlapping maps.
 *
 *	Device mc lists are changed by bh at least if IPv6 is enabled,
 *	so that it must be bh protected.
 *
H
Herbert Xu 已提交
63
 *	We block accesses to device mc filters with netif_tx_lock.
L
Linus Torvalds 已提交
64 65 66 67 68
 */

/*
 *	Update the multicast list into the physical NIC controller.
 */
69

L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
static void __dev_mc_upload(struct net_device *dev)
{
	/* Don't do anything till we up the interface
	 * [dev_open will call this function so the list will
	 * stay sane]
	 */

	if (!(dev->flags&IFF_UP))
		return;

	/*
	 *	Devices with no set multicast or which have been
	 *	detached don't get set.
	 */

	if (dev->set_multicast_list == NULL ||
	    !netif_device_present(dev))
		return;

	dev->set_multicast_list(dev);
}

void dev_mc_upload(struct net_device *dev)
{
H
Herbert Xu 已提交
94
	netif_tx_lock_bh(dev);
L
Linus Torvalds 已提交
95
	__dev_mc_upload(dev);
H
Herbert Xu 已提交
96
	netif_tx_unlock_bh(dev);
L
Linus Torvalds 已提交
97 98 99 100 101
}

/*
 *	Delete a device level multicast
 */
102

L
Linus Torvalds 已提交
103 104
int dev_mc_delete(struct net_device *dev, void *addr, int alen, int glbl)
{
105
	int err;
L
Linus Torvalds 已提交
106

H
Herbert Xu 已提交
107
	netif_tx_lock_bh(dev);
108 109 110
	err = __dev_addr_delete(&dev->mc_list, addr, alen, glbl);
	if (!err) {
		dev->mc_count--;
L
Linus Torvalds 已提交
111 112

		/*
113 114
		 *	We have altered the list, so the card
		 *	loaded filter is now wrong. Fix it
L
Linus Torvalds 已提交
115
		 */
116 117

		__dev_mc_upload(dev);
L
Linus Torvalds 已提交
118
	}
H
Herbert Xu 已提交
119
	netif_tx_unlock_bh(dev);
L
Linus Torvalds 已提交
120 121 122 123 124 125
	return err;
}

/*
 *	Add a device level multicast
 */
126

L
Linus Torvalds 已提交
127 128
int dev_mc_add(struct net_device *dev, void *addr, int alen, int glbl)
{
129
	int err;
L
Linus Torvalds 已提交
130

H
Herbert Xu 已提交
131
	netif_tx_lock_bh(dev);
132 133 134 135
	err = __dev_addr_add(&dev->mc_list, addr, alen, glbl);
	if (!err) {
		dev->mc_count++;
		__dev_mc_upload(dev);
L
Linus Torvalds 已提交
136
	}
H
Herbert Xu 已提交
137
	netif_tx_unlock_bh(dev);
L
Linus Torvalds 已提交
138 139 140 141 142 143 144 145 146
	return err;
}

/*
 *	Discard multicast list when a device is downed
 */

void dev_mc_discard(struct net_device *dev)
{
H
Herbert Xu 已提交
147
	netif_tx_lock_bh(dev);
148
	__dev_addr_discard(&dev->mc_list);
L
Linus Torvalds 已提交
149
	dev->mc_count = 0;
H
Herbert Xu 已提交
150
	netif_tx_unlock_bh(dev);
L
Linus Torvalds 已提交
151 152 153 154 155 156 157 158 159
}

#ifdef CONFIG_PROC_FS
static void *dev_mc_seq_start(struct seq_file *seq, loff_t *pos)
{
	struct net_device *dev;
	loff_t off = 0;

	read_lock(&dev_base_lock);
160
	for_each_netdev(dev) {
161
		if (off++ == *pos)
L
Linus Torvalds 已提交
162 163 164 165 166 167 168 169
			return dev;
	}
	return NULL;
}

static void *dev_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
	++*pos;
170
	return next_net_device((struct net_device *)v);
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178 179 180
}

static void dev_mc_seq_stop(struct seq_file *seq, void *v)
{
	read_unlock(&dev_base_lock);
}


static int dev_mc_seq_show(struct seq_file *seq, void *v)
{
181
	struct dev_addr_list *m;
L
Linus Torvalds 已提交
182 183
	struct net_device *dev = v;

H
Herbert Xu 已提交
184
	netif_tx_lock_bh(dev);
L
Linus Torvalds 已提交
185 186 187 188 189 190 191 192 193 194 195
	for (m = dev->mc_list; m; m = m->next) {
		int i;

		seq_printf(seq, "%-4d %-15s %-5d %-5d ", dev->ifindex,
			   dev->name, m->dmi_users, m->dmi_gusers);

		for (i = 0; i < m->dmi_addrlen; i++)
			seq_printf(seq, "%02x", m->dmi_addr[i]);

		seq_putc(seq, '\n');
	}
H
Herbert Xu 已提交
196
	netif_tx_unlock_bh(dev);
L
Linus Torvalds 已提交
197 198 199
	return 0;
}

200
static const struct seq_operations dev_mc_seq_ops = {
L
Linus Torvalds 已提交
201 202 203 204 205 206 207 208 209 210 211
	.start = dev_mc_seq_start,
	.next  = dev_mc_seq_next,
	.stop  = dev_mc_seq_stop,
	.show  = dev_mc_seq_show,
};

static int dev_mc_seq_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &dev_mc_seq_ops);
}

212
static const struct file_operations dev_mc_seq_fops = {
L
Linus Torvalds 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	.owner	 = THIS_MODULE,
	.open    = dev_mc_seq_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release,
};

#endif

void __init dev_mcast_init(void)
{
	proc_net_fops_create("dev_mcast", 0, &dev_mc_seq_fops);
}

EXPORT_SYMBOL(dev_mc_add);
EXPORT_SYMBOL(dev_mc_delete);