-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns-trace.bpf.c
238 lines (196 loc) · 6.29 KB
/
dns-trace.bpf.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
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
/* Copyright (c) 2023 Red Hat */
#include <linux/bpf.h>
#include <stdbool.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
#include <bpf/bpf_core_read.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include "dns-trace.h"
#include "bpf-log2.h"
struct dns_header
{
__u16 id;
__u16 flags;
__u16 qdcount;
__u16 ancount;
__u16 nscount;
__u16 arcount;
};
struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 4096);
} dns_events SEC(".maps");
struct request_key {
__u16 id;
};
struct request_val {
__u64 ts;
__u32 srcip;
};
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, 1024);
__type(key, struct request_key);
__type(value, struct request_val);
} requests SEC(".maps");
struct latency_key {
__u32 ip;
__u64 slot;
};
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 256);
__type(key, struct latency_key);
__type(value, __u64);
} latency SEC(".maps");
struct qname_key {
__u32 ip;
unsigned char qname[MAXNAME];
};
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, 256);
__type(key, struct qname_key);
__type(value, __u64);
} query_count SEC(".maps");
struct rcode_key {
__u32 ip;
__u16 rcode;
};
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, 256);
__type(key, struct rcode_key);
__type(value, __u64);
} rcode_count SEC(".maps");
struct sk_buff {
unsigned char *data;
unsigned int len;
} __attribute__((preserve_access_index));
struct trace_event_raw_net_dev_template {
struct sk_buff *skbaddr;
} __attribute__((preserve_access_index));
static inline int do_trace(unsigned char *data, unsigned int len)
{
__u64 initval = 1;
unsigned char *end = data + len;
struct ethhdr eth;
if (data + sizeof(eth) > end)
return 0;
bpf_probe_read_kernel(ð, sizeof(eth), data);
data += sizeof(eth);
if (eth.h_proto != bpf_htons(ETH_P_IP))
return 0;
struct iphdr ip;
if (data + sizeof(ip) > end)
return 0;
bpf_probe_read_kernel(&ip, sizeof(ip), data);
data += sizeof(ip);
if (ip.protocol != IPPROTO_UDP)
return 0;
struct udphdr udp;
if (data + sizeof(udp) > end)
return 0;
bpf_probe_read_kernel(&udp, sizeof(udp), data);
data += sizeof(udp);
if (udp.source == bpf_htons(53) || udp.dest == bpf_htons(53)) {
struct dns_header dns;
if (data + sizeof(dns) > end)
return 0;
bpf_probe_read_kernel(&dns, sizeof(dns), data);
unsigned int length = bpf_ntohs(udp.len) - sizeof(udp);
if (length > MAXMSG) {
length = MAXMSG;
}
if (data + length > end) {
char format[] = "dns-trace: length too long: wanted %d, got %d\n";
bpf_trace_printk(format, sizeof(format), length, end - data);
return 0;
}
struct request_key req_key = {
.id = dns.id
};
struct request_val newval = {};
#ifdef DEBUG
char format[] = "dns-trace: found DNS packet: id=0x%x, flags=0x%04x\n";
bpf_trace_printk(format, sizeof(format), bpf_ntohs(dns.id), bpf_ntohs(dns.flags));
#endif
if ((dns.flags & 0x80) == 0) { /* query */
if (bpf_map_lookup_elem(&requests, &req_key) == NULL) { // only interested in first query packet with id
newval.ts = bpf_ktime_get_ns();
newval.srcip = ip.saddr;
bpf_map_update_elem(&requests, &req_key, &newval, BPF_ANY);
}
} else { /* response */
struct request_val *value = bpf_map_lookup_elem(&requests, &req_key);
if (value != 0 && value->srcip == ip.daddr) { /* first response packet with right dst IP */
struct dns_event *evt = bpf_ringbuf_reserve(&dns_events, sizeof(struct dns_event), 0);
if (!evt) return 1;
evt->id = bpf_ntohs(dns.id);
evt->flags = bpf_ntohs(dns.flags);
evt->duration = bpf_ktime_get_ns() - value->ts;
evt->srcip = bpf_ntohl(ip.daddr);
evt->dstip = bpf_ntohl(ip.saddr);
evt->length = length;
bpf_probe_read_kernel(&evt->payload, length, data);
/* build a log2 latency histogram keyed by resolver IP and slot */
struct latency_key lkey = {};
lkey.ip = evt->srcip;
lkey.slot = bpf_log2l(evt->duration / 1000000);
__u64 *lvalue = bpf_map_lookup_elem(&latency, &lkey);
if (lvalue) {
__sync_fetch_and_add(lvalue, 1);
} else {
bpf_map_update_elem(&latency, &lkey,
&initval, BPF_ANY);
}
/* build a histogram of qname lookups keyed by resolver IP */
struct qname_key qkey = {};
qkey.ip = evt->srcip;
unsigned char *pos = evt->payload + sizeof(struct dns_header);
__u16 i = 0;
for (i = 0; i < MAXNAME; i++) {
if (pos[i] == 0) break;
qkey.qname[i] = pos[i];
}
__u64 *qvalue = bpf_map_lookup_elem(&query_count, &qkey);
if (qvalue) {
__sync_fetch_and_add(qvalue, 1);
} else {
bpf_map_update_elem(&query_count, &qkey,
&initval, BPF_ANY);
}
/* build a histogram of response codes, keyed by resolver IP */
struct rcode_key rkey = {};
rkey.ip = evt->srcip;
rkey.rcode = (dns.flags & 0x0f00) >> 8;
__u64 *rvalue = bpf_map_lookup_elem(&rcode_count, &rkey);
if (rvalue) {
__sync_fetch_and_add(rvalue, 1);
} else {
bpf_map_update_elem(&rcode_count, &rkey,
&initval, BPF_ANY);
}
bpf_ringbuf_submit(evt, 0);
bpf_map_delete_elem(&requests, &req_key);
#ifdef DEBUG
char format[] = "dns-trace: dns_event added to ringbuf\n";
bpf_trace_printk(format, sizeof(format));
#endif
}
}
}
return 0;
}
SEC("tracepoint/net/net_dev_queue")
int trace_net_packets(struct trace_event_raw_net_dev_template *ctx) {
unsigned char *data = BPF_CORE_READ(ctx, skbaddr, data);
unsigned int len = BPF_CORE_READ(ctx, skbaddr, len);
do_trace(data, len);
return BPF_OK;
}
char LICENSE[] SEC("license") = "Dual BSD/GPL";