xref: /openbmc/linux/net/ipv6/mcast.c (revision fc28ab18)
1 /*
2  *	Multicast support for IPv6
3  *	Linux INET6 implementation
4  *
5  *	Authors:
6  *	Pedro Roque		<roque@di.fc.ul.pt>
7  *
8  *	Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
9  *
10  *	This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15 
16 /* Changes:
17  *
18  *	yoshfuji	: fix format of router-alert option
19  *	YOSHIFUJI Hideaki @USAGI:
20  *		Fixed source address for MLD message based on
21  *		<draft-ietf-magma-mld-source-05.txt>.
22  *	YOSHIFUJI Hideaki @USAGI:
23  *		- Ignore Queries for invalid addresses.
24  *		- MLD for link-local addresses.
25  *	David L Stevens <dlstevens@us.ibm.com>:
26  *		- MLDv2 support
27  */
28 
29 #include <linux/module.h>
30 #include <linux/errno.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/socket.h>
34 #include <linux/sockios.h>
35 #include <linux/jiffies.h>
36 #include <linux/times.h>
37 #include <linux/net.h>
38 #include <linux/in.h>
39 #include <linux/in6.h>
40 #include <linux/netdevice.h>
41 #include <linux/if_arp.h>
42 #include <linux/route.h>
43 #include <linux/init.h>
44 #include <linux/proc_fs.h>
45 #include <linux/seq_file.h>
46 #include <linux/slab.h>
47 #include <linux/pkt_sched.h>
48 #include <net/mld.h>
49 
50 #include <linux/netfilter.h>
51 #include <linux/netfilter_ipv6.h>
52 
53 #include <net/net_namespace.h>
54 #include <net/sock.h>
55 #include <net/snmp.h>
56 
57 #include <net/ipv6.h>
58 #include <net/protocol.h>
59 #include <net/if_inet6.h>
60 #include <net/ndisc.h>
61 #include <net/addrconf.h>
62 #include <net/ip6_route.h>
63 #include <net/inet_common.h>
64 
65 #include <net/ip6_checksum.h>
66 
67 /* Ensure that we have struct in6_addr aligned on 32bit word. */
68 static void *__mld2_query_bugs[] __attribute__((__unused__)) = {
69 	BUILD_BUG_ON_NULL(offsetof(struct mld2_query, mld2q_srcs) % 4),
70 	BUILD_BUG_ON_NULL(offsetof(struct mld2_report, mld2r_grec) % 4),
71 	BUILD_BUG_ON_NULL(offsetof(struct mld2_grec, grec_mca) % 4)
72 };
73 
74 static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
75 
76 static void igmp6_join_group(struct ifmcaddr6 *ma);
77 static void igmp6_leave_group(struct ifmcaddr6 *ma);
78 static void igmp6_timer_handler(unsigned long data);
79 
80 static void mld_gq_timer_expire(unsigned long data);
81 static void mld_ifc_timer_expire(unsigned long data);
82 static void mld_ifc_event(struct inet6_dev *idev);
83 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
84 static void mld_del_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
85 static void mld_clear_delrec(struct inet6_dev *idev);
86 static bool mld_in_v1_mode(const struct inet6_dev *idev);
87 static int sf_setstate(struct ifmcaddr6 *pmc);
88 static void sf_markstate(struct ifmcaddr6 *pmc);
89 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
90 static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
91 			  int sfmode, int sfcount, const struct in6_addr *psfsrc,
92 			  int delta);
93 static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
94 			  int sfmode, int sfcount, const struct in6_addr *psfsrc,
95 			  int delta);
96 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
97 			    struct inet6_dev *idev);
98 
99 #define MLD_QRV_DEFAULT		2
100 /* RFC3810, 9.2. Query Interval */
101 #define MLD_QI_DEFAULT		(125 * HZ)
102 /* RFC3810, 9.3. Query Response Interval */
103 #define MLD_QRI_DEFAULT		(10 * HZ)
104 
105 /* RFC3810, 8.1 Query Version Distinctions */
106 #define MLD_V1_QUERY_LEN	24
107 #define MLD_V2_QUERY_LEN_MIN	28
108 
109 #define IPV6_MLD_MAX_MSF	64
110 
111 int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
112 int sysctl_mld_qrv __read_mostly = MLD_QRV_DEFAULT;
113 
114 /*
115  *	socket join on multicast group
116  */
117 
118 #define for_each_pmc_rcu(np, pmc)				\
119 	for (pmc = rcu_dereference(np->ipv6_mc_list);		\
120 	     pmc != NULL;					\
121 	     pmc = rcu_dereference(pmc->next))
122 
123 static int unsolicited_report_interval(struct inet6_dev *idev)
124 {
125 	int iv;
126 
127 	if (mld_in_v1_mode(idev))
128 		iv = idev->cnf.mldv1_unsolicited_report_interval;
129 	else
130 		iv = idev->cnf.mldv2_unsolicited_report_interval;
131 
132 	return iv > 0 ? iv : 1;
133 }
134 
135 int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
136 {
137 	struct net_device *dev = NULL;
138 	struct ipv6_mc_socklist *mc_lst;
139 	struct ipv6_pinfo *np = inet6_sk(sk);
140 	struct net *net = sock_net(sk);
141 	int err;
142 
143 	ASSERT_RTNL();
144 
145 	if (!ipv6_addr_is_multicast(addr))
146 		return -EINVAL;
147 
148 	rcu_read_lock();
149 	for_each_pmc_rcu(np, mc_lst) {
150 		if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
151 		    ipv6_addr_equal(&mc_lst->addr, addr)) {
152 			rcu_read_unlock();
153 			return -EADDRINUSE;
154 		}
155 	}
156 	rcu_read_unlock();
157 
158 	mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
159 
160 	if (!mc_lst)
161 		return -ENOMEM;
162 
163 	mc_lst->next = NULL;
164 	mc_lst->addr = *addr;
165 
166 	if (ifindex == 0) {
167 		struct rt6_info *rt;
168 		rt = rt6_lookup(net, addr, NULL, 0, 0);
169 		if (rt) {
170 			dev = rt->dst.dev;
171 			ip6_rt_put(rt);
172 		}
173 	} else
174 		dev = __dev_get_by_index(net, ifindex);
175 
176 	if (!dev) {
177 		sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
178 		return -ENODEV;
179 	}
180 
181 	mc_lst->ifindex = dev->ifindex;
182 	mc_lst->sfmode = MCAST_EXCLUDE;
183 	rwlock_init(&mc_lst->sflock);
184 	mc_lst->sflist = NULL;
185 
186 	/*
187 	 *	now add/increase the group membership on the device
188 	 */
189 
190 	err = ipv6_dev_mc_inc(dev, addr);
191 
192 	if (err) {
193 		sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
194 		return err;
195 	}
196 
197 	mc_lst->next = np->ipv6_mc_list;
198 	rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
199 
200 	return 0;
201 }
202 EXPORT_SYMBOL(ipv6_sock_mc_join);
203 
204 /*
205  *	socket leave on multicast group
206  */
207 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
208 {
209 	struct ipv6_pinfo *np = inet6_sk(sk);
210 	struct ipv6_mc_socklist *mc_lst;
211 	struct ipv6_mc_socklist __rcu **lnk;
212 	struct net *net = sock_net(sk);
213 
214 	ASSERT_RTNL();
215 
216 	if (!ipv6_addr_is_multicast(addr))
217 		return -EINVAL;
218 
219 	for (lnk = &np->ipv6_mc_list;
220 	     (mc_lst = rtnl_dereference(*lnk)) != NULL;
221 	      lnk = &mc_lst->next) {
222 		if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
223 		    ipv6_addr_equal(&mc_lst->addr, addr)) {
224 			struct net_device *dev;
225 
226 			*lnk = mc_lst->next;
227 
228 			dev = __dev_get_by_index(net, mc_lst->ifindex);
229 			if (dev) {
230 				struct inet6_dev *idev = __in6_dev_get(dev);
231 
232 				(void) ip6_mc_leave_src(sk, mc_lst, idev);
233 				if (idev)
234 					__ipv6_dev_mc_dec(idev, &mc_lst->addr);
235 			} else
236 				(void) ip6_mc_leave_src(sk, mc_lst, NULL);
237 
238 			atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
239 			kfree_rcu(mc_lst, rcu);
240 			return 0;
241 		}
242 	}
243 
244 	return -EADDRNOTAVAIL;
245 }
246 EXPORT_SYMBOL(ipv6_sock_mc_drop);
247 
248 /* called with rcu_read_lock() */
249 static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
250 					     const struct in6_addr *group,
251 					     int ifindex)
252 {
253 	struct net_device *dev = NULL;
254 	struct inet6_dev *idev = NULL;
255 
256 	if (ifindex == 0) {
257 		struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, 0);
258 
259 		if (rt) {
260 			dev = rt->dst.dev;
261 			ip6_rt_put(rt);
262 		}
263 	} else
264 		dev = dev_get_by_index_rcu(net, ifindex);
265 
266 	if (!dev)
267 		return NULL;
268 	idev = __in6_dev_get(dev);
269 	if (!idev)
270 		return NULL;
271 	read_lock_bh(&idev->lock);
272 	if (idev->dead) {
273 		read_unlock_bh(&idev->lock);
274 		return NULL;
275 	}
276 	return idev;
277 }
278 
279 void __ipv6_sock_mc_close(struct sock *sk)
280 {
281 	struct ipv6_pinfo *np = inet6_sk(sk);
282 	struct ipv6_mc_socklist *mc_lst;
283 	struct net *net = sock_net(sk);
284 
285 	ASSERT_RTNL();
286 
287 	while ((mc_lst = rtnl_dereference(np->ipv6_mc_list)) != NULL) {
288 		struct net_device *dev;
289 
290 		np->ipv6_mc_list = mc_lst->next;
291 
292 		dev = __dev_get_by_index(net, mc_lst->ifindex);
293 		if (dev) {
294 			struct inet6_dev *idev = __in6_dev_get(dev);
295 
296 			(void) ip6_mc_leave_src(sk, mc_lst, idev);
297 			if (idev)
298 				__ipv6_dev_mc_dec(idev, &mc_lst->addr);
299 		} else
300 			(void) ip6_mc_leave_src(sk, mc_lst, NULL);
301 
302 		atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
303 		kfree_rcu(mc_lst, rcu);
304 	}
305 }
306 
307 void ipv6_sock_mc_close(struct sock *sk)
308 {
309 	struct ipv6_pinfo *np = inet6_sk(sk);
310 
311 	if (!rcu_access_pointer(np->ipv6_mc_list))
312 		return;
313 	rtnl_lock();
314 	__ipv6_sock_mc_close(sk);
315 	rtnl_unlock();
316 }
317 
318 int ip6_mc_source(int add, int omode, struct sock *sk,
319 	struct group_source_req *pgsr)
320 {
321 	struct in6_addr *source, *group;
322 	struct ipv6_mc_socklist *pmc;
323 	struct inet6_dev *idev;
324 	struct ipv6_pinfo *inet6 = inet6_sk(sk);
325 	struct ip6_sf_socklist *psl;
326 	struct net *net = sock_net(sk);
327 	int i, j, rv;
328 	int leavegroup = 0;
329 	int pmclocked = 0;
330 	int err;
331 
332 	source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
333 	group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
334 
335 	if (!ipv6_addr_is_multicast(group))
336 		return -EINVAL;
337 
338 	rcu_read_lock();
339 	idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface);
340 	if (!idev) {
341 		rcu_read_unlock();
342 		return -ENODEV;
343 	}
344 
345 	err = -EADDRNOTAVAIL;
346 
347 	for_each_pmc_rcu(inet6, pmc) {
348 		if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
349 			continue;
350 		if (ipv6_addr_equal(&pmc->addr, group))
351 			break;
352 	}
353 	if (!pmc) {		/* must have a prior join */
354 		err = -EINVAL;
355 		goto done;
356 	}
357 	/* if a source filter was set, must be the same mode as before */
358 	if (pmc->sflist) {
359 		if (pmc->sfmode != omode) {
360 			err = -EINVAL;
361 			goto done;
362 		}
363 	} else if (pmc->sfmode != omode) {
364 		/* allow mode switches for empty-set filters */
365 		ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
366 		ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
367 		pmc->sfmode = omode;
368 	}
369 
370 	write_lock(&pmc->sflock);
371 	pmclocked = 1;
372 
373 	psl = pmc->sflist;
374 	if (!add) {
375 		if (!psl)
376 			goto done;	/* err = -EADDRNOTAVAIL */
377 		rv = !0;
378 		for (i = 0; i < psl->sl_count; i++) {
379 			rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
380 			if (rv == 0)
381 				break;
382 		}
383 		if (rv)		/* source not found */
384 			goto done;	/* err = -EADDRNOTAVAIL */
385 
386 		/* special case - (INCLUDE, empty) == LEAVE_GROUP */
387 		if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
388 			leavegroup = 1;
389 			goto done;
390 		}
391 
392 		/* update the interface filter */
393 		ip6_mc_del_src(idev, group, omode, 1, source, 1);
394 
395 		for (j = i+1; j < psl->sl_count; j++)
396 			psl->sl_addr[j-1] = psl->sl_addr[j];
397 		psl->sl_count--;
398 		err = 0;
399 		goto done;
400 	}
401 	/* else, add a new source to the filter */
402 
403 	if (psl && psl->sl_count >= sysctl_mld_max_msf) {
404 		err = -ENOBUFS;
405 		goto done;
406 	}
407 	if (!psl || psl->sl_count == psl->sl_max) {
408 		struct ip6_sf_socklist *newpsl;
409 		int count = IP6_SFBLOCK;
410 
411 		if (psl)
412 			count += psl->sl_max;
413 		newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
414 		if (!newpsl) {
415 			err = -ENOBUFS;
416 			goto done;
417 		}
418 		newpsl->sl_max = count;
419 		newpsl->sl_count = count - IP6_SFBLOCK;
420 		if (psl) {
421 			for (i = 0; i < psl->sl_count; i++)
422 				newpsl->sl_addr[i] = psl->sl_addr[i];
423 			sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
424 		}
425 		pmc->sflist = psl = newpsl;
426 	}
427 	rv = 1;	/* > 0 for insert logic below if sl_count is 0 */
428 	for (i = 0; i < psl->sl_count; i++) {
429 		rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
430 		if (rv == 0) /* There is an error in the address. */
431 			goto done;
432 	}
433 	for (j = psl->sl_count-1; j >= i; j--)
434 		psl->sl_addr[j+1] = psl->sl_addr[j];
435 	psl->sl_addr[i] = *source;
436 	psl->sl_count++;
437 	err = 0;
438 	/* update the interface list */
439 	ip6_mc_add_src(idev, group, omode, 1, source, 1);
440 done:
441 	if (pmclocked)
442 		write_unlock(&pmc->sflock);
443 	read_unlock_bh(&idev->lock);
444 	rcu_read_unlock();
445 	if (leavegroup)
446 		err = ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
447 	return err;
448 }
449 
450 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
451 {
452 	const struct in6_addr *group;
453 	struct ipv6_mc_socklist *pmc;
454 	struct inet6_dev *idev;
455 	struct ipv6_pinfo *inet6 = inet6_sk(sk);
456 	struct ip6_sf_socklist *newpsl, *psl;
457 	struct net *net = sock_net(sk);
458 	int leavegroup = 0;
459 	int i, err;
460 
461 	group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
462 
463 	if (!ipv6_addr_is_multicast(group))
464 		return -EINVAL;
465 	if (gsf->gf_fmode != MCAST_INCLUDE &&
466 	    gsf->gf_fmode != MCAST_EXCLUDE)
467 		return -EINVAL;
468 
469 	rcu_read_lock();
470 	idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
471 
472 	if (!idev) {
473 		rcu_read_unlock();
474 		return -ENODEV;
475 	}
476 
477 	err = 0;
478 
479 	if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
480 		leavegroup = 1;
481 		goto done;
482 	}
483 
484 	for_each_pmc_rcu(inet6, pmc) {
485 		if (pmc->ifindex != gsf->gf_interface)
486 			continue;
487 		if (ipv6_addr_equal(&pmc->addr, group))
488 			break;
489 	}
490 	if (!pmc) {		/* must have a prior join */
491 		err = -EINVAL;
492 		goto done;
493 	}
494 	if (gsf->gf_numsrc) {
495 		newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
496 							  GFP_ATOMIC);
497 		if (!newpsl) {
498 			err = -ENOBUFS;
499 			goto done;
500 		}
501 		newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
502 		for (i = 0; i < newpsl->sl_count; ++i) {
503 			struct sockaddr_in6 *psin6;
504 
505 			psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
506 			newpsl->sl_addr[i] = psin6->sin6_addr;
507 		}
508 		err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
509 			newpsl->sl_count, newpsl->sl_addr, 0);
510 		if (err) {
511 			sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
512 			goto done;
513 		}
514 	} else {
515 		newpsl = NULL;
516 		(void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
517 	}
518 
519 	write_lock(&pmc->sflock);
520 	psl = pmc->sflist;
521 	if (psl) {
522 		(void) ip6_mc_del_src(idev, group, pmc->sfmode,
523 			psl->sl_count, psl->sl_addr, 0);
524 		sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
525 	} else
526 		(void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
527 	pmc->sflist = newpsl;
528 	pmc->sfmode = gsf->gf_fmode;
529 	write_unlock(&pmc->sflock);
530 	err = 0;
531 done:
532 	read_unlock_bh(&idev->lock);
533 	rcu_read_unlock();
534 	if (leavegroup)
535 		err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
536 	return err;
537 }
538 
539 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
540 	struct group_filter __user *optval, int __user *optlen)
541 {
542 	int err, i, count, copycount;
543 	const struct in6_addr *group;
544 	struct ipv6_mc_socklist *pmc;
545 	struct inet6_dev *idev;
546 	struct ipv6_pinfo *inet6 = inet6_sk(sk);
547 	struct ip6_sf_socklist *psl;
548 	struct net *net = sock_net(sk);
549 
550 	group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
551 
552 	if (!ipv6_addr_is_multicast(group))
553 		return -EINVAL;
554 
555 	rcu_read_lock();
556 	idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
557 
558 	if (!idev) {
559 		rcu_read_unlock();
560 		return -ENODEV;
561 	}
562 
563 	err = -EADDRNOTAVAIL;
564 	/* changes to the ipv6_mc_list require the socket lock and
565 	 * rtnl lock. We have the socket lock and rcu read lock,
566 	 * so reading the list is safe.
567 	 */
568 
569 	for_each_pmc_rcu(inet6, pmc) {
570 		if (pmc->ifindex != gsf->gf_interface)
571 			continue;
572 		if (ipv6_addr_equal(group, &pmc->addr))
573 			break;
574 	}
575 	if (!pmc)		/* must have a prior join */
576 		goto done;
577 	gsf->gf_fmode = pmc->sfmode;
578 	psl = pmc->sflist;
579 	count = psl ? psl->sl_count : 0;
580 	read_unlock_bh(&idev->lock);
581 	rcu_read_unlock();
582 
583 	copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
584 	gsf->gf_numsrc = count;
585 	if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
586 	    copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
587 		return -EFAULT;
588 	}
589 	/* changes to psl require the socket lock, and a write lock
590 	 * on pmc->sflock. We have the socket lock so reading here is safe.
591 	 */
592 	for (i = 0; i < copycount; i++) {
593 		struct sockaddr_in6 *psin6;
594 		struct sockaddr_storage ss;
595 
596 		psin6 = (struct sockaddr_in6 *)&ss;
597 		memset(&ss, 0, sizeof(ss));
598 		psin6->sin6_family = AF_INET6;
599 		psin6->sin6_addr = psl->sl_addr[i];
600 		if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
601 			return -EFAULT;
602 	}
603 	return 0;
604 done:
605 	read_unlock_bh(&idev->lock);
606 	rcu_read_unlock();
607 	return err;
608 }
609 
610 bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
611 		    const struct in6_addr *src_addr)
612 {
613 	struct ipv6_pinfo *np = inet6_sk(sk);
614 	struct ipv6_mc_socklist *mc;
615 	struct ip6_sf_socklist *psl;
616 	bool rv = true;
617 
618 	rcu_read_lock();
619 	for_each_pmc_rcu(np, mc) {
620 		if (ipv6_addr_equal(&mc->addr, mc_addr))
621 			break;
622 	}
623 	if (!mc) {
624 		rcu_read_unlock();
625 		return true;
626 	}
627 	read_lock(&mc->sflock);
628 	psl = mc->sflist;
629 	if (!psl) {
630 		rv = mc->sfmode == MCAST_EXCLUDE;
631 	} else {
632 		int i;
633 
634 		for (i = 0; i < psl->sl_count; i++) {
635 			if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
636 				break;
637 		}
638 		if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
639 			rv = false;
640 		if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
641 			rv = false;
642 	}
643 	read_unlock(&mc->sflock);
644 	rcu_read_unlock();
645 
646 	return rv;
647 }
648 
649 static void igmp6_group_added(struct ifmcaddr6 *mc)
650 {
651 	struct net_device *dev = mc->idev->dev;
652 	char buf[MAX_ADDR_LEN];
653 
654 	if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
655 	    IPV6_ADDR_SCOPE_LINKLOCAL)
656 		return;
657 
658 	spin_lock_bh(&mc->mca_lock);
659 	if (!(mc->mca_flags&MAF_LOADED)) {
660 		mc->mca_flags |= MAF_LOADED;
661 		if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
662 			dev_mc_add(dev, buf);
663 	}
664 	spin_unlock_bh(&mc->mca_lock);
665 
666 	if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
667 		return;
668 
669 	if (mld_in_v1_mode(mc->idev)) {
670 		igmp6_join_group(mc);
671 		return;
672 	}
673 	/* else v2 */
674 
675 	mc->mca_crcount = mc->idev->mc_qrv;
676 	mld_ifc_event(mc->idev);
677 }
678 
679 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
680 {
681 	struct net_device *dev = mc->idev->dev;
682 	char buf[MAX_ADDR_LEN];
683 
684 	if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
685 	    IPV6_ADDR_SCOPE_LINKLOCAL)
686 		return;
687 
688 	spin_lock_bh(&mc->mca_lock);
689 	if (mc->mca_flags&MAF_LOADED) {
690 		mc->mca_flags &= ~MAF_LOADED;
691 		if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
692 			dev_mc_del(dev, buf);
693 	}
694 
695 	spin_unlock_bh(&mc->mca_lock);
696 	if (mc->mca_flags & MAF_NOREPORT)
697 		return;
698 
699 	if (!mc->idev->dead)
700 		igmp6_leave_group(mc);
701 
702 	spin_lock_bh(&mc->mca_lock);
703 	if (del_timer(&mc->mca_timer))
704 		atomic_dec(&mc->mca_refcnt);
705 	spin_unlock_bh(&mc->mca_lock);
706 }
707 
708 /*
709  * deleted ifmcaddr6 manipulation
710  */
711 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
712 {
713 	struct ifmcaddr6 *pmc;
714 
715 	/* this is an "ifmcaddr6" for convenience; only the fields below
716 	 * are actually used. In particular, the refcnt and users are not
717 	 * used for management of the delete list. Using the same structure
718 	 * for deleted items allows change reports to use common code with
719 	 * non-deleted or query-response MCA's.
720 	 */
721 	pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
722 	if (!pmc)
723 		return;
724 
725 	spin_lock_bh(&im->mca_lock);
726 	spin_lock_init(&pmc->mca_lock);
727 	pmc->idev = im->idev;
728 	in6_dev_hold(idev);
729 	pmc->mca_addr = im->mca_addr;
730 	pmc->mca_crcount = idev->mc_qrv;
731 	pmc->mca_sfmode = im->mca_sfmode;
732 	if (pmc->mca_sfmode == MCAST_INCLUDE) {
733 		struct ip6_sf_list *psf;
734 
735 		pmc->mca_tomb = im->mca_tomb;
736 		pmc->mca_sources = im->mca_sources;
737 		im->mca_tomb = im->mca_sources = NULL;
738 		for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
739 			psf->sf_crcount = pmc->mca_crcount;
740 	}
741 	spin_unlock_bh(&im->mca_lock);
742 
743 	spin_lock_bh(&idev->mc_lock);
744 	pmc->next = idev->mc_tomb;
745 	idev->mc_tomb = pmc;
746 	spin_unlock_bh(&idev->mc_lock);
747 }
748 
749 static void mld_del_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
750 {
751 	struct ifmcaddr6 *pmc, *pmc_prev;
752 	struct ip6_sf_list *psf;
753 	struct in6_addr *pmca = &im->mca_addr;
754 
755 	spin_lock_bh(&idev->mc_lock);
756 	pmc_prev = NULL;
757 	for (pmc = idev->mc_tomb; pmc; pmc = pmc->next) {
758 		if (ipv6_addr_equal(&pmc->mca_addr, pmca))
759 			break;
760 		pmc_prev = pmc;
761 	}
762 	if (pmc) {
763 		if (pmc_prev)
764 			pmc_prev->next = pmc->next;
765 		else
766 			idev->mc_tomb = pmc->next;
767 	}
768 	spin_unlock_bh(&idev->mc_lock);
769 
770 	spin_lock_bh(&im->mca_lock);
771 	if (pmc) {
772 		im->idev = pmc->idev;
773 		im->mca_crcount = idev->mc_qrv;
774 		im->mca_sfmode = pmc->mca_sfmode;
775 		if (pmc->mca_sfmode == MCAST_INCLUDE) {
776 			im->mca_tomb = pmc->mca_tomb;
777 			im->mca_sources = pmc->mca_sources;
778 			for (psf = im->mca_sources; psf; psf = psf->sf_next)
779 				psf->sf_crcount = im->mca_crcount;
780 		}
781 		in6_dev_put(pmc->idev);
782 	}
783 	spin_unlock_bh(&im->mca_lock);
784 }
785 
786 static void mld_clear_delrec(struct inet6_dev *idev)
787 {
788 	struct ifmcaddr6 *pmc, *nextpmc;
789 
790 	spin_lock_bh(&idev->mc_lock);
791 	pmc = idev->mc_tomb;
792 	idev->mc_tomb = NULL;
793 	spin_unlock_bh(&idev->mc_lock);
794 
795 	for (; pmc; pmc = nextpmc) {
796 		nextpmc = pmc->next;
797 		ip6_mc_clear_src(pmc);
798 		in6_dev_put(pmc->idev);
799 		kfree(pmc);
800 	}
801 
802 	/* clear dead sources, too */
803 	read_lock_bh(&idev->lock);
804 	for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
805 		struct ip6_sf_list *psf, *psf_next;
806 
807 		spin_lock_bh(&pmc->mca_lock);
808 		psf = pmc->mca_tomb;
809 		pmc->mca_tomb = NULL;
810 		spin_unlock_bh(&pmc->mca_lock);
811 		for (; psf; psf = psf_next) {
812 			psf_next = psf->sf_next;
813 			kfree(psf);
814 		}
815 	}
816 	read_unlock_bh(&idev->lock);
817 }
818 
819 static void mca_get(struct ifmcaddr6 *mc)
820 {
821 	atomic_inc(&mc->mca_refcnt);
822 }
823 
824 static void ma_put(struct ifmcaddr6 *mc)
825 {
826 	if (atomic_dec_and_test(&mc->mca_refcnt)) {
827 		in6_dev_put(mc->idev);
828 		kfree(mc);
829 	}
830 }
831 
832 static struct ifmcaddr6 *mca_alloc(struct inet6_dev *idev,
833 				   const struct in6_addr *addr)
834 {
835 	struct ifmcaddr6 *mc;
836 
837 	mc = kzalloc(sizeof(*mc), GFP_ATOMIC);
838 	if (!mc)
839 		return NULL;
840 
841 	setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
842 
843 	mc->mca_addr = *addr;
844 	mc->idev = idev; /* reference taken by caller */
845 	mc->mca_users = 1;
846 	/* mca_stamp should be updated upon changes */
847 	mc->mca_cstamp = mc->mca_tstamp = jiffies;
848 	atomic_set(&mc->mca_refcnt, 1);
849 	spin_lock_init(&mc->mca_lock);
850 
851 	/* initial mode is (EX, empty) */
852 	mc->mca_sfmode = MCAST_EXCLUDE;
853 	mc->mca_sfcount[MCAST_EXCLUDE] = 1;
854 
855 	if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
856 	    IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
857 		mc->mca_flags |= MAF_NOREPORT;
858 
859 	return mc;
860 }
861 
862 /*
863  *	device multicast group inc (add if not found)
864  */
865 int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
866 {
867 	struct ifmcaddr6 *mc;
868 	struct inet6_dev *idev;
869 
870 	ASSERT_RTNL();
871 
872 	/* we need to take a reference on idev */
873 	idev = in6_dev_get(dev);
874 
875 	if (!idev)
876 		return -EINVAL;
877 
878 	write_lock_bh(&idev->lock);
879 	if (idev->dead) {
880 		write_unlock_bh(&idev->lock);
881 		in6_dev_put(idev);
882 		return -ENODEV;
883 	}
884 
885 	for (mc = idev->mc_list; mc; mc = mc->next) {
886 		if (ipv6_addr_equal(&mc->mca_addr, addr)) {
887 			mc->mca_users++;
888 			write_unlock_bh(&idev->lock);
889 			ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
890 				NULL, 0);
891 			in6_dev_put(idev);
892 			return 0;
893 		}
894 	}
895 
896 	mc = mca_alloc(idev, addr);
897 	if (!mc) {
898 		write_unlock_bh(&idev->lock);
899 		in6_dev_put(idev);
900 		return -ENOMEM;
901 	}
902 
903 	mc->next = idev->mc_list;
904 	idev->mc_list = mc;
905 
906 	/* Hold this for the code below before we unlock,
907 	 * it is already exposed via idev->mc_list.
908 	 */
909 	mca_get(mc);
910 	write_unlock_bh(&idev->lock);
911 
912 	mld_del_delrec(idev, mc);
913 	igmp6_group_added(mc);
914 	ma_put(mc);
915 	return 0;
916 }
917 
918 /*
919  *	device multicast group del
920  */
921 int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
922 {
923 	struct ifmcaddr6 *ma, **map;
924 
925 	ASSERT_RTNL();
926 
927 	write_lock_bh(&idev->lock);
928 	for (map = &idev->mc_list; (ma = *map) != NULL; map = &ma->next) {
929 		if (ipv6_addr_equal(&ma->mca_addr, addr)) {
930 			if (--ma->mca_users == 0) {
931 				*map = ma->next;
932 				write_unlock_bh(&idev->lock);
933 
934 				igmp6_group_dropped(ma);
935 				ip6_mc_clear_src(ma);
936 
937 				ma_put(ma);
938 				return 0;
939 			}
940 			write_unlock_bh(&idev->lock);
941 			return 0;
942 		}
943 	}
944 	write_unlock_bh(&idev->lock);
945 
946 	return -ENOENT;
947 }
948 
949 int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
950 {
951 	struct inet6_dev *idev;
952 	int err;
953 
954 	ASSERT_RTNL();
955 
956 	idev = __in6_dev_get(dev);
957 	if (!idev)
958 		err = -ENODEV;
959 	else
960 		err = __ipv6_dev_mc_dec(idev, addr);
961 
962 	return err;
963 }
964 
965 /*
966  *	check if the interface/address pair is valid
967  */
968 bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
969 			 const struct in6_addr *src_addr)
970 {
971 	struct inet6_dev *idev;
972 	struct ifmcaddr6 *mc;
973 	bool rv = false;
974 
975 	rcu_read_lock();
976 	idev = __in6_dev_get(dev);
977 	if (idev) {
978 		read_lock_bh(&idev->lock);
979 		for (mc = idev->mc_list; mc; mc = mc->next) {
980 			if (ipv6_addr_equal(&mc->mca_addr, group))
981 				break;
982 		}
983 		if (mc) {
984 			if (src_addr && !ipv6_addr_any(src_addr)) {
985 				struct ip6_sf_list *psf;
986 
987 				spin_lock_bh(&mc->mca_lock);
988 				for (psf = mc->mca_sources; psf; psf = psf->sf_next) {
989 					if (ipv6_addr_equal(&psf->sf_addr, src_addr))
990 						break;
991 				}
992 				if (psf)
993 					rv = psf->sf_count[MCAST_INCLUDE] ||
994 						psf->sf_count[MCAST_EXCLUDE] !=
995 						mc->mca_sfcount[MCAST_EXCLUDE];
996 				else
997 					rv = mc->mca_sfcount[MCAST_EXCLUDE] != 0;
998 				spin_unlock_bh(&mc->mca_lock);
999 			} else
1000 				rv = true; /* don't filter unspecified source */
1001 		}
1002 		read_unlock_bh(&idev->lock);
1003 	}
1004 	rcu_read_unlock();
1005 	return rv;
1006 }
1007 
1008 static void mld_gq_start_timer(struct inet6_dev *idev)
1009 {
1010 	unsigned long tv = prandom_u32() % idev->mc_maxdelay;
1011 
1012 	idev->mc_gq_running = 1;
1013 	if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1014 		in6_dev_hold(idev);
1015 }
1016 
1017 static void mld_gq_stop_timer(struct inet6_dev *idev)
1018 {
1019 	idev->mc_gq_running = 0;
1020 	if (del_timer(&idev->mc_gq_timer))
1021 		__in6_dev_put(idev);
1022 }
1023 
1024 static void mld_ifc_start_timer(struct inet6_dev *idev, unsigned long delay)
1025 {
1026 	unsigned long tv = prandom_u32() % delay;
1027 
1028 	if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1029 		in6_dev_hold(idev);
1030 }
1031 
1032 static void mld_ifc_stop_timer(struct inet6_dev *idev)
1033 {
1034 	idev->mc_ifc_count = 0;
1035 	if (del_timer(&idev->mc_ifc_timer))
1036 		__in6_dev_put(idev);
1037 }
1038 
1039 static void mld_dad_start_timer(struct inet6_dev *idev, unsigned long delay)
1040 {
1041 	unsigned long tv = prandom_u32() % delay;
1042 
1043 	if (!mod_timer(&idev->mc_dad_timer, jiffies+tv+2))
1044 		in6_dev_hold(idev);
1045 }
1046 
1047 static void mld_dad_stop_timer(struct inet6_dev *idev)
1048 {
1049 	if (del_timer(&idev->mc_dad_timer))
1050 		__in6_dev_put(idev);
1051 }
1052 
1053 /*
1054  *	IGMP handling (alias multicast ICMPv6 messages)
1055  */
1056 
1057 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1058 {
1059 	unsigned long delay = resptime;
1060 
1061 	/* Do not start timer for these addresses */
1062 	if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1063 	    IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1064 		return;
1065 
1066 	if (del_timer(&ma->mca_timer)) {
1067 		atomic_dec(&ma->mca_refcnt);
1068 		delay = ma->mca_timer.expires - jiffies;
1069 	}
1070 
1071 	if (delay >= resptime)
1072 		delay = prandom_u32() % resptime;
1073 
1074 	ma->mca_timer.expires = jiffies + delay;
1075 	if (!mod_timer(&ma->mca_timer, jiffies + delay))
1076 		atomic_inc(&ma->mca_refcnt);
1077 	ma->mca_flags |= MAF_TIMER_RUNNING;
1078 }
1079 
1080 /* mark EXCLUDE-mode sources */
1081 static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1082 			     const struct in6_addr *srcs)
1083 {
1084 	struct ip6_sf_list *psf;
1085 	int i, scount;
1086 
1087 	scount = 0;
1088 	for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
1089 		if (scount == nsrcs)
1090 			break;
1091 		for (i = 0; i < nsrcs; i++) {
1092 			/* skip inactive filters */
1093 			if (psf->sf_count[MCAST_INCLUDE] ||
1094 			    pmc->mca_sfcount[MCAST_EXCLUDE] !=
1095 			    psf->sf_count[MCAST_EXCLUDE])
1096 				break;
1097 			if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1098 				scount++;
1099 				break;
1100 			}
1101 		}
1102 	}
1103 	pmc->mca_flags &= ~MAF_GSQUERY;
1104 	if (scount == nsrcs)	/* all sources excluded */
1105 		return false;
1106 	return true;
1107 }
1108 
1109 static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1110 			    const struct in6_addr *srcs)
1111 {
1112 	struct ip6_sf_list *psf;
1113 	int i, scount;
1114 
1115 	if (pmc->mca_sfmode == MCAST_EXCLUDE)
1116 		return mld_xmarksources(pmc, nsrcs, srcs);
1117 
1118 	/* mark INCLUDE-mode sources */
1119 
1120 	scount = 0;
1121 	for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
1122 		if (scount == nsrcs)
1123 			break;
1124 		for (i = 0; i < nsrcs; i++) {
1125 			if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1126 				psf->sf_gsresp = 1;
1127 				scount++;
1128 				break;
1129 			}
1130 		}
1131 	}
1132 	if (!scount) {
1133 		pmc->mca_flags &= ~MAF_GSQUERY;
1134 		return false;
1135 	}
1136 	pmc->mca_flags |= MAF_GSQUERY;
1137 	return true;
1138 }
1139 
1140 static int mld_force_mld_version(const struct inet6_dev *idev)
1141 {
1142 	/* Normally, both are 0 here. If enforcement to a particular is
1143 	 * being used, individual device enforcement will have a lower
1144 	 * precedence over 'all' device (.../conf/all/force_mld_version).
1145 	 */
1146 
1147 	if (dev_net(idev->dev)->ipv6.devconf_all->force_mld_version != 0)
1148 		return dev_net(idev->dev)->ipv6.devconf_all->force_mld_version;
1149 	else
1150 		return idev->cnf.force_mld_version;
1151 }
1152 
1153 static bool mld_in_v2_mode_only(const struct inet6_dev *idev)
1154 {
1155 	return mld_force_mld_version(idev) == 2;
1156 }
1157 
1158 static bool mld_in_v1_mode_only(const struct inet6_dev *idev)
1159 {
1160 	return mld_force_mld_version(idev) == 1;
1161 }
1162 
1163 static bool mld_in_v1_mode(const struct inet6_dev *idev)
1164 {
1165 	if (mld_in_v2_mode_only(idev))
1166 		return false;
1167 	if (mld_in_v1_mode_only(idev))
1168 		return true;
1169 	if (idev->mc_v1_seen && time_before(jiffies, idev->mc_v1_seen))
1170 		return true;
1171 
1172 	return false;
1173 }
1174 
1175 static void mld_set_v1_mode(struct inet6_dev *idev)
1176 {
1177 	/* RFC3810, relevant sections:
1178 	 *  - 9.1. Robustness Variable
1179 	 *  - 9.2. Query Interval
1180 	 *  - 9.3. Query Response Interval
1181 	 *  - 9.12. Older Version Querier Present Timeout
1182 	 */
1183 	unsigned long switchback;
1184 
1185 	switchback = (idev->mc_qrv * idev->mc_qi) + idev->mc_qri;
1186 
1187 	idev->mc_v1_seen = jiffies + switchback;
1188 }
1189 
1190 static void mld_update_qrv(struct inet6_dev *idev,
1191 			   const struct mld2_query *mlh2)
1192 {
1193 	/* RFC3810, relevant sections:
1194 	 *  - 5.1.8. QRV (Querier's Robustness Variable)
1195 	 *  - 9.1. Robustness Variable
1196 	 */
1197 
1198 	/* The value of the Robustness Variable MUST NOT be zero,
1199 	 * and SHOULD NOT be one. Catch this here if we ever run
1200 	 * into such a case in future.
1201 	 */
1202 	const int min_qrv = min(MLD_QRV_DEFAULT, sysctl_mld_qrv);
1203 	WARN_ON(idev->mc_qrv == 0);
1204 
1205 	if (mlh2->mld2q_qrv > 0)
1206 		idev->mc_qrv = mlh2->mld2q_qrv;
1207 
1208 	if (unlikely(idev->mc_qrv < min_qrv)) {
1209 		net_warn_ratelimited("IPv6: MLD: clamping QRV from %u to %u!\n",
1210 				     idev->mc_qrv, min_qrv);
1211 		idev->mc_qrv = min_qrv;
1212 	}
1213 }
1214 
1215 static void mld_update_qi(struct inet6_dev *idev,
1216 			  const struct mld2_query *mlh2)
1217 {
1218 	/* RFC3810, relevant sections:
1219 	 *  - 5.1.9. QQIC (Querier's Query Interval Code)
1220 	 *  - 9.2. Query Interval
1221 	 *  - 9.12. Older Version Querier Present Timeout
1222 	 *    (the [Query Interval] in the last Query received)
1223 	 */
1224 	unsigned long mc_qqi;
1225 
1226 	if (mlh2->mld2q_qqic < 128) {
1227 		mc_qqi = mlh2->mld2q_qqic;
1228 	} else {
1229 		unsigned long mc_man, mc_exp;
1230 
1231 		mc_exp = MLDV2_QQIC_EXP(mlh2->mld2q_qqic);
1232 		mc_man = MLDV2_QQIC_MAN(mlh2->mld2q_qqic);
1233 
1234 		mc_qqi = (mc_man | 0x10) << (mc_exp + 3);
1235 	}
1236 
1237 	idev->mc_qi = mc_qqi * HZ;
1238 }
1239 
1240 static void mld_update_qri(struct inet6_dev *idev,
1241 			   const struct mld2_query *mlh2)
1242 {
1243 	/* RFC3810, relevant sections:
1244 	 *  - 5.1.3. Maximum Response Code
1245 	 *  - 9.3. Query Response Interval
1246 	 */
1247 	idev->mc_qri = msecs_to_jiffies(mldv2_mrc(mlh2));
1248 }
1249 
1250 static int mld_process_v1(struct inet6_dev *idev, struct mld_msg *mld,
1251 			  unsigned long *max_delay, bool v1_query)
1252 {
1253 	unsigned long mldv1_md;
1254 
1255 	/* Ignore v1 queries */
1256 	if (mld_in_v2_mode_only(idev))
1257 		return -EINVAL;
1258 
1259 	mldv1_md = ntohs(mld->mld_maxdelay);
1260 
1261 	/* When in MLDv1 fallback and a MLDv2 router start-up being
1262 	 * unaware of current MLDv1 operation, the MRC == MRD mapping
1263 	 * only works when the exponential algorithm is not being
1264 	 * used (as MLDv1 is unaware of such things).
1265 	 *
1266 	 * According to the RFC author, the MLDv2 implementations
1267 	 * he's aware of all use a MRC < 32768 on start up queries.
1268 	 *
1269 	 * Thus, should we *ever* encounter something else larger
1270 	 * than that, just assume the maximum possible within our
1271 	 * reach.
1272 	 */
1273 	if (!v1_query)
1274 		mldv1_md = min(mldv1_md, MLDV1_MRD_MAX_COMPAT);
1275 
1276 	*max_delay = max(msecs_to_jiffies(mldv1_md), 1UL);
1277 
1278 	/* MLDv1 router present: we need to go into v1 mode *only*
1279 	 * when an MLDv1 query is received as per section 9.12. of
1280 	 * RFC3810! And we know from RFC2710 section 3.7 that MLDv1
1281 	 * queries MUST be of exactly 24 octets.
1282 	 */
1283 	if (v1_query)
1284 		mld_set_v1_mode(idev);
1285 
1286 	/* cancel MLDv2 report timer */
1287 	mld_gq_stop_timer(idev);
1288 	/* cancel the interface change timer */
1289 	mld_ifc_stop_timer(idev);
1290 	/* clear deleted report items */
1291 	mld_clear_delrec(idev);
1292 
1293 	return 0;
1294 }
1295 
1296 static int mld_process_v2(struct inet6_dev *idev, struct mld2_query *mld,
1297 			  unsigned long *max_delay)
1298 {
1299 	*max_delay = max(msecs_to_jiffies(mldv2_mrc(mld)), 1UL);
1300 
1301 	mld_update_qrv(idev, mld);
1302 	mld_update_qi(idev, mld);
1303 	mld_update_qri(idev, mld);
1304 
1305 	idev->mc_maxdelay = *max_delay;
1306 
1307 	return 0;
1308 }
1309 
1310 /* called with rcu_read_lock() */
1311 int igmp6_event_query(struct sk_buff *skb)
1312 {
1313 	struct mld2_query *mlh2 = NULL;
1314 	struct ifmcaddr6 *ma;
1315 	const struct in6_addr *group;
1316 	unsigned long max_delay;
1317 	struct inet6_dev *idev;
1318 	struct mld_msg *mld;
1319 	int group_type;
1320 	int mark = 0;
1321 	int len, err;
1322 
1323 	if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1324 		return -EINVAL;
1325 
1326 	/* compute payload length excluding extension headers */
1327 	len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
1328 	len -= skb_network_header_len(skb);
1329 
1330 	/* RFC3810 6.2
1331 	 * Upon reception of an MLD message that contains a Query, the node
1332 	 * checks if the source address of the message is a valid link-local
1333 	 * address, if the Hop Limit is set to 1, and if the Router Alert
1334 	 * option is present in the Hop-By-Hop Options header of the IPv6
1335 	 * packet.  If any of these checks fails, the packet is dropped.
1336 	 */
1337 	if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL) ||
1338 	    ipv6_hdr(skb)->hop_limit != 1 ||
1339 	    !(IP6CB(skb)->flags & IP6SKB_ROUTERALERT) ||
1340 	    IP6CB(skb)->ra != htons(IPV6_OPT_ROUTERALERT_MLD))
1341 		return -EINVAL;
1342 
1343 	idev = __in6_dev_get(skb->dev);
1344 	if (!idev)
1345 		return 0;
1346 
1347 	mld = (struct mld_msg *)icmp6_hdr(skb);
1348 	group = &mld->mld_mca;
1349 	group_type = ipv6_addr_type(group);
1350 
1351 	if (group_type != IPV6_ADDR_ANY &&
1352 	    !(group_type&IPV6_ADDR_MULTICAST))
1353 		return -EINVAL;
1354 
1355 	if (len < MLD_V1_QUERY_LEN) {
1356 		return -EINVAL;
1357 	} else if (len == MLD_V1_QUERY_LEN || mld_in_v1_mode(idev)) {
1358 		err = mld_process_v1(idev, mld, &max_delay,
1359 				     len == MLD_V1_QUERY_LEN);
1360 		if (err < 0)
1361 			return err;
1362 	} else if (len >= MLD_V2_QUERY_LEN_MIN) {
1363 		int srcs_offset = sizeof(struct mld2_query) -
1364 				  sizeof(struct icmp6hdr);
1365 
1366 		if (!pskb_may_pull(skb, srcs_offset))
1367 			return -EINVAL;
1368 
1369 		mlh2 = (struct mld2_query *)skb_transport_header(skb);
1370 
1371 		err = mld_process_v2(idev, mlh2, &max_delay);
1372 		if (err < 0)
1373 			return err;
1374 
1375 		if (group_type == IPV6_ADDR_ANY) { /* general query */
1376 			if (mlh2->mld2q_nsrcs)
1377 				return -EINVAL; /* no sources allowed */
1378 
1379 			mld_gq_start_timer(idev);
1380 			return 0;
1381 		}
1382 		/* mark sources to include, if group & source-specific */
1383 		if (mlh2->mld2q_nsrcs != 0) {
1384 			if (!pskb_may_pull(skb, srcs_offset +
1385 			    ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
1386 				return -EINVAL;
1387 
1388 			mlh2 = (struct mld2_query *)skb_transport_header(skb);
1389 			mark = 1;
1390 		}
1391 	} else {
1392 		return -EINVAL;
1393 	}
1394 
1395 	read_lock_bh(&idev->lock);
1396 	if (group_type == IPV6_ADDR_ANY) {
1397 		for (ma = idev->mc_list; ma; ma = ma->next) {
1398 			spin_lock_bh(&ma->mca_lock);
1399 			igmp6_group_queried(ma, max_delay);
1400 			spin_unlock_bh(&ma->mca_lock);
1401 		}
1402 	} else {
1403 		for (ma = idev->mc_list; ma; ma = ma->next) {
1404 			if (!ipv6_addr_equal(group, &ma->mca_addr))
1405 				continue;
1406 			spin_lock_bh(&ma->mca_lock);
1407 			if (ma->mca_flags & MAF_TIMER_RUNNING) {
1408 				/* gsquery <- gsquery && mark */
1409 				if (!mark)
1410 					ma->mca_flags &= ~MAF_GSQUERY;
1411 			} else {
1412 				/* gsquery <- mark */
1413 				if (mark)
1414 					ma->mca_flags |= MAF_GSQUERY;
1415 				else
1416 					ma->mca_flags &= ~MAF_GSQUERY;
1417 			}
1418 			if (!(ma->mca_flags & MAF_GSQUERY) ||
1419 			    mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
1420 				igmp6_group_queried(ma, max_delay);
1421 			spin_unlock_bh(&ma->mca_lock);
1422 			break;
1423 		}
1424 	}
1425 	read_unlock_bh(&idev->lock);
1426 
1427 	return 0;
1428 }
1429 
1430 /* called with rcu_read_lock() */
1431 int igmp6_event_report(struct sk_buff *skb)
1432 {
1433 	struct ifmcaddr6 *ma;
1434 	struct inet6_dev *idev;
1435 	struct mld_msg *mld;
1436 	int addr_type;
1437 
1438 	/* Our own report looped back. Ignore it. */
1439 	if (skb->pkt_type == PACKET_LOOPBACK)
1440 		return 0;
1441 
1442 	/* send our report if the MC router may not have heard this report */
1443 	if (skb->pkt_type != PACKET_MULTICAST &&
1444 	    skb->pkt_type != PACKET_BROADCAST)
1445 		return 0;
1446 
1447 	if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
1448 		return -EINVAL;
1449 
1450 	mld = (struct mld_msg *)icmp6_hdr(skb);
1451 
1452 	/* Drop reports with not link local source */
1453 	addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
1454 	if (addr_type != IPV6_ADDR_ANY &&
1455 	    !(addr_type&IPV6_ADDR_LINKLOCAL))
1456 		return -EINVAL;
1457 
1458 	idev = __in6_dev_get(skb->dev);
1459 	if (!idev)
1460 		return -ENODEV;
1461 
1462 	/*
1463 	 *	Cancel the timer for this group
1464 	 */
1465 
1466 	read_lock_bh(&idev->lock);
1467 	for (ma = idev->mc_list; ma; ma = ma->next) {
1468 		if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
1469 			spin_lock(&ma->mca_lock);
1470 			if (del_timer(&ma->mca_timer))
1471 				atomic_dec(&ma->mca_refcnt);
1472 			ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1473 			spin_unlock(&ma->mca_lock);
1474 			break;
1475 		}
1476 	}
1477 	read_unlock_bh(&idev->lock);
1478 	return 0;
1479 }
1480 
1481 static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1482 		  int gdeleted, int sdeleted)
1483 {
1484 	switch (type) {
1485 	case MLD2_MODE_IS_INCLUDE:
1486 	case MLD2_MODE_IS_EXCLUDE:
1487 		if (gdeleted || sdeleted)
1488 			return false;
1489 		if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1490 			if (pmc->mca_sfmode == MCAST_INCLUDE)
1491 				return true;
1492 			/* don't include if this source is excluded
1493 			 * in all filters
1494 			 */
1495 			if (psf->sf_count[MCAST_INCLUDE])
1496 				return type == MLD2_MODE_IS_INCLUDE;
1497 			return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1498 				psf->sf_count[MCAST_EXCLUDE];
1499 		}
1500 		return false;
1501 	case MLD2_CHANGE_TO_INCLUDE:
1502 		if (gdeleted || sdeleted)
1503 			return false;
1504 		return psf->sf_count[MCAST_INCLUDE] != 0;
1505 	case MLD2_CHANGE_TO_EXCLUDE:
1506 		if (gdeleted || sdeleted)
1507 			return false;
1508 		if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1509 		    psf->sf_count[MCAST_INCLUDE])
1510 			return false;
1511 		return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1512 			psf->sf_count[MCAST_EXCLUDE];
1513 	case MLD2_ALLOW_NEW_SOURCES:
1514 		if (gdeleted || !psf->sf_crcount)
1515 			return false;
1516 		return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1517 	case MLD2_BLOCK_OLD_SOURCES:
1518 		if (pmc->mca_sfmode == MCAST_INCLUDE)
1519 			return gdeleted || (psf->sf_crcount && sdeleted);
1520 		return psf->sf_crcount && !gdeleted && !sdeleted;
1521 	}
1522 	return false;
1523 }
1524 
1525 static int
1526 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1527 {
1528 	struct ip6_sf_list *psf;
1529 	int scount = 0;
1530 
1531 	for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
1532 		if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1533 			continue;
1534 		scount++;
1535 	}
1536 	return scount;
1537 }
1538 
1539 static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
1540 		       struct net_device *dev,
1541 		       const struct in6_addr *saddr,
1542 		       const struct in6_addr *daddr,
1543 		       int proto, int len)
1544 {
1545 	struct ipv6hdr *hdr;
1546 
1547 	skb->protocol = htons(ETH_P_IPV6);
1548 	skb->dev = dev;
1549 
1550 	skb_reset_network_header(skb);
1551 	skb_put(skb, sizeof(struct ipv6hdr));
1552 	hdr = ipv6_hdr(skb);
1553 
1554 	ip6_flow_hdr(hdr, 0, 0);
1555 
1556 	hdr->payload_len = htons(len);
1557 	hdr->nexthdr = proto;
1558 	hdr->hop_limit = inet6_sk(sk)->hop_limit;
1559 
1560 	hdr->saddr = *saddr;
1561 	hdr->daddr = *daddr;
1562 }
1563 
1564 static struct sk_buff *mld_newpack(struct inet6_dev *idev, unsigned int mtu)
1565 {
1566 	struct net_device *dev = idev->dev;
1567 	struct net *net = dev_net(dev);
1568 	struct sock *sk = net->ipv6.igmp_sk;
1569 	struct sk_buff *skb;
1570 	struct mld2_report *pmr;
1571 	struct in6_addr addr_buf;
1572 	const struct in6_addr *saddr;
1573 	int hlen = LL_RESERVED_SPACE(dev);
1574 	int tlen = dev->needed_tailroom;
1575 	unsigned int size = mtu + hlen + tlen;
1576 	int err;
1577 	u8 ra[8] = { IPPROTO_ICMPV6, 0,
1578 		     IPV6_TLV_ROUTERALERT, 2, 0, 0,
1579 		     IPV6_TLV_PADN, 0 };
1580 
1581 	/* we assume size > sizeof(ra) here */
1582 	/* limit our allocations to order-0 page */
1583 	size = min_t(int, size, SKB_MAX_ORDER(0, 0));
1584 	skb = sock_alloc_send_skb(sk, size, 1, &err);
1585 
1586 	if (!skb)
1587 		return NULL;
1588 
1589 	skb->priority = TC_PRIO_CONTROL;
1590 	skb_reserve(skb, hlen);
1591 	skb_tailroom_reserve(skb, mtu, tlen);
1592 
1593 	if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
1594 		/* <draft-ietf-magma-mld-source-05.txt>:
1595 		 * use unspecified address as the source address
1596 		 * when a valid link-local address is not available.
1597 		 */
1598 		saddr = &in6addr_any;
1599 	} else
1600 		saddr = &addr_buf;
1601 
1602 	ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
1603 
1604 	memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1605 
1606 	skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
1607 	skb_put(skb, sizeof(*pmr));
1608 	pmr = (struct mld2_report *)skb_transport_header(skb);
1609 	pmr->mld2r_type = ICMPV6_MLD2_REPORT;
1610 	pmr->mld2r_resv1 = 0;
1611 	pmr->mld2r_cksum = 0;
1612 	pmr->mld2r_resv2 = 0;
1613 	pmr->mld2r_ngrec = 0;
1614 	return skb;
1615 }
1616 
1617 static void mld_sendpack(struct sk_buff *skb)
1618 {
1619 	struct ipv6hdr *pip6 = ipv6_hdr(skb);
1620 	struct mld2_report *pmr =
1621 			      (struct mld2_report *)skb_transport_header(skb);
1622 	int payload_len, mldlen;
1623 	struct inet6_dev *idev;
1624 	struct net *net = dev_net(skb->dev);
1625 	int err;
1626 	struct flowi6 fl6;
1627 	struct dst_entry *dst;
1628 
1629 	rcu_read_lock();
1630 	idev = __in6_dev_get(skb->dev);
1631 	IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1632 
1633 	payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
1634 		sizeof(*pip6);
1635 	mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
1636 	pip6->payload_len = htons(payload_len);
1637 
1638 	pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1639 					   IPPROTO_ICMPV6,
1640 					   csum_partial(skb_transport_header(skb),
1641 							mldlen, 0));
1642 
1643 	icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
1644 			 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1645 			 skb->dev->ifindex);
1646 	dst = icmp6_dst_alloc(skb->dev, &fl6);
1647 
1648 	err = 0;
1649 	if (IS_ERR(dst)) {
1650 		err = PTR_ERR(dst);
1651 		dst = NULL;
1652 	}
1653 	skb_dst_set(skb, dst);
1654 	if (err)
1655 		goto err_out;
1656 
1657 	payload_len = skb->len;
1658 
1659 	err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
1660 		      net, net->ipv6.igmp_sk, skb, NULL, skb->dev,
1661 		      dst_output);
1662 out:
1663 	if (!err) {
1664 		ICMP6MSGOUT_INC_STATS(net, idev, ICMPV6_MLD2_REPORT);
1665 		ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
1666 	} else {
1667 		IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
1668 	}
1669 
1670 	rcu_read_unlock();
1671 	return;
1672 
1673 err_out:
1674 	kfree_skb(skb);
1675 	goto out;
1676 }
1677 
1678 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1679 {
1680 	return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1681 }
1682 
1683 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1684 	int type, struct mld2_grec **ppgr)
1685 {
1686 	struct net_device *dev = pmc->idev->dev;
1687 	struct mld2_report *pmr;
1688 	struct mld2_grec *pgr;
1689 
1690 	if (!skb)
1691 		skb = mld_newpack(pmc->idev, dev->mtu);
1692 	if (!skb)
1693 		return NULL;
1694 	pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1695 	pgr->grec_type = type;
1696 	pgr->grec_auxwords = 0;
1697 	pgr->grec_nsrcs = 0;
1698 	pgr->grec_mca = pmc->mca_addr;	/* structure copy */
1699 	pmr = (struct mld2_report *)skb_transport_header(skb);
1700 	pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
1701 	*ppgr = pgr;
1702 	return skb;
1703 }
1704 
1705 #define AVAILABLE(skb)	((skb) ? skb_availroom(skb) : 0)
1706 
1707 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1708 	int type, int gdeleted, int sdeleted, int crsend)
1709 {
1710 	struct inet6_dev *idev = pmc->idev;
1711 	struct net_device *dev = idev->dev;
1712 	struct mld2_report *pmr;
1713 	struct mld2_grec *pgr = NULL;
1714 	struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1715 	int scount, stotal, first, isquery, truncate;
1716 
1717 	if (pmc->mca_flags & MAF_NOREPORT)
1718 		return skb;
1719 
1720 	isquery = type == MLD2_MODE_IS_INCLUDE ||
1721 		  type == MLD2_MODE_IS_EXCLUDE;
1722 	truncate = type == MLD2_MODE_IS_EXCLUDE ||
1723 		    type == MLD2_CHANGE_TO_EXCLUDE;
1724 
1725 	stotal = scount = 0;
1726 
1727 	psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1728 
1729 	if (!*psf_list)
1730 		goto empty_source;
1731 
1732 	pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
1733 
1734 	/* EX and TO_EX get a fresh packet, if needed */
1735 	if (truncate) {
1736 		if (pmr && pmr->mld2r_ngrec &&
1737 		    AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1738 			if (skb)
1739 				mld_sendpack(skb);
1740 			skb = mld_newpack(idev, dev->mtu);
1741 		}
1742 	}
1743 	first = 1;
1744 	psf_prev = NULL;
1745 	for (psf = *psf_list; psf; psf = psf_next) {
1746 		struct in6_addr *psrc;
1747 
1748 		psf_next = psf->sf_next;
1749 
1750 		if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1751 			psf_prev = psf;
1752 			continue;
1753 		}
1754 
1755 		/* Based on RFC3810 6.1. Should not send source-list change
1756 		 * records when there is a filter mode change.
1757 		 */
1758 		if (((gdeleted && pmc->mca_sfmode == MCAST_EXCLUDE) ||
1759 		     (!gdeleted && pmc->mca_crcount)) &&
1760 		    (type == MLD2_ALLOW_NEW_SOURCES ||
1761 		     type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount)
1762 			goto decrease_sf_crcount;
1763 
1764 		/* clear marks on query responses */
1765 		if (isquery)
1766 			psf->sf_gsresp = 0;
1767 
1768 		if (AVAILABLE(skb) < sizeof(*psrc) +
1769 		    first*sizeof(struct mld2_grec)) {
1770 			if (truncate && !first)
1771 				break;	 /* truncate these */
1772 			if (pgr)
1773 				pgr->grec_nsrcs = htons(scount);
1774 			if (skb)
1775 				mld_sendpack(skb);
1776 			skb = mld_newpack(idev, dev->mtu);
1777 			first = 1;
1778 			scount = 0;
1779 		}
1780 		if (first) {
1781 			skb = add_grhead(skb, pmc, type, &pgr);
1782 			first = 0;
1783 		}
1784 		if (!skb)
1785 			return NULL;
1786 		psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1787 		*psrc = psf->sf_addr;
1788 		scount++; stotal++;
1789 		if ((type == MLD2_ALLOW_NEW_SOURCES ||
1790 		     type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1791 decrease_sf_crcount:
1792 			psf->sf_crcount--;
1793 			if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1794 				if (psf_prev)
1795 					psf_prev->sf_next = psf->sf_next;
1796 				else
1797 					*psf_list = psf->sf_next;
1798 				kfree(psf);
1799 				continue;
1800 			}
1801 		}
1802 		psf_prev = psf;
1803 	}
1804 
1805 empty_source:
1806 	if (!stotal) {
1807 		if (type == MLD2_ALLOW_NEW_SOURCES ||
1808 		    type == MLD2_BLOCK_OLD_SOURCES)
1809 			return skb;
1810 		if (pmc->mca_crcount || isquery || crsend) {
1811 			/* make sure we have room for group header */
1812 			if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1813 				mld_sendpack(skb);
1814 				skb = NULL; /* add_grhead will get a new one */
1815 			}
1816 			skb = add_grhead(skb, pmc, type, &pgr);
1817 		}
1818 	}
1819 	if (pgr)
1820 		pgr->grec_nsrcs = htons(scount);
1821 
1822 	if (isquery)
1823 		pmc->mca_flags &= ~MAF_GSQUERY;	/* clear query state */
1824 	return skb;
1825 }
1826 
1827 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1828 {
1829 	struct sk_buff *skb = NULL;
1830 	int type;
1831 
1832 	read_lock_bh(&idev->lock);
1833 	if (!pmc) {
1834 		for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
1835 			if (pmc->mca_flags & MAF_NOREPORT)
1836 				continue;
1837 			spin_lock_bh(&pmc->mca_lock);
1838 			if (pmc->mca_sfcount[MCAST_EXCLUDE])
1839 				type = MLD2_MODE_IS_EXCLUDE;
1840 			else
1841 				type = MLD2_MODE_IS_INCLUDE;
1842 			skb = add_grec(skb, pmc, type, 0, 0, 0);
1843 			spin_unlock_bh(&pmc->mca_lock);
1844 		}
1845 	} else {
1846 		spin_lock_bh(&pmc->mca_lock);
1847 		if (pmc->mca_sfcount[MCAST_EXCLUDE])
1848 			type = MLD2_MODE_IS_EXCLUDE;
1849 		else
1850 			type = MLD2_MODE_IS_INCLUDE;
1851 		skb = add_grec(skb, pmc, type, 0, 0, 0);
1852 		spin_unlock_bh(&pmc->mca_lock);
1853 	}
1854 	read_unlock_bh(&idev->lock);
1855 	if (skb)
1856 		mld_sendpack(skb);
1857 }
1858 
1859 /*
1860  * remove zero-count source records from a source filter list
1861  */
1862 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1863 {
1864 	struct ip6_sf_list *psf_prev, *psf_next, *psf;
1865 
1866 	psf_prev = NULL;
1867 	for (psf = *ppsf; psf; psf = psf_next) {
1868 		psf_next = psf->sf_next;
1869 		if (psf->sf_crcount == 0) {
1870 			if (psf_prev)
1871 				psf_prev->sf_next = psf->sf_next;
1872 			else
1873 				*ppsf = psf->sf_next;
1874 			kfree(psf);
1875 		} else
1876 			psf_prev = psf;
1877 	}
1878 }
1879 
1880 static void mld_send_cr(struct inet6_dev *idev)
1881 {
1882 	struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1883 	struct sk_buff *skb = NULL;
1884 	int type, dtype;
1885 
1886 	read_lock_bh(&idev->lock);
1887 	spin_lock(&idev->mc_lock);
1888 
1889 	/* deleted MCA's */
1890 	pmc_prev = NULL;
1891 	for (pmc = idev->mc_tomb; pmc; pmc = pmc_next) {
1892 		pmc_next = pmc->next;
1893 		if (pmc->mca_sfmode == MCAST_INCLUDE) {
1894 			type = MLD2_BLOCK_OLD_SOURCES;
1895 			dtype = MLD2_BLOCK_OLD_SOURCES;
1896 			skb = add_grec(skb, pmc, type, 1, 0, 0);
1897 			skb = add_grec(skb, pmc, dtype, 1, 1, 0);
1898 		}
1899 		if (pmc->mca_crcount) {
1900 			if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1901 				type = MLD2_CHANGE_TO_INCLUDE;
1902 				skb = add_grec(skb, pmc, type, 1, 0, 0);
1903 			}
1904 			pmc->mca_crcount--;
1905 			if (pmc->mca_crcount == 0) {
1906 				mld_clear_zeros(&pmc->mca_tomb);
1907 				mld_clear_zeros(&pmc->mca_sources);
1908 			}
1909 		}
1910 		if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1911 		    !pmc->mca_sources) {
1912 			if (pmc_prev)
1913 				pmc_prev->next = pmc_next;
1914 			else
1915 				idev->mc_tomb = pmc_next;
1916 			in6_dev_put(pmc->idev);
1917 			kfree(pmc);
1918 		} else
1919 			pmc_prev = pmc;
1920 	}
1921 	spin_unlock(&idev->mc_lock);
1922 
1923 	/* change recs */
1924 	for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
1925 		spin_lock_bh(&pmc->mca_lock);
1926 		if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1927 			type = MLD2_BLOCK_OLD_SOURCES;
1928 			dtype = MLD2_ALLOW_NEW_SOURCES;
1929 		} else {
1930 			type = MLD2_ALLOW_NEW_SOURCES;
1931 			dtype = MLD2_BLOCK_OLD_SOURCES;
1932 		}
1933 		skb = add_grec(skb, pmc, type, 0, 0, 0);
1934 		skb = add_grec(skb, pmc, dtype, 0, 1, 0);	/* deleted sources */
1935 
1936 		/* filter mode changes */
1937 		if (pmc->mca_crcount) {
1938 			if (pmc->mca_sfmode == MCAST_EXCLUDE)
1939 				type = MLD2_CHANGE_TO_EXCLUDE;
1940 			else
1941 				type = MLD2_CHANGE_TO_INCLUDE;
1942 			skb = add_grec(skb, pmc, type, 0, 0, 0);
1943 			pmc->mca_crcount--;
1944 		}
1945 		spin_unlock_bh(&pmc->mca_lock);
1946 	}
1947 	read_unlock_bh(&idev->lock);
1948 	if (!skb)
1949 		return;
1950 	(void) mld_sendpack(skb);
1951 }
1952 
1953 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1954 {
1955 	struct net *net = dev_net(dev);
1956 	struct sock *sk = net->ipv6.igmp_sk;
1957 	struct inet6_dev *idev;
1958 	struct sk_buff *skb;
1959 	struct mld_msg *hdr;
1960 	const struct in6_addr *snd_addr, *saddr;
1961 	struct in6_addr addr_buf;
1962 	int hlen = LL_RESERVED_SPACE(dev);
1963 	int tlen = dev->needed_tailroom;
1964 	int err, len, payload_len, full_len;
1965 	u8 ra[8] = { IPPROTO_ICMPV6, 0,
1966 		     IPV6_TLV_ROUTERALERT, 2, 0, 0,
1967 		     IPV6_TLV_PADN, 0 };
1968 	struct flowi6 fl6;
1969 	struct dst_entry *dst;
1970 
1971 	if (type == ICMPV6_MGM_REDUCTION)
1972 		snd_addr = &in6addr_linklocal_allrouters;
1973 	else
1974 		snd_addr = addr;
1975 
1976 	len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1977 	payload_len = len + sizeof(ra);
1978 	full_len = sizeof(struct ipv6hdr) + payload_len;
1979 
1980 	rcu_read_lock();
1981 	IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
1982 		      IPSTATS_MIB_OUT, full_len);
1983 	rcu_read_unlock();
1984 
1985 	skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
1986 
1987 	if (!skb) {
1988 		rcu_read_lock();
1989 		IP6_INC_STATS(net, __in6_dev_get(dev),
1990 			      IPSTATS_MIB_OUTDISCARDS);
1991 		rcu_read_unlock();
1992 		return;
1993 	}
1994 	skb->priority = TC_PRIO_CONTROL;
1995 	skb_reserve(skb, hlen);
1996 
1997 	if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1998 		/* <draft-ietf-magma-mld-source-05.txt>:
1999 		 * use unspecified address as the source address
2000 		 * when a valid link-local address is not available.
2001 		 */
2002 		saddr = &in6addr_any;
2003 	} else
2004 		saddr = &addr_buf;
2005 
2006 	ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
2007 
2008 	memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
2009 
2010 	hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg));
2011 	memset(hdr, 0, sizeof(struct mld_msg));
2012 	hdr->mld_type = type;
2013 	hdr->mld_mca = *addr;
2014 
2015 	hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
2016 					 IPPROTO_ICMPV6,
2017 					 csum_partial(hdr, len, 0));
2018 
2019 	rcu_read_lock();
2020 	idev = __in6_dev_get(skb->dev);
2021 
2022 	icmpv6_flow_init(sk, &fl6, type,
2023 			 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
2024 			 skb->dev->ifindex);
2025 	dst = icmp6_dst_alloc(skb->dev, &fl6);
2026 	if (IS_ERR(dst)) {
2027 		err = PTR_ERR(dst);
2028 		goto err_out;
2029 	}
2030 
2031 	skb_dst_set(skb, dst);
2032 	err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
2033 		      net, sk, skb, NULL, skb->dev,
2034 		      dst_output);
2035 out:
2036 	if (!err) {
2037 		ICMP6MSGOUT_INC_STATS(net, idev, type);
2038 		ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
2039 	} else
2040 		IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
2041 
2042 	rcu_read_unlock();
2043 	return;
2044 
2045 err_out:
2046 	kfree_skb(skb);
2047 	goto out;
2048 }
2049 
2050 static void mld_send_initial_cr(struct inet6_dev *idev)
2051 {
2052 	struct sk_buff *skb;
2053 	struct ifmcaddr6 *pmc;
2054 	int type;
2055 
2056 	if (mld_in_v1_mode(idev))
2057 		return;
2058 
2059 	skb = NULL;
2060 	read_lock_bh(&idev->lock);
2061 	for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
2062 		spin_lock_bh(&pmc->mca_lock);
2063 		if (pmc->mca_sfcount[MCAST_EXCLUDE])
2064 			type = MLD2_CHANGE_TO_EXCLUDE;
2065 		else
2066 			type = MLD2_CHANGE_TO_INCLUDE;
2067 		skb = add_grec(skb, pmc, type, 0, 0, 1);
2068 		spin_unlock_bh(&pmc->mca_lock);
2069 	}
2070 	read_unlock_bh(&idev->lock);
2071 	if (skb)
2072 		mld_sendpack(skb);
2073 }
2074 
2075 void ipv6_mc_dad_complete(struct inet6_dev *idev)
2076 {
2077 	idev->mc_dad_count = idev->mc_qrv;
2078 	if (idev->mc_dad_count) {
2079 		mld_send_initial_cr(idev);
2080 		idev->mc_dad_count--;
2081 		if (idev->mc_dad_count)
2082 			mld_dad_start_timer(idev, idev->mc_maxdelay);
2083 	}
2084 }
2085 
2086 static void mld_dad_timer_expire(unsigned long data)
2087 {
2088 	struct inet6_dev *idev = (struct inet6_dev *)data;
2089 
2090 	mld_send_initial_cr(idev);
2091 	if (idev->mc_dad_count) {
2092 		idev->mc_dad_count--;
2093 		if (idev->mc_dad_count)
2094 			mld_dad_start_timer(idev, idev->mc_maxdelay);
2095 	}
2096 	in6_dev_put(idev);
2097 }
2098 
2099 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
2100 	const struct in6_addr *psfsrc)
2101 {
2102 	struct ip6_sf_list *psf, *psf_prev;
2103 	int rv = 0;
2104 
2105 	psf_prev = NULL;
2106 	for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
2107 		if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2108 			break;
2109 		psf_prev = psf;
2110 	}
2111 	if (!psf || psf->sf_count[sfmode] == 0) {
2112 		/* source filter not found, or count wrong =>  bug */
2113 		return -ESRCH;
2114 	}
2115 	psf->sf_count[sfmode]--;
2116 	if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
2117 		struct inet6_dev *idev = pmc->idev;
2118 
2119 		/* no more filters for this source */
2120 		if (psf_prev)
2121 			psf_prev->sf_next = psf->sf_next;
2122 		else
2123 			pmc->mca_sources = psf->sf_next;
2124 		if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
2125 		    !mld_in_v1_mode(idev)) {
2126 			psf->sf_crcount = idev->mc_qrv;
2127 			psf->sf_next = pmc->mca_tomb;
2128 			pmc->mca_tomb = psf;
2129 			rv = 1;
2130 		} else
2131 			kfree(psf);
2132 	}
2133 	return rv;
2134 }
2135 
2136 static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2137 			  int sfmode, int sfcount, const struct in6_addr *psfsrc,
2138 			  int delta)
2139 {
2140 	struct ifmcaddr6 *pmc;
2141 	int	changerec = 0;
2142 	int	i, err;
2143 
2144 	if (!idev)
2145 		return -ENODEV;
2146 	read_lock_bh(&idev->lock);
2147 	for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
2148 		if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2149 			break;
2150 	}
2151 	if (!pmc) {
2152 		/* MCA not found?? bug */
2153 		read_unlock_bh(&idev->lock);
2154 		return -ESRCH;
2155 	}
2156 	spin_lock_bh(&pmc->mca_lock);
2157 	sf_markstate(pmc);
2158 	if (!delta) {
2159 		if (!pmc->mca_sfcount[sfmode]) {
2160 			spin_unlock_bh(&pmc->mca_lock);
2161 			read_unlock_bh(&idev->lock);
2162 			return -EINVAL;
2163 		}
2164 		pmc->mca_sfcount[sfmode]--;
2165 	}
2166 	err = 0;
2167 	for (i = 0; i < sfcount; i++) {
2168 		int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2169 
2170 		changerec |= rv > 0;
2171 		if (!err && rv < 0)
2172 			err = rv;
2173 	}
2174 	if (pmc->mca_sfmode == MCAST_EXCLUDE &&
2175 	    pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
2176 	    pmc->mca_sfcount[MCAST_INCLUDE]) {
2177 		struct ip6_sf_list *psf;
2178 
2179 		/* filter mode change */
2180 		pmc->mca_sfmode = MCAST_INCLUDE;
2181 		pmc->mca_crcount = idev->mc_qrv;
2182 		idev->mc_ifc_count = pmc->mca_crcount;
2183 		for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
2184 			psf->sf_crcount = 0;
2185 		mld_ifc_event(pmc->idev);
2186 	} else if (sf_setstate(pmc) || changerec)
2187 		mld_ifc_event(pmc->idev);
2188 	spin_unlock_bh(&pmc->mca_lock);
2189 	read_unlock_bh(&idev->lock);
2190 	return err;
2191 }
2192 
2193 /*
2194  * Add multicast single-source filter to the interface list
2195  */
2196 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
2197 	const struct in6_addr *psfsrc)
2198 {
2199 	struct ip6_sf_list *psf, *psf_prev;
2200 
2201 	psf_prev = NULL;
2202 	for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
2203 		if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2204 			break;
2205 		psf_prev = psf;
2206 	}
2207 	if (!psf) {
2208 		psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
2209 		if (!psf)
2210 			return -ENOBUFS;
2211 
2212 		psf->sf_addr = *psfsrc;
2213 		if (psf_prev) {
2214 			psf_prev->sf_next = psf;
2215 		} else
2216 			pmc->mca_sources = psf;
2217 	}
2218 	psf->sf_count[sfmode]++;
2219 	return 0;
2220 }
2221 
2222 static void sf_markstate(struct ifmcaddr6 *pmc)
2223 {
2224 	struct ip6_sf_list *psf;
2225 	int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2226 
2227 	for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
2228 		if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2229 			psf->sf_oldin = mca_xcount ==
2230 				psf->sf_count[MCAST_EXCLUDE] &&
2231 				!psf->sf_count[MCAST_INCLUDE];
2232 		} else
2233 			psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2234 }
2235 
2236 static int sf_setstate(struct ifmcaddr6 *pmc)
2237 {
2238 	struct ip6_sf_list *psf, *dpsf;
2239 	int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2240 	int qrv = pmc->idev->mc_qrv;
2241 	int new_in, rv;
2242 
2243 	rv = 0;
2244 	for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
2245 		if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2246 			new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2247 				!psf->sf_count[MCAST_INCLUDE];
2248 		} else
2249 			new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2250 		if (new_in) {
2251 			if (!psf->sf_oldin) {
2252 				struct ip6_sf_list *prev = NULL;
2253 
2254 				for (dpsf = pmc->mca_tomb; dpsf;
2255 				     dpsf = dpsf->sf_next) {
2256 					if (ipv6_addr_equal(&dpsf->sf_addr,
2257 					    &psf->sf_addr))
2258 						break;
2259 					prev = dpsf;
2260 				}
2261 				if (dpsf) {
2262 					if (prev)
2263 						prev->sf_next = dpsf->sf_next;
2264 					else
2265 						pmc->mca_tomb = dpsf->sf_next;
2266 					kfree(dpsf);
2267 				}
2268 				psf->sf_crcount = qrv;
2269 				rv++;
2270 			}
2271 		} else if (psf->sf_oldin) {
2272 			psf->sf_crcount = 0;
2273 			/*
2274 			 * add or update "delete" records if an active filter
2275 			 * is now inactive
2276 			 */
2277 			for (dpsf = pmc->mca_tomb; dpsf; dpsf = dpsf->sf_next)
2278 				if (ipv6_addr_equal(&dpsf->sf_addr,
2279 				    &psf->sf_addr))
2280 					break;
2281 			if (!dpsf) {
2282 				dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2283 				if (!dpsf)
2284 					continue;
2285 				*dpsf = *psf;
2286 				/* pmc->mca_lock held by callers */
2287 				dpsf->sf_next = pmc->mca_tomb;
2288 				pmc->mca_tomb = dpsf;
2289 			}
2290 			dpsf->sf_crcount = qrv;
2291 			rv++;
2292 		}
2293 	}
2294 	return rv;
2295 }
2296 
2297 /*
2298  * Add multicast source filter list to the interface list
2299  */
2300 static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2301 			  int sfmode, int sfcount, const struct in6_addr *psfsrc,
2302 			  int delta)
2303 {
2304 	struct ifmcaddr6 *pmc;
2305 	int	isexclude;
2306 	int	i, err;
2307 
2308 	if (!idev)
2309 		return -ENODEV;
2310 	read_lock_bh(&idev->lock);
2311 	for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
2312 		if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2313 			break;
2314 	}
2315 	if (!pmc) {
2316 		/* MCA not found?? bug */
2317 		read_unlock_bh(&idev->lock);
2318 		return -ESRCH;
2319 	}
2320 	spin_lock_bh(&pmc->mca_lock);
2321 
2322 	sf_markstate(pmc);
2323 	isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2324 	if (!delta)
2325 		pmc->mca_sfcount[sfmode]++;
2326 	err = 0;
2327 	for (i = 0; i < sfcount; i++) {
2328 		err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2329 		if (err)
2330 			break;
2331 	}
2332 	if (err) {
2333 		int j;
2334 
2335 		if (!delta)
2336 			pmc->mca_sfcount[sfmode]--;
2337 		for (j = 0; j < i; j++)
2338 			ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2339 	} else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2340 		struct ip6_sf_list *psf;
2341 
2342 		/* filter mode change */
2343 		if (pmc->mca_sfcount[MCAST_EXCLUDE])
2344 			pmc->mca_sfmode = MCAST_EXCLUDE;
2345 		else if (pmc->mca_sfcount[MCAST_INCLUDE])
2346 			pmc->mca_sfmode = MCAST_INCLUDE;
2347 		/* else no filters; keep old mode for reports */
2348 
2349 		pmc->mca_crcount = idev->mc_qrv;
2350 		idev->mc_ifc_count = pmc->mca_crcount;
2351 		for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
2352 			psf->sf_crcount = 0;
2353 		mld_ifc_event(idev);
2354 	} else if (sf_setstate(pmc))
2355 		mld_ifc_event(idev);
2356 	spin_unlock_bh(&pmc->mca_lock);
2357 	read_unlock_bh(&idev->lock);
2358 	return err;
2359 }
2360 
2361 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2362 {
2363 	struct ip6_sf_list *psf, *nextpsf;
2364 
2365 	for (psf = pmc->mca_tomb; psf; psf = nextpsf) {
2366 		nextpsf = psf->sf_next;
2367 		kfree(psf);
2368 	}
2369 	pmc->mca_tomb = NULL;
2370 	for (psf = pmc->mca_sources; psf; psf = nextpsf) {
2371 		nextpsf = psf->sf_next;
2372 		kfree(psf);
2373 	}
2374 	pmc->mca_sources = NULL;
2375 	pmc->mca_sfmode = MCAST_EXCLUDE;
2376 	pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2377 	pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2378 }
2379 
2380 
2381 static void igmp6_join_group(struct ifmcaddr6 *ma)
2382 {
2383 	unsigned long delay;
2384 
2385 	if (ma->mca_flags & MAF_NOREPORT)
2386 		return;
2387 
2388 	igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2389 
2390 	delay = prandom_u32() % unsolicited_report_interval(ma->idev);
2391 
2392 	spin_lock_bh(&ma->mca_lock);
2393 	if (del_timer(&ma->mca_timer)) {
2394 		atomic_dec(&ma->mca_refcnt);
2395 		delay = ma->mca_timer.expires - jiffies;
2396 	}
2397 
2398 	if (!mod_timer(&ma->mca_timer, jiffies + delay))
2399 		atomic_inc(&ma->mca_refcnt);
2400 	ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2401 	spin_unlock_bh(&ma->mca_lock);
2402 }
2403 
2404 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2405 			    struct inet6_dev *idev)
2406 {
2407 	int err;
2408 
2409 	/* callers have the socket lock and rtnl lock
2410 	 * so no other readers or writers of iml or its sflist
2411 	 */
2412 	if (!iml->sflist) {
2413 		/* any-source empty exclude case */
2414 		return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2415 	}
2416 	err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2417 		iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2418 	sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2419 	iml->sflist = NULL;
2420 	return err;
2421 }
2422 
2423 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2424 {
2425 	if (mld_in_v1_mode(ma->idev)) {
2426 		if (ma->mca_flags & MAF_LAST_REPORTER)
2427 			igmp6_send(&ma->mca_addr, ma->idev->dev,
2428 				ICMPV6_MGM_REDUCTION);
2429 	} else {
2430 		mld_add_delrec(ma->idev, ma);
2431 		mld_ifc_event(ma->idev);
2432 	}
2433 }
2434 
2435 static void mld_gq_timer_expire(unsigned long data)
2436 {
2437 	struct inet6_dev *idev = (struct inet6_dev *)data;
2438 
2439 	idev->mc_gq_running = 0;
2440 	mld_send_report(idev, NULL);
2441 	in6_dev_put(idev);
2442 }
2443 
2444 static void mld_ifc_timer_expire(unsigned long data)
2445 {
2446 	struct inet6_dev *idev = (struct inet6_dev *)data;
2447 
2448 	mld_send_cr(idev);
2449 	if (idev->mc_ifc_count) {
2450 		idev->mc_ifc_count--;
2451 		if (idev->mc_ifc_count)
2452 			mld_ifc_start_timer(idev, idev->mc_maxdelay);
2453 	}
2454 	in6_dev_put(idev);
2455 }
2456 
2457 static void mld_ifc_event(struct inet6_dev *idev)
2458 {
2459 	if (mld_in_v1_mode(idev))
2460 		return;
2461 	idev->mc_ifc_count = idev->mc_qrv;
2462 	mld_ifc_start_timer(idev, 1);
2463 }
2464 
2465 
2466 static void igmp6_timer_handler(unsigned long data)
2467 {
2468 	struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2469 
2470 	if (mld_in_v1_mode(ma->idev))
2471 		igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2472 	else
2473 		mld_send_report(ma->idev, ma);
2474 
2475 	spin_lock(&ma->mca_lock);
2476 	ma->mca_flags |=  MAF_LAST_REPORTER;
2477 	ma->mca_flags &= ~MAF_TIMER_RUNNING;
2478 	spin_unlock(&ma->mca_lock);
2479 	ma_put(ma);
2480 }
2481 
2482 /* Device changing type */
2483 
2484 void ipv6_mc_unmap(struct inet6_dev *idev)
2485 {
2486 	struct ifmcaddr6 *i;
2487 
2488 	/* Install multicast list, except for all-nodes (already installed) */
2489 
2490 	read_lock_bh(&idev->lock);
2491 	for (i = idev->mc_list; i; i = i->next)
2492 		igmp6_group_dropped(i);
2493 	read_unlock_bh(&idev->lock);
2494 }
2495 
2496 void ipv6_mc_remap(struct inet6_dev *idev)
2497 {
2498 	ipv6_mc_up(idev);
2499 }
2500 
2501 /* Device going down */
2502 
2503 void ipv6_mc_down(struct inet6_dev *idev)
2504 {
2505 	struct ifmcaddr6 *i;
2506 
2507 	/* Withdraw multicast list */
2508 
2509 	read_lock_bh(&idev->lock);
2510 
2511 	for (i = idev->mc_list; i; i = i->next)
2512 		igmp6_group_dropped(i);
2513 
2514 	/* Should stop timer after group drop. or we will
2515 	 * start timer again in mld_ifc_event()
2516 	 */
2517 	mld_ifc_stop_timer(idev);
2518 	mld_gq_stop_timer(idev);
2519 	mld_dad_stop_timer(idev);
2520 	read_unlock_bh(&idev->lock);
2521 }
2522 
2523 static void ipv6_mc_reset(struct inet6_dev *idev)
2524 {
2525 	idev->mc_qrv = sysctl_mld_qrv;
2526 	idev->mc_qi = MLD_QI_DEFAULT;
2527 	idev->mc_qri = MLD_QRI_DEFAULT;
2528 	idev->mc_v1_seen = 0;
2529 	idev->mc_maxdelay = unsolicited_report_interval(idev);
2530 }
2531 
2532 /* Device going up */
2533 
2534 void ipv6_mc_up(struct inet6_dev *idev)
2535 {
2536 	struct ifmcaddr6 *i;
2537 
2538 	/* Install multicast list, except for all-nodes (already installed) */
2539 
2540 	read_lock_bh(&idev->lock);
2541 	ipv6_mc_reset(idev);
2542 	for (i = idev->mc_list; i; i = i->next) {
2543 		mld_del_delrec(idev, i);
2544 		igmp6_group_added(i);
2545 	}
2546 	read_unlock_bh(&idev->lock);
2547 }
2548 
2549 /* IPv6 device initialization. */
2550 
2551 void ipv6_mc_init_dev(struct inet6_dev *idev)
2552 {
2553 	write_lock_bh(&idev->lock);
2554 	spin_lock_init(&idev->mc_lock);
2555 	idev->mc_gq_running = 0;
2556 	setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2557 			(unsigned long)idev);
2558 	idev->mc_tomb = NULL;
2559 	idev->mc_ifc_count = 0;
2560 	setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2561 			(unsigned long)idev);
2562 	setup_timer(&idev->mc_dad_timer, mld_dad_timer_expire,
2563 		    (unsigned long)idev);
2564 	ipv6_mc_reset(idev);
2565 	write_unlock_bh(&idev->lock);
2566 }
2567 
2568 /*
2569  *	Device is about to be destroyed: clean up.
2570  */
2571 
2572 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2573 {
2574 	struct ifmcaddr6 *i;
2575 
2576 	/* Deactivate timers */
2577 	ipv6_mc_down(idev);
2578 	mld_clear_delrec(idev);
2579 
2580 	/* Delete all-nodes address. */
2581 	/* We cannot call ipv6_dev_mc_dec() directly, our caller in
2582 	 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2583 	 * fail.
2584 	 */
2585 	__ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
2586 
2587 	if (idev->cnf.forwarding)
2588 		__ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
2589 
2590 	write_lock_bh(&idev->lock);
2591 	while ((i = idev->mc_list) != NULL) {
2592 		idev->mc_list = i->next;
2593 
2594 		write_unlock_bh(&idev->lock);
2595 		ma_put(i);
2596 		write_lock_bh(&idev->lock);
2597 	}
2598 	write_unlock_bh(&idev->lock);
2599 }
2600 
2601 #ifdef CONFIG_PROC_FS
2602 struct igmp6_mc_iter_state {
2603 	struct seq_net_private p;
2604 	struct net_device *dev;
2605 	struct inet6_dev *idev;
2606 };
2607 
2608 #define igmp6_mc_seq_private(seq)	((struct igmp6_mc_iter_state *)(seq)->private)
2609 
2610 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2611 {
2612 	struct ifmcaddr6 *im = NULL;
2613 	struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2614 	struct net *net = seq_file_net(seq);
2615 
2616 	state->idev = NULL;
2617 	for_each_netdev_rcu(net, state->dev) {
2618 		struct inet6_dev *idev;
2619 		idev = __in6_dev_get(state->dev);
2620 		if (!idev)
2621 			continue;
2622 		read_lock_bh(&idev->lock);
2623 		im = idev->mc_list;
2624 		if (im) {
2625 			state->idev = idev;
2626 			break;
2627 		}
2628 		read_unlock_bh(&idev->lock);
2629 	}
2630 	return im;
2631 }
2632 
2633 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2634 {
2635 	struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2636 
2637 	im = im->next;
2638 	while (!im) {
2639 		if (likely(state->idev))
2640 			read_unlock_bh(&state->idev->lock);
2641 
2642 		state->dev = next_net_device_rcu(state->dev);
2643 		if (!state->dev) {
2644 			state->idev = NULL;
2645 			break;
2646 		}
2647 		state->idev = __in6_dev_get(state->dev);
2648 		if (!state->idev)
2649 			continue;
2650 		read_lock_bh(&state->idev->lock);
2651 		im = state->idev->mc_list;
2652 	}
2653 	return im;
2654 }
2655 
2656 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2657 {
2658 	struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2659 	if (im)
2660 		while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2661 			--pos;
2662 	return pos ? NULL : im;
2663 }
2664 
2665 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2666 	__acquires(RCU)
2667 {
2668 	rcu_read_lock();
2669 	return igmp6_mc_get_idx(seq, *pos);
2670 }
2671 
2672 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2673 {
2674 	struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2675 
2676 	++*pos;
2677 	return im;
2678 }
2679 
2680 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2681 	__releases(RCU)
2682 {
2683 	struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2684 
2685 	if (likely(state->idev)) {
2686 		read_unlock_bh(&state->idev->lock);
2687 		state->idev = NULL;
2688 	}
2689 	state->dev = NULL;
2690 	rcu_read_unlock();
2691 }
2692 
2693 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2694 {
2695 	struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2696 	struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2697 
2698 	seq_printf(seq,
2699 		   "%-4d %-15s %pi6 %5d %08X %ld\n",
2700 		   state->dev->ifindex, state->dev->name,
2701 		   &im->mca_addr,
2702 		   im->mca_users, im->mca_flags,
2703 		   (im->mca_flags&MAF_TIMER_RUNNING) ?
2704 		   jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2705 	return 0;
2706 }
2707 
2708 static const struct seq_operations igmp6_mc_seq_ops = {
2709 	.start	=	igmp6_mc_seq_start,
2710 	.next	=	igmp6_mc_seq_next,
2711 	.stop	=	igmp6_mc_seq_stop,
2712 	.show	=	igmp6_mc_seq_show,
2713 };
2714 
2715 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2716 {
2717 	return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2718 			    sizeof(struct igmp6_mc_iter_state));
2719 }
2720 
2721 static const struct file_operations igmp6_mc_seq_fops = {
2722 	.owner		=	THIS_MODULE,
2723 	.open		=	igmp6_mc_seq_open,
2724 	.read		=	seq_read,
2725 	.llseek		=	seq_lseek,
2726 	.release	=	seq_release_net,
2727 };
2728 
2729 struct igmp6_mcf_iter_state {
2730 	struct seq_net_private p;
2731 	struct net_device *dev;
2732 	struct inet6_dev *idev;
2733 	struct ifmcaddr6 *im;
2734 };
2735 
2736 #define igmp6_mcf_seq_private(seq)	((struct igmp6_mcf_iter_state *)(seq)->private)
2737 
2738 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2739 {
2740 	struct ip6_sf_list *psf = NULL;
2741 	struct ifmcaddr6 *im = NULL;
2742 	struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2743 	struct net *net = seq_file_net(seq);
2744 
2745 	state->idev = NULL;
2746 	state->im = NULL;
2747 	for_each_netdev_rcu(net, state->dev) {
2748 		struct inet6_dev *idev;
2749 		idev = __in6_dev_get(state->dev);
2750 		if (unlikely(idev == NULL))
2751 			continue;
2752 		read_lock_bh(&idev->lock);
2753 		im = idev->mc_list;
2754 		if (likely(im)) {
2755 			spin_lock_bh(&im->mca_lock);
2756 			psf = im->mca_sources;
2757 			if (likely(psf)) {
2758 				state->im = im;
2759 				state->idev = idev;
2760 				break;
2761 			}
2762 			spin_unlock_bh(&im->mca_lock);
2763 		}
2764 		read_unlock_bh(&idev->lock);
2765 	}
2766 	return psf;
2767 }
2768 
2769 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2770 {
2771 	struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2772 
2773 	psf = psf->sf_next;
2774 	while (!psf) {
2775 		spin_unlock_bh(&state->im->mca_lock);
2776 		state->im = state->im->next;
2777 		while (!state->im) {
2778 			if (likely(state->idev))
2779 				read_unlock_bh(&state->idev->lock);
2780 
2781 			state->dev = next_net_device_rcu(state->dev);
2782 			if (!state->dev) {
2783 				state->idev = NULL;
2784 				goto out;
2785 			}
2786 			state->idev = __in6_dev_get(state->dev);
2787 			if (!state->idev)
2788 				continue;
2789 			read_lock_bh(&state->idev->lock);
2790 			state->im = state->idev->mc_list;
2791 		}
2792 		if (!state->im)
2793 			break;
2794 		spin_lock_bh(&state->im->mca_lock);
2795 		psf = state->im->mca_sources;
2796 	}
2797 out:
2798 	return psf;
2799 }
2800 
2801 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2802 {
2803 	struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2804 	if (psf)
2805 		while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2806 			--pos;
2807 	return pos ? NULL : psf;
2808 }
2809 
2810 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2811 	__acquires(RCU)
2812 {
2813 	rcu_read_lock();
2814 	return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2815 }
2816 
2817 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2818 {
2819 	struct ip6_sf_list *psf;
2820 	if (v == SEQ_START_TOKEN)
2821 		psf = igmp6_mcf_get_first(seq);
2822 	else
2823 		psf = igmp6_mcf_get_next(seq, v);
2824 	++*pos;
2825 	return psf;
2826 }
2827 
2828 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2829 	__releases(RCU)
2830 {
2831 	struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2832 	if (likely(state->im)) {
2833 		spin_unlock_bh(&state->im->mca_lock);
2834 		state->im = NULL;
2835 	}
2836 	if (likely(state->idev)) {
2837 		read_unlock_bh(&state->idev->lock);
2838 		state->idev = NULL;
2839 	}
2840 	state->dev = NULL;
2841 	rcu_read_unlock();
2842 }
2843 
2844 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2845 {
2846 	struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2847 	struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2848 
2849 	if (v == SEQ_START_TOKEN) {
2850 		seq_puts(seq, "Idx Device                Multicast Address                   Source Address    INC    EXC\n");
2851 	} else {
2852 		seq_printf(seq,
2853 			   "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
2854 			   state->dev->ifindex, state->dev->name,
2855 			   &state->im->mca_addr,
2856 			   &psf->sf_addr,
2857 			   psf->sf_count[MCAST_INCLUDE],
2858 			   psf->sf_count[MCAST_EXCLUDE]);
2859 	}
2860 	return 0;
2861 }
2862 
2863 static const struct seq_operations igmp6_mcf_seq_ops = {
2864 	.start	=	igmp6_mcf_seq_start,
2865 	.next	=	igmp6_mcf_seq_next,
2866 	.stop	=	igmp6_mcf_seq_stop,
2867 	.show	=	igmp6_mcf_seq_show,
2868 };
2869 
2870 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2871 {
2872 	return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2873 			    sizeof(struct igmp6_mcf_iter_state));
2874 }
2875 
2876 static const struct file_operations igmp6_mcf_seq_fops = {
2877 	.owner		=	THIS_MODULE,
2878 	.open		=	igmp6_mcf_seq_open,
2879 	.read		=	seq_read,
2880 	.llseek		=	seq_lseek,
2881 	.release	=	seq_release_net,
2882 };
2883 
2884 static int __net_init igmp6_proc_init(struct net *net)
2885 {
2886 	int err;
2887 
2888 	err = -ENOMEM;
2889 	if (!proc_create("igmp6", S_IRUGO, net->proc_net, &igmp6_mc_seq_fops))
2890 		goto out;
2891 	if (!proc_create("mcfilter6", S_IRUGO, net->proc_net,
2892 			 &igmp6_mcf_seq_fops))
2893 		goto out_proc_net_igmp6;
2894 
2895 	err = 0;
2896 out:
2897 	return err;
2898 
2899 out_proc_net_igmp6:
2900 	remove_proc_entry("igmp6", net->proc_net);
2901 	goto out;
2902 }
2903 
2904 static void __net_exit igmp6_proc_exit(struct net *net)
2905 {
2906 	remove_proc_entry("mcfilter6", net->proc_net);
2907 	remove_proc_entry("igmp6", net->proc_net);
2908 }
2909 #else
2910 static inline int igmp6_proc_init(struct net *net)
2911 {
2912 	return 0;
2913 }
2914 static inline void igmp6_proc_exit(struct net *net)
2915 {
2916 }
2917 #endif
2918 
2919 static int __net_init igmp6_net_init(struct net *net)
2920 {
2921 	int err;
2922 
2923 	err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2924 				   SOCK_RAW, IPPROTO_ICMPV6, net);
2925 	if (err < 0) {
2926 		pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
2927 		       err);
2928 		goto out;
2929 	}
2930 
2931 	inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
2932 
2933 	err = inet_ctl_sock_create(&net->ipv6.mc_autojoin_sk, PF_INET6,
2934 				   SOCK_RAW, IPPROTO_ICMPV6, net);
2935 	if (err < 0) {
2936 		pr_err("Failed to initialize the IGMP6 autojoin socket (err %d)\n",
2937 		       err);
2938 		goto out_sock_create;
2939 	}
2940 
2941 	err = igmp6_proc_init(net);
2942 	if (err)
2943 		goto out_sock_create_autojoin;
2944 
2945 	return 0;
2946 
2947 out_sock_create_autojoin:
2948 	inet_ctl_sock_destroy(net->ipv6.mc_autojoin_sk);
2949 out_sock_create:
2950 	inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2951 out:
2952 	return err;
2953 }
2954 
2955 static void __net_exit igmp6_net_exit(struct net *net)
2956 {
2957 	inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2958 	inet_ctl_sock_destroy(net->ipv6.mc_autojoin_sk);
2959 	igmp6_proc_exit(net);
2960 }
2961 
2962 static struct pernet_operations igmp6_net_ops = {
2963 	.init = igmp6_net_init,
2964 	.exit = igmp6_net_exit,
2965 };
2966 
2967 int __init igmp6_init(void)
2968 {
2969 	return register_pernet_subsys(&igmp6_net_ops);
2970 }
2971 
2972 void igmp6_cleanup(void)
2973 {
2974 	unregister_pernet_subsys(&igmp6_net_ops);
2975 }
2976