1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Multicast support for IPv6
4 * Linux INET6 implementation
7 * Pedro Roque <roque@di.fc.ul.pt>
9 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
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>:
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>
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>
44 #include <linux/workqueue.h>
46 #include <linux/netfilter.h>
47 #include <linux/netfilter_ipv6.h>
49 #include <net/net_namespace.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>
61 #include <net/ip6_checksum.h>
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)
70 static struct workqueue_struct *mld_wq;
71 static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
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);
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,
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,
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
115 #define for_each_pmc_rtnl(np, pmc) \
116 for (pmc = rtnl_dereference((np)->ipv6_mc_list); \
118 pmc = rtnl_dereference(pmc->next))
120 #define for_each_pmc_rcu(np, pmc) \
121 for (pmc = rcu_dereference((np)->ipv6_mc_list); \
123 pmc = rcu_dereference(pmc->next))
125 #define for_each_psf_rtnl(mc, psf) \
126 for (psf = rtnl_dereference((mc)->mca_sources); \
128 psf = rtnl_dereference(psf->sf_next))
130 #define for_each_psf_rcu(mc, psf) \
131 for (psf = rcu_dereference((mc)->mca_sources); \
133 psf = rcu_dereference(psf->sf_next))
135 #define for_each_psf_tomb(mc, psf) \
136 for (psf = rtnl_dereference((mc)->mca_tomb); \
138 psf = rtnl_dereference(psf->sf_next))
140 #define for_each_mc_rtnl(idev, mc) \
141 for (mc = rtnl_dereference((idev)->mc_list); \
143 mc = rtnl_dereference(mc->next))
145 #define for_each_mc_rcu(idev, mc) \
146 for (mc = rcu_dereference((idev)->mc_list); \
148 mc = rcu_dereference(mc->next))
150 #define for_each_mc_tomb(idev, mc) \
151 for (mc = rtnl_dereference((idev)->mc_tomb); \
153 mc = rtnl_dereference(mc->next))
155 static int unsolicited_report_interval(struct inet6_dev *idev)
159 if (mld_in_v1_mode(idev))
160 iv = idev->cnf.mldv1_unsolicited_report_interval;
162 iv = idev->cnf.mldv2_unsolicited_report_interval;
164 return iv > 0 ? iv : 1;
167 static int __ipv6_sock_mc_join(struct sock *sk, int ifindex,
168 const struct in6_addr *addr, unsigned int mode)
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);
178 if (!ipv6_addr_is_multicast(addr))
181 for_each_pmc_rtnl(np, mc_lst) {
182 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
183 ipv6_addr_equal(&mc_lst->addr, addr))
187 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
193 mc_lst->addr = *addr;
197 rt = rt6_lookup(net, addr, NULL, 0, NULL, 0);
203 dev = __dev_get_by_index(net, ifindex);
206 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
210 mc_lst->ifindex = dev->ifindex;
211 mc_lst->sfmode = mode;
212 RCU_INIT_POINTER(mc_lst->sflist, NULL);
215 * now add/increase the group membership on the device
218 err = __ipv6_dev_mc_inc(dev, addr, mode);
221 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
225 mc_lst->next = np->ipv6_mc_list;
226 rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
231 int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
233 return __ipv6_sock_mc_join(sk, ifindex, addr, MCAST_EXCLUDE);
235 EXPORT_SYMBOL(ipv6_sock_mc_join);
237 int ipv6_sock_mc_join_ssm(struct sock *sk, int ifindex,
238 const struct in6_addr *addr, unsigned int mode)
240 return __ipv6_sock_mc_join(sk, ifindex, addr, mode);
244 * socket leave on multicast group
246 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
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);
255 if (!ipv6_addr_is_multicast(addr))
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;
267 dev = __dev_get_by_index(net, mc_lst->ifindex);
269 struct inet6_dev *idev = __in6_dev_get(dev);
271 (void) ip6_mc_leave_src(sk, mc_lst, idev);
273 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
275 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
277 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
278 kfree_rcu(mc_lst, rcu);
283 return -EADDRNOTAVAIL;
285 EXPORT_SYMBOL(ipv6_sock_mc_drop);
287 static struct inet6_dev *ip6_mc_find_dev_rtnl(struct net *net,
288 const struct in6_addr *group,
291 struct net_device *dev = NULL;
292 struct inet6_dev *idev = NULL;
295 struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, NULL, 0);
302 dev = __dev_get_by_index(net, ifindex);
307 idev = __in6_dev_get(dev);
315 void __ipv6_sock_mc_close(struct sock *sk)
317 struct ipv6_pinfo *np = inet6_sk(sk);
318 struct ipv6_mc_socklist *mc_lst;
319 struct net *net = sock_net(sk);
323 while ((mc_lst = rtnl_dereference(np->ipv6_mc_list)) != NULL) {
324 struct net_device *dev;
326 np->ipv6_mc_list = mc_lst->next;
328 dev = __dev_get_by_index(net, mc_lst->ifindex);
330 struct inet6_dev *idev = __in6_dev_get(dev);
332 (void) ip6_mc_leave_src(sk, mc_lst, idev);
334 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
336 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
338 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
339 kfree_rcu(mc_lst, rcu);
343 void ipv6_sock_mc_close(struct sock *sk)
345 struct ipv6_pinfo *np = inet6_sk(sk);
347 if (!rcu_access_pointer(np->ipv6_mc_list))
350 __ipv6_sock_mc_close(sk);
354 int ip6_mc_source(int add, int omode, struct sock *sk,
355 struct group_source_req *pgsr)
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);
367 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
368 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
370 if (!ipv6_addr_is_multicast(group))
373 idev = ip6_mc_find_dev_rtnl(net, group, pgsr->gsr_interface);
377 err = -EADDRNOTAVAIL;
379 for_each_pmc_rtnl(inet6, pmc) {
380 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
382 if (ipv6_addr_equal(&pmc->addr, group))
385 if (!pmc) { /* must have a prior join */
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) {
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);
402 psl = rtnl_dereference(pmc->sflist);
405 goto done; /* err = -EADDRNOTAVAIL */
407 for (i = 0; i < psl->sl_count; i++) {
408 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
412 if (rv) /* source not found */
413 goto done; /* err = -EADDRNOTAVAIL */
415 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
416 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
421 /* update the interface filter */
422 ip6_mc_del_src(idev, group, omode, 1, source, 1);
424 for (j = i+1; j < psl->sl_count; j++)
425 psl->sl_addr[j-1] = psl->sl_addr[j];
430 /* else, add a new source to the filter */
432 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
436 if (!psl || psl->sl_count == psl->sl_max) {
437 struct ip6_sf_socklist *newpsl;
438 int count = IP6_SFBLOCK;
441 count += psl->sl_max;
442 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_KERNEL);
447 newpsl->sl_max = count;
448 newpsl->sl_count = count - IP6_SFBLOCK;
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);
456 rcu_assign_pointer(pmc->sflist, psl);
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. */
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;
469 /* update the interface list */
470 ip6_mc_add_src(idev, group, omode, 1, source, 1);
473 err = ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
477 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf,
478 struct sockaddr_storage *list)
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);
489 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
491 if (!ipv6_addr_is_multicast(group))
493 if (gsf->gf_fmode != MCAST_INCLUDE &&
494 gsf->gf_fmode != MCAST_EXCLUDE)
497 idev = ip6_mc_find_dev_rtnl(net, group, gsf->gf_interface);
503 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
508 for_each_pmc_rtnl(inet6, pmc) {
509 if (pmc->ifindex != gsf->gf_interface)
511 if (ipv6_addr_equal(&pmc->addr, group))
514 if (!pmc) { /* must have a prior join */
518 if (gsf->gf_numsrc) {
519 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
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;
529 psin6 = (struct sockaddr_in6 *)list;
530 newpsl->sl_addr[i] = psin6->sin6_addr;
532 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
533 newpsl->sl_count, newpsl->sl_addr, 0);
535 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
540 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
543 psl = rtnl_dereference(pmc->sflist);
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);
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;
556 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
560 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
561 struct sockaddr_storage __user *p)
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);
571 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
573 if (!ipv6_addr_is_multicast(group))
576 idev = ip6_mc_find_dev_rtnl(net, group, gsf->gf_interface);
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.
586 for_each_pmc_rtnl(inet6, pmc) {
587 if (pmc->ifindex != gsf->gf_interface)
589 if (ipv6_addr_equal(group, &pmc->addr))
592 if (!pmc) /* must have a prior join */
595 gsf->gf_fmode = pmc->sfmode;
596 psl = rtnl_dereference(pmc->sflist);
597 count = psl ? psl->sl_count : 0;
599 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
600 gsf->gf_numsrc = count;
602 for (i = 0; i < copycount; i++, p++) {
603 struct sockaddr_in6 *psin6;
604 struct sockaddr_storage ss;
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)))
616 bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
617 const struct in6_addr *src_addr)
619 struct ipv6_pinfo *np = inet6_sk(sk);
620 struct ipv6_mc_socklist *mc;
621 struct ip6_sf_socklist *psl;
625 for_each_pmc_rcu(np, mc) {
626 if (ipv6_addr_equal(&mc->addr, mc_addr))
633 psl = rcu_dereference(mc->sflist);
635 rv = mc->sfmode == MCAST_EXCLUDE;
639 for (i = 0; i < psl->sl_count; i++) {
640 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
643 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
645 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
653 static void igmp6_group_added(struct ifmcaddr6 *mc)
655 struct net_device *dev = mc->idev->dev;
656 char buf[MAX_ADDR_LEN];
658 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
659 IPV6_ADDR_SCOPE_LINKLOCAL)
662 if (!(mc->mca_flags&MAF_LOADED)) {
663 mc->mca_flags |= MAF_LOADED;
664 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
665 dev_mc_add(dev, buf);
668 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
671 if (mld_in_v1_mode(mc->idev)) {
672 igmp6_join_group(mc);
677 /* Based on RFC3810 6.1, for newly added INCLUDE SSM, we
678 * should not send filter-mode change record as the mode
679 * should be from IN() to IN(A).
681 if (mc->mca_sfmode == MCAST_EXCLUDE)
682 mc->mca_crcount = mc->idev->mc_qrv;
684 mld_ifc_event(mc->idev);
687 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
689 struct net_device *dev = mc->idev->dev;
690 char buf[MAX_ADDR_LEN];
692 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
693 IPV6_ADDR_SCOPE_LINKLOCAL)
696 if (mc->mca_flags&MAF_LOADED) {
697 mc->mca_flags &= ~MAF_LOADED;
698 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
699 dev_mc_del(dev, buf);
702 if (mc->mca_flags & MAF_NOREPORT)
706 igmp6_leave_group(mc);
708 if (cancel_delayed_work(&mc->mca_work))
709 refcount_dec(&mc->mca_refcnt);
713 * deleted ifmcaddr6 manipulation
715 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
717 struct ifmcaddr6 *pmc;
719 /* this is an "ifmcaddr6" for convenience; only the fields below
720 * are actually used. In particular, the refcnt and users are not
721 * used for management of the delete list. Using the same structure
722 * for deleted items allows change reports to use common code with
723 * non-deleted or query-response MCA's.
725 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
729 pmc->idev = im->idev;
731 pmc->mca_addr = im->mca_addr;
732 pmc->mca_crcount = idev->mc_qrv;
733 pmc->mca_sfmode = im->mca_sfmode;
734 if (pmc->mca_sfmode == MCAST_INCLUDE) {
735 struct ip6_sf_list *psf;
737 rcu_assign_pointer(pmc->mca_tomb,
738 rtnl_dereference(im->mca_tomb));
739 rcu_assign_pointer(pmc->mca_sources,
740 rtnl_dereference(im->mca_sources));
741 RCU_INIT_POINTER(im->mca_tomb, NULL);
742 RCU_INIT_POINTER(im->mca_sources, NULL);
744 for_each_psf_rtnl(pmc, psf)
745 psf->sf_crcount = pmc->mca_crcount;
748 rcu_assign_pointer(pmc->next, idev->mc_tomb);
749 rcu_assign_pointer(idev->mc_tomb, pmc);
752 static void mld_del_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
754 struct ip6_sf_list *psf, *sources, *tomb;
755 struct in6_addr *pmca = &im->mca_addr;
756 struct ifmcaddr6 *pmc, *pmc_prev;
759 for_each_mc_tomb(idev, pmc) {
760 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
766 rcu_assign_pointer(pmc_prev->next, pmc->next);
768 rcu_assign_pointer(idev->mc_tomb, pmc->next);
772 im->idev = pmc->idev;
773 if (im->mca_sfmode == MCAST_INCLUDE) {
774 tomb = rcu_replace_pointer(im->mca_tomb,
775 rtnl_dereference(pmc->mca_tomb),
776 lockdep_rtnl_is_held());
777 rcu_assign_pointer(pmc->mca_tomb, tomb);
779 sources = rcu_replace_pointer(im->mca_sources,
780 rtnl_dereference(pmc->mca_sources),
781 lockdep_rtnl_is_held());
782 rcu_assign_pointer(pmc->mca_sources, sources);
783 for_each_psf_rtnl(im, psf)
784 psf->sf_crcount = idev->mc_qrv;
786 im->mca_crcount = idev->mc_qrv;
788 in6_dev_put(pmc->idev);
789 ip6_mc_clear_src(pmc);
794 static void mld_clear_delrec(struct inet6_dev *idev)
796 struct ifmcaddr6 *pmc, *nextpmc;
798 pmc = rtnl_dereference(idev->mc_tomb);
799 RCU_INIT_POINTER(idev->mc_tomb, NULL);
801 for (; pmc; pmc = nextpmc) {
802 nextpmc = rtnl_dereference(pmc->next);
803 ip6_mc_clear_src(pmc);
804 in6_dev_put(pmc->idev);
808 /* clear dead sources, too */
809 for_each_mc_rtnl(idev, pmc) {
810 struct ip6_sf_list *psf, *psf_next;
812 psf = rtnl_dereference(pmc->mca_tomb);
813 RCU_INIT_POINTER(pmc->mca_tomb, NULL);
814 for (; psf; psf = psf_next) {
815 psf_next = rtnl_dereference(psf->sf_next);
821 static void mld_clear_query(struct inet6_dev *idev)
825 spin_lock_bh(&idev->mc_query_lock);
826 while ((skb = __skb_dequeue(&idev->mc_query_queue)))
828 spin_unlock_bh(&idev->mc_query_lock);
831 static void mld_clear_report(struct inet6_dev *idev)
835 spin_lock_bh(&idev->mc_report_lock);
836 while ((skb = __skb_dequeue(&idev->mc_report_queue)))
838 spin_unlock_bh(&idev->mc_report_lock);
841 static void mca_get(struct ifmcaddr6 *mc)
843 refcount_inc(&mc->mca_refcnt);
846 static void ma_put(struct ifmcaddr6 *mc)
848 if (refcount_dec_and_test(&mc->mca_refcnt)) {
849 in6_dev_put(mc->idev);
854 static struct ifmcaddr6 *mca_alloc(struct inet6_dev *idev,
855 const struct in6_addr *addr,
858 struct ifmcaddr6 *mc;
860 mc = kzalloc(sizeof(*mc), GFP_KERNEL);
864 INIT_DELAYED_WORK(&mc->mca_work, mld_mca_work);
866 mc->mca_addr = *addr;
867 mc->idev = idev; /* reference taken by caller */
869 /* mca_stamp should be updated upon changes */
870 mc->mca_cstamp = mc->mca_tstamp = jiffies;
871 refcount_set(&mc->mca_refcnt, 1);
873 mc->mca_sfmode = mode;
874 mc->mca_sfcount[mode] = 1;
876 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
877 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
878 mc->mca_flags |= MAF_NOREPORT;
884 * device multicast group inc (add if not found)
886 static int __ipv6_dev_mc_inc(struct net_device *dev,
887 const struct in6_addr *addr, unsigned int mode)
889 struct ifmcaddr6 *mc;
890 struct inet6_dev *idev;
894 /* we need to take a reference on idev */
895 idev = in6_dev_get(dev);
905 for_each_mc_rtnl(idev, mc) {
906 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
908 ip6_mc_add_src(idev, &mc->mca_addr, mode, 0, NULL, 0);
914 mc = mca_alloc(idev, addr, mode);
920 rcu_assign_pointer(mc->next, idev->mc_list);
921 rcu_assign_pointer(idev->mc_list, mc);
925 mld_del_delrec(idev, mc);
926 igmp6_group_added(mc);
931 int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
933 return __ipv6_dev_mc_inc(dev, addr, MCAST_EXCLUDE);
935 EXPORT_SYMBOL(ipv6_dev_mc_inc);
938 * device multicast group del
940 int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
942 struct ifmcaddr6 *ma, __rcu **map;
946 for (map = &idev->mc_list;
947 (ma = rtnl_dereference(*map));
949 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
950 if (--ma->mca_users == 0) {
953 igmp6_group_dropped(ma);
954 ip6_mc_clear_src(ma);
966 int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
968 struct inet6_dev *idev;
973 idev = __in6_dev_get(dev);
977 err = __ipv6_dev_mc_dec(idev, addr);
981 EXPORT_SYMBOL(ipv6_dev_mc_dec);
984 * check if the interface/address pair is valid
986 bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
987 const struct in6_addr *src_addr)
989 struct inet6_dev *idev;
990 struct ifmcaddr6 *mc;
994 idev = __in6_dev_get(dev);
996 for_each_mc_rcu(idev, mc) {
997 if (ipv6_addr_equal(&mc->mca_addr, group))
1001 if (src_addr && !ipv6_addr_any(src_addr)) {
1002 struct ip6_sf_list *psf;
1004 for_each_psf_rcu(mc, psf) {
1005 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1009 rv = psf->sf_count[MCAST_INCLUDE] ||
1010 psf->sf_count[MCAST_EXCLUDE] !=
1011 mc->mca_sfcount[MCAST_EXCLUDE];
1013 rv = mc->mca_sfcount[MCAST_EXCLUDE] != 0;
1015 rv = true; /* don't filter unspecified source */
1022 static void mld_gq_start_work(struct inet6_dev *idev)
1024 unsigned long tv = prandom_u32() % idev->mc_maxdelay;
1026 idev->mc_gq_running = 1;
1027 if (!mod_delayed_work(mld_wq, &idev->mc_gq_work, tv + 2))
1031 static void mld_gq_stop_work(struct inet6_dev *idev)
1033 idev->mc_gq_running = 0;
1034 if (cancel_delayed_work(&idev->mc_gq_work))
1035 __in6_dev_put(idev);
1038 static void mld_ifc_start_work(struct inet6_dev *idev, unsigned long delay)
1040 unsigned long tv = prandom_u32() % delay;
1042 if (!mod_delayed_work(mld_wq, &idev->mc_ifc_work, tv + 2))
1046 static void mld_ifc_stop_work(struct inet6_dev *idev)
1048 idev->mc_ifc_count = 0;
1049 if (cancel_delayed_work(&idev->mc_ifc_work))
1050 __in6_dev_put(idev);
1053 static void mld_dad_start_work(struct inet6_dev *idev, unsigned long delay)
1055 unsigned long tv = prandom_u32() % delay;
1057 if (!mod_delayed_work(mld_wq, &idev->mc_dad_work, tv + 2))
1061 static void mld_dad_stop_work(struct inet6_dev *idev)
1063 if (cancel_delayed_work(&idev->mc_dad_work))
1064 __in6_dev_put(idev);
1067 static void mld_query_stop_work(struct inet6_dev *idev)
1069 spin_lock_bh(&idev->mc_query_lock);
1070 if (cancel_delayed_work(&idev->mc_query_work))
1071 __in6_dev_put(idev);
1072 spin_unlock_bh(&idev->mc_query_lock);
1075 static void mld_report_stop_work(struct inet6_dev *idev)
1077 if (cancel_delayed_work_sync(&idev->mc_report_work))
1078 __in6_dev_put(idev);
1082 * IGMP handling (alias multicast ICMPv6 messages)
1084 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1086 unsigned long delay = resptime;
1088 /* Do not start work for these addresses */
1089 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1090 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1093 if (cancel_delayed_work(&ma->mca_work)) {
1094 refcount_dec(&ma->mca_refcnt);
1095 delay = ma->mca_work.timer.expires - jiffies;
1098 if (delay >= resptime)
1099 delay = prandom_u32() % resptime;
1101 if (!mod_delayed_work(mld_wq, &ma->mca_work, delay))
1102 refcount_inc(&ma->mca_refcnt);
1103 ma->mca_flags |= MAF_TIMER_RUNNING;
1106 /* mark EXCLUDE-mode sources */
1107 static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1108 const struct in6_addr *srcs)
1110 struct ip6_sf_list *psf;
1114 for_each_psf_rtnl(pmc, psf) {
1115 if (scount == nsrcs)
1117 for (i = 0; i < nsrcs; i++) {
1118 /* skip inactive filters */
1119 if (psf->sf_count[MCAST_INCLUDE] ||
1120 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1121 psf->sf_count[MCAST_EXCLUDE])
1123 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1129 pmc->mca_flags &= ~MAF_GSQUERY;
1130 if (scount == nsrcs) /* all sources excluded */
1135 static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1136 const struct in6_addr *srcs)
1138 struct ip6_sf_list *psf;
1141 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1142 return mld_xmarksources(pmc, nsrcs, srcs);
1144 /* mark INCLUDE-mode sources */
1147 for_each_psf_rtnl(pmc, psf) {
1148 if (scount == nsrcs)
1150 for (i = 0; i < nsrcs; i++) {
1151 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1159 pmc->mca_flags &= ~MAF_GSQUERY;
1162 pmc->mca_flags |= MAF_GSQUERY;
1166 static int mld_force_mld_version(const struct inet6_dev *idev)
1168 /* Normally, both are 0 here. If enforcement to a particular is
1169 * being used, individual device enforcement will have a lower
1170 * precedence over 'all' device (.../conf/all/force_mld_version).
1173 if (dev_net(idev->dev)->ipv6.devconf_all->force_mld_version != 0)
1174 return dev_net(idev->dev)->ipv6.devconf_all->force_mld_version;
1176 return idev->cnf.force_mld_version;
1179 static bool mld_in_v2_mode_only(const struct inet6_dev *idev)
1181 return mld_force_mld_version(idev) == 2;
1184 static bool mld_in_v1_mode_only(const struct inet6_dev *idev)
1186 return mld_force_mld_version(idev) == 1;
1189 static bool mld_in_v1_mode(const struct inet6_dev *idev)
1191 if (mld_in_v2_mode_only(idev))
1193 if (mld_in_v1_mode_only(idev))
1195 if (idev->mc_v1_seen && time_before(jiffies, idev->mc_v1_seen))
1201 static void mld_set_v1_mode(struct inet6_dev *idev)
1203 /* RFC3810, relevant sections:
1204 * - 9.1. Robustness Variable
1205 * - 9.2. Query Interval
1206 * - 9.3. Query Response Interval
1207 * - 9.12. Older Version Querier Present Timeout
1209 unsigned long switchback;
1211 switchback = (idev->mc_qrv * idev->mc_qi) + idev->mc_qri;
1213 idev->mc_v1_seen = jiffies + switchback;
1216 static void mld_update_qrv(struct inet6_dev *idev,
1217 const struct mld2_query *mlh2)
1219 /* RFC3810, relevant sections:
1220 * - 5.1.8. QRV (Querier's Robustness Variable)
1221 * - 9.1. Robustness Variable
1224 /* The value of the Robustness Variable MUST NOT be zero,
1225 * and SHOULD NOT be one. Catch this here if we ever run
1226 * into such a case in future.
1228 const int min_qrv = min(MLD_QRV_DEFAULT, sysctl_mld_qrv);
1229 WARN_ON(idev->mc_qrv == 0);
1231 if (mlh2->mld2q_qrv > 0)
1232 idev->mc_qrv = mlh2->mld2q_qrv;
1234 if (unlikely(idev->mc_qrv < min_qrv)) {
1235 net_warn_ratelimited("IPv6: MLD: clamping QRV from %u to %u!\n",
1236 idev->mc_qrv, min_qrv);
1237 idev->mc_qrv = min_qrv;
1241 static void mld_update_qi(struct inet6_dev *idev,
1242 const struct mld2_query *mlh2)
1244 /* RFC3810, relevant sections:
1245 * - 5.1.9. QQIC (Querier's Query Interval Code)
1246 * - 9.2. Query Interval
1247 * - 9.12. Older Version Querier Present Timeout
1248 * (the [Query Interval] in the last Query received)
1250 unsigned long mc_qqi;
1252 if (mlh2->mld2q_qqic < 128) {
1253 mc_qqi = mlh2->mld2q_qqic;
1255 unsigned long mc_man, mc_exp;
1257 mc_exp = MLDV2_QQIC_EXP(mlh2->mld2q_qqic);
1258 mc_man = MLDV2_QQIC_MAN(mlh2->mld2q_qqic);
1260 mc_qqi = (mc_man | 0x10) << (mc_exp + 3);
1263 idev->mc_qi = mc_qqi * HZ;
1266 static void mld_update_qri(struct inet6_dev *idev,
1267 const struct mld2_query *mlh2)
1269 /* RFC3810, relevant sections:
1270 * - 5.1.3. Maximum Response Code
1271 * - 9.3. Query Response Interval
1273 idev->mc_qri = msecs_to_jiffies(mldv2_mrc(mlh2));
1276 static int mld_process_v1(struct inet6_dev *idev, struct mld_msg *mld,
1277 unsigned long *max_delay, bool v1_query)
1279 unsigned long mldv1_md;
1281 /* Ignore v1 queries */
1282 if (mld_in_v2_mode_only(idev))
1285 mldv1_md = ntohs(mld->mld_maxdelay);
1287 /* When in MLDv1 fallback and a MLDv2 router start-up being
1288 * unaware of current MLDv1 operation, the MRC == MRD mapping
1289 * only works when the exponential algorithm is not being
1290 * used (as MLDv1 is unaware of such things).
1292 * According to the RFC author, the MLDv2 implementations
1293 * he's aware of all use a MRC < 32768 on start up queries.
1295 * Thus, should we *ever* encounter something else larger
1296 * than that, just assume the maximum possible within our
1300 mldv1_md = min(mldv1_md, MLDV1_MRD_MAX_COMPAT);
1302 *max_delay = max(msecs_to_jiffies(mldv1_md), 1UL);
1304 /* MLDv1 router present: we need to go into v1 mode *only*
1305 * when an MLDv1 query is received as per section 9.12. of
1306 * RFC3810! And we know from RFC2710 section 3.7 that MLDv1
1307 * queries MUST be of exactly 24 octets.
1310 mld_set_v1_mode(idev);
1312 /* cancel MLDv2 report work */
1313 mld_gq_stop_work(idev);
1314 /* cancel the interface change work */
1315 mld_ifc_stop_work(idev);
1316 /* clear deleted report items */
1317 mld_clear_delrec(idev);
1322 static int mld_process_v2(struct inet6_dev *idev, struct mld2_query *mld,
1323 unsigned long *max_delay)
1325 *max_delay = max(msecs_to_jiffies(mldv2_mrc(mld)), 1UL);
1327 mld_update_qrv(idev, mld);
1328 mld_update_qi(idev, mld);
1329 mld_update_qri(idev, mld);
1331 idev->mc_maxdelay = *max_delay;
1336 /* called with rcu_read_lock() */
1337 int igmp6_event_query(struct sk_buff *skb)
1339 struct inet6_dev *idev = __in6_dev_get(skb->dev);
1349 spin_lock_bh(&idev->mc_query_lock);
1350 if (skb_queue_len(&idev->mc_query_queue) < MLD_MAX_SKBS) {
1351 __skb_queue_tail(&idev->mc_query_queue, skb);
1352 if (!mod_delayed_work(mld_wq, &idev->mc_query_work, 0))
1355 spin_unlock_bh(&idev->mc_query_lock);
1360 static void __mld_query_work(struct sk_buff *skb)
1362 struct mld2_query *mlh2 = NULL;
1363 const struct in6_addr *group;
1364 unsigned long max_delay;
1365 struct inet6_dev *idev;
1366 struct ifmcaddr6 *ma;
1367 struct mld_msg *mld;
1372 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1375 /* compute payload length excluding extension headers */
1376 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
1377 len -= skb_network_header_len(skb);
1380 * Upon reception of an MLD message that contains a Query, the node
1381 * checks if the source address of the message is a valid link-local
1382 * address, if the Hop Limit is set to 1, and if the Router Alert
1383 * option is present in the Hop-By-Hop Options header of the IPv6
1384 * packet. If any of these checks fails, the packet is dropped.
1386 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL) ||
1387 ipv6_hdr(skb)->hop_limit != 1 ||
1388 !(IP6CB(skb)->flags & IP6SKB_ROUTERALERT) ||
1389 IP6CB(skb)->ra != htons(IPV6_OPT_ROUTERALERT_MLD))
1392 idev = __in6_dev_get(skb->dev);
1396 mld = (struct mld_msg *)icmp6_hdr(skb);
1397 group = &mld->mld_mca;
1398 group_type = ipv6_addr_type(group);
1400 if (group_type != IPV6_ADDR_ANY &&
1401 !(group_type&IPV6_ADDR_MULTICAST))
1404 if (len < MLD_V1_QUERY_LEN) {
1406 } else if (len == MLD_V1_QUERY_LEN || mld_in_v1_mode(idev)) {
1407 err = mld_process_v1(idev, mld, &max_delay,
1408 len == MLD_V1_QUERY_LEN);
1411 } else if (len >= MLD_V2_QUERY_LEN_MIN) {
1412 int srcs_offset = sizeof(struct mld2_query) -
1413 sizeof(struct icmp6hdr);
1415 if (!pskb_may_pull(skb, srcs_offset))
1418 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1420 err = mld_process_v2(idev, mlh2, &max_delay);
1424 if (group_type == IPV6_ADDR_ANY) { /* general query */
1425 if (mlh2->mld2q_nsrcs)
1426 goto out; /* no sources allowed */
1428 mld_gq_start_work(idev);
1431 /* mark sources to include, if group & source-specific */
1432 if (mlh2->mld2q_nsrcs != 0) {
1433 if (!pskb_may_pull(skb, srcs_offset +
1434 ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
1437 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1444 if (group_type == IPV6_ADDR_ANY) {
1445 for_each_mc_rtnl(idev, ma) {
1446 igmp6_group_queried(ma, max_delay);
1449 for_each_mc_rtnl(idev, ma) {
1450 if (!ipv6_addr_equal(group, &ma->mca_addr))
1452 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1453 /* gsquery <- gsquery && mark */
1455 ma->mca_flags &= ~MAF_GSQUERY;
1457 /* gsquery <- mark */
1459 ma->mca_flags |= MAF_GSQUERY;
1461 ma->mca_flags &= ~MAF_GSQUERY;
1463 if (!(ma->mca_flags & MAF_GSQUERY) ||
1464 mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
1465 igmp6_group_queried(ma, max_delay);
1474 static void mld_query_work(struct work_struct *work)
1476 struct inet6_dev *idev = container_of(to_delayed_work(work),
1479 struct sk_buff_head q;
1480 struct sk_buff *skb;
1481 bool rework = false;
1484 skb_queue_head_init(&q);
1486 spin_lock_bh(&idev->mc_query_lock);
1487 while ((skb = __skb_dequeue(&idev->mc_query_queue))) {
1488 __skb_queue_tail(&q, skb);
1490 if (++cnt >= MLD_MAX_QUEUE) {
1492 schedule_delayed_work(&idev->mc_query_work, 0);
1496 spin_unlock_bh(&idev->mc_query_lock);
1499 while ((skb = __skb_dequeue(&q)))
1500 __mld_query_work(skb);
1507 /* called with rcu_read_lock() */
1508 int igmp6_event_report(struct sk_buff *skb)
1510 struct inet6_dev *idev = __in6_dev_get(skb->dev);
1520 spin_lock_bh(&idev->mc_report_lock);
1521 if (skb_queue_len(&idev->mc_report_queue) < MLD_MAX_SKBS) {
1522 __skb_queue_tail(&idev->mc_report_queue, skb);
1523 if (!mod_delayed_work(mld_wq, &idev->mc_report_work, 0))
1526 spin_unlock_bh(&idev->mc_report_lock);
1531 static void __mld_report_work(struct sk_buff *skb)
1533 struct ifmcaddr6 *ma;
1534 struct inet6_dev *idev;
1535 struct mld_msg *mld;
1538 /* Our own report looped back. Ignore it. */
1539 if (skb->pkt_type == PACKET_LOOPBACK)
1542 /* send our report if the MC router may not have heard this report */
1543 if (skb->pkt_type != PACKET_MULTICAST &&
1544 skb->pkt_type != PACKET_BROADCAST)
1547 if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
1550 mld = (struct mld_msg *)icmp6_hdr(skb);
1552 /* Drop reports with not link local source */
1553 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
1554 if (addr_type != IPV6_ADDR_ANY &&
1555 !(addr_type&IPV6_ADDR_LINKLOCAL))
1558 idev = __in6_dev_get(skb->dev);
1563 * Cancel the work for this group
1566 for_each_mc_rtnl(idev, ma) {
1567 if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
1568 if (cancel_delayed_work(&ma->mca_work))
1569 refcount_dec(&ma->mca_refcnt);
1570 ma->mca_flags &= ~(MAF_LAST_REPORTER |
1580 static void mld_report_work(struct work_struct *work)
1582 struct inet6_dev *idev = container_of(to_delayed_work(work),
1585 struct sk_buff_head q;
1586 struct sk_buff *skb;
1587 bool rework = false;
1590 skb_queue_head_init(&q);
1591 spin_lock_bh(&idev->mc_report_lock);
1592 while ((skb = __skb_dequeue(&idev->mc_report_queue))) {
1593 __skb_queue_tail(&q, skb);
1595 if (++cnt >= MLD_MAX_QUEUE) {
1597 schedule_delayed_work(&idev->mc_report_work, 0);
1601 spin_unlock_bh(&idev->mc_report_lock);
1604 while ((skb = __skb_dequeue(&q)))
1605 __mld_report_work(skb);
1612 static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1613 int gdeleted, int sdeleted)
1616 case MLD2_MODE_IS_INCLUDE:
1617 case MLD2_MODE_IS_EXCLUDE:
1618 if (gdeleted || sdeleted)
1620 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1621 if (pmc->mca_sfmode == MCAST_INCLUDE)
1623 /* don't include if this source is excluded
1626 if (psf->sf_count[MCAST_INCLUDE])
1627 return type == MLD2_MODE_IS_INCLUDE;
1628 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1629 psf->sf_count[MCAST_EXCLUDE];
1632 case MLD2_CHANGE_TO_INCLUDE:
1633 if (gdeleted || sdeleted)
1635 return psf->sf_count[MCAST_INCLUDE] != 0;
1636 case MLD2_CHANGE_TO_EXCLUDE:
1637 if (gdeleted || sdeleted)
1639 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1640 psf->sf_count[MCAST_INCLUDE])
1642 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1643 psf->sf_count[MCAST_EXCLUDE];
1644 case MLD2_ALLOW_NEW_SOURCES:
1645 if (gdeleted || !psf->sf_crcount)
1647 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1648 case MLD2_BLOCK_OLD_SOURCES:
1649 if (pmc->mca_sfmode == MCAST_INCLUDE)
1650 return gdeleted || (psf->sf_crcount && sdeleted);
1651 return psf->sf_crcount && !gdeleted && !sdeleted;
1657 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1659 struct ip6_sf_list *psf;
1662 for_each_psf_rtnl(pmc, psf) {
1663 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1670 static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
1671 struct net_device *dev,
1672 const struct in6_addr *saddr,
1673 const struct in6_addr *daddr,
1676 struct ipv6hdr *hdr;
1678 skb->protocol = htons(ETH_P_IPV6);
1681 skb_reset_network_header(skb);
1682 skb_put(skb, sizeof(struct ipv6hdr));
1683 hdr = ipv6_hdr(skb);
1685 ip6_flow_hdr(hdr, 0, 0);
1687 hdr->payload_len = htons(len);
1688 hdr->nexthdr = proto;
1689 hdr->hop_limit = inet6_sk(sk)->hop_limit;
1691 hdr->saddr = *saddr;
1692 hdr->daddr = *daddr;
1695 static struct sk_buff *mld_newpack(struct inet6_dev *idev, unsigned int mtu)
1697 struct net_device *dev = idev->dev;
1698 struct net *net = dev_net(dev);
1699 struct sock *sk = net->ipv6.igmp_sk;
1700 struct sk_buff *skb;
1701 struct mld2_report *pmr;
1702 struct in6_addr addr_buf;
1703 const struct in6_addr *saddr;
1704 int hlen = LL_RESERVED_SPACE(dev);
1705 int tlen = dev->needed_tailroom;
1706 unsigned int size = mtu + hlen + tlen;
1708 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1709 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1712 /* we assume size > sizeof(ra) here */
1713 /* limit our allocations to order-0 page */
1714 size = min_t(int, size, SKB_MAX_ORDER(0, 0));
1715 skb = sock_alloc_send_skb(sk, size, 1, &err);
1720 skb->priority = TC_PRIO_CONTROL;
1721 skb_reserve(skb, hlen);
1722 skb_tailroom_reserve(skb, mtu, tlen);
1724 if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
1725 /* <draft-ietf-magma-mld-source-05.txt>:
1726 * use unspecified address as the source address
1727 * when a valid link-local address is not available.
1729 saddr = &in6addr_any;
1733 ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
1735 skb_put_data(skb, ra, sizeof(ra));
1737 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
1738 skb_put(skb, sizeof(*pmr));
1739 pmr = (struct mld2_report *)skb_transport_header(skb);
1740 pmr->mld2r_type = ICMPV6_MLD2_REPORT;
1741 pmr->mld2r_resv1 = 0;
1742 pmr->mld2r_cksum = 0;
1743 pmr->mld2r_resv2 = 0;
1744 pmr->mld2r_ngrec = 0;
1748 static void mld_sendpack(struct sk_buff *skb)
1750 struct ipv6hdr *pip6 = ipv6_hdr(skb);
1751 struct mld2_report *pmr =
1752 (struct mld2_report *)skb_transport_header(skb);
1753 int payload_len, mldlen;
1754 struct inet6_dev *idev;
1755 struct net *net = dev_net(skb->dev);
1758 struct dst_entry *dst;
1761 idev = __in6_dev_get(skb->dev);
1762 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1764 payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
1766 mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
1767 pip6->payload_len = htons(payload_len);
1769 pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1771 csum_partial(skb_transport_header(skb),
1774 icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
1775 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1777 dst = icmp6_dst_alloc(skb->dev, &fl6);
1784 skb_dst_set(skb, dst);
1788 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
1789 net, net->ipv6.igmp_sk, skb, NULL, skb->dev,
1793 ICMP6MSGOUT_INC_STATS(net, idev, ICMPV6_MLD2_REPORT);
1794 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
1796 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
1807 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1809 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1812 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1813 int type, struct mld2_grec **ppgr, unsigned int mtu)
1815 struct mld2_report *pmr;
1816 struct mld2_grec *pgr;
1819 skb = mld_newpack(pmc->idev, mtu);
1823 pgr = skb_put(skb, sizeof(struct mld2_grec));
1824 pgr->grec_type = type;
1825 pgr->grec_auxwords = 0;
1826 pgr->grec_nsrcs = 0;
1827 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1828 pmr = (struct mld2_report *)skb_transport_header(skb);
1829 pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
1834 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
1836 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1837 int type, int gdeleted, int sdeleted,
1840 struct ip6_sf_list *psf, *psf_prev, *psf_next;
1841 int scount, stotal, first, isquery, truncate;
1842 struct ip6_sf_list __rcu **psf_list;
1843 struct inet6_dev *idev = pmc->idev;
1844 struct net_device *dev = idev->dev;
1845 struct mld2_grec *pgr = NULL;
1846 struct mld2_report *pmr;
1849 if (pmc->mca_flags & MAF_NOREPORT)
1852 mtu = READ_ONCE(dev->mtu);
1853 if (mtu < IPV6_MIN_MTU)
1856 isquery = type == MLD2_MODE_IS_INCLUDE ||
1857 type == MLD2_MODE_IS_EXCLUDE;
1858 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1859 type == MLD2_CHANGE_TO_EXCLUDE;
1861 stotal = scount = 0;
1863 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1865 if (!rcu_access_pointer(*psf_list))
1868 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
1870 /* EX and TO_EX get a fresh packet, if needed */
1872 if (pmr && pmr->mld2r_ngrec &&
1873 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1876 skb = mld_newpack(idev, mtu);
1881 for (psf = rtnl_dereference(*psf_list);
1884 struct in6_addr *psrc;
1886 psf_next = rtnl_dereference(psf->sf_next);
1888 if (!is_in(pmc, psf, type, gdeleted, sdeleted) && !crsend) {
1893 /* Based on RFC3810 6.1. Should not send source-list change
1894 * records when there is a filter mode change.
1896 if (((gdeleted && pmc->mca_sfmode == MCAST_EXCLUDE) ||
1897 (!gdeleted && pmc->mca_crcount)) &&
1898 (type == MLD2_ALLOW_NEW_SOURCES ||
1899 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount)
1900 goto decrease_sf_crcount;
1902 /* clear marks on query responses */
1906 if (AVAILABLE(skb) < sizeof(*psrc) +
1907 first*sizeof(struct mld2_grec)) {
1908 if (truncate && !first)
1909 break; /* truncate these */
1911 pgr->grec_nsrcs = htons(scount);
1914 skb = mld_newpack(idev, mtu);
1919 skb = add_grhead(skb, pmc, type, &pgr, mtu);
1924 psrc = skb_put(skb, sizeof(*psrc));
1925 *psrc = psf->sf_addr;
1927 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1928 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1929 decrease_sf_crcount:
1931 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1933 rcu_assign_pointer(psf_prev->sf_next,
1934 rtnl_dereference(psf->sf_next));
1936 rcu_assign_pointer(*psf_list,
1937 rtnl_dereference(psf->sf_next));
1938 kfree_rcu(psf, rcu);
1947 if (type == MLD2_ALLOW_NEW_SOURCES ||
1948 type == MLD2_BLOCK_OLD_SOURCES)
1950 if (pmc->mca_crcount || isquery || crsend) {
1951 /* make sure we have room for group header */
1952 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1954 skb = NULL; /* add_grhead will get a new one */
1956 skb = add_grhead(skb, pmc, type, &pgr, mtu);
1960 pgr->grec_nsrcs = htons(scount);
1963 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1967 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1969 struct sk_buff *skb = NULL;
1973 for_each_mc_rtnl(idev, pmc) {
1974 if (pmc->mca_flags & MAF_NOREPORT)
1976 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1977 type = MLD2_MODE_IS_EXCLUDE;
1979 type = MLD2_MODE_IS_INCLUDE;
1980 skb = add_grec(skb, pmc, type, 0, 0, 0);
1983 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1984 type = MLD2_MODE_IS_EXCLUDE;
1986 type = MLD2_MODE_IS_INCLUDE;
1987 skb = add_grec(skb, pmc, type, 0, 0, 0);
1994 * remove zero-count source records from a source filter list
1996 static void mld_clear_zeros(struct ip6_sf_list __rcu **ppsf)
1998 struct ip6_sf_list *psf_prev, *psf_next, *psf;
2001 for (psf = rtnl_dereference(*ppsf);
2004 psf_next = rtnl_dereference(psf->sf_next);
2005 if (psf->sf_crcount == 0) {
2007 rcu_assign_pointer(psf_prev->sf_next,
2008 rtnl_dereference(psf->sf_next));
2010 rcu_assign_pointer(*ppsf,
2011 rtnl_dereference(psf->sf_next));
2012 kfree_rcu(psf, rcu);
2019 static void mld_send_cr(struct inet6_dev *idev)
2021 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
2022 struct sk_buff *skb = NULL;
2027 for (pmc = rtnl_dereference(idev->mc_tomb);
2030 pmc_next = rtnl_dereference(pmc->next);
2031 if (pmc->mca_sfmode == MCAST_INCLUDE) {
2032 type = MLD2_BLOCK_OLD_SOURCES;
2033 dtype = MLD2_BLOCK_OLD_SOURCES;
2034 skb = add_grec(skb, pmc, type, 1, 0, 0);
2035 skb = add_grec(skb, pmc, dtype, 1, 1, 0);
2037 if (pmc->mca_crcount) {
2038 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
2039 type = MLD2_CHANGE_TO_INCLUDE;
2040 skb = add_grec(skb, pmc, type, 1, 0, 0);
2043 if (pmc->mca_crcount == 0) {
2044 mld_clear_zeros(&pmc->mca_tomb);
2045 mld_clear_zeros(&pmc->mca_sources);
2048 if (pmc->mca_crcount == 0 &&
2049 !rcu_access_pointer(pmc->mca_tomb) &&
2050 !rcu_access_pointer(pmc->mca_sources)) {
2052 rcu_assign_pointer(pmc_prev->next, pmc_next);
2054 rcu_assign_pointer(idev->mc_tomb, pmc_next);
2055 in6_dev_put(pmc->idev);
2056 kfree_rcu(pmc, rcu);
2062 for_each_mc_rtnl(idev, pmc) {
2063 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2064 type = MLD2_BLOCK_OLD_SOURCES;
2065 dtype = MLD2_ALLOW_NEW_SOURCES;
2067 type = MLD2_ALLOW_NEW_SOURCES;
2068 dtype = MLD2_BLOCK_OLD_SOURCES;
2070 skb = add_grec(skb, pmc, type, 0, 0, 0);
2071 skb = add_grec(skb, pmc, dtype, 0, 1, 0); /* deleted sources */
2073 /* filter mode changes */
2074 if (pmc->mca_crcount) {
2075 if (pmc->mca_sfmode == MCAST_EXCLUDE)
2076 type = MLD2_CHANGE_TO_EXCLUDE;
2078 type = MLD2_CHANGE_TO_INCLUDE;
2079 skb = add_grec(skb, pmc, type, 0, 0, 0);
2085 (void) mld_sendpack(skb);
2088 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
2090 struct net *net = dev_net(dev);
2091 struct sock *sk = net->ipv6.igmp_sk;
2092 struct inet6_dev *idev;
2093 struct sk_buff *skb;
2094 struct mld_msg *hdr;
2095 const struct in6_addr *snd_addr, *saddr;
2096 struct in6_addr addr_buf;
2097 int hlen = LL_RESERVED_SPACE(dev);
2098 int tlen = dev->needed_tailroom;
2099 int err, len, payload_len, full_len;
2100 u8 ra[8] = { IPPROTO_ICMPV6, 0,
2101 IPV6_TLV_ROUTERALERT, 2, 0, 0,
2104 struct dst_entry *dst;
2106 if (type == ICMPV6_MGM_REDUCTION)
2107 snd_addr = &in6addr_linklocal_allrouters;
2111 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
2112 payload_len = len + sizeof(ra);
2113 full_len = sizeof(struct ipv6hdr) + payload_len;
2116 IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
2117 IPSTATS_MIB_OUT, full_len);
2120 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
2124 IP6_INC_STATS(net, __in6_dev_get(dev),
2125 IPSTATS_MIB_OUTDISCARDS);
2129 skb->priority = TC_PRIO_CONTROL;
2130 skb_reserve(skb, hlen);
2132 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
2133 /* <draft-ietf-magma-mld-source-05.txt>:
2134 * use unspecified address as the source address
2135 * when a valid link-local address is not available.
2137 saddr = &in6addr_any;
2141 ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
2143 skb_put_data(skb, ra, sizeof(ra));
2145 hdr = skb_put_zero(skb, sizeof(struct mld_msg));
2146 hdr->mld_type = type;
2147 hdr->mld_mca = *addr;
2149 hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
2151 csum_partial(hdr, len, 0));
2154 idev = __in6_dev_get(skb->dev);
2156 icmpv6_flow_init(sk, &fl6, type,
2157 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
2159 dst = icmp6_dst_alloc(skb->dev, &fl6);
2165 skb_dst_set(skb, dst);
2166 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
2167 net, sk, skb, NULL, skb->dev,
2171 ICMP6MSGOUT_INC_STATS(net, idev, type);
2172 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
2174 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
2184 static void mld_send_initial_cr(struct inet6_dev *idev)
2186 struct sk_buff *skb;
2187 struct ifmcaddr6 *pmc;
2190 if (mld_in_v1_mode(idev))
2194 for_each_mc_rtnl(idev, pmc) {
2195 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2196 type = MLD2_CHANGE_TO_EXCLUDE;
2198 type = MLD2_ALLOW_NEW_SOURCES;
2199 skb = add_grec(skb, pmc, type, 0, 0, 1);
2205 void ipv6_mc_dad_complete(struct inet6_dev *idev)
2207 idev->mc_dad_count = idev->mc_qrv;
2208 if (idev->mc_dad_count) {
2209 mld_send_initial_cr(idev);
2210 idev->mc_dad_count--;
2211 if (idev->mc_dad_count)
2212 mld_dad_start_work(idev,
2213 unsolicited_report_interval(idev));
2217 static void mld_dad_work(struct work_struct *work)
2219 struct inet6_dev *idev = container_of(to_delayed_work(work),
2224 mld_send_initial_cr(idev);
2225 if (idev->mc_dad_count) {
2226 idev->mc_dad_count--;
2227 if (idev->mc_dad_count)
2228 mld_dad_start_work(idev,
2229 unsolicited_report_interval(idev));
2235 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
2236 const struct in6_addr *psfsrc)
2238 struct ip6_sf_list *psf, *psf_prev;
2242 for_each_psf_rtnl(pmc, psf) {
2243 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2247 if (!psf || psf->sf_count[sfmode] == 0) {
2248 /* source filter not found, or count wrong => bug */
2251 psf->sf_count[sfmode]--;
2252 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
2253 struct inet6_dev *idev = pmc->idev;
2255 /* no more filters for this source */
2257 rcu_assign_pointer(psf_prev->sf_next,
2258 rtnl_dereference(psf->sf_next));
2260 rcu_assign_pointer(pmc->mca_sources,
2261 rtnl_dereference(psf->sf_next));
2263 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
2264 !mld_in_v1_mode(idev)) {
2265 psf->sf_crcount = idev->mc_qrv;
2266 rcu_assign_pointer(psf->sf_next,
2267 rtnl_dereference(pmc->mca_tomb));
2268 rcu_assign_pointer(pmc->mca_tomb, psf);
2271 kfree_rcu(psf, rcu);
2277 static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2278 int sfmode, int sfcount, const struct in6_addr *psfsrc,
2281 struct ifmcaddr6 *pmc;
2288 for_each_mc_rtnl(idev, pmc) {
2289 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2297 if (!pmc->mca_sfcount[sfmode]) {
2301 pmc->mca_sfcount[sfmode]--;
2304 for (i = 0; i < sfcount; i++) {
2305 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2307 changerec |= rv > 0;
2311 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
2312 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
2313 pmc->mca_sfcount[MCAST_INCLUDE]) {
2314 struct ip6_sf_list *psf;
2316 /* filter mode change */
2317 pmc->mca_sfmode = MCAST_INCLUDE;
2318 pmc->mca_crcount = idev->mc_qrv;
2319 idev->mc_ifc_count = pmc->mca_crcount;
2320 for_each_psf_rtnl(pmc, psf)
2321 psf->sf_crcount = 0;
2322 mld_ifc_event(pmc->idev);
2323 } else if (sf_setstate(pmc) || changerec)
2324 mld_ifc_event(pmc->idev);
2329 * Add multicast single-source filter to the interface list
2331 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
2332 const struct in6_addr *psfsrc)
2334 struct ip6_sf_list *psf, *psf_prev;
2337 for_each_psf_rtnl(pmc, psf) {
2338 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2343 psf = kzalloc(sizeof(*psf), GFP_KERNEL);
2347 psf->sf_addr = *psfsrc;
2349 rcu_assign_pointer(psf_prev->sf_next, psf);
2351 rcu_assign_pointer(pmc->mca_sources, psf);
2354 psf->sf_count[sfmode]++;
2358 static void sf_markstate(struct ifmcaddr6 *pmc)
2360 struct ip6_sf_list *psf;
2361 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2363 for_each_psf_rtnl(pmc, psf) {
2364 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2365 psf->sf_oldin = mca_xcount ==
2366 psf->sf_count[MCAST_EXCLUDE] &&
2367 !psf->sf_count[MCAST_INCLUDE];
2369 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2374 static int sf_setstate(struct ifmcaddr6 *pmc)
2376 struct ip6_sf_list *psf, *dpsf;
2377 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2378 int qrv = pmc->idev->mc_qrv;
2382 for_each_psf_rtnl(pmc, psf) {
2383 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2384 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2385 !psf->sf_count[MCAST_INCLUDE];
2387 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2389 if (!psf->sf_oldin) {
2390 struct ip6_sf_list *prev = NULL;
2392 for_each_psf_tomb(pmc, dpsf) {
2393 if (ipv6_addr_equal(&dpsf->sf_addr,
2400 rcu_assign_pointer(prev->sf_next,
2401 rtnl_dereference(dpsf->sf_next));
2403 rcu_assign_pointer(pmc->mca_tomb,
2404 rtnl_dereference(dpsf->sf_next));
2405 kfree_rcu(dpsf, rcu);
2407 psf->sf_crcount = qrv;
2410 } else if (psf->sf_oldin) {
2411 psf->sf_crcount = 0;
2413 * add or update "delete" records if an active filter
2417 for_each_psf_tomb(pmc, dpsf)
2418 if (ipv6_addr_equal(&dpsf->sf_addr,
2422 dpsf = kmalloc(sizeof(*dpsf), GFP_KERNEL);
2426 rcu_assign_pointer(dpsf->sf_next,
2427 rtnl_dereference(pmc->mca_tomb));
2428 rcu_assign_pointer(pmc->mca_tomb, dpsf);
2430 dpsf->sf_crcount = qrv;
2438 * Add multicast source filter list to the interface list
2440 static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2441 int sfmode, int sfcount, const struct in6_addr *psfsrc,
2444 struct ifmcaddr6 *pmc;
2451 for_each_mc_rtnl(idev, pmc) {
2452 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2459 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2461 pmc->mca_sfcount[sfmode]++;
2463 for (i = 0; i < sfcount; i++) {
2464 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2472 pmc->mca_sfcount[sfmode]--;
2473 for (j = 0; j < i; j++)
2474 ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2475 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2476 struct ip6_sf_list *psf;
2478 /* filter mode change */
2479 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2480 pmc->mca_sfmode = MCAST_EXCLUDE;
2481 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2482 pmc->mca_sfmode = MCAST_INCLUDE;
2483 /* else no filters; keep old mode for reports */
2485 pmc->mca_crcount = idev->mc_qrv;
2486 idev->mc_ifc_count = pmc->mca_crcount;
2487 for_each_psf_rtnl(pmc, psf)
2488 psf->sf_crcount = 0;
2489 mld_ifc_event(idev);
2490 } else if (sf_setstate(pmc)) {
2491 mld_ifc_event(idev);
2496 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2498 struct ip6_sf_list *psf, *nextpsf;
2500 for (psf = rtnl_dereference(pmc->mca_tomb);
2503 nextpsf = rtnl_dereference(psf->sf_next);
2504 kfree_rcu(psf, rcu);
2506 RCU_INIT_POINTER(pmc->mca_tomb, NULL);
2507 for (psf = rtnl_dereference(pmc->mca_sources);
2510 nextpsf = rtnl_dereference(psf->sf_next);
2511 kfree_rcu(psf, rcu);
2513 RCU_INIT_POINTER(pmc->mca_sources, NULL);
2514 pmc->mca_sfmode = MCAST_EXCLUDE;
2515 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2516 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2520 static void igmp6_join_group(struct ifmcaddr6 *ma)
2522 unsigned long delay;
2524 if (ma->mca_flags & MAF_NOREPORT)
2527 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2529 delay = prandom_u32() % unsolicited_report_interval(ma->idev);
2531 if (cancel_delayed_work(&ma->mca_work)) {
2532 refcount_dec(&ma->mca_refcnt);
2533 delay = ma->mca_work.timer.expires - jiffies;
2536 if (!mod_delayed_work(mld_wq, &ma->mca_work, delay))
2537 refcount_inc(&ma->mca_refcnt);
2538 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2541 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2542 struct inet6_dev *idev)
2544 struct ip6_sf_socklist *psl;
2547 psl = rtnl_dereference(iml->sflist);
2550 /* any-source empty exclude case */
2551 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2553 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2554 psl->sl_count, psl->sl_addr, 0);
2555 RCU_INIT_POINTER(iml->sflist, NULL);
2556 atomic_sub(IP6_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2557 kfree_rcu(psl, rcu);
2562 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2564 if (mld_in_v1_mode(ma->idev)) {
2565 if (ma->mca_flags & MAF_LAST_REPORTER) {
2566 igmp6_send(&ma->mca_addr, ma->idev->dev,
2567 ICMPV6_MGM_REDUCTION);
2570 mld_add_delrec(ma->idev, ma);
2571 mld_ifc_event(ma->idev);
2575 static void mld_gq_work(struct work_struct *work)
2577 struct inet6_dev *idev = container_of(to_delayed_work(work),
2582 mld_send_report(idev, NULL);
2583 idev->mc_gq_running = 0;
2589 static void mld_ifc_work(struct work_struct *work)
2591 struct inet6_dev *idev = container_of(to_delayed_work(work),
2598 if (idev->mc_ifc_count) {
2599 idev->mc_ifc_count--;
2600 if (idev->mc_ifc_count)
2601 mld_ifc_start_work(idev,
2602 unsolicited_report_interval(idev));
2608 static void mld_ifc_event(struct inet6_dev *idev)
2610 if (mld_in_v1_mode(idev))
2613 idev->mc_ifc_count = idev->mc_qrv;
2614 mld_ifc_start_work(idev, 1);
2617 static void mld_mca_work(struct work_struct *work)
2619 struct ifmcaddr6 *ma = container_of(to_delayed_work(work),
2620 struct ifmcaddr6, mca_work);
2623 if (mld_in_v1_mode(ma->idev))
2624 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2626 mld_send_report(ma->idev, ma);
2627 ma->mca_flags |= MAF_LAST_REPORTER;
2628 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2634 /* Device changing type */
2636 void ipv6_mc_unmap(struct inet6_dev *idev)
2638 struct ifmcaddr6 *i;
2640 /* Install multicast list, except for all-nodes (already installed) */
2642 for_each_mc_rtnl(idev, i)
2643 igmp6_group_dropped(i);
2646 void ipv6_mc_remap(struct inet6_dev *idev)
2651 /* Device going down */
2653 void ipv6_mc_down(struct inet6_dev *idev)
2655 struct ifmcaddr6 *i;
2657 /* Withdraw multicast list */
2658 for_each_mc_rtnl(idev, i)
2659 igmp6_group_dropped(i);
2661 /* Should stop work after group drop. or we will
2662 * start work again in mld_ifc_event()
2665 mld_query_stop_work(idev);
2666 mld_report_stop_work(idev);
2667 mld_ifc_stop_work(idev);
2668 mld_gq_stop_work(idev);
2669 mld_dad_stop_work(idev);
2672 static void ipv6_mc_reset(struct inet6_dev *idev)
2674 idev->mc_qrv = sysctl_mld_qrv;
2675 idev->mc_qi = MLD_QI_DEFAULT;
2676 idev->mc_qri = MLD_QRI_DEFAULT;
2677 idev->mc_v1_seen = 0;
2678 idev->mc_maxdelay = unsolicited_report_interval(idev);
2681 /* Device going up */
2683 void ipv6_mc_up(struct inet6_dev *idev)
2685 struct ifmcaddr6 *i;
2687 /* Install multicast list, except for all-nodes (already installed) */
2689 ipv6_mc_reset(idev);
2690 for_each_mc_rtnl(idev, i) {
2691 mld_del_delrec(idev, i);
2692 igmp6_group_added(i);
2696 /* IPv6 device initialization. */
2698 void ipv6_mc_init_dev(struct inet6_dev *idev)
2700 idev->mc_gq_running = 0;
2701 INIT_DELAYED_WORK(&idev->mc_gq_work, mld_gq_work);
2702 RCU_INIT_POINTER(idev->mc_tomb, NULL);
2703 idev->mc_ifc_count = 0;
2704 INIT_DELAYED_WORK(&idev->mc_ifc_work, mld_ifc_work);
2705 INIT_DELAYED_WORK(&idev->mc_dad_work, mld_dad_work);
2706 INIT_DELAYED_WORK(&idev->mc_query_work, mld_query_work);
2707 INIT_DELAYED_WORK(&idev->mc_report_work, mld_report_work);
2708 skb_queue_head_init(&idev->mc_query_queue);
2709 skb_queue_head_init(&idev->mc_report_queue);
2710 spin_lock_init(&idev->mc_query_lock);
2711 spin_lock_init(&idev->mc_report_lock);
2712 ipv6_mc_reset(idev);
2716 * Device is about to be destroyed: clean up.
2719 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2721 struct ifmcaddr6 *i;
2723 /* Deactivate works */
2725 mld_clear_delrec(idev);
2726 mld_clear_query(idev);
2727 mld_clear_report(idev);
2729 /* Delete all-nodes address. */
2730 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2731 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2734 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
2736 if (idev->cnf.forwarding)
2737 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
2739 while ((i = rtnl_dereference(idev->mc_list))) {
2740 rcu_assign_pointer(idev->mc_list, rtnl_dereference(i->next));
2742 ip6_mc_clear_src(i);
2747 static void ipv6_mc_rejoin_groups(struct inet6_dev *idev)
2749 struct ifmcaddr6 *pmc;
2753 if (mld_in_v1_mode(idev)) {
2754 for_each_mc_rtnl(idev, pmc)
2755 igmp6_join_group(pmc);
2757 mld_send_report(idev, NULL);
2761 static int ipv6_mc_netdev_event(struct notifier_block *this,
2762 unsigned long event,
2765 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2766 struct inet6_dev *idev = __in6_dev_get(dev);
2769 case NETDEV_RESEND_IGMP:
2771 ipv6_mc_rejoin_groups(idev);
2780 static struct notifier_block igmp6_netdev_notifier = {
2781 .notifier_call = ipv6_mc_netdev_event,
2784 #ifdef CONFIG_PROC_FS
2785 struct igmp6_mc_iter_state {
2786 struct seq_net_private p;
2787 struct net_device *dev;
2788 struct inet6_dev *idev;
2791 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2793 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2795 struct ifmcaddr6 *im = NULL;
2796 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2797 struct net *net = seq_file_net(seq);
2800 for_each_netdev_rcu(net, state->dev) {
2801 struct inet6_dev *idev;
2802 idev = __in6_dev_get(state->dev);
2806 im = rcu_dereference(idev->mc_list);
2815 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2817 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2819 im = rcu_dereference(im->next);
2821 state->dev = next_net_device_rcu(state->dev);
2826 state->idev = __in6_dev_get(state->dev);
2829 im = rcu_dereference(state->idev->mc_list);
2834 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2836 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2838 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2840 return pos ? NULL : im;
2843 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2847 return igmp6_mc_get_idx(seq, *pos);
2850 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2852 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2858 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2861 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2863 if (likely(state->idev))
2869 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2871 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2872 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2875 "%-4d %-15s %pi6 %5d %08X %ld\n",
2876 state->dev->ifindex, state->dev->name,
2878 im->mca_users, im->mca_flags,
2879 (im->mca_flags & MAF_TIMER_RUNNING) ?
2880 jiffies_to_clock_t(im->mca_work.timer.expires - jiffies) : 0);
2884 static const struct seq_operations igmp6_mc_seq_ops = {
2885 .start = igmp6_mc_seq_start,
2886 .next = igmp6_mc_seq_next,
2887 .stop = igmp6_mc_seq_stop,
2888 .show = igmp6_mc_seq_show,
2891 struct igmp6_mcf_iter_state {
2892 struct seq_net_private p;
2893 struct net_device *dev;
2894 struct inet6_dev *idev;
2895 struct ifmcaddr6 *im;
2898 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2900 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2902 struct ip6_sf_list *psf = NULL;
2903 struct ifmcaddr6 *im = NULL;
2904 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2905 struct net *net = seq_file_net(seq);
2909 for_each_netdev_rcu(net, state->dev) {
2910 struct inet6_dev *idev;
2911 idev = __in6_dev_get(state->dev);
2912 if (unlikely(idev == NULL))
2915 im = rcu_dereference(idev->mc_list);
2917 psf = rcu_dereference(im->mca_sources);
2928 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2930 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2932 psf = rcu_dereference(psf->sf_next);
2934 state->im = rcu_dereference(state->im->next);
2935 while (!state->im) {
2936 state->dev = next_net_device_rcu(state->dev);
2941 state->idev = __in6_dev_get(state->dev);
2944 state->im = rcu_dereference(state->idev->mc_list);
2948 psf = rcu_dereference(state->im->mca_sources);
2954 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2956 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2958 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2960 return pos ? NULL : psf;
2963 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2967 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2970 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2972 struct ip6_sf_list *psf;
2973 if (v == SEQ_START_TOKEN)
2974 psf = igmp6_mcf_get_first(seq);
2976 psf = igmp6_mcf_get_next(seq, v);
2981 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2984 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2986 if (likely(state->im))
2988 if (likely(state->idev))
2995 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2997 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2998 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
3000 if (v == SEQ_START_TOKEN) {
3001 seq_puts(seq, "Idx Device Multicast Address Source Address INC EXC\n");
3004 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
3005 state->dev->ifindex, state->dev->name,
3006 &state->im->mca_addr,
3008 psf->sf_count[MCAST_INCLUDE],
3009 psf->sf_count[MCAST_EXCLUDE]);
3014 static const struct seq_operations igmp6_mcf_seq_ops = {
3015 .start = igmp6_mcf_seq_start,
3016 .next = igmp6_mcf_seq_next,
3017 .stop = igmp6_mcf_seq_stop,
3018 .show = igmp6_mcf_seq_show,
3021 static int __net_init igmp6_proc_init(struct net *net)
3026 if (!proc_create_net("igmp6", 0444, net->proc_net, &igmp6_mc_seq_ops,
3027 sizeof(struct igmp6_mc_iter_state)))
3029 if (!proc_create_net("mcfilter6", 0444, net->proc_net,
3031 sizeof(struct igmp6_mcf_iter_state)))
3032 goto out_proc_net_igmp6;
3039 remove_proc_entry("igmp6", net->proc_net);
3043 static void __net_exit igmp6_proc_exit(struct net *net)
3045 remove_proc_entry("mcfilter6", net->proc_net);
3046 remove_proc_entry("igmp6", net->proc_net);
3049 static inline int igmp6_proc_init(struct net *net)
3053 static inline void igmp6_proc_exit(struct net *net)
3058 static int __net_init igmp6_net_init(struct net *net)
3062 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
3063 SOCK_RAW, IPPROTO_ICMPV6, net);
3065 pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
3070 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
3071 net->ipv6.igmp_sk->sk_allocation = GFP_KERNEL;
3073 err = inet_ctl_sock_create(&net->ipv6.mc_autojoin_sk, PF_INET6,
3074 SOCK_RAW, IPPROTO_ICMPV6, net);
3076 pr_err("Failed to initialize the IGMP6 autojoin socket (err %d)\n",
3078 goto out_sock_create;
3081 err = igmp6_proc_init(net);
3083 goto out_sock_create_autojoin;
3087 out_sock_create_autojoin:
3088 inet_ctl_sock_destroy(net->ipv6.mc_autojoin_sk);
3090 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
3095 static void __net_exit igmp6_net_exit(struct net *net)
3097 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
3098 inet_ctl_sock_destroy(net->ipv6.mc_autojoin_sk);
3099 igmp6_proc_exit(net);
3102 static struct pernet_operations igmp6_net_ops = {
3103 .init = igmp6_net_init,
3104 .exit = igmp6_net_exit,
3107 int __init igmp6_init(void)
3111 err = register_pernet_subsys(&igmp6_net_ops);
3115 mld_wq = create_workqueue("mld");
3117 unregister_pernet_subsys(&igmp6_net_ops);
3124 int __init igmp6_late_init(void)
3126 return register_netdevice_notifier(&igmp6_netdev_notifier);
3129 void igmp6_cleanup(void)
3131 unregister_pernet_subsys(&igmp6_net_ops);
3132 destroy_workqueue(mld_wq);
3135 void igmp6_late_cleanup(void)
3137 unregister_netdevice_notifier(&igmp6_netdev_notifier);