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