2 * Multicast support for IPv6
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: mcast.c,v 1.40 2002/02/08 03:57:19 davem Exp $
10 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
20 * yoshfuji : fix format of router-alert option
21 * YOSHIFUJI Hideaki @USAGI:
22 * Fixed source address for MLD message based on
23 * <draft-ietf-magma-mld-source-05.txt>.
24 * YOSHIFUJI Hideaki @USAGI:
25 * - Ignore Queries for invalid addresses.
26 * - MLD for link-local addresses.
27 * David L Stevens <dlstevens@us.ibm.com>:
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/errno.h>
34 #include <linux/types.h>
35 #include <linux/string.h>
36 #include <linux/socket.h>
37 #include <linux/sockios.h>
38 #include <linux/jiffies.h>
39 #include <linux/times.h>
40 #include <linux/net.h>
42 #include <linux/in6.h>
43 #include <linux/netdevice.h>
44 #include <linux/if_arp.h>
45 #include <linux/route.h>
46 #include <linux/init.h>
47 #include <linux/proc_fs.h>
48 #include <linux/seq_file.h>
50 #include <linux/netfilter.h>
51 #include <linux/netfilter_ipv6.h>
57 #include <net/protocol.h>
58 #include <net/if_inet6.h>
59 #include <net/ndisc.h>
60 #include <net/addrconf.h>
61 #include <net/ip6_route.h>
63 #include <net/ip6_checksum.h>
65 /* Set to 3 to get tracing... */
69 #define MDBG(x) printk x
75 * These header formats should be in a separate include file, but icmpv6.h
76 * doesn't have in6_addr defined in all cases, there is no __u128, and no
77 * other files reference these.
82 /* Multicast Listener Discovery version 2 headers */
88 struct in6_addr grec_mca
;
89 struct in6_addr grec_src
[0];
98 struct mld2_grec grec
[0];
108 #if defined(__LITTLE_ENDIAN_BITFIELD)
112 #elif defined(__BIG_ENDIAN_BITFIELD)
117 #error "Please fix <asm/byteorder.h>"
121 struct in6_addr srcs
[0];
124 static struct in6_addr mld2_all_mcr
= MLD2_ALL_MCR_INIT
;
126 /* Big mc list lock for all the sockets */
127 static DEFINE_RWLOCK(ipv6_sk_mc_lock
);
129 static struct socket
*igmp6_socket
;
131 int __ipv6_dev_mc_dec(struct inet6_dev
*idev
, struct in6_addr
*addr
);
133 static void igmp6_join_group(struct ifmcaddr6
*ma
);
134 static void igmp6_leave_group(struct ifmcaddr6
*ma
);
135 static void igmp6_timer_handler(unsigned long data
);
137 static void mld_gq_timer_expire(unsigned long data
);
138 static void mld_ifc_timer_expire(unsigned long data
);
139 static void mld_ifc_event(struct inet6_dev
*idev
);
140 static void mld_add_delrec(struct inet6_dev
*idev
, struct ifmcaddr6
*pmc
);
141 static void mld_del_delrec(struct inet6_dev
*idev
, struct in6_addr
*addr
);
142 static void mld_clear_delrec(struct inet6_dev
*idev
);
143 static int sf_setstate(struct ifmcaddr6
*pmc
);
144 static void sf_markstate(struct ifmcaddr6
*pmc
);
145 static void ip6_mc_clear_src(struct ifmcaddr6
*pmc
);
146 static int ip6_mc_del_src(struct inet6_dev
*idev
, struct in6_addr
*pmca
,
147 int sfmode
, int sfcount
, struct in6_addr
*psfsrc
,
149 static int ip6_mc_add_src(struct inet6_dev
*idev
, struct in6_addr
*pmca
,
150 int sfmode
, int sfcount
, struct in6_addr
*psfsrc
,
152 static int ip6_mc_leave_src(struct sock
*sk
, struct ipv6_mc_socklist
*iml
,
153 struct inet6_dev
*idev
);
156 #define IGMP6_UNSOLICITED_IVAL (10*HZ)
157 #define MLD_QRV_DEFAULT 2
159 #define MLD_V1_SEEN(idev) (ipv6_devconf.force_mld_version == 1 || \
160 (idev)->cnf.force_mld_version == 1 || \
161 ((idev)->mc_v1_seen && \
162 time_before(jiffies, (idev)->mc_v1_seen)))
164 #define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
165 #define MLDV2_EXP(thresh, nbmant, nbexp, value) \
166 ((value) < (thresh) ? (value) : \
167 ((MLDV2_MASK(value, nbmant) | (1<<(nbmant+nbexp))) << \
168 (MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp))))
170 #define MLDV2_QQIC(value) MLDV2_EXP(0x80, 4, 3, value)
171 #define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
173 #define IPV6_MLD_MAX_MSF 10
175 int sysctl_mld_max_msf
= IPV6_MLD_MAX_MSF
;
178 * socket join on multicast group
181 int ipv6_sock_mc_join(struct sock
*sk
, int ifindex
, struct in6_addr
*addr
)
183 struct net_device
*dev
= NULL
;
184 struct ipv6_mc_socklist
*mc_lst
;
185 struct ipv6_pinfo
*np
= inet6_sk(sk
);
188 if (!ipv6_addr_is_multicast(addr
))
191 mc_lst
= sock_kmalloc(sk
, sizeof(struct ipv6_mc_socklist
), GFP_KERNEL
);
197 ipv6_addr_copy(&mc_lst
->addr
, addr
);
201 rt
= rt6_lookup(addr
, NULL
, 0, 0);
205 dst_release(&rt
->u
.dst
);
208 dev
= dev_get_by_index(ifindex
);
211 sock_kfree_s(sk
, mc_lst
, sizeof(*mc_lst
));
215 mc_lst
->ifindex
= dev
->ifindex
;
216 mc_lst
->sfmode
= MCAST_EXCLUDE
;
217 mc_lst
->sflist
= NULL
;
220 * now add/increase the group membership on the device
223 err
= ipv6_dev_mc_inc(dev
, addr
);
226 sock_kfree_s(sk
, mc_lst
, sizeof(*mc_lst
));
231 write_lock_bh(&ipv6_sk_mc_lock
);
232 mc_lst
->next
= np
->ipv6_mc_list
;
233 np
->ipv6_mc_list
= mc_lst
;
234 write_unlock_bh(&ipv6_sk_mc_lock
);
242 * socket leave on multicast group
244 int ipv6_sock_mc_drop(struct sock
*sk
, int ifindex
, struct in6_addr
*addr
)
246 struct ipv6_pinfo
*np
= inet6_sk(sk
);
247 struct ipv6_mc_socklist
*mc_lst
, **lnk
;
249 write_lock_bh(&ipv6_sk_mc_lock
);
250 for (lnk
= &np
->ipv6_mc_list
; (mc_lst
= *lnk
) !=NULL
; lnk
= &mc_lst
->next
) {
251 if ((ifindex
== 0 || mc_lst
->ifindex
== ifindex
) &&
252 ipv6_addr_equal(&mc_lst
->addr
, addr
)) {
253 struct net_device
*dev
;
256 write_unlock_bh(&ipv6_sk_mc_lock
);
258 if ((dev
= dev_get_by_index(mc_lst
->ifindex
)) != NULL
) {
259 struct inet6_dev
*idev
= in6_dev_get(dev
);
262 (void) ip6_mc_leave_src(sk
,mc_lst
,idev
);
263 __ipv6_dev_mc_dec(idev
, &mc_lst
->addr
);
268 sock_kfree_s(sk
, mc_lst
, sizeof(*mc_lst
));
272 write_unlock_bh(&ipv6_sk_mc_lock
);
277 static struct inet6_dev
*ip6_mc_find_dev(struct in6_addr
*group
, int ifindex
)
279 struct net_device
*dev
= NULL
;
280 struct inet6_dev
*idev
= NULL
;
285 rt
= rt6_lookup(group
, NULL
, 0, 0);
289 dst_release(&rt
->u
.dst
);
292 dev
= dev_get_by_index(ifindex
);
296 idev
= in6_dev_get(dev
);
301 read_lock_bh(&idev
->lock
);
303 read_unlock_bh(&idev
->lock
);
311 void ipv6_sock_mc_close(struct sock
*sk
)
313 struct ipv6_pinfo
*np
= inet6_sk(sk
);
314 struct ipv6_mc_socklist
*mc_lst
;
316 write_lock_bh(&ipv6_sk_mc_lock
);
317 while ((mc_lst
= np
->ipv6_mc_list
) != NULL
) {
318 struct net_device
*dev
;
320 np
->ipv6_mc_list
= mc_lst
->next
;
321 write_unlock_bh(&ipv6_sk_mc_lock
);
323 dev
= dev_get_by_index(mc_lst
->ifindex
);
325 struct inet6_dev
*idev
= in6_dev_get(dev
);
328 (void) ip6_mc_leave_src(sk
, mc_lst
, idev
);
329 __ipv6_dev_mc_dec(idev
, &mc_lst
->addr
);
335 sock_kfree_s(sk
, mc_lst
, sizeof(*mc_lst
));
337 write_lock_bh(&ipv6_sk_mc_lock
);
339 write_unlock_bh(&ipv6_sk_mc_lock
);
342 int ip6_mc_source(int add
, int omode
, struct sock
*sk
,
343 struct group_source_req
*pgsr
)
345 struct in6_addr
*source
, *group
;
346 struct ipv6_mc_socklist
*pmc
;
347 struct net_device
*dev
;
348 struct inet6_dev
*idev
;
349 struct ipv6_pinfo
*inet6
= inet6_sk(sk
);
350 struct ip6_sf_socklist
*psl
;
354 if (pgsr
->gsr_group
.ss_family
!= AF_INET6
||
355 pgsr
->gsr_source
.ss_family
!= AF_INET6
)
358 source
= &((struct sockaddr_in6
*)&pgsr
->gsr_source
)->sin6_addr
;
359 group
= &((struct sockaddr_in6
*)&pgsr
->gsr_group
)->sin6_addr
;
361 if (!ipv6_addr_is_multicast(group
))
364 idev
= ip6_mc_find_dev(group
, pgsr
->gsr_interface
);
369 err
= -EADDRNOTAVAIL
;
371 for (pmc
=inet6
->ipv6_mc_list
; pmc
; pmc
=pmc
->next
) {
372 if (pgsr
->gsr_interface
&& pmc
->ifindex
!= pgsr
->gsr_interface
)
374 if (ipv6_addr_equal(&pmc
->addr
, group
))
377 if (!pmc
) /* must have a prior join */
379 /* if a source filter was set, must be the same mode as before */
381 if (pmc
->sfmode
!= omode
)
383 } else if (pmc
->sfmode
!= omode
) {
384 /* allow mode switches for empty-set filters */
385 ip6_mc_add_src(idev
, group
, omode
, 0, NULL
, 0);
386 ip6_mc_del_src(idev
, group
, pmc
->sfmode
, 0, NULL
, 0);
395 for (i
=0; i
<psl
->sl_count
; i
++) {
396 rv
= memcmp(&psl
->sl_addr
[i
], source
,
397 sizeof(struct in6_addr
));
401 if (rv
) /* source not found */
404 /* update the interface filter */
405 ip6_mc_del_src(idev
, group
, omode
, 1, source
, 1);
407 for (j
=i
+1; j
<psl
->sl_count
; j
++)
408 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
413 /* else, add a new source to the filter */
415 if (psl
&& psl
->sl_count
>= sysctl_mld_max_msf
) {
419 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
420 struct ip6_sf_socklist
*newpsl
;
421 int count
= IP6_SFBLOCK
;
424 count
+= psl
->sl_max
;
425 newpsl
= (struct ip6_sf_socklist
*)sock_kmalloc(sk
,
426 IP6_SFLSIZE(count
), GFP_ATOMIC
);
431 newpsl
->sl_max
= count
;
432 newpsl
->sl_count
= count
- IP6_SFBLOCK
;
434 for (i
=0; i
<psl
->sl_count
; i
++)
435 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
436 sock_kfree_s(sk
, psl
, IP6_SFLSIZE(psl
->sl_max
));
438 pmc
->sflist
= psl
= newpsl
;
440 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
441 for (i
=0; i
<psl
->sl_count
; i
++) {
442 rv
= memcmp(&psl
->sl_addr
[i
], source
, sizeof(struct in6_addr
));
446 if (rv
== 0) /* address already there is an error */
448 for (j
=psl
->sl_count
-1; j
>=i
; j
--)
449 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
450 psl
->sl_addr
[i
] = *source
;
453 /* update the interface list */
454 ip6_mc_add_src(idev
, group
, omode
, 1, source
, 1);
456 read_unlock_bh(&idev
->lock
);
462 int ip6_mc_msfilter(struct sock
*sk
, struct group_filter
*gsf
)
464 struct in6_addr
*group
;
465 struct ipv6_mc_socklist
*pmc
;
466 struct net_device
*dev
;
467 struct inet6_dev
*idev
;
468 struct ipv6_pinfo
*inet6
= inet6_sk(sk
);
469 struct ip6_sf_socklist
*newpsl
, *psl
;
472 group
= &((struct sockaddr_in6
*)&gsf
->gf_group
)->sin6_addr
;
474 if (!ipv6_addr_is_multicast(group
))
476 if (gsf
->gf_fmode
!= MCAST_INCLUDE
&&
477 gsf
->gf_fmode
!= MCAST_EXCLUDE
)
480 idev
= ip6_mc_find_dev(group
, gsf
->gf_interface
);
485 err
= -EADDRNOTAVAIL
;
487 for (pmc
=inet6
->ipv6_mc_list
; pmc
; pmc
=pmc
->next
) {
488 if (pmc
->ifindex
!= gsf
->gf_interface
)
490 if (ipv6_addr_equal(&pmc
->addr
, group
))
493 if (!pmc
) /* must have a prior join */
495 if (gsf
->gf_numsrc
) {
496 newpsl
= (struct ip6_sf_socklist
*)sock_kmalloc(sk
,
497 IP6_SFLSIZE(gsf
->gf_numsrc
), GFP_ATOMIC
);
502 newpsl
->sl_max
= newpsl
->sl_count
= gsf
->gf_numsrc
;
503 for (i
=0; i
<newpsl
->sl_count
; ++i
) {
504 struct sockaddr_in6
*psin6
;
506 psin6
= (struct sockaddr_in6
*)&gsf
->gf_slist
[i
];
507 newpsl
->sl_addr
[i
] = psin6
->sin6_addr
;
509 err
= ip6_mc_add_src(idev
, group
, gsf
->gf_fmode
,
510 newpsl
->sl_count
, newpsl
->sl_addr
, 0);
512 sock_kfree_s(sk
, newpsl
, IP6_SFLSIZE(newpsl
->sl_max
));
519 (void) ip6_mc_del_src(idev
, group
, pmc
->sfmode
,
520 psl
->sl_count
, psl
->sl_addr
, 0);
521 sock_kfree_s(sk
, psl
, IP6_SFLSIZE(psl
->sl_max
));
523 (void) ip6_mc_del_src(idev
, group
, pmc
->sfmode
, 0, NULL
, 0);
524 pmc
->sflist
= newpsl
;
525 pmc
->sfmode
= gsf
->gf_fmode
;
527 read_unlock_bh(&idev
->lock
);
533 int ip6_mc_msfget(struct sock
*sk
, struct group_filter
*gsf
,
534 struct group_filter __user
*optval
, int __user
*optlen
)
536 int err
, i
, count
, copycount
;
537 struct in6_addr
*group
;
538 struct ipv6_mc_socklist
*pmc
;
539 struct inet6_dev
*idev
;
540 struct net_device
*dev
;
541 struct ipv6_pinfo
*inet6
= inet6_sk(sk
);
542 struct ip6_sf_socklist
*psl
;
544 group
= &((struct sockaddr_in6
*)&gsf
->gf_group
)->sin6_addr
;
546 if (!ipv6_addr_is_multicast(group
))
549 idev
= ip6_mc_find_dev(group
, gsf
->gf_interface
);
556 err
= -EADDRNOTAVAIL
;
558 for (pmc
=inet6
->ipv6_mc_list
; pmc
; pmc
=pmc
->next
) {
559 if (pmc
->ifindex
!= gsf
->gf_interface
)
561 if (ipv6_addr_equal(group
, &pmc
->addr
))
564 if (!pmc
) /* must have a prior join */
566 gsf
->gf_fmode
= pmc
->sfmode
;
568 count
= psl
? psl
->sl_count
: 0;
569 read_unlock_bh(&idev
->lock
);
573 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
574 gsf
->gf_numsrc
= count
;
575 if (put_user(GROUP_FILTER_SIZE(copycount
), optlen
) ||
576 copy_to_user(optval
, gsf
, GROUP_FILTER_SIZE(0))) {
579 for (i
=0; i
<copycount
; i
++) {
580 struct sockaddr_in6
*psin6
;
581 struct sockaddr_storage ss
;
583 psin6
= (struct sockaddr_in6
*)&ss
;
584 memset(&ss
, 0, sizeof(ss
));
585 psin6
->sin6_family
= AF_INET6
;
586 psin6
->sin6_addr
= psl
->sl_addr
[i
];
587 if (copy_to_user(&optval
->gf_slist
[i
], &ss
, sizeof(ss
)))
592 read_unlock_bh(&idev
->lock
);
598 int inet6_mc_check(struct sock
*sk
, struct in6_addr
*mc_addr
,
599 struct in6_addr
*src_addr
)
601 struct ipv6_pinfo
*np
= inet6_sk(sk
);
602 struct ipv6_mc_socklist
*mc
;
603 struct ip6_sf_socklist
*psl
;
606 read_lock(&ipv6_sk_mc_lock
);
607 for (mc
= np
->ipv6_mc_list
; mc
; mc
= mc
->next
) {
608 if (ipv6_addr_equal(&mc
->addr
, mc_addr
))
612 read_unlock(&ipv6_sk_mc_lock
);
617 rv
= mc
->sfmode
== MCAST_EXCLUDE
;
621 for (i
=0; i
<psl
->sl_count
; i
++) {
622 if (ipv6_addr_equal(&psl
->sl_addr
[i
], src_addr
))
625 if (mc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
627 if (mc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
630 read_unlock(&ipv6_sk_mc_lock
);
635 static void ma_put(struct ifmcaddr6
*mc
)
637 if (atomic_dec_and_test(&mc
->mca_refcnt
)) {
638 in6_dev_put(mc
->idev
);
643 static void igmp6_group_added(struct ifmcaddr6
*mc
)
645 struct net_device
*dev
= mc
->idev
->dev
;
646 char buf
[MAX_ADDR_LEN
];
648 spin_lock_bh(&mc
->mca_lock
);
649 if (!(mc
->mca_flags
&MAF_LOADED
)) {
650 mc
->mca_flags
|= MAF_LOADED
;
651 if (ndisc_mc_map(&mc
->mca_addr
, buf
, dev
, 0) == 0)
652 dev_mc_add(dev
, buf
, dev
->addr_len
, 0);
654 spin_unlock_bh(&mc
->mca_lock
);
656 if (!(dev
->flags
& IFF_UP
) || (mc
->mca_flags
& MAF_NOREPORT
))
659 if (MLD_V1_SEEN(mc
->idev
)) {
660 igmp6_join_group(mc
);
665 mc
->mca_crcount
= mc
->idev
->mc_qrv
;
666 mld_ifc_event(mc
->idev
);
669 static void igmp6_group_dropped(struct ifmcaddr6
*mc
)
671 struct net_device
*dev
= mc
->idev
->dev
;
672 char buf
[MAX_ADDR_LEN
];
674 spin_lock_bh(&mc
->mca_lock
);
675 if (mc
->mca_flags
&MAF_LOADED
) {
676 mc
->mca_flags
&= ~MAF_LOADED
;
677 if (ndisc_mc_map(&mc
->mca_addr
, buf
, dev
, 0) == 0)
678 dev_mc_delete(dev
, buf
, dev
->addr_len
, 0);
681 if (mc
->mca_flags
& MAF_NOREPORT
)
683 spin_unlock_bh(&mc
->mca_lock
);
686 igmp6_leave_group(mc
);
688 spin_lock_bh(&mc
->mca_lock
);
689 if (del_timer(&mc
->mca_timer
))
690 atomic_dec(&mc
->mca_refcnt
);
692 ip6_mc_clear_src(mc
);
693 spin_unlock_bh(&mc
->mca_lock
);
697 * deleted ifmcaddr6 manipulation
699 static void mld_add_delrec(struct inet6_dev
*idev
, struct ifmcaddr6
*im
)
701 struct ifmcaddr6
*pmc
;
703 /* this is an "ifmcaddr6" for convenience; only the fields below
704 * are actually used. In particular, the refcnt and users are not
705 * used for management of the delete list. Using the same structure
706 * for deleted items allows change reports to use common code with
707 * non-deleted or query-response MCA's.
709 pmc
= (struct ifmcaddr6
*)kmalloc(sizeof(*pmc
), GFP_ATOMIC
);
712 memset(pmc
, 0, sizeof(*pmc
));
713 spin_lock_bh(&im
->mca_lock
);
714 spin_lock_init(&pmc
->mca_lock
);
715 pmc
->idev
= im
->idev
;
717 pmc
->mca_addr
= im
->mca_addr
;
718 pmc
->mca_crcount
= idev
->mc_qrv
;
719 pmc
->mca_sfmode
= im
->mca_sfmode
;
720 if (pmc
->mca_sfmode
== MCAST_INCLUDE
) {
721 struct ip6_sf_list
*psf
;
723 pmc
->mca_tomb
= im
->mca_tomb
;
724 pmc
->mca_sources
= im
->mca_sources
;
725 im
->mca_tomb
= im
->mca_sources
= NULL
;
726 for (psf
=pmc
->mca_sources
; psf
; psf
=psf
->sf_next
)
727 psf
->sf_crcount
= pmc
->mca_crcount
;
729 spin_unlock_bh(&im
->mca_lock
);
731 write_lock_bh(&idev
->mc_lock
);
732 pmc
->next
= idev
->mc_tomb
;
734 write_unlock_bh(&idev
->mc_lock
);
737 static void mld_del_delrec(struct inet6_dev
*idev
, struct in6_addr
*pmca
)
739 struct ifmcaddr6
*pmc
, *pmc_prev
;
740 struct ip6_sf_list
*psf
, *psf_next
;
742 write_lock_bh(&idev
->mc_lock
);
744 for (pmc
=idev
->mc_tomb
; pmc
; pmc
=pmc
->next
) {
745 if (ipv6_addr_equal(&pmc
->mca_addr
, pmca
))
751 pmc_prev
->next
= pmc
->next
;
753 idev
->mc_tomb
= pmc
->next
;
755 write_unlock_bh(&idev
->mc_lock
);
757 for (psf
=pmc
->mca_tomb
; psf
; psf
=psf_next
) {
758 psf_next
= psf
->sf_next
;
761 in6_dev_put(pmc
->idev
);
766 static void mld_clear_delrec(struct inet6_dev
*idev
)
768 struct ifmcaddr6
*pmc
, *nextpmc
;
770 write_lock_bh(&idev
->mc_lock
);
772 idev
->mc_tomb
= NULL
;
773 write_unlock_bh(&idev
->mc_lock
);
775 for (; pmc
; pmc
= nextpmc
) {
777 ip6_mc_clear_src(pmc
);
778 in6_dev_put(pmc
->idev
);
782 /* clear dead sources, too */
783 read_lock_bh(&idev
->lock
);
784 for (pmc
=idev
->mc_list
; pmc
; pmc
=pmc
->next
) {
785 struct ip6_sf_list
*psf
, *psf_next
;
787 spin_lock_bh(&pmc
->mca_lock
);
789 pmc
->mca_tomb
= NULL
;
790 spin_unlock_bh(&pmc
->mca_lock
);
791 for (; psf
; psf
=psf_next
) {
792 psf_next
= psf
->sf_next
;
796 read_unlock_bh(&idev
->lock
);
801 * device multicast group inc (add if not found)
803 int ipv6_dev_mc_inc(struct net_device
*dev
, struct in6_addr
*addr
)
805 struct ifmcaddr6
*mc
;
806 struct inet6_dev
*idev
;
808 idev
= in6_dev_get(dev
);
813 write_lock_bh(&idev
->lock
);
815 write_unlock_bh(&idev
->lock
);
820 for (mc
= idev
->mc_list
; mc
; mc
= mc
->next
) {
821 if (ipv6_addr_equal(&mc
->mca_addr
, addr
)) {
823 write_unlock_bh(&idev
->lock
);
824 ip6_mc_add_src(idev
, &mc
->mca_addr
, MCAST_EXCLUDE
, 0,
832 * not found: create a new one.
835 mc
= kmalloc(sizeof(struct ifmcaddr6
), GFP_ATOMIC
);
838 write_unlock_bh(&idev
->lock
);
843 memset(mc
, 0, sizeof(struct ifmcaddr6
));
844 init_timer(&mc
->mca_timer
);
845 mc
->mca_timer
.function
= igmp6_timer_handler
;
846 mc
->mca_timer
.data
= (unsigned long) mc
;
848 ipv6_addr_copy(&mc
->mca_addr
, addr
);
851 /* mca_stamp should be updated upon changes */
852 mc
->mca_cstamp
= mc
->mca_tstamp
= jiffies
;
853 atomic_set(&mc
->mca_refcnt
, 2);
854 spin_lock_init(&mc
->mca_lock
);
856 /* initial mode is (EX, empty) */
857 mc
->mca_sfmode
= MCAST_EXCLUDE
;
858 mc
->mca_sfcount
[MCAST_EXCLUDE
] = 1;
860 if (ipv6_addr_is_ll_all_nodes(&mc
->mca_addr
) ||
861 IPV6_ADDR_MC_SCOPE(&mc
->mca_addr
) < IPV6_ADDR_SCOPE_LINKLOCAL
)
862 mc
->mca_flags
|= MAF_NOREPORT
;
864 mc
->next
= idev
->mc_list
;
866 write_unlock_bh(&idev
->lock
);
868 mld_del_delrec(idev
, &mc
->mca_addr
);
869 igmp6_group_added(mc
);
875 * device multicast group del
877 int __ipv6_dev_mc_dec(struct inet6_dev
*idev
, struct in6_addr
*addr
)
879 struct ifmcaddr6
*ma
, **map
;
881 write_lock_bh(&idev
->lock
);
882 for (map
= &idev
->mc_list
; (ma
=*map
) != NULL
; map
= &ma
->next
) {
883 if (ipv6_addr_equal(&ma
->mca_addr
, addr
)) {
884 if (--ma
->mca_users
== 0) {
886 write_unlock_bh(&idev
->lock
);
888 igmp6_group_dropped(ma
);
893 write_unlock_bh(&idev
->lock
);
897 write_unlock_bh(&idev
->lock
);
902 int ipv6_dev_mc_dec(struct net_device
*dev
, struct in6_addr
*addr
)
904 struct inet6_dev
*idev
= in6_dev_get(dev
);
910 err
= __ipv6_dev_mc_dec(idev
, addr
);
918 * identify MLD packets for MLD filter exceptions
920 int ipv6_is_mld(struct sk_buff
*skb
, int nexthdr
)
922 struct icmp6hdr
*pic
;
924 if (nexthdr
!= IPPROTO_ICMPV6
)
927 if (!pskb_may_pull(skb
, sizeof(struct icmp6hdr
)))
930 pic
= (struct icmp6hdr
*)skb
->h
.raw
;
932 switch (pic
->icmp6_type
) {
933 case ICMPV6_MGM_QUERY
:
934 case ICMPV6_MGM_REPORT
:
935 case ICMPV6_MGM_REDUCTION
:
936 case ICMPV6_MLD2_REPORT
:
945 * check if the interface/address pair is valid
947 int ipv6_chk_mcast_addr(struct net_device
*dev
, struct in6_addr
*group
,
948 struct in6_addr
*src_addr
)
950 struct inet6_dev
*idev
;
951 struct ifmcaddr6
*mc
;
954 idev
= in6_dev_get(dev
);
956 read_lock_bh(&idev
->lock
);
957 for (mc
= idev
->mc_list
; mc
; mc
=mc
->next
) {
958 if (ipv6_addr_equal(&mc
->mca_addr
, group
))
962 if (src_addr
&& !ipv6_addr_any(src_addr
)) {
963 struct ip6_sf_list
*psf
;
965 spin_lock_bh(&mc
->mca_lock
);
966 for (psf
=mc
->mca_sources
;psf
;psf
=psf
->sf_next
) {
967 if (ipv6_addr_equal(&psf
->sf_addr
, src_addr
))
971 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
972 psf
->sf_count
[MCAST_EXCLUDE
] !=
973 mc
->mca_sfcount
[MCAST_EXCLUDE
];
975 rv
= mc
->mca_sfcount
[MCAST_EXCLUDE
] !=0;
976 spin_unlock_bh(&mc
->mca_lock
);
978 rv
= 1; /* don't filter unspecified source */
980 read_unlock_bh(&idev
->lock
);
986 static void mld_gq_start_timer(struct inet6_dev
*idev
)
988 int tv
= net_random() % idev
->mc_maxdelay
;
990 idev
->mc_gq_running
= 1;
991 if (!mod_timer(&idev
->mc_gq_timer
, jiffies
+tv
+2))
995 static void mld_ifc_start_timer(struct inet6_dev
*idev
, int delay
)
997 int tv
= net_random() % delay
;
999 if (!mod_timer(&idev
->mc_ifc_timer
, jiffies
+tv
+2))
1004 * IGMP handling (alias multicast ICMPv6 messages)
1007 static void igmp6_group_queried(struct ifmcaddr6
*ma
, unsigned long resptime
)
1009 unsigned long delay
= resptime
;
1011 /* Do not start timer for these addresses */
1012 if (ipv6_addr_is_ll_all_nodes(&ma
->mca_addr
) ||
1013 IPV6_ADDR_MC_SCOPE(&ma
->mca_addr
) < IPV6_ADDR_SCOPE_LINKLOCAL
)
1016 if (del_timer(&ma
->mca_timer
)) {
1017 atomic_dec(&ma
->mca_refcnt
);
1018 delay
= ma
->mca_timer
.expires
- jiffies
;
1021 if (delay
>= resptime
) {
1023 delay
= net_random() % resptime
;
1027 ma
->mca_timer
.expires
= jiffies
+ delay
;
1028 if (!mod_timer(&ma
->mca_timer
, jiffies
+ delay
))
1029 atomic_inc(&ma
->mca_refcnt
);
1030 ma
->mca_flags
|= MAF_TIMER_RUNNING
;
1033 static void mld_marksources(struct ifmcaddr6
*pmc
, int nsrcs
,
1034 struct in6_addr
*srcs
)
1036 struct ip6_sf_list
*psf
;
1040 for (psf
=pmc
->mca_sources
; psf
; psf
=psf
->sf_next
) {
1041 if (scount
== nsrcs
)
1043 for (i
=0; i
<nsrcs
; i
++)
1044 if (ipv6_addr_equal(&srcs
[i
], &psf
->sf_addr
)) {
1052 int igmp6_event_query(struct sk_buff
*skb
)
1054 struct mld2_query
*mlh2
= (struct mld2_query
*) skb
->h
.raw
;
1055 struct ifmcaddr6
*ma
;
1056 struct in6_addr
*group
;
1057 unsigned long max_delay
;
1058 struct inet6_dev
*idev
;
1059 struct icmp6hdr
*hdr
;
1064 if (!pskb_may_pull(skb
, sizeof(struct in6_addr
)))
1067 /* compute payload length excluding extension headers */
1068 len
= ntohs(skb
->nh
.ipv6h
->payload_len
) + sizeof(struct ipv6hdr
);
1069 len
-= (char *)skb
->h
.raw
- (char *)skb
->nh
.ipv6h
;
1071 /* Drop queries with not link local source */
1072 if (!(ipv6_addr_type(&skb
->nh
.ipv6h
->saddr
)&IPV6_ADDR_LINKLOCAL
))
1075 idev
= in6_dev_get(skb
->dev
);
1080 hdr
= (struct icmp6hdr
*) skb
->h
.raw
;
1081 group
= (struct in6_addr
*) (hdr
+ 1);
1082 group_type
= ipv6_addr_type(group
);
1084 if (group_type
!= IPV6_ADDR_ANY
&&
1085 !(group_type
&IPV6_ADDR_MULTICAST
)) {
1092 /* MLDv1 router present */
1094 /* Translate milliseconds to jiffies */
1095 max_delay
= (ntohs(hdr
->icmp6_maxdelay
)*HZ
)/1000;
1097 switchback
= (idev
->mc_qrv
+ 1) * max_delay
;
1098 idev
->mc_v1_seen
= jiffies
+ switchback
;
1100 /* cancel the interface change timer */
1101 idev
->mc_ifc_count
= 0;
1102 if (del_timer(&idev
->mc_ifc_timer
))
1103 __in6_dev_put(idev
);
1104 /* clear deleted report items */
1105 mld_clear_delrec(idev
);
1106 } else if (len
>= 28) {
1107 max_delay
= (MLDV2_MRC(ntohs(mlh2
->mrc
))*HZ
)/1000;
1110 idev
->mc_maxdelay
= max_delay
;
1112 idev
->mc_qrv
= mlh2
->qrv
;
1113 if (group_type
== IPV6_ADDR_ANY
) { /* general query */
1116 return -EINVAL
; /* no sources allowed */
1118 mld_gq_start_timer(idev
);
1122 /* mark sources to include, if group & source-specific */
1123 mark
= mlh2
->nsrcs
!= 0;
1129 read_lock_bh(&idev
->lock
);
1130 if (group_type
== IPV6_ADDR_ANY
) {
1131 for (ma
= idev
->mc_list
; ma
; ma
=ma
->next
) {
1132 spin_lock_bh(&ma
->mca_lock
);
1133 igmp6_group_queried(ma
, max_delay
);
1134 spin_unlock_bh(&ma
->mca_lock
);
1137 for (ma
= idev
->mc_list
; ma
; ma
=ma
->next
) {
1138 if (group_type
!= IPV6_ADDR_ANY
&&
1139 !ipv6_addr_equal(group
, &ma
->mca_addr
))
1141 spin_lock_bh(&ma
->mca_lock
);
1142 if (ma
->mca_flags
& MAF_TIMER_RUNNING
) {
1143 /* gsquery <- gsquery && mark */
1145 ma
->mca_flags
&= ~MAF_GSQUERY
;
1147 /* gsquery <- mark */
1149 ma
->mca_flags
|= MAF_GSQUERY
;
1151 ma
->mca_flags
&= ~MAF_GSQUERY
;
1153 if (ma
->mca_flags
& MAF_GSQUERY
)
1154 mld_marksources(ma
, ntohs(mlh2
->nsrcs
),
1156 igmp6_group_queried(ma
, max_delay
);
1157 spin_unlock_bh(&ma
->mca_lock
);
1158 if (group_type
!= IPV6_ADDR_ANY
)
1162 read_unlock_bh(&idev
->lock
);
1169 int igmp6_event_report(struct sk_buff
*skb
)
1171 struct ifmcaddr6
*ma
;
1172 struct in6_addr
*addrp
;
1173 struct inet6_dev
*idev
;
1174 struct icmp6hdr
*hdr
;
1177 /* Our own report looped back. Ignore it. */
1178 if (skb
->pkt_type
== PACKET_LOOPBACK
)
1181 if (!pskb_may_pull(skb
, sizeof(struct in6_addr
)))
1184 hdr
= (struct icmp6hdr
*) skb
->h
.raw
;
1186 /* Drop reports with not link local source */
1187 addr_type
= ipv6_addr_type(&skb
->nh
.ipv6h
->saddr
);
1188 if (addr_type
!= IPV6_ADDR_ANY
&&
1189 !(addr_type
&IPV6_ADDR_LINKLOCAL
))
1192 addrp
= (struct in6_addr
*) (hdr
+ 1);
1194 idev
= in6_dev_get(skb
->dev
);
1199 * Cancel the timer for this group
1202 read_lock_bh(&idev
->lock
);
1203 for (ma
= idev
->mc_list
; ma
; ma
=ma
->next
) {
1204 if (ipv6_addr_equal(&ma
->mca_addr
, addrp
)) {
1205 spin_lock(&ma
->mca_lock
);
1206 if (del_timer(&ma
->mca_timer
))
1207 atomic_dec(&ma
->mca_refcnt
);
1208 ma
->mca_flags
&= ~(MAF_LAST_REPORTER
|MAF_TIMER_RUNNING
);
1209 spin_unlock(&ma
->mca_lock
);
1213 read_unlock_bh(&idev
->lock
);
1218 static int is_in(struct ifmcaddr6
*pmc
, struct ip6_sf_list
*psf
, int type
,
1219 int gdeleted
, int sdeleted
)
1222 case MLD2_MODE_IS_INCLUDE
:
1223 case MLD2_MODE_IS_EXCLUDE
:
1224 if (gdeleted
|| sdeleted
)
1226 return !((pmc
->mca_flags
& MAF_GSQUERY
) && !psf
->sf_gsresp
);
1227 case MLD2_CHANGE_TO_INCLUDE
:
1228 if (gdeleted
|| sdeleted
)
1230 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
1231 case MLD2_CHANGE_TO_EXCLUDE
:
1232 if (gdeleted
|| sdeleted
)
1234 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
] == 0 ||
1235 psf
->sf_count
[MCAST_INCLUDE
])
1237 return pmc
->mca_sfcount
[MCAST_EXCLUDE
] ==
1238 psf
->sf_count
[MCAST_EXCLUDE
];
1239 case MLD2_ALLOW_NEW_SOURCES
:
1240 if (gdeleted
|| !psf
->sf_crcount
)
1242 return (pmc
->mca_sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
1243 case MLD2_BLOCK_OLD_SOURCES
:
1244 if (pmc
->mca_sfmode
== MCAST_INCLUDE
)
1245 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
1246 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
1252 mld_scount(struct ifmcaddr6
*pmc
, int type
, int gdeleted
, int sdeleted
)
1254 struct ip6_sf_list
*psf
;
1257 for (psf
=pmc
->mca_sources
; psf
; psf
=psf
->sf_next
) {
1258 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
1265 static struct sk_buff
*mld_newpack(struct net_device
*dev
, int size
)
1267 struct sock
*sk
= igmp6_socket
->sk
;
1268 struct sk_buff
*skb
;
1269 struct mld2_report
*pmr
;
1270 struct in6_addr addr_buf
;
1272 u8 ra
[8] = { IPPROTO_ICMPV6
, 0,
1273 IPV6_TLV_ROUTERALERT
, 2, 0, 0,
1276 /* we assume size > sizeof(ra) here */
1277 skb
= sock_alloc_send_skb(sk
, size
+ LL_RESERVED_SPACE(dev
), 1, &err
);
1282 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
1283 if (dev
->hard_header
) {
1284 unsigned char ha
[MAX_ADDR_LEN
];
1286 ndisc_mc_map(&mld2_all_mcr
, ha
, dev
, 1);
1287 if (dev
->hard_header(skb
, dev
, ETH_P_IPV6
,ha
,NULL
,size
) < 0) {
1293 if (ipv6_get_lladdr(dev
, &addr_buf
)) {
1294 /* <draft-ietf-magma-mld-source-05.txt>:
1295 * use unspecified address as the source address
1296 * when a valid link-local address is not available.
1298 memset(&addr_buf
, 0, sizeof(addr_buf
));
1301 ip6_nd_hdr(sk
, skb
, dev
, &addr_buf
, &mld2_all_mcr
, NEXTHDR_HOP
, 0);
1303 memcpy(skb_put(skb
, sizeof(ra
)), ra
, sizeof(ra
));
1305 pmr
=(struct mld2_report
*)skb_put(skb
, sizeof(*pmr
));
1306 skb
->h
.raw
= (unsigned char *)pmr
;
1307 pmr
->type
= ICMPV6_MLD2_REPORT
;
1315 static void mld_sendpack(struct sk_buff
*skb
)
1317 struct ipv6hdr
*pip6
= skb
->nh
.ipv6h
;
1318 struct mld2_report
*pmr
= (struct mld2_report
*)skb
->h
.raw
;
1319 int payload_len
, mldlen
;
1320 struct inet6_dev
*idev
= in6_dev_get(skb
->dev
);
1323 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS
);
1324 payload_len
= skb
->tail
- (unsigned char *)skb
->nh
.ipv6h
-
1325 sizeof(struct ipv6hdr
);
1326 mldlen
= skb
->tail
- skb
->h
.raw
;
1327 pip6
->payload_len
= htons(payload_len
);
1329 pmr
->csum
= csum_ipv6_magic(&pip6
->saddr
, &pip6
->daddr
, mldlen
,
1330 IPPROTO_ICMPV6
, csum_partial(skb
->h
.raw
, mldlen
, 0));
1331 err
= NF_HOOK(PF_INET6
, NF_IP6_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
1334 ICMP6_INC_STATS(idev
,ICMP6_MIB_OUTMSGS
);
1335 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS
);
1337 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS
);
1339 if (likely(idev
!= NULL
))
1343 static int grec_size(struct ifmcaddr6
*pmc
, int type
, int gdel
, int sdel
)
1345 return sizeof(struct mld2_grec
) + 4*mld_scount(pmc
,type
,gdel
,sdel
);
1348 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ifmcaddr6
*pmc
,
1349 int type
, struct mld2_grec
**ppgr
)
1351 struct net_device
*dev
= pmc
->idev
->dev
;
1352 struct mld2_report
*pmr
;
1353 struct mld2_grec
*pgr
;
1356 skb
= mld_newpack(dev
, dev
->mtu
);
1359 pgr
= (struct mld2_grec
*)skb_put(skb
, sizeof(struct mld2_grec
));
1360 pgr
->grec_type
= type
;
1361 pgr
->grec_auxwords
= 0;
1362 pgr
->grec_nsrcs
= 0;
1363 pgr
->grec_mca
= pmc
->mca_addr
; /* structure copy */
1364 pmr
= (struct mld2_report
*)skb
->h
.raw
;
1365 pmr
->ngrec
= htons(ntohs(pmr
->ngrec
)+1);
1370 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1371 skb_tailroom(skb)) : 0)
1373 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ifmcaddr6
*pmc
,
1374 int type
, int gdeleted
, int sdeleted
)
1376 struct net_device
*dev
= pmc
->idev
->dev
;
1377 struct mld2_report
*pmr
;
1378 struct mld2_grec
*pgr
= NULL
;
1379 struct ip6_sf_list
*psf
, *psf_next
, *psf_prev
, **psf_list
;
1380 int scount
, first
, isquery
, truncate
;
1382 if (pmc
->mca_flags
& MAF_NOREPORT
)
1385 isquery
= type
== MLD2_MODE_IS_INCLUDE
||
1386 type
== MLD2_MODE_IS_EXCLUDE
;
1387 truncate
= type
== MLD2_MODE_IS_EXCLUDE
||
1388 type
== MLD2_CHANGE_TO_EXCLUDE
;
1390 psf_list
= sdeleted
? &pmc
->mca_tomb
: &pmc
->mca_sources
;
1393 if (type
== MLD2_ALLOW_NEW_SOURCES
||
1394 type
== MLD2_BLOCK_OLD_SOURCES
)
1396 if (pmc
->mca_crcount
|| isquery
) {
1397 /* make sure we have room for group header and at
1400 if (skb
&& AVAILABLE(skb
) < sizeof(struct mld2_grec
)+
1401 sizeof(struct in6_addr
)) {
1403 skb
= NULL
; /* add_grhead will get a new one */
1405 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
1409 pmr
= skb
? (struct mld2_report
*)skb
->h
.raw
: NULL
;
1411 /* EX and TO_EX get a fresh packet, if needed */
1413 if (pmr
&& pmr
->ngrec
&&
1414 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
1417 skb
= mld_newpack(dev
, dev
->mtu
);
1423 for (psf
=*psf_list
; psf
; psf
=psf_next
) {
1424 struct in6_addr
*psrc
;
1426 psf_next
= psf
->sf_next
;
1428 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
)) {
1433 /* clear marks on query responses */
1437 if (AVAILABLE(skb
) < sizeof(*psrc
) +
1438 first
*sizeof(struct mld2_grec
)) {
1439 if (truncate
&& !first
)
1440 break; /* truncate these */
1442 pgr
->grec_nsrcs
= htons(scount
);
1445 skb
= mld_newpack(dev
, dev
->mtu
);
1450 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
1453 psrc
= (struct in6_addr
*)skb_put(skb
, sizeof(*psrc
));
1454 *psrc
= psf
->sf_addr
;
1456 if ((type
== MLD2_ALLOW_NEW_SOURCES
||
1457 type
== MLD2_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
) {
1459 if ((sdeleted
|| gdeleted
) && psf
->sf_crcount
== 0) {
1461 psf_prev
->sf_next
= psf
->sf_next
;
1463 *psf_list
= psf
->sf_next
;
1471 pgr
->grec_nsrcs
= htons(scount
);
1474 pmc
->mca_flags
&= ~MAF_GSQUERY
; /* clear query state */
1478 static void mld_send_report(struct inet6_dev
*idev
, struct ifmcaddr6
*pmc
)
1480 struct sk_buff
*skb
= NULL
;
1484 read_lock_bh(&idev
->lock
);
1485 for (pmc
=idev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1486 if (pmc
->mca_flags
& MAF_NOREPORT
)
1488 spin_lock_bh(&pmc
->mca_lock
);
1489 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
])
1490 type
= MLD2_MODE_IS_EXCLUDE
;
1492 type
= MLD2_MODE_IS_INCLUDE
;
1493 skb
= add_grec(skb
, pmc
, type
, 0, 0);
1494 spin_unlock_bh(&pmc
->mca_lock
);
1496 read_unlock_bh(&idev
->lock
);
1498 spin_lock_bh(&pmc
->mca_lock
);
1499 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
])
1500 type
= MLD2_MODE_IS_EXCLUDE
;
1502 type
= MLD2_MODE_IS_INCLUDE
;
1503 skb
= add_grec(skb
, pmc
, type
, 0, 0);
1504 spin_unlock_bh(&pmc
->mca_lock
);
1511 * remove zero-count source records from a source filter list
1513 static void mld_clear_zeros(struct ip6_sf_list
**ppsf
)
1515 struct ip6_sf_list
*psf_prev
, *psf_next
, *psf
;
1518 for (psf
=*ppsf
; psf
; psf
= psf_next
) {
1519 psf_next
= psf
->sf_next
;
1520 if (psf
->sf_crcount
== 0) {
1522 psf_prev
->sf_next
= psf
->sf_next
;
1524 *ppsf
= psf
->sf_next
;
1531 static void mld_send_cr(struct inet6_dev
*idev
)
1533 struct ifmcaddr6
*pmc
, *pmc_prev
, *pmc_next
;
1534 struct sk_buff
*skb
= NULL
;
1537 read_lock_bh(&idev
->lock
);
1538 write_lock_bh(&idev
->mc_lock
);
1542 for (pmc
=idev
->mc_tomb
; pmc
; pmc
=pmc_next
) {
1543 pmc_next
= pmc
->next
;
1544 if (pmc
->mca_sfmode
== MCAST_INCLUDE
) {
1545 type
= MLD2_BLOCK_OLD_SOURCES
;
1546 dtype
= MLD2_BLOCK_OLD_SOURCES
;
1547 skb
= add_grec(skb
, pmc
, type
, 1, 0);
1548 skb
= add_grec(skb
, pmc
, dtype
, 1, 1);
1550 if (pmc
->mca_crcount
) {
1552 if (pmc
->mca_sfmode
== MCAST_EXCLUDE
) {
1553 type
= MLD2_CHANGE_TO_INCLUDE
;
1554 skb
= add_grec(skb
, pmc
, type
, 1, 0);
1556 if (pmc
->mca_crcount
== 0) {
1557 mld_clear_zeros(&pmc
->mca_tomb
);
1558 mld_clear_zeros(&pmc
->mca_sources
);
1561 if (pmc
->mca_crcount
== 0 && !pmc
->mca_tomb
&&
1562 !pmc
->mca_sources
) {
1564 pmc_prev
->next
= pmc_next
;
1566 idev
->mc_tomb
= pmc_next
;
1567 in6_dev_put(pmc
->idev
);
1572 write_unlock_bh(&idev
->mc_lock
);
1575 for (pmc
=idev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1576 spin_lock_bh(&pmc
->mca_lock
);
1577 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
]) {
1578 type
= MLD2_BLOCK_OLD_SOURCES
;
1579 dtype
= MLD2_ALLOW_NEW_SOURCES
;
1581 type
= MLD2_ALLOW_NEW_SOURCES
;
1582 dtype
= MLD2_BLOCK_OLD_SOURCES
;
1584 skb
= add_grec(skb
, pmc
, type
, 0, 0);
1585 skb
= add_grec(skb
, pmc
, dtype
, 0, 1); /* deleted sources */
1587 /* filter mode changes */
1588 if (pmc
->mca_crcount
) {
1590 if (pmc
->mca_sfmode
== MCAST_EXCLUDE
)
1591 type
= MLD2_CHANGE_TO_EXCLUDE
;
1593 type
= MLD2_CHANGE_TO_INCLUDE
;
1594 skb
= add_grec(skb
, pmc
, type
, 0, 0);
1596 spin_unlock_bh(&pmc
->mca_lock
);
1598 read_unlock_bh(&idev
->lock
);
1601 (void) mld_sendpack(skb
);
1604 static void igmp6_send(struct in6_addr
*addr
, struct net_device
*dev
, int type
)
1606 struct sock
*sk
= igmp6_socket
->sk
;
1607 struct inet6_dev
*idev
;
1608 struct sk_buff
*skb
;
1609 struct icmp6hdr
*hdr
;
1610 struct in6_addr
*snd_addr
;
1611 struct in6_addr
*addrp
;
1612 struct in6_addr addr_buf
;
1613 struct in6_addr all_routers
;
1614 int err
, len
, payload_len
, full_len
;
1615 u8 ra
[8] = { IPPROTO_ICMPV6
, 0,
1616 IPV6_TLV_ROUTERALERT
, 2, 0, 0,
1619 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS
);
1621 if (type
== ICMPV6_MGM_REDUCTION
) {
1622 snd_addr
= &all_routers
;
1623 ipv6_addr_all_routers(&all_routers
);
1626 len
= sizeof(struct icmp6hdr
) + sizeof(struct in6_addr
);
1627 payload_len
= len
+ sizeof(ra
);
1628 full_len
= sizeof(struct ipv6hdr
) + payload_len
;
1630 skb
= sock_alloc_send_skb(sk
, LL_RESERVED_SPACE(dev
) + full_len
, 1, &err
);
1633 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS
);
1637 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
1638 if (dev
->hard_header
) {
1639 unsigned char ha
[MAX_ADDR_LEN
];
1640 ndisc_mc_map(snd_addr
, ha
, dev
, 1);
1641 if (dev
->hard_header(skb
, dev
, ETH_P_IPV6
, ha
, NULL
, full_len
) < 0)
1645 if (ipv6_get_lladdr(dev
, &addr_buf
)) {
1646 /* <draft-ietf-magma-mld-source-05.txt>:
1647 * use unspecified address as the source address
1648 * when a valid link-local address is not available.
1650 memset(&addr_buf
, 0, sizeof(addr_buf
));
1653 ip6_nd_hdr(sk
, skb
, dev
, &addr_buf
, snd_addr
, NEXTHDR_HOP
, payload_len
);
1655 memcpy(skb_put(skb
, sizeof(ra
)), ra
, sizeof(ra
));
1657 hdr
= (struct icmp6hdr
*) skb_put(skb
, sizeof(struct icmp6hdr
));
1658 memset(hdr
, 0, sizeof(struct icmp6hdr
));
1659 hdr
->icmp6_type
= type
;
1661 addrp
= (struct in6_addr
*) skb_put(skb
, sizeof(struct in6_addr
));
1662 ipv6_addr_copy(addrp
, addr
);
1664 hdr
->icmp6_cksum
= csum_ipv6_magic(&addr_buf
, snd_addr
, len
,
1666 csum_partial((__u8
*) hdr
, len
, 0));
1668 idev
= in6_dev_get(skb
->dev
);
1670 err
= NF_HOOK(PF_INET6
, NF_IP6_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
1673 if (type
== ICMPV6_MGM_REDUCTION
)
1674 ICMP6_INC_STATS(idev
, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS
);
1676 ICMP6_INC_STATS(idev
, ICMP6_MIB_OUTGROUPMEMBRESPONSES
);
1677 ICMP6_INC_STATS(idev
, ICMP6_MIB_OUTMSGS
);
1678 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS
);
1680 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS
);
1682 if (likely(idev
!= NULL
))
1687 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS
);
1691 static int ip6_mc_del1_src(struct ifmcaddr6
*pmc
, int sfmode
,
1692 struct in6_addr
*psfsrc
)
1694 struct ip6_sf_list
*psf
, *psf_prev
;
1698 for (psf
=pmc
->mca_sources
; psf
; psf
=psf
->sf_next
) {
1699 if (ipv6_addr_equal(&psf
->sf_addr
, psfsrc
))
1703 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1704 /* source filter not found, or count wrong => bug */
1707 psf
->sf_count
[sfmode
]--;
1708 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1709 struct inet6_dev
*idev
= pmc
->idev
;
1711 /* no more filters for this source */
1713 psf_prev
->sf_next
= psf
->sf_next
;
1715 pmc
->mca_sources
= psf
->sf_next
;
1716 if (psf
->sf_oldin
&& !(pmc
->mca_flags
& MAF_NOREPORT
) &&
1717 !MLD_V1_SEEN(idev
)) {
1718 psf
->sf_crcount
= idev
->mc_qrv
;
1719 psf
->sf_next
= pmc
->mca_tomb
;
1720 pmc
->mca_tomb
= psf
;
1728 static int ip6_mc_del_src(struct inet6_dev
*idev
, struct in6_addr
*pmca
,
1729 int sfmode
, int sfcount
, struct in6_addr
*psfsrc
,
1732 struct ifmcaddr6
*pmc
;
1738 read_lock_bh(&idev
->lock
);
1739 for (pmc
=idev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1740 if (ipv6_addr_equal(pmca
, &pmc
->mca_addr
))
1744 /* MCA not found?? bug */
1745 read_unlock_bh(&idev
->lock
);
1748 spin_lock_bh(&pmc
->mca_lock
);
1751 if (!pmc
->mca_sfcount
[sfmode
]) {
1752 spin_unlock_bh(&pmc
->mca_lock
);
1753 read_unlock_bh(&idev
->lock
);
1756 pmc
->mca_sfcount
[sfmode
]--;
1759 for (i
=0; i
<sfcount
; i
++) {
1760 int rv
= ip6_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1762 changerec
|= rv
> 0;
1766 if (pmc
->mca_sfmode
== MCAST_EXCLUDE
&&
1767 pmc
->mca_sfcount
[MCAST_EXCLUDE
] == 0 &&
1768 pmc
->mca_sfcount
[MCAST_INCLUDE
]) {
1769 struct ip6_sf_list
*psf
;
1771 /* filter mode change */
1772 pmc
->mca_sfmode
= MCAST_INCLUDE
;
1773 pmc
->mca_crcount
= idev
->mc_qrv
;
1774 idev
->mc_ifc_count
= pmc
->mca_crcount
;
1775 for (psf
=pmc
->mca_sources
; psf
; psf
= psf
->sf_next
)
1776 psf
->sf_crcount
= 0;
1777 mld_ifc_event(pmc
->idev
);
1778 } else if (sf_setstate(pmc
) || changerec
)
1779 mld_ifc_event(pmc
->idev
);
1780 spin_unlock_bh(&pmc
->mca_lock
);
1781 read_unlock_bh(&idev
->lock
);
1786 * Add multicast single-source filter to the interface list
1788 static int ip6_mc_add1_src(struct ifmcaddr6
*pmc
, int sfmode
,
1789 struct in6_addr
*psfsrc
, int delta
)
1791 struct ip6_sf_list
*psf
, *psf_prev
;
1794 for (psf
=pmc
->mca_sources
; psf
; psf
=psf
->sf_next
) {
1795 if (ipv6_addr_equal(&psf
->sf_addr
, psfsrc
))
1800 psf
= (struct ip6_sf_list
*)kmalloc(sizeof(*psf
), GFP_ATOMIC
);
1803 memset(psf
, 0, sizeof(*psf
));
1804 psf
->sf_addr
= *psfsrc
;
1806 psf_prev
->sf_next
= psf
;
1808 pmc
->mca_sources
= psf
;
1810 psf
->sf_count
[sfmode
]++;
1814 static void sf_markstate(struct ifmcaddr6
*pmc
)
1816 struct ip6_sf_list
*psf
;
1817 int mca_xcount
= pmc
->mca_sfcount
[MCAST_EXCLUDE
];
1819 for (psf
=pmc
->mca_sources
; psf
; psf
=psf
->sf_next
)
1820 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
]) {
1821 psf
->sf_oldin
= mca_xcount
==
1822 psf
->sf_count
[MCAST_EXCLUDE
] &&
1823 !psf
->sf_count
[MCAST_INCLUDE
];
1825 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1828 static int sf_setstate(struct ifmcaddr6
*pmc
)
1830 struct ip6_sf_list
*psf
;
1831 int mca_xcount
= pmc
->mca_sfcount
[MCAST_EXCLUDE
];
1832 int qrv
= pmc
->idev
->mc_qrv
;
1836 for (psf
=pmc
->mca_sources
; psf
; psf
=psf
->sf_next
) {
1837 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
]) {
1838 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
1839 !psf
->sf_count
[MCAST_INCLUDE
];
1841 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1842 if (new_in
!= psf
->sf_oldin
) {
1843 psf
->sf_crcount
= qrv
;
1851 * Add multicast source filter list to the interface list
1853 static int ip6_mc_add_src(struct inet6_dev
*idev
, struct in6_addr
*pmca
,
1854 int sfmode
, int sfcount
, struct in6_addr
*psfsrc
,
1857 struct ifmcaddr6
*pmc
;
1863 read_lock_bh(&idev
->lock
);
1864 for (pmc
=idev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1865 if (ipv6_addr_equal(pmca
, &pmc
->mca_addr
))
1869 /* MCA not found?? bug */
1870 read_unlock_bh(&idev
->lock
);
1873 spin_lock_bh(&pmc
->mca_lock
);
1876 isexclude
= pmc
->mca_sfmode
== MCAST_EXCLUDE
;
1878 pmc
->mca_sfcount
[sfmode
]++;
1880 for (i
=0; i
<sfcount
; i
++) {
1881 err
= ip6_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
], delta
);
1889 pmc
->mca_sfcount
[sfmode
]--;
1891 (void) ip6_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1892 } else if (isexclude
!= (pmc
->mca_sfcount
[MCAST_EXCLUDE
] != 0)) {
1893 struct inet6_dev
*idev
= pmc
->idev
;
1894 struct ip6_sf_list
*psf
;
1896 /* filter mode change */
1897 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
])
1898 pmc
->mca_sfmode
= MCAST_EXCLUDE
;
1899 else if (pmc
->mca_sfcount
[MCAST_INCLUDE
])
1900 pmc
->mca_sfmode
= MCAST_INCLUDE
;
1901 /* else no filters; keep old mode for reports */
1903 pmc
->mca_crcount
= idev
->mc_qrv
;
1904 idev
->mc_ifc_count
= pmc
->mca_crcount
;
1905 for (psf
=pmc
->mca_sources
; psf
; psf
= psf
->sf_next
)
1906 psf
->sf_crcount
= 0;
1907 mld_ifc_event(idev
);
1908 } else if (sf_setstate(pmc
))
1909 mld_ifc_event(idev
);
1910 spin_unlock_bh(&pmc
->mca_lock
);
1911 read_unlock_bh(&idev
->lock
);
1915 static void ip6_mc_clear_src(struct ifmcaddr6
*pmc
)
1917 struct ip6_sf_list
*psf
, *nextpsf
;
1919 for (psf
=pmc
->mca_tomb
; psf
; psf
=nextpsf
) {
1920 nextpsf
= psf
->sf_next
;
1923 pmc
->mca_tomb
= NULL
;
1924 for (psf
=pmc
->mca_sources
; psf
; psf
=nextpsf
) {
1925 nextpsf
= psf
->sf_next
;
1928 pmc
->mca_sources
= NULL
;
1929 pmc
->mca_sfmode
= MCAST_EXCLUDE
;
1930 pmc
->mca_sfcount
[MCAST_EXCLUDE
] = 0;
1931 pmc
->mca_sfcount
[MCAST_EXCLUDE
] = 1;
1935 static void igmp6_join_group(struct ifmcaddr6
*ma
)
1937 unsigned long delay
;
1939 if (ma
->mca_flags
& MAF_NOREPORT
)
1942 igmp6_send(&ma
->mca_addr
, ma
->idev
->dev
, ICMPV6_MGM_REPORT
);
1944 delay
= net_random() % IGMP6_UNSOLICITED_IVAL
;
1946 spin_lock_bh(&ma
->mca_lock
);
1947 if (del_timer(&ma
->mca_timer
)) {
1948 atomic_dec(&ma
->mca_refcnt
);
1949 delay
= ma
->mca_timer
.expires
- jiffies
;
1952 if (!mod_timer(&ma
->mca_timer
, jiffies
+ delay
))
1953 atomic_inc(&ma
->mca_refcnt
);
1954 ma
->mca_flags
|= MAF_TIMER_RUNNING
| MAF_LAST_REPORTER
;
1955 spin_unlock_bh(&ma
->mca_lock
);
1958 static int ip6_mc_leave_src(struct sock
*sk
, struct ipv6_mc_socklist
*iml
,
1959 struct inet6_dev
*idev
)
1963 if (iml
->sflist
== 0) {
1964 /* any-source empty exclude case */
1965 return ip6_mc_del_src(idev
, &iml
->addr
, iml
->sfmode
, 0, NULL
, 0);
1967 err
= ip6_mc_del_src(idev
, &iml
->addr
, iml
->sfmode
,
1968 iml
->sflist
->sl_count
, iml
->sflist
->sl_addr
, 0);
1969 sock_kfree_s(sk
, iml
->sflist
, IP6_SFLSIZE(iml
->sflist
->sl_max
));
1974 static void igmp6_leave_group(struct ifmcaddr6
*ma
)
1976 if (MLD_V1_SEEN(ma
->idev
)) {
1977 if (ma
->mca_flags
& MAF_LAST_REPORTER
)
1978 igmp6_send(&ma
->mca_addr
, ma
->idev
->dev
,
1979 ICMPV6_MGM_REDUCTION
);
1981 mld_add_delrec(ma
->idev
, ma
);
1982 mld_ifc_event(ma
->idev
);
1986 static void mld_gq_timer_expire(unsigned long data
)
1988 struct inet6_dev
*idev
= (struct inet6_dev
*)data
;
1990 idev
->mc_gq_running
= 0;
1991 mld_send_report(idev
, NULL
);
1992 __in6_dev_put(idev
);
1995 static void mld_ifc_timer_expire(unsigned long data
)
1997 struct inet6_dev
*idev
= (struct inet6_dev
*)data
;
2000 if (idev
->mc_ifc_count
) {
2001 idev
->mc_ifc_count
--;
2002 if (idev
->mc_ifc_count
)
2003 mld_ifc_start_timer(idev
, idev
->mc_maxdelay
);
2005 __in6_dev_put(idev
);
2008 static void mld_ifc_event(struct inet6_dev
*idev
)
2010 if (MLD_V1_SEEN(idev
))
2012 idev
->mc_ifc_count
= idev
->mc_qrv
;
2013 mld_ifc_start_timer(idev
, 1);
2017 static void igmp6_timer_handler(unsigned long data
)
2019 struct ifmcaddr6
*ma
= (struct ifmcaddr6
*) data
;
2021 if (MLD_V1_SEEN(ma
->idev
))
2022 igmp6_send(&ma
->mca_addr
, ma
->idev
->dev
, ICMPV6_MGM_REPORT
);
2024 mld_send_report(ma
->idev
, ma
);
2026 spin_lock(&ma
->mca_lock
);
2027 ma
->mca_flags
|= MAF_LAST_REPORTER
;
2028 ma
->mca_flags
&= ~MAF_TIMER_RUNNING
;
2029 spin_unlock(&ma
->mca_lock
);
2033 /* Device going down */
2035 void ipv6_mc_down(struct inet6_dev
*idev
)
2037 struct ifmcaddr6
*i
;
2039 /* Withdraw multicast list */
2041 read_lock_bh(&idev
->lock
);
2042 idev
->mc_ifc_count
= 0;
2043 if (del_timer(&idev
->mc_ifc_timer
))
2044 __in6_dev_put(idev
);
2045 idev
->mc_gq_running
= 0;
2046 if (del_timer(&idev
->mc_gq_timer
))
2047 __in6_dev_put(idev
);
2049 for (i
= idev
->mc_list
; i
; i
=i
->next
)
2050 igmp6_group_dropped(i
);
2051 read_unlock_bh(&idev
->lock
);
2053 mld_clear_delrec(idev
);
2057 /* Device going up */
2059 void ipv6_mc_up(struct inet6_dev
*idev
)
2061 struct ifmcaddr6
*i
;
2063 /* Install multicast list, except for all-nodes (already installed) */
2065 read_lock_bh(&idev
->lock
);
2066 for (i
= idev
->mc_list
; i
; i
=i
->next
)
2067 igmp6_group_added(i
);
2068 read_unlock_bh(&idev
->lock
);
2071 /* IPv6 device initialization. */
2073 void ipv6_mc_init_dev(struct inet6_dev
*idev
)
2075 struct in6_addr maddr
;
2077 write_lock_bh(&idev
->lock
);
2078 rwlock_init(&idev
->mc_lock
);
2079 idev
->mc_gq_running
= 0;
2080 init_timer(&idev
->mc_gq_timer
);
2081 idev
->mc_gq_timer
.data
= (unsigned long) idev
;
2082 idev
->mc_gq_timer
.function
= &mld_gq_timer_expire
;
2083 idev
->mc_tomb
= NULL
;
2084 idev
->mc_ifc_count
= 0;
2085 init_timer(&idev
->mc_ifc_timer
);
2086 idev
->mc_ifc_timer
.data
= (unsigned long) idev
;
2087 idev
->mc_ifc_timer
.function
= &mld_ifc_timer_expire
;
2088 idev
->mc_qrv
= MLD_QRV_DEFAULT
;
2089 idev
->mc_maxdelay
= IGMP6_UNSOLICITED_IVAL
;
2090 idev
->mc_v1_seen
= 0;
2091 write_unlock_bh(&idev
->lock
);
2093 /* Add all-nodes address. */
2094 ipv6_addr_all_nodes(&maddr
);
2095 ipv6_dev_mc_inc(idev
->dev
, &maddr
);
2099 * Device is about to be destroyed: clean up.
2102 void ipv6_mc_destroy_dev(struct inet6_dev
*idev
)
2104 struct ifmcaddr6
*i
;
2105 struct in6_addr maddr
;
2107 /* Deactivate timers */
2110 /* Delete all-nodes address. */
2111 ipv6_addr_all_nodes(&maddr
);
2113 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2114 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2117 __ipv6_dev_mc_dec(idev
, &maddr
);
2119 if (idev
->cnf
.forwarding
) {
2120 ipv6_addr_all_routers(&maddr
);
2121 __ipv6_dev_mc_dec(idev
, &maddr
);
2124 write_lock_bh(&idev
->lock
);
2125 while ((i
= idev
->mc_list
) != NULL
) {
2126 idev
->mc_list
= i
->next
;
2127 write_unlock_bh(&idev
->lock
);
2129 igmp6_group_dropped(i
);
2132 write_lock_bh(&idev
->lock
);
2134 write_unlock_bh(&idev
->lock
);
2137 #ifdef CONFIG_PROC_FS
2138 struct igmp6_mc_iter_state
{
2139 struct net_device
*dev
;
2140 struct inet6_dev
*idev
;
2143 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2145 static inline struct ifmcaddr6
*igmp6_mc_get_first(struct seq_file
*seq
)
2147 struct ifmcaddr6
*im
= NULL
;
2148 struct igmp6_mc_iter_state
*state
= igmp6_mc_seq_private(seq
);
2150 for (state
->dev
= dev_base
, state
->idev
= NULL
;
2152 state
->dev
= state
->dev
->next
) {
2153 struct inet6_dev
*idev
;
2154 idev
= in6_dev_get(state
->dev
);
2157 read_lock_bh(&idev
->lock
);
2163 read_unlock_bh(&idev
->lock
);
2169 static struct ifmcaddr6
*igmp6_mc_get_next(struct seq_file
*seq
, struct ifmcaddr6
*im
)
2171 struct igmp6_mc_iter_state
*state
= igmp6_mc_seq_private(seq
);
2175 if (likely(state
->idev
!= NULL
)) {
2176 read_unlock_bh(&state
->idev
->lock
);
2177 in6_dev_put(state
->idev
);
2179 state
->dev
= state
->dev
->next
;
2184 state
->idev
= in6_dev_get(state
->dev
);
2187 read_lock_bh(&state
->idev
->lock
);
2188 im
= state
->idev
->mc_list
;
2193 static struct ifmcaddr6
*igmp6_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2195 struct ifmcaddr6
*im
= igmp6_mc_get_first(seq
);
2197 while (pos
&& (im
= igmp6_mc_get_next(seq
, im
)) != NULL
)
2199 return pos
? NULL
: im
;
2202 static void *igmp6_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2204 read_lock(&dev_base_lock
);
2205 return igmp6_mc_get_idx(seq
, *pos
);
2208 static void *igmp6_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2210 struct ifmcaddr6
*im
;
2211 im
= igmp6_mc_get_next(seq
, v
);
2216 static void igmp6_mc_seq_stop(struct seq_file
*seq
, void *v
)
2218 struct igmp6_mc_iter_state
*state
= igmp6_mc_seq_private(seq
);
2219 if (likely(state
->idev
!= NULL
)) {
2220 read_unlock_bh(&state
->idev
->lock
);
2221 in6_dev_put(state
->idev
);
2225 read_unlock(&dev_base_lock
);
2228 static int igmp6_mc_seq_show(struct seq_file
*seq
, void *v
)
2230 struct ifmcaddr6
*im
= (struct ifmcaddr6
*)v
;
2231 struct igmp6_mc_iter_state
*state
= igmp6_mc_seq_private(seq
);
2234 "%-4d %-15s %04x%04x%04x%04x%04x%04x%04x%04x %5d %08X %ld\n",
2235 state
->dev
->ifindex
, state
->dev
->name
,
2237 im
->mca_users
, im
->mca_flags
,
2238 (im
->mca_flags
&MAF_TIMER_RUNNING
) ?
2239 jiffies_to_clock_t(im
->mca_timer
.expires
-jiffies
) : 0);
2243 static struct seq_operations igmp6_mc_seq_ops
= {
2244 .start
= igmp6_mc_seq_start
,
2245 .next
= igmp6_mc_seq_next
,
2246 .stop
= igmp6_mc_seq_stop
,
2247 .show
= igmp6_mc_seq_show
,
2250 static int igmp6_mc_seq_open(struct inode
*inode
, struct file
*file
)
2252 struct seq_file
*seq
;
2254 struct igmp6_mc_iter_state
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2259 rc
= seq_open(file
, &igmp6_mc_seq_ops
);
2263 seq
= file
->private_data
;
2265 memset(s
, 0, sizeof(*s
));
2273 static struct file_operations igmp6_mc_seq_fops
= {
2274 .owner
= THIS_MODULE
,
2275 .open
= igmp6_mc_seq_open
,
2277 .llseek
= seq_lseek
,
2278 .release
= seq_release_private
,
2281 struct igmp6_mcf_iter_state
{
2282 struct net_device
*dev
;
2283 struct inet6_dev
*idev
;
2284 struct ifmcaddr6
*im
;
2287 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2289 static inline struct ip6_sf_list
*igmp6_mcf_get_first(struct seq_file
*seq
)
2291 struct ip6_sf_list
*psf
= NULL
;
2292 struct ifmcaddr6
*im
= NULL
;
2293 struct igmp6_mcf_iter_state
*state
= igmp6_mcf_seq_private(seq
);
2295 for (state
->dev
= dev_base
, state
->idev
= NULL
, state
->im
= NULL
;
2297 state
->dev
= state
->dev
->next
) {
2298 struct inet6_dev
*idev
;
2299 idev
= in6_dev_get(state
->dev
);
2300 if (unlikely(idev
== NULL
))
2302 read_lock_bh(&idev
->lock
);
2304 if (likely(im
!= NULL
)) {
2305 spin_lock_bh(&im
->mca_lock
);
2306 psf
= im
->mca_sources
;
2307 if (likely(psf
!= NULL
)) {
2312 spin_unlock_bh(&im
->mca_lock
);
2314 read_unlock_bh(&idev
->lock
);
2320 static struct ip6_sf_list
*igmp6_mcf_get_next(struct seq_file
*seq
, struct ip6_sf_list
*psf
)
2322 struct igmp6_mcf_iter_state
*state
= igmp6_mcf_seq_private(seq
);
2326 spin_unlock_bh(&state
->im
->mca_lock
);
2327 state
->im
= state
->im
->next
;
2328 while (!state
->im
) {
2329 if (likely(state
->idev
!= NULL
)) {
2330 read_unlock_bh(&state
->idev
->lock
);
2331 in6_dev_put(state
->idev
);
2333 state
->dev
= state
->dev
->next
;
2338 state
->idev
= in6_dev_get(state
->dev
);
2341 read_lock_bh(&state
->idev
->lock
);
2342 state
->im
= state
->idev
->mc_list
;
2346 spin_lock_bh(&state
->im
->mca_lock
);
2347 psf
= state
->im
->mca_sources
;
2353 static struct ip6_sf_list
*igmp6_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2355 struct ip6_sf_list
*psf
= igmp6_mcf_get_first(seq
);
2357 while (pos
&& (psf
= igmp6_mcf_get_next(seq
, psf
)) != NULL
)
2359 return pos
? NULL
: psf
;
2362 static void *igmp6_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2364 read_lock(&dev_base_lock
);
2365 return *pos
? igmp6_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2368 static void *igmp6_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2370 struct ip6_sf_list
*psf
;
2371 if (v
== SEQ_START_TOKEN
)
2372 psf
= igmp6_mcf_get_first(seq
);
2374 psf
= igmp6_mcf_get_next(seq
, v
);
2379 static void igmp6_mcf_seq_stop(struct seq_file
*seq
, void *v
)
2381 struct igmp6_mcf_iter_state
*state
= igmp6_mcf_seq_private(seq
);
2382 if (likely(state
->im
!= NULL
)) {
2383 spin_unlock_bh(&state
->im
->mca_lock
);
2386 if (likely(state
->idev
!= NULL
)) {
2387 read_unlock_bh(&state
->idev
->lock
);
2388 in6_dev_put(state
->idev
);
2392 read_unlock(&dev_base_lock
);
2395 static int igmp6_mcf_seq_show(struct seq_file
*seq
, void *v
)
2397 struct ip6_sf_list
*psf
= (struct ip6_sf_list
*)v
;
2398 struct igmp6_mcf_iter_state
*state
= igmp6_mcf_seq_private(seq
);
2400 if (v
== SEQ_START_TOKEN
) {
2403 "%32s %32s %6s %6s\n", "Idx",
2404 "Device", "Multicast Address",
2405 "Source Address", "INC", "EXC");
2409 "%04x%04x%04x%04x%04x%04x%04x%04x "
2410 "%04x%04x%04x%04x%04x%04x%04x%04x "
2412 state
->dev
->ifindex
, state
->dev
->name
,
2413 NIP6(state
->im
->mca_addr
),
2415 psf
->sf_count
[MCAST_INCLUDE
],
2416 psf
->sf_count
[MCAST_EXCLUDE
]);
2421 static struct seq_operations igmp6_mcf_seq_ops
= {
2422 .start
= igmp6_mcf_seq_start
,
2423 .next
= igmp6_mcf_seq_next
,
2424 .stop
= igmp6_mcf_seq_stop
,
2425 .show
= igmp6_mcf_seq_show
,
2428 static int igmp6_mcf_seq_open(struct inode
*inode
, struct file
*file
)
2430 struct seq_file
*seq
;
2432 struct igmp6_mcf_iter_state
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2437 rc
= seq_open(file
, &igmp6_mcf_seq_ops
);
2441 seq
= file
->private_data
;
2443 memset(s
, 0, sizeof(*s
));
2451 static struct file_operations igmp6_mcf_seq_fops
= {
2452 .owner
= THIS_MODULE
,
2453 .open
= igmp6_mcf_seq_open
,
2455 .llseek
= seq_lseek
,
2456 .release
= seq_release_private
,
2460 int __init
igmp6_init(struct net_proto_family
*ops
)
2462 struct ipv6_pinfo
*np
;
2466 err
= sock_create_kern(PF_INET6
, SOCK_RAW
, IPPROTO_ICMPV6
, &igmp6_socket
);
2469 "Failed to initialize the IGMP6 control socket (err %d).\n",
2471 igmp6_socket
= NULL
; /* For safety. */
2475 sk
= igmp6_socket
->sk
;
2476 sk
->sk_allocation
= GFP_ATOMIC
;
2477 sk
->sk_prot
->unhash(sk
);
2482 #ifdef CONFIG_PROC_FS
2483 proc_net_fops_create("igmp6", S_IRUGO
, &igmp6_mc_seq_fops
);
2484 proc_net_fops_create("mcfilter6", S_IRUGO
, &igmp6_mcf_seq_fops
);
2490 void igmp6_cleanup(void)
2492 sock_release(igmp6_socket
);
2493 igmp6_socket
= NULL
; /* for safety */
2495 #ifdef CONFIG_PROC_FS
2496 proc_net_remove("mcfilter6");
2497 proc_net_remove("igmp6");