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
|
%{
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include "ssfilter.h"
typedef struct ssfilter * ssfilter_t;
#define YYSTYPE ssfilter_t
static struct ssfilter * alloc_node(int type, void *pred)
{
struct ssfilter *n;
if (!ssfilter_is_supported(type)) {
fprintf(stderr, "It looks like such filter is not supported! Too old kernel?\n");
exit(-1);
}
n = malloc(sizeof(*n));
if (n == NULL)
abort();
n->type = type;
n->pred = pred;
n->post = NULL;
return n;
}
static char **yy_argv;
static int yy_argc;
static FILE *yy_fp;
static ssfilter_t *yy_ret;
static int tok_type = -1;
static int yylex(void);
static void yyerror(char *s)
{
fprintf(stderr, "ss: bison bellows (while parsing filter): \"%s!\"", s);
}
%}
%token HOSTCOND DCOND SCOND DPORT SPORT LEQ GEQ NEQ AUTOBOUND DEVCOND DEVNAME MARKMASK FWMARK CGROUPCOND CGROUPPATH
%left '|'
%left '&'
%nonassoc '!'
%%
applet: exprlist
{
*yy_ret = $1;
$$ = $1;
}
| null
;
null: /* NOTHING */ { $$ = NULL; }
;
exprlist: expr
| exprlist '|' expr
{
$$ = alloc_node(SSF_OR, $1);
$$->post = $3;
}
| exprlist '&' expr
{
$$ = alloc_node(SSF_AND, $1);
$$->post = $3;
}
| exprlist expr
{
$$ = alloc_node(SSF_AND, $1);
$$->post = $2;
}
;
eq: '='
| /* nothing */
;
expr: '(' exprlist ')'
{
$$ = $2;
}
| '!' expr
{
$$ = alloc_node(SSF_NOT, $2);
}
| DCOND eq HOSTCOND
{
$$ = alloc_node(SSF_DCOND, $3);
}
| SCOND eq HOSTCOND
{
$$ = alloc_node(SSF_SCOND, $3);
}
| DPORT GEQ HOSTCOND
{
$$ = alloc_node(SSF_D_GE, $3);
}
| DPORT LEQ HOSTCOND
{
$$ = alloc_node(SSF_D_LE, $3);
}
| DPORT '>' HOSTCOND
{
$$ = alloc_node(SSF_NOT, alloc_node(SSF_D_LE, $3));
}
| DPORT '<' HOSTCOND
{
$$ = alloc_node(SSF_NOT, alloc_node(SSF_D_GE, $3));
}
| DPORT eq HOSTCOND
{
$$ = alloc_node(SSF_DCOND, $3);
}
| DPORT NEQ HOSTCOND
{
$$ = alloc_node(SSF_NOT, alloc_node(SSF_DCOND, $3));
}
| SPORT GEQ HOSTCOND
{
$$ = alloc_node(SSF_S_GE, $3);
}
| SPORT LEQ HOSTCOND
{
$$ = alloc_node(SSF_S_LE, $3);
}
| SPORT '>' HOSTCOND
{
$$ = alloc_node(SSF_NOT, alloc_node(SSF_S_LE, $3));
}
| SPORT '<' HOSTCOND
{
$$ = alloc_node(SSF_NOT, alloc_node(SSF_S_GE, $3));
}
| SPORT eq HOSTCOND
{
$$ = alloc_node(SSF_SCOND, $3);
}
| SPORT NEQ HOSTCOND
{
$$ = alloc_node(SSF_NOT, alloc_node(SSF_SCOND, $3));
}
| DEVNAME eq DEVCOND
{
$$ = alloc_node(SSF_DEVCOND, $3);
}
| DEVNAME NEQ DEVCOND
{
$$ = alloc_node(SSF_NOT, alloc_node(SSF_DEVCOND, $3));
}
| FWMARK eq MARKMASK
{
$$ = alloc_node(SSF_MARKMASK, $3);
}
| FWMARK NEQ MARKMASK
{
$$ = alloc_node(SSF_NOT, alloc_node(SSF_MARKMASK, $3));
}
| CGROUPPATH eq CGROUPCOND
{
$$ = alloc_node(SSF_CGROUPCOND, $3);
}
| CGROUPPATH NEQ CGROUPCOND
{
$$ = alloc_node(SSF_NOT, alloc_node(SSF_CGROUPCOND, $3));
}
| AUTOBOUND
{
$$ = alloc_node(SSF_S_AUTO, NULL);
}
;
%%
static char *get_token_from_line(char **ptr)
{
char *tok, *cp = *ptr;
while (*cp == ' ' || *cp == '\t') cp++;
if (*cp == 0) {
*ptr = cp;
return NULL;
}
tok = cp;
while (*cp != 0 && *cp != ' ' && *cp != '\t') {
/* Backslash escapes everything. */
if (*cp == '\\') {
char *tp;
for (tp = cp; tp != tok; tp--)
*tp = *(tp-1);
cp++;
tok++;
if (*cp == 0)
break;
}
cp++;
}
if (*cp)
*cp++ = 0;
*ptr = cp;
return tok;
}
int yylex(void)
{
static char argbuf[1024];
static char *tokptr = argbuf;
static int argc;
char *curtok;
do {
while (*tokptr == 0) {
tokptr = NULL;
if (argc < yy_argc) {
tokptr = yy_argv[argc];
argc++;
} else if (yy_fp) {
while (tokptr == NULL) {
size_t len;
if (fgets(argbuf, sizeof(argbuf), yy_fp) == NULL)
return 0;
len = strnlen(argbuf, sizeof(argbuf));
if (len == 0) {
fprintf(stderr, "Invalid line\n");
exit(-1);
}
if (len >= sizeof(argbuf) - 1) {
fprintf(stderr, "Too long line in filter\n");
exit(-1);
}
if (argbuf[len - 1] == '\n')
argbuf[len-1] = 0;
if (argbuf[0] == '#' || argbuf[0] == '0')
continue;
tokptr = argbuf;
}
} else {
return 0;
}
}
} while ((curtok = get_token_from_line(&tokptr)) == NULL);
if (strcmp(curtok, "!") == 0 ||
strcmp(curtok, "not") == 0)
return '!';
if (strcmp(curtok, "&") == 0 ||
strcmp(curtok, "&&") == 0 ||
strcmp(curtok, "and") == 0)
return '&';
if (strcmp(curtok, "|") == 0 ||
strcmp(curtok, "||") == 0 ||
strcmp(curtok, "or") == 0)
return '|';
if (strcmp(curtok, "(") == 0)
return '(';
if (strcmp(curtok, ")") == 0)
return ')';
if (strcmp(curtok, "dst") == 0) {
tok_type = DCOND;
return DCOND;
}
if (strcmp(curtok, "src") == 0) {
tok_type = SCOND;
return SCOND;
}
if (strcmp(curtok, "dport") == 0) {
tok_type = DPORT;
return DPORT;
}
if (strcmp(curtok, "sport") == 0) {
tok_type = SPORT;
return SPORT;
}
if (strcmp(curtok, "dev") == 0) {
tok_type = DEVNAME;
return DEVNAME;
}
if (strcmp(curtok, "fwmark") == 0) {
tok_type = FWMARK;
return FWMARK;
}
if (strcmp(curtok, "cgroup") == 0) {
tok_type = CGROUPPATH;
return CGROUPPATH;
}
if (strcmp(curtok, ">=") == 0 ||
strcmp(curtok, "ge") == 0 ||
strcmp(curtok, "geq") == 0)
return GEQ;
if (strcmp(curtok, "<=") == 0 ||
strcmp(curtok, "le") == 0 ||
strcmp(curtok, "leq") == 0)
return LEQ;
if (strcmp(curtok, "!=") == 0 ||
strcmp(curtok, "ne") == 0 ||
strcmp(curtok, "neq") == 0)
return NEQ;
if (strcmp(curtok, "=") == 0 ||
strcmp(curtok, "==") == 0 ||
strcmp(curtok, "eq") == 0)
return '=';
if (strcmp(curtok, ">") == 0 ||
strcmp(curtok, "gt") == 0)
return '>';
if (strcmp(curtok, "<") == 0 ||
strcmp(curtok, "lt") == 0)
return '<';
if (strcmp(curtok, "autobound") == 0) {
tok_type = AUTOBOUND;
return AUTOBOUND;
}
if (tok_type == DEVNAME) {
yylval = (void*)parse_devcond(curtok);
if (yylval == NULL) {
fprintf(stderr, "Cannot parse device.\n");
exit(1);
}
return DEVCOND;
}
if (tok_type == FWMARK) {
yylval = (void*)parse_markmask(curtok);
if (yylval == NULL) {
fprintf(stderr, "Cannot parse mark %s.\n", curtok);
exit(1);
}
return MARKMASK;
}
if (tok_type == CGROUPPATH) {
yylval = (void*)parse_cgroupcond(curtok);
if (yylval == NULL) {
fprintf(stderr, "Cannot parse cgroup %s.\n", curtok);
exit(1);
}
return CGROUPCOND;
}
yylval = (void*)parse_hostcond(curtok, tok_type == SPORT || tok_type == DPORT);
if (yylval == NULL) {
fprintf(stderr, "Cannot parse dst/src address.\n");
exit(1);
}
return HOSTCOND;
}
int ssfilter_parse(struct ssfilter **f, int argc, char **argv, FILE *fp)
{
yy_argc = argc;
yy_argv = argv;
yy_fp = fp;
yy_ret = f;
if (yyparse()) {
fprintf(stderr, " Sorry.\n");
return -1;
}
return 0;
}
|