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