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