Commit 5225d5f5 authored by Jakub Kicinski's avatar Jakub Kicinski

Merge branch 'net-bridge-multicast-add-initial-eht-support'

Nikolay Aleksandrov says:

====================
net: bridge: multicast: add initial EHT support

This set adds explicit host tracking support for IGMPv3/MLDv2. The
already present per-port fast leave flag is used to enable it since that
is the primary goal of EHT, to track a group and its S,Gs usage per-host
and when left without any interested hosts delete them before the standard
timers. The EHT code is pretty self-contained and not enabled by default.
There is no new uAPI added, all of the functionality is currently hidden
behind the fast leave flag. In the future that will change (more below).
The host tracking uses two new sets per port group: one having an entry for
each host which contains that host's view of the group (source list and
filter mode), and one set which contains an entry for each source having
an internal set which contains an entry for each host that has reported
an interest for that source. RB trees are used for all sets so they're
compact when not used and fast when we need to do lookups.
To illustrate it:
 [ bridge port group ]
  ` [ host set (rb) ]
   ` [ host entry with a list of sources and filter mode ]
  ` [ source set (rb) ]
   ` [ source entry ]
    ` [ source host set (rb) ]
     ` [ source host entry with a timer ]

The number of tracked sources per host is limited to the maximum total
number of S,G entries per port group - PG_SRC_ENT_LIMIT (currently 32).
The number of hosts is unlimited, I think the argument that a local
attacker can exhaust the memory/cause high CPU usage can be applied to
fdb entries as well which are unlimited. In the future if needed we can
add an option to limit these, but I don't think it's necessary for a
start. All of the new sets are protected by the bridge's multicast lock.
I'm pretty sure we'll be changing the cases and improving the
convergence time in the future, but this seems like a good start.

Patch breakdown:
 patch 1 -  4: minor cleanups and preparations for EHT
 patch      5: adds the new structures which will be used in the
               following patches
 patch      6: adds support to create, destroy and lookup host entries
 patch      7: adds support to create, delete and lokup source set entries
 patch      8: adds a host "delete" function which is just a host's
               source list flush since that would automatically delete
               the host
 patch 9 - 10: add support for handling all IGMPv3/MLDv2 report types
               more information can be found in the individual patches
 patch     11: optmizes a specific TO_INCLUDE use-case with host timeouts
 patch     12: handles per-host filter mode changing (include <-> exclude)
 patch     13: pulls out block group deletion since now it can be
               deleted in both filter modes
 patch     14: marks deletions done due to fast leave

Future plans:
 - export host information
 - add an option to reduce queries
 - add an option to limit the number of host entries
 - tune more fast leave cases for quicker convergence

By the way I think this is the first open-source EHT implementation, I
couldn't find any while researching it. :)
====================

Link: https://lore.kernel.org/r/20210120145203.1109140-1-razor@blackwall.orgSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parents b9046e88 d5a10222
......@@ -18,7 +18,7 @@ br_netfilter-y := br_netfilter_hooks.o
br_netfilter-$(subst m,y,$(CONFIG_IPV6)) += br_netfilter_ipv6.o
obj-$(CONFIG_BRIDGE_NETFILTER) += br_netfilter.o
bridge-$(CONFIG_BRIDGE_IGMP_SNOOPING) += br_multicast.o br_mdb.o
bridge-$(CONFIG_BRIDGE_IGMP_SNOOPING) += br_multicast.o br_mdb.o br_multicast_eht.o
bridge-$(CONFIG_BRIDGE_VLAN_FILTERING) += br_vlan.o br_vlan_tunnel.o br_vlan_options.o
......
......@@ -33,6 +33,7 @@
#endif
#include "br_private.h"
#include "br_private_mcast_eht.h"
static const struct rhashtable_params br_mdb_rht_params = {
.head_offset = offsetof(struct net_bridge_mdb_entry, rhnode),
......@@ -441,7 +442,8 @@ static void br_multicast_fwd_src_add(struct net_bridge_group_src *src)
br_multicast_sg_add_exclude_ports(star_mp, sg);
}
static void br_multicast_fwd_src_remove(struct net_bridge_group_src *src)
static void br_multicast_fwd_src_remove(struct net_bridge_group_src *src,
bool fastleave)
{
struct net_bridge_port_group *p, *pg = src->pg;
struct net_bridge_port_group __rcu **pp;
......@@ -466,6 +468,8 @@ static void br_multicast_fwd_src_remove(struct net_bridge_group_src *src)
(p->flags & MDB_PG_FLAGS_PERMANENT))
break;
if (fastleave)
p->flags |= MDB_PG_FLAGS_FAST_LEAVE;
br_multicast_del_pg(mp, p, pp);
break;
}
......@@ -559,11 +563,12 @@ static void br_multicast_destroy_group_src(struct net_bridge_mcast_gc *gc)
kfree_rcu(src, rcu);
}
static void br_multicast_del_group_src(struct net_bridge_group_src *src)
void br_multicast_del_group_src(struct net_bridge_group_src *src,
bool fastleave)
{
struct net_bridge *br = src->pg->key.port->br;
br_multicast_fwd_src_remove(src);
br_multicast_fwd_src_remove(src, fastleave);
hlist_del_init_rcu(&src->node);
src->pg->src_ents--;
hlist_add_head(&src->mcast_gc.gc_node, &br->mcast_gc_list);
......@@ -593,8 +598,9 @@ void br_multicast_del_pg(struct net_bridge_mdb_entry *mp,
rcu_assign_pointer(*pp, pg->next);
hlist_del_init(&pg->mglist);
br_multicast_eht_clean_sets(pg);
hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
br_multicast_del_group_src(ent);
br_multicast_del_group_src(ent, false);
br_mdb_notify(br->dev, mp, pg, RTM_DELMDB);
if (!br_multicast_is_star_g(&mp->addr)) {
rhashtable_remove_fast(&br->sg_port_tbl, &pg->rhnode,
......@@ -651,7 +657,7 @@ static void br_multicast_port_group_expired(struct timer_list *t)
pg->filter_mode = MCAST_INCLUDE;
hlist_for_each_entry_safe(src_ent, tmp, &pg->src_list, node) {
if (!timer_pending(&src_ent->timer)) {
br_multicast_del_group_src(src_ent);
br_multicast_del_group_src(src_ent, false);
changed = true;
}
}
......@@ -1078,7 +1084,7 @@ static void br_multicast_group_src_expired(struct timer_list *t)
pg = src->pg;
if (pg->filter_mode == MCAST_INCLUDE) {
br_multicast_del_group_src(src);
br_multicast_del_group_src(src, false);
if (!hlist_empty(&pg->src_list))
goto out;
br_multicast_find_del_pg(br, pg);
......@@ -1090,7 +1096,7 @@ static void br_multicast_group_src_expired(struct timer_list *t)
spin_unlock(&br->multicast_lock);
}
static struct net_bridge_group_src *
struct net_bridge_group_src *
br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip)
{
struct net_bridge_group_src *ent;
......@@ -1172,6 +1178,8 @@ struct net_bridge_port_group *br_multicast_new_port_group(
p->flags = flags;
p->filter_mode = filter_mode;
p->rt_protocol = rt_protocol;
p->eht_host_tree = RB_ROOT;
p->eht_set_tree = RB_ROOT;
p->mcast_gc.destroy = br_multicast_destroy_port_group;
INIT_HLIST_HEAD(&p->src_list);
......@@ -1700,7 +1708,7 @@ static int __grp_src_delete_marked(struct net_bridge_port_group *pg)
hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
if (ent->flags & BR_SGRP_F_DELETE) {
br_multicast_del_group_src(ent);
br_multicast_del_group_src(ent, false);
deleted++;
}
......@@ -1799,8 +1807,9 @@ static void __grp_send_query_and_rexmit(struct net_bridge_port_group *pg)
* INCLUDE (A) ALLOW (B) INCLUDE (A+B) (B)=GMI
* EXCLUDE (X,Y) ALLOW (A) EXCLUDE (X+A,Y-A) (A)=GMI
*/
static bool br_multicast_isinc_allow(struct net_bridge_port_group *pg,
void *srcs, u32 nsrcs, size_t src_size)
static bool br_multicast_isinc_allow(struct net_bridge_port_group *pg, void *h_addr,
void *srcs, u32 nsrcs, size_t addr_size,
int grec_type)
{
struct net_bridge *br = pg->key.port->br;
struct net_bridge_group_src *ent;
......@@ -1812,7 +1821,7 @@ static bool br_multicast_isinc_allow(struct net_bridge_port_group *pg,
memset(&src_ip, 0, sizeof(src_ip));
src_ip.proto = pg->key.addr.proto;
for (src_idx = 0; src_idx < nsrcs; src_idx++) {
memcpy(&src_ip.src, srcs, src_size);
memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
ent = br_multicast_find_group_src(pg, &src_ip);
if (!ent) {
ent = br_multicast_new_group_src(pg, &src_ip);
......@@ -1822,9 +1831,11 @@ static bool br_multicast_isinc_allow(struct net_bridge_port_group *pg,
if (ent)
__grp_src_mod_timer(ent, now + br_multicast_gmi(br));
srcs += src_size;
}
if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
changed = true;
return changed;
}
......@@ -1833,8 +1844,9 @@ static bool br_multicast_isinc_allow(struct net_bridge_port_group *pg,
* Delete (A-B)
* Group Timer=GMI
*/
static void __grp_src_isexc_incl(struct net_bridge_port_group *pg,
void *srcs, u32 nsrcs, size_t src_size)
static void __grp_src_isexc_incl(struct net_bridge_port_group *pg, void *h_addr,
void *srcs, u32 nsrcs, size_t addr_size,
int grec_type)
{
struct net_bridge_group_src *ent;
struct br_ip src_ip;
......@@ -1846,7 +1858,7 @@ static void __grp_src_isexc_incl(struct net_bridge_port_group *pg,
memset(&src_ip, 0, sizeof(src_ip));
src_ip.proto = pg->key.addr.proto;
for (src_idx = 0; src_idx < nsrcs; src_idx++) {
memcpy(&src_ip.src, srcs, src_size);
memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
ent = br_multicast_find_group_src(pg, &src_ip);
if (ent)
ent->flags &= ~BR_SGRP_F_DELETE;
......@@ -1854,9 +1866,10 @@ static void __grp_src_isexc_incl(struct net_bridge_port_group *pg,
ent = br_multicast_new_group_src(pg, &src_ip);
if (ent)
br_multicast_fwd_src_handle(ent);
srcs += src_size;
}
br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type);
__grp_src_delete_marked(pg);
}
......@@ -1866,8 +1879,9 @@ static void __grp_src_isexc_incl(struct net_bridge_port_group *pg,
* Delete (Y-A)
* Group Timer=GMI
*/
static bool __grp_src_isexc_excl(struct net_bridge_port_group *pg,
void *srcs, u32 nsrcs, size_t src_size)
static bool __grp_src_isexc_excl(struct net_bridge_port_group *pg, void *h_addr,
void *srcs, u32 nsrcs, size_t addr_size,
int grec_type)
{
struct net_bridge *br = pg->key.port->br;
struct net_bridge_group_src *ent;
......@@ -1882,7 +1896,7 @@ static bool __grp_src_isexc_excl(struct net_bridge_port_group *pg,
memset(&src_ip, 0, sizeof(src_ip));
src_ip.proto = pg->key.addr.proto;
for (src_idx = 0; src_idx < nsrcs; src_idx++) {
memcpy(&src_ip.src, srcs, src_size);
memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
ent = br_multicast_find_group_src(pg, &src_ip);
if (ent) {
ent->flags &= ~BR_SGRP_F_DELETE;
......@@ -1894,29 +1908,34 @@ static bool __grp_src_isexc_excl(struct net_bridge_port_group *pg,
changed = true;
}
}
srcs += src_size;
}
if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
changed = true;
if (__grp_src_delete_marked(pg))
changed = true;
return changed;
}
static bool br_multicast_isexc(struct net_bridge_port_group *pg,
void *srcs, u32 nsrcs, size_t src_size)
static bool br_multicast_isexc(struct net_bridge_port_group *pg, void *h_addr,
void *srcs, u32 nsrcs, size_t addr_size,
int grec_type)
{
struct net_bridge *br = pg->key.port->br;
bool changed = false;
switch (pg->filter_mode) {
case MCAST_INCLUDE:
__grp_src_isexc_incl(pg, srcs, nsrcs, src_size);
__grp_src_isexc_incl(pg, h_addr, srcs, nsrcs, addr_size,
grec_type);
br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE);
changed = true;
break;
case MCAST_EXCLUDE:
changed = __grp_src_isexc_excl(pg, srcs, nsrcs, src_size);
changed = __grp_src_isexc_excl(pg, h_addr, srcs, nsrcs, addr_size,
grec_type);
break;
}
......@@ -1930,8 +1949,9 @@ static bool br_multicast_isexc(struct net_bridge_port_group *pg,
* INCLUDE (A) TO_IN (B) INCLUDE (A+B) (B)=GMI
* Send Q(G,A-B)
*/
static bool __grp_src_toin_incl(struct net_bridge_port_group *pg,
void *srcs, u32 nsrcs, size_t src_size)
static bool __grp_src_toin_incl(struct net_bridge_port_group *pg, void *h_addr,
void *srcs, u32 nsrcs, size_t addr_size,
int grec_type)
{
struct net_bridge *br = pg->key.port->br;
u32 src_idx, to_send = pg->src_ents;
......@@ -1946,7 +1966,7 @@ static bool __grp_src_toin_incl(struct net_bridge_port_group *pg,
memset(&src_ip, 0, sizeof(src_ip));
src_ip.proto = pg->key.addr.proto;
for (src_idx = 0; src_idx < nsrcs; src_idx++) {
memcpy(&src_ip.src, srcs, src_size);
memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
ent = br_multicast_find_group_src(pg, &src_ip);
if (ent) {
ent->flags &= ~BR_SGRP_F_SEND;
......@@ -1958,9 +1978,11 @@ static bool __grp_src_toin_incl(struct net_bridge_port_group *pg,
}
if (ent)
__grp_src_mod_timer(ent, now + br_multicast_gmi(br));
srcs += src_size;
}
if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
changed = true;
if (to_send)
__grp_src_query_marked_and_rexmit(pg);
......@@ -1972,8 +1994,9 @@ static bool __grp_src_toin_incl(struct net_bridge_port_group *pg,
* Send Q(G,X-A)
* Send Q(G)
*/
static bool __grp_src_toin_excl(struct net_bridge_port_group *pg,
void *srcs, u32 nsrcs, size_t src_size)
static bool __grp_src_toin_excl(struct net_bridge_port_group *pg, void *h_addr,
void *srcs, u32 nsrcs, size_t addr_size,
int grec_type)
{
struct net_bridge *br = pg->key.port->br;
u32 src_idx, to_send = pg->src_ents;
......@@ -1989,7 +2012,7 @@ static bool __grp_src_toin_excl(struct net_bridge_port_group *pg,
memset(&src_ip, 0, sizeof(src_ip));
src_ip.proto = pg->key.addr.proto;
for (src_idx = 0; src_idx < nsrcs; src_idx++) {
memcpy(&src_ip.src, srcs, src_size);
memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
ent = br_multicast_find_group_src(pg, &src_ip);
if (ent) {
if (timer_pending(&ent->timer)) {
......@@ -2003,9 +2026,11 @@ static bool __grp_src_toin_excl(struct net_bridge_port_group *pg,
}
if (ent)
__grp_src_mod_timer(ent, now + br_multicast_gmi(br));
srcs += src_size;
}
if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
changed = true;
if (to_send)
__grp_src_query_marked_and_rexmit(pg);
......@@ -2014,20 +2039,32 @@ static bool __grp_src_toin_excl(struct net_bridge_port_group *pg,
return changed;
}
static bool br_multicast_toin(struct net_bridge_port_group *pg,
void *srcs, u32 nsrcs, size_t src_size)
static bool br_multicast_toin(struct net_bridge_port_group *pg, void *h_addr,
void *srcs, u32 nsrcs, size_t addr_size,
int grec_type)
{
bool changed = false;
switch (pg->filter_mode) {
case MCAST_INCLUDE:
changed = __grp_src_toin_incl(pg, srcs, nsrcs, src_size);
changed = __grp_src_toin_incl(pg, h_addr, srcs, nsrcs, addr_size,
grec_type);
break;
case MCAST_EXCLUDE:
changed = __grp_src_toin_excl(pg, srcs, nsrcs, src_size);
changed = __grp_src_toin_excl(pg, h_addr, srcs, nsrcs, addr_size,
grec_type);
break;
}
if (br_multicast_eht_should_del_pg(pg)) {
pg->flags |= MDB_PG_FLAGS_FAST_LEAVE;
br_multicast_find_del_pg(pg->key.port->br, pg);
/* a notification has already been sent and we shouldn't
* access pg after the delete so we have to return false
*/
changed = false;
}
return changed;
}
......@@ -2037,8 +2074,9 @@ static bool br_multicast_toin(struct net_bridge_port_group *pg,
* Send Q(G,A*B)
* Group Timer=GMI
*/
static void __grp_src_toex_incl(struct net_bridge_port_group *pg,
void *srcs, u32 nsrcs, size_t src_size)
static void __grp_src_toex_incl(struct net_bridge_port_group *pg, void *h_addr,
void *srcs, u32 nsrcs, size_t addr_size,
int grec_type)
{
struct net_bridge_group_src *ent;
u32 src_idx, to_send = 0;
......@@ -2050,7 +2088,7 @@ static void __grp_src_toex_incl(struct net_bridge_port_group *pg,
memset(&src_ip, 0, sizeof(src_ip));
src_ip.proto = pg->key.addr.proto;
for (src_idx = 0; src_idx < nsrcs; src_idx++) {
memcpy(&src_ip.src, srcs, src_size);
memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
ent = br_multicast_find_group_src(pg, &src_ip);
if (ent) {
ent->flags = (ent->flags & ~BR_SGRP_F_DELETE) |
......@@ -2061,9 +2099,10 @@ static void __grp_src_toex_incl(struct net_bridge_port_group *pg,
}
if (ent)
br_multicast_fwd_src_handle(ent);
srcs += src_size;
}
br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type);
__grp_src_delete_marked(pg);
if (to_send)
__grp_src_query_marked_and_rexmit(pg);
......@@ -2076,8 +2115,9 @@ static void __grp_src_toex_incl(struct net_bridge_port_group *pg,
* Send Q(G,A-Y)
* Group Timer=GMI
*/
static bool __grp_src_toex_excl(struct net_bridge_port_group *pg,
void *srcs, u32 nsrcs, size_t src_size)
static bool __grp_src_toex_excl(struct net_bridge_port_group *pg, void *h_addr,
void *srcs, u32 nsrcs, size_t addr_size,
int grec_type)
{
struct net_bridge_group_src *ent;
u32 src_idx, to_send = 0;
......@@ -2090,7 +2130,7 @@ static bool __grp_src_toex_excl(struct net_bridge_port_group *pg,
memset(&src_ip, 0, sizeof(src_ip));
src_ip.proto = pg->key.addr.proto;
for (src_idx = 0; src_idx < nsrcs; src_idx++) {
memcpy(&src_ip.src, srcs, src_size);
memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
ent = br_multicast_find_group_src(pg, &src_ip);
if (ent) {
ent->flags &= ~BR_SGRP_F_DELETE;
......@@ -2105,9 +2145,11 @@ static bool __grp_src_toex_excl(struct net_bridge_port_group *pg,
ent->flags |= BR_SGRP_F_SEND;
to_send++;
}
srcs += src_size;
}
if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
changed = true;
if (__grp_src_delete_marked(pg))
changed = true;
if (to_send)
......@@ -2116,20 +2158,23 @@ static bool __grp_src_toex_excl(struct net_bridge_port_group *pg,
return changed;
}
static bool br_multicast_toex(struct net_bridge_port_group *pg,
void *srcs, u32 nsrcs, size_t src_size)
static bool br_multicast_toex(struct net_bridge_port_group *pg, void *h_addr,
void *srcs, u32 nsrcs, size_t addr_size,
int grec_type)
{
struct net_bridge *br = pg->key.port->br;
bool changed = false;
switch (pg->filter_mode) {
case MCAST_INCLUDE:
__grp_src_toex_incl(pg, srcs, nsrcs, src_size);
__grp_src_toex_incl(pg, h_addr, srcs, nsrcs, addr_size,
grec_type);
br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE);
changed = true;
break;
case MCAST_EXCLUDE:
changed = __grp_src_toex_excl(pg, srcs, nsrcs, src_size);
changed = __grp_src_toex_excl(pg, h_addr, srcs, nsrcs, addr_size,
grec_type);
break;
}
......@@ -2142,11 +2187,12 @@ static bool br_multicast_toex(struct net_bridge_port_group *pg,
/* State Msg type New state Actions
* INCLUDE (A) BLOCK (B) INCLUDE (A) Send Q(G,A*B)
*/
static void __grp_src_block_incl(struct net_bridge_port_group *pg,
void *srcs, u32 nsrcs, size_t src_size)
static bool __grp_src_block_incl(struct net_bridge_port_group *pg, void *h_addr,
void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
{
struct net_bridge_group_src *ent;
u32 src_idx, to_send = 0;
bool changed = false;
struct br_ip src_ip;
hlist_for_each_entry(ent, &pg->src_list, node)
......@@ -2155,28 +2201,29 @@ static void __grp_src_block_incl(struct net_bridge_port_group *pg,
memset(&src_ip, 0, sizeof(src_ip));
src_ip.proto = pg->key.addr.proto;
for (src_idx = 0; src_idx < nsrcs; src_idx++) {
memcpy(&src_ip.src, srcs, src_size);
memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
ent = br_multicast_find_group_src(pg, &src_ip);
if (ent) {
ent->flags |= BR_SGRP_F_SEND;
to_send++;
}
srcs += src_size;
}
if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
changed = true;
if (to_send)
__grp_src_query_marked_and_rexmit(pg);
if (pg->filter_mode == MCAST_INCLUDE && hlist_empty(&pg->src_list))
br_multicast_find_del_pg(pg->key.port->br, pg);
return changed;
}
/* State Msg type New state Actions
* EXCLUDE (X,Y) BLOCK (A) EXCLUDE (X+(A-Y),Y) (A-X-Y)=Group Timer
* Send Q(G,A-Y)
*/
static bool __grp_src_block_excl(struct net_bridge_port_group *pg,
void *srcs, u32 nsrcs, size_t src_size)
static bool __grp_src_block_excl(struct net_bridge_port_group *pg, void *h_addr,
void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
{
struct net_bridge_group_src *ent;
u32 src_idx, to_send = 0;
......@@ -2189,7 +2236,7 @@ static bool __grp_src_block_excl(struct net_bridge_port_group *pg,
memset(&src_ip, 0, sizeof(src_ip));
src_ip.proto = pg->key.addr.proto;
for (src_idx = 0; src_idx < nsrcs; src_idx++) {
memcpy(&src_ip.src, srcs, src_size);
memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
ent = br_multicast_find_group_src(pg, &src_ip);
if (!ent) {
ent = br_multicast_new_group_src(pg, &src_ip);
......@@ -2202,29 +2249,44 @@ static bool __grp_src_block_excl(struct net_bridge_port_group *pg,
ent->flags |= BR_SGRP_F_SEND;
to_send++;
}
srcs += src_size;
}
if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
changed = true;
if (to_send)
__grp_src_query_marked_and_rexmit(pg);
return changed;
}
static bool br_multicast_block(struct net_bridge_port_group *pg,
void *srcs, u32 nsrcs, size_t src_size)
static bool br_multicast_block(struct net_bridge_port_group *pg, void *h_addr,
void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
{
bool changed = false;
switch (pg->filter_mode) {
case MCAST_INCLUDE:
__grp_src_block_incl(pg, srcs, nsrcs, src_size);
changed = __grp_src_block_incl(pg, h_addr, srcs, nsrcs, addr_size,
grec_type);
break;
case MCAST_EXCLUDE:
changed = __grp_src_block_excl(pg, srcs, nsrcs, src_size);
changed = __grp_src_block_excl(pg, h_addr, srcs, nsrcs, addr_size,
grec_type);
break;
}
if ((pg->filter_mode == MCAST_INCLUDE && hlist_empty(&pg->src_list)) ||
br_multicast_eht_should_del_pg(pg)) {
if (br_multicast_eht_should_del_pg(pg))
pg->flags |= MDB_PG_FLAGS_FAST_LEAVE;
br_multicast_find_del_pg(pg->key.port->br, pg);
/* a notification has already been sent and we shouldn't
* access pg after the delete so we have to return false
*/
changed = false;
}
return changed;
}
......@@ -2257,8 +2319,8 @@ static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
struct igmpv3_report *ih;
struct igmpv3_grec *grec;
int i, len, num, type;
__be32 group, *h_addr;
bool changed = false;
__be32 group;
int err = 0;
u16 nsrcs;
......@@ -2318,32 +2380,33 @@ static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
pg = br_multicast_find_port(mdst, port, src);
if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
goto unlock_continue;
/* reload grec */
/* reload grec and host addr */
grec = (void *)(skb->data + len - sizeof(*grec) - (nsrcs * 4));
h_addr = &ip_hdr(skb)->saddr;
switch (type) {
case IGMPV3_ALLOW_NEW_SOURCES:
changed = br_multicast_isinc_allow(pg, grec->grec_src,
nsrcs, sizeof(__be32));
changed = br_multicast_isinc_allow(pg, h_addr, grec->grec_src,
nsrcs, sizeof(__be32), type);
break;
case IGMPV3_MODE_IS_INCLUDE:
changed = br_multicast_isinc_allow(pg, grec->grec_src, nsrcs,
sizeof(__be32));
changed = br_multicast_isinc_allow(pg, h_addr, grec->grec_src,
nsrcs, sizeof(__be32), type);
break;
case IGMPV3_MODE_IS_EXCLUDE:
changed = br_multicast_isexc(pg, grec->grec_src, nsrcs,
sizeof(__be32));
changed = br_multicast_isexc(pg, h_addr, grec->grec_src,
nsrcs, sizeof(__be32), type);
break;
case IGMPV3_CHANGE_TO_INCLUDE:
changed = br_multicast_toin(pg, grec->grec_src, nsrcs,
sizeof(__be32));
changed = br_multicast_toin(pg, h_addr, grec->grec_src,
nsrcs, sizeof(__be32), type);
break;
case IGMPV3_CHANGE_TO_EXCLUDE:
changed = br_multicast_toex(pg, grec->grec_src, nsrcs,
sizeof(__be32));
changed = br_multicast_toex(pg, h_addr, grec->grec_src,
nsrcs, sizeof(__be32), type);
break;
case IGMPV3_BLOCK_OLD_SOURCES:
changed = br_multicast_block(pg, grec->grec_src, nsrcs,
sizeof(__be32));
changed = br_multicast_block(pg, h_addr, grec->grec_src,
nsrcs, sizeof(__be32), type);
break;
}
if (changed)
......@@ -2367,6 +2430,7 @@ static int br_ip6_multicast_mld2_report(struct net_bridge *br,
unsigned int nsrcs_offset;
const unsigned char *src;
struct icmp6hdr *icmp6h;
struct in6_addr *h_addr;
struct mld2_grec *grec;
unsigned int grec_len;
bool changed = false;
......@@ -2445,31 +2509,43 @@ static int br_ip6_multicast_mld2_report(struct net_bridge *br,
pg = br_multicast_find_port(mdst, port, src);
if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
goto unlock_continue;
h_addr = &ipv6_hdr(skb)->saddr;
switch (grec->grec_type) {
case MLD2_ALLOW_NEW_SOURCES:
changed = br_multicast_isinc_allow(pg, grec->grec_src,
nsrcs,
sizeof(struct in6_addr));
changed = br_multicast_isinc_allow(pg, h_addr,
grec->grec_src, nsrcs,
sizeof(struct in6_addr),
grec->grec_type);
break;
case MLD2_MODE_IS_INCLUDE:
changed = br_multicast_isinc_allow(pg, grec->grec_src, nsrcs,
sizeof(struct in6_addr));
changed = br_multicast_isinc_allow(pg, h_addr,
grec->grec_src, nsrcs,
sizeof(struct in6_addr),
grec->grec_type);
break;
case MLD2_MODE_IS_EXCLUDE:
changed = br_multicast_isexc(pg, grec->grec_src, nsrcs,
sizeof(struct in6_addr));
changed = br_multicast_isexc(pg, h_addr,
grec->grec_src, nsrcs,
sizeof(struct in6_addr),
grec->grec_type);
break;
case MLD2_CHANGE_TO_INCLUDE:
changed = br_multicast_toin(pg, grec->grec_src, nsrcs,
sizeof(struct in6_addr));
changed = br_multicast_toin(pg, h_addr,
grec->grec_src, nsrcs,
sizeof(struct in6_addr),
grec->grec_type);
break;
case MLD2_CHANGE_TO_EXCLUDE:
changed = br_multicast_toex(pg, grec->grec_src, nsrcs,
sizeof(struct in6_addr));
changed = br_multicast_toex(pg, h_addr,
grec->grec_src, nsrcs,
sizeof(struct in6_addr),
grec->grec_type);
break;
case MLD2_BLOCK_OLD_SOURCES:
changed = br_multicast_block(pg, grec->grec_src, nsrcs,
sizeof(struct in6_addr));
changed = br_multicast_block(pg, h_addr,
grec->grec_src, nsrcs,
sizeof(struct in6_addr),
grec->grec_type);
break;
}
if (changed)
......
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (c) 2020, Nikolay Aleksandrov <nikolay@nvidia.com>
#include <linux/err.h>
#include <linux/export.h>
#include <linux/if_ether.h>
#include <linux/igmp.h>
#include <linux/in.h>
#include <linux/jhash.h>
#include <linux/kernel.h>
#include <linux/log2.h>
#include <linux/netdevice.h>
#include <linux/netfilter_bridge.h>
#include <linux/random.h>
#include <linux/rculist.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include <linux/inetdevice.h>
#include <linux/mroute.h>
#include <net/ip.h>
#include <net/switchdev.h>
#if IS_ENABLED(CONFIG_IPV6)
#include <linux/icmpv6.h>
#include <net/ipv6.h>
#include <net/mld.h>
#include <net/ip6_checksum.h>
#include <net/addrconf.h>
#endif
#include "br_private.h"
#include "br_private_mcast_eht.h"
static bool br_multicast_del_eht_set_entry(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *src_addr,
union net_bridge_eht_addr *h_addr);
static void br_multicast_create_eht_set_entry(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *src_addr,
union net_bridge_eht_addr *h_addr,
int filter_mode,
bool allow_zero_src);
static struct net_bridge_group_eht_host *
br_multicast_eht_host_lookup(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *h_addr)
{
struct rb_node *node = pg->eht_host_tree.rb_node;
while (node) {
struct net_bridge_group_eht_host *this;
int result;
this = rb_entry(node, struct net_bridge_group_eht_host,
rb_node);
result = memcmp(h_addr, &this->h_addr, sizeof(*h_addr));
if (result < 0)
node = node->rb_left;
else if (result > 0)
node = node->rb_right;
else
return this;
}
return NULL;
}
static int br_multicast_eht_host_filter_mode(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *h_addr)
{
struct net_bridge_group_eht_host *eht_host;
eht_host = br_multicast_eht_host_lookup(pg, h_addr);
if (!eht_host)
return MCAST_INCLUDE;
return eht_host->filter_mode;
}
static struct net_bridge_group_eht_set_entry *
br_multicast_eht_set_entry_lookup(struct net_bridge_group_eht_set *eht_set,
union net_bridge_eht_addr *h_addr)
{
struct rb_node *node = eht_set->entry_tree.rb_node;
while (node) {
struct net_bridge_group_eht_set_entry *this;
int result;
this = rb_entry(node, struct net_bridge_group_eht_set_entry,
rb_node);
result = memcmp(h_addr, &this->h_addr, sizeof(*h_addr));
if (result < 0)
node = node->rb_left;
else if (result > 0)
node = node->rb_right;
else
return this;
}
return NULL;
}
static struct net_bridge_group_eht_set *
br_multicast_eht_set_lookup(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *src_addr)
{
struct rb_node *node = pg->eht_set_tree.rb_node;
while (node) {
struct net_bridge_group_eht_set *this;
int result;
this = rb_entry(node, struct net_bridge_group_eht_set,
rb_node);
result = memcmp(src_addr, &this->src_addr, sizeof(*src_addr));
if (result < 0)
node = node->rb_left;
else if (result > 0)
node = node->rb_right;
else
return this;
}
return NULL;
}
static void __eht_destroy_host(struct net_bridge_group_eht_host *eht_host)
{
WARN_ON(!hlist_empty(&eht_host->set_entries));
rb_erase(&eht_host->rb_node, &eht_host->pg->eht_host_tree);
RB_CLEAR_NODE(&eht_host->rb_node);
kfree(eht_host);
}
static void br_multicast_destroy_eht_set_entry(struct net_bridge_mcast_gc *gc)
{
struct net_bridge_group_eht_set_entry *set_h;
set_h = container_of(gc, struct net_bridge_group_eht_set_entry, mcast_gc);
WARN_ON(!RB_EMPTY_NODE(&set_h->rb_node));
del_timer_sync(&set_h->timer);
kfree(set_h);
}
static void br_multicast_destroy_eht_set(struct net_bridge_mcast_gc *gc)
{
struct net_bridge_group_eht_set *eht_set;
eht_set = container_of(gc, struct net_bridge_group_eht_set, mcast_gc);
WARN_ON(!RB_EMPTY_NODE(&eht_set->rb_node));
WARN_ON(!RB_EMPTY_ROOT(&eht_set->entry_tree));
del_timer_sync(&eht_set->timer);
kfree(eht_set);
}
static void __eht_del_set_entry(struct net_bridge_group_eht_set_entry *set_h)
{
struct net_bridge_group_eht_host *eht_host = set_h->h_parent;
union net_bridge_eht_addr zero_addr;
rb_erase(&set_h->rb_node, &set_h->eht_set->entry_tree);
RB_CLEAR_NODE(&set_h->rb_node);
hlist_del_init(&set_h->host_list);
memset(&zero_addr, 0, sizeof(zero_addr));
if (memcmp(&set_h->h_addr, &zero_addr, sizeof(zero_addr)))
eht_host->num_entries--;
hlist_add_head(&set_h->mcast_gc.gc_node, &set_h->br->mcast_gc_list);
queue_work(system_long_wq, &set_h->br->mcast_gc_work);
if (hlist_empty(&eht_host->set_entries))
__eht_destroy_host(eht_host);
}
static void br_multicast_del_eht_set(struct net_bridge_group_eht_set *eht_set)
{
struct net_bridge_group_eht_set_entry *set_h;
struct rb_node *node;
while ((node = rb_first(&eht_set->entry_tree))) {
set_h = rb_entry(node, struct net_bridge_group_eht_set_entry,
rb_node);
__eht_del_set_entry(set_h);
}
rb_erase(&eht_set->rb_node, &eht_set->pg->eht_set_tree);
RB_CLEAR_NODE(&eht_set->rb_node);
hlist_add_head(&eht_set->mcast_gc.gc_node, &eht_set->br->mcast_gc_list);
queue_work(system_long_wq, &eht_set->br->mcast_gc_work);
}
void br_multicast_eht_clean_sets(struct net_bridge_port_group *pg)
{
struct net_bridge_group_eht_set *eht_set;
struct rb_node *node;
while ((node = rb_first(&pg->eht_set_tree))) {
eht_set = rb_entry(node, struct net_bridge_group_eht_set,
rb_node);
br_multicast_del_eht_set(eht_set);
}
}
static void br_multicast_eht_set_entry_expired(struct timer_list *t)
{
struct net_bridge_group_eht_set_entry *set_h = from_timer(set_h, t, timer);
struct net_bridge *br = set_h->br;
spin_lock(&br->multicast_lock);
if (RB_EMPTY_NODE(&set_h->rb_node) || timer_pending(&set_h->timer))
goto out;
br_multicast_del_eht_set_entry(set_h->eht_set->pg,
&set_h->eht_set->src_addr,
&set_h->h_addr);
out:
spin_unlock(&br->multicast_lock);
}
static void br_multicast_eht_set_expired(struct timer_list *t)
{
struct net_bridge_group_eht_set *eht_set = from_timer(eht_set, t,
timer);
struct net_bridge *br = eht_set->br;
spin_lock(&br->multicast_lock);
if (RB_EMPTY_NODE(&eht_set->rb_node) || timer_pending(&eht_set->timer))
goto out;
br_multicast_del_eht_set(eht_set);
out:
spin_unlock(&br->multicast_lock);
}
static struct net_bridge_group_eht_host *
__eht_lookup_create_host(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *h_addr,
unsigned char filter_mode)
{
struct rb_node **link = &pg->eht_host_tree.rb_node, *parent = NULL;
struct net_bridge_group_eht_host *eht_host;
while (*link) {
struct net_bridge_group_eht_host *this;
int result;
this = rb_entry(*link, struct net_bridge_group_eht_host,
rb_node);
result = memcmp(h_addr, &this->h_addr, sizeof(*h_addr));
parent = *link;
if (result < 0)
link = &((*link)->rb_left);
else if (result > 0)
link = &((*link)->rb_right);
else
return this;
}
eht_host = kzalloc(sizeof(*eht_host), GFP_ATOMIC);
if (!eht_host)
return NULL;
memcpy(&eht_host->h_addr, h_addr, sizeof(*h_addr));
INIT_HLIST_HEAD(&eht_host->set_entries);
eht_host->pg = pg;
eht_host->filter_mode = filter_mode;
rb_link_node(&eht_host->rb_node, parent, link);
rb_insert_color(&eht_host->rb_node, &pg->eht_host_tree);
return eht_host;
}
static struct net_bridge_group_eht_set_entry *
__eht_lookup_create_set_entry(struct net_bridge *br,
struct net_bridge_group_eht_set *eht_set,
struct net_bridge_group_eht_host *eht_host,
bool allow_zero_src)
{
struct rb_node **link = &eht_set->entry_tree.rb_node, *parent = NULL;
struct net_bridge_group_eht_set_entry *set_h;
while (*link) {
struct net_bridge_group_eht_set_entry *this;
int result;
this = rb_entry(*link, struct net_bridge_group_eht_set_entry,
rb_node);
result = memcmp(&eht_host->h_addr, &this->h_addr,
sizeof(union net_bridge_eht_addr));
parent = *link;
if (result < 0)
link = &((*link)->rb_left);
else if (result > 0)
link = &((*link)->rb_right);
else
return this;
}
/* always allow auto-created zero entry */
if (!allow_zero_src && eht_host->num_entries >= PG_SRC_ENT_LIMIT)
return NULL;
set_h = kzalloc(sizeof(*set_h), GFP_ATOMIC);
if (!set_h)
return NULL;
memcpy(&set_h->h_addr, &eht_host->h_addr,
sizeof(union net_bridge_eht_addr));
set_h->mcast_gc.destroy = br_multicast_destroy_eht_set_entry;
set_h->eht_set = eht_set;
set_h->h_parent = eht_host;
set_h->br = br;
timer_setup(&set_h->timer, br_multicast_eht_set_entry_expired, 0);
hlist_add_head(&set_h->host_list, &eht_host->set_entries);
rb_link_node(&set_h->rb_node, parent, link);
rb_insert_color(&set_h->rb_node, &eht_set->entry_tree);
/* we must not count the auto-created zero entry otherwise we won't be
* able to track the full list of PG_SRC_ENT_LIMIT entries
*/
if (!allow_zero_src)
eht_host->num_entries++;
return set_h;
}
static struct net_bridge_group_eht_set *
__eht_lookup_create_set(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *src_addr)
{
struct rb_node **link = &pg->eht_set_tree.rb_node, *parent = NULL;
struct net_bridge_group_eht_set *eht_set;
while (*link) {
struct net_bridge_group_eht_set *this;
int result;
this = rb_entry(*link, struct net_bridge_group_eht_set,
rb_node);
result = memcmp(src_addr, &this->src_addr, sizeof(*src_addr));
parent = *link;
if (result < 0)
link = &((*link)->rb_left);
else if (result > 0)
link = &((*link)->rb_right);
else
return this;
}
eht_set = kzalloc(sizeof(*eht_set), GFP_ATOMIC);
if (!eht_set)
return NULL;
memcpy(&eht_set->src_addr, src_addr, sizeof(*src_addr));
eht_set->mcast_gc.destroy = br_multicast_destroy_eht_set;
eht_set->pg = pg;
eht_set->br = pg->key.port->br;
eht_set->entry_tree = RB_ROOT;
timer_setup(&eht_set->timer, br_multicast_eht_set_expired, 0);
rb_link_node(&eht_set->rb_node, parent, link);
rb_insert_color(&eht_set->rb_node, &pg->eht_set_tree);
return eht_set;
}
static void br_multicast_ip_src_to_eht_addr(const struct br_ip *src,
union net_bridge_eht_addr *dest)
{
switch (src->proto) {
case htons(ETH_P_IP):
dest->ip4 = src->src.ip4;
break;
#if IS_ENABLED(CONFIG_IPV6)
case htons(ETH_P_IPV6):
memcpy(&dest->ip6, &src->src.ip6, sizeof(struct in6_addr));
break;
#endif
}
}
static void br_eht_convert_host_filter_mode(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *h_addr,
int filter_mode)
{
struct net_bridge_group_eht_host *eht_host;
union net_bridge_eht_addr zero_addr;
eht_host = br_multicast_eht_host_lookup(pg, h_addr);
if (eht_host)
eht_host->filter_mode = filter_mode;
memset(&zero_addr, 0, sizeof(zero_addr));
switch (filter_mode) {
case MCAST_INCLUDE:
br_multicast_del_eht_set_entry(pg, &zero_addr, h_addr);
break;
case MCAST_EXCLUDE:
br_multicast_create_eht_set_entry(pg, &zero_addr, h_addr,
MCAST_EXCLUDE,
true);
break;
}
}
static void br_multicast_create_eht_set_entry(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *src_addr,
union net_bridge_eht_addr *h_addr,
int filter_mode,
bool allow_zero_src)
{
struct net_bridge_group_eht_set_entry *set_h;
struct net_bridge_group_eht_host *eht_host;
struct net_bridge *br = pg->key.port->br;
struct net_bridge_group_eht_set *eht_set;
union net_bridge_eht_addr zero_addr;
memset(&zero_addr, 0, sizeof(zero_addr));
if (!allow_zero_src && !memcmp(src_addr, &zero_addr, sizeof(zero_addr)))
return;
eht_set = __eht_lookup_create_set(pg, src_addr);
if (!eht_set)
return;
eht_host = __eht_lookup_create_host(pg, h_addr, filter_mode);
if (!eht_host)
goto fail_host;
set_h = __eht_lookup_create_set_entry(br, eht_set, eht_host,
allow_zero_src);
if (!set_h)
goto fail_set_entry;
mod_timer(&set_h->timer, jiffies + br_multicast_gmi(br));
mod_timer(&eht_set->timer, jiffies + br_multicast_gmi(br));
return;
fail_set_entry:
if (hlist_empty(&eht_host->set_entries))
__eht_destroy_host(eht_host);
fail_host:
if (RB_EMPTY_ROOT(&eht_set->entry_tree))
br_multicast_del_eht_set(eht_set);
}
static bool br_multicast_del_eht_set_entry(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *src_addr,
union net_bridge_eht_addr *h_addr)
{
struct net_bridge_group_eht_set_entry *set_h;
struct net_bridge_group_eht_set *eht_set;
bool set_deleted = false;
eht_set = br_multicast_eht_set_lookup(pg, src_addr);
if (!eht_set)
goto out;
set_h = br_multicast_eht_set_entry_lookup(eht_set, h_addr);
if (!set_h)
goto out;
__eht_del_set_entry(set_h);
if (RB_EMPTY_ROOT(&eht_set->entry_tree)) {
br_multicast_del_eht_set(eht_set);
set_deleted = true;
}
out:
return set_deleted;
}
static void br_multicast_del_eht_host(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *h_addr)
{
struct net_bridge_group_eht_set_entry *set_h;
struct net_bridge_group_eht_host *eht_host;
struct hlist_node *tmp;
eht_host = br_multicast_eht_host_lookup(pg, h_addr);
if (!eht_host)
return;
hlist_for_each_entry_safe(set_h, tmp, &eht_host->set_entries, host_list)
br_multicast_del_eht_set_entry(set_h->eht_set->pg,
&set_h->eht_set->src_addr,
&set_h->h_addr);
}
static void __eht_allow_incl(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *h_addr,
void *srcs,
u32 nsrcs,
size_t addr_size)
{
union net_bridge_eht_addr eht_src_addr;
u32 src_idx;
memset(&eht_src_addr, 0, sizeof(eht_src_addr));
for (src_idx = 0; src_idx < nsrcs; src_idx++) {
memcpy(&eht_src_addr, srcs + (src_idx * addr_size), addr_size);
br_multicast_create_eht_set_entry(pg, &eht_src_addr, h_addr,
MCAST_INCLUDE,
false);
}
}
static bool __eht_allow_excl(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *h_addr,
void *srcs,
u32 nsrcs,
size_t addr_size)
{
bool changed = false, host_excl = false;
union net_bridge_eht_addr eht_src_addr;
struct net_bridge_group_src *src_ent;
struct br_ip src_ip;
u32 src_idx;
host_excl = !!(br_multicast_eht_host_filter_mode(pg, h_addr) == MCAST_EXCLUDE);
memset(&eht_src_addr, 0, sizeof(eht_src_addr));
for (src_idx = 0; src_idx < nsrcs; src_idx++) {
memcpy(&eht_src_addr, srcs + (src_idx * addr_size), addr_size);
if (!host_excl) {
br_multicast_create_eht_set_entry(pg, &eht_src_addr, h_addr,
MCAST_INCLUDE,
false);
} else {
if (!br_multicast_del_eht_set_entry(pg, &eht_src_addr,
h_addr))
continue;
memcpy(&src_ip, srcs + (src_idx * addr_size), addr_size);
src_ent = br_multicast_find_group_src(pg, &src_ip);
if (!src_ent)
continue;
br_multicast_del_group_src(src_ent, true);
changed = true;
}
}
return changed;
}
static bool br_multicast_eht_allow(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *h_addr,
void *srcs,
u32 nsrcs,
size_t addr_size)
{
bool changed = false;
switch (br_multicast_eht_host_filter_mode(pg, h_addr)) {
case MCAST_INCLUDE:
__eht_allow_incl(pg, h_addr, srcs, nsrcs, addr_size);
break;
case MCAST_EXCLUDE:
changed = __eht_allow_excl(pg, h_addr, srcs, nsrcs, addr_size);
break;
}
return changed;
}
static bool __eht_block_incl(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *h_addr,
void *srcs,
u32 nsrcs,
size_t addr_size)
{
union net_bridge_eht_addr eht_src_addr;
struct net_bridge_group_src *src_ent;
bool changed = false;
struct br_ip src_ip;
u32 src_idx;
memset(&eht_src_addr, 0, sizeof(eht_src_addr));
memset(&src_ip, 0, sizeof(src_ip));
src_ip.proto = pg->key.addr.proto;
for (src_idx = 0; src_idx < nsrcs; src_idx++) {
memcpy(&eht_src_addr, srcs + (src_idx * addr_size), addr_size);
if (!br_multicast_del_eht_set_entry(pg, &eht_src_addr, h_addr))
continue;
memcpy(&src_ip, srcs + (src_idx * addr_size), addr_size);
src_ent = br_multicast_find_group_src(pg, &src_ip);
if (!src_ent)
continue;
br_multicast_del_group_src(src_ent, true);
changed = true;
}
return changed;
}
static bool __eht_block_excl(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *h_addr,
void *srcs,
u32 nsrcs,
size_t addr_size)
{
bool changed = false, host_excl = false;
union net_bridge_eht_addr eht_src_addr;
struct net_bridge_group_src *src_ent;
struct br_ip src_ip;
u32 src_idx;
host_excl = !!(br_multicast_eht_host_filter_mode(pg, h_addr) == MCAST_EXCLUDE);
memset(&eht_src_addr, 0, sizeof(eht_src_addr));
memset(&src_ip, 0, sizeof(src_ip));
src_ip.proto = pg->key.addr.proto;
for (src_idx = 0; src_idx < nsrcs; src_idx++) {
memcpy(&eht_src_addr, srcs + (src_idx * addr_size), addr_size);
if (host_excl) {
br_multicast_create_eht_set_entry(pg, &eht_src_addr, h_addr,
MCAST_EXCLUDE,
false);
} else {
if (!br_multicast_del_eht_set_entry(pg, &eht_src_addr,
h_addr))
continue;
memcpy(&src_ip, srcs + (src_idx * addr_size), addr_size);
src_ent = br_multicast_find_group_src(pg, &src_ip);
if (!src_ent)
continue;
br_multicast_del_group_src(src_ent, true);
changed = true;
}
}
return changed;
}
static bool br_multicast_eht_block(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *h_addr,
void *srcs,
u32 nsrcs,
size_t addr_size)
{
bool changed = false;
switch (br_multicast_eht_host_filter_mode(pg, h_addr)) {
case MCAST_INCLUDE:
changed = __eht_block_incl(pg, h_addr, srcs, nsrcs, addr_size);
break;
case MCAST_EXCLUDE:
changed = __eht_block_excl(pg, h_addr, srcs, nsrcs, addr_size);
break;
}
return changed;
}
/* flush_entries is true when changing mode */
static bool __eht_inc_exc(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *h_addr,
void *srcs,
u32 nsrcs,
size_t addr_size,
unsigned char filter_mode,
bool to_report)
{
bool changed = false, flush_entries = to_report;
union net_bridge_eht_addr eht_src_addr;
u32 src_idx;
if (br_multicast_eht_host_filter_mode(pg, h_addr) != filter_mode)
flush_entries = true;
memset(&eht_src_addr, 0, sizeof(eht_src_addr));
/* if we're changing mode del host and its entries */
if (flush_entries)
br_multicast_del_eht_host(pg, h_addr);
for (src_idx = 0; src_idx < nsrcs; src_idx++) {
memcpy(&eht_src_addr, srcs + (src_idx * addr_size), addr_size);
br_multicast_create_eht_set_entry(pg, &eht_src_addr, h_addr,
filter_mode, false);
}
/* we can be missing sets only if we've deleted some entries */
if (flush_entries) {
struct net_bridge *br = pg->key.port->br;
struct net_bridge_group_eht_set *eht_set;
struct net_bridge_group_src *src_ent;
struct hlist_node *tmp;
hlist_for_each_entry_safe(src_ent, tmp, &pg->src_list, node) {
br_multicast_ip_src_to_eht_addr(&src_ent->addr,
&eht_src_addr);
if (!br_multicast_eht_set_lookup(pg, &eht_src_addr)) {
br_multicast_del_group_src(src_ent, true);
changed = true;
continue;
}
/* this is an optimization for TO_INCLUDE where we lower
* the set's timeout to LMQT to catch timeout hosts:
* - host A (timing out): set entries X, Y
* - host B: set entry Z (new from current TO_INCLUDE)
* sends BLOCK Z after LMQT but host A's EHT
* entries still exist (unless lowered to LMQT
* so they can timeout with the S,Gs)
* => we wait another LMQT, when we can just delete the
* group immediately
*/
if (!(src_ent->flags & BR_SGRP_F_SEND) ||
filter_mode != MCAST_INCLUDE ||
!to_report)
continue;
eht_set = br_multicast_eht_set_lookup(pg,
&eht_src_addr);
if (!eht_set)
continue;
mod_timer(&eht_set->timer, jiffies + br_multicast_lmqt(br));
}
}
return changed;
}
static bool br_multicast_eht_inc(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *h_addr,
void *srcs,
u32 nsrcs,
size_t addr_size,
bool to_report)
{
bool changed;
changed = __eht_inc_exc(pg, h_addr, srcs, nsrcs, addr_size,
MCAST_INCLUDE, to_report);
br_eht_convert_host_filter_mode(pg, h_addr, MCAST_INCLUDE);
return changed;
}
static bool br_multicast_eht_exc(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *h_addr,
void *srcs,
u32 nsrcs,
size_t addr_size,
bool to_report)
{
bool changed;
changed = __eht_inc_exc(pg, h_addr, srcs, nsrcs, addr_size,
MCAST_EXCLUDE, to_report);
br_eht_convert_host_filter_mode(pg, h_addr, MCAST_EXCLUDE);
return changed;
}
static bool __eht_ip4_handle(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *h_addr,
void *srcs,
u32 nsrcs,
int grec_type)
{
bool changed = false, to_report = false;
switch (grec_type) {
case IGMPV3_ALLOW_NEW_SOURCES:
br_multicast_eht_allow(pg, h_addr, srcs, nsrcs, sizeof(__be32));
break;
case IGMPV3_BLOCK_OLD_SOURCES:
changed = br_multicast_eht_block(pg, h_addr, srcs, nsrcs,
sizeof(__be32));
break;
case IGMPV3_CHANGE_TO_INCLUDE:
to_report = true;
fallthrough;
case IGMPV3_MODE_IS_INCLUDE:
changed = br_multicast_eht_inc(pg, h_addr, srcs, nsrcs,
sizeof(__be32), to_report);
break;
case IGMPV3_CHANGE_TO_EXCLUDE:
to_report = true;
fallthrough;
case IGMPV3_MODE_IS_EXCLUDE:
changed = br_multicast_eht_exc(pg, h_addr, srcs, nsrcs,
sizeof(__be32), to_report);
break;
}
return changed;
}
#if IS_ENABLED(CONFIG_IPV6)
static bool __eht_ip6_handle(struct net_bridge_port_group *pg,
union net_bridge_eht_addr *h_addr,
void *srcs,
u32 nsrcs,
int grec_type)
{
bool changed = false, to_report = false;
switch (grec_type) {
case MLD2_ALLOW_NEW_SOURCES:
br_multicast_eht_allow(pg, h_addr, srcs, nsrcs,
sizeof(struct in6_addr));
break;
case MLD2_BLOCK_OLD_SOURCES:
changed = br_multicast_eht_block(pg, h_addr, srcs, nsrcs,
sizeof(struct in6_addr));
break;
case MLD2_CHANGE_TO_INCLUDE:
to_report = true;
fallthrough;
case MLD2_MODE_IS_INCLUDE:
changed = br_multicast_eht_inc(pg, h_addr, srcs, nsrcs,
sizeof(struct in6_addr),
to_report);
break;
case MLD2_CHANGE_TO_EXCLUDE:
to_report = true;
fallthrough;
case MLD2_MODE_IS_EXCLUDE:
changed = br_multicast_eht_exc(pg, h_addr, srcs, nsrcs,
sizeof(struct in6_addr),
to_report);
break;
}
return changed;
}
#endif
/* true means an entry was deleted */
bool br_multicast_eht_handle(struct net_bridge_port_group *pg,
void *h_addr,
void *srcs,
u32 nsrcs,
size_t addr_size,
int grec_type)
{
bool eht_enabled = !!(pg->key.port->flags & BR_MULTICAST_FAST_LEAVE);
union net_bridge_eht_addr eht_host_addr;
bool changed = false;
if (!eht_enabled)
goto out;
memset(&eht_host_addr, 0, sizeof(eht_host_addr));
memcpy(&eht_host_addr, h_addr, addr_size);
if (addr_size == sizeof(__be32))
changed = __eht_ip4_handle(pg, &eht_host_addr, srcs, nsrcs,
grec_type);
#if IS_ENABLED(CONFIG_IPV6)
else
changed = __eht_ip6_handle(pg, &eht_host_addr, srcs, nsrcs,
grec_type);
#endif
out:
return changed;
}
......@@ -252,6 +252,8 @@ struct net_bridge_port_group {
struct timer_list timer;
struct timer_list rexmit_timer;
struct hlist_node mglist;
struct rb_root eht_set_tree;
struct rb_root eht_host_tree;
struct rhash_head rhnode;
struct net_bridge_mcast_gc mcast_gc;
......@@ -846,6 +848,10 @@ void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg,
u8 filter_mode);
void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp,
struct net_bridge_port_group *sg);
struct net_bridge_group_src *
br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip);
void br_multicast_del_group_src(struct net_bridge_group_src *src,
bool fastleave);
static inline bool br_group_is_l2(const struct br_ip *group)
{
......
/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright (c) 2020, Nikolay Aleksandrov <nikolay@nvidia.com>
*/
#ifndef _BR_PRIVATE_MCAST_EHT_H_
#define _BR_PRIVATE_MCAST_EHT_H_
union net_bridge_eht_addr {
__be32 ip4;
#if IS_ENABLED(CONFIG_IPV6)
struct in6_addr ip6;
#endif
};
/* single host's list of set entries and filter_mode */
struct net_bridge_group_eht_host {
struct rb_node rb_node;
union net_bridge_eht_addr h_addr;
struct hlist_head set_entries;
unsigned int num_entries;
unsigned char filter_mode;
struct net_bridge_port_group *pg;
};
/* (host, src entry) added to a per-src set and host's list */
struct net_bridge_group_eht_set_entry {
struct rb_node rb_node;
struct hlist_node host_list;
union net_bridge_eht_addr h_addr;
struct timer_list timer;
struct net_bridge *br;
struct net_bridge_group_eht_set *eht_set;
struct net_bridge_group_eht_host *h_parent;
struct net_bridge_mcast_gc mcast_gc;
};
/* per-src set */
struct net_bridge_group_eht_set {
struct rb_node rb_node;
union net_bridge_eht_addr src_addr;
struct rb_root entry_tree;
struct timer_list timer;
struct net_bridge_port_group *pg;
struct net_bridge *br;
struct net_bridge_mcast_gc mcast_gc;
};
void br_multicast_eht_clean_sets(struct net_bridge_port_group *pg);
bool br_multicast_eht_handle(struct net_bridge_port_group *pg,
void *h_addr,
void *srcs,
u32 nsrcs,
size_t addr_size,
int grec_type);
static inline bool
br_multicast_eht_should_del_pg(const struct net_bridge_port_group *pg)
{
return !!((pg->key.port->flags & BR_MULTICAST_FAST_LEAVE) &&
RB_EMPTY_ROOT(&pg->eht_host_tree));
}
#endif /* _BR_PRIVATE_MCAST_EHT_H_ */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment