All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH nf-next v2 0/6] netfilter: logging cleanups
@ 2026-11-18 11:57 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
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Florian Westphal @ 2026-11-18 11:57 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Florian Westphal

v2: LLM cargo-culting and more paranoia checks.
Couldn't find any place where skb->dev == NULL && indev != NULL, but
I consider logging slowpath so I just gave up and had LLM blindly
follow AI/sashiko "advice".

See patches for details.

1) Remove dead ARPHRD_SIT special-casing from MAC/HW header logging in
nf_log_syslog and nfnetlink_log. The branch became unreachable once SIT
tunnel hard_header_len was zeroed.

2) Fix MAC header dump in nf_log_syslog to use skb_mac_header_len() clamped
to dev->hard_header_len instead of unconditionally logging hard_header_len
bytes, preventing reads into the IP/IPv6 header.

3) Refactor hw address extraction into a shared helper for nfnetlink_log and
nfnetlink_queue. Clamp dev_parse_header() result to 8 bytes and validate
skb preconditions in the common helper. Add BUILD_BUG_ON assertions for
identical container struct layouts.

4) Use skb_mac_header_len() from indev instead of hard_header_len from
skb->dev for NFULA_HWHEADER length. Omit the NFULA_HWHEADER attribute when
the computed length is zero.

5) Skip hwheader logging on output hooks where the MAC header is stale. Gate
emission with a family-and-hook aware nfulnl_pre_egress() check.

6) Emit the nfnetlink_log timestamp only on the input path, since output-path
skb->tstamp may carry an SO_TXTIME delivery deadline rather than an
observation time. Replace the broken raw hooknum comparison with
nfulnl_pre_egress() so ARP and NETDEV families no longer emit timestamps
at their output hooks.

Florian Westphal (6):
  netfilter: nf_log: remove dead ARPHRD_SIT handling from MAC/HW header logging
  netfilter: nf_log_syslog: fix MAC header dump to use real header length
  netfilter: nfnetlink_log: fix up dev_parse_header usage
  netfilter: nfnetlink_log: use real mac header length for hwheader
  netfilter: nfnetlink: don't log hwaddr/hwheader on output hooks
  netfilter: nfnetlink_log: only add timestamp on the input path

 net/netfilter/nf_log_syslog.c        |  28 ++---
 net/netfilter/nfnetlink_log.c        | 151 ++++++++++++++++++++-------
 net/netfilter/nfnetlink_log_common.h | 117 +++++++++++++++++++++
 net/netfilter/nfnetlink_queue.c      |  19 +---
 4 files changed, 244 insertions(+), 71 deletions(-)
 create mode 100644 net/netfilter/nfnetlink_log_common.h

-- 
2.56.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH nf-next v2 1/6] netfilter: nf_log: remove dead ARPHRD_SIT handling from MAC/HW header logging
  2026-11-18 11:57 [PATCH nf-next v2 0/6] netfilter: logging cleanups Florian Westphal
@ 2026-11-18 11:57 ` 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
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Florian Westphal @ 2026-11-18 11:57 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Florian Westphal

dump_mac_header() in nf_log_syslog.c and the HWHEADER dump in
nfnetlink_log.c special-case ARPHRD_SIT devices by backing the header
pointer up by ETH_HLEN before dumping dev->hard_header_len bytes, on the
assumption that a SIT tunnel's "hard header" covers the underlying
device's real Ethernet header plus the tunnel's IPv4 header.

That assumption stopped holding after commit 8ef9ba4d6666 ("IPv6: Set
SIT tunnel hard_header_len to zero"), which zeroed dev->hard_header_len
for SIT tunnels.

The syslog call site was gated on dev->hard_header_len being nonzero,
so the ARPHRD_SIT branch there has been unreachable dead code for every
SIT device ever since.

For nfnetlink_log the branch was still reachable, but the adjusted
pointer is never read: nla_put() for the NFULA_HWHEADER attribute uses
dev->hard_header_len as the length, which is 0 for SIT tunnels.

Remove the ARPHRD_SIT special-handling; it's been 6 years and no one
reported this ever since.

Also use the ingress device for the netlink message size estimate of
the hardware header attributes: skb->dev can have been repointed to a
device with a different hard_header_len before the hook runs (multicast
routing does so before the FORWARD hook), and it is dereferenced
without a NULL check.

Signed-off-by: Florian Westphal <fw@strlen.de>
---
 v2: use  nla_total_size(in->hard_header_len), not
 skb->dev.

 net/netfilter/nf_log_syslog.c | 17 +----------------
 net/netfilter/nfnetlink_log.c |  8 ++------
 2 files changed, 3 insertions(+), 22 deletions(-)

diff --git a/net/netfilter/nf_log_syslog.c b/net/netfilter/nf_log_syslog.c
index c3fd398ffcd7..910f6975cdf7 100644
--- a/net/netfilter/nf_log_syslog.c
+++ b/net/netfilter/nf_log_syslog.c
@@ -820,26 +820,11 @@ static void dump_mac_header(struct nf_log_buf *m,
 		const unsigned char *p = skb_mac_header(skb);
 		unsigned int i;
 
-		if (dev->type == ARPHRD_SIT) {
-			p -= ETH_HLEN;
-
-			if (p < skb->head)
-				p = NULL;
-		}
-
-		if (p) {
+		if (1) {
 			nf_log_buf_add(m, "%02x", *p++);
 			for (i = 1; i < dev->hard_header_len; i++)
 				nf_log_buf_add(m, ":%02x", *p++);
 		}
-
-		if (dev->type == ARPHRD_SIT) {
-			const struct iphdr *iph =
-				(struct iphdr *)skb_mac_header(skb);
-
-			nf_log_buf_add(m, " TUNNEL=%pI4->%pI4", &iph->saddr,
-				       &iph->daddr);
-		}
 	}
 	nf_log_buf_add(m, " ");
 }
diff --git a/net/netfilter/nfnetlink_log.c b/net/netfilter/nfnetlink_log.c
index d923f2cb1398..90e87c34df85 100644
--- a/net/netfilter/nfnetlink_log.c
+++ b/net/netfilter/nfnetlink_log.c
@@ -610,11 +610,7 @@ __build_packet_message(struct nfnl_log_net *log,
 
 		hwhdrp = skb_mac_header(skb);
 
-		if (skb->dev->type == ARPHRD_SIT)
-			hwhdrp -= ETH_HLEN;
-
-		if (hwhdrp >= skb->head &&
-		    nla_put(inst->skb, NFULA_HWHEADER,
+		if (nla_put(inst->skb, NFULA_HWHEADER,
 			    skb->dev->hard_header_len, hwhdrp))
 			goto nla_put_failure;
 	}
@@ -772,7 +768,7 @@ nfulnl_log_packet(struct net *net,
 		+ nlmsg_total_size(sizeof(struct nfgenmsg));	/* NLMSG_DONE */
 
 	if (in && skb_mac_header_was_set(skb)) {
-		size += nla_total_size(skb->dev->hard_header_len)
+		size += nla_total_size(in->hard_header_len)
 			+ nla_total_size(sizeof(u_int16_t))	/* hwtype */
 			+ nla_total_size(sizeof(u_int16_t));	/* hwlen */
 	}
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH nf-next v2 2/6] netfilter: nf_log_syslog: fix MAC header dump to use real header length
  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 ` Florian Westphal
  2026-11-18 11:57 ` [PATCH nf-next v2 3/6] netfilter: nfnetlink_log: fix up dev_parse_header usage Florian Westphal
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Florian Westphal @ 2026-11-18 11:57 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Florian Westphal, Fernando Fernandez Mancera

old code unconditionally iterated dev->hard_header_len bytes, assuming
the SKB's MAC header is always exactly that long.

If the actual mac header in the packet is smaller, the loop reads past
the MAC and logs ip/ipv6 header as if it was part of the address.

Use skb_mac_header_len() as the base length, then clamp the result to
dev->hard_header_len so we never read beyond what the
device actually uses for its hard header, even if mac header was
mangled.

LLM finding, no bug was ever reported about this.

Fixes: 7eb9282cd0ef ("netfilter: ipt_LOG/ip6t_LOG: add option to print decoded MAC header")
Reviewed-by: Fernando Fernandez Mancera <fmancera@suse.de>
Signed-off-by: Florian Westphal <fw@strlen.de>
---
 no changes.

 net/netfilter/nf_log_syslog.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/net/netfilter/nf_log_syslog.c b/net/netfilter/nf_log_syslog.c
index 910f6975cdf7..5f7435caff22 100644
--- a/net/netfilter/nf_log_syslog.c
+++ b/net/netfilter/nf_log_syslog.c
@@ -815,14 +815,17 @@ static void dump_mac_header(struct nf_log_buf *m,
 
 fallback:
 	nf_log_buf_add(m, "MAC=");
-	if (dev->hard_header_len && skb_mac_header_was_set(skb) &&
-	    skb_mac_header_len(skb) != 0) {
-		const unsigned char *p = skb_mac_header(skb);
-		unsigned int i;
+	if (dev->hard_header_len && skb_mac_header_was_set(skb)) {
+		unsigned int mac_len;
+
+		mac_len = min_t(unsigned int, skb_mac_header_len(skb),
+				dev->hard_header_len);
+		if (mac_len) {
+			const unsigned char *p = skb_mac_header(skb);
+			unsigned int i;
 
-		if (1) {
 			nf_log_buf_add(m, "%02x", *p++);
-			for (i = 1; i < dev->hard_header_len; i++)
+			for (i = 1; i < mac_len; i++)
 				nf_log_buf_add(m, ":%02x", *p++);
 		}
 	}
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH nf-next v2 3/6] netfilter: nfnetlink_log: fix up dev_parse_header usage
  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 ` 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
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Florian Westphal @ 2026-11-18 11:57 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Florian Westphal

dev_parse_header() grabs the source hw address from the skb and stores
it in the given buffer.

e.g.: eth_header_parse() is a simple:

int eth_header_parse(const struct sk_buff *skb,
		     const struct net_device *dev, unsigned char *haddr) {
	memcpy(haddr, eth_hdr(skb)->h_source, ETH_ALEN);
	return ETH_ALEN;
}

Problem is that the compound structure used by the UABI in nfnetlink_log
and nfnetlink_queue has a fixed 8-byte size.

Enough for ETH_ALEN, but not necessarily enough for other address types.
Move this to a helper and allow sharing it between the copypasted users.

The struct layout of the two container structures have same layout:
add BUILD_BUG_ON checks to assert this.

Due to the nature of the dev_parse_header() helpers:

1. skb MUST have a valid mac header, else we read garbage
2. users MUST NOT use this when ingress device and skb->dev
have diverged (we can't know we would decode something
meaningful anymore).
3. Because container structure only has space for 8 octets,
   we have to clamp the result.

Do all of this in the common helper.

The helper also rejects skbs whose network header ended up below the
mac header: encapsulation (ip_encap(), xfrm) can do this and the
unsigned skb_mac_header_len() would then wrap to a huge value,
bypassing the sanity checks.  The mac header must also span the
device's full hard_header_len, not just min_header_len:
header_ops->parse() implementations read fixed offsets sized for the
complete header format (ipgre leaves min_header_len at 0 but
ipgre_header_parse() reads offset 12..15 of the mac header).

This is an LLM finding, no bug was ever reported about this.

Fixes: 0597f2680d66 ("[NETFILTER]: Add new "nfnetlink_log" userspace packet logging facility")
Fixes: 7af4cc3fa158 ("[NETFILTER]: Add "nfnetlink_queue" netfilter queue handler over nfnetlink")
Signed-off-by: Florian Westphal <fw@strlen.de>
---
 v2: more paranoia checks that AI insists are needed
 (ip_encap, ipgre, ...)

 net/netfilter/nfnetlink_log.c        |  18 +----
 net/netfilter/nfnetlink_log_common.h | 117 +++++++++++++++++++++++++++
 net/netfilter/nfnetlink_queue.c      |  18 +----
 3 files changed, 125 insertions(+), 28 deletions(-)
 create mode 100644 net/netfilter/nfnetlink_log_common.h

diff --git a/net/netfilter/nfnetlink_log.c b/net/netfilter/nfnetlink_log.c
index 90e87c34df85..37f3e7b6b538 100644
--- a/net/netfilter/nfnetlink_log.c
+++ b/net/netfilter/nfnetlink_log.c
@@ -47,6 +47,8 @@
 #include <net/netfilter/nf_conntrack.h>
 #endif
 
+#include "nfnetlink_log_common.h"
+
 #define NFULNL_COPY_DISABLED	0xff
 #define NFULNL_NLBUFSIZ_DEFAULT	NLMSG_GOODSIZE
 #define NFULNL_TIMEOUT_DEFAULT 	100	/* every second */
@@ -587,20 +589,8 @@ __build_packet_message(struct nfnl_log_net *log,
 	    nla_put_be32(inst->skb, NFULA_MARK, htonl(skb->mark)))
 		goto nla_put_failure;
 
-	if (indev && skb->dev &&
-	    skb_mac_header_was_set(skb) &&
-	    skb_mac_header_len(skb) != 0) {
-		struct nfulnl_msg_packet_hw phw;
-		int len;
-
-		memset(&phw, 0, sizeof(phw));
-		len = dev_parse_header(skb, phw.hw_addr);
-		if (len > 0) {
-			phw.hw_addrlen = htons(len);
-			if (nla_put(inst->skb, NFULA_HWADDR, sizeof(phw), &phw))
-				goto nla_put_failure;
-		}
-	}
+	if (nfnetlink_put_hwaddr(inst->skb, skb, indev, NFULA_HWADDR))
+		goto nla_put_failure;
 
 	if (indev && skb_mac_header_was_set(skb)) {
 		if (nla_put_be16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type)) ||
diff --git a/net/netfilter/nfnetlink_log_common.h b/net/netfilter/nfnetlink_log_common.h
new file mode 100644
index 000000000000..99012543b32f
--- /dev/null
+++ b/net/netfilter/nfnetlink_log_common.h
@@ -0,0 +1,117 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _NF_NETLINK_LOG_COMMON_H
+#define _NF_NETLINK_LOG_COMMON_H
+
+#include <linux/netdevice.h>
+#include <linux/skbuff.h>
+#include <net/netlink.h>
+#include <linux/netfilter/nfnetlink_queue.h>
+#include <linux/netfilter/nfnetlink_log.h>
+
+/* nfnetlink_log and nfnetlink_queue suffer from copypaste; avoid adding
+ * more and place shared code here.
+ */
+static __always_inline void assert_nfqnl_nfunl_hdrs(void)
+{
+	struct nfulnl_msg_packet_hw phwu;
+	struct nfqnl_msg_packet_hw phwq;
+
+	BUILD_BUG_ON(sizeof(phwu) != sizeof(phwq));
+
+#define X(memb) offsetof(struct nfulnl_msg_packet_hw, memb) != \
+		offsetof(struct nfqnl_msg_packet_hw, memb)
+
+	BUILD_BUG_ON(X(hw_addrlen));
+	BUILD_BUG_ON(X(hw_addr));
+#undef X
+#define X(memb) sizeof(phwu.memb) != sizeof(phwq.memb)
+	BUILD_BUG_ON(X(hw_addrlen));
+#undef X
+}
+
+/**
+ * nfnetlink_put_hwaddr - Serialize L2 hardware address into netlink attr
+ *
+ * @nlskb: netlink message to append the attribute to.
+ * @skb: the packet whose hardware address is being serialized.
+ * @indev: the input device that received @skb.
+ * @attr: netlink attribute type identifier.
+ *
+ * Extracts the MAC header from @skb using @indev's header_ops and appends
+ * it as a netlink attribute of type @attr to @nlskb.
+ *
+ * UAPI NOTE: The attribute is serialized in a legacy container structure
+ * (nfulnl_msg_packet_hw / nfqnl_msg_packet_hw) rather than a plain
+ * nla_put(hwaddr, len).
+ * The hw_addrlen member in that structure is redundant -- userspace knows
+ * the size from the netlink attribute length -- but is retained for ABI
+ * compatibility. nfnetlink_queue duplicates this layout; both structs are
+ * layout-identical, so this one helper serves both.
+ *
+ * The maximum address length is 8 octets, even though netlink itself
+ * supports longer addresses. This is a limitation of the
+ * compound-structure ABI (hw_addr[8] member).
+ *
+ * This helper checks:
+ *  - @indev is non-NULL and implements header_ops->parse.
+ *  - network_header is not below mac_header (encapsulation), so
+ *    skb_mac_header_len() does not wrap.
+ *  - L2 header is present and at least @indev's hard_header_len, so
+ *    header_ops->parse does not read past a short or missing header.
+ *
+ * @indev, not @skb->dev, is used: @skb->dev may have been changed since
+ * the packet was received, so it is not necessarily the device whose
+ * header_ops can parse the L2 header.
+ *
+ * Return: 0 on success or if the attribute is skipped (no parser, no or
+ * short L2 header, or empty address), -EMSGSIZE if the tailroom of the
+ * skb is insufficient to store the attribute header and payload.
+ */
+static inline int
+nfnetlink_put_hwaddr(struct sk_buff *nlskb,
+		     const struct sk_buff *skb,
+		     const struct net_device *indev,
+		     u16 attr)
+{
+	char hw_addr[MAX_ADDR_LEN];
+	unsigned int mac_len;
+	int len;
+
+	if (!indev || !indev->header_ops || !indev->header_ops->parse ||
+	    !skb_mac_header_was_set(skb))
+		return 0;
+
+	/* encap (ip_encap, xfrm) can set network_header < mac_header:
+	 * skb_mac_header_len() would wrap and bypass the checks below.
+	 */
+	if (skb->network_header < skb->mac_header)
+		return 0;
+
+	mac_len = skb_mac_header_len(skb);
+	/* parse() reads fixed offsets sized for the full header
+	 * (ipgre: min_header_len 0, reads offset 12..15).
+	 */
+	if (mac_len == 0 || mac_len < indev->hard_header_len)
+		return 0;
+
+	len = indev->header_ops->parse(skb, indev, hw_addr);
+	if (len > 0) {
+		struct nfulnl_msg_packet_hw phw;
+		const int max_hwlen = (int)sizeof(phw.hw_addr);
+
+		assert_nfqnl_nfunl_hdrs();
+
+		/* UAPI limitation, must clamp. */
+		if (len > max_hwlen)
+			len = max_hwlen;
+
+		memset(&phw, 0, sizeof(phw));
+		memcpy(phw.hw_addr, hw_addr, len);
+		phw.hw_addrlen = htons(len);
+
+		return nla_put(nlskb, attr, sizeof(phw), &phw);
+	}
+
+	return 0;
+}
+#endif
diff --git a/net/netfilter/nfnetlink_queue.c b/net/netfilter/nfnetlink_queue.c
index 862d2a762106..37f8953b7c49 100644
--- a/net/netfilter/nfnetlink_queue.c
+++ b/net/netfilter/nfnetlink_queue.c
@@ -57,6 +57,8 @@
 #include <net/netfilter/nf_conntrack.h>
 #endif
 
+#include "nfnetlink_log_common.h"
+
 #define NFQNL_QMAX_DEFAULT 1024
 #define NFQNL_HASH_MIN     8
 #define NFQNL_HASH_MAX     32768
@@ -917,20 +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 (indev && entskb->dev &&
-	    skb_mac_header_was_set(entskb) &&
-	    skb_mac_header_len(entskb) != 0) {
-		struct nfqnl_msg_packet_hw phw;
-		int len;
-
-		memset(&phw, 0, sizeof(phw));
-		len = dev_parse_header(entskb, phw.hw_addr);
-		if (len) {
-			phw.hw_addrlen = htons(len);
-			if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw))
-				goto nla_put_failure;
-		}
-	}
+	if (nfnetlink_put_hwaddr(skb, entskb, indev, NFQA_HWADDR))
+		goto nla_put_failure;
 
 	if (nfqnl_put_bridge(entry, skb) < 0)
 		goto nla_put_failure;
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH nf-next v2 4/6] netfilter: nfnetlink_log: use real mac header length for hwheader
  2026-11-18 11:57 [PATCH nf-next v2 0/6] netfilter: logging cleanups Florian Westphal
                   ` (2 preceding siblings ...)
  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 ` Florian Westphal
  2026-11-18 11:57 ` [PATCH nf-next v2 5/6] netfilter: nfnetlink: don't log hwaddr/hwheader on output hooks Florian Westphal
  2026-11-18 11:57 ` [PATCH nf-next v2 6/6] netfilter: nfnetlink_log: only add timestamp on the input path Florian Westphal
  5 siblings, 0 replies; 7+ messages in thread
From: Florian Westphal @ 2026-11-18 11:57 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Florian Westphal

nfnetlink_log uses skb->dev->hard_header_len for the length of the
NFULA_HWHEADER attribute.

hard_header_len says how long a link layer header can get, not how many
bytes this packet holds.  Use skb_mac_header_len() instead and cap at
hard_header_len, as before, if needed.

Also take hwtype and hard_header_len from indev instead of skb->dev:
skb->dev may have been changed since the packet was received, indev is
the device that wrote the mac header.

If the resulting length is 0, omit NFULA_HWHEADER instead of sending an
empty attribute.  NFULA_HWLEN is still sent, userspace relies on it.

I suspect that this is almost exclusively used with plain ethernet,
where hard_header_len and skb_mac_header_len() are both 14 bytes, which
would explain the absence of bug reports.

Fixes: 72961ecf84d6 ("netfilter: nfnetlink_log: send complete hardware header")
Signed-off-by: Florian Westphal <fw@strlen.de>
---
 net/netfilter/nfnetlink_log.c | 75 +++++++++++++++++++++++++++++------
 1 file changed, 62 insertions(+), 13 deletions(-)

diff --git a/net/netfilter/nfnetlink_log.c b/net/netfilter/nfnetlink_log.c
index 37f3e7b6b538..b54fbfe56795 100644
--- a/net/netfilter/nfnetlink_log.c
+++ b/net/netfilter/nfnetlink_log.c
@@ -458,6 +458,65 @@ static int nfulnl_put_bridge(struct nfulnl_instance *inst, const struct sk_buff
 	return -1;
 }
 
+/**
+ * nfulnl_put_hwheader - Append link-layer header info to the netlink skb.
+ * @nlskb:      Target netlink message to append attributes to.
+ * @skb:        Original packet skb whose link-layer header is described.
+ * @indev:      The input device that received @skb.
+ *
+ * Emits the NFULA_HWTYPE and NFULA_HWLEN netlink attributes into @nlskb
+ * based on the link-layer header of @skb.
+ *
+ * @indev, not @skb->dev, is the source of the device's type and
+ * hard_header_len: on bridging and br_netfilter paths skb->dev is
+ * repointed to the logical or egress device while @indev keeps
+ * referring to the device that actually wrote @skb's mac header.
+ *
+ * The device's hard_header_len describes the maximum size a link-layer
+ * header can grow to, not the actual size of this packet's header.
+ * The actual length is taken from skb_mac_header_len() and capped at
+ * hard_header_len if needed.
+ *
+ * The NFULA_HWLEN attribute is retained for backward compatibility.
+ * Userspace should derive the length from the NFULA_HWHEADER attribute
+ * size instead.
+ *
+ * Caller must ensure @indev is not NULL and check
+ * skb_mac_header_was_set(skb) before using this function.
+ *
+ * Return: -EMSGSIZE if the tailroom of the skb is insufficient to store
+ * the attribute header and payload.
+ */
+static int nfulnl_put_hwheader(struct sk_buff *nlskb, const struct sk_buff *skb,
+			       const struct net_device *indev)
+{
+	unsigned int hwlen = READ_ONCE(indev->hard_header_len);
+	const unsigned char *hwhdrp = skb_mac_header(skb);
+	int len = skb_mac_header_len(skb);
+	u16 hwtype = indev->type;
+
+	if (nla_put_be16(nlskb, NFULA_HWTYPE, htons(hwtype)))
+		return -EMSGSIZE;
+
+	if (len < 0)
+		len = 0;
+	else if ((unsigned int)len > hwlen)
+		len = hwlen;
+
+	/* This attribute should not exist, userspace should
+	 * use NFULA_HWHEADER attribute size. Removing
+	 * this breaks existing users which unfortunately
+	 * rely on NFULA_HWLEN presence.
+	 */
+	if (nla_put_be16(nlskb, NFULA_HWLEN, htons(len)))
+		return -EMSGSIZE;
+
+	if (len == 0)
+		return 0;
+
+	return nla_put(nlskb, NFULA_HWHEADER, len, hwhdrp);
+}
+
 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
 static int nflog_put_master_ifindex(struct sk_buff *nlskb, int attr,
 				    const struct net_device *dev)
@@ -494,7 +553,6 @@ __build_packet_message(struct nfnl_log_net *log,
 	struct nlmsghdr *nlh;
 	sk_buff_data_t old_tail = inst->skb->tail;
 	struct sock *sk;
-	const unsigned char *hwhdrp;
 
 	nlh = nfnl_msg_put(inst->skb, 0, 0,
 			   nfnl_msg_type(NFNL_SUBSYS_ULOG, NFULNL_MSG_PACKET),
@@ -592,18 +650,9 @@ __build_packet_message(struct nfnl_log_net *log,
 	if (nfnetlink_put_hwaddr(inst->skb, skb, indev, NFULA_HWADDR))
 		goto nla_put_failure;
 
-	if (indev && skb_mac_header_was_set(skb)) {
-		if (nla_put_be16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type)) ||
-		    nla_put_be16(inst->skb, NFULA_HWLEN,
-				 htons(skb->dev->hard_header_len)))
-			goto nla_put_failure;
-
-		hwhdrp = skb_mac_header(skb);
-
-		if (nla_put(inst->skb, NFULA_HWHEADER,
-			    skb->dev->hard_header_len, hwhdrp))
-			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));
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH nf-next v2 5/6] netfilter: nfnetlink: don't log hwaddr/hwheader on output hooks
  2026-11-18 11:57 [PATCH nf-next v2 0/6] netfilter: logging cleanups Florian Westphal
                   ` (3 preceding siblings ...)
  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
  2026-11-18 11:57 ` [PATCH nf-next v2 6/6] netfilter: nfnetlink_log: only add timestamp on the input path Florian Westphal
  5 siblings, 0 replies; 7+ messages in thread
From: Florian Westphal @ 2026-11-18 11:57 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Florian Westphal

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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH nf-next v2 6/6] netfilter: nfnetlink_log: only add timestamp on the input path
  2026-11-18 11:57 [PATCH nf-next v2 0/6] netfilter: logging cleanups Florian Westphal
                   ` (4 preceding siblings ...)
  2026-11-18 11:57 ` [PATCH nf-next v2 5/6] netfilter: nfnetlink: don't log hwaddr/hwheader on output hooks Florian Westphal
@ 2026-11-18 11:57 ` Florian Westphal
  5 siblings, 0 replies; 7+ messages in thread
From: Florian Westphal @ 2026-11-18 11:57 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Florian Westphal

The timestamp attribute should only be emitted while the packet is on
the input path, like the hardware header attributes.  On output hooks
skb->tstamp can hold the SO_TXTIME transmit time, a delivery deadline
rather than the time the packet was observed.

The old check compared the raw hooknum against NF_INET_FORWARD, which is
meaningless for families with their own hook numbering: it was too
liberal for NFPROTO_ARP and NFPROTO_NETDEV.  Use nfulnl_pre_egress()
instead, so those no longer emit a timestamp at their output hooks.

Signed-off-by: Florian Westphal <fw@strlen.de>
---
 v2: adapt to changes in earlier patches, no changes per se.

 net/netfilter/nfnetlink_log.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/net/netfilter/nfnetlink_log.c b/net/netfilter/nfnetlink_log.c
index 5645297b4b3b..697ca7fb1dba 100644
--- a/net/netfilter/nfnetlink_log.c
+++ b/net/netfilter/nfnetlink_log.c
@@ -548,6 +548,22 @@ static int nfulnl_put_hwheader(struct sk_buff *nlskb, const struct sk_buff *skb,
 	return nla_put(nlskb, NFULA_HWHEADER, len, hwhdrp);
 }
 
+/* The timestamp is the time the packet was observed at the hook.
+ * Only called while the packet is on the input path: on output
+ * hooks skb->tstamp can hold the SO_TXTIME transmit time, which
+ * is a delivery deadline, not an observation time.
+ */
+static int nfulnl_put_timestamp(struct sk_buff *nlskb, const struct sk_buff *skb)
+{
+	struct timespec64 kts = ktime_to_timespec64(skb_tstamp_cond(skb, true));
+	struct nfulnl_msg_packet_timestamp ts = {
+		.sec = cpu_to_be64(kts.tv_sec),
+		.usec = cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC),
+	};
+
+	return nla_put(nlskb, NFULA_TIMESTAMP, sizeof(ts), &ts);
+}
+
 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
 static int nflog_put_master_ifindex(struct sk_buff *nlskb, int attr,
 				    const struct net_device *dev)
@@ -685,15 +701,8 @@ __build_packet_message(struct nfnl_log_net *log,
 		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));
-		struct nfulnl_msg_packet_timestamp ts;
-		ts.sec = cpu_to_be64(kts.tv_sec);
-		ts.usec = cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC);
 
-		if (nla_put(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts))
+		if (nfulnl_put_timestamp(inst->skb, skb))
 			goto nla_put_failure;
 	}
 
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-18 11:58 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH nf-next v2 5/6] netfilter: nfnetlink: don't log hwaddr/hwheader on output hooks Florian Westphal
2026-11-18 11:57 ` [PATCH nf-next v2 6/6] netfilter: nfnetlink_log: only add timestamp on the input path Florian Westphal

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.