Skip to contentSkip to conversation

audit: improve NETFILTER_PKT records

PATCH In review Ricardo Robaina 3 participants 8 messages Nov 14, 2025 – Dec 17, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v7 cover audit: improve NETFILTER_PKT records

Currently, NETFILTER_PKT records lack source and destination port information, which is often valuable for troubleshooting. This patch series adds ports numbers, to NETFILTER_PKT records.

The first patch refactors netfilter-related code, by moving duplicated code to audit.c, by creating audit_log_nf_skb() helper function.
The second one, improves the NETFILTER_PKT records, by including source and destination ports for protocols of interest.

Ricardo Robaina (2):
audit: add audit_log_nf_skb helper function
audit: include source and destination ports to NETFILTER_PKT

 include/linux/audit.h    |   8 ++
 kernel/audit.c           | 159 +++++++++++++++++++++++++++++++++++++++
 net/netfilter/nft_log.c  |  58 +-------------
 net/netfilter/xt_AUDIT.c |  58 +-------------
 4 files changed, 169 insertions(+), 114 deletions(-)
Signature · 1 line
2.51.1
#1 lore raw

PATCH v7 1/2 audit: add audit_log_nf_skb helper function

Netfilter code (net/netfilter/nft_log.c and net/netfilter/xt_AUDIT.c) have to be kept in sync. Both source files had duplicated versions of audit_ip4() and audit_ip6() functions, which can result in lack of consistency and/or duplicated work.

This patch adds a helper function in audit.c that can be called by netfilter code commonly, aiming to improve maintainability and consistency.

---
 include/linux/audit.h    |  8 +++++
 kernel/audit.c           | 64 ++++++++++++++++++++++++++++++++++++++++
 net/netfilter/nft_log.c  | 58 +-----------------------------------
 net/netfilter/xt_AUDIT.c | 58 +-----------------------------------
 4 files changed, 74 insertions(+), 114 deletions(-)
include/linux/audit.h +8 -0
index 536f8ee8da81..d8173af498ba 100644
@@ -195,6 +195,8 @@ extern int audit_log_subj_ctx(struct audit_buffer *ab, struct lsm_prop *prop);
 extern int audit_log_obj_ctx(struct audit_buffer *ab, struct lsm_prop *prop);
 extern int audit_log_task_context(struct audit_buffer *ab);
 extern void audit_log_task_info(struct audit_buffer *ab);
+extern int audit_log_nf_skb(struct audit_buffer *ab,
+			    const struct sk_buff *skb, u8 nfproto);
 
 extern int		    audit_update_lsm_rules(void);
 
@@ -272,6 +274,12 @@ static inline int audit_log_task_context(struct audit_buffer *ab)
 static inline void audit_log_task_info(struct audit_buffer *ab)
 { }
 
+static inline int audit_log_nf_skb(struct audit_buffer *ab,
+				   const struct sk_buff *skb, u8 nfproto)
+{
+	return 0;
+}
+
 static inline kuid_t audit_get_loginuid(struct task_struct *tsk)
 {
 	return INVALID_UID;
kernel/audit.c +64 -0

76 lines not on this page · raw message

net/netfilter/nft_log.c +1 -57

74 lines not on this page · raw message

net/netfilter/xt_AUDIT.c +1 -57

74 lines not on this page · raw message

Signature · 1 line
2.51.1
maintainer Acked-by on v7 1/2
#2 lore raw

Re: [PATCH v7 1/2] audit: add audit_log_nf_skb helper function

Quoted 8 lines · Ricardo Robaina

Netfilter code (net/netfilter/nft_log.c and net/netfilter/xt_AUDIT.c) have to be kept in sync. Both source files had duplicated versions of audit_ip4() and audit_ip6() functions, which can result in lack of consistency and/or duplicated work.

This patch adds a helper function in audit.c that can be called by netfilter code commonly, aiming to improve maintainability and consistency.

#3 lore raw

PATCH v7 2/2 audit: include source and destination ports to NETFILTER_PKT

In reply to Ricardo Robaina

NETFILTER_PKT records show both source and destination addresses, in addition to the associated networking protocol. However, it lacks the ports information, which is often valuable for troubleshooting.

This patch adds both source and destination port numbers, 'sport' and 'dport' respectively, to TCP, UDP, UDP-Lite and SCTP-related NETFILTER_PKT records.

 $ TESTS="netfilter_pkt" make -e test &> /dev/null
 $ ausearch -i -ts recent |grep NETFILTER_PKT
 type=NETFILTER_PKT ... proto=icmp
 type=NETFILTER_PKT ... proto=ipv6-icmp
 type=NETFILTER_PKT ... proto=udp sport=46333 dport=42424
 type=NETFILTER_PKT ... proto=udp sport=35953 dport=42424
 type=NETFILTER_PKT ... proto=tcp sport=50314 dport=42424
 type=NETFILTER_PKT ... proto=tcp sport=57346 dport=42424
---
 kernel/audit.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 99 insertions(+), 4 deletions(-)
kernel/audit.c +99 -4
index 5c302c4592db..39c4f26c484d 100644
@@ -60,6 +60,7 @@
 #include <net/netns/generic.h>
 #include <net/ip.h>
 #include <net/ipv6.h>
+#include <linux/sctp.h>
 
 #include "audit.h"
 
@@ -2517,8 +2518,55 @@ int audit_log_nf_skb(struct audit_buffer *ab,
 		if (!ih)
 			return -ENOMEM;
 
-		audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu",
-				 &ih->saddr, &ih->daddr, ih->protocol);
+		switch (ih->protocol) {
+		case IPPROTO_TCP: {
+			struct tcphdr _tcph;
+			const struct tcphdr *th;
+
+			th = skb_header_pointer(skb, skb_transport_offset(skb),
+						sizeof(_tcph), &_tcph);
+			if (!th)
+				return -ENOMEM;
+
+			audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu sport=%hu dport=%hu",
+					 &ih->saddr, &ih->daddr, ih->protocol,
+					 ntohs(th->source), ntohs(th->dest));
+			break;
+		}
+		case IPPROTO_UDP:
+		case IPPROTO_UDPLITE: {
+			struct udphdr _udph;
+			const struct udphdr *uh;
+
+			uh = skb_header_pointer(skb, skb_transport_offset(skb),
+						sizeof(_udph), &_udph);
+			if (!uh)
+				return -ENOMEM;
+
+			audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu sport=%hu dport=%hu",
+					 &ih->saddr, &ih->daddr, ih->protocol,
+					 ntohs(uh->source), ntohs(uh->dest));
+			break;
+		}
+		case IPPROTO_SCTP: {
+			struct sctphdr _sctph;
+			const struct sctphdr *sh;
+
+			sh = skb_header_pointer(skb, skb_transport_offset(skb),
+						sizeof(_sctph), &_sctph);
+			if (!sh)
+				return -ENOMEM;
+
+			audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu sport=%hu dport=%hu",
+					 &ih->saddr, &ih->daddr, ih->protocol,
+					 ntohs(sh->source), ntohs(sh->dest));
+			break;
+		}
+		default:
+			audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu",
+					 &ih->saddr, &ih->daddr, ih->protocol);
+		}
+
 		break;
 	}
 	case NFPROTO_IPV6: {
@@ -2536,8 +2584,55 @@ int audit_log_nf_skb(struct audit_buffer *ab,
 		ipv6_skip_exthdr(skb, skb_network_offset(skb) + sizeof(iph),
 				 &nexthdr, &frag_off);
 
-		audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu",
-				 &ih->saddr, &ih->daddr, nexthdr);
+		switch (nexthdr) {
+		case IPPROTO_TCP: {
+			struct tcphdr _tcph;
+			const struct tcphdr *th;
+
+			th = skb_header_pointer(skb, skb_transport_offset(skb),
+						sizeof(_tcph), &_tcph);
+			if (!th)
+				return -ENOMEM;
+
+			audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu sport=%hu dport=%hu",
+					 &ih->saddr, &ih->daddr, nexthdr,
+					 ntohs(th->source), ntohs(th->dest));
+			break;
+		}
+		case IPPROTO_UDP:
+		case IPPROTO_UDPLITE: {
+			struct udphdr _udph;
+			const struct udphdr *uh;
+
+			uh = skb_header_pointer(skb, skb_transport_offset(skb),
+						sizeof(_udph), &_udph);
+			if (!uh)
+				return -ENOMEM;
+
+			audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu sport=%hu dport=%hu",
+					 &ih->saddr, &ih->daddr, nexthdr,
+					 ntohs(uh->source), ntohs(uh->dest));
+			break;
+		}
+		case IPPROTO_SCTP: {
+			struct sctphdr _sctph;
+			const struct sctphdr *sh;
+
+			sh = skb_header_pointer(skb, skb_transport_offset(skb),
+						sizeof(_sctph), &_sctph);
+			if (!sh)
+				return -ENOMEM;
+
+			audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu sport=%hu dport=%hu",
+					 &ih->saddr, &ih->daddr, nexthdr,
+					 ntohs(sh->source), ntohs(sh->dest));
+			break;
+		}
+		default:
+			audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu",
+					 &ih->saddr, &ih->daddr, nexthdr);
+		}
+
 		break;
 	}
 	default:
Signature · 1 line
2.51.1
maintainer Acked-by on v7 2/2
#4 lore raw

Re: [PATCH v7 2/2] audit: include source and destination ports to NETFILTER_PKT

Quoted 18 lines · Ricardo Robaina

NETFILTER_PKT records show both source and destination addresses, in addition to the associated networking protocol. However, it lacks the ports information, which is often valuable for troubleshooting.

This patch adds both source and destination port numbers, 'sport' and 'dport' respectively, to TCP, UDP, UDP-Lite and SCTP-related NETFILTER_PKT records.

 $ TESTS="netfilter_pkt" make -e test &> /dev/null
 $ ausearch -i -ts recent |grep NETFILTER_PKT
 type=NETFILTER_PKT ... proto=icmp
 type=NETFILTER_PKT ... proto=ipv6-icmp
 type=NETFILTER_PKT ... proto=udp sport=46333 dport=42424
 type=NETFILTER_PKT ... proto=udp sport=35953 dport=42424
 type=NETFILTER_PKT ... proto=tcp sport=50314 dport=42424
 type=NETFILTER_PKT ... proto=tcp sport=57346 dport=42424
Quoted 1 line
maintainer
#5 lore raw
In reply to Ricardo Robaina
Quoted 16 lines · Ricardo Robaina

Currently, NETFILTER_PKT records lack source and destination port information, which is often valuable for troubleshooting. This patch series adds ports numbers, to NETFILTER_PKT records.

The first patch refactors netfilter-related code, by moving duplicated code to audit.c, by creating audit_log_nf_skb() helper function.
The second one, improves the NETFILTER_PKT records, by including source and destination ports for protocols of interest.

Ricardo Robaina (2):
audit: add audit_log_nf_skb helper function
audit: include source and destination ports to NETFILTER_PKT

Quoted 5 lines
 include/linux/audit.h    |   8 ++
 kernel/audit.c           | 159 +++++++++++++++++++++++++++++++++++++++
 net/netfilter/nft_log.c  |  58 +-------------
 net/netfilter/xt_AUDIT.c |  58 +-------------
 4 files changed, 169 insertions(+), 114 deletions(-)

Thanks Ricardo, both patches look good to me, I'm going to merge them into audit/dev-staging just to get some very basic testing, but if I can get an ACK from Florian on the patchset I'll go ahead and move the patches over to audit/dev (feeds into linux-next and the next merge window).

Signature · 1 line
paul-moore.com
maintainer
#6 lore raw
Quoted 23 lines · Paul Moore
Quoted 16 lines · Ricardo Robaina

Currently, NETFILTER_PKT records lack source and destination port information, which is often valuable for troubleshooting. This patch series adds ports numbers, to NETFILTER_PKT records.

The first patch refactors netfilter-related code, by moving duplicated code to audit.c, by creating audit_log_nf_skb() helper function.
The second one, improves the NETFILTER_PKT records, by including source and destination ports for protocols of interest.

Ricardo Robaina (2):
audit: add audit_log_nf_skb helper function
audit: include source and destination ports to NETFILTER_PKT

Quoted 5 lines
 include/linux/audit.h    |   8 ++
 kernel/audit.c           | 159 +++++++++++++++++++++++++++++++++++++++
 net/netfilter/nft_log.c  |  58 +-------------
 net/netfilter/xt_AUDIT.c |  58 +-------------
 4 files changed, 169 insertions(+), 114 deletions(-)
Quoted 5 lines

Thanks Ricardo, both patches look good to me, I'm going to merge them into audit/dev-staging just to get some very basic testing, but if I can get an ACK from Florian on the patchset I'll go ahead and move the patches over to audit/dev (feeds into linux-next and the next merge window).

I just moved these patches in audit/dev with Florian's ACK. Thanks everyone!

Signature · 1 line
paul-moore.com
#7 lore raw
Quoted 31 lines · Paul Moore
Quoted 23 lines · Paul Moore
Quoted 16 lines · Ricardo Robaina

Currently, NETFILTER_PKT records lack source and destination port information, which is often valuable for troubleshooting. This patch series adds ports numbers, to NETFILTER_PKT records.

The first patch refactors netfilter-related code, by moving duplicated code to audit.c, by creating audit_log_nf_skb() helper function.
The second one, improves the NETFILTER_PKT records, by including source and destination ports for protocols of interest.

Ricardo Robaina (2):
audit: add audit_log_nf_skb helper function
audit: include source and destination ports to NETFILTER_PKT

Quoted 5 lines
 include/linux/audit.h    |   8 ++
 kernel/audit.c           | 159 +++++++++++++++++++++++++++++++++++++++
 net/netfilter/nft_log.c  |  58 +-------------
 net/netfilter/xt_AUDIT.c |  58 +-------------
 4 files changed, 169 insertions(+), 114 deletions(-)
Quoted 5 lines

Thanks Ricardo, both patches look good to me, I'm going to merge them into audit/dev-staging just to get some very basic testing, but if I can get an ACK from Florian on the patchset I'll go ahead and move the patches over to audit/dev (feeds into linux-next and the next merge window).

Quoted 5 lines

I just moved these patches in audit/dev with Florian's ACK. Thanks everyone!

--
paul-moore.com

I'm happy to hear it. Thanks, Paul and Florian!