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