audit: include source and destination ports to NETFILTER_PKT
Subsystems (3)
PATCH v2 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 | 42 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-)
net/netfilter/xt_AUDIT.c +41 -1
@@ -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]>" );
@@ -32,6 +33,7 @@ static bool audit_ip4(struct audit_buffer *ab, struct sk_buff *skb) {struct iphdr _iph;const struct iphdr *ih; +__be16 dport, sport; ih =skb_header_pointer (skb,skb_network_offset (skb),sizeof (_iph), &_iph);if (!ih)
@@ -40,6 +42,25 @@ static bool audit_ip4(struct audit_buffer *ab, struct sk_buff *skb)audit_log_format (ab," saddr=%pI4 daddr=%pI4 proto=%hhu" , &ih->saddr, &ih->daddr, ih->protocol); +switch (ih->protocol) { +case IPPROTO_TCP : + sport =tcp_hdr (skb)->source; + dport =tcp_hdr (skb)->dest; +break ; +case IPPROTO_UDP : +case IPPROTO_UDPLITE : + sport =udp_hdr (skb)->source; + dport =udp_hdr (skb)->dest; +break ; +case IPPROTO_SCTP : + sport =sctp_hdr (skb)->source; + dport =sctp_hdr (skb)->dest; + } + +if (ih->protocol ==IPPROTO_TCP || ih->protocol ==IPPROTO_UDP || + ih->protocol ==IPPROTO_UDPLITE || ih->protocol ==IPPROTO_SCTP ) +audit_log_format (ab," sport=%hu dport=%hu" ,ntohs (sport),ntohs (dport)); +return true; }
@@ -48,7 +69,7 @@ static bool audit_ip6(struct audit_buffer *ab, struct sk_buff *skb)struct ipv6hdr _ip6h;const struct ipv6hdr *ih;u8 nexthdr; -__be16 frag_off; +__be16 frag_off, dport, sport; ih =skb_header_pointer (skb,skb_network_offset (skb),sizeof (_ip6h), &_ip6h);if (!ih)
@@ -60,6 +81,25 @@ static bool audit_ip6(struct audit_buffer *ab, struct sk_buff *skb)audit_log_format (ab," saddr=%pI6c daddr=%pI6c proto=%hhu" , &ih->saddr, &ih->daddr, nexthdr); +switch (ih->nexthdr) { +case IPPROTO_TCP : + sport =tcp_hdr (skb)->source; + dport =tcp_hdr (skb)->dest; +break ; +case IPPROTO_UDP : +case IPPROTO_UDPLITE : + sport =udp_hdr (skb)->source; + dport =udp_hdr (skb)->dest; +break ; +case IPPROTO_SCTP : + sport =sctp_hdr (skb)->source; + dport =sctp_hdr (skb)->dest; + } + +if (ih->nexthdr ==IPPROTO_TCP || ih->nexthdr ==IPPROTO_UDP || + ih->nexthdr ==IPPROTO_UDPLITE || ih->nexthdr ==IPPROTO_SCTP ) +audit_log_format (ab," sport=%hu dport=%hu" ,ntohs (sport),ntohs (dport)); +return true; }
Signature · 1 line
2.51.0
Paul Moore
maintainerQuoted 59 lines · On Sep 25, 2025 Ricardo Robaina <[email protected]> wrote:
NETFILTER_PKTrecords 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_PKTrecords.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
Signed-off-by Ricardo Robaina <[email protected]>--- net/netfilter/xt_AUDIT.c | 42 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-)net/netfilter/xt_AUDIT.c +12
-0@@ -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]>" );@@ -32,6 +33,7 @@ static bool audit_ip4(struct audit_buffer *ab, struct sk_buff *skb) {struct iphdr _iph;const struct iphdr *ih; +__be16 dport, sport; ih =skb_header_pointer (skb,skb_network_offset (skb),sizeof (_iph), &_iph);if (!ih)@@ -40,6 +42,25 @@ static bool audit_ip4(struct audit_buffer *ab, struct sk_buff *skb)audit_log_format (ab," saddr=%pI4 daddr=%pI4 proto=%hhu" , &ih->saddr, &ih->daddr, ih->protocol); +switch (ih->protocol) { +case IPPROTO_TCP : + sport =tcp_hdr (skb)->source; + dport =tcp_hdr (skb)->dest; +break ; +case IPPROTO_UDP : +case IPPROTO_UDPLITE : + sport =udp_hdr (skb)->source; + dport =udp_hdr (skb)->dest; +break ;
net/netfilter/xt_AUDIT.c static bool audit_ip4(struct audit_buffer *ab, struct sk_buff *skb) · line 64
Quoted 10 lines
net/netfilter/xt_AUDIT.c +8
-0+case IPPROTO_SCTP : + sport =sctp_hdr (skb)->source; + dport =sctp_hdr (skb)->dest; + } + +if (ih->protocol ==IPPROTO_TCP || ih->protocol ==IPPROTO_UDP || + ih->protocol ==IPPROTO_UDPLITE || ih->protocol ==IPPROTO_SCTP ) +audit_log_format (ab," sport=%hu dport=%hu" ,ntohs (sport),ntohs (dport));return true; }
Instead of having the switch statement and then doing an additional if statement, why not fold it all into the switch statement? Yes, you would have multiple audit_log_format() calls, but they are trivial to cut-n-paste, and it saves the extra per-packet checking at runtime.
switch (ih->protocol) {
case IPPROTO_TCP:
audit_log_format(ab, " sport=...",
tcp_hdr(skb)->source,
tcp_hdr(skb)->dest);
break;
...
}... considering how expensive multiple audit_log_format() calls can be, it might even be worth considering consolidating the two calls into one:
switch (ih->protocol) {
case IPPROTO_TCP:
audit_log_format(ab, " saddr=...",
ih->saddr,
...
tcp_hdr(skb)->source,
tcp_hdr(skb)->dest);
break;
...
default:
audit_log_format(ab, " saddr=...",
ih->saddr,
...);
}--
paul-moore.com
Thanks for reviewing this patch, Paul! Sounds great, I'll work on the v3 shortly.
Quoted 107 lines · Paul Moore
Quoted 59 lines · On Sep 25, 2025 Ricardo Robaina <[email protected]> wrote:
NETFILTER_PKTrecords 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_PKTrecords.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
Signed-off-by Ricardo Robaina <[email protected]>--- net/netfilter/xt_AUDIT.c | 42 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-)net/netfilter/xt_AUDIT.c +12
-0@@ -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]>" );@@ -32,6 +33,7 @@ static bool audit_ip4(struct audit_buffer *ab, struct sk_buff *skb) {struct iphdr _iph;const struct iphdr *ih; +__be16 dport, sport; ih =skb_header_pointer (skb,skb_network_offset (skb),sizeof (_iph), &_iph);if (!ih)@@ -40,6 +42,25 @@ static bool audit_ip4(struct audit_buffer *ab, struct sk_buff *skb)audit_log_format (ab," saddr=%pI4 daddr=%pI4 proto=%hhu" , &ih->saddr, &ih->daddr, ih->protocol); +switch (ih->protocol) { +case IPPROTO_TCP : + sport =tcp_hdr (skb)->source; + dport =tcp_hdr (skb)->dest; +break ; +case IPPROTO_UDP : +case IPPROTO_UDPLITE : + sport =udp_hdr (skb)->source; + dport =udp_hdr (skb)->dest; +break ;net/netfilter/xt_AUDIT.c static bool audit_ip4(struct audit_buffer *ab, struct sk_buff *skb) · line 64
Quoted 10 lines
net/netfilter/xt_AUDIT.c +8
-0+case IPPROTO_SCTP : + sport =sctp_hdr (skb)->source; + dport =sctp_hdr (skb)->dest; + } + +if (ih->protocol ==IPPROTO_TCP || ih->protocol ==IPPROTO_UDP || + ih->protocol ==IPPROTO_UDPLITE || ih->protocol ==IPPROTO_SCTP ) +audit_log_format (ab," sport=%hu dport=%hu" ,ntohs (sport),ntohs (dport));return true; }Instead of having the switch statement and then doing an additional if statement, why not fold it all into the switch statement? Yes, you would have multiple
audit_log_format()calls, but they are trivial to cut-n-paste, and it saves the extra per-packet checking at runtime.switch (ih->protocol) { case IPPROTO_TCP: audit_log_format(ab, " sport=...", tcp_hdr(skb)->source, tcp_hdr(skb)->dest); break; ... }... considering how expensive multiple
audit_log_format()calls can be, it might even be worth considering consolidating the two calls into one:switch (ih->protocol) { case IPPROTO_TCP: audit_log_format(ab, " saddr=...", ih->saddr, ... tcp_hdr(skb)->source, tcp_hdr(skb)->dest); break; ... default: audit_log_format(ab, " saddr=...", ih->saddr, ...); }--
paul-moore.com