forked from OISF/suricata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode-store.c
320 lines (285 loc) · 9.87 KB
/
decode-store.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
/* Copyright (C) 2007-2019 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "suricata-common.h"
#include "suricata.h"
#include "conf.h"
#include "decode.h"
#include "decode-store.h"
static inline struct DecodeStore *GetActiveStore(struct DecodeStore *s)
{
while (s && s->is_full) {
s = s->next;
}
return s;
}
static inline struct DecodeStore *GetNewStore(void)
{
struct DecodeStore *s = SCCalloc(1, sizeof(*s));
if (s == NULL)
return NULL;
return s;
}
static inline void Copy(struct DecodeStore *s, const uint8_t *data, size_t data_size)
{
memcpy(s->data+s->data_offset, data, data_size);
s->data_offset += data_size;
s->is_full = (s->data_offset == DECODE_STORE_SIZE - 1);
}
static void PrintInfo(struct DecodeStore *s)
{
SCLogDebug("store %p used %u out of %u", s, s->data_offset, DECODE_STORE_SIZE);
}
static void Append(Packet *p, uint8_t *data, size_t data_size)
{
SCLogDebug("start");
struct DecodeStore *s = GetActiveStore(&p->decode_store);
SCLogDebug("s %p", s);
if (!s) {
s = GetNewStore();
if (!s) {
// SET EVENT?
return;
}
}
SCLogDebug("s %p", s);
if (data_size + s->data_offset < DECODE_STORE_SIZE) {
Copy(s, data, data_size);
SCLogDebug("data fits, done");
PrintInfo(s);
return;
}
SCLogDebug("data does not fit, get additional store");
s->data[s->data_offset] = DECODE_STORE_TYPE_NEXT;
s->is_full = true;
s->next = GetNewStore();
if (!s->next) {
// SET EVENT?
return;
}
s = s->next;
Copy(s, data, data_size);
SCLogDebug("got additional store. Done");
PrintInfo(s);
}
typedef union DecodeStoreBuiltinVLAN {
struct {
uint16_t type:4;
uint16_t value:12;
};
uint8_t x[2];
} DecodeStoreBuiltinVLAN;
typedef union DecodeStoreBuiltinMPLS {
struct {
uint32_t type:4;
uint32_t pad:4;
uint32_t value:24;
};
uint8_t x[4];
} DecodeStoreBuiltinMPLS;
void DecodeStoreAddVLAN(Packet *p, const uint16_t vlan_id)
{
DecodeStoreBuiltinVLAN v = {
.type = DECODE_STORE_TYPE_VLAN, .value = vlan_id
};
Append(p, v.x, sizeof(v.x));
SCLogDebug("id %u added (v.type %u v.value %u)", vlan_id, v.type, v.value);
}
void DecodeStoreAddMPLS(Packet *p, const uint32_t label)
{
DecodeStoreBuiltinMPLS v = {
.type = DECODE_STORE_TYPE_MPLS, .value = label
};
Append(p, v.x, sizeof(v.x));
SCLogDebug("label %u added (v.type %u v.value %u)", label, v.type, v.value);
}
typedef struct DecodeStoreBuiltinTLV {
uint8_t type:4;
uint8_t pad:4;
uint8_t etype;
uint8_t len;
} DecodeStoreBuiltinTLV;
int DecodeStoreAddTLV(Packet *p, uint8_t etype, const uint8_t *data, uint8_t data_len)
{
if (data_len + sizeof(DecodeStoreBuiltinTLV) >= (size_t)DECODE_STORE_SIZE)
return -EINVAL;
const size_t buf_size = sizeof(DecodeStoreBuiltinTLV) + data_len;
uint8_t buf[buf_size];
DecodeStoreBuiltinTLV hdr = {
.type = DECODE_STORE_TYPE_TLV, .pad = 0, .etype = etype, .len = data_len + 3
};
memcpy(buf, &hdr, sizeof(hdr));
memcpy(buf+sizeof(hdr), data, data_len);
Append(p, buf, buf_size);
SCLogDebug("TLV added %u datalen %u total %u", etype, data_len, hdr.len);
return 0;
}
static inline int GetTypeAndSize(const uint8_t *data, uint8_t size, uint8_t *type, uint8_t *out_size)
{
assert(size >= 1);
*type = *data & 0x0f;
*out_size = 0;
switch (*type) {
case DECODE_STORE_TYPE_VLAN:
*out_size = 2;
break;
case DECODE_STORE_TYPE_MPLS:
*out_size = 4;
break;
case DECODE_STORE_TYPE_NEXT:
*out_size = 1;
break;
case DECODE_STORE_TYPE_TLV: {
DecodeStoreBuiltinTLV hdr;
memcpy(&hdr, data, sizeof(hdr));
*out_size = hdr.len;
break;
}
}
if (*out_size > 0 && *out_size <= size)
return 1;
return 0;
}
#define LOOP_TOP(stype) \
int i = 0; \
for (const struct DecodeStore *s = &p->decode_store; \
s != NULL; s = s->next) { \
uint8_t offset = 0; \
while (offset < s->data_offset) { \
uint8_t type, size; \
if (GetTypeAndSize(&s->data[offset], s->data_offset - offset, \
&type, &size) != 1) \
return -1; \
\
if (type == (stype)) { \
if (i == n) {
#define LOOP_BOT \
return 1; \
} \
i++; \
} \
offset += size; \
} \
} \
return 0;
int DecodeStoreGetVLAN(const Packet *p, int n, uint16_t *vlan_id)
{
LOOP_TOP(DECODE_STORE_TYPE_VLAN);
DecodeStoreBuiltinVLAN v;
memcpy(&v, &s->data[offset], sizeof(v));
*vlan_id = v.value;
LOOP_BOT
}
int DecodeStoreGetMPLS(const Packet *p, int n, uint32_t *label)
{
LOOP_TOP(DECODE_STORE_TYPE_MPLS);
DecodeStoreBuiltinMPLS v;
memcpy(&v, &s->data[offset], sizeof(v));
*label = v.value;
LOOP_BOT
}
int DecodeStoreGetTLV(const Packet *p, int n, const uint8_t etype,
uint8_t **data, uint8_t *data_len, uint8_t data_size)
{
LOOP_TOP(DECODE_STORE_TYPE_TLV);
DecodeStoreBuiltinTLV hdr;
memcpy(&hdr, &s->data[offset], sizeof(hdr));
if (hdr.etype == etype) {
if (hdr.len > data_size)
return -ENOBUFS;
memcpy(*data, &s->data[offset + sizeof(hdr)], hdr.len);
*data_len = hdr.len;
}
LOOP_BOT
}
#undef LOOP_TOP
#undef LOOP_BOT
static inline void FreeDynamic(struct DecodeStore *s)
{
while (s != NULL) {
struct DecodeStore *next = s->next;
SCFree(s);
s = next;
}
}
void DecodeStoreCleanup(Packet *p)
{
struct DecodeStore *s = &p->decode_store;
if (s->data_offset == 0) {
return;
}
s->data_offset = 0;
s->is_full = false;
FreeDynamic(s->next);
s->next = NULL;
}
void DecodeStoreIterate(const Packet *p,
void (*Callback)(void *cb_data, const uint8_t type, const uint8_t etype, const uint8_t size, const uint8_t *data),
void *cb_data)
{
for (const struct DecodeStore *s = &p->decode_store; s != NULL; s = s->next) {
uint8_t offset = 0;
while (offset < s->data_offset) {
uint8_t type, size;
if (GetTypeAndSize(&s->data[offset], s->data_offset - offset, &type, &size) != 1)
break;
switch (type) {
case DECODE_STORE_TYPE_MPLS: {
SCLogDebug("MPLS type %d size %u", type, size);
DecodeStoreBuiltinMPLS v;
memcpy(&v, &s->data[offset], sizeof(v));
union {
uint32_t label;
uint8_t data[4];
} u;
u.label = v.value;
Callback(cb_data, type, 0, 4, u.data);
break;
}
case DECODE_STORE_TYPE_VLAN: {
SCLogDebug("VLAN type %d size %u", type, size);
DecodeStoreBuiltinVLAN v;
memcpy(&v, &s->data[offset], sizeof(v));
union {
uint16_t vlan_id;
uint8_t data[2];
} u;
u.vlan_id = v.value;
Callback(cb_data, type, 0, 2, u.data);
break;
}
case DECODE_STORE_TYPE_TLV: {
const uint8_t *data = &s->data[offset];
DecodeStoreBuiltinTLV v;
memcpy(&v, &s->data[offset], sizeof(v));
SCLogDebug("TLV type %d size %u", type, size);
Callback(cb_data, type, v.etype, v.len - 3, data + 3);
break;
}
case DECODE_STORE_TYPE_NEXT:
SCLogNotice("NEXT type %d size %u", type, size);
break;
default:
SCLogNotice("type %d size %u", type, size);
break;
}
offset += size;
}
}
}
// TODO dumper/iterator for building EVE obj
// TODO for TLV have global registery for id's.
// TODO instead of true TLV, use types? E.g. u8, u16, u32, u64? -> these we can print in json w/o special code