2 * Multicast support for IPv6
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: mcast.c,v 1.40 2002/02/08 03:57:19 davem Exp $
10 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
20 * yoshfuji : fix format of router-alert option
21 * YOSHIFUJI Hideaki @USAGI:
22 * Fixed source address for MLD message based on
23 * <draft-ietf-magma-mld-source-05.txt>.
24 * YOSHIFUJI Hideaki @USAGI:
25 * - Ignore Queries for invalid addresses.
26 * - MLD for link-local addresses.
27 * David L Stevens <dlstevens@us.ibm.com>:
31 #include <linux/module.h>
32 #include <linux/errno.h>
33 #include <linux/types.h>
34 #include <linux/string.h>
35 #include <linux/socket.h>
36 #include <linux/sockios.h>
37 #include <linux/jiffies.h>
38 #include <linux/times.h>
39 #include <linux/net.h>
41 #include <linux/in6.h>
42 #include <linux/netdevice.h>
43 #include <linux/if_arp.h>
44 #include <linux/route.h>
45 #include <linux/init.h>
46 #include <linux/proc_fs.h>
47 #include <linux/seq_file.h>
49 #include <linux/netfilter.h>
50 #include <linux/netfilter_ipv6.h>
52 #include <net/net_namespace.h>
57 #include <net/protocol.h>
58 #include <net/if_inet6.h>
59 #include <net/ndisc.h>
60 #include <net/addrconf.h>
61 #include <net/ip6_route.h>
62 #include <net/inet_common.h>
64 #include <net/ip6_checksum.h>
66 /* Set to 3 to get tracing... */
70 #define MDBG(x) printk x
76 * These header formats should be in a separate include file, but icmpv6.h
77 * doesn't have in6_addr defined in all cases, there is no __u128, and no
78 * other files reference these.
83 /* Multicast Listener Discovery version 2 headers */
89 struct in6_addr grec_mca;
90 struct in6_addr grec_src[0];
99 struct mld2_grec grec[0];
109 #if defined(__LITTLE_ENDIAN_BITFIELD)
113 #elif defined(__BIG_ENDIAN_BITFIELD)
118 #error "Please fix <asm/byteorder.h>"
122 struct in6_addr srcs[0];
125 static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
127 /* Big mc list lock for all the sockets */
128 static DEFINE_RWLOCK(ipv6_sk_mc_lock);
130 static void igmp6_join_group(struct ifmcaddr6 *ma);
131 static void igmp6_leave_group(struct ifmcaddr6 *ma);
132 static void igmp6_timer_handler(unsigned long data);
134 static void mld_gq_timer_expire(unsigned long data);
135 static void mld_ifc_timer_expire(unsigned long data);
136 static void mld_ifc_event(struct inet6_dev *idev);
137 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
138 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
139 static void mld_clear_delrec(struct inet6_dev *idev);
140 static int sf_setstate(struct ifmcaddr6 *pmc);
141 static void sf_markstate(struct ifmcaddr6 *pmc);
142 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
143 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
144 int sfmode, int sfcount, struct in6_addr *psfsrc,
146 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
147 int sfmode, int sfcount, struct in6_addr *psfsrc,
149 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
150 struct inet6_dev *idev);
153 #define IGMP6_UNSOLICITED_IVAL (10*HZ)
154 #define MLD_QRV_DEFAULT 2
156 #define MLD_V1_SEEN(idev) (ipv6_devconf.force_mld_version == 1 || \
157 (idev)->cnf.force_mld_version == 1 || \
158 ((idev)->mc_v1_seen && \
159 time_before(jiffies, (idev)->mc_v1_seen)))
161 #define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
162 #define MLDV2_EXP(thresh, nbmant, nbexp, value) \
163 ((value) < (thresh) ? (value) : \
164 ((MLDV2_MASK(value, nbmant) | (1<<(nbmant))) << \
165 (MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp))))
167 #define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
169 #define IPV6_MLD_MAX_MSF 64
171 int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
174 * socket join on multicast group
177 int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
179 struct net_device *dev = NULL;
180 struct ipv6_mc_socklist *mc_lst;
181 struct ipv6_pinfo *np = inet6_sk(sk);
182 struct net *net = sock_net(sk);
185 if (!ipv6_addr_is_multicast(addr))
188 read_lock_bh(&ipv6_sk_mc_lock);
189 for (mc_lst=np->ipv6_mc_list; mc_lst; mc_lst=mc_lst->next) {
190 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
191 ipv6_addr_equal(&mc_lst->addr, addr)) {
192 read_unlock_bh(&ipv6_sk_mc_lock);
196 read_unlock_bh(&ipv6_sk_mc_lock);
198 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
204 ipv6_addr_copy(&mc_lst->addr, addr);
208 rt = rt6_lookup(net, addr, NULL, 0, 0);
212 dst_release(&rt->u.dst);
215 dev = dev_get_by_index(net, ifindex);
218 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
222 mc_lst->ifindex = dev->ifindex;
223 mc_lst->sfmode = MCAST_EXCLUDE;
224 rwlock_init(&mc_lst->sflock);
225 mc_lst->sflist = NULL;
228 * now add/increase the group membership on the device
231 err = ipv6_dev_mc_inc(dev, addr);
234 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
239 write_lock_bh(&ipv6_sk_mc_lock);
240 mc_lst->next = np->ipv6_mc_list;
241 np->ipv6_mc_list = mc_lst;
242 write_unlock_bh(&ipv6_sk_mc_lock);
250 * socket leave on multicast group
252 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
254 struct ipv6_pinfo *np = inet6_sk(sk);
255 struct ipv6_mc_socklist *mc_lst, **lnk;
256 struct net *net = sock_net(sk);
258 write_lock_bh(&ipv6_sk_mc_lock);
259 for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
260 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
261 ipv6_addr_equal(&mc_lst->addr, addr)) {
262 struct net_device *dev;
265 write_unlock_bh(&ipv6_sk_mc_lock);
267 dev = dev_get_by_index(net, mc_lst->ifindex);
269 struct inet6_dev *idev = in6_dev_get(dev);
271 (void) ip6_mc_leave_src(sk, mc_lst, idev);
273 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
278 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
279 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
283 write_unlock_bh(&ipv6_sk_mc_lock);
285 return -EADDRNOTAVAIL;
288 static struct inet6_dev *ip6_mc_find_dev(struct net *net,
289 struct in6_addr *group,
292 struct net_device *dev = NULL;
293 struct inet6_dev *idev = NULL;
298 rt = rt6_lookup(net, group, NULL, 0, 0);
302 dst_release(&rt->u.dst);
305 dev = dev_get_by_index(net, ifindex);
309 idev = in6_dev_get(dev);
314 read_lock_bh(&idev->lock);
316 read_unlock_bh(&idev->lock);
324 void ipv6_sock_mc_close(struct sock *sk)
326 struct ipv6_pinfo *np = inet6_sk(sk);
327 struct ipv6_mc_socklist *mc_lst;
328 struct net *net = sock_net(sk);
330 write_lock_bh(&ipv6_sk_mc_lock);
331 while ((mc_lst = np->ipv6_mc_list) != NULL) {
332 struct net_device *dev;
334 np->ipv6_mc_list = mc_lst->next;
335 write_unlock_bh(&ipv6_sk_mc_lock);
337 dev = dev_get_by_index(net, mc_lst->ifindex);
339 struct inet6_dev *idev = in6_dev_get(dev);
341 (void) ip6_mc_leave_src(sk, mc_lst, idev);
343 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
348 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
350 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
352 write_lock_bh(&ipv6_sk_mc_lock);
354 write_unlock_bh(&ipv6_sk_mc_lock);
357 int ip6_mc_source(int add, int omode, struct sock *sk,
358 struct group_source_req *pgsr)
360 struct in6_addr *source, *group;
361 struct ipv6_mc_socklist *pmc;
362 struct net_device *dev;
363 struct inet6_dev *idev;
364 struct ipv6_pinfo *inet6 = inet6_sk(sk);
365 struct ip6_sf_socklist *psl;
366 struct net *net = sock_net(sk);
372 if (pgsr->gsr_group.ss_family != AF_INET6 ||
373 pgsr->gsr_source.ss_family != AF_INET6)
376 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
377 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
379 if (!ipv6_addr_is_multicast(group))
382 idev = ip6_mc_find_dev(net, group, pgsr->gsr_interface);
387 err = -EADDRNOTAVAIL;
389 read_lock_bh(&ipv6_sk_mc_lock);
390 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
391 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
393 if (ipv6_addr_equal(&pmc->addr, group))
396 if (!pmc) { /* must have a prior join */
400 /* if a source filter was set, must be the same mode as before */
402 if (pmc->sfmode != omode) {
406 } else if (pmc->sfmode != omode) {
407 /* allow mode switches for empty-set filters */
408 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
409 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
413 write_lock_bh(&pmc->sflock);
419 goto done; /* err = -EADDRNOTAVAIL */
421 for (i=0; i<psl->sl_count; i++) {
422 rv = memcmp(&psl->sl_addr[i], source,
423 sizeof(struct in6_addr));
427 if (rv) /* source not found */
428 goto done; /* err = -EADDRNOTAVAIL */
430 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
431 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
436 /* update the interface filter */
437 ip6_mc_del_src(idev, group, omode, 1, source, 1);
439 for (j=i+1; j<psl->sl_count; j++)
440 psl->sl_addr[j-1] = psl->sl_addr[j];
445 /* else, add a new source to the filter */
447 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
451 if (!psl || psl->sl_count == psl->sl_max) {
452 struct ip6_sf_socklist *newpsl;
453 int count = IP6_SFBLOCK;
456 count += psl->sl_max;
457 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
462 newpsl->sl_max = count;
463 newpsl->sl_count = count - IP6_SFBLOCK;
465 for (i=0; i<psl->sl_count; i++)
466 newpsl->sl_addr[i] = psl->sl_addr[i];
467 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
469 pmc->sflist = psl = newpsl;
471 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
472 for (i=0; i<psl->sl_count; i++) {
473 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
477 if (rv == 0) /* address already there is an error */
479 for (j=psl->sl_count-1; j>=i; j--)
480 psl->sl_addr[j+1] = psl->sl_addr[j];
481 psl->sl_addr[i] = *source;
484 /* update the interface list */
485 ip6_mc_add_src(idev, group, omode, 1, source, 1);
488 write_unlock_bh(&pmc->sflock);
489 read_unlock_bh(&ipv6_sk_mc_lock);
490 read_unlock_bh(&idev->lock);
494 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
498 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
500 struct in6_addr *group;
501 struct ipv6_mc_socklist *pmc;
502 struct net_device *dev;
503 struct inet6_dev *idev;
504 struct ipv6_pinfo *inet6 = inet6_sk(sk);
505 struct ip6_sf_socklist *newpsl, *psl;
506 struct net *net = sock_net(sk);
510 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
512 if (!ipv6_addr_is_multicast(group))
514 if (gsf->gf_fmode != MCAST_INCLUDE &&
515 gsf->gf_fmode != MCAST_EXCLUDE)
518 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
525 read_lock_bh(&ipv6_sk_mc_lock);
527 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
532 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
533 if (pmc->ifindex != gsf->gf_interface)
535 if (ipv6_addr_equal(&pmc->addr, group))
538 if (!pmc) { /* must have a prior join */
542 if (gsf->gf_numsrc) {
543 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
549 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
550 for (i=0; i<newpsl->sl_count; ++i) {
551 struct sockaddr_in6 *psin6;
553 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
554 newpsl->sl_addr[i] = psin6->sin6_addr;
556 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
557 newpsl->sl_count, newpsl->sl_addr, 0);
559 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
564 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
567 write_lock_bh(&pmc->sflock);
570 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
571 psl->sl_count, psl->sl_addr, 0);
572 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
574 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
575 pmc->sflist = newpsl;
576 pmc->sfmode = gsf->gf_fmode;
577 write_unlock_bh(&pmc->sflock);
580 read_unlock_bh(&ipv6_sk_mc_lock);
581 read_unlock_bh(&idev->lock);
585 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
589 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
590 struct group_filter __user *optval, int __user *optlen)
592 int err, i, count, copycount;
593 struct in6_addr *group;
594 struct ipv6_mc_socklist *pmc;
595 struct inet6_dev *idev;
596 struct net_device *dev;
597 struct ipv6_pinfo *inet6 = inet6_sk(sk);
598 struct ip6_sf_socklist *psl;
599 struct net *net = sock_net(sk);
601 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
603 if (!ipv6_addr_is_multicast(group))
606 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
613 err = -EADDRNOTAVAIL;
615 * changes to the ipv6_mc_list require the socket lock and
616 * a read lock on ip6_sk_mc_lock. We have the socket lock,
617 * so reading the list is safe.
620 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
621 if (pmc->ifindex != gsf->gf_interface)
623 if (ipv6_addr_equal(group, &pmc->addr))
626 if (!pmc) /* must have a prior join */
628 gsf->gf_fmode = pmc->sfmode;
630 count = psl ? psl->sl_count : 0;
631 read_unlock_bh(&idev->lock);
635 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
636 gsf->gf_numsrc = count;
637 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
638 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
641 /* changes to psl require the socket lock, a read lock on
642 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
643 * have the socket lock, so reading here is safe.
645 for (i=0; i<copycount; i++) {
646 struct sockaddr_in6 *psin6;
647 struct sockaddr_storage ss;
649 psin6 = (struct sockaddr_in6 *)&ss;
650 memset(&ss, 0, sizeof(ss));
651 psin6->sin6_family = AF_INET6;
652 psin6->sin6_addr = psl->sl_addr[i];
653 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
658 read_unlock_bh(&idev->lock);
664 int inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
665 const struct in6_addr *src_addr)
667 struct ipv6_pinfo *np = inet6_sk(sk);
668 struct ipv6_mc_socklist *mc;
669 struct ip6_sf_socklist *psl;
672 read_lock(&ipv6_sk_mc_lock);
673 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
674 if (ipv6_addr_equal(&mc->addr, mc_addr))
678 read_unlock(&ipv6_sk_mc_lock);
681 read_lock(&mc->sflock);
684 rv = mc->sfmode == MCAST_EXCLUDE;
688 for (i=0; i<psl->sl_count; i++) {
689 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
692 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
694 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
697 read_unlock(&mc->sflock);
698 read_unlock(&ipv6_sk_mc_lock);
703 static void ma_put(struct ifmcaddr6 *mc)
705 if (atomic_dec_and_test(&mc->mca_refcnt)) {
706 in6_dev_put(mc->idev);
711 static void igmp6_group_added(struct ifmcaddr6 *mc)
713 struct net_device *dev = mc->idev->dev;
714 char buf[MAX_ADDR_LEN];
716 spin_lock_bh(&mc->mca_lock);
717 if (!(mc->mca_flags&MAF_LOADED)) {
718 mc->mca_flags |= MAF_LOADED;
719 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
720 dev_mc_add(dev, buf, dev->addr_len, 0);
722 spin_unlock_bh(&mc->mca_lock);
724 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
727 if (MLD_V1_SEEN(mc->idev)) {
728 igmp6_join_group(mc);
733 mc->mca_crcount = mc->idev->mc_qrv;
734 mld_ifc_event(mc->idev);
737 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
739 struct net_device *dev = mc->idev->dev;
740 char buf[MAX_ADDR_LEN];
742 spin_lock_bh(&mc->mca_lock);
743 if (mc->mca_flags&MAF_LOADED) {
744 mc->mca_flags &= ~MAF_LOADED;
745 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
746 dev_mc_delete(dev, buf, dev->addr_len, 0);
749 if (mc->mca_flags & MAF_NOREPORT)
751 spin_unlock_bh(&mc->mca_lock);
754 igmp6_leave_group(mc);
756 spin_lock_bh(&mc->mca_lock);
757 if (del_timer(&mc->mca_timer))
758 atomic_dec(&mc->mca_refcnt);
760 ip6_mc_clear_src(mc);
761 spin_unlock_bh(&mc->mca_lock);
765 * deleted ifmcaddr6 manipulation
767 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
769 struct ifmcaddr6 *pmc;
771 /* this is an "ifmcaddr6" for convenience; only the fields below
772 * are actually used. In particular, the refcnt and users are not
773 * used for management of the delete list. Using the same structure
774 * for deleted items allows change reports to use common code with
775 * non-deleted or query-response MCA's.
777 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
781 spin_lock_bh(&im->mca_lock);
782 spin_lock_init(&pmc->mca_lock);
783 pmc->idev = im->idev;
785 pmc->mca_addr = im->mca_addr;
786 pmc->mca_crcount = idev->mc_qrv;
787 pmc->mca_sfmode = im->mca_sfmode;
788 if (pmc->mca_sfmode == MCAST_INCLUDE) {
789 struct ip6_sf_list *psf;
791 pmc->mca_tomb = im->mca_tomb;
792 pmc->mca_sources = im->mca_sources;
793 im->mca_tomb = im->mca_sources = NULL;
794 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
795 psf->sf_crcount = pmc->mca_crcount;
797 spin_unlock_bh(&im->mca_lock);
799 write_lock_bh(&idev->mc_lock);
800 pmc->next = idev->mc_tomb;
802 write_unlock_bh(&idev->mc_lock);
805 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
807 struct ifmcaddr6 *pmc, *pmc_prev;
808 struct ip6_sf_list *psf, *psf_next;
810 write_lock_bh(&idev->mc_lock);
812 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
813 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
819 pmc_prev->next = pmc->next;
821 idev->mc_tomb = pmc->next;
823 write_unlock_bh(&idev->mc_lock);
825 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
826 psf_next = psf->sf_next;
829 in6_dev_put(pmc->idev);
834 static void mld_clear_delrec(struct inet6_dev *idev)
836 struct ifmcaddr6 *pmc, *nextpmc;
838 write_lock_bh(&idev->mc_lock);
840 idev->mc_tomb = NULL;
841 write_unlock_bh(&idev->mc_lock);
843 for (; pmc; pmc = nextpmc) {
845 ip6_mc_clear_src(pmc);
846 in6_dev_put(pmc->idev);
850 /* clear dead sources, too */
851 read_lock_bh(&idev->lock);
852 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
853 struct ip6_sf_list *psf, *psf_next;
855 spin_lock_bh(&pmc->mca_lock);
857 pmc->mca_tomb = NULL;
858 spin_unlock_bh(&pmc->mca_lock);
859 for (; psf; psf=psf_next) {
860 psf_next = psf->sf_next;
864 read_unlock_bh(&idev->lock);
869 * device multicast group inc (add if not found)
871 int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
873 struct ifmcaddr6 *mc;
874 struct inet6_dev *idev;
876 idev = in6_dev_get(dev);
881 write_lock_bh(&idev->lock);
883 write_unlock_bh(&idev->lock);
888 for (mc = idev->mc_list; mc; mc = mc->next) {
889 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
891 write_unlock_bh(&idev->lock);
892 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
900 * not found: create a new one.
903 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
906 write_unlock_bh(&idev->lock);
911 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
913 ipv6_addr_copy(&mc->mca_addr, addr);
916 /* mca_stamp should be updated upon changes */
917 mc->mca_cstamp = mc->mca_tstamp = jiffies;
918 atomic_set(&mc->mca_refcnt, 2);
919 spin_lock_init(&mc->mca_lock);
921 /* initial mode is (EX, empty) */
922 mc->mca_sfmode = MCAST_EXCLUDE;
923 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
925 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
926 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
927 mc->mca_flags |= MAF_NOREPORT;
929 mc->next = idev->mc_list;
931 write_unlock_bh(&idev->lock);
933 mld_del_delrec(idev, &mc->mca_addr);
934 igmp6_group_added(mc);
940 * device multicast group del
942 int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
944 struct ifmcaddr6 *ma, **map;
946 write_lock_bh(&idev->lock);
947 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
948 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
949 if (--ma->mca_users == 0) {
951 write_unlock_bh(&idev->lock);
953 igmp6_group_dropped(ma);
958 write_unlock_bh(&idev->lock);
962 write_unlock_bh(&idev->lock);
967 int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
969 struct inet6_dev *idev = in6_dev_get(dev);
975 err = __ipv6_dev_mc_dec(idev, addr);
983 * identify MLD packets for MLD filter exceptions
985 int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
987 struct icmp6hdr *pic;
989 if (nexthdr != IPPROTO_ICMPV6)
992 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
995 pic = icmp6_hdr(skb);
997 switch (pic->icmp6_type) {
998 case ICMPV6_MGM_QUERY:
999 case ICMPV6_MGM_REPORT:
1000 case ICMPV6_MGM_REDUCTION:
1001 case ICMPV6_MLD2_REPORT:
1010 * check if the interface/address pair is valid
1012 int ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
1013 const struct in6_addr *src_addr)
1015 struct inet6_dev *idev;
1016 struct ifmcaddr6 *mc;
1019 idev = in6_dev_get(dev);
1021 read_lock_bh(&idev->lock);
1022 for (mc = idev->mc_list; mc; mc=mc->next) {
1023 if (ipv6_addr_equal(&mc->mca_addr, group))
1027 if (src_addr && !ipv6_addr_any(src_addr)) {
1028 struct ip6_sf_list *psf;
1030 spin_lock_bh(&mc->mca_lock);
1031 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1032 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1036 rv = psf->sf_count[MCAST_INCLUDE] ||
1037 psf->sf_count[MCAST_EXCLUDE] !=
1038 mc->mca_sfcount[MCAST_EXCLUDE];
1040 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1041 spin_unlock_bh(&mc->mca_lock);
1043 rv = 1; /* don't filter unspecified source */
1045 read_unlock_bh(&idev->lock);
1051 static void mld_gq_start_timer(struct inet6_dev *idev)
1053 int tv = net_random() % idev->mc_maxdelay;
1055 idev->mc_gq_running = 1;
1056 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1060 static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1062 int tv = net_random() % delay;
1064 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1069 * IGMP handling (alias multicast ICMPv6 messages)
1072 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1074 unsigned long delay = resptime;
1076 /* Do not start timer for these addresses */
1077 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1078 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1081 if (del_timer(&ma->mca_timer)) {
1082 atomic_dec(&ma->mca_refcnt);
1083 delay = ma->mca_timer.expires - jiffies;
1086 if (delay >= resptime) {
1088 delay = net_random() % resptime;
1092 ma->mca_timer.expires = jiffies + delay;
1093 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1094 atomic_inc(&ma->mca_refcnt);
1095 ma->mca_flags |= MAF_TIMER_RUNNING;
1098 /* mark EXCLUDE-mode sources */
1099 static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1100 struct in6_addr *srcs)
1102 struct ip6_sf_list *psf;
1106 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1107 if (scount == nsrcs)
1109 for (i=0; i<nsrcs; i++) {
1110 /* skip inactive filters */
1111 if (pmc->mca_sfcount[MCAST_INCLUDE] ||
1112 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1113 psf->sf_count[MCAST_EXCLUDE])
1115 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1121 pmc->mca_flags &= ~MAF_GSQUERY;
1122 if (scount == nsrcs) /* all sources excluded */
1127 static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1128 struct in6_addr *srcs)
1130 struct ip6_sf_list *psf;
1133 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1134 return mld_xmarksources(pmc, nsrcs, srcs);
1136 /* mark INCLUDE-mode sources */
1139 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1140 if (scount == nsrcs)
1142 for (i=0; i<nsrcs; i++) {
1143 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1151 pmc->mca_flags &= ~MAF_GSQUERY;
1154 pmc->mca_flags |= MAF_GSQUERY;
1158 int igmp6_event_query(struct sk_buff *skb)
1160 struct mld2_query *mlh2 = NULL;
1161 struct ifmcaddr6 *ma;
1162 struct in6_addr *group;
1163 unsigned long max_delay;
1164 struct inet6_dev *idev;
1165 struct icmp6hdr *hdr;
1170 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1173 /* compute payload length excluding extension headers */
1174 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
1175 len -= skb_network_header_len(skb);
1177 /* Drop queries with not link local source */
1178 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
1181 idev = in6_dev_get(skb->dev);
1186 hdr = icmp6_hdr(skb);
1187 group = (struct in6_addr *) (hdr + 1);
1188 group_type = ipv6_addr_type(group);
1190 if (group_type != IPV6_ADDR_ANY &&
1191 !(group_type&IPV6_ADDR_MULTICAST)) {
1198 /* MLDv1 router present */
1200 /* Translate milliseconds to jiffies */
1201 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1203 switchback = (idev->mc_qrv + 1) * max_delay;
1204 idev->mc_v1_seen = jiffies + switchback;
1206 /* cancel the interface change timer */
1207 idev->mc_ifc_count = 0;
1208 if (del_timer(&idev->mc_ifc_timer))
1209 __in6_dev_put(idev);
1210 /* clear deleted report items */
1211 mld_clear_delrec(idev);
1212 } else if (len >= 28) {
1213 int srcs_offset = sizeof(struct mld2_query) -
1214 sizeof(struct icmp6hdr);
1215 if (!pskb_may_pull(skb, srcs_offset)) {
1219 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1220 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1223 idev->mc_maxdelay = max_delay;
1225 idev->mc_qrv = mlh2->qrv;
1226 if (group_type == IPV6_ADDR_ANY) { /* general query */
1229 return -EINVAL; /* no sources allowed */
1231 mld_gq_start_timer(idev);
1235 /* mark sources to include, if group & source-specific */
1236 if (mlh2->nsrcs != 0) {
1237 if (!pskb_may_pull(skb, srcs_offset +
1238 ntohs(mlh2->nsrcs) * sizeof(struct in6_addr))) {
1242 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1250 read_lock_bh(&idev->lock);
1251 if (group_type == IPV6_ADDR_ANY) {
1252 for (ma = idev->mc_list; ma; ma=ma->next) {
1253 spin_lock_bh(&ma->mca_lock);
1254 igmp6_group_queried(ma, max_delay);
1255 spin_unlock_bh(&ma->mca_lock);
1258 for (ma = idev->mc_list; ma; ma=ma->next) {
1259 if (!ipv6_addr_equal(group, &ma->mca_addr))
1261 spin_lock_bh(&ma->mca_lock);
1262 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1263 /* gsquery <- gsquery && mark */
1265 ma->mca_flags &= ~MAF_GSQUERY;
1267 /* gsquery <- mark */
1269 ma->mca_flags |= MAF_GSQUERY;
1271 ma->mca_flags &= ~MAF_GSQUERY;
1273 if (!(ma->mca_flags & MAF_GSQUERY) ||
1274 mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
1275 igmp6_group_queried(ma, max_delay);
1276 spin_unlock_bh(&ma->mca_lock);
1280 read_unlock_bh(&idev->lock);
1287 int igmp6_event_report(struct sk_buff *skb)
1289 struct ifmcaddr6 *ma;
1290 struct in6_addr *addrp;
1291 struct inet6_dev *idev;
1292 struct icmp6hdr *hdr;
1295 /* Our own report looped back. Ignore it. */
1296 if (skb->pkt_type == PACKET_LOOPBACK)
1299 /* send our report if the MC router may not have heard this report */
1300 if (skb->pkt_type != PACKET_MULTICAST &&
1301 skb->pkt_type != PACKET_BROADCAST)
1304 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1307 hdr = icmp6_hdr(skb);
1309 /* Drop reports with not link local source */
1310 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
1311 if (addr_type != IPV6_ADDR_ANY &&
1312 !(addr_type&IPV6_ADDR_LINKLOCAL))
1315 addrp = (struct in6_addr *) (hdr + 1);
1317 idev = in6_dev_get(skb->dev);
1322 * Cancel the timer for this group
1325 read_lock_bh(&idev->lock);
1326 for (ma = idev->mc_list; ma; ma=ma->next) {
1327 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1328 spin_lock(&ma->mca_lock);
1329 if (del_timer(&ma->mca_timer))
1330 atomic_dec(&ma->mca_refcnt);
1331 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1332 spin_unlock(&ma->mca_lock);
1336 read_unlock_bh(&idev->lock);
1341 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1342 int gdeleted, int sdeleted)
1345 case MLD2_MODE_IS_INCLUDE:
1346 case MLD2_MODE_IS_EXCLUDE:
1347 if (gdeleted || sdeleted)
1349 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1350 if (pmc->mca_sfmode == MCAST_INCLUDE)
1352 /* don't include if this source is excluded
1355 if (psf->sf_count[MCAST_INCLUDE])
1356 return type == MLD2_MODE_IS_INCLUDE;
1357 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1358 psf->sf_count[MCAST_EXCLUDE];
1361 case MLD2_CHANGE_TO_INCLUDE:
1362 if (gdeleted || sdeleted)
1364 return psf->sf_count[MCAST_INCLUDE] != 0;
1365 case MLD2_CHANGE_TO_EXCLUDE:
1366 if (gdeleted || sdeleted)
1368 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1369 psf->sf_count[MCAST_INCLUDE])
1371 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1372 psf->sf_count[MCAST_EXCLUDE];
1373 case MLD2_ALLOW_NEW_SOURCES:
1374 if (gdeleted || !psf->sf_crcount)
1376 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1377 case MLD2_BLOCK_OLD_SOURCES:
1378 if (pmc->mca_sfmode == MCAST_INCLUDE)
1379 return gdeleted || (psf->sf_crcount && sdeleted);
1380 return psf->sf_crcount && !gdeleted && !sdeleted;
1386 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1388 struct ip6_sf_list *psf;
1391 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1392 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1399 static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1401 struct net *net = dev_net(dev);
1402 struct sock *sk = net->ipv6.igmp_sk;
1403 struct sk_buff *skb;
1404 struct mld2_report *pmr;
1405 struct in6_addr addr_buf;
1406 const struct in6_addr *saddr;
1408 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1409 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1412 /* we assume size > sizeof(ra) here */
1413 skb = sock_alloc_send_skb(sk, size + LL_ALLOCATED_SPACE(dev), 1, &err);
1418 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1420 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1421 /* <draft-ietf-magma-mld-source-05.txt>:
1422 * use unspecified address as the source address
1423 * when a valid link-local address is not available.
1425 saddr = &in6addr_any;
1429 ip6_nd_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
1431 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1433 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
1434 skb_put(skb, sizeof(*pmr));
1435 pmr = (struct mld2_report *)skb_transport_header(skb);
1436 pmr->type = ICMPV6_MLD2_REPORT;
1444 static void mld_sendpack(struct sk_buff *skb)
1446 struct ipv6hdr *pip6 = ipv6_hdr(skb);
1447 struct mld2_report *pmr =
1448 (struct mld2_report *)skb_transport_header(skb);
1449 int payload_len, mldlen;
1450 struct inet6_dev *idev = in6_dev_get(skb->dev);
1451 struct net *net = dev_net(skb->dev);
1455 IP6_INC_STATS(idev, IPSTATS_MIB_OUTREQUESTS);
1456 payload_len = (skb->tail - skb->network_header) - sizeof(*pip6);
1457 mldlen = skb->tail - skb->transport_header;
1458 pip6->payload_len = htons(payload_len);
1460 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1461 IPPROTO_ICMPV6, csum_partial(skb_transport_header(skb),
1464 skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1471 icmpv6_flow_init(net->ipv6.igmp_sk, &fl, ICMPV6_MLD2_REPORT,
1472 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1475 err = xfrm_lookup(&skb->dst, &fl, NULL, 0);
1479 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1483 ICMP6MSGOUT_INC_STATS_BH(idev, ICMPV6_MLD2_REPORT);
1484 ICMP6_INC_STATS_BH(idev, ICMP6_MIB_OUTMSGS);
1485 IP6_INC_STATS_BH(idev, IPSTATS_MIB_OUTMCASTPKTS);
1487 IP6_INC_STATS_BH(idev, IPSTATS_MIB_OUTDISCARDS);
1489 if (likely(idev != NULL))
1498 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1500 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1503 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1504 int type, struct mld2_grec **ppgr)
1506 struct net_device *dev = pmc->idev->dev;
1507 struct mld2_report *pmr;
1508 struct mld2_grec *pgr;
1511 skb = mld_newpack(dev, dev->mtu);
1514 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1515 pgr->grec_type = type;
1516 pgr->grec_auxwords = 0;
1517 pgr->grec_nsrcs = 0;
1518 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1519 pmr = (struct mld2_report *)skb_transport_header(skb);
1520 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1525 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1526 skb_tailroom(skb)) : 0)
1528 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1529 int type, int gdeleted, int sdeleted)
1531 struct net_device *dev = pmc->idev->dev;
1532 struct mld2_report *pmr;
1533 struct mld2_grec *pgr = NULL;
1534 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1535 int scount, stotal, first, isquery, truncate;
1537 if (pmc->mca_flags & MAF_NOREPORT)
1540 isquery = type == MLD2_MODE_IS_INCLUDE ||
1541 type == MLD2_MODE_IS_EXCLUDE;
1542 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1543 type == MLD2_CHANGE_TO_EXCLUDE;
1545 stotal = scount = 0;
1547 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1552 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
1554 /* EX and TO_EX get a fresh packet, if needed */
1556 if (pmr && pmr->ngrec &&
1557 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1560 skb = mld_newpack(dev, dev->mtu);
1565 for (psf=*psf_list; psf; psf=psf_next) {
1566 struct in6_addr *psrc;
1568 psf_next = psf->sf_next;
1570 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1575 /* clear marks on query responses */
1579 if (AVAILABLE(skb) < sizeof(*psrc) +
1580 first*sizeof(struct mld2_grec)) {
1581 if (truncate && !first)
1582 break; /* truncate these */
1584 pgr->grec_nsrcs = htons(scount);
1587 skb = mld_newpack(dev, dev->mtu);
1592 skb = add_grhead(skb, pmc, type, &pgr);
1597 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1598 *psrc = psf->sf_addr;
1600 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1601 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1603 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1605 psf_prev->sf_next = psf->sf_next;
1607 *psf_list = psf->sf_next;
1617 if (type == MLD2_ALLOW_NEW_SOURCES ||
1618 type == MLD2_BLOCK_OLD_SOURCES)
1620 if (pmc->mca_crcount || isquery) {
1621 /* make sure we have room for group header */
1622 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1624 skb = NULL; /* add_grhead will get a new one */
1626 skb = add_grhead(skb, pmc, type, &pgr);
1630 pgr->grec_nsrcs = htons(scount);
1633 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1637 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1639 struct sk_buff *skb = NULL;
1643 read_lock_bh(&idev->lock);
1644 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1645 if (pmc->mca_flags & MAF_NOREPORT)
1647 spin_lock_bh(&pmc->mca_lock);
1648 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1649 type = MLD2_MODE_IS_EXCLUDE;
1651 type = MLD2_MODE_IS_INCLUDE;
1652 skb = add_grec(skb, pmc, type, 0, 0);
1653 spin_unlock_bh(&pmc->mca_lock);
1655 read_unlock_bh(&idev->lock);
1657 spin_lock_bh(&pmc->mca_lock);
1658 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1659 type = MLD2_MODE_IS_EXCLUDE;
1661 type = MLD2_MODE_IS_INCLUDE;
1662 skb = add_grec(skb, pmc, type, 0, 0);
1663 spin_unlock_bh(&pmc->mca_lock);
1670 * remove zero-count source records from a source filter list
1672 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1674 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1677 for (psf=*ppsf; psf; psf = psf_next) {
1678 psf_next = psf->sf_next;
1679 if (psf->sf_crcount == 0) {
1681 psf_prev->sf_next = psf->sf_next;
1683 *ppsf = psf->sf_next;
1690 static void mld_send_cr(struct inet6_dev *idev)
1692 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1693 struct sk_buff *skb = NULL;
1696 read_lock_bh(&idev->lock);
1697 write_lock_bh(&idev->mc_lock);
1701 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1702 pmc_next = pmc->next;
1703 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1704 type = MLD2_BLOCK_OLD_SOURCES;
1705 dtype = MLD2_BLOCK_OLD_SOURCES;
1706 skb = add_grec(skb, pmc, type, 1, 0);
1707 skb = add_grec(skb, pmc, dtype, 1, 1);
1709 if (pmc->mca_crcount) {
1710 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1711 type = MLD2_CHANGE_TO_INCLUDE;
1712 skb = add_grec(skb, pmc, type, 1, 0);
1715 if (pmc->mca_crcount == 0) {
1716 mld_clear_zeros(&pmc->mca_tomb);
1717 mld_clear_zeros(&pmc->mca_sources);
1720 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1721 !pmc->mca_sources) {
1723 pmc_prev->next = pmc_next;
1725 idev->mc_tomb = pmc_next;
1726 in6_dev_put(pmc->idev);
1731 write_unlock_bh(&idev->mc_lock);
1734 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1735 spin_lock_bh(&pmc->mca_lock);
1736 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1737 type = MLD2_BLOCK_OLD_SOURCES;
1738 dtype = MLD2_ALLOW_NEW_SOURCES;
1740 type = MLD2_ALLOW_NEW_SOURCES;
1741 dtype = MLD2_BLOCK_OLD_SOURCES;
1743 skb = add_grec(skb, pmc, type, 0, 0);
1744 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1746 /* filter mode changes */
1747 if (pmc->mca_crcount) {
1748 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1749 type = MLD2_CHANGE_TO_EXCLUDE;
1751 type = MLD2_CHANGE_TO_INCLUDE;
1752 skb = add_grec(skb, pmc, type, 0, 0);
1755 spin_unlock_bh(&pmc->mca_lock);
1757 read_unlock_bh(&idev->lock);
1760 (void) mld_sendpack(skb);
1763 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1765 struct net *net = dev_net(dev);
1766 struct sock *sk = net->ipv6.igmp_sk;
1767 struct inet6_dev *idev;
1768 struct sk_buff *skb;
1769 struct icmp6hdr *hdr;
1770 const struct in6_addr *snd_addr, *saddr;
1771 struct in6_addr *addrp;
1772 struct in6_addr addr_buf;
1773 int err, len, payload_len, full_len;
1774 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1775 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1780 IP6_INC_STATS(__in6_dev_get(dev),
1781 IPSTATS_MIB_OUTREQUESTS);
1783 if (type == ICMPV6_MGM_REDUCTION)
1784 snd_addr = &in6addr_linklocal_allrouters;
1788 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1789 payload_len = len + sizeof(ra);
1790 full_len = sizeof(struct ipv6hdr) + payload_len;
1792 skb = sock_alloc_send_skb(sk, LL_ALLOCATED_SPACE(dev) + full_len, 1, &err);
1796 IP6_INC_STATS(__in6_dev_get(dev),
1797 IPSTATS_MIB_OUTDISCARDS);
1802 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1804 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1805 /* <draft-ietf-magma-mld-source-05.txt>:
1806 * use unspecified address as the source address
1807 * when a valid link-local address is not available.
1809 saddr = &in6addr_any;
1813 ip6_nd_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
1815 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1817 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1818 memset(hdr, 0, sizeof(struct icmp6hdr));
1819 hdr->icmp6_type = type;
1821 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1822 ipv6_addr_copy(addrp, addr);
1824 hdr->icmp6_cksum = csum_ipv6_magic(saddr, snd_addr, len,
1826 csum_partial((__u8 *) hdr, len, 0));
1828 idev = in6_dev_get(skb->dev);
1830 skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1836 icmpv6_flow_init(sk, &fl, type,
1837 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1840 err = xfrm_lookup(&skb->dst, &fl, NULL, 0);
1844 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1848 ICMP6MSGOUT_INC_STATS(idev, type);
1849 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1850 IP6_INC_STATS(idev, IPSTATS_MIB_OUTMCASTPKTS);
1852 IP6_INC_STATS(idev, IPSTATS_MIB_OUTDISCARDS);
1854 if (likely(idev != NULL))
1863 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1864 struct in6_addr *psfsrc)
1866 struct ip6_sf_list *psf, *psf_prev;
1870 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1871 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1875 if (!psf || psf->sf_count[sfmode] == 0) {
1876 /* source filter not found, or count wrong => bug */
1879 psf->sf_count[sfmode]--;
1880 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1881 struct inet6_dev *idev = pmc->idev;
1883 /* no more filters for this source */
1885 psf_prev->sf_next = psf->sf_next;
1887 pmc->mca_sources = psf->sf_next;
1888 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1889 !MLD_V1_SEEN(idev)) {
1890 psf->sf_crcount = idev->mc_qrv;
1891 psf->sf_next = pmc->mca_tomb;
1892 pmc->mca_tomb = psf;
1900 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1901 int sfmode, int sfcount, struct in6_addr *psfsrc,
1904 struct ifmcaddr6 *pmc;
1910 read_lock_bh(&idev->lock);
1911 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1912 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1916 /* MCA not found?? bug */
1917 read_unlock_bh(&idev->lock);
1920 spin_lock_bh(&pmc->mca_lock);
1923 if (!pmc->mca_sfcount[sfmode]) {
1924 spin_unlock_bh(&pmc->mca_lock);
1925 read_unlock_bh(&idev->lock);
1928 pmc->mca_sfcount[sfmode]--;
1931 for (i=0; i<sfcount; i++) {
1932 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1934 changerec |= rv > 0;
1938 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1939 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1940 pmc->mca_sfcount[MCAST_INCLUDE]) {
1941 struct ip6_sf_list *psf;
1943 /* filter mode change */
1944 pmc->mca_sfmode = MCAST_INCLUDE;
1945 pmc->mca_crcount = idev->mc_qrv;
1946 idev->mc_ifc_count = pmc->mca_crcount;
1947 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1948 psf->sf_crcount = 0;
1949 mld_ifc_event(pmc->idev);
1950 } else if (sf_setstate(pmc) || changerec)
1951 mld_ifc_event(pmc->idev);
1952 spin_unlock_bh(&pmc->mca_lock);
1953 read_unlock_bh(&idev->lock);
1958 * Add multicast single-source filter to the interface list
1960 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1961 struct in6_addr *psfsrc, int delta)
1963 struct ip6_sf_list *psf, *psf_prev;
1966 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1967 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1972 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1976 psf->sf_addr = *psfsrc;
1978 psf_prev->sf_next = psf;
1980 pmc->mca_sources = psf;
1982 psf->sf_count[sfmode]++;
1986 static void sf_markstate(struct ifmcaddr6 *pmc)
1988 struct ip6_sf_list *psf;
1989 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1991 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1992 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1993 psf->sf_oldin = mca_xcount ==
1994 psf->sf_count[MCAST_EXCLUDE] &&
1995 !psf->sf_count[MCAST_INCLUDE];
1997 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2000 static int sf_setstate(struct ifmcaddr6 *pmc)
2002 struct ip6_sf_list *psf, *dpsf;
2003 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2004 int qrv = pmc->idev->mc_qrv;
2008 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
2009 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2010 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2011 !psf->sf_count[MCAST_INCLUDE];
2013 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2015 if (!psf->sf_oldin) {
2016 struct ip6_sf_list *prev = NULL;
2018 for (dpsf=pmc->mca_tomb; dpsf;
2019 dpsf=dpsf->sf_next) {
2020 if (ipv6_addr_equal(&dpsf->sf_addr,
2027 prev->sf_next = dpsf->sf_next;
2029 pmc->mca_tomb = dpsf->sf_next;
2032 psf->sf_crcount = qrv;
2035 } else if (psf->sf_oldin) {
2036 psf->sf_crcount = 0;
2038 * add or update "delete" records if an active filter
2041 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2042 if (ipv6_addr_equal(&dpsf->sf_addr,
2046 dpsf = (struct ip6_sf_list *)
2047 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2051 /* pmc->mca_lock held by callers */
2052 dpsf->sf_next = pmc->mca_tomb;
2053 pmc->mca_tomb = dpsf;
2055 dpsf->sf_crcount = qrv;
2063 * Add multicast source filter list to the interface list
2065 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
2066 int sfmode, int sfcount, struct in6_addr *psfsrc,
2069 struct ifmcaddr6 *pmc;
2075 read_lock_bh(&idev->lock);
2076 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2077 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2081 /* MCA not found?? bug */
2082 read_unlock_bh(&idev->lock);
2085 spin_lock_bh(&pmc->mca_lock);
2088 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2090 pmc->mca_sfcount[sfmode]++;
2092 for (i=0; i<sfcount; i++) {
2093 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
2101 pmc->mca_sfcount[sfmode]--;
2103 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2104 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2105 struct inet6_dev *idev = pmc->idev;
2106 struct ip6_sf_list *psf;
2108 /* filter mode change */
2109 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2110 pmc->mca_sfmode = MCAST_EXCLUDE;
2111 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2112 pmc->mca_sfmode = MCAST_INCLUDE;
2113 /* else no filters; keep old mode for reports */
2115 pmc->mca_crcount = idev->mc_qrv;
2116 idev->mc_ifc_count = pmc->mca_crcount;
2117 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2118 psf->sf_crcount = 0;
2119 mld_ifc_event(idev);
2120 } else if (sf_setstate(pmc))
2121 mld_ifc_event(idev);
2122 spin_unlock_bh(&pmc->mca_lock);
2123 read_unlock_bh(&idev->lock);
2127 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2129 struct ip6_sf_list *psf, *nextpsf;
2131 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2132 nextpsf = psf->sf_next;
2135 pmc->mca_tomb = NULL;
2136 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2137 nextpsf = psf->sf_next;
2140 pmc->mca_sources = NULL;
2141 pmc->mca_sfmode = MCAST_EXCLUDE;
2142 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2143 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2147 static void igmp6_join_group(struct ifmcaddr6 *ma)
2149 unsigned long delay;
2151 if (ma->mca_flags & MAF_NOREPORT)
2154 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2156 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2158 spin_lock_bh(&ma->mca_lock);
2159 if (del_timer(&ma->mca_timer)) {
2160 atomic_dec(&ma->mca_refcnt);
2161 delay = ma->mca_timer.expires - jiffies;
2164 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2165 atomic_inc(&ma->mca_refcnt);
2166 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2167 spin_unlock_bh(&ma->mca_lock);
2170 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2171 struct inet6_dev *idev)
2175 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2176 * so no other readers or writers of iml or its sflist
2179 /* any-source empty exclude case */
2180 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2182 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2183 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2184 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2189 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2191 if (MLD_V1_SEEN(ma->idev)) {
2192 if (ma->mca_flags & MAF_LAST_REPORTER)
2193 igmp6_send(&ma->mca_addr, ma->idev->dev,
2194 ICMPV6_MGM_REDUCTION);
2196 mld_add_delrec(ma->idev, ma);
2197 mld_ifc_event(ma->idev);
2201 static void mld_gq_timer_expire(unsigned long data)
2203 struct inet6_dev *idev = (struct inet6_dev *)data;
2205 idev->mc_gq_running = 0;
2206 mld_send_report(idev, NULL);
2207 __in6_dev_put(idev);
2210 static void mld_ifc_timer_expire(unsigned long data)
2212 struct inet6_dev *idev = (struct inet6_dev *)data;
2215 if (idev->mc_ifc_count) {
2216 idev->mc_ifc_count--;
2217 if (idev->mc_ifc_count)
2218 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2220 __in6_dev_put(idev);
2223 static void mld_ifc_event(struct inet6_dev *idev)
2225 if (MLD_V1_SEEN(idev))
2227 idev->mc_ifc_count = idev->mc_qrv;
2228 mld_ifc_start_timer(idev, 1);
2232 static void igmp6_timer_handler(unsigned long data)
2234 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2236 if (MLD_V1_SEEN(ma->idev))
2237 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2239 mld_send_report(ma->idev, ma);
2241 spin_lock(&ma->mca_lock);
2242 ma->mca_flags |= MAF_LAST_REPORTER;
2243 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2244 spin_unlock(&ma->mca_lock);
2248 /* Device going down */
2250 void ipv6_mc_down(struct inet6_dev *idev)
2252 struct ifmcaddr6 *i;
2254 /* Withdraw multicast list */
2256 read_lock_bh(&idev->lock);
2257 idev->mc_ifc_count = 0;
2258 if (del_timer(&idev->mc_ifc_timer))
2259 __in6_dev_put(idev);
2260 idev->mc_gq_running = 0;
2261 if (del_timer(&idev->mc_gq_timer))
2262 __in6_dev_put(idev);
2264 for (i = idev->mc_list; i; i=i->next)
2265 igmp6_group_dropped(i);
2266 read_unlock_bh(&idev->lock);
2268 mld_clear_delrec(idev);
2272 /* Device going up */
2274 void ipv6_mc_up(struct inet6_dev *idev)
2276 struct ifmcaddr6 *i;
2278 /* Install multicast list, except for all-nodes (already installed) */
2280 read_lock_bh(&idev->lock);
2281 for (i = idev->mc_list; i; i=i->next)
2282 igmp6_group_added(i);
2283 read_unlock_bh(&idev->lock);
2286 /* IPv6 device initialization. */
2288 void ipv6_mc_init_dev(struct inet6_dev *idev)
2290 write_lock_bh(&idev->lock);
2291 rwlock_init(&idev->mc_lock);
2292 idev->mc_gq_running = 0;
2293 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2294 (unsigned long)idev);
2295 idev->mc_tomb = NULL;
2296 idev->mc_ifc_count = 0;
2297 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2298 (unsigned long)idev);
2299 idev->mc_qrv = MLD_QRV_DEFAULT;
2300 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2301 idev->mc_v1_seen = 0;
2302 write_unlock_bh(&idev->lock);
2306 * Device is about to be destroyed: clean up.
2309 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2311 struct ifmcaddr6 *i;
2313 /* Deactivate timers */
2316 /* Delete all-nodes address. */
2317 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2318 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2321 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
2323 if (idev->cnf.forwarding)
2324 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
2326 write_lock_bh(&idev->lock);
2327 while ((i = idev->mc_list) != NULL) {
2328 idev->mc_list = i->next;
2329 write_unlock_bh(&idev->lock);
2331 igmp6_group_dropped(i);
2334 write_lock_bh(&idev->lock);
2336 write_unlock_bh(&idev->lock);
2339 #ifdef CONFIG_PROC_FS
2340 struct igmp6_mc_iter_state {
2341 struct seq_net_private p;
2342 struct net_device *dev;
2343 struct inet6_dev *idev;
2346 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2348 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2350 struct ifmcaddr6 *im = NULL;
2351 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2352 struct net *net = seq_file_net(seq);
2355 for_each_netdev(net, state->dev) {
2356 struct inet6_dev *idev;
2357 idev = in6_dev_get(state->dev);
2360 read_lock_bh(&idev->lock);
2366 read_unlock_bh(&idev->lock);
2372 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2374 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2378 if (likely(state->idev != NULL)) {
2379 read_unlock_bh(&state->idev->lock);
2380 in6_dev_put(state->idev);
2382 state->dev = next_net_device(state->dev);
2387 state->idev = in6_dev_get(state->dev);
2390 read_lock_bh(&state->idev->lock);
2391 im = state->idev->mc_list;
2396 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2398 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2400 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2402 return pos ? NULL : im;
2405 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2406 __acquires(dev_base_lock)
2408 read_lock(&dev_base_lock);
2409 return igmp6_mc_get_idx(seq, *pos);
2412 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2414 struct ifmcaddr6 *im;
2415 im = igmp6_mc_get_next(seq, v);
2420 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2421 __releases(dev_base_lock)
2423 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2424 if (likely(state->idev != NULL)) {
2425 read_unlock_bh(&state->idev->lock);
2426 in6_dev_put(state->idev);
2430 read_unlock(&dev_base_lock);
2433 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2435 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2436 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2439 "%-4d %-15s " NIP6_SEQFMT " %5d %08X %ld\n",
2440 state->dev->ifindex, state->dev->name,
2442 im->mca_users, im->mca_flags,
2443 (im->mca_flags&MAF_TIMER_RUNNING) ?
2444 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2448 static const struct seq_operations igmp6_mc_seq_ops = {
2449 .start = igmp6_mc_seq_start,
2450 .next = igmp6_mc_seq_next,
2451 .stop = igmp6_mc_seq_stop,
2452 .show = igmp6_mc_seq_show,
2455 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2457 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2458 sizeof(struct igmp6_mc_iter_state));
2461 static const struct file_operations igmp6_mc_seq_fops = {
2462 .owner = THIS_MODULE,
2463 .open = igmp6_mc_seq_open,
2465 .llseek = seq_lseek,
2466 .release = seq_release_net,
2469 struct igmp6_mcf_iter_state {
2470 struct seq_net_private p;
2471 struct net_device *dev;
2472 struct inet6_dev *idev;
2473 struct ifmcaddr6 *im;
2476 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2478 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2480 struct ip6_sf_list *psf = NULL;
2481 struct ifmcaddr6 *im = NULL;
2482 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2483 struct net *net = seq_file_net(seq);
2487 for_each_netdev(net, state->dev) {
2488 struct inet6_dev *idev;
2489 idev = in6_dev_get(state->dev);
2490 if (unlikely(idev == NULL))
2492 read_lock_bh(&idev->lock);
2494 if (likely(im != NULL)) {
2495 spin_lock_bh(&im->mca_lock);
2496 psf = im->mca_sources;
2497 if (likely(psf != NULL)) {
2502 spin_unlock_bh(&im->mca_lock);
2504 read_unlock_bh(&idev->lock);
2510 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2512 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2516 spin_unlock_bh(&state->im->mca_lock);
2517 state->im = state->im->next;
2518 while (!state->im) {
2519 if (likely(state->idev != NULL)) {
2520 read_unlock_bh(&state->idev->lock);
2521 in6_dev_put(state->idev);
2523 state->dev = next_net_device(state->dev);
2528 state->idev = in6_dev_get(state->dev);
2531 read_lock_bh(&state->idev->lock);
2532 state->im = state->idev->mc_list;
2536 spin_lock_bh(&state->im->mca_lock);
2537 psf = state->im->mca_sources;
2543 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2545 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2547 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2549 return pos ? NULL : psf;
2552 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2553 __acquires(dev_base_lock)
2555 read_lock(&dev_base_lock);
2556 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2559 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2561 struct ip6_sf_list *psf;
2562 if (v == SEQ_START_TOKEN)
2563 psf = igmp6_mcf_get_first(seq);
2565 psf = igmp6_mcf_get_next(seq, v);
2570 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2571 __releases(dev_base_lock)
2573 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2574 if (likely(state->im != NULL)) {
2575 spin_unlock_bh(&state->im->mca_lock);
2578 if (likely(state->idev != NULL)) {
2579 read_unlock_bh(&state->idev->lock);
2580 in6_dev_put(state->idev);
2584 read_unlock(&dev_base_lock);
2587 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2589 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2590 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2592 if (v == SEQ_START_TOKEN) {
2595 "%32s %32s %6s %6s\n", "Idx",
2596 "Device", "Multicast Address",
2597 "Source Address", "INC", "EXC");
2600 "%3d %6.6s " NIP6_SEQFMT " " NIP6_SEQFMT " %6lu %6lu\n",
2601 state->dev->ifindex, state->dev->name,
2602 NIP6(state->im->mca_addr),
2604 psf->sf_count[MCAST_INCLUDE],
2605 psf->sf_count[MCAST_EXCLUDE]);
2610 static const struct seq_operations igmp6_mcf_seq_ops = {
2611 .start = igmp6_mcf_seq_start,
2612 .next = igmp6_mcf_seq_next,
2613 .stop = igmp6_mcf_seq_stop,
2614 .show = igmp6_mcf_seq_show,
2617 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2619 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2620 sizeof(struct igmp6_mcf_iter_state));
2623 static const struct file_operations igmp6_mcf_seq_fops = {
2624 .owner = THIS_MODULE,
2625 .open = igmp6_mcf_seq_open,
2627 .llseek = seq_lseek,
2628 .release = seq_release_net,
2631 static int igmp6_proc_init(struct net *net)
2636 if (!proc_net_fops_create(net, "igmp6", S_IRUGO, &igmp6_mc_seq_fops))
2638 if (!proc_net_fops_create(net, "mcfilter6", S_IRUGO,
2639 &igmp6_mcf_seq_fops))
2640 goto out_proc_net_igmp6;
2647 proc_net_remove(net, "igmp6");
2651 static void igmp6_proc_exit(struct net *net)
2653 proc_net_remove(net, "mcfilter6");
2654 proc_net_remove(net, "igmp6");
2657 static int igmp6_proc_init(struct net *net)
2661 static void igmp6_proc_exit(struct net *net)
2667 static int igmp6_net_init(struct net *net)
2671 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2672 SOCK_RAW, IPPROTO_ICMPV6, net);
2675 "Failed to initialize the IGMP6 control socket (err %d).\n",
2680 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
2682 err = igmp6_proc_init(net);
2684 goto out_sock_create;
2689 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2693 static void igmp6_net_exit(struct net *net)
2695 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2696 igmp6_proc_exit(net);
2699 static struct pernet_operations igmp6_net_ops = {
2700 .init = igmp6_net_init,
2701 .exit = igmp6_net_exit,
2704 int __init igmp6_init(void)
2706 return register_pernet_subsys(&igmp6_net_ops);
2709 void igmp6_cleanup(void)
2711 unregister_pernet_subsys(&igmp6_net_ops);