2 * Multicast support for IPv6
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: mcast.c,v 1.40 2002/02/08 03:57:19 davem Exp $
10 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
20 * yoshfuji : fix format of router-alert option
21 * YOSHIFUJI Hideaki @USAGI:
22 * Fixed source address for MLD message based on
23 * <draft-ietf-magma-mld-source-05.txt>.
24 * YOSHIFUJI Hideaki @USAGI:
25 * - Ignore Queries for invalid addresses.
26 * - MLD for link-local addresses.
27 * David L Stevens <dlstevens@us.ibm.com>:
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/errno.h>
34 #include <linux/types.h>
35 #include <linux/string.h>
36 #include <linux/socket.h>
37 #include <linux/sockios.h>
38 #include <linux/jiffies.h>
39 #include <linux/times.h>
40 #include <linux/net.h>
42 #include <linux/in6.h>
43 #include <linux/netdevice.h>
44 #include <linux/if_arp.h>
45 #include <linux/route.h>
46 #include <linux/init.h>
47 #include <linux/proc_fs.h>
48 #include <linux/seq_file.h>
50 #include <linux/netfilter.h>
51 #include <linux/netfilter_ipv6.h>
57 #include <net/protocol.h>
58 #include <net/if_inet6.h>
59 #include <net/ndisc.h>
60 #include <net/addrconf.h>
61 #include <net/ip6_route.h>
63 #include <net/ip6_checksum.h>
65 /* Set to 3 to get tracing... */
69 #define MDBG(x) printk x
75 * These header formats should be in a separate include file, but icmpv6.h
76 * doesn't have in6_addr defined in all cases, there is no __u128, and no
77 * other files reference these.
82 /* Multicast Listener Discovery version 2 headers */
88 struct in6_addr grec_mca;
89 struct in6_addr grec_src[0];
98 struct mld2_grec grec[0];
108 #if defined(__LITTLE_ENDIAN_BITFIELD)
112 #elif defined(__BIG_ENDIAN_BITFIELD)
117 #error "Please fix <asm/byteorder.h>"
121 struct in6_addr srcs[0];
124 static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
126 /* Big mc list lock for all the sockets */
127 static DEFINE_RWLOCK(ipv6_sk_mc_lock);
129 static struct socket *igmp6_socket;
131 int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr);
133 static void igmp6_join_group(struct ifmcaddr6 *ma);
134 static void igmp6_leave_group(struct ifmcaddr6 *ma);
135 static void igmp6_timer_handler(unsigned long data);
137 static void mld_gq_timer_expire(unsigned long data);
138 static void mld_ifc_timer_expire(unsigned long data);
139 static void mld_ifc_event(struct inet6_dev *idev);
140 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
141 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
142 static void mld_clear_delrec(struct inet6_dev *idev);
143 static int sf_setstate(struct ifmcaddr6 *pmc);
144 static void sf_markstate(struct ifmcaddr6 *pmc);
145 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
146 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
147 int sfmode, int sfcount, struct in6_addr *psfsrc,
149 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
150 int sfmode, int sfcount, struct in6_addr *psfsrc,
152 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
153 struct inet6_dev *idev);
156 #define IGMP6_UNSOLICITED_IVAL (10*HZ)
157 #define MLD_QRV_DEFAULT 2
159 #define MLD_V1_SEEN(idev) (ipv6_devconf.force_mld_version == 1 || \
160 (idev)->cnf.force_mld_version == 1 || \
161 ((idev)->mc_v1_seen && \
162 time_before(jiffies, (idev)->mc_v1_seen)))
164 #define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
165 #define MLDV2_EXP(thresh, nbmant, nbexp, value) \
166 ((value) < (thresh) ? (value) : \
167 ((MLDV2_MASK(value, nbmant) | (1<<(nbmant))) << \
168 (MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp))))
170 #define MLDV2_QQIC(value) MLDV2_EXP(0x80, 4, 3, value)
171 #define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
173 #define IPV6_MLD_MAX_MSF 64
175 int sysctl_mld_max_msf = IPV6_MLD_MAX_MSF;
178 * socket join on multicast group
181 int ipv6_sock_mc_join(struct sock *sk, int ifindex, struct in6_addr *addr)
183 struct net_device *dev = NULL;
184 struct ipv6_mc_socklist *mc_lst;
185 struct ipv6_pinfo *np = inet6_sk(sk);
188 if (!ipv6_addr_is_multicast(addr))
191 read_lock_bh(&ipv6_sk_mc_lock);
192 for (mc_lst=np->ipv6_mc_list; mc_lst; mc_lst=mc_lst->next) {
193 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
194 ipv6_addr_equal(&mc_lst->addr, addr)) {
195 read_unlock_bh(&ipv6_sk_mc_lock);
199 read_unlock_bh(&ipv6_sk_mc_lock);
201 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
207 ipv6_addr_copy(&mc_lst->addr, addr);
211 rt = rt6_lookup(addr, NULL, 0, 0);
215 dst_release(&rt->u.dst);
218 dev = dev_get_by_index(ifindex);
221 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
225 mc_lst->ifindex = dev->ifindex;
226 mc_lst->sfmode = MCAST_EXCLUDE;
227 rwlock_init(&mc_lst->sflock);
228 mc_lst->sflist = NULL;
231 * now add/increase the group membership on the device
234 err = ipv6_dev_mc_inc(dev, addr);
237 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
242 write_lock_bh(&ipv6_sk_mc_lock);
243 mc_lst->next = np->ipv6_mc_list;
244 np->ipv6_mc_list = mc_lst;
245 write_unlock_bh(&ipv6_sk_mc_lock);
253 * socket leave on multicast group
255 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, struct in6_addr *addr)
257 struct ipv6_pinfo *np = inet6_sk(sk);
258 struct ipv6_mc_socklist *mc_lst, **lnk;
260 write_lock_bh(&ipv6_sk_mc_lock);
261 for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
262 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
263 ipv6_addr_equal(&mc_lst->addr, addr)) {
264 struct net_device *dev;
267 write_unlock_bh(&ipv6_sk_mc_lock);
269 if ((dev = dev_get_by_index(mc_lst->ifindex)) != NULL) {
270 struct inet6_dev *idev = in6_dev_get(dev);
273 (void) ip6_mc_leave_src(sk,mc_lst,idev);
274 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
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);
339 (void) ip6_mc_leave_src(sk, mc_lst, idev);
340 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
346 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
348 write_lock_bh(&ipv6_sk_mc_lock);
350 write_unlock_bh(&ipv6_sk_mc_lock);
353 int ip6_mc_source(int add, int omode, struct sock *sk,
354 struct group_source_req *pgsr)
356 struct in6_addr *source, *group;
357 struct ipv6_mc_socklist *pmc;
358 struct net_device *dev;
359 struct inet6_dev *idev;
360 struct ipv6_pinfo *inet6 = inet6_sk(sk);
361 struct ip6_sf_socklist *psl;
367 if (pgsr->gsr_group.ss_family != AF_INET6 ||
368 pgsr->gsr_source.ss_family != AF_INET6)
371 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
372 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
374 if (!ipv6_addr_is_multicast(group))
377 idev = ip6_mc_find_dev(group, pgsr->gsr_interface);
382 err = -EADDRNOTAVAIL;
384 read_lock_bh(&ipv6_sk_mc_lock);
385 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
386 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
388 if (ipv6_addr_equal(&pmc->addr, group))
391 if (!pmc) { /* must have a prior join */
395 /* if a source filter was set, must be the same mode as before */
397 if (pmc->sfmode != omode) {
401 } else if (pmc->sfmode != omode) {
402 /* allow mode switches for empty-set filters */
403 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
404 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
408 write_lock_bh(&pmc->sflock);
414 goto done; /* err = -EADDRNOTAVAIL */
416 for (i=0; i<psl->sl_count; i++) {
417 rv = memcmp(&psl->sl_addr[i], source,
418 sizeof(struct in6_addr));
422 if (rv) /* source not found */
423 goto done; /* err = -EADDRNOTAVAIL */
425 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
426 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
431 /* update the interface filter */
432 ip6_mc_del_src(idev, group, omode, 1, source, 1);
434 for (j=i+1; j<psl->sl_count; j++)
435 psl->sl_addr[j-1] = psl->sl_addr[j];
440 /* else, add a new source to the filter */
442 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
446 if (!psl || psl->sl_count == psl->sl_max) {
447 struct ip6_sf_socklist *newpsl;
448 int count = IP6_SFBLOCK;
451 count += psl->sl_max;
452 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
457 newpsl->sl_max = count;
458 newpsl->sl_count = count - IP6_SFBLOCK;
460 for (i=0; i<psl->sl_count; i++)
461 newpsl->sl_addr[i] = psl->sl_addr[i];
462 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
464 pmc->sflist = psl = newpsl;
466 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
467 for (i=0; i<psl->sl_count; i++) {
468 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
472 if (rv == 0) /* address already there is an error */
474 for (j=psl->sl_count-1; j>=i; j--)
475 psl->sl_addr[j+1] = psl->sl_addr[j];
476 psl->sl_addr[i] = *source;
479 /* update the interface list */
480 ip6_mc_add_src(idev, group, omode, 1, source, 1);
483 write_unlock_bh(&pmc->sflock);
484 read_unlock_bh(&ipv6_sk_mc_lock);
485 read_unlock_bh(&idev->lock);
489 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
493 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
495 struct in6_addr *group;
496 struct ipv6_mc_socklist *pmc;
497 struct net_device *dev;
498 struct inet6_dev *idev;
499 struct ipv6_pinfo *inet6 = inet6_sk(sk);
500 struct ip6_sf_socklist *newpsl, *psl;
504 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
506 if (!ipv6_addr_is_multicast(group))
508 if (gsf->gf_fmode != MCAST_INCLUDE &&
509 gsf->gf_fmode != MCAST_EXCLUDE)
512 idev = ip6_mc_find_dev(group, gsf->gf_interface);
519 read_lock_bh(&ipv6_sk_mc_lock);
521 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
526 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
527 if (pmc->ifindex != gsf->gf_interface)
529 if (ipv6_addr_equal(&pmc->addr, group))
532 if (!pmc) { /* must have a prior join */
536 if (gsf->gf_numsrc) {
537 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
543 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
544 for (i=0; i<newpsl->sl_count; ++i) {
545 struct sockaddr_in6 *psin6;
547 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
548 newpsl->sl_addr[i] = psin6->sin6_addr;
550 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
551 newpsl->sl_count, newpsl->sl_addr, 0);
553 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
558 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
561 write_lock_bh(&pmc->sflock);
564 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
565 psl->sl_count, psl->sl_addr, 0);
566 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
568 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
569 pmc->sflist = newpsl;
570 pmc->sfmode = gsf->gf_fmode;
571 write_unlock_bh(&pmc->sflock);
574 read_unlock_bh(&ipv6_sk_mc_lock);
575 read_unlock_bh(&idev->lock);
579 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
583 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
584 struct group_filter __user *optval, int __user *optlen)
586 int err, i, count, copycount;
587 struct in6_addr *group;
588 struct ipv6_mc_socklist *pmc;
589 struct inet6_dev *idev;
590 struct net_device *dev;
591 struct ipv6_pinfo *inet6 = inet6_sk(sk);
592 struct ip6_sf_socklist *psl;
594 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
596 if (!ipv6_addr_is_multicast(group))
599 idev = ip6_mc_find_dev(group, gsf->gf_interface);
606 err = -EADDRNOTAVAIL;
608 * changes to the ipv6_mc_list require the socket lock and
609 * a read lock on ip6_sk_mc_lock. We have the socket lock,
610 * so reading the list is safe.
613 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
614 if (pmc->ifindex != gsf->gf_interface)
616 if (ipv6_addr_equal(group, &pmc->addr))
619 if (!pmc) /* must have a prior join */
621 gsf->gf_fmode = pmc->sfmode;
623 count = psl ? psl->sl_count : 0;
624 read_unlock_bh(&idev->lock);
628 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
629 gsf->gf_numsrc = count;
630 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
631 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
634 /* changes to psl require the socket lock, a read lock on
635 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
636 * have the socket lock, so reading here is safe.
638 for (i=0; i<copycount; i++) {
639 struct sockaddr_in6 *psin6;
640 struct sockaddr_storage ss;
642 psin6 = (struct sockaddr_in6 *)&ss;
643 memset(&ss, 0, sizeof(ss));
644 psin6->sin6_family = AF_INET6;
645 psin6->sin6_addr = psl->sl_addr[i];
646 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
651 read_unlock_bh(&idev->lock);
657 int inet6_mc_check(struct sock *sk, struct in6_addr *mc_addr,
658 struct in6_addr *src_addr)
660 struct ipv6_pinfo *np = inet6_sk(sk);
661 struct ipv6_mc_socklist *mc;
662 struct ip6_sf_socklist *psl;
665 read_lock(&ipv6_sk_mc_lock);
666 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
667 if (ipv6_addr_equal(&mc->addr, mc_addr))
671 read_unlock(&ipv6_sk_mc_lock);
674 read_lock(&mc->sflock);
677 rv = mc->sfmode == MCAST_EXCLUDE;
681 for (i=0; i<psl->sl_count; i++) {
682 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
685 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
687 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
690 read_unlock(&mc->sflock);
691 read_unlock(&ipv6_sk_mc_lock);
696 static void ma_put(struct ifmcaddr6 *mc)
698 if (atomic_dec_and_test(&mc->mca_refcnt)) {
699 in6_dev_put(mc->idev);
704 static void igmp6_group_added(struct ifmcaddr6 *mc)
706 struct net_device *dev = mc->idev->dev;
707 char buf[MAX_ADDR_LEN];
709 spin_lock_bh(&mc->mca_lock);
710 if (!(mc->mca_flags&MAF_LOADED)) {
711 mc->mca_flags |= MAF_LOADED;
712 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
713 dev_mc_add(dev, buf, dev->addr_len, 0);
715 spin_unlock_bh(&mc->mca_lock);
717 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
720 if (MLD_V1_SEEN(mc->idev)) {
721 igmp6_join_group(mc);
726 mc->mca_crcount = mc->idev->mc_qrv;
727 mld_ifc_event(mc->idev);
730 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
732 struct net_device *dev = mc->idev->dev;
733 char buf[MAX_ADDR_LEN];
735 spin_lock_bh(&mc->mca_lock);
736 if (mc->mca_flags&MAF_LOADED) {
737 mc->mca_flags &= ~MAF_LOADED;
738 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
739 dev_mc_delete(dev, buf, dev->addr_len, 0);
742 if (mc->mca_flags & MAF_NOREPORT)
744 spin_unlock_bh(&mc->mca_lock);
747 igmp6_leave_group(mc);
749 spin_lock_bh(&mc->mca_lock);
750 if (del_timer(&mc->mca_timer))
751 atomic_dec(&mc->mca_refcnt);
753 ip6_mc_clear_src(mc);
754 spin_unlock_bh(&mc->mca_lock);
758 * deleted ifmcaddr6 manipulation
760 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
762 struct ifmcaddr6 *pmc;
764 /* this is an "ifmcaddr6" for convenience; only the fields below
765 * are actually used. In particular, the refcnt and users are not
766 * used for management of the delete list. Using the same structure
767 * for deleted items allows change reports to use common code with
768 * non-deleted or query-response MCA's.
770 pmc = kmalloc(sizeof(*pmc), GFP_ATOMIC);
773 memset(pmc, 0, sizeof(*pmc));
774 spin_lock_bh(&im->mca_lock);
775 spin_lock_init(&pmc->mca_lock);
776 pmc->idev = im->idev;
778 pmc->mca_addr = im->mca_addr;
779 pmc->mca_crcount = idev->mc_qrv;
780 pmc->mca_sfmode = im->mca_sfmode;
781 if (pmc->mca_sfmode == MCAST_INCLUDE) {
782 struct ip6_sf_list *psf;
784 pmc->mca_tomb = im->mca_tomb;
785 pmc->mca_sources = im->mca_sources;
786 im->mca_tomb = im->mca_sources = NULL;
787 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
788 psf->sf_crcount = pmc->mca_crcount;
790 spin_unlock_bh(&im->mca_lock);
792 write_lock_bh(&idev->mc_lock);
793 pmc->next = idev->mc_tomb;
795 write_unlock_bh(&idev->mc_lock);
798 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
800 struct ifmcaddr6 *pmc, *pmc_prev;
801 struct ip6_sf_list *psf, *psf_next;
803 write_lock_bh(&idev->mc_lock);
805 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
806 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
812 pmc_prev->next = pmc->next;
814 idev->mc_tomb = pmc->next;
816 write_unlock_bh(&idev->mc_lock);
818 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
819 psf_next = psf->sf_next;
822 in6_dev_put(pmc->idev);
827 static void mld_clear_delrec(struct inet6_dev *idev)
829 struct ifmcaddr6 *pmc, *nextpmc;
831 write_lock_bh(&idev->mc_lock);
833 idev->mc_tomb = NULL;
834 write_unlock_bh(&idev->mc_lock);
836 for (; pmc; pmc = nextpmc) {
838 ip6_mc_clear_src(pmc);
839 in6_dev_put(pmc->idev);
843 /* clear dead sources, too */
844 read_lock_bh(&idev->lock);
845 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
846 struct ip6_sf_list *psf, *psf_next;
848 spin_lock_bh(&pmc->mca_lock);
850 pmc->mca_tomb = NULL;
851 spin_unlock_bh(&pmc->mca_lock);
852 for (; psf; psf=psf_next) {
853 psf_next = psf->sf_next;
857 read_unlock_bh(&idev->lock);
862 * device multicast group inc (add if not found)
864 int ipv6_dev_mc_inc(struct net_device *dev, struct in6_addr *addr)
866 struct ifmcaddr6 *mc;
867 struct inet6_dev *idev;
869 idev = in6_dev_get(dev);
874 write_lock_bh(&idev->lock);
876 write_unlock_bh(&idev->lock);
881 for (mc = idev->mc_list; mc; mc = mc->next) {
882 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
884 write_unlock_bh(&idev->lock);
885 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
893 * not found: create a new one.
896 mc = kmalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
899 write_unlock_bh(&idev->lock);
904 memset(mc, 0, sizeof(struct ifmcaddr6));
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 = (struct icmp6hdr *)skb->h.raw;
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(skb->nh.ipv6h->payload_len) + sizeof(struct ipv6hdr);
1171 len -= (char *)skb->h.raw - (char *)skb->nh.ipv6h;
1173 /* Drop queries with not link local source */
1174 if (!(ipv6_addr_type(&skb->nh.ipv6h->saddr)&IPV6_ADDR_LINKLOCAL))
1177 idev = in6_dev_get(skb->dev);
1182 hdr = (struct icmp6hdr *) skb->h.raw;
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->h.raw;
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->h.raw;
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 = (struct icmp6hdr*) skb->h.raw;
1305 /* Drop reports with not link local source */
1306 addr_type = ipv6_addr_type(&skb->nh.ipv6h->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)) {
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 pmr =(struct mld2_report *)skb_put(skb, sizeof(*pmr));
1427 skb->h.raw = (unsigned char *)pmr;
1428 pmr->type = ICMPV6_MLD2_REPORT;
1436 static inline int mld_dev_queue_xmit2(struct sk_buff *skb)
1438 struct net_device *dev = skb->dev;
1440 if (dev->hard_header) {
1441 unsigned char ha[MAX_ADDR_LEN];
1444 ndisc_mc_map(&skb->nh.ipv6h->daddr, ha, dev, 1);
1445 err = dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, skb->len);
1451 return dev_queue_xmit(skb);
1454 static inline int mld_dev_queue_xmit(struct sk_buff *skb)
1456 return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dev,
1457 mld_dev_queue_xmit2);
1460 static void mld_sendpack(struct sk_buff *skb)
1462 struct ipv6hdr *pip6 = skb->nh.ipv6h;
1463 struct mld2_report *pmr = (struct mld2_report *)skb->h.raw;
1464 int payload_len, mldlen;
1465 struct inet6_dev *idev = in6_dev_get(skb->dev);
1468 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1469 payload_len = skb->tail - (unsigned char *)skb->nh.ipv6h -
1470 sizeof(struct ipv6hdr);
1471 mldlen = skb->tail - skb->h.raw;
1472 pip6->payload_len = htons(payload_len);
1474 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1475 IPPROTO_ICMPV6, csum_partial(skb->h.raw, mldlen, 0));
1476 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1477 mld_dev_queue_xmit);
1479 ICMP6_INC_STATS(idev,ICMP6_MIB_OUTMSGS);
1480 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1482 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1484 if (likely(idev != NULL))
1488 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1490 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1493 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1494 int type, struct mld2_grec **ppgr)
1496 struct net_device *dev = pmc->idev->dev;
1497 struct mld2_report *pmr;
1498 struct mld2_grec *pgr;
1501 skb = mld_newpack(dev, dev->mtu);
1504 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1505 pgr->grec_type = type;
1506 pgr->grec_auxwords = 0;
1507 pgr->grec_nsrcs = 0;
1508 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1509 pmr = (struct mld2_report *)skb->h.raw;
1510 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1515 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1516 skb_tailroom(skb)) : 0)
1518 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1519 int type, int gdeleted, int sdeleted)
1521 struct net_device *dev = pmc->idev->dev;
1522 struct mld2_report *pmr;
1523 struct mld2_grec *pgr = NULL;
1524 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1525 int scount, stotal, first, isquery, truncate;
1527 if (pmc->mca_flags & MAF_NOREPORT)
1530 isquery = type == MLD2_MODE_IS_INCLUDE ||
1531 type == MLD2_MODE_IS_EXCLUDE;
1532 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1533 type == MLD2_CHANGE_TO_EXCLUDE;
1535 stotal = scount = 0;
1537 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1542 pmr = skb ? (struct mld2_report *)skb->h.raw : NULL;
1544 /* EX and TO_EX get a fresh packet, if needed */
1546 if (pmr && pmr->ngrec &&
1547 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1550 skb = mld_newpack(dev, dev->mtu);
1555 for (psf=*psf_list; psf; psf=psf_next) {
1556 struct in6_addr *psrc;
1558 psf_next = psf->sf_next;
1560 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1565 /* clear marks on query responses */
1569 if (AVAILABLE(skb) < sizeof(*psrc) +
1570 first*sizeof(struct mld2_grec)) {
1571 if (truncate && !first)
1572 break; /* truncate these */
1574 pgr->grec_nsrcs = htons(scount);
1577 skb = mld_newpack(dev, dev->mtu);
1582 skb = add_grhead(skb, pmc, type, &pgr);
1585 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1586 *psrc = psf->sf_addr;
1588 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1589 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1591 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1593 psf_prev->sf_next = psf->sf_next;
1595 *psf_list = psf->sf_next;
1605 if (type == MLD2_ALLOW_NEW_SOURCES ||
1606 type == MLD2_BLOCK_OLD_SOURCES)
1608 if (pmc->mca_crcount || isquery) {
1609 /* make sure we have room for group header */
1610 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1612 skb = NULL; /* add_grhead will get a new one */
1614 skb = add_grhead(skb, pmc, type, &pgr);
1618 pgr->grec_nsrcs = htons(scount);
1621 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1625 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1627 struct sk_buff *skb = NULL;
1631 read_lock_bh(&idev->lock);
1632 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1633 if (pmc->mca_flags & MAF_NOREPORT)
1635 spin_lock_bh(&pmc->mca_lock);
1636 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1637 type = MLD2_MODE_IS_EXCLUDE;
1639 type = MLD2_MODE_IS_INCLUDE;
1640 skb = add_grec(skb, pmc, type, 0, 0);
1641 spin_unlock_bh(&pmc->mca_lock);
1643 read_unlock_bh(&idev->lock);
1645 spin_lock_bh(&pmc->mca_lock);
1646 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1647 type = MLD2_MODE_IS_EXCLUDE;
1649 type = MLD2_MODE_IS_INCLUDE;
1650 skb = add_grec(skb, pmc, type, 0, 0);
1651 spin_unlock_bh(&pmc->mca_lock);
1658 * remove zero-count source records from a source filter list
1660 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1662 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1665 for (psf=*ppsf; psf; psf = psf_next) {
1666 psf_next = psf->sf_next;
1667 if (psf->sf_crcount == 0) {
1669 psf_prev->sf_next = psf->sf_next;
1671 *ppsf = psf->sf_next;
1678 static void mld_send_cr(struct inet6_dev *idev)
1680 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1681 struct sk_buff *skb = NULL;
1684 read_lock_bh(&idev->lock);
1685 write_lock_bh(&idev->mc_lock);
1689 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1690 pmc_next = pmc->next;
1691 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1692 type = MLD2_BLOCK_OLD_SOURCES;
1693 dtype = MLD2_BLOCK_OLD_SOURCES;
1694 skb = add_grec(skb, pmc, type, 1, 0);
1695 skb = add_grec(skb, pmc, dtype, 1, 1);
1697 if (pmc->mca_crcount) {
1698 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1699 type = MLD2_CHANGE_TO_INCLUDE;
1700 skb = add_grec(skb, pmc, type, 1, 0);
1703 if (pmc->mca_crcount == 0) {
1704 mld_clear_zeros(&pmc->mca_tomb);
1705 mld_clear_zeros(&pmc->mca_sources);
1708 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1709 !pmc->mca_sources) {
1711 pmc_prev->next = pmc_next;
1713 idev->mc_tomb = pmc_next;
1714 in6_dev_put(pmc->idev);
1719 write_unlock_bh(&idev->mc_lock);
1722 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1723 spin_lock_bh(&pmc->mca_lock);
1724 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1725 type = MLD2_BLOCK_OLD_SOURCES;
1726 dtype = MLD2_ALLOW_NEW_SOURCES;
1728 type = MLD2_ALLOW_NEW_SOURCES;
1729 dtype = MLD2_BLOCK_OLD_SOURCES;
1731 skb = add_grec(skb, pmc, type, 0, 0);
1732 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1734 /* filter mode changes */
1735 if (pmc->mca_crcount) {
1736 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1737 type = MLD2_CHANGE_TO_EXCLUDE;
1739 type = MLD2_CHANGE_TO_INCLUDE;
1740 skb = add_grec(skb, pmc, type, 0, 0);
1743 spin_unlock_bh(&pmc->mca_lock);
1745 read_unlock_bh(&idev->lock);
1748 (void) mld_sendpack(skb);
1751 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1753 struct sock *sk = igmp6_socket->sk;
1754 struct inet6_dev *idev;
1755 struct sk_buff *skb;
1756 struct icmp6hdr *hdr;
1757 struct in6_addr *snd_addr;
1758 struct in6_addr *addrp;
1759 struct in6_addr addr_buf;
1760 struct in6_addr all_routers;
1761 int err, len, payload_len, full_len;
1762 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1763 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1766 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1768 if (type == ICMPV6_MGM_REDUCTION) {
1769 snd_addr = &all_routers;
1770 ipv6_addr_all_routers(&all_routers);
1773 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1774 payload_len = len + sizeof(ra);
1775 full_len = sizeof(struct ipv6hdr) + payload_len;
1777 skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1780 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1784 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1786 if (ipv6_get_lladdr(dev, &addr_buf)) {
1787 /* <draft-ietf-magma-mld-source-05.txt>:
1788 * use unspecified address as the source address
1789 * when a valid link-local address is not available.
1791 memset(&addr_buf, 0, sizeof(addr_buf));
1794 ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
1796 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1798 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1799 memset(hdr, 0, sizeof(struct icmp6hdr));
1800 hdr->icmp6_type = type;
1802 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1803 ipv6_addr_copy(addrp, addr);
1805 hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
1807 csum_partial((__u8 *) hdr, len, 0));
1809 idev = in6_dev_get(skb->dev);
1811 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1812 mld_dev_queue_xmit);
1814 if (type == ICMPV6_MGM_REDUCTION)
1815 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS);
1817 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBRESPONSES);
1818 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1819 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1821 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1823 if (likely(idev != NULL))
1828 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1829 struct in6_addr *psfsrc)
1831 struct ip6_sf_list *psf, *psf_prev;
1835 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1836 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1840 if (!psf || psf->sf_count[sfmode] == 0) {
1841 /* source filter not found, or count wrong => bug */
1844 psf->sf_count[sfmode]--;
1845 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1846 struct inet6_dev *idev = pmc->idev;
1848 /* no more filters for this source */
1850 psf_prev->sf_next = psf->sf_next;
1852 pmc->mca_sources = psf->sf_next;
1853 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1854 !MLD_V1_SEEN(idev)) {
1855 psf->sf_crcount = idev->mc_qrv;
1856 psf->sf_next = pmc->mca_tomb;
1857 pmc->mca_tomb = psf;
1865 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1866 int sfmode, int sfcount, struct in6_addr *psfsrc,
1869 struct ifmcaddr6 *pmc;
1875 read_lock_bh(&idev->lock);
1876 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1877 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1881 /* MCA not found?? bug */
1882 read_unlock_bh(&idev->lock);
1885 spin_lock_bh(&pmc->mca_lock);
1888 if (!pmc->mca_sfcount[sfmode]) {
1889 spin_unlock_bh(&pmc->mca_lock);
1890 read_unlock_bh(&idev->lock);
1893 pmc->mca_sfcount[sfmode]--;
1896 for (i=0; i<sfcount; i++) {
1897 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1899 changerec |= rv > 0;
1903 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1904 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1905 pmc->mca_sfcount[MCAST_INCLUDE]) {
1906 struct ip6_sf_list *psf;
1908 /* filter mode change */
1909 pmc->mca_sfmode = MCAST_INCLUDE;
1910 pmc->mca_crcount = idev->mc_qrv;
1911 idev->mc_ifc_count = pmc->mca_crcount;
1912 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1913 psf->sf_crcount = 0;
1914 mld_ifc_event(pmc->idev);
1915 } else if (sf_setstate(pmc) || changerec)
1916 mld_ifc_event(pmc->idev);
1917 spin_unlock_bh(&pmc->mca_lock);
1918 read_unlock_bh(&idev->lock);
1923 * Add multicast single-source filter to the interface list
1925 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1926 struct in6_addr *psfsrc, int delta)
1928 struct ip6_sf_list *psf, *psf_prev;
1931 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1932 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1937 psf = kmalloc(sizeof(*psf), GFP_ATOMIC);
1940 memset(psf, 0, sizeof(*psf));
1941 psf->sf_addr = *psfsrc;
1943 psf_prev->sf_next = psf;
1945 pmc->mca_sources = psf;
1947 psf->sf_count[sfmode]++;
1951 static void sf_markstate(struct ifmcaddr6 *pmc)
1953 struct ip6_sf_list *psf;
1954 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1956 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1957 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1958 psf->sf_oldin = mca_xcount ==
1959 psf->sf_count[MCAST_EXCLUDE] &&
1960 !psf->sf_count[MCAST_INCLUDE];
1962 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1965 static int sf_setstate(struct ifmcaddr6 *pmc)
1967 struct ip6_sf_list *psf, *dpsf;
1968 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1969 int qrv = pmc->idev->mc_qrv;
1973 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1974 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1975 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1976 !psf->sf_count[MCAST_INCLUDE];
1978 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1980 if (!psf->sf_oldin) {
1981 struct ip6_sf_list *prev = NULL;
1983 for (dpsf=pmc->mca_tomb; dpsf;
1984 dpsf=dpsf->sf_next) {
1985 if (ipv6_addr_equal(&dpsf->sf_addr,
1992 prev->sf_next = dpsf->sf_next;
1994 pmc->mca_tomb = dpsf->sf_next;
1997 psf->sf_crcount = qrv;
2000 } else if (psf->sf_oldin) {
2001 psf->sf_crcount = 0;
2003 * add or update "delete" records if an active filter
2006 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2007 if (ipv6_addr_equal(&dpsf->sf_addr,
2011 dpsf = (struct ip6_sf_list *)
2012 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2016 /* pmc->mca_lock held by callers */
2017 dpsf->sf_next = pmc->mca_tomb;
2018 pmc->mca_tomb = dpsf;
2020 dpsf->sf_crcount = qrv;
2028 * Add multicast source filter list to the interface list
2030 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
2031 int sfmode, int sfcount, struct in6_addr *psfsrc,
2034 struct ifmcaddr6 *pmc;
2040 read_lock_bh(&idev->lock);
2041 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2042 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2046 /* MCA not found?? bug */
2047 read_unlock_bh(&idev->lock);
2050 spin_lock_bh(&pmc->mca_lock);
2053 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2055 pmc->mca_sfcount[sfmode]++;
2057 for (i=0; i<sfcount; i++) {
2058 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
2066 pmc->mca_sfcount[sfmode]--;
2068 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2069 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2070 struct inet6_dev *idev = pmc->idev;
2071 struct ip6_sf_list *psf;
2073 /* filter mode change */
2074 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2075 pmc->mca_sfmode = MCAST_EXCLUDE;
2076 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2077 pmc->mca_sfmode = MCAST_INCLUDE;
2078 /* else no filters; keep old mode for reports */
2080 pmc->mca_crcount = idev->mc_qrv;
2081 idev->mc_ifc_count = pmc->mca_crcount;
2082 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2083 psf->sf_crcount = 0;
2084 mld_ifc_event(idev);
2085 } else if (sf_setstate(pmc))
2086 mld_ifc_event(idev);
2087 spin_unlock_bh(&pmc->mca_lock);
2088 read_unlock_bh(&idev->lock);
2092 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2094 struct ip6_sf_list *psf, *nextpsf;
2096 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2097 nextpsf = psf->sf_next;
2100 pmc->mca_tomb = NULL;
2101 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2102 nextpsf = psf->sf_next;
2105 pmc->mca_sources = NULL;
2106 pmc->mca_sfmode = MCAST_EXCLUDE;
2107 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2108 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2112 static void igmp6_join_group(struct ifmcaddr6 *ma)
2114 unsigned long delay;
2116 if (ma->mca_flags & MAF_NOREPORT)
2119 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2121 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2123 spin_lock_bh(&ma->mca_lock);
2124 if (del_timer(&ma->mca_timer)) {
2125 atomic_dec(&ma->mca_refcnt);
2126 delay = ma->mca_timer.expires - jiffies;
2129 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2130 atomic_inc(&ma->mca_refcnt);
2131 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2132 spin_unlock_bh(&ma->mca_lock);
2135 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2136 struct inet6_dev *idev)
2140 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2141 * so no other readers or writers of iml or its sflist
2143 if (iml->sflist == 0) {
2144 /* any-source empty exclude case */
2145 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2147 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2148 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2149 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2154 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2156 if (MLD_V1_SEEN(ma->idev)) {
2157 if (ma->mca_flags & MAF_LAST_REPORTER)
2158 igmp6_send(&ma->mca_addr, ma->idev->dev,
2159 ICMPV6_MGM_REDUCTION);
2161 mld_add_delrec(ma->idev, ma);
2162 mld_ifc_event(ma->idev);
2166 static void mld_gq_timer_expire(unsigned long data)
2168 struct inet6_dev *idev = (struct inet6_dev *)data;
2170 idev->mc_gq_running = 0;
2171 mld_send_report(idev, NULL);
2172 __in6_dev_put(idev);
2175 static void mld_ifc_timer_expire(unsigned long data)
2177 struct inet6_dev *idev = (struct inet6_dev *)data;
2180 if (idev->mc_ifc_count) {
2181 idev->mc_ifc_count--;
2182 if (idev->mc_ifc_count)
2183 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2185 __in6_dev_put(idev);
2188 static void mld_ifc_event(struct inet6_dev *idev)
2190 if (MLD_V1_SEEN(idev))
2192 idev->mc_ifc_count = idev->mc_qrv;
2193 mld_ifc_start_timer(idev, 1);
2197 static void igmp6_timer_handler(unsigned long data)
2199 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2201 if (MLD_V1_SEEN(ma->idev))
2202 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2204 mld_send_report(ma->idev, ma);
2206 spin_lock(&ma->mca_lock);
2207 ma->mca_flags |= MAF_LAST_REPORTER;
2208 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2209 spin_unlock(&ma->mca_lock);
2213 /* Device going down */
2215 void ipv6_mc_down(struct inet6_dev *idev)
2217 struct ifmcaddr6 *i;
2219 /* Withdraw multicast list */
2221 read_lock_bh(&idev->lock);
2222 idev->mc_ifc_count = 0;
2223 if (del_timer(&idev->mc_ifc_timer))
2224 __in6_dev_put(idev);
2225 idev->mc_gq_running = 0;
2226 if (del_timer(&idev->mc_gq_timer))
2227 __in6_dev_put(idev);
2229 for (i = idev->mc_list; i; i=i->next)
2230 igmp6_group_dropped(i);
2231 read_unlock_bh(&idev->lock);
2233 mld_clear_delrec(idev);
2237 /* Device going up */
2239 void ipv6_mc_up(struct inet6_dev *idev)
2241 struct ifmcaddr6 *i;
2243 /* Install multicast list, except for all-nodes (already installed) */
2245 read_lock_bh(&idev->lock);
2246 for (i = idev->mc_list; i; i=i->next)
2247 igmp6_group_added(i);
2248 read_unlock_bh(&idev->lock);
2251 /* IPv6 device initialization. */
2253 void ipv6_mc_init_dev(struct inet6_dev *idev)
2255 struct in6_addr maddr;
2257 write_lock_bh(&idev->lock);
2258 rwlock_init(&idev->mc_lock);
2259 idev->mc_gq_running = 0;
2260 init_timer(&idev->mc_gq_timer);
2261 idev->mc_gq_timer.data = (unsigned long) idev;
2262 idev->mc_gq_timer.function = &mld_gq_timer_expire;
2263 idev->mc_tomb = NULL;
2264 idev->mc_ifc_count = 0;
2265 init_timer(&idev->mc_ifc_timer);
2266 idev->mc_ifc_timer.data = (unsigned long) idev;
2267 idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
2268 idev->mc_qrv = MLD_QRV_DEFAULT;
2269 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2270 idev->mc_v1_seen = 0;
2271 write_unlock_bh(&idev->lock);
2273 /* Add all-nodes address. */
2274 ipv6_addr_all_nodes(&maddr);
2275 ipv6_dev_mc_inc(idev->dev, &maddr);
2279 * Device is about to be destroyed: clean up.
2282 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2284 struct ifmcaddr6 *i;
2285 struct in6_addr maddr;
2287 /* Deactivate timers */
2290 /* Delete all-nodes address. */
2291 ipv6_addr_all_nodes(&maddr);
2293 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2294 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2297 __ipv6_dev_mc_dec(idev, &maddr);
2299 if (idev->cnf.forwarding) {
2300 ipv6_addr_all_routers(&maddr);
2301 __ipv6_dev_mc_dec(idev, &maddr);
2304 write_lock_bh(&idev->lock);
2305 while ((i = idev->mc_list) != NULL) {
2306 idev->mc_list = i->next;
2307 write_unlock_bh(&idev->lock);
2309 igmp6_group_dropped(i);
2312 write_lock_bh(&idev->lock);
2314 write_unlock_bh(&idev->lock);
2317 #ifdef CONFIG_PROC_FS
2318 struct igmp6_mc_iter_state {
2319 struct net_device *dev;
2320 struct inet6_dev *idev;
2323 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2325 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2327 struct ifmcaddr6 *im = NULL;
2328 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2330 for (state->dev = dev_base, state->idev = NULL;
2332 state->dev = state->dev->next) {
2333 struct inet6_dev *idev;
2334 idev = in6_dev_get(state->dev);
2337 read_lock_bh(&idev->lock);
2343 read_unlock_bh(&idev->lock);
2349 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2351 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2355 if (likely(state->idev != NULL)) {
2356 read_unlock_bh(&state->idev->lock);
2357 in6_dev_put(state->idev);
2359 state->dev = state->dev->next;
2364 state->idev = in6_dev_get(state->dev);
2367 read_lock_bh(&state->idev->lock);
2368 im = state->idev->mc_list;
2373 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2375 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2377 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2379 return pos ? NULL : im;
2382 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2384 read_lock(&dev_base_lock);
2385 return igmp6_mc_get_idx(seq, *pos);
2388 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2390 struct ifmcaddr6 *im;
2391 im = igmp6_mc_get_next(seq, v);
2396 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2398 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2399 if (likely(state->idev != NULL)) {
2400 read_unlock_bh(&state->idev->lock);
2401 in6_dev_put(state->idev);
2405 read_unlock(&dev_base_lock);
2408 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2410 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2411 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2414 "%-4d %-15s " NIP6_SEQFMT " %5d %08X %ld\n",
2415 state->dev->ifindex, state->dev->name,
2417 im->mca_users, im->mca_flags,
2418 (im->mca_flags&MAF_TIMER_RUNNING) ?
2419 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2423 static struct seq_operations igmp6_mc_seq_ops = {
2424 .start = igmp6_mc_seq_start,
2425 .next = igmp6_mc_seq_next,
2426 .stop = igmp6_mc_seq_stop,
2427 .show = igmp6_mc_seq_show,
2430 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2432 struct seq_file *seq;
2434 struct igmp6_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2439 rc = seq_open(file, &igmp6_mc_seq_ops);
2443 seq = file->private_data;
2445 memset(s, 0, sizeof(*s));
2453 static struct file_operations igmp6_mc_seq_fops = {
2454 .owner = THIS_MODULE,
2455 .open = igmp6_mc_seq_open,
2457 .llseek = seq_lseek,
2458 .release = seq_release_private,
2461 struct igmp6_mcf_iter_state {
2462 struct net_device *dev;
2463 struct inet6_dev *idev;
2464 struct ifmcaddr6 *im;
2467 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2469 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2471 struct ip6_sf_list *psf = NULL;
2472 struct ifmcaddr6 *im = NULL;
2473 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2475 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2477 state->dev = state->dev->next) {
2478 struct inet6_dev *idev;
2479 idev = in6_dev_get(state->dev);
2480 if (unlikely(idev == NULL))
2482 read_lock_bh(&idev->lock);
2484 if (likely(im != NULL)) {
2485 spin_lock_bh(&im->mca_lock);
2486 psf = im->mca_sources;
2487 if (likely(psf != NULL)) {
2492 spin_unlock_bh(&im->mca_lock);
2494 read_unlock_bh(&idev->lock);
2500 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2502 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2506 spin_unlock_bh(&state->im->mca_lock);
2507 state->im = state->im->next;
2508 while (!state->im) {
2509 if (likely(state->idev != NULL)) {
2510 read_unlock_bh(&state->idev->lock);
2511 in6_dev_put(state->idev);
2513 state->dev = state->dev->next;
2518 state->idev = in6_dev_get(state->dev);
2521 read_lock_bh(&state->idev->lock);
2522 state->im = state->idev->mc_list;
2526 spin_lock_bh(&state->im->mca_lock);
2527 psf = state->im->mca_sources;
2533 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2535 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2537 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2539 return pos ? NULL : psf;
2542 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2544 read_lock(&dev_base_lock);
2545 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2548 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2550 struct ip6_sf_list *psf;
2551 if (v == SEQ_START_TOKEN)
2552 psf = igmp6_mcf_get_first(seq);
2554 psf = igmp6_mcf_get_next(seq, v);
2559 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2561 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2562 if (likely(state->im != NULL)) {
2563 spin_unlock_bh(&state->im->mca_lock);
2566 if (likely(state->idev != NULL)) {
2567 read_unlock_bh(&state->idev->lock);
2568 in6_dev_put(state->idev);
2572 read_unlock(&dev_base_lock);
2575 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2577 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2578 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2580 if (v == SEQ_START_TOKEN) {
2583 "%32s %32s %6s %6s\n", "Idx",
2584 "Device", "Multicast Address",
2585 "Source Address", "INC", "EXC");
2588 "%3d %6.6s " NIP6_SEQFMT " " NIP6_SEQFMT " %6lu %6lu\n",
2589 state->dev->ifindex, state->dev->name,
2590 NIP6(state->im->mca_addr),
2592 psf->sf_count[MCAST_INCLUDE],
2593 psf->sf_count[MCAST_EXCLUDE]);
2598 static struct seq_operations igmp6_mcf_seq_ops = {
2599 .start = igmp6_mcf_seq_start,
2600 .next = igmp6_mcf_seq_next,
2601 .stop = igmp6_mcf_seq_stop,
2602 .show = igmp6_mcf_seq_show,
2605 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2607 struct seq_file *seq;
2609 struct igmp6_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2614 rc = seq_open(file, &igmp6_mcf_seq_ops);
2618 seq = file->private_data;
2620 memset(s, 0, sizeof(*s));
2628 static struct file_operations igmp6_mcf_seq_fops = {
2629 .owner = THIS_MODULE,
2630 .open = igmp6_mcf_seq_open,
2632 .llseek = seq_lseek,
2633 .release = seq_release_private,
2637 int __init igmp6_init(struct net_proto_family *ops)
2639 struct ipv6_pinfo *np;
2643 err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
2646 "Failed to initialize the IGMP6 control socket (err %d).\n",
2648 igmp6_socket = NULL; /* For safety. */
2652 sk = igmp6_socket->sk;
2653 sk->sk_allocation = GFP_ATOMIC;
2654 sk->sk_prot->unhash(sk);
2659 #ifdef CONFIG_PROC_FS
2660 proc_net_fops_create("igmp6", S_IRUGO, &igmp6_mc_seq_fops);
2661 proc_net_fops_create("mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
2667 void igmp6_cleanup(void)
2669 sock_release(igmp6_socket);
2670 igmp6_socket = NULL; /* for safety */
2672 #ifdef CONFIG_PROC_FS
2673 proc_net_remove("mcfilter6");
2674 proc_net_remove("igmp6");