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_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);
312 read_lock_bh(&idev->lock);
314 read_unlock_bh(&idev->lock);
322 void ipv6_sock_mc_close(struct sock *sk)
324 struct ipv6_pinfo *np = inet6_sk(sk);
325 struct ipv6_mc_socklist *mc_lst;
326 struct net *net = sock_net(sk);
328 write_lock_bh(&ipv6_sk_mc_lock);
329 while ((mc_lst = np->ipv6_mc_list) != NULL) {
330 struct net_device *dev;
332 np->ipv6_mc_list = mc_lst->next;
333 write_unlock_bh(&ipv6_sk_mc_lock);
335 dev = dev_get_by_index(net, mc_lst->ifindex);
337 struct inet6_dev *idev = in6_dev_get(dev);
339 (void) ip6_mc_leave_src(sk, mc_lst, idev);
341 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
346 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
348 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
350 write_lock_bh(&ipv6_sk_mc_lock);
352 write_unlock_bh(&ipv6_sk_mc_lock);
355 int ip6_mc_source(int add, int omode, struct sock *sk,
356 struct group_source_req *pgsr)
358 struct in6_addr *source, *group;
359 struct ipv6_mc_socklist *pmc;
360 struct net_device *dev;
361 struct inet6_dev *idev;
362 struct ipv6_pinfo *inet6 = inet6_sk(sk);
363 struct ip6_sf_socklist *psl;
364 struct net *net = sock_net(sk);
370 if (pgsr->gsr_group.ss_family != AF_INET6 ||
371 pgsr->gsr_source.ss_family != AF_INET6)
374 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
375 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
377 if (!ipv6_addr_is_multicast(group))
380 idev = ip6_mc_find_dev(net, group, pgsr->gsr_interface);
385 err = -EADDRNOTAVAIL;
387 read_lock_bh(&ipv6_sk_mc_lock);
388 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
389 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
391 if (ipv6_addr_equal(&pmc->addr, group))
394 if (!pmc) { /* must have a prior join */
398 /* if a source filter was set, must be the same mode as before */
400 if (pmc->sfmode != omode) {
404 } else if (pmc->sfmode != omode) {
405 /* allow mode switches for empty-set filters */
406 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
407 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
411 write_lock_bh(&pmc->sflock);
417 goto done; /* err = -EADDRNOTAVAIL */
419 for (i=0; i<psl->sl_count; i++) {
420 rv = memcmp(&psl->sl_addr[i], source,
421 sizeof(struct in6_addr));
425 if (rv) /* source not found */
426 goto done; /* err = -EADDRNOTAVAIL */
428 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
429 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
434 /* update the interface filter */
435 ip6_mc_del_src(idev, group, omode, 1, source, 1);
437 for (j=i+1; j<psl->sl_count; j++)
438 psl->sl_addr[j-1] = psl->sl_addr[j];
443 /* else, add a new source to the filter */
445 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
449 if (!psl || psl->sl_count == psl->sl_max) {
450 struct ip6_sf_socklist *newpsl;
451 int count = IP6_SFBLOCK;
454 count += psl->sl_max;
455 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
460 newpsl->sl_max = count;
461 newpsl->sl_count = count - IP6_SFBLOCK;
463 for (i=0; i<psl->sl_count; i++)
464 newpsl->sl_addr[i] = psl->sl_addr[i];
465 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
467 pmc->sflist = psl = newpsl;
469 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
470 for (i=0; i<psl->sl_count; i++) {
471 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
475 if (rv == 0) /* address already there is an error */
477 for (j=psl->sl_count-1; j>=i; j--)
478 psl->sl_addr[j+1] = psl->sl_addr[j];
479 psl->sl_addr[i] = *source;
482 /* update the interface list */
483 ip6_mc_add_src(idev, group, omode, 1, source, 1);
486 write_unlock_bh(&pmc->sflock);
487 read_unlock_bh(&ipv6_sk_mc_lock);
488 read_unlock_bh(&idev->lock);
492 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
496 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
498 struct in6_addr *group;
499 struct ipv6_mc_socklist *pmc;
500 struct net_device *dev;
501 struct inet6_dev *idev;
502 struct ipv6_pinfo *inet6 = inet6_sk(sk);
503 struct ip6_sf_socklist *newpsl, *psl;
504 struct net *net = sock_net(sk);
508 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
510 if (!ipv6_addr_is_multicast(group))
512 if (gsf->gf_fmode != MCAST_INCLUDE &&
513 gsf->gf_fmode != MCAST_EXCLUDE)
516 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
523 read_lock_bh(&ipv6_sk_mc_lock);
525 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
530 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
531 if (pmc->ifindex != gsf->gf_interface)
533 if (ipv6_addr_equal(&pmc->addr, group))
536 if (!pmc) { /* must have a prior join */
540 if (gsf->gf_numsrc) {
541 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
547 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
548 for (i=0; i<newpsl->sl_count; ++i) {
549 struct sockaddr_in6 *psin6;
551 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
552 newpsl->sl_addr[i] = psin6->sin6_addr;
554 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
555 newpsl->sl_count, newpsl->sl_addr, 0);
557 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
562 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
565 write_lock_bh(&pmc->sflock);
568 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
569 psl->sl_count, psl->sl_addr, 0);
570 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
572 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
573 pmc->sflist = newpsl;
574 pmc->sfmode = gsf->gf_fmode;
575 write_unlock_bh(&pmc->sflock);
578 read_unlock_bh(&ipv6_sk_mc_lock);
579 read_unlock_bh(&idev->lock);
583 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
587 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
588 struct group_filter __user *optval, int __user *optlen)
590 int err, i, count, copycount;
591 struct in6_addr *group;
592 struct ipv6_mc_socklist *pmc;
593 struct inet6_dev *idev;
594 struct net_device *dev;
595 struct ipv6_pinfo *inet6 = inet6_sk(sk);
596 struct ip6_sf_socklist *psl;
597 struct net *net = sock_net(sk);
599 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
601 if (!ipv6_addr_is_multicast(group))
604 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
611 err = -EADDRNOTAVAIL;
613 * changes to the ipv6_mc_list require the socket lock and
614 * a read lock on ip6_sk_mc_lock. We have the socket lock,
615 * so reading the list is safe.
618 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
619 if (pmc->ifindex != gsf->gf_interface)
621 if (ipv6_addr_equal(group, &pmc->addr))
624 if (!pmc) /* must have a prior join */
626 gsf->gf_fmode = pmc->sfmode;
628 count = psl ? psl->sl_count : 0;
629 read_unlock_bh(&idev->lock);
633 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
634 gsf->gf_numsrc = count;
635 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
636 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
639 /* changes to psl require the socket lock, a read lock on
640 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
641 * have the socket lock, so reading here is safe.
643 for (i=0; i<copycount; i++) {
644 struct sockaddr_in6 *psin6;
645 struct sockaddr_storage ss;
647 psin6 = (struct sockaddr_in6 *)&ss;
648 memset(&ss, 0, sizeof(ss));
649 psin6->sin6_family = AF_INET6;
650 psin6->sin6_addr = psl->sl_addr[i];
651 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
656 read_unlock_bh(&idev->lock);
662 int inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
663 const struct in6_addr *src_addr)
665 struct ipv6_pinfo *np = inet6_sk(sk);
666 struct ipv6_mc_socklist *mc;
667 struct ip6_sf_socklist *psl;
670 read_lock(&ipv6_sk_mc_lock);
671 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
672 if (ipv6_addr_equal(&mc->addr, mc_addr))
676 read_unlock(&ipv6_sk_mc_lock);
679 read_lock(&mc->sflock);
682 rv = mc->sfmode == MCAST_EXCLUDE;
686 for (i=0; i<psl->sl_count; i++) {
687 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
690 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
692 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
695 read_unlock(&mc->sflock);
696 read_unlock(&ipv6_sk_mc_lock);
701 static void ma_put(struct ifmcaddr6 *mc)
703 if (atomic_dec_and_test(&mc->mca_refcnt)) {
704 in6_dev_put(mc->idev);
709 static void igmp6_group_added(struct ifmcaddr6 *mc)
711 struct net_device *dev = mc->idev->dev;
712 char buf[MAX_ADDR_LEN];
714 spin_lock_bh(&mc->mca_lock);
715 if (!(mc->mca_flags&MAF_LOADED)) {
716 mc->mca_flags |= MAF_LOADED;
717 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
718 dev_mc_add(dev, buf, dev->addr_len, 0);
720 spin_unlock_bh(&mc->mca_lock);
722 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
725 if (MLD_V1_SEEN(mc->idev)) {
726 igmp6_join_group(mc);
731 mc->mca_crcount = mc->idev->mc_qrv;
732 mld_ifc_event(mc->idev);
735 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
737 struct net_device *dev = mc->idev->dev;
738 char buf[MAX_ADDR_LEN];
740 spin_lock_bh(&mc->mca_lock);
741 if (mc->mca_flags&MAF_LOADED) {
742 mc->mca_flags &= ~MAF_LOADED;
743 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
744 dev_mc_delete(dev, buf, dev->addr_len, 0);
747 if (mc->mca_flags & MAF_NOREPORT)
749 spin_unlock_bh(&mc->mca_lock);
752 igmp6_leave_group(mc);
754 spin_lock_bh(&mc->mca_lock);
755 if (del_timer(&mc->mca_timer))
756 atomic_dec(&mc->mca_refcnt);
758 ip6_mc_clear_src(mc);
759 spin_unlock_bh(&mc->mca_lock);
763 * deleted ifmcaddr6 manipulation
765 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
767 struct ifmcaddr6 *pmc;
769 /* this is an "ifmcaddr6" for convenience; only the fields below
770 * are actually used. In particular, the refcnt and users are not
771 * used for management of the delete list. Using the same structure
772 * for deleted items allows change reports to use common code with
773 * non-deleted or query-response MCA's.
775 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
779 spin_lock_bh(&im->mca_lock);
780 spin_lock_init(&pmc->mca_lock);
781 pmc->idev = im->idev;
783 pmc->mca_addr = im->mca_addr;
784 pmc->mca_crcount = idev->mc_qrv;
785 pmc->mca_sfmode = im->mca_sfmode;
786 if (pmc->mca_sfmode == MCAST_INCLUDE) {
787 struct ip6_sf_list *psf;
789 pmc->mca_tomb = im->mca_tomb;
790 pmc->mca_sources = im->mca_sources;
791 im->mca_tomb = im->mca_sources = NULL;
792 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
793 psf->sf_crcount = pmc->mca_crcount;
795 spin_unlock_bh(&im->mca_lock);
797 write_lock_bh(&idev->mc_lock);
798 pmc->next = idev->mc_tomb;
800 write_unlock_bh(&idev->mc_lock);
803 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
805 struct ifmcaddr6 *pmc, *pmc_prev;
806 struct ip6_sf_list *psf, *psf_next;
808 write_lock_bh(&idev->mc_lock);
810 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
811 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
817 pmc_prev->next = pmc->next;
819 idev->mc_tomb = pmc->next;
821 write_unlock_bh(&idev->mc_lock);
823 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
824 psf_next = psf->sf_next;
827 in6_dev_put(pmc->idev);
832 static void mld_clear_delrec(struct inet6_dev *idev)
834 struct ifmcaddr6 *pmc, *nextpmc;
836 write_lock_bh(&idev->mc_lock);
838 idev->mc_tomb = NULL;
839 write_unlock_bh(&idev->mc_lock);
841 for (; pmc; pmc = nextpmc) {
843 ip6_mc_clear_src(pmc);
844 in6_dev_put(pmc->idev);
848 /* clear dead sources, too */
849 read_lock_bh(&idev->lock);
850 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
851 struct ip6_sf_list *psf, *psf_next;
853 spin_lock_bh(&pmc->mca_lock);
855 pmc->mca_tomb = NULL;
856 spin_unlock_bh(&pmc->mca_lock);
857 for (; psf; psf=psf_next) {
858 psf_next = psf->sf_next;
862 read_unlock_bh(&idev->lock);
867 * device multicast group inc (add if not found)
869 int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
871 struct ifmcaddr6 *mc;
872 struct inet6_dev *idev;
874 idev = in6_dev_get(dev);
879 write_lock_bh(&idev->lock);
881 write_unlock_bh(&idev->lock);
886 for (mc = idev->mc_list; mc; mc = mc->next) {
887 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
889 write_unlock_bh(&idev->lock);
890 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
898 * not found: create a new one.
901 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
904 write_unlock_bh(&idev->lock);
909 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
911 ipv6_addr_copy(&mc->mca_addr, addr);
914 /* mca_stamp should be updated upon changes */
915 mc->mca_cstamp = mc->mca_tstamp = jiffies;
916 atomic_set(&mc->mca_refcnt, 2);
917 spin_lock_init(&mc->mca_lock);
919 /* initial mode is (EX, empty) */
920 mc->mca_sfmode = MCAST_EXCLUDE;
921 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
923 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
924 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
925 mc->mca_flags |= MAF_NOREPORT;
927 mc->next = idev->mc_list;
929 write_unlock_bh(&idev->lock);
931 mld_del_delrec(idev, &mc->mca_addr);
932 igmp6_group_added(mc);
938 * device multicast group del
940 int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
942 struct ifmcaddr6 *ma, **map;
944 write_lock_bh(&idev->lock);
945 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
946 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
947 if (--ma->mca_users == 0) {
949 write_unlock_bh(&idev->lock);
951 igmp6_group_dropped(ma);
956 write_unlock_bh(&idev->lock);
960 write_unlock_bh(&idev->lock);
965 int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
967 struct inet6_dev *idev = in6_dev_get(dev);
973 err = __ipv6_dev_mc_dec(idev, addr);
981 * identify MLD packets for MLD filter exceptions
983 int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
985 struct icmp6hdr *pic;
987 if (nexthdr != IPPROTO_ICMPV6)
990 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
993 pic = icmp6_hdr(skb);
995 switch (pic->icmp6_type) {
996 case ICMPV6_MGM_QUERY:
997 case ICMPV6_MGM_REPORT:
998 case ICMPV6_MGM_REDUCTION:
999 case ICMPV6_MLD2_REPORT:
1008 * check if the interface/address pair is valid
1010 int ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
1011 const struct in6_addr *src_addr)
1013 struct inet6_dev *idev;
1014 struct ifmcaddr6 *mc;
1017 idev = in6_dev_get(dev);
1019 read_lock_bh(&idev->lock);
1020 for (mc = idev->mc_list; mc; mc=mc->next) {
1021 if (ipv6_addr_equal(&mc->mca_addr, group))
1025 if (src_addr && !ipv6_addr_any(src_addr)) {
1026 struct ip6_sf_list *psf;
1028 spin_lock_bh(&mc->mca_lock);
1029 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1030 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1034 rv = psf->sf_count[MCAST_INCLUDE] ||
1035 psf->sf_count[MCAST_EXCLUDE] !=
1036 mc->mca_sfcount[MCAST_EXCLUDE];
1038 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1039 spin_unlock_bh(&mc->mca_lock);
1041 rv = 1; /* don't filter unspecified source */
1043 read_unlock_bh(&idev->lock);
1049 static void mld_gq_start_timer(struct inet6_dev *idev)
1051 int tv = net_random() % idev->mc_maxdelay;
1053 idev->mc_gq_running = 1;
1054 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1058 static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1060 int tv = net_random() % delay;
1062 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1067 * IGMP handling (alias multicast ICMPv6 messages)
1070 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1072 unsigned long delay = resptime;
1074 /* Do not start timer for these addresses */
1075 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1076 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1079 if (del_timer(&ma->mca_timer)) {
1080 atomic_dec(&ma->mca_refcnt);
1081 delay = ma->mca_timer.expires - jiffies;
1084 if (delay >= resptime) {
1086 delay = net_random() % resptime;
1090 ma->mca_timer.expires = jiffies + delay;
1091 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1092 atomic_inc(&ma->mca_refcnt);
1093 ma->mca_flags |= MAF_TIMER_RUNNING;
1096 /* mark EXCLUDE-mode sources */
1097 static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1098 struct in6_addr *srcs)
1100 struct ip6_sf_list *psf;
1104 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1105 if (scount == nsrcs)
1107 for (i=0; i<nsrcs; i++) {
1108 /* skip inactive filters */
1109 if (pmc->mca_sfcount[MCAST_INCLUDE] ||
1110 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1111 psf->sf_count[MCAST_EXCLUDE])
1113 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1119 pmc->mca_flags &= ~MAF_GSQUERY;
1120 if (scount == nsrcs) /* all sources excluded */
1125 static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1126 struct in6_addr *srcs)
1128 struct ip6_sf_list *psf;
1131 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1132 return mld_xmarksources(pmc, nsrcs, srcs);
1134 /* mark INCLUDE-mode sources */
1137 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1138 if (scount == nsrcs)
1140 for (i=0; i<nsrcs; i++) {
1141 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1149 pmc->mca_flags &= ~MAF_GSQUERY;
1152 pmc->mca_flags |= MAF_GSQUERY;
1156 int igmp6_event_query(struct sk_buff *skb)
1158 struct mld2_query *mlh2 = NULL;
1159 struct ifmcaddr6 *ma;
1160 struct in6_addr *group;
1161 unsigned long max_delay;
1162 struct inet6_dev *idev;
1163 struct icmp6hdr *hdr;
1168 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1171 /* compute payload length excluding extension headers */
1172 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
1173 len -= skb_network_header_len(skb);
1175 /* Drop queries with not link local source */
1176 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
1179 idev = in6_dev_get(skb->dev);
1184 hdr = icmp6_hdr(skb);
1185 group = (struct in6_addr *) (hdr + 1);
1186 group_type = ipv6_addr_type(group);
1188 if (group_type != IPV6_ADDR_ANY &&
1189 !(group_type&IPV6_ADDR_MULTICAST)) {
1196 /* MLDv1 router present */
1198 /* Translate milliseconds to jiffies */
1199 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1201 switchback = (idev->mc_qrv + 1) * max_delay;
1202 idev->mc_v1_seen = jiffies + switchback;
1204 /* cancel the interface change timer */
1205 idev->mc_ifc_count = 0;
1206 if (del_timer(&idev->mc_ifc_timer))
1207 __in6_dev_put(idev);
1208 /* clear deleted report items */
1209 mld_clear_delrec(idev);
1210 } else if (len >= 28) {
1211 int srcs_offset = sizeof(struct mld2_query) -
1212 sizeof(struct icmp6hdr);
1213 if (!pskb_may_pull(skb, srcs_offset)) {
1217 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1218 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1221 idev->mc_maxdelay = max_delay;
1223 idev->mc_qrv = mlh2->qrv;
1224 if (group_type == IPV6_ADDR_ANY) { /* general query */
1227 return -EINVAL; /* no sources allowed */
1229 mld_gq_start_timer(idev);
1233 /* mark sources to include, if group & source-specific */
1234 if (mlh2->nsrcs != 0) {
1235 if (!pskb_may_pull(skb, srcs_offset +
1236 ntohs(mlh2->nsrcs) * sizeof(struct in6_addr))) {
1240 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1248 read_lock_bh(&idev->lock);
1249 if (group_type == IPV6_ADDR_ANY) {
1250 for (ma = idev->mc_list; ma; ma=ma->next) {
1251 spin_lock_bh(&ma->mca_lock);
1252 igmp6_group_queried(ma, max_delay);
1253 spin_unlock_bh(&ma->mca_lock);
1256 for (ma = idev->mc_list; ma; ma=ma->next) {
1257 if (!ipv6_addr_equal(group, &ma->mca_addr))
1259 spin_lock_bh(&ma->mca_lock);
1260 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1261 /* gsquery <- gsquery && mark */
1263 ma->mca_flags &= ~MAF_GSQUERY;
1265 /* gsquery <- mark */
1267 ma->mca_flags |= MAF_GSQUERY;
1269 ma->mca_flags &= ~MAF_GSQUERY;
1271 if (!(ma->mca_flags & MAF_GSQUERY) ||
1272 mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
1273 igmp6_group_queried(ma, max_delay);
1274 spin_unlock_bh(&ma->mca_lock);
1278 read_unlock_bh(&idev->lock);
1285 int igmp6_event_report(struct sk_buff *skb)
1287 struct ifmcaddr6 *ma;
1288 struct in6_addr *addrp;
1289 struct inet6_dev *idev;
1290 struct icmp6hdr *hdr;
1293 /* Our own report looped back. Ignore it. */
1294 if (skb->pkt_type == PACKET_LOOPBACK)
1297 /* send our report if the MC router may not have heard this report */
1298 if (skb->pkt_type != PACKET_MULTICAST &&
1299 skb->pkt_type != PACKET_BROADCAST)
1302 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1305 hdr = icmp6_hdr(skb);
1307 /* Drop reports with not link local source */
1308 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
1309 if (addr_type != IPV6_ADDR_ANY &&
1310 !(addr_type&IPV6_ADDR_LINKLOCAL))
1313 addrp = (struct in6_addr *) (hdr + 1);
1315 idev = in6_dev_get(skb->dev);
1320 * Cancel the timer for this group
1323 read_lock_bh(&idev->lock);
1324 for (ma = idev->mc_list; ma; ma=ma->next) {
1325 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1326 spin_lock(&ma->mca_lock);
1327 if (del_timer(&ma->mca_timer))
1328 atomic_dec(&ma->mca_refcnt);
1329 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1330 spin_unlock(&ma->mca_lock);
1334 read_unlock_bh(&idev->lock);
1339 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1340 int gdeleted, int sdeleted)
1343 case MLD2_MODE_IS_INCLUDE:
1344 case MLD2_MODE_IS_EXCLUDE:
1345 if (gdeleted || sdeleted)
1347 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1348 if (pmc->mca_sfmode == MCAST_INCLUDE)
1350 /* don't include if this source is excluded
1353 if (psf->sf_count[MCAST_INCLUDE])
1354 return type == MLD2_MODE_IS_INCLUDE;
1355 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1356 psf->sf_count[MCAST_EXCLUDE];
1359 case MLD2_CHANGE_TO_INCLUDE:
1360 if (gdeleted || sdeleted)
1362 return psf->sf_count[MCAST_INCLUDE] != 0;
1363 case MLD2_CHANGE_TO_EXCLUDE:
1364 if (gdeleted || sdeleted)
1366 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1367 psf->sf_count[MCAST_INCLUDE])
1369 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1370 psf->sf_count[MCAST_EXCLUDE];
1371 case MLD2_ALLOW_NEW_SOURCES:
1372 if (gdeleted || !psf->sf_crcount)
1374 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1375 case MLD2_BLOCK_OLD_SOURCES:
1376 if (pmc->mca_sfmode == MCAST_INCLUDE)
1377 return gdeleted || (psf->sf_crcount && sdeleted);
1378 return psf->sf_crcount && !gdeleted && !sdeleted;
1384 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1386 struct ip6_sf_list *psf;
1389 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1390 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1397 static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1399 struct net *net = dev_net(dev);
1400 struct sock *sk = net->ipv6.igmp_sk;
1401 struct sk_buff *skb;
1402 struct mld2_report *pmr;
1403 struct in6_addr addr_buf;
1404 const struct in6_addr *saddr;
1406 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1407 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1410 /* we assume size > sizeof(ra) here */
1411 skb = sock_alloc_send_skb(sk, size + LL_ALLOCATED_SPACE(dev), 1, &err);
1416 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1418 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1419 /* <draft-ietf-magma-mld-source-05.txt>:
1420 * use unspecified address as the source address
1421 * when a valid link-local address is not available.
1423 saddr = &in6addr_any;
1427 ip6_nd_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
1429 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1431 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
1432 skb_put(skb, sizeof(*pmr));
1433 pmr = (struct mld2_report *)skb_transport_header(skb);
1434 pmr->type = ICMPV6_MLD2_REPORT;
1442 static void mld_sendpack(struct sk_buff *skb)
1444 struct ipv6hdr *pip6 = ipv6_hdr(skb);
1445 struct mld2_report *pmr =
1446 (struct mld2_report *)skb_transport_header(skb);
1447 int payload_len, mldlen;
1448 struct inet6_dev *idev = in6_dev_get(skb->dev);
1449 struct net *net = dev_net(skb->dev);
1453 IP6_INC_STATS(idev, IPSTATS_MIB_OUTREQUESTS);
1454 payload_len = (skb->tail - skb->network_header) - sizeof(*pip6);
1455 mldlen = skb->tail - skb->transport_header;
1456 pip6->payload_len = htons(payload_len);
1458 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1459 IPPROTO_ICMPV6, csum_partial(skb_transport_header(skb),
1462 skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1469 icmpv6_flow_init(net->ipv6.igmp_sk, &fl, ICMPV6_MLD2_REPORT,
1470 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1473 err = xfrm_lookup(&skb->dst, &fl, NULL, 0);
1477 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1481 ICMP6MSGOUT_INC_STATS_BH(idev, ICMPV6_MLD2_REPORT);
1482 ICMP6_INC_STATS_BH(idev, ICMP6_MIB_OUTMSGS);
1483 IP6_INC_STATS_BH(idev, IPSTATS_MIB_OUTMCASTPKTS);
1485 IP6_INC_STATS_BH(idev, IPSTATS_MIB_OUTDISCARDS);
1487 if (likely(idev != NULL))
1496 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1498 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1501 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1502 int type, struct mld2_grec **ppgr)
1504 struct net_device *dev = pmc->idev->dev;
1505 struct mld2_report *pmr;
1506 struct mld2_grec *pgr;
1509 skb = mld_newpack(dev, dev->mtu);
1512 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1513 pgr->grec_type = type;
1514 pgr->grec_auxwords = 0;
1515 pgr->grec_nsrcs = 0;
1516 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1517 pmr = (struct mld2_report *)skb_transport_header(skb);
1518 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1523 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1524 skb_tailroom(skb)) : 0)
1526 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1527 int type, int gdeleted, int sdeleted)
1529 struct net_device *dev = pmc->idev->dev;
1530 struct mld2_report *pmr;
1531 struct mld2_grec *pgr = NULL;
1532 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1533 int scount, stotal, first, isquery, truncate;
1535 if (pmc->mca_flags & MAF_NOREPORT)
1538 isquery = type == MLD2_MODE_IS_INCLUDE ||
1539 type == MLD2_MODE_IS_EXCLUDE;
1540 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1541 type == MLD2_CHANGE_TO_EXCLUDE;
1543 stotal = scount = 0;
1545 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1550 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
1552 /* EX and TO_EX get a fresh packet, if needed */
1554 if (pmr && pmr->ngrec &&
1555 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1558 skb = mld_newpack(dev, dev->mtu);
1563 for (psf=*psf_list; psf; psf=psf_next) {
1564 struct in6_addr *psrc;
1566 psf_next = psf->sf_next;
1568 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1573 /* clear marks on query responses */
1577 if (AVAILABLE(skb) < sizeof(*psrc) +
1578 first*sizeof(struct mld2_grec)) {
1579 if (truncate && !first)
1580 break; /* truncate these */
1582 pgr->grec_nsrcs = htons(scount);
1585 skb = mld_newpack(dev, dev->mtu);
1590 skb = add_grhead(skb, pmc, type, &pgr);
1595 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1596 *psrc = psf->sf_addr;
1598 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1599 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1601 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1603 psf_prev->sf_next = psf->sf_next;
1605 *psf_list = psf->sf_next;
1615 if (type == MLD2_ALLOW_NEW_SOURCES ||
1616 type == MLD2_BLOCK_OLD_SOURCES)
1618 if (pmc->mca_crcount || isquery) {
1619 /* make sure we have room for group header */
1620 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1622 skb = NULL; /* add_grhead will get a new one */
1624 skb = add_grhead(skb, pmc, type, &pgr);
1628 pgr->grec_nsrcs = htons(scount);
1631 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1635 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1637 struct sk_buff *skb = NULL;
1641 read_lock_bh(&idev->lock);
1642 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1643 if (pmc->mca_flags & MAF_NOREPORT)
1645 spin_lock_bh(&pmc->mca_lock);
1646 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1647 type = MLD2_MODE_IS_EXCLUDE;
1649 type = MLD2_MODE_IS_INCLUDE;
1650 skb = add_grec(skb, pmc, type, 0, 0);
1651 spin_unlock_bh(&pmc->mca_lock);
1653 read_unlock_bh(&idev->lock);
1655 spin_lock_bh(&pmc->mca_lock);
1656 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1657 type = MLD2_MODE_IS_EXCLUDE;
1659 type = MLD2_MODE_IS_INCLUDE;
1660 skb = add_grec(skb, pmc, type, 0, 0);
1661 spin_unlock_bh(&pmc->mca_lock);
1668 * remove zero-count source records from a source filter list
1670 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1672 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1675 for (psf=*ppsf; psf; psf = psf_next) {
1676 psf_next = psf->sf_next;
1677 if (psf->sf_crcount == 0) {
1679 psf_prev->sf_next = psf->sf_next;
1681 *ppsf = psf->sf_next;
1688 static void mld_send_cr(struct inet6_dev *idev)
1690 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1691 struct sk_buff *skb = NULL;
1694 read_lock_bh(&idev->lock);
1695 write_lock_bh(&idev->mc_lock);
1699 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1700 pmc_next = pmc->next;
1701 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1702 type = MLD2_BLOCK_OLD_SOURCES;
1703 dtype = MLD2_BLOCK_OLD_SOURCES;
1704 skb = add_grec(skb, pmc, type, 1, 0);
1705 skb = add_grec(skb, pmc, dtype, 1, 1);
1707 if (pmc->mca_crcount) {
1708 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1709 type = MLD2_CHANGE_TO_INCLUDE;
1710 skb = add_grec(skb, pmc, type, 1, 0);
1713 if (pmc->mca_crcount == 0) {
1714 mld_clear_zeros(&pmc->mca_tomb);
1715 mld_clear_zeros(&pmc->mca_sources);
1718 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1719 !pmc->mca_sources) {
1721 pmc_prev->next = pmc_next;
1723 idev->mc_tomb = pmc_next;
1724 in6_dev_put(pmc->idev);
1729 write_unlock_bh(&idev->mc_lock);
1732 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1733 spin_lock_bh(&pmc->mca_lock);
1734 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1735 type = MLD2_BLOCK_OLD_SOURCES;
1736 dtype = MLD2_ALLOW_NEW_SOURCES;
1738 type = MLD2_ALLOW_NEW_SOURCES;
1739 dtype = MLD2_BLOCK_OLD_SOURCES;
1741 skb = add_grec(skb, pmc, type, 0, 0);
1742 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1744 /* filter mode changes */
1745 if (pmc->mca_crcount) {
1746 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1747 type = MLD2_CHANGE_TO_EXCLUDE;
1749 type = MLD2_CHANGE_TO_INCLUDE;
1750 skb = add_grec(skb, pmc, type, 0, 0);
1753 spin_unlock_bh(&pmc->mca_lock);
1755 read_unlock_bh(&idev->lock);
1758 (void) mld_sendpack(skb);
1761 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1763 struct net *net = dev_net(dev);
1764 struct sock *sk = net->ipv6.igmp_sk;
1765 struct inet6_dev *idev;
1766 struct sk_buff *skb;
1767 struct icmp6hdr *hdr;
1768 const struct in6_addr *snd_addr, *saddr;
1769 struct in6_addr *addrp;
1770 struct in6_addr addr_buf;
1771 int err, len, payload_len, full_len;
1772 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1773 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1778 IP6_INC_STATS(__in6_dev_get(dev),
1779 IPSTATS_MIB_OUTREQUESTS);
1781 if (type == ICMPV6_MGM_REDUCTION)
1782 snd_addr = &in6addr_linklocal_allrouters;
1786 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1787 payload_len = len + sizeof(ra);
1788 full_len = sizeof(struct ipv6hdr) + payload_len;
1790 skb = sock_alloc_send_skb(sk, LL_ALLOCATED_SPACE(dev) + full_len, 1, &err);
1794 IP6_INC_STATS(__in6_dev_get(dev),
1795 IPSTATS_MIB_OUTDISCARDS);
1800 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1802 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1803 /* <draft-ietf-magma-mld-source-05.txt>:
1804 * use unspecified address as the source address
1805 * when a valid link-local address is not available.
1807 saddr = &in6addr_any;
1811 ip6_nd_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
1813 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1815 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1816 memset(hdr, 0, sizeof(struct icmp6hdr));
1817 hdr->icmp6_type = type;
1819 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1820 ipv6_addr_copy(addrp, addr);
1822 hdr->icmp6_cksum = csum_ipv6_magic(saddr, snd_addr, len,
1824 csum_partial((__u8 *) hdr, len, 0));
1826 idev = in6_dev_get(skb->dev);
1828 skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1834 icmpv6_flow_init(sk, &fl, type,
1835 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1838 err = xfrm_lookup(&skb->dst, &fl, NULL, 0);
1842 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1846 ICMP6MSGOUT_INC_STATS(idev, type);
1847 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1848 IP6_INC_STATS(idev, IPSTATS_MIB_OUTMCASTPKTS);
1850 IP6_INC_STATS(idev, IPSTATS_MIB_OUTDISCARDS);
1852 if (likely(idev != NULL))
1861 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1862 struct in6_addr *psfsrc)
1864 struct ip6_sf_list *psf, *psf_prev;
1868 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1869 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1873 if (!psf || psf->sf_count[sfmode] == 0) {
1874 /* source filter not found, or count wrong => bug */
1877 psf->sf_count[sfmode]--;
1878 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1879 struct inet6_dev *idev = pmc->idev;
1881 /* no more filters for this source */
1883 psf_prev->sf_next = psf->sf_next;
1885 pmc->mca_sources = psf->sf_next;
1886 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1887 !MLD_V1_SEEN(idev)) {
1888 psf->sf_crcount = idev->mc_qrv;
1889 psf->sf_next = pmc->mca_tomb;
1890 pmc->mca_tomb = psf;
1898 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1899 int sfmode, int sfcount, struct in6_addr *psfsrc,
1902 struct ifmcaddr6 *pmc;
1908 read_lock_bh(&idev->lock);
1909 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1910 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1914 /* MCA not found?? bug */
1915 read_unlock_bh(&idev->lock);
1918 spin_lock_bh(&pmc->mca_lock);
1921 if (!pmc->mca_sfcount[sfmode]) {
1922 spin_unlock_bh(&pmc->mca_lock);
1923 read_unlock_bh(&idev->lock);
1926 pmc->mca_sfcount[sfmode]--;
1929 for (i=0; i<sfcount; i++) {
1930 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1932 changerec |= rv > 0;
1936 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1937 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1938 pmc->mca_sfcount[MCAST_INCLUDE]) {
1939 struct ip6_sf_list *psf;
1941 /* filter mode change */
1942 pmc->mca_sfmode = MCAST_INCLUDE;
1943 pmc->mca_crcount = idev->mc_qrv;
1944 idev->mc_ifc_count = pmc->mca_crcount;
1945 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1946 psf->sf_crcount = 0;
1947 mld_ifc_event(pmc->idev);
1948 } else if (sf_setstate(pmc) || changerec)
1949 mld_ifc_event(pmc->idev);
1950 spin_unlock_bh(&pmc->mca_lock);
1951 read_unlock_bh(&idev->lock);
1956 * Add multicast single-source filter to the interface list
1958 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1959 struct in6_addr *psfsrc, int delta)
1961 struct ip6_sf_list *psf, *psf_prev;
1964 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1965 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1970 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1974 psf->sf_addr = *psfsrc;
1976 psf_prev->sf_next = psf;
1978 pmc->mca_sources = psf;
1980 psf->sf_count[sfmode]++;
1984 static void sf_markstate(struct ifmcaddr6 *pmc)
1986 struct ip6_sf_list *psf;
1987 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1989 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1990 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1991 psf->sf_oldin = mca_xcount ==
1992 psf->sf_count[MCAST_EXCLUDE] &&
1993 !psf->sf_count[MCAST_INCLUDE];
1995 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1998 static int sf_setstate(struct ifmcaddr6 *pmc)
2000 struct ip6_sf_list *psf, *dpsf;
2001 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2002 int qrv = pmc->idev->mc_qrv;
2006 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
2007 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2008 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2009 !psf->sf_count[MCAST_INCLUDE];
2011 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2013 if (!psf->sf_oldin) {
2014 struct ip6_sf_list *prev = NULL;
2016 for (dpsf=pmc->mca_tomb; dpsf;
2017 dpsf=dpsf->sf_next) {
2018 if (ipv6_addr_equal(&dpsf->sf_addr,
2025 prev->sf_next = dpsf->sf_next;
2027 pmc->mca_tomb = dpsf->sf_next;
2030 psf->sf_crcount = qrv;
2033 } else if (psf->sf_oldin) {
2034 psf->sf_crcount = 0;
2036 * add or update "delete" records if an active filter
2039 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2040 if (ipv6_addr_equal(&dpsf->sf_addr,
2044 dpsf = (struct ip6_sf_list *)
2045 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2049 /* pmc->mca_lock held by callers */
2050 dpsf->sf_next = pmc->mca_tomb;
2051 pmc->mca_tomb = dpsf;
2053 dpsf->sf_crcount = qrv;
2061 * Add multicast source filter list to the interface list
2063 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
2064 int sfmode, int sfcount, struct in6_addr *psfsrc,
2067 struct ifmcaddr6 *pmc;
2073 read_lock_bh(&idev->lock);
2074 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2075 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2079 /* MCA not found?? bug */
2080 read_unlock_bh(&idev->lock);
2083 spin_lock_bh(&pmc->mca_lock);
2086 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2088 pmc->mca_sfcount[sfmode]++;
2090 for (i=0; i<sfcount; i++) {
2091 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
2099 pmc->mca_sfcount[sfmode]--;
2101 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2102 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2103 struct inet6_dev *idev = pmc->idev;
2104 struct ip6_sf_list *psf;
2106 /* filter mode change */
2107 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2108 pmc->mca_sfmode = MCAST_EXCLUDE;
2109 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2110 pmc->mca_sfmode = MCAST_INCLUDE;
2111 /* else no filters; keep old mode for reports */
2113 pmc->mca_crcount = idev->mc_qrv;
2114 idev->mc_ifc_count = pmc->mca_crcount;
2115 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2116 psf->sf_crcount = 0;
2117 mld_ifc_event(idev);
2118 } else if (sf_setstate(pmc))
2119 mld_ifc_event(idev);
2120 spin_unlock_bh(&pmc->mca_lock);
2121 read_unlock_bh(&idev->lock);
2125 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2127 struct ip6_sf_list *psf, *nextpsf;
2129 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2130 nextpsf = psf->sf_next;
2133 pmc->mca_tomb = NULL;
2134 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2135 nextpsf = psf->sf_next;
2138 pmc->mca_sources = NULL;
2139 pmc->mca_sfmode = MCAST_EXCLUDE;
2140 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2141 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2145 static void igmp6_join_group(struct ifmcaddr6 *ma)
2147 unsigned long delay;
2149 if (ma->mca_flags & MAF_NOREPORT)
2152 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2154 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2156 spin_lock_bh(&ma->mca_lock);
2157 if (del_timer(&ma->mca_timer)) {
2158 atomic_dec(&ma->mca_refcnt);
2159 delay = ma->mca_timer.expires - jiffies;
2162 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2163 atomic_inc(&ma->mca_refcnt);
2164 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2165 spin_unlock_bh(&ma->mca_lock);
2168 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2169 struct inet6_dev *idev)
2173 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2174 * so no other readers or writers of iml or its sflist
2177 /* any-source empty exclude case */
2178 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2180 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2181 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2182 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2187 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2189 if (MLD_V1_SEEN(ma->idev)) {
2190 if (ma->mca_flags & MAF_LAST_REPORTER)
2191 igmp6_send(&ma->mca_addr, ma->idev->dev,
2192 ICMPV6_MGM_REDUCTION);
2194 mld_add_delrec(ma->idev, ma);
2195 mld_ifc_event(ma->idev);
2199 static void mld_gq_timer_expire(unsigned long data)
2201 struct inet6_dev *idev = (struct inet6_dev *)data;
2203 idev->mc_gq_running = 0;
2204 mld_send_report(idev, NULL);
2205 __in6_dev_put(idev);
2208 static void mld_ifc_timer_expire(unsigned long data)
2210 struct inet6_dev *idev = (struct inet6_dev *)data;
2213 if (idev->mc_ifc_count) {
2214 idev->mc_ifc_count--;
2215 if (idev->mc_ifc_count)
2216 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2218 __in6_dev_put(idev);
2221 static void mld_ifc_event(struct inet6_dev *idev)
2223 if (MLD_V1_SEEN(idev))
2225 idev->mc_ifc_count = idev->mc_qrv;
2226 mld_ifc_start_timer(idev, 1);
2230 static void igmp6_timer_handler(unsigned long data)
2232 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2234 if (MLD_V1_SEEN(ma->idev))
2235 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2237 mld_send_report(ma->idev, ma);
2239 spin_lock(&ma->mca_lock);
2240 ma->mca_flags |= MAF_LAST_REPORTER;
2241 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2242 spin_unlock(&ma->mca_lock);
2246 /* Device going down */
2248 void ipv6_mc_down(struct inet6_dev *idev)
2250 struct ifmcaddr6 *i;
2252 /* Withdraw multicast list */
2254 read_lock_bh(&idev->lock);
2255 idev->mc_ifc_count = 0;
2256 if (del_timer(&idev->mc_ifc_timer))
2257 __in6_dev_put(idev);
2258 idev->mc_gq_running = 0;
2259 if (del_timer(&idev->mc_gq_timer))
2260 __in6_dev_put(idev);
2262 for (i = idev->mc_list; i; i=i->next)
2263 igmp6_group_dropped(i);
2264 read_unlock_bh(&idev->lock);
2266 mld_clear_delrec(idev);
2270 /* Device going up */
2272 void ipv6_mc_up(struct inet6_dev *idev)
2274 struct ifmcaddr6 *i;
2276 /* Install multicast list, except for all-nodes (already installed) */
2278 read_lock_bh(&idev->lock);
2279 for (i = idev->mc_list; i; i=i->next)
2280 igmp6_group_added(i);
2281 read_unlock_bh(&idev->lock);
2284 /* IPv6 device initialization. */
2286 void ipv6_mc_init_dev(struct inet6_dev *idev)
2288 write_lock_bh(&idev->lock);
2289 rwlock_init(&idev->mc_lock);
2290 idev->mc_gq_running = 0;
2291 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2292 (unsigned long)idev);
2293 idev->mc_tomb = NULL;
2294 idev->mc_ifc_count = 0;
2295 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2296 (unsigned long)idev);
2297 idev->mc_qrv = MLD_QRV_DEFAULT;
2298 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2299 idev->mc_v1_seen = 0;
2300 write_unlock_bh(&idev->lock);
2304 * Device is about to be destroyed: clean up.
2307 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2309 struct ifmcaddr6 *i;
2311 /* Deactivate timers */
2314 /* Delete all-nodes address. */
2315 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2316 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2319 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
2321 if (idev->cnf.forwarding)
2322 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
2324 write_lock_bh(&idev->lock);
2325 while ((i = idev->mc_list) != NULL) {
2326 idev->mc_list = i->next;
2327 write_unlock_bh(&idev->lock);
2329 igmp6_group_dropped(i);
2332 write_lock_bh(&idev->lock);
2334 write_unlock_bh(&idev->lock);
2337 #ifdef CONFIG_PROC_FS
2338 struct igmp6_mc_iter_state {
2339 struct seq_net_private p;
2340 struct net_device *dev;
2341 struct inet6_dev *idev;
2344 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2346 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2348 struct ifmcaddr6 *im = NULL;
2349 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2350 struct net *net = seq_file_net(seq);
2353 for_each_netdev(net, state->dev) {
2354 struct inet6_dev *idev;
2355 idev = in6_dev_get(state->dev);
2358 read_lock_bh(&idev->lock);
2364 read_unlock_bh(&idev->lock);
2370 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2372 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2376 if (likely(state->idev != NULL)) {
2377 read_unlock_bh(&state->idev->lock);
2378 in6_dev_put(state->idev);
2380 state->dev = next_net_device(state->dev);
2385 state->idev = in6_dev_get(state->dev);
2388 read_lock_bh(&state->idev->lock);
2389 im = state->idev->mc_list;
2394 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2396 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2398 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2400 return pos ? NULL : im;
2403 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2404 __acquires(dev_base_lock)
2406 read_lock(&dev_base_lock);
2407 return igmp6_mc_get_idx(seq, *pos);
2410 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2412 struct ifmcaddr6 *im;
2413 im = igmp6_mc_get_next(seq, v);
2418 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2419 __releases(dev_base_lock)
2421 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2422 if (likely(state->idev != NULL)) {
2423 read_unlock_bh(&state->idev->lock);
2424 in6_dev_put(state->idev);
2428 read_unlock(&dev_base_lock);
2431 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2433 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2434 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2437 "%-4d %-15s " NIP6_SEQFMT " %5d %08X %ld\n",
2438 state->dev->ifindex, state->dev->name,
2440 im->mca_users, im->mca_flags,
2441 (im->mca_flags&MAF_TIMER_RUNNING) ?
2442 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2446 static const struct seq_operations igmp6_mc_seq_ops = {
2447 .start = igmp6_mc_seq_start,
2448 .next = igmp6_mc_seq_next,
2449 .stop = igmp6_mc_seq_stop,
2450 .show = igmp6_mc_seq_show,
2453 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2455 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2456 sizeof(struct igmp6_mc_iter_state));
2459 static const struct file_operations igmp6_mc_seq_fops = {
2460 .owner = THIS_MODULE,
2461 .open = igmp6_mc_seq_open,
2463 .llseek = seq_lseek,
2464 .release = seq_release_net,
2467 struct igmp6_mcf_iter_state {
2468 struct seq_net_private p;
2469 struct net_device *dev;
2470 struct inet6_dev *idev;
2471 struct ifmcaddr6 *im;
2474 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2476 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2478 struct ip6_sf_list *psf = NULL;
2479 struct ifmcaddr6 *im = NULL;
2480 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2481 struct net *net = seq_file_net(seq);
2485 for_each_netdev(net, state->dev) {
2486 struct inet6_dev *idev;
2487 idev = in6_dev_get(state->dev);
2488 if (unlikely(idev == NULL))
2490 read_lock_bh(&idev->lock);
2492 if (likely(im != NULL)) {
2493 spin_lock_bh(&im->mca_lock);
2494 psf = im->mca_sources;
2495 if (likely(psf != NULL)) {
2500 spin_unlock_bh(&im->mca_lock);
2502 read_unlock_bh(&idev->lock);
2508 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2510 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2514 spin_unlock_bh(&state->im->mca_lock);
2515 state->im = state->im->next;
2516 while (!state->im) {
2517 if (likely(state->idev != NULL)) {
2518 read_unlock_bh(&state->idev->lock);
2519 in6_dev_put(state->idev);
2521 state->dev = next_net_device(state->dev);
2526 state->idev = in6_dev_get(state->dev);
2529 read_lock_bh(&state->idev->lock);
2530 state->im = state->idev->mc_list;
2534 spin_lock_bh(&state->im->mca_lock);
2535 psf = state->im->mca_sources;
2541 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2543 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2545 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2547 return pos ? NULL : psf;
2550 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2551 __acquires(dev_base_lock)
2553 read_lock(&dev_base_lock);
2554 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2557 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2559 struct ip6_sf_list *psf;
2560 if (v == SEQ_START_TOKEN)
2561 psf = igmp6_mcf_get_first(seq);
2563 psf = igmp6_mcf_get_next(seq, v);
2568 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2569 __releases(dev_base_lock)
2571 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2572 if (likely(state->im != NULL)) {
2573 spin_unlock_bh(&state->im->mca_lock);
2576 if (likely(state->idev != NULL)) {
2577 read_unlock_bh(&state->idev->lock);
2578 in6_dev_put(state->idev);
2582 read_unlock(&dev_base_lock);
2585 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2587 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2588 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2590 if (v == SEQ_START_TOKEN) {
2593 "%32s %32s %6s %6s\n", "Idx",
2594 "Device", "Multicast Address",
2595 "Source Address", "INC", "EXC");
2598 "%3d %6.6s " NIP6_SEQFMT " " NIP6_SEQFMT " %6lu %6lu\n",
2599 state->dev->ifindex, state->dev->name,
2600 NIP6(state->im->mca_addr),
2602 psf->sf_count[MCAST_INCLUDE],
2603 psf->sf_count[MCAST_EXCLUDE]);
2608 static const struct seq_operations igmp6_mcf_seq_ops = {
2609 .start = igmp6_mcf_seq_start,
2610 .next = igmp6_mcf_seq_next,
2611 .stop = igmp6_mcf_seq_stop,
2612 .show = igmp6_mcf_seq_show,
2615 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2617 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2618 sizeof(struct igmp6_mcf_iter_state));
2621 static const struct file_operations igmp6_mcf_seq_fops = {
2622 .owner = THIS_MODULE,
2623 .open = igmp6_mcf_seq_open,
2625 .llseek = seq_lseek,
2626 .release = seq_release_net,
2629 static int igmp6_proc_init(struct net *net)
2634 if (!proc_net_fops_create(net, "igmp6", S_IRUGO, &igmp6_mc_seq_fops))
2636 if (!proc_net_fops_create(net, "mcfilter6", S_IRUGO,
2637 &igmp6_mcf_seq_fops))
2638 goto out_proc_net_igmp6;
2645 proc_net_remove(net, "igmp6");
2649 static void igmp6_proc_exit(struct net *net)
2651 proc_net_remove(net, "mcfilter6");
2652 proc_net_remove(net, "igmp6");
2655 static int igmp6_proc_init(struct net *net)
2659 static void igmp6_proc_exit(struct net *net)
2665 static int igmp6_net_init(struct net *net)
2669 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2670 SOCK_RAW, IPPROTO_ICMPV6, net);
2673 "Failed to initialize the IGMP6 control socket (err %d).\n",
2678 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
2680 err = igmp6_proc_init(net);
2682 goto out_sock_create;
2687 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2691 static void igmp6_net_exit(struct net *net)
2693 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2694 igmp6_proc_exit(net);
2697 static struct pernet_operations igmp6_net_ops = {
2698 .init = igmp6_net_init,
2699 .exit = igmp6_net_exit,
2702 int __init igmp6_init(void)
2704 return register_pernet_subsys(&igmp6_net_ops);
2707 void igmp6_cleanup(void)
2709 unregister_pernet_subsys(&igmp6_net_ops);