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