-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshow_pkt_info.bpf.c
executable file
·210 lines (171 loc) · 4.9 KB
/
show_pkt_info.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
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
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
/* HIKe Prog Name comes always first */
#define HIKE_PROG_NAME show_pkt_info
/*
* works on IPv6 packets
* assumes that a program (usually, the classifier) has
* already parsed the program up to the network layer
* i.e. cur->nhoff is set
*
* TODO : may be some errors could be handled instead of dropping
* packet, considering that this is a debug tool
*
* TODO : we could improve the printing by buffering different print
* and the making a single print
*
* TODO : print TCP ports
*/
#define HIKE_DEBUG 1
#define REAL
//#define REPL
#include <stddef.h>
#include <linux/in.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/ipv6.h>
#include <linux/udp.h>
#include <linux/tcp.h>
#include <linux/seg6.h>
#include <linux/errno.h>
#ifdef REAL
#include "tb_defs.h"
#include "hike_vm.h"
#include "parse_helpers.h"
#include "ip6_hset.h"
#endif
#ifdef REPL
#define HIKE_DEBUG 1
#include "tb_defs.h"
#include "ip6_hset_repl.h"
#include "mock.h"
#endif
#define LAYER_2 1
#define NET_LAYER 2
#define TRANSP_LAYER 4
/* show_pkt_info ()
*
*
*
* input:
* - ARG1: HIKe Program ID;
* - ARG2: which parts of the packet needs to be printed
* - ARG3: user supplied info
*
*
*/
HIKE_PROG(HIKE_PROG_NAME) {
struct pkt_info *info = hike_pcpu_shmem();
struct hdr_cursor *cur;
union {
struct ethhdr *eth_h;
struct ipv6hdr *ip6h;
struct udphdr *udph;
struct tcphdr *tcph;
#define eth_h hdr.eth_h
#define ip6h hdr.ip6h
#define udph hdr.udph
#define tcph hdr.tcph
} hdr;
int select_layers = HVM_ARG2;
int user_info = HVM_ARG3;
int offset = 0;
int ret;
__u64 display;
__u64 display2;
__u16 short_disp;
if (unlikely(!info))
goto drop;
/* take the reference to the cursor object which has been saved into
* the HIKe per-cpu shared memory
*/
cur = pkt_info_cur(info);
/* no need for checking cur != NULL here */
if (select_layers & LAYER_2) {
eth_h = (struct ethhdr *)cur_header_pointer(ctx, cur, cur->mhoff,
sizeof(*eth_h));
if (unlikely(!eth_h))
goto drop;
display = *((__u64 *)ð_h->h_dest[0]) ;
display = bpf_be64_to_cpu(display) >> 16;
DEBUG_HKPRG_PRINT("Layer 2 dst : %llx",display);
display = *((__u64 *)ð_h->h_source[0]) ;
display = bpf_be64_to_cpu(display) >> 16;
DEBUG_HKPRG_PRINT("Layer 2 src : %llx",display);
}
if (select_layers & NET_LAYER) {
//TODO check that network layer is parsed ad is IPv6
//TODO 2 if not parsed, we could parse it (but we should make sure
// that the program is idempotent on ctx, cur...)
ip6h = (struct ipv6hdr *)cur_header_pointer(ctx, cur, cur->nhoff,
sizeof(*ip6h));
if (unlikely(!ip6h))
goto drop;
display = *((__u64 *)&ip6h->saddr) ;
display = bpf_be64_to_cpu(display);
display2 = *((__u64 *)&ip6h->saddr + 1);
display2 = bpf_be64_to_cpu(display2);
DEBUG_HKPRG_PRINT("Net Layer src : %llx %llx", display, display2);
display = *((__u64 *)&ip6h->daddr);
display = bpf_be64_to_cpu(display);
display2 = *((__u64 *)&ip6h->daddr + 1);
display2 = bpf_be64_to_cpu(display2);
DEBUG_HKPRG_PRINT("Net Layer dst : %llx %llx", display, display2);
}
if (select_layers & TRANSP_LAYER) {
// ipv6_find_hdr is defined in parse_helpers.h
ret = ipv6_find_hdr(ctx, cur, &offset, -1, NULL, NULL);
if (unlikely(ret < 0)) {
switch (ret) {
case -ENOENT:
/* fallthrough */
case -ELOOP:
/* fallthrough */
case -EOPNOTSUPP:
DEBUG_HKPRG_PRINT("No Transport Info; error: %d", ret);
goto out;
default:
DEBUG_HKPRG_PRINT("Unrecoverable error: %d", ret);
goto drop;
}
}
switch (ret) {
case IPPROTO_UDP:
udph = (struct udphdr *)cur_header_pointer(ctx, cur, offset,
sizeof(*udph));
if (unlikely(!udph))
goto drop;
short_disp = *((__u16 *)&udph->source) ;
short_disp = bpf_ntohs(short_disp) ;
DEBUG_HKPRG_PRINT("UDP src: %d",short_disp);
short_disp = *((__u16 *)&udph->dest) ;
short_disp = bpf_ntohs(short_disp) ;
DEBUG_HKPRG_PRINT("UDP dst: %d",short_disp);
break;
case IPPROTO_TCP:
DEBUG_HKPRG_PRINT("Transport: TCP");
break;
default:
DEBUG_HKPRG_PRINT("Transport: %d",ret);
break;
}
}
DEBUG_HKPRG_PRINT("User info : %llu",user_info);
out:
return HIKE_XDP_VM;
drop:
DEBUG_HKPRG_PRINT("drop packet");
return HIKE_XDP_ABORTED;
#undef eth_h
#undef ip6h
#undef udph
#undef tcph
}
//EXPORT_HIKE_PROG(HIKE_PROG_NAME);
EXPORT_HIKE_PROG_3(HIKE_PROG_NAME, __u64, select_layers, __u64, user_info);
/* Export const */
EXPORT_HIKE_CONST(LAYER_2);
EXPORT_HIKE_CONST(NET_LAYER);
EXPORT_HIKE_CONST(TRANSP_LAYER);
#ifdef REAL
char LICENSE[] SEC("license") = "Dual BSD/GPL";
#endif