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