From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.228 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796149380; b=hscafd6oOwXvcoIjI9aZISungCH6fUgvIYxMIQ3iZ5wVP39YrjdbP6fQW7vBDc3eOYee aStyAxBUBCZRWE7TFJridgZcD8MeeI76BtER3iPJhqJUmIu5q/lEVDabzCGsbDcMjNbis GoQtE1BMwU46dOZQj7UI64ETYoImrZJb0N+Nx0bWOiV0ldVtu8dz7o2QBQ8V2MVUvV+mO da9+HDnfFxouCPqNIBfQ06/1BCKHaxn7tQXED9e62dZeZ9/eF4aiR9bkbxeJO/ltGFGbk nYkjctSH5+URhcphg4TiLDa59vIR/MrmcYIkgTJjm57m41+DwXdzjORHj5B3HE7KFJA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796149380; h=DMARC-Filter:Received:X-Google-DKIM-Signature:X-Gm-Message-State: X-Gm-Gg:X-Received:Received:X-Relaying-Domain:X-CFilter-Loop:Received: DKIM-Signature:X-Received:X-Received:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-DetectorID-Processed; bh=uwTXfxY7dGX2+uwzqcZ9UcoCso7TsjxM2l19bs7jhdc=; b=tH8bFUbLDEQnv85GaNmdlRHQnHFBN8lFOWUkKL1GfPP5drShKt5KUKb0vqkhMH0r/ALk Mt3V4uoRohdV4qKS8+Gd6ro4nNTEhn4vJoRxkZdWqAGNO1tMqId5ZA8glC0Ki0cPN0zQb i20BrEusncF/4EcAMPgjdOOCEdDhbXpslLsIwWzn9jWLktkE6p7CQQX2HVVfpMo6Rad7w XdJFnvRv5eTG/SjFplCFrKAR7GTSZm0ZcVzOtWOO9R+VKPL/czsq09rdKmQePoqE5tVTg ragmtrwA5PZiM6punlAcC9jgL8GMLHhXBbOhMFc3vUfn6+T9tKqtDSmbQTnPmwA1nuw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=broadcom.com; spf=fail smtp.mailfrom=broadcom.com; dkim=pass header.d=broadcom.com header.i=@broadcom.com header.a=rsa-sha256 header.s=google header.b=dWGsyu84; arc=none smtp.remote-ip=74.125.82.228 Received: from mail-dy1-f228.google.com (mail-dy1-f228.google.com [74.125.82.228]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8AECE606A322 for ; Tue, 1 Dec 2026 18:22:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8AECE606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=broadcom.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=broadcom.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.a=rsa-sha256 header.s=google header.b=dWGsyu84 Received: by mail-dy1-f228.google.com with SMTP id 5a478bee46e88-38190551625so22674545eec.1 for ; Tue, 01 Dec 2026 10:22:59 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796149379; x=1796754179; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=uwTXfxY7dGX2+uwzqcZ9UcoCso7TsjxM2l19bs7jhdc=; b=dNhCjj3cjS3jy/oEhbEcD3ErOsFaNqutNHlr9EzDpD3DDIyqMQtPTqGE9GD9Q+mUGt bWOVShbNaqrVxzVvuBGHOiBUA1JuIkffzw5GaLOil66w5Xya2npOrsQUkda6ybUuCa6D HqgfIv0Sc4h2Y8CLOnOFYAyRnuGtG1FqLyIx2p5n+2z7q75grrdr0+bfh5+fSeoG1jbO QTWlkNQtNVmvih0G3QvKmme4H6eTGkZGl0OwIeZC6CNxdVwwmmT0yUrlRscCvJNTWro1 xWEjNibVLkISP5dns3uPR5OcHbuNE+45Fy34XtPNLkPiTO/1EzhrO4ybIaTPmEmm4itu hEDA== X-Gm-Message-State: APuU4Z8eXumfi8mREMavyoXTQ7URjjHbQVBhUlY7MJ86o3EHNJntAzdp enCgz1QBLg/57h2TYD5JWteeNgX4lJchTkqK74v9ewckOmygvbAZ7p5+dre7+SSfOfdfZxq2GM9 JydIFJM8LRrsJqRWIEdYOu1lTP3/IupBgc4pKtofRmzwMcq+v080LukgUjSDWqg9I8emoiHkaZ7 YdqOm3br4WXaHlAU/zyRgd7VIURbuTqmHVgwQQu3isZRIdmjfLX0PSkIHLl6AiQ2LvrhBze7U4J TcrpRkhzBiEIqUB X-Gm-Gg: AZFdTZcWsk88nRwwFwaF+beP+V6oNM9eXlClaqx4Igq/36Xd6IIdohI7os/QkVl284b PMRcycRRlkxR76+JAireZJ8YfG6qI8WunImYvJVqT9pqH45IIk82L4yGB89Rav0CilsLu55ZilJ FEunrY1X3SWPp08MpUtPsnFcD64MIH2qsDOFOw/QAM5coen5kk5c58jrxErKX6Dr7iKriDSDV46 ul86VzcvDCmtip0W1vYyvUfy+GZ0cz7ppt4CuAEuzA44+Aes7bqUCwYj0laaKWxt73hVVzBHhxm adQlaxXIxHsQ6YRJuXkSg1rQlHxFgDeuTlO8HfL9XVTVfVOi8aRlBEvKnyy4J2Tb3EsqHyuZPu3 XM6s+Md2l7ihMoWDSRIks4NdT9iBrhZ3WRBOHqbzVKbsZGeeGIXrL9W1p/mO80k2ACulTVK39C3 Hjli/ZdXQL5ZTLXGeKS9kDcWudDCxpwD+5OsCzhiVi X-Received: by 2002:a05:7301:6188:10b0:386:9ba8:2864 with SMTP id 5a478bee46e88-3869ba82acfmr30339347eec.0.1796149378217; Tue, 01 Dec 2026 10:22:58 -0800 (PST) Received: from smtp-us-east1-p01-i01-si01.dlp.protect.broadcom.com (address-144-49-247-28.dlp.protect.broadcom.com. [144.49.247.28]) by smtp-relay.gmail.com with ESMTPS id 5a478bee46e88-385667decddsm6373345eec.9.2026.12.01.10.22.57 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Dec 2026 10:22:58 -0800 (PST) X-Relaying-Domain: broadcom.com X-CFilter-Loop: Reflected Received: by mail-pf1-f197.google.com with SMTP id d2e1a72fcca58-8b691cf0eeaso14151791b3a.1 for ; Tue, 01 Dec 2026 10:22:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; t=1796149376; x=1796754176; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=uwTXfxY7dGX2+uwzqcZ9UcoCso7TsjxM2l19bs7jhdc=; b=dWGsyu84jd+IdXcWNVEMfhSoB10yjCiAOnPEvrzv/fMLvOihAslP02PAI5obY9Strw OUZxVYTIxWmE0zS4+AoW85tfTYo6XW9u83THrRotHg/pAe8Vk5ZVddbONN7BnHdu64zs cjt4ezkYpAW+9Br1wjWSD7TyHONe+thhhi14A= X-Received: by 2002:a05:6a00:3d10:b0:880:78ca:8400 with SMTP id d2e1a72fcca58-8c87bdb46c0mr39524033b3a.12.1796149376083; Tue, 01 Dec 2026 10:22:56 -0800 (PST) X-Received: by 2002:a05:6a00:3d10:b0:880:78ca:8400 with SMTP id d2e1a72fcca58-8c87bdb46c0mr39523985b3a.12.1796149375299; Tue, 01 Dec 2026 10:22:55 -0800 (PST) Received: from fainelli-desktop.igp.broadcom.net ([192.19.223.252]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8c87392d408sm20225242b3a.44.2026.12.01.10.22.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 10:22:54 -0800 (PST) From: Florian Fainelli To: netdev@vger.kernel.org Cc: Florian Fainelli , 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 Message-ID: <20261201182227.3990791-4-florian.fainelli@broadcom.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261201182227.3990791-1-florian.fainelli@broadcom.com> References: <20261201182227.3990791-1-florian.fainelli@broadcom.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-DetectorID-Processed: b00c1d49-9d2e-4205-b15f-d015386d3d5e 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 --- 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 + 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 #include #include +#include #include #include #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 #include #include +#include #include #include #include @@ -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 #include #include +#include +#include #include +#include #include #include #include @@ -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);