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_select_own_querier(struct net_bridge_mcast *brmctx,
1766 struct sk_buff *skb)
1768 if (ip->proto == htons(ETH_P_IP))
1769 brmctx->ip4_querier.addr.src.ip4 = ip_hdr(skb)->saddr;
1770 #if IS_ENABLED(CONFIG_IPV6)
1772 brmctx->ip6_querier.addr.src.ip6 = ipv6_hdr(skb)->saddr;
1776 static void __br_multicast_send_query(struct net_bridge_mcast *brmctx,
1777 struct net_bridge_mcast_port *pmctx,
1778 struct net_bridge_port_group *pg,
1779 struct br_ip *ip_dst,
1780 struct br_ip *group,
1785 bool over_lmqt = !!sflag;
1786 struct sk_buff *skb;
1789 if (!br_multicast_ctx_should_use(brmctx, pmctx) ||
1790 !br_multicast_ctx_matches_vlan_snooping(brmctx))
1794 skb = br_multicast_alloc_query(brmctx, pmctx, pg, ip_dst, group,
1795 with_srcs, over_lmqt, sflag, &igmp_type,
1801 skb->dev = pmctx->port->dev;
1802 br_multicast_count(brmctx->br, pmctx->port, skb, igmp_type,
1804 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
1805 dev_net(pmctx->port->dev), NULL, skb, NULL, skb->dev,
1806 br_dev_queue_push_xmit);
1808 if (over_lmqt && with_srcs && sflag) {
1810 goto again_under_lmqt;
1813 br_multicast_select_own_querier(brmctx, group, skb);
1814 br_multicast_count(brmctx->br, NULL, skb, igmp_type,
1820 static void br_multicast_read_querier(const struct bridge_mcast_querier *querier,
1821 struct bridge_mcast_querier *dest)
1825 memset(dest, 0, sizeof(*dest));
1827 seq = read_seqcount_begin(&querier->seq);
1828 dest->port_ifidx = querier->port_ifidx;
1829 memcpy(&dest->addr, &querier->addr, sizeof(struct br_ip));
1830 } while (read_seqcount_retry(&querier->seq, seq));
1833 static void br_multicast_update_querier(struct net_bridge_mcast *brmctx,
1834 struct bridge_mcast_querier *querier,
1836 struct br_ip *saddr)
1838 write_seqcount_begin(&querier->seq);
1839 querier->port_ifidx = ifindex;
1840 memcpy(&querier->addr, saddr, sizeof(*saddr));
1841 write_seqcount_end(&querier->seq);
1844 static void br_multicast_send_query(struct net_bridge_mcast *brmctx,
1845 struct net_bridge_mcast_port *pmctx,
1846 struct bridge_mcast_own_query *own_query)
1848 struct bridge_mcast_other_query *other_query = NULL;
1849 struct bridge_mcast_querier *querier;
1850 struct br_ip br_group;
1853 if (!br_multicast_ctx_should_use(brmctx, pmctx) ||
1854 !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED) ||
1855 !brmctx->multicast_querier)
1858 memset(&br_group.dst, 0, sizeof(br_group.dst));
1860 if (pmctx ? (own_query == &pmctx->ip4_own_query) :
1861 (own_query == &brmctx->ip4_own_query)) {
1862 querier = &brmctx->ip4_querier;
1863 other_query = &brmctx->ip4_other_query;
1864 br_group.proto = htons(ETH_P_IP);
1865 #if IS_ENABLED(CONFIG_IPV6)
1867 querier = &brmctx->ip6_querier;
1868 other_query = &brmctx->ip6_other_query;
1869 br_group.proto = htons(ETH_P_IPV6);
1873 if (!other_query || timer_pending(&other_query->timer))
1876 /* we're about to select ourselves as querier */
1877 if (!pmctx && querier->port_ifidx) {
1878 struct br_ip zeroip = {};
1880 br_multicast_update_querier(brmctx, querier, 0, &zeroip);
1883 __br_multicast_send_query(brmctx, pmctx, NULL, NULL, &br_group, false,
1887 time += own_query->startup_sent < brmctx->multicast_startup_query_count ?
1888 brmctx->multicast_startup_query_interval :
1889 brmctx->multicast_query_interval;
1890 mod_timer(&own_query->timer, time);
1894 br_multicast_port_query_expired(struct net_bridge_mcast_port *pmctx,
1895 struct bridge_mcast_own_query *query)
1897 struct net_bridge *br = pmctx->port->br;
1898 struct net_bridge_mcast *brmctx;
1900 spin_lock(&br->multicast_lock);
1901 if (br_multicast_port_ctx_state_stopped(pmctx))
1904 brmctx = br_multicast_port_ctx_get_global(pmctx);
1905 if (query->startup_sent < brmctx->multicast_startup_query_count)
1906 query->startup_sent++;
1908 br_multicast_send_query(brmctx, pmctx, query);
1911 spin_unlock(&br->multicast_lock);
1914 static void br_ip4_multicast_port_query_expired(struct timer_list *t)
1916 struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t,
1917 ip4_own_query.timer);
1919 br_multicast_port_query_expired(pmctx, &pmctx->ip4_own_query);
1922 #if IS_ENABLED(CONFIG_IPV6)
1923 static void br_ip6_multicast_port_query_expired(struct timer_list *t)
1925 struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t,
1926 ip6_own_query.timer);
1928 br_multicast_port_query_expired(pmctx, &pmctx->ip6_own_query);
1932 static void br_multicast_port_group_rexmit(struct timer_list *t)
1934 struct net_bridge_port_group *pg = from_timer(pg, t, rexmit_timer);
1935 struct bridge_mcast_other_query *other_query = NULL;
1936 struct net_bridge *br = pg->key.port->br;
1937 struct net_bridge_mcast_port *pmctx;
1938 struct net_bridge_mcast *brmctx;
1939 bool need_rexmit = false;
1941 spin_lock(&br->multicast_lock);
1942 if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
1943 !br_opt_get(br, BROPT_MULTICAST_ENABLED))
1946 pmctx = br_multicast_pg_to_port_ctx(pg);
1949 brmctx = br_multicast_port_ctx_get_global(pmctx);
1950 if (!brmctx->multicast_querier)
1953 if (pg->key.addr.proto == htons(ETH_P_IP))
1954 other_query = &brmctx->ip4_other_query;
1955 #if IS_ENABLED(CONFIG_IPV6)
1957 other_query = &brmctx->ip6_other_query;
1960 if (!other_query || timer_pending(&other_query->timer))
1963 if (pg->grp_query_rexmit_cnt) {
1964 pg->grp_query_rexmit_cnt--;
1965 __br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr,
1966 &pg->key.addr, false, 1, NULL);
1968 __br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr,
1969 &pg->key.addr, true, 0, &need_rexmit);
1971 if (pg->grp_query_rexmit_cnt || need_rexmit)
1972 mod_timer(&pg->rexmit_timer, jiffies +
1973 brmctx->multicast_last_member_interval);
1975 spin_unlock(&br->multicast_lock);
1978 static int br_mc_disabled_update(struct net_device *dev, bool value,
1979 struct netlink_ext_ack *extack)
1981 struct switchdev_attr attr = {
1983 .id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
1984 .flags = SWITCHDEV_F_DEFER,
1985 .u.mc_disabled = !value,
1988 return switchdev_port_attr_set(dev, &attr, extack);
1991 void br_multicast_port_ctx_init(struct net_bridge_port *port,
1992 struct net_bridge_vlan *vlan,
1993 struct net_bridge_mcast_port *pmctx)
1997 pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1998 timer_setup(&pmctx->ip4_mc_router_timer,
1999 br_ip4_multicast_router_expired, 0);
2000 timer_setup(&pmctx->ip4_own_query.timer,
2001 br_ip4_multicast_port_query_expired, 0);
2002 #if IS_ENABLED(CONFIG_IPV6)
2003 timer_setup(&pmctx->ip6_mc_router_timer,
2004 br_ip6_multicast_router_expired, 0);
2005 timer_setup(&pmctx->ip6_own_query.timer,
2006 br_ip6_multicast_port_query_expired, 0);
2010 void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx)
2012 #if IS_ENABLED(CONFIG_IPV6)
2013 del_timer_sync(&pmctx->ip6_mc_router_timer);
2015 del_timer_sync(&pmctx->ip4_mc_router_timer);
2018 int br_multicast_add_port(struct net_bridge_port *port)
2022 port->multicast_eht_hosts_limit = BR_MCAST_DEFAULT_EHT_HOSTS_LIMIT;
2023 br_multicast_port_ctx_init(port, NULL, &port->multicast_ctx);
2025 err = br_mc_disabled_update(port->dev,
2026 br_opt_get(port->br,
2027 BROPT_MULTICAST_ENABLED),
2029 if (err && err != -EOPNOTSUPP)
2032 port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
2033 if (!port->mcast_stats)
2039 void br_multicast_del_port(struct net_bridge_port *port)
2041 struct net_bridge *br = port->br;
2042 struct net_bridge_port_group *pg;
2043 HLIST_HEAD(deleted_head);
2044 struct hlist_node *n;
2046 /* Take care of the remaining groups, only perm ones should be left */
2047 spin_lock_bh(&br->multicast_lock);
2048 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
2049 br_multicast_find_del_pg(br, pg);
2050 hlist_move_list(&br->mcast_gc_list, &deleted_head);
2051 spin_unlock_bh(&br->multicast_lock);
2052 br_multicast_gc(&deleted_head);
2053 br_multicast_port_ctx_deinit(&port->multicast_ctx);
2054 free_percpu(port->mcast_stats);
2057 static void br_multicast_enable(struct bridge_mcast_own_query *query)
2059 query->startup_sent = 0;
2061 if (try_to_del_timer_sync(&query->timer) >= 0 ||
2062 del_timer(&query->timer))
2063 mod_timer(&query->timer, jiffies);
2066 static void __br_multicast_enable_port_ctx(struct net_bridge_mcast_port *pmctx)
2068 struct net_bridge *br = pmctx->port->br;
2069 struct net_bridge_mcast *brmctx;
2071 brmctx = br_multicast_port_ctx_get_global(pmctx);
2072 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
2073 !netif_running(br->dev))
2076 br_multicast_enable(&pmctx->ip4_own_query);
2077 #if IS_ENABLED(CONFIG_IPV6)
2078 br_multicast_enable(&pmctx->ip6_own_query);
2080 if (pmctx->multicast_router == MDB_RTR_TYPE_PERM) {
2081 br_ip4_multicast_add_router(brmctx, pmctx);
2082 br_ip6_multicast_add_router(brmctx, pmctx);
2085 if (br_multicast_port_ctx_is_vlan(pmctx)) {
2086 struct net_bridge_port_group *pg;
2089 /* The mcast_n_groups counter might be wrong. First,
2090 * BR_VLFLAG_MCAST_ENABLED is toggled before temporary entries
2091 * are flushed, thus mcast_n_groups after the toggle does not
2092 * reflect the true values. And second, permanent entries added
2093 * while BR_VLFLAG_MCAST_ENABLED was disabled, are not reflected
2094 * either. Thus we have to refresh the counter.
2097 hlist_for_each_entry(pg, &pmctx->port->mglist, mglist) {
2098 if (pg->key.addr.vid == pmctx->vlan->vid)
2101 WRITE_ONCE(pmctx->mdb_n_entries, n);
2105 void br_multicast_enable_port(struct net_bridge_port *port)
2107 struct net_bridge *br = port->br;
2109 spin_lock_bh(&br->multicast_lock);
2110 __br_multicast_enable_port_ctx(&port->multicast_ctx);
2111 spin_unlock_bh(&br->multicast_lock);
2114 static void __br_multicast_disable_port_ctx(struct net_bridge_mcast_port *pmctx)
2116 struct net_bridge_port_group *pg;
2117 struct hlist_node *n;
2120 hlist_for_each_entry_safe(pg, n, &pmctx->port->mglist, mglist)
2121 if (!(pg->flags & MDB_PG_FLAGS_PERMANENT) &&
2122 (!br_multicast_port_ctx_is_vlan(pmctx) ||
2123 pg->key.addr.vid == pmctx->vlan->vid))
2124 br_multicast_find_del_pg(pmctx->port->br, pg);
2126 del |= br_ip4_multicast_rport_del(pmctx);
2127 del_timer(&pmctx->ip4_mc_router_timer);
2128 del_timer(&pmctx->ip4_own_query.timer);
2129 del |= br_ip6_multicast_rport_del(pmctx);
2130 #if IS_ENABLED(CONFIG_IPV6)
2131 del_timer(&pmctx->ip6_mc_router_timer);
2132 del_timer(&pmctx->ip6_own_query.timer);
2134 br_multicast_rport_del_notify(pmctx, del);
2137 void br_multicast_disable_port(struct net_bridge_port *port)
2139 spin_lock_bh(&port->br->multicast_lock);
2140 __br_multicast_disable_port_ctx(&port->multicast_ctx);
2141 spin_unlock_bh(&port->br->multicast_lock);
2144 static int __grp_src_delete_marked(struct net_bridge_port_group *pg)
2146 struct net_bridge_group_src *ent;
2147 struct hlist_node *tmp;
2150 hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
2151 if (ent->flags & BR_SGRP_F_DELETE) {
2152 br_multicast_del_group_src(ent, false);
2159 static void __grp_src_mod_timer(struct net_bridge_group_src *src,
2160 unsigned long expires)
2162 mod_timer(&src->timer, expires);
2163 br_multicast_fwd_src_handle(src);
2166 static void __grp_src_query_marked_and_rexmit(struct net_bridge_mcast *brmctx,
2167 struct net_bridge_mcast_port *pmctx,
2168 struct net_bridge_port_group *pg)
2170 struct bridge_mcast_other_query *other_query = NULL;
2171 u32 lmqc = brmctx->multicast_last_member_count;
2172 unsigned long lmqt, lmi, now = jiffies;
2173 struct net_bridge_group_src *ent;
2175 if (!netif_running(brmctx->br->dev) ||
2176 !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED))
2179 if (pg->key.addr.proto == htons(ETH_P_IP))
2180 other_query = &brmctx->ip4_other_query;
2181 #if IS_ENABLED(CONFIG_IPV6)
2183 other_query = &brmctx->ip6_other_query;
2186 lmqt = now + br_multicast_lmqt(brmctx);
2187 hlist_for_each_entry(ent, &pg->src_list, node) {
2188 if (ent->flags & BR_SGRP_F_SEND) {
2189 ent->flags &= ~BR_SGRP_F_SEND;
2190 if (ent->timer.expires > lmqt) {
2191 if (brmctx->multicast_querier &&
2193 !timer_pending(&other_query->timer))
2194 ent->src_query_rexmit_cnt = lmqc;
2195 __grp_src_mod_timer(ent, lmqt);
2200 if (!brmctx->multicast_querier ||
2201 !other_query || timer_pending(&other_query->timer))
2204 __br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr,
2205 &pg->key.addr, true, 1, NULL);
2207 lmi = now + brmctx->multicast_last_member_interval;
2208 if (!timer_pending(&pg->rexmit_timer) ||
2209 time_after(pg->rexmit_timer.expires, lmi))
2210 mod_timer(&pg->rexmit_timer, lmi);
2213 static void __grp_send_query_and_rexmit(struct net_bridge_mcast *brmctx,
2214 struct net_bridge_mcast_port *pmctx,
2215 struct net_bridge_port_group *pg)
2217 struct bridge_mcast_other_query *other_query = NULL;
2218 unsigned long now = jiffies, lmi;
2220 if (!netif_running(brmctx->br->dev) ||
2221 !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED))
2224 if (pg->key.addr.proto == htons(ETH_P_IP))
2225 other_query = &brmctx->ip4_other_query;
2226 #if IS_ENABLED(CONFIG_IPV6)
2228 other_query = &brmctx->ip6_other_query;
2231 if (brmctx->multicast_querier &&
2232 other_query && !timer_pending(&other_query->timer)) {
2233 lmi = now + brmctx->multicast_last_member_interval;
2234 pg->grp_query_rexmit_cnt = brmctx->multicast_last_member_count - 1;
2235 __br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr,
2236 &pg->key.addr, false, 0, NULL);
2237 if (!timer_pending(&pg->rexmit_timer) ||
2238 time_after(pg->rexmit_timer.expires, lmi))
2239 mod_timer(&pg->rexmit_timer, lmi);
2242 if (pg->filter_mode == MCAST_EXCLUDE &&
2243 (!timer_pending(&pg->timer) ||
2244 time_after(pg->timer.expires, now + br_multicast_lmqt(brmctx))))
2245 mod_timer(&pg->timer, now + br_multicast_lmqt(brmctx));
2248 /* State Msg type New state Actions
2249 * INCLUDE (A) IS_IN (B) INCLUDE (A+B) (B)=GMI
2250 * INCLUDE (A) ALLOW (B) INCLUDE (A+B) (B)=GMI
2251 * EXCLUDE (X,Y) ALLOW (A) EXCLUDE (X+A,Y-A) (A)=GMI
2253 static bool br_multicast_isinc_allow(const struct net_bridge_mcast *brmctx,
2254 struct net_bridge_port_group *pg, void *h_addr,
2255 void *srcs, u32 nsrcs, size_t addr_size,
2258 struct net_bridge_group_src *ent;
2259 unsigned long now = jiffies;
2260 bool changed = false;
2261 struct br_ip src_ip;
2264 memset(&src_ip, 0, sizeof(src_ip));
2265 src_ip.proto = pg->key.addr.proto;
2266 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2267 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2268 ent = br_multicast_find_group_src(pg, &src_ip);
2270 ent = br_multicast_new_group_src(pg, &src_ip);
2276 __grp_src_mod_timer(ent, now + br_multicast_gmi(brmctx));
2279 if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2286 /* State Msg type New state Actions
2287 * INCLUDE (A) IS_EX (B) EXCLUDE (A*B,B-A) (B-A)=0
2291 static void __grp_src_isexc_incl(const struct net_bridge_mcast *brmctx,
2292 struct net_bridge_port_group *pg, void *h_addr,
2293 void *srcs, u32 nsrcs, size_t addr_size,
2296 struct net_bridge_group_src *ent;
2297 struct br_ip src_ip;
2300 hlist_for_each_entry(ent, &pg->src_list, node)
2301 ent->flags |= BR_SGRP_F_DELETE;
2303 memset(&src_ip, 0, sizeof(src_ip));
2304 src_ip.proto = pg->key.addr.proto;
2305 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2306 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2307 ent = br_multicast_find_group_src(pg, &src_ip);
2309 ent->flags &= ~BR_SGRP_F_DELETE;
2311 ent = br_multicast_new_group_src(pg, &src_ip);
2313 br_multicast_fwd_src_handle(ent);
2316 br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2319 __grp_src_delete_marked(pg);
2322 /* State Msg type New state Actions
2323 * EXCLUDE (X,Y) IS_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=GMI
2328 static bool __grp_src_isexc_excl(const struct net_bridge_mcast *brmctx,
2329 struct net_bridge_port_group *pg, void *h_addr,
2330 void *srcs, u32 nsrcs, size_t addr_size,
2333 struct net_bridge_group_src *ent;
2334 unsigned long now = jiffies;
2335 bool changed = false;
2336 struct br_ip src_ip;
2339 hlist_for_each_entry(ent, &pg->src_list, node)
2340 ent->flags |= BR_SGRP_F_DELETE;
2342 memset(&src_ip, 0, sizeof(src_ip));
2343 src_ip.proto = pg->key.addr.proto;
2344 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2345 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2346 ent = br_multicast_find_group_src(pg, &src_ip);
2348 ent->flags &= ~BR_SGRP_F_DELETE;
2350 ent = br_multicast_new_group_src(pg, &src_ip);
2352 __grp_src_mod_timer(ent,
2353 now + br_multicast_gmi(brmctx));
2359 if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2363 if (__grp_src_delete_marked(pg))
2369 static bool br_multicast_isexc(const struct net_bridge_mcast *brmctx,
2370 struct net_bridge_port_group *pg, void *h_addr,
2371 void *srcs, u32 nsrcs, size_t addr_size,
2374 bool changed = false;
2376 switch (pg->filter_mode) {
2378 __grp_src_isexc_incl(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2380 br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE);
2384 changed = __grp_src_isexc_excl(brmctx, pg, h_addr, srcs, nsrcs,
2385 addr_size, grec_type);
2389 pg->filter_mode = MCAST_EXCLUDE;
2390 mod_timer(&pg->timer, jiffies + br_multicast_gmi(brmctx));
2395 /* State Msg type New state Actions
2396 * INCLUDE (A) TO_IN (B) INCLUDE (A+B) (B)=GMI
2399 static bool __grp_src_toin_incl(struct net_bridge_mcast *brmctx,
2400 struct net_bridge_mcast_port *pmctx,
2401 struct net_bridge_port_group *pg, void *h_addr,
2402 void *srcs, u32 nsrcs, size_t addr_size,
2405 u32 src_idx, to_send = pg->src_ents;
2406 struct net_bridge_group_src *ent;
2407 unsigned long now = jiffies;
2408 bool changed = false;
2409 struct br_ip src_ip;
2411 hlist_for_each_entry(ent, &pg->src_list, node)
2412 ent->flags |= BR_SGRP_F_SEND;
2414 memset(&src_ip, 0, sizeof(src_ip));
2415 src_ip.proto = pg->key.addr.proto;
2416 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2417 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2418 ent = br_multicast_find_group_src(pg, &src_ip);
2420 ent->flags &= ~BR_SGRP_F_SEND;
2423 ent = br_multicast_new_group_src(pg, &src_ip);
2428 __grp_src_mod_timer(ent, now + br_multicast_gmi(brmctx));
2431 if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2436 __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
2441 /* State Msg type New state Actions
2442 * EXCLUDE (X,Y) TO_IN (A) EXCLUDE (X+A,Y-A) (A)=GMI
2446 static bool __grp_src_toin_excl(struct net_bridge_mcast *brmctx,
2447 struct net_bridge_mcast_port *pmctx,
2448 struct net_bridge_port_group *pg, void *h_addr,
2449 void *srcs, u32 nsrcs, size_t addr_size,
2452 u32 src_idx, to_send = pg->src_ents;
2453 struct net_bridge_group_src *ent;
2454 unsigned long now = jiffies;
2455 bool changed = false;
2456 struct br_ip src_ip;
2458 hlist_for_each_entry(ent, &pg->src_list, node)
2459 if (timer_pending(&ent->timer))
2460 ent->flags |= BR_SGRP_F_SEND;
2462 memset(&src_ip, 0, sizeof(src_ip));
2463 src_ip.proto = pg->key.addr.proto;
2464 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2465 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2466 ent = br_multicast_find_group_src(pg, &src_ip);
2468 if (timer_pending(&ent->timer)) {
2469 ent->flags &= ~BR_SGRP_F_SEND;
2473 ent = br_multicast_new_group_src(pg, &src_ip);
2478 __grp_src_mod_timer(ent, now + br_multicast_gmi(brmctx));
2481 if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2486 __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
2488 __grp_send_query_and_rexmit(brmctx, pmctx, pg);
2493 static bool br_multicast_toin(struct net_bridge_mcast *brmctx,
2494 struct net_bridge_mcast_port *pmctx,
2495 struct net_bridge_port_group *pg, void *h_addr,
2496 void *srcs, u32 nsrcs, size_t addr_size,
2499 bool changed = false;
2501 switch (pg->filter_mode) {
2503 changed = __grp_src_toin_incl(brmctx, pmctx, pg, h_addr, srcs,
2504 nsrcs, addr_size, grec_type);
2507 changed = __grp_src_toin_excl(brmctx, pmctx, pg, h_addr, srcs,
2508 nsrcs, addr_size, grec_type);
2512 if (br_multicast_eht_should_del_pg(pg)) {
2513 pg->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2514 br_multicast_find_del_pg(pg->key.port->br, pg);
2515 /* a notification has already been sent and we shouldn't
2516 * access pg after the delete so we have to return false
2524 /* State Msg type New state Actions
2525 * INCLUDE (A) TO_EX (B) EXCLUDE (A*B,B-A) (B-A)=0
2530 static void __grp_src_toex_incl(struct net_bridge_mcast *brmctx,
2531 struct net_bridge_mcast_port *pmctx,
2532 struct net_bridge_port_group *pg, void *h_addr,
2533 void *srcs, u32 nsrcs, size_t addr_size,
2536 struct net_bridge_group_src *ent;
2537 u32 src_idx, to_send = 0;
2538 struct br_ip src_ip;
2540 hlist_for_each_entry(ent, &pg->src_list, node)
2541 ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE;
2543 memset(&src_ip, 0, sizeof(src_ip));
2544 src_ip.proto = pg->key.addr.proto;
2545 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2546 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2547 ent = br_multicast_find_group_src(pg, &src_ip);
2549 ent->flags = (ent->flags & ~BR_SGRP_F_DELETE) |
2553 ent = br_multicast_new_group_src(pg, &src_ip);
2556 br_multicast_fwd_src_handle(ent);
2559 br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2562 __grp_src_delete_marked(pg);
2564 __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
2567 /* State Msg type New state Actions
2568 * EXCLUDE (X,Y) TO_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=Group Timer
2574 static bool __grp_src_toex_excl(struct net_bridge_mcast *brmctx,
2575 struct net_bridge_mcast_port *pmctx,
2576 struct net_bridge_port_group *pg, void *h_addr,
2577 void *srcs, u32 nsrcs, size_t addr_size,
2580 struct net_bridge_group_src *ent;
2581 u32 src_idx, to_send = 0;
2582 bool changed = false;
2583 struct br_ip src_ip;
2585 hlist_for_each_entry(ent, &pg->src_list, node)
2586 ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE;
2588 memset(&src_ip, 0, sizeof(src_ip));
2589 src_ip.proto = pg->key.addr.proto;
2590 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2591 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2592 ent = br_multicast_find_group_src(pg, &src_ip);
2594 ent->flags &= ~BR_SGRP_F_DELETE;
2596 ent = br_multicast_new_group_src(pg, &src_ip);
2598 __grp_src_mod_timer(ent, pg->timer.expires);
2602 if (ent && timer_pending(&ent->timer)) {
2603 ent->flags |= BR_SGRP_F_SEND;
2608 if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2612 if (__grp_src_delete_marked(pg))
2615 __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
2620 static bool br_multicast_toex(struct net_bridge_mcast *brmctx,
2621 struct net_bridge_mcast_port *pmctx,
2622 struct net_bridge_port_group *pg, void *h_addr,
2623 void *srcs, u32 nsrcs, size_t addr_size,
2626 bool changed = false;
2628 switch (pg->filter_mode) {
2630 __grp_src_toex_incl(brmctx, pmctx, pg, h_addr, srcs, nsrcs,
2631 addr_size, grec_type);
2632 br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE);
2636 changed = __grp_src_toex_excl(brmctx, pmctx, pg, h_addr, srcs,
2637 nsrcs, addr_size, grec_type);
2641 pg->filter_mode = MCAST_EXCLUDE;
2642 mod_timer(&pg->timer, jiffies + br_multicast_gmi(brmctx));
2647 /* State Msg type New state Actions
2648 * INCLUDE (A) BLOCK (B) INCLUDE (A) Send Q(G,A*B)
2650 static bool __grp_src_block_incl(struct net_bridge_mcast *brmctx,
2651 struct net_bridge_mcast_port *pmctx,
2652 struct net_bridge_port_group *pg, void *h_addr,
2653 void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2655 struct net_bridge_group_src *ent;
2656 u32 src_idx, to_send = 0;
2657 bool changed = false;
2658 struct br_ip src_ip;
2660 hlist_for_each_entry(ent, &pg->src_list, node)
2661 ent->flags &= ~BR_SGRP_F_SEND;
2663 memset(&src_ip, 0, sizeof(src_ip));
2664 src_ip.proto = pg->key.addr.proto;
2665 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2666 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2667 ent = br_multicast_find_group_src(pg, &src_ip);
2669 ent->flags |= BR_SGRP_F_SEND;
2674 if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2679 __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
2684 /* State Msg type New state Actions
2685 * EXCLUDE (X,Y) BLOCK (A) EXCLUDE (X+(A-Y),Y) (A-X-Y)=Group Timer
2688 static bool __grp_src_block_excl(struct net_bridge_mcast *brmctx,
2689 struct net_bridge_mcast_port *pmctx,
2690 struct net_bridge_port_group *pg, void *h_addr,
2691 void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2693 struct net_bridge_group_src *ent;
2694 u32 src_idx, to_send = 0;
2695 bool changed = false;
2696 struct br_ip src_ip;
2698 hlist_for_each_entry(ent, &pg->src_list, node)
2699 ent->flags &= ~BR_SGRP_F_SEND;
2701 memset(&src_ip, 0, sizeof(src_ip));
2702 src_ip.proto = pg->key.addr.proto;
2703 for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2704 memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2705 ent = br_multicast_find_group_src(pg, &src_ip);
2707 ent = br_multicast_new_group_src(pg, &src_ip);
2709 __grp_src_mod_timer(ent, pg->timer.expires);
2713 if (ent && timer_pending(&ent->timer)) {
2714 ent->flags |= BR_SGRP_F_SEND;
2719 if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2724 __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
2729 static bool br_multicast_block(struct net_bridge_mcast *brmctx,
2730 struct net_bridge_mcast_port *pmctx,
2731 struct net_bridge_port_group *pg, void *h_addr,
2732 void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2734 bool changed = false;
2736 switch (pg->filter_mode) {
2738 changed = __grp_src_block_incl(brmctx, pmctx, pg, h_addr, srcs,
2739 nsrcs, addr_size, grec_type);
2742 changed = __grp_src_block_excl(brmctx, pmctx, pg, h_addr, srcs,
2743 nsrcs, addr_size, grec_type);
2747 if ((pg->filter_mode == MCAST_INCLUDE && hlist_empty(&pg->src_list)) ||
2748 br_multicast_eht_should_del_pg(pg)) {
2749 if (br_multicast_eht_should_del_pg(pg))
2750 pg->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2751 br_multicast_find_del_pg(pg->key.port->br, pg);
2752 /* a notification has already been sent and we shouldn't
2753 * access pg after the delete so we have to return false
2761 static struct net_bridge_port_group *
2762 br_multicast_find_port(struct net_bridge_mdb_entry *mp,
2763 struct net_bridge_port *p,
2764 const unsigned char *src)
2766 struct net_bridge *br __maybe_unused = mp->br;
2767 struct net_bridge_port_group *pg;
2769 for (pg = mlock_dereference(mp->ports, br);
2771 pg = mlock_dereference(pg->next, br))
2772 if (br_port_group_equal(pg, p, src))
2778 static int br_ip4_multicast_igmp3_report(struct net_bridge_mcast *brmctx,
2779 struct net_bridge_mcast_port *pmctx,
2780 struct sk_buff *skb,
2783 bool igmpv2 = brmctx->multicast_igmp_version == 2;
2784 struct net_bridge_mdb_entry *mdst;
2785 struct net_bridge_port_group *pg;
2786 const unsigned char *src;
2787 struct igmpv3_report *ih;
2788 struct igmpv3_grec *grec;
2789 int i, len, num, type;
2790 __be32 group, *h_addr;
2791 bool changed = false;
2795 ih = igmpv3_report_hdr(skb);
2796 num = ntohs(ih->ngrec);
2797 len = skb_transport_offset(skb) + sizeof(*ih);
2799 for (i = 0; i < num; i++) {
2800 len += sizeof(*grec);
2801 if (!ip_mc_may_pull(skb, len))
2804 grec = (void *)(skb->data + len - sizeof(*grec));
2805 group = grec->grec_mca;
2806 type = grec->grec_type;
2807 nsrcs = ntohs(grec->grec_nsrcs);
2810 if (!ip_mc_may_pull(skb, len))
2814 case IGMPV3_MODE_IS_INCLUDE:
2815 case IGMPV3_MODE_IS_EXCLUDE:
2816 case IGMPV3_CHANGE_TO_INCLUDE:
2817 case IGMPV3_CHANGE_TO_EXCLUDE:
2818 case IGMPV3_ALLOW_NEW_SOURCES:
2819 case IGMPV3_BLOCK_OLD_SOURCES:
2826 src = eth_hdr(skb)->h_source;
2828 (type == IGMPV3_CHANGE_TO_INCLUDE ||
2829 type == IGMPV3_MODE_IS_INCLUDE)) {
2830 if (!pmctx || igmpv2) {
2831 br_ip4_multicast_leave_group(brmctx, pmctx,
2836 err = br_ip4_multicast_add_group(brmctx, pmctx, group,
2842 if (!pmctx || igmpv2)
2845 spin_lock(&brmctx->br->multicast_lock);
2846 if (!br_multicast_ctx_should_use(brmctx, pmctx))
2847 goto unlock_continue;
2849 mdst = br_mdb_ip4_get(brmctx->br, group, vid);
2851 goto unlock_continue;
2852 pg = br_multicast_find_port(mdst, pmctx->port, src);
2853 if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
2854 goto unlock_continue;
2855 /* reload grec and host addr */
2856 grec = (void *)(skb->data + len - sizeof(*grec) - (nsrcs * 4));
2857 h_addr = &ip_hdr(skb)->saddr;
2859 case IGMPV3_ALLOW_NEW_SOURCES:
2860 changed = br_multicast_isinc_allow(brmctx, pg, h_addr,
2862 nsrcs, sizeof(__be32), type);
2864 case IGMPV3_MODE_IS_INCLUDE:
2865 changed = br_multicast_isinc_allow(brmctx, pg, h_addr,
2867 nsrcs, sizeof(__be32), type);
2869 case IGMPV3_MODE_IS_EXCLUDE:
2870 changed = br_multicast_isexc(brmctx, pg, h_addr,
2872 nsrcs, sizeof(__be32), type);
2874 case IGMPV3_CHANGE_TO_INCLUDE:
2875 changed = br_multicast_toin(brmctx, pmctx, pg, h_addr,
2877 nsrcs, sizeof(__be32), type);
2879 case IGMPV3_CHANGE_TO_EXCLUDE:
2880 changed = br_multicast_toex(brmctx, pmctx, pg, h_addr,
2882 nsrcs, sizeof(__be32), type);
2884 case IGMPV3_BLOCK_OLD_SOURCES:
2885 changed = br_multicast_block(brmctx, pmctx, pg, h_addr,
2887 nsrcs, sizeof(__be32), type);
2891 br_mdb_notify(brmctx->br->dev, mdst, pg, RTM_NEWMDB);
2893 spin_unlock(&brmctx->br->multicast_lock);
2899 #if IS_ENABLED(CONFIG_IPV6)
2900 static int br_ip6_multicast_mld2_report(struct net_bridge_mcast *brmctx,
2901 struct net_bridge_mcast_port *pmctx,
2902 struct sk_buff *skb,
2905 bool mldv1 = brmctx->multicast_mld_version == 1;
2906 struct net_bridge_mdb_entry *mdst;
2907 struct net_bridge_port_group *pg;
2908 unsigned int nsrcs_offset;
2909 struct mld2_report *mld2r;
2910 const unsigned char *src;
2911 struct in6_addr *h_addr;
2912 struct mld2_grec *grec;
2913 unsigned int grec_len;
2914 bool changed = false;
2918 if (!ipv6_mc_may_pull(skb, sizeof(*mld2r)))
2921 mld2r = (struct mld2_report *)icmp6_hdr(skb);
2922 num = ntohs(mld2r->mld2r_ngrec);
2923 len = skb_transport_offset(skb) + sizeof(*mld2r);
2925 for (i = 0; i < num; i++) {
2926 __be16 *_nsrcs, __nsrcs;
2929 nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs);
2931 if (skb_transport_offset(skb) + ipv6_transport_len(skb) <
2932 nsrcs_offset + sizeof(__nsrcs))
2935 _nsrcs = skb_header_pointer(skb, nsrcs_offset,
2936 sizeof(__nsrcs), &__nsrcs);
2940 nsrcs = ntohs(*_nsrcs);
2941 grec_len = struct_size(grec, grec_src, nsrcs);
2943 if (!ipv6_mc_may_pull(skb, len + grec_len))
2946 grec = (struct mld2_grec *)(skb->data + len);
2949 switch (grec->grec_type) {
2950 case MLD2_MODE_IS_INCLUDE:
2951 case MLD2_MODE_IS_EXCLUDE:
2952 case MLD2_CHANGE_TO_INCLUDE:
2953 case MLD2_CHANGE_TO_EXCLUDE:
2954 case MLD2_ALLOW_NEW_SOURCES:
2955 case MLD2_BLOCK_OLD_SOURCES:
2962 src = eth_hdr(skb)->h_source;
2963 if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
2964 grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
2966 if (!pmctx || mldv1) {
2967 br_ip6_multicast_leave_group(brmctx, pmctx,
2973 err = br_ip6_multicast_add_group(brmctx, pmctx,
2974 &grec->grec_mca, vid,
2980 if (!pmctx || mldv1)
2983 spin_lock(&brmctx->br->multicast_lock);
2984 if (!br_multicast_ctx_should_use(brmctx, pmctx))
2985 goto unlock_continue;
2987 mdst = br_mdb_ip6_get(brmctx->br, &grec->grec_mca, vid);
2989 goto unlock_continue;
2990 pg = br_multicast_find_port(mdst, pmctx->port, src);
2991 if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
2992 goto unlock_continue;
2993 h_addr = &ipv6_hdr(skb)->saddr;
2994 switch (grec->grec_type) {
2995 case MLD2_ALLOW_NEW_SOURCES:
2996 changed = br_multicast_isinc_allow(brmctx, pg, h_addr,
2997 grec->grec_src, nsrcs,
2998 sizeof(struct in6_addr),
3001 case MLD2_MODE_IS_INCLUDE:
3002 changed = br_multicast_isinc_allow(brmctx, pg, h_addr,
3003 grec->grec_src, nsrcs,
3004 sizeof(struct in6_addr),
3007 case MLD2_MODE_IS_EXCLUDE:
3008 changed = br_multicast_isexc(brmctx, pg, h_addr,
3009 grec->grec_src, nsrcs,
3010 sizeof(struct in6_addr),
3013 case MLD2_CHANGE_TO_INCLUDE:
3014 changed = br_multicast_toin(brmctx, pmctx, pg, h_addr,
3015 grec->grec_src, nsrcs,
3016 sizeof(struct in6_addr),
3019 case MLD2_CHANGE_TO_EXCLUDE:
3020 changed = br_multicast_toex(brmctx, pmctx, pg, h_addr,
3021 grec->grec_src, nsrcs,
3022 sizeof(struct in6_addr),
3025 case MLD2_BLOCK_OLD_SOURCES:
3026 changed = br_multicast_block(brmctx, pmctx, pg, h_addr,
3027 grec->grec_src, nsrcs,
3028 sizeof(struct in6_addr),
3033 br_mdb_notify(brmctx->br->dev, mdst, pg, RTM_NEWMDB);
3035 spin_unlock(&brmctx->br->multicast_lock);
3042 static bool br_multicast_select_querier(struct net_bridge_mcast *brmctx,
3043 struct net_bridge_mcast_port *pmctx,
3044 struct br_ip *saddr)
3046 int port_ifidx = pmctx ? pmctx->port->dev->ifindex : 0;
3047 struct timer_list *own_timer, *other_timer;
3048 struct bridge_mcast_querier *querier;
3050 switch (saddr->proto) {
3051 case htons(ETH_P_IP):
3052 querier = &brmctx->ip4_querier;
3053 own_timer = &brmctx->ip4_own_query.timer;
3054 other_timer = &brmctx->ip4_other_query.timer;
3055 if (!querier->addr.src.ip4 ||
3056 ntohl(saddr->src.ip4) <= ntohl(querier->addr.src.ip4))
3059 #if IS_ENABLED(CONFIG_IPV6)
3060 case htons(ETH_P_IPV6):
3061 querier = &brmctx->ip6_querier;
3062 own_timer = &brmctx->ip6_own_query.timer;
3063 other_timer = &brmctx->ip6_other_query.timer;
3064 if (ipv6_addr_cmp(&saddr->src.ip6, &querier->addr.src.ip6) <= 0)
3072 if (!timer_pending(own_timer) && !timer_pending(other_timer))
3078 br_multicast_update_querier(brmctx, querier, port_ifidx, saddr);
3083 static struct net_bridge_port *
3084 __br_multicast_get_querier_port(struct net_bridge *br,
3085 const struct bridge_mcast_querier *querier)
3087 int port_ifidx = READ_ONCE(querier->port_ifidx);
3088 struct net_bridge_port *p;
3089 struct net_device *dev;
3091 if (port_ifidx == 0)
3094 dev = dev_get_by_index_rcu(dev_net(br->dev), port_ifidx);
3097 p = br_port_get_rtnl_rcu(dev);
3098 if (!p || p->br != br)
3104 size_t br_multicast_querier_state_size(void)
3106 return nla_total_size(0) + /* nest attribute */
3107 nla_total_size(sizeof(__be32)) + /* BRIDGE_QUERIER_IP_ADDRESS */
3108 nla_total_size(sizeof(int)) + /* BRIDGE_QUERIER_IP_PORT */
3109 nla_total_size_64bit(sizeof(u64)) + /* BRIDGE_QUERIER_IP_OTHER_TIMER */
3110 #if IS_ENABLED(CONFIG_IPV6)
3111 nla_total_size(sizeof(struct in6_addr)) + /* BRIDGE_QUERIER_IPV6_ADDRESS */
3112 nla_total_size(sizeof(int)) + /* BRIDGE_QUERIER_IPV6_PORT */
3113 nla_total_size_64bit(sizeof(u64)) + /* BRIDGE_QUERIER_IPV6_OTHER_TIMER */
3118 /* protected by rtnl or rcu */
3119 int br_multicast_dump_querier_state(struct sk_buff *skb,
3120 const struct net_bridge_mcast *brmctx,
3123 struct bridge_mcast_querier querier = {};
3124 struct net_bridge_port *p;
3125 struct nlattr *nest;
3127 if (!br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED) ||
3128 br_multicast_ctx_vlan_global_disabled(brmctx))
3131 nest = nla_nest_start(skb, nest_attr);
3136 if (!brmctx->multicast_querier &&
3137 !timer_pending(&brmctx->ip4_other_query.timer))
3140 br_multicast_read_querier(&brmctx->ip4_querier, &querier);
3141 if (nla_put_in_addr(skb, BRIDGE_QUERIER_IP_ADDRESS,
3142 querier.addr.src.ip4)) {
3147 p = __br_multicast_get_querier_port(brmctx->br, &querier);
3148 if (timer_pending(&brmctx->ip4_other_query.timer) &&
3149 (nla_put_u64_64bit(skb, BRIDGE_QUERIER_IP_OTHER_TIMER,
3150 br_timer_value(&brmctx->ip4_other_query.timer),
3151 BRIDGE_QUERIER_PAD) ||
3152 (p && nla_put_u32(skb, BRIDGE_QUERIER_IP_PORT, p->dev->ifindex)))) {
3158 #if IS_ENABLED(CONFIG_IPV6)
3159 if (!brmctx->multicast_querier &&
3160 !timer_pending(&brmctx->ip6_other_query.timer))
3163 br_multicast_read_querier(&brmctx->ip6_querier, &querier);
3164 if (nla_put_in6_addr(skb, BRIDGE_QUERIER_IPV6_ADDRESS,
3165 &querier.addr.src.ip6)) {
3170 p = __br_multicast_get_querier_port(brmctx->br, &querier);
3171 if (timer_pending(&brmctx->ip6_other_query.timer) &&
3172 (nla_put_u64_64bit(skb, BRIDGE_QUERIER_IPV6_OTHER_TIMER,
3173 br_timer_value(&brmctx->ip6_other_query.timer),
3174 BRIDGE_QUERIER_PAD) ||
3175 (p && nla_put_u32(skb, BRIDGE_QUERIER_IPV6_PORT,
3176 p->dev->ifindex)))) {
3183 nla_nest_end(skb, nest);
3185 nla_nest_cancel(skb, nest);
3190 nla_nest_cancel(skb, nest);
3195 br_multicast_update_query_timer(struct net_bridge_mcast *brmctx,
3196 struct bridge_mcast_other_query *query,
3197 unsigned long max_delay)
3199 if (!timer_pending(&query->timer))
3200 query->delay_time = jiffies + max_delay;
3202 mod_timer(&query->timer, jiffies + brmctx->multicast_querier_interval);
3205 static void br_port_mc_router_state_change(struct net_bridge_port *p,
3208 struct switchdev_attr attr = {
3210 .id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
3211 .flags = SWITCHDEV_F_DEFER,
3212 .u.mrouter = is_mc_router,
3215 switchdev_port_attr_set(p->dev, &attr, NULL);
3218 static struct net_bridge_port *
3219 br_multicast_rport_from_node(struct net_bridge_mcast *brmctx,
3220 struct hlist_head *mc_router_list,
3221 struct hlist_node *rlist)
3223 struct net_bridge_mcast_port *pmctx;
3225 #if IS_ENABLED(CONFIG_IPV6)
3226 if (mc_router_list == &brmctx->ip6_mc_router_list)
3227 pmctx = hlist_entry(rlist, struct net_bridge_mcast_port,
3231 pmctx = hlist_entry(rlist, struct net_bridge_mcast_port,
3237 static struct hlist_node *
3238 br_multicast_get_rport_slot(struct net_bridge_mcast *brmctx,
3239 struct net_bridge_port *port,
3240 struct hlist_head *mc_router_list)
3243 struct hlist_node *slot = NULL;
3244 struct net_bridge_port *p;
3245 struct hlist_node *rlist;
3247 hlist_for_each(rlist, mc_router_list) {
3248 p = br_multicast_rport_from_node(brmctx, mc_router_list, rlist);
3250 if ((unsigned long)port >= (unsigned long)p)
3259 static bool br_multicast_no_router_otherpf(struct net_bridge_mcast_port *pmctx,
3260 struct hlist_node *rnode)
3262 #if IS_ENABLED(CONFIG_IPV6)
3263 if (rnode != &pmctx->ip6_rlist)
3264 return hlist_unhashed(&pmctx->ip6_rlist);
3266 return hlist_unhashed(&pmctx->ip4_rlist);
3272 /* Add port to router_list
3273 * list is maintained ordered by pointer value
3274 * and locked by br->multicast_lock and RCU
3276 static void br_multicast_add_router(struct net_bridge_mcast *brmctx,
3277 struct net_bridge_mcast_port *pmctx,
3278 struct hlist_node *rlist,
3279 struct hlist_head *mc_router_list)
3281 struct hlist_node *slot;
3283 if (!hlist_unhashed(rlist))
3286 slot = br_multicast_get_rport_slot(brmctx, pmctx->port, mc_router_list);
3289 hlist_add_behind_rcu(rlist, slot);
3291 hlist_add_head_rcu(rlist, mc_router_list);
3293 /* For backwards compatibility for now, only notify if we
3294 * switched from no IPv4/IPv6 multicast router to a new
3295 * IPv4 or IPv6 multicast router.
3297 if (br_multicast_no_router_otherpf(pmctx, rlist)) {
3298 br_rtr_notify(pmctx->port->br->dev, pmctx, RTM_NEWMDB);
3299 br_port_mc_router_state_change(pmctx->port, true);
3303 /* Add port to router_list
3304 * list is maintained ordered by pointer value
3305 * and locked by br->multicast_lock and RCU
3307 static void br_ip4_multicast_add_router(struct net_bridge_mcast *brmctx,
3308 struct net_bridge_mcast_port *pmctx)
3310 br_multicast_add_router(brmctx, pmctx, &pmctx->ip4_rlist,
3311 &brmctx->ip4_mc_router_list);
3314 /* Add port to router_list
3315 * list is maintained ordered by pointer value
3316 * and locked by br->multicast_lock and RCU
3318 static void br_ip6_multicast_add_router(struct net_bridge_mcast *brmctx,
3319 struct net_bridge_mcast_port *pmctx)
3321 #if IS_ENABLED(CONFIG_IPV6)
3322 br_multicast_add_router(brmctx, pmctx, &pmctx->ip6_rlist,
3323 &brmctx->ip6_mc_router_list);
3327 static void br_multicast_mark_router(struct net_bridge_mcast *brmctx,
3328 struct net_bridge_mcast_port *pmctx,
3329 struct timer_list *timer,
3330 struct hlist_node *rlist,
3331 struct hlist_head *mc_router_list)
3333 unsigned long now = jiffies;
3335 if (!br_multicast_ctx_should_use(brmctx, pmctx))
3339 if (brmctx->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
3340 if (!br_ip4_multicast_is_router(brmctx) &&
3341 !br_ip6_multicast_is_router(brmctx))
3342 br_mc_router_state_change(brmctx->br, true);
3343 mod_timer(timer, now + brmctx->multicast_querier_interval);
3348 if (pmctx->multicast_router == MDB_RTR_TYPE_DISABLED ||
3349 pmctx->multicast_router == MDB_RTR_TYPE_PERM)
3352 br_multicast_add_router(brmctx, pmctx, rlist, mc_router_list);
3353 mod_timer(timer, now + brmctx->multicast_querier_interval);
3356 static void br_ip4_multicast_mark_router(struct net_bridge_mcast *brmctx,
3357 struct net_bridge_mcast_port *pmctx)
3359 struct timer_list *timer = &brmctx->ip4_mc_router_timer;
3360 struct hlist_node *rlist = NULL;
3363 timer = &pmctx->ip4_mc_router_timer;
3364 rlist = &pmctx->ip4_rlist;
3367 br_multicast_mark_router(brmctx, pmctx, timer, rlist,
3368 &brmctx->ip4_mc_router_list);
3371 static void br_ip6_multicast_mark_router(struct net_bridge_mcast *brmctx,
3372 struct net_bridge_mcast_port *pmctx)
3374 #if IS_ENABLED(CONFIG_IPV6)
3375 struct timer_list *timer = &brmctx->ip6_mc_router_timer;
3376 struct hlist_node *rlist = NULL;
3379 timer = &pmctx->ip6_mc_router_timer;
3380 rlist = &pmctx->ip6_rlist;
3383 br_multicast_mark_router(brmctx, pmctx, timer, rlist,
3384 &brmctx->ip6_mc_router_list);
3389 br_ip4_multicast_query_received(struct net_bridge_mcast *brmctx,
3390 struct net_bridge_mcast_port *pmctx,
3391 struct bridge_mcast_other_query *query,
3392 struct br_ip *saddr,
3393 unsigned long max_delay)
3395 if (!br_multicast_select_querier(brmctx, pmctx, saddr))
3398 br_multicast_update_query_timer(brmctx, query, max_delay);
3399 br_ip4_multicast_mark_router(brmctx, pmctx);
3402 #if IS_ENABLED(CONFIG_IPV6)
3404 br_ip6_multicast_query_received(struct net_bridge_mcast *brmctx,
3405 struct net_bridge_mcast_port *pmctx,
3406 struct bridge_mcast_other_query *query,
3407 struct br_ip *saddr,
3408 unsigned long max_delay)
3410 if (!br_multicast_select_querier(brmctx, pmctx, saddr))
3413 br_multicast_update_query_timer(brmctx, query, max_delay);
3414 br_ip6_multicast_mark_router(brmctx, pmctx);
3418 static void br_ip4_multicast_query(struct net_bridge_mcast *brmctx,
3419 struct net_bridge_mcast_port *pmctx,
3420 struct sk_buff *skb,
3423 unsigned int transport_len = ip_transport_len(skb);
3424 const struct iphdr *iph = ip_hdr(skb);
3425 struct igmphdr *ih = igmp_hdr(skb);
3426 struct net_bridge_mdb_entry *mp;
3427 struct igmpv3_query *ih3;
3428 struct net_bridge_port_group *p;
3429 struct net_bridge_port_group __rcu **pp;
3430 struct br_ip saddr = {};
3431 unsigned long max_delay;
3432 unsigned long now = jiffies;
3435 spin_lock(&brmctx->br->multicast_lock);
3436 if (!br_multicast_ctx_should_use(brmctx, pmctx))
3441 if (transport_len == sizeof(*ih)) {
3442 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
3445 max_delay = 10 * HZ;
3448 } else if (transport_len >= sizeof(*ih3)) {
3449 ih3 = igmpv3_query_hdr(skb);
3451 (brmctx->multicast_igmp_version == 3 && group &&
3455 max_delay = ih3->code ?
3456 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
3462 saddr.proto = htons(ETH_P_IP);
3463 saddr.src.ip4 = iph->saddr;
3465 br_ip4_multicast_query_received(brmctx, pmctx,
3466 &brmctx->ip4_other_query,
3471 mp = br_mdb_ip4_get(brmctx->br, group, vid);
3475 max_delay *= brmctx->multicast_last_member_count;
3477 if (mp->host_joined &&
3478 (timer_pending(&mp->timer) ?
3479 time_after(mp->timer.expires, now + max_delay) :
3480 try_to_del_timer_sync(&mp->timer) >= 0))
3481 mod_timer(&mp->timer, now + max_delay);
3483 for (pp = &mp->ports;
3484 (p = mlock_dereference(*pp, brmctx->br)) != NULL;
3486 if (timer_pending(&p->timer) ?
3487 time_after(p->timer.expires, now + max_delay) :
3488 try_to_del_timer_sync(&p->timer) >= 0 &&
3489 (brmctx->multicast_igmp_version == 2 ||
3490 p->filter_mode == MCAST_EXCLUDE))
3491 mod_timer(&p->timer, now + max_delay);
3495 spin_unlock(&brmctx->br->multicast_lock);
3498 #if IS_ENABLED(CONFIG_IPV6)
3499 static int br_ip6_multicast_query(struct net_bridge_mcast *brmctx,
3500 struct net_bridge_mcast_port *pmctx,
3501 struct sk_buff *skb,
3504 unsigned int transport_len = ipv6_transport_len(skb);
3505 struct mld_msg *mld;
3506 struct net_bridge_mdb_entry *mp;
3507 struct mld2_query *mld2q;
3508 struct net_bridge_port_group *p;
3509 struct net_bridge_port_group __rcu **pp;
3510 struct br_ip saddr = {};
3511 unsigned long max_delay;
3512 unsigned long now = jiffies;
3513 unsigned int offset = skb_transport_offset(skb);
3514 const struct in6_addr *group = NULL;
3515 bool is_general_query;
3518 spin_lock(&brmctx->br->multicast_lock);
3519 if (!br_multicast_ctx_should_use(brmctx, pmctx))
3522 if (transport_len == sizeof(*mld)) {
3523 if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
3527 mld = (struct mld_msg *) icmp6_hdr(skb);
3528 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
3530 group = &mld->mld_mca;
3532 if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
3536 mld2q = (struct mld2_query *)icmp6_hdr(skb);
3537 if (!mld2q->mld2q_nsrcs)
3538 group = &mld2q->mld2q_mca;
3539 if (brmctx->multicast_mld_version == 2 &&
3540 !ipv6_addr_any(&mld2q->mld2q_mca) &&
3541 mld2q->mld2q_suppress)
3544 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
3547 is_general_query = group && ipv6_addr_any(group);
3549 if (is_general_query) {
3550 saddr.proto = htons(ETH_P_IPV6);
3551 saddr.src.ip6 = ipv6_hdr(skb)->saddr;
3553 br_ip6_multicast_query_received(brmctx, pmctx,
3554 &brmctx->ip6_other_query,
3557 } else if (!group) {
3561 mp = br_mdb_ip6_get(brmctx->br, group, vid);
3565 max_delay *= brmctx->multicast_last_member_count;
3566 if (mp->host_joined &&
3567 (timer_pending(&mp->timer) ?
3568 time_after(mp->timer.expires, now + max_delay) :
3569 try_to_del_timer_sync(&mp->timer) >= 0))
3570 mod_timer(&mp->timer, now + max_delay);
3572 for (pp = &mp->ports;
3573 (p = mlock_dereference(*pp, brmctx->br)) != NULL;
3575 if (timer_pending(&p->timer) ?
3576 time_after(p->timer.expires, now + max_delay) :
3577 try_to_del_timer_sync(&p->timer) >= 0 &&
3578 (brmctx->multicast_mld_version == 1 ||
3579 p->filter_mode == MCAST_EXCLUDE))
3580 mod_timer(&p->timer, now + max_delay);
3584 spin_unlock(&brmctx->br->multicast_lock);
3590 br_multicast_leave_group(struct net_bridge_mcast *brmctx,
3591 struct net_bridge_mcast_port *pmctx,
3592 struct br_ip *group,
3593 struct bridge_mcast_other_query *other_query,
3594 struct bridge_mcast_own_query *own_query,
3595 const unsigned char *src)
3597 struct net_bridge_mdb_entry *mp;
3598 struct net_bridge_port_group *p;
3602 spin_lock(&brmctx->br->multicast_lock);
3603 if (!br_multicast_ctx_should_use(brmctx, pmctx))
3606 mp = br_mdb_ip_get(brmctx->br, group);
3610 if (pmctx && (pmctx->port->flags & BR_MULTICAST_FAST_LEAVE)) {
3611 struct net_bridge_port_group __rcu **pp;
3613 for (pp = &mp->ports;
3614 (p = mlock_dereference(*pp, brmctx->br)) != NULL;
3616 if (!br_port_group_equal(p, pmctx->port, src))
3619 if (p->flags & MDB_PG_FLAGS_PERMANENT)
3622 p->flags |= MDB_PG_FLAGS_FAST_LEAVE;
3623 br_multicast_del_pg(mp, p, pp);
3628 if (timer_pending(&other_query->timer))
3631 if (brmctx->multicast_querier) {
3632 __br_multicast_send_query(brmctx, pmctx, NULL, NULL, &mp->addr,
3635 time = jiffies + brmctx->multicast_last_member_count *
3636 brmctx->multicast_last_member_interval;
3638 mod_timer(&own_query->timer, time);
3640 for (p = mlock_dereference(mp->ports, brmctx->br);
3641 p != NULL && pmctx != NULL;
3642 p = mlock_dereference(p->next, brmctx->br)) {
3643 if (!br_port_group_equal(p, pmctx->port, src))
3646 if (!hlist_unhashed(&p->mglist) &&
3647 (timer_pending(&p->timer) ?
3648 time_after(p->timer.expires, time) :
3649 try_to_del_timer_sync(&p->timer) >= 0)) {
3650 mod_timer(&p->timer, time);
3658 time = now + brmctx->multicast_last_member_count *
3659 brmctx->multicast_last_member_interval;
3662 if (mp->host_joined &&
3663 (timer_pending(&mp->timer) ?
3664 time_after(mp->timer.expires, time) :
3665 try_to_del_timer_sync(&mp->timer) >= 0)) {
3666 mod_timer(&mp->timer, time);
3672 for (p = mlock_dereference(mp->ports, brmctx->br);
3674 p = mlock_dereference(p->next, brmctx->br)) {
3675 if (p->key.port != pmctx->port)
3678 if (!hlist_unhashed(&p->mglist) &&
3679 (timer_pending(&p->timer) ?
3680 time_after(p->timer.expires, time) :
3681 try_to_del_timer_sync(&p->timer) >= 0)) {
3682 mod_timer(&p->timer, time);
3688 spin_unlock(&brmctx->br->multicast_lock);
3691 static void br_ip4_multicast_leave_group(struct net_bridge_mcast *brmctx,
3692 struct net_bridge_mcast_port *pmctx,
3695 const unsigned char *src)
3697 struct br_ip br_group;
3698 struct bridge_mcast_own_query *own_query;
3700 if (ipv4_is_local_multicast(group))
3703 own_query = pmctx ? &pmctx->ip4_own_query : &brmctx->ip4_own_query;
3705 memset(&br_group, 0, sizeof(br_group));
3706 br_group.dst.ip4 = group;
3707 br_group.proto = htons(ETH_P_IP);
3710 br_multicast_leave_group(brmctx, pmctx, &br_group,
3711 &brmctx->ip4_other_query,
3715 #if IS_ENABLED(CONFIG_IPV6)
3716 static void br_ip6_multicast_leave_group(struct net_bridge_mcast *brmctx,
3717 struct net_bridge_mcast_port *pmctx,
3718 const struct in6_addr *group,
3720 const unsigned char *src)
3722 struct br_ip br_group;
3723 struct bridge_mcast_own_query *own_query;
3725 if (ipv6_addr_is_ll_all_nodes(group))
3728 own_query = pmctx ? &pmctx->ip6_own_query : &brmctx->ip6_own_query;
3730 memset(&br_group, 0, sizeof(br_group));
3731 br_group.dst.ip6 = *group;
3732 br_group.proto = htons(ETH_P_IPV6);
3735 br_multicast_leave_group(brmctx, pmctx, &br_group,
3736 &brmctx->ip6_other_query,
3741 static void br_multicast_err_count(const struct net_bridge *br,
3742 const struct net_bridge_port *p,
3745 struct bridge_mcast_stats __percpu *stats;
3746 struct bridge_mcast_stats *pstats;
3748 if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
3752 stats = p->mcast_stats;
3754 stats = br->mcast_stats;
3755 if (WARN_ON(!stats))
3758 pstats = this_cpu_ptr(stats);
3760 u64_stats_update_begin(&pstats->syncp);
3762 case htons(ETH_P_IP):
3763 pstats->mstats.igmp_parse_errors++;
3765 #if IS_ENABLED(CONFIG_IPV6)
3766 case htons(ETH_P_IPV6):
3767 pstats->mstats.mld_parse_errors++;
3771 u64_stats_update_end(&pstats->syncp);
3774 static void br_multicast_pim(struct net_bridge_mcast *brmctx,
3775 struct net_bridge_mcast_port *pmctx,
3776 const struct sk_buff *skb)
3778 unsigned int offset = skb_transport_offset(skb);
3779 struct pimhdr *pimhdr, _pimhdr;
3781 pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
3782 if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
3783 pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
3786 spin_lock(&brmctx->br->multicast_lock);
3787 br_ip4_multicast_mark_router(brmctx, pmctx);
3788 spin_unlock(&brmctx->br->multicast_lock);
3791 static int br_ip4_multicast_mrd_rcv(struct net_bridge_mcast *brmctx,
3792 struct net_bridge_mcast_port *pmctx,
3793 struct sk_buff *skb)
3795 if (ip_hdr(skb)->protocol != IPPROTO_IGMP ||
3796 igmp_hdr(skb)->type != IGMP_MRDISC_ADV)
3799 spin_lock(&brmctx->br->multicast_lock);
3800 br_ip4_multicast_mark_router(brmctx, pmctx);
3801 spin_unlock(&brmctx->br->multicast_lock);
3806 static int br_multicast_ipv4_rcv(struct net_bridge_mcast *brmctx,
3807 struct net_bridge_mcast_port *pmctx,
3808 struct sk_buff *skb,
3811 struct net_bridge_port *p = pmctx ? pmctx->port : NULL;
3812 const unsigned char *src;
3816 err = ip_mc_check_igmp(skb);
3818 if (err == -ENOMSG) {
3819 if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
3820 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
3821 } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
3822 if (ip_hdr(skb)->protocol == IPPROTO_PIM)
3823 br_multicast_pim(brmctx, pmctx, skb);
3824 } else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) {
3825 br_ip4_multicast_mrd_rcv(brmctx, pmctx, skb);
3829 } else if (err < 0) {
3830 br_multicast_err_count(brmctx->br, p, skb->protocol);
3835 src = eth_hdr(skb)->h_source;
3836 BR_INPUT_SKB_CB(skb)->igmp = ih->type;
3839 case IGMP_HOST_MEMBERSHIP_REPORT:
3840 case IGMPV2_HOST_MEMBERSHIP_REPORT:
3841 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
3842 err = br_ip4_multicast_add_group(brmctx, pmctx, ih->group, vid,
3845 case IGMPV3_HOST_MEMBERSHIP_REPORT:
3846 err = br_ip4_multicast_igmp3_report(brmctx, pmctx, skb, vid);
3848 case IGMP_HOST_MEMBERSHIP_QUERY:
3849 br_ip4_multicast_query(brmctx, pmctx, skb, vid);
3851 case IGMP_HOST_LEAVE_MESSAGE:
3852 br_ip4_multicast_leave_group(brmctx, pmctx, ih->group, vid, src);
3856 br_multicast_count(brmctx->br, p, skb, BR_INPUT_SKB_CB(skb)->igmp,
3862 #if IS_ENABLED(CONFIG_IPV6)
3863 static void br_ip6_multicast_mrd_rcv(struct net_bridge_mcast *brmctx,
3864 struct net_bridge_mcast_port *pmctx,
3865 struct sk_buff *skb)
3867 if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV)
3870 spin_lock(&brmctx->br->multicast_lock);
3871 br_ip6_multicast_mark_router(brmctx, pmctx);
3872 spin_unlock(&brmctx->br->multicast_lock);
3875 static int br_multicast_ipv6_rcv(struct net_bridge_mcast *brmctx,
3876 struct net_bridge_mcast_port *pmctx,
3877 struct sk_buff *skb,
3880 struct net_bridge_port *p = pmctx ? pmctx->port : NULL;
3881 const unsigned char *src;
3882 struct mld_msg *mld;
3885 err = ipv6_mc_check_mld(skb);
3887 if (err == -ENOMSG || err == -ENODATA) {
3888 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
3889 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
3890 if (err == -ENODATA &&
3891 ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr))
3892 br_ip6_multicast_mrd_rcv(brmctx, pmctx, skb);
3895 } else if (err < 0) {
3896 br_multicast_err_count(brmctx->br, p, skb->protocol);
3900 mld = (struct mld_msg *)skb_transport_header(skb);
3901 BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
3903 switch (mld->mld_type) {
3904 case ICMPV6_MGM_REPORT:
3905 src = eth_hdr(skb)->h_source;
3906 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
3907 err = br_ip6_multicast_add_group(brmctx, pmctx, &mld->mld_mca,
3910 case ICMPV6_MLD2_REPORT:
3911 err = br_ip6_multicast_mld2_report(brmctx, pmctx, skb, vid);
3913 case ICMPV6_MGM_QUERY:
3914 err = br_ip6_multicast_query(brmctx, pmctx, skb, vid);
3916 case ICMPV6_MGM_REDUCTION:
3917 src = eth_hdr(skb)->h_source;
3918 br_ip6_multicast_leave_group(brmctx, pmctx, &mld->mld_mca, vid,
3923 br_multicast_count(brmctx->br, p, skb, BR_INPUT_SKB_CB(skb)->igmp,
3930 int br_multicast_rcv(struct net_bridge_mcast **brmctx,
3931 struct net_bridge_mcast_port **pmctx,
3932 struct net_bridge_vlan *vlan,
3933 struct sk_buff *skb, u16 vid)
3937 BR_INPUT_SKB_CB(skb)->igmp = 0;
3938 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
3940 if (!br_opt_get((*brmctx)->br, BROPT_MULTICAST_ENABLED))
3943 if (br_opt_get((*brmctx)->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) && vlan) {
3944 const struct net_bridge_vlan *masterv;
3946 /* the vlan has the master flag set only when transmitting
3947 * through the bridge device
3949 if (br_vlan_is_master(vlan)) {
3951 *brmctx = &vlan->br_mcast_ctx;
3954 masterv = vlan->brvlan;
3955 *brmctx = &vlan->brvlan->br_mcast_ctx;
3956 *pmctx = &vlan->port_mcast_ctx;
3959 if (!(masterv->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED))
3963 switch (skb->protocol) {
3964 case htons(ETH_P_IP):
3965 ret = br_multicast_ipv4_rcv(*brmctx, *pmctx, skb, vid);
3967 #if IS_ENABLED(CONFIG_IPV6)
3968 case htons(ETH_P_IPV6):
3969 ret = br_multicast_ipv6_rcv(*brmctx, *pmctx, skb, vid);
3977 static void br_multicast_query_expired(struct net_bridge_mcast *brmctx,
3978 struct bridge_mcast_own_query *query,
3979 struct bridge_mcast_querier *querier)
3981 spin_lock(&brmctx->br->multicast_lock);
3982 if (br_multicast_ctx_vlan_disabled(brmctx))
3985 if (query->startup_sent < brmctx->multicast_startup_query_count)
3986 query->startup_sent++;
3988 br_multicast_send_query(brmctx, NULL, query);
3990 spin_unlock(&brmctx->br->multicast_lock);
3993 static void br_ip4_multicast_query_expired(struct timer_list *t)
3995 struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
3996 ip4_own_query.timer);
3998 br_multicast_query_expired(brmctx, &brmctx->ip4_own_query,
3999 &brmctx->ip4_querier);
4002 #if IS_ENABLED(CONFIG_IPV6)
4003 static void br_ip6_multicast_query_expired(struct timer_list *t)
4005 struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
4006 ip6_own_query.timer);
4008 br_multicast_query_expired(brmctx, &brmctx->ip6_own_query,
4009 &brmctx->ip6_querier);
4013 static void br_multicast_gc_work(struct work_struct *work)
4015 struct net_bridge *br = container_of(work, struct net_bridge,
4017 HLIST_HEAD(deleted_head);
4019 spin_lock_bh(&br->multicast_lock);
4020 hlist_move_list(&br->mcast_gc_list, &deleted_head);
4021 spin_unlock_bh(&br->multicast_lock);
4023 br_multicast_gc(&deleted_head);
4026 void br_multicast_ctx_init(struct net_bridge *br,
4027 struct net_bridge_vlan *vlan,
4028 struct net_bridge_mcast *brmctx)
4031 brmctx->vlan = vlan;
4032 brmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
4033 brmctx->multicast_last_member_count = 2;
4034 brmctx->multicast_startup_query_count = 2;
4036 brmctx->multicast_last_member_interval = HZ;
4037 brmctx->multicast_query_response_interval = 10 * HZ;
4038 brmctx->multicast_startup_query_interval = 125 * HZ / 4;
4039 brmctx->multicast_query_interval = 125 * HZ;
4040 brmctx->multicast_querier_interval = 255 * HZ;
4041 brmctx->multicast_membership_interval = 260 * HZ;
4043 brmctx->ip4_other_query.delay_time = 0;
4044 brmctx->ip4_querier.port_ifidx = 0;
4045 seqcount_spinlock_init(&brmctx->ip4_querier.seq, &br->multicast_lock);
4046 brmctx->multicast_igmp_version = 2;
4047 #if IS_ENABLED(CONFIG_IPV6)
4048 brmctx->multicast_mld_version = 1;
4049 brmctx->ip6_other_query.delay_time = 0;
4050 brmctx->ip6_querier.port_ifidx = 0;
4051 seqcount_spinlock_init(&brmctx->ip6_querier.seq, &br->multicast_lock);
4054 timer_setup(&brmctx->ip4_mc_router_timer,
4055 br_ip4_multicast_local_router_expired, 0);
4056 timer_setup(&brmctx->ip4_other_query.timer,
4057 br_ip4_multicast_querier_expired, 0);
4058 timer_setup(&brmctx->ip4_own_query.timer,
4059 br_ip4_multicast_query_expired, 0);
4060 #if IS_ENABLED(CONFIG_IPV6)
4061 timer_setup(&brmctx->ip6_mc_router_timer,
4062 br_ip6_multicast_local_router_expired, 0);
4063 timer_setup(&brmctx->ip6_other_query.timer,
4064 br_ip6_multicast_querier_expired, 0);
4065 timer_setup(&brmctx->ip6_own_query.timer,
4066 br_ip6_multicast_query_expired, 0);
4070 void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx)
4072 __br_multicast_stop(brmctx);
4075 void br_multicast_init(struct net_bridge *br)
4077 br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
4079 br_multicast_ctx_init(br, NULL, &br->multicast_ctx);
4081 br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true);
4082 br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
4084 spin_lock_init(&br->multicast_lock);
4085 INIT_HLIST_HEAD(&br->mdb_list);
4086 INIT_HLIST_HEAD(&br->mcast_gc_list);
4087 INIT_WORK(&br->mcast_gc_work, br_multicast_gc_work);
4090 static void br_ip4_multicast_join_snoopers(struct net_bridge *br)
4092 struct in_device *in_dev = in_dev_get(br->dev);
4097 __ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
4101 #if IS_ENABLED(CONFIG_IPV6)
4102 static void br_ip6_multicast_join_snoopers(struct net_bridge *br)
4104 struct in6_addr addr;
4106 ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
4107 ipv6_dev_mc_inc(br->dev, &addr);
4110 static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br)
4115 void br_multicast_join_snoopers(struct net_bridge *br)
4117 br_ip4_multicast_join_snoopers(br);
4118 br_ip6_multicast_join_snoopers(br);
4121 static void br_ip4_multicast_leave_snoopers(struct net_bridge *br)
4123 struct in_device *in_dev = in_dev_get(br->dev);
4125 if (WARN_ON(!in_dev))
4128 __ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
4132 #if IS_ENABLED(CONFIG_IPV6)
4133 static void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
4135 struct in6_addr addr;
4137 ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
4138 ipv6_dev_mc_dec(br->dev, &addr);
4141 static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
4146 void br_multicast_leave_snoopers(struct net_bridge *br)
4148 br_ip4_multicast_leave_snoopers(br);
4149 br_ip6_multicast_leave_snoopers(br);
4152 static void __br_multicast_open_query(struct net_bridge *br,
4153 struct bridge_mcast_own_query *query)
4155 query->startup_sent = 0;
4157 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
4160 mod_timer(&query->timer, jiffies);
4163 static void __br_multicast_open(struct net_bridge_mcast *brmctx)
4165 __br_multicast_open_query(brmctx->br, &brmctx->ip4_own_query);
4166 #if IS_ENABLED(CONFIG_IPV6)
4167 __br_multicast_open_query(brmctx->br, &brmctx->ip6_own_query);
4171 void br_multicast_open(struct net_bridge *br)
4175 if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) {
4176 struct net_bridge_vlan_group *vg;
4177 struct net_bridge_vlan *vlan;
4179 vg = br_vlan_group(br);
4181 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
4182 struct net_bridge_mcast *brmctx;
4184 brmctx = &vlan->br_mcast_ctx;
4185 if (br_vlan_is_brentry(vlan) &&
4186 !br_multicast_ctx_vlan_disabled(brmctx))
4187 __br_multicast_open(&vlan->br_mcast_ctx);
4191 __br_multicast_open(&br->multicast_ctx);
4195 static void __br_multicast_stop(struct net_bridge_mcast *brmctx)
4197 del_timer_sync(&brmctx->ip4_mc_router_timer);
4198 del_timer_sync(&brmctx->ip4_other_query.timer);
4199 del_timer_sync(&brmctx->ip4_own_query.timer);
4200 #if IS_ENABLED(CONFIG_IPV6)
4201 del_timer_sync(&brmctx->ip6_mc_router_timer);
4202 del_timer_sync(&brmctx->ip6_other_query.timer);
4203 del_timer_sync(&brmctx->ip6_own_query.timer);
4207 void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan, bool on)
4209 struct net_bridge *br;
4211 /* it's okay to check for the flag without the multicast lock because it
4212 * can only change under RTNL -> multicast_lock, we need the latter to
4213 * sync with timers and packets
4215 if (on == !!(vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED))
4218 if (br_vlan_is_master(vlan)) {
4221 if (!br_vlan_is_brentry(vlan) ||
4223 br_multicast_ctx_vlan_global_disabled(&vlan->br_mcast_ctx)))
4226 spin_lock_bh(&br->multicast_lock);
4227 vlan->priv_flags ^= BR_VLFLAG_MCAST_ENABLED;
4228 spin_unlock_bh(&br->multicast_lock);
4231 __br_multicast_open(&vlan->br_mcast_ctx);
4233 __br_multicast_stop(&vlan->br_mcast_ctx);
4235 struct net_bridge_mcast *brmctx;
4237 brmctx = br_multicast_port_ctx_get_global(&vlan->port_mcast_ctx);
4238 if (on && br_multicast_ctx_vlan_global_disabled(brmctx))
4241 br = vlan->port->br;
4242 spin_lock_bh(&br->multicast_lock);
4243 vlan->priv_flags ^= BR_VLFLAG_MCAST_ENABLED;
4245 __br_multicast_enable_port_ctx(&vlan->port_mcast_ctx);
4247 __br_multicast_disable_port_ctx(&vlan->port_mcast_ctx);
4248 spin_unlock_bh(&br->multicast_lock);
4252 static void br_multicast_toggle_vlan(struct net_bridge_vlan *vlan, bool on)
4254 struct net_bridge_port *p;
4256 if (WARN_ON_ONCE(!br_vlan_is_master(vlan)))
4259 list_for_each_entry(p, &vlan->br->port_list, list) {
4260 struct net_bridge_vlan *vport;
4262 vport = br_vlan_find(nbp_vlan_group(p), vlan->vid);
4265 br_multicast_toggle_one_vlan(vport, on);
4268 if (br_vlan_is_brentry(vlan))
4269 br_multicast_toggle_one_vlan(vlan, on);
4272 int br_multicast_toggle_vlan_snooping(struct net_bridge *br, bool on,
4273 struct netlink_ext_ack *extack)
4275 struct net_bridge_vlan_group *vg;
4276 struct net_bridge_vlan *vlan;
4277 struct net_bridge_port *p;
4279 if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) == on)
4282 if (on && !br_opt_get(br, BROPT_VLAN_ENABLED)) {
4283 NL_SET_ERR_MSG_MOD(extack, "Cannot enable multicast vlan snooping with vlan filtering disabled");
4287 vg = br_vlan_group(br);
4291 br_opt_toggle(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED, on);
4293 /* disable/enable non-vlan mcast contexts based on vlan snooping */
4295 __br_multicast_stop(&br->multicast_ctx);
4297 __br_multicast_open(&br->multicast_ctx);
4298 list_for_each_entry(p, &br->port_list, list) {
4300 br_multicast_disable_port(p);
4302 br_multicast_enable_port(p);
4305 list_for_each_entry(vlan, &vg->vlan_list, vlist)
4306 br_multicast_toggle_vlan(vlan, on);
4311 bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan, bool on)
4315 /* BR_VLFLAG_GLOBAL_MCAST_ENABLED relies on eventual consistency and
4316 * requires only RTNL to change
4318 if (on == !!(vlan->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED))
4321 vlan->priv_flags ^= BR_VLFLAG_GLOBAL_MCAST_ENABLED;
4322 br_multicast_toggle_vlan(vlan, on);
4327 void br_multicast_stop(struct net_bridge *br)
4331 if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) {
4332 struct net_bridge_vlan_group *vg;
4333 struct net_bridge_vlan *vlan;
4335 vg = br_vlan_group(br);
4337 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
4338 struct net_bridge_mcast *brmctx;
4340 brmctx = &vlan->br_mcast_ctx;
4341 if (br_vlan_is_brentry(vlan) &&
4342 !br_multicast_ctx_vlan_disabled(brmctx))
4343 __br_multicast_stop(&vlan->br_mcast_ctx);
4347 __br_multicast_stop(&br->multicast_ctx);
4351 void br_multicast_dev_del(struct net_bridge *br)
4353 struct net_bridge_mdb_entry *mp;
4354 HLIST_HEAD(deleted_head);
4355 struct hlist_node *tmp;
4357 spin_lock_bh(&br->multicast_lock);
4358 hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node)
4359 br_multicast_del_mdb_entry(mp);
4360 hlist_move_list(&br->mcast_gc_list, &deleted_head);
4361 spin_unlock_bh(&br->multicast_lock);
4363 br_multicast_ctx_deinit(&br->multicast_ctx);
4364 br_multicast_gc(&deleted_head);
4365 cancel_work_sync(&br->mcast_gc_work);
4370 int br_multicast_set_router(struct net_bridge_mcast *brmctx, unsigned long val)
4374 spin_lock_bh(&brmctx->br->multicast_lock);
4377 case MDB_RTR_TYPE_DISABLED:
4378 case MDB_RTR_TYPE_PERM:
4379 br_mc_router_state_change(brmctx->br, val == MDB_RTR_TYPE_PERM);
4380 del_timer(&brmctx->ip4_mc_router_timer);
4381 #if IS_ENABLED(CONFIG_IPV6)
4382 del_timer(&brmctx->ip6_mc_router_timer);
4384 brmctx->multicast_router = val;
4387 case MDB_RTR_TYPE_TEMP_QUERY:
4388 if (brmctx->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
4389 br_mc_router_state_change(brmctx->br, false);
4390 brmctx->multicast_router = val;
4395 spin_unlock_bh(&brmctx->br->multicast_lock);
4401 br_multicast_rport_del_notify(struct net_bridge_mcast_port *pmctx, bool deleted)
4406 /* For backwards compatibility for now, only notify if there is
4407 * no multicast router anymore for both IPv4 and IPv6.
4409 if (!hlist_unhashed(&pmctx->ip4_rlist))
4411 #if IS_ENABLED(CONFIG_IPV6)
4412 if (!hlist_unhashed(&pmctx->ip6_rlist))
4416 br_rtr_notify(pmctx->port->br->dev, pmctx, RTM_DELMDB);
4417 br_port_mc_router_state_change(pmctx->port, false);
4419 /* don't allow timer refresh */
4420 if (pmctx->multicast_router == MDB_RTR_TYPE_TEMP)
4421 pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
4424 int br_multicast_set_port_router(struct net_bridge_mcast_port *pmctx,
4427 struct net_bridge_mcast *brmctx;
4428 unsigned long now = jiffies;
4432 brmctx = br_multicast_port_ctx_get_global(pmctx);
4433 spin_lock_bh(&brmctx->br->multicast_lock);
4434 if (pmctx->multicast_router == val) {
4435 /* Refresh the temp router port timer */
4436 if (pmctx->multicast_router == MDB_RTR_TYPE_TEMP) {
4437 mod_timer(&pmctx->ip4_mc_router_timer,
4438 now + brmctx->multicast_querier_interval);
4439 #if IS_ENABLED(CONFIG_IPV6)
4440 mod_timer(&pmctx->ip6_mc_router_timer,
4441 now + brmctx->multicast_querier_interval);
4448 case MDB_RTR_TYPE_DISABLED:
4449 pmctx->multicast_router = MDB_RTR_TYPE_DISABLED;
4450 del |= br_ip4_multicast_rport_del(pmctx);
4451 del_timer(&pmctx->ip4_mc_router_timer);
4452 del |= br_ip6_multicast_rport_del(pmctx);
4453 #if IS_ENABLED(CONFIG_IPV6)
4454 del_timer(&pmctx->ip6_mc_router_timer);
4456 br_multicast_rport_del_notify(pmctx, del);
4458 case MDB_RTR_TYPE_TEMP_QUERY:
4459 pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
4460 del |= br_ip4_multicast_rport_del(pmctx);
4461 del |= br_ip6_multicast_rport_del(pmctx);
4462 br_multicast_rport_del_notify(pmctx, del);
4464 case MDB_RTR_TYPE_PERM:
4465 pmctx->multicast_router = MDB_RTR_TYPE_PERM;
4466 del_timer(&pmctx->ip4_mc_router_timer);
4467 br_ip4_multicast_add_router(brmctx, pmctx);
4468 #if IS_ENABLED(CONFIG_IPV6)
4469 del_timer(&pmctx->ip6_mc_router_timer);
4471 br_ip6_multicast_add_router(brmctx, pmctx);
4473 case MDB_RTR_TYPE_TEMP:
4474 pmctx->multicast_router = MDB_RTR_TYPE_TEMP;
4475 br_ip4_multicast_mark_router(brmctx, pmctx);
4476 br_ip6_multicast_mark_router(brmctx, pmctx);
4483 spin_unlock_bh(&brmctx->br->multicast_lock);
4488 int br_multicast_set_vlan_router(struct net_bridge_vlan *v, u8 mcast_router)
4492 if (br_vlan_is_master(v))
4493 err = br_multicast_set_router(&v->br_mcast_ctx, mcast_router);
4495 err = br_multicast_set_port_router(&v->port_mcast_ctx,
4501 static void br_multicast_start_querier(struct net_bridge_mcast *brmctx,
4502 struct bridge_mcast_own_query *query)
4504 struct net_bridge_port *port;
4506 if (!br_multicast_ctx_matches_vlan_snooping(brmctx))
4509 __br_multicast_open_query(brmctx->br, query);
4512 list_for_each_entry_rcu(port, &brmctx->br->port_list, list) {
4513 struct bridge_mcast_own_query *ip4_own_query;
4514 #if IS_ENABLED(CONFIG_IPV6)
4515 struct bridge_mcast_own_query *ip6_own_query;
4518 if (br_multicast_port_ctx_state_stopped(&port->multicast_ctx))
4521 if (br_multicast_ctx_is_vlan(brmctx)) {
4522 struct net_bridge_vlan *vlan;
4524 vlan = br_vlan_find(nbp_vlan_group_rcu(port),
4527 br_multicast_port_ctx_state_stopped(&vlan->port_mcast_ctx))
4530 ip4_own_query = &vlan->port_mcast_ctx.ip4_own_query;
4531 #if IS_ENABLED(CONFIG_IPV6)
4532 ip6_own_query = &vlan->port_mcast_ctx.ip6_own_query;
4535 ip4_own_query = &port->multicast_ctx.ip4_own_query;
4536 #if IS_ENABLED(CONFIG_IPV6)
4537 ip6_own_query = &port->multicast_ctx.ip6_own_query;
4541 if (query == &brmctx->ip4_own_query)
4542 br_multicast_enable(ip4_own_query);
4543 #if IS_ENABLED(CONFIG_IPV6)
4545 br_multicast_enable(ip6_own_query);
4551 int br_multicast_toggle(struct net_bridge *br, unsigned long val,
4552 struct netlink_ext_ack *extack)
4554 struct net_bridge_port *port;
4555 bool change_snoopers = false;
4558 spin_lock_bh(&br->multicast_lock);
4559 if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val)
4562 err = br_mc_disabled_update(br->dev, val, extack);
4563 if (err == -EOPNOTSUPP)
4568 br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val);
4569 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
4570 change_snoopers = true;
4574 if (!netif_running(br->dev))
4577 br_multicast_open(br);
4578 list_for_each_entry(port, &br->port_list, list)
4579 __br_multicast_enable_port_ctx(&port->multicast_ctx);
4581 change_snoopers = true;
4584 spin_unlock_bh(&br->multicast_lock);
4586 /* br_multicast_join_snoopers has the potential to cause
4587 * an MLD Report/Leave to be delivered to br_multicast_rcv,
4588 * which would in turn call br_multicast_add_group, which would
4589 * attempt to acquire multicast_lock. This function should be
4590 * called after the lock has been released to avoid deadlocks on
4593 * br_multicast_leave_snoopers does not have the problem since
4594 * br_multicast_rcv first checks BROPT_MULTICAST_ENABLED, and
4595 * returns without calling br_multicast_ipv4/6_rcv if it's not
4596 * enabled. Moved both functions out just for symmetry.
4598 if (change_snoopers) {
4599 if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
4600 br_multicast_join_snoopers(br);
4602 br_multicast_leave_snoopers(br);
4608 bool br_multicast_enabled(const struct net_device *dev)
4610 struct net_bridge *br = netdev_priv(dev);
4612 return !!br_opt_get(br, BROPT_MULTICAST_ENABLED);
4614 EXPORT_SYMBOL_GPL(br_multicast_enabled);
4616 bool br_multicast_router(const struct net_device *dev)
4618 struct net_bridge *br = netdev_priv(dev);
4621 spin_lock_bh(&br->multicast_lock);
4622 is_router = br_multicast_is_router(&br->multicast_ctx, NULL);
4623 spin_unlock_bh(&br->multicast_lock);
4626 EXPORT_SYMBOL_GPL(br_multicast_router);
4628 int br_multicast_set_querier(struct net_bridge_mcast *brmctx, unsigned long val)
4630 unsigned long max_delay;
4634 spin_lock_bh(&brmctx->br->multicast_lock);
4635 if (brmctx->multicast_querier == val)
4638 WRITE_ONCE(brmctx->multicast_querier, val);
4642 max_delay = brmctx->multicast_query_response_interval;
4644 if (!timer_pending(&brmctx->ip4_other_query.timer))
4645 brmctx->ip4_other_query.delay_time = jiffies + max_delay;
4647 br_multicast_start_querier(brmctx, &brmctx->ip4_own_query);
4649 #if IS_ENABLED(CONFIG_IPV6)
4650 if (!timer_pending(&brmctx->ip6_other_query.timer))
4651 brmctx->ip6_other_query.delay_time = jiffies + max_delay;
4653 br_multicast_start_querier(brmctx, &brmctx->ip6_own_query);
4657 spin_unlock_bh(&brmctx->br->multicast_lock);
4662 int br_multicast_set_igmp_version(struct net_bridge_mcast *brmctx,
4665 /* Currently we support only version 2 and 3 */
4674 spin_lock_bh(&brmctx->br->multicast_lock);
4675 brmctx->multicast_igmp_version = val;
4676 spin_unlock_bh(&brmctx->br->multicast_lock);
4681 #if IS_ENABLED(CONFIG_IPV6)
4682 int br_multicast_set_mld_version(struct net_bridge_mcast *brmctx,
4685 /* Currently we support version 1 and 2 */
4694 spin_lock_bh(&brmctx->br->multicast_lock);
4695 brmctx->multicast_mld_version = val;
4696 spin_unlock_bh(&brmctx->br->multicast_lock);
4702 void br_multicast_set_query_intvl(struct net_bridge_mcast *brmctx,
4705 unsigned long intvl_jiffies = clock_t_to_jiffies(val);
4707 if (intvl_jiffies < BR_MULTICAST_QUERY_INTVL_MIN) {
4709 "trying to set multicast query interval below minimum, setting to %lu (%ums)\n",
4710 jiffies_to_clock_t(BR_MULTICAST_QUERY_INTVL_MIN),
4711 jiffies_to_msecs(BR_MULTICAST_QUERY_INTVL_MIN));
4712 intvl_jiffies = BR_MULTICAST_QUERY_INTVL_MIN;
4715 brmctx->multicast_query_interval = intvl_jiffies;
4718 void br_multicast_set_startup_query_intvl(struct net_bridge_mcast *brmctx,
4721 unsigned long intvl_jiffies = clock_t_to_jiffies(val);
4723 if (intvl_jiffies < BR_MULTICAST_STARTUP_QUERY_INTVL_MIN) {
4725 "trying to set multicast startup query interval below minimum, setting to %lu (%ums)\n",
4726 jiffies_to_clock_t(BR_MULTICAST_STARTUP_QUERY_INTVL_MIN),
4727 jiffies_to_msecs(BR_MULTICAST_STARTUP_QUERY_INTVL_MIN));
4728 intvl_jiffies = BR_MULTICAST_STARTUP_QUERY_INTVL_MIN;
4731 brmctx->multicast_startup_query_interval = intvl_jiffies;
4735 * br_multicast_list_adjacent - Returns snooped multicast addresses
4736 * @dev: The bridge port adjacent to which to retrieve addresses
4737 * @br_ip_list: The list to store found, snooped multicast IP addresses in
4739 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
4740 * snooping feature on all bridge ports of dev's bridge device, excluding
4741 * the addresses from dev itself.
4743 * Returns the number of items added to br_ip_list.
4746 * - br_ip_list needs to be initialized by caller
4747 * - br_ip_list might contain duplicates in the end
4748 * (needs to be taken care of by caller)
4749 * - br_ip_list needs to be freed by caller
4751 int br_multicast_list_adjacent(struct net_device *dev,
4752 struct list_head *br_ip_list)
4754 struct net_bridge *br;
4755 struct net_bridge_port *port;
4756 struct net_bridge_port_group *group;
4757 struct br_ip_list *entry;
4761 if (!br_ip_list || !netif_is_bridge_port(dev))
4764 port = br_port_get_rcu(dev);
4765 if (!port || !port->br)
4770 list_for_each_entry_rcu(port, &br->port_list, list) {
4771 if (!port->dev || port->dev == dev)
4774 hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
4775 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
4779 entry->addr = group->key.addr;
4780 list_add(&entry->list, br_ip_list);
4789 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
4792 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
4793 * @dev: The bridge port providing the bridge on which to check for a querier
4794 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
4796 * Checks whether the given interface has a bridge on top and if so returns
4797 * true if a valid querier exists anywhere on the bridged link layer.
4798 * Otherwise returns false.
4800 bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
4802 struct net_bridge *br;
4803 struct net_bridge_port *port;
4808 if (!netif_is_bridge_port(dev))
4811 port = br_port_get_rcu(dev);
4812 if (!port || !port->br)
4817 memset(ð, 0, sizeof(eth));
4818 eth.h_proto = htons(proto);
4820 ret = br_multicast_querier_exists(&br->multicast_ctx, ð, NULL);
4826 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
4829 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
4830 * @dev: The bridge port adjacent to which to check for a querier
4831 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
4833 * Checks whether the given interface has a bridge on top and if so returns
4834 * true if a selected querier is behind one of the other ports of this
4835 * bridge. Otherwise returns false.
4837 bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
4839 struct net_bridge_mcast *brmctx;
4840 struct net_bridge *br;
4841 struct net_bridge_port *port;
4846 if (!netif_is_bridge_port(dev))
4849 port = br_port_get_rcu(dev);
4850 if (!port || !port->br)
4854 brmctx = &br->multicast_ctx;
4858 port_ifidx = brmctx->ip4_querier.port_ifidx;
4859 if (!timer_pending(&brmctx->ip4_other_query.timer) ||
4860 port_ifidx == port->dev->ifindex)
4863 #if IS_ENABLED(CONFIG_IPV6)
4865 port_ifidx = brmctx->ip6_querier.port_ifidx;
4866 if (!timer_pending(&brmctx->ip6_other_query.timer) ||
4867 port_ifidx == port->dev->ifindex)
4880 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
4883 * br_multicast_has_router_adjacent - Checks for a router behind a bridge port
4884 * @dev: The bridge port adjacent to which to check for a multicast router
4885 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
4887 * Checks whether the given interface has a bridge on top and if so returns
4888 * true if a multicast router is behind one of the other ports of this
4889 * bridge. Otherwise returns false.
4891 bool br_multicast_has_router_adjacent(struct net_device *dev, int proto)
4893 struct net_bridge_mcast_port *pmctx;
4894 struct net_bridge_mcast *brmctx;
4895 struct net_bridge_port *port;
4899 port = br_port_get_check_rcu(dev);
4903 brmctx = &port->br->multicast_ctx;
4906 hlist_for_each_entry_rcu(pmctx, &brmctx->ip4_mc_router_list,
4908 if (pmctx->port == port)
4915 #if IS_ENABLED(CONFIG_IPV6)
4917 hlist_for_each_entry_rcu(pmctx, &brmctx->ip6_mc_router_list,
4919 if (pmctx->port == port)
4928 /* when compiled without IPv6 support, be conservative and
4929 * always assume presence of an IPv6 multicast router
4938 EXPORT_SYMBOL_GPL(br_multicast_has_router_adjacent);
4940 static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
4941 const struct sk_buff *skb, u8 type, u8 dir)
4943 struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
4944 __be16 proto = skb->protocol;
4947 u64_stats_update_begin(&pstats->syncp);
4949 case htons(ETH_P_IP):
4950 t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
4952 case IGMP_HOST_MEMBERSHIP_REPORT:
4953 pstats->mstats.igmp_v1reports[dir]++;
4955 case IGMPV2_HOST_MEMBERSHIP_REPORT:
4956 pstats->mstats.igmp_v2reports[dir]++;
4958 case IGMPV3_HOST_MEMBERSHIP_REPORT:
4959 pstats->mstats.igmp_v3reports[dir]++;
4961 case IGMP_HOST_MEMBERSHIP_QUERY:
4962 if (t_len != sizeof(struct igmphdr)) {
4963 pstats->mstats.igmp_v3queries[dir]++;
4965 unsigned int offset = skb_transport_offset(skb);
4966 struct igmphdr *ih, _ihdr;
4968 ih = skb_header_pointer(skb, offset,
4969 sizeof(_ihdr), &_ihdr);
4973 pstats->mstats.igmp_v1queries[dir]++;
4975 pstats->mstats.igmp_v2queries[dir]++;
4978 case IGMP_HOST_LEAVE_MESSAGE:
4979 pstats->mstats.igmp_leaves[dir]++;
4983 #if IS_ENABLED(CONFIG_IPV6)
4984 case htons(ETH_P_IPV6):
4985 t_len = ntohs(ipv6_hdr(skb)->payload_len) +
4986 sizeof(struct ipv6hdr);
4987 t_len -= skb_network_header_len(skb);
4989 case ICMPV6_MGM_REPORT:
4990 pstats->mstats.mld_v1reports[dir]++;
4992 case ICMPV6_MLD2_REPORT:
4993 pstats->mstats.mld_v2reports[dir]++;
4995 case ICMPV6_MGM_QUERY:
4996 if (t_len != sizeof(struct mld_msg))
4997 pstats->mstats.mld_v2queries[dir]++;
4999 pstats->mstats.mld_v1queries[dir]++;
5001 case ICMPV6_MGM_REDUCTION:
5002 pstats->mstats.mld_leaves[dir]++;
5006 #endif /* CONFIG_IPV6 */
5008 u64_stats_update_end(&pstats->syncp);
5011 void br_multicast_count(struct net_bridge *br,
5012 const struct net_bridge_port *p,
5013 const struct sk_buff *skb, u8 type, u8 dir)
5015 struct bridge_mcast_stats __percpu *stats;
5017 /* if multicast_disabled is true then igmp type can't be set */
5018 if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
5022 stats = p->mcast_stats;
5024 stats = br->mcast_stats;
5025 if (WARN_ON(!stats))
5028 br_mcast_stats_add(stats, skb, type, dir);
5031 int br_multicast_init_stats(struct net_bridge *br)
5033 br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
5034 if (!br->mcast_stats)
5040 void br_multicast_uninit_stats(struct net_bridge *br)
5042 free_percpu(br->mcast_stats);
5045 /* noinline for https://bugs.llvm.org/show_bug.cgi?id=45802#c9 */
5046 static noinline_for_stack void mcast_stats_add_dir(u64 *dst, u64 *src)
5048 dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
5049 dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
5052 void br_multicast_get_stats(const struct net_bridge *br,
5053 const struct net_bridge_port *p,
5054 struct br_mcast_stats *dest)
5056 struct bridge_mcast_stats __percpu *stats;
5057 struct br_mcast_stats tdst;
5060 memset(dest, 0, sizeof(*dest));
5062 stats = p->mcast_stats;
5064 stats = br->mcast_stats;
5065 if (WARN_ON(!stats))
5068 memset(&tdst, 0, sizeof(tdst));
5069 for_each_possible_cpu(i) {
5070 struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
5071 struct br_mcast_stats temp;
5075 start = u64_stats_fetch_begin(&cpu_stats->syncp);
5076 memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
5077 } while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
5079 mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
5080 mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
5081 mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
5082 mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
5083 mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
5084 mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
5085 mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
5086 tdst.igmp_parse_errors += temp.igmp_parse_errors;
5088 mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
5089 mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
5090 mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
5091 mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
5092 mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
5093 tdst.mld_parse_errors += temp.mld_parse_errors;
5095 memcpy(dest, &tdst, sizeof(*dest));
5098 int br_mdb_hash_init(struct net_bridge *br)
5102 err = rhashtable_init(&br->sg_port_tbl, &br_sg_port_rht_params);
5106 err = rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params);
5108 rhashtable_destroy(&br->sg_port_tbl);
5115 void br_mdb_hash_fini(struct net_bridge *br)
5117 rhashtable_destroy(&br->sg_port_tbl);
5118 rhashtable_destroy(&br->mdb_hash_tbl);