-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean-dns.c
184 lines (154 loc) · 4.71 KB
/
clean-dns.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
/* SPDX-License-Identifier: GPL-2.0-or-later */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <net/if.h>
#include <linux/if_arp.h>
#include <linux/if_link.h>
#include <net/if.h> // For if_nametoindex
#include <signal.h>
#include <sys/signalfd.h>
#include <sys/stat.h>
#include <signal.h> // For detecting Ctrl-C
#include <sys/resource.h> // For setting rlmit
#include <limits.h>
#include <bpf/libbpf.h>
#include "clean-dns.h"
#include "clean-dns.kern.skel.h"
int usage(const char *progname)
{
fprintf(stderr, "Usage: %s <ifname> [--unload]\n", progname);
return 1;
}
static int set_rlimit(long int lim)
{
struct rlimit rlim = {
.rlim_cur = lim,
.rlim_max = lim,
};
return !setrlimit(RLIMIT_MEMLOCK, &rlim) ? 0 : -errno;
}
/*
* Simple convenience wrapper around libbpf_strerror for which you don't have
* to provide a buffer. Instead uses its own static buffer and returns a pointer
* to it.
*
* This of course comes with the tradeoff that it is no longer thread safe and
* later invocations overwrite previous results.
*/
static const char *get_libbpf_strerror(int err) {
static char buf[200];
libbpf_strerror(err, buf, sizeof(buf));
return buf;
}
int main(int argc, char *argv[])
{
int err = 0, i, _err, ingress_fd, egress_fd;
int ifindex = 0;
char pin_path[100];
struct clean_dns_kern *skel = NULL;
bool unload = false;
DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .attach_point = BPF_TC_INGRESS | BPF_TC_EGRESS);
// Detect if running as root
if (geteuid() != 0) {
fprintf(stderr, "This program must be run as root.\n");
return EXIT_FAILURE;
}
// Increase rlimit
err = set_rlimit(RLIM_INFINITY);
if (err) {
fprintf(stderr, "Could not set rlimit to infinity: %s\n",
get_libbpf_strerror(err));
return EXIT_FAILURE;
}
if (argc < 2)
return usage(argv[0]);
for (i = 0; i < argc - 1; i++) {
char *ifname = argv[i+1];
if (!strcmp(ifname, "--unload")) {
unload = true;
continue;
}
if (ifindex)
return usage(argv[0]);
ifindex = if_nametoindex(ifname);
if (!ifindex) {
fprintf(stderr, "Couldn't find interface '%s'\n", ifname);
return 1;
}
}
snprintf(pin_path, sizeof(pin_path), "/sys/fs/bpf/clean-dns-%d", ifindex);
pin_path[sizeof(pin_path) - 1] = '\0';
if (unload)
goto unload;
skel = clean_dns_kern__open();
err = libbpf_get_error(skel);
if (err) {
fprintf(stderr, "Couldn't open BPF skeleton: %s\n", strerror(errno));
return err;
}
err = clean_dns_kern__load(skel);
if (err) {
fprintf(stderr, "Failed to load object\n");
goto out;
}
egress_fd = bpf_program__fd(skel->progs.tc_egress);
if (egress_fd < 0) {
fprintf(stderr, "Couldn't find program 'tc_egress'\n");
err = -ENOENT;
goto out;
}
ingress_fd = bpf_program__fd(skel->progs.tc_ingress);
if (ingress_fd < 0) {
fprintf(stderr, "Couldn't find program 'filter_ingress_pkt'\n");
err = -ENOENT;
goto out;
}
DECLARE_LIBBPF_OPTS(bpf_tc_opts, attach_egress,
.prog_fd = egress_fd);
DECLARE_LIBBPF_OPTS(bpf_tc_opts, attach_ingress,
.prog_fd = ingress_fd);
char ifname[IF_NAMESIZE];
if (!if_indextoname(ifindex, ifname)) {
err = -errno;
fprintf(stderr, "Couldn't get ifname for ifindex %d: %s\n", ifindex, strerror(-err));
goto out;
}
hook.ifindex = ifindex;
hook.attach_point = BPF_TC_EGRESS | BPF_TC_INGRESS;
err = bpf_tc_hook_create(&hook);
if (err && err != -EEXIST) {
fprintf(stderr, "Couldn't create egress hook for interface %s\n", ifname);
goto unload;
}
hook.attach_point = BPF_TC_EGRESS;
err = bpf_tc_attach(&hook, &attach_egress);
if (err) {
fprintf(stderr, "Couldn't attach egress program to interface %s: %s\n", ifname, strerror(errno));
goto unload;
}
hook.attach_point = BPF_TC_INGRESS;
err = bpf_tc_attach(&hook, &attach_ingress);
if (err) {
fprintf(stderr, "Couldn't attach ingress program to interface %s: %s\n", ifname, strerror(errno));
goto unload;
}
out:
clean_dns_kern__destroy(skel);
return err;
unload:
hook.ifindex = ifindex;
hook.attach_point = BPF_TC_EGRESS | BPF_TC_INGRESS;
_err = bpf_tc_hook_destroy(&hook);
if (_err) {
fprintf(stderr, "Couldn't remove clsact qdisc on %s\n", ifname);
err = _err;
}
unlink(pin_path);
goto out;
}