Repository navigation
Expand file tree
/
Copy pathipt_trace.c
More file actions
447 lines (389 loc) · 13.4 KB
/
Copy pathipt_trace.c
File metadata and controls
447 lines (389 loc) · 13.4 KB
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#include <bcc/proto.h>
#include <uapi/linux/ip.h>
#include <uapi/linux/ipv6.h>
#include <uapi/linux/tcp.h>
#include <uapi/linux/udp.h>
#include <uapi/linux/icmp.h>
#include <uapi/linux/icmpv6.h>
#include <net/inet_sock.h>
#include <linux/netfilter/x_tables.h>
#define ROUTE_EVT_IF 1
#define ROUTE_EVT_IPTABLE 2
// Event structure
struct route_evt_t {
/* Content flags */
u64 flags;
/* Routing information */
char ifname[IFNAMSIZ];
u64 netns;
/* Packet type (IPv4 or IPv6) and address */
u64 ip_version; // familiy (IPv4 or IPv6)
u64 icmptype;
u64 icmpid; // In practice, this is the PID of the ping process (see "ident" field in https://github.com/iputils/iputils/blob/master/ping_common.c)
u64 icmpseq; // Sequence number
u64 saddr[2]; // Source address. IPv4: store in saddr[0]
u64 daddr[2]; // Dest address. IPv4: store in daddr[0]
// u32 sport; // L4 source port
// u32 dport; // L4 dest port
/* Iptables trace */
u64 hook;
u64 verdict;
char tablename[XT_TABLE_MAXNAMELEN];
};
BPF_PERF_OUTPUT(route_evt);
// Arg stash structure
struct ipt_do_table_args
{
struct sk_buff *skb;
const struct nf_hook_state *state;
struct xt_table *table;
};
BPF_HASH(cur_ipt_do_table_args, u32, struct ipt_do_table_args);
#define MAC_HEADER_SIZE 14;
#define member_address(source_struct, source_member) \
({ \
void* __ret; \
__ret = (void*) (((char*)source_struct) + offsetof(typeof(*source_struct), source_member)); \
__ret; \
})
#define member_read(destination, source_struct, source_member) \
do{ \
bpf_probe_read( \
destination, \
sizeof(source_struct->source_member), \
member_address(source_struct, source_member) \
); \
} while(0)
/**
* Common tracepoint handler. Detect IPv4/IPv6 ICMP echo request and replies and
* emit event with address, interface and namespace.
*/
static inline int do_trace_skb(struct route_evt_t *evt, void *ctx, struct sk_buff *skb)
{
// Prepare event for userland
evt->flags |= ROUTE_EVT_IF;
// Compute MAC header address
char* head;
u16 mac_header;
u16 network_header;
member_read(&head, skb, head);
member_read(&mac_header, skb, mac_header);
member_read(&network_header, skb, network_header);
if(network_header == 0) {
network_header = mac_header + MAC_HEADER_SIZE;
}
// Compute IP Header address
char *ip_header_address = head + network_header;
// Abstract IPv4 / IPv6
u8 proto_icmp;
u8 proto_icmp_echo_request;
u8 proto_icmp_echo_reply;
u8 icmp_offset_from_ip_header;
u8 l4proto;
// Load IP protocol version
bpf_probe_read(&evt->ip_version, sizeof(u8), ip_header_address);
evt->ip_version = evt->ip_version >> 4 & 0xf;
// *********************
// Filter IP packets
if (evt->ip_version == 4) {
// Load IP Header
struct iphdr iphdr;
bpf_probe_read(&iphdr, sizeof(iphdr), ip_header_address);
// Load protocol and address
icmp_offset_from_ip_header = iphdr.ihl * 4;
l4proto = iphdr.protocol;
evt->saddr[0] = iphdr.saddr;
evt->daddr[0] = iphdr.daddr;
// Load constants
proto_icmp = IPPROTO_ICMP;
proto_icmp_echo_request = ICMP_ECHO;
proto_icmp_echo_reply = ICMP_ECHOREPLY;
} else if (evt->ip_version == 6) {
// Assume no option header --> fixed size header
struct ipv6hdr* ipv6hdr = (struct ipv6hdr*)ip_header_address;
icmp_offset_from_ip_header = sizeof(*ipv6hdr);
// Load protocol and address
bpf_probe_read(&l4proto, sizeof(ipv6hdr->nexthdr), (char*)ipv6hdr + offsetof(struct ipv6hdr, nexthdr));
bpf_probe_read(evt->saddr, sizeof(ipv6hdr->saddr), (char*)ipv6hdr + offsetof(struct ipv6hdr, saddr));
bpf_probe_read(evt->daddr, sizeof(ipv6hdr->daddr), (char*)ipv6hdr + offsetof(struct ipv6hdr, daddr));
// Load constants
proto_icmp = IPPROTO_ICMPV6;
proto_icmp_echo_request = ICMPV6_ECHO_REQUEST;
proto_icmp_echo_reply = ICMPV6_ECHO_REPLY;
} else {
// return 0;
}
// Filter ICMP packets
// if (l4proto != proto_icmp) {
// return 0;
// }
// Compute ICMP header address and load ICMP header
char* icmp_header_address = ip_header_address + icmp_offset_from_ip_header;
struct icmphdr icmphdr;
bpf_probe_read(&icmphdr, sizeof(icmphdr), icmp_header_address);
// Filter ICMP echo request and echo reply
// if (icmphdr.type != proto_icmp_echo_request && icmphdr.type != proto_icmp_echo_reply) {
// return 0;
// }
// Get ICMP info
evt->icmptype = icmphdr.type;
evt->icmpid = icmphdr.un.echo.id;
evt->icmpseq = icmphdr.un.echo.sequence;
// Fix endian
evt->icmpid = be16_to_cpu(evt->icmpid);
evt->icmpseq = be16_to_cpu(evt->icmpseq);
// *********************
// // Filter IP packets
// if (evt->ip_version == 4) {
// // Load IP Header
// struct iphdr iphdr;
// bpf_probe_read(&iphdr, sizeof(iphdr), ip_header_address);
//
//
// // Load protocol and address
// offset_from_ip_header = iphdr.ihl * 4;
// l4proto = iphdr.protocol;
// evt->saddr[0] = iphdr.saddr;
// evt->daddr[0] = iphdr.daddr;
//
// switch (l4proto) {
// case IPPROTO_TCP:{
// struct tcphdr tcphdr;
// char* tcp_header_address = ip_header_address + offset_from_ip_header;
// bpf_probe_read(&tcphdr, sizeof(tcphdr), tcp_header_address);
// evt->sport = tcphdr.source;
// evt->dport = tcphdr.dest;
// evt->sport = be16_to_cpu(evt->sport);
// evt->dport = be16_to_cpu(evt->dport);
// evt->icmptype = 100;
// }
// break;
//
// case IPPROTO_UDP:{
// struct udphdr udphdr;
// char* udp_header_address = ip_header_address + offset_from_ip_header;
// bpf_probe_read(&udphdr, sizeof(udphdr), udp_header_address);
// evt->sport = udphdr.source;
// evt->dport = udphdr.dest;
// evt->sport = be16_to_cpu(evt->sport);
// evt->dport = be16_to_cpu(evt->dport);
// }
// break;
//
// case IPPROTO_ICMP:{
// // Load constants
// proto_icmp = IPPROTO_ICMP;
// proto_icmp_echo_request = ICMP_ECHO;
// proto_icmp_echo_reply = ICMP_ECHOREPLY;
//
// // Compute ICMP header address and load ICMP header
// char* icmp_header_address = ip_header_address + offset_from_ip_header;
// struct icmphdr icmphdr;
// bpf_probe_read(&icmphdr, sizeof(icmphdr), icmp_header_address);
//
// // Get ICMP info
// evt->icmptype = icmphdr.type;
// evt->icmpid = icmphdr.un.echo.id;
// evt->icmpseq = icmphdr.un.echo.sequence;
//
// // Fix endian
// evt->icmpid = be16_to_cpu(evt->icmpid);
// evt->icmpseq = be16_to_cpu(evt->icmpseq);
//
//
// }
// break;
//
// default:
// break;
// }
//
//
// } else if (evt->ip_version == 6) {
// // Assume no option header --> fixed size header
// struct ipv6hdr* ipv6hdr = (struct ipv6hdr*)ip_header_address;
// offset_from_ip_header = sizeof(*ipv6hdr);
//
// // Load protocol and address
// bpf_probe_read(&l4proto, sizeof(ipv6hdr->nexthdr), (char*)ipv6hdr + offsetof(struct ipv6hdr, nexthdr));
// bpf_probe_read(evt->saddr, sizeof(ipv6hdr->saddr), (char*)ipv6hdr + offsetof(struct ipv6hdr, saddr));
// bpf_probe_read(evt->daddr, sizeof(ipv6hdr->daddr), (char*)ipv6hdr + offsetof(struct ipv6hdr, daddr));
//
//
// switch (l4proto) {
// case IPPROTO_TCP:{
// struct tcphdr tcphdr;
// char* tcp_header_address = ip_header_address + offset_from_ip_header;
// bpf_probe_read(&tcphdr, sizeof(tcphdr), tcp_header_address);
// // evt->sport = tcphdr.source;
// // evt->dport = tcphdr.dest;
// // evt->sport = be16_to_cpu(evt->sport);
// // evt->dport = be16_to_cpu(evt->dport);
//
// }
// break;
//
// case IPPROTO_UDP:{
// struct udphdr udphdr;
// char* udp_header_address = ip_header_address + offset_from_ip_header;
// bpf_probe_read(&udphdr, sizeof(udphdr), udp_header_address);
// // evt->sport = udphdr.source;
// // evt->dport = udphdr.dest;
// // evt->sport = be16_to_cpu(evt->sport);
// // evt->dport = be16_to_cpu(evt->dport);
// }
// break;
//
// case IPPROTO_ICMPV6:{
// // Load constants
// proto_icmp = IPPROTO_ICMPV6;
// proto_icmp_echo_request = ICMPV6_ECHO_REQUEST;
// proto_icmp_echo_reply = ICMPV6_ECHO_REPLY;
//
// // Compute ICMP header address and load ICMP header
// char* icmp_header_address = ip_header_address + offset_from_ip_header;
// struct icmphdr icmphdr;
// bpf_probe_read(&icmphdr, sizeof(icmphdr), icmp_header_address);
//
// // Get ICMP info
// evt->icmptype = icmphdr.type;
// evt->icmpid = icmphdr.un.echo.id;
// evt->icmpseq = icmphdr.un.echo.sequence;
//
// // Fix endian
// evt->icmpid = be16_to_cpu(evt->icmpid);
// evt->icmpseq = be16_to_cpu(evt->icmpseq);
//
// }
// break;
//
// default:
// break;
//
// }
//
//
// } else {
// return 0;
// }
//
// bpf_trace_printk("flow sport=%d\n", evt->icmptype);
// Get device pointer, we'll need it to get the name and network namespace
struct net_device *dev;
member_read(&dev, skb, dev);
// Load interface name
bpf_probe_read(&evt->ifname, IFNAMSIZ, dev->name);
#ifdef CONFIG_NET_NS
struct net* net;
// Get netns id. The code below is equivalent to: evt->netns = dev->nd_net.net->ns.inum
possible_net_t *skc_net = &dev->nd_net;
member_read(&net, skc_net, net);
struct ns_common* ns = member_address(net, ns);
member_read(&evt->netns, ns, inum);
#endif
return 0;
}
// static inline int do_trace(void *ctx, struct sk_buff *skb)
// {
// // Prepare event for userland
// struct route_evt_t evt = {};
//
// // Process packet
// int ret = do_trace_skb(&evt, ctx, skb);
//
// // Send event
// route_evt.perf_submit(ctx, &evt, sizeof(evt));
//
// // Return
// return ret;
// }
//
//
// /**
// * Attach to Kernel Interface Tracepoints
// */
//
// TRACEPOINT_PROBE(net, netif_rx)
// {
// return do_trace(args, (struct sk_buff *)args->skbaddr);
// }
//
// TRACEPOINT_PROBE(net, net_dev_queue)
// {
// return do_trace(args, (struct sk_buff *)args->skbaddr);
// }
//
// TRACEPOINT_PROBE(net, napi_gro_receive_entry)
// {
// return do_trace(args, (struct sk_buff *)args->skbaddr);
// }
//
// TRACEPOINT_PROBE(net, netif_receive_skb_entry)
// {
// return do_trace(args, (struct sk_buff *)args->skbaddr);
// }
/**
* Common iptables functions
*/
static inline int __ipt_do_table_in(struct pt_regs *ctx, struct sk_buff *skb, const struct nf_hook_state *state, struct xt_table *table)
{
u32 pid = bpf_get_current_pid_tgid();
// stash the arguments for use in retprobe
struct ipt_do_table_args args = {
.skb = skb,
.state = state,
.table = table,
};
cur_ipt_do_table_args.update(&pid, &args);
return 0;
};
static inline int __ipt_do_table_out(struct pt_regs * ctx)
{
// Load arguments
u32 pid = bpf_get_current_pid_tgid();
struct ipt_do_table_args *args;
args = cur_ipt_do_table_args.lookup(&pid);
if (args == 0)
{
return 0; // missed entry
}
cur_ipt_do_table_args.delete(&pid);
// Prepare event for userland
struct route_evt_t evt = {
.flags = ROUTE_EVT_IPTABLE,
};
// Load packet information
struct sk_buff *skb = args->skb;
do_trace_skb(&evt, ctx, skb);
// Store the hook
const struct nf_hook_state *state = args->state;
member_read(&evt.hook, state, hook);
// Store the table name
struct xt_table *table = args->table;
member_read(&evt.tablename, table, name);
// Store the verdict
int ret = PT_REGS_RC(ctx);
evt.verdict = ret;
// Send event
route_evt.perf_submit(ctx, &evt, sizeof(evt));
return 0;
}
/**
* Attach to Kernel iptables main function
**/
int kprobe__ipt_do_table(struct pt_regs *ctx, struct sk_buff *skb, const struct nf_hook_state *state, struct xt_table *table)
{
return __ipt_do_table_in(ctx, skb, state, table);
};
int kretprobe__ipt_do_table(struct pt_regs *ctx)
{
return __ipt_do_table_out(ctx);
}
int kprobe__ip6t_do_table(struct pt_regs *ctx, struct sk_buff *skb, const struct nf_hook_state *state, struct xt_table *table)
{
return __ipt_do_table_in(ctx, skb, state, table);
};
int kretprobe__ip6t_do_table(struct pt_regs *ctx)
{
return __ipt_do_table_out(ctx);
}