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