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