forked from neuvector/neuvector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
459 lines (372 loc) · 11.3 KB
/
main.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "debug.h"
#include "apis.h"
#include "utils/helper.h"
#include "utils/rcu_map.h"
extern void *dp_timer_thr(void *args);
extern void *dp_bld_dlp_thr(void *args);
extern void *dp_data_thr(void *args);
extern void dp_ctrl_loop(void);
extern int dp_ctrl_send_json(json_t *root);
extern int dp_ctrl_send_binary(void *data, int len);
extern int dp_ctrl_threat_log(DPMsgThreatLog *log);
extern int dp_ctrl_traffic_log(DPMsgSession *log);
extern int dp_ctrl_connect_report(DPMsgSession *log, int count_session, int count_violate);
extern void dp_ctrl_init_thread_data(void);
extern int dp_data_add_tap(const char *netns, const char *iface, const char *ep_mac, int thr_id);
extern int dp_send_packet(io_ctx_t *context, uint8_t *pkt, int len);
__thread int THREAD_ID;
__thread char THREAD_NAME[32];
#define DEBUG_FILE "/var/log/agent/dp.log"
int g_running;
dp_mnt_shm_t *g_shm;
rcu_map_t g_ep_map;
struct cds_list_head g_subnet4_list;
struct cds_list_head g_subnet6_list;
struct timeval g_now;
int g_dp_threads = 0;
int g_stats_slot = 0;
char *g_in_iface;
pthread_mutex_t g_debug_lock;
io_callback_t g_callback;
io_config_t g_config;
static void dp_signal_dump_policy(int num)
{
int thr_id;
for (thr_id = 0; thr_id < g_dp_threads; thr_id ++) {
dp_data_wait_ctrl_req_thr(CTRL_REQ_DUMP_POLICY, thr_id);
}
}
static void dp_signal_exit(int num)
{
g_running = false;
}
static inline int debug_ts(FILE *logfp)
{
struct timeval now;
struct tm *tm;
if (g_now.tv_sec == 0) {
//gettimeofday(&now, NULL);
time_t t = get_current_time();
tm = localtime((const time_t *)&t);
} else {
now = g_now;
tm = localtime(&now.tv_sec);
}
return fprintf(logfp, "%04d-%02d-%02dT%02d:%02d:%02d|DEBU|%s|",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec, THREAD_NAME);
}
static int debug_stdout(bool print_ts, const char *fmt, va_list args)
{
int len = 0;
pthread_mutex_lock(&g_debug_lock);
if (print_ts) {
len = debug_ts(stdout);
}
len += vprintf(fmt, args);
pthread_mutex_unlock(&g_debug_lock);
return len;
}
int debug_file(bool print_ts, const char *fmt, va_list args)
{
static FILE *logfp = NULL;
if (logfp == NULL) {
logfp = fopen(DEBUG_FILE, "a");
if (logfp != NULL) {
int flags;
if ((flags = fcntl(fileno(logfp), F_GETFL, 0)) == -1) {
flags = 0;
}
fcntl(fileno(logfp), F_SETFL, flags | O_NONBLOCK);
} else {
return debug_stdout(print_ts, fmt, args);
}
}
int len = 0;
pthread_mutex_lock(&g_debug_lock);
if (print_ts) {
len = debug_ts(logfp);
}
len += vfprintf(logfp, fmt, args);
fflush(logfp);
pthread_mutex_unlock(&g_debug_lock);
return len;
}
static void *get_shm(size_t size)
{
int fd;
void *ptr;
fd = shm_open(DP_MNT_SHM_NAME, O_RDWR, S_IRWXU | S_IRWXG);
if (fd < 0) {
return NULL;
}
ptr = mmap(NULL, sizeof(dp_mnt_shm_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED || ptr == NULL) {
close(fd);
return NULL;
}
close(fd);
return ptr;
}
static int net_run(const char *in_iface)
{
pthread_t timer_thr;
pthread_t bld_dlp_thr;
pthread_t dp_thr[MAX_DP_THREADS];
int i, timer_thr_id, bld_dlp_thr_id, thr_id[MAX_DP_THREADS];
DEBUG_FUNC_ENTRY(DBG_INIT);
g_running = true;
signal(SIGTERM, dp_signal_exit);
signal(SIGINT, dp_signal_exit);
signal(SIGQUIT, dp_signal_exit);
signal(SIGUSR1, dp_signal_dump_policy);
// Calculate number of dp threads
if (g_dp_threads == 0) {
g_dp_threads = count_cpu();
}
if (g_dp_threads > MAX_DP_THREADS) {
g_dp_threads = MAX_DP_THREADS;
}
dp_ctrl_init_thread_data();
pthread_create(&timer_thr, NULL, dp_timer_thr, &timer_thr_id);
pthread_create(&bld_dlp_thr, NULL, dp_bld_dlp_thr, &bld_dlp_thr_id);
for (i = 0; i < g_dp_threads; i ++) {
thr_id[i] = i;
pthread_create(&dp_thr[i], NULL, dp_data_thr, &thr_id[i]);
}
if (in_iface != NULL) {
sleep(2);
dp_data_add_tap("/proc/1/ns/net", in_iface, "11:22:33:44:55:66", 0);
}
dp_ctrl_loop();
pthread_join(timer_thr, NULL);
pthread_join(bld_dlp_thr, NULL);
for (i = 0; i < g_dp_threads; i ++) {
pthread_join(dp_thr[i], NULL);
}
return 0;
}
static void help(const char *prog)
{
printf("%s:\n", prog);
printf(" h: help\n");
printf(" d: debug flags\n");
printf(" (none, all, int, error, ctrl, packet, session, timer, tcp, parser, log, ddos, policy, dlp)\n");
printf(" p: pcap file or directory\n");
printf(" s: standalone mode (listen to the control channel)\n");
}
// -- pcap
void init_dummy_ep(io_ep_t *ep);
static struct timeval tv_diff(struct timeval s, struct timeval e)
{
struct timeval d;
if (e.tv_usec < s.tv_usec) {
d.tv_sec = e.tv_sec - s.tv_sec - 1;
d.tv_usec = 1000000 + e.tv_usec - s.tv_usec;
} else {
d.tv_sec = e.tv_sec - s.tv_sec;
d.tv_usec = e.tv_usec - s.tv_usec;
}
return d;
}
#include <pcap/pcap.h>
static int pcap_send_packet(io_ctx_t *context, uint8_t *pkt, int len)
{
DEBUG_PACKET("Send %u\n", len);
return len;
}
static void pcap_packet(char *user, struct pcap_pkthdr *hdr, uint8_t *pkt)
{
io_ctx_t context;
struct timeval last_now = g_now;
context.dp_ctx = NULL;
g_now = hdr->ts;
context.tick = g_now.tv_sec;
context.tap = true;
context.quar = false;
dpi_recv_packet(&context, pkt, hdr->caplen);
struct timeval td = tv_diff(last_now, g_now);
if (td.tv_sec > 0) {
dpi_timeout(g_now.tv_sec);
}
}
static int pcap_run(const char *path)
{
struct stat st;
memset(&st, 0, sizeof(st));
if (lstat(path, &st) == 0) {
if (S_ISDIR(st.st_mode)) {
DIR *dir = opendir(path);
struct dirent *file;
if (dir == NULL) {
return -1;
}
while ((file = readdir(dir)) != NULL) {
if (file->d_name[0] == '.') {
continue;
}
char dir_path[1024];
snprintf(dir_path, sizeof(dir_path), "%s/%s", path, file->d_name);
printf("Enter: %s\n", dir_path);
pcap_run(dir_path);
}
} else {
pcap_t *pcap;
char err[PCAP_ERRBUF_SIZE];
pcap = pcap_open_offline(path, err);
if (pcap == NULL) {
printf("Cannot open pcap file: %s\n", path);
return -1;
}
pcap_loop(pcap, -1, (pcap_handler)pcap_packet, NULL);
pcap_close(pcap);
return 0;
}
}
return -1;
}
int pcap_send_json(json_t *root)
{
return 0;
}
int pcap_threat_log(DPMsgThreatLog *log)
{
return printf("Threat: id=%u action=%s\n",
ntohl(log->ThreatID), debug_action_name(log->Action));
}
int pcap_traffic_log(DPMsgSession *log)
{
return sizeof(*log);
}
int pcap_connect_report(DPMsgSession *log, int count_session, int count_violate)
{
return sizeof(*log);
}
// ---
static int dp_ep_match(struct cds_lfht_node *ht_node, const void *key)
{
io_mac_t *ht_mac = STRUCT_OF(ht_node, io_mac_t, node);
const uint8_t *mac = key;
return memcmp(mac, &ht_mac->mac, sizeof(ht_mac->mac)) == 0 ? 1 : 0;
}
static uint32_t dp_ep_hash(const void *key)
{
return sdbm_hash(key, ETH_ALEN);
}
int main(int argc, char *argv[])
{
char *pcap = NULL;
bool standalone = false;
int arg = 0;
struct rlimit core_limits;
core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &core_limits);
memset(&g_config, 0, sizeof(g_config));
while (arg != -1) {
arg = getopt(argc, argv, "hcd:i:j:n:p:s");
switch (arg) {
case -1:
break;
case 'c':
g_config.enable_cksum = true;
break;
case 'd':
if (strcasecmp(optarg, "none") == 0) {
g_debug_levels = 0;
} else if (optarg[0] == '-') {
g_debug_levels &= ~debug_name2level(optarg + 1);
} else {
g_debug_levels |= debug_name2level(optarg);
}
break;
case 'i':
g_in_iface = strdup(optarg);
g_config.promisc = true;
break;
case 'n':
g_dp_threads = atoi(optarg);
break;
case 'p':
pcap = optarg;
g_config.promisc = true;
break;
case 's':
standalone = true;
break;
case 'h':
default:
help(argv[0]);
exit(-2);
}
}
setlinebuf(stdout);
pthread_mutex_init(&g_debug_lock, NULL);
rcu_map_init(&g_ep_map, 1, offsetof(io_mac_t, node), dp_ep_match, dp_ep_hash);
CDS_INIT_LIST_HEAD(&g_subnet4_list);
CDS_INIT_LIST_HEAD(&g_subnet6_list);
init_dummy_ep(&g_config.dummy_ep);
g_config.dummy_mac.ep = &g_config.dummy_ep;
if (pcap != NULL) {
g_callback.debug = debug_stdout;
g_callback.send_packet = pcap_send_packet;
g_callback.send_ctrl_json = dp_ctrl_send_json;
g_callback.send_ctrl_binary = dp_ctrl_send_binary;
g_callback.threat_log = pcap_threat_log;
g_callback.traffic_log = pcap_traffic_log;
g_callback.connect_report = pcap_connect_report;
dpi_setup(&g_callback, &g_config);
dpi_init(DPI_INIT);
return pcap_run(pcap);
} else if (standalone) {
g_callback.debug = debug_stdout;
g_callback.send_packet = dp_send_packet;
g_callback.send_ctrl_json = dp_ctrl_send_json;
g_callback.send_ctrl_binary = dp_ctrl_send_binary;
g_callback.threat_log = dp_ctrl_threat_log;
g_callback.traffic_log = dp_ctrl_traffic_log;
g_callback.connect_report = dp_ctrl_connect_report;
dpi_setup(&g_callback, &g_config);
g_shm = calloc(1, sizeof(dp_mnt_shm_t));
if (g_shm == NULL) {
DEBUG_INIT("Unable to allocate shared memory.\n");
return -1;
}
int ret = net_run(g_in_iface);
free(g_shm);
return ret;
} else {
g_callback.debug = debug_stdout;
g_callback.send_packet = dp_send_packet;
g_callback.send_ctrl_json = dp_ctrl_send_json;
g_callback.send_ctrl_binary = dp_ctrl_send_binary;
g_callback.threat_log = dp_ctrl_threat_log;
g_callback.traffic_log = dp_ctrl_traffic_log;
g_callback.connect_report = dp_ctrl_connect_report;
dpi_setup(&g_callback, &g_config);
g_shm = get_shm(sizeof(dp_mnt_shm_t));
if (g_shm == NULL) {
DEBUG_INIT("Unable to get shared memory.\n");
return -1;
}
// Start
int ret = net_run(g_in_iface);
munmap(g_shm, sizeof(dp_mnt_shm_t));
return ret;
}
return 0;
}