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