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