-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsockex1_kern_after.c
110 lines (89 loc) · 2.74 KB
/
sockex1_kern_after.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
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/ip.h>
#include "bpf_helpers.h"
#include <linux/tcp.h>
#include <linux/in.h>
#include <stddef.h>
struct bpf_map_def SEC("maps") my_map = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(long),
.max_entries = 256,
};
struct bpf_map_def SEC("maps") my_map2 = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(long),
.max_entries = 65536,
};
struct bpf_map_def SEC("maps") my_map3 = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(long),
.max_entries = 65536,
};
struct bpf_map_def SEC("maps") my_map4 = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(long),
.max_entries = 256,
};
struct bpf_map_def SEC("maps") my_map5 = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(long),
.max_entries = 65536,
};
struct bpf_map_def SEC("maps") my_map6 = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(long),
.max_entries = 65536,
};
SEC("socket1")
int bpf_prog1(struct __sk_buff *skb)
{
int index = load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol));
long *value;
//char a = load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol));
int index2 = load_half(skb, offsetof(struct ethhdr, h_proto));
long *value2;
int index3 = load_half(skb, ETH_HLEN + offsetof(struct iphdr, id));
long *value3;
int index4 = load_byte(skb, ETH_HLEN);
long *value4;
//Based on index4, calculate the value of the ip header length
int ihl = index4 & 15;
int ihl_bytes = ihl*4;
int index5 = load_half(skb, ETH_HLEN + offsetof(struct iphdr, tot_len));
long * value5;
int index6 = 0;
long * value6;
if(index == IPPROTO_ICMP){
index6 = load_byte(skb, ETH_HLEN+ihl_bytes+8);
}
if (skb->pkt_type != PACKET_OUTGOING)
return 0;
value = bpf_map_lookup_elem(&my_map, &index);
if (value)
__sync_fetch_and_add(value, 1);
value2 = bpf_map_lookup_elem(&my_map2, &index2);
if (value2)
__sync_fetch_and_add(value2, 1);
value3 = bpf_map_lookup_elem(&my_map3, &index3);
if (value3)
__sync_fetch_and_add(value3, 1);
value4 = bpf_map_lookup_elem(&my_map4, &index4);
if(value4)
__sync_fetch_and_add(value4, 1);
value5 = bpf_map_lookup_elem(&my_map5, &index5);
if(value5)
__sync_fetch_and_add(value5, 1);
value6 = bpf_map_lookup_elem(&my_map6, &index6);
if(value6)
__sync_fetch_and_add(value6, 1);
return 0;
}
char _license[] SEC("license") = "GPL";