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
52 null: /* NOTHING */ { $$ = NULL; }
56 $$ = alloc_node(SSF_DCOND, $2);
60 $$ = alloc_node(SSF_SCOND, $2);
64 $$ = alloc_node(SSF_D_GE, $3);
68 $$ = alloc_node(SSF_D_LE, $3);
72 $$ = alloc_node(SSF_NOT, alloc_node(SSF_D_LE, $3));
76 $$ = alloc_node(SSF_NOT, alloc_node(SSF_D_GE, $3));
80 $$ = alloc_node(SSF_DCOND, $3);
84 $$ = alloc_node(SSF_NOT, alloc_node(SSF_DCOND, $3));
89 $$ = alloc_node(SSF_S_GE, $3);
93 $$ = alloc_node(SSF_S_LE, $3);
97 $$ = alloc_node(SSF_NOT, alloc_node(SSF_S_LE, $3));
101 $$ = alloc_node(SSF_NOT, alloc_node(SSF_S_GE, $3));
105 $$ = alloc_node(SSF_SCOND, $3);
109 $$ = alloc_node(SSF_NOT, alloc_node(SSF_SCOND, $3));
111 | DEVNAME '=' DEVCOND
113 $$ = alloc_node(SSF_DEVCOND, $3);
115 | DEVNAME NEQ DEVCOND
117 $$ = alloc_node(SSF_NOT, alloc_node(SSF_DEVCOND, $3));
119 | FWMARK '=' MARKMASK
121 $$ = alloc_node(SSF_MARKMASK, $3);
123 | FWMARK NEQ MARKMASK
125 $$ = alloc_node(SSF_NOT, alloc_node(SSF_MARKMASK, $3));
129 $$ = alloc_node(SSF_S_AUTO, NULL);
133 $$ = alloc_node(SSF_OR, $1);
138 $$ = alloc_node(SSF_AND, $1);
144 $$ = alloc_node(SSF_AND, $1);
149 $$ = alloc_node(SSF_NOT, $2);
158 static char *get_token_from_line(char **ptr)
160 char *tok, *cp = *ptr;
162 while (*cp == ' ' || *cp == '\t') cp++;
171 while (*cp != 0 && *cp != ' ' && *cp != '\t') {
172 /* Backslash escapes everything. */
175 for (tp = cp; tp != tok; tp--)
192 static char argbuf[1024];
193 static char *tokptr = argbuf;
198 while (*tokptr == 0) {
200 if (argc < yy_argc) {
201 tokptr = yy_argv[argc];
204 while (tokptr == NULL) {
205 if (fgets(argbuf, sizeof(argbuf)-1, yy_fp) == NULL)
207 argbuf[sizeof(argbuf)-1] = 0;
208 if (strlen(argbuf) == sizeof(argbuf) - 1) {
209 fprintf(stderr, "Too long line in filter");
212 if (argbuf[strlen(argbuf)-1] == '\n')
213 argbuf[strlen(argbuf)-1] = 0;
214 if (argbuf[0] == '#' || argbuf[0] == '0')
222 } while ((curtok = get_token_from_line(&tokptr)) == NULL);
224 if (strcmp(curtok, "!") == 0 ||
225 strcmp(curtok, "not") == 0)
227 if (strcmp(curtok, "&") == 0 ||
228 strcmp(curtok, "&&") == 0 ||
229 strcmp(curtok, "and") == 0)
231 if (strcmp(curtok, "|") == 0 ||
232 strcmp(curtok, "||") == 0 ||
233 strcmp(curtok, "or") == 0)
235 if (strcmp(curtok, "(") == 0)
237 if (strcmp(curtok, ")") == 0)
239 if (strcmp(curtok, "dst") == 0) {
243 if (strcmp(curtok, "src") == 0) {
247 if (strcmp(curtok, "dport") == 0) {
251 if (strcmp(curtok, "sport") == 0) {
255 if (strcmp(curtok, "dev") == 0) {
259 if (strcmp(curtok, "fwmark") == 0) {
263 if (strcmp(curtok, ">=") == 0 ||
264 strcmp(curtok, "ge") == 0 ||
265 strcmp(curtok, "geq") == 0)
267 if (strcmp(curtok, "<=") == 0 ||
268 strcmp(curtok, "le") == 0 ||
269 strcmp(curtok, "leq") == 0)
271 if (strcmp(curtok, "!=") == 0 ||
272 strcmp(curtok, "ne") == 0 ||
273 strcmp(curtok, "neq") == 0)
275 if (strcmp(curtok, "=") == 0 ||
276 strcmp(curtok, "==") == 0 ||
277 strcmp(curtok, "eq") == 0)
279 if (strcmp(curtok, ">") == 0 ||
280 strcmp(curtok, "gt") == 0)
282 if (strcmp(curtok, "<") == 0 ||
283 strcmp(curtok, "lt") == 0)
285 if (strcmp(curtok, "autobound") == 0) {
286 tok_type = AUTOBOUND;
289 if (tok_type == DEVNAME) {
290 yylval = (void*)parse_devcond(curtok);
291 if (yylval == NULL) {
292 fprintf(stderr, "Cannot parse device.\n");
297 if (tok_type == FWMARK) {
298 yylval = (void*)parse_markmask(curtok);
299 if (yylval == NULL) {
300 fprintf(stderr, "Cannot parse mark %s.\n", curtok);
305 yylval = (void*)parse_hostcond(curtok, tok_type == SPORT || tok_type == DPORT);
306 if (yylval == NULL) {
307 fprintf(stderr, "Cannot parse dst/src address.\n");
313 int ssfilter_parse(struct ssfilter **f, int argc, char **argv, FILE *fp)
321 fprintf(stderr, " Sorry.\n");