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