dev_mcast.c 5.8 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
#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>
44
#include <net/net_namespace.h>
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52
#include <net/ip.h>
#include <net/route.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <net/arp.h>


/*
53
 *	Device multicast list maintenance.
L
Linus Torvalds 已提交
54
 *
55 56 57
 *	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 已提交
58 59 60 61 62 63
 *	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 已提交
64
 *	We block accesses to device mc filters with netif_tx_lock.
L
Linus Torvalds 已提交
65 66 67 68 69
 */

/*
 *	Delete a device level multicast
 */
70

L
Linus Torvalds 已提交
71 72
int dev_mc_delete(struct net_device *dev, void *addr, int alen, int glbl)
{
73
	int err;
L
Linus Torvalds 已提交
74

H
Herbert Xu 已提交
75
	netif_tx_lock_bh(dev);
76
	netif_addr_lock(dev);
77 78
	err = __dev_addr_delete(&dev->mc_list, &dev->mc_count,
				addr, alen, glbl);
79
	if (!err) {
L
Linus Torvalds 已提交
80
		/*
81 82
		 *	We have altered the list, so the card
		 *	loaded filter is now wrong. Fix it
L
Linus Torvalds 已提交
83
		 */
84

85
		__dev_set_rx_mode(dev);
L
Linus Torvalds 已提交
86
	}
87
	netif_addr_unlock(dev);
H
Herbert Xu 已提交
88
	netif_tx_unlock_bh(dev);
L
Linus Torvalds 已提交
89 90 91 92 93 94
	return err;
}

/*
 *	Add a device level multicast
 */
95

L
Linus Torvalds 已提交
96 97
int dev_mc_add(struct net_device *dev, void *addr, int alen, int glbl)
{
98
	int err;
L
Linus Torvalds 已提交
99

H
Herbert Xu 已提交
100
	netif_tx_lock_bh(dev);
101
	netif_addr_lock(dev);
102 103
	err = __dev_addr_add(&dev->mc_list, &dev->mc_count, addr, alen, glbl);
	if (!err)
104
		__dev_set_rx_mode(dev);
105
	netif_addr_unlock(dev);
H
Herbert Xu 已提交
106
	netif_tx_unlock_bh(dev);
L
Linus Torvalds 已提交
107 108 109
	return err;
}

110 111 112 113 114 115 116 117 118 119
/**
 *	dev_mc_sync	- Synchronize device's multicast list to another device
 *	@to: destination device
 *	@from: source device
 *
 * 	Add newly added addresses to the destination device and release
 * 	addresses that have no users left. The source device must be
 * 	locked by netif_tx_lock_bh.
 *
 *	This function is intended to be called from the dev->set_multicast_list
120
 *	or dev->set_rx_mode function of layered software devices.
121 122 123 124 125 126
 */
int dev_mc_sync(struct net_device *to, struct net_device *from)
{
	int err = 0;

	netif_tx_lock_bh(to);
127
	netif_addr_lock(to);
128 129
	err = __dev_addr_sync(&to->mc_list, &to->mc_count,
			      &from->mc_list, &from->mc_count);
130 131
	if (!err)
		__dev_set_rx_mode(to);
132
	netif_addr_unlock(to);
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	netif_tx_unlock_bh(to);

	return err;
}
EXPORT_SYMBOL(dev_mc_sync);


/**
 * 	dev_mc_unsync	- Remove synchronized addresses from the destination
 * 			  device
 *	@to: destination device
 *	@from: source device
 *
 * 	Remove all addresses that were added to the destination device by
 * 	dev_mc_sync(). This function is intended to be called from the
 * 	dev->stop function of layered software devices.
 */
void dev_mc_unsync(struct net_device *to, struct net_device *from)
{
	netif_tx_lock_bh(from);
153
	netif_addr_lock(from);
154
	netif_tx_lock_bh(to);
155
	netif_addr_lock(to);
156

157 158
	__dev_addr_unsync(&to->mc_list, &to->mc_count,
			  &from->mc_list, &from->mc_count);
159 160
	__dev_set_rx_mode(to);

161
	netif_addr_unlock(to);
162
	netif_tx_unlock_bh(to);
163
	netif_addr_unlock(from);
164 165 166 167
	netif_tx_unlock_bh(from);
}
EXPORT_SYMBOL(dev_mc_unsync);

L
Linus Torvalds 已提交
168 169 170
#ifdef CONFIG_PROC_FS
static int dev_mc_seq_show(struct seq_file *seq, void *v)
{
171
	struct dev_addr_list *m;
L
Linus Torvalds 已提交
172 173
	struct net_device *dev = v;

174 175 176
	if (v == SEQ_START_TOKEN)
		return 0;

H
Herbert Xu 已提交
177
	netif_tx_lock_bh(dev);
178
	netif_addr_lock(dev);
L
Linus Torvalds 已提交
179 180 181 182 183 184 185 186 187 188 189
	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');
	}
190
	netif_addr_unlock(dev);
H
Herbert Xu 已提交
191
	netif_tx_unlock_bh(dev);
L
Linus Torvalds 已提交
192 193 194
	return 0;
}

195
static const struct seq_operations dev_mc_seq_ops = {
196 197 198
	.start = dev_seq_start,
	.next  = dev_seq_next,
	.stop  = dev_seq_stop,
L
Linus Torvalds 已提交
199 200 201 202 203
	.show  = dev_mc_seq_show,
};

static int dev_mc_seq_open(struct inode *inode, struct file *file)
{
204 205
	return seq_open_net(inode, file, &dev_mc_seq_ops,
			    sizeof(struct seq_net_private));
L
Linus Torvalds 已提交
206 207
}

208
static const struct file_operations dev_mc_seq_fops = {
L
Linus Torvalds 已提交
209 210 211 212
	.owner	 = THIS_MODULE,
	.open    = dev_mc_seq_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
213
	.release = seq_release_net,
L
Linus Torvalds 已提交
214 215 216 217
};

#endif

218
static int __net_init dev_mc_net_init(struct net *net)
219 220 221 222 223 224
{
	if (!proc_net_fops_create(net, "dev_mcast", 0, &dev_mc_seq_fops))
		return -ENOMEM;
	return 0;
}

225
static void __net_exit dev_mc_net_exit(struct net *net)
226 227 228 229
{
	proc_net_remove(net, "dev_mcast");
}

230
static struct pernet_operations __net_initdata dev_mc_net_ops = {
231 232 233 234
	.init = dev_mc_net_init,
	.exit = dev_mc_net_exit,
};

L
Linus Torvalds 已提交
235 236
void __init dev_mcast_init(void)
{
237
	register_pernet_subsys(&dev_mc_net_ops);
L
Linus Torvalds 已提交
238 239 240 241
}

EXPORT_SYMBOL(dev_mc_add);
EXPORT_SYMBOL(dev_mc_delete);