1#!/usr/bin/env python 2# @lint-avoid-python-3-compatibility-imports 3# 4# tcpaccept Trace TCP accept()s. 5# For Linux, uses BCC, eBPF. Embedded C. 6# 7# USAGE: tcpaccept [-h] [-T] [-t] [-p PID] [-P PORTS] [-4 | -6] 8# 9# This uses dynamic tracing of the kernel inet_csk_accept() socket function 10# (from tcp_prot.accept), and will need to be modified to match kernel changes. 11# 12# Copyright (c) 2015 Brendan Gregg. 13# Licensed under the Apache License, Version 2.0 (the "License") 14# 15# 13-Oct-2015 Brendan Gregg Created this. 16# 14-Feb-2016 " " Switch to bpf_perf_output. 17 18from __future__ import print_function 19from bcc.containers import filter_by_containers 20from bcc import BPF 21from socket import inet_ntop, AF_INET, AF_INET6 22from struct import pack 23import argparse 24from bcc.utils import printb 25from time import strftime 26 27# arguments 28examples = """examples: 29 ./tcpaccept # trace all TCP accept()s 30 ./tcpaccept -t # include timestamps 31 ./tcpaccept -P 80,81 # only trace port 80 and 81 32 ./tcpaccept -p 181 # only trace PID 181 33 ./tcpaccept --cgroupmap mappath # only trace cgroups in this BPF map 34 ./tcpaccept --mntnsmap mappath # only trace mount namespaces in the map 35 ./tcpaccept -4 # trace IPv4 family 36 ./tcpaccept -6 # trace IPv6 family 37""" 38parser = argparse.ArgumentParser( 39 description="Trace TCP accepts", 40 formatter_class=argparse.RawDescriptionHelpFormatter, 41 epilog=examples) 42parser.add_argument("-T", "--time", action="store_true", 43 help="include time column on output (HH:MM:SS)") 44parser.add_argument("-t", "--timestamp", action="store_true", 45 help="include timestamp on output") 46parser.add_argument("-p", "--pid", 47 help="trace this PID only") 48parser.add_argument("-P", "--port", 49 help="comma-separated list of local ports to trace") 50group = parser.add_mutually_exclusive_group() 51group.add_argument("-4", "--ipv4", action="store_true", 52 help="trace IPv4 family only") 53group.add_argument("-6", "--ipv6", action="store_true", 54 help="trace IPv6 family only") 55parser.add_argument("--cgroupmap", 56 help="trace cgroups in this BPF map only") 57parser.add_argument("--mntnsmap", 58 help="trace mount namespaces in this BPF map only") 59parser.add_argument("--ebpf", action="store_true", 60 help=argparse.SUPPRESS) 61args = parser.parse_args() 62debug = 0 63 64# define BPF program 65bpf_text = """ 66#include <uapi/linux/ptrace.h> 67#include <net/sock.h> 68#include <bcc/proto.h> 69 70// separate data structs for ipv4 and ipv6 71struct ipv4_data_t { 72 u64 ts_us; 73 u32 pid; 74 u32 saddr; 75 u32 daddr; 76 u64 ip; 77 u16 lport; 78 u16 dport; 79 char task[TASK_COMM_LEN]; 80}; 81BPF_PERF_OUTPUT(ipv4_events); 82 83struct ipv6_data_t { 84 u64 ts_us; 85 u32 pid; 86 unsigned __int128 saddr; 87 unsigned __int128 daddr; 88 u64 ip; 89 u16 lport; 90 u16 dport; 91 char task[TASK_COMM_LEN]; 92}; 93BPF_PERF_OUTPUT(ipv6_events); 94""" 95 96# 97# The following code uses kprobes to instrument inet_csk_accept(). 98# On Linux 4.16 and later, we could use sock:inet_sock_set_state 99# tracepoint for efficiency, but it may output wrong PIDs. This is 100# because sock:inet_sock_set_state may run outside of process context. 101# Hence, we stick to kprobes until we find a proper solution. 102# 103bpf_text_kprobe = """ 104int kretprobe__inet_csk_accept(struct pt_regs *ctx) 105{ 106 if (container_should_be_filtered()) { 107 return 0; 108 } 109 110 struct sock *newsk = (struct sock *)PT_REGS_RC(ctx); 111 u32 pid = bpf_get_current_pid_tgid() >> 32; 112 113 ##FILTER_PID## 114 115 if (newsk == NULL) 116 return 0; 117 118 // check this is TCP 119 u16 protocol = 0; 120 // workaround for reading the sk_protocol bitfield: 121 122 // Following comments add by Joe Yin: 123 // Unfortunately,it can not work since Linux 4.10, 124 // because the sk_wmem_queued is not following the bitfield of sk_protocol. 125 // And the following member is sk_gso_max_segs. 126 // So, we can use this: 127 // bpf_probe_read_kernel(&protocol, 1, (void *)((u64)&newsk->sk_gso_max_segs) - 3); 128 // In order to diff the pre-4.10 and 4.10+ ,introduce the variables gso_max_segs_offset,sk_lingertime, 129 // sk_lingertime is closed to the gso_max_segs_offset,and 130 // the offset between the two members is 4 131 132 int gso_max_segs_offset = offsetof(struct sock, sk_gso_max_segs); 133 int sk_lingertime_offset = offsetof(struct sock, sk_lingertime); 134 135 136 // Since kernel v5.6 sk_protocol is its own u16 field and gso_max_segs 137 // precedes sk_lingertime. 138 if (sk_lingertime_offset - gso_max_segs_offset == 2) 139 protocol = newsk->sk_protocol; 140 else if (sk_lingertime_offset - gso_max_segs_offset == 4) 141 // 4.10+ with little endian 142#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 143 protocol = *(u8 *)((u64)&newsk->sk_gso_max_segs - 3); 144 else 145 // pre-4.10 with little endian 146 protocol = *(u8 *)((u64)&newsk->sk_wmem_queued - 3); 147#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 148 // 4.10+ with big endian 149 protocol = *(u8 *)((u64)&newsk->sk_gso_max_segs - 1); 150 else 151 // pre-4.10 with big endian 152 protocol = *(u8 *)((u64)&newsk->sk_wmem_queued - 1); 153#else 154# error "Fix your compiler's __BYTE_ORDER__?!" 155#endif 156 157 if (protocol != IPPROTO_TCP) 158 return 0; 159 160 // pull in details 161 u16 family = 0, lport = 0, dport; 162 family = newsk->__sk_common.skc_family; 163 lport = newsk->__sk_common.skc_num; 164 dport = newsk->__sk_common.skc_dport; 165 dport = ntohs(dport); 166 167 ##FILTER_FAMILY## 168 169 ##FILTER_PORT## 170 171 if (family == AF_INET) { 172 struct ipv4_data_t data4 = {.pid = pid, .ip = 4}; 173 data4.ts_us = bpf_ktime_get_ns() / 1000; 174 data4.saddr = newsk->__sk_common.skc_rcv_saddr; 175 data4.daddr = newsk->__sk_common.skc_daddr; 176 data4.lport = lport; 177 data4.dport = dport; 178 bpf_get_current_comm(&data4.task, sizeof(data4.task)); 179 ipv4_events.perf_submit(ctx, &data4, sizeof(data4)); 180 181 } else if (family == AF_INET6) { 182 struct ipv6_data_t data6 = {.pid = pid, .ip = 6}; 183 data6.ts_us = bpf_ktime_get_ns() / 1000; 184 bpf_probe_read_kernel(&data6.saddr, sizeof(data6.saddr), 185 &newsk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32); 186 bpf_probe_read_kernel(&data6.daddr, sizeof(data6.daddr), 187 &newsk->__sk_common.skc_v6_daddr.in6_u.u6_addr32); 188 data6.lport = lport; 189 data6.dport = dport; 190 bpf_get_current_comm(&data6.task, sizeof(data6.task)); 191 ipv6_events.perf_submit(ctx, &data6, sizeof(data6)); 192 } 193 // else drop 194 195 return 0; 196} 197""" 198 199bpf_text += bpf_text_kprobe 200 201# code substitutions 202if args.pid: 203 bpf_text = bpf_text.replace('##FILTER_PID##', 204 'if (pid != %s) { return 0; }' % args.pid) 205else: 206 bpf_text = bpf_text.replace('##FILTER_PID##', '') 207if args.port: 208 lports = [int(lport) for lport in args.port.split(',')] 209 lports_if = ' && '.join(['lport != %d' % lport for lport in lports]) 210 bpf_text = bpf_text.replace('##FILTER_PORT##', 211 'if (%s) { return 0; }' % lports_if) 212if args.ipv4: 213 bpf_text = bpf_text.replace('##FILTER_FAMILY##', 214 'if (family != AF_INET) { return 0; }') 215elif args.ipv6: 216 bpf_text = bpf_text.replace('##FILTER_FAMILY##', 217 'if (family != AF_INET6) { return 0; }') 218 219bpf_text = filter_by_containers(args) + bpf_text 220if debug or args.ebpf: 221 print(bpf_text) 222 if args.ebpf: 223 exit() 224 225bpf_text = bpf_text.replace('##FILTER_PORT##', '') 226bpf_text = bpf_text.replace('##FILTER_FAMILY##', '') 227 228# process event 229def print_ipv4_event(cpu, data, size): 230 event = b["ipv4_events"].event(data) 231 global start_ts 232 if args.time: 233 printb(b"%-9s" % strftime("%H:%M:%S").encode('ascii'), nl="") 234 if args.timestamp: 235 if start_ts == 0: 236 start_ts = event.ts_us 237 printb(b"%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), nl="") 238 printb(b"%-7d %-12.12s %-2d %-16s %-5d %-16s %-5d" % (event.pid, 239 event.task, event.ip, 240 inet_ntop(AF_INET, pack("I", event.daddr)).encode(), 241 event.dport, 242 inet_ntop(AF_INET, pack("I", event.saddr)).encode(), 243 event.lport)) 244 245def print_ipv6_event(cpu, data, size): 246 event = b["ipv6_events"].event(data) 247 global start_ts 248 if args.time: 249 printb(b"%-9s" % strftime("%H:%M:%S").encode('ascii'), nl="") 250 if args.timestamp: 251 if start_ts == 0: 252 start_ts = event.ts_us 253 printb(b"%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), nl="") 254 printb(b"%-7d %-12.12s %-2d %-16s %-5d %-16s %-5d" % (event.pid, 255 event.task, event.ip, 256 inet_ntop(AF_INET6, event.daddr).encode(), 257 event.dport, 258 inet_ntop(AF_INET6, event.saddr).encode(), 259 event.lport)) 260 261# initialize BPF 262b = BPF(text=bpf_text) 263 264# header 265if args.time: 266 print("%-9s" % ("TIME"), end="") 267if args.timestamp: 268 print("%-9s" % ("TIME(s)"), end="") 269print("%-7s %-12s %-2s %-16s %-5s %-16s %-5s" % ("PID", "COMM", "IP", "RADDR", 270 "RPORT", "LADDR", "LPORT")) 271 272start_ts = 0 273 274# read events 275b["ipv4_events"].open_perf_buffer(print_ipv4_event) 276b["ipv6_events"].open_perf_buffer(print_ipv6_event) 277while 1: 278 try: 279 b.perf_buffer_poll() 280 except KeyboardInterrupt: 281 exit() 282