Home | History | Annotate | Line # | Download | only in pfctl
parse.y revision 1.1.1.4
      1 /*	$NetBSD: parse.y,v 1.1.1.4 2009/12/01 07:03:06 martti Exp $	*/
      2 /*	$OpenBSD: parse.y,v 1.519 2007/06/21 19:30:03 henning Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
      6  * Copyright (c) 2001 Daniel Hartmeier.  All rights reserved.
      7  * Copyright (c) 2001 Theo de Raadt.  All rights reserved.
      8  * Copyright (c) 2002,2003 Henning Brauer. All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 %{
     31 #include <sys/types.h>
     32 #include <sys/socket.h>
     33 #include <net/if.h>
     34 #include <netinet/in.h>
     35 #include <netinet/in_systm.h>
     36 #include <netinet/ip.h>
     37 #include <netinet/ip_icmp.h>
     38 #include <netinet/icmp6.h>
     39 #include <net/pfvar.h>
     40 #include <arpa/inet.h>
     41 #include <altq/altq.h>
     42 #include <altq/altq_cbq.h>
     43 #include <altq/altq_priq.h>
     44 #include <altq/altq_hfsc.h>
     45 
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <netdb.h>
     49 #include <stdarg.h>
     50 #include <errno.h>
     51 #include <string.h>
     52 #include <ctype.h>
     53 #include <math.h>
     54 #include <err.h>
     55 #include <limits.h>
     56 #include <pwd.h>
     57 #include <grp.h>
     58 #include <md5.h>
     59 
     60 #include "pfctl_parser.h"
     61 #include "pfctl.h"
     62 
     63 static struct pfctl	*pf = NULL;
     64 static FILE		*fin = NULL;
     65 static int		 debug = 0;
     66 static int		 lineno = 1;
     67 static int		 errors = 0;
     68 static int		 rulestate = 0;
     69 static u_int16_t	 returnicmpdefault =
     70 			    (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
     71 static u_int16_t	 returnicmp6default =
     72 			    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
     73 static int		 blockpolicy = PFRULE_DROP;
     74 static int		 require_order = 1;
     75 static int		 default_statelock;
     76 
     77 enum {
     78 	PFCTL_STATE_NONE,
     79 	PFCTL_STATE_OPTION,
     80 	PFCTL_STATE_SCRUB,
     81 	PFCTL_STATE_QUEUE,
     82 	PFCTL_STATE_NAT,
     83 	PFCTL_STATE_FILTER
     84 };
     85 
     86 struct node_proto {
     87 	u_int8_t		 proto;
     88 	struct node_proto	*next;
     89 	struct node_proto	*tail;
     90 };
     91 
     92 struct node_port {
     93 	u_int16_t		 port[2];
     94 	u_int8_t		 op;
     95 	struct node_port	*next;
     96 	struct node_port	*tail;
     97 };
     98 
     99 struct node_uid {
    100 	uid_t			 uid[2];
    101 	u_int8_t		 op;
    102 	struct node_uid		*next;
    103 	struct node_uid		*tail;
    104 };
    105 
    106 struct node_gid {
    107 	gid_t			 gid[2];
    108 	u_int8_t		 op;
    109 	struct node_gid		*next;
    110 	struct node_gid		*tail;
    111 };
    112 
    113 struct node_icmp {
    114 	u_int8_t		 code;
    115 	u_int8_t		 type;
    116 	u_int8_t		 proto;
    117 	struct node_icmp	*next;
    118 	struct node_icmp	*tail;
    119 };
    120 
    121 enum	{ PF_STATE_OPT_MAX, PF_STATE_OPT_NOSYNC, PF_STATE_OPT_SRCTRACK,
    122 	    PF_STATE_OPT_MAX_SRC_STATES, PF_STATE_OPT_MAX_SRC_CONN,
    123 	    PF_STATE_OPT_MAX_SRC_CONN_RATE, PF_STATE_OPT_MAX_SRC_NODES,
    124 	    PF_STATE_OPT_OVERLOAD, PF_STATE_OPT_STATELOCK,
    125 	    PF_STATE_OPT_TIMEOUT };
    126 
    127 enum	{ PF_SRCTRACK_NONE, PF_SRCTRACK, PF_SRCTRACK_GLOBAL, PF_SRCTRACK_RULE };
    128 
    129 struct node_state_opt {
    130 	int			 type;
    131 	union {
    132 		u_int32_t	 max_states;
    133 		u_int32_t	 max_src_states;
    134 		u_int32_t	 max_src_conn;
    135 		struct {
    136 			u_int32_t	limit;
    137 			u_int32_t	seconds;
    138 		}		 max_src_conn_rate;
    139 		struct {
    140 			u_int8_t	flush;
    141 			char		tblname[PF_TABLE_NAME_SIZE];
    142 		}		 overload;
    143 		u_int32_t	 max_src_nodes;
    144 		u_int8_t	 src_track;
    145 		u_int32_t	 statelock;
    146 		struct {
    147 			int		number;
    148 			u_int32_t	seconds;
    149 		}		 timeout;
    150 	}			 data;
    151 	struct node_state_opt	*next;
    152 	struct node_state_opt	*tail;
    153 };
    154 
    155 struct peer {
    156 	struct node_host	*host;
    157 	struct node_port	*port;
    158 };
    159 
    160 struct node_queue {
    161 	char			 queue[PF_QNAME_SIZE];
    162 	char			 parent[PF_QNAME_SIZE];
    163 	char			 ifname[IFNAMSIZ];
    164 	int			 scheduler;
    165 	struct node_queue	*next;
    166 	struct node_queue	*tail;
    167 }	*queues = NULL;
    168 
    169 struct node_qassign {
    170 	char		*qname;
    171 	char		*pqname;
    172 };
    173 
    174 struct filter_opts {
    175 	int			 marker;
    176 #define FOM_FLAGS	0x01
    177 #define FOM_ICMP	0x02
    178 #define FOM_TOS		0x04
    179 #define FOM_KEEP	0x08
    180 #define FOM_SRCTRACK	0x10
    181 	struct node_uid		*uid;
    182 	struct node_gid		*gid;
    183 	struct {
    184 		u_int8_t	 b1;
    185 		u_int8_t	 b2;
    186 		u_int16_t	 w;
    187 		u_int16_t	 w2;
    188 	} flags;
    189 	struct node_icmp	*icmpspec;
    190 	u_int32_t		 tos;
    191 	u_int32_t		 prob;
    192 	struct {
    193 		int			 action;
    194 		struct node_state_opt	*options;
    195 	} keep;
    196 	int			 fragment;
    197 	int			 allowopts;
    198 	char			*label;
    199 	struct node_qassign	 queues;
    200 	char			*tag;
    201 	char			*match_tag;
    202 	u_int8_t		 match_tag_not;
    203 	int			 rtableid;
    204 } filter_opts;
    205 
    206 struct antispoof_opts {
    207 	char			*label;
    208 	int			 rtableid;
    209 } antispoof_opts;
    210 
    211 struct scrub_opts {
    212 	int			marker;
    213 #define SOM_MINTTL	0x01
    214 #define SOM_MAXMSS	0x02
    215 #define SOM_FRAGCACHE	0x04
    216 	int			nodf;
    217 	int			minttl;
    218 	int			maxmss;
    219 	int			fragcache;
    220 	int			randomid;
    221 	int			reassemble_tcp;
    222 	int			rtableid;
    223 } scrub_opts;
    224 
    225 struct queue_opts {
    226 	int			marker;
    227 #define QOM_BWSPEC	0x01
    228 #define QOM_SCHEDULER	0x02
    229 #define QOM_PRIORITY	0x04
    230 #define QOM_TBRSIZE	0x08
    231 #define QOM_QLIMIT	0x10
    232 	struct node_queue_bw	queue_bwspec;
    233 	struct node_queue_opt	scheduler;
    234 	int			priority;
    235 	int			tbrsize;
    236 	int			qlimit;
    237 } queue_opts;
    238 
    239 struct table_opts {
    240 	int			flags;
    241 	int			init_addr;
    242 	struct node_tinithead	init_nodes;
    243 } table_opts;
    244 
    245 struct pool_opts {
    246 	int			 marker;
    247 #define POM_TYPE		0x01
    248 #define POM_STICKYADDRESS	0x02
    249 	u_int8_t		 opts;
    250 	int			 type;
    251 	int			 staticport;
    252 	struct pf_poolhashkey	*key;
    253 
    254 } pool_opts;
    255 
    256 
    257 struct node_hfsc_opts	hfsc_opts;
    258 
    259 int	yyerror(const char *, ...);
    260 int	disallow_table(struct node_host *, const char *);
    261 int	disallow_urpf_failed(struct node_host *, const char *);
    262 int	disallow_alias(struct node_host *, const char *);
    263 int	rule_consistent(struct pf_rule *, int);
    264 int	filter_consistent(struct pf_rule *, int);
    265 int	nat_consistent(struct pf_rule *);
    266 int	rdr_consistent(struct pf_rule *);
    267 int	process_tabledef(char *, struct table_opts *);
    268 int	yyparse(void);
    269 void	expand_label_str(char *, size_t, const char *, const char *);
    270 void	expand_label_if(const char *, char *, size_t, const char *);
    271 void	expand_label_addr(const char *, char *, size_t, u_int8_t,
    272 	    struct node_host *);
    273 void	expand_label_port(const char *, char *, size_t, struct node_port *);
    274 void	expand_label_proto(const char *, char *, size_t, u_int8_t);
    275 void	expand_label_nr(const char *, char *, size_t);
    276 void	expand_label(char *, size_t, const char *, u_int8_t, struct node_host *,
    277 	    struct node_port *, struct node_host *, struct node_port *,
    278 	    u_int8_t);
    279 void	expand_rule(struct pf_rule *, struct node_if *, struct node_host *,
    280 	    struct node_proto *, struct node_os*, struct node_host *,
    281 	    struct node_port *, struct node_host *, struct node_port *,
    282 	    struct node_uid *, struct node_gid *, struct node_icmp *,
    283 	    const char *);
    284 int	expand_altq(struct pf_altq *, struct node_if *, struct node_queue *,
    285 	    struct node_queue_bw bwspec, struct node_queue_opt *);
    286 int	expand_queue(struct pf_altq *, struct node_if *, struct node_queue *,
    287 	    struct node_queue_bw, struct node_queue_opt *);
    288 int	expand_skip_interface(struct node_if *);
    289 
    290 int	 check_rulestate(int);
    291 int	 kw_cmp(const void *, const void *);
    292 int	 lookup(char *);
    293 int	 lgetc(FILE *);
    294 int	 lungetc(int);
    295 int	 findeol(void);
    296 int	 yylex(void);
    297 int	 atoul(char *, u_long *);
    298 int	 getservice(char *);
    299 int	 rule_label(struct pf_rule *, char *);
    300 
    301 TAILQ_HEAD(symhead, sym)	 symhead = TAILQ_HEAD_INITIALIZER(symhead);
    302 struct sym {
    303 	TAILQ_ENTRY(sym)	 entries;
    304 	int			 used;
    305 	int			 persist;
    306 	char			*nam;
    307 	char			*val;
    308 };
    309 
    310 
    311 int	 symset(const char *, const char *, int);
    312 char	*symget(const char *);
    313 
    314 void	 mv_rules(struct pf_ruleset *, struct pf_ruleset *);
    315 void	 decide_address_family(struct node_host *, sa_family_t *);
    316 void	 remove_invalid_hosts(struct node_host **, sa_family_t *);
    317 int	 invalid_redirect(struct node_host *, sa_family_t);
    318 u_int16_t parseicmpspec(char *, sa_family_t);
    319 
    320 TAILQ_HEAD(loadanchorshead, loadanchors)
    321     loadanchorshead = TAILQ_HEAD_INITIALIZER(loadanchorshead);
    322 
    323 struct loadanchors {
    324 	TAILQ_ENTRY(loadanchors)	 entries;
    325 	char				*anchorname;
    326 	char				*filename;
    327 };
    328 
    329 typedef struct {
    330 	union {
    331 		u_int32_t		 number;
    332 		int			 i;
    333 		char			*string;
    334 		int			 rtableid;
    335 		struct {
    336 			u_int8_t	 b1;
    337 			u_int8_t	 b2;
    338 			u_int16_t	 w;
    339 			u_int16_t	 w2;
    340 		}			 b;
    341 		struct range {
    342 			int		 a;
    343 			int		 b;
    344 			int		 t;
    345 		}			 range;
    346 		struct node_if		*interface;
    347 		struct node_proto	*proto;
    348 		struct node_icmp	*icmp;
    349 		struct node_host	*host;
    350 		struct node_os		*os;
    351 		struct node_port	*port;
    352 		struct node_uid		*uid;
    353 		struct node_gid		*gid;
    354 		struct node_state_opt	*state_opt;
    355 		struct peer		 peer;
    356 		struct {
    357 			struct peer	 src, dst;
    358 			struct node_os	*src_os;
    359 		}			 fromto;
    360 		struct {
    361 			struct node_host	*host;
    362 			u_int8_t		 rt;
    363 			u_int8_t		 pool_opts;
    364 			sa_family_t		 af;
    365 			struct pf_poolhashkey	*key;
    366 		}			 route;
    367 		struct redirection {
    368 			struct node_host	*host;
    369 			struct range		 rport;
    370 		}			*redirection;
    371 		struct {
    372 			int			 action;
    373 			struct node_state_opt	*options;
    374 		}			 keep_state;
    375 		struct {
    376 			u_int8_t	 log;
    377 			u_int8_t	 logif;
    378 			u_int8_t	 quick;
    379 		}			 logquick;
    380 		struct {
    381 			int		 neg;
    382 			char		*name;
    383 		}			 tagged;
    384 		struct pf_poolhashkey	*hashkey;
    385 		struct node_queue	*queue;
    386 		struct node_queue_opt	 queue_options;
    387 		struct node_queue_bw	 queue_bwspec;
    388 		struct node_qassign	 qassign;
    389 		struct filter_opts	 filter_opts;
    390 		struct antispoof_opts	 antispoof_opts;
    391 		struct queue_opts	 queue_opts;
    392 		struct scrub_opts	 scrub_opts;
    393 		struct table_opts	 table_opts;
    394 		struct pool_opts	 pool_opts;
    395 		struct node_hfsc_opts	 hfsc_opts;
    396 	} v;
    397 	int lineno;
    398 } YYSTYPE;
    399 
    400 #define DYNIF_MULTIADDR(addr) ((addr).type == PF_ADDR_DYNIFTL && \
    401 	(!((addr).iflags & PFI_AFLAG_NOALIAS) ||		 \
    402 	!isdigit((addr).v.ifname[strlen((addr).v.ifname)-1])))
    403 
    404 %}
    405 
    406 %token	PASS BLOCK SCRUB RETURN IN OS OUT LOG QUICK ON FROM TO FLAGS
    407 %token	RETURNRST RETURNICMP RETURNICMP6 PROTO INET INET6 ALL ANY ICMPTYPE
    408 %token	ICMP6TYPE CODE KEEP MODULATE STATE PORT RDR NAT BINAT ARROW NODF
    409 %token	MINTTL ERROR ALLOWOPTS FASTROUTE FILENAME ROUTETO DUPTO REPLYTO NO LABEL
    410 %token	NOROUTE URPFFAILED FRAGMENT USER GROUP MAXMSS MAXIMUM TTL TOS DROP TABLE
    411 %token	REASSEMBLE FRAGDROP FRAGCROP ANCHOR NATANCHOR RDRANCHOR BINATANCHOR
    412 %token	SET OPTIMIZATION TIMEOUT LIMIT LOGINTERFACE BLOCKPOLICY RANDOMID
    413 %token	REQUIREORDER SYNPROXY FINGERPRINTS NOSYNC DEBUG SKIP HOSTID
    414 %token	ANTISPOOF FOR
    415 %token	BITMASK RANDOM SOURCEHASH ROUNDROBIN STATICPORT PROBABILITY
    416 %token	ALTQ CBQ PRIQ HFSC BANDWIDTH TBRSIZE LINKSHARE REALTIME UPPERLIMIT
    417 %token	QUEUE PRIORITY QLIMIT RTABLE
    418 %token	LOAD RULESET_OPTIMIZATION
    419 %token	STICKYADDRESS MAXSRCSTATES MAXSRCNODES SOURCETRACK GLOBAL RULE
    420 %token	MAXSRCCONN MAXSRCCONNRATE OVERLOAD FLUSH
    421 %token	TAGGED TAG IFBOUND FLOATING STATEPOLICY ROUTE
    422 %token	<v.string>		STRING
    423 %token	<v.i>			PORTBINARY
    424 %type	<v.interface>		interface if_list if_item_not if_item
    425 %type	<v.number>		number icmptype icmp6type uid gid
    426 %type	<v.number>		tos not yesno
    427 %type	<v.i>			no dir af fragcache optimizer
    428 %type	<v.i>			sourcetrack flush unaryop statelock
    429 %type	<v.b>			action nataction natpasslog scrubaction
    430 %type	<v.b>			flags flag blockspec
    431 %type	<v.range>		port rport
    432 %type	<v.hashkey>		hashkey
    433 %type	<v.proto>		proto proto_list proto_item
    434 %type	<v.icmp>		icmpspec
    435 %type	<v.icmp>		icmp_list icmp_item
    436 %type	<v.icmp>		icmp6_list icmp6_item
    437 %type	<v.fromto>		fromto
    438 %type	<v.peer>		ipportspec from to
    439 %type	<v.host>		ipspec xhost host dynaddr host_list
    440 %type	<v.host>		redir_host_list redirspec
    441 %type	<v.host>		route_host route_host_list routespec
    442 %type	<v.os>			os xos os_list
    443 %type	<v.port>		portspec port_list port_item
    444 %type	<v.uid>			uids uid_list uid_item
    445 %type	<v.gid>			gids gid_list gid_item
    446 %type	<v.route>		route
    447 %type	<v.redirection>		redirection redirpool
    448 %type	<v.string>		label string tag anchorname
    449 %type	<v.keep_state>		keep
    450 %type	<v.state_opt>		state_opt_spec state_opt_list state_opt_item
    451 %type	<v.logquick>		logquick quick log logopts logopt
    452 %type	<v.interface>		antispoof_ifspc antispoof_iflst antispoof_if
    453 %type	<v.qassign>		qname
    454 %type	<v.queue>		qassign qassign_list qassign_item
    455 %type	<v.queue_options>	scheduler
    456 %type	<v.number>		cbqflags_list cbqflags_item
    457 %type	<v.number>		priqflags_list priqflags_item
    458 %type	<v.hfsc_opts>		hfscopts_list hfscopts_item hfsc_opts
    459 %type	<v.queue_bwspec>	bandwidth
    460 %type	<v.filter_opts>		filter_opts filter_opt filter_opts_l
    461 %type	<v.antispoof_opts>	antispoof_opts antispoof_opt antispoof_opts_l
    462 %type	<v.queue_opts>		queue_opts queue_opt queue_opts_l
    463 %type	<v.scrub_opts>		scrub_opts scrub_opt scrub_opts_l
    464 %type	<v.table_opts>		table_opts table_opt table_opts_l
    465 %type	<v.pool_opts>		pool_opts pool_opt pool_opts_l
    466 %type	<v.tagged>		tagged
    467 %type	<v.rtableid>		rtable
    468 %%
    469 
    470 ruleset		: /* empty */
    471 		| ruleset '\n'
    472 		| ruleset option '\n'
    473 		| ruleset scrubrule '\n'
    474 		| ruleset natrule '\n'
    475 		| ruleset binatrule '\n'
    476 		| ruleset pfrule '\n'
    477 		| ruleset anchorrule '\n'
    478 		| ruleset loadrule '\n'
    479 		| ruleset altqif '\n'
    480 		| ruleset queuespec '\n'
    481 		| ruleset varset '\n'
    482 		| ruleset antispoof '\n'
    483 		| ruleset tabledef '\n'
    484 		| '{' fakeanchor '}' '\n';
    485 		| ruleset error '\n'		{ errors++; }
    486 		;
    487 
    488 /*
    489  * apply to previouslys specified rule: must be careful to note
    490  * what that is: pf or nat or binat or rdr
    491  */
    492 fakeanchor	: fakeanchor '\n'
    493 		| fakeanchor anchorrule '\n'
    494 		| fakeanchor binatrule '\n'
    495 		| fakeanchor natrule '\n'
    496 		| fakeanchor pfrule '\n'
    497 		| fakeanchor error '\n'
    498 		;
    499 
    500 optimizer	: string	{
    501 			if (!strcmp($1, "none"))
    502 				$$ = 0;
    503 			else if (!strcmp($1, "basic"))
    504 				$$ = PF_OPTIMIZE_BASIC;
    505 			else if (!strcmp($1, "profile"))
    506 				$$ = PF_OPTIMIZE_BASIC | PF_OPTIMIZE_PROFILE;
    507 			else {
    508 				yyerror("unknown ruleset-optimization %s", $$);
    509 				YYERROR;
    510 			}
    511 		}
    512 		;
    513 
    514 option		: SET OPTIMIZATION STRING		{
    515 			if (check_rulestate(PFCTL_STATE_OPTION)) {
    516 				free($3);
    517 				YYERROR;
    518 			}
    519 			if (pfctl_set_optimization(pf, $3) != 0) {
    520 				yyerror("unknown optimization %s", $3);
    521 				free($3);
    522 				YYERROR;
    523 			}
    524 			free($3);
    525 		}
    526 		| SET RULESET_OPTIMIZATION optimizer {
    527 			if (!(pf->opts & PF_OPT_OPTIMIZE)) {
    528 				pf->opts |= PF_OPT_OPTIMIZE;
    529 				pf->optimize = $3;
    530 			}
    531 		}
    532 		| SET TIMEOUT timeout_spec
    533 		| SET TIMEOUT '{' timeout_list '}'
    534 		| SET LIMIT limit_spec
    535 		| SET LIMIT '{' limit_list '}'
    536 		| SET LOGINTERFACE STRING		{
    537 			if (check_rulestate(PFCTL_STATE_OPTION)) {
    538 				free($3);
    539 				YYERROR;
    540 			}
    541 			if (pfctl_set_logif(pf, $3) != 0) {
    542 				yyerror("error setting loginterface %s", $3);
    543 				free($3);
    544 				YYERROR;
    545 			}
    546 			free($3);
    547 		}
    548 		| SET HOSTID number {
    549 			if ($3 == 0) {
    550 				yyerror("hostid must be non-zero");
    551 				YYERROR;
    552 			}
    553 			if (pfctl_set_hostid(pf, $3) != 0) {
    554 				yyerror("error setting hostid %08x", $3);
    555 				YYERROR;
    556 			}
    557 		}
    558 		| SET BLOCKPOLICY DROP	{
    559 			if (pf->opts & PF_OPT_VERBOSE)
    560 				printf("set block-policy drop\n");
    561 			if (check_rulestate(PFCTL_STATE_OPTION))
    562 				YYERROR;
    563 			blockpolicy = PFRULE_DROP;
    564 		}
    565 		| SET BLOCKPOLICY RETURN {
    566 			if (pf->opts & PF_OPT_VERBOSE)
    567 				printf("set block-policy return\n");
    568 			if (check_rulestate(PFCTL_STATE_OPTION))
    569 				YYERROR;
    570 			blockpolicy = PFRULE_RETURN;
    571 		}
    572 		| SET REQUIREORDER yesno {
    573 			if (pf->opts & PF_OPT_VERBOSE)
    574 				printf("set require-order %s\n",
    575 				    $3 == 1 ? "yes" : "no");
    576 			require_order = $3;
    577 		}
    578 		| SET FINGERPRINTS STRING {
    579 			if (pf->opts & PF_OPT_VERBOSE)
    580 				printf("set fingerprints \"%s\"\n", $3);
    581 			if (check_rulestate(PFCTL_STATE_OPTION)) {
    582 				free($3);
    583 				YYERROR;
    584 			}
    585 			if (!pf->anchor->name[0]) {
    586 				if (pfctl_file_fingerprints(pf->dev,
    587 				    pf->opts, $3)) {
    588 					yyerror("error loading "
    589 					    "fingerprints %s", $3);
    590 					free($3);
    591 					YYERROR;
    592 				}
    593 			}
    594 			free($3);
    595 		}
    596 		| SET STATEPOLICY statelock {
    597 			if (pf->opts & PF_OPT_VERBOSE)
    598 				switch ($3) {
    599 				case 0:
    600 					printf("set state-policy floating\n");
    601 					break;
    602 				case PFRULE_IFBOUND:
    603 					printf("set state-policy if-bound\n");
    604 					break;
    605 				}
    606 			default_statelock = $3;
    607 		}
    608 		| SET DEBUG STRING {
    609 			if (check_rulestate(PFCTL_STATE_OPTION)) {
    610 				free($3);
    611 				YYERROR;
    612 			}
    613 			if (pfctl_set_debug(pf, $3) != 0) {
    614 				yyerror("error setting debuglevel %s", $3);
    615 				free($3);
    616 				YYERROR;
    617 			}
    618 			free($3);
    619 		}
    620 		| SET SKIP interface {
    621 			if (expand_skip_interface($3) != 0) {
    622 				yyerror("error setting skip interface(s)");
    623 				YYERROR;
    624 			}
    625 		}
    626 		;
    627 
    628 string		: string STRING				{
    629 			if (asprintf(&$$, "%s %s", $1, $2) == -1)
    630 				err(1, "string: asprintf");
    631 			free($1);
    632 			free($2);
    633 		}
    634 		| STRING
    635 		;
    636 
    637 varset		: STRING '=' string		{
    638 			if (pf->opts & PF_OPT_VERBOSE)
    639 				printf("%s = \"%s\"\n", $1, $3);
    640 			if (symset($1, $3, 0) == -1)
    641 				err(1, "cannot store variable %s", $1);
    642 			free($1);
    643 			free($3);
    644 		}
    645 		;
    646 
    647 anchorname	: STRING			{ $$ = $1; }
    648 		| /* empty */			{ $$ = NULL; }
    649 		;
    650 
    651 optnl		: optnl '\n'
    652 		|
    653 		;
    654 
    655 pfa_anchorlist	: pfrule optnl
    656 		| anchorrule optnl
    657 		| pfa_anchorlist pfrule optnl
    658 		| pfa_anchorlist anchorrule optnl
    659 		;
    660 
    661 pfa_anchor	: '{'
    662 		{
    663 			char ta[PF_ANCHOR_NAME_SIZE];
    664 			struct pf_ruleset *rs;
    665 
    666 			/* steping into a brace anchor */
    667 			pf->asd++;
    668 			pf->bn++;
    669 			pf->brace = 1;
    670 
    671 			/* create a holding ruleset in the root */
    672 			snprintf(ta, PF_ANCHOR_NAME_SIZE, "_%d", pf->bn);
    673 			rs = pf_find_or_create_ruleset(ta);
    674 			if (rs == NULL)
    675 				err(1, "pfa_anchor: pf_find_or_create_ruleset");
    676 			pf->astack[pf->asd] = rs->anchor;
    677 			pf->anchor = rs->anchor;
    678 		} '\n' pfa_anchorlist '}'
    679 		{
    680 			pf->alast = pf->anchor;
    681 			pf->asd--;
    682 			pf->anchor = pf->astack[pf->asd];
    683 		}
    684 		| /* empty */
    685 		;
    686 
    687 anchorrule	: ANCHOR anchorname dir quick interface af proto fromto
    688 		    filter_opts pfa_anchor
    689 		{
    690 			struct pf_rule	r;
    691 
    692 			if (check_rulestate(PFCTL_STATE_FILTER)) {
    693 				if ($2)
    694 					free($2);
    695 				YYERROR;
    696 			}
    697 
    698 			if ($2 && ($2[0] == '_' || strstr($2, "/_") != NULL)) {
    699 				free($2);
    700 				yyerror("anchor names beginning with '_' "
    701 				    "are reserved for internal use");
    702 				YYERROR;
    703 			}
    704 
    705 			memset(&r, 0, sizeof(r));
    706 			if (pf->astack[pf->asd + 1]) {
    707 				/* move inline rules into relative location */
    708 				pf_anchor_setup(&r,
    709 				    &pf->astack[pf->asd]->ruleset,
    710 				    $2 ? $2 : pf->alast->name);
    711 
    712 				if (r.anchor == NULL)
    713 					err(1, "anchorrule: unable to "
    714 					    "create ruleset");
    715 
    716 				if (pf->alast != r.anchor) {
    717 					if (r.anchor->match) {
    718 						yyerror("inline anchor '%s' "
    719 						    "already exists",
    720 						    r.anchor->name);
    721 						YYERROR;
    722 					}
    723 					mv_rules(&pf->alast->ruleset,
    724 					    &r.anchor->ruleset);
    725 				}
    726 				pf_remove_if_empty_ruleset(&pf->alast->ruleset);
    727 				pf->alast = r.anchor;
    728 			} else {
    729 				if (!$2) {
    730 					yyerror("anchors without explicit "
    731 					    "rules must specify a name");
    732 					YYERROR;
    733 				}
    734 			}
    735 			r.direction = $3;
    736 			r.quick = $4.quick;
    737 			r.af = $6;
    738 			r.prob = $9.prob;
    739 			r.rtableid = $9.rtableid;
    740 
    741 			if ($9.match_tag)
    742 				if (strlcpy(r.match_tagname, $9.match_tag,
    743 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
    744 					yyerror("tag too long, max %u chars",
    745 					    PF_TAG_NAME_SIZE - 1);
    746 					YYERROR;
    747 				}
    748 			r.match_tag_not = $9.match_tag_not;
    749 
    750 			decide_address_family($8.src.host, &r.af);
    751 			decide_address_family($8.dst.host, &r.af);
    752 
    753 			expand_rule(&r, $5, NULL, $7, $8.src_os,
    754 			    $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
    755 			    0, 0, 0, pf->astack[pf->asd + 1] ?
    756 			    pf->alast->name : $2);
    757 			free($2);
    758 			pf->astack[pf->asd + 1] = NULL;
    759 		}
    760 		| NATANCHOR string interface af proto fromto rtable {
    761 			struct pf_rule	r;
    762 
    763 			if (check_rulestate(PFCTL_STATE_NAT)) {
    764 				free($2);
    765 				YYERROR;
    766 			}
    767 
    768 			memset(&r, 0, sizeof(r));
    769 			r.action = PF_NAT;
    770 			r.af = $4;
    771 			r.rtableid = $7;
    772 
    773 			decide_address_family($6.src.host, &r.af);
    774 			decide_address_family($6.dst.host, &r.af);
    775 
    776 			expand_rule(&r, $3, NULL, $5, $6.src_os,
    777 			    $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
    778 			    0, 0, 0, $2);
    779 			free($2);
    780 		}
    781 		| RDRANCHOR string interface af proto fromto rtable {
    782 			struct pf_rule	r;
    783 
    784 			if (check_rulestate(PFCTL_STATE_NAT)) {
    785 				free($2);
    786 				YYERROR;
    787 			}
    788 
    789 			memset(&r, 0, sizeof(r));
    790 			r.action = PF_RDR;
    791 			r.af = $4;
    792 			r.rtableid = $7;
    793 
    794 			decide_address_family($6.src.host, &r.af);
    795 			decide_address_family($6.dst.host, &r.af);
    796 
    797 			if ($6.src.port != NULL) {
    798 				yyerror("source port parameter not supported"
    799 				    " in rdr-anchor");
    800 				YYERROR;
    801 			}
    802 			if ($6.dst.port != NULL) {
    803 				if ($6.dst.port->next != NULL) {
    804 					yyerror("destination port list "
    805 					    "expansion not supported in "
    806 					    "rdr-anchor");
    807 					YYERROR;
    808 				} else if ($6.dst.port->op != PF_OP_EQ) {
    809 					yyerror("destination port operators"
    810 					    " not supported in rdr-anchor");
    811 					YYERROR;
    812 				}
    813 				r.dst.port[0] = $6.dst.port->port[0];
    814 				r.dst.port[1] = $6.dst.port->port[1];
    815 				r.dst.port_op = $6.dst.port->op;
    816 			}
    817 
    818 			expand_rule(&r, $3, NULL, $5, $6.src_os,
    819 			    $6.src.host, $6.src.port, $6.dst.host, $6.dst.port,
    820 			    0, 0, 0, $2);
    821 			free($2);
    822 		}
    823 		| BINATANCHOR string interface af proto fromto rtable {
    824 			struct pf_rule	r;
    825 
    826 			if (check_rulestate(PFCTL_STATE_NAT)) {
    827 				free($2);
    828 				YYERROR;
    829 			}
    830 
    831 			memset(&r, 0, sizeof(r));
    832 			r.action = PF_BINAT;
    833 			r.af = $4;
    834 			r.rtableid = $7;
    835 			if ($5 != NULL) {
    836 				if ($5->next != NULL) {
    837 					yyerror("proto list expansion"
    838 					    " not supported in binat-anchor");
    839 					YYERROR;
    840 				}
    841 				r.proto = $5->proto;
    842 				free($5);
    843 			}
    844 
    845 			if ($6.src.host != NULL || $6.src.port != NULL ||
    846 			    $6.dst.host != NULL || $6.dst.port != NULL) {
    847 				yyerror("fromto parameter not supported"
    848 				    " in binat-anchor");
    849 				YYERROR;
    850 			}
    851 
    852 			decide_address_family($6.src.host, &r.af);
    853 			decide_address_family($6.dst.host, &r.af);
    854 
    855 			pfctl_add_rule(pf, &r, $2);
    856 			free($2);
    857 		}
    858 		;
    859 
    860 loadrule	: LOAD ANCHOR string FROM string	{
    861 			struct loadanchors	*loadanchor;
    862 
    863 			if (strlen(pf->anchor->name) + 1 +
    864 			    strlen($3) >= MAXPATHLEN) {
    865 				yyerror("anchorname %s too long, max %u\n",
    866 				    $3, MAXPATHLEN - 1);
    867 				free($3);
    868 				YYERROR;
    869 			}
    870 			loadanchor = calloc(1, sizeof(struct loadanchors));
    871 			if (loadanchor == NULL)
    872 				err(1, "loadrule: calloc");
    873 			if ((loadanchor->anchorname = malloc(MAXPATHLEN)) ==
    874 			    NULL)
    875 				err(1, "loadrule: malloc");
    876 			if (pf->anchor->name[0])
    877 				snprintf(loadanchor->anchorname, MAXPATHLEN,
    878 				    "%s/%s", pf->anchor->name, $3);
    879 			else
    880 				strlcpy(loadanchor->anchorname, $3, MAXPATHLEN);
    881 			if ((loadanchor->filename = strdup($5)) == NULL)
    882 				err(1, "loadrule: strdup");
    883 
    884 			TAILQ_INSERT_TAIL(&loadanchorshead, loadanchor,
    885 			    entries);
    886 
    887 			free($3);
    888 			free($5);
    889 		};
    890 
    891 scrubaction	: no SCRUB {
    892 			$$.b2 = $$.w = 0;
    893 			if ($1)
    894 				$$.b1 = PF_NOSCRUB;
    895 			else
    896 				$$.b1 = PF_SCRUB;
    897 		}
    898 		;
    899 
    900 scrubrule	: scrubaction dir logquick interface af proto fromto scrub_opts
    901 		{
    902 			struct pf_rule	r;
    903 
    904 			if (check_rulestate(PFCTL_STATE_SCRUB))
    905 				YYERROR;
    906 
    907 			memset(&r, 0, sizeof(r));
    908 
    909 			r.action = $1.b1;
    910 			r.direction = $2;
    911 
    912 			r.log = $3.log;
    913 			r.logif = $3.logif;
    914 			if ($3.quick) {
    915 				yyerror("scrub rules do not support 'quick'");
    916 				YYERROR;
    917 			}
    918 
    919 			r.af = $5;
    920 			if ($8.nodf)
    921 				r.rule_flag |= PFRULE_NODF;
    922 			if ($8.randomid)
    923 				r.rule_flag |= PFRULE_RANDOMID;
    924 			if ($8.reassemble_tcp) {
    925 				if (r.direction != PF_INOUT) {
    926 					yyerror("reassemble tcp rules can not "
    927 					    "specify direction");
    928 					YYERROR;
    929 				}
    930 				r.rule_flag |= PFRULE_REASSEMBLE_TCP;
    931 			}
    932 			if ($8.minttl)
    933 				r.min_ttl = $8.minttl;
    934 			if ($8.maxmss)
    935 				r.max_mss = $8.maxmss;
    936 			if ($8.fragcache)
    937 				r.rule_flag |= $8.fragcache;
    938 			r.rtableid = $8.rtableid;
    939 
    940 			expand_rule(&r, $4, NULL, $6, $7.src_os,
    941 			    $7.src.host, $7.src.port, $7.dst.host, $7.dst.port,
    942 			    NULL, NULL, NULL, "");
    943 		}
    944 		;
    945 
    946 scrub_opts	:	{
    947 				bzero(&scrub_opts, sizeof scrub_opts);
    948 				scrub_opts.rtableid = -1;
    949 			}
    950 		    scrub_opts_l
    951 			{ $$ = scrub_opts; }
    952 		| /* empty */ {
    953 			bzero(&scrub_opts, sizeof scrub_opts);
    954 			scrub_opts.rtableid = -1;
    955 			$$ = scrub_opts;
    956 		}
    957 		;
    958 
    959 scrub_opts_l	: scrub_opts_l scrub_opt
    960 		| scrub_opt
    961 		;
    962 
    963 scrub_opt	: NODF	{
    964 			if (scrub_opts.nodf) {
    965 				yyerror("no-df cannot be respecified");
    966 				YYERROR;
    967 			}
    968 			scrub_opts.nodf = 1;
    969 		}
    970 		| MINTTL number {
    971 			if (scrub_opts.marker & SOM_MINTTL) {
    972 				yyerror("min-ttl cannot be respecified");
    973 				YYERROR;
    974 			}
    975 			if ($2 > 255) {
    976 				yyerror("illegal min-ttl value %d", $2);
    977 				YYERROR;
    978 			}
    979 			scrub_opts.marker |= SOM_MINTTL;
    980 			scrub_opts.minttl = $2;
    981 		}
    982 		| MAXMSS number {
    983 			if (scrub_opts.marker & SOM_MAXMSS) {
    984 				yyerror("max-mss cannot be respecified");
    985 				YYERROR;
    986 			}
    987 			if ($2 > 65535) {
    988 				yyerror("illegal max-mss value %d", $2);
    989 				YYERROR;
    990 			}
    991 			scrub_opts.marker |= SOM_MAXMSS;
    992 			scrub_opts.maxmss = $2;
    993 		}
    994 		| fragcache {
    995 			if (scrub_opts.marker & SOM_FRAGCACHE) {
    996 				yyerror("fragcache cannot be respecified");
    997 				YYERROR;
    998 			}
    999 			scrub_opts.marker |= SOM_FRAGCACHE;
   1000 			scrub_opts.fragcache = $1;
   1001 		}
   1002 		| REASSEMBLE STRING {
   1003 			if (strcasecmp($2, "tcp") != 0) {
   1004 				yyerror("scrub reassemble supports only tcp, "
   1005 				    "not '%s'", $2);
   1006 				free($2);
   1007 				YYERROR;
   1008 			}
   1009 			free($2);
   1010 			if (scrub_opts.reassemble_tcp) {
   1011 				yyerror("reassemble tcp cannot be respecified");
   1012 				YYERROR;
   1013 			}
   1014 			scrub_opts.reassemble_tcp = 1;
   1015 		}
   1016 		| RANDOMID {
   1017 			if (scrub_opts.randomid) {
   1018 				yyerror("random-id cannot be respecified");
   1019 				YYERROR;
   1020 			}
   1021 			scrub_opts.randomid = 1;
   1022 		}
   1023 		| RTABLE number				{
   1024 			if ($2 > RT_TABLEID_MAX || $2 < 0) {
   1025 				yyerror("invalid rtable id");
   1026 				YYERROR;
   1027 			}
   1028 			scrub_opts.rtableid = $2;
   1029 		}
   1030 		;
   1031 
   1032 fragcache	: FRAGMENT REASSEMBLE	{ $$ = 0; /* default */ }
   1033 		| FRAGMENT FRAGCROP	{ $$ = PFRULE_FRAGCROP; }
   1034 		| FRAGMENT FRAGDROP	{ $$ = PFRULE_FRAGDROP; }
   1035 		;
   1036 
   1037 antispoof	: ANTISPOOF logquick antispoof_ifspc af antispoof_opts {
   1038 			struct pf_rule		 r;
   1039 			struct node_host	*h = NULL, *hh;
   1040 			struct node_if		*i, *j;
   1041 
   1042 			if (check_rulestate(PFCTL_STATE_FILTER))
   1043 				YYERROR;
   1044 
   1045 			for (i = $3; i; i = i->next) {
   1046 				bzero(&r, sizeof(r));
   1047 
   1048 				r.action = PF_DROP;
   1049 				r.direction = PF_IN;
   1050 				r.log = $2.log;
   1051 				r.logif = $2.logif;
   1052 				r.quick = $2.quick;
   1053 				r.af = $4;
   1054 				if (rule_label(&r, $5.label))
   1055 					YYERROR;
   1056 				r.rtableid = $5.rtableid;
   1057 				j = calloc(1, sizeof(struct node_if));
   1058 				if (j == NULL)
   1059 					err(1, "antispoof: calloc");
   1060 				if (strlcpy(j->ifname, i->ifname,
   1061 				    sizeof(j->ifname)) >= sizeof(j->ifname)) {
   1062 					free(j);
   1063 					yyerror("interface name too long");
   1064 					YYERROR;
   1065 				}
   1066 				j->not = 1;
   1067 				if (i->dynamic) {
   1068 					h = calloc(1, sizeof(*h));
   1069 					if (h == NULL)
   1070 						err(1, "address: calloc");
   1071 					h->addr.type = PF_ADDR_DYNIFTL;
   1072 					set_ipmask(h, 128);
   1073 					if (strlcpy(h->addr.v.ifname, i->ifname,
   1074 					    sizeof(h->addr.v.ifname)) >=
   1075 					    sizeof(h->addr.v.ifname)) {
   1076 						free(h);
   1077 						yyerror(
   1078 						    "interface name too long");
   1079 						YYERROR;
   1080 					}
   1081 					hh = malloc(sizeof(*hh));
   1082 					if (hh == NULL)
   1083 						 err(1, "address: malloc");
   1084 					bcopy(h, hh, sizeof(*hh));
   1085 					h->addr.iflags = PFI_AFLAG_NETWORK;
   1086 				} else {
   1087 					h = ifa_lookup(j->ifname,
   1088 					    PFI_AFLAG_NETWORK);
   1089 					hh = NULL;
   1090 				}
   1091 
   1092 				if (h != NULL)
   1093 					expand_rule(&r, j, NULL, NULL, NULL, h,
   1094 					    NULL, NULL, NULL, NULL, NULL,
   1095 					    NULL, "");
   1096 
   1097 				if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
   1098 					bzero(&r, sizeof(r));
   1099 
   1100 					r.action = PF_DROP;
   1101 					r.direction = PF_IN;
   1102 					r.log = $2.log;
   1103 					r.quick = $2.quick;
   1104 					r.af = $4;
   1105 					if (rule_label(&r, $5.label))
   1106 						YYERROR;
   1107 					r.rtableid = $5.rtableid;
   1108 					if (hh != NULL)
   1109 						h = hh;
   1110 					else
   1111 						h = ifa_lookup(i->ifname, 0);
   1112 					if (h != NULL)
   1113 						expand_rule(&r, NULL, NULL,
   1114 						    NULL, NULL, h, NULL, NULL,
   1115 						    NULL, NULL, NULL, NULL, "");
   1116 				} else
   1117 					free(hh);
   1118 			}
   1119 			free($5.label);
   1120 		}
   1121 		;
   1122 
   1123 antispoof_ifspc	: FOR antispoof_if		{ $$ = $2; }
   1124 		| FOR '{' antispoof_iflst '}'	{ $$ = $3; }
   1125 		;
   1126 
   1127 antispoof_iflst	: antispoof_if				{ $$ = $1; }
   1128 		| antispoof_iflst comma antispoof_if	{
   1129 			$1->tail->next = $3;
   1130 			$1->tail = $3;
   1131 			$$ = $1;
   1132 		}
   1133 		;
   1134 
   1135 antispoof_if  : if_item				{ $$ = $1; }
   1136 		| '(' if_item ')'		{
   1137 			$2->dynamic = 1;
   1138 			$$ = $2;
   1139 		}
   1140 		;
   1141 
   1142 antispoof_opts	:	{
   1143 				bzero(&antispoof_opts, sizeof antispoof_opts);
   1144 				antispoof_opts.rtableid = -1;
   1145 			}
   1146 		    antispoof_opts_l
   1147 			{ $$ = antispoof_opts; }
   1148 		| /* empty */	{
   1149 			bzero(&antispoof_opts, sizeof antispoof_opts);
   1150 			antispoof_opts.rtableid = -1;
   1151 			$$ = antispoof_opts;
   1152 		}
   1153 		;
   1154 
   1155 antispoof_opts_l	: antispoof_opts_l antispoof_opt
   1156 			| antispoof_opt
   1157 			;
   1158 
   1159 antispoof_opt	: label	{
   1160 			if (antispoof_opts.label) {
   1161 				yyerror("label cannot be redefined");
   1162 				YYERROR;
   1163 			}
   1164 			antispoof_opts.label = $1;
   1165 		}
   1166 		| RTABLE number				{
   1167 			if ($2 > RT_TABLEID_MAX || $2 < 0) {
   1168 				yyerror("invalid rtable id");
   1169 				YYERROR;
   1170 			}
   1171 			antispoof_opts.rtableid = $2;
   1172 		}
   1173 		;
   1174 
   1175 not		: '!'		{ $$ = 1; }
   1176 		| /* empty */	{ $$ = 0; }
   1177 		;
   1178 
   1179 tabledef	: TABLE '<' STRING '>' table_opts {
   1180 			struct node_host	 *h, *nh;
   1181 			struct node_tinit	 *ti, *nti;
   1182 
   1183 			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
   1184 				yyerror("table name too long, max %d chars",
   1185 				    PF_TABLE_NAME_SIZE - 1);
   1186 				free($3);
   1187 				YYERROR;
   1188 			}
   1189 			if (pf->loadopt & PFCTL_FLAG_TABLE)
   1190 				if (process_tabledef($3, &$5)) {
   1191 					free($3);
   1192 					YYERROR;
   1193 				}
   1194 			free($3);
   1195 			for (ti = SIMPLEQ_FIRST(&$5.init_nodes);
   1196 			    ti != SIMPLEQ_END(&$5.init_nodes); ti = nti) {
   1197 				if (ti->file)
   1198 					free(ti->file);
   1199 				for (h = ti->host; h != NULL; h = nh) {
   1200 					nh = h->next;
   1201 					free(h);
   1202 				}
   1203 				nti = SIMPLEQ_NEXT(ti, entries);
   1204 				free(ti);
   1205 			}
   1206 		}
   1207 		;
   1208 
   1209 table_opts	:	{
   1210 			bzero(&table_opts, sizeof table_opts);
   1211 			SIMPLEQ_INIT(&table_opts.init_nodes);
   1212 		}
   1213 		    table_opts_l
   1214 			{ $$ = table_opts; }
   1215 		| /* empty */
   1216 			{
   1217 			bzero(&table_opts, sizeof table_opts);
   1218 			SIMPLEQ_INIT(&table_opts.init_nodes);
   1219 			$$ = table_opts;
   1220 		}
   1221 		;
   1222 
   1223 table_opts_l	: table_opts_l table_opt
   1224 		| table_opt
   1225 		;
   1226 
   1227 table_opt	: STRING		{
   1228 			if (!strcmp($1, "const"))
   1229 				table_opts.flags |= PFR_TFLAG_CONST;
   1230 			else if (!strcmp($1, "persist"))
   1231 				table_opts.flags |= PFR_TFLAG_PERSIST;
   1232 			else {
   1233 				yyerror("invalid table option '%s'", $1);
   1234 				free($1);
   1235 				YYERROR;
   1236 			}
   1237 			free($1);
   1238 		}
   1239 		| '{' '}'		{ table_opts.init_addr = 1; }
   1240 		| '{' host_list '}'	{
   1241 			struct node_host	*n;
   1242 			struct node_tinit	*ti;
   1243 
   1244 			for (n = $2; n != NULL; n = n->next) {
   1245 				switch (n->addr.type) {
   1246 				case PF_ADDR_ADDRMASK:
   1247 					continue; /* ok */
   1248 				case PF_ADDR_DYNIFTL:
   1249 					yyerror("dynamic addresses are not "
   1250 					    "permitted inside tables");
   1251 					break;
   1252 				case PF_ADDR_TABLE:
   1253 					yyerror("tables cannot contain tables");
   1254 					break;
   1255 				case PF_ADDR_NOROUTE:
   1256 					yyerror("\"no-route\" is not permitted "
   1257 					    "inside tables");
   1258 					break;
   1259 				case PF_ADDR_URPFFAILED:
   1260 					yyerror("\"urpf-failed\" is not "
   1261 					    "permitted inside tables");
   1262 					break;
   1263 				default:
   1264 					yyerror("unknown address type %d",
   1265 					    n->addr.type);
   1266 				}
   1267 				YYERROR;
   1268 			}
   1269 			if (!(ti = calloc(1, sizeof(*ti))))
   1270 				err(1, "table_opt: calloc");
   1271 			ti->host = $2;
   1272 			SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
   1273 			    entries);
   1274 			table_opts.init_addr = 1;
   1275 		}
   1276 		| FILENAME STRING	{
   1277 			struct node_tinit	*ti;
   1278 
   1279 			if (!(ti = calloc(1, sizeof(*ti))))
   1280 				err(1, "table_opt: calloc");
   1281 			ti->file = $2;
   1282 			SIMPLEQ_INSERT_TAIL(&table_opts.init_nodes, ti,
   1283 			    entries);
   1284 			table_opts.init_addr = 1;
   1285 		}
   1286 		;
   1287 
   1288 altqif		: ALTQ interface queue_opts QUEUE qassign {
   1289 			struct pf_altq	a;
   1290 
   1291 			if (check_rulestate(PFCTL_STATE_QUEUE))
   1292 				YYERROR;
   1293 
   1294 			memset(&a, 0, sizeof(a));
   1295 			if ($3.scheduler.qtype == ALTQT_NONE) {
   1296 				yyerror("no scheduler specified!");
   1297 				YYERROR;
   1298 			}
   1299 			a.scheduler = $3.scheduler.qtype;
   1300 			a.qlimit = $3.qlimit;
   1301 			a.tbrsize = $3.tbrsize;
   1302 			if ($5 == NULL) {
   1303 				yyerror("no child queues specified");
   1304 				YYERROR;
   1305 			}
   1306 			if (expand_altq(&a, $2, $5, $3.queue_bwspec,
   1307 			    &$3.scheduler))
   1308 				YYERROR;
   1309 		}
   1310 		;
   1311 
   1312 queuespec	: QUEUE STRING interface queue_opts qassign {
   1313 			struct pf_altq	a;
   1314 
   1315 			if (check_rulestate(PFCTL_STATE_QUEUE)) {
   1316 				free($2);
   1317 				YYERROR;
   1318 			}
   1319 
   1320 			memset(&a, 0, sizeof(a));
   1321 
   1322 			if (strlcpy(a.qname, $2, sizeof(a.qname)) >=
   1323 			    sizeof(a.qname)) {
   1324 				yyerror("queue name too long (max "
   1325 				    "%d chars)", PF_QNAME_SIZE-1);
   1326 				free($2);
   1327 				YYERROR;
   1328 			}
   1329 			free($2);
   1330 			if ($4.tbrsize) {
   1331 				yyerror("cannot specify tbrsize for queue");
   1332 				YYERROR;
   1333 			}
   1334 			if ($4.priority > 255) {
   1335 				yyerror("priority out of range: max 255");
   1336 				YYERROR;
   1337 			}
   1338 			a.priority = $4.priority;
   1339 			a.qlimit = $4.qlimit;
   1340 			a.scheduler = $4.scheduler.qtype;
   1341 			if (expand_queue(&a, $3, $5, $4.queue_bwspec,
   1342 			    &$4.scheduler)) {
   1343 				yyerror("errors in queue definition");
   1344 				YYERROR;
   1345 			}
   1346 		}
   1347 		;
   1348 
   1349 queue_opts	:	{
   1350 			bzero(&queue_opts, sizeof queue_opts);
   1351 			queue_opts.priority = DEFAULT_PRIORITY;
   1352 			queue_opts.qlimit = DEFAULT_QLIMIT;
   1353 			queue_opts.scheduler.qtype = ALTQT_NONE;
   1354 			queue_opts.queue_bwspec.bw_percent = 100;
   1355 		}
   1356 		    queue_opts_l
   1357 			{ $$ = queue_opts; }
   1358 		| /* empty */ {
   1359 			bzero(&queue_opts, sizeof queue_opts);
   1360 			queue_opts.priority = DEFAULT_PRIORITY;
   1361 			queue_opts.qlimit = DEFAULT_QLIMIT;
   1362 			queue_opts.scheduler.qtype = ALTQT_NONE;
   1363 			queue_opts.queue_bwspec.bw_percent = 100;
   1364 			$$ = queue_opts;
   1365 		}
   1366 		;
   1367 
   1368 queue_opts_l	: queue_opts_l queue_opt
   1369 		| queue_opt
   1370 		;
   1371 
   1372 queue_opt	: BANDWIDTH bandwidth	{
   1373 			if (queue_opts.marker & QOM_BWSPEC) {
   1374 				yyerror("bandwidth cannot be respecified");
   1375 				YYERROR;
   1376 			}
   1377 			queue_opts.marker |= QOM_BWSPEC;
   1378 			queue_opts.queue_bwspec = $2;
   1379 		}
   1380 		| PRIORITY number	{
   1381 			if (queue_opts.marker & QOM_PRIORITY) {
   1382 				yyerror("priority cannot be respecified");
   1383 				YYERROR;
   1384 			}
   1385 			if ($2 > 255) {
   1386 				yyerror("priority out of range: max 255");
   1387 				YYERROR;
   1388 			}
   1389 			queue_opts.marker |= QOM_PRIORITY;
   1390 			queue_opts.priority = $2;
   1391 		}
   1392 		| QLIMIT number	{
   1393 			if (queue_opts.marker & QOM_QLIMIT) {
   1394 				yyerror("qlimit cannot be respecified");
   1395 				YYERROR;
   1396 			}
   1397 			if ($2 > 65535) {
   1398 				yyerror("qlimit out of range: max 65535");
   1399 				YYERROR;
   1400 			}
   1401 			queue_opts.marker |= QOM_QLIMIT;
   1402 			queue_opts.qlimit = $2;
   1403 		}
   1404 		| scheduler	{
   1405 			if (queue_opts.marker & QOM_SCHEDULER) {
   1406 				yyerror("scheduler cannot be respecified");
   1407 				YYERROR;
   1408 			}
   1409 			queue_opts.marker |= QOM_SCHEDULER;
   1410 			queue_opts.scheduler = $1;
   1411 		}
   1412 		| TBRSIZE number	{
   1413 			if (queue_opts.marker & QOM_TBRSIZE) {
   1414 				yyerror("tbrsize cannot be respecified");
   1415 				YYERROR;
   1416 			}
   1417 			if ($2 > 65535) {
   1418 				yyerror("tbrsize too big: max 65535");
   1419 				YYERROR;
   1420 			}
   1421 			queue_opts.marker |= QOM_TBRSIZE;
   1422 			queue_opts.tbrsize = $2;
   1423 		}
   1424 		;
   1425 
   1426 bandwidth	: STRING {
   1427 			double	 bps;
   1428 			char	*cp;
   1429 
   1430 			$$.bw_percent = 0;
   1431 
   1432 			bps = strtod($1, &cp);
   1433 			if (cp != NULL) {
   1434 				if (!strcmp(cp, "b"))
   1435 					; /* nothing */
   1436 				else if (!strcmp(cp, "Kb"))
   1437 					bps *= 1000;
   1438 				else if (!strcmp(cp, "Mb"))
   1439 					bps *= 1000 * 1000;
   1440 				else if (!strcmp(cp, "Gb"))
   1441 					bps *= 1000 * 1000 * 1000;
   1442 				else if (!strcmp(cp, "%")) {
   1443 					if (bps < 0 || bps > 100) {
   1444 						yyerror("bandwidth spec "
   1445 						    "out of range");
   1446 						free($1);
   1447 						YYERROR;
   1448 					}
   1449 					$$.bw_percent = bps;
   1450 					bps = 0;
   1451 				} else {
   1452 					yyerror("unknown unit %s", cp);
   1453 					free($1);
   1454 					YYERROR;
   1455 				}
   1456 			}
   1457 			free($1);
   1458 			$$.bw_absolute = (u_int32_t)bps;
   1459 		}
   1460 		;
   1461 
   1462 scheduler	: CBQ				{
   1463 			$$.qtype = ALTQT_CBQ;
   1464 			$$.data.cbq_opts.flags = 0;
   1465 		}
   1466 		| CBQ '(' cbqflags_list ')'	{
   1467 			$$.qtype = ALTQT_CBQ;
   1468 			$$.data.cbq_opts.flags = $3;
   1469 		}
   1470 		| PRIQ				{
   1471 			$$.qtype = ALTQT_PRIQ;
   1472 			$$.data.priq_opts.flags = 0;
   1473 		}
   1474 		| PRIQ '(' priqflags_list ')'	{
   1475 			$$.qtype = ALTQT_PRIQ;
   1476 			$$.data.priq_opts.flags = $3;
   1477 		}
   1478 		| HFSC				{
   1479 			$$.qtype = ALTQT_HFSC;
   1480 			bzero(&$$.data.hfsc_opts,
   1481 			    sizeof(struct node_hfsc_opts));
   1482 		}
   1483 		| HFSC '(' hfsc_opts ')'	{
   1484 			$$.qtype = ALTQT_HFSC;
   1485 			$$.data.hfsc_opts = $3;
   1486 		}
   1487 		;
   1488 
   1489 cbqflags_list	: cbqflags_item				{ $$ |= $1; }
   1490 		| cbqflags_list comma cbqflags_item	{ $$ |= $3; }
   1491 		;
   1492 
   1493 cbqflags_item	: STRING	{
   1494 			if (!strcmp($1, "default"))
   1495 				$$ = CBQCLF_DEFCLASS;
   1496 			else if (!strcmp($1, "borrow"))
   1497 				$$ = CBQCLF_BORROW;
   1498 			else if (!strcmp($1, "red"))
   1499 				$$ = CBQCLF_RED;
   1500 			else if (!strcmp($1, "ecn"))
   1501 				$$ = CBQCLF_RED|CBQCLF_ECN;
   1502 			else if (!strcmp($1, "rio"))
   1503 				$$ = CBQCLF_RIO;
   1504 			else {
   1505 				yyerror("unknown cbq flag \"%s\"", $1);
   1506 				free($1);
   1507 				YYERROR;
   1508 			}
   1509 			free($1);
   1510 		}
   1511 		;
   1512 
   1513 priqflags_list	: priqflags_item			{ $$ |= $1; }
   1514 		| priqflags_list comma priqflags_item	{ $$ |= $3; }
   1515 		;
   1516 
   1517 priqflags_item	: STRING	{
   1518 			if (!strcmp($1, "default"))
   1519 				$$ = PRCF_DEFAULTCLASS;
   1520 			else if (!strcmp($1, "red"))
   1521 				$$ = PRCF_RED;
   1522 			else if (!strcmp($1, "ecn"))
   1523 				$$ = PRCF_RED|PRCF_ECN;
   1524 			else if (!strcmp($1, "rio"))
   1525 				$$ = PRCF_RIO;
   1526 			else {
   1527 				yyerror("unknown priq flag \"%s\"", $1);
   1528 				free($1);
   1529 				YYERROR;
   1530 			}
   1531 			free($1);
   1532 		}
   1533 		;
   1534 
   1535 hfsc_opts	:	{
   1536 				bzero(&hfsc_opts,
   1537 				    sizeof(struct node_hfsc_opts));
   1538 			}
   1539 		    hfscopts_list				{
   1540 			$$ = hfsc_opts;
   1541 		}
   1542 		;
   1543 
   1544 hfscopts_list	: hfscopts_item
   1545 		| hfscopts_list comma hfscopts_item
   1546 		;
   1547 
   1548 hfscopts_item	: LINKSHARE bandwidth				{
   1549 			if (hfsc_opts.linkshare.used) {
   1550 				yyerror("linkshare already specified");
   1551 				YYERROR;
   1552 			}
   1553 			hfsc_opts.linkshare.m2 = $2;
   1554 			hfsc_opts.linkshare.used = 1;
   1555 		}
   1556 		| LINKSHARE '(' bandwidth comma number comma bandwidth ')'
   1557 		    {
   1558 			if (hfsc_opts.linkshare.used) {
   1559 				yyerror("linkshare already specified");
   1560 				YYERROR;
   1561 			}
   1562 			hfsc_opts.linkshare.m1 = $3;
   1563 			hfsc_opts.linkshare.d = $5;
   1564 			hfsc_opts.linkshare.m2 = $7;
   1565 			hfsc_opts.linkshare.used = 1;
   1566 		}
   1567 		| REALTIME bandwidth				{
   1568 			if (hfsc_opts.realtime.used) {
   1569 				yyerror("realtime already specified");
   1570 				YYERROR;
   1571 			}
   1572 			hfsc_opts.realtime.m2 = $2;
   1573 			hfsc_opts.realtime.used = 1;
   1574 		}
   1575 		| REALTIME '(' bandwidth comma number comma bandwidth ')'
   1576 		    {
   1577 			if (hfsc_opts.realtime.used) {
   1578 				yyerror("realtime already specified");
   1579 				YYERROR;
   1580 			}
   1581 			hfsc_opts.realtime.m1 = $3;
   1582 			hfsc_opts.realtime.d = $5;
   1583 			hfsc_opts.realtime.m2 = $7;
   1584 			hfsc_opts.realtime.used = 1;
   1585 		}
   1586 		| UPPERLIMIT bandwidth				{
   1587 			if (hfsc_opts.upperlimit.used) {
   1588 				yyerror("upperlimit already specified");
   1589 				YYERROR;
   1590 			}
   1591 			hfsc_opts.upperlimit.m2 = $2;
   1592 			hfsc_opts.upperlimit.used = 1;
   1593 		}
   1594 		| UPPERLIMIT '(' bandwidth comma number comma bandwidth ')'
   1595 		    {
   1596 			if (hfsc_opts.upperlimit.used) {
   1597 				yyerror("upperlimit already specified");
   1598 				YYERROR;
   1599 			}
   1600 			hfsc_opts.upperlimit.m1 = $3;
   1601 			hfsc_opts.upperlimit.d = $5;
   1602 			hfsc_opts.upperlimit.m2 = $7;
   1603 			hfsc_opts.upperlimit.used = 1;
   1604 		}
   1605 		| STRING	{
   1606 			if (!strcmp($1, "default"))
   1607 				hfsc_opts.flags |= HFCF_DEFAULTCLASS;
   1608 			else if (!strcmp($1, "red"))
   1609 				hfsc_opts.flags |= HFCF_RED;
   1610 			else if (!strcmp($1, "ecn"))
   1611 				hfsc_opts.flags |= HFCF_RED|HFCF_ECN;
   1612 			else if (!strcmp($1, "rio"))
   1613 				hfsc_opts.flags |= HFCF_RIO;
   1614 			else {
   1615 				yyerror("unknown hfsc flag \"%s\"", $1);
   1616 				free($1);
   1617 				YYERROR;
   1618 			}
   1619 			free($1);
   1620 		}
   1621 		;
   1622 
   1623 qassign		: /* empty */		{ $$ = NULL; }
   1624 		| qassign_item		{ $$ = $1; }
   1625 		| '{' qassign_list '}'	{ $$ = $2; }
   1626 		;
   1627 
   1628 qassign_list	: qassign_item			{ $$ = $1; }
   1629 		| qassign_list comma qassign_item	{
   1630 			$1->tail->next = $3;
   1631 			$1->tail = $3;
   1632 			$$ = $1;
   1633 		}
   1634 		;
   1635 
   1636 qassign_item	: STRING			{
   1637 			$$ = calloc(1, sizeof(struct node_queue));
   1638 			if ($$ == NULL)
   1639 				err(1, "qassign_item: calloc");
   1640 			if (strlcpy($$->queue, $1, sizeof($$->queue)) >=
   1641 			    sizeof($$->queue)) {
   1642 				yyerror("queue name '%s' too long (max "
   1643 				    "%d chars)", $1, sizeof($$->queue)-1);
   1644 				free($1);
   1645 				free($$);
   1646 				YYERROR;
   1647 			}
   1648 			free($1);
   1649 			$$->next = NULL;
   1650 			$$->tail = $$;
   1651 		}
   1652 		;
   1653 
   1654 pfrule		: action dir logquick interface route af proto fromto
   1655 		    filter_opts
   1656 		{
   1657 			struct pf_rule		 r;
   1658 			struct node_state_opt	*o;
   1659 			struct node_proto	*proto;
   1660 			int			 srctrack = 0;
   1661 			int			 statelock = 0;
   1662 			int			 adaptive = 0;
   1663 
   1664 			if (check_rulestate(PFCTL_STATE_FILTER))
   1665 				YYERROR;
   1666 
   1667 			memset(&r, 0, sizeof(r));
   1668 
   1669 			r.action = $1.b1;
   1670 			switch ($1.b2) {
   1671 			case PFRULE_RETURNRST:
   1672 				r.rule_flag |= PFRULE_RETURNRST;
   1673 				r.return_ttl = $1.w;
   1674 				break;
   1675 			case PFRULE_RETURNICMP:
   1676 				r.rule_flag |= PFRULE_RETURNICMP;
   1677 				r.return_icmp = $1.w;
   1678 				r.return_icmp6 = $1.w2;
   1679 				break;
   1680 			case PFRULE_RETURN:
   1681 				r.rule_flag |= PFRULE_RETURN;
   1682 				r.return_icmp = $1.w;
   1683 				r.return_icmp6 = $1.w2;
   1684 				break;
   1685 			}
   1686 			r.direction = $2;
   1687 			r.log = $3.log;
   1688 			r.logif = $3.logif;
   1689 			r.quick = $3.quick;
   1690 			r.prob = $9.prob;
   1691 			r.rtableid = $9.rtableid;
   1692 
   1693 			r.af = $6;
   1694 			if ($9.tag)
   1695 				if (strlcpy(r.tagname, $9.tag,
   1696 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
   1697 					yyerror("tag too long, max %u chars",
   1698 					    PF_TAG_NAME_SIZE - 1);
   1699 					YYERROR;
   1700 				}
   1701 			if ($9.match_tag)
   1702 				if (strlcpy(r.match_tagname, $9.match_tag,
   1703 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
   1704 					yyerror("tag too long, max %u chars",
   1705 					    PF_TAG_NAME_SIZE - 1);
   1706 					YYERROR;
   1707 				}
   1708 			r.match_tag_not = $9.match_tag_not;
   1709 			if (rule_label(&r, $9.label))
   1710 				YYERROR;
   1711 			free($9.label);
   1712 			r.flags = $9.flags.b1;
   1713 			r.flagset = $9.flags.b2;
   1714 			if (($9.flags.b1 & $9.flags.b2) != $9.flags.b1) {
   1715 				yyerror("flags always false");
   1716 				YYERROR;
   1717 			}
   1718 			if ($9.flags.b1 || $9.flags.b2 || $8.src_os) {
   1719 				for (proto = $7; proto != NULL &&
   1720 				    proto->proto != IPPROTO_TCP;
   1721 				    proto = proto->next)
   1722 					;	/* nothing */
   1723 				if (proto == NULL && $7 != NULL) {
   1724 					if ($9.flags.b1 || $9.flags.b2)
   1725 						yyerror(
   1726 						    "flags only apply to tcp");
   1727 					if ($8.src_os)
   1728 						yyerror(
   1729 						    "OS fingerprinting only "
   1730 						    "apply to tcp");
   1731 					YYERROR;
   1732 				}
   1733 #if 0
   1734 				if (($9.flags.b1 & parse_flags("S")) == 0 &&
   1735 				    $8.src_os) {
   1736 					yyerror("OS fingerprinting requires "
   1737 					    "the SYN TCP flag (flags S/SA)");
   1738 					YYERROR;
   1739 				}
   1740 #endif
   1741 			}
   1742 
   1743 			r.tos = $9.tos;
   1744 			r.keep_state = $9.keep.action;
   1745 
   1746 			/* 'keep state' by default on pass rules. */
   1747 			if (!r.keep_state && !r.action &&
   1748 			    !($9.marker & FOM_KEEP))
   1749 				r.keep_state = PF_STATE_NORMAL;
   1750 
   1751 			o = $9.keep.options;
   1752 			while (o) {
   1753 				struct node_state_opt	*p = o;
   1754 
   1755 				switch (o->type) {
   1756 				case PF_STATE_OPT_MAX:
   1757 					if (r.max_states) {
   1758 						yyerror("state option 'max' "
   1759 						    "multiple definitions");
   1760 						YYERROR;
   1761 					}
   1762 					r.max_states = o->data.max_states;
   1763 					break;
   1764 				case PF_STATE_OPT_NOSYNC:
   1765 					if (r.rule_flag & PFRULE_NOSYNC) {
   1766 						yyerror("state option 'sync' "
   1767 						    "multiple definitions");
   1768 						YYERROR;
   1769 					}
   1770 					r.rule_flag |= PFRULE_NOSYNC;
   1771 					break;
   1772 				case PF_STATE_OPT_SRCTRACK:
   1773 					if (srctrack) {
   1774 						yyerror("state option "
   1775 						    "'source-track' "
   1776 						    "multiple definitions");
   1777 						YYERROR;
   1778 					}
   1779 					srctrack =  o->data.src_track;
   1780 					r.rule_flag |= PFRULE_SRCTRACK;
   1781 					break;
   1782 				case PF_STATE_OPT_MAX_SRC_STATES:
   1783 					if (r.max_src_states) {
   1784 						yyerror("state option "
   1785 						    "'max-src-states' "
   1786 						    "multiple definitions");
   1787 						YYERROR;
   1788 					}
   1789 					if (o->data.max_src_states == 0) {
   1790 						yyerror("'max-src-states' must "
   1791 						    "be > 0");
   1792 						YYERROR;
   1793 					}
   1794 					r.max_src_states =
   1795 					    o->data.max_src_states;
   1796 					r.rule_flag |= PFRULE_SRCTRACK;
   1797 					break;
   1798 				case PF_STATE_OPT_OVERLOAD:
   1799 					if (r.overload_tblname[0]) {
   1800 						yyerror("multiple 'overload' "
   1801 						    "table definitions");
   1802 						YYERROR;
   1803 					}
   1804 					if (strlcpy(r.overload_tblname,
   1805 					    o->data.overload.tblname,
   1806 					    PF_TABLE_NAME_SIZE) >=
   1807 					    PF_TABLE_NAME_SIZE) {
   1808 						yyerror("state option: "
   1809 						    "strlcpy");
   1810 						YYERROR;
   1811 					}
   1812 					r.flush = o->data.overload.flush;
   1813 					break;
   1814 				case PF_STATE_OPT_MAX_SRC_CONN:
   1815 					if (r.max_src_conn) {
   1816 						yyerror("state option "
   1817 						    "'max-src-conn' "
   1818 						    "multiple definitions");
   1819 						YYERROR;
   1820 					}
   1821 					if (o->data.max_src_conn == 0) {
   1822 						yyerror("'max-src-conn' "
   1823 						    "must be > 0");
   1824 						YYERROR;
   1825 					}
   1826 					r.max_src_conn =
   1827 					    o->data.max_src_conn;
   1828 					r.rule_flag |= PFRULE_SRCTRACK |
   1829 					    PFRULE_RULESRCTRACK;
   1830 					break;
   1831 				case PF_STATE_OPT_MAX_SRC_CONN_RATE:
   1832 					if (r.max_src_conn_rate.limit) {
   1833 						yyerror("state option "
   1834 						    "'max-src-conn-rate' "
   1835 						    "multiple definitions");
   1836 						YYERROR;
   1837 					}
   1838 					if (!o->data.max_src_conn_rate.limit ||
   1839 					    !o->data.max_src_conn_rate.seconds) {
   1840 						yyerror("'max-src-conn-rate' "
   1841 						    "values must be > 0");
   1842 						YYERROR;
   1843 					}
   1844 					if (o->data.max_src_conn_rate.limit >
   1845 					    PF_THRESHOLD_MAX) {
   1846 						yyerror("'max-src-conn-rate' "
   1847 						    "maximum rate must be < %u",
   1848 						    PF_THRESHOLD_MAX);
   1849 						YYERROR;
   1850 					}
   1851 					r.max_src_conn_rate.limit =
   1852 					    o->data.max_src_conn_rate.limit;
   1853 					r.max_src_conn_rate.seconds =
   1854 					    o->data.max_src_conn_rate.seconds;
   1855 					r.rule_flag |= PFRULE_SRCTRACK |
   1856 					    PFRULE_RULESRCTRACK;
   1857 					break;
   1858 				case PF_STATE_OPT_MAX_SRC_NODES:
   1859 					if (r.max_src_nodes) {
   1860 						yyerror("state option "
   1861 						    "'max-src-nodes' "
   1862 						    "multiple definitions");
   1863 						YYERROR;
   1864 					}
   1865 					if (o->data.max_src_nodes == 0) {
   1866 						yyerror("'max-src-nodes' must "
   1867 						    "be > 0");
   1868 						YYERROR;
   1869 					}
   1870 					r.max_src_nodes =
   1871 					    o->data.max_src_nodes;
   1872 					r.rule_flag |= PFRULE_SRCTRACK |
   1873 					    PFRULE_RULESRCTRACK;
   1874 					break;
   1875 				case PF_STATE_OPT_STATELOCK:
   1876 					if (statelock) {
   1877 						yyerror("state locking option: "
   1878 						    "multiple definitions");
   1879 						YYERROR;
   1880 					}
   1881 					statelock = 1;
   1882 					r.rule_flag |= o->data.statelock;
   1883 					break;
   1884 				case PF_STATE_OPT_TIMEOUT:
   1885 					if (o->data.timeout.number ==
   1886 					    PFTM_ADAPTIVE_START ||
   1887 					    o->data.timeout.number ==
   1888 					    PFTM_ADAPTIVE_END)
   1889 						adaptive = 1;
   1890 					if (r.timeout[o->data.timeout.number]) {
   1891 						yyerror("state timeout %s "
   1892 						    "multiple definitions",
   1893 						    pf_timeouts[o->data.
   1894 						    timeout.number].name);
   1895 						YYERROR;
   1896 					}
   1897 					r.timeout[o->data.timeout.number] =
   1898 					    o->data.timeout.seconds;
   1899 				}
   1900 				o = o->next;
   1901 				free(p);
   1902 			}
   1903 
   1904 			/* 'flags S/SA' by default on stateful rules */
   1905 			if (!r.action && !r.flags && !r.flagset &&
   1906 			    !$9.fragment && !($9.marker & FOM_FLAGS) &&
   1907 			    r.keep_state) {
   1908 				r.flags = parse_flags("S");
   1909 				r.flagset =  parse_flags("SA");
   1910 			}
   1911 			if (!adaptive && r.max_states) {
   1912 				r.timeout[PFTM_ADAPTIVE_START] =
   1913 				    (r.max_states / 10) * 6;
   1914 				r.timeout[PFTM_ADAPTIVE_END] =
   1915 				    (r.max_states / 10) * 12;
   1916 			}
   1917 			if (r.rule_flag & PFRULE_SRCTRACK) {
   1918 				if (srctrack == PF_SRCTRACK_GLOBAL &&
   1919 				    r.max_src_nodes) {
   1920 					yyerror("'max-src-nodes' is "
   1921 					    "incompatible with "
   1922 					    "'source-track global'");
   1923 					YYERROR;
   1924 				}
   1925 				if (srctrack == PF_SRCTRACK_GLOBAL &&
   1926 				    r.max_src_conn) {
   1927 					yyerror("'max-src-conn' is "
   1928 					    "incompatible with "
   1929 					    "'source-track global'");
   1930 					YYERROR;
   1931 				}
   1932 				if (srctrack == PF_SRCTRACK_GLOBAL &&
   1933 				    r.max_src_conn_rate.seconds) {
   1934 					yyerror("'max-src-conn-rate' is "
   1935 					    "incompatible with "
   1936 					    "'source-track global'");
   1937 					YYERROR;
   1938 				}
   1939 				if (r.timeout[PFTM_SRC_NODE] <
   1940 				    r.max_src_conn_rate.seconds)
   1941 					r.timeout[PFTM_SRC_NODE] =
   1942 					    r.max_src_conn_rate.seconds;
   1943 				r.rule_flag |= PFRULE_SRCTRACK;
   1944 				if (srctrack == PF_SRCTRACK_RULE)
   1945 					r.rule_flag |= PFRULE_RULESRCTRACK;
   1946 			}
   1947 			if (r.keep_state && !statelock)
   1948 				r.rule_flag |= default_statelock;
   1949 
   1950 			if ($9.fragment)
   1951 				r.rule_flag |= PFRULE_FRAGMENT;
   1952 			r.allow_opts = $9.allowopts;
   1953 
   1954 			decide_address_family($8.src.host, &r.af);
   1955 			decide_address_family($8.dst.host, &r.af);
   1956 
   1957 			if ($5.rt) {
   1958 				if (!r.direction) {
   1959 					yyerror("direction must be explicit "
   1960 					    "with rules that specify routing");
   1961 					YYERROR;
   1962 				}
   1963 				r.rt = $5.rt;
   1964 				r.rpool.opts = $5.pool_opts;
   1965 				if ($5.key != NULL)
   1966 					memcpy(&r.rpool.key, $5.key,
   1967 					    sizeof(struct pf_poolhashkey));
   1968 			}
   1969 			if (r.rt && r.rt != PF_FASTROUTE) {
   1970 				decide_address_family($5.host, &r.af);
   1971 				remove_invalid_hosts(&$5.host, &r.af);
   1972 				if ($5.host == NULL) {
   1973 					yyerror("no routing address with "
   1974 					    "matching address family found.");
   1975 					YYERROR;
   1976 				}
   1977 				if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
   1978 				    PF_POOL_NONE && ($5.host->next != NULL ||
   1979 				    $5.host->addr.type == PF_ADDR_TABLE ||
   1980 				    DYNIF_MULTIADDR($5.host->addr)))
   1981 					r.rpool.opts |= PF_POOL_ROUNDROBIN;
   1982 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
   1983 				    PF_POOL_ROUNDROBIN &&
   1984 				    disallow_table($5.host, "tables are only "
   1985 				    "supported in round-robin routing pools"))
   1986 					YYERROR;
   1987 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
   1988 				    PF_POOL_ROUNDROBIN &&
   1989 				    disallow_alias($5.host, "interface (%s) "
   1990 				    "is only supported in round-robin "
   1991 				    "routing pools"))
   1992 					YYERROR;
   1993 				if ($5.host->next != NULL) {
   1994 					if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
   1995 					    PF_POOL_ROUNDROBIN) {
   1996 						yyerror("r.rpool.opts must "
   1997 						    "be PF_POOL_ROUNDROBIN");
   1998 						YYERROR;
   1999 					}
   2000 				}
   2001 			}
   2002 			if ($9.queues.qname != NULL) {
   2003 				if (strlcpy(r.qname, $9.queues.qname,
   2004 				    sizeof(r.qname)) >= sizeof(r.qname)) {
   2005 					yyerror("rule qname too long (max "
   2006 					    "%d chars)", sizeof(r.qname)-1);
   2007 					YYERROR;
   2008 				}
   2009 				free($9.queues.qname);
   2010 			}
   2011 			if ($9.queues.pqname != NULL) {
   2012 				if (strlcpy(r.pqname, $9.queues.pqname,
   2013 				    sizeof(r.pqname)) >= sizeof(r.pqname)) {
   2014 					yyerror("rule pqname too long (max "
   2015 					    "%d chars)", sizeof(r.pqname)-1);
   2016 					YYERROR;
   2017 				}
   2018 				free($9.queues.pqname);
   2019 			}
   2020 
   2021 			expand_rule(&r, $4, $5.host, $7, $8.src_os,
   2022 			    $8.src.host, $8.src.port, $8.dst.host, $8.dst.port,
   2023 			    $9.uid, $9.gid, $9.icmpspec, "");
   2024 		}
   2025 		;
   2026 
   2027 filter_opts	:	{
   2028 				bzero(&filter_opts, sizeof filter_opts);
   2029 				filter_opts.rtableid = -1;
   2030 			}
   2031 		    filter_opts_l
   2032 			{ $$ = filter_opts; }
   2033 		| /* empty */	{
   2034 			bzero(&filter_opts, sizeof filter_opts);
   2035 			filter_opts.rtableid = -1;
   2036 			$$ = filter_opts;
   2037 		}
   2038 		;
   2039 
   2040 filter_opts_l	: filter_opts_l filter_opt
   2041 		| filter_opt
   2042 		;
   2043 
   2044 filter_opt	: USER uids {
   2045 			if (filter_opts.uid)
   2046 				$2->tail->next = filter_opts.uid;
   2047 			filter_opts.uid = $2;
   2048 		}
   2049 		| GROUP gids {
   2050 			if (filter_opts.gid)
   2051 				$2->tail->next = filter_opts.gid;
   2052 			filter_opts.gid = $2;
   2053 		}
   2054 		| flags {
   2055 			if (filter_opts.marker & FOM_FLAGS) {
   2056 				yyerror("flags cannot be redefined");
   2057 				YYERROR;
   2058 			}
   2059 			filter_opts.marker |= FOM_FLAGS;
   2060 			filter_opts.flags.b1 |= $1.b1;
   2061 			filter_opts.flags.b2 |= $1.b2;
   2062 			filter_opts.flags.w |= $1.w;
   2063 			filter_opts.flags.w2 |= $1.w2;
   2064 		}
   2065 		| icmpspec {
   2066 			if (filter_opts.marker & FOM_ICMP) {
   2067 				yyerror("icmp-type cannot be redefined");
   2068 				YYERROR;
   2069 			}
   2070 			filter_opts.marker |= FOM_ICMP;
   2071 			filter_opts.icmpspec = $1;
   2072 		}
   2073 		| tos {
   2074 			if (filter_opts.marker & FOM_TOS) {
   2075 				yyerror("tos cannot be redefined");
   2076 				YYERROR;
   2077 			}
   2078 			filter_opts.marker |= FOM_TOS;
   2079 			filter_opts.tos = $1;
   2080 		}
   2081 		| keep {
   2082 			if (filter_opts.marker & FOM_KEEP) {
   2083 				yyerror("modulate or keep cannot be redefined");
   2084 				YYERROR;
   2085 			}
   2086 			filter_opts.marker |= FOM_KEEP;
   2087 			filter_opts.keep.action = $1.action;
   2088 			filter_opts.keep.options = $1.options;
   2089 		}
   2090 		| FRAGMENT {
   2091 			filter_opts.fragment = 1;
   2092 		}
   2093 		| ALLOWOPTS {
   2094 			filter_opts.allowopts = 1;
   2095 		}
   2096 		| label	{
   2097 			if (filter_opts.label) {
   2098 				yyerror("label cannot be redefined");
   2099 				YYERROR;
   2100 			}
   2101 			filter_opts.label = $1;
   2102 		}
   2103 		| qname	{
   2104 			if (filter_opts.queues.qname) {
   2105 				yyerror("queue cannot be redefined");
   2106 				YYERROR;
   2107 			}
   2108 			filter_opts.queues = $1;
   2109 		}
   2110 		| TAG string				{
   2111 			filter_opts.tag = $2;
   2112 		}
   2113 		| not TAGGED string			{
   2114 			filter_opts.match_tag = $3;
   2115 			filter_opts.match_tag_not = $1;
   2116 		}
   2117 		| PROBABILITY STRING			{
   2118 			char	*e;
   2119 			double	 p = strtod($2, &e);
   2120 
   2121 			if (*e == '%') {
   2122 				p *= 0.01;
   2123 				e++;
   2124 			}
   2125 			if (*e) {
   2126 				yyerror("invalid probability: %s", $2);
   2127 				free($2);
   2128 				YYERROR;
   2129 			}
   2130 			p = floor(p * (UINT_MAX+1.0) + 0.5);
   2131 			if (p < 1.0 || p >= (UINT_MAX+1.0)) {
   2132 				yyerror("invalid probability: %s", $2);
   2133 				free($2);
   2134 				YYERROR;
   2135 			}
   2136 			filter_opts.prob = (u_int32_t)p;
   2137 			free($2);
   2138 		}
   2139 		| RTABLE number				{
   2140 			if ($2 > RT_TABLEID_MAX || $2 < 0) {
   2141 				yyerror("invalid rtable id");
   2142 				YYERROR;
   2143 			}
   2144 			filter_opts.rtableid = $2;
   2145 		}
   2146 		;
   2147 
   2148 action		: PASS			{ $$.b1 = PF_PASS; $$.b2 = $$.w = 0; }
   2149 		| BLOCK blockspec	{ $$ = $2; $$.b1 = PF_DROP; }
   2150 		;
   2151 
   2152 blockspec	: /* empty */		{
   2153 			$$.b2 = blockpolicy;
   2154 			$$.w = returnicmpdefault;
   2155 			$$.w2 = returnicmp6default;
   2156 		}
   2157 		| DROP			{
   2158 			$$.b2 = PFRULE_DROP;
   2159 			$$.w = 0;
   2160 			$$.w2 = 0;
   2161 		}
   2162 		| RETURNRST		{
   2163 			$$.b2 = PFRULE_RETURNRST;
   2164 			$$.w = 0;
   2165 			$$.w2 = 0;
   2166 		}
   2167 		| RETURNRST '(' TTL number ')'	{
   2168 			if ($4 > 255) {
   2169 				yyerror("illegal ttl value %d", $4);
   2170 				YYERROR;
   2171 			}
   2172 			$$.b2 = PFRULE_RETURNRST;
   2173 			$$.w = $4;
   2174 			$$.w2 = 0;
   2175 		}
   2176 		| RETURNICMP		{
   2177 			$$.b2 = PFRULE_RETURNICMP;
   2178 			$$.w = returnicmpdefault;
   2179 			$$.w2 = returnicmp6default;
   2180 		}
   2181 		| RETURNICMP6		{
   2182 			$$.b2 = PFRULE_RETURNICMP;
   2183 			$$.w = returnicmpdefault;
   2184 			$$.w2 = returnicmp6default;
   2185 		}
   2186 		| RETURNICMP '(' STRING ')'	{
   2187 			$$.b2 = PFRULE_RETURNICMP;
   2188 			if (!($$.w = parseicmpspec($3, AF_INET))) {
   2189 				free($3);
   2190 				YYERROR;
   2191 			}
   2192 			free($3);
   2193 			$$.w2 = returnicmp6default;
   2194 		}
   2195 		| RETURNICMP6 '(' STRING ')'	{
   2196 			$$.b2 = PFRULE_RETURNICMP;
   2197 			$$.w = returnicmpdefault;
   2198 			if (!($$.w2 = parseicmpspec($3, AF_INET6))) {
   2199 				free($3);
   2200 				YYERROR;
   2201 			}
   2202 			free($3);
   2203 		}
   2204 		| RETURNICMP '(' STRING comma STRING ')' {
   2205 			$$.b2 = PFRULE_RETURNICMP;
   2206 			if (!($$.w = parseicmpspec($3, AF_INET)) ||
   2207 			    !($$.w2 = parseicmpspec($5, AF_INET6))) {
   2208 				free($3);
   2209 				free($5);
   2210 				YYERROR;
   2211 			}
   2212 			free($3);
   2213 			free($5);
   2214 		}
   2215 		| RETURN {
   2216 			$$.b2 = PFRULE_RETURN;
   2217 			$$.w = returnicmpdefault;
   2218 			$$.w2 = returnicmp6default;
   2219 		}
   2220 		;
   2221 
   2222 dir		: /* empty */			{ $$ = 0; }
   2223 		| IN				{ $$ = PF_IN; }
   2224 		| OUT				{ $$ = PF_OUT; }
   2225 		;
   2226 
   2227 quick		: /* empty */			{ $$.quick = 0; }
   2228 		| QUICK				{ $$.quick = 1; }
   2229 		;
   2230 
   2231 logquick	: /* empty */	{ $$.log = 0; $$.quick = 0; $$.logif = 0; }
   2232 		| log		{ $$ = $1; $$.quick = 0; }
   2233 		| QUICK		{ $$.quick = 1; $$.log = 0; $$.logif = 0; }
   2234 		| log QUICK	{ $$ = $1; $$.quick = 1; }
   2235 		| QUICK log	{ $$ = $2; $$.quick = 1; }
   2236 		;
   2237 
   2238 log		: LOG			{ $$.log = PF_LOG; $$.logif = 0; }
   2239 		| LOG '(' logopts ')'	{
   2240 			$$.log = PF_LOG | $3.log;
   2241 			$$.logif = $3.logif;
   2242 		}
   2243 		;
   2244 
   2245 logopts		: logopt			{ $$ = $1; }
   2246 		| logopts comma logopt		{
   2247 			$$.log = $1.log | $3.log;
   2248 			$$.logif = $3.logif;
   2249 			if ($$.logif == 0)
   2250 				$$.logif = $1.logif;
   2251 		}
   2252 		;
   2253 
   2254 logopt		: ALL		{ $$.log = PF_LOG_ALL; $$.logif = 0; }
   2255 		| USER		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
   2256 		| GROUP		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
   2257 		| TO string	{
   2258 			const char	*errstr;
   2259 			u_int		 i;
   2260 
   2261 			$$.log = 0;
   2262 			if (strncmp($2, "pflog", 5)) {
   2263 				yyerror("%s: should be a pflog interface", $2);
   2264 				free($2);
   2265 				YYERROR;
   2266 			}
   2267 			i = strtonum($2 + 5, 0, 255, &errstr);
   2268 			if (errstr) {
   2269 				yyerror("%s: %s", $2, errstr);
   2270 				free($2);
   2271 				YYERROR;
   2272 			}
   2273 			free($2);
   2274 			$$.logif = i;
   2275 		}
   2276 		;
   2277 
   2278 interface	: /* empty */			{ $$ = NULL; }
   2279 		| ON if_item_not		{ $$ = $2; }
   2280 		| ON '{' if_list '}'		{ $$ = $3; }
   2281 		;
   2282 
   2283 if_list		: if_item_not			{ $$ = $1; }
   2284 		| if_list comma if_item_not	{
   2285 			$1->tail->next = $3;
   2286 			$1->tail = $3;
   2287 			$$ = $1;
   2288 		}
   2289 		;
   2290 
   2291 if_item_not	: not if_item			{ $$ = $2; $$->not = $1; }
   2292 		;
   2293 
   2294 if_item		: STRING			{
   2295 			struct node_host	*n;
   2296 
   2297 			$$ = calloc(1, sizeof(struct node_if));
   2298 			if ($$ == NULL)
   2299 				err(1, "if_item: calloc");
   2300 			if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
   2301 			    sizeof($$->ifname)) {
   2302 				free($1);
   2303 				free($$);
   2304 				yyerror("interface name too long");
   2305 				YYERROR;
   2306 			}
   2307 
   2308 			if ((n = ifa_exists($1)) != NULL)
   2309 				$$->ifa_flags = n->ifa_flags;
   2310 
   2311 			free($1);
   2312 			$$->not = 0;
   2313 			$$->next = NULL;
   2314 			$$->tail = $$;
   2315 		}
   2316 		;
   2317 
   2318 af		: /* empty */			{ $$ = 0; }
   2319 		| INET				{ $$ = AF_INET; }
   2320 		| INET6				{ $$ = AF_INET6; }
   2321 		;
   2322 
   2323 proto		: /* empty */			{ $$ = NULL; }
   2324 		| PROTO proto_item		{ $$ = $2; }
   2325 		| PROTO '{' proto_list '}'	{ $$ = $3; }
   2326 		;
   2327 
   2328 proto_list	: proto_item			{ $$ = $1; }
   2329 		| proto_list comma proto_item	{
   2330 			$1->tail->next = $3;
   2331 			$1->tail = $3;
   2332 			$$ = $1;
   2333 		}
   2334 		;
   2335 
   2336 proto_item	: STRING			{
   2337 			u_int8_t	pr;
   2338 			u_long		ulval;
   2339 
   2340 			if (atoul($1, &ulval) == 0) {
   2341 				if (ulval > 255) {
   2342 					yyerror("protocol outside range");
   2343 					free($1);
   2344 					YYERROR;
   2345 				}
   2346 				pr = (u_int8_t)ulval;
   2347 			} else {
   2348 				struct protoent	*p;
   2349 
   2350 				p = getprotobyname($1);
   2351 				if (p == NULL) {
   2352 					yyerror("unknown protocol %s", $1);
   2353 					free($1);
   2354 					YYERROR;
   2355 				}
   2356 				pr = p->p_proto;
   2357 			}
   2358 			free($1);
   2359 			if (pr == 0) {
   2360 				yyerror("proto 0 cannot be used");
   2361 				YYERROR;
   2362 			}
   2363 			$$ = calloc(1, sizeof(struct node_proto));
   2364 			if ($$ == NULL)
   2365 				err(1, "proto_item: calloc");
   2366 			$$->proto = pr;
   2367 			$$->next = NULL;
   2368 			$$->tail = $$;
   2369 		}
   2370 		;
   2371 
   2372 fromto		: ALL				{
   2373 			$$.src.host = NULL;
   2374 			$$.src.port = NULL;
   2375 			$$.dst.host = NULL;
   2376 			$$.dst.port = NULL;
   2377 			$$.src_os = NULL;
   2378 		}
   2379 		| from os to			{
   2380 			$$.src = $1;
   2381 			$$.src_os = $2;
   2382 			$$.dst = $3;
   2383 		}
   2384 		;
   2385 
   2386 os		: /* empty */			{ $$ = NULL; }
   2387 		| OS xos			{ $$ = $2; }
   2388 		| OS '{' os_list '}'		{ $$ = $3; }
   2389 		;
   2390 
   2391 xos		: STRING {
   2392 			$$ = calloc(1, sizeof(struct node_os));
   2393 			if ($$ == NULL)
   2394 				err(1, "os: calloc");
   2395 			$$->os = $1;
   2396 			$$->tail = $$;
   2397 		}
   2398 		;
   2399 
   2400 os_list		: xos				{ $$ = $1; }
   2401 		| os_list comma xos		{
   2402 			$1->tail->next = $3;
   2403 			$1->tail = $3;
   2404 			$$ = $1;
   2405 		}
   2406 		;
   2407 
   2408 from		: /* empty */			{
   2409 			$$.host = NULL;
   2410 			$$.port = NULL;
   2411 		}
   2412 		| FROM ipportspec		{
   2413 			$$ = $2;
   2414 		}
   2415 		;
   2416 
   2417 to		: /* empty */			{
   2418 			$$.host = NULL;
   2419 			$$.port = NULL;
   2420 		}
   2421 		| TO ipportspec		{
   2422 			if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
   2423 			    "not permitted in a destination address"))
   2424 				YYERROR;
   2425 			$$ = $2;
   2426 		}
   2427 		;
   2428 
   2429 ipportspec	: ipspec			{
   2430 			$$.host = $1;
   2431 			$$.port = NULL;
   2432 		}
   2433 		| ipspec PORT portspec		{
   2434 			$$.host = $1;
   2435 			$$.port = $3;
   2436 		}
   2437 		| PORT portspec			{
   2438 			$$.host = NULL;
   2439 			$$.port = $2;
   2440 		}
   2441 		;
   2442 
   2443 ipspec		: ANY				{ $$ = NULL; }
   2444 		| xhost				{ $$ = $1; }
   2445 		| '{' host_list '}'		{ $$ = $2; }
   2446 		;
   2447 
   2448 host_list	: ipspec			{ $$ = $1; }
   2449 		| host_list comma ipspec	{
   2450 			if ($3 == NULL)
   2451 				$$ = $1;
   2452 			else if ($1 == NULL)
   2453 				$$ = $3;
   2454 			else {
   2455 				$1->tail->next = $3;
   2456 				$1->tail = $3->tail;
   2457 				$$ = $1;
   2458 			}
   2459 		}
   2460 		;
   2461 
   2462 xhost		: not host			{
   2463 			struct node_host	*n;
   2464 
   2465 			for (n = $2; n != NULL; n = n->next)
   2466 				n->not = $1;
   2467 			$$ = $2;
   2468 		}
   2469 		| not NOROUTE			{
   2470 			$$ = calloc(1, sizeof(struct node_host));
   2471 			if ($$ == NULL)
   2472 				err(1, "xhost: calloc");
   2473 			$$->addr.type = PF_ADDR_NOROUTE;
   2474 			$$->next = NULL;
   2475 			$$->not = $1;
   2476 			$$->tail = $$;
   2477 		}
   2478 		| not URPFFAILED		{
   2479 			$$ = calloc(1, sizeof(struct node_host));
   2480 			if ($$ == NULL)
   2481 				err(1, "xhost: calloc");
   2482 			$$->addr.type = PF_ADDR_URPFFAILED;
   2483 			$$->next = NULL;
   2484 			$$->not = $1;
   2485 			$$->tail = $$;
   2486 		}
   2487 		;
   2488 
   2489 host		: STRING			{
   2490 			if (($$ = host($1)) == NULL)	{
   2491 				/* error. "any" is handled elsewhere */
   2492 				free($1);
   2493 				yyerror("could not parse host specification");
   2494 				YYERROR;
   2495 			}
   2496 			free($1);
   2497 
   2498 		}
   2499 		| STRING '/' number		{
   2500 			char	*buf;
   2501 
   2502 			if (asprintf(&buf, "%s/%u", $1, $3) == -1)
   2503 				err(1, "host: asprintf");
   2504 			free($1);
   2505 			if (($$ = host(buf)) == NULL)	{
   2506 				/* error. "any" is handled elsewhere */
   2507 				free(buf);
   2508 				yyerror("could not parse host specification");
   2509 				YYERROR;
   2510 			}
   2511 			free(buf);
   2512 		}
   2513 		| dynaddr
   2514 		| dynaddr '/' number		{
   2515 			struct node_host	*n;
   2516 
   2517 			$$ = $1;
   2518 			for (n = $1; n != NULL; n = n->next)
   2519 				set_ipmask(n, $3);
   2520 		}
   2521 		| '<' STRING '>'	{
   2522 			if (strlen($2) >= PF_TABLE_NAME_SIZE) {
   2523 				yyerror("table name '%s' too long", $2);
   2524 				free($2);
   2525 				YYERROR;
   2526 			}
   2527 			$$ = calloc(1, sizeof(struct node_host));
   2528 			if ($$ == NULL)
   2529 				err(1, "host: calloc");
   2530 			$$->addr.type = PF_ADDR_TABLE;
   2531 			if (strlcpy($$->addr.v.tblname, $2,
   2532 			    sizeof($$->addr.v.tblname)) >=
   2533 			    sizeof($$->addr.v.tblname))
   2534 				errx(1, "host: strlcpy");
   2535 			free($2);
   2536 			$$->next = NULL;
   2537 			$$->tail = $$;
   2538 		}
   2539 		| ROUTE	STRING		{
   2540 			$$ = calloc(1, sizeof(struct node_host));
   2541 			if ($$ == NULL) {
   2542 				free($2);
   2543 				err(1, "host: calloc");
   2544 			}
   2545 			$$->addr.type = PF_ADDR_RTLABEL;
   2546 			if (strlcpy($$->addr.v.rtlabelname, $2,
   2547 			    sizeof($$->addr.v.rtlabelname)) >=
   2548 			    sizeof($$->addr.v.rtlabelname)) {
   2549 				yyerror("route label too long, max %u chars",
   2550 				    sizeof($$->addr.v.rtlabelname) - 1);
   2551 				free($2);
   2552 				free($$);
   2553 				YYERROR;
   2554 			}
   2555 			$$->next = NULL;
   2556 			$$->tail = $$;
   2557 			free($2);
   2558 		}
   2559 		;
   2560 
   2561 number		: STRING			{
   2562 			u_long	ulval;
   2563 
   2564 			if (atoul($1, &ulval) == -1) {
   2565 				yyerror("%s is not a number", $1);
   2566 				free($1);
   2567 				YYERROR;
   2568 			} else
   2569 				$$ = ulval;
   2570 			free($1);
   2571 		}
   2572 		;
   2573 
   2574 dynaddr		: '(' STRING ')'		{
   2575 			int	 flags = 0;
   2576 			char	*p, *op;
   2577 
   2578 			op = $2;
   2579 			if (!isalpha(op[0])) {
   2580 				yyerror("invalid interface name '%s'", op);
   2581 				free(op);
   2582 				YYERROR;
   2583 			}
   2584 			while ((p = strrchr($2, ':')) != NULL) {
   2585 				if (!strcmp(p+1, "network"))
   2586 					flags |= PFI_AFLAG_NETWORK;
   2587 				else if (!strcmp(p+1, "broadcast"))
   2588 					flags |= PFI_AFLAG_BROADCAST;
   2589 				else if (!strcmp(p+1, "peer"))
   2590 					flags |= PFI_AFLAG_PEER;
   2591 				else if (!strcmp(p+1, "0"))
   2592 					flags |= PFI_AFLAG_NOALIAS;
   2593 				else {
   2594 					yyerror("interface %s has bad modifier",
   2595 					    $2);
   2596 					free(op);
   2597 					YYERROR;
   2598 				}
   2599 				*p = '\0';
   2600 			}
   2601 			if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
   2602 				free(op);
   2603 				yyerror("illegal combination of "
   2604 				    "interface modifiers");
   2605 				YYERROR;
   2606 			}
   2607 			$$ = calloc(1, sizeof(struct node_host));
   2608 			if ($$ == NULL)
   2609 				err(1, "address: calloc");
   2610 			$$->af = 0;
   2611 			set_ipmask($$, 128);
   2612 			$$->addr.type = PF_ADDR_DYNIFTL;
   2613 			$$->addr.iflags = flags;
   2614 			if (strlcpy($$->addr.v.ifname, $2,
   2615 			    sizeof($$->addr.v.ifname)) >=
   2616 			    sizeof($$->addr.v.ifname)) {
   2617 				free(op);
   2618 				free($$);
   2619 				yyerror("interface name too long");
   2620 				YYERROR;
   2621 			}
   2622 			free(op);
   2623 			$$->next = NULL;
   2624 			$$->tail = $$;
   2625 		}
   2626 		;
   2627 
   2628 portspec	: port_item			{ $$ = $1; }
   2629 		| '{' port_list '}'		{ $$ = $2; }
   2630 		;
   2631 
   2632 port_list	: port_item			{ $$ = $1; }
   2633 		| port_list comma port_item	{
   2634 			$1->tail->next = $3;
   2635 			$1->tail = $3;
   2636 			$$ = $1;
   2637 		}
   2638 		;
   2639 
   2640 port_item	: port				{
   2641 			$$ = calloc(1, sizeof(struct node_port));
   2642 			if ($$ == NULL)
   2643 				err(1, "port_item: calloc");
   2644 			$$->port[0] = $1.a;
   2645 			$$->port[1] = $1.b;
   2646 			if ($1.t)
   2647 				$$->op = PF_OP_RRG;
   2648 			else
   2649 				$$->op = PF_OP_EQ;
   2650 			$$->next = NULL;
   2651 			$$->tail = $$;
   2652 		}
   2653 		| unaryop port		{
   2654 			if ($2.t) {
   2655 				yyerror("':' cannot be used with an other "
   2656 				    "port operator");
   2657 				YYERROR;
   2658 			}
   2659 			$$ = calloc(1, sizeof(struct node_port));
   2660 			if ($$ == NULL)
   2661 				err(1, "port_item: calloc");
   2662 			$$->port[0] = $2.a;
   2663 			$$->port[1] = $2.b;
   2664 			$$->op = $1;
   2665 			$$->next = NULL;
   2666 			$$->tail = $$;
   2667 		}
   2668 		| port PORTBINARY port		{
   2669 			if ($1.t || $3.t) {
   2670 				yyerror("':' cannot be used with an other "
   2671 				    "port operator");
   2672 				YYERROR;
   2673 			}
   2674 			$$ = calloc(1, sizeof(struct node_port));
   2675 			if ($$ == NULL)
   2676 				err(1, "port_item: calloc");
   2677 			$$->port[0] = $1.a;
   2678 			$$->port[1] = $3.a;
   2679 			$$->op = $2;
   2680 			$$->next = NULL;
   2681 			$$->tail = $$;
   2682 		}
   2683 		;
   2684 
   2685 port		: STRING			{
   2686 			char	*p = strchr($1, ':');
   2687 
   2688 			if (p == NULL) {
   2689 				if (($$.a = getservice($1)) == -1) {
   2690 					free($1);
   2691 					YYERROR;
   2692 				}
   2693 				$$.b = $$.t = 0;
   2694 			} else {
   2695 				int port[2];
   2696 
   2697 				*p++ = 0;
   2698 				if ((port[0] = getservice($1)) == -1 ||
   2699 				    (port[1] = getservice(p)) == -1) {
   2700 					free($1);
   2701 					YYERROR;
   2702 				}
   2703 				$$.a = port[0];
   2704 				$$.b = port[1];
   2705 				$$.t = PF_OP_RRG;
   2706 			}
   2707 			free($1);
   2708 		}
   2709 		;
   2710 
   2711 uids		: uid_item			{ $$ = $1; }
   2712 		| '{' uid_list '}'		{ $$ = $2; }
   2713 		;
   2714 
   2715 uid_list	: uid_item			{ $$ = $1; }
   2716 		| uid_list comma uid_item	{
   2717 			$1->tail->next = $3;
   2718 			$1->tail = $3;
   2719 			$$ = $1;
   2720 		}
   2721 		;
   2722 
   2723 uid_item	: uid				{
   2724 			$$ = calloc(1, sizeof(struct node_uid));
   2725 			if ($$ == NULL)
   2726 				err(1, "uid_item: calloc");
   2727 			$$->uid[0] = $1;
   2728 			$$->uid[1] = $1;
   2729 			$$->op = PF_OP_EQ;
   2730 			$$->next = NULL;
   2731 			$$->tail = $$;
   2732 		}
   2733 		| unaryop uid			{
   2734 			if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
   2735 				yyerror("user unknown requires operator = or "
   2736 				    "!=");
   2737 				YYERROR;
   2738 			}
   2739 			$$ = calloc(1, sizeof(struct node_uid));
   2740 			if ($$ == NULL)
   2741 				err(1, "uid_item: calloc");
   2742 			$$->uid[0] = $2;
   2743 			$$->uid[1] = $2;
   2744 			$$->op = $1;
   2745 			$$->next = NULL;
   2746 			$$->tail = $$;
   2747 		}
   2748 		| uid PORTBINARY uid		{
   2749 			if ($1 == UID_MAX || $3 == UID_MAX) {
   2750 				yyerror("user unknown requires operator = or "
   2751 				    "!=");
   2752 				YYERROR;
   2753 			}
   2754 			$$ = calloc(1, sizeof(struct node_uid));
   2755 			if ($$ == NULL)
   2756 				err(1, "uid_item: calloc");
   2757 			$$->uid[0] = $1;
   2758 			$$->uid[1] = $3;
   2759 			$$->op = $2;
   2760 			$$->next = NULL;
   2761 			$$->tail = $$;
   2762 		}
   2763 		;
   2764 
   2765 uid		: STRING			{
   2766 			u_long	ulval;
   2767 
   2768 			if (atoul($1, &ulval) == -1) {
   2769 				if (!strcmp($1, "unknown"))
   2770 					$$ = UID_MAX;
   2771 				else {
   2772 					struct passwd	*pw;
   2773 
   2774 					if ((pw = getpwnam($1)) == NULL) {
   2775 						yyerror("unknown user %s", $1);
   2776 						free($1);
   2777 						YYERROR;
   2778 					}
   2779 					$$ = pw->pw_uid;
   2780 				}
   2781 			} else {
   2782 				if (ulval >= UID_MAX) {
   2783 					free($1);
   2784 					yyerror("illegal uid value %lu", ulval);
   2785 					YYERROR;
   2786 				}
   2787 				$$ = ulval;
   2788 			}
   2789 			free($1);
   2790 		}
   2791 		;
   2792 
   2793 gids		: gid_item			{ $$ = $1; }
   2794 		| '{' gid_list '}'		{ $$ = $2; }
   2795 		;
   2796 
   2797 gid_list	: gid_item			{ $$ = $1; }
   2798 		| gid_list comma gid_item	{
   2799 			$1->tail->next = $3;
   2800 			$1->tail = $3;
   2801 			$$ = $1;
   2802 		}
   2803 		;
   2804 
   2805 gid_item	: gid				{
   2806 			$$ = calloc(1, sizeof(struct node_gid));
   2807 			if ($$ == NULL)
   2808 				err(1, "gid_item: calloc");
   2809 			$$->gid[0] = $1;
   2810 			$$->gid[1] = $1;
   2811 			$$->op = PF_OP_EQ;
   2812 			$$->next = NULL;
   2813 			$$->tail = $$;
   2814 		}
   2815 		| unaryop gid			{
   2816 			if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
   2817 				yyerror("group unknown requires operator = or "
   2818 				    "!=");
   2819 				YYERROR;
   2820 			}
   2821 			$$ = calloc(1, sizeof(struct node_gid));
   2822 			if ($$ == NULL)
   2823 				err(1, "gid_item: calloc");
   2824 			$$->gid[0] = $2;
   2825 			$$->gid[1] = $2;
   2826 			$$->op = $1;
   2827 			$$->next = NULL;
   2828 			$$->tail = $$;
   2829 		}
   2830 		| gid PORTBINARY gid		{
   2831 			if ($1 == GID_MAX || $3 == GID_MAX) {
   2832 				yyerror("group unknown requires operator = or "
   2833 				    "!=");
   2834 				YYERROR;
   2835 			}
   2836 			$$ = calloc(1, sizeof(struct node_gid));
   2837 			if ($$ == NULL)
   2838 				err(1, "gid_item: calloc");
   2839 			$$->gid[0] = $1;
   2840 			$$->gid[1] = $3;
   2841 			$$->op = $2;
   2842 			$$->next = NULL;
   2843 			$$->tail = $$;
   2844 		}
   2845 		;
   2846 
   2847 gid		: STRING			{
   2848 			u_long	ulval;
   2849 
   2850 			if (atoul($1, &ulval) == -1) {
   2851 				if (!strcmp($1, "unknown"))
   2852 					$$ = GID_MAX;
   2853 				else {
   2854 					struct group	*grp;
   2855 
   2856 					if ((grp = getgrnam($1)) == NULL) {
   2857 						yyerror("unknown group %s", $1);
   2858 						free($1);
   2859 						YYERROR;
   2860 					}
   2861 					$$ = grp->gr_gid;
   2862 				}
   2863 			} else {
   2864 				if (ulval >= GID_MAX) {
   2865 					yyerror("illegal gid value %lu", ulval);
   2866 					free($1);
   2867 					YYERROR;
   2868 				}
   2869 				$$ = ulval;
   2870 			}
   2871 			free($1);
   2872 		}
   2873 		;
   2874 
   2875 flag		: STRING			{
   2876 			int	f;
   2877 
   2878 			if ((f = parse_flags($1)) < 0) {
   2879 				yyerror("bad flags %s", $1);
   2880 				free($1);
   2881 				YYERROR;
   2882 			}
   2883 			free($1);
   2884 			$$.b1 = f;
   2885 		}
   2886 		;
   2887 
   2888 flags		: FLAGS flag '/' flag	{ $$.b1 = $2.b1; $$.b2 = $4.b1; }
   2889 		| FLAGS '/' flag	{ $$.b1 = 0; $$.b2 = $3.b1; }
   2890 		| FLAGS ANY		{ $$.b1 = 0; $$.b2 = 0; }
   2891 		;
   2892 
   2893 icmpspec	: ICMPTYPE icmp_item		{ $$ = $2; }
   2894 		| ICMPTYPE '{' icmp_list '}'	{ $$ = $3; }
   2895 		| ICMP6TYPE icmp6_item		{ $$ = $2; }
   2896 		| ICMP6TYPE '{' icmp6_list '}'	{ $$ = $3; }
   2897 		;
   2898 
   2899 icmp_list	: icmp_item			{ $$ = $1; }
   2900 		| icmp_list comma icmp_item	{
   2901 			$1->tail->next = $3;
   2902 			$1->tail = $3;
   2903 			$$ = $1;
   2904 		}
   2905 		;
   2906 
   2907 icmp6_list	: icmp6_item			{ $$ = $1; }
   2908 		| icmp6_list comma icmp6_item	{
   2909 			$1->tail->next = $3;
   2910 			$1->tail = $3;
   2911 			$$ = $1;
   2912 		}
   2913 		;
   2914 
   2915 icmp_item	: icmptype		{
   2916 			$$ = calloc(1, sizeof(struct node_icmp));
   2917 			if ($$ == NULL)
   2918 				err(1, "icmp_item: calloc");
   2919 			$$->type = $1;
   2920 			$$->code = 0;
   2921 			$$->proto = IPPROTO_ICMP;
   2922 			$$->next = NULL;
   2923 			$$->tail = $$;
   2924 		}
   2925 		| icmptype CODE STRING	{
   2926 			const struct icmpcodeent	*p;
   2927 			u_long				 ulval;
   2928 
   2929 			if (atoul($3, &ulval) == 0) {
   2930 				if (ulval > 255) {
   2931 					free($3);
   2932 					yyerror("illegal icmp-code %lu", ulval);
   2933 					YYERROR;
   2934 				}
   2935 			} else {
   2936 				if ((p = geticmpcodebyname($1-1, $3,
   2937 				    AF_INET)) == NULL) {
   2938 					yyerror("unknown icmp-code %s", $3);
   2939 					free($3);
   2940 					YYERROR;
   2941 				}
   2942 				ulval = p->code;
   2943 			}
   2944 			free($3);
   2945 			$$ = calloc(1, sizeof(struct node_icmp));
   2946 			if ($$ == NULL)
   2947 				err(1, "icmp_item: calloc");
   2948 			$$->type = $1;
   2949 			$$->code = ulval + 1;
   2950 			$$->proto = IPPROTO_ICMP;
   2951 			$$->next = NULL;
   2952 			$$->tail = $$;
   2953 		}
   2954 		;
   2955 
   2956 icmp6_item	: icmp6type		{
   2957 			$$ = calloc(1, sizeof(struct node_icmp));
   2958 			if ($$ == NULL)
   2959 				err(1, "icmp_item: calloc");
   2960 			$$->type = $1;
   2961 			$$->code = 0;
   2962 			$$->proto = IPPROTO_ICMPV6;
   2963 			$$->next = NULL;
   2964 			$$->tail = $$;
   2965 		}
   2966 		| icmp6type CODE STRING	{
   2967 			const struct icmpcodeent	*p;
   2968 			u_long				 ulval;
   2969 
   2970 			if (atoul($3, &ulval) == 0) {
   2971 				if (ulval > 255) {
   2972 					yyerror("illegal icmp6-code %lu",
   2973 					    ulval);
   2974 					free($3);
   2975 					YYERROR;
   2976 				}
   2977 			} else {
   2978 				if ((p = geticmpcodebyname($1-1, $3,
   2979 				    AF_INET6)) == NULL) {
   2980 					yyerror("unknown icmp6-code %s", $3);
   2981 					free($3);
   2982 					YYERROR;
   2983 				}
   2984 				ulval = p->code;
   2985 			}
   2986 			free($3);
   2987 			$$ = calloc(1, sizeof(struct node_icmp));
   2988 			if ($$ == NULL)
   2989 				err(1, "icmp_item: calloc");
   2990 			$$->type = $1;
   2991 			$$->code = ulval + 1;
   2992 			$$->proto = IPPROTO_ICMPV6;
   2993 			$$->next = NULL;
   2994 			$$->tail = $$;
   2995 		}
   2996 		;
   2997 
   2998 icmptype	: STRING			{
   2999 			const struct icmptypeent	*p;
   3000 			u_long				 ulval;
   3001 
   3002 			if (atoul($1, &ulval) == 0) {
   3003 				if (ulval > 255) {
   3004 					yyerror("illegal icmp-type %lu", ulval);
   3005 					free($1);
   3006 					YYERROR;
   3007 				}
   3008 				$$ = ulval + 1;
   3009 			} else {
   3010 				if ((p = geticmptypebyname($1, AF_INET)) ==
   3011 				    NULL) {
   3012 					yyerror("unknown icmp-type %s", $1);
   3013 					free($1);
   3014 					YYERROR;
   3015 				}
   3016 				$$ = p->type + 1;
   3017 			}
   3018 			free($1);
   3019 		}
   3020 		;
   3021 
   3022 icmp6type	: STRING			{
   3023 			const struct icmptypeent	*p;
   3024 			u_long				 ulval;
   3025 
   3026 			if (atoul($1, &ulval) == 0) {
   3027 				if (ulval > 255) {
   3028 					yyerror("illegal icmp6-type %lu",
   3029 					    ulval);
   3030 					free($1);
   3031 					YYERROR;
   3032 				}
   3033 				$$ = ulval + 1;
   3034 			} else {
   3035 				if ((p = geticmptypebyname($1, AF_INET6)) ==
   3036 				    NULL) {
   3037 					yyerror("unknown icmp6-type %s", $1);
   3038 					free($1);
   3039 					YYERROR;
   3040 				}
   3041 				$$ = p->type + 1;
   3042 			}
   3043 			free($1);
   3044 		}
   3045 		;
   3046 
   3047 tos		: TOS STRING			{
   3048 			if (!strcmp($2, "lowdelay"))
   3049 				$$ = IPTOS_LOWDELAY;
   3050 			else if (!strcmp($2, "throughput"))
   3051 				$$ = IPTOS_THROUGHPUT;
   3052 			else if (!strcmp($2, "reliability"))
   3053 				$$ = IPTOS_RELIABILITY;
   3054 			else if ($2[0] == '0' && $2[1] == 'x')
   3055 				$$ = strtoul($2, NULL, 16);
   3056 			else
   3057 				$$ = strtoul($2, NULL, 10);
   3058 			if (!$$ || $$ > 255) {
   3059 				yyerror("illegal tos value %s", $2);
   3060 				free($2);
   3061 				YYERROR;
   3062 			}
   3063 			free($2);
   3064 		}
   3065 		;
   3066 
   3067 sourcetrack	: SOURCETRACK		{ $$ = PF_SRCTRACK; }
   3068 		| SOURCETRACK GLOBAL	{ $$ = PF_SRCTRACK_GLOBAL; }
   3069 		| SOURCETRACK RULE	{ $$ = PF_SRCTRACK_RULE; }
   3070 		;
   3071 
   3072 statelock	: IFBOUND {
   3073 			$$ = PFRULE_IFBOUND;
   3074 		}
   3075 		| FLOATING {
   3076 			$$ = 0;
   3077 		}
   3078 		;
   3079 
   3080 keep		: NO STATE			{
   3081 			$$.action = 0;
   3082 			$$.options = NULL;
   3083 		}
   3084 		| KEEP STATE state_opt_spec	{
   3085 			$$.action = PF_STATE_NORMAL;
   3086 			$$.options = $3;
   3087 		}
   3088 		| MODULATE STATE state_opt_spec {
   3089 			$$.action = PF_STATE_MODULATE;
   3090 			$$.options = $3;
   3091 		}
   3092 		| SYNPROXY STATE state_opt_spec {
   3093 			$$.action = PF_STATE_SYNPROXY;
   3094 			$$.options = $3;
   3095 		}
   3096 		;
   3097 
   3098 flush		: /* empty */			{ $$ = 0; }
   3099 		| FLUSH				{ $$ = PF_FLUSH; }
   3100 		| FLUSH GLOBAL			{
   3101 			$$ = PF_FLUSH | PF_FLUSH_GLOBAL;
   3102 		}
   3103 		;
   3104 
   3105 state_opt_spec	: '(' state_opt_list ')'	{ $$ = $2; }
   3106 		| /* empty */			{ $$ = NULL; }
   3107 		;
   3108 
   3109 state_opt_list	: state_opt_item		{ $$ = $1; }
   3110 		| state_opt_list comma state_opt_item {
   3111 			$1->tail->next = $3;
   3112 			$1->tail = $3;
   3113 			$$ = $1;
   3114 		}
   3115 		;
   3116 
   3117 state_opt_item	: MAXIMUM number		{
   3118 			$$ = calloc(1, sizeof(struct node_state_opt));
   3119 			if ($$ == NULL)
   3120 				err(1, "state_opt_item: calloc");
   3121 			$$->type = PF_STATE_OPT_MAX;
   3122 			$$->data.max_states = $2;
   3123 			$$->next = NULL;
   3124 			$$->tail = $$;
   3125 		}
   3126 		| NOSYNC				{
   3127 			$$ = calloc(1, sizeof(struct node_state_opt));
   3128 			if ($$ == NULL)
   3129 				err(1, "state_opt_item: calloc");
   3130 			$$->type = PF_STATE_OPT_NOSYNC;
   3131 			$$->next = NULL;
   3132 			$$->tail = $$;
   3133 		}
   3134 		| MAXSRCSTATES number			{
   3135 			$$ = calloc(1, sizeof(struct node_state_opt));
   3136 			if ($$ == NULL)
   3137 				err(1, "state_opt_item: calloc");
   3138 			$$->type = PF_STATE_OPT_MAX_SRC_STATES;
   3139 			$$->data.max_src_states = $2;
   3140 			$$->next = NULL;
   3141 			$$->tail = $$;
   3142 		}
   3143 		| MAXSRCCONN number			{
   3144 			$$ = calloc(1, sizeof(struct node_state_opt));
   3145 			if ($$ == NULL)
   3146 				err(1, "state_opt_item: calloc");
   3147 			$$->type = PF_STATE_OPT_MAX_SRC_CONN;
   3148 			$$->data.max_src_conn = $2;
   3149 			$$->next = NULL;
   3150 			$$->tail = $$;
   3151 		}
   3152 		| MAXSRCCONNRATE number '/' number	{
   3153 			$$ = calloc(1, sizeof(struct node_state_opt));
   3154 			if ($$ == NULL)
   3155 				err(1, "state_opt_item: calloc");
   3156 			$$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
   3157 			$$->data.max_src_conn_rate.limit = $2;
   3158 			$$->data.max_src_conn_rate.seconds = $4;
   3159 			$$->next = NULL;
   3160 			$$->tail = $$;
   3161 		}
   3162 		| OVERLOAD '<' STRING '>' flush		{
   3163 			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
   3164 				yyerror("table name '%s' too long", $3);
   3165 				free($3);
   3166 				YYERROR;
   3167 			}
   3168 			$$ = calloc(1, sizeof(struct node_state_opt));
   3169 			if ($$ == NULL)
   3170 				err(1, "state_opt_item: calloc");
   3171 			if (strlcpy($$->data.overload.tblname, $3,
   3172 			    PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
   3173 				errx(1, "state_opt_item: strlcpy");
   3174 			free($3);
   3175 			$$->type = PF_STATE_OPT_OVERLOAD;
   3176 			$$->data.overload.flush = $5;
   3177 			$$->next = NULL;
   3178 			$$->tail = $$;
   3179 		}
   3180 		| MAXSRCNODES number			{
   3181 			$$ = calloc(1, sizeof(struct node_state_opt));
   3182 			if ($$ == NULL)
   3183 				err(1, "state_opt_item: calloc");
   3184 			$$->type = PF_STATE_OPT_MAX_SRC_NODES;
   3185 			$$->data.max_src_nodes = $2;
   3186 			$$->next = NULL;
   3187 			$$->tail = $$;
   3188 		}
   3189 		| sourcetrack {
   3190 			$$ = calloc(1, sizeof(struct node_state_opt));
   3191 			if ($$ == NULL)
   3192 				err(1, "state_opt_item: calloc");
   3193 			$$->type = PF_STATE_OPT_SRCTRACK;
   3194 			$$->data.src_track = $1;
   3195 			$$->next = NULL;
   3196 			$$->tail = $$;
   3197 		}
   3198 		| statelock {
   3199 			$$ = calloc(1, sizeof(struct node_state_opt));
   3200 			if ($$ == NULL)
   3201 				err(1, "state_opt_item: calloc");
   3202 			$$->type = PF_STATE_OPT_STATELOCK;
   3203 			$$->data.statelock = $1;
   3204 			$$->next = NULL;
   3205 			$$->tail = $$;
   3206 		}
   3207 		| STRING number			{
   3208 			int	i;
   3209 
   3210 			for (i = 0; pf_timeouts[i].name &&
   3211 			    strcmp(pf_timeouts[i].name, $1); ++i)
   3212 				;	/* nothing */
   3213 			if (!pf_timeouts[i].name) {
   3214 				yyerror("illegal timeout name %s", $1);
   3215 				free($1);
   3216 				YYERROR;
   3217 			}
   3218 			if (strchr(pf_timeouts[i].name, '.') == NULL) {
   3219 				yyerror("illegal state timeout %s", $1);
   3220 				free($1);
   3221 				YYERROR;
   3222 			}
   3223 			free($1);
   3224 			$$ = calloc(1, sizeof(struct node_state_opt));
   3225 			if ($$ == NULL)
   3226 				err(1, "state_opt_item: calloc");
   3227 			$$->type = PF_STATE_OPT_TIMEOUT;
   3228 			$$->data.timeout.number = pf_timeouts[i].timeout;
   3229 			$$->data.timeout.seconds = $2;
   3230 			$$->next = NULL;
   3231 			$$->tail = $$;
   3232 		}
   3233 		;
   3234 
   3235 label		: LABEL STRING			{
   3236 			$$ = $2;
   3237 		}
   3238 		;
   3239 
   3240 qname		: QUEUE STRING				{
   3241 			$$.qname = $2;
   3242 		}
   3243 		| QUEUE '(' STRING ')'			{
   3244 			$$.qname = $3;
   3245 		}
   3246 		| QUEUE '(' STRING comma STRING ')'	{
   3247 			$$.qname = $3;
   3248 			$$.pqname = $5;
   3249 		}
   3250 		;
   3251 
   3252 no		: /* empty */			{ $$ = 0; }
   3253 		| NO				{ $$ = 1; }
   3254 		;
   3255 
   3256 rport		: STRING			{
   3257 			char	*p = strchr($1, ':');
   3258 
   3259 			if (p == NULL) {
   3260 				if (($$.a = getservice($1)) == -1) {
   3261 					free($1);
   3262 					YYERROR;
   3263 				}
   3264 				$$.b = $$.t = 0;
   3265 			} else if (!strcmp(p+1, "*")) {
   3266 				*p = 0;
   3267 				if (($$.a = getservice($1)) == -1) {
   3268 					free($1);
   3269 					YYERROR;
   3270 				}
   3271 				$$.b = 0;
   3272 				$$.t = 1;
   3273 			} else {
   3274 				*p++ = 0;
   3275 				if (($$.a = getservice($1)) == -1 ||
   3276 				    ($$.b = getservice(p)) == -1) {
   3277 					free($1);
   3278 					YYERROR;
   3279 				}
   3280 				if ($$.a == $$.b)
   3281 					$$.b = 0;
   3282 				$$.t = 0;
   3283 			}
   3284 			free($1);
   3285 		}
   3286 		;
   3287 
   3288 redirspec	: host				{ $$ = $1; }
   3289 		| '{' redir_host_list '}'	{ $$ = $2; }
   3290 		;
   3291 
   3292 redir_host_list	: host				{ $$ = $1; }
   3293 		| redir_host_list comma host	{
   3294 			$1->tail->next = $3;
   3295 			$1->tail = $3->tail;
   3296 			$$ = $1;
   3297 		}
   3298 		;
   3299 
   3300 redirpool	: /* empty */			{ $$ = NULL; }
   3301 		| ARROW redirspec		{
   3302 			$$ = calloc(1, sizeof(struct redirection));
   3303 			if ($$ == NULL)
   3304 				err(1, "redirection: calloc");
   3305 			$$->host = $2;
   3306 			$$->rport.a = $$->rport.b = $$->rport.t = 0;
   3307 		}
   3308 		| ARROW redirspec PORT rport	{
   3309 			$$ = calloc(1, sizeof(struct redirection));
   3310 			if ($$ == NULL)
   3311 				err(1, "redirection: calloc");
   3312 			$$->host = $2;
   3313 			$$->rport = $4;
   3314 		}
   3315 		;
   3316 
   3317 hashkey		: /* empty */
   3318 		{
   3319 			$$ = calloc(1, sizeof(struct pf_poolhashkey));
   3320 			if ($$ == NULL)
   3321 				err(1, "hashkey: calloc");
   3322 			$$->key32[0] = arc4random();
   3323 			$$->key32[1] = arc4random();
   3324 			$$->key32[2] = arc4random();
   3325 			$$->key32[3] = arc4random();
   3326 		}
   3327 		| string
   3328 		{
   3329 			if (!strncmp($1, "0x", 2)) {
   3330 				if (strlen($1) != 34) {
   3331 					free($1);
   3332 					yyerror("hex key must be 128 bits "
   3333 						"(32 hex digits) long");
   3334 					YYERROR;
   3335 				}
   3336 				$$ = calloc(1, sizeof(struct pf_poolhashkey));
   3337 				if ($$ == NULL)
   3338 					err(1, "hashkey: calloc");
   3339 
   3340 				if (sscanf($1, "0x%8x%8x%8x%8x",
   3341 				    &$$->key32[0], &$$->key32[1],
   3342 				    &$$->key32[2], &$$->key32[3]) != 4) {
   3343 					free($$);
   3344 					free($1);
   3345 					yyerror("invalid hex key");
   3346 					YYERROR;
   3347 				}
   3348 			} else {
   3349 				MD5_CTX	context;
   3350 
   3351 				$$ = calloc(1, sizeof(struct pf_poolhashkey));
   3352 				if ($$ == NULL)
   3353 					err(1, "hashkey: calloc");
   3354 				MD5Init(&context);
   3355 				MD5Update(&context, (unsigned char *)$1,
   3356 				    strlen($1));
   3357 				MD5Final((unsigned char *)$$, &context);
   3358 				HTONL($$->key32[0]);
   3359 				HTONL($$->key32[1]);
   3360 				HTONL($$->key32[2]);
   3361 				HTONL($$->key32[3]);
   3362 			}
   3363 			free($1);
   3364 		}
   3365 		;
   3366 
   3367 pool_opts	:	{ bzero(&pool_opts, sizeof pool_opts); }
   3368 		    pool_opts_l
   3369 			{ $$ = pool_opts; }
   3370 		| /* empty */	{
   3371 			bzero(&pool_opts, sizeof pool_opts);
   3372 			$$ = pool_opts;
   3373 		}
   3374 		;
   3375 
   3376 pool_opts_l	: pool_opts_l pool_opt
   3377 		| pool_opt
   3378 		;
   3379 
   3380 pool_opt	: BITMASK	{
   3381 			if (pool_opts.type) {
   3382 				yyerror("pool type cannot be redefined");
   3383 				YYERROR;
   3384 			}
   3385 			pool_opts.type =  PF_POOL_BITMASK;
   3386 		}
   3387 		| RANDOM	{
   3388 			if (pool_opts.type) {
   3389 				yyerror("pool type cannot be redefined");
   3390 				YYERROR;
   3391 			}
   3392 			pool_opts.type = PF_POOL_RANDOM;
   3393 		}
   3394 		| SOURCEHASH hashkey {
   3395 			if (pool_opts.type) {
   3396 				yyerror("pool type cannot be redefined");
   3397 				YYERROR;
   3398 			}
   3399 			pool_opts.type = PF_POOL_SRCHASH;
   3400 			pool_opts.key = $2;
   3401 		}
   3402 		| ROUNDROBIN	{
   3403 			if (pool_opts.type) {
   3404 				yyerror("pool type cannot be redefined");
   3405 				YYERROR;
   3406 			}
   3407 			pool_opts.type = PF_POOL_ROUNDROBIN;
   3408 		}
   3409 		| STATICPORT	{
   3410 			if (pool_opts.staticport) {
   3411 				yyerror("static-port cannot be redefined");
   3412 				YYERROR;
   3413 			}
   3414 			pool_opts.staticport = 1;
   3415 		}
   3416 		| STICKYADDRESS	{
   3417 			if (filter_opts.marker & POM_STICKYADDRESS) {
   3418 				yyerror("sticky-address cannot be redefined");
   3419 				YYERROR;
   3420 			}
   3421 			pool_opts.marker |= POM_STICKYADDRESS;
   3422 			pool_opts.opts |= PF_POOL_STICKYADDR;
   3423 		}
   3424 		;
   3425 
   3426 redirection	: /* empty */			{ $$ = NULL; }
   3427 		| ARROW host			{
   3428 			$$ = calloc(1, sizeof(struct redirection));
   3429 			if ($$ == NULL)
   3430 				err(1, "redirection: calloc");
   3431 			$$->host = $2;
   3432 			$$->rport.a = $$->rport.b = $$->rport.t = 0;
   3433 		}
   3434 		| ARROW host PORT rport	{
   3435 			$$ = calloc(1, sizeof(struct redirection));
   3436 			if ($$ == NULL)
   3437 				err(1, "redirection: calloc");
   3438 			$$->host = $2;
   3439 			$$->rport = $4;
   3440 		}
   3441 		;
   3442 
   3443 natpasslog	: /* empty */	{ $$.b1 = $$.b2 = 0; $$.w2 = 0; }
   3444 		| PASS		{ $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
   3445 		| PASS log	{ $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
   3446 		| log		{ $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
   3447 		;
   3448 
   3449 nataction	: no NAT natpasslog {
   3450 			if ($1 && $3.b1) {
   3451 				yyerror("\"pass\" not valid with \"no\"");
   3452 				YYERROR;
   3453 			}
   3454 			if ($1)
   3455 				$$.b1 = PF_NONAT;
   3456 			else
   3457 				$$.b1 = PF_NAT;
   3458 			$$.b2 = $3.b1;
   3459 			$$.w = $3.b2;
   3460 			$$.w2 = $3.w2;
   3461 		}
   3462 		| no RDR natpasslog {
   3463 			if ($1 && $3.b1) {
   3464 				yyerror("\"pass\" not valid with \"no\"");
   3465 				YYERROR;
   3466 			}
   3467 			if ($1)
   3468 				$$.b1 = PF_NORDR;
   3469 			else
   3470 				$$.b1 = PF_RDR;
   3471 			$$.b2 = $3.b1;
   3472 			$$.w = $3.b2;
   3473 			$$.w2 = $3.w2;
   3474 		}
   3475 		;
   3476 
   3477 natrule		: nataction interface af proto fromto tag tagged rtable
   3478 		    redirpool pool_opts
   3479 		{
   3480 			struct pf_rule	r;
   3481 
   3482 			if (check_rulestate(PFCTL_STATE_NAT))
   3483 				YYERROR;
   3484 
   3485 			memset(&r, 0, sizeof(r));
   3486 
   3487 			r.action = $1.b1;
   3488 			r.natpass = $1.b2;
   3489 			r.log = $1.w;
   3490 			r.logif = $1.w2;
   3491 			r.af = $3;
   3492 
   3493 			if (!r.af) {
   3494 				if ($5.src.host && $5.src.host->af &&
   3495 				    !$5.src.host->ifindex)
   3496 					r.af = $5.src.host->af;
   3497 				else if ($5.dst.host && $5.dst.host->af &&
   3498 				    !$5.dst.host->ifindex)
   3499 					r.af = $5.dst.host->af;
   3500 			}
   3501 
   3502 			if ($6 != NULL)
   3503 				if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
   3504 				    PF_TAG_NAME_SIZE) {
   3505 					yyerror("tag too long, max %u chars",
   3506 					    PF_TAG_NAME_SIZE - 1);
   3507 					YYERROR;
   3508 				}
   3509 
   3510 			if ($7.name)
   3511 				if (strlcpy(r.match_tagname, $7.name,
   3512 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
   3513 					yyerror("tag too long, max %u chars",
   3514 					    PF_TAG_NAME_SIZE - 1);
   3515 					YYERROR;
   3516 				}
   3517 			r.match_tag_not = $7.neg;
   3518 			r.rtableid = $8;
   3519 
   3520 			if (r.action == PF_NONAT || r.action == PF_NORDR) {
   3521 				if ($9 != NULL) {
   3522 					yyerror("translation rule with 'no' "
   3523 					    "does not need '->'");
   3524 					YYERROR;
   3525 				}
   3526 			} else {
   3527 				if ($9 == NULL || $9->host == NULL) {
   3528 					yyerror("translation rule requires '-> "
   3529 					    "address'");
   3530 					YYERROR;
   3531 				}
   3532 				if (!r.af && ! $9->host->ifindex)
   3533 					r.af = $9->host->af;
   3534 
   3535 				remove_invalid_hosts(&$9->host, &r.af);
   3536 				if (invalid_redirect($9->host, r.af))
   3537 					YYERROR;
   3538 				if (check_netmask($9->host, r.af))
   3539 					YYERROR;
   3540 
   3541 				r.rpool.proxy_port[0] = ntohs($9->rport.a);
   3542 
   3543 				switch (r.action) {
   3544 				case PF_RDR:
   3545 					if (!$9->rport.b && $9->rport.t &&
   3546 					    $5.dst.port != NULL) {
   3547 						r.rpool.proxy_port[1] =
   3548 						    ntohs($9->rport.a) +
   3549 						    (ntohs(
   3550 						    $5.dst.port->port[1]) -
   3551 						    ntohs(
   3552 						    $5.dst.port->port[0]));
   3553 					} else
   3554 						r.rpool.proxy_port[1] =
   3555 						    ntohs($9->rport.b);
   3556 					break;
   3557 				case PF_NAT:
   3558 					r.rpool.proxy_port[1] =
   3559 					    ntohs($9->rport.b);
   3560 					if (!r.rpool.proxy_port[0] &&
   3561 					    !r.rpool.proxy_port[1]) {
   3562 						r.rpool.proxy_port[0] =
   3563 						    PF_NAT_PROXY_PORT_LOW;
   3564 						r.rpool.proxy_port[1] =
   3565 						    PF_NAT_PROXY_PORT_HIGH;
   3566 					} else if (!r.rpool.proxy_port[1])
   3567 						r.rpool.proxy_port[1] =
   3568 						    r.rpool.proxy_port[0];
   3569 					break;
   3570 				default:
   3571 					break;
   3572 				}
   3573 
   3574 				r.rpool.opts = $10.type;
   3575 				if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
   3576 				    PF_POOL_NONE && ($9->host->next != NULL ||
   3577 				    $9->host->addr.type == PF_ADDR_TABLE ||
   3578 				    DYNIF_MULTIADDR($9->host->addr)))
   3579 					r.rpool.opts = PF_POOL_ROUNDROBIN;
   3580 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
   3581 				    PF_POOL_ROUNDROBIN &&
   3582 				    disallow_table($9->host, "tables are only "
   3583 				    "supported in round-robin redirection "
   3584 				    "pools"))
   3585 					YYERROR;
   3586 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
   3587 				    PF_POOL_ROUNDROBIN &&
   3588 				    disallow_alias($9->host, "interface (%s) "
   3589 				    "is only supported in round-robin "
   3590 				    "redirection pools"))
   3591 					YYERROR;
   3592 				if ($9->host->next != NULL) {
   3593 					if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
   3594 					    PF_POOL_ROUNDROBIN) {
   3595 						yyerror("only round-robin "
   3596 						    "valid for multiple "
   3597 						    "redirection addresses");
   3598 						YYERROR;
   3599 					}
   3600 				}
   3601 			}
   3602 
   3603 			if ($10.key != NULL)
   3604 				memcpy(&r.rpool.key, $10.key,
   3605 				    sizeof(struct pf_poolhashkey));
   3606 
   3607 			 if ($10.opts)
   3608 				r.rpool.opts |= $10.opts;
   3609 
   3610 			if ($10.staticport) {
   3611 				if (r.action != PF_NAT) {
   3612 					yyerror("the 'static-port' option is "
   3613 					    "only valid with nat rules");
   3614 					YYERROR;
   3615 				}
   3616 				if (r.rpool.proxy_port[0] !=
   3617 				    PF_NAT_PROXY_PORT_LOW &&
   3618 				    r.rpool.proxy_port[1] !=
   3619 				    PF_NAT_PROXY_PORT_HIGH) {
   3620 					yyerror("the 'static-port' option can't"
   3621 					    " be used when specifying a port"
   3622 					    " range");
   3623 					YYERROR;
   3624 				}
   3625 				r.rpool.proxy_port[0] = 0;
   3626 				r.rpool.proxy_port[1] = 0;
   3627 			}
   3628 
   3629 			expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, $4,
   3630 			    $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
   3631 			    $5.dst.port, 0, 0, 0, "");
   3632 			free($9);
   3633 		}
   3634 		;
   3635 
   3636 binatrule	: no BINAT natpasslog interface af proto FROM host TO ipspec tag
   3637 		    tagged rtable redirection
   3638 		{
   3639 			struct pf_rule		binat;
   3640 			struct pf_pooladdr	*pa;
   3641 
   3642 			if (check_rulestate(PFCTL_STATE_NAT))
   3643 				YYERROR;
   3644 			if (disallow_urpf_failed($10, "\"urpf-failed\" is not "
   3645 			    "permitted as a binat destination"))
   3646 				YYERROR;
   3647 
   3648 			memset(&binat, 0, sizeof(binat));
   3649 
   3650 			if ($1 && $3.b1) {
   3651 				yyerror("\"pass\" not valid with \"no\"");
   3652 				YYERROR;
   3653 			}
   3654 			if ($1)
   3655 				binat.action = PF_NOBINAT;
   3656 			else
   3657 				binat.action = PF_BINAT;
   3658 			binat.natpass = $3.b1;
   3659 			binat.log = $3.b2;
   3660 			binat.logif = $3.w2;
   3661 			binat.af = $5;
   3662 			if (!binat.af && $8 != NULL && $8->af)
   3663 				binat.af = $8->af;
   3664 			if (!binat.af && $10 != NULL && $10->af)
   3665 				binat.af = $10->af;
   3666 
   3667 			if (!binat.af && $14 != NULL && $14->host)
   3668 				binat.af = $14->host->af;
   3669 			if (!binat.af) {
   3670 				yyerror("address family (inet/inet6) "
   3671 				    "undefined");
   3672 				YYERROR;
   3673 			}
   3674 
   3675 			if ($4 != NULL) {
   3676 				memcpy(binat.ifname, $4->ifname,
   3677 				    sizeof(binat.ifname));
   3678 				binat.ifnot = $4->not;
   3679 				free($4);
   3680 			}
   3681 
   3682 			if ($11 != NULL)
   3683 				if (strlcpy(binat.tagname, $11,
   3684 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
   3685 					yyerror("tag too long, max %u chars",
   3686 					    PF_TAG_NAME_SIZE - 1);
   3687 					YYERROR;
   3688 				}
   3689 			if ($12.name)
   3690 				if (strlcpy(binat.match_tagname, $12.name,
   3691 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
   3692 					yyerror("tag too long, max %u chars",
   3693 					    PF_TAG_NAME_SIZE - 1);
   3694 					YYERROR;
   3695 				}
   3696 			binat.match_tag_not = $12.neg;
   3697 			binat.rtableid = $13;
   3698 
   3699 			if ($6 != NULL) {
   3700 				binat.proto = $6->proto;
   3701 				free($6);
   3702 			}
   3703 
   3704 			if ($8 != NULL && disallow_table($8, "invalid use of "
   3705 			    "table <%s> as the source address of a binat rule"))
   3706 				YYERROR;
   3707 			if ($8 != NULL && disallow_alias($8, "invalid use of "
   3708 			    "interface (%s) as the source address of a binat "
   3709 			    "rule"))
   3710 				YYERROR;
   3711 			if ($14 != NULL && $14->host != NULL && disallow_table(
   3712 			    $14->host, "invalid use of table <%s> as the "
   3713 			    "redirect address of a binat rule"))
   3714 				YYERROR;
   3715 			if ($14 != NULL && $14->host != NULL && disallow_alias(
   3716 			    $14->host, "invalid use of interface (%s) as the "
   3717 			    "redirect address of a binat rule"))
   3718 				YYERROR;
   3719 
   3720 			if ($8 != NULL) {
   3721 				if ($8->next) {
   3722 					yyerror("multiple binat ip addresses");
   3723 					YYERROR;
   3724 				}
   3725 				if ($8->addr.type == PF_ADDR_DYNIFTL)
   3726 					$8->af = binat.af;
   3727 				if ($8->af != binat.af) {
   3728 					yyerror("binat ip versions must match");
   3729 					YYERROR;
   3730 				}
   3731 				if (check_netmask($8, binat.af))
   3732 					YYERROR;
   3733 				memcpy(&binat.src.addr, &$8->addr,
   3734 				    sizeof(binat.src.addr));
   3735 				free($8);
   3736 			}
   3737 			if ($10 != NULL) {
   3738 				if ($10->next) {
   3739 					yyerror("multiple binat ip addresses");
   3740 					YYERROR;
   3741 				}
   3742 				if ($10->af != binat.af && $10->af) {
   3743 					yyerror("binat ip versions must match");
   3744 					YYERROR;
   3745 				}
   3746 				if (check_netmask($10, binat.af))
   3747 					YYERROR;
   3748 				memcpy(&binat.dst.addr, &$10->addr,
   3749 				    sizeof(binat.dst.addr));
   3750 				binat.dst.neg = $10->not;
   3751 				free($10);
   3752 			}
   3753 
   3754 			if (binat.action == PF_NOBINAT) {
   3755 				if ($14 != NULL) {
   3756 					yyerror("'no binat' rule does not need"
   3757 					    " '->'");
   3758 					YYERROR;
   3759 				}
   3760 			} else {
   3761 				if ($14 == NULL || $14->host == NULL) {
   3762 					yyerror("'binat' rule requires"
   3763 					    " '-> address'");
   3764 					YYERROR;
   3765 				}
   3766 
   3767 				remove_invalid_hosts(&$14->host, &binat.af);
   3768 				if (invalid_redirect($14->host, binat.af))
   3769 					YYERROR;
   3770 				if ($14->host->next != NULL) {
   3771 					yyerror("binat rule must redirect to "
   3772 					    "a single address");
   3773 					YYERROR;
   3774 				}
   3775 				if (check_netmask($14->host, binat.af))
   3776 					YYERROR;
   3777 
   3778 				if (!PF_AZERO(&binat.src.addr.v.a.mask,
   3779 				    binat.af) &&
   3780 				    !PF_AEQ(&binat.src.addr.v.a.mask,
   3781 				    &$14->host->addr.v.a.mask, binat.af)) {
   3782 					yyerror("'binat' source mask and "
   3783 					    "redirect mask must be the same");
   3784 					YYERROR;
   3785 				}
   3786 
   3787 				TAILQ_INIT(&binat.rpool.list);
   3788 				pa = calloc(1, sizeof(struct pf_pooladdr));
   3789 				if (pa == NULL)
   3790 					err(1, "binat: calloc");
   3791 				pa->addr = $14->host->addr;
   3792 				pa->ifname[0] = 0;
   3793 				TAILQ_INSERT_TAIL(&binat.rpool.list,
   3794 				    pa, entries);
   3795 
   3796 				free($14);
   3797 			}
   3798 
   3799 			pfctl_add_rule(pf, &binat, "");
   3800 		}
   3801 		;
   3802 
   3803 tag		: /* empty */		{ $$ = NULL; }
   3804 		| TAG STRING		{ $$ = $2; }
   3805 		;
   3806 
   3807 tagged		: /* empty */		{ $$.neg = 0; $$.name = NULL; }
   3808 		| not TAGGED string	{ $$.neg = $1; $$.name = $3; }
   3809 		;
   3810 
   3811 rtable		: /* empty */		{ $$ = -1; }
   3812 		| RTABLE number		{
   3813 			if ($2 > RT_TABLEID_MAX || $2 < 0) {
   3814 				yyerror("invalid rtable id");
   3815 				YYERROR;
   3816 			}
   3817 			$$ = $2;
   3818 		}
   3819 		;
   3820 
   3821 route_host	: STRING			{
   3822 			$$ = calloc(1, sizeof(struct node_host));
   3823 			if ($$ == NULL)
   3824 				err(1, "route_host: calloc");
   3825 			$$->ifname = $1;
   3826 			set_ipmask($$, 128);
   3827 			$$->next = NULL;
   3828 			$$->tail = $$;
   3829 		}
   3830 		| '(' STRING host ')'		{
   3831 			$$ = $3;
   3832 			$$->ifname = $2;
   3833 		}
   3834 		;
   3835 
   3836 route_host_list	: route_host				{ $$ = $1; }
   3837 		| route_host_list comma route_host	{
   3838 			if ($1->af == 0)
   3839 				$1->af = $3->af;
   3840 			if ($1->af != $3->af) {
   3841 				yyerror("all pool addresses must be in the "
   3842 				    "same address family");
   3843 				YYERROR;
   3844 			}
   3845 			$1->tail->next = $3;
   3846 			$1->tail = $3->tail;
   3847 			$$ = $1;
   3848 		}
   3849 		;
   3850 
   3851 routespec	: route_host			{ $$ = $1; }
   3852 		| '{' route_host_list '}'	{ $$ = $2; }
   3853 		;
   3854 
   3855 route		: /* empty */			{
   3856 			$$.host = NULL;
   3857 			$$.rt = 0;
   3858 			$$.pool_opts = 0;
   3859 		}
   3860 		| FASTROUTE {
   3861 			$$.host = NULL;
   3862 			$$.rt = PF_FASTROUTE;
   3863 			$$.pool_opts = 0;
   3864 		}
   3865 		| ROUTETO routespec pool_opts {
   3866 			$$.host = $2;
   3867 			$$.rt = PF_ROUTETO;
   3868 			$$.pool_opts = $3.type | $3.opts;
   3869 			if ($3.key != NULL)
   3870 				$$.key = $3.key;
   3871 		}
   3872 		| REPLYTO routespec pool_opts {
   3873 			$$.host = $2;
   3874 			$$.rt = PF_REPLYTO;
   3875 			$$.pool_opts = $3.type | $3.opts;
   3876 			if ($3.key != NULL)
   3877 				$$.key = $3.key;
   3878 		}
   3879 		| DUPTO routespec pool_opts {
   3880 			$$.host = $2;
   3881 			$$.rt = PF_DUPTO;
   3882 			$$.pool_opts = $3.type | $3.opts;
   3883 			if ($3.key != NULL)
   3884 				$$.key = $3.key;
   3885 		}
   3886 		;
   3887 
   3888 timeout_spec	: STRING number
   3889 		{
   3890 			if (check_rulestate(PFCTL_STATE_OPTION)) {
   3891 				free($1);
   3892 				YYERROR;
   3893 			}
   3894 			if (pfctl_set_timeout(pf, $1, $2, 0) != 0) {
   3895 				yyerror("unknown timeout %s", $1);
   3896 				free($1);
   3897 				YYERROR;
   3898 			}
   3899 			free($1);
   3900 		}
   3901 		;
   3902 
   3903 timeout_list	: timeout_list comma timeout_spec
   3904 		| timeout_spec
   3905 		;
   3906 
   3907 limit_spec	: STRING number
   3908 		{
   3909 			if (check_rulestate(PFCTL_STATE_OPTION)) {
   3910 				free($1);
   3911 				YYERROR;
   3912 			}
   3913 			if (pfctl_set_limit(pf, $1, $2) != 0) {
   3914 				yyerror("unable to set limit %s %u", $1, $2);
   3915 				free($1);
   3916 				YYERROR;
   3917 			}
   3918 			free($1);
   3919 		}
   3920 		;
   3921 
   3922 limit_list	: limit_list comma limit_spec
   3923 		| limit_spec
   3924 		;
   3925 
   3926 comma		: ','
   3927 		| /* empty */
   3928 		;
   3929 
   3930 yesno		: NO			{ $$ = 0; }
   3931 		| STRING		{
   3932 			if (!strcmp($1, "yes"))
   3933 				$$ = 1;
   3934 			else {
   3935 				yyerror("invalid value '%s', expected 'yes' "
   3936 				    "or 'no'", $1);
   3937 				free($1);
   3938 				YYERROR;
   3939 			}
   3940 			free($1);
   3941 		}
   3942 		;
   3943 
   3944 unaryop		: '='		{ $$ = PF_OP_EQ; }
   3945 		| '!' '='	{ $$ = PF_OP_NE; }
   3946 		| '<' '='	{ $$ = PF_OP_LE; }
   3947 		| '<'		{ $$ = PF_OP_LT; }
   3948 		| '>' '='	{ $$ = PF_OP_GE; }
   3949 		| '>'		{ $$ = PF_OP_GT; }
   3950 		;
   3951 
   3952 %%
   3953 
   3954 int
   3955 yyerror(const char *fmt, ...)
   3956 {
   3957 	va_list		 ap;
   3958 	extern char	*infile;
   3959 
   3960 	errors = 1;
   3961 	va_start(ap, fmt);
   3962 	fprintf(stderr, "%s:%d: ", infile, yylval.lineno);
   3963 	vfprintf(stderr, fmt, ap);
   3964 	fprintf(stderr, "\n");
   3965 	va_end(ap);
   3966 	return (0);
   3967 }
   3968 
   3969 int
   3970 disallow_table(struct node_host *h, const char *fmt)
   3971 {
   3972 	for (; h != NULL; h = h->next)
   3973 		if (h->addr.type == PF_ADDR_TABLE) {
   3974 			yyerror(fmt, h->addr.v.tblname);
   3975 			return (1);
   3976 		}
   3977 	return (0);
   3978 }
   3979 
   3980 int
   3981 disallow_urpf_failed(struct node_host *h, const char *fmt)
   3982 {
   3983 	for (; h != NULL; h = h->next)
   3984 		if (h->addr.type == PF_ADDR_URPFFAILED) {
   3985 			yyerror(fmt);
   3986 			return (1);
   3987 		}
   3988 	return (0);
   3989 }
   3990 
   3991 int
   3992 disallow_alias(struct node_host *h, const char *fmt)
   3993 {
   3994 	for (; h != NULL; h = h->next)
   3995 		if (DYNIF_MULTIADDR(h->addr)) {
   3996 			yyerror(fmt, h->addr.v.tblname);
   3997 			return (1);
   3998 		}
   3999 	return (0);
   4000 }
   4001 
   4002 int
   4003 rule_consistent(struct pf_rule *r, int anchor_call)
   4004 {
   4005 	int	problems = 0;
   4006 
   4007 	switch (r->action) {
   4008 	case PF_PASS:
   4009 	case PF_DROP:
   4010 	case PF_SCRUB:
   4011 	case PF_NOSCRUB:
   4012 		problems = filter_consistent(r, anchor_call);
   4013 		break;
   4014 	case PF_NAT:
   4015 	case PF_NONAT:
   4016 		problems = nat_consistent(r);
   4017 		break;
   4018 	case PF_RDR:
   4019 	case PF_NORDR:
   4020 		problems = rdr_consistent(r);
   4021 		break;
   4022 	case PF_BINAT:
   4023 	case PF_NOBINAT:
   4024 	default:
   4025 		break;
   4026 	}
   4027 	return (problems);
   4028 }
   4029 
   4030 int
   4031 filter_consistent(struct pf_rule *r, int anchor_call)
   4032 {
   4033 	int	problems = 0;
   4034 
   4035 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
   4036 	    (r->src.port_op || r->dst.port_op)) {
   4037 		yyerror("port only applies to tcp/udp");
   4038 		problems++;
   4039 	}
   4040 	if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
   4041 	    (r->type || r->code)) {
   4042 		yyerror("icmp-type/code only applies to icmp");
   4043 		problems++;
   4044 	}
   4045 	if (!r->af && (r->type || r->code)) {
   4046 		yyerror("must indicate address family with icmp-type/code");
   4047 		problems++;
   4048 	}
   4049 	if (r->overload_tblname[0] &&
   4050 	    r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
   4051 		yyerror("'overload' requires 'max-src-conn' "
   4052 		    "or 'max-src-conn-rate'");
   4053 		problems++;
   4054 	}
   4055 	if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
   4056 	    (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
   4057 		yyerror("proto %s doesn't match address family %s",
   4058 		    r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
   4059 		    r->af == AF_INET ? "inet" : "inet6");
   4060 		problems++;
   4061 	}
   4062 	if (r->allow_opts && r->action != PF_PASS) {
   4063 		yyerror("allow-opts can only be specified for pass rules");
   4064 		problems++;
   4065 	}
   4066 	if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
   4067 	    r->dst.port_op || r->flagset || r->type || r->code)) {
   4068 		yyerror("fragments can be filtered only on IP header fields");
   4069 		problems++;
   4070 	}
   4071 	if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
   4072 		yyerror("return-rst can only be applied to TCP rules");
   4073 		problems++;
   4074 	}
   4075 	if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
   4076 		yyerror("max-src-nodes requires 'source-track rule'");
   4077 		problems++;
   4078 	}
   4079 	if (r->action == PF_DROP && r->keep_state) {
   4080 		yyerror("keep state on block rules doesn't make sense");
   4081 		problems++;
   4082 	}
   4083 	return (-problems);
   4084 }
   4085 
   4086 int
   4087 nat_consistent(struct pf_rule *r)
   4088 {
   4089 	return (0);	/* yeah! */
   4090 }
   4091 
   4092 int
   4093 rdr_consistent(struct pf_rule *r)
   4094 {
   4095 	int			 problems = 0;
   4096 
   4097 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP) {
   4098 		if (r->src.port_op) {
   4099 			yyerror("src port only applies to tcp/udp");
   4100 			problems++;
   4101 		}
   4102 		if (r->dst.port_op) {
   4103 			yyerror("dst port only applies to tcp/udp");
   4104 			problems++;
   4105 		}
   4106 		if (r->rpool.proxy_port[0]) {
   4107 			yyerror("rpool port only applies to tcp/udp");
   4108 			problems++;
   4109 		}
   4110 	}
   4111 	if (r->dst.port_op &&
   4112 	    r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
   4113 		yyerror("invalid port operator for rdr destination port");
   4114 		problems++;
   4115 	}
   4116 	return (-problems);
   4117 }
   4118 
   4119 int
   4120 process_tabledef(char *name, struct table_opts *opts)
   4121 {
   4122 	struct pfr_buffer	 ab;
   4123 	struct node_tinit	*ti;
   4124 
   4125 	bzero(&ab, sizeof(ab));
   4126 	ab.pfrb_type = PFRB_ADDRS;
   4127 	SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
   4128 		if (ti->file)
   4129 			if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
   4130 				if (errno)
   4131 					yyerror("cannot load \"%s\": %s",
   4132 					    ti->file, strerror(errno));
   4133 				else
   4134 					yyerror("file \"%s\" contains bad data",
   4135 					    ti->file);
   4136 				goto _error;
   4137 			}
   4138 		if (ti->host)
   4139 			if (append_addr_host(&ab, ti->host, 0, 0)) {
   4140 				yyerror("cannot create address buffer: %s",
   4141 				    strerror(errno));
   4142 				goto _error;
   4143 			}
   4144 	}
   4145 	if (pf->opts & PF_OPT_VERBOSE)
   4146 		print_tabledef(name, opts->flags, opts->init_addr,
   4147 		    &opts->init_nodes);
   4148 	if (!(pf->opts & PF_OPT_NOACTION) &&
   4149 	    pfctl_define_table(name, opts->flags, opts->init_addr,
   4150 	    pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
   4151 		yyerror("cannot define table %s: %s", name,
   4152 		    pfr_strerror(errno));
   4153 		goto _error;
   4154 	}
   4155 	pf->tdirty = 1;
   4156 	pfr_buf_clear(&ab);
   4157 	return (0);
   4158 _error:
   4159 	pfr_buf_clear(&ab);
   4160 	return (-1);
   4161 }
   4162 
   4163 struct keywords {
   4164 	const char	*k_name;
   4165 	int		 k_val;
   4166 };
   4167 
   4168 /* macro gore, but you should've seen the prior indentation nightmare... */
   4169 
   4170 #define FREE_LIST(T,r) \
   4171 	do { \
   4172 		T *p, *node = r; \
   4173 		while (node != NULL) { \
   4174 			p = node; \
   4175 			node = node->next; \
   4176 			free(p); \
   4177 		} \
   4178 	} while (0)
   4179 
   4180 #define LOOP_THROUGH(T,n,r,C) \
   4181 	do { \
   4182 		T *n; \
   4183 		if (r == NULL) { \
   4184 			r = calloc(1, sizeof(T)); \
   4185 			if (r == NULL) \
   4186 				err(1, "LOOP: calloc"); \
   4187 			r->next = NULL; \
   4188 		} \
   4189 		n = r; \
   4190 		while (n != NULL) { \
   4191 			do { \
   4192 				C; \
   4193 			} while (0); \
   4194 			n = n->next; \
   4195 		} \
   4196 	} while (0)
   4197 
   4198 void
   4199 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
   4200 {
   4201 	char *tmp;
   4202 	char *p, *q;
   4203 
   4204 	if ((tmp = calloc(1, len)) == NULL)
   4205 		err(1, "expand_label_str: calloc");
   4206 	p = q = label;
   4207 	while ((q = strstr(p, srch)) != NULL) {
   4208 		*q = '\0';
   4209 		if ((strlcat(tmp, p, len) >= len) ||
   4210 		    (strlcat(tmp, repl, len) >= len))
   4211 			errx(1, "expand_label: label too long");
   4212 		q += strlen(srch);
   4213 		p = q;
   4214 	}
   4215 	if (strlcat(tmp, p, len) >= len)
   4216 		errx(1, "expand_label: label too long");
   4217 	strlcpy(label, tmp, len);	/* always fits */
   4218 	free(tmp);
   4219 }
   4220 
   4221 void
   4222 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
   4223 {
   4224 	if (strstr(label, name) != NULL) {
   4225 		if (!*ifname)
   4226 			expand_label_str(label, len, name, "any");
   4227 		else
   4228 			expand_label_str(label, len, name, ifname);
   4229 	}
   4230 }
   4231 
   4232 void
   4233 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
   4234     struct node_host *h)
   4235 {
   4236 	char tmp[64], tmp_not[66];
   4237 
   4238 	if (strstr(label, name) != NULL) {
   4239 		switch (h->addr.type) {
   4240 		case PF_ADDR_DYNIFTL:
   4241 			snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname);
   4242 			break;
   4243 		case PF_ADDR_TABLE:
   4244 			snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname);
   4245 			break;
   4246 		case PF_ADDR_NOROUTE:
   4247 			snprintf(tmp, sizeof(tmp), "no-route");
   4248 			break;
   4249 		case PF_ADDR_URPFFAILED:
   4250 			snprintf(tmp, sizeof(tmp), "urpf-failed");
   4251 			break;
   4252 		case PF_ADDR_ADDRMASK:
   4253 			if (!af || (PF_AZERO(&h->addr.v.a.addr, af) &&
   4254 			    PF_AZERO(&h->addr.v.a.mask, af)))
   4255 				snprintf(tmp, sizeof(tmp), "any");
   4256 			else {
   4257 				char	a[48];
   4258 				int	bits;
   4259 
   4260 				if (inet_ntop(af, &h->addr.v.a.addr, a,
   4261 				    sizeof(a)) == NULL)
   4262 					snprintf(tmp, sizeof(tmp), "?");
   4263 				else {
   4264 					bits = unmask(&h->addr.v.a.mask, af);
   4265 					if ((af == AF_INET && bits < 32) ||
   4266 					    (af == AF_INET6 && bits < 128))
   4267 						snprintf(tmp, sizeof(tmp),
   4268 						    "%s/%d", a, bits);
   4269 					else
   4270 						snprintf(tmp, sizeof(tmp),
   4271 						    "%s", a);
   4272 				}
   4273 			}
   4274 			break;
   4275 		default:
   4276 			snprintf(tmp, sizeof(tmp), "?");
   4277 			break;
   4278 		}
   4279 
   4280 		if (h->not) {
   4281 			snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
   4282 			expand_label_str(label, len, name, tmp_not);
   4283 		} else
   4284 			expand_label_str(label, len, name, tmp);
   4285 	}
   4286 }
   4287 
   4288 void
   4289 expand_label_port(const char *name, char *label, size_t len,
   4290     struct node_port *port)
   4291 {
   4292 	char	 a1[6], a2[6], op[13] = "";
   4293 
   4294 	if (strstr(label, name) != NULL) {
   4295 		snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0]));
   4296 		snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1]));
   4297 		if (!port->op)
   4298 			;
   4299 		else if (port->op == PF_OP_IRG)
   4300 			snprintf(op, sizeof(op), "%s><%s", a1, a2);
   4301 		else if (port->op == PF_OP_XRG)
   4302 			snprintf(op, sizeof(op), "%s<>%s", a1, a2);
   4303 		else if (port->op == PF_OP_EQ)
   4304 			snprintf(op, sizeof(op), "%s", a1);
   4305 		else if (port->op == PF_OP_NE)
   4306 			snprintf(op, sizeof(op), "!=%s", a1);
   4307 		else if (port->op == PF_OP_LT)
   4308 			snprintf(op, sizeof(op), "<%s", a1);
   4309 		else if (port->op == PF_OP_LE)
   4310 			snprintf(op, sizeof(op), "<=%s", a1);
   4311 		else if (port->op == PF_OP_GT)
   4312 			snprintf(op, sizeof(op), ">%s", a1);
   4313 		else if (port->op == PF_OP_GE)
   4314 			snprintf(op, sizeof(op), ">=%s", a1);
   4315 		expand_label_str(label, len, name, op);
   4316 	}
   4317 }
   4318 
   4319 void
   4320 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
   4321 {
   4322 	struct protoent *pe;
   4323 	char n[4];
   4324 
   4325 	if (strstr(label, name) != NULL) {
   4326 		pe = getprotobynumber(proto);
   4327 		if (pe != NULL)
   4328 			expand_label_str(label, len, name, pe->p_name);
   4329 		else {
   4330 			snprintf(n, sizeof(n), "%u", proto);
   4331 			expand_label_str(label, len, name, n);
   4332 		}
   4333 	}
   4334 }
   4335 
   4336 void
   4337 expand_label_nr(const char *name, char *label, size_t len)
   4338 {
   4339 	char n[11];
   4340 
   4341 	if (strstr(label, name) != NULL) {
   4342 		snprintf(n, sizeof(n), "%u", pf->anchor->match);
   4343 		expand_label_str(label, len, name, n);
   4344 	}
   4345 }
   4346 
   4347 void
   4348 expand_label(char *label, size_t len, const char *ifname, sa_family_t af,
   4349     struct node_host *src_host, struct node_port *src_port,
   4350     struct node_host *dst_host, struct node_port *dst_port,
   4351     u_int8_t proto)
   4352 {
   4353 	expand_label_if("$if", label, len, ifname);
   4354 	expand_label_addr("$srcaddr", label, len, af, src_host);
   4355 	expand_label_addr("$dstaddr", label, len, af, dst_host);
   4356 	expand_label_port("$srcport", label, len, src_port);
   4357 	expand_label_port("$dstport", label, len, dst_port);
   4358 	expand_label_proto("$proto", label, len, proto);
   4359 	expand_label_nr("$nr", label, len);
   4360 }
   4361 
   4362 int
   4363 expand_altq(struct pf_altq *a, struct node_if *interfaces,
   4364     struct node_queue *nqueues, struct node_queue_bw bwspec,
   4365     struct node_queue_opt *opts)
   4366 {
   4367 	struct pf_altq		 pa, pb;
   4368 	char			 qname[PF_QNAME_SIZE];
   4369 	struct node_queue	*n;
   4370 	struct node_queue_bw	 bw;
   4371 	int			 errs = 0;
   4372 
   4373 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
   4374 		FREE_LIST(struct node_if, interfaces);
   4375 		FREE_LIST(struct node_queue, nqueues);
   4376 		return (0);
   4377 	}
   4378 
   4379 	LOOP_THROUGH(struct node_if, interface, interfaces,
   4380 		memcpy(&pa, a, sizeof(struct pf_altq));
   4381 		if (strlcpy(pa.ifname, interface->ifname,
   4382 		    sizeof(pa.ifname)) >= sizeof(pa.ifname))
   4383 			errx(1, "expand_altq: strlcpy");
   4384 
   4385 		if (interface->not) {
   4386 			yyerror("altq on ! <interface> is not supported");
   4387 			errs++;
   4388 		} else {
   4389 			if (eval_pfaltq(pf, &pa, &bwspec, opts))
   4390 				errs++;
   4391 			else
   4392 				if (pfctl_add_altq(pf, &pa))
   4393 					errs++;
   4394 
   4395 			if (pf->opts & PF_OPT_VERBOSE) {
   4396 				print_altq(&pf->paltq->altq, 0,
   4397 				    &bwspec, opts);
   4398 				if (nqueues && nqueues->tail) {
   4399 					printf("queue { ");
   4400 					LOOP_THROUGH(struct node_queue, queue,
   4401 					    nqueues,
   4402 						printf("%s ",
   4403 						    queue->queue);
   4404 					);
   4405 					printf("}");
   4406 				}
   4407 				printf("\n");
   4408 			}
   4409 
   4410 			if (pa.scheduler == ALTQT_CBQ ||
   4411 			    pa.scheduler == ALTQT_HFSC) {
   4412 				/* now create a root queue */
   4413 				memset(&pb, 0, sizeof(struct pf_altq));
   4414 				if (strlcpy(qname, "root_", sizeof(qname)) >=
   4415 				    sizeof(qname))
   4416 					errx(1, "expand_altq: strlcpy");
   4417 				if (strlcat(qname, interface->ifname,
   4418 				    sizeof(qname)) >= sizeof(qname))
   4419 					errx(1, "expand_altq: strlcat");
   4420 				if (strlcpy(pb.qname, qname,
   4421 				    sizeof(pb.qname)) >= sizeof(pb.qname))
   4422 					errx(1, "expand_altq: strlcpy");
   4423 				if (strlcpy(pb.ifname, interface->ifname,
   4424 				    sizeof(pb.ifname)) >= sizeof(pb.ifname))
   4425 					errx(1, "expand_altq: strlcpy");
   4426 				pb.qlimit = pa.qlimit;
   4427 				pb.scheduler = pa.scheduler;
   4428 				bw.bw_absolute = pa.ifbandwidth;
   4429 				bw.bw_percent = 0;
   4430 				if (eval_pfqueue(pf, &pb, &bw, opts))
   4431 					errs++;
   4432 				else
   4433 					if (pfctl_add_altq(pf, &pb))
   4434 						errs++;
   4435 			}
   4436 
   4437 			LOOP_THROUGH(struct node_queue, queue, nqueues,
   4438 				n = calloc(1, sizeof(struct node_queue));
   4439 				if (n == NULL)
   4440 					err(1, "expand_altq: calloc");
   4441 				if (pa.scheduler == ALTQT_CBQ ||
   4442 				    pa.scheduler == ALTQT_HFSC)
   4443 					if (strlcpy(n->parent, qname,
   4444 					    sizeof(n->parent)) >=
   4445 					    sizeof(n->parent))
   4446 						errx(1, "expand_altq: strlcpy");
   4447 				if (strlcpy(n->queue, queue->queue,
   4448 				    sizeof(n->queue)) >= sizeof(n->queue))
   4449 					errx(1, "expand_altq: strlcpy");
   4450 				if (strlcpy(n->ifname, interface->ifname,
   4451 				    sizeof(n->ifname)) >= sizeof(n->ifname))
   4452 					errx(1, "expand_altq: strlcpy");
   4453 				n->scheduler = pa.scheduler;
   4454 				n->next = NULL;
   4455 				n->tail = n;
   4456 				if (queues == NULL)
   4457 					queues = n;
   4458 				else {
   4459 					queues->tail->next = n;
   4460 					queues->tail = n;
   4461 				}
   4462 			);
   4463 		}
   4464 	);
   4465 	FREE_LIST(struct node_if, interfaces);
   4466 	FREE_LIST(struct node_queue, nqueues);
   4467 
   4468 	return (errs);
   4469 }
   4470 
   4471 int
   4472 expand_queue(struct pf_altq *a, struct node_if *interfaces,
   4473     struct node_queue *nqueues, struct node_queue_bw bwspec,
   4474     struct node_queue_opt *opts)
   4475 {
   4476 	struct node_queue	*n, *nq;
   4477 	struct pf_altq		 pa;
   4478 	u_int8_t		 found = 0;
   4479 	u_int8_t		 errs = 0;
   4480 
   4481 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
   4482 		FREE_LIST(struct node_queue, nqueues);
   4483 		return (0);
   4484 	}
   4485 
   4486 	if (queues == NULL) {
   4487 		yyerror("queue %s has no parent", a->qname);
   4488 		FREE_LIST(struct node_queue, nqueues);
   4489 		return (1);
   4490 	}
   4491 
   4492 	LOOP_THROUGH(struct node_if, interface, interfaces,
   4493 		LOOP_THROUGH(struct node_queue, tqueue, queues,
   4494 			if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
   4495 			    (interface->ifname[0] == 0 ||
   4496 			    (!interface->not && !strncmp(interface->ifname,
   4497 			    tqueue->ifname, IFNAMSIZ)) ||
   4498 			    (interface->not && strncmp(interface->ifname,
   4499 			    tqueue->ifname, IFNAMSIZ)))) {
   4500 				/* found ourself in queues */
   4501 				found++;
   4502 
   4503 				memcpy(&pa, a, sizeof(struct pf_altq));
   4504 
   4505 				if (pa.scheduler != ALTQT_NONE &&
   4506 				    pa.scheduler != tqueue->scheduler) {
   4507 					yyerror("exactly one scheduler type "
   4508 					    "per interface allowed");
   4509 					return (1);
   4510 				}
   4511 				pa.scheduler = tqueue->scheduler;
   4512 
   4513 				/* scheduler dependent error checking */
   4514 				switch (pa.scheduler) {
   4515 				case ALTQT_PRIQ:
   4516 					if (nqueues != NULL) {
   4517 						yyerror("priq queues cannot "
   4518 						    "have child queues");
   4519 						return (1);
   4520 					}
   4521 					if (bwspec.bw_absolute > 0 ||
   4522 					    bwspec.bw_percent < 100) {
   4523 						yyerror("priq doesn't take "
   4524 						    "bandwidth");
   4525 						return (1);
   4526 					}
   4527 					break;
   4528 				default:
   4529 					break;
   4530 				}
   4531 
   4532 				if (strlcpy(pa.ifname, tqueue->ifname,
   4533 				    sizeof(pa.ifname)) >= sizeof(pa.ifname))
   4534 					errx(1, "expand_queue: strlcpy");
   4535 				if (strlcpy(pa.parent, tqueue->parent,
   4536 				    sizeof(pa.parent)) >= sizeof(pa.parent))
   4537 					errx(1, "expand_queue: strlcpy");
   4538 
   4539 				if (eval_pfqueue(pf, &pa, &bwspec, opts))
   4540 					errs++;
   4541 				else
   4542 					if (pfctl_add_altq(pf, &pa))
   4543 						errs++;
   4544 
   4545 				for (nq = nqueues; nq != NULL; nq = nq->next) {
   4546 					if (!strcmp(a->qname, nq->queue)) {
   4547 						yyerror("queue cannot have "
   4548 						    "itself as child");
   4549 						errs++;
   4550 						continue;
   4551 					}
   4552 					n = calloc(1,
   4553 					    sizeof(struct node_queue));
   4554 					if (n == NULL)
   4555 						err(1, "expand_queue: calloc");
   4556 					if (strlcpy(n->parent, a->qname,
   4557 					    sizeof(n->parent)) >=
   4558 					    sizeof(n->parent))
   4559 						errx(1, "expand_queue strlcpy");
   4560 					if (strlcpy(n->queue, nq->queue,
   4561 					    sizeof(n->queue)) >=
   4562 					    sizeof(n->queue))
   4563 						errx(1, "expand_queue strlcpy");
   4564 					if (strlcpy(n->ifname, tqueue->ifname,
   4565 					    sizeof(n->ifname)) >=
   4566 					    sizeof(n->ifname))
   4567 						errx(1, "expand_queue strlcpy");
   4568 					n->scheduler = tqueue->scheduler;
   4569 					n->next = NULL;
   4570 					n->tail = n;
   4571 					if (queues == NULL)
   4572 						queues = n;
   4573 					else {
   4574 						queues->tail->next = n;
   4575 						queues->tail = n;
   4576 					}
   4577 				}
   4578 				if ((pf->opts & PF_OPT_VERBOSE) && (
   4579 				    (found == 1 && interface->ifname[0] == 0) ||
   4580 				    (found > 0 && interface->ifname[0] != 0))) {
   4581 					print_queue(&pf->paltq->altq, 0,
   4582 					    &bwspec, interface->ifname[0] != 0,
   4583 					    opts);
   4584 					if (nqueues && nqueues->tail) {
   4585 						printf("{ ");
   4586 						LOOP_THROUGH(struct node_queue,
   4587 						    queue, nqueues,
   4588 							printf("%s ",
   4589 							    queue->queue);
   4590 						);
   4591 						printf("}");
   4592 					}
   4593 					printf("\n");
   4594 				}
   4595 			}
   4596 		);
   4597 	);
   4598 
   4599 	FREE_LIST(struct node_queue, nqueues);
   4600 	FREE_LIST(struct node_if, interfaces);
   4601 
   4602 	if (!found) {
   4603 		yyerror("queue %s has no parent", a->qname);
   4604 		errs++;
   4605 	}
   4606 
   4607 	if (errs)
   4608 		return (1);
   4609 	else
   4610 		return (0);
   4611 }
   4612 
   4613 void
   4614 expand_rule(struct pf_rule *r,
   4615     struct node_if *interfaces, struct node_host *rpool_hosts,
   4616     struct node_proto *protos, struct node_os *src_oses,
   4617     struct node_host *src_hosts, struct node_port *src_ports,
   4618     struct node_host *dst_hosts, struct node_port *dst_ports,
   4619     struct node_uid *uids, struct node_gid *gids, struct node_icmp *icmp_types,
   4620     const char *anchor_call)
   4621 {
   4622 	sa_family_t		 af = r->af;
   4623 	int			 added = 0, error = 0;
   4624 	char			 ifname[IF_NAMESIZE];
   4625 	char			 label[PF_RULE_LABEL_SIZE];
   4626 	char			 tagname[PF_TAG_NAME_SIZE];
   4627 	char			 match_tagname[PF_TAG_NAME_SIZE];
   4628 	struct pf_pooladdr	*pa;
   4629 	struct node_host	*h;
   4630 	u_int8_t		 flags, flagset, keep_state;
   4631 
   4632 	if (strlcpy(label, r->label, sizeof(label)) >= sizeof(label))
   4633 		errx(1, "expand_rule: strlcpy");
   4634 	if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
   4635 		errx(1, "expand_rule: strlcpy");
   4636 	if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
   4637 	    sizeof(match_tagname))
   4638 		errx(1, "expand_rule: strlcpy");
   4639 	flags = r->flags;
   4640 	flagset = r->flagset;
   4641 	keep_state = r->keep_state;
   4642 
   4643 	LOOP_THROUGH(struct node_if, interface, interfaces,
   4644 	LOOP_THROUGH(struct node_proto, proto, protos,
   4645 	LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
   4646 	LOOP_THROUGH(struct node_host, src_host, src_hosts,
   4647 	LOOP_THROUGH(struct node_port, src_port, src_ports,
   4648 	LOOP_THROUGH(struct node_os, src_os, src_oses,
   4649 	LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
   4650 	LOOP_THROUGH(struct node_port, dst_port, dst_ports,
   4651 	LOOP_THROUGH(struct node_uid, uid, uids,
   4652 	LOOP_THROUGH(struct node_gid, gid, gids,
   4653 
   4654 		r->af = af;
   4655 		/* for link-local IPv6 address, interface must match up */
   4656 		if ((r->af && src_host->af && r->af != src_host->af) ||
   4657 		    (r->af && dst_host->af && r->af != dst_host->af) ||
   4658 		    (src_host->af && dst_host->af &&
   4659 		    src_host->af != dst_host->af) ||
   4660 		    (src_host->ifindex && dst_host->ifindex &&
   4661 		    src_host->ifindex != dst_host->ifindex) ||
   4662 		    (src_host->ifindex && *interface->ifname &&
   4663 		    src_host->ifindex != if_nametoindex(interface->ifname)) ||
   4664 		    (dst_host->ifindex && *interface->ifname &&
   4665 		    dst_host->ifindex != if_nametoindex(interface->ifname)))
   4666 			continue;
   4667 		if (!r->af && src_host->af)
   4668 			r->af = src_host->af;
   4669 		else if (!r->af && dst_host->af)
   4670 			r->af = dst_host->af;
   4671 
   4672 		if (*interface->ifname)
   4673 			strlcpy(r->ifname, interface->ifname,
   4674 			    sizeof(r->ifname));
   4675 		else if (if_indextoname(src_host->ifindex, ifname))
   4676 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
   4677 		else if (if_indextoname(dst_host->ifindex, ifname))
   4678 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
   4679 		else
   4680 			memset(r->ifname, '\0', sizeof(r->ifname));
   4681 
   4682 		if (strlcpy(r->label, label, sizeof(r->label)) >=
   4683 		    sizeof(r->label))
   4684 			errx(1, "expand_rule: strlcpy");
   4685 		if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
   4686 		    sizeof(r->tagname))
   4687 			errx(1, "expand_rule: strlcpy");
   4688 		if (strlcpy(r->match_tagname, match_tagname,
   4689 		    sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
   4690 			errx(1, "expand_rule: strlcpy");
   4691 		expand_label(r->label, PF_RULE_LABEL_SIZE, r->ifname, r->af,
   4692 		    src_host, src_port, dst_host, dst_port, proto->proto);
   4693 		expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af,
   4694 		    src_host, src_port, dst_host, dst_port, proto->proto);
   4695 		expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname,
   4696 		    r->af, src_host, src_port, dst_host, dst_port,
   4697 		    proto->proto);
   4698 
   4699 		error += check_netmask(src_host, r->af);
   4700 		error += check_netmask(dst_host, r->af);
   4701 
   4702 		r->ifnot = interface->not;
   4703 		r->proto = proto->proto;
   4704 		r->src.addr = src_host->addr;
   4705 		r->src.neg = src_host->not;
   4706 		r->src.port[0] = src_port->port[0];
   4707 		r->src.port[1] = src_port->port[1];
   4708 		r->src.port_op = src_port->op;
   4709 		r->dst.addr = dst_host->addr;
   4710 		r->dst.neg = dst_host->not;
   4711 		r->dst.port[0] = dst_port->port[0];
   4712 		r->dst.port[1] = dst_port->port[1];
   4713 		r->dst.port_op = dst_port->op;
   4714 		r->uid.op = uid->op;
   4715 		r->uid.uid[0] = uid->uid[0];
   4716 		r->uid.uid[1] = uid->uid[1];
   4717 		r->gid.op = gid->op;
   4718 		r->gid.gid[0] = gid->gid[0];
   4719 		r->gid.gid[1] = gid->gid[1];
   4720 		r->type = icmp_type->type;
   4721 		r->code = icmp_type->code;
   4722 
   4723 		if ((keep_state == PF_STATE_MODULATE ||
   4724 		    keep_state == PF_STATE_SYNPROXY) &&
   4725 		    r->proto && r->proto != IPPROTO_TCP)
   4726 			r->keep_state = PF_STATE_NORMAL;
   4727 		else
   4728 			r->keep_state = keep_state;
   4729 
   4730 		if (r->proto && r->proto != IPPROTO_TCP) {
   4731 			r->flags = 0;
   4732 			r->flagset = 0;
   4733 		} else {
   4734 			r->flags = flags;
   4735 			r->flagset = flagset;
   4736 		}
   4737 		if (icmp_type->proto && r->proto != icmp_type->proto) {
   4738 			yyerror("icmp-type mismatch");
   4739 			error++;
   4740 		}
   4741 
   4742 		if (src_os && src_os->os) {
   4743 			r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
   4744 			if ((pf->opts & PF_OPT_VERBOSE2) &&
   4745 			    r->os_fingerprint == PF_OSFP_NOMATCH)
   4746 				fprintf(stderr,
   4747 				    "warning: unknown '%s' OS fingerprint\n",
   4748 				    src_os->os);
   4749 		} else {
   4750 			r->os_fingerprint = PF_OSFP_ANY;
   4751 		}
   4752 
   4753 		TAILQ_INIT(&r->rpool.list);
   4754 		for (h = rpool_hosts; h != NULL; h = h->next) {
   4755 			pa = calloc(1, sizeof(struct pf_pooladdr));
   4756 			if (pa == NULL)
   4757 				err(1, "expand_rule: calloc");
   4758 			pa->addr = h->addr;
   4759 			if (h->ifname != NULL) {
   4760 				if (strlcpy(pa->ifname, h->ifname,
   4761 				    sizeof(pa->ifname)) >=
   4762 				    sizeof(pa->ifname))
   4763 					errx(1, "expand_rule: strlcpy");
   4764 			} else
   4765 				pa->ifname[0] = 0;
   4766 			TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
   4767 		}
   4768 
   4769 		if (rule_consistent(r, anchor_call[0]) < 0 || error)
   4770 			yyerror("skipping rule due to errors");
   4771 		else {
   4772 			r->nr = pf->astack[pf->asd]->match++;
   4773 			pfctl_add_rule(pf, r, anchor_call);
   4774 			added++;
   4775 		}
   4776 
   4777 	))))))))));
   4778 
   4779 	FREE_LIST(struct node_if, interfaces);
   4780 	FREE_LIST(struct node_proto, protos);
   4781 	FREE_LIST(struct node_host, src_hosts);
   4782 	FREE_LIST(struct node_port, src_ports);
   4783 	FREE_LIST(struct node_os, src_oses);
   4784 	FREE_LIST(struct node_host, dst_hosts);
   4785 	FREE_LIST(struct node_port, dst_ports);
   4786 	FREE_LIST(struct node_uid, uids);
   4787 	FREE_LIST(struct node_gid, gids);
   4788 	FREE_LIST(struct node_icmp, icmp_types);
   4789 	FREE_LIST(struct node_host, rpool_hosts);
   4790 
   4791 	if (!added)
   4792 		yyerror("rule expands to no valid combination");
   4793 }
   4794 
   4795 int
   4796 expand_skip_interface(struct node_if *interfaces)
   4797 {
   4798 	int	errs = 0;
   4799 
   4800 	if (!interfaces || (!interfaces->next && !interfaces->not &&
   4801 	    !strcmp(interfaces->ifname, "none"))) {
   4802 		if (pf->opts & PF_OPT_VERBOSE)
   4803 			printf("set skip on none\n");
   4804 		errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
   4805 		return (errs);
   4806 	}
   4807 
   4808 	if (pf->opts & PF_OPT_VERBOSE)
   4809 		printf("set skip on {");
   4810 	LOOP_THROUGH(struct node_if, interface, interfaces,
   4811 		if (pf->opts & PF_OPT_VERBOSE)
   4812 			printf(" %s", interface->ifname);
   4813 		if (interface->not) {
   4814 			yyerror("skip on ! <interface> is not supported");
   4815 			errs++;
   4816 		} else
   4817 			errs += pfctl_set_interface_flags(pf,
   4818 			    interface->ifname, PFI_IFLAG_SKIP, 1);
   4819 	);
   4820 	if (pf->opts & PF_OPT_VERBOSE)
   4821 		printf(" }\n");
   4822 
   4823 	FREE_LIST(struct node_if, interfaces);
   4824 
   4825 	if (errs)
   4826 		return (1);
   4827 	else
   4828 		return (0);
   4829 }
   4830 
   4831 #undef FREE_LIST
   4832 #undef LOOP_THROUGH
   4833 
   4834 int
   4835 check_rulestate(int desired_state)
   4836 {
   4837 	if (require_order && (rulestate > desired_state)) {
   4838 		yyerror("Rules must be in order: options, normalization, "
   4839 		    "queueing, translation, filtering");
   4840 		return (1);
   4841 	}
   4842 	rulestate = desired_state;
   4843 	return (0);
   4844 }
   4845 
   4846 int
   4847 kw_cmp(const void *k, const void *e)
   4848 {
   4849 	return (strcmp(k, ((const struct keywords *)e)->k_name));
   4850 }
   4851 
   4852 int
   4853 lookup(char *s)
   4854 {
   4855 	/* this has to be sorted always */
   4856 	static const struct keywords keywords[] = {
   4857 		{ "all",		ALL},
   4858 		{ "allow-opts",		ALLOWOPTS},
   4859 		{ "altq",		ALTQ},
   4860 		{ "anchor",		ANCHOR},
   4861 		{ "antispoof",		ANTISPOOF},
   4862 		{ "any",		ANY},
   4863 		{ "bandwidth",		BANDWIDTH},
   4864 		{ "binat",		BINAT},
   4865 		{ "binat-anchor",	BINATANCHOR},
   4866 		{ "bitmask",		BITMASK},
   4867 		{ "block",		BLOCK},
   4868 		{ "block-policy",	BLOCKPOLICY},
   4869 		{ "cbq",		CBQ},
   4870 		{ "code",		CODE},
   4871 		{ "crop",		FRAGCROP},
   4872 		{ "debug",		DEBUG},
   4873 		{ "drop",		DROP},
   4874 		{ "drop-ovl",		FRAGDROP},
   4875 		{ "dup-to",		DUPTO},
   4876 		{ "fastroute",		FASTROUTE},
   4877 		{ "file",		FILENAME},
   4878 		{ "fingerprints",	FINGERPRINTS},
   4879 		{ "flags",		FLAGS},
   4880 		{ "floating",		FLOATING},
   4881 		{ "flush",		FLUSH},
   4882 		{ "for",		FOR},
   4883 		{ "fragment",		FRAGMENT},
   4884 		{ "from",		FROM},
   4885 		{ "global",		GLOBAL},
   4886 		{ "group",		GROUP},
   4887 		{ "hfsc",		HFSC},
   4888 		{ "hostid",		HOSTID},
   4889 		{ "icmp-type",		ICMPTYPE},
   4890 		{ "icmp6-type",		ICMP6TYPE},
   4891 		{ "if-bound",		IFBOUND},
   4892 		{ "in",			IN},
   4893 		{ "inet",		INET},
   4894 		{ "inet6",		INET6},
   4895 		{ "keep",		KEEP},
   4896 		{ "label",		LABEL},
   4897 		{ "limit",		LIMIT},
   4898 		{ "linkshare",		LINKSHARE},
   4899 		{ "load",		LOAD},
   4900 		{ "log",		LOG},
   4901 		{ "loginterface",	LOGINTERFACE},
   4902 		{ "max",		MAXIMUM},
   4903 		{ "max-mss",		MAXMSS},
   4904 		{ "max-src-conn",	MAXSRCCONN},
   4905 		{ "max-src-conn-rate",	MAXSRCCONNRATE},
   4906 		{ "max-src-nodes",	MAXSRCNODES},
   4907 		{ "max-src-states",	MAXSRCSTATES},
   4908 		{ "min-ttl",		MINTTL},
   4909 		{ "modulate",		MODULATE},
   4910 		{ "nat",		NAT},
   4911 		{ "nat-anchor",		NATANCHOR},
   4912 		{ "no",			NO},
   4913 		{ "no-df",		NODF},
   4914 		{ "no-route",		NOROUTE},
   4915 		{ "no-sync",		NOSYNC},
   4916 		{ "on",			ON},
   4917 		{ "optimization",	OPTIMIZATION},
   4918 		{ "os",			OS},
   4919 		{ "out",		OUT},
   4920 		{ "overload",		OVERLOAD},
   4921 		{ "pass",		PASS},
   4922 		{ "port",		PORT},
   4923 		{ "priority",		PRIORITY},
   4924 		{ "priq",		PRIQ},
   4925 		{ "probability",	PROBABILITY},
   4926 		{ "proto",		PROTO},
   4927 		{ "qlimit",		QLIMIT},
   4928 		{ "queue",		QUEUE},
   4929 		{ "quick",		QUICK},
   4930 		{ "random",		RANDOM},
   4931 		{ "random-id",		RANDOMID},
   4932 		{ "rdr",		RDR},
   4933 		{ "rdr-anchor",		RDRANCHOR},
   4934 		{ "realtime",		REALTIME},
   4935 		{ "reassemble",		REASSEMBLE},
   4936 		{ "reply-to",		REPLYTO},
   4937 		{ "require-order",	REQUIREORDER},
   4938 		{ "return",		RETURN},
   4939 		{ "return-icmp",	RETURNICMP},
   4940 		{ "return-icmp6",	RETURNICMP6},
   4941 		{ "return-rst",		RETURNRST},
   4942 		{ "round-robin",	ROUNDROBIN},
   4943 		{ "route",		ROUTE},
   4944 		{ "route-to",		ROUTETO},
   4945 		{ "rtable",		RTABLE},
   4946 		{ "rule",		RULE},
   4947 		{ "ruleset-optimization",	RULESET_OPTIMIZATION},
   4948 		{ "scrub",		SCRUB},
   4949 		{ "set",		SET},
   4950 		{ "skip",		SKIP},
   4951 		{ "source-hash",	SOURCEHASH},
   4952 		{ "source-track",	SOURCETRACK},
   4953 		{ "state",		STATE},
   4954 		{ "state-policy",	STATEPOLICY},
   4955 		{ "static-port",	STATICPORT},
   4956 		{ "sticky-address",	STICKYADDRESS},
   4957 		{ "synproxy",		SYNPROXY},
   4958 		{ "table",		TABLE},
   4959 		{ "tag",		TAG},
   4960 		{ "tagged",		TAGGED},
   4961 		{ "tbrsize",		TBRSIZE},
   4962 		{ "timeout",		TIMEOUT},
   4963 		{ "to",			TO},
   4964 		{ "tos",		TOS},
   4965 		{ "ttl",		TTL},
   4966 		{ "upperlimit",		UPPERLIMIT},
   4967 		{ "urpf-failed",	URPFFAILED},
   4968 		{ "user",		USER},
   4969 	};
   4970 	const struct keywords	*p;
   4971 
   4972 	p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
   4973 	    sizeof(keywords[0]), kw_cmp);
   4974 
   4975 	if (p) {
   4976 		if (debug > 1)
   4977 			fprintf(stderr, "%s: %d\n", s, p->k_val);
   4978 		return (p->k_val);
   4979 	} else {
   4980 		if (debug > 1)
   4981 			fprintf(stderr, "string: %s\n", s);
   4982 		return (STRING);
   4983 	}
   4984 }
   4985 
   4986 #define MAXPUSHBACK	128
   4987 
   4988 char	*parsebuf;
   4989 int	 parseindex;
   4990 char	 pushback_buffer[MAXPUSHBACK];
   4991 int	 pushback_index = 0;
   4992 
   4993 int
   4994 lgetc(FILE *f)
   4995 {
   4996 	int	c, next;
   4997 
   4998 	if (parsebuf) {
   4999 		/* Read character from the parsebuffer instead of input. */
   5000 		if (parseindex >= 0) {
   5001 			c = parsebuf[parseindex++];
   5002 			if (c != '\0')
   5003 				return (c);
   5004 			parsebuf = NULL;
   5005 		} else
   5006 			parseindex++;
   5007 	}
   5008 
   5009 	if (pushback_index)
   5010 		return (pushback_buffer[--pushback_index]);
   5011 
   5012 	while ((c = getc(f)) == '\\') {
   5013 		next = getc(f);
   5014 		if (next != '\n') {
   5015 			c = next;
   5016 			break;
   5017 		}
   5018 		yylval.lineno = lineno;
   5019 		lineno++;
   5020 	}
   5021 	if (c == '\t' || c == ' ') {
   5022 		/* Compress blanks to a single space. */
   5023 		do {
   5024 			c = getc(f);
   5025 		} while (c == '\t' || c == ' ');
   5026 		ungetc(c, f);
   5027 		c = ' ';
   5028 	}
   5029 
   5030 	return (c);
   5031 }
   5032 
   5033 int
   5034 lungetc(int c)
   5035 {
   5036 	if (c == EOF)
   5037 		return (EOF);
   5038 	if (parsebuf) {
   5039 		parseindex--;
   5040 		if (parseindex >= 0)
   5041 			return (c);
   5042 	}
   5043 	if (pushback_index < MAXPUSHBACK-1)
   5044 		return (pushback_buffer[pushback_index++] = c);
   5045 	else
   5046 		return (EOF);
   5047 }
   5048 
   5049 int
   5050 findeol(void)
   5051 {
   5052 	int	c;
   5053 
   5054 	parsebuf = NULL;
   5055 	pushback_index = 0;
   5056 
   5057 	/* skip to either EOF or the first real EOL */
   5058 	while (1) {
   5059 		c = lgetc(fin);
   5060 		if (c == '\n') {
   5061 			lineno++;
   5062 			break;
   5063 		}
   5064 		if (c == EOF)
   5065 			break;
   5066 	}
   5067 	return (ERROR);
   5068 }
   5069 
   5070 int
   5071 yylex(void)
   5072 {
   5073 	char	 buf[8096];
   5074 	char	*p, *val;
   5075 	int	 endc, c, next;
   5076 	int	 token;
   5077 
   5078 top:
   5079 	p = buf;
   5080 	while ((c = lgetc(fin)) == ' ')
   5081 		; /* nothing */
   5082 
   5083 	yylval.lineno = lineno;
   5084 	if (c == '#')
   5085 		while ((c = lgetc(fin)) != '\n' && c != EOF)
   5086 			; /* nothing */
   5087 	if (c == '$' && parsebuf == NULL) {
   5088 		while (1) {
   5089 			if ((c = lgetc(fin)) == EOF)
   5090 				return (0);
   5091 
   5092 			if (p + 1 >= buf + sizeof(buf) - 1) {
   5093 				yyerror("string too long");
   5094 				return (findeol());
   5095 			}
   5096 			if (isalnum(c) || c == '_') {
   5097 				*p++ = (char)c;
   5098 				continue;
   5099 			}
   5100 			*p = '\0';
   5101 			lungetc(c);
   5102 			break;
   5103 		}
   5104 		val = symget(buf);
   5105 		if (val == NULL) {
   5106 			yyerror("macro '%s' not defined", buf);
   5107 			return (findeol());
   5108 		}
   5109 		parsebuf = val;
   5110 		parseindex = 0;
   5111 		goto top;
   5112 	}
   5113 
   5114 	switch (c) {
   5115 	case '\'':
   5116 	case '"':
   5117 		endc = c;
   5118 		while (1) {
   5119 			if ((c = lgetc(fin)) == EOF)
   5120 				return (0);
   5121 			if (c == endc) {
   5122 				*p = '\0';
   5123 				break;
   5124 			}
   5125 			if (c == '\n') {
   5126 				lineno++;
   5127 				continue;
   5128 			}
   5129 			if (p + 1 >= buf + sizeof(buf) - 1) {
   5130 				yyerror("string too long");
   5131 				return (findeol());
   5132 			}
   5133 			*p++ = (char)c;
   5134 		}
   5135 		yylval.v.string = strdup(buf);
   5136 		if (yylval.v.string == NULL)
   5137 			err(1, "yylex: strdup");
   5138 		return (STRING);
   5139 	case '<':
   5140 		next = lgetc(fin);
   5141 		if (next == '>') {
   5142 			yylval.v.i = PF_OP_XRG;
   5143 			return (PORTBINARY);
   5144 		}
   5145 		lungetc(next);
   5146 		break;
   5147 	case '>':
   5148 		next = lgetc(fin);
   5149 		if (next == '<') {
   5150 			yylval.v.i = PF_OP_IRG;
   5151 			return (PORTBINARY);
   5152 		}
   5153 		lungetc(next);
   5154 		break;
   5155 	case '-':
   5156 		next = lgetc(fin);
   5157 		if (next == '>')
   5158 			return (ARROW);
   5159 		lungetc(next);
   5160 		break;
   5161 	}
   5162 
   5163 #define allowed_in_string(x) \
   5164 	(isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
   5165 	x != '{' && x != '}' && x != '<' && x != '>' && \
   5166 	x != '!' && x != '=' && x != '/' && x != '#' && \
   5167 	x != ','))
   5168 
   5169 	if (isalnum(c) || c == ':' || c == '_') {
   5170 		do {
   5171 			*p++ = c;
   5172 			if ((unsigned)(p-buf) >= sizeof(buf)) {
   5173 				yyerror("string too long");
   5174 				return (findeol());
   5175 			}
   5176 		} while ((c = lgetc(fin)) != EOF && (allowed_in_string(c)));
   5177 		lungetc(c);
   5178 		*p = '\0';
   5179 		if ((token = lookup(buf)) == STRING)
   5180 			if ((yylval.v.string = strdup(buf)) == NULL)
   5181 				err(1, "yylex: strdup");
   5182 		return (token);
   5183 	}
   5184 	if (c == '\n') {
   5185 		yylval.lineno = lineno;
   5186 		lineno++;
   5187 	}
   5188 	if (c == EOF)
   5189 		return (0);
   5190 	return (c);
   5191 }
   5192 
   5193 int
   5194 parse_rules(FILE *input, struct pfctl *xpf)
   5195 {
   5196 	struct sym	*sym, *next;
   5197 
   5198 	fin = input;
   5199 	pf = xpf;
   5200 	lineno = 1;
   5201 	errors = 0;
   5202 	rulestate = PFCTL_STATE_NONE;
   5203 	returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
   5204 	returnicmp6default =
   5205 	    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
   5206 	blockpolicy = PFRULE_DROP;
   5207 	require_order = 1;
   5208 
   5209 	yyparse();
   5210 
   5211 	/* Free macros and check which have not been used. */
   5212 	for (sym = TAILQ_FIRST(&symhead); sym != NULL; sym = next) {
   5213 		next = TAILQ_NEXT(sym, entries);
   5214 		if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
   5215 			fprintf(stderr, "warning: macro '%s' not "
   5216 			    "used\n", sym->nam);
   5217 		free(sym->nam);
   5218 		free(sym->val);
   5219 		TAILQ_REMOVE(&symhead, sym, entries);
   5220 		free(sym);
   5221 	}
   5222 
   5223 	return (errors ? -1 : 0);
   5224 }
   5225 
   5226 /*
   5227  * Over-designed efficiency is a French and German concept, so how about
   5228  * we wait until they discover this ugliness and make it all fancy.
   5229  */
   5230 int
   5231 symset(const char *nam, const char *val, int persist)
   5232 {
   5233 	struct sym	*sym;
   5234 
   5235 	for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
   5236 	    sym = TAILQ_NEXT(sym, entries))
   5237 		;	/* nothing */
   5238 
   5239 	if (sym != NULL) {
   5240 		if (sym->persist == 1)
   5241 			return (0);
   5242 		else {
   5243 			free(sym->nam);
   5244 			free(sym->val);
   5245 			TAILQ_REMOVE(&symhead, sym, entries);
   5246 			free(sym);
   5247 		}
   5248 	}
   5249 	if ((sym = calloc(1, sizeof(*sym))) == NULL)
   5250 		return (-1);
   5251 
   5252 	sym->nam = strdup(nam);
   5253 	if (sym->nam == NULL) {
   5254 		free(sym);
   5255 		return (-1);
   5256 	}
   5257 	sym->val = strdup(val);
   5258 	if (sym->val == NULL) {
   5259 		free(sym->nam);
   5260 		free(sym);
   5261 		return (-1);
   5262 	}
   5263 	sym->used = 0;
   5264 	sym->persist = persist;
   5265 	TAILQ_INSERT_TAIL(&symhead, sym, entries);
   5266 	return (0);
   5267 }
   5268 
   5269 int
   5270 pfctl_cmdline_symset(char *s)
   5271 {
   5272 	char	*sym, *val;
   5273 	int	 ret;
   5274 
   5275 	if ((val = strrchr(s, '=')) == NULL)
   5276 		return (-1);
   5277 
   5278 	if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
   5279 		err(1, "pfctl_cmdline_symset: malloc");
   5280 
   5281 	strlcpy(sym, s, strlen(s) - strlen(val) + 1);
   5282 
   5283 	ret = symset(sym, val + 1, 1);
   5284 	free(sym);
   5285 
   5286 	return (ret);
   5287 }
   5288 
   5289 char *
   5290 symget(const char *nam)
   5291 {
   5292 	struct sym	*sym;
   5293 
   5294 	TAILQ_FOREACH(sym, &symhead, entries)
   5295 		if (strcmp(nam, sym->nam) == 0) {
   5296 			sym->used = 1;
   5297 			return (sym->val);
   5298 		}
   5299 	return (NULL);
   5300 }
   5301 
   5302 void
   5303 mv_rules(struct pf_ruleset *src, struct pf_ruleset *dst)
   5304 {
   5305 	int i;
   5306 	struct pf_rule *r;
   5307 
   5308 	for (i = 0; i < PF_RULESET_MAX; ++i) {
   5309 		while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
   5310 		    != NULL) {
   5311 			TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
   5312 			TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
   5313 			dst->anchor->match++;
   5314 		}
   5315 		src->anchor->match = 0;
   5316 		while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
   5317 		    != NULL) {
   5318 			TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
   5319 			TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
   5320 				r, entries);
   5321 		}
   5322 	}
   5323 }
   5324 
   5325 void
   5326 decide_address_family(struct node_host *n, sa_family_t *af)
   5327 {
   5328 	if (*af != 0 || n == NULL)
   5329 		return;
   5330 	*af = n->af;
   5331 	while ((n = n->next) != NULL) {
   5332 		if (n->af != *af) {
   5333 			*af = 0;
   5334 			return;
   5335 		}
   5336 	}
   5337 }
   5338 
   5339 void
   5340 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
   5341 {
   5342 	struct node_host	*n = *nh, *prev = NULL;
   5343 
   5344 	while (n != NULL) {
   5345 		if (*af && n->af && n->af != *af) {
   5346 			/* unlink and free n */
   5347 			struct node_host *next = n->next;
   5348 
   5349 			/* adjust tail pointer */
   5350 			if (n == (*nh)->tail)
   5351 				(*nh)->tail = prev;
   5352 			/* adjust previous node's next pointer */
   5353 			if (prev == NULL)
   5354 				*nh = next;
   5355 			else
   5356 				prev->next = next;
   5357 			/* free node */
   5358 			if (n->ifname != NULL)
   5359 				free(n->ifname);
   5360 			free(n);
   5361 			n = next;
   5362 		} else {
   5363 			if (n->af && !*af)
   5364 				*af = n->af;
   5365 			prev = n;
   5366 			n = n->next;
   5367 		}
   5368 	}
   5369 }
   5370 
   5371 int
   5372 invalid_redirect(struct node_host *nh, sa_family_t af)
   5373 {
   5374 	if (!af) {
   5375 		struct node_host *n;
   5376 
   5377 		/* tables and dyniftl are ok without an address family */
   5378 		for (n = nh; n != NULL; n = n->next) {
   5379 			if (n->addr.type != PF_ADDR_TABLE &&
   5380 			    n->addr.type != PF_ADDR_DYNIFTL) {
   5381 				yyerror("address family not given and "
   5382 				    "translation address expands to multiple "
   5383 				    "address families");
   5384 				return (1);
   5385 			}
   5386 		}
   5387 	}
   5388 	if (nh == NULL) {
   5389 		yyerror("no translation address with matching address family "
   5390 		    "found.");
   5391 		return (1);
   5392 	}
   5393 	return (0);
   5394 }
   5395 
   5396 int
   5397 atoul(char *s, u_long *ulvalp)
   5398 {
   5399 	u_long	 ulval;
   5400 	char	*ep;
   5401 
   5402 	errno = 0;
   5403 	ulval = strtoul(s, &ep, 0);
   5404 	if (s[0] == '\0' || *ep != '\0')
   5405 		return (-1);
   5406 	if (errno == ERANGE && ulval == ULONG_MAX)
   5407 		return (-1);
   5408 	*ulvalp = ulval;
   5409 	return (0);
   5410 }
   5411 
   5412 int
   5413 getservice(char *n)
   5414 {
   5415 	struct servent	*s;
   5416 	u_long		 ulval;
   5417 
   5418 	if (atoul(n, &ulval) == 0) {
   5419 		if (ulval > 65535) {
   5420 			yyerror("illegal port value %lu", ulval);
   5421 			return (-1);
   5422 		}
   5423 		return (htons(ulval));
   5424 	} else {
   5425 		s = getservbyname(n, "tcp");
   5426 		if (s == NULL)
   5427 			s = getservbyname(n, "udp");
   5428 		if (s == NULL) {
   5429 			yyerror("unknown port %s", n);
   5430 			return (-1);
   5431 		}
   5432 		return (s->s_port);
   5433 	}
   5434 }
   5435 
   5436 int
   5437 rule_label(struct pf_rule *r, char *s)
   5438 {
   5439 	if (s) {
   5440 		if (strlcpy(r->label, s, sizeof(r->label)) >=
   5441 		    sizeof(r->label)) {
   5442 			yyerror("rule label too long (max %d chars)",
   5443 			    sizeof(r->label)-1);
   5444 			return (-1);
   5445 		}
   5446 	}
   5447 	return (0);
   5448 }
   5449 
   5450 u_int16_t
   5451 parseicmpspec(char *w, sa_family_t af)
   5452 {
   5453 	const struct icmpcodeent	*p;
   5454 	u_long				 ulval;
   5455 	u_int8_t			 icmptype;
   5456 
   5457 	if (af == AF_INET)
   5458 		icmptype = returnicmpdefault >> 8;
   5459 	else
   5460 		icmptype = returnicmp6default >> 8;
   5461 
   5462 	if (atoul(w, &ulval) == -1) {
   5463 		if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
   5464 			yyerror("unknown icmp code %s", w);
   5465 			return (0);
   5466 		}
   5467 		ulval = p->code;
   5468 	}
   5469 	if (ulval > 255) {
   5470 		yyerror("invalid icmp code %lu", ulval);
   5471 		return (0);
   5472 	}
   5473 	return (icmptype << 8 | ulval);
   5474 }
   5475 
   5476 int
   5477 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
   5478 {
   5479 	struct loadanchors	*la;
   5480 	FILE			*fin;
   5481 
   5482 	TAILQ_FOREACH(la, &loadanchorshead, entries) {
   5483 		if (pf->opts & PF_OPT_VERBOSE)
   5484 			fprintf(stderr, "\nLoading anchor %s from %s\n",
   5485 			    la->anchorname, la->filename);
   5486 		if ((fin = pfctl_fopen(la->filename, "r")) == NULL) {
   5487 			warn("%s", la->filename);
   5488 			continue;
   5489 		}
   5490 		if (pfctl_rules(dev, la->filename, fin, pf->opts, pf->optimize,
   5491 		    la->anchorname, trans) == -1)
   5492 			return (-1);
   5493 	}
   5494 
   5495 	return (0);
   5496 }
   5497