On a router whose LAN is a VLAN-filtering bridge over switch ports, with the ports untagged members of the bridge VLAN and the address on a VLAN device of the bridge, the software flowtable does not work for the LAN: on switches that deliver such frames untagged, packets from the LAN never match the flow, and packets towards the LAN leave the port tagged. When the switch offloads the bridge VLAN, the forward path reports such a port as DEV_PATH_BR_VLAN_UNTAG_HW. The flow keeps the VLAN in the tuple that enters on the port, for a switch that adds the tag towards the CPU, and the transmit path pushes it again. Whether the tag reaches the stack depends on the switch: mt7530 and b53 have the DSA core remove it, gswip, bcm_sf2, ksz9477 and sja1105 keep it. Handle DEV_PATH_BR_VLAN_UNTAG_HW like DEV_PATH_BR_VLAN_UNTAG, and leave the PVID of a VLAN-filtering bridge port out of the lookup when the port sends it untagged and the frame carries it: the bridge puts an untagged frame and one tagged with the PVID into the same VLAN. Offload only a VLAN that is the untagged PVID of the port, since an untagged frame from the port belongs to the PVID and must not match the flow of another VLAN. The hardware rule does not change, since nf_flow_rule_match() and nf_flow_rule_route_common() already left this VLAN out. Leaving the VLAN out only for DSA ports does not work: the DSA core removes the tag only for drivers that set untag_vlan_aware_bridge_pvid, and the other drivers that tag the PVID towards the CPU would stop matching. The bridge is asked only for a frame that carries a tag and arrives on a bridge port. The flowtable now asks the bridge for the port's VLAN, so it depends on BRIDGE || BRIDGE=n, as DSA does, and loading it loads the bridge module when that is built with VLAN filtering. Fixes: 26267bf9bb57 ("netfilter: flowtable: bridge vlan hardware offload and switchdev") Fixes: c653d5a78f34 ("netfilter: flowtable: inline vlan encapsulation in xmit path") Link: https://github.com/openwrt/openwrt/issues/27001 Signed-off-by: Julius Bairaktaris --- net/netfilter/Kconfig | 1 + net/netfilter/nf_flow_table_ip.c | 41 +++++++++++++++++++++++++++--- net/netfilter/nf_flow_table_path.c | 29 ++++++++++++++++----- 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/net/netfilter/Kconfig b/net/netfilter/Kconfig index 09874c26fd13..8d72d0c2014f 100644 --- a/net/netfilter/Kconfig +++ b/net/netfilter/Kconfig @@ -710,6 +710,7 @@ config NF_FLOW_TABLE_INET config NF_FLOW_TABLE tristate "Netfilter flow table module" + depends on BRIDGE || BRIDGE=n depends on NETFILTER_INGRESS depends on NF_CONNTRACK depends on NF_TABLES diff --git a/net/netfilter/nf_flow_table_ip.c b/net/netfilter/nf_flow_table_ip.c index 19cafae748ab..17d47c9c4e02 100644 --- a/net/netfilter/nf_flow_table_ip.c +++ b/net/netfilter/nf_flow_table_ip.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -156,8 +157,35 @@ struct nf_flowtable_ctx { /* IP tunnel protocol */ u8 inner_proto; } tun; + bool br_pvid; }; +/* A vlan-filtering bridge port sends its untagged pvid without a tag, so the + * flow does not carry that vlan. A switch may still add it to the frames it + * hands to the cpu; leave it out of the lookup as well. + */ +static bool nf_flow_br_pvid_untagged(const struct net_device *dev, + const struct sk_buff *skb) +{ + struct bridge_vlan_info vinfo; + struct net_device *br; + u16 proto; + + if (!netif_is_bridge_port(dev)) + return false; + + br = netdev_master_upper_dev_get_rcu((struct net_device *)dev); + if (!br || !br_vlan_enabled(br) || br_vlan_get_proto(br, &proto) || + skb->vlan_proto != htons(proto)) + return false; + + if (br_vlan_get_info_rcu(dev, skb_vlan_tag_get_id(skb), &vinfo)) + return false; + + return (vinfo.flags & BRIDGE_VLAN_INFO_PVID) && + (vinfo.flags & BRIDGE_VLAN_INFO_UNTAGGED); +} + static void nf_flow_tuple_encap(struct nf_flowtable_ctx *ctx, struct sk_buff *skb, struct flow_offload_tuple *tuple) @@ -170,9 +198,13 @@ static void nf_flow_tuple_encap(struct nf_flowtable_ctx *ctx, int i = 0; if (skb_vlan_tag_present(skb)) { - tuple->encap[i].id = skb_vlan_tag_get_id(skb); - tuple->encap[i].proto = skb->vlan_proto; - i++; + if (nf_flow_br_pvid_untagged(ctx->in, skb)) { + ctx->br_pvid = true; + } else { + tuple->encap[i].id = skb_vlan_tag_get_id(skb); + tuple->encap[i].proto = skb->vlan_proto; + i++; + } } switch (skb->protocol) { case htons(ETH_P_8021Q): @@ -435,6 +467,9 @@ static void nf_flow_encap_pop(struct nf_flowtable_ctx *ctx, struct vlan_hdr *vlan_hdr; int i; + if (ctx->br_pvid) + __vlan_hwaccel_clear_tag(skb); + for (i = 0; i < tuplehash->tuple.encap_num; i++) { if (skb_vlan_tag_present(skb)) { __vlan_hwaccel_clear_tag(skb); diff --git a/net/netfilter/nf_flow_table_path.c b/net/netfilter/nf_flow_table_path.c index 841f099434d9..cf33fa2a6012 100644 --- a/net/netfilter/nf_flow_table_path.c +++ b/net/netfilter/nf_flow_table_path.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -102,6 +103,24 @@ struct nft_forward_info { static bool nft_flowtable_find_dev(const struct net_device *dev, struct nft_flowtable *ft); +/* A flow leaves out the vlan that its bridge port sends untagged. Only the + * untagged pvid of the port comes back without that vlan, see + * nf_flow_tuple_encap(), so offload no other untagged vlan. + */ +static bool nft_dev_br_pvid_untagged(const struct net_device_path_stack *stack, + int i) +{ + struct bridge_vlan_info vinfo; + + if (i + 1 >= stack->num_paths || + br_vlan_get_info_rcu(stack->path[i + 1].dev, + stack->path[i].bridge.vlan_id, &vinfo)) + return false; + + return (vinfo.flags & BRIDGE_VLAN_INFO_PVID) && + (vinfo.flags & BRIDGE_VLAN_INFO_UNTAGGED); +} + static int nft_dev_path_info(struct net_device_path_stack *stack, struct nft_forward_info *info, unsigned char *ha, struct nft_flowtable *ft) @@ -161,12 +180,6 @@ static int nft_dev_path_info(struct net_device_path_stack *stack, memcpy(info->h_source, path->dev->dev_addr, ETH_ALEN); switch (path->bridge.vlan_mode) { - case DEV_PATH_BR_VLAN_UNTAG_HW: - if (info->num_encaps == 0) - goto err_out; - - info->ingress_vlans |= BIT(info->num_encaps - 1); - break; case DEV_PATH_BR_VLAN_TAG: if (info->num_encaps >= NF_FLOW_TABLE_ENCAP_MAX) goto err_out; @@ -175,8 +188,10 @@ static int nft_dev_path_info(struct net_device_path_stack *stack, info->encap[info->num_encaps].proto = path->bridge.vlan_proto; info->num_encaps++; break; + case DEV_PATH_BR_VLAN_UNTAG_HW: case DEV_PATH_BR_VLAN_UNTAG: - if (info->num_encaps == 0) + if (info->num_encaps == 0 || + !nft_dev_br_pvid_untagged(stack, i)) goto err_out; info->num_encaps--; -- 2.53.0