2 * Multicast support for IPv6
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
18 * yoshfuji : fix format of router-alert option
19 * YOSHIFUJI Hideaki @USAGI:
20 * Fixed source address for MLD message based on
21 * <draft-ietf-magma-mld-source-05.txt>.
22 * YOSHIFUJI Hideaki @USAGI:
23 * - Ignore Queries for invalid addresses.
24 * - MLD for link-local addresses.
25 * David L Stevens <dlstevens@us.ibm.com>:
29 #include <linux/module.h>
30 #include <linux/errno.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/socket.h>
34 #include <linux/sockios.h>
35 #include <linux/jiffies.h>
36 #include <linux/times.h>
37 #include <linux/net.h>
39 #include <linux/in6.h>
40 #include <linux/netdevice.h>
41 #include <linux/if_arp.h>
42 #include <linux/route.h>
43 #include <linux/init.h>
44 #include <linux/proc_fs.h>
45 #include <linux/seq_file.h>
47 #include <linux/netfilter.h>
48 #include <linux/netfilter_ipv6.h>
50 #include <net/net_namespace.h>
55 #include <net/protocol.h>
56 #include <net/if_inet6.h>
57 #include <net/ndisc.h>
58 #include <net/addrconf.h>
59 #include <net/ip6_route.h>
60 #include <net/inet_common.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 void igmp6_join_group(struct ifmcaddr6 *ma);
129 static void igmp6_leave_group(struct ifmcaddr6 *ma);
130 static void igmp6_timer_handler(unsigned long data);
132 static void mld_gq_timer_expire(unsigned long data);
133 static void mld_ifc_timer_expire(unsigned long data);
134 static void mld_ifc_event(struct inet6_dev *idev);
135 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
136 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
137 static void mld_clear_delrec(struct inet6_dev *idev);
138 static int sf_setstate(struct ifmcaddr6 *pmc);
139 static void sf_markstate(struct ifmcaddr6 *pmc);
140 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
141 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
142 int sfmode, int sfcount, struct in6_addr *psfsrc,
144 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
145 int sfmode, int sfcount, struct in6_addr *psfsrc,
147 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
148 struct inet6_dev *idev);
151 #define IGMP6_UNSOLICITED_IVAL (10*HZ)
152 #define MLD_QRV_DEFAULT 2
154 #define MLD_V1_SEEN(idev) (ipv6_devconf.force_mld_version == 1 || \
155 (idev)->cnf.force_mld_version == 1 || \
156 ((idev)->mc_v1_seen && \
157 time_before(jiffies, (idev)->mc_v1_seen)))
159 #define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
160 #define MLDV2_EXP(thresh, nbmant, nbexp, value) \
161 ((value) < (thresh) ? (value) : \
162 ((MLDV2_MASK(value, nbmant) | (1<<(nbmant))) << \
163 (MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp))))
165 #define MLDV2_QQIC(value) MLDV2_EXP(0x80, 4, 3, value)
166 #define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
168 #define IPV6_MLD_MAX_MSF 64
170 int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
173 * socket join on multicast group
176 int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
178 struct net_device *dev = NULL;
179 struct ipv6_mc_socklist *mc_lst;
180 struct ipv6_pinfo *np = inet6_sk(sk);
181 struct net *net = sock_net(sk);
184 if (!ipv6_addr_is_multicast(addr))
187 read_lock_bh(&ipv6_sk_mc_lock);
188 for (mc_lst=np->ipv6_mc_list; mc_lst; mc_lst=mc_lst->next) {
189 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
190 ipv6_addr_equal(&mc_lst->addr, addr)) {
191 read_unlock_bh(&ipv6_sk_mc_lock);
195 read_unlock_bh(&ipv6_sk_mc_lock);
197 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
203 ipv6_addr_copy(&mc_lst->addr, addr);
207 rt = rt6_lookup(net, addr, NULL, 0, 0);
211 dst_release(&rt->u.dst);
214 dev = dev_get_by_index(net, ifindex);
217 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
221 mc_lst->ifindex = dev->ifindex;
222 mc_lst->sfmode = MCAST_EXCLUDE;
223 rwlock_init(&mc_lst->sflock);
224 mc_lst->sflist = NULL;
227 * now add/increase the group membership on the device
230 err = ipv6_dev_mc_inc(dev, addr);
233 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
238 write_lock_bh(&ipv6_sk_mc_lock);
239 mc_lst->next = np->ipv6_mc_list;
240 np->ipv6_mc_list = mc_lst;
241 write_unlock_bh(&ipv6_sk_mc_lock);
249 * socket leave on multicast group
251 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
253 struct ipv6_pinfo *np = inet6_sk(sk);
254 struct ipv6_mc_socklist *mc_lst, **lnk;
255 struct net *net = sock_net(sk);
257 write_lock_bh(&ipv6_sk_mc_lock);
258 for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
259 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
260 ipv6_addr_equal(&mc_lst->addr, addr)) {
261 struct net_device *dev;
264 write_unlock_bh(&ipv6_sk_mc_lock);
266 dev = dev_get_by_index(net, mc_lst->ifindex);
268 struct inet6_dev *idev = in6_dev_get(dev);
270 (void) ip6_mc_leave_src(sk, mc_lst, idev);
272 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
277 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
278 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
282 write_unlock_bh(&ipv6_sk_mc_lock);
284 return -EADDRNOTAVAIL;
287 static struct inet6_dev *ip6_mc_find_dev(struct net *net,
288 struct in6_addr *group,
291 struct net_device *dev = NULL;
292 struct inet6_dev *idev = NULL;
297 rt = rt6_lookup(net, group, NULL, 0, 0);
301 dst_release(&rt->u.dst);
304 dev = dev_get_by_index(net, ifindex);
308 idev = in6_dev_get(dev);
313 read_lock_bh(&idev->lock);
315 read_unlock_bh(&idev->lock);
323 void ipv6_sock_mc_close(struct sock *sk)
325 struct ipv6_pinfo *np = inet6_sk(sk);
326 struct ipv6_mc_socklist *mc_lst;
327 struct net *net = sock_net(sk);
329 write_lock_bh(&ipv6_sk_mc_lock);
330 while ((mc_lst = np->ipv6_mc_list) != NULL) {
331 struct net_device *dev;
333 np->ipv6_mc_list = mc_lst->next;
334 write_unlock_bh(&ipv6_sk_mc_lock);
336 dev = dev_get_by_index(net, mc_lst->ifindex);
338 struct inet6_dev *idev = in6_dev_get(dev);
340 (void) ip6_mc_leave_src(sk, mc_lst, idev);
342 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
347 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
349 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
351 write_lock_bh(&ipv6_sk_mc_lock);
353 write_unlock_bh(&ipv6_sk_mc_lock);
356 int ip6_mc_source(int add, int omode, struct sock *sk,
357 struct group_source_req *pgsr)
359 struct in6_addr *source, *group;
360 struct ipv6_mc_socklist *pmc;
361 struct net_device *dev;
362 struct inet6_dev *idev;
363 struct ipv6_pinfo *inet6 = inet6_sk(sk);
364 struct ip6_sf_socklist *psl;
365 struct net *net = sock_net(sk);
371 if (pgsr->gsr_group.ss_family != AF_INET6 ||
372 pgsr->gsr_source.ss_family != AF_INET6)
375 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
376 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
378 if (!ipv6_addr_is_multicast(group))
381 idev = ip6_mc_find_dev(net, group, pgsr->gsr_interface);
386 err = -EADDRNOTAVAIL;
388 read_lock_bh(&ipv6_sk_mc_lock);
389 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
390 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
392 if (ipv6_addr_equal(&pmc->addr, group))
395 if (!pmc) { /* must have a prior join */
399 /* if a source filter was set, must be the same mode as before */
401 if (pmc->sfmode != omode) {
405 } else if (pmc->sfmode != omode) {
406 /* allow mode switches for empty-set filters */
407 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
408 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
412 write_lock_bh(&pmc->sflock);
418 goto done; /* err = -EADDRNOTAVAIL */
420 for (i=0; i<psl->sl_count; i++) {
421 rv = memcmp(&psl->sl_addr[i], source,
422 sizeof(struct in6_addr));
426 if (rv) /* source not found */
427 goto done; /* err = -EADDRNOTAVAIL */
429 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
430 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
435 /* update the interface filter */
436 ip6_mc_del_src(idev, group, omode, 1, source, 1);
438 for (j=i+1; j<psl->sl_count; j++)
439 psl->sl_addr[j-1] = psl->sl_addr[j];
444 /* else, add a new source to the filter */
446 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
450 if (!psl || psl->sl_count == psl->sl_max) {
451 struct ip6_sf_socklist *newpsl;
452 int count = IP6_SFBLOCK;
455 count += psl->sl_max;
456 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
461 newpsl->sl_max = count;
462 newpsl->sl_count = count - IP6_SFBLOCK;
464 for (i=0; i<psl->sl_count; i++)
465 newpsl->sl_addr[i] = psl->sl_addr[i];
466 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
468 pmc->sflist = psl = newpsl;
470 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
471 for (i=0; i<psl->sl_count; i++) {
472 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
476 if (rv == 0) /* address already there is an error */
478 for (j=psl->sl_count-1; j>=i; j--)
479 psl->sl_addr[j+1] = psl->sl_addr[j];
480 psl->sl_addr[i] = *source;
483 /* update the interface list */
484 ip6_mc_add_src(idev, group, omode, 1, source, 1);
487 write_unlock_bh(&pmc->sflock);
488 read_unlock_bh(&ipv6_sk_mc_lock);
489 read_unlock_bh(&idev->lock);
493 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
497 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
499 struct in6_addr *group;
500 struct ipv6_mc_socklist *pmc;
501 struct net_device *dev;
502 struct inet6_dev *idev;
503 struct ipv6_pinfo *inet6 = inet6_sk(sk);
504 struct ip6_sf_socklist *newpsl, *psl;
505 struct net *net = sock_net(sk);
509 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
511 if (!ipv6_addr_is_multicast(group))
513 if (gsf->gf_fmode != MCAST_INCLUDE &&
514 gsf->gf_fmode != MCAST_EXCLUDE)
517 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
524 read_lock_bh(&ipv6_sk_mc_lock);
526 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
531 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
532 if (pmc->ifindex != gsf->gf_interface)
534 if (ipv6_addr_equal(&pmc->addr, group))
537 if (!pmc) { /* must have a prior join */
541 if (gsf->gf_numsrc) {
542 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
548 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
549 for (i=0; i<newpsl->sl_count; ++i) {
550 struct sockaddr_in6 *psin6;
552 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
553 newpsl->sl_addr[i] = psin6->sin6_addr;
555 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
556 newpsl->sl_count, newpsl->sl_addr, 0);
558 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
563 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
566 write_lock_bh(&pmc->sflock);
569 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
570 psl->sl_count, psl->sl_addr, 0);
571 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
573 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
574 pmc->sflist = newpsl;
575 pmc->sfmode = gsf->gf_fmode;
576 write_unlock_bh(&pmc->sflock);
579 read_unlock_bh(&ipv6_sk_mc_lock);
580 read_unlock_bh(&idev->lock);
584 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
588 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
589 struct group_filter __user *optval, int __user *optlen)
591 int err, i, count, copycount;
592 struct in6_addr *group;
593 struct ipv6_mc_socklist *pmc;
594 struct inet6_dev *idev;
595 struct net_device *dev;
596 struct ipv6_pinfo *inet6 = inet6_sk(sk);
597 struct ip6_sf_socklist *psl;
598 struct net *net = sock_net(sk);
600 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
602 if (!ipv6_addr_is_multicast(group))
605 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
612 err = -EADDRNOTAVAIL;
614 * changes to the ipv6_mc_list require the socket lock and
615 * a read lock on ip6_sk_mc_lock. We have the socket lock,
616 * so reading the list is safe.
619 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
620 if (pmc->ifindex != gsf->gf_interface)
622 if (ipv6_addr_equal(group, &pmc->addr))
625 if (!pmc) /* must have a prior join */
627 gsf->gf_fmode = pmc->sfmode;
629 count = psl ? psl->sl_count : 0;
630 read_unlock_bh(&idev->lock);
634 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
635 gsf->gf_numsrc = count;
636 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
637 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
640 /* changes to psl require the socket lock, a read lock on
641 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
642 * have the socket lock, so reading here is safe.
644 for (i=0; i<copycount; i++) {
645 struct sockaddr_in6 *psin6;
646 struct sockaddr_storage ss;
648 psin6 = (struct sockaddr_in6 *)&ss;
649 memset(&ss, 0, sizeof(ss));
650 psin6->sin6_family = AF_INET6;
651 psin6->sin6_addr = psl->sl_addr[i];
652 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
657 read_unlock_bh(&idev->lock);
663 int inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
664 const struct in6_addr *src_addr)
666 struct ipv6_pinfo *np = inet6_sk(sk);
667 struct ipv6_mc_socklist *mc;
668 struct ip6_sf_socklist *psl;
671 read_lock(&ipv6_sk_mc_lock);
672 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
673 if (ipv6_addr_equal(&mc->addr, mc_addr))
677 read_unlock(&ipv6_sk_mc_lock);
680 read_lock(&mc->sflock);
683 rv = mc->sfmode == MCAST_EXCLUDE;
687 for (i=0; i<psl->sl_count; i++) {
688 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
691 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
693 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
696 read_unlock(&mc->sflock);
697 read_unlock(&ipv6_sk_mc_lock);
702 static void ma_put(struct ifmcaddr6 *mc)
704 if (atomic_dec_and_test(&mc->mca_refcnt)) {
705 in6_dev_put(mc->idev);
710 static void igmp6_group_added(struct ifmcaddr6 *mc)
712 struct net_device *dev = mc->idev->dev;
713 char buf[MAX_ADDR_LEN];
715 spin_lock_bh(&mc->mca_lock);
716 if (!(mc->mca_flags&MAF_LOADED)) {
717 mc->mca_flags |= MAF_LOADED;
718 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
719 dev_mc_add(dev, buf, dev->addr_len, 0);
721 spin_unlock_bh(&mc->mca_lock);
723 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
726 if (MLD_V1_SEEN(mc->idev)) {
727 igmp6_join_group(mc);
732 mc->mca_crcount = mc->idev->mc_qrv;
733 mld_ifc_event(mc->idev);
736 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
738 struct net_device *dev = mc->idev->dev;
739 char buf[MAX_ADDR_LEN];
741 spin_lock_bh(&mc->mca_lock);
742 if (mc->mca_flags&MAF_LOADED) {
743 mc->mca_flags &= ~MAF_LOADED;
744 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
745 dev_mc_delete(dev, buf, dev->addr_len, 0);
748 if (mc->mca_flags & MAF_NOREPORT)
750 spin_unlock_bh(&mc->mca_lock);
753 igmp6_leave_group(mc);
755 spin_lock_bh(&mc->mca_lock);
756 if (del_timer(&mc->mca_timer))
757 atomic_dec(&mc->mca_refcnt);
759 ip6_mc_clear_src(mc);
760 spin_unlock_bh(&mc->mca_lock);
764 * deleted ifmcaddr6 manipulation
766 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
768 struct ifmcaddr6 *pmc;
770 /* this is an "ifmcaddr6" for convenience; only the fields below
771 * are actually used. In particular, the refcnt and users are not
772 * used for management of the delete list. Using the same structure
773 * for deleted items allows change reports to use common code with
774 * non-deleted or query-response MCA's.
776 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
780 spin_lock_bh(&im->mca_lock);
781 spin_lock_init(&pmc->mca_lock);
782 pmc->idev = im->idev;
784 pmc->mca_addr = im->mca_addr;
785 pmc->mca_crcount = idev->mc_qrv;
786 pmc->mca_sfmode = im->mca_sfmode;
787 if (pmc->mca_sfmode == MCAST_INCLUDE) {
788 struct ip6_sf_list *psf;
790 pmc->mca_tomb = im->mca_tomb;
791 pmc->mca_sources = im->mca_sources;
792 im->mca_tomb = im->mca_sources = NULL;
793 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
794 psf->sf_crcount = pmc->mca_crcount;
796 spin_unlock_bh(&im->mca_lock);
798 write_lock_bh(&idev->mc_lock);
799 pmc->next = idev->mc_tomb;
801 write_unlock_bh(&idev->mc_lock);
804 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
806 struct ifmcaddr6 *pmc, *pmc_prev;
807 struct ip6_sf_list *psf, *psf_next;
809 write_lock_bh(&idev->mc_lock);
811 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
812 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
818 pmc_prev->next = pmc->next;
820 idev->mc_tomb = pmc->next;
822 write_unlock_bh(&idev->mc_lock);
824 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
825 psf_next = psf->sf_next;
828 in6_dev_put(pmc->idev);
833 static void mld_clear_delrec(struct inet6_dev *idev)
835 struct ifmcaddr6 *pmc, *nextpmc;
837 write_lock_bh(&idev->mc_lock);
839 idev->mc_tomb = NULL;
840 write_unlock_bh(&idev->mc_lock);
842 for (; pmc; pmc = nextpmc) {
844 ip6_mc_clear_src(pmc);
845 in6_dev_put(pmc->idev);
849 /* clear dead sources, too */
850 read_lock_bh(&idev->lock);
851 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
852 struct ip6_sf_list *psf, *psf_next;
854 spin_lock_bh(&pmc->mca_lock);
856 pmc->mca_tomb = NULL;
857 spin_unlock_bh(&pmc->mca_lock);
858 for (; psf; psf=psf_next) {
859 psf_next = psf->sf_next;
863 read_unlock_bh(&idev->lock);
868 * device multicast group inc (add if not found)
870 int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
872 struct ifmcaddr6 *mc;
873 struct inet6_dev *idev;
875 idev = in6_dev_get(dev);
880 write_lock_bh(&idev->lock);
882 write_unlock_bh(&idev->lock);
887 for (mc = idev->mc_list; mc; mc = mc->next) {
888 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
890 write_unlock_bh(&idev->lock);
891 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
899 * not found: create a new one.
902 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
905 write_unlock_bh(&idev->lock);
910 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
912 ipv6_addr_copy(&mc->mca_addr, addr);
915 /* mca_stamp should be updated upon changes */
916 mc->mca_cstamp = mc->mca_tstamp = jiffies;
917 atomic_set(&mc->mca_refcnt, 2);
918 spin_lock_init(&mc->mca_lock);
920 /* initial mode is (EX, empty) */
921 mc->mca_sfmode = MCAST_EXCLUDE;
922 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
924 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
925 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
926 mc->mca_flags |= MAF_NOREPORT;
928 mc->next = idev->mc_list;
930 write_unlock_bh(&idev->lock);
932 mld_del_delrec(idev, &mc->mca_addr);
933 igmp6_group_added(mc);
939 * device multicast group del
941 int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
943 struct ifmcaddr6 *ma, **map;
945 write_lock_bh(&idev->lock);
946 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
947 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
948 if (--ma->mca_users == 0) {
950 write_unlock_bh(&idev->lock);
952 igmp6_group_dropped(ma);
957 write_unlock_bh(&idev->lock);
961 write_unlock_bh(&idev->lock);
966 int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
968 struct inet6_dev *idev = in6_dev_get(dev);
974 err = __ipv6_dev_mc_dec(idev, addr);
982 * identify MLD packets for MLD filter exceptions
984 int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
986 struct icmp6hdr *pic;
988 if (nexthdr != IPPROTO_ICMPV6)
991 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
994 pic = icmp6_hdr(skb);
996 switch (pic->icmp6_type) {
997 case ICMPV6_MGM_QUERY:
998 case ICMPV6_MGM_REPORT:
999 case ICMPV6_MGM_REDUCTION:
1000 case ICMPV6_MLD2_REPORT:
1009 * check if the interface/address pair is valid
1011 int ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
1012 const struct in6_addr *src_addr)
1014 struct inet6_dev *idev;
1015 struct ifmcaddr6 *mc;
1018 idev = in6_dev_get(dev);
1020 read_lock_bh(&idev->lock);
1021 for (mc = idev->mc_list; mc; mc=mc->next) {
1022 if (ipv6_addr_equal(&mc->mca_addr, group))
1026 if (src_addr && !ipv6_addr_any(src_addr)) {
1027 struct ip6_sf_list *psf;
1029 spin_lock_bh(&mc->mca_lock);
1030 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1031 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1035 rv = psf->sf_count[MCAST_INCLUDE] ||
1036 psf->sf_count[MCAST_EXCLUDE] !=
1037 mc->mca_sfcount[MCAST_EXCLUDE];
1039 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1040 spin_unlock_bh(&mc->mca_lock);
1042 rv = 1; /* don't filter unspecified source */
1044 read_unlock_bh(&idev->lock);
1050 static void mld_gq_start_timer(struct inet6_dev *idev)
1052 int tv = net_random() % idev->mc_maxdelay;
1054 idev->mc_gq_running = 1;
1055 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1059 static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1061 int tv = net_random() % delay;
1063 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1068 * IGMP handling (alias multicast ICMPv6 messages)
1071 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1073 unsigned long delay = resptime;
1075 /* Do not start timer for these addresses */
1076 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1077 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1080 if (del_timer(&ma->mca_timer)) {
1081 atomic_dec(&ma->mca_refcnt);
1082 delay = ma->mca_timer.expires - jiffies;
1085 if (delay >= resptime) {
1087 delay = net_random() % resptime;
1091 ma->mca_timer.expires = jiffies + delay;
1092 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1093 atomic_inc(&ma->mca_refcnt);
1094 ma->mca_flags |= MAF_TIMER_RUNNING;
1097 /* mark EXCLUDE-mode sources */
1098 static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1099 struct in6_addr *srcs)
1101 struct ip6_sf_list *psf;
1105 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1106 if (scount == nsrcs)
1108 for (i=0; i<nsrcs; i++) {
1109 /* skip inactive filters */
1110 if (pmc->mca_sfcount[MCAST_INCLUDE] ||
1111 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1112 psf->sf_count[MCAST_EXCLUDE])
1114 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1120 pmc->mca_flags &= ~MAF_GSQUERY;
1121 if (scount == nsrcs) /* all sources excluded */
1126 static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1127 struct in6_addr *srcs)
1129 struct ip6_sf_list *psf;
1132 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1133 return mld_xmarksources(pmc, nsrcs, srcs);
1135 /* mark INCLUDE-mode sources */
1138 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1139 if (scount == nsrcs)
1141 for (i=0; i<nsrcs; i++) {
1142 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1150 pmc->mca_flags &= ~MAF_GSQUERY;
1153 pmc->mca_flags |= MAF_GSQUERY;
1157 int igmp6_event_query(struct sk_buff *skb)
1159 struct mld2_query *mlh2 = NULL;
1160 struct ifmcaddr6 *ma;
1161 struct in6_addr *group;
1162 unsigned long max_delay;
1163 struct inet6_dev *idev;
1164 struct icmp6hdr *hdr;
1169 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1172 /* compute payload length excluding extension headers */
1173 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
1174 len -= skb_network_header_len(skb);
1176 /* Drop queries with not link local source */
1177 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
1180 idev = in6_dev_get(skb->dev);
1185 hdr = icmp6_hdr(skb);
1186 group = (struct in6_addr *) (hdr + 1);
1187 group_type = ipv6_addr_type(group);
1189 if (group_type != IPV6_ADDR_ANY &&
1190 !(group_type&IPV6_ADDR_MULTICAST)) {
1197 /* MLDv1 router present */
1199 /* Translate milliseconds to jiffies */
1200 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1202 switchback = (idev->mc_qrv + 1) * max_delay;
1203 idev->mc_v1_seen = jiffies + switchback;
1205 /* cancel the interface change timer */
1206 idev->mc_ifc_count = 0;
1207 if (del_timer(&idev->mc_ifc_timer))
1208 __in6_dev_put(idev);
1209 /* clear deleted report items */
1210 mld_clear_delrec(idev);
1211 } else if (len >= 28) {
1212 int srcs_offset = sizeof(struct mld2_query) -
1213 sizeof(struct icmp6hdr);
1214 if (!pskb_may_pull(skb, srcs_offset)) {
1218 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1219 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1222 idev->mc_maxdelay = max_delay;
1224 idev->mc_qrv = mlh2->qrv;
1225 if (group_type == IPV6_ADDR_ANY) { /* general query */
1228 return -EINVAL; /* no sources allowed */
1230 mld_gq_start_timer(idev);
1234 /* mark sources to include, if group & source-specific */
1235 if (mlh2->nsrcs != 0) {
1236 if (!pskb_may_pull(skb, srcs_offset +
1237 ntohs(mlh2->nsrcs) * sizeof(struct in6_addr))) {
1241 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1249 read_lock_bh(&idev->lock);
1250 if (group_type == IPV6_ADDR_ANY) {
1251 for (ma = idev->mc_list; ma; ma=ma->next) {
1252 spin_lock_bh(&ma->mca_lock);
1253 igmp6_group_queried(ma, max_delay);
1254 spin_unlock_bh(&ma->mca_lock);
1257 for (ma = idev->mc_list; ma; ma=ma->next) {
1258 if (!ipv6_addr_equal(group, &ma->mca_addr))
1260 spin_lock_bh(&ma->mca_lock);
1261 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1262 /* gsquery <- gsquery && mark */
1264 ma->mca_flags &= ~MAF_GSQUERY;
1266 /* gsquery <- mark */
1268 ma->mca_flags |= MAF_GSQUERY;
1270 ma->mca_flags &= ~MAF_GSQUERY;
1272 if (!(ma->mca_flags & MAF_GSQUERY) ||
1273 mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
1274 igmp6_group_queried(ma, max_delay);
1275 spin_unlock_bh(&ma->mca_lock);
1279 read_unlock_bh(&idev->lock);
1286 int igmp6_event_report(struct sk_buff *skb)
1288 struct ifmcaddr6 *ma;
1289 struct in6_addr *addrp;
1290 struct inet6_dev *idev;
1291 struct icmp6hdr *hdr;
1294 /* Our own report looped back. Ignore it. */
1295 if (skb->pkt_type == PACKET_LOOPBACK)
1298 /* send our report if the MC router may not have heard this report */
1299 if (skb->pkt_type != PACKET_MULTICAST &&
1300 skb->pkt_type != PACKET_BROADCAST)
1303 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1306 hdr = icmp6_hdr(skb);
1308 /* Drop reports with not link local source */
1309 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
1310 if (addr_type != IPV6_ADDR_ANY &&
1311 !(addr_type&IPV6_ADDR_LINKLOCAL))
1314 addrp = (struct in6_addr *) (hdr + 1);
1316 idev = in6_dev_get(skb->dev);
1321 * Cancel the timer for this group
1324 read_lock_bh(&idev->lock);
1325 for (ma = idev->mc_list; ma; ma=ma->next) {
1326 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1327 spin_lock(&ma->mca_lock);
1328 if (del_timer(&ma->mca_timer))
1329 atomic_dec(&ma->mca_refcnt);
1330 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1331 spin_unlock(&ma->mca_lock);
1335 read_unlock_bh(&idev->lock);
1340 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1341 int gdeleted, int sdeleted)
1344 case MLD2_MODE_IS_INCLUDE:
1345 case MLD2_MODE_IS_EXCLUDE:
1346 if (gdeleted || sdeleted)
1348 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1349 if (pmc->mca_sfmode == MCAST_INCLUDE)
1351 /* don't include if this source is excluded
1354 if (psf->sf_count[MCAST_INCLUDE])
1355 return type == MLD2_MODE_IS_INCLUDE;
1356 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1357 psf->sf_count[MCAST_EXCLUDE];
1360 case MLD2_CHANGE_TO_INCLUDE:
1361 if (gdeleted || sdeleted)
1363 return psf->sf_count[MCAST_INCLUDE] != 0;
1364 case MLD2_CHANGE_TO_EXCLUDE:
1365 if (gdeleted || sdeleted)
1367 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1368 psf->sf_count[MCAST_INCLUDE])
1370 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1371 psf->sf_count[MCAST_EXCLUDE];
1372 case MLD2_ALLOW_NEW_SOURCES:
1373 if (gdeleted || !psf->sf_crcount)
1375 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1376 case MLD2_BLOCK_OLD_SOURCES:
1377 if (pmc->mca_sfmode == MCAST_INCLUDE)
1378 return gdeleted || (psf->sf_crcount && sdeleted);
1379 return psf->sf_crcount && !gdeleted && !sdeleted;
1385 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1387 struct ip6_sf_list *psf;
1390 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1391 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1398 static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1400 struct net *net = dev_net(dev);
1401 struct sock *sk = net->ipv6.igmp_sk;
1402 struct sk_buff *skb;
1403 struct mld2_report *pmr;
1404 struct in6_addr addr_buf;
1405 const struct in6_addr *saddr;
1407 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1408 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1411 /* we assume size > sizeof(ra) here */
1412 skb = sock_alloc_send_skb(sk, size + LL_ALLOCATED_SPACE(dev), 1, &err);
1417 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1419 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1420 /* <draft-ietf-magma-mld-source-05.txt>:
1421 * use unspecified address as the source address
1422 * when a valid link-local address is not available.
1424 saddr = &in6addr_any;
1428 ip6_nd_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
1430 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1432 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
1433 skb_put(skb, sizeof(*pmr));
1434 pmr = (struct mld2_report *)skb_transport_header(skb);
1435 pmr->type = ICMPV6_MLD2_REPORT;
1443 static void mld_sendpack(struct sk_buff *skb)
1445 struct ipv6hdr *pip6 = ipv6_hdr(skb);
1446 struct mld2_report *pmr =
1447 (struct mld2_report *)skb_transport_header(skb);
1448 int payload_len, mldlen;
1449 struct inet6_dev *idev = in6_dev_get(skb->dev);
1450 struct net *net = dev_net(skb->dev);
1454 IP6_INC_STATS(idev, IPSTATS_MIB_OUTREQUESTS);
1455 payload_len = (skb->tail - skb->network_header) - sizeof(*pip6);
1456 mldlen = skb->tail - skb->transport_header;
1457 pip6->payload_len = htons(payload_len);
1459 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1460 IPPROTO_ICMPV6, csum_partial(skb_transport_header(skb),
1463 skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1470 icmpv6_flow_init(net->ipv6.igmp_sk, &fl, ICMPV6_MLD2_REPORT,
1471 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1474 err = xfrm_lookup(&skb->dst, &fl, NULL, 0);
1478 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1482 ICMP6MSGOUT_INC_STATS_BH(idev, ICMPV6_MLD2_REPORT);
1483 ICMP6_INC_STATS_BH(idev, ICMP6_MIB_OUTMSGS);
1484 IP6_INC_STATS_BH(idev, IPSTATS_MIB_OUTMCASTPKTS);
1486 IP6_INC_STATS_BH(idev, IPSTATS_MIB_OUTDISCARDS);
1488 if (likely(idev != NULL))
1497 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1499 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1502 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1503 int type, struct mld2_grec **ppgr)
1505 struct net_device *dev = pmc->idev->dev;
1506 struct mld2_report *pmr;
1507 struct mld2_grec *pgr;
1510 skb = mld_newpack(dev, dev->mtu);
1513 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1514 pgr->grec_type = type;
1515 pgr->grec_auxwords = 0;
1516 pgr->grec_nsrcs = 0;
1517 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1518 pmr = (struct mld2_report *)skb_transport_header(skb);
1519 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1524 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1525 skb_tailroom(skb)) : 0)
1527 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1528 int type, int gdeleted, int sdeleted)
1530 struct net_device *dev = pmc->idev->dev;
1531 struct mld2_report *pmr;
1532 struct mld2_grec *pgr = NULL;
1533 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1534 int scount, stotal, first, isquery, truncate;
1536 if (pmc->mca_flags & MAF_NOREPORT)
1539 isquery = type == MLD2_MODE_IS_INCLUDE ||
1540 type == MLD2_MODE_IS_EXCLUDE;
1541 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1542 type == MLD2_CHANGE_TO_EXCLUDE;
1544 stotal = scount = 0;
1546 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1551 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
1553 /* EX and TO_EX get a fresh packet, if needed */
1555 if (pmr && pmr->ngrec &&
1556 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1559 skb = mld_newpack(dev, dev->mtu);
1564 for (psf=*psf_list; psf; psf=psf_next) {
1565 struct in6_addr *psrc;
1567 psf_next = psf->sf_next;
1569 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1574 /* clear marks on query responses */
1578 if (AVAILABLE(skb) < sizeof(*psrc) +
1579 first*sizeof(struct mld2_grec)) {
1580 if (truncate && !first)
1581 break; /* truncate these */
1583 pgr->grec_nsrcs = htons(scount);
1586 skb = mld_newpack(dev, dev->mtu);
1591 skb = add_grhead(skb, pmc, type, &pgr);
1596 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1597 *psrc = psf->sf_addr;
1599 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1600 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1602 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1604 psf_prev->sf_next = psf->sf_next;
1606 *psf_list = psf->sf_next;
1616 if (type == MLD2_ALLOW_NEW_SOURCES ||
1617 type == MLD2_BLOCK_OLD_SOURCES)
1619 if (pmc->mca_crcount || isquery) {
1620 /* make sure we have room for group header */
1621 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1623 skb = NULL; /* add_grhead will get a new one */
1625 skb = add_grhead(skb, pmc, type, &pgr);
1629 pgr->grec_nsrcs = htons(scount);
1632 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1636 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1638 struct sk_buff *skb = NULL;
1642 read_lock_bh(&idev->lock);
1643 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1644 if (pmc->mca_flags & MAF_NOREPORT)
1646 spin_lock_bh(&pmc->mca_lock);
1647 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1648 type = MLD2_MODE_IS_EXCLUDE;
1650 type = MLD2_MODE_IS_INCLUDE;
1651 skb = add_grec(skb, pmc, type, 0, 0);
1652 spin_unlock_bh(&pmc->mca_lock);
1654 read_unlock_bh(&idev->lock);
1656 spin_lock_bh(&pmc->mca_lock);
1657 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1658 type = MLD2_MODE_IS_EXCLUDE;
1660 type = MLD2_MODE_IS_INCLUDE;
1661 skb = add_grec(skb, pmc, type, 0, 0);
1662 spin_unlock_bh(&pmc->mca_lock);
1669 * remove zero-count source records from a source filter list
1671 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1673 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1676 for (psf=*ppsf; psf; psf = psf_next) {
1677 psf_next = psf->sf_next;
1678 if (psf->sf_crcount == 0) {
1680 psf_prev->sf_next = psf->sf_next;
1682 *ppsf = psf->sf_next;
1689 static void mld_send_cr(struct inet6_dev *idev)
1691 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1692 struct sk_buff *skb = NULL;
1695 read_lock_bh(&idev->lock);
1696 write_lock_bh(&idev->mc_lock);
1700 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1701 pmc_next = pmc->next;
1702 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1703 type = MLD2_BLOCK_OLD_SOURCES;
1704 dtype = MLD2_BLOCK_OLD_SOURCES;
1705 skb = add_grec(skb, pmc, type, 1, 0);
1706 skb = add_grec(skb, pmc, dtype, 1, 1);
1708 if (pmc->mca_crcount) {
1709 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1710 type = MLD2_CHANGE_TO_INCLUDE;
1711 skb = add_grec(skb, pmc, type, 1, 0);
1714 if (pmc->mca_crcount == 0) {
1715 mld_clear_zeros(&pmc->mca_tomb);
1716 mld_clear_zeros(&pmc->mca_sources);
1719 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1720 !pmc->mca_sources) {
1722 pmc_prev->next = pmc_next;
1724 idev->mc_tomb = pmc_next;
1725 in6_dev_put(pmc->idev);
1730 write_unlock_bh(&idev->mc_lock);
1733 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1734 spin_lock_bh(&pmc->mca_lock);
1735 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1736 type = MLD2_BLOCK_OLD_SOURCES;
1737 dtype = MLD2_ALLOW_NEW_SOURCES;
1739 type = MLD2_ALLOW_NEW_SOURCES;
1740 dtype = MLD2_BLOCK_OLD_SOURCES;
1742 skb = add_grec(skb, pmc, type, 0, 0);
1743 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1745 /* filter mode changes */
1746 if (pmc->mca_crcount) {
1747 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1748 type = MLD2_CHANGE_TO_EXCLUDE;
1750 type = MLD2_CHANGE_TO_INCLUDE;
1751 skb = add_grec(skb, pmc, type, 0, 0);
1754 spin_unlock_bh(&pmc->mca_lock);
1756 read_unlock_bh(&idev->lock);
1759 (void) mld_sendpack(skb);
1762 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1764 struct net *net = dev_net(dev);
1765 struct sock *sk = net->ipv6.igmp_sk;
1766 struct inet6_dev *idev;
1767 struct sk_buff *skb;
1768 struct icmp6hdr *hdr;
1769 const struct in6_addr *snd_addr, *saddr;
1770 struct in6_addr *addrp;
1771 struct in6_addr addr_buf;
1772 int err, len, payload_len, full_len;
1773 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1774 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1779 IP6_INC_STATS(__in6_dev_get(dev),
1780 IPSTATS_MIB_OUTREQUESTS);
1782 if (type == ICMPV6_MGM_REDUCTION)
1783 snd_addr = &in6addr_linklocal_allrouters;
1787 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1788 payload_len = len + sizeof(ra);
1789 full_len = sizeof(struct ipv6hdr) + payload_len;
1791 skb = sock_alloc_send_skb(sk, LL_ALLOCATED_SPACE(dev) + full_len, 1, &err);
1795 IP6_INC_STATS(__in6_dev_get(dev),
1796 IPSTATS_MIB_OUTDISCARDS);
1801 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1803 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1804 /* <draft-ietf-magma-mld-source-05.txt>:
1805 * use unspecified address as the source address
1806 * when a valid link-local address is not available.
1808 saddr = &in6addr_any;
1812 ip6_nd_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
1814 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1816 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1817 memset(hdr, 0, sizeof(struct icmp6hdr));
1818 hdr->icmp6_type = type;
1820 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1821 ipv6_addr_copy(addrp, addr);
1823 hdr->icmp6_cksum = csum_ipv6_magic(saddr, snd_addr, len,
1825 csum_partial((__u8 *) hdr, len, 0));
1827 idev = in6_dev_get(skb->dev);
1829 skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1835 icmpv6_flow_init(sk, &fl, type,
1836 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1839 err = xfrm_lookup(&skb->dst, &fl, NULL, 0);
1843 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1847 ICMP6MSGOUT_INC_STATS(idev, type);
1848 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1849 IP6_INC_STATS(idev, IPSTATS_MIB_OUTMCASTPKTS);
1851 IP6_INC_STATS(idev, IPSTATS_MIB_OUTDISCARDS);
1853 if (likely(idev != NULL))
1862 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1863 struct in6_addr *psfsrc)
1865 struct ip6_sf_list *psf, *psf_prev;
1869 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1870 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1874 if (!psf || psf->sf_count[sfmode] == 0) {
1875 /* source filter not found, or count wrong => bug */
1878 psf->sf_count[sfmode]--;
1879 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1880 struct inet6_dev *idev = pmc->idev;
1882 /* no more filters for this source */
1884 psf_prev->sf_next = psf->sf_next;
1886 pmc->mca_sources = psf->sf_next;
1887 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1888 !MLD_V1_SEEN(idev)) {
1889 psf->sf_crcount = idev->mc_qrv;
1890 psf->sf_next = pmc->mca_tomb;
1891 pmc->mca_tomb = psf;
1899 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1900 int sfmode, int sfcount, struct in6_addr *psfsrc,
1903 struct ifmcaddr6 *pmc;
1909 read_lock_bh(&idev->lock);
1910 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1911 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1915 /* MCA not found?? bug */
1916 read_unlock_bh(&idev->lock);
1919 spin_lock_bh(&pmc->mca_lock);
1922 if (!pmc->mca_sfcount[sfmode]) {
1923 spin_unlock_bh(&pmc->mca_lock);
1924 read_unlock_bh(&idev->lock);
1927 pmc->mca_sfcount[sfmode]--;
1930 for (i=0; i<sfcount; i++) {
1931 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1933 changerec |= rv > 0;
1937 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1938 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1939 pmc->mca_sfcount[MCAST_INCLUDE]) {
1940 struct ip6_sf_list *psf;
1942 /* filter mode change */
1943 pmc->mca_sfmode = MCAST_INCLUDE;
1944 pmc->mca_crcount = idev->mc_qrv;
1945 idev->mc_ifc_count = pmc->mca_crcount;
1946 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1947 psf->sf_crcount = 0;
1948 mld_ifc_event(pmc->idev);
1949 } else if (sf_setstate(pmc) || changerec)
1950 mld_ifc_event(pmc->idev);
1951 spin_unlock_bh(&pmc->mca_lock);
1952 read_unlock_bh(&idev->lock);
1957 * Add multicast single-source filter to the interface list
1959 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1960 struct in6_addr *psfsrc, int delta)
1962 struct ip6_sf_list *psf, *psf_prev;
1965 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1966 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1971 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1975 psf->sf_addr = *psfsrc;
1977 psf_prev->sf_next = psf;
1979 pmc->mca_sources = psf;
1981 psf->sf_count[sfmode]++;
1985 static void sf_markstate(struct ifmcaddr6 *pmc)
1987 struct ip6_sf_list *psf;
1988 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1990 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1991 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1992 psf->sf_oldin = mca_xcount ==
1993 psf->sf_count[MCAST_EXCLUDE] &&
1994 !psf->sf_count[MCAST_INCLUDE];
1996 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1999 static int sf_setstate(struct ifmcaddr6 *pmc)
2001 struct ip6_sf_list *psf, *dpsf;
2002 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2003 int qrv = pmc->idev->mc_qrv;
2007 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
2008 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2009 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2010 !psf->sf_count[MCAST_INCLUDE];
2012 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2014 if (!psf->sf_oldin) {
2015 struct ip6_sf_list *prev = NULL;
2017 for (dpsf=pmc->mca_tomb; dpsf;
2018 dpsf=dpsf->sf_next) {
2019 if (ipv6_addr_equal(&dpsf->sf_addr,
2026 prev->sf_next = dpsf->sf_next;
2028 pmc->mca_tomb = dpsf->sf_next;
2031 psf->sf_crcount = qrv;
2034 } else if (psf->sf_oldin) {
2035 psf->sf_crcount = 0;
2037 * add or update "delete" records if an active filter
2040 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2041 if (ipv6_addr_equal(&dpsf->sf_addr,
2045 dpsf = (struct ip6_sf_list *)
2046 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2050 /* pmc->mca_lock held by callers */
2051 dpsf->sf_next = pmc->mca_tomb;
2052 pmc->mca_tomb = dpsf;
2054 dpsf->sf_crcount = qrv;
2062 * Add multicast source filter list to the interface list
2064 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
2065 int sfmode, int sfcount, struct in6_addr *psfsrc,
2068 struct ifmcaddr6 *pmc;
2074 read_lock_bh(&idev->lock);
2075 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2076 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2080 /* MCA not found?? bug */
2081 read_unlock_bh(&idev->lock);
2084 spin_lock_bh(&pmc->mca_lock);
2087 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2089 pmc->mca_sfcount[sfmode]++;
2091 for (i=0; i<sfcount; i++) {
2092 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
2100 pmc->mca_sfcount[sfmode]--;
2102 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2103 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2104 struct inet6_dev *idev = pmc->idev;
2105 struct ip6_sf_list *psf;
2107 /* filter mode change */
2108 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2109 pmc->mca_sfmode = MCAST_EXCLUDE;
2110 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2111 pmc->mca_sfmode = MCAST_INCLUDE;
2112 /* else no filters; keep old mode for reports */
2114 pmc->mca_crcount = idev->mc_qrv;
2115 idev->mc_ifc_count = pmc->mca_crcount;
2116 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2117 psf->sf_crcount = 0;
2118 mld_ifc_event(idev);
2119 } else if (sf_setstate(pmc))
2120 mld_ifc_event(idev);
2121 spin_unlock_bh(&pmc->mca_lock);
2122 read_unlock_bh(&idev->lock);
2126 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2128 struct ip6_sf_list *psf, *nextpsf;
2130 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2131 nextpsf = psf->sf_next;
2134 pmc->mca_tomb = NULL;
2135 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2136 nextpsf = psf->sf_next;
2139 pmc->mca_sources = NULL;
2140 pmc->mca_sfmode = MCAST_EXCLUDE;
2141 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2142 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2146 static void igmp6_join_group(struct ifmcaddr6 *ma)
2148 unsigned long delay;
2150 if (ma->mca_flags & MAF_NOREPORT)
2153 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2155 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2157 spin_lock_bh(&ma->mca_lock);
2158 if (del_timer(&ma->mca_timer)) {
2159 atomic_dec(&ma->mca_refcnt);
2160 delay = ma->mca_timer.expires - jiffies;
2163 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2164 atomic_inc(&ma->mca_refcnt);
2165 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2166 spin_unlock_bh(&ma->mca_lock);
2169 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2170 struct inet6_dev *idev)
2174 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2175 * so no other readers or writers of iml or its sflist
2178 /* any-source empty exclude case */
2179 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2181 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2182 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2183 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2188 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2190 if (MLD_V1_SEEN(ma->idev)) {
2191 if (ma->mca_flags & MAF_LAST_REPORTER)
2192 igmp6_send(&ma->mca_addr, ma->idev->dev,
2193 ICMPV6_MGM_REDUCTION);
2195 mld_add_delrec(ma->idev, ma);
2196 mld_ifc_event(ma->idev);
2200 static void mld_gq_timer_expire(unsigned long data)
2202 struct inet6_dev *idev = (struct inet6_dev *)data;
2204 idev->mc_gq_running = 0;
2205 mld_send_report(idev, NULL);
2206 __in6_dev_put(idev);
2209 static void mld_ifc_timer_expire(unsigned long data)
2211 struct inet6_dev *idev = (struct inet6_dev *)data;
2214 if (idev->mc_ifc_count) {
2215 idev->mc_ifc_count--;
2216 if (idev->mc_ifc_count)
2217 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2219 __in6_dev_put(idev);
2222 static void mld_ifc_event(struct inet6_dev *idev)
2224 if (MLD_V1_SEEN(idev))
2226 idev->mc_ifc_count = idev->mc_qrv;
2227 mld_ifc_start_timer(idev, 1);
2231 static void igmp6_timer_handler(unsigned long data)
2233 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2235 if (MLD_V1_SEEN(ma->idev))
2236 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2238 mld_send_report(ma->idev, ma);
2240 spin_lock(&ma->mca_lock);
2241 ma->mca_flags |= MAF_LAST_REPORTER;
2242 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2243 spin_unlock(&ma->mca_lock);
2247 /* Device going down */
2249 void ipv6_mc_down(struct inet6_dev *idev)
2251 struct ifmcaddr6 *i;
2253 /* Withdraw multicast list */
2255 read_lock_bh(&idev->lock);
2256 idev->mc_ifc_count = 0;
2257 if (del_timer(&idev->mc_ifc_timer))
2258 __in6_dev_put(idev);
2259 idev->mc_gq_running = 0;
2260 if (del_timer(&idev->mc_gq_timer))
2261 __in6_dev_put(idev);
2263 for (i = idev->mc_list; i; i=i->next)
2264 igmp6_group_dropped(i);
2265 read_unlock_bh(&idev->lock);
2267 mld_clear_delrec(idev);
2271 /* Device going up */
2273 void ipv6_mc_up(struct inet6_dev *idev)
2275 struct ifmcaddr6 *i;
2277 /* Install multicast list, except for all-nodes (already installed) */
2279 read_lock_bh(&idev->lock);
2280 for (i = idev->mc_list; i; i=i->next)
2281 igmp6_group_added(i);
2282 read_unlock_bh(&idev->lock);
2285 /* IPv6 device initialization. */
2287 void ipv6_mc_init_dev(struct inet6_dev *idev)
2289 write_lock_bh(&idev->lock);
2290 rwlock_init(&idev->mc_lock);
2291 idev->mc_gq_running = 0;
2292 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2293 (unsigned long)idev);
2294 idev->mc_tomb = NULL;
2295 idev->mc_ifc_count = 0;
2296 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2297 (unsigned long)idev);
2298 idev->mc_qrv = MLD_QRV_DEFAULT;
2299 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2300 idev->mc_v1_seen = 0;
2301 write_unlock_bh(&idev->lock);
2305 * Device is about to be destroyed: clean up.
2308 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2310 struct ifmcaddr6 *i;
2312 /* Deactivate timers */
2315 /* Delete all-nodes address. */
2316 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2317 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2320 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
2322 if (idev->cnf.forwarding)
2323 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
2325 write_lock_bh(&idev->lock);
2326 while ((i = idev->mc_list) != NULL) {
2327 idev->mc_list = i->next;
2328 write_unlock_bh(&idev->lock);
2330 igmp6_group_dropped(i);
2333 write_lock_bh(&idev->lock);
2335 write_unlock_bh(&idev->lock);
2338 #ifdef CONFIG_PROC_FS
2339 struct igmp6_mc_iter_state {
2340 struct seq_net_private p;
2341 struct net_device *dev;
2342 struct inet6_dev *idev;
2345 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2347 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2349 struct ifmcaddr6 *im = NULL;
2350 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2351 struct net *net = seq_file_net(seq);
2354 for_each_netdev(net, state->dev) {
2355 struct inet6_dev *idev;
2356 idev = in6_dev_get(state->dev);
2359 read_lock_bh(&idev->lock);
2365 read_unlock_bh(&idev->lock);
2371 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2373 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2377 if (likely(state->idev != NULL)) {
2378 read_unlock_bh(&state->idev->lock);
2379 in6_dev_put(state->idev);
2381 state->dev = next_net_device(state->dev);
2386 state->idev = in6_dev_get(state->dev);
2389 read_lock_bh(&state->idev->lock);
2390 im = state->idev->mc_list;
2395 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2397 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2399 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2401 return pos ? NULL : im;
2404 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2405 __acquires(dev_base_lock)
2407 read_lock(&dev_base_lock);
2408 return igmp6_mc_get_idx(seq, *pos);
2411 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2413 struct ifmcaddr6 *im;
2414 im = igmp6_mc_get_next(seq, v);
2419 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2420 __releases(dev_base_lock)
2422 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2423 if (likely(state->idev != NULL)) {
2424 read_unlock_bh(&state->idev->lock);
2425 in6_dev_put(state->idev);
2429 read_unlock(&dev_base_lock);
2432 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2434 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2435 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2438 "%-4d %-15s " NIP6_SEQFMT " %5d %08X %ld\n",
2439 state->dev->ifindex, state->dev->name,
2441 im->mca_users, im->mca_flags,
2442 (im->mca_flags&MAF_TIMER_RUNNING) ?
2443 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2447 static const struct seq_operations igmp6_mc_seq_ops = {
2448 .start = igmp6_mc_seq_start,
2449 .next = igmp6_mc_seq_next,
2450 .stop = igmp6_mc_seq_stop,
2451 .show = igmp6_mc_seq_show,
2454 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2456 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2457 sizeof(struct igmp6_mc_iter_state));
2460 static const struct file_operations igmp6_mc_seq_fops = {
2461 .owner = THIS_MODULE,
2462 .open = igmp6_mc_seq_open,
2464 .llseek = seq_lseek,
2465 .release = seq_release_net,
2468 struct igmp6_mcf_iter_state {
2469 struct seq_net_private p;
2470 struct net_device *dev;
2471 struct inet6_dev *idev;
2472 struct ifmcaddr6 *im;
2475 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2477 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2479 struct ip6_sf_list *psf = NULL;
2480 struct ifmcaddr6 *im = NULL;
2481 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2482 struct net *net = seq_file_net(seq);
2486 for_each_netdev(net, state->dev) {
2487 struct inet6_dev *idev;
2488 idev = in6_dev_get(state->dev);
2489 if (unlikely(idev == NULL))
2491 read_lock_bh(&idev->lock);
2493 if (likely(im != NULL)) {
2494 spin_lock_bh(&im->mca_lock);
2495 psf = im->mca_sources;
2496 if (likely(psf != NULL)) {
2501 spin_unlock_bh(&im->mca_lock);
2503 read_unlock_bh(&idev->lock);
2509 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2511 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2515 spin_unlock_bh(&state->im->mca_lock);
2516 state->im = state->im->next;
2517 while (!state->im) {
2518 if (likely(state->idev != NULL)) {
2519 read_unlock_bh(&state->idev->lock);
2520 in6_dev_put(state->idev);
2522 state->dev = next_net_device(state->dev);
2527 state->idev = in6_dev_get(state->dev);
2530 read_lock_bh(&state->idev->lock);
2531 state->im = state->idev->mc_list;
2535 spin_lock_bh(&state->im->mca_lock);
2536 psf = state->im->mca_sources;
2542 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2544 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2546 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2548 return pos ? NULL : psf;
2551 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2552 __acquires(dev_base_lock)
2554 read_lock(&dev_base_lock);
2555 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2558 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2560 struct ip6_sf_list *psf;
2561 if (v == SEQ_START_TOKEN)
2562 psf = igmp6_mcf_get_first(seq);
2564 psf = igmp6_mcf_get_next(seq, v);
2569 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2570 __releases(dev_base_lock)
2572 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2573 if (likely(state->im != NULL)) {
2574 spin_unlock_bh(&state->im->mca_lock);
2577 if (likely(state->idev != NULL)) {
2578 read_unlock_bh(&state->idev->lock);
2579 in6_dev_put(state->idev);
2583 read_unlock(&dev_base_lock);
2586 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2588 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2589 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2591 if (v == SEQ_START_TOKEN) {
2594 "%32s %32s %6s %6s\n", "Idx",
2595 "Device", "Multicast Address",
2596 "Source Address", "INC", "EXC");
2599 "%3d %6.6s " NIP6_SEQFMT " " NIP6_SEQFMT " %6lu %6lu\n",
2600 state->dev->ifindex, state->dev->name,
2601 NIP6(state->im->mca_addr),
2603 psf->sf_count[MCAST_INCLUDE],
2604 psf->sf_count[MCAST_EXCLUDE]);
2609 static const struct seq_operations igmp6_mcf_seq_ops = {
2610 .start = igmp6_mcf_seq_start,
2611 .next = igmp6_mcf_seq_next,
2612 .stop = igmp6_mcf_seq_stop,
2613 .show = igmp6_mcf_seq_show,
2616 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2618 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2619 sizeof(struct igmp6_mcf_iter_state));
2622 static const struct file_operations igmp6_mcf_seq_fops = {
2623 .owner = THIS_MODULE,
2624 .open = igmp6_mcf_seq_open,
2626 .llseek = seq_lseek,
2627 .release = seq_release_net,
2630 static int igmp6_proc_init(struct net *net)
2635 if (!proc_net_fops_create(net, "igmp6", S_IRUGO, &igmp6_mc_seq_fops))
2637 if (!proc_net_fops_create(net, "mcfilter6", S_IRUGO,
2638 &igmp6_mcf_seq_fops))
2639 goto out_proc_net_igmp6;
2646 proc_net_remove(net, "igmp6");
2650 static void igmp6_proc_exit(struct net *net)
2652 proc_net_remove(net, "mcfilter6");
2653 proc_net_remove(net, "igmp6");
2656 static int igmp6_proc_init(struct net *net)
2660 static void igmp6_proc_exit(struct net *net)
2666 static int igmp6_net_init(struct net *net)
2670 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2671 SOCK_RAW, IPPROTO_ICMPV6, net);
2674 "Failed to initialize the IGMP6 control socket (err %d).\n",
2679 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
2681 err = igmp6_proc_init(net);
2683 goto out_sock_create;
2688 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2692 static void igmp6_net_exit(struct net *net)
2694 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2695 igmp6_proc_exit(net);
2698 static struct pernet_operations igmp6_net_ops = {
2699 .init = igmp6_net_init,
2700 .exit = igmp6_net_exit,
2703 int __init igmp6_init(void)
2705 return register_pernet_subsys(&igmp6_net_ops);
2708 void igmp6_cleanup(void)
2710 unregister_pernet_subsys(&igmp6_net_ops);