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 *br_mdb_get(struct net_bridge_mcast *brmctx,
149 struct sk_buff *skb, u16 vid)
151 struct net_bridge *br = brmctx->br;
154 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
155 br_multicast_ctx_vlan_global_disabled(brmctx))
158 if (BR_INPUT_SKB_CB(skb)->igmp)
161 memset(&ip, 0, sizeof(ip));
162 ip.proto = skb->protocol;
165 switch (skb->protocol) {
166 case htons(ETH_P_IP):
167 ip.dst.ip4 = ip_hdr(skb)->daddr;
168 if (brmctx->multicast_igmp_version == 3) {
169 struct net_bridge_mdb_entry *mdb;
171 ip.src.ip4 = ip_hdr(skb)->saddr;
172 mdb = br_mdb_ip_get_rcu(br, &ip);
178 #if IS_ENABLED(CONFIG_IPV6)
179 case htons(ETH_P_IPV6):
180 ip.dst.ip6 = ipv6_hdr(skb)->daddr;
181 if (brmctx->multicast_mld_version == 2) {
182 struct net_bridge_mdb_entry *mdb;
184 ip.src.ip6 = ipv6_hdr(skb)->saddr;
185 mdb = br_mdb_ip_get_rcu(br, &ip);
188 memset(&ip.src.ip6, 0, sizeof(ip.src.ip6));
194 ether_addr_copy(ip.dst.mac_addr, eth_hdr(skb)->h_dest);
197 return br_mdb_ip_get_rcu(br, &ip);
200 /* IMPORTANT: this function must be used only when the contexts cannot be
201 * passed down (e.g. timer) and must be used for read-only purposes because
202 * the vlan snooping option can change, so it can return any context
203 * (non-vlan or vlan). Its initial intended purpose is to read timer values
204 * from the *current* context based on the option. At worst that could lead
205 * to inconsistent timers when the contexts are changed, i.e. src timer
206 * which needs to re-arm with a specific delay taken from the old context
208 static struct net_bridge_mcast_port *
209 br_multicast_pg_to_port_ctx(const struct net_bridge_port_group *pg)
211 struct net_bridge_mcast_port *pmctx = &pg->key.port->multicast_ctx;
212 struct net_bridge_vlan *vlan;
214 lockdep_assert_held_once(&pg->key.port->br->multicast_lock);
216 /* if vlan snooping is disabled use the port's multicast context */
217 if (!pg->key.addr.vid ||
218 !br_opt_get(pg->key.port->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED))
221 /* locking is tricky here, due to different rules for multicast and
222 * vlans we need to take rcu to find the vlan and make sure it has
223 * the BR_VLFLAG_MCAST_ENABLED flag set, it can only change under
224 * multicast_lock which must be already held here, so the vlan's pmctx
225 * can safely be used on return
228 vlan = br_vlan_find(nbp_vlan_group_rcu(pg->key.port), pg->key.addr.vid);
229 if (vlan && !br_multicast_port_ctx_vlan_disabled(&vlan->port_mcast_ctx))
230 pmctx = &vlan->port_mcast_ctx;
238 static struct net_bridge_mcast_port *
239 br_multicast_port_vid_to_port_ctx(struct net_bridge_port *port, u16 vid)
241 struct net_bridge_mcast_port *pmctx = NULL;
242 struct net_bridge_vlan *vlan;
244 lockdep_assert_held_once(&port->br->multicast_lock);
246 if (!br_opt_get(port->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED))
249 /* Take RCU to access the vlan. */
252 vlan = br_vlan_find(nbp_vlan_group_rcu(port), vid);
253 if (vlan && !br_multicast_port_ctx_vlan_disabled(&vlan->port_mcast_ctx))
254 pmctx = &vlan->port_mcast_ctx;
261 /* when snooping we need to check if the contexts should be used
262 * in the following order:
263 * - if pmctx is non-NULL (port), check if it should be used
264 * - if pmctx is NULL (bridge), check if brmctx should be used
267 br_multicast_ctx_should_use(const struct net_bridge_mcast *brmctx,
268 const struct net_bridge_mcast_port *pmctx)
270 if (!netif_running(brmctx->br->dev))
274 return !br_multicast_port_ctx_state_disabled(pmctx);
276 return !br_multicast_ctx_vlan_disabled(brmctx);
279 static bool br_port_group_equal(struct net_bridge_port_group *p,
280 struct net_bridge_port *port,
281 const unsigned char *src)
283 if (p->key.port != port)
286 if (!(port->flags & BR_MULTICAST_TO_UNICAST))
289 return ether_addr_equal(src, p->eth_addr);
292 static void __fwd_add_star_excl(struct net_bridge_mcast_port *pmctx,
293 struct net_bridge_port_group *pg,
296 struct net_bridge_port_group_sg_key sg_key;
297 struct net_bridge_port_group *src_pg;
298 struct net_bridge_mcast *brmctx;
300 memset(&sg_key, 0, sizeof(sg_key));
301 brmctx = br_multicast_port_ctx_get_global(pmctx);
302 sg_key.port = pg->key.port;
303 sg_key.addr = *sg_ip;
304 if (br_sg_port_find(brmctx->br, &sg_key))
307 src_pg = __br_multicast_add_group(brmctx, pmctx,
309 MCAST_INCLUDE, false, false);
310 if (IS_ERR_OR_NULL(src_pg) ||
311 src_pg->rt_protocol != RTPROT_KERNEL)
314 src_pg->flags |= MDB_PG_FLAGS_STAR_EXCL;
317 static void __fwd_del_star_excl(struct net_bridge_port_group *pg,
320 struct net_bridge_port_group_sg_key sg_key;
321 struct net_bridge *br = pg->key.port->br;
322 struct net_bridge_port_group *src_pg;
324 memset(&sg_key, 0, sizeof(sg_key));
325 sg_key.port = pg->key.port;
326 sg_key.addr = *sg_ip;
327 src_pg = br_sg_port_find(br, &sg_key);
328 if (!src_pg || !(src_pg->flags & MDB_PG_FLAGS_STAR_EXCL) ||
329 src_pg->rt_protocol != RTPROT_KERNEL)
332 br_multicast_find_del_pg(br, src_pg);
335 /* When a port group transitions to (or is added as) EXCLUDE we need to add it
336 * to all other ports' S,G entries which are not blocked by the current group
337 * for proper replication, the assumption is that any S,G blocked entries
338 * are already added so the S,G,port lookup should skip them.
339 * When a port group transitions from EXCLUDE -> INCLUDE mode or is being
340 * deleted we need to remove it from all ports' S,G entries where it was
341 * automatically installed before (i.e. where it's MDB_PG_FLAGS_STAR_EXCL).
343 void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg,
346 struct net_bridge *br = pg->key.port->br;
347 struct net_bridge_port_group *pg_lst;
348 struct net_bridge_mcast_port *pmctx;
349 struct net_bridge_mdb_entry *mp;
352 if (WARN_ON(!br_multicast_is_star_g(&pg->key.addr)))
355 mp = br_mdb_ip_get(br, &pg->key.addr);
358 pmctx = br_multicast_pg_to_port_ctx(pg);
362 memset(&sg_ip, 0, sizeof(sg_ip));
363 sg_ip = pg->key.addr;
365 for (pg_lst = mlock_dereference(mp->ports, br);
367 pg_lst = mlock_dereference(pg_lst->next, br)) {
368 struct net_bridge_group_src *src_ent;
372 hlist_for_each_entry(src_ent, &pg_lst->src_list, node) {
373 if (!(src_ent->flags & BR_SGRP_F_INSTALLED))
375 sg_ip.src = src_ent->addr.src;
376 switch (filter_mode) {
378 __fwd_del_star_excl(pg, &sg_ip);
381 __fwd_add_star_excl(pmctx, pg, &sg_ip);
388 /* called when adding a new S,G with host_joined == false by default */
389 static void br_multicast_sg_host_state(struct net_bridge_mdb_entry *star_mp,
390 struct net_bridge_port_group *sg)
392 struct net_bridge_mdb_entry *sg_mp;
394 if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr)))
396 if (!star_mp->host_joined)
399 sg_mp = br_mdb_ip_get(star_mp->br, &sg->key.addr);
402 sg_mp->host_joined = true;
405 /* set the host_joined state of all of *,G's S,G entries */
406 static void br_multicast_star_g_host_state(struct net_bridge_mdb_entry *star_mp)
408 struct net_bridge *br = star_mp->br;
409 struct net_bridge_mdb_entry *sg_mp;
410 struct net_bridge_port_group *pg;
413 if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr)))
416 memset(&sg_ip, 0, sizeof(sg_ip));
417 sg_ip = star_mp->addr;
418 for (pg = mlock_dereference(star_mp->ports, br);
420 pg = mlock_dereference(pg->next, br)) {
421 struct net_bridge_group_src *src_ent;
423 hlist_for_each_entry(src_ent, &pg->src_list, node) {
424 if (!(src_ent->flags & BR_SGRP_F_INSTALLED))
426 sg_ip.src = src_ent->addr.src;
427 sg_mp = br_mdb_ip_get(br, &sg_ip);
430 sg_mp->host_joined = star_mp->host_joined;
435 static void br_multicast_sg_del_exclude_ports(struct net_bridge_mdb_entry *sgmp)
437 struct net_bridge_port_group __rcu **pp;
438 struct net_bridge_port_group *p;
440 /* *,G exclude ports are only added to S,G entries */
441 if (WARN_ON(br_multicast_is_star_g(&sgmp->addr)))
444 /* we need the STAR_EXCLUDE ports if there are non-STAR_EXCLUDE ports
445 * we should ignore perm entries since they're managed by user-space
447 for (pp = &sgmp->ports;
448 (p = mlock_dereference(*pp, sgmp->br)) != NULL;
450 if (!(p->flags & (MDB_PG_FLAGS_STAR_EXCL |
451 MDB_PG_FLAGS_PERMANENT)))
454 /* currently the host can only have joined the *,G which means
455 * we treat it as EXCLUDE {}, so for an S,G it's considered a
456 * STAR_EXCLUDE entry and we can safely leave it
458 sgmp->host_joined = false;
460 for (pp = &sgmp->ports;
461 (p = mlock_dereference(*pp, sgmp->br)) != NULL;) {
462 if (!(p->flags & MDB_PG_FLAGS_PERMANENT))
463 br_multicast_del_pg(sgmp, p, pp);
469 void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp,
470 struct net_bridge_port_group *sg)
472 struct net_bridge_port_group_sg_key sg_key;
473 struct net_bridge *br = star_mp->br;
474 struct net_bridge_mcast_port *pmctx;
475 struct net_bridge_port_group *pg;
476 struct net_bridge_mcast *brmctx;
478 if (WARN_ON(br_multicast_is_star_g(&sg->key.addr)))
480 if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr)))
483 br_multicast_sg_host_state(star_mp, sg);
484 memset(&sg_key, 0, sizeof(sg_key));
485 sg_key.addr = sg->key.addr;
486 /* we need to add all exclude ports to the S,G */
487 for (pg = mlock_dereference(star_mp->ports, br);
489 pg = mlock_dereference(pg->next, br)) {
490 struct net_bridge_port_group *src_pg;
492 if (pg == sg || pg->filter_mode == MCAST_INCLUDE)
495 sg_key.port = pg->key.port;
496 if (br_sg_port_find(br, &sg_key))
499 pmctx = br_multicast_pg_to_port_ctx(pg);
502 brmctx = br_multicast_port_ctx_get_global(pmctx);
504 src_pg = __br_multicast_add_group(brmctx, pmctx,
507 MCAST_INCLUDE, false, false);
508 if (IS_ERR_OR_NULL(src_pg) ||
509 src_pg->rt_protocol != RTPROT_KERNEL)
511 src_pg->flags |= MDB_PG_FLAGS_STAR_EXCL;
515 static void br_multicast_fwd_src_add(struct net_bridge_group_src *src)
517 struct net_bridge_mdb_entry *star_mp;
518 struct net_bridge_mcast_port *pmctx;
519 struct net_bridge_port_group *sg;
520 struct net_bridge_mcast *brmctx;
523 if (src->flags & BR_SGRP_F_INSTALLED)
526 memset(&sg_ip, 0, sizeof(sg_ip));
527 pmctx = br_multicast_pg_to_port_ctx(src->pg);
530 brmctx = br_multicast_port_ctx_get_global(pmctx);
531 sg_ip = src->pg->key.addr;
532 sg_ip.src = src->addr.src;
534 sg = __br_multicast_add_group(brmctx, pmctx, &sg_ip,
535 src->pg->eth_addr, MCAST_INCLUDE, false,
536 !timer_pending(&src->timer));
537 if (IS_ERR_OR_NULL(sg))
539 src->flags |= BR_SGRP_F_INSTALLED;
540 sg->flags &= ~MDB_PG_FLAGS_STAR_EXCL;
542 /* if it was added by user-space as perm we can skip next steps */
543 if (sg->rt_protocol != RTPROT_KERNEL &&
544 (sg->flags & MDB_PG_FLAGS_PERMANENT))
547 /* the kernel is now responsible for removing this S,G */
548 del_timer(&sg->timer);
549 star_mp = br_mdb_ip_get(src->br, &src->pg->key.addr);
553 br_multicast_sg_add_exclude_ports(star_mp, sg);
556 static void br_multicast_fwd_src_remove(struct net_bridge_group_src *src,
559 struct net_bridge_port_group *p, *pg = src->pg;
560 struct net_bridge_port_group __rcu **pp;
561 struct net_bridge_mdb_entry *mp;
564 memset(&sg_ip, 0, sizeof(sg_ip));
565 sg_ip = pg->key.addr;
566 sg_ip.src = src->addr.src;
568 mp = br_mdb_ip_get(src->br, &sg_ip);
572 for (pp = &mp->ports;
573 (p = mlock_dereference(*pp, src->br)) != NULL;
575 if (!br_port_group_equal(p, pg->key.port, pg->eth_addr))
578 if (p->rt_protocol != RTPROT_KERNEL &&
579 (p->flags & MDB_PG_FLAGS_PERMANENT) &&
580 !(src->flags & BR_SGRP_F_USER_ADDED))
584 p->flags |= MDB_PG_FLAGS_FAST_LEAVE;
585 br_multicast_del_pg(mp, p, pp);
588 src->flags &= ~BR_SGRP_F_INSTALLED;
591 /* install S,G and based on src's timer enable or disable forwarding */
592 static void br_multicast_fwd_src_handle(struct net_bridge_group_src *src)
594 struct net_bridge_port_group_sg_key sg_key;
595 struct net_bridge_port_group *sg;
598 br_multicast_fwd_src_add(src);
600 memset(&sg_key, 0, sizeof(sg_key));
601 sg_key.addr = src->pg->key.addr;
602 sg_key.addr.src = src->addr.src;
603 sg_key.port = src->pg->key.port;
605 sg = br_sg_port_find(src->br, &sg_key);
606 if (!sg || (sg->flags & MDB_PG_FLAGS_PERMANENT))
609 old_flags = sg->flags;
610 if (timer_pending(&src->timer))
611 sg->flags &= ~MDB_PG_FLAGS_BLOCKED;
613 sg->flags |= MDB_PG_FLAGS_BLOCKED;
615 if (old_flags != sg->flags) {
616 struct net_bridge_mdb_entry *sg_mp;
618 sg_mp = br_mdb_ip_get(src->br, &sg_key.addr);
621 br_mdb_notify(src->br->dev, sg_mp, sg, RTM_NEWMDB);
625 static void br_multicast_destroy_mdb_entry(struct net_bridge_mcast_gc *gc)
627 struct net_bridge_mdb_entry *mp;
629 mp = container_of(gc, struct net_bridge_mdb_entry, mcast_gc);
630 WARN_ON(!hlist_unhashed(&mp->mdb_node));
633 timer_shutdown_sync(&mp->timer);
637 static void br_multicast_del_mdb_entry(struct net_bridge_mdb_entry *mp)
639 struct net_bridge *br = mp->br;
641 rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
643 hlist_del_init_rcu(&mp->mdb_node);
644 hlist_add_head(&mp->mcast_gc.gc_node, &br->mcast_gc_list);
645 queue_work(system_long_wq, &br->mcast_gc_work);
648 static void br_multicast_group_expired(struct timer_list *t)
650 struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer);
651 struct net_bridge *br = mp->br;
653 spin_lock(&br->multicast_lock);
654 if (hlist_unhashed(&mp->mdb_node) || !netif_running(br->dev) ||
655 timer_pending(&mp->timer))
658 br_multicast_host_leave(mp, true);
662 br_multicast_del_mdb_entry(mp);
664 spin_unlock(&br->multicast_lock);
667 static void br_multicast_destroy_group_src(struct net_bridge_mcast_gc *gc)
669 struct net_bridge_group_src *src;
671 src = container_of(gc, struct net_bridge_group_src, mcast_gc);
672 WARN_ON(!hlist_unhashed(&src->node));
674 timer_shutdown_sync(&src->timer);
678 void __br_multicast_del_group_src(struct net_bridge_group_src *src)
680 struct net_bridge *br = src->pg->key.port->br;
682 hlist_del_init_rcu(&src->node);
684 hlist_add_head(&src->mcast_gc.gc_node, &br->mcast_gc_list);
685 queue_work(system_long_wq, &br->mcast_gc_work);
688 void br_multicast_del_group_src(struct net_bridge_group_src *src,
691 br_multicast_fwd_src_remove(src, fastleave);
692 __br_multicast_del_group_src(src);
696 br_multicast_port_ngroups_inc_one(struct net_bridge_mcast_port *pmctx,
697 struct netlink_ext_ack *extack,
700 u32 max = READ_ONCE(pmctx->mdb_max_entries);
701 u32 n = READ_ONCE(pmctx->mdb_n_entries);
703 if (max && n >= max) {
704 NL_SET_ERR_MSG_FMT_MOD(extack, "%s is already in %u groups, and mcast_max_groups=%u",
709 WRITE_ONCE(pmctx->mdb_n_entries, n + 1);
713 static void br_multicast_port_ngroups_dec_one(struct net_bridge_mcast_port *pmctx)
715 u32 n = READ_ONCE(pmctx->mdb_n_entries);
717 WARN_ON_ONCE(n == 0);
718 WRITE_ONCE(pmctx->mdb_n_entries, n - 1);
721 static int br_multicast_port_ngroups_inc(struct net_bridge_port *port,
722 const struct br_ip *group,
723 struct netlink_ext_ack *extack)
725 struct net_bridge_mcast_port *pmctx;
728 lockdep_assert_held_once(&port->br->multicast_lock);
730 /* Always count on the port context. */
731 err = br_multicast_port_ngroups_inc_one(&port->multicast_ctx, extack,
734 trace_br_mdb_full(port->dev, group);
738 /* Only count on the VLAN context if VID is given, and if snooping on
739 * that VLAN is enabled.
744 pmctx = br_multicast_port_vid_to_port_ctx(port, group->vid);
748 err = br_multicast_port_ngroups_inc_one(pmctx, extack, "Port-VLAN");
750 trace_br_mdb_full(port->dev, group);
757 br_multicast_port_ngroups_dec_one(&port->multicast_ctx);
761 static void br_multicast_port_ngroups_dec(struct net_bridge_port *port, u16 vid)
763 struct net_bridge_mcast_port *pmctx;
765 lockdep_assert_held_once(&port->br->multicast_lock);
768 pmctx = br_multicast_port_vid_to_port_ctx(port, vid);
770 br_multicast_port_ngroups_dec_one(pmctx);
772 br_multicast_port_ngroups_dec_one(&port->multicast_ctx);
775 u32 br_multicast_ngroups_get(const struct net_bridge_mcast_port *pmctx)
777 return READ_ONCE(pmctx->mdb_n_entries);
780 void br_multicast_ngroups_set_max(struct net_bridge_mcast_port *pmctx, u32 max)
782 WRITE_ONCE(pmctx->mdb_max_entries, max);
785 u32 br_multicast_ngroups_get_max(const struct net_bridge_mcast_port *pmctx)
787 return READ_ONCE(pmctx->mdb_max_entries);
790 static void br_multicast_destroy_port_group(struct net_bridge_mcast_gc *gc)
792 struct net_bridge_port_group *pg;
794 pg = container_of(gc, struct net_bridge_port_group, mcast_gc);
795 WARN_ON(!hlist_unhashed(&pg->mglist));
796 WARN_ON(!hlist_empty(&pg->src_list));
798 timer_shutdown_sync(&pg->rexmit_timer);
799 timer_shutdown_sync(&pg->timer);
803 void br_multicast_del_pg(struct net_bridge_mdb_entry *mp,
804 struct net_bridge_port_group *pg,
805 struct net_bridge_port_group __rcu **pp)
807 struct net_bridge *br = pg->key.port->br;
808 struct net_bridge_group_src *ent;
809 struct hlist_node *tmp;
811 rcu_assign_pointer(*pp, pg->next);
812 hlist_del_init(&pg->mglist);
813 br_multicast_eht_clean_sets(pg);
814 hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
815 br_multicast_del_group_src(ent, false);
816 br_mdb_notify(br->dev, mp, pg, RTM_DELMDB);
817 if (!br_multicast_is_star_g(&mp->addr)) {
818 rhashtable_remove_fast(&br->sg_port_tbl, &pg->rhnode,
819 br_sg_port_rht_params);
820 br_multicast_sg_del_exclude_ports(mp);
822 br_multicast_star_g_handle_mode(pg, MCAST_INCLUDE);
824 br_multicast_port_ngroups_dec(pg->key.port, pg->key.addr.vid);
825 hlist_add_head(&pg->mcast_gc.gc_node, &br->mcast_gc_list);
826 queue_work(system_long_wq, &br->mcast_gc_work);
828 if (!mp->ports && !mp->host_joined && netif_running(br->dev))
829 mod_timer(&mp->timer, jiffies);
832 static void br_multicast_find_del_pg(struct net_bridge *br,
833 struct net_bridge_port_group *pg)
835 struct net_bridge_port_group __rcu **pp;
836 struct net_bridge_mdb_entry *mp;
837 struct net_bridge_port_group *p;
839 mp = br_mdb_ip_get(br, &pg->key.addr);
843 for (pp = &mp->ports;
844 (p = mlock_dereference(*pp, br)) != NULL;
849 br_multicast_del_pg(mp, pg, pp);
856 static void br_multicast_port_group_expired(struct timer_list *t)
858 struct net_bridge_port_group *pg = from_timer(pg, t, timer);
859 struct net_bridge_group_src *src_ent;
860 struct net_bridge *br = pg->key.port->br;
861 struct hlist_node *tmp;
864 spin_lock(&br->multicast_lock);
865 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
866 hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
869 changed = !!(pg->filter_mode == MCAST_EXCLUDE);
870 pg->filter_mode = MCAST_INCLUDE;
871 hlist_for_each_entry_safe(src_ent, tmp, &pg->src_list, node) {
872 if (!timer_pending(&src_ent->timer)) {
873 br_multicast_del_group_src(src_ent, false);
878 if (hlist_empty(&pg->src_list)) {
879 br_multicast_find_del_pg(br, pg);
880 } else if (changed) {
881 struct net_bridge_mdb_entry *mp = br_mdb_ip_get(br, &pg->key.addr);
883 if (changed && br_multicast_is_star_g(&pg->key.addr))
884 br_multicast_star_g_handle_mode(pg, MCAST_INCLUDE);
888 br_mdb_notify(br->dev, mp, pg, RTM_NEWMDB);
891 spin_unlock(&br->multicast_lock);
894 static void br_multicast_gc(struct hlist_head *head)
896 struct net_bridge_mcast_gc *gcent;
897 struct hlist_node *tmp;
899 hlist_for_each_entry_safe(gcent, tmp, head, gc_node) {
900 hlist_del_init(&gcent->gc_node);
901 gcent->destroy(gcent);
905 static void __br_multicast_query_handle_vlan(struct net_bridge_mcast *brmctx,
906 struct net_bridge_mcast_port *pmctx,
909 struct net_bridge_vlan *vlan = NULL;
911 if (pmctx && br_multicast_port_ctx_is_vlan(pmctx))
913 else if (br_multicast_ctx_is_vlan(brmctx))
916 if (vlan && !(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED)) {
919 if (br_vlan_get_proto(brmctx->br->dev, &vlan_proto) != 0)
921 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vlan->vid);
925 static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge_mcast *brmctx,
926 struct net_bridge_mcast_port *pmctx,
927 struct net_bridge_port_group *pg,
928 __be32 ip_dst, __be32 group,
929 bool with_srcs, bool over_lmqt,
930 u8 sflag, u8 *igmp_type,
933 struct net_bridge_port *p = pg ? pg->key.port : NULL;
934 struct net_bridge_group_src *ent;
935 size_t pkt_size, igmp_hdr_size;
936 unsigned long now = jiffies;
937 struct igmpv3_query *ihv3;
938 void *csum_start = NULL;
939 __sum16 *csum = NULL;
947 igmp_hdr_size = sizeof(*ih);
948 if (brmctx->multicast_igmp_version == 3) {
949 igmp_hdr_size = sizeof(*ihv3);
950 if (pg && with_srcs) {
951 lmqt = now + (brmctx->multicast_last_member_interval *
952 brmctx->multicast_last_member_count);
953 hlist_for_each_entry(ent, &pg->src_list, node) {
954 if (over_lmqt == time_after(ent->timer.expires,
956 ent->src_query_rexmit_cnt > 0)
962 igmp_hdr_size += lmqt_srcs * sizeof(__be32);
966 pkt_size = sizeof(*eth) + sizeof(*iph) + 4 + igmp_hdr_size;
967 if ((p && pkt_size > p->dev->mtu) ||
968 pkt_size > brmctx->br->dev->mtu)
971 skb = netdev_alloc_skb_ip_align(brmctx->br->dev, pkt_size);
975 __br_multicast_query_handle_vlan(brmctx, pmctx, skb);
976 skb->protocol = htons(ETH_P_IP);
978 skb_reset_mac_header(skb);
981 ether_addr_copy(eth->h_source, brmctx->br->dev->dev_addr);
982 ip_eth_mc_map(ip_dst, eth->h_dest);
983 eth->h_proto = htons(ETH_P_IP);
984 skb_put(skb, sizeof(*eth));
986 skb_set_network_header(skb, skb->len);
988 iph->tot_len = htons(pkt_size - sizeof(*eth));
994 iph->frag_off = htons(IP_DF);
996 iph->protocol = IPPROTO_IGMP;
997 iph->saddr = br_opt_get(brmctx->br, BROPT_MULTICAST_QUERY_USE_IFADDR) ?
998 inet_select_addr(brmctx->br->dev, 0, RT_SCOPE_LINK) : 0;
1000 ((u8 *)&iph[1])[0] = IPOPT_RA;
1001 ((u8 *)&iph[1])[1] = 4;
1002 ((u8 *)&iph[1])[2] = 0;
1003 ((u8 *)&iph[1])[3] = 0;
1007 skb_set_transport_header(skb, skb->len);
1008 *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
1010 switch (brmctx->multicast_igmp_version) {
1013 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
1014 ih->code = (group ? brmctx->multicast_last_member_interval :
1015 brmctx->multicast_query_response_interval) /
1016 (HZ / IGMP_TIMER_SCALE);
1020 csum_start = (void *)ih;
1023 ihv3 = igmpv3_query_hdr(skb);
1024 ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
1025 ihv3->code = (group ? brmctx->multicast_last_member_interval :
1026 brmctx->multicast_query_response_interval) /
1027 (HZ / IGMP_TIMER_SCALE);
1028 ihv3->group = group;
1029 ihv3->qqic = brmctx->multicast_query_interval / HZ;
1030 ihv3->nsrcs = htons(lmqt_srcs);
1032 ihv3->suppress = sflag;
1036 csum_start = (void *)ihv3;
1037 if (!pg || !with_srcs)
1041 hlist_for_each_entry(ent, &pg->src_list, node) {
1042 if (over_lmqt == time_after(ent->timer.expires,
1044 ent->src_query_rexmit_cnt > 0) {
1045 ihv3->srcs[lmqt_srcs++] = ent->addr.src.ip4;
1046 ent->src_query_rexmit_cnt--;
1047 if (need_rexmit && ent->src_query_rexmit_cnt)
1048 *need_rexmit = true;
1051 if (WARN_ON(lmqt_srcs != ntohs(ihv3->nsrcs))) {
1058 if (WARN_ON(!csum || !csum_start)) {
1063 *csum = ip_compute_csum(csum_start, igmp_hdr_size);
1064 skb_put(skb, igmp_hdr_size);
1065 __skb_pull(skb, sizeof(*eth));
1071 #if IS_ENABLED(CONFIG_IPV6)
1072 static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge_mcast *brmctx,
1073 struct net_bridge_mcast_port *pmctx,
1074 struct net_bridge_port_group *pg,
1075 const struct in6_addr *ip6_dst,
1076 const struct in6_addr *group,
1077 bool with_srcs, bool over_llqt,
1078 u8 sflag, u8 *igmp_type,
1081 struct net_bridge_port *p = pg ? pg->key.port : NULL;
1082 struct net_bridge_group_src *ent;
1083 size_t pkt_size, mld_hdr_size;
1084 unsigned long now = jiffies;
1085 struct mld2_query *mld2q;
1086 void *csum_start = NULL;
1087 unsigned long interval;
1088 __sum16 *csum = NULL;
1089 struct ipv6hdr *ip6h;
1090 struct mld_msg *mldq;
1091 struct sk_buff *skb;
1097 mld_hdr_size = sizeof(*mldq);
1098 if (brmctx->multicast_mld_version == 2) {
1099 mld_hdr_size = sizeof(*mld2q);
1100 if (pg && with_srcs) {
1101 llqt = now + (brmctx->multicast_last_member_interval *
1102 brmctx->multicast_last_member_count);
1103 hlist_for_each_entry(ent, &pg->src_list, node) {
1104 if (over_llqt == time_after(ent->timer.expires,
1106 ent->src_query_rexmit_cnt > 0)
1112 mld_hdr_size += llqt_srcs * sizeof(struct in6_addr);
1116 pkt_size = sizeof(*eth) + sizeof(*ip6h) + 8 + mld_hdr_size;
1117 if ((p && pkt_size > p->dev->mtu) ||
1118 pkt_size > brmctx->br->dev->mtu)
1121 skb = netdev_alloc_skb_ip_align(brmctx->br->dev, pkt_size);
1125 __br_multicast_query_handle_vlan(brmctx, pmctx, skb);
1126 skb->protocol = htons(ETH_P_IPV6);
1128 /* Ethernet header */
1129 skb_reset_mac_header(skb);
1132 ether_addr_copy(eth->h_source, brmctx->br->dev->dev_addr);
1133 eth->h_proto = htons(ETH_P_IPV6);
1134 skb_put(skb, sizeof(*eth));
1136 /* IPv6 header + HbH option */
1137 skb_set_network_header(skb, skb->len);
1138 ip6h = ipv6_hdr(skb);
1140 *(__force __be32 *)ip6h = htonl(0x60000000);
1141 ip6h->payload_len = htons(8 + mld_hdr_size);
1142 ip6h->nexthdr = IPPROTO_HOPOPTS;
1143 ip6h->hop_limit = 1;
1144 ip6h->daddr = *ip6_dst;
1145 if (ipv6_dev_get_saddr(dev_net(brmctx->br->dev), brmctx->br->dev,
1146 &ip6h->daddr, 0, &ip6h->saddr)) {
1148 br_opt_toggle(brmctx->br, BROPT_HAS_IPV6_ADDR, false);
1152 br_opt_toggle(brmctx->br, BROPT_HAS_IPV6_ADDR, true);
1153 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
1155 hopopt = (u8 *)(ip6h + 1);
1156 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
1157 hopopt[1] = 0; /* length of HbH */
1158 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
1159 hopopt[3] = 2; /* Length of RA Option */
1160 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
1162 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
1163 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
1165 skb_put(skb, sizeof(*ip6h) + 8);
1168 skb_set_transport_header(skb, skb->len);
1169 interval = ipv6_addr_any(group) ?
1170 brmctx->multicast_query_response_interval :
1171 brmctx->multicast_last_member_interval;
1172 *igmp_type = ICMPV6_MGM_QUERY;
1173 switch (brmctx->multicast_mld_version) {
1175 mldq = (struct mld_msg *)icmp6_hdr(skb);
1176 mldq->mld_type = ICMPV6_MGM_QUERY;
1178 mldq->mld_cksum = 0;
1179 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
1180 mldq->mld_reserved = 0;
1181 mldq->mld_mca = *group;
1182 csum = &mldq->mld_cksum;
1183 csum_start = (void *)mldq;
1186 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1187 mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
1188 mld2q->mld2q_type = ICMPV6_MGM_QUERY;
1189 mld2q->mld2q_code = 0;
1190 mld2q->mld2q_cksum = 0;
1191 mld2q->mld2q_resv1 = 0;
1192 mld2q->mld2q_resv2 = 0;
1193 mld2q->mld2q_suppress = sflag;
1194 mld2q->mld2q_qrv = 2;
1195 mld2q->mld2q_nsrcs = htons(llqt_srcs);
1196 mld2q->mld2q_qqic = brmctx->multicast_query_interval / HZ;
1197 mld2q->mld2q_mca = *group;
1198 csum = &mld2q->mld2q_cksum;
1199 csum_start = (void *)mld2q;
1200 if (!pg || !with_srcs)
1204 hlist_for_each_entry(ent, &pg->src_list, node) {
1205 if (over_llqt == time_after(ent->timer.expires,
1207 ent->src_query_rexmit_cnt > 0) {
1208 mld2q->mld2q_srcs[llqt_srcs++] = ent->addr.src.ip6;
1209 ent->src_query_rexmit_cnt--;
1210 if (need_rexmit && ent->src_query_rexmit_cnt)
1211 *need_rexmit = true;
1214 if (WARN_ON(llqt_srcs != ntohs(mld2q->mld2q_nsrcs))) {
1221 if (WARN_ON(!csum || !csum_start)) {
1226 *csum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, mld_hdr_size,
1228 csum_partial(csum_start, mld_hdr_size, 0));
1229 skb_put(skb, mld_hdr_size);
1230 __skb_pull(skb, sizeof(*eth));
1237 static struct sk_buff *br_multicast_alloc_query(struct net_bridge_mcast *brmctx,
1238 struct net_bridge_mcast_port *pmctx,
1239 struct net_bridge_port_group *pg,
1240 struct br_ip *ip_dst,
1241 struct br_ip *group,
1242 bool with_srcs, bool over_lmqt,
1243 u8 sflag, u8 *igmp_type,
1248 switch (group->proto) {
1249 case htons(ETH_P_IP):
1250 ip4_dst = ip_dst ? ip_dst->dst.ip4 : htonl(INADDR_ALLHOSTS_GROUP);
1251 return br_ip4_multicast_alloc_query(brmctx, pmctx, pg,
1252 ip4_dst, group->dst.ip4,
1253 with_srcs, over_lmqt,
1256 #if IS_ENABLED(CONFIG_IPV6)
1257 case htons(ETH_P_IPV6): {
1258 struct in6_addr ip6_dst;
1261 ip6_dst = ip_dst->dst.ip6;
1263 ipv6_addr_set(&ip6_dst, htonl(0xff020000), 0, 0,
1266 return br_ip6_multicast_alloc_query(brmctx, pmctx, pg,
1267 &ip6_dst, &group->dst.ip6,
1268 with_srcs, over_lmqt,
1277 struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
1278 struct br_ip *group)
1280 struct net_bridge_mdb_entry *mp;
1283 mp = br_mdb_ip_get(br, group);
1287 if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) {
1288 trace_br_mdb_full(br->dev, group);
1289 br_mc_disabled_update(br->dev, false, NULL);
1290 br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false);
1291 return ERR_PTR(-E2BIG);
1294 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
1296 return ERR_PTR(-ENOMEM);
1300 mp->mcast_gc.destroy = br_multicast_destroy_mdb_entry;
1301 timer_setup(&mp->timer, br_multicast_group_expired, 0);
1302 err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode,
1308 hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list);
1314 static void br_multicast_group_src_expired(struct timer_list *t)
1316 struct net_bridge_group_src *src = from_timer(src, t, timer);
1317 struct net_bridge_port_group *pg;
1318 struct net_bridge *br = src->br;
1320 spin_lock(&br->multicast_lock);
1321 if (hlist_unhashed(&src->node) || !netif_running(br->dev) ||
1322 timer_pending(&src->timer))
1326 if (pg->filter_mode == MCAST_INCLUDE) {
1327 br_multicast_del_group_src(src, false);
1328 if (!hlist_empty(&pg->src_list))
1330 br_multicast_find_del_pg(br, pg);
1332 br_multicast_fwd_src_handle(src);
1336 spin_unlock(&br->multicast_lock);
1339 struct net_bridge_group_src *
1340 br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip)
1342 struct net_bridge_group_src *ent;
1344 switch (ip->proto) {
1345 case htons(ETH_P_IP):
1346 hlist_for_each_entry(ent, &pg->src_list, node)
1347 if (ip->src.ip4 == ent->addr.src.ip4)
1350 #if IS_ENABLED(CONFIG_IPV6)
1351 case htons(ETH_P_IPV6):
1352 hlist_for_each_entry(ent, &pg->src_list, node)
1353 if (!ipv6_addr_cmp(&ent->addr.src.ip6, &ip->src.ip6))
1362 struct net_bridge_group_src *
1363 br_multicast_new_group_src(struct net_bridge_port_group *pg, struct br_ip *src_ip)
1365 struct net_bridge_group_src *grp_src;
1367 if (unlikely(pg->src_ents >= PG_SRC_ENT_LIMIT))
1370 switch (src_ip->proto) {
1371 case htons(ETH_P_IP):
1372 if (ipv4_is_zeronet(src_ip->src.ip4) ||
1373 ipv4_is_multicast(src_ip->src.ip4))
1376 #if IS_ENABLED(CONFIG_IPV6)
1377 case htons(ETH_P_IPV6):
1378 if (ipv6_addr_any(&src_ip->src.ip6) ||
1379 ipv6_addr_is_multicast(&src_ip->src.ip6))
1385 grp_src = kzalloc(sizeof(*grp_src), GFP_ATOMIC);
1386 if (unlikely(!grp_src))
1390 grp_src->br = pg->key.port->br;
1391 grp_src->addr = *src_ip;
1392 grp_src->mcast_gc.destroy = br_multicast_destroy_group_src;
1393 timer_setup(&grp_src->timer, br_multicast_group_src_expired, 0);
1395 hlist_add_head_rcu(&grp_src->node, &pg->src_list);
1401 struct net_bridge_port_group *br_multicast_new_port_group(
1402 struct net_bridge_port *port,
1403 const struct br_ip *group,
1404 struct net_bridge_port_group __rcu *next,
1405 unsigned char flags,
1406 const unsigned char *src,
1409 struct netlink_ext_ack *extack)
1411 struct net_bridge_port_group *p;
1414 err = br_multicast_port_ngroups_inc(port, group, extack);
1418 p = kzalloc(sizeof(*p), GFP_ATOMIC);
1420 NL_SET_ERR_MSG_MOD(extack, "Couldn't allocate new port group");
1424 p->key.addr = *group;
1427 p->filter_mode = filter_mode;
1428 p->rt_protocol = rt_protocol;
1429 p->eht_host_tree = RB_ROOT;
1430 p->eht_set_tree = RB_ROOT;
1431 p->mcast_gc.destroy = br_multicast_destroy_port_group;
1432 INIT_HLIST_HEAD(&p->src_list);
1434 if (!br_multicast_is_star_g(group) &&
1435 rhashtable_lookup_insert_fast(&port->br->sg_port_tbl, &p->rhnode,
1436 br_sg_port_rht_params)) {
1437 NL_SET_ERR_MSG_MOD(extack, "Couldn't insert new port group");
1441 rcu_assign_pointer(p->next, next);
1442 timer_setup(&p->timer, br_multicast_port_group_expired, 0);
1443 timer_setup(&p->rexmit_timer, br_multicast_port_group_rexmit, 0);
1444 hlist_add_head(&p->mglist, &port->mglist);
1447 memcpy(p->eth_addr, src, ETH_ALEN);
1449 eth_broadcast_addr(p->eth_addr);
1456 br_multicast_port_ngroups_dec(port, group->vid);
1460 void br_multicast_del_port_group(struct net_bridge_port_group *p)
1462 struct net_bridge_port *port = p->key.port;
1463 __u16 vid = p->key.addr.vid;
1465 hlist_del_init(&p->mglist);
1466 if (!br_multicast_is_star_g(&p->key.addr))
1467 rhashtable_remove_fast(&port->br->sg_port_tbl, &p->rhnode,
1468 br_sg_port_rht_params);
1470 br_multicast_port_ngroups_dec(port, vid);
1473 void br_multicast_host_join(const struct net_bridge_mcast *brmctx,
1474 struct net_bridge_mdb_entry *mp, bool notify)
1476 if (!mp->host_joined) {
1477 mp->host_joined = true;
1478 if (br_multicast_is_star_g(&mp->addr))
1479 br_multicast_star_g_host_state(mp);
1481 br_mdb_notify(mp->br->dev, mp, NULL, RTM_NEWMDB);
1484 if (br_group_is_l2(&mp->addr))
1487 mod_timer(&mp->timer, jiffies + brmctx->multicast_membership_interval);
1490 void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify)
1492 if (!mp->host_joined)
1495 mp->host_joined = false;
1496 if (br_multicast_is_star_g(&mp->addr))
1497 br_multicast_star_g_host_state(mp);
1499 br_mdb_notify(mp->br->dev, mp, NULL, RTM_DELMDB);
1502 static struct net_bridge_port_group *
1503 __br_multicast_add_group(struct net_bridge_mcast *brmctx,
1504 struct net_bridge_mcast_port *pmctx,
1505 struct br_ip *group,
1506 const unsigned char *src,
1511 struct net_bridge_port_group __rcu **pp;
1512 struct net_bridge_port_group *p = NULL;
1513 struct net_bridge_mdb_entry *mp;
1514 unsigned long now = jiffies;
1516 if (!br_multicast_ctx_should_use(brmctx, pmctx))
1519 mp = br_multicast_new_group(brmctx->br, group);
1521 return ERR_CAST(mp);
1524 br_multicast_host_join(brmctx, mp, true);
1528 for (pp = &mp->ports;
1529 (p = mlock_dereference(*pp, brmctx->br)) != NULL;
1531 if (br_port_group_equal(p, pmctx->port, src))
1533 if ((unsigned long)p->key.port < (unsigned long)pmctx->port)
1537 p = br_multicast_new_port_group(pmctx->port, group, *pp, 0, src,
1538 filter_mode, RTPROT_KERNEL, NULL);
1540 p = ERR_PTR(-ENOMEM);
1543 rcu_assign_pointer(*pp, p);
1545 p->flags |= MDB_PG_FLAGS_BLOCKED;
1546 br_mdb_notify(brmctx->br->dev, mp, p, RTM_NEWMDB);
1550 mod_timer(&p->timer,
1551 now + brmctx->multicast_membership_interval);
1557 static int br_multicast_add_group(struct net_bridge_mcast *brmctx,
1558 struct net_bridge_mcast_port *pmctx,
1559 struct br_ip *group,
1560 const unsigned char *src,
1564 struct net_bridge_port_group *pg;
1567 spin_lock(&brmctx->br->multicast_lock);
1568 pg = __br_multicast_add_group(brmctx, pmctx, group, src, filter_mode,
1569 igmpv2_mldv1, false);
1570 /* NULL is considered valid for host joined groups */
1571 err = PTR_ERR_OR_ZERO(pg);
1572 spin_unlock(&brmctx->br->multicast_lock);
1577 static int br_ip4_multicast_add_group(struct net_bridge_mcast *brmctx,
1578 struct net_bridge_mcast_port *pmctx,
1581 const unsigned char *src,
1584 struct br_ip br_group;
1587 if (ipv4_is_local_multicast(group))
1590 memset(&br_group, 0, sizeof(br_group));
1591 br_group.dst.ip4 = group;
1592 br_group.proto = htons(ETH_P_IP);
1594 filter_mode = igmpv2 ? MCAST_EXCLUDE : MCAST_INCLUDE;
1596 return br_multicast_add_group(brmctx, pmctx, &br_group, src,
1597 filter_mode, igmpv2);
1600 #if IS_ENABLED(CONFIG_IPV6)
1601 static int br_ip6_multicast_add_group(struct net_bridge_mcast *brmctx,
1602 struct net_bridge_mcast_port *pmctx,
1603 const struct in6_addr *group,
1605 const unsigned char *src,
1608 struct br_ip br_group;
1611 if (ipv6_addr_is_ll_all_nodes(group))
1614 memset(&br_group, 0, sizeof(br_group));
1615 br_group.dst.ip6 = *group;
1616 br_group.proto = htons(ETH_P_IPV6);
1618 filter_mode = mldv1 ? MCAST_EXCLUDE : MCAST_INCLUDE;
1620 return br_multicast_add_group(brmctx, pmctx, &br_group, src,
1621 filter_mode, mldv1);
1625 static bool br_multicast_rport_del(struct hlist_node *rlist)
1627 if (hlist_unhashed(rlist))
1630 hlist_del_init_rcu(rlist);
1634 static bool br_ip4_multicast_rport_del(struct net_bridge_mcast_port *pmctx)
1636 return br_multicast_rport_del(&pmctx->ip4_rlist);
1639 static bool br_ip6_multicast_rport_del(struct net_bridge_mcast_port *pmctx)
1641 #if IS_ENABLED(CONFIG_IPV6)
1642 return br_multicast_rport_del(&pmctx->ip6_rlist);
1648 static void br_multicast_router_expired(struct net_bridge_mcast_port *pmctx,
1649 struct timer_list *t,
1650 struct hlist_node *rlist)
1652 struct net_bridge *br = pmctx->port->br;
1655 spin_lock(&br->multicast_lock);
1656 if (pmctx->multicast_router == MDB_RTR_TYPE_DISABLED ||
1657 pmctx->multicast_router == MDB_RTR_TYPE_PERM ||
1661 del = br_multicast_rport_del(rlist);
1662 br_multicast_rport_del_notify(pmctx, del);
1664 spin_unlock(&br->multicast_lock);
1667 static void br_ip4_multicast_router_expired(struct timer_list *t)
1669 struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t,
1670 ip4_mc_router_timer);
1672 br_multicast_router_expired(pmctx, t, &pmctx->ip4_rlist);
1675 #if IS_ENABLED(CONFIG_IPV6)
1676 static void br_ip6_multicast_router_expired(struct timer_list *t)
1678 struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t,
1679 ip6_mc_router_timer);
1681 br_multicast_router_expired(pmctx, t, &pmctx->ip6_rlist);
1685 static void br_mc_router_state_change(struct net_bridge *p,
1688 struct switchdev_attr attr = {
1690 .id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
1691 .flags = SWITCHDEV_F_DEFER,
1692 .u.mrouter = is_mc_router,
1695 switchdev_port_attr_set(p->dev, &attr, NULL);
1698 static void br_multicast_local_router_expired(struct net_bridge_mcast *brmctx,
1699 struct timer_list *timer)
1701 spin_lock(&brmctx->br->multicast_lock);
1702 if (brmctx->multicast_router == MDB_RTR_TYPE_DISABLED ||
1703 brmctx->multicast_router == MDB_RTR_TYPE_PERM ||
1704 br_ip4_multicast_is_router(brmctx) ||
1705 br_ip6_multicast_is_router(brmctx))
1708 br_mc_router_state_change(brmctx->br, false);
1710 spin_unlock(&brmctx->br->multicast_lock);
1713 static void br_ip4_multicast_local_router_expired(struct timer_list *t)
1715 struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
1716 ip4_mc_router_timer);
1718 br_multicast_local_router_expired(brmctx, t);
1721 #if IS_ENABLED(CONFIG_IPV6)
1722 static void br_ip6_multicast_local_router_expired(struct timer_list *t)
1724 struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
1725 ip6_mc_router_timer);
1727 br_multicast_local_router_expired(brmctx, t);
1731 static void br_multicast_querier_expired(struct net_bridge_mcast *brmctx,
1732 struct bridge_mcast_own_query *query)
1734 spin_lock(&brmctx->br->multicast_lock);
1735 if (!netif_running(brmctx->br->dev) ||
1736 br_multicast_ctx_vlan_global_disabled(brmctx) ||
1737 !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED))
1740 br_multicast_start_querier(brmctx, query);
1743 spin_unlock(&brmctx->br->multicast_lock);
1746 static void br_ip4_multicast_querier_expired(struct timer_list *t)
1748 struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
1749 ip4_other_query.timer);
1751 br_multicast_querier_expired(brmctx, &brmctx->ip4_own_query);
1754 #if IS_ENABLED(CONFIG_IPV6)
1755 static void br_ip6_multicast_querier_expired(struct timer_list *t)
1757 struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
1758 ip6_other_query.timer);
1760 br_multicast_querier_expired(brmctx, &brmctx->ip6_own_query);
1764 static void br_multicast_query_delay_expired(struct timer_list *t)
1768 static void br_multicast_select_own_querier(struct net_bridge_mcast *brmctx,
1770 struct sk_buff *skb)
1772 if (ip->proto == htons(ETH_P_IP))
1773 brmctx->ip4_querier.addr.src.ip4 = ip_hdr(skb)->saddr;
1774 #if IS_ENABLED(CONFIG_IPV6)
1776 brmctx->ip6_querier.addr.src.ip6 = ipv6_hdr(skb)->saddr;
1780 static void __br_multicast_send_query(struct net_bridge_mcast *brmctx,
1781 struct net_bridge_mcast_port *pmctx,
1782 struct net_bridge_port_group *pg,
1783 struct br_ip *ip_dst,
1784 struct br_ip *group,
1789 bool over_lmqt = !!sflag;
1790 struct sk_buff *skb;
1793 if (!br_multicast_ctx_should_use(brmctx, pmctx) ||
1794 !br_multicast_ctx_matches_vlan_snooping(brmctx))
1798 skb = br_multicast_alloc_query(brmctx, pmctx, pg, ip_dst, group,
1799 with_srcs, over_lmqt, sflag, &igmp_type,
1805 skb->dev = pmctx->port->dev;
1806 br_multicast_count(brmctx->br, pmctx->port, skb, igmp_type,
1808 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
1809 dev_net(pmctx->port->dev), NULL, skb, NULL, skb->dev,
1810 br_dev_queue_push_xmit);
1812 if (over_lmqt && with_srcs && sflag) {
1814 goto again_under_lmqt;
1817 br_multicast_select_own_querier(brmctx, group, skb);
1818 br_multicast_count(brmctx->br, NULL, skb, igmp_type,
1824 static void br_multicast_read_querier(const struct bridge_mcast_querier *querier,
1825 struct bridge_mcast_querier *dest)
1829 memset(dest, 0, sizeof(*dest));
1831 seq = read_seqcount_begin(&querier->seq);
1832 dest->port_ifidx = querier->port_ifidx;
1833 memcpy(&dest->addr, &querier->addr, sizeof(struct br_ip));
1834 } while (read_seqcount_retry(&querier->seq, seq));
1837 static void br_multicast_update_querier(struct net_bridge_mcast *brmctx,
1838 struct bridge_mcast_querier *querier,
1840 struct br_ip *saddr)
1842 write_seqcount_begin(&querier->seq);
1843 querier->port_ifidx = ifindex;
1844 memcpy(&querier->addr, saddr, sizeof(*saddr));
1845 write_seqcount_end(&querier->seq);
1848 static void br_multicast_send_query(struct net_bridge_mcast *brmctx,
1849 struct net_bridge_mcast_port *pmctx,
1850 struct bridge_mcast_own_query *own_query)
1852 struct bridge_mcast_other_query *other_query = NULL;
1853 struct bridge_mcast_querier *querier;
1854 struct br_ip br_group;
1857 if (!br_multicast_ctx_should_use(brmctx, pmctx) ||
1858 !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED) ||
1859 !brmctx->multicast_querier)
1862 memset(&br_group.dst, 0, sizeof(br_group.dst));
1864 if (pmctx ? (own_query == &pmctx->ip4_own_query) :
1865 (own_query == &brmctx->ip4_own_query)) {
1866 querier = &brmctx->ip4_querier;
1867 other_query = &brmctx->ip4_other_query;
1868 br_group.proto = htons(ETH_P_IP);
1869 #if IS_ENABLED(CONFIG_IPV6)
1871 querier = &brmctx->ip6_querier;
1872 other_query = &brmctx->ip6_other_query;
1873 br_group.proto = htons(ETH_P_IPV6);
1877 if (!other_query || timer_pending(&other_query->timer))
1880 /* we're about to select ourselves as querier */
1881 if (!pmctx && querier->port_ifidx) {
1882 struct br_ip zeroip = {};
1884 br_multicast_update_querier(brmctx, querier, 0, &zeroip);
1887 __br_multicast_send_query(brmctx, pmctx, NULL, NULL, &br_group, false,
1891 time += own_query->startup_sent < brmctx->multicast_startup_query_count ?
1892 brmctx->multicast_startup_query_interval :
1893 brmctx->multicast_query_interval;
1894 mod_timer(&own_query->timer, time);
1898 br_multicast_port_query_expired(struct net_bridge_mcast_port *pmctx,
1899 struct bridge_mcast_own_query *query)
1901 struct net_bridge *br = pmctx->port->br;
1902 struct net_bridge_mcast *brmctx;
1904 spin_lock(&br->multicast_lock);
1905 if (br_multicast_port_ctx_state_stopped(pmctx))
1908 brmctx = br_multicast_port_ctx_get_global(pmctx);
1909 if (query->startup_sent < brmctx->multicast_startup_query_count)
1910 query->startup_sent++;
1912 br_multicast_send_query(brmctx, pmctx, query);
1915 spin_unlock(&br->multicast_lock);
1918 static void br_ip4_multicast_port_query_expired(struct timer_list *t)
1920 struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t,
1921 ip4_own_query.timer);
1923 br_multicast_port_query_expired(pmctx, &pmctx->ip4_own_query);
1926 #if IS_ENABLED(CONFIG_IPV6)
1927 static void br_ip6_multicast_port_query_expired(struct timer_list *t)
1929 struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t,
1930 ip6_own_query.timer);
1932 br_multicast_port_query_expired(pmctx, &pmctx->ip6_own_query);
1936 static void br_multicast_port_group_rexmit(struct timer_list *t)
1938 struct net_bridge_port_group *pg = from_timer(pg, t, rexmit_timer);
1939 struct bridge_mcast_other_query *other_query = NULL;
1940 struct net_bridge *br = pg->key.port->br;
1941 struct net_bridge_mcast_port *pmctx;
1942 struct net_bridge_mcast *brmctx;
1943 bool need_rexmit = false;
1945 spin_lock(&br->multicast_lock);
1946 if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
1947 !br_opt_get(br, BROPT_MULTICAST_ENABLED))
1950 pmctx = br_multicast_pg_to_port_ctx(pg);
1953 brmctx = br_multicast_port_ctx_get_global(pmctx);
1954 if (!brmctx->multicast_querier)
1957 if (pg->key.addr.proto == htons(ETH_P_IP))
1958 other_query = &brmctx->ip4_other_query;
1959 #if IS_ENABLED(CONFIG_IPV6)
1961 other_query = &brmctx->ip6_other_query;
1964 if (!other_query || timer_pending(&other_query->timer))
1967 if (pg->grp_query_rexmit_cnt) {
1968 pg->grp_query_rexmit_cnt--;
1969 __br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr,
1970 &pg->key.addr, false, 1, NULL);
1972 __br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr,
1973 &pg->key.addr, true, 0, &need_rexmit);
1975 if (pg->grp_query_rexmit_cnt || need_rexmit)
1976 mod_timer(&pg->rexmit_timer, jiffies +
1977 brmctx->multicast_last_member_interval);
1979 spin_unlock(&br->multicast_lock);
1982 static int br_mc_disabled_update(struct net_device *dev, bool value,
1983 struct netlink_ext_ack *extack)
1985 struct switchdev_attr attr = {
1987 .id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
1988 .flags = SWITCHDEV_F_DEFER,
1989 .u.mc_disabled = !value,
1992 return switchdev_port_attr_set(dev, &attr, extack);
1995 void br_multicast_port_ctx_init(struct net_bridge_port *port,
1996 struct net_bridge_vlan *vlan,
1997 struct net_bridge_mcast_port *pmctx)
2001 pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
2002 timer_setup(&pmctx->ip4_mc_router_timer,
2003 br_ip4_multicast_router_expired, 0);
2004 timer_setup(&pmctx->ip4_own_query.timer,
2005 br_ip4_multicast_port_query_expired, 0);
2006 #if IS_ENABLED(CONFIG_IPV6)
2007 timer_setup(&pmctx->ip6_mc_router_timer,
2008 br_ip6_multicast_router_expired, 0);
2009 timer_setup(&pmctx->ip6_own_query.timer,
2010 br_ip6_multicast_port_query_expired, 0);
2014 void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx)
2016 #if IS_ENABLED(CONFIG_IPV6)
2017 del_timer_sync(&pmctx->ip6_mc_router_timer);
2019 del_timer_sync(&pmctx->ip4_mc_router_timer);
2022 int br_multicast_add_port(struct net_bridge_port *port)
2026 port->multicast_eht_hosts_limit = BR_MCAST_DEFAULT_EHT_HOSTS_LIMIT;
2027 br_multicast_port_ctx_init(port, NULL, &port->multicast_ctx);
2029 err = br_mc_disabled_update(port->dev,
2030 br_opt_get(port->br,
2031 BROPT_MULTICAST_ENABLED),
2033 if (err && err != -EOPNOTSUPP)
2036 port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
2037 if (!port->mcast_stats)
2043 void br_multicast_del_port(struct net_bridge_port *port)
2045 struct net_bridge *br = port->br;
2046 struct net_bridge_port_group *pg;
2047 HLIST_HEAD(deleted_head);
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 hlist_move_list(&br->mcast_gc_list, &deleted_head);
2055 spin_unlock_bh(&br->multicast_lock);
2056 br_multicast_gc(&deleted_head);
2057 br_multicast_port_ctx_deinit(&port->multicast_ctx);
2058 free_percpu(port->mcast_stats);
2061 static void br_multicast_enable(struct bridge_mcast_own_query *query)
2063 query->startup_sent = 0;
2065 if (try_to_del_timer_sync(&query->timer) >= 0 ||
2066 del_timer(&query->timer))
2067 mod_timer(&query->timer, jiffies);
2070 static void __br_multicast_enable_port_ctx(struct net_bridge_mcast_port *pmctx)
2072 struct net_bridge *br = pmctx->port->br;
2073 struct net_bridge_mcast *brmctx;
2075 brmctx = br_multicast_port_ctx_get_global(pmctx);
2076 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
2077 !netif_running(br->dev))
2080 br_multicast_enable(&pmctx->ip4_own_query);
2081 #if IS_ENABLED(CONFIG_IPV6)
2082 br_multicast_enable(&pmctx->ip6_own_query);
2084 if (pmctx->multicast_router == MDB_RTR_TYPE_PERM) {
2085 br_ip4_multicast_add_router(brmctx, pmctx);
2086 br_ip6_multicast_add_router(brmctx, pmctx);
2089 if (br_multicast_port_ctx_is_vlan(pmctx)) {
2090 struct net_bridge_port_group *pg;
2093 /* The mcast_n_groups counter might be wrong. First,
2094 * BR_VLFLAG_MCAST_ENABLED is toggled before temporary entries
2095 * are flushed, thus mcast_n_groups after the toggle does not
2096 * reflect the true values. And second, permanent entries added
2097 * while BR_VLFLAG_MCAST_ENABLED was disabled, are not reflected
2098 * either. Thus we have to refresh the counter.
2101 hlist_for_each_entry(pg, &pmctx->port->mglist, mglist) {
2102 if (pg->key.addr.vid == pmctx->vlan->vid)
2105 WRITE_ONCE(pmctx->mdb_n_entries, n);
2109 void br_multicast_enable_port(struct net_bridge_port *port)
2111 struct net_bridge *br = port->br;
2113 spin_lock_bh(&br->multicast_lock);
2114 __br_multicast_enable_port_ctx(&port->multicast_ctx);
2115 spin_unlock_bh(&br->multicast_lock);
2118 static void __br_multicast_disable_port_ctx(struct net_bridge_mcast_port *pmctx)
2120 struct net_bridge_port_group *pg;
2121 struct hlist_node *n;
2124 hlist_for_each_entry_safe(pg, n, &pmctx->port->mglist, mglist)
2125 if (!(pg->flags & MDB_PG_FLAGS_PERMANENT) &&
2126 (!br_multicast_port_ctx_is_vlan(pmctx) ||
2127 pg->key.addr.vid == pmctx->vlan->vid))
2128 br_multicast_find_del_pg(pmctx->port->br, pg);
2130 del |= br_ip4_multicast_rport_del(pmctx);
2131 del_timer(&pmctx->ip4_mc_router_timer);
2132 del_timer(&pmctx->ip4_own_query.timer);
2133 del |= br_ip6_multicast_rport_del(pmctx);
2134 #if IS_ENABLED(CONFIG_IPV6)
2135 del_timer(&pmctx->ip6_mc_router_timer);
2136 del_timer(&pmctx->ip6_own_query.timer);
2138 br_multicast_rport_del_notify(pmctx, del);
2141 void br_multicast_disable_port(struct net_bridge_port *port)
2143 spin_lock_bh(&port->br->multicast_lock);
2144 __br_multicast_disable_port_ctx(&port->multicast_ctx);
2145 spin_unlock_bh(&port->br->multicast_lock);
2148 static int __grp_src_delete_marked(struct net_bridge_port_group *pg)
2150 struct net_bridge_group_src *ent;
2151 struct hlist_node *tmp;
2154 hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
2155 if (ent->flags & BR_SGRP_F_DELETE) {
2156 br_multicast_del_group_src(ent, false);
2163 static void __grp_src_mod_timer(struct net_bridge_group_src *src,
2164 unsigned long expires)
2166 mod_timer(&src->timer, expires);
2167 br_multicast_fwd_src_handle(src);
2170 static void __grp_src_query_marked_and_rexmit(struct net_bridge_mcast *brmctx,
2171 struct net_bridge_mcast_port *pmctx,
2172 struct net_bridge_port_group *pg)
2174 struct bridge_mcast_other_query *other_query = NULL;
2175 u32 lmqc = brmctx->multicast_last_member_count;
2176 unsigned long lmqt, lmi, now = jiffies;
2177 struct net_bridge_group_src *ent;
2179 if (!netif_running(brmctx->br->dev) ||
2180 !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED))
2183 if (pg->key.addr.proto == htons(ETH_P_IP))
2184 other_query = &brmctx->ip4_other_query;
2185 #if IS_ENABLED(CONFIG_IPV6)
2187 other_query = &brmctx->ip6_other_query;
2190 lmqt = now + br_multicast_lmqt(brmctx);
2191 hlist_for_each_entry(ent, &pg->src_list, node) {
2192 if (ent->flags & BR_SGRP_F_SEND) {
2193 ent->flags &= ~BR_SGRP_F_SEND;
2194 if (ent->timer.expires > lmqt) {
2195 if (brmctx->multicast_querier &&
2197 !timer_pending(&other_query->timer))
2198 ent->src_query_rexmit_cnt = lmqc;
2199 __grp_src_mod_timer(ent, lmqt);
2204 if (!brmctx->multicast_querier ||
2205 !other_query || timer_pending(&other_query->timer))
2208 __br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr,
2209 &pg->key.addr, true, 1, NULL);
2211 lmi = now + brmctx->multicast_last_member_interval;
2212 if (!timer_pending(&pg->rexmit_timer) ||
2213 time_after(pg->rexmit_timer.expires, lmi))
2214 mod_timer(&pg->rexmit_timer, lmi);
2217 static void __grp_send_query_and_rexmit(struct net_bridge_mcast *brmctx,
2218 struct net_bridge_mcast_port *pmctx,
2219 struct net_bridge_port_group *pg)
2221 struct bridge_mcast_other_query *other_query = NULL;
2222 unsigned long now = jiffies, lmi;
2224 if (!netif_running(brmctx->br->dev) ||
2225 !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED))
2228 if (pg->key.addr.proto == htons(ETH_P_IP))
2229 other_query = &brmctx->ip4_other_query;
2230 #if IS_ENABLED(CONFIG_IPV6)
2232 other_query = &brmctx->ip6_other_query;
2235 if (brmctx->multicast_querier &&
2236 other_query && !timer_pending(&other_query->timer)) {
2237 lmi = now + brmctx->multicast_last_member_interval;
2238 pg->grp_query_rexmit_cnt = brmctx->multicast_last_member_count - 1;
2239 __br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr,
2240 &pg->key.addr, false, 0, NULL);
2241 if (!timer_pending(&pg->rexmit_timer) ||
2242 time_after(pg->rexmit_timer.expires, lmi))
2243 mod_timer(&pg->rexmit_timer, lmi);
2246 if (pg->filter_mode == MCAST_EXCLUDE &&
2247 (!timer_pending(&pg->timer) ||
2248 time_after(pg->timer.expires, now + br_multicast_lmqt(brmctx))))
2249 mod_timer(&pg->timer, now + br_multicast_lmqt(brmctx));
2252 /* State Msg type New state Actions
2253 * INCLUDE (A) IS_IN (B) INCLUDE (A+B) (B)=GMI
2254 * INCLUDE (A) ALLOW (B) INCLUDE (A+B) (B)=GMI
2255 * EXCLUDE (X,Y) ALLOW (A) EXCLUDE (X+A,Y-A) (A)=GMI
2257 static bool br_multicast_isinc_allow(const struct net_bridge_mcast *brmctx,
2258 struct net_bridge_port_group *pg, void *h_addr,
2259 void *srcs, u32 nsrcs, size_t addr_size,
2262 struct net_bridge_group_src *ent;
2263 unsigned long now = jiffies;
2264 bool changed = false;
2265 struct br_ip src_ip;
2268 memset(&src_ip, 0, sizeof(src_ip));
2269 src_ip.proto = pg->key.addr.proto;
2270 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2271 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2272 ent = br_multicast_find_group_src(pg, &src_ip);
2274 ent = br_multicast_new_group_src(pg, &src_ip);
2280 __grp_src_mod_timer(ent, now + br_multicast_gmi(brmctx));
2283 if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2290 /* State Msg type New state Actions
2291 * INCLUDE (A) IS_EX (B) EXCLUDE (A*B,B-A) (B-A)=0
2295 static void __grp_src_isexc_incl(const struct net_bridge_mcast *brmctx,
2296 struct net_bridge_port_group *pg, void *h_addr,
2297 void *srcs, u32 nsrcs, size_t addr_size,
2300 struct net_bridge_group_src *ent;
2301 struct br_ip src_ip;
2304 hlist_for_each_entry(ent, &pg->src_list, node)
2305 ent->flags |= BR_SGRP_F_DELETE;
2307 memset(&src_ip, 0, sizeof(src_ip));
2308 src_ip.proto = pg->key.addr.proto;
2309 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2310 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2311 ent = br_multicast_find_group_src(pg, &src_ip);
2313 ent->flags &= ~BR_SGRP_F_DELETE;
2315 ent = br_multicast_new_group_src(pg, &src_ip);
2317 br_multicast_fwd_src_handle(ent);
2320 br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2323 __grp_src_delete_marked(pg);
2326 /* State Msg type New state Actions
2327 * EXCLUDE (X,Y) IS_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=GMI
2332 static bool __grp_src_isexc_excl(const struct net_bridge_mcast *brmctx,
2333 struct net_bridge_port_group *pg, void *h_addr,
2334 void *srcs, u32 nsrcs, size_t addr_size,
2337 struct net_bridge_group_src *ent;
2338 unsigned long now = jiffies;
2339 bool changed = false;
2340 struct br_ip src_ip;
2343 hlist_for_each_entry(ent, &pg->src_list, node)
2344 ent->flags |= BR_SGRP_F_DELETE;
2346 memset(&src_ip, 0, sizeof(src_ip));
2347 src_ip.proto = pg->key.addr.proto;
2348 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2349 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2350 ent = br_multicast_find_group_src(pg, &src_ip);
2352 ent->flags &= ~BR_SGRP_F_DELETE;
2354 ent = br_multicast_new_group_src(pg, &src_ip);
2356 __grp_src_mod_timer(ent,
2357 now + br_multicast_gmi(brmctx));
2363 if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2367 if (__grp_src_delete_marked(pg))
2373 static bool br_multicast_isexc(const struct net_bridge_mcast *brmctx,
2374 struct net_bridge_port_group *pg, void *h_addr,
2375 void *srcs, u32 nsrcs, size_t addr_size,
2378 bool changed = false;
2380 switch (pg->filter_mode) {
2382 __grp_src_isexc_incl(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2384 br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE);
2388 changed = __grp_src_isexc_excl(brmctx, pg, h_addr, srcs, nsrcs,
2389 addr_size, grec_type);
2393 pg->filter_mode = MCAST_EXCLUDE;
2394 mod_timer(&pg->timer, jiffies + br_multicast_gmi(brmctx));
2399 /* State Msg type New state Actions
2400 * INCLUDE (A) TO_IN (B) INCLUDE (A+B) (B)=GMI
2403 static bool __grp_src_toin_incl(struct net_bridge_mcast *brmctx,
2404 struct net_bridge_mcast_port *pmctx,
2405 struct net_bridge_port_group *pg, void *h_addr,
2406 void *srcs, u32 nsrcs, size_t addr_size,
2409 u32 src_idx, to_send = pg->src_ents;
2410 struct net_bridge_group_src *ent;
2411 unsigned long now = jiffies;
2412 bool changed = false;
2413 struct br_ip src_ip;
2415 hlist_for_each_entry(ent, &pg->src_list, node)
2416 ent->flags |= BR_SGRP_F_SEND;
2418 memset(&src_ip, 0, sizeof(src_ip));
2419 src_ip.proto = pg->key.addr.proto;
2420 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2421 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2422 ent = br_multicast_find_group_src(pg, &src_ip);
2424 ent->flags &= ~BR_SGRP_F_SEND;
2427 ent = br_multicast_new_group_src(pg, &src_ip);
2432 __grp_src_mod_timer(ent, now + br_multicast_gmi(brmctx));
2435 if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2440 __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
2445 /* State Msg type New state Actions
2446 * EXCLUDE (X,Y) TO_IN (A) EXCLUDE (X+A,Y-A) (A)=GMI
2450 static bool __grp_src_toin_excl(struct net_bridge_mcast *brmctx,
2451 struct net_bridge_mcast_port *pmctx,
2452 struct net_bridge_port_group *pg, void *h_addr,
2453 void *srcs, u32 nsrcs, size_t addr_size,
2456 u32 src_idx, to_send = pg->src_ents;
2457 struct net_bridge_group_src *ent;
2458 unsigned long now = jiffies;
2459 bool changed = false;
2460 struct br_ip src_ip;
2462 hlist_for_each_entry(ent, &pg->src_list, node)
2463 if (timer_pending(&ent->timer))
2464 ent->flags |= BR_SGRP_F_SEND;
2466 memset(&src_ip, 0, sizeof(src_ip));
2467 src_ip.proto = pg->key.addr.proto;
2468 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2469 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2470 ent = br_multicast_find_group_src(pg, &src_ip);
2472 if (timer_pending(&ent->timer)) {
2473 ent->flags &= ~BR_SGRP_F_SEND;
2477 ent = br_multicast_new_group_src(pg, &src_ip);
2482 __grp_src_mod_timer(ent, now + br_multicast_gmi(brmctx));
2485 if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2490 __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
2492 __grp_send_query_and_rexmit(brmctx, pmctx, pg);
2497 static bool br_multicast_toin(struct net_bridge_mcast *brmctx,
2498 struct net_bridge_mcast_port *pmctx,
2499 struct net_bridge_port_group *pg, void *h_addr,
2500 void *srcs, u32 nsrcs, size_t addr_size,
2503 bool changed = false;
2505 switch (pg->filter_mode) {
2507 changed = __grp_src_toin_incl(brmctx, pmctx, pg, h_addr, srcs,
2508 nsrcs, addr_size, grec_type);
2511 changed = __grp_src_toin_excl(brmctx, pmctx, pg, h_addr, srcs,
2512 nsrcs, addr_size, grec_type);
2516 if (br_multicast_eht_should_del_pg(pg)) {
2517 pg->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2518 br_multicast_find_del_pg(pg->key.port->br, pg);
2519 /* a notification has already been sent and we shouldn't
2520 * access pg after the delete so we have to return false
2528 /* State Msg type New state Actions
2529 * INCLUDE (A) TO_EX (B) EXCLUDE (A*B,B-A) (B-A)=0
2534 static void __grp_src_toex_incl(struct net_bridge_mcast *brmctx,
2535 struct net_bridge_mcast_port *pmctx,
2536 struct net_bridge_port_group *pg, void *h_addr,
2537 void *srcs, u32 nsrcs, size_t addr_size,
2540 struct net_bridge_group_src *ent;
2541 u32 src_idx, to_send = 0;
2542 struct br_ip src_ip;
2544 hlist_for_each_entry(ent, &pg->src_list, node)
2545 ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE;
2547 memset(&src_ip, 0, sizeof(src_ip));
2548 src_ip.proto = pg->key.addr.proto;
2549 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2550 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2551 ent = br_multicast_find_group_src(pg, &src_ip);
2553 ent->flags = (ent->flags & ~BR_SGRP_F_DELETE) |
2557 ent = br_multicast_new_group_src(pg, &src_ip);
2560 br_multicast_fwd_src_handle(ent);
2563 br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2566 __grp_src_delete_marked(pg);
2568 __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
2571 /* State Msg type New state Actions
2572 * EXCLUDE (X,Y) TO_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=Group Timer
2578 static bool __grp_src_toex_excl(struct net_bridge_mcast *brmctx,
2579 struct net_bridge_mcast_port *pmctx,
2580 struct net_bridge_port_group *pg, void *h_addr,
2581 void *srcs, u32 nsrcs, size_t addr_size,
2584 struct net_bridge_group_src *ent;
2585 u32 src_idx, to_send = 0;
2586 bool changed = false;
2587 struct br_ip src_ip;
2589 hlist_for_each_entry(ent, &pg->src_list, node)
2590 ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE;
2592 memset(&src_ip, 0, sizeof(src_ip));
2593 src_ip.proto = pg->key.addr.proto;
2594 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2595 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2596 ent = br_multicast_find_group_src(pg, &src_ip);
2598 ent->flags &= ~BR_SGRP_F_DELETE;
2600 ent = br_multicast_new_group_src(pg, &src_ip);
2602 __grp_src_mod_timer(ent, pg->timer.expires);
2606 if (ent && timer_pending(&ent->timer)) {
2607 ent->flags |= BR_SGRP_F_SEND;
2612 if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2616 if (__grp_src_delete_marked(pg))
2619 __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
2624 static bool br_multicast_toex(struct net_bridge_mcast *brmctx,
2625 struct net_bridge_mcast_port *pmctx,
2626 struct net_bridge_port_group *pg, void *h_addr,
2627 void *srcs, u32 nsrcs, size_t addr_size,
2630 bool changed = false;
2632 switch (pg->filter_mode) {
2634 __grp_src_toex_incl(brmctx, pmctx, pg, h_addr, srcs, nsrcs,
2635 addr_size, grec_type);
2636 br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE);
2640 changed = __grp_src_toex_excl(brmctx, pmctx, pg, h_addr, srcs,
2641 nsrcs, addr_size, grec_type);
2645 pg->filter_mode = MCAST_EXCLUDE;
2646 mod_timer(&pg->timer, jiffies + br_multicast_gmi(brmctx));
2651 /* State Msg type New state Actions
2652 * INCLUDE (A) BLOCK (B) INCLUDE (A) Send Q(G,A*B)
2654 static bool __grp_src_block_incl(struct net_bridge_mcast *brmctx,
2655 struct net_bridge_mcast_port *pmctx,
2656 struct net_bridge_port_group *pg, void *h_addr,
2657 void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2659 struct net_bridge_group_src *ent;
2660 u32 src_idx, to_send = 0;
2661 bool changed = false;
2662 struct br_ip src_ip;
2664 hlist_for_each_entry(ent, &pg->src_list, node)
2665 ent->flags &= ~BR_SGRP_F_SEND;
2667 memset(&src_ip, 0, sizeof(src_ip));
2668 src_ip.proto = pg->key.addr.proto;
2669 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2670 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2671 ent = br_multicast_find_group_src(pg, &src_ip);
2673 ent->flags |= BR_SGRP_F_SEND;
2678 if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2683 __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
2688 /* State Msg type New state Actions
2689 * EXCLUDE (X,Y) BLOCK (A) EXCLUDE (X+(A-Y),Y) (A-X-Y)=Group Timer
2692 static bool __grp_src_block_excl(struct net_bridge_mcast *brmctx,
2693 struct net_bridge_mcast_port *pmctx,
2694 struct net_bridge_port_group *pg, void *h_addr,
2695 void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2697 struct net_bridge_group_src *ent;
2698 u32 src_idx, to_send = 0;
2699 bool changed = false;
2700 struct br_ip src_ip;
2702 hlist_for_each_entry(ent, &pg->src_list, node)
2703 ent->flags &= ~BR_SGRP_F_SEND;
2705 memset(&src_ip, 0, sizeof(src_ip));
2706 src_ip.proto = pg->key.addr.proto;
2707 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2708 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2709 ent = br_multicast_find_group_src(pg, &src_ip);
2711 ent = br_multicast_new_group_src(pg, &src_ip);
2713 __grp_src_mod_timer(ent, pg->timer.expires);
2717 if (ent && timer_pending(&ent->timer)) {
2718 ent->flags |= BR_SGRP_F_SEND;
2723 if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2728 __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
2733 static bool br_multicast_block(struct net_bridge_mcast *brmctx,
2734 struct net_bridge_mcast_port *pmctx,
2735 struct net_bridge_port_group *pg, void *h_addr,
2736 void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2738 bool changed = false;
2740 switch (pg->filter_mode) {
2742 changed = __grp_src_block_incl(brmctx, pmctx, pg, h_addr, srcs,
2743 nsrcs, addr_size, grec_type);
2746 changed = __grp_src_block_excl(brmctx, pmctx, pg, h_addr, srcs,
2747 nsrcs, addr_size, grec_type);
2751 if ((pg->filter_mode == MCAST_INCLUDE && hlist_empty(&pg->src_list)) ||
2752 br_multicast_eht_should_del_pg(pg)) {
2753 if (br_multicast_eht_should_del_pg(pg))
2754 pg->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2755 br_multicast_find_del_pg(pg->key.port->br, pg);
2756 /* a notification has already been sent and we shouldn't
2757 * access pg after the delete so we have to return false
2765 static struct net_bridge_port_group *
2766 br_multicast_find_port(struct net_bridge_mdb_entry *mp,
2767 struct net_bridge_port *p,
2768 const unsigned char *src)
2770 struct net_bridge *br __maybe_unused = mp->br;
2771 struct net_bridge_port_group *pg;
2773 for (pg = mlock_dereference(mp->ports, br);
2775 pg = mlock_dereference(pg->next, br))
2776 if (br_port_group_equal(pg, p, src))
2782 static int br_ip4_multicast_igmp3_report(struct net_bridge_mcast *brmctx,
2783 struct net_bridge_mcast_port *pmctx,
2784 struct sk_buff *skb,
2787 bool igmpv2 = brmctx->multicast_igmp_version == 2;
2788 struct net_bridge_mdb_entry *mdst;
2789 struct net_bridge_port_group *pg;
2790 const unsigned char *src;
2791 struct igmpv3_report *ih;
2792 struct igmpv3_grec *grec;
2793 int i, len, num, type;
2794 __be32 group, *h_addr;
2795 bool changed = false;
2799 ih = igmpv3_report_hdr(skb);
2800 num = ntohs(ih->ngrec);
2801 len = skb_transport_offset(skb) + sizeof(*ih);
2803 for (i = 0; i < num; i++) {
2804 len += sizeof(*grec);
2805 if (!ip_mc_may_pull(skb, len))
2808 grec = (void *)(skb->data + len - sizeof(*grec));
2809 group = grec->grec_mca;
2810 type = grec->grec_type;
2811 nsrcs = ntohs(grec->grec_nsrcs);
2814 if (!ip_mc_may_pull(skb, len))
2818 case IGMPV3_MODE_IS_INCLUDE:
2819 case IGMPV3_MODE_IS_EXCLUDE:
2820 case IGMPV3_CHANGE_TO_INCLUDE:
2821 case IGMPV3_CHANGE_TO_EXCLUDE:
2822 case IGMPV3_ALLOW_NEW_SOURCES:
2823 case IGMPV3_BLOCK_OLD_SOURCES:
2830 src = eth_hdr(skb)->h_source;
2832 (type == IGMPV3_CHANGE_TO_INCLUDE ||
2833 type == IGMPV3_MODE_IS_INCLUDE)) {
2834 if (!pmctx || igmpv2) {
2835 br_ip4_multicast_leave_group(brmctx, pmctx,
2840 err = br_ip4_multicast_add_group(brmctx, pmctx, group,
2846 if (!pmctx || igmpv2)
2849 spin_lock(&brmctx->br->multicast_lock);
2850 if (!br_multicast_ctx_should_use(brmctx, pmctx))
2851 goto unlock_continue;
2853 mdst = br_mdb_ip4_get(brmctx->br, group, vid);
2855 goto unlock_continue;
2856 pg = br_multicast_find_port(mdst, pmctx->port, src);
2857 if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
2858 goto unlock_continue;
2859 /* reload grec and host addr */
2860 grec = (void *)(skb->data + len - sizeof(*grec) - (nsrcs * 4));
2861 h_addr = &ip_hdr(skb)->saddr;
2863 case IGMPV3_ALLOW_NEW_SOURCES:
2864 changed = br_multicast_isinc_allow(brmctx, pg, h_addr,
2866 nsrcs, sizeof(__be32), type);
2868 case IGMPV3_MODE_IS_INCLUDE:
2869 changed = br_multicast_isinc_allow(brmctx, pg, h_addr,
2871 nsrcs, sizeof(__be32), type);
2873 case IGMPV3_MODE_IS_EXCLUDE:
2874 changed = br_multicast_isexc(brmctx, pg, h_addr,
2876 nsrcs, sizeof(__be32), type);
2878 case IGMPV3_CHANGE_TO_INCLUDE:
2879 changed = br_multicast_toin(brmctx, pmctx, pg, h_addr,
2881 nsrcs, sizeof(__be32), type);
2883 case IGMPV3_CHANGE_TO_EXCLUDE:
2884 changed = br_multicast_toex(brmctx, pmctx, pg, h_addr,
2886 nsrcs, sizeof(__be32), type);
2888 case IGMPV3_BLOCK_OLD_SOURCES:
2889 changed = br_multicast_block(brmctx, pmctx, pg, h_addr,
2891 nsrcs, sizeof(__be32), type);
2895 br_mdb_notify(brmctx->br->dev, mdst, pg, RTM_NEWMDB);
2897 spin_unlock(&brmctx->br->multicast_lock);
2903 #if IS_ENABLED(CONFIG_IPV6)
2904 static int br_ip6_multicast_mld2_report(struct net_bridge_mcast *brmctx,
2905 struct net_bridge_mcast_port *pmctx,
2906 struct sk_buff *skb,
2909 bool mldv1 = brmctx->multicast_mld_version == 1;
2910 struct net_bridge_mdb_entry *mdst;
2911 struct net_bridge_port_group *pg;
2912 unsigned int nsrcs_offset;
2913 struct mld2_report *mld2r;
2914 const unsigned char *src;
2915 struct in6_addr *h_addr;
2916 struct mld2_grec *grec;
2917 unsigned int grec_len;
2918 bool changed = false;
2922 if (!ipv6_mc_may_pull(skb, sizeof(*mld2r)))
2925 mld2r = (struct mld2_report *)icmp6_hdr(skb);
2926 num = ntohs(mld2r->mld2r_ngrec);
2927 len = skb_transport_offset(skb) + sizeof(*mld2r);
2929 for (i = 0; i < num; i++) {
2930 __be16 *_nsrcs, __nsrcs;
2933 nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs);
2935 if (skb_transport_offset(skb) + ipv6_transport_len(skb) <
2936 nsrcs_offset + sizeof(__nsrcs))
2939 _nsrcs = skb_header_pointer(skb, nsrcs_offset,
2940 sizeof(__nsrcs), &__nsrcs);
2944 nsrcs = ntohs(*_nsrcs);
2945 grec_len = struct_size(grec, grec_src, nsrcs);
2947 if (!ipv6_mc_may_pull(skb, len + grec_len))
2950 grec = (struct mld2_grec *)(skb->data + len);
2953 switch (grec->grec_type) {
2954 case MLD2_MODE_IS_INCLUDE:
2955 case MLD2_MODE_IS_EXCLUDE:
2956 case MLD2_CHANGE_TO_INCLUDE:
2957 case MLD2_CHANGE_TO_EXCLUDE:
2958 case MLD2_ALLOW_NEW_SOURCES:
2959 case MLD2_BLOCK_OLD_SOURCES:
2966 src = eth_hdr(skb)->h_source;
2967 if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
2968 grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
2970 if (!pmctx || mldv1) {
2971 br_ip6_multicast_leave_group(brmctx, pmctx,
2977 err = br_ip6_multicast_add_group(brmctx, pmctx,
2978 &grec->grec_mca, vid,
2984 if (!pmctx || mldv1)
2987 spin_lock(&brmctx->br->multicast_lock);
2988 if (!br_multicast_ctx_should_use(brmctx, pmctx))
2989 goto unlock_continue;
2991 mdst = br_mdb_ip6_get(brmctx->br, &grec->grec_mca, vid);
2993 goto unlock_continue;
2994 pg = br_multicast_find_port(mdst, pmctx->port, src);
2995 if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
2996 goto unlock_continue;
2997 h_addr = &ipv6_hdr(skb)->saddr;
2998 switch (grec->grec_type) {
2999 case MLD2_ALLOW_NEW_SOURCES:
3000 changed = br_multicast_isinc_allow(brmctx, pg, h_addr,
3001 grec->grec_src, nsrcs,
3002 sizeof(struct in6_addr),
3005 case MLD2_MODE_IS_INCLUDE:
3006 changed = br_multicast_isinc_allow(brmctx, pg, h_addr,
3007 grec->grec_src, nsrcs,
3008 sizeof(struct in6_addr),
3011 case MLD2_MODE_IS_EXCLUDE:
3012 changed = br_multicast_isexc(brmctx, pg, h_addr,
3013 grec->grec_src, nsrcs,
3014 sizeof(struct in6_addr),
3017 case MLD2_CHANGE_TO_INCLUDE:
3018 changed = br_multicast_toin(brmctx, pmctx, pg, h_addr,
3019 grec->grec_src, nsrcs,
3020 sizeof(struct in6_addr),
3023 case MLD2_CHANGE_TO_EXCLUDE:
3024 changed = br_multicast_toex(brmctx, pmctx, pg, h_addr,
3025 grec->grec_src, nsrcs,
3026 sizeof(struct in6_addr),
3029 case MLD2_BLOCK_OLD_SOURCES:
3030 changed = br_multicast_block(brmctx, pmctx, pg, h_addr,
3031 grec->grec_src, nsrcs,
3032 sizeof(struct in6_addr),
3037 br_mdb_notify(brmctx->br->dev, mdst, pg, RTM_NEWMDB);
3039 spin_unlock(&brmctx->br->multicast_lock);
3046 static bool br_multicast_select_querier(struct net_bridge_mcast *brmctx,
3047 struct net_bridge_mcast_port *pmctx,
3048 struct br_ip *saddr)
3050 int port_ifidx = pmctx ? pmctx->port->dev->ifindex : 0;
3051 struct timer_list *own_timer, *other_timer;
3052 struct bridge_mcast_querier *querier;
3054 switch (saddr->proto) {
3055 case htons(ETH_P_IP):
3056 querier = &brmctx->ip4_querier;
3057 own_timer = &brmctx->ip4_own_query.timer;
3058 other_timer = &brmctx->ip4_other_query.timer;
3059 if (!querier->addr.src.ip4 ||
3060 ntohl(saddr->src.ip4) <= ntohl(querier->addr.src.ip4))
3063 #if IS_ENABLED(CONFIG_IPV6)
3064 case htons(ETH_P_IPV6):
3065 querier = &brmctx->ip6_querier;
3066 own_timer = &brmctx->ip6_own_query.timer;
3067 other_timer = &brmctx->ip6_other_query.timer;
3068 if (ipv6_addr_cmp(&saddr->src.ip6, &querier->addr.src.ip6) <= 0)
3076 if (!timer_pending(own_timer) && !timer_pending(other_timer))
3082 br_multicast_update_querier(brmctx, querier, port_ifidx, saddr);
3087 static struct net_bridge_port *
3088 __br_multicast_get_querier_port(struct net_bridge *br,
3089 const struct bridge_mcast_querier *querier)
3091 int port_ifidx = READ_ONCE(querier->port_ifidx);
3092 struct net_bridge_port *p;
3093 struct net_device *dev;
3095 if (port_ifidx == 0)
3098 dev = dev_get_by_index_rcu(dev_net(br->dev), port_ifidx);
3101 p = br_port_get_rtnl_rcu(dev);
3102 if (!p || p->br != br)
3108 size_t br_multicast_querier_state_size(void)
3110 return nla_total_size(0) + /* nest attribute */
3111 nla_total_size(sizeof(__be32)) + /* BRIDGE_QUERIER_IP_ADDRESS */
3112 nla_total_size(sizeof(int)) + /* BRIDGE_QUERIER_IP_PORT */
3113 nla_total_size_64bit(sizeof(u64)) + /* BRIDGE_QUERIER_IP_OTHER_TIMER */
3114 #if IS_ENABLED(CONFIG_IPV6)
3115 nla_total_size(sizeof(struct in6_addr)) + /* BRIDGE_QUERIER_IPV6_ADDRESS */
3116 nla_total_size(sizeof(int)) + /* BRIDGE_QUERIER_IPV6_PORT */
3117 nla_total_size_64bit(sizeof(u64)) + /* BRIDGE_QUERIER_IPV6_OTHER_TIMER */
3122 /* protected by rtnl or rcu */
3123 int br_multicast_dump_querier_state(struct sk_buff *skb,
3124 const struct net_bridge_mcast *brmctx,
3127 struct bridge_mcast_querier querier = {};
3128 struct net_bridge_port *p;
3129 struct nlattr *nest;
3131 if (!br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED) ||
3132 br_multicast_ctx_vlan_global_disabled(brmctx))
3135 nest = nla_nest_start(skb, nest_attr);
3140 if (!brmctx->multicast_querier &&
3141 !timer_pending(&brmctx->ip4_other_query.timer))
3144 br_multicast_read_querier(&brmctx->ip4_querier, &querier);
3145 if (nla_put_in_addr(skb, BRIDGE_QUERIER_IP_ADDRESS,
3146 querier.addr.src.ip4)) {
3151 p = __br_multicast_get_querier_port(brmctx->br, &querier);
3152 if (timer_pending(&brmctx->ip4_other_query.timer) &&
3153 (nla_put_u64_64bit(skb, BRIDGE_QUERIER_IP_OTHER_TIMER,
3154 br_timer_value(&brmctx->ip4_other_query.timer),
3155 BRIDGE_QUERIER_PAD) ||
3156 (p && nla_put_u32(skb, BRIDGE_QUERIER_IP_PORT, p->dev->ifindex)))) {
3162 #if IS_ENABLED(CONFIG_IPV6)
3163 if (!brmctx->multicast_querier &&
3164 !timer_pending(&brmctx->ip6_other_query.timer))
3167 br_multicast_read_querier(&brmctx->ip6_querier, &querier);
3168 if (nla_put_in6_addr(skb, BRIDGE_QUERIER_IPV6_ADDRESS,
3169 &querier.addr.src.ip6)) {
3174 p = __br_multicast_get_querier_port(brmctx->br, &querier);
3175 if (timer_pending(&brmctx->ip6_other_query.timer) &&
3176 (nla_put_u64_64bit(skb, BRIDGE_QUERIER_IPV6_OTHER_TIMER,
3177 br_timer_value(&brmctx->ip6_other_query.timer),
3178 BRIDGE_QUERIER_PAD) ||
3179 (p && nla_put_u32(skb, BRIDGE_QUERIER_IPV6_PORT,
3180 p->dev->ifindex)))) {
3187 nla_nest_end(skb, nest);
3189 nla_nest_cancel(skb, nest);
3194 nla_nest_cancel(skb, nest);
3199 br_multicast_update_query_timer(struct net_bridge_mcast *brmctx,
3200 struct bridge_mcast_other_query *query,
3201 unsigned long max_delay)
3203 if (!timer_pending(&query->timer))
3204 mod_timer(&query->delay_timer, jiffies + max_delay);
3206 mod_timer(&query->timer, jiffies + brmctx->multicast_querier_interval);
3209 static void br_port_mc_router_state_change(struct net_bridge_port *p,
3212 struct switchdev_attr attr = {
3214 .id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
3215 .flags = SWITCHDEV_F_DEFER,
3216 .u.mrouter = is_mc_router,
3219 switchdev_port_attr_set(p->dev, &attr, NULL);
3222 static struct net_bridge_port *
3223 br_multicast_rport_from_node(struct net_bridge_mcast *brmctx,
3224 struct hlist_head *mc_router_list,
3225 struct hlist_node *rlist)
3227 struct net_bridge_mcast_port *pmctx;
3229 #if IS_ENABLED(CONFIG_IPV6)
3230 if (mc_router_list == &brmctx->ip6_mc_router_list)
3231 pmctx = hlist_entry(rlist, struct net_bridge_mcast_port,
3235 pmctx = hlist_entry(rlist, struct net_bridge_mcast_port,
3241 static struct hlist_node *
3242 br_multicast_get_rport_slot(struct net_bridge_mcast *brmctx,
3243 struct net_bridge_port *port,
3244 struct hlist_head *mc_router_list)
3247 struct hlist_node *slot = NULL;
3248 struct net_bridge_port *p;
3249 struct hlist_node *rlist;
3251 hlist_for_each(rlist, mc_router_list) {
3252 p = br_multicast_rport_from_node(brmctx, mc_router_list, rlist);
3254 if ((unsigned long)port >= (unsigned long)p)
3263 static bool br_multicast_no_router_otherpf(struct net_bridge_mcast_port *pmctx,
3264 struct hlist_node *rnode)
3266 #if IS_ENABLED(CONFIG_IPV6)
3267 if (rnode != &pmctx->ip6_rlist)
3268 return hlist_unhashed(&pmctx->ip6_rlist);
3270 return hlist_unhashed(&pmctx->ip4_rlist);
3276 /* Add port to router_list
3277 * list is maintained ordered by pointer value
3278 * and locked by br->multicast_lock and RCU
3280 static void br_multicast_add_router(struct net_bridge_mcast *brmctx,
3281 struct net_bridge_mcast_port *pmctx,
3282 struct hlist_node *rlist,
3283 struct hlist_head *mc_router_list)
3285 struct hlist_node *slot;
3287 if (!hlist_unhashed(rlist))
3290 slot = br_multicast_get_rport_slot(brmctx, pmctx->port, mc_router_list);
3293 hlist_add_behind_rcu(rlist, slot);
3295 hlist_add_head_rcu(rlist, mc_router_list);
3297 /* For backwards compatibility for now, only notify if we
3298 * switched from no IPv4/IPv6 multicast router to a new
3299 * IPv4 or IPv6 multicast router.
3301 if (br_multicast_no_router_otherpf(pmctx, rlist)) {
3302 br_rtr_notify(pmctx->port->br->dev, pmctx, RTM_NEWMDB);
3303 br_port_mc_router_state_change(pmctx->port, true);
3307 /* Add port to router_list
3308 * list is maintained ordered by pointer value
3309 * and locked by br->multicast_lock and RCU
3311 static void br_ip4_multicast_add_router(struct net_bridge_mcast *brmctx,
3312 struct net_bridge_mcast_port *pmctx)
3314 br_multicast_add_router(brmctx, pmctx, &pmctx->ip4_rlist,
3315 &brmctx->ip4_mc_router_list);
3318 /* Add port to router_list
3319 * list is maintained ordered by pointer value
3320 * and locked by br->multicast_lock and RCU
3322 static void br_ip6_multicast_add_router(struct net_bridge_mcast *brmctx,
3323 struct net_bridge_mcast_port *pmctx)
3325 #if IS_ENABLED(CONFIG_IPV6)
3326 br_multicast_add_router(brmctx, pmctx, &pmctx->ip6_rlist,
3327 &brmctx->ip6_mc_router_list);
3331 static void br_multicast_mark_router(struct net_bridge_mcast *brmctx,
3332 struct net_bridge_mcast_port *pmctx,
3333 struct timer_list *timer,
3334 struct hlist_node *rlist,
3335 struct hlist_head *mc_router_list)
3337 unsigned long now = jiffies;
3339 if (!br_multicast_ctx_should_use(brmctx, pmctx))
3343 if (brmctx->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
3344 if (!br_ip4_multicast_is_router(brmctx) &&
3345 !br_ip6_multicast_is_router(brmctx))
3346 br_mc_router_state_change(brmctx->br, true);
3347 mod_timer(timer, now + brmctx->multicast_querier_interval);
3352 if (pmctx->multicast_router == MDB_RTR_TYPE_DISABLED ||
3353 pmctx->multicast_router == MDB_RTR_TYPE_PERM)
3356 br_multicast_add_router(brmctx, pmctx, rlist, mc_router_list);
3357 mod_timer(timer, now + brmctx->multicast_querier_interval);
3360 static void br_ip4_multicast_mark_router(struct net_bridge_mcast *brmctx,
3361 struct net_bridge_mcast_port *pmctx)
3363 struct timer_list *timer = &brmctx->ip4_mc_router_timer;
3364 struct hlist_node *rlist = NULL;
3367 timer = &pmctx->ip4_mc_router_timer;
3368 rlist = &pmctx->ip4_rlist;
3371 br_multicast_mark_router(brmctx, pmctx, timer, rlist,
3372 &brmctx->ip4_mc_router_list);
3375 static void br_ip6_multicast_mark_router(struct net_bridge_mcast *brmctx,
3376 struct net_bridge_mcast_port *pmctx)
3378 #if IS_ENABLED(CONFIG_IPV6)
3379 struct timer_list *timer = &brmctx->ip6_mc_router_timer;
3380 struct hlist_node *rlist = NULL;
3383 timer = &pmctx->ip6_mc_router_timer;
3384 rlist = &pmctx->ip6_rlist;
3387 br_multicast_mark_router(brmctx, pmctx, timer, rlist,
3388 &brmctx->ip6_mc_router_list);
3393 br_ip4_multicast_query_received(struct net_bridge_mcast *brmctx,
3394 struct net_bridge_mcast_port *pmctx,
3395 struct bridge_mcast_other_query *query,
3396 struct br_ip *saddr,
3397 unsigned long max_delay)
3399 if (!br_multicast_select_querier(brmctx, pmctx, saddr))
3402 br_multicast_update_query_timer(brmctx, query, max_delay);
3403 br_ip4_multicast_mark_router(brmctx, pmctx);
3406 #if IS_ENABLED(CONFIG_IPV6)
3408 br_ip6_multicast_query_received(struct net_bridge_mcast *brmctx,
3409 struct net_bridge_mcast_port *pmctx,
3410 struct bridge_mcast_other_query *query,
3411 struct br_ip *saddr,
3412 unsigned long max_delay)
3414 if (!br_multicast_select_querier(brmctx, pmctx, saddr))
3417 br_multicast_update_query_timer(brmctx, query, max_delay);
3418 br_ip6_multicast_mark_router(brmctx, pmctx);
3422 static void br_ip4_multicast_query(struct net_bridge_mcast *brmctx,
3423 struct net_bridge_mcast_port *pmctx,
3424 struct sk_buff *skb,
3427 unsigned int transport_len = ip_transport_len(skb);
3428 const struct iphdr *iph = ip_hdr(skb);
3429 struct igmphdr *ih = igmp_hdr(skb);
3430 struct net_bridge_mdb_entry *mp;
3431 struct igmpv3_query *ih3;
3432 struct net_bridge_port_group *p;
3433 struct net_bridge_port_group __rcu **pp;
3434 struct br_ip saddr = {};
3435 unsigned long max_delay;
3436 unsigned long now = jiffies;
3439 spin_lock(&brmctx->br->multicast_lock);
3440 if (!br_multicast_ctx_should_use(brmctx, pmctx))
3445 if (transport_len == sizeof(*ih)) {
3446 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
3449 max_delay = 10 * HZ;
3452 } else if (transport_len >= sizeof(*ih3)) {
3453 ih3 = igmpv3_query_hdr(skb);
3455 (brmctx->multicast_igmp_version == 3 && group &&
3459 max_delay = ih3->code ?
3460 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
3466 saddr.proto = htons(ETH_P_IP);
3467 saddr.src.ip4 = iph->saddr;
3469 br_ip4_multicast_query_received(brmctx, pmctx,
3470 &brmctx->ip4_other_query,
3475 mp = br_mdb_ip4_get(brmctx->br, group, vid);
3479 max_delay *= brmctx->multicast_last_member_count;
3481 if (mp->host_joined &&
3482 (timer_pending(&mp->timer) ?
3483 time_after(mp->timer.expires, now + max_delay) :
3484 try_to_del_timer_sync(&mp->timer) >= 0))
3485 mod_timer(&mp->timer, now + max_delay);
3487 for (pp = &mp->ports;
3488 (p = mlock_dereference(*pp, brmctx->br)) != NULL;
3490 if (timer_pending(&p->timer) ?
3491 time_after(p->timer.expires, now + max_delay) :
3492 try_to_del_timer_sync(&p->timer) >= 0 &&
3493 (brmctx->multicast_igmp_version == 2 ||
3494 p->filter_mode == MCAST_EXCLUDE))
3495 mod_timer(&p->timer, now + max_delay);
3499 spin_unlock(&brmctx->br->multicast_lock);
3502 #if IS_ENABLED(CONFIG_IPV6)
3503 static int br_ip6_multicast_query(struct net_bridge_mcast *brmctx,
3504 struct net_bridge_mcast_port *pmctx,
3505 struct sk_buff *skb,
3508 unsigned int transport_len = ipv6_transport_len(skb);
3509 struct mld_msg *mld;
3510 struct net_bridge_mdb_entry *mp;
3511 struct mld2_query *mld2q;
3512 struct net_bridge_port_group *p;
3513 struct net_bridge_port_group __rcu **pp;
3514 struct br_ip saddr = {};
3515 unsigned long max_delay;
3516 unsigned long now = jiffies;
3517 unsigned int offset = skb_transport_offset(skb);
3518 const struct in6_addr *group = NULL;
3519 bool is_general_query;
3522 spin_lock(&brmctx->br->multicast_lock);
3523 if (!br_multicast_ctx_should_use(brmctx, pmctx))
3526 if (transport_len == sizeof(*mld)) {
3527 if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
3531 mld = (struct mld_msg *) icmp6_hdr(skb);
3532 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
3534 group = &mld->mld_mca;
3536 if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
3540 mld2q = (struct mld2_query *)icmp6_hdr(skb);
3541 if (!mld2q->mld2q_nsrcs)
3542 group = &mld2q->mld2q_mca;
3543 if (brmctx->multicast_mld_version == 2 &&
3544 !ipv6_addr_any(&mld2q->mld2q_mca) &&
3545 mld2q->mld2q_suppress)
3548 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
3551 is_general_query = group && ipv6_addr_any(group);
3553 if (is_general_query) {
3554 saddr.proto = htons(ETH_P_IPV6);
3555 saddr.src.ip6 = ipv6_hdr(skb)->saddr;
3557 br_ip6_multicast_query_received(brmctx, pmctx,
3558 &brmctx->ip6_other_query,
3561 } else if (!group) {
3565 mp = br_mdb_ip6_get(brmctx->br, group, vid);
3569 max_delay *= brmctx->multicast_last_member_count;
3570 if (mp->host_joined &&
3571 (timer_pending(&mp->timer) ?
3572 time_after(mp->timer.expires, now + max_delay) :
3573 try_to_del_timer_sync(&mp->timer) >= 0))
3574 mod_timer(&mp->timer, now + max_delay);
3576 for (pp = &mp->ports;
3577 (p = mlock_dereference(*pp, brmctx->br)) != NULL;
3579 if (timer_pending(&p->timer) ?
3580 time_after(p->timer.expires, now + max_delay) :
3581 try_to_del_timer_sync(&p->timer) >= 0 &&
3582 (brmctx->multicast_mld_version == 1 ||
3583 p->filter_mode == MCAST_EXCLUDE))
3584 mod_timer(&p->timer, now + max_delay);
3588 spin_unlock(&brmctx->br->multicast_lock);
3594 br_multicast_leave_group(struct net_bridge_mcast *brmctx,
3595 struct net_bridge_mcast_port *pmctx,
3596 struct br_ip *group,
3597 struct bridge_mcast_other_query *other_query,
3598 struct bridge_mcast_own_query *own_query,
3599 const unsigned char *src)
3601 struct net_bridge_mdb_entry *mp;
3602 struct net_bridge_port_group *p;
3606 spin_lock(&brmctx->br->multicast_lock);
3607 if (!br_multicast_ctx_should_use(brmctx, pmctx))
3610 mp = br_mdb_ip_get(brmctx->br, group);
3614 if (pmctx && (pmctx->port->flags & BR_MULTICAST_FAST_LEAVE)) {
3615 struct net_bridge_port_group __rcu **pp;
3617 for (pp = &mp->ports;
3618 (p = mlock_dereference(*pp, brmctx->br)) != NULL;
3620 if (!br_port_group_equal(p, pmctx->port, src))
3623 if (p->flags & MDB_PG_FLAGS_PERMANENT)
3626 p->flags |= MDB_PG_FLAGS_FAST_LEAVE;
3627 br_multicast_del_pg(mp, p, pp);
3632 if (timer_pending(&other_query->timer))
3635 if (brmctx->multicast_querier) {
3636 __br_multicast_send_query(brmctx, pmctx, NULL, NULL, &mp->addr,
3639 time = jiffies + brmctx->multicast_last_member_count *
3640 brmctx->multicast_last_member_interval;
3642 mod_timer(&own_query->timer, time);
3644 for (p = mlock_dereference(mp->ports, brmctx->br);
3645 p != NULL && pmctx != NULL;
3646 p = mlock_dereference(p->next, brmctx->br)) {
3647 if (!br_port_group_equal(p, pmctx->port, src))
3650 if (!hlist_unhashed(&p->mglist) &&
3651 (timer_pending(&p->timer) ?
3652 time_after(p->timer.expires, time) :
3653 try_to_del_timer_sync(&p->timer) >= 0)) {
3654 mod_timer(&p->timer, time);
3662 time = now + brmctx->multicast_last_member_count *
3663 brmctx->multicast_last_member_interval;
3666 if (mp->host_joined &&
3667 (timer_pending(&mp->timer) ?
3668 time_after(mp->timer.expires, time) :
3669 try_to_del_timer_sync(&mp->timer) >= 0)) {
3670 mod_timer(&mp->timer, time);
3676 for (p = mlock_dereference(mp->ports, brmctx->br);
3678 p = mlock_dereference(p->next, brmctx->br)) {
3679 if (p->key.port != pmctx->port)
3682 if (!hlist_unhashed(&p->mglist) &&
3683 (timer_pending(&p->timer) ?
3684 time_after(p->timer.expires, time) :
3685 try_to_del_timer_sync(&p->timer) >= 0)) {
3686 mod_timer(&p->timer, time);
3692 spin_unlock(&brmctx->br->multicast_lock);
3695 static void br_ip4_multicast_leave_group(struct net_bridge_mcast *brmctx,
3696 struct net_bridge_mcast_port *pmctx,
3699 const unsigned char *src)
3701 struct br_ip br_group;
3702 struct bridge_mcast_own_query *own_query;
3704 if (ipv4_is_local_multicast(group))
3707 own_query = pmctx ? &pmctx->ip4_own_query : &brmctx->ip4_own_query;
3709 memset(&br_group, 0, sizeof(br_group));
3710 br_group.dst.ip4 = group;
3711 br_group.proto = htons(ETH_P_IP);
3714 br_multicast_leave_group(brmctx, pmctx, &br_group,
3715 &brmctx->ip4_other_query,
3719 #if IS_ENABLED(CONFIG_IPV6)
3720 static void br_ip6_multicast_leave_group(struct net_bridge_mcast *brmctx,
3721 struct net_bridge_mcast_port *pmctx,
3722 const struct in6_addr *group,
3724 const unsigned char *src)
3726 struct br_ip br_group;
3727 struct bridge_mcast_own_query *own_query;
3729 if (ipv6_addr_is_ll_all_nodes(group))
3732 own_query = pmctx ? &pmctx->ip6_own_query : &brmctx->ip6_own_query;
3734 memset(&br_group, 0, sizeof(br_group));
3735 br_group.dst.ip6 = *group;
3736 br_group.proto = htons(ETH_P_IPV6);
3739 br_multicast_leave_group(brmctx, pmctx, &br_group,
3740 &brmctx->ip6_other_query,
3745 static void br_multicast_err_count(const struct net_bridge *br,
3746 const struct net_bridge_port *p,
3749 struct bridge_mcast_stats __percpu *stats;
3750 struct bridge_mcast_stats *pstats;
3752 if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
3756 stats = p->mcast_stats;
3758 stats = br->mcast_stats;
3759 if (WARN_ON(!stats))
3762 pstats = this_cpu_ptr(stats);
3764 u64_stats_update_begin(&pstats->syncp);
3766 case htons(ETH_P_IP):
3767 pstats->mstats.igmp_parse_errors++;
3769 #if IS_ENABLED(CONFIG_IPV6)
3770 case htons(ETH_P_IPV6):
3771 pstats->mstats.mld_parse_errors++;
3775 u64_stats_update_end(&pstats->syncp);
3778 static void br_multicast_pim(struct net_bridge_mcast *brmctx,
3779 struct net_bridge_mcast_port *pmctx,
3780 const struct sk_buff *skb)
3782 unsigned int offset = skb_transport_offset(skb);
3783 struct pimhdr *pimhdr, _pimhdr;
3785 pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
3786 if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
3787 pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
3790 spin_lock(&brmctx->br->multicast_lock);
3791 br_ip4_multicast_mark_router(brmctx, pmctx);
3792 spin_unlock(&brmctx->br->multicast_lock);
3795 static int br_ip4_multicast_mrd_rcv(struct net_bridge_mcast *brmctx,
3796 struct net_bridge_mcast_port *pmctx,
3797 struct sk_buff *skb)
3799 if (ip_hdr(skb)->protocol != IPPROTO_IGMP ||
3800 igmp_hdr(skb)->type != IGMP_MRDISC_ADV)
3803 spin_lock(&brmctx->br->multicast_lock);
3804 br_ip4_multicast_mark_router(brmctx, pmctx);
3805 spin_unlock(&brmctx->br->multicast_lock);
3810 static int br_multicast_ipv4_rcv(struct net_bridge_mcast *brmctx,
3811 struct net_bridge_mcast_port *pmctx,
3812 struct sk_buff *skb,
3815 struct net_bridge_port *p = pmctx ? pmctx->port : NULL;
3816 const unsigned char *src;
3820 err = ip_mc_check_igmp(skb);
3822 if (err == -ENOMSG) {
3823 if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
3824 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
3825 } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
3826 if (ip_hdr(skb)->protocol == IPPROTO_PIM)
3827 br_multicast_pim(brmctx, pmctx, skb);
3828 } else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) {
3829 br_ip4_multicast_mrd_rcv(brmctx, pmctx, skb);
3833 } else if (err < 0) {
3834 br_multicast_err_count(brmctx->br, p, skb->protocol);
3839 src = eth_hdr(skb)->h_source;
3840 BR_INPUT_SKB_CB(skb)->igmp = ih->type;
3843 case IGMP_HOST_MEMBERSHIP_REPORT:
3844 case IGMPV2_HOST_MEMBERSHIP_REPORT:
3845 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
3846 err = br_ip4_multicast_add_group(brmctx, pmctx, ih->group, vid,
3849 case IGMPV3_HOST_MEMBERSHIP_REPORT:
3850 err = br_ip4_multicast_igmp3_report(brmctx, pmctx, skb, vid);
3852 case IGMP_HOST_MEMBERSHIP_QUERY:
3853 br_ip4_multicast_query(brmctx, pmctx, skb, vid);
3855 case IGMP_HOST_LEAVE_MESSAGE:
3856 br_ip4_multicast_leave_group(brmctx, pmctx, ih->group, vid, src);
3860 br_multicast_count(brmctx->br, p, skb, BR_INPUT_SKB_CB(skb)->igmp,
3866 #if IS_ENABLED(CONFIG_IPV6)
3867 static void br_ip6_multicast_mrd_rcv(struct net_bridge_mcast *brmctx,
3868 struct net_bridge_mcast_port *pmctx,
3869 struct sk_buff *skb)
3871 if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV)
3874 spin_lock(&brmctx->br->multicast_lock);
3875 br_ip6_multicast_mark_router(brmctx, pmctx);
3876 spin_unlock(&brmctx->br->multicast_lock);
3879 static int br_multicast_ipv6_rcv(struct net_bridge_mcast *brmctx,
3880 struct net_bridge_mcast_port *pmctx,
3881 struct sk_buff *skb,
3884 struct net_bridge_port *p = pmctx ? pmctx->port : NULL;
3885 const unsigned char *src;
3886 struct mld_msg *mld;
3889 err = ipv6_mc_check_mld(skb);
3891 if (err == -ENOMSG || err == -ENODATA) {
3892 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
3893 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
3894 if (err == -ENODATA &&
3895 ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr))
3896 br_ip6_multicast_mrd_rcv(brmctx, pmctx, skb);
3899 } else if (err < 0) {
3900 br_multicast_err_count(brmctx->br, p, skb->protocol);
3904 mld = (struct mld_msg *)skb_transport_header(skb);
3905 BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
3907 switch (mld->mld_type) {
3908 case ICMPV6_MGM_REPORT:
3909 src = eth_hdr(skb)->h_source;
3910 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
3911 err = br_ip6_multicast_add_group(brmctx, pmctx, &mld->mld_mca,
3914 case ICMPV6_MLD2_REPORT:
3915 err = br_ip6_multicast_mld2_report(brmctx, pmctx, skb, vid);
3917 case ICMPV6_MGM_QUERY:
3918 err = br_ip6_multicast_query(brmctx, pmctx, skb, vid);
3920 case ICMPV6_MGM_REDUCTION:
3921 src = eth_hdr(skb)->h_source;
3922 br_ip6_multicast_leave_group(brmctx, pmctx, &mld->mld_mca, vid,
3927 br_multicast_count(brmctx->br, p, skb, BR_INPUT_SKB_CB(skb)->igmp,
3934 int br_multicast_rcv(struct net_bridge_mcast **brmctx,
3935 struct net_bridge_mcast_port **pmctx,
3936 struct net_bridge_vlan *vlan,
3937 struct sk_buff *skb, u16 vid)
3941 BR_INPUT_SKB_CB(skb)->igmp = 0;
3942 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
3944 if (!br_opt_get((*brmctx)->br, BROPT_MULTICAST_ENABLED))
3947 if (br_opt_get((*brmctx)->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) && vlan) {
3948 const struct net_bridge_vlan *masterv;
3950 /* the vlan has the master flag set only when transmitting
3951 * through the bridge device
3953 if (br_vlan_is_master(vlan)) {
3955 *brmctx = &vlan->br_mcast_ctx;
3958 masterv = vlan->brvlan;
3959 *brmctx = &vlan->brvlan->br_mcast_ctx;
3960 *pmctx = &vlan->port_mcast_ctx;
3963 if (!(masterv->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED))
3967 switch (skb->protocol) {
3968 case htons(ETH_P_IP):
3969 ret = br_multicast_ipv4_rcv(*brmctx, *pmctx, skb, vid);
3971 #if IS_ENABLED(CONFIG_IPV6)
3972 case htons(ETH_P_IPV6):
3973 ret = br_multicast_ipv6_rcv(*brmctx, *pmctx, skb, vid);
3981 static void br_multicast_query_expired(struct net_bridge_mcast *brmctx,
3982 struct bridge_mcast_own_query *query,
3983 struct bridge_mcast_querier *querier)
3985 spin_lock(&brmctx->br->multicast_lock);
3986 if (br_multicast_ctx_vlan_disabled(brmctx))
3989 if (query->startup_sent < brmctx->multicast_startup_query_count)
3990 query->startup_sent++;
3992 br_multicast_send_query(brmctx, NULL, query);
3994 spin_unlock(&brmctx->br->multicast_lock);
3997 static void br_ip4_multicast_query_expired(struct timer_list *t)
3999 struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
4000 ip4_own_query.timer);
4002 br_multicast_query_expired(brmctx, &brmctx->ip4_own_query,
4003 &brmctx->ip4_querier);
4006 #if IS_ENABLED(CONFIG_IPV6)
4007 static void br_ip6_multicast_query_expired(struct timer_list *t)
4009 struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
4010 ip6_own_query.timer);
4012 br_multicast_query_expired(brmctx, &brmctx->ip6_own_query,
4013 &brmctx->ip6_querier);
4017 static void br_multicast_gc_work(struct work_struct *work)
4019 struct net_bridge *br = container_of(work, struct net_bridge,
4021 HLIST_HEAD(deleted_head);
4023 spin_lock_bh(&br->multicast_lock);
4024 hlist_move_list(&br->mcast_gc_list, &deleted_head);
4025 spin_unlock_bh(&br->multicast_lock);
4027 br_multicast_gc(&deleted_head);
4030 void br_multicast_ctx_init(struct net_bridge *br,
4031 struct net_bridge_vlan *vlan,
4032 struct net_bridge_mcast *brmctx)
4035 brmctx->vlan = vlan;
4036 brmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
4037 brmctx->multicast_last_member_count = 2;
4038 brmctx->multicast_startup_query_count = 2;
4040 brmctx->multicast_last_member_interval = HZ;
4041 brmctx->multicast_query_response_interval = 10 * HZ;
4042 brmctx->multicast_startup_query_interval = 125 * HZ / 4;
4043 brmctx->multicast_query_interval = 125 * HZ;
4044 brmctx->multicast_querier_interval = 255 * HZ;
4045 brmctx->multicast_membership_interval = 260 * HZ;
4047 brmctx->ip4_querier.port_ifidx = 0;
4048 seqcount_spinlock_init(&brmctx->ip4_querier.seq, &br->multicast_lock);
4049 brmctx->multicast_igmp_version = 2;
4050 #if IS_ENABLED(CONFIG_IPV6)
4051 brmctx->multicast_mld_version = 1;
4052 brmctx->ip6_querier.port_ifidx = 0;
4053 seqcount_spinlock_init(&brmctx->ip6_querier.seq, &br->multicast_lock);
4056 timer_setup(&brmctx->ip4_mc_router_timer,
4057 br_ip4_multicast_local_router_expired, 0);
4058 timer_setup(&brmctx->ip4_other_query.timer,
4059 br_ip4_multicast_querier_expired, 0);
4060 timer_setup(&brmctx->ip4_other_query.delay_timer,
4061 br_multicast_query_delay_expired, 0);
4062 timer_setup(&brmctx->ip4_own_query.timer,
4063 br_ip4_multicast_query_expired, 0);
4064 #if IS_ENABLED(CONFIG_IPV6)
4065 timer_setup(&brmctx->ip6_mc_router_timer,
4066 br_ip6_multicast_local_router_expired, 0);
4067 timer_setup(&brmctx->ip6_other_query.timer,
4068 br_ip6_multicast_querier_expired, 0);
4069 timer_setup(&brmctx->ip6_other_query.delay_timer,
4070 br_multicast_query_delay_expired, 0);
4071 timer_setup(&brmctx->ip6_own_query.timer,
4072 br_ip6_multicast_query_expired, 0);
4076 void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx)
4078 __br_multicast_stop(brmctx);
4081 void br_multicast_init(struct net_bridge *br)
4083 br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
4085 br_multicast_ctx_init(br, NULL, &br->multicast_ctx);
4087 br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true);
4088 br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
4090 spin_lock_init(&br->multicast_lock);
4091 INIT_HLIST_HEAD(&br->mdb_list);
4092 INIT_HLIST_HEAD(&br->mcast_gc_list);
4093 INIT_WORK(&br->mcast_gc_work, br_multicast_gc_work);
4096 static void br_ip4_multicast_join_snoopers(struct net_bridge *br)
4098 struct in_device *in_dev = in_dev_get(br->dev);
4103 __ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
4107 #if IS_ENABLED(CONFIG_IPV6)
4108 static void br_ip6_multicast_join_snoopers(struct net_bridge *br)
4110 struct in6_addr addr;
4112 ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
4113 ipv6_dev_mc_inc(br->dev, &addr);
4116 static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br)
4121 void br_multicast_join_snoopers(struct net_bridge *br)
4123 br_ip4_multicast_join_snoopers(br);
4124 br_ip6_multicast_join_snoopers(br);
4127 static void br_ip4_multicast_leave_snoopers(struct net_bridge *br)
4129 struct in_device *in_dev = in_dev_get(br->dev);
4131 if (WARN_ON(!in_dev))
4134 __ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
4138 #if IS_ENABLED(CONFIG_IPV6)
4139 static void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
4141 struct in6_addr addr;
4143 ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
4144 ipv6_dev_mc_dec(br->dev, &addr);
4147 static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
4152 void br_multicast_leave_snoopers(struct net_bridge *br)
4154 br_ip4_multicast_leave_snoopers(br);
4155 br_ip6_multicast_leave_snoopers(br);
4158 static void __br_multicast_open_query(struct net_bridge *br,
4159 struct bridge_mcast_own_query *query)
4161 query->startup_sent = 0;
4163 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
4166 mod_timer(&query->timer, jiffies);
4169 static void __br_multicast_open(struct net_bridge_mcast *brmctx)
4171 __br_multicast_open_query(brmctx->br, &brmctx->ip4_own_query);
4172 #if IS_ENABLED(CONFIG_IPV6)
4173 __br_multicast_open_query(brmctx->br, &brmctx->ip6_own_query);
4177 void br_multicast_open(struct net_bridge *br)
4181 if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) {
4182 struct net_bridge_vlan_group *vg;
4183 struct net_bridge_vlan *vlan;
4185 vg = br_vlan_group(br);
4187 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
4188 struct net_bridge_mcast *brmctx;
4190 brmctx = &vlan->br_mcast_ctx;
4191 if (br_vlan_is_brentry(vlan) &&
4192 !br_multicast_ctx_vlan_disabled(brmctx))
4193 __br_multicast_open(&vlan->br_mcast_ctx);
4197 __br_multicast_open(&br->multicast_ctx);
4201 static void __br_multicast_stop(struct net_bridge_mcast *brmctx)
4203 del_timer_sync(&brmctx->ip4_mc_router_timer);
4204 del_timer_sync(&brmctx->ip4_other_query.timer);
4205 del_timer_sync(&brmctx->ip4_other_query.delay_timer);
4206 del_timer_sync(&brmctx->ip4_own_query.timer);
4207 #if IS_ENABLED(CONFIG_IPV6)
4208 del_timer_sync(&brmctx->ip6_mc_router_timer);
4209 del_timer_sync(&brmctx->ip6_other_query.timer);
4210 del_timer_sync(&brmctx->ip6_other_query.delay_timer);
4211 del_timer_sync(&brmctx->ip6_own_query.timer);
4215 void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan, bool on)
4217 struct net_bridge *br;
4219 /* it's okay to check for the flag without the multicast lock because it
4220 * can only change under RTNL -> multicast_lock, we need the latter to
4221 * sync with timers and packets
4223 if (on == !!(vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED))
4226 if (br_vlan_is_master(vlan)) {
4229 if (!br_vlan_is_brentry(vlan) ||
4231 br_multicast_ctx_vlan_global_disabled(&vlan->br_mcast_ctx)))
4234 spin_lock_bh(&br->multicast_lock);
4235 vlan->priv_flags ^= BR_VLFLAG_MCAST_ENABLED;
4236 spin_unlock_bh(&br->multicast_lock);
4239 __br_multicast_open(&vlan->br_mcast_ctx);
4241 __br_multicast_stop(&vlan->br_mcast_ctx);
4243 struct net_bridge_mcast *brmctx;
4245 brmctx = br_multicast_port_ctx_get_global(&vlan->port_mcast_ctx);
4246 if (on && br_multicast_ctx_vlan_global_disabled(brmctx))
4249 br = vlan->port->br;
4250 spin_lock_bh(&br->multicast_lock);
4251 vlan->priv_flags ^= BR_VLFLAG_MCAST_ENABLED;
4253 __br_multicast_enable_port_ctx(&vlan->port_mcast_ctx);
4255 __br_multicast_disable_port_ctx(&vlan->port_mcast_ctx);
4256 spin_unlock_bh(&br->multicast_lock);
4260 static void br_multicast_toggle_vlan(struct net_bridge_vlan *vlan, bool on)
4262 struct net_bridge_port *p;
4264 if (WARN_ON_ONCE(!br_vlan_is_master(vlan)))
4267 list_for_each_entry(p, &vlan->br->port_list, list) {
4268 struct net_bridge_vlan *vport;
4270 vport = br_vlan_find(nbp_vlan_group(p), vlan->vid);
4273 br_multicast_toggle_one_vlan(vport, on);
4276 if (br_vlan_is_brentry(vlan))
4277 br_multicast_toggle_one_vlan(vlan, on);
4280 int br_multicast_toggle_vlan_snooping(struct net_bridge *br, bool on,
4281 struct netlink_ext_ack *extack)
4283 struct net_bridge_vlan_group *vg;
4284 struct net_bridge_vlan *vlan;
4285 struct net_bridge_port *p;
4287 if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) == on)
4290 if (on && !br_opt_get(br, BROPT_VLAN_ENABLED)) {
4291 NL_SET_ERR_MSG_MOD(extack, "Cannot enable multicast vlan snooping with vlan filtering disabled");
4295 vg = br_vlan_group(br);
4299 br_opt_toggle(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED, on);
4301 /* disable/enable non-vlan mcast contexts based on vlan snooping */
4303 __br_multicast_stop(&br->multicast_ctx);
4305 __br_multicast_open(&br->multicast_ctx);
4306 list_for_each_entry(p, &br->port_list, list) {
4308 br_multicast_disable_port(p);
4310 br_multicast_enable_port(p);
4313 list_for_each_entry(vlan, &vg->vlan_list, vlist)
4314 br_multicast_toggle_vlan(vlan, on);
4319 bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan, bool on)
4323 /* BR_VLFLAG_GLOBAL_MCAST_ENABLED relies on eventual consistency and
4324 * requires only RTNL to change
4326 if (on == !!(vlan->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED))
4329 vlan->priv_flags ^= BR_VLFLAG_GLOBAL_MCAST_ENABLED;
4330 br_multicast_toggle_vlan(vlan, on);
4335 void br_multicast_stop(struct net_bridge *br)
4339 if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) {
4340 struct net_bridge_vlan_group *vg;
4341 struct net_bridge_vlan *vlan;
4343 vg = br_vlan_group(br);
4345 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
4346 struct net_bridge_mcast *brmctx;
4348 brmctx = &vlan->br_mcast_ctx;
4349 if (br_vlan_is_brentry(vlan) &&
4350 !br_multicast_ctx_vlan_disabled(brmctx))
4351 __br_multicast_stop(&vlan->br_mcast_ctx);
4355 __br_multicast_stop(&br->multicast_ctx);
4359 void br_multicast_dev_del(struct net_bridge *br)
4361 struct net_bridge_mdb_entry *mp;
4362 HLIST_HEAD(deleted_head);
4363 struct hlist_node *tmp;
4365 spin_lock_bh(&br->multicast_lock);
4366 hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node)
4367 br_multicast_del_mdb_entry(mp);
4368 hlist_move_list(&br->mcast_gc_list, &deleted_head);
4369 spin_unlock_bh(&br->multicast_lock);
4371 br_multicast_ctx_deinit(&br->multicast_ctx);
4372 br_multicast_gc(&deleted_head);
4373 cancel_work_sync(&br->mcast_gc_work);
4378 int br_multicast_set_router(struct net_bridge_mcast *brmctx, unsigned long val)
4382 spin_lock_bh(&brmctx->br->multicast_lock);
4385 case MDB_RTR_TYPE_DISABLED:
4386 case MDB_RTR_TYPE_PERM:
4387 br_mc_router_state_change(brmctx->br, val == MDB_RTR_TYPE_PERM);
4388 del_timer(&brmctx->ip4_mc_router_timer);
4389 #if IS_ENABLED(CONFIG_IPV6)
4390 del_timer(&brmctx->ip6_mc_router_timer);
4392 brmctx->multicast_router = val;
4395 case MDB_RTR_TYPE_TEMP_QUERY:
4396 if (brmctx->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
4397 br_mc_router_state_change(brmctx->br, false);
4398 brmctx->multicast_router = val;
4403 spin_unlock_bh(&brmctx->br->multicast_lock);
4409 br_multicast_rport_del_notify(struct net_bridge_mcast_port *pmctx, bool deleted)
4414 /* For backwards compatibility for now, only notify if there is
4415 * no multicast router anymore for both IPv4 and IPv6.
4417 if (!hlist_unhashed(&pmctx->ip4_rlist))
4419 #if IS_ENABLED(CONFIG_IPV6)
4420 if (!hlist_unhashed(&pmctx->ip6_rlist))
4424 br_rtr_notify(pmctx->port->br->dev, pmctx, RTM_DELMDB);
4425 br_port_mc_router_state_change(pmctx->port, false);
4427 /* don't allow timer refresh */
4428 if (pmctx->multicast_router == MDB_RTR_TYPE_TEMP)
4429 pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
4432 int br_multicast_set_port_router(struct net_bridge_mcast_port *pmctx,
4435 struct net_bridge_mcast *brmctx;
4436 unsigned long now = jiffies;
4440 brmctx = br_multicast_port_ctx_get_global(pmctx);
4441 spin_lock_bh(&brmctx->br->multicast_lock);
4442 if (pmctx->multicast_router == val) {
4443 /* Refresh the temp router port timer */
4444 if (pmctx->multicast_router == MDB_RTR_TYPE_TEMP) {
4445 mod_timer(&pmctx->ip4_mc_router_timer,
4446 now + brmctx->multicast_querier_interval);
4447 #if IS_ENABLED(CONFIG_IPV6)
4448 mod_timer(&pmctx->ip6_mc_router_timer,
4449 now + brmctx->multicast_querier_interval);
4456 case MDB_RTR_TYPE_DISABLED:
4457 pmctx->multicast_router = MDB_RTR_TYPE_DISABLED;
4458 del |= br_ip4_multicast_rport_del(pmctx);
4459 del_timer(&pmctx->ip4_mc_router_timer);
4460 del |= br_ip6_multicast_rport_del(pmctx);
4461 #if IS_ENABLED(CONFIG_IPV6)
4462 del_timer(&pmctx->ip6_mc_router_timer);
4464 br_multicast_rport_del_notify(pmctx, del);
4466 case MDB_RTR_TYPE_TEMP_QUERY:
4467 pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
4468 del |= br_ip4_multicast_rport_del(pmctx);
4469 del |= br_ip6_multicast_rport_del(pmctx);
4470 br_multicast_rport_del_notify(pmctx, del);
4472 case MDB_RTR_TYPE_PERM:
4473 pmctx->multicast_router = MDB_RTR_TYPE_PERM;
4474 del_timer(&pmctx->ip4_mc_router_timer);
4475 br_ip4_multicast_add_router(brmctx, pmctx);
4476 #if IS_ENABLED(CONFIG_IPV6)
4477 del_timer(&pmctx->ip6_mc_router_timer);
4479 br_ip6_multicast_add_router(brmctx, pmctx);
4481 case MDB_RTR_TYPE_TEMP:
4482 pmctx->multicast_router = MDB_RTR_TYPE_TEMP;
4483 br_ip4_multicast_mark_router(brmctx, pmctx);
4484 br_ip6_multicast_mark_router(brmctx, pmctx);
4491 spin_unlock_bh(&brmctx->br->multicast_lock);
4496 int br_multicast_set_vlan_router(struct net_bridge_vlan *v, u8 mcast_router)
4500 if (br_vlan_is_master(v))
4501 err = br_multicast_set_router(&v->br_mcast_ctx, mcast_router);
4503 err = br_multicast_set_port_router(&v->port_mcast_ctx,
4509 static void br_multicast_start_querier(struct net_bridge_mcast *brmctx,
4510 struct bridge_mcast_own_query *query)
4512 struct net_bridge_port *port;
4514 if (!br_multicast_ctx_matches_vlan_snooping(brmctx))
4517 __br_multicast_open_query(brmctx->br, query);
4520 list_for_each_entry_rcu(port, &brmctx->br->port_list, list) {
4521 struct bridge_mcast_own_query *ip4_own_query;
4522 #if IS_ENABLED(CONFIG_IPV6)
4523 struct bridge_mcast_own_query *ip6_own_query;
4526 if (br_multicast_port_ctx_state_stopped(&port->multicast_ctx))
4529 if (br_multicast_ctx_is_vlan(brmctx)) {
4530 struct net_bridge_vlan *vlan;
4532 vlan = br_vlan_find(nbp_vlan_group_rcu(port),
4535 br_multicast_port_ctx_state_stopped(&vlan->port_mcast_ctx))
4538 ip4_own_query = &vlan->port_mcast_ctx.ip4_own_query;
4539 #if IS_ENABLED(CONFIG_IPV6)
4540 ip6_own_query = &vlan->port_mcast_ctx.ip6_own_query;
4543 ip4_own_query = &port->multicast_ctx.ip4_own_query;
4544 #if IS_ENABLED(CONFIG_IPV6)
4545 ip6_own_query = &port->multicast_ctx.ip6_own_query;
4549 if (query == &brmctx->ip4_own_query)
4550 br_multicast_enable(ip4_own_query);
4551 #if IS_ENABLED(CONFIG_IPV6)
4553 br_multicast_enable(ip6_own_query);
4559 int br_multicast_toggle(struct net_bridge *br, unsigned long val,
4560 struct netlink_ext_ack *extack)
4562 struct net_bridge_port *port;
4563 bool change_snoopers = false;
4566 spin_lock_bh(&br->multicast_lock);
4567 if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val)
4570 err = br_mc_disabled_update(br->dev, val, extack);
4571 if (err == -EOPNOTSUPP)
4576 br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val);
4577 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
4578 change_snoopers = true;
4582 if (!netif_running(br->dev))
4585 br_multicast_open(br);
4586 list_for_each_entry(port, &br->port_list, list)
4587 __br_multicast_enable_port_ctx(&port->multicast_ctx);
4589 change_snoopers = true;
4592 spin_unlock_bh(&br->multicast_lock);
4594 /* br_multicast_join_snoopers has the potential to cause
4595 * an MLD Report/Leave to be delivered to br_multicast_rcv,
4596 * which would in turn call br_multicast_add_group, which would
4597 * attempt to acquire multicast_lock. This function should be
4598 * called after the lock has been released to avoid deadlocks on
4601 * br_multicast_leave_snoopers does not have the problem since
4602 * br_multicast_rcv first checks BROPT_MULTICAST_ENABLED, and
4603 * returns without calling br_multicast_ipv4/6_rcv if it's not
4604 * enabled. Moved both functions out just for symmetry.
4606 if (change_snoopers) {
4607 if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
4608 br_multicast_join_snoopers(br);
4610 br_multicast_leave_snoopers(br);
4616 bool br_multicast_enabled(const struct net_device *dev)
4618 struct net_bridge *br = netdev_priv(dev);
4620 return !!br_opt_get(br, BROPT_MULTICAST_ENABLED);
4622 EXPORT_SYMBOL_GPL(br_multicast_enabled);
4624 bool br_multicast_router(const struct net_device *dev)
4626 struct net_bridge *br = netdev_priv(dev);
4629 spin_lock_bh(&br->multicast_lock);
4630 is_router = br_multicast_is_router(&br->multicast_ctx, NULL);
4631 spin_unlock_bh(&br->multicast_lock);
4634 EXPORT_SYMBOL_GPL(br_multicast_router);
4636 int br_multicast_set_querier(struct net_bridge_mcast *brmctx, unsigned long val)
4638 unsigned long max_delay;
4642 spin_lock_bh(&brmctx->br->multicast_lock);
4643 if (brmctx->multicast_querier == val)
4646 WRITE_ONCE(brmctx->multicast_querier, val);
4650 max_delay = brmctx->multicast_query_response_interval;
4652 if (!timer_pending(&brmctx->ip4_other_query.timer))
4653 mod_timer(&brmctx->ip4_other_query.delay_timer,
4654 jiffies + max_delay);
4656 br_multicast_start_querier(brmctx, &brmctx->ip4_own_query);
4658 #if IS_ENABLED(CONFIG_IPV6)
4659 if (!timer_pending(&brmctx->ip6_other_query.timer))
4660 mod_timer(&brmctx->ip6_other_query.delay_timer,
4661 jiffies + max_delay);
4663 br_multicast_start_querier(brmctx, &brmctx->ip6_own_query);
4667 spin_unlock_bh(&brmctx->br->multicast_lock);
4672 int br_multicast_set_igmp_version(struct net_bridge_mcast *brmctx,
4675 /* Currently we support only version 2 and 3 */
4684 spin_lock_bh(&brmctx->br->multicast_lock);
4685 brmctx->multicast_igmp_version = val;
4686 spin_unlock_bh(&brmctx->br->multicast_lock);
4691 #if IS_ENABLED(CONFIG_IPV6)
4692 int br_multicast_set_mld_version(struct net_bridge_mcast *brmctx,
4695 /* Currently we support version 1 and 2 */
4704 spin_lock_bh(&brmctx->br->multicast_lock);
4705 brmctx->multicast_mld_version = val;
4706 spin_unlock_bh(&brmctx->br->multicast_lock);
4712 void br_multicast_set_query_intvl(struct net_bridge_mcast *brmctx,
4715 unsigned long intvl_jiffies = clock_t_to_jiffies(val);
4717 if (intvl_jiffies < BR_MULTICAST_QUERY_INTVL_MIN) {
4719 "trying to set multicast query interval below minimum, setting to %lu (%ums)\n",
4720 jiffies_to_clock_t(BR_MULTICAST_QUERY_INTVL_MIN),
4721 jiffies_to_msecs(BR_MULTICAST_QUERY_INTVL_MIN));
4722 intvl_jiffies = BR_MULTICAST_QUERY_INTVL_MIN;
4725 brmctx->multicast_query_interval = intvl_jiffies;
4728 void br_multicast_set_startup_query_intvl(struct net_bridge_mcast *brmctx,
4731 unsigned long intvl_jiffies = clock_t_to_jiffies(val);
4733 if (intvl_jiffies < BR_MULTICAST_STARTUP_QUERY_INTVL_MIN) {
4735 "trying to set multicast startup query interval below minimum, setting to %lu (%ums)\n",
4736 jiffies_to_clock_t(BR_MULTICAST_STARTUP_QUERY_INTVL_MIN),
4737 jiffies_to_msecs(BR_MULTICAST_STARTUP_QUERY_INTVL_MIN));
4738 intvl_jiffies = BR_MULTICAST_STARTUP_QUERY_INTVL_MIN;
4741 brmctx->multicast_startup_query_interval = intvl_jiffies;
4745 * br_multicast_list_adjacent - Returns snooped multicast addresses
4746 * @dev: The bridge port adjacent to which to retrieve addresses
4747 * @br_ip_list: The list to store found, snooped multicast IP addresses in
4749 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
4750 * snooping feature on all bridge ports of dev's bridge device, excluding
4751 * the addresses from dev itself.
4753 * Returns the number of items added to br_ip_list.
4756 * - br_ip_list needs to be initialized by caller
4757 * - br_ip_list might contain duplicates in the end
4758 * (needs to be taken care of by caller)
4759 * - br_ip_list needs to be freed by caller
4761 int br_multicast_list_adjacent(struct net_device *dev,
4762 struct list_head *br_ip_list)
4764 struct net_bridge *br;
4765 struct net_bridge_port *port;
4766 struct net_bridge_port_group *group;
4767 struct br_ip_list *entry;
4771 if (!br_ip_list || !netif_is_bridge_port(dev))
4774 port = br_port_get_rcu(dev);
4775 if (!port || !port->br)
4780 list_for_each_entry_rcu(port, &br->port_list, list) {
4781 if (!port->dev || port->dev == dev)
4784 hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
4785 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
4789 entry->addr = group->key.addr;
4790 list_add(&entry->list, br_ip_list);
4799 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
4802 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
4803 * @dev: The bridge port providing the bridge on which to check for a querier
4804 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
4806 * Checks whether the given interface has a bridge on top and if so returns
4807 * true if a valid querier exists anywhere on the bridged link layer.
4808 * Otherwise returns false.
4810 bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
4812 struct net_bridge *br;
4813 struct net_bridge_port *port;
4818 if (!netif_is_bridge_port(dev))
4821 port = br_port_get_rcu(dev);
4822 if (!port || !port->br)
4827 memset(ð, 0, sizeof(eth));
4828 eth.h_proto = htons(proto);
4830 ret = br_multicast_querier_exists(&br->multicast_ctx, ð, NULL);
4836 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
4839 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
4840 * @dev: The bridge port adjacent to which to check for a querier
4841 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
4843 * Checks whether the given interface has a bridge on top and if so returns
4844 * true if a selected querier is behind one of the other ports of this
4845 * bridge. Otherwise returns false.
4847 bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
4849 struct net_bridge_mcast *brmctx;
4850 struct net_bridge *br;
4851 struct net_bridge_port *port;
4856 if (!netif_is_bridge_port(dev))
4859 port = br_port_get_rcu(dev);
4860 if (!port || !port->br)
4864 brmctx = &br->multicast_ctx;
4868 port_ifidx = brmctx->ip4_querier.port_ifidx;
4869 if (!timer_pending(&brmctx->ip4_other_query.timer) ||
4870 port_ifidx == port->dev->ifindex)
4873 #if IS_ENABLED(CONFIG_IPV6)
4875 port_ifidx = brmctx->ip6_querier.port_ifidx;
4876 if (!timer_pending(&brmctx->ip6_other_query.timer) ||
4877 port_ifidx == port->dev->ifindex)
4890 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
4893 * br_multicast_has_router_adjacent - Checks for a router behind a bridge port
4894 * @dev: The bridge port adjacent to which to check for a multicast router
4895 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
4897 * Checks whether the given interface has a bridge on top and if so returns
4898 * true if a multicast router is behind one of the other ports of this
4899 * bridge. Otherwise returns false.
4901 bool br_multicast_has_router_adjacent(struct net_device *dev, int proto)
4903 struct net_bridge_mcast_port *pmctx;
4904 struct net_bridge_mcast *brmctx;
4905 struct net_bridge_port *port;
4909 port = br_port_get_check_rcu(dev);
4913 brmctx = &port->br->multicast_ctx;
4916 hlist_for_each_entry_rcu(pmctx, &brmctx->ip4_mc_router_list,
4918 if (pmctx->port == port)
4925 #if IS_ENABLED(CONFIG_IPV6)
4927 hlist_for_each_entry_rcu(pmctx, &brmctx->ip6_mc_router_list,
4929 if (pmctx->port == port)
4938 /* when compiled without IPv6 support, be conservative and
4939 * always assume presence of an IPv6 multicast router
4948 EXPORT_SYMBOL_GPL(br_multicast_has_router_adjacent);
4950 static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
4951 const struct sk_buff *skb, u8 type, u8 dir)
4953 struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
4954 __be16 proto = skb->protocol;
4957 u64_stats_update_begin(&pstats->syncp);
4959 case htons(ETH_P_IP):
4960 t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
4962 case IGMP_HOST_MEMBERSHIP_REPORT:
4963 pstats->mstats.igmp_v1reports[dir]++;
4965 case IGMPV2_HOST_MEMBERSHIP_REPORT:
4966 pstats->mstats.igmp_v2reports[dir]++;
4968 case IGMPV3_HOST_MEMBERSHIP_REPORT:
4969 pstats->mstats.igmp_v3reports[dir]++;
4971 case IGMP_HOST_MEMBERSHIP_QUERY:
4972 if (t_len != sizeof(struct igmphdr)) {
4973 pstats->mstats.igmp_v3queries[dir]++;
4975 unsigned int offset = skb_transport_offset(skb);
4976 struct igmphdr *ih, _ihdr;
4978 ih = skb_header_pointer(skb, offset,
4979 sizeof(_ihdr), &_ihdr);
4983 pstats->mstats.igmp_v1queries[dir]++;
4985 pstats->mstats.igmp_v2queries[dir]++;
4988 case IGMP_HOST_LEAVE_MESSAGE:
4989 pstats->mstats.igmp_leaves[dir]++;
4993 #if IS_ENABLED(CONFIG_IPV6)
4994 case htons(ETH_P_IPV6):
4995 t_len = ntohs(ipv6_hdr(skb)->payload_len) +
4996 sizeof(struct ipv6hdr);
4997 t_len -= skb_network_header_len(skb);
4999 case ICMPV6_MGM_REPORT:
5000 pstats->mstats.mld_v1reports[dir]++;
5002 case ICMPV6_MLD2_REPORT:
5003 pstats->mstats.mld_v2reports[dir]++;
5005 case ICMPV6_MGM_QUERY:
5006 if (t_len != sizeof(struct mld_msg))
5007 pstats->mstats.mld_v2queries[dir]++;
5009 pstats->mstats.mld_v1queries[dir]++;
5011 case ICMPV6_MGM_REDUCTION:
5012 pstats->mstats.mld_leaves[dir]++;
5016 #endif /* CONFIG_IPV6 */
5018 u64_stats_update_end(&pstats->syncp);
5021 void br_multicast_count(struct net_bridge *br,
5022 const struct net_bridge_port *p,
5023 const struct sk_buff *skb, u8 type, u8 dir)
5025 struct bridge_mcast_stats __percpu *stats;
5027 /* if multicast_disabled is true then igmp type can't be set */
5028 if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
5032 stats = p->mcast_stats;
5034 stats = br->mcast_stats;
5035 if (WARN_ON(!stats))
5038 br_mcast_stats_add(stats, skb, type, dir);
5041 int br_multicast_init_stats(struct net_bridge *br)
5043 br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
5044 if (!br->mcast_stats)
5050 void br_multicast_uninit_stats(struct net_bridge *br)
5052 free_percpu(br->mcast_stats);
5055 /* noinline for https://bugs.llvm.org/show_bug.cgi?id=45802#c9 */
5056 static noinline_for_stack void mcast_stats_add_dir(u64 *dst, u64 *src)
5058 dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
5059 dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
5062 void br_multicast_get_stats(const struct net_bridge *br,
5063 const struct net_bridge_port *p,
5064 struct br_mcast_stats *dest)
5066 struct bridge_mcast_stats __percpu *stats;
5067 struct br_mcast_stats tdst;
5070 memset(dest, 0, sizeof(*dest));
5072 stats = p->mcast_stats;
5074 stats = br->mcast_stats;
5075 if (WARN_ON(!stats))
5078 memset(&tdst, 0, sizeof(tdst));
5079 for_each_possible_cpu(i) {
5080 struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
5081 struct br_mcast_stats temp;
5085 start = u64_stats_fetch_begin(&cpu_stats->syncp);
5086 memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
5087 } while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
5089 mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
5090 mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
5091 mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
5092 mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
5093 mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
5094 mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
5095 mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
5096 tdst.igmp_parse_errors += temp.igmp_parse_errors;
5098 mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
5099 mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
5100 mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
5101 mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
5102 mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
5103 tdst.mld_parse_errors += temp.mld_parse_errors;
5105 memcpy(dest, &tdst, sizeof(*dest));
5108 int br_mdb_hash_init(struct net_bridge *br)
5112 err = rhashtable_init(&br->sg_port_tbl, &br_sg_port_rht_params);
5116 err = rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params);
5118 rhashtable_destroy(&br->sg_port_tbl);
5125 void br_mdb_hash_fini(struct net_bridge *br)
5127 rhashtable_destroy(&br->sg_port_tbl);
5128 rhashtable_destroy(&br->mdb_hash_tbl);