Home | History | Annotate | Line # | Download | only in npfctl
npf_parse.y revision 1.29
      1  1.29     rmind /*	$NetBSD: npf_parse.y,v 1.29 2013/11/19 00:28:41 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 void
     50   1.1     rmind yyerror(const char *fmt, ...)
     51   1.1     rmind {
     52   1.1     rmind 	extern int yyleng;
     53   1.1     rmind 	extern char *yytext;
     54   1.1     rmind 
     55  1.15     rmind 	char *msg, *context = estrndup(yytext, yyleng);
     56  1.14     rmind 	bool eol = (*context == '\n');
     57   1.1     rmind 	va_list ap;
     58   1.1     rmind 
     59   1.1     rmind 	va_start(ap, fmt);
     60   1.1     rmind 	vasprintf(&msg, fmt, ap);
     61   1.1     rmind 	va_end(ap);
     62   1.1     rmind 
     63  1.14     rmind 	fprintf(stderr, "%s:%d:%d: %s", yyfilename,
     64  1.14     rmind 	    yylineno - (int)eol, yycolumn, msg);
     65  1.14     rmind 	if (!eol) {
     66  1.14     rmind 		size_t len = strlen(context);
     67  1.16     rmind 		char *dst = ecalloc(1, len * 4 + 1);
     68  1.14     rmind 
     69  1.14     rmind 		strvisx(dst, context, len, VIS_WHITE|VIS_CSTYLE);
     70  1.14     rmind 		fprintf(stderr, " near '%s'", dst);
     71  1.14     rmind 	}
     72  1.14     rmind 	fprintf(stderr, "\n");
     73   1.1     rmind 	exit(EXIT_FAILURE);
     74   1.1     rmind }
     75   1.1     rmind 
     76  1.18     rmind #define	CHECK_PARSER_FILE				\
     77  1.18     rmind 	if (yyparsetarget != NPFCTL_PARSE_FILE)		\
     78  1.18     rmind 		yyerror("rule must be in the group");
     79  1.18     rmind 
     80  1.18     rmind #define	CHECK_PARSER_STRING				\
     81  1.18     rmind 	if (yyparsetarget != NPFCTL_PARSE_STRING)	\
     82  1.18     rmind 		yyerror("invalid rule syntax");
     83  1.18     rmind 
     84   1.1     rmind %}
     85   1.1     rmind 
     86  1.22  christos %token			ALG
     87   1.1     rmind %token			ALL
     88   1.1     rmind %token			ANY
     89   1.1     rmind %token			APPLY
     90   1.8     rmind %token			ARROWBOTH
     91   1.8     rmind %token			ARROWLEFT
     92   1.8     rmind %token			ARROWRIGHT
     93   1.1     rmind %token			BLOCK
     94   1.1     rmind %token			CURLY_CLOSE
     95   1.1     rmind %token			CURLY_OPEN
     96   1.1     rmind %token			CODE
     97   1.1     rmind %token			COLON
     98   1.1     rmind %token			COMMA
     99   1.1     rmind %token			DEFAULT
    100   1.1     rmind %token			TDYNAMIC
    101   1.8     rmind %token			TSTATIC
    102   1.1     rmind %token			EQ
    103   1.1     rmind %token			TFILE
    104   1.1     rmind %token			FLAGS
    105   1.1     rmind %token			FROM
    106   1.1     rmind %token			GROUP
    107   1.1     rmind %token			HASH
    108   1.1     rmind %token			ICMPTYPE
    109   1.1     rmind %token			ID
    110  1.17     rmind %token			IFNET
    111   1.1     rmind %token			IN
    112  1.29     rmind %token			INET4
    113   1.1     rmind %token			INET6
    114   1.1     rmind %token			INTERFACE
    115   1.8     rmind %token			MAP
    116   1.1     rmind %token			MINUS
    117   1.1     rmind %token			NAME
    118   1.1     rmind %token			ON
    119   1.1     rmind %token			OUT
    120   1.1     rmind %token			PAR_CLOSE
    121   1.1     rmind %token			PAR_OPEN
    122   1.1     rmind %token			PASS
    123  1.26     rmind %token			PCAP_FILTER
    124   1.1     rmind %token			PORT
    125   1.1     rmind %token			PROCEDURE
    126   1.1     rmind %token			PROTO
    127   1.1     rmind %token			FAMILY
    128   1.7     rmind %token			FINAL
    129  1.18     rmind %token			FORW
    130   1.1     rmind %token			RETURN
    131   1.1     rmind %token			RETURNICMP
    132   1.1     rmind %token			RETURNRST
    133  1.21     rmind %token			RULESET
    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.29     rmind %type	<str>		addr, some_name, table_store
    156  1.29     rmind %type	<str>		proc_param_val, opt_apply, ifname, on_ifname, ifref
    157  1.27     rmind %type	<num>		port, opt_final, number, afamily, opt_family
    158  1.26     rmind %type	<num>		block_or_pass, rule_dir, group_dir, block_opts
    159   1.8     rmind %type	<num>		opt_stateful, icmp_type, table_type, map_sd, map_type
    160  1.29     rmind %type	<var>		ifaddrs, addr_or_ifaddr, port_range, icmp_type_and_code
    161   1.1     rmind %type	<var>		filt_addr, addr_and_mask, tcp_flags, tcp_flags_and_mask
    162  1.13     rmind %type	<var>		procs, proc_call, proc_param_list, proc_param
    163  1.29     rmind %type	<var>		element, list_elems, list, value
    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.26     rmind %type	<rulegroup>	group_opts
    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.28     rmind 	: vardef
    194   1.1     rmind 	| table
    195   1.8     rmind 	| map
    196   1.1     rmind 	| group
    197   1.1     rmind 	| rproc
    198  1.22  christos 	| alg
    199   1.1     rmind 	|
    200   1.1     rmind 	;
    201   1.1     rmind 
    202  1.28     rmind alg
    203  1.28     rmind 	: ALG STRING
    204  1.28     rmind 	{
    205  1.28     rmind 		npfctl_build_alg($2);
    206  1.28     rmind 	}
    207  1.28     rmind 	;
    208  1.28     rmind 
    209  1.28     rmind /*
    210  1.28     rmind  * A value - an element or a list of elements.
    211  1.28     rmind  * Can be assigned to a variable or used inline.
    212  1.28     rmind  */
    213  1.28     rmind 
    214  1.28     rmind vardef
    215  1.29     rmind 	: VAR_ID EQ value
    216   1.1     rmind 	{
    217  1.29     rmind 		npfvar_add($3, $1);
    218   1.1     rmind 	}
    219   1.1     rmind 	;
    220   1.1     rmind 
    221  1.28     rmind value
    222  1.28     rmind 	: element
    223  1.28     rmind 	| list
    224   1.1     rmind 	;
    225   1.1     rmind 
    226  1.28     rmind list
    227   1.1     rmind 	: CURLY_OPEN list_elems CURLY_CLOSE
    228  1.29     rmind 	{
    229  1.29     rmind 		$$ = $2;
    230  1.29     rmind 	}
    231   1.1     rmind 	;
    232   1.1     rmind 
    233   1.1     rmind list_elems
    234  1.28     rmind 	: element COMMA list_elems
    235  1.29     rmind 	{
    236  1.29     rmind 		npfvar_add_elements($1, $3);
    237  1.29     rmind 	}
    238  1.28     rmind 	| element
    239   1.1     rmind 	;
    240   1.1     rmind 
    241  1.28     rmind element
    242   1.1     rmind 	: IDENTIFIER
    243   1.1     rmind 	{
    244  1.29     rmind 		$$ = npfvar_create_from_string(NPFVAR_IDENTIFIER, $1);
    245   1.1     rmind 	}
    246   1.1     rmind 	| STRING
    247   1.1     rmind 	{
    248  1.29     rmind 		$$ = npfvar_create_from_string(NPFVAR_STRING, $1);
    249   1.1     rmind 	}
    250  1.23  christos 	| number MINUS number
    251   1.5  christos 	{
    252  1.29     rmind 		$$ = npfctl_parse_port_range($1, $3);
    253   1.5  christos 	}
    254  1.23  christos 	| number
    255   1.1     rmind 	{
    256  1.29     rmind 		$$ = npfvar_create_element(NPFVAR_NUM, &$1, sizeof($1));
    257   1.1     rmind 	}
    258   1.1     rmind 	| VAR_ID
    259   1.1     rmind 	{
    260  1.29     rmind 		$$ = npfvar_create_from_string(NPFVAR_VAR_ID, $1);
    261   1.1     rmind 	}
    262  1.29     rmind 	| TABLE_ID		{ $$ = npfctl_parse_table_id($1); }
    263  1.29     rmind 	| ifaddrs		{ $$ = $1; }
    264  1.29     rmind 	| addr_and_mask		{ $$ = $1; }
    265   1.1     rmind 	;
    266   1.1     rmind 
    267  1.28     rmind /*
    268  1.28     rmind  * Table definition.
    269  1.28     rmind  */
    270  1.28     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.28     rmind /*
    289  1.28     rmind  * Map definition.
    290  1.28     rmind  */
    291  1.28     rmind 
    292   1.8     rmind map_sd
    293   1.8     rmind 	: TSTATIC	{ $$ = NPFCTL_NAT_STATIC; }
    294   1.8     rmind 	| TDYNAMIC	{ $$ = NPFCTL_NAT_DYNAMIC; }
    295   1.8     rmind 	|		{ $$ = NPFCTL_NAT_DYNAMIC; }
    296   1.1     rmind 	;
    297   1.1     rmind 
    298   1.8     rmind map_type
    299   1.8     rmind 	: ARROWBOTH	{ $$ = NPF_NATIN | NPF_NATOUT; }
    300   1.8     rmind 	| ARROWLEFT	{ $$ = NPF_NATIN; }
    301   1.8     rmind 	| ARROWRIGHT	{ $$ = NPF_NATOUT; }
    302   1.8     rmind 	;
    303   1.8     rmind 
    304   1.8     rmind mapseg
    305  1.29     rmind 	: addr_or_ifaddr port_range
    306   1.1     rmind 	{
    307   1.8     rmind 		$$.ap_netaddr = $1;
    308   1.8     rmind 		$$.ap_portrange = $2;
    309   1.1     rmind 	}
    310   1.1     rmind 	;
    311   1.1     rmind 
    312   1.8     rmind map
    313  1.29     rmind 	: MAP ifref map_sd mapseg map_type mapseg PASS filt_opts
    314   1.1     rmind 	{
    315  1.12     rmind 		npfctl_build_natseg($3, $5, $2, &$4, &$6, &$8);
    316   1.1     rmind 	}
    317  1.29     rmind 	| MAP ifref map_sd mapseg map_type mapseg
    318   1.1     rmind 	{
    319  1.12     rmind 		npfctl_build_natseg($3, $5, $2, &$4, &$6, NULL);
    320   1.1     rmind 	}
    321  1.26     rmind 	| MAP RULESET group_opts
    322  1.21     rmind 	{
    323  1.27     rmind 		npfctl_build_maprset($3.rg_name, $3.rg_attr, $3.rg_ifname);
    324  1.21     rmind 	}
    325   1.1     rmind 	;
    326   1.1     rmind 
    327  1.28     rmind /*
    328  1.28     rmind  * Rule procedure definition and its parameters.
    329  1.28     rmind  */
    330  1.28     rmind 
    331   1.1     rmind rproc
    332   1.1     rmind 	: PROCEDURE STRING CURLY_OPEN procs CURLY_CLOSE
    333   1.1     rmind 	{
    334   1.1     rmind 		npfctl_build_rproc($2, $4);
    335   1.1     rmind 	}
    336   1.1     rmind 	;
    337   1.1     rmind 
    338   1.1     rmind procs
    339  1.13     rmind 	: proc_call SEPLINE procs
    340  1.13     rmind 	{
    341  1.13     rmind 		$$ = npfvar_add_elements($1, $3);
    342  1.13     rmind 	}
    343  1.13     rmind 	| proc_call	{ $$ = $1; }
    344   1.1     rmind 	;
    345   1.1     rmind 
    346  1.13     rmind proc_call
    347  1.13     rmind 	: IDENTIFIER COLON proc_param_list
    348   1.1     rmind 	{
    349  1.13     rmind 		proc_call_t pc;
    350   1.1     rmind 
    351  1.15     rmind 		pc.pc_name = estrdup($1);
    352  1.13     rmind 		pc.pc_opts = $3;
    353  1.29     rmind 
    354  1.29     rmind 		$$ = npfvar_create_element(NPFVAR_PROC, &pc, sizeof(pc));
    355   1.1     rmind 	}
    356  1.29     rmind 	|		{ $$ = NULL; }
    357   1.1     rmind 	;
    358   1.1     rmind 
    359  1.13     rmind proc_param_list
    360  1.13     rmind 	: proc_param COMMA proc_param_list
    361   1.1     rmind 	{
    362   1.1     rmind 		$$ = npfvar_add_elements($1, $3);
    363   1.1     rmind 	}
    364  1.13     rmind 	| proc_param	{ $$ = $1; }
    365   1.1     rmind 	|		{ $$ = NULL; }
    366   1.1     rmind 	;
    367   1.1     rmind 
    368  1.13     rmind proc_param
    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.29     rmind 
    376  1.29     rmind 		$$ = npfvar_create_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.28     rmind /*
    388  1.28     rmind  * Group and dynamic ruleset definition.
    389  1.28     rmind  */
    390  1.28     rmind 
    391   1.1     rmind group
    392  1.26     rmind 	: GROUP group_opts
    393   1.1     rmind 	{
    394  1.29     rmind 		/* Build a group.  Increase the nesting level. */
    395  1.26     rmind 		npfctl_build_group($2.rg_name, $2.rg_attr,
    396  1.27     rmind 		    $2.rg_ifname, $2.rg_default);
    397  1.18     rmind 	}
    398  1.18     rmind 	  ruleset_block
    399  1.18     rmind 	{
    400  1.18     rmind 		/* Decrease the nesting level. */
    401  1.18     rmind 		npfctl_build_group_end();
    402   1.1     rmind 	}
    403   1.1     rmind 	;
    404   1.1     rmind 
    405  1.21     rmind ruleset
    406  1.26     rmind 	: RULESET group_opts
    407  1.21     rmind 	{
    408  1.21     rmind 		/* Ruleset is a dynamic group. */
    409  1.26     rmind 		npfctl_build_group($2.rg_name, $2.rg_attr | NPF_RULE_DYNAMIC,
    410  1.27     rmind 		    $2.rg_ifname, $2.rg_default);
    411  1.21     rmind 		npfctl_build_group_end();
    412  1.21     rmind 	}
    413  1.26     rmind 	;
    414  1.21     rmind 
    415  1.26     rmind group_dir
    416  1.26     rmind 	: FORW		{ $$ = NPF_RULE_FORW; }
    417  1.26     rmind 	| rule_dir
    418   1.1     rmind 	;
    419   1.1     rmind 
    420  1.26     rmind group_opts
    421   1.1     rmind 	: DEFAULT
    422   1.1     rmind 	{
    423  1.18     rmind 		memset(&$$, 0, sizeof(rule_group_t));
    424  1.18     rmind 		$$.rg_default = true;
    425   1.1     rmind 	}
    426  1.27     rmind 	| STRING group_dir on_ifname
    427   1.1     rmind 	{
    428  1.18     rmind 		memset(&$$, 0, sizeof(rule_group_t));
    429  1.26     rmind 		$$.rg_name = $1;
    430  1.26     rmind 		$$.rg_attr = $2;
    431  1.27     rmind 		$$.rg_ifname = $3;
    432   1.1     rmind 	}
    433   1.1     rmind 	;
    434   1.1     rmind 
    435  1.18     rmind ruleset_block
    436  1.21     rmind 	: CURLY_OPEN ruleset_def CURLY_CLOSE
    437  1.18     rmind 	;
    438  1.18     rmind 
    439  1.21     rmind ruleset_def
    440  1.21     rmind 	: rule_group SEPLINE ruleset_def
    441  1.18     rmind 	| rule_group
    442   1.1     rmind 	;
    443   1.1     rmind 
    444  1.18     rmind rule_group
    445  1.18     rmind 	: rule
    446  1.18     rmind 	| group
    447  1.21     rmind 	| ruleset
    448  1.18     rmind 	|
    449  1.24     rmind 	;
    450   1.1     rmind 
    451  1.28     rmind /*
    452  1.28     rmind  * Rule and misc.
    453  1.28     rmind  */
    454  1.28     rmind 
    455   1.1     rmind rule
    456  1.27     rmind 	: block_or_pass opt_stateful rule_dir opt_final on_ifname
    457  1.17     rmind 	  opt_family opt_proto all_or_filt_opts opt_apply
    458   1.1     rmind 	{
    459   1.7     rmind 		npfctl_build_rule($1 | $2 | $3 | $4, $5,
    460  1.26     rmind 		    $6, &$7, &$8, NULL, $9);
    461  1.26     rmind 	}
    462  1.27     rmind 	| block_or_pass opt_stateful rule_dir opt_final on_ifname
    463  1.26     rmind 	  PCAP_FILTER STRING opt_apply
    464  1.26     rmind 	{
    465  1.26     rmind 		npfctl_build_rule($1 | $2 | $3 | $4, $5,
    466  1.26     rmind 		    AF_UNSPEC, NULL, NULL, $7, $8);
    467   1.1     rmind 	}
    468   1.1     rmind 	;
    469   1.1     rmind 
    470   1.1     rmind block_or_pass
    471   1.1     rmind 	: BLOCK block_opts	{ $$ = $2; }
    472   1.1     rmind 	| PASS			{ $$ = NPF_RULE_PASS; }
    473   1.1     rmind 	;
    474   1.1     rmind 
    475   1.1     rmind rule_dir
    476   1.1     rmind 	: IN			{ $$ = NPF_RULE_IN; }
    477   1.1     rmind 	| OUT			{ $$ = NPF_RULE_OUT; }
    478   1.1     rmind 	|			{ $$ = NPF_RULE_IN | NPF_RULE_OUT; }
    479   1.1     rmind 	;
    480   1.1     rmind 
    481   1.7     rmind opt_final
    482   1.7     rmind 	: FINAL			{ $$ = NPF_RULE_FINAL; }
    483   1.1     rmind 	|			{ $$ = 0; }
    484   1.1     rmind 	;
    485   1.1     rmind 
    486  1.27     rmind on_ifname
    487  1.29     rmind 	: ON ifref		{ $$ = $2; }
    488  1.27     rmind 	|			{ $$ = NULL; }
    489   1.1     rmind 	;
    490   1.1     rmind 
    491  1.17     rmind afamily
    492  1.29     rmind 	: INET4			{ $$ = AF_INET; }
    493  1.17     rmind 	| INET6			{ $$ = AF_INET6; }
    494  1.17     rmind 	;
    495  1.17     rmind 
    496   1.9     rmind opt_family
    497  1.17     rmind 	: FAMILY afamily	{ $$ = $2; }
    498   1.9     rmind 	|			{ $$ = AF_UNSPEC; }
    499   1.1     rmind 	;
    500   1.1     rmind 
    501   1.1     rmind opt_proto
    502   1.1     rmind 	: PROTO TCP tcp_flags_and_mask
    503   1.1     rmind 	{
    504   1.1     rmind 		$$.op_proto = IPPROTO_TCP;
    505   1.1     rmind 		$$.op_opts = $3;
    506   1.1     rmind 	}
    507   1.1     rmind 	| PROTO ICMP icmp_type_and_code
    508   1.1     rmind 	{
    509   1.1     rmind 		$$.op_proto = IPPROTO_ICMP;
    510   1.1     rmind 		$$.op_opts = $3;
    511   1.1     rmind 	}
    512  1.11       spz 	| PROTO ICMP6 icmp_type_and_code
    513  1.11       spz 	{
    514  1.11       spz 		$$.op_proto = IPPROTO_ICMPV6;
    515  1.11       spz 		$$.op_opts = $3;
    516  1.11       spz 	}
    517   1.9     rmind 	| PROTO some_name
    518   1.9     rmind 	{
    519   1.9     rmind 		$$.op_proto = npfctl_protono($2);
    520   1.9     rmind 		$$.op_opts = NULL;
    521   1.9     rmind 	}
    522  1.23  christos 	| PROTO number
    523   1.1     rmind 	{
    524   1.9     rmind 		$$.op_proto = $2;
    525   1.1     rmind 		$$.op_opts = NULL;
    526   1.1     rmind 	}
    527   1.1     rmind 	|
    528   1.1     rmind 	{
    529   1.1     rmind 		$$.op_proto = -1;
    530   1.1     rmind 		$$.op_opts = NULL;
    531   1.1     rmind 	}
    532   1.1     rmind 	;
    533   1.1     rmind 
    534   1.1     rmind all_or_filt_opts
    535   1.1     rmind 	: ALL
    536   1.1     rmind 	{
    537   1.8     rmind 		$$.fo_from.ap_netaddr = NULL;
    538   1.8     rmind 		$$.fo_from.ap_portrange = NULL;
    539   1.8     rmind 		$$.fo_to.ap_netaddr = NULL;
    540   1.8     rmind 		$$.fo_to.ap_portrange = NULL;
    541   1.1     rmind 	}
    542   1.1     rmind 	| filt_opts	{ $$ = $1; }
    543   1.1     rmind 	;
    544   1.1     rmind 
    545   1.7     rmind opt_stateful
    546   1.9     rmind 	: STATEFUL	{ $$ = NPF_RULE_STATEFUL; }
    547   1.1     rmind 	|		{ $$ = 0; }
    548   1.1     rmind 	;
    549   1.1     rmind 
    550   1.1     rmind opt_apply
    551   1.1     rmind 	: APPLY STRING	{ $$ = $2; }
    552   1.1     rmind 	|		{ $$ = NULL; }
    553   1.1     rmind 	;
    554   1.1     rmind 
    555   1.1     rmind block_opts
    556   1.1     rmind 	: RETURNRST	{ $$ = NPF_RULE_RETRST; }
    557   1.1     rmind 	| RETURNICMP	{ $$ = NPF_RULE_RETICMP; }
    558   1.1     rmind 	| RETURN	{ $$ = NPF_RULE_RETRST | NPF_RULE_RETICMP; }
    559   1.1     rmind 	|		{ $$ = 0; }
    560   1.1     rmind 	;
    561   1.1     rmind 
    562   1.1     rmind filt_opts
    563   1.1     rmind 	: FROM filt_addr port_range TO filt_addr port_range
    564   1.1     rmind 	{
    565   1.8     rmind 		$$.fo_from.ap_netaddr = $2;
    566   1.8     rmind 		$$.fo_from.ap_portrange = $3;
    567   1.8     rmind 		$$.fo_to.ap_netaddr = $5;
    568   1.8     rmind 		$$.fo_to.ap_portrange = $6;
    569   1.1     rmind 	}
    570   1.1     rmind 	| FROM filt_addr port_range
    571   1.1     rmind 	{
    572   1.8     rmind 		$$.fo_from.ap_netaddr = $2;
    573   1.8     rmind 		$$.fo_from.ap_portrange = $3;
    574   1.8     rmind 		$$.fo_to.ap_netaddr = NULL;
    575   1.8     rmind 		$$.fo_to.ap_portrange = NULL;
    576   1.1     rmind 	}
    577   1.1     rmind 	| TO filt_addr port_range
    578   1.1     rmind 	{
    579   1.8     rmind 		$$.fo_from.ap_netaddr = NULL;
    580   1.8     rmind 		$$.fo_from.ap_portrange = NULL;
    581   1.8     rmind 		$$.fo_to.ap_netaddr = $2;
    582   1.8     rmind 		$$.fo_to.ap_portrange = $3;
    583   1.1     rmind 	}
    584   1.1     rmind 	;
    585   1.1     rmind 
    586   1.1     rmind filt_addr
    587  1.29     rmind 	: addr_or_ifaddr	{ $$ = $1; }
    588   1.4     rmind 	| TABLE_ID		{ $$ = npfctl_parse_table_id($1); }
    589   1.4     rmind 	| ANY			{ $$ = NULL; }
    590   1.1     rmind 	;
    591   1.1     rmind 
    592   1.1     rmind addr_and_mask
    593  1.23  christos 	: addr SLASH number
    594   1.1     rmind 	{
    595   1.1     rmind 		$$ = npfctl_parse_fam_addr_mask($1, NULL, &$3);
    596   1.1     rmind 	}
    597   1.1     rmind 	| addr SLASH addr
    598   1.1     rmind 	{
    599   1.1     rmind 		$$ = npfctl_parse_fam_addr_mask($1, $3, NULL);
    600   1.1     rmind 	}
    601   1.1     rmind 	| addr
    602   1.1     rmind 	{
    603   1.1     rmind 		$$ = npfctl_parse_fam_addr_mask($1, NULL, NULL);
    604   1.1     rmind 	}
    605   1.1     rmind 	;
    606   1.1     rmind 
    607  1.29     rmind addr_or_ifaddr
    608   1.9     rmind 	: addr_and_mask
    609   1.9     rmind 	{
    610   1.9     rmind 		assert($1 != NULL);
    611   1.9     rmind 		$$ = $1;
    612   1.9     rmind 	}
    613  1.29     rmind 	| ifaddrs
    614   1.4     rmind 	{
    615  1.17     rmind 		ifnet_addr_t *ifna = npfvar_get_data($1, NPFVAR_INTERFACE, 0);
    616  1.17     rmind 		$$ = ifna->ifna_addrs;
    617   1.4     rmind 	}
    618   1.4     rmind 	| VAR_ID
    619   1.4     rmind 	{
    620   1.4     rmind 		npfvar_t *vp = npfvar_lookup($1);
    621  1.19  christos 		int type = npfvar_get_type(vp, 0);
    622  1.17     rmind 		ifnet_addr_t *ifna;
    623   1.4     rmind 
    624  1.19  christos again:
    625   1.4     rmind 		switch (type) {
    626  1.19  christos 		case NPFVAR_IDENTIFIER:
    627  1.19  christos 		case NPFVAR_STRING:
    628  1.19  christos 			vp = npfctl_parse_ifnet(npfvar_expand_string(vp),
    629  1.19  christos 			    AF_UNSPEC);
    630  1.19  christos 			type = npfvar_get_type(vp, 0);
    631  1.19  christos 			goto again;
    632   1.4     rmind 		case NPFVAR_FAM:
    633   1.4     rmind 			$$ = vp;
    634   1.4     rmind 			break;
    635  1.17     rmind 		case NPFVAR_INTERFACE:
    636  1.17     rmind 			ifna = npfvar_get_data(vp, type, 0);
    637  1.17     rmind 			$$ = ifna->ifna_addrs;
    638  1.17     rmind 			break;
    639   1.4     rmind 		case -1:
    640  1.17     rmind 			yyerror("undefined variable '%s'", $1);
    641   1.4     rmind 			break;
    642   1.4     rmind 		default:
    643  1.17     rmind 			yyerror("wrong variable '%s' type '%s' for address "
    644  1.17     rmind 			    "or interface", $1, npfvar_type(type));
    645   1.4     rmind 			break;
    646   1.4     rmind 		}
    647   1.4     rmind 	}
    648   1.1     rmind 	;
    649   1.1     rmind 
    650   1.1     rmind addr
    651   1.1     rmind 	: IPV4ADDR	{ $$ = $1; }
    652   1.1     rmind 	| IPV6ADDR	{ $$ = $1; }
    653   1.1     rmind 	;
    654   1.1     rmind 
    655   1.1     rmind port_range
    656   1.1     rmind 	: PORT port		/* just port */
    657   1.1     rmind 	{
    658   1.1     rmind 		$$ = npfctl_parse_port_range($2, $2);
    659   1.1     rmind 	}
    660   1.8     rmind 	| PORT port MINUS port	/* port from-to */
    661   1.1     rmind 	{
    662   1.1     rmind 		$$ = npfctl_parse_port_range($2, $4);
    663   1.1     rmind 	}
    664   1.8     rmind 	| PORT VAR_ID
    665   1.8     rmind 	{
    666   1.5  christos 		$$ = npfctl_parse_port_range_variable($2);
    667   1.5  christos 	}
    668   1.1     rmind 	|
    669   1.1     rmind 	{
    670   1.1     rmind 		$$ = NULL;
    671   1.1     rmind 	}
    672   1.1     rmind 	;
    673   1.1     rmind 
    674   1.1     rmind port
    675  1.23  christos 	: number	{ $$ = $1; }
    676   1.1     rmind 	| IDENTIFIER	{ $$ = npfctl_portno($1); }
    677  1.20  christos 	| STRING	{ $$ = npfctl_portno($1); }
    678   1.1     rmind 	;
    679   1.1     rmind 
    680   1.1     rmind icmp_type_and_code
    681   1.1     rmind 	: ICMPTYPE icmp_type
    682   1.1     rmind 	{
    683  1.11       spz 		$$ = npfctl_parse_icmp($<num>0, $2, -1);
    684   1.1     rmind 	}
    685  1.23  christos 	| ICMPTYPE icmp_type CODE number
    686   1.1     rmind 	{
    687  1.11       spz 		$$ = npfctl_parse_icmp($<num>0, $2, $4);
    688   1.1     rmind 	}
    689   1.1     rmind 	| ICMPTYPE icmp_type CODE IDENTIFIER
    690   1.1     rmind 	{
    691  1.17     rmind 		$$ = npfctl_parse_icmp($<num>0, $2,
    692  1.17     rmind 		    npfctl_icmpcode($<num>0, $2, $4));
    693   1.1     rmind 	}
    694   1.1     rmind 	| ICMPTYPE icmp_type CODE VAR_ID
    695   1.1     rmind 	{
    696   1.1     rmind 		char *s = npfvar_expand_string(npfvar_lookup($4));
    697  1.17     rmind 		$$ = npfctl_parse_icmp($<num>0, $2,
    698  1.17     rmind 		    npfctl_icmpcode($<num>0, $2, s));
    699   1.1     rmind 	}
    700  1.25     rmind 	|		{ $$ = NULL; }
    701   1.1     rmind 	;
    702   1.1     rmind 
    703   1.1     rmind tcp_flags_and_mask
    704   1.1     rmind 	: FLAGS tcp_flags SLASH tcp_flags
    705   1.1     rmind 	{
    706   1.1     rmind 		npfvar_add_elements($2, $4);
    707   1.1     rmind 		$$ = $2;
    708   1.1     rmind 	}
    709   1.1     rmind 	| FLAGS tcp_flags
    710   1.1     rmind 	{
    711   1.1     rmind 		char *s = npfvar_get_data($2, NPFVAR_TCPFLAG, 0);
    712   1.1     rmind 		npfvar_add_elements($2, npfctl_parse_tcpflag(s));
    713   1.1     rmind 		$$ = $2;
    714   1.1     rmind 	}
    715   1.1     rmind 	|		{ $$ = NULL; }
    716   1.1     rmind 	;
    717   1.1     rmind 
    718   1.1     rmind tcp_flags
    719   1.1     rmind 	: IDENTIFIER	{ $$ = npfctl_parse_tcpflag($1); }
    720   1.1     rmind 	;
    721   1.1     rmind 
    722   1.1     rmind icmp_type
    723  1.23  christos 	: number	{ $$ = $1; }
    724  1.11       spz 	| IDENTIFIER	{ $$ = npfctl_icmptype($<num>-1, $1); }
    725   1.1     rmind 	| VAR_ID
    726   1.1     rmind 	{
    727   1.1     rmind 		char *s = npfvar_expand_string(npfvar_lookup($1));
    728  1.11       spz 		$$ = npfctl_icmptype($<num>-1, s);
    729   1.1     rmind 	}
    730   1.1     rmind 	;
    731   1.1     rmind 
    732  1.29     rmind ifname
    733  1.29     rmind 	: some_name
    734  1.19  christos 	{
    735  1.29     rmind 		npfctl_note_interface($1);
    736  1.19  christos 		$$ = $1;
    737  1.19  christos 	}
    738  1.19  christos 	| VAR_ID
    739  1.19  christos 	{
    740  1.19  christos 		npfvar_t *vp = npfvar_lookup($1);
    741  1.19  christos 		const int type = npfvar_get_type(vp, 0);
    742  1.29     rmind 		ifnet_addr_t *ifna;
    743  1.19  christos 
    744  1.19  christos 		switch (type) {
    745  1.19  christos 		case NPFVAR_STRING:
    746  1.19  christos 		case NPFVAR_IDENTIFIER:
    747  1.19  christos 			$$ = npfvar_expand_string(vp);
    748  1.19  christos 			break;
    749  1.29     rmind 		case NPFVAR_INTERFACE:
    750  1.29     rmind 			ifna = npfvar_get_data(vp, type, 0);
    751  1.29     rmind 			$$ = ifna->ifna_name;
    752  1.29     rmind 			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.29     rmind 			yyerror("wrong variable '%s' type '%s' for interface",
    758  1.19  christos 			    $1, npfvar_type(type));
    759  1.19  christos 			break;
    760  1.19  christos 		}
    761  1.29     rmind 		npfctl_note_interface($$);
    762  1.19  christos 	}
    763  1.19  christos 	;
    764  1.19  christos 
    765  1.29     rmind ifaddrs
    766  1.29     rmind 	: afamily PAR_OPEN ifname PAR_CLOSE
    767  1.17     rmind 	{
    768  1.17     rmind 		$$ = npfctl_parse_ifnet($3, $1);
    769  1.17     rmind 	}
    770  1.19  christos 	;
    771  1.17     rmind 
    772  1.29     rmind ifref
    773  1.29     rmind 	: ifname
    774  1.29     rmind 	| ifaddrs
    775  1.17     rmind 	{
    776  1.17     rmind 		ifnet_addr_t *ifna = npfvar_get_data($1, NPFVAR_INTERFACE, 0);
    777  1.27     rmind 		npfctl_note_interface(ifna->ifna_name);
    778  1.27     rmind 		$$ = ifna->ifna_name;
    779  1.17     rmind 	}
    780   1.1     rmind 	;
    781   1.1     rmind 
    782  1.23  christos number
    783  1.23  christos 	: HEX		{ $$ = $1; }
    784  1.23  christos 	| NUM		{ $$ = $1; }
    785  1.23  christos 	;
    786  1.23  christos 
    787   1.9     rmind some_name
    788   1.1     rmind 	: IDENTIFIER	{ $$ = $1; }
    789   1.1     rmind 	| STRING	{ $$ = $1; }
    790   1.1     rmind 	;
    791   1.1     rmind 
    792   1.1     rmind %%
    793