-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattach-reuse-bpf.c
66 lines (56 loc) · 1.48 KB
/
attach-reuse-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
#define _GNU_SOURCE 1
#include <sys/types.h>
#include <sys/socket.h>
#include <dlfcn.h>
#include <unistd.h>
#include "reuse.skel.c"
static struct reuse_kern *reuse_kern;
static int (*old_setsockopt)(int fd, int level, int optname, const void *optval, socklen_t optlen);
int
setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
{
if (level == SOL_SOCKET && optname == SO_REUSEPORT)
{
int rc = old_setsockopt(fd, level, optname, optval, optlen);
if (rc >= 0)
{
write(2, "foo\n", 4);
int bpf_fd = bpf_program__fd(reuse_kern->progs.random_choice);
write(2, "bar\n", 4);
if (bpf_fd < 0)
return -1;
write(2, "baz\n", 4);
if (old_setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_EBPF, &bpf_fd, sizeof(bpf_fd)) < 0)
return -1;
write(2, "bak\n", 4);
// close(bpf_fd);
}
}
return old_setsockopt(fd, level, optname, optval, optlen);
}
static void
_init_wrapped_functions(void)
{
old_setsockopt = dlsym(RTLD_NEXT, "setsockopt");
if (!old_setsockopt)
abort();
}
__attribute__((constructor))
static void
_initialize()
{
reuse_kern = reuse_kern__open_and_load();
if (!reuse_kern)
{
char *msg = "unable to load eBPF program to the kernel\n";
write(2, msg, strlen(msg));
abort();
}
_init_wrapped_functions();
}
__attribute__((destructor))
static void
_deinitialize()
{
reuse_kern__destroy(reuse_kern);
}