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 IGMP6_UNSOLICITED_IVAL (10*HZ)
112 #define MLD_QRV_DEFAULT 2
114 #define MLD_V1_SEEN(idev) (dev_net((idev)->dev)->ipv6.devconf_all->force_mld_version == 1 || \
115 (idev)->cnf.force_mld_version == 1 || \
116 ((idev)->mc_v1_seen && \
117 time_before(jiffies, (idev)->mc_v1_seen)))
119 #define IPV6_MLD_MAX_MSF 64
121 int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
124 * socket join on multicast group
127 #define for_each_pmc_rcu(np, pmc) \
128 for (pmc = rcu_dereference(np->ipv6_mc_list); \
130 pmc = rcu_dereference(pmc->next))
132 int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
134 struct net_device *dev = NULL;
135 struct ipv6_mc_socklist *mc_lst;
136 struct ipv6_pinfo *np = inet6_sk(sk);
137 struct net *net = sock_net(sk);
140 if (!ipv6_addr_is_multicast(addr))
144 for_each_pmc_rcu(np, mc_lst) {
145 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
146 ipv6_addr_equal(&mc_lst->addr, addr)) {
153 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
159 mc_lst->addr = *addr;
164 rt = rt6_lookup(net, addr, NULL, 0, 0);
170 dev = dev_get_by_index_rcu(net, ifindex);
174 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
178 mc_lst->ifindex = dev->ifindex;
179 mc_lst->sfmode = MCAST_EXCLUDE;
180 rwlock_init(&mc_lst->sflock);
181 mc_lst->sflist = NULL;
184 * now add/increase the group membership on the device
187 err = ipv6_dev_mc_inc(dev, addr);
191 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
195 spin_lock(&ipv6_sk_mc_lock);
196 mc_lst->next = np->ipv6_mc_list;
197 rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
198 spin_unlock(&ipv6_sk_mc_lock);
206 * socket leave on multicast group
208 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
210 struct ipv6_pinfo *np = inet6_sk(sk);
211 struct ipv6_mc_socklist *mc_lst;
212 struct ipv6_mc_socklist __rcu **lnk;
213 struct net *net = sock_net(sk);
215 if (!ipv6_addr_is_multicast(addr))
218 spin_lock(&ipv6_sk_mc_lock);
219 for (lnk = &np->ipv6_mc_list;
220 (mc_lst = rcu_dereference_protected(*lnk,
221 lockdep_is_held(&ipv6_sk_mc_lock))) !=NULL ;
222 lnk = &mc_lst->next) {
223 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
224 ipv6_addr_equal(&mc_lst->addr, addr)) {
225 struct net_device *dev;
228 spin_unlock(&ipv6_sk_mc_lock);
231 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
233 struct inet6_dev *idev = __in6_dev_get(dev);
235 (void) ip6_mc_leave_src(sk, mc_lst, idev);
237 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
239 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
241 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
242 kfree_rcu(mc_lst, rcu);
246 spin_unlock(&ipv6_sk_mc_lock);
248 return -EADDRNOTAVAIL;
251 /* called with rcu_read_lock() */
252 static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
253 const struct in6_addr *group,
256 struct net_device *dev = NULL;
257 struct inet6_dev *idev = NULL;
260 struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, 0);
267 dev = dev_get_by_index_rcu(net, ifindex);
271 idev = __in6_dev_get(dev);
274 read_lock_bh(&idev->lock);
276 read_unlock_bh(&idev->lock);
282 void ipv6_sock_mc_close(struct sock *sk)
284 struct ipv6_pinfo *np = inet6_sk(sk);
285 struct ipv6_mc_socklist *mc_lst;
286 struct net *net = sock_net(sk);
288 if (!rcu_access_pointer(np->ipv6_mc_list))
291 spin_lock(&ipv6_sk_mc_lock);
292 while ((mc_lst = rcu_dereference_protected(np->ipv6_mc_list,
293 lockdep_is_held(&ipv6_sk_mc_lock))) != NULL) {
294 struct net_device *dev;
296 np->ipv6_mc_list = mc_lst->next;
297 spin_unlock(&ipv6_sk_mc_lock);
300 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
302 struct inet6_dev *idev = __in6_dev_get(dev);
304 (void) ip6_mc_leave_src(sk, mc_lst, idev);
306 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
308 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
311 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
312 kfree_rcu(mc_lst, rcu);
314 spin_lock(&ipv6_sk_mc_lock);
316 spin_unlock(&ipv6_sk_mc_lock);
319 int ip6_mc_source(int add, int omode, struct sock *sk,
320 struct group_source_req *pgsr)
322 struct in6_addr *source, *group;
323 struct ipv6_mc_socklist *pmc;
324 struct inet6_dev *idev;
325 struct ipv6_pinfo *inet6 = inet6_sk(sk);
326 struct ip6_sf_socklist *psl;
327 struct net *net = sock_net(sk);
333 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
334 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
336 if (!ipv6_addr_is_multicast(group))
340 idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface);
346 err = -EADDRNOTAVAIL;
348 for_each_pmc_rcu(inet6, pmc) {
349 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
351 if (ipv6_addr_equal(&pmc->addr, group))
354 if (!pmc) { /* must have a prior join */
358 /* if a source filter was set, must be the same mode as before */
360 if (pmc->sfmode != omode) {
364 } else if (pmc->sfmode != omode) {
365 /* allow mode switches for empty-set filters */
366 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
367 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
371 write_lock(&pmc->sflock);
377 goto done; /* err = -EADDRNOTAVAIL */
379 for (i=0; i<psl->sl_count; i++) {
380 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
384 if (rv) /* source not found */
385 goto done; /* err = -EADDRNOTAVAIL */
387 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
388 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
393 /* update the interface filter */
394 ip6_mc_del_src(idev, group, omode, 1, source, 1);
396 for (j=i+1; j<psl->sl_count; j++)
397 psl->sl_addr[j-1] = psl->sl_addr[j];
402 /* else, add a new source to the filter */
404 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
408 if (!psl || psl->sl_count == psl->sl_max) {
409 struct ip6_sf_socklist *newpsl;
410 int count = IP6_SFBLOCK;
413 count += psl->sl_max;
414 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
419 newpsl->sl_max = count;
420 newpsl->sl_count = count - IP6_SFBLOCK;
422 for (i=0; i<psl->sl_count; i++)
423 newpsl->sl_addr[i] = psl->sl_addr[i];
424 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
426 pmc->sflist = psl = newpsl;
428 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
429 for (i=0; i<psl->sl_count; i++) {
430 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
431 if (rv == 0) /* There is an error in the address. */
434 for (j=psl->sl_count-1; j>=i; j--)
435 psl->sl_addr[j+1] = psl->sl_addr[j];
436 psl->sl_addr[i] = *source;
439 /* update the interface list */
440 ip6_mc_add_src(idev, group, omode, 1, source, 1);
443 write_unlock(&pmc->sflock);
444 read_unlock_bh(&idev->lock);
447 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
451 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
453 const struct in6_addr *group;
454 struct ipv6_mc_socklist *pmc;
455 struct inet6_dev *idev;
456 struct ipv6_pinfo *inet6 = inet6_sk(sk);
457 struct ip6_sf_socklist *newpsl, *psl;
458 struct net *net = sock_net(sk);
462 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
464 if (!ipv6_addr_is_multicast(group))
466 if (gsf->gf_fmode != MCAST_INCLUDE &&
467 gsf->gf_fmode != MCAST_EXCLUDE)
471 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
480 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
485 for_each_pmc_rcu(inet6, pmc) {
486 if (pmc->ifindex != gsf->gf_interface)
488 if (ipv6_addr_equal(&pmc->addr, group))
491 if (!pmc) { /* must have a prior join */
495 if (gsf->gf_numsrc) {
496 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
502 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
503 for (i=0; i<newpsl->sl_count; ++i) {
504 struct sockaddr_in6 *psin6;
506 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
507 newpsl->sl_addr[i] = psin6->sin6_addr;
509 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
510 newpsl->sl_count, newpsl->sl_addr, 0);
512 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
517 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
520 write_lock(&pmc->sflock);
523 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
524 psl->sl_count, psl->sl_addr, 0);
525 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
527 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
528 pmc->sflist = newpsl;
529 pmc->sfmode = gsf->gf_fmode;
530 write_unlock(&pmc->sflock);
533 read_unlock_bh(&idev->lock);
536 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
540 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
541 struct group_filter __user *optval, int __user *optlen)
543 int err, i, count, copycount;
544 const struct in6_addr *group;
545 struct ipv6_mc_socklist *pmc;
546 struct inet6_dev *idev;
547 struct ipv6_pinfo *inet6 = inet6_sk(sk);
548 struct ip6_sf_socklist *psl;
549 struct net *net = sock_net(sk);
551 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
553 if (!ipv6_addr_is_multicast(group))
557 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
564 err = -EADDRNOTAVAIL;
566 * changes to the ipv6_mc_list require the socket lock and
567 * a read lock on ip6_sk_mc_lock. We have the socket lock,
568 * so reading the list is safe.
571 for_each_pmc_rcu(inet6, pmc) {
572 if (pmc->ifindex != gsf->gf_interface)
574 if (ipv6_addr_equal(group, &pmc->addr))
577 if (!pmc) /* must have a prior join */
579 gsf->gf_fmode = pmc->sfmode;
581 count = psl ? psl->sl_count : 0;
582 read_unlock_bh(&idev->lock);
585 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
586 gsf->gf_numsrc = count;
587 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
588 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
591 /* changes to psl require the socket lock, a read lock on
592 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
593 * have the socket lock, so reading here is safe.
595 for (i=0; i<copycount; i++) {
596 struct sockaddr_in6 *psin6;
597 struct sockaddr_storage ss;
599 psin6 = (struct sockaddr_in6 *)&ss;
600 memset(&ss, 0, sizeof(ss));
601 psin6->sin6_family = AF_INET6;
602 psin6->sin6_addr = psl->sl_addr[i];
603 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
608 read_unlock_bh(&idev->lock);
613 bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
614 const struct in6_addr *src_addr)
616 struct ipv6_pinfo *np = inet6_sk(sk);
617 struct ipv6_mc_socklist *mc;
618 struct ip6_sf_socklist *psl;
622 for_each_pmc_rcu(np, mc) {
623 if (ipv6_addr_equal(&mc->addr, mc_addr))
630 read_lock(&mc->sflock);
633 rv = mc->sfmode == MCAST_EXCLUDE;
637 for (i=0; i<psl->sl_count; i++) {
638 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
641 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
643 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
646 read_unlock(&mc->sflock);
652 static void ma_put(struct ifmcaddr6 *mc)
654 if (atomic_dec_and_test(&mc->mca_refcnt)) {
655 in6_dev_put(mc->idev);
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_V1_SEEN(mc->idev)) {
681 igmp6_join_group(mc);
686 mc->mca_crcount = mc->idev->mc_qrv;
687 mld_ifc_event(mc->idev);
690 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
692 struct net_device *dev = mc->idev->dev;
693 char buf[MAX_ADDR_LEN];
695 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
696 IPV6_ADDR_SCOPE_LINKLOCAL)
699 spin_lock_bh(&mc->mca_lock);
700 if (mc->mca_flags&MAF_LOADED) {
701 mc->mca_flags &= ~MAF_LOADED;
702 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
703 dev_mc_del(dev, buf);
706 if (mc->mca_flags & MAF_NOREPORT)
708 spin_unlock_bh(&mc->mca_lock);
711 igmp6_leave_group(mc);
713 spin_lock_bh(&mc->mca_lock);
714 if (del_timer(&mc->mca_timer))
715 atomic_dec(&mc->mca_refcnt);
717 ip6_mc_clear_src(mc);
718 spin_unlock_bh(&mc->mca_lock);
722 * deleted ifmcaddr6 manipulation
724 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
726 struct ifmcaddr6 *pmc;
728 /* this is an "ifmcaddr6" for convenience; only the fields below
729 * are actually used. In particular, the refcnt and users are not
730 * used for management of the delete list. Using the same structure
731 * for deleted items allows change reports to use common code with
732 * non-deleted or query-response MCA's.
734 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
738 spin_lock_bh(&im->mca_lock);
739 spin_lock_init(&pmc->mca_lock);
740 pmc->idev = im->idev;
742 pmc->mca_addr = im->mca_addr;
743 pmc->mca_crcount = idev->mc_qrv;
744 pmc->mca_sfmode = im->mca_sfmode;
745 if (pmc->mca_sfmode == MCAST_INCLUDE) {
746 struct ip6_sf_list *psf;
748 pmc->mca_tomb = im->mca_tomb;
749 pmc->mca_sources = im->mca_sources;
750 im->mca_tomb = im->mca_sources = NULL;
751 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
752 psf->sf_crcount = pmc->mca_crcount;
754 spin_unlock_bh(&im->mca_lock);
756 spin_lock_bh(&idev->mc_lock);
757 pmc->next = idev->mc_tomb;
759 spin_unlock_bh(&idev->mc_lock);
762 static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *pmca)
764 struct ifmcaddr6 *pmc, *pmc_prev;
765 struct ip6_sf_list *psf, *psf_next;
767 spin_lock_bh(&idev->mc_lock);
769 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
770 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
776 pmc_prev->next = pmc->next;
778 idev->mc_tomb = pmc->next;
780 spin_unlock_bh(&idev->mc_lock);
783 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
784 psf_next = psf->sf_next;
787 in6_dev_put(pmc->idev);
792 static void mld_clear_delrec(struct inet6_dev *idev)
794 struct ifmcaddr6 *pmc, *nextpmc;
796 spin_lock_bh(&idev->mc_lock);
798 idev->mc_tomb = NULL;
799 spin_unlock_bh(&idev->mc_lock);
801 for (; pmc; pmc = nextpmc) {
803 ip6_mc_clear_src(pmc);
804 in6_dev_put(pmc->idev);
808 /* clear dead sources, too */
809 read_lock_bh(&idev->lock);
810 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
811 struct ip6_sf_list *psf, *psf_next;
813 spin_lock_bh(&pmc->mca_lock);
815 pmc->mca_tomb = NULL;
816 spin_unlock_bh(&pmc->mca_lock);
817 for (; psf; psf=psf_next) {
818 psf_next = psf->sf_next;
822 read_unlock_bh(&idev->lock);
827 * device multicast group inc (add if not found)
829 int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
831 struct ifmcaddr6 *mc;
832 struct inet6_dev *idev;
834 /* we need to take a reference on idev */
835 idev = in6_dev_get(dev);
840 write_lock_bh(&idev->lock);
842 write_unlock_bh(&idev->lock);
847 for (mc = idev->mc_list; mc; mc = mc->next) {
848 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
850 write_unlock_bh(&idev->lock);
851 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
859 * not found: create a new one.
862 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
865 write_unlock_bh(&idev->lock);
870 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
872 mc->mca_addr = *addr;
873 mc->idev = idev; /* (reference taken) */
875 /* mca_stamp should be updated upon changes */
876 mc->mca_cstamp = mc->mca_tstamp = jiffies;
877 atomic_set(&mc->mca_refcnt, 2);
878 spin_lock_init(&mc->mca_lock);
880 /* initial mode is (EX, empty) */
881 mc->mca_sfmode = MCAST_EXCLUDE;
882 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
884 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
885 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
886 mc->mca_flags |= MAF_NOREPORT;
888 mc->next = idev->mc_list;
890 write_unlock_bh(&idev->lock);
892 mld_del_delrec(idev, &mc->mca_addr);
893 igmp6_group_added(mc);
899 * device multicast group del
901 int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
903 struct ifmcaddr6 *ma, **map;
905 write_lock_bh(&idev->lock);
906 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
907 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
908 if (--ma->mca_users == 0) {
910 write_unlock_bh(&idev->lock);
912 igmp6_group_dropped(ma);
917 write_unlock_bh(&idev->lock);
921 write_unlock_bh(&idev->lock);
926 int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
928 struct inet6_dev *idev;
933 idev = __in6_dev_get(dev);
937 err = __ipv6_dev_mc_dec(idev, addr);
944 * check if the interface/address pair is valid
946 bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
947 const struct in6_addr *src_addr)
949 struct inet6_dev *idev;
950 struct ifmcaddr6 *mc;
954 idev = __in6_dev_get(dev);
956 read_lock_bh(&idev->lock);
957 for (mc = idev->mc_list; mc; mc=mc->next) {
958 if (ipv6_addr_equal(&mc->mca_addr, group))
962 if (src_addr && !ipv6_addr_any(src_addr)) {
963 struct ip6_sf_list *psf;
965 spin_lock_bh(&mc->mca_lock);
966 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
967 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
971 rv = psf->sf_count[MCAST_INCLUDE] ||
972 psf->sf_count[MCAST_EXCLUDE] !=
973 mc->mca_sfcount[MCAST_EXCLUDE];
975 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
976 spin_unlock_bh(&mc->mca_lock);
978 rv = true; /* don't filter unspecified source */
980 read_unlock_bh(&idev->lock);
986 static void mld_gq_start_timer(struct inet6_dev *idev)
988 int tv = net_random() % idev->mc_maxdelay;
990 idev->mc_gq_running = 1;
991 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
995 static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
997 int tv = net_random() % delay;
999 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1003 static void mld_dad_start_timer(struct inet6_dev *idev, int delay)
1005 int tv = net_random() % delay;
1007 if (!mod_timer(&idev->mc_dad_timer, jiffies+tv+2))
1012 * IGMP handling (alias multicast ICMPv6 messages)
1015 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1017 unsigned long delay = resptime;
1019 /* Do not start timer for these addresses */
1020 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1021 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1024 if (del_timer(&ma->mca_timer)) {
1025 atomic_dec(&ma->mca_refcnt);
1026 delay = ma->mca_timer.expires - jiffies;
1029 if (delay >= resptime) {
1031 delay = net_random() % resptime;
1035 ma->mca_timer.expires = jiffies + delay;
1036 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1037 atomic_inc(&ma->mca_refcnt);
1038 ma->mca_flags |= MAF_TIMER_RUNNING;
1041 /* mark EXCLUDE-mode sources */
1042 static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1043 const struct in6_addr *srcs)
1045 struct ip6_sf_list *psf;
1049 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1050 if (scount == nsrcs)
1052 for (i=0; i<nsrcs; i++) {
1053 /* skip inactive filters */
1054 if (psf->sf_count[MCAST_INCLUDE] ||
1055 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1056 psf->sf_count[MCAST_EXCLUDE])
1058 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1064 pmc->mca_flags &= ~MAF_GSQUERY;
1065 if (scount == nsrcs) /* all sources excluded */
1070 static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1071 const struct in6_addr *srcs)
1073 struct ip6_sf_list *psf;
1076 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1077 return mld_xmarksources(pmc, nsrcs, srcs);
1079 /* mark INCLUDE-mode sources */
1082 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1083 if (scount == nsrcs)
1085 for (i=0; i<nsrcs; i++) {
1086 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1094 pmc->mca_flags &= ~MAF_GSQUERY;
1097 pmc->mca_flags |= MAF_GSQUERY;
1101 /* called with rcu_read_lock() */
1102 int igmp6_event_query(struct sk_buff *skb)
1104 struct mld2_query *mlh2 = NULL;
1105 struct ifmcaddr6 *ma;
1106 const struct in6_addr *group;
1107 unsigned long max_delay;
1108 struct inet6_dev *idev;
1109 struct mld_msg *mld;
1114 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1117 /* compute payload length excluding extension headers */
1118 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
1119 len -= skb_network_header_len(skb);
1121 /* Drop queries with not link local source */
1122 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
1125 idev = __in6_dev_get(skb->dev);
1130 mld = (struct mld_msg *)icmp6_hdr(skb);
1131 group = &mld->mld_mca;
1132 group_type = ipv6_addr_type(group);
1134 if (group_type != IPV6_ADDR_ANY &&
1135 !(group_type&IPV6_ADDR_MULTICAST))
1140 /* MLDv1 router present */
1142 /* Translate milliseconds to jiffies */
1143 max_delay = (ntohs(mld->mld_maxdelay)*HZ)/1000;
1145 switchback = (idev->mc_qrv + 1) * max_delay;
1146 idev->mc_v1_seen = jiffies + switchback;
1148 /* cancel the interface change timer */
1149 idev->mc_ifc_count = 0;
1150 if (del_timer(&idev->mc_ifc_timer))
1151 __in6_dev_put(idev);
1152 /* clear deleted report items */
1153 mld_clear_delrec(idev);
1154 } else if (len >= 28) {
1155 int srcs_offset = sizeof(struct mld2_query) -
1156 sizeof(struct icmp6hdr);
1157 if (!pskb_may_pull(skb, srcs_offset))
1160 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1161 max_delay = (MLDV2_MRC(ntohs(mlh2->mld2q_mrc))*HZ)/1000;
1164 idev->mc_maxdelay = max_delay;
1165 if (mlh2->mld2q_qrv)
1166 idev->mc_qrv = mlh2->mld2q_qrv;
1167 if (group_type == IPV6_ADDR_ANY) { /* general query */
1168 if (mlh2->mld2q_nsrcs)
1169 return -EINVAL; /* no sources allowed */
1171 mld_gq_start_timer(idev);
1174 /* mark sources to include, if group & source-specific */
1175 if (mlh2->mld2q_nsrcs != 0) {
1176 if (!pskb_may_pull(skb, srcs_offset +
1177 ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
1180 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1186 read_lock_bh(&idev->lock);
1187 if (group_type == IPV6_ADDR_ANY) {
1188 for (ma = idev->mc_list; ma; ma=ma->next) {
1189 spin_lock_bh(&ma->mca_lock);
1190 igmp6_group_queried(ma, max_delay);
1191 spin_unlock_bh(&ma->mca_lock);
1194 for (ma = idev->mc_list; ma; ma=ma->next) {
1195 if (!ipv6_addr_equal(group, &ma->mca_addr))
1197 spin_lock_bh(&ma->mca_lock);
1198 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1199 /* gsquery <- gsquery && mark */
1201 ma->mca_flags &= ~MAF_GSQUERY;
1203 /* gsquery <- mark */
1205 ma->mca_flags |= MAF_GSQUERY;
1207 ma->mca_flags &= ~MAF_GSQUERY;
1209 if (!(ma->mca_flags & MAF_GSQUERY) ||
1210 mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
1211 igmp6_group_queried(ma, max_delay);
1212 spin_unlock_bh(&ma->mca_lock);
1216 read_unlock_bh(&idev->lock);
1221 /* called with rcu_read_lock() */
1222 int igmp6_event_report(struct sk_buff *skb)
1224 struct ifmcaddr6 *ma;
1225 struct inet6_dev *idev;
1226 struct mld_msg *mld;
1229 /* Our own report looped back. Ignore it. */
1230 if (skb->pkt_type == PACKET_LOOPBACK)
1233 /* send our report if the MC router may not have heard this report */
1234 if (skb->pkt_type != PACKET_MULTICAST &&
1235 skb->pkt_type != PACKET_BROADCAST)
1238 if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
1241 mld = (struct mld_msg *)icmp6_hdr(skb);
1243 /* Drop reports with not link local source */
1244 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
1245 if (addr_type != IPV6_ADDR_ANY &&
1246 !(addr_type&IPV6_ADDR_LINKLOCAL))
1249 idev = __in6_dev_get(skb->dev);
1254 * Cancel the timer for this group
1257 read_lock_bh(&idev->lock);
1258 for (ma = idev->mc_list; ma; ma=ma->next) {
1259 if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
1260 spin_lock(&ma->mca_lock);
1261 if (del_timer(&ma->mca_timer))
1262 atomic_dec(&ma->mca_refcnt);
1263 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1264 spin_unlock(&ma->mca_lock);
1268 read_unlock_bh(&idev->lock);
1272 static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1273 int gdeleted, int sdeleted)
1276 case MLD2_MODE_IS_INCLUDE:
1277 case MLD2_MODE_IS_EXCLUDE:
1278 if (gdeleted || sdeleted)
1280 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1281 if (pmc->mca_sfmode == MCAST_INCLUDE)
1283 /* don't include if this source is excluded
1286 if (psf->sf_count[MCAST_INCLUDE])
1287 return type == MLD2_MODE_IS_INCLUDE;
1288 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1289 psf->sf_count[MCAST_EXCLUDE];
1292 case MLD2_CHANGE_TO_INCLUDE:
1293 if (gdeleted || sdeleted)
1295 return psf->sf_count[MCAST_INCLUDE] != 0;
1296 case MLD2_CHANGE_TO_EXCLUDE:
1297 if (gdeleted || sdeleted)
1299 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1300 psf->sf_count[MCAST_INCLUDE])
1302 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1303 psf->sf_count[MCAST_EXCLUDE];
1304 case MLD2_ALLOW_NEW_SOURCES:
1305 if (gdeleted || !psf->sf_crcount)
1307 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1308 case MLD2_BLOCK_OLD_SOURCES:
1309 if (pmc->mca_sfmode == MCAST_INCLUDE)
1310 return gdeleted || (psf->sf_crcount && sdeleted);
1311 return psf->sf_crcount && !gdeleted && !sdeleted;
1317 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1319 struct ip6_sf_list *psf;
1322 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1323 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1330 static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
1331 struct net_device *dev,
1332 const struct in6_addr *saddr,
1333 const struct in6_addr *daddr,
1336 struct ipv6hdr *hdr;
1338 skb->protocol = htons(ETH_P_IPV6);
1341 skb_reset_network_header(skb);
1342 skb_put(skb, sizeof(struct ipv6hdr));
1343 hdr = ipv6_hdr(skb);
1345 ip6_flow_hdr(hdr, 0, 0);
1347 hdr->payload_len = htons(len);
1348 hdr->nexthdr = proto;
1349 hdr->hop_limit = inet6_sk(sk)->hop_limit;
1351 hdr->saddr = *saddr;
1352 hdr->daddr = *daddr;
1355 static struct sk_buff *mld_newpack(struct inet6_dev *idev, int size)
1357 struct net_device *dev = idev->dev;
1358 struct net *net = dev_net(dev);
1359 struct sock *sk = net->ipv6.igmp_sk;
1360 struct sk_buff *skb;
1361 struct mld2_report *pmr;
1362 struct in6_addr addr_buf;
1363 const struct in6_addr *saddr;
1364 int hlen = LL_RESERVED_SPACE(dev);
1365 int tlen = dev->needed_tailroom;
1367 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1368 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1371 /* we assume size > sizeof(ra) here */
1372 size += hlen + tlen;
1373 /* limit our allocations to order-0 page */
1374 size = min_t(int, size, SKB_MAX_ORDER(0, 0));
1375 skb = sock_alloc_send_skb(sk, size, 1, &err);
1380 skb->priority = TC_PRIO_CONTROL;
1381 skb_reserve(skb, hlen);
1383 if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
1384 /* <draft-ietf-magma-mld-source-05.txt>:
1385 * use unspecified address as the source address
1386 * when a valid link-local address is not available.
1388 saddr = &in6addr_any;
1392 ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
1394 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1396 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
1397 skb_put(skb, sizeof(*pmr));
1398 pmr = (struct mld2_report *)skb_transport_header(skb);
1399 pmr->mld2r_type = ICMPV6_MLD2_REPORT;
1400 pmr->mld2r_resv1 = 0;
1401 pmr->mld2r_cksum = 0;
1402 pmr->mld2r_resv2 = 0;
1403 pmr->mld2r_ngrec = 0;
1407 static void mld_sendpack(struct sk_buff *skb)
1409 struct ipv6hdr *pip6 = ipv6_hdr(skb);
1410 struct mld2_report *pmr =
1411 (struct mld2_report *)skb_transport_header(skb);
1412 int payload_len, mldlen;
1413 struct inet6_dev *idev;
1414 struct net *net = dev_net(skb->dev);
1417 struct dst_entry *dst;
1420 idev = __in6_dev_get(skb->dev);
1421 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1423 payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
1425 mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
1426 pip6->payload_len = htons(payload_len);
1428 pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1430 csum_partial(skb_transport_header(skb),
1433 icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
1434 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1436 dst = icmp6_dst_alloc(skb->dev, &fl6);
1443 skb_dst_set(skb, dst);
1447 payload_len = skb->len;
1449 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1453 ICMP6MSGOUT_INC_STATS_BH(net, idev, ICMPV6_MLD2_REPORT);
1454 ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
1455 IP6_UPD_PO_STATS_BH(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
1457 IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTDISCARDS);
1467 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1469 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1472 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1473 int type, struct mld2_grec **ppgr)
1475 struct net_device *dev = pmc->idev->dev;
1476 struct mld2_report *pmr;
1477 struct mld2_grec *pgr;
1480 skb = mld_newpack(pmc->idev, dev->mtu);
1483 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1484 pgr->grec_type = type;
1485 pgr->grec_auxwords = 0;
1486 pgr->grec_nsrcs = 0;
1487 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1488 pmr = (struct mld2_report *)skb_transport_header(skb);
1489 pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
1494 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1495 skb_tailroom(skb)) : 0)
1497 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1498 int type, int gdeleted, int sdeleted)
1500 struct inet6_dev *idev = pmc->idev;
1501 struct net_device *dev = idev->dev;
1502 struct mld2_report *pmr;
1503 struct mld2_grec *pgr = NULL;
1504 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1505 int scount, stotal, first, isquery, truncate;
1507 if (pmc->mca_flags & MAF_NOREPORT)
1510 isquery = type == MLD2_MODE_IS_INCLUDE ||
1511 type == MLD2_MODE_IS_EXCLUDE;
1512 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1513 type == MLD2_CHANGE_TO_EXCLUDE;
1515 stotal = scount = 0;
1517 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1522 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
1524 /* EX and TO_EX get a fresh packet, if needed */
1526 if (pmr && pmr->mld2r_ngrec &&
1527 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1530 skb = mld_newpack(idev, dev->mtu);
1535 for (psf=*psf_list; psf; psf=psf_next) {
1536 struct in6_addr *psrc;
1538 psf_next = psf->sf_next;
1540 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1545 /* clear marks on query responses */
1549 if (AVAILABLE(skb) < sizeof(*psrc) +
1550 first*sizeof(struct mld2_grec)) {
1551 if (truncate && !first)
1552 break; /* truncate these */
1554 pgr->grec_nsrcs = htons(scount);
1557 skb = mld_newpack(idev, dev->mtu);
1562 skb = add_grhead(skb, pmc, type, &pgr);
1567 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1568 *psrc = psf->sf_addr;
1570 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1571 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1573 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1575 psf_prev->sf_next = psf->sf_next;
1577 *psf_list = psf->sf_next;
1587 if (type == MLD2_ALLOW_NEW_SOURCES ||
1588 type == MLD2_BLOCK_OLD_SOURCES)
1590 if (pmc->mca_crcount || isquery) {
1591 /* make sure we have room for group header */
1592 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1594 skb = NULL; /* add_grhead will get a new one */
1596 skb = add_grhead(skb, pmc, type, &pgr);
1600 pgr->grec_nsrcs = htons(scount);
1603 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1607 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1609 struct sk_buff *skb = NULL;
1612 read_lock_bh(&idev->lock);
1614 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1615 if (pmc->mca_flags & MAF_NOREPORT)
1617 spin_lock_bh(&pmc->mca_lock);
1618 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1619 type = MLD2_MODE_IS_EXCLUDE;
1621 type = MLD2_MODE_IS_INCLUDE;
1622 skb = add_grec(skb, pmc, type, 0, 0);
1623 spin_unlock_bh(&pmc->mca_lock);
1626 spin_lock_bh(&pmc->mca_lock);
1627 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1628 type = MLD2_MODE_IS_EXCLUDE;
1630 type = MLD2_MODE_IS_INCLUDE;
1631 skb = add_grec(skb, pmc, type, 0, 0);
1632 spin_unlock_bh(&pmc->mca_lock);
1634 read_unlock_bh(&idev->lock);
1640 * remove zero-count source records from a source filter list
1642 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1644 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1647 for (psf=*ppsf; psf; psf = psf_next) {
1648 psf_next = psf->sf_next;
1649 if (psf->sf_crcount == 0) {
1651 psf_prev->sf_next = psf->sf_next;
1653 *ppsf = psf->sf_next;
1660 static void mld_send_cr(struct inet6_dev *idev)
1662 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1663 struct sk_buff *skb = NULL;
1666 read_lock_bh(&idev->lock);
1667 spin_lock(&idev->mc_lock);
1671 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1672 pmc_next = pmc->next;
1673 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1674 type = MLD2_BLOCK_OLD_SOURCES;
1675 dtype = MLD2_BLOCK_OLD_SOURCES;
1676 skb = add_grec(skb, pmc, type, 1, 0);
1677 skb = add_grec(skb, pmc, dtype, 1, 1);
1679 if (pmc->mca_crcount) {
1680 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1681 type = MLD2_CHANGE_TO_INCLUDE;
1682 skb = add_grec(skb, pmc, type, 1, 0);
1685 if (pmc->mca_crcount == 0) {
1686 mld_clear_zeros(&pmc->mca_tomb);
1687 mld_clear_zeros(&pmc->mca_sources);
1690 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1691 !pmc->mca_sources) {
1693 pmc_prev->next = pmc_next;
1695 idev->mc_tomb = pmc_next;
1696 in6_dev_put(pmc->idev);
1701 spin_unlock(&idev->mc_lock);
1704 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1705 spin_lock_bh(&pmc->mca_lock);
1706 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1707 type = MLD2_BLOCK_OLD_SOURCES;
1708 dtype = MLD2_ALLOW_NEW_SOURCES;
1710 type = MLD2_ALLOW_NEW_SOURCES;
1711 dtype = MLD2_BLOCK_OLD_SOURCES;
1713 skb = add_grec(skb, pmc, type, 0, 0);
1714 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1716 /* filter mode changes */
1717 if (pmc->mca_crcount) {
1718 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1719 type = MLD2_CHANGE_TO_EXCLUDE;
1721 type = MLD2_CHANGE_TO_INCLUDE;
1722 skb = add_grec(skb, pmc, type, 0, 0);
1725 spin_unlock_bh(&pmc->mca_lock);
1727 read_unlock_bh(&idev->lock);
1730 (void) mld_sendpack(skb);
1733 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1735 struct net *net = dev_net(dev);
1736 struct sock *sk = net->ipv6.igmp_sk;
1737 struct inet6_dev *idev;
1738 struct sk_buff *skb;
1739 struct mld_msg *hdr;
1740 const struct in6_addr *snd_addr, *saddr;
1741 struct in6_addr addr_buf;
1742 int hlen = LL_RESERVED_SPACE(dev);
1743 int tlen = dev->needed_tailroom;
1744 int err, len, payload_len, full_len;
1745 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1746 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1749 struct dst_entry *dst;
1751 if (type == ICMPV6_MGM_REDUCTION)
1752 snd_addr = &in6addr_linklocal_allrouters;
1756 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1757 payload_len = len + sizeof(ra);
1758 full_len = sizeof(struct ipv6hdr) + payload_len;
1761 IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
1762 IPSTATS_MIB_OUT, full_len);
1765 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
1769 IP6_INC_STATS(net, __in6_dev_get(dev),
1770 IPSTATS_MIB_OUTDISCARDS);
1774 skb->priority = TC_PRIO_CONTROL;
1775 skb_reserve(skb, hlen);
1777 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1778 /* <draft-ietf-magma-mld-source-05.txt>:
1779 * use unspecified address as the source address
1780 * when a valid link-local address is not available.
1782 saddr = &in6addr_any;
1786 ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
1788 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1790 hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg));
1791 memset(hdr, 0, sizeof(struct mld_msg));
1792 hdr->mld_type = type;
1793 hdr->mld_mca = *addr;
1795 hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
1797 csum_partial(hdr, len, 0));
1800 idev = __in6_dev_get(skb->dev);
1802 icmpv6_flow_init(sk, &fl6, type,
1803 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1805 dst = icmp6_dst_alloc(skb->dev, &fl6);
1811 skb_dst_set(skb, dst);
1812 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1816 ICMP6MSGOUT_INC_STATS(net, idev, type);
1817 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
1818 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
1820 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
1830 static void mld_resend_report(struct inet6_dev *idev)
1832 if (MLD_V1_SEEN(idev)) {
1833 struct ifmcaddr6 *mcaddr;
1834 read_lock_bh(&idev->lock);
1835 for (mcaddr = idev->mc_list; mcaddr; mcaddr = mcaddr->next) {
1836 if (!(mcaddr->mca_flags & MAF_NOREPORT))
1837 igmp6_send(&mcaddr->mca_addr, idev->dev,
1840 read_unlock_bh(&idev->lock);
1842 mld_send_report(idev, NULL);
1846 void ipv6_mc_dad_complete(struct inet6_dev *idev)
1848 idev->mc_dad_count = idev->mc_qrv;
1849 if (idev->mc_dad_count) {
1850 mld_resend_report(idev);
1851 idev->mc_dad_count--;
1852 if (idev->mc_dad_count)
1853 mld_dad_start_timer(idev, idev->mc_maxdelay);
1857 static void mld_dad_timer_expire(unsigned long data)
1859 struct inet6_dev *idev = (struct inet6_dev *)data;
1861 mld_resend_report(idev);
1862 if (idev->mc_dad_count) {
1863 idev->mc_dad_count--;
1864 if (idev->mc_dad_count)
1865 mld_dad_start_timer(idev, idev->mc_maxdelay);
1867 __in6_dev_put(idev);
1870 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1871 const struct in6_addr *psfsrc)
1873 struct ip6_sf_list *psf, *psf_prev;
1877 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1878 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1882 if (!psf || psf->sf_count[sfmode] == 0) {
1883 /* source filter not found, or count wrong => bug */
1886 psf->sf_count[sfmode]--;
1887 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1888 struct inet6_dev *idev = pmc->idev;
1890 /* no more filters for this source */
1892 psf_prev->sf_next = psf->sf_next;
1894 pmc->mca_sources = psf->sf_next;
1895 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1896 !MLD_V1_SEEN(idev)) {
1897 psf->sf_crcount = idev->mc_qrv;
1898 psf->sf_next = pmc->mca_tomb;
1899 pmc->mca_tomb = psf;
1907 static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
1908 int sfmode, int sfcount, const struct in6_addr *psfsrc,
1911 struct ifmcaddr6 *pmc;
1917 read_lock_bh(&idev->lock);
1918 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1919 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1923 /* MCA not found?? bug */
1924 read_unlock_bh(&idev->lock);
1927 spin_lock_bh(&pmc->mca_lock);
1930 if (!pmc->mca_sfcount[sfmode]) {
1931 spin_unlock_bh(&pmc->mca_lock);
1932 read_unlock_bh(&idev->lock);
1935 pmc->mca_sfcount[sfmode]--;
1938 for (i=0; i<sfcount; i++) {
1939 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1941 changerec |= rv > 0;
1945 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1946 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1947 pmc->mca_sfcount[MCAST_INCLUDE]) {
1948 struct ip6_sf_list *psf;
1950 /* filter mode change */
1951 pmc->mca_sfmode = MCAST_INCLUDE;
1952 pmc->mca_crcount = idev->mc_qrv;
1953 idev->mc_ifc_count = pmc->mca_crcount;
1954 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1955 psf->sf_crcount = 0;
1956 mld_ifc_event(pmc->idev);
1957 } else if (sf_setstate(pmc) || changerec)
1958 mld_ifc_event(pmc->idev);
1959 spin_unlock_bh(&pmc->mca_lock);
1960 read_unlock_bh(&idev->lock);
1965 * Add multicast single-source filter to the interface list
1967 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1968 const struct in6_addr *psfsrc)
1970 struct ip6_sf_list *psf, *psf_prev;
1973 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1974 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1979 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1983 psf->sf_addr = *psfsrc;
1985 psf_prev->sf_next = psf;
1987 pmc->mca_sources = psf;
1989 psf->sf_count[sfmode]++;
1993 static void sf_markstate(struct ifmcaddr6 *pmc)
1995 struct ip6_sf_list *psf;
1996 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1998 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1999 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2000 psf->sf_oldin = mca_xcount ==
2001 psf->sf_count[MCAST_EXCLUDE] &&
2002 !psf->sf_count[MCAST_INCLUDE];
2004 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2007 static int sf_setstate(struct ifmcaddr6 *pmc)
2009 struct ip6_sf_list *psf, *dpsf;
2010 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2011 int qrv = pmc->idev->mc_qrv;
2015 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
2016 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2017 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2018 !psf->sf_count[MCAST_INCLUDE];
2020 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2022 if (!psf->sf_oldin) {
2023 struct ip6_sf_list *prev = NULL;
2025 for (dpsf=pmc->mca_tomb; dpsf;
2026 dpsf=dpsf->sf_next) {
2027 if (ipv6_addr_equal(&dpsf->sf_addr,
2034 prev->sf_next = dpsf->sf_next;
2036 pmc->mca_tomb = dpsf->sf_next;
2039 psf->sf_crcount = qrv;
2042 } else if (psf->sf_oldin) {
2043 psf->sf_crcount = 0;
2045 * add or update "delete" records if an active filter
2048 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2049 if (ipv6_addr_equal(&dpsf->sf_addr,
2053 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2057 /* pmc->mca_lock held by callers */
2058 dpsf->sf_next = pmc->mca_tomb;
2059 pmc->mca_tomb = dpsf;
2061 dpsf->sf_crcount = qrv;
2069 * Add multicast source filter list to the interface list
2071 static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2072 int sfmode, int sfcount, const struct in6_addr *psfsrc,
2075 struct ifmcaddr6 *pmc;
2081 read_lock_bh(&idev->lock);
2082 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2083 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2087 /* MCA not found?? bug */
2088 read_unlock_bh(&idev->lock);
2091 spin_lock_bh(&pmc->mca_lock);
2094 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2096 pmc->mca_sfcount[sfmode]++;
2098 for (i=0; i<sfcount; i++) {
2099 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2107 pmc->mca_sfcount[sfmode]--;
2109 ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2110 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2111 struct ip6_sf_list *psf;
2113 /* filter mode change */
2114 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2115 pmc->mca_sfmode = MCAST_EXCLUDE;
2116 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2117 pmc->mca_sfmode = MCAST_INCLUDE;
2118 /* else no filters; keep old mode for reports */
2120 pmc->mca_crcount = idev->mc_qrv;
2121 idev->mc_ifc_count = pmc->mca_crcount;
2122 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2123 psf->sf_crcount = 0;
2124 mld_ifc_event(idev);
2125 } else if (sf_setstate(pmc))
2126 mld_ifc_event(idev);
2127 spin_unlock_bh(&pmc->mca_lock);
2128 read_unlock_bh(&idev->lock);
2132 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2134 struct ip6_sf_list *psf, *nextpsf;
2136 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2137 nextpsf = psf->sf_next;
2140 pmc->mca_tomb = NULL;
2141 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2142 nextpsf = psf->sf_next;
2145 pmc->mca_sources = NULL;
2146 pmc->mca_sfmode = MCAST_EXCLUDE;
2147 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2148 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2152 static void igmp6_join_group(struct ifmcaddr6 *ma)
2154 unsigned long delay;
2156 if (ma->mca_flags & MAF_NOREPORT)
2159 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2161 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2163 spin_lock_bh(&ma->mca_lock);
2164 if (del_timer(&ma->mca_timer)) {
2165 atomic_dec(&ma->mca_refcnt);
2166 delay = ma->mca_timer.expires - jiffies;
2169 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2170 atomic_inc(&ma->mca_refcnt);
2171 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2172 spin_unlock_bh(&ma->mca_lock);
2175 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2176 struct inet6_dev *idev)
2180 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2181 * so no other readers or writers of iml or its sflist
2184 /* any-source empty exclude case */
2185 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2187 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2188 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2189 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2194 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2196 if (MLD_V1_SEEN(ma->idev)) {
2197 if (ma->mca_flags & MAF_LAST_REPORTER)
2198 igmp6_send(&ma->mca_addr, ma->idev->dev,
2199 ICMPV6_MGM_REDUCTION);
2201 mld_add_delrec(ma->idev, ma);
2202 mld_ifc_event(ma->idev);
2206 static void mld_gq_timer_expire(unsigned long data)
2208 struct inet6_dev *idev = (struct inet6_dev *)data;
2210 idev->mc_gq_running = 0;
2211 mld_send_report(idev, NULL);
2212 __in6_dev_put(idev);
2215 static void mld_ifc_timer_expire(unsigned long data)
2217 struct inet6_dev *idev = (struct inet6_dev *)data;
2220 if (idev->mc_ifc_count) {
2221 idev->mc_ifc_count--;
2222 if (idev->mc_ifc_count)
2223 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2225 __in6_dev_put(idev);
2228 static void mld_ifc_event(struct inet6_dev *idev)
2230 if (MLD_V1_SEEN(idev))
2232 idev->mc_ifc_count = idev->mc_qrv;
2233 mld_ifc_start_timer(idev, 1);
2237 static void igmp6_timer_handler(unsigned long data)
2239 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2241 if (MLD_V1_SEEN(ma->idev))
2242 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2244 mld_send_report(ma->idev, ma);
2246 spin_lock(&ma->mca_lock);
2247 ma->mca_flags |= MAF_LAST_REPORTER;
2248 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2249 spin_unlock(&ma->mca_lock);
2253 /* Device changing type */
2255 void ipv6_mc_unmap(struct inet6_dev *idev)
2257 struct ifmcaddr6 *i;
2259 /* Install multicast list, except for all-nodes (already installed) */
2261 read_lock_bh(&idev->lock);
2262 for (i = idev->mc_list; i; i = i->next)
2263 igmp6_group_dropped(i);
2264 read_unlock_bh(&idev->lock);
2267 void ipv6_mc_remap(struct inet6_dev *idev)
2272 /* Device going down */
2274 void ipv6_mc_down(struct inet6_dev *idev)
2276 struct ifmcaddr6 *i;
2278 /* Withdraw multicast list */
2280 read_lock_bh(&idev->lock);
2281 idev->mc_ifc_count = 0;
2282 if (del_timer(&idev->mc_ifc_timer))
2283 __in6_dev_put(idev);
2284 idev->mc_gq_running = 0;
2285 if (del_timer(&idev->mc_gq_timer))
2286 __in6_dev_put(idev);
2287 if (del_timer(&idev->mc_dad_timer))
2288 __in6_dev_put(idev);
2290 for (i = idev->mc_list; i; i=i->next)
2291 igmp6_group_dropped(i);
2292 read_unlock_bh(&idev->lock);
2294 mld_clear_delrec(idev);
2298 /* Device going up */
2300 void ipv6_mc_up(struct inet6_dev *idev)
2302 struct ifmcaddr6 *i;
2304 /* Install multicast list, except for all-nodes (already installed) */
2306 read_lock_bh(&idev->lock);
2307 for (i = idev->mc_list; i; i=i->next)
2308 igmp6_group_added(i);
2309 read_unlock_bh(&idev->lock);
2312 /* IPv6 device initialization. */
2314 void ipv6_mc_init_dev(struct inet6_dev *idev)
2316 write_lock_bh(&idev->lock);
2317 spin_lock_init(&idev->mc_lock);
2318 idev->mc_gq_running = 0;
2319 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2320 (unsigned long)idev);
2321 idev->mc_tomb = NULL;
2322 idev->mc_ifc_count = 0;
2323 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2324 (unsigned long)idev);
2325 setup_timer(&idev->mc_dad_timer, mld_dad_timer_expire,
2326 (unsigned long)idev);
2327 idev->mc_qrv = MLD_QRV_DEFAULT;
2328 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2329 idev->mc_v1_seen = 0;
2330 write_unlock_bh(&idev->lock);
2334 * Device is about to be destroyed: clean up.
2337 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2339 struct ifmcaddr6 *i;
2341 /* Deactivate timers */
2344 /* Delete all-nodes address. */
2345 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2346 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2349 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
2351 if (idev->cnf.forwarding)
2352 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
2354 write_lock_bh(&idev->lock);
2355 while ((i = idev->mc_list) != NULL) {
2356 idev->mc_list = i->next;
2357 write_unlock_bh(&idev->lock);
2359 igmp6_group_dropped(i);
2362 write_lock_bh(&idev->lock);
2364 write_unlock_bh(&idev->lock);
2367 #ifdef CONFIG_PROC_FS
2368 struct igmp6_mc_iter_state {
2369 struct seq_net_private p;
2370 struct net_device *dev;
2371 struct inet6_dev *idev;
2374 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2376 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2378 struct ifmcaddr6 *im = NULL;
2379 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2380 struct net *net = seq_file_net(seq);
2383 for_each_netdev_rcu(net, state->dev) {
2384 struct inet6_dev *idev;
2385 idev = __in6_dev_get(state->dev);
2388 read_lock_bh(&idev->lock);
2394 read_unlock_bh(&idev->lock);
2399 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2401 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2405 if (likely(state->idev != NULL))
2406 read_unlock_bh(&state->idev->lock);
2408 state->dev = next_net_device_rcu(state->dev);
2413 state->idev = __in6_dev_get(state->dev);
2416 read_lock_bh(&state->idev->lock);
2417 im = state->idev->mc_list;
2422 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2424 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2426 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2428 return pos ? NULL : im;
2431 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2435 return igmp6_mc_get_idx(seq, *pos);
2438 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2440 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2446 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2449 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2451 if (likely(state->idev != NULL)) {
2452 read_unlock_bh(&state->idev->lock);
2459 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2461 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2462 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2465 "%-4d %-15s %pi6 %5d %08X %ld\n",
2466 state->dev->ifindex, state->dev->name,
2468 im->mca_users, im->mca_flags,
2469 (im->mca_flags&MAF_TIMER_RUNNING) ?
2470 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2474 static const struct seq_operations igmp6_mc_seq_ops = {
2475 .start = igmp6_mc_seq_start,
2476 .next = igmp6_mc_seq_next,
2477 .stop = igmp6_mc_seq_stop,
2478 .show = igmp6_mc_seq_show,
2481 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2483 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2484 sizeof(struct igmp6_mc_iter_state));
2487 static const struct file_operations igmp6_mc_seq_fops = {
2488 .owner = THIS_MODULE,
2489 .open = igmp6_mc_seq_open,
2491 .llseek = seq_lseek,
2492 .release = seq_release_net,
2495 struct igmp6_mcf_iter_state {
2496 struct seq_net_private p;
2497 struct net_device *dev;
2498 struct inet6_dev *idev;
2499 struct ifmcaddr6 *im;
2502 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2504 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2506 struct ip6_sf_list *psf = NULL;
2507 struct ifmcaddr6 *im = NULL;
2508 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2509 struct net *net = seq_file_net(seq);
2513 for_each_netdev_rcu(net, state->dev) {
2514 struct inet6_dev *idev;
2515 idev = __in6_dev_get(state->dev);
2516 if (unlikely(idev == NULL))
2518 read_lock_bh(&idev->lock);
2520 if (likely(im != NULL)) {
2521 spin_lock_bh(&im->mca_lock);
2522 psf = im->mca_sources;
2523 if (likely(psf != NULL)) {
2528 spin_unlock_bh(&im->mca_lock);
2530 read_unlock_bh(&idev->lock);
2535 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2537 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2541 spin_unlock_bh(&state->im->mca_lock);
2542 state->im = state->im->next;
2543 while (!state->im) {
2544 if (likely(state->idev != NULL))
2545 read_unlock_bh(&state->idev->lock);
2547 state->dev = next_net_device_rcu(state->dev);
2552 state->idev = __in6_dev_get(state->dev);
2555 read_lock_bh(&state->idev->lock);
2556 state->im = state->idev->mc_list;
2560 spin_lock_bh(&state->im->mca_lock);
2561 psf = state->im->mca_sources;
2567 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2569 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2571 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2573 return pos ? NULL : psf;
2576 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2580 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2583 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2585 struct ip6_sf_list *psf;
2586 if (v == SEQ_START_TOKEN)
2587 psf = igmp6_mcf_get_first(seq);
2589 psf = igmp6_mcf_get_next(seq, v);
2594 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2597 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2598 if (likely(state->im != NULL)) {
2599 spin_unlock_bh(&state->im->mca_lock);
2602 if (likely(state->idev != NULL)) {
2603 read_unlock_bh(&state->idev->lock);
2610 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2612 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2613 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2615 if (v == SEQ_START_TOKEN) {
2618 "%32s %32s %6s %6s\n", "Idx",
2619 "Device", "Multicast Address",
2620 "Source Address", "INC", "EXC");
2623 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
2624 state->dev->ifindex, state->dev->name,
2625 &state->im->mca_addr,
2627 psf->sf_count[MCAST_INCLUDE],
2628 psf->sf_count[MCAST_EXCLUDE]);
2633 static const struct seq_operations igmp6_mcf_seq_ops = {
2634 .start = igmp6_mcf_seq_start,
2635 .next = igmp6_mcf_seq_next,
2636 .stop = igmp6_mcf_seq_stop,
2637 .show = igmp6_mcf_seq_show,
2640 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2642 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2643 sizeof(struct igmp6_mcf_iter_state));
2646 static const struct file_operations igmp6_mcf_seq_fops = {
2647 .owner = THIS_MODULE,
2648 .open = igmp6_mcf_seq_open,
2650 .llseek = seq_lseek,
2651 .release = seq_release_net,
2654 static int __net_init igmp6_proc_init(struct net *net)
2659 if (!proc_create("igmp6", S_IRUGO, net->proc_net, &igmp6_mc_seq_fops))
2661 if (!proc_create("mcfilter6", S_IRUGO, net->proc_net,
2662 &igmp6_mcf_seq_fops))
2663 goto out_proc_net_igmp6;
2670 remove_proc_entry("igmp6", net->proc_net);
2674 static void __net_exit igmp6_proc_exit(struct net *net)
2676 remove_proc_entry("mcfilter6", net->proc_net);
2677 remove_proc_entry("igmp6", net->proc_net);
2680 static inline int igmp6_proc_init(struct net *net)
2684 static inline void igmp6_proc_exit(struct net *net)
2689 static int __net_init igmp6_net_init(struct net *net)
2693 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2694 SOCK_RAW, IPPROTO_ICMPV6, net);
2696 pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
2701 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
2703 err = igmp6_proc_init(net);
2705 goto out_sock_create;
2710 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2714 static void __net_exit igmp6_net_exit(struct net *net)
2716 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2717 igmp6_proc_exit(net);
2720 static struct pernet_operations igmp6_net_ops = {
2721 .init = igmp6_net_init,
2722 .exit = igmp6_net_exit,
2725 int __init igmp6_init(void)
2727 return register_pernet_subsys(&igmp6_net_ops);
2730 void igmp6_cleanup(void)
2732 unregister_pernet_subsys(&igmp6_net_ops);