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