1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Multicast support for IPv6
4 * Linux INET6 implementation
7 * Pedro Roque <roque@di.fc.ul.pt>
9 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
14 * yoshfuji : fix format of router-alert option
15 * YOSHIFUJI Hideaki @USAGI:
16 * Fixed source address for MLD message based on
17 * <draft-ietf-magma-mld-source-05.txt>.
18 * YOSHIFUJI Hideaki @USAGI:
19 * - Ignore Queries for invalid addresses.
20 * - MLD for link-local addresses.
21 * David L Stevens <dlstevens@us.ibm.com>:
25 #include <linux/module.h>
26 #include <linux/errno.h>
27 #include <linux/types.h>
28 #include <linux/string.h>
29 #include <linux/socket.h>
30 #include <linux/sockios.h>
31 #include <linux/jiffies.h>
32 #include <linux/net.h>
34 #include <linux/in6.h>
35 #include <linux/netdevice.h>
36 #include <linux/if_addr.h>
37 #include <linux/if_arp.h>
38 #include <linux/route.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/init.h>
41 #include <linux/proc_fs.h>
42 #include <linux/seq_file.h>
43 #include <linux/slab.h>
44 #include <linux/pkt_sched.h>
46 #include <linux/workqueue.h>
48 #include <linux/netfilter.h>
49 #include <linux/netfilter_ipv6.h>
51 #include <net/net_namespace.h>
52 #include <net/netlink.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>
62 #include <net/inet_common.h>
64 #include <net/ip6_checksum.h>
66 /* Ensure that we have struct in6_addr aligned on 32bit word. */
67 static int __mld2_query_bugs
[] __attribute__((__unused__
)) = {
68 BUILD_BUG_ON_ZERO(offsetof(struct mld2_query
, mld2q_srcs
) % 4),
69 BUILD_BUG_ON_ZERO(offsetof(struct mld2_report
, mld2r_grec
) % 4),
70 BUILD_BUG_ON_ZERO(offsetof(struct mld2_grec
, grec_mca
) % 4)
73 static struct workqueue_struct
*mld_wq
;
74 static struct in6_addr mld2_all_mcr
= MLD2_ALL_MCR_INIT
;
76 static void igmp6_join_group(struct ifmcaddr6
*ma
);
77 static void igmp6_leave_group(struct ifmcaddr6
*ma
);
78 static void mld_mca_work(struct work_struct
*work
);
80 static void mld_ifc_event(struct inet6_dev
*idev
);
81 static bool mld_in_v1_mode(const struct inet6_dev
*idev
);
82 static int sf_setstate(struct ifmcaddr6
*pmc
);
83 static void sf_markstate(struct ifmcaddr6
*pmc
);
84 static void ip6_mc_clear_src(struct ifmcaddr6
*pmc
);
85 static int ip6_mc_del_src(struct inet6_dev
*idev
, const struct in6_addr
*pmca
,
86 int sfmode
, int sfcount
, const struct in6_addr
*psfsrc
,
88 static int ip6_mc_add_src(struct inet6_dev
*idev
, const struct in6_addr
*pmca
,
89 int sfmode
, int sfcount
, const struct in6_addr
*psfsrc
,
91 static int ip6_mc_leave_src(struct sock
*sk
, struct ipv6_mc_socklist
*iml
,
92 struct inet6_dev
*idev
);
93 static int __ipv6_dev_mc_inc(struct net_device
*dev
,
94 const struct in6_addr
*addr
, unsigned int mode
);
96 #define MLD_QRV_DEFAULT 2
97 /* RFC3810, 9.2. Query Interval */
98 #define MLD_QI_DEFAULT (125 * HZ)
99 /* RFC3810, 9.3. Query Response Interval */
100 #define MLD_QRI_DEFAULT (10 * HZ)
102 /* RFC3810, 8.1 Query Version Distinctions */
103 #define MLD_V1_QUERY_LEN 24
104 #define MLD_V2_QUERY_LEN_MIN 28
106 #define IPV6_MLD_MAX_MSF 64
108 int sysctl_mld_max_msf __read_mostly
= IPV6_MLD_MAX_MSF
;
109 int sysctl_mld_qrv __read_mostly
= MLD_QRV_DEFAULT
;
112 * socket join on multicast group
114 #define mc_dereference(e, idev) \
115 rcu_dereference_protected(e, lockdep_is_held(&(idev)->mc_lock))
117 #define sock_dereference(e, sk) \
118 rcu_dereference_protected(e, lockdep_sock_is_held(sk))
120 #define for_each_pmc_socklock(np, sk, pmc) \
121 for (pmc = sock_dereference((np)->ipv6_mc_list, sk); \
123 pmc = sock_dereference(pmc->next, sk))
125 #define for_each_pmc_rcu(np, pmc) \
126 for (pmc = rcu_dereference((np)->ipv6_mc_list); \
128 pmc = rcu_dereference(pmc->next))
130 #define for_each_psf_mclock(mc, psf) \
131 for (psf = mc_dereference((mc)->mca_sources, mc->idev); \
133 psf = mc_dereference(psf->sf_next, mc->idev))
135 #define for_each_psf_rcu(mc, psf) \
136 for (psf = rcu_dereference((mc)->mca_sources); \
138 psf = rcu_dereference(psf->sf_next))
140 #define for_each_psf_tomb(mc, psf) \
141 for (psf = mc_dereference((mc)->mca_tomb, mc->idev); \
143 psf = mc_dereference(psf->sf_next, mc->idev))
145 #define for_each_mc_mclock(idev, mc) \
146 for (mc = mc_dereference((idev)->mc_list, idev); \
148 mc = mc_dereference(mc->next, idev))
150 #define for_each_mc_rcu(idev, mc) \
151 for (mc = rcu_dereference((idev)->mc_list); \
153 mc = rcu_dereference(mc->next))
155 #define for_each_mc_tomb(idev, mc) \
156 for (mc = mc_dereference((idev)->mc_tomb, idev); \
158 mc = mc_dereference(mc->next, idev))
160 static int unsolicited_report_interval(struct inet6_dev
*idev
)
164 if (mld_in_v1_mode(idev
))
165 iv
= READ_ONCE(idev
->cnf
.mldv1_unsolicited_report_interval
);
167 iv
= READ_ONCE(idev
->cnf
.mldv2_unsolicited_report_interval
);
169 return iv
> 0 ? iv
: 1;
172 static int __ipv6_sock_mc_join(struct sock
*sk
, int ifindex
,
173 const struct in6_addr
*addr
, unsigned int mode
)
175 struct net_device
*dev
= NULL
;
176 struct ipv6_mc_socklist
*mc_lst
;
177 struct ipv6_pinfo
*np
= inet6_sk(sk
);
178 struct net
*net
= sock_net(sk
);
183 if (!ipv6_addr_is_multicast(addr
))
186 for_each_pmc_socklock(np
, sk
, mc_lst
) {
187 if ((ifindex
== 0 || mc_lst
->ifindex
== ifindex
) &&
188 ipv6_addr_equal(&mc_lst
->addr
, addr
))
192 mc_lst
= sock_kmalloc(sk
, sizeof(struct ipv6_mc_socklist
), GFP_KERNEL
);
198 mc_lst
->addr
= *addr
;
202 rt
= rt6_lookup(net
, addr
, NULL
, 0, NULL
, 0);
208 dev
= __dev_get_by_index(net
, ifindex
);
211 sock_kfree_s(sk
, mc_lst
, sizeof(*mc_lst
));
215 mc_lst
->ifindex
= dev
->ifindex
;
216 mc_lst
->sfmode
= mode
;
217 RCU_INIT_POINTER(mc_lst
->sflist
, NULL
);
220 * now add/increase the group membership on the device
223 err
= __ipv6_dev_mc_inc(dev
, addr
, mode
);
226 sock_kfree_s(sk
, mc_lst
, sizeof(*mc_lst
));
230 mc_lst
->next
= np
->ipv6_mc_list
;
231 rcu_assign_pointer(np
->ipv6_mc_list
, mc_lst
);
236 int ipv6_sock_mc_join(struct sock
*sk
, int ifindex
, const struct in6_addr
*addr
)
238 return __ipv6_sock_mc_join(sk
, ifindex
, addr
, MCAST_EXCLUDE
);
240 EXPORT_SYMBOL(ipv6_sock_mc_join
);
242 int ipv6_sock_mc_join_ssm(struct sock
*sk
, int ifindex
,
243 const struct in6_addr
*addr
, unsigned int mode
)
245 return __ipv6_sock_mc_join(sk
, ifindex
, addr
, mode
);
249 * socket leave on multicast group
251 int ipv6_sock_mc_drop(struct sock
*sk
, int ifindex
, const struct in6_addr
*addr
)
253 struct ipv6_pinfo
*np
= inet6_sk(sk
);
254 struct ipv6_mc_socklist
*mc_lst
;
255 struct ipv6_mc_socklist __rcu
**lnk
;
256 struct net
*net
= sock_net(sk
);
260 if (!ipv6_addr_is_multicast(addr
))
263 for (lnk
= &np
->ipv6_mc_list
;
264 (mc_lst
= sock_dereference(*lnk
, sk
)) != NULL
;
265 lnk
= &mc_lst
->next
) {
266 if ((ifindex
== 0 || mc_lst
->ifindex
== ifindex
) &&
267 ipv6_addr_equal(&mc_lst
->addr
, addr
)) {
268 struct net_device
*dev
;
272 dev
= __dev_get_by_index(net
, mc_lst
->ifindex
);
274 struct inet6_dev
*idev
= __in6_dev_get(dev
);
276 ip6_mc_leave_src(sk
, mc_lst
, idev
);
278 __ipv6_dev_mc_dec(idev
, &mc_lst
->addr
);
280 ip6_mc_leave_src(sk
, mc_lst
, NULL
);
283 atomic_sub(sizeof(*mc_lst
), &sk
->sk_omem_alloc
);
284 kfree_rcu(mc_lst
, rcu
);
289 return -EADDRNOTAVAIL
;
291 EXPORT_SYMBOL(ipv6_sock_mc_drop
);
293 static struct inet6_dev
*ip6_mc_find_dev_rtnl(struct net
*net
,
294 const struct in6_addr
*group
,
297 struct net_device
*dev
= NULL
;
298 struct inet6_dev
*idev
= NULL
;
301 struct rt6_info
*rt
= rt6_lookup(net
, group
, NULL
, 0, NULL
, 0);
308 dev
= __dev_get_by_index(net
, ifindex
);
313 idev
= __in6_dev_get(dev
);
321 void __ipv6_sock_mc_close(struct sock
*sk
)
323 struct ipv6_pinfo
*np
= inet6_sk(sk
);
324 struct ipv6_mc_socklist
*mc_lst
;
325 struct net
*net
= sock_net(sk
);
329 while ((mc_lst
= sock_dereference(np
->ipv6_mc_list
, sk
)) != NULL
) {
330 struct net_device
*dev
;
332 np
->ipv6_mc_list
= mc_lst
->next
;
334 dev
= __dev_get_by_index(net
, mc_lst
->ifindex
);
336 struct inet6_dev
*idev
= __in6_dev_get(dev
);
338 ip6_mc_leave_src(sk
, mc_lst
, idev
);
340 __ipv6_dev_mc_dec(idev
, &mc_lst
->addr
);
342 ip6_mc_leave_src(sk
, mc_lst
, NULL
);
345 atomic_sub(sizeof(*mc_lst
), &sk
->sk_omem_alloc
);
346 kfree_rcu(mc_lst
, rcu
);
350 void ipv6_sock_mc_close(struct sock
*sk
)
352 struct ipv6_pinfo
*np
= inet6_sk(sk
);
354 if (!rcu_access_pointer(np
->ipv6_mc_list
))
359 __ipv6_sock_mc_close(sk
);
364 int ip6_mc_source(int add
, int omode
, struct sock
*sk
,
365 struct group_source_req
*pgsr
)
367 struct in6_addr
*source
, *group
;
368 struct ipv6_mc_socklist
*pmc
;
369 struct inet6_dev
*idev
;
370 struct ipv6_pinfo
*inet6
= inet6_sk(sk
);
371 struct ip6_sf_socklist
*psl
;
372 struct net
*net
= sock_net(sk
);
377 source
= &((struct sockaddr_in6
*)&pgsr
->gsr_source
)->sin6_addr
;
378 group
= &((struct sockaddr_in6
*)&pgsr
->gsr_group
)->sin6_addr
;
380 if (!ipv6_addr_is_multicast(group
))
383 idev
= ip6_mc_find_dev_rtnl(net
, group
, pgsr
->gsr_interface
);
387 err
= -EADDRNOTAVAIL
;
389 mutex_lock(&idev
->mc_lock
);
390 for_each_pmc_socklock(inet6
, sk
, pmc
) {
391 if (pgsr
->gsr_interface
&& pmc
->ifindex
!= pgsr
->gsr_interface
)
393 if (ipv6_addr_equal(&pmc
->addr
, group
))
396 if (!pmc
) { /* must have a prior join */
400 /* if a source filter was set, must be the same mode as before */
401 if (rcu_access_pointer(pmc
->sflist
)) {
402 if (pmc
->sfmode
!= omode
) {
406 } else if (pmc
->sfmode
!= omode
) {
407 /* allow mode switches for empty-set filters */
408 ip6_mc_add_src(idev
, group
, omode
, 0, NULL
, 0);
409 ip6_mc_del_src(idev
, group
, pmc
->sfmode
, 0, NULL
, 0);
413 psl
= sock_dereference(pmc
->sflist
, sk
);
416 goto done
; /* err = -EADDRNOTAVAIL */
418 for (i
= 0; i
< psl
->sl_count
; i
++) {
419 rv
= !ipv6_addr_equal(&psl
->sl_addr
[i
], source
);
423 if (rv
) /* source not found */
424 goto done
; /* err = -EADDRNOTAVAIL */
426 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
427 if (psl
->sl_count
== 1 && omode
== MCAST_INCLUDE
) {
432 /* update the interface filter */
433 ip6_mc_del_src(idev
, group
, omode
, 1, source
, 1);
435 for (j
= i
+1; j
< psl
->sl_count
; j
++)
436 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
441 /* else, add a new source to the filter */
443 if (psl
&& psl
->sl_count
>= sysctl_mld_max_msf
) {
447 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
448 struct ip6_sf_socklist
*newpsl
;
449 int count
= IP6_SFBLOCK
;
452 count
+= psl
->sl_max
;
453 newpsl
= sock_kmalloc(sk
, struct_size(newpsl
, sl_addr
, count
),
459 newpsl
->sl_max
= count
;
460 newpsl
->sl_count
= count
- IP6_SFBLOCK
;
462 for (i
= 0; i
< psl
->sl_count
; i
++)
463 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
464 atomic_sub(struct_size(psl
, sl_addr
, psl
->sl_max
),
467 rcu_assign_pointer(pmc
->sflist
, newpsl
);
471 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
472 for (i
= 0; i
< psl
->sl_count
; i
++) {
473 rv
= !ipv6_addr_equal(&psl
->sl_addr
[i
], source
);
474 if (rv
== 0) /* There is an error in the address. */
477 for (j
= psl
->sl_count
-1; j
>= i
; j
--)
478 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
479 psl
->sl_addr
[i
] = *source
;
482 /* update the interface list */
483 ip6_mc_add_src(idev
, group
, omode
, 1, source
, 1);
485 mutex_unlock(&idev
->mc_lock
);
487 err
= ipv6_sock_mc_drop(sk
, pgsr
->gsr_interface
, group
);
491 int ip6_mc_msfilter(struct sock
*sk
, struct group_filter
*gsf
,
492 struct sockaddr_storage
*list
)
494 const struct in6_addr
*group
;
495 struct ipv6_mc_socklist
*pmc
;
496 struct inet6_dev
*idev
;
497 struct ipv6_pinfo
*inet6
= inet6_sk(sk
);
498 struct ip6_sf_socklist
*newpsl
, *psl
;
499 struct net
*net
= sock_net(sk
);
503 group
= &((struct sockaddr_in6
*)&gsf
->gf_group
)->sin6_addr
;
505 if (!ipv6_addr_is_multicast(group
))
507 if (gsf
->gf_fmode
!= MCAST_INCLUDE
&&
508 gsf
->gf_fmode
!= MCAST_EXCLUDE
)
511 idev
= ip6_mc_find_dev_rtnl(net
, group
, gsf
->gf_interface
);
517 if (gsf
->gf_fmode
== MCAST_INCLUDE
&& gsf
->gf_numsrc
== 0) {
522 for_each_pmc_socklock(inet6
, sk
, pmc
) {
523 if (pmc
->ifindex
!= gsf
->gf_interface
)
525 if (ipv6_addr_equal(&pmc
->addr
, group
))
528 if (!pmc
) { /* must have a prior join */
532 if (gsf
->gf_numsrc
) {
533 newpsl
= sock_kmalloc(sk
, struct_size(newpsl
, sl_addr
,
540 newpsl
->sl_max
= newpsl
->sl_count
= gsf
->gf_numsrc
;
541 for (i
= 0; i
< newpsl
->sl_count
; ++i
, ++list
) {
542 struct sockaddr_in6
*psin6
;
544 psin6
= (struct sockaddr_in6
*)list
;
545 newpsl
->sl_addr
[i
] = psin6
->sin6_addr
;
547 mutex_lock(&idev
->mc_lock
);
548 err
= ip6_mc_add_src(idev
, group
, gsf
->gf_fmode
,
549 newpsl
->sl_count
, newpsl
->sl_addr
, 0);
551 mutex_unlock(&idev
->mc_lock
);
552 sock_kfree_s(sk
, newpsl
, struct_size(newpsl
, sl_addr
,
556 mutex_unlock(&idev
->mc_lock
);
559 mutex_lock(&idev
->mc_lock
);
560 ip6_mc_add_src(idev
, group
, gsf
->gf_fmode
, 0, NULL
, 0);
561 mutex_unlock(&idev
->mc_lock
);
564 mutex_lock(&idev
->mc_lock
);
565 psl
= sock_dereference(pmc
->sflist
, sk
);
567 ip6_mc_del_src(idev
, group
, pmc
->sfmode
,
568 psl
->sl_count
, psl
->sl_addr
, 0);
569 atomic_sub(struct_size(psl
, sl_addr
, psl
->sl_max
),
572 ip6_mc_del_src(idev
, group
, pmc
->sfmode
, 0, NULL
, 0);
574 rcu_assign_pointer(pmc
->sflist
, newpsl
);
575 mutex_unlock(&idev
->mc_lock
);
577 pmc
->sfmode
= gsf
->gf_fmode
;
581 err
= ipv6_sock_mc_drop(sk
, gsf
->gf_interface
, group
);
585 int ip6_mc_msfget(struct sock
*sk
, struct group_filter
*gsf
,
586 sockptr_t optval
, size_t ss_offset
)
588 struct ipv6_pinfo
*inet6
= inet6_sk(sk
);
589 const struct in6_addr
*group
;
590 struct ipv6_mc_socklist
*pmc
;
591 struct ip6_sf_socklist
*psl
;
595 group
= &((struct sockaddr_in6
*)&gsf
->gf_group
)->sin6_addr
;
597 if (!ipv6_addr_is_multicast(group
))
600 /* changes to the ipv6_mc_list require the socket lock and
601 * rtnl lock. We have the socket lock, so reading the list is safe.
604 for_each_pmc_socklock(inet6
, sk
, pmc
) {
605 if (pmc
->ifindex
!= gsf
->gf_interface
)
607 if (ipv6_addr_equal(group
, &pmc
->addr
))
610 if (!pmc
) /* must have a prior join */
611 return -EADDRNOTAVAIL
;
613 gsf
->gf_fmode
= pmc
->sfmode
;
614 psl
= sock_dereference(pmc
->sflist
, sk
);
615 count
= psl
? psl
->sl_count
: 0;
617 copycount
= min(count
, gsf
->gf_numsrc
);
618 gsf
->gf_numsrc
= count
;
619 for (i
= 0; i
< copycount
; i
++) {
620 struct sockaddr_in6
*psin6
;
621 struct sockaddr_storage ss
;
623 psin6
= (struct sockaddr_in6
*)&ss
;
624 memset(&ss
, 0, sizeof(ss
));
625 psin6
->sin6_family
= AF_INET6
;
626 psin6
->sin6_addr
= psl
->sl_addr
[i
];
627 if (copy_to_sockptr_offset(optval
, ss_offset
, &ss
, sizeof(ss
)))
629 ss_offset
+= sizeof(ss
);
634 bool inet6_mc_check(const struct sock
*sk
, const struct in6_addr
*mc_addr
,
635 const struct in6_addr
*src_addr
)
637 const struct ipv6_pinfo
*np
= inet6_sk(sk
);
638 const struct ipv6_mc_socklist
*mc
;
639 const struct ip6_sf_socklist
*psl
;
643 for_each_pmc_rcu(np
, mc
) {
644 if (ipv6_addr_equal(&mc
->addr
, mc_addr
))
649 return inet6_test_bit(MC6_ALL
, sk
);
651 psl
= rcu_dereference(mc
->sflist
);
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
)
671 /* called with mc_lock */
672 static void igmp6_group_added(struct ifmcaddr6
*mc
)
674 struct net_device
*dev
= mc
->idev
->dev
;
675 char buf
[MAX_ADDR_LEN
];
677 if (IPV6_ADDR_MC_SCOPE(&mc
->mca_addr
) <
678 IPV6_ADDR_SCOPE_LINKLOCAL
)
681 if (!(mc
->mca_flags
&MAF_LOADED
)) {
682 mc
->mca_flags
|= MAF_LOADED
;
683 if (ndisc_mc_map(&mc
->mca_addr
, buf
, dev
, 0) == 0)
684 dev_mc_add(dev
, buf
);
687 if (!(dev
->flags
& IFF_UP
) || (mc
->mca_flags
& MAF_NOREPORT
))
690 if (mld_in_v1_mode(mc
->idev
)) {
691 igmp6_join_group(mc
);
696 /* Based on RFC3810 6.1, for newly added INCLUDE SSM, we
697 * should not send filter-mode change record as the mode
698 * should be from IN() to IN(A).
700 if (mc
->mca_sfmode
== MCAST_EXCLUDE
)
701 mc
->mca_crcount
= mc
->idev
->mc_qrv
;
703 mld_ifc_event(mc
->idev
);
706 /* called with mc_lock */
707 static void igmp6_group_dropped(struct ifmcaddr6
*mc
)
709 struct net_device
*dev
= mc
->idev
->dev
;
710 char buf
[MAX_ADDR_LEN
];
712 if (IPV6_ADDR_MC_SCOPE(&mc
->mca_addr
) <
713 IPV6_ADDR_SCOPE_LINKLOCAL
)
716 if (mc
->mca_flags
&MAF_LOADED
) {
717 mc
->mca_flags
&= ~MAF_LOADED
;
718 if (ndisc_mc_map(&mc
->mca_addr
, buf
, dev
, 0) == 0)
719 dev_mc_del(dev
, buf
);
722 if (mc
->mca_flags
& MAF_NOREPORT
)
726 igmp6_leave_group(mc
);
728 if (cancel_delayed_work(&mc
->mca_work
))
729 refcount_dec(&mc
->mca_refcnt
);
733 * deleted ifmcaddr6 manipulation
734 * called with mc_lock
736 static void mld_add_delrec(struct inet6_dev
*idev
, struct ifmcaddr6
*im
)
738 struct ifmcaddr6
*pmc
;
740 /* this is an "ifmcaddr6" for convenience; only the fields below
741 * are actually used. In particular, the refcnt and users are not
742 * used for management of the delete list. Using the same structure
743 * for deleted items allows change reports to use common code with
744 * non-deleted or query-response MCA's.
746 pmc
= kzalloc(sizeof(*pmc
), GFP_KERNEL
);
750 pmc
->idev
= im
->idev
;
752 pmc
->mca_addr
= im
->mca_addr
;
753 pmc
->mca_crcount
= idev
->mc_qrv
;
754 pmc
->mca_sfmode
= im
->mca_sfmode
;
755 if (pmc
->mca_sfmode
== MCAST_INCLUDE
) {
756 struct ip6_sf_list
*psf
;
758 rcu_assign_pointer(pmc
->mca_tomb
,
759 mc_dereference(im
->mca_tomb
, idev
));
760 rcu_assign_pointer(pmc
->mca_sources
,
761 mc_dereference(im
->mca_sources
, idev
));
762 RCU_INIT_POINTER(im
->mca_tomb
, NULL
);
763 RCU_INIT_POINTER(im
->mca_sources
, NULL
);
765 for_each_psf_mclock(pmc
, psf
)
766 psf
->sf_crcount
= pmc
->mca_crcount
;
769 rcu_assign_pointer(pmc
->next
, idev
->mc_tomb
);
770 rcu_assign_pointer(idev
->mc_tomb
, pmc
);
773 /* called with mc_lock */
774 static void mld_del_delrec(struct inet6_dev
*idev
, struct ifmcaddr6
*im
)
776 struct ip6_sf_list
*psf
, *sources
, *tomb
;
777 struct in6_addr
*pmca
= &im
->mca_addr
;
778 struct ifmcaddr6
*pmc
, *pmc_prev
;
781 for_each_mc_tomb(idev
, pmc
) {
782 if (ipv6_addr_equal(&pmc
->mca_addr
, pmca
))
788 rcu_assign_pointer(pmc_prev
->next
, pmc
->next
);
790 rcu_assign_pointer(idev
->mc_tomb
, pmc
->next
);
794 im
->idev
= pmc
->idev
;
795 if (im
->mca_sfmode
== MCAST_INCLUDE
) {
796 tomb
= rcu_replace_pointer(im
->mca_tomb
,
797 mc_dereference(pmc
->mca_tomb
, pmc
->idev
),
798 lockdep_is_held(&im
->idev
->mc_lock
));
799 rcu_assign_pointer(pmc
->mca_tomb
, tomb
);
801 sources
= rcu_replace_pointer(im
->mca_sources
,
802 mc_dereference(pmc
->mca_sources
, pmc
->idev
),
803 lockdep_is_held(&im
->idev
->mc_lock
));
804 rcu_assign_pointer(pmc
->mca_sources
, sources
);
805 for_each_psf_mclock(im
, psf
)
806 psf
->sf_crcount
= idev
->mc_qrv
;
808 im
->mca_crcount
= idev
->mc_qrv
;
810 in6_dev_put(pmc
->idev
);
811 ip6_mc_clear_src(pmc
);
816 /* called with mc_lock */
817 static void mld_clear_delrec(struct inet6_dev
*idev
)
819 struct ifmcaddr6
*pmc
, *nextpmc
;
821 pmc
= mc_dereference(idev
->mc_tomb
, idev
);
822 RCU_INIT_POINTER(idev
->mc_tomb
, NULL
);
824 for (; pmc
; pmc
= nextpmc
) {
825 nextpmc
= mc_dereference(pmc
->next
, idev
);
826 ip6_mc_clear_src(pmc
);
827 in6_dev_put(pmc
->idev
);
831 /* clear dead sources, too */
832 for_each_mc_mclock(idev
, pmc
) {
833 struct ip6_sf_list
*psf
, *psf_next
;
835 psf
= mc_dereference(pmc
->mca_tomb
, idev
);
836 RCU_INIT_POINTER(pmc
->mca_tomb
, NULL
);
837 for (; psf
; psf
= psf_next
) {
838 psf_next
= mc_dereference(psf
->sf_next
, idev
);
844 static void mld_clear_query(struct inet6_dev
*idev
)
848 spin_lock_bh(&idev
->mc_query_lock
);
849 while ((skb
= __skb_dequeue(&idev
->mc_query_queue
)))
851 spin_unlock_bh(&idev
->mc_query_lock
);
854 static void mld_clear_report(struct inet6_dev
*idev
)
858 spin_lock_bh(&idev
->mc_report_lock
);
859 while ((skb
= __skb_dequeue(&idev
->mc_report_queue
)))
861 spin_unlock_bh(&idev
->mc_report_lock
);
864 static void mca_get(struct ifmcaddr6
*mc
)
866 refcount_inc(&mc
->mca_refcnt
);
869 static void ma_put(struct ifmcaddr6
*mc
)
871 if (refcount_dec_and_test(&mc
->mca_refcnt
)) {
872 in6_dev_put(mc
->idev
);
877 /* called with mc_lock */
878 static struct ifmcaddr6
*mca_alloc(struct inet6_dev
*idev
,
879 const struct in6_addr
*addr
,
882 struct ifmcaddr6
*mc
;
884 mc
= kzalloc(sizeof(*mc
), GFP_KERNEL
);
888 INIT_DELAYED_WORK(&mc
->mca_work
, mld_mca_work
);
890 mc
->mca_addr
= *addr
;
891 mc
->idev
= idev
; /* reference taken by caller */
893 /* mca_stamp should be updated upon changes */
894 mc
->mca_cstamp
= mc
->mca_tstamp
= jiffies
;
895 refcount_set(&mc
->mca_refcnt
, 1);
897 mc
->mca_sfmode
= mode
;
898 mc
->mca_sfcount
[mode
] = 1;
900 if (ipv6_addr_is_ll_all_nodes(&mc
->mca_addr
) ||
901 IPV6_ADDR_MC_SCOPE(&mc
->mca_addr
) < IPV6_ADDR_SCOPE_LINKLOCAL
)
902 mc
->mca_flags
|= MAF_NOREPORT
;
907 static void inet6_ifmcaddr_notify(struct net_device
*dev
,
908 const struct ifmcaddr6
*ifmca
, int event
)
910 struct inet6_fill_args fillargs
= {
916 .force_rt_scope_universe
= true,
918 struct net
*net
= dev_net(dev
);
922 skb
= nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg
)) +
923 nla_total_size(sizeof(struct in6_addr
)) +
924 nla_total_size(sizeof(struct ifa_cacheinfo
)),
929 err
= inet6_fill_ifmcaddr(skb
, ifmca
, &fillargs
);
931 WARN_ON_ONCE(err
== -EMSGSIZE
);
936 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_MCADDR
, NULL
, GFP_KERNEL
);
939 rtnl_set_sk_err(net
, RTNLGRP_IPV6_MCADDR
, err
);
943 * device multicast group inc (add if not found)
945 static int __ipv6_dev_mc_inc(struct net_device
*dev
,
946 const struct in6_addr
*addr
, unsigned int mode
)
948 struct ifmcaddr6
*mc
;
949 struct inet6_dev
*idev
;
953 /* we need to take a reference on idev */
954 idev
= in6_dev_get(dev
);
964 mutex_lock(&idev
->mc_lock
);
965 for_each_mc_mclock(idev
, mc
) {
966 if (ipv6_addr_equal(&mc
->mca_addr
, addr
)) {
968 ip6_mc_add_src(idev
, &mc
->mca_addr
, mode
, 0, NULL
, 0);
969 mutex_unlock(&idev
->mc_lock
);
975 mc
= mca_alloc(idev
, addr
, mode
);
977 mutex_unlock(&idev
->mc_lock
);
982 rcu_assign_pointer(mc
->next
, idev
->mc_list
);
983 rcu_assign_pointer(idev
->mc_list
, mc
);
987 mld_del_delrec(idev
, mc
);
988 igmp6_group_added(mc
);
989 inet6_ifmcaddr_notify(dev
, mc
, RTM_NEWMULTICAST
);
990 mutex_unlock(&idev
->mc_lock
);
995 int ipv6_dev_mc_inc(struct net_device
*dev
, const struct in6_addr
*addr
)
997 return __ipv6_dev_mc_inc(dev
, addr
, MCAST_EXCLUDE
);
999 EXPORT_SYMBOL(ipv6_dev_mc_inc
);
1002 * device multicast group del
1004 int __ipv6_dev_mc_dec(struct inet6_dev
*idev
, const struct in6_addr
*addr
)
1006 struct ifmcaddr6
*ma
, __rcu
**map
;
1010 mutex_lock(&idev
->mc_lock
);
1011 for (map
= &idev
->mc_list
;
1012 (ma
= mc_dereference(*map
, idev
));
1014 if (ipv6_addr_equal(&ma
->mca_addr
, addr
)) {
1015 if (--ma
->mca_users
== 0) {
1018 igmp6_group_dropped(ma
);
1019 inet6_ifmcaddr_notify(idev
->dev
, ma
,
1021 ip6_mc_clear_src(ma
);
1022 mutex_unlock(&idev
->mc_lock
);
1027 mutex_unlock(&idev
->mc_lock
);
1032 mutex_unlock(&idev
->mc_lock
);
1036 int ipv6_dev_mc_dec(struct net_device
*dev
, const struct in6_addr
*addr
)
1038 struct inet6_dev
*idev
;
1043 idev
= __in6_dev_get(dev
);
1047 err
= __ipv6_dev_mc_dec(idev
, addr
);
1051 EXPORT_SYMBOL(ipv6_dev_mc_dec
);
1054 * check if the interface/address pair is valid
1056 bool ipv6_chk_mcast_addr(struct net_device
*dev
, const struct in6_addr
*group
,
1057 const struct in6_addr
*src_addr
)
1059 struct inet6_dev
*idev
;
1060 struct ifmcaddr6
*mc
;
1064 idev
= __in6_dev_get(dev
);
1067 for_each_mc_rcu(idev
, mc
) {
1068 if (ipv6_addr_equal(&mc
->mca_addr
, group
))
1073 if (src_addr
&& !ipv6_addr_any(src_addr
)) {
1074 struct ip6_sf_list
*psf
;
1076 for_each_psf_rcu(mc
, psf
) {
1077 if (ipv6_addr_equal(&psf
->sf_addr
, src_addr
))
1081 rv
= READ_ONCE(psf
->sf_count
[MCAST_INCLUDE
]) ||
1082 READ_ONCE(psf
->sf_count
[MCAST_EXCLUDE
]) !=
1083 READ_ONCE(mc
->mca_sfcount
[MCAST_EXCLUDE
]);
1085 rv
= READ_ONCE(mc
->mca_sfcount
[MCAST_EXCLUDE
]) != 0;
1087 rv
= true; /* don't filter unspecified source */
1094 /* called with mc_lock */
1095 static void mld_gq_start_work(struct inet6_dev
*idev
)
1097 unsigned long tv
= get_random_u32_below(idev
->mc_maxdelay
);
1099 idev
->mc_gq_running
= 1;
1100 if (!mod_delayed_work(mld_wq
, &idev
->mc_gq_work
, tv
+ 2))
1104 /* called with mc_lock */
1105 static void mld_gq_stop_work(struct inet6_dev
*idev
)
1107 idev
->mc_gq_running
= 0;
1108 if (cancel_delayed_work(&idev
->mc_gq_work
))
1109 __in6_dev_put(idev
);
1112 /* called with mc_lock */
1113 static void mld_ifc_start_work(struct inet6_dev
*idev
, unsigned long delay
)
1115 unsigned long tv
= get_random_u32_below(delay
);
1117 if (!mod_delayed_work(mld_wq
, &idev
->mc_ifc_work
, tv
+ 2))
1121 /* called with mc_lock */
1122 static void mld_ifc_stop_work(struct inet6_dev
*idev
)
1124 idev
->mc_ifc_count
= 0;
1125 if (cancel_delayed_work(&idev
->mc_ifc_work
))
1126 __in6_dev_put(idev
);
1129 /* called with mc_lock */
1130 static void mld_dad_start_work(struct inet6_dev
*idev
, unsigned long delay
)
1132 unsigned long tv
= get_random_u32_below(delay
);
1134 if (!mod_delayed_work(mld_wq
, &idev
->mc_dad_work
, tv
+ 2))
1138 static void mld_dad_stop_work(struct inet6_dev
*idev
)
1140 if (cancel_delayed_work(&idev
->mc_dad_work
))
1141 __in6_dev_put(idev
);
1144 static void mld_query_stop_work(struct inet6_dev
*idev
)
1146 spin_lock_bh(&idev
->mc_query_lock
);
1147 if (cancel_delayed_work(&idev
->mc_query_work
))
1148 __in6_dev_put(idev
);
1149 spin_unlock_bh(&idev
->mc_query_lock
);
1152 static void mld_report_stop_work(struct inet6_dev
*idev
)
1154 if (cancel_delayed_work_sync(&idev
->mc_report_work
))
1155 __in6_dev_put(idev
);
1159 * IGMP handling (alias multicast ICMPv6 messages)
1160 * called with mc_lock
1162 static void igmp6_group_queried(struct ifmcaddr6
*ma
, unsigned long resptime
)
1164 unsigned long delay
= resptime
;
1166 /* Do not start work for these addresses */
1167 if (ipv6_addr_is_ll_all_nodes(&ma
->mca_addr
) ||
1168 IPV6_ADDR_MC_SCOPE(&ma
->mca_addr
) < IPV6_ADDR_SCOPE_LINKLOCAL
)
1171 if (cancel_delayed_work(&ma
->mca_work
)) {
1172 refcount_dec(&ma
->mca_refcnt
);
1173 delay
= ma
->mca_work
.timer
.expires
- jiffies
;
1176 if (delay
>= resptime
)
1177 delay
= get_random_u32_below(resptime
);
1179 if (!mod_delayed_work(mld_wq
, &ma
->mca_work
, delay
))
1180 refcount_inc(&ma
->mca_refcnt
);
1181 ma
->mca_flags
|= MAF_TIMER_RUNNING
;
1184 /* mark EXCLUDE-mode sources
1185 * called with mc_lock
1187 static bool mld_xmarksources(struct ifmcaddr6
*pmc
, int nsrcs
,
1188 const struct in6_addr
*srcs
)
1190 struct ip6_sf_list
*psf
;
1194 for_each_psf_mclock(pmc
, psf
) {
1195 if (scount
== nsrcs
)
1197 for (i
= 0; i
< nsrcs
; i
++) {
1198 /* skip inactive filters */
1199 if (psf
->sf_count
[MCAST_INCLUDE
] ||
1200 pmc
->mca_sfcount
[MCAST_EXCLUDE
] !=
1201 psf
->sf_count
[MCAST_EXCLUDE
])
1203 if (ipv6_addr_equal(&srcs
[i
], &psf
->sf_addr
)) {
1209 pmc
->mca_flags
&= ~MAF_GSQUERY
;
1210 if (scount
== nsrcs
) /* all sources excluded */
1215 /* called with mc_lock */
1216 static bool mld_marksources(struct ifmcaddr6
*pmc
, int nsrcs
,
1217 const struct in6_addr
*srcs
)
1219 struct ip6_sf_list
*psf
;
1222 if (pmc
->mca_sfmode
== MCAST_EXCLUDE
)
1223 return mld_xmarksources(pmc
, nsrcs
, srcs
);
1225 /* mark INCLUDE-mode sources */
1228 for_each_psf_mclock(pmc
, psf
) {
1229 if (scount
== nsrcs
)
1231 for (i
= 0; i
< nsrcs
; i
++) {
1232 if (ipv6_addr_equal(&srcs
[i
], &psf
->sf_addr
)) {
1240 pmc
->mca_flags
&= ~MAF_GSQUERY
;
1243 pmc
->mca_flags
|= MAF_GSQUERY
;
1247 static int mld_force_mld_version(const struct inet6_dev
*idev
)
1249 const struct net
*net
= dev_net(idev
->dev
);
1252 all_force
= READ_ONCE(net
->ipv6
.devconf_all
->force_mld_version
);
1253 /* Normally, both are 0 here. If enforcement to a particular is
1254 * being used, individual device enforcement will have a lower
1255 * precedence over 'all' device (.../conf/all/force_mld_version).
1257 return all_force
?: READ_ONCE(idev
->cnf
.force_mld_version
);
1260 static bool mld_in_v2_mode_only(const struct inet6_dev
*idev
)
1262 return mld_force_mld_version(idev
) == 2;
1265 static bool mld_in_v1_mode_only(const struct inet6_dev
*idev
)
1267 return mld_force_mld_version(idev
) == 1;
1270 static bool mld_in_v1_mode(const struct inet6_dev
*idev
)
1272 if (mld_in_v2_mode_only(idev
))
1274 if (mld_in_v1_mode_only(idev
))
1276 if (idev
->mc_v1_seen
&& time_before(jiffies
, idev
->mc_v1_seen
))
1282 static void mld_set_v1_mode(struct inet6_dev
*idev
)
1284 /* RFC3810, relevant sections:
1285 * - 9.1. Robustness Variable
1286 * - 9.2. Query Interval
1287 * - 9.3. Query Response Interval
1288 * - 9.12. Older Version Querier Present Timeout
1290 unsigned long switchback
;
1292 switchback
= (idev
->mc_qrv
* idev
->mc_qi
) + idev
->mc_qri
;
1294 idev
->mc_v1_seen
= jiffies
+ switchback
;
1297 static void mld_update_qrv(struct inet6_dev
*idev
,
1298 const struct mld2_query
*mlh2
)
1300 /* RFC3810, relevant sections:
1301 * - 5.1.8. QRV (Querier's Robustness Variable)
1302 * - 9.1. Robustness Variable
1305 /* The value of the Robustness Variable MUST NOT be zero,
1306 * and SHOULD NOT be one. Catch this here if we ever run
1307 * into such a case in future.
1309 const int min_qrv
= min(MLD_QRV_DEFAULT
, sysctl_mld_qrv
);
1310 WARN_ON(idev
->mc_qrv
== 0);
1312 if (mlh2
->mld2q_qrv
> 0)
1313 idev
->mc_qrv
= mlh2
->mld2q_qrv
;
1315 if (unlikely(idev
->mc_qrv
< min_qrv
)) {
1316 net_warn_ratelimited("IPv6: MLD: clamping QRV from %u to %u!\n",
1317 idev
->mc_qrv
, min_qrv
);
1318 idev
->mc_qrv
= min_qrv
;
1322 static void mld_update_qi(struct inet6_dev
*idev
,
1323 const struct mld2_query
*mlh2
)
1325 /* RFC3810, relevant sections:
1326 * - 5.1.9. QQIC (Querier's Query Interval Code)
1327 * - 9.2. Query Interval
1328 * - 9.12. Older Version Querier Present Timeout
1329 * (the [Query Interval] in the last Query received)
1331 unsigned long mc_qqi
;
1333 if (mlh2
->mld2q_qqic
< 128) {
1334 mc_qqi
= mlh2
->mld2q_qqic
;
1336 unsigned long mc_man
, mc_exp
;
1338 mc_exp
= MLDV2_QQIC_EXP(mlh2
->mld2q_qqic
);
1339 mc_man
= MLDV2_QQIC_MAN(mlh2
->mld2q_qqic
);
1341 mc_qqi
= (mc_man
| 0x10) << (mc_exp
+ 3);
1344 idev
->mc_qi
= mc_qqi
* HZ
;
1347 static void mld_update_qri(struct inet6_dev
*idev
,
1348 const struct mld2_query
*mlh2
)
1350 /* RFC3810, relevant sections:
1351 * - 5.1.3. Maximum Response Code
1352 * - 9.3. Query Response Interval
1354 idev
->mc_qri
= msecs_to_jiffies(mldv2_mrc(mlh2
));
1357 static int mld_process_v1(struct inet6_dev
*idev
, struct mld_msg
*mld
,
1358 unsigned long *max_delay
, bool v1_query
)
1360 unsigned long mldv1_md
;
1362 /* Ignore v1 queries */
1363 if (mld_in_v2_mode_only(idev
))
1366 mldv1_md
= ntohs(mld
->mld_maxdelay
);
1368 /* When in MLDv1 fallback and a MLDv2 router start-up being
1369 * unaware of current MLDv1 operation, the MRC == MRD mapping
1370 * only works when the exponential algorithm is not being
1371 * used (as MLDv1 is unaware of such things).
1373 * According to the RFC author, the MLDv2 implementations
1374 * he's aware of all use a MRC < 32768 on start up queries.
1376 * Thus, should we *ever* encounter something else larger
1377 * than that, just assume the maximum possible within our
1381 mldv1_md
= min(mldv1_md
, MLDV1_MRD_MAX_COMPAT
);
1383 *max_delay
= max(msecs_to_jiffies(mldv1_md
), 1UL);
1385 /* MLDv1 router present: we need to go into v1 mode *only*
1386 * when an MLDv1 query is received as per section 9.12. of
1387 * RFC3810! And we know from RFC2710 section 3.7 that MLDv1
1388 * queries MUST be of exactly 24 octets.
1391 mld_set_v1_mode(idev
);
1393 /* cancel MLDv2 report work */
1394 mld_gq_stop_work(idev
);
1395 /* cancel the interface change work */
1396 mld_ifc_stop_work(idev
);
1397 /* clear deleted report items */
1398 mld_clear_delrec(idev
);
1403 static void mld_process_v2(struct inet6_dev
*idev
, struct mld2_query
*mld
,
1404 unsigned long *max_delay
)
1406 *max_delay
= max(msecs_to_jiffies(mldv2_mrc(mld
)), 1UL);
1408 mld_update_qrv(idev
, mld
);
1409 mld_update_qi(idev
, mld
);
1410 mld_update_qri(idev
, mld
);
1412 idev
->mc_maxdelay
= *max_delay
;
1417 /* called with rcu_read_lock() */
1418 void igmp6_event_query(struct sk_buff
*skb
)
1420 struct inet6_dev
*idev
= __in6_dev_get(skb
->dev
);
1422 if (!idev
|| idev
->dead
)
1425 spin_lock_bh(&idev
->mc_query_lock
);
1426 if (skb_queue_len(&idev
->mc_query_queue
) < MLD_MAX_SKBS
) {
1427 __skb_queue_tail(&idev
->mc_query_queue
, skb
);
1428 if (!mod_delayed_work(mld_wq
, &idev
->mc_query_work
, 0))
1432 spin_unlock_bh(&idev
->mc_query_lock
);
1437 static void __mld_query_work(struct sk_buff
*skb
)
1439 struct mld2_query
*mlh2
= NULL
;
1440 const struct in6_addr
*group
;
1441 unsigned long max_delay
;
1442 struct inet6_dev
*idev
;
1443 struct ifmcaddr6
*ma
;
1444 struct mld_msg
*mld
;
1449 if (!pskb_may_pull(skb
, sizeof(struct in6_addr
)))
1452 /* compute payload length excluding extension headers */
1453 len
= ntohs(ipv6_hdr(skb
)->payload_len
) + sizeof(struct ipv6hdr
);
1454 len
-= skb_network_header_len(skb
);
1457 * Upon reception of an MLD message that contains a Query, the node
1458 * checks if the source address of the message is a valid link-local
1459 * address, if the Hop Limit is set to 1, and if the Router Alert
1460 * option is present in the Hop-By-Hop Options header of the IPv6
1461 * packet. If any of these checks fails, the packet is dropped.
1463 if (!(ipv6_addr_type(&ipv6_hdr(skb
)->saddr
) & IPV6_ADDR_LINKLOCAL
) ||
1464 ipv6_hdr(skb
)->hop_limit
!= 1 ||
1465 !(IP6CB(skb
)->flags
& IP6SKB_ROUTERALERT
) ||
1466 IP6CB(skb
)->ra
!= htons(IPV6_OPT_ROUTERALERT_MLD
))
1469 idev
= in6_dev_get(skb
->dev
);
1473 mld
= (struct mld_msg
*)icmp6_hdr(skb
);
1474 group
= &mld
->mld_mca
;
1475 group_type
= ipv6_addr_type(group
);
1477 if (group_type
!= IPV6_ADDR_ANY
&&
1478 !(group_type
&IPV6_ADDR_MULTICAST
))
1481 if (len
< MLD_V1_QUERY_LEN
) {
1483 } else if (len
== MLD_V1_QUERY_LEN
|| mld_in_v1_mode(idev
)) {
1484 err
= mld_process_v1(idev
, mld
, &max_delay
,
1485 len
== MLD_V1_QUERY_LEN
);
1488 } else if (len
>= MLD_V2_QUERY_LEN_MIN
) {
1489 int srcs_offset
= sizeof(struct mld2_query
) -
1490 sizeof(struct icmp6hdr
);
1492 if (!pskb_may_pull(skb
, srcs_offset
))
1495 mlh2
= (struct mld2_query
*)skb_transport_header(skb
);
1497 mld_process_v2(idev
, mlh2
, &max_delay
);
1499 if (group_type
== IPV6_ADDR_ANY
) { /* general query */
1500 if (mlh2
->mld2q_nsrcs
)
1501 goto out
; /* no sources allowed */
1503 mld_gq_start_work(idev
);
1506 /* mark sources to include, if group & source-specific */
1507 if (mlh2
->mld2q_nsrcs
!= 0) {
1508 if (!pskb_may_pull(skb
, srcs_offset
+
1509 ntohs(mlh2
->mld2q_nsrcs
) * sizeof(struct in6_addr
)))
1512 mlh2
= (struct mld2_query
*)skb_transport_header(skb
);
1519 if (group_type
== IPV6_ADDR_ANY
) {
1520 for_each_mc_mclock(idev
, ma
) {
1521 igmp6_group_queried(ma
, max_delay
);
1524 for_each_mc_mclock(idev
, ma
) {
1525 if (!ipv6_addr_equal(group
, &ma
->mca_addr
))
1527 if (ma
->mca_flags
& MAF_TIMER_RUNNING
) {
1528 /* gsquery <- gsquery && mark */
1530 ma
->mca_flags
&= ~MAF_GSQUERY
;
1532 /* gsquery <- mark */
1534 ma
->mca_flags
|= MAF_GSQUERY
;
1536 ma
->mca_flags
&= ~MAF_GSQUERY
;
1538 if (!(ma
->mca_flags
& MAF_GSQUERY
) ||
1539 mld_marksources(ma
, ntohs(mlh2
->mld2q_nsrcs
), mlh2
->mld2q_srcs
))
1540 igmp6_group_queried(ma
, max_delay
);
1551 static void mld_query_work(struct work_struct
*work
)
1553 struct inet6_dev
*idev
= container_of(to_delayed_work(work
),
1556 struct sk_buff_head q
;
1557 struct sk_buff
*skb
;
1558 bool rework
= false;
1561 skb_queue_head_init(&q
);
1563 spin_lock_bh(&idev
->mc_query_lock
);
1564 while ((skb
= __skb_dequeue(&idev
->mc_query_queue
))) {
1565 __skb_queue_tail(&q
, skb
);
1567 if (++cnt
>= MLD_MAX_QUEUE
) {
1572 spin_unlock_bh(&idev
->mc_query_lock
);
1574 mutex_lock(&idev
->mc_lock
);
1575 while ((skb
= __skb_dequeue(&q
)))
1576 __mld_query_work(skb
);
1577 mutex_unlock(&idev
->mc_lock
);
1579 if (rework
&& queue_delayed_work(mld_wq
, &idev
->mc_query_work
, 0))
1585 /* called with rcu_read_lock() */
1586 void igmp6_event_report(struct sk_buff
*skb
)
1588 struct inet6_dev
*idev
= __in6_dev_get(skb
->dev
);
1590 if (!idev
|| idev
->dead
)
1593 spin_lock_bh(&idev
->mc_report_lock
);
1594 if (skb_queue_len(&idev
->mc_report_queue
) < MLD_MAX_SKBS
) {
1595 __skb_queue_tail(&idev
->mc_report_queue
, skb
);
1596 if (!mod_delayed_work(mld_wq
, &idev
->mc_report_work
, 0))
1600 spin_unlock_bh(&idev
->mc_report_lock
);
1605 static void __mld_report_work(struct sk_buff
*skb
)
1607 struct inet6_dev
*idev
;
1608 struct ifmcaddr6
*ma
;
1609 struct mld_msg
*mld
;
1612 /* Our own report looped back. Ignore it. */
1613 if (skb
->pkt_type
== PACKET_LOOPBACK
)
1616 /* send our report if the MC router may not have heard this report */
1617 if (skb
->pkt_type
!= PACKET_MULTICAST
&&
1618 skb
->pkt_type
!= PACKET_BROADCAST
)
1621 if (!pskb_may_pull(skb
, sizeof(*mld
) - sizeof(struct icmp6hdr
)))
1624 mld
= (struct mld_msg
*)icmp6_hdr(skb
);
1626 /* Drop reports with not link local source */
1627 addr_type
= ipv6_addr_type(&ipv6_hdr(skb
)->saddr
);
1628 if (addr_type
!= IPV6_ADDR_ANY
&&
1629 !(addr_type
&IPV6_ADDR_LINKLOCAL
))
1632 idev
= in6_dev_get(skb
->dev
);
1637 * Cancel the work for this group
1640 for_each_mc_mclock(idev
, ma
) {
1641 if (ipv6_addr_equal(&ma
->mca_addr
, &mld
->mld_mca
)) {
1642 if (cancel_delayed_work(&ma
->mca_work
))
1643 refcount_dec(&ma
->mca_refcnt
);
1644 ma
->mca_flags
&= ~(MAF_LAST_REPORTER
|
1655 static void mld_report_work(struct work_struct
*work
)
1657 struct inet6_dev
*idev
= container_of(to_delayed_work(work
),
1660 struct sk_buff_head q
;
1661 struct sk_buff
*skb
;
1662 bool rework
= false;
1665 skb_queue_head_init(&q
);
1666 spin_lock_bh(&idev
->mc_report_lock
);
1667 while ((skb
= __skb_dequeue(&idev
->mc_report_queue
))) {
1668 __skb_queue_tail(&q
, skb
);
1670 if (++cnt
>= MLD_MAX_QUEUE
) {
1675 spin_unlock_bh(&idev
->mc_report_lock
);
1677 mutex_lock(&idev
->mc_lock
);
1678 while ((skb
= __skb_dequeue(&q
)))
1679 __mld_report_work(skb
);
1680 mutex_unlock(&idev
->mc_lock
);
1682 if (rework
&& queue_delayed_work(mld_wq
, &idev
->mc_report_work
, 0))
1688 static bool is_in(struct ifmcaddr6
*pmc
, struct ip6_sf_list
*psf
, int type
,
1689 int gdeleted
, int sdeleted
)
1692 case MLD2_MODE_IS_INCLUDE
:
1693 case MLD2_MODE_IS_EXCLUDE
:
1694 if (gdeleted
|| sdeleted
)
1696 if (!((pmc
->mca_flags
& MAF_GSQUERY
) && !psf
->sf_gsresp
)) {
1697 if (pmc
->mca_sfmode
== MCAST_INCLUDE
)
1699 /* don't include if this source is excluded
1702 if (psf
->sf_count
[MCAST_INCLUDE
])
1703 return type
== MLD2_MODE_IS_INCLUDE
;
1704 return pmc
->mca_sfcount
[MCAST_EXCLUDE
] ==
1705 psf
->sf_count
[MCAST_EXCLUDE
];
1708 case MLD2_CHANGE_TO_INCLUDE
:
1709 if (gdeleted
|| sdeleted
)
1711 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
1712 case MLD2_CHANGE_TO_EXCLUDE
:
1713 if (gdeleted
|| sdeleted
)
1715 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
] == 0 ||
1716 psf
->sf_count
[MCAST_INCLUDE
])
1718 return pmc
->mca_sfcount
[MCAST_EXCLUDE
] ==
1719 psf
->sf_count
[MCAST_EXCLUDE
];
1720 case MLD2_ALLOW_NEW_SOURCES
:
1721 if (gdeleted
|| !psf
->sf_crcount
)
1723 return (pmc
->mca_sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
1724 case MLD2_BLOCK_OLD_SOURCES
:
1725 if (pmc
->mca_sfmode
== MCAST_INCLUDE
)
1726 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
1727 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
1733 mld_scount(struct ifmcaddr6
*pmc
, int type
, int gdeleted
, int sdeleted
)
1735 struct ip6_sf_list
*psf
;
1738 for_each_psf_mclock(pmc
, psf
) {
1739 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
1746 static void ip6_mc_hdr(const struct sock
*sk
, struct sk_buff
*skb
,
1747 struct net_device
*dev
, const struct in6_addr
*saddr
,
1748 const struct in6_addr
*daddr
, int proto
, int len
)
1750 struct ipv6hdr
*hdr
;
1752 skb
->protocol
= htons(ETH_P_IPV6
);
1755 skb_reset_network_header(skb
);
1756 skb_put(skb
, sizeof(struct ipv6hdr
));
1757 hdr
= ipv6_hdr(skb
);
1759 ip6_flow_hdr(hdr
, 0, 0);
1761 hdr
->payload_len
= htons(len
);
1762 hdr
->nexthdr
= proto
;
1763 hdr
->hop_limit
= READ_ONCE(inet6_sk(sk
)->hop_limit
);
1765 hdr
->saddr
= *saddr
;
1766 hdr
->daddr
= *daddr
;
1769 static struct sk_buff
*mld_newpack(struct inet6_dev
*idev
, unsigned int mtu
)
1771 u8 ra
[8] = { IPPROTO_ICMPV6
, 0, IPV6_TLV_ROUTERALERT
,
1772 2, 0, 0, IPV6_TLV_PADN
, 0 };
1773 struct net_device
*dev
= idev
->dev
;
1774 int hlen
= LL_RESERVED_SPACE(dev
);
1775 int tlen
= dev
->needed_tailroom
;
1776 struct net
*net
= dev_net(dev
);
1777 const struct in6_addr
*saddr
;
1778 struct in6_addr addr_buf
;
1779 struct mld2_report
*pmr
;
1780 struct sk_buff
*skb
;
1785 sk
= net
->ipv6
.igmp_sk
;
1786 /* we assume size > sizeof(ra) here
1787 * Also try to not allocate high-order pages for big MTU
1789 size
= min_t(int, mtu
, PAGE_SIZE
/ 2) + hlen
+ tlen
;
1790 skb
= sock_alloc_send_skb(sk
, size
, 1, &err
);
1794 skb
->priority
= TC_PRIO_CONTROL
;
1795 skb_reserve(skb
, hlen
);
1796 skb_tailroom_reserve(skb
, mtu
, tlen
);
1798 if (ipv6_get_lladdr(dev
, &addr_buf
, IFA_F_TENTATIVE
)) {
1799 /* <draft-ietf-magma-mld-source-05.txt>:
1800 * use unspecified address as the source address
1801 * when a valid link-local address is not available.
1803 saddr
= &in6addr_any
;
1807 ip6_mc_hdr(sk
, skb
, dev
, saddr
, &mld2_all_mcr
, NEXTHDR_HOP
, 0);
1809 skb_put_data(skb
, ra
, sizeof(ra
));
1811 skb_set_transport_header(skb
, skb_tail_pointer(skb
) - skb
->data
);
1812 skb_put(skb
, sizeof(*pmr
));
1813 pmr
= (struct mld2_report
*)skb_transport_header(skb
);
1814 pmr
->mld2r_type
= ICMPV6_MLD2_REPORT
;
1815 pmr
->mld2r_resv1
= 0;
1816 pmr
->mld2r_cksum
= 0;
1817 pmr
->mld2r_resv2
= 0;
1818 pmr
->mld2r_ngrec
= 0;
1822 static void mld_sendpack(struct sk_buff
*skb
)
1824 struct ipv6hdr
*pip6
= ipv6_hdr(skb
);
1825 struct mld2_report
*pmr
=
1826 (struct mld2_report
*)skb_transport_header(skb
);
1827 int payload_len
, mldlen
;
1828 struct inet6_dev
*idev
;
1829 struct net
*net
= dev_net(skb
->dev
);
1832 struct dst_entry
*dst
;
1835 idev
= __in6_dev_get(skb
->dev
);
1836 IP6_INC_STATS(net
, idev
, IPSTATS_MIB_OUTREQUESTS
);
1838 payload_len
= (skb_tail_pointer(skb
) - skb_network_header(skb
)) -
1840 mldlen
= skb_tail_pointer(skb
) - skb_transport_header(skb
);
1841 pip6
->payload_len
= htons(payload_len
);
1843 pmr
->mld2r_cksum
= csum_ipv6_magic(&pip6
->saddr
, &pip6
->daddr
, mldlen
,
1845 csum_partial(skb_transport_header(skb
),
1848 icmpv6_flow_init(net
->ipv6
.igmp_sk
, &fl6
, ICMPV6_MLD2_REPORT
,
1849 &ipv6_hdr(skb
)->saddr
, &ipv6_hdr(skb
)->daddr
,
1851 dst
= icmp6_dst_alloc(skb
->dev
, &fl6
);
1858 skb_dst_set(skb
, dst
);
1862 err
= NF_HOOK(NFPROTO_IPV6
, NF_INET_LOCAL_OUT
,
1863 net
, net
->ipv6
.igmp_sk
, skb
, NULL
, skb
->dev
,
1867 ICMP6MSGOUT_INC_STATS(net
, idev
, ICMPV6_MLD2_REPORT
);
1868 ICMP6_INC_STATS(net
, idev
, ICMP6_MIB_OUTMSGS
);
1870 IP6_INC_STATS(net
, idev
, IPSTATS_MIB_OUTDISCARDS
);
1881 static int grec_size(struct ifmcaddr6
*pmc
, int type
, int gdel
, int sdel
)
1883 return sizeof(struct mld2_grec
) + 16 * mld_scount(pmc
,type
,gdel
,sdel
);
1886 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ifmcaddr6
*pmc
,
1887 int type
, struct mld2_grec
**ppgr
, unsigned int mtu
)
1889 struct mld2_report
*pmr
;
1890 struct mld2_grec
*pgr
;
1893 skb
= mld_newpack(pmc
->idev
, mtu
);
1897 pgr
= skb_put(skb
, sizeof(struct mld2_grec
));
1898 pgr
->grec_type
= type
;
1899 pgr
->grec_auxwords
= 0;
1900 pgr
->grec_nsrcs
= 0;
1901 pgr
->grec_mca
= pmc
->mca_addr
; /* structure copy */
1902 pmr
= (struct mld2_report
*)skb_transport_header(skb
);
1903 pmr
->mld2r_ngrec
= htons(ntohs(pmr
->mld2r_ngrec
)+1);
1908 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
1910 /* called with mc_lock */
1911 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ifmcaddr6
*pmc
,
1912 int type
, int gdeleted
, int sdeleted
,
1915 struct ip6_sf_list
*psf
, *psf_prev
, *psf_next
;
1916 int scount
, stotal
, first
, isquery
, truncate
;
1917 struct ip6_sf_list __rcu
**psf_list
;
1918 struct inet6_dev
*idev
= pmc
->idev
;
1919 struct net_device
*dev
= idev
->dev
;
1920 struct mld2_grec
*pgr
= NULL
;
1921 struct mld2_report
*pmr
;
1924 if (pmc
->mca_flags
& MAF_NOREPORT
)
1927 mtu
= READ_ONCE(dev
->mtu
);
1928 if (mtu
< IPV6_MIN_MTU
)
1931 isquery
= type
== MLD2_MODE_IS_INCLUDE
||
1932 type
== MLD2_MODE_IS_EXCLUDE
;
1933 truncate
= type
== MLD2_MODE_IS_EXCLUDE
||
1934 type
== MLD2_CHANGE_TO_EXCLUDE
;
1936 stotal
= scount
= 0;
1938 psf_list
= sdeleted
? &pmc
->mca_tomb
: &pmc
->mca_sources
;
1940 if (!rcu_access_pointer(*psf_list
))
1943 pmr
= skb
? (struct mld2_report
*)skb_transport_header(skb
) : NULL
;
1945 /* EX and TO_EX get a fresh packet, if needed */
1947 if (pmr
&& pmr
->mld2r_ngrec
&&
1948 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
1951 skb
= mld_newpack(idev
, mtu
);
1956 for (psf
= mc_dereference(*psf_list
, idev
);
1959 struct in6_addr
*psrc
;
1961 psf_next
= mc_dereference(psf
->sf_next
, idev
);
1963 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
) && !crsend
) {
1968 /* Based on RFC3810 6.1. Should not send source-list change
1969 * records when there is a filter mode change.
1971 if (((gdeleted
&& pmc
->mca_sfmode
== MCAST_EXCLUDE
) ||
1972 (!gdeleted
&& pmc
->mca_crcount
)) &&
1973 (type
== MLD2_ALLOW_NEW_SOURCES
||
1974 type
== MLD2_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
)
1975 goto decrease_sf_crcount
;
1977 /* clear marks on query responses */
1981 if (AVAILABLE(skb
) < sizeof(*psrc
) +
1982 first
*sizeof(struct mld2_grec
)) {
1983 if (truncate
&& !first
)
1984 break; /* truncate these */
1986 pgr
->grec_nsrcs
= htons(scount
);
1989 skb
= mld_newpack(idev
, mtu
);
1994 skb
= add_grhead(skb
, pmc
, type
, &pgr
, mtu
);
1999 psrc
= skb_put(skb
, sizeof(*psrc
));
2000 *psrc
= psf
->sf_addr
;
2002 if ((type
== MLD2_ALLOW_NEW_SOURCES
||
2003 type
== MLD2_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
) {
2004 decrease_sf_crcount
:
2006 if ((sdeleted
|| gdeleted
) && psf
->sf_crcount
== 0) {
2008 rcu_assign_pointer(psf_prev
->sf_next
,
2009 mc_dereference(psf
->sf_next
, idev
));
2011 rcu_assign_pointer(*psf_list
,
2012 mc_dereference(psf
->sf_next
, idev
));
2013 kfree_rcu(psf
, rcu
);
2022 if (type
== MLD2_ALLOW_NEW_SOURCES
||
2023 type
== MLD2_BLOCK_OLD_SOURCES
)
2025 if (pmc
->mca_crcount
|| isquery
|| crsend
) {
2026 /* make sure we have room for group header */
2027 if (skb
&& AVAILABLE(skb
) < sizeof(struct mld2_grec
)) {
2029 skb
= NULL
; /* add_grhead will get a new one */
2031 skb
= add_grhead(skb
, pmc
, type
, &pgr
, mtu
);
2035 pgr
->grec_nsrcs
= htons(scount
);
2038 pmc
->mca_flags
&= ~MAF_GSQUERY
; /* clear query state */
2042 /* called with mc_lock */
2043 static void mld_send_report(struct inet6_dev
*idev
, struct ifmcaddr6
*pmc
)
2045 struct sk_buff
*skb
= NULL
;
2049 for_each_mc_mclock(idev
, pmc
) {
2050 if (pmc
->mca_flags
& MAF_NOREPORT
)
2052 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
])
2053 type
= MLD2_MODE_IS_EXCLUDE
;
2055 type
= MLD2_MODE_IS_INCLUDE
;
2056 skb
= add_grec(skb
, pmc
, type
, 0, 0, 0);
2059 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
])
2060 type
= MLD2_MODE_IS_EXCLUDE
;
2062 type
= MLD2_MODE_IS_INCLUDE
;
2063 skb
= add_grec(skb
, pmc
, type
, 0, 0, 0);
2070 * remove zero-count source records from a source filter list
2071 * called with mc_lock
2073 static void mld_clear_zeros(struct ip6_sf_list __rcu
**ppsf
, struct inet6_dev
*idev
)
2075 struct ip6_sf_list
*psf_prev
, *psf_next
, *psf
;
2078 for (psf
= mc_dereference(*ppsf
, idev
);
2081 psf_next
= mc_dereference(psf
->sf_next
, idev
);
2082 if (psf
->sf_crcount
== 0) {
2084 rcu_assign_pointer(psf_prev
->sf_next
,
2085 mc_dereference(psf
->sf_next
, idev
));
2087 rcu_assign_pointer(*ppsf
,
2088 mc_dereference(psf
->sf_next
, idev
));
2089 kfree_rcu(psf
, rcu
);
2096 /* called with mc_lock */
2097 static void mld_send_cr(struct inet6_dev
*idev
)
2099 struct ifmcaddr6
*pmc
, *pmc_prev
, *pmc_next
;
2100 struct sk_buff
*skb
= NULL
;
2105 for (pmc
= mc_dereference(idev
->mc_tomb
, idev
);
2108 pmc_next
= mc_dereference(pmc
->next
, idev
);
2109 if (pmc
->mca_sfmode
== MCAST_INCLUDE
) {
2110 type
= MLD2_BLOCK_OLD_SOURCES
;
2111 dtype
= MLD2_BLOCK_OLD_SOURCES
;
2112 skb
= add_grec(skb
, pmc
, type
, 1, 0, 0);
2113 skb
= add_grec(skb
, pmc
, dtype
, 1, 1, 0);
2115 if (pmc
->mca_crcount
) {
2116 if (pmc
->mca_sfmode
== MCAST_EXCLUDE
) {
2117 type
= MLD2_CHANGE_TO_INCLUDE
;
2118 skb
= add_grec(skb
, pmc
, type
, 1, 0, 0);
2121 if (pmc
->mca_crcount
== 0) {
2122 mld_clear_zeros(&pmc
->mca_tomb
, idev
);
2123 mld_clear_zeros(&pmc
->mca_sources
, idev
);
2126 if (pmc
->mca_crcount
== 0 &&
2127 !rcu_access_pointer(pmc
->mca_tomb
) &&
2128 !rcu_access_pointer(pmc
->mca_sources
)) {
2130 rcu_assign_pointer(pmc_prev
->next
, pmc_next
);
2132 rcu_assign_pointer(idev
->mc_tomb
, pmc_next
);
2133 in6_dev_put(pmc
->idev
);
2134 kfree_rcu(pmc
, rcu
);
2140 for_each_mc_mclock(idev
, pmc
) {
2141 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
]) {
2142 type
= MLD2_BLOCK_OLD_SOURCES
;
2143 dtype
= MLD2_ALLOW_NEW_SOURCES
;
2145 type
= MLD2_ALLOW_NEW_SOURCES
;
2146 dtype
= MLD2_BLOCK_OLD_SOURCES
;
2148 skb
= add_grec(skb
, pmc
, type
, 0, 0, 0);
2149 skb
= add_grec(skb
, pmc
, dtype
, 0, 1, 0); /* deleted sources */
2151 /* filter mode changes */
2152 if (pmc
->mca_crcount
) {
2153 if (pmc
->mca_sfmode
== MCAST_EXCLUDE
)
2154 type
= MLD2_CHANGE_TO_EXCLUDE
;
2156 type
= MLD2_CHANGE_TO_INCLUDE
;
2157 skb
= add_grec(skb
, pmc
, type
, 0, 0, 0);
2163 (void) mld_sendpack(skb
);
2166 static void igmp6_send(struct in6_addr
*addr
, struct net_device
*dev
, int type
)
2168 struct net
*net
= dev_net(dev
);
2169 struct sock
*sk
= net
->ipv6
.igmp_sk
;
2170 struct inet6_dev
*idev
;
2171 struct sk_buff
*skb
;
2172 struct mld_msg
*hdr
;
2173 const struct in6_addr
*snd_addr
, *saddr
;
2174 struct in6_addr addr_buf
;
2175 int hlen
= LL_RESERVED_SPACE(dev
);
2176 int tlen
= dev
->needed_tailroom
;
2177 int err
, len
, payload_len
, full_len
;
2178 u8 ra
[8] = { IPPROTO_ICMPV6
, 0,
2179 IPV6_TLV_ROUTERALERT
, 2, 0, 0,
2182 struct dst_entry
*dst
;
2184 if (type
== ICMPV6_MGM_REDUCTION
)
2185 snd_addr
= &in6addr_linklocal_allrouters
;
2189 len
= sizeof(struct icmp6hdr
) + sizeof(struct in6_addr
);
2190 payload_len
= len
+ sizeof(ra
);
2191 full_len
= sizeof(struct ipv6hdr
) + payload_len
;
2194 IP6_INC_STATS(net
, __in6_dev_get(dev
), IPSTATS_MIB_OUTREQUESTS
);
2197 skb
= sock_alloc_send_skb(sk
, hlen
+ tlen
+ full_len
, 1, &err
);
2201 IP6_INC_STATS(net
, __in6_dev_get(dev
),
2202 IPSTATS_MIB_OUTDISCARDS
);
2206 skb
->priority
= TC_PRIO_CONTROL
;
2207 skb_reserve(skb
, hlen
);
2209 if (ipv6_get_lladdr(dev
, &addr_buf
, IFA_F_TENTATIVE
)) {
2210 /* <draft-ietf-magma-mld-source-05.txt>:
2211 * use unspecified address as the source address
2212 * when a valid link-local address is not available.
2214 saddr
= &in6addr_any
;
2218 ip6_mc_hdr(sk
, skb
, dev
, saddr
, snd_addr
, NEXTHDR_HOP
, payload_len
);
2220 skb_put_data(skb
, ra
, sizeof(ra
));
2222 hdr
= skb_put_zero(skb
, sizeof(struct mld_msg
));
2223 hdr
->mld_type
= type
;
2224 hdr
->mld_mca
= *addr
;
2226 hdr
->mld_cksum
= csum_ipv6_magic(saddr
, snd_addr
, len
,
2228 csum_partial(hdr
, len
, 0));
2231 idev
= __in6_dev_get(skb
->dev
);
2233 icmpv6_flow_init(sk
, &fl6
, type
,
2234 &ipv6_hdr(skb
)->saddr
, &ipv6_hdr(skb
)->daddr
,
2236 dst
= icmp6_dst_alloc(skb
->dev
, &fl6
);
2242 skb_dst_set(skb
, dst
);
2243 err
= NF_HOOK(NFPROTO_IPV6
, NF_INET_LOCAL_OUT
,
2244 net
, sk
, skb
, NULL
, skb
->dev
,
2248 ICMP6MSGOUT_INC_STATS(net
, idev
, type
);
2249 ICMP6_INC_STATS(net
, idev
, ICMP6_MIB_OUTMSGS
);
2251 IP6_INC_STATS(net
, idev
, IPSTATS_MIB_OUTDISCARDS
);
2261 /* called with mc_lock */
2262 static void mld_send_initial_cr(struct inet6_dev
*idev
)
2264 struct sk_buff
*skb
;
2265 struct ifmcaddr6
*pmc
;
2268 if (mld_in_v1_mode(idev
))
2272 for_each_mc_mclock(idev
, pmc
) {
2273 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
])
2274 type
= MLD2_CHANGE_TO_EXCLUDE
;
2276 type
= MLD2_ALLOW_NEW_SOURCES
;
2277 skb
= add_grec(skb
, pmc
, type
, 0, 0, 1);
2283 void ipv6_mc_dad_complete(struct inet6_dev
*idev
)
2285 mutex_lock(&idev
->mc_lock
);
2286 idev
->mc_dad_count
= idev
->mc_qrv
;
2287 if (idev
->mc_dad_count
) {
2288 mld_send_initial_cr(idev
);
2289 idev
->mc_dad_count
--;
2290 if (idev
->mc_dad_count
)
2291 mld_dad_start_work(idev
,
2292 unsolicited_report_interval(idev
));
2294 mutex_unlock(&idev
->mc_lock
);
2297 static void mld_dad_work(struct work_struct
*work
)
2299 struct inet6_dev
*idev
= container_of(to_delayed_work(work
),
2302 mutex_lock(&idev
->mc_lock
);
2303 mld_send_initial_cr(idev
);
2304 if (idev
->mc_dad_count
) {
2305 idev
->mc_dad_count
--;
2306 if (idev
->mc_dad_count
)
2307 mld_dad_start_work(idev
,
2308 unsolicited_report_interval(idev
));
2310 mutex_unlock(&idev
->mc_lock
);
2314 /* called with mc_lock */
2315 static int ip6_mc_del1_src(struct ifmcaddr6
*pmc
, int sfmode
,
2316 const struct in6_addr
*psfsrc
)
2318 struct ip6_sf_list
*psf
, *psf_prev
;
2322 for_each_psf_mclock(pmc
, psf
) {
2323 if (ipv6_addr_equal(&psf
->sf_addr
, psfsrc
))
2327 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
2328 /* source filter not found, or count wrong => bug */
2331 WRITE_ONCE(psf
->sf_count
[sfmode
], psf
->sf_count
[sfmode
] - 1);
2332 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
2333 struct inet6_dev
*idev
= pmc
->idev
;
2335 /* no more filters for this source */
2337 rcu_assign_pointer(psf_prev
->sf_next
,
2338 mc_dereference(psf
->sf_next
, idev
));
2340 rcu_assign_pointer(pmc
->mca_sources
,
2341 mc_dereference(psf
->sf_next
, idev
));
2343 if (psf
->sf_oldin
&& !(pmc
->mca_flags
& MAF_NOREPORT
) &&
2344 !mld_in_v1_mode(idev
)) {
2345 psf
->sf_crcount
= idev
->mc_qrv
;
2346 rcu_assign_pointer(psf
->sf_next
,
2347 mc_dereference(pmc
->mca_tomb
, idev
));
2348 rcu_assign_pointer(pmc
->mca_tomb
, psf
);
2351 kfree_rcu(psf
, rcu
);
2357 /* called with mc_lock */
2358 static int ip6_mc_del_src(struct inet6_dev
*idev
, const struct in6_addr
*pmca
,
2359 int sfmode
, int sfcount
, const struct in6_addr
*psfsrc
,
2362 struct ifmcaddr6
*pmc
;
2369 for_each_mc_mclock(idev
, pmc
) {
2370 if (ipv6_addr_equal(pmca
, &pmc
->mca_addr
))
2378 if (!pmc
->mca_sfcount
[sfmode
])
2381 pmc
->mca_sfcount
[sfmode
]--;
2384 for (i
= 0; i
< sfcount
; i
++) {
2385 int rv
= ip6_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
2387 changerec
|= rv
> 0;
2391 if (pmc
->mca_sfmode
== MCAST_EXCLUDE
&&
2392 pmc
->mca_sfcount
[MCAST_EXCLUDE
] == 0 &&
2393 pmc
->mca_sfcount
[MCAST_INCLUDE
]) {
2394 struct ip6_sf_list
*psf
;
2396 /* filter mode change */
2397 pmc
->mca_sfmode
= MCAST_INCLUDE
;
2398 pmc
->mca_crcount
= idev
->mc_qrv
;
2399 idev
->mc_ifc_count
= pmc
->mca_crcount
;
2400 for_each_psf_mclock(pmc
, psf
)
2401 psf
->sf_crcount
= 0;
2402 mld_ifc_event(pmc
->idev
);
2403 } else if (sf_setstate(pmc
) || changerec
) {
2404 mld_ifc_event(pmc
->idev
);
2411 * Add multicast single-source filter to the interface list
2412 * called with mc_lock
2414 static int ip6_mc_add1_src(struct ifmcaddr6
*pmc
, int sfmode
,
2415 const struct in6_addr
*psfsrc
)
2417 struct ip6_sf_list
*psf
, *psf_prev
;
2420 for_each_psf_mclock(pmc
, psf
) {
2421 if (ipv6_addr_equal(&psf
->sf_addr
, psfsrc
))
2426 psf
= kzalloc(sizeof(*psf
), GFP_KERNEL
);
2430 psf
->sf_addr
= *psfsrc
;
2432 rcu_assign_pointer(psf_prev
->sf_next
, psf
);
2434 rcu_assign_pointer(pmc
->mca_sources
, psf
);
2437 WRITE_ONCE(psf
->sf_count
[sfmode
], psf
->sf_count
[sfmode
] + 1);
2441 /* called with mc_lock */
2442 static void sf_markstate(struct ifmcaddr6
*pmc
)
2444 struct ip6_sf_list
*psf
;
2445 int mca_xcount
= pmc
->mca_sfcount
[MCAST_EXCLUDE
];
2447 for_each_psf_mclock(pmc
, psf
) {
2448 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
]) {
2449 psf
->sf_oldin
= mca_xcount
==
2450 psf
->sf_count
[MCAST_EXCLUDE
] &&
2451 !psf
->sf_count
[MCAST_INCLUDE
];
2453 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
2458 /* called with mc_lock */
2459 static int sf_setstate(struct ifmcaddr6
*pmc
)
2461 struct ip6_sf_list
*psf
, *dpsf
;
2462 int mca_xcount
= pmc
->mca_sfcount
[MCAST_EXCLUDE
];
2463 int qrv
= pmc
->idev
->mc_qrv
;
2467 for_each_psf_mclock(pmc
, psf
) {
2468 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
]) {
2469 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
2470 !psf
->sf_count
[MCAST_INCLUDE
];
2472 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
2474 if (!psf
->sf_oldin
) {
2475 struct ip6_sf_list
*prev
= NULL
;
2477 for_each_psf_tomb(pmc
, dpsf
) {
2478 if (ipv6_addr_equal(&dpsf
->sf_addr
,
2485 rcu_assign_pointer(prev
->sf_next
,
2486 mc_dereference(dpsf
->sf_next
,
2489 rcu_assign_pointer(pmc
->mca_tomb
,
2490 mc_dereference(dpsf
->sf_next
,
2492 kfree_rcu(dpsf
, rcu
);
2494 psf
->sf_crcount
= qrv
;
2497 } else if (psf
->sf_oldin
) {
2498 psf
->sf_crcount
= 0;
2500 * add or update "delete" records if an active filter
2504 for_each_psf_tomb(pmc
, dpsf
)
2505 if (ipv6_addr_equal(&dpsf
->sf_addr
,
2509 dpsf
= kmalloc(sizeof(*dpsf
), GFP_KERNEL
);
2513 rcu_assign_pointer(dpsf
->sf_next
,
2514 mc_dereference(pmc
->mca_tomb
, pmc
->idev
));
2515 rcu_assign_pointer(pmc
->mca_tomb
, dpsf
);
2517 dpsf
->sf_crcount
= qrv
;
2525 * Add multicast source filter list to the interface list
2526 * called with mc_lock
2528 static int ip6_mc_add_src(struct inet6_dev
*idev
, const struct in6_addr
*pmca
,
2529 int sfmode
, int sfcount
, const struct in6_addr
*psfsrc
,
2532 struct ifmcaddr6
*pmc
;
2539 for_each_mc_mclock(idev
, pmc
) {
2540 if (ipv6_addr_equal(pmca
, &pmc
->mca_addr
))
2547 isexclude
= pmc
->mca_sfmode
== MCAST_EXCLUDE
;
2549 WRITE_ONCE(pmc
->mca_sfcount
[sfmode
],
2550 pmc
->mca_sfcount
[sfmode
] + 1);
2552 for (i
= 0; i
< sfcount
; i
++) {
2553 err
= ip6_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
]);
2561 WRITE_ONCE(pmc
->mca_sfcount
[sfmode
],
2562 pmc
->mca_sfcount
[sfmode
] - 1);
2563 for (j
= 0; j
< i
; j
++)
2564 ip6_mc_del1_src(pmc
, sfmode
, &psfsrc
[j
]);
2565 } else if (isexclude
!= (pmc
->mca_sfcount
[MCAST_EXCLUDE
] != 0)) {
2566 struct ip6_sf_list
*psf
;
2568 /* filter mode change */
2569 if (pmc
->mca_sfcount
[MCAST_EXCLUDE
])
2570 pmc
->mca_sfmode
= MCAST_EXCLUDE
;
2571 else if (pmc
->mca_sfcount
[MCAST_INCLUDE
])
2572 pmc
->mca_sfmode
= MCAST_INCLUDE
;
2573 /* else no filters; keep old mode for reports */
2575 pmc
->mca_crcount
= idev
->mc_qrv
;
2576 idev
->mc_ifc_count
= pmc
->mca_crcount
;
2577 for_each_psf_mclock(pmc
, psf
)
2578 psf
->sf_crcount
= 0;
2579 mld_ifc_event(idev
);
2580 } else if (sf_setstate(pmc
)) {
2581 mld_ifc_event(idev
);
2586 /* called with mc_lock */
2587 static void ip6_mc_clear_src(struct ifmcaddr6
*pmc
)
2589 struct ip6_sf_list
*psf
, *nextpsf
;
2591 for (psf
= mc_dereference(pmc
->mca_tomb
, pmc
->idev
);
2594 nextpsf
= mc_dereference(psf
->sf_next
, pmc
->idev
);
2595 kfree_rcu(psf
, rcu
);
2597 RCU_INIT_POINTER(pmc
->mca_tomb
, NULL
);
2598 for (psf
= mc_dereference(pmc
->mca_sources
, pmc
->idev
);
2601 nextpsf
= mc_dereference(psf
->sf_next
, pmc
->idev
);
2602 kfree_rcu(psf
, rcu
);
2604 RCU_INIT_POINTER(pmc
->mca_sources
, NULL
);
2605 pmc
->mca_sfmode
= MCAST_EXCLUDE
;
2606 pmc
->mca_sfcount
[MCAST_INCLUDE
] = 0;
2607 /* Paired with the READ_ONCE() from ipv6_chk_mcast_addr() */
2608 WRITE_ONCE(pmc
->mca_sfcount
[MCAST_EXCLUDE
], 1);
2611 /* called with mc_lock */
2612 static void igmp6_join_group(struct ifmcaddr6
*ma
)
2614 unsigned long delay
;
2616 if (ma
->mca_flags
& MAF_NOREPORT
)
2619 igmp6_send(&ma
->mca_addr
, ma
->idev
->dev
, ICMPV6_MGM_REPORT
);
2621 delay
= get_random_u32_below(unsolicited_report_interval(ma
->idev
));
2623 if (cancel_delayed_work(&ma
->mca_work
)) {
2624 refcount_dec(&ma
->mca_refcnt
);
2625 delay
= ma
->mca_work
.timer
.expires
- jiffies
;
2628 if (!mod_delayed_work(mld_wq
, &ma
->mca_work
, delay
))
2629 refcount_inc(&ma
->mca_refcnt
);
2630 ma
->mca_flags
|= MAF_TIMER_RUNNING
| MAF_LAST_REPORTER
;
2633 static int ip6_mc_leave_src(struct sock
*sk
, struct ipv6_mc_socklist
*iml
,
2634 struct inet6_dev
*idev
)
2636 struct ip6_sf_socklist
*psl
;
2639 psl
= sock_dereference(iml
->sflist
, sk
);
2642 mutex_lock(&idev
->mc_lock
);
2645 /* any-source empty exclude case */
2646 err
= ip6_mc_del_src(idev
, &iml
->addr
, iml
->sfmode
, 0, NULL
, 0);
2648 err
= ip6_mc_del_src(idev
, &iml
->addr
, iml
->sfmode
,
2649 psl
->sl_count
, psl
->sl_addr
, 0);
2650 RCU_INIT_POINTER(iml
->sflist
, NULL
);
2651 atomic_sub(struct_size(psl
, sl_addr
, psl
->sl_max
),
2652 &sk
->sk_omem_alloc
);
2653 kfree_rcu(psl
, rcu
);
2657 mutex_unlock(&idev
->mc_lock
);
2662 /* called with mc_lock */
2663 static void igmp6_leave_group(struct ifmcaddr6
*ma
)
2665 if (mld_in_v1_mode(ma
->idev
)) {
2666 if (ma
->mca_flags
& MAF_LAST_REPORTER
) {
2667 igmp6_send(&ma
->mca_addr
, ma
->idev
->dev
,
2668 ICMPV6_MGM_REDUCTION
);
2671 mld_add_delrec(ma
->idev
, ma
);
2672 mld_ifc_event(ma
->idev
);
2676 static void mld_gq_work(struct work_struct
*work
)
2678 struct inet6_dev
*idev
= container_of(to_delayed_work(work
),
2682 mutex_lock(&idev
->mc_lock
);
2683 mld_send_report(idev
, NULL
);
2684 idev
->mc_gq_running
= 0;
2685 mutex_unlock(&idev
->mc_lock
);
2690 static void mld_ifc_work(struct work_struct
*work
)
2692 struct inet6_dev
*idev
= container_of(to_delayed_work(work
),
2696 mutex_lock(&idev
->mc_lock
);
2699 if (idev
->mc_ifc_count
) {
2700 idev
->mc_ifc_count
--;
2701 if (idev
->mc_ifc_count
)
2702 mld_ifc_start_work(idev
,
2703 unsolicited_report_interval(idev
));
2705 mutex_unlock(&idev
->mc_lock
);
2709 /* called with mc_lock */
2710 static void mld_ifc_event(struct inet6_dev
*idev
)
2712 if (mld_in_v1_mode(idev
))
2715 idev
->mc_ifc_count
= idev
->mc_qrv
;
2716 mld_ifc_start_work(idev
, 1);
2719 static void mld_mca_work(struct work_struct
*work
)
2721 struct ifmcaddr6
*ma
= container_of(to_delayed_work(work
),
2722 struct ifmcaddr6
, mca_work
);
2724 mutex_lock(&ma
->idev
->mc_lock
);
2725 if (mld_in_v1_mode(ma
->idev
))
2726 igmp6_send(&ma
->mca_addr
, ma
->idev
->dev
, ICMPV6_MGM_REPORT
);
2728 mld_send_report(ma
->idev
, ma
);
2729 ma
->mca_flags
|= MAF_LAST_REPORTER
;
2730 ma
->mca_flags
&= ~MAF_TIMER_RUNNING
;
2731 mutex_unlock(&ma
->idev
->mc_lock
);
2736 /* Device changing type */
2738 void ipv6_mc_unmap(struct inet6_dev
*idev
)
2740 struct ifmcaddr6
*i
;
2742 /* Install multicast list, except for all-nodes (already installed) */
2744 mutex_lock(&idev
->mc_lock
);
2745 for_each_mc_mclock(idev
, i
)
2746 igmp6_group_dropped(i
);
2747 mutex_unlock(&idev
->mc_lock
);
2750 void ipv6_mc_remap(struct inet6_dev
*idev
)
2755 /* Device going down */
2756 void ipv6_mc_down(struct inet6_dev
*idev
)
2758 struct ifmcaddr6
*i
;
2760 mutex_lock(&idev
->mc_lock
);
2761 /* Withdraw multicast list */
2762 for_each_mc_mclock(idev
, i
)
2763 igmp6_group_dropped(i
);
2764 mutex_unlock(&idev
->mc_lock
);
2766 /* Should stop work after group drop. or we will
2767 * start work again in mld_ifc_event()
2769 mld_query_stop_work(idev
);
2770 mld_report_stop_work(idev
);
2772 mutex_lock(&idev
->mc_lock
);
2773 mld_ifc_stop_work(idev
);
2774 mld_gq_stop_work(idev
);
2775 mutex_unlock(&idev
->mc_lock
);
2777 mld_dad_stop_work(idev
);
2780 static void ipv6_mc_reset(struct inet6_dev
*idev
)
2782 idev
->mc_qrv
= sysctl_mld_qrv
;
2783 idev
->mc_qi
= MLD_QI_DEFAULT
;
2784 idev
->mc_qri
= MLD_QRI_DEFAULT
;
2785 idev
->mc_v1_seen
= 0;
2786 idev
->mc_maxdelay
= unsolicited_report_interval(idev
);
2789 /* Device going up */
2791 void ipv6_mc_up(struct inet6_dev
*idev
)
2793 struct ifmcaddr6
*i
;
2795 /* Install multicast list, except for all-nodes (already installed) */
2797 ipv6_mc_reset(idev
);
2798 mutex_lock(&idev
->mc_lock
);
2799 for_each_mc_mclock(idev
, i
) {
2800 mld_del_delrec(idev
, i
);
2801 igmp6_group_added(i
);
2803 mutex_unlock(&idev
->mc_lock
);
2806 /* IPv6 device initialization. */
2808 void ipv6_mc_init_dev(struct inet6_dev
*idev
)
2810 idev
->mc_gq_running
= 0;
2811 INIT_DELAYED_WORK(&idev
->mc_gq_work
, mld_gq_work
);
2812 RCU_INIT_POINTER(idev
->mc_tomb
, NULL
);
2813 idev
->mc_ifc_count
= 0;
2814 INIT_DELAYED_WORK(&idev
->mc_ifc_work
, mld_ifc_work
);
2815 INIT_DELAYED_WORK(&idev
->mc_dad_work
, mld_dad_work
);
2816 INIT_DELAYED_WORK(&idev
->mc_query_work
, mld_query_work
);
2817 INIT_DELAYED_WORK(&idev
->mc_report_work
, mld_report_work
);
2818 skb_queue_head_init(&idev
->mc_query_queue
);
2819 skb_queue_head_init(&idev
->mc_report_queue
);
2820 spin_lock_init(&idev
->mc_query_lock
);
2821 spin_lock_init(&idev
->mc_report_lock
);
2822 mutex_init(&idev
->mc_lock
);
2823 ipv6_mc_reset(idev
);
2827 * Device is about to be destroyed: clean up.
2830 void ipv6_mc_destroy_dev(struct inet6_dev
*idev
)
2832 struct ifmcaddr6
*i
;
2834 /* Deactivate works */
2836 mutex_lock(&idev
->mc_lock
);
2837 mld_clear_delrec(idev
);
2838 mutex_unlock(&idev
->mc_lock
);
2839 mld_clear_query(idev
);
2840 mld_clear_report(idev
);
2842 /* Delete all-nodes address. */
2843 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2844 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2847 __ipv6_dev_mc_dec(idev
, &in6addr_linklocal_allnodes
);
2849 if (idev
->cnf
.forwarding
)
2850 __ipv6_dev_mc_dec(idev
, &in6addr_linklocal_allrouters
);
2852 mutex_lock(&idev
->mc_lock
);
2853 while ((i
= mc_dereference(idev
->mc_list
, idev
))) {
2854 rcu_assign_pointer(idev
->mc_list
, mc_dereference(i
->next
, idev
));
2856 ip6_mc_clear_src(i
);
2859 mutex_unlock(&idev
->mc_lock
);
2862 static void ipv6_mc_rejoin_groups(struct inet6_dev
*idev
)
2864 struct ifmcaddr6
*pmc
;
2868 mutex_lock(&idev
->mc_lock
);
2869 if (mld_in_v1_mode(idev
)) {
2870 for_each_mc_mclock(idev
, pmc
)
2871 igmp6_join_group(pmc
);
2873 mld_send_report(idev
, NULL
);
2875 mutex_unlock(&idev
->mc_lock
);
2878 static int ipv6_mc_netdev_event(struct notifier_block
*this,
2879 unsigned long event
,
2882 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2883 struct inet6_dev
*idev
= __in6_dev_get(dev
);
2886 case NETDEV_RESEND_IGMP
:
2888 ipv6_mc_rejoin_groups(idev
);
2897 static struct notifier_block igmp6_netdev_notifier
= {
2898 .notifier_call
= ipv6_mc_netdev_event
,
2901 #ifdef CONFIG_PROC_FS
2902 struct igmp6_mc_iter_state
{
2903 struct seq_net_private p
;
2904 struct net_device
*dev
;
2905 struct inet6_dev
*idev
;
2908 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2910 static inline struct ifmcaddr6
*igmp6_mc_get_first(struct seq_file
*seq
)
2912 struct ifmcaddr6
*im
= NULL
;
2913 struct igmp6_mc_iter_state
*state
= igmp6_mc_seq_private(seq
);
2914 struct net
*net
= seq_file_net(seq
);
2917 for_each_netdev_rcu(net
, state
->dev
) {
2918 struct inet6_dev
*idev
;
2919 idev
= __in6_dev_get(state
->dev
);
2923 im
= rcu_dereference(idev
->mc_list
);
2932 static struct ifmcaddr6
*igmp6_mc_get_next(struct seq_file
*seq
, struct ifmcaddr6
*im
)
2934 struct igmp6_mc_iter_state
*state
= igmp6_mc_seq_private(seq
);
2936 im
= rcu_dereference(im
->next
);
2938 state
->dev
= next_net_device_rcu(state
->dev
);
2943 state
->idev
= __in6_dev_get(state
->dev
);
2946 im
= rcu_dereference(state
->idev
->mc_list
);
2951 static struct ifmcaddr6
*igmp6_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2953 struct ifmcaddr6
*im
= igmp6_mc_get_first(seq
);
2955 while (pos
&& (im
= igmp6_mc_get_next(seq
, im
)) != NULL
)
2957 return pos
? NULL
: im
;
2960 static void *igmp6_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2964 return igmp6_mc_get_idx(seq
, *pos
);
2967 static void *igmp6_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2969 struct ifmcaddr6
*im
= igmp6_mc_get_next(seq
, v
);
2975 static void igmp6_mc_seq_stop(struct seq_file
*seq
, void *v
)
2978 struct igmp6_mc_iter_state
*state
= igmp6_mc_seq_private(seq
);
2980 if (likely(state
->idev
))
2986 static int igmp6_mc_seq_show(struct seq_file
*seq
, void *v
)
2988 struct ifmcaddr6
*im
= (struct ifmcaddr6
*)v
;
2989 struct igmp6_mc_iter_state
*state
= igmp6_mc_seq_private(seq
);
2992 "%-4d %-15s %pi6 %5d %08X %ld\n",
2993 state
->dev
->ifindex
, state
->dev
->name
,
2995 im
->mca_users
, im
->mca_flags
,
2996 (im
->mca_flags
& MAF_TIMER_RUNNING
) ?
2997 jiffies_to_clock_t(im
->mca_work
.timer
.expires
- jiffies
) : 0);
3001 static const struct seq_operations igmp6_mc_seq_ops
= {
3002 .start
= igmp6_mc_seq_start
,
3003 .next
= igmp6_mc_seq_next
,
3004 .stop
= igmp6_mc_seq_stop
,
3005 .show
= igmp6_mc_seq_show
,
3008 struct igmp6_mcf_iter_state
{
3009 struct seq_net_private p
;
3010 struct net_device
*dev
;
3011 struct inet6_dev
*idev
;
3012 struct ifmcaddr6
*im
;
3015 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
3017 static inline struct ip6_sf_list
*igmp6_mcf_get_first(struct seq_file
*seq
)
3019 struct ip6_sf_list
*psf
= NULL
;
3020 struct ifmcaddr6
*im
= NULL
;
3021 struct igmp6_mcf_iter_state
*state
= igmp6_mcf_seq_private(seq
);
3022 struct net
*net
= seq_file_net(seq
);
3026 for_each_netdev_rcu(net
, state
->dev
) {
3027 struct inet6_dev
*idev
;
3028 idev
= __in6_dev_get(state
->dev
);
3029 if (unlikely(idev
== NULL
))
3032 im
= rcu_dereference(idev
->mc_list
);
3034 psf
= rcu_dereference(im
->mca_sources
);
3045 static struct ip6_sf_list
*igmp6_mcf_get_next(struct seq_file
*seq
, struct ip6_sf_list
*psf
)
3047 struct igmp6_mcf_iter_state
*state
= igmp6_mcf_seq_private(seq
);
3049 psf
= rcu_dereference(psf
->sf_next
);
3051 state
->im
= rcu_dereference(state
->im
->next
);
3052 while (!state
->im
) {
3053 state
->dev
= next_net_device_rcu(state
->dev
);
3058 state
->idev
= __in6_dev_get(state
->dev
);
3061 state
->im
= rcu_dereference(state
->idev
->mc_list
);
3063 psf
= rcu_dereference(state
->im
->mca_sources
);
3069 static struct ip6_sf_list
*igmp6_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
3071 struct ip6_sf_list
*psf
= igmp6_mcf_get_first(seq
);
3073 while (pos
&& (psf
= igmp6_mcf_get_next(seq
, psf
)) != NULL
)
3075 return pos
? NULL
: psf
;
3078 static void *igmp6_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
3082 return *pos
? igmp6_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
3085 static void *igmp6_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
3087 struct ip6_sf_list
*psf
;
3088 if (v
== SEQ_START_TOKEN
)
3089 psf
= igmp6_mcf_get_first(seq
);
3091 psf
= igmp6_mcf_get_next(seq
, v
);
3096 static void igmp6_mcf_seq_stop(struct seq_file
*seq
, void *v
)
3099 struct igmp6_mcf_iter_state
*state
= igmp6_mcf_seq_private(seq
);
3101 if (likely(state
->im
))
3103 if (likely(state
->idev
))
3110 static int igmp6_mcf_seq_show(struct seq_file
*seq
, void *v
)
3112 struct ip6_sf_list
*psf
= (struct ip6_sf_list
*)v
;
3113 struct igmp6_mcf_iter_state
*state
= igmp6_mcf_seq_private(seq
);
3115 if (v
== SEQ_START_TOKEN
) {
3116 seq_puts(seq
, "Idx Device Multicast Address Source Address INC EXC\n");
3119 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
3120 state
->dev
->ifindex
, state
->dev
->name
,
3121 &state
->im
->mca_addr
,
3123 READ_ONCE(psf
->sf_count
[MCAST_INCLUDE
]),
3124 READ_ONCE(psf
->sf_count
[MCAST_EXCLUDE
]));
3129 static const struct seq_operations igmp6_mcf_seq_ops
= {
3130 .start
= igmp6_mcf_seq_start
,
3131 .next
= igmp6_mcf_seq_next
,
3132 .stop
= igmp6_mcf_seq_stop
,
3133 .show
= igmp6_mcf_seq_show
,
3136 static int __net_init
igmp6_proc_init(struct net
*net
)
3141 if (!proc_create_net("igmp6", 0444, net
->proc_net
, &igmp6_mc_seq_ops
,
3142 sizeof(struct igmp6_mc_iter_state
)))
3144 if (!proc_create_net("mcfilter6", 0444, net
->proc_net
,
3146 sizeof(struct igmp6_mcf_iter_state
)))
3147 goto out_proc_net_igmp6
;
3154 remove_proc_entry("igmp6", net
->proc_net
);
3158 static void __net_exit
igmp6_proc_exit(struct net
*net
)
3160 remove_proc_entry("mcfilter6", net
->proc_net
);
3161 remove_proc_entry("igmp6", net
->proc_net
);
3164 static inline int igmp6_proc_init(struct net
*net
)
3168 static inline void igmp6_proc_exit(struct net
*net
)
3173 static int __net_init
igmp6_net_init(struct net
*net
)
3177 err
= inet_ctl_sock_create(&net
->ipv6
.igmp_sk
, PF_INET6
,
3178 SOCK_RAW
, IPPROTO_ICMPV6
, net
);
3180 pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
3185 inet6_sk(net
->ipv6
.igmp_sk
)->hop_limit
= 1;
3186 net
->ipv6
.igmp_sk
->sk_allocation
= GFP_KERNEL
;
3188 err
= inet_ctl_sock_create(&net
->ipv6
.mc_autojoin_sk
, PF_INET6
,
3189 SOCK_RAW
, IPPROTO_ICMPV6
, net
);
3191 pr_err("Failed to initialize the IGMP6 autojoin socket (err %d)\n",
3193 goto out_sock_create
;
3196 err
= igmp6_proc_init(net
);
3198 goto out_sock_create_autojoin
;
3202 out_sock_create_autojoin
:
3203 inet_ctl_sock_destroy(net
->ipv6
.mc_autojoin_sk
);
3205 inet_ctl_sock_destroy(net
->ipv6
.igmp_sk
);
3210 static void __net_exit
igmp6_net_exit(struct net
*net
)
3212 inet_ctl_sock_destroy(net
->ipv6
.igmp_sk
);
3213 inet_ctl_sock_destroy(net
->ipv6
.mc_autojoin_sk
);
3214 igmp6_proc_exit(net
);
3217 static struct pernet_operations igmp6_net_ops
= {
3218 .init
= igmp6_net_init
,
3219 .exit
= igmp6_net_exit
,
3222 int __init
igmp6_init(void)
3226 err
= register_pernet_subsys(&igmp6_net_ops
);
3230 mld_wq
= create_workqueue("mld");
3232 unregister_pernet_subsys(&igmp6_net_ops
);
3239 int __init
igmp6_late_init(void)
3241 return register_netdevice_notifier(&igmp6_netdev_notifier
);
3244 void igmp6_cleanup(void)
3246 unregister_pernet_subsys(&igmp6_net_ops
);
3247 destroy_workqueue(mld_wq
);
3250 void igmp6_late_cleanup(void)
3252 unregister_netdevice_notifier(&igmp6_netdev_notifier
);