Skip to contentSkip to conversation

netfilter: logging cleanups

PATCH Superseded Florian Westphal 1 participant 7 messages Nov 18

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

v2 cover netfilter: logging cleanups

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
Signature · 1 line
2.56.1
author maintainer
#1 lore raw

PATCH v2 1/6 netfilter: nf_log: remove dead ARPHRD_SIT handling from MAC/HW header logging

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.

---
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(-)
net/netfilter/nf_log_syslog.c +1 -16
index c3fd398ffcd7..910f6975cdf7 100644
@@ -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, " ");
 }
net/netfilter/nfnetlink_log.c +2 -6
index d923f2cb1398..90e87c34df85 100644
@@ -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 */
 	}
Signature · 1 line
2.56.1
author maintainer
#2 lore raw

PATCH v2 2/6 netfilter: nf_log_syslog: fix MAC header dump to use real header length

In reply to Florian Westphal

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")

---
no changes.

 net/netfilter/nf_log_syslog.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
net/netfilter/nf_log_syslog.c +9 -6
index 910f6975cdf7..5f7435caff22 100644
@@ -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++);
 		}
 	}
Signature · 1 line
2.56.1
author maintainer
#3 lore raw

PATCH v2 3/6 netfilter: nfnetlink_log: fix up dev_parse_header usage

In reply to 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")

---
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
net/netfilter/nfnetlink_log.c +4 -14
index 90e87c34df85..37f3e7b6b538 100644
@@ -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)) ||
net/netfilter/nfnetlink_log_common.h new file +117 -0

117 lines not on this page · raw message

net/netfilter/nfnetlink_queue.c +4 -14
index 862d2a762106..37f8953b7c49 100644
@@ -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;
Signature · 1 line
2.56.1
author maintainer
#4 lore raw

PATCH v2 4/6 netfilter: nfnetlink_log: use real mac header length for hwheader

In reply to 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")
---
 net/netfilter/nfnetlink_log.c | 75 +++++++++++++++++++++++++++++------
 1 file changed, 62 insertions(+), 13 deletions(-)
net/netfilter/nfnetlink_log.c +62 -13
index 37f3e7b6b538..b54fbfe56795 100644
@@ -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));
Signature · 1 line
2.56.1
author maintainer
#5 lore raw

PATCH v2 5/6 netfilter: nfnetlink: don't log hwaddr/hwheader on output hooks

In reply to 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")

---
v2: restrict hwaddr too.

 net/netfilter/nfnetlink_log.c   | 43 +++++++++++++++++++++++++++++----
 net/netfilter/nfnetlink_queue.c |  3 ++-
 2 files changed, 40 insertions(+), 6 deletions(-)
net/netfilter/nfnetlink_log.c +38 -5
index b54fbfe56795..5645297b4b3b 100644
@@ -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));
net/netfilter/nfnetlink_queue.c +2 -1
index 37f8953b7c49..c1afeb0c3e2a 100644
@@ -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)
Signature · 1 line
2.56.1
author maintainer
#6 lore raw

PATCH v2 6/6 netfilter: nfnetlink_log: only add timestamp on the input path

In reply to 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.

---
v2: adapt to changes in earlier patches, no changes per se.

 net/netfilter/nfnetlink_log.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)
net/netfilter/nfnetlink_log.c +17 -8
index 5645297b4b3b..697ca7fb1dba 100644
@@ -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;
 	}
 
Signature · 1 line
2.56.1