2 * Multicast support for IPv6
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: mcast.c,v 1.40 2002/02/08 03:57:19 davem Exp $
10 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
20 * yoshfuji : fix format of router-alert option
21 * YOSHIFUJI Hideaki @USAGI:
22 * Fixed source address for MLD message based on
23 * <draft-ietf-magma-mld-source-05.txt>.
24 * YOSHIFUJI Hideaki @USAGI:
25 * - Ignore Queries for invalid addresses.
26 * - MLD for link-local addresses.
27 * David L Stevens <dlstevens@us.ibm.com>:
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/errno.h>
34 #include <linux/types.h>
35 #include <linux/string.h>
36 #include <linux/socket.h>
37 #include <linux/sockios.h>
38 #include <linux/jiffies.h>
39 #include <linux/times.h>
40 #include <linux/net.h>
42 #include <linux/in6.h>
43 #include <linux/netdevice.h>
44 #include <linux/if_arp.h>
45 #include <linux/route.h>
46 #include <linux/init.h>
47 #include <linux/proc_fs.h>
48 #include <linux/seq_file.h>
50 #include <linux/netfilter.h>
51 #include <linux/netfilter_ipv6.h>
57 #include <net/protocol.h>
58 #include <net/if_inet6.h>
59 #include <net/ndisc.h>
60 #include <net/addrconf.h>
61 #include <net/ip6_route.h>
63 #include <net/ip6_checksum.h>
65 /* Set to 3 to get tracing... */
69 #define MDBG(x) printk x
75 * These header formats should be in a separate include file, but icmpv6.h
76 * doesn't have in6_addr defined in all cases, there is no __u128, and no
77 * other files reference these.
82 /* Multicast Listener Discovery version 2 headers */
88 struct in6_addr grec_mca
;
89 struct in6_addr grec_src
[0];
98 struct mld2_grec grec
[0];
108 #if defined(__LITTLE_ENDIAN_BITFIELD)
112 #elif defined(__BIG_ENDIAN_BITFIELD)
117 #error "Please fix <asm/byteorder.h>"
121 struct in6_addr srcs
[0];
124 static struct in6_addr mld2_all_mcr
= MLD2_ALL_MCR_INIT
;
126 /* Big mc list lock for all the sockets */
127 static DEFINE_RWLOCK(ipv6_sk_mc_lock
);
129 static struct socket
*igmp6_socket
;
131 int __ipv6_dev_mc_dec(struct inet6_dev
*idev
, struct in6_addr
*addr
);
133 static void igmp6_join_group(struct ifmcaddr6
*ma
);
134 static void igmp6_leave_group(struct ifmcaddr6
*ma
);
135 static void igmp6_timer_handler(unsigned long data
);
137 static void mld_gq_timer_expire(unsigned long data
);
138 static void mld_ifc_timer_expire(unsigned long data
);
139 static void mld_ifc_event(struct inet6_dev
*idev
);
140 static void mld_add_delrec(struct inet6_dev
*idev
, struct ifmcaddr6
*pmc
);
141 static void mld_del_delrec(struct inet6_dev
*idev
, struct in6_addr
*addr
);
142 static void mld_clear_delrec(struct inet6_dev
*idev
);
143 static int sf_setstate(struct ifmcaddr6
*pmc
);
144 static void sf_markstate(struct ifmcaddr6
*pmc
);
145 static void ip6_mc_clear_src(struct ifmcaddr6
*pmc
);
146 static int ip6_mc_del_src(struct inet6_dev
*idev
, struct in6_addr
*pmca
,
147 int sfmode
, int sfcount
, struct in6_addr
*psfsrc
,
149 static int ip6_mc_add_src(struct inet6_dev
*idev
, struct in6_addr
*pmca
,
150 int sfmode
, int sfcount
, struct in6_addr
*psfsrc
,
152 static int ip6_mc_leave_src(struct sock
*sk
, struct ipv6_mc_socklist
*iml
,
153 struct inet6_dev
*idev
);
156 #define IGMP6_UNSOLICITED_IVAL (10*HZ)
157 #define MLD_QRV_DEFAULT 2
159 #define MLD_V1_SEEN(idev) (ipv6_devconf.force_mld_version == 1 || \
160 (idev)->cnf.force_mld_version == 1 || \
161 ((idev)->mc_v1_seen && \
162 time_before(jiffies, (idev)->mc_v1_seen)))
164 #define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
165 #define MLDV2_EXP(thresh, nbmant, nbexp, value) \
166 ((value) < (thresh) ? (value) : \
167 ((MLDV2_MASK(value, nbmant) | (1<<(nbmant))) << \
168 (MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp))))
170 #define MLDV2_QQIC(value) MLDV2_EXP(0x80, 4, 3, value)
171 #define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
173 #define IPV6_MLD_MAX_MSF 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 read_lock_bh(&ipv6_sk_mc_lock
);
192 for (mc_lst
=np
->ipv6_mc_list
; mc_lst
; mc_lst
=mc_lst
->next
) {
193 if ((ifindex
== 0 || mc_lst
->ifindex
== ifindex
) &&
194 ipv6_addr_equal(&mc_lst
->addr
, addr
)) {
195 read_unlock_bh(&ipv6_sk_mc_lock
);
199 read_unlock_bh(&ipv6_sk_mc_lock
);
201 mc_lst
= sock_kmalloc(sk
, sizeof(struct ipv6_mc_socklist
), GFP_KERNEL
);
207 ipv6_addr_copy(&mc_lst
->addr
, addr
);
211 rt
= rt6_lookup(addr
, NULL
, 0, 0);
215 dst_release(&rt
->u
.dst
);
218 dev
= dev_get_by_index(ifindex
);
221 sock_kfree_s(sk
, mc_lst
, sizeof(*mc_lst
));
225 mc_lst
->ifindex
= dev
->ifindex
;
226 mc_lst
->sfmode
= MCAST_EXCLUDE
;
227 mc_lst
->sflist
= NULL
;
230 * now add/increase the group membership on the device
233 err
= ipv6_dev_mc_inc(dev
, addr
);
236 sock_kfree_s(sk
, mc_lst
, sizeof(*mc_lst
));
241 write_lock_bh(&ipv6_sk_mc_lock
);
242 mc_lst
->next
= np
->ipv6_mc_list
;
243 np
->ipv6_mc_list
= mc_lst
;
244 write_unlock_bh(&ipv6_sk_mc_lock
);
252 * socket leave on multicast group
254 int ipv6_sock_mc_drop(struct sock
*sk
, int ifindex
, struct in6_addr
*addr
)
256 struct ipv6_pinfo
*np
= inet6_sk(sk
);
257 struct ipv6_mc_socklist
*mc_lst
, **lnk
;
259 write_lock_bh(&ipv6_sk_mc_lock
);
260 for (lnk
= &np
->ipv6_mc_list
; (mc_lst
= *lnk
) !=NULL
; lnk
= &mc_lst
->next
) {
261 if ((ifindex
== 0 || mc_lst
->ifindex
== ifindex
) &&
262 ipv6_addr_equal(&mc_lst
->addr
, addr
)) {
263 struct net_device
*dev
;
266 write_unlock_bh(&ipv6_sk_mc_lock
);
268 if ((dev
= dev_get_by_index(mc_lst
->ifindex
)) != NULL
) {
269 struct inet6_dev
*idev
= in6_dev_get(dev
);
272 (void) ip6_mc_leave_src(sk
,mc_lst
,idev
);
273 __ipv6_dev_mc_dec(idev
, &mc_lst
->addr
);
278 sock_kfree_s(sk
, mc_lst
, sizeof(*mc_lst
));
282 write_unlock_bh(&ipv6_sk_mc_lock
);
284 return -EADDRNOTAVAIL
;
287 static struct inet6_dev
*ip6_mc_find_dev(struct in6_addr
*group
, int ifindex
)
289 struct net_device
*dev
= NULL
;
290 struct inet6_dev
*idev
= NULL
;
295 rt
= rt6_lookup(group
, NULL
, 0, 0);
299 dst_release(&rt
->u
.dst
);
302 dev
= dev_get_by_index(ifindex
);
306 idev
= in6_dev_get(dev
);
311 read_lock_bh(&idev
->lock
);
313 read_unlock_bh(&idev
->lock
);
321 void ipv6_sock_mc_close(struct sock
*sk
)
323 struct ipv6_pinfo
*np
= inet6_sk(sk
);
324 struct ipv6_mc_socklist
*mc_lst
;
326 write_lock_bh(&ipv6_sk_mc_lock
);
327 while ((mc_lst
= np
->ipv6_mc_list
) != NULL
) {
328 struct net_device
*dev
;
330 np
->ipv6_mc_list
= mc_lst
->next
;
331 write_unlock_bh(&ipv6_sk_mc_lock
);
333 dev
= dev_get_by_index(mc_lst
->ifindex
);
335 struct inet6_dev
*idev
= in6_dev_get(dev
);
338 (void) ip6_mc_leave_src(sk
, mc_lst
, idev
);
339 __ipv6_dev_mc_dec(idev
, &mc_lst
->addr
);
345 sock_kfree_s(sk
, mc_lst
, sizeof(*mc_lst
));
347 write_lock_bh(&ipv6_sk_mc_lock
);
349 write_unlock_bh(&ipv6_sk_mc_lock
);
352 int ip6_mc_source(int add
, int omode
, struct sock
*sk
,
353 struct group_source_req
*pgsr
)
355 struct in6_addr
*source
, *group
;
356 struct ipv6_mc_socklist
*pmc
;
357 struct net_device
*dev
;
358 struct inet6_dev
*idev
;
359 struct ipv6_pinfo
*inet6
= inet6_sk(sk
);
360 struct ip6_sf_socklist
*psl
;
365 if (pgsr
->gsr_group
.ss_family
!= AF_INET6
||
366 pgsr
->gsr_source
.ss_family
!= AF_INET6
)
369 source
= &((struct sockaddr_in6
*)&pgsr
->gsr_source
)->sin6_addr
;
370 group
= &((struct sockaddr_in6
*)&pgsr
->gsr_group
)->sin6_addr
;
372 if (!ipv6_addr_is_multicast(group
))
375 idev
= ip6_mc_find_dev(group
, pgsr
->gsr_interface
);
380 err
= -EADDRNOTAVAIL
;
382 read_lock_bh(&ipv6_sk_mc_lock
);
383 for (pmc
=inet6
->ipv6_mc_list
; pmc
; pmc
=pmc
->next
) {
384 if (pgsr
->gsr_interface
&& pmc
->ifindex
!= pgsr
->gsr_interface
)
386 if (ipv6_addr_equal(&pmc
->addr
, group
))
389 if (!pmc
) { /* must have a prior join */
393 /* if a source filter was set, must be the same mode as before */
395 if (pmc
->sfmode
!= omode
) {
399 } else if (pmc
->sfmode
!= omode
) {
400 /* allow mode switches for empty-set filters */
401 ip6_mc_add_src(idev
, group
, omode
, 0, NULL
, 0);
402 ip6_mc_del_src(idev
, group
, pmc
->sfmode
, 0, NULL
, 0);
409 goto done
; /* err = -EADDRNOTAVAIL */
411 for (i
=0; i
<psl
->sl_count
; i
++) {
412 rv
= memcmp(&psl
->sl_addr
[i
], source
,
413 sizeof(struct in6_addr
));
417 if (rv
) /* source not found */
418 goto done
; /* err = -EADDRNOTAVAIL */
420 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
421 if (psl
->sl_count
== 1 && omode
== MCAST_INCLUDE
) {
426 /* update the interface filter */
427 ip6_mc_del_src(idev
, group
, omode
, 1, source
, 1);
429 for (j
=i
+1; j
<psl
->sl_count
; j
++)
430 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
435 /* else, add a new source to the filter */
437 if (psl
&& psl
->sl_count
>= sysctl_mld_max_msf
) {
441 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
442 struct ip6_sf_socklist
*newpsl
;
443 int count
= IP6_SFBLOCK
;
446 count
+= psl
->sl_max
;
447 newpsl
= (struct ip6_sf_socklist
*)sock_kmalloc(sk
,
448 IP6_SFLSIZE(count
), GFP_ATOMIC
);
453 newpsl
->sl_max
= count
;
454 newpsl
->sl_count
= count
- IP6_SFBLOCK
;
456 for (i
=0; i
<psl
->sl_count
; i
++)
457 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
458 sock_kfree_s(sk
, psl
, IP6_SFLSIZE(psl
->sl_max
));
460 pmc
->sflist
= psl
= newpsl
;
462 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
463 for (i
=0; i
<psl
->sl_count
; i
++) {
464 rv
= memcmp(&psl
->sl_addr
[i
], source
, sizeof(struct in6_addr
));
468 if (rv
== 0) /* address already there is an error */
470 for (j
=psl
->sl_count
-1; j
>=i
; j
--)
471 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
472 psl
->sl_addr
[i
] = *source
;
475 /* update the interface list */
476 ip6_mc_add_src(idev
, group
, omode
, 1, source
, 1);
478 read_unlock_bh(&ipv6_sk_mc_lock
);
479 read_unlock_bh(&idev
->lock
);
483 return ipv6_sock_mc_drop(sk
, pgsr
->gsr_interface
, group
);
487 int ip6_mc_msfilter(struct sock
*sk
, struct group_filter
*gsf
)
489 struct in6_addr
*group
;
490 struct ipv6_mc_socklist
*pmc
;
491 struct net_device
*dev
;
492 struct inet6_dev
*idev
;
493 struct ipv6_pinfo
*inet6
= inet6_sk(sk
);
494 struct ip6_sf_socklist
*newpsl
, *psl
;
498 group
= &((struct sockaddr_in6
*)&gsf
->gf_group
)->sin6_addr
;
500 if (!ipv6_addr_is_multicast(group
))
502 if (gsf
->gf_fmode
!= MCAST_INCLUDE
&&
503 gsf
->gf_fmode
!= MCAST_EXCLUDE
)
506 idev
= ip6_mc_find_dev(group
, gsf
->gf_interface
);
513 if (gsf
->gf_fmode
== MCAST_INCLUDE
&& gsf
->gf_numsrc
== 0) {
518 for (pmc
=inet6
->ipv6_mc_list
; pmc
; pmc
=pmc
->next
) {
519 if (pmc
->ifindex
!= gsf
->gf_interface
)
521 if (ipv6_addr_equal(&pmc
->addr
, group
))
524 if (!pmc
) { /* must have a prior join */
528 if (gsf
->gf_numsrc
) {
529 newpsl
= (struct ip6_sf_socklist
*)sock_kmalloc(sk
,
530 IP6_SFLSIZE(gsf
->gf_numsrc
), GFP_ATOMIC
);
535 newpsl
->sl_max
= newpsl
->sl_count
= gsf
->gf_numsrc
;
536 for (i
=0; i
<newpsl
->sl_count
; ++i
) {
537 struct sockaddr_in6
*psin6
;
539 psin6
= (struct sockaddr_in6
*)&gsf
->gf_slist
[i
];
540 newpsl
->sl_addr
[i
] = psin6
->sin6_addr
;
542 err
= ip6_mc_add_src(idev
, group
, gsf
->gf_fmode
,
543 newpsl
->sl_count
, newpsl
->sl_addr
, 0);
545 sock_kfree_s(sk
, newpsl
, IP6_SFLSIZE(newpsl
->sl_max
));
552 (void) ip6_mc_del_src(idev
, group
, pmc
->sfmode
,
553 psl
->sl_count
, psl
->sl_addr
, 0);
554 sock_kfree_s(sk
, psl
, IP6_SFLSIZE(psl
->sl_max
));
556 (void) ip6_mc_del_src(idev
, group
, pmc
->sfmode
, 0, NULL
, 0);
557 pmc
->sflist
= newpsl
;
558 pmc
->sfmode
= gsf
->gf_fmode
;
561 read_unlock_bh(&idev
->lock
);
565 err
= ipv6_sock_mc_drop(sk
, gsf
->gf_interface
, group
);
569 int ip6_mc_msfget(struct sock
*sk
, struct group_filter
*gsf
,
570 struct group_filter __user
*optval
, int __user
*optlen
)
572 int err
, i
, count
, copycount
;
573 struct in6_addr
*group
;
574 struct ipv6_mc_socklist
*pmc
;
575 struct inet6_dev
*idev
;
576 struct net_device
*dev
;
577 struct ipv6_pinfo
*inet6
= inet6_sk(sk
);
578 struct ip6_sf_socklist
*psl
;
580 group
= &((struct sockaddr_in6
*)&gsf
->gf_group
)->sin6_addr
;
582 if (!ipv6_addr_is_multicast(group
))
585 idev
= ip6_mc_find_dev(group
, gsf
->gf_interface
);
592 err
= -EADDRNOTAVAIL
;
594 for (pmc
=inet6
->ipv6_mc_list
; pmc
; pmc
=pmc
->next
) {
595 if (pmc
->ifindex
!= gsf
->gf_interface
)
597 if (ipv6_addr_equal(group
, &pmc
->addr
))
600 if (!pmc
) /* must have a prior join */
602 gsf
->gf_fmode
= pmc
->sfmode
;
604 count
= psl
? psl
->sl_count
: 0;
605 read_unlock_bh(&idev
->lock
);
609 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
610 gsf
->gf_numsrc
= count
;
611 if (put_user(GROUP_FILTER_SIZE(copycount
), optlen
) ||
612 copy_to_user(optval
, gsf
, GROUP_FILTER_SIZE(0))) {
615 for (i
=0; i
<copycount
; i
++) {
616 struct sockaddr_in6
*psin6
;
617 struct sockaddr_storage ss
;
619 psin6
= (struct sockaddr_in6
*)&ss
;
620 memset(&ss
, 0, sizeof(ss
));
621 psin6
->sin6_family
= AF_INET6
;
622 psin6
->sin6_addr
= psl
->sl_addr
[i
];
623 if (copy_to_user(&optval
->gf_slist
[i
], &ss
, sizeof(ss
)))
628 read_unlock_bh(&idev
->lock
);
634 int inet6_mc_check(struct sock
*sk
, struct in6_addr
*mc_addr
,
635 struct in6_addr
*src_addr
)
637 struct ipv6_pinfo
*np
= inet6_sk(sk
);
638 struct ipv6_mc_socklist
*mc
;
639 struct ip6_sf_socklist
*psl
;
642 read_lock(&ipv6_sk_mc_lock
);
643 for (mc
= np
->ipv6_mc_list
; mc
; mc
= mc
->next
) {
644 if (ipv6_addr_equal(&mc
->addr
, mc_addr
))
648 read_unlock(&ipv6_sk_mc_lock
);
653 rv
= mc
->sfmode
== MCAST_EXCLUDE
;
657 for (i
=0; i
<psl
->sl_count
; i
++) {
658 if (ipv6_addr_equal(&psl
->sl_addr
[i
], src_addr
))
661 if (mc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
663 if (mc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
666 read_unlock(&ipv6_sk_mc_lock
);
671 static void ma_put(struct ifmcaddr6
*mc
)
673 if (atomic_dec_and_test(&mc
->mca_refcnt
)) {
674 in6_dev_put(mc
->idev
);
679 static void igmp6_group_added(struct ifmcaddr6
*mc
)
681 struct net_device
*dev
= mc
->idev
->dev
;
682 char buf
[MAX_ADDR_LEN
];
684 spin_lock_bh(&mc
->mca_lock
);
685 if (!(mc
->mca_flags
&MAF_LOADED
)) {
686 mc
->mca_flags
|= MAF_LOADED
;
687 if (ndisc_mc_map(&mc
->mca_addr
, buf
, dev
, 0) == 0)
688 dev_mc_add(dev
, buf
, dev
->addr_len
, 0);
690 spin_unlock_bh(&mc
->mca_lock
);
692 if (!(dev
->flags
& IFF_UP
) || (mc
->mca_flags
& MAF_NOREPORT
))
695 if (MLD_V1_SEEN(mc
->idev
)) {
696 igmp6_join_group(mc
);
701 mc
->mca_crcount
= mc
->idev
->mc_qrv
;
702 mld_ifc_event(mc
->idev
);
705 static void igmp6_group_dropped(struct ifmcaddr6
*mc
)
707 struct net_device
*dev
= mc
->idev
->dev
;
708 char buf
[MAX_ADDR_LEN
];
710 spin_lock_bh(&mc
->mca_lock
);
711 if (mc
->mca_flags
&MAF_LOADED
) {
712 mc
->mca_flags
&= ~MAF_LOADED
;
713 if (ndisc_mc_map(&mc
->mca_addr
, buf
, dev
, 0) == 0)
714 dev_mc_delete(dev
, buf
, dev
->addr_len
, 0);
717 if (mc
->mca_flags
& MAF_NOREPORT
)
719 spin_unlock_bh(&mc
->mca_lock
);
722 igmp6_leave_group(mc
);
724 spin_lock_bh(&mc
->mca_lock
);
725 if (del_timer(&mc
->mca_timer
))
726 atomic_dec(&mc
->mca_refcnt
);
728 ip6_mc_clear_src(mc
);
729 spin_unlock_bh(&mc
->mca_lock
);
733 * deleted ifmcaddr6 manipulation
735 static void mld_add_delrec(struct inet6_dev
*idev
, struct ifmcaddr6
*im
)
737 struct ifmcaddr6
*pmc
;
739 /* this is an "ifmcaddr6" for convenience; only the fields below
740 * are actually used. In particular, the refcnt and users are not
741 * used for management of the delete list. Using the same structure
742 * for deleted items allows change reports to use common code with
743 * non-deleted or query-response MCA's.
745 pmc
= (struct ifmcaddr6
*)kmalloc(sizeof(*pmc
), GFP_ATOMIC
);
748 memset(pmc
, 0, sizeof(*pmc
));
749 spin_lock_bh(&im
->mca_lock
);
750 spin_lock_init(&pmc
->mca_lock
);
751 pmc
->idev
= im
->idev
;
753 pmc
->mca_addr
= im
->mca_addr
;
754 pmc
->mca_crcount
= idev
->mc_qrv
;
755 pmc
->mca_sfmode
= im
->mca_sfmode
;
756 if (pmc
->mca_sfmode
== MCAST_INCLUDE
) {
757 struct ip6_sf_list
*psf
;
759 pmc
->mca_tomb
= im
->mca_tomb
;
760 pmc
->mca_sources
= im
->mca_sources
;
761 im
->mca_tomb
= im
->mca_sources
= NULL
;
762 for (psf
=pmc
->mca_sources
; psf
; psf
=psf
->sf_next
)
763 psf
->sf_crcount
= pmc
->mca_crcount
;
765 spin_unlock_bh(&im
->mca_lock
);
767 write_lock_bh(&idev
->mc_lock
);
768 pmc
->next
= idev
->mc_tomb
;
770 write_unlock_bh(&idev
->mc_lock
);
773 static void mld_del_delrec(struct inet6_dev
*idev
, struct in6_addr
*pmca
)
775 struct ifmcaddr6
*pmc
, *pmc_prev
;
776 struct ip6_sf_list
*psf
, *psf_next
;
778 write_lock_bh(&idev
->mc_lock
);
780 for (pmc
=idev
->mc_tomb
; pmc
; pmc
=pmc
->next
) {
781 if (ipv6_addr_equal(&pmc
->mca_addr
, pmca
))
787 pmc_prev
->next
= pmc
->next
;
789 idev
->mc_tomb
= pmc
->next
;
791 write_unlock_bh(&idev
->mc_lock
);
793 for (psf
=pmc
->mca_tomb
; psf
; psf
=psf_next
) {
794 psf_next
= psf
->sf_next
;
797 in6_dev_put(pmc
->idev
);
802 static void mld_clear_delrec(struct inet6_dev
*idev
)
804 struct ifmcaddr6
*pmc
, *nextpmc
;
806 write_lock_bh(&idev
->mc_lock
);
808 idev
->mc_tomb
= NULL
;
809 write_unlock_bh(&idev
->mc_lock
);
811 for (; pmc
; pmc
= nextpmc
) {
813 ip6_mc_clear_src(pmc
);
814 in6_dev_put(pmc
->idev
);
818 /* clear dead sources, too */
819 read_lock_bh(&idev
->lock
);
820 for (pmc
=idev
->mc_list
; pmc
; pmc
=pmc
->next
) {
821 struct ip6_sf_list
*psf
, *psf_next
;
823 spin_lock_bh(&pmc
->mca_lock
);
825 pmc
->mca_tomb
= NULL
;
826 spin_unlock_bh(&pmc
->mca_lock
);
827 for (; psf
; psf
=psf_next
) {
828 psf_next
= psf
->sf_next
;
832 read_unlock_bh(&idev
->lock
);
837 * device multicast group inc (add if not found)
839 int ipv6_dev_mc_inc(struct net_device
*dev
, struct in6_addr
*addr
)
841 struct ifmcaddr6
*mc
;
842 struct inet6_dev
*idev
;
844 idev
= in6_dev_get(dev
);
849 write_lock_bh(&idev
->lock
);
851 write_unlock_bh(&idev
->lock
);
856 for (mc
= idev
->mc_list
; mc
; mc
= mc
->next
) {
857 if (ipv6_addr_equal(&mc
->mca_addr
, addr
)) {
859 write_unlock_bh(&idev
->lock
);
860 ip6_mc_add_src(idev
, &mc
->mca_addr
, MCAST_EXCLUDE
, 0,
868 * not found: create a new one.
871 mc
= kmalloc(sizeof(struct ifmcaddr6
), GFP_ATOMIC
);
874 write_unlock_bh(&idev
->lock
);
879 memset(mc
, 0, sizeof(struct ifmcaddr6
));
880 init_timer(&mc
->mca_timer
);
881 mc
->mca_timer
.function
= igmp6_timer_handler
;
882 mc
->mca_timer
.data
= (unsigned long) mc
;
884 ipv6_addr_copy(&mc
->mca_addr
, addr
);
887 /* mca_stamp should be updated upon changes */
888 mc
->mca_cstamp
= mc
->mca_tstamp
= jiffies
;
889 atomic_set(&mc
->mca_refcnt
, 2);
890 spin_lock_init(&mc
->mca_lock
);
892 /* initial mode is (EX, empty) */
893 mc
->mca_sfmode
= MCAST_EXCLUDE
;
894 mc
->mca_sfcount
[MCAST_EXCLUDE
] = 1;
896 if (ipv6_addr_is_ll_all_nodes(&mc
->mca_addr
) ||
897 IPV6_ADDR_MC_SCOPE(&mc
->mca_addr
) < IPV6_ADDR_SCOPE_LINKLOCAL
)
898 mc
->mca_flags
|= MAF_NOREPORT
;
900 mc
->next
= idev
->mc_list
;
902 write_unlock_bh(&idev
->lock
);
904 mld_del_delrec(idev
, &mc
->mca_addr
);
905 igmp6_group_added(mc
);
911 * device multicast group del
913 int __ipv6_dev_mc_dec(struct inet6_dev
*idev
, struct in6_addr
*addr
)
915 struct ifmcaddr6
*ma
, **map
;
917 write_lock_bh(&idev
->lock
);
918 for (map
= &idev
->mc_list
; (ma
=*map
) != NULL
; map
= &ma
->next
) {
919 if (ipv6_addr_equal(&ma
->mca_addr
, addr
)) {
920 if (--ma
->mca_users
== 0) {
922 write_unlock_bh(&idev
->lock
);
924 igmp6_group_dropped(ma
);
929 write_unlock_bh(&idev
->lock
);
933 write_unlock_bh(&idev
->lock
);
938 int ipv6_dev_mc_dec(struct net_device
*dev
, struct in6_addr
*addr
)
940 struct inet6_dev
*idev
= in6_dev_get(dev
);
946 err
= __ipv6_dev_mc_dec(idev
, addr
);
954 * identify MLD packets for MLD filter exceptions
956 int ipv6_is_mld(struct sk_buff
*skb
, int nexthdr
)
958 struct icmp6hdr
*pic
;
960 if (nexthdr
!= IPPROTO_ICMPV6
)
963 if (!pskb_may_pull(skb
, sizeof(struct icmp6hdr
)))
966 pic
= (struct icmp6hdr
*)skb
->h
.raw
;
968 switch (pic
->icmp6_type
) {
969 case ICMPV6_MGM_QUERY
:
970 case ICMPV6_MGM_REPORT
:
971 case ICMPV6_MGM_REDUCTION
:
972 case ICMPV6_MLD2_REPORT
:
981 * check if the interface/address pair is valid
983 int ipv6_chk_mcast_addr(struct net_device
*dev
, struct in6_addr
*group
,
984 struct in6_addr
*src_addr
)
986 struct inet6_dev
*idev
;
987 struct ifmcaddr6
*mc
;
990 idev
= in6_dev_get(dev
);
992 read_lock_bh(&idev
->lock
);
993 for (mc
= idev
->mc_list
; mc
; mc
=mc
->next
) {
994 if (ipv6_addr_equal(&mc
->mca_addr
, group
))
998 if (src_addr
&& !ipv6_addr_any(src_addr
)) {
999 struct ip6_sf_list
*psf
;
1001 spin_lock_bh(&mc
->mca_lock
);
1002 for (psf
=mc
->mca_sources
;psf
;psf
=psf
->sf_next
) {
1003 if (ipv6_addr_equal(&psf
->sf_addr
, src_addr
))
1007 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
1008 psf
->sf_count
[MCAST_EXCLUDE
] !=
1009 mc
->mca_sfcount
[MCAST_EXCLUDE
];
1011 rv
= mc
->mca_sfcount
[MCAST_EXCLUDE
] !=0;
1012 spin_unlock_bh(&mc
->mca_lock
);
1014 rv
= 1; /* don't filter unspecified source */
1016 read_unlock_bh(&idev
->lock
);
1022 static void mld_gq_start_timer(struct inet6_dev
*idev
)
1024 int tv
= net_random() % idev
->mc_maxdelay
;
1026 idev
->mc_gq_running
= 1;
1027 if (!mod_timer(&idev
->mc_gq_timer
, jiffies
+tv
+2))
1031 static void mld_ifc_start_timer(struct inet6_dev
*idev
, int delay
)
1033 int tv
= net_random() % delay
;
1035 if (!mod_timer(&idev
->mc_ifc_timer
, jiffies
+tv
+2))
1040 * IGMP handling (alias multicast ICMPv6 messages)
1043 static void igmp6_group_queried(struct ifmcaddr6
*ma
, unsigned long resptime
)
1045 unsigned long delay
= resptime
;
1047 /* Do not start timer for these addresses */
1048 if (ipv6_addr_is_ll_all_nodes(&ma
->mca_addr
) ||
1049 IPV6_ADDR_MC_SCOPE(&ma
->mca_addr
) < IPV6_ADDR_SCOPE_LINKLOCAL
)
1052 if (del_timer(&ma
->mca_timer
)) {
1053 atomic_dec(&ma
->mca_refcnt
);
1054 delay
= ma
->mca_timer
.expires
- jiffies
;
1057 if (delay
>= resptime
) {
1059 delay
= net_random() % resptime
;
1063 ma
->mca_timer
.expires
= jiffies
+ delay
;
1064 if (!mod_timer(&ma
->mca_timer
, jiffies
+ delay
))
1065 atomic_inc(&ma
->mca_refcnt
);
1066 ma
->mca_flags
|= MAF_TIMER_RUNNING
;
1069 static void mld_marksources(struct ifmcaddr6
*pmc
, int nsrcs
,
1070 struct in6_addr
*srcs
)
1072 struct ip6_sf_list
*psf
;
1076 for (psf
=pmc
->mca_sources
; psf
; psf
=psf
->sf_next
) {
1077 if (scount
== nsrcs
)
1079 for (i
=0; i
<nsrcs
; i
++)
1080 if (ipv6_addr_equal(&srcs
[i
], &psf
->sf_addr
)) {
1088 int igmp6_event_query(struct sk_buff
*skb
)
1090 struct mld2_query
*mlh2
= NULL
;
1091 struct ifmcaddr6
*ma
;
1092 struct in6_addr
*group
;
1093 unsigned long max_delay
;
1094 struct inet6_dev
*idev
;
1095 struct icmp6hdr
*hdr
;
1100 if (!pskb_may_pull(skb
, sizeof(struct in6_addr
)))
1103 /* compute payload length excluding extension headers */
1104 len
= ntohs(skb
->nh
.ipv6h
->payload_len
) + sizeof(struct ipv6hdr
);
1105 len
-= (char *)skb
->h
.raw
- (char *)skb
->nh
.ipv6h
;
1107 /* Drop queries with not link local source */
1108 if (!(ipv6_addr_type(&skb
->nh
.ipv6h
->saddr
)&IPV6_ADDR_LINKLOCAL
))
1111 idev
= in6_dev_get(skb
->dev
);
1116 hdr
= (struct icmp6hdr
*) skb
->h
.raw
;
1117 group
= (struct in6_addr
*) (hdr
+ 1);
1118 group_type
= ipv6_addr_type(group
);
1120 if (group_type
!= IPV6_ADDR_ANY
&&
1121 !(group_type
&IPV6_ADDR_MULTICAST
)) {
1128 /* MLDv1 router present */
1130 /* Translate milliseconds to jiffies */
1131 max_delay
= (ntohs(hdr
->icmp6_maxdelay
)*HZ
)/1000;
1133 switchback
= (idev
->mc_qrv
+ 1) * max_delay
;
1134 idev
->mc_v1_seen
= jiffies
+ switchback
;
1136 /* cancel the interface change timer */
1137 idev
->mc_ifc_count
= 0;
1138 if (del_timer(&idev
->mc_ifc_timer
))
1139 __in6_dev_put(idev
);
1140 /* clear deleted report items */
1141 mld_clear_delrec(idev
);
1142 } else if (len
>= 28) {
1143 int srcs_offset
= sizeof(struct mld2_query
) -
1144 sizeof(struct icmp6hdr
);
1145 if (!pskb_may_pull(skb
, srcs_offset
)) {
1149 mlh2
= (struct mld2_query
*) skb
->h
.raw
;
1150 max_delay
= (MLDV2_MRC(ntohs(mlh2
->mrc
))*HZ
)/1000;
1153 idev
->mc_maxdelay
= max_delay
;
1155 idev
->mc_qrv
= mlh2
->qrv
;
1156 if (group_type
== IPV6_ADDR_ANY
) { /* general query */
1159 return -EINVAL
; /* no sources allowed */
1161 mld_gq_start_timer(idev
);
1165 /* mark sources to include, if group & source-specific */
1166 if (mlh2
->nsrcs
!= 0) {
1167 if (!pskb_may_pull(skb
, srcs_offset
+
1168 mlh2
->nsrcs
* sizeof(struct in6_addr
))) {
1172 mlh2
= (struct mld2_query
*) skb
->h
.raw
;
1180 read_lock_bh(&idev
->lock
);
1181 if (group_type
== IPV6_ADDR_ANY
) {
1182 for (ma
= idev
->mc_list
; ma
; ma
=ma
->next
) {
1183 spin_lock_bh(&ma
->mca_lock
);
1184 igmp6_group_queried(ma
, max_delay
);
1185 spin_unlock_bh(&ma
->mca_lock
);
1188 for (ma
= idev
->mc_list
; ma
; ma
=ma
->next
) {
1189 if (group_type
!= IPV6_ADDR_ANY
&&
1190 !ipv6_addr_equal(group
, &ma
->mca_addr
))
1192 spin_lock_bh(&ma
->mca_lock
);
1193 if (ma
->mca_flags
& MAF_TIMER_RUNNING
) {
1194 /* gsquery <- gsquery && mark */
1196 ma
->mca_flags
&= ~MAF_GSQUERY
;
1198 /* gsquery <- mark */
1200 ma
->mca_flags
|= MAF_GSQUERY
;
1202 ma
->mca_flags
&= ~MAF_GSQUERY
;
1204 if (ma
->mca_flags
& MAF_GSQUERY
)
1205 mld_marksources(ma
, ntohs(mlh2
->nsrcs
),
1207 igmp6_group_queried(ma
, max_delay
);
1208 spin_unlock_bh(&ma
->mca_lock
);
1209 if (group_type
!= IPV6_ADDR_ANY
)
1213 read_unlock_bh(&idev
->lock
);
1220 int igmp6_event_report(struct sk_buff
*skb
)
1222 struct ifmcaddr6
*ma
;
1223 struct in6_addr
*addrp
;
1224 struct inet6_dev
*idev
;
1225 struct icmp6hdr
*hdr
;
1228 /* Our own report looped back. Ignore it. */
1229 if (skb
->pkt_type
== PACKET_LOOPBACK
)
1232 if (!pskb_may_pull(skb
, sizeof(struct in6_addr
)))
1235 hdr
= (struct icmp6hdr
*) skb
->h
.raw
;
1237 /* Drop reports with not link local source */
1238 addr_type
= ipv6_addr_type(&skb
->nh
.ipv6h
->saddr
);
1239 if (addr_type
!= IPV6_ADDR_ANY
&&
1240 !(addr_type
&IPV6_ADDR_LINKLOCAL
))
1243 addrp
= (struct in6_addr
*) (hdr
+ 1);
1245 idev
= in6_dev_get(skb
->dev
);
1250 * Cancel the timer for this group
1253 read_lock_bh(&idev
->lock
);
1254 for (ma
= idev
->mc_list
; ma
; ma
=ma
->next
) {
1255 if (ipv6_addr_equal(&ma
->mca_addr
, addrp
)) {
1256 spin_lock(&ma
->mca_lock
);
1257 if (del_timer(&ma
->mca_timer
))
1258 atomic_dec(&ma
->mca_refcnt
);
1259 ma
->mca_flags
&= ~(MAF_LAST_REPORTER
|MAF_TIMER_RUNNING
);
1260 spin_unlock(&ma
->mca_lock
);
1264 read_unlock_bh(&idev
->lock
);
1269 static int is_in(struct ifmcaddr6
*pmc
, struct ip6_sf_list
*psf
, int type
,
1270 int gdeleted
, int sdeleted
)
1273 case MLD2_MODE_IS_INCLUDE
:
1274 case MLD2_MODE_IS_EXCLUDE
:
1275 if (gdeleted
|| sdeleted
)
1277 return !((pmc
->mca_flags
& MAF_GSQUERY
) && !psf
->sf_gsresp
);
1278 case MLD2_CHANGE_TO_INCLUDE
:
1279 if (gdeleted
|| sdeleted
)
1281 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
1282 case MLD2_CHANGE_TO_EXCLUDE
:
1283 if (gdeleted
|| sdeleted
)
1285 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
] == 0 ||
1286 psf
->sf_count
[MCAST_INCLUDE
])
1288 return pmc
->mca_sfcount
[MCAST_EXCLUDE
] ==
1289 psf
->sf_count
[MCAST_EXCLUDE
];
1290 case MLD2_ALLOW_NEW_SOURCES
:
1291 if (gdeleted
|| !psf
->sf_crcount
)
1293 return (pmc
->mca_sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
1294 case MLD2_BLOCK_OLD_SOURCES
:
1295 if (pmc
->mca_sfmode
== MCAST_INCLUDE
)
1296 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
1297 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
1303 mld_scount(struct ifmcaddr6
*pmc
, int type
, int gdeleted
, int sdeleted
)
1305 struct ip6_sf_list
*psf
;
1308 for (psf
=pmc
->mca_sources
; psf
; psf
=psf
->sf_next
) {
1309 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
1316 static struct sk_buff
*mld_newpack(struct net_device
*dev
, int size
)
1318 struct sock
*sk
= igmp6_socket
->sk
;
1319 struct sk_buff
*skb
;
1320 struct mld2_report
*pmr
;
1321 struct in6_addr addr_buf
;
1323 u8 ra
[8] = { IPPROTO_ICMPV6
, 0,
1324 IPV6_TLV_ROUTERALERT
, 2, 0, 0,
1327 /* we assume size > sizeof(ra) here */
1328 skb
= sock_alloc_send_skb(sk
, size
+ LL_RESERVED_SPACE(dev
), 1, &err
);
1333 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
1335 if (ipv6_get_lladdr(dev
, &addr_buf
)) {
1336 /* <draft-ietf-magma-mld-source-05.txt>:
1337 * use unspecified address as the source address
1338 * when a valid link-local address is not available.
1340 memset(&addr_buf
, 0, sizeof(addr_buf
));
1343 ip6_nd_hdr(sk
, skb
, dev
, &addr_buf
, &mld2_all_mcr
, NEXTHDR_HOP
, 0);
1345 memcpy(skb_put(skb
, sizeof(ra
)), ra
, sizeof(ra
));
1347 pmr
=(struct mld2_report
*)skb_put(skb
, sizeof(*pmr
));
1348 skb
->h
.raw
= (unsigned char *)pmr
;
1349 pmr
->type
= ICMPV6_MLD2_REPORT
;
1357 static inline int mld_dev_queue_xmit2(struct sk_buff
*skb
)
1359 struct net_device
*dev
= skb
->dev
;
1361 if (dev
->hard_header
) {
1362 unsigned char ha
[MAX_ADDR_LEN
];
1365 ndisc_mc_map(&skb
->nh
.ipv6h
->daddr
, ha
, dev
, 1);
1366 err
= dev
->hard_header(skb
, dev
, ETH_P_IPV6
, ha
, NULL
, skb
->len
);
1372 return dev_queue_xmit(skb
);
1375 static inline int mld_dev_queue_xmit(struct sk_buff
*skb
)
1377 return NF_HOOK(PF_INET6
, NF_IP6_POST_ROUTING
, skb
, NULL
, skb
->dev
,
1378 mld_dev_queue_xmit2
);
1381 static void mld_sendpack(struct sk_buff
*skb
)
1383 struct ipv6hdr
*pip6
= skb
->nh
.ipv6h
;
1384 struct mld2_report
*pmr
= (struct mld2_report
*)skb
->h
.raw
;
1385 int payload_len
, mldlen
;
1386 struct inet6_dev
*idev
= in6_dev_get(skb
->dev
);
1389 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS
);
1390 payload_len
= skb
->tail
- (unsigned char *)skb
->nh
.ipv6h
-
1391 sizeof(struct ipv6hdr
);
1392 mldlen
= skb
->tail
- skb
->h
.raw
;
1393 pip6
->payload_len
= htons(payload_len
);
1395 pmr
->csum
= csum_ipv6_magic(&pip6
->saddr
, &pip6
->daddr
, mldlen
,
1396 IPPROTO_ICMPV6
, csum_partial(skb
->h
.raw
, mldlen
, 0));
1397 err
= NF_HOOK(PF_INET6
, NF_IP6_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
1398 mld_dev_queue_xmit
);
1400 ICMP6_INC_STATS(idev
,ICMP6_MIB_OUTMSGS
);
1401 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS
);
1403 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS
);
1405 if (likely(idev
!= NULL
))
1409 static int grec_size(struct ifmcaddr6
*pmc
, int type
, int gdel
, int sdel
)
1411 return sizeof(struct mld2_grec
) + 16 * mld_scount(pmc
,type
,gdel
,sdel
);
1414 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ifmcaddr6
*pmc
,
1415 int type
, struct mld2_grec
**ppgr
)
1417 struct net_device
*dev
= pmc
->idev
->dev
;
1418 struct mld2_report
*pmr
;
1419 struct mld2_grec
*pgr
;
1422 skb
= mld_newpack(dev
, dev
->mtu
);
1425 pgr
= (struct mld2_grec
*)skb_put(skb
, sizeof(struct mld2_grec
));
1426 pgr
->grec_type
= type
;
1427 pgr
->grec_auxwords
= 0;
1428 pgr
->grec_nsrcs
= 0;
1429 pgr
->grec_mca
= pmc
->mca_addr
; /* structure copy */
1430 pmr
= (struct mld2_report
*)skb
->h
.raw
;
1431 pmr
->ngrec
= htons(ntohs(pmr
->ngrec
)+1);
1436 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1437 skb_tailroom(skb)) : 0)
1439 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ifmcaddr6
*pmc
,
1440 int type
, int gdeleted
, int sdeleted
)
1442 struct net_device
*dev
= pmc
->idev
->dev
;
1443 struct mld2_report
*pmr
;
1444 struct mld2_grec
*pgr
= NULL
;
1445 struct ip6_sf_list
*psf
, *psf_next
, *psf_prev
, **psf_list
;
1446 int scount
, first
, isquery
, truncate
;
1448 if (pmc
->mca_flags
& MAF_NOREPORT
)
1451 isquery
= type
== MLD2_MODE_IS_INCLUDE
||
1452 type
== MLD2_MODE_IS_EXCLUDE
;
1453 truncate
= type
== MLD2_MODE_IS_EXCLUDE
||
1454 type
== MLD2_CHANGE_TO_EXCLUDE
;
1456 psf_list
= sdeleted
? &pmc
->mca_tomb
: &pmc
->mca_sources
;
1459 if (type
== MLD2_ALLOW_NEW_SOURCES
||
1460 type
== MLD2_BLOCK_OLD_SOURCES
)
1462 if (pmc
->mca_crcount
|| isquery
) {
1463 /* make sure we have room for group header and at
1466 if (skb
&& AVAILABLE(skb
) < sizeof(struct mld2_grec
)+
1467 sizeof(struct in6_addr
)) {
1469 skb
= NULL
; /* add_grhead will get a new one */
1471 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
1475 pmr
= skb
? (struct mld2_report
*)skb
->h
.raw
: NULL
;
1477 /* EX and TO_EX get a fresh packet, if needed */
1479 if (pmr
&& pmr
->ngrec
&&
1480 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
1483 skb
= mld_newpack(dev
, dev
->mtu
);
1489 for (psf
=*psf_list
; psf
; psf
=psf_next
) {
1490 struct in6_addr
*psrc
;
1492 psf_next
= psf
->sf_next
;
1494 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
)) {
1499 /* clear marks on query responses */
1503 if (AVAILABLE(skb
) < sizeof(*psrc
) +
1504 first
*sizeof(struct mld2_grec
)) {
1505 if (truncate
&& !first
)
1506 break; /* truncate these */
1508 pgr
->grec_nsrcs
= htons(scount
);
1511 skb
= mld_newpack(dev
, dev
->mtu
);
1516 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
1519 psrc
= (struct in6_addr
*)skb_put(skb
, sizeof(*psrc
));
1520 *psrc
= psf
->sf_addr
;
1522 if ((type
== MLD2_ALLOW_NEW_SOURCES
||
1523 type
== MLD2_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
) {
1525 if ((sdeleted
|| gdeleted
) && psf
->sf_crcount
== 0) {
1527 psf_prev
->sf_next
= psf
->sf_next
;
1529 *psf_list
= psf
->sf_next
;
1537 pgr
->grec_nsrcs
= htons(scount
);
1540 pmc
->mca_flags
&= ~MAF_GSQUERY
; /* clear query state */
1544 static void mld_send_report(struct inet6_dev
*idev
, struct ifmcaddr6
*pmc
)
1546 struct sk_buff
*skb
= NULL
;
1550 read_lock_bh(&idev
->lock
);
1551 for (pmc
=idev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1552 if (pmc
->mca_flags
& MAF_NOREPORT
)
1554 spin_lock_bh(&pmc
->mca_lock
);
1555 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
])
1556 type
= MLD2_MODE_IS_EXCLUDE
;
1558 type
= MLD2_MODE_IS_INCLUDE
;
1559 skb
= add_grec(skb
, pmc
, type
, 0, 0);
1560 spin_unlock_bh(&pmc
->mca_lock
);
1562 read_unlock_bh(&idev
->lock
);
1564 spin_lock_bh(&pmc
->mca_lock
);
1565 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
])
1566 type
= MLD2_MODE_IS_EXCLUDE
;
1568 type
= MLD2_MODE_IS_INCLUDE
;
1569 skb
= add_grec(skb
, pmc
, type
, 0, 0);
1570 spin_unlock_bh(&pmc
->mca_lock
);
1577 * remove zero-count source records from a source filter list
1579 static void mld_clear_zeros(struct ip6_sf_list
**ppsf
)
1581 struct ip6_sf_list
*psf_prev
, *psf_next
, *psf
;
1584 for (psf
=*ppsf
; psf
; psf
= psf_next
) {
1585 psf_next
= psf
->sf_next
;
1586 if (psf
->sf_crcount
== 0) {
1588 psf_prev
->sf_next
= psf
->sf_next
;
1590 *ppsf
= psf
->sf_next
;
1597 static void mld_send_cr(struct inet6_dev
*idev
)
1599 struct ifmcaddr6
*pmc
, *pmc_prev
, *pmc_next
;
1600 struct sk_buff
*skb
= NULL
;
1603 read_lock_bh(&idev
->lock
);
1604 write_lock_bh(&idev
->mc_lock
);
1608 for (pmc
=idev
->mc_tomb
; pmc
; pmc
=pmc_next
) {
1609 pmc_next
= pmc
->next
;
1610 if (pmc
->mca_sfmode
== MCAST_INCLUDE
) {
1611 type
= MLD2_BLOCK_OLD_SOURCES
;
1612 dtype
= MLD2_BLOCK_OLD_SOURCES
;
1613 skb
= add_grec(skb
, pmc
, type
, 1, 0);
1614 skb
= add_grec(skb
, pmc
, dtype
, 1, 1);
1616 if (pmc
->mca_crcount
) {
1618 if (pmc
->mca_sfmode
== MCAST_EXCLUDE
) {
1619 type
= MLD2_CHANGE_TO_INCLUDE
;
1620 skb
= add_grec(skb
, pmc
, type
, 1, 0);
1622 if (pmc
->mca_crcount
== 0) {
1623 mld_clear_zeros(&pmc
->mca_tomb
);
1624 mld_clear_zeros(&pmc
->mca_sources
);
1627 if (pmc
->mca_crcount
== 0 && !pmc
->mca_tomb
&&
1628 !pmc
->mca_sources
) {
1630 pmc_prev
->next
= pmc_next
;
1632 idev
->mc_tomb
= pmc_next
;
1633 in6_dev_put(pmc
->idev
);
1638 write_unlock_bh(&idev
->mc_lock
);
1641 for (pmc
=idev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1642 spin_lock_bh(&pmc
->mca_lock
);
1643 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
]) {
1644 type
= MLD2_BLOCK_OLD_SOURCES
;
1645 dtype
= MLD2_ALLOW_NEW_SOURCES
;
1647 type
= MLD2_ALLOW_NEW_SOURCES
;
1648 dtype
= MLD2_BLOCK_OLD_SOURCES
;
1650 skb
= add_grec(skb
, pmc
, type
, 0, 0);
1651 skb
= add_grec(skb
, pmc
, dtype
, 0, 1); /* deleted sources */
1653 /* filter mode changes */
1654 if (pmc
->mca_crcount
) {
1656 if (pmc
->mca_sfmode
== MCAST_EXCLUDE
)
1657 type
= MLD2_CHANGE_TO_EXCLUDE
;
1659 type
= MLD2_CHANGE_TO_INCLUDE
;
1660 skb
= add_grec(skb
, pmc
, type
, 0, 0);
1662 spin_unlock_bh(&pmc
->mca_lock
);
1664 read_unlock_bh(&idev
->lock
);
1667 (void) mld_sendpack(skb
);
1670 static void igmp6_send(struct in6_addr
*addr
, struct net_device
*dev
, int type
)
1672 struct sock
*sk
= igmp6_socket
->sk
;
1673 struct inet6_dev
*idev
;
1674 struct sk_buff
*skb
;
1675 struct icmp6hdr
*hdr
;
1676 struct in6_addr
*snd_addr
;
1677 struct in6_addr
*addrp
;
1678 struct in6_addr addr_buf
;
1679 struct in6_addr all_routers
;
1680 int err
, len
, payload_len
, full_len
;
1681 u8 ra
[8] = { IPPROTO_ICMPV6
, 0,
1682 IPV6_TLV_ROUTERALERT
, 2, 0, 0,
1685 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS
);
1687 if (type
== ICMPV6_MGM_REDUCTION
) {
1688 snd_addr
= &all_routers
;
1689 ipv6_addr_all_routers(&all_routers
);
1692 len
= sizeof(struct icmp6hdr
) + sizeof(struct in6_addr
);
1693 payload_len
= len
+ sizeof(ra
);
1694 full_len
= sizeof(struct ipv6hdr
) + payload_len
;
1696 skb
= sock_alloc_send_skb(sk
, LL_RESERVED_SPACE(dev
) + full_len
, 1, &err
);
1699 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS
);
1703 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
1705 if (ipv6_get_lladdr(dev
, &addr_buf
)) {
1706 /* <draft-ietf-magma-mld-source-05.txt>:
1707 * use unspecified address as the source address
1708 * when a valid link-local address is not available.
1710 memset(&addr_buf
, 0, sizeof(addr_buf
));
1713 ip6_nd_hdr(sk
, skb
, dev
, &addr_buf
, snd_addr
, NEXTHDR_HOP
, payload_len
);
1715 memcpy(skb_put(skb
, sizeof(ra
)), ra
, sizeof(ra
));
1717 hdr
= (struct icmp6hdr
*) skb_put(skb
, sizeof(struct icmp6hdr
));
1718 memset(hdr
, 0, sizeof(struct icmp6hdr
));
1719 hdr
->icmp6_type
= type
;
1721 addrp
= (struct in6_addr
*) skb_put(skb
, sizeof(struct in6_addr
));
1722 ipv6_addr_copy(addrp
, addr
);
1724 hdr
->icmp6_cksum
= csum_ipv6_magic(&addr_buf
, snd_addr
, len
,
1726 csum_partial((__u8
*) hdr
, len
, 0));
1728 idev
= in6_dev_get(skb
->dev
);
1730 err
= NF_HOOK(PF_INET6
, NF_IP6_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
1731 mld_dev_queue_xmit
);
1733 if (type
== ICMPV6_MGM_REDUCTION
)
1734 ICMP6_INC_STATS(idev
, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS
);
1736 ICMP6_INC_STATS(idev
, ICMP6_MIB_OUTGROUPMEMBRESPONSES
);
1737 ICMP6_INC_STATS(idev
, ICMP6_MIB_OUTMSGS
);
1738 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS
);
1740 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS
);
1742 if (likely(idev
!= NULL
))
1747 static int ip6_mc_del1_src(struct ifmcaddr6
*pmc
, int sfmode
,
1748 struct in6_addr
*psfsrc
)
1750 struct ip6_sf_list
*psf
, *psf_prev
;
1754 for (psf
=pmc
->mca_sources
; psf
; psf
=psf
->sf_next
) {
1755 if (ipv6_addr_equal(&psf
->sf_addr
, psfsrc
))
1759 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1760 /* source filter not found, or count wrong => bug */
1763 psf
->sf_count
[sfmode
]--;
1764 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1765 struct inet6_dev
*idev
= pmc
->idev
;
1767 /* no more filters for this source */
1769 psf_prev
->sf_next
= psf
->sf_next
;
1771 pmc
->mca_sources
= psf
->sf_next
;
1772 if (psf
->sf_oldin
&& !(pmc
->mca_flags
& MAF_NOREPORT
) &&
1773 !MLD_V1_SEEN(idev
)) {
1774 psf
->sf_crcount
= idev
->mc_qrv
;
1775 psf
->sf_next
= pmc
->mca_tomb
;
1776 pmc
->mca_tomb
= psf
;
1784 static int ip6_mc_del_src(struct inet6_dev
*idev
, struct in6_addr
*pmca
,
1785 int sfmode
, int sfcount
, struct in6_addr
*psfsrc
,
1788 struct ifmcaddr6
*pmc
;
1794 read_lock_bh(&idev
->lock
);
1795 for (pmc
=idev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1796 if (ipv6_addr_equal(pmca
, &pmc
->mca_addr
))
1800 /* MCA not found?? bug */
1801 read_unlock_bh(&idev
->lock
);
1804 spin_lock_bh(&pmc
->mca_lock
);
1807 if (!pmc
->mca_sfcount
[sfmode
]) {
1808 spin_unlock_bh(&pmc
->mca_lock
);
1809 read_unlock_bh(&idev
->lock
);
1812 pmc
->mca_sfcount
[sfmode
]--;
1815 for (i
=0; i
<sfcount
; i
++) {
1816 int rv
= ip6_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1818 changerec
|= rv
> 0;
1822 if (pmc
->mca_sfmode
== MCAST_EXCLUDE
&&
1823 pmc
->mca_sfcount
[MCAST_EXCLUDE
] == 0 &&
1824 pmc
->mca_sfcount
[MCAST_INCLUDE
]) {
1825 struct ip6_sf_list
*psf
;
1827 /* filter mode change */
1828 pmc
->mca_sfmode
= MCAST_INCLUDE
;
1829 pmc
->mca_crcount
= idev
->mc_qrv
;
1830 idev
->mc_ifc_count
= pmc
->mca_crcount
;
1831 for (psf
=pmc
->mca_sources
; psf
; psf
= psf
->sf_next
)
1832 psf
->sf_crcount
= 0;
1833 mld_ifc_event(pmc
->idev
);
1834 } else if (sf_setstate(pmc
) || changerec
)
1835 mld_ifc_event(pmc
->idev
);
1836 spin_unlock_bh(&pmc
->mca_lock
);
1837 read_unlock_bh(&idev
->lock
);
1842 * Add multicast single-source filter to the interface list
1844 static int ip6_mc_add1_src(struct ifmcaddr6
*pmc
, int sfmode
,
1845 struct in6_addr
*psfsrc
, int delta
)
1847 struct ip6_sf_list
*psf
, *psf_prev
;
1850 for (psf
=pmc
->mca_sources
; psf
; psf
=psf
->sf_next
) {
1851 if (ipv6_addr_equal(&psf
->sf_addr
, psfsrc
))
1856 psf
= (struct ip6_sf_list
*)kmalloc(sizeof(*psf
), GFP_ATOMIC
);
1859 memset(psf
, 0, sizeof(*psf
));
1860 psf
->sf_addr
= *psfsrc
;
1862 psf_prev
->sf_next
= psf
;
1864 pmc
->mca_sources
= psf
;
1866 psf
->sf_count
[sfmode
]++;
1870 static void sf_markstate(struct ifmcaddr6
*pmc
)
1872 struct ip6_sf_list
*psf
;
1873 int mca_xcount
= pmc
->mca_sfcount
[MCAST_EXCLUDE
];
1875 for (psf
=pmc
->mca_sources
; psf
; psf
=psf
->sf_next
)
1876 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
]) {
1877 psf
->sf_oldin
= mca_xcount
==
1878 psf
->sf_count
[MCAST_EXCLUDE
] &&
1879 !psf
->sf_count
[MCAST_INCLUDE
];
1881 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1884 static int sf_setstate(struct ifmcaddr6
*pmc
)
1886 struct ip6_sf_list
*psf
;
1887 int mca_xcount
= pmc
->mca_sfcount
[MCAST_EXCLUDE
];
1888 int qrv
= pmc
->idev
->mc_qrv
;
1892 for (psf
=pmc
->mca_sources
; psf
; psf
=psf
->sf_next
) {
1893 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
]) {
1894 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
1895 !psf
->sf_count
[MCAST_INCLUDE
];
1897 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1898 if (new_in
!= psf
->sf_oldin
) {
1899 psf
->sf_crcount
= qrv
;
1907 * Add multicast source filter list to the interface list
1909 static int ip6_mc_add_src(struct inet6_dev
*idev
, struct in6_addr
*pmca
,
1910 int sfmode
, int sfcount
, struct in6_addr
*psfsrc
,
1913 struct ifmcaddr6
*pmc
;
1919 read_lock_bh(&idev
->lock
);
1920 for (pmc
=idev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1921 if (ipv6_addr_equal(pmca
, &pmc
->mca_addr
))
1925 /* MCA not found?? bug */
1926 read_unlock_bh(&idev
->lock
);
1929 spin_lock_bh(&pmc
->mca_lock
);
1932 isexclude
= pmc
->mca_sfmode
== MCAST_EXCLUDE
;
1934 pmc
->mca_sfcount
[sfmode
]++;
1936 for (i
=0; i
<sfcount
; i
++) {
1937 err
= ip6_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
], delta
);
1945 pmc
->mca_sfcount
[sfmode
]--;
1947 (void) ip6_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1948 } else if (isexclude
!= (pmc
->mca_sfcount
[MCAST_EXCLUDE
] != 0)) {
1949 struct inet6_dev
*idev
= pmc
->idev
;
1950 struct ip6_sf_list
*psf
;
1952 /* filter mode change */
1953 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
])
1954 pmc
->mca_sfmode
= MCAST_EXCLUDE
;
1955 else if (pmc
->mca_sfcount
[MCAST_INCLUDE
])
1956 pmc
->mca_sfmode
= MCAST_INCLUDE
;
1957 /* else no filters; keep old mode for reports */
1959 pmc
->mca_crcount
= idev
->mc_qrv
;
1960 idev
->mc_ifc_count
= pmc
->mca_crcount
;
1961 for (psf
=pmc
->mca_sources
; psf
; psf
= psf
->sf_next
)
1962 psf
->sf_crcount
= 0;
1963 mld_ifc_event(idev
);
1964 } else if (sf_setstate(pmc
))
1965 mld_ifc_event(idev
);
1966 spin_unlock_bh(&pmc
->mca_lock
);
1967 read_unlock_bh(&idev
->lock
);
1971 static void ip6_mc_clear_src(struct ifmcaddr6
*pmc
)
1973 struct ip6_sf_list
*psf
, *nextpsf
;
1975 for (psf
=pmc
->mca_tomb
; psf
; psf
=nextpsf
) {
1976 nextpsf
= psf
->sf_next
;
1979 pmc
->mca_tomb
= NULL
;
1980 for (psf
=pmc
->mca_sources
; psf
; psf
=nextpsf
) {
1981 nextpsf
= psf
->sf_next
;
1984 pmc
->mca_sources
= NULL
;
1985 pmc
->mca_sfmode
= MCAST_EXCLUDE
;
1986 pmc
->mca_sfcount
[MCAST_INCLUDE
] = 0;
1987 pmc
->mca_sfcount
[MCAST_EXCLUDE
] = 1;
1991 static void igmp6_join_group(struct ifmcaddr6
*ma
)
1993 unsigned long delay
;
1995 if (ma
->mca_flags
& MAF_NOREPORT
)
1998 igmp6_send(&ma
->mca_addr
, ma
->idev
->dev
, ICMPV6_MGM_REPORT
);
2000 delay
= net_random() % IGMP6_UNSOLICITED_IVAL
;
2002 spin_lock_bh(&ma
->mca_lock
);
2003 if (del_timer(&ma
->mca_timer
)) {
2004 atomic_dec(&ma
->mca_refcnt
);
2005 delay
= ma
->mca_timer
.expires
- jiffies
;
2008 if (!mod_timer(&ma
->mca_timer
, jiffies
+ delay
))
2009 atomic_inc(&ma
->mca_refcnt
);
2010 ma
->mca_flags
|= MAF_TIMER_RUNNING
| MAF_LAST_REPORTER
;
2011 spin_unlock_bh(&ma
->mca_lock
);
2014 static int ip6_mc_leave_src(struct sock
*sk
, struct ipv6_mc_socklist
*iml
,
2015 struct inet6_dev
*idev
)
2019 if (iml
->sflist
== 0) {
2020 /* any-source empty exclude case */
2021 return ip6_mc_del_src(idev
, &iml
->addr
, iml
->sfmode
, 0, NULL
, 0);
2023 err
= ip6_mc_del_src(idev
, &iml
->addr
, iml
->sfmode
,
2024 iml
->sflist
->sl_count
, iml
->sflist
->sl_addr
, 0);
2025 sock_kfree_s(sk
, iml
->sflist
, IP6_SFLSIZE(iml
->sflist
->sl_max
));
2030 static void igmp6_leave_group(struct ifmcaddr6
*ma
)
2032 if (MLD_V1_SEEN(ma
->idev
)) {
2033 if (ma
->mca_flags
& MAF_LAST_REPORTER
)
2034 igmp6_send(&ma
->mca_addr
, ma
->idev
->dev
,
2035 ICMPV6_MGM_REDUCTION
);
2037 mld_add_delrec(ma
->idev
, ma
);
2038 mld_ifc_event(ma
->idev
);
2042 static void mld_gq_timer_expire(unsigned long data
)
2044 struct inet6_dev
*idev
= (struct inet6_dev
*)data
;
2046 idev
->mc_gq_running
= 0;
2047 mld_send_report(idev
, NULL
);
2048 __in6_dev_put(idev
);
2051 static void mld_ifc_timer_expire(unsigned long data
)
2053 struct inet6_dev
*idev
= (struct inet6_dev
*)data
;
2056 if (idev
->mc_ifc_count
) {
2057 idev
->mc_ifc_count
--;
2058 if (idev
->mc_ifc_count
)
2059 mld_ifc_start_timer(idev
, idev
->mc_maxdelay
);
2061 __in6_dev_put(idev
);
2064 static void mld_ifc_event(struct inet6_dev
*idev
)
2066 if (MLD_V1_SEEN(idev
))
2068 idev
->mc_ifc_count
= idev
->mc_qrv
;
2069 mld_ifc_start_timer(idev
, 1);
2073 static void igmp6_timer_handler(unsigned long data
)
2075 struct ifmcaddr6
*ma
= (struct ifmcaddr6
*) data
;
2077 if (MLD_V1_SEEN(ma
->idev
))
2078 igmp6_send(&ma
->mca_addr
, ma
->idev
->dev
, ICMPV6_MGM_REPORT
);
2080 mld_send_report(ma
->idev
, ma
);
2082 spin_lock(&ma
->mca_lock
);
2083 ma
->mca_flags
|= MAF_LAST_REPORTER
;
2084 ma
->mca_flags
&= ~MAF_TIMER_RUNNING
;
2085 spin_unlock(&ma
->mca_lock
);
2089 /* Device going down */
2091 void ipv6_mc_down(struct inet6_dev
*idev
)
2093 struct ifmcaddr6
*i
;
2095 /* Withdraw multicast list */
2097 read_lock_bh(&idev
->lock
);
2098 idev
->mc_ifc_count
= 0;
2099 if (del_timer(&idev
->mc_ifc_timer
))
2100 __in6_dev_put(idev
);
2101 idev
->mc_gq_running
= 0;
2102 if (del_timer(&idev
->mc_gq_timer
))
2103 __in6_dev_put(idev
);
2105 for (i
= idev
->mc_list
; i
; i
=i
->next
)
2106 igmp6_group_dropped(i
);
2107 read_unlock_bh(&idev
->lock
);
2109 mld_clear_delrec(idev
);
2113 /* Device going up */
2115 void ipv6_mc_up(struct inet6_dev
*idev
)
2117 struct ifmcaddr6
*i
;
2119 /* Install multicast list, except for all-nodes (already installed) */
2121 read_lock_bh(&idev
->lock
);
2122 for (i
= idev
->mc_list
; i
; i
=i
->next
)
2123 igmp6_group_added(i
);
2124 read_unlock_bh(&idev
->lock
);
2127 /* IPv6 device initialization. */
2129 void ipv6_mc_init_dev(struct inet6_dev
*idev
)
2131 struct in6_addr maddr
;
2133 write_lock_bh(&idev
->lock
);
2134 rwlock_init(&idev
->mc_lock
);
2135 idev
->mc_gq_running
= 0;
2136 init_timer(&idev
->mc_gq_timer
);
2137 idev
->mc_gq_timer
.data
= (unsigned long) idev
;
2138 idev
->mc_gq_timer
.function
= &mld_gq_timer_expire
;
2139 idev
->mc_tomb
= NULL
;
2140 idev
->mc_ifc_count
= 0;
2141 init_timer(&idev
->mc_ifc_timer
);
2142 idev
->mc_ifc_timer
.data
= (unsigned long) idev
;
2143 idev
->mc_ifc_timer
.function
= &mld_ifc_timer_expire
;
2144 idev
->mc_qrv
= MLD_QRV_DEFAULT
;
2145 idev
->mc_maxdelay
= IGMP6_UNSOLICITED_IVAL
;
2146 idev
->mc_v1_seen
= 0;
2147 write_unlock_bh(&idev
->lock
);
2149 /* Add all-nodes address. */
2150 ipv6_addr_all_nodes(&maddr
);
2151 ipv6_dev_mc_inc(idev
->dev
, &maddr
);
2155 * Device is about to be destroyed: clean up.
2158 void ipv6_mc_destroy_dev(struct inet6_dev
*idev
)
2160 struct ifmcaddr6
*i
;
2161 struct in6_addr maddr
;
2163 /* Deactivate timers */
2166 /* Delete all-nodes address. */
2167 ipv6_addr_all_nodes(&maddr
);
2169 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2170 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2173 __ipv6_dev_mc_dec(idev
, &maddr
);
2175 if (idev
->cnf
.forwarding
) {
2176 ipv6_addr_all_routers(&maddr
);
2177 __ipv6_dev_mc_dec(idev
, &maddr
);
2180 write_lock_bh(&idev
->lock
);
2181 while ((i
= idev
->mc_list
) != NULL
) {
2182 idev
->mc_list
= i
->next
;
2183 write_unlock_bh(&idev
->lock
);
2185 igmp6_group_dropped(i
);
2188 write_lock_bh(&idev
->lock
);
2190 write_unlock_bh(&idev
->lock
);
2193 #ifdef CONFIG_PROC_FS
2194 struct igmp6_mc_iter_state
{
2195 struct net_device
*dev
;
2196 struct inet6_dev
*idev
;
2199 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2201 static inline struct ifmcaddr6
*igmp6_mc_get_first(struct seq_file
*seq
)
2203 struct ifmcaddr6
*im
= NULL
;
2204 struct igmp6_mc_iter_state
*state
= igmp6_mc_seq_private(seq
);
2206 for (state
->dev
= dev_base
, state
->idev
= NULL
;
2208 state
->dev
= state
->dev
->next
) {
2209 struct inet6_dev
*idev
;
2210 idev
= in6_dev_get(state
->dev
);
2213 read_lock_bh(&idev
->lock
);
2219 read_unlock_bh(&idev
->lock
);
2225 static struct ifmcaddr6
*igmp6_mc_get_next(struct seq_file
*seq
, struct ifmcaddr6
*im
)
2227 struct igmp6_mc_iter_state
*state
= igmp6_mc_seq_private(seq
);
2231 if (likely(state
->idev
!= NULL
)) {
2232 read_unlock_bh(&state
->idev
->lock
);
2233 in6_dev_put(state
->idev
);
2235 state
->dev
= state
->dev
->next
;
2240 state
->idev
= in6_dev_get(state
->dev
);
2243 read_lock_bh(&state
->idev
->lock
);
2244 im
= state
->idev
->mc_list
;
2249 static struct ifmcaddr6
*igmp6_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2251 struct ifmcaddr6
*im
= igmp6_mc_get_first(seq
);
2253 while (pos
&& (im
= igmp6_mc_get_next(seq
, im
)) != NULL
)
2255 return pos
? NULL
: im
;
2258 static void *igmp6_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2260 read_lock(&dev_base_lock
);
2261 return igmp6_mc_get_idx(seq
, *pos
);
2264 static void *igmp6_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2266 struct ifmcaddr6
*im
;
2267 im
= igmp6_mc_get_next(seq
, v
);
2272 static void igmp6_mc_seq_stop(struct seq_file
*seq
, void *v
)
2274 struct igmp6_mc_iter_state
*state
= igmp6_mc_seq_private(seq
);
2275 if (likely(state
->idev
!= NULL
)) {
2276 read_unlock_bh(&state
->idev
->lock
);
2277 in6_dev_put(state
->idev
);
2281 read_unlock(&dev_base_lock
);
2284 static int igmp6_mc_seq_show(struct seq_file
*seq
, void *v
)
2286 struct ifmcaddr6
*im
= (struct ifmcaddr6
*)v
;
2287 struct igmp6_mc_iter_state
*state
= igmp6_mc_seq_private(seq
);
2290 "%-4d %-15s %04x%04x%04x%04x%04x%04x%04x%04x %5d %08X %ld\n",
2291 state
->dev
->ifindex
, state
->dev
->name
,
2293 im
->mca_users
, im
->mca_flags
,
2294 (im
->mca_flags
&MAF_TIMER_RUNNING
) ?
2295 jiffies_to_clock_t(im
->mca_timer
.expires
-jiffies
) : 0);
2299 static struct seq_operations igmp6_mc_seq_ops
= {
2300 .start
= igmp6_mc_seq_start
,
2301 .next
= igmp6_mc_seq_next
,
2302 .stop
= igmp6_mc_seq_stop
,
2303 .show
= igmp6_mc_seq_show
,
2306 static int igmp6_mc_seq_open(struct inode
*inode
, struct file
*file
)
2308 struct seq_file
*seq
;
2310 struct igmp6_mc_iter_state
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2315 rc
= seq_open(file
, &igmp6_mc_seq_ops
);
2319 seq
= file
->private_data
;
2321 memset(s
, 0, sizeof(*s
));
2329 static struct file_operations igmp6_mc_seq_fops
= {
2330 .owner
= THIS_MODULE
,
2331 .open
= igmp6_mc_seq_open
,
2333 .llseek
= seq_lseek
,
2334 .release
= seq_release_private
,
2337 struct igmp6_mcf_iter_state
{
2338 struct net_device
*dev
;
2339 struct inet6_dev
*idev
;
2340 struct ifmcaddr6
*im
;
2343 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2345 static inline struct ip6_sf_list
*igmp6_mcf_get_first(struct seq_file
*seq
)
2347 struct ip6_sf_list
*psf
= NULL
;
2348 struct ifmcaddr6
*im
= NULL
;
2349 struct igmp6_mcf_iter_state
*state
= igmp6_mcf_seq_private(seq
);
2351 for (state
->dev
= dev_base
, state
->idev
= NULL
, state
->im
= NULL
;
2353 state
->dev
= state
->dev
->next
) {
2354 struct inet6_dev
*idev
;
2355 idev
= in6_dev_get(state
->dev
);
2356 if (unlikely(idev
== NULL
))
2358 read_lock_bh(&idev
->lock
);
2360 if (likely(im
!= NULL
)) {
2361 spin_lock_bh(&im
->mca_lock
);
2362 psf
= im
->mca_sources
;
2363 if (likely(psf
!= NULL
)) {
2368 spin_unlock_bh(&im
->mca_lock
);
2370 read_unlock_bh(&idev
->lock
);
2376 static struct ip6_sf_list
*igmp6_mcf_get_next(struct seq_file
*seq
, struct ip6_sf_list
*psf
)
2378 struct igmp6_mcf_iter_state
*state
= igmp6_mcf_seq_private(seq
);
2382 spin_unlock_bh(&state
->im
->mca_lock
);
2383 state
->im
= state
->im
->next
;
2384 while (!state
->im
) {
2385 if (likely(state
->idev
!= NULL
)) {
2386 read_unlock_bh(&state
->idev
->lock
);
2387 in6_dev_put(state
->idev
);
2389 state
->dev
= state
->dev
->next
;
2394 state
->idev
= in6_dev_get(state
->dev
);
2397 read_lock_bh(&state
->idev
->lock
);
2398 state
->im
= state
->idev
->mc_list
;
2402 spin_lock_bh(&state
->im
->mca_lock
);
2403 psf
= state
->im
->mca_sources
;
2409 static struct ip6_sf_list
*igmp6_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2411 struct ip6_sf_list
*psf
= igmp6_mcf_get_first(seq
);
2413 while (pos
&& (psf
= igmp6_mcf_get_next(seq
, psf
)) != NULL
)
2415 return pos
? NULL
: psf
;
2418 static void *igmp6_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2420 read_lock(&dev_base_lock
);
2421 return *pos
? igmp6_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2424 static void *igmp6_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2426 struct ip6_sf_list
*psf
;
2427 if (v
== SEQ_START_TOKEN
)
2428 psf
= igmp6_mcf_get_first(seq
);
2430 psf
= igmp6_mcf_get_next(seq
, v
);
2435 static void igmp6_mcf_seq_stop(struct seq_file
*seq
, void *v
)
2437 struct igmp6_mcf_iter_state
*state
= igmp6_mcf_seq_private(seq
);
2438 if (likely(state
->im
!= NULL
)) {
2439 spin_unlock_bh(&state
->im
->mca_lock
);
2442 if (likely(state
->idev
!= NULL
)) {
2443 read_unlock_bh(&state
->idev
->lock
);
2444 in6_dev_put(state
->idev
);
2448 read_unlock(&dev_base_lock
);
2451 static int igmp6_mcf_seq_show(struct seq_file
*seq
, void *v
)
2453 struct ip6_sf_list
*psf
= (struct ip6_sf_list
*)v
;
2454 struct igmp6_mcf_iter_state
*state
= igmp6_mcf_seq_private(seq
);
2456 if (v
== SEQ_START_TOKEN
) {
2459 "%32s %32s %6s %6s\n", "Idx",
2460 "Device", "Multicast Address",
2461 "Source Address", "INC", "EXC");
2465 "%04x%04x%04x%04x%04x%04x%04x%04x "
2466 "%04x%04x%04x%04x%04x%04x%04x%04x "
2468 state
->dev
->ifindex
, state
->dev
->name
,
2469 NIP6(state
->im
->mca_addr
),
2471 psf
->sf_count
[MCAST_INCLUDE
],
2472 psf
->sf_count
[MCAST_EXCLUDE
]);
2477 static struct seq_operations igmp6_mcf_seq_ops
= {
2478 .start
= igmp6_mcf_seq_start
,
2479 .next
= igmp6_mcf_seq_next
,
2480 .stop
= igmp6_mcf_seq_stop
,
2481 .show
= igmp6_mcf_seq_show
,
2484 static int igmp6_mcf_seq_open(struct inode
*inode
, struct file
*file
)
2486 struct seq_file
*seq
;
2488 struct igmp6_mcf_iter_state
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2493 rc
= seq_open(file
, &igmp6_mcf_seq_ops
);
2497 seq
= file
->private_data
;
2499 memset(s
, 0, sizeof(*s
));
2507 static struct file_operations igmp6_mcf_seq_fops
= {
2508 .owner
= THIS_MODULE
,
2509 .open
= igmp6_mcf_seq_open
,
2511 .llseek
= seq_lseek
,
2512 .release
= seq_release_private
,
2516 int __init
igmp6_init(struct net_proto_family
*ops
)
2518 struct ipv6_pinfo
*np
;
2522 err
= sock_create_kern(PF_INET6
, SOCK_RAW
, IPPROTO_ICMPV6
, &igmp6_socket
);
2525 "Failed to initialize the IGMP6 control socket (err %d).\n",
2527 igmp6_socket
= NULL
; /* For safety. */
2531 sk
= igmp6_socket
->sk
;
2532 sk
->sk_allocation
= GFP_ATOMIC
;
2533 sk
->sk_prot
->unhash(sk
);
2538 #ifdef CONFIG_PROC_FS
2539 proc_net_fops_create("igmp6", S_IRUGO
, &igmp6_mc_seq_fops
);
2540 proc_net_fops_create("mcfilter6", S_IRUGO
, &igmp6_mcf_seq_fops
);
2546 void igmp6_cleanup(void)
2548 sock_release(igmp6_socket
);
2549 igmp6_socket
= NULL
; /* for safety */
2551 #ifdef CONFIG_PROC_FS
2552 proc_net_remove("mcfilter6");
2553 proc_net_remove("igmp6");