-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns-trace.c
281 lines (249 loc) · 5.54 KB
/
dns-trace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/* Copyright (c) 2023 Red Hat */
#include <argp.h>
#include <signal.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <sys/resource.h>
#include <bpf/bpf.h>
#include <string.h>
#include "dns-trace.skel.h"
#include "dns-trace.h"
#include <linux/btf.h>
#include <bpf/btf.h>
#include <resolv.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <netinet/in.h>
static struct env {
int interval;
bool verbose;
} env = { 1, 0 };
const char *argp_program_version = "dns-trace 0.0";
const char *argp_program_bug_address = "<[email protected]>";
const char argp_program_doc[] =
"BPF program to trace DNS requests.\n"
"\n"
"USAGE: ./dns-trace [-v]\n";
static const struct argp_option opts[] = {
{ "interval", 'i', "seconds", 0, "Interval between reports" },
{ "verbose", 'v', NULL, 0, "Verbose debug output" },
{},
};
static error_t parse_arg(int key, char *arg, struct argp_state *state)
{
switch (key) {
case 'i':
env.interval = atoi(arg);
break;
case 'v':
env.verbose = true;
break;
case ARGP_KEY_ARG:
argp_usage(state);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static const struct argp argp = {
.options = opts,
.parser = parse_arg,
.doc = argp_program_doc,
};
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
{
if (level == LIBBPF_DEBUG && !env.verbose)
return 0;
return vfprintf(stderr, format, args);
}
static volatile bool exiting = false;
static void sig_handler(int sig)
{
exiting = true;
}
const char *rcode_names[] = {
"NOERROR",
"FORMERR",
"SERVFAIL",
"NXDOMAIN",
"NOTIMP",
"REFUSED",
"YXDOMAIN",
"YXRRSET",
"NXRRSET",
"NOTAUTH",
"NOTZONE",
"ns_r_max"
};
const char * rcode_name(ns_rcode code)
{
static char buffer[10];
if (code > ns_r_max) {
snprintf(buffer, 10, "%d", code);
return buffer;
} else {
return rcode_names[code];
}
}
const char *type_names[] = {
"invalid",
"A",
"NS",
"MD",
"MF",
"CNAME",
"SOA",
"MB",
"MG",
"MR",
"NULL",
"WKS",
"PTR",
"HINFO",
"MINFO",
"MX",
"TXT",
"RP",
"AFSDB",
"X25",
"ISDN",
"RT",
"NSAP",
"NSAP_PTR",
"SIG",
"KEY",
"PX",
"GPOS",
"AAAA",
"LOC",
"NIMLOC",
"SRV"
};
static const char *type_name(ns_type type)
{
static char buffer[10];
if (type > ns_t_srv) {
snprintf(buffer, 10, "%d", type);
return buffer;
} else {
return type_names[type];
}
}
static int print_records(bool q, ns_msg *msg, const char *name, ns_sect section)
{
int i;
for (i = 0; i < ns_msg_count(*msg, section); i++) {
char data[100] = { 0 };
ns_rr rr;
int err = ns_parserr(msg, section, i, &rr);
if (err) {
perror("ns_parserr");
return 1;
}
if (q) {
printf("%s: %5s %s\n",
name, type_name(ns_rr_type(rr)), ns_rr_name(rr));
} else {
const char *rdata = (const char *) ns_rr_rdata(rr);
int rdlen = ns_rr_rdlen(rr);
switch (ns_rr_type(rr)) {
case ns_t_a:
if (rdlen == NS_INADDRSZ)
inet_ntop(AF_INET, rdata, data, sizeof(data));
break;
case ns_t_aaaa:
if (rdlen == NS_IN6ADDRSZ)
inet_ntop(AF_INET6, rdata, data, sizeof(data));
break;
case ns_t_txt:
int len = *rdata++;
strncpy(data, rdata, len);
data[len] = 0;
break;
default:
break;
}
printf("%s: %5s %s [%s] ttl %ds\n",
name,
type_name(ns_rr_type(rr)),
ns_rr_name(rr),
data,
ns_rr_ttl(rr));
}
}
return 0;
}
static int process_event(void *ctx, void *data, size_t len)
{
if (sizeof(struct dns_event) > len) {
fprintf(stderr, "Message too short, discarding\n");
return 1;
}
struct dns_event *event = (struct dns_event *) data;
double latency_ms = ((double)event->duration) / 1000000;
fprintf(stderr, "Received dns event id=%04x flags=%04x latency=%.3fms\n",
event->id, event->flags, latency_ms);
ns_msg msg;
int err = ns_initparse(event->payload, event->length, &msg);
if (err) {
perror("ns_initparse");
return 1;
}
printf("%s – q: %d, a: %d, ns: %d, ar: %d, rcode: %s\n",
ns_msg_getflag(msg, ns_f_qr) ? "Reply" : "Query",
ns_msg_count(msg, ns_s_qd),
ns_msg_count(msg, ns_s_an),
ns_msg_count(msg, ns_s_ns),
ns_msg_count(msg, ns_s_ar),
rcode_name(ns_msg_getflag(msg, ns_f_rcode)));
print_records(true, &msg, " Q", ns_s_qd);
print_records(false, &msg, " A", ns_s_an);
print_records(false, &msg, "NS", ns_s_ns);
print_records(false, &msg, "AR", ns_s_ar);
return 0;
}
int main(int argc, char **argv)
{
struct dns_trace_bpf *skel;
int err;
/* Parse command line arguments */
err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
if (err)
return err;
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
/* Set up libbpf errors and debug info callback */
libbpf_set_print(libbpf_print_fn);
/* Cleaner handling of Ctrl-C */
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
/* Load and verify BPF object file */
skel = dns_trace_bpf__open();
if (!skel) {
fprintf(stderr, "Failed to open and load BPF skeleton\n");
return 1;
}
/* Load & verify BPF programs */
err = dns_trace_bpf__load(skel);
if (err) {
fprintf(stderr, "Failed to load and verify BPF skeleton\n");
goto cleanup;
}
/* Attach tracepoints */
err = dns_trace_bpf__attach(skel);
if (err) {
fprintf(stderr, "Failed to attach BPF skeleton\n");
goto cleanup;
}
struct ring_buffer* ringbuf =
ring_buffer__new(bpf_map__fd(skel->maps.dns_events), process_event, NULL, NULL);
while (!exiting) {
ring_buffer__poll(ringbuf, 100);
}
cleanup:
/* Clean up */
dns_trace_bpf__destroy(skel);
return err < 0 ? -err : 0;
}