mcast.c 69.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *	Multicast support for IPv6
3
 *	Linux INET6 implementation
L
Linus Torvalds 已提交
4 5
 *
 *	Authors:
6
 *	Pedro Roque		<roque@di.fc.ul.pt>
L
Linus Torvalds 已提交
7
 *
8
 *	Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 *
 *	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.
 */

/* Changes:
 *
 *	yoshfuji	: fix format of router-alert option
 *	YOSHIFUJI Hideaki @USAGI:
 *		Fixed source address for MLD message based on
 *		<draft-ietf-magma-mld-source-05.txt>.
 *	YOSHIFUJI Hideaki @USAGI:
 *		- Ignore Queries for invalid addresses.
 *		- MLD for link-local addresses.
 *	David L Stevens <dlstevens@us.ibm.com>:
 *		- MLDv2 support
 */

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/jiffies.h>
#include <linux/times.h>
#include <linux/net.h>
#include <linux/in.h>
#include <linux/in6.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/route.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
46
#include <linux/slab.h>
47
#include <linux/pkt_sched.h>
48
#include <net/mld.h>
L
Linus Torvalds 已提交
49 50 51 52

#include <linux/netfilter.h>
#include <linux/netfilter_ipv6.h>

53
#include <net/net_namespace.h>
L
Linus Torvalds 已提交
54 55 56 57 58 59 60 61 62
#include <net/sock.h>
#include <net/snmp.h>

#include <net/ipv6.h>
#include <net/protocol.h>
#include <net/if_inet6.h>
#include <net/ndisc.h>
#include <net/addrconf.h>
#include <net/ip6_route.h>
63
#include <net/inet_common.h>
L
Linus Torvalds 已提交
64 65 66

#include <net/ip6_checksum.h>

67 68 69 70 71
/* Ensure that we have struct in6_addr aligned on 32bit word. */
static void *__mld2_query_bugs[] __attribute__((__unused__)) = {
	BUILD_BUG_ON_NULL(offsetof(struct mld2_query, mld2q_srcs) % 4),
	BUILD_BUG_ON_NULL(offsetof(struct mld2_report, mld2r_grec) % 4),
	BUILD_BUG_ON_NULL(offsetof(struct mld2_grec, grec_mca) % 4)
L
Linus Torvalds 已提交
72 73 74 75 76 77 78 79 80 81 82 83
};

static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;

static void igmp6_join_group(struct ifmcaddr6 *ma);
static void igmp6_leave_group(struct ifmcaddr6 *ma);
static void igmp6_timer_handler(unsigned long data);

static void mld_gq_timer_expire(unsigned long data);
static void mld_ifc_timer_expire(unsigned long data);
static void mld_ifc_event(struct inet6_dev *idev);
static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
84
static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *addr);
L
Linus Torvalds 已提交
85
static void mld_clear_delrec(struct inet6_dev *idev);
86
static bool mld_in_v1_mode(const struct inet6_dev *idev);
L
Linus Torvalds 已提交
87 88 89
static int sf_setstate(struct ifmcaddr6 *pmc);
static void sf_markstate(struct ifmcaddr6 *pmc);
static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
90 91
static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
			  int sfmode, int sfcount, const struct in6_addr *psfsrc,
L
Linus Torvalds 已提交
92
			  int delta);
93 94
static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
			  int sfmode, int sfcount, const struct in6_addr *psfsrc,
L
Linus Torvalds 已提交
95 96 97 98 99
			  int delta);
static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
			    struct inet6_dev *idev);

#define MLD_QRV_DEFAULT		2
100 101 102 103
/* RFC3810, 9.2. Query Interval */
#define MLD_QI_DEFAULT		(125 * HZ)
/* RFC3810, 9.3. Query Response Interval */
#define MLD_QRI_DEFAULT		(10 * HZ)
L
Linus Torvalds 已提交
104

105 106 107 108
/* RFC3810, 8.1 Query Version Distinctions */
#define MLD_V1_QUERY_LEN	24
#define MLD_V2_QUERY_LEN_MIN	28

109
#define IPV6_MLD_MAX_MSF	64
L
Linus Torvalds 已提交
110

111
int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
112
int sysctl_mld_qrv __read_mostly = MLD_QRV_DEFAULT;
L
Linus Torvalds 已提交
113 114 115 116 117

/*
 *	socket join on multicast group
 */

E
Eric Dumazet 已提交
118 119 120 121 122
#define for_each_pmc_rcu(np, pmc)				\
	for (pmc = rcu_dereference(np->ipv6_mc_list);		\
	     pmc != NULL;					\
	     pmc = rcu_dereference(pmc->next))

123 124 125 126
static int unsolicited_report_interval(struct inet6_dev *idev)
{
	int iv;

127
	if (mld_in_v1_mode(idev))
128 129 130 131 132 133 134
		iv = idev->cnf.mldv1_unsolicited_report_interval;
	else
		iv = idev->cnf.mldv2_unsolicited_report_interval;

	return iv > 0 ? iv : 1;
}

135
int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
L
Linus Torvalds 已提交
136 137 138 139
{
	struct net_device *dev = NULL;
	struct ipv6_mc_socklist *mc_lst;
	struct ipv6_pinfo *np = inet6_sk(sk);
140
	struct net *net = sock_net(sk);
L
Linus Torvalds 已提交
141 142
	int err;

143 144
	ASSERT_RTNL();

L
Linus Torvalds 已提交
145 146 147
	if (!ipv6_addr_is_multicast(addr))
		return -EINVAL;

E
Eric Dumazet 已提交
148 149
	rcu_read_lock();
	for_each_pmc_rcu(np, mc_lst) {
150 151
		if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
		    ipv6_addr_equal(&mc_lst->addr, addr)) {
E
Eric Dumazet 已提交
152
			rcu_read_unlock();
153 154 155
			return -EADDRINUSE;
		}
	}
E
Eric Dumazet 已提交
156
	rcu_read_unlock();
157

L
Linus Torvalds 已提交
158 159
	mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);

160
	if (!mc_lst)
L
Linus Torvalds 已提交
161 162 163
		return -ENOMEM;

	mc_lst->next = NULL;
A
Alexey Dobriyan 已提交
164
	mc_lst->addr = *addr;
L
Linus Torvalds 已提交
165 166 167

	if (ifindex == 0) {
		struct rt6_info *rt;
168
		rt = rt6_lookup(net, addr, NULL, 0, 0);
L
Linus Torvalds 已提交
169
		if (rt) {
170
			dev = rt->dst.dev;
A
Amerigo Wang 已提交
171
			ip6_rt_put(rt);
L
Linus Torvalds 已提交
172 173
		}
	} else
174
		dev = __dev_get_by_index(net, ifindex);
L
Linus Torvalds 已提交
175

176
	if (!dev) {
L
Linus Torvalds 已提交
177 178 179 180 181 182
		sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
		return -ENODEV;
	}

	mc_lst->ifindex = dev->ifindex;
	mc_lst->sfmode = MCAST_EXCLUDE;
183
	rwlock_init(&mc_lst->sflock);
L
Linus Torvalds 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197
	mc_lst->sflist = NULL;

	/*
	 *	now add/increase the group membership on the device
	 */

	err = ipv6_dev_mc_inc(dev, addr);

	if (err) {
		sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
		return err;
	}

	mc_lst->next = np->ipv6_mc_list;
E
Eric Dumazet 已提交
198
	rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
L
Linus Torvalds 已提交
199

200 201 202
	return 0;
}
EXPORT_SYMBOL(ipv6_sock_mc_join);
L
Linus Torvalds 已提交
203 204 205 206

/*
 *	socket leave on multicast group
 */
207
int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
L
Linus Torvalds 已提交
208 209
{
	struct ipv6_pinfo *np = inet6_sk(sk);
E
Eric Dumazet 已提交
210 211
	struct ipv6_mc_socklist *mc_lst;
	struct ipv6_mc_socklist __rcu **lnk;
212
	struct net *net = sock_net(sk);
L
Linus Torvalds 已提交
213

214 215
	ASSERT_RTNL();

216 217 218
	if (!ipv6_addr_is_multicast(addr))
		return -EINVAL;

E
Eric Dumazet 已提交
219
	for (lnk = &np->ipv6_mc_list;
W
WANG Cong 已提交
220
	     (mc_lst = rtnl_dereference(*lnk)) != NULL;
E
Eric Dumazet 已提交
221
	      lnk = &mc_lst->next) {
L
Linus Torvalds 已提交
222 223 224 225 226 227
		if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
		    ipv6_addr_equal(&mc_lst->addr, addr)) {
			struct net_device *dev;

			*lnk = mc_lst->next;

228
			dev = __dev_get_by_index(net, mc_lst->ifindex);
229
			if (dev != NULL) {
E
Eric Dumazet 已提交
230
				struct inet6_dev *idev = __in6_dev_get(dev);
L
Linus Torvalds 已提交
231

232
				(void) ip6_mc_leave_src(sk, mc_lst, idev);
E
Eric Dumazet 已提交
233
				if (idev)
L
Linus Torvalds 已提交
234
					__ipv6_dev_mc_dec(idev, &mc_lst->addr);
235 236
			} else
				(void) ip6_mc_leave_src(sk, mc_lst, NULL);
237

E
Eric Dumazet 已提交
238
			atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
239
			kfree_rcu(mc_lst, rcu);
L
Linus Torvalds 已提交
240 241 242 243
			return 0;
		}
	}

244
	return -EADDRNOTAVAIL;
L
Linus Torvalds 已提交
245
}
246
EXPORT_SYMBOL(ipv6_sock_mc_drop);
L
Linus Torvalds 已提交
247

E
Eric Dumazet 已提交
248 249
/* called with rcu_read_lock() */
static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
250
					     const struct in6_addr *group,
E
Eric Dumazet 已提交
251
					     int ifindex)
L
Linus Torvalds 已提交
252 253 254 255 256
{
	struct net_device *dev = NULL;
	struct inet6_dev *idev = NULL;

	if (ifindex == 0) {
E
Eric Dumazet 已提交
257
		struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, 0);
L
Linus Torvalds 已提交
258 259

		if (rt) {
260
			dev = rt->dst.dev;
A
Amerigo Wang 已提交
261
			ip6_rt_put(rt);
L
Linus Torvalds 已提交
262 263
		}
	} else
E
Eric Dumazet 已提交
264
		dev = dev_get_by_index_rcu(net, ifindex);
L
Linus Torvalds 已提交
265 266

	if (!dev)
E
Eric Dumazet 已提交
267 268
		return NULL;
	idev = __in6_dev_get(dev);
269
	if (!idev)
270
		return NULL;
L
Linus Torvalds 已提交
271
	read_lock_bh(&idev->lock);
E
Eric Dumazet 已提交
272 273 274 275
	if (idev->dead) {
		read_unlock_bh(&idev->lock);
		return NULL;
	}
L
Linus Torvalds 已提交
276 277 278 279 280 281 282
	return idev;
}

void ipv6_sock_mc_close(struct sock *sk)
{
	struct ipv6_pinfo *np = inet6_sk(sk);
	struct ipv6_mc_socklist *mc_lst;
283
	struct net *net = sock_net(sk);
L
Linus Torvalds 已提交
284

285 286 287
	if (!rcu_access_pointer(np->ipv6_mc_list))
		return;

288
	rtnl_lock();
W
WANG Cong 已提交
289
	while ((mc_lst = rtnl_dereference(np->ipv6_mc_list)) != NULL) {
L
Linus Torvalds 已提交
290 291 292 293
		struct net_device *dev;

		np->ipv6_mc_list = mc_lst->next;

294
		dev = __dev_get_by_index(net, mc_lst->ifindex);
L
Linus Torvalds 已提交
295
		if (dev) {
E
Eric Dumazet 已提交
296
			struct inet6_dev *idev = __in6_dev_get(dev);
L
Linus Torvalds 已提交
297

298
			(void) ip6_mc_leave_src(sk, mc_lst, idev);
E
Eric Dumazet 已提交
299
			if (idev)
L
Linus Torvalds 已提交
300
				__ipv6_dev_mc_dec(idev, &mc_lst->addr);
301 302
		} else
			(void) ip6_mc_leave_src(sk, mc_lst, NULL);
L
Linus Torvalds 已提交
303

E
Eric Dumazet 已提交
304
		atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
305
		kfree_rcu(mc_lst, rcu);
E
Eric Dumazet 已提交
306

L
Linus Torvalds 已提交
307
	}
308
	rtnl_unlock();
L
Linus Torvalds 已提交
309 310 311 312 313 314 315 316 317 318
}

int ip6_mc_source(int add, int omode, struct sock *sk,
	struct group_source_req *pgsr)
{
	struct in6_addr *source, *group;
	struct ipv6_mc_socklist *pmc;
	struct inet6_dev *idev;
	struct ipv6_pinfo *inet6 = inet6_sk(sk);
	struct ip6_sf_socklist *psl;
319
	struct net *net = sock_net(sk);
L
Linus Torvalds 已提交
320
	int i, j, rv;
321
	int leavegroup = 0;
322
	int pmclocked = 0;
L
Linus Torvalds 已提交
323 324 325 326 327 328 329 330
	int err;

	source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
	group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;

	if (!ipv6_addr_is_multicast(group))
		return -EINVAL;

E
Eric Dumazet 已提交
331 332 333 334
	rcu_read_lock();
	idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface);
	if (!idev) {
		rcu_read_unlock();
L
Linus Torvalds 已提交
335
		return -ENODEV;
E
Eric Dumazet 已提交
336
	}
L
Linus Torvalds 已提交
337 338 339

	err = -EADDRNOTAVAIL;

E
Eric Dumazet 已提交
340
	for_each_pmc_rcu(inet6, pmc) {
L
Linus Torvalds 已提交
341 342 343 344 345
		if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
			continue;
		if (ipv6_addr_equal(&pmc->addr, group))
			break;
	}
346 347
	if (!pmc) {		/* must have a prior join */
		err = -EINVAL;
L
Linus Torvalds 已提交
348
		goto done;
349
	}
L
Linus Torvalds 已提交
350 351
	/* if a source filter was set, must be the same mode as before */
	if (pmc->sflist) {
352 353
		if (pmc->sfmode != omode) {
			err = -EINVAL;
L
Linus Torvalds 已提交
354
			goto done;
355
		}
L
Linus Torvalds 已提交
356 357 358 359 360 361 362
	} else if (pmc->sfmode != omode) {
		/* allow mode switches for empty-set filters */
		ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
		ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
		pmc->sfmode = omode;
	}

E
Eric Dumazet 已提交
363
	write_lock(&pmc->sflock);
364 365
	pmclocked = 1;

L
Linus Torvalds 已提交
366 367 368
	psl = pmc->sflist;
	if (!add) {
		if (!psl)
369
			goto done;	/* err = -EADDRNOTAVAIL */
L
Linus Torvalds 已提交
370
		rv = !0;
371
		for (i = 0; i < psl->sl_count; i++) {
372
			rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
L
Linus Torvalds 已提交
373 374 375 376
			if (rv == 0)
				break;
		}
		if (rv)		/* source not found */
377
			goto done;	/* err = -EADDRNOTAVAIL */
L
Linus Torvalds 已提交
378

379 380 381 382 383 384
		/* special case - (INCLUDE, empty) == LEAVE_GROUP */
		if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
			leavegroup = 1;
			goto done;
		}

L
Linus Torvalds 已提交
385 386 387
		/* update the interface filter */
		ip6_mc_del_src(idev, group, omode, 1, source, 1);

388
		for (j = i+1; j < psl->sl_count; j++)
L
Linus Torvalds 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
			psl->sl_addr[j-1] = psl->sl_addr[j];
		psl->sl_count--;
		err = 0;
		goto done;
	}
	/* else, add a new source to the filter */

	if (psl && psl->sl_count >= sysctl_mld_max_msf) {
		err = -ENOBUFS;
		goto done;
	}
	if (!psl || psl->sl_count == psl->sl_max) {
		struct ip6_sf_socklist *newpsl;
		int count = IP6_SFBLOCK;

		if (psl)
			count += psl->sl_max;
406
		newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
L
Linus Torvalds 已提交
407 408 409 410 411 412 413
		if (!newpsl) {
			err = -ENOBUFS;
			goto done;
		}
		newpsl->sl_max = count;
		newpsl->sl_count = count - IP6_SFBLOCK;
		if (psl) {
414
			for (i = 0; i < psl->sl_count; i++)
L
Linus Torvalds 已提交
415 416 417 418 419 420
				newpsl->sl_addr[i] = psl->sl_addr[i];
			sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
		}
		pmc->sflist = psl = newpsl;
	}
	rv = 1;	/* > 0 for insert logic below if sl_count is 0 */
421
	for (i = 0; i < psl->sl_count; i++) {
422
		rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
423 424
		if (rv == 0) /* There is an error in the address. */
			goto done;
L
Linus Torvalds 已提交
425
	}
426
	for (j = psl->sl_count-1; j >= i; j--)
L
Linus Torvalds 已提交
427 428 429 430 431 432 433
		psl->sl_addr[j+1] = psl->sl_addr[j];
	psl->sl_addr[i] = *source;
	psl->sl_count++;
	err = 0;
	/* update the interface list */
	ip6_mc_add_src(idev, group, omode, 1, source, 1);
done:
434
	if (pmclocked)
E
Eric Dumazet 已提交
435
		write_unlock(&pmc->sflock);
L
Linus Torvalds 已提交
436
	read_unlock_bh(&idev->lock);
E
Eric Dumazet 已提交
437
	rcu_read_unlock();
438
	if (leavegroup)
439
		err = ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
L
Linus Torvalds 已提交
440 441 442 443 444
	return err;
}

int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
{
445
	const struct in6_addr *group;
L
Linus Torvalds 已提交
446 447 448 449
	struct ipv6_mc_socklist *pmc;
	struct inet6_dev *idev;
	struct ipv6_pinfo *inet6 = inet6_sk(sk);
	struct ip6_sf_socklist *newpsl, *psl;
450
	struct net *net = sock_net(sk);
451
	int leavegroup = 0;
L
Linus Torvalds 已提交
452 453 454 455 456 457 458 459 460 461
	int i, err;

	group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;

	if (!ipv6_addr_is_multicast(group))
		return -EINVAL;
	if (gsf->gf_fmode != MCAST_INCLUDE &&
	    gsf->gf_fmode != MCAST_EXCLUDE)
		return -EINVAL;

E
Eric Dumazet 已提交
462 463
	rcu_read_lock();
	idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
L
Linus Torvalds 已提交
464

E
Eric Dumazet 已提交
465 466
	if (!idev) {
		rcu_read_unlock();
L
Linus Torvalds 已提交
467
		return -ENODEV;
E
Eric Dumazet 已提交
468
	}
L
Linus Torvalds 已提交
469

470
	err = 0;
471

472 473 474 475 476
	if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
		leavegroup = 1;
		goto done;
	}

E
Eric Dumazet 已提交
477
	for_each_pmc_rcu(inet6, pmc) {
L
Linus Torvalds 已提交
478 479 480 481 482
		if (pmc->ifindex != gsf->gf_interface)
			continue;
		if (ipv6_addr_equal(&pmc->addr, group))
			break;
	}
483 484
	if (!pmc) {		/* must have a prior join */
		err = -EINVAL;
L
Linus Torvalds 已提交
485
		goto done;
486
	}
L
Linus Torvalds 已提交
487
	if (gsf->gf_numsrc) {
488 489
		newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
							  GFP_ATOMIC);
L
Linus Torvalds 已提交
490 491 492 493 494
		if (!newpsl) {
			err = -ENOBUFS;
			goto done;
		}
		newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
495
		for (i = 0; i < newpsl->sl_count; ++i) {
L
Linus Torvalds 已提交
496 497 498 499 500 501 502 503 504 505 506
			struct sockaddr_in6 *psin6;

			psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
			newpsl->sl_addr[i] = psin6->sin6_addr;
		}
		err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
			newpsl->sl_count, newpsl->sl_addr, 0);
		if (err) {
			sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
			goto done;
		}
507
	} else {
L
Linus Torvalds 已提交
508
		newpsl = NULL;
509 510
		(void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
	}
511

E
Eric Dumazet 已提交
512
	write_lock(&pmc->sflock);
L
Linus Torvalds 已提交
513 514 515 516 517 518 519 520 521
	psl = pmc->sflist;
	if (psl) {
		(void) ip6_mc_del_src(idev, group, pmc->sfmode,
			psl->sl_count, psl->sl_addr, 0);
		sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
	} else
		(void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
	pmc->sflist = newpsl;
	pmc->sfmode = gsf->gf_fmode;
E
Eric Dumazet 已提交
522
	write_unlock(&pmc->sflock);
523
	err = 0;
L
Linus Torvalds 已提交
524 525
done:
	read_unlock_bh(&idev->lock);
E
Eric Dumazet 已提交
526
	rcu_read_unlock();
527 528
	if (leavegroup)
		err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
L
Linus Torvalds 已提交
529 530 531 532 533 534 535
	return err;
}

int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
	struct group_filter __user *optval, int __user *optlen)
{
	int err, i, count, copycount;
536
	const struct in6_addr *group;
L
Linus Torvalds 已提交
537 538 539 540
	struct ipv6_mc_socklist *pmc;
	struct inet6_dev *idev;
	struct ipv6_pinfo *inet6 = inet6_sk(sk);
	struct ip6_sf_socklist *psl;
541
	struct net *net = sock_net(sk);
L
Linus Torvalds 已提交
542 543 544 545 546 547

	group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;

	if (!ipv6_addr_is_multicast(group))
		return -EINVAL;

E
Eric Dumazet 已提交
548 549
	rcu_read_lock();
	idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
L
Linus Torvalds 已提交
550

E
Eric Dumazet 已提交
551 552
	if (!idev) {
		rcu_read_unlock();
L
Linus Torvalds 已提交
553
		return -ENODEV;
E
Eric Dumazet 已提交
554
	}
L
Linus Torvalds 已提交
555 556

	err = -EADDRNOTAVAIL;
W
WANG Cong 已提交
557 558
	/* changes to the ipv6_mc_list require the socket lock and
	 * rtnl lock. We have the socket lock and rcu read lock,
559 560
	 * so reading the list is safe.
	 */
L
Linus Torvalds 已提交
561

E
Eric Dumazet 已提交
562
	for_each_pmc_rcu(inet6, pmc) {
L
Linus Torvalds 已提交
563 564 565 566 567 568 569 570 571 572 573
		if (pmc->ifindex != gsf->gf_interface)
			continue;
		if (ipv6_addr_equal(group, &pmc->addr))
			break;
	}
	if (!pmc)		/* must have a prior join */
		goto done;
	gsf->gf_fmode = pmc->sfmode;
	psl = pmc->sflist;
	count = psl ? psl->sl_count : 0;
	read_unlock_bh(&idev->lock);
E
Eric Dumazet 已提交
574
	rcu_read_unlock();
L
Linus Torvalds 已提交
575 576 577 578 579 580 581

	copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
	gsf->gf_numsrc = count;
	if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
	    copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
		return -EFAULT;
	}
W
WANG Cong 已提交
582 583
	/* changes to psl require the socket lock, and a write lock
	 * on pmc->sflock. We have the socket lock so reading here is safe.
584
	 */
585
	for (i = 0; i < copycount; i++) {
L
Linus Torvalds 已提交
586 587 588 589 590 591 592
		struct sockaddr_in6 *psin6;
		struct sockaddr_storage ss;

		psin6 = (struct sockaddr_in6 *)&ss;
		memset(&ss, 0, sizeof(ss));
		psin6->sin6_family = AF_INET6;
		psin6->sin6_addr = psl->sl_addr[i];
593
		if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
L
Linus Torvalds 已提交
594 595 596 597 598
			return -EFAULT;
	}
	return 0;
done:
	read_unlock_bh(&idev->lock);
E
Eric Dumazet 已提交
599
	rcu_read_unlock();
L
Linus Torvalds 已提交
600 601 602
	return err;
}

603 604
bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
		    const struct in6_addr *src_addr)
L
Linus Torvalds 已提交
605 606 607 608
{
	struct ipv6_pinfo *np = inet6_sk(sk);
	struct ipv6_mc_socklist *mc;
	struct ip6_sf_socklist *psl;
609
	bool rv = true;
L
Linus Torvalds 已提交
610

E
Eric Dumazet 已提交
611 612
	rcu_read_lock();
	for_each_pmc_rcu(np, mc) {
L
Linus Torvalds 已提交
613 614 615 616
		if (ipv6_addr_equal(&mc->addr, mc_addr))
			break;
	}
	if (!mc) {
E
Eric Dumazet 已提交
617
		rcu_read_unlock();
618
		return true;
L
Linus Torvalds 已提交
619
	}
620
	read_lock(&mc->sflock);
L
Linus Torvalds 已提交
621 622 623 624 625 626
	psl = mc->sflist;
	if (!psl) {
		rv = mc->sfmode == MCAST_EXCLUDE;
	} else {
		int i;

627
		for (i = 0; i < psl->sl_count; i++) {
L
Linus Torvalds 已提交
628 629 630 631
			if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
				break;
		}
		if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
632
			rv = false;
L
Linus Torvalds 已提交
633
		if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
634
			rv = false;
L
Linus Torvalds 已提交
635
	}
636
	read_unlock(&mc->sflock);
E
Eric Dumazet 已提交
637
	rcu_read_unlock();
L
Linus Torvalds 已提交
638 639 640 641 642 643 644 645 646

	return rv;
}

static void igmp6_group_added(struct ifmcaddr6 *mc)
{
	struct net_device *dev = mc->idev->dev;
	char buf[MAX_ADDR_LEN];

647 648 649 650
	if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
	    IPV6_ADDR_SCOPE_LINKLOCAL)
		return;

L
Linus Torvalds 已提交
651 652 653 654
	spin_lock_bh(&mc->mca_lock);
	if (!(mc->mca_flags&MAF_LOADED)) {
		mc->mca_flags |= MAF_LOADED;
		if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
655
			dev_mc_add(dev, buf);
L
Linus Torvalds 已提交
656 657 658 659 660 661
	}
	spin_unlock_bh(&mc->mca_lock);

	if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
		return;

662
	if (mld_in_v1_mode(mc->idev)) {
L
Linus Torvalds 已提交
663 664 665 666 667 668 669 670 671 672 673 674 675 676
		igmp6_join_group(mc);
		return;
	}
	/* else v2 */

	mc->mca_crcount = mc->idev->mc_qrv;
	mld_ifc_event(mc->idev);
}

static void igmp6_group_dropped(struct ifmcaddr6 *mc)
{
	struct net_device *dev = mc->idev->dev;
	char buf[MAX_ADDR_LEN];

677 678 679 680
	if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
	    IPV6_ADDR_SCOPE_LINKLOCAL)
		return;

L
Linus Torvalds 已提交
681 682 683 684
	spin_lock_bh(&mc->mca_lock);
	if (mc->mca_flags&MAF_LOADED) {
		mc->mca_flags &= ~MAF_LOADED;
		if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
685
			dev_mc_del(dev, buf);
L
Linus Torvalds 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
	}

	if (mc->mca_flags & MAF_NOREPORT)
		goto done;
	spin_unlock_bh(&mc->mca_lock);

	if (!mc->idev->dead)
		igmp6_leave_group(mc);

	spin_lock_bh(&mc->mca_lock);
	if (del_timer(&mc->mca_timer))
		atomic_dec(&mc->mca_refcnt);
done:
	ip6_mc_clear_src(mc);
	spin_unlock_bh(&mc->mca_lock);
}

/*
 * deleted ifmcaddr6 manipulation
 */
static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
{
	struct ifmcaddr6 *pmc;

	/* this is an "ifmcaddr6" for convenience; only the fields below
	 * are actually used. In particular, the refcnt and users are not
	 * used for management of the delete list. Using the same structure
	 * for deleted items allows change reports to use common code with
	 * non-deleted or query-response MCA's.
	 */
716
	pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
L
Linus Torvalds 已提交
717 718
	if (!pmc)
		return;
719

L
Linus Torvalds 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732
	spin_lock_bh(&im->mca_lock);
	spin_lock_init(&pmc->mca_lock);
	pmc->idev = im->idev;
	in6_dev_hold(idev);
	pmc->mca_addr = im->mca_addr;
	pmc->mca_crcount = idev->mc_qrv;
	pmc->mca_sfmode = im->mca_sfmode;
	if (pmc->mca_sfmode == MCAST_INCLUDE) {
		struct ip6_sf_list *psf;

		pmc->mca_tomb = im->mca_tomb;
		pmc->mca_sources = im->mca_sources;
		im->mca_tomb = im->mca_sources = NULL;
733
		for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
L
Linus Torvalds 已提交
734 735 736 737
			psf->sf_crcount = pmc->mca_crcount;
	}
	spin_unlock_bh(&im->mca_lock);

738
	spin_lock_bh(&idev->mc_lock);
L
Linus Torvalds 已提交
739 740
	pmc->next = idev->mc_tomb;
	idev->mc_tomb = pmc;
741
	spin_unlock_bh(&idev->mc_lock);
L
Linus Torvalds 已提交
742 743
}

744
static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *pmca)
L
Linus Torvalds 已提交
745 746 747 748
{
	struct ifmcaddr6 *pmc, *pmc_prev;
	struct ip6_sf_list *psf, *psf_next;

749
	spin_lock_bh(&idev->mc_lock);
L
Linus Torvalds 已提交
750
	pmc_prev = NULL;
751
	for (pmc = idev->mc_tomb; pmc; pmc = pmc->next) {
L
Linus Torvalds 已提交
752 753 754 755 756 757 758 759 760 761
		if (ipv6_addr_equal(&pmc->mca_addr, pmca))
			break;
		pmc_prev = pmc;
	}
	if (pmc) {
		if (pmc_prev)
			pmc_prev->next = pmc->next;
		else
			idev->mc_tomb = pmc->next;
	}
762 763
	spin_unlock_bh(&idev->mc_lock);

L
Linus Torvalds 已提交
764
	if (pmc) {
765
		for (psf = pmc->mca_tomb; psf; psf = psf_next) {
L
Linus Torvalds 已提交
766 767 768 769 770 771 772 773 774 775 776 777
			psf_next = psf->sf_next;
			kfree(psf);
		}
		in6_dev_put(pmc->idev);
		kfree(pmc);
	}
}

static void mld_clear_delrec(struct inet6_dev *idev)
{
	struct ifmcaddr6 *pmc, *nextpmc;

778
	spin_lock_bh(&idev->mc_lock);
L
Linus Torvalds 已提交
779 780
	pmc = idev->mc_tomb;
	idev->mc_tomb = NULL;
781
	spin_unlock_bh(&idev->mc_lock);
L
Linus Torvalds 已提交
782 783 784 785 786 787 788 789 790 791

	for (; pmc; pmc = nextpmc) {
		nextpmc = pmc->next;
		ip6_mc_clear_src(pmc);
		in6_dev_put(pmc->idev);
		kfree(pmc);
	}

	/* clear dead sources, too */
	read_lock_bh(&idev->lock);
792
	for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
L
Linus Torvalds 已提交
793 794 795 796 797 798
		struct ip6_sf_list *psf, *psf_next;

		spin_lock_bh(&pmc->mca_lock);
		psf = pmc->mca_tomb;
		pmc->mca_tomb = NULL;
		spin_unlock_bh(&pmc->mca_lock);
799
		for (; psf; psf = psf_next) {
L
Linus Torvalds 已提交
800 801 802 803 804 805 806
			psf_next = psf->sf_next;
			kfree(psf);
		}
	}
	read_unlock_bh(&idev->lock);
}

W
WANG Cong 已提交
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
static void mca_get(struct ifmcaddr6 *mc)
{
	atomic_inc(&mc->mca_refcnt);
}

static void ma_put(struct ifmcaddr6 *mc)
{
	if (atomic_dec_and_test(&mc->mca_refcnt)) {
		in6_dev_put(mc->idev);
		kfree(mc);
	}
}

static struct ifmcaddr6 *mca_alloc(struct inet6_dev *idev,
				   const struct in6_addr *addr)
{
	struct ifmcaddr6 *mc;

	mc = kzalloc(sizeof(*mc), GFP_ATOMIC);
826
	if (!mc)
W
WANG Cong 已提交
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
		return NULL;

	setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);

	mc->mca_addr = *addr;
	mc->idev = idev; /* reference taken by caller */
	mc->mca_users = 1;
	/* mca_stamp should be updated upon changes */
	mc->mca_cstamp = mc->mca_tstamp = jiffies;
	atomic_set(&mc->mca_refcnt, 1);
	spin_lock_init(&mc->mca_lock);

	/* initial mode is (EX, empty) */
	mc->mca_sfmode = MCAST_EXCLUDE;
	mc->mca_sfcount[MCAST_EXCLUDE] = 1;

	if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
	    IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
		mc->mca_flags |= MAF_NOREPORT;

	return mc;
}
L
Linus Torvalds 已提交
849 850 851 852

/*
 *	device multicast group inc (add if not found)
 */
853
int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
L
Linus Torvalds 已提交
854 855 856 857
{
	struct ifmcaddr6 *mc;
	struct inet6_dev *idev;

858 859
	ASSERT_RTNL();

E
Eric Dumazet 已提交
860
	/* we need to take a reference on idev */
L
Linus Torvalds 已提交
861 862
	idev = in6_dev_get(dev);

863
	if (!idev)
L
Linus Torvalds 已提交
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
		return -EINVAL;

	write_lock_bh(&idev->lock);
	if (idev->dead) {
		write_unlock_bh(&idev->lock);
		in6_dev_put(idev);
		return -ENODEV;
	}

	for (mc = idev->mc_list; mc; mc = mc->next) {
		if (ipv6_addr_equal(&mc->mca_addr, addr)) {
			mc->mca_users++;
			write_unlock_bh(&idev->lock);
			ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
				NULL, 0);
			in6_dev_put(idev);
			return 0;
		}
	}

W
WANG Cong 已提交
884 885
	mc = mca_alloc(idev, addr);
	if (!mc) {
L
Linus Torvalds 已提交
886 887 888 889 890 891 892
		write_unlock_bh(&idev->lock);
		in6_dev_put(idev);
		return -ENOMEM;
	}

	mc->next = idev->mc_list;
	idev->mc_list = mc;
W
WANG Cong 已提交
893 894 895 896 897

	/* Hold this for the code below before we unlock,
	 * it is already exposed via idev->mc_list.
	 */
	mca_get(mc);
L
Linus Torvalds 已提交
898 899 900 901 902 903 904 905 906 907 908
	write_unlock_bh(&idev->lock);

	mld_del_delrec(idev, &mc->mca_addr);
	igmp6_group_added(mc);
	ma_put(mc);
	return 0;
}

/*
 *	device multicast group del
 */
909
int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
L
Linus Torvalds 已提交
910 911 912
{
	struct ifmcaddr6 *ma, **map;

913 914
	ASSERT_RTNL();

L
Linus Torvalds 已提交
915
	write_lock_bh(&idev->lock);
916
	for (map = &idev->mc_list; (ma = *map) != NULL; map = &ma->next) {
L
Linus Torvalds 已提交
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
		if (ipv6_addr_equal(&ma->mca_addr, addr)) {
			if (--ma->mca_users == 0) {
				*map = ma->next;
				write_unlock_bh(&idev->lock);

				igmp6_group_dropped(ma);

				ma_put(ma);
				return 0;
			}
			write_unlock_bh(&idev->lock);
			return 0;
		}
	}
	write_unlock_bh(&idev->lock);

	return -ENOENT;
}

936
int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
L
Linus Torvalds 已提交
937
{
E
Eric Dumazet 已提交
938
	struct inet6_dev *idev;
L
Linus Torvalds 已提交
939 940
	int err;

941
	ASSERT_RTNL();
L
Linus Torvalds 已提交
942

E
Eric Dumazet 已提交
943 944 945 946 947
	idev = __in6_dev_get(dev);
	if (!idev)
		err = -ENODEV;
	else
		err = __ipv6_dev_mc_dec(idev, addr);
L
Linus Torvalds 已提交
948 949 950 951 952 953 954

	return err;
}

/*
 *	check if the interface/address pair is valid
 */
955 956
bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
			 const struct in6_addr *src_addr)
L
Linus Torvalds 已提交
957 958 959
{
	struct inet6_dev *idev;
	struct ifmcaddr6 *mc;
960
	bool rv = false;
L
Linus Torvalds 已提交
961

E
Eric Dumazet 已提交
962 963
	rcu_read_lock();
	idev = __in6_dev_get(dev);
L
Linus Torvalds 已提交
964 965
	if (idev) {
		read_lock_bh(&idev->lock);
966
		for (mc = idev->mc_list; mc; mc = mc->next) {
L
Linus Torvalds 已提交
967 968 969 970 971 972 973 974
			if (ipv6_addr_equal(&mc->mca_addr, group))
				break;
		}
		if (mc) {
			if (src_addr && !ipv6_addr_any(src_addr)) {
				struct ip6_sf_list *psf;

				spin_lock_bh(&mc->mca_lock);
975
				for (psf = mc->mca_sources; psf; psf = psf->sf_next) {
L
Linus Torvalds 已提交
976 977 978 979 980 981 982 983
					if (ipv6_addr_equal(&psf->sf_addr, src_addr))
						break;
				}
				if (psf)
					rv = psf->sf_count[MCAST_INCLUDE] ||
						psf->sf_count[MCAST_EXCLUDE] !=
						mc->mca_sfcount[MCAST_EXCLUDE];
				else
984
					rv = mc->mca_sfcount[MCAST_EXCLUDE] != 0;
L
Linus Torvalds 已提交
985 986
				spin_unlock_bh(&mc->mca_lock);
			} else
987
				rv = true; /* don't filter unspecified source */
L
Linus Torvalds 已提交
988 989 990
		}
		read_unlock_bh(&idev->lock);
	}
E
Eric Dumazet 已提交
991
	rcu_read_unlock();
L
Linus Torvalds 已提交
992 993 994 995 996
	return rv;
}

static void mld_gq_start_timer(struct inet6_dev *idev)
{
997
	unsigned long tv = prandom_u32() % idev->mc_maxdelay;
L
Linus Torvalds 已提交
998 999 1000 1001 1002 1003

	idev->mc_gq_running = 1;
	if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
		in6_dev_hold(idev);
}

1004 1005 1006 1007 1008 1009 1010
static void mld_gq_stop_timer(struct inet6_dev *idev)
{
	idev->mc_gq_running = 0;
	if (del_timer(&idev->mc_gq_timer))
		__in6_dev_put(idev);
}

1011
static void mld_ifc_start_timer(struct inet6_dev *idev, unsigned long delay)
L
Linus Torvalds 已提交
1012
{
1013
	unsigned long tv = prandom_u32() % delay;
L
Linus Torvalds 已提交
1014 1015 1016 1017 1018

	if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
		in6_dev_hold(idev);
}

1019 1020 1021 1022 1023 1024 1025
static void mld_ifc_stop_timer(struct inet6_dev *idev)
{
	idev->mc_ifc_count = 0;
	if (del_timer(&idev->mc_ifc_timer))
		__in6_dev_put(idev);
}

1026
static void mld_dad_start_timer(struct inet6_dev *idev, unsigned long delay)
1027
{
1028
	unsigned long tv = prandom_u32() % delay;
1029 1030 1031 1032 1033

	if (!mod_timer(&idev->mc_dad_timer, jiffies+tv+2))
		in6_dev_hold(idev);
}

1034 1035 1036 1037 1038 1039
static void mld_dad_stop_timer(struct inet6_dev *idev)
{
	if (del_timer(&idev->mc_dad_timer))
		__in6_dev_put(idev);
}

L
Linus Torvalds 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
/*
 *	IGMP handling (alias multicast ICMPv6 messages)
 */

static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
{
	unsigned long delay = resptime;

	/* Do not start timer for these addresses */
	if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
	    IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
		return;

	if (del_timer(&ma->mca_timer)) {
		atomic_dec(&ma->mca_refcnt);
		delay = ma->mca_timer.expires - jiffies;
	}

1058
	if (delay >= resptime)
1059
		delay = prandom_u32() % resptime;
1060

L
Linus Torvalds 已提交
1061 1062 1063 1064 1065 1066
	ma->mca_timer.expires = jiffies + delay;
	if (!mod_timer(&ma->mca_timer, jiffies + delay))
		atomic_inc(&ma->mca_refcnt);
	ma->mca_flags |= MAF_TIMER_RUNNING;
}

1067
/* mark EXCLUDE-mode sources */
1068 1069
static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
			     const struct in6_addr *srcs)
L
Linus Torvalds 已提交
1070 1071 1072 1073 1074
{
	struct ip6_sf_list *psf;
	int i, scount;

	scount = 0;
1075
	for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
L
Linus Torvalds 已提交
1076 1077
		if (scount == nsrcs)
			break;
1078
		for (i = 0; i < nsrcs; i++) {
1079
			/* skip inactive filters */
1080
			if (psf->sf_count[MCAST_INCLUDE] ||
1081 1082
			    pmc->mca_sfcount[MCAST_EXCLUDE] !=
			    psf->sf_count[MCAST_EXCLUDE])
1083
				break;
1084 1085 1086 1087 1088 1089 1090 1091
			if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
				scount++;
				break;
			}
		}
	}
	pmc->mca_flags &= ~MAF_GSQUERY;
	if (scount == nsrcs)	/* all sources excluded */
1092 1093
		return false;
	return true;
1094 1095
}

1096 1097
static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
			    const struct in6_addr *srcs)
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
{
	struct ip6_sf_list *psf;
	int i, scount;

	if (pmc->mca_sfmode == MCAST_EXCLUDE)
		return mld_xmarksources(pmc, nsrcs, srcs);

	/* mark INCLUDE-mode sources */

	scount = 0;
1108
	for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
1109 1110
		if (scount == nsrcs)
			break;
1111
		for (i = 0; i < nsrcs; i++) {
L
Linus Torvalds 已提交
1112 1113 1114 1115 1116
			if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
				psf->sf_gsresp = 1;
				scount++;
				break;
			}
1117 1118 1119 1120
		}
	}
	if (!scount) {
		pmc->mca_flags &= ~MAF_GSQUERY;
1121
		return false;
L
Linus Torvalds 已提交
1122
	}
1123
	pmc->mca_flags |= MAF_GSQUERY;
1124
	return true;
L
Linus Torvalds 已提交
1125 1126
}

1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
static int mld_force_mld_version(const struct inet6_dev *idev)
{
	/* Normally, both are 0 here. If enforcement to a particular is
	 * being used, individual device enforcement will have a lower
	 * precedence over 'all' device (.../conf/all/force_mld_version).
	 */

	if (dev_net(idev->dev)->ipv6.devconf_all->force_mld_version != 0)
		return dev_net(idev->dev)->ipv6.devconf_all->force_mld_version;
	else
		return idev->cnf.force_mld_version;
}

static bool mld_in_v2_mode_only(const struct inet6_dev *idev)
{
	return mld_force_mld_version(idev) == 2;
}

static bool mld_in_v1_mode_only(const struct inet6_dev *idev)
{
	return mld_force_mld_version(idev) == 1;
}

1150 1151
static bool mld_in_v1_mode(const struct inet6_dev *idev)
{
1152 1153 1154
	if (mld_in_v2_mode_only(idev))
		return false;
	if (mld_in_v1_mode_only(idev))
1155 1156 1157 1158 1159 1160 1161
		return true;
	if (idev->mc_v1_seen && time_before(jiffies, idev->mc_v1_seen))
		return true;

	return false;
}

1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
static void mld_set_v1_mode(struct inet6_dev *idev)
{
	/* RFC3810, relevant sections:
	 *  - 9.1. Robustness Variable
	 *  - 9.2. Query Interval
	 *  - 9.3. Query Response Interval
	 *  - 9.12. Older Version Querier Present Timeout
	 */
	unsigned long switchback;

	switchback = (idev->mc_qrv * idev->mc_qi) + idev->mc_qri;

	idev->mc_v1_seen = jiffies + switchback;
}

static void mld_update_qrv(struct inet6_dev *idev,
			   const struct mld2_query *mlh2)
{
	/* RFC3810, relevant sections:
	 *  - 5.1.8. QRV (Querier's Robustness Variable)
	 *  - 9.1. Robustness Variable
	 */

	/* The value of the Robustness Variable MUST NOT be zero,
	 * and SHOULD NOT be one. Catch this here if we ever run
	 * into such a case in future.
	 */
1189
	const int min_qrv = min(MLD_QRV_DEFAULT, sysctl_mld_qrv);
1190 1191 1192 1193 1194
	WARN_ON(idev->mc_qrv == 0);

	if (mlh2->mld2q_qrv > 0)
		idev->mc_qrv = mlh2->mld2q_qrv;

1195
	if (unlikely(idev->mc_qrv < min_qrv)) {
1196
		net_warn_ratelimited("IPv6: MLD: clamping QRV from %u to %u!\n",
1197 1198
				     idev->mc_qrv, min_qrv);
		idev->mc_qrv = min_qrv;
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
	}
}

static void mld_update_qi(struct inet6_dev *idev,
			  const struct mld2_query *mlh2)
{
	/* RFC3810, relevant sections:
	 *  - 5.1.9. QQIC (Querier's Query Interval Code)
	 *  - 9.2. Query Interval
	 *  - 9.12. Older Version Querier Present Timeout
	 *    (the [Query Interval] in the last Query received)
	 */
	unsigned long mc_qqi;

	if (mlh2->mld2q_qqic < 128) {
		mc_qqi = mlh2->mld2q_qqic;
	} else {
		unsigned long mc_man, mc_exp;

		mc_exp = MLDV2_QQIC_EXP(mlh2->mld2q_qqic);
		mc_man = MLDV2_QQIC_MAN(mlh2->mld2q_qqic);

		mc_qqi = (mc_man | 0x10) << (mc_exp + 3);
	}

	idev->mc_qi = mc_qqi * HZ;
}

static void mld_update_qri(struct inet6_dev *idev,
			   const struct mld2_query *mlh2)
{
	/* RFC3810, relevant sections:
	 *  - 5.1.3. Maximum Response Code
	 *  - 9.3. Query Response Interval
	 */
1234
	idev->mc_qri = msecs_to_jiffies(mldv2_mrc(mlh2));
1235 1236
}

1237
static int mld_process_v1(struct inet6_dev *idev, struct mld_msg *mld,
1238
			  unsigned long *max_delay, bool v1_query)
1239 1240 1241 1242 1243 1244 1245 1246
{
	unsigned long mldv1_md;

	/* Ignore v1 queries */
	if (mld_in_v2_mode_only(idev))
		return -EINVAL;

	mldv1_md = ntohs(mld->mld_maxdelay);
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262

	/* When in MLDv1 fallback and a MLDv2 router start-up being
	 * unaware of current MLDv1 operation, the MRC == MRD mapping
	 * only works when the exponential algorithm is not being
	 * used (as MLDv1 is unaware of such things).
	 *
	 * According to the RFC author, the MLDv2 implementations
	 * he's aware of all use a MRC < 32768 on start up queries.
	 *
	 * Thus, should we *ever* encounter something else larger
	 * than that, just assume the maximum possible within our
	 * reach.
	 */
	if (!v1_query)
		mldv1_md = min(mldv1_md, MLDV1_MRD_MAX_COMPAT);

1263 1264
	*max_delay = max(msecs_to_jiffies(mldv1_md), 1UL);

1265 1266 1267 1268 1269 1270 1271
	/* MLDv1 router present: we need to go into v1 mode *only*
	 * when an MLDv1 query is received as per section 9.12. of
	 * RFC3810! And we know from RFC2710 section 3.7 that MLDv1
	 * queries MUST be of exactly 24 octets.
	 */
	if (v1_query)
		mld_set_v1_mode(idev);
1272 1273

	/* cancel MLDv2 report timer */
1274
	mld_gq_stop_timer(idev);
1275
	/* cancel the interface change timer */
1276
	mld_ifc_stop_timer(idev);
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
	/* clear deleted report items */
	mld_clear_delrec(idev);

	return 0;
}

static int mld_process_v2(struct inet6_dev *idev, struct mld2_query *mld,
			  unsigned long *max_delay)
{
	*max_delay = max(msecs_to_jiffies(mldv2_mrc(mld)), 1UL);

	mld_update_qrv(idev, mld);
	mld_update_qi(idev, mld);
	mld_update_qri(idev, mld);

	idev->mc_maxdelay = *max_delay;

	return 0;
}

E
Eric Dumazet 已提交
1297
/* called with rcu_read_lock() */
L
Linus Torvalds 已提交
1298 1299
int igmp6_event_query(struct sk_buff *skb)
{
1300
	struct mld2_query *mlh2 = NULL;
L
Linus Torvalds 已提交
1301
	struct ifmcaddr6 *ma;
1302
	const struct in6_addr *group;
L
Linus Torvalds 已提交
1303 1304
	unsigned long max_delay;
	struct inet6_dev *idev;
1305
	struct mld_msg *mld;
L
Linus Torvalds 已提交
1306 1307
	int group_type;
	int mark = 0;
1308
	int len, err;
L
Linus Torvalds 已提交
1309 1310 1311 1312 1313

	if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
		return -EINVAL;

	/* compute payload length excluding extension headers */
1314
	len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
1315
	len -= skb_network_header_len(skb);
L
Linus Torvalds 已提交
1316

H
Hangbin Liu 已提交
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
	/* RFC3810 6.2
	 * Upon reception of an MLD message that contains a Query, the node
	 * checks if the source address of the message is a valid link-local
	 * address, if the Hop Limit is set to 1, and if the Router Alert
	 * option is present in the Hop-By-Hop Options header of the IPv6
	 * packet.  If any of these checks fails, the packet is dropped.
	 */
	if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL) ||
	    ipv6_hdr(skb)->hop_limit != 1 ||
	    !(IP6CB(skb)->flags & IP6SKB_ROUTERALERT) ||
	    IP6CB(skb)->ra != htons(IPV6_OPT_ROUTERALERT_MLD))
L
Linus Torvalds 已提交
1328 1329
		return -EINVAL;

E
Eric Dumazet 已提交
1330
	idev = __in6_dev_get(skb->dev);
1331
	if (!idev)
L
Linus Torvalds 已提交
1332 1333
		return 0;

1334 1335
	mld = (struct mld_msg *)icmp6_hdr(skb);
	group = &mld->mld_mca;
L
Linus Torvalds 已提交
1336 1337 1338
	group_type = ipv6_addr_type(group);

	if (group_type != IPV6_ADDR_ANY &&
E
Eric Dumazet 已提交
1339
	    !(group_type&IPV6_ADDR_MULTICAST))
L
Linus Torvalds 已提交
1340 1341
		return -EINVAL;

1342 1343 1344 1345 1346
	if (len < MLD_V1_QUERY_LEN) {
		return -EINVAL;
	} else if (len == MLD_V1_QUERY_LEN || mld_in_v1_mode(idev)) {
		err = mld_process_v1(idev, mld, &max_delay,
				     len == MLD_V1_QUERY_LEN);
1347 1348
		if (err < 0)
			return err;
1349
	} else if (len >= MLD_V2_QUERY_LEN_MIN) {
1350
		int srcs_offset = sizeof(struct mld2_query) -
1351
				  sizeof(struct icmp6hdr);
1352

E
Eric Dumazet 已提交
1353
		if (!pskb_may_pull(skb, srcs_offset))
1354
			return -EINVAL;
E
Eric Dumazet 已提交
1355

1356
		mlh2 = (struct mld2_query *)skb_transport_header(skb);
1357

1358 1359 1360
		err = mld_process_v2(idev, mlh2, &max_delay);
		if (err < 0)
			return err;
1361

L
Linus Torvalds 已提交
1362
		if (group_type == IPV6_ADDR_ANY) { /* general query */
E
Eric Dumazet 已提交
1363
			if (mlh2->mld2q_nsrcs)
L
Linus Torvalds 已提交
1364
				return -EINVAL; /* no sources allowed */
E
Eric Dumazet 已提交
1365

L
Linus Torvalds 已提交
1366 1367 1368 1369
			mld_gq_start_timer(idev);
			return 0;
		}
		/* mark sources to include, if group & source-specific */
1370
		if (mlh2->mld2q_nsrcs != 0) {
1371
			if (!pskb_may_pull(skb, srcs_offset +
E
Eric Dumazet 已提交
1372
			    ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
1373
				return -EINVAL;
E
Eric Dumazet 已提交
1374

1375
			mlh2 = (struct mld2_query *)skb_transport_header(skb);
1376 1377
			mark = 1;
		}
1378
	} else {
L
Linus Torvalds 已提交
1379
		return -EINVAL;
1380
	}
L
Linus Torvalds 已提交
1381 1382 1383

	read_lock_bh(&idev->lock);
	if (group_type == IPV6_ADDR_ANY) {
1384
		for (ma = idev->mc_list; ma; ma = ma->next) {
L
Linus Torvalds 已提交
1385 1386 1387 1388 1389
			spin_lock_bh(&ma->mca_lock);
			igmp6_group_queried(ma, max_delay);
			spin_unlock_bh(&ma->mca_lock);
		}
	} else {
1390
		for (ma = idev->mc_list; ma; ma = ma->next) {
1391
			if (!ipv6_addr_equal(group, &ma->mca_addr))
L
Linus Torvalds 已提交
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
				continue;
			spin_lock_bh(&ma->mca_lock);
			if (ma->mca_flags & MAF_TIMER_RUNNING) {
				/* gsquery <- gsquery && mark */
				if (!mark)
					ma->mca_flags &= ~MAF_GSQUERY;
			} else {
				/* gsquery <- mark */
				if (mark)
					ma->mca_flags |= MAF_GSQUERY;
				else
					ma->mca_flags &= ~MAF_GSQUERY;
			}
1405
			if (!(ma->mca_flags & MAF_GSQUERY) ||
1406
			    mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
1407
				igmp6_group_queried(ma, max_delay);
L
Linus Torvalds 已提交
1408
			spin_unlock_bh(&ma->mca_lock);
1409
			break;
L
Linus Torvalds 已提交
1410 1411 1412 1413 1414 1415 1416
		}
	}
	read_unlock_bh(&idev->lock);

	return 0;
}

E
Eric Dumazet 已提交
1417
/* called with rcu_read_lock() */
L
Linus Torvalds 已提交
1418 1419 1420 1421
int igmp6_event_report(struct sk_buff *skb)
{
	struct ifmcaddr6 *ma;
	struct inet6_dev *idev;
1422
	struct mld_msg *mld;
L
Linus Torvalds 已提交
1423 1424 1425 1426 1427 1428
	int addr_type;

	/* Our own report looped back. Ignore it. */
	if (skb->pkt_type == PACKET_LOOPBACK)
		return 0;

1429 1430 1431 1432 1433
	/* send our report if the MC router may not have heard this report */
	if (skb->pkt_type != PACKET_MULTICAST &&
	    skb->pkt_type != PACKET_BROADCAST)
		return 0;

1434
	if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
L
Linus Torvalds 已提交
1435 1436
		return -EINVAL;

1437
	mld = (struct mld_msg *)icmp6_hdr(skb);
L
Linus Torvalds 已提交
1438 1439

	/* Drop reports with not link local source */
1440
	addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
1441
	if (addr_type != IPV6_ADDR_ANY &&
L
Linus Torvalds 已提交
1442 1443 1444
	    !(addr_type&IPV6_ADDR_LINKLOCAL))
		return -EINVAL;

E
Eric Dumazet 已提交
1445
	idev = __in6_dev_get(skb->dev);
1446
	if (!idev)
L
Linus Torvalds 已提交
1447 1448 1449 1450 1451 1452 1453
		return -ENODEV;

	/*
	 *	Cancel the timer for this group
	 */

	read_lock_bh(&idev->lock);
1454
	for (ma = idev->mc_list; ma; ma = ma->next) {
1455
		if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
L
Linus Torvalds 已提交
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
			spin_lock(&ma->mca_lock);
			if (del_timer(&ma->mca_timer))
				atomic_dec(&ma->mca_refcnt);
			ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
			spin_unlock(&ma->mca_lock);
			break;
		}
	}
	read_unlock_bh(&idev->lock);
	return 0;
}

1468 1469
static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
		  int gdeleted, int sdeleted)
L
Linus Torvalds 已提交
1470 1471 1472 1473 1474
{
	switch (type) {
	case MLD2_MODE_IS_INCLUDE:
	case MLD2_MODE_IS_EXCLUDE:
		if (gdeleted || sdeleted)
1475
			return false;
1476 1477
		if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
			if (pmc->mca_sfmode == MCAST_INCLUDE)
1478
				return true;
1479 1480 1481 1482
			/* don't include if this source is excluded
			 * in all filters
			 */
			if (psf->sf_count[MCAST_INCLUDE])
1483
				return type == MLD2_MODE_IS_INCLUDE;
1484 1485 1486
			return pmc->mca_sfcount[MCAST_EXCLUDE] ==
				psf->sf_count[MCAST_EXCLUDE];
		}
1487
		return false;
L
Linus Torvalds 已提交
1488 1489
	case MLD2_CHANGE_TO_INCLUDE:
		if (gdeleted || sdeleted)
1490
			return false;
L
Linus Torvalds 已提交
1491 1492 1493
		return psf->sf_count[MCAST_INCLUDE] != 0;
	case MLD2_CHANGE_TO_EXCLUDE:
		if (gdeleted || sdeleted)
1494
			return false;
L
Linus Torvalds 已提交
1495 1496
		if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
		    psf->sf_count[MCAST_INCLUDE])
1497
			return false;
L
Linus Torvalds 已提交
1498 1499 1500 1501
		return pmc->mca_sfcount[MCAST_EXCLUDE] ==
			psf->sf_count[MCAST_EXCLUDE];
	case MLD2_ALLOW_NEW_SOURCES:
		if (gdeleted || !psf->sf_crcount)
1502
			return false;
L
Linus Torvalds 已提交
1503 1504 1505 1506 1507 1508
		return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
	case MLD2_BLOCK_OLD_SOURCES:
		if (pmc->mca_sfmode == MCAST_INCLUDE)
			return gdeleted || (psf->sf_crcount && sdeleted);
		return psf->sf_crcount && !gdeleted && !sdeleted;
	}
1509
	return false;
L
Linus Torvalds 已提交
1510 1511 1512 1513 1514 1515 1516 1517
}

static int
mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
{
	struct ip6_sf_list *psf;
	int scount = 0;

1518
	for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
L
Linus Torvalds 已提交
1519 1520 1521 1522 1523 1524 1525
		if (!is_in(pmc, psf, type, gdeleted, sdeleted))
			continue;
		scount++;
	}
	return scount;
}

1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
		       struct net_device *dev,
		       const struct in6_addr *saddr,
		       const struct in6_addr *daddr,
		       int proto, int len)
{
	struct ipv6hdr *hdr;

	skb->protocol = htons(ETH_P_IPV6);
	skb->dev = dev;

	skb_reset_network_header(skb);
	skb_put(skb, sizeof(struct ipv6hdr));
	hdr = ipv6_hdr(skb);

	ip6_flow_hdr(hdr, 0, 0);

	hdr->payload_len = htons(len);
	hdr->nexthdr = proto;
	hdr->hop_limit = inet6_sk(sk)->hop_limit;

	hdr->saddr = *saddr;
	hdr->daddr = *daddr;
}

1551
static struct sk_buff *mld_newpack(struct inet6_dev *idev, unsigned int mtu)
L
Linus Torvalds 已提交
1552
{
1553
	struct net_device *dev = idev->dev;
1554
	struct net *net = dev_net(dev);
1555
	struct sock *sk = net->ipv6.igmp_sk;
L
Linus Torvalds 已提交
1556 1557 1558
	struct sk_buff *skb;
	struct mld2_report *pmr;
	struct in6_addr addr_buf;
1559
	const struct in6_addr *saddr;
1560 1561
	int hlen = LL_RESERVED_SPACE(dev);
	int tlen = dev->needed_tailroom;
1562
	unsigned int size = mtu + hlen + tlen;
L
Linus Torvalds 已提交
1563 1564 1565 1566 1567 1568
	int err;
	u8 ra[8] = { IPPROTO_ICMPV6, 0,
		     IPV6_TLV_ROUTERALERT, 2, 0, 0,
		     IPV6_TLV_PADN, 0 };

	/* we assume size > sizeof(ra) here */
1569 1570 1571
	/* limit our allocations to order-0 page */
	size = min_t(int, size, SKB_MAX_ORDER(0, 0));
	skb = sock_alloc_send_skb(sk, size, 1, &err);
L
Linus Torvalds 已提交
1572

S
Stephen Hemminger 已提交
1573
	if (!skb)
L
Linus Torvalds 已提交
1574 1575
		return NULL;

1576
	skb->priority = TC_PRIO_CONTROL;
1577 1578
	skb->reserved_tailroom = skb_end_offset(skb) -
				 min(mtu, skb_end_offset(skb));
1579
	skb_reserve(skb, hlen);
L
Linus Torvalds 已提交
1580

1581
	if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
L
Linus Torvalds 已提交
1582
		/* <draft-ietf-magma-mld-source-05.txt>:
1583
		 * use unspecified address as the source address
L
Linus Torvalds 已提交
1584 1585
		 * when a valid link-local address is not available.
		 */
1586 1587 1588
		saddr = &in6addr_any;
	} else
		saddr = &addr_buf;
L
Linus Torvalds 已提交
1589

1590
	ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
L
Linus Torvalds 已提交
1591 1592 1593

	memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));

1594
	skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
1595 1596
	skb_put(skb, sizeof(*pmr));
	pmr = (struct mld2_report *)skb_transport_header(skb);
1597 1598 1599 1600 1601
	pmr->mld2r_type = ICMPV6_MLD2_REPORT;
	pmr->mld2r_resv1 = 0;
	pmr->mld2r_cksum = 0;
	pmr->mld2r_resv2 = 0;
	pmr->mld2r_ngrec = 0;
L
Linus Torvalds 已提交
1602 1603 1604 1605 1606
	return skb;
}

static void mld_sendpack(struct sk_buff *skb)
{
1607
	struct ipv6hdr *pip6 = ipv6_hdr(skb);
1608 1609
	struct mld2_report *pmr =
			      (struct mld2_report *)skb_transport_header(skb);
L
Linus Torvalds 已提交
1610
	int payload_len, mldlen;
E
Eric Dumazet 已提交
1611
	struct inet6_dev *idev;
1612
	struct net *net = dev_net(skb->dev);
L
Linus Torvalds 已提交
1613
	int err;
1614
	struct flowi6 fl6;
E
Eric Dumazet 已提交
1615
	struct dst_entry *dst;
L
Linus Torvalds 已提交
1616

E
Eric Dumazet 已提交
1617 1618
	rcu_read_lock();
	idev = __in6_dev_get(skb->dev);
1619 1620
	IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);

1621 1622 1623
	payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
		sizeof(*pip6);
	mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
L
Linus Torvalds 已提交
1624 1625
	pip6->payload_len = htons(payload_len);

1626 1627 1628 1629
	pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
					   IPPROTO_ICMPV6,
					   csum_partial(skb_transport_header(skb),
							mldlen, 0));
1630

1631
	icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
1632 1633
			 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
			 skb->dev->ifindex);
1634
	dst = icmp6_dst_alloc(skb->dev, &fl6);
1635

1636 1637 1638 1639 1640
	err = 0;
	if (IS_ERR(dst)) {
		err = PTR_ERR(dst);
		dst = NULL;
	}
E
Eric Dumazet 已提交
1641
	skb_dst_set(skb, dst);
1642 1643 1644
	if (err)
		goto err_out;

1645 1646
	payload_len = skb->len;

1647
	err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1648 1649
		      dst_output);
out:
L
Linus Torvalds 已提交
1650
	if (!err) {
1651 1652 1653 1654 1655 1656
		ICMP6MSGOUT_INC_STATS(net, idev, ICMPV6_MLD2_REPORT);
		ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
		IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
	} else {
		IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
	}
L
Linus Torvalds 已提交
1657

E
Eric Dumazet 已提交
1658
	rcu_read_unlock();
1659 1660 1661 1662 1663
	return;

err_out:
	kfree_skb(skb);
	goto out;
L
Linus Torvalds 已提交
1664 1665 1666 1667
}

static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
{
1668
	return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
L
Linus Torvalds 已提交
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
}

static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
	int type, struct mld2_grec **ppgr)
{
	struct net_device *dev = pmc->idev->dev;
	struct mld2_report *pmr;
	struct mld2_grec *pgr;

	if (!skb)
1679
		skb = mld_newpack(pmc->idev, dev->mtu);
L
Linus Torvalds 已提交
1680 1681 1682 1683 1684 1685 1686
	if (!skb)
		return NULL;
	pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
	pgr->grec_type = type;
	pgr->grec_auxwords = 0;
	pgr->grec_nsrcs = 0;
	pgr->grec_mca = pmc->mca_addr;	/* structure copy */
1687
	pmr = (struct mld2_report *)skb_transport_header(skb);
1688
	pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
L
Linus Torvalds 已提交
1689 1690 1691 1692
	*ppgr = pgr;
	return skb;
}

1693
#define AVAILABLE(skb)	((skb) ? skb_availroom(skb) : 0)
L
Linus Torvalds 已提交
1694 1695

static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1696
	int type, int gdeleted, int sdeleted, int crsend)
L
Linus Torvalds 已提交
1697
{
1698 1699
	struct inet6_dev *idev = pmc->idev;
	struct net_device *dev = idev->dev;
L
Linus Torvalds 已提交
1700 1701 1702
	struct mld2_report *pmr;
	struct mld2_grec *pgr = NULL;
	struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1703
	int scount, stotal, first, isquery, truncate;
L
Linus Torvalds 已提交
1704 1705 1706 1707 1708 1709 1710 1711 1712

	if (pmc->mca_flags & MAF_NOREPORT)
		return skb;

	isquery = type == MLD2_MODE_IS_INCLUDE ||
		  type == MLD2_MODE_IS_EXCLUDE;
	truncate = type == MLD2_MODE_IS_EXCLUDE ||
		    type == MLD2_CHANGE_TO_EXCLUDE;

1713 1714
	stotal = scount = 0;

L
Linus Torvalds 已提交
1715 1716
	psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;

1717 1718 1719
	if (!*psf_list)
		goto empty_source;

1720
	pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
L
Linus Torvalds 已提交
1721 1722 1723

	/* EX and TO_EX get a fresh packet, if needed */
	if (truncate) {
1724
		if (pmr && pmr->mld2r_ngrec &&
L
Linus Torvalds 已提交
1725 1726 1727
		    AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
			if (skb)
				mld_sendpack(skb);
1728
			skb = mld_newpack(idev, dev->mtu);
L
Linus Torvalds 已提交
1729 1730 1731 1732
		}
	}
	first = 1;
	psf_prev = NULL;
1733
	for (psf = *psf_list; psf; psf = psf_next) {
L
Linus Torvalds 已提交
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
		struct in6_addr *psrc;

		psf_next = psf->sf_next;

		if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
			psf_prev = psf;
			continue;
		}

		/* clear marks on query responses */
		if (isquery)
			psf->sf_gsresp = 0;

		if (AVAILABLE(skb) < sizeof(*psrc) +
		    first*sizeof(struct mld2_grec)) {
			if (truncate && !first)
				break;	 /* truncate these */
			if (pgr)
				pgr->grec_nsrcs = htons(scount);
			if (skb)
				mld_sendpack(skb);
1755
			skb = mld_newpack(idev, dev->mtu);
L
Linus Torvalds 已提交
1756 1757 1758 1759 1760 1761 1762
			first = 1;
			scount = 0;
		}
		if (first) {
			skb = add_grhead(skb, pmc, type, &pgr);
			first = 0;
		}
1763 1764
		if (!skb)
			return NULL;
L
Linus Torvalds 已提交
1765 1766
		psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
		*psrc = psf->sf_addr;
1767
		scount++; stotal++;
L
Linus Torvalds 已提交
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
		if ((type == MLD2_ALLOW_NEW_SOURCES ||
		     type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
			psf->sf_crcount--;
			if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
				if (psf_prev)
					psf_prev->sf_next = psf->sf_next;
				else
					*psf_list = psf->sf_next;
				kfree(psf);
				continue;
			}
		}
		psf_prev = psf;
	}
1782 1783 1784 1785 1786 1787

empty_source:
	if (!stotal) {
		if (type == MLD2_ALLOW_NEW_SOURCES ||
		    type == MLD2_BLOCK_OLD_SOURCES)
			return skb;
1788
		if (pmc->mca_crcount || isquery || crsend) {
1789 1790 1791 1792 1793 1794 1795 1796
			/* make sure we have room for group header */
			if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
				mld_sendpack(skb);
				skb = NULL; /* add_grhead will get a new one */
			}
			skb = add_grhead(skb, pmc, type, &pgr);
		}
	}
L
Linus Torvalds 已提交
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
	if (pgr)
		pgr->grec_nsrcs = htons(scount);

	if (isquery)
		pmc->mca_flags &= ~MAF_GSQUERY;	/* clear query state */
	return skb;
}

static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
{
	struct sk_buff *skb = NULL;
	int type;

1810
	read_lock_bh(&idev->lock);
L
Linus Torvalds 已提交
1811
	if (!pmc) {
1812
		for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
L
Linus Torvalds 已提交
1813 1814 1815 1816 1817 1818 1819
			if (pmc->mca_flags & MAF_NOREPORT)
				continue;
			spin_lock_bh(&pmc->mca_lock);
			if (pmc->mca_sfcount[MCAST_EXCLUDE])
				type = MLD2_MODE_IS_EXCLUDE;
			else
				type = MLD2_MODE_IS_INCLUDE;
1820
			skb = add_grec(skb, pmc, type, 0, 0, 0);
L
Linus Torvalds 已提交
1821 1822 1823 1824 1825 1826 1827 1828
			spin_unlock_bh(&pmc->mca_lock);
		}
	} else {
		spin_lock_bh(&pmc->mca_lock);
		if (pmc->mca_sfcount[MCAST_EXCLUDE])
			type = MLD2_MODE_IS_EXCLUDE;
		else
			type = MLD2_MODE_IS_INCLUDE;
1829
		skb = add_grec(skb, pmc, type, 0, 0, 0);
L
Linus Torvalds 已提交
1830 1831
		spin_unlock_bh(&pmc->mca_lock);
	}
1832
	read_unlock_bh(&idev->lock);
L
Linus Torvalds 已提交
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
	if (skb)
		mld_sendpack(skb);
}

/*
 * remove zero-count source records from a source filter list
 */
static void mld_clear_zeros(struct ip6_sf_list **ppsf)
{
	struct ip6_sf_list *psf_prev, *psf_next, *psf;

	psf_prev = NULL;
1845
	for (psf = *ppsf; psf; psf = psf_next) {
L
Linus Torvalds 已提交
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
		psf_next = psf->sf_next;
		if (psf->sf_crcount == 0) {
			if (psf_prev)
				psf_prev->sf_next = psf->sf_next;
			else
				*ppsf = psf->sf_next;
			kfree(psf);
		} else
			psf_prev = psf;
	}
}

static void mld_send_cr(struct inet6_dev *idev)
{
	struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
	struct sk_buff *skb = NULL;
	int type, dtype;

	read_lock_bh(&idev->lock);
1865
	spin_lock(&idev->mc_lock);
L
Linus Torvalds 已提交
1866 1867 1868

	/* deleted MCA's */
	pmc_prev = NULL;
1869
	for (pmc = idev->mc_tomb; pmc; pmc = pmc_next) {
L
Linus Torvalds 已提交
1870 1871 1872 1873
		pmc_next = pmc->next;
		if (pmc->mca_sfmode == MCAST_INCLUDE) {
			type = MLD2_BLOCK_OLD_SOURCES;
			dtype = MLD2_BLOCK_OLD_SOURCES;
1874 1875
			skb = add_grec(skb, pmc, type, 1, 0, 0);
			skb = add_grec(skb, pmc, dtype, 1, 1, 0);
L
Linus Torvalds 已提交
1876 1877 1878 1879
		}
		if (pmc->mca_crcount) {
			if (pmc->mca_sfmode == MCAST_EXCLUDE) {
				type = MLD2_CHANGE_TO_INCLUDE;
1880
				skb = add_grec(skb, pmc, type, 1, 0, 0);
L
Linus Torvalds 已提交
1881
			}
1882
			pmc->mca_crcount--;
L
Linus Torvalds 已提交
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
			if (pmc->mca_crcount == 0) {
				mld_clear_zeros(&pmc->mca_tomb);
				mld_clear_zeros(&pmc->mca_sources);
			}
		}
		if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
		    !pmc->mca_sources) {
			if (pmc_prev)
				pmc_prev->next = pmc_next;
			else
				idev->mc_tomb = pmc_next;
			in6_dev_put(pmc->idev);
			kfree(pmc);
		} else
			pmc_prev = pmc;
	}
1899
	spin_unlock(&idev->mc_lock);
L
Linus Torvalds 已提交
1900 1901

	/* change recs */
1902
	for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
L
Linus Torvalds 已提交
1903 1904 1905 1906 1907 1908 1909 1910
		spin_lock_bh(&pmc->mca_lock);
		if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
			type = MLD2_BLOCK_OLD_SOURCES;
			dtype = MLD2_ALLOW_NEW_SOURCES;
		} else {
			type = MLD2_ALLOW_NEW_SOURCES;
			dtype = MLD2_BLOCK_OLD_SOURCES;
		}
1911 1912
		skb = add_grec(skb, pmc, type, 0, 0, 0);
		skb = add_grec(skb, pmc, dtype, 0, 1, 0);	/* deleted sources */
L
Linus Torvalds 已提交
1913 1914 1915 1916 1917 1918 1919

		/* filter mode changes */
		if (pmc->mca_crcount) {
			if (pmc->mca_sfmode == MCAST_EXCLUDE)
				type = MLD2_CHANGE_TO_EXCLUDE;
			else
				type = MLD2_CHANGE_TO_INCLUDE;
1920
			skb = add_grec(skb, pmc, type, 0, 0, 0);
1921
			pmc->mca_crcount--;
L
Linus Torvalds 已提交
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
		}
		spin_unlock_bh(&pmc->mca_lock);
	}
	read_unlock_bh(&idev->lock);
	if (!skb)
		return;
	(void) mld_sendpack(skb);
}

static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
{
1933
	struct net *net = dev_net(dev);
1934
	struct sock *sk = net->ipv6.igmp_sk;
L
Linus Torvalds 已提交
1935
	struct inet6_dev *idev;
1936
	struct sk_buff *skb;
1937
	struct mld_msg *hdr;
1938
	const struct in6_addr *snd_addr, *saddr;
L
Linus Torvalds 已提交
1939
	struct in6_addr addr_buf;
1940 1941
	int hlen = LL_RESERVED_SPACE(dev);
	int tlen = dev->needed_tailroom;
L
Linus Torvalds 已提交
1942 1943 1944 1945
	int err, len, payload_len, full_len;
	u8 ra[8] = { IPPROTO_ICMPV6, 0,
		     IPV6_TLV_ROUTERALERT, 2, 0, 0,
		     IPV6_TLV_PADN, 0 };
1946
	struct flowi6 fl6;
E
Eric Dumazet 已提交
1947
	struct dst_entry *dst;
L
Linus Torvalds 已提交
1948

1949 1950 1951 1952
	if (type == ICMPV6_MGM_REDUCTION)
		snd_addr = &in6addr_linklocal_allrouters;
	else
		snd_addr = addr;
L
Linus Torvalds 已提交
1953 1954 1955 1956 1957

	len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
	payload_len = len + sizeof(ra);
	full_len = sizeof(struct ipv6hdr) + payload_len;

1958 1959 1960 1961 1962
	rcu_read_lock();
	IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
		      IPSTATS_MIB_OUT, full_len);
	rcu_read_unlock();

1963
	skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
L
Linus Torvalds 已提交
1964

1965
	if (!skb) {
1966
		rcu_read_lock();
1967
		IP6_INC_STATS(net, __in6_dev_get(dev),
1968 1969
			      IPSTATS_MIB_OUTDISCARDS);
		rcu_read_unlock();
L
Linus Torvalds 已提交
1970 1971
		return;
	}
1972
	skb->priority = TC_PRIO_CONTROL;
1973
	skb_reserve(skb, hlen);
L
Linus Torvalds 已提交
1974

1975
	if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
L
Linus Torvalds 已提交
1976
		/* <draft-ietf-magma-mld-source-05.txt>:
1977
		 * use unspecified address as the source address
L
Linus Torvalds 已提交
1978 1979
		 * when a valid link-local address is not available.
		 */
1980 1981 1982
		saddr = &in6addr_any;
	} else
		saddr = &addr_buf;
L
Linus Torvalds 已提交
1983

1984
	ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
L
Linus Torvalds 已提交
1985 1986 1987

	memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));

1988 1989 1990
	hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg));
	memset(hdr, 0, sizeof(struct mld_msg));
	hdr->mld_type = type;
A
Alexey Dobriyan 已提交
1991
	hdr->mld_mca = *addr;
L
Linus Torvalds 已提交
1992

1993 1994 1995
	hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
					 IPPROTO_ICMPV6,
					 csum_partial(hdr, len, 0));
L
Linus Torvalds 已提交
1996

E
Eric Dumazet 已提交
1997 1998
	rcu_read_lock();
	idev = __in6_dev_get(skb->dev);
L
Linus Torvalds 已提交
1999

2000
	icmpv6_flow_init(sk, &fl6, type,
2001 2002
			 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
			 skb->dev->ifindex);
2003
	dst = icmp6_dst_alloc(skb->dev, &fl6);
2004 2005
	if (IS_ERR(dst)) {
		err = PTR_ERR(dst);
2006
		goto err_out;
2007
	}
2008

E
Eric Dumazet 已提交
2009
	skb_dst_set(skb, dst);
2010
	err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
2011 2012
		      dst_output);
out:
L
Linus Torvalds 已提交
2013
	if (!err) {
2014
		ICMP6MSGOUT_INC_STATS(net, idev, type);
2015
		ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
2016
		IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
L
Linus Torvalds 已提交
2017
	} else
2018
		IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
L
Linus Torvalds 已提交
2019

E
Eric Dumazet 已提交
2020
	rcu_read_unlock();
L
Linus Torvalds 已提交
2021
	return;
2022 2023 2024 2025

err_out:
	kfree_skb(skb);
	goto out;
L
Linus Torvalds 已提交
2026 2027
}

2028
static void mld_send_initial_cr(struct inet6_dev *idev)
2029
{
2030 2031 2032 2033 2034 2035 2036 2037 2038
	struct sk_buff *skb;
	struct ifmcaddr6 *pmc;
	int type;

	if (mld_in_v1_mode(idev))
		return;

	skb = NULL;
	read_lock_bh(&idev->lock);
2039
	for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
2040 2041 2042 2043 2044 2045 2046
		spin_lock_bh(&pmc->mca_lock);
		if (pmc->mca_sfcount[MCAST_EXCLUDE])
			type = MLD2_CHANGE_TO_EXCLUDE;
		else
			type = MLD2_CHANGE_TO_INCLUDE;
		skb = add_grec(skb, pmc, type, 0, 0, 1);
		spin_unlock_bh(&pmc->mca_lock);
2047
	}
2048 2049 2050
	read_unlock_bh(&idev->lock);
	if (skb)
		mld_sendpack(skb);
2051 2052 2053 2054 2055 2056
}

void ipv6_mc_dad_complete(struct inet6_dev *idev)
{
	idev->mc_dad_count = idev->mc_qrv;
	if (idev->mc_dad_count) {
2057
		mld_send_initial_cr(idev);
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
		idev->mc_dad_count--;
		if (idev->mc_dad_count)
			mld_dad_start_timer(idev, idev->mc_maxdelay);
	}
}

static void mld_dad_timer_expire(unsigned long data)
{
	struct inet6_dev *idev = (struct inet6_dev *)data;

2068
	mld_send_initial_cr(idev);
2069 2070 2071 2072 2073
	if (idev->mc_dad_count) {
		idev->mc_dad_count--;
		if (idev->mc_dad_count)
			mld_dad_start_timer(idev, idev->mc_maxdelay);
	}
2074
	in6_dev_put(idev);
2075 2076
}

L
Linus Torvalds 已提交
2077
static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
2078
	const struct in6_addr *psfsrc)
L
Linus Torvalds 已提交
2079 2080 2081 2082 2083
{
	struct ip6_sf_list *psf, *psf_prev;
	int rv = 0;

	psf_prev = NULL;
2084
	for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
L
Linus Torvalds 已提交
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
		if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
			break;
		psf_prev = psf;
	}
	if (!psf || psf->sf_count[sfmode] == 0) {
		/* source filter not found, or count wrong =>  bug */
		return -ESRCH;
	}
	psf->sf_count[sfmode]--;
	if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
		struct inet6_dev *idev = pmc->idev;

		/* no more filters for this source */
		if (psf_prev)
			psf_prev->sf_next = psf->sf_next;
		else
			pmc->mca_sources = psf->sf_next;
		if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
2103
		    !mld_in_v1_mode(idev)) {
L
Linus Torvalds 已提交
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
			psf->sf_crcount = idev->mc_qrv;
			psf->sf_next = pmc->mca_tomb;
			pmc->mca_tomb = psf;
			rv = 1;
		} else
			kfree(psf);
	}
	return rv;
}

2114 2115
static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
			  int sfmode, int sfcount, const struct in6_addr *psfsrc,
L
Linus Torvalds 已提交
2116 2117 2118 2119 2120 2121 2122 2123 2124
			  int delta)
{
	struct ifmcaddr6 *pmc;
	int	changerec = 0;
	int	i, err;

	if (!idev)
		return -ENODEV;
	read_lock_bh(&idev->lock);
2125
	for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
L
Linus Torvalds 已提交
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
		if (ipv6_addr_equal(pmca, &pmc->mca_addr))
			break;
	}
	if (!pmc) {
		/* MCA not found?? bug */
		read_unlock_bh(&idev->lock);
		return -ESRCH;
	}
	spin_lock_bh(&pmc->mca_lock);
	sf_markstate(pmc);
	if (!delta) {
		if (!pmc->mca_sfcount[sfmode]) {
			spin_unlock_bh(&pmc->mca_lock);
			read_unlock_bh(&idev->lock);
			return -EINVAL;
		}
		pmc->mca_sfcount[sfmode]--;
	}
	err = 0;
2145
	for (i = 0; i < sfcount; i++) {
L
Linus Torvalds 已提交
2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
		int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);

		changerec |= rv > 0;
		if (!err && rv < 0)
			err = rv;
	}
	if (pmc->mca_sfmode == MCAST_EXCLUDE &&
	    pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
	    pmc->mca_sfcount[MCAST_INCLUDE]) {
		struct ip6_sf_list *psf;

		/* filter mode change */
		pmc->mca_sfmode = MCAST_INCLUDE;
		pmc->mca_crcount = idev->mc_qrv;
		idev->mc_ifc_count = pmc->mca_crcount;
2161
		for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
L
Linus Torvalds 已提交
2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
			psf->sf_crcount = 0;
		mld_ifc_event(pmc->idev);
	} else if (sf_setstate(pmc) || changerec)
		mld_ifc_event(pmc->idev);
	spin_unlock_bh(&pmc->mca_lock);
	read_unlock_bh(&idev->lock);
	return err;
}

/*
 * Add multicast single-source filter to the interface list
 */
static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
2175
	const struct in6_addr *psfsrc)
L
Linus Torvalds 已提交
2176 2177 2178 2179
{
	struct ip6_sf_list *psf, *psf_prev;

	psf_prev = NULL;
2180
	for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
L
Linus Torvalds 已提交
2181 2182 2183 2184 2185
		if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
			break;
		psf_prev = psf;
	}
	if (!psf) {
2186
		psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
L
Linus Torvalds 已提交
2187 2188
		if (!psf)
			return -ENOBUFS;
2189

L
Linus Torvalds 已提交
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
		psf->sf_addr = *psfsrc;
		if (psf_prev) {
			psf_prev->sf_next = psf;
		} else
			pmc->mca_sources = psf;
	}
	psf->sf_count[sfmode]++;
	return 0;
}

static void sf_markstate(struct ifmcaddr6 *pmc)
{
	struct ip6_sf_list *psf;
	int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];

2205
	for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
L
Linus Torvalds 已提交
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215
		if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
			psf->sf_oldin = mca_xcount ==
				psf->sf_count[MCAST_EXCLUDE] &&
				!psf->sf_count[MCAST_INCLUDE];
		} else
			psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
}

static int sf_setstate(struct ifmcaddr6 *pmc)
{
2216
	struct ip6_sf_list *psf, *dpsf;
L
Linus Torvalds 已提交
2217 2218 2219 2220 2221
	int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
	int qrv = pmc->idev->mc_qrv;
	int new_in, rv;

	rv = 0;
2222
	for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
L
Linus Torvalds 已提交
2223 2224 2225 2226 2227
		if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
			new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
				!psf->sf_count[MCAST_INCLUDE];
		} else
			new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2228 2229
		if (new_in) {
			if (!psf->sf_oldin) {
2230
				struct ip6_sf_list *prev = NULL;
2231

2232 2233
				for (dpsf = pmc->mca_tomb; dpsf;
				     dpsf = dpsf->sf_next) {
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
					if (ipv6_addr_equal(&dpsf->sf_addr,
					    &psf->sf_addr))
						break;
					prev = dpsf;
				}
				if (dpsf) {
					if (prev)
						prev->sf_next = dpsf->sf_next;
					else
						pmc->mca_tomb = dpsf->sf_next;
					kfree(dpsf);
				}
				psf->sf_crcount = qrv;
				rv++;
			}
		} else if (psf->sf_oldin) {
			psf->sf_crcount = 0;
			/*
			 * add or update "delete" records if an active filter
			 * is now inactive
			 */
2255
			for (dpsf = pmc->mca_tomb; dpsf; dpsf = dpsf->sf_next)
2256 2257 2258 2259
				if (ipv6_addr_equal(&dpsf->sf_addr,
				    &psf->sf_addr))
					break;
			if (!dpsf) {
2260
				dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2261 2262 2263 2264 2265 2266 2267 2268
				if (!dpsf)
					continue;
				*dpsf = *psf;
				/* pmc->mca_lock held by callers */
				dpsf->sf_next = pmc->mca_tomb;
				pmc->mca_tomb = dpsf;
			}
			dpsf->sf_crcount = qrv;
L
Linus Torvalds 已提交
2269 2270 2271 2272 2273 2274 2275 2276 2277
			rv++;
		}
	}
	return rv;
}

/*
 * Add multicast source filter list to the interface list
 */
2278 2279
static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
			  int sfmode, int sfcount, const struct in6_addr *psfsrc,
L
Linus Torvalds 已提交
2280 2281 2282 2283 2284 2285 2286 2287 2288
			  int delta)
{
	struct ifmcaddr6 *pmc;
	int	isexclude;
	int	i, err;

	if (!idev)
		return -ENODEV;
	read_lock_bh(&idev->lock);
2289
	for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
L
Linus Torvalds 已提交
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304
		if (ipv6_addr_equal(pmca, &pmc->mca_addr))
			break;
	}
	if (!pmc) {
		/* MCA not found?? bug */
		read_unlock_bh(&idev->lock);
		return -ESRCH;
	}
	spin_lock_bh(&pmc->mca_lock);

	sf_markstate(pmc);
	isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
	if (!delta)
		pmc->mca_sfcount[sfmode]++;
	err = 0;
2305
	for (i = 0; i < sfcount; i++) {
2306
		err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
L
Linus Torvalds 已提交
2307 2308 2309 2310 2311 2312 2313 2314
		if (err)
			break;
	}
	if (err) {
		int j;

		if (!delta)
			pmc->mca_sfcount[sfmode]--;
2315
		for (j = 0; j < i; j++)
2316
			ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
L
Linus Torvalds 已提交
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
	} else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
		struct ip6_sf_list *psf;

		/* filter mode change */
		if (pmc->mca_sfcount[MCAST_EXCLUDE])
			pmc->mca_sfmode = MCAST_EXCLUDE;
		else if (pmc->mca_sfcount[MCAST_INCLUDE])
			pmc->mca_sfmode = MCAST_INCLUDE;
		/* else no filters; keep old mode for reports */

		pmc->mca_crcount = idev->mc_qrv;
		idev->mc_ifc_count = pmc->mca_crcount;
2329
		for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
L
Linus Torvalds 已提交
2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
			psf->sf_crcount = 0;
		mld_ifc_event(idev);
	} else if (sf_setstate(pmc))
		mld_ifc_event(idev);
	spin_unlock_bh(&pmc->mca_lock);
	read_unlock_bh(&idev->lock);
	return err;
}

static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
{
	struct ip6_sf_list *psf, *nextpsf;

2343
	for (psf = pmc->mca_tomb; psf; psf = nextpsf) {
L
Linus Torvalds 已提交
2344 2345 2346 2347
		nextpsf = psf->sf_next;
		kfree(psf);
	}
	pmc->mca_tomb = NULL;
2348
	for (psf = pmc->mca_sources; psf; psf = nextpsf) {
L
Linus Torvalds 已提交
2349 2350 2351 2352 2353
		nextpsf = psf->sf_next;
		kfree(psf);
	}
	pmc->mca_sources = NULL;
	pmc->mca_sfmode = MCAST_EXCLUDE;
2354
	pmc->mca_sfcount[MCAST_INCLUDE] = 0;
L
Linus Torvalds 已提交
2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367
	pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
}


static void igmp6_join_group(struct ifmcaddr6 *ma)
{
	unsigned long delay;

	if (ma->mca_flags & MAF_NOREPORT)
		return;

	igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);

2368
	delay = prandom_u32() % unsolicited_report_interval(ma->idev);
L
Linus Torvalds 已提交
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386

	spin_lock_bh(&ma->mca_lock);
	if (del_timer(&ma->mca_timer)) {
		atomic_dec(&ma->mca_refcnt);
		delay = ma->mca_timer.expires - jiffies;
	}

	if (!mod_timer(&ma->mca_timer, jiffies + delay))
		atomic_inc(&ma->mca_refcnt);
	ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
	spin_unlock_bh(&ma->mca_lock);
}

static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
			    struct inet6_dev *idev)
{
	int err;

W
WANG Cong 已提交
2387
	/* callers have the socket lock and rtnl lock
2388 2389
	 * so no other readers or writers of iml or its sflist
	 */
S
Stephen Hemminger 已提交
2390
	if (!iml->sflist) {
L
Linus Torvalds 已提交
2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402
		/* any-source empty exclude case */
		return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
	}
	err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
		iml->sflist->sl_count, iml->sflist->sl_addr, 0);
	sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
	iml->sflist = NULL;
	return err;
}

static void igmp6_leave_group(struct ifmcaddr6 *ma)
{
2403
	if (mld_in_v1_mode(ma->idev)) {
L
Linus Torvalds 已提交
2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418
		if (ma->mca_flags & MAF_LAST_REPORTER)
			igmp6_send(&ma->mca_addr, ma->idev->dev,
				ICMPV6_MGM_REDUCTION);
	} else {
		mld_add_delrec(ma->idev, ma);
		mld_ifc_event(ma->idev);
	}
}

static void mld_gq_timer_expire(unsigned long data)
{
	struct inet6_dev *idev = (struct inet6_dev *)data;

	idev->mc_gq_running = 0;
	mld_send_report(idev, NULL);
2419
	in6_dev_put(idev);
L
Linus Torvalds 已提交
2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431
}

static void mld_ifc_timer_expire(unsigned long data)
{
	struct inet6_dev *idev = (struct inet6_dev *)data;

	mld_send_cr(idev);
	if (idev->mc_ifc_count) {
		idev->mc_ifc_count--;
		if (idev->mc_ifc_count)
			mld_ifc_start_timer(idev, idev->mc_maxdelay);
	}
2432
	in6_dev_put(idev);
L
Linus Torvalds 已提交
2433 2434 2435 2436
}

static void mld_ifc_event(struct inet6_dev *idev)
{
2437
	if (mld_in_v1_mode(idev))
L
Linus Torvalds 已提交
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447
		return;
	idev->mc_ifc_count = idev->mc_qrv;
	mld_ifc_start_timer(idev, 1);
}


static void igmp6_timer_handler(unsigned long data)
{
	struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;

2448
	if (mld_in_v1_mode(ma->idev))
L
Linus Torvalds 已提交
2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
		igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
	else
		mld_send_report(ma->idev, ma);

	spin_lock(&ma->mca_lock);
	ma->mca_flags |=  MAF_LAST_REPORTER;
	ma->mca_flags &= ~MAF_TIMER_RUNNING;
	spin_unlock(&ma->mca_lock);
	ma_put(ma);
}

2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478
/* Device changing type */

void ipv6_mc_unmap(struct inet6_dev *idev)
{
	struct ifmcaddr6 *i;

	/* Install multicast list, except for all-nodes (already installed) */

	read_lock_bh(&idev->lock);
	for (i = idev->mc_list; i; i = i->next)
		igmp6_group_dropped(i);
	read_unlock_bh(&idev->lock);
}

void ipv6_mc_remap(struct inet6_dev *idev)
{
	ipv6_mc_up(idev);
}

L
Linus Torvalds 已提交
2479 2480 2481 2482 2483 2484 2485 2486 2487
/* Device going down */

void ipv6_mc_down(struct inet6_dev *idev)
{
	struct ifmcaddr6 *i;

	/* Withdraw multicast list */

	read_lock_bh(&idev->lock);
2488 2489 2490
	mld_ifc_stop_timer(idev);
	mld_gq_stop_timer(idev);
	mld_dad_stop_timer(idev);
L
Linus Torvalds 已提交
2491

2492
	for (i = idev->mc_list; i; i = i->next)
L
Linus Torvalds 已提交
2493 2494 2495 2496 2497 2498
		igmp6_group_dropped(i);
	read_unlock_bh(&idev->lock);

	mld_clear_delrec(idev);
}

2499 2500 2501 2502 2503 2504 2505 2506
static void ipv6_mc_reset(struct inet6_dev *idev)
{
	idev->mc_qrv = sysctl_mld_qrv;
	idev->mc_qi = MLD_QI_DEFAULT;
	idev->mc_qri = MLD_QRI_DEFAULT;
	idev->mc_v1_seen = 0;
	idev->mc_maxdelay = unsolicited_report_interval(idev);
}
L
Linus Torvalds 已提交
2507 2508 2509 2510 2511 2512 2513 2514 2515 2516

/* Device going up */

void ipv6_mc_up(struct inet6_dev *idev)
{
	struct ifmcaddr6 *i;

	/* Install multicast list, except for all-nodes (already installed) */

	read_lock_bh(&idev->lock);
2517
	ipv6_mc_reset(idev);
2518
	for (i = idev->mc_list; i; i = i->next)
L
Linus Torvalds 已提交
2519 2520 2521 2522 2523 2524 2525 2526 2527
		igmp6_group_added(i);
	read_unlock_bh(&idev->lock);
}

/* IPv6 device initialization. */

void ipv6_mc_init_dev(struct inet6_dev *idev)
{
	write_lock_bh(&idev->lock);
2528
	spin_lock_init(&idev->mc_lock);
L
Linus Torvalds 已提交
2529
	idev->mc_gq_running = 0;
2530 2531
	setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
			(unsigned long)idev);
L
Linus Torvalds 已提交
2532 2533
	idev->mc_tomb = NULL;
	idev->mc_ifc_count = 0;
2534 2535
	setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
			(unsigned long)idev);
2536 2537
	setup_timer(&idev->mc_dad_timer, mld_dad_timer_expire,
		    (unsigned long)idev);
2538
	ipv6_mc_reset(idev);
L
Linus Torvalds 已提交
2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557
	write_unlock_bh(&idev->lock);
}

/*
 *	Device is about to be destroyed: clean up.
 */

void ipv6_mc_destroy_dev(struct inet6_dev *idev)
{
	struct ifmcaddr6 *i;

	/* Deactivate timers */
	ipv6_mc_down(idev);

	/* Delete all-nodes address. */
	/* We cannot call ipv6_dev_mc_dec() directly, our caller in
	 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
	 * fail.
	 */
2558
	__ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
L
Linus Torvalds 已提交
2559

2560 2561
	if (idev->cnf.forwarding)
		__ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
L
Linus Torvalds 已提交
2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577

	write_lock_bh(&idev->lock);
	while ((i = idev->mc_list) != NULL) {
		idev->mc_list = i->next;
		write_unlock_bh(&idev->lock);

		igmp6_group_dropped(i);
		ma_put(i);

		write_lock_bh(&idev->lock);
	}
	write_unlock_bh(&idev->lock);
}

#ifdef CONFIG_PROC_FS
struct igmp6_mc_iter_state {
2578
	struct seq_net_private p;
L
Linus Torvalds 已提交
2579 2580 2581 2582 2583 2584 2585 2586 2587 2588
	struct net_device *dev;
	struct inet6_dev *idev;
};

#define igmp6_mc_seq_private(seq)	((struct igmp6_mc_iter_state *)(seq)->private)

static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
{
	struct ifmcaddr6 *im = NULL;
	struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2589
	struct net *net = seq_file_net(seq);
L
Linus Torvalds 已提交
2590

2591
	state->idev = NULL;
2592
	for_each_netdev_rcu(net, state->dev) {
L
Linus Torvalds 已提交
2593
		struct inet6_dev *idev;
2594
		idev = __in6_dev_get(state->dev);
L
Linus Torvalds 已提交
2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613
		if (!idev)
			continue;
		read_lock_bh(&idev->lock);
		im = idev->mc_list;
		if (im) {
			state->idev = idev;
			break;
		}
		read_unlock_bh(&idev->lock);
	}
	return im;
}

static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
{
	struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);

	im = im->next;
	while (!im) {
2614
		if (likely(state->idev != NULL))
L
Linus Torvalds 已提交
2615
			read_unlock_bh(&state->idev->lock);
2616 2617

		state->dev = next_net_device_rcu(state->dev);
L
Linus Torvalds 已提交
2618 2619 2620 2621
		if (!state->dev) {
			state->idev = NULL;
			break;
		}
2622
		state->idev = __in6_dev_get(state->dev);
L
Linus Torvalds 已提交
2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640
		if (!state->idev)
			continue;
		read_lock_bh(&state->idev->lock);
		im = state->idev->mc_list;
	}
	return im;
}

static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
{
	struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
	if (im)
		while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
			--pos;
	return pos ? NULL : im;
}

static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2641
	__acquires(RCU)
L
Linus Torvalds 已提交
2642
{
2643
	rcu_read_lock();
L
Linus Torvalds 已提交
2644 2645 2646 2647 2648
	return igmp6_mc_get_idx(seq, *pos);
}

static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
2649 2650
	struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);

L
Linus Torvalds 已提交
2651 2652 2653 2654 2655
	++*pos;
	return im;
}

static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2656
	__releases(RCU)
L
Linus Torvalds 已提交
2657 2658
{
	struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2659

L
Linus Torvalds 已提交
2660 2661 2662 2663 2664
	if (likely(state->idev != NULL)) {
		read_unlock_bh(&state->idev->lock);
		state->idev = NULL;
	}
	state->dev = NULL;
2665
	rcu_read_unlock();
L
Linus Torvalds 已提交
2666 2667 2668 2669 2670 2671 2672 2673
}

static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
{
	struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
	struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);

	seq_printf(seq,
2674
		   "%-4d %-15s %pi6 %5d %08X %ld\n",
L
Linus Torvalds 已提交
2675
		   state->dev->ifindex, state->dev->name,
2676
		   &im->mca_addr,
L
Linus Torvalds 已提交
2677 2678 2679 2680 2681 2682
		   im->mca_users, im->mca_flags,
		   (im->mca_flags&MAF_TIMER_RUNNING) ?
		   jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
	return 0;
}

2683
static const struct seq_operations igmp6_mc_seq_ops = {
L
Linus Torvalds 已提交
2684 2685 2686 2687 2688 2689 2690 2691
	.start	=	igmp6_mc_seq_start,
	.next	=	igmp6_mc_seq_next,
	.stop	=	igmp6_mc_seq_stop,
	.show	=	igmp6_mc_seq_show,
};

static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
{
2692 2693
	return seq_open_net(inode, file, &igmp6_mc_seq_ops,
			    sizeof(struct igmp6_mc_iter_state));
L
Linus Torvalds 已提交
2694 2695
}

2696
static const struct file_operations igmp6_mc_seq_fops = {
L
Linus Torvalds 已提交
2697 2698 2699 2700
	.owner		=	THIS_MODULE,
	.open		=	igmp6_mc_seq_open,
	.read		=	seq_read,
	.llseek		=	seq_lseek,
2701
	.release	=	seq_release_net,
L
Linus Torvalds 已提交
2702 2703 2704
};

struct igmp6_mcf_iter_state {
2705
	struct seq_net_private p;
L
Linus Torvalds 已提交
2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717
	struct net_device *dev;
	struct inet6_dev *idev;
	struct ifmcaddr6 *im;
};

#define igmp6_mcf_seq_private(seq)	((struct igmp6_mcf_iter_state *)(seq)->private)

static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
{
	struct ip6_sf_list *psf = NULL;
	struct ifmcaddr6 *im = NULL;
	struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2718
	struct net *net = seq_file_net(seq);
L
Linus Torvalds 已提交
2719

2720 2721
	state->idev = NULL;
	state->im = NULL;
2722
	for_each_netdev_rcu(net, state->dev) {
L
Linus Torvalds 已提交
2723
		struct inet6_dev *idev;
2724
		idev = __in6_dev_get(state->dev);
L
Linus Torvalds 已提交
2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752
		if (unlikely(idev == NULL))
			continue;
		read_lock_bh(&idev->lock);
		im = idev->mc_list;
		if (likely(im != NULL)) {
			spin_lock_bh(&im->mca_lock);
			psf = im->mca_sources;
			if (likely(psf != NULL)) {
				state->im = im;
				state->idev = idev;
				break;
			}
			spin_unlock_bh(&im->mca_lock);
		}
		read_unlock_bh(&idev->lock);
	}
	return psf;
}

static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
{
	struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);

	psf = psf->sf_next;
	while (!psf) {
		spin_unlock_bh(&state->im->mca_lock);
		state->im = state->im->next;
		while (!state->im) {
2753
			if (likely(state->idev != NULL))
L
Linus Torvalds 已提交
2754
				read_unlock_bh(&state->idev->lock);
2755 2756

			state->dev = next_net_device_rcu(state->dev);
L
Linus Torvalds 已提交
2757 2758 2759 2760
			if (!state->dev) {
				state->idev = NULL;
				goto out;
			}
2761
			state->idev = __in6_dev_get(state->dev);
L
Linus Torvalds 已提交
2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785
			if (!state->idev)
				continue;
			read_lock_bh(&state->idev->lock);
			state->im = state->idev->mc_list;
		}
		if (!state->im)
			break;
		spin_lock_bh(&state->im->mca_lock);
		psf = state->im->mca_sources;
	}
out:
	return psf;
}

static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
{
	struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
	if (psf)
		while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
			--pos;
	return pos ? NULL : psf;
}

static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2786
	__acquires(RCU)
L
Linus Torvalds 已提交
2787
{
2788
	rcu_read_lock();
L
Linus Torvalds 已提交
2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803
	return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
}

static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
	struct ip6_sf_list *psf;
	if (v == SEQ_START_TOKEN)
		psf = igmp6_mcf_get_first(seq);
	else
		psf = igmp6_mcf_get_next(seq, v);
	++*pos;
	return psf;
}

static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2804
	__releases(RCU)
L
Linus Torvalds 已提交
2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815
{
	struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
	if (likely(state->im != NULL)) {
		spin_unlock_bh(&state->im->mca_lock);
		state->im = NULL;
	}
	if (likely(state->idev != NULL)) {
		read_unlock_bh(&state->idev->lock);
		state->idev = NULL;
	}
	state->dev = NULL;
2816
	rcu_read_unlock();
L
Linus Torvalds 已提交
2817 2818 2819 2820 2821 2822 2823 2824
}

static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
{
	struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
	struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);

	if (v == SEQ_START_TOKEN) {
2825
		seq_puts(seq, "Idx Device                Multicast Address                   Source Address    INC    EXC\n");
L
Linus Torvalds 已提交
2826 2827
	} else {
		seq_printf(seq,
2828
			   "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
L
Linus Torvalds 已提交
2829
			   state->dev->ifindex, state->dev->name,
2830 2831
			   &state->im->mca_addr,
			   &psf->sf_addr,
L
Linus Torvalds 已提交
2832 2833 2834 2835 2836 2837
			   psf->sf_count[MCAST_INCLUDE],
			   psf->sf_count[MCAST_EXCLUDE]);
	}
	return 0;
}

2838
static const struct seq_operations igmp6_mcf_seq_ops = {
L
Linus Torvalds 已提交
2839 2840 2841 2842 2843 2844 2845 2846
	.start	=	igmp6_mcf_seq_start,
	.next	=	igmp6_mcf_seq_next,
	.stop	=	igmp6_mcf_seq_stop,
	.show	=	igmp6_mcf_seq_show,
};

static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
{
2847 2848
	return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
			    sizeof(struct igmp6_mcf_iter_state));
L
Linus Torvalds 已提交
2849 2850
}

2851
static const struct file_operations igmp6_mcf_seq_fops = {
L
Linus Torvalds 已提交
2852 2853 2854 2855
	.owner		=	THIS_MODULE,
	.open		=	igmp6_mcf_seq_open,
	.read		=	seq_read,
	.llseek		=	seq_lseek,
2856
	.release	=	seq_release_net,
L
Linus Torvalds 已提交
2857
};
2858

2859
static int __net_init igmp6_proc_init(struct net *net)
2860 2861 2862 2863
{
	int err;

	err = -ENOMEM;
2864
	if (!proc_create("igmp6", S_IRUGO, net->proc_net, &igmp6_mc_seq_fops))
2865
		goto out;
2866 2867
	if (!proc_create("mcfilter6", S_IRUGO, net->proc_net,
			 &igmp6_mcf_seq_fops))
2868 2869 2870 2871 2872 2873 2874
		goto out_proc_net_igmp6;

	err = 0;
out:
	return err;

out_proc_net_igmp6:
2875
	remove_proc_entry("igmp6", net->proc_net);
2876 2877 2878
	goto out;
}

2879
static void __net_exit igmp6_proc_exit(struct net *net)
2880
{
2881 2882
	remove_proc_entry("mcfilter6", net->proc_net);
	remove_proc_entry("igmp6", net->proc_net);
2883 2884
}
#else
2885
static inline int igmp6_proc_init(struct net *net)
2886 2887 2888
{
	return 0;
}
2889
static inline void igmp6_proc_exit(struct net *net)
2890 2891
{
}
L
Linus Torvalds 已提交
2892 2893
#endif

2894
static int __net_init igmp6_net_init(struct net *net)
L
Linus Torvalds 已提交
2895 2896 2897
{
	int err;

2898 2899
	err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
				   SOCK_RAW, IPPROTO_ICMPV6, net);
L
Linus Torvalds 已提交
2900
	if (err < 0) {
2901
		pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
L
Linus Torvalds 已提交
2902
		       err);
2903
		goto out;
L
Linus Torvalds 已提交
2904 2905
	}

2906
	inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
L
Linus Torvalds 已提交
2907

2908 2909 2910 2911 2912 2913 2914 2915
	err = inet_ctl_sock_create(&net->ipv6.mc_autojoin_sk, PF_INET6,
				   SOCK_RAW, IPPROTO_ICMPV6, net);
	if (err < 0) {
		pr_err("Failed to initialize the IGMP6 autojoin socket (err %d)\n",
		       err);
		goto out_sock_create;
	}

2916 2917
	err = igmp6_proc_init(net);
	if (err)
2918 2919 2920
		goto out_sock_create_autojoin;

	return 0;
2921

2922 2923
out_sock_create_autojoin:
	inet_ctl_sock_destroy(net->ipv6.mc_autojoin_sk);
2924
out_sock_create:
2925
	inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2926 2927
out:
	return err;
L
Linus Torvalds 已提交
2928 2929
}

2930
static void __net_exit igmp6_net_exit(struct net *net)
L
Linus Torvalds 已提交
2931
{
2932
	inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2933
	inet_ctl_sock_destroy(net->ipv6.mc_autojoin_sk);
2934
	igmp6_proc_exit(net);
L
Linus Torvalds 已提交
2935
}
2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950

static struct pernet_operations igmp6_net_ops = {
	.init = igmp6_net_init,
	.exit = igmp6_net_exit,
};

int __init igmp6_init(void)
{
	return register_pernet_subsys(&igmp6_net_ops);
}

void igmp6_cleanup(void)
{
	unregister_pernet_subsys(&igmp6_net_ops);
}