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