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