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