2 * Multicast support for IPv6
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: mcast.c,v 1.40 2002/02/08 03:57:19 davem Exp $
10 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
20 * yoshfuji : fix format of router-alert option
21 * YOSHIFUJI Hideaki @USAGI:
22 * Fixed source address for MLD message based on
23 * <draft-ietf-magma-mld-source-05.txt>.
24 * YOSHIFUJI Hideaki @USAGI:
25 * - Ignore Queries for invalid addresses.
26 * - MLD for link-local addresses.
27 * David L Stevens <dlstevens@us.ibm.com>:
31 #include <linux/module.h>
32 #include <linux/errno.h>
33 #include <linux/types.h>
34 #include <linux/string.h>
35 #include <linux/socket.h>
36 #include <linux/sockios.h>
37 #include <linux/jiffies.h>
38 #include <linux/times.h>
39 #include <linux/net.h>
41 #include <linux/in6.h>
42 #include <linux/netdevice.h>
43 #include <linux/if_arp.h>
44 #include <linux/route.h>
45 #include <linux/init.h>
46 #include <linux/proc_fs.h>
47 #include <linux/seq_file.h>
49 #include <linux/netfilter.h>
50 #include <linux/netfilter_ipv6.h>
56 #include <net/protocol.h>
57 #include <net/if_inet6.h>
58 #include <net/ndisc.h>
59 #include <net/addrconf.h>
60 #include <net/ip6_route.h>
62 #include <net/ip6_checksum.h>
64 /* Set to 3 to get tracing... */
68 #define MDBG(x) printk x
74 * These header formats should be in a separate include file, but icmpv6.h
75 * doesn't have in6_addr defined in all cases, there is no __u128, and no
76 * other files reference these.
81 /* Multicast Listener Discovery version 2 headers */
87 struct in6_addr grec_mca;
88 struct in6_addr grec_src[0];
97 struct mld2_grec grec[0];
107 #if defined(__LITTLE_ENDIAN_BITFIELD)
111 #elif defined(__BIG_ENDIAN_BITFIELD)
116 #error "Please fix <asm/byteorder.h>"
120 struct in6_addr srcs[0];
123 static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
125 /* Big mc list lock for all the sockets */
126 static DEFINE_RWLOCK(ipv6_sk_mc_lock);
128 static struct socket *igmp6_socket;
130 int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr);
132 static void igmp6_join_group(struct ifmcaddr6 *ma);
133 static void igmp6_leave_group(struct ifmcaddr6 *ma);
134 static void igmp6_timer_handler(unsigned long data);
136 static void mld_gq_timer_expire(unsigned long data);
137 static void mld_ifc_timer_expire(unsigned long data);
138 static void mld_ifc_event(struct inet6_dev *idev);
139 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
140 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
141 static void mld_clear_delrec(struct inet6_dev *idev);
142 static int sf_setstate(struct ifmcaddr6 *pmc);
143 static void sf_markstate(struct ifmcaddr6 *pmc);
144 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
145 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
146 int sfmode, int sfcount, struct in6_addr *psfsrc,
148 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
149 int sfmode, int sfcount, struct in6_addr *psfsrc,
151 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
152 struct inet6_dev *idev);
155 #define IGMP6_UNSOLICITED_IVAL (10*HZ)
156 #define MLD_QRV_DEFAULT 2
158 #define MLD_V1_SEEN(idev) (ipv6_devconf.force_mld_version == 1 || \
159 (idev)->cnf.force_mld_version == 1 || \
160 ((idev)->mc_v1_seen && \
161 time_before(jiffies, (idev)->mc_v1_seen)))
163 #define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
164 #define MLDV2_EXP(thresh, nbmant, nbexp, value) \
165 ((value) < (thresh) ? (value) : \
166 ((MLDV2_MASK(value, nbmant) | (1<<(nbmant))) << \
167 (MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp))))
169 #define MLDV2_QQIC(value) MLDV2_EXP(0x80, 4, 3, value)
170 #define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
172 #define IPV6_MLD_MAX_MSF 64
174 int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
177 * socket join on multicast group
180 int ipv6_sock_mc_join(struct sock *sk, int ifindex, struct in6_addr *addr)
182 struct net_device *dev = NULL;
183 struct ipv6_mc_socklist *mc_lst;
184 struct ipv6_pinfo *np = inet6_sk(sk);
187 if (!ipv6_addr_is_multicast(addr))
190 read_lock_bh(&ipv6_sk_mc_lock);
191 for (mc_lst=np->ipv6_mc_list; mc_lst; mc_lst=mc_lst->next) {
192 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
193 ipv6_addr_equal(&mc_lst->addr, addr)) {
194 read_unlock_bh(&ipv6_sk_mc_lock);
198 read_unlock_bh(&ipv6_sk_mc_lock);
200 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
206 ipv6_addr_copy(&mc_lst->addr, addr);
210 rt = rt6_lookup(addr, NULL, 0, 0);
214 dst_release(&rt->u.dst);
217 dev = dev_get_by_index(ifindex);
220 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
224 mc_lst->ifindex = dev->ifindex;
225 mc_lst->sfmode = MCAST_EXCLUDE;
226 rwlock_init(&mc_lst->sflock);
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);
271 (void) ip6_mc_leave_src(sk, mc_lst, idev);
273 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
278 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
279 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
283 write_unlock_bh(&ipv6_sk_mc_lock);
285 return -EADDRNOTAVAIL;
288 static struct inet6_dev *ip6_mc_find_dev(struct in6_addr *group, int ifindex)
290 struct net_device *dev = NULL;
291 struct inet6_dev *idev = NULL;
296 rt = rt6_lookup(group, NULL, 0, 0);
300 dst_release(&rt->u.dst);
303 dev = dev_get_by_index(ifindex);
307 idev = in6_dev_get(dev);
312 read_lock_bh(&idev->lock);
314 read_unlock_bh(&idev->lock);
322 void ipv6_sock_mc_close(struct sock *sk)
324 struct ipv6_pinfo *np = inet6_sk(sk);
325 struct ipv6_mc_socklist *mc_lst;
327 write_lock_bh(&ipv6_sk_mc_lock);
328 while ((mc_lst = np->ipv6_mc_list) != NULL) {
329 struct net_device *dev;
331 np->ipv6_mc_list = mc_lst->next;
332 write_unlock_bh(&ipv6_sk_mc_lock);
334 dev = dev_get_by_index(mc_lst->ifindex);
336 struct inet6_dev *idev = in6_dev_get(dev);
338 (void) ip6_mc_leave_src(sk, mc_lst, idev);
340 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
345 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
347 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
349 write_lock_bh(&ipv6_sk_mc_lock);
351 write_unlock_bh(&ipv6_sk_mc_lock);
354 int ip6_mc_source(int add, int omode, struct sock *sk,
355 struct group_source_req *pgsr)
357 struct in6_addr *source, *group;
358 struct ipv6_mc_socklist *pmc;
359 struct net_device *dev;
360 struct inet6_dev *idev;
361 struct ipv6_pinfo *inet6 = inet6_sk(sk);
362 struct ip6_sf_socklist *psl;
368 if (pgsr->gsr_group.ss_family != AF_INET6 ||
369 pgsr->gsr_source.ss_family != AF_INET6)
372 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
373 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
375 if (!ipv6_addr_is_multicast(group))
378 idev = ip6_mc_find_dev(group, pgsr->gsr_interface);
383 err = -EADDRNOTAVAIL;
385 read_lock_bh(&ipv6_sk_mc_lock);
386 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
387 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
389 if (ipv6_addr_equal(&pmc->addr, group))
392 if (!pmc) { /* must have a prior join */
396 /* if a source filter was set, must be the same mode as before */
398 if (pmc->sfmode != omode) {
402 } else if (pmc->sfmode != omode) {
403 /* allow mode switches for empty-set filters */
404 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
405 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
409 write_lock_bh(&pmc->sflock);
415 goto done; /* err = -EADDRNOTAVAIL */
417 for (i=0; i<psl->sl_count; i++) {
418 rv = memcmp(&psl->sl_addr[i], source,
419 sizeof(struct in6_addr));
423 if (rv) /* source not found */
424 goto done; /* err = -EADDRNOTAVAIL */
426 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
427 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
432 /* update the interface filter */
433 ip6_mc_del_src(idev, group, omode, 1, source, 1);
435 for (j=i+1; j<psl->sl_count; j++)
436 psl->sl_addr[j-1] = psl->sl_addr[j];
441 /* else, add a new source to the filter */
443 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
447 if (!psl || psl->sl_count == psl->sl_max) {
448 struct ip6_sf_socklist *newpsl;
449 int count = IP6_SFBLOCK;
452 count += psl->sl_max;
453 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
458 newpsl->sl_max = count;
459 newpsl->sl_count = count - IP6_SFBLOCK;
461 for (i=0; i<psl->sl_count; i++)
462 newpsl->sl_addr[i] = psl->sl_addr[i];
463 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
465 pmc->sflist = psl = newpsl;
467 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
468 for (i=0; i<psl->sl_count; i++) {
469 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
473 if (rv == 0) /* address already there is an error */
475 for (j=psl->sl_count-1; j>=i; j--)
476 psl->sl_addr[j+1] = psl->sl_addr[j];
477 psl->sl_addr[i] = *source;
480 /* update the interface list */
481 ip6_mc_add_src(idev, group, omode, 1, source, 1);
484 write_unlock_bh(&pmc->sflock);
485 read_unlock_bh(&ipv6_sk_mc_lock);
486 read_unlock_bh(&idev->lock);
490 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
494 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
496 struct in6_addr *group;
497 struct ipv6_mc_socklist *pmc;
498 struct net_device *dev;
499 struct inet6_dev *idev;
500 struct ipv6_pinfo *inet6 = inet6_sk(sk);
501 struct ip6_sf_socklist *newpsl, *psl;
505 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
507 if (!ipv6_addr_is_multicast(group))
509 if (gsf->gf_fmode != MCAST_INCLUDE &&
510 gsf->gf_fmode != MCAST_EXCLUDE)
513 idev = ip6_mc_find_dev(group, gsf->gf_interface);
520 read_lock_bh(&ipv6_sk_mc_lock);
522 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
527 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
528 if (pmc->ifindex != gsf->gf_interface)
530 if (ipv6_addr_equal(&pmc->addr, group))
533 if (!pmc) { /* must have a prior join */
537 if (gsf->gf_numsrc) {
538 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
544 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
545 for (i=0; i<newpsl->sl_count; ++i) {
546 struct sockaddr_in6 *psin6;
548 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
549 newpsl->sl_addr[i] = psin6->sin6_addr;
551 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
552 newpsl->sl_count, newpsl->sl_addr, 0);
554 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
559 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
562 write_lock_bh(&pmc->sflock);
565 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
566 psl->sl_count, psl->sl_addr, 0);
567 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
569 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
570 pmc->sflist = newpsl;
571 pmc->sfmode = gsf->gf_fmode;
572 write_unlock_bh(&pmc->sflock);
575 read_unlock_bh(&ipv6_sk_mc_lock);
576 read_unlock_bh(&idev->lock);
580 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
584 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
585 struct group_filter __user *optval, int __user *optlen)
587 int err, i, count, copycount;
588 struct in6_addr *group;
589 struct ipv6_mc_socklist *pmc;
590 struct inet6_dev *idev;
591 struct net_device *dev;
592 struct ipv6_pinfo *inet6 = inet6_sk(sk);
593 struct ip6_sf_socklist *psl;
595 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
597 if (!ipv6_addr_is_multicast(group))
600 idev = ip6_mc_find_dev(group, gsf->gf_interface);
607 err = -EADDRNOTAVAIL;
609 * changes to the ipv6_mc_list require the socket lock and
610 * a read lock on ip6_sk_mc_lock. We have the socket lock,
611 * so reading the list is safe.
614 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
615 if (pmc->ifindex != gsf->gf_interface)
617 if (ipv6_addr_equal(group, &pmc->addr))
620 if (!pmc) /* must have a prior join */
622 gsf->gf_fmode = pmc->sfmode;
624 count = psl ? psl->sl_count : 0;
625 read_unlock_bh(&idev->lock);
629 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
630 gsf->gf_numsrc = count;
631 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
632 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
635 /* changes to psl require the socket lock, a read lock on
636 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
637 * have the socket lock, so reading here is safe.
639 for (i=0; i<copycount; i++) {
640 struct sockaddr_in6 *psin6;
641 struct sockaddr_storage ss;
643 psin6 = (struct sockaddr_in6 *)&ss;
644 memset(&ss, 0, sizeof(ss));
645 psin6->sin6_family = AF_INET6;
646 psin6->sin6_addr = psl->sl_addr[i];
647 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
652 read_unlock_bh(&idev->lock);
658 int inet6_mc_check(struct sock *sk, struct in6_addr *mc_addr,
659 struct in6_addr *src_addr)
661 struct ipv6_pinfo *np = inet6_sk(sk);
662 struct ipv6_mc_socklist *mc;
663 struct ip6_sf_socklist *psl;
666 read_lock(&ipv6_sk_mc_lock);
667 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
668 if (ipv6_addr_equal(&mc->addr, mc_addr))
672 read_unlock(&ipv6_sk_mc_lock);
675 read_lock(&mc->sflock);
678 rv = mc->sfmode == MCAST_EXCLUDE;
682 for (i=0; i<psl->sl_count; i++) {
683 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
686 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
688 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
691 read_unlock(&mc->sflock);
692 read_unlock(&ipv6_sk_mc_lock);
697 static void ma_put(struct ifmcaddr6 *mc)
699 if (atomic_dec_and_test(&mc->mca_refcnt)) {
700 in6_dev_put(mc->idev);
705 static void igmp6_group_added(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_add(dev, buf, dev->addr_len, 0);
716 spin_unlock_bh(&mc->mca_lock);
718 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
721 if (MLD_V1_SEEN(mc->idev)) {
722 igmp6_join_group(mc);
727 mc->mca_crcount = mc->idev->mc_qrv;
728 mld_ifc_event(mc->idev);
731 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
733 struct net_device *dev = mc->idev->dev;
734 char buf[MAX_ADDR_LEN];
736 spin_lock_bh(&mc->mca_lock);
737 if (mc->mca_flags&MAF_LOADED) {
738 mc->mca_flags &= ~MAF_LOADED;
739 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
740 dev_mc_delete(dev, buf, dev->addr_len, 0);
743 if (mc->mca_flags & MAF_NOREPORT)
745 spin_unlock_bh(&mc->mca_lock);
748 igmp6_leave_group(mc);
750 spin_lock_bh(&mc->mca_lock);
751 if (del_timer(&mc->mca_timer))
752 atomic_dec(&mc->mca_refcnt);
754 ip6_mc_clear_src(mc);
755 spin_unlock_bh(&mc->mca_lock);
759 * deleted ifmcaddr6 manipulation
761 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
763 struct ifmcaddr6 *pmc;
765 /* this is an "ifmcaddr6" for convenience; only the fields below
766 * are actually used. In particular, the refcnt and users are not
767 * used for management of the delete list. Using the same structure
768 * for deleted items allows change reports to use common code with
769 * non-deleted or query-response MCA's.
771 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
775 spin_lock_bh(&im->mca_lock);
776 spin_lock_init(&pmc->mca_lock);
777 pmc->idev = im->idev;
779 pmc->mca_addr = im->mca_addr;
780 pmc->mca_crcount = idev->mc_qrv;
781 pmc->mca_sfmode = im->mca_sfmode;
782 if (pmc->mca_sfmode == MCAST_INCLUDE) {
783 struct ip6_sf_list *psf;
785 pmc->mca_tomb = im->mca_tomb;
786 pmc->mca_sources = im->mca_sources;
787 im->mca_tomb = im->mca_sources = NULL;
788 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
789 psf->sf_crcount = pmc->mca_crcount;
791 spin_unlock_bh(&im->mca_lock);
793 write_lock_bh(&idev->mc_lock);
794 pmc->next = idev->mc_tomb;
796 write_unlock_bh(&idev->mc_lock);
799 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
801 struct ifmcaddr6 *pmc, *pmc_prev;
802 struct ip6_sf_list *psf, *psf_next;
804 write_lock_bh(&idev->mc_lock);
806 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
807 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
813 pmc_prev->next = pmc->next;
815 idev->mc_tomb = pmc->next;
817 write_unlock_bh(&idev->mc_lock);
819 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
820 psf_next = psf->sf_next;
823 in6_dev_put(pmc->idev);
828 static void mld_clear_delrec(struct inet6_dev *idev)
830 struct ifmcaddr6 *pmc, *nextpmc;
832 write_lock_bh(&idev->mc_lock);
834 idev->mc_tomb = NULL;
835 write_unlock_bh(&idev->mc_lock);
837 for (; pmc; pmc = nextpmc) {
839 ip6_mc_clear_src(pmc);
840 in6_dev_put(pmc->idev);
844 /* clear dead sources, too */
845 read_lock_bh(&idev->lock);
846 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
847 struct ip6_sf_list *psf, *psf_next;
849 spin_lock_bh(&pmc->mca_lock);
851 pmc->mca_tomb = NULL;
852 spin_unlock_bh(&pmc->mca_lock);
853 for (; psf; psf=psf_next) {
854 psf_next = psf->sf_next;
858 read_unlock_bh(&idev->lock);
863 * device multicast group inc (add if not found)
865 int ipv6_dev_mc_inc(struct net_device *dev, struct in6_addr *addr)
867 struct ifmcaddr6 *mc;
868 struct inet6_dev *idev;
870 idev = in6_dev_get(dev);
875 write_lock_bh(&idev->lock);
877 write_unlock_bh(&idev->lock);
882 for (mc = idev->mc_list; mc; mc = mc->next) {
883 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
885 write_unlock_bh(&idev->lock);
886 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
894 * not found: create a new one.
897 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
900 write_unlock_bh(&idev->lock);
905 init_timer(&mc->mca_timer);
906 mc->mca_timer.function = igmp6_timer_handler;
907 mc->mca_timer.data = (unsigned long) mc;
909 ipv6_addr_copy(&mc->mca_addr, addr);
912 /* mca_stamp should be updated upon changes */
913 mc->mca_cstamp = mc->mca_tstamp = jiffies;
914 atomic_set(&mc->mca_refcnt, 2);
915 spin_lock_init(&mc->mca_lock);
917 /* initial mode is (EX, empty) */
918 mc->mca_sfmode = MCAST_EXCLUDE;
919 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
921 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
922 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
923 mc->mca_flags |= MAF_NOREPORT;
925 mc->next = idev->mc_list;
927 write_unlock_bh(&idev->lock);
929 mld_del_delrec(idev, &mc->mca_addr);
930 igmp6_group_added(mc);
936 * device multicast group del
938 int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr)
940 struct ifmcaddr6 *ma, **map;
942 write_lock_bh(&idev->lock);
943 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
944 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
945 if (--ma->mca_users == 0) {
947 write_unlock_bh(&idev->lock);
949 igmp6_group_dropped(ma);
954 write_unlock_bh(&idev->lock);
958 write_unlock_bh(&idev->lock);
963 int ipv6_dev_mc_dec(struct net_device *dev, struct in6_addr *addr)
965 struct inet6_dev *idev = in6_dev_get(dev);
971 err = __ipv6_dev_mc_dec(idev, addr);
979 * identify MLD packets for MLD filter exceptions
981 int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
983 struct icmp6hdr *pic;
985 if (nexthdr != IPPROTO_ICMPV6)
988 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
991 pic = icmp6_hdr(skb);
993 switch (pic->icmp6_type) {
994 case ICMPV6_MGM_QUERY:
995 case ICMPV6_MGM_REPORT:
996 case ICMPV6_MGM_REDUCTION:
997 case ICMPV6_MLD2_REPORT:
1006 * check if the interface/address pair is valid
1008 int ipv6_chk_mcast_addr(struct net_device *dev, struct in6_addr *group,
1009 struct in6_addr *src_addr)
1011 struct inet6_dev *idev;
1012 struct ifmcaddr6 *mc;
1015 idev = in6_dev_get(dev);
1017 read_lock_bh(&idev->lock);
1018 for (mc = idev->mc_list; mc; mc=mc->next) {
1019 if (ipv6_addr_equal(&mc->mca_addr, group))
1023 if (src_addr && !ipv6_addr_any(src_addr)) {
1024 struct ip6_sf_list *psf;
1026 spin_lock_bh(&mc->mca_lock);
1027 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1028 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1032 rv = psf->sf_count[MCAST_INCLUDE] ||
1033 psf->sf_count[MCAST_EXCLUDE] !=
1034 mc->mca_sfcount[MCAST_EXCLUDE];
1036 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1037 spin_unlock_bh(&mc->mca_lock);
1039 rv = 1; /* don't filter unspecified source */
1041 read_unlock_bh(&idev->lock);
1047 static void mld_gq_start_timer(struct inet6_dev *idev)
1049 int tv = net_random() % idev->mc_maxdelay;
1051 idev->mc_gq_running = 1;
1052 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1056 static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1058 int tv = net_random() % delay;
1060 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1065 * IGMP handling (alias multicast ICMPv6 messages)
1068 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1070 unsigned long delay = resptime;
1072 /* Do not start timer for these addresses */
1073 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1074 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1077 if (del_timer(&ma->mca_timer)) {
1078 atomic_dec(&ma->mca_refcnt);
1079 delay = ma->mca_timer.expires - jiffies;
1082 if (delay >= resptime) {
1084 delay = net_random() % resptime;
1088 ma->mca_timer.expires = jiffies + delay;
1089 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1090 atomic_inc(&ma->mca_refcnt);
1091 ma->mca_flags |= MAF_TIMER_RUNNING;
1094 /* mark EXCLUDE-mode sources */
1095 static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1096 struct in6_addr *srcs)
1098 struct ip6_sf_list *psf;
1102 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1103 if (scount == nsrcs)
1105 for (i=0; i<nsrcs; i++) {
1106 /* skip inactive filters */
1107 if (pmc->mca_sfcount[MCAST_INCLUDE] ||
1108 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1109 psf->sf_count[MCAST_EXCLUDE])
1111 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1117 pmc->mca_flags &= ~MAF_GSQUERY;
1118 if (scount == nsrcs) /* all sources excluded */
1123 static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1124 struct in6_addr *srcs)
1126 struct ip6_sf_list *psf;
1129 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1130 return mld_xmarksources(pmc, nsrcs, srcs);
1132 /* mark INCLUDE-mode sources */
1135 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1136 if (scount == nsrcs)
1138 for (i=0; i<nsrcs; i++) {
1139 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1147 pmc->mca_flags &= ~MAF_GSQUERY;
1150 pmc->mca_flags |= MAF_GSQUERY;
1154 int igmp6_event_query(struct sk_buff *skb)
1156 struct mld2_query *mlh2 = NULL;
1157 struct ifmcaddr6 *ma;
1158 struct in6_addr *group;
1159 unsigned long max_delay;
1160 struct inet6_dev *idev;
1161 struct icmp6hdr *hdr;
1166 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1169 /* compute payload length excluding extension headers */
1170 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
1171 len -= skb_network_header_len(skb);
1173 /* Drop queries with not link local source */
1174 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
1177 idev = in6_dev_get(skb->dev);
1182 hdr = icmp6_hdr(skb);
1183 group = (struct in6_addr *) (hdr + 1);
1184 group_type = ipv6_addr_type(group);
1186 if (group_type != IPV6_ADDR_ANY &&
1187 !(group_type&IPV6_ADDR_MULTICAST)) {
1194 /* MLDv1 router present */
1196 /* Translate milliseconds to jiffies */
1197 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1199 switchback = (idev->mc_qrv + 1) * max_delay;
1200 idev->mc_v1_seen = jiffies + switchback;
1202 /* cancel the interface change timer */
1203 idev->mc_ifc_count = 0;
1204 if (del_timer(&idev->mc_ifc_timer))
1205 __in6_dev_put(idev);
1206 /* clear deleted report items */
1207 mld_clear_delrec(idev);
1208 } else if (len >= 28) {
1209 int srcs_offset = sizeof(struct mld2_query) -
1210 sizeof(struct icmp6hdr);
1211 if (!pskb_may_pull(skb, srcs_offset)) {
1215 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1216 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1219 idev->mc_maxdelay = max_delay;
1221 idev->mc_qrv = mlh2->qrv;
1222 if (group_type == IPV6_ADDR_ANY) { /* general query */
1225 return -EINVAL; /* no sources allowed */
1227 mld_gq_start_timer(idev);
1231 /* mark sources to include, if group & source-specific */
1232 if (mlh2->nsrcs != 0) {
1233 if (!pskb_may_pull(skb, srcs_offset +
1234 ntohs(mlh2->nsrcs) * sizeof(struct in6_addr))) {
1238 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1246 read_lock_bh(&idev->lock);
1247 if (group_type == IPV6_ADDR_ANY) {
1248 for (ma = idev->mc_list; ma; ma=ma->next) {
1249 spin_lock_bh(&ma->mca_lock);
1250 igmp6_group_queried(ma, max_delay);
1251 spin_unlock_bh(&ma->mca_lock);
1254 for (ma = idev->mc_list; ma; ma=ma->next) {
1255 if (!ipv6_addr_equal(group, &ma->mca_addr))
1257 spin_lock_bh(&ma->mca_lock);
1258 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1259 /* gsquery <- gsquery && mark */
1261 ma->mca_flags &= ~MAF_GSQUERY;
1263 /* gsquery <- mark */
1265 ma->mca_flags |= MAF_GSQUERY;
1267 ma->mca_flags &= ~MAF_GSQUERY;
1269 if (!(ma->mca_flags & MAF_GSQUERY) ||
1270 mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
1271 igmp6_group_queried(ma, max_delay);
1272 spin_unlock_bh(&ma->mca_lock);
1276 read_unlock_bh(&idev->lock);
1283 int igmp6_event_report(struct sk_buff *skb)
1285 struct ifmcaddr6 *ma;
1286 struct in6_addr *addrp;
1287 struct inet6_dev *idev;
1288 struct icmp6hdr *hdr;
1291 /* Our own report looped back. Ignore it. */
1292 if (skb->pkt_type == PACKET_LOOPBACK)
1295 /* send our report if the MC router may not have heard this report */
1296 if (skb->pkt_type != PACKET_MULTICAST &&
1297 skb->pkt_type != PACKET_BROADCAST)
1300 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1303 hdr = icmp6_hdr(skb);
1305 /* Drop reports with not link local source */
1306 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
1307 if (addr_type != IPV6_ADDR_ANY &&
1308 !(addr_type&IPV6_ADDR_LINKLOCAL))
1311 addrp = (struct in6_addr *) (hdr + 1);
1313 idev = in6_dev_get(skb->dev);
1318 * Cancel the timer for this group
1321 read_lock_bh(&idev->lock);
1322 for (ma = idev->mc_list; ma; ma=ma->next) {
1323 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1324 spin_lock(&ma->mca_lock);
1325 if (del_timer(&ma->mca_timer))
1326 atomic_dec(&ma->mca_refcnt);
1327 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1328 spin_unlock(&ma->mca_lock);
1332 read_unlock_bh(&idev->lock);
1337 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1338 int gdeleted, int sdeleted)
1341 case MLD2_MODE_IS_INCLUDE:
1342 case MLD2_MODE_IS_EXCLUDE:
1343 if (gdeleted || sdeleted)
1345 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1346 if (pmc->mca_sfmode == MCAST_INCLUDE)
1348 /* don't include if this source is excluded
1351 if (psf->sf_count[MCAST_INCLUDE])
1352 return type == MLD2_MODE_IS_INCLUDE;
1353 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1354 psf->sf_count[MCAST_EXCLUDE];
1357 case MLD2_CHANGE_TO_INCLUDE:
1358 if (gdeleted || sdeleted)
1360 return psf->sf_count[MCAST_INCLUDE] != 0;
1361 case MLD2_CHANGE_TO_EXCLUDE:
1362 if (gdeleted || sdeleted)
1364 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1365 psf->sf_count[MCAST_INCLUDE])
1367 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1368 psf->sf_count[MCAST_EXCLUDE];
1369 case MLD2_ALLOW_NEW_SOURCES:
1370 if (gdeleted || !psf->sf_crcount)
1372 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1373 case MLD2_BLOCK_OLD_SOURCES:
1374 if (pmc->mca_sfmode == MCAST_INCLUDE)
1375 return gdeleted || (psf->sf_crcount && sdeleted);
1376 return psf->sf_crcount && !gdeleted && !sdeleted;
1382 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1384 struct ip6_sf_list *psf;
1387 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1388 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1395 static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1397 struct sock *sk = igmp6_socket->sk;
1398 struct sk_buff *skb;
1399 struct mld2_report *pmr;
1400 struct in6_addr addr_buf;
1402 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1403 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1406 /* we assume size > sizeof(ra) here */
1407 skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
1412 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1414 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1415 /* <draft-ietf-magma-mld-source-05.txt>:
1416 * use unspecified address as the source address
1417 * when a valid link-local address is not available.
1419 memset(&addr_buf, 0, sizeof(addr_buf));
1422 ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);
1424 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1426 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
1427 skb_put(skb, sizeof(*pmr));
1428 pmr = (struct mld2_report *)skb_transport_header(skb);
1429 pmr->type = ICMPV6_MLD2_REPORT;
1437 static inline int mld_dev_queue_xmit2(struct sk_buff *skb)
1439 struct net_device *dev = skb->dev;
1441 if (dev->hard_header) {
1442 unsigned char ha[MAX_ADDR_LEN];
1445 ndisc_mc_map(&ipv6_hdr(skb)->daddr, ha, dev, 1);
1446 err = dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, skb->len);
1452 return dev_queue_xmit(skb);
1455 static inline int mld_dev_queue_xmit(struct sk_buff *skb)
1457 return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dev,
1458 mld_dev_queue_xmit2);
1461 static void mld_sendpack(struct sk_buff *skb)
1463 struct ipv6hdr *pip6 = ipv6_hdr(skb);
1464 struct mld2_report *pmr =
1465 (struct mld2_report *)skb_transport_header(skb);
1466 int payload_len, mldlen;
1467 struct inet6_dev *idev = in6_dev_get(skb->dev);
1470 IP6_INC_STATS(idev, IPSTATS_MIB_OUTREQUESTS);
1471 payload_len = (skb->tail - skb->network_header) - sizeof(*pip6);
1472 mldlen = skb->tail - skb->transport_header;
1473 pip6->payload_len = htons(payload_len);
1475 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1476 IPPROTO_ICMPV6, csum_partial(skb_transport_header(skb),
1478 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1479 mld_dev_queue_xmit);
1481 ICMP6_INC_STATS(idev,ICMP6_MIB_OUTMSGS);
1482 IP6_INC_STATS(idev, IPSTATS_MIB_OUTMCASTPKTS);
1484 IP6_INC_STATS(idev, IPSTATS_MIB_OUTDISCARDS);
1486 if (likely(idev != NULL))
1490 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1492 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1495 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1496 int type, struct mld2_grec **ppgr)
1498 struct net_device *dev = pmc->idev->dev;
1499 struct mld2_report *pmr;
1500 struct mld2_grec *pgr;
1503 skb = mld_newpack(dev, dev->mtu);
1506 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1507 pgr->grec_type = type;
1508 pgr->grec_auxwords = 0;
1509 pgr->grec_nsrcs = 0;
1510 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1511 pmr = (struct mld2_report *)skb_transport_header(skb);
1512 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1517 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1518 skb_tailroom(skb)) : 0)
1520 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1521 int type, int gdeleted, int sdeleted)
1523 struct net_device *dev = pmc->idev->dev;
1524 struct mld2_report *pmr;
1525 struct mld2_grec *pgr = NULL;
1526 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1527 int scount, stotal, first, isquery, truncate;
1529 if (pmc->mca_flags & MAF_NOREPORT)
1532 isquery = type == MLD2_MODE_IS_INCLUDE ||
1533 type == MLD2_MODE_IS_EXCLUDE;
1534 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1535 type == MLD2_CHANGE_TO_EXCLUDE;
1537 stotal = scount = 0;
1539 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1544 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
1546 /* EX and TO_EX get a fresh packet, if needed */
1548 if (pmr && pmr->ngrec &&
1549 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1552 skb = mld_newpack(dev, dev->mtu);
1557 for (psf=*psf_list; psf; psf=psf_next) {
1558 struct in6_addr *psrc;
1560 psf_next = psf->sf_next;
1562 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1567 /* clear marks on query responses */
1571 if (AVAILABLE(skb) < sizeof(*psrc) +
1572 first*sizeof(struct mld2_grec)) {
1573 if (truncate && !first)
1574 break; /* truncate these */
1576 pgr->grec_nsrcs = htons(scount);
1579 skb = mld_newpack(dev, dev->mtu);
1584 skb = add_grhead(skb, pmc, type, &pgr);
1589 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1590 *psrc = psf->sf_addr;
1592 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1593 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1595 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1597 psf_prev->sf_next = psf->sf_next;
1599 *psf_list = psf->sf_next;
1609 if (type == MLD2_ALLOW_NEW_SOURCES ||
1610 type == MLD2_BLOCK_OLD_SOURCES)
1612 if (pmc->mca_crcount || isquery) {
1613 /* make sure we have room for group header */
1614 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1616 skb = NULL; /* add_grhead will get a new one */
1618 skb = add_grhead(skb, pmc, type, &pgr);
1622 pgr->grec_nsrcs = htons(scount);
1625 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1629 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1631 struct sk_buff *skb = NULL;
1635 read_lock_bh(&idev->lock);
1636 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1637 if (pmc->mca_flags & MAF_NOREPORT)
1639 spin_lock_bh(&pmc->mca_lock);
1640 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1641 type = MLD2_MODE_IS_EXCLUDE;
1643 type = MLD2_MODE_IS_INCLUDE;
1644 skb = add_grec(skb, pmc, type, 0, 0);
1645 spin_unlock_bh(&pmc->mca_lock);
1647 read_unlock_bh(&idev->lock);
1649 spin_lock_bh(&pmc->mca_lock);
1650 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1651 type = MLD2_MODE_IS_EXCLUDE;
1653 type = MLD2_MODE_IS_INCLUDE;
1654 skb = add_grec(skb, pmc, type, 0, 0);
1655 spin_unlock_bh(&pmc->mca_lock);
1662 * remove zero-count source records from a source filter list
1664 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1666 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1669 for (psf=*ppsf; psf; psf = psf_next) {
1670 psf_next = psf->sf_next;
1671 if (psf->sf_crcount == 0) {
1673 psf_prev->sf_next = psf->sf_next;
1675 *ppsf = psf->sf_next;
1682 static void mld_send_cr(struct inet6_dev *idev)
1684 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1685 struct sk_buff *skb = NULL;
1688 read_lock_bh(&idev->lock);
1689 write_lock_bh(&idev->mc_lock);
1693 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1694 pmc_next = pmc->next;
1695 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1696 type = MLD2_BLOCK_OLD_SOURCES;
1697 dtype = MLD2_BLOCK_OLD_SOURCES;
1698 skb = add_grec(skb, pmc, type, 1, 0);
1699 skb = add_grec(skb, pmc, dtype, 1, 1);
1701 if (pmc->mca_crcount) {
1702 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1703 type = MLD2_CHANGE_TO_INCLUDE;
1704 skb = add_grec(skb, pmc, type, 1, 0);
1707 if (pmc->mca_crcount == 0) {
1708 mld_clear_zeros(&pmc->mca_tomb);
1709 mld_clear_zeros(&pmc->mca_sources);
1712 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1713 !pmc->mca_sources) {
1715 pmc_prev->next = pmc_next;
1717 idev->mc_tomb = pmc_next;
1718 in6_dev_put(pmc->idev);
1723 write_unlock_bh(&idev->mc_lock);
1726 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1727 spin_lock_bh(&pmc->mca_lock);
1728 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1729 type = MLD2_BLOCK_OLD_SOURCES;
1730 dtype = MLD2_ALLOW_NEW_SOURCES;
1732 type = MLD2_ALLOW_NEW_SOURCES;
1733 dtype = MLD2_BLOCK_OLD_SOURCES;
1735 skb = add_grec(skb, pmc, type, 0, 0);
1736 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1738 /* filter mode changes */
1739 if (pmc->mca_crcount) {
1740 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1741 type = MLD2_CHANGE_TO_EXCLUDE;
1743 type = MLD2_CHANGE_TO_INCLUDE;
1744 skb = add_grec(skb, pmc, type, 0, 0);
1747 spin_unlock_bh(&pmc->mca_lock);
1749 read_unlock_bh(&idev->lock);
1752 (void) mld_sendpack(skb);
1755 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1757 struct sock *sk = igmp6_socket->sk;
1758 struct inet6_dev *idev;
1759 struct sk_buff *skb;
1760 struct icmp6hdr *hdr;
1761 struct in6_addr *snd_addr;
1762 struct in6_addr *addrp;
1763 struct in6_addr addr_buf;
1764 struct in6_addr all_routers;
1765 int err, len, payload_len, full_len;
1766 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1767 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1771 IP6_INC_STATS(__in6_dev_get(dev),
1772 IPSTATS_MIB_OUTREQUESTS);
1775 if (type == ICMPV6_MGM_REDUCTION) {
1776 snd_addr = &all_routers;
1777 ipv6_addr_all_routers(&all_routers);
1780 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1781 payload_len = len + sizeof(ra);
1782 full_len = sizeof(struct ipv6hdr) + payload_len;
1784 skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1788 IP6_INC_STATS(__in6_dev_get(dev),
1789 IPSTATS_MIB_OUTDISCARDS);
1794 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1796 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1797 /* <draft-ietf-magma-mld-source-05.txt>:
1798 * use unspecified address as the source address
1799 * when a valid link-local address is not available.
1801 memset(&addr_buf, 0, sizeof(addr_buf));
1804 ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
1806 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1808 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1809 memset(hdr, 0, sizeof(struct icmp6hdr));
1810 hdr->icmp6_type = type;
1812 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1813 ipv6_addr_copy(addrp, addr);
1815 hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
1817 csum_partial((__u8 *) hdr, len, 0));
1819 idev = in6_dev_get(skb->dev);
1821 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1822 mld_dev_queue_xmit);
1824 if (type == ICMPV6_MGM_REDUCTION)
1825 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS);
1827 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBRESPONSES);
1828 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1829 IP6_INC_STATS(idev, IPSTATS_MIB_OUTMCASTPKTS);
1831 IP6_INC_STATS(idev, IPSTATS_MIB_OUTDISCARDS);
1833 if (likely(idev != NULL))
1838 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1839 struct in6_addr *psfsrc)
1841 struct ip6_sf_list *psf, *psf_prev;
1845 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1846 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1850 if (!psf || psf->sf_count[sfmode] == 0) {
1851 /* source filter not found, or count wrong => bug */
1854 psf->sf_count[sfmode]--;
1855 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1856 struct inet6_dev *idev = pmc->idev;
1858 /* no more filters for this source */
1860 psf_prev->sf_next = psf->sf_next;
1862 pmc->mca_sources = psf->sf_next;
1863 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1864 !MLD_V1_SEEN(idev)) {
1865 psf->sf_crcount = idev->mc_qrv;
1866 psf->sf_next = pmc->mca_tomb;
1867 pmc->mca_tomb = psf;
1875 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1876 int sfmode, int sfcount, struct in6_addr *psfsrc,
1879 struct ifmcaddr6 *pmc;
1885 read_lock_bh(&idev->lock);
1886 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1887 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1891 /* MCA not found?? bug */
1892 read_unlock_bh(&idev->lock);
1895 spin_lock_bh(&pmc->mca_lock);
1898 if (!pmc->mca_sfcount[sfmode]) {
1899 spin_unlock_bh(&pmc->mca_lock);
1900 read_unlock_bh(&idev->lock);
1903 pmc->mca_sfcount[sfmode]--;
1906 for (i=0; i<sfcount; i++) {
1907 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1909 changerec |= rv > 0;
1913 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1914 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1915 pmc->mca_sfcount[MCAST_INCLUDE]) {
1916 struct ip6_sf_list *psf;
1918 /* filter mode change */
1919 pmc->mca_sfmode = MCAST_INCLUDE;
1920 pmc->mca_crcount = idev->mc_qrv;
1921 idev->mc_ifc_count = pmc->mca_crcount;
1922 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1923 psf->sf_crcount = 0;
1924 mld_ifc_event(pmc->idev);
1925 } else if (sf_setstate(pmc) || changerec)
1926 mld_ifc_event(pmc->idev);
1927 spin_unlock_bh(&pmc->mca_lock);
1928 read_unlock_bh(&idev->lock);
1933 * Add multicast single-source filter to the interface list
1935 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1936 struct in6_addr *psfsrc, int delta)
1938 struct ip6_sf_list *psf, *psf_prev;
1941 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1942 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1947 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1951 psf->sf_addr = *psfsrc;
1953 psf_prev->sf_next = psf;
1955 pmc->mca_sources = psf;
1957 psf->sf_count[sfmode]++;
1961 static void sf_markstate(struct ifmcaddr6 *pmc)
1963 struct ip6_sf_list *psf;
1964 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1966 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1967 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1968 psf->sf_oldin = mca_xcount ==
1969 psf->sf_count[MCAST_EXCLUDE] &&
1970 !psf->sf_count[MCAST_INCLUDE];
1972 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1975 static int sf_setstate(struct ifmcaddr6 *pmc)
1977 struct ip6_sf_list *psf, *dpsf;
1978 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1979 int qrv = pmc->idev->mc_qrv;
1983 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1984 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1985 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1986 !psf->sf_count[MCAST_INCLUDE];
1988 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1990 if (!psf->sf_oldin) {
1991 struct ip6_sf_list *prev = NULL;
1993 for (dpsf=pmc->mca_tomb; dpsf;
1994 dpsf=dpsf->sf_next) {
1995 if (ipv6_addr_equal(&dpsf->sf_addr,
2002 prev->sf_next = dpsf->sf_next;
2004 pmc->mca_tomb = dpsf->sf_next;
2007 psf->sf_crcount = qrv;
2010 } else if (psf->sf_oldin) {
2011 psf->sf_crcount = 0;
2013 * add or update "delete" records if an active filter
2016 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2017 if (ipv6_addr_equal(&dpsf->sf_addr,
2021 dpsf = (struct ip6_sf_list *)
2022 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2026 /* pmc->mca_lock held by callers */
2027 dpsf->sf_next = pmc->mca_tomb;
2028 pmc->mca_tomb = dpsf;
2030 dpsf->sf_crcount = qrv;
2038 * Add multicast source filter list to the interface list
2040 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
2041 int sfmode, int sfcount, struct in6_addr *psfsrc,
2044 struct ifmcaddr6 *pmc;
2050 read_lock_bh(&idev->lock);
2051 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2052 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2056 /* MCA not found?? bug */
2057 read_unlock_bh(&idev->lock);
2060 spin_lock_bh(&pmc->mca_lock);
2063 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2065 pmc->mca_sfcount[sfmode]++;
2067 for (i=0; i<sfcount; i++) {
2068 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
2076 pmc->mca_sfcount[sfmode]--;
2078 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2079 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2080 struct inet6_dev *idev = pmc->idev;
2081 struct ip6_sf_list *psf;
2083 /* filter mode change */
2084 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2085 pmc->mca_sfmode = MCAST_EXCLUDE;
2086 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2087 pmc->mca_sfmode = MCAST_INCLUDE;
2088 /* else no filters; keep old mode for reports */
2090 pmc->mca_crcount = idev->mc_qrv;
2091 idev->mc_ifc_count = pmc->mca_crcount;
2092 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2093 psf->sf_crcount = 0;
2094 mld_ifc_event(idev);
2095 } else if (sf_setstate(pmc))
2096 mld_ifc_event(idev);
2097 spin_unlock_bh(&pmc->mca_lock);
2098 read_unlock_bh(&idev->lock);
2102 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2104 struct ip6_sf_list *psf, *nextpsf;
2106 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2107 nextpsf = psf->sf_next;
2110 pmc->mca_tomb = NULL;
2111 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2112 nextpsf = psf->sf_next;
2115 pmc->mca_sources = NULL;
2116 pmc->mca_sfmode = MCAST_EXCLUDE;
2117 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2118 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2122 static void igmp6_join_group(struct ifmcaddr6 *ma)
2124 unsigned long delay;
2126 if (ma->mca_flags & MAF_NOREPORT)
2129 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2131 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2133 spin_lock_bh(&ma->mca_lock);
2134 if (del_timer(&ma->mca_timer)) {
2135 atomic_dec(&ma->mca_refcnt);
2136 delay = ma->mca_timer.expires - jiffies;
2139 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2140 atomic_inc(&ma->mca_refcnt);
2141 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2142 spin_unlock_bh(&ma->mca_lock);
2145 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2146 struct inet6_dev *idev)
2150 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2151 * so no other readers or writers of iml or its sflist
2153 if (iml->sflist == 0) {
2154 /* any-source empty exclude case */
2155 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2157 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2158 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2159 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2164 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2166 if (MLD_V1_SEEN(ma->idev)) {
2167 if (ma->mca_flags & MAF_LAST_REPORTER)
2168 igmp6_send(&ma->mca_addr, ma->idev->dev,
2169 ICMPV6_MGM_REDUCTION);
2171 mld_add_delrec(ma->idev, ma);
2172 mld_ifc_event(ma->idev);
2176 static void mld_gq_timer_expire(unsigned long data)
2178 struct inet6_dev *idev = (struct inet6_dev *)data;
2180 idev->mc_gq_running = 0;
2181 mld_send_report(idev, NULL);
2182 __in6_dev_put(idev);
2185 static void mld_ifc_timer_expire(unsigned long data)
2187 struct inet6_dev *idev = (struct inet6_dev *)data;
2190 if (idev->mc_ifc_count) {
2191 idev->mc_ifc_count--;
2192 if (idev->mc_ifc_count)
2193 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2195 __in6_dev_put(idev);
2198 static void mld_ifc_event(struct inet6_dev *idev)
2200 if (MLD_V1_SEEN(idev))
2202 idev->mc_ifc_count = idev->mc_qrv;
2203 mld_ifc_start_timer(idev, 1);
2207 static void igmp6_timer_handler(unsigned long data)
2209 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2211 if (MLD_V1_SEEN(ma->idev))
2212 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2214 mld_send_report(ma->idev, ma);
2216 spin_lock(&ma->mca_lock);
2217 ma->mca_flags |= MAF_LAST_REPORTER;
2218 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2219 spin_unlock(&ma->mca_lock);
2223 /* Device going down */
2225 void ipv6_mc_down(struct inet6_dev *idev)
2227 struct ifmcaddr6 *i;
2229 /* Withdraw multicast list */
2231 read_lock_bh(&idev->lock);
2232 idev->mc_ifc_count = 0;
2233 if (del_timer(&idev->mc_ifc_timer))
2234 __in6_dev_put(idev);
2235 idev->mc_gq_running = 0;
2236 if (del_timer(&idev->mc_gq_timer))
2237 __in6_dev_put(idev);
2239 for (i = idev->mc_list; i; i=i->next)
2240 igmp6_group_dropped(i);
2241 read_unlock_bh(&idev->lock);
2243 mld_clear_delrec(idev);
2247 /* Device going up */
2249 void ipv6_mc_up(struct inet6_dev *idev)
2251 struct ifmcaddr6 *i;
2253 /* Install multicast list, except for all-nodes (already installed) */
2255 read_lock_bh(&idev->lock);
2256 for (i = idev->mc_list; i; i=i->next)
2257 igmp6_group_added(i);
2258 read_unlock_bh(&idev->lock);
2261 /* IPv6 device initialization. */
2263 void ipv6_mc_init_dev(struct inet6_dev *idev)
2265 write_lock_bh(&idev->lock);
2266 rwlock_init(&idev->mc_lock);
2267 idev->mc_gq_running = 0;
2268 init_timer(&idev->mc_gq_timer);
2269 idev->mc_gq_timer.data = (unsigned long) idev;
2270 idev->mc_gq_timer.function = &mld_gq_timer_expire;
2271 idev->mc_tomb = NULL;
2272 idev->mc_ifc_count = 0;
2273 init_timer(&idev->mc_ifc_timer);
2274 idev->mc_ifc_timer.data = (unsigned long) idev;
2275 idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
2276 idev->mc_qrv = MLD_QRV_DEFAULT;
2277 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2278 idev->mc_v1_seen = 0;
2279 write_unlock_bh(&idev->lock);
2283 * Device is about to be destroyed: clean up.
2286 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2288 struct ifmcaddr6 *i;
2289 struct in6_addr maddr;
2291 /* Deactivate timers */
2294 /* Delete all-nodes address. */
2295 ipv6_addr_all_nodes(&maddr);
2297 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2298 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2301 __ipv6_dev_mc_dec(idev, &maddr);
2303 if (idev->cnf.forwarding) {
2304 ipv6_addr_all_routers(&maddr);
2305 __ipv6_dev_mc_dec(idev, &maddr);
2308 write_lock_bh(&idev->lock);
2309 while ((i = idev->mc_list) != NULL) {
2310 idev->mc_list = i->next;
2311 write_unlock_bh(&idev->lock);
2313 igmp6_group_dropped(i);
2316 write_lock_bh(&idev->lock);
2318 write_unlock_bh(&idev->lock);
2321 #ifdef CONFIG_PROC_FS
2322 struct igmp6_mc_iter_state {
2323 struct net_device *dev;
2324 struct inet6_dev *idev;
2327 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2329 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2331 struct ifmcaddr6 *im = NULL;
2332 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2334 for (state->dev = dev_base, state->idev = NULL;
2336 state->dev = state->dev->next) {
2337 struct inet6_dev *idev;
2338 idev = in6_dev_get(state->dev);
2341 read_lock_bh(&idev->lock);
2347 read_unlock_bh(&idev->lock);
2353 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2355 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2359 if (likely(state->idev != NULL)) {
2360 read_unlock_bh(&state->idev->lock);
2361 in6_dev_put(state->idev);
2363 state->dev = state->dev->next;
2368 state->idev = in6_dev_get(state->dev);
2371 read_lock_bh(&state->idev->lock);
2372 im = state->idev->mc_list;
2377 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2379 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2381 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2383 return pos ? NULL : im;
2386 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2388 read_lock(&dev_base_lock);
2389 return igmp6_mc_get_idx(seq, *pos);
2392 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2394 struct ifmcaddr6 *im;
2395 im = igmp6_mc_get_next(seq, v);
2400 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2402 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2403 if (likely(state->idev != NULL)) {
2404 read_unlock_bh(&state->idev->lock);
2405 in6_dev_put(state->idev);
2409 read_unlock(&dev_base_lock);
2412 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2414 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2415 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2418 "%-4d %-15s " NIP6_SEQFMT " %5d %08X %ld\n",
2419 state->dev->ifindex, state->dev->name,
2421 im->mca_users, im->mca_flags,
2422 (im->mca_flags&MAF_TIMER_RUNNING) ?
2423 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2427 static struct seq_operations igmp6_mc_seq_ops = {
2428 .start = igmp6_mc_seq_start,
2429 .next = igmp6_mc_seq_next,
2430 .stop = igmp6_mc_seq_stop,
2431 .show = igmp6_mc_seq_show,
2434 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2436 struct seq_file *seq;
2438 struct igmp6_mc_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
2443 rc = seq_open(file, &igmp6_mc_seq_ops);
2447 seq = file->private_data;
2456 static const struct file_operations igmp6_mc_seq_fops = {
2457 .owner = THIS_MODULE,
2458 .open = igmp6_mc_seq_open,
2460 .llseek = seq_lseek,
2461 .release = seq_release_private,
2464 struct igmp6_mcf_iter_state {
2465 struct net_device *dev;
2466 struct inet6_dev *idev;
2467 struct ifmcaddr6 *im;
2470 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2472 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2474 struct ip6_sf_list *psf = NULL;
2475 struct ifmcaddr6 *im = NULL;
2476 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2478 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2480 state->dev = state->dev->next) {
2481 struct inet6_dev *idev;
2482 idev = in6_dev_get(state->dev);
2483 if (unlikely(idev == NULL))
2485 read_lock_bh(&idev->lock);
2487 if (likely(im != NULL)) {
2488 spin_lock_bh(&im->mca_lock);
2489 psf = im->mca_sources;
2490 if (likely(psf != NULL)) {
2495 spin_unlock_bh(&im->mca_lock);
2497 read_unlock_bh(&idev->lock);
2503 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2505 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2509 spin_unlock_bh(&state->im->mca_lock);
2510 state->im = state->im->next;
2511 while (!state->im) {
2512 if (likely(state->idev != NULL)) {
2513 read_unlock_bh(&state->idev->lock);
2514 in6_dev_put(state->idev);
2516 state->dev = state->dev->next;
2521 state->idev = in6_dev_get(state->dev);
2524 read_lock_bh(&state->idev->lock);
2525 state->im = state->idev->mc_list;
2529 spin_lock_bh(&state->im->mca_lock);
2530 psf = state->im->mca_sources;
2536 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2538 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2540 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2542 return pos ? NULL : psf;
2545 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2547 read_lock(&dev_base_lock);
2548 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2551 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2553 struct ip6_sf_list *psf;
2554 if (v == SEQ_START_TOKEN)
2555 psf = igmp6_mcf_get_first(seq);
2557 psf = igmp6_mcf_get_next(seq, v);
2562 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2564 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2565 if (likely(state->im != NULL)) {
2566 spin_unlock_bh(&state->im->mca_lock);
2569 if (likely(state->idev != NULL)) {
2570 read_unlock_bh(&state->idev->lock);
2571 in6_dev_put(state->idev);
2575 read_unlock(&dev_base_lock);
2578 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2580 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2581 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2583 if (v == SEQ_START_TOKEN) {
2586 "%32s %32s %6s %6s\n", "Idx",
2587 "Device", "Multicast Address",
2588 "Source Address", "INC", "EXC");
2591 "%3d %6.6s " NIP6_SEQFMT " " NIP6_SEQFMT " %6lu %6lu\n",
2592 state->dev->ifindex, state->dev->name,
2593 NIP6(state->im->mca_addr),
2595 psf->sf_count[MCAST_INCLUDE],
2596 psf->sf_count[MCAST_EXCLUDE]);
2601 static struct seq_operations igmp6_mcf_seq_ops = {
2602 .start = igmp6_mcf_seq_start,
2603 .next = igmp6_mcf_seq_next,
2604 .stop = igmp6_mcf_seq_stop,
2605 .show = igmp6_mcf_seq_show,
2608 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2610 struct seq_file *seq;
2612 struct igmp6_mcf_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
2617 rc = seq_open(file, &igmp6_mcf_seq_ops);
2621 seq = file->private_data;
2630 static const struct file_operations igmp6_mcf_seq_fops = {
2631 .owner = THIS_MODULE,
2632 .open = igmp6_mcf_seq_open,
2634 .llseek = seq_lseek,
2635 .release = seq_release_private,
2639 int __init igmp6_init(struct net_proto_family *ops)
2641 struct ipv6_pinfo *np;
2645 err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
2648 "Failed to initialize the IGMP6 control socket (err %d).\n",
2650 igmp6_socket = NULL; /* For safety. */
2654 sk = igmp6_socket->sk;
2655 sk->sk_allocation = GFP_ATOMIC;
2656 sk->sk_prot->unhash(sk);
2661 #ifdef CONFIG_PROC_FS
2662 proc_net_fops_create("igmp6", S_IRUGO, &igmp6_mc_seq_fops);
2663 proc_net_fops_create("mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
2669 void igmp6_cleanup(void)
2671 sock_release(igmp6_socket);
2672 igmp6_socket = NULL; /* for safety */
2674 #ifdef CONFIG_PROC_FS
2675 proc_net_remove("mcfilter6");
2676 proc_net_remove("igmp6");