From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.216.245.30 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796313518; b=ZEWbY4rox15DCVM/AsPVmp3q5cgV1UvJfxwvfJVbpCbyAcKuIeGYXJvsddQ5CiTb5CSO 4bn9oMyT8yF2qOUhzmNpWD/wZygNKpJ8gLfVhH1m4BUewLO7Z1cDnhSmsWqdAfGfMaFWH PzA2XpftiJInWSq+zUGqZYx2l+PwFbGyhuw9ZuSUrcAMHYq7s/TIXnMePTJ+iBbXKGIZ6 mNphNbz561/XQwpMU0HjetlSn83f1C558QdECaXgylktQPtKg6GRNS9pAQvgLmTmtzygW TGauJZYZt2BqDOMLXp/qHjBOUHWOzpMA036I016vC4qxgkpb0IlEeRKFPWRRJQY0MNw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796313518; h=DMARC-Filter:Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=/r6d17NjtF0wHb6o/DoYmmBMzdTIzPR2f4cCEKddEKg=; b=DqC24rtGUzYRSLvNgTGQM04i8r+raTizjb93EhQq7VuzXu3t5HZMT2B3bUwoXxZNj96Y f0JxUhlJZIdZuX1pkCU3TfRBAV0sBEirscEzq4tKodctnRJn6thc0TwUsp5dQ54c6tadE vv24ept4AEQpvdsQ7WmQUzW/fHHbxJlSuCHyJ3GBZ5sog+B6X1UL9Y+26ZpbB2GROA+1G p+mbHjKE6fKggeqjV/vRbowW353jrXKQbZVGmjHCfSkovmxIWPI3htKavihCBZBOWjEfS 0yJo81JXTOn9XVTi1sKdBumJshWBonnvR8x8DLMPQtECZYYi5U5m2fZnrDn8cRKMNFA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=strlen.de; spf=pass smtp.mailfrom=Chamillionaire.breakpoint.cc; arc=none smtp.remote-ip=91.216.245.30 Received: from Chamillionaire.breakpoint.cc (Chamillionaire.breakpoint.cc [91.216.245.30]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E7A13606A322; Thu, 3 Dec 2026 15:58:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E7A13606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=strlen.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=Chamillionaire.breakpoint.cc Received: by Chamillionaire.breakpoint.cc (Postfix, from userid 1003) id 5430A60538; Thu, 03 Dec 2026 16:58:36 +0100 (CET) From: Florian Westphal To: Cc: Florian Westphal , stable@vger.kernel.org Subject: [PATCH nf-next v4 3/9] netfilter: conntrack: refactor helper call logic in nf_confirm() Date: Thu, 3 Dec 2026 16:58:06 +0100 Message-ID: <20261203155812.8873-4-fw@strlen.de> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261203155812.8873-1-fw@strlen.de> References: <20261203155812.8873-1-fw@strlen.de> X-Mailing-List: stable@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 The current implementation of nf_confirm() handles several distinct tasks: retrieving the connection, calculating protocol offsets for different network families, and executing the associated helpers or sequence adjustments. Move functionality to separate functions: 1. nf_confirm_get_protoff(): Isolates the logic for determining the protocol offset (protoff) for IPv4 and IPv6 packets. 2. nf_ct_call_helper(): Encapsulates the decision-making process of whether to call a connection tracking helper or perform sequence adjustments, utilizing nf_confirm_get_protoff() to find the correct offset. NF_CT_STAT_INC_ATOMIC() is now called for any NF_DROP; earlier this was elided in some cases. IMO that was a bug all along. Follow-up patches will extend these new functions with stricter checks. It would have been good to add stricter validation for the "previously seen" case - pedit or BPF can be used to munge the skb to no longer reflect the nf_conn tuples. But it cannot easily be done: fragmented traffic is a valid corner case, and such skbs cannot be validated. Add a comment about this. Cc: stable@vger.kernel.org Signed-off-by: Florian Westphal --- include/net/netfilter/nf_conntrack_helper.h | 6 + net/netfilter/nf_conntrack_core.c | 38 +++---- net/netfilter/nf_conntrack_proto.c | 118 +++++++++++++++----- 3 files changed, 113 insertions(+), 49 deletions(-) diff --git a/include/net/netfilter/nf_conntrack_helper.h b/include/net/netfilter/nf_conntrack_helper.h index 335b8c43694f..676373fb1143 100644 --- a/include/net/netfilter/nf_conntrack_helper.h +++ b/include/net/netfilter/nf_conntrack_helper.h @@ -124,6 +124,12 @@ int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl, int nf_ct_helper(struct sk_buff *skb, struct nf_conn *ct, enum ip_conntrack_info ctinfo, u16 proto); +bool nf_confirm_get_protoff(struct sk_buff *skb, struct net *net, + struct nf_conn *ct, + enum ip_conntrack_info ctinfo, + unsigned int *protoffp); +int nf_ct_call_helper(struct sk_buff *skb, struct nf_conn *ct, + enum ip_conntrack_info ctinfo); int nf_ct_add_helper(struct nf_conn *ct, const char *name, u8 family, u8 proto, bool nat, struct nf_conntrack_helper **hp); diff --git a/net/netfilter/nf_conntrack_core.c b/net/netfilter/nf_conntrack_core.c index 931b76aacd93..5506c4ab1f26 100644 --- a/net/netfilter/nf_conntrack_core.c +++ b/net/netfilter/nf_conntrack_core.c @@ -2008,7 +2008,21 @@ nf_conntrack_in(struct sk_buff *skb, const struct nf_hook_state *state) tmpl = nf_ct_get(skb, &ctinfo); if (tmpl || ctinfo == IP_CT_UNTRACKED) { - /* Previously seen (loopback or untracked)? Ignore. */ + /* Previously seen (loopback or untracked)? Ignore. + * + * The attached ct is deliberately not revalidated against the + * packet headers. Non-first fragments (e.g. on loopback with a + * reduced MTU) carry the ct but have no L4 header to compare + * against. Rejecting them breaks such setups (they become + * 'ct state invalid'). Accepting them makes revalidation moot, + * since whoever can mangle headers (pedit/BPF) can just set a + * fragment bit. + * + * Users of an attached ct that depend on skb contents (helpers, + * seqadj, NAT) must therefore validate protocol and offsets + * themselves and bounds-check all accesses, e.g. via + * skb_header_pointer()/skb_ensure_writable(). + */ if ((tmpl && !nf_ct_is_template(tmpl)) || ctinfo == IP_CT_UNTRACKED) return NF_ACCEPT; @@ -2204,7 +2218,7 @@ static int nf_confirm_cthelper(struct sk_buff *skb, struct nf_conn *ct, const struct nf_conntrack_helper *helper; const struct nf_conn_help *help; unsigned int helper_flags; - int protoff; + unsigned int protoff; help = nfct_help(ct); if (!help) @@ -2218,26 +2232,8 @@ static int nf_confirm_cthelper(struct sk_buff *skb, struct nf_conn *ct, if (!(helper_flags & NF_CT_HELPER_F_USERSPACE)) return NF_ACCEPT; - switch (nf_ct_l3num(ct)) { - case NFPROTO_IPV4: - protoff = skb_network_offset(skb) + ip_hdrlen(skb); - break; -#if IS_ENABLED(CONFIG_IPV6) - case NFPROTO_IPV6: { - __be16 frag_off; - u8 pnum; - - pnum = ipv6_hdr(skb)->nexthdr; - protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &pnum, - &frag_off); - if (protoff < 0 || (frag_off & htons(~0x7)) != 0) - return NF_ACCEPT; - break; - } -#endif - default: + if (!nf_confirm_get_protoff(skb, nf_ct_net(ct), ct, ctinfo, &protoff)) return NF_ACCEPT; - } if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) && !nf_is_loopback_packet(skb)) { diff --git a/net/netfilter/nf_conntrack_proto.c b/net/netfilter/nf_conntrack_proto.c index 7a40e4e0e33e..9c74bce8bf25 100644 --- a/net/netfilter/nf_conntrack_proto.c +++ b/net/netfilter/nf_conntrack_proto.c @@ -131,36 +131,16 @@ static bool in_vrf_postrouting(const struct nf_hook_state *state) return false; } -unsigned int nf_confirm(void *priv, - struct sk_buff *skb, - const struct nf_hook_state *state) +bool nf_confirm_get_protoff(struct sk_buff *skb, struct net *net, + struct nf_conn *ct, + enum ip_conntrack_info ctinfo, + unsigned int *protoffp) { - int (*helper_cb)(struct sk_buff *skb, unsigned int protoff, - struct nf_conn *ct, - enum ip_conntrack_info conntrackinfo); - const struct nf_conn_help *help; - enum ip_conntrack_info ctinfo; unsigned int protoff; - struct nf_conn *ct; - bool seqadj_needed; __be16 frag_off; int start; u8 pnum; - ct = nf_ct_get(skb, &ctinfo); - if (!ct || in_vrf_postrouting(state)) - return NF_ACCEPT; - - help = nfct_help(ct); - - seqadj_needed = test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) && !nf_is_loopback_packet(skb); - if (!help && !seqadj_needed) - return nf_conntrack_confirm(skb); - - /* helper->help() do not expect ICMP packets */ - if (ctinfo == IP_CT_RELATED_REPLY) - return nf_conntrack_confirm(skb); - switch (nf_ct_l3num(ct)) { case NFPROTO_IPV4: protoff = skb_network_offset(skb) + ip_hdrlen(skb); @@ -169,16 +149,60 @@ unsigned int nf_confirm(void *priv, pnum = ipv6_hdr(skb)->nexthdr; start = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &pnum, &frag_off); if (start < 0 || (frag_off & htons(~0x7)) != 0) - return nf_conntrack_confirm(skb); + return false; protoff = start; break; default: - return nf_conntrack_confirm(skb); + DEBUG_NET_WARN_ONCE(1, "helper invoked on non-IP family!"); + return false; } + *protoffp = protoff; + return true; +} + +/** + * nf_ct_call_helper() - Invoke the connection tracking helper for a packet + * @skb: The socket buffer containing the packet to be processed. + * @ct: The connection tracking entry associated with this packet. + * @ctinfo: The current connection track information (state) of the packet. + * + * This function calls the l4 connection tracking helper (e.g. ftp, sip...) if + * one was assigned to the connection. + * + * It also performs TCP sequence number adjustment if the connection has + * IPS_SEQ_ADJUST set (e.g. after a helper or NAT changed the payload length). + * This may mangle the packet, or return NF_DROP if the adjustment fails. + * + * Return: verdict (NF_ACCEPT, NF_DROP, ...) + */ +int nf_ct_call_helper(struct sk_buff *skb, struct nf_conn *ct, + enum ip_conntrack_info ctinfo) +{ + struct net *net = nf_ct_net(ct); + const struct nf_conn_help *help; + bool seqadj_needed; + unsigned int protoff; + + help = nfct_help(ct); + + seqadj_needed = test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) && !nf_is_loopback_packet(skb); + if (!help && !seqadj_needed) + return NF_ACCEPT; + + /* helper->help() do not expect ICMP packets */ + if (ctinfo == IP_CT_RELATED_REPLY) + return NF_ACCEPT; + + if (!nf_confirm_get_protoff(skb, net, ct, ctinfo, &protoff)) + return NF_ACCEPT; + if (help) { const struct nf_conntrack_helper *helper; + int (*helper_cb)(struct sk_buff *skb, unsigned int protoff, + struct nf_conn *ct, + enum ip_conntrack_info conntrackinfo); int ret; /* rcu_read_lock()ed by nf_hook */ @@ -195,9 +219,47 @@ unsigned int nf_confirm(void *priv, } if (seqadj_needed && - !nf_ct_seq_adjust(skb, ct, ctinfo, protoff)) { - NF_CT_STAT_INC_ATOMIC(nf_ct_net(ct), drop); + !nf_ct_seq_adjust(skb, ct, ctinfo, protoff)) return NF_DROP; + + return NF_ACCEPT; +} + +/** + * nf_confirm - Confirm (commit) a connection tracking entry + * @priv: Private data from the netfilter hook registration (unused) + * @skb: Packet + * @state: Netfilter hook state containing context (netns, hooknum, in/out devices) + * + * This function is invoked as the final step of the connection tracking + * processing pipeline. It performs two main operations: + * + * 1. Invokes the associated conntrack helper (if registered) to allow + * layer 7 specific parsing and NAT mangling. This includes TCP + * sequence number adjustment if the connection requires it. + * + * 2. Commits a new conntrack entry to the conntrack hash table, making it + * globally visible and enabling future packet lookups for stateful tracking. + * + * Returns: A netfilter verdict, e.g. NF_ACCEPT or NF_DROP. + */ +unsigned int nf_confirm(void *priv, + struct sk_buff *skb, + const struct nf_hook_state *state) +{ + enum ip_conntrack_info ctinfo; + struct nf_conn *ct; + int ret; + + ct = nf_ct_get(skb, &ctinfo); + if (!ct || in_vrf_postrouting(state)) + return NF_ACCEPT; + + ret = nf_ct_call_helper(skb, ct, ctinfo); + if (unlikely(ret != NF_ACCEPT)) { + if (ret == NF_DROP) + NF_CT_STAT_INC_ATOMIC(state->net, drop); + return ret; } /* We've seen it coming out the other side: confirm it */ -- 2.56.1