Merge tag 'rproc-v6.14' of git://git.kernel.org/pub/scm/linux/kernel/git/remoteproc...
[linux.git] / net / ipv6 / mcast.c
blob9dfdb40988b0f8edd882c07b555ea0115ee95cab
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Multicast support for IPv6
4 * Linux INET6 implementation
6 * Authors:
7 * Pedro Roque <roque@di.fc.ul.pt>
9 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
12 /* Changes:
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
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_addr.h>
37 #include <linux/if_arp.h>
38 #include <linux/route.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/init.h>
41 #include <linux/proc_fs.h>
42 #include <linux/seq_file.h>
43 #include <linux/slab.h>
44 #include <linux/pkt_sched.h>
45 #include <net/mld.h>
46 #include <linux/workqueue.h>
48 #include <linux/netfilter.h>
49 #include <linux/netfilter_ipv6.h>
51 #include <net/net_namespace.h>
52 #include <net/netlink.h>
53 #include <net/sock.h>
54 #include <net/snmp.h>
56 #include <net/ipv6.h>
57 #include <net/protocol.h>
58 #include <net/if_inet6.h>
59 #include <net/ndisc.h>
60 #include <net/addrconf.h>
61 #include <net/ip6_route.h>
62 #include <net/inet_common.h>
64 #include <net/ip6_checksum.h>
66 /* Ensure that we have struct in6_addr aligned on 32bit word. */
67 static int __mld2_query_bugs[] __attribute__((__unused__)) = {
68 BUILD_BUG_ON_ZERO(offsetof(struct mld2_query, mld2q_srcs) % 4),
69 BUILD_BUG_ON_ZERO(offsetof(struct mld2_report, mld2r_grec) % 4),
70 BUILD_BUG_ON_ZERO(offsetof(struct mld2_grec, grec_mca) % 4)
73 static struct workqueue_struct *mld_wq;
74 static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
76 static void igmp6_join_group(struct ifmcaddr6 *ma);
77 static void igmp6_leave_group(struct ifmcaddr6 *ma);
78 static void mld_mca_work(struct work_struct *work);
80 static void mld_ifc_event(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);
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)
102 /* RFC3810, 8.1 Query Version Distinctions */
103 #define MLD_V1_QUERY_LEN 24
104 #define MLD_V2_QUERY_LEN_MIN 28
106 #define IPV6_MLD_MAX_MSF 64
108 int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
109 int sysctl_mld_qrv __read_mostly = MLD_QRV_DEFAULT;
112 * socket join on multicast group
114 #define mc_dereference(e, idev) \
115 rcu_dereference_protected(e, lockdep_is_held(&(idev)->mc_lock))
117 #define sock_dereference(e, sk) \
118 rcu_dereference_protected(e, lockdep_sock_is_held(sk))
120 #define for_each_pmc_socklock(np, sk, pmc) \
121 for (pmc = sock_dereference((np)->ipv6_mc_list, sk); \
122 pmc; \
123 pmc = sock_dereference(pmc->next, sk))
125 #define for_each_pmc_rcu(np, pmc) \
126 for (pmc = rcu_dereference((np)->ipv6_mc_list); \
127 pmc; \
128 pmc = rcu_dereference(pmc->next))
130 #define for_each_psf_mclock(mc, psf) \
131 for (psf = mc_dereference((mc)->mca_sources, mc->idev); \
132 psf; \
133 psf = mc_dereference(psf->sf_next, mc->idev))
135 #define for_each_psf_rcu(mc, psf) \
136 for (psf = rcu_dereference((mc)->mca_sources); \
137 psf; \
138 psf = rcu_dereference(psf->sf_next))
140 #define for_each_psf_tomb(mc, psf) \
141 for (psf = mc_dereference((mc)->mca_tomb, mc->idev); \
142 psf; \
143 psf = mc_dereference(psf->sf_next, mc->idev))
145 #define for_each_mc_mclock(idev, mc) \
146 for (mc = mc_dereference((idev)->mc_list, idev); \
147 mc; \
148 mc = mc_dereference(mc->next, idev))
150 #define for_each_mc_rcu(idev, mc) \
151 for (mc = rcu_dereference((idev)->mc_list); \
152 mc; \
153 mc = rcu_dereference(mc->next))
155 #define for_each_mc_tomb(idev, mc) \
156 for (mc = mc_dereference((idev)->mc_tomb, idev); \
157 mc; \
158 mc = mc_dereference(mc->next, idev))
160 static int unsolicited_report_interval(struct inet6_dev *idev)
162 int iv;
164 if (mld_in_v1_mode(idev))
165 iv = READ_ONCE(idev->cnf.mldv1_unsolicited_report_interval);
166 else
167 iv = READ_ONCE(idev->cnf.mldv2_unsolicited_report_interval);
169 return iv > 0 ? iv : 1;
172 static int __ipv6_sock_mc_join(struct sock *sk, int ifindex,
173 const struct in6_addr *addr, unsigned int mode)
175 struct net_device *dev = NULL;
176 struct ipv6_mc_socklist *mc_lst;
177 struct ipv6_pinfo *np = inet6_sk(sk);
178 struct net *net = sock_net(sk);
179 int err;
181 ASSERT_RTNL();
183 if (!ipv6_addr_is_multicast(addr))
184 return -EINVAL;
186 for_each_pmc_socklock(np, sk, mc_lst) {
187 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
188 ipv6_addr_equal(&mc_lst->addr, addr))
189 return -EADDRINUSE;
192 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
194 if (!mc_lst)
195 return -ENOMEM;
197 mc_lst->next = NULL;
198 mc_lst->addr = *addr;
200 if (ifindex == 0) {
201 struct rt6_info *rt;
202 rt = rt6_lookup(net, addr, NULL, 0, NULL, 0);
203 if (rt) {
204 dev = rt->dst.dev;
205 ip6_rt_put(rt);
207 } else
208 dev = __dev_get_by_index(net, ifindex);
210 if (!dev) {
211 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
212 return -ENODEV;
215 mc_lst->ifindex = dev->ifindex;
216 mc_lst->sfmode = mode;
217 RCU_INIT_POINTER(mc_lst->sflist, NULL);
220 * now add/increase the group membership on the device
223 err = __ipv6_dev_mc_inc(dev, addr, mode);
225 if (err) {
226 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
227 return err;
230 mc_lst->next = np->ipv6_mc_list;
231 rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
233 return 0;
236 int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
238 return __ipv6_sock_mc_join(sk, ifindex, addr, MCAST_EXCLUDE);
240 EXPORT_SYMBOL(ipv6_sock_mc_join);
242 int ipv6_sock_mc_join_ssm(struct sock *sk, int ifindex,
243 const struct in6_addr *addr, unsigned int mode)
245 return __ipv6_sock_mc_join(sk, ifindex, addr, mode);
249 * socket leave on multicast group
251 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
253 struct ipv6_pinfo *np = inet6_sk(sk);
254 struct ipv6_mc_socklist *mc_lst;
255 struct ipv6_mc_socklist __rcu **lnk;
256 struct net *net = sock_net(sk);
258 ASSERT_RTNL();
260 if (!ipv6_addr_is_multicast(addr))
261 return -EINVAL;
263 for (lnk = &np->ipv6_mc_list;
264 (mc_lst = sock_dereference(*lnk, sk)) != NULL;
265 lnk = &mc_lst->next) {
266 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
267 ipv6_addr_equal(&mc_lst->addr, addr)) {
268 struct net_device *dev;
270 *lnk = mc_lst->next;
272 dev = __dev_get_by_index(net, mc_lst->ifindex);
273 if (dev) {
274 struct inet6_dev *idev = __in6_dev_get(dev);
276 ip6_mc_leave_src(sk, mc_lst, idev);
277 if (idev)
278 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
279 } else {
280 ip6_mc_leave_src(sk, mc_lst, NULL);
283 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
284 kfree_rcu(mc_lst, rcu);
285 return 0;
289 return -EADDRNOTAVAIL;
291 EXPORT_SYMBOL(ipv6_sock_mc_drop);
293 static struct inet6_dev *ip6_mc_find_dev_rtnl(struct net *net,
294 const struct in6_addr *group,
295 int ifindex)
297 struct net_device *dev = NULL;
298 struct inet6_dev *idev = NULL;
300 if (ifindex == 0) {
301 struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, NULL, 0);
303 if (rt) {
304 dev = rt->dst.dev;
305 ip6_rt_put(rt);
307 } else {
308 dev = __dev_get_by_index(net, ifindex);
311 if (!dev)
312 return NULL;
313 idev = __in6_dev_get(dev);
314 if (!idev)
315 return NULL;
316 if (idev->dead)
317 return NULL;
318 return idev;
321 void __ipv6_sock_mc_close(struct sock *sk)
323 struct ipv6_pinfo *np = inet6_sk(sk);
324 struct ipv6_mc_socklist *mc_lst;
325 struct net *net = sock_net(sk);
327 ASSERT_RTNL();
329 while ((mc_lst = sock_dereference(np->ipv6_mc_list, sk)) != NULL) {
330 struct net_device *dev;
332 np->ipv6_mc_list = mc_lst->next;
334 dev = __dev_get_by_index(net, mc_lst->ifindex);
335 if (dev) {
336 struct inet6_dev *idev = __in6_dev_get(dev);
338 ip6_mc_leave_src(sk, mc_lst, idev);
339 if (idev)
340 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
341 } else {
342 ip6_mc_leave_src(sk, mc_lst, NULL);
345 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
346 kfree_rcu(mc_lst, rcu);
350 void ipv6_sock_mc_close(struct sock *sk)
352 struct ipv6_pinfo *np = inet6_sk(sk);
354 if (!rcu_access_pointer(np->ipv6_mc_list))
355 return;
357 rtnl_lock();
358 lock_sock(sk);
359 __ipv6_sock_mc_close(sk);
360 release_sock(sk);
361 rtnl_unlock();
364 int ip6_mc_source(int add, int omode, struct sock *sk,
365 struct group_source_req *pgsr)
367 struct in6_addr *source, *group;
368 struct ipv6_mc_socklist *pmc;
369 struct inet6_dev *idev;
370 struct ipv6_pinfo *inet6 = inet6_sk(sk);
371 struct ip6_sf_socklist *psl;
372 struct net *net = sock_net(sk);
373 int i, j, rv;
374 int leavegroup = 0;
375 int err;
377 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
378 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
380 if (!ipv6_addr_is_multicast(group))
381 return -EINVAL;
383 idev = ip6_mc_find_dev_rtnl(net, group, pgsr->gsr_interface);
384 if (!idev)
385 return -ENODEV;
387 err = -EADDRNOTAVAIL;
389 mutex_lock(&idev->mc_lock);
390 for_each_pmc_socklock(inet6, sk, pmc) {
391 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
392 continue;
393 if (ipv6_addr_equal(&pmc->addr, group))
394 break;
396 if (!pmc) { /* must have a prior join */
397 err = -EINVAL;
398 goto done;
400 /* if a source filter was set, must be the same mode as before */
401 if (rcu_access_pointer(pmc->sflist)) {
402 if (pmc->sfmode != omode) {
403 err = -EINVAL;
404 goto done;
406 } else if (pmc->sfmode != omode) {
407 /* allow mode switches for empty-set filters */
408 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
409 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
410 pmc->sfmode = omode;
413 psl = sock_dereference(pmc->sflist, sk);
414 if (!add) {
415 if (!psl)
416 goto done; /* err = -EADDRNOTAVAIL */
417 rv = !0;
418 for (i = 0; i < psl->sl_count; i++) {
419 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
420 if (rv == 0)
421 break;
423 if (rv) /* source not found */
424 goto done; /* err = -EADDRNOTAVAIL */
426 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
427 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
428 leavegroup = 1;
429 goto done;
432 /* update the interface filter */
433 ip6_mc_del_src(idev, group, omode, 1, source, 1);
435 for (j = i+1; j < psl->sl_count; j++)
436 psl->sl_addr[j-1] = psl->sl_addr[j];
437 psl->sl_count--;
438 err = 0;
439 goto done;
441 /* else, add a new source to the filter */
443 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
444 err = -ENOBUFS;
445 goto done;
447 if (!psl || psl->sl_count == psl->sl_max) {
448 struct ip6_sf_socklist *newpsl;
449 int count = IP6_SFBLOCK;
451 if (psl)
452 count += psl->sl_max;
453 newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr, count),
454 GFP_KERNEL);
455 if (!newpsl) {
456 err = -ENOBUFS;
457 goto done;
459 newpsl->sl_max = count;
460 newpsl->sl_count = count - IP6_SFBLOCK;
461 if (psl) {
462 for (i = 0; i < psl->sl_count; i++)
463 newpsl->sl_addr[i] = psl->sl_addr[i];
464 atomic_sub(struct_size(psl, sl_addr, psl->sl_max),
465 &sk->sk_omem_alloc);
467 rcu_assign_pointer(pmc->sflist, newpsl);
468 kfree_rcu(psl, rcu);
469 psl = newpsl;
471 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
472 for (i = 0; i < psl->sl_count; i++) {
473 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
474 if (rv == 0) /* There is an error in the address. */
475 goto done;
477 for (j = psl->sl_count-1; j >= i; j--)
478 psl->sl_addr[j+1] = psl->sl_addr[j];
479 psl->sl_addr[i] = *source;
480 psl->sl_count++;
481 err = 0;
482 /* update the interface list */
483 ip6_mc_add_src(idev, group, omode, 1, source, 1);
484 done:
485 mutex_unlock(&idev->mc_lock);
486 if (leavegroup)
487 err = ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
488 return err;
491 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf,
492 struct sockaddr_storage *list)
494 const struct in6_addr *group;
495 struct ipv6_mc_socklist *pmc;
496 struct inet6_dev *idev;
497 struct ipv6_pinfo *inet6 = inet6_sk(sk);
498 struct ip6_sf_socklist *newpsl, *psl;
499 struct net *net = sock_net(sk);
500 int leavegroup = 0;
501 int i, err;
503 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
505 if (!ipv6_addr_is_multicast(group))
506 return -EINVAL;
507 if (gsf->gf_fmode != MCAST_INCLUDE &&
508 gsf->gf_fmode != MCAST_EXCLUDE)
509 return -EINVAL;
511 idev = ip6_mc_find_dev_rtnl(net, group, gsf->gf_interface);
512 if (!idev)
513 return -ENODEV;
515 err = 0;
517 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
518 leavegroup = 1;
519 goto done;
522 for_each_pmc_socklock(inet6, sk, pmc) {
523 if (pmc->ifindex != gsf->gf_interface)
524 continue;
525 if (ipv6_addr_equal(&pmc->addr, group))
526 break;
528 if (!pmc) { /* must have a prior join */
529 err = -EINVAL;
530 goto done;
532 if (gsf->gf_numsrc) {
533 newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr,
534 gsf->gf_numsrc),
535 GFP_KERNEL);
536 if (!newpsl) {
537 err = -ENOBUFS;
538 goto done;
540 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
541 for (i = 0; i < newpsl->sl_count; ++i, ++list) {
542 struct sockaddr_in6 *psin6;
544 psin6 = (struct sockaddr_in6 *)list;
545 newpsl->sl_addr[i] = psin6->sin6_addr;
547 mutex_lock(&idev->mc_lock);
548 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
549 newpsl->sl_count, newpsl->sl_addr, 0);
550 if (err) {
551 mutex_unlock(&idev->mc_lock);
552 sock_kfree_s(sk, newpsl, struct_size(newpsl, sl_addr,
553 newpsl->sl_max));
554 goto done;
556 mutex_unlock(&idev->mc_lock);
557 } else {
558 newpsl = NULL;
559 mutex_lock(&idev->mc_lock);
560 ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
561 mutex_unlock(&idev->mc_lock);
564 mutex_lock(&idev->mc_lock);
565 psl = sock_dereference(pmc->sflist, sk);
566 if (psl) {
567 ip6_mc_del_src(idev, group, pmc->sfmode,
568 psl->sl_count, psl->sl_addr, 0);
569 atomic_sub(struct_size(psl, sl_addr, psl->sl_max),
570 &sk->sk_omem_alloc);
571 } else {
572 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
574 rcu_assign_pointer(pmc->sflist, newpsl);
575 mutex_unlock(&idev->mc_lock);
576 kfree_rcu(psl, rcu);
577 pmc->sfmode = gsf->gf_fmode;
578 err = 0;
579 done:
580 if (leavegroup)
581 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
582 return err;
585 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
586 sockptr_t optval, size_t ss_offset)
588 struct ipv6_pinfo *inet6 = inet6_sk(sk);
589 const struct in6_addr *group;
590 struct ipv6_mc_socklist *pmc;
591 struct ip6_sf_socklist *psl;
592 unsigned int count;
593 int i, copycount;
595 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
597 if (!ipv6_addr_is_multicast(group))
598 return -EINVAL;
600 /* changes to the ipv6_mc_list require the socket lock and
601 * rtnl lock. We have the socket lock, so reading the list is safe.
604 for_each_pmc_socklock(inet6, sk, pmc) {
605 if (pmc->ifindex != gsf->gf_interface)
606 continue;
607 if (ipv6_addr_equal(group, &pmc->addr))
608 break;
610 if (!pmc) /* must have a prior join */
611 return -EADDRNOTAVAIL;
613 gsf->gf_fmode = pmc->sfmode;
614 psl = sock_dereference(pmc->sflist, sk);
615 count = psl ? psl->sl_count : 0;
617 copycount = min(count, gsf->gf_numsrc);
618 gsf->gf_numsrc = count;
619 for (i = 0; i < copycount; i++) {
620 struct sockaddr_in6 *psin6;
621 struct sockaddr_storage ss;
623 psin6 = (struct sockaddr_in6 *)&ss;
624 memset(&ss, 0, sizeof(ss));
625 psin6->sin6_family = AF_INET6;
626 psin6->sin6_addr = psl->sl_addr[i];
627 if (copy_to_sockptr_offset(optval, ss_offset, &ss, sizeof(ss)))
628 return -EFAULT;
629 ss_offset += sizeof(ss);
631 return 0;
634 bool inet6_mc_check(const struct sock *sk, const struct in6_addr *mc_addr,
635 const struct in6_addr *src_addr)
637 const struct ipv6_pinfo *np = inet6_sk(sk);
638 const struct ipv6_mc_socklist *mc;
639 const struct ip6_sf_socklist *psl;
640 bool rv = true;
642 rcu_read_lock();
643 for_each_pmc_rcu(np, mc) {
644 if (ipv6_addr_equal(&mc->addr, mc_addr))
645 break;
647 if (!mc) {
648 rcu_read_unlock();
649 return inet6_test_bit(MC6_ALL, sk);
651 psl = rcu_dereference(mc->sflist);
652 if (!psl) {
653 rv = mc->sfmode == MCAST_EXCLUDE;
654 } else {
655 int i;
657 for (i = 0; i < psl->sl_count; i++) {
658 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
659 break;
661 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
662 rv = false;
663 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
664 rv = false;
666 rcu_read_unlock();
668 return rv;
671 /* called with mc_lock */
672 static void igmp6_group_added(struct ifmcaddr6 *mc)
674 struct net_device *dev = mc->idev->dev;
675 char buf[MAX_ADDR_LEN];
677 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
678 IPV6_ADDR_SCOPE_LINKLOCAL)
679 return;
681 if (!(mc->mca_flags&MAF_LOADED)) {
682 mc->mca_flags |= MAF_LOADED;
683 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
684 dev_mc_add(dev, buf);
687 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
688 return;
690 if (mld_in_v1_mode(mc->idev)) {
691 igmp6_join_group(mc);
692 return;
694 /* else v2 */
696 /* Based on RFC3810 6.1, for newly added INCLUDE SSM, we
697 * should not send filter-mode change record as the mode
698 * should be from IN() to IN(A).
700 if (mc->mca_sfmode == MCAST_EXCLUDE)
701 mc->mca_crcount = mc->idev->mc_qrv;
703 mld_ifc_event(mc->idev);
706 /* called with mc_lock */
707 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
709 struct net_device *dev = mc->idev->dev;
710 char buf[MAX_ADDR_LEN];
712 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
713 IPV6_ADDR_SCOPE_LINKLOCAL)
714 return;
716 if (mc->mca_flags&MAF_LOADED) {
717 mc->mca_flags &= ~MAF_LOADED;
718 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
719 dev_mc_del(dev, buf);
722 if (mc->mca_flags & MAF_NOREPORT)
723 return;
725 if (!mc->idev->dead)
726 igmp6_leave_group(mc);
728 if (cancel_delayed_work(&mc->mca_work))
729 refcount_dec(&mc->mca_refcnt);
733 * deleted ifmcaddr6 manipulation
734 * called with mc_lock
736 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
738 struct ifmcaddr6 *pmc;
740 /* this is an "ifmcaddr6" for convenience; only the fields below
741 * are actually used. In particular, the refcnt and users are not
742 * used for management of the delete list. Using the same structure
743 * for deleted items allows change reports to use common code with
744 * non-deleted or query-response MCA's.
746 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
747 if (!pmc)
748 return;
750 pmc->idev = im->idev;
751 in6_dev_hold(idev);
752 pmc->mca_addr = im->mca_addr;
753 pmc->mca_crcount = idev->mc_qrv;
754 pmc->mca_sfmode = im->mca_sfmode;
755 if (pmc->mca_sfmode == MCAST_INCLUDE) {
756 struct ip6_sf_list *psf;
758 rcu_assign_pointer(pmc->mca_tomb,
759 mc_dereference(im->mca_tomb, idev));
760 rcu_assign_pointer(pmc->mca_sources,
761 mc_dereference(im->mca_sources, idev));
762 RCU_INIT_POINTER(im->mca_tomb, NULL);
763 RCU_INIT_POINTER(im->mca_sources, NULL);
765 for_each_psf_mclock(pmc, psf)
766 psf->sf_crcount = pmc->mca_crcount;
769 rcu_assign_pointer(pmc->next, idev->mc_tomb);
770 rcu_assign_pointer(idev->mc_tomb, pmc);
773 /* called with mc_lock */
774 static void mld_del_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
776 struct ip6_sf_list *psf, *sources, *tomb;
777 struct in6_addr *pmca = &im->mca_addr;
778 struct ifmcaddr6 *pmc, *pmc_prev;
780 pmc_prev = NULL;
781 for_each_mc_tomb(idev, pmc) {
782 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
783 break;
784 pmc_prev = pmc;
786 if (pmc) {
787 if (pmc_prev)
788 rcu_assign_pointer(pmc_prev->next, pmc->next);
789 else
790 rcu_assign_pointer(idev->mc_tomb, pmc->next);
793 if (pmc) {
794 im->idev = pmc->idev;
795 if (im->mca_sfmode == MCAST_INCLUDE) {
796 tomb = rcu_replace_pointer(im->mca_tomb,
797 mc_dereference(pmc->mca_tomb, pmc->idev),
798 lockdep_is_held(&im->idev->mc_lock));
799 rcu_assign_pointer(pmc->mca_tomb, tomb);
801 sources = rcu_replace_pointer(im->mca_sources,
802 mc_dereference(pmc->mca_sources, pmc->idev),
803 lockdep_is_held(&im->idev->mc_lock));
804 rcu_assign_pointer(pmc->mca_sources, sources);
805 for_each_psf_mclock(im, psf)
806 psf->sf_crcount = idev->mc_qrv;
807 } else {
808 im->mca_crcount = idev->mc_qrv;
810 in6_dev_put(pmc->idev);
811 ip6_mc_clear_src(pmc);
812 kfree_rcu(pmc, rcu);
816 /* called with mc_lock */
817 static void mld_clear_delrec(struct inet6_dev *idev)
819 struct ifmcaddr6 *pmc, *nextpmc;
821 pmc = mc_dereference(idev->mc_tomb, idev);
822 RCU_INIT_POINTER(idev->mc_tomb, NULL);
824 for (; pmc; pmc = nextpmc) {
825 nextpmc = mc_dereference(pmc->next, idev);
826 ip6_mc_clear_src(pmc);
827 in6_dev_put(pmc->idev);
828 kfree_rcu(pmc, rcu);
831 /* clear dead sources, too */
832 for_each_mc_mclock(idev, pmc) {
833 struct ip6_sf_list *psf, *psf_next;
835 psf = mc_dereference(pmc->mca_tomb, idev);
836 RCU_INIT_POINTER(pmc->mca_tomb, NULL);
837 for (; psf; psf = psf_next) {
838 psf_next = mc_dereference(psf->sf_next, idev);
839 kfree_rcu(psf, rcu);
844 static void mld_clear_query(struct inet6_dev *idev)
846 struct sk_buff *skb;
848 spin_lock_bh(&idev->mc_query_lock);
849 while ((skb = __skb_dequeue(&idev->mc_query_queue)))
850 kfree_skb(skb);
851 spin_unlock_bh(&idev->mc_query_lock);
854 static void mld_clear_report(struct inet6_dev *idev)
856 struct sk_buff *skb;
858 spin_lock_bh(&idev->mc_report_lock);
859 while ((skb = __skb_dequeue(&idev->mc_report_queue)))
860 kfree_skb(skb);
861 spin_unlock_bh(&idev->mc_report_lock);
864 static void mca_get(struct ifmcaddr6 *mc)
866 refcount_inc(&mc->mca_refcnt);
869 static void ma_put(struct ifmcaddr6 *mc)
871 if (refcount_dec_and_test(&mc->mca_refcnt)) {
872 in6_dev_put(mc->idev);
873 kfree_rcu(mc, rcu);
877 /* called with mc_lock */
878 static struct ifmcaddr6 *mca_alloc(struct inet6_dev *idev,
879 const struct in6_addr *addr,
880 unsigned int mode)
882 struct ifmcaddr6 *mc;
884 mc = kzalloc(sizeof(*mc), GFP_KERNEL);
885 if (!mc)
886 return NULL;
888 INIT_DELAYED_WORK(&mc->mca_work, mld_mca_work);
890 mc->mca_addr = *addr;
891 mc->idev = idev; /* reference taken by caller */
892 mc->mca_users = 1;
893 /* mca_stamp should be updated upon changes */
894 mc->mca_cstamp = mc->mca_tstamp = jiffies;
895 refcount_set(&mc->mca_refcnt, 1);
897 mc->mca_sfmode = mode;
898 mc->mca_sfcount[mode] = 1;
900 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
901 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
902 mc->mca_flags |= MAF_NOREPORT;
904 return mc;
907 static void inet6_ifmcaddr_notify(struct net_device *dev,
908 const struct ifmcaddr6 *ifmca, int event)
910 struct inet6_fill_args fillargs = {
911 .portid = 0,
912 .seq = 0,
913 .event = event,
914 .flags = 0,
915 .netnsid = -1,
916 .force_rt_scope_universe = true,
918 struct net *net = dev_net(dev);
919 struct sk_buff *skb;
920 int err = -ENOMEM;
922 skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
923 nla_total_size(sizeof(struct in6_addr)) +
924 nla_total_size(sizeof(struct ifa_cacheinfo)),
925 GFP_KERNEL);
926 if (!skb)
927 goto error;
929 err = inet6_fill_ifmcaddr(skb, ifmca, &fillargs);
930 if (err < 0) {
931 WARN_ON_ONCE(err == -EMSGSIZE);
932 nlmsg_free(skb);
933 goto error;
936 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_MCADDR, NULL, GFP_KERNEL);
937 return;
938 error:
939 rtnl_set_sk_err(net, RTNLGRP_IPV6_MCADDR, err);
943 * device multicast group inc (add if not found)
945 static int __ipv6_dev_mc_inc(struct net_device *dev,
946 const struct in6_addr *addr, unsigned int mode)
948 struct ifmcaddr6 *mc;
949 struct inet6_dev *idev;
951 ASSERT_RTNL();
953 /* we need to take a reference on idev */
954 idev = in6_dev_get(dev);
956 if (!idev)
957 return -EINVAL;
959 if (idev->dead) {
960 in6_dev_put(idev);
961 return -ENODEV;
964 mutex_lock(&idev->mc_lock);
965 for_each_mc_mclock(idev, mc) {
966 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
967 mc->mca_users++;
968 ip6_mc_add_src(idev, &mc->mca_addr, mode, 0, NULL, 0);
969 mutex_unlock(&idev->mc_lock);
970 in6_dev_put(idev);
971 return 0;
975 mc = mca_alloc(idev, addr, mode);
976 if (!mc) {
977 mutex_unlock(&idev->mc_lock);
978 in6_dev_put(idev);
979 return -ENOMEM;
982 rcu_assign_pointer(mc->next, idev->mc_list);
983 rcu_assign_pointer(idev->mc_list, mc);
985 mca_get(mc);
987 mld_del_delrec(idev, mc);
988 igmp6_group_added(mc);
989 inet6_ifmcaddr_notify(dev, mc, RTM_NEWMULTICAST);
990 mutex_unlock(&idev->mc_lock);
991 ma_put(mc);
992 return 0;
995 int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
997 return __ipv6_dev_mc_inc(dev, addr, MCAST_EXCLUDE);
999 EXPORT_SYMBOL(ipv6_dev_mc_inc);
1002 * device multicast group del
1004 int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
1006 struct ifmcaddr6 *ma, __rcu **map;
1008 ASSERT_RTNL();
1010 mutex_lock(&idev->mc_lock);
1011 for (map = &idev->mc_list;
1012 (ma = mc_dereference(*map, idev));
1013 map = &ma->next) {
1014 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
1015 if (--ma->mca_users == 0) {
1016 *map = ma->next;
1018 igmp6_group_dropped(ma);
1019 inet6_ifmcaddr_notify(idev->dev, ma,
1020 RTM_DELMULTICAST);
1021 ip6_mc_clear_src(ma);
1022 mutex_unlock(&idev->mc_lock);
1024 ma_put(ma);
1025 return 0;
1027 mutex_unlock(&idev->mc_lock);
1028 return 0;
1032 mutex_unlock(&idev->mc_lock);
1033 return -ENOENT;
1036 int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
1038 struct inet6_dev *idev;
1039 int err;
1041 ASSERT_RTNL();
1043 idev = __in6_dev_get(dev);
1044 if (!idev)
1045 err = -ENODEV;
1046 else
1047 err = __ipv6_dev_mc_dec(idev, addr);
1049 return err;
1051 EXPORT_SYMBOL(ipv6_dev_mc_dec);
1054 * check if the interface/address pair is valid
1056 bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
1057 const struct in6_addr *src_addr)
1059 struct inet6_dev *idev;
1060 struct ifmcaddr6 *mc;
1061 bool rv = false;
1063 rcu_read_lock();
1064 idev = __in6_dev_get(dev);
1065 if (!idev)
1066 goto unlock;
1067 for_each_mc_rcu(idev, mc) {
1068 if (ipv6_addr_equal(&mc->mca_addr, group))
1069 break;
1071 if (!mc)
1072 goto unlock;
1073 if (src_addr && !ipv6_addr_any(src_addr)) {
1074 struct ip6_sf_list *psf;
1076 for_each_psf_rcu(mc, psf) {
1077 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1078 break;
1080 if (psf)
1081 rv = READ_ONCE(psf->sf_count[MCAST_INCLUDE]) ||
1082 READ_ONCE(psf->sf_count[MCAST_EXCLUDE]) !=
1083 READ_ONCE(mc->mca_sfcount[MCAST_EXCLUDE]);
1084 else
1085 rv = READ_ONCE(mc->mca_sfcount[MCAST_EXCLUDE]) != 0;
1086 } else {
1087 rv = true; /* don't filter unspecified source */
1089 unlock:
1090 rcu_read_unlock();
1091 return rv;
1094 /* called with mc_lock */
1095 static void mld_gq_start_work(struct inet6_dev *idev)
1097 unsigned long tv = get_random_u32_below(idev->mc_maxdelay);
1099 idev->mc_gq_running = 1;
1100 if (!mod_delayed_work(mld_wq, &idev->mc_gq_work, tv + 2))
1101 in6_dev_hold(idev);
1104 /* called with mc_lock */
1105 static void mld_gq_stop_work(struct inet6_dev *idev)
1107 idev->mc_gq_running = 0;
1108 if (cancel_delayed_work(&idev->mc_gq_work))
1109 __in6_dev_put(idev);
1112 /* called with mc_lock */
1113 static void mld_ifc_start_work(struct inet6_dev *idev, unsigned long delay)
1115 unsigned long tv = get_random_u32_below(delay);
1117 if (!mod_delayed_work(mld_wq, &idev->mc_ifc_work, tv + 2))
1118 in6_dev_hold(idev);
1121 /* called with mc_lock */
1122 static void mld_ifc_stop_work(struct inet6_dev *idev)
1124 idev->mc_ifc_count = 0;
1125 if (cancel_delayed_work(&idev->mc_ifc_work))
1126 __in6_dev_put(idev);
1129 /* called with mc_lock */
1130 static void mld_dad_start_work(struct inet6_dev *idev, unsigned long delay)
1132 unsigned long tv = get_random_u32_below(delay);
1134 if (!mod_delayed_work(mld_wq, &idev->mc_dad_work, tv + 2))
1135 in6_dev_hold(idev);
1138 static void mld_dad_stop_work(struct inet6_dev *idev)
1140 if (cancel_delayed_work(&idev->mc_dad_work))
1141 __in6_dev_put(idev);
1144 static void mld_query_stop_work(struct inet6_dev *idev)
1146 spin_lock_bh(&idev->mc_query_lock);
1147 if (cancel_delayed_work(&idev->mc_query_work))
1148 __in6_dev_put(idev);
1149 spin_unlock_bh(&idev->mc_query_lock);
1152 static void mld_report_stop_work(struct inet6_dev *idev)
1154 if (cancel_delayed_work_sync(&idev->mc_report_work))
1155 __in6_dev_put(idev);
1159 * IGMP handling (alias multicast ICMPv6 messages)
1160 * called with mc_lock
1162 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1164 unsigned long delay = resptime;
1166 /* Do not start work for these addresses */
1167 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1168 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1169 return;
1171 if (cancel_delayed_work(&ma->mca_work)) {
1172 refcount_dec(&ma->mca_refcnt);
1173 delay = ma->mca_work.timer.expires - jiffies;
1176 if (delay >= resptime)
1177 delay = get_random_u32_below(resptime);
1179 if (!mod_delayed_work(mld_wq, &ma->mca_work, delay))
1180 refcount_inc(&ma->mca_refcnt);
1181 ma->mca_flags |= MAF_TIMER_RUNNING;
1184 /* mark EXCLUDE-mode sources
1185 * called with mc_lock
1187 static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1188 const struct in6_addr *srcs)
1190 struct ip6_sf_list *psf;
1191 int i, scount;
1193 scount = 0;
1194 for_each_psf_mclock(pmc, psf) {
1195 if (scount == nsrcs)
1196 break;
1197 for (i = 0; i < nsrcs; i++) {
1198 /* skip inactive filters */
1199 if (psf->sf_count[MCAST_INCLUDE] ||
1200 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1201 psf->sf_count[MCAST_EXCLUDE])
1202 break;
1203 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1204 scount++;
1205 break;
1209 pmc->mca_flags &= ~MAF_GSQUERY;
1210 if (scount == nsrcs) /* all sources excluded */
1211 return false;
1212 return true;
1215 /* called with mc_lock */
1216 static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1217 const struct in6_addr *srcs)
1219 struct ip6_sf_list *psf;
1220 int i, scount;
1222 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1223 return mld_xmarksources(pmc, nsrcs, srcs);
1225 /* mark INCLUDE-mode sources */
1227 scount = 0;
1228 for_each_psf_mclock(pmc, psf) {
1229 if (scount == nsrcs)
1230 break;
1231 for (i = 0; i < nsrcs; i++) {
1232 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1233 psf->sf_gsresp = 1;
1234 scount++;
1235 break;
1239 if (!scount) {
1240 pmc->mca_flags &= ~MAF_GSQUERY;
1241 return false;
1243 pmc->mca_flags |= MAF_GSQUERY;
1244 return true;
1247 static int mld_force_mld_version(const struct inet6_dev *idev)
1249 const struct net *net = dev_net(idev->dev);
1250 int all_force;
1252 all_force = READ_ONCE(net->ipv6.devconf_all->force_mld_version);
1253 /* Normally, both are 0 here. If enforcement to a particular is
1254 * being used, individual device enforcement will have a lower
1255 * precedence over 'all' device (.../conf/all/force_mld_version).
1257 return all_force ?: READ_ONCE(idev->cnf.force_mld_version);
1260 static bool mld_in_v2_mode_only(const struct inet6_dev *idev)
1262 return mld_force_mld_version(idev) == 2;
1265 static bool mld_in_v1_mode_only(const struct inet6_dev *idev)
1267 return mld_force_mld_version(idev) == 1;
1270 static bool mld_in_v1_mode(const struct inet6_dev *idev)
1272 if (mld_in_v2_mode_only(idev))
1273 return false;
1274 if (mld_in_v1_mode_only(idev))
1275 return true;
1276 if (idev->mc_v1_seen && time_before(jiffies, idev->mc_v1_seen))
1277 return true;
1279 return false;
1282 static void mld_set_v1_mode(struct inet6_dev *idev)
1284 /* RFC3810, relevant sections:
1285 * - 9.1. Robustness Variable
1286 * - 9.2. Query Interval
1287 * - 9.3. Query Response Interval
1288 * - 9.12. Older Version Querier Present Timeout
1290 unsigned long switchback;
1292 switchback = (idev->mc_qrv * idev->mc_qi) + idev->mc_qri;
1294 idev->mc_v1_seen = jiffies + switchback;
1297 static void mld_update_qrv(struct inet6_dev *idev,
1298 const struct mld2_query *mlh2)
1300 /* RFC3810, relevant sections:
1301 * - 5.1.8. QRV (Querier's Robustness Variable)
1302 * - 9.1. Robustness Variable
1305 /* The value of the Robustness Variable MUST NOT be zero,
1306 * and SHOULD NOT be one. Catch this here if we ever run
1307 * into such a case in future.
1309 const int min_qrv = min(MLD_QRV_DEFAULT, sysctl_mld_qrv);
1310 WARN_ON(idev->mc_qrv == 0);
1312 if (mlh2->mld2q_qrv > 0)
1313 idev->mc_qrv = mlh2->mld2q_qrv;
1315 if (unlikely(idev->mc_qrv < min_qrv)) {
1316 net_warn_ratelimited("IPv6: MLD: clamping QRV from %u to %u!\n",
1317 idev->mc_qrv, min_qrv);
1318 idev->mc_qrv = min_qrv;
1322 static void mld_update_qi(struct inet6_dev *idev,
1323 const struct mld2_query *mlh2)
1325 /* RFC3810, relevant sections:
1326 * - 5.1.9. QQIC (Querier's Query Interval Code)
1327 * - 9.2. Query Interval
1328 * - 9.12. Older Version Querier Present Timeout
1329 * (the [Query Interval] in the last Query received)
1331 unsigned long mc_qqi;
1333 if (mlh2->mld2q_qqic < 128) {
1334 mc_qqi = mlh2->mld2q_qqic;
1335 } else {
1336 unsigned long mc_man, mc_exp;
1338 mc_exp = MLDV2_QQIC_EXP(mlh2->mld2q_qqic);
1339 mc_man = MLDV2_QQIC_MAN(mlh2->mld2q_qqic);
1341 mc_qqi = (mc_man | 0x10) << (mc_exp + 3);
1344 idev->mc_qi = mc_qqi * HZ;
1347 static void mld_update_qri(struct inet6_dev *idev,
1348 const struct mld2_query *mlh2)
1350 /* RFC3810, relevant sections:
1351 * - 5.1.3. Maximum Response Code
1352 * - 9.3. Query Response Interval
1354 idev->mc_qri = msecs_to_jiffies(mldv2_mrc(mlh2));
1357 static int mld_process_v1(struct inet6_dev *idev, struct mld_msg *mld,
1358 unsigned long *max_delay, bool v1_query)
1360 unsigned long mldv1_md;
1362 /* Ignore v1 queries */
1363 if (mld_in_v2_mode_only(idev))
1364 return -EINVAL;
1366 mldv1_md = ntohs(mld->mld_maxdelay);
1368 /* When in MLDv1 fallback and a MLDv2 router start-up being
1369 * unaware of current MLDv1 operation, the MRC == MRD mapping
1370 * only works when the exponential algorithm is not being
1371 * used (as MLDv1 is unaware of such things).
1373 * According to the RFC author, the MLDv2 implementations
1374 * he's aware of all use a MRC < 32768 on start up queries.
1376 * Thus, should we *ever* encounter something else larger
1377 * than that, just assume the maximum possible within our
1378 * reach.
1380 if (!v1_query)
1381 mldv1_md = min(mldv1_md, MLDV1_MRD_MAX_COMPAT);
1383 *max_delay = max(msecs_to_jiffies(mldv1_md), 1UL);
1385 /* MLDv1 router present: we need to go into v1 mode *only*
1386 * when an MLDv1 query is received as per section 9.12. of
1387 * RFC3810! And we know from RFC2710 section 3.7 that MLDv1
1388 * queries MUST be of exactly 24 octets.
1390 if (v1_query)
1391 mld_set_v1_mode(idev);
1393 /* cancel MLDv2 report work */
1394 mld_gq_stop_work(idev);
1395 /* cancel the interface change work */
1396 mld_ifc_stop_work(idev);
1397 /* clear deleted report items */
1398 mld_clear_delrec(idev);
1400 return 0;
1403 static void mld_process_v2(struct inet6_dev *idev, struct mld2_query *mld,
1404 unsigned long *max_delay)
1406 *max_delay = max(msecs_to_jiffies(mldv2_mrc(mld)), 1UL);
1408 mld_update_qrv(idev, mld);
1409 mld_update_qi(idev, mld);
1410 mld_update_qri(idev, mld);
1412 idev->mc_maxdelay = *max_delay;
1414 return;
1417 /* called with rcu_read_lock() */
1418 void igmp6_event_query(struct sk_buff *skb)
1420 struct inet6_dev *idev = __in6_dev_get(skb->dev);
1422 if (!idev || idev->dead)
1423 goto out;
1425 spin_lock_bh(&idev->mc_query_lock);
1426 if (skb_queue_len(&idev->mc_query_queue) < MLD_MAX_SKBS) {
1427 __skb_queue_tail(&idev->mc_query_queue, skb);
1428 if (!mod_delayed_work(mld_wq, &idev->mc_query_work, 0))
1429 in6_dev_hold(idev);
1430 skb = NULL;
1432 spin_unlock_bh(&idev->mc_query_lock);
1433 out:
1434 kfree_skb(skb);
1437 static void __mld_query_work(struct sk_buff *skb)
1439 struct mld2_query *mlh2 = NULL;
1440 const struct in6_addr *group;
1441 unsigned long max_delay;
1442 struct inet6_dev *idev;
1443 struct ifmcaddr6 *ma;
1444 struct mld_msg *mld;
1445 int group_type;
1446 int mark = 0;
1447 int len, err;
1449 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1450 goto kfree_skb;
1452 /* compute payload length excluding extension headers */
1453 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
1454 len -= skb_network_header_len(skb);
1456 /* RFC3810 6.2
1457 * Upon reception of an MLD message that contains a Query, the node
1458 * checks if the source address of the message is a valid link-local
1459 * address, if the Hop Limit is set to 1, and if the Router Alert
1460 * option is present in the Hop-By-Hop Options header of the IPv6
1461 * packet. If any of these checks fails, the packet is dropped.
1463 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL) ||
1464 ipv6_hdr(skb)->hop_limit != 1 ||
1465 !(IP6CB(skb)->flags & IP6SKB_ROUTERALERT) ||
1466 IP6CB(skb)->ra != htons(IPV6_OPT_ROUTERALERT_MLD))
1467 goto kfree_skb;
1469 idev = in6_dev_get(skb->dev);
1470 if (!idev)
1471 goto kfree_skb;
1473 mld = (struct mld_msg *)icmp6_hdr(skb);
1474 group = &mld->mld_mca;
1475 group_type = ipv6_addr_type(group);
1477 if (group_type != IPV6_ADDR_ANY &&
1478 !(group_type&IPV6_ADDR_MULTICAST))
1479 goto out;
1481 if (len < MLD_V1_QUERY_LEN) {
1482 goto out;
1483 } else if (len == MLD_V1_QUERY_LEN || mld_in_v1_mode(idev)) {
1484 err = mld_process_v1(idev, mld, &max_delay,
1485 len == MLD_V1_QUERY_LEN);
1486 if (err < 0)
1487 goto out;
1488 } else if (len >= MLD_V2_QUERY_LEN_MIN) {
1489 int srcs_offset = sizeof(struct mld2_query) -
1490 sizeof(struct icmp6hdr);
1492 if (!pskb_may_pull(skb, srcs_offset))
1493 goto out;
1495 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1497 mld_process_v2(idev, mlh2, &max_delay);
1499 if (group_type == IPV6_ADDR_ANY) { /* general query */
1500 if (mlh2->mld2q_nsrcs)
1501 goto out; /* no sources allowed */
1503 mld_gq_start_work(idev);
1504 goto out;
1506 /* mark sources to include, if group & source-specific */
1507 if (mlh2->mld2q_nsrcs != 0) {
1508 if (!pskb_may_pull(skb, srcs_offset +
1509 ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
1510 goto out;
1512 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1513 mark = 1;
1515 } else {
1516 goto out;
1519 if (group_type == IPV6_ADDR_ANY) {
1520 for_each_mc_mclock(idev, ma) {
1521 igmp6_group_queried(ma, max_delay);
1523 } else {
1524 for_each_mc_mclock(idev, ma) {
1525 if (!ipv6_addr_equal(group, &ma->mca_addr))
1526 continue;
1527 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1528 /* gsquery <- gsquery && mark */
1529 if (!mark)
1530 ma->mca_flags &= ~MAF_GSQUERY;
1531 } else {
1532 /* gsquery <- mark */
1533 if (mark)
1534 ma->mca_flags |= MAF_GSQUERY;
1535 else
1536 ma->mca_flags &= ~MAF_GSQUERY;
1538 if (!(ma->mca_flags & MAF_GSQUERY) ||
1539 mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
1540 igmp6_group_queried(ma, max_delay);
1541 break;
1545 out:
1546 in6_dev_put(idev);
1547 kfree_skb:
1548 consume_skb(skb);
1551 static void mld_query_work(struct work_struct *work)
1553 struct inet6_dev *idev = container_of(to_delayed_work(work),
1554 struct inet6_dev,
1555 mc_query_work);
1556 struct sk_buff_head q;
1557 struct sk_buff *skb;
1558 bool rework = false;
1559 int cnt = 0;
1561 skb_queue_head_init(&q);
1563 spin_lock_bh(&idev->mc_query_lock);
1564 while ((skb = __skb_dequeue(&idev->mc_query_queue))) {
1565 __skb_queue_tail(&q, skb);
1567 if (++cnt >= MLD_MAX_QUEUE) {
1568 rework = true;
1569 break;
1572 spin_unlock_bh(&idev->mc_query_lock);
1574 mutex_lock(&idev->mc_lock);
1575 while ((skb = __skb_dequeue(&q)))
1576 __mld_query_work(skb);
1577 mutex_unlock(&idev->mc_lock);
1579 if (rework && queue_delayed_work(mld_wq, &idev->mc_query_work, 0))
1580 return;
1582 in6_dev_put(idev);
1585 /* called with rcu_read_lock() */
1586 void igmp6_event_report(struct sk_buff *skb)
1588 struct inet6_dev *idev = __in6_dev_get(skb->dev);
1590 if (!idev || idev->dead)
1591 goto out;
1593 spin_lock_bh(&idev->mc_report_lock);
1594 if (skb_queue_len(&idev->mc_report_queue) < MLD_MAX_SKBS) {
1595 __skb_queue_tail(&idev->mc_report_queue, skb);
1596 if (!mod_delayed_work(mld_wq, &idev->mc_report_work, 0))
1597 in6_dev_hold(idev);
1598 skb = NULL;
1600 spin_unlock_bh(&idev->mc_report_lock);
1601 out:
1602 kfree_skb(skb);
1605 static void __mld_report_work(struct sk_buff *skb)
1607 struct inet6_dev *idev;
1608 struct ifmcaddr6 *ma;
1609 struct mld_msg *mld;
1610 int addr_type;
1612 /* Our own report looped back. Ignore it. */
1613 if (skb->pkt_type == PACKET_LOOPBACK)
1614 goto kfree_skb;
1616 /* send our report if the MC router may not have heard this report */
1617 if (skb->pkt_type != PACKET_MULTICAST &&
1618 skb->pkt_type != PACKET_BROADCAST)
1619 goto kfree_skb;
1621 if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
1622 goto kfree_skb;
1624 mld = (struct mld_msg *)icmp6_hdr(skb);
1626 /* Drop reports with not link local source */
1627 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
1628 if (addr_type != IPV6_ADDR_ANY &&
1629 !(addr_type&IPV6_ADDR_LINKLOCAL))
1630 goto kfree_skb;
1632 idev = in6_dev_get(skb->dev);
1633 if (!idev)
1634 goto kfree_skb;
1637 * Cancel the work for this group
1640 for_each_mc_mclock(idev, ma) {
1641 if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
1642 if (cancel_delayed_work(&ma->mca_work))
1643 refcount_dec(&ma->mca_refcnt);
1644 ma->mca_flags &= ~(MAF_LAST_REPORTER |
1645 MAF_TIMER_RUNNING);
1646 break;
1650 in6_dev_put(idev);
1651 kfree_skb:
1652 consume_skb(skb);
1655 static void mld_report_work(struct work_struct *work)
1657 struct inet6_dev *idev = container_of(to_delayed_work(work),
1658 struct inet6_dev,
1659 mc_report_work);
1660 struct sk_buff_head q;
1661 struct sk_buff *skb;
1662 bool rework = false;
1663 int cnt = 0;
1665 skb_queue_head_init(&q);
1666 spin_lock_bh(&idev->mc_report_lock);
1667 while ((skb = __skb_dequeue(&idev->mc_report_queue))) {
1668 __skb_queue_tail(&q, skb);
1670 if (++cnt >= MLD_MAX_QUEUE) {
1671 rework = true;
1672 break;
1675 spin_unlock_bh(&idev->mc_report_lock);
1677 mutex_lock(&idev->mc_lock);
1678 while ((skb = __skb_dequeue(&q)))
1679 __mld_report_work(skb);
1680 mutex_unlock(&idev->mc_lock);
1682 if (rework && queue_delayed_work(mld_wq, &idev->mc_report_work, 0))
1683 return;
1685 in6_dev_put(idev);
1688 static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1689 int gdeleted, int sdeleted)
1691 switch (type) {
1692 case MLD2_MODE_IS_INCLUDE:
1693 case MLD2_MODE_IS_EXCLUDE:
1694 if (gdeleted || sdeleted)
1695 return false;
1696 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1697 if (pmc->mca_sfmode == MCAST_INCLUDE)
1698 return true;
1699 /* don't include if this source is excluded
1700 * in all filters
1702 if (psf->sf_count[MCAST_INCLUDE])
1703 return type == MLD2_MODE_IS_INCLUDE;
1704 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1705 psf->sf_count[MCAST_EXCLUDE];
1707 return false;
1708 case MLD2_CHANGE_TO_INCLUDE:
1709 if (gdeleted || sdeleted)
1710 return false;
1711 return psf->sf_count[MCAST_INCLUDE] != 0;
1712 case MLD2_CHANGE_TO_EXCLUDE:
1713 if (gdeleted || sdeleted)
1714 return false;
1715 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1716 psf->sf_count[MCAST_INCLUDE])
1717 return false;
1718 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1719 psf->sf_count[MCAST_EXCLUDE];
1720 case MLD2_ALLOW_NEW_SOURCES:
1721 if (gdeleted || !psf->sf_crcount)
1722 return false;
1723 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1724 case MLD2_BLOCK_OLD_SOURCES:
1725 if (pmc->mca_sfmode == MCAST_INCLUDE)
1726 return gdeleted || (psf->sf_crcount && sdeleted);
1727 return psf->sf_crcount && !gdeleted && !sdeleted;
1729 return false;
1732 static int
1733 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1735 struct ip6_sf_list *psf;
1736 int scount = 0;
1738 for_each_psf_mclock(pmc, psf) {
1739 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1740 continue;
1741 scount++;
1743 return scount;
1746 static void ip6_mc_hdr(const struct sock *sk, struct sk_buff *skb,
1747 struct net_device *dev, const struct in6_addr *saddr,
1748 const struct in6_addr *daddr, int proto, int len)
1750 struct ipv6hdr *hdr;
1752 skb->protocol = htons(ETH_P_IPV6);
1753 skb->dev = dev;
1755 skb_reset_network_header(skb);
1756 skb_put(skb, sizeof(struct ipv6hdr));
1757 hdr = ipv6_hdr(skb);
1759 ip6_flow_hdr(hdr, 0, 0);
1761 hdr->payload_len = htons(len);
1762 hdr->nexthdr = proto;
1763 hdr->hop_limit = READ_ONCE(inet6_sk(sk)->hop_limit);
1765 hdr->saddr = *saddr;
1766 hdr->daddr = *daddr;
1769 static struct sk_buff *mld_newpack(struct inet6_dev *idev, unsigned int mtu)
1771 u8 ra[8] = { IPPROTO_ICMPV6, 0, IPV6_TLV_ROUTERALERT,
1772 2, 0, 0, IPV6_TLV_PADN, 0 };
1773 struct net_device *dev = idev->dev;
1774 int hlen = LL_RESERVED_SPACE(dev);
1775 int tlen = dev->needed_tailroom;
1776 struct net *net = dev_net(dev);
1777 const struct in6_addr *saddr;
1778 struct in6_addr addr_buf;
1779 struct mld2_report *pmr;
1780 struct sk_buff *skb;
1781 unsigned int size;
1782 struct sock *sk;
1783 int err;
1785 sk = net->ipv6.igmp_sk;
1786 /* we assume size > sizeof(ra) here
1787 * Also try to not allocate high-order pages for big MTU
1789 size = min_t(int, mtu, PAGE_SIZE / 2) + hlen + tlen;
1790 skb = sock_alloc_send_skb(sk, size, 1, &err);
1791 if (!skb)
1792 return NULL;
1794 skb->priority = TC_PRIO_CONTROL;
1795 skb_reserve(skb, hlen);
1796 skb_tailroom_reserve(skb, mtu, tlen);
1798 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1799 /* <draft-ietf-magma-mld-source-05.txt>:
1800 * use unspecified address as the source address
1801 * when a valid link-local address is not available.
1803 saddr = &in6addr_any;
1804 } else
1805 saddr = &addr_buf;
1807 ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
1809 skb_put_data(skb, ra, sizeof(ra));
1811 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
1812 skb_put(skb, sizeof(*pmr));
1813 pmr = (struct mld2_report *)skb_transport_header(skb);
1814 pmr->mld2r_type = ICMPV6_MLD2_REPORT;
1815 pmr->mld2r_resv1 = 0;
1816 pmr->mld2r_cksum = 0;
1817 pmr->mld2r_resv2 = 0;
1818 pmr->mld2r_ngrec = 0;
1819 return skb;
1822 static void mld_sendpack(struct sk_buff *skb)
1824 struct ipv6hdr *pip6 = ipv6_hdr(skb);
1825 struct mld2_report *pmr =
1826 (struct mld2_report *)skb_transport_header(skb);
1827 int payload_len, mldlen;
1828 struct inet6_dev *idev;
1829 struct net *net = dev_net(skb->dev);
1830 int err;
1831 struct flowi6 fl6;
1832 struct dst_entry *dst;
1834 rcu_read_lock();
1835 idev = __in6_dev_get(skb->dev);
1836 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTREQUESTS);
1838 payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
1839 sizeof(*pip6);
1840 mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
1841 pip6->payload_len = htons(payload_len);
1843 pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1844 IPPROTO_ICMPV6,
1845 csum_partial(skb_transport_header(skb),
1846 mldlen, 0));
1848 icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
1849 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1850 skb->dev->ifindex);
1851 dst = icmp6_dst_alloc(skb->dev, &fl6);
1853 err = 0;
1854 if (IS_ERR(dst)) {
1855 err = PTR_ERR(dst);
1856 dst = NULL;
1858 skb_dst_set(skb, dst);
1859 if (err)
1860 goto err_out;
1862 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
1863 net, net->ipv6.igmp_sk, skb, NULL, skb->dev,
1864 dst_output);
1865 out:
1866 if (!err) {
1867 ICMP6MSGOUT_INC_STATS(net, idev, ICMPV6_MLD2_REPORT);
1868 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
1869 } else {
1870 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
1873 rcu_read_unlock();
1874 return;
1876 err_out:
1877 kfree_skb(skb);
1878 goto out;
1881 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1883 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1886 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1887 int type, struct mld2_grec **ppgr, unsigned int mtu)
1889 struct mld2_report *pmr;
1890 struct mld2_grec *pgr;
1892 if (!skb) {
1893 skb = mld_newpack(pmc->idev, mtu);
1894 if (!skb)
1895 return NULL;
1897 pgr = skb_put(skb, sizeof(struct mld2_grec));
1898 pgr->grec_type = type;
1899 pgr->grec_auxwords = 0;
1900 pgr->grec_nsrcs = 0;
1901 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1902 pmr = (struct mld2_report *)skb_transport_header(skb);
1903 pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
1904 *ppgr = pgr;
1905 return skb;
1908 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
1910 /* called with mc_lock */
1911 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1912 int type, int gdeleted, int sdeleted,
1913 int crsend)
1915 struct ip6_sf_list *psf, *psf_prev, *psf_next;
1916 int scount, stotal, first, isquery, truncate;
1917 struct ip6_sf_list __rcu **psf_list;
1918 struct inet6_dev *idev = pmc->idev;
1919 struct net_device *dev = idev->dev;
1920 struct mld2_grec *pgr = NULL;
1921 struct mld2_report *pmr;
1922 unsigned int mtu;
1924 if (pmc->mca_flags & MAF_NOREPORT)
1925 return skb;
1927 mtu = READ_ONCE(dev->mtu);
1928 if (mtu < IPV6_MIN_MTU)
1929 return skb;
1931 isquery = type == MLD2_MODE_IS_INCLUDE ||
1932 type == MLD2_MODE_IS_EXCLUDE;
1933 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1934 type == MLD2_CHANGE_TO_EXCLUDE;
1936 stotal = scount = 0;
1938 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1940 if (!rcu_access_pointer(*psf_list))
1941 goto empty_source;
1943 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
1945 /* EX and TO_EX get a fresh packet, if needed */
1946 if (truncate) {
1947 if (pmr && pmr->mld2r_ngrec &&
1948 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1949 if (skb)
1950 mld_sendpack(skb);
1951 skb = mld_newpack(idev, mtu);
1954 first = 1;
1955 psf_prev = NULL;
1956 for (psf = mc_dereference(*psf_list, idev);
1957 psf;
1958 psf = psf_next) {
1959 struct in6_addr *psrc;
1961 psf_next = mc_dereference(psf->sf_next, idev);
1963 if (!is_in(pmc, psf, type, gdeleted, sdeleted) && !crsend) {
1964 psf_prev = psf;
1965 continue;
1968 /* Based on RFC3810 6.1. Should not send source-list change
1969 * records when there is a filter mode change.
1971 if (((gdeleted && pmc->mca_sfmode == MCAST_EXCLUDE) ||
1972 (!gdeleted && pmc->mca_crcount)) &&
1973 (type == MLD2_ALLOW_NEW_SOURCES ||
1974 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount)
1975 goto decrease_sf_crcount;
1977 /* clear marks on query responses */
1978 if (isquery)
1979 psf->sf_gsresp = 0;
1981 if (AVAILABLE(skb) < sizeof(*psrc) +
1982 first*sizeof(struct mld2_grec)) {
1983 if (truncate && !first)
1984 break; /* truncate these */
1985 if (pgr)
1986 pgr->grec_nsrcs = htons(scount);
1987 if (skb)
1988 mld_sendpack(skb);
1989 skb = mld_newpack(idev, mtu);
1990 first = 1;
1991 scount = 0;
1993 if (first) {
1994 skb = add_grhead(skb, pmc, type, &pgr, mtu);
1995 first = 0;
1997 if (!skb)
1998 return NULL;
1999 psrc = skb_put(skb, sizeof(*psrc));
2000 *psrc = psf->sf_addr;
2001 scount++; stotal++;
2002 if ((type == MLD2_ALLOW_NEW_SOURCES ||
2003 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
2004 decrease_sf_crcount:
2005 psf->sf_crcount--;
2006 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
2007 if (psf_prev)
2008 rcu_assign_pointer(psf_prev->sf_next,
2009 mc_dereference(psf->sf_next, idev));
2010 else
2011 rcu_assign_pointer(*psf_list,
2012 mc_dereference(psf->sf_next, idev));
2013 kfree_rcu(psf, rcu);
2014 continue;
2017 psf_prev = psf;
2020 empty_source:
2021 if (!stotal) {
2022 if (type == MLD2_ALLOW_NEW_SOURCES ||
2023 type == MLD2_BLOCK_OLD_SOURCES)
2024 return skb;
2025 if (pmc->mca_crcount || isquery || crsend) {
2026 /* make sure we have room for group header */
2027 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
2028 mld_sendpack(skb);
2029 skb = NULL; /* add_grhead will get a new one */
2031 skb = add_grhead(skb, pmc, type, &pgr, mtu);
2034 if (pgr)
2035 pgr->grec_nsrcs = htons(scount);
2037 if (isquery)
2038 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
2039 return skb;
2042 /* called with mc_lock */
2043 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
2045 struct sk_buff *skb = NULL;
2046 int type;
2048 if (!pmc) {
2049 for_each_mc_mclock(idev, pmc) {
2050 if (pmc->mca_flags & MAF_NOREPORT)
2051 continue;
2052 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2053 type = MLD2_MODE_IS_EXCLUDE;
2054 else
2055 type = MLD2_MODE_IS_INCLUDE;
2056 skb = add_grec(skb, pmc, type, 0, 0, 0);
2058 } else {
2059 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2060 type = MLD2_MODE_IS_EXCLUDE;
2061 else
2062 type = MLD2_MODE_IS_INCLUDE;
2063 skb = add_grec(skb, pmc, type, 0, 0, 0);
2065 if (skb)
2066 mld_sendpack(skb);
2070 * remove zero-count source records from a source filter list
2071 * called with mc_lock
2073 static void mld_clear_zeros(struct ip6_sf_list __rcu **ppsf, struct inet6_dev *idev)
2075 struct ip6_sf_list *psf_prev, *psf_next, *psf;
2077 psf_prev = NULL;
2078 for (psf = mc_dereference(*ppsf, idev);
2079 psf;
2080 psf = psf_next) {
2081 psf_next = mc_dereference(psf->sf_next, idev);
2082 if (psf->sf_crcount == 0) {
2083 if (psf_prev)
2084 rcu_assign_pointer(psf_prev->sf_next,
2085 mc_dereference(psf->sf_next, idev));
2086 else
2087 rcu_assign_pointer(*ppsf,
2088 mc_dereference(psf->sf_next, idev));
2089 kfree_rcu(psf, rcu);
2090 } else {
2091 psf_prev = psf;
2096 /* called with mc_lock */
2097 static void mld_send_cr(struct inet6_dev *idev)
2099 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
2100 struct sk_buff *skb = NULL;
2101 int type, dtype;
2103 /* deleted MCA's */
2104 pmc_prev = NULL;
2105 for (pmc = mc_dereference(idev->mc_tomb, idev);
2106 pmc;
2107 pmc = pmc_next) {
2108 pmc_next = mc_dereference(pmc->next, idev);
2109 if (pmc->mca_sfmode == MCAST_INCLUDE) {
2110 type = MLD2_BLOCK_OLD_SOURCES;
2111 dtype = MLD2_BLOCK_OLD_SOURCES;
2112 skb = add_grec(skb, pmc, type, 1, 0, 0);
2113 skb = add_grec(skb, pmc, dtype, 1, 1, 0);
2115 if (pmc->mca_crcount) {
2116 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
2117 type = MLD2_CHANGE_TO_INCLUDE;
2118 skb = add_grec(skb, pmc, type, 1, 0, 0);
2120 pmc->mca_crcount--;
2121 if (pmc->mca_crcount == 0) {
2122 mld_clear_zeros(&pmc->mca_tomb, idev);
2123 mld_clear_zeros(&pmc->mca_sources, idev);
2126 if (pmc->mca_crcount == 0 &&
2127 !rcu_access_pointer(pmc->mca_tomb) &&
2128 !rcu_access_pointer(pmc->mca_sources)) {
2129 if (pmc_prev)
2130 rcu_assign_pointer(pmc_prev->next, pmc_next);
2131 else
2132 rcu_assign_pointer(idev->mc_tomb, pmc_next);
2133 in6_dev_put(pmc->idev);
2134 kfree_rcu(pmc, rcu);
2135 } else
2136 pmc_prev = pmc;
2139 /* change recs */
2140 for_each_mc_mclock(idev, pmc) {
2141 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2142 type = MLD2_BLOCK_OLD_SOURCES;
2143 dtype = MLD2_ALLOW_NEW_SOURCES;
2144 } else {
2145 type = MLD2_ALLOW_NEW_SOURCES;
2146 dtype = MLD2_BLOCK_OLD_SOURCES;
2148 skb = add_grec(skb, pmc, type, 0, 0, 0);
2149 skb = add_grec(skb, pmc, dtype, 0, 1, 0); /* deleted sources */
2151 /* filter mode changes */
2152 if (pmc->mca_crcount) {
2153 if (pmc->mca_sfmode == MCAST_EXCLUDE)
2154 type = MLD2_CHANGE_TO_EXCLUDE;
2155 else
2156 type = MLD2_CHANGE_TO_INCLUDE;
2157 skb = add_grec(skb, pmc, type, 0, 0, 0);
2158 pmc->mca_crcount--;
2161 if (!skb)
2162 return;
2163 (void) mld_sendpack(skb);
2166 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
2168 struct net *net = dev_net(dev);
2169 struct sock *sk = net->ipv6.igmp_sk;
2170 struct inet6_dev *idev;
2171 struct sk_buff *skb;
2172 struct mld_msg *hdr;
2173 const struct in6_addr *snd_addr, *saddr;
2174 struct in6_addr addr_buf;
2175 int hlen = LL_RESERVED_SPACE(dev);
2176 int tlen = dev->needed_tailroom;
2177 int err, len, payload_len, full_len;
2178 u8 ra[8] = { IPPROTO_ICMPV6, 0,
2179 IPV6_TLV_ROUTERALERT, 2, 0, 0,
2180 IPV6_TLV_PADN, 0 };
2181 struct flowi6 fl6;
2182 struct dst_entry *dst;
2184 if (type == ICMPV6_MGM_REDUCTION)
2185 snd_addr = &in6addr_linklocal_allrouters;
2186 else
2187 snd_addr = addr;
2189 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
2190 payload_len = len + sizeof(ra);
2191 full_len = sizeof(struct ipv6hdr) + payload_len;
2193 rcu_read_lock();
2194 IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_OUTREQUESTS);
2195 rcu_read_unlock();
2197 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
2199 if (!skb) {
2200 rcu_read_lock();
2201 IP6_INC_STATS(net, __in6_dev_get(dev),
2202 IPSTATS_MIB_OUTDISCARDS);
2203 rcu_read_unlock();
2204 return;
2206 skb->priority = TC_PRIO_CONTROL;
2207 skb_reserve(skb, hlen);
2209 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
2210 /* <draft-ietf-magma-mld-source-05.txt>:
2211 * use unspecified address as the source address
2212 * when a valid link-local address is not available.
2214 saddr = &in6addr_any;
2215 } else
2216 saddr = &addr_buf;
2218 ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
2220 skb_put_data(skb, ra, sizeof(ra));
2222 hdr = skb_put_zero(skb, sizeof(struct mld_msg));
2223 hdr->mld_type = type;
2224 hdr->mld_mca = *addr;
2226 hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
2227 IPPROTO_ICMPV6,
2228 csum_partial(hdr, len, 0));
2230 rcu_read_lock();
2231 idev = __in6_dev_get(skb->dev);
2233 icmpv6_flow_init(sk, &fl6, type,
2234 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
2235 skb->dev->ifindex);
2236 dst = icmp6_dst_alloc(skb->dev, &fl6);
2237 if (IS_ERR(dst)) {
2238 err = PTR_ERR(dst);
2239 goto err_out;
2242 skb_dst_set(skb, dst);
2243 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
2244 net, sk, skb, NULL, skb->dev,
2245 dst_output);
2246 out:
2247 if (!err) {
2248 ICMP6MSGOUT_INC_STATS(net, idev, type);
2249 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
2250 } else
2251 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
2253 rcu_read_unlock();
2254 return;
2256 err_out:
2257 kfree_skb(skb);
2258 goto out;
2261 /* called with mc_lock */
2262 static void mld_send_initial_cr(struct inet6_dev *idev)
2264 struct sk_buff *skb;
2265 struct ifmcaddr6 *pmc;
2266 int type;
2268 if (mld_in_v1_mode(idev))
2269 return;
2271 skb = NULL;
2272 for_each_mc_mclock(idev, pmc) {
2273 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2274 type = MLD2_CHANGE_TO_EXCLUDE;
2275 else
2276 type = MLD2_ALLOW_NEW_SOURCES;
2277 skb = add_grec(skb, pmc, type, 0, 0, 1);
2279 if (skb)
2280 mld_sendpack(skb);
2283 void ipv6_mc_dad_complete(struct inet6_dev *idev)
2285 mutex_lock(&idev->mc_lock);
2286 idev->mc_dad_count = idev->mc_qrv;
2287 if (idev->mc_dad_count) {
2288 mld_send_initial_cr(idev);
2289 idev->mc_dad_count--;
2290 if (idev->mc_dad_count)
2291 mld_dad_start_work(idev,
2292 unsolicited_report_interval(idev));
2294 mutex_unlock(&idev->mc_lock);
2297 static void mld_dad_work(struct work_struct *work)
2299 struct inet6_dev *idev = container_of(to_delayed_work(work),
2300 struct inet6_dev,
2301 mc_dad_work);
2302 mutex_lock(&idev->mc_lock);
2303 mld_send_initial_cr(idev);
2304 if (idev->mc_dad_count) {
2305 idev->mc_dad_count--;
2306 if (idev->mc_dad_count)
2307 mld_dad_start_work(idev,
2308 unsolicited_report_interval(idev));
2310 mutex_unlock(&idev->mc_lock);
2311 in6_dev_put(idev);
2314 /* called with mc_lock */
2315 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
2316 const struct in6_addr *psfsrc)
2318 struct ip6_sf_list *psf, *psf_prev;
2319 int rv = 0;
2321 psf_prev = NULL;
2322 for_each_psf_mclock(pmc, psf) {
2323 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2324 break;
2325 psf_prev = psf;
2327 if (!psf || psf->sf_count[sfmode] == 0) {
2328 /* source filter not found, or count wrong => bug */
2329 return -ESRCH;
2331 WRITE_ONCE(psf->sf_count[sfmode], psf->sf_count[sfmode] - 1);
2332 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
2333 struct inet6_dev *idev = pmc->idev;
2335 /* no more filters for this source */
2336 if (psf_prev)
2337 rcu_assign_pointer(psf_prev->sf_next,
2338 mc_dereference(psf->sf_next, idev));
2339 else
2340 rcu_assign_pointer(pmc->mca_sources,
2341 mc_dereference(psf->sf_next, idev));
2343 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
2344 !mld_in_v1_mode(idev)) {
2345 psf->sf_crcount = idev->mc_qrv;
2346 rcu_assign_pointer(psf->sf_next,
2347 mc_dereference(pmc->mca_tomb, idev));
2348 rcu_assign_pointer(pmc->mca_tomb, psf);
2349 rv = 1;
2350 } else {
2351 kfree_rcu(psf, rcu);
2354 return rv;
2357 /* called with mc_lock */
2358 static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2359 int sfmode, int sfcount, const struct in6_addr *psfsrc,
2360 int delta)
2362 struct ifmcaddr6 *pmc;
2363 int changerec = 0;
2364 int i, err;
2366 if (!idev)
2367 return -ENODEV;
2369 for_each_mc_mclock(idev, pmc) {
2370 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2371 break;
2373 if (!pmc)
2374 return -ESRCH;
2376 sf_markstate(pmc);
2377 if (!delta) {
2378 if (!pmc->mca_sfcount[sfmode])
2379 return -EINVAL;
2381 pmc->mca_sfcount[sfmode]--;
2383 err = 0;
2384 for (i = 0; i < sfcount; i++) {
2385 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2387 changerec |= rv > 0;
2388 if (!err && rv < 0)
2389 err = rv;
2391 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
2392 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
2393 pmc->mca_sfcount[MCAST_INCLUDE]) {
2394 struct ip6_sf_list *psf;
2396 /* filter mode change */
2397 pmc->mca_sfmode = MCAST_INCLUDE;
2398 pmc->mca_crcount = idev->mc_qrv;
2399 idev->mc_ifc_count = pmc->mca_crcount;
2400 for_each_psf_mclock(pmc, psf)
2401 psf->sf_crcount = 0;
2402 mld_ifc_event(pmc->idev);
2403 } else if (sf_setstate(pmc) || changerec) {
2404 mld_ifc_event(pmc->idev);
2407 return err;
2411 * Add multicast single-source filter to the interface list
2412 * called with mc_lock
2414 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
2415 const struct in6_addr *psfsrc)
2417 struct ip6_sf_list *psf, *psf_prev;
2419 psf_prev = NULL;
2420 for_each_psf_mclock(pmc, psf) {
2421 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2422 break;
2423 psf_prev = psf;
2425 if (!psf) {
2426 psf = kzalloc(sizeof(*psf), GFP_KERNEL);
2427 if (!psf)
2428 return -ENOBUFS;
2430 psf->sf_addr = *psfsrc;
2431 if (psf_prev) {
2432 rcu_assign_pointer(psf_prev->sf_next, psf);
2433 } else {
2434 rcu_assign_pointer(pmc->mca_sources, psf);
2437 WRITE_ONCE(psf->sf_count[sfmode], psf->sf_count[sfmode] + 1);
2438 return 0;
2441 /* called with mc_lock */
2442 static void sf_markstate(struct ifmcaddr6 *pmc)
2444 struct ip6_sf_list *psf;
2445 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2447 for_each_psf_mclock(pmc, psf) {
2448 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2449 psf->sf_oldin = mca_xcount ==
2450 psf->sf_count[MCAST_EXCLUDE] &&
2451 !psf->sf_count[MCAST_INCLUDE];
2452 } else {
2453 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2458 /* called with mc_lock */
2459 static int sf_setstate(struct ifmcaddr6 *pmc)
2461 struct ip6_sf_list *psf, *dpsf;
2462 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2463 int qrv = pmc->idev->mc_qrv;
2464 int new_in, rv;
2466 rv = 0;
2467 for_each_psf_mclock(pmc, psf) {
2468 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2469 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2470 !psf->sf_count[MCAST_INCLUDE];
2471 } else
2472 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2473 if (new_in) {
2474 if (!psf->sf_oldin) {
2475 struct ip6_sf_list *prev = NULL;
2477 for_each_psf_tomb(pmc, dpsf) {
2478 if (ipv6_addr_equal(&dpsf->sf_addr,
2479 &psf->sf_addr))
2480 break;
2481 prev = dpsf;
2483 if (dpsf) {
2484 if (prev)
2485 rcu_assign_pointer(prev->sf_next,
2486 mc_dereference(dpsf->sf_next,
2487 pmc->idev));
2488 else
2489 rcu_assign_pointer(pmc->mca_tomb,
2490 mc_dereference(dpsf->sf_next,
2491 pmc->idev));
2492 kfree_rcu(dpsf, rcu);
2494 psf->sf_crcount = qrv;
2495 rv++;
2497 } else if (psf->sf_oldin) {
2498 psf->sf_crcount = 0;
2500 * add or update "delete" records if an active filter
2501 * is now inactive
2504 for_each_psf_tomb(pmc, dpsf)
2505 if (ipv6_addr_equal(&dpsf->sf_addr,
2506 &psf->sf_addr))
2507 break;
2508 if (!dpsf) {
2509 dpsf = kmalloc(sizeof(*dpsf), GFP_KERNEL);
2510 if (!dpsf)
2511 continue;
2512 *dpsf = *psf;
2513 rcu_assign_pointer(dpsf->sf_next,
2514 mc_dereference(pmc->mca_tomb, pmc->idev));
2515 rcu_assign_pointer(pmc->mca_tomb, dpsf);
2517 dpsf->sf_crcount = qrv;
2518 rv++;
2521 return rv;
2525 * Add multicast source filter list to the interface list
2526 * called with mc_lock
2528 static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2529 int sfmode, int sfcount, const struct in6_addr *psfsrc,
2530 int delta)
2532 struct ifmcaddr6 *pmc;
2533 int isexclude;
2534 int i, err;
2536 if (!idev)
2537 return -ENODEV;
2539 for_each_mc_mclock(idev, pmc) {
2540 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2541 break;
2543 if (!pmc)
2544 return -ESRCH;
2546 sf_markstate(pmc);
2547 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2548 if (!delta)
2549 WRITE_ONCE(pmc->mca_sfcount[sfmode],
2550 pmc->mca_sfcount[sfmode] + 1);
2551 err = 0;
2552 for (i = 0; i < sfcount; i++) {
2553 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2554 if (err)
2555 break;
2557 if (err) {
2558 int j;
2560 if (!delta)
2561 WRITE_ONCE(pmc->mca_sfcount[sfmode],
2562 pmc->mca_sfcount[sfmode] - 1);
2563 for (j = 0; j < i; j++)
2564 ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2565 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2566 struct ip6_sf_list *psf;
2568 /* filter mode change */
2569 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2570 pmc->mca_sfmode = MCAST_EXCLUDE;
2571 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2572 pmc->mca_sfmode = MCAST_INCLUDE;
2573 /* else no filters; keep old mode for reports */
2575 pmc->mca_crcount = idev->mc_qrv;
2576 idev->mc_ifc_count = pmc->mca_crcount;
2577 for_each_psf_mclock(pmc, psf)
2578 psf->sf_crcount = 0;
2579 mld_ifc_event(idev);
2580 } else if (sf_setstate(pmc)) {
2581 mld_ifc_event(idev);
2583 return err;
2586 /* called with mc_lock */
2587 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2589 struct ip6_sf_list *psf, *nextpsf;
2591 for (psf = mc_dereference(pmc->mca_tomb, pmc->idev);
2592 psf;
2593 psf = nextpsf) {
2594 nextpsf = mc_dereference(psf->sf_next, pmc->idev);
2595 kfree_rcu(psf, rcu);
2597 RCU_INIT_POINTER(pmc->mca_tomb, NULL);
2598 for (psf = mc_dereference(pmc->mca_sources, pmc->idev);
2599 psf;
2600 psf = nextpsf) {
2601 nextpsf = mc_dereference(psf->sf_next, pmc->idev);
2602 kfree_rcu(psf, rcu);
2604 RCU_INIT_POINTER(pmc->mca_sources, NULL);
2605 pmc->mca_sfmode = MCAST_EXCLUDE;
2606 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2607 /* Paired with the READ_ONCE() from ipv6_chk_mcast_addr() */
2608 WRITE_ONCE(pmc->mca_sfcount[MCAST_EXCLUDE], 1);
2611 /* called with mc_lock */
2612 static void igmp6_join_group(struct ifmcaddr6 *ma)
2614 unsigned long delay;
2616 if (ma->mca_flags & MAF_NOREPORT)
2617 return;
2619 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2621 delay = get_random_u32_below(unsolicited_report_interval(ma->idev));
2623 if (cancel_delayed_work(&ma->mca_work)) {
2624 refcount_dec(&ma->mca_refcnt);
2625 delay = ma->mca_work.timer.expires - jiffies;
2628 if (!mod_delayed_work(mld_wq, &ma->mca_work, delay))
2629 refcount_inc(&ma->mca_refcnt);
2630 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2633 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2634 struct inet6_dev *idev)
2636 struct ip6_sf_socklist *psl;
2637 int err;
2639 psl = sock_dereference(iml->sflist, sk);
2641 if (idev)
2642 mutex_lock(&idev->mc_lock);
2644 if (!psl) {
2645 /* any-source empty exclude case */
2646 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2647 } else {
2648 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2649 psl->sl_count, psl->sl_addr, 0);
2650 RCU_INIT_POINTER(iml->sflist, NULL);
2651 atomic_sub(struct_size(psl, sl_addr, psl->sl_max),
2652 &sk->sk_omem_alloc);
2653 kfree_rcu(psl, rcu);
2656 if (idev)
2657 mutex_unlock(&idev->mc_lock);
2659 return err;
2662 /* called with mc_lock */
2663 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2665 if (mld_in_v1_mode(ma->idev)) {
2666 if (ma->mca_flags & MAF_LAST_REPORTER) {
2667 igmp6_send(&ma->mca_addr, ma->idev->dev,
2668 ICMPV6_MGM_REDUCTION);
2670 } else {
2671 mld_add_delrec(ma->idev, ma);
2672 mld_ifc_event(ma->idev);
2676 static void mld_gq_work(struct work_struct *work)
2678 struct inet6_dev *idev = container_of(to_delayed_work(work),
2679 struct inet6_dev,
2680 mc_gq_work);
2682 mutex_lock(&idev->mc_lock);
2683 mld_send_report(idev, NULL);
2684 idev->mc_gq_running = 0;
2685 mutex_unlock(&idev->mc_lock);
2687 in6_dev_put(idev);
2690 static void mld_ifc_work(struct work_struct *work)
2692 struct inet6_dev *idev = container_of(to_delayed_work(work),
2693 struct inet6_dev,
2694 mc_ifc_work);
2696 mutex_lock(&idev->mc_lock);
2697 mld_send_cr(idev);
2699 if (idev->mc_ifc_count) {
2700 idev->mc_ifc_count--;
2701 if (idev->mc_ifc_count)
2702 mld_ifc_start_work(idev,
2703 unsolicited_report_interval(idev));
2705 mutex_unlock(&idev->mc_lock);
2706 in6_dev_put(idev);
2709 /* called with mc_lock */
2710 static void mld_ifc_event(struct inet6_dev *idev)
2712 if (mld_in_v1_mode(idev))
2713 return;
2715 idev->mc_ifc_count = idev->mc_qrv;
2716 mld_ifc_start_work(idev, 1);
2719 static void mld_mca_work(struct work_struct *work)
2721 struct ifmcaddr6 *ma = container_of(to_delayed_work(work),
2722 struct ifmcaddr6, mca_work);
2724 mutex_lock(&ma->idev->mc_lock);
2725 if (mld_in_v1_mode(ma->idev))
2726 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2727 else
2728 mld_send_report(ma->idev, ma);
2729 ma->mca_flags |= MAF_LAST_REPORTER;
2730 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2731 mutex_unlock(&ma->idev->mc_lock);
2733 ma_put(ma);
2736 /* Device changing type */
2738 void ipv6_mc_unmap(struct inet6_dev *idev)
2740 struct ifmcaddr6 *i;
2742 /* Install multicast list, except for all-nodes (already installed) */
2744 mutex_lock(&idev->mc_lock);
2745 for_each_mc_mclock(idev, i)
2746 igmp6_group_dropped(i);
2747 mutex_unlock(&idev->mc_lock);
2750 void ipv6_mc_remap(struct inet6_dev *idev)
2752 ipv6_mc_up(idev);
2755 /* Device going down */
2756 void ipv6_mc_down(struct inet6_dev *idev)
2758 struct ifmcaddr6 *i;
2760 mutex_lock(&idev->mc_lock);
2761 /* Withdraw multicast list */
2762 for_each_mc_mclock(idev, i)
2763 igmp6_group_dropped(i);
2764 mutex_unlock(&idev->mc_lock);
2766 /* Should stop work after group drop. or we will
2767 * start work again in mld_ifc_event()
2769 mld_query_stop_work(idev);
2770 mld_report_stop_work(idev);
2772 mutex_lock(&idev->mc_lock);
2773 mld_ifc_stop_work(idev);
2774 mld_gq_stop_work(idev);
2775 mutex_unlock(&idev->mc_lock);
2777 mld_dad_stop_work(idev);
2780 static void ipv6_mc_reset(struct inet6_dev *idev)
2782 idev->mc_qrv = sysctl_mld_qrv;
2783 idev->mc_qi = MLD_QI_DEFAULT;
2784 idev->mc_qri = MLD_QRI_DEFAULT;
2785 idev->mc_v1_seen = 0;
2786 idev->mc_maxdelay = unsolicited_report_interval(idev);
2789 /* Device going up */
2791 void ipv6_mc_up(struct inet6_dev *idev)
2793 struct ifmcaddr6 *i;
2795 /* Install multicast list, except for all-nodes (already installed) */
2797 ipv6_mc_reset(idev);
2798 mutex_lock(&idev->mc_lock);
2799 for_each_mc_mclock(idev, i) {
2800 mld_del_delrec(idev, i);
2801 igmp6_group_added(i);
2803 mutex_unlock(&idev->mc_lock);
2806 /* IPv6 device initialization. */
2808 void ipv6_mc_init_dev(struct inet6_dev *idev)
2810 idev->mc_gq_running = 0;
2811 INIT_DELAYED_WORK(&idev->mc_gq_work, mld_gq_work);
2812 RCU_INIT_POINTER(idev->mc_tomb, NULL);
2813 idev->mc_ifc_count = 0;
2814 INIT_DELAYED_WORK(&idev->mc_ifc_work, mld_ifc_work);
2815 INIT_DELAYED_WORK(&idev->mc_dad_work, mld_dad_work);
2816 INIT_DELAYED_WORK(&idev->mc_query_work, mld_query_work);
2817 INIT_DELAYED_WORK(&idev->mc_report_work, mld_report_work);
2818 skb_queue_head_init(&idev->mc_query_queue);
2819 skb_queue_head_init(&idev->mc_report_queue);
2820 spin_lock_init(&idev->mc_query_lock);
2821 spin_lock_init(&idev->mc_report_lock);
2822 mutex_init(&idev->mc_lock);
2823 ipv6_mc_reset(idev);
2827 * Device is about to be destroyed: clean up.
2830 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2832 struct ifmcaddr6 *i;
2834 /* Deactivate works */
2835 ipv6_mc_down(idev);
2836 mutex_lock(&idev->mc_lock);
2837 mld_clear_delrec(idev);
2838 mutex_unlock(&idev->mc_lock);
2839 mld_clear_query(idev);
2840 mld_clear_report(idev);
2842 /* Delete all-nodes address. */
2843 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2844 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2845 * fail.
2847 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
2849 if (idev->cnf.forwarding)
2850 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
2852 mutex_lock(&idev->mc_lock);
2853 while ((i = mc_dereference(idev->mc_list, idev))) {
2854 rcu_assign_pointer(idev->mc_list, mc_dereference(i->next, idev));
2856 ip6_mc_clear_src(i);
2857 ma_put(i);
2859 mutex_unlock(&idev->mc_lock);
2862 static void ipv6_mc_rejoin_groups(struct inet6_dev *idev)
2864 struct ifmcaddr6 *pmc;
2866 ASSERT_RTNL();
2868 mutex_lock(&idev->mc_lock);
2869 if (mld_in_v1_mode(idev)) {
2870 for_each_mc_mclock(idev, pmc)
2871 igmp6_join_group(pmc);
2872 } else {
2873 mld_send_report(idev, NULL);
2875 mutex_unlock(&idev->mc_lock);
2878 static int ipv6_mc_netdev_event(struct notifier_block *this,
2879 unsigned long event,
2880 void *ptr)
2882 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2883 struct inet6_dev *idev = __in6_dev_get(dev);
2885 switch (event) {
2886 case NETDEV_RESEND_IGMP:
2887 if (idev)
2888 ipv6_mc_rejoin_groups(idev);
2889 break;
2890 default:
2891 break;
2894 return NOTIFY_DONE;
2897 static struct notifier_block igmp6_netdev_notifier = {
2898 .notifier_call = ipv6_mc_netdev_event,
2901 #ifdef CONFIG_PROC_FS
2902 struct igmp6_mc_iter_state {
2903 struct seq_net_private p;
2904 struct net_device *dev;
2905 struct inet6_dev *idev;
2908 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2910 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2912 struct ifmcaddr6 *im = NULL;
2913 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2914 struct net *net = seq_file_net(seq);
2916 state->idev = NULL;
2917 for_each_netdev_rcu(net, state->dev) {
2918 struct inet6_dev *idev;
2919 idev = __in6_dev_get(state->dev);
2920 if (!idev)
2921 continue;
2923 im = rcu_dereference(idev->mc_list);
2924 if (im) {
2925 state->idev = idev;
2926 break;
2929 return im;
2932 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2934 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2936 im = rcu_dereference(im->next);
2937 while (!im) {
2938 state->dev = next_net_device_rcu(state->dev);
2939 if (!state->dev) {
2940 state->idev = NULL;
2941 break;
2943 state->idev = __in6_dev_get(state->dev);
2944 if (!state->idev)
2945 continue;
2946 im = rcu_dereference(state->idev->mc_list);
2948 return im;
2951 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2953 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2954 if (im)
2955 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2956 --pos;
2957 return pos ? NULL : im;
2960 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2961 __acquires(RCU)
2963 rcu_read_lock();
2964 return igmp6_mc_get_idx(seq, *pos);
2967 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2969 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2971 ++*pos;
2972 return im;
2975 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2976 __releases(RCU)
2978 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2980 if (likely(state->idev))
2981 state->idev = NULL;
2982 state->dev = NULL;
2983 rcu_read_unlock();
2986 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2988 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2989 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2991 seq_printf(seq,
2992 "%-4d %-15s %pi6 %5d %08X %ld\n",
2993 state->dev->ifindex, state->dev->name,
2994 &im->mca_addr,
2995 im->mca_users, im->mca_flags,
2996 (im->mca_flags & MAF_TIMER_RUNNING) ?
2997 jiffies_to_clock_t(im->mca_work.timer.expires - jiffies) : 0);
2998 return 0;
3001 static const struct seq_operations igmp6_mc_seq_ops = {
3002 .start = igmp6_mc_seq_start,
3003 .next = igmp6_mc_seq_next,
3004 .stop = igmp6_mc_seq_stop,
3005 .show = igmp6_mc_seq_show,
3008 struct igmp6_mcf_iter_state {
3009 struct seq_net_private p;
3010 struct net_device *dev;
3011 struct inet6_dev *idev;
3012 struct ifmcaddr6 *im;
3015 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
3017 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
3019 struct ip6_sf_list *psf = NULL;
3020 struct ifmcaddr6 *im = NULL;
3021 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
3022 struct net *net = seq_file_net(seq);
3024 state->idev = NULL;
3025 state->im = NULL;
3026 for_each_netdev_rcu(net, state->dev) {
3027 struct inet6_dev *idev;
3028 idev = __in6_dev_get(state->dev);
3029 if (unlikely(idev == NULL))
3030 continue;
3032 im = rcu_dereference(idev->mc_list);
3033 if (likely(im)) {
3034 psf = rcu_dereference(im->mca_sources);
3035 if (likely(psf)) {
3036 state->im = im;
3037 state->idev = idev;
3038 break;
3042 return psf;
3045 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
3047 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
3049 psf = rcu_dereference(psf->sf_next);
3050 while (!psf) {
3051 state->im = rcu_dereference(state->im->next);
3052 while (!state->im) {
3053 state->dev = next_net_device_rcu(state->dev);
3054 if (!state->dev) {
3055 state->idev = NULL;
3056 goto out;
3058 state->idev = __in6_dev_get(state->dev);
3059 if (!state->idev)
3060 continue;
3061 state->im = rcu_dereference(state->idev->mc_list);
3063 psf = rcu_dereference(state->im->mca_sources);
3065 out:
3066 return psf;
3069 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
3071 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
3072 if (psf)
3073 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
3074 --pos;
3075 return pos ? NULL : psf;
3078 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
3079 __acquires(RCU)
3081 rcu_read_lock();
3082 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
3085 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3087 struct ip6_sf_list *psf;
3088 if (v == SEQ_START_TOKEN)
3089 psf = igmp6_mcf_get_first(seq);
3090 else
3091 psf = igmp6_mcf_get_next(seq, v);
3092 ++*pos;
3093 return psf;
3096 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
3097 __releases(RCU)
3099 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
3101 if (likely(state->im))
3102 state->im = NULL;
3103 if (likely(state->idev))
3104 state->idev = NULL;
3106 state->dev = NULL;
3107 rcu_read_unlock();
3110 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
3112 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
3113 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
3115 if (v == SEQ_START_TOKEN) {
3116 seq_puts(seq, "Idx Device Multicast Address Source Address INC EXC\n");
3117 } else {
3118 seq_printf(seq,
3119 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
3120 state->dev->ifindex, state->dev->name,
3121 &state->im->mca_addr,
3122 &psf->sf_addr,
3123 READ_ONCE(psf->sf_count[MCAST_INCLUDE]),
3124 READ_ONCE(psf->sf_count[MCAST_EXCLUDE]));
3126 return 0;
3129 static const struct seq_operations igmp6_mcf_seq_ops = {
3130 .start = igmp6_mcf_seq_start,
3131 .next = igmp6_mcf_seq_next,
3132 .stop = igmp6_mcf_seq_stop,
3133 .show = igmp6_mcf_seq_show,
3136 static int __net_init igmp6_proc_init(struct net *net)
3138 int err;
3140 err = -ENOMEM;
3141 if (!proc_create_net("igmp6", 0444, net->proc_net, &igmp6_mc_seq_ops,
3142 sizeof(struct igmp6_mc_iter_state)))
3143 goto out;
3144 if (!proc_create_net("mcfilter6", 0444, net->proc_net,
3145 &igmp6_mcf_seq_ops,
3146 sizeof(struct igmp6_mcf_iter_state)))
3147 goto out_proc_net_igmp6;
3149 err = 0;
3150 out:
3151 return err;
3153 out_proc_net_igmp6:
3154 remove_proc_entry("igmp6", net->proc_net);
3155 goto out;
3158 static void __net_exit igmp6_proc_exit(struct net *net)
3160 remove_proc_entry("mcfilter6", net->proc_net);
3161 remove_proc_entry("igmp6", net->proc_net);
3163 #else
3164 static inline int igmp6_proc_init(struct net *net)
3166 return 0;
3168 static inline void igmp6_proc_exit(struct net *net)
3171 #endif
3173 static int __net_init igmp6_net_init(struct net *net)
3175 int err;
3177 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
3178 SOCK_RAW, IPPROTO_ICMPV6, net);
3179 if (err < 0) {
3180 pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
3181 err);
3182 goto out;
3185 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
3186 net->ipv6.igmp_sk->sk_allocation = GFP_KERNEL;
3188 err = inet_ctl_sock_create(&net->ipv6.mc_autojoin_sk, PF_INET6,
3189 SOCK_RAW, IPPROTO_ICMPV6, net);
3190 if (err < 0) {
3191 pr_err("Failed to initialize the IGMP6 autojoin socket (err %d)\n",
3192 err);
3193 goto out_sock_create;
3196 err = igmp6_proc_init(net);
3197 if (err)
3198 goto out_sock_create_autojoin;
3200 return 0;
3202 out_sock_create_autojoin:
3203 inet_ctl_sock_destroy(net->ipv6.mc_autojoin_sk);
3204 out_sock_create:
3205 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
3206 out:
3207 return err;
3210 static void __net_exit igmp6_net_exit(struct net *net)
3212 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
3213 inet_ctl_sock_destroy(net->ipv6.mc_autojoin_sk);
3214 igmp6_proc_exit(net);
3217 static struct pernet_operations igmp6_net_ops = {
3218 .init = igmp6_net_init,
3219 .exit = igmp6_net_exit,
3222 int __init igmp6_init(void)
3224 int err;
3226 err = register_pernet_subsys(&igmp6_net_ops);
3227 if (err)
3228 return err;
3230 mld_wq = create_workqueue("mld");
3231 if (!mld_wq) {
3232 unregister_pernet_subsys(&igmp6_net_ops);
3233 return -ENOMEM;
3236 return err;
3239 int __init igmp6_late_init(void)
3241 return register_netdevice_notifier(&igmp6_netdev_notifier);
3244 void igmp6_cleanup(void)
3246 unregister_pernet_subsys(&igmp6_net_ops);
3247 destroy_workqueue(mld_wq);
3250 void igmp6_late_cleanup(void)
3252 unregister_netdevice_notifier(&igmp6_netdev_notifier);