All of lore.kernel.org
 help / color / mirror / Atom feed
From: Florian Westphal <fw@strlen.de>
To: <netfilter-devel@vger.kernel.org>
Cc: Florian Westphal <fw@strlen.de>
Subject: [PATCH nf-next v2 5/6] netfilter: nfnetlink: don't log hwaddr/hwheader on output hooks
Date: Wed, 18 Nov 2026 12:57:31 +0100	[thread overview]
Message-ID: <20261118115732.9529-6-fw@strlen.de> (raw)
In-Reply-To: <20261118115732.9529-1-fw@strlen.de>

On output hooks (LOCAL_OUT, POST_ROUTING and their per-family
equivalents) the mac header is either unset or still belongs to the
ingress device, not the egress one, so we would log stale data.  Only
emit the hardware header attributes while the packet is still on the
input path.

Same for the HWADDR attribute: it is parsed with the ingress device's
header_ops, but ip_output() and xfrm4_output() pass the pre-switch
skb->dev as indev while the packet may have been re-routed or
re-encapsulated, so the source address can be stale.

The check has to use both family and hook value, hook numbers are family
specific, e.g. NF_BR_BROUTING has the same value as NF_INET_INGRESS.

nfulnl_pre_egress(pf, hooknum) answers the question the check cares
about: has the packet left indev's input path yet, i.e. does skb's mac
header still belong to indev.

This keeps hwaddr/hwheader logging working for the nftables inet ingress
hook (nft_do_chain_inet_ingress() sets pf to NFPROTO_IPV4/IPV6 with hook
= NF_INET_INGRESS) and for the ebtables broute table (NF_BR_BROUTING,
indev and mac header are those of the received frame).  For NFPROTO_ARP
and NFPROTO_NETDEV only NF_ARP_IN and NF_NETDEV_INGRESS are accepted.

nfnetlink_queue uses the hook number only; its only supported in the
ipv4/ipv6/inet/bridge families.

Fixes: 72961ecf84d6 ("netfilter: nfnetlink_log: send complete hardware header")
Signed-off-by: Florian Westphal <fw@strlen.de>
---
 v2: restrict hwaddr too.

 net/netfilter/nfnetlink_log.c   | 43 +++++++++++++++++++++++++++++----
 net/netfilter/nfnetlink_queue.c |  3 ++-
 2 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/net/netfilter/nfnetlink_log.c b/net/netfilter/nfnetlink_log.c
index b54fbfe56795..5645297b4b3b 100644
--- a/net/netfilter/nfnetlink_log.c
+++ b/net/netfilter/nfnetlink_log.c
@@ -21,6 +21,7 @@
 #include <linux/netdevice.h>
 #include <linux/netfilter.h>
 #include <linux/netfilter_bridge.h>
+#include <linux/netfilter_arp.h>
 #include <net/netlink.h>
 #include <linux/netfilter/nfnetlink.h>
 #include <linux/netfilter/nfnetlink_log.h>
@@ -458,6 +459,36 @@ static int nfulnl_put_bridge(struct nfulnl_instance *inst, const struct sk_buff
 	return -1;
 }
 
+/**
+ * nfulnl_pre_egress - Can @skb's mac header still be attributed to @indev?
+ * @pf: protocol family of the hook this packet is being logged from.
+ * @hooknum: hook number within @pf's family.
+ *
+ * True only for hook points where the packet has not yet left @indev's
+ * input path, i.e. where @skb's mac header still reflects what @indev
+ * actually received. Past LOCAL_OUT/POST_ROUTING (and their per-family
+ * equivalents) the packet may have been re-routed, re-encapsulated, or
+ * is headed to a different device than it arrived on.
+ *
+ * Return: true if mac header can be attributed to indev
+ */
+static bool nfulnl_pre_egress(u8 pf, unsigned int hooknum)
+{
+	switch (pf) {
+	case NFPROTO_IPV4:
+	case NFPROTO_IPV6:
+		return hooknum <= NF_INET_FORWARD || hooknum == NF_INET_INGRESS;
+	case NFPROTO_BRIDGE:
+		return hooknum <= NF_BR_FORWARD || hooknum == NF_BR_BROUTING;
+	case NFPROTO_ARP:
+		return hooknum == NF_ARP_IN;
+	case NFPROTO_NETDEV:
+		return hooknum == NF_NETDEV_INGRESS;
+	default:
+		return false;
+	}
+}
+
 /**
  * nfulnl_put_hwheader - Append link-layer header info to the netlink skb.
  * @nlskb:      Target netlink message to append attributes to.
@@ -647,12 +678,14 @@ __build_packet_message(struct nfnl_log_net *log,
 	    nla_put_be32(inst->skb, NFULA_MARK, htonl(skb->mark)))
 		goto nla_put_failure;
 
-	if (nfnetlink_put_hwaddr(inst->skb, skb, indev, NFULA_HWADDR))
-		goto nla_put_failure;
+	if (nfulnl_pre_egress(pf, hooknum)) {
+		if (nfnetlink_put_hwaddr(inst->skb, skb, indev, NFULA_HWADDR))
+			goto nla_put_failure;
 
-	if (indev && skb_mac_header_was_set(skb) &&
-	    nfulnl_put_hwheader(inst->skb, skb, indev))
-		goto nla_put_failure;
+		if (indev && skb_mac_header_was_set(skb) &&
+		    nfulnl_put_hwheader(inst->skb, skb, indev))
+			goto nla_put_failure;
+	}
 
 	if (hooknum <= NF_INET_FORWARD) {
 		struct timespec64 kts = ktime_to_timespec64(skb_tstamp_cond(skb, true));
diff --git a/net/netfilter/nfnetlink_queue.c b/net/netfilter/nfnetlink_queue.c
index 37f8953b7c49..c1afeb0c3e2a 100644
--- a/net/netfilter/nfnetlink_queue.c
+++ b/net/netfilter/nfnetlink_queue.c
@@ -919,7 +919,8 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
 	    nla_put_be32(skb, NFQA_PRIORITY, htonl(entskb->priority)))
 		goto nla_put_failure;
 
-	if (nfnetlink_put_hwaddr(skb, entskb, indev, NFQA_HWADDR))
+	if (entry->state.hook <= NF_INET_FORWARD &&
+	    nfnetlink_put_hwaddr(skb, entskb, indev, NFQA_HWADDR))
 		goto nla_put_failure;
 
 	if (nfqnl_put_bridge(entry, skb) < 0)
-- 
2.56.1


  parent reply	other threads:[~2026-11-18 11:58 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 11:57 [PATCH nf-next v2 0/6] netfilter: logging cleanups Florian Westphal
2026-11-18 11:57 ` [PATCH nf-next v2 1/6] netfilter: nf_log: remove dead ARPHRD_SIT handling from MAC/HW header logging Florian Westphal
2026-11-18 11:57 ` [PATCH nf-next v2 2/6] netfilter: nf_log_syslog: fix MAC header dump to use real header length Florian Westphal
2026-11-18 11:57 ` [PATCH nf-next v2 3/6] netfilter: nfnetlink_log: fix up dev_parse_header usage Florian Westphal
2026-11-18 11:57 ` [PATCH nf-next v2 4/6] netfilter: nfnetlink_log: use real mac header length for hwheader Florian Westphal
2026-11-18 11:57 ` Florian Westphal [this message]
2026-11-18 11:57 ` [PATCH nf-next v2 6/6] netfilter: nfnetlink_log: only add timestamp on the input path Florian Westphal

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=20261118115732.9529-6-fw@strlen.de \
    --to=fw@strlen.de \
    --cc=netfilter-devel@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.