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_ATOMIC);
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 spin_lock_bh(&mc->mca_lock);
663 if (!(mc->mca_flags&MAF_LOADED)) {
664 mc->mca_flags |= MAF_LOADED;
665 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
666 dev_mc_add(dev, buf);
668 spin_unlock_bh(&mc->mca_lock);
670 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
673 if (mld_in_v1_mode(mc->idev)) {
674 igmp6_join_group(mc);
679 /* Based on RFC3810 6.1, for newly added INCLUDE SSM, we
680 * should not send filter-mode change record as the mode
681 * should be from IN() to IN(A).
683 if (mc->mca_sfmode == MCAST_EXCLUDE)
684 mc->mca_crcount = mc->idev->mc_qrv;
686 mld_ifc_event(mc->idev);
689 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
691 struct net_device *dev = mc->idev->dev;
692 char buf[MAX_ADDR_LEN];
694 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
695 IPV6_ADDR_SCOPE_LINKLOCAL)
698 spin_lock_bh(&mc->mca_lock);
699 if (mc->mca_flags&MAF_LOADED) {
700 mc->mca_flags &= ~MAF_LOADED;
701 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
702 dev_mc_del(dev, buf);
705 spin_unlock_bh(&mc->mca_lock);
706 if (mc->mca_flags & MAF_NOREPORT)
710 igmp6_leave_group(mc);
712 spin_lock_bh(&mc->mca_lock);
713 if (cancel_delayed_work(&mc->mca_work))
714 refcount_dec(&mc->mca_refcnt);
715 spin_unlock_bh(&mc->mca_lock);
719 * deleted ifmcaddr6 manipulation
721 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
723 struct ifmcaddr6 *pmc;
725 /* this is an "ifmcaddr6" for convenience; only the fields below
726 * are actually used. In particular, the refcnt and users are not
727 * used for management of the delete list. Using the same structure
728 * for deleted items allows change reports to use common code with
729 * non-deleted or query-response MCA's.
731 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
735 spin_lock_bh(&im->mca_lock);
736 spin_lock_init(&pmc->mca_lock);
737 pmc->idev = im->idev;
739 pmc->mca_addr = im->mca_addr;
740 pmc->mca_crcount = idev->mc_qrv;
741 pmc->mca_sfmode = im->mca_sfmode;
742 if (pmc->mca_sfmode == MCAST_INCLUDE) {
743 struct ip6_sf_list *psf;
745 rcu_assign_pointer(pmc->mca_tomb,
746 rtnl_dereference(im->mca_tomb));
747 rcu_assign_pointer(pmc->mca_sources,
748 rtnl_dereference(im->mca_sources));
749 RCU_INIT_POINTER(im->mca_tomb, NULL);
750 RCU_INIT_POINTER(im->mca_sources, NULL);
752 for_each_psf_rtnl(pmc, psf)
753 psf->sf_crcount = pmc->mca_crcount;
755 spin_unlock_bh(&im->mca_lock);
757 rcu_assign_pointer(pmc->next, idev->mc_tomb);
758 rcu_assign_pointer(idev->mc_tomb, pmc);
761 static void mld_del_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
763 struct ip6_sf_list *psf, *sources, *tomb;
764 struct in6_addr *pmca = &im->mca_addr;
765 struct ifmcaddr6 *pmc, *pmc_prev;
768 for_each_mc_tomb(idev, pmc) {
769 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
775 rcu_assign_pointer(pmc_prev->next, pmc->next);
777 rcu_assign_pointer(idev->mc_tomb, pmc->next);
780 spin_lock_bh(&im->mca_lock);
782 im->idev = pmc->idev;
783 if (im->mca_sfmode == MCAST_INCLUDE) {
784 tomb = rcu_replace_pointer(im->mca_tomb,
785 rtnl_dereference(pmc->mca_tomb),
786 lockdep_rtnl_is_held());
787 rcu_assign_pointer(pmc->mca_tomb, tomb);
789 sources = rcu_replace_pointer(im->mca_sources,
790 rtnl_dereference(pmc->mca_sources),
791 lockdep_rtnl_is_held());
792 rcu_assign_pointer(pmc->mca_sources, sources);
793 for_each_psf_rtnl(im, psf)
794 psf->sf_crcount = idev->mc_qrv;
796 im->mca_crcount = idev->mc_qrv;
798 in6_dev_put(pmc->idev);
799 ip6_mc_clear_src(pmc);
802 spin_unlock_bh(&im->mca_lock);
805 static void mld_clear_delrec(struct inet6_dev *idev)
807 struct ifmcaddr6 *pmc, *nextpmc;
809 pmc = rtnl_dereference(idev->mc_tomb);
810 RCU_INIT_POINTER(idev->mc_tomb, NULL);
812 for (; pmc; pmc = nextpmc) {
813 nextpmc = rtnl_dereference(pmc->next);
814 ip6_mc_clear_src(pmc);
815 in6_dev_put(pmc->idev);
819 /* clear dead sources, too */
820 for_each_mc_rtnl(idev, pmc) {
821 struct ip6_sf_list *psf, *psf_next;
823 spin_lock_bh(&pmc->mca_lock);
824 psf = rtnl_dereference(pmc->mca_tomb);
825 RCU_INIT_POINTER(pmc->mca_tomb, NULL);
826 spin_unlock_bh(&pmc->mca_lock);
827 for (; psf; psf = psf_next) {
828 psf_next = rtnl_dereference(psf->sf_next);
834 static void mca_get(struct ifmcaddr6 *mc)
836 refcount_inc(&mc->mca_refcnt);
839 static void ma_put(struct ifmcaddr6 *mc)
841 if (refcount_dec_and_test(&mc->mca_refcnt)) {
842 in6_dev_put(mc->idev);
847 static struct ifmcaddr6 *mca_alloc(struct inet6_dev *idev,
848 const struct in6_addr *addr,
851 struct ifmcaddr6 *mc;
853 mc = kzalloc(sizeof(*mc), GFP_ATOMIC);
857 INIT_DELAYED_WORK(&mc->mca_work, mld_mca_work);
859 mc->mca_addr = *addr;
860 mc->idev = idev; /* reference taken by caller */
862 /* mca_stamp should be updated upon changes */
863 mc->mca_cstamp = mc->mca_tstamp = jiffies;
864 refcount_set(&mc->mca_refcnt, 1);
865 spin_lock_init(&mc->mca_lock);
867 mc->mca_sfmode = mode;
868 mc->mca_sfcount[mode] = 1;
870 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
871 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
872 mc->mca_flags |= MAF_NOREPORT;
878 * device multicast group inc (add if not found)
880 static int __ipv6_dev_mc_inc(struct net_device *dev,
881 const struct in6_addr *addr, unsigned int mode)
883 struct ifmcaddr6 *mc;
884 struct inet6_dev *idev;
888 /* we need to take a reference on idev */
889 idev = in6_dev_get(dev);
899 for_each_mc_rtnl(idev, mc) {
900 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
902 ip6_mc_add_src(idev, &mc->mca_addr, mode, 0, NULL, 0);
908 mc = mca_alloc(idev, addr, mode);
914 rcu_assign_pointer(mc->next, idev->mc_list);
915 rcu_assign_pointer(idev->mc_list, mc);
919 mld_del_delrec(idev, mc);
920 igmp6_group_added(mc);
925 int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
927 return __ipv6_dev_mc_inc(dev, addr, MCAST_EXCLUDE);
929 EXPORT_SYMBOL(ipv6_dev_mc_inc);
932 * device multicast group del
934 int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
936 struct ifmcaddr6 *ma, __rcu **map;
940 for (map = &idev->mc_list;
941 (ma = rtnl_dereference(*map));
943 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
944 if (--ma->mca_users == 0) {
947 igmp6_group_dropped(ma);
948 ip6_mc_clear_src(ma);
960 int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
962 struct inet6_dev *idev;
967 idev = __in6_dev_get(dev);
971 err = __ipv6_dev_mc_dec(idev, addr);
975 EXPORT_SYMBOL(ipv6_dev_mc_dec);
978 * check if the interface/address pair is valid
980 bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
981 const struct in6_addr *src_addr)
983 struct inet6_dev *idev;
984 struct ifmcaddr6 *mc;
988 idev = __in6_dev_get(dev);
990 for_each_mc_rcu(idev, mc) {
991 if (ipv6_addr_equal(&mc->mca_addr, group))
995 if (src_addr && !ipv6_addr_any(src_addr)) {
996 struct ip6_sf_list *psf;
998 spin_lock_bh(&mc->mca_lock);
999 for_each_psf_rcu(mc, psf) {
1000 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1004 rv = psf->sf_count[MCAST_INCLUDE] ||
1005 psf->sf_count[MCAST_EXCLUDE] !=
1006 mc->mca_sfcount[MCAST_EXCLUDE];
1008 rv = mc->mca_sfcount[MCAST_EXCLUDE] != 0;
1009 spin_unlock_bh(&mc->mca_lock);
1011 rv = true; /* don't filter unspecified source */
1018 static void mld_gq_start_work(struct inet6_dev *idev)
1020 unsigned long tv = prandom_u32() % idev->mc_maxdelay;
1022 idev->mc_gq_running = 1;
1023 if (!mod_delayed_work(mld_wq, &idev->mc_gq_work, tv + 2))
1027 static void mld_gq_stop_work(struct inet6_dev *idev)
1029 idev->mc_gq_running = 0;
1030 if (cancel_delayed_work(&idev->mc_gq_work))
1031 __in6_dev_put(idev);
1034 static void mld_ifc_start_work(struct inet6_dev *idev, unsigned long delay)
1036 unsigned long tv = prandom_u32() % delay;
1038 if (!mod_delayed_work(mld_wq, &idev->mc_ifc_work, tv + 2))
1042 static void mld_ifc_stop_work(struct inet6_dev *idev)
1044 idev->mc_ifc_count = 0;
1045 if (cancel_delayed_work(&idev->mc_ifc_work))
1046 __in6_dev_put(idev);
1049 static void mld_dad_start_work(struct inet6_dev *idev, unsigned long delay)
1051 unsigned long tv = prandom_u32() % delay;
1053 if (!mod_delayed_work(mld_wq, &idev->mc_dad_work, tv + 2))
1057 static void mld_dad_stop_work(struct inet6_dev *idev)
1059 if (cancel_delayed_work(&idev->mc_dad_work))
1060 __in6_dev_put(idev);
1064 * IGMP handling (alias multicast ICMPv6 messages)
1066 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1068 unsigned long delay = resptime;
1070 /* Do not start work for these addresses */
1071 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1072 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1075 if (cancel_delayed_work(&ma->mca_work)) {
1076 refcount_dec(&ma->mca_refcnt);
1077 delay = ma->mca_work.timer.expires - jiffies;
1080 if (delay >= resptime)
1081 delay = prandom_u32() % resptime;
1083 if (!mod_delayed_work(mld_wq, &ma->mca_work, delay))
1084 refcount_inc(&ma->mca_refcnt);
1085 ma->mca_flags |= MAF_TIMER_RUNNING;
1088 /* mark EXCLUDE-mode sources */
1089 static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1090 const struct in6_addr *srcs)
1092 struct ip6_sf_list *psf;
1096 for_each_psf_rcu(pmc, psf) {
1097 if (scount == nsrcs)
1099 for (i = 0; i < nsrcs; i++) {
1100 /* skip inactive filters */
1101 if (psf->sf_count[MCAST_INCLUDE] ||
1102 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1103 psf->sf_count[MCAST_EXCLUDE])
1105 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1111 pmc->mca_flags &= ~MAF_GSQUERY;
1112 if (scount == nsrcs) /* all sources excluded */
1117 static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1118 const struct in6_addr *srcs)
1120 struct ip6_sf_list *psf;
1123 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1124 return mld_xmarksources(pmc, nsrcs, srcs);
1126 /* mark INCLUDE-mode sources */
1129 for_each_psf_rcu(pmc, psf) {
1130 if (scount == nsrcs)
1132 for (i = 0; i < nsrcs; i++) {
1133 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1141 pmc->mca_flags &= ~MAF_GSQUERY;
1144 pmc->mca_flags |= MAF_GSQUERY;
1148 static int mld_force_mld_version(const struct inet6_dev *idev)
1150 /* Normally, both are 0 here. If enforcement to a particular is
1151 * being used, individual device enforcement will have a lower
1152 * precedence over 'all' device (.../conf/all/force_mld_version).
1155 if (dev_net(idev->dev)->ipv6.devconf_all->force_mld_version != 0)
1156 return dev_net(idev->dev)->ipv6.devconf_all->force_mld_version;
1158 return idev->cnf.force_mld_version;
1161 static bool mld_in_v2_mode_only(const struct inet6_dev *idev)
1163 return mld_force_mld_version(idev) == 2;
1166 static bool mld_in_v1_mode_only(const struct inet6_dev *idev)
1168 return mld_force_mld_version(idev) == 1;
1171 static bool mld_in_v1_mode(const struct inet6_dev *idev)
1173 if (mld_in_v2_mode_only(idev))
1175 if (mld_in_v1_mode_only(idev))
1177 if (idev->mc_v1_seen && time_before(jiffies, idev->mc_v1_seen))
1183 static void mld_set_v1_mode(struct inet6_dev *idev)
1185 /* RFC3810, relevant sections:
1186 * - 9.1. Robustness Variable
1187 * - 9.2. Query Interval
1188 * - 9.3. Query Response Interval
1189 * - 9.12. Older Version Querier Present Timeout
1191 unsigned long switchback;
1193 switchback = (idev->mc_qrv * idev->mc_qi) + idev->mc_qri;
1195 idev->mc_v1_seen = jiffies + switchback;
1198 static void mld_update_qrv(struct inet6_dev *idev,
1199 const struct mld2_query *mlh2)
1201 /* RFC3810, relevant sections:
1202 * - 5.1.8. QRV (Querier's Robustness Variable)
1203 * - 9.1. Robustness Variable
1206 /* The value of the Robustness Variable MUST NOT be zero,
1207 * and SHOULD NOT be one. Catch this here if we ever run
1208 * into such a case in future.
1210 const int min_qrv = min(MLD_QRV_DEFAULT, sysctl_mld_qrv);
1211 WARN_ON(idev->mc_qrv == 0);
1213 if (mlh2->mld2q_qrv > 0)
1214 idev->mc_qrv = mlh2->mld2q_qrv;
1216 if (unlikely(idev->mc_qrv < min_qrv)) {
1217 net_warn_ratelimited("IPv6: MLD: clamping QRV from %u to %u!\n",
1218 idev->mc_qrv, min_qrv);
1219 idev->mc_qrv = min_qrv;
1223 static void mld_update_qi(struct inet6_dev *idev,
1224 const struct mld2_query *mlh2)
1226 /* RFC3810, relevant sections:
1227 * - 5.1.9. QQIC (Querier's Query Interval Code)
1228 * - 9.2. Query Interval
1229 * - 9.12. Older Version Querier Present Timeout
1230 * (the [Query Interval] in the last Query received)
1232 unsigned long mc_qqi;
1234 if (mlh2->mld2q_qqic < 128) {
1235 mc_qqi = mlh2->mld2q_qqic;
1237 unsigned long mc_man, mc_exp;
1239 mc_exp = MLDV2_QQIC_EXP(mlh2->mld2q_qqic);
1240 mc_man = MLDV2_QQIC_MAN(mlh2->mld2q_qqic);
1242 mc_qqi = (mc_man | 0x10) << (mc_exp + 3);
1245 idev->mc_qi = mc_qqi * HZ;
1248 static void mld_update_qri(struct inet6_dev *idev,
1249 const struct mld2_query *mlh2)
1251 /* RFC3810, relevant sections:
1252 * - 5.1.3. Maximum Response Code
1253 * - 9.3. Query Response Interval
1255 idev->mc_qri = msecs_to_jiffies(mldv2_mrc(mlh2));
1258 static int mld_process_v1(struct inet6_dev *idev, struct mld_msg *mld,
1259 unsigned long *max_delay, bool v1_query)
1261 unsigned long mldv1_md;
1263 /* Ignore v1 queries */
1264 if (mld_in_v2_mode_only(idev))
1267 mldv1_md = ntohs(mld->mld_maxdelay);
1269 /* When in MLDv1 fallback and a MLDv2 router start-up being
1270 * unaware of current MLDv1 operation, the MRC == MRD mapping
1271 * only works when the exponential algorithm is not being
1272 * used (as MLDv1 is unaware of such things).
1274 * According to the RFC author, the MLDv2 implementations
1275 * he's aware of all use a MRC < 32768 on start up queries.
1277 * Thus, should we *ever* encounter something else larger
1278 * than that, just assume the maximum possible within our
1282 mldv1_md = min(mldv1_md, MLDV1_MRD_MAX_COMPAT);
1284 *max_delay = max(msecs_to_jiffies(mldv1_md), 1UL);
1286 /* MLDv1 router present: we need to go into v1 mode *only*
1287 * when an MLDv1 query is received as per section 9.12. of
1288 * RFC3810! And we know from RFC2710 section 3.7 that MLDv1
1289 * queries MUST be of exactly 24 octets.
1292 mld_set_v1_mode(idev);
1294 /* cancel MLDv2 report work */
1295 mld_gq_stop_work(idev);
1296 /* cancel the interface change work */
1297 mld_ifc_stop_work(idev);
1298 /* clear deleted report items */
1299 mld_clear_delrec(idev);
1304 static int mld_process_v2(struct inet6_dev *idev, struct mld2_query *mld,
1305 unsigned long *max_delay)
1307 *max_delay = max(msecs_to_jiffies(mldv2_mrc(mld)), 1UL);
1309 mld_update_qrv(idev, mld);
1310 mld_update_qi(idev, mld);
1311 mld_update_qri(idev, mld);
1313 idev->mc_maxdelay = *max_delay;
1318 /* called with rcu_read_lock() */
1319 int igmp6_event_query(struct sk_buff *skb)
1321 struct mld2_query *mlh2 = NULL;
1322 struct ifmcaddr6 *ma;
1323 const struct in6_addr *group;
1324 unsigned long max_delay;
1325 struct inet6_dev *idev;
1326 struct mld_msg *mld;
1331 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1334 /* compute payload length excluding extension headers */
1335 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
1336 len -= skb_network_header_len(skb);
1339 * Upon reception of an MLD message that contains a Query, the node
1340 * checks if the source address of the message is a valid link-local
1341 * address, if the Hop Limit is set to 1, and if the Router Alert
1342 * option is present in the Hop-By-Hop Options header of the IPv6
1343 * packet. If any of these checks fails, the packet is dropped.
1345 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL) ||
1346 ipv6_hdr(skb)->hop_limit != 1 ||
1347 !(IP6CB(skb)->flags & IP6SKB_ROUTERALERT) ||
1348 IP6CB(skb)->ra != htons(IPV6_OPT_ROUTERALERT_MLD))
1351 idev = __in6_dev_get(skb->dev);
1355 mld = (struct mld_msg *)icmp6_hdr(skb);
1356 group = &mld->mld_mca;
1357 group_type = ipv6_addr_type(group);
1359 if (group_type != IPV6_ADDR_ANY &&
1360 !(group_type&IPV6_ADDR_MULTICAST))
1363 if (len < MLD_V1_QUERY_LEN) {
1365 } else if (len == MLD_V1_QUERY_LEN || mld_in_v1_mode(idev)) {
1366 err = mld_process_v1(idev, mld, &max_delay,
1367 len == MLD_V1_QUERY_LEN);
1370 } else if (len >= MLD_V2_QUERY_LEN_MIN) {
1371 int srcs_offset = sizeof(struct mld2_query) -
1372 sizeof(struct icmp6hdr);
1374 if (!pskb_may_pull(skb, srcs_offset))
1377 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1379 err = mld_process_v2(idev, mlh2, &max_delay);
1383 if (group_type == IPV6_ADDR_ANY) { /* general query */
1384 if (mlh2->mld2q_nsrcs)
1385 return -EINVAL; /* no sources allowed */
1387 mld_gq_start_work(idev);
1390 /* mark sources to include, if group & source-specific */
1391 if (mlh2->mld2q_nsrcs != 0) {
1392 if (!pskb_may_pull(skb, srcs_offset +
1393 ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
1396 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1403 if (group_type == IPV6_ADDR_ANY) {
1404 for_each_mc_rcu(idev, ma) {
1405 spin_lock_bh(&ma->mca_lock);
1406 igmp6_group_queried(ma, max_delay);
1407 spin_unlock_bh(&ma->mca_lock);
1410 for_each_mc_rcu(idev, ma) {
1411 if (!ipv6_addr_equal(group, &ma->mca_addr))
1413 spin_lock_bh(&ma->mca_lock);
1414 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1415 /* gsquery <- gsquery && mark */
1417 ma->mca_flags &= ~MAF_GSQUERY;
1419 /* gsquery <- mark */
1421 ma->mca_flags |= MAF_GSQUERY;
1423 ma->mca_flags &= ~MAF_GSQUERY;
1425 if (!(ma->mca_flags & MAF_GSQUERY) ||
1426 mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
1427 igmp6_group_queried(ma, max_delay);
1428 spin_unlock_bh(&ma->mca_lock);
1436 /* called with rcu_read_lock() */
1437 int igmp6_event_report(struct sk_buff *skb)
1439 struct ifmcaddr6 *ma;
1440 struct inet6_dev *idev;
1441 struct mld_msg *mld;
1444 /* Our own report looped back. Ignore it. */
1445 if (skb->pkt_type == PACKET_LOOPBACK)
1448 /* send our report if the MC router may not have heard this report */
1449 if (skb->pkt_type != PACKET_MULTICAST &&
1450 skb->pkt_type != PACKET_BROADCAST)
1453 if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
1456 mld = (struct mld_msg *)icmp6_hdr(skb);
1458 /* Drop reports with not link local source */
1459 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
1460 if (addr_type != IPV6_ADDR_ANY &&
1461 !(addr_type&IPV6_ADDR_LINKLOCAL))
1464 idev = __in6_dev_get(skb->dev);
1469 * Cancel the work for this group
1472 for_each_mc_rcu(idev, ma) {
1473 if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
1474 spin_lock(&ma->mca_lock);
1475 if (cancel_delayed_work(&ma->mca_work))
1476 refcount_dec(&ma->mca_refcnt);
1477 ma->mca_flags &= ~(MAF_LAST_REPORTER |
1479 spin_unlock(&ma->mca_lock);
1486 static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1487 int gdeleted, int sdeleted)
1490 case MLD2_MODE_IS_INCLUDE:
1491 case MLD2_MODE_IS_EXCLUDE:
1492 if (gdeleted || sdeleted)
1494 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1495 if (pmc->mca_sfmode == MCAST_INCLUDE)
1497 /* don't include if this source is excluded
1500 if (psf->sf_count[MCAST_INCLUDE])
1501 return type == MLD2_MODE_IS_INCLUDE;
1502 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1503 psf->sf_count[MCAST_EXCLUDE];
1506 case MLD2_CHANGE_TO_INCLUDE:
1507 if (gdeleted || sdeleted)
1509 return psf->sf_count[MCAST_INCLUDE] != 0;
1510 case MLD2_CHANGE_TO_EXCLUDE:
1511 if (gdeleted || sdeleted)
1513 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1514 psf->sf_count[MCAST_INCLUDE])
1516 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1517 psf->sf_count[MCAST_EXCLUDE];
1518 case MLD2_ALLOW_NEW_SOURCES:
1519 if (gdeleted || !psf->sf_crcount)
1521 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1522 case MLD2_BLOCK_OLD_SOURCES:
1523 if (pmc->mca_sfmode == MCAST_INCLUDE)
1524 return gdeleted || (psf->sf_crcount && sdeleted);
1525 return psf->sf_crcount && !gdeleted && !sdeleted;
1531 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1533 struct ip6_sf_list *psf;
1536 for_each_psf_rtnl(pmc, psf) {
1537 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1544 static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
1545 struct net_device *dev,
1546 const struct in6_addr *saddr,
1547 const struct in6_addr *daddr,
1550 struct ipv6hdr *hdr;
1552 skb->protocol = htons(ETH_P_IPV6);
1555 skb_reset_network_header(skb);
1556 skb_put(skb, sizeof(struct ipv6hdr));
1557 hdr = ipv6_hdr(skb);
1559 ip6_flow_hdr(hdr, 0, 0);
1561 hdr->payload_len = htons(len);
1562 hdr->nexthdr = proto;
1563 hdr->hop_limit = inet6_sk(sk)->hop_limit;
1565 hdr->saddr = *saddr;
1566 hdr->daddr = *daddr;
1569 static struct sk_buff *mld_newpack(struct inet6_dev *idev, unsigned int mtu)
1571 struct net_device *dev = idev->dev;
1572 struct net *net = dev_net(dev);
1573 struct sock *sk = net->ipv6.igmp_sk;
1574 struct sk_buff *skb;
1575 struct mld2_report *pmr;
1576 struct in6_addr addr_buf;
1577 const struct in6_addr *saddr;
1578 int hlen = LL_RESERVED_SPACE(dev);
1579 int tlen = dev->needed_tailroom;
1580 unsigned int size = mtu + hlen + tlen;
1582 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1583 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1586 /* we assume size > sizeof(ra) here */
1587 /* limit our allocations to order-0 page */
1588 size = min_t(int, size, SKB_MAX_ORDER(0, 0));
1589 skb = sock_alloc_send_skb(sk, size, 1, &err);
1594 skb->priority = TC_PRIO_CONTROL;
1595 skb_reserve(skb, hlen);
1596 skb_tailroom_reserve(skb, mtu, tlen);
1598 if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
1599 /* <draft-ietf-magma-mld-source-05.txt>:
1600 * use unspecified address as the source address
1601 * when a valid link-local address is not available.
1603 saddr = &in6addr_any;
1607 ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
1609 skb_put_data(skb, ra, sizeof(ra));
1611 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
1612 skb_put(skb, sizeof(*pmr));
1613 pmr = (struct mld2_report *)skb_transport_header(skb);
1614 pmr->mld2r_type = ICMPV6_MLD2_REPORT;
1615 pmr->mld2r_resv1 = 0;
1616 pmr->mld2r_cksum = 0;
1617 pmr->mld2r_resv2 = 0;
1618 pmr->mld2r_ngrec = 0;
1622 static void mld_sendpack(struct sk_buff *skb)
1624 struct ipv6hdr *pip6 = ipv6_hdr(skb);
1625 struct mld2_report *pmr =
1626 (struct mld2_report *)skb_transport_header(skb);
1627 int payload_len, mldlen;
1628 struct inet6_dev *idev;
1629 struct net *net = dev_net(skb->dev);
1632 struct dst_entry *dst;
1635 idev = __in6_dev_get(skb->dev);
1636 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1638 payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
1640 mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
1641 pip6->payload_len = htons(payload_len);
1643 pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1645 csum_partial(skb_transport_header(skb),
1648 icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
1649 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1651 dst = icmp6_dst_alloc(skb->dev, &fl6);
1658 skb_dst_set(skb, dst);
1662 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
1663 net, net->ipv6.igmp_sk, skb, NULL, skb->dev,
1667 ICMP6MSGOUT_INC_STATS(net, idev, ICMPV6_MLD2_REPORT);
1668 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
1670 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
1681 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1683 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1686 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1687 int type, struct mld2_grec **ppgr, unsigned int mtu)
1689 struct mld2_report *pmr;
1690 struct mld2_grec *pgr;
1693 skb = mld_newpack(pmc->idev, mtu);
1697 pgr = skb_put(skb, sizeof(struct mld2_grec));
1698 pgr->grec_type = type;
1699 pgr->grec_auxwords = 0;
1700 pgr->grec_nsrcs = 0;
1701 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1702 pmr = (struct mld2_report *)skb_transport_header(skb);
1703 pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
1708 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
1710 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1711 int type, int gdeleted, int sdeleted,
1714 struct ip6_sf_list *psf, *psf_prev, *psf_next;
1715 int scount, stotal, first, isquery, truncate;
1716 struct ip6_sf_list __rcu **psf_list;
1717 struct inet6_dev *idev = pmc->idev;
1718 struct net_device *dev = idev->dev;
1719 struct mld2_grec *pgr = NULL;
1720 struct mld2_report *pmr;
1723 if (pmc->mca_flags & MAF_NOREPORT)
1726 mtu = READ_ONCE(dev->mtu);
1727 if (mtu < IPV6_MIN_MTU)
1730 isquery = type == MLD2_MODE_IS_INCLUDE ||
1731 type == MLD2_MODE_IS_EXCLUDE;
1732 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1733 type == MLD2_CHANGE_TO_EXCLUDE;
1735 stotal = scount = 0;
1737 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1739 if (!rcu_access_pointer(*psf_list))
1742 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
1744 /* EX and TO_EX get a fresh packet, if needed */
1746 if (pmr && pmr->mld2r_ngrec &&
1747 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1750 skb = mld_newpack(idev, mtu);
1755 for (psf = rtnl_dereference(*psf_list);
1758 struct in6_addr *psrc;
1760 psf_next = rtnl_dereference(psf->sf_next);
1762 if (!is_in(pmc, psf, type, gdeleted, sdeleted) && !crsend) {
1767 /* Based on RFC3810 6.1. Should not send source-list change
1768 * records when there is a filter mode change.
1770 if (((gdeleted && pmc->mca_sfmode == MCAST_EXCLUDE) ||
1771 (!gdeleted && pmc->mca_crcount)) &&
1772 (type == MLD2_ALLOW_NEW_SOURCES ||
1773 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount)
1774 goto decrease_sf_crcount;
1776 /* clear marks on query responses */
1780 if (AVAILABLE(skb) < sizeof(*psrc) +
1781 first*sizeof(struct mld2_grec)) {
1782 if (truncate && !first)
1783 break; /* truncate these */
1785 pgr->grec_nsrcs = htons(scount);
1788 skb = mld_newpack(idev, mtu);
1793 skb = add_grhead(skb, pmc, type, &pgr, mtu);
1798 psrc = skb_put(skb, sizeof(*psrc));
1799 *psrc = psf->sf_addr;
1801 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1802 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1803 decrease_sf_crcount:
1805 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1807 rcu_assign_pointer(psf_prev->sf_next,
1808 rtnl_dereference(psf->sf_next));
1810 rcu_assign_pointer(*psf_list,
1811 rtnl_dereference(psf->sf_next));
1812 kfree_rcu(psf, rcu);
1821 if (type == MLD2_ALLOW_NEW_SOURCES ||
1822 type == MLD2_BLOCK_OLD_SOURCES)
1824 if (pmc->mca_crcount || isquery || crsend) {
1825 /* make sure we have room for group header */
1826 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1828 skb = NULL; /* add_grhead will get a new one */
1830 skb = add_grhead(skb, pmc, type, &pgr, mtu);
1834 pgr->grec_nsrcs = htons(scount);
1837 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1841 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1843 struct sk_buff *skb = NULL;
1847 for_each_mc_rtnl(idev, pmc) {
1848 if (pmc->mca_flags & MAF_NOREPORT)
1850 spin_lock_bh(&pmc->mca_lock);
1851 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1852 type = MLD2_MODE_IS_EXCLUDE;
1854 type = MLD2_MODE_IS_INCLUDE;
1855 skb = add_grec(skb, pmc, type, 0, 0, 0);
1856 spin_unlock_bh(&pmc->mca_lock);
1859 spin_lock_bh(&pmc->mca_lock);
1860 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1861 type = MLD2_MODE_IS_EXCLUDE;
1863 type = MLD2_MODE_IS_INCLUDE;
1864 skb = add_grec(skb, pmc, type, 0, 0, 0);
1865 spin_unlock_bh(&pmc->mca_lock);
1872 * remove zero-count source records from a source filter list
1874 static void mld_clear_zeros(struct ip6_sf_list __rcu **ppsf)
1876 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1879 for (psf = rtnl_dereference(*ppsf);
1882 psf_next = rtnl_dereference(psf->sf_next);
1883 if (psf->sf_crcount == 0) {
1885 rcu_assign_pointer(psf_prev->sf_next,
1886 rtnl_dereference(psf->sf_next));
1888 rcu_assign_pointer(*ppsf,
1889 rtnl_dereference(psf->sf_next));
1890 kfree_rcu(psf, rcu);
1897 static void mld_send_cr(struct inet6_dev *idev)
1899 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1900 struct sk_buff *skb = NULL;
1905 for (pmc = rtnl_dereference(idev->mc_tomb);
1908 pmc_next = rtnl_dereference(pmc->next);
1909 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1910 type = MLD2_BLOCK_OLD_SOURCES;
1911 dtype = MLD2_BLOCK_OLD_SOURCES;
1912 skb = add_grec(skb, pmc, type, 1, 0, 0);
1913 skb = add_grec(skb, pmc, dtype, 1, 1, 0);
1915 if (pmc->mca_crcount) {
1916 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1917 type = MLD2_CHANGE_TO_INCLUDE;
1918 skb = add_grec(skb, pmc, type, 1, 0, 0);
1921 if (pmc->mca_crcount == 0) {
1922 mld_clear_zeros(&pmc->mca_tomb);
1923 mld_clear_zeros(&pmc->mca_sources);
1926 if (pmc->mca_crcount == 0 &&
1927 !rcu_access_pointer(pmc->mca_tomb) &&
1928 !rcu_access_pointer(pmc->mca_sources)) {
1930 rcu_assign_pointer(pmc_prev->next, pmc_next);
1932 rcu_assign_pointer(idev->mc_tomb, pmc_next);
1933 in6_dev_put(pmc->idev);
1934 kfree_rcu(pmc, rcu);
1940 for_each_mc_rtnl(idev, pmc) {
1941 spin_lock_bh(&pmc->mca_lock);
1942 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1943 type = MLD2_BLOCK_OLD_SOURCES;
1944 dtype = MLD2_ALLOW_NEW_SOURCES;
1946 type = MLD2_ALLOW_NEW_SOURCES;
1947 dtype = MLD2_BLOCK_OLD_SOURCES;
1949 skb = add_grec(skb, pmc, type, 0, 0, 0);
1950 skb = add_grec(skb, pmc, dtype, 0, 1, 0); /* deleted sources */
1952 /* filter mode changes */
1953 if (pmc->mca_crcount) {
1954 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1955 type = MLD2_CHANGE_TO_EXCLUDE;
1957 type = MLD2_CHANGE_TO_INCLUDE;
1958 skb = add_grec(skb, pmc, type, 0, 0, 0);
1961 spin_unlock_bh(&pmc->mca_lock);
1965 (void) mld_sendpack(skb);
1968 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1970 struct net *net = dev_net(dev);
1971 struct sock *sk = net->ipv6.igmp_sk;
1972 struct inet6_dev *idev;
1973 struct sk_buff *skb;
1974 struct mld_msg *hdr;
1975 const struct in6_addr *snd_addr, *saddr;
1976 struct in6_addr addr_buf;
1977 int hlen = LL_RESERVED_SPACE(dev);
1978 int tlen = dev->needed_tailroom;
1979 int err, len, payload_len, full_len;
1980 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1981 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1984 struct dst_entry *dst;
1986 if (type == ICMPV6_MGM_REDUCTION)
1987 snd_addr = &in6addr_linklocal_allrouters;
1991 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1992 payload_len = len + sizeof(ra);
1993 full_len = sizeof(struct ipv6hdr) + payload_len;
1996 IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
1997 IPSTATS_MIB_OUT, full_len);
2000 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
2004 IP6_INC_STATS(net, __in6_dev_get(dev),
2005 IPSTATS_MIB_OUTDISCARDS);
2009 skb->priority = TC_PRIO_CONTROL;
2010 skb_reserve(skb, hlen);
2012 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
2013 /* <draft-ietf-magma-mld-source-05.txt>:
2014 * use unspecified address as the source address
2015 * when a valid link-local address is not available.
2017 saddr = &in6addr_any;
2021 ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
2023 skb_put_data(skb, ra, sizeof(ra));
2025 hdr = skb_put_zero(skb, sizeof(struct mld_msg));
2026 hdr->mld_type = type;
2027 hdr->mld_mca = *addr;
2029 hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
2031 csum_partial(hdr, len, 0));
2034 idev = __in6_dev_get(skb->dev);
2036 icmpv6_flow_init(sk, &fl6, type,
2037 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
2039 dst = icmp6_dst_alloc(skb->dev, &fl6);
2045 skb_dst_set(skb, dst);
2046 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
2047 net, sk, skb, NULL, skb->dev,
2051 ICMP6MSGOUT_INC_STATS(net, idev, type);
2052 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
2054 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
2064 static void mld_send_initial_cr(struct inet6_dev *idev)
2066 struct sk_buff *skb;
2067 struct ifmcaddr6 *pmc;
2070 if (mld_in_v1_mode(idev))
2074 for_each_mc_rtnl(idev, pmc) {
2075 spin_lock_bh(&pmc->mca_lock);
2076 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2077 type = MLD2_CHANGE_TO_EXCLUDE;
2079 type = MLD2_ALLOW_NEW_SOURCES;
2080 skb = add_grec(skb, pmc, type, 0, 0, 1);
2081 spin_unlock_bh(&pmc->mca_lock);
2087 void ipv6_mc_dad_complete(struct inet6_dev *idev)
2089 idev->mc_dad_count = idev->mc_qrv;
2090 if (idev->mc_dad_count) {
2091 mld_send_initial_cr(idev);
2092 idev->mc_dad_count--;
2093 if (idev->mc_dad_count)
2094 mld_dad_start_work(idev,
2095 unsolicited_report_interval(idev));
2099 static void mld_dad_work(struct work_struct *work)
2101 struct inet6_dev *idev = container_of(to_delayed_work(work),
2106 mld_send_initial_cr(idev);
2108 if (idev->mc_dad_count) {
2109 idev->mc_dad_count--;
2110 if (idev->mc_dad_count)
2111 mld_dad_start_work(idev,
2112 unsolicited_report_interval(idev));
2117 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
2118 const struct in6_addr *psfsrc)
2120 struct ip6_sf_list *psf, *psf_prev;
2124 for_each_psf_rtnl(pmc, psf) {
2125 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2129 if (!psf || psf->sf_count[sfmode] == 0) {
2130 /* source filter not found, or count wrong => bug */
2133 psf->sf_count[sfmode]--;
2134 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
2135 struct inet6_dev *idev = pmc->idev;
2137 /* no more filters for this source */
2139 rcu_assign_pointer(psf_prev->sf_next,
2140 rtnl_dereference(psf->sf_next));
2142 rcu_assign_pointer(pmc->mca_sources,
2143 rtnl_dereference(psf->sf_next));
2145 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
2146 !mld_in_v1_mode(idev)) {
2147 psf->sf_crcount = idev->mc_qrv;
2148 rcu_assign_pointer(psf->sf_next,
2149 rtnl_dereference(pmc->mca_tomb));
2150 rcu_assign_pointer(pmc->mca_tomb, psf);
2153 kfree_rcu(psf, rcu);
2159 static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2160 int sfmode, int sfcount, const struct in6_addr *psfsrc,
2163 struct ifmcaddr6 *pmc;
2170 for_each_mc_rtnl(idev, pmc) {
2171 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2176 spin_lock_bh(&pmc->mca_lock);
2180 if (!pmc->mca_sfcount[sfmode]) {
2181 spin_unlock_bh(&pmc->mca_lock);
2185 pmc->mca_sfcount[sfmode]--;
2188 for (i = 0; i < sfcount; i++) {
2189 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2191 changerec |= rv > 0;
2195 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
2196 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
2197 pmc->mca_sfcount[MCAST_INCLUDE]) {
2198 struct ip6_sf_list *psf;
2200 /* filter mode change */
2201 pmc->mca_sfmode = MCAST_INCLUDE;
2202 pmc->mca_crcount = idev->mc_qrv;
2203 idev->mc_ifc_count = pmc->mca_crcount;
2204 for_each_psf_rtnl(pmc, psf)
2205 psf->sf_crcount = 0;
2206 mld_ifc_event(pmc->idev);
2207 } else if (sf_setstate(pmc) || changerec)
2208 mld_ifc_event(pmc->idev);
2209 spin_unlock_bh(&pmc->mca_lock);
2214 * Add multicast single-source filter to the interface list
2216 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
2217 const struct in6_addr *psfsrc)
2219 struct ip6_sf_list *psf, *psf_prev;
2222 for_each_psf_rtnl(pmc, psf) {
2223 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2228 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
2232 psf->sf_addr = *psfsrc;
2234 rcu_assign_pointer(psf_prev->sf_next, psf);
2236 rcu_assign_pointer(pmc->mca_sources, psf);
2239 psf->sf_count[sfmode]++;
2243 static void sf_markstate(struct ifmcaddr6 *pmc)
2245 struct ip6_sf_list *psf;
2246 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2248 for_each_psf_rtnl(pmc, psf) {
2249 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2250 psf->sf_oldin = mca_xcount ==
2251 psf->sf_count[MCAST_EXCLUDE] &&
2252 !psf->sf_count[MCAST_INCLUDE];
2254 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2259 static int sf_setstate(struct ifmcaddr6 *pmc)
2261 struct ip6_sf_list *psf, *dpsf;
2262 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2263 int qrv = pmc->idev->mc_qrv;
2267 for_each_psf_rtnl(pmc, psf) {
2268 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2269 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2270 !psf->sf_count[MCAST_INCLUDE];
2272 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2274 if (!psf->sf_oldin) {
2275 struct ip6_sf_list *prev = NULL;
2277 for_each_psf_tomb(pmc, dpsf) {
2278 if (ipv6_addr_equal(&dpsf->sf_addr,
2285 rcu_assign_pointer(prev->sf_next,
2286 rtnl_dereference(dpsf->sf_next));
2288 rcu_assign_pointer(pmc->mca_tomb,
2289 rtnl_dereference(dpsf->sf_next));
2290 kfree_rcu(dpsf, rcu);
2292 psf->sf_crcount = qrv;
2295 } else if (psf->sf_oldin) {
2296 psf->sf_crcount = 0;
2298 * add or update "delete" records if an active filter
2302 for_each_psf_tomb(pmc, dpsf)
2303 if (ipv6_addr_equal(&dpsf->sf_addr,
2307 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2311 rcu_assign_pointer(dpsf->sf_next,
2312 rtnl_dereference(pmc->mca_tomb));
2313 rcu_assign_pointer(pmc->mca_tomb, dpsf);
2315 dpsf->sf_crcount = qrv;
2323 * Add multicast source filter list to the interface list
2325 static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2326 int sfmode, int sfcount, const struct in6_addr *psfsrc,
2329 struct ifmcaddr6 *pmc;
2336 for_each_mc_rtnl(idev, pmc) {
2337 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2342 spin_lock_bh(&pmc->mca_lock);
2345 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2347 pmc->mca_sfcount[sfmode]++;
2349 for (i = 0; i < sfcount; i++) {
2350 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2358 pmc->mca_sfcount[sfmode]--;
2359 for (j = 0; j < i; j++)
2360 ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2361 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2362 struct ip6_sf_list *psf;
2364 /* filter mode change */
2365 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2366 pmc->mca_sfmode = MCAST_EXCLUDE;
2367 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2368 pmc->mca_sfmode = MCAST_INCLUDE;
2369 /* else no filters; keep old mode for reports */
2371 pmc->mca_crcount = idev->mc_qrv;
2372 idev->mc_ifc_count = pmc->mca_crcount;
2373 for_each_psf_rtnl(pmc, psf)
2374 psf->sf_crcount = 0;
2375 mld_ifc_event(idev);
2376 } else if (sf_setstate(pmc)) {
2377 mld_ifc_event(idev);
2379 spin_unlock_bh(&pmc->mca_lock);
2383 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2385 struct ip6_sf_list *psf, *nextpsf;
2387 for (psf = rtnl_dereference(pmc->mca_tomb);
2390 nextpsf = rtnl_dereference(psf->sf_next);
2391 kfree_rcu(psf, rcu);
2393 RCU_INIT_POINTER(pmc->mca_tomb, NULL);
2394 for (psf = rtnl_dereference(pmc->mca_sources);
2397 nextpsf = rtnl_dereference(psf->sf_next);
2398 kfree_rcu(psf, rcu);
2400 RCU_INIT_POINTER(pmc->mca_sources, NULL);
2401 pmc->mca_sfmode = MCAST_EXCLUDE;
2402 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2403 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2407 static void igmp6_join_group(struct ifmcaddr6 *ma)
2409 unsigned long delay;
2411 if (ma->mca_flags & MAF_NOREPORT)
2414 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2416 delay = prandom_u32() % unsolicited_report_interval(ma->idev);
2418 spin_lock_bh(&ma->mca_lock);
2419 if (cancel_delayed_work(&ma->mca_work)) {
2420 refcount_dec(&ma->mca_refcnt);
2421 delay = ma->mca_work.timer.expires - jiffies;
2424 if (!mod_delayed_work(mld_wq, &ma->mca_work, delay))
2425 refcount_inc(&ma->mca_refcnt);
2426 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2427 spin_unlock_bh(&ma->mca_lock);
2430 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2431 struct inet6_dev *idev)
2433 struct ip6_sf_socklist *psl;
2436 psl = rtnl_dereference(iml->sflist);
2439 /* any-source empty exclude case */
2440 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2442 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2443 psl->sl_count, psl->sl_addr, 0);
2444 RCU_INIT_POINTER(iml->sflist, NULL);
2445 atomic_sub(IP6_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2446 kfree_rcu(psl, rcu);
2451 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2453 if (mld_in_v1_mode(ma->idev)) {
2454 if (ma->mca_flags & MAF_LAST_REPORTER) {
2455 igmp6_send(&ma->mca_addr, ma->idev->dev,
2456 ICMPV6_MGM_REDUCTION);
2459 mld_add_delrec(ma->idev, ma);
2460 mld_ifc_event(ma->idev);
2464 static void mld_gq_work(struct work_struct *work)
2466 struct inet6_dev *idev = container_of(to_delayed_work(work),
2471 mld_send_report(idev, NULL);
2474 idev->mc_gq_running = 0;
2479 static void mld_ifc_work(struct work_struct *work)
2481 struct inet6_dev *idev = container_of(to_delayed_work(work),
2489 if (idev->mc_ifc_count) {
2490 idev->mc_ifc_count--;
2491 if (idev->mc_ifc_count)
2492 mld_ifc_start_work(idev,
2493 unsolicited_report_interval(idev));
2498 static void mld_ifc_event(struct inet6_dev *idev)
2500 if (mld_in_v1_mode(idev))
2503 idev->mc_ifc_count = idev->mc_qrv;
2504 mld_ifc_start_work(idev, 1);
2507 static void mld_mca_work(struct work_struct *work)
2509 struct ifmcaddr6 *ma = container_of(to_delayed_work(work),
2510 struct ifmcaddr6, mca_work);
2513 if (mld_in_v1_mode(ma->idev))
2514 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2516 mld_send_report(ma->idev, ma);
2519 spin_lock_bh(&ma->mca_lock);
2520 ma->mca_flags |= MAF_LAST_REPORTER;
2521 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2522 spin_unlock_bh(&ma->mca_lock);
2526 /* Device changing type */
2528 void ipv6_mc_unmap(struct inet6_dev *idev)
2530 struct ifmcaddr6 *i;
2532 /* Install multicast list, except for all-nodes (already installed) */
2534 for_each_mc_rtnl(idev, i)
2535 igmp6_group_dropped(i);
2538 void ipv6_mc_remap(struct inet6_dev *idev)
2543 /* Device going down */
2545 void ipv6_mc_down(struct inet6_dev *idev)
2547 struct ifmcaddr6 *i;
2549 /* Withdraw multicast list */
2550 for_each_mc_rtnl(idev, i)
2551 igmp6_group_dropped(i);
2553 /* Should stop work after group drop. or we will
2554 * start work again in mld_ifc_event()
2556 mld_ifc_stop_work(idev);
2557 mld_gq_stop_work(idev);
2558 mld_dad_stop_work(idev);
2561 static void ipv6_mc_reset(struct inet6_dev *idev)
2563 idev->mc_qrv = sysctl_mld_qrv;
2564 idev->mc_qi = MLD_QI_DEFAULT;
2565 idev->mc_qri = MLD_QRI_DEFAULT;
2566 idev->mc_v1_seen = 0;
2567 idev->mc_maxdelay = unsolicited_report_interval(idev);
2570 /* Device going up */
2572 void ipv6_mc_up(struct inet6_dev *idev)
2574 struct ifmcaddr6 *i;
2576 /* Install multicast list, except for all-nodes (already installed) */
2578 ipv6_mc_reset(idev);
2579 for_each_mc_rtnl(idev, i) {
2580 mld_del_delrec(idev, i);
2581 igmp6_group_added(i);
2585 /* IPv6 device initialization. */
2587 void ipv6_mc_init_dev(struct inet6_dev *idev)
2589 idev->mc_gq_running = 0;
2590 INIT_DELAYED_WORK(&idev->mc_gq_work, mld_gq_work);
2591 RCU_INIT_POINTER(idev->mc_tomb, NULL);
2592 idev->mc_ifc_count = 0;
2593 INIT_DELAYED_WORK(&idev->mc_ifc_work, mld_ifc_work);
2594 INIT_DELAYED_WORK(&idev->mc_dad_work, mld_dad_work);
2595 ipv6_mc_reset(idev);
2599 * Device is about to be destroyed: clean up.
2602 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2604 struct ifmcaddr6 *i;
2606 /* Deactivate works */
2608 mld_clear_delrec(idev);
2610 /* Delete all-nodes address. */
2611 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2612 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2615 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
2617 if (idev->cnf.forwarding)
2618 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
2620 while ((i = rtnl_dereference(idev->mc_list))) {
2621 rcu_assign_pointer(idev->mc_list, rtnl_dereference(i->next));
2623 ip6_mc_clear_src(i);
2628 static void ipv6_mc_rejoin_groups(struct inet6_dev *idev)
2630 struct ifmcaddr6 *pmc;
2634 if (mld_in_v1_mode(idev)) {
2635 for_each_mc_rtnl(idev, pmc)
2636 igmp6_join_group(pmc);
2638 mld_send_report(idev, NULL);
2642 static int ipv6_mc_netdev_event(struct notifier_block *this,
2643 unsigned long event,
2646 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2647 struct inet6_dev *idev = __in6_dev_get(dev);
2650 case NETDEV_RESEND_IGMP:
2652 ipv6_mc_rejoin_groups(idev);
2661 static struct notifier_block igmp6_netdev_notifier = {
2662 .notifier_call = ipv6_mc_netdev_event,
2665 #ifdef CONFIG_PROC_FS
2666 struct igmp6_mc_iter_state {
2667 struct seq_net_private p;
2668 struct net_device *dev;
2669 struct inet6_dev *idev;
2672 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2674 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2676 struct ifmcaddr6 *im = NULL;
2677 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2678 struct net *net = seq_file_net(seq);
2681 for_each_netdev_rcu(net, state->dev) {
2682 struct inet6_dev *idev;
2683 idev = __in6_dev_get(state->dev);
2687 im = rcu_dereference(idev->mc_list);
2696 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2698 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2700 im = rcu_dereference(im->next);
2702 state->dev = next_net_device_rcu(state->dev);
2707 state->idev = __in6_dev_get(state->dev);
2710 im = rcu_dereference(state->idev->mc_list);
2715 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2717 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2719 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2721 return pos ? NULL : im;
2724 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2728 return igmp6_mc_get_idx(seq, *pos);
2731 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2733 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2739 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2742 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2744 if (likely(state->idev))
2750 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2752 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2753 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2756 "%-4d %-15s %pi6 %5d %08X %ld\n",
2757 state->dev->ifindex, state->dev->name,
2759 im->mca_users, im->mca_flags,
2760 (im->mca_flags & MAF_TIMER_RUNNING) ?
2761 jiffies_to_clock_t(im->mca_work.timer.expires - jiffies) : 0);
2765 static const struct seq_operations igmp6_mc_seq_ops = {
2766 .start = igmp6_mc_seq_start,
2767 .next = igmp6_mc_seq_next,
2768 .stop = igmp6_mc_seq_stop,
2769 .show = igmp6_mc_seq_show,
2772 struct igmp6_mcf_iter_state {
2773 struct seq_net_private p;
2774 struct net_device *dev;
2775 struct inet6_dev *idev;
2776 struct ifmcaddr6 *im;
2779 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2781 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2783 struct ip6_sf_list *psf = NULL;
2784 struct ifmcaddr6 *im = NULL;
2785 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2786 struct net *net = seq_file_net(seq);
2790 for_each_netdev_rcu(net, state->dev) {
2791 struct inet6_dev *idev;
2792 idev = __in6_dev_get(state->dev);
2793 if (unlikely(idev == NULL))
2796 im = rcu_dereference(idev->mc_list);
2798 spin_lock_bh(&im->mca_lock);
2799 psf = rcu_dereference(im->mca_sources);
2805 spin_unlock_bh(&im->mca_lock);
2811 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2813 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2815 psf = rcu_dereference(psf->sf_next);
2817 spin_unlock_bh(&state->im->mca_lock);
2818 state->im = rcu_dereference(state->im->next);
2819 while (!state->im) {
2820 state->dev = next_net_device_rcu(state->dev);
2825 state->idev = __in6_dev_get(state->dev);
2828 state->im = rcu_dereference(state->idev->mc_list);
2832 spin_lock_bh(&state->im->mca_lock);
2833 psf = rcu_dereference(state->im->mca_sources);
2839 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2841 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2843 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2845 return pos ? NULL : psf;
2848 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2852 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2855 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2857 struct ip6_sf_list *psf;
2858 if (v == SEQ_START_TOKEN)
2859 psf = igmp6_mcf_get_first(seq);
2861 psf = igmp6_mcf_get_next(seq, v);
2866 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2869 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2871 if (likely(state->im)) {
2872 spin_unlock_bh(&state->im->mca_lock);
2875 if (likely(state->idev))
2882 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2884 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2885 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2887 if (v == SEQ_START_TOKEN) {
2888 seq_puts(seq, "Idx Device Multicast Address Source Address INC EXC\n");
2891 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
2892 state->dev->ifindex, state->dev->name,
2893 &state->im->mca_addr,
2895 psf->sf_count[MCAST_INCLUDE],
2896 psf->sf_count[MCAST_EXCLUDE]);
2901 static const struct seq_operations igmp6_mcf_seq_ops = {
2902 .start = igmp6_mcf_seq_start,
2903 .next = igmp6_mcf_seq_next,
2904 .stop = igmp6_mcf_seq_stop,
2905 .show = igmp6_mcf_seq_show,
2908 static int __net_init igmp6_proc_init(struct net *net)
2913 if (!proc_create_net("igmp6", 0444, net->proc_net, &igmp6_mc_seq_ops,
2914 sizeof(struct igmp6_mc_iter_state)))
2916 if (!proc_create_net("mcfilter6", 0444, net->proc_net,
2918 sizeof(struct igmp6_mcf_iter_state)))
2919 goto out_proc_net_igmp6;
2926 remove_proc_entry("igmp6", net->proc_net);
2930 static void __net_exit igmp6_proc_exit(struct net *net)
2932 remove_proc_entry("mcfilter6", net->proc_net);
2933 remove_proc_entry("igmp6", net->proc_net);
2936 static inline int igmp6_proc_init(struct net *net)
2940 static inline void igmp6_proc_exit(struct net *net)
2945 static int __net_init igmp6_net_init(struct net *net)
2949 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2950 SOCK_RAW, IPPROTO_ICMPV6, net);
2952 pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
2957 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
2959 err = inet_ctl_sock_create(&net->ipv6.mc_autojoin_sk, PF_INET6,
2960 SOCK_RAW, IPPROTO_ICMPV6, net);
2962 pr_err("Failed to initialize the IGMP6 autojoin socket (err %d)\n",
2964 goto out_sock_create;
2967 err = igmp6_proc_init(net);
2969 goto out_sock_create_autojoin;
2973 out_sock_create_autojoin:
2974 inet_ctl_sock_destroy(net->ipv6.mc_autojoin_sk);
2976 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2981 static void __net_exit igmp6_net_exit(struct net *net)
2983 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2984 inet_ctl_sock_destroy(net->ipv6.mc_autojoin_sk);
2985 igmp6_proc_exit(net);
2988 static struct pernet_operations igmp6_net_ops = {
2989 .init = igmp6_net_init,
2990 .exit = igmp6_net_exit,
2993 int __init igmp6_init(void)
2997 err = register_pernet_subsys(&igmp6_net_ops);
3001 mld_wq = create_workqueue("mld");
3003 unregister_pernet_subsys(&igmp6_net_ops);
3010 int __init igmp6_late_init(void)
3012 return register_netdevice_notifier(&igmp6_netdev_notifier);
3015 void igmp6_cleanup(void)
3017 unregister_pernet_subsys(&igmp6_net_ops);
3018 destroy_workqueue(mld_wq);
3021 void igmp6_late_cleanup(void)
3023 unregister_netdevice_notifier(&igmp6_netdev_notifier);