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_rcu(np, pmc) \
116 for (pmc = rcu_dereference((np)->ipv6_mc_list); \
118 pmc = rcu_dereference(pmc->next))
120 #define for_each_psf_rtnl(mc, psf) \
121 for (psf = rtnl_dereference((mc)->mca_sources); \
123 psf = rtnl_dereference(psf->sf_next))
125 #define for_each_psf_rcu(mc, psf) \
126 for (psf = rcu_dereference((mc)->mca_sources); \
128 psf = rcu_dereference(psf->sf_next))
130 #define for_each_psf_tomb(mc, psf) \
131 for (psf = rtnl_dereference((mc)->mca_tomb); \
133 psf = rtnl_dereference(psf->sf_next))
135 static int unsolicited_report_interval(struct inet6_dev *idev)
139 if (mld_in_v1_mode(idev))
140 iv = idev->cnf.mldv1_unsolicited_report_interval;
142 iv = idev->cnf.mldv2_unsolicited_report_interval;
144 return iv > 0 ? iv : 1;
147 static int __ipv6_sock_mc_join(struct sock *sk, int ifindex,
148 const struct in6_addr *addr, unsigned int mode)
150 struct net_device *dev = NULL;
151 struct ipv6_mc_socklist *mc_lst;
152 struct ipv6_pinfo *np = inet6_sk(sk);
153 struct net *net = sock_net(sk);
158 if (!ipv6_addr_is_multicast(addr))
162 for_each_pmc_rcu(np, mc_lst) {
163 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
164 ipv6_addr_equal(&mc_lst->addr, addr)) {
171 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
177 mc_lst->addr = *addr;
181 rt = rt6_lookup(net, addr, NULL, 0, NULL, 0);
187 dev = __dev_get_by_index(net, ifindex);
190 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
194 mc_lst->ifindex = dev->ifindex;
195 mc_lst->sfmode = mode;
196 RCU_INIT_POINTER(mc_lst->sflist, NULL);
199 * now add/increase the group membership on the device
202 err = __ipv6_dev_mc_inc(dev, addr, mode);
205 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
209 mc_lst->next = np->ipv6_mc_list;
210 rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
215 int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
217 return __ipv6_sock_mc_join(sk, ifindex, addr, MCAST_EXCLUDE);
219 EXPORT_SYMBOL(ipv6_sock_mc_join);
221 int ipv6_sock_mc_join_ssm(struct sock *sk, int ifindex,
222 const struct in6_addr *addr, unsigned int mode)
224 return __ipv6_sock_mc_join(sk, ifindex, addr, mode);
228 * socket leave on multicast group
230 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
232 struct ipv6_pinfo *np = inet6_sk(sk);
233 struct ipv6_mc_socklist *mc_lst;
234 struct ipv6_mc_socklist __rcu **lnk;
235 struct net *net = sock_net(sk);
239 if (!ipv6_addr_is_multicast(addr))
242 for (lnk = &np->ipv6_mc_list;
243 (mc_lst = rtnl_dereference(*lnk)) != NULL;
244 lnk = &mc_lst->next) {
245 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
246 ipv6_addr_equal(&mc_lst->addr, addr)) {
247 struct net_device *dev;
251 dev = __dev_get_by_index(net, mc_lst->ifindex);
253 struct inet6_dev *idev = __in6_dev_get(dev);
255 (void) ip6_mc_leave_src(sk, mc_lst, idev);
257 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
259 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
261 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
262 kfree_rcu(mc_lst, rcu);
267 return -EADDRNOTAVAIL;
269 EXPORT_SYMBOL(ipv6_sock_mc_drop);
271 /* called with rcu_read_lock() */
272 static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
273 const struct in6_addr *group,
276 struct net_device *dev = NULL;
277 struct inet6_dev *idev = NULL;
280 struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, NULL, 0);
287 dev = dev_get_by_index_rcu(net, ifindex);
291 idev = __in6_dev_get(dev);
294 read_lock_bh(&idev->lock);
296 read_unlock_bh(&idev->lock);
302 void __ipv6_sock_mc_close(struct sock *sk)
304 struct ipv6_pinfo *np = inet6_sk(sk);
305 struct ipv6_mc_socklist *mc_lst;
306 struct net *net = sock_net(sk);
310 while ((mc_lst = rtnl_dereference(np->ipv6_mc_list)) != NULL) {
311 struct net_device *dev;
313 np->ipv6_mc_list = mc_lst->next;
315 dev = __dev_get_by_index(net, mc_lst->ifindex);
317 struct inet6_dev *idev = __in6_dev_get(dev);
319 (void) ip6_mc_leave_src(sk, mc_lst, idev);
321 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
323 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
325 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
326 kfree_rcu(mc_lst, rcu);
330 void ipv6_sock_mc_close(struct sock *sk)
332 struct ipv6_pinfo *np = inet6_sk(sk);
334 if (!rcu_access_pointer(np->ipv6_mc_list))
337 __ipv6_sock_mc_close(sk);
341 int ip6_mc_source(int add, int omode, struct sock *sk,
342 struct group_source_req *pgsr)
344 struct in6_addr *source, *group;
345 struct ipv6_mc_socklist *pmc;
346 struct inet6_dev *idev;
347 struct ipv6_pinfo *inet6 = inet6_sk(sk);
348 struct ip6_sf_socklist *psl;
349 struct net *net = sock_net(sk);
354 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
355 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
357 if (!ipv6_addr_is_multicast(group))
361 idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface);
367 err = -EADDRNOTAVAIL;
369 for_each_pmc_rcu(inet6, pmc) {
370 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
372 if (ipv6_addr_equal(&pmc->addr, group))
375 if (!pmc) { /* must have a prior join */
379 /* if a source filter was set, must be the same mode as before */
380 if (rcu_access_pointer(pmc->sflist)) {
381 if (pmc->sfmode != omode) {
385 } else if (pmc->sfmode != omode) {
386 /* allow mode switches for empty-set filters */
387 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
388 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
392 psl = rtnl_dereference(pmc->sflist);
395 goto done; /* err = -EADDRNOTAVAIL */
397 for (i = 0; i < psl->sl_count; i++) {
398 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
402 if (rv) /* source not found */
403 goto done; /* err = -EADDRNOTAVAIL */
405 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
406 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
411 /* update the interface filter */
412 ip6_mc_del_src(idev, group, omode, 1, source, 1);
414 for (j = i+1; j < psl->sl_count; j++)
415 psl->sl_addr[j-1] = psl->sl_addr[j];
420 /* else, add a new source to the filter */
422 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
426 if (!psl || psl->sl_count == psl->sl_max) {
427 struct ip6_sf_socklist *newpsl;
428 int count = IP6_SFBLOCK;
431 count += psl->sl_max;
432 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
437 newpsl->sl_max = count;
438 newpsl->sl_count = count - IP6_SFBLOCK;
440 for (i = 0; i < psl->sl_count; i++)
441 newpsl->sl_addr[i] = psl->sl_addr[i];
442 atomic_sub(IP6_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
446 rcu_assign_pointer(pmc->sflist, psl);
448 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
449 for (i = 0; i < psl->sl_count; i++) {
450 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
451 if (rv == 0) /* There is an error in the address. */
454 for (j = psl->sl_count-1; j >= i; j--)
455 psl->sl_addr[j+1] = psl->sl_addr[j];
456 psl->sl_addr[i] = *source;
459 /* update the interface list */
460 ip6_mc_add_src(idev, group, omode, 1, source, 1);
462 read_unlock_bh(&idev->lock);
465 err = ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
469 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf,
470 struct sockaddr_storage *list)
472 const struct in6_addr *group;
473 struct ipv6_mc_socklist *pmc;
474 struct inet6_dev *idev;
475 struct ipv6_pinfo *inet6 = inet6_sk(sk);
476 struct ip6_sf_socklist *newpsl, *psl;
477 struct net *net = sock_net(sk);
481 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
483 if (!ipv6_addr_is_multicast(group))
485 if (gsf->gf_fmode != MCAST_INCLUDE &&
486 gsf->gf_fmode != MCAST_EXCLUDE)
490 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
499 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
504 for_each_pmc_rcu(inet6, pmc) {
505 if (pmc->ifindex != gsf->gf_interface)
507 if (ipv6_addr_equal(&pmc->addr, group))
510 if (!pmc) { /* must have a prior join */
514 if (gsf->gf_numsrc) {
515 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
521 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
522 for (i = 0; i < newpsl->sl_count; ++i, ++list) {
523 struct sockaddr_in6 *psin6;
525 psin6 = (struct sockaddr_in6 *)list;
526 newpsl->sl_addr[i] = psin6->sin6_addr;
528 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
529 newpsl->sl_count, newpsl->sl_addr, 0);
531 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
536 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
539 psl = rtnl_dereference(pmc->sflist);
541 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
542 psl->sl_count, psl->sl_addr, 0);
543 atomic_sub(IP6_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
546 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
547 rcu_assign_pointer(pmc->sflist, newpsl);
548 pmc->sfmode = gsf->gf_fmode;
551 read_unlock_bh(&idev->lock);
554 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
558 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
559 struct sockaddr_storage __user *p)
561 int err, i, count, copycount;
562 const struct in6_addr *group;
563 struct ipv6_mc_socklist *pmc;
564 struct inet6_dev *idev;
565 struct ipv6_pinfo *inet6 = inet6_sk(sk);
566 struct ip6_sf_socklist *psl;
567 struct net *net = sock_net(sk);
569 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
571 if (!ipv6_addr_is_multicast(group))
575 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
582 err = -EADDRNOTAVAIL;
583 /* changes to the ipv6_mc_list require the socket lock and
584 * rtnl lock. We have the socket lock and rcu read lock,
585 * so reading the list is safe.
588 for_each_pmc_rcu(inet6, pmc) {
589 if (pmc->ifindex != gsf->gf_interface)
591 if (ipv6_addr_equal(group, &pmc->addr))
594 if (!pmc) /* must have a prior join */
596 gsf->gf_fmode = pmc->sfmode;
597 psl = rtnl_dereference(pmc->sflist);
598 count = psl ? psl->sl_count : 0;
599 read_unlock_bh(&idev->lock);
602 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
603 gsf->gf_numsrc = count;
605 for (i = 0; i < copycount; i++, p++) {
606 struct sockaddr_in6 *psin6;
607 struct sockaddr_storage ss;
609 psin6 = (struct sockaddr_in6 *)&ss;
610 memset(&ss, 0, sizeof(ss));
611 psin6->sin6_family = AF_INET6;
612 psin6->sin6_addr = psl->sl_addr[i];
613 if (copy_to_user(p, &ss, sizeof(ss)))
618 read_unlock_bh(&idev->lock);
623 bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
624 const struct in6_addr *src_addr)
626 struct ipv6_pinfo *np = inet6_sk(sk);
627 struct ipv6_mc_socklist *mc;
628 struct ip6_sf_socklist *psl;
632 for_each_pmc_rcu(np, mc) {
633 if (ipv6_addr_equal(&mc->addr, mc_addr))
640 psl = rcu_dereference(mc->sflist);
642 rv = mc->sfmode == MCAST_EXCLUDE;
646 for (i = 0; i < psl->sl_count; i++) {
647 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
650 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
652 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
660 static void igmp6_group_added(struct ifmcaddr6 *mc)
662 struct net_device *dev = mc->idev->dev;
663 char buf[MAX_ADDR_LEN];
665 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
666 IPV6_ADDR_SCOPE_LINKLOCAL)
669 spin_lock_bh(&mc->mca_lock);
670 if (!(mc->mca_flags&MAF_LOADED)) {
671 mc->mca_flags |= MAF_LOADED;
672 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
673 dev_mc_add(dev, buf);
675 spin_unlock_bh(&mc->mca_lock);
677 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
680 if (mld_in_v1_mode(mc->idev)) {
681 igmp6_join_group(mc);
686 /* Based on RFC3810 6.1, for newly added INCLUDE SSM, we
687 * should not send filter-mode change record as the mode
688 * should be from IN() to IN(A).
690 if (mc->mca_sfmode == MCAST_EXCLUDE)
691 mc->mca_crcount = mc->idev->mc_qrv;
693 mld_ifc_event(mc->idev);
696 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
698 struct net_device *dev = mc->idev->dev;
699 char buf[MAX_ADDR_LEN];
701 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
702 IPV6_ADDR_SCOPE_LINKLOCAL)
705 spin_lock_bh(&mc->mca_lock);
706 if (mc->mca_flags&MAF_LOADED) {
707 mc->mca_flags &= ~MAF_LOADED;
708 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
709 dev_mc_del(dev, buf);
712 spin_unlock_bh(&mc->mca_lock);
713 if (mc->mca_flags & MAF_NOREPORT)
717 igmp6_leave_group(mc);
719 spin_lock_bh(&mc->mca_lock);
720 if (cancel_delayed_work(&mc->mca_work))
721 refcount_dec(&mc->mca_refcnt);
722 spin_unlock_bh(&mc->mca_lock);
726 * deleted ifmcaddr6 manipulation
728 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
730 struct ifmcaddr6 *pmc;
732 /* this is an "ifmcaddr6" for convenience; only the fields below
733 * are actually used. In particular, the refcnt and users are not
734 * used for management of the delete list. Using the same structure
735 * for deleted items allows change reports to use common code with
736 * non-deleted or query-response MCA's.
738 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
742 spin_lock_bh(&im->mca_lock);
743 spin_lock_init(&pmc->mca_lock);
744 pmc->idev = im->idev;
746 pmc->mca_addr = im->mca_addr;
747 pmc->mca_crcount = idev->mc_qrv;
748 pmc->mca_sfmode = im->mca_sfmode;
749 if (pmc->mca_sfmode == MCAST_INCLUDE) {
750 struct ip6_sf_list *psf;
752 rcu_assign_pointer(pmc->mca_tomb,
753 rtnl_dereference(im->mca_tomb));
754 rcu_assign_pointer(pmc->mca_sources,
755 rtnl_dereference(im->mca_sources));
756 RCU_INIT_POINTER(im->mca_tomb, NULL);
757 RCU_INIT_POINTER(im->mca_sources, NULL);
759 for_each_psf_rtnl(pmc, psf)
760 psf->sf_crcount = pmc->mca_crcount;
762 spin_unlock_bh(&im->mca_lock);
764 pmc->next = idev->mc_tomb;
768 static void mld_del_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
770 struct ip6_sf_list *psf, *sources, *tomb;
771 struct in6_addr *pmca = &im->mca_addr;
772 struct ifmcaddr6 *pmc, *pmc_prev;
775 for (pmc = idev->mc_tomb; pmc; pmc = pmc->next) {
776 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
782 pmc_prev->next = pmc->next;
784 idev->mc_tomb = pmc->next;
787 spin_lock_bh(&im->mca_lock);
789 im->idev = pmc->idev;
790 if (im->mca_sfmode == MCAST_INCLUDE) {
791 tomb = rcu_replace_pointer(im->mca_tomb,
792 rtnl_dereference(pmc->mca_tomb),
793 lockdep_rtnl_is_held());
794 rcu_assign_pointer(pmc->mca_tomb, tomb);
796 sources = rcu_replace_pointer(im->mca_sources,
797 rtnl_dereference(pmc->mca_sources),
798 lockdep_rtnl_is_held());
799 rcu_assign_pointer(pmc->mca_sources, sources);
800 for_each_psf_rtnl(im, psf)
801 psf->sf_crcount = idev->mc_qrv;
803 im->mca_crcount = idev->mc_qrv;
805 in6_dev_put(pmc->idev);
806 ip6_mc_clear_src(pmc);
809 spin_unlock_bh(&im->mca_lock);
812 static void mld_clear_delrec(struct inet6_dev *idev)
814 struct ifmcaddr6 *pmc, *nextpmc;
817 idev->mc_tomb = NULL;
819 for (; pmc; pmc = nextpmc) {
821 ip6_mc_clear_src(pmc);
822 in6_dev_put(pmc->idev);
826 /* clear dead sources, too */
827 read_lock_bh(&idev->lock);
828 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
829 struct ip6_sf_list *psf, *psf_next;
831 spin_lock_bh(&pmc->mca_lock);
832 psf = rtnl_dereference(pmc->mca_tomb);
833 RCU_INIT_POINTER(pmc->mca_tomb, NULL);
834 spin_unlock_bh(&pmc->mca_lock);
835 for (; psf; psf = psf_next) {
836 psf_next = rtnl_dereference(psf->sf_next);
840 read_unlock_bh(&idev->lock);
843 static void mca_get(struct ifmcaddr6 *mc)
845 refcount_inc(&mc->mca_refcnt);
848 static void ma_put(struct ifmcaddr6 *mc)
850 if (refcount_dec_and_test(&mc->mca_refcnt)) {
851 in6_dev_put(mc->idev);
856 static struct ifmcaddr6 *mca_alloc(struct inet6_dev *idev,
857 const struct in6_addr *addr,
860 struct ifmcaddr6 *mc;
862 mc = kzalloc(sizeof(*mc), GFP_ATOMIC);
866 INIT_DELAYED_WORK(&mc->mca_work, mld_mca_work);
868 mc->mca_addr = *addr;
869 mc->idev = idev; /* reference taken by caller */
871 /* mca_stamp should be updated upon changes */
872 mc->mca_cstamp = mc->mca_tstamp = jiffies;
873 refcount_set(&mc->mca_refcnt, 1);
874 spin_lock_init(&mc->mca_lock);
876 mc->mca_sfmode = mode;
877 mc->mca_sfcount[mode] = 1;
879 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
880 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
881 mc->mca_flags |= MAF_NOREPORT;
887 * device multicast group inc (add if not found)
889 static int __ipv6_dev_mc_inc(struct net_device *dev,
890 const struct in6_addr *addr, unsigned int mode)
892 struct ifmcaddr6 *mc;
893 struct inet6_dev *idev;
897 /* we need to take a reference on idev */
898 idev = in6_dev_get(dev);
903 write_lock_bh(&idev->lock);
905 write_unlock_bh(&idev->lock);
910 for (mc = idev->mc_list; mc; mc = mc->next) {
911 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
913 write_unlock_bh(&idev->lock);
914 ip6_mc_add_src(idev, &mc->mca_addr, mode, 0, NULL, 0);
920 mc = mca_alloc(idev, addr, mode);
922 write_unlock_bh(&idev->lock);
927 mc->next = idev->mc_list;
930 /* Hold this for the code below before we unlock,
931 * it is already exposed via idev->mc_list.
934 write_unlock_bh(&idev->lock);
936 mld_del_delrec(idev, mc);
937 igmp6_group_added(mc);
942 int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
944 return __ipv6_dev_mc_inc(dev, addr, MCAST_EXCLUDE);
946 EXPORT_SYMBOL(ipv6_dev_mc_inc);
949 * device multicast group del
951 int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
953 struct ifmcaddr6 *ma, **map;
957 write_lock_bh(&idev->lock);
958 for (map = &idev->mc_list; (ma = *map) != NULL; map = &ma->next) {
959 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
960 if (--ma->mca_users == 0) {
962 write_unlock_bh(&idev->lock);
964 igmp6_group_dropped(ma);
965 ip6_mc_clear_src(ma);
970 write_unlock_bh(&idev->lock);
974 write_unlock_bh(&idev->lock);
979 int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
981 struct inet6_dev *idev;
986 idev = __in6_dev_get(dev);
990 err = __ipv6_dev_mc_dec(idev, addr);
994 EXPORT_SYMBOL(ipv6_dev_mc_dec);
997 * check if the interface/address pair is valid
999 bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
1000 const struct in6_addr *src_addr)
1002 struct inet6_dev *idev;
1003 struct ifmcaddr6 *mc;
1007 idev = __in6_dev_get(dev);
1009 read_lock_bh(&idev->lock);
1010 for (mc = idev->mc_list; mc; mc = mc->next) {
1011 if (ipv6_addr_equal(&mc->mca_addr, group))
1015 if (src_addr && !ipv6_addr_any(src_addr)) {
1016 struct ip6_sf_list *psf;
1018 spin_lock_bh(&mc->mca_lock);
1019 for_each_psf_rcu(mc, psf) {
1020 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1024 rv = psf->sf_count[MCAST_INCLUDE] ||
1025 psf->sf_count[MCAST_EXCLUDE] !=
1026 mc->mca_sfcount[MCAST_EXCLUDE];
1028 rv = mc->mca_sfcount[MCAST_EXCLUDE] != 0;
1029 spin_unlock_bh(&mc->mca_lock);
1031 rv = true; /* don't filter unspecified source */
1033 read_unlock_bh(&idev->lock);
1039 static void mld_gq_start_work(struct inet6_dev *idev)
1041 unsigned long tv = prandom_u32() % idev->mc_maxdelay;
1043 idev->mc_gq_running = 1;
1044 if (!mod_delayed_work(mld_wq, &idev->mc_gq_work, tv + 2))
1048 static void mld_gq_stop_work(struct inet6_dev *idev)
1050 idev->mc_gq_running = 0;
1051 if (cancel_delayed_work(&idev->mc_gq_work))
1052 __in6_dev_put(idev);
1055 static void mld_ifc_start_work(struct inet6_dev *idev, unsigned long delay)
1057 unsigned long tv = prandom_u32() % delay;
1059 if (!mod_delayed_work(mld_wq, &idev->mc_ifc_work, tv + 2))
1063 static void mld_ifc_stop_work(struct inet6_dev *idev)
1065 idev->mc_ifc_count = 0;
1066 if (cancel_delayed_work(&idev->mc_ifc_work))
1067 __in6_dev_put(idev);
1070 static void mld_dad_start_work(struct inet6_dev *idev, unsigned long delay)
1072 unsigned long tv = prandom_u32() % delay;
1074 if (!mod_delayed_work(mld_wq, &idev->mc_dad_work, tv + 2))
1078 static void mld_dad_stop_work(struct inet6_dev *idev)
1080 if (cancel_delayed_work(&idev->mc_dad_work))
1081 __in6_dev_put(idev);
1085 * IGMP handling (alias multicast ICMPv6 messages)
1088 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1090 unsigned long delay = resptime;
1092 /* Do not start work for these addresses */
1093 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1094 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1097 if (cancel_delayed_work(&ma->mca_work)) {
1098 refcount_dec(&ma->mca_refcnt);
1099 delay = ma->mca_work.timer.expires - jiffies;
1102 if (delay >= resptime)
1103 delay = prandom_u32() % resptime;
1105 if (!mod_delayed_work(mld_wq, &ma->mca_work, delay))
1106 refcount_inc(&ma->mca_refcnt);
1107 ma->mca_flags |= MAF_TIMER_RUNNING;
1110 /* mark EXCLUDE-mode sources */
1111 static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1112 const struct in6_addr *srcs)
1114 struct ip6_sf_list *psf;
1118 for_each_psf_rcu(pmc, psf) {
1119 if (scount == nsrcs)
1121 for (i = 0; i < nsrcs; i++) {
1122 /* skip inactive filters */
1123 if (psf->sf_count[MCAST_INCLUDE] ||
1124 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1125 psf->sf_count[MCAST_EXCLUDE])
1127 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1133 pmc->mca_flags &= ~MAF_GSQUERY;
1134 if (scount == nsrcs) /* all sources excluded */
1139 static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1140 const struct in6_addr *srcs)
1142 struct ip6_sf_list *psf;
1145 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1146 return mld_xmarksources(pmc, nsrcs, srcs);
1148 /* mark INCLUDE-mode sources */
1151 for_each_psf_rcu(pmc, psf) {
1152 if (scount == nsrcs)
1154 for (i = 0; i < nsrcs; i++) {
1155 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1163 pmc->mca_flags &= ~MAF_GSQUERY;
1166 pmc->mca_flags |= MAF_GSQUERY;
1170 static int mld_force_mld_version(const struct inet6_dev *idev)
1172 /* Normally, both are 0 here. If enforcement to a particular is
1173 * being used, individual device enforcement will have a lower
1174 * precedence over 'all' device (.../conf/all/force_mld_version).
1177 if (dev_net(idev->dev)->ipv6.devconf_all->force_mld_version != 0)
1178 return dev_net(idev->dev)->ipv6.devconf_all->force_mld_version;
1180 return idev->cnf.force_mld_version;
1183 static bool mld_in_v2_mode_only(const struct inet6_dev *idev)
1185 return mld_force_mld_version(idev) == 2;
1188 static bool mld_in_v1_mode_only(const struct inet6_dev *idev)
1190 return mld_force_mld_version(idev) == 1;
1193 static bool mld_in_v1_mode(const struct inet6_dev *idev)
1195 if (mld_in_v2_mode_only(idev))
1197 if (mld_in_v1_mode_only(idev))
1199 if (idev->mc_v1_seen && time_before(jiffies, idev->mc_v1_seen))
1205 static void mld_set_v1_mode(struct inet6_dev *idev)
1207 /* RFC3810, relevant sections:
1208 * - 9.1. Robustness Variable
1209 * - 9.2. Query Interval
1210 * - 9.3. Query Response Interval
1211 * - 9.12. Older Version Querier Present Timeout
1213 unsigned long switchback;
1215 switchback = (idev->mc_qrv * idev->mc_qi) + idev->mc_qri;
1217 idev->mc_v1_seen = jiffies + switchback;
1220 static void mld_update_qrv(struct inet6_dev *idev,
1221 const struct mld2_query *mlh2)
1223 /* RFC3810, relevant sections:
1224 * - 5.1.8. QRV (Querier's Robustness Variable)
1225 * - 9.1. Robustness Variable
1228 /* The value of the Robustness Variable MUST NOT be zero,
1229 * and SHOULD NOT be one. Catch this here if we ever run
1230 * into such a case in future.
1232 const int min_qrv = min(MLD_QRV_DEFAULT, sysctl_mld_qrv);
1233 WARN_ON(idev->mc_qrv == 0);
1235 if (mlh2->mld2q_qrv > 0)
1236 idev->mc_qrv = mlh2->mld2q_qrv;
1238 if (unlikely(idev->mc_qrv < min_qrv)) {
1239 net_warn_ratelimited("IPv6: MLD: clamping QRV from %u to %u!\n",
1240 idev->mc_qrv, min_qrv);
1241 idev->mc_qrv = min_qrv;
1245 static void mld_update_qi(struct inet6_dev *idev,
1246 const struct mld2_query *mlh2)
1248 /* RFC3810, relevant sections:
1249 * - 5.1.9. QQIC (Querier's Query Interval Code)
1250 * - 9.2. Query Interval
1251 * - 9.12. Older Version Querier Present Timeout
1252 * (the [Query Interval] in the last Query received)
1254 unsigned long mc_qqi;
1256 if (mlh2->mld2q_qqic < 128) {
1257 mc_qqi = mlh2->mld2q_qqic;
1259 unsigned long mc_man, mc_exp;
1261 mc_exp = MLDV2_QQIC_EXP(mlh2->mld2q_qqic);
1262 mc_man = MLDV2_QQIC_MAN(mlh2->mld2q_qqic);
1264 mc_qqi = (mc_man | 0x10) << (mc_exp + 3);
1267 idev->mc_qi = mc_qqi * HZ;
1270 static void mld_update_qri(struct inet6_dev *idev,
1271 const struct mld2_query *mlh2)
1273 /* RFC3810, relevant sections:
1274 * - 5.1.3. Maximum Response Code
1275 * - 9.3. Query Response Interval
1277 idev->mc_qri = msecs_to_jiffies(mldv2_mrc(mlh2));
1280 static int mld_process_v1(struct inet6_dev *idev, struct mld_msg *mld,
1281 unsigned long *max_delay, bool v1_query)
1283 unsigned long mldv1_md;
1285 /* Ignore v1 queries */
1286 if (mld_in_v2_mode_only(idev))
1289 mldv1_md = ntohs(mld->mld_maxdelay);
1291 /* When in MLDv1 fallback and a MLDv2 router start-up being
1292 * unaware of current MLDv1 operation, the MRC == MRD mapping
1293 * only works when the exponential algorithm is not being
1294 * used (as MLDv1 is unaware of such things).
1296 * According to the RFC author, the MLDv2 implementations
1297 * he's aware of all use a MRC < 32768 on start up queries.
1299 * Thus, should we *ever* encounter something else larger
1300 * than that, just assume the maximum possible within our
1304 mldv1_md = min(mldv1_md, MLDV1_MRD_MAX_COMPAT);
1306 *max_delay = max(msecs_to_jiffies(mldv1_md), 1UL);
1308 /* MLDv1 router present: we need to go into v1 mode *only*
1309 * when an MLDv1 query is received as per section 9.12. of
1310 * RFC3810! And we know from RFC2710 section 3.7 that MLDv1
1311 * queries MUST be of exactly 24 octets.
1314 mld_set_v1_mode(idev);
1316 /* cancel MLDv2 report work */
1317 mld_gq_stop_work(idev);
1318 /* cancel the interface change work */
1319 mld_ifc_stop_work(idev);
1320 /* clear deleted report items */
1321 mld_clear_delrec(idev);
1326 static int mld_process_v2(struct inet6_dev *idev, struct mld2_query *mld,
1327 unsigned long *max_delay)
1329 *max_delay = max(msecs_to_jiffies(mldv2_mrc(mld)), 1UL);
1331 mld_update_qrv(idev, mld);
1332 mld_update_qi(idev, mld);
1333 mld_update_qri(idev, mld);
1335 idev->mc_maxdelay = *max_delay;
1340 /* called with rcu_read_lock() */
1341 int igmp6_event_query(struct sk_buff *skb)
1343 struct mld2_query *mlh2 = NULL;
1344 struct ifmcaddr6 *ma;
1345 const struct in6_addr *group;
1346 unsigned long max_delay;
1347 struct inet6_dev *idev;
1348 struct mld_msg *mld;
1353 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1356 /* compute payload length excluding extension headers */
1357 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
1358 len -= skb_network_header_len(skb);
1361 * Upon reception of an MLD message that contains a Query, the node
1362 * checks if the source address of the message is a valid link-local
1363 * address, if the Hop Limit is set to 1, and if the Router Alert
1364 * option is present in the Hop-By-Hop Options header of the IPv6
1365 * packet. If any of these checks fails, the packet is dropped.
1367 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL) ||
1368 ipv6_hdr(skb)->hop_limit != 1 ||
1369 !(IP6CB(skb)->flags & IP6SKB_ROUTERALERT) ||
1370 IP6CB(skb)->ra != htons(IPV6_OPT_ROUTERALERT_MLD))
1373 idev = __in6_dev_get(skb->dev);
1377 mld = (struct mld_msg *)icmp6_hdr(skb);
1378 group = &mld->mld_mca;
1379 group_type = ipv6_addr_type(group);
1381 if (group_type != IPV6_ADDR_ANY &&
1382 !(group_type&IPV6_ADDR_MULTICAST))
1385 if (len < MLD_V1_QUERY_LEN) {
1387 } else if (len == MLD_V1_QUERY_LEN || mld_in_v1_mode(idev)) {
1388 err = mld_process_v1(idev, mld, &max_delay,
1389 len == MLD_V1_QUERY_LEN);
1392 } else if (len >= MLD_V2_QUERY_LEN_MIN) {
1393 int srcs_offset = sizeof(struct mld2_query) -
1394 sizeof(struct icmp6hdr);
1396 if (!pskb_may_pull(skb, srcs_offset))
1399 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1401 err = mld_process_v2(idev, mlh2, &max_delay);
1405 if (group_type == IPV6_ADDR_ANY) { /* general query */
1406 if (mlh2->mld2q_nsrcs)
1407 return -EINVAL; /* no sources allowed */
1409 mld_gq_start_work(idev);
1412 /* mark sources to include, if group & source-specific */
1413 if (mlh2->mld2q_nsrcs != 0) {
1414 if (!pskb_may_pull(skb, srcs_offset +
1415 ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
1418 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1425 read_lock_bh(&idev->lock);
1426 if (group_type == IPV6_ADDR_ANY) {
1427 for (ma = idev->mc_list; ma; ma = ma->next) {
1428 spin_lock_bh(&ma->mca_lock);
1429 igmp6_group_queried(ma, max_delay);
1430 spin_unlock_bh(&ma->mca_lock);
1433 for (ma = idev->mc_list; ma; ma = ma->next) {
1434 if (!ipv6_addr_equal(group, &ma->mca_addr))
1436 spin_lock_bh(&ma->mca_lock);
1437 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1438 /* gsquery <- gsquery && mark */
1440 ma->mca_flags &= ~MAF_GSQUERY;
1442 /* gsquery <- mark */
1444 ma->mca_flags |= MAF_GSQUERY;
1446 ma->mca_flags &= ~MAF_GSQUERY;
1448 if (!(ma->mca_flags & MAF_GSQUERY) ||
1449 mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
1450 igmp6_group_queried(ma, max_delay);
1451 spin_unlock_bh(&ma->mca_lock);
1455 read_unlock_bh(&idev->lock);
1460 /* called with rcu_read_lock() */
1461 int igmp6_event_report(struct sk_buff *skb)
1463 struct ifmcaddr6 *ma;
1464 struct inet6_dev *idev;
1465 struct mld_msg *mld;
1468 /* Our own report looped back. Ignore it. */
1469 if (skb->pkt_type == PACKET_LOOPBACK)
1472 /* send our report if the MC router may not have heard this report */
1473 if (skb->pkt_type != PACKET_MULTICAST &&
1474 skb->pkt_type != PACKET_BROADCAST)
1477 if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
1480 mld = (struct mld_msg *)icmp6_hdr(skb);
1482 /* Drop reports with not link local source */
1483 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
1484 if (addr_type != IPV6_ADDR_ANY &&
1485 !(addr_type&IPV6_ADDR_LINKLOCAL))
1488 idev = __in6_dev_get(skb->dev);
1493 * Cancel the work for this group
1496 read_lock_bh(&idev->lock);
1497 for (ma = idev->mc_list; ma; ma = ma->next) {
1498 if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
1499 spin_lock(&ma->mca_lock);
1500 if (cancel_delayed_work(&ma->mca_work))
1501 refcount_dec(&ma->mca_refcnt);
1502 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1503 spin_unlock(&ma->mca_lock);
1507 read_unlock_bh(&idev->lock);
1511 static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1512 int gdeleted, int sdeleted)
1515 case MLD2_MODE_IS_INCLUDE:
1516 case MLD2_MODE_IS_EXCLUDE:
1517 if (gdeleted || sdeleted)
1519 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1520 if (pmc->mca_sfmode == MCAST_INCLUDE)
1522 /* don't include if this source is excluded
1525 if (psf->sf_count[MCAST_INCLUDE])
1526 return type == MLD2_MODE_IS_INCLUDE;
1527 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1528 psf->sf_count[MCAST_EXCLUDE];
1531 case MLD2_CHANGE_TO_INCLUDE:
1532 if (gdeleted || sdeleted)
1534 return psf->sf_count[MCAST_INCLUDE] != 0;
1535 case MLD2_CHANGE_TO_EXCLUDE:
1536 if (gdeleted || sdeleted)
1538 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1539 psf->sf_count[MCAST_INCLUDE])
1541 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1542 psf->sf_count[MCAST_EXCLUDE];
1543 case MLD2_ALLOW_NEW_SOURCES:
1544 if (gdeleted || !psf->sf_crcount)
1546 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1547 case MLD2_BLOCK_OLD_SOURCES:
1548 if (pmc->mca_sfmode == MCAST_INCLUDE)
1549 return gdeleted || (psf->sf_crcount && sdeleted);
1550 return psf->sf_crcount && !gdeleted && !sdeleted;
1556 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1558 struct ip6_sf_list *psf;
1561 for_each_psf_rtnl(pmc, psf) {
1562 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1569 static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
1570 struct net_device *dev,
1571 const struct in6_addr *saddr,
1572 const struct in6_addr *daddr,
1575 struct ipv6hdr *hdr;
1577 skb->protocol = htons(ETH_P_IPV6);
1580 skb_reset_network_header(skb);
1581 skb_put(skb, sizeof(struct ipv6hdr));
1582 hdr = ipv6_hdr(skb);
1584 ip6_flow_hdr(hdr, 0, 0);
1586 hdr->payload_len = htons(len);
1587 hdr->nexthdr = proto;
1588 hdr->hop_limit = inet6_sk(sk)->hop_limit;
1590 hdr->saddr = *saddr;
1591 hdr->daddr = *daddr;
1594 static struct sk_buff *mld_newpack(struct inet6_dev *idev, unsigned int mtu)
1596 struct net_device *dev = idev->dev;
1597 struct net *net = dev_net(dev);
1598 struct sock *sk = net->ipv6.igmp_sk;
1599 struct sk_buff *skb;
1600 struct mld2_report *pmr;
1601 struct in6_addr addr_buf;
1602 const struct in6_addr *saddr;
1603 int hlen = LL_RESERVED_SPACE(dev);
1604 int tlen = dev->needed_tailroom;
1605 unsigned int size = mtu + hlen + tlen;
1607 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1608 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1611 /* we assume size > sizeof(ra) here */
1612 /* limit our allocations to order-0 page */
1613 size = min_t(int, size, SKB_MAX_ORDER(0, 0));
1614 skb = sock_alloc_send_skb(sk, size, 1, &err);
1619 skb->priority = TC_PRIO_CONTROL;
1620 skb_reserve(skb, hlen);
1621 skb_tailroom_reserve(skb, mtu, tlen);
1623 if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
1624 /* <draft-ietf-magma-mld-source-05.txt>:
1625 * use unspecified address as the source address
1626 * when a valid link-local address is not available.
1628 saddr = &in6addr_any;
1632 ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
1634 skb_put_data(skb, ra, sizeof(ra));
1636 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
1637 skb_put(skb, sizeof(*pmr));
1638 pmr = (struct mld2_report *)skb_transport_header(skb);
1639 pmr->mld2r_type = ICMPV6_MLD2_REPORT;
1640 pmr->mld2r_resv1 = 0;
1641 pmr->mld2r_cksum = 0;
1642 pmr->mld2r_resv2 = 0;
1643 pmr->mld2r_ngrec = 0;
1647 static void mld_sendpack(struct sk_buff *skb)
1649 struct ipv6hdr *pip6 = ipv6_hdr(skb);
1650 struct mld2_report *pmr =
1651 (struct mld2_report *)skb_transport_header(skb);
1652 int payload_len, mldlen;
1653 struct inet6_dev *idev;
1654 struct net *net = dev_net(skb->dev);
1657 struct dst_entry *dst;
1660 idev = __in6_dev_get(skb->dev);
1661 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1663 payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
1665 mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
1666 pip6->payload_len = htons(payload_len);
1668 pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1670 csum_partial(skb_transport_header(skb),
1673 icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
1674 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1676 dst = icmp6_dst_alloc(skb->dev, &fl6);
1683 skb_dst_set(skb, dst);
1687 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
1688 net, net->ipv6.igmp_sk, skb, NULL, skb->dev,
1692 ICMP6MSGOUT_INC_STATS(net, idev, ICMPV6_MLD2_REPORT);
1693 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
1695 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
1706 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1708 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1711 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1712 int type, struct mld2_grec **ppgr, unsigned int mtu)
1714 struct mld2_report *pmr;
1715 struct mld2_grec *pgr;
1718 skb = mld_newpack(pmc->idev, mtu);
1722 pgr = skb_put(skb, sizeof(struct mld2_grec));
1723 pgr->grec_type = type;
1724 pgr->grec_auxwords = 0;
1725 pgr->grec_nsrcs = 0;
1726 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1727 pmr = (struct mld2_report *)skb_transport_header(skb);
1728 pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
1733 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
1735 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1736 int type, int gdeleted, int sdeleted,
1739 struct ip6_sf_list *psf, *psf_prev, *psf_next;
1740 int scount, stotal, first, isquery, truncate;
1741 struct ip6_sf_list __rcu **psf_list;
1742 struct inet6_dev *idev = pmc->idev;
1743 struct net_device *dev = idev->dev;
1744 struct mld2_grec *pgr = NULL;
1745 struct mld2_report *pmr;
1748 if (pmc->mca_flags & MAF_NOREPORT)
1751 mtu = READ_ONCE(dev->mtu);
1752 if (mtu < IPV6_MIN_MTU)
1755 isquery = type == MLD2_MODE_IS_INCLUDE ||
1756 type == MLD2_MODE_IS_EXCLUDE;
1757 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1758 type == MLD2_CHANGE_TO_EXCLUDE;
1760 stotal = scount = 0;
1762 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1764 if (!rcu_access_pointer(*psf_list))
1767 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
1769 /* EX and TO_EX get a fresh packet, if needed */
1771 if (pmr && pmr->mld2r_ngrec &&
1772 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1775 skb = mld_newpack(idev, mtu);
1780 for (psf = rtnl_dereference(*psf_list);
1783 struct in6_addr *psrc;
1785 psf_next = rtnl_dereference(psf->sf_next);
1787 if (!is_in(pmc, psf, type, gdeleted, sdeleted) && !crsend) {
1792 /* Based on RFC3810 6.1. Should not send source-list change
1793 * records when there is a filter mode change.
1795 if (((gdeleted && pmc->mca_sfmode == MCAST_EXCLUDE) ||
1796 (!gdeleted && pmc->mca_crcount)) &&
1797 (type == MLD2_ALLOW_NEW_SOURCES ||
1798 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount)
1799 goto decrease_sf_crcount;
1801 /* clear marks on query responses */
1805 if (AVAILABLE(skb) < sizeof(*psrc) +
1806 first*sizeof(struct mld2_grec)) {
1807 if (truncate && !first)
1808 break; /* truncate these */
1810 pgr->grec_nsrcs = htons(scount);
1813 skb = mld_newpack(idev, mtu);
1818 skb = add_grhead(skb, pmc, type, &pgr, mtu);
1823 psrc = skb_put(skb, sizeof(*psrc));
1824 *psrc = psf->sf_addr;
1826 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1827 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1828 decrease_sf_crcount:
1830 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1832 rcu_assign_pointer(psf_prev->sf_next,
1833 rtnl_dereference(psf->sf_next));
1835 rcu_assign_pointer(*psf_list,
1836 rtnl_dereference(psf->sf_next));
1837 kfree_rcu(psf, rcu);
1846 if (type == MLD2_ALLOW_NEW_SOURCES ||
1847 type == MLD2_BLOCK_OLD_SOURCES)
1849 if (pmc->mca_crcount || isquery || crsend) {
1850 /* make sure we have room for group header */
1851 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1853 skb = NULL; /* add_grhead will get a new one */
1855 skb = add_grhead(skb, pmc, type, &pgr, mtu);
1859 pgr->grec_nsrcs = htons(scount);
1862 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1866 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1868 struct sk_buff *skb = NULL;
1871 read_lock_bh(&idev->lock);
1873 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
1874 if (pmc->mca_flags & MAF_NOREPORT)
1876 spin_lock_bh(&pmc->mca_lock);
1877 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1878 type = MLD2_MODE_IS_EXCLUDE;
1880 type = MLD2_MODE_IS_INCLUDE;
1881 skb = add_grec(skb, pmc, type, 0, 0, 0);
1882 spin_unlock_bh(&pmc->mca_lock);
1885 spin_lock_bh(&pmc->mca_lock);
1886 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1887 type = MLD2_MODE_IS_EXCLUDE;
1889 type = MLD2_MODE_IS_INCLUDE;
1890 skb = add_grec(skb, pmc, type, 0, 0, 0);
1891 spin_unlock_bh(&pmc->mca_lock);
1893 read_unlock_bh(&idev->lock);
1899 * remove zero-count source records from a source filter list
1901 static void mld_clear_zeros(struct ip6_sf_list __rcu **ppsf)
1903 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1906 for (psf = rtnl_dereference(*ppsf);
1909 psf_next = rtnl_dereference(psf->sf_next);
1910 if (psf->sf_crcount == 0) {
1912 rcu_assign_pointer(psf_prev->sf_next,
1913 rtnl_dereference(psf->sf_next));
1915 rcu_assign_pointer(*ppsf,
1916 rtnl_dereference(psf->sf_next));
1917 kfree_rcu(psf, rcu);
1924 static void mld_send_cr(struct inet6_dev *idev)
1926 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1927 struct sk_buff *skb = NULL;
1930 read_lock_bh(&idev->lock);
1934 for (pmc = idev->mc_tomb; pmc; pmc = pmc_next) {
1935 pmc_next = pmc->next;
1936 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1937 type = MLD2_BLOCK_OLD_SOURCES;
1938 dtype = MLD2_BLOCK_OLD_SOURCES;
1939 skb = add_grec(skb, pmc, type, 1, 0, 0);
1940 skb = add_grec(skb, pmc, dtype, 1, 1, 0);
1942 if (pmc->mca_crcount) {
1943 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1944 type = MLD2_CHANGE_TO_INCLUDE;
1945 skb = add_grec(skb, pmc, type, 1, 0, 0);
1948 if (pmc->mca_crcount == 0) {
1949 mld_clear_zeros(&pmc->mca_tomb);
1950 mld_clear_zeros(&pmc->mca_sources);
1953 if (pmc->mca_crcount == 0 &&
1954 !rcu_access_pointer(pmc->mca_tomb) &&
1955 !rcu_access_pointer(pmc->mca_sources)) {
1957 pmc_prev->next = pmc_next;
1959 idev->mc_tomb = pmc_next;
1960 in6_dev_put(pmc->idev);
1967 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
1968 spin_lock_bh(&pmc->mca_lock);
1969 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1970 type = MLD2_BLOCK_OLD_SOURCES;
1971 dtype = MLD2_ALLOW_NEW_SOURCES;
1973 type = MLD2_ALLOW_NEW_SOURCES;
1974 dtype = MLD2_BLOCK_OLD_SOURCES;
1976 skb = add_grec(skb, pmc, type, 0, 0, 0);
1977 skb = add_grec(skb, pmc, dtype, 0, 1, 0); /* deleted sources */
1979 /* filter mode changes */
1980 if (pmc->mca_crcount) {
1981 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1982 type = MLD2_CHANGE_TO_EXCLUDE;
1984 type = MLD2_CHANGE_TO_INCLUDE;
1985 skb = add_grec(skb, pmc, type, 0, 0, 0);
1988 spin_unlock_bh(&pmc->mca_lock);
1990 read_unlock_bh(&idev->lock);
1993 (void) mld_sendpack(skb);
1996 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1998 struct net *net = dev_net(dev);
1999 struct sock *sk = net->ipv6.igmp_sk;
2000 struct inet6_dev *idev;
2001 struct sk_buff *skb;
2002 struct mld_msg *hdr;
2003 const struct in6_addr *snd_addr, *saddr;
2004 struct in6_addr addr_buf;
2005 int hlen = LL_RESERVED_SPACE(dev);
2006 int tlen = dev->needed_tailroom;
2007 int err, len, payload_len, full_len;
2008 u8 ra[8] = { IPPROTO_ICMPV6, 0,
2009 IPV6_TLV_ROUTERALERT, 2, 0, 0,
2012 struct dst_entry *dst;
2014 if (type == ICMPV6_MGM_REDUCTION)
2015 snd_addr = &in6addr_linklocal_allrouters;
2019 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
2020 payload_len = len + sizeof(ra);
2021 full_len = sizeof(struct ipv6hdr) + payload_len;
2024 IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
2025 IPSTATS_MIB_OUT, full_len);
2028 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
2032 IP6_INC_STATS(net, __in6_dev_get(dev),
2033 IPSTATS_MIB_OUTDISCARDS);
2037 skb->priority = TC_PRIO_CONTROL;
2038 skb_reserve(skb, hlen);
2040 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
2041 /* <draft-ietf-magma-mld-source-05.txt>:
2042 * use unspecified address as the source address
2043 * when a valid link-local address is not available.
2045 saddr = &in6addr_any;
2049 ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
2051 skb_put_data(skb, ra, sizeof(ra));
2053 hdr = skb_put_zero(skb, sizeof(struct mld_msg));
2054 hdr->mld_type = type;
2055 hdr->mld_mca = *addr;
2057 hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
2059 csum_partial(hdr, len, 0));
2062 idev = __in6_dev_get(skb->dev);
2064 icmpv6_flow_init(sk, &fl6, type,
2065 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
2067 dst = icmp6_dst_alloc(skb->dev, &fl6);
2073 skb_dst_set(skb, dst);
2074 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
2075 net, sk, skb, NULL, skb->dev,
2079 ICMP6MSGOUT_INC_STATS(net, idev, type);
2080 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
2082 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
2092 static void mld_send_initial_cr(struct inet6_dev *idev)
2094 struct sk_buff *skb;
2095 struct ifmcaddr6 *pmc;
2098 if (mld_in_v1_mode(idev))
2102 read_lock_bh(&idev->lock);
2103 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
2104 spin_lock_bh(&pmc->mca_lock);
2105 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2106 type = MLD2_CHANGE_TO_EXCLUDE;
2108 type = MLD2_ALLOW_NEW_SOURCES;
2109 skb = add_grec(skb, pmc, type, 0, 0, 1);
2110 spin_unlock_bh(&pmc->mca_lock);
2112 read_unlock_bh(&idev->lock);
2117 void ipv6_mc_dad_complete(struct inet6_dev *idev)
2119 idev->mc_dad_count = idev->mc_qrv;
2120 if (idev->mc_dad_count) {
2121 mld_send_initial_cr(idev);
2122 idev->mc_dad_count--;
2123 if (idev->mc_dad_count)
2124 mld_dad_start_work(idev,
2125 unsolicited_report_interval(idev));
2129 static void mld_dad_work(struct work_struct *work)
2131 struct inet6_dev *idev = container_of(to_delayed_work(work),
2135 mld_send_initial_cr(idev);
2136 if (idev->mc_dad_count) {
2137 idev->mc_dad_count--;
2138 if (idev->mc_dad_count)
2139 mld_dad_start_work(idev,
2140 unsolicited_report_interval(idev));
2145 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
2146 const struct in6_addr *psfsrc)
2148 struct ip6_sf_list *psf, *psf_prev;
2152 for_each_psf_rtnl(pmc, psf) {
2153 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2157 if (!psf || psf->sf_count[sfmode] == 0) {
2158 /* source filter not found, or count wrong => bug */
2161 psf->sf_count[sfmode]--;
2162 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
2163 struct inet6_dev *idev = pmc->idev;
2165 /* no more filters for this source */
2167 rcu_assign_pointer(psf_prev->sf_next,
2168 rtnl_dereference(psf->sf_next));
2170 rcu_assign_pointer(pmc->mca_sources,
2171 rtnl_dereference(psf->sf_next));
2173 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
2174 !mld_in_v1_mode(idev)) {
2175 psf->sf_crcount = idev->mc_qrv;
2176 rcu_assign_pointer(psf->sf_next,
2177 rtnl_dereference(pmc->mca_tomb));
2178 rcu_assign_pointer(pmc->mca_tomb, psf);
2181 kfree_rcu(psf, rcu);
2187 static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2188 int sfmode, int sfcount, const struct in6_addr *psfsrc,
2191 struct ifmcaddr6 *pmc;
2197 read_lock_bh(&idev->lock);
2198 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
2199 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2203 /* MCA not found?? bug */
2204 read_unlock_bh(&idev->lock);
2207 spin_lock_bh(&pmc->mca_lock);
2210 if (!pmc->mca_sfcount[sfmode]) {
2211 spin_unlock_bh(&pmc->mca_lock);
2212 read_unlock_bh(&idev->lock);
2215 pmc->mca_sfcount[sfmode]--;
2218 for (i = 0; i < sfcount; i++) {
2219 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2221 changerec |= rv > 0;
2225 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
2226 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
2227 pmc->mca_sfcount[MCAST_INCLUDE]) {
2228 struct ip6_sf_list *psf;
2230 /* filter mode change */
2231 pmc->mca_sfmode = MCAST_INCLUDE;
2232 pmc->mca_crcount = idev->mc_qrv;
2233 idev->mc_ifc_count = pmc->mca_crcount;
2234 for_each_psf_rtnl(pmc, psf)
2235 psf->sf_crcount = 0;
2236 mld_ifc_event(pmc->idev);
2237 } else if (sf_setstate(pmc) || changerec)
2238 mld_ifc_event(pmc->idev);
2239 spin_unlock_bh(&pmc->mca_lock);
2240 read_unlock_bh(&idev->lock);
2245 * Add multicast single-source filter to the interface list
2247 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
2248 const struct in6_addr *psfsrc)
2250 struct ip6_sf_list *psf, *psf_prev;
2253 for_each_psf_rtnl(pmc, psf) {
2254 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2259 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
2263 psf->sf_addr = *psfsrc;
2265 rcu_assign_pointer(psf_prev->sf_next, psf);
2267 rcu_assign_pointer(pmc->mca_sources, psf);
2270 psf->sf_count[sfmode]++;
2274 static void sf_markstate(struct ifmcaddr6 *pmc)
2276 struct ip6_sf_list *psf;
2277 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2279 for_each_psf_rtnl(pmc, psf) {
2280 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2281 psf->sf_oldin = mca_xcount ==
2282 psf->sf_count[MCAST_EXCLUDE] &&
2283 !psf->sf_count[MCAST_INCLUDE];
2285 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2290 static int sf_setstate(struct ifmcaddr6 *pmc)
2292 struct ip6_sf_list *psf, *dpsf;
2293 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2294 int qrv = pmc->idev->mc_qrv;
2298 for_each_psf_rtnl(pmc, psf) {
2299 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2300 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2301 !psf->sf_count[MCAST_INCLUDE];
2303 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2305 if (!psf->sf_oldin) {
2306 struct ip6_sf_list *prev = NULL;
2308 for_each_psf_tomb(pmc, dpsf) {
2309 if (ipv6_addr_equal(&dpsf->sf_addr,
2316 rcu_assign_pointer(prev->sf_next,
2317 rtnl_dereference(dpsf->sf_next));
2319 rcu_assign_pointer(pmc->mca_tomb,
2320 rtnl_dereference(dpsf->sf_next));
2321 kfree_rcu(dpsf, rcu);
2323 psf->sf_crcount = qrv;
2326 } else if (psf->sf_oldin) {
2327 psf->sf_crcount = 0;
2329 * add or update "delete" records if an active filter
2333 for_each_psf_tomb(pmc, dpsf)
2334 if (ipv6_addr_equal(&dpsf->sf_addr,
2338 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2342 rcu_assign_pointer(dpsf->sf_next,
2343 rtnl_dereference(pmc->mca_tomb));
2344 rcu_assign_pointer(pmc->mca_tomb, dpsf);
2346 dpsf->sf_crcount = qrv;
2354 * Add multicast source filter list to the interface list
2356 static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2357 int sfmode, int sfcount, const struct in6_addr *psfsrc,
2360 struct ifmcaddr6 *pmc;
2366 read_lock_bh(&idev->lock);
2367 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
2368 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2372 /* MCA not found?? bug */
2373 read_unlock_bh(&idev->lock);
2376 spin_lock_bh(&pmc->mca_lock);
2379 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2381 pmc->mca_sfcount[sfmode]++;
2383 for (i = 0; i < sfcount; i++) {
2384 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2392 pmc->mca_sfcount[sfmode]--;
2393 for (j = 0; j < i; j++)
2394 ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2395 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2396 struct ip6_sf_list *psf;
2398 /* filter mode change */
2399 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2400 pmc->mca_sfmode = MCAST_EXCLUDE;
2401 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2402 pmc->mca_sfmode = MCAST_INCLUDE;
2403 /* else no filters; keep old mode for reports */
2405 pmc->mca_crcount = idev->mc_qrv;
2406 idev->mc_ifc_count = pmc->mca_crcount;
2407 for_each_psf_rtnl(pmc, psf)
2408 psf->sf_crcount = 0;
2409 mld_ifc_event(idev);
2410 } else if (sf_setstate(pmc))
2411 mld_ifc_event(idev);
2412 spin_unlock_bh(&pmc->mca_lock);
2413 read_unlock_bh(&idev->lock);
2417 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2419 struct ip6_sf_list *psf, *nextpsf;
2421 for (psf = rtnl_dereference(pmc->mca_tomb);
2424 nextpsf = rtnl_dereference(psf->sf_next);
2425 kfree_rcu(psf, rcu);
2427 RCU_INIT_POINTER(pmc->mca_tomb, NULL);
2428 for (psf = rtnl_dereference(pmc->mca_sources);
2431 nextpsf = rtnl_dereference(psf->sf_next);
2432 kfree_rcu(psf, rcu);
2434 RCU_INIT_POINTER(pmc->mca_sources, NULL);
2435 pmc->mca_sfmode = MCAST_EXCLUDE;
2436 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2437 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2441 static void igmp6_join_group(struct ifmcaddr6 *ma)
2443 unsigned long delay;
2445 if (ma->mca_flags & MAF_NOREPORT)
2448 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2450 delay = prandom_u32() % unsolicited_report_interval(ma->idev);
2452 spin_lock_bh(&ma->mca_lock);
2453 if (cancel_delayed_work(&ma->mca_work)) {
2454 refcount_dec(&ma->mca_refcnt);
2455 delay = ma->mca_work.timer.expires - jiffies;
2458 if (!mod_delayed_work(mld_wq, &ma->mca_work, delay))
2459 refcount_inc(&ma->mca_refcnt);
2460 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2461 spin_unlock_bh(&ma->mca_lock);
2464 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2465 struct inet6_dev *idev)
2467 struct ip6_sf_socklist *psl;
2470 psl = rtnl_dereference(iml->sflist);
2473 /* any-source empty exclude case */
2474 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2476 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2477 psl->sl_count, psl->sl_addr, 0);
2478 RCU_INIT_POINTER(iml->sflist, NULL);
2479 atomic_sub(IP6_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2480 kfree_rcu(psl, rcu);
2485 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2487 if (mld_in_v1_mode(ma->idev)) {
2488 if (ma->mca_flags & MAF_LAST_REPORTER)
2489 igmp6_send(&ma->mca_addr, ma->idev->dev,
2490 ICMPV6_MGM_REDUCTION);
2492 mld_add_delrec(ma->idev, ma);
2493 mld_ifc_event(ma->idev);
2497 static void mld_gq_work(struct work_struct *work)
2499 struct inet6_dev *idev = container_of(to_delayed_work(work),
2503 idev->mc_gq_running = 0;
2504 mld_send_report(idev, NULL);
2508 static void mld_ifc_work(struct work_struct *work)
2510 struct inet6_dev *idev = container_of(to_delayed_work(work),
2515 if (idev->mc_ifc_count) {
2516 idev->mc_ifc_count--;
2517 if (idev->mc_ifc_count)
2518 mld_ifc_start_work(idev,
2519 unsolicited_report_interval(idev));
2524 static void mld_ifc_event(struct inet6_dev *idev)
2526 if (mld_in_v1_mode(idev))
2528 idev->mc_ifc_count = idev->mc_qrv;
2529 mld_ifc_start_work(idev, 1);
2532 static void mld_mca_work(struct work_struct *work)
2534 struct ifmcaddr6 *ma = container_of(to_delayed_work(work),
2535 struct ifmcaddr6, mca_work);
2537 if (mld_in_v1_mode(ma->idev))
2538 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2540 mld_send_report(ma->idev, ma);
2542 spin_lock_bh(&ma->mca_lock);
2543 ma->mca_flags |= MAF_LAST_REPORTER;
2544 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2545 spin_unlock_bh(&ma->mca_lock);
2549 /* Device changing type */
2551 void ipv6_mc_unmap(struct inet6_dev *idev)
2553 struct ifmcaddr6 *i;
2555 /* Install multicast list, except for all-nodes (already installed) */
2557 read_lock_bh(&idev->lock);
2558 for (i = idev->mc_list; i; i = i->next)
2559 igmp6_group_dropped(i);
2560 read_unlock_bh(&idev->lock);
2563 void ipv6_mc_remap(struct inet6_dev *idev)
2568 /* Device going down */
2570 void ipv6_mc_down(struct inet6_dev *idev)
2572 struct ifmcaddr6 *i;
2574 /* Withdraw multicast list */
2576 read_lock_bh(&idev->lock);
2578 for (i = idev->mc_list; i; i = i->next)
2579 igmp6_group_dropped(i);
2581 /* Should stop work after group drop. or we will
2582 * start work again in mld_ifc_event()
2584 mld_ifc_stop_work(idev);
2585 mld_gq_stop_work(idev);
2586 mld_dad_stop_work(idev);
2587 read_unlock_bh(&idev->lock);
2590 static void ipv6_mc_reset(struct inet6_dev *idev)
2592 idev->mc_qrv = sysctl_mld_qrv;
2593 idev->mc_qi = MLD_QI_DEFAULT;
2594 idev->mc_qri = MLD_QRI_DEFAULT;
2595 idev->mc_v1_seen = 0;
2596 idev->mc_maxdelay = unsolicited_report_interval(idev);
2599 /* Device going up */
2601 void ipv6_mc_up(struct inet6_dev *idev)
2603 struct ifmcaddr6 *i;
2605 /* Install multicast list, except for all-nodes (already installed) */
2607 read_lock_bh(&idev->lock);
2608 ipv6_mc_reset(idev);
2609 for (i = idev->mc_list; i; i = i->next) {
2610 mld_del_delrec(idev, i);
2611 igmp6_group_added(i);
2613 read_unlock_bh(&idev->lock);
2616 /* IPv6 device initialization. */
2618 void ipv6_mc_init_dev(struct inet6_dev *idev)
2620 write_lock_bh(&idev->lock);
2621 idev->mc_gq_running = 0;
2622 INIT_DELAYED_WORK(&idev->mc_gq_work, mld_gq_work);
2623 idev->mc_tomb = NULL;
2624 idev->mc_ifc_count = 0;
2625 INIT_DELAYED_WORK(&idev->mc_ifc_work, mld_ifc_work);
2626 INIT_DELAYED_WORK(&idev->mc_dad_work, mld_dad_work);
2627 ipv6_mc_reset(idev);
2628 write_unlock_bh(&idev->lock);
2632 * Device is about to be destroyed: clean up.
2635 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2637 struct ifmcaddr6 *i;
2639 /* Deactivate works */
2641 mld_clear_delrec(idev);
2643 /* Delete all-nodes address. */
2644 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2645 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2648 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
2650 if (idev->cnf.forwarding)
2651 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
2653 write_lock_bh(&idev->lock);
2654 while ((i = idev->mc_list) != NULL) {
2655 idev->mc_list = i->next;
2657 write_unlock_bh(&idev->lock);
2658 ip6_mc_clear_src(i);
2660 write_lock_bh(&idev->lock);
2662 write_unlock_bh(&idev->lock);
2665 static void ipv6_mc_rejoin_groups(struct inet6_dev *idev)
2667 struct ifmcaddr6 *pmc;
2671 if (mld_in_v1_mode(idev)) {
2672 read_lock_bh(&idev->lock);
2673 for (pmc = idev->mc_list; pmc; pmc = pmc->next)
2674 igmp6_join_group(pmc);
2675 read_unlock_bh(&idev->lock);
2677 mld_send_report(idev, NULL);
2680 static int ipv6_mc_netdev_event(struct notifier_block *this,
2681 unsigned long event,
2684 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2685 struct inet6_dev *idev = __in6_dev_get(dev);
2688 case NETDEV_RESEND_IGMP:
2690 ipv6_mc_rejoin_groups(idev);
2699 static struct notifier_block igmp6_netdev_notifier = {
2700 .notifier_call = ipv6_mc_netdev_event,
2703 #ifdef CONFIG_PROC_FS
2704 struct igmp6_mc_iter_state {
2705 struct seq_net_private p;
2706 struct net_device *dev;
2707 struct inet6_dev *idev;
2710 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2712 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2714 struct ifmcaddr6 *im = NULL;
2715 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2716 struct net *net = seq_file_net(seq);
2719 for_each_netdev_rcu(net, state->dev) {
2720 struct inet6_dev *idev;
2721 idev = __in6_dev_get(state->dev);
2724 read_lock_bh(&idev->lock);
2730 read_unlock_bh(&idev->lock);
2735 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2737 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2741 if (likely(state->idev))
2742 read_unlock_bh(&state->idev->lock);
2744 state->dev = next_net_device_rcu(state->dev);
2749 state->idev = __in6_dev_get(state->dev);
2752 read_lock_bh(&state->idev->lock);
2753 im = state->idev->mc_list;
2758 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2760 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2762 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2764 return pos ? NULL : im;
2767 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2771 return igmp6_mc_get_idx(seq, *pos);
2774 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2776 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2782 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2785 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2787 if (likely(state->idev)) {
2788 read_unlock_bh(&state->idev->lock);
2795 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2797 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2798 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2801 "%-4d %-15s %pi6 %5d %08X %ld\n",
2802 state->dev->ifindex, state->dev->name,
2804 im->mca_users, im->mca_flags,
2805 (im->mca_flags&MAF_TIMER_RUNNING) ?
2806 jiffies_to_clock_t(im->mca_work.timer.expires - jiffies) : 0);
2810 static const struct seq_operations igmp6_mc_seq_ops = {
2811 .start = igmp6_mc_seq_start,
2812 .next = igmp6_mc_seq_next,
2813 .stop = igmp6_mc_seq_stop,
2814 .show = igmp6_mc_seq_show,
2817 struct igmp6_mcf_iter_state {
2818 struct seq_net_private p;
2819 struct net_device *dev;
2820 struct inet6_dev *idev;
2821 struct ifmcaddr6 *im;
2824 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2826 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2828 struct ip6_sf_list *psf = NULL;
2829 struct ifmcaddr6 *im = NULL;
2830 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2831 struct net *net = seq_file_net(seq);
2835 for_each_netdev_rcu(net, state->dev) {
2836 struct inet6_dev *idev;
2837 idev = __in6_dev_get(state->dev);
2838 if (unlikely(idev == NULL))
2840 read_lock_bh(&idev->lock);
2843 spin_lock_bh(&im->mca_lock);
2844 psf = rcu_dereference(im->mca_sources);
2850 spin_unlock_bh(&im->mca_lock);
2852 read_unlock_bh(&idev->lock);
2857 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2859 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2861 psf = rcu_dereference(psf->sf_next);
2863 spin_unlock_bh(&state->im->mca_lock);
2864 state->im = state->im->next;
2865 while (!state->im) {
2866 if (likely(state->idev))
2867 read_unlock_bh(&state->idev->lock);
2869 state->dev = next_net_device_rcu(state->dev);
2874 state->idev = __in6_dev_get(state->dev);
2877 read_lock_bh(&state->idev->lock);
2878 state->im = state->idev->mc_list;
2882 spin_lock_bh(&state->im->mca_lock);
2883 psf = rcu_dereference(state->im->mca_sources);
2889 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2891 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2893 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2895 return pos ? NULL : psf;
2898 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2902 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2905 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2907 struct ip6_sf_list *psf;
2908 if (v == SEQ_START_TOKEN)
2909 psf = igmp6_mcf_get_first(seq);
2911 psf = igmp6_mcf_get_next(seq, v);
2916 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2919 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2920 if (likely(state->im)) {
2921 spin_unlock_bh(&state->im->mca_lock);
2924 if (likely(state->idev)) {
2925 read_unlock_bh(&state->idev->lock);
2932 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2934 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2935 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2937 if (v == SEQ_START_TOKEN) {
2938 seq_puts(seq, "Idx Device Multicast Address Source Address INC EXC\n");
2941 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
2942 state->dev->ifindex, state->dev->name,
2943 &state->im->mca_addr,
2945 psf->sf_count[MCAST_INCLUDE],
2946 psf->sf_count[MCAST_EXCLUDE]);
2951 static const struct seq_operations igmp6_mcf_seq_ops = {
2952 .start = igmp6_mcf_seq_start,
2953 .next = igmp6_mcf_seq_next,
2954 .stop = igmp6_mcf_seq_stop,
2955 .show = igmp6_mcf_seq_show,
2958 static int __net_init igmp6_proc_init(struct net *net)
2963 if (!proc_create_net("igmp6", 0444, net->proc_net, &igmp6_mc_seq_ops,
2964 sizeof(struct igmp6_mc_iter_state)))
2966 if (!proc_create_net("mcfilter6", 0444, net->proc_net,
2968 sizeof(struct igmp6_mcf_iter_state)))
2969 goto out_proc_net_igmp6;
2976 remove_proc_entry("igmp6", net->proc_net);
2980 static void __net_exit igmp6_proc_exit(struct net *net)
2982 remove_proc_entry("mcfilter6", net->proc_net);
2983 remove_proc_entry("igmp6", net->proc_net);
2986 static inline int igmp6_proc_init(struct net *net)
2990 static inline void igmp6_proc_exit(struct net *net)
2995 static int __net_init igmp6_net_init(struct net *net)
2999 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
3000 SOCK_RAW, IPPROTO_ICMPV6, net);
3002 pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
3007 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
3009 err = inet_ctl_sock_create(&net->ipv6.mc_autojoin_sk, PF_INET6,
3010 SOCK_RAW, IPPROTO_ICMPV6, net);
3012 pr_err("Failed to initialize the IGMP6 autojoin socket (err %d)\n",
3014 goto out_sock_create;
3017 err = igmp6_proc_init(net);
3019 goto out_sock_create_autojoin;
3023 out_sock_create_autojoin:
3024 inet_ctl_sock_destroy(net->ipv6.mc_autojoin_sk);
3026 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
3031 static void __net_exit igmp6_net_exit(struct net *net)
3033 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
3034 inet_ctl_sock_destroy(net->ipv6.mc_autojoin_sk);
3035 igmp6_proc_exit(net);
3038 static struct pernet_operations igmp6_net_ops = {
3039 .init = igmp6_net_init,
3040 .exit = igmp6_net_exit,
3043 int __init igmp6_init(void)
3047 err = register_pernet_subsys(&igmp6_net_ops);
3051 mld_wq = create_workqueue("mld");
3053 unregister_pernet_subsys(&igmp6_net_ops);
3060 int __init igmp6_late_init(void)
3062 return register_netdevice_notifier(&igmp6_netdev_notifier);
3065 void igmp6_cleanup(void)
3067 unregister_pernet_subsys(&igmp6_net_ops);
3068 destroy_workqueue(mld_wq);
3071 void igmp6_late_cleanup(void)
3073 unregister_netdevice_notifier(&igmp6_netdev_notifier);