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