2 * Multicast support for IPv6
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
18 * yoshfuji : fix format of router-alert option
19 * YOSHIFUJI Hideaki @USAGI:
20 * Fixed source address for MLD message based on
21 * <draft-ietf-magma-mld-source-05.txt>.
22 * YOSHIFUJI Hideaki @USAGI:
23 * - Ignore Queries for invalid addresses.
24 * - MLD for link-local addresses.
25 * David L Stevens <dlstevens@us.ibm.com>:
29 #include <linux/module.h>
30 #include <linux/errno.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/socket.h>
34 #include <linux/sockios.h>
35 #include <linux/jiffies.h>
36 #include <linux/times.h>
37 #include <linux/net.h>
39 #include <linux/in6.h>
40 #include <linux/netdevice.h>
41 #include <linux/if_arp.h>
42 #include <linux/route.h>
43 #include <linux/init.h>
44 #include <linux/proc_fs.h>
45 #include <linux/seq_file.h>
46 #include <linux/slab.h>
47 #include <linux/pkt_sched.h>
50 #include <linux/netfilter.h>
51 #include <linux/netfilter_ipv6.h>
53 #include <net/net_namespace.h>
58 #include <net/protocol.h>
59 #include <net/if_inet6.h>
60 #include <net/ndisc.h>
61 #include <net/addrconf.h>
62 #include <net/ip6_route.h>
63 #include <net/inet_common.h>
65 #include <net/ip6_checksum.h>
67 /* Set to 3 to get tracing... */
71 #define MDBG(x) printk x
76 /* Ensure that we have struct in6_addr aligned on 32bit word. */
77 static void *__mld2_query_bugs[] __attribute__((__unused__)) = {
78 BUILD_BUG_ON_NULL(offsetof(struct mld2_query, mld2q_srcs) % 4),
79 BUILD_BUG_ON_NULL(offsetof(struct mld2_report, mld2r_grec) % 4),
80 BUILD_BUG_ON_NULL(offsetof(struct mld2_grec, grec_mca) % 4)
83 static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
85 /* Big mc list lock for all the sockets */
86 static DEFINE_SPINLOCK(ipv6_sk_mc_lock);
88 static void igmp6_join_group(struct ifmcaddr6 *ma);
89 static void igmp6_leave_group(struct ifmcaddr6 *ma);
90 static void igmp6_timer_handler(unsigned long data);
92 static void mld_gq_timer_expire(unsigned long data);
93 static void mld_ifc_timer_expire(unsigned long data);
94 static void mld_ifc_event(struct inet6_dev *idev);
95 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
96 static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *addr);
97 static void mld_clear_delrec(struct inet6_dev *idev);
98 static int sf_setstate(struct ifmcaddr6 *pmc);
99 static void sf_markstate(struct ifmcaddr6 *pmc);
100 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
101 static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
102 int sfmode, int sfcount, const struct in6_addr *psfsrc,
104 static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
105 int sfmode, int sfcount, const struct in6_addr *psfsrc,
107 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
108 struct inet6_dev *idev);
111 #define MLD_QRV_DEFAULT 2
113 #define MLD_V1_SEEN(idev) (dev_net((idev)->dev)->ipv6.devconf_all->force_mld_version == 1 || \
114 (idev)->cnf.force_mld_version == 1 || \
115 ((idev)->mc_v1_seen && \
116 time_before(jiffies, (idev)->mc_v1_seen)))
118 #define IPV6_MLD_MAX_MSF 64
120 int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
123 * socket join on multicast group
126 #define for_each_pmc_rcu(np, pmc) \
127 for (pmc = rcu_dereference(np->ipv6_mc_list); \
129 pmc = rcu_dereference(pmc->next))
131 static int unsolicited_report_interval(struct inet6_dev *idev)
135 if (MLD_V1_SEEN(idev))
136 iv = idev->cnf.mldv1_unsolicited_report_interval;
138 iv = idev->cnf.mldv2_unsolicited_report_interval;
140 return iv > 0 ? iv : 1;
143 int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
145 struct net_device *dev = NULL;
146 struct ipv6_mc_socklist *mc_lst;
147 struct ipv6_pinfo *np = inet6_sk(sk);
148 struct net *net = sock_net(sk);
151 if (!ipv6_addr_is_multicast(addr))
155 for_each_pmc_rcu(np, mc_lst) {
156 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
157 ipv6_addr_equal(&mc_lst->addr, addr)) {
164 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
170 mc_lst->addr = *addr;
175 rt = rt6_lookup(net, addr, NULL, 0, 0);
181 dev = dev_get_by_index_rcu(net, ifindex);
185 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
189 mc_lst->ifindex = dev->ifindex;
190 mc_lst->sfmode = MCAST_EXCLUDE;
191 rwlock_init(&mc_lst->sflock);
192 mc_lst->sflist = NULL;
195 * now add/increase the group membership on the device
198 err = ipv6_dev_mc_inc(dev, addr);
202 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
206 spin_lock(&ipv6_sk_mc_lock);
207 mc_lst->next = np->ipv6_mc_list;
208 rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
209 spin_unlock(&ipv6_sk_mc_lock);
217 * socket leave on multicast group
219 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
221 struct ipv6_pinfo *np = inet6_sk(sk);
222 struct ipv6_mc_socklist *mc_lst;
223 struct ipv6_mc_socklist __rcu **lnk;
224 struct net *net = sock_net(sk);
226 if (!ipv6_addr_is_multicast(addr))
229 spin_lock(&ipv6_sk_mc_lock);
230 for (lnk = &np->ipv6_mc_list;
231 (mc_lst = rcu_dereference_protected(*lnk,
232 lockdep_is_held(&ipv6_sk_mc_lock))) !=NULL ;
233 lnk = &mc_lst->next) {
234 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
235 ipv6_addr_equal(&mc_lst->addr, addr)) {
236 struct net_device *dev;
239 spin_unlock(&ipv6_sk_mc_lock);
242 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
244 struct inet6_dev *idev = __in6_dev_get(dev);
246 (void) ip6_mc_leave_src(sk, mc_lst, idev);
248 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
250 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
252 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
253 kfree_rcu(mc_lst, rcu);
257 spin_unlock(&ipv6_sk_mc_lock);
259 return -EADDRNOTAVAIL;
262 /* called with rcu_read_lock() */
263 static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
264 const struct in6_addr *group,
267 struct net_device *dev = NULL;
268 struct inet6_dev *idev = NULL;
271 struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, 0);
278 dev = dev_get_by_index_rcu(net, ifindex);
282 idev = __in6_dev_get(dev);
285 read_lock_bh(&idev->lock);
287 read_unlock_bh(&idev->lock);
293 void ipv6_sock_mc_close(struct sock *sk)
295 struct ipv6_pinfo *np = inet6_sk(sk);
296 struct ipv6_mc_socklist *mc_lst;
297 struct net *net = sock_net(sk);
299 if (!rcu_access_pointer(np->ipv6_mc_list))
302 spin_lock(&ipv6_sk_mc_lock);
303 while ((mc_lst = rcu_dereference_protected(np->ipv6_mc_list,
304 lockdep_is_held(&ipv6_sk_mc_lock))) != NULL) {
305 struct net_device *dev;
307 np->ipv6_mc_list = mc_lst->next;
308 spin_unlock(&ipv6_sk_mc_lock);
311 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
313 struct inet6_dev *idev = __in6_dev_get(dev);
315 (void) ip6_mc_leave_src(sk, mc_lst, idev);
317 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
319 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
322 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
323 kfree_rcu(mc_lst, rcu);
325 spin_lock(&ipv6_sk_mc_lock);
327 spin_unlock(&ipv6_sk_mc_lock);
330 int ip6_mc_source(int add, int omode, struct sock *sk,
331 struct group_source_req *pgsr)
333 struct in6_addr *source, *group;
334 struct ipv6_mc_socklist *pmc;
335 struct inet6_dev *idev;
336 struct ipv6_pinfo *inet6 = inet6_sk(sk);
337 struct ip6_sf_socklist *psl;
338 struct net *net = sock_net(sk);
344 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
345 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
347 if (!ipv6_addr_is_multicast(group))
351 idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface);
357 err = -EADDRNOTAVAIL;
359 for_each_pmc_rcu(inet6, pmc) {
360 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
362 if (ipv6_addr_equal(&pmc->addr, group))
365 if (!pmc) { /* must have a prior join */
369 /* if a source filter was set, must be the same mode as before */
371 if (pmc->sfmode != omode) {
375 } else if (pmc->sfmode != omode) {
376 /* allow mode switches for empty-set filters */
377 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
378 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
382 write_lock(&pmc->sflock);
388 goto done; /* err = -EADDRNOTAVAIL */
390 for (i=0; i<psl->sl_count; i++) {
391 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
395 if (rv) /* source not found */
396 goto done; /* err = -EADDRNOTAVAIL */
398 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
399 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
404 /* update the interface filter */
405 ip6_mc_del_src(idev, group, omode, 1, source, 1);
407 for (j=i+1; j<psl->sl_count; j++)
408 psl->sl_addr[j-1] = psl->sl_addr[j];
413 /* else, add a new source to the filter */
415 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
419 if (!psl || psl->sl_count == psl->sl_max) {
420 struct ip6_sf_socklist *newpsl;
421 int count = IP6_SFBLOCK;
424 count += psl->sl_max;
425 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
430 newpsl->sl_max = count;
431 newpsl->sl_count = count - IP6_SFBLOCK;
433 for (i=0; i<psl->sl_count; i++)
434 newpsl->sl_addr[i] = psl->sl_addr[i];
435 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
437 pmc->sflist = psl = newpsl;
439 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
440 for (i=0; i<psl->sl_count; i++) {
441 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
442 if (rv == 0) /* There is an error in the address. */
445 for (j=psl->sl_count-1; j>=i; j--)
446 psl->sl_addr[j+1] = psl->sl_addr[j];
447 psl->sl_addr[i] = *source;
450 /* update the interface list */
451 ip6_mc_add_src(idev, group, omode, 1, source, 1);
454 write_unlock(&pmc->sflock);
455 read_unlock_bh(&idev->lock);
458 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
462 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
464 const struct in6_addr *group;
465 struct ipv6_mc_socklist *pmc;
466 struct inet6_dev *idev;
467 struct ipv6_pinfo *inet6 = inet6_sk(sk);
468 struct ip6_sf_socklist *newpsl, *psl;
469 struct net *net = sock_net(sk);
473 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
475 if (!ipv6_addr_is_multicast(group))
477 if (gsf->gf_fmode != MCAST_INCLUDE &&
478 gsf->gf_fmode != MCAST_EXCLUDE)
482 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
491 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
496 for_each_pmc_rcu(inet6, pmc) {
497 if (pmc->ifindex != gsf->gf_interface)
499 if (ipv6_addr_equal(&pmc->addr, group))
502 if (!pmc) { /* must have a prior join */
506 if (gsf->gf_numsrc) {
507 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
513 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
514 for (i=0; i<newpsl->sl_count; ++i) {
515 struct sockaddr_in6 *psin6;
517 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
518 newpsl->sl_addr[i] = psin6->sin6_addr;
520 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
521 newpsl->sl_count, newpsl->sl_addr, 0);
523 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
528 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
531 write_lock(&pmc->sflock);
534 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
535 psl->sl_count, psl->sl_addr, 0);
536 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
538 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
539 pmc->sflist = newpsl;
540 pmc->sfmode = gsf->gf_fmode;
541 write_unlock(&pmc->sflock);
544 read_unlock_bh(&idev->lock);
547 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
551 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
552 struct group_filter __user *optval, int __user *optlen)
554 int err, i, count, copycount;
555 const struct in6_addr *group;
556 struct ipv6_mc_socklist *pmc;
557 struct inet6_dev *idev;
558 struct ipv6_pinfo *inet6 = inet6_sk(sk);
559 struct ip6_sf_socklist *psl;
560 struct net *net = sock_net(sk);
562 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
564 if (!ipv6_addr_is_multicast(group))
568 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
575 err = -EADDRNOTAVAIL;
577 * changes to the ipv6_mc_list require the socket lock and
578 * a read lock on ip6_sk_mc_lock. We have the socket lock,
579 * so reading the list is safe.
582 for_each_pmc_rcu(inet6, pmc) {
583 if (pmc->ifindex != gsf->gf_interface)
585 if (ipv6_addr_equal(group, &pmc->addr))
588 if (!pmc) /* must have a prior join */
590 gsf->gf_fmode = pmc->sfmode;
592 count = psl ? psl->sl_count : 0;
593 read_unlock_bh(&idev->lock);
596 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
597 gsf->gf_numsrc = count;
598 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
599 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
602 /* changes to psl require the socket lock, a read lock on
603 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
604 * have the socket lock, so reading here is safe.
606 for (i=0; i<copycount; i++) {
607 struct sockaddr_in6 *psin6;
608 struct sockaddr_storage ss;
610 psin6 = (struct sockaddr_in6 *)&ss;
611 memset(&ss, 0, sizeof(ss));
612 psin6->sin6_family = AF_INET6;
613 psin6->sin6_addr = psl->sl_addr[i];
614 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
619 read_unlock_bh(&idev->lock);
624 bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
625 const struct in6_addr *src_addr)
627 struct ipv6_pinfo *np = inet6_sk(sk);
628 struct ipv6_mc_socklist *mc;
629 struct ip6_sf_socklist *psl;
633 for_each_pmc_rcu(np, mc) {
634 if (ipv6_addr_equal(&mc->addr, mc_addr))
641 read_lock(&mc->sflock);
644 rv = mc->sfmode == MCAST_EXCLUDE;
648 for (i=0; i<psl->sl_count; i++) {
649 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
652 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
654 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
657 read_unlock(&mc->sflock);
663 static void ma_put(struct ifmcaddr6 *mc)
665 if (atomic_dec_and_test(&mc->mca_refcnt)) {
666 in6_dev_put(mc->idev);
671 static void igmp6_group_added(struct ifmcaddr6 *mc)
673 struct net_device *dev = mc->idev->dev;
674 char buf[MAX_ADDR_LEN];
676 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
677 IPV6_ADDR_SCOPE_LINKLOCAL)
680 spin_lock_bh(&mc->mca_lock);
681 if (!(mc->mca_flags&MAF_LOADED)) {
682 mc->mca_flags |= MAF_LOADED;
683 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
684 dev_mc_add(dev, buf);
686 spin_unlock_bh(&mc->mca_lock);
688 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
691 if (MLD_V1_SEEN(mc->idev)) {
692 igmp6_join_group(mc);
697 mc->mca_crcount = mc->idev->mc_qrv;
698 mld_ifc_event(mc->idev);
701 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
703 struct net_device *dev = mc->idev->dev;
704 char buf[MAX_ADDR_LEN];
706 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
707 IPV6_ADDR_SCOPE_LINKLOCAL)
710 spin_lock_bh(&mc->mca_lock);
711 if (mc->mca_flags&MAF_LOADED) {
712 mc->mca_flags &= ~MAF_LOADED;
713 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
714 dev_mc_del(dev, buf);
717 if (mc->mca_flags & MAF_NOREPORT)
719 spin_unlock_bh(&mc->mca_lock);
722 igmp6_leave_group(mc);
724 spin_lock_bh(&mc->mca_lock);
725 if (del_timer(&mc->mca_timer))
726 atomic_dec(&mc->mca_refcnt);
728 ip6_mc_clear_src(mc);
729 spin_unlock_bh(&mc->mca_lock);
733 * deleted ifmcaddr6 manipulation
735 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
737 struct ifmcaddr6 *pmc;
739 /* this is an "ifmcaddr6" for convenience; only the fields below
740 * are actually used. In particular, the refcnt and users are not
741 * used for management of the delete list. Using the same structure
742 * for deleted items allows change reports to use common code with
743 * non-deleted or query-response MCA's.
745 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
749 spin_lock_bh(&im->mca_lock);
750 spin_lock_init(&pmc->mca_lock);
751 pmc->idev = im->idev;
753 pmc->mca_addr = im->mca_addr;
754 pmc->mca_crcount = idev->mc_qrv;
755 pmc->mca_sfmode = im->mca_sfmode;
756 if (pmc->mca_sfmode == MCAST_INCLUDE) {
757 struct ip6_sf_list *psf;
759 pmc->mca_tomb = im->mca_tomb;
760 pmc->mca_sources = im->mca_sources;
761 im->mca_tomb = im->mca_sources = NULL;
762 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
763 psf->sf_crcount = pmc->mca_crcount;
765 spin_unlock_bh(&im->mca_lock);
767 spin_lock_bh(&idev->mc_lock);
768 pmc->next = idev->mc_tomb;
770 spin_unlock_bh(&idev->mc_lock);
773 static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *pmca)
775 struct ifmcaddr6 *pmc, *pmc_prev;
776 struct ip6_sf_list *psf, *psf_next;
778 spin_lock_bh(&idev->mc_lock);
780 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
781 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
787 pmc_prev->next = pmc->next;
789 idev->mc_tomb = pmc->next;
791 spin_unlock_bh(&idev->mc_lock);
794 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
795 psf_next = psf->sf_next;
798 in6_dev_put(pmc->idev);
803 static void mld_clear_delrec(struct inet6_dev *idev)
805 struct ifmcaddr6 *pmc, *nextpmc;
807 spin_lock_bh(&idev->mc_lock);
809 idev->mc_tomb = NULL;
810 spin_unlock_bh(&idev->mc_lock);
812 for (; pmc; pmc = nextpmc) {
814 ip6_mc_clear_src(pmc);
815 in6_dev_put(pmc->idev);
819 /* clear dead sources, too */
820 read_lock_bh(&idev->lock);
821 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
822 struct ip6_sf_list *psf, *psf_next;
824 spin_lock_bh(&pmc->mca_lock);
826 pmc->mca_tomb = NULL;
827 spin_unlock_bh(&pmc->mca_lock);
828 for (; psf; psf=psf_next) {
829 psf_next = psf->sf_next;
833 read_unlock_bh(&idev->lock);
838 * device multicast group inc (add if not found)
840 int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
842 struct ifmcaddr6 *mc;
843 struct inet6_dev *idev;
845 /* we need to take a reference on idev */
846 idev = in6_dev_get(dev);
851 write_lock_bh(&idev->lock);
853 write_unlock_bh(&idev->lock);
858 for (mc = idev->mc_list; mc; mc = mc->next) {
859 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
861 write_unlock_bh(&idev->lock);
862 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
870 * not found: create a new one.
873 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
876 write_unlock_bh(&idev->lock);
881 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
883 mc->mca_addr = *addr;
884 mc->idev = idev; /* (reference taken) */
886 /* mca_stamp should be updated upon changes */
887 mc->mca_cstamp = mc->mca_tstamp = jiffies;
888 atomic_set(&mc->mca_refcnt, 2);
889 spin_lock_init(&mc->mca_lock);
891 /* initial mode is (EX, empty) */
892 mc->mca_sfmode = MCAST_EXCLUDE;
893 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
895 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
896 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
897 mc->mca_flags |= MAF_NOREPORT;
899 mc->next = idev->mc_list;
901 write_unlock_bh(&idev->lock);
903 mld_del_delrec(idev, &mc->mca_addr);
904 igmp6_group_added(mc);
910 * device multicast group del
912 int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
914 struct ifmcaddr6 *ma, **map;
916 write_lock_bh(&idev->lock);
917 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
918 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
919 if (--ma->mca_users == 0) {
921 write_unlock_bh(&idev->lock);
923 igmp6_group_dropped(ma);
928 write_unlock_bh(&idev->lock);
932 write_unlock_bh(&idev->lock);
937 int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
939 struct inet6_dev *idev;
944 idev = __in6_dev_get(dev);
948 err = __ipv6_dev_mc_dec(idev, addr);
955 * check if the interface/address pair is valid
957 bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
958 const struct in6_addr *src_addr)
960 struct inet6_dev *idev;
961 struct ifmcaddr6 *mc;
965 idev = __in6_dev_get(dev);
967 read_lock_bh(&idev->lock);
968 for (mc = idev->mc_list; mc; mc=mc->next) {
969 if (ipv6_addr_equal(&mc->mca_addr, group))
973 if (src_addr && !ipv6_addr_any(src_addr)) {
974 struct ip6_sf_list *psf;
976 spin_lock_bh(&mc->mca_lock);
977 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
978 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
982 rv = psf->sf_count[MCAST_INCLUDE] ||
983 psf->sf_count[MCAST_EXCLUDE] !=
984 mc->mca_sfcount[MCAST_EXCLUDE];
986 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
987 spin_unlock_bh(&mc->mca_lock);
989 rv = true; /* don't filter unspecified source */
991 read_unlock_bh(&idev->lock);
997 static void mld_gq_start_timer(struct inet6_dev *idev)
999 unsigned long tv = net_random() % idev->mc_maxdelay;
1001 idev->mc_gq_running = 1;
1002 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1006 static void mld_ifc_start_timer(struct inet6_dev *idev, unsigned long delay)
1008 unsigned long tv = net_random() % delay;
1010 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1014 static void mld_dad_start_timer(struct inet6_dev *idev, unsigned long delay)
1016 unsigned long tv = net_random() % delay;
1018 if (!mod_timer(&idev->mc_dad_timer, jiffies+tv+2))
1023 * IGMP handling (alias multicast ICMPv6 messages)
1026 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1028 unsigned long delay = resptime;
1030 /* Do not start timer for these addresses */
1031 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1032 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1035 if (del_timer(&ma->mca_timer)) {
1036 atomic_dec(&ma->mca_refcnt);
1037 delay = ma->mca_timer.expires - jiffies;
1040 if (delay >= resptime) {
1042 delay = net_random() % resptime;
1046 ma->mca_timer.expires = jiffies + delay;
1047 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1048 atomic_inc(&ma->mca_refcnt);
1049 ma->mca_flags |= MAF_TIMER_RUNNING;
1052 /* mark EXCLUDE-mode sources */
1053 static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1054 const struct in6_addr *srcs)
1056 struct ip6_sf_list *psf;
1060 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1061 if (scount == nsrcs)
1063 for (i=0; i<nsrcs; i++) {
1064 /* skip inactive filters */
1065 if (psf->sf_count[MCAST_INCLUDE] ||
1066 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1067 psf->sf_count[MCAST_EXCLUDE])
1069 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1075 pmc->mca_flags &= ~MAF_GSQUERY;
1076 if (scount == nsrcs) /* all sources excluded */
1081 static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1082 const struct in6_addr *srcs)
1084 struct ip6_sf_list *psf;
1087 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1088 return mld_xmarksources(pmc, nsrcs, srcs);
1090 /* mark INCLUDE-mode sources */
1093 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1094 if (scount == nsrcs)
1096 for (i=0; i<nsrcs; i++) {
1097 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1105 pmc->mca_flags &= ~MAF_GSQUERY;
1108 pmc->mca_flags |= MAF_GSQUERY;
1112 /* called with rcu_read_lock() */
1113 int igmp6_event_query(struct sk_buff *skb)
1115 struct mld2_query *mlh2 = NULL;
1116 struct ifmcaddr6 *ma;
1117 const struct in6_addr *group;
1118 unsigned long max_delay;
1119 struct inet6_dev *idev;
1120 struct mld_msg *mld;
1125 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1128 /* compute payload length excluding extension headers */
1129 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
1130 len -= skb_network_header_len(skb);
1132 /* Drop queries with not link local source */
1133 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
1136 idev = __in6_dev_get(skb->dev);
1141 mld = (struct mld_msg *)icmp6_hdr(skb);
1142 group = &mld->mld_mca;
1143 group_type = ipv6_addr_type(group);
1145 if (group_type != IPV6_ADDR_ANY &&
1146 !(group_type&IPV6_ADDR_MULTICAST))
1151 /* MLDv1 router present */
1153 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
1154 switchback = (idev->mc_qrv + 1) * max_delay;
1155 idev->mc_v1_seen = jiffies + switchback;
1157 /* cancel the interface change timer */
1158 idev->mc_ifc_count = 0;
1159 if (del_timer(&idev->mc_ifc_timer))
1160 __in6_dev_put(idev);
1161 /* clear deleted report items */
1162 mld_clear_delrec(idev);
1163 } else if (len >= 28) {
1164 int srcs_offset = sizeof(struct mld2_query) -
1165 sizeof(struct icmp6hdr);
1166 if (!pskb_may_pull(skb, srcs_offset))
1169 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1171 max_delay = max(msecs_to_jiffies(MLDV2_MRC(ntohs(mlh2->mld2q_mrc))), 1UL);
1173 idev->mc_maxdelay = max_delay;
1175 if (mlh2->mld2q_qrv)
1176 idev->mc_qrv = mlh2->mld2q_qrv;
1177 if (group_type == IPV6_ADDR_ANY) { /* general query */
1178 if (mlh2->mld2q_nsrcs)
1179 return -EINVAL; /* no sources allowed */
1181 mld_gq_start_timer(idev);
1184 /* mark sources to include, if group & source-specific */
1185 if (mlh2->mld2q_nsrcs != 0) {
1186 if (!pskb_may_pull(skb, srcs_offset +
1187 ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
1190 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1196 read_lock_bh(&idev->lock);
1197 if (group_type == IPV6_ADDR_ANY) {
1198 for (ma = idev->mc_list; ma; ma=ma->next) {
1199 spin_lock_bh(&ma->mca_lock);
1200 igmp6_group_queried(ma, max_delay);
1201 spin_unlock_bh(&ma->mca_lock);
1204 for (ma = idev->mc_list; ma; ma=ma->next) {
1205 if (!ipv6_addr_equal(group, &ma->mca_addr))
1207 spin_lock_bh(&ma->mca_lock);
1208 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1209 /* gsquery <- gsquery && mark */
1211 ma->mca_flags &= ~MAF_GSQUERY;
1213 /* gsquery <- mark */
1215 ma->mca_flags |= MAF_GSQUERY;
1217 ma->mca_flags &= ~MAF_GSQUERY;
1219 if (!(ma->mca_flags & MAF_GSQUERY) ||
1220 mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
1221 igmp6_group_queried(ma, max_delay);
1222 spin_unlock_bh(&ma->mca_lock);
1226 read_unlock_bh(&idev->lock);
1231 /* called with rcu_read_lock() */
1232 int igmp6_event_report(struct sk_buff *skb)
1234 struct ifmcaddr6 *ma;
1235 struct inet6_dev *idev;
1236 struct mld_msg *mld;
1239 /* Our own report looped back. Ignore it. */
1240 if (skb->pkt_type == PACKET_LOOPBACK)
1243 /* send our report if the MC router may not have heard this report */
1244 if (skb->pkt_type != PACKET_MULTICAST &&
1245 skb->pkt_type != PACKET_BROADCAST)
1248 if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
1251 mld = (struct mld_msg *)icmp6_hdr(skb);
1253 /* Drop reports with not link local source */
1254 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
1255 if (addr_type != IPV6_ADDR_ANY &&
1256 !(addr_type&IPV6_ADDR_LINKLOCAL))
1259 idev = __in6_dev_get(skb->dev);
1264 * Cancel the timer for this group
1267 read_lock_bh(&idev->lock);
1268 for (ma = idev->mc_list; ma; ma=ma->next) {
1269 if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
1270 spin_lock(&ma->mca_lock);
1271 if (del_timer(&ma->mca_timer))
1272 atomic_dec(&ma->mca_refcnt);
1273 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1274 spin_unlock(&ma->mca_lock);
1278 read_unlock_bh(&idev->lock);
1282 static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1283 int gdeleted, int sdeleted)
1286 case MLD2_MODE_IS_INCLUDE:
1287 case MLD2_MODE_IS_EXCLUDE:
1288 if (gdeleted || sdeleted)
1290 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1291 if (pmc->mca_sfmode == MCAST_INCLUDE)
1293 /* don't include if this source is excluded
1296 if (psf->sf_count[MCAST_INCLUDE])
1297 return type == MLD2_MODE_IS_INCLUDE;
1298 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1299 psf->sf_count[MCAST_EXCLUDE];
1302 case MLD2_CHANGE_TO_INCLUDE:
1303 if (gdeleted || sdeleted)
1305 return psf->sf_count[MCAST_INCLUDE] != 0;
1306 case MLD2_CHANGE_TO_EXCLUDE:
1307 if (gdeleted || sdeleted)
1309 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1310 psf->sf_count[MCAST_INCLUDE])
1312 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1313 psf->sf_count[MCAST_EXCLUDE];
1314 case MLD2_ALLOW_NEW_SOURCES:
1315 if (gdeleted || !psf->sf_crcount)
1317 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1318 case MLD2_BLOCK_OLD_SOURCES:
1319 if (pmc->mca_sfmode == MCAST_INCLUDE)
1320 return gdeleted || (psf->sf_crcount && sdeleted);
1321 return psf->sf_crcount && !gdeleted && !sdeleted;
1327 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1329 struct ip6_sf_list *psf;
1332 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1333 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1340 static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
1341 struct net_device *dev,
1342 const struct in6_addr *saddr,
1343 const struct in6_addr *daddr,
1346 struct ipv6hdr *hdr;
1348 skb->protocol = htons(ETH_P_IPV6);
1351 skb_reset_network_header(skb);
1352 skb_put(skb, sizeof(struct ipv6hdr));
1353 hdr = ipv6_hdr(skb);
1355 ip6_flow_hdr(hdr, 0, 0);
1357 hdr->payload_len = htons(len);
1358 hdr->nexthdr = proto;
1359 hdr->hop_limit = inet6_sk(sk)->hop_limit;
1361 hdr->saddr = *saddr;
1362 hdr->daddr = *daddr;
1365 static struct sk_buff *mld_newpack(struct inet6_dev *idev, int size)
1367 struct net_device *dev = idev->dev;
1368 struct net *net = dev_net(dev);
1369 struct sock *sk = net->ipv6.igmp_sk;
1370 struct sk_buff *skb;
1371 struct mld2_report *pmr;
1372 struct in6_addr addr_buf;
1373 const struct in6_addr *saddr;
1374 int hlen = LL_RESERVED_SPACE(dev);
1375 int tlen = dev->needed_tailroom;
1377 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1378 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1381 /* we assume size > sizeof(ra) here */
1382 size += hlen + tlen;
1383 /* limit our allocations to order-0 page */
1384 size = min_t(int, size, SKB_MAX_ORDER(0, 0));
1385 skb = sock_alloc_send_skb(sk, size, 1, &err);
1390 skb->priority = TC_PRIO_CONTROL;
1391 skb_reserve(skb, hlen);
1393 if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
1394 /* <draft-ietf-magma-mld-source-05.txt>:
1395 * use unspecified address as the source address
1396 * when a valid link-local address is not available.
1398 saddr = &in6addr_any;
1402 ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
1404 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1406 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
1407 skb_put(skb, sizeof(*pmr));
1408 pmr = (struct mld2_report *)skb_transport_header(skb);
1409 pmr->mld2r_type = ICMPV6_MLD2_REPORT;
1410 pmr->mld2r_resv1 = 0;
1411 pmr->mld2r_cksum = 0;
1412 pmr->mld2r_resv2 = 0;
1413 pmr->mld2r_ngrec = 0;
1417 static void mld_sendpack(struct sk_buff *skb)
1419 struct ipv6hdr *pip6 = ipv6_hdr(skb);
1420 struct mld2_report *pmr =
1421 (struct mld2_report *)skb_transport_header(skb);
1422 int payload_len, mldlen;
1423 struct inet6_dev *idev;
1424 struct net *net = dev_net(skb->dev);
1427 struct dst_entry *dst;
1430 idev = __in6_dev_get(skb->dev);
1431 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1433 payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
1435 mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
1436 pip6->payload_len = htons(payload_len);
1438 pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1440 csum_partial(skb_transport_header(skb),
1443 icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
1444 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1446 dst = icmp6_dst_alloc(skb->dev, &fl6);
1453 skb_dst_set(skb, dst);
1457 payload_len = skb->len;
1459 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1463 ICMP6MSGOUT_INC_STATS_BH(net, idev, ICMPV6_MLD2_REPORT);
1464 ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
1465 IP6_UPD_PO_STATS_BH(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
1467 IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTDISCARDS);
1477 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1479 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1482 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1483 int type, struct mld2_grec **ppgr)
1485 struct net_device *dev = pmc->idev->dev;
1486 struct mld2_report *pmr;
1487 struct mld2_grec *pgr;
1490 skb = mld_newpack(pmc->idev, dev->mtu);
1493 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1494 pgr->grec_type = type;
1495 pgr->grec_auxwords = 0;
1496 pgr->grec_nsrcs = 0;
1497 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1498 pmr = (struct mld2_report *)skb_transport_header(skb);
1499 pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
1504 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1505 skb_tailroom(skb)) : 0)
1507 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1508 int type, int gdeleted, int sdeleted)
1510 struct inet6_dev *idev = pmc->idev;
1511 struct net_device *dev = idev->dev;
1512 struct mld2_report *pmr;
1513 struct mld2_grec *pgr = NULL;
1514 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1515 int scount, stotal, first, isquery, truncate;
1517 if (pmc->mca_flags & MAF_NOREPORT)
1520 isquery = type == MLD2_MODE_IS_INCLUDE ||
1521 type == MLD2_MODE_IS_EXCLUDE;
1522 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1523 type == MLD2_CHANGE_TO_EXCLUDE;
1525 stotal = scount = 0;
1527 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1532 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
1534 /* EX and TO_EX get a fresh packet, if needed */
1536 if (pmr && pmr->mld2r_ngrec &&
1537 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1540 skb = mld_newpack(idev, dev->mtu);
1545 for (psf=*psf_list; psf; psf=psf_next) {
1546 struct in6_addr *psrc;
1548 psf_next = psf->sf_next;
1550 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1555 /* clear marks on query responses */
1559 if (AVAILABLE(skb) < sizeof(*psrc) +
1560 first*sizeof(struct mld2_grec)) {
1561 if (truncate && !first)
1562 break; /* truncate these */
1564 pgr->grec_nsrcs = htons(scount);
1567 skb = mld_newpack(idev, dev->mtu);
1572 skb = add_grhead(skb, pmc, type, &pgr);
1577 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1578 *psrc = psf->sf_addr;
1580 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1581 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1583 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1585 psf_prev->sf_next = psf->sf_next;
1587 *psf_list = psf->sf_next;
1597 if (type == MLD2_ALLOW_NEW_SOURCES ||
1598 type == MLD2_BLOCK_OLD_SOURCES)
1600 if (pmc->mca_crcount || isquery) {
1601 /* make sure we have room for group header */
1602 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1604 skb = NULL; /* add_grhead will get a new one */
1606 skb = add_grhead(skb, pmc, type, &pgr);
1610 pgr->grec_nsrcs = htons(scount);
1613 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1617 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1619 struct sk_buff *skb = NULL;
1622 read_lock_bh(&idev->lock);
1624 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1625 if (pmc->mca_flags & MAF_NOREPORT)
1627 spin_lock_bh(&pmc->mca_lock);
1628 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1629 type = MLD2_MODE_IS_EXCLUDE;
1631 type = MLD2_MODE_IS_INCLUDE;
1632 skb = add_grec(skb, pmc, type, 0, 0);
1633 spin_unlock_bh(&pmc->mca_lock);
1636 spin_lock_bh(&pmc->mca_lock);
1637 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1638 type = MLD2_MODE_IS_EXCLUDE;
1640 type = MLD2_MODE_IS_INCLUDE;
1641 skb = add_grec(skb, pmc, type, 0, 0);
1642 spin_unlock_bh(&pmc->mca_lock);
1644 read_unlock_bh(&idev->lock);
1650 * remove zero-count source records from a source filter list
1652 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1654 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1657 for (psf=*ppsf; psf; psf = psf_next) {
1658 psf_next = psf->sf_next;
1659 if (psf->sf_crcount == 0) {
1661 psf_prev->sf_next = psf->sf_next;
1663 *ppsf = psf->sf_next;
1670 static void mld_send_cr(struct inet6_dev *idev)
1672 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1673 struct sk_buff *skb = NULL;
1676 read_lock_bh(&idev->lock);
1677 spin_lock(&idev->mc_lock);
1681 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1682 pmc_next = pmc->next;
1683 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1684 type = MLD2_BLOCK_OLD_SOURCES;
1685 dtype = MLD2_BLOCK_OLD_SOURCES;
1686 skb = add_grec(skb, pmc, type, 1, 0);
1687 skb = add_grec(skb, pmc, dtype, 1, 1);
1689 if (pmc->mca_crcount) {
1690 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1691 type = MLD2_CHANGE_TO_INCLUDE;
1692 skb = add_grec(skb, pmc, type, 1, 0);
1695 if (pmc->mca_crcount == 0) {
1696 mld_clear_zeros(&pmc->mca_tomb);
1697 mld_clear_zeros(&pmc->mca_sources);
1700 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1701 !pmc->mca_sources) {
1703 pmc_prev->next = pmc_next;
1705 idev->mc_tomb = pmc_next;
1706 in6_dev_put(pmc->idev);
1711 spin_unlock(&idev->mc_lock);
1714 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1715 spin_lock_bh(&pmc->mca_lock);
1716 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1717 type = MLD2_BLOCK_OLD_SOURCES;
1718 dtype = MLD2_ALLOW_NEW_SOURCES;
1720 type = MLD2_ALLOW_NEW_SOURCES;
1721 dtype = MLD2_BLOCK_OLD_SOURCES;
1723 skb = add_grec(skb, pmc, type, 0, 0);
1724 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1726 /* filter mode changes */
1727 if (pmc->mca_crcount) {
1728 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1729 type = MLD2_CHANGE_TO_EXCLUDE;
1731 type = MLD2_CHANGE_TO_INCLUDE;
1732 skb = add_grec(skb, pmc, type, 0, 0);
1735 spin_unlock_bh(&pmc->mca_lock);
1737 read_unlock_bh(&idev->lock);
1740 (void) mld_sendpack(skb);
1743 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1745 struct net *net = dev_net(dev);
1746 struct sock *sk = net->ipv6.igmp_sk;
1747 struct inet6_dev *idev;
1748 struct sk_buff *skb;
1749 struct mld_msg *hdr;
1750 const struct in6_addr *snd_addr, *saddr;
1751 struct in6_addr addr_buf;
1752 int hlen = LL_RESERVED_SPACE(dev);
1753 int tlen = dev->needed_tailroom;
1754 int err, len, payload_len, full_len;
1755 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1756 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1759 struct dst_entry *dst;
1761 if (type == ICMPV6_MGM_REDUCTION)
1762 snd_addr = &in6addr_linklocal_allrouters;
1766 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1767 payload_len = len + sizeof(ra);
1768 full_len = sizeof(struct ipv6hdr) + payload_len;
1771 IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
1772 IPSTATS_MIB_OUT, full_len);
1775 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
1779 IP6_INC_STATS(net, __in6_dev_get(dev),
1780 IPSTATS_MIB_OUTDISCARDS);
1784 skb->priority = TC_PRIO_CONTROL;
1785 skb_reserve(skb, hlen);
1787 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1788 /* <draft-ietf-magma-mld-source-05.txt>:
1789 * use unspecified address as the source address
1790 * when a valid link-local address is not available.
1792 saddr = &in6addr_any;
1796 ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
1798 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1800 hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg));
1801 memset(hdr, 0, sizeof(struct mld_msg));
1802 hdr->mld_type = type;
1803 hdr->mld_mca = *addr;
1805 hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
1807 csum_partial(hdr, len, 0));
1810 idev = __in6_dev_get(skb->dev);
1812 icmpv6_flow_init(sk, &fl6, type,
1813 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1815 dst = icmp6_dst_alloc(skb->dev, &fl6);
1821 skb_dst_set(skb, dst);
1822 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1826 ICMP6MSGOUT_INC_STATS(net, idev, type);
1827 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
1828 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
1830 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
1840 static void mld_resend_report(struct inet6_dev *idev)
1842 if (MLD_V1_SEEN(idev)) {
1843 struct ifmcaddr6 *mcaddr;
1844 read_lock_bh(&idev->lock);
1845 for (mcaddr = idev->mc_list; mcaddr; mcaddr = mcaddr->next) {
1846 if (!(mcaddr->mca_flags & MAF_NOREPORT))
1847 igmp6_send(&mcaddr->mca_addr, idev->dev,
1850 read_unlock_bh(&idev->lock);
1852 mld_send_report(idev, NULL);
1856 void ipv6_mc_dad_complete(struct inet6_dev *idev)
1858 idev->mc_dad_count = idev->mc_qrv;
1859 if (idev->mc_dad_count) {
1860 mld_resend_report(idev);
1861 idev->mc_dad_count--;
1862 if (idev->mc_dad_count)
1863 mld_dad_start_timer(idev, idev->mc_maxdelay);
1867 static void mld_dad_timer_expire(unsigned long data)
1869 struct inet6_dev *idev = (struct inet6_dev *)data;
1871 mld_resend_report(idev);
1872 if (idev->mc_dad_count) {
1873 idev->mc_dad_count--;
1874 if (idev->mc_dad_count)
1875 mld_dad_start_timer(idev, idev->mc_maxdelay);
1877 __in6_dev_put(idev);
1880 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1881 const struct in6_addr *psfsrc)
1883 struct ip6_sf_list *psf, *psf_prev;
1887 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1888 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1892 if (!psf || psf->sf_count[sfmode] == 0) {
1893 /* source filter not found, or count wrong => bug */
1896 psf->sf_count[sfmode]--;
1897 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1898 struct inet6_dev *idev = pmc->idev;
1900 /* no more filters for this source */
1902 psf_prev->sf_next = psf->sf_next;
1904 pmc->mca_sources = psf->sf_next;
1905 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1906 !MLD_V1_SEEN(idev)) {
1907 psf->sf_crcount = idev->mc_qrv;
1908 psf->sf_next = pmc->mca_tomb;
1909 pmc->mca_tomb = psf;
1917 static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
1918 int sfmode, int sfcount, const struct in6_addr *psfsrc,
1921 struct ifmcaddr6 *pmc;
1927 read_lock_bh(&idev->lock);
1928 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1929 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1933 /* MCA not found?? bug */
1934 read_unlock_bh(&idev->lock);
1937 spin_lock_bh(&pmc->mca_lock);
1940 if (!pmc->mca_sfcount[sfmode]) {
1941 spin_unlock_bh(&pmc->mca_lock);
1942 read_unlock_bh(&idev->lock);
1945 pmc->mca_sfcount[sfmode]--;
1948 for (i=0; i<sfcount; i++) {
1949 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1951 changerec |= rv > 0;
1955 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1956 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1957 pmc->mca_sfcount[MCAST_INCLUDE]) {
1958 struct ip6_sf_list *psf;
1960 /* filter mode change */
1961 pmc->mca_sfmode = MCAST_INCLUDE;
1962 pmc->mca_crcount = idev->mc_qrv;
1963 idev->mc_ifc_count = pmc->mca_crcount;
1964 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1965 psf->sf_crcount = 0;
1966 mld_ifc_event(pmc->idev);
1967 } else if (sf_setstate(pmc) || changerec)
1968 mld_ifc_event(pmc->idev);
1969 spin_unlock_bh(&pmc->mca_lock);
1970 read_unlock_bh(&idev->lock);
1975 * Add multicast single-source filter to the interface list
1977 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1978 const struct in6_addr *psfsrc)
1980 struct ip6_sf_list *psf, *psf_prev;
1983 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1984 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1989 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1993 psf->sf_addr = *psfsrc;
1995 psf_prev->sf_next = psf;
1997 pmc->mca_sources = psf;
1999 psf->sf_count[sfmode]++;
2003 static void sf_markstate(struct ifmcaddr6 *pmc)
2005 struct ip6_sf_list *psf;
2006 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2008 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
2009 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2010 psf->sf_oldin = mca_xcount ==
2011 psf->sf_count[MCAST_EXCLUDE] &&
2012 !psf->sf_count[MCAST_INCLUDE];
2014 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2017 static int sf_setstate(struct ifmcaddr6 *pmc)
2019 struct ip6_sf_list *psf, *dpsf;
2020 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2021 int qrv = pmc->idev->mc_qrv;
2025 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
2026 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2027 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2028 !psf->sf_count[MCAST_INCLUDE];
2030 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2032 if (!psf->sf_oldin) {
2033 struct ip6_sf_list *prev = NULL;
2035 for (dpsf=pmc->mca_tomb; dpsf;
2036 dpsf=dpsf->sf_next) {
2037 if (ipv6_addr_equal(&dpsf->sf_addr,
2044 prev->sf_next = dpsf->sf_next;
2046 pmc->mca_tomb = dpsf->sf_next;
2049 psf->sf_crcount = qrv;
2052 } else if (psf->sf_oldin) {
2053 psf->sf_crcount = 0;
2055 * add or update "delete" records if an active filter
2058 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2059 if (ipv6_addr_equal(&dpsf->sf_addr,
2063 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2067 /* pmc->mca_lock held by callers */
2068 dpsf->sf_next = pmc->mca_tomb;
2069 pmc->mca_tomb = dpsf;
2071 dpsf->sf_crcount = qrv;
2079 * Add multicast source filter list to the interface list
2081 static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2082 int sfmode, int sfcount, const struct in6_addr *psfsrc,
2085 struct ifmcaddr6 *pmc;
2091 read_lock_bh(&idev->lock);
2092 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2093 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2097 /* MCA not found?? bug */
2098 read_unlock_bh(&idev->lock);
2101 spin_lock_bh(&pmc->mca_lock);
2104 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2106 pmc->mca_sfcount[sfmode]++;
2108 for (i=0; i<sfcount; i++) {
2109 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2117 pmc->mca_sfcount[sfmode]--;
2119 ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2120 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2121 struct ip6_sf_list *psf;
2123 /* filter mode change */
2124 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2125 pmc->mca_sfmode = MCAST_EXCLUDE;
2126 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2127 pmc->mca_sfmode = MCAST_INCLUDE;
2128 /* else no filters; keep old mode for reports */
2130 pmc->mca_crcount = idev->mc_qrv;
2131 idev->mc_ifc_count = pmc->mca_crcount;
2132 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2133 psf->sf_crcount = 0;
2134 mld_ifc_event(idev);
2135 } else if (sf_setstate(pmc))
2136 mld_ifc_event(idev);
2137 spin_unlock_bh(&pmc->mca_lock);
2138 read_unlock_bh(&idev->lock);
2142 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2144 struct ip6_sf_list *psf, *nextpsf;
2146 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2147 nextpsf = psf->sf_next;
2150 pmc->mca_tomb = NULL;
2151 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2152 nextpsf = psf->sf_next;
2155 pmc->mca_sources = NULL;
2156 pmc->mca_sfmode = MCAST_EXCLUDE;
2157 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2158 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2162 static void igmp6_join_group(struct ifmcaddr6 *ma)
2164 unsigned long delay;
2166 if (ma->mca_flags & MAF_NOREPORT)
2169 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2171 delay = net_random() % unsolicited_report_interval(ma->idev);
2173 spin_lock_bh(&ma->mca_lock);
2174 if (del_timer(&ma->mca_timer)) {
2175 atomic_dec(&ma->mca_refcnt);
2176 delay = ma->mca_timer.expires - jiffies;
2179 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2180 atomic_inc(&ma->mca_refcnt);
2181 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2182 spin_unlock_bh(&ma->mca_lock);
2185 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2186 struct inet6_dev *idev)
2190 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2191 * so no other readers or writers of iml or its sflist
2194 /* any-source empty exclude case */
2195 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2197 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2198 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2199 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2204 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2206 if (MLD_V1_SEEN(ma->idev)) {
2207 if (ma->mca_flags & MAF_LAST_REPORTER)
2208 igmp6_send(&ma->mca_addr, ma->idev->dev,
2209 ICMPV6_MGM_REDUCTION);
2211 mld_add_delrec(ma->idev, ma);
2212 mld_ifc_event(ma->idev);
2216 static void mld_gq_timer_expire(unsigned long data)
2218 struct inet6_dev *idev = (struct inet6_dev *)data;
2220 idev->mc_gq_running = 0;
2221 mld_send_report(idev, NULL);
2222 __in6_dev_put(idev);
2225 static void mld_ifc_timer_expire(unsigned long data)
2227 struct inet6_dev *idev = (struct inet6_dev *)data;
2230 if (idev->mc_ifc_count) {
2231 idev->mc_ifc_count--;
2232 if (idev->mc_ifc_count)
2233 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2235 __in6_dev_put(idev);
2238 static void mld_ifc_event(struct inet6_dev *idev)
2240 if (MLD_V1_SEEN(idev))
2242 idev->mc_ifc_count = idev->mc_qrv;
2243 mld_ifc_start_timer(idev, 1);
2247 static void igmp6_timer_handler(unsigned long data)
2249 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2251 if (MLD_V1_SEEN(ma->idev))
2252 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2254 mld_send_report(ma->idev, ma);
2256 spin_lock(&ma->mca_lock);
2257 ma->mca_flags |= MAF_LAST_REPORTER;
2258 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2259 spin_unlock(&ma->mca_lock);
2263 /* Device changing type */
2265 void ipv6_mc_unmap(struct inet6_dev *idev)
2267 struct ifmcaddr6 *i;
2269 /* Install multicast list, except for all-nodes (already installed) */
2271 read_lock_bh(&idev->lock);
2272 for (i = idev->mc_list; i; i = i->next)
2273 igmp6_group_dropped(i);
2274 read_unlock_bh(&idev->lock);
2277 void ipv6_mc_remap(struct inet6_dev *idev)
2282 /* Device going down */
2284 void ipv6_mc_down(struct inet6_dev *idev)
2286 struct ifmcaddr6 *i;
2288 /* Withdraw multicast list */
2290 read_lock_bh(&idev->lock);
2291 idev->mc_ifc_count = 0;
2292 if (del_timer(&idev->mc_ifc_timer))
2293 __in6_dev_put(idev);
2294 idev->mc_gq_running = 0;
2295 if (del_timer(&idev->mc_gq_timer))
2296 __in6_dev_put(idev);
2297 if (del_timer(&idev->mc_dad_timer))
2298 __in6_dev_put(idev);
2300 for (i = idev->mc_list; i; i=i->next)
2301 igmp6_group_dropped(i);
2302 read_unlock_bh(&idev->lock);
2304 mld_clear_delrec(idev);
2308 /* Device going up */
2310 void ipv6_mc_up(struct inet6_dev *idev)
2312 struct ifmcaddr6 *i;
2314 /* Install multicast list, except for all-nodes (already installed) */
2316 read_lock_bh(&idev->lock);
2317 for (i = idev->mc_list; i; i=i->next)
2318 igmp6_group_added(i);
2319 read_unlock_bh(&idev->lock);
2322 /* IPv6 device initialization. */
2324 void ipv6_mc_init_dev(struct inet6_dev *idev)
2326 write_lock_bh(&idev->lock);
2327 spin_lock_init(&idev->mc_lock);
2328 idev->mc_gq_running = 0;
2329 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2330 (unsigned long)idev);
2331 idev->mc_tomb = NULL;
2332 idev->mc_ifc_count = 0;
2333 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2334 (unsigned long)idev);
2335 setup_timer(&idev->mc_dad_timer, mld_dad_timer_expire,
2336 (unsigned long)idev);
2337 idev->mc_qrv = MLD_QRV_DEFAULT;
2338 idev->mc_maxdelay = unsolicited_report_interval(idev);
2339 idev->mc_v1_seen = 0;
2340 write_unlock_bh(&idev->lock);
2344 * Device is about to be destroyed: clean up.
2347 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2349 struct ifmcaddr6 *i;
2351 /* Deactivate timers */
2354 /* Delete all-nodes address. */
2355 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2356 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2359 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
2361 if (idev->cnf.forwarding)
2362 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
2364 write_lock_bh(&idev->lock);
2365 while ((i = idev->mc_list) != NULL) {
2366 idev->mc_list = i->next;
2367 write_unlock_bh(&idev->lock);
2369 igmp6_group_dropped(i);
2372 write_lock_bh(&idev->lock);
2374 write_unlock_bh(&idev->lock);
2377 #ifdef CONFIG_PROC_FS
2378 struct igmp6_mc_iter_state {
2379 struct seq_net_private p;
2380 struct net_device *dev;
2381 struct inet6_dev *idev;
2384 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2386 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2388 struct ifmcaddr6 *im = NULL;
2389 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2390 struct net *net = seq_file_net(seq);
2393 for_each_netdev_rcu(net, state->dev) {
2394 struct inet6_dev *idev;
2395 idev = __in6_dev_get(state->dev);
2398 read_lock_bh(&idev->lock);
2404 read_unlock_bh(&idev->lock);
2409 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2411 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2415 if (likely(state->idev != NULL))
2416 read_unlock_bh(&state->idev->lock);
2418 state->dev = next_net_device_rcu(state->dev);
2423 state->idev = __in6_dev_get(state->dev);
2426 read_lock_bh(&state->idev->lock);
2427 im = state->idev->mc_list;
2432 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2434 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2436 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2438 return pos ? NULL : im;
2441 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2445 return igmp6_mc_get_idx(seq, *pos);
2448 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2450 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2456 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2459 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2461 if (likely(state->idev != NULL)) {
2462 read_unlock_bh(&state->idev->lock);
2469 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2471 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2472 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2475 "%-4d %-15s %pi6 %5d %08X %ld\n",
2476 state->dev->ifindex, state->dev->name,
2478 im->mca_users, im->mca_flags,
2479 (im->mca_flags&MAF_TIMER_RUNNING) ?
2480 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2484 static const struct seq_operations igmp6_mc_seq_ops = {
2485 .start = igmp6_mc_seq_start,
2486 .next = igmp6_mc_seq_next,
2487 .stop = igmp6_mc_seq_stop,
2488 .show = igmp6_mc_seq_show,
2491 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2493 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2494 sizeof(struct igmp6_mc_iter_state));
2497 static const struct file_operations igmp6_mc_seq_fops = {
2498 .owner = THIS_MODULE,
2499 .open = igmp6_mc_seq_open,
2501 .llseek = seq_lseek,
2502 .release = seq_release_net,
2505 struct igmp6_mcf_iter_state {
2506 struct seq_net_private p;
2507 struct net_device *dev;
2508 struct inet6_dev *idev;
2509 struct ifmcaddr6 *im;
2512 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2514 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2516 struct ip6_sf_list *psf = NULL;
2517 struct ifmcaddr6 *im = NULL;
2518 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2519 struct net *net = seq_file_net(seq);
2523 for_each_netdev_rcu(net, state->dev) {
2524 struct inet6_dev *idev;
2525 idev = __in6_dev_get(state->dev);
2526 if (unlikely(idev == NULL))
2528 read_lock_bh(&idev->lock);
2530 if (likely(im != NULL)) {
2531 spin_lock_bh(&im->mca_lock);
2532 psf = im->mca_sources;
2533 if (likely(psf != NULL)) {
2538 spin_unlock_bh(&im->mca_lock);
2540 read_unlock_bh(&idev->lock);
2545 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2547 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2551 spin_unlock_bh(&state->im->mca_lock);
2552 state->im = state->im->next;
2553 while (!state->im) {
2554 if (likely(state->idev != NULL))
2555 read_unlock_bh(&state->idev->lock);
2557 state->dev = next_net_device_rcu(state->dev);
2562 state->idev = __in6_dev_get(state->dev);
2565 read_lock_bh(&state->idev->lock);
2566 state->im = state->idev->mc_list;
2570 spin_lock_bh(&state->im->mca_lock);
2571 psf = state->im->mca_sources;
2577 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2579 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2581 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2583 return pos ? NULL : psf;
2586 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2590 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2593 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2595 struct ip6_sf_list *psf;
2596 if (v == SEQ_START_TOKEN)
2597 psf = igmp6_mcf_get_first(seq);
2599 psf = igmp6_mcf_get_next(seq, v);
2604 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2607 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2608 if (likely(state->im != NULL)) {
2609 spin_unlock_bh(&state->im->mca_lock);
2612 if (likely(state->idev != NULL)) {
2613 read_unlock_bh(&state->idev->lock);
2620 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2622 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2623 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2625 if (v == SEQ_START_TOKEN) {
2628 "%32s %32s %6s %6s\n", "Idx",
2629 "Device", "Multicast Address",
2630 "Source Address", "INC", "EXC");
2633 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
2634 state->dev->ifindex, state->dev->name,
2635 &state->im->mca_addr,
2637 psf->sf_count[MCAST_INCLUDE],
2638 psf->sf_count[MCAST_EXCLUDE]);
2643 static const struct seq_operations igmp6_mcf_seq_ops = {
2644 .start = igmp6_mcf_seq_start,
2645 .next = igmp6_mcf_seq_next,
2646 .stop = igmp6_mcf_seq_stop,
2647 .show = igmp6_mcf_seq_show,
2650 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2652 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2653 sizeof(struct igmp6_mcf_iter_state));
2656 static const struct file_operations igmp6_mcf_seq_fops = {
2657 .owner = THIS_MODULE,
2658 .open = igmp6_mcf_seq_open,
2660 .llseek = seq_lseek,
2661 .release = seq_release_net,
2664 static int __net_init igmp6_proc_init(struct net *net)
2669 if (!proc_create("igmp6", S_IRUGO, net->proc_net, &igmp6_mc_seq_fops))
2671 if (!proc_create("mcfilter6", S_IRUGO, net->proc_net,
2672 &igmp6_mcf_seq_fops))
2673 goto out_proc_net_igmp6;
2680 remove_proc_entry("igmp6", net->proc_net);
2684 static void __net_exit igmp6_proc_exit(struct net *net)
2686 remove_proc_entry("mcfilter6", net->proc_net);
2687 remove_proc_entry("igmp6", net->proc_net);
2690 static inline int igmp6_proc_init(struct net *net)
2694 static inline void igmp6_proc_exit(struct net *net)
2699 static int __net_init igmp6_net_init(struct net *net)
2703 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2704 SOCK_RAW, IPPROTO_ICMPV6, net);
2706 pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
2711 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
2713 err = igmp6_proc_init(net);
2715 goto out_sock_create;
2720 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2724 static void __net_exit igmp6_net_exit(struct net *net)
2726 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2727 igmp6_proc_exit(net);
2730 static struct pernet_operations igmp6_net_ops = {
2731 .init = igmp6_net_init,
2732 .exit = igmp6_net_exit,
2735 int __init igmp6_init(void)
2737 return register_pernet_subsys(&igmp6_net_ops);
2740 void igmp6_cleanup(void)
2742 unregister_pernet_subsys(&igmp6_net_ops);