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