-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.bpf.c
58 lines (48 loc) · 1.54 KB
/
main.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
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "main.h"
struct {
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
__uint(key_size, sizeof(u32));
__uint(value_size, sizeof(u32));
} events SEC(".maps");
static __always_inline void handle_skb(struct __sk_buff *skb, bool is_ingress) {
struct event_t event = {};
// 通过指针操作解析数据包
void *data_end = (void *)(long)skb->data_end;
void *data = (void *)(long)skb->data;
// 从 IP 首部中过滤协议类型,只处理 ICMP 协议
if ((data + ETH_HLEN + sizeof(struct iphdr)) > data_end)
return;
struct iphdr *ip_hdr = data + ETH_HLEN;
if (ip_hdr->protocol != IPPROTO_ICMP)
return;
// 解析 ICMP 消息
if ((data + ETH_HLEN + sizeof(struct iphdr) + sizeof(struct icmphdr)) > data_end)
return;
struct icmphdr *icmp_hdr = (void *)ip_hdr + sizeof(struct iphdr);
if (is_ingress) {
event.is_ingress = 1;
} else {
event.is_ingress = 0;
}
event.src_addr = ip_hdr->saddr;
event.dst_addr = ip_hdr->daddr;
event.type = icmp_hdr->type;
event.code = icmp_hdr->code;
bpf_perf_event_output(skb, &events, BPF_F_CURRENT_CPU, &event, sizeof(event));
}
SEC("tc")
int on_ingress(struct __sk_buff *skb) {
handle_skb(skb, true);
return TC_ACT_UNSPEC;
}
SEC("tc")
int on_egress(struct __sk_buff *skb) {
handle_skb(skb, false);
return TC_ACT_UNSPEC;
}
char _license[] SEC("license") = "GPL";