Netdev List
 help / color / mirror / Atom feed
From: Florian Fainelli <florian.fainelli@broadcom.com>
To: netdev@vger.kernel.org
Cc: Florian Fainelli <florian.fainelli@broadcom.com>,
	bridge@lists.linux.dev, coreteam@netfilter.org,
	davem@davemloft.net, edumazet@kernel.org, fw@strlen.de,
	horms@kernel.org, idosch@nvidia.com, kuba@kernel.org,
	netfilter-devel@vger.kernel.org, pabeni@redhat.com,
	pablo@netfilter.org, phil@nwl.cc, razor@blackwall.org,
	willemb@google.com
Subject: [PATCH net-next v2 3/6] net: flow_offload: allow drivers to validate and invalidate cached paths
Date: Tue,  1 Dec 2026 10:22:24 -0800	[thread overview]
Message-ID: <20261201182227.3990791-4-florian.fainelli@broadcom.com> (raw)
In-Reply-To: <20261201182227.3990791-1-florian.fainelli@broadcom.com>

A classifier can lack the mark, DSCP and output-interface context used
to select its forwarding path. Provide optional owner validation and
invalidation callbacks so an accelerator can validate the actual cached
routes and Ethernet peers instead of treating a partial FIB lookup as
proof that a software path is stale.

Retain direct-path dst references for validating hardware consumers.
Mark references held only for that purpose so packet lookup and GC keep
their existing route-validation policy. Validate without creating a
neighbour, and report missing or unsupported context as unavailable. For
untagged bridge paths, resolve the bridge neighbour through the FDB and
compare the physical transmit port. Return the reverse cached peer MAC
when requested, so ingress guards use the owner's authoritative peer.
Allow the bridge STP accessor under either RTNL or RCU for flow workers.

Notify forwarding caches before FDB movement or deletion and after
publication, including administrative dynamic entries hidden from
switchdev. Track a generation per network namespace for flows traversing
a bridge, sampled before path construction. Reject stale construction,
packet lookup, GC and hardware admission. This also retires software-
only and hardware-capacity fallback owners after a client moves, without
walking flowtables while bridge learning holds its locks. Non-bridge
flows keep their existing semantics.

Invalidation marks the owning connection for teardown. Owners retain the
cookie through an RCU grace period after DESTROY; drivers stop calling
back when their rules are destroyed. Conntrack ownership remains held
until asynchronous deletion has completed.

Signed-off-by: Florian Fainelli <florian.fainelli@broadcom.com>
---
Changes in v2:
  - Validate the owner's cached route and neighbour without allocating
    entries; missing context is unavailable validation rather than proof of
    a stale software path.
  - Resolve untagged bridge neighbours through the FDB and return the
    authoritative reverse peer MAC for ingress guards. Permit the bridge
    STP accessor under RCU as well as RTNL.
  - Notify forwarding caches around FDB mutation and publication. Track a
    per-network-namespace generation for bridge paths so software-only and
    capacity-fallback owners also retire on moves, deletion and aging.
    Preserve non-bridge cache semantics.

 include/net/flow_offload.h            |  14 +++
 include/net/netevent.h                |   1 +
 include/net/netfilter/nf_flow_table.h |  13 ++-
 include/net/netns/flow_table.h        |   3 +
 net/bridge/br_fdb.c                   |  26 ++++-
 net/bridge/br_stp.c                   |   7 +-
 net/netfilter/nf_flow_table_core.c    |  57 ++++++++++-
 net/netfilter/nf_flow_table_offload.c | 139 ++++++++++++++++++++++++++
 net/netfilter/nf_flow_table_path.c    |   8 ++
 net/netfilter/nft_flow_offload.c      |   6 +-
 10 files changed, 257 insertions(+), 17 deletions(-)

diff --git a/include/net/flow_offload.h b/include/net/flow_offload.h
index 70a02ee143080..ab5c0677debb4 100644
--- a/include/net/flow_offload.h
+++ b/include/net/flow_offload.h
@@ -697,6 +697,20 @@ struct flow_cls_offload {
 	bool use_act_stats;
 	unsigned long cookie;
 	struct flow_rule *rule;
+	/* Optional owner notification when a rule no longer represents the path.
+	 * Call under RCU; the owner retains the cookie and callback until a
+	 * grace period after DESTROY. Drivers must stop new calls at DESTROY.
+	 * The callback must not sleep or call back into the offload driver.
+	 */
+	void (*invalidate)(unsigned long cookie);
+	/* Optional cached-path validation, called under RCU during REPLACE.
+	 * Return 0 for a valid path, -ESTALE for a demonstrably stale path,
+	 * or -EOPNOTSUPP when validation is unavailable. Do not sleep or
+	 * call back into the driver. The callback does not invalidate its owner.
+	 * If ingress_mac is non-NULL, also return the peer MAC from the cached
+	 * reverse Ethernet path, or -EOPNOTSUPP if it is unavailable.
+	 */
+	int (*validate)(unsigned long cookie, u8 *ingress_mac);
 	struct flow_stats stats;
 	u32 classid;
 };
diff --git a/include/net/netevent.h b/include/net/netevent.h
index 1be3757a8b7f0..68258accc9eab 100644
--- a/include/net/netevent.h
+++ b/include/net/netevent.h
@@ -30,6 +30,7 @@ enum netevent_notif_type {
 	NETEVENT_IPV4_MPATH_HASH_UPDATE, /* arg is struct net ptr */
 	NETEVENT_IPV6_MPATH_HASH_UPDATE, /* arg is struct net ptr */
 	NETEVENT_IPV4_FWD_UPDATE_PRIORITY_UPDATE, /* arg is struct net ptr */
+	NETEVENT_BRIDGE_FDB_UPDATE, /* arg is the bridge net_device ptr */
 };
 
 int register_netevent_notifier(struct notifier_block *nb);
diff --git a/include/net/netfilter/nf_flow_table.h b/include/net/netfilter/nf_flow_table.h
index 5b22d31cb1407..c9dcd9bc8d3d3 100644
--- a/include/net/netfilter/nf_flow_table.h
+++ b/include/net/netfilter/nf_flow_table.h
@@ -154,7 +154,8 @@ struct flow_offload_tuple {
 					encap_num:2,
 					needs_gso_segment:1,
 					tun_num:2,
-					in_vlan_ingress:2;
+					in_vlan_ingress:2,
+					dst_validation_only:1;
 	u16				mtu;
 	u32				dst_cookie;
 	struct dst_entry		*dst_cache;
@@ -190,6 +191,7 @@ enum nf_flow_flags {
 	NF_FLOW_HW_DEAD,
 	NF_FLOW_HW_BIDIRECTIONAL,
 	NF_FLOW_HW_ESTABLISHED,
+	NF_FLOW_BRIDGE,
 };
 
 enum flow_offload_type {
@@ -203,6 +205,7 @@ struct flow_offload {
 	unsigned long				flags;
 	u16					type;
 	u32					timeout;
+	u32					bridge_genid;
 	struct rcu_head				rcu_head;
 };
 
@@ -210,6 +213,7 @@ struct flow_offload {
 #define nf_flowtable_time_stamp	(u32)jiffies
 
 unsigned long flow_offload_get_timeout(struct flow_offload *flow);
+bool nf_flow_bridge_stale(const struct flow_offload *flow);
 
 static inline __s32 nf_flow_timeout_delta(unsigned int timeout)
 {
@@ -217,6 +221,8 @@ static inline __s32 nf_flow_timeout_delta(unsigned int timeout)
 }
 
 struct nf_flow_route {
+	u32 bridge_genid;
+	bool uses_bridge;
 	struct {
 		struct dst_entry		*dst;
 		struct {
@@ -305,7 +311,7 @@ nf_flow_table_offload_del_cb(struct nf_flowtable *flow_table,
 }
 
 void flow_offload_route_init(struct flow_offload *flow,
-			     struct nf_flow_route *route);
+			     struct nf_flow_route *route, bool keep_dst);
 
 int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow);
 void flow_offload_refresh(struct nf_flowtable *flow_table,
@@ -313,7 +319,8 @@ void flow_offload_refresh(struct nf_flowtable *flow_table,
 
 static inline bool nf_flow_dst_check(struct flow_offload_tuple *tuple)
 {
-	if (!tuple->dst_cache)
+	/* A driver-only reference must not change direct-path/GC semantics. */
+	if (!tuple->dst_cache || tuple->dst_validation_only)
 		return true;
 
 	return dst_check(tuple->dst_cache, tuple->dst_cookie);
diff --git a/include/net/netns/flow_table.h b/include/net/netns/flow_table.h
index 1c5fc657e2675..b286b23116ac1 100644
--- a/include/net/netns/flow_table.h
+++ b/include/net/netns/flow_table.h
@@ -2,6 +2,8 @@
 #ifndef __NETNS_FLOW_TABLE_H
 #define __NETNS_FLOW_TABLE_H
 
+#include <linux/atomic.h>
+
 struct nf_flow_table_stat {
 	unsigned int count_wq_add;
 	unsigned int count_wq_del;
@@ -10,5 +12,6 @@ struct nf_flow_table_stat {
 
 struct netns_ft {
 	struct nf_flow_table_stat __percpu *stat;
+	atomic_t bridge_genid;
 };
 #endif
diff --git a/net/bridge/br_fdb.c b/net/bridge/br_fdb.c
index b601245ad9239..922c34fdfecd4 100644
--- a/net/bridge/br_fdb.c
+++ b/net/bridge/br_fdb.c
@@ -20,6 +20,7 @@
 #include <linux/atomic.h>
 #include <linux/unaligned.h>
 #include <linux/if_vlan.h>
+#include <net/netevent.h>
 #include <net/switchdev.h>
 #include <trace/events/bridge.h>
 #include "br_private.h"
@@ -174,6 +175,15 @@ static inline size_t fdb_nlmsg_size(void)
 		+ nla_total_size(sizeof(u8)); /* NFEA_ACTIVITY_NOTIFY */
 }
 
+/* Forwarding caches must also observe FDB changes which are intentionally
+ * hidden from switchdev, such as software-only administrative entries.
+ * Notify before changing an existing destination and after publication.
+ */
+static void fdb_notify_cache(struct net_bridge *br)
+{
+	call_netevent_notifiers(NETEVENT_BRIDGE_FDB_UPDATE, br->dev);
+}
+
 static void fdb_notify(struct net_bridge *br,
 		       const struct net_bridge_fdb_entry *fdb, int type,
 		       bool swdev_notify)
@@ -182,6 +192,7 @@ static void fdb_notify(struct net_bridge *br,
 	struct sk_buff *skb;
 	int err = -ENOBUFS;
 
+	fdb_notify_cache(br);
 	if (swdev_notify)
 		br_switchdev_fdb_notify(br, fdb, type);
 
@@ -315,6 +326,7 @@ static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
 static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
 		       bool swdev_notify)
 {
+	fdb_notify_cache(br);
 	trace_fdb_delete(br, f, br_fdb_dst_port(f));
 
 	if (test_bit(BR_FDB_STATIC, &f->flags))
@@ -350,8 +362,10 @@ static void fdb_delete_local(struct net_bridge *br,
 		vg = nbp_vlan_group(op);
 		if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
 		    (!vid || br_vlan_find(vg, vid))) {
+			fdb_notify_cache(br);
 			br_fdb_dst_write(f, br_port_to_dst(op));
 			clear_bit(BR_FDB_ADDED_BY_USER, &f->flags);
+			fdb_notify_cache(br);
 			return;
 		}
 	}
@@ -361,8 +375,10 @@ static void fdb_delete_local(struct net_bridge *br,
 	/* Maybe bridge device has same hw addr? */
 	if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
 	    (!vid || (v && br_vlan_should_use(v)))) {
+		fdb_notify_cache(br);
 		br_fdb_dst_write(f, br_port_to_dst(NULL));
 		clear_bit(BR_FDB_ADDED_BY_USER, &f->flags);
+		fdb_notify_cache(br);
 		return;
 	}
 
@@ -409,6 +425,7 @@ static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
 		return NULL;
 
 	memcpy(fdb->key.addr.addr, addr, ETH_ALEN);
+	fdb_notify_cache(br);
 	br_fdb_dst_write(fdb, dst);
 	fdb->key.vlan_id = vid;
 	fdb->flags = flags;
@@ -1030,6 +1047,7 @@ static void __fdb_update(struct net_bridge *br,
 		      !test_bit(BR_FDB_STICKY, &fdb->flags)))) {
 		if (source != old_port) {
 			br_switchdev_fdb_notify(br, fdb, RTM_DELNEIGH);
+			fdb_notify_cache(br);
 			br_fdb_dst_write(fdb, dst);
 			fdb_modified = true;
 			/* take over HW learned entry */
@@ -1194,7 +1212,7 @@ static bool fdb_handle_notify(struct net_bridge_fdb_entry *fdb, u8 notify)
 	return modified;
 }
 
-static bool fdb_update_dst(struct net_bridge_fdb_entry *fdb,
+static bool fdb_update_dst(struct net_bridge *br, struct net_bridge_fdb_entry *fdb,
 			   struct net_bridge_dst old_dst,
 			   struct net_bridge_dst dst)
 {
@@ -1205,7 +1223,7 @@ static bool fdb_update_dst(struct net_bridge_fdb_entry *fdb,
 		br_fdb_dst_replace(fdb, old_dst, dst);
 		return false;
 	}
-
+	fdb_notify_cache(br);
 	br_fdb_dst_write(fdb, dst);
 	return true;
 }
@@ -1263,7 +1281,7 @@ static int fdb_add_entry(struct net_bridge *br, struct net_bridge_dst dst,
 			return -EEXIST;
 
 		old_dst = br_fdb_dst_read(fdb);
-		modified = fdb_update_dst(fdb, old_dst, dst);
+		modified = fdb_update_dst(br, fdb, old_dst, dst);
 
 		set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
 		if (test_and_clear_bit(BR_FDB_DYNAMIC_LEARNED, &fdb->flags))
@@ -1644,7 +1662,7 @@ int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
 
 		WRITE_ONCE(fdb->updated, jiffies);
 
-		modified = fdb_update_dst(fdb, old_dst, dst);
+		modified = fdb_update_dst(br, fdb, old_dst, dst);
 
 		if (test_and_set_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) {
 			/* Refresh entry */
diff --git a/net/bridge/br_stp.c b/net/bridge/br_stp.c
index c7e7e924f1557..5717bf33c2667 100644
--- a/net/bridge/br_stp.c
+++ b/net/bridge/br_stp.c
@@ -70,17 +70,16 @@ void br_set_state(struct net_bridge_port *p, unsigned int state)
 	}
 }
 
+/* Caller holds RTNL or RCU. */
 u8 br_port_get_stp_state(const struct net_device *dev)
 {
 	struct net_bridge_port *p;
 
-	ASSERT_RTNL();
-
-	p = br_port_get_rtnl(dev);
+	p = br_port_get_rtnl_rcu(dev);
 	if (!p)
 		return BR_STATE_DISABLED;
 
-	return p->state;
+	return READ_ONCE(p->state);
 }
 EXPORT_SYMBOL_GPL(br_port_get_stp_state);
 
diff --git a/net/netfilter/nf_flow_table_core.c b/net/netfilter/nf_flow_table_core.c
index 0efa55de878ee..b5cb465542831 100644
--- a/net/netfilter/nf_flow_table_core.c
+++ b/net/netfilter/nf_flow_table_core.c
@@ -7,6 +7,7 @@
 #include <linux/netdevice.h>
 #include <net/ip.h>
 #include <net/ip6_route.h>
+#include <net/netevent.h>
 #include <net/netfilter/nf_tables.h>
 #include <net/netfilter/nf_flow_table.h>
 #include <net/netfilter/nf_conntrack.h>
@@ -96,7 +97,7 @@ static struct dst_entry *nft_route_dst_fetch(struct nf_flow_route *route,
 
 static int flow_offload_fill_route(struct flow_offload *flow,
 				   struct nf_flow_route *route,
-				   enum flow_offload_tuple_dir dir)
+				   enum flow_offload_tuple_dir dir, bool keep_dst)
 {
 	struct flow_offload_tuple *flow_tuple = &flow->tuplehash[dir].tuple;
 	struct dst_entry *dst = nft_route_dst_fetch(route, dir);
@@ -127,7 +128,9 @@ static int flow_offload_fill_route(struct flow_offload *flow,
 
 	switch (route->tuple[dir].xmit_type) {
 	case FLOW_OFFLOAD_XMIT_DIRECT:
-		if (route->tuple[!dir].in.num_tuns) {
+		flow_tuple->dst_validation_only = keep_dst &&
+			!route->tuple[!dir].in.num_tuns;
+		if (keep_dst || route->tuple[!dir].in.num_tuns) {
 			flow_tuple->dst_cache = dst;
 			flow_tuple->dst_cookie =
 				flow_offload_dst_cookie(flow_tuple);
@@ -162,14 +165,27 @@ static void nft_flow_dst_release(struct flow_offload *flow,
 }
 
 void flow_offload_route_init(struct flow_offload *flow,
-			     struct nf_flow_route *route)
+			     struct nf_flow_route *route, bool keep_dst)
 {
-	flow_offload_fill_route(flow, route, FLOW_OFFLOAD_DIR_ORIGINAL);
-	flow_offload_fill_route(flow, route, FLOW_OFFLOAD_DIR_REPLY);
+	flow_offload_fill_route(flow, route, FLOW_OFFLOAD_DIR_ORIGINAL, keep_dst);
+	flow_offload_fill_route(flow, route, FLOW_OFFLOAD_DIR_REPLY, keep_dst);
 	flow->type = NF_FLOW_OFFLOAD_ROUTE;
+	if (route->uses_bridge) {
+		flow->bridge_genid = route->bridge_genid;
+		__set_bit(NF_FLOW_BRIDGE, &flow->flags);
+	}
 }
 EXPORT_SYMBOL_GPL(flow_offload_route_init);
 
+bool nf_flow_bridge_stale(const struct flow_offload *flow)
+{
+	if (!test_bit(NF_FLOW_BRIDGE, &flow->flags))
+		return false;
+
+	return flow->bridge_genid !=
+	       (u32)atomic_read(&nf_ct_net(flow->ct)->ft.bridge_genid);
+}
+
 static inline bool nf_flow_has_expired(const struct flow_offload *flow)
 {
 	return nf_flow_timeout_delta(flow->timeout) <= 0;
@@ -336,6 +352,9 @@ int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)
 {
 	int err;
 
+	if (nf_flow_bridge_stale(flow))
+		return -ESTALE;
+
 	flow->timeout = nf_flowtable_time_stamp + flow_offload_get_timeout(flow);
 
 	err = rhashtable_insert_fast(&flow_table->rhashtable,
@@ -425,6 +444,11 @@ flow_offload_lookup(struct nf_flowtable *flow_table,
 	if (test_bit(NF_FLOW_TEARDOWN, &flow->flags))
 		return NULL;
 
+	if (unlikely(nf_flow_bridge_stale(flow))) {
+		flow_offload_teardown(flow);
+		return NULL;
+	}
+
 	if (unlikely(nf_ct_is_dying(flow->ct)))
 		return NULL;
 
@@ -595,6 +619,7 @@ 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_bridge_stale(flow) ||
 	    !nf_flow_dst_check(&flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple) ||
 	    !nf_flow_dst_check(&flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple) ||
 	    nf_flow_custom_gc(flow_table, flow)) {
@@ -798,6 +823,7 @@ EXPORT_SYMBOL_GPL(nf_flow_table_free);
 
 static int nf_flow_table_init_net(struct net *net)
 {
+	atomic_set(&net->ft.bridge_genid, 0);
 	net->ft.stat = alloc_percpu(struct nf_flow_table_stat);
 	return net->ft.stat ? 0 : -ENOMEM;
 }
@@ -841,6 +867,24 @@ static struct pernet_operations nf_flow_table_net_ops = {
 	.exit_batch = nf_flow_table_pernet_exit,
 };
 
+static int nf_flow_table_netevent(struct notifier_block *nb,
+				  unsigned long event, void *ptr)
+{
+	struct net_device *dev = ptr;
+
+	if (event == NETEVENT_BRIDGE_FDB_UPDATE) {
+		/* Order the notification against the FDB mutation on either side.
+		 * Do not walk flowtables here: learning can hold the bridge lock.
+		 */
+		atomic_inc_return(&dev_net(dev)->ft.bridge_genid);
+	}
+	return NOTIFY_DONE;
+}
+
+static struct notifier_block nf_flow_table_netevent_notifier = {
+	.notifier_call = nf_flow_table_netevent,
+};
+
 static int __init nf_flow_table_module_init(void)
 {
 	int ret;
@@ -861,6 +905,8 @@ static int __init nf_flow_table_module_init(void)
 	if (ret)
 		goto out_bpf;
 
+	register_netevent_notifier(&nf_flow_table_netevent_notifier);
+
 	return 0;
 
 out_bpf:
@@ -874,6 +920,7 @@ static int __init nf_flow_table_module_init(void)
 
 static void __exit nf_flow_table_module_exit(void)
 {
+	unregister_netevent_notifier(&nf_flow_table_netevent_notifier);
 	rcu_barrier();
 	nf_flow_table_offload_exit();
 	unregister_pernet_subsys(&nf_flow_table_net_ops);
diff --git a/net/netfilter/nf_flow_table_offload.c b/net/netfilter/nf_flow_table_offload.c
index 4365859220e6a..daf42db2b31cf 100644
--- a/net/netfilter/nf_flow_table_offload.c
+++ b/net/netfilter/nf_flow_table_offload.c
@@ -7,7 +7,10 @@
 #include <linux/netdevice.h>
 #include <linux/tc_act/tc_csum.h>
 #include <net/flow_offload.h>
+#include <kunit/static_stub.h>
+#include <net/arp.h>
 #include <net/ip_tunnels.h>
+#include <net/route.h>
 #include <net/netfilter/nf_flow_table.h>
 #include <net/netfilter/nf_tables.h>
 #include <net/netfilter/nf_conntrack.h>
@@ -885,6 +888,140 @@ static int nf_flow_offload_alloc(const struct flow_offload_work *offload,
 	return 0;
 }
 
+static struct flow_offload *nf_flow_offload_cookie_flow(unsigned long cookie)
+{
+	struct flow_offload_tuple *tuple = (struct flow_offload_tuple *)cookie;
+	struct flow_offload_tuple_rhash *tuplehash;
+	struct flow_offload *flow;
+
+	tuplehash = container_of(tuple, struct flow_offload_tuple_rhash, tuple);
+	flow = container_of(tuplehash, struct flow_offload, tuplehash[tuple->dir]);
+	return flow;
+}
+
+static void nf_flow_offload_invalidate(unsigned long cookie)
+{
+	flow_offload_teardown(nf_flow_offload_cookie_flow(cookie));
+}
+
+/* Look up the next hop of a cached IPv4 route without creating an entry:
+ * a neighbour which was garbage collected does not make the path stale.
+ * Must be called under RCU.
+ */
+static struct neighbour *nf_flow_offload_neigh(const struct dst_entry *dst,
+					       struct net_device *dev,
+					       const struct flow_offload_tuple *other)
+{
+	KUNIT_STATIC_STUB_REDIRECT(nf_flow_offload_neigh, dst, dev, other);
+	return __ipv4_neigh_lookup_noref(dev,
+					 (__force u32)rt_nexthop(dst_rtable(dst),
+								 other->src_v4.s_addr));
+}
+
+/* A bridge route retains its L3 neighbour on the bridge, while the direct
+ * transmit cache names a bridge port. Resolve that same neighbour through the
+ * current FDB instead of treating the different devices as unavailable context.
+ */
+static int nf_flow_offload_bridge_path(const struct flow_offload_tuple *tuple,
+				       struct net_device *dev, const u8 *mac)
+{
+	struct net_device_path_ctx ctx = {
+		.dev = dev,
+		.ether_type = htons(ETH_P_IP),
+	};
+	struct net_device_path_stack stack;
+	const struct net_device_path *path;
+	int err;
+
+	if (!netif_is_bridge_master(dev))
+		return -EOPNOTSUPP;
+	ether_addr_copy(ctx.daddr, mac);
+	if (dev_fill_forward_path(&ctx, &stack) < 0)
+		return -ESTALE;
+	err = -EOPNOTSUPP;
+	path = &stack.path[0];
+	if (stack.num_paths != 2 || path->type != DEV_PATH_BRIDGE ||
+	    path->bridge.vlan_mode != DEV_PATH_BR_VLAN_KEEP || path->bridge.vlan_id)
+		goto out;
+	path = &stack.path[1];
+	if (path->type != DEV_PATH_ETHERNET && path->type != DEV_PATH_DSA)
+		goto out;
+	err = path->dev->ifindex == tuple->out.ifidx &&
+	      ether_addr_equal(dev->dev_addr, tuple->out.h_source) ? 0 : -ESTALE;
+out:
+	dev_fill_forward_path_release(&stack);
+	return err;
+}
+
+static int __nf_flow_offload_validate(unsigned long cookie)
+{
+	struct flow_offload *flow = nf_flow_offload_cookie_flow(cookie);
+	unsigned int dir;
+
+	if (nf_flow_bridge_stale(flow))
+		return -ESTALE;
+
+	for (dir = 0; dir < FLOW_OFFLOAD_DIR_MAX; dir++) {
+		struct flow_offload_tuple *tuple = &flow->tuplehash[dir].tuple;
+		struct flow_offload_tuple *other = &flow->tuplehash[!dir].tuple;
+		struct dst_entry *dst = tuple->dst_cache;
+		struct net_device *dev;
+		struct neighbour *neigh;
+		u8 mac[ETH_ALEN];
+
+		if (!dst)
+			return -EOPNOTSUPP;
+		/* This is the actual route selected with the packet's policy
+		 * context, including mark, DSCP and the reverse lookup's oif.
+		 */
+		if (!dst_check(dst, tuple->dst_cookie))
+			return -ESTALE;
+		if (tuple->xmit_type != FLOW_OFFLOAD_XMIT_DIRECT)
+			continue;
+		/* A stacked path may have a different final Ethernet neighbour.
+		 * Do not mistake that unsupported context for a stale owner.
+		 */
+		dev = dst_dev_rcu(dst);
+		if (tuple->l3proto != NFPROTO_IPV4)
+			return -EOPNOTSUPP;
+		/* Without an entry the path is not proven stale. */
+		neigh = nf_flow_offload_neigh(dst, dev, other);
+		if (!neigh)
+			return -EOPNOTSUPP;
+		neigh_ha_snapshot(mac, neigh, dev);
+		if (!(READ_ONCE(neigh->nud_state) & NUD_VALID) ||
+		    !ether_addr_equal(mac, tuple->out.h_dest))
+			return -ESTALE;
+		if (dev->ifindex != tuple->out.ifidx) {
+			int err = nf_flow_offload_bridge_path(tuple, dev, mac);
+
+			if (err)
+				return err;
+		}
+	}
+	return 0;
+}
+
+static int nf_flow_offload_validate(unsigned long cookie, u8 *ingress_mac)
+{
+	int err;
+
+	rcu_read_lock();
+	err = __nf_flow_offload_validate(cookie);
+	if (!err && ingress_mac) {
+		const struct flow_offload_tuple *tuple = (void *)cookie;
+		struct flow_offload *flow = nf_flow_offload_cookie_flow(cookie);
+		const struct flow_offload_tuple *other = &flow->tuplehash[!tuple->dir].tuple;
+
+		if (other->xmit_type != FLOW_OFFLOAD_XMIT_DIRECT)
+			err = -EOPNOTSUPP;
+		else
+			ether_addr_copy(ingress_mac, other->out.h_dest);
+	}
+	rcu_read_unlock();
+	return err;
+}
+
 static void nf_flow_offload_init(struct flow_cls_offload *cls_flow,
 				 __be16 proto, int priority,
 				 enum flow_cls_command cmd,
@@ -896,6 +1033,8 @@ static void nf_flow_offload_init(struct flow_cls_offload *cls_flow,
 	cls_flow->common.extack = extack;
 	cls_flow->command = cmd;
 	cls_flow->cookie = (unsigned long)tuple;
+	cls_flow->invalidate = nf_flow_offload_invalidate;
+	cls_flow->validate = nf_flow_offload_validate;
 }
 
 static int nf_flow_offload_tuple(struct nf_flowtable *flowtable,
diff --git a/net/netfilter/nf_flow_table_path.c b/net/netfilter/nf_flow_table_path.c
index 841f099434d98..de862e3e7770d 100644
--- a/net/netfilter/nf_flow_table_path.c
+++ b/net/netfilter/nf_flow_table_path.c
@@ -96,6 +96,7 @@ struct nft_forward_info {
 	u8 h_source[ETH_ALEN];
 	u8 h_dest[ETH_ALEN];
 	bool needs_gso_segment;
+	bool uses_bridge;
 	enum flow_offload_xmit_type xmit_type;
 };
 
@@ -157,6 +158,7 @@ static int nft_dev_path_info(struct net_device_path_stack *stack,
 			}
 			break;
 		case DEV_PATH_BRIDGE:
+			info->uses_bridge = true;
 			if (is_zero_ether_addr(info->h_source))
 				memcpy(info->h_source, path->dev->dev_addr, ETH_ALEN);
 
@@ -265,6 +267,7 @@ static int nft_dev_forward_path(const struct nft_pktinfo *pkt,
 		route->tuple[dir].xmit_type = info.xmit_type;
 	}
 	route->tuple[dir].out.needs_gso_segment = info.needs_gso_segment;
+	route->uses_bridge |= info.uses_bridge;
 
 	return 0;
 }
@@ -277,6 +280,11 @@ int nft_flow_route(const struct nft_pktinfo *pkt, const struct nf_conn *ct,
 	struct dst_entry *other_dst = NULL;
 	struct flowi fl;
 
+	/* Snapshot before either FDB lookup. An update during path resolution
+	 * must invalidate the resulting cache, including software-only owners.
+	 */
+	route->bridge_genid = atomic_read_acquire(&nft_net(pkt)->ft.bridge_genid);
+
 	memset(&fl, 0, sizeof(fl));
 	switch (nft_pf(pkt)) {
 	case NFPROTO_IPV4:
diff --git a/net/netfilter/nft_flow_offload.c b/net/netfilter/nft_flow_offload.c
index d3c5651dd6997..05207ca808ee7 100644
--- a/net/netfilter/nft_flow_offload.c
+++ b/net/netfilter/nft_flow_offload.c
@@ -113,7 +113,11 @@ static void nft_flow_offload_eval(const struct nft_expr *expr,
 	if (!flow)
 		goto err_flow_alloc;
 
-	flow_offload_route_init(flow, &route);
+	/* Hardware admission may validate the route used to build a direct path.
+	 * Keep its reference and cookie instead of reconstructing a lookup with
+	 * incomplete policy-routing inputs in the driver.
+	 */
+	flow_offload_route_init(flow, &route, nf_flowtable_hw_offload(flowtable));
 	if (tcph)
 		flow_offload_ct_tcp(ct);
 

  parent reply	other threads:[~2026-12-01 18:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 18:22 [PATCH net-next v2 0/6] Add Northstar FA TCP/UDP NAT offload with a bridged LAN Florian Fainelli
2026-12-01 18:22 ` [PATCH net-next v2 1/6] dt-bindings: net: describe the Northstar flow accelerator Florian Fainelli
2026-12-01 18:22 ` [PATCH net-next v2 2/6] net: dsa: add Broadcom FA metadata and IPv4 exception paths Florian Fainelli
2026-12-01 18:22 ` Florian Fainelli [this message]
2026-12-01 18:22 ` [PATCH net-next v2 4/6] net: bgmac: add Northstar IPv4 TCP/UDP NAT flowtable offload Florian Fainelli
2026-12-01 18:22 ` [PATCH net-next v2 5/6] net: add KUnit coverage for FA and flowtable path validation Florian Fainelli
2026-12-01 18:22 ` [PATCH net-next v2 6/6] docs: net: document Northstar FA setup and NAT qualification Florian Fainelli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201182227.3990791-4-florian.fainelli@broadcom.com \
    --to=florian.fainelli@broadcom.com \
    --cc=bridge@lists.linux.dev \
    --cc=coreteam@netfilter.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=fw@strlen.de \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pablo@netfilter.org \
    --cc=phil@nwl.cc \
    --cc=razor@blackwall.org \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox