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