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
|
#include <assert.h>
#include <MESA/MESA_prof_load.h>
#include "sce.h"
#include "log.h"
#include "global_metrics.h"
/******************************************************************************
* Struct Metadata
******************************************************************************/
int metadata_isempty(struct metadata *meta)
{
if (meta->write_ref == 0)
{
return 1;
}
else
{
return 0;
}
}
void metadata_copy(struct metadata *dst, struct metadata *src)
{
dst->write_ref++;
dst->session_id = src->session_id;
dst->rehash_index = src->rehash_index;
dst->raw_data = NULL;
dst->raw_len = 0;
dst->l7offset = src->l7offset;
dst->direction = src->direction;
dst->is_ctrl_pkt = src->is_ctrl_pkt;
dst->is_decrypted = src->is_decrypted;
sids_copy(&dst->sids, &src->sids);
route_ctx_copy(&dst->route_ctx, &src->route_ctx);
}
char *memdup(const char *src, int len)
{
if (src == NULL || len == 0)
{
return NULL;
}
char *dst = (char *)calloc(len + 1, sizeof(char));
memcpy(dst, src, len);
return dst;
}
/******************************************************************************
* Struct Session Ctx
******************************************************************************/
struct session_ctx *session_ctx_new()
{
struct session_ctx *session_ctx = (struct session_ctx *)calloc(1, sizeof(struct session_ctx));
assert(session_ctx != NULL);
mutable_array_init(&session_ctx->rule_ids);
return session_ctx;
}
void session_ctx_free(struct session_ctx *session_ctx)
{
if (session_ctx)
{
if (session_ctx->session_addr)
{
free(session_ctx->session_addr);
session_ctx->session_addr = NULL;
}
if (session_ctx->ctrl_packet_header_data)
{
free(session_ctx->ctrl_packet_header_data);
session_ctx->ctrl_packet_header_data = NULL;
}
if (session_ctx->chaining_raw)
{
selected_chaining_destory(session_ctx->chaining_raw);
session_ctx->chaining_raw = NULL;
}
if (session_ctx->chaining_decrypted)
{
selected_chaining_destory(session_ctx->chaining_decrypted);
session_ctx->chaining_decrypted = NULL;
}
free(session_ctx);
session_ctx = 0;
}
}
/******************************************************************************
* Struct SCE Ctx
******************************************************************************/
struct sce_ctx *sce_ctx_create(const char *profile)
{
struct sce_ctx *sce_ctx = (struct sce_ctx *)calloc(1, sizeof(struct sce_ctx));
MESA_load_profile_int_def(profile, "system", "enable_debug", (int *)&(sce_ctx->enable_debug), 0);
MESA_load_profile_int_def(profile, "system", "enable_send_log", (int *)&(sce_ctx->enable_send_log), 0);
MESA_load_profile_int_def(profile, "system", "firewall_sids", (int *)&(sce_ctx->firewall_sids), 1001);
MESA_load_profile_int_def(profile, "system", "nr_worker_threads", (int *)&(sce_ctx->nr_worker_threads), 8);
MESA_load_profile_uint_range(profile, "system", "cpu_affinity_mask", MAX_THREAD_NUM, (unsigned int *)sce_ctx->cpu_affinity_mask);
MESA_load_profile_int_def(profile, "system", "ts_update_interval_ms", (int *)&(sce_ctx->ts_update_interval_ms), 1);
sce_ctx->nr_worker_threads = MIN(sce_ctx->nr_worker_threads, MAX_THREAD_NUM);
CPU_ZERO(&sce_ctx->coremask);
for (int i = 0; i < sce_ctx->nr_worker_threads; i++)
{
int cpu_id = sce_ctx->cpu_affinity_mask[i];
CPU_SET(cpu_id, &sce_ctx->coremask);
}
sce_ctx->ts = timestamp_new(sce_ctx->ts_update_interval_ms);
sce_ctx->metrics = global_metrics_create(profile, sce_ctx->nr_worker_threads);
if (sce_ctx->metrics == NULL)
{
goto error_out;
}
sce_ctx->enforcer = policy_enforcer_create("SCE", profile, sce_ctx->nr_worker_threads);
if (sce_ctx->enforcer == NULL)
{
goto error_out;
}
if (policy_enforcer_register(sce_ctx->enforcer) == -1)
{
goto error_out;
}
sce_ctx->io = packet_io_create(profile, sce_ctx->nr_worker_threads, &sce_ctx->coremask);
if (sce_ctx->io == NULL)
{
goto error_out;
}
return sce_ctx;
error_out:
sce_ctx_destory(sce_ctx);
return NULL;
}
void sce_ctx_destory(struct sce_ctx *sce_ctx)
{
if (sce_ctx)
{
packet_io_destory(sce_ctx->io);
policy_enforcer_destory(sce_ctx->enforcer);
global_metrics_destory(sce_ctx->metrics);
timestamp_free(sce_ctx->ts);
free(sce_ctx);
sce_ctx = NULL;
}
}
|