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+nbexp))) << \
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 10
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));
552 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
553 psl->sl_count, psl->sl_addr, 0);
554 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
556 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
557 pmc->sflist = newpsl;
558 pmc->sfmode = gsf->gf_fmode;
561 read_unlock_bh(&idev->lock);
565 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
569 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
570 struct group_filter __user *optval, int __user *optlen)
572 int err, i, count, copycount;
573 struct in6_addr *group;
574 struct ipv6_mc_socklist *pmc;
575 struct inet6_dev *idev;
576 struct net_device *dev;
577 struct ipv6_pinfo *inet6 = inet6_sk(sk);
578 struct ip6_sf_socklist *psl;
580 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
582 if (!ipv6_addr_is_multicast(group))
585 idev = ip6_mc_find_dev(group, gsf->gf_interface);
592 err = -EADDRNOTAVAIL;
594 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
595 if (pmc->ifindex != gsf->gf_interface)
597 if (ipv6_addr_equal(group, &pmc->addr))
600 if (!pmc) /* must have a prior join */
602 gsf->gf_fmode = pmc->sfmode;
604 count = psl ? psl->sl_count : 0;
605 read_unlock_bh(&idev->lock);
609 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
610 gsf->gf_numsrc = count;
611 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
612 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
615 for (i=0; i<copycount; i++) {
616 struct sockaddr_in6 *psin6;
617 struct sockaddr_storage ss;
619 psin6 = (struct sockaddr_in6 *)&ss;
620 memset(&ss, 0, sizeof(ss));
621 psin6->sin6_family = AF_INET6;
622 psin6->sin6_addr = psl->sl_addr[i];
623 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
628 read_unlock_bh(&idev->lock);
634 int inet6_mc_check(struct sock *sk, struct in6_addr *mc_addr,
635 struct in6_addr *src_addr)
637 struct ipv6_pinfo *np = inet6_sk(sk);
638 struct ipv6_mc_socklist *mc;
639 struct ip6_sf_socklist *psl;
642 read_lock(&ipv6_sk_mc_lock);
643 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
644 if (ipv6_addr_equal(&mc->addr, mc_addr))
648 read_unlock(&ipv6_sk_mc_lock);
653 rv = mc->sfmode == MCAST_EXCLUDE;
657 for (i=0; i<psl->sl_count; i++) {
658 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
661 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
663 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
666 read_unlock(&ipv6_sk_mc_lock);
671 static void ma_put(struct ifmcaddr6 *mc)
673 if (atomic_dec_and_test(&mc->mca_refcnt)) {
674 in6_dev_put(mc->idev);
679 static void igmp6_group_added(struct ifmcaddr6 *mc)
681 struct net_device *dev = mc->idev->dev;
682 char buf[MAX_ADDR_LEN];
684 spin_lock_bh(&mc->mca_lock);
685 if (!(mc->mca_flags&MAF_LOADED)) {
686 mc->mca_flags |= MAF_LOADED;
687 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
688 dev_mc_add(dev, buf, dev->addr_len, 0);
690 spin_unlock_bh(&mc->mca_lock);
692 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
695 if (MLD_V1_SEEN(mc->idev)) {
696 igmp6_join_group(mc);
701 mc->mca_crcount = mc->idev->mc_qrv;
702 mld_ifc_event(mc->idev);
705 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
707 struct net_device *dev = mc->idev->dev;
708 char buf[MAX_ADDR_LEN];
710 spin_lock_bh(&mc->mca_lock);
711 if (mc->mca_flags&MAF_LOADED) {
712 mc->mca_flags &= ~MAF_LOADED;
713 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
714 dev_mc_delete(dev, buf, dev->addr_len, 0);
717 if (mc->mca_flags & MAF_NOREPORT)
719 spin_unlock_bh(&mc->mca_lock);
722 igmp6_leave_group(mc);
724 spin_lock_bh(&mc->mca_lock);
725 if (del_timer(&mc->mca_timer))
726 atomic_dec(&mc->mca_refcnt);
728 ip6_mc_clear_src(mc);
729 spin_unlock_bh(&mc->mca_lock);
733 * deleted ifmcaddr6 manipulation
735 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
737 struct ifmcaddr6 *pmc;
739 /* this is an "ifmcaddr6" for convenience; only the fields below
740 * are actually used. In particular, the refcnt and users are not
741 * used for management of the delete list. Using the same structure
742 * for deleted items allows change reports to use common code with
743 * non-deleted or query-response MCA's.
745 pmc = (struct ifmcaddr6 *)kmalloc(sizeof(*pmc), GFP_ATOMIC);
748 memset(pmc, 0, sizeof(*pmc));
749 spin_lock_bh(&im->mca_lock);
750 spin_lock_init(&pmc->mca_lock);
751 pmc->idev = im->idev;
753 pmc->mca_addr = im->mca_addr;
754 pmc->mca_crcount = idev->mc_qrv;
755 pmc->mca_sfmode = im->mca_sfmode;
756 if (pmc->mca_sfmode == MCAST_INCLUDE) {
757 struct ip6_sf_list *psf;
759 pmc->mca_tomb = im->mca_tomb;
760 pmc->mca_sources = im->mca_sources;
761 im->mca_tomb = im->mca_sources = NULL;
762 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
763 psf->sf_crcount = pmc->mca_crcount;
765 spin_unlock_bh(&im->mca_lock);
767 write_lock_bh(&idev->mc_lock);
768 pmc->next = idev->mc_tomb;
770 write_unlock_bh(&idev->mc_lock);
773 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
775 struct ifmcaddr6 *pmc, *pmc_prev;
776 struct ip6_sf_list *psf, *psf_next;
778 write_lock_bh(&idev->mc_lock);
780 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
781 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
787 pmc_prev->next = pmc->next;
789 idev->mc_tomb = pmc->next;
791 write_unlock_bh(&idev->mc_lock);
793 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
794 psf_next = psf->sf_next;
797 in6_dev_put(pmc->idev);
802 static void mld_clear_delrec(struct inet6_dev *idev)
804 struct ifmcaddr6 *pmc, *nextpmc;
806 write_lock_bh(&idev->mc_lock);
808 idev->mc_tomb = NULL;
809 write_unlock_bh(&idev->mc_lock);
811 for (; pmc; pmc = nextpmc) {
813 ip6_mc_clear_src(pmc);
814 in6_dev_put(pmc->idev);
818 /* clear dead sources, too */
819 read_lock_bh(&idev->lock);
820 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
821 struct ip6_sf_list *psf, *psf_next;
823 spin_lock_bh(&pmc->mca_lock);
825 pmc->mca_tomb = NULL;
826 spin_unlock_bh(&pmc->mca_lock);
827 for (; psf; psf=psf_next) {
828 psf_next = psf->sf_next;
832 read_unlock_bh(&idev->lock);
837 * device multicast group inc (add if not found)
839 int ipv6_dev_mc_inc(struct net_device *dev, struct in6_addr *addr)
841 struct ifmcaddr6 *mc;
842 struct inet6_dev *idev;
844 idev = in6_dev_get(dev);
849 write_lock_bh(&idev->lock);
851 write_unlock_bh(&idev->lock);
856 for (mc = idev->mc_list; mc; mc = mc->next) {
857 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
859 write_unlock_bh(&idev->lock);
860 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
868 * not found: create a new one.
871 mc = kmalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
874 write_unlock_bh(&idev->lock);
879 memset(mc, 0, sizeof(struct ifmcaddr6));
880 init_timer(&mc->mca_timer);
881 mc->mca_timer.function = igmp6_timer_handler;
882 mc->mca_timer.data = (unsigned long) mc;
884 ipv6_addr_copy(&mc->mca_addr, addr);
887 /* mca_stamp should be updated upon changes */
888 mc->mca_cstamp = mc->mca_tstamp = jiffies;
889 atomic_set(&mc->mca_refcnt, 2);
890 spin_lock_init(&mc->mca_lock);
892 /* initial mode is (EX, empty) */
893 mc->mca_sfmode = MCAST_EXCLUDE;
894 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
896 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
897 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
898 mc->mca_flags |= MAF_NOREPORT;
900 mc->next = idev->mc_list;
902 write_unlock_bh(&idev->lock);
904 mld_del_delrec(idev, &mc->mca_addr);
905 igmp6_group_added(mc);
911 * device multicast group del
913 int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr)
915 struct ifmcaddr6 *ma, **map;
917 write_lock_bh(&idev->lock);
918 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
919 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
920 if (--ma->mca_users == 0) {
922 write_unlock_bh(&idev->lock);
924 igmp6_group_dropped(ma);
929 write_unlock_bh(&idev->lock);
933 write_unlock_bh(&idev->lock);
938 int ipv6_dev_mc_dec(struct net_device *dev, struct in6_addr *addr)
940 struct inet6_dev *idev = in6_dev_get(dev);
946 err = __ipv6_dev_mc_dec(idev, addr);
954 * identify MLD packets for MLD filter exceptions
956 int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
958 struct icmp6hdr *pic;
960 if (nexthdr != IPPROTO_ICMPV6)
963 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
966 pic = (struct icmp6hdr *)skb->h.raw;
968 switch (pic->icmp6_type) {
969 case ICMPV6_MGM_QUERY:
970 case ICMPV6_MGM_REPORT:
971 case ICMPV6_MGM_REDUCTION:
972 case ICMPV6_MLD2_REPORT:
981 * check if the interface/address pair is valid
983 int ipv6_chk_mcast_addr(struct net_device *dev, struct in6_addr *group,
984 struct in6_addr *src_addr)
986 struct inet6_dev *idev;
987 struct ifmcaddr6 *mc;
990 idev = in6_dev_get(dev);
992 read_lock_bh(&idev->lock);
993 for (mc = idev->mc_list; mc; mc=mc->next) {
994 if (ipv6_addr_equal(&mc->mca_addr, group))
998 if (src_addr && !ipv6_addr_any(src_addr)) {
999 struct ip6_sf_list *psf;
1001 spin_lock_bh(&mc->mca_lock);
1002 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1003 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1007 rv = psf->sf_count[MCAST_INCLUDE] ||
1008 psf->sf_count[MCAST_EXCLUDE] !=
1009 mc->mca_sfcount[MCAST_EXCLUDE];
1011 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1012 spin_unlock_bh(&mc->mca_lock);
1014 rv = 1; /* don't filter unspecified source */
1016 read_unlock_bh(&idev->lock);
1022 static void mld_gq_start_timer(struct inet6_dev *idev)
1024 int tv = net_random() % idev->mc_maxdelay;
1026 idev->mc_gq_running = 1;
1027 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1031 static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1033 int tv = net_random() % delay;
1035 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1040 * IGMP handling (alias multicast ICMPv6 messages)
1043 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1045 unsigned long delay = resptime;
1047 /* Do not start timer for these addresses */
1048 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1049 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1052 if (del_timer(&ma->mca_timer)) {
1053 atomic_dec(&ma->mca_refcnt);
1054 delay = ma->mca_timer.expires - jiffies;
1057 if (delay >= resptime) {
1059 delay = net_random() % resptime;
1063 ma->mca_timer.expires = jiffies + delay;
1064 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1065 atomic_inc(&ma->mca_refcnt);
1066 ma->mca_flags |= MAF_TIMER_RUNNING;
1069 static void mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1070 struct in6_addr *srcs)
1072 struct ip6_sf_list *psf;
1076 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1077 if (scount == nsrcs)
1079 for (i=0; i<nsrcs; i++)
1080 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1088 int igmp6_event_query(struct sk_buff *skb)
1090 struct mld2_query *mlh2 = (struct mld2_query *) skb->h.raw;
1091 struct ifmcaddr6 *ma;
1092 struct in6_addr *group;
1093 unsigned long max_delay;
1094 struct inet6_dev *idev;
1095 struct icmp6hdr *hdr;
1100 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1103 /* compute payload length excluding extension headers */
1104 len = ntohs(skb->nh.ipv6h->payload_len) + sizeof(struct ipv6hdr);
1105 len -= (char *)skb->h.raw - (char *)skb->nh.ipv6h;
1107 /* Drop queries with not link local source */
1108 if (!(ipv6_addr_type(&skb->nh.ipv6h->saddr)&IPV6_ADDR_LINKLOCAL))
1111 idev = in6_dev_get(skb->dev);
1116 hdr = (struct icmp6hdr *) skb->h.raw;
1117 group = (struct in6_addr *) (hdr + 1);
1118 group_type = ipv6_addr_type(group);
1120 if (group_type != IPV6_ADDR_ANY &&
1121 !(group_type&IPV6_ADDR_MULTICAST)) {
1128 /* MLDv1 router present */
1130 /* Translate milliseconds to jiffies */
1131 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1133 switchback = (idev->mc_qrv + 1) * max_delay;
1134 idev->mc_v1_seen = jiffies + switchback;
1136 /* cancel the interface change timer */
1137 idev->mc_ifc_count = 0;
1138 if (del_timer(&idev->mc_ifc_timer))
1139 __in6_dev_put(idev);
1140 /* clear deleted report items */
1141 mld_clear_delrec(idev);
1142 } else if (len >= 28) {
1143 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1146 idev->mc_maxdelay = max_delay;
1148 idev->mc_qrv = mlh2->qrv;
1149 if (group_type == IPV6_ADDR_ANY) { /* general query */
1152 return -EINVAL; /* no sources allowed */
1154 mld_gq_start_timer(idev);
1158 /* mark sources to include, if group & source-specific */
1159 mark = mlh2->nsrcs != 0;
1165 read_lock_bh(&idev->lock);
1166 if (group_type == IPV6_ADDR_ANY) {
1167 for (ma = idev->mc_list; ma; ma=ma->next) {
1168 spin_lock_bh(&ma->mca_lock);
1169 igmp6_group_queried(ma, max_delay);
1170 spin_unlock_bh(&ma->mca_lock);
1173 for (ma = idev->mc_list; ma; ma=ma->next) {
1174 if (group_type != IPV6_ADDR_ANY &&
1175 !ipv6_addr_equal(group, &ma->mca_addr))
1177 spin_lock_bh(&ma->mca_lock);
1178 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1179 /* gsquery <- gsquery && mark */
1181 ma->mca_flags &= ~MAF_GSQUERY;
1183 /* gsquery <- mark */
1185 ma->mca_flags |= MAF_GSQUERY;
1187 ma->mca_flags &= ~MAF_GSQUERY;
1189 if (ma->mca_flags & MAF_GSQUERY)
1190 mld_marksources(ma, ntohs(mlh2->nsrcs),
1192 igmp6_group_queried(ma, max_delay);
1193 spin_unlock_bh(&ma->mca_lock);
1194 if (group_type != IPV6_ADDR_ANY)
1198 read_unlock_bh(&idev->lock);
1205 int igmp6_event_report(struct sk_buff *skb)
1207 struct ifmcaddr6 *ma;
1208 struct in6_addr *addrp;
1209 struct inet6_dev *idev;
1210 struct icmp6hdr *hdr;
1213 /* Our own report looped back. Ignore it. */
1214 if (skb->pkt_type == PACKET_LOOPBACK)
1217 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1220 hdr = (struct icmp6hdr*) skb->h.raw;
1222 /* Drop reports with not link local source */
1223 addr_type = ipv6_addr_type(&skb->nh.ipv6h->saddr);
1224 if (addr_type != IPV6_ADDR_ANY &&
1225 !(addr_type&IPV6_ADDR_LINKLOCAL))
1228 addrp = (struct in6_addr *) (hdr + 1);
1230 idev = in6_dev_get(skb->dev);
1235 * Cancel the timer for this group
1238 read_lock_bh(&idev->lock);
1239 for (ma = idev->mc_list; ma; ma=ma->next) {
1240 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1241 spin_lock(&ma->mca_lock);
1242 if (del_timer(&ma->mca_timer))
1243 atomic_dec(&ma->mca_refcnt);
1244 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1245 spin_unlock(&ma->mca_lock);
1249 read_unlock_bh(&idev->lock);
1254 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1255 int gdeleted, int sdeleted)
1258 case MLD2_MODE_IS_INCLUDE:
1259 case MLD2_MODE_IS_EXCLUDE:
1260 if (gdeleted || sdeleted)
1262 return !((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp);
1263 case MLD2_CHANGE_TO_INCLUDE:
1264 if (gdeleted || sdeleted)
1266 return psf->sf_count[MCAST_INCLUDE] != 0;
1267 case MLD2_CHANGE_TO_EXCLUDE:
1268 if (gdeleted || sdeleted)
1270 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1271 psf->sf_count[MCAST_INCLUDE])
1273 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1274 psf->sf_count[MCAST_EXCLUDE];
1275 case MLD2_ALLOW_NEW_SOURCES:
1276 if (gdeleted || !psf->sf_crcount)
1278 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1279 case MLD2_BLOCK_OLD_SOURCES:
1280 if (pmc->mca_sfmode == MCAST_INCLUDE)
1281 return gdeleted || (psf->sf_crcount && sdeleted);
1282 return psf->sf_crcount && !gdeleted && !sdeleted;
1288 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1290 struct ip6_sf_list *psf;
1293 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1294 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1301 static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1303 struct sock *sk = igmp6_socket->sk;
1304 struct sk_buff *skb;
1305 struct mld2_report *pmr;
1306 struct in6_addr addr_buf;
1308 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1309 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1312 /* we assume size > sizeof(ra) here */
1313 skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
1318 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1320 if (ipv6_get_lladdr(dev, &addr_buf)) {
1321 /* <draft-ietf-magma-mld-source-05.txt>:
1322 * use unspecified address as the source address
1323 * when a valid link-local address is not available.
1325 memset(&addr_buf, 0, sizeof(addr_buf));
1328 ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);
1330 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1332 pmr =(struct mld2_report *)skb_put(skb, sizeof(*pmr));
1333 skb->h.raw = (unsigned char *)pmr;
1334 pmr->type = ICMPV6_MLD2_REPORT;
1342 static inline int mld_dev_queue_xmit2(struct sk_buff *skb)
1344 struct net_device *dev = skb->dev;
1346 if (dev->hard_header) {
1347 unsigned char ha[MAX_ADDR_LEN];
1350 ndisc_mc_map(&skb->nh.ipv6h->daddr, ha, dev, 1);
1351 err = dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, skb->len);
1357 return dev_queue_xmit(skb);
1360 static inline int mld_dev_queue_xmit(struct sk_buff *skb)
1362 return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dev,
1363 mld_dev_queue_xmit2);
1366 static void mld_sendpack(struct sk_buff *skb)
1368 struct ipv6hdr *pip6 = skb->nh.ipv6h;
1369 struct mld2_report *pmr = (struct mld2_report *)skb->h.raw;
1370 int payload_len, mldlen;
1371 struct inet6_dev *idev = in6_dev_get(skb->dev);
1374 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1375 payload_len = skb->tail - (unsigned char *)skb->nh.ipv6h -
1376 sizeof(struct ipv6hdr);
1377 mldlen = skb->tail - skb->h.raw;
1378 pip6->payload_len = htons(payload_len);
1380 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1381 IPPROTO_ICMPV6, csum_partial(skb->h.raw, mldlen, 0));
1382 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1383 mld_dev_queue_xmit);
1385 ICMP6_INC_STATS(idev,ICMP6_MIB_OUTMSGS);
1386 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1388 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1390 if (likely(idev != NULL))
1394 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1396 return sizeof(struct mld2_grec) + 4*mld_scount(pmc,type,gdel,sdel);
1399 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1400 int type, struct mld2_grec **ppgr)
1402 struct net_device *dev = pmc->idev->dev;
1403 struct mld2_report *pmr;
1404 struct mld2_grec *pgr;
1407 skb = mld_newpack(dev, dev->mtu);
1410 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1411 pgr->grec_type = type;
1412 pgr->grec_auxwords = 0;
1413 pgr->grec_nsrcs = 0;
1414 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1415 pmr = (struct mld2_report *)skb->h.raw;
1416 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1421 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1422 skb_tailroom(skb)) : 0)
1424 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1425 int type, int gdeleted, int sdeleted)
1427 struct net_device *dev = pmc->idev->dev;
1428 struct mld2_report *pmr;
1429 struct mld2_grec *pgr = NULL;
1430 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1431 int scount, first, isquery, truncate;
1433 if (pmc->mca_flags & MAF_NOREPORT)
1436 isquery = type == MLD2_MODE_IS_INCLUDE ||
1437 type == MLD2_MODE_IS_EXCLUDE;
1438 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1439 type == MLD2_CHANGE_TO_EXCLUDE;
1441 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1444 if (type == MLD2_ALLOW_NEW_SOURCES ||
1445 type == MLD2_BLOCK_OLD_SOURCES)
1447 if (pmc->mca_crcount || isquery) {
1448 /* make sure we have room for group header and at
1451 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)+
1452 sizeof(struct in6_addr)) {
1454 skb = NULL; /* add_grhead will get a new one */
1456 skb = add_grhead(skb, pmc, type, &pgr);
1460 pmr = skb ? (struct mld2_report *)skb->h.raw : NULL;
1462 /* EX and TO_EX get a fresh packet, if needed */
1464 if (pmr && pmr->ngrec &&
1465 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1468 skb = mld_newpack(dev, dev->mtu);
1474 for (psf=*psf_list; psf; psf=psf_next) {
1475 struct in6_addr *psrc;
1477 psf_next = psf->sf_next;
1479 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1484 /* clear marks on query responses */
1488 if (AVAILABLE(skb) < sizeof(*psrc) +
1489 first*sizeof(struct mld2_grec)) {
1490 if (truncate && !first)
1491 break; /* truncate these */
1493 pgr->grec_nsrcs = htons(scount);
1496 skb = mld_newpack(dev, dev->mtu);
1501 skb = add_grhead(skb, pmc, type, &pgr);
1504 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1505 *psrc = psf->sf_addr;
1507 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1508 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1510 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1512 psf_prev->sf_next = psf->sf_next;
1514 *psf_list = psf->sf_next;
1522 pgr->grec_nsrcs = htons(scount);
1525 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1529 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1531 struct sk_buff *skb = NULL;
1535 read_lock_bh(&idev->lock);
1536 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1537 if (pmc->mca_flags & MAF_NOREPORT)
1539 spin_lock_bh(&pmc->mca_lock);
1540 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1541 type = MLD2_MODE_IS_EXCLUDE;
1543 type = MLD2_MODE_IS_INCLUDE;
1544 skb = add_grec(skb, pmc, type, 0, 0);
1545 spin_unlock_bh(&pmc->mca_lock);
1547 read_unlock_bh(&idev->lock);
1549 spin_lock_bh(&pmc->mca_lock);
1550 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1551 type = MLD2_MODE_IS_EXCLUDE;
1553 type = MLD2_MODE_IS_INCLUDE;
1554 skb = add_grec(skb, pmc, type, 0, 0);
1555 spin_unlock_bh(&pmc->mca_lock);
1562 * remove zero-count source records from a source filter list
1564 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1566 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1569 for (psf=*ppsf; psf; psf = psf_next) {
1570 psf_next = psf->sf_next;
1571 if (psf->sf_crcount == 0) {
1573 psf_prev->sf_next = psf->sf_next;
1575 *ppsf = psf->sf_next;
1582 static void mld_send_cr(struct inet6_dev *idev)
1584 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1585 struct sk_buff *skb = NULL;
1588 read_lock_bh(&idev->lock);
1589 write_lock_bh(&idev->mc_lock);
1593 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1594 pmc_next = pmc->next;
1595 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1596 type = MLD2_BLOCK_OLD_SOURCES;
1597 dtype = MLD2_BLOCK_OLD_SOURCES;
1598 skb = add_grec(skb, pmc, type, 1, 0);
1599 skb = add_grec(skb, pmc, dtype, 1, 1);
1601 if (pmc->mca_crcount) {
1603 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1604 type = MLD2_CHANGE_TO_INCLUDE;
1605 skb = add_grec(skb, pmc, type, 1, 0);
1607 if (pmc->mca_crcount == 0) {
1608 mld_clear_zeros(&pmc->mca_tomb);
1609 mld_clear_zeros(&pmc->mca_sources);
1612 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1613 !pmc->mca_sources) {
1615 pmc_prev->next = pmc_next;
1617 idev->mc_tomb = pmc_next;
1618 in6_dev_put(pmc->idev);
1623 write_unlock_bh(&idev->mc_lock);
1626 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1627 spin_lock_bh(&pmc->mca_lock);
1628 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1629 type = MLD2_BLOCK_OLD_SOURCES;
1630 dtype = MLD2_ALLOW_NEW_SOURCES;
1632 type = MLD2_ALLOW_NEW_SOURCES;
1633 dtype = MLD2_BLOCK_OLD_SOURCES;
1635 skb = add_grec(skb, pmc, type, 0, 0);
1636 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1638 /* filter mode changes */
1639 if (pmc->mca_crcount) {
1641 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1642 type = MLD2_CHANGE_TO_EXCLUDE;
1644 type = MLD2_CHANGE_TO_INCLUDE;
1645 skb = add_grec(skb, pmc, type, 0, 0);
1647 spin_unlock_bh(&pmc->mca_lock);
1649 read_unlock_bh(&idev->lock);
1652 (void) mld_sendpack(skb);
1655 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1657 struct sock *sk = igmp6_socket->sk;
1658 struct inet6_dev *idev;
1659 struct sk_buff *skb;
1660 struct icmp6hdr *hdr;
1661 struct in6_addr *snd_addr;
1662 struct in6_addr *addrp;
1663 struct in6_addr addr_buf;
1664 struct in6_addr all_routers;
1665 int err, len, payload_len, full_len;
1666 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1667 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1670 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1672 if (type == ICMPV6_MGM_REDUCTION) {
1673 snd_addr = &all_routers;
1674 ipv6_addr_all_routers(&all_routers);
1677 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1678 payload_len = len + sizeof(ra);
1679 full_len = sizeof(struct ipv6hdr) + payload_len;
1681 skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1684 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1688 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1690 if (ipv6_get_lladdr(dev, &addr_buf)) {
1691 /* <draft-ietf-magma-mld-source-05.txt>:
1692 * use unspecified address as the source address
1693 * when a valid link-local address is not available.
1695 memset(&addr_buf, 0, sizeof(addr_buf));
1698 ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
1700 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1702 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1703 memset(hdr, 0, sizeof(struct icmp6hdr));
1704 hdr->icmp6_type = type;
1706 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1707 ipv6_addr_copy(addrp, addr);
1709 hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
1711 csum_partial((__u8 *) hdr, len, 0));
1713 idev = in6_dev_get(skb->dev);
1715 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1716 mld_dev_queue_xmit);
1718 if (type == ICMPV6_MGM_REDUCTION)
1719 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS);
1721 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBRESPONSES);
1722 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1723 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1725 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1727 if (likely(idev != NULL))
1732 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1733 struct in6_addr *psfsrc)
1735 struct ip6_sf_list *psf, *psf_prev;
1739 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1740 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1744 if (!psf || psf->sf_count[sfmode] == 0) {
1745 /* source filter not found, or count wrong => bug */
1748 psf->sf_count[sfmode]--;
1749 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1750 struct inet6_dev *idev = pmc->idev;
1752 /* no more filters for this source */
1754 psf_prev->sf_next = psf->sf_next;
1756 pmc->mca_sources = psf->sf_next;
1757 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1758 !MLD_V1_SEEN(idev)) {
1759 psf->sf_crcount = idev->mc_qrv;
1760 psf->sf_next = pmc->mca_tomb;
1761 pmc->mca_tomb = psf;
1769 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1770 int sfmode, int sfcount, struct in6_addr *psfsrc,
1773 struct ifmcaddr6 *pmc;
1779 read_lock_bh(&idev->lock);
1780 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1781 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1785 /* MCA not found?? bug */
1786 read_unlock_bh(&idev->lock);
1789 spin_lock_bh(&pmc->mca_lock);
1792 if (!pmc->mca_sfcount[sfmode]) {
1793 spin_unlock_bh(&pmc->mca_lock);
1794 read_unlock_bh(&idev->lock);
1797 pmc->mca_sfcount[sfmode]--;
1800 for (i=0; i<sfcount; i++) {
1801 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1803 changerec |= rv > 0;
1807 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1808 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1809 pmc->mca_sfcount[MCAST_INCLUDE]) {
1810 struct ip6_sf_list *psf;
1812 /* filter mode change */
1813 pmc->mca_sfmode = MCAST_INCLUDE;
1814 pmc->mca_crcount = idev->mc_qrv;
1815 idev->mc_ifc_count = pmc->mca_crcount;
1816 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1817 psf->sf_crcount = 0;
1818 mld_ifc_event(pmc->idev);
1819 } else if (sf_setstate(pmc) || changerec)
1820 mld_ifc_event(pmc->idev);
1821 spin_unlock_bh(&pmc->mca_lock);
1822 read_unlock_bh(&idev->lock);
1827 * Add multicast single-source filter to the interface list
1829 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1830 struct in6_addr *psfsrc, int delta)
1832 struct ip6_sf_list *psf, *psf_prev;
1835 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1836 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1841 psf = (struct ip6_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1844 memset(psf, 0, sizeof(*psf));
1845 psf->sf_addr = *psfsrc;
1847 psf_prev->sf_next = psf;
1849 pmc->mca_sources = psf;
1851 psf->sf_count[sfmode]++;
1855 static void sf_markstate(struct ifmcaddr6 *pmc)
1857 struct ip6_sf_list *psf;
1858 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1860 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1861 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1862 psf->sf_oldin = mca_xcount ==
1863 psf->sf_count[MCAST_EXCLUDE] &&
1864 !psf->sf_count[MCAST_INCLUDE];
1866 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1869 static int sf_setstate(struct ifmcaddr6 *pmc)
1871 struct ip6_sf_list *psf;
1872 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1873 int qrv = pmc->idev->mc_qrv;
1877 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1878 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1879 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1880 !psf->sf_count[MCAST_INCLUDE];
1882 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1883 if (new_in != psf->sf_oldin) {
1884 psf->sf_crcount = qrv;
1892 * Add multicast source filter list to the interface list
1894 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
1895 int sfmode, int sfcount, struct in6_addr *psfsrc,
1898 struct ifmcaddr6 *pmc;
1904 read_lock_bh(&idev->lock);
1905 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1906 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1910 /* MCA not found?? bug */
1911 read_unlock_bh(&idev->lock);
1914 spin_lock_bh(&pmc->mca_lock);
1917 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
1919 pmc->mca_sfcount[sfmode]++;
1921 for (i=0; i<sfcount; i++) {
1922 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1930 pmc->mca_sfcount[sfmode]--;
1932 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1933 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
1934 struct inet6_dev *idev = pmc->idev;
1935 struct ip6_sf_list *psf;
1937 /* filter mode change */
1938 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1939 pmc->mca_sfmode = MCAST_EXCLUDE;
1940 else if (pmc->mca_sfcount[MCAST_INCLUDE])
1941 pmc->mca_sfmode = MCAST_INCLUDE;
1942 /* else no filters; keep old mode for reports */
1944 pmc->mca_crcount = idev->mc_qrv;
1945 idev->mc_ifc_count = pmc->mca_crcount;
1946 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1947 psf->sf_crcount = 0;
1948 mld_ifc_event(idev);
1949 } else if (sf_setstate(pmc))
1950 mld_ifc_event(idev);
1951 spin_unlock_bh(&pmc->mca_lock);
1952 read_unlock_bh(&idev->lock);
1956 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
1958 struct ip6_sf_list *psf, *nextpsf;
1960 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
1961 nextpsf = psf->sf_next;
1964 pmc->mca_tomb = NULL;
1965 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
1966 nextpsf = psf->sf_next;
1969 pmc->mca_sources = NULL;
1970 pmc->mca_sfmode = MCAST_EXCLUDE;
1971 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
1972 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
1976 static void igmp6_join_group(struct ifmcaddr6 *ma)
1978 unsigned long delay;
1980 if (ma->mca_flags & MAF_NOREPORT)
1983 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
1985 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
1987 spin_lock_bh(&ma->mca_lock);
1988 if (del_timer(&ma->mca_timer)) {
1989 atomic_dec(&ma->mca_refcnt);
1990 delay = ma->mca_timer.expires - jiffies;
1993 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1994 atomic_inc(&ma->mca_refcnt);
1995 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
1996 spin_unlock_bh(&ma->mca_lock);
1999 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2000 struct inet6_dev *idev)
2004 if (iml->sflist == 0) {
2005 /* any-source empty exclude case */
2006 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2008 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2009 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2010 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2015 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2017 if (MLD_V1_SEEN(ma->idev)) {
2018 if (ma->mca_flags & MAF_LAST_REPORTER)
2019 igmp6_send(&ma->mca_addr, ma->idev->dev,
2020 ICMPV6_MGM_REDUCTION);
2022 mld_add_delrec(ma->idev, ma);
2023 mld_ifc_event(ma->idev);
2027 static void mld_gq_timer_expire(unsigned long data)
2029 struct inet6_dev *idev = (struct inet6_dev *)data;
2031 idev->mc_gq_running = 0;
2032 mld_send_report(idev, NULL);
2033 __in6_dev_put(idev);
2036 static void mld_ifc_timer_expire(unsigned long data)
2038 struct inet6_dev *idev = (struct inet6_dev *)data;
2041 if (idev->mc_ifc_count) {
2042 idev->mc_ifc_count--;
2043 if (idev->mc_ifc_count)
2044 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2046 __in6_dev_put(idev);
2049 static void mld_ifc_event(struct inet6_dev *idev)
2051 if (MLD_V1_SEEN(idev))
2053 idev->mc_ifc_count = idev->mc_qrv;
2054 mld_ifc_start_timer(idev, 1);
2058 static void igmp6_timer_handler(unsigned long data)
2060 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2062 if (MLD_V1_SEEN(ma->idev))
2063 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2065 mld_send_report(ma->idev, ma);
2067 spin_lock(&ma->mca_lock);
2068 ma->mca_flags |= MAF_LAST_REPORTER;
2069 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2070 spin_unlock(&ma->mca_lock);
2074 /* Device going down */
2076 void ipv6_mc_down(struct inet6_dev *idev)
2078 struct ifmcaddr6 *i;
2080 /* Withdraw multicast list */
2082 read_lock_bh(&idev->lock);
2083 idev->mc_ifc_count = 0;
2084 if (del_timer(&idev->mc_ifc_timer))
2085 __in6_dev_put(idev);
2086 idev->mc_gq_running = 0;
2087 if (del_timer(&idev->mc_gq_timer))
2088 __in6_dev_put(idev);
2090 for (i = idev->mc_list; i; i=i->next)
2091 igmp6_group_dropped(i);
2092 read_unlock_bh(&idev->lock);
2094 mld_clear_delrec(idev);
2098 /* Device going up */
2100 void ipv6_mc_up(struct inet6_dev *idev)
2102 struct ifmcaddr6 *i;
2104 /* Install multicast list, except for all-nodes (already installed) */
2106 read_lock_bh(&idev->lock);
2107 for (i = idev->mc_list; i; i=i->next)
2108 igmp6_group_added(i);
2109 read_unlock_bh(&idev->lock);
2112 /* IPv6 device initialization. */
2114 void ipv6_mc_init_dev(struct inet6_dev *idev)
2116 struct in6_addr maddr;
2118 write_lock_bh(&idev->lock);
2119 rwlock_init(&idev->mc_lock);
2120 idev->mc_gq_running = 0;
2121 init_timer(&idev->mc_gq_timer);
2122 idev->mc_gq_timer.data = (unsigned long) idev;
2123 idev->mc_gq_timer.function = &mld_gq_timer_expire;
2124 idev->mc_tomb = NULL;
2125 idev->mc_ifc_count = 0;
2126 init_timer(&idev->mc_ifc_timer);
2127 idev->mc_ifc_timer.data = (unsigned long) idev;
2128 idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
2129 idev->mc_qrv = MLD_QRV_DEFAULT;
2130 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2131 idev->mc_v1_seen = 0;
2132 write_unlock_bh(&idev->lock);
2134 /* Add all-nodes address. */
2135 ipv6_addr_all_nodes(&maddr);
2136 ipv6_dev_mc_inc(idev->dev, &maddr);
2140 * Device is about to be destroyed: clean up.
2143 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2145 struct ifmcaddr6 *i;
2146 struct in6_addr maddr;
2148 /* Deactivate timers */
2151 /* Delete all-nodes address. */
2152 ipv6_addr_all_nodes(&maddr);
2154 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2155 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2158 __ipv6_dev_mc_dec(idev, &maddr);
2160 if (idev->cnf.forwarding) {
2161 ipv6_addr_all_routers(&maddr);
2162 __ipv6_dev_mc_dec(idev, &maddr);
2165 write_lock_bh(&idev->lock);
2166 while ((i = idev->mc_list) != NULL) {
2167 idev->mc_list = i->next;
2168 write_unlock_bh(&idev->lock);
2170 igmp6_group_dropped(i);
2173 write_lock_bh(&idev->lock);
2175 write_unlock_bh(&idev->lock);
2178 #ifdef CONFIG_PROC_FS
2179 struct igmp6_mc_iter_state {
2180 struct net_device *dev;
2181 struct inet6_dev *idev;
2184 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2186 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2188 struct ifmcaddr6 *im = NULL;
2189 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2191 for (state->dev = dev_base, state->idev = NULL;
2193 state->dev = state->dev->next) {
2194 struct inet6_dev *idev;
2195 idev = in6_dev_get(state->dev);
2198 read_lock_bh(&idev->lock);
2204 read_unlock_bh(&idev->lock);
2210 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2212 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2216 if (likely(state->idev != NULL)) {
2217 read_unlock_bh(&state->idev->lock);
2218 in6_dev_put(state->idev);
2220 state->dev = state->dev->next;
2225 state->idev = in6_dev_get(state->dev);
2228 read_lock_bh(&state->idev->lock);
2229 im = state->idev->mc_list;
2234 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2236 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2238 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2240 return pos ? NULL : im;
2243 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2245 read_lock(&dev_base_lock);
2246 return igmp6_mc_get_idx(seq, *pos);
2249 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2251 struct ifmcaddr6 *im;
2252 im = igmp6_mc_get_next(seq, v);
2257 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2259 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2260 if (likely(state->idev != NULL)) {
2261 read_unlock_bh(&state->idev->lock);
2262 in6_dev_put(state->idev);
2266 read_unlock(&dev_base_lock);
2269 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2271 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2272 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2275 "%-4d %-15s %04x%04x%04x%04x%04x%04x%04x%04x %5d %08X %ld\n",
2276 state->dev->ifindex, state->dev->name,
2278 im->mca_users, im->mca_flags,
2279 (im->mca_flags&MAF_TIMER_RUNNING) ?
2280 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2284 static struct seq_operations igmp6_mc_seq_ops = {
2285 .start = igmp6_mc_seq_start,
2286 .next = igmp6_mc_seq_next,
2287 .stop = igmp6_mc_seq_stop,
2288 .show = igmp6_mc_seq_show,
2291 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2293 struct seq_file *seq;
2295 struct igmp6_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2300 rc = seq_open(file, &igmp6_mc_seq_ops);
2304 seq = file->private_data;
2306 memset(s, 0, sizeof(*s));
2314 static struct file_operations igmp6_mc_seq_fops = {
2315 .owner = THIS_MODULE,
2316 .open = igmp6_mc_seq_open,
2318 .llseek = seq_lseek,
2319 .release = seq_release_private,
2322 struct igmp6_mcf_iter_state {
2323 struct net_device *dev;
2324 struct inet6_dev *idev;
2325 struct ifmcaddr6 *im;
2328 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2330 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2332 struct ip6_sf_list *psf = NULL;
2333 struct ifmcaddr6 *im = NULL;
2334 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2336 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2338 state->dev = state->dev->next) {
2339 struct inet6_dev *idev;
2340 idev = in6_dev_get(state->dev);
2341 if (unlikely(idev == NULL))
2343 read_lock_bh(&idev->lock);
2345 if (likely(im != NULL)) {
2346 spin_lock_bh(&im->mca_lock);
2347 psf = im->mca_sources;
2348 if (likely(psf != NULL)) {
2353 spin_unlock_bh(&im->mca_lock);
2355 read_unlock_bh(&idev->lock);
2361 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2363 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2367 spin_unlock_bh(&state->im->mca_lock);
2368 state->im = state->im->next;
2369 while (!state->im) {
2370 if (likely(state->idev != NULL)) {
2371 read_unlock_bh(&state->idev->lock);
2372 in6_dev_put(state->idev);
2374 state->dev = state->dev->next;
2379 state->idev = in6_dev_get(state->dev);
2382 read_lock_bh(&state->idev->lock);
2383 state->im = state->idev->mc_list;
2387 spin_lock_bh(&state->im->mca_lock);
2388 psf = state->im->mca_sources;
2394 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2396 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2398 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2400 return pos ? NULL : psf;
2403 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2405 read_lock(&dev_base_lock);
2406 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2409 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2411 struct ip6_sf_list *psf;
2412 if (v == SEQ_START_TOKEN)
2413 psf = igmp6_mcf_get_first(seq);
2415 psf = igmp6_mcf_get_next(seq, v);
2420 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2422 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2423 if (likely(state->im != NULL)) {
2424 spin_unlock_bh(&state->im->mca_lock);
2427 if (likely(state->idev != NULL)) {
2428 read_unlock_bh(&state->idev->lock);
2429 in6_dev_put(state->idev);
2433 read_unlock(&dev_base_lock);
2436 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2438 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2439 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2441 if (v == SEQ_START_TOKEN) {
2444 "%32s %32s %6s %6s\n", "Idx",
2445 "Device", "Multicast Address",
2446 "Source Address", "INC", "EXC");
2450 "%04x%04x%04x%04x%04x%04x%04x%04x "
2451 "%04x%04x%04x%04x%04x%04x%04x%04x "
2453 state->dev->ifindex, state->dev->name,
2454 NIP6(state->im->mca_addr),
2456 psf->sf_count[MCAST_INCLUDE],
2457 psf->sf_count[MCAST_EXCLUDE]);
2462 static struct seq_operations igmp6_mcf_seq_ops = {
2463 .start = igmp6_mcf_seq_start,
2464 .next = igmp6_mcf_seq_next,
2465 .stop = igmp6_mcf_seq_stop,
2466 .show = igmp6_mcf_seq_show,
2469 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2471 struct seq_file *seq;
2473 struct igmp6_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2478 rc = seq_open(file, &igmp6_mcf_seq_ops);
2482 seq = file->private_data;
2484 memset(s, 0, sizeof(*s));
2492 static struct file_operations igmp6_mcf_seq_fops = {
2493 .owner = THIS_MODULE,
2494 .open = igmp6_mcf_seq_open,
2496 .llseek = seq_lseek,
2497 .release = seq_release_private,
2501 int __init igmp6_init(struct net_proto_family *ops)
2503 struct ipv6_pinfo *np;
2507 err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
2510 "Failed to initialize the IGMP6 control socket (err %d).\n",
2512 igmp6_socket = NULL; /* For safety. */
2516 sk = igmp6_socket->sk;
2517 sk->sk_allocation = GFP_ATOMIC;
2518 sk->sk_prot->unhash(sk);
2523 #ifdef CONFIG_PROC_FS
2524 proc_net_fops_create("igmp6", S_IRUGO, &igmp6_mc_seq_fops);
2525 proc_net_fops_create("mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
2531 void igmp6_cleanup(void)
2533 sock_release(igmp6_socket);
2534 igmp6_socket = NULL; /* for safety */
2536 #ifdef CONFIG_PROC_FS
2537 proc_net_remove("mcfilter6");
2538 proc_net_remove("igmp6");