Skip to contentSkip to conversation

audit: include source and destination ports to NETFILTER_PKT

PATCH In review Ricardo Robaina 3 participants 7 messages Sep 26, 2025 – Oct 13, 2025

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

PATCH v3 1/1 audit: include source and destination ports to NETFILTER_PKT

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.

type=NETFILTER_PKT ... saddr=127.0.0.1 daddr=127.0.0.1 proto=icmp type=NETFILTER_PKT ... saddr=::1 daddr=::1 proto=ipv6-icmp type=NETFILTER_PKT ... daddr=127.0.0.1 proto=udp sport=38173 dport=42424 type=NETFILTER_PKT ... daddr=::1 proto=udp sport=56852 dport=42424 type=NETFILTER_PKT ... daddr=127.0.0.1 proto=tcp sport=57022 dport=42424 type=NETFILTER_PKT ... daddr=::1 proto=tcp sport=50810 dport=42424 type=NETFILTER_PKT ... daddr=127.0.0.1 proto=sctp sport=54944 dport=42424 type=NETFILTER_PKT ... daddr=::1 proto=sctp sport=57963 dport=42424

---
 net/netfilter/xt_AUDIT.c | 47 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 43 insertions(+), 4 deletions(-)
net/netfilter/xt_AUDIT.c +43 -4
index b6a015aee0ce..fb16f20cfb1b 100644
@@ -19,6 +19,7 @@
 #include <linux/netfilter_bridge/ebtables.h>
 #include <net/ipv6.h>
 #include <net/ip.h>
+#include <linux/sctp.h>
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Thomas Graf <[email protected]>");
@@ -37,8 +38,27 @@ static bool audit_ip4(struct audit_buffer *ab, struct sk_buff *skb)
 	if (!ih)
 		return false;
 
-	audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu",
-			 &ih->saddr, &ih->daddr, ih->protocol);
+	switch (ih->protocol) {
+	case IPPROTO_TCP:
+		audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu sport=%hu dport=%hu",
+				 &ih->saddr, &ih->daddr, ih->protocol,
+				 ntohs(tcp_hdr(skb)->source), ntohs(tcp_hdr(skb)->dest));
+		break;
+	case IPPROTO_UDP:
+	case IPPROTO_UDPLITE:
+		audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu sport=%hu dport=%hu",
+				 &ih->saddr, &ih->daddr, ih->protocol,
+				 ntohs(udp_hdr(skb)->source), ntohs(udp_hdr(skb)->dest));
+		break;
+	case IPPROTO_SCTP:
+		audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu sport=%hu dport=%hu",
+				 &ih->saddr, &ih->daddr, ih->protocol,
+				 ntohs(sctp_hdr(skb)->source), ntohs(sctp_hdr(skb)->dest));
+		break;
+	default:
+		audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu",
+				 &ih->saddr, &ih->daddr, ih->protocol);
+	}
 
 	return true;
 }
@@ -57,8 +77,27 @@ static bool audit_ip6(struct audit_buffer *ab, struct sk_buff *skb)
 	nexthdr = ih->nexthdr;
 	ipv6_skip_exthdr(skb, skb_network_offset(skb) + sizeof(_ip6h), &nexthdr, &frag_off);
 
-	audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu",
-			 &ih->saddr, &ih->daddr, nexthdr);
+	switch (nexthdr) {
+	case IPPROTO_TCP:
+		audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu sport=%hu dport=%hu",
+				 &ih->saddr, &ih->daddr, nexthdr,
+				 ntohs(tcp_hdr(skb)->source), ntohs(tcp_hdr(skb)->dest));
+		break;
+	case IPPROTO_UDP:
+	case IPPROTO_UDPLITE:
+		audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu sport=%hu dport=%hu",
+				 &ih->saddr, &ih->daddr, nexthdr,
+				 ntohs(udp_hdr(skb)->source), ntohs(udp_hdr(skb)->dest));
+		break;
+	case IPPROTO_SCTP:
+		audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu sport=%hu dport=%hu",
+				 &ih->saddr, &ih->daddr, nexthdr,
+				 ntohs(sctp_hdr(skb)->source), ntohs(sctp_hdr(skb)->dest));
+		break;
+	default:
+		audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu",
+				 &ih->saddr, &ih->daddr, nexthdr);
+	}
 
 	return true;
 }
Signature · 1 line
2.51.0
maintainer
#1 lore raw

net/netfilter/xt_AUDIT.c static bool audit_ip4(struct audit_buffer *ab, struct sk_buff *skb) · line 45

Quoted 4 lines · Ricardo Robaina
net/netfilter/xt_AUDIT.c +4 -0
+	case IPPROTO_TCP:
+		audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu sport=%hu dport=%hu",
+				 &ih->saddr, &ih->daddr, ih->protocol,
+				 ntohs(tcp_hdr(skb)->source), ntohs(tcp_hdr(skb)->dest));

You need to use skb_header_pointer() like elsewhere in netfilter to access the transport protocol header.

You can have a look at nf_log_dump_tcp_header() in nf_log_syslog.c for a template.

Also please have a look at net/netfilter/nft_log.c, in particular nft_log_eval_audit(): xt_AUDIT and nft audit should be kept in sync wrt. their formatting.

Maybe Paul would be open to adding something like audit_log_packet() to kernel/audit.c and then have xt_AUDIT.c and nft_log.c just call the common helper.

#2 lore raw
Quoted 10 lines · Florian Westphal

net/netfilter/xt_AUDIT.c static bool audit_ip4(struct audit_buffer *ab, struct sk_buff *skb) · line 45

Quoted 4 lines · Ricardo Robaina
net/netfilter/xt_AUDIT.c +4 -0
+     case IPPROTO_TCP:
+             audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu sport=%hu dport=%hu",
+                              &ih->saddr, &ih->daddr, ih->protocol,
+                              ntohs(tcp_hdr(skb)->source), ntohs(tcp_hdr(skb)->dest));

You need to use skb_header_pointer() like elsewhere in netfilter to access the transport protocol header.

Quoted 7 lines

You can have a look at nf_log_dump_tcp_header() in nf_log_syslog.c for a template.

Also please have a look at net/netfilter/nft_log.c, in particular nft_log_eval_audit(): xt_AUDIT and nft audit should be kept in sync wrt. their formatting.

Thanks for reviewing this patch, Florian!
I’ll work on a newer version addressing your suggestions.

Quoted 4 lines

Maybe Paul would be open to adding something like audit_log_packet() to kernel/audit.c and then have xt_AUDIT.c and nft_log.c just call the common helper.

It sounds like a good idea to me. What do you think, Paul?

maintainer
#3 lore raw
Quoted 2 lines · Ricardo Robaina
Quoted 1 line · Florian Westphal

Ricardo Robaina <[email protected]> wrote:

...

Quoted 5 lines
Quoted 3 lines

Maybe Paul would be open to adding something like audit_log_packet() to kernel/audit.c and then have xt_AUDIT.c and nft_log.c just call the common helper.

It sounds like a good idea to me. What do you think, Paul?

Seems like a good idea to me too.

Signature · 1 line
paul-moore.com
maintainer
#4 lore raw
Quoted 6 lines · Paul Moore
Quoted 2 lines · Ricardo Robaina
Quoted 1 line · Florian Westphal

Ricardo Robaina <[email protected]> wrote:

...

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Maybe Paul would be open to adding something like audit_log_packet() to kernel/audit.c and then have xt_AUDIT.c and nft_log.c just call the common helper.

It sounds like a good idea to me. What do you think, Paul?

Seems like a good idea to me too.

A quick follow-up to this ... when you are doing the work Ricardo, please do this as a two patch patchset; the first patch should introduce a new common function called by both audit_tg() and nft_log_eval_audit(), and the second patch should add new port information to the audit record.

Signature · 1 line
paul-moore.com
#5 lore raw
Quoted 16 lines · Paul Moore
Quoted 6 lines · Paul Moore
Quoted 2 lines · Ricardo Robaina
Quoted 1 line · Florian Westphal

Ricardo Robaina <[email protected]> wrote:

...

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Maybe Paul would be open to adding something like audit_log_packet() to kernel/audit.c and then have xt_AUDIT.c and nft_log.c just call the common helper.

It sounds like a good idea to me. What do you think, Paul?

Seems like a good idea to me too.

Quoted 9 lines

A quick follow-up to this ... when you are doing the work Ricardo, please do this as a two patch patchset; the first patch should introduce a new common function called by both audit_tg() and nft_log_eval_audit(), and the second patch should add new port information to the audit record.

--
paul-moore.com

Thanks for the tip, Paul! I'll work on it next week.

maintainer
#6 lore raw
Quoted 16 lines · Ricardo Robaina
Quoted 15 lines · Paul Moore
Quoted 13 lines · Paul Moore
Quoted 2 lines · Ricardo Robaina
Quoted 1 line · Florian Westphal

Ricardo Robaina <[email protected]> wrote:

...

Quoted 5 lines
Quoted 3 lines

Maybe Paul would be open to adding something like audit_log_packet() to kernel/audit.c and then have xt_AUDIT.c and nft_log.c just call the common helper.

It sounds like a good idea to me. What do you think, Paul?

Seems like a good idea to me too.

Quoted 7 lines
Quoted 5 lines

A quick follow-up to this ... when you are doing the work Ricardo, please do this as a two patch patchset; the first patch should introduce a new common function called by both audit_tg() and nft_log_eval_audit(), and the second patch should add new port information to the audit record.

Thanks for the tip, Paul! I'll work on it next week.

Great, thanks :)

Signature · 1 line
paul-moore.com