-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnettrace.c
391 lines (344 loc) · 7.69 KB
/
nettrace.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include <uapi/linux/ptrace.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <linux/types.h>
#include <uapi/linux/if_ether.h>
#include <uapi/linux/tcp.h>
#include <uapi/linux/ip.h>
#include <uapi/linux/udp.h>
#include <uapi/linux/icmp.h>
#include <bcc/proto.h>
#define DO_TRACE(regs, args...) { \
return do_trace(regs, skb, args); \
}
#if defined(NT_ENABLE_DETAIL) || defined(NT_ENABLE_RET) || \
defined(NT_ENABLE_SKB_MODE)
#define NT_ENABLE_ID
#endif
typedef struct sk_buff sk_buff_t;
typedef struct {
u64 ts; /* timestamp */
union {
struct {
u32 saddr;
u32 daddr;
} ip;
} field_l3;
u64 ret_val;
#ifdef NT_ENABLE_ID
u64 id;
#endif
#ifdef NT_ENABLE_DETAIL
char ifname[IFNAMSIZ];
u32 ifindex;
char comm[16];
u32 pid;
u32 cpu;
#endif
#ifdef NT_ENABLE_STACK
u32 stack_id;
#endif
#define field_ip field_l3.ip
#define field_saddr field_ip.saddr
#define field_daddr field_ip.daddr
union {
struct {
u16 sport;
u16 dport;
u32 seq;
u32 ack;
u8 flags;
} tcp;
#define field_tcp field_l4.tcp
#define field_sport field_tcp.sport
#define field_dport field_tcp.dport
#define field_flags field_tcp.flags
struct {
u16 sport;
u16 dport;
} udp;
struct {
u8 type;
u8 code;
u16 seq;
u16 id;
} icmp;
struct {
u16 op;
} arp_ext;
#define field_udp field_l4.udp
} field_l4;
u16 proto_l3;
u8 proto_l4;
u8 func;
#ifdef NT_ENABLE_RET
bool is_ret;
#endif
u8 align0;
} context_t;
typedef struct {
context_t ctx;
bool match;
} ret_context_t;
BPF_PERF_OUTPUT(m_output);
#ifdef NT_ENABLE_RET
BPF_PERCPU_ARRAY(m_rets, ret_context_t, BPF_PH_count);
#endif
#ifdef NT_ENABLE_SKB_MODE
BPF_HASH(m_match, u64, bool);
#endif
#ifdef NT_ENABLE_STACK
BPF_STACK_TRACE(stacks, 2048);
#endif
static inline bool skb_l4_was_set(const struct sk_buff *skb)
{
return skb->transport_header != 0xFFFF &&
skb->transport_header > skb->network_header;
}
static inline bool skb_l2_was_set(const struct sk_buff *skb)
{
return skb->mac_header != 0xFFFF && skb->mac_header;
}
static inline void *get_l2(sk_buff_t *skb)
{
if (skb_l2_was_set(skb))
return skb->head + skb->mac_header;
else
return NULL;
}
static inline void *get_l3(sk_buff_t *skb)
{
if (skb->network_header > skb->mac_header)
return skb->head + skb->network_header;
else if (get_l2(skb))
return get_l2(skb) + ETH_HLEN;
else
return NULL;
}
static inline void *get_l3_send(sk_buff_t *skb)
{
if (skb->network_header)
return skb->head + skb->network_header;
else
return NULL;
}
static inline void *get_l4(sk_buff_t *skb)
{
if (skb_l4_was_set(skb))
return skb->head + skb->transport_header;
void *ip = get_l3(skb);
if (!ip)
return NULL;
u8 hlen = (*(u8*)ip & 0xf) * 4;
return ip + hlen;
}
static inline bool do_filter(context_t *ctx, sk_buff_t *skb)
{
#ifdef NT_ENABLE_SKB_MODE
u64 key = (u64)(void *)skb;
bool *matched = (bool *)(m_match.lookup(&key));
if (matched)
return true;
#endif
bool res = (BPF_PH_filter);
#ifdef NT_ENABLE_SKB_MODE
if (res)
m_match.update(&key, &res);
#endif
return res;
}
static inline int parse_ip(context_t *ctx, sk_buff_t *skb,
struct iphdr *ip)
{
void *l4;
ctx->proto_l4 = ip->protocol;
ctx->field_saddr = ip->saddr;
ctx->field_daddr = ip->daddr;
l4 = get_l4(skb);
switch (ctx->proto_l4) {
case IPPROTO_TCP: {
struct tcphdr *tcp = l4;
ctx->field_flags = ((u8 *)tcp)[13];
ctx->field_sport = tcp->source;
ctx->field_dport = tcp->dest;
ctx->field_l4.tcp.seq = tcp->seq;
ctx->field_l4.tcp.ack = tcp->ack_seq;
break;
}
case IPPROTO_UDP: {
struct udphdr *udp = l4;
ctx->field_sport = udp->source;
ctx->field_dport = udp->dest;
break;
}
case IPPROTO_ICMP: {
struct icmphdr *icmp = l4;
ctx->field_l4.icmp.code = icmp->code;
ctx->field_l4.icmp.type = icmp->type;
ctx->field_l4.icmp.seq = icmp->un.echo.sequence;
ctx->field_l4.icmp.id = icmp->un.echo.id;
break;
}
}
return 0;
}
static inline int parse_sk(context_t *ctx, struct sock *sk,
sk_buff_t *skb)
{
#ifdef CONFIG_CPU_BIG_ENDIAN
u8 proto = *(u8 *)((void *)sk +
offsetof(struct sock, sk_gso_max_segs) - 3);
#else
u8 proto = *(u8 *)((void *)sk +
offsetof(struct sock, sk_gso_max_segs) - 2);
#endif
ctx->field_saddr = sk->sk_rcv_saddr;
ctx->field_daddr = sk->sk_daddr;
ctx->proto_l4 = proto;
switch (proto) {
case IPPROTO_TCP:
case IPPROTO_UDP:
ctx->field_sport = sk->sk_num;
ctx->field_dport = sk->sk_dport;
break;
}
return 0;
}
struct arphdr {
__be16 ar_hrd; /* format of hardware address */
__be16 ar_pro; /* format of protocol address */
unsigned char ar_hln; /* length of hardware address */
unsigned char ar_pln; /* length of protocol address */
__be16 ar_op; /* ARP opcode (command) */
/*
* Ethernet looks like this : This bit is variable sized however...
*/
unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
unsigned char ar_sip[4]; /* sender IP address */
unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
unsigned char ar_tip[4]; /* target IP address */
};
static inline int parse_arp(context_t *ctx, sk_buff_t *skb,
struct arphdr *arp)
{
bpf_probe_read(&ctx->field_saddr, 4, arp->ar_sip);
bpf_probe_read(&ctx->field_daddr, 4, arp->ar_tip);
ctx->field_l4.arp_ext.op = arp->ar_op;
return 0;
}
static inline int init_ctx(context_t *ctx, sk_buff_t *skb)
{
struct ethhdr *eth = get_l2(skb);
struct sock *sk;
void *l3;
if (!eth)
goto on_send;
ctx->proto_l3 = eth->h_proto;
l3 = get_l3(skb);
switch (ctx->proto_l3) {
case htons(ETH_P_IP):
return parse_ip(ctx, skb, l3);
case htons(ETH_P_ARP):
return parse_arp(ctx, skb, l3);
default:
return 0;
}
on_send:
sk = skb->sk;
if (!sk || sk->sk_family != PF_INET)
return 0;
ctx->proto_l3 = htons(ETH_P_IP);
l3 = get_l3_send(skb);
if (l3)
return parse_ip(ctx, skb, l3);
return 0;
}
static inline void do_output(void *regs, context_t *ctx)
{
ctx->ts = bpf_ktime_get_ns();
m_output.perf_submit(regs, ctx, sizeof(context_t));
}
static inline int do_trace(void *regs, sk_buff_t *skb, u32 func
#ifdef NT_ENABLE_RET
,bool ret, bool ret_only
#endif
#ifdef NT_ENABLE_SKB_MODE
,bool is_end
#endif
#ifdef NT_ENABLE_STACK
,bool stack
#endif
)
{
context_t lctx = {.func = (u8)func}, *ctx = &lctx;
ret_context_t *rctx;
if (!skb)
return 0;
#ifdef NT_ENABLE_RET
if (ret) {
rctx = (ret_context_t *)m_rets.lookup(&func);
if (!rctx)
return 0;
memset(rctx, 0, sizeof(ret_context_t));
ctx = (context_t *)rctx;
ctx->func = (u8) func;
}
#endif
if (init_ctx(ctx, skb))
return 0;
#ifdef NT_ENABLE_ID
ctx->id = (u64)(void *)skb;
#endif
if (!do_filter(ctx, skb))
return 0;
#ifdef NT_ENABLE_STACK
if (stack)
ctx->stack_id = stacks.get_stackid(regs, 0);
#endif
#ifdef NT_ENABLE_DETAIL
struct task_struct *t = (struct task_struct *)bpf_get_current_task();
ctx->pid = t->pid;
bpf_get_current_comm(&ctx->comm, sizeof(ctx->comm));
if (skb->dev) {
bpf_probe_read_str(ctx->ifname, IFNAMSIZ, skb->dev->name);
ctx->ifindex = skb->dev->ifindex;
} else {
ctx->ifindex = skb->skb_iif;
}
ctx->cpu = bpf_get_smp_processor_id();
#endif
#ifdef NT_ENABLE_SKB_MODE
if (is_end)
m_match.delete(&ctx->id);
#endif
#ifdef NT_ENABLE_RET
if (ret) {
rctx->match = true;
if(!ret_only)
do_output(regs, ctx);
return 0;
}
#endif
do_output(regs, ctx);
return 0;
}
#ifdef NT_ENABLE_RET
static inline int ret_trace(struct pt_regs *regs, u32 func, bool is_clone)
{
ret_context_t *rctx = m_rets.lookup(&func);
if (!rctx || !rctx->match)
return 0;
context_t *ctx = (context_t *)rctx;
u64 ret_val = PT_REGS_RC(regs);
ctx->ret_val = ret_val;
ctx->is_ret = true;
do_output(regs, (context_t *)rctx);
#ifdef NT_ENABLE_SKB_MODE
if (is_clone)
m_match.update(&ret_val, &is_clone);
#endif
rctx->match = false;
return 0;
}
#endif
BPF_PH_function