1 %{
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <malloc.h>
6 #include <string.h>
7 /* NOTE: Android yacc build rules transform ssfilter.y into ssfilter.h, and
8 * #include "ssfilter.h" gets this file instead of the ssfilter.h in the
9 * source tree. This does not work. #include <ssfilter.h> instead. */
10 #include <ssfilter.h>
11
12 typedef struct ssfilter * ssfilter_t;
13
14 #define YYSTYPE ssfilter_t
15
alloc_node(int type,void * pred)16 static struct ssfilter * alloc_node(int type, void *pred)
17 {
18 struct ssfilter *n = malloc(sizeof(*n));
19 if (n == NULL)
20 abort();
21 n->type = type;
22 n->pred = pred;
23 n->post = NULL;
24 return n;
25 }
26
27 static char **yy_argv;
28 static int yy_argc;
29 static FILE *yy_fp;
30 static ssfilter_t *yy_ret;
31 static int tok_type = -1;
32
33 static int yylex(void);
34
yyerror(char * s)35 static void yyerror(char *s)
36 {
37 fprintf(stderr, "ss: bison bellows (while parsing filter): \"%s!\"", s);
38 }
39
40 %}
41
42 %token HOSTCOND DCOND SCOND DPORT SPORT LEQ GEQ NEQ AUTOBOUND
43 %left '|'
44 %left '&'
45 %nonassoc '!'
46
47 %%
48 applet: null expr
49 {
50 *yy_ret = $2;
51 $$ = $2;
52 }
53 | null
54 ;
55 null: /* NOTHING */ { $$ = NULL; }
56 ;
57 expr: DCOND HOSTCOND
58 {
59 $$ = alloc_node(SSF_DCOND, $2);
60 }
61 | SCOND HOSTCOND
62 {
63 $$ = alloc_node(SSF_SCOND, $2);
64 }
65 | DPORT GEQ HOSTCOND
66 {
67 $$ = alloc_node(SSF_D_GE, $3);
68 }
69 | DPORT LEQ HOSTCOND
70 {
71 $$ = alloc_node(SSF_D_LE, $3);
72 }
73 | DPORT '>' HOSTCOND
74 {
75 $$ = alloc_node(SSF_NOT, alloc_node(SSF_D_LE, $3));
76 }
77 | DPORT '<' HOSTCOND
78 {
79 $$ = alloc_node(SSF_NOT, alloc_node(SSF_D_GE, $3));
80 }
81 | DPORT '=' HOSTCOND
82 {
83 $$ = alloc_node(SSF_DCOND, $3);
84 }
85 | DPORT NEQ HOSTCOND
86 {
87 $$ = alloc_node(SSF_NOT, alloc_node(SSF_DCOND, $3));
88 }
89
90 | SPORT GEQ HOSTCOND
91 {
92 $$ = alloc_node(SSF_S_GE, $3);
93 }
94 | SPORT LEQ HOSTCOND
95 {
96 $$ = alloc_node(SSF_S_LE, $3);
97 }
98 | SPORT '>' HOSTCOND
99 {
100 $$ = alloc_node(SSF_NOT, alloc_node(SSF_S_LE, $3));
101 }
102 | SPORT '<' HOSTCOND
103 {
104 $$ = alloc_node(SSF_NOT, alloc_node(SSF_S_GE, $3));
105 }
106 | SPORT '=' HOSTCOND
107 {
108 $$ = alloc_node(SSF_SCOND, $3);
109 }
110 | SPORT NEQ HOSTCOND
111 {
112 $$ = alloc_node(SSF_NOT, alloc_node(SSF_SCOND, $3));
113 }
114
115 | AUTOBOUND
116 {
117 $$ = alloc_node(SSF_S_AUTO, NULL);
118 }
119 | expr '|' expr
120 {
121 $$ = alloc_node(SSF_OR, $1);
122 $$->post = $3;
123 }
124 | expr expr
125 {
126 $$ = alloc_node(SSF_AND, $1);
127 $$->post = $2;
128 }
129 | expr '&' expr
130
131 {
132 $$ = alloc_node(SSF_AND, $1);
133 $$->post = $3;
134 }
135 | '!' expr
136 {
137 $$ = alloc_node(SSF_NOT, $2);
138 }
139 | '(' expr ')'
140 {
141 $$ = $2;
142 }
143 ;
144 %%
145
146 static char *get_token_from_line(char **ptr)
147 {
148 char *tok, *cp = *ptr;
149
150 while (*cp == ' ' || *cp == '\t') cp++;
151
152 if (*cp == 0) {
153 *ptr = cp;
154 return NULL;
155 }
156
157 tok = cp;
158
159 while (*cp != 0 && *cp != ' ' && *cp != '\t') {
160 /* Backslash escapes everything. */
161 if (*cp == '\\') {
162 char *tp;
163 for (tp = cp; tp != tok; tp--)
164 *tp = *(tp-1);
165 cp++;
166 tok++;
167 if (*cp == 0)
168 break;
169 }
170 cp++;
171 }
172 if (*cp)
173 *cp++ = 0;
174 *ptr = cp;
175 return tok;
176 }
177
yylex(void)178 int yylex(void)
179 {
180 static char argbuf[1024];
181 static char *tokptr = argbuf;
182 static int argc;
183 char *curtok;
184
185 do {
186 while (*tokptr == 0) {
187 tokptr = NULL;
188 if (argc < yy_argc) {
189 tokptr = yy_argv[argc];
190 argc++;
191 } else if (yy_fp) {
192 while (tokptr == NULL) {
193 if (fgets(argbuf, sizeof(argbuf)-1, yy_fp) == NULL)
194 return 0;
195 argbuf[sizeof(argbuf)-1] = 0;
196 if (strlen(argbuf) == sizeof(argbuf) - 1) {
197 fprintf(stderr, "Too long line in filter");
198 exit(-1);
199 }
200 if (argbuf[strlen(argbuf)-1] == '\n')
201 argbuf[strlen(argbuf)-1] = 0;
202 if (argbuf[0] == '#' || argbuf[0] == '0')
203 continue;
204 tokptr = argbuf;
205 }
206 } else {
207 return 0;
208 }
209 }
210 } while ((curtok = get_token_from_line(&tokptr)) == NULL);
211
212 if (strcmp(curtok, "!") == 0 ||
213 strcmp(curtok, "not") == 0)
214 return '!';
215 if (strcmp(curtok, "&") == 0 ||
216 strcmp(curtok, "&&") == 0 ||
217 strcmp(curtok, "and") == 0)
218 return '&';
219 if (strcmp(curtok, "|") == 0 ||
220 strcmp(curtok, "||") == 0 ||
221 strcmp(curtok, "or") == 0)
222 return '|';
223 if (strcmp(curtok, "(") == 0)
224 return '(';
225 if (strcmp(curtok, ")") == 0)
226 return ')';
227 if (strcmp(curtok, "dst") == 0) {
228 tok_type = DCOND;
229 return DCOND;
230 }
231 if (strcmp(curtok, "src") == 0) {
232 tok_type = SCOND;
233 return SCOND;
234 }
235 if (strcmp(curtok, "dport") == 0) {
236 tok_type = DPORT;
237 return DPORT;
238 }
239 if (strcmp(curtok, "sport") == 0) {
240 tok_type = SPORT;
241 return SPORT;
242 }
243 if (strcmp(curtok, ">=") == 0 ||
244 strcmp(curtok, "ge") == 0 ||
245 strcmp(curtok, "geq") == 0)
246 return GEQ;
247 if (strcmp(curtok, "<=") == 0 ||
248 strcmp(curtok, "le") == 0 ||
249 strcmp(curtok, "leq") == 0)
250 return LEQ;
251 if (strcmp(curtok, "!=") == 0 ||
252 strcmp(curtok, "ne") == 0 ||
253 strcmp(curtok, "neq") == 0)
254 return NEQ;
255 if (strcmp(curtok, "=") == 0 ||
256 strcmp(curtok, "==") == 0 ||
257 strcmp(curtok, "eq") == 0)
258 return '=';
259 if (strcmp(curtok, ">") == 0 ||
260 strcmp(curtok, "gt") == 0)
261 return '>';
262 if (strcmp(curtok, "<") == 0 ||
263 strcmp(curtok, "lt") == 0)
264 return '<';
265 if (strcmp(curtok, "autobound") == 0) {
266 tok_type = AUTOBOUND;
267 return AUTOBOUND;
268 }
269 yylval = (void*)parse_hostcond(curtok, tok_type == SPORT || tok_type == DPORT);
270 if (yylval == NULL) {
271 fprintf(stderr, "Cannot parse dst/src address.\n");
272 exit(1);
273 }
274 return HOSTCOND;
275 }
276
ssfilter_parse(struct ssfilter ** f,int argc,char ** argv,FILE * fp)277 int ssfilter_parse(struct ssfilter **f, int argc, char **argv, FILE *fp)
278 {
279 yy_argc = argc;
280 yy_argv = argv;
281 yy_fp = fp;
282 yy_ret = f;
283
284 if (yyparse()) {
285 fprintf(stderr, " Sorry.\n");
286 return -1;
287 }
288 return 0;
289 }
290