-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebpf.h
334 lines (294 loc) · 8.3 KB
/
ebpf.h
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
/*
* EBPF header file.
*
* (C) Copyright Pantelis Antoniou <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* (1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* (3)The name of the author may not be used to
* endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Copied and modified from ubpf at:
* [email protected]:iovisor/ubpf.git
*
* Original Copyright Notice:
*
* Copyright 2015 Big Switch Networks, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef EBPF_H
#define EBPF_H
#include <inttypes.h>
#include <stdint.h>
#include <errno.h>
#include <libelf.h>
struct ebpf_vm;
struct ebpf_ctx {
struct ebpf_vm *vm;
void *mem;
void *mem_end;
size_t mem_size;
void *stack;
void *stack_end;
size_t stack_size;
uint64_t *reg;
uint16_t pc;
int errcode;
struct list_head allocs;
};
typedef uint64_t
(*ebpf_callback_func_t)(uint64_t arg0, uint64_t arg1, uint64_t arg2,
uint64_t arg3, uint64_t arg4, struct ebpf_ctx *ctx);
typedef uint64_t
(*ebpf_lazy_func_t)(uint64_t arg0, uint64_t arg1, uint64_t arg2,
uint64_t arg3, uint64_t arg4,
struct ebpf_ctx *ctx, const char *funcname);
struct ebpf_callback {
const char *name;
ebpf_callback_func_t func;
};
struct ebpf_section {
const Elf64_Shdr *shdr;
const void *data;
uint64_t size;
uint64_t offset;
bool text;
};
struct ebpf_unresolved_entry {
struct list_head node;
const char *name;
Elf64_Addr r_offset;
Elf64_Xword r_type;
};
typedef void (*ebpf_debugf_t)(void *arg, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 0)));
struct ebpf_vm {
bool initialized;
const void *elf; /* original ELF file */
size_t elf_size;
size_t total_size;
size_t text_size;
size_t data_size;
size_t bss_size;
size_t code_size;
size_t rodata_size;
size_t code_offset;
size_t rodata_offset;
size_t rwdata_offset;
void *workspace;
const void *text_start; /* where code may execute */
const void *text_end;
const void *rodata_start; /* where loads may happen but not writes */
const void *rodata_end;
void *rwdata_start; /* where both loads and stores are allowed */
void *rwdata_end;
struct list_head unres;
int num_callbacks;
const struct ebpf_callback *callbacks;
ebpf_lazy_func_t lazy_func;
int num_sections;
struct ebpf_section *sections; /* section follow */
ebpf_debugf_t debugf;
void *debugarg;
};
#define ebpf_debug(_vm, _fmt, ...) \
do { \
const struct ebpf_vm *__vm = (_vm); \
if (__vm->debugf) \
__vm->debugf(__vm->debugarg, _fmt, ##__VA_ARGS__); \
} while (0)
int ebpf_setup(struct ebpf_vm *vm, const struct ebpf_callback *callbacks,
ebpf_lazy_func_t lazy_func,
ebpf_debugf_t debugf, void *debugarg);
void ebpf_cleanup(struct ebpf_vm *vm);
int ebpf_load_elf(struct ebpf_vm *vm, const void *elf, size_t elf_size);
uint64_t ebpf_exec(struct ebpf_vm *vm, void *mem, size_t mem_len,
int *errcode);
bool
ebpf_load_store_check(const struct ebpf_ctx *ctx, const void *addr,
int size, bool store);
static inline bool
ebpf_load_check(const struct ebpf_ctx *ctx, const void *addr, int size)
{
return ebpf_load_store_check(ctx, addr, size, false);
}
static inline bool
ebpf_store_check(const struct ebpf_ctx *ctx, const void *addr, int size)
{
return ebpf_load_store_check(ctx, addr, size, true);
}
static inline uint64_t
ebpf_load64(struct ebpf_ctx *ctx, const void *addr)
{
uint64_t val;
if (!ebpf_load_check(ctx, addr, sizeof(uint64_t))) {
ctx->errcode = -EFAULT;
return 0xdeadbeefdeadbeef;
}
val = *(const uint64_t *)addr;
ebpf_debug(ctx->vm, "%s %p -> 0x%016lx\n", __func__, addr, val);
return val;
}
static inline uint32_t
ebpf_load32(struct ebpf_ctx *ctx, const void *addr)
{
uint32_t val;
if (!ebpf_load_check(ctx, addr, sizeof(uint32_t))) {
ctx->errcode = -EFAULT;
return 0xdeadbeef;
}
val = *(const uint32_t *)addr;
ebpf_debug(ctx->vm, "%s %p -> 0x%08x\n", __func__, addr, val);
return val;
}
static inline uint16_t
ebpf_load16(struct ebpf_ctx *ctx, const void *addr)
{
uint16_t val;
if (!ebpf_load_check(ctx, addr, sizeof(uint16_t))) {
ctx->errcode = -EFAULT;
return 0xdead;
}
val = *(const uint16_t *)addr;
ebpf_debug(ctx->vm, "%s %p -> 0x%04x\n", __func__, addr, val);
return val;
}
static inline uint8_t
ebpf_load8(struct ebpf_ctx *ctx, const void *addr)
{
uint8_t val;
if (!ebpf_load_check(ctx, addr, sizeof(uint8_t))) {
ctx->errcode = -EFAULT;
return 0xde;
}
val = *(const uint8_t *)addr;
ebpf_debug(ctx->vm, "%s %p -> 0x%02x\n", __func__, addr, val);
return val;
}
static inline int
ebpf_strlen(const struct ebpf_ctx *ctx, const char *str)
{
const char *s;
uint8_t c;
s = str;
while (ebpf_load_check(ctx, s, 1)) {
c = *s++;
if (!c)
return s - str;
}
return -EFAULT;
}
static inline int
ebpf_store64(struct ebpf_ctx *ctx, const void *addr, uint64_t val)
{
if (!ebpf_store_check(ctx, addr, sizeof(uint64_t)))
return ctx->errcode = -EFAULT;
*(uint64_t *)addr = val;
return 0;
}
static inline int
ebpf_store32(struct ebpf_ctx *ctx, void *addr, uint32_t val)
{
if (!ebpf_store_check(ctx, addr, sizeof(uint32_t)))
return ctx->errcode = -EFAULT;
*(uint32_t *)addr = val;
return 0;
}
static inline int
ebpf_store16(struct ebpf_ctx *ctx, void *addr, uint16_t val)
{
if (!ebpf_store_check(ctx, addr, sizeof(uint16_t)))
return ctx->errcode = -EFAULT;
*(uint16_t *)addr = val;
return 0;
}
static inline int
ebpf_store8(struct ebpf_ctx *ctx, void *addr, uint8_t val)
{
if (!ebpf_store_check(ctx, addr, sizeof(uint8_t)))
return ctx->errcode = -EFAULT;
*(uint8_t *)addr = val;
return 0;
}
struct ebpf_chunk {
struct list_head node;
bool writeable;
size_t size;
const void *addr;
uint8_t data[0];
};
#define to_ebpf_chunk(_p) container_of(_p, struct ebpf_chunk, data)
/* allocate memory that the filter can access */
static inline void *ebpf_alloc(struct ebpf_ctx *ctx, bool writeable, size_t size)
{
struct ebpf_chunk *c;
size += sizeof(struct ebpf_chunk);
c = malloc(size);
if (!c)
return NULL;
memset(c, 0, size);
c->writeable = writeable;
c->size = size;
c->addr = c->data; /* address is at our data */
list_add_tail(&c->node, &ctx->allocs);
return c->data;
}
/* open read only window for the filter */
static inline int ebpf_open_memory_window(struct ebpf_ctx *ctx, const void *addr, size_t size)
{
struct ebpf_chunk *c;
c = malloc(sizeof(*c));
if (!c)
return -1;
memset(c, 0, sizeof(*c));
c->writeable = false;
c->size = size;
c->addr = addr;
list_add_tail(&c->node, &ctx->allocs);
/* note that no entry is returned */
return 0;
}
static inline void ebpf_free(struct ebpf_ctx *ctx, void *p)
{
struct ebpf_chunk *c;
if (!p)
return;
c = to_ebpf_chunk(p);
list_del(&c->node);
free(c);
}
#endif