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