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