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/config.h>
32 #include <linux/module.h>
33 #include <linux/errno.h>
34 #include <linux/types.h>
35 #include <linux/string.h>
36 #include <linux/socket.h>
37 #include <linux/sockios.h>
38 #include <linux/jiffies.h>
39 #include <linux/times.h>
40 #include <linux/net.h>
42 #include <linux/in6.h>
43 #include <linux/netdevice.h>
44 #include <linux/if_arp.h>
45 #include <linux/route.h>
46 #include <linux/init.h>
47 #include <linux/proc_fs.h>
48 #include <linux/seq_file.h>
50 #include <linux/netfilter.h>
51 #include <linux/netfilter_ipv6.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>
63 #include <net/ip6_checksum.h>
65 /* Set to 3 to get tracing... */
69 #define MDBG(x) printk x
75 * These header formats should be in a separate include file, but icmpv6.h
76 * doesn't have in6_addr defined in all cases, there is no __u128, and no
77 * other files reference these.
82 /* Multicast Listener Discovery version 2 headers */
88 struct in6_addr grec_mca;
89 struct in6_addr grec_src[0];
98 struct mld2_grec grec[0];
108 #if defined(__LITTLE_ENDIAN_BITFIELD)
112 #elif defined(__BIG_ENDIAN_BITFIELD)
117 #error "Please fix <asm/byteorder.h>"
121 struct in6_addr srcs[0];
124 static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
126 /* Big mc list lock for all the sockets */
127 static DEFINE_RWLOCK(ipv6_sk_mc_lock);
129 static struct socket *igmp6_socket;
131 int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr);
133 static void igmp6_join_group(struct ifmcaddr6 *ma);
134 static void igmp6_leave_group(struct ifmcaddr6 *ma);
135 static void igmp6_timer_handler(unsigned long data);
137 static void mld_gq_timer_expire(unsigned long data);
138 static void mld_ifc_timer_expire(unsigned long data);
139 static void mld_ifc_event(struct inet6_dev *idev);
140 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
141 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
142 static void mld_clear_delrec(struct inet6_dev *idev);
143 static int sf_setstate(struct ifmcaddr6 *pmc);
144 static void sf_markstate(struct ifmcaddr6 *pmc);
145 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
146 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
147 int sfmode, int sfcount, struct in6_addr *psfsrc,
149 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
150 int sfmode, int sfcount, struct in6_addr *psfsrc,
152 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
153 struct inet6_dev *idev);
156 #define IGMP6_UNSOLICITED_IVAL (10*HZ)
157 #define MLD_QRV_DEFAULT 2
159 #define MLD_V1_SEEN(idev) (ipv6_devconf.force_mld_version == 1 || \
160 (idev)->cnf.force_mld_version == 1 || \
161 ((idev)->mc_v1_seen && \
162 time_before(jiffies, (idev)->mc_v1_seen)))
164 #define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
165 #define MLDV2_EXP(thresh, nbmant, nbexp, value) \
166 ((value) < (thresh) ? (value) : \
167 ((MLDV2_MASK(value, nbmant) | (1<<(nbmant))) << \
168 (MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp))))
170 #define MLDV2_QQIC(value) MLDV2_EXP(0x80, 4, 3, value)
171 #define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
173 #define IPV6_MLD_MAX_MSF 64
175 int sysctl_mld_max_msf = IPV6_MLD_MAX_MSF;
178 * socket join on multicast group
181 int ipv6_sock_mc_join(struct sock *sk, int ifindex, struct in6_addr *addr)
183 struct net_device *dev = NULL;
184 struct ipv6_mc_socklist *mc_lst;
185 struct ipv6_pinfo *np = inet6_sk(sk);
188 if (!ipv6_addr_is_multicast(addr))
191 read_lock_bh(&ipv6_sk_mc_lock);
192 for (mc_lst=np->ipv6_mc_list; mc_lst; mc_lst=mc_lst->next) {
193 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
194 ipv6_addr_equal(&mc_lst->addr, addr)) {
195 read_unlock_bh(&ipv6_sk_mc_lock);
199 read_unlock_bh(&ipv6_sk_mc_lock);
201 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
207 ipv6_addr_copy(&mc_lst->addr, addr);
211 rt = rt6_lookup(addr, NULL, 0, 0);
215 dst_release(&rt->u.dst);
218 dev = dev_get_by_index(ifindex);
221 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
225 mc_lst->ifindex = dev->ifindex;
226 mc_lst->sfmode = MCAST_EXCLUDE;
227 mc_lst->sflist = NULL;
230 * now add/increase the group membership on the device
233 err = ipv6_dev_mc_inc(dev, addr);
236 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
241 write_lock_bh(&ipv6_sk_mc_lock);
242 mc_lst->next = np->ipv6_mc_list;
243 np->ipv6_mc_list = mc_lst;
244 write_unlock_bh(&ipv6_sk_mc_lock);
252 * socket leave on multicast group
254 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, struct in6_addr *addr)
256 struct ipv6_pinfo *np = inet6_sk(sk);
257 struct ipv6_mc_socklist *mc_lst, **lnk;
259 write_lock_bh(&ipv6_sk_mc_lock);
260 for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
261 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
262 ipv6_addr_equal(&mc_lst->addr, addr)) {
263 struct net_device *dev;
266 write_unlock_bh(&ipv6_sk_mc_lock);
268 if ((dev = dev_get_by_index(mc_lst->ifindex)) != NULL) {
269 struct inet6_dev *idev = in6_dev_get(dev);
272 (void) ip6_mc_leave_src(sk,mc_lst,idev);
273 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
278 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
282 write_unlock_bh(&ipv6_sk_mc_lock);
284 return -EADDRNOTAVAIL;
287 static struct inet6_dev *ip6_mc_find_dev(struct in6_addr *group, int ifindex)
289 struct net_device *dev = NULL;
290 struct inet6_dev *idev = NULL;
295 rt = rt6_lookup(group, NULL, 0, 0);
299 dst_release(&rt->u.dst);
302 dev = dev_get_by_index(ifindex);
306 idev = in6_dev_get(dev);
311 read_lock_bh(&idev->lock);
313 read_unlock_bh(&idev->lock);
321 void ipv6_sock_mc_close(struct sock *sk)
323 struct ipv6_pinfo *np = inet6_sk(sk);
324 struct ipv6_mc_socklist *mc_lst;
326 write_lock_bh(&ipv6_sk_mc_lock);
327 while ((mc_lst = np->ipv6_mc_list) != NULL) {
328 struct net_device *dev;
330 np->ipv6_mc_list = mc_lst->next;
331 write_unlock_bh(&ipv6_sk_mc_lock);
333 dev = dev_get_by_index(mc_lst->ifindex);
335 struct inet6_dev *idev = in6_dev_get(dev);
338 (void) ip6_mc_leave_src(sk, mc_lst, idev);
339 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
345 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
347 write_lock_bh(&ipv6_sk_mc_lock);
349 write_unlock_bh(&ipv6_sk_mc_lock);
352 int ip6_mc_source(int add, int omode, struct sock *sk,
353 struct group_source_req *pgsr)
355 struct in6_addr *source, *group;
356 struct ipv6_mc_socklist *pmc;
357 struct net_device *dev;
358 struct inet6_dev *idev;
359 struct ipv6_pinfo *inet6 = inet6_sk(sk);
360 struct ip6_sf_socklist *psl;
365 if (pgsr->gsr_group.ss_family != AF_INET6 ||
366 pgsr->gsr_source.ss_family != AF_INET6)
369 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
370 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
372 if (!ipv6_addr_is_multicast(group))
375 idev = ip6_mc_find_dev(group, pgsr->gsr_interface);
380 err = -EADDRNOTAVAIL;
382 read_lock_bh(&ipv6_sk_mc_lock);
383 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
384 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
386 if (ipv6_addr_equal(&pmc->addr, group))
389 if (!pmc) { /* must have a prior join */
393 /* if a source filter was set, must be the same mode as before */
395 if (pmc->sfmode != omode) {
399 } else if (pmc->sfmode != omode) {
400 /* allow mode switches for empty-set filters */
401 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
402 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
409 goto done; /* err = -EADDRNOTAVAIL */
411 for (i=0; i<psl->sl_count; i++) {
412 rv = memcmp(&psl->sl_addr[i], source,
413 sizeof(struct in6_addr));
417 if (rv) /* source not found */
418 goto done; /* err = -EADDRNOTAVAIL */
420 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
421 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
426 /* update the interface filter */
427 ip6_mc_del_src(idev, group, omode, 1, source, 1);
429 for (j=i+1; j<psl->sl_count; j++)
430 psl->sl_addr[j-1] = psl->sl_addr[j];
435 /* else, add a new source to the filter */
437 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
441 if (!psl || psl->sl_count == psl->sl_max) {
442 struct ip6_sf_socklist *newpsl;
443 int count = IP6_SFBLOCK;
446 count += psl->sl_max;
447 newpsl = (struct ip6_sf_socklist *)sock_kmalloc(sk,
448 IP6_SFLSIZE(count), GFP_ATOMIC);
453 newpsl->sl_max = count;
454 newpsl->sl_count = count - IP6_SFBLOCK;
456 for (i=0; i<psl->sl_count; i++)
457 newpsl->sl_addr[i] = psl->sl_addr[i];
458 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
460 pmc->sflist = psl = newpsl;
462 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
463 for (i=0; i<psl->sl_count; i++) {
464 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
468 if (rv == 0) /* address already there is an error */
470 for (j=psl->sl_count-1; j>=i; j--)
471 psl->sl_addr[j+1] = psl->sl_addr[j];
472 psl->sl_addr[i] = *source;
475 /* update the interface list */
476 ip6_mc_add_src(idev, group, omode, 1, source, 1);
478 read_unlock_bh(&ipv6_sk_mc_lock);
479 read_unlock_bh(&idev->lock);
483 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
487 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
489 struct in6_addr *group;
490 struct ipv6_mc_socklist *pmc;
491 struct net_device *dev;
492 struct inet6_dev *idev;
493 struct ipv6_pinfo *inet6 = inet6_sk(sk);
494 struct ip6_sf_socklist *newpsl, *psl;
498 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
500 if (!ipv6_addr_is_multicast(group))
502 if (gsf->gf_fmode != MCAST_INCLUDE &&
503 gsf->gf_fmode != MCAST_EXCLUDE)
506 idev = ip6_mc_find_dev(group, gsf->gf_interface);
513 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
518 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
519 if (pmc->ifindex != gsf->gf_interface)
521 if (ipv6_addr_equal(&pmc->addr, group))
524 if (!pmc) { /* must have a prior join */
528 if (gsf->gf_numsrc) {
529 newpsl = (struct ip6_sf_socklist *)sock_kmalloc(sk,
530 IP6_SFLSIZE(gsf->gf_numsrc), GFP_ATOMIC);
535 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
536 for (i=0; i<newpsl->sl_count; ++i) {
537 struct sockaddr_in6 *psin6;
539 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
540 newpsl->sl_addr[i] = psin6->sin6_addr;
542 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
543 newpsl->sl_count, newpsl->sl_addr, 0);
545 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
550 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
554 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
555 psl->sl_count, psl->sl_addr, 0);
556 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
558 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
559 pmc->sflist = newpsl;
560 pmc->sfmode = gsf->gf_fmode;
563 read_unlock_bh(&idev->lock);
567 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
571 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
572 struct group_filter __user *optval, int __user *optlen)
574 int err, i, count, copycount;
575 struct in6_addr *group;
576 struct ipv6_mc_socklist *pmc;
577 struct inet6_dev *idev;
578 struct net_device *dev;
579 struct ipv6_pinfo *inet6 = inet6_sk(sk);
580 struct ip6_sf_socklist *psl;
582 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
584 if (!ipv6_addr_is_multicast(group))
587 idev = ip6_mc_find_dev(group, gsf->gf_interface);
594 err = -EADDRNOTAVAIL;
596 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
597 if (pmc->ifindex != gsf->gf_interface)
599 if (ipv6_addr_equal(group, &pmc->addr))
602 if (!pmc) /* must have a prior join */
604 gsf->gf_fmode = pmc->sfmode;
606 count = psl ? psl->sl_count : 0;
607 read_unlock_bh(&idev->lock);
611 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
612 gsf->gf_numsrc = count;
613 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
614 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
617 for (i=0; i<copycount; i++) {
618 struct sockaddr_in6 *psin6;
619 struct sockaddr_storage ss;
621 psin6 = (struct sockaddr_in6 *)&ss;
622 memset(&ss, 0, sizeof(ss));
623 psin6->sin6_family = AF_INET6;
624 psin6->sin6_addr = psl->sl_addr[i];
625 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
630 read_unlock_bh(&idev->lock);
636 int inet6_mc_check(struct sock *sk, struct in6_addr *mc_addr,
637 struct in6_addr *src_addr)
639 struct ipv6_pinfo *np = inet6_sk(sk);
640 struct ipv6_mc_socklist *mc;
641 struct ip6_sf_socklist *psl;
644 read_lock(&ipv6_sk_mc_lock);
645 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
646 if (ipv6_addr_equal(&mc->addr, mc_addr))
650 read_unlock(&ipv6_sk_mc_lock);
655 rv = mc->sfmode == MCAST_EXCLUDE;
659 for (i=0; i<psl->sl_count; i++) {
660 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
663 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
665 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
668 read_unlock(&ipv6_sk_mc_lock);
673 static void ma_put(struct ifmcaddr6 *mc)
675 if (atomic_dec_and_test(&mc->mca_refcnt)) {
676 in6_dev_put(mc->idev);
681 static void igmp6_group_added(struct ifmcaddr6 *mc)
683 struct net_device *dev = mc->idev->dev;
684 char buf[MAX_ADDR_LEN];
686 spin_lock_bh(&mc->mca_lock);
687 if (!(mc->mca_flags&MAF_LOADED)) {
688 mc->mca_flags |= MAF_LOADED;
689 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
690 dev_mc_add(dev, buf, dev->addr_len, 0);
692 spin_unlock_bh(&mc->mca_lock);
694 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
697 if (MLD_V1_SEEN(mc->idev)) {
698 igmp6_join_group(mc);
703 mc->mca_crcount = mc->idev->mc_qrv;
704 mld_ifc_event(mc->idev);
707 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
709 struct net_device *dev = mc->idev->dev;
710 char buf[MAX_ADDR_LEN];
712 spin_lock_bh(&mc->mca_lock);
713 if (mc->mca_flags&MAF_LOADED) {
714 mc->mca_flags &= ~MAF_LOADED;
715 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
716 dev_mc_delete(dev, buf, dev->addr_len, 0);
719 if (mc->mca_flags & MAF_NOREPORT)
721 spin_unlock_bh(&mc->mca_lock);
724 igmp6_leave_group(mc);
726 spin_lock_bh(&mc->mca_lock);
727 if (del_timer(&mc->mca_timer))
728 atomic_dec(&mc->mca_refcnt);
730 ip6_mc_clear_src(mc);
731 spin_unlock_bh(&mc->mca_lock);
735 * deleted ifmcaddr6 manipulation
737 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
739 struct ifmcaddr6 *pmc;
741 /* this is an "ifmcaddr6" for convenience; only the fields below
742 * are actually used. In particular, the refcnt and users are not
743 * used for management of the delete list. Using the same structure
744 * for deleted items allows change reports to use common code with
745 * non-deleted or query-response MCA's.
747 pmc = (struct ifmcaddr6 *)kmalloc(sizeof(*pmc), GFP_ATOMIC);
750 memset(pmc, 0, sizeof(*pmc));
751 spin_lock_bh(&im->mca_lock);
752 spin_lock_init(&pmc->mca_lock);
753 pmc->idev = im->idev;
755 pmc->mca_addr = im->mca_addr;
756 pmc->mca_crcount = idev->mc_qrv;
757 pmc->mca_sfmode = im->mca_sfmode;
758 if (pmc->mca_sfmode == MCAST_INCLUDE) {
759 struct ip6_sf_list *psf;
761 pmc->mca_tomb = im->mca_tomb;
762 pmc->mca_sources = im->mca_sources;
763 im->mca_tomb = im->mca_sources = NULL;
764 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
765 psf->sf_crcount = pmc->mca_crcount;
767 spin_unlock_bh(&im->mca_lock);
769 write_lock_bh(&idev->mc_lock);
770 pmc->next = idev->mc_tomb;
772 write_unlock_bh(&idev->mc_lock);
775 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
777 struct ifmcaddr6 *pmc, *pmc_prev;
778 struct ip6_sf_list *psf, *psf_next;
780 write_lock_bh(&idev->mc_lock);
782 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
783 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
789 pmc_prev->next = pmc->next;
791 idev->mc_tomb = pmc->next;
793 write_unlock_bh(&idev->mc_lock);
795 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
796 psf_next = psf->sf_next;
799 in6_dev_put(pmc->idev);
804 static void mld_clear_delrec(struct inet6_dev *idev)
806 struct ifmcaddr6 *pmc, *nextpmc;
808 write_lock_bh(&idev->mc_lock);
810 idev->mc_tomb = NULL;
811 write_unlock_bh(&idev->mc_lock);
813 for (; pmc; pmc = nextpmc) {
815 ip6_mc_clear_src(pmc);
816 in6_dev_put(pmc->idev);
820 /* clear dead sources, too */
821 read_lock_bh(&idev->lock);
822 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
823 struct ip6_sf_list *psf, *psf_next;
825 spin_lock_bh(&pmc->mca_lock);
827 pmc->mca_tomb = NULL;
828 spin_unlock_bh(&pmc->mca_lock);
829 for (; psf; psf=psf_next) {
830 psf_next = psf->sf_next;
834 read_unlock_bh(&idev->lock);
839 * device multicast group inc (add if not found)
841 int ipv6_dev_mc_inc(struct net_device *dev, struct in6_addr *addr)
843 struct ifmcaddr6 *mc;
844 struct inet6_dev *idev;
846 idev = in6_dev_get(dev);
851 write_lock_bh(&idev->lock);
853 write_unlock_bh(&idev->lock);
858 for (mc = idev->mc_list; mc; mc = mc->next) {
859 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
861 write_unlock_bh(&idev->lock);
862 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
870 * not found: create a new one.
873 mc = kmalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
876 write_unlock_bh(&idev->lock);
881 memset(mc, 0, sizeof(struct ifmcaddr6));
882 init_timer(&mc->mca_timer);
883 mc->mca_timer.function = igmp6_timer_handler;
884 mc->mca_timer.data = (unsigned long) mc;
886 ipv6_addr_copy(&mc->mca_addr, addr);
889 /* mca_stamp should be updated upon changes */
890 mc->mca_cstamp = mc->mca_tstamp = jiffies;
891 atomic_set(&mc->mca_refcnt, 2);
892 spin_lock_init(&mc->mca_lock);
894 /* initial mode is (EX, empty) */
895 mc->mca_sfmode = MCAST_EXCLUDE;
896 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
898 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
899 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
900 mc->mca_flags |= MAF_NOREPORT;
902 mc->next = idev->mc_list;
904 write_unlock_bh(&idev->lock);
906 mld_del_delrec(idev, &mc->mca_addr);
907 igmp6_group_added(mc);
913 * device multicast group del
915 int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr)
917 struct ifmcaddr6 *ma, **map;
919 write_lock_bh(&idev->lock);
920 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
921 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
922 if (--ma->mca_users == 0) {
924 write_unlock_bh(&idev->lock);
926 igmp6_group_dropped(ma);
931 write_unlock_bh(&idev->lock);
935 write_unlock_bh(&idev->lock);
940 int ipv6_dev_mc_dec(struct net_device *dev, struct in6_addr *addr)
942 struct inet6_dev *idev = in6_dev_get(dev);
948 err = __ipv6_dev_mc_dec(idev, addr);
956 * identify MLD packets for MLD filter exceptions
958 int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
960 struct icmp6hdr *pic;
962 if (nexthdr != IPPROTO_ICMPV6)
965 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
968 pic = (struct icmp6hdr *)skb->h.raw;
970 switch (pic->icmp6_type) {
971 case ICMPV6_MGM_QUERY:
972 case ICMPV6_MGM_REPORT:
973 case ICMPV6_MGM_REDUCTION:
974 case ICMPV6_MLD2_REPORT:
983 * check if the interface/address pair is valid
985 int ipv6_chk_mcast_addr(struct net_device *dev, struct in6_addr *group,
986 struct in6_addr *src_addr)
988 struct inet6_dev *idev;
989 struct ifmcaddr6 *mc;
992 idev = in6_dev_get(dev);
994 read_lock_bh(&idev->lock);
995 for (mc = idev->mc_list; mc; mc=mc->next) {
996 if (ipv6_addr_equal(&mc->mca_addr, group))
1000 if (src_addr && !ipv6_addr_any(src_addr)) {
1001 struct ip6_sf_list *psf;
1003 spin_lock_bh(&mc->mca_lock);
1004 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1005 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1009 rv = psf->sf_count[MCAST_INCLUDE] ||
1010 psf->sf_count[MCAST_EXCLUDE] !=
1011 mc->mca_sfcount[MCAST_EXCLUDE];
1013 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1014 spin_unlock_bh(&mc->mca_lock);
1016 rv = 1; /* don't filter unspecified source */
1018 read_unlock_bh(&idev->lock);
1024 static void mld_gq_start_timer(struct inet6_dev *idev)
1026 int tv = net_random() % idev->mc_maxdelay;
1028 idev->mc_gq_running = 1;
1029 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1033 static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1035 int tv = net_random() % delay;
1037 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1042 * IGMP handling (alias multicast ICMPv6 messages)
1045 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1047 unsigned long delay = resptime;
1049 /* Do not start timer for these addresses */
1050 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1051 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1054 if (del_timer(&ma->mca_timer)) {
1055 atomic_dec(&ma->mca_refcnt);
1056 delay = ma->mca_timer.expires - jiffies;
1059 if (delay >= resptime) {
1061 delay = net_random() % resptime;
1065 ma->mca_timer.expires = jiffies + delay;
1066 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1067 atomic_inc(&ma->mca_refcnt);
1068 ma->mca_flags |= MAF_TIMER_RUNNING;
1071 static void mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1072 struct in6_addr *srcs)
1074 struct ip6_sf_list *psf;
1078 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1079 if (scount == nsrcs)
1081 for (i=0; i<nsrcs; i++)
1082 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1090 int igmp6_event_query(struct sk_buff *skb)
1092 struct mld2_query *mlh2 = NULL;
1093 struct ifmcaddr6 *ma;
1094 struct in6_addr *group;
1095 unsigned long max_delay;
1096 struct inet6_dev *idev;
1097 struct icmp6hdr *hdr;
1102 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1105 /* compute payload length excluding extension headers */
1106 len = ntohs(skb->nh.ipv6h->payload_len) + sizeof(struct ipv6hdr);
1107 len -= (char *)skb->h.raw - (char *)skb->nh.ipv6h;
1109 /* Drop queries with not link local source */
1110 if (!(ipv6_addr_type(&skb->nh.ipv6h->saddr)&IPV6_ADDR_LINKLOCAL))
1113 idev = in6_dev_get(skb->dev);
1118 hdr = (struct icmp6hdr *) skb->h.raw;
1119 group = (struct in6_addr *) (hdr + 1);
1120 group_type = ipv6_addr_type(group);
1122 if (group_type != IPV6_ADDR_ANY &&
1123 !(group_type&IPV6_ADDR_MULTICAST)) {
1130 /* MLDv1 router present */
1132 /* Translate milliseconds to jiffies */
1133 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1135 switchback = (idev->mc_qrv + 1) * max_delay;
1136 idev->mc_v1_seen = jiffies + switchback;
1138 /* cancel the interface change timer */
1139 idev->mc_ifc_count = 0;
1140 if (del_timer(&idev->mc_ifc_timer))
1141 __in6_dev_put(idev);
1142 /* clear deleted report items */
1143 mld_clear_delrec(idev);
1144 } else if (len >= 28) {
1145 int srcs_offset = sizeof(struct mld2_query) -
1146 sizeof(struct icmp6hdr);
1147 if (!pskb_may_pull(skb, srcs_offset)) {
1151 mlh2 = (struct mld2_query *) skb->h.raw;
1152 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1155 idev->mc_maxdelay = max_delay;
1157 idev->mc_qrv = mlh2->qrv;
1158 if (group_type == IPV6_ADDR_ANY) { /* general query */
1161 return -EINVAL; /* no sources allowed */
1163 mld_gq_start_timer(idev);
1167 /* mark sources to include, if group & source-specific */
1168 if (mlh2->nsrcs != 0) {
1169 if (!pskb_may_pull(skb, srcs_offset +
1170 mlh2->nsrcs * sizeof(struct in6_addr))) {
1174 mlh2 = (struct mld2_query *) skb->h.raw;
1182 read_lock_bh(&idev->lock);
1183 if (group_type == IPV6_ADDR_ANY) {
1184 for (ma = idev->mc_list; ma; ma=ma->next) {
1185 spin_lock_bh(&ma->mca_lock);
1186 igmp6_group_queried(ma, max_delay);
1187 spin_unlock_bh(&ma->mca_lock);
1190 for (ma = idev->mc_list; ma; ma=ma->next) {
1191 if (group_type != IPV6_ADDR_ANY &&
1192 !ipv6_addr_equal(group, &ma->mca_addr))
1194 spin_lock_bh(&ma->mca_lock);
1195 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1196 /* gsquery <- gsquery && mark */
1198 ma->mca_flags &= ~MAF_GSQUERY;
1200 /* gsquery <- mark */
1202 ma->mca_flags |= MAF_GSQUERY;
1204 ma->mca_flags &= ~MAF_GSQUERY;
1206 if (ma->mca_flags & MAF_GSQUERY)
1207 mld_marksources(ma, ntohs(mlh2->nsrcs),
1209 igmp6_group_queried(ma, max_delay);
1210 spin_unlock_bh(&ma->mca_lock);
1211 if (group_type != IPV6_ADDR_ANY)
1215 read_unlock_bh(&idev->lock);
1222 int igmp6_event_report(struct sk_buff *skb)
1224 struct ifmcaddr6 *ma;
1225 struct in6_addr *addrp;
1226 struct inet6_dev *idev;
1227 struct icmp6hdr *hdr;
1230 /* Our own report looped back. Ignore it. */
1231 if (skb->pkt_type == PACKET_LOOPBACK)
1234 /* send our report if the MC router may not have heard this report */
1235 if (skb->pkt_type != PACKET_MULTICAST &&
1236 skb->pkt_type != PACKET_BROADCAST)
1239 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1242 hdr = (struct icmp6hdr*) skb->h.raw;
1244 /* Drop reports with not link local source */
1245 addr_type = ipv6_addr_type(&skb->nh.ipv6h->saddr);
1246 if (addr_type != IPV6_ADDR_ANY &&
1247 !(addr_type&IPV6_ADDR_LINKLOCAL))
1250 addrp = (struct in6_addr *) (hdr + 1);
1252 idev = in6_dev_get(skb->dev);
1257 * Cancel the timer for this group
1260 read_lock_bh(&idev->lock);
1261 for (ma = idev->mc_list; ma; ma=ma->next) {
1262 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1263 spin_lock(&ma->mca_lock);
1264 if (del_timer(&ma->mca_timer))
1265 atomic_dec(&ma->mca_refcnt);
1266 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1267 spin_unlock(&ma->mca_lock);
1271 read_unlock_bh(&idev->lock);
1276 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1277 int gdeleted, int sdeleted)
1280 case MLD2_MODE_IS_INCLUDE:
1281 case MLD2_MODE_IS_EXCLUDE:
1282 if (gdeleted || sdeleted)
1284 return !((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp);
1285 case MLD2_CHANGE_TO_INCLUDE:
1286 if (gdeleted || sdeleted)
1288 return psf->sf_count[MCAST_INCLUDE] != 0;
1289 case MLD2_CHANGE_TO_EXCLUDE:
1290 if (gdeleted || sdeleted)
1292 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1293 psf->sf_count[MCAST_INCLUDE])
1295 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1296 psf->sf_count[MCAST_EXCLUDE];
1297 case MLD2_ALLOW_NEW_SOURCES:
1298 if (gdeleted || !psf->sf_crcount)
1300 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1301 case MLD2_BLOCK_OLD_SOURCES:
1302 if (pmc->mca_sfmode == MCAST_INCLUDE)
1303 return gdeleted || (psf->sf_crcount && sdeleted);
1304 return psf->sf_crcount && !gdeleted && !sdeleted;
1310 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1312 struct ip6_sf_list *psf;
1315 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1316 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1323 static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1325 struct sock *sk = igmp6_socket->sk;
1326 struct sk_buff *skb;
1327 struct mld2_report *pmr;
1328 struct in6_addr addr_buf;
1330 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1331 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1334 /* we assume size > sizeof(ra) here */
1335 skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
1340 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1342 if (ipv6_get_lladdr(dev, &addr_buf)) {
1343 /* <draft-ietf-magma-mld-source-05.txt>:
1344 * use unspecified address as the source address
1345 * when a valid link-local address is not available.
1347 memset(&addr_buf, 0, sizeof(addr_buf));
1350 ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);
1352 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1354 pmr =(struct mld2_report *)skb_put(skb, sizeof(*pmr));
1355 skb->h.raw = (unsigned char *)pmr;
1356 pmr->type = ICMPV6_MLD2_REPORT;
1364 static inline int mld_dev_queue_xmit2(struct sk_buff *skb)
1366 struct net_device *dev = skb->dev;
1368 if (dev->hard_header) {
1369 unsigned char ha[MAX_ADDR_LEN];
1372 ndisc_mc_map(&skb->nh.ipv6h->daddr, ha, dev, 1);
1373 err = dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, skb->len);
1379 return dev_queue_xmit(skb);
1382 static inline int mld_dev_queue_xmit(struct sk_buff *skb)
1384 return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dev,
1385 mld_dev_queue_xmit2);
1388 static void mld_sendpack(struct sk_buff *skb)
1390 struct ipv6hdr *pip6 = skb->nh.ipv6h;
1391 struct mld2_report *pmr = (struct mld2_report *)skb->h.raw;
1392 int payload_len, mldlen;
1393 struct inet6_dev *idev = in6_dev_get(skb->dev);
1396 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1397 payload_len = skb->tail - (unsigned char *)skb->nh.ipv6h -
1398 sizeof(struct ipv6hdr);
1399 mldlen = skb->tail - skb->h.raw;
1400 pip6->payload_len = htons(payload_len);
1402 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1403 IPPROTO_ICMPV6, csum_partial(skb->h.raw, mldlen, 0));
1404 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1405 mld_dev_queue_xmit);
1407 ICMP6_INC_STATS(idev,ICMP6_MIB_OUTMSGS);
1408 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1410 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1412 if (likely(idev != NULL))
1416 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1418 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1421 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1422 int type, struct mld2_grec **ppgr)
1424 struct net_device *dev = pmc->idev->dev;
1425 struct mld2_report *pmr;
1426 struct mld2_grec *pgr;
1429 skb = mld_newpack(dev, dev->mtu);
1432 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1433 pgr->grec_type = type;
1434 pgr->grec_auxwords = 0;
1435 pgr->grec_nsrcs = 0;
1436 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1437 pmr = (struct mld2_report *)skb->h.raw;
1438 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1443 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1444 skb_tailroom(skb)) : 0)
1446 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1447 int type, int gdeleted, int sdeleted)
1449 struct net_device *dev = pmc->idev->dev;
1450 struct mld2_report *pmr;
1451 struct mld2_grec *pgr = NULL;
1452 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1453 int scount, first, isquery, truncate;
1455 if (pmc->mca_flags & MAF_NOREPORT)
1458 isquery = type == MLD2_MODE_IS_INCLUDE ||
1459 type == MLD2_MODE_IS_EXCLUDE;
1460 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1461 type == MLD2_CHANGE_TO_EXCLUDE;
1463 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1466 if (type == MLD2_ALLOW_NEW_SOURCES ||
1467 type == MLD2_BLOCK_OLD_SOURCES)
1469 if (pmc->mca_crcount || isquery) {
1470 /* make sure we have room for group header and at
1473 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)+
1474 sizeof(struct in6_addr)) {
1476 skb = NULL; /* add_grhead will get a new one */
1478 skb = add_grhead(skb, pmc, type, &pgr);
1482 pmr = skb ? (struct mld2_report *)skb->h.raw : NULL;
1484 /* EX and TO_EX get a fresh packet, if needed */
1486 if (pmr && pmr->ngrec &&
1487 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1490 skb = mld_newpack(dev, dev->mtu);
1496 for (psf=*psf_list; psf; psf=psf_next) {
1497 struct in6_addr *psrc;
1499 psf_next = psf->sf_next;
1501 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1506 /* clear marks on query responses */
1510 if (AVAILABLE(skb) < sizeof(*psrc) +
1511 first*sizeof(struct mld2_grec)) {
1512 if (truncate && !first)
1513 break; /* truncate these */
1515 pgr->grec_nsrcs = htons(scount);
1518 skb = mld_newpack(dev, dev->mtu);
1523 skb = add_grhead(skb, pmc, type, &pgr);
1526 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1527 *psrc = psf->sf_addr;
1529 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1530 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1532 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1534 psf_prev->sf_next = psf->sf_next;
1536 *psf_list = psf->sf_next;
1544 pgr->grec_nsrcs = htons(scount);
1547 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1551 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1553 struct sk_buff *skb = NULL;
1557 read_lock_bh(&idev->lock);
1558 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1559 if (pmc->mca_flags & MAF_NOREPORT)
1561 spin_lock_bh(&pmc->mca_lock);
1562 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1563 type = MLD2_MODE_IS_EXCLUDE;
1565 type = MLD2_MODE_IS_INCLUDE;
1566 skb = add_grec(skb, pmc, type, 0, 0);
1567 spin_unlock_bh(&pmc->mca_lock);
1569 read_unlock_bh(&idev->lock);
1571 spin_lock_bh(&pmc->mca_lock);
1572 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1573 type = MLD2_MODE_IS_EXCLUDE;
1575 type = MLD2_MODE_IS_INCLUDE;
1576 skb = add_grec(skb, pmc, type, 0, 0);
1577 spin_unlock_bh(&pmc->mca_lock);
1584 * remove zero-count source records from a source filter list
1586 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1588 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1591 for (psf=*ppsf; psf; psf = psf_next) {
1592 psf_next = psf->sf_next;
1593 if (psf->sf_crcount == 0) {
1595 psf_prev->sf_next = psf->sf_next;
1597 *ppsf = psf->sf_next;
1604 static void mld_send_cr(struct inet6_dev *idev)
1606 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1607 struct sk_buff *skb = NULL;
1610 read_lock_bh(&idev->lock);
1611 write_lock_bh(&idev->mc_lock);
1615 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1616 pmc_next = pmc->next;
1617 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1618 type = MLD2_BLOCK_OLD_SOURCES;
1619 dtype = MLD2_BLOCK_OLD_SOURCES;
1620 skb = add_grec(skb, pmc, type, 1, 0);
1621 skb = add_grec(skb, pmc, dtype, 1, 1);
1623 if (pmc->mca_crcount) {
1625 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1626 type = MLD2_CHANGE_TO_INCLUDE;
1627 skb = add_grec(skb, pmc, type, 1, 0);
1629 if (pmc->mca_crcount == 0) {
1630 mld_clear_zeros(&pmc->mca_tomb);
1631 mld_clear_zeros(&pmc->mca_sources);
1634 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1635 !pmc->mca_sources) {
1637 pmc_prev->next = pmc_next;
1639 idev->mc_tomb = pmc_next;
1640 in6_dev_put(pmc->idev);
1645 write_unlock_bh(&idev->mc_lock);
1648 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1649 spin_lock_bh(&pmc->mca_lock);
1650 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1651 type = MLD2_BLOCK_OLD_SOURCES;
1652 dtype = MLD2_ALLOW_NEW_SOURCES;
1654 type = MLD2_ALLOW_NEW_SOURCES;
1655 dtype = MLD2_BLOCK_OLD_SOURCES;
1657 skb = add_grec(skb, pmc, type, 0, 0);
1658 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1660 /* filter mode changes */
1661 if (pmc->mca_crcount) {
1663 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1664 type = MLD2_CHANGE_TO_EXCLUDE;
1666 type = MLD2_CHANGE_TO_INCLUDE;
1667 skb = add_grec(skb, pmc, type, 0, 0);
1669 spin_unlock_bh(&pmc->mca_lock);
1671 read_unlock_bh(&idev->lock);
1674 (void) mld_sendpack(skb);
1677 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1679 struct sock *sk = igmp6_socket->sk;
1680 struct inet6_dev *idev;
1681 struct sk_buff *skb;
1682 struct icmp6hdr *hdr;
1683 struct in6_addr *snd_addr;
1684 struct in6_addr *addrp;
1685 struct in6_addr addr_buf;
1686 struct in6_addr all_routers;
1687 int err, len, payload_len, full_len;
1688 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1689 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1692 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1694 if (type == ICMPV6_MGM_REDUCTION) {
1695 snd_addr = &all_routers;
1696 ipv6_addr_all_routers(&all_routers);
1699 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1700 payload_len = len + sizeof(ra);
1701 full_len = sizeof(struct ipv6hdr) + payload_len;
1703 skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1706 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1710 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1712 if (ipv6_get_lladdr(dev, &addr_buf)) {
1713 /* <draft-ietf-magma-mld-source-05.txt>:
1714 * use unspecified address as the source address
1715 * when a valid link-local address is not available.
1717 memset(&addr_buf, 0, sizeof(addr_buf));
1720 ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
1722 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1724 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1725 memset(hdr, 0, sizeof(struct icmp6hdr));
1726 hdr->icmp6_type = type;
1728 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1729 ipv6_addr_copy(addrp, addr);
1731 hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
1733 csum_partial((__u8 *) hdr, len, 0));
1735 idev = in6_dev_get(skb->dev);
1737 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1738 mld_dev_queue_xmit);
1740 if (type == ICMPV6_MGM_REDUCTION)
1741 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS);
1743 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBRESPONSES);
1744 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1745 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1747 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1749 if (likely(idev != NULL))
1754 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1755 struct in6_addr *psfsrc)
1757 struct ip6_sf_list *psf, *psf_prev;
1761 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1762 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1766 if (!psf || psf->sf_count[sfmode] == 0) {
1767 /* source filter not found, or count wrong => bug */
1770 psf->sf_count[sfmode]--;
1771 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1772 struct inet6_dev *idev = pmc->idev;
1774 /* no more filters for this source */
1776 psf_prev->sf_next = psf->sf_next;
1778 pmc->mca_sources = psf->sf_next;
1779 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1780 !MLD_V1_SEEN(idev)) {
1781 psf->sf_crcount = idev->mc_qrv;
1782 psf->sf_next = pmc->mca_tomb;
1783 pmc->mca_tomb = psf;
1791 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1792 int sfmode, int sfcount, struct in6_addr *psfsrc,
1795 struct ifmcaddr6 *pmc;
1801 read_lock_bh(&idev->lock);
1802 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1803 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1807 /* MCA not found?? bug */
1808 read_unlock_bh(&idev->lock);
1811 spin_lock_bh(&pmc->mca_lock);
1814 if (!pmc->mca_sfcount[sfmode]) {
1815 spin_unlock_bh(&pmc->mca_lock);
1816 read_unlock_bh(&idev->lock);
1819 pmc->mca_sfcount[sfmode]--;
1822 for (i=0; i<sfcount; i++) {
1823 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1825 changerec |= rv > 0;
1829 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1830 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1831 pmc->mca_sfcount[MCAST_INCLUDE]) {
1832 struct ip6_sf_list *psf;
1834 /* filter mode change */
1835 pmc->mca_sfmode = MCAST_INCLUDE;
1836 pmc->mca_crcount = idev->mc_qrv;
1837 idev->mc_ifc_count = pmc->mca_crcount;
1838 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1839 psf->sf_crcount = 0;
1840 mld_ifc_event(pmc->idev);
1841 } else if (sf_setstate(pmc) || changerec)
1842 mld_ifc_event(pmc->idev);
1843 spin_unlock_bh(&pmc->mca_lock);
1844 read_unlock_bh(&idev->lock);
1849 * Add multicast single-source filter to the interface list
1851 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1852 struct in6_addr *psfsrc, int delta)
1854 struct ip6_sf_list *psf, *psf_prev;
1857 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1858 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1863 psf = (struct ip6_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1866 memset(psf, 0, sizeof(*psf));
1867 psf->sf_addr = *psfsrc;
1869 psf_prev->sf_next = psf;
1871 pmc->mca_sources = psf;
1873 psf->sf_count[sfmode]++;
1877 static void sf_markstate(struct ifmcaddr6 *pmc)
1879 struct ip6_sf_list *psf;
1880 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1882 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1883 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1884 psf->sf_oldin = mca_xcount ==
1885 psf->sf_count[MCAST_EXCLUDE] &&
1886 !psf->sf_count[MCAST_INCLUDE];
1888 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1891 static int sf_setstate(struct ifmcaddr6 *pmc)
1893 struct ip6_sf_list *psf;
1894 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1895 int qrv = pmc->idev->mc_qrv;
1899 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1900 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1901 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1902 !psf->sf_count[MCAST_INCLUDE];
1904 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1905 if (new_in != psf->sf_oldin) {
1906 psf->sf_crcount = qrv;
1914 * Add multicast source filter list to the interface list
1916 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
1917 int sfmode, int sfcount, struct in6_addr *psfsrc,
1920 struct ifmcaddr6 *pmc;
1926 read_lock_bh(&idev->lock);
1927 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1928 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1932 /* MCA not found?? bug */
1933 read_unlock_bh(&idev->lock);
1936 spin_lock_bh(&pmc->mca_lock);
1939 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
1941 pmc->mca_sfcount[sfmode]++;
1943 for (i=0; i<sfcount; i++) {
1944 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1952 pmc->mca_sfcount[sfmode]--;
1954 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1955 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
1956 struct inet6_dev *idev = pmc->idev;
1957 struct ip6_sf_list *psf;
1959 /* filter mode change */
1960 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1961 pmc->mca_sfmode = MCAST_EXCLUDE;
1962 else if (pmc->mca_sfcount[MCAST_INCLUDE])
1963 pmc->mca_sfmode = MCAST_INCLUDE;
1964 /* else no filters; keep old mode for reports */
1966 pmc->mca_crcount = idev->mc_qrv;
1967 idev->mc_ifc_count = pmc->mca_crcount;
1968 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1969 psf->sf_crcount = 0;
1970 mld_ifc_event(idev);
1971 } else if (sf_setstate(pmc))
1972 mld_ifc_event(idev);
1973 spin_unlock_bh(&pmc->mca_lock);
1974 read_unlock_bh(&idev->lock);
1978 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
1980 struct ip6_sf_list *psf, *nextpsf;
1982 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
1983 nextpsf = psf->sf_next;
1986 pmc->mca_tomb = NULL;
1987 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
1988 nextpsf = psf->sf_next;
1991 pmc->mca_sources = NULL;
1992 pmc->mca_sfmode = MCAST_EXCLUDE;
1993 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
1994 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
1998 static void igmp6_join_group(struct ifmcaddr6 *ma)
2000 unsigned long delay;
2002 if (ma->mca_flags & MAF_NOREPORT)
2005 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2007 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2009 spin_lock_bh(&ma->mca_lock);
2010 if (del_timer(&ma->mca_timer)) {
2011 atomic_dec(&ma->mca_refcnt);
2012 delay = ma->mca_timer.expires - jiffies;
2015 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2016 atomic_inc(&ma->mca_refcnt);
2017 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2018 spin_unlock_bh(&ma->mca_lock);
2021 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2022 struct inet6_dev *idev)
2026 if (iml->sflist == 0) {
2027 /* any-source empty exclude case */
2028 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2030 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2031 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2032 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2037 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2039 if (MLD_V1_SEEN(ma->idev)) {
2040 if (ma->mca_flags & MAF_LAST_REPORTER)
2041 igmp6_send(&ma->mca_addr, ma->idev->dev,
2042 ICMPV6_MGM_REDUCTION);
2044 mld_add_delrec(ma->idev, ma);
2045 mld_ifc_event(ma->idev);
2049 static void mld_gq_timer_expire(unsigned long data)
2051 struct inet6_dev *idev = (struct inet6_dev *)data;
2053 idev->mc_gq_running = 0;
2054 mld_send_report(idev, NULL);
2055 __in6_dev_put(idev);
2058 static void mld_ifc_timer_expire(unsigned long data)
2060 struct inet6_dev *idev = (struct inet6_dev *)data;
2063 if (idev->mc_ifc_count) {
2064 idev->mc_ifc_count--;
2065 if (idev->mc_ifc_count)
2066 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2068 __in6_dev_put(idev);
2071 static void mld_ifc_event(struct inet6_dev *idev)
2073 if (MLD_V1_SEEN(idev))
2075 idev->mc_ifc_count = idev->mc_qrv;
2076 mld_ifc_start_timer(idev, 1);
2080 static void igmp6_timer_handler(unsigned long data)
2082 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2084 if (MLD_V1_SEEN(ma->idev))
2085 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2087 mld_send_report(ma->idev, ma);
2089 spin_lock(&ma->mca_lock);
2090 ma->mca_flags |= MAF_LAST_REPORTER;
2091 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2092 spin_unlock(&ma->mca_lock);
2096 /* Device going down */
2098 void ipv6_mc_down(struct inet6_dev *idev)
2100 struct ifmcaddr6 *i;
2102 /* Withdraw multicast list */
2104 read_lock_bh(&idev->lock);
2105 idev->mc_ifc_count = 0;
2106 if (del_timer(&idev->mc_ifc_timer))
2107 __in6_dev_put(idev);
2108 idev->mc_gq_running = 0;
2109 if (del_timer(&idev->mc_gq_timer))
2110 __in6_dev_put(idev);
2112 for (i = idev->mc_list; i; i=i->next)
2113 igmp6_group_dropped(i);
2114 read_unlock_bh(&idev->lock);
2116 mld_clear_delrec(idev);
2120 /* Device going up */
2122 void ipv6_mc_up(struct inet6_dev *idev)
2124 struct ifmcaddr6 *i;
2126 /* Install multicast list, except for all-nodes (already installed) */
2128 read_lock_bh(&idev->lock);
2129 for (i = idev->mc_list; i; i=i->next)
2130 igmp6_group_added(i);
2131 read_unlock_bh(&idev->lock);
2134 /* IPv6 device initialization. */
2136 void ipv6_mc_init_dev(struct inet6_dev *idev)
2138 struct in6_addr maddr;
2140 write_lock_bh(&idev->lock);
2141 rwlock_init(&idev->mc_lock);
2142 idev->mc_gq_running = 0;
2143 init_timer(&idev->mc_gq_timer);
2144 idev->mc_gq_timer.data = (unsigned long) idev;
2145 idev->mc_gq_timer.function = &mld_gq_timer_expire;
2146 idev->mc_tomb = NULL;
2147 idev->mc_ifc_count = 0;
2148 init_timer(&idev->mc_ifc_timer);
2149 idev->mc_ifc_timer.data = (unsigned long) idev;
2150 idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
2151 idev->mc_qrv = MLD_QRV_DEFAULT;
2152 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2153 idev->mc_v1_seen = 0;
2154 write_unlock_bh(&idev->lock);
2156 /* Add all-nodes address. */
2157 ipv6_addr_all_nodes(&maddr);
2158 ipv6_dev_mc_inc(idev->dev, &maddr);
2162 * Device is about to be destroyed: clean up.
2165 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2167 struct ifmcaddr6 *i;
2168 struct in6_addr maddr;
2170 /* Deactivate timers */
2173 /* Delete all-nodes address. */
2174 ipv6_addr_all_nodes(&maddr);
2176 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2177 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2180 __ipv6_dev_mc_dec(idev, &maddr);
2182 if (idev->cnf.forwarding) {
2183 ipv6_addr_all_routers(&maddr);
2184 __ipv6_dev_mc_dec(idev, &maddr);
2187 write_lock_bh(&idev->lock);
2188 while ((i = idev->mc_list) != NULL) {
2189 idev->mc_list = i->next;
2190 write_unlock_bh(&idev->lock);
2192 igmp6_group_dropped(i);
2195 write_lock_bh(&idev->lock);
2197 write_unlock_bh(&idev->lock);
2200 #ifdef CONFIG_PROC_FS
2201 struct igmp6_mc_iter_state {
2202 struct net_device *dev;
2203 struct inet6_dev *idev;
2206 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2208 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2210 struct ifmcaddr6 *im = NULL;
2211 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2213 for (state->dev = dev_base, state->idev = NULL;
2215 state->dev = state->dev->next) {
2216 struct inet6_dev *idev;
2217 idev = in6_dev_get(state->dev);
2220 read_lock_bh(&idev->lock);
2226 read_unlock_bh(&idev->lock);
2232 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2234 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2238 if (likely(state->idev != NULL)) {
2239 read_unlock_bh(&state->idev->lock);
2240 in6_dev_put(state->idev);
2242 state->dev = state->dev->next;
2247 state->idev = in6_dev_get(state->dev);
2250 read_lock_bh(&state->idev->lock);
2251 im = state->idev->mc_list;
2256 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2258 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2260 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2262 return pos ? NULL : im;
2265 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2267 read_lock(&dev_base_lock);
2268 return igmp6_mc_get_idx(seq, *pos);
2271 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2273 struct ifmcaddr6 *im;
2274 im = igmp6_mc_get_next(seq, v);
2279 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2281 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2282 if (likely(state->idev != NULL)) {
2283 read_unlock_bh(&state->idev->lock);
2284 in6_dev_put(state->idev);
2288 read_unlock(&dev_base_lock);
2291 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2293 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2294 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2297 "%-4d %-15s %04x%04x%04x%04x%04x%04x%04x%04x %5d %08X %ld\n",
2298 state->dev->ifindex, state->dev->name,
2300 im->mca_users, im->mca_flags,
2301 (im->mca_flags&MAF_TIMER_RUNNING) ?
2302 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2306 static struct seq_operations igmp6_mc_seq_ops = {
2307 .start = igmp6_mc_seq_start,
2308 .next = igmp6_mc_seq_next,
2309 .stop = igmp6_mc_seq_stop,
2310 .show = igmp6_mc_seq_show,
2313 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2315 struct seq_file *seq;
2317 struct igmp6_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2322 rc = seq_open(file, &igmp6_mc_seq_ops);
2326 seq = file->private_data;
2328 memset(s, 0, sizeof(*s));
2336 static struct file_operations igmp6_mc_seq_fops = {
2337 .owner = THIS_MODULE,
2338 .open = igmp6_mc_seq_open,
2340 .llseek = seq_lseek,
2341 .release = seq_release_private,
2344 struct igmp6_mcf_iter_state {
2345 struct net_device *dev;
2346 struct inet6_dev *idev;
2347 struct ifmcaddr6 *im;
2350 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2352 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2354 struct ip6_sf_list *psf = NULL;
2355 struct ifmcaddr6 *im = NULL;
2356 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2358 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2360 state->dev = state->dev->next) {
2361 struct inet6_dev *idev;
2362 idev = in6_dev_get(state->dev);
2363 if (unlikely(idev == NULL))
2365 read_lock_bh(&idev->lock);
2367 if (likely(im != NULL)) {
2368 spin_lock_bh(&im->mca_lock);
2369 psf = im->mca_sources;
2370 if (likely(psf != NULL)) {
2375 spin_unlock_bh(&im->mca_lock);
2377 read_unlock_bh(&idev->lock);
2383 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2385 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2389 spin_unlock_bh(&state->im->mca_lock);
2390 state->im = state->im->next;
2391 while (!state->im) {
2392 if (likely(state->idev != NULL)) {
2393 read_unlock_bh(&state->idev->lock);
2394 in6_dev_put(state->idev);
2396 state->dev = state->dev->next;
2401 state->idev = in6_dev_get(state->dev);
2404 read_lock_bh(&state->idev->lock);
2405 state->im = state->idev->mc_list;
2409 spin_lock_bh(&state->im->mca_lock);
2410 psf = state->im->mca_sources;
2416 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2418 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2420 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2422 return pos ? NULL : psf;
2425 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2427 read_lock(&dev_base_lock);
2428 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2431 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2433 struct ip6_sf_list *psf;
2434 if (v == SEQ_START_TOKEN)
2435 psf = igmp6_mcf_get_first(seq);
2437 psf = igmp6_mcf_get_next(seq, v);
2442 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2444 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2445 if (likely(state->im != NULL)) {
2446 spin_unlock_bh(&state->im->mca_lock);
2449 if (likely(state->idev != NULL)) {
2450 read_unlock_bh(&state->idev->lock);
2451 in6_dev_put(state->idev);
2455 read_unlock(&dev_base_lock);
2458 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2460 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2461 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2463 if (v == SEQ_START_TOKEN) {
2466 "%32s %32s %6s %6s\n", "Idx",
2467 "Device", "Multicast Address",
2468 "Source Address", "INC", "EXC");
2472 "%04x%04x%04x%04x%04x%04x%04x%04x "
2473 "%04x%04x%04x%04x%04x%04x%04x%04x "
2475 state->dev->ifindex, state->dev->name,
2476 NIP6(state->im->mca_addr),
2478 psf->sf_count[MCAST_INCLUDE],
2479 psf->sf_count[MCAST_EXCLUDE]);
2484 static struct seq_operations igmp6_mcf_seq_ops = {
2485 .start = igmp6_mcf_seq_start,
2486 .next = igmp6_mcf_seq_next,
2487 .stop = igmp6_mcf_seq_stop,
2488 .show = igmp6_mcf_seq_show,
2491 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2493 struct seq_file *seq;
2495 struct igmp6_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2500 rc = seq_open(file, &igmp6_mcf_seq_ops);
2504 seq = file->private_data;
2506 memset(s, 0, sizeof(*s));
2514 static struct file_operations igmp6_mcf_seq_fops = {
2515 .owner = THIS_MODULE,
2516 .open = igmp6_mcf_seq_open,
2518 .llseek = seq_lseek,
2519 .release = seq_release_private,
2523 int __init igmp6_init(struct net_proto_family *ops)
2525 struct ipv6_pinfo *np;
2529 err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
2532 "Failed to initialize the IGMP6 control socket (err %d).\n",
2534 igmp6_socket = NULL; /* For safety. */
2538 sk = igmp6_socket->sk;
2539 sk->sk_allocation = GFP_ATOMIC;
2540 sk->sk_prot->unhash(sk);
2545 #ifdef CONFIG_PROC_FS
2546 proc_net_fops_create("igmp6", S_IRUGO, &igmp6_mc_seq_fops);
2547 proc_net_fops_create("mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
2553 void igmp6_cleanup(void)
2555 sock_release(igmp6_socket);
2556 igmp6_socket = NULL; /* for safety */
2558 #ifdef CONFIG_PROC_FS
2559 proc_net_remove("mcfilter6");
2560 proc_net_remove("igmp6");