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