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 4/6] netfilter: nfnetlink_log: use real mac header length for hwheader
Date: Wed, 18 Nov 2026 12:57:30 +0100	[thread overview]
Message-ID: <20261118115732.9529-5-fw@strlen.de> (raw)
In-Reply-To: <20261118115732.9529-1-fw@strlen.de>

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


  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 ` Florian Westphal [this message]
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

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-5-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.