From: Pablo Neira Ayuso A direct xmit flow that reaches a bridge port keeps sending to that port when the host moves to another port of the bridge, and packets from the other side keep the flow from timing out. The gc now checks the fdb entry after nf_flow_dst_check_gc(), through nf_br_utils_hook, and tears the flow down when the entry points to another port. An entry that aged out does not tear it down, because offloaded packets do not refresh it. One possibility to address this issue is to subscribe to SWITCHDEV_FDB_DEL_TO_DEVICE event notifications, however, this requires iterating over the whole flowtable whenever a fdb entry is removed. The bridge port is found from out.ifidx through the vlan devices that the forward path has below the port. The flow keeps the vid the forward path looked the fdb entry up in: a vlan that leaves the port untagged is in neither encap stack. struct flow_offload grows from 296 to 312 bytes on x86_64; its slab cache is cacheline aligned, so an object takes 320 bytes either way. The flow is also torn down when the port no longer forwards in that vid, where the bridge would no longer deliver either: the port left the forwarding state, the vlan left the port, or vlan filtering was turned on or off. With two bridges on the path, at most two fit in a forward path, the gc goes up from the inner bridge through the vlan devices between them to the port of the outer one and checks its entry as well. Neigh xmit flows need no fdb check: a forward path that crosses a bridge always makes the flow direct xmit, and a failed walk is not offloaded. Reported-by: Eric Woudstra Link: https://lore.kernel.org/netfilter-devel/20250925182623.114045-1-ericwouds@gmail.com/ Signed-off-by: Pablo Neira Ayuso [julius@bairaktaris.de: find the bridge port below the vlan devices of the path, keep the vid of the fdb lookup, keep a flow whose entry aged out, check that the port forwards the vid, check both bridges of a stacked path, follow struct net_bridge_dst, build br_nf_utils.o only with the flowtable] Link: https://lore.kernel.org/netfilter-devel/20261014210256.1573072-1-pablo@netfilter.org/ Signed-off-by: Julius Bairaktaris --- v11: rebased onto Pablo's xmit series: the gc checks the fdb entry after his nf_flow_dst_check_gc(), which replaces nf_flow_dst_check() there, and his patch now sets out.h_dest for every xmit type v10: https://lore.kernel.org/netfilter-devel/20261115005555.645727-3-julius@bairaktaris.de/ include/linux/netfilter.h | 7 +++ include/net/netfilter/nf_flow_table.h | 22 ++++++++- net/bridge/Makefile | 2 + net/bridge/br.c | 2 + net/bridge/br_nf_utils.c | 35 ++++++++++++++ net/bridge/br_private.h | 13 +++++ net/netfilter/core.c | 3 ++ net/netfilter/nf_flow_table_core.c | 68 ++++++++++++++++++++++++++- net/netfilter/nf_flow_table_path.c | 29 +++++++++++- 9 files changed, 176 insertions(+), 5 deletions(-) create mode 100644 net/bridge/br_nf_utils.c diff --git a/include/linux/netfilter.h b/include/linux/netfilter.h index e99afc1414cd..be85d8f68b45 100644 --- a/include/linux/netfilter.h +++ b/include/linux/netfilter.h @@ -67,6 +67,13 @@ static inline void nf_inet_addr_mask(const union nf_inet_addr *a1, #endif } +struct nf_br_utils { + bool (*fdb_valid_dst)(const struct net_device *dev, + const unsigned char *addr, u16 vid); +}; + +extern const struct nf_br_utils __rcu *nf_br_utils_hook; + int netfilter_init(void); struct sk_buff; diff --git a/include/net/netfilter/nf_flow_table.h b/include/net/netfilter/nf_flow_table.h index 143bcfce3678..206296483fe1 100644 --- a/include/net/netfilter/nf_flow_table.h +++ b/include/net/netfilter/nf_flow_table.h @@ -121,6 +121,18 @@ struct flow_offload_tunnel { u8 encap_proto; }; +/* The outer of two bridges on the path: its vid, and how to reach its port + * from the inner bridge. + */ +struct flow_offload_br_outer { + u16 vid; + u8 present:1, + link_vlans:2, + link_untag:1, + link_8021ad:1, + inner_tag:1; +}; + struct flow_offload_tuple { union { struct in_addr src_v4; @@ -153,6 +165,8 @@ struct flow_offload_tuple { xmit_type:3, encap_num:2, needs_gso_segment:1, + bridge_path:1, + port_vlans:2, tun_num:2, in_vlan_ingress:2; u16 mtu; @@ -164,6 +178,8 @@ struct flow_offload_tuple { u32 ifidx; u8 h_source[ETH_ALEN]; u8 h_dest[ETH_ALEN]; + u16 bridge_vid; + struct flow_offload_br_outer br_outer; } out; struct { u32 iifidx; @@ -232,7 +248,11 @@ struct nf_flow_route { u32 hw_ifindex; u8 h_source[ETH_ALEN]; u8 h_dest[ETH_ALEN]; - u8 needs_gso_segment:1; + u16 bridge_vid; + struct flow_offload_br_outer br_outer; + u8 needs_gso_segment:1, + bridge_path:1, + port_vlans:2; } out; enum flow_offload_xmit_type xmit_type; } tuple[FLOW_OFFLOAD_DIR_MAX]; diff --git a/net/bridge/Makefile b/net/bridge/Makefile index 1203dc19e15c..fc53c332246d 100644 --- a/net/bridge/Makefile +++ b/net/bridge/Makefile @@ -14,6 +14,8 @@ bridge-$(CONFIG_SYSFS) += br_sysfs_if.o br_sysfs_br.o bridge-$(subst m,y,$(CONFIG_BRIDGE_NETFILTER)) += br_nf_core.o +bridge-$(subst m,y,$(CONFIG_NF_FLOW_TABLE)) += br_nf_utils.o + 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 diff --git a/net/bridge/br.c b/net/bridge/br.c index d9a39ff98868..c0618ef6dac5 100644 --- a/net/bridge/br.c +++ b/net/bridge/br.c @@ -458,6 +458,7 @@ static int __init br_init(void) goto err_out6; brioctl_set(br_ioctl_stub); + br_nf_utils_init(); #if IS_MODULE(CONFIG_BRIDGE_NETFILTER) pr_info("bridge: filtering via arp/ip/ip6tables is no longer available " @@ -486,6 +487,7 @@ static int __init br_init(void) static void __exit br_deinit(void) { + br_nf_utils_fini(); stp_proto_unregister(&br_stp_proto); br_netlink_fini(); unregister_switchdev_blocking_notifier(&br_switchdev_blocking_notifier); diff --git a/net/bridge/br_nf_utils.c b/net/bridge/br_nf_utils.c new file mode 100644 index 000000000000..c9a7c94d301c --- /dev/null +++ b/net/bridge/br_nf_utils.c @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include + +#include "br_private.h" + +static bool nf_br_fdb_valid_dst(const struct net_device *dev, + const unsigned char *addr, u16 vid) +{ + const struct net_bridge_fdb_entry *f; + const struct net_bridge_port *p; + + p = br_port_get_check_rcu(dev); + if (!p || !br_port_forwards(p, vid)) + return false; + + /* Offloaded packets do not refresh the entry, it can age out. */ + f = br_fdb_find_rcu(p->br, addr, vid); + + return !f || br_fdb_dst_port(f) == p; +} + +static const struct nf_br_utils nf_br_utils = { + .fdb_valid_dst = nf_br_fdb_valid_dst, +}; + +void br_nf_utils_init(void) +{ + rcu_assign_pointer(nf_br_utils_hook, &nf_br_utils); +} + +void br_nf_utils_fini(void) +{ + RCU_INIT_POINTER(nf_br_utils_hook, NULL); +} diff --git a/net/bridge/br_private.h b/net/bridge/br_private.h index 2c73d2549eae..6a6685e93b49 100644 --- a/net/bridge/br_private.h +++ b/net/bridge/br_private.h @@ -2166,6 +2166,19 @@ struct nf_br_ops { }; extern const struct nf_br_ops __rcu *nf_br_ops; +#if IS_ENABLED(CONFIG_NF_FLOW_TABLE) +void br_nf_utils_init(void); +void br_nf_utils_fini(void); +#else +static inline void br_nf_utils_init(void) +{ +} + +static inline void br_nf_utils_fini(void) +{ +} +#endif + /* br_netfilter.c */ #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) int br_nf_core_init(void); diff --git a/net/netfilter/core.c b/net/netfilter/core.c index 44c856c1e621..92e9852c80c0 100644 --- a/net/netfilter/core.c +++ b/net/netfilter/core.c @@ -814,6 +814,9 @@ static struct pernet_operations netfilter_net_ops = { .exit = netfilter_net_exit, }; +const struct nf_br_utils __rcu *nf_br_utils_hook; +EXPORT_SYMBOL_GPL(nf_br_utils_hook); + int __init netfilter_init(void) { int ret; diff --git a/net/netfilter/nf_flow_table_core.c b/net/netfilter/nf_flow_table_core.c index 283a6fc215e5..64d0b3a916dd 100644 --- a/net/netfilter/nf_flow_table_core.c +++ b/net/netfilter/nf_flow_table_core.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -140,6 +141,10 @@ static int flow_offload_fill_route(struct flow_offload *flow, memcpy(flow_tuple->out.h_source, route->tuple[dir].out.h_source, ETH_ALEN); flow_tuple->out.ifidx = route->tuple[dir].out.ifindex; + flow_tuple->bridge_path = route->tuple[dir].out.bridge_path; + flow_tuple->out.bridge_vid = route->tuple[dir].out.bridge_vid; + flow_tuple->out.br_outer = route->tuple[dir].out.br_outer; + flow_tuple->port_vlans = route->tuple[dir].out.port_vlans; break; case FLOW_OFFLOAD_XMIT_NEIGH: memcpy(flow_tuple->out.h_dest, route->tuple[dir].out.h_dest, @@ -636,6 +641,65 @@ static inline bool nf_flow_dst_check_gc(struct flow_offload *flow, int dir) return ret; } +static bool nf_flow_dst_valid(struct nf_flowtable *flow_table, + struct flow_offload *flow, int dir) +{ + struct flow_offload_tuple *other = &flow->tuplehash[!dir].tuple; + struct flow_offload_tuple *tuple = &flow->tuplehash[dir].tuple; + struct net *net = read_pnet(&flow_table->net); + const struct flow_offload_br_outer *outer = &tuple->out.br_outer; + const struct nf_br_utils *br_utils; + struct net_device *dev; + bool valid = true; + __be16 proto; + int i, n; + + if (!nf_flow_dst_check_gc(flow, dir)) + return false; + + if (!tuple->bridge_path) + return true; + + rcu_read_lock(); + br_utils = rcu_dereference(nf_br_utils_hook); + if (!br_utils) + goto out; + + dev = dev_get_by_index_rcu(net, tuple->out.ifidx); + for (i = 0; dev && i < tuple->port_vlans; i++) + dev = __vlan_find_dev_deep_rcu(dev, other->encap[i].proto, + other->encap[i].id); + + valid = dev && br_utils->fdb_valid_dst(dev, tuple->out.h_dest, + tuple->out.bridge_vid); + if (!valid || !outer->present) + goto out; + + /* Up from the inner bridge through the vlan devices to the port of + * the outer one. A link vlan the inner bridge untags is not in the + * encap stack, its vid is the inner bridge vid. + */ + dev = netdev_master_upper_dev_get_rcu(dev); + if (dev && outer->link_untag) { + proto = outer->link_8021ad ? htons(ETH_P_8021AD) : + htons(ETH_P_8021Q); + dev = __vlan_find_dev_deep_rcu(dev, proto, + tuple->out.bridge_vid); + } + i += outer->inner_tag; + n = i + outer->link_vlans - outer->link_untag; + for (; dev && i < n; i++) + dev = __vlan_find_dev_deep_rcu(dev, other->encap[i].proto, + other->encap[i].id); + + valid = dev && br_utils->fdb_valid_dst(dev, tuple->out.h_dest, + outer->vid); +out: + rcu_read_unlock(); + + return valid; +} + static void nf_flow_offload_gc_step(struct nf_flowtable *flow_table, struct flow_offload *flow, void *data) { @@ -648,8 +712,8 @@ static void nf_flow_offload_gc_step(struct nf_flowtable *flow_table, if (nf_flow_has_expired(flow) || nf_ct_is_dying(flow->ct) || - !nf_flow_dst_check_gc(flow, FLOW_OFFLOAD_DIR_ORIGINAL) || - !nf_flow_dst_check_gc(flow, FLOW_OFFLOAD_DIR_REPLY) || + !nf_flow_dst_valid(flow_table, flow, FLOW_OFFLOAD_DIR_ORIGINAL) || + !nf_flow_dst_valid(flow_table, flow, FLOW_OFFLOAD_DIR_REPLY) || nf_flow_custom_gc(flow_table, flow)) { flow_offload_teardown(flow); teardown = true; diff --git a/net/netfilter/nf_flow_table_path.c b/net/netfilter/nf_flow_table_path.c index 8db396ddbd00..a18fce90b3b2 100644 --- a/net/netfilter/nf_flow_table_path.c +++ b/net/netfilter/nf_flow_table_path.c @@ -101,7 +101,11 @@ struct nft_forward_info { u8 ingress_vlans; u8 h_source[ETH_ALEN]; u8 h_dest[ETH_ALEN]; - bool needs_gso_segment; + u16 bridge_vid; + struct flow_offload_br_outer br_outer; + u8 needs_gso_segment:1, + bridge_path:1, + port_vlans:2; enum flow_offload_xmit_type xmit_type; }; @@ -112,8 +116,9 @@ static int nft_dev_path_info(struct net_device_path_stack *stack, struct nft_forward_info *info, unsigned char *ha, struct nft_flowtable *ft) { + struct flow_offload_br_outer *o = &info->br_outer; const struct net_device_path *path; - int i; + int i, n; memcpy(info->h_dest, ha, ETH_ALEN); @@ -160,6 +165,8 @@ static int nft_dev_path_info(struct net_device_path_stack *stack, info->encap[info->num_encaps].proto = path->encap.proto; info->num_encaps++; + if (info->bridge_path) + info->port_vlans++; } if (path->type == DEV_PATH_PPPOE) { memcpy(info->h_dest, path->encap.h_dest, ETH_ALEN); @@ -171,6 +178,7 @@ static int nft_dev_path_info(struct net_device_path_stack *stack, if (is_zero_ether_addr(info->h_source)) memcpy(info->h_source, path->dev->dev_addr, ETH_ALEN); + n = info->num_encaps; switch (path->bridge.vlan_mode) { case DEV_PATH_BR_VLAN_UNTAG_HW: if (info->num_encaps == 0) @@ -195,6 +203,19 @@ static int nft_dev_path_info(struct net_device_path_stack *stack, case DEV_PATH_BR_VLAN_KEEP: break; } + if (info->bridge_path) { + o->present = 1; + o->vid = info->bridge_vid; + o->link_vlans = info->port_vlans; + o->link_untag = path->bridge.vlan_mode == + DEV_PATH_BR_VLAN_UNTAG; + o->link_8021ad = path->bridge.vlan_proto == + htons(ETH_P_8021AD); + o->inner_tag = info->num_encaps > n; + } + info->bridge_path = 1; + info->bridge_vid = path->bridge.vlan_id; + info->port_vlans = 0; info->xmit_type = FLOW_OFFLOAD_XMIT_DIRECT; break; default: @@ -274,6 +295,10 @@ static int nft_dev_forward_path(const struct nft_pktinfo *pkt, if (info.xmit_type == FLOW_OFFLOAD_XMIT_DIRECT) { memcpy(route->tuple[dir].out.h_source, info.h_source, ETH_ALEN); + route->tuple[dir].out.bridge_path = info.bridge_path; + route->tuple[dir].out.bridge_vid = info.bridge_vid; + route->tuple[dir].out.br_outer = info.br_outer; + route->tuple[dir].out.port_vlans = info.port_vlans; route->tuple[dir].xmit_type = info.xmit_type; } route->tuple[dir].out.needs_gso_segment = info.needs_gso_segment; -- 2.53.0