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