net: dsa: sja1105: make sure untagged packets are dropped on ingress ports with no...
authorVladimir Oltean <vladimir.oltean@nxp.com>
Wed, 28 Jul 2021 21:54:28 +0000 (00:54 +0300)
committerDavid S. Miller <davem@davemloft.net>
Thu, 29 Jul 2021 14:35:01 +0000 (15:35 +0100)
Surprisingly, this configuration:

ip link add br0 type bridge vlan_filtering 1
ip link set swp2 master br0
bridge vlan del dev swp2 vid 1

still has the sja1105 switch sending untagged packets to the CPU (and
failing to decode them, since dsa_find_designated_bridge_port_by_vid
searches by VID 1 and rightfully finds no bridge VLAN 1 on a port).

Dumping the switch configuration, the VLANs are managed properly:
- the pvid of swp2 is 1 in the MAC Configuration Table, but
- only the CPU port is in the port membership of VLANID 1 in the VLAN
  Lookup Table

When the ingress packets are tagged with VID 1, they are properly
dropped. But when they are untagged, they are able to reach the CPU
port. Also, when the pvid in the MAC Configuration Table is changed to
e.g. 55 (an unused VLAN), the untagged packets are also dropped.

So it looks like:
- the switch bypasses ingress VLAN membership checks for untagged traffic
- the reason why the untagged traffic is dropped when I make the pvid 55
  is due to the lack of valid destination ports in VLAN 55, rather than
  an ingress membership violation
- the ingress VLAN membership cheks are only done for VLAN-tagged traffic

Interesting. It looks like there is an explicit bit to drop untagged
traffic, so we should probably be using that to preserve user expectations.

Note that only VLAN-aware ports should drop untagged packets due to no
pvid - when VLAN-unaware, the software bridge doesn't do this even if
there is no pvid on any bridge port and on the bridge itself. So the new
sja1105_drop_untagged() function cannot simply be called with "false"
from sja1105_bridge_vlan_add() and with "true" from sja1105_bridge_vlan_del.
Instead, we need to also consider the VLAN awareness state. That means
we need to hook the "drop untagged" setting in all the same places where
the "commit pvid" logic is, and it needs to factor in all the state when
flipping the "drop untagged" bit: is our current pvid in the VLAN Lookup
Table, and is the current port in that VLAN's port membership list?
VLAN-unaware ports will never drop untagged frames because these checks
always succeed by construction, and the tag_8021q VLANs cannot be changed
by the user.

Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/dsa/sja1105/sja1105_main.c

index 293c776..5ab1676 100644 (file)
@@ -57,6 +57,38 @@ static bool sja1105_can_forward(struct sja1105_l2_forwarding_entry *l2_fwd,
        return !!(l2_fwd[from].reach_port & BIT(to));
 }
 
+static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
+{
+       struct sja1105_vlan_lookup_entry *vlan;
+       int count, i;
+
+       vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
+       count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
+
+       for (i = 0; i < count; i++)
+               if (vlan[i].vlanid == vid)
+                       return i;
+
+       /* Return an invalid entry index if not found */
+       return -1;
+}
+
+static int sja1105_drop_untagged(struct dsa_switch *ds, int port, bool drop)
+{
+       struct sja1105_private *priv = ds->priv;
+       struct sja1105_mac_config_entry *mac;
+
+       mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
+
+       if (mac[port].drpuntag == drop)
+               return 0;
+
+       mac[port].drpuntag = drop;
+
+       return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
+                                           &mac[port], true);
+}
+
 static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
 {
        struct sja1105_mac_config_entry *mac;
@@ -76,6 +108,9 @@ static int sja1105_commit_pvid(struct dsa_switch *ds, int port)
 {
        struct dsa_port *dp = dsa_to_port(ds, port);
        struct sja1105_private *priv = ds->priv;
+       struct sja1105_vlan_lookup_entry *vlan;
+       bool drop_untagged = false;
+       int match, rc;
        u16 pvid;
 
        if (dp->bridge_dev && br_vlan_enabled(dp->bridge_dev))
@@ -83,7 +118,18 @@ static int sja1105_commit_pvid(struct dsa_switch *ds, int port)
        else
                pvid = priv->tag_8021q_pvid[port];
 
-       return sja1105_pvid_apply(priv, port, pvid);
+       rc = sja1105_pvid_apply(priv, port, pvid);
+       if (rc)
+               return rc;
+
+       vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
+
+       match = sja1105_is_vlan_configured(priv, pvid);
+
+       if (match < 0 || !(vlan[match].vmemb_port & BIT(port)))
+               drop_untagged = true;
+
+       return sja1105_drop_untagged(ds, port, drop_untagged);
 }
 
 static int sja1105_init_mac_settings(struct sja1105_private *priv)
@@ -1997,22 +2043,6 @@ sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
        return priv->info->tag_proto;
 }
 
-static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
-{
-       struct sja1105_vlan_lookup_entry *vlan;
-       int count, i;
-
-       vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
-       count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
-
-       for (i = 0; i < count; i++)
-               if (vlan[i].vlanid == vid)
-                       return i;
-
-       /* Return an invalid entry index if not found */
-       return -1;
-}
-
 /* The TPID setting belongs to the General Parameters table,
  * which can only be partially reconfigured at runtime (and not the TPID).
  * So a switch reset is required.
@@ -2219,8 +2249,16 @@ static int sja1105_bridge_vlan_del(struct dsa_switch *ds, int port,
                                   const struct switchdev_obj_port_vlan *vlan)
 {
        struct sja1105_private *priv = ds->priv;
+       int rc;
 
-       return sja1105_vlan_del(priv, port, vlan->vid);
+       rc = sja1105_vlan_del(priv, port, vlan->vid);
+       if (rc)
+               return rc;
+
+       /* In case the pvid was deleted, make sure that untagged packets will
+        * be dropped.
+        */
+       return sja1105_commit_pvid(ds, port);
 }
 
 static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,