-
Notifications
You must be signed in to change notification settings - Fork 4
/
fakeip.bpf.c
67 lines (54 loc) · 1.64 KB
/
fakeip.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
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_endian.h>
char __license[] SEC("license") = "Dual MIT/GPL";
struct toaData {
u8 opcode;
u8 opsize;
u16 port;
u32 ip;
};
const volatile u8 opcode = 0;
const volatile u16 port = 0;
const volatile u32 ip = 0;
SEC("sockops")
int bpf_sockops_handler(struct bpf_sock_ops *skops){
u32 op = skops->op;
s32 rv = -1;
switch (op) {
case BPF_SOCK_OPS_TCP_CONNECT_CB:
case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: {
//bpf_printk("enter connect cb\n");
bpf_sock_ops_cb_flags_set(skops, skops->bpf_sock_ops_cb_flags | BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG);
break;
}
case BPF_SOCK_OPS_HDR_OPT_LEN_CB: {
//bpf_printk("enter opt len cv\n");
rv = sizeof(struct toaData);
bpf_reserve_hdr_opt(skops, sizeof(struct toaData),0);
break;
}
case BPF_SOCK_OPS_WRITE_HDR_OPT_CB: {
//bpf_printk("enter write hdr opt\n");
//bpf_printk("ip:%d",bpf_htons(ip));
struct toaData fakeToa = {
.opcode = opcode,
.opsize = 0x08,
.port = bpf_htons(port),
.ip = bpf_htonl(ip)
};
int ret = bpf_store_hdr_opt(skops, &fakeToa, sizeof(fakeToa), 0);
if (ret!=0){
bpf_printk("error");
}
break;
}
default:
rv = -1;
}
skops->reply = rv;
return 1;
}