blob: acdf176723dc158b7e6e5a365fdcfb75b2ac28bb [file] [log] [blame]
Alexey Ivanovcc01a9c2019-01-16 09:50:46 -08001#!/usr/bin/python
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08002# @lint-avoid-python-3-compatibility-imports
Brendan Greggf06d3b42015-10-15 17:21:32 -07003#
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08004# tcpconnect Trace TCP connect()s.
5# For Linux, uses BCC, eBPF. Embedded C.
Brendan Greggf06d3b42015-10-15 17:21:32 -07006#
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +08007# USAGE: tcpconnect [-h] [-c] [-t] [-p PID] [-P PORT [PORT ...]]
Brendan Greggf06d3b42015-10-15 17:21:32 -07008#
9# All connection attempts are traced, even if they ultimately fail.
10#
Brendan Gregg24825522016-02-14 16:32:29 -080011# This uses dynamic tracing of kernel functions, and will need to be updated
12# to match kernel changes.
13#
Brendan Greggf06d3b42015-10-15 17:21:32 -070014# Copyright (c) 2015 Brendan Gregg.
15# Licensed under the Apache License, Version 2.0 (the "License")
16#
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080017# 25-Sep-2015 Brendan Gregg Created this.
Brendan Gregg24825522016-02-14 16:32:29 -080018# 14-Feb-2016 " " Switch to bpf_perf_output.
Takuma Kumeb181a8e2019-01-10 05:49:59 +090019# 09-Jan-2019 Takuma Kume Support filtering by UID
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +080020# 30-Jul-2019 Xiaozhou Liu Count connects.
Nabil Schear33817e62020-10-07 21:58:07 -070021# 07-Oct-2020 Nabil Schear Correlate connects with DNS responses
Brendan Greggf06d3b42015-10-15 17:21:32 -070022
23from __future__ import print_function
24from bcc import BPF
Alban Crequy32ab8582020-03-22 16:06:44 +010025from bcc.containers import filter_by_containers
japrocaed9b1e2019-01-04 20:21:46 +030026from bcc.utils import printb
Brendan Greggf06d3b42015-10-15 17:21:32 -070027import argparse
chantra52938052016-09-10 09:44:50 -070028from socket import inet_ntop, ntohs, AF_INET, AF_INET6
Mark Drayton11de2982016-06-26 21:14:44 +010029from struct import pack
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +080030from time import sleep
Nabil Schear33817e62020-10-07 21:58:07 -070031from datetime import datetime
Brendan Greggf06d3b42015-10-15 17:21:32 -070032
33# arguments
34examples = """examples:
35 ./tcpconnect # trace all TCP connect()s
36 ./tcpconnect -t # include timestamps
Nabil Schear33817e62020-10-07 21:58:07 -070037 ./tcpconnect -d # include DNS queries associated with connects
Brendan Greggf06d3b42015-10-15 17:21:32 -070038 ./tcpconnect -p 181 # only trace PID 181
chantra52938052016-09-10 09:44:50 -070039 ./tcpconnect -P 80 # only trace port 80
40 ./tcpconnect -P 80,81 # only trace port 80 and 81
Takuma Kumeb181a8e2019-01-10 05:49:59 +090041 ./tcpconnect -U # include UID
42 ./tcpconnect -u 1000 # only trace UID 1000
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +080043 ./tcpconnect -c # count connects per src ip and dest ip/port
Alban Crequy32ab8582020-03-22 16:06:44 +010044 ./tcpconnect --cgroupmap mappath # only trace cgroups in this BPF map
45 ./tcpconnect --mntnsmap mappath # only trace mount namespaces in the map
Brendan Greggf06d3b42015-10-15 17:21:32 -070046"""
47parser = argparse.ArgumentParser(
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080048 description="Trace TCP connects",
49 formatter_class=argparse.RawDescriptionHelpFormatter,
50 epilog=examples)
Brendan Greggf06d3b42015-10-15 17:21:32 -070051parser.add_argument("-t", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080052 help="include timestamp on output")
Brendan Greggf06d3b42015-10-15 17:21:32 -070053parser.add_argument("-p", "--pid",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080054 help="trace this PID only")
chantra52938052016-09-10 09:44:50 -070055parser.add_argument("-P", "--port",
56 help="comma-separated list of destination ports to trace.")
Takuma Kumeb181a8e2019-01-10 05:49:59 +090057parser.add_argument("-U", "--print-uid", action="store_true",
58 help="include UID on output")
59parser.add_argument("-u", "--uid",
60 help="trace this UID only")
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +080061parser.add_argument("-c", "--count", action="store_true",
62 help="count connects per src ip and dest ip/port")
Alban Crequy1ce868f2020-02-19 17:07:41 +010063parser.add_argument("--cgroupmap",
64 help="trace cgroups in this BPF map only")
Alban Crequy32ab8582020-03-22 16:06:44 +010065parser.add_argument("--mntnsmap",
66 help="trace mount namespaces in this BPF map only")
Nabil Schear33817e62020-10-07 21:58:07 -070067parser.add_argument("-d", "--dns", action="store_true",
68 help="include likely DNS query associated with each connect")
Nathan Scottcf0792f2018-02-02 16:56:50 +110069parser.add_argument("--ebpf", action="store_true",
70 help=argparse.SUPPRESS)
Brendan Greggf06d3b42015-10-15 17:21:32 -070071args = parser.parse_args()
72debug = 0
73
74# define BPF program
75bpf_text = """
76#include <uapi/linux/ptrace.h>
77#include <net/sock.h>
78#include <bcc/proto.h>
79
80BPF_HASH(currsock, u32, struct sock *);
81
Brendan Gregg24825522016-02-14 16:32:29 -080082// separate data structs for ipv4 and ipv6
83struct ipv4_data_t {
Brendan Gregg24825522016-02-14 16:32:29 -080084 u64 ts_us;
Joe Yin36ce1122018-08-17 06:04:00 +080085 u32 pid;
Takuma Kumeb181a8e2019-01-10 05:49:59 +090086 u32 uid;
Joe Yin36ce1122018-08-17 06:04:00 +080087 u32 saddr;
88 u32 daddr;
Mark Drayton11de2982016-06-26 21:14:44 +010089 u64 ip;
Joe Yin36ce1122018-08-17 06:04:00 +080090 u16 dport;
Brendan Gregg24825522016-02-14 16:32:29 -080091 char task[TASK_COMM_LEN];
92};
93BPF_PERF_OUTPUT(ipv4_events);
94
95struct ipv6_data_t {
Brendan Gregg24825522016-02-14 16:32:29 -080096 u64 ts_us;
Joe Yin36ce1122018-08-17 06:04:00 +080097 u32 pid;
Takuma Kumeb181a8e2019-01-10 05:49:59 +090098 u32 uid;
Mark Drayton11de2982016-06-26 21:14:44 +010099 unsigned __int128 saddr;
100 unsigned __int128 daddr;
Brendan Gregg24825522016-02-14 16:32:29 -0800101 u64 ip;
Joe Yin36ce1122018-08-17 06:04:00 +0800102 u16 dport;
Brendan Gregg24825522016-02-14 16:32:29 -0800103 char task[TASK_COMM_LEN];
104};
105BPF_PERF_OUTPUT(ipv6_events);
106
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800107// separate flow keys per address family
108struct ipv4_flow_key_t {
109 u32 saddr;
110 u32 daddr;
111 u16 dport;
112};
113BPF_HASH(ipv4_count, struct ipv4_flow_key_t);
114
115struct ipv6_flow_key_t {
116 unsigned __int128 saddr;
117 unsigned __int128 daddr;
118 u16 dport;
119};
120BPF_HASH(ipv6_count, struct ipv6_flow_key_t);
121
Brendan Greggf06d3b42015-10-15 17:21:32 -0700122int trace_connect_entry(struct pt_regs *ctx, struct sock *sk)
123{
Alban Crequy32ab8582020-03-22 16:06:44 +0100124 if (container_should_be_filtered()) {
125 return 0;
Alban Crequy1ce868f2020-02-19 17:07:41 +0100126 }
Alban Crequy1ce868f2020-02-19 17:07:41 +0100127
Brendan Gregg8cd3efd2019-04-07 12:32:53 -0700128 u64 pid_tgid = bpf_get_current_pid_tgid();
129 u32 pid = pid_tgid >> 32;
130 u32 tid = pid_tgid;
chantra52938052016-09-10 09:44:50 -0700131 FILTER_PID
Brendan Greggf06d3b42015-10-15 17:21:32 -0700132
Takuma Kumeb181a8e2019-01-10 05:49:59 +0900133 u32 uid = bpf_get_current_uid_gid();
134 FILTER_UID
135
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800136 // stash the sock ptr for lookup on return
Brendan Gregg8cd3efd2019-04-07 12:32:53 -0700137 currsock.update(&tid, &sk);
Brendan Greggf06d3b42015-10-15 17:21:32 -0700138
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800139 return 0;
Brendan Greggf06d3b42015-10-15 17:21:32 -0700140};
141
142static int trace_connect_return(struct pt_regs *ctx, short ipver)
143{
Naveen N. Rao4afa96a2016-05-03 14:54:21 +0530144 int ret = PT_REGS_RC(ctx);
Brendan Gregg8cd3efd2019-04-07 12:32:53 -0700145 u64 pid_tgid = bpf_get_current_pid_tgid();
146 u32 pid = pid_tgid >> 32;
147 u32 tid = pid_tgid;
Brendan Greggf06d3b42015-10-15 17:21:32 -0700148
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800149 struct sock **skpp;
Brendan Gregg8cd3efd2019-04-07 12:32:53 -0700150 skpp = currsock.lookup(&tid);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800151 if (skpp == 0) {
152 return 0; // missed entry
153 }
Brendan Greggf06d3b42015-10-15 17:21:32 -0700154
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800155 if (ret != 0) {
156 // failed to send SYNC packet, may not have populated
157 // socket __sk_common.{skc_rcv_saddr, ...}
Brendan Gregg8cd3efd2019-04-07 12:32:53 -0700158 currsock.delete(&tid);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800159 return 0;
160 }
Brendan Greggf06d3b42015-10-15 17:21:32 -0700161
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800162 // pull in details
163 struct sock *skp = *skpp;
Paul Chaignoneae0acf2017-08-05 23:04:41 +0200164 u16 dport = skp->__sk_common.skc_dport;
Brendan Greggf06d3b42015-10-15 17:21:32 -0700165
chantra52938052016-09-10 09:44:50 -0700166 FILTER_PORT
167
Brendan Gregg24825522016-02-14 16:32:29 -0800168 if (ipver == 4) {
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800169 IPV4_CODE
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800170 } else /* 6 */ {
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800171 IPV6_CODE
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800172 }
Brendan Greggf06d3b42015-10-15 17:21:32 -0700173
Brendan Gregg8cd3efd2019-04-07 12:32:53 -0700174 currsock.delete(&tid);
Brendan Greggf06d3b42015-10-15 17:21:32 -0700175
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800176 return 0;
Brendan Greggf06d3b42015-10-15 17:21:32 -0700177}
178
179int trace_connect_v4_return(struct pt_regs *ctx)
180{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800181 return trace_connect_return(ctx, 4);
Brendan Greggf06d3b42015-10-15 17:21:32 -0700182}
183
184int trace_connect_v6_return(struct pt_regs *ctx)
185{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800186 return trace_connect_return(ctx, 6);
Brendan Greggf06d3b42015-10-15 17:21:32 -0700187}
188"""
189
Nabil Schear33817e62020-10-07 21:58:07 -0700190struct_init = {'ipv4':
191 {'count':
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800192 """
193 struct ipv4_flow_key_t flow_key = {};
194 flow_key.saddr = skp->__sk_common.skc_rcv_saddr;
195 flow_key.daddr = skp->__sk_common.skc_daddr;
196 flow_key.dport = ntohs(dport);
197 ipv4_count.increment(flow_key);""",
Nabil Schear33817e62020-10-07 21:58:07 -0700198 'trace':
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800199 """
200 struct ipv4_data_t data4 = {.pid = pid, .ip = ipver};
201 data4.uid = bpf_get_current_uid_gid();
202 data4.ts_us = bpf_ktime_get_ns() / 1000;
203 data4.saddr = skp->__sk_common.skc_rcv_saddr;
204 data4.daddr = skp->__sk_common.skc_daddr;
205 data4.dport = ntohs(dport);
206 bpf_get_current_comm(&data4.task, sizeof(data4.task));
207 ipv4_events.perf_submit(ctx, &data4, sizeof(data4));"""
208 },
209 'ipv6':
Nabil Schear33817e62020-10-07 21:58:07 -0700210 {'count':
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800211 """
212 struct ipv6_flow_key_t flow_key = {};
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -0500213 bpf_probe_read_kernel(&flow_key.saddr, sizeof(flow_key.saddr),
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800214 skp->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32);
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -0500215 bpf_probe_read_kernel(&flow_key.daddr, sizeof(flow_key.daddr),
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800216 skp->__sk_common.skc_v6_daddr.in6_u.u6_addr32);
217 flow_key.dport = ntohs(dport);
218 ipv6_count.increment(flow_key);""",
Nabil Schear33817e62020-10-07 21:58:07 -0700219 'trace':
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800220 """
221 struct ipv6_data_t data6 = {.pid = pid, .ip = ipver};
222 data6.uid = bpf_get_current_uid_gid();
223 data6.ts_us = bpf_ktime_get_ns() / 1000;
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -0500224 bpf_probe_read_kernel(&data6.saddr, sizeof(data6.saddr),
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800225 skp->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32);
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -0500226 bpf_probe_read_kernel(&data6.daddr, sizeof(data6.daddr),
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800227 skp->__sk_common.skc_v6_daddr.in6_u.u6_addr32);
228 data6.dport = ntohs(dport);
229 bpf_get_current_comm(&data6.task, sizeof(data6.task));
230 ipv6_events.perf_submit(ctx, &data6, sizeof(data6));"""
231 }
Nabil Schear33817e62020-10-07 21:58:07 -0700232 }
233
234# This defines an additional BPF program that instruments udp_recvmsg system
235# call to locate DNS response packets on UDP port 53. When these packets are
236# located, the data is copied to user-space where python will parse them with
237# dnslib.
238#
239# uses a percpu array of length 1 to store the dns_data_t off the stack to
240# allow for a maximum DNS packet length of 512 bytes.
241dns_bpf_text = """
242#include <net/inet_sock.h>
243
244#define MAX_PKT 512
245struct dns_data_t {
246 u8 pkt[MAX_PKT];
247};
248
249BPF_PERF_OUTPUT(dns_events);
250
251// store msghdr pointer captured on syscall entry to parse on syscall return
252BPF_HASH(tbl_udp_msg_hdr, u64, struct msghdr *);
253
254// single element per-cpu array to hold the current event off the stack
255BPF_PERCPU_ARRAY(dns_data,struct dns_data_t,1);
256
257int trace_udp_recvmsg(struct pt_regs *ctx)
258{
259 __u64 pid_tgid = bpf_get_current_pid_tgid();
260 struct sock *sk = (struct sock *)PT_REGS_PARM1(ctx);
261 struct inet_sock *is = inet_sk(sk);
262
263 // only grab port 53 packets, 13568 is ntohs(53)
264 if (is->inet_dport == 13568) {
265 struct msghdr *msghdr = (struct msghdr *)PT_REGS_PARM2(ctx);
266 tbl_udp_msg_hdr.update(&pid_tgid, &msghdr);
267 }
268 return 0;
269}
270
271int trace_udp_ret_recvmsg(struct pt_regs *ctx)
272{
273 __u64 pid_tgid = bpf_get_current_pid_tgid();
274 u32 zero = 0;
275 struct msghdr **msgpp = tbl_udp_msg_hdr.lookup(&pid_tgid);
276 if (msgpp == 0)
277 return 0;
278
279 struct msghdr *msghdr = (struct msghdr *)*msgpp;
280 if (msghdr->msg_iter.type != ITER_IOVEC)
281 goto delete_and_return;
282
283 int copied = (int)PT_REGS_RC(ctx);
284 if (copied < 0)
285 goto delete_and_return;
286 size_t buflen = (size_t)copied;
287
288 if (buflen > msghdr->msg_iter.iov->iov_len)
289 goto delete_and_return;
290
291 if (buflen > MAX_PKT)
292 buflen = MAX_PKT;
293
294 struct dns_data_t *data = dns_data.lookup(&zero);
295 if (!data) // this should never happen, just making the verifier happy
296 return 0;
297
298 void *iovbase = msghdr->msg_iter.iov->iov_base;
299 bpf_probe_read(data->pkt, buflen, iovbase);
300 dns_events.perf_submit(ctx, data, buflen);
301
302delete_and_return:
303 tbl_udp_msg_hdr.delete(&pid_tgid);
304 return 0;
305}
306
307"""
308
309if args.count and args.dns:
310 print("Error: you may not specify -d/--dns with -c/--count.")
311 exit()
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800312
Brendan Greggf06d3b42015-10-15 17:21:32 -0700313# code substitutions
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800314if args.count:
315 bpf_text = bpf_text.replace("IPV4_CODE", struct_init['ipv4']['count'])
316 bpf_text = bpf_text.replace("IPV6_CODE", struct_init['ipv6']['count'])
317else:
318 bpf_text = bpf_text.replace("IPV4_CODE", struct_init['ipv4']['trace'])
319 bpf_text = bpf_text.replace("IPV6_CODE", struct_init['ipv6']['trace'])
320
Brendan Greggf06d3b42015-10-15 17:21:32 -0700321if args.pid:
chantra52938052016-09-10 09:44:50 -0700322 bpf_text = bpf_text.replace('FILTER_PID',
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800323 'if (pid != %s) { return 0; }' % args.pid)
chantra52938052016-09-10 09:44:50 -0700324if args.port:
325 dports = [int(dport) for dport in args.port.split(',')]
326 dports_if = ' && '.join(['dport != %d' % ntohs(dport) for dport in dports])
327 bpf_text = bpf_text.replace('FILTER_PORT',
Mauricio Vásquez884799f2020-10-07 20:11:25 -0500328 'if (%s) { currsock.delete(&tid); return 0; }' % dports_if)
Takuma Kumeb181a8e2019-01-10 05:49:59 +0900329if args.uid:
330 bpf_text = bpf_text.replace('FILTER_UID',
331 'if (uid != %s) { return 0; }' % args.uid)
Alban Crequy32ab8582020-03-22 16:06:44 +0100332bpf_text = filter_by_containers(args) + bpf_text
chantra52938052016-09-10 09:44:50 -0700333
334bpf_text = bpf_text.replace('FILTER_PID', '')
335bpf_text = bpf_text.replace('FILTER_PORT', '')
Takuma Kumeb181a8e2019-01-10 05:49:59 +0900336bpf_text = bpf_text.replace('FILTER_UID', '')
chantra52938052016-09-10 09:44:50 -0700337
Nabil Schear33817e62020-10-07 21:58:07 -0700338if args.dns:
339 bpf_text += dns_bpf_text
340
Nathan Scottcf0792f2018-02-02 16:56:50 +1100341if debug or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800342 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100343 if args.ebpf:
344 exit()
Brendan Greggf06d3b42015-10-15 17:21:32 -0700345
Brendan Gregg24825522016-02-14 16:32:29 -0800346# process event
347def print_ipv4_event(cpu, data, size):
Xiaozhou Liu31563032019-02-14 14:33:58 +0800348 event = b["ipv4_events"].event(data)
Mark Drayton11de2982016-06-26 21:14:44 +0100349 global start_ts
Brendan Gregg24825522016-02-14 16:32:29 -0800350 if args.timestamp:
351 if start_ts == 0:
352 start_ts = event.ts_us
Gary Lin6c793312019-04-18 15:17:56 +0800353 printb(b"%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), nl="")
Takuma Kumeb181a8e2019-01-10 05:49:59 +0900354 if args.print_uid:
Gary Lin6c793312019-04-18 15:17:56 +0800355 printb(b"%-6d" % event.uid, nl="")
Nabil Schear33817e62020-10-07 21:58:07 -0700356 dest_ip = inet_ntop(AF_INET, pack("I", event.daddr)).encode()
357 printb(b"%-6d %-12.12s %-2d %-16s %-16s %-6d %s" % (event.pid,
Yonghong Songebe19512019-01-10 14:54:16 -0800358 event.task, event.ip,
359 inet_ntop(AF_INET, pack("I", event.saddr)).encode(),
Nabil Schear33817e62020-10-07 21:58:07 -0700360 dest_ip, event.dport, print_dns(dest_ip)))
Brendan Gregg9e0b0872016-03-28 12:11:45 -0700361
Brendan Gregg24825522016-02-14 16:32:29 -0800362def print_ipv6_event(cpu, data, size):
Xiaozhou Liu31563032019-02-14 14:33:58 +0800363 event = b["ipv6_events"].event(data)
Mark Drayton11de2982016-06-26 21:14:44 +0100364 global start_ts
Brendan Gregg24825522016-02-14 16:32:29 -0800365 if args.timestamp:
366 if start_ts == 0:
367 start_ts = event.ts_us
Gary Lin6c793312019-04-18 15:17:56 +0800368 printb(b"%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), nl="")
Takuma Kumeb181a8e2019-01-10 05:49:59 +0900369 if args.print_uid:
Gary Lin6c793312019-04-18 15:17:56 +0800370 printb(b"%-6d" % event.uid, nl="")
Nabil Schear33817e62020-10-07 21:58:07 -0700371 dest_ip = inet_ntop(AF_INET6, event.daddr).encode()
372 printb(b"%-6d %-12.12s %-2d %-16s %-16s %-6d %s" % (event.pid,
Yonghong Songebe19512019-01-10 14:54:16 -0800373 event.task, event.ip,
Nabil Schear33817e62020-10-07 21:58:07 -0700374 inet_ntop(AF_INET6, event.saddr).encode(), dest_ip,
375 event.dport, print_dns(dest_ip)))
Brendan Gregg24825522016-02-14 16:32:29 -0800376
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800377def depict_cnt(counts_tab, l3prot='ipv4'):
Nabil Schear33817e62020-10-07 21:58:07 -0700378 for k, v in sorted(counts_tab.items(),
379 key=lambda counts: counts[1].value, reverse=True):
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800380 depict_key = ""
381 if l3prot == 'ipv4':
Nabil Schear33817e62020-10-07 21:58:07 -0700382 depict_key = "%-25s %-25s %-20s" % \
383 ((inet_ntop(AF_INET, pack('I', k.saddr))),
384 inet_ntop(AF_INET, pack('I', k.daddr)), k.dport)
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800385 else:
Nabil Schear33817e62020-10-07 21:58:07 -0700386 depict_key = "%-25s %-25s %-20s" % \
387 ((inet_ntop(AF_INET6, k.saddr)),
388 inet_ntop(AF_INET6, k.daddr), k.dport)
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800389
Nabil Schear33817e62020-10-07 21:58:07 -0700390 print("%s %-10d" % (depict_key, v.value))
391
392def print_dns(dest_ip):
393 if not args.dns:
394 return b""
395
396 dnsname, timestamp = dns_cache.get(dest_ip, (None, None))
397 if timestamp is not None:
398 diff = datetime.now() - timestamp
399 diff = float(diff.seconds) * 1000 + float(diff.microseconds) / 1000
400 else:
401 diff = 0
402 if dnsname is None:
403 dnsname = b"No DNS Query"
404 if dest_ip == b"127.0.0.1" or dest_ip == b"::1":
405 dnsname = b"localhost"
406 retval = b"%s" % dnsname
407 if diff > DELAY_DNS:
408 retval += b" (%.3fms)" % diff
409 return retval
410
411if args.dns:
412 try:
413 import dnslib
414 from cachetools import TTLCache
415 except ImportError:
416 print("Error: The python packages dnslib and cachetools are required "
417 "to use the -d/--dns option.")
418 print("Install this package with:")
419 print("\t$ pip3 install dnslib cachetools")
420 print(" or")
421 print("\t$ sudo apt-get install python3-dnslib python3-cachetools "
422 "(on Ubuntu 18.04+)")
423 exit(1)
424
425 # 24 hours
426 DEFAULT_TTL = 86400
427
428 # Cache Size in entries
429 DNS_CACHE_SIZE = 10240
430
431 # delay in ms in which to warn users of long delay between the query
432 # and the connect that used the IP
433 DELAY_DNS = 100
434
435 dns_cache = TTLCache(maxsize=DNS_CACHE_SIZE, ttl=DEFAULT_TTL)
436
437 # process event
438 def save_dns(cpu, data, size):
439 event = b["dns_events"].event(data)
440 payload = event.pkt[:size]
441
442 # pass the payload to dnslib for parsing
443 dnspkt = dnslib.DNSRecord.parse(payload)
444 # lets only look at responses
445 if dnspkt.header.qr != 1:
446 return
447 # must be some questions in there
448 if dnspkt.header.q != 1:
449 return
450 # make sure there are answers
451 if dnspkt.header.a == 0 and dnspkt.header.aa == 0:
452 return
453
454 # lop off the trailing .
455 question = ("%s" % dnspkt.q.qname)[:-1].encode('utf-8')
456
457 for answer in dnspkt.rr:
458 # skip all but A and AAAA records
459 if answer.rtype == 1 or answer.rtype == 28:
460 dns_cache[str(answer.rdata).encode('utf-8')] = (question,
461 datetime.now())
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800462
Brendan Greggf06d3b42015-10-15 17:21:32 -0700463# initialize BPF
464b = BPF(text=bpf_text)
465b.attach_kprobe(event="tcp_v4_connect", fn_name="trace_connect_entry")
466b.attach_kprobe(event="tcp_v6_connect", fn_name="trace_connect_entry")
467b.attach_kretprobe(event="tcp_v4_connect", fn_name="trace_connect_v4_return")
468b.attach_kretprobe(event="tcp_v6_connect", fn_name="trace_connect_v6_return")
Nabil Schear33817e62020-10-07 21:58:07 -0700469if args.dns:
470 b.attach_kprobe(event="udp_recvmsg", fn_name="trace_udp_recvmsg")
471 b.attach_kretprobe(event="udp_recvmsg", fn_name="trace_udp_ret_recvmsg")
Brendan Greggf06d3b42015-10-15 17:21:32 -0700472
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800473print("Tracing connect ... Hit Ctrl-C to end")
474if args.count:
Jerome Marchand51671272018-12-19 01:57:24 +0100475 try:
Nabil Schear33817e62020-10-07 21:58:07 -0700476 while True:
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800477 sleep(99999999)
Jerome Marchand51671272018-12-19 01:57:24 +0100478 except KeyboardInterrupt:
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800479 pass
480
481 # header
482 print("\n%-25s %-25s %-20s %-10s" % (
483 "LADDR", "RADDR", "RPORT", "CONNECTS"))
484 depict_cnt(b["ipv4_count"])
485 depict_cnt(b["ipv6_count"], l3prot='ipv6')
486# read events
487else:
488 # header
489 if args.timestamp:
490 print("%-9s" % ("TIME(s)"), end="")
491 if args.print_uid:
492 print("%-6s" % ("UID"), end="")
Nabil Schear33817e62020-10-07 21:58:07 -0700493 print("%-6s %-12s %-2s %-16s %-16s %-6s" % ("PID", "COMM", "IP", "SADDR",
494 "DADDR", "DPORT"), end="")
495 if args.dns:
496 print(" QUERY")
497 else:
498 print()
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800499
500 start_ts = 0
501
502 # read events
503 b["ipv4_events"].open_perf_buffer(print_ipv4_event)
504 b["ipv6_events"].open_perf_buffer(print_ipv6_event)
Nabil Schear33817e62020-10-07 21:58:07 -0700505 if args.dns:
506 b["dns_events"].open_perf_buffer(save_dns)
507 while True:
Xiaozhou Liu9518a5b2019-08-02 01:13:53 +0800508 try:
509 b.perf_buffer_poll()
510 except KeyboardInterrupt:
511 exit()