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