Netdev List
 help / color / mirror / Atom feed
* [PATCH net-next v3 0/5] net: dsa: motorcomm: Reject isolated redirect targets
@ 2026-12-01  8:14 David Yang
  2026-12-01  8:14 ` [PATCH net-next v3 1/5] net: dsa: motorcomm: Remove local cpu_ports_mask David Yang
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: David Yang @ 2026-12-01  8:14 UTC (permalink / raw)
  To: netdev
  Cc: David Yang, Andrew Lunn, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni

Port isolation takes after REDIR judgment, but software delivers such
frames. Reject such offloading and handle bridge changes.

Although triggered on YT9215S, target to net-next for low severity but
major refactor.

v3:
 - compute the isolation masks in pre_bridge_flags
 - fix code type
v2: https://lore.kernel.org/r/20261129115556.1669188-1-mmyangfl@gmail.com
 - recompute redirect rules on bridge changes
 - reject bridge flags that break redirect rules
v1: https://lore.kernel.org/r/20261127142732.1337041-1-mmyangfl@gmail.com

David Yang (5):
  net: dsa: motorcomm: Remove local cpu_ports_mask
  net: dsa: motorcomm: Cache port isolation masks
  net: dsa: motorcomm: Reject isolated redirect targets
  net: dsa: motorcomm: Reject bridge flags that break redirect rules
  net: dsa: motorcomm: Recompute redirect rules on bridge changes

 drivers/net/dsa/motorcomm/chip.c | 168 ++++++++++++++++++++++---------
 drivers/net/dsa/motorcomm/chip.h |   7 +-
 drivers/net/dsa/motorcomm/tc.c   | 148 +++++++++++++++++++++++++--
 drivers/net/dsa/motorcomm/tc.h   |   8 +-
 4 files changed, 273 insertions(+), 58 deletions(-)

-- 
2.53.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH net-next v3 1/5] net: dsa: motorcomm: Remove local cpu_ports_mask
  2026-12-01  8:14 [PATCH net-next v3 0/5] net: dsa: motorcomm: Reject isolated redirect targets David Yang
@ 2026-12-01  8:14 ` David Yang
  2026-12-01  8:14 ` [PATCH net-next v3 2/5] net: dsa: motorcomm: Cache port isolation masks David Yang
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: David Yang @ 2026-12-01  8:14 UTC (permalink / raw)
  To: netdev
  Cc: David Yang, Andrew Lunn, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni

Use dsa_cpu_ports() and dsa_is_cpu_port() instead for a clearer view of
code.

Signed-off-by: David Yang <mmyangfl@gmail.com>
---
 drivers/net/dsa/motorcomm/chip.c | 18 +++++++++---------
 drivers/net/dsa/motorcomm/chip.h |  2 --
 2 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/drivers/net/dsa/motorcomm/chip.c b/drivers/net/dsa/motorcomm/chip.c
index a21d4bb5e141..49c9055c7512 100644
--- a/drivers/net/dsa/motorcomm/chip.c
+++ b/drivers/net/dsa/motorcomm/chip.c
@@ -1101,7 +1101,7 @@ yt921x_vlan_add(struct yt921x_priv *priv, int port, u16 vid, bool untagged)
 	u32 ctrls[2];
 
 	masks[0] = YT921X_VLAN_CTRLa_PORTn(port) |
-		   YT921X_VLAN_CTRLa_PORTS(priv->cpu_ports_mask);
+		   YT921X_VLAN_CTRLa_PORTS(dsa_cpu_ports(&priv->ds));
 	ctrls[0] = masks[0];
 
 	masks[1] = YT921X_VLAN_CTRLb_UNTAG_PORTn(port);
@@ -1256,11 +1256,12 @@ yt921x_dsa_port_vlan_add(struct dsa_switch *ds, int port,
 
 static int yt921x_userport_standalone(struct yt921x_priv *priv, int port)
 {
+	struct dsa_switch *ds = &priv->ds;
 	u32 mask;
 	u32 ctrl;
 	int res;
 
-	ctrl = ~priv->cpu_ports_mask;
+	ctrl = ~dsa_cpu_ports(ds);
 	res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port), ctrl);
 	if (res)
 		return res;
@@ -1306,7 +1307,7 @@ static int yt921x_isolate(struct yt921x_priv *priv, int port)
 
 	mask = BIT(port);
 	for (int i = 0; i < YT921X_PORT_NUM; i++) {
-		if ((BIT(i) & priv->cpu_ports_mask) || i == port)
+		if (dsa_is_cpu_port(&priv->ds, i) || i == port)
 			continue;
 
 		res = yt921x_reg_set_bits(priv, YT921X_PORTn_ISOLATION(i),
@@ -1323,7 +1324,7 @@ static int yt921x_isolate(struct yt921x_priv *priv, int port)
  */
 static int yt921x_bridge(struct yt921x_priv *priv, u16 ports_mask)
 {
-	unsigned long targets_mask = ports_mask & ~priv->cpu_ports_mask;
+	unsigned long targets_mask = ports_mask & ~dsa_cpu_ports(&priv->ds);
 	u32 isolated_mask;
 	u32 ctrl;
 	int port;
@@ -1436,7 +1437,7 @@ yt921x_bridge_flags(struct yt921x_priv *priv, int port,
 			u32 ports_mask;
 
 			ports_mask = dsa_bridge_ports(ds, bdev);
-			ports_mask |= priv->cpu_ports_mask;
+			ports_mask |= dsa_cpu_ports(ds);
 			res = yt921x_bridge(priv, ports_mask);
 			if (res)
 				return res;
@@ -1511,7 +1512,7 @@ yt921x_dsa_port_bridge_join(struct dsa_switch *ds, int port,
 		return 0;
 
 	ports_mask = dsa_bridge_ports(ds, bridge.dev);
-	ports_mask |= priv->cpu_ports_mask;
+	ports_mask |= dsa_cpu_ports(ds);
 
 	dp = dsa_to_port(ds, port);
 	brport_dev = dsa_port_to_bridge_port(dp);
@@ -2377,10 +2378,10 @@ static int yt921x_chip_setup_dsa(struct yt921x_priv *priv)
 	int res;
 
 	/* Enable DSA */
-	priv->cpu_ports_mask = dsa_cpu_ports(ds);
+	cpu_ports_mask = dsa_cpu_ports(ds);
 
 	ctrl = YT921X_EXT_CPU_PORT_TAG_EN | YT921X_EXT_CPU_PORT_PORT_EN |
-	       YT921X_EXT_CPU_PORT_PORT(__ffs(priv->cpu_ports_mask));
+	       YT921X_EXT_CPU_PORT_PORT(__ffs(cpu_ports_mask));
 	res = yt921x_reg_write(priv, YT921X_EXT_CPU_PORT, ctrl);
 	if (res)
 		return res;
@@ -2417,7 +2418,6 @@ static int yt921x_chip_setup_dsa(struct yt921x_priv *priv)
 	/* Except for CPU ports, if any packets are sent via CPU ports without
 	 * tag, they should be dropped.
 	 */
-	cpu_ports_mask = priv->cpu_ports_mask;
 	for_each_set_bit(port, &cpu_ports_mask, YT921X_PORT_NUM) {
 		ctrl &= ~YT921X_ACT_UNK_ACTn_M(port);
 		ctrl |= YT921X_ACT_UNK_ACTn_DROP(port);
diff --git a/drivers/net/dsa/motorcomm/chip.h b/drivers/net/dsa/motorcomm/chip.h
index fc315f1d8f87..d5ed580f8a78 100644
--- a/drivers/net/dsa/motorcomm/chip.h
+++ b/drivers/net/dsa/motorcomm/chip.h
@@ -509,8 +509,6 @@ struct yt921x_priv {
 	const struct yt921x_info *info;
 	unsigned int meter_slot_ns;
 	unsigned int port_shape_slot_ns;
-	/* cache of dsa_cpu_ports(ds) */
-	u16 cpu_ports_mask;
 	unsigned char cycle_ns;
 
 	/* protect the access to the switch registers */
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH net-next v3 2/5] net: dsa: motorcomm: Cache port isolation masks
  2026-12-01  8:14 [PATCH net-next v3 0/5] net: dsa: motorcomm: Reject isolated redirect targets David Yang
  2026-12-01  8:14 ` [PATCH net-next v3 1/5] net: dsa: motorcomm: Remove local cpu_ports_mask David Yang
@ 2026-12-01  8:14 ` David Yang
  2026-12-01  8:14 ` [PATCH net-next v3 3/5] net: dsa: motorcomm: Reject isolated redirect targets David Yang
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: David Yang @ 2026-12-01  8:14 UTC (permalink / raw)
  To: netdev
  Cc: David Yang, Andrew Lunn, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni

For a following commit, an accurate snapshot of port isolation masks is
required. However, the bridge core invokes .port_pre_bridge_flags()
synchronously in an atomic context, and userspace already sees such
changes applied before .port_bridge_flags() is called, causing the stale
snapshot to be used before the deferred call.

Calculate first, so that part can be made atomic, then apply later.

Signed-off-by: David Yang <mmyangfl@gmail.com>
---
 drivers/net/dsa/motorcomm/chip.c | 90 ++++++++++++++++++--------------
 drivers/net/dsa/motorcomm/chip.h |  4 ++
 2 files changed, 56 insertions(+), 38 deletions(-)

diff --git a/drivers/net/dsa/motorcomm/chip.c b/drivers/net/dsa/motorcomm/chip.c
index 49c9055c7512..6c6a87207bd5 100644
--- a/drivers/net/dsa/motorcomm/chip.c
+++ b/drivers/net/dsa/motorcomm/chip.c
@@ -1256,12 +1256,14 @@ yt921x_dsa_port_vlan_add(struct dsa_switch *ds, int port,
 
 static int yt921x_userport_standalone(struct yt921x_priv *priv, int port)
 {
+	struct yt921x_port *pp = &priv->ports[port];
 	struct dsa_switch *ds = &priv->ds;
 	u32 mask;
 	u32 ctrl;
 	int res;
 
 	ctrl = ~dsa_cpu_ports(ds);
+	pp->isolation_mask = ctrl;
 	res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port), ctrl);
 	if (res)
 		return res;
@@ -1300,35 +1302,22 @@ static int yt921x_userport_bridge(struct yt921x_priv *priv, int port)
 	return 0;
 }
 
-static int yt921x_isolate(struct yt921x_priv *priv, int port)
+static void yt921x_isolation_calc_standalone(struct yt921x_priv *priv, int port)
 {
-	u32 mask;
-	int res;
-
-	mask = BIT(port);
-	for (int i = 0; i < YT921X_PORT_NUM; i++) {
-		if (dsa_is_cpu_port(&priv->ds, i) || i == port)
-			continue;
-
-		res = yt921x_reg_set_bits(priv, YT921X_PORTn_ISOLATION(i),
-					  mask);
-		if (res)
-			return res;
-	}
-
-	return 0;
+	for (int i = 0; i < YT921X_PORT_NUM; i++)
+		priv->ports[i].isolation_mask |= BIT(port);
 }
 
 /* Make sure to include the CPU port in ports_mask, or your bridge will
  * not have it.
  */
-static int yt921x_bridge(struct yt921x_priv *priv, u16 ports_mask)
+static void
+yt921x_isolation_calc_bridge(struct yt921x_priv *priv, u16 ports_mask)
 {
 	unsigned long targets_mask = ports_mask & ~dsa_cpu_ports(&priv->ds);
 	u32 isolated_mask;
 	u32 ctrl;
 	int port;
-	int res;
 
 	isolated_mask = 0;
 	for_each_set_bit(port, &targets_mask, YT921X_PORT_NUM) {
@@ -1353,11 +1342,21 @@ static int yt921x_bridge(struct yt921x_priv *priv, u16 ports_mask)
 		else
 			ctrl &= ~BIT(port);
 
-		res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port),
-				       ctrl);
-		if (res)
-			return res;
+		pp->isolation_mask = ctrl;
 	}
+}
+
+static int yt921x_isolation_commit(struct yt921x_priv *priv, u16 ports_mask)
+{
+	int res;
+
+	for (int i = 0; i < YT921X_PORT_NUM; i++)
+		if (BIT(i) & ports_mask) {
+			res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(i),
+					       priv->ports[i].isolation_mask);
+			if (res)
+				return res;
+		}
 
 	return 0;
 }
@@ -1370,7 +1369,8 @@ static int yt921x_bridge_leave(struct yt921x_priv *priv, int port)
 	if (res)
 		return res;
 
-	res = yt921x_isolate(priv, port);
+	yt921x_isolation_calc_standalone(priv, port);
+	res = yt921x_isolation_commit(priv, ~0);
 	if (res)
 		return res;
 
@@ -1386,7 +1386,8 @@ yt921x_bridge_join(struct yt921x_priv *priv, int port, u16 ports_mask)
 	if (res)
 		return res;
 
-	res = yt921x_bridge(priv, ports_mask);
+	yt921x_isolation_calc_bridge(priv, ports_mask);
+	res = yt921x_isolation_commit(priv, ports_mask);
 	if (res)
 		return res;
 
@@ -1397,8 +1398,6 @@ static int
 yt921x_bridge_flags(struct yt921x_priv *priv, int port,
 		    struct switchdev_brport_flags flags)
 {
-	struct yt921x_port *pp = &priv->ports[port];
-	bool do_flush;
 	u32 mask;
 	int res;
 
@@ -1418,16 +1417,7 @@ yt921x_bridge_flags(struct yt921x_priv *priv, int port,
 
 	/* BR_BCAST_FLOOD: we can filter bcast, but cannot trap them */
 
-	do_flush = false;
-	if (flags.mask & BR_HAIRPIN_MODE) {
-		pp->hairpin = flags.val & BR_HAIRPIN_MODE;
-		do_flush = true;
-	}
-	if (flags.mask & BR_ISOLATED) {
-		pp->isolated = flags.val & BR_ISOLATED;
-		do_flush = true;
-	}
-	if (do_flush) {
+	if ((flags.mask & BR_HAIRPIN_MODE) || (flags.mask & BR_ISOLATED)) {
 		struct dsa_switch *ds = &priv->ds;
 		struct dsa_port *dp = dsa_to_port(ds, port);
 		struct net_device *bdev;
@@ -1437,8 +1427,7 @@ yt921x_bridge_flags(struct yt921x_priv *priv, int port,
 			u32 ports_mask;
 
 			ports_mask = dsa_bridge_ports(ds, bdev);
-			ports_mask |= dsa_cpu_ports(ds);
-			res = yt921x_bridge(priv, ports_mask);
+			res = yt921x_isolation_commit(priv, ports_mask);
 			if (res)
 				return res;
 		}
@@ -1452,9 +1441,32 @@ yt921x_dsa_port_pre_bridge_flags(struct dsa_switch *ds, int port,
 				 struct switchdev_brport_flags flags,
 				 struct netlink_ext_ack *extack)
 {
+	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
+	struct yt921x_port *pp = &priv->ports[port];
+
 	if (flags.mask & ~(BR_HAIRPIN_MODE | BR_LEARNING | BR_FLOOD |
 			   BR_MCAST_FLOOD | BR_ISOLATED))
 		return -EINVAL;
+
+	if ((flags.mask & BR_HAIRPIN_MODE) || (flags.mask & BR_ISOLATED)) {
+		struct dsa_port *dp = dsa_to_port(ds, port);
+		struct net_device *bdev;
+
+		if (flags.mask & BR_HAIRPIN_MODE)
+			pp->hairpin = flags.val & BR_HAIRPIN_MODE;
+		if (flags.mask & BR_ISOLATED)
+			pp->isolated = flags.val & BR_ISOLATED;
+
+		bdev = dsa_port_bridge_dev_get(dp);
+		if (bdev) {
+			u32 ports_mask;
+
+			ports_mask = dsa_bridge_ports(ds, bdev);
+			ports_mask |= dsa_cpu_ports(ds);
+			yt921x_isolation_calc_bridge(priv, ports_mask);
+		}
+	}
+
 	return 0;
 }
 
@@ -2046,6 +2058,7 @@ yt921x_dsa_phylink_get_caps(struct dsa_switch *ds, int port,
 
 static int yt921x_port_setup(struct yt921x_priv *priv, int port)
 {
+	struct yt921x_port *pp = &priv->ports[port];
 	struct dsa_switch *ds = &priv->ds;
 	u32 ctrl;
 	int res;
@@ -2067,6 +2080,7 @@ static int yt921x_port_setup(struct yt921x_priv *priv, int port)
 		 * without tag).
 		 */
 		ctrl = ~(u32)0;
+		pp->isolation_mask = ctrl;
 		res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port),
 				       ctrl);
 		if (res)
diff --git a/drivers/net/dsa/motorcomm/chip.h b/drivers/net/dsa/motorcomm/chip.h
index d5ed580f8a78..203371888c45 100644
--- a/drivers/net/dsa/motorcomm/chip.h
+++ b/drivers/net/dsa/motorcomm/chip.h
@@ -457,11 +457,15 @@ struct yt921x_port {
 	bool serdes:1;
 	/* Link from in-band status (PHYLINK_PCS_NEG_INBAND) */
 	bool inband:1;
+	bool:0;
+
 	/* BR_HAIRPIN_MODE */
 	bool hairpin:1;
 	/* BR_ISOLATED */
 	bool isolated:1;
 
+	u16 isolation_mask;
+
 	unsigned short acl_cnt;
 
 #if IS_ENABLED(CONFIG_NET_DSA_YT921X_LEDS)
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH net-next v3 3/5] net: dsa: motorcomm: Reject isolated redirect targets
  2026-12-01  8:14 [PATCH net-next v3 0/5] net: dsa: motorcomm: Reject isolated redirect targets David Yang
  2026-12-01  8:14 ` [PATCH net-next v3 1/5] net: dsa: motorcomm: Remove local cpu_ports_mask David Yang
  2026-12-01  8:14 ` [PATCH net-next v3 2/5] net: dsa: motorcomm: Cache port isolation masks David Yang
@ 2026-12-01  8:14 ` David Yang
  2026-12-01  8:14 ` [PATCH net-next v3 4/5] net: dsa: motorcomm: Reject bridge flags that break redirect rules David Yang
  2026-12-01  8:14 ` [PATCH net-next v3 5/5] net: dsa: motorcomm: Recompute redirect rules on bridge changes David Yang
  4 siblings, 0 replies; 6+ messages in thread
From: David Yang @ 2026-12-01  8:14 UTC (permalink / raw)
  To: netdev
  Cc: David Yang, Andrew Lunn, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni

Port isolation takes after REDIR judgment, but software delivers such
frames. Either route such rules to the software, or reject offloading.

Fixes: 8eb9e7520b17 ("net: dsa: yt921x: Add ACL support")
Signed-off-by: David Yang <mmyangfl@gmail.com>
---
 drivers/net/dsa/motorcomm/tc.c | 33 +++++++++++++++++++++++++++++++--
 drivers/net/dsa/motorcomm/tc.h |  6 +++++-
 2 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/drivers/net/dsa/motorcomm/tc.c b/drivers/net/dsa/motorcomm/tc.c
index e3f057ed580f..b7fe867da088 100644
--- a/drivers/net/dsa/motorcomm/tc.c
+++ b/drivers/net/dsa/motorcomm/tc.c
@@ -1030,6 +1030,7 @@ yt921x_acl_rule_ext_parse_flow_action(struct yt921x_acl_rule_ext *ruleext,
 	const struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
 	const struct flow_action *flow_action = &rule->action;
 	struct netlink_ext_ack *extack = cls->common.extack;
+	struct yt921x_port *pp = &priv->ports[port];
 	const struct flow_action_entry *act;
 	u32 *action = ruleext->r.action;
 	bool seen_priority = false;
@@ -1041,6 +1042,8 @@ yt921x_acl_rule_ext_parse_flow_action(struct yt921x_acl_rule_ext *ruleext,
 	int res;
 
 	memset(action, 0, 3 * sizeof(*action));
+	ruleext->r.sw_assisting = false;
+
 	flow_action_for_each(i, act, flow_action) {
 		/* The flow action entry carries no mirred control, so there
 		 * is no way to tell whether software would continue past a
@@ -1072,6 +1075,20 @@ yt921x_acl_rule_ext_parse_flow_action(struct yt921x_acl_rule_ext *ruleext,
 				goto fallback;
 			}
 
+			/* Port isolation takes after REDIR judgment, but
+			 * software delivers such frames.
+			 */
+			if (pp->isolation_mask & BIT(to_dp->index)) {
+				if (cls->common.skip_sw) {
+					if (to_dp->index == port)
+						reason = "Redirect to self, which is not hairpinning";
+					else
+						reason = "Redirect target isolated, or not in the same bridge";
+					goto fallback;
+				}
+				ruleext->r.sw_assisting = true;
+			}
+
 			action[2] |= YT921X_ACL_ACTc_FWD_EN |
 				     YT921X_ACL_ACTc_FWD_REDIR |
 				     YT921X_ACL_ACTc_FWD_REDIR_DPORTn(to_dp->index);
@@ -1301,8 +1318,19 @@ yt921x_acl_commit(struct yt921x_priv *priv, unsigned int entid, u8 entsmask)
 	aclrule = aclblk->rules[binid];
 
 	/* Write actions */
-	res = yt921x_reg96_write(priv, YT921X_ACLn_ACT(entid),
-				 aclrule ? aclrule->action : zeros);
+	if (!aclrule || !aclrule->sw_assisting) {
+		res = yt921x_reg96_write(priv, YT921X_ACLn_ACT(entid),
+					 aclrule ? aclrule->action : zeros);
+	} else {
+		u32 action[3];
+
+		memcpy(action, aclrule->action, sizeof(action));
+		action[2] &= ~YT921X_ACL_ACTc_FWD_REDIR_DPORTS_M &
+			     ~YT921X_ACL_ACTc_FWD_M;
+		action[2] |= YT921X_ACL_ACTc_FWD_EN | YT921X_ACL_ACTc_FWD_TRAP;
+
+		res = yt921x_reg96_write(priv, YT921X_ACLn_ACT(entid), action);
+	}
 	if (res)
 		return res;
 
@@ -1502,6 +1530,7 @@ yt921x_acl_add(struct yt921x_priv *priv,
 				      ~YT921X_ACL_ACTc_FWD_M;
 		aclrule->action[2] |= YT921X_ACL_ACTc_FWD_EN |
 				      YT921X_ACL_ACTc_FWD_TRAP;
+		aclrule->sw_assisting = false;
 	}
 	if (meterid < YT921X_METER_NUM)
 		aclrule->action[0] |= YT921X_ACL_ACTa_METER_ID(meterid);
diff --git a/drivers/net/dsa/motorcomm/tc.h b/drivers/net/dsa/motorcomm/tc.h
index cc53f148d690..81c29a8eec5a 100644
--- a/drivers/net/dsa/motorcomm/tc.h
+++ b/drivers/net/dsa/motorcomm/tc.h
@@ -349,7 +349,11 @@ struct yt921x_acl_rule {
 	enum tc_setup_type type;
 
 	u32 action[3];
-	bool sw_assisted;
+	bool sw_assisted:1;
+	/* not a TRAP rule, but installed as trapped as some conditions are not
+	 * met
+	 */
+	bool sw_assisting:1;
 
 	u8 mask;
 	struct yt921x_acl_entry entries[YT921X_ACL_ENT_PER_BLK];
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH net-next v3 4/5] net: dsa: motorcomm: Reject bridge flags that break redirect rules
  2026-12-01  8:14 [PATCH net-next v3 0/5] net: dsa: motorcomm: Reject isolated redirect targets David Yang
                   ` (2 preceding siblings ...)
  2026-12-01  8:14 ` [PATCH net-next v3 3/5] net: dsa: motorcomm: Reject isolated redirect targets David Yang
@ 2026-12-01  8:14 ` David Yang
  2026-12-01  8:14 ` [PATCH net-next v3 5/5] net: dsa: motorcomm: Recompute redirect rules on bridge changes David Yang
  4 siblings, 0 replies; 6+ messages in thread
From: David Yang @ 2026-12-01  8:14 UTC (permalink / raw)
  To: netdev
  Cc: David Yang, Andrew Lunn, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni

Bridge flag changes rewrite the isolation masks, so a reachable redirect
rule may be blocked by port isolation. The masks are rewritten only from
the deferred handler, so judge a rule and a flag change against the flags
as configured, not against what the switch was last programmed with.

Reject such changes based on bookkeeping counters:
.port_pre_bridge_flags() is atomic.

Fixes: 8eb9e7520b17 ("net: dsa: yt921x: Add ACL support")
Signed-off-by: David Yang <mmyangfl@gmail.com>
---
 drivers/net/dsa/motorcomm/chip.c | 31 +++++++++++++++++++++++++++
 drivers/net/dsa/motorcomm/chip.h |  1 +
 drivers/net/dsa/motorcomm/tc.c   | 36 +++++++++++++++++++++++++-------
 3 files changed, 60 insertions(+), 8 deletions(-)

diff --git a/drivers/net/dsa/motorcomm/chip.c b/drivers/net/dsa/motorcomm/chip.c
index 6c6a87207bd5..0bc5da2b80d7 100644
--- a/drivers/net/dsa/motorcomm/chip.c
+++ b/drivers/net/dsa/motorcomm/chip.c
@@ -1448,6 +1448,37 @@ yt921x_dsa_port_pre_bridge_flags(struct dsa_switch *ds, int port,
 			   BR_MCAST_FLOOD | BR_ISOLATED))
 		return -EINVAL;
 
+	if ((flags.mask & BR_HAIRPIN_MODE) && !(flags.val & BR_HAIRPIN_MODE) &&
+	    pp->acl_dst_cnts[port]) {
+		NL_SET_ERR_MSG_MOD(extack,
+				   "Cannot disable hairpin mode while there are hairpin redirect rules installed");
+		return -EINVAL;
+	}
+
+	if ((flags.mask & BR_ISOLATED) && (flags.val & BR_ISOLATED)) {
+		struct dsa_port *dp = dsa_to_port(ds, port);
+		unsigned long ports_mask;
+		struct net_device *bdev;
+		unsigned int i;
+
+		bdev = dsa_port_bridge_dev_get(dp);
+		if (!bdev)
+			return 0;
+
+		ports_mask = dsa_bridge_ports(ds, bdev);
+		for_each_set_bit(i, &ports_mask, YT921X_PORT_NUM) {
+			if (i == port || !priv->ports[i].isolated)
+				continue;
+
+			if (pp->acl_dst_cnts[i] ||
+			    priv->ports[i].acl_dst_cnts[port]) {
+				NL_SET_ERR_MSG_MOD(extack,
+						   "Cannot isolate port while there are redirect rules installed");
+				return -EINVAL;
+			}
+		}
+	}
+
 	if ((flags.mask & BR_HAIRPIN_MODE) || (flags.mask & BR_ISOLATED)) {
 		struct dsa_port *dp = dsa_to_port(ds, port);
 		struct net_device *bdev;
diff --git a/drivers/net/dsa/motorcomm/chip.h b/drivers/net/dsa/motorcomm/chip.h
index 203371888c45..b87329dac609 100644
--- a/drivers/net/dsa/motorcomm/chip.h
+++ b/drivers/net/dsa/motorcomm/chip.h
@@ -467,6 +467,7 @@ struct yt921x_port {
 	u16 isolation_mask;
 
 	unsigned short acl_cnt;
+	unsigned short acl_dst_cnts[YT921X_PORT_NUM];
 
 #if IS_ENABLED(CONFIG_NET_DSA_YT921X_LEDS)
 	unsigned char led_duty;
diff --git a/drivers/net/dsa/motorcomm/tc.c b/drivers/net/dsa/motorcomm/tc.c
index b7fe867da088..9a19ee7d4358 100644
--- a/drivers/net/dsa/motorcomm/tc.c
+++ b/drivers/net/dsa/motorcomm/tc.c
@@ -1403,7 +1403,8 @@ yt921x_acl_del(struct yt921x_priv *priv, enum tc_setup_type type,
 	unsigned int binid;
 	unsigned int blkid;
 	unsigned int entid;
-	unsigned long m;
+	unsigned long srcs;
+	unsigned long dsts;
 	unsigned int o;
 	u32 ctrl;
 	int res;
@@ -1422,9 +1423,17 @@ yt921x_acl_del(struct yt921x_priv *priv, enum tc_setup_type type,
 	ret = yt921x_acl_commit(priv, entid, aclrule->mask);
 	/* the kernel never rolls back on failure */
 
-	m = FIELD_GET(YT921X_ACL_KEYb_SPORTS_M, aclrule->entries[0].key[1]);
-	for_each_set_bit(o, &m, YT921X_PORT_NUM) {
+	srcs = FIELD_GET(YT921X_ACL_KEYb_SPORTS_M, aclrule->entries[0].key[1]);
+	dsts = FIELD_GET(YT921X_ACL_ACTc_FWD_REDIR_DPORTS_M,
+			 aclrule->action[2]);
+	for_each_set_bit(o, &srcs, YT921X_PORT_NUM) {
 		struct yt921x_port *pp = &priv->ports[o];
+		unsigned int i;
+
+		if (!aclrule->sw_assisted)
+			for_each_set_bit(i, &dsts, YT921X_PORT_NUM)
+				if (!WARN_ON(!pp->acl_dst_cnts[i]))
+					pp->acl_dst_cnts[i]--;
 
 		if (!WARN_ON(!pp->acl_cnt)) {
 			pp->acl_cnt--;
@@ -1466,6 +1475,8 @@ yt921x_acl_add(struct yt921x_priv *priv,
 	unsigned int binid;
 	unsigned int blkid;
 	unsigned int entid;
+	unsigned long srcs;
+	unsigned long dsts;
 	unsigned long m;
 	unsigned int o;
 	u32 ctrl;
@@ -1539,8 +1550,8 @@ yt921x_acl_add(struct yt921x_priv *priv,
 
 	/* Activate ACL if needed */
 	ctrl = 0;
-	m = FIELD_GET(YT921X_ACL_KEYb_SPORTS_M, ruleext->r.entries[0].key[1]);
-	for_each_set_bit(o, &m, YT921X_PORT_NUM)
+	srcs = FIELD_GET(YT921X_ACL_KEYb_SPORTS_M, aclrule->entries[0].key[1]);
+	for_each_set_bit(o, &srcs, YT921X_PORT_NUM)
 		if (!priv->ports[o].acl_cnt)
 			ctrl |= YT921X_ACL_PORT_PORTn(o);
 	if (ctrl) {
@@ -1559,9 +1570,18 @@ yt921x_acl_add(struct yt921x_priv *priv,
 	}
 
 	/* Bookkeeping */
-	m = FIELD_GET(YT921X_ACL_KEYb_SPORTS_M, ruleext->r.entries[0].key[1]);
-	for_each_set_bit(o, &m, YT921X_PORT_NUM)
-		priv->ports[o].acl_cnt++;
+	dsts = FIELD_GET(YT921X_ACL_ACTc_FWD_REDIR_DPORTS_M,
+			 aclrule->action[2]);
+	for_each_set_bit(o, &srcs, YT921X_PORT_NUM) {
+		struct yt921x_port *pp = &priv->ports[o];
+		unsigned int i;
+
+		pp->acl_cnt++;
+
+		if (!aclrule->sw_assisted)
+			for_each_set_bit(i, &dsts, YT921X_PORT_NUM)
+				pp->acl_dst_cnts[i]++;
+	}
 
 	if (meterid < YT921X_METER_NUM)
 		set_bit(meterid, priv->meters_map);
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH net-next v3 5/5] net: dsa: motorcomm: Recompute redirect rules on bridge changes
  2026-12-01  8:14 [PATCH net-next v3 0/5] net: dsa: motorcomm: Reject isolated redirect targets David Yang
                   ` (3 preceding siblings ...)
  2026-12-01  8:14 ` [PATCH net-next v3 4/5] net: dsa: motorcomm: Reject bridge flags that break redirect rules David Yang
@ 2026-12-01  8:14 ` David Yang
  4 siblings, 0 replies; 6+ messages in thread
From: David Yang @ 2026-12-01  8:14 UTC (permalink / raw)
  To: netdev
  Cc: David Yang, Andrew Lunn, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni

Bridge join, leave and flag changes rewrite the isolation masks, so a
redirect rule that was trapped because of isolation may become
reachable again, and a reachable one may become trapped.

Reload such non-skip_sw redirect rules accordingly. skip_sw rules are
not touched at all: they cannot be degraded, port_bridge_leave() cannot
be rejected (return value is void), nor can tc be told which rule got
invalidated.

Fixes: 8eb9e7520b17 ("net: dsa: yt921x: Add ACL support")
Signed-off-by: David Yang <mmyangfl@gmail.com>
---
 drivers/net/dsa/motorcomm/chip.c | 33 +++++++++++++
 drivers/net/dsa/motorcomm/tc.c   | 79 ++++++++++++++++++++++++++++++++
 drivers/net/dsa/motorcomm/tc.h   |  2 +
 3 files changed, 114 insertions(+)

diff --git a/drivers/net/dsa/motorcomm/chip.c b/drivers/net/dsa/motorcomm/chip.c
index 0bc5da2b80d7..a5e673a69355 100644
--- a/drivers/net/dsa/motorcomm/chip.c
+++ b/drivers/net/dsa/motorcomm/chip.c
@@ -1398,6 +1398,7 @@ static int
 yt921x_bridge_flags(struct yt921x_priv *priv, int port,
 		    struct switchdev_brport_flags flags)
 {
+	struct device *dev = yt921x_priv_to_device(priv);
 	u32 mask;
 	int res;
 
@@ -1431,6 +1432,14 @@ yt921x_bridge_flags(struct yt921x_priv *priv, int port,
 			if (res)
 				return res;
 		}
+
+		res = yt921x_acl_recompute(priv, port);
+		if (res) {
+			dev_err(dev,
+				"Failed to refresh ACL rules for port %d: %i\n",
+				port, res);
+			res = 0;
+		}
 	}
 
 	return 0;
@@ -1531,7 +1540,19 @@ yt921x_dsa_port_bridge_leave(struct dsa_switch *ds, int port,
 		return;
 
 	mutex_lock(&priv->reg_lock);
+
 	res = yt921x_bridge_leave(priv, port);
+	if (res)
+		goto end;
+
+	res = yt921x_acl_recompute(priv, port);
+	if (res) {
+		dev_err(dev, "Failed to refresh ACL rules for port %d: %i\n",
+			port, res);
+		res = 0;
+	}
+
+end:
 	mutex_unlock(&priv->reg_lock);
 
 	if (res)
@@ -1545,6 +1566,7 @@ yt921x_dsa_port_bridge_join(struct dsa_switch *ds, int port,
 			    struct netlink_ext_ack *extack)
 {
 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
+	struct device *dev = yt921x_priv_to_device(priv);
 	struct yt921x_port *pp = &priv->ports[port];
 	struct net_device *brport_dev;
 	struct dsa_port *dp;
@@ -1564,6 +1586,17 @@ yt921x_dsa_port_bridge_join(struct dsa_switch *ds, int port,
 	pp->isolated = br_port_flag_is_set(brport_dev, BR_ISOLATED);
 	pp->hairpin = br_port_flag_is_set(brport_dev, BR_HAIRPIN_MODE);
 	res = yt921x_bridge_join(priv, port, ports_mask);
+	if (res)
+		goto end;
+
+	res = yt921x_acl_recompute(priv, port);
+	if (res) {
+		dev_err(dev, "Failed to refresh ACL rules for port %d: %i\n",
+			port, res);
+		res = 0;
+	}
+
+end:
 	mutex_unlock(&priv->reg_lock);
 
 	return res;
diff --git a/drivers/net/dsa/motorcomm/tc.c b/drivers/net/dsa/motorcomm/tc.c
index 9a19ee7d4358..210f7af3a206 100644
--- a/drivers/net/dsa/motorcomm/tc.c
+++ b/drivers/net/dsa/motorcomm/tc.c
@@ -1393,6 +1393,85 @@ yt921x_acl_commit(struct yt921x_priv *priv, unsigned int entid, u8 entsmask)
 	return 0;
 }
 
+static int
+yt921x_acl_recompute_rule(struct yt921x_priv *priv, int port,
+			  unsigned int entid)
+{
+	const struct yt921x_acl_blk *aclblk;
+	struct yt921x_acl_rule *aclrule;
+	unsigned int blkid;
+	unsigned int binid;
+	unsigned long dsts;
+	unsigned long srcs;
+	unsigned int o;
+	bool assisting;
+	int res;
+
+	blkid = entid / YT921X_ACL_ENT_PER_BLK;
+	binid = entid % YT921X_ACL_ENT_PER_BLK;
+	aclblk = priv->acl_blks[blkid];
+	aclrule = aclblk->rules[binid];
+
+	if (!(aclrule->action[2] & YT921X_ACL_ACTc_FWD_EN) ||
+	    (aclrule->action[2] & YT921X_ACL_ACTc_FWD_M) !=
+	    YT921X_ACL_ACTc_FWD_REDIR)
+		return 0;
+
+	srcs = FIELD_GET(YT921X_ACL_KEYb_SPORTS_M, aclrule->entries[0].key[1]);
+	dsts = FIELD_GET(YT921X_ACL_ACTc_FWD_REDIR_DPORTS_M,
+			 aclrule->action[2]);
+	if (!((srcs | dsts) & BIT(port)))
+		return 0;
+
+	assisting = false;
+	for_each_set_bit(o, &srcs, YT921X_PORT_NUM)
+		if (dsts & priv->ports[o].isolation_mask) {
+			assisting = true;
+			break;
+		}
+
+	if (aclrule->sw_assisting != assisting) {
+		aclrule->sw_assisting = assisting;
+		res = yt921x_acl_commit(priv, entid, aclrule->mask);
+		if (res)
+			return res;
+	}
+
+	return 0;
+}
+
+/* Only non-skip_sw rules are refreshes; skip_sw rules cannot be degraded,
+ * port_bridge_leave() cannot be rejected (return value is void), nor can tc be
+ * told which rule got invalidated.
+ */
+int yt921x_acl_recompute(struct yt921x_priv *priv, int port)
+{
+	int res;
+
+	for (unsigned int blkid = 0; blkid < YT921X_ACL_BLK_NUM; blkid++) {
+		const struct yt921x_acl_blk *aclblk = priv->acl_blks[blkid];
+
+		if (!aclblk)
+			continue;
+
+		for (unsigned int i = 0; i < YT921X_ACL_ENT_PER_BLK; i++) {
+			struct yt921x_acl_rule *aclrule;
+			unsigned int entid;
+
+			aclrule = aclblk->rules[i];
+			if (!aclrule || !aclrule->sw_assisted)
+				continue;
+
+			entid = YT921X_ACL_ENT_PER_BLK * blkid + i;
+			res = yt921x_acl_recompute_rule(priv, port, entid);
+			if (res)
+				return res;
+		}
+	}
+
+	return 0;
+}
+
 static int
 yt921x_acl_del(struct yt921x_priv *priv, enum tc_setup_type type,
 	       unsigned long tag, u16 ports_mask)
diff --git a/drivers/net/dsa/motorcomm/tc.h b/drivers/net/dsa/motorcomm/tc.h
index 81c29a8eec5a..be4c258873b8 100644
--- a/drivers/net/dsa/motorcomm/tc.h
+++ b/drivers/net/dsa/motorcomm/tc.h
@@ -371,6 +371,8 @@ yt921x_dsa_port_policer_add(struct dsa_switch *ds, int port,
 int
 yt921x_dsa_port_setup_tc(struct dsa_switch *ds, int port,
 			 enum tc_setup_type type, void *type_data);
+
+int yt921x_acl_recompute(struct yt921x_priv *priv, int port);
 int
 yt921x_dsa_cls_flower_del(struct dsa_switch *ds, int port,
 			  struct flow_cls_offload *cls, bool ingress);
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-01  8:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  8:14 [PATCH net-next v3 0/5] net: dsa: motorcomm: Reject isolated redirect targets David Yang
2026-12-01  8:14 ` [PATCH net-next v3 1/5] net: dsa: motorcomm: Remove local cpu_ports_mask David Yang
2026-12-01  8:14 ` [PATCH net-next v3 2/5] net: dsa: motorcomm: Cache port isolation masks David Yang
2026-12-01  8:14 ` [PATCH net-next v3 3/5] net: dsa: motorcomm: Reject isolated redirect targets David Yang
2026-12-01  8:14 ` [PATCH net-next v3 4/5] net: dsa: motorcomm: Reject bridge flags that break redirect rules David Yang
2026-12-01  8:14 ` [PATCH net-next v3 5/5] net: dsa: motorcomm: Recompute redirect rules on bridge changes David Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox