9 typedef struct ssfilter * ssfilter_t;
11 #define YYSTYPE ssfilter_t
13 static struct ssfilter * alloc_node(int type, void *pred)
15 struct ssfilter *n = malloc(sizeof(*n));
24 static char **yy_argv;
27 static ssfilter_t *yy_ret;
28 static int tok_type = -1;
30 static int yylex(void);
32 static void yyerror(char *s)
34 fprintf(stderr, "ss: bison bellows (while parsing filter): \"%s!\"", s);
39 %token HOSTCOND DCOND SCOND DPORT SPORT LEQ GEQ NEQ AUTOBOUND DEVCOND DEVNAME MARKMASK FWMARK
53 null: /* NOTHING */ { $$ = NULL; }
59 $$ = alloc_node(SSF_OR, $1);
64 $$ = alloc_node(SSF_AND, $1);
69 $$ = alloc_node(SSF_AND, $1);
78 expr: '(' exprlist ')'
84 $$ = alloc_node(SSF_NOT, $2);
88 $$ = alloc_node(SSF_DCOND, $3);
92 $$ = alloc_node(SSF_SCOND, $3);
96 $$ = alloc_node(SSF_D_GE, $3);
100 $$ = alloc_node(SSF_D_LE, $3);
104 $$ = alloc_node(SSF_NOT, alloc_node(SSF_D_LE, $3));
108 $$ = alloc_node(SSF_NOT, alloc_node(SSF_D_GE, $3));
112 $$ = alloc_node(SSF_DCOND, $3);
116 $$ = alloc_node(SSF_NOT, alloc_node(SSF_DCOND, $3));
121 $$ = alloc_node(SSF_S_GE, $3);
125 $$ = alloc_node(SSF_S_LE, $3);
129 $$ = alloc_node(SSF_NOT, alloc_node(SSF_S_LE, $3));
133 $$ = alloc_node(SSF_NOT, alloc_node(SSF_S_GE, $3));
137 $$ = alloc_node(SSF_SCOND, $3);
141 $$ = alloc_node(SSF_NOT, alloc_node(SSF_SCOND, $3));
145 $$ = alloc_node(SSF_DEVCOND, $3);
147 | DEVNAME NEQ DEVCOND
149 $$ = alloc_node(SSF_NOT, alloc_node(SSF_DEVCOND, $3));
153 $$ = alloc_node(SSF_MARKMASK, $3);
155 | FWMARK NEQ MARKMASK
157 $$ = alloc_node(SSF_NOT, alloc_node(SSF_MARKMASK, $3));
161 $$ = alloc_node(SSF_S_AUTO, NULL);
166 static char *get_token_from_line(char **ptr)
168 char *tok, *cp = *ptr;
170 while (*cp == ' ' || *cp == '\t') cp++;
179 while (*cp != 0 && *cp != ' ' && *cp != '\t') {
180 /* Backslash escapes everything. */
183 for (tp = cp; tp != tok; tp--)
200 static char argbuf[1024];
201 static char *tokptr = argbuf;
206 while (*tokptr == 0) {
208 if (argc < yy_argc) {
209 tokptr = yy_argv[argc];
212 while (tokptr == NULL) {
215 if (fgets(argbuf, sizeof(argbuf), yy_fp) == NULL)
218 len = strnlen(argbuf, sizeof(argbuf));
220 fprintf(stderr, "Invalid line\n");
224 if (len >= sizeof(argbuf) - 1) {
225 fprintf(stderr, "Too long line in filter\n");
228 if (argbuf[len - 1] == '\n')
230 if (argbuf[0] == '#' || argbuf[0] == '0')
238 } while ((curtok = get_token_from_line(&tokptr)) == NULL);
240 if (strcmp(curtok, "!") == 0 ||
241 strcmp(curtok, "not") == 0)
243 if (strcmp(curtok, "&") == 0 ||
244 strcmp(curtok, "&&") == 0 ||
245 strcmp(curtok, "and") == 0)
247 if (strcmp(curtok, "|") == 0 ||
248 strcmp(curtok, "||") == 0 ||
249 strcmp(curtok, "or") == 0)
251 if (strcmp(curtok, "(") == 0)
253 if (strcmp(curtok, ")") == 0)
255 if (strcmp(curtok, "dst") == 0) {
259 if (strcmp(curtok, "src") == 0) {
263 if (strcmp(curtok, "dport") == 0) {
267 if (strcmp(curtok, "sport") == 0) {
271 if (strcmp(curtok, "dev") == 0) {
275 if (strcmp(curtok, "fwmark") == 0) {
279 if (strcmp(curtok, ">=") == 0 ||
280 strcmp(curtok, "ge") == 0 ||
281 strcmp(curtok, "geq") == 0)
283 if (strcmp(curtok, "<=") == 0 ||
284 strcmp(curtok, "le") == 0 ||
285 strcmp(curtok, "leq") == 0)
287 if (strcmp(curtok, "!=") == 0 ||
288 strcmp(curtok, "ne") == 0 ||
289 strcmp(curtok, "neq") == 0)
291 if (strcmp(curtok, "=") == 0 ||
292 strcmp(curtok, "==") == 0 ||
293 strcmp(curtok, "eq") == 0)
295 if (strcmp(curtok, ">") == 0 ||
296 strcmp(curtok, "gt") == 0)
298 if (strcmp(curtok, "<") == 0 ||
299 strcmp(curtok, "lt") == 0)
301 if (strcmp(curtok, "autobound") == 0) {
302 tok_type = AUTOBOUND;
305 if (tok_type == DEVNAME) {
306 yylval = (void*)parse_devcond(curtok);
307 if (yylval == NULL) {
308 fprintf(stderr, "Cannot parse device.\n");
313 if (tok_type == FWMARK) {
314 yylval = (void*)parse_markmask(curtok);
315 if (yylval == NULL) {
316 fprintf(stderr, "Cannot parse mark %s.\n", curtok);
321 yylval = (void*)parse_hostcond(curtok, tok_type == SPORT || tok_type == DPORT);
322 if (yylval == NULL) {
323 fprintf(stderr, "Cannot parse dst/src address.\n");
329 int ssfilter_parse(struct ssfilter **f, int argc, char **argv, FILE *fp)
337 fprintf(stderr, " Sorry.\n");