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 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));
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 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1237 hdr = (struct icmp6hdr*) skb->h.raw;
1239 /* Drop reports with not link local source */
1240 addr_type = ipv6_addr_type(&skb->nh.ipv6h->saddr);
1241 if (addr_type != IPV6_ADDR_ANY &&
1242 !(addr_type&IPV6_ADDR_LINKLOCAL))
1245 addrp = (struct in6_addr *) (hdr + 1);
1247 idev = in6_dev_get(skb->dev);
1252 * Cancel the timer for this group
1255 read_lock_bh(&idev->lock);
1256 for (ma = idev->mc_list; ma; ma=ma->next) {
1257 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1258 spin_lock(&ma->mca_lock);
1259 if (del_timer(&ma->mca_timer))
1260 atomic_dec(&ma->mca_refcnt);
1261 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1262 spin_unlock(&ma->mca_lock);
1266 read_unlock_bh(&idev->lock);
1271 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1272 int gdeleted, int sdeleted)
1275 case MLD2_MODE_IS_INCLUDE:
1276 case MLD2_MODE_IS_EXCLUDE:
1277 if (gdeleted || sdeleted)
1279 return !((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp);
1280 case MLD2_CHANGE_TO_INCLUDE:
1281 if (gdeleted || sdeleted)
1283 return psf->sf_count[MCAST_INCLUDE] != 0;
1284 case MLD2_CHANGE_TO_EXCLUDE:
1285 if (gdeleted || sdeleted)
1287 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1288 psf->sf_count[MCAST_INCLUDE])
1290 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1291 psf->sf_count[MCAST_EXCLUDE];
1292 case MLD2_ALLOW_NEW_SOURCES:
1293 if (gdeleted || !psf->sf_crcount)
1295 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1296 case MLD2_BLOCK_OLD_SOURCES:
1297 if (pmc->mca_sfmode == MCAST_INCLUDE)
1298 return gdeleted || (psf->sf_crcount && sdeleted);
1299 return psf->sf_crcount && !gdeleted && !sdeleted;
1305 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1307 struct ip6_sf_list *psf;
1310 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1311 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1318 static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1320 struct sock *sk = igmp6_socket->sk;
1321 struct sk_buff *skb;
1322 struct mld2_report *pmr;
1323 struct in6_addr addr_buf;
1325 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1326 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1329 /* we assume size > sizeof(ra) here */
1330 skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
1335 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1337 if (ipv6_get_lladdr(dev, &addr_buf)) {
1338 /* <draft-ietf-magma-mld-source-05.txt>:
1339 * use unspecified address as the source address
1340 * when a valid link-local address is not available.
1342 memset(&addr_buf, 0, sizeof(addr_buf));
1345 ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);
1347 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1349 pmr =(struct mld2_report *)skb_put(skb, sizeof(*pmr));
1350 skb->h.raw = (unsigned char *)pmr;
1351 pmr->type = ICMPV6_MLD2_REPORT;
1359 static inline int mld_dev_queue_xmit2(struct sk_buff *skb)
1361 struct net_device *dev = skb->dev;
1363 if (dev->hard_header) {
1364 unsigned char ha[MAX_ADDR_LEN];
1367 ndisc_mc_map(&skb->nh.ipv6h->daddr, ha, dev, 1);
1368 err = dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, skb->len);
1374 return dev_queue_xmit(skb);
1377 static inline int mld_dev_queue_xmit(struct sk_buff *skb)
1379 return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dev,
1380 mld_dev_queue_xmit2);
1383 static void mld_sendpack(struct sk_buff *skb)
1385 struct ipv6hdr *pip6 = skb->nh.ipv6h;
1386 struct mld2_report *pmr = (struct mld2_report *)skb->h.raw;
1387 int payload_len, mldlen;
1388 struct inet6_dev *idev = in6_dev_get(skb->dev);
1391 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1392 payload_len = skb->tail - (unsigned char *)skb->nh.ipv6h -
1393 sizeof(struct ipv6hdr);
1394 mldlen = skb->tail - skb->h.raw;
1395 pip6->payload_len = htons(payload_len);
1397 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1398 IPPROTO_ICMPV6, csum_partial(skb->h.raw, mldlen, 0));
1399 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1400 mld_dev_queue_xmit);
1402 ICMP6_INC_STATS(idev,ICMP6_MIB_OUTMSGS);
1403 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1405 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1407 if (likely(idev != NULL))
1411 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1413 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1416 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1417 int type, struct mld2_grec **ppgr)
1419 struct net_device *dev = pmc->idev->dev;
1420 struct mld2_report *pmr;
1421 struct mld2_grec *pgr;
1424 skb = mld_newpack(dev, dev->mtu);
1427 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1428 pgr->grec_type = type;
1429 pgr->grec_auxwords = 0;
1430 pgr->grec_nsrcs = 0;
1431 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1432 pmr = (struct mld2_report *)skb->h.raw;
1433 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1438 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1439 skb_tailroom(skb)) : 0)
1441 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1442 int type, int gdeleted, int sdeleted)
1444 struct net_device *dev = pmc->idev->dev;
1445 struct mld2_report *pmr;
1446 struct mld2_grec *pgr = NULL;
1447 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1448 int scount, first, isquery, truncate;
1450 if (pmc->mca_flags & MAF_NOREPORT)
1453 isquery = type == MLD2_MODE_IS_INCLUDE ||
1454 type == MLD2_MODE_IS_EXCLUDE;
1455 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1456 type == MLD2_CHANGE_TO_EXCLUDE;
1458 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1461 if (type == MLD2_ALLOW_NEW_SOURCES ||
1462 type == MLD2_BLOCK_OLD_SOURCES)
1464 if (pmc->mca_crcount || isquery) {
1465 /* make sure we have room for group header and at
1468 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)+
1469 sizeof(struct in6_addr)) {
1471 skb = NULL; /* add_grhead will get a new one */
1473 skb = add_grhead(skb, pmc, type, &pgr);
1477 pmr = skb ? (struct mld2_report *)skb->h.raw : NULL;
1479 /* EX and TO_EX get a fresh packet, if needed */
1481 if (pmr && pmr->ngrec &&
1482 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1485 skb = mld_newpack(dev, dev->mtu);
1491 for (psf=*psf_list; psf; psf=psf_next) {
1492 struct in6_addr *psrc;
1494 psf_next = psf->sf_next;
1496 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1501 /* clear marks on query responses */
1505 if (AVAILABLE(skb) < sizeof(*psrc) +
1506 first*sizeof(struct mld2_grec)) {
1507 if (truncate && !first)
1508 break; /* truncate these */
1510 pgr->grec_nsrcs = htons(scount);
1513 skb = mld_newpack(dev, dev->mtu);
1518 skb = add_grhead(skb, pmc, type, &pgr);
1521 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1522 *psrc = psf->sf_addr;
1524 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1525 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1527 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1529 psf_prev->sf_next = psf->sf_next;
1531 *psf_list = psf->sf_next;
1539 pgr->grec_nsrcs = htons(scount);
1542 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1546 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1548 struct sk_buff *skb = NULL;
1552 read_lock_bh(&idev->lock);
1553 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1554 if (pmc->mca_flags & MAF_NOREPORT)
1556 spin_lock_bh(&pmc->mca_lock);
1557 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1558 type = MLD2_MODE_IS_EXCLUDE;
1560 type = MLD2_MODE_IS_INCLUDE;
1561 skb = add_grec(skb, pmc, type, 0, 0);
1562 spin_unlock_bh(&pmc->mca_lock);
1564 read_unlock_bh(&idev->lock);
1566 spin_lock_bh(&pmc->mca_lock);
1567 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1568 type = MLD2_MODE_IS_EXCLUDE;
1570 type = MLD2_MODE_IS_INCLUDE;
1571 skb = add_grec(skb, pmc, type, 0, 0);
1572 spin_unlock_bh(&pmc->mca_lock);
1579 * remove zero-count source records from a source filter list
1581 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1583 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1586 for (psf=*ppsf; psf; psf = psf_next) {
1587 psf_next = psf->sf_next;
1588 if (psf->sf_crcount == 0) {
1590 psf_prev->sf_next = psf->sf_next;
1592 *ppsf = psf->sf_next;
1599 static void mld_send_cr(struct inet6_dev *idev)
1601 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1602 struct sk_buff *skb = NULL;
1605 read_lock_bh(&idev->lock);
1606 write_lock_bh(&idev->mc_lock);
1610 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1611 pmc_next = pmc->next;
1612 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1613 type = MLD2_BLOCK_OLD_SOURCES;
1614 dtype = MLD2_BLOCK_OLD_SOURCES;
1615 skb = add_grec(skb, pmc, type, 1, 0);
1616 skb = add_grec(skb, pmc, dtype, 1, 1);
1618 if (pmc->mca_crcount) {
1620 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1621 type = MLD2_CHANGE_TO_INCLUDE;
1622 skb = add_grec(skb, pmc, type, 1, 0);
1624 if (pmc->mca_crcount == 0) {
1625 mld_clear_zeros(&pmc->mca_tomb);
1626 mld_clear_zeros(&pmc->mca_sources);
1629 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1630 !pmc->mca_sources) {
1632 pmc_prev->next = pmc_next;
1634 idev->mc_tomb = pmc_next;
1635 in6_dev_put(pmc->idev);
1640 write_unlock_bh(&idev->mc_lock);
1643 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1644 spin_lock_bh(&pmc->mca_lock);
1645 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1646 type = MLD2_BLOCK_OLD_SOURCES;
1647 dtype = MLD2_ALLOW_NEW_SOURCES;
1649 type = MLD2_ALLOW_NEW_SOURCES;
1650 dtype = MLD2_BLOCK_OLD_SOURCES;
1652 skb = add_grec(skb, pmc, type, 0, 0);
1653 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1655 /* filter mode changes */
1656 if (pmc->mca_crcount) {
1658 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1659 type = MLD2_CHANGE_TO_EXCLUDE;
1661 type = MLD2_CHANGE_TO_INCLUDE;
1662 skb = add_grec(skb, pmc, type, 0, 0);
1664 spin_unlock_bh(&pmc->mca_lock);
1666 read_unlock_bh(&idev->lock);
1669 (void) mld_sendpack(skb);
1672 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1674 struct sock *sk = igmp6_socket->sk;
1675 struct inet6_dev *idev;
1676 struct sk_buff *skb;
1677 struct icmp6hdr *hdr;
1678 struct in6_addr *snd_addr;
1679 struct in6_addr *addrp;
1680 struct in6_addr addr_buf;
1681 struct in6_addr all_routers;
1682 int err, len, payload_len, full_len;
1683 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1684 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1687 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1689 if (type == ICMPV6_MGM_REDUCTION) {
1690 snd_addr = &all_routers;
1691 ipv6_addr_all_routers(&all_routers);
1694 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1695 payload_len = len + sizeof(ra);
1696 full_len = sizeof(struct ipv6hdr) + payload_len;
1698 skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1701 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1705 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1707 if (ipv6_get_lladdr(dev, &addr_buf)) {
1708 /* <draft-ietf-magma-mld-source-05.txt>:
1709 * use unspecified address as the source address
1710 * when a valid link-local address is not available.
1712 memset(&addr_buf, 0, sizeof(addr_buf));
1715 ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
1717 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1719 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1720 memset(hdr, 0, sizeof(struct icmp6hdr));
1721 hdr->icmp6_type = type;
1723 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1724 ipv6_addr_copy(addrp, addr);
1726 hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
1728 csum_partial((__u8 *) hdr, len, 0));
1730 idev = in6_dev_get(skb->dev);
1732 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1733 mld_dev_queue_xmit);
1735 if (type == ICMPV6_MGM_REDUCTION)
1736 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS);
1738 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBRESPONSES);
1739 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1740 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1742 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1744 if (likely(idev != NULL))
1749 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1750 struct in6_addr *psfsrc)
1752 struct ip6_sf_list *psf, *psf_prev;
1756 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1757 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1761 if (!psf || psf->sf_count[sfmode] == 0) {
1762 /* source filter not found, or count wrong => bug */
1765 psf->sf_count[sfmode]--;
1766 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1767 struct inet6_dev *idev = pmc->idev;
1769 /* no more filters for this source */
1771 psf_prev->sf_next = psf->sf_next;
1773 pmc->mca_sources = psf->sf_next;
1774 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1775 !MLD_V1_SEEN(idev)) {
1776 psf->sf_crcount = idev->mc_qrv;
1777 psf->sf_next = pmc->mca_tomb;
1778 pmc->mca_tomb = psf;
1786 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1787 int sfmode, int sfcount, struct in6_addr *psfsrc,
1790 struct ifmcaddr6 *pmc;
1796 read_lock_bh(&idev->lock);
1797 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1798 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1802 /* MCA not found?? bug */
1803 read_unlock_bh(&idev->lock);
1806 spin_lock_bh(&pmc->mca_lock);
1809 if (!pmc->mca_sfcount[sfmode]) {
1810 spin_unlock_bh(&pmc->mca_lock);
1811 read_unlock_bh(&idev->lock);
1814 pmc->mca_sfcount[sfmode]--;
1817 for (i=0; i<sfcount; i++) {
1818 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1820 changerec |= rv > 0;
1824 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1825 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1826 pmc->mca_sfcount[MCAST_INCLUDE]) {
1827 struct ip6_sf_list *psf;
1829 /* filter mode change */
1830 pmc->mca_sfmode = MCAST_INCLUDE;
1831 pmc->mca_crcount = idev->mc_qrv;
1832 idev->mc_ifc_count = pmc->mca_crcount;
1833 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1834 psf->sf_crcount = 0;
1835 mld_ifc_event(pmc->idev);
1836 } else if (sf_setstate(pmc) || changerec)
1837 mld_ifc_event(pmc->idev);
1838 spin_unlock_bh(&pmc->mca_lock);
1839 read_unlock_bh(&idev->lock);
1844 * Add multicast single-source filter to the interface list
1846 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1847 struct in6_addr *psfsrc, int delta)
1849 struct ip6_sf_list *psf, *psf_prev;
1852 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1853 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1858 psf = (struct ip6_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1861 memset(psf, 0, sizeof(*psf));
1862 psf->sf_addr = *psfsrc;
1864 psf_prev->sf_next = psf;
1866 pmc->mca_sources = psf;
1868 psf->sf_count[sfmode]++;
1872 static void sf_markstate(struct ifmcaddr6 *pmc)
1874 struct ip6_sf_list *psf;
1875 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1877 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1878 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1879 psf->sf_oldin = mca_xcount ==
1880 psf->sf_count[MCAST_EXCLUDE] &&
1881 !psf->sf_count[MCAST_INCLUDE];
1883 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1886 static int sf_setstate(struct ifmcaddr6 *pmc)
1888 struct ip6_sf_list *psf;
1889 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1890 int qrv = pmc->idev->mc_qrv;
1894 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1895 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1896 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1897 !psf->sf_count[MCAST_INCLUDE];
1899 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1900 if (new_in != psf->sf_oldin) {
1901 psf->sf_crcount = qrv;
1909 * Add multicast source filter list to the interface list
1911 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
1912 int sfmode, int sfcount, struct in6_addr *psfsrc,
1915 struct ifmcaddr6 *pmc;
1921 read_lock_bh(&idev->lock);
1922 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1923 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1927 /* MCA not found?? bug */
1928 read_unlock_bh(&idev->lock);
1931 spin_lock_bh(&pmc->mca_lock);
1934 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
1936 pmc->mca_sfcount[sfmode]++;
1938 for (i=0; i<sfcount; i++) {
1939 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1947 pmc->mca_sfcount[sfmode]--;
1949 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1950 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
1951 struct inet6_dev *idev = pmc->idev;
1952 struct ip6_sf_list *psf;
1954 /* filter mode change */
1955 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1956 pmc->mca_sfmode = MCAST_EXCLUDE;
1957 else if (pmc->mca_sfcount[MCAST_INCLUDE])
1958 pmc->mca_sfmode = MCAST_INCLUDE;
1959 /* else no filters; keep old mode for reports */
1961 pmc->mca_crcount = idev->mc_qrv;
1962 idev->mc_ifc_count = pmc->mca_crcount;
1963 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1964 psf->sf_crcount = 0;
1965 mld_ifc_event(idev);
1966 } else if (sf_setstate(pmc))
1967 mld_ifc_event(idev);
1968 spin_unlock_bh(&pmc->mca_lock);
1969 read_unlock_bh(&idev->lock);
1973 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
1975 struct ip6_sf_list *psf, *nextpsf;
1977 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
1978 nextpsf = psf->sf_next;
1981 pmc->mca_tomb = NULL;
1982 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
1983 nextpsf = psf->sf_next;
1986 pmc->mca_sources = NULL;
1987 pmc->mca_sfmode = MCAST_EXCLUDE;
1988 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
1989 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
1993 static void igmp6_join_group(struct ifmcaddr6 *ma)
1995 unsigned long delay;
1997 if (ma->mca_flags & MAF_NOREPORT)
2000 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2002 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2004 spin_lock_bh(&ma->mca_lock);
2005 if (del_timer(&ma->mca_timer)) {
2006 atomic_dec(&ma->mca_refcnt);
2007 delay = ma->mca_timer.expires - jiffies;
2010 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2011 atomic_inc(&ma->mca_refcnt);
2012 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2013 spin_unlock_bh(&ma->mca_lock);
2016 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2017 struct inet6_dev *idev)
2021 if (iml->sflist == 0) {
2022 /* any-source empty exclude case */
2023 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2025 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2026 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2027 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2032 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2034 if (MLD_V1_SEEN(ma->idev)) {
2035 if (ma->mca_flags & MAF_LAST_REPORTER)
2036 igmp6_send(&ma->mca_addr, ma->idev->dev,
2037 ICMPV6_MGM_REDUCTION);
2039 mld_add_delrec(ma->idev, ma);
2040 mld_ifc_event(ma->idev);
2044 static void mld_gq_timer_expire(unsigned long data)
2046 struct inet6_dev *idev = (struct inet6_dev *)data;
2048 idev->mc_gq_running = 0;
2049 mld_send_report(idev, NULL);
2050 __in6_dev_put(idev);
2053 static void mld_ifc_timer_expire(unsigned long data)
2055 struct inet6_dev *idev = (struct inet6_dev *)data;
2058 if (idev->mc_ifc_count) {
2059 idev->mc_ifc_count--;
2060 if (idev->mc_ifc_count)
2061 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2063 __in6_dev_put(idev);
2066 static void mld_ifc_event(struct inet6_dev *idev)
2068 if (MLD_V1_SEEN(idev))
2070 idev->mc_ifc_count = idev->mc_qrv;
2071 mld_ifc_start_timer(idev, 1);
2075 static void igmp6_timer_handler(unsigned long data)
2077 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2079 if (MLD_V1_SEEN(ma->idev))
2080 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2082 mld_send_report(ma->idev, ma);
2084 spin_lock(&ma->mca_lock);
2085 ma->mca_flags |= MAF_LAST_REPORTER;
2086 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2087 spin_unlock(&ma->mca_lock);
2091 /* Device going down */
2093 void ipv6_mc_down(struct inet6_dev *idev)
2095 struct ifmcaddr6 *i;
2097 /* Withdraw multicast list */
2099 read_lock_bh(&idev->lock);
2100 idev->mc_ifc_count = 0;
2101 if (del_timer(&idev->mc_ifc_timer))
2102 __in6_dev_put(idev);
2103 idev->mc_gq_running = 0;
2104 if (del_timer(&idev->mc_gq_timer))
2105 __in6_dev_put(idev);
2107 for (i = idev->mc_list; i; i=i->next)
2108 igmp6_group_dropped(i);
2109 read_unlock_bh(&idev->lock);
2111 mld_clear_delrec(idev);
2115 /* Device going up */
2117 void ipv6_mc_up(struct inet6_dev *idev)
2119 struct ifmcaddr6 *i;
2121 /* Install multicast list, except for all-nodes (already installed) */
2123 read_lock_bh(&idev->lock);
2124 for (i = idev->mc_list; i; i=i->next)
2125 igmp6_group_added(i);
2126 read_unlock_bh(&idev->lock);
2129 /* IPv6 device initialization. */
2131 void ipv6_mc_init_dev(struct inet6_dev *idev)
2133 struct in6_addr maddr;
2135 write_lock_bh(&idev->lock);
2136 rwlock_init(&idev->mc_lock);
2137 idev->mc_gq_running = 0;
2138 init_timer(&idev->mc_gq_timer);
2139 idev->mc_gq_timer.data = (unsigned long) idev;
2140 idev->mc_gq_timer.function = &mld_gq_timer_expire;
2141 idev->mc_tomb = NULL;
2142 idev->mc_ifc_count = 0;
2143 init_timer(&idev->mc_ifc_timer);
2144 idev->mc_ifc_timer.data = (unsigned long) idev;
2145 idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
2146 idev->mc_qrv = MLD_QRV_DEFAULT;
2147 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2148 idev->mc_v1_seen = 0;
2149 write_unlock_bh(&idev->lock);
2151 /* Add all-nodes address. */
2152 ipv6_addr_all_nodes(&maddr);
2153 ipv6_dev_mc_inc(idev->dev, &maddr);
2157 * Device is about to be destroyed: clean up.
2160 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2162 struct ifmcaddr6 *i;
2163 struct in6_addr maddr;
2165 /* Deactivate timers */
2168 /* Delete all-nodes address. */
2169 ipv6_addr_all_nodes(&maddr);
2171 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2172 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2175 __ipv6_dev_mc_dec(idev, &maddr);
2177 if (idev->cnf.forwarding) {
2178 ipv6_addr_all_routers(&maddr);
2179 __ipv6_dev_mc_dec(idev, &maddr);
2182 write_lock_bh(&idev->lock);
2183 while ((i = idev->mc_list) != NULL) {
2184 idev->mc_list = i->next;
2185 write_unlock_bh(&idev->lock);
2187 igmp6_group_dropped(i);
2190 write_lock_bh(&idev->lock);
2192 write_unlock_bh(&idev->lock);
2195 #ifdef CONFIG_PROC_FS
2196 struct igmp6_mc_iter_state {
2197 struct net_device *dev;
2198 struct inet6_dev *idev;
2201 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2203 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2205 struct ifmcaddr6 *im = NULL;
2206 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2208 for (state->dev = dev_base, state->idev = NULL;
2210 state->dev = state->dev->next) {
2211 struct inet6_dev *idev;
2212 idev = in6_dev_get(state->dev);
2215 read_lock_bh(&idev->lock);
2221 read_unlock_bh(&idev->lock);
2227 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2229 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2233 if (likely(state->idev != NULL)) {
2234 read_unlock_bh(&state->idev->lock);
2235 in6_dev_put(state->idev);
2237 state->dev = state->dev->next;
2242 state->idev = in6_dev_get(state->dev);
2245 read_lock_bh(&state->idev->lock);
2246 im = state->idev->mc_list;
2251 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2253 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2255 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2257 return pos ? NULL : im;
2260 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2262 read_lock(&dev_base_lock);
2263 return igmp6_mc_get_idx(seq, *pos);
2266 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2268 struct ifmcaddr6 *im;
2269 im = igmp6_mc_get_next(seq, v);
2274 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2276 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2277 if (likely(state->idev != NULL)) {
2278 read_unlock_bh(&state->idev->lock);
2279 in6_dev_put(state->idev);
2283 read_unlock(&dev_base_lock);
2286 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2288 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2289 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2292 "%-4d %-15s %04x%04x%04x%04x%04x%04x%04x%04x %5d %08X %ld\n",
2293 state->dev->ifindex, state->dev->name,
2295 im->mca_users, im->mca_flags,
2296 (im->mca_flags&MAF_TIMER_RUNNING) ?
2297 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2301 static struct seq_operations igmp6_mc_seq_ops = {
2302 .start = igmp6_mc_seq_start,
2303 .next = igmp6_mc_seq_next,
2304 .stop = igmp6_mc_seq_stop,
2305 .show = igmp6_mc_seq_show,
2308 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2310 struct seq_file *seq;
2312 struct igmp6_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2317 rc = seq_open(file, &igmp6_mc_seq_ops);
2321 seq = file->private_data;
2323 memset(s, 0, sizeof(*s));
2331 static struct file_operations igmp6_mc_seq_fops = {
2332 .owner = THIS_MODULE,
2333 .open = igmp6_mc_seq_open,
2335 .llseek = seq_lseek,
2336 .release = seq_release_private,
2339 struct igmp6_mcf_iter_state {
2340 struct net_device *dev;
2341 struct inet6_dev *idev;
2342 struct ifmcaddr6 *im;
2345 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2347 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2349 struct ip6_sf_list *psf = NULL;
2350 struct ifmcaddr6 *im = NULL;
2351 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2353 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2355 state->dev = state->dev->next) {
2356 struct inet6_dev *idev;
2357 idev = in6_dev_get(state->dev);
2358 if (unlikely(idev == NULL))
2360 read_lock_bh(&idev->lock);
2362 if (likely(im != NULL)) {
2363 spin_lock_bh(&im->mca_lock);
2364 psf = im->mca_sources;
2365 if (likely(psf != NULL)) {
2370 spin_unlock_bh(&im->mca_lock);
2372 read_unlock_bh(&idev->lock);
2378 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2380 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2384 spin_unlock_bh(&state->im->mca_lock);
2385 state->im = state->im->next;
2386 while (!state->im) {
2387 if (likely(state->idev != NULL)) {
2388 read_unlock_bh(&state->idev->lock);
2389 in6_dev_put(state->idev);
2391 state->dev = state->dev->next;
2396 state->idev = in6_dev_get(state->dev);
2399 read_lock_bh(&state->idev->lock);
2400 state->im = state->idev->mc_list;
2404 spin_lock_bh(&state->im->mca_lock);
2405 psf = state->im->mca_sources;
2411 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2413 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2415 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2417 return pos ? NULL : psf;
2420 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2422 read_lock(&dev_base_lock);
2423 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2426 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2428 struct ip6_sf_list *psf;
2429 if (v == SEQ_START_TOKEN)
2430 psf = igmp6_mcf_get_first(seq);
2432 psf = igmp6_mcf_get_next(seq, v);
2437 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2439 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2440 if (likely(state->im != NULL)) {
2441 spin_unlock_bh(&state->im->mca_lock);
2444 if (likely(state->idev != NULL)) {
2445 read_unlock_bh(&state->idev->lock);
2446 in6_dev_put(state->idev);
2450 read_unlock(&dev_base_lock);
2453 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2455 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2456 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2458 if (v == SEQ_START_TOKEN) {
2461 "%32s %32s %6s %6s\n", "Idx",
2462 "Device", "Multicast Address",
2463 "Source Address", "INC", "EXC");
2467 "%04x%04x%04x%04x%04x%04x%04x%04x "
2468 "%04x%04x%04x%04x%04x%04x%04x%04x "
2470 state->dev->ifindex, state->dev->name,
2471 NIP6(state->im->mca_addr),
2473 psf->sf_count[MCAST_INCLUDE],
2474 psf->sf_count[MCAST_EXCLUDE]);
2479 static struct seq_operations igmp6_mcf_seq_ops = {
2480 .start = igmp6_mcf_seq_start,
2481 .next = igmp6_mcf_seq_next,
2482 .stop = igmp6_mcf_seq_stop,
2483 .show = igmp6_mcf_seq_show,
2486 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2488 struct seq_file *seq;
2490 struct igmp6_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2495 rc = seq_open(file, &igmp6_mcf_seq_ops);
2499 seq = file->private_data;
2501 memset(s, 0, sizeof(*s));
2509 static struct file_operations igmp6_mcf_seq_fops = {
2510 .owner = THIS_MODULE,
2511 .open = igmp6_mcf_seq_open,
2513 .llseek = seq_lseek,
2514 .release = seq_release_private,
2518 int __init igmp6_init(struct net_proto_family *ops)
2520 struct ipv6_pinfo *np;
2524 err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
2527 "Failed to initialize the IGMP6 control socket (err %d).\n",
2529 igmp6_socket = NULL; /* For safety. */
2533 sk = igmp6_socket->sk;
2534 sk->sk_allocation = GFP_ATOMIC;
2535 sk->sk_prot->unhash(sk);
2540 #ifdef CONFIG_PROC_FS
2541 proc_net_fops_create("igmp6", S_IRUGO, &igmp6_mc_seq_fops);
2542 proc_net_fops_create("mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
2548 void igmp6_cleanup(void)
2550 sock_release(igmp6_socket);
2551 igmp6_socket = NULL; /* for safety */
2553 #ifdef CONFIG_PROC_FS
2554 proc_net_remove("mcfilter6");
2555 proc_net_remove("igmp6");