1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Bridge multicast support.
5 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
9 #include <linux/export.h>
10 #include <linux/if_ether.h>
11 #include <linux/igmp.h>
13 #include <linux/jhash.h>
14 #include <linux/kernel.h>
15 #include <linux/log2.h>
16 #include <linux/netdevice.h>
17 #include <linux/netfilter_bridge.h>
18 #include <linux/random.h>
19 #include <linux/rculist.h>
20 #include <linux/skbuff.h>
21 #include <linux/slab.h>
22 #include <linux/timer.h>
23 #include <linux/inetdevice.h>
24 #include <linux/mroute.h>
26 #include <net/switchdev.h>
27 #if IS_ENABLED(CONFIG_IPV6)
28 #include <linux/icmpv6.h>
31 #include <net/ip6_checksum.h>
32 #include <net/addrconf.h>
34 #include <trace/events/bridge.h>
36 #include "br_private.h"
37 #include "br_private_mcast_eht.h"
39 static const struct rhashtable_params br_mdb_rht_params
= {
40 .head_offset
= offsetof(struct net_bridge_mdb_entry
, rhnode
),
41 .key_offset
= offsetof(struct net_bridge_mdb_entry
, addr
),
42 .key_len
= sizeof(struct br_ip
),
43 .automatic_shrinking
= true,
46 static const struct rhashtable_params br_sg_port_rht_params
= {
47 .head_offset
= offsetof(struct net_bridge_port_group
, rhnode
),
48 .key_offset
= offsetof(struct net_bridge_port_group
, key
),
49 .key_len
= sizeof(struct net_bridge_port_group_sg_key
),
50 .automatic_shrinking
= true,
53 static void br_multicast_start_querier(struct net_bridge_mcast
*brmctx
,
54 struct bridge_mcast_own_query
*query
);
55 static void br_ip4_multicast_add_router(struct net_bridge_mcast
*brmctx
,
56 struct net_bridge_mcast_port
*pmctx
);
57 static void br_ip4_multicast_leave_group(struct net_bridge_mcast
*brmctx
,
58 struct net_bridge_mcast_port
*pmctx
,
61 const unsigned char *src
);
62 static void br_multicast_port_group_rexmit(struct timer_list
*t
);
65 br_multicast_rport_del_notify(struct net_bridge_mcast_port
*pmctx
, bool deleted
);
66 static void br_ip6_multicast_add_router(struct net_bridge_mcast
*brmctx
,
67 struct net_bridge_mcast_port
*pmctx
);
68 #if IS_ENABLED(CONFIG_IPV6)
69 static void br_ip6_multicast_leave_group(struct net_bridge_mcast
*brmctx
,
70 struct net_bridge_mcast_port
*pmctx
,
71 const struct in6_addr
*group
,
72 __u16 vid
, const unsigned char *src
);
74 static struct net_bridge_port_group
*
75 __br_multicast_add_group(struct net_bridge_mcast
*brmctx
,
76 struct net_bridge_mcast_port
*pmctx
,
78 const unsigned char *src
,
82 static void br_multicast_find_del_pg(struct net_bridge
*br
,
83 struct net_bridge_port_group
*pg
);
84 static void __br_multicast_stop(struct net_bridge_mcast
*brmctx
);
86 static int br_mc_disabled_update(struct net_device
*dev
, bool value
,
87 struct netlink_ext_ack
*extack
);
89 static struct net_bridge_port_group
*
90 br_sg_port_find(struct net_bridge
*br
,
91 struct net_bridge_port_group_sg_key
*sg_p
)
93 lockdep_assert_held_once(&br
->multicast_lock
);
95 return rhashtable_lookup_fast(&br
->sg_port_tbl
, sg_p
,
96 br_sg_port_rht_params
);
99 static struct net_bridge_mdb_entry
*br_mdb_ip_get_rcu(struct net_bridge
*br
,
102 return rhashtable_lookup(&br
->mdb_hash_tbl
, dst
, br_mdb_rht_params
);
105 struct net_bridge_mdb_entry
*br_mdb_ip_get(struct net_bridge
*br
,
108 struct net_bridge_mdb_entry
*ent
;
110 lockdep_assert_held_once(&br
->multicast_lock
);
113 ent
= rhashtable_lookup(&br
->mdb_hash_tbl
, dst
, br_mdb_rht_params
);
119 static struct net_bridge_mdb_entry
*br_mdb_ip4_get(struct net_bridge
*br
,
120 __be32 dst
, __u16 vid
)
124 memset(&br_dst
, 0, sizeof(br_dst
));
125 br_dst
.dst
.ip4
= dst
;
126 br_dst
.proto
= htons(ETH_P_IP
);
129 return br_mdb_ip_get(br
, &br_dst
);
132 #if IS_ENABLED(CONFIG_IPV6)
133 static struct net_bridge_mdb_entry
*br_mdb_ip6_get(struct net_bridge
*br
,
134 const struct in6_addr
*dst
,
139 memset(&br_dst
, 0, sizeof(br_dst
));
140 br_dst
.dst
.ip6
= *dst
;
141 br_dst
.proto
= htons(ETH_P_IPV6
);
144 return br_mdb_ip_get(br
, &br_dst
);
148 struct net_bridge_mdb_entry
*
149 br_mdb_entry_skb_get(struct net_bridge_mcast
*brmctx
, struct sk_buff
*skb
,
152 struct net_bridge
*br
= brmctx
->br
;
155 if (!br_opt_get(br
, BROPT_MULTICAST_ENABLED
) ||
156 br_multicast_ctx_vlan_global_disabled(brmctx
))
159 if (BR_INPUT_SKB_CB(skb
)->igmp
)
162 memset(&ip
, 0, sizeof(ip
));
163 ip
.proto
= skb
->protocol
;
166 switch (skb
->protocol
) {
167 case htons(ETH_P_IP
):
168 ip
.dst
.ip4
= ip_hdr(skb
)->daddr
;
169 if (brmctx
->multicast_igmp_version
== 3) {
170 struct net_bridge_mdb_entry
*mdb
;
172 ip
.src
.ip4
= ip_hdr(skb
)->saddr
;
173 mdb
= br_mdb_ip_get_rcu(br
, &ip
);
179 #if IS_ENABLED(CONFIG_IPV6)
180 case htons(ETH_P_IPV6
):
181 ip
.dst
.ip6
= ipv6_hdr(skb
)->daddr
;
182 if (brmctx
->multicast_mld_version
== 2) {
183 struct net_bridge_mdb_entry
*mdb
;
185 ip
.src
.ip6
= ipv6_hdr(skb
)->saddr
;
186 mdb
= br_mdb_ip_get_rcu(br
, &ip
);
189 memset(&ip
.src
.ip6
, 0, sizeof(ip
.src
.ip6
));
195 ether_addr_copy(ip
.dst
.mac_addr
, eth_hdr(skb
)->h_dest
);
198 return br_mdb_ip_get_rcu(br
, &ip
);
201 /* IMPORTANT: this function must be used only when the contexts cannot be
202 * passed down (e.g. timer) and must be used for read-only purposes because
203 * the vlan snooping option can change, so it can return any context
204 * (non-vlan or vlan). Its initial intended purpose is to read timer values
205 * from the *current* context based on the option. At worst that could lead
206 * to inconsistent timers when the contexts are changed, i.e. src timer
207 * which needs to re-arm with a specific delay taken from the old context
209 static struct net_bridge_mcast_port
*
210 br_multicast_pg_to_port_ctx(const struct net_bridge_port_group
*pg
)
212 struct net_bridge_mcast_port
*pmctx
= &pg
->key
.port
->multicast_ctx
;
213 struct net_bridge_vlan
*vlan
;
215 lockdep_assert_held_once(&pg
->key
.port
->br
->multicast_lock
);
217 /* if vlan snooping is disabled use the port's multicast context */
218 if (!pg
->key
.addr
.vid
||
219 !br_opt_get(pg
->key
.port
->br
, BROPT_MCAST_VLAN_SNOOPING_ENABLED
))
222 /* locking is tricky here, due to different rules for multicast and
223 * vlans we need to take rcu to find the vlan and make sure it has
224 * the BR_VLFLAG_MCAST_ENABLED flag set, it can only change under
225 * multicast_lock which must be already held here, so the vlan's pmctx
226 * can safely be used on return
229 vlan
= br_vlan_find(nbp_vlan_group_rcu(pg
->key
.port
), pg
->key
.addr
.vid
);
230 if (vlan
&& !br_multicast_port_ctx_vlan_disabled(&vlan
->port_mcast_ctx
))
231 pmctx
= &vlan
->port_mcast_ctx
;
239 static struct net_bridge_mcast_port
*
240 br_multicast_port_vid_to_port_ctx(struct net_bridge_port
*port
, u16 vid
)
242 struct net_bridge_mcast_port
*pmctx
= NULL
;
243 struct net_bridge_vlan
*vlan
;
245 lockdep_assert_held_once(&port
->br
->multicast_lock
);
247 if (!br_opt_get(port
->br
, BROPT_MCAST_VLAN_SNOOPING_ENABLED
))
250 /* Take RCU to access the vlan. */
253 vlan
= br_vlan_find(nbp_vlan_group_rcu(port
), vid
);
254 if (vlan
&& !br_multicast_port_ctx_vlan_disabled(&vlan
->port_mcast_ctx
))
255 pmctx
= &vlan
->port_mcast_ctx
;
262 /* when snooping we need to check if the contexts should be used
263 * in the following order:
264 * - if pmctx is non-NULL (port), check if it should be used
265 * - if pmctx is NULL (bridge), check if brmctx should be used
268 br_multicast_ctx_should_use(const struct net_bridge_mcast
*brmctx
,
269 const struct net_bridge_mcast_port
*pmctx
)
271 if (!netif_running(brmctx
->br
->dev
))
275 return !br_multicast_port_ctx_state_disabled(pmctx
);
277 return !br_multicast_ctx_vlan_disabled(brmctx
);
280 static bool br_port_group_equal(struct net_bridge_port_group
*p
,
281 struct net_bridge_port
*port
,
282 const unsigned char *src
)
284 if (p
->key
.port
!= port
)
287 if (!(port
->flags
& BR_MULTICAST_TO_UNICAST
))
290 return ether_addr_equal(src
, p
->eth_addr
);
293 static void __fwd_add_star_excl(struct net_bridge_mcast_port
*pmctx
,
294 struct net_bridge_port_group
*pg
,
297 struct net_bridge_port_group_sg_key sg_key
;
298 struct net_bridge_port_group
*src_pg
;
299 struct net_bridge_mcast
*brmctx
;
301 memset(&sg_key
, 0, sizeof(sg_key
));
302 brmctx
= br_multicast_port_ctx_get_global(pmctx
);
303 sg_key
.port
= pg
->key
.port
;
304 sg_key
.addr
= *sg_ip
;
305 if (br_sg_port_find(brmctx
->br
, &sg_key
))
308 src_pg
= __br_multicast_add_group(brmctx
, pmctx
,
310 MCAST_INCLUDE
, false, false);
311 if (IS_ERR_OR_NULL(src_pg
) ||
312 src_pg
->rt_protocol
!= RTPROT_KERNEL
)
315 src_pg
->flags
|= MDB_PG_FLAGS_STAR_EXCL
;
318 static void __fwd_del_star_excl(struct net_bridge_port_group
*pg
,
321 struct net_bridge_port_group_sg_key sg_key
;
322 struct net_bridge
*br
= pg
->key
.port
->br
;
323 struct net_bridge_port_group
*src_pg
;
325 memset(&sg_key
, 0, sizeof(sg_key
));
326 sg_key
.port
= pg
->key
.port
;
327 sg_key
.addr
= *sg_ip
;
328 src_pg
= br_sg_port_find(br
, &sg_key
);
329 if (!src_pg
|| !(src_pg
->flags
& MDB_PG_FLAGS_STAR_EXCL
) ||
330 src_pg
->rt_protocol
!= RTPROT_KERNEL
)
333 br_multicast_find_del_pg(br
, src_pg
);
336 /* When a port group transitions to (or is added as) EXCLUDE we need to add it
337 * to all other ports' S,G entries which are not blocked by the current group
338 * for proper replication, the assumption is that any S,G blocked entries
339 * are already added so the S,G,port lookup should skip them.
340 * When a port group transitions from EXCLUDE -> INCLUDE mode or is being
341 * deleted we need to remove it from all ports' S,G entries where it was
342 * automatically installed before (i.e. where it's MDB_PG_FLAGS_STAR_EXCL).
344 void br_multicast_star_g_handle_mode(struct net_bridge_port_group
*pg
,
347 struct net_bridge
*br
= pg
->key
.port
->br
;
348 struct net_bridge_port_group
*pg_lst
;
349 struct net_bridge_mcast_port
*pmctx
;
350 struct net_bridge_mdb_entry
*mp
;
353 if (WARN_ON(!br_multicast_is_star_g(&pg
->key
.addr
)))
356 mp
= br_mdb_ip_get(br
, &pg
->key
.addr
);
359 pmctx
= br_multicast_pg_to_port_ctx(pg
);
363 memset(&sg_ip
, 0, sizeof(sg_ip
));
364 sg_ip
= pg
->key
.addr
;
366 for (pg_lst
= mlock_dereference(mp
->ports
, br
);
368 pg_lst
= mlock_dereference(pg_lst
->next
, br
)) {
369 struct net_bridge_group_src
*src_ent
;
373 hlist_for_each_entry(src_ent
, &pg_lst
->src_list
, node
) {
374 if (!(src_ent
->flags
& BR_SGRP_F_INSTALLED
))
376 sg_ip
.src
= src_ent
->addr
.src
;
377 switch (filter_mode
) {
379 __fwd_del_star_excl(pg
, &sg_ip
);
382 __fwd_add_star_excl(pmctx
, pg
, &sg_ip
);
389 /* called when adding a new S,G with host_joined == false by default */
390 static void br_multicast_sg_host_state(struct net_bridge_mdb_entry
*star_mp
,
391 struct net_bridge_port_group
*sg
)
393 struct net_bridge_mdb_entry
*sg_mp
;
395 if (WARN_ON(!br_multicast_is_star_g(&star_mp
->addr
)))
397 if (!star_mp
->host_joined
)
400 sg_mp
= br_mdb_ip_get(star_mp
->br
, &sg
->key
.addr
);
403 sg_mp
->host_joined
= true;
406 /* set the host_joined state of all of *,G's S,G entries */
407 static void br_multicast_star_g_host_state(struct net_bridge_mdb_entry
*star_mp
)
409 struct net_bridge
*br
= star_mp
->br
;
410 struct net_bridge_mdb_entry
*sg_mp
;
411 struct net_bridge_port_group
*pg
;
414 if (WARN_ON(!br_multicast_is_star_g(&star_mp
->addr
)))
417 memset(&sg_ip
, 0, sizeof(sg_ip
));
418 sg_ip
= star_mp
->addr
;
419 for (pg
= mlock_dereference(star_mp
->ports
, br
);
421 pg
= mlock_dereference(pg
->next
, br
)) {
422 struct net_bridge_group_src
*src_ent
;
424 hlist_for_each_entry(src_ent
, &pg
->src_list
, node
) {
425 if (!(src_ent
->flags
& BR_SGRP_F_INSTALLED
))
427 sg_ip
.src
= src_ent
->addr
.src
;
428 sg_mp
= br_mdb_ip_get(br
, &sg_ip
);
431 sg_mp
->host_joined
= star_mp
->host_joined
;
436 static void br_multicast_sg_del_exclude_ports(struct net_bridge_mdb_entry
*sgmp
)
438 struct net_bridge_port_group __rcu
**pp
;
439 struct net_bridge_port_group
*p
;
441 /* *,G exclude ports are only added to S,G entries */
442 if (WARN_ON(br_multicast_is_star_g(&sgmp
->addr
)))
445 /* we need the STAR_EXCLUDE ports if there are non-STAR_EXCLUDE ports
446 * we should ignore perm entries since they're managed by user-space
448 for (pp
= &sgmp
->ports
;
449 (p
= mlock_dereference(*pp
, sgmp
->br
)) != NULL
;
451 if (!(p
->flags
& (MDB_PG_FLAGS_STAR_EXCL
|
452 MDB_PG_FLAGS_PERMANENT
)))
455 /* currently the host can only have joined the *,G which means
456 * we treat it as EXCLUDE {}, so for an S,G it's considered a
457 * STAR_EXCLUDE entry and we can safely leave it
459 sgmp
->host_joined
= false;
461 for (pp
= &sgmp
->ports
;
462 (p
= mlock_dereference(*pp
, sgmp
->br
)) != NULL
;) {
463 if (!(p
->flags
& MDB_PG_FLAGS_PERMANENT
))
464 br_multicast_del_pg(sgmp
, p
, pp
);
470 void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry
*star_mp
,
471 struct net_bridge_port_group
*sg
)
473 struct net_bridge_port_group_sg_key sg_key
;
474 struct net_bridge
*br
= star_mp
->br
;
475 struct net_bridge_mcast_port
*pmctx
;
476 struct net_bridge_port_group
*pg
;
477 struct net_bridge_mcast
*brmctx
;
479 if (WARN_ON(br_multicast_is_star_g(&sg
->key
.addr
)))
481 if (WARN_ON(!br_multicast_is_star_g(&star_mp
->addr
)))
484 br_multicast_sg_host_state(star_mp
, sg
);
485 memset(&sg_key
, 0, sizeof(sg_key
));
486 sg_key
.addr
= sg
->key
.addr
;
487 /* we need to add all exclude ports to the S,G */
488 for (pg
= mlock_dereference(star_mp
->ports
, br
);
490 pg
= mlock_dereference(pg
->next
, br
)) {
491 struct net_bridge_port_group
*src_pg
;
493 if (pg
== sg
|| pg
->filter_mode
== MCAST_INCLUDE
)
496 sg_key
.port
= pg
->key
.port
;
497 if (br_sg_port_find(br
, &sg_key
))
500 pmctx
= br_multicast_pg_to_port_ctx(pg
);
503 brmctx
= br_multicast_port_ctx_get_global(pmctx
);
505 src_pg
= __br_multicast_add_group(brmctx
, pmctx
,
508 MCAST_INCLUDE
, false, false);
509 if (IS_ERR_OR_NULL(src_pg
) ||
510 src_pg
->rt_protocol
!= RTPROT_KERNEL
)
512 src_pg
->flags
|= MDB_PG_FLAGS_STAR_EXCL
;
516 static void br_multicast_fwd_src_add(struct net_bridge_group_src
*src
)
518 struct net_bridge_mdb_entry
*star_mp
;
519 struct net_bridge_mcast_port
*pmctx
;
520 struct net_bridge_port_group
*sg
;
521 struct net_bridge_mcast
*brmctx
;
524 if (src
->flags
& BR_SGRP_F_INSTALLED
)
527 memset(&sg_ip
, 0, sizeof(sg_ip
));
528 pmctx
= br_multicast_pg_to_port_ctx(src
->pg
);
531 brmctx
= br_multicast_port_ctx_get_global(pmctx
);
532 sg_ip
= src
->pg
->key
.addr
;
533 sg_ip
.src
= src
->addr
.src
;
535 sg
= __br_multicast_add_group(brmctx
, pmctx
, &sg_ip
,
536 src
->pg
->eth_addr
, MCAST_INCLUDE
, false,
537 !timer_pending(&src
->timer
));
538 if (IS_ERR_OR_NULL(sg
))
540 src
->flags
|= BR_SGRP_F_INSTALLED
;
541 sg
->flags
&= ~MDB_PG_FLAGS_STAR_EXCL
;
543 /* if it was added by user-space as perm we can skip next steps */
544 if (sg
->rt_protocol
!= RTPROT_KERNEL
&&
545 (sg
->flags
& MDB_PG_FLAGS_PERMANENT
))
548 /* the kernel is now responsible for removing this S,G */
549 del_timer(&sg
->timer
);
550 star_mp
= br_mdb_ip_get(src
->br
, &src
->pg
->key
.addr
);
554 br_multicast_sg_add_exclude_ports(star_mp
, sg
);
557 static void br_multicast_fwd_src_remove(struct net_bridge_group_src
*src
,
560 struct net_bridge_port_group
*p
, *pg
= src
->pg
;
561 struct net_bridge_port_group __rcu
**pp
;
562 struct net_bridge_mdb_entry
*mp
;
565 memset(&sg_ip
, 0, sizeof(sg_ip
));
566 sg_ip
= pg
->key
.addr
;
567 sg_ip
.src
= src
->addr
.src
;
569 mp
= br_mdb_ip_get(src
->br
, &sg_ip
);
573 for (pp
= &mp
->ports
;
574 (p
= mlock_dereference(*pp
, src
->br
)) != NULL
;
576 if (!br_port_group_equal(p
, pg
->key
.port
, pg
->eth_addr
))
579 if (p
->rt_protocol
!= RTPROT_KERNEL
&&
580 (p
->flags
& MDB_PG_FLAGS_PERMANENT
) &&
581 !(src
->flags
& BR_SGRP_F_USER_ADDED
))
585 p
->flags
|= MDB_PG_FLAGS_FAST_LEAVE
;
586 br_multicast_del_pg(mp
, p
, pp
);
589 src
->flags
&= ~BR_SGRP_F_INSTALLED
;
592 /* install S,G and based on src's timer enable or disable forwarding */
593 static void br_multicast_fwd_src_handle(struct net_bridge_group_src
*src
)
595 struct net_bridge_port_group_sg_key sg_key
;
596 struct net_bridge_port_group
*sg
;
599 br_multicast_fwd_src_add(src
);
601 memset(&sg_key
, 0, sizeof(sg_key
));
602 sg_key
.addr
= src
->pg
->key
.addr
;
603 sg_key
.addr
.src
= src
->addr
.src
;
604 sg_key
.port
= src
->pg
->key
.port
;
606 sg
= br_sg_port_find(src
->br
, &sg_key
);
607 if (!sg
|| (sg
->flags
& MDB_PG_FLAGS_PERMANENT
))
610 old_flags
= sg
->flags
;
611 if (timer_pending(&src
->timer
))
612 sg
->flags
&= ~MDB_PG_FLAGS_BLOCKED
;
614 sg
->flags
|= MDB_PG_FLAGS_BLOCKED
;
616 if (old_flags
!= sg
->flags
) {
617 struct net_bridge_mdb_entry
*sg_mp
;
619 sg_mp
= br_mdb_ip_get(src
->br
, &sg_key
.addr
);
622 br_mdb_notify(src
->br
->dev
, sg_mp
, sg
, RTM_NEWMDB
);
626 static void br_multicast_destroy_mdb_entry(struct net_bridge_mcast_gc
*gc
)
628 struct net_bridge_mdb_entry
*mp
;
630 mp
= container_of(gc
, struct net_bridge_mdb_entry
, mcast_gc
);
631 WARN_ON(!hlist_unhashed(&mp
->mdb_node
));
634 timer_shutdown_sync(&mp
->timer
);
638 static void br_multicast_del_mdb_entry(struct net_bridge_mdb_entry
*mp
)
640 struct net_bridge
*br
= mp
->br
;
642 rhashtable_remove_fast(&br
->mdb_hash_tbl
, &mp
->rhnode
,
644 hlist_del_init_rcu(&mp
->mdb_node
);
645 hlist_add_head(&mp
->mcast_gc
.gc_node
, &br
->mcast_gc_list
);
646 queue_work(system_long_wq
, &br
->mcast_gc_work
);
649 static void br_multicast_group_expired(struct timer_list
*t
)
651 struct net_bridge_mdb_entry
*mp
= from_timer(mp
, t
, timer
);
652 struct net_bridge
*br
= mp
->br
;
654 spin_lock(&br
->multicast_lock
);
655 if (hlist_unhashed(&mp
->mdb_node
) || !netif_running(br
->dev
) ||
656 timer_pending(&mp
->timer
))
659 br_multicast_host_leave(mp
, true);
663 br_multicast_del_mdb_entry(mp
);
665 spin_unlock(&br
->multicast_lock
);
668 static void br_multicast_destroy_group_src(struct net_bridge_mcast_gc
*gc
)
670 struct net_bridge_group_src
*src
;
672 src
= container_of(gc
, struct net_bridge_group_src
, mcast_gc
);
673 WARN_ON(!hlist_unhashed(&src
->node
));
675 timer_shutdown_sync(&src
->timer
);
679 void __br_multicast_del_group_src(struct net_bridge_group_src
*src
)
681 struct net_bridge
*br
= src
->pg
->key
.port
->br
;
683 hlist_del_init_rcu(&src
->node
);
685 hlist_add_head(&src
->mcast_gc
.gc_node
, &br
->mcast_gc_list
);
686 queue_work(system_long_wq
, &br
->mcast_gc_work
);
689 void br_multicast_del_group_src(struct net_bridge_group_src
*src
,
692 br_multicast_fwd_src_remove(src
, fastleave
);
693 __br_multicast_del_group_src(src
);
697 br_multicast_port_ngroups_inc_one(struct net_bridge_mcast_port
*pmctx
,
698 struct netlink_ext_ack
*extack
,
701 u32 max
= READ_ONCE(pmctx
->mdb_max_entries
);
702 u32 n
= READ_ONCE(pmctx
->mdb_n_entries
);
704 if (max
&& n
>= max
) {
705 NL_SET_ERR_MSG_FMT_MOD(extack
, "%s is already in %u groups, and mcast_max_groups=%u",
710 WRITE_ONCE(pmctx
->mdb_n_entries
, n
+ 1);
714 static void br_multicast_port_ngroups_dec_one(struct net_bridge_mcast_port
*pmctx
)
716 u32 n
= READ_ONCE(pmctx
->mdb_n_entries
);
718 WARN_ON_ONCE(n
== 0);
719 WRITE_ONCE(pmctx
->mdb_n_entries
, n
- 1);
722 static int br_multicast_port_ngroups_inc(struct net_bridge_port
*port
,
723 const struct br_ip
*group
,
724 struct netlink_ext_ack
*extack
)
726 struct net_bridge_mcast_port
*pmctx
;
729 lockdep_assert_held_once(&port
->br
->multicast_lock
);
731 /* Always count on the port context. */
732 err
= br_multicast_port_ngroups_inc_one(&port
->multicast_ctx
, extack
,
735 trace_br_mdb_full(port
->dev
, group
);
739 /* Only count on the VLAN context if VID is given, and if snooping on
740 * that VLAN is enabled.
745 pmctx
= br_multicast_port_vid_to_port_ctx(port
, group
->vid
);
749 err
= br_multicast_port_ngroups_inc_one(pmctx
, extack
, "Port-VLAN");
751 trace_br_mdb_full(port
->dev
, group
);
758 br_multicast_port_ngroups_dec_one(&port
->multicast_ctx
);
762 static void br_multicast_port_ngroups_dec(struct net_bridge_port
*port
, u16 vid
)
764 struct net_bridge_mcast_port
*pmctx
;
766 lockdep_assert_held_once(&port
->br
->multicast_lock
);
769 pmctx
= br_multicast_port_vid_to_port_ctx(port
, vid
);
771 br_multicast_port_ngroups_dec_one(pmctx
);
773 br_multicast_port_ngroups_dec_one(&port
->multicast_ctx
);
776 u32
br_multicast_ngroups_get(const struct net_bridge_mcast_port
*pmctx
)
778 return READ_ONCE(pmctx
->mdb_n_entries
);
781 void br_multicast_ngroups_set_max(struct net_bridge_mcast_port
*pmctx
, u32 max
)
783 WRITE_ONCE(pmctx
->mdb_max_entries
, max
);
786 u32
br_multicast_ngroups_get_max(const struct net_bridge_mcast_port
*pmctx
)
788 return READ_ONCE(pmctx
->mdb_max_entries
);
791 static void br_multicast_destroy_port_group(struct net_bridge_mcast_gc
*gc
)
793 struct net_bridge_port_group
*pg
;
795 pg
= container_of(gc
, struct net_bridge_port_group
, mcast_gc
);
796 WARN_ON(!hlist_unhashed(&pg
->mglist
));
797 WARN_ON(!hlist_empty(&pg
->src_list
));
799 timer_shutdown_sync(&pg
->rexmit_timer
);
800 timer_shutdown_sync(&pg
->timer
);
804 void br_multicast_del_pg(struct net_bridge_mdb_entry
*mp
,
805 struct net_bridge_port_group
*pg
,
806 struct net_bridge_port_group __rcu
**pp
)
808 struct net_bridge
*br
= pg
->key
.port
->br
;
809 struct net_bridge_group_src
*ent
;
810 struct hlist_node
*tmp
;
812 rcu_assign_pointer(*pp
, pg
->next
);
813 hlist_del_init(&pg
->mglist
);
814 br_multicast_eht_clean_sets(pg
);
815 hlist_for_each_entry_safe(ent
, tmp
, &pg
->src_list
, node
)
816 br_multicast_del_group_src(ent
, false);
817 br_mdb_notify(br
->dev
, mp
, pg
, RTM_DELMDB
);
818 if (!br_multicast_is_star_g(&mp
->addr
)) {
819 rhashtable_remove_fast(&br
->sg_port_tbl
, &pg
->rhnode
,
820 br_sg_port_rht_params
);
821 br_multicast_sg_del_exclude_ports(mp
);
823 br_multicast_star_g_handle_mode(pg
, MCAST_INCLUDE
);
825 br_multicast_port_ngroups_dec(pg
->key
.port
, pg
->key
.addr
.vid
);
826 hlist_add_head(&pg
->mcast_gc
.gc_node
, &br
->mcast_gc_list
);
827 queue_work(system_long_wq
, &br
->mcast_gc_work
);
829 if (!mp
->ports
&& !mp
->host_joined
&& netif_running(br
->dev
))
830 mod_timer(&mp
->timer
, jiffies
);
833 static void br_multicast_find_del_pg(struct net_bridge
*br
,
834 struct net_bridge_port_group
*pg
)
836 struct net_bridge_port_group __rcu
**pp
;
837 struct net_bridge_mdb_entry
*mp
;
838 struct net_bridge_port_group
*p
;
840 mp
= br_mdb_ip_get(br
, &pg
->key
.addr
);
844 for (pp
= &mp
->ports
;
845 (p
= mlock_dereference(*pp
, br
)) != NULL
;
850 br_multicast_del_pg(mp
, pg
, pp
);
857 static void br_multicast_port_group_expired(struct timer_list
*t
)
859 struct net_bridge_port_group
*pg
= from_timer(pg
, t
, timer
);
860 struct net_bridge_group_src
*src_ent
;
861 struct net_bridge
*br
= pg
->key
.port
->br
;
862 struct hlist_node
*tmp
;
865 spin_lock(&br
->multicast_lock
);
866 if (!netif_running(br
->dev
) || timer_pending(&pg
->timer
) ||
867 hlist_unhashed(&pg
->mglist
) || pg
->flags
& MDB_PG_FLAGS_PERMANENT
)
870 changed
= !!(pg
->filter_mode
== MCAST_EXCLUDE
);
871 pg
->filter_mode
= MCAST_INCLUDE
;
872 hlist_for_each_entry_safe(src_ent
, tmp
, &pg
->src_list
, node
) {
873 if (!timer_pending(&src_ent
->timer
)) {
874 br_multicast_del_group_src(src_ent
, false);
879 if (hlist_empty(&pg
->src_list
)) {
880 br_multicast_find_del_pg(br
, pg
);
881 } else if (changed
) {
882 struct net_bridge_mdb_entry
*mp
= br_mdb_ip_get(br
, &pg
->key
.addr
);
884 if (changed
&& br_multicast_is_star_g(&pg
->key
.addr
))
885 br_multicast_star_g_handle_mode(pg
, MCAST_INCLUDE
);
889 br_mdb_notify(br
->dev
, mp
, pg
, RTM_NEWMDB
);
892 spin_unlock(&br
->multicast_lock
);
895 static void br_multicast_gc(struct hlist_head
*head
)
897 struct net_bridge_mcast_gc
*gcent
;
898 struct hlist_node
*tmp
;
900 hlist_for_each_entry_safe(gcent
, tmp
, head
, gc_node
) {
901 hlist_del_init(&gcent
->gc_node
);
902 gcent
->destroy(gcent
);
906 static void __br_multicast_query_handle_vlan(struct net_bridge_mcast
*brmctx
,
907 struct net_bridge_mcast_port
*pmctx
,
910 struct net_bridge_vlan
*vlan
= NULL
;
912 if (pmctx
&& br_multicast_port_ctx_is_vlan(pmctx
))
914 else if (br_multicast_ctx_is_vlan(brmctx
))
917 if (vlan
&& !(vlan
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
)) {
920 if (br_vlan_get_proto(brmctx
->br
->dev
, &vlan_proto
) != 0)
922 __vlan_hwaccel_put_tag(skb
, htons(vlan_proto
), vlan
->vid
);
926 static struct sk_buff
*br_ip4_multicast_alloc_query(struct net_bridge_mcast
*brmctx
,
927 struct net_bridge_mcast_port
*pmctx
,
928 struct net_bridge_port_group
*pg
,
929 __be32 ip_dst
, __be32 group
,
930 bool with_srcs
, bool over_lmqt
,
931 u8 sflag
, u8
*igmp_type
,
934 struct net_bridge_port
*p
= pg
? pg
->key
.port
: NULL
;
935 struct net_bridge_group_src
*ent
;
936 size_t pkt_size
, igmp_hdr_size
;
937 unsigned long now
= jiffies
;
938 struct igmpv3_query
*ihv3
;
939 void *csum_start
= NULL
;
940 __sum16
*csum
= NULL
;
948 igmp_hdr_size
= sizeof(*ih
);
949 if (brmctx
->multicast_igmp_version
== 3) {
950 igmp_hdr_size
= sizeof(*ihv3
);
951 if (pg
&& with_srcs
) {
952 lmqt
= now
+ (brmctx
->multicast_last_member_interval
*
953 brmctx
->multicast_last_member_count
);
954 hlist_for_each_entry(ent
, &pg
->src_list
, node
) {
955 if (over_lmqt
== time_after(ent
->timer
.expires
,
957 ent
->src_query_rexmit_cnt
> 0)
963 igmp_hdr_size
+= lmqt_srcs
* sizeof(__be32
);
967 pkt_size
= sizeof(*eth
) + sizeof(*iph
) + 4 + igmp_hdr_size
;
968 if ((p
&& pkt_size
> p
->dev
->mtu
) ||
969 pkt_size
> brmctx
->br
->dev
->mtu
)
972 skb
= netdev_alloc_skb_ip_align(brmctx
->br
->dev
, pkt_size
);
976 __br_multicast_query_handle_vlan(brmctx
, pmctx
, skb
);
977 skb
->protocol
= htons(ETH_P_IP
);
979 skb_reset_mac_header(skb
);
982 ether_addr_copy(eth
->h_source
, brmctx
->br
->dev
->dev_addr
);
983 ip_eth_mc_map(ip_dst
, eth
->h_dest
);
984 eth
->h_proto
= htons(ETH_P_IP
);
985 skb_put(skb
, sizeof(*eth
));
987 skb_set_network_header(skb
, skb
->len
);
989 iph
->tot_len
= htons(pkt_size
- sizeof(*eth
));
995 iph
->frag_off
= htons(IP_DF
);
997 iph
->protocol
= IPPROTO_IGMP
;
998 iph
->saddr
= br_opt_get(brmctx
->br
, BROPT_MULTICAST_QUERY_USE_IFADDR
) ?
999 inet_select_addr(brmctx
->br
->dev
, 0, RT_SCOPE_LINK
) : 0;
1000 iph
->daddr
= ip_dst
;
1001 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
1002 ((u8
*)&iph
[1])[1] = 4;
1003 ((u8
*)&iph
[1])[2] = 0;
1004 ((u8
*)&iph
[1])[3] = 0;
1008 skb_set_transport_header(skb
, skb
->len
);
1009 *igmp_type
= IGMP_HOST_MEMBERSHIP_QUERY
;
1011 switch (brmctx
->multicast_igmp_version
) {
1014 ih
->type
= IGMP_HOST_MEMBERSHIP_QUERY
;
1015 ih
->code
= (group
? brmctx
->multicast_last_member_interval
:
1016 brmctx
->multicast_query_response_interval
) /
1017 (HZ
/ IGMP_TIMER_SCALE
);
1021 csum_start
= (void *)ih
;
1024 ihv3
= igmpv3_query_hdr(skb
);
1025 ihv3
->type
= IGMP_HOST_MEMBERSHIP_QUERY
;
1026 ihv3
->code
= (group
? brmctx
->multicast_last_member_interval
:
1027 brmctx
->multicast_query_response_interval
) /
1028 (HZ
/ IGMP_TIMER_SCALE
);
1029 ihv3
->group
= group
;
1030 ihv3
->qqic
= brmctx
->multicast_query_interval
/ HZ
;
1031 ihv3
->nsrcs
= htons(lmqt_srcs
);
1033 ihv3
->suppress
= sflag
;
1037 csum_start
= (void *)ihv3
;
1038 if (!pg
|| !with_srcs
)
1042 hlist_for_each_entry(ent
, &pg
->src_list
, node
) {
1043 if (over_lmqt
== time_after(ent
->timer
.expires
,
1045 ent
->src_query_rexmit_cnt
> 0) {
1046 ihv3
->srcs
[lmqt_srcs
++] = ent
->addr
.src
.ip4
;
1047 ent
->src_query_rexmit_cnt
--;
1048 if (need_rexmit
&& ent
->src_query_rexmit_cnt
)
1049 *need_rexmit
= true;
1052 if (WARN_ON(lmqt_srcs
!= ntohs(ihv3
->nsrcs
))) {
1059 if (WARN_ON(!csum
|| !csum_start
)) {
1064 *csum
= ip_compute_csum(csum_start
, igmp_hdr_size
);
1065 skb_put(skb
, igmp_hdr_size
);
1066 __skb_pull(skb
, sizeof(*eth
));
1072 #if IS_ENABLED(CONFIG_IPV6)
1073 static struct sk_buff
*br_ip6_multicast_alloc_query(struct net_bridge_mcast
*brmctx
,
1074 struct net_bridge_mcast_port
*pmctx
,
1075 struct net_bridge_port_group
*pg
,
1076 const struct in6_addr
*ip6_dst
,
1077 const struct in6_addr
*group
,
1078 bool with_srcs
, bool over_llqt
,
1079 u8 sflag
, u8
*igmp_type
,
1082 struct net_bridge_port
*p
= pg
? pg
->key
.port
: NULL
;
1083 struct net_bridge_group_src
*ent
;
1084 size_t pkt_size
, mld_hdr_size
;
1085 unsigned long now
= jiffies
;
1086 struct mld2_query
*mld2q
;
1087 void *csum_start
= NULL
;
1088 unsigned long interval
;
1089 __sum16
*csum
= NULL
;
1090 struct ipv6hdr
*ip6h
;
1091 struct mld_msg
*mldq
;
1092 struct sk_buff
*skb
;
1098 mld_hdr_size
= sizeof(*mldq
);
1099 if (brmctx
->multicast_mld_version
== 2) {
1100 mld_hdr_size
= sizeof(*mld2q
);
1101 if (pg
&& with_srcs
) {
1102 llqt
= now
+ (brmctx
->multicast_last_member_interval
*
1103 brmctx
->multicast_last_member_count
);
1104 hlist_for_each_entry(ent
, &pg
->src_list
, node
) {
1105 if (over_llqt
== time_after(ent
->timer
.expires
,
1107 ent
->src_query_rexmit_cnt
> 0)
1113 mld_hdr_size
+= llqt_srcs
* sizeof(struct in6_addr
);
1117 pkt_size
= sizeof(*eth
) + sizeof(*ip6h
) + 8 + mld_hdr_size
;
1118 if ((p
&& pkt_size
> p
->dev
->mtu
) ||
1119 pkt_size
> brmctx
->br
->dev
->mtu
)
1122 skb
= netdev_alloc_skb_ip_align(brmctx
->br
->dev
, pkt_size
);
1126 __br_multicast_query_handle_vlan(brmctx
, pmctx
, skb
);
1127 skb
->protocol
= htons(ETH_P_IPV6
);
1129 /* Ethernet header */
1130 skb_reset_mac_header(skb
);
1133 ether_addr_copy(eth
->h_source
, brmctx
->br
->dev
->dev_addr
);
1134 eth
->h_proto
= htons(ETH_P_IPV6
);
1135 skb_put(skb
, sizeof(*eth
));
1137 /* IPv6 header + HbH option */
1138 skb_set_network_header(skb
, skb
->len
);
1139 ip6h
= ipv6_hdr(skb
);
1141 *(__force __be32
*)ip6h
= htonl(0x60000000);
1142 ip6h
->payload_len
= htons(8 + mld_hdr_size
);
1143 ip6h
->nexthdr
= IPPROTO_HOPOPTS
;
1144 ip6h
->hop_limit
= 1;
1145 ip6h
->daddr
= *ip6_dst
;
1146 if (ipv6_dev_get_saddr(dev_net(brmctx
->br
->dev
), brmctx
->br
->dev
,
1147 &ip6h
->daddr
, 0, &ip6h
->saddr
)) {
1149 br_opt_toggle(brmctx
->br
, BROPT_HAS_IPV6_ADDR
, false);
1153 br_opt_toggle(brmctx
->br
, BROPT_HAS_IPV6_ADDR
, true);
1154 ipv6_eth_mc_map(&ip6h
->daddr
, eth
->h_dest
);
1156 hopopt
= (u8
*)(ip6h
+ 1);
1157 hopopt
[0] = IPPROTO_ICMPV6
; /* next hdr */
1158 hopopt
[1] = 0; /* length of HbH */
1159 hopopt
[2] = IPV6_TLV_ROUTERALERT
; /* Router Alert */
1160 hopopt
[3] = 2; /* Length of RA Option */
1161 hopopt
[4] = 0; /* Type = 0x0000 (MLD) */
1163 hopopt
[6] = IPV6_TLV_PAD1
; /* Pad1 */
1164 hopopt
[7] = IPV6_TLV_PAD1
; /* Pad1 */
1166 skb_put(skb
, sizeof(*ip6h
) + 8);
1169 skb_set_transport_header(skb
, skb
->len
);
1170 interval
= ipv6_addr_any(group
) ?
1171 brmctx
->multicast_query_response_interval
:
1172 brmctx
->multicast_last_member_interval
;
1173 *igmp_type
= ICMPV6_MGM_QUERY
;
1174 switch (brmctx
->multicast_mld_version
) {
1176 mldq
= (struct mld_msg
*)icmp6_hdr(skb
);
1177 mldq
->mld_type
= ICMPV6_MGM_QUERY
;
1179 mldq
->mld_cksum
= 0;
1180 mldq
->mld_maxdelay
= htons((u16
)jiffies_to_msecs(interval
));
1181 mldq
->mld_reserved
= 0;
1182 mldq
->mld_mca
= *group
;
1183 csum
= &mldq
->mld_cksum
;
1184 csum_start
= (void *)mldq
;
1187 mld2q
= (struct mld2_query
*)icmp6_hdr(skb
);
1188 mld2q
->mld2q_mrc
= htons((u16
)jiffies_to_msecs(interval
));
1189 mld2q
->mld2q_type
= ICMPV6_MGM_QUERY
;
1190 mld2q
->mld2q_code
= 0;
1191 mld2q
->mld2q_cksum
= 0;
1192 mld2q
->mld2q_resv1
= 0;
1193 mld2q
->mld2q_resv2
= 0;
1194 mld2q
->mld2q_suppress
= sflag
;
1195 mld2q
->mld2q_qrv
= 2;
1196 mld2q
->mld2q_nsrcs
= htons(llqt_srcs
);
1197 mld2q
->mld2q_qqic
= brmctx
->multicast_query_interval
/ HZ
;
1198 mld2q
->mld2q_mca
= *group
;
1199 csum
= &mld2q
->mld2q_cksum
;
1200 csum_start
= (void *)mld2q
;
1201 if (!pg
|| !with_srcs
)
1205 hlist_for_each_entry(ent
, &pg
->src_list
, node
) {
1206 if (over_llqt
== time_after(ent
->timer
.expires
,
1208 ent
->src_query_rexmit_cnt
> 0) {
1209 mld2q
->mld2q_srcs
[llqt_srcs
++] = ent
->addr
.src
.ip6
;
1210 ent
->src_query_rexmit_cnt
--;
1211 if (need_rexmit
&& ent
->src_query_rexmit_cnt
)
1212 *need_rexmit
= true;
1215 if (WARN_ON(llqt_srcs
!= ntohs(mld2q
->mld2q_nsrcs
))) {
1222 if (WARN_ON(!csum
|| !csum_start
)) {
1227 *csum
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
, mld_hdr_size
,
1229 csum_partial(csum_start
, mld_hdr_size
, 0));
1230 skb_put(skb
, mld_hdr_size
);
1231 __skb_pull(skb
, sizeof(*eth
));
1238 static struct sk_buff
*br_multicast_alloc_query(struct net_bridge_mcast
*brmctx
,
1239 struct net_bridge_mcast_port
*pmctx
,
1240 struct net_bridge_port_group
*pg
,
1241 struct br_ip
*ip_dst
,
1242 struct br_ip
*group
,
1243 bool with_srcs
, bool over_lmqt
,
1244 u8 sflag
, u8
*igmp_type
,
1249 switch (group
->proto
) {
1250 case htons(ETH_P_IP
):
1251 ip4_dst
= ip_dst
? ip_dst
->dst
.ip4
: htonl(INADDR_ALLHOSTS_GROUP
);
1252 return br_ip4_multicast_alloc_query(brmctx
, pmctx
, pg
,
1253 ip4_dst
, group
->dst
.ip4
,
1254 with_srcs
, over_lmqt
,
1257 #if IS_ENABLED(CONFIG_IPV6)
1258 case htons(ETH_P_IPV6
): {
1259 struct in6_addr ip6_dst
;
1262 ip6_dst
= ip_dst
->dst
.ip6
;
1264 ipv6_addr_set(&ip6_dst
, htonl(0xff020000), 0, 0,
1267 return br_ip6_multicast_alloc_query(brmctx
, pmctx
, pg
,
1268 &ip6_dst
, &group
->dst
.ip6
,
1269 with_srcs
, over_lmqt
,
1278 struct net_bridge_mdb_entry
*br_multicast_new_group(struct net_bridge
*br
,
1279 struct br_ip
*group
)
1281 struct net_bridge_mdb_entry
*mp
;
1284 mp
= br_mdb_ip_get(br
, group
);
1288 if (atomic_read(&br
->mdb_hash_tbl
.nelems
) >= br
->hash_max
) {
1289 trace_br_mdb_full(br
->dev
, group
);
1290 br_mc_disabled_update(br
->dev
, false, NULL
);
1291 br_opt_toggle(br
, BROPT_MULTICAST_ENABLED
, false);
1292 return ERR_PTR(-E2BIG
);
1295 mp
= kzalloc(sizeof(*mp
), GFP_ATOMIC
);
1297 return ERR_PTR(-ENOMEM
);
1301 mp
->mcast_gc
.destroy
= br_multicast_destroy_mdb_entry
;
1302 timer_setup(&mp
->timer
, br_multicast_group_expired
, 0);
1303 err
= rhashtable_lookup_insert_fast(&br
->mdb_hash_tbl
, &mp
->rhnode
,
1309 hlist_add_head_rcu(&mp
->mdb_node
, &br
->mdb_list
);
1315 static void br_multicast_group_src_expired(struct timer_list
*t
)
1317 struct net_bridge_group_src
*src
= from_timer(src
, t
, timer
);
1318 struct net_bridge_port_group
*pg
;
1319 struct net_bridge
*br
= src
->br
;
1321 spin_lock(&br
->multicast_lock
);
1322 if (hlist_unhashed(&src
->node
) || !netif_running(br
->dev
) ||
1323 timer_pending(&src
->timer
))
1327 if (pg
->filter_mode
== MCAST_INCLUDE
) {
1328 br_multicast_del_group_src(src
, false);
1329 if (!hlist_empty(&pg
->src_list
))
1331 br_multicast_find_del_pg(br
, pg
);
1333 br_multicast_fwd_src_handle(src
);
1337 spin_unlock(&br
->multicast_lock
);
1340 struct net_bridge_group_src
*
1341 br_multicast_find_group_src(struct net_bridge_port_group
*pg
, struct br_ip
*ip
)
1343 struct net_bridge_group_src
*ent
;
1345 switch (ip
->proto
) {
1346 case htons(ETH_P_IP
):
1347 hlist_for_each_entry(ent
, &pg
->src_list
, node
)
1348 if (ip
->src
.ip4
== ent
->addr
.src
.ip4
)
1351 #if IS_ENABLED(CONFIG_IPV6)
1352 case htons(ETH_P_IPV6
):
1353 hlist_for_each_entry(ent
, &pg
->src_list
, node
)
1354 if (!ipv6_addr_cmp(&ent
->addr
.src
.ip6
, &ip
->src
.ip6
))
1363 struct net_bridge_group_src
*
1364 br_multicast_new_group_src(struct net_bridge_port_group
*pg
, struct br_ip
*src_ip
)
1366 struct net_bridge_group_src
*grp_src
;
1368 if (unlikely(pg
->src_ents
>= PG_SRC_ENT_LIMIT
))
1371 switch (src_ip
->proto
) {
1372 case htons(ETH_P_IP
):
1373 if (ipv4_is_zeronet(src_ip
->src
.ip4
) ||
1374 ipv4_is_multicast(src_ip
->src
.ip4
))
1377 #if IS_ENABLED(CONFIG_IPV6)
1378 case htons(ETH_P_IPV6
):
1379 if (ipv6_addr_any(&src_ip
->src
.ip6
) ||
1380 ipv6_addr_is_multicast(&src_ip
->src
.ip6
))
1386 grp_src
= kzalloc(sizeof(*grp_src
), GFP_ATOMIC
);
1387 if (unlikely(!grp_src
))
1391 grp_src
->br
= pg
->key
.port
->br
;
1392 grp_src
->addr
= *src_ip
;
1393 grp_src
->mcast_gc
.destroy
= br_multicast_destroy_group_src
;
1394 timer_setup(&grp_src
->timer
, br_multicast_group_src_expired
, 0);
1396 hlist_add_head_rcu(&grp_src
->node
, &pg
->src_list
);
1402 struct net_bridge_port_group
*br_multicast_new_port_group(
1403 struct net_bridge_port
*port
,
1404 const struct br_ip
*group
,
1405 struct net_bridge_port_group __rcu
*next
,
1406 unsigned char flags
,
1407 const unsigned char *src
,
1410 struct netlink_ext_ack
*extack
)
1412 struct net_bridge_port_group
*p
;
1415 err
= br_multicast_port_ngroups_inc(port
, group
, extack
);
1419 p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
1421 NL_SET_ERR_MSG_MOD(extack
, "Couldn't allocate new port group");
1425 p
->key
.addr
= *group
;
1428 p
->filter_mode
= filter_mode
;
1429 p
->rt_protocol
= rt_protocol
;
1430 p
->eht_host_tree
= RB_ROOT
;
1431 p
->eht_set_tree
= RB_ROOT
;
1432 p
->mcast_gc
.destroy
= br_multicast_destroy_port_group
;
1433 INIT_HLIST_HEAD(&p
->src_list
);
1435 if (!br_multicast_is_star_g(group
) &&
1436 rhashtable_lookup_insert_fast(&port
->br
->sg_port_tbl
, &p
->rhnode
,
1437 br_sg_port_rht_params
)) {
1438 NL_SET_ERR_MSG_MOD(extack
, "Couldn't insert new port group");
1442 rcu_assign_pointer(p
->next
, next
);
1443 timer_setup(&p
->timer
, br_multicast_port_group_expired
, 0);
1444 timer_setup(&p
->rexmit_timer
, br_multicast_port_group_rexmit
, 0);
1445 hlist_add_head(&p
->mglist
, &port
->mglist
);
1448 memcpy(p
->eth_addr
, src
, ETH_ALEN
);
1450 eth_broadcast_addr(p
->eth_addr
);
1457 br_multicast_port_ngroups_dec(port
, group
->vid
);
1461 void br_multicast_del_port_group(struct net_bridge_port_group
*p
)
1463 struct net_bridge_port
*port
= p
->key
.port
;
1464 __u16 vid
= p
->key
.addr
.vid
;
1466 hlist_del_init(&p
->mglist
);
1467 if (!br_multicast_is_star_g(&p
->key
.addr
))
1468 rhashtable_remove_fast(&port
->br
->sg_port_tbl
, &p
->rhnode
,
1469 br_sg_port_rht_params
);
1471 br_multicast_port_ngroups_dec(port
, vid
);
1474 void br_multicast_host_join(const struct net_bridge_mcast
*brmctx
,
1475 struct net_bridge_mdb_entry
*mp
, bool notify
)
1477 if (!mp
->host_joined
) {
1478 mp
->host_joined
= true;
1479 if (br_multicast_is_star_g(&mp
->addr
))
1480 br_multicast_star_g_host_state(mp
);
1482 br_mdb_notify(mp
->br
->dev
, mp
, NULL
, RTM_NEWMDB
);
1485 if (br_group_is_l2(&mp
->addr
))
1488 mod_timer(&mp
->timer
, jiffies
+ brmctx
->multicast_membership_interval
);
1491 void br_multicast_host_leave(struct net_bridge_mdb_entry
*mp
, bool notify
)
1493 if (!mp
->host_joined
)
1496 mp
->host_joined
= false;
1497 if (br_multicast_is_star_g(&mp
->addr
))
1498 br_multicast_star_g_host_state(mp
);
1500 br_mdb_notify(mp
->br
->dev
, mp
, NULL
, RTM_DELMDB
);
1503 static struct net_bridge_port_group
*
1504 __br_multicast_add_group(struct net_bridge_mcast
*brmctx
,
1505 struct net_bridge_mcast_port
*pmctx
,
1506 struct br_ip
*group
,
1507 const unsigned char *src
,
1512 struct net_bridge_port_group __rcu
**pp
;
1513 struct net_bridge_port_group
*p
= NULL
;
1514 struct net_bridge_mdb_entry
*mp
;
1515 unsigned long now
= jiffies
;
1517 if (!br_multicast_ctx_should_use(brmctx
, pmctx
))
1520 mp
= br_multicast_new_group(brmctx
->br
, group
);
1522 return ERR_CAST(mp
);
1525 br_multicast_host_join(brmctx
, mp
, true);
1529 for (pp
= &mp
->ports
;
1530 (p
= mlock_dereference(*pp
, brmctx
->br
)) != NULL
;
1532 if (br_port_group_equal(p
, pmctx
->port
, src
))
1534 if ((unsigned long)p
->key
.port
< (unsigned long)pmctx
->port
)
1538 p
= br_multicast_new_port_group(pmctx
->port
, group
, *pp
, 0, src
,
1539 filter_mode
, RTPROT_KERNEL
, NULL
);
1541 p
= ERR_PTR(-ENOMEM
);
1544 rcu_assign_pointer(*pp
, p
);
1546 p
->flags
|= MDB_PG_FLAGS_BLOCKED
;
1547 br_mdb_notify(brmctx
->br
->dev
, mp
, p
, RTM_NEWMDB
);
1551 mod_timer(&p
->timer
,
1552 now
+ brmctx
->multicast_membership_interval
);
1558 static int br_multicast_add_group(struct net_bridge_mcast
*brmctx
,
1559 struct net_bridge_mcast_port
*pmctx
,
1560 struct br_ip
*group
,
1561 const unsigned char *src
,
1565 struct net_bridge_port_group
*pg
;
1568 spin_lock(&brmctx
->br
->multicast_lock
);
1569 pg
= __br_multicast_add_group(brmctx
, pmctx
, group
, src
, filter_mode
,
1570 igmpv2_mldv1
, false);
1571 /* NULL is considered valid for host joined groups */
1572 err
= PTR_ERR_OR_ZERO(pg
);
1573 spin_unlock(&brmctx
->br
->multicast_lock
);
1578 static int br_ip4_multicast_add_group(struct net_bridge_mcast
*brmctx
,
1579 struct net_bridge_mcast_port
*pmctx
,
1582 const unsigned char *src
,
1585 struct br_ip br_group
;
1588 if (ipv4_is_local_multicast(group
))
1591 memset(&br_group
, 0, sizeof(br_group
));
1592 br_group
.dst
.ip4
= group
;
1593 br_group
.proto
= htons(ETH_P_IP
);
1595 filter_mode
= igmpv2
? MCAST_EXCLUDE
: MCAST_INCLUDE
;
1597 return br_multicast_add_group(brmctx
, pmctx
, &br_group
, src
,
1598 filter_mode
, igmpv2
);
1601 #if IS_ENABLED(CONFIG_IPV6)
1602 static int br_ip6_multicast_add_group(struct net_bridge_mcast
*brmctx
,
1603 struct net_bridge_mcast_port
*pmctx
,
1604 const struct in6_addr
*group
,
1606 const unsigned char *src
,
1609 struct br_ip br_group
;
1612 if (ipv6_addr_is_ll_all_nodes(group
))
1615 memset(&br_group
, 0, sizeof(br_group
));
1616 br_group
.dst
.ip6
= *group
;
1617 br_group
.proto
= htons(ETH_P_IPV6
);
1619 filter_mode
= mldv1
? MCAST_EXCLUDE
: MCAST_INCLUDE
;
1621 return br_multicast_add_group(brmctx
, pmctx
, &br_group
, src
,
1622 filter_mode
, mldv1
);
1626 static bool br_multicast_rport_del(struct hlist_node
*rlist
)
1628 if (hlist_unhashed(rlist
))
1631 hlist_del_init_rcu(rlist
);
1635 static bool br_ip4_multicast_rport_del(struct net_bridge_mcast_port
*pmctx
)
1637 return br_multicast_rport_del(&pmctx
->ip4_rlist
);
1640 static bool br_ip6_multicast_rport_del(struct net_bridge_mcast_port
*pmctx
)
1642 #if IS_ENABLED(CONFIG_IPV6)
1643 return br_multicast_rport_del(&pmctx
->ip6_rlist
);
1649 static void br_multicast_router_expired(struct net_bridge_mcast_port
*pmctx
,
1650 struct timer_list
*t
,
1651 struct hlist_node
*rlist
)
1653 struct net_bridge
*br
= pmctx
->port
->br
;
1656 spin_lock(&br
->multicast_lock
);
1657 if (pmctx
->multicast_router
== MDB_RTR_TYPE_DISABLED
||
1658 pmctx
->multicast_router
== MDB_RTR_TYPE_PERM
||
1662 del
= br_multicast_rport_del(rlist
);
1663 br_multicast_rport_del_notify(pmctx
, del
);
1665 spin_unlock(&br
->multicast_lock
);
1668 static void br_ip4_multicast_router_expired(struct timer_list
*t
)
1670 struct net_bridge_mcast_port
*pmctx
= from_timer(pmctx
, t
,
1671 ip4_mc_router_timer
);
1673 br_multicast_router_expired(pmctx
, t
, &pmctx
->ip4_rlist
);
1676 #if IS_ENABLED(CONFIG_IPV6)
1677 static void br_ip6_multicast_router_expired(struct timer_list
*t
)
1679 struct net_bridge_mcast_port
*pmctx
= from_timer(pmctx
, t
,
1680 ip6_mc_router_timer
);
1682 br_multicast_router_expired(pmctx
, t
, &pmctx
->ip6_rlist
);
1686 static void br_mc_router_state_change(struct net_bridge
*p
,
1689 struct switchdev_attr attr
= {
1691 .id
= SWITCHDEV_ATTR_ID_BRIDGE_MROUTER
,
1692 .flags
= SWITCHDEV_F_DEFER
,
1693 .u
.mrouter
= is_mc_router
,
1696 switchdev_port_attr_set(p
->dev
, &attr
, NULL
);
1699 static void br_multicast_local_router_expired(struct net_bridge_mcast
*brmctx
,
1700 struct timer_list
*timer
)
1702 spin_lock(&brmctx
->br
->multicast_lock
);
1703 if (brmctx
->multicast_router
== MDB_RTR_TYPE_DISABLED
||
1704 brmctx
->multicast_router
== MDB_RTR_TYPE_PERM
||
1705 br_ip4_multicast_is_router(brmctx
) ||
1706 br_ip6_multicast_is_router(brmctx
))
1709 br_mc_router_state_change(brmctx
->br
, false);
1711 spin_unlock(&brmctx
->br
->multicast_lock
);
1714 static void br_ip4_multicast_local_router_expired(struct timer_list
*t
)
1716 struct net_bridge_mcast
*brmctx
= from_timer(brmctx
, t
,
1717 ip4_mc_router_timer
);
1719 br_multicast_local_router_expired(brmctx
, t
);
1722 #if IS_ENABLED(CONFIG_IPV6)
1723 static void br_ip6_multicast_local_router_expired(struct timer_list
*t
)
1725 struct net_bridge_mcast
*brmctx
= from_timer(brmctx
, t
,
1726 ip6_mc_router_timer
);
1728 br_multicast_local_router_expired(brmctx
, t
);
1732 static void br_multicast_querier_expired(struct net_bridge_mcast
*brmctx
,
1733 struct bridge_mcast_own_query
*query
)
1735 spin_lock(&brmctx
->br
->multicast_lock
);
1736 if (!netif_running(brmctx
->br
->dev
) ||
1737 br_multicast_ctx_vlan_global_disabled(brmctx
) ||
1738 !br_opt_get(brmctx
->br
, BROPT_MULTICAST_ENABLED
))
1741 br_multicast_start_querier(brmctx
, query
);
1744 spin_unlock(&brmctx
->br
->multicast_lock
);
1747 static void br_ip4_multicast_querier_expired(struct timer_list
*t
)
1749 struct net_bridge_mcast
*brmctx
= from_timer(brmctx
, t
,
1750 ip4_other_query
.timer
);
1752 br_multicast_querier_expired(brmctx
, &brmctx
->ip4_own_query
);
1755 #if IS_ENABLED(CONFIG_IPV6)
1756 static void br_ip6_multicast_querier_expired(struct timer_list
*t
)
1758 struct net_bridge_mcast
*brmctx
= from_timer(brmctx
, t
,
1759 ip6_other_query
.timer
);
1761 br_multicast_querier_expired(brmctx
, &brmctx
->ip6_own_query
);
1765 static void br_multicast_query_delay_expired(struct timer_list
*t
)
1769 static void br_multicast_select_own_querier(struct net_bridge_mcast
*brmctx
,
1771 struct sk_buff
*skb
)
1773 if (ip
->proto
== htons(ETH_P_IP
))
1774 brmctx
->ip4_querier
.addr
.src
.ip4
= ip_hdr(skb
)->saddr
;
1775 #if IS_ENABLED(CONFIG_IPV6)
1777 brmctx
->ip6_querier
.addr
.src
.ip6
= ipv6_hdr(skb
)->saddr
;
1781 static void __br_multicast_send_query(struct net_bridge_mcast
*brmctx
,
1782 struct net_bridge_mcast_port
*pmctx
,
1783 struct net_bridge_port_group
*pg
,
1784 struct br_ip
*ip_dst
,
1785 struct br_ip
*group
,
1790 bool over_lmqt
= !!sflag
;
1791 struct sk_buff
*skb
;
1794 if (!br_multicast_ctx_should_use(brmctx
, pmctx
) ||
1795 !br_multicast_ctx_matches_vlan_snooping(brmctx
))
1799 skb
= br_multicast_alloc_query(brmctx
, pmctx
, pg
, ip_dst
, group
,
1800 with_srcs
, over_lmqt
, sflag
, &igmp_type
,
1806 skb
->dev
= pmctx
->port
->dev
;
1807 br_multicast_count(brmctx
->br
, pmctx
->port
, skb
, igmp_type
,
1809 NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_OUT
,
1810 dev_net(pmctx
->port
->dev
), NULL
, skb
, NULL
, skb
->dev
,
1811 br_dev_queue_push_xmit
);
1813 if (over_lmqt
&& with_srcs
&& sflag
) {
1815 goto again_under_lmqt
;
1818 br_multicast_select_own_querier(brmctx
, group
, skb
);
1819 br_multicast_count(brmctx
->br
, NULL
, skb
, igmp_type
,
1825 static void br_multicast_read_querier(const struct bridge_mcast_querier
*querier
,
1826 struct bridge_mcast_querier
*dest
)
1830 memset(dest
, 0, sizeof(*dest
));
1832 seq
= read_seqcount_begin(&querier
->seq
);
1833 dest
->port_ifidx
= querier
->port_ifidx
;
1834 memcpy(&dest
->addr
, &querier
->addr
, sizeof(struct br_ip
));
1835 } while (read_seqcount_retry(&querier
->seq
, seq
));
1838 static void br_multicast_update_querier(struct net_bridge_mcast
*brmctx
,
1839 struct bridge_mcast_querier
*querier
,
1841 struct br_ip
*saddr
)
1843 write_seqcount_begin(&querier
->seq
);
1844 querier
->port_ifidx
= ifindex
;
1845 memcpy(&querier
->addr
, saddr
, sizeof(*saddr
));
1846 write_seqcount_end(&querier
->seq
);
1849 static void br_multicast_send_query(struct net_bridge_mcast
*brmctx
,
1850 struct net_bridge_mcast_port
*pmctx
,
1851 struct bridge_mcast_own_query
*own_query
)
1853 struct bridge_mcast_other_query
*other_query
= NULL
;
1854 struct bridge_mcast_querier
*querier
;
1855 struct br_ip br_group
;
1858 if (!br_multicast_ctx_should_use(brmctx
, pmctx
) ||
1859 !br_opt_get(brmctx
->br
, BROPT_MULTICAST_ENABLED
) ||
1860 !brmctx
->multicast_querier
)
1863 memset(&br_group
.dst
, 0, sizeof(br_group
.dst
));
1865 if (pmctx
? (own_query
== &pmctx
->ip4_own_query
) :
1866 (own_query
== &brmctx
->ip4_own_query
)) {
1867 querier
= &brmctx
->ip4_querier
;
1868 other_query
= &brmctx
->ip4_other_query
;
1869 br_group
.proto
= htons(ETH_P_IP
);
1870 #if IS_ENABLED(CONFIG_IPV6)
1872 querier
= &brmctx
->ip6_querier
;
1873 other_query
= &brmctx
->ip6_other_query
;
1874 br_group
.proto
= htons(ETH_P_IPV6
);
1878 if (!other_query
|| timer_pending(&other_query
->timer
))
1881 /* we're about to select ourselves as querier */
1882 if (!pmctx
&& querier
->port_ifidx
) {
1883 struct br_ip zeroip
= {};
1885 br_multicast_update_querier(brmctx
, querier
, 0, &zeroip
);
1888 __br_multicast_send_query(brmctx
, pmctx
, NULL
, NULL
, &br_group
, false,
1892 time
+= own_query
->startup_sent
< brmctx
->multicast_startup_query_count
?
1893 brmctx
->multicast_startup_query_interval
:
1894 brmctx
->multicast_query_interval
;
1895 mod_timer(&own_query
->timer
, time
);
1899 br_multicast_port_query_expired(struct net_bridge_mcast_port
*pmctx
,
1900 struct bridge_mcast_own_query
*query
)
1902 struct net_bridge
*br
= pmctx
->port
->br
;
1903 struct net_bridge_mcast
*brmctx
;
1905 spin_lock(&br
->multicast_lock
);
1906 if (br_multicast_port_ctx_state_stopped(pmctx
))
1909 brmctx
= br_multicast_port_ctx_get_global(pmctx
);
1910 if (query
->startup_sent
< brmctx
->multicast_startup_query_count
)
1911 query
->startup_sent
++;
1913 br_multicast_send_query(brmctx
, pmctx
, query
);
1916 spin_unlock(&br
->multicast_lock
);
1919 static void br_ip4_multicast_port_query_expired(struct timer_list
*t
)
1921 struct net_bridge_mcast_port
*pmctx
= from_timer(pmctx
, t
,
1922 ip4_own_query
.timer
);
1924 br_multicast_port_query_expired(pmctx
, &pmctx
->ip4_own_query
);
1927 #if IS_ENABLED(CONFIG_IPV6)
1928 static void br_ip6_multicast_port_query_expired(struct timer_list
*t
)
1930 struct net_bridge_mcast_port
*pmctx
= from_timer(pmctx
, t
,
1931 ip6_own_query
.timer
);
1933 br_multicast_port_query_expired(pmctx
, &pmctx
->ip6_own_query
);
1937 static void br_multicast_port_group_rexmit(struct timer_list
*t
)
1939 struct net_bridge_port_group
*pg
= from_timer(pg
, t
, rexmit_timer
);
1940 struct bridge_mcast_other_query
*other_query
= NULL
;
1941 struct net_bridge
*br
= pg
->key
.port
->br
;
1942 struct net_bridge_mcast_port
*pmctx
;
1943 struct net_bridge_mcast
*brmctx
;
1944 bool need_rexmit
= false;
1946 spin_lock(&br
->multicast_lock
);
1947 if (!netif_running(br
->dev
) || hlist_unhashed(&pg
->mglist
) ||
1948 !br_opt_get(br
, BROPT_MULTICAST_ENABLED
))
1951 pmctx
= br_multicast_pg_to_port_ctx(pg
);
1954 brmctx
= br_multicast_port_ctx_get_global(pmctx
);
1955 if (!brmctx
->multicast_querier
)
1958 if (pg
->key
.addr
.proto
== htons(ETH_P_IP
))
1959 other_query
= &brmctx
->ip4_other_query
;
1960 #if IS_ENABLED(CONFIG_IPV6)
1962 other_query
= &brmctx
->ip6_other_query
;
1965 if (!other_query
|| timer_pending(&other_query
->timer
))
1968 if (pg
->grp_query_rexmit_cnt
) {
1969 pg
->grp_query_rexmit_cnt
--;
1970 __br_multicast_send_query(brmctx
, pmctx
, pg
, &pg
->key
.addr
,
1971 &pg
->key
.addr
, false, 1, NULL
);
1973 __br_multicast_send_query(brmctx
, pmctx
, pg
, &pg
->key
.addr
,
1974 &pg
->key
.addr
, true, 0, &need_rexmit
);
1976 if (pg
->grp_query_rexmit_cnt
|| need_rexmit
)
1977 mod_timer(&pg
->rexmit_timer
, jiffies
+
1978 brmctx
->multicast_last_member_interval
);
1980 spin_unlock(&br
->multicast_lock
);
1983 static int br_mc_disabled_update(struct net_device
*dev
, bool value
,
1984 struct netlink_ext_ack
*extack
)
1986 struct switchdev_attr attr
= {
1988 .id
= SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED
,
1989 .flags
= SWITCHDEV_F_DEFER
,
1990 .u
.mc_disabled
= !value
,
1993 return switchdev_port_attr_set(dev
, &attr
, extack
);
1996 void br_multicast_port_ctx_init(struct net_bridge_port
*port
,
1997 struct net_bridge_vlan
*vlan
,
1998 struct net_bridge_mcast_port
*pmctx
)
2002 pmctx
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
2003 timer_setup(&pmctx
->ip4_mc_router_timer
,
2004 br_ip4_multicast_router_expired
, 0);
2005 timer_setup(&pmctx
->ip4_own_query
.timer
,
2006 br_ip4_multicast_port_query_expired
, 0);
2007 #if IS_ENABLED(CONFIG_IPV6)
2008 timer_setup(&pmctx
->ip6_mc_router_timer
,
2009 br_ip6_multicast_router_expired
, 0);
2010 timer_setup(&pmctx
->ip6_own_query
.timer
,
2011 br_ip6_multicast_port_query_expired
, 0);
2015 void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port
*pmctx
)
2017 #if IS_ENABLED(CONFIG_IPV6)
2018 del_timer_sync(&pmctx
->ip6_mc_router_timer
);
2020 del_timer_sync(&pmctx
->ip4_mc_router_timer
);
2023 int br_multicast_add_port(struct net_bridge_port
*port
)
2027 port
->multicast_eht_hosts_limit
= BR_MCAST_DEFAULT_EHT_HOSTS_LIMIT
;
2028 br_multicast_port_ctx_init(port
, NULL
, &port
->multicast_ctx
);
2030 err
= br_mc_disabled_update(port
->dev
,
2031 br_opt_get(port
->br
,
2032 BROPT_MULTICAST_ENABLED
),
2034 if (err
&& err
!= -EOPNOTSUPP
)
2037 port
->mcast_stats
= netdev_alloc_pcpu_stats(struct bridge_mcast_stats
);
2038 if (!port
->mcast_stats
)
2044 void br_multicast_del_port(struct net_bridge_port
*port
)
2046 struct net_bridge
*br
= port
->br
;
2047 struct net_bridge_port_group
*pg
;
2048 struct hlist_node
*n
;
2050 /* Take care of the remaining groups, only perm ones should be left */
2051 spin_lock_bh(&br
->multicast_lock
);
2052 hlist_for_each_entry_safe(pg
, n
, &port
->mglist
, mglist
)
2053 br_multicast_find_del_pg(br
, pg
);
2054 spin_unlock_bh(&br
->multicast_lock
);
2055 flush_work(&br
->mcast_gc_work
);
2056 br_multicast_port_ctx_deinit(&port
->multicast_ctx
);
2057 free_percpu(port
->mcast_stats
);
2060 static void br_multicast_enable(struct bridge_mcast_own_query
*query
)
2062 query
->startup_sent
= 0;
2064 if (try_to_del_timer_sync(&query
->timer
) >= 0 ||
2065 del_timer(&query
->timer
))
2066 mod_timer(&query
->timer
, jiffies
);
2069 static void __br_multicast_enable_port_ctx(struct net_bridge_mcast_port
*pmctx
)
2071 struct net_bridge
*br
= pmctx
->port
->br
;
2072 struct net_bridge_mcast
*brmctx
;
2074 brmctx
= br_multicast_port_ctx_get_global(pmctx
);
2075 if (!br_opt_get(br
, BROPT_MULTICAST_ENABLED
) ||
2076 !netif_running(br
->dev
))
2079 br_multicast_enable(&pmctx
->ip4_own_query
);
2080 #if IS_ENABLED(CONFIG_IPV6)
2081 br_multicast_enable(&pmctx
->ip6_own_query
);
2083 if (pmctx
->multicast_router
== MDB_RTR_TYPE_PERM
) {
2084 br_ip4_multicast_add_router(brmctx
, pmctx
);
2085 br_ip6_multicast_add_router(brmctx
, pmctx
);
2088 if (br_multicast_port_ctx_is_vlan(pmctx
)) {
2089 struct net_bridge_port_group
*pg
;
2092 /* The mcast_n_groups counter might be wrong. First,
2093 * BR_VLFLAG_MCAST_ENABLED is toggled before temporary entries
2094 * are flushed, thus mcast_n_groups after the toggle does not
2095 * reflect the true values. And second, permanent entries added
2096 * while BR_VLFLAG_MCAST_ENABLED was disabled, are not reflected
2097 * either. Thus we have to refresh the counter.
2100 hlist_for_each_entry(pg
, &pmctx
->port
->mglist
, mglist
) {
2101 if (pg
->key
.addr
.vid
== pmctx
->vlan
->vid
)
2104 WRITE_ONCE(pmctx
->mdb_n_entries
, n
);
2108 void br_multicast_enable_port(struct net_bridge_port
*port
)
2110 struct net_bridge
*br
= port
->br
;
2112 spin_lock_bh(&br
->multicast_lock
);
2113 __br_multicast_enable_port_ctx(&port
->multicast_ctx
);
2114 spin_unlock_bh(&br
->multicast_lock
);
2117 static void __br_multicast_disable_port_ctx(struct net_bridge_mcast_port
*pmctx
)
2119 struct net_bridge_port_group
*pg
;
2120 struct hlist_node
*n
;
2123 hlist_for_each_entry_safe(pg
, n
, &pmctx
->port
->mglist
, mglist
)
2124 if (!(pg
->flags
& MDB_PG_FLAGS_PERMANENT
) &&
2125 (!br_multicast_port_ctx_is_vlan(pmctx
) ||
2126 pg
->key
.addr
.vid
== pmctx
->vlan
->vid
))
2127 br_multicast_find_del_pg(pmctx
->port
->br
, pg
);
2129 del
|= br_ip4_multicast_rport_del(pmctx
);
2130 del_timer(&pmctx
->ip4_mc_router_timer
);
2131 del_timer(&pmctx
->ip4_own_query
.timer
);
2132 del
|= br_ip6_multicast_rport_del(pmctx
);
2133 #if IS_ENABLED(CONFIG_IPV6)
2134 del_timer(&pmctx
->ip6_mc_router_timer
);
2135 del_timer(&pmctx
->ip6_own_query
.timer
);
2137 br_multicast_rport_del_notify(pmctx
, del
);
2140 void br_multicast_disable_port(struct net_bridge_port
*port
)
2142 spin_lock_bh(&port
->br
->multicast_lock
);
2143 __br_multicast_disable_port_ctx(&port
->multicast_ctx
);
2144 spin_unlock_bh(&port
->br
->multicast_lock
);
2147 static int __grp_src_delete_marked(struct net_bridge_port_group
*pg
)
2149 struct net_bridge_group_src
*ent
;
2150 struct hlist_node
*tmp
;
2153 hlist_for_each_entry_safe(ent
, tmp
, &pg
->src_list
, node
)
2154 if (ent
->flags
& BR_SGRP_F_DELETE
) {
2155 br_multicast_del_group_src(ent
, false);
2162 static void __grp_src_mod_timer(struct net_bridge_group_src
*src
,
2163 unsigned long expires
)
2165 mod_timer(&src
->timer
, expires
);
2166 br_multicast_fwd_src_handle(src
);
2169 static void __grp_src_query_marked_and_rexmit(struct net_bridge_mcast
*brmctx
,
2170 struct net_bridge_mcast_port
*pmctx
,
2171 struct net_bridge_port_group
*pg
)
2173 struct bridge_mcast_other_query
*other_query
= NULL
;
2174 u32 lmqc
= brmctx
->multicast_last_member_count
;
2175 unsigned long lmqt
, lmi
, now
= jiffies
;
2176 struct net_bridge_group_src
*ent
;
2178 if (!netif_running(brmctx
->br
->dev
) ||
2179 !br_opt_get(brmctx
->br
, BROPT_MULTICAST_ENABLED
))
2182 if (pg
->key
.addr
.proto
== htons(ETH_P_IP
))
2183 other_query
= &brmctx
->ip4_other_query
;
2184 #if IS_ENABLED(CONFIG_IPV6)
2186 other_query
= &brmctx
->ip6_other_query
;
2189 lmqt
= now
+ br_multicast_lmqt(brmctx
);
2190 hlist_for_each_entry(ent
, &pg
->src_list
, node
) {
2191 if (ent
->flags
& BR_SGRP_F_SEND
) {
2192 ent
->flags
&= ~BR_SGRP_F_SEND
;
2193 if (ent
->timer
.expires
> lmqt
) {
2194 if (brmctx
->multicast_querier
&&
2196 !timer_pending(&other_query
->timer
))
2197 ent
->src_query_rexmit_cnt
= lmqc
;
2198 __grp_src_mod_timer(ent
, lmqt
);
2203 if (!brmctx
->multicast_querier
||
2204 !other_query
|| timer_pending(&other_query
->timer
))
2207 __br_multicast_send_query(brmctx
, pmctx
, pg
, &pg
->key
.addr
,
2208 &pg
->key
.addr
, true, 1, NULL
);
2210 lmi
= now
+ brmctx
->multicast_last_member_interval
;
2211 if (!timer_pending(&pg
->rexmit_timer
) ||
2212 time_after(pg
->rexmit_timer
.expires
, lmi
))
2213 mod_timer(&pg
->rexmit_timer
, lmi
);
2216 static void __grp_send_query_and_rexmit(struct net_bridge_mcast
*brmctx
,
2217 struct net_bridge_mcast_port
*pmctx
,
2218 struct net_bridge_port_group
*pg
)
2220 struct bridge_mcast_other_query
*other_query
= NULL
;
2221 unsigned long now
= jiffies
, lmi
;
2223 if (!netif_running(brmctx
->br
->dev
) ||
2224 !br_opt_get(brmctx
->br
, BROPT_MULTICAST_ENABLED
))
2227 if (pg
->key
.addr
.proto
== htons(ETH_P_IP
))
2228 other_query
= &brmctx
->ip4_other_query
;
2229 #if IS_ENABLED(CONFIG_IPV6)
2231 other_query
= &brmctx
->ip6_other_query
;
2234 if (brmctx
->multicast_querier
&&
2235 other_query
&& !timer_pending(&other_query
->timer
)) {
2236 lmi
= now
+ brmctx
->multicast_last_member_interval
;
2237 pg
->grp_query_rexmit_cnt
= brmctx
->multicast_last_member_count
- 1;
2238 __br_multicast_send_query(brmctx
, pmctx
, pg
, &pg
->key
.addr
,
2239 &pg
->key
.addr
, false, 0, NULL
);
2240 if (!timer_pending(&pg
->rexmit_timer
) ||
2241 time_after(pg
->rexmit_timer
.expires
, lmi
))
2242 mod_timer(&pg
->rexmit_timer
, lmi
);
2245 if (pg
->filter_mode
== MCAST_EXCLUDE
&&
2246 (!timer_pending(&pg
->timer
) ||
2247 time_after(pg
->timer
.expires
, now
+ br_multicast_lmqt(brmctx
))))
2248 mod_timer(&pg
->timer
, now
+ br_multicast_lmqt(brmctx
));
2251 /* State Msg type New state Actions
2252 * INCLUDE (A) IS_IN (B) INCLUDE (A+B) (B)=GMI
2253 * INCLUDE (A) ALLOW (B) INCLUDE (A+B) (B)=GMI
2254 * EXCLUDE (X,Y) ALLOW (A) EXCLUDE (X+A,Y-A) (A)=GMI
2256 static bool br_multicast_isinc_allow(const struct net_bridge_mcast
*brmctx
,
2257 struct net_bridge_port_group
*pg
, void *h_addr
,
2258 void *srcs
, u32 nsrcs
, size_t addr_size
,
2261 struct net_bridge_group_src
*ent
;
2262 unsigned long now
= jiffies
;
2263 bool changed
= false;
2264 struct br_ip src_ip
;
2267 memset(&src_ip
, 0, sizeof(src_ip
));
2268 src_ip
.proto
= pg
->key
.addr
.proto
;
2269 for (src_idx
= 0; src_idx
< nsrcs
; src_idx
++) {
2270 memcpy(&src_ip
.src
, srcs
+ (src_idx
* addr_size
), addr_size
);
2271 ent
= br_multicast_find_group_src(pg
, &src_ip
);
2273 ent
= br_multicast_new_group_src(pg
, &src_ip
);
2279 __grp_src_mod_timer(ent
, now
+ br_multicast_gmi(brmctx
));
2282 if (br_multicast_eht_handle(brmctx
, pg
, h_addr
, srcs
, nsrcs
, addr_size
,
2289 /* State Msg type New state Actions
2290 * INCLUDE (A) IS_EX (B) EXCLUDE (A*B,B-A) (B-A)=0
2294 static void __grp_src_isexc_incl(const struct net_bridge_mcast
*brmctx
,
2295 struct net_bridge_port_group
*pg
, void *h_addr
,
2296 void *srcs
, u32 nsrcs
, size_t addr_size
,
2299 struct net_bridge_group_src
*ent
;
2300 struct br_ip src_ip
;
2303 hlist_for_each_entry(ent
, &pg
->src_list
, node
)
2304 ent
->flags
|= BR_SGRP_F_DELETE
;
2306 memset(&src_ip
, 0, sizeof(src_ip
));
2307 src_ip
.proto
= pg
->key
.addr
.proto
;
2308 for (src_idx
= 0; src_idx
< nsrcs
; src_idx
++) {
2309 memcpy(&src_ip
.src
, srcs
+ (src_idx
* addr_size
), addr_size
);
2310 ent
= br_multicast_find_group_src(pg
, &src_ip
);
2312 ent
->flags
&= ~BR_SGRP_F_DELETE
;
2314 ent
= br_multicast_new_group_src(pg
, &src_ip
);
2316 br_multicast_fwd_src_handle(ent
);
2319 br_multicast_eht_handle(brmctx
, pg
, h_addr
, srcs
, nsrcs
, addr_size
,
2322 __grp_src_delete_marked(pg
);
2325 /* State Msg type New state Actions
2326 * EXCLUDE (X,Y) IS_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=GMI
2331 static bool __grp_src_isexc_excl(const struct net_bridge_mcast
*brmctx
,
2332 struct net_bridge_port_group
*pg
, void *h_addr
,
2333 void *srcs
, u32 nsrcs
, size_t addr_size
,
2336 struct net_bridge_group_src
*ent
;
2337 unsigned long now
= jiffies
;
2338 bool changed
= false;
2339 struct br_ip src_ip
;
2342 hlist_for_each_entry(ent
, &pg
->src_list
, node
)
2343 ent
->flags
|= BR_SGRP_F_DELETE
;
2345 memset(&src_ip
, 0, sizeof(src_ip
));
2346 src_ip
.proto
= pg
->key
.addr
.proto
;
2347 for (src_idx
= 0; src_idx
< nsrcs
; src_idx
++) {
2348 memcpy(&src_ip
.src
, srcs
+ (src_idx
* addr_size
), addr_size
);
2349 ent
= br_multicast_find_group_src(pg
, &src_ip
);
2351 ent
->flags
&= ~BR_SGRP_F_DELETE
;
2353 ent
= br_multicast_new_group_src(pg
, &src_ip
);
2355 __grp_src_mod_timer(ent
,
2356 now
+ br_multicast_gmi(brmctx
));
2362 if (br_multicast_eht_handle(brmctx
, pg
, h_addr
, srcs
, nsrcs
, addr_size
,
2366 if (__grp_src_delete_marked(pg
))
2372 static bool br_multicast_isexc(const struct net_bridge_mcast
*brmctx
,
2373 struct net_bridge_port_group
*pg
, void *h_addr
,
2374 void *srcs
, u32 nsrcs
, size_t addr_size
,
2377 bool changed
= false;
2379 switch (pg
->filter_mode
) {
2381 __grp_src_isexc_incl(brmctx
, pg
, h_addr
, srcs
, nsrcs
, addr_size
,
2383 br_multicast_star_g_handle_mode(pg
, MCAST_EXCLUDE
);
2387 changed
= __grp_src_isexc_excl(brmctx
, pg
, h_addr
, srcs
, nsrcs
,
2388 addr_size
, grec_type
);
2392 pg
->filter_mode
= MCAST_EXCLUDE
;
2393 mod_timer(&pg
->timer
, jiffies
+ br_multicast_gmi(brmctx
));
2398 /* State Msg type New state Actions
2399 * INCLUDE (A) TO_IN (B) INCLUDE (A+B) (B)=GMI
2402 static bool __grp_src_toin_incl(struct net_bridge_mcast
*brmctx
,
2403 struct net_bridge_mcast_port
*pmctx
,
2404 struct net_bridge_port_group
*pg
, void *h_addr
,
2405 void *srcs
, u32 nsrcs
, size_t addr_size
,
2408 u32 src_idx
, to_send
= pg
->src_ents
;
2409 struct net_bridge_group_src
*ent
;
2410 unsigned long now
= jiffies
;
2411 bool changed
= false;
2412 struct br_ip src_ip
;
2414 hlist_for_each_entry(ent
, &pg
->src_list
, node
)
2415 ent
->flags
|= BR_SGRP_F_SEND
;
2417 memset(&src_ip
, 0, sizeof(src_ip
));
2418 src_ip
.proto
= pg
->key
.addr
.proto
;
2419 for (src_idx
= 0; src_idx
< nsrcs
; src_idx
++) {
2420 memcpy(&src_ip
.src
, srcs
+ (src_idx
* addr_size
), addr_size
);
2421 ent
= br_multicast_find_group_src(pg
, &src_ip
);
2423 ent
->flags
&= ~BR_SGRP_F_SEND
;
2426 ent
= br_multicast_new_group_src(pg
, &src_ip
);
2431 __grp_src_mod_timer(ent
, now
+ br_multicast_gmi(brmctx
));
2434 if (br_multicast_eht_handle(brmctx
, pg
, h_addr
, srcs
, nsrcs
, addr_size
,
2439 __grp_src_query_marked_and_rexmit(brmctx
, pmctx
, pg
);
2444 /* State Msg type New state Actions
2445 * EXCLUDE (X,Y) TO_IN (A) EXCLUDE (X+A,Y-A) (A)=GMI
2449 static bool __grp_src_toin_excl(struct net_bridge_mcast
*brmctx
,
2450 struct net_bridge_mcast_port
*pmctx
,
2451 struct net_bridge_port_group
*pg
, void *h_addr
,
2452 void *srcs
, u32 nsrcs
, size_t addr_size
,
2455 u32 src_idx
, to_send
= pg
->src_ents
;
2456 struct net_bridge_group_src
*ent
;
2457 unsigned long now
= jiffies
;
2458 bool changed
= false;
2459 struct br_ip src_ip
;
2461 hlist_for_each_entry(ent
, &pg
->src_list
, node
)
2462 if (timer_pending(&ent
->timer
))
2463 ent
->flags
|= BR_SGRP_F_SEND
;
2465 memset(&src_ip
, 0, sizeof(src_ip
));
2466 src_ip
.proto
= pg
->key
.addr
.proto
;
2467 for (src_idx
= 0; src_idx
< nsrcs
; src_idx
++) {
2468 memcpy(&src_ip
.src
, srcs
+ (src_idx
* addr_size
), addr_size
);
2469 ent
= br_multicast_find_group_src(pg
, &src_ip
);
2471 if (timer_pending(&ent
->timer
)) {
2472 ent
->flags
&= ~BR_SGRP_F_SEND
;
2476 ent
= br_multicast_new_group_src(pg
, &src_ip
);
2481 __grp_src_mod_timer(ent
, now
+ br_multicast_gmi(brmctx
));
2484 if (br_multicast_eht_handle(brmctx
, pg
, h_addr
, srcs
, nsrcs
, addr_size
,
2489 __grp_src_query_marked_and_rexmit(brmctx
, pmctx
, pg
);
2491 __grp_send_query_and_rexmit(brmctx
, pmctx
, pg
);
2496 static bool br_multicast_toin(struct net_bridge_mcast
*brmctx
,
2497 struct net_bridge_mcast_port
*pmctx
,
2498 struct net_bridge_port_group
*pg
, void *h_addr
,
2499 void *srcs
, u32 nsrcs
, size_t addr_size
,
2502 bool changed
= false;
2504 switch (pg
->filter_mode
) {
2506 changed
= __grp_src_toin_incl(brmctx
, pmctx
, pg
, h_addr
, srcs
,
2507 nsrcs
, addr_size
, grec_type
);
2510 changed
= __grp_src_toin_excl(brmctx
, pmctx
, pg
, h_addr
, srcs
,
2511 nsrcs
, addr_size
, grec_type
);
2515 if (br_multicast_eht_should_del_pg(pg
)) {
2516 pg
->flags
|= MDB_PG_FLAGS_FAST_LEAVE
;
2517 br_multicast_find_del_pg(pg
->key
.port
->br
, pg
);
2518 /* a notification has already been sent and we shouldn't
2519 * access pg after the delete so we have to return false
2527 /* State Msg type New state Actions
2528 * INCLUDE (A) TO_EX (B) EXCLUDE (A*B,B-A) (B-A)=0
2533 static void __grp_src_toex_incl(struct net_bridge_mcast
*brmctx
,
2534 struct net_bridge_mcast_port
*pmctx
,
2535 struct net_bridge_port_group
*pg
, void *h_addr
,
2536 void *srcs
, u32 nsrcs
, size_t addr_size
,
2539 struct net_bridge_group_src
*ent
;
2540 u32 src_idx
, to_send
= 0;
2541 struct br_ip src_ip
;
2543 hlist_for_each_entry(ent
, &pg
->src_list
, node
)
2544 ent
->flags
= (ent
->flags
& ~BR_SGRP_F_SEND
) | BR_SGRP_F_DELETE
;
2546 memset(&src_ip
, 0, sizeof(src_ip
));
2547 src_ip
.proto
= pg
->key
.addr
.proto
;
2548 for (src_idx
= 0; src_idx
< nsrcs
; src_idx
++) {
2549 memcpy(&src_ip
.src
, srcs
+ (src_idx
* addr_size
), addr_size
);
2550 ent
= br_multicast_find_group_src(pg
, &src_ip
);
2552 ent
->flags
= (ent
->flags
& ~BR_SGRP_F_DELETE
) |
2556 ent
= br_multicast_new_group_src(pg
, &src_ip
);
2559 br_multicast_fwd_src_handle(ent
);
2562 br_multicast_eht_handle(brmctx
, pg
, h_addr
, srcs
, nsrcs
, addr_size
,
2565 __grp_src_delete_marked(pg
);
2567 __grp_src_query_marked_and_rexmit(brmctx
, pmctx
, pg
);
2570 /* State Msg type New state Actions
2571 * EXCLUDE (X,Y) TO_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=Group Timer
2577 static bool __grp_src_toex_excl(struct net_bridge_mcast
*brmctx
,
2578 struct net_bridge_mcast_port
*pmctx
,
2579 struct net_bridge_port_group
*pg
, void *h_addr
,
2580 void *srcs
, u32 nsrcs
, size_t addr_size
,
2583 struct net_bridge_group_src
*ent
;
2584 u32 src_idx
, to_send
= 0;
2585 bool changed
= false;
2586 struct br_ip src_ip
;
2588 hlist_for_each_entry(ent
, &pg
->src_list
, node
)
2589 ent
->flags
= (ent
->flags
& ~BR_SGRP_F_SEND
) | BR_SGRP_F_DELETE
;
2591 memset(&src_ip
, 0, sizeof(src_ip
));
2592 src_ip
.proto
= pg
->key
.addr
.proto
;
2593 for (src_idx
= 0; src_idx
< nsrcs
; src_idx
++) {
2594 memcpy(&src_ip
.src
, srcs
+ (src_idx
* addr_size
), addr_size
);
2595 ent
= br_multicast_find_group_src(pg
, &src_ip
);
2597 ent
->flags
&= ~BR_SGRP_F_DELETE
;
2599 ent
= br_multicast_new_group_src(pg
, &src_ip
);
2601 __grp_src_mod_timer(ent
, pg
->timer
.expires
);
2605 if (ent
&& timer_pending(&ent
->timer
)) {
2606 ent
->flags
|= BR_SGRP_F_SEND
;
2611 if (br_multicast_eht_handle(brmctx
, pg
, h_addr
, srcs
, nsrcs
, addr_size
,
2615 if (__grp_src_delete_marked(pg
))
2618 __grp_src_query_marked_and_rexmit(brmctx
, pmctx
, pg
);
2623 static bool br_multicast_toex(struct net_bridge_mcast
*brmctx
,
2624 struct net_bridge_mcast_port
*pmctx
,
2625 struct net_bridge_port_group
*pg
, void *h_addr
,
2626 void *srcs
, u32 nsrcs
, size_t addr_size
,
2629 bool changed
= false;
2631 switch (pg
->filter_mode
) {
2633 __grp_src_toex_incl(brmctx
, pmctx
, pg
, h_addr
, srcs
, nsrcs
,
2634 addr_size
, grec_type
);
2635 br_multicast_star_g_handle_mode(pg
, MCAST_EXCLUDE
);
2639 changed
= __grp_src_toex_excl(brmctx
, pmctx
, pg
, h_addr
, srcs
,
2640 nsrcs
, addr_size
, grec_type
);
2644 pg
->filter_mode
= MCAST_EXCLUDE
;
2645 mod_timer(&pg
->timer
, jiffies
+ br_multicast_gmi(brmctx
));
2650 /* State Msg type New state Actions
2651 * INCLUDE (A) BLOCK (B) INCLUDE (A) Send Q(G,A*B)
2653 static bool __grp_src_block_incl(struct net_bridge_mcast
*brmctx
,
2654 struct net_bridge_mcast_port
*pmctx
,
2655 struct net_bridge_port_group
*pg
, void *h_addr
,
2656 void *srcs
, u32 nsrcs
, size_t addr_size
, int grec_type
)
2658 struct net_bridge_group_src
*ent
;
2659 u32 src_idx
, to_send
= 0;
2660 bool changed
= false;
2661 struct br_ip src_ip
;
2663 hlist_for_each_entry(ent
, &pg
->src_list
, node
)
2664 ent
->flags
&= ~BR_SGRP_F_SEND
;
2666 memset(&src_ip
, 0, sizeof(src_ip
));
2667 src_ip
.proto
= pg
->key
.addr
.proto
;
2668 for (src_idx
= 0; src_idx
< nsrcs
; src_idx
++) {
2669 memcpy(&src_ip
.src
, srcs
+ (src_idx
* addr_size
), addr_size
);
2670 ent
= br_multicast_find_group_src(pg
, &src_ip
);
2672 ent
->flags
|= BR_SGRP_F_SEND
;
2677 if (br_multicast_eht_handle(brmctx
, pg
, h_addr
, srcs
, nsrcs
, addr_size
,
2682 __grp_src_query_marked_and_rexmit(brmctx
, pmctx
, pg
);
2687 /* State Msg type New state Actions
2688 * EXCLUDE (X,Y) BLOCK (A) EXCLUDE (X+(A-Y),Y) (A-X-Y)=Group Timer
2691 static bool __grp_src_block_excl(struct net_bridge_mcast
*brmctx
,
2692 struct net_bridge_mcast_port
*pmctx
,
2693 struct net_bridge_port_group
*pg
, void *h_addr
,
2694 void *srcs
, u32 nsrcs
, size_t addr_size
, int grec_type
)
2696 struct net_bridge_group_src
*ent
;
2697 u32 src_idx
, to_send
= 0;
2698 bool changed
= false;
2699 struct br_ip src_ip
;
2701 hlist_for_each_entry(ent
, &pg
->src_list
, node
)
2702 ent
->flags
&= ~BR_SGRP_F_SEND
;
2704 memset(&src_ip
, 0, sizeof(src_ip
));
2705 src_ip
.proto
= pg
->key
.addr
.proto
;
2706 for (src_idx
= 0; src_idx
< nsrcs
; src_idx
++) {
2707 memcpy(&src_ip
.src
, srcs
+ (src_idx
* addr_size
), addr_size
);
2708 ent
= br_multicast_find_group_src(pg
, &src_ip
);
2710 ent
= br_multicast_new_group_src(pg
, &src_ip
);
2712 __grp_src_mod_timer(ent
, pg
->timer
.expires
);
2716 if (ent
&& timer_pending(&ent
->timer
)) {
2717 ent
->flags
|= BR_SGRP_F_SEND
;
2722 if (br_multicast_eht_handle(brmctx
, pg
, h_addr
, srcs
, nsrcs
, addr_size
,
2727 __grp_src_query_marked_and_rexmit(brmctx
, pmctx
, pg
);
2732 static bool br_multicast_block(struct net_bridge_mcast
*brmctx
,
2733 struct net_bridge_mcast_port
*pmctx
,
2734 struct net_bridge_port_group
*pg
, void *h_addr
,
2735 void *srcs
, u32 nsrcs
, size_t addr_size
, int grec_type
)
2737 bool changed
= false;
2739 switch (pg
->filter_mode
) {
2741 changed
= __grp_src_block_incl(brmctx
, pmctx
, pg
, h_addr
, srcs
,
2742 nsrcs
, addr_size
, grec_type
);
2745 changed
= __grp_src_block_excl(brmctx
, pmctx
, pg
, h_addr
, srcs
,
2746 nsrcs
, addr_size
, grec_type
);
2750 if ((pg
->filter_mode
== MCAST_INCLUDE
&& hlist_empty(&pg
->src_list
)) ||
2751 br_multicast_eht_should_del_pg(pg
)) {
2752 if (br_multicast_eht_should_del_pg(pg
))
2753 pg
->flags
|= MDB_PG_FLAGS_FAST_LEAVE
;
2754 br_multicast_find_del_pg(pg
->key
.port
->br
, pg
);
2755 /* a notification has already been sent and we shouldn't
2756 * access pg after the delete so we have to return false
2764 static struct net_bridge_port_group
*
2765 br_multicast_find_port(struct net_bridge_mdb_entry
*mp
,
2766 struct net_bridge_port
*p
,
2767 const unsigned char *src
)
2769 struct net_bridge
*br __maybe_unused
= mp
->br
;
2770 struct net_bridge_port_group
*pg
;
2772 for (pg
= mlock_dereference(mp
->ports
, br
);
2774 pg
= mlock_dereference(pg
->next
, br
))
2775 if (br_port_group_equal(pg
, p
, src
))
2781 static int br_ip4_multicast_igmp3_report(struct net_bridge_mcast
*brmctx
,
2782 struct net_bridge_mcast_port
*pmctx
,
2783 struct sk_buff
*skb
,
2786 bool igmpv2
= brmctx
->multicast_igmp_version
== 2;
2787 struct net_bridge_mdb_entry
*mdst
;
2788 struct net_bridge_port_group
*pg
;
2789 const unsigned char *src
;
2790 struct igmpv3_report
*ih
;
2791 struct igmpv3_grec
*grec
;
2792 int i
, len
, num
, type
;
2793 __be32 group
, *h_addr
;
2794 bool changed
= false;
2798 ih
= igmpv3_report_hdr(skb
);
2799 num
= ntohs(ih
->ngrec
);
2800 len
= skb_transport_offset(skb
) + sizeof(*ih
);
2802 for (i
= 0; i
< num
; i
++) {
2803 len
+= sizeof(*grec
);
2804 if (!ip_mc_may_pull(skb
, len
))
2807 grec
= (void *)(skb
->data
+ len
- sizeof(*grec
));
2808 group
= grec
->grec_mca
;
2809 type
= grec
->grec_type
;
2810 nsrcs
= ntohs(grec
->grec_nsrcs
);
2813 if (!ip_mc_may_pull(skb
, len
))
2817 case IGMPV3_MODE_IS_INCLUDE
:
2818 case IGMPV3_MODE_IS_EXCLUDE
:
2819 case IGMPV3_CHANGE_TO_INCLUDE
:
2820 case IGMPV3_CHANGE_TO_EXCLUDE
:
2821 case IGMPV3_ALLOW_NEW_SOURCES
:
2822 case IGMPV3_BLOCK_OLD_SOURCES
:
2829 src
= eth_hdr(skb
)->h_source
;
2831 (type
== IGMPV3_CHANGE_TO_INCLUDE
||
2832 type
== IGMPV3_MODE_IS_INCLUDE
)) {
2833 if (!pmctx
|| igmpv2
) {
2834 br_ip4_multicast_leave_group(brmctx
, pmctx
,
2839 err
= br_ip4_multicast_add_group(brmctx
, pmctx
, group
,
2845 if (!pmctx
|| igmpv2
)
2848 spin_lock(&brmctx
->br
->multicast_lock
);
2849 if (!br_multicast_ctx_should_use(brmctx
, pmctx
))
2850 goto unlock_continue
;
2852 mdst
= br_mdb_ip4_get(brmctx
->br
, group
, vid
);
2854 goto unlock_continue
;
2855 pg
= br_multicast_find_port(mdst
, pmctx
->port
, src
);
2856 if (!pg
|| (pg
->flags
& MDB_PG_FLAGS_PERMANENT
))
2857 goto unlock_continue
;
2858 /* reload grec and host addr */
2859 grec
= (void *)(skb
->data
+ len
- sizeof(*grec
) - (nsrcs
* 4));
2860 h_addr
= &ip_hdr(skb
)->saddr
;
2862 case IGMPV3_ALLOW_NEW_SOURCES
:
2863 changed
= br_multicast_isinc_allow(brmctx
, pg
, h_addr
,
2865 nsrcs
, sizeof(__be32
), type
);
2867 case IGMPV3_MODE_IS_INCLUDE
:
2868 changed
= br_multicast_isinc_allow(brmctx
, pg
, h_addr
,
2870 nsrcs
, sizeof(__be32
), type
);
2872 case IGMPV3_MODE_IS_EXCLUDE
:
2873 changed
= br_multicast_isexc(brmctx
, pg
, h_addr
,
2875 nsrcs
, sizeof(__be32
), type
);
2877 case IGMPV3_CHANGE_TO_INCLUDE
:
2878 changed
= br_multicast_toin(brmctx
, pmctx
, pg
, h_addr
,
2880 nsrcs
, sizeof(__be32
), type
);
2882 case IGMPV3_CHANGE_TO_EXCLUDE
:
2883 changed
= br_multicast_toex(brmctx
, pmctx
, pg
, h_addr
,
2885 nsrcs
, sizeof(__be32
), type
);
2887 case IGMPV3_BLOCK_OLD_SOURCES
:
2888 changed
= br_multicast_block(brmctx
, pmctx
, pg
, h_addr
,
2890 nsrcs
, sizeof(__be32
), type
);
2894 br_mdb_notify(brmctx
->br
->dev
, mdst
, pg
, RTM_NEWMDB
);
2896 spin_unlock(&brmctx
->br
->multicast_lock
);
2902 #if IS_ENABLED(CONFIG_IPV6)
2903 static int br_ip6_multicast_mld2_report(struct net_bridge_mcast
*brmctx
,
2904 struct net_bridge_mcast_port
*pmctx
,
2905 struct sk_buff
*skb
,
2908 bool mldv1
= brmctx
->multicast_mld_version
== 1;
2909 struct net_bridge_mdb_entry
*mdst
;
2910 struct net_bridge_port_group
*pg
;
2911 unsigned int nsrcs_offset
;
2912 struct mld2_report
*mld2r
;
2913 const unsigned char *src
;
2914 struct in6_addr
*h_addr
;
2915 struct mld2_grec
*grec
;
2916 unsigned int grec_len
;
2917 bool changed
= false;
2921 if (!ipv6_mc_may_pull(skb
, sizeof(*mld2r
)))
2924 mld2r
= (struct mld2_report
*)icmp6_hdr(skb
);
2925 num
= ntohs(mld2r
->mld2r_ngrec
);
2926 len
= skb_transport_offset(skb
) + sizeof(*mld2r
);
2928 for (i
= 0; i
< num
; i
++) {
2929 __be16
*_nsrcs
, __nsrcs
;
2932 nsrcs_offset
= len
+ offsetof(struct mld2_grec
, grec_nsrcs
);
2934 if (skb_transport_offset(skb
) + ipv6_transport_len(skb
) <
2935 nsrcs_offset
+ sizeof(__nsrcs
))
2938 _nsrcs
= skb_header_pointer(skb
, nsrcs_offset
,
2939 sizeof(__nsrcs
), &__nsrcs
);
2943 nsrcs
= ntohs(*_nsrcs
);
2944 grec_len
= struct_size(grec
, grec_src
, nsrcs
);
2946 if (!ipv6_mc_may_pull(skb
, len
+ grec_len
))
2949 grec
= (struct mld2_grec
*)(skb
->data
+ len
);
2952 switch (grec
->grec_type
) {
2953 case MLD2_MODE_IS_INCLUDE
:
2954 case MLD2_MODE_IS_EXCLUDE
:
2955 case MLD2_CHANGE_TO_INCLUDE
:
2956 case MLD2_CHANGE_TO_EXCLUDE
:
2957 case MLD2_ALLOW_NEW_SOURCES
:
2958 case MLD2_BLOCK_OLD_SOURCES
:
2965 src
= eth_hdr(skb
)->h_source
;
2966 if ((grec
->grec_type
== MLD2_CHANGE_TO_INCLUDE
||
2967 grec
->grec_type
== MLD2_MODE_IS_INCLUDE
) &&
2969 if (!pmctx
|| mldv1
) {
2970 br_ip6_multicast_leave_group(brmctx
, pmctx
,
2976 err
= br_ip6_multicast_add_group(brmctx
, pmctx
,
2977 &grec
->grec_mca
, vid
,
2983 if (!pmctx
|| mldv1
)
2986 spin_lock(&brmctx
->br
->multicast_lock
);
2987 if (!br_multicast_ctx_should_use(brmctx
, pmctx
))
2988 goto unlock_continue
;
2990 mdst
= br_mdb_ip6_get(brmctx
->br
, &grec
->grec_mca
, vid
);
2992 goto unlock_continue
;
2993 pg
= br_multicast_find_port(mdst
, pmctx
->port
, src
);
2994 if (!pg
|| (pg
->flags
& MDB_PG_FLAGS_PERMANENT
))
2995 goto unlock_continue
;
2996 h_addr
= &ipv6_hdr(skb
)->saddr
;
2997 switch (grec
->grec_type
) {
2998 case MLD2_ALLOW_NEW_SOURCES
:
2999 changed
= br_multicast_isinc_allow(brmctx
, pg
, h_addr
,
3000 grec
->grec_src
, nsrcs
,
3001 sizeof(struct in6_addr
),
3004 case MLD2_MODE_IS_INCLUDE
:
3005 changed
= br_multicast_isinc_allow(brmctx
, pg
, h_addr
,
3006 grec
->grec_src
, nsrcs
,
3007 sizeof(struct in6_addr
),
3010 case MLD2_MODE_IS_EXCLUDE
:
3011 changed
= br_multicast_isexc(brmctx
, pg
, h_addr
,
3012 grec
->grec_src
, nsrcs
,
3013 sizeof(struct in6_addr
),
3016 case MLD2_CHANGE_TO_INCLUDE
:
3017 changed
= br_multicast_toin(brmctx
, pmctx
, pg
, h_addr
,
3018 grec
->grec_src
, nsrcs
,
3019 sizeof(struct in6_addr
),
3022 case MLD2_CHANGE_TO_EXCLUDE
:
3023 changed
= br_multicast_toex(brmctx
, pmctx
, pg
, h_addr
,
3024 grec
->grec_src
, nsrcs
,
3025 sizeof(struct in6_addr
),
3028 case MLD2_BLOCK_OLD_SOURCES
:
3029 changed
= br_multicast_block(brmctx
, pmctx
, pg
, h_addr
,
3030 grec
->grec_src
, nsrcs
,
3031 sizeof(struct in6_addr
),
3036 br_mdb_notify(brmctx
->br
->dev
, mdst
, pg
, RTM_NEWMDB
);
3038 spin_unlock(&brmctx
->br
->multicast_lock
);
3045 static bool br_multicast_select_querier(struct net_bridge_mcast
*brmctx
,
3046 struct net_bridge_mcast_port
*pmctx
,
3047 struct br_ip
*saddr
)
3049 int port_ifidx
= pmctx
? pmctx
->port
->dev
->ifindex
: 0;
3050 struct timer_list
*own_timer
, *other_timer
;
3051 struct bridge_mcast_querier
*querier
;
3053 switch (saddr
->proto
) {
3054 case htons(ETH_P_IP
):
3055 querier
= &brmctx
->ip4_querier
;
3056 own_timer
= &brmctx
->ip4_own_query
.timer
;
3057 other_timer
= &brmctx
->ip4_other_query
.timer
;
3058 if (!querier
->addr
.src
.ip4
||
3059 ntohl(saddr
->src
.ip4
) <= ntohl(querier
->addr
.src
.ip4
))
3062 #if IS_ENABLED(CONFIG_IPV6)
3063 case htons(ETH_P_IPV6
):
3064 querier
= &brmctx
->ip6_querier
;
3065 own_timer
= &brmctx
->ip6_own_query
.timer
;
3066 other_timer
= &brmctx
->ip6_other_query
.timer
;
3067 if (ipv6_addr_cmp(&saddr
->src
.ip6
, &querier
->addr
.src
.ip6
) <= 0)
3075 if (!timer_pending(own_timer
) && !timer_pending(other_timer
))
3081 br_multicast_update_querier(brmctx
, querier
, port_ifidx
, saddr
);
3086 static struct net_bridge_port
*
3087 __br_multicast_get_querier_port(struct net_bridge
*br
,
3088 const struct bridge_mcast_querier
*querier
)
3090 int port_ifidx
= READ_ONCE(querier
->port_ifidx
);
3091 struct net_bridge_port
*p
;
3092 struct net_device
*dev
;
3094 if (port_ifidx
== 0)
3097 dev
= dev_get_by_index_rcu(dev_net(br
->dev
), port_ifidx
);
3100 p
= br_port_get_rtnl_rcu(dev
);
3101 if (!p
|| p
->br
!= br
)
3107 size_t br_multicast_querier_state_size(void)
3109 return nla_total_size(0) + /* nest attribute */
3110 nla_total_size(sizeof(__be32
)) + /* BRIDGE_QUERIER_IP_ADDRESS */
3111 nla_total_size(sizeof(int)) + /* BRIDGE_QUERIER_IP_PORT */
3112 nla_total_size_64bit(sizeof(u64
)) + /* BRIDGE_QUERIER_IP_OTHER_TIMER */
3113 #if IS_ENABLED(CONFIG_IPV6)
3114 nla_total_size(sizeof(struct in6_addr
)) + /* BRIDGE_QUERIER_IPV6_ADDRESS */
3115 nla_total_size(sizeof(int)) + /* BRIDGE_QUERIER_IPV6_PORT */
3116 nla_total_size_64bit(sizeof(u64
)) + /* BRIDGE_QUERIER_IPV6_OTHER_TIMER */
3121 /* protected by rtnl or rcu */
3122 int br_multicast_dump_querier_state(struct sk_buff
*skb
,
3123 const struct net_bridge_mcast
*brmctx
,
3126 struct bridge_mcast_querier querier
= {};
3127 struct net_bridge_port
*p
;
3128 struct nlattr
*nest
;
3130 if (!br_opt_get(brmctx
->br
, BROPT_MULTICAST_ENABLED
) ||
3131 br_multicast_ctx_vlan_global_disabled(brmctx
))
3134 nest
= nla_nest_start(skb
, nest_attr
);
3139 if (!brmctx
->multicast_querier
&&
3140 !timer_pending(&brmctx
->ip4_other_query
.timer
))
3143 br_multicast_read_querier(&brmctx
->ip4_querier
, &querier
);
3144 if (nla_put_in_addr(skb
, BRIDGE_QUERIER_IP_ADDRESS
,
3145 querier
.addr
.src
.ip4
)) {
3150 p
= __br_multicast_get_querier_port(brmctx
->br
, &querier
);
3151 if (timer_pending(&brmctx
->ip4_other_query
.timer
) &&
3152 (nla_put_u64_64bit(skb
, BRIDGE_QUERIER_IP_OTHER_TIMER
,
3153 br_timer_value(&brmctx
->ip4_other_query
.timer
),
3154 BRIDGE_QUERIER_PAD
) ||
3155 (p
&& nla_put_u32(skb
, BRIDGE_QUERIER_IP_PORT
, p
->dev
->ifindex
)))) {
3161 #if IS_ENABLED(CONFIG_IPV6)
3162 if (!brmctx
->multicast_querier
&&
3163 !timer_pending(&brmctx
->ip6_other_query
.timer
))
3166 br_multicast_read_querier(&brmctx
->ip6_querier
, &querier
);
3167 if (nla_put_in6_addr(skb
, BRIDGE_QUERIER_IPV6_ADDRESS
,
3168 &querier
.addr
.src
.ip6
)) {
3173 p
= __br_multicast_get_querier_port(brmctx
->br
, &querier
);
3174 if (timer_pending(&brmctx
->ip6_other_query
.timer
) &&
3175 (nla_put_u64_64bit(skb
, BRIDGE_QUERIER_IPV6_OTHER_TIMER
,
3176 br_timer_value(&brmctx
->ip6_other_query
.timer
),
3177 BRIDGE_QUERIER_PAD
) ||
3178 (p
&& nla_put_u32(skb
, BRIDGE_QUERIER_IPV6_PORT
,
3179 p
->dev
->ifindex
)))) {
3186 nla_nest_end(skb
, nest
);
3188 nla_nest_cancel(skb
, nest
);
3193 nla_nest_cancel(skb
, nest
);
3198 br_multicast_update_query_timer(struct net_bridge_mcast
*brmctx
,
3199 struct bridge_mcast_other_query
*query
,
3200 unsigned long max_delay
)
3202 if (!timer_pending(&query
->timer
))
3203 mod_timer(&query
->delay_timer
, jiffies
+ max_delay
);
3205 mod_timer(&query
->timer
, jiffies
+ brmctx
->multicast_querier_interval
);
3208 static void br_port_mc_router_state_change(struct net_bridge_port
*p
,
3211 struct switchdev_attr attr
= {
3213 .id
= SWITCHDEV_ATTR_ID_PORT_MROUTER
,
3214 .flags
= SWITCHDEV_F_DEFER
,
3215 .u
.mrouter
= is_mc_router
,
3218 switchdev_port_attr_set(p
->dev
, &attr
, NULL
);
3221 static struct net_bridge_port
*
3222 br_multicast_rport_from_node(struct net_bridge_mcast
*brmctx
,
3223 struct hlist_head
*mc_router_list
,
3224 struct hlist_node
*rlist
)
3226 struct net_bridge_mcast_port
*pmctx
;
3228 #if IS_ENABLED(CONFIG_IPV6)
3229 if (mc_router_list
== &brmctx
->ip6_mc_router_list
)
3230 pmctx
= hlist_entry(rlist
, struct net_bridge_mcast_port
,
3234 pmctx
= hlist_entry(rlist
, struct net_bridge_mcast_port
,
3240 static struct hlist_node
*
3241 br_multicast_get_rport_slot(struct net_bridge_mcast
*brmctx
,
3242 struct net_bridge_port
*port
,
3243 struct hlist_head
*mc_router_list
)
3246 struct hlist_node
*slot
= NULL
;
3247 struct net_bridge_port
*p
;
3248 struct hlist_node
*rlist
;
3250 hlist_for_each(rlist
, mc_router_list
) {
3251 p
= br_multicast_rport_from_node(brmctx
, mc_router_list
, rlist
);
3253 if ((unsigned long)port
>= (unsigned long)p
)
3262 static bool br_multicast_no_router_otherpf(struct net_bridge_mcast_port
*pmctx
,
3263 struct hlist_node
*rnode
)
3265 #if IS_ENABLED(CONFIG_IPV6)
3266 if (rnode
!= &pmctx
->ip6_rlist
)
3267 return hlist_unhashed(&pmctx
->ip6_rlist
);
3269 return hlist_unhashed(&pmctx
->ip4_rlist
);
3275 /* Add port to router_list
3276 * list is maintained ordered by pointer value
3277 * and locked by br->multicast_lock and RCU
3279 static void br_multicast_add_router(struct net_bridge_mcast
*brmctx
,
3280 struct net_bridge_mcast_port
*pmctx
,
3281 struct hlist_node
*rlist
,
3282 struct hlist_head
*mc_router_list
)
3284 struct hlist_node
*slot
;
3286 if (!hlist_unhashed(rlist
))
3289 slot
= br_multicast_get_rport_slot(brmctx
, pmctx
->port
, mc_router_list
);
3292 hlist_add_behind_rcu(rlist
, slot
);
3294 hlist_add_head_rcu(rlist
, mc_router_list
);
3296 /* For backwards compatibility for now, only notify if we
3297 * switched from no IPv4/IPv6 multicast router to a new
3298 * IPv4 or IPv6 multicast router.
3300 if (br_multicast_no_router_otherpf(pmctx
, rlist
)) {
3301 br_rtr_notify(pmctx
->port
->br
->dev
, pmctx
, RTM_NEWMDB
);
3302 br_port_mc_router_state_change(pmctx
->port
, true);
3306 /* Add port to router_list
3307 * list is maintained ordered by pointer value
3308 * and locked by br->multicast_lock and RCU
3310 static void br_ip4_multicast_add_router(struct net_bridge_mcast
*brmctx
,
3311 struct net_bridge_mcast_port
*pmctx
)
3313 br_multicast_add_router(brmctx
, pmctx
, &pmctx
->ip4_rlist
,
3314 &brmctx
->ip4_mc_router_list
);
3317 /* Add port to router_list
3318 * list is maintained ordered by pointer value
3319 * and locked by br->multicast_lock and RCU
3321 static void br_ip6_multicast_add_router(struct net_bridge_mcast
*brmctx
,
3322 struct net_bridge_mcast_port
*pmctx
)
3324 #if IS_ENABLED(CONFIG_IPV6)
3325 br_multicast_add_router(brmctx
, pmctx
, &pmctx
->ip6_rlist
,
3326 &brmctx
->ip6_mc_router_list
);
3330 static void br_multicast_mark_router(struct net_bridge_mcast
*brmctx
,
3331 struct net_bridge_mcast_port
*pmctx
,
3332 struct timer_list
*timer
,
3333 struct hlist_node
*rlist
,
3334 struct hlist_head
*mc_router_list
)
3336 unsigned long now
= jiffies
;
3338 if (!br_multicast_ctx_should_use(brmctx
, pmctx
))
3342 if (brmctx
->multicast_router
== MDB_RTR_TYPE_TEMP_QUERY
) {
3343 if (!br_ip4_multicast_is_router(brmctx
) &&
3344 !br_ip6_multicast_is_router(brmctx
))
3345 br_mc_router_state_change(brmctx
->br
, true);
3346 mod_timer(timer
, now
+ brmctx
->multicast_querier_interval
);
3351 if (pmctx
->multicast_router
== MDB_RTR_TYPE_DISABLED
||
3352 pmctx
->multicast_router
== MDB_RTR_TYPE_PERM
)
3355 br_multicast_add_router(brmctx
, pmctx
, rlist
, mc_router_list
);
3356 mod_timer(timer
, now
+ brmctx
->multicast_querier_interval
);
3359 static void br_ip4_multicast_mark_router(struct net_bridge_mcast
*brmctx
,
3360 struct net_bridge_mcast_port
*pmctx
)
3362 struct timer_list
*timer
= &brmctx
->ip4_mc_router_timer
;
3363 struct hlist_node
*rlist
= NULL
;
3366 timer
= &pmctx
->ip4_mc_router_timer
;
3367 rlist
= &pmctx
->ip4_rlist
;
3370 br_multicast_mark_router(brmctx
, pmctx
, timer
, rlist
,
3371 &brmctx
->ip4_mc_router_list
);
3374 static void br_ip6_multicast_mark_router(struct net_bridge_mcast
*brmctx
,
3375 struct net_bridge_mcast_port
*pmctx
)
3377 #if IS_ENABLED(CONFIG_IPV6)
3378 struct timer_list
*timer
= &brmctx
->ip6_mc_router_timer
;
3379 struct hlist_node
*rlist
= NULL
;
3382 timer
= &pmctx
->ip6_mc_router_timer
;
3383 rlist
= &pmctx
->ip6_rlist
;
3386 br_multicast_mark_router(brmctx
, pmctx
, timer
, rlist
,
3387 &brmctx
->ip6_mc_router_list
);
3392 br_ip4_multicast_query_received(struct net_bridge_mcast
*brmctx
,
3393 struct net_bridge_mcast_port
*pmctx
,
3394 struct bridge_mcast_other_query
*query
,
3395 struct br_ip
*saddr
,
3396 unsigned long max_delay
)
3398 if (!br_multicast_select_querier(brmctx
, pmctx
, saddr
))
3401 br_multicast_update_query_timer(brmctx
, query
, max_delay
);
3402 br_ip4_multicast_mark_router(brmctx
, pmctx
);
3405 #if IS_ENABLED(CONFIG_IPV6)
3407 br_ip6_multicast_query_received(struct net_bridge_mcast
*brmctx
,
3408 struct net_bridge_mcast_port
*pmctx
,
3409 struct bridge_mcast_other_query
*query
,
3410 struct br_ip
*saddr
,
3411 unsigned long max_delay
)
3413 if (!br_multicast_select_querier(brmctx
, pmctx
, saddr
))
3416 br_multicast_update_query_timer(brmctx
, query
, max_delay
);
3417 br_ip6_multicast_mark_router(brmctx
, pmctx
);
3421 static void br_ip4_multicast_query(struct net_bridge_mcast
*brmctx
,
3422 struct net_bridge_mcast_port
*pmctx
,
3423 struct sk_buff
*skb
,
3426 unsigned int transport_len
= ip_transport_len(skb
);
3427 const struct iphdr
*iph
= ip_hdr(skb
);
3428 struct igmphdr
*ih
= igmp_hdr(skb
);
3429 struct net_bridge_mdb_entry
*mp
;
3430 struct igmpv3_query
*ih3
;
3431 struct net_bridge_port_group
*p
;
3432 struct net_bridge_port_group __rcu
**pp
;
3433 struct br_ip saddr
= {};
3434 unsigned long max_delay
;
3435 unsigned long now
= jiffies
;
3438 spin_lock(&brmctx
->br
->multicast_lock
);
3439 if (!br_multicast_ctx_should_use(brmctx
, pmctx
))
3444 if (transport_len
== sizeof(*ih
)) {
3445 max_delay
= ih
->code
* (HZ
/ IGMP_TIMER_SCALE
);
3448 max_delay
= 10 * HZ
;
3451 } else if (transport_len
>= sizeof(*ih3
)) {
3452 ih3
= igmpv3_query_hdr(skb
);
3454 (brmctx
->multicast_igmp_version
== 3 && group
&&
3458 max_delay
= ih3
->code
?
3459 IGMPV3_MRC(ih3
->code
) * (HZ
/ IGMP_TIMER_SCALE
) : 1;
3465 saddr
.proto
= htons(ETH_P_IP
);
3466 saddr
.src
.ip4
= iph
->saddr
;
3468 br_ip4_multicast_query_received(brmctx
, pmctx
,
3469 &brmctx
->ip4_other_query
,
3474 mp
= br_mdb_ip4_get(brmctx
->br
, group
, vid
);
3478 max_delay
*= brmctx
->multicast_last_member_count
;
3480 if (mp
->host_joined
&&
3481 (timer_pending(&mp
->timer
) ?
3482 time_after(mp
->timer
.expires
, now
+ max_delay
) :
3483 try_to_del_timer_sync(&mp
->timer
) >= 0))
3484 mod_timer(&mp
->timer
, now
+ max_delay
);
3486 for (pp
= &mp
->ports
;
3487 (p
= mlock_dereference(*pp
, brmctx
->br
)) != NULL
;
3489 if (timer_pending(&p
->timer
) ?
3490 time_after(p
->timer
.expires
, now
+ max_delay
) :
3491 try_to_del_timer_sync(&p
->timer
) >= 0 &&
3492 (brmctx
->multicast_igmp_version
== 2 ||
3493 p
->filter_mode
== MCAST_EXCLUDE
))
3494 mod_timer(&p
->timer
, now
+ max_delay
);
3498 spin_unlock(&brmctx
->br
->multicast_lock
);
3501 #if IS_ENABLED(CONFIG_IPV6)
3502 static int br_ip6_multicast_query(struct net_bridge_mcast
*brmctx
,
3503 struct net_bridge_mcast_port
*pmctx
,
3504 struct sk_buff
*skb
,
3507 unsigned int transport_len
= ipv6_transport_len(skb
);
3508 struct mld_msg
*mld
;
3509 struct net_bridge_mdb_entry
*mp
;
3510 struct mld2_query
*mld2q
;
3511 struct net_bridge_port_group
*p
;
3512 struct net_bridge_port_group __rcu
**pp
;
3513 struct br_ip saddr
= {};
3514 unsigned long max_delay
;
3515 unsigned long now
= jiffies
;
3516 unsigned int offset
= skb_transport_offset(skb
);
3517 const struct in6_addr
*group
= NULL
;
3518 bool is_general_query
;
3521 spin_lock(&brmctx
->br
->multicast_lock
);
3522 if (!br_multicast_ctx_should_use(brmctx
, pmctx
))
3525 if (transport_len
== sizeof(*mld
)) {
3526 if (!pskb_may_pull(skb
, offset
+ sizeof(*mld
))) {
3530 mld
= (struct mld_msg
*) icmp6_hdr(skb
);
3531 max_delay
= msecs_to_jiffies(ntohs(mld
->mld_maxdelay
));
3533 group
= &mld
->mld_mca
;
3535 if (!pskb_may_pull(skb
, offset
+ sizeof(*mld2q
))) {
3539 mld2q
= (struct mld2_query
*)icmp6_hdr(skb
);
3540 if (!mld2q
->mld2q_nsrcs
)
3541 group
= &mld2q
->mld2q_mca
;
3542 if (brmctx
->multicast_mld_version
== 2 &&
3543 !ipv6_addr_any(&mld2q
->mld2q_mca
) &&
3544 mld2q
->mld2q_suppress
)
3547 max_delay
= max(msecs_to_jiffies(mldv2_mrc(mld2q
)), 1UL);
3550 is_general_query
= group
&& ipv6_addr_any(group
);
3552 if (is_general_query
) {
3553 saddr
.proto
= htons(ETH_P_IPV6
);
3554 saddr
.src
.ip6
= ipv6_hdr(skb
)->saddr
;
3556 br_ip6_multicast_query_received(brmctx
, pmctx
,
3557 &brmctx
->ip6_other_query
,
3560 } else if (!group
) {
3564 mp
= br_mdb_ip6_get(brmctx
->br
, group
, vid
);
3568 max_delay
*= brmctx
->multicast_last_member_count
;
3569 if (mp
->host_joined
&&
3570 (timer_pending(&mp
->timer
) ?
3571 time_after(mp
->timer
.expires
, now
+ max_delay
) :
3572 try_to_del_timer_sync(&mp
->timer
) >= 0))
3573 mod_timer(&mp
->timer
, now
+ max_delay
);
3575 for (pp
= &mp
->ports
;
3576 (p
= mlock_dereference(*pp
, brmctx
->br
)) != NULL
;
3578 if (timer_pending(&p
->timer
) ?
3579 time_after(p
->timer
.expires
, now
+ max_delay
) :
3580 try_to_del_timer_sync(&p
->timer
) >= 0 &&
3581 (brmctx
->multicast_mld_version
== 1 ||
3582 p
->filter_mode
== MCAST_EXCLUDE
))
3583 mod_timer(&p
->timer
, now
+ max_delay
);
3587 spin_unlock(&brmctx
->br
->multicast_lock
);
3593 br_multicast_leave_group(struct net_bridge_mcast
*brmctx
,
3594 struct net_bridge_mcast_port
*pmctx
,
3595 struct br_ip
*group
,
3596 struct bridge_mcast_other_query
*other_query
,
3597 struct bridge_mcast_own_query
*own_query
,
3598 const unsigned char *src
)
3600 struct net_bridge_mdb_entry
*mp
;
3601 struct net_bridge_port_group
*p
;
3605 spin_lock(&brmctx
->br
->multicast_lock
);
3606 if (!br_multicast_ctx_should_use(brmctx
, pmctx
))
3609 mp
= br_mdb_ip_get(brmctx
->br
, group
);
3613 if (pmctx
&& (pmctx
->port
->flags
& BR_MULTICAST_FAST_LEAVE
)) {
3614 struct net_bridge_port_group __rcu
**pp
;
3616 for (pp
= &mp
->ports
;
3617 (p
= mlock_dereference(*pp
, brmctx
->br
)) != NULL
;
3619 if (!br_port_group_equal(p
, pmctx
->port
, src
))
3622 if (p
->flags
& MDB_PG_FLAGS_PERMANENT
)
3625 p
->flags
|= MDB_PG_FLAGS_FAST_LEAVE
;
3626 br_multicast_del_pg(mp
, p
, pp
);
3631 if (timer_pending(&other_query
->timer
))
3634 if (brmctx
->multicast_querier
) {
3635 __br_multicast_send_query(brmctx
, pmctx
, NULL
, NULL
, &mp
->addr
,
3638 time
= jiffies
+ brmctx
->multicast_last_member_count
*
3639 brmctx
->multicast_last_member_interval
;
3641 mod_timer(&own_query
->timer
, time
);
3643 for (p
= mlock_dereference(mp
->ports
, brmctx
->br
);
3644 p
!= NULL
&& pmctx
!= NULL
;
3645 p
= mlock_dereference(p
->next
, brmctx
->br
)) {
3646 if (!br_port_group_equal(p
, pmctx
->port
, src
))
3649 if (!hlist_unhashed(&p
->mglist
) &&
3650 (timer_pending(&p
->timer
) ?
3651 time_after(p
->timer
.expires
, time
) :
3652 try_to_del_timer_sync(&p
->timer
) >= 0)) {
3653 mod_timer(&p
->timer
, time
);
3661 time
= now
+ brmctx
->multicast_last_member_count
*
3662 brmctx
->multicast_last_member_interval
;
3665 if (mp
->host_joined
&&
3666 (timer_pending(&mp
->timer
) ?
3667 time_after(mp
->timer
.expires
, time
) :
3668 try_to_del_timer_sync(&mp
->timer
) >= 0)) {
3669 mod_timer(&mp
->timer
, time
);
3675 for (p
= mlock_dereference(mp
->ports
, brmctx
->br
);
3677 p
= mlock_dereference(p
->next
, brmctx
->br
)) {
3678 if (p
->key
.port
!= pmctx
->port
)
3681 if (!hlist_unhashed(&p
->mglist
) &&
3682 (timer_pending(&p
->timer
) ?
3683 time_after(p
->timer
.expires
, time
) :
3684 try_to_del_timer_sync(&p
->timer
) >= 0)) {
3685 mod_timer(&p
->timer
, time
);
3691 spin_unlock(&brmctx
->br
->multicast_lock
);
3694 static void br_ip4_multicast_leave_group(struct net_bridge_mcast
*brmctx
,
3695 struct net_bridge_mcast_port
*pmctx
,
3698 const unsigned char *src
)
3700 struct br_ip br_group
;
3701 struct bridge_mcast_own_query
*own_query
;
3703 if (ipv4_is_local_multicast(group
))
3706 own_query
= pmctx
? &pmctx
->ip4_own_query
: &brmctx
->ip4_own_query
;
3708 memset(&br_group
, 0, sizeof(br_group
));
3709 br_group
.dst
.ip4
= group
;
3710 br_group
.proto
= htons(ETH_P_IP
);
3713 br_multicast_leave_group(brmctx
, pmctx
, &br_group
,
3714 &brmctx
->ip4_other_query
,
3718 #if IS_ENABLED(CONFIG_IPV6)
3719 static void br_ip6_multicast_leave_group(struct net_bridge_mcast
*brmctx
,
3720 struct net_bridge_mcast_port
*pmctx
,
3721 const struct in6_addr
*group
,
3723 const unsigned char *src
)
3725 struct br_ip br_group
;
3726 struct bridge_mcast_own_query
*own_query
;
3728 if (ipv6_addr_is_ll_all_nodes(group
))
3731 own_query
= pmctx
? &pmctx
->ip6_own_query
: &brmctx
->ip6_own_query
;
3733 memset(&br_group
, 0, sizeof(br_group
));
3734 br_group
.dst
.ip6
= *group
;
3735 br_group
.proto
= htons(ETH_P_IPV6
);
3738 br_multicast_leave_group(brmctx
, pmctx
, &br_group
,
3739 &brmctx
->ip6_other_query
,
3744 static void br_multicast_err_count(const struct net_bridge
*br
,
3745 const struct net_bridge_port
*p
,
3748 struct bridge_mcast_stats __percpu
*stats
;
3749 struct bridge_mcast_stats
*pstats
;
3751 if (!br_opt_get(br
, BROPT_MULTICAST_STATS_ENABLED
))
3755 stats
= p
->mcast_stats
;
3757 stats
= br
->mcast_stats
;
3758 if (WARN_ON(!stats
))
3761 pstats
= this_cpu_ptr(stats
);
3763 u64_stats_update_begin(&pstats
->syncp
);
3765 case htons(ETH_P_IP
):
3766 pstats
->mstats
.igmp_parse_errors
++;
3768 #if IS_ENABLED(CONFIG_IPV6)
3769 case htons(ETH_P_IPV6
):
3770 pstats
->mstats
.mld_parse_errors
++;
3774 u64_stats_update_end(&pstats
->syncp
);
3777 static void br_multicast_pim(struct net_bridge_mcast
*brmctx
,
3778 struct net_bridge_mcast_port
*pmctx
,
3779 const struct sk_buff
*skb
)
3781 unsigned int offset
= skb_transport_offset(skb
);
3782 struct pimhdr
*pimhdr
, _pimhdr
;
3784 pimhdr
= skb_header_pointer(skb
, offset
, sizeof(_pimhdr
), &_pimhdr
);
3785 if (!pimhdr
|| pim_hdr_version(pimhdr
) != PIM_VERSION
||
3786 pim_hdr_type(pimhdr
) != PIM_TYPE_HELLO
)
3789 spin_lock(&brmctx
->br
->multicast_lock
);
3790 br_ip4_multicast_mark_router(brmctx
, pmctx
);
3791 spin_unlock(&brmctx
->br
->multicast_lock
);
3794 static int br_ip4_multicast_mrd_rcv(struct net_bridge_mcast
*brmctx
,
3795 struct net_bridge_mcast_port
*pmctx
,
3796 struct sk_buff
*skb
)
3798 if (ip_hdr(skb
)->protocol
!= IPPROTO_IGMP
||
3799 igmp_hdr(skb
)->type
!= IGMP_MRDISC_ADV
)
3802 spin_lock(&brmctx
->br
->multicast_lock
);
3803 br_ip4_multicast_mark_router(brmctx
, pmctx
);
3804 spin_unlock(&brmctx
->br
->multicast_lock
);
3809 static int br_multicast_ipv4_rcv(struct net_bridge_mcast
*brmctx
,
3810 struct net_bridge_mcast_port
*pmctx
,
3811 struct sk_buff
*skb
,
3814 struct net_bridge_port
*p
= pmctx
? pmctx
->port
: NULL
;
3815 const unsigned char *src
;
3819 err
= ip_mc_check_igmp(skb
);
3821 if (err
== -ENOMSG
) {
3822 if (!ipv4_is_local_multicast(ip_hdr(skb
)->daddr
)) {
3823 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
3824 } else if (pim_ipv4_all_pim_routers(ip_hdr(skb
)->daddr
)) {
3825 if (ip_hdr(skb
)->protocol
== IPPROTO_PIM
)
3826 br_multicast_pim(brmctx
, pmctx
, skb
);
3827 } else if (ipv4_is_all_snoopers(ip_hdr(skb
)->daddr
)) {
3828 br_ip4_multicast_mrd_rcv(brmctx
, pmctx
, skb
);
3832 } else if (err
< 0) {
3833 br_multicast_err_count(brmctx
->br
, p
, skb
->protocol
);
3838 src
= eth_hdr(skb
)->h_source
;
3839 BR_INPUT_SKB_CB(skb
)->igmp
= ih
->type
;
3842 case IGMP_HOST_MEMBERSHIP_REPORT
:
3843 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
3844 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
3845 err
= br_ip4_multicast_add_group(brmctx
, pmctx
, ih
->group
, vid
,
3848 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
3849 err
= br_ip4_multicast_igmp3_report(brmctx
, pmctx
, skb
, vid
);
3851 case IGMP_HOST_MEMBERSHIP_QUERY
:
3852 br_ip4_multicast_query(brmctx
, pmctx
, skb
, vid
);
3854 case IGMP_HOST_LEAVE_MESSAGE
:
3855 br_ip4_multicast_leave_group(brmctx
, pmctx
, ih
->group
, vid
, src
);
3859 br_multicast_count(brmctx
->br
, p
, skb
, BR_INPUT_SKB_CB(skb
)->igmp
,
3865 #if IS_ENABLED(CONFIG_IPV6)
3866 static void br_ip6_multicast_mrd_rcv(struct net_bridge_mcast
*brmctx
,
3867 struct net_bridge_mcast_port
*pmctx
,
3868 struct sk_buff
*skb
)
3870 if (icmp6_hdr(skb
)->icmp6_type
!= ICMPV6_MRDISC_ADV
)
3873 spin_lock(&brmctx
->br
->multicast_lock
);
3874 br_ip6_multicast_mark_router(brmctx
, pmctx
);
3875 spin_unlock(&brmctx
->br
->multicast_lock
);
3878 static int br_multicast_ipv6_rcv(struct net_bridge_mcast
*brmctx
,
3879 struct net_bridge_mcast_port
*pmctx
,
3880 struct sk_buff
*skb
,
3883 struct net_bridge_port
*p
= pmctx
? pmctx
->port
: NULL
;
3884 const unsigned char *src
;
3885 struct mld_msg
*mld
;
3888 err
= ipv6_mc_check_mld(skb
);
3890 if (err
== -ENOMSG
|| err
== -ENODATA
) {
3891 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb
)->daddr
))
3892 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
3893 if (err
== -ENODATA
&&
3894 ipv6_addr_is_all_snoopers(&ipv6_hdr(skb
)->daddr
))
3895 br_ip6_multicast_mrd_rcv(brmctx
, pmctx
, skb
);
3898 } else if (err
< 0) {
3899 br_multicast_err_count(brmctx
->br
, p
, skb
->protocol
);
3903 mld
= (struct mld_msg
*)skb_transport_header(skb
);
3904 BR_INPUT_SKB_CB(skb
)->igmp
= mld
->mld_type
;
3906 switch (mld
->mld_type
) {
3907 case ICMPV6_MGM_REPORT
:
3908 src
= eth_hdr(skb
)->h_source
;
3909 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
3910 err
= br_ip6_multicast_add_group(brmctx
, pmctx
, &mld
->mld_mca
,
3913 case ICMPV6_MLD2_REPORT
:
3914 err
= br_ip6_multicast_mld2_report(brmctx
, pmctx
, skb
, vid
);
3916 case ICMPV6_MGM_QUERY
:
3917 err
= br_ip6_multicast_query(brmctx
, pmctx
, skb
, vid
);
3919 case ICMPV6_MGM_REDUCTION
:
3920 src
= eth_hdr(skb
)->h_source
;
3921 br_ip6_multicast_leave_group(brmctx
, pmctx
, &mld
->mld_mca
, vid
,
3926 br_multicast_count(brmctx
->br
, p
, skb
, BR_INPUT_SKB_CB(skb
)->igmp
,
3933 int br_multicast_rcv(struct net_bridge_mcast
**brmctx
,
3934 struct net_bridge_mcast_port
**pmctx
,
3935 struct net_bridge_vlan
*vlan
,
3936 struct sk_buff
*skb
, u16 vid
)
3940 BR_INPUT_SKB_CB(skb
)->igmp
= 0;
3941 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 0;
3943 if (!br_opt_get((*brmctx
)->br
, BROPT_MULTICAST_ENABLED
))
3946 if (br_opt_get((*brmctx
)->br
, BROPT_MCAST_VLAN_SNOOPING_ENABLED
) && vlan
) {
3947 const struct net_bridge_vlan
*masterv
;
3949 /* the vlan has the master flag set only when transmitting
3950 * through the bridge device
3952 if (br_vlan_is_master(vlan
)) {
3954 *brmctx
= &vlan
->br_mcast_ctx
;
3957 masterv
= vlan
->brvlan
;
3958 *brmctx
= &vlan
->brvlan
->br_mcast_ctx
;
3959 *pmctx
= &vlan
->port_mcast_ctx
;
3962 if (!(masterv
->priv_flags
& BR_VLFLAG_GLOBAL_MCAST_ENABLED
))
3966 switch (skb
->protocol
) {
3967 case htons(ETH_P_IP
):
3968 ret
= br_multicast_ipv4_rcv(*brmctx
, *pmctx
, skb
, vid
);
3970 #if IS_ENABLED(CONFIG_IPV6)
3971 case htons(ETH_P_IPV6
):
3972 ret
= br_multicast_ipv6_rcv(*brmctx
, *pmctx
, skb
, vid
);
3980 static void br_multicast_query_expired(struct net_bridge_mcast
*brmctx
,
3981 struct bridge_mcast_own_query
*query
,
3982 struct bridge_mcast_querier
*querier
)
3984 spin_lock(&brmctx
->br
->multicast_lock
);
3985 if (br_multicast_ctx_vlan_disabled(brmctx
))
3988 if (query
->startup_sent
< brmctx
->multicast_startup_query_count
)
3989 query
->startup_sent
++;
3991 br_multicast_send_query(brmctx
, NULL
, query
);
3993 spin_unlock(&brmctx
->br
->multicast_lock
);
3996 static void br_ip4_multicast_query_expired(struct timer_list
*t
)
3998 struct net_bridge_mcast
*brmctx
= from_timer(brmctx
, t
,
3999 ip4_own_query
.timer
);
4001 br_multicast_query_expired(brmctx
, &brmctx
->ip4_own_query
,
4002 &brmctx
->ip4_querier
);
4005 #if IS_ENABLED(CONFIG_IPV6)
4006 static void br_ip6_multicast_query_expired(struct timer_list
*t
)
4008 struct net_bridge_mcast
*brmctx
= from_timer(brmctx
, t
,
4009 ip6_own_query
.timer
);
4011 br_multicast_query_expired(brmctx
, &brmctx
->ip6_own_query
,
4012 &brmctx
->ip6_querier
);
4016 static void br_multicast_gc_work(struct work_struct
*work
)
4018 struct net_bridge
*br
= container_of(work
, struct net_bridge
,
4020 HLIST_HEAD(deleted_head
);
4022 spin_lock_bh(&br
->multicast_lock
);
4023 hlist_move_list(&br
->mcast_gc_list
, &deleted_head
);
4024 spin_unlock_bh(&br
->multicast_lock
);
4026 br_multicast_gc(&deleted_head
);
4029 void br_multicast_ctx_init(struct net_bridge
*br
,
4030 struct net_bridge_vlan
*vlan
,
4031 struct net_bridge_mcast
*brmctx
)
4034 brmctx
->vlan
= vlan
;
4035 brmctx
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
4036 brmctx
->multicast_last_member_count
= 2;
4037 brmctx
->multicast_startup_query_count
= 2;
4039 brmctx
->multicast_last_member_interval
= HZ
;
4040 brmctx
->multicast_query_response_interval
= 10 * HZ
;
4041 brmctx
->multicast_startup_query_interval
= 125 * HZ
/ 4;
4042 brmctx
->multicast_query_interval
= 125 * HZ
;
4043 brmctx
->multicast_querier_interval
= 255 * HZ
;
4044 brmctx
->multicast_membership_interval
= 260 * HZ
;
4046 brmctx
->ip4_querier
.port_ifidx
= 0;
4047 seqcount_spinlock_init(&brmctx
->ip4_querier
.seq
, &br
->multicast_lock
);
4048 brmctx
->multicast_igmp_version
= 2;
4049 #if IS_ENABLED(CONFIG_IPV6)
4050 brmctx
->multicast_mld_version
= 1;
4051 brmctx
->ip6_querier
.port_ifidx
= 0;
4052 seqcount_spinlock_init(&brmctx
->ip6_querier
.seq
, &br
->multicast_lock
);
4055 timer_setup(&brmctx
->ip4_mc_router_timer
,
4056 br_ip4_multicast_local_router_expired
, 0);
4057 timer_setup(&brmctx
->ip4_other_query
.timer
,
4058 br_ip4_multicast_querier_expired
, 0);
4059 timer_setup(&brmctx
->ip4_other_query
.delay_timer
,
4060 br_multicast_query_delay_expired
, 0);
4061 timer_setup(&brmctx
->ip4_own_query
.timer
,
4062 br_ip4_multicast_query_expired
, 0);
4063 #if IS_ENABLED(CONFIG_IPV6)
4064 timer_setup(&brmctx
->ip6_mc_router_timer
,
4065 br_ip6_multicast_local_router_expired
, 0);
4066 timer_setup(&brmctx
->ip6_other_query
.timer
,
4067 br_ip6_multicast_querier_expired
, 0);
4068 timer_setup(&brmctx
->ip6_other_query
.delay_timer
,
4069 br_multicast_query_delay_expired
, 0);
4070 timer_setup(&brmctx
->ip6_own_query
.timer
,
4071 br_ip6_multicast_query_expired
, 0);
4075 void br_multicast_ctx_deinit(struct net_bridge_mcast
*brmctx
)
4077 __br_multicast_stop(brmctx
);
4080 void br_multicast_init(struct net_bridge
*br
)
4082 br
->hash_max
= BR_MULTICAST_DEFAULT_HASH_MAX
;
4084 br_multicast_ctx_init(br
, NULL
, &br
->multicast_ctx
);
4086 br_opt_toggle(br
, BROPT_MULTICAST_ENABLED
, true);
4087 br_opt_toggle(br
, BROPT_HAS_IPV6_ADDR
, true);
4089 spin_lock_init(&br
->multicast_lock
);
4090 INIT_HLIST_HEAD(&br
->mdb_list
);
4091 INIT_HLIST_HEAD(&br
->mcast_gc_list
);
4092 INIT_WORK(&br
->mcast_gc_work
, br_multicast_gc_work
);
4095 static void br_ip4_multicast_join_snoopers(struct net_bridge
*br
)
4097 struct in_device
*in_dev
= in_dev_get(br
->dev
);
4102 __ip_mc_inc_group(in_dev
, htonl(INADDR_ALLSNOOPERS_GROUP
), GFP_ATOMIC
);
4106 #if IS_ENABLED(CONFIG_IPV6)
4107 static void br_ip6_multicast_join_snoopers(struct net_bridge
*br
)
4109 struct in6_addr addr
;
4111 ipv6_addr_set(&addr
, htonl(0xff020000), 0, 0, htonl(0x6a));
4112 ipv6_dev_mc_inc(br
->dev
, &addr
);
4115 static inline void br_ip6_multicast_join_snoopers(struct net_bridge
*br
)
4120 void br_multicast_join_snoopers(struct net_bridge
*br
)
4122 br_ip4_multicast_join_snoopers(br
);
4123 br_ip6_multicast_join_snoopers(br
);
4126 static void br_ip4_multicast_leave_snoopers(struct net_bridge
*br
)
4128 struct in_device
*in_dev
= in_dev_get(br
->dev
);
4130 if (WARN_ON(!in_dev
))
4133 __ip_mc_dec_group(in_dev
, htonl(INADDR_ALLSNOOPERS_GROUP
), GFP_ATOMIC
);
4137 #if IS_ENABLED(CONFIG_IPV6)
4138 static void br_ip6_multicast_leave_snoopers(struct net_bridge
*br
)
4140 struct in6_addr addr
;
4142 ipv6_addr_set(&addr
, htonl(0xff020000), 0, 0, htonl(0x6a));
4143 ipv6_dev_mc_dec(br
->dev
, &addr
);
4146 static inline void br_ip6_multicast_leave_snoopers(struct net_bridge
*br
)
4151 void br_multicast_leave_snoopers(struct net_bridge
*br
)
4153 br_ip4_multicast_leave_snoopers(br
);
4154 br_ip6_multicast_leave_snoopers(br
);
4157 static void __br_multicast_open_query(struct net_bridge
*br
,
4158 struct bridge_mcast_own_query
*query
)
4160 query
->startup_sent
= 0;
4162 if (!br_opt_get(br
, BROPT_MULTICAST_ENABLED
))
4165 mod_timer(&query
->timer
, jiffies
);
4168 static void __br_multicast_open(struct net_bridge_mcast
*brmctx
)
4170 __br_multicast_open_query(brmctx
->br
, &brmctx
->ip4_own_query
);
4171 #if IS_ENABLED(CONFIG_IPV6)
4172 __br_multicast_open_query(brmctx
->br
, &brmctx
->ip6_own_query
);
4176 void br_multicast_open(struct net_bridge
*br
)
4180 if (br_opt_get(br
, BROPT_MCAST_VLAN_SNOOPING_ENABLED
)) {
4181 struct net_bridge_vlan_group
*vg
;
4182 struct net_bridge_vlan
*vlan
;
4184 vg
= br_vlan_group(br
);
4186 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
) {
4187 struct net_bridge_mcast
*brmctx
;
4189 brmctx
= &vlan
->br_mcast_ctx
;
4190 if (br_vlan_is_brentry(vlan
) &&
4191 !br_multicast_ctx_vlan_disabled(brmctx
))
4192 __br_multicast_open(&vlan
->br_mcast_ctx
);
4196 __br_multicast_open(&br
->multicast_ctx
);
4200 static void __br_multicast_stop(struct net_bridge_mcast
*brmctx
)
4202 del_timer_sync(&brmctx
->ip4_mc_router_timer
);
4203 del_timer_sync(&brmctx
->ip4_other_query
.timer
);
4204 del_timer_sync(&brmctx
->ip4_other_query
.delay_timer
);
4205 del_timer_sync(&brmctx
->ip4_own_query
.timer
);
4206 #if IS_ENABLED(CONFIG_IPV6)
4207 del_timer_sync(&brmctx
->ip6_mc_router_timer
);
4208 del_timer_sync(&brmctx
->ip6_other_query
.timer
);
4209 del_timer_sync(&brmctx
->ip6_other_query
.delay_timer
);
4210 del_timer_sync(&brmctx
->ip6_own_query
.timer
);
4214 void br_multicast_toggle_one_vlan(struct net_bridge_vlan
*vlan
, bool on
)
4216 struct net_bridge
*br
;
4218 /* it's okay to check for the flag without the multicast lock because it
4219 * can only change under RTNL -> multicast_lock, we need the latter to
4220 * sync with timers and packets
4222 if (on
== !!(vlan
->priv_flags
& BR_VLFLAG_MCAST_ENABLED
))
4225 if (br_vlan_is_master(vlan
)) {
4228 if (!br_vlan_is_brentry(vlan
) ||
4230 br_multicast_ctx_vlan_global_disabled(&vlan
->br_mcast_ctx
)))
4233 spin_lock_bh(&br
->multicast_lock
);
4234 vlan
->priv_flags
^= BR_VLFLAG_MCAST_ENABLED
;
4235 spin_unlock_bh(&br
->multicast_lock
);
4238 __br_multicast_open(&vlan
->br_mcast_ctx
);
4240 __br_multicast_stop(&vlan
->br_mcast_ctx
);
4242 struct net_bridge_mcast
*brmctx
;
4244 brmctx
= br_multicast_port_ctx_get_global(&vlan
->port_mcast_ctx
);
4245 if (on
&& br_multicast_ctx_vlan_global_disabled(brmctx
))
4248 br
= vlan
->port
->br
;
4249 spin_lock_bh(&br
->multicast_lock
);
4250 vlan
->priv_flags
^= BR_VLFLAG_MCAST_ENABLED
;
4252 __br_multicast_enable_port_ctx(&vlan
->port_mcast_ctx
);
4254 __br_multicast_disable_port_ctx(&vlan
->port_mcast_ctx
);
4255 spin_unlock_bh(&br
->multicast_lock
);
4259 static void br_multicast_toggle_vlan(struct net_bridge_vlan
*vlan
, bool on
)
4261 struct net_bridge_port
*p
;
4263 if (WARN_ON_ONCE(!br_vlan_is_master(vlan
)))
4266 list_for_each_entry(p
, &vlan
->br
->port_list
, list
) {
4267 struct net_bridge_vlan
*vport
;
4269 vport
= br_vlan_find(nbp_vlan_group(p
), vlan
->vid
);
4272 br_multicast_toggle_one_vlan(vport
, on
);
4275 if (br_vlan_is_brentry(vlan
))
4276 br_multicast_toggle_one_vlan(vlan
, on
);
4279 int br_multicast_toggle_vlan_snooping(struct net_bridge
*br
, bool on
,
4280 struct netlink_ext_ack
*extack
)
4282 struct net_bridge_vlan_group
*vg
;
4283 struct net_bridge_vlan
*vlan
;
4284 struct net_bridge_port
*p
;
4286 if (br_opt_get(br
, BROPT_MCAST_VLAN_SNOOPING_ENABLED
) == on
)
4289 if (on
&& !br_opt_get(br
, BROPT_VLAN_ENABLED
)) {
4290 NL_SET_ERR_MSG_MOD(extack
, "Cannot enable multicast vlan snooping with vlan filtering disabled");
4294 vg
= br_vlan_group(br
);
4298 br_opt_toggle(br
, BROPT_MCAST_VLAN_SNOOPING_ENABLED
, on
);
4300 /* disable/enable non-vlan mcast contexts based on vlan snooping */
4302 __br_multicast_stop(&br
->multicast_ctx
);
4304 __br_multicast_open(&br
->multicast_ctx
);
4305 list_for_each_entry(p
, &br
->port_list
, list
) {
4307 br_multicast_disable_port(p
);
4309 br_multicast_enable_port(p
);
4312 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
)
4313 br_multicast_toggle_vlan(vlan
, on
);
4318 bool br_multicast_toggle_global_vlan(struct net_bridge_vlan
*vlan
, bool on
)
4322 /* BR_VLFLAG_GLOBAL_MCAST_ENABLED relies on eventual consistency and
4323 * requires only RTNL to change
4325 if (on
== !!(vlan
->priv_flags
& BR_VLFLAG_GLOBAL_MCAST_ENABLED
))
4328 vlan
->priv_flags
^= BR_VLFLAG_GLOBAL_MCAST_ENABLED
;
4329 br_multicast_toggle_vlan(vlan
, on
);
4334 void br_multicast_stop(struct net_bridge
*br
)
4338 if (br_opt_get(br
, BROPT_MCAST_VLAN_SNOOPING_ENABLED
)) {
4339 struct net_bridge_vlan_group
*vg
;
4340 struct net_bridge_vlan
*vlan
;
4342 vg
= br_vlan_group(br
);
4344 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
) {
4345 struct net_bridge_mcast
*brmctx
;
4347 brmctx
= &vlan
->br_mcast_ctx
;
4348 if (br_vlan_is_brentry(vlan
) &&
4349 !br_multicast_ctx_vlan_disabled(brmctx
))
4350 __br_multicast_stop(&vlan
->br_mcast_ctx
);
4354 __br_multicast_stop(&br
->multicast_ctx
);
4358 void br_multicast_dev_del(struct net_bridge
*br
)
4360 struct net_bridge_mdb_entry
*mp
;
4361 HLIST_HEAD(deleted_head
);
4362 struct hlist_node
*tmp
;
4364 spin_lock_bh(&br
->multicast_lock
);
4365 hlist_for_each_entry_safe(mp
, tmp
, &br
->mdb_list
, mdb_node
)
4366 br_multicast_del_mdb_entry(mp
);
4367 hlist_move_list(&br
->mcast_gc_list
, &deleted_head
);
4368 spin_unlock_bh(&br
->multicast_lock
);
4370 br_multicast_ctx_deinit(&br
->multicast_ctx
);
4371 br_multicast_gc(&deleted_head
);
4372 cancel_work_sync(&br
->mcast_gc_work
);
4377 int br_multicast_set_router(struct net_bridge_mcast
*brmctx
, unsigned long val
)
4381 spin_lock_bh(&brmctx
->br
->multicast_lock
);
4384 case MDB_RTR_TYPE_DISABLED
:
4385 case MDB_RTR_TYPE_PERM
:
4386 br_mc_router_state_change(brmctx
->br
, val
== MDB_RTR_TYPE_PERM
);
4387 del_timer(&brmctx
->ip4_mc_router_timer
);
4388 #if IS_ENABLED(CONFIG_IPV6)
4389 del_timer(&brmctx
->ip6_mc_router_timer
);
4391 brmctx
->multicast_router
= val
;
4394 case MDB_RTR_TYPE_TEMP_QUERY
:
4395 if (brmctx
->multicast_router
!= MDB_RTR_TYPE_TEMP_QUERY
)
4396 br_mc_router_state_change(brmctx
->br
, false);
4397 brmctx
->multicast_router
= val
;
4402 spin_unlock_bh(&brmctx
->br
->multicast_lock
);
4408 br_multicast_rport_del_notify(struct net_bridge_mcast_port
*pmctx
, bool deleted
)
4413 /* For backwards compatibility for now, only notify if there is
4414 * no multicast router anymore for both IPv4 and IPv6.
4416 if (!hlist_unhashed(&pmctx
->ip4_rlist
))
4418 #if IS_ENABLED(CONFIG_IPV6)
4419 if (!hlist_unhashed(&pmctx
->ip6_rlist
))
4423 br_rtr_notify(pmctx
->port
->br
->dev
, pmctx
, RTM_DELMDB
);
4424 br_port_mc_router_state_change(pmctx
->port
, false);
4426 /* don't allow timer refresh */
4427 if (pmctx
->multicast_router
== MDB_RTR_TYPE_TEMP
)
4428 pmctx
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
4431 int br_multicast_set_port_router(struct net_bridge_mcast_port
*pmctx
,
4434 struct net_bridge_mcast
*brmctx
;
4435 unsigned long now
= jiffies
;
4439 brmctx
= br_multicast_port_ctx_get_global(pmctx
);
4440 spin_lock_bh(&brmctx
->br
->multicast_lock
);
4441 if (pmctx
->multicast_router
== val
) {
4442 /* Refresh the temp router port timer */
4443 if (pmctx
->multicast_router
== MDB_RTR_TYPE_TEMP
) {
4444 mod_timer(&pmctx
->ip4_mc_router_timer
,
4445 now
+ brmctx
->multicast_querier_interval
);
4446 #if IS_ENABLED(CONFIG_IPV6)
4447 mod_timer(&pmctx
->ip6_mc_router_timer
,
4448 now
+ brmctx
->multicast_querier_interval
);
4455 case MDB_RTR_TYPE_DISABLED
:
4456 pmctx
->multicast_router
= MDB_RTR_TYPE_DISABLED
;
4457 del
|= br_ip4_multicast_rport_del(pmctx
);
4458 del_timer(&pmctx
->ip4_mc_router_timer
);
4459 del
|= br_ip6_multicast_rport_del(pmctx
);
4460 #if IS_ENABLED(CONFIG_IPV6)
4461 del_timer(&pmctx
->ip6_mc_router_timer
);
4463 br_multicast_rport_del_notify(pmctx
, del
);
4465 case MDB_RTR_TYPE_TEMP_QUERY
:
4466 pmctx
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
4467 del
|= br_ip4_multicast_rport_del(pmctx
);
4468 del
|= br_ip6_multicast_rport_del(pmctx
);
4469 br_multicast_rport_del_notify(pmctx
, del
);
4471 case MDB_RTR_TYPE_PERM
:
4472 pmctx
->multicast_router
= MDB_RTR_TYPE_PERM
;
4473 del_timer(&pmctx
->ip4_mc_router_timer
);
4474 br_ip4_multicast_add_router(brmctx
, pmctx
);
4475 #if IS_ENABLED(CONFIG_IPV6)
4476 del_timer(&pmctx
->ip6_mc_router_timer
);
4478 br_ip6_multicast_add_router(brmctx
, pmctx
);
4480 case MDB_RTR_TYPE_TEMP
:
4481 pmctx
->multicast_router
= MDB_RTR_TYPE_TEMP
;
4482 br_ip4_multicast_mark_router(brmctx
, pmctx
);
4483 br_ip6_multicast_mark_router(brmctx
, pmctx
);
4490 spin_unlock_bh(&brmctx
->br
->multicast_lock
);
4495 int br_multicast_set_vlan_router(struct net_bridge_vlan
*v
, u8 mcast_router
)
4499 if (br_vlan_is_master(v
))
4500 err
= br_multicast_set_router(&v
->br_mcast_ctx
, mcast_router
);
4502 err
= br_multicast_set_port_router(&v
->port_mcast_ctx
,
4508 static void br_multicast_start_querier(struct net_bridge_mcast
*brmctx
,
4509 struct bridge_mcast_own_query
*query
)
4511 struct net_bridge_port
*port
;
4513 if (!br_multicast_ctx_matches_vlan_snooping(brmctx
))
4516 __br_multicast_open_query(brmctx
->br
, query
);
4519 list_for_each_entry_rcu(port
, &brmctx
->br
->port_list
, list
) {
4520 struct bridge_mcast_own_query
*ip4_own_query
;
4521 #if IS_ENABLED(CONFIG_IPV6)
4522 struct bridge_mcast_own_query
*ip6_own_query
;
4525 if (br_multicast_port_ctx_state_stopped(&port
->multicast_ctx
))
4528 if (br_multicast_ctx_is_vlan(brmctx
)) {
4529 struct net_bridge_vlan
*vlan
;
4531 vlan
= br_vlan_find(nbp_vlan_group_rcu(port
),
4534 br_multicast_port_ctx_state_stopped(&vlan
->port_mcast_ctx
))
4537 ip4_own_query
= &vlan
->port_mcast_ctx
.ip4_own_query
;
4538 #if IS_ENABLED(CONFIG_IPV6)
4539 ip6_own_query
= &vlan
->port_mcast_ctx
.ip6_own_query
;
4542 ip4_own_query
= &port
->multicast_ctx
.ip4_own_query
;
4543 #if IS_ENABLED(CONFIG_IPV6)
4544 ip6_own_query
= &port
->multicast_ctx
.ip6_own_query
;
4548 if (query
== &brmctx
->ip4_own_query
)
4549 br_multicast_enable(ip4_own_query
);
4550 #if IS_ENABLED(CONFIG_IPV6)
4552 br_multicast_enable(ip6_own_query
);
4558 int br_multicast_toggle(struct net_bridge
*br
, unsigned long val
,
4559 struct netlink_ext_ack
*extack
)
4561 struct net_bridge_port
*port
;
4562 bool change_snoopers
= false;
4565 spin_lock_bh(&br
->multicast_lock
);
4566 if (!!br_opt_get(br
, BROPT_MULTICAST_ENABLED
) == !!val
)
4569 err
= br_mc_disabled_update(br
->dev
, val
, extack
);
4570 if (err
== -EOPNOTSUPP
)
4575 br_opt_toggle(br
, BROPT_MULTICAST_ENABLED
, !!val
);
4576 if (!br_opt_get(br
, BROPT_MULTICAST_ENABLED
)) {
4577 change_snoopers
= true;
4581 if (!netif_running(br
->dev
))
4584 br_multicast_open(br
);
4585 list_for_each_entry(port
, &br
->port_list
, list
)
4586 __br_multicast_enable_port_ctx(&port
->multicast_ctx
);
4588 change_snoopers
= true;
4591 spin_unlock_bh(&br
->multicast_lock
);
4593 /* br_multicast_join_snoopers has the potential to cause
4594 * an MLD Report/Leave to be delivered to br_multicast_rcv,
4595 * which would in turn call br_multicast_add_group, which would
4596 * attempt to acquire multicast_lock. This function should be
4597 * called after the lock has been released to avoid deadlocks on
4600 * br_multicast_leave_snoopers does not have the problem since
4601 * br_multicast_rcv first checks BROPT_MULTICAST_ENABLED, and
4602 * returns without calling br_multicast_ipv4/6_rcv if it's not
4603 * enabled. Moved both functions out just for symmetry.
4605 if (change_snoopers
) {
4606 if (br_opt_get(br
, BROPT_MULTICAST_ENABLED
))
4607 br_multicast_join_snoopers(br
);
4609 br_multicast_leave_snoopers(br
);
4615 bool br_multicast_enabled(const struct net_device
*dev
)
4617 struct net_bridge
*br
= netdev_priv(dev
);
4619 return !!br_opt_get(br
, BROPT_MULTICAST_ENABLED
);
4621 EXPORT_SYMBOL_GPL(br_multicast_enabled
);
4623 bool br_multicast_router(const struct net_device
*dev
)
4625 struct net_bridge
*br
= netdev_priv(dev
);
4628 spin_lock_bh(&br
->multicast_lock
);
4629 is_router
= br_multicast_is_router(&br
->multicast_ctx
, NULL
);
4630 spin_unlock_bh(&br
->multicast_lock
);
4633 EXPORT_SYMBOL_GPL(br_multicast_router
);
4635 int br_multicast_set_querier(struct net_bridge_mcast
*brmctx
, unsigned long val
)
4637 unsigned long max_delay
;
4641 spin_lock_bh(&brmctx
->br
->multicast_lock
);
4642 if (brmctx
->multicast_querier
== val
)
4645 WRITE_ONCE(brmctx
->multicast_querier
, val
);
4649 max_delay
= brmctx
->multicast_query_response_interval
;
4651 if (!timer_pending(&brmctx
->ip4_other_query
.timer
))
4652 mod_timer(&brmctx
->ip4_other_query
.delay_timer
,
4653 jiffies
+ max_delay
);
4655 br_multicast_start_querier(brmctx
, &brmctx
->ip4_own_query
);
4657 #if IS_ENABLED(CONFIG_IPV6)
4658 if (!timer_pending(&brmctx
->ip6_other_query
.timer
))
4659 mod_timer(&brmctx
->ip6_other_query
.delay_timer
,
4660 jiffies
+ max_delay
);
4662 br_multicast_start_querier(brmctx
, &brmctx
->ip6_own_query
);
4666 spin_unlock_bh(&brmctx
->br
->multicast_lock
);
4671 int br_multicast_set_igmp_version(struct net_bridge_mcast
*brmctx
,
4674 /* Currently we support only version 2 and 3 */
4683 spin_lock_bh(&brmctx
->br
->multicast_lock
);
4684 brmctx
->multicast_igmp_version
= val
;
4685 spin_unlock_bh(&brmctx
->br
->multicast_lock
);
4690 #if IS_ENABLED(CONFIG_IPV6)
4691 int br_multicast_set_mld_version(struct net_bridge_mcast
*brmctx
,
4694 /* Currently we support version 1 and 2 */
4703 spin_lock_bh(&brmctx
->br
->multicast_lock
);
4704 brmctx
->multicast_mld_version
= val
;
4705 spin_unlock_bh(&brmctx
->br
->multicast_lock
);
4711 void br_multicast_set_query_intvl(struct net_bridge_mcast
*brmctx
,
4714 unsigned long intvl_jiffies
= clock_t_to_jiffies(val
);
4716 if (intvl_jiffies
< BR_MULTICAST_QUERY_INTVL_MIN
) {
4718 "trying to set multicast query interval below minimum, setting to %lu (%ums)\n",
4719 jiffies_to_clock_t(BR_MULTICAST_QUERY_INTVL_MIN
),
4720 jiffies_to_msecs(BR_MULTICAST_QUERY_INTVL_MIN
));
4721 intvl_jiffies
= BR_MULTICAST_QUERY_INTVL_MIN
;
4724 brmctx
->multicast_query_interval
= intvl_jiffies
;
4727 void br_multicast_set_startup_query_intvl(struct net_bridge_mcast
*brmctx
,
4730 unsigned long intvl_jiffies
= clock_t_to_jiffies(val
);
4732 if (intvl_jiffies
< BR_MULTICAST_STARTUP_QUERY_INTVL_MIN
) {
4734 "trying to set multicast startup query interval below minimum, setting to %lu (%ums)\n",
4735 jiffies_to_clock_t(BR_MULTICAST_STARTUP_QUERY_INTVL_MIN
),
4736 jiffies_to_msecs(BR_MULTICAST_STARTUP_QUERY_INTVL_MIN
));
4737 intvl_jiffies
= BR_MULTICAST_STARTUP_QUERY_INTVL_MIN
;
4740 brmctx
->multicast_startup_query_interval
= intvl_jiffies
;
4744 * br_multicast_list_adjacent - Returns snooped multicast addresses
4745 * @dev: The bridge port adjacent to which to retrieve addresses
4746 * @br_ip_list: The list to store found, snooped multicast IP addresses in
4748 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
4749 * snooping feature on all bridge ports of dev's bridge device, excluding
4750 * the addresses from dev itself.
4752 * Returns the number of items added to br_ip_list.
4755 * - br_ip_list needs to be initialized by caller
4756 * - br_ip_list might contain duplicates in the end
4757 * (needs to be taken care of by caller)
4758 * - br_ip_list needs to be freed by caller
4760 int br_multicast_list_adjacent(struct net_device
*dev
,
4761 struct list_head
*br_ip_list
)
4763 struct net_bridge
*br
;
4764 struct net_bridge_port
*port
;
4765 struct net_bridge_port_group
*group
;
4766 struct br_ip_list
*entry
;
4770 if (!br_ip_list
|| !netif_is_bridge_port(dev
))
4773 port
= br_port_get_rcu(dev
);
4774 if (!port
|| !port
->br
)
4779 list_for_each_entry_rcu(port
, &br
->port_list
, list
) {
4780 if (!port
->dev
|| port
->dev
== dev
)
4783 hlist_for_each_entry_rcu(group
, &port
->mglist
, mglist
) {
4784 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
4788 entry
->addr
= group
->key
.addr
;
4789 list_add(&entry
->list
, br_ip_list
);
4798 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent
);
4801 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
4802 * @dev: The bridge port providing the bridge on which to check for a querier
4803 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
4805 * Checks whether the given interface has a bridge on top and if so returns
4806 * true if a valid querier exists anywhere on the bridged link layer.
4807 * Otherwise returns false.
4809 bool br_multicast_has_querier_anywhere(struct net_device
*dev
, int proto
)
4811 struct net_bridge
*br
;
4812 struct net_bridge_port
*port
;
4817 if (!netif_is_bridge_port(dev
))
4820 port
= br_port_get_rcu(dev
);
4821 if (!port
|| !port
->br
)
4826 memset(ð
, 0, sizeof(eth
));
4827 eth
.h_proto
= htons(proto
);
4829 ret
= br_multicast_querier_exists(&br
->multicast_ctx
, ð
, NULL
);
4835 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere
);
4838 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
4839 * @dev: The bridge port adjacent to which to check for a querier
4840 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
4842 * Checks whether the given interface has a bridge on top and if so returns
4843 * true if a selected querier is behind one of the other ports of this
4844 * bridge. Otherwise returns false.
4846 bool br_multicast_has_querier_adjacent(struct net_device
*dev
, int proto
)
4848 struct net_bridge_mcast
*brmctx
;
4849 struct net_bridge
*br
;
4850 struct net_bridge_port
*port
;
4855 if (!netif_is_bridge_port(dev
))
4858 port
= br_port_get_rcu(dev
);
4859 if (!port
|| !port
->br
)
4863 brmctx
= &br
->multicast_ctx
;
4867 port_ifidx
= brmctx
->ip4_querier
.port_ifidx
;
4868 if (!timer_pending(&brmctx
->ip4_other_query
.timer
) ||
4869 port_ifidx
== port
->dev
->ifindex
)
4872 #if IS_ENABLED(CONFIG_IPV6)
4874 port_ifidx
= brmctx
->ip6_querier
.port_ifidx
;
4875 if (!timer_pending(&brmctx
->ip6_other_query
.timer
) ||
4876 port_ifidx
== port
->dev
->ifindex
)
4889 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent
);
4892 * br_multicast_has_router_adjacent - Checks for a router behind a bridge port
4893 * @dev: The bridge port adjacent to which to check for a multicast router
4894 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
4896 * Checks whether the given interface has a bridge on top and if so returns
4897 * true if a multicast router is behind one of the other ports of this
4898 * bridge. Otherwise returns false.
4900 bool br_multicast_has_router_adjacent(struct net_device
*dev
, int proto
)
4902 struct net_bridge_mcast_port
*pmctx
;
4903 struct net_bridge_mcast
*brmctx
;
4904 struct net_bridge_port
*port
;
4908 port
= br_port_get_check_rcu(dev
);
4912 brmctx
= &port
->br
->multicast_ctx
;
4915 hlist_for_each_entry_rcu(pmctx
, &brmctx
->ip4_mc_router_list
,
4917 if (pmctx
->port
== port
)
4924 #if IS_ENABLED(CONFIG_IPV6)
4926 hlist_for_each_entry_rcu(pmctx
, &brmctx
->ip6_mc_router_list
,
4928 if (pmctx
->port
== port
)
4937 /* when compiled without IPv6 support, be conservative and
4938 * always assume presence of an IPv6 multicast router
4947 EXPORT_SYMBOL_GPL(br_multicast_has_router_adjacent
);
4949 static void br_mcast_stats_add(struct bridge_mcast_stats __percpu
*stats
,
4950 const struct sk_buff
*skb
, u8 type
, u8 dir
)
4952 struct bridge_mcast_stats
*pstats
= this_cpu_ptr(stats
);
4953 __be16 proto
= skb
->protocol
;
4956 u64_stats_update_begin(&pstats
->syncp
);
4958 case htons(ETH_P_IP
):
4959 t_len
= ntohs(ip_hdr(skb
)->tot_len
) - ip_hdrlen(skb
);
4961 case IGMP_HOST_MEMBERSHIP_REPORT
:
4962 pstats
->mstats
.igmp_v1reports
[dir
]++;
4964 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
4965 pstats
->mstats
.igmp_v2reports
[dir
]++;
4967 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
4968 pstats
->mstats
.igmp_v3reports
[dir
]++;
4970 case IGMP_HOST_MEMBERSHIP_QUERY
:
4971 if (t_len
!= sizeof(struct igmphdr
)) {
4972 pstats
->mstats
.igmp_v3queries
[dir
]++;
4974 unsigned int offset
= skb_transport_offset(skb
);
4975 struct igmphdr
*ih
, _ihdr
;
4977 ih
= skb_header_pointer(skb
, offset
,
4978 sizeof(_ihdr
), &_ihdr
);
4982 pstats
->mstats
.igmp_v1queries
[dir
]++;
4984 pstats
->mstats
.igmp_v2queries
[dir
]++;
4987 case IGMP_HOST_LEAVE_MESSAGE
:
4988 pstats
->mstats
.igmp_leaves
[dir
]++;
4992 #if IS_ENABLED(CONFIG_IPV6)
4993 case htons(ETH_P_IPV6
):
4994 t_len
= ntohs(ipv6_hdr(skb
)->payload_len
) +
4995 sizeof(struct ipv6hdr
);
4996 t_len
-= skb_network_header_len(skb
);
4998 case ICMPV6_MGM_REPORT
:
4999 pstats
->mstats
.mld_v1reports
[dir
]++;
5001 case ICMPV6_MLD2_REPORT
:
5002 pstats
->mstats
.mld_v2reports
[dir
]++;
5004 case ICMPV6_MGM_QUERY
:
5005 if (t_len
!= sizeof(struct mld_msg
))
5006 pstats
->mstats
.mld_v2queries
[dir
]++;
5008 pstats
->mstats
.mld_v1queries
[dir
]++;
5010 case ICMPV6_MGM_REDUCTION
:
5011 pstats
->mstats
.mld_leaves
[dir
]++;
5015 #endif /* CONFIG_IPV6 */
5017 u64_stats_update_end(&pstats
->syncp
);
5020 void br_multicast_count(struct net_bridge
*br
,
5021 const struct net_bridge_port
*p
,
5022 const struct sk_buff
*skb
, u8 type
, u8 dir
)
5024 struct bridge_mcast_stats __percpu
*stats
;
5026 /* if multicast_disabled is true then igmp type can't be set */
5027 if (!type
|| !br_opt_get(br
, BROPT_MULTICAST_STATS_ENABLED
))
5031 stats
= p
->mcast_stats
;
5033 stats
= br
->mcast_stats
;
5034 if (WARN_ON(!stats
))
5037 br_mcast_stats_add(stats
, skb
, type
, dir
);
5040 int br_multicast_init_stats(struct net_bridge
*br
)
5042 br
->mcast_stats
= netdev_alloc_pcpu_stats(struct bridge_mcast_stats
);
5043 if (!br
->mcast_stats
)
5049 void br_multicast_uninit_stats(struct net_bridge
*br
)
5051 free_percpu(br
->mcast_stats
);
5054 /* noinline for https://llvm.org/pr45802#c9 */
5055 static noinline_for_stack
void mcast_stats_add_dir(u64
*dst
, u64
*src
)
5057 dst
[BR_MCAST_DIR_RX
] += src
[BR_MCAST_DIR_RX
];
5058 dst
[BR_MCAST_DIR_TX
] += src
[BR_MCAST_DIR_TX
];
5061 void br_multicast_get_stats(const struct net_bridge
*br
,
5062 const struct net_bridge_port
*p
,
5063 struct br_mcast_stats
*dest
)
5065 struct bridge_mcast_stats __percpu
*stats
;
5066 struct br_mcast_stats tdst
;
5069 memset(dest
, 0, sizeof(*dest
));
5071 stats
= p
->mcast_stats
;
5073 stats
= br
->mcast_stats
;
5074 if (WARN_ON(!stats
))
5077 memset(&tdst
, 0, sizeof(tdst
));
5078 for_each_possible_cpu(i
) {
5079 struct bridge_mcast_stats
*cpu_stats
= per_cpu_ptr(stats
, i
);
5080 struct br_mcast_stats temp
;
5084 start
= u64_stats_fetch_begin(&cpu_stats
->syncp
);
5085 memcpy(&temp
, &cpu_stats
->mstats
, sizeof(temp
));
5086 } while (u64_stats_fetch_retry(&cpu_stats
->syncp
, start
));
5088 mcast_stats_add_dir(tdst
.igmp_v1queries
, temp
.igmp_v1queries
);
5089 mcast_stats_add_dir(tdst
.igmp_v2queries
, temp
.igmp_v2queries
);
5090 mcast_stats_add_dir(tdst
.igmp_v3queries
, temp
.igmp_v3queries
);
5091 mcast_stats_add_dir(tdst
.igmp_leaves
, temp
.igmp_leaves
);
5092 mcast_stats_add_dir(tdst
.igmp_v1reports
, temp
.igmp_v1reports
);
5093 mcast_stats_add_dir(tdst
.igmp_v2reports
, temp
.igmp_v2reports
);
5094 mcast_stats_add_dir(tdst
.igmp_v3reports
, temp
.igmp_v3reports
);
5095 tdst
.igmp_parse_errors
+= temp
.igmp_parse_errors
;
5097 mcast_stats_add_dir(tdst
.mld_v1queries
, temp
.mld_v1queries
);
5098 mcast_stats_add_dir(tdst
.mld_v2queries
, temp
.mld_v2queries
);
5099 mcast_stats_add_dir(tdst
.mld_leaves
, temp
.mld_leaves
);
5100 mcast_stats_add_dir(tdst
.mld_v1reports
, temp
.mld_v1reports
);
5101 mcast_stats_add_dir(tdst
.mld_v2reports
, temp
.mld_v2reports
);
5102 tdst
.mld_parse_errors
+= temp
.mld_parse_errors
;
5104 memcpy(dest
, &tdst
, sizeof(*dest
));
5107 int br_mdb_hash_init(struct net_bridge
*br
)
5111 err
= rhashtable_init(&br
->sg_port_tbl
, &br_sg_port_rht_params
);
5115 err
= rhashtable_init(&br
->mdb_hash_tbl
, &br_mdb_rht_params
);
5117 rhashtable_destroy(&br
->sg_port_tbl
);
5124 void br_mdb_hash_fini(struct net_bridge
*br
)
5126 rhashtable_destroy(&br
->sg_port_tbl
);
5127 rhashtable_destroy(&br
->mdb_hash_tbl
);