Home | History | Annotate | Line # | Download | only in npfctl
npf_parse.y revision 1.1
      1  1.1  rmind /*	$NetBSD: npf_parse.y,v 1.1 2012/01/08 21:34:21 rmind Exp $	*/
      2  1.1  rmind 
      3  1.1  rmind /*-
      4  1.1  rmind  * Copyright (c) 2011-2012 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.1  rmind  * by Martin Husemann and Christos Zoulas.
      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.1  rmind #include <stdio.h>
     35  1.1  rmind #include <err.h>
     36  1.1  rmind #include <vis.h>
     37  1.1  rmind #include <netdb.h>
     38  1.1  rmind #include <assert.h>
     39  1.1  rmind 
     40  1.1  rmind #include "npfctl.h"
     41  1.1  rmind 
     42  1.1  rmind const char *		yyfilename;
     43  1.1  rmind 
     44  1.1  rmind extern int		yylineno, yycolumn;
     45  1.1  rmind extern int		yylex(void);
     46  1.1  rmind 
     47  1.1  rmind /* Variable under construction (bottom up). */
     48  1.1  rmind static npfvar_t *	cvar;
     49  1.1  rmind 
     50  1.1  rmind void
     51  1.1  rmind yyerror(const char *fmt, ...)
     52  1.1  rmind {
     53  1.1  rmind 	extern int yyleng;
     54  1.1  rmind 	extern char *yytext;
     55  1.1  rmind 
     56  1.1  rmind 	char *msg, *context = xstrndup(yytext, yyleng);
     57  1.1  rmind 	size_t len = strlen(context);
     58  1.1  rmind 	char *dst = zalloc(len * 4 + 1);
     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.1  rmind 	strvisx(dst, context, len, VIS_WHITE|VIS_CSTYLE);
     66  1.1  rmind 	fprintf(stderr, "%s:%d:%d: %s near '%s'\n", yyfilename, yylineno,
     67  1.1  rmind 	    yycolumn, msg, dst);
     68  1.1  rmind 	exit(EXIT_FAILURE);
     69  1.1  rmind }
     70  1.1  rmind 
     71  1.1  rmind %}
     72  1.1  rmind 
     73  1.1  rmind %token			ALL
     74  1.1  rmind %token			ANY
     75  1.1  rmind %token			APPLY
     76  1.1  rmind %token			ARROW
     77  1.1  rmind %token			BINAT
     78  1.1  rmind %token			BLOCK
     79  1.1  rmind %token			CURLY_CLOSE
     80  1.1  rmind %token			CURLY_OPEN
     81  1.1  rmind %token			CODE
     82  1.1  rmind %token			COLON
     83  1.1  rmind %token			COMMA
     84  1.1  rmind %token			DEFAULT
     85  1.1  rmind %token			TDYNAMIC
     86  1.1  rmind %token			EQ
     87  1.1  rmind %token			TFILE
     88  1.1  rmind %token			FLAGS
     89  1.1  rmind %token			FROM
     90  1.1  rmind %token			GROUP
     91  1.1  rmind %token			HASH
     92  1.1  rmind %token			ICMPTYPE
     93  1.1  rmind %token			ID
     94  1.1  rmind %token			IN
     95  1.1  rmind %token			INET
     96  1.1  rmind %token			INET6
     97  1.1  rmind %token			INTERFACE
     98  1.1  rmind %token			KEEP
     99  1.1  rmind %token			MINUS
    100  1.1  rmind %token			NAT
    101  1.1  rmind %token			NAME
    102  1.1  rmind %token			ON
    103  1.1  rmind %token			OUT
    104  1.1  rmind %token			PAR_CLOSE
    105  1.1  rmind %token			PAR_OPEN
    106  1.1  rmind %token			PASS
    107  1.1  rmind %token			PORT
    108  1.1  rmind %token			PROCEDURE
    109  1.1  rmind %token			PROTO
    110  1.1  rmind %token			FAMILY
    111  1.1  rmind %token			QUICK
    112  1.1  rmind %token			RDR
    113  1.1  rmind %token			RETURN
    114  1.1  rmind %token			RETURNICMP
    115  1.1  rmind %token			RETURNRST
    116  1.1  rmind %token			SEPLINE
    117  1.1  rmind %token			SLASH
    118  1.1  rmind %token			STATE
    119  1.1  rmind %token			TABLE
    120  1.1  rmind %token			TCP
    121  1.1  rmind %token			TO
    122  1.1  rmind %token			TREE
    123  1.1  rmind %token			TYPE
    124  1.1  rmind %token			UDP
    125  1.1  rmind %token			ICMP
    126  1.1  rmind 
    127  1.1  rmind %token	<num>		HEX
    128  1.1  rmind %token	<str>		IDENTIFIER
    129  1.1  rmind %token	<str>		IPV4ADDR
    130  1.1  rmind %token	<str>		IPV6ADDR
    131  1.1  rmind %token	<num>		NUM
    132  1.1  rmind %token	<str>		STRING
    133  1.1  rmind %token	<str>		TABLE_ID
    134  1.1  rmind %token	<str>		VAR_ID
    135  1.1  rmind 
    136  1.1  rmind %type	<str>		addr, iface_name, moduleargname, list_elem, table_store
    137  1.1  rmind %type	<str>		opt_apply
    138  1.1  rmind %type	<num>		ifindex, port, opt_quick, on_iface
    139  1.1  rmind %type	<num>		block_or_pass, rule_dir, block_opts, family, opt_family
    140  1.1  rmind %type	<num>		opt_keep_state, icmp_type, table_type
    141  1.1  rmind %type	<var>		addr_or_iface, port_range, iface, icmp_type_and_code
    142  1.1  rmind %type	<var>		filt_addr, addr_and_mask, tcp_flags, tcp_flags_and_mask
    143  1.1  rmind %type	<var>		modulearg_opts, procs, proc_op, modulearg, moduleargs
    144  1.1  rmind %type	<filtopts>	filt_opts, all_or_filt_opts
    145  1.1  rmind %type	<optproto>	opt_proto
    146  1.1  rmind %type	<rulegroup>	group_attr, group_opt
    147  1.1  rmind 
    148  1.1  rmind %union {
    149  1.1  rmind 	char *		str;
    150  1.1  rmind 	unsigned long	num;
    151  1.1  rmind 	filt_opts_t	filtopts;
    152  1.1  rmind 	npfvar_t *	var;
    153  1.1  rmind 	opt_proto_t	optproto;
    154  1.1  rmind 	rule_group_t	rulegroup;
    155  1.1  rmind }
    156  1.1  rmind 
    157  1.1  rmind %%
    158  1.1  rmind 
    159  1.1  rmind input
    160  1.1  rmind 	: lines
    161  1.1  rmind 	;
    162  1.1  rmind 
    163  1.1  rmind lines
    164  1.1  rmind 	: line SEPLINE lines
    165  1.1  rmind 	| line
    166  1.1  rmind 	;
    167  1.1  rmind 
    168  1.1  rmind line
    169  1.1  rmind 	: def
    170  1.1  rmind 	| table
    171  1.1  rmind 	| nat
    172  1.1  rmind 	| group
    173  1.1  rmind 	| rproc
    174  1.1  rmind 	|
    175  1.1  rmind 	;
    176  1.1  rmind 
    177  1.1  rmind def
    178  1.1  rmind 	: VAR_ID
    179  1.1  rmind 	{
    180  1.1  rmind 		cvar = npfvar_create($1);
    181  1.1  rmind 		npfvar_add(cvar);
    182  1.1  rmind 	}
    183  1.1  rmind 	  EQ definition
    184  1.1  rmind 	{
    185  1.1  rmind 		cvar = NULL;
    186  1.1  rmind 	}
    187  1.1  rmind 	;
    188  1.1  rmind 
    189  1.1  rmind definition
    190  1.1  rmind 	: list_elem
    191  1.1  rmind 	| listdef
    192  1.1  rmind 	;
    193  1.1  rmind 
    194  1.1  rmind listdef
    195  1.1  rmind 	: CURLY_OPEN list_elems CURLY_CLOSE
    196  1.1  rmind 	;
    197  1.1  rmind 
    198  1.1  rmind list_elems
    199  1.1  rmind 	: list_elem COMMA list_elems
    200  1.1  rmind 	| list_elem
    201  1.1  rmind 	;
    202  1.1  rmind 
    203  1.1  rmind list_elem
    204  1.1  rmind 	: IDENTIFIER
    205  1.1  rmind 	{
    206  1.1  rmind 		npfvar_t *vp = npfvar_create(".identifier");
    207  1.1  rmind 		npfvar_add_element(vp, NPFVAR_IDENTIFIER, $1, strlen($1) + 1);
    208  1.1  rmind 		npfvar_add_elements(cvar, vp);
    209  1.1  rmind 	}
    210  1.1  rmind 	| STRING
    211  1.1  rmind 	{
    212  1.1  rmind 		npfvar_t *vp = npfvar_create(".string");
    213  1.1  rmind 		npfvar_add_element(vp, NPFVAR_STRING, $1, strlen($1) + 1);
    214  1.1  rmind 		npfvar_add_elements(cvar, vp);
    215  1.1  rmind 	}
    216  1.1  rmind 	| NUM
    217  1.1  rmind 	{
    218  1.1  rmind 		npfvar_t *vp = npfvar_create(".num");
    219  1.1  rmind 		npfvar_add_element(vp, NPFVAR_NUM, &$1, sizeof($1));
    220  1.1  rmind 		npfvar_add_elements(cvar, vp);
    221  1.1  rmind 	}
    222  1.1  rmind 	| VAR_ID
    223  1.1  rmind 	{
    224  1.1  rmind 		npfvar_t *vp = npfvar_create(".var_id");
    225  1.1  rmind 		npfvar_add_element(vp, NPFVAR_VAR_ID, $1, strlen($1) + 1);
    226  1.1  rmind 		npfvar_add_elements(cvar, vp);
    227  1.1  rmind 	}
    228  1.1  rmind 	| addr_and_mask
    229  1.1  rmind 	{
    230  1.1  rmind 		npfvar_add_elements(cvar, $1);
    231  1.1  rmind 	}
    232  1.1  rmind 	;
    233  1.1  rmind 
    234  1.1  rmind table
    235  1.1  rmind 	: TABLE TABLE_ID TYPE table_type table_store
    236  1.1  rmind 	{
    237  1.1  rmind 		npfctl_build_table($2, $4, $5);
    238  1.1  rmind 	}
    239  1.1  rmind 	;
    240  1.1  rmind 
    241  1.1  rmind table_type
    242  1.1  rmind 	: HASH		{ $$ = NPF_TABLE_HASH; }
    243  1.1  rmind 	| TREE		{ $$ = NPF_TABLE_RBTREE; }
    244  1.1  rmind 	;
    245  1.1  rmind 
    246  1.1  rmind table_store
    247  1.1  rmind 	: TDYNAMIC	{ $$ = NULL; }
    248  1.1  rmind 	| TFILE STRING	{ $$ = $2; }
    249  1.1  rmind 	;
    250  1.1  rmind 
    251  1.1  rmind nat
    252  1.1  rmind 	: natdef
    253  1.1  rmind 	| binatdef
    254  1.1  rmind 	| rdrdef
    255  1.1  rmind 	;
    256  1.1  rmind 
    257  1.1  rmind natdef
    258  1.1  rmind 	: NAT ifindex filt_opts ARROW addr_or_iface
    259  1.1  rmind 	{
    260  1.1  rmind 		npfctl_build_nat(NPFCTL_NAT, $2, &$3, $5, NULL);
    261  1.1  rmind 	}
    262  1.1  rmind 	;
    263  1.1  rmind 
    264  1.1  rmind binatdef
    265  1.1  rmind 	: BINAT ifindex filt_opts ARROW addr_or_iface
    266  1.1  rmind 	{
    267  1.1  rmind 		npfctl_build_nat(NPFCTL_BINAT, $2, &$3, $5, $3.fo_from);
    268  1.1  rmind 	}
    269  1.1  rmind 	;
    270  1.1  rmind 
    271  1.1  rmind rdrdef
    272  1.1  rmind 	: RDR ifindex filt_opts ARROW addr_or_iface port_range
    273  1.1  rmind 	{
    274  1.1  rmind 		npfctl_build_nat(NPFCTL_RDR, $2, &$3, $5, $6);
    275  1.1  rmind 	}
    276  1.1  rmind 	;
    277  1.1  rmind 
    278  1.1  rmind rproc
    279  1.1  rmind 	: PROCEDURE STRING CURLY_OPEN procs CURLY_CLOSE
    280  1.1  rmind 	{
    281  1.1  rmind 		npfctl_build_rproc($2, $4);
    282  1.1  rmind 	}
    283  1.1  rmind 	;
    284  1.1  rmind 
    285  1.1  rmind procs
    286  1.1  rmind 	: proc_op SEPLINE procs	{ $$ = npfvar_add_elements($1, $3); }
    287  1.1  rmind 	| proc_op		{ $$ = $1; }
    288  1.1  rmind 	;
    289  1.1  rmind 
    290  1.1  rmind proc_op
    291  1.1  rmind 	: IDENTIFIER COLON moduleargs
    292  1.1  rmind 	{
    293  1.1  rmind 		proc_op_t po;
    294  1.1  rmind 
    295  1.1  rmind 		po.po_name = xstrdup($1);
    296  1.1  rmind 		po.po_opts = $3;
    297  1.1  rmind 		$$ = npfvar_create(".proc_ops");
    298  1.1  rmind 		npfvar_add_element($$, NPFVAR_PROC_OP, &po, sizeof(po));
    299  1.1  rmind 	}
    300  1.1  rmind 	|	{ $$ = NULL; }
    301  1.1  rmind 	;
    302  1.1  rmind 
    303  1.1  rmind moduleargs
    304  1.1  rmind 	: modulearg COMMA moduleargs
    305  1.1  rmind 	{
    306  1.1  rmind 		$$ = npfvar_add_elements($1, $3);
    307  1.1  rmind 	}
    308  1.1  rmind 	| modulearg	{ $$ = $1; }
    309  1.1  rmind 	|		{ $$ = NULL; }
    310  1.1  rmind 	;
    311  1.1  rmind 
    312  1.1  rmind modulearg
    313  1.1  rmind 	: moduleargname modulearg_opts
    314  1.1  rmind 	{
    315  1.1  rmind 		module_arg_t ma;
    316  1.1  rmind 
    317  1.1  rmind 		ma.ma_name = xstrdup($1);
    318  1.1  rmind 		ma.ma_opts = $2;
    319  1.1  rmind 		$$ = npfvar_create(".module_arg");
    320  1.1  rmind 		npfvar_add_element($$, NPFVAR_MODULE_ARG, &ma, sizeof(ma));
    321  1.1  rmind 	}
    322  1.1  rmind 	;
    323  1.1  rmind 
    324  1.1  rmind moduleargname
    325  1.1  rmind 	: STRING	{ $$ = $1; }
    326  1.1  rmind 	| IDENTIFIER	{ $$ = $1; }
    327  1.1  rmind 	;
    328  1.1  rmind 
    329  1.1  rmind modulearg_opts
    330  1.1  rmind 	: STRING modulearg_opts
    331  1.1  rmind 	{
    332  1.1  rmind 		npfvar_t *vp = npfvar_create(".modstring");
    333  1.1  rmind 		npfvar_add_element(vp, NPFVAR_STRING, $1, strlen($1) + 1);
    334  1.1  rmind 		$$ = $2 ? npfvar_add_elements($2, vp) : vp;
    335  1.1  rmind 	}
    336  1.1  rmind 	| IDENTIFIER modulearg_opts
    337  1.1  rmind 	{
    338  1.1  rmind 		npfvar_t *vp = npfvar_create(".modident");
    339  1.1  rmind 		npfvar_add_element(vp, NPFVAR_IDENTIFIER, $1, strlen($1) + 1);
    340  1.1  rmind 		$$ = $2 ? npfvar_add_elements($2, vp) : vp;
    341  1.1  rmind 	}
    342  1.1  rmind 	| NUM modulearg_opts
    343  1.1  rmind 	{
    344  1.1  rmind 		npfvar_t *vp = npfvar_create(".modnum");
    345  1.1  rmind 		npfvar_add_element(vp, NPFVAR_NUM, &$1, sizeof($1));
    346  1.1  rmind 		$$ = $2 ? npfvar_add_elements($2, vp) : vp;
    347  1.1  rmind 	}
    348  1.1  rmind 	|	{ $$ = NULL; }
    349  1.1  rmind 	;
    350  1.1  rmind 
    351  1.1  rmind group
    352  1.1  rmind 	: GROUP PAR_OPEN group_attr PAR_CLOSE
    353  1.1  rmind 	{
    354  1.1  rmind 		npfctl_build_group($3.rg_name, $3.rg_attr, $3.rg_ifnum);
    355  1.1  rmind 	}
    356  1.1  rmind 	  ruleset
    357  1.1  rmind 	;
    358  1.1  rmind 
    359  1.1  rmind group_attr
    360  1.1  rmind 	: group_opt COMMA group_attr
    361  1.1  rmind 	{
    362  1.1  rmind 		$$ = $3;
    363  1.1  rmind 
    364  1.1  rmind 		if (($1.rg_name && $$.rg_name) ||
    365  1.1  rmind 		    ($1.rg_ifnum && $$.rg_ifnum) ||
    366  1.1  rmind 		    ($1.rg_attr & $$.rg_attr) != 0)
    367  1.1  rmind 			yyerror("duplicate group option");
    368  1.1  rmind 
    369  1.1  rmind 		if ($1.rg_name) {
    370  1.1  rmind 			$$.rg_name = $1.rg_name;
    371  1.1  rmind 		}
    372  1.1  rmind 		if ($1.rg_attr) {
    373  1.1  rmind 			$$.rg_attr |= $1.rg_attr;
    374  1.1  rmind 		}
    375  1.1  rmind 		if ($1.rg_ifnum) {
    376  1.1  rmind 			$$.rg_ifnum = $1.rg_ifnum;
    377  1.1  rmind 		}
    378  1.1  rmind 	}
    379  1.1  rmind 	| group_opt		{ $$ = $1; }
    380  1.1  rmind 	;
    381  1.1  rmind 
    382  1.1  rmind group_opt
    383  1.1  rmind 	: DEFAULT
    384  1.1  rmind 	{
    385  1.1  rmind 		$$.rg_name = NULL;
    386  1.1  rmind 		$$.rg_ifnum = 0;
    387  1.1  rmind 		$$.rg_attr = NPF_RULE_DEFAULT;
    388  1.1  rmind 	}
    389  1.1  rmind 	| NAME STRING
    390  1.1  rmind 	{
    391  1.1  rmind 		$$.rg_name = $2;
    392  1.1  rmind 		$$.rg_ifnum = 0;
    393  1.1  rmind 		$$.rg_attr = 0;
    394  1.1  rmind 	}
    395  1.1  rmind 	| INTERFACE ifindex
    396  1.1  rmind 	{
    397  1.1  rmind 		$$.rg_name = NULL;
    398  1.1  rmind 		$$.rg_ifnum = $2;
    399  1.1  rmind 		$$.rg_attr = 0;
    400  1.1  rmind 	}
    401  1.1  rmind 	| rule_dir
    402  1.1  rmind 	{
    403  1.1  rmind 		$$.rg_name = NULL;
    404  1.1  rmind 		$$.rg_ifnum = 0;
    405  1.1  rmind 		$$.rg_attr = $1;
    406  1.1  rmind 	}
    407  1.1  rmind 	;
    408  1.1  rmind 
    409  1.1  rmind ruleset
    410  1.1  rmind 	: CURLY_OPEN rules CURLY_CLOSE
    411  1.1  rmind 	;
    412  1.1  rmind 
    413  1.1  rmind rules
    414  1.1  rmind 	: rule SEPLINE rules
    415  1.1  rmind 	| rule
    416  1.1  rmind 	;
    417  1.1  rmind 
    418  1.1  rmind rule
    419  1.1  rmind 	: block_or_pass rule_dir opt_quick on_iface opt_family
    420  1.1  rmind 	  opt_proto all_or_filt_opts opt_keep_state opt_apply
    421  1.1  rmind 	{
    422  1.1  rmind 		/*
    423  1.1  rmind 		 * Arguments: attributes, interface index, address
    424  1.1  rmind 		 * family, protocol options, filter options.
    425  1.1  rmind 		 */
    426  1.1  rmind 		npfctl_build_rule($1 | $2 | $3 | $8, $4,
    427  1.1  rmind 		    $5, &$6, &$7, $9);
    428  1.1  rmind 	}
    429  1.1  rmind 	|
    430  1.1  rmind 	;
    431  1.1  rmind 
    432  1.1  rmind block_or_pass
    433  1.1  rmind 	: BLOCK block_opts	{ $$ = $2; }
    434  1.1  rmind 	| PASS			{ $$ = NPF_RULE_PASS; }
    435  1.1  rmind 	;
    436  1.1  rmind 
    437  1.1  rmind rule_dir
    438  1.1  rmind 	: IN			{ $$ = NPF_RULE_IN; }
    439  1.1  rmind 	| OUT			{ $$ = NPF_RULE_OUT; }
    440  1.1  rmind 	|			{ $$ = NPF_RULE_IN | NPF_RULE_OUT; }
    441  1.1  rmind 	;
    442  1.1  rmind 
    443  1.1  rmind opt_quick
    444  1.1  rmind 	: QUICK			{ $$ = NPF_RULE_FINAL; }
    445  1.1  rmind 	|			{ $$ = 0; }
    446  1.1  rmind 	;
    447  1.1  rmind 
    448  1.1  rmind on_iface
    449  1.1  rmind 	: ON ifindex		{ $$ = $2; }
    450  1.1  rmind 	|			{ $$ = 0; }
    451  1.1  rmind 	;
    452  1.1  rmind 
    453  1.1  rmind family
    454  1.1  rmind 	: INET			{ $$ = AF_INET; }
    455  1.1  rmind 	| INET6			{ $$ = AF_INET6; }
    456  1.1  rmind 	;
    457  1.1  rmind 
    458  1.1  rmind opt_proto
    459  1.1  rmind 	: PROTO TCP tcp_flags_and_mask
    460  1.1  rmind 	{
    461  1.1  rmind 		$$.op_proto = IPPROTO_TCP;
    462  1.1  rmind 		$$.op_opts = $3;
    463  1.1  rmind 	}
    464  1.1  rmind 	| PROTO ICMP icmp_type_and_code
    465  1.1  rmind 	{
    466  1.1  rmind 		$$.op_proto = IPPROTO_ICMP;
    467  1.1  rmind 		$$.op_opts = $3;
    468  1.1  rmind 	}
    469  1.1  rmind 	| PROTO UDP
    470  1.1  rmind 	{
    471  1.1  rmind 		$$.op_proto = IPPROTO_UDP;
    472  1.1  rmind 		$$.op_opts = NULL;
    473  1.1  rmind 	}
    474  1.1  rmind 	|
    475  1.1  rmind 	{
    476  1.1  rmind 		$$.op_proto = -1;
    477  1.1  rmind 		$$.op_opts = NULL;
    478  1.1  rmind 	}
    479  1.1  rmind 	;
    480  1.1  rmind 
    481  1.1  rmind opt_family
    482  1.1  rmind 	: FAMILY family		{ $$ = $2; }
    483  1.1  rmind 	|			{ $$ = AF_UNSPEC; }
    484  1.1  rmind 	;
    485  1.1  rmind 
    486  1.1  rmind all_or_filt_opts
    487  1.1  rmind 	: ALL
    488  1.1  rmind 	{
    489  1.1  rmind 		$$.fo_from = NULL;
    490  1.1  rmind 		$$.fo_from_port_range = NULL;
    491  1.1  rmind 		$$.fo_to = NULL;
    492  1.1  rmind 		$$.fo_to_port_range = NULL;
    493  1.1  rmind 	}
    494  1.1  rmind 	| filt_opts	{ $$ = $1; }
    495  1.1  rmind 	;
    496  1.1  rmind 
    497  1.1  rmind opt_keep_state
    498  1.1  rmind 	: KEEP STATE	{ $$ = NPF_RULE_KEEPSTATE; }
    499  1.1  rmind 	|		{ $$ = 0; }
    500  1.1  rmind 	;
    501  1.1  rmind 
    502  1.1  rmind opt_apply
    503  1.1  rmind 	: APPLY STRING	{ $$ = $2; }
    504  1.1  rmind 	|		{ $$ = NULL; }
    505  1.1  rmind 	;
    506  1.1  rmind 
    507  1.1  rmind block_opts
    508  1.1  rmind 	: RETURNRST	{ $$ = NPF_RULE_RETRST; }
    509  1.1  rmind 	| RETURNICMP	{ $$ = NPF_RULE_RETICMP; }
    510  1.1  rmind 	| RETURN	{ $$ = NPF_RULE_RETRST | NPF_RULE_RETICMP; }
    511  1.1  rmind 	|		{ $$ = 0; }
    512  1.1  rmind 	;
    513  1.1  rmind 
    514  1.1  rmind filt_opts
    515  1.1  rmind 	: FROM filt_addr port_range TO filt_addr port_range
    516  1.1  rmind 	{
    517  1.1  rmind 		$$.fo_from = $2;
    518  1.1  rmind 		$$.fo_from_port_range = $3;
    519  1.1  rmind 		$$.fo_to = $5;
    520  1.1  rmind 		$$.fo_to_port_range = $6;
    521  1.1  rmind 	}
    522  1.1  rmind 	| FROM filt_addr port_range
    523  1.1  rmind 	{
    524  1.1  rmind 		$$.fo_from = $2;
    525  1.1  rmind 		$$.fo_from_port_range = $3;
    526  1.1  rmind 		$$.fo_to = NULL;
    527  1.1  rmind 		$$.fo_to_port_range = NULL;
    528  1.1  rmind 	}
    529  1.1  rmind 	| TO filt_addr port_range
    530  1.1  rmind 	{
    531  1.1  rmind 		$$.fo_from = NULL;
    532  1.1  rmind 		$$.fo_from_port_range = NULL;
    533  1.1  rmind 		$$.fo_to = $2;
    534  1.1  rmind 		$$.fo_to_port_range = $3;
    535  1.1  rmind 	}
    536  1.1  rmind 	;
    537  1.1  rmind 
    538  1.1  rmind filt_addr
    539  1.1  rmind 	: iface		{ $$ = $1; }
    540  1.1  rmind 	| addr_and_mask	{ $$ = $1; }
    541  1.1  rmind 	| TABLE_ID 	{ $$ = npfctl_parse_table_id($1); }
    542  1.1  rmind 	| ANY		{ $$ = NULL; }
    543  1.1  rmind 	;
    544  1.1  rmind 
    545  1.1  rmind addr_and_mask
    546  1.1  rmind 	: addr SLASH NUM
    547  1.1  rmind 	{
    548  1.1  rmind 		$$ = npfctl_parse_fam_addr_mask($1, NULL, &$3);
    549  1.1  rmind 	}
    550  1.1  rmind 	| addr SLASH HEX
    551  1.1  rmind 	{
    552  1.1  rmind 		$$ = npfctl_parse_fam_addr_mask($1, NULL, &$3);
    553  1.1  rmind 	}
    554  1.1  rmind 	| addr SLASH addr
    555  1.1  rmind 	{
    556  1.1  rmind 		$$ = npfctl_parse_fam_addr_mask($1, $3, NULL);
    557  1.1  rmind 	}
    558  1.1  rmind 	| addr
    559  1.1  rmind 	{
    560  1.1  rmind 		$$ = npfctl_parse_fam_addr_mask($1, NULL, NULL);
    561  1.1  rmind 	}
    562  1.1  rmind 	;
    563  1.1  rmind 
    564  1.1  rmind addr_or_iface
    565  1.1  rmind 	: addr_and_mask	{ assert($1 != NULL); $$ = $1; }
    566  1.1  rmind 	| iface		{ assert($1 != NULL); $$ = $1; }
    567  1.1  rmind 	;
    568  1.1  rmind 
    569  1.1  rmind addr
    570  1.1  rmind 	: IPV4ADDR	{ $$ = $1; }
    571  1.1  rmind 	| IPV6ADDR	{ $$ = $1; }
    572  1.1  rmind 	;
    573  1.1  rmind 
    574  1.1  rmind 
    575  1.1  rmind port_range
    576  1.1  rmind 	: PORT port		/* just port */
    577  1.1  rmind 	{
    578  1.1  rmind 		$$ = npfctl_parse_port_range($2, $2);
    579  1.1  rmind 	}
    580  1.1  rmind 	| PORT port MINUS port	/* port from:to */
    581  1.1  rmind 	{
    582  1.1  rmind 		$$ = npfctl_parse_port_range($2, $4);
    583  1.1  rmind 	}
    584  1.1  rmind 	|
    585  1.1  rmind 	{
    586  1.1  rmind 		$$ = NULL;
    587  1.1  rmind 	}
    588  1.1  rmind 	;
    589  1.1  rmind 
    590  1.1  rmind port
    591  1.1  rmind 	: NUM		{ $$ = htons($1); }
    592  1.1  rmind 	| IDENTIFIER	{ $$ = npfctl_portno($1); }
    593  1.1  rmind 	| VAR_ID
    594  1.1  rmind 	{
    595  1.1  rmind 		char *s = npfvar_expand_string(npfvar_lookup($1));
    596  1.1  rmind 		$$ = npfctl_portno(s);
    597  1.1  rmind 	}
    598  1.1  rmind 	;
    599  1.1  rmind 
    600  1.1  rmind icmp_type_and_code
    601  1.1  rmind 	: ICMPTYPE icmp_type
    602  1.1  rmind 	{
    603  1.1  rmind 		$$ = npfctl_parse_icmp($2, -1);
    604  1.1  rmind 	}
    605  1.1  rmind 	| ICMPTYPE icmp_type CODE NUM
    606  1.1  rmind 	{
    607  1.1  rmind 		$$ = npfctl_parse_icmp($2, $4);
    608  1.1  rmind 	}
    609  1.1  rmind 	| ICMPTYPE icmp_type CODE IDENTIFIER
    610  1.1  rmind 	{
    611  1.1  rmind 		$$ = npfctl_parse_icmp($2, npfctl_icmpcode($2, $4));
    612  1.1  rmind 	}
    613  1.1  rmind 	| ICMPTYPE icmp_type CODE VAR_ID
    614  1.1  rmind 	{
    615  1.1  rmind 		char *s = npfvar_expand_string(npfvar_lookup($4));
    616  1.1  rmind 		$$ = npfctl_parse_icmp($2, npfctl_icmpcode($2, s));
    617  1.1  rmind 	}
    618  1.1  rmind 	|
    619  1.1  rmind 	{
    620  1.1  rmind 		$$ = npfctl_parse_icmp(-1, -1);
    621  1.1  rmind 	}
    622  1.1  rmind 	;
    623  1.1  rmind 
    624  1.1  rmind tcp_flags_and_mask
    625  1.1  rmind 	: FLAGS tcp_flags SLASH tcp_flags
    626  1.1  rmind 	{
    627  1.1  rmind 		npfvar_add_elements($2, $4);
    628  1.1  rmind 		$$ = $2;
    629  1.1  rmind 	}
    630  1.1  rmind 	| FLAGS tcp_flags
    631  1.1  rmind 	{
    632  1.1  rmind 		char *s = npfvar_get_data($2, NPFVAR_TCPFLAG, 0);
    633  1.1  rmind 		npfvar_add_elements($2, npfctl_parse_tcpflag(s));
    634  1.1  rmind 		$$ = $2;
    635  1.1  rmind 	}
    636  1.1  rmind 	|		{ $$ = NULL; }
    637  1.1  rmind 	;
    638  1.1  rmind 
    639  1.1  rmind tcp_flags
    640  1.1  rmind 	: IDENTIFIER	{ $$ = npfctl_parse_tcpflag($1); }
    641  1.1  rmind 	;
    642  1.1  rmind 
    643  1.1  rmind icmp_type
    644  1.1  rmind 	: NUM		{ $$ = $1; }
    645  1.1  rmind 	| IDENTIFIER	{ $$ = npfctl_icmptype($1); }
    646  1.1  rmind 	| VAR_ID
    647  1.1  rmind 	{
    648  1.1  rmind 		char *s = npfvar_expand_string(npfvar_lookup($1));
    649  1.1  rmind 		$$ = npfctl_icmptype(s);
    650  1.1  rmind 	}
    651  1.1  rmind 	;
    652  1.1  rmind 
    653  1.1  rmind iface
    654  1.1  rmind 	: iface_name
    655  1.1  rmind 	{
    656  1.1  rmind 		$$ = npfctl_parse_iface($1);
    657  1.1  rmind 	}
    658  1.1  rmind 	| VAR_ID
    659  1.1  rmind 	{
    660  1.1  rmind 		npfvar_t *vp = npfvar_lookup($1);
    661  1.1  rmind 		const int type = npfvar_get_type(vp);
    662  1.1  rmind 
    663  1.1  rmind 		switch (type) {
    664  1.1  rmind 		case NPFVAR_STRING:
    665  1.1  rmind 			$$ = npfctl_parse_iface(npfvar_expand_string(vp));
    666  1.1  rmind 			break;
    667  1.1  rmind 		case NPFVAR_FAM:
    668  1.1  rmind 			$$ = vp;
    669  1.1  rmind 			break;
    670  1.1  rmind 		case -1:
    671  1.1  rmind 			yyerror("undefined variable '%s' for interface", $1);
    672  1.1  rmind 			break;
    673  1.1  rmind 		default:
    674  1.1  rmind 			yyerror("wrong variable '%s' type '%s' or interface",
    675  1.1  rmind 			    $1, npfvar_type(type));
    676  1.1  rmind 			$$ = NULL;
    677  1.1  rmind 			break;
    678  1.1  rmind 		}
    679  1.1  rmind 	}
    680  1.1  rmind 	;
    681  1.1  rmind 
    682  1.1  rmind ifindex
    683  1.1  rmind 	: iface_name
    684  1.1  rmind 	{
    685  1.1  rmind 		$$ = npfctl_find_ifindex($1);
    686  1.1  rmind 	}
    687  1.1  rmind 	| VAR_ID
    688  1.1  rmind 	{
    689  1.1  rmind 		npfvar_t *vp = npfvar_lookup($1);
    690  1.1  rmind 		const int type = npfvar_get_type(vp);
    691  1.1  rmind 
    692  1.1  rmind 		switch (type) {
    693  1.1  rmind 		case NPFVAR_STRING:
    694  1.1  rmind 			$$ = npfctl_find_ifindex(npfvar_expand_string(vp));
    695  1.1  rmind 			break;
    696  1.1  rmind 		case -1:
    697  1.1  rmind 			yyerror("undefined variable '%s' for interface", $1);
    698  1.1  rmind 			break;
    699  1.1  rmind 		default:
    700  1.1  rmind 			yyerror("wrong variable '%s' type '%s' for interface",
    701  1.1  rmind 			    $1, npfvar_type(type));
    702  1.1  rmind 			$$ = -1;
    703  1.1  rmind 			break;
    704  1.1  rmind 		}
    705  1.1  rmind 	}
    706  1.1  rmind 	;
    707  1.1  rmind 
    708  1.1  rmind iface_name
    709  1.1  rmind 	: IDENTIFIER	{ $$ = $1; }
    710  1.1  rmind 	| STRING	{ $$ = $1; }
    711  1.1  rmind 	;
    712  1.1  rmind 
    713  1.1  rmind %%
    714