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) (dev_net((idev)->dev)->ipv6.devconf_all->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_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
167 #define IPV6_MLD_MAX_MSF 64
169 int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
172 * socket join on multicast group
175 int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
177 struct net_device *dev = NULL;
178 struct ipv6_mc_socklist *mc_lst;
179 struct ipv6_pinfo *np = inet6_sk(sk);
180 struct net *net = sock_net(sk);
183 if (!ipv6_addr_is_multicast(addr))
186 read_lock_bh(&ipv6_sk_mc_lock);
187 for (mc_lst=np->ipv6_mc_list; mc_lst; mc_lst=mc_lst->next) {
188 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
189 ipv6_addr_equal(&mc_lst->addr, addr)) {
190 read_unlock_bh(&ipv6_sk_mc_lock);
194 read_unlock_bh(&ipv6_sk_mc_lock);
196 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
202 ipv6_addr_copy(&mc_lst->addr, addr);
206 rt = rt6_lookup(net, addr, NULL, 0, 0);
210 dst_release(&rt->u.dst);
213 dev = dev_get_by_index(net, ifindex);
216 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
220 mc_lst->ifindex = dev->ifindex;
221 mc_lst->sfmode = MCAST_EXCLUDE;
222 rwlock_init(&mc_lst->sflock);
223 mc_lst->sflist = NULL;
226 * now add/increase the group membership on the device
229 err = ipv6_dev_mc_inc(dev, addr);
232 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
237 write_lock_bh(&ipv6_sk_mc_lock);
238 mc_lst->next = np->ipv6_mc_list;
239 np->ipv6_mc_list = mc_lst;
240 write_unlock_bh(&ipv6_sk_mc_lock);
248 * socket leave on multicast group
250 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
252 struct ipv6_pinfo *np = inet6_sk(sk);
253 struct ipv6_mc_socklist *mc_lst, **lnk;
254 struct net *net = sock_net(sk);
256 write_lock_bh(&ipv6_sk_mc_lock);
257 for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
258 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
259 ipv6_addr_equal(&mc_lst->addr, addr)) {
260 struct net_device *dev;
263 write_unlock_bh(&ipv6_sk_mc_lock);
265 dev = dev_get_by_index(net, mc_lst->ifindex);
267 struct inet6_dev *idev = in6_dev_get(dev);
269 (void) ip6_mc_leave_src(sk, mc_lst, idev);
271 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
276 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
277 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
281 write_unlock_bh(&ipv6_sk_mc_lock);
283 return -EADDRNOTAVAIL;
286 static struct inet6_dev *ip6_mc_find_dev(struct net *net,
287 struct in6_addr *group,
290 struct net_device *dev = NULL;
291 struct inet6_dev *idev = NULL;
296 rt = rt6_lookup(net, group, NULL, 0, 0);
300 dst_release(&rt->u.dst);
303 dev = dev_get_by_index(net, ifindex);
307 idev = in6_dev_get(dev);
310 read_lock_bh(&idev->lock);
317 read_unlock_bh(&idev->lock);
325 void ipv6_sock_mc_close(struct sock *sk)
327 struct ipv6_pinfo *np = inet6_sk(sk);
328 struct ipv6_mc_socklist *mc_lst;
329 struct net *net = sock_net(sk);
331 write_lock_bh(&ipv6_sk_mc_lock);
332 while ((mc_lst = np->ipv6_mc_list) != NULL) {
333 struct net_device *dev;
335 np->ipv6_mc_list = mc_lst->next;
336 write_unlock_bh(&ipv6_sk_mc_lock);
338 dev = dev_get_by_index(net, mc_lst->ifindex);
340 struct inet6_dev *idev = in6_dev_get(dev);
342 (void) ip6_mc_leave_src(sk, mc_lst, idev);
344 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
349 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
351 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
353 write_lock_bh(&ipv6_sk_mc_lock);
355 write_unlock_bh(&ipv6_sk_mc_lock);
358 int ip6_mc_source(int add, int omode, struct sock *sk,
359 struct group_source_req *pgsr)
361 struct in6_addr *source, *group;
362 struct ipv6_mc_socklist *pmc;
363 struct net_device *dev;
364 struct inet6_dev *idev;
365 struct ipv6_pinfo *inet6 = inet6_sk(sk);
366 struct ip6_sf_socklist *psl;
367 struct net *net = sock_net(sk);
373 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
374 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
376 if (!ipv6_addr_is_multicast(group))
379 idev = ip6_mc_find_dev(net, group, pgsr->gsr_interface);
384 err = -EADDRNOTAVAIL;
386 read_lock_bh(&ipv6_sk_mc_lock);
387 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
388 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
390 if (ipv6_addr_equal(&pmc->addr, group))
393 if (!pmc) { /* must have a prior join */
397 /* if a source filter was set, must be the same mode as before */
399 if (pmc->sfmode != omode) {
403 } else if (pmc->sfmode != omode) {
404 /* allow mode switches for empty-set filters */
405 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
406 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
410 write_lock_bh(&pmc->sflock);
416 goto done; /* err = -EADDRNOTAVAIL */
418 for (i=0; i<psl->sl_count; i++) {
419 rv = memcmp(&psl->sl_addr[i], source,
420 sizeof(struct in6_addr));
424 if (rv) /* source not found */
425 goto done; /* err = -EADDRNOTAVAIL */
427 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
428 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
433 /* update the interface filter */
434 ip6_mc_del_src(idev, group, omode, 1, source, 1);
436 for (j=i+1; j<psl->sl_count; j++)
437 psl->sl_addr[j-1] = psl->sl_addr[j];
442 /* else, add a new source to the filter */
444 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
448 if (!psl || psl->sl_count == psl->sl_max) {
449 struct ip6_sf_socklist *newpsl;
450 int count = IP6_SFBLOCK;
453 count += psl->sl_max;
454 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
459 newpsl->sl_max = count;
460 newpsl->sl_count = count - IP6_SFBLOCK;
462 for (i=0; i<psl->sl_count; i++)
463 newpsl->sl_addr[i] = psl->sl_addr[i];
464 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
466 pmc->sflist = psl = newpsl;
468 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
469 for (i=0; i<psl->sl_count; i++) {
470 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
474 if (rv == 0) /* address already there is an error */
476 for (j=psl->sl_count-1; j>=i; j--)
477 psl->sl_addr[j+1] = psl->sl_addr[j];
478 psl->sl_addr[i] = *source;
481 /* update the interface list */
482 ip6_mc_add_src(idev, group, omode, 1, source, 1);
485 write_unlock_bh(&pmc->sflock);
486 read_unlock_bh(&ipv6_sk_mc_lock);
487 read_unlock_bh(&idev->lock);
491 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
495 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
497 struct in6_addr *group;
498 struct ipv6_mc_socklist *pmc;
499 struct net_device *dev;
500 struct inet6_dev *idev;
501 struct ipv6_pinfo *inet6 = inet6_sk(sk);
502 struct ip6_sf_socklist *newpsl, *psl;
503 struct net *net = sock_net(sk);
507 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
509 if (!ipv6_addr_is_multicast(group))
511 if (gsf->gf_fmode != MCAST_INCLUDE &&
512 gsf->gf_fmode != MCAST_EXCLUDE)
515 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
522 read_lock_bh(&ipv6_sk_mc_lock);
524 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
529 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
530 if (pmc->ifindex != gsf->gf_interface)
532 if (ipv6_addr_equal(&pmc->addr, group))
535 if (!pmc) { /* must have a prior join */
539 if (gsf->gf_numsrc) {
540 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
546 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
547 for (i=0; i<newpsl->sl_count; ++i) {
548 struct sockaddr_in6 *psin6;
550 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
551 newpsl->sl_addr[i] = psin6->sin6_addr;
553 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
554 newpsl->sl_count, newpsl->sl_addr, 0);
556 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
561 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
564 write_lock_bh(&pmc->sflock);
567 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
568 psl->sl_count, psl->sl_addr, 0);
569 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
571 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
572 pmc->sflist = newpsl;
573 pmc->sfmode = gsf->gf_fmode;
574 write_unlock_bh(&pmc->sflock);
577 read_unlock_bh(&ipv6_sk_mc_lock);
578 read_unlock_bh(&idev->lock);
582 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
586 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
587 struct group_filter __user *optval, int __user *optlen)
589 int err, i, count, copycount;
590 struct in6_addr *group;
591 struct ipv6_mc_socklist *pmc;
592 struct inet6_dev *idev;
593 struct net_device *dev;
594 struct ipv6_pinfo *inet6 = inet6_sk(sk);
595 struct ip6_sf_socklist *psl;
596 struct net *net = sock_net(sk);
598 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
600 if (!ipv6_addr_is_multicast(group))
603 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
610 err = -EADDRNOTAVAIL;
612 * changes to the ipv6_mc_list require the socket lock and
613 * a read lock on ip6_sk_mc_lock. We have the socket lock,
614 * so reading the list is safe.
617 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
618 if (pmc->ifindex != gsf->gf_interface)
620 if (ipv6_addr_equal(group, &pmc->addr))
623 if (!pmc) /* must have a prior join */
625 gsf->gf_fmode = pmc->sfmode;
627 count = psl ? psl->sl_count : 0;
628 read_unlock_bh(&idev->lock);
632 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
633 gsf->gf_numsrc = count;
634 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
635 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
638 /* changes to psl require the socket lock, a read lock on
639 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
640 * have the socket lock, so reading here is safe.
642 for (i=0; i<copycount; i++) {
643 struct sockaddr_in6 *psin6;
644 struct sockaddr_storage ss;
646 psin6 = (struct sockaddr_in6 *)&ss;
647 memset(&ss, 0, sizeof(ss));
648 psin6->sin6_family = AF_INET6;
649 psin6->sin6_addr = psl->sl_addr[i];
650 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
655 read_unlock_bh(&idev->lock);
661 int inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
662 const struct in6_addr *src_addr)
664 struct ipv6_pinfo *np = inet6_sk(sk);
665 struct ipv6_mc_socklist *mc;
666 struct ip6_sf_socklist *psl;
669 read_lock(&ipv6_sk_mc_lock);
670 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
671 if (ipv6_addr_equal(&mc->addr, mc_addr))
675 read_unlock(&ipv6_sk_mc_lock);
678 read_lock(&mc->sflock);
681 rv = mc->sfmode == MCAST_EXCLUDE;
685 for (i=0; i<psl->sl_count; i++) {
686 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
689 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
691 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
694 read_unlock(&mc->sflock);
695 read_unlock(&ipv6_sk_mc_lock);
700 static void ma_put(struct ifmcaddr6 *mc)
702 if (atomic_dec_and_test(&mc->mca_refcnt)) {
703 in6_dev_put(mc->idev);
708 static void igmp6_group_added(struct ifmcaddr6 *mc)
710 struct net_device *dev = mc->idev->dev;
711 char buf[MAX_ADDR_LEN];
713 spin_lock_bh(&mc->mca_lock);
714 if (!(mc->mca_flags&MAF_LOADED)) {
715 mc->mca_flags |= MAF_LOADED;
716 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
717 dev_mc_add(dev, buf, dev->addr_len, 0);
719 spin_unlock_bh(&mc->mca_lock);
721 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
724 if (MLD_V1_SEEN(mc->idev)) {
725 igmp6_join_group(mc);
730 mc->mca_crcount = mc->idev->mc_qrv;
731 mld_ifc_event(mc->idev);
734 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
736 struct net_device *dev = mc->idev->dev;
737 char buf[MAX_ADDR_LEN];
739 spin_lock_bh(&mc->mca_lock);
740 if (mc->mca_flags&MAF_LOADED) {
741 mc->mca_flags &= ~MAF_LOADED;
742 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
743 dev_mc_delete(dev, buf, dev->addr_len, 0);
746 if (mc->mca_flags & MAF_NOREPORT)
748 spin_unlock_bh(&mc->mca_lock);
751 igmp6_leave_group(mc);
753 spin_lock_bh(&mc->mca_lock);
754 if (del_timer(&mc->mca_timer))
755 atomic_dec(&mc->mca_refcnt);
757 ip6_mc_clear_src(mc);
758 spin_unlock_bh(&mc->mca_lock);
762 * deleted ifmcaddr6 manipulation
764 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
766 struct ifmcaddr6 *pmc;
768 /* this is an "ifmcaddr6" for convenience; only the fields below
769 * are actually used. In particular, the refcnt and users are not
770 * used for management of the delete list. Using the same structure
771 * for deleted items allows change reports to use common code with
772 * non-deleted or query-response MCA's.
774 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
778 spin_lock_bh(&im->mca_lock);
779 spin_lock_init(&pmc->mca_lock);
780 pmc->idev = im->idev;
782 pmc->mca_addr = im->mca_addr;
783 pmc->mca_crcount = idev->mc_qrv;
784 pmc->mca_sfmode = im->mca_sfmode;
785 if (pmc->mca_sfmode == MCAST_INCLUDE) {
786 struct ip6_sf_list *psf;
788 pmc->mca_tomb = im->mca_tomb;
789 pmc->mca_sources = im->mca_sources;
790 im->mca_tomb = im->mca_sources = NULL;
791 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
792 psf->sf_crcount = pmc->mca_crcount;
794 spin_unlock_bh(&im->mca_lock);
796 write_lock_bh(&idev->mc_lock);
797 pmc->next = idev->mc_tomb;
799 write_unlock_bh(&idev->mc_lock);
802 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
804 struct ifmcaddr6 *pmc, *pmc_prev;
805 struct ip6_sf_list *psf, *psf_next;
807 write_lock_bh(&idev->mc_lock);
809 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
810 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
816 pmc_prev->next = pmc->next;
818 idev->mc_tomb = pmc->next;
820 write_unlock_bh(&idev->mc_lock);
822 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
823 psf_next = psf->sf_next;
826 in6_dev_put(pmc->idev);
831 static void mld_clear_delrec(struct inet6_dev *idev)
833 struct ifmcaddr6 *pmc, *nextpmc;
835 write_lock_bh(&idev->mc_lock);
837 idev->mc_tomb = NULL;
838 write_unlock_bh(&idev->mc_lock);
840 for (; pmc; pmc = nextpmc) {
842 ip6_mc_clear_src(pmc);
843 in6_dev_put(pmc->idev);
847 /* clear dead sources, too */
848 read_lock_bh(&idev->lock);
849 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
850 struct ip6_sf_list *psf, *psf_next;
852 spin_lock_bh(&pmc->mca_lock);
854 pmc->mca_tomb = NULL;
855 spin_unlock_bh(&pmc->mca_lock);
856 for (; psf; psf=psf_next) {
857 psf_next = psf->sf_next;
861 read_unlock_bh(&idev->lock);
866 * device multicast group inc (add if not found)
868 int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
870 struct ifmcaddr6 *mc;
871 struct inet6_dev *idev;
873 idev = in6_dev_get(dev);
878 write_lock_bh(&idev->lock);
880 write_unlock_bh(&idev->lock);
885 for (mc = idev->mc_list; mc; mc = mc->next) {
886 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
888 write_unlock_bh(&idev->lock);
889 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
897 * not found: create a new one.
900 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
903 write_unlock_bh(&idev->lock);
908 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
910 ipv6_addr_copy(&mc->mca_addr, addr);
913 /* mca_stamp should be updated upon changes */
914 mc->mca_cstamp = mc->mca_tstamp = jiffies;
915 atomic_set(&mc->mca_refcnt, 2);
916 spin_lock_init(&mc->mca_lock);
918 /* initial mode is (EX, empty) */
919 mc->mca_sfmode = MCAST_EXCLUDE;
920 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
922 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
923 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
924 mc->mca_flags |= MAF_NOREPORT;
926 mc->next = idev->mc_list;
928 write_unlock_bh(&idev->lock);
930 mld_del_delrec(idev, &mc->mca_addr);
931 igmp6_group_added(mc);
937 * device multicast group del
939 int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
941 struct ifmcaddr6 *ma, **map;
943 write_lock_bh(&idev->lock);
944 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
945 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
946 if (--ma->mca_users == 0) {
948 write_unlock_bh(&idev->lock);
950 igmp6_group_dropped(ma);
955 write_unlock_bh(&idev->lock);
959 write_unlock_bh(&idev->lock);
964 int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
966 struct inet6_dev *idev = in6_dev_get(dev);
972 err = __ipv6_dev_mc_dec(idev, addr);
980 * identify MLD packets for MLD filter exceptions
982 int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
984 struct icmp6hdr *pic;
986 if (nexthdr != IPPROTO_ICMPV6)
989 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
992 pic = icmp6_hdr(skb);
994 switch (pic->icmp6_type) {
995 case ICMPV6_MGM_QUERY:
996 case ICMPV6_MGM_REPORT:
997 case ICMPV6_MGM_REDUCTION:
998 case ICMPV6_MLD2_REPORT:
1007 * check if the interface/address pair is valid
1009 int ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
1010 const struct in6_addr *src_addr)
1012 struct inet6_dev *idev;
1013 struct ifmcaddr6 *mc;
1016 idev = in6_dev_get(dev);
1018 read_lock_bh(&idev->lock);
1019 for (mc = idev->mc_list; mc; mc=mc->next) {
1020 if (ipv6_addr_equal(&mc->mca_addr, group))
1024 if (src_addr && !ipv6_addr_any(src_addr)) {
1025 struct ip6_sf_list *psf;
1027 spin_lock_bh(&mc->mca_lock);
1028 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1029 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1033 rv = psf->sf_count[MCAST_INCLUDE] ||
1034 psf->sf_count[MCAST_EXCLUDE] !=
1035 mc->mca_sfcount[MCAST_EXCLUDE];
1037 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1038 spin_unlock_bh(&mc->mca_lock);
1040 rv = 1; /* don't filter unspecified source */
1042 read_unlock_bh(&idev->lock);
1048 static void mld_gq_start_timer(struct inet6_dev *idev)
1050 int tv = net_random() % idev->mc_maxdelay;
1052 idev->mc_gq_running = 1;
1053 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1057 static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1059 int tv = net_random() % delay;
1061 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1066 * IGMP handling (alias multicast ICMPv6 messages)
1069 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1071 unsigned long delay = resptime;
1073 /* Do not start timer for these addresses */
1074 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1075 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1078 if (del_timer(&ma->mca_timer)) {
1079 atomic_dec(&ma->mca_refcnt);
1080 delay = ma->mca_timer.expires - jiffies;
1083 if (delay >= resptime) {
1085 delay = net_random() % resptime;
1089 ma->mca_timer.expires = jiffies + delay;
1090 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1091 atomic_inc(&ma->mca_refcnt);
1092 ma->mca_flags |= MAF_TIMER_RUNNING;
1095 /* mark EXCLUDE-mode sources */
1096 static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1097 struct in6_addr *srcs)
1099 struct ip6_sf_list *psf;
1103 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1104 if (scount == nsrcs)
1106 for (i=0; i<nsrcs; i++) {
1107 /* skip inactive filters */
1108 if (pmc->mca_sfcount[MCAST_INCLUDE] ||
1109 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1110 psf->sf_count[MCAST_EXCLUDE])
1112 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1118 pmc->mca_flags &= ~MAF_GSQUERY;
1119 if (scount == nsrcs) /* all sources excluded */
1124 static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1125 struct in6_addr *srcs)
1127 struct ip6_sf_list *psf;
1130 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1131 return mld_xmarksources(pmc, nsrcs, srcs);
1133 /* mark INCLUDE-mode sources */
1136 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1137 if (scount == nsrcs)
1139 for (i=0; i<nsrcs; i++) {
1140 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1148 pmc->mca_flags &= ~MAF_GSQUERY;
1151 pmc->mca_flags |= MAF_GSQUERY;
1155 int igmp6_event_query(struct sk_buff *skb)
1157 struct mld2_query *mlh2 = NULL;
1158 struct ifmcaddr6 *ma;
1159 struct in6_addr *group;
1160 unsigned long max_delay;
1161 struct inet6_dev *idev;
1162 struct icmp6hdr *hdr;
1167 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1170 /* compute payload length excluding extension headers */
1171 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
1172 len -= skb_network_header_len(skb);
1174 /* Drop queries with not link local source */
1175 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
1178 idev = in6_dev_get(skb->dev);
1183 hdr = icmp6_hdr(skb);
1184 group = (struct in6_addr *) (hdr + 1);
1185 group_type = ipv6_addr_type(group);
1187 if (group_type != IPV6_ADDR_ANY &&
1188 !(group_type&IPV6_ADDR_MULTICAST)) {
1195 /* MLDv1 router present */
1197 /* Translate milliseconds to jiffies */
1198 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1200 switchback = (idev->mc_qrv + 1) * max_delay;
1201 idev->mc_v1_seen = jiffies + switchback;
1203 /* cancel the interface change timer */
1204 idev->mc_ifc_count = 0;
1205 if (del_timer(&idev->mc_ifc_timer))
1206 __in6_dev_put(idev);
1207 /* clear deleted report items */
1208 mld_clear_delrec(idev);
1209 } else if (len >= 28) {
1210 int srcs_offset = sizeof(struct mld2_query) -
1211 sizeof(struct icmp6hdr);
1212 if (!pskb_may_pull(skb, srcs_offset)) {
1216 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1217 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1220 idev->mc_maxdelay = max_delay;
1222 idev->mc_qrv = mlh2->qrv;
1223 if (group_type == IPV6_ADDR_ANY) { /* general query */
1226 return -EINVAL; /* no sources allowed */
1228 mld_gq_start_timer(idev);
1232 /* mark sources to include, if group & source-specific */
1233 if (mlh2->nsrcs != 0) {
1234 if (!pskb_may_pull(skb, srcs_offset +
1235 ntohs(mlh2->nsrcs) * sizeof(struct in6_addr))) {
1239 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1247 read_lock_bh(&idev->lock);
1248 if (group_type == IPV6_ADDR_ANY) {
1249 for (ma = idev->mc_list; ma; ma=ma->next) {
1250 spin_lock_bh(&ma->mca_lock);
1251 igmp6_group_queried(ma, max_delay);
1252 spin_unlock_bh(&ma->mca_lock);
1255 for (ma = idev->mc_list; ma; ma=ma->next) {
1256 if (!ipv6_addr_equal(group, &ma->mca_addr))
1258 spin_lock_bh(&ma->mca_lock);
1259 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1260 /* gsquery <- gsquery && mark */
1262 ma->mca_flags &= ~MAF_GSQUERY;
1264 /* gsquery <- mark */
1266 ma->mca_flags |= MAF_GSQUERY;
1268 ma->mca_flags &= ~MAF_GSQUERY;
1270 if (!(ma->mca_flags & MAF_GSQUERY) ||
1271 mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
1272 igmp6_group_queried(ma, max_delay);
1273 spin_unlock_bh(&ma->mca_lock);
1277 read_unlock_bh(&idev->lock);
1284 int igmp6_event_report(struct sk_buff *skb)
1286 struct ifmcaddr6 *ma;
1287 struct in6_addr *addrp;
1288 struct inet6_dev *idev;
1289 struct icmp6hdr *hdr;
1292 /* Our own report looped back. Ignore it. */
1293 if (skb->pkt_type == PACKET_LOOPBACK)
1296 /* send our report if the MC router may not have heard this report */
1297 if (skb->pkt_type != PACKET_MULTICAST &&
1298 skb->pkt_type != PACKET_BROADCAST)
1301 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1304 hdr = icmp6_hdr(skb);
1306 /* Drop reports with not link local source */
1307 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
1308 if (addr_type != IPV6_ADDR_ANY &&
1309 !(addr_type&IPV6_ADDR_LINKLOCAL))
1312 addrp = (struct in6_addr *) (hdr + 1);
1314 idev = in6_dev_get(skb->dev);
1319 * Cancel the timer for this group
1322 read_lock_bh(&idev->lock);
1323 for (ma = idev->mc_list; ma; ma=ma->next) {
1324 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1325 spin_lock(&ma->mca_lock);
1326 if (del_timer(&ma->mca_timer))
1327 atomic_dec(&ma->mca_refcnt);
1328 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1329 spin_unlock(&ma->mca_lock);
1333 read_unlock_bh(&idev->lock);
1338 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1339 int gdeleted, int sdeleted)
1342 case MLD2_MODE_IS_INCLUDE:
1343 case MLD2_MODE_IS_EXCLUDE:
1344 if (gdeleted || sdeleted)
1346 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1347 if (pmc->mca_sfmode == MCAST_INCLUDE)
1349 /* don't include if this source is excluded
1352 if (psf->sf_count[MCAST_INCLUDE])
1353 return type == MLD2_MODE_IS_INCLUDE;
1354 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1355 psf->sf_count[MCAST_EXCLUDE];
1358 case MLD2_CHANGE_TO_INCLUDE:
1359 if (gdeleted || sdeleted)
1361 return psf->sf_count[MCAST_INCLUDE] != 0;
1362 case MLD2_CHANGE_TO_EXCLUDE:
1363 if (gdeleted || sdeleted)
1365 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1366 psf->sf_count[MCAST_INCLUDE])
1368 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1369 psf->sf_count[MCAST_EXCLUDE];
1370 case MLD2_ALLOW_NEW_SOURCES:
1371 if (gdeleted || !psf->sf_crcount)
1373 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1374 case MLD2_BLOCK_OLD_SOURCES:
1375 if (pmc->mca_sfmode == MCAST_INCLUDE)
1376 return gdeleted || (psf->sf_crcount && sdeleted);
1377 return psf->sf_crcount && !gdeleted && !sdeleted;
1383 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1385 struct ip6_sf_list *psf;
1388 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1389 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1396 static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1398 struct net *net = dev_net(dev);
1399 struct sock *sk = net->ipv6.igmp_sk;
1400 struct sk_buff *skb;
1401 struct mld2_report *pmr;
1402 struct in6_addr addr_buf;
1403 const struct in6_addr *saddr;
1405 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1406 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1409 /* we assume size > sizeof(ra) here */
1410 skb = sock_alloc_send_skb(sk, size + LL_ALLOCATED_SPACE(dev), 1, &err);
1415 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1417 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1418 /* <draft-ietf-magma-mld-source-05.txt>:
1419 * use unspecified address as the source address
1420 * when a valid link-local address is not available.
1422 saddr = &in6addr_any;
1426 ip6_nd_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
1428 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1430 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
1431 skb_put(skb, sizeof(*pmr));
1432 pmr = (struct mld2_report *)skb_transport_header(skb);
1433 pmr->type = ICMPV6_MLD2_REPORT;
1441 static void mld_sendpack(struct sk_buff *skb)
1443 struct ipv6hdr *pip6 = ipv6_hdr(skb);
1444 struct mld2_report *pmr =
1445 (struct mld2_report *)skb_transport_header(skb);
1446 int payload_len, mldlen;
1447 struct inet6_dev *idev = in6_dev_get(skb->dev);
1448 struct net *net = dev_net(skb->dev);
1452 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTREQUESTS);
1453 payload_len = (skb->tail - skb->network_header) - sizeof(*pip6);
1454 mldlen = skb->tail - skb->transport_header;
1455 pip6->payload_len = htons(payload_len);
1457 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1458 IPPROTO_ICMPV6, csum_partial(skb_transport_header(skb),
1461 skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1468 icmpv6_flow_init(net->ipv6.igmp_sk, &fl, ICMPV6_MLD2_REPORT,
1469 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1472 err = xfrm_lookup(net, &skb->dst, &fl, NULL, 0);
1476 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1480 ICMP6MSGOUT_INC_STATS_BH(net, idev, ICMPV6_MLD2_REPORT);
1481 ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
1482 IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTMCASTPKTS);
1484 IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTDISCARDS);
1486 if (likely(idev != NULL))
1495 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1497 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1500 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1501 int type, struct mld2_grec **ppgr)
1503 struct net_device *dev = pmc->idev->dev;
1504 struct mld2_report *pmr;
1505 struct mld2_grec *pgr;
1508 skb = mld_newpack(dev, dev->mtu);
1511 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1512 pgr->grec_type = type;
1513 pgr->grec_auxwords = 0;
1514 pgr->grec_nsrcs = 0;
1515 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1516 pmr = (struct mld2_report *)skb_transport_header(skb);
1517 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1522 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1523 skb_tailroom(skb)) : 0)
1525 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1526 int type, int gdeleted, int sdeleted)
1528 struct net_device *dev = pmc->idev->dev;
1529 struct mld2_report *pmr;
1530 struct mld2_grec *pgr = NULL;
1531 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1532 int scount, stotal, first, isquery, truncate;
1534 if (pmc->mca_flags & MAF_NOREPORT)
1537 isquery = type == MLD2_MODE_IS_INCLUDE ||
1538 type == MLD2_MODE_IS_EXCLUDE;
1539 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1540 type == MLD2_CHANGE_TO_EXCLUDE;
1542 stotal = scount = 0;
1544 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1549 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
1551 /* EX and TO_EX get a fresh packet, if needed */
1553 if (pmr && pmr->ngrec &&
1554 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1557 skb = mld_newpack(dev, dev->mtu);
1562 for (psf=*psf_list; psf; psf=psf_next) {
1563 struct in6_addr *psrc;
1565 psf_next = psf->sf_next;
1567 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1572 /* clear marks on query responses */
1576 if (AVAILABLE(skb) < sizeof(*psrc) +
1577 first*sizeof(struct mld2_grec)) {
1578 if (truncate && !first)
1579 break; /* truncate these */
1581 pgr->grec_nsrcs = htons(scount);
1584 skb = mld_newpack(dev, dev->mtu);
1589 skb = add_grhead(skb, pmc, type, &pgr);
1594 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1595 *psrc = psf->sf_addr;
1597 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1598 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1600 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1602 psf_prev->sf_next = psf->sf_next;
1604 *psf_list = psf->sf_next;
1614 if (type == MLD2_ALLOW_NEW_SOURCES ||
1615 type == MLD2_BLOCK_OLD_SOURCES)
1617 if (pmc->mca_crcount || isquery) {
1618 /* make sure we have room for group header */
1619 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1621 skb = NULL; /* add_grhead will get a new one */
1623 skb = add_grhead(skb, pmc, type, &pgr);
1627 pgr->grec_nsrcs = htons(scount);
1630 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1634 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1636 struct sk_buff *skb = NULL;
1640 read_lock_bh(&idev->lock);
1641 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1642 if (pmc->mca_flags & MAF_NOREPORT)
1644 spin_lock_bh(&pmc->mca_lock);
1645 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1646 type = MLD2_MODE_IS_EXCLUDE;
1648 type = MLD2_MODE_IS_INCLUDE;
1649 skb = add_grec(skb, pmc, type, 0, 0);
1650 spin_unlock_bh(&pmc->mca_lock);
1652 read_unlock_bh(&idev->lock);
1654 spin_lock_bh(&pmc->mca_lock);
1655 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1656 type = MLD2_MODE_IS_EXCLUDE;
1658 type = MLD2_MODE_IS_INCLUDE;
1659 skb = add_grec(skb, pmc, type, 0, 0);
1660 spin_unlock_bh(&pmc->mca_lock);
1667 * remove zero-count source records from a source filter list
1669 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1671 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1674 for (psf=*ppsf; psf; psf = psf_next) {
1675 psf_next = psf->sf_next;
1676 if (psf->sf_crcount == 0) {
1678 psf_prev->sf_next = psf->sf_next;
1680 *ppsf = psf->sf_next;
1687 static void mld_send_cr(struct inet6_dev *idev)
1689 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1690 struct sk_buff *skb = NULL;
1693 read_lock_bh(&idev->lock);
1694 write_lock_bh(&idev->mc_lock);
1698 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1699 pmc_next = pmc->next;
1700 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1701 type = MLD2_BLOCK_OLD_SOURCES;
1702 dtype = MLD2_BLOCK_OLD_SOURCES;
1703 skb = add_grec(skb, pmc, type, 1, 0);
1704 skb = add_grec(skb, pmc, dtype, 1, 1);
1706 if (pmc->mca_crcount) {
1707 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1708 type = MLD2_CHANGE_TO_INCLUDE;
1709 skb = add_grec(skb, pmc, type, 1, 0);
1712 if (pmc->mca_crcount == 0) {
1713 mld_clear_zeros(&pmc->mca_tomb);
1714 mld_clear_zeros(&pmc->mca_sources);
1717 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1718 !pmc->mca_sources) {
1720 pmc_prev->next = pmc_next;
1722 idev->mc_tomb = pmc_next;
1723 in6_dev_put(pmc->idev);
1728 write_unlock_bh(&idev->mc_lock);
1731 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1732 spin_lock_bh(&pmc->mca_lock);
1733 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1734 type = MLD2_BLOCK_OLD_SOURCES;
1735 dtype = MLD2_ALLOW_NEW_SOURCES;
1737 type = MLD2_ALLOW_NEW_SOURCES;
1738 dtype = MLD2_BLOCK_OLD_SOURCES;
1740 skb = add_grec(skb, pmc, type, 0, 0);
1741 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1743 /* filter mode changes */
1744 if (pmc->mca_crcount) {
1745 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1746 type = MLD2_CHANGE_TO_EXCLUDE;
1748 type = MLD2_CHANGE_TO_INCLUDE;
1749 skb = add_grec(skb, pmc, type, 0, 0);
1752 spin_unlock_bh(&pmc->mca_lock);
1754 read_unlock_bh(&idev->lock);
1757 (void) mld_sendpack(skb);
1760 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1762 struct net *net = dev_net(dev);
1763 struct sock *sk = net->ipv6.igmp_sk;
1764 struct inet6_dev *idev;
1765 struct sk_buff *skb;
1766 struct icmp6hdr *hdr;
1767 const struct in6_addr *snd_addr, *saddr;
1768 struct in6_addr *addrp;
1769 struct in6_addr addr_buf;
1770 int err, len, payload_len, full_len;
1771 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1772 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1777 IP6_INC_STATS(net, __in6_dev_get(dev),
1778 IPSTATS_MIB_OUTREQUESTS);
1780 if (type == ICMPV6_MGM_REDUCTION)
1781 snd_addr = &in6addr_linklocal_allrouters;
1785 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1786 payload_len = len + sizeof(ra);
1787 full_len = sizeof(struct ipv6hdr) + payload_len;
1789 skb = sock_alloc_send_skb(sk, LL_ALLOCATED_SPACE(dev) + full_len, 1, &err);
1793 IP6_INC_STATS(net, __in6_dev_get(dev),
1794 IPSTATS_MIB_OUTDISCARDS);
1799 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1801 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1802 /* <draft-ietf-magma-mld-source-05.txt>:
1803 * use unspecified address as the source address
1804 * when a valid link-local address is not available.
1806 saddr = &in6addr_any;
1810 ip6_nd_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
1812 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1814 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1815 memset(hdr, 0, sizeof(struct icmp6hdr));
1816 hdr->icmp6_type = type;
1818 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1819 ipv6_addr_copy(addrp, addr);
1821 hdr->icmp6_cksum = csum_ipv6_magic(saddr, snd_addr, len,
1823 csum_partial(hdr, len, 0));
1825 idev = in6_dev_get(skb->dev);
1827 skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1833 icmpv6_flow_init(sk, &fl, type,
1834 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1837 err = xfrm_lookup(net, &skb->dst, &fl, NULL, 0);
1841 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1845 ICMP6MSGOUT_INC_STATS(net, idev, type);
1846 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
1847 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTMCASTPKTS);
1849 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
1851 if (likely(idev != NULL))
1860 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1861 struct in6_addr *psfsrc)
1863 struct ip6_sf_list *psf, *psf_prev;
1867 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1868 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1872 if (!psf || psf->sf_count[sfmode] == 0) {
1873 /* source filter not found, or count wrong => bug */
1876 psf->sf_count[sfmode]--;
1877 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1878 struct inet6_dev *idev = pmc->idev;
1880 /* no more filters for this source */
1882 psf_prev->sf_next = psf->sf_next;
1884 pmc->mca_sources = psf->sf_next;
1885 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1886 !MLD_V1_SEEN(idev)) {
1887 psf->sf_crcount = idev->mc_qrv;
1888 psf->sf_next = pmc->mca_tomb;
1889 pmc->mca_tomb = psf;
1897 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1898 int sfmode, int sfcount, struct in6_addr *psfsrc,
1901 struct ifmcaddr6 *pmc;
1907 read_lock_bh(&idev->lock);
1908 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1909 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1913 /* MCA not found?? bug */
1914 read_unlock_bh(&idev->lock);
1917 spin_lock_bh(&pmc->mca_lock);
1920 if (!pmc->mca_sfcount[sfmode]) {
1921 spin_unlock_bh(&pmc->mca_lock);
1922 read_unlock_bh(&idev->lock);
1925 pmc->mca_sfcount[sfmode]--;
1928 for (i=0; i<sfcount; i++) {
1929 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1931 changerec |= rv > 0;
1935 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1936 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1937 pmc->mca_sfcount[MCAST_INCLUDE]) {
1938 struct ip6_sf_list *psf;
1940 /* filter mode change */
1941 pmc->mca_sfmode = MCAST_INCLUDE;
1942 pmc->mca_crcount = idev->mc_qrv;
1943 idev->mc_ifc_count = pmc->mca_crcount;
1944 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1945 psf->sf_crcount = 0;
1946 mld_ifc_event(pmc->idev);
1947 } else if (sf_setstate(pmc) || changerec)
1948 mld_ifc_event(pmc->idev);
1949 spin_unlock_bh(&pmc->mca_lock);
1950 read_unlock_bh(&idev->lock);
1955 * Add multicast single-source filter to the interface list
1957 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1958 struct in6_addr *psfsrc, int delta)
1960 struct ip6_sf_list *psf, *psf_prev;
1963 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1964 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1969 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1973 psf->sf_addr = *psfsrc;
1975 psf_prev->sf_next = psf;
1977 pmc->mca_sources = psf;
1979 psf->sf_count[sfmode]++;
1983 static void sf_markstate(struct ifmcaddr6 *pmc)
1985 struct ip6_sf_list *psf;
1986 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1988 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1989 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1990 psf->sf_oldin = mca_xcount ==
1991 psf->sf_count[MCAST_EXCLUDE] &&
1992 !psf->sf_count[MCAST_INCLUDE];
1994 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1997 static int sf_setstate(struct ifmcaddr6 *pmc)
1999 struct ip6_sf_list *psf, *dpsf;
2000 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2001 int qrv = pmc->idev->mc_qrv;
2005 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
2006 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2007 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2008 !psf->sf_count[MCAST_INCLUDE];
2010 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2012 if (!psf->sf_oldin) {
2013 struct ip6_sf_list *prev = NULL;
2015 for (dpsf=pmc->mca_tomb; dpsf;
2016 dpsf=dpsf->sf_next) {
2017 if (ipv6_addr_equal(&dpsf->sf_addr,
2024 prev->sf_next = dpsf->sf_next;
2026 pmc->mca_tomb = dpsf->sf_next;
2029 psf->sf_crcount = qrv;
2032 } else if (psf->sf_oldin) {
2033 psf->sf_crcount = 0;
2035 * add or update "delete" records if an active filter
2038 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2039 if (ipv6_addr_equal(&dpsf->sf_addr,
2043 dpsf = (struct ip6_sf_list *)
2044 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2048 /* pmc->mca_lock held by callers */
2049 dpsf->sf_next = pmc->mca_tomb;
2050 pmc->mca_tomb = dpsf;
2052 dpsf->sf_crcount = qrv;
2060 * Add multicast source filter list to the interface list
2062 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
2063 int sfmode, int sfcount, struct in6_addr *psfsrc,
2066 struct ifmcaddr6 *pmc;
2072 read_lock_bh(&idev->lock);
2073 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2074 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2078 /* MCA not found?? bug */
2079 read_unlock_bh(&idev->lock);
2082 spin_lock_bh(&pmc->mca_lock);
2085 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2087 pmc->mca_sfcount[sfmode]++;
2089 for (i=0; i<sfcount; i++) {
2090 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
2098 pmc->mca_sfcount[sfmode]--;
2100 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2101 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2102 struct inet6_dev *idev = pmc->idev;
2103 struct ip6_sf_list *psf;
2105 /* filter mode change */
2106 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2107 pmc->mca_sfmode = MCAST_EXCLUDE;
2108 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2109 pmc->mca_sfmode = MCAST_INCLUDE;
2110 /* else no filters; keep old mode for reports */
2112 pmc->mca_crcount = idev->mc_qrv;
2113 idev->mc_ifc_count = pmc->mca_crcount;
2114 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2115 psf->sf_crcount = 0;
2116 mld_ifc_event(idev);
2117 } else if (sf_setstate(pmc))
2118 mld_ifc_event(idev);
2119 spin_unlock_bh(&pmc->mca_lock);
2120 read_unlock_bh(&idev->lock);
2124 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2126 struct ip6_sf_list *psf, *nextpsf;
2128 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2129 nextpsf = psf->sf_next;
2132 pmc->mca_tomb = NULL;
2133 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2134 nextpsf = psf->sf_next;
2137 pmc->mca_sources = NULL;
2138 pmc->mca_sfmode = MCAST_EXCLUDE;
2139 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2140 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2144 static void igmp6_join_group(struct ifmcaddr6 *ma)
2146 unsigned long delay;
2148 if (ma->mca_flags & MAF_NOREPORT)
2151 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2153 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2155 spin_lock_bh(&ma->mca_lock);
2156 if (del_timer(&ma->mca_timer)) {
2157 atomic_dec(&ma->mca_refcnt);
2158 delay = ma->mca_timer.expires - jiffies;
2161 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2162 atomic_inc(&ma->mca_refcnt);
2163 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2164 spin_unlock_bh(&ma->mca_lock);
2167 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2168 struct inet6_dev *idev)
2172 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2173 * so no other readers or writers of iml or its sflist
2176 /* any-source empty exclude case */
2177 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2179 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2180 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2181 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2186 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2188 if (MLD_V1_SEEN(ma->idev)) {
2189 if (ma->mca_flags & MAF_LAST_REPORTER)
2190 igmp6_send(&ma->mca_addr, ma->idev->dev,
2191 ICMPV6_MGM_REDUCTION);
2193 mld_add_delrec(ma->idev, ma);
2194 mld_ifc_event(ma->idev);
2198 static void mld_gq_timer_expire(unsigned long data)
2200 struct inet6_dev *idev = (struct inet6_dev *)data;
2202 idev->mc_gq_running = 0;
2203 mld_send_report(idev, NULL);
2204 __in6_dev_put(idev);
2207 static void mld_ifc_timer_expire(unsigned long data)
2209 struct inet6_dev *idev = (struct inet6_dev *)data;
2212 if (idev->mc_ifc_count) {
2213 idev->mc_ifc_count--;
2214 if (idev->mc_ifc_count)
2215 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2217 __in6_dev_put(idev);
2220 static void mld_ifc_event(struct inet6_dev *idev)
2222 if (MLD_V1_SEEN(idev))
2224 idev->mc_ifc_count = idev->mc_qrv;
2225 mld_ifc_start_timer(idev, 1);
2229 static void igmp6_timer_handler(unsigned long data)
2231 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2233 if (MLD_V1_SEEN(ma->idev))
2234 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2236 mld_send_report(ma->idev, ma);
2238 spin_lock(&ma->mca_lock);
2239 ma->mca_flags |= MAF_LAST_REPORTER;
2240 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2241 spin_unlock(&ma->mca_lock);
2245 /* Device going down */
2247 void ipv6_mc_down(struct inet6_dev *idev)
2249 struct ifmcaddr6 *i;
2251 /* Withdraw multicast list */
2253 read_lock_bh(&idev->lock);
2254 idev->mc_ifc_count = 0;
2255 if (del_timer(&idev->mc_ifc_timer))
2256 __in6_dev_put(idev);
2257 idev->mc_gq_running = 0;
2258 if (del_timer(&idev->mc_gq_timer))
2259 __in6_dev_put(idev);
2261 for (i = idev->mc_list; i; i=i->next)
2262 igmp6_group_dropped(i);
2263 read_unlock_bh(&idev->lock);
2265 mld_clear_delrec(idev);
2269 /* Device going up */
2271 void ipv6_mc_up(struct inet6_dev *idev)
2273 struct ifmcaddr6 *i;
2275 /* Install multicast list, except for all-nodes (already installed) */
2277 read_lock_bh(&idev->lock);
2278 for (i = idev->mc_list; i; i=i->next)
2279 igmp6_group_added(i);
2280 read_unlock_bh(&idev->lock);
2283 /* IPv6 device initialization. */
2285 void ipv6_mc_init_dev(struct inet6_dev *idev)
2287 write_lock_bh(&idev->lock);
2288 rwlock_init(&idev->mc_lock);
2289 idev->mc_gq_running = 0;
2290 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2291 (unsigned long)idev);
2292 idev->mc_tomb = NULL;
2293 idev->mc_ifc_count = 0;
2294 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2295 (unsigned long)idev);
2296 idev->mc_qrv = MLD_QRV_DEFAULT;
2297 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2298 idev->mc_v1_seen = 0;
2299 write_unlock_bh(&idev->lock);
2303 * Device is about to be destroyed: clean up.
2306 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2308 struct ifmcaddr6 *i;
2310 /* Deactivate timers */
2313 /* Delete all-nodes address. */
2314 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2315 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2318 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
2320 if (idev->cnf.forwarding)
2321 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
2323 write_lock_bh(&idev->lock);
2324 while ((i = idev->mc_list) != NULL) {
2325 idev->mc_list = i->next;
2326 write_unlock_bh(&idev->lock);
2328 igmp6_group_dropped(i);
2331 write_lock_bh(&idev->lock);
2333 write_unlock_bh(&idev->lock);
2336 #ifdef CONFIG_PROC_FS
2337 struct igmp6_mc_iter_state {
2338 struct seq_net_private p;
2339 struct net_device *dev;
2340 struct inet6_dev *idev;
2343 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2345 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2347 struct ifmcaddr6 *im = NULL;
2348 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2349 struct net *net = seq_file_net(seq);
2352 for_each_netdev(net, state->dev) {
2353 struct inet6_dev *idev;
2354 idev = in6_dev_get(state->dev);
2357 read_lock_bh(&idev->lock);
2363 read_unlock_bh(&idev->lock);
2369 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2371 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2375 if (likely(state->idev != NULL)) {
2376 read_unlock_bh(&state->idev->lock);
2377 in6_dev_put(state->idev);
2379 state->dev = next_net_device(state->dev);
2384 state->idev = in6_dev_get(state->dev);
2387 read_lock_bh(&state->idev->lock);
2388 im = state->idev->mc_list;
2393 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2395 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2397 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2399 return pos ? NULL : im;
2402 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2403 __acquires(dev_base_lock)
2405 read_lock(&dev_base_lock);
2406 return igmp6_mc_get_idx(seq, *pos);
2409 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2411 struct ifmcaddr6 *im;
2412 im = igmp6_mc_get_next(seq, v);
2417 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2418 __releases(dev_base_lock)
2420 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2421 if (likely(state->idev != NULL)) {
2422 read_unlock_bh(&state->idev->lock);
2423 in6_dev_put(state->idev);
2427 read_unlock(&dev_base_lock);
2430 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2432 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2433 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2436 "%-4d %-15s %pi6 %5d %08X %ld\n",
2437 state->dev->ifindex, state->dev->name,
2439 im->mca_users, im->mca_flags,
2440 (im->mca_flags&MAF_TIMER_RUNNING) ?
2441 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2445 static const struct seq_operations igmp6_mc_seq_ops = {
2446 .start = igmp6_mc_seq_start,
2447 .next = igmp6_mc_seq_next,
2448 .stop = igmp6_mc_seq_stop,
2449 .show = igmp6_mc_seq_show,
2452 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2454 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2455 sizeof(struct igmp6_mc_iter_state));
2458 static const struct file_operations igmp6_mc_seq_fops = {
2459 .owner = THIS_MODULE,
2460 .open = igmp6_mc_seq_open,
2462 .llseek = seq_lseek,
2463 .release = seq_release_net,
2466 struct igmp6_mcf_iter_state {
2467 struct seq_net_private p;
2468 struct net_device *dev;
2469 struct inet6_dev *idev;
2470 struct ifmcaddr6 *im;
2473 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2475 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2477 struct ip6_sf_list *psf = NULL;
2478 struct ifmcaddr6 *im = NULL;
2479 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2480 struct net *net = seq_file_net(seq);
2484 for_each_netdev(net, state->dev) {
2485 struct inet6_dev *idev;
2486 idev = in6_dev_get(state->dev);
2487 if (unlikely(idev == NULL))
2489 read_lock_bh(&idev->lock);
2491 if (likely(im != NULL)) {
2492 spin_lock_bh(&im->mca_lock);
2493 psf = im->mca_sources;
2494 if (likely(psf != NULL)) {
2499 spin_unlock_bh(&im->mca_lock);
2501 read_unlock_bh(&idev->lock);
2507 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2509 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2513 spin_unlock_bh(&state->im->mca_lock);
2514 state->im = state->im->next;
2515 while (!state->im) {
2516 if (likely(state->idev != NULL)) {
2517 read_unlock_bh(&state->idev->lock);
2518 in6_dev_put(state->idev);
2520 state->dev = next_net_device(state->dev);
2525 state->idev = in6_dev_get(state->dev);
2528 read_lock_bh(&state->idev->lock);
2529 state->im = state->idev->mc_list;
2533 spin_lock_bh(&state->im->mca_lock);
2534 psf = state->im->mca_sources;
2540 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2542 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2544 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2546 return pos ? NULL : psf;
2549 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2550 __acquires(dev_base_lock)
2552 read_lock(&dev_base_lock);
2553 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2556 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2558 struct ip6_sf_list *psf;
2559 if (v == SEQ_START_TOKEN)
2560 psf = igmp6_mcf_get_first(seq);
2562 psf = igmp6_mcf_get_next(seq, v);
2567 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2568 __releases(dev_base_lock)
2570 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2571 if (likely(state->im != NULL)) {
2572 spin_unlock_bh(&state->im->mca_lock);
2575 if (likely(state->idev != NULL)) {
2576 read_unlock_bh(&state->idev->lock);
2577 in6_dev_put(state->idev);
2581 read_unlock(&dev_base_lock);
2584 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2586 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2587 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2589 if (v == SEQ_START_TOKEN) {
2592 "%32s %32s %6s %6s\n", "Idx",
2593 "Device", "Multicast Address",
2594 "Source Address", "INC", "EXC");
2597 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
2598 state->dev->ifindex, state->dev->name,
2599 &state->im->mca_addr,
2601 psf->sf_count[MCAST_INCLUDE],
2602 psf->sf_count[MCAST_EXCLUDE]);
2607 static const struct seq_operations igmp6_mcf_seq_ops = {
2608 .start = igmp6_mcf_seq_start,
2609 .next = igmp6_mcf_seq_next,
2610 .stop = igmp6_mcf_seq_stop,
2611 .show = igmp6_mcf_seq_show,
2614 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2616 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2617 sizeof(struct igmp6_mcf_iter_state));
2620 static const struct file_operations igmp6_mcf_seq_fops = {
2621 .owner = THIS_MODULE,
2622 .open = igmp6_mcf_seq_open,
2624 .llseek = seq_lseek,
2625 .release = seq_release_net,
2628 static int igmp6_proc_init(struct net *net)
2633 if (!proc_net_fops_create(net, "igmp6", S_IRUGO, &igmp6_mc_seq_fops))
2635 if (!proc_net_fops_create(net, "mcfilter6", S_IRUGO,
2636 &igmp6_mcf_seq_fops))
2637 goto out_proc_net_igmp6;
2644 proc_net_remove(net, "igmp6");
2648 static void igmp6_proc_exit(struct net *net)
2650 proc_net_remove(net, "mcfilter6");
2651 proc_net_remove(net, "igmp6");
2654 static int igmp6_proc_init(struct net *net)
2658 static void igmp6_proc_exit(struct net *net)
2664 static int igmp6_net_init(struct net *net)
2668 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2669 SOCK_RAW, IPPROTO_ICMPV6, net);
2672 "Failed to initialize the IGMP6 control socket (err %d).\n",
2677 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
2679 err = igmp6_proc_init(net);
2681 goto out_sock_create;
2686 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2690 static void igmp6_net_exit(struct net *net)
2692 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2693 igmp6_proc_exit(net);
2696 static struct pernet_operations igmp6_net_ops = {
2697 .init = igmp6_net_init,
2698 .exit = igmp6_net_exit,
2701 int __init igmp6_init(void)
2703 return register_pernet_subsys(&igmp6_net_ops);
2706 void igmp6_cleanup(void)
2708 unregister_pernet_subsys(&igmp6_net_ops);