forked from zhangmenghao/eBPF-IDS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdp_prog_user.c
338 lines (292 loc) · 9.47 KB
/
xdp_prog_user.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
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
335
336
337
338
/* SPDX-License-Identifier: GPL-2.0 */
static const char *__doc__ = "XDP redirect helper\n"
" - Allows to populate/query tx_port and redirect_params maps\n";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <stdbool.h>
#include <locale.h>
#include <unistd.h>
#include <time.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include <net/if.h>
#include <linux/if_ether.h>
#include <linux/if_link.h> /* depend on kernel-headers installed */
#include "common/common_params.h"
#include "common/common_user_bpf_xdp.h"
#include "common/common_libbpf.h"
#include "common/xdp_stats_kern_user.h"
/* re2dfa and str2dfa library */
#include "common/re2dfa.h"
#include "common/str2dfa.h"
#include "common_kern_user.h"
#define LINE_BUFFER_MAX 160
static const char *ids_inspect_map_name = "ids_inspect_map";
static const char *pattern_file_name = \
// "./patterns/snort2-community-rules-content.txt";
"./patterns/patterns.txt";
static const struct option_wrapper long_options[] = {
{{"help", no_argument, NULL, 'h' },
"Show help", false},
{{"dev", required_argument, NULL, 'd' },
"Operate on device <ifname>", "<ifname>", true},
{{"redirect-dev", required_argument, NULL, 'r' },
"Redirect to device <ifname>", "<ifname>", true},
{{"src-mac", required_argument, NULL, 'L' },
"Source MAC address of <dev>", "<mac>", true },
{{"dest-mac", required_argument, NULL, 'R' },
"Destination MAC address of <redirect-dev>", "<mac>", true },
{{"quiet", no_argument, NULL, 'q' },
"Quiet mode (no output)"},
{{0, 0, NULL, 0 }, NULL, false}
};
/* Follow struct declaration is for fixing the bug of bpf_map_update_elem */
struct ids_inspect_map_update_value {
struct ids_inspect_map_value value;
__u8 padding[8 - sizeof(struct ids_inspect_map_value)];
};
/*
static int re2dfa2map(char *re_string, int map_fd)
{
struct DFA_state *dfa;
struct generic_list state_list;
struct DFA_state **state, *next_state;
struct ids_inspect_map_key map_key;
struct ids_inspect_map_value map_value;
int i_state, n_state;
// Convert the RE string to DFA first
dfa = re2dfa(re_string);
if (!dfa) {
fprintf(stderr, "ERR: can't convert the RE to DFA\n");
return EXIT_FAIL_RE2DFA;
}
// Save all state in DFA into a generic list
create_generic_list(struct DFA_state *, &state_list);
generic_list_push_back(&state_list, &dfa);
DFA_traverse(dfa, &state_list);
// Encode each state
n_state = state_list.length;
state = (struct DFA_state **) state_list.p_dat;
for (i_state = 0; i_state < n_state; i_state++, state++) {
(*state)->state_id = i_state;
}
// Convert dfa to map
state = (struct DFA_state **) state_list.p_dat;
map_key.padding = 0;
map_value.padding = 0;
for (i_state = 0; i_state < n_state; i_state++, state++) {
int i_trans, n_trans = (*state)->n_transitions;
for (i_trans = 0; i_trans < n_trans; i_trans++) {
next_state = (*state)->trans[i_trans].to;
map_key.state = (*state)->state_id;
map_key.unit = (*state)->trans[i_trans].trans_char;
map_value.state = next_state->state_id;
map_value.flag = next_state->flag;
if (bpf_map_update_elem(map_fd, &map_key, &map_value, 0) < 0) {
fprintf(stderr,
"WARN: Failed to update bpf map file: err(%d):%s\n",
errno, strerror(errno));
return -1;
} else {
printf("---------------------------------------------------\n");
printf(
"New element is added in to map (%s)\n",
ids_inspect_map_name);
printf(
"Key - state: %d, unit: %c\n",
map_key.state, map_key.unit);
printf(
"Value - flag: %d, state: %d\n",
map_value.flag, map_value.state);
printf("---------------------------------------------------\n");
}
printf("Insert match (src_state: %d, chars: %d) and action (dst_state: %d)\n", map_key.state, map_key.unit, map_value.state);
}
}
return 0;
}
*/
/*
static int get_number_of_nonblank_lines(const char *source_file) {
FILE *fp;
char buf[LINE_BUFFER_MAX];
int count = 0;
if ((fp = fopen(source_file, "r")) == NULL) {
fprintf(stderr, "ERR: can not open the source file\n");
return 0;
} else {
while (fgets(buf, sizeof(buf), fp)) {
// Skip blank line (only '\n')
if (strlen(buf) > 1) {
count += 1;
}
}
}
fclose(fp);
return count;
}
static int get_pattern_list(const char *source_file, char ***pattern_list) {
FILE *fp;
char buf[LINE_BUFFER_MAX];
char *pattern;
int pattern_len = 0;
int pattern_count = 0;
if ((fp = fopen(source_file, "r")) == NULL) {
fprintf(stderr, "ERR: can not open pattern source file\n");
return -1;
} else {
memset(buf, 0, LINE_BUFFER_MAX);
while (fgets(buf, sizeof(buf), fp)) {
pattern_len = strchr(buf, '\n') - buf;
if (pattern_len == 0) {
// Skip blank line (only '\n')
continue;
}
pattern = (char *)malloc(sizeof(char) * pattern_len);
memset(pattern, 0, pattern_len);
memcpy(pattern, buf, pattern_len);
memset(buf, 0, LINE_BUFFER_MAX);
printf("Get pattern with length %d: %s\n", pattern_len, pattern);
(*pattern_list)[pattern_count++] = pattern;
};
}
printf("Total %d patterns fetched\n", pattern_count);
fclose(fp);
return 0;
};
static int str2dfa2map(char **pattern_list, int pattern_number, int map_fd) {
struct str2dfa_kv *map_entries;
int i_entry, n_entry;
struct ids_inspect_map_key map_key;
struct ids_inspect_map_value map_value;
// Convert string to DFA first
n_entry = str2dfa(pattern_list, pattern_number, &map_entries);
if (n_entry < 0) {
fprintf(stderr, "ERR: can't convert the String to DFA/Map\n");
return -1;
} else {
printf("Totol %d entries generated from pattern list\n", n_entry);
}
// Convert dfa to map
map_key.padding = 0;
map_value.padding = 0;
for (i_entry = 0; i_entry < n_entry; i_entry++) {
map_key.state = map_entries[i_entry].key_state;
map_key.unit = map_entries[i_entry].key_unit;
map_value.state = map_entries[i_entry].value_state;
map_value.flag = map_entries[i_entry].value_flag;
if (bpf_map_update_elem(map_fd, &map_key, &map_value, 0) < 0) {
fprintf(stderr,
"WARN: Failed to update bpf map file: err(%d):%s\n",
errno, strerror(errno));
return -1;
} else {
printf("---------------------------------------------------\n");
printf(
"New element is added in to map (%s)\n",
ids_inspect_map_name);
printf(
"Key - state: %d, unit: %c\n",
map_key.state, map_key.unit);
printf(
"Value - flag: %d, state: %d\n",
map_value.flag, map_value.state);
printf("---------------------------------------------------\n");
}
}
printf("Total entries are inserted: %d\n", n_entry);
return 0;
}
*/
static int str2dfa2map_fromfile(const char *pattern_file, int ids_map_fd) {
struct str2dfa_kv *map_entries;
int i_entry, n_entry;
int i_cpu, n_cpu = libbpf_num_possible_cpus();
struct ids_inspect_map_key ids_map_key;
struct ids_inspect_map_update_value ids_map_values[n_cpu];
ids_inspect_state value_state;
accept_state_flag value_flag;
printf("Number of CPUs: %d\n", n_cpu);
/* Convert string to DFA first */
n_entry = str2dfa_fromfile(pattern_file, &map_entries);
if (n_entry < 0) {
fprintf(stderr, "ERR: can't convert the String to DFA/Map\n");
return -1;
} else {
printf("Totol %d entries generated from pattern list\n", n_entry);
}
/* Initial */
ids_map_key.padding = 0;
memset(ids_map_values, 0, sizeof(ids_map_values));
/* Convert dfa to map */
for (i_entry = 0; i_entry < n_entry; i_entry++) {
ids_map_key.state = map_entries[i_entry].key_state;
ids_map_key.unit = map_entries[i_entry].key_unit;
value_state = map_entries[i_entry].value_state;
value_flag = map_entries[i_entry].value_flag;
for (i_cpu = 0; i_cpu < n_cpu; i_cpu++) {
ids_map_values[i_cpu].value.state = value_state;
ids_map_values[i_cpu].value.flag = value_flag;
}
if (bpf_map_update_elem(ids_map_fd,
&ids_map_key, ids_map_values, 0) < 0) {
fprintf(stderr,
"WARN: Failed to update bpf map file: err(%d):%s\n",
errno, strerror(errno));
return -1;
} else {
printf("---------------------------------------------------\n");
printf(
"New element is added in to map (%s)\n",
ids_inspect_map_name);
printf(
"Key - state: %d, unit: %c\n",
ids_map_key.state, ids_map_key.unit);
printf("Value - state: %d, flag: %d\n", value_state, value_flag);
printf("---------------------------------------------------\n");
}
}
printf("\nTotal entries are inserted: %d\n\n", n_entry);
return 0;
}
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
const char *pin_basedir = "/sys/fs/bpf";
int main(int argc, char **argv)
{
int len;
int ids_map_fd;
char pin_dir[PATH_MAX];
struct config cfg = {
.ifindex = -1,
.redirect_ifindex = -1,
};
/* Cmdline options can change progsec */
parse_cmdline_args(argc, argv, long_options, &cfg, __doc__);
if (cfg.redirect_ifindex > 0 && cfg.ifindex == -1) {
fprintf(stderr, "ERR: required option --dev missing\n\n");
usage(argv[0], __doc__, long_options, (argc == 1));
return EXIT_FAIL_OPTION;
}
len = snprintf(pin_dir, PATH_MAX, "%s/%s", pin_basedir, cfg.ifname);
if (len < 0) {
fprintf(stderr, "ERR: creating pin dirname\n");
return EXIT_FAIL_OPTION;
}
printf("map dir: %s\n", pin_dir);
/* Open the maps corresponding to the cfg.ifname interface */
ids_map_fd = open_bpf_map_file(pin_dir, ids_inspect_map_name, NULL);
if (ids_map_fd < 0) {
return EXIT_FAIL_BPF;
}
/* Convert the string to DFA and map */
if (str2dfa2map_fromfile(pattern_file_name, ids_map_fd) < 0) {
fprintf(stderr, "ERR: can't convert the string to DFA/Map\n");
return EXIT_FAIL_RE2DFA;
}
return EXIT_OK;
}