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