-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter-parser-funcs.c
526 lines (422 loc) · 10.5 KB
/
filter-parser-funcs.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
* xenoeye
*
* Copyright (c) 2023, Vladimir Misyurov
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include "filter.h"
#include "netflow.h"
#include "flow-info.h"
static int
nf_field_to_off(const char *name, unsigned int *off, unsigned int *size)
{
if (0) {
#define FIELD(NAME, DESC, FLDTYPE, FLDID, SIZEMIN, SIZEMAX) \
} else if (strcmp(#NAME, name) == 0) { \
*off = offsetof(struct flow_info, NAME); \
*size = SIZEMAX; \
return 1;
#include "netflow.def"
}
return 0;
}
static int
nf_aggr_field_off_size(struct filter_input *q, unsigned int *off,
unsigned int *size)
{
if (0) {
#define FIELD(NAME, STR, FLD, SCALE) \
} else if (accept_(q, NAME)) { \
return nf_field_to_off(#FLD, off, size);
#include "filter-ag.def"
}
return 0;
}
static int
nf_field_off_size(struct filter_input *in, unsigned int *off,
unsigned int *size, int dir)
{
if (0) {
#define FIELD(NAME, STR, TYPE, SRC, DST) \
} else if (accept_(in, NAME)) { \
if (dir == FILTER_BASIC_DIR_SRC) { \
return nf_field_to_off(#SRC, off, size); \
} else if (dir == FILTER_BASIC_DIR_DST) { \
return nf_field_to_off(#DST, off, size); \
} else { \
if (strcmp(#SRC, #DST) != 0) { \
mkerror(in, "This field requires direction");\
return 0; \
} \
return nf_field_to_off(#SRC, off, size); \
}
#include "filter.def"
}
return 0;
}
/* div */
int
function_div_parse(struct filter_input *in, struct function_div *div,
enum TOKEN_ID *tok)
{
if (accept_(in, DIV)) {
*tok = DIV;
} else if (accept_(in, DIV_L)) {
*tok = DIV_L;
div->is_log = 1;
} else if (accept_(in, DIV_R)) {
*tok = DIV_R;
} else {
return 0;
}
if (!accept_(in, LPAREN)) {
mkerror(in, "Expected '(' after 'div'");
return 0;
}
if (!nf_aggr_field_off_size(in, &div->dividend_off,
&div->dividend_size)) {
mkerror(in, "Expected aggregable field name");
return 0;
}
if (!accept_(in, COMMA)) {
mkerror(in, "Expected ',' after field name");
return 0;
}
if (!nf_aggr_field_off_size(in, &div->divisor_off,
&div->divisor_size)) {
mkerror(in, "Expected aggregable field name after comma");
return 0;
}
if ((*tok == DIV_L) || (*tok == DIV_R)) {
int k;
if (!accept_(in, COMMA)) {
mkerror(in, "Expected ',' after second field name");
return 0;
}
k = in->current_token.data.range.low;
if (!accept_(in, INT_RANGE)) {
mkerror(in, "Expected INT");
return 0;
}
div->k = k;
}
if (!accept_(in, RPAREN)) {
mkerror(in, "Expected ')'");
return 0;
}
return 1;
}
int
function_div(struct filter_input *in, struct filter_expr *e,
enum TOKEN_ID *tok)
{
struct function_div div;
struct filter_basic *fb;
div.is_log = 0;
div.k = 1;
if (!function_div_parse(in, &div, tok)) {
return 0;
}
if (*tok == DIV) {
if (!filter_add_basic_filter(e, FILTER_BASIC_RANGE,
FILTER_BASIC_NAME_DIV,
FILTER_BASIC_DIR_NONE)) {
return 0;
}
} else if (*tok == DIV_L) {
if (!filter_add_basic_filter(e, FILTER_BASIC_RANGE,
FILTER_BASIC_NAME_DIV_L,
FILTER_BASIC_DIR_NONE)) {
return 0;
}
} else if (*tok == DIV_R) {
if (!filter_add_basic_filter(e, FILTER_BASIC_RANGE,
FILTER_BASIC_NAME_DIV_R,
FILTER_BASIC_DIR_NONE)) {
return 0;
}
}
fb = e->filter[e->n - 1].arg;
fb->func_data.div = malloc(sizeof(struct function_div));
if (!fb->func_data.div) {
return 0;
}
*fb->func_data.div = div;
fb->is_func = 1;
return id(in, e, FILTER_BASIC_RANGE);
}
/* min */
static int
parse_nonaggr_field(struct filter_input *in, unsigned int *off,
unsigned int *size)
{
if (accept_(in, SRC)) {
if (!nf_field_off_size(in, off, size, FILTER_BASIC_DIR_SRC)) {
return 0;
}
} else if (accept_(in, DST)) {
if (!nf_field_off_size(in, off, size, FILTER_BASIC_DIR_DST)) {
return 0;
}
} else {
if (!nf_field_off_size(in, off, size, FILTER_BASIC_DIR_BOTH)) {
return 0;
}
}
return 1;
}
int
function_min_parse(struct filter_input *in, struct function_min *min)
{
if (!accept_(in, MIN)) {
return 0;
}
if (!accept_(in, LPAREN)) {
mkerror(in, "Expected '(' after 'min'");
return 0;
}
/* arg1 */
if (!parse_nonaggr_field(in, &min->arg1_off, &min->arg1_size)) {
mkerror(in, "Incorrect field name");
return 0;
}
if (!accept_(in, COMMA)) {
mkerror(in, "Expected ',' after field name");
return 0;
}
/* arg2 */
if (!parse_nonaggr_field(in, &min->arg2_off, &min->arg2_size)) {
mkerror(in, "Incorrect field name after comma");
return 0;
}
if (!accept_(in, RPAREN)) {
mkerror(in, "Expected ')'");
return 0;
}
return 1;
}
int
function_min(struct filter_input *in, struct filter_expr *e)
{
struct function_min min;
struct filter_basic *fb;
if (!function_min_parse(in, &min)) {
return 0;
}
if (!filter_add_basic_filter(e, FILTER_BASIC_RANGE,
FILTER_BASIC_NAME_MIN,
FILTER_BASIC_DIR_NONE)) {
return 0;
}
fb = e->filter[e->n - 1].arg;
fb->func_data.min = malloc(sizeof(struct function_min));
if (!fb->func_data.min) {
return 0;
}
*fb->func_data.min = min;
fb->is_func = 1;
return id(in, e, FILTER_BASIC_RANGE);
}
int
function_mfreq_parse(struct filter_input *in, struct function_mfreq *mfreq)
{
if (!accept_(in, MFREQ)) {
return 0;
}
if (!accept_(in, LPAREN)) {
mkerror(in, "Expected '(' after 'mfreq'");
return 0;
}
/* arg1 */
if (!parse_nonaggr_field(in, &mfreq->arg1_off, &mfreq->arg1_size)) {
mkerror(in, "Incorrect field name");
return 0;
}
if (mfreq->arg1_size > sizeof(uint16_t)) {
LOG("Field is too big for mfreq");
}
if (!accept_(in, COMMA)) {
mkerror(in, "Expected ',' after field name");
return 0;
}
/* arg2 */
if (!parse_nonaggr_field(in, &mfreq->arg2_off, &mfreq->arg2_size)) {
mkerror(in, "Incorrect field name after comma");
return 0;
}
if (mfreq->arg2_size > sizeof(uint16_t)) {
LOG("Field is too big for mfreq");
}
if (!accept_(in, RPAREN)) {
mkerror(in, "Expected ')'");
return 0;
}
/* init freqmap */
mfreq->freqmap = calloc(UINT16_MAX + 1, sizeof(uint64_t));
if (!mfreq->freqmap) {
LOG("calloc() failed");
return 0;
}
return 1;
}
int
function_mfreq(struct filter_input *in, struct filter_expr *e)
{
struct function_mfreq mfreq;
struct filter_basic *fb;
if (!function_mfreq_parse(in, &mfreq)) {
return 0;
}
if (!filter_add_basic_filter(e, FILTER_BASIC_RANGE,
FILTER_BASIC_NAME_MFREQ,
FILTER_BASIC_DIR_NONE)) {
return 0;
}
fb = e->filter[e->n - 1].arg;
fb->func_data.mfreq = malloc(sizeof(struct function_mfreq));
if (!fb->func_data.mfreq) {
return 0;
}
*fb->func_data.mfreq = mfreq;
fb->is_func = 1;
return id(in, e, FILTER_BASIC_RANGE);
}
/* geo */
int
function_geoip_parse(struct filter_input *in, struct function_geoip *geoip,
enum TOKEN_ID *tok)
{
memset(geoip, 0, sizeof(struct function_geoip));
if (0) {
#define DO(FIELD, SIZE) \
} else if (accept_(in, FIELD)) { \
geoip->field = GEOIP_##FIELD; \
*tok = FIELD;
FOR_LIST_OF_GEOIP_FIELDS
#undef DO
} else {
return 0;
}
if (!accept_(in, LPAREN)) {
mkerror(in, "Expected '(' after GeoIP function");
return 0;
}
/* arg */
if (!parse_nonaggr_field(in, &geoip->ip_off, &geoip->ip_size)) {
mkerror(in, "Incorrect field name");
return 0;
}
/* check args? */
if (!accept_(in, RPAREN)) {
mkerror(in, "Expected ')'");
return 0;
}
return 1;
}
int
function_geoip(struct filter_input *in, struct filter_expr *e)
{
struct function_geoip geoip;
struct filter_basic *fb;
enum TOKEN_ID tok;
if (!function_geoip_parse(in, &geoip, &tok)) {
return 0;
}
if (0) {
#define DO(FIELD, SIZE) \
} else if (tok == FIELD) { \
if (!filter_add_basic_filter(e, FILTER_BASIC_STRING, \
FILTER_BASIC_NAME_##FIELD, \
FILTER_BASIC_DIR_NONE)) { \
return 0; \
}
FOR_LIST_OF_GEOIP_FIELDS
#undef DO
} /* end if */
fb = e->filter[e->n - 1].arg;
fb->func_data.geoip = malloc(sizeof(struct function_geoip));
if (!fb->func_data.geoip) {
return 0;
}
*fb->func_data.geoip = geoip;
fb->is_func = 1;
return id(in, e, FILTER_BASIC_STRING);
}
/* as */
int
function_as_parse(struct filter_input *in, struct function_as *as,
enum TOKEN_ID *tok)
{
memset(as, 0, sizeof(struct function_as));
if (accept_(in, ASN)) {
as->num = 1;
*tok = ASN;
} else if (accept_(in, ASD)) {
as->num = 0;
*tok = ASD;
} else {
return 0;
}
if (!accept_(in, LPAREN)) {
mkerror(in, "Expected '(' after AS function");
return 0;
}
/* arg */
if (!parse_nonaggr_field(in, &as->ip_off, &as->ip_size)) {
mkerror(in, "Incorrect field name");
return 0;
}
/* check args? */
if (!accept_(in, RPAREN)) {
mkerror(in, "Expected ')'");
return 0;
}
return 1;
}
int
function_as(struct filter_input *in, struct filter_expr *e)
{
struct function_as as;
struct filter_basic *fb;
enum TOKEN_ID tok;
if (!function_as_parse(in, &as, &tok)) {
return 0;
}
if (tok == ASN) {
if (!filter_add_basic_filter(e, FILTER_BASIC_RANGE,
FILTER_BASIC_NAME_ASN,
FILTER_BASIC_DIR_NONE)) {
return 0;
}
} else {
if (!filter_add_basic_filter(e, FILTER_BASIC_STRING,
FILTER_BASIC_NAME_ASD,
FILTER_BASIC_DIR_NONE)) {
return 0;
}
}
fb = e->filter[e->n - 1].arg;
fb->func_data.as = malloc(sizeof(struct function_as));
if (!fb->func_data.as) {
return 0;
}
*fb->func_data.as = as;
fb->is_func = 1;
if (tok == ASN) {
return id(in, e, FILTER_BASIC_RANGE);
} else {
return id(in, e, FILTER_BASIC_STRING);
}
}