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