To monitor the processing of TCP packets with source or destination port equal to 9876 across all stages of packet processing in the Linux kernel (version 5.15) using bpftrace (Version 0.14.0), you can use the following script. This script will trace TCP packets that have either the source or destination port equal to 9876, covering various stages of packet processing.
// tcp_monitor.bt
// Define a probe for when TCP packets are received
// (skb is the packet, and sk is the socket)
kfunc:tcp_rcv_established
{
// Get the socket and skb
$sk = arg1;
$skb = arg2;
// Check if the socket is not null and the packet is a TCP packet
if ($sk && $skb->protocol == 0x06) { // 0x06 is the protocol number for TCP
// Get the source and destination ports
$src_port = $skb->head->transport_header + 2; // 2 bytes for src port in TCP header
$dst_port = $skb->head->transport_header + 4; // 2 bytes for dst port in TCP header
// Check if either port matches 9876
if (*(u16)$src_port == 9876 || *(u16)$dst_port == 9876) {
// Print the details
printf("TCP packet received:\n");
printf(" Source IP: %s\n", inet_ntop(AF_INET, $skb->head->network_header + 12));
printf(" Destination IP: %s\n", inet_ntop(AF_INET, $skb->head->network_header + 16));
printf(" Source Port: %d\n", *(u16)$src_port);
printf(" Destination Port: %d\n", *(u16)$dst_port);
}
}
}
// Probe for when TCP packets are sent
kfunc:tcp_send_skb
{
$sk = arg1;
$skb = arg2;
if ($sk && $skb->protocol == 0x06) {
$src_port = $skb->head->transport_header + 2;
$dst_port = $skb->head->transport_header + 4;
if (*(u16)$src_port == 9876 || *(u16)$dst_port == 9876) {
printf("TCP packet sent:\n");
printf(" Source IP: %s\n", inet_ntop(AF_INET, $skb->head->network_header + 12));
printf(" Destination IP: %s\n", inet_ntop(AF_INET, $skb->head->network_header + 16));
printf(" Source Port: %d\n", *(u16)$src_port);
printf(" Destination Port: %d\n", *(u16)$dst_port);
}
}
}
// Additional probes for other packet processing stages can be added similarly
However, note the following:
-
bpftrace and Linux kernel version compatibility: Ensure that your bpftrace and Linux kernel versions are compatible. The script provided is a general example and might need adjustments based on the actual kernel and bpftrace version you're using.
-
Accessing packet data: The script assumes certain offsets for accessing packet data (e.g., transport_header, network_header). These might vary slightly depending on the kernel version and configuration.
-
Permissions: Running bpftrace requires appropriate permissions. You might need to use sudo to run your script.
-
Attach probes: The script uses kfunc probes which are available in newer kernel versions that support BPF_TRACE_KFUNC (which was introduced in Linux 5.10 and enhanced in later versions).
Before running the script, ensure you have bpftrace installed and that your system supports BPF (e.g., check if /sys/kernel/bpf exists and is accessible).
To run the script, save it to a file (e.g., tcp_monitor.bt), then execute it with bpftrace:
sudo bpftrace tcp_monitor.bt
This will start tracing TCP packets with source or destination port 9876 across various stages of packet processing. Adjust the script as necessary to fit specific requirements or kernel versions.