npf_parse.y revision 1.39 1 1.39 rmind /* $NetBSD: npf_parse.y,v 1.39 2016/12/27 22:35:33 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.32 rmind * Copyright (c) 2011-2014 The NetBSD Foundation, Inc.
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * This code is derived from software contributed to The NetBSD Foundation
8 1.26 rmind * by Martin Husemann, Christos Zoulas and Mindaugas Rasiukevicius.
9 1.1 rmind *
10 1.1 rmind * Redistribution and use in source and binary forms, with or without
11 1.1 rmind * modification, are permitted provided that the following conditions
12 1.1 rmind * are met:
13 1.1 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer.
15 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1 rmind * documentation and/or other materials provided with the distribution.
18 1.1 rmind *
19 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1 rmind */
31 1.1 rmind
32 1.1 rmind %{
33 1.1 rmind
34 1.37 joerg #include <err.h>
35 1.37 joerg #include <netdb.h>
36 1.1 rmind #include <stdio.h>
37 1.37 joerg #include <stdlib.h>
38 1.37 joerg #include <string.h>
39 1.1 rmind #include <vis.h>
40 1.1 rmind
41 1.1 rmind #include "npfctl.h"
42 1.1 rmind
43 1.12 rmind #define YYSTACKSIZE 4096
44 1.12 rmind
45 1.18 rmind int yyparsetarget;
46 1.1 rmind const char * yyfilename;
47 1.1 rmind
48 1.1 rmind extern int yylineno, yycolumn;
49 1.1 rmind extern int yylex(void);
50 1.1 rmind
51 1.1 rmind void
52 1.1 rmind yyerror(const char *fmt, ...)
53 1.1 rmind {
54 1.1 rmind extern int yyleng;
55 1.1 rmind extern char *yytext;
56 1.1 rmind
57 1.15 rmind char *msg, *context = estrndup(yytext, yyleng);
58 1.14 rmind bool eol = (*context == '\n');
59 1.1 rmind va_list ap;
60 1.1 rmind
61 1.1 rmind va_start(ap, fmt);
62 1.1 rmind vasprintf(&msg, fmt, ap);
63 1.1 rmind va_end(ap);
64 1.1 rmind
65 1.14 rmind fprintf(stderr, "%s:%d:%d: %s", yyfilename,
66 1.14 rmind yylineno - (int)eol, yycolumn, msg);
67 1.14 rmind if (!eol) {
68 1.14 rmind size_t len = strlen(context);
69 1.16 rmind char *dst = ecalloc(1, len * 4 + 1);
70 1.14 rmind
71 1.14 rmind strvisx(dst, context, len, VIS_WHITE|VIS_CSTYLE);
72 1.14 rmind fprintf(stderr, " near '%s'", dst);
73 1.14 rmind }
74 1.14 rmind fprintf(stderr, "\n");
75 1.1 rmind exit(EXIT_FAILURE);
76 1.1 rmind }
77 1.1 rmind
78 1.18 rmind #define CHECK_PARSER_FILE \
79 1.18 rmind if (yyparsetarget != NPFCTL_PARSE_FILE) \
80 1.18 rmind yyerror("rule must be in the group");
81 1.18 rmind
82 1.18 rmind #define CHECK_PARSER_STRING \
83 1.18 rmind if (yyparsetarget != NPFCTL_PARSE_STRING) \
84 1.18 rmind yyerror("invalid rule syntax");
85 1.18 rmind
86 1.1 rmind %}
87 1.1 rmind
88 1.22 christos %token ALG
89 1.32 rmind %token ALGO
90 1.1 rmind %token ALL
91 1.1 rmind %token ANY
92 1.1 rmind %token APPLY
93 1.8 rmind %token ARROWBOTH
94 1.8 rmind %token ARROWLEFT
95 1.8 rmind %token ARROWRIGHT
96 1.1 rmind %token BLOCK
97 1.36 christos %token BPFJIT
98 1.30 rmind %token CDB
99 1.1 rmind %token CURLY_CLOSE
100 1.1 rmind %token CURLY_OPEN
101 1.1 rmind %token CODE
102 1.1 rmind %token COLON
103 1.1 rmind %token COMMA
104 1.1 rmind %token DEFAULT
105 1.1 rmind %token TDYNAMIC
106 1.8 rmind %token TSTATIC
107 1.1 rmind %token EQ
108 1.39 rmind %token EXCL_MARK
109 1.1 rmind %token TFILE
110 1.1 rmind %token FLAGS
111 1.1 rmind %token FROM
112 1.1 rmind %token GROUP
113 1.1 rmind %token HASH
114 1.1 rmind %token ICMPTYPE
115 1.1 rmind %token ID
116 1.1 rmind %token IN
117 1.29 rmind %token INET4
118 1.1 rmind %token INET6
119 1.1 rmind %token INTERFACE
120 1.8 rmind %token MAP
121 1.1 rmind %token MINUS
122 1.1 rmind %token NAME
123 1.32 rmind %token NPT66
124 1.1 rmind %token ON
125 1.36 christos %token OFF
126 1.1 rmind %token OUT
127 1.1 rmind %token PAR_CLOSE
128 1.1 rmind %token PAR_OPEN
129 1.1 rmind %token PASS
130 1.26 rmind %token PCAP_FILTER
131 1.1 rmind %token PORT
132 1.1 rmind %token PROCEDURE
133 1.1 rmind %token PROTO
134 1.1 rmind %token FAMILY
135 1.7 rmind %token FINAL
136 1.18 rmind %token FORW
137 1.1 rmind %token RETURN
138 1.1 rmind %token RETURNICMP
139 1.1 rmind %token RETURNRST
140 1.21 rmind %token RULESET
141 1.1 rmind %token SEPLINE
142 1.36 christos %token SET
143 1.1 rmind %token SLASH
144 1.7 rmind %token STATEFUL
145 1.34 rmind %token STATEFUL_ENDS
146 1.1 rmind %token TABLE
147 1.1 rmind %token TCP
148 1.1 rmind %token TO
149 1.1 rmind %token TREE
150 1.1 rmind %token TYPE
151 1.11 spz %token <num> ICMP
152 1.11 spz %token <num> ICMP6
153 1.1 rmind
154 1.1 rmind %token <num> HEX
155 1.1 rmind %token <str> IDENTIFIER
156 1.1 rmind %token <str> IPV4ADDR
157 1.1 rmind %token <str> IPV6ADDR
158 1.1 rmind %token <num> NUM
159 1.13 rmind %token <fpnum> FPNUM
160 1.1 rmind %token <str> STRING
161 1.1 rmind %token <str> TABLE_ID
162 1.1 rmind %token <str> VAR_ID
163 1.1 rmind
164 1.29 rmind %type <str> addr, some_name, table_store
165 1.29 rmind %type <str> proc_param_val, opt_apply, ifname, on_ifname, ifref
166 1.27 rmind %type <num> port, opt_final, number, afamily, opt_family
167 1.26 rmind %type <num> block_or_pass, rule_dir, group_dir, block_opts
168 1.39 rmind %type <num> maybe_not, opt_stateful, icmp_type, table_type
169 1.32 rmind %type <num> map_sd, map_algo, map_type
170 1.29 rmind %type <var> ifaddrs, addr_or_ifaddr, port_range, icmp_type_and_code
171 1.1 rmind %type <var> filt_addr, addr_and_mask, tcp_flags, tcp_flags_and_mask
172 1.13 rmind %type <var> procs, proc_call, proc_param_list, proc_param
173 1.29 rmind %type <var> element, list_elems, list, value
174 1.8 rmind %type <addrport> mapseg
175 1.1 rmind %type <filtopts> filt_opts, all_or_filt_opts
176 1.1 rmind %type <optproto> opt_proto
177 1.26 rmind %type <rulegroup> group_opts
178 1.36 christos %type <tf> onoff
179 1.1 rmind
180 1.1 rmind %union {
181 1.1 rmind char * str;
182 1.36 christos bool tf;
183 1.1 rmind unsigned long num;
184 1.13 rmind double fpnum;
185 1.17 rmind npfvar_t * var;
186 1.8 rmind addr_port_t addrport;
187 1.1 rmind filt_opts_t filtopts;
188 1.1 rmind opt_proto_t optproto;
189 1.1 rmind rule_group_t rulegroup;
190 1.1 rmind }
191 1.1 rmind
192 1.1 rmind %%
193 1.1 rmind
194 1.1 rmind input
195 1.18 rmind : { CHECK_PARSER_FILE } lines
196 1.18 rmind | { CHECK_PARSER_STRING } rule
197 1.1 rmind ;
198 1.1 rmind
199 1.1 rmind lines
200 1.35 riastrad : lines SEPLINE line
201 1.1 rmind | line
202 1.1 rmind ;
203 1.1 rmind
204 1.1 rmind line
205 1.28 rmind : vardef
206 1.1 rmind | table
207 1.8 rmind | map
208 1.1 rmind | group
209 1.1 rmind | rproc
210 1.22 christos | alg
211 1.36 christos | set
212 1.1 rmind |
213 1.1 rmind ;
214 1.1 rmind
215 1.28 rmind alg
216 1.28 rmind : ALG STRING
217 1.28 rmind {
218 1.28 rmind npfctl_build_alg($2);
219 1.28 rmind }
220 1.28 rmind ;
221 1.28 rmind
222 1.36 christos onoff
223 1.36 christos : ON {
224 1.36 christos $$ = true;
225 1.36 christos }
226 1.36 christos | OFF {
227 1.36 christos $$ = false;
228 1.36 christos }
229 1.36 christos ;
230 1.36 christos
231 1.36 christos set
232 1.36 christos : SET BPFJIT onoff {
233 1.36 christos npfctl_bpfjit($3);
234 1.36 christos }
235 1.36 christos ;
236 1.36 christos
237 1.28 rmind /*
238 1.28 rmind * A value - an element or a list of elements.
239 1.28 rmind * Can be assigned to a variable or used inline.
240 1.28 rmind */
241 1.28 rmind
242 1.28 rmind vardef
243 1.29 rmind : VAR_ID EQ value
244 1.1 rmind {
245 1.29 rmind npfvar_add($3, $1);
246 1.1 rmind }
247 1.1 rmind ;
248 1.1 rmind
249 1.28 rmind value
250 1.28 rmind : element
251 1.28 rmind | list
252 1.1 rmind ;
253 1.1 rmind
254 1.28 rmind list
255 1.1 rmind : CURLY_OPEN list_elems CURLY_CLOSE
256 1.29 rmind {
257 1.29 rmind $$ = $2;
258 1.29 rmind }
259 1.1 rmind ;
260 1.1 rmind
261 1.1 rmind list_elems
262 1.35 riastrad : list_elems COMMA element
263 1.29 rmind {
264 1.29 rmind npfvar_add_elements($1, $3);
265 1.29 rmind }
266 1.28 rmind | element
267 1.1 rmind ;
268 1.1 rmind
269 1.28 rmind element
270 1.1 rmind : IDENTIFIER
271 1.1 rmind {
272 1.29 rmind $$ = npfvar_create_from_string(NPFVAR_IDENTIFIER, $1);
273 1.1 rmind }
274 1.1 rmind | STRING
275 1.1 rmind {
276 1.29 rmind $$ = npfvar_create_from_string(NPFVAR_STRING, $1);
277 1.1 rmind }
278 1.23 christos | number MINUS number
279 1.5 christos {
280 1.29 rmind $$ = npfctl_parse_port_range($1, $3);
281 1.5 christos }
282 1.23 christos | number
283 1.1 rmind {
284 1.29 rmind $$ = npfvar_create_element(NPFVAR_NUM, &$1, sizeof($1));
285 1.1 rmind }
286 1.1 rmind | VAR_ID
287 1.1 rmind {
288 1.29 rmind $$ = npfvar_create_from_string(NPFVAR_VAR_ID, $1);
289 1.1 rmind }
290 1.29 rmind | TABLE_ID { $$ = npfctl_parse_table_id($1); }
291 1.29 rmind | ifaddrs { $$ = $1; }
292 1.29 rmind | addr_and_mask { $$ = $1; }
293 1.1 rmind ;
294 1.1 rmind
295 1.28 rmind /*
296 1.28 rmind * Table definition.
297 1.28 rmind */
298 1.28 rmind
299 1.1 rmind table
300 1.1 rmind : TABLE TABLE_ID TYPE table_type table_store
301 1.1 rmind {
302 1.1 rmind npfctl_build_table($2, $4, $5);
303 1.1 rmind }
304 1.1 rmind ;
305 1.1 rmind
306 1.1 rmind table_type
307 1.1 rmind : HASH { $$ = NPF_TABLE_HASH; }
308 1.3 rmind | TREE { $$ = NPF_TABLE_TREE; }
309 1.30 rmind | CDB { $$ = NPF_TABLE_CDB; }
310 1.1 rmind ;
311 1.1 rmind
312 1.1 rmind table_store
313 1.1 rmind : TDYNAMIC { $$ = NULL; }
314 1.1 rmind | TFILE STRING { $$ = $2; }
315 1.1 rmind ;
316 1.1 rmind
317 1.28 rmind /*
318 1.28 rmind * Map definition.
319 1.28 rmind */
320 1.28 rmind
321 1.8 rmind map_sd
322 1.8 rmind : TSTATIC { $$ = NPFCTL_NAT_STATIC; }
323 1.8 rmind | TDYNAMIC { $$ = NPFCTL_NAT_DYNAMIC; }
324 1.8 rmind | { $$ = NPFCTL_NAT_DYNAMIC; }
325 1.1 rmind ;
326 1.1 rmind
327 1.32 rmind map_algo
328 1.32 rmind : ALGO NPT66 { $$ = NPF_ALGO_NPT66; }
329 1.32 rmind | { $$ = 0; }
330 1.32 rmind ;
331 1.32 rmind
332 1.8 rmind map_type
333 1.8 rmind : ARROWBOTH { $$ = NPF_NATIN | NPF_NATOUT; }
334 1.8 rmind | ARROWLEFT { $$ = NPF_NATIN; }
335 1.8 rmind | ARROWRIGHT { $$ = NPF_NATOUT; }
336 1.8 rmind ;
337 1.8 rmind
338 1.8 rmind mapseg
339 1.29 rmind : addr_or_ifaddr port_range
340 1.1 rmind {
341 1.8 rmind $$.ap_netaddr = $1;
342 1.8 rmind $$.ap_portrange = $2;
343 1.1 rmind }
344 1.1 rmind ;
345 1.1 rmind
346 1.8 rmind map
347 1.32 rmind : MAP ifref map_sd map_algo mapseg map_type mapseg PASS filt_opts
348 1.1 rmind {
349 1.32 rmind npfctl_build_natseg($3, $6, $2, &$5, &$7, &$9, $4);
350 1.1 rmind }
351 1.32 rmind | MAP ifref map_sd map_algo mapseg map_type mapseg
352 1.1 rmind {
353 1.32 rmind npfctl_build_natseg($3, $6, $2, &$5, &$7, NULL, $4);
354 1.1 rmind }
355 1.26 rmind | MAP RULESET group_opts
356 1.21 rmind {
357 1.27 rmind npfctl_build_maprset($3.rg_name, $3.rg_attr, $3.rg_ifname);
358 1.21 rmind }
359 1.1 rmind ;
360 1.1 rmind
361 1.28 rmind /*
362 1.28 rmind * Rule procedure definition and its parameters.
363 1.28 rmind */
364 1.28 rmind
365 1.1 rmind rproc
366 1.1 rmind : PROCEDURE STRING CURLY_OPEN procs CURLY_CLOSE
367 1.1 rmind {
368 1.1 rmind npfctl_build_rproc($2, $4);
369 1.1 rmind }
370 1.1 rmind ;
371 1.1 rmind
372 1.1 rmind procs
373 1.35 riastrad : procs SEPLINE proc_call
374 1.13 rmind {
375 1.13 rmind $$ = npfvar_add_elements($1, $3);
376 1.13 rmind }
377 1.13 rmind | proc_call { $$ = $1; }
378 1.1 rmind ;
379 1.1 rmind
380 1.13 rmind proc_call
381 1.13 rmind : IDENTIFIER COLON proc_param_list
382 1.1 rmind {
383 1.13 rmind proc_call_t pc;
384 1.1 rmind
385 1.15 rmind pc.pc_name = estrdup($1);
386 1.13 rmind pc.pc_opts = $3;
387 1.29 rmind
388 1.29 rmind $$ = npfvar_create_element(NPFVAR_PROC, &pc, sizeof(pc));
389 1.1 rmind }
390 1.29 rmind | { $$ = NULL; }
391 1.1 rmind ;
392 1.1 rmind
393 1.13 rmind proc_param_list
394 1.35 riastrad : proc_param_list COMMA proc_param
395 1.1 rmind {
396 1.1 rmind $$ = npfvar_add_elements($1, $3);
397 1.1 rmind }
398 1.13 rmind | proc_param { $$ = $1; }
399 1.1 rmind | { $$ = NULL; }
400 1.1 rmind ;
401 1.1 rmind
402 1.13 rmind proc_param
403 1.13 rmind : some_name proc_param_val
404 1.1 rmind {
405 1.13 rmind proc_param_t pp;
406 1.1 rmind
407 1.15 rmind pp.pp_param = estrdup($1);
408 1.15 rmind pp.pp_value = $2 ? estrdup($2) : NULL;
409 1.29 rmind
410 1.29 rmind $$ = npfvar_create_element(NPFVAR_PROC_PARAM, &pp, sizeof(pp));
411 1.1 rmind }
412 1.1 rmind ;
413 1.1 rmind
414 1.13 rmind proc_param_val
415 1.13 rmind : some_name { $$ = $1; }
416 1.23 christos | number { (void)asprintf(&$$, "%ld", $1); }
417 1.13 rmind | FPNUM { (void)asprintf(&$$, "%lf", $1); }
418 1.13 rmind | { $$ = NULL; }
419 1.1 rmind ;
420 1.1 rmind
421 1.28 rmind /*
422 1.28 rmind * Group and dynamic ruleset definition.
423 1.28 rmind */
424 1.28 rmind
425 1.1 rmind group
426 1.26 rmind : GROUP group_opts
427 1.1 rmind {
428 1.29 rmind /* Build a group. Increase the nesting level. */
429 1.26 rmind npfctl_build_group($2.rg_name, $2.rg_attr,
430 1.27 rmind $2.rg_ifname, $2.rg_default);
431 1.18 rmind }
432 1.18 rmind ruleset_block
433 1.18 rmind {
434 1.18 rmind /* Decrease the nesting level. */
435 1.18 rmind npfctl_build_group_end();
436 1.1 rmind }
437 1.1 rmind ;
438 1.1 rmind
439 1.21 rmind ruleset
440 1.26 rmind : RULESET group_opts
441 1.21 rmind {
442 1.21 rmind /* Ruleset is a dynamic group. */
443 1.26 rmind npfctl_build_group($2.rg_name, $2.rg_attr | NPF_RULE_DYNAMIC,
444 1.27 rmind $2.rg_ifname, $2.rg_default);
445 1.21 rmind npfctl_build_group_end();
446 1.21 rmind }
447 1.26 rmind ;
448 1.21 rmind
449 1.26 rmind group_dir
450 1.26 rmind : FORW { $$ = NPF_RULE_FORW; }
451 1.26 rmind | rule_dir
452 1.1 rmind ;
453 1.1 rmind
454 1.26 rmind group_opts
455 1.1 rmind : DEFAULT
456 1.1 rmind {
457 1.18 rmind memset(&$$, 0, sizeof(rule_group_t));
458 1.18 rmind $$.rg_default = true;
459 1.1 rmind }
460 1.27 rmind | STRING group_dir on_ifname
461 1.1 rmind {
462 1.18 rmind memset(&$$, 0, sizeof(rule_group_t));
463 1.26 rmind $$.rg_name = $1;
464 1.26 rmind $$.rg_attr = $2;
465 1.27 rmind $$.rg_ifname = $3;
466 1.1 rmind }
467 1.1 rmind ;
468 1.1 rmind
469 1.18 rmind ruleset_block
470 1.21 rmind : CURLY_OPEN ruleset_def CURLY_CLOSE
471 1.18 rmind ;
472 1.18 rmind
473 1.21 rmind ruleset_def
474 1.35 riastrad : ruleset_def SEPLINE rule_group
475 1.18 rmind | rule_group
476 1.1 rmind ;
477 1.1 rmind
478 1.18 rmind rule_group
479 1.18 rmind : rule
480 1.18 rmind | group
481 1.21 rmind | ruleset
482 1.18 rmind |
483 1.24 rmind ;
484 1.1 rmind
485 1.28 rmind /*
486 1.28 rmind * Rule and misc.
487 1.28 rmind */
488 1.28 rmind
489 1.1 rmind rule
490 1.27 rmind : block_or_pass opt_stateful rule_dir opt_final on_ifname
491 1.17 rmind opt_family opt_proto all_or_filt_opts opt_apply
492 1.1 rmind {
493 1.7 rmind npfctl_build_rule($1 | $2 | $3 | $4, $5,
494 1.26 rmind $6, &$7, &$8, NULL, $9);
495 1.26 rmind }
496 1.27 rmind | block_or_pass opt_stateful rule_dir opt_final on_ifname
497 1.26 rmind PCAP_FILTER STRING opt_apply
498 1.26 rmind {
499 1.26 rmind npfctl_build_rule($1 | $2 | $3 | $4, $5,
500 1.26 rmind AF_UNSPEC, NULL, NULL, $7, $8);
501 1.1 rmind }
502 1.1 rmind ;
503 1.1 rmind
504 1.1 rmind block_or_pass
505 1.1 rmind : BLOCK block_opts { $$ = $2; }
506 1.1 rmind | PASS { $$ = NPF_RULE_PASS; }
507 1.1 rmind ;
508 1.1 rmind
509 1.1 rmind rule_dir
510 1.1 rmind : IN { $$ = NPF_RULE_IN; }
511 1.1 rmind | OUT { $$ = NPF_RULE_OUT; }
512 1.1 rmind | { $$ = NPF_RULE_IN | NPF_RULE_OUT; }
513 1.1 rmind ;
514 1.1 rmind
515 1.7 rmind opt_final
516 1.7 rmind : FINAL { $$ = NPF_RULE_FINAL; }
517 1.1 rmind | { $$ = 0; }
518 1.1 rmind ;
519 1.1 rmind
520 1.27 rmind on_ifname
521 1.29 rmind : ON ifref { $$ = $2; }
522 1.27 rmind | { $$ = NULL; }
523 1.1 rmind ;
524 1.1 rmind
525 1.17 rmind afamily
526 1.29 rmind : INET4 { $$ = AF_INET; }
527 1.17 rmind | INET6 { $$ = AF_INET6; }
528 1.17 rmind ;
529 1.17 rmind
530 1.39 rmind maybe_not
531 1.39 rmind : EXCL_MARK { $$ = true; }
532 1.39 rmind | { $$ = false; }
533 1.39 rmind ;
534 1.39 rmind
535 1.9 rmind opt_family
536 1.17 rmind : FAMILY afamily { $$ = $2; }
537 1.9 rmind | { $$ = AF_UNSPEC; }
538 1.1 rmind ;
539 1.1 rmind
540 1.1 rmind opt_proto
541 1.1 rmind : PROTO TCP tcp_flags_and_mask
542 1.1 rmind {
543 1.1 rmind $$.op_proto = IPPROTO_TCP;
544 1.1 rmind $$.op_opts = $3;
545 1.1 rmind }
546 1.1 rmind | PROTO ICMP icmp_type_and_code
547 1.1 rmind {
548 1.1 rmind $$.op_proto = IPPROTO_ICMP;
549 1.1 rmind $$.op_opts = $3;
550 1.1 rmind }
551 1.11 spz | PROTO ICMP6 icmp_type_and_code
552 1.11 spz {
553 1.11 spz $$.op_proto = IPPROTO_ICMPV6;
554 1.11 spz $$.op_opts = $3;
555 1.11 spz }
556 1.9 rmind | PROTO some_name
557 1.9 rmind {
558 1.9 rmind $$.op_proto = npfctl_protono($2);
559 1.9 rmind $$.op_opts = NULL;
560 1.9 rmind }
561 1.23 christos | PROTO number
562 1.1 rmind {
563 1.9 rmind $$.op_proto = $2;
564 1.1 rmind $$.op_opts = NULL;
565 1.1 rmind }
566 1.1 rmind |
567 1.1 rmind {
568 1.1 rmind $$.op_proto = -1;
569 1.1 rmind $$.op_opts = NULL;
570 1.1 rmind }
571 1.1 rmind ;
572 1.1 rmind
573 1.1 rmind all_or_filt_opts
574 1.1 rmind : ALL
575 1.1 rmind {
576 1.39 rmind $$.fo_finvert = false;
577 1.8 rmind $$.fo_from.ap_netaddr = NULL;
578 1.8 rmind $$.fo_from.ap_portrange = NULL;
579 1.39 rmind $$.fo_tinvert = false;
580 1.8 rmind $$.fo_to.ap_netaddr = NULL;
581 1.8 rmind $$.fo_to.ap_portrange = NULL;
582 1.1 rmind }
583 1.1 rmind | filt_opts { $$ = $1; }
584 1.1 rmind ;
585 1.1 rmind
586 1.7 rmind opt_stateful
587 1.9 rmind : STATEFUL { $$ = NPF_RULE_STATEFUL; }
588 1.34 rmind | STATEFUL_ENDS { $$ = NPF_RULE_STATEFUL | NPF_RULE_MULTIENDS; }
589 1.1 rmind | { $$ = 0; }
590 1.1 rmind ;
591 1.1 rmind
592 1.1 rmind opt_apply
593 1.1 rmind : APPLY STRING { $$ = $2; }
594 1.1 rmind | { $$ = NULL; }
595 1.1 rmind ;
596 1.1 rmind
597 1.1 rmind block_opts
598 1.1 rmind : RETURNRST { $$ = NPF_RULE_RETRST; }
599 1.1 rmind | RETURNICMP { $$ = NPF_RULE_RETICMP; }
600 1.1 rmind | RETURN { $$ = NPF_RULE_RETRST | NPF_RULE_RETICMP; }
601 1.1 rmind | { $$ = 0; }
602 1.1 rmind ;
603 1.1 rmind
604 1.1 rmind filt_opts
605 1.39 rmind : FROM maybe_not filt_addr port_range TO maybe_not filt_addr port_range
606 1.1 rmind {
607 1.39 rmind $$.fo_finvert = $2;
608 1.39 rmind $$.fo_from.ap_netaddr = $3;
609 1.39 rmind $$.fo_from.ap_portrange = $4;
610 1.39 rmind $$.fo_tinvert = $6;
611 1.39 rmind $$.fo_to.ap_netaddr = $7;
612 1.39 rmind $$.fo_to.ap_portrange = $8;
613 1.39 rmind }
614 1.39 rmind | FROM maybe_not filt_addr port_range
615 1.39 rmind {
616 1.39 rmind $$.fo_finvert = $2;
617 1.39 rmind $$.fo_from.ap_netaddr = $3;
618 1.39 rmind $$.fo_from.ap_portrange = $4;
619 1.39 rmind $$.fo_tinvert = false;
620 1.8 rmind $$.fo_to.ap_netaddr = NULL;
621 1.8 rmind $$.fo_to.ap_portrange = NULL;
622 1.1 rmind }
623 1.39 rmind | TO maybe_not filt_addr port_range
624 1.1 rmind {
625 1.39 rmind $$.fo_finvert = false;
626 1.8 rmind $$.fo_from.ap_netaddr = NULL;
627 1.8 rmind $$.fo_from.ap_portrange = NULL;
628 1.39 rmind $$.fo_tinvert = $2;
629 1.39 rmind $$.fo_to.ap_netaddr = $3;
630 1.39 rmind $$.fo_to.ap_portrange = $4;
631 1.1 rmind }
632 1.1 rmind ;
633 1.1 rmind
634 1.1 rmind filt_addr
635 1.38 christos : list { $$ = $1; }
636 1.38 christos | addr_or_ifaddr { $$ = $1; }
637 1.4 rmind | TABLE_ID { $$ = npfctl_parse_table_id($1); }
638 1.4 rmind | ANY { $$ = NULL; }
639 1.1 rmind ;
640 1.1 rmind
641 1.1 rmind addr_and_mask
642 1.23 christos : addr SLASH number
643 1.1 rmind {
644 1.1 rmind $$ = npfctl_parse_fam_addr_mask($1, NULL, &$3);
645 1.1 rmind }
646 1.1 rmind | addr SLASH addr
647 1.1 rmind {
648 1.1 rmind $$ = npfctl_parse_fam_addr_mask($1, $3, NULL);
649 1.1 rmind }
650 1.1 rmind | addr
651 1.1 rmind {
652 1.1 rmind $$ = npfctl_parse_fam_addr_mask($1, NULL, NULL);
653 1.1 rmind }
654 1.1 rmind ;
655 1.1 rmind
656 1.29 rmind addr_or_ifaddr
657 1.9 rmind : addr_and_mask
658 1.9 rmind {
659 1.9 rmind assert($1 != NULL);
660 1.9 rmind $$ = $1;
661 1.9 rmind }
662 1.29 rmind | ifaddrs
663 1.4 rmind {
664 1.17 rmind ifnet_addr_t *ifna = npfvar_get_data($1, NPFVAR_INTERFACE, 0);
665 1.17 rmind $$ = ifna->ifna_addrs;
666 1.4 rmind }
667 1.4 rmind | VAR_ID
668 1.4 rmind {
669 1.4 rmind npfvar_t *vp = npfvar_lookup($1);
670 1.19 christos int type = npfvar_get_type(vp, 0);
671 1.17 rmind ifnet_addr_t *ifna;
672 1.4 rmind
673 1.19 christos again:
674 1.4 rmind switch (type) {
675 1.19 christos case NPFVAR_IDENTIFIER:
676 1.19 christos case NPFVAR_STRING:
677 1.19 christos vp = npfctl_parse_ifnet(npfvar_expand_string(vp),
678 1.19 christos AF_UNSPEC);
679 1.19 christos type = npfvar_get_type(vp, 0);
680 1.19 christos goto again;
681 1.4 rmind case NPFVAR_FAM:
682 1.4 rmind $$ = vp;
683 1.4 rmind break;
684 1.17 rmind case NPFVAR_INTERFACE:
685 1.33 rmind $$ = NULL;
686 1.33 rmind for (u_int i = 0; i < npfvar_get_count(vp); i++) {
687 1.33 rmind ifna = npfvar_get_data(vp, type, i);
688 1.33 rmind $$ = npfvar_add_elements($$, ifna->ifna_addrs);
689 1.33 rmind }
690 1.17 rmind break;
691 1.4 rmind case -1:
692 1.17 rmind yyerror("undefined variable '%s'", $1);
693 1.4 rmind break;
694 1.4 rmind default:
695 1.17 rmind yyerror("wrong variable '%s' type '%s' for address "
696 1.17 rmind "or interface", $1, npfvar_type(type));
697 1.4 rmind break;
698 1.4 rmind }
699 1.4 rmind }
700 1.1 rmind ;
701 1.1 rmind
702 1.1 rmind addr
703 1.1 rmind : IPV4ADDR { $$ = $1; }
704 1.1 rmind | IPV6ADDR { $$ = $1; }
705 1.1 rmind ;
706 1.1 rmind
707 1.1 rmind port_range
708 1.1 rmind : PORT port /* just port */
709 1.1 rmind {
710 1.1 rmind $$ = npfctl_parse_port_range($2, $2);
711 1.1 rmind }
712 1.8 rmind | PORT port MINUS port /* port from-to */
713 1.1 rmind {
714 1.1 rmind $$ = npfctl_parse_port_range($2, $4);
715 1.1 rmind }
716 1.8 rmind | PORT VAR_ID
717 1.8 rmind {
718 1.5 christos $$ = npfctl_parse_port_range_variable($2);
719 1.5 christos }
720 1.1 rmind |
721 1.1 rmind {
722 1.1 rmind $$ = NULL;
723 1.1 rmind }
724 1.1 rmind ;
725 1.1 rmind
726 1.1 rmind port
727 1.23 christos : number { $$ = $1; }
728 1.1 rmind | IDENTIFIER { $$ = npfctl_portno($1); }
729 1.20 christos | STRING { $$ = npfctl_portno($1); }
730 1.1 rmind ;
731 1.1 rmind
732 1.1 rmind icmp_type_and_code
733 1.1 rmind : ICMPTYPE icmp_type
734 1.1 rmind {
735 1.11 spz $$ = npfctl_parse_icmp($<num>0, $2, -1);
736 1.1 rmind }
737 1.23 christos | ICMPTYPE icmp_type CODE number
738 1.1 rmind {
739 1.11 spz $$ = npfctl_parse_icmp($<num>0, $2, $4);
740 1.1 rmind }
741 1.1 rmind | ICMPTYPE icmp_type CODE IDENTIFIER
742 1.1 rmind {
743 1.17 rmind $$ = npfctl_parse_icmp($<num>0, $2,
744 1.17 rmind npfctl_icmpcode($<num>0, $2, $4));
745 1.1 rmind }
746 1.1 rmind | ICMPTYPE icmp_type CODE VAR_ID
747 1.1 rmind {
748 1.1 rmind char *s = npfvar_expand_string(npfvar_lookup($4));
749 1.17 rmind $$ = npfctl_parse_icmp($<num>0, $2,
750 1.17 rmind npfctl_icmpcode($<num>0, $2, s));
751 1.1 rmind }
752 1.25 rmind | { $$ = NULL; }
753 1.1 rmind ;
754 1.1 rmind
755 1.1 rmind tcp_flags_and_mask
756 1.1 rmind : FLAGS tcp_flags SLASH tcp_flags
757 1.1 rmind {
758 1.1 rmind npfvar_add_elements($2, $4);
759 1.1 rmind $$ = $2;
760 1.1 rmind }
761 1.1 rmind | FLAGS tcp_flags
762 1.1 rmind {
763 1.1 rmind char *s = npfvar_get_data($2, NPFVAR_TCPFLAG, 0);
764 1.1 rmind npfvar_add_elements($2, npfctl_parse_tcpflag(s));
765 1.1 rmind $$ = $2;
766 1.1 rmind }
767 1.1 rmind | { $$ = NULL; }
768 1.1 rmind ;
769 1.1 rmind
770 1.1 rmind tcp_flags
771 1.1 rmind : IDENTIFIER { $$ = npfctl_parse_tcpflag($1); }
772 1.1 rmind ;
773 1.1 rmind
774 1.1 rmind icmp_type
775 1.23 christos : number { $$ = $1; }
776 1.11 spz | IDENTIFIER { $$ = npfctl_icmptype($<num>-1, $1); }
777 1.1 rmind | VAR_ID
778 1.1 rmind {
779 1.1 rmind char *s = npfvar_expand_string(npfvar_lookup($1));
780 1.11 spz $$ = npfctl_icmptype($<num>-1, s);
781 1.1 rmind }
782 1.1 rmind ;
783 1.1 rmind
784 1.29 rmind ifname
785 1.29 rmind : some_name
786 1.19 christos {
787 1.29 rmind npfctl_note_interface($1);
788 1.19 christos $$ = $1;
789 1.19 christos }
790 1.19 christos | VAR_ID
791 1.19 christos {
792 1.19 christos npfvar_t *vp = npfvar_lookup($1);
793 1.19 christos const int type = npfvar_get_type(vp, 0);
794 1.29 rmind ifnet_addr_t *ifna;
795 1.19 christos
796 1.19 christos switch (type) {
797 1.19 christos case NPFVAR_STRING:
798 1.19 christos case NPFVAR_IDENTIFIER:
799 1.19 christos $$ = npfvar_expand_string(vp);
800 1.19 christos break;
801 1.29 rmind case NPFVAR_INTERFACE:
802 1.29 rmind ifna = npfvar_get_data(vp, type, 0);
803 1.29 rmind $$ = ifna->ifna_name;
804 1.29 rmind break;
805 1.19 christos case -1:
806 1.19 christos yyerror("undefined variable '%s' for interface", $1);
807 1.19 christos break;
808 1.19 christos default:
809 1.29 rmind yyerror("wrong variable '%s' type '%s' for interface",
810 1.19 christos $1, npfvar_type(type));
811 1.19 christos break;
812 1.19 christos }
813 1.29 rmind npfctl_note_interface($$);
814 1.19 christos }
815 1.19 christos ;
816 1.19 christos
817 1.29 rmind ifaddrs
818 1.29 rmind : afamily PAR_OPEN ifname PAR_CLOSE
819 1.17 rmind {
820 1.17 rmind $$ = npfctl_parse_ifnet($3, $1);
821 1.17 rmind }
822 1.19 christos ;
823 1.17 rmind
824 1.29 rmind ifref
825 1.29 rmind : ifname
826 1.29 rmind | ifaddrs
827 1.17 rmind {
828 1.17 rmind ifnet_addr_t *ifna = npfvar_get_data($1, NPFVAR_INTERFACE, 0);
829 1.27 rmind npfctl_note_interface(ifna->ifna_name);
830 1.27 rmind $$ = ifna->ifna_name;
831 1.17 rmind }
832 1.1 rmind ;
833 1.1 rmind
834 1.23 christos number
835 1.23 christos : HEX { $$ = $1; }
836 1.23 christos | NUM { $$ = $1; }
837 1.23 christos ;
838 1.23 christos
839 1.9 rmind some_name
840 1.1 rmind : IDENTIFIER { $$ = $1; }
841 1.1 rmind | STRING { $$ = $1; }
842 1.1 rmind ;
843 1.1 rmind
844 1.1 rmind %%
845