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