xref: /freebsd/sbin/pfctl/parse.y (revision aa69fdf1542db0247e9b991002603fc2046bcfbc)
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 			expand_rule(&r, $4, $5.host, $9.nat.rdr ? $9.nat.rdr->host : NULL,
2836 			    $7, $8.src_os, $8.src.host, $8.src.port, $8.dst.host,
2837 			    $8.dst.port, $9.uid, $9.gid, $9.rcv, $9.icmpspec, "");
2838 		}
2839 		;
2840 
2841 filter_opts	:	{
2842 				bzero(&filter_opts, sizeof filter_opts);
2843 				filter_opts.rtableid = -1;
2844 			}
2845 		    filter_opts_l
2846 			{ $$ = filter_opts; }
2847 		| /* empty */	{
2848 			bzero(&filter_opts, sizeof filter_opts);
2849 			filter_opts.rtableid = -1;
2850 			$$ = filter_opts;
2851 		}
2852 		;
2853 
2854 filter_opts_l	: filter_opts_l filter_opt
2855 		| filter_opt
2856 		;
2857 
2858 filter_opt	: USER uids {
2859 			if (filter_opts.uid)
2860 				$2->tail->next = filter_opts.uid;
2861 			filter_opts.uid = $2;
2862 		}
2863 		| GROUP gids {
2864 			if (filter_opts.gid)
2865 				$2->tail->next = filter_opts.gid;
2866 			filter_opts.gid = $2;
2867 		}
2868 		| flags {
2869 			if (filter_opts.marker & FOM_FLAGS) {
2870 				yyerror("flags cannot be redefined");
2871 				YYERROR;
2872 			}
2873 			filter_opts.marker |= FOM_FLAGS;
2874 			filter_opts.flags.b1 |= $1.b1;
2875 			filter_opts.flags.b2 |= $1.b2;
2876 			filter_opts.flags.w |= $1.w;
2877 			filter_opts.flags.w2 |= $1.w2;
2878 		}
2879 		| icmpspec {
2880 			if (filter_opts.marker & FOM_ICMP) {
2881 				yyerror("icmp-type cannot be redefined");
2882 				YYERROR;
2883 			}
2884 			filter_opts.marker |= FOM_ICMP;
2885 			filter_opts.icmpspec = $1;
2886 		}
2887 		| PRIO NUMBER {
2888 			if (filter_opts.marker & FOM_PRIO) {
2889 				yyerror("prio cannot be redefined");
2890 				YYERROR;
2891 			}
2892 			if ($2 < 0 || $2 > PF_PRIO_MAX) {
2893 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
2894 				YYERROR;
2895 			}
2896 			filter_opts.marker |= FOM_PRIO;
2897 			filter_opts.prio = $2;
2898 		}
2899 		| TOS tos {
2900 			if (filter_opts.marker & FOM_TOS) {
2901 				yyerror("tos cannot be redefined");
2902 				YYERROR;
2903 			}
2904 			filter_opts.marker |= FOM_TOS;
2905 			filter_opts.tos = $2;
2906 		}
2907 		| keep {
2908 			if (filter_opts.marker & FOM_KEEP) {
2909 				yyerror("modulate or keep cannot be redefined");
2910 				YYERROR;
2911 			}
2912 			filter_opts.marker |= FOM_KEEP;
2913 			filter_opts.keep.action = $1.action;
2914 			filter_opts.keep.options = $1.options;
2915 		}
2916 		| RIDENTIFIER number {
2917 			filter_opts.ridentifier = $2;
2918 		}
2919 		| FRAGMENT {
2920 			filter_opts.fragment = 1;
2921 		}
2922 		| ALLOWOPTS {
2923 			filter_opts.allowopts = 1;
2924 		}
2925 		| label	{
2926 			if (filter_opts.labelcount >= PF_RULE_MAX_LABEL_COUNT) {
2927 				yyerror("label can only be used %d times", PF_RULE_MAX_LABEL_COUNT);
2928 				YYERROR;
2929 			}
2930 			filter_opts.label[filter_opts.labelcount++] = $1;
2931 		}
2932 		| qname	{
2933 			if (filter_opts.queues.qname) {
2934 				yyerror("queue cannot be redefined");
2935 				YYERROR;
2936 			}
2937 			filter_opts.queues = $1;
2938 		}
2939 		| DNPIPE number {
2940 			filter_opts.dnpipe = $2;
2941 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
2942 		}
2943 		| DNPIPE '(' number ')' {
2944 			filter_opts.dnpipe = $3;
2945 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
2946 		}
2947 		| DNPIPE '(' number comma number ')' {
2948 			filter_opts.dnrpipe = $5;
2949 			filter_opts.dnpipe = $3;
2950 			filter_opts.free_flags |= PFRULE_DN_IS_PIPE;
2951 		}
2952 		| DNQUEUE number {
2953 			filter_opts.dnpipe = $2;
2954 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
2955 		}
2956 		| DNQUEUE '(' number comma number ')' {
2957 			filter_opts.dnrpipe = $5;
2958 			filter_opts.dnpipe = $3;
2959 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
2960 		}
2961 		| DNQUEUE '(' number ')' {
2962 			filter_opts.dnpipe = $3;
2963 			filter_opts.free_flags |= PFRULE_DN_IS_QUEUE;
2964 		}
2965 		| TAG string				{
2966 			filter_opts.tag = $2;
2967 		}
2968 		| not TAGGED string			{
2969 			filter_opts.match_tag = $3;
2970 			filter_opts.match_tag_not = $1;
2971 		}
2972 		| RECEIVEDON if_item {
2973 			if (filter_opts.rcv) {
2974 				yyerror("cannot respecify received-on");
2975 				YYERROR;
2976 			}
2977 			filter_opts.rcv = $2;
2978 		}
2979 		| PROBABILITY probability		{
2980 			double	p;
2981 
2982 			p = floor($2 * UINT_MAX + 0.5);
2983 			if (p < 0.0 || p > UINT_MAX) {
2984 				yyerror("invalid probability: %lf", p);
2985 				YYERROR;
2986 			}
2987 			filter_opts.prob = (u_int32_t)p;
2988 			if (filter_opts.prob == 0)
2989 				filter_opts.prob = 1;
2990 		}
2991 		| RTABLE NUMBER				{
2992 			if ($2 < 0 || $2 > rt_tableid_max()) {
2993 				yyerror("invalid rtable id");
2994 				YYERROR;
2995 			}
2996 			filter_opts.rtableid = $2;
2997 		}
2998 		| DIVERTTO portplain {
2999 #ifdef __FreeBSD__
3000 			filter_opts.divert.port = $2.a;
3001 			if (!filter_opts.divert.port) {
3002 				yyerror("invalid divert port: %u", ntohs($2.a));
3003 				YYERROR;
3004 			}
3005 #endif
3006 		}
3007 		| DIVERTTO STRING PORT portplain {
3008 #ifndef __FreeBSD__
3009 			if ((filter_opts.divert.addr = host($2)) == NULL) {
3010 				yyerror("could not parse divert address: %s",
3011 				    $2);
3012 				free($2);
3013 				YYERROR;
3014 			}
3015 #else
3016 			if ($2)
3017 #endif
3018 			free($2);
3019 			filter_opts.divert.port = $4.a;
3020 			if (!filter_opts.divert.port) {
3021 				yyerror("invalid divert port: %u", ntohs($4.a));
3022 				YYERROR;
3023 			}
3024 		}
3025 		| DIVERTREPLY {
3026 #ifdef __FreeBSD__
3027 			yyerror("divert-reply has no meaning in FreeBSD pf(4)");
3028 			YYERROR;
3029 #else
3030 			filter_opts.divert.port = 1;	/* some random value */
3031 #endif
3032 		}
3033 		| SCRUB '(' scrub_opts ')' {
3034 			filter_opts.nodf = $3.nodf;
3035 			filter_opts.minttl = $3.minttl;
3036 			if ($3.marker & FOM_SETTOS) {
3037 				/* Old style rules are "scrub set-tos 0x42"
3038 				 * New style are "set tos 0x42 scrub (...)"
3039 				 * What is in "scrub(...)"" is unfortunately the
3040 				 * original scrub syntax so it would overwrite
3041 				 * "set tos" of a pass/match rule.
3042 				 */
3043 				filter_opts.settos = $3.settos;
3044 			}
3045 			filter_opts.randomid = $3.randomid;
3046 			filter_opts.max_mss = $3.maxmss;
3047 			if ($3.reassemble_tcp)
3048 				filter_opts.marker |= FOM_SCRUB_TCP;
3049 			filter_opts.marker |= $3.marker;
3050 		}
3051 		| AFTO af FROM redirspec pool_opts {
3052 			if (filter_opts.nat.rdr) {
3053 				yyerror("cannot respecify af-to");
3054 				YYERROR;
3055 			}
3056 			if ($2 == 0) {
3057 				yyerror("no address family specified");
3058 				YYERROR;
3059 			}
3060 			if ($4->af && $4->af != $2) {
3061 				yyerror("af-to addresses must be in the "
3062 				   "target address family");
3063 				YYERROR;
3064 			}
3065 			filter_opts.nat.af = $2;
3066 			filter_opts.nat.rdr = calloc(1, sizeof(struct redirection));
3067 			if (filter_opts.nat.rdr == NULL)
3068 				err(1, "af-to: calloc");
3069 			filter_opts.nat.rdr->host = $4;
3070 			memcpy(&filter_opts.nat.pool_opts, &$5,
3071 			    sizeof(filter_opts.nat.pool_opts));
3072 			filter_opts.rdr.rdr =
3073 			    calloc(1, sizeof(struct redirection));
3074 			bzero(&filter_opts.rdr.pool_opts,
3075 			    sizeof(filter_opts.rdr.pool_opts));
3076 			filter_opts.marker |= FOM_AFTO;
3077 		}
3078 		| AFTO af FROM redirspec pool_opts TO redirspec pool_opts {
3079 			if (filter_opts.nat.rdr) {
3080 				yyerror("cannot respecify af-to");
3081 				YYERROR;
3082 			}
3083 			if ($2 == 0) {
3084 				yyerror("no address family specified");
3085 				YYERROR;
3086 			}
3087 				if (($4->af && $4->af != $2) ||
3088 				($7->af && $7->af != $2)) {
3089 				yyerror("af-to addresses must be in the "
3090 				   "target address family");
3091 				YYERROR;
3092 			}
3093 			filter_opts.nat.af = $2;
3094 			filter_opts.nat.rdr = calloc(1, sizeof(struct redirection));
3095 			if (filter_opts.nat.rdr == NULL)
3096 				err(1, "af-to: calloc");
3097 			filter_opts.nat.rdr->host = $4;
3098 			memcpy(&filter_opts.nat.pool_opts, &$5,
3099 			    sizeof(filter_opts.nat.pool_opts));
3100 			filter_opts.rdr.af = $2;
3101 			filter_opts.rdr.rdr = calloc(1, sizeof(struct redirection));
3102 			if (filter_opts.rdr.rdr == NULL)
3103 				err(1, "af-to: calloc");
3104 			filter_opts.rdr.rdr->host = $7;
3105 			memcpy(&filter_opts.nat.pool_opts, &$8,
3106 			    sizeof(filter_opts.nat.pool_opts));
3107 			filter_opts.marker |= FOM_AFTO;
3108 		}
3109 		| filter_sets
3110 		;
3111 
3112 filter_sets	: SET '(' filter_sets_l ')'	{ $$ = filter_opts; }
3113 		| SET filter_set		{ $$ = filter_opts; }
3114 		;
3115 
3116 filter_sets_l	: filter_sets_l comma filter_set
3117 		| filter_set
3118 		;
3119 
3120 filter_set	: prio {
3121 			if (filter_opts.marker & FOM_SETPRIO) {
3122 				yyerror("prio cannot be redefined");
3123 				YYERROR;
3124 			}
3125 			filter_opts.marker |= FOM_SETPRIO;
3126 			filter_opts.set_prio[0] = $1.b1;
3127 			filter_opts.set_prio[1] = $1.b2;
3128 		}
3129 		| TOS tos {
3130 			if (filter_opts.marker & FOM_SETTOS) {
3131 				yyerror("tos cannot be respecified");
3132 				YYERROR;
3133 			}
3134 			filter_opts.marker |= FOM_SETTOS;
3135 			filter_opts.settos = $2;
3136 		}
3137 prio		: PRIO NUMBER {
3138 			if ($2 < 0 || $2 > PF_PRIO_MAX) {
3139 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
3140 				YYERROR;
3141 			}
3142 			$$.b1 = $$.b2 = $2;
3143 		}
3144 		| PRIO '(' NUMBER comma NUMBER ')' {
3145 			if ($3 < 0 || $3 > PF_PRIO_MAX ||
3146 			    $5 < 0 || $5 > PF_PRIO_MAX) {
3147 				yyerror("prio must be 0 - %u", PF_PRIO_MAX);
3148 				YYERROR;
3149 			}
3150 			$$.b1 = $3;
3151 			$$.b2 = $5;
3152 		}
3153 		;
3154 
3155 probability	: STRING				{
3156 			char	*e;
3157 			double	 p = strtod($1, &e);
3158 
3159 			if (*e == '%') {
3160 				p *= 0.01;
3161 				e++;
3162 			}
3163 			if (*e) {
3164 				yyerror("invalid probability: %s", $1);
3165 				free($1);
3166 				YYERROR;
3167 			}
3168 			free($1);
3169 			$$ = p;
3170 		}
3171 		| NUMBER				{
3172 			$$ = (double)$1;
3173 		}
3174 		;
3175 
3176 
3177 action		: PASS 			{
3178 			$$.b1 = PF_PASS;
3179 			$$.b2 = failpolicy;
3180 			$$.w = returnicmpdefault;
3181 			$$.w2 = returnicmp6default;
3182 		}
3183 		| MATCH			{ $$.b1 = PF_MATCH; $$.b2 = $$.w = 0; }
3184 		| BLOCK blockspec	{ $$ = $2; $$.b1 = PF_DROP; }
3185 		;
3186 
3187 blockspec	: /* empty */		{
3188 			$$.b2 = blockpolicy;
3189 			$$.w = returnicmpdefault;
3190 			$$.w2 = returnicmp6default;
3191 		}
3192 		| DROP			{
3193 			$$.b2 = PFRULE_DROP;
3194 			$$.w = 0;
3195 			$$.w2 = 0;
3196 		}
3197 		| RETURNRST		{
3198 			$$.b2 = PFRULE_RETURNRST;
3199 			$$.w = 0;
3200 			$$.w2 = 0;
3201 		}
3202 		| RETURNRST '(' TTL NUMBER ')'	{
3203 			if ($4 < 0 || $4 > 255) {
3204 				yyerror("illegal ttl value %d", $4);
3205 				YYERROR;
3206 			}
3207 			$$.b2 = PFRULE_RETURNRST;
3208 			$$.w = $4;
3209 			$$.w2 = 0;
3210 		}
3211 		| RETURNICMP		{
3212 			$$.b2 = PFRULE_RETURNICMP;
3213 			$$.w = returnicmpdefault;
3214 			$$.w2 = returnicmp6default;
3215 		}
3216 		| RETURNICMP6		{
3217 			$$.b2 = PFRULE_RETURNICMP;
3218 			$$.w = returnicmpdefault;
3219 			$$.w2 = returnicmp6default;
3220 		}
3221 		| RETURNICMP '(' reticmpspec ')'	{
3222 			$$.b2 = PFRULE_RETURNICMP;
3223 			$$.w = $3;
3224 			$$.w2 = returnicmpdefault;
3225 		}
3226 		| RETURNICMP6 '(' reticmp6spec ')'	{
3227 			$$.b2 = PFRULE_RETURNICMP;
3228 			$$.w = returnicmpdefault;
3229 			$$.w2 = $3;
3230 		}
3231 		| RETURNICMP '(' reticmpspec comma reticmp6spec ')' {
3232 			$$.b2 = PFRULE_RETURNICMP;
3233 			$$.w = $3;
3234 			$$.w2 = $5;
3235 		}
3236 		| RETURN {
3237 			$$.b2 = PFRULE_RETURN;
3238 			$$.w = returnicmpdefault;
3239 			$$.w2 = returnicmp6default;
3240 		}
3241 		;
3242 
3243 reticmpspec	: STRING			{
3244 			if (!($$ = parseicmpspec($1, AF_INET))) {
3245 				free($1);
3246 				YYERROR;
3247 			}
3248 			free($1);
3249 		}
3250 		| NUMBER			{
3251 			u_int8_t		icmptype;
3252 
3253 			if ($1 < 0 || $1 > 255) {
3254 				yyerror("invalid icmp code %lu", $1);
3255 				YYERROR;
3256 			}
3257 			icmptype = returnicmpdefault >> 8;
3258 			$$ = (icmptype << 8 | $1);
3259 		}
3260 		;
3261 
3262 reticmp6spec	: STRING			{
3263 			if (!($$ = parseicmpspec($1, AF_INET6))) {
3264 				free($1);
3265 				YYERROR;
3266 			}
3267 			free($1);
3268 		}
3269 		| NUMBER			{
3270 			u_int8_t		icmptype;
3271 
3272 			if ($1 < 0 || $1 > 255) {
3273 				yyerror("invalid icmp code %lu", $1);
3274 				YYERROR;
3275 			}
3276 			icmptype = returnicmp6default >> 8;
3277 			$$ = (icmptype << 8 | $1);
3278 		}
3279 		;
3280 
3281 dir		: /* empty */			{ $$ = PF_INOUT; }
3282 		| IN				{ $$ = PF_IN; }
3283 		| OUT				{ $$ = PF_OUT; }
3284 		;
3285 
3286 quick		: /* empty */			{ $$.quick = 0; }
3287 		| QUICK				{ $$.quick = 1; }
3288 		;
3289 
3290 logquick	: /* empty */	{ $$.log = 0; $$.quick = 0; $$.logif = 0; }
3291 		| log		{ $$ = $1; $$.quick = 0; }
3292 		| QUICK		{ $$.quick = 1; $$.log = 0; $$.logif = 0; }
3293 		| log QUICK	{ $$ = $1; $$.quick = 1; }
3294 		| QUICK log	{ $$ = $2; $$.quick = 1; }
3295 		;
3296 
3297 log		: LOG			{ $$.log = PF_LOG; $$.logif = 0; }
3298 		| LOG '(' logopts ')'	{
3299 			$$.log = PF_LOG | $3.log;
3300 			$$.logif = $3.logif;
3301 		}
3302 		;
3303 
3304 logopts		: logopt			{ $$ = $1; }
3305 		| logopts comma logopt		{
3306 			$$.log = $1.log | $3.log;
3307 			$$.logif = $3.logif;
3308 			if ($$.logif == 0)
3309 				$$.logif = $1.logif;
3310 		}
3311 		;
3312 
3313 logopt		: ALL		{ $$.log = PF_LOG_ALL; $$.logif = 0; }
3314 		| MATCHES		{ $$.log = PF_LOG_MATCHES; $$.logif = 0; }
3315 		| USER		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
3316 		| GROUP		{ $$.log = PF_LOG_SOCKET_LOOKUP; $$.logif = 0; }
3317 		| TO string	{
3318 			const char	*errstr;
3319 			u_int		 i;
3320 
3321 			$$.log = 0;
3322 			if (strncmp($2, "pflog", 5)) {
3323 				yyerror("%s: should be a pflog interface", $2);
3324 				free($2);
3325 				YYERROR;
3326 			}
3327 			i = strtonum($2 + 5, 0, 255, &errstr);
3328 			if (errstr) {
3329 				yyerror("%s: %s", $2, errstr);
3330 				free($2);
3331 				YYERROR;
3332 			}
3333 			free($2);
3334 			$$.logif = i;
3335 		}
3336 		;
3337 
3338 interface	: /* empty */			{ $$ = NULL; }
3339 		| ON if_item_not		{ $$ = $2; }
3340 		| ON '{' optnl if_list '}'	{ $$ = $4; }
3341 		;
3342 
3343 if_list		: if_item_not optnl		{ $$ = $1; }
3344 		| if_list comma if_item_not optnl	{
3345 			$1->tail->next = $3;
3346 			$1->tail = $3;
3347 			$$ = $1;
3348 		}
3349 		;
3350 
3351 if_item_not	: not if_item			{ $$ = $2; $$->not = $1; }
3352 		;
3353 
3354 if_item		: STRING			{
3355 			struct node_host	*n;
3356 
3357 			$$ = calloc(1, sizeof(struct node_if));
3358 			if ($$ == NULL)
3359 				err(1, "if_item: calloc");
3360 			if (strlcpy($$->ifname, $1, sizeof($$->ifname)) >=
3361 			    sizeof($$->ifname)) {
3362 				free($1);
3363 				free($$);
3364 				yyerror("interface name too long");
3365 				YYERROR;
3366 			}
3367 
3368 			if ((n = ifa_exists($1)) != NULL)
3369 				$$->ifa_flags = n->ifa_flags;
3370 
3371 			free($1);
3372 			$$->not = 0;
3373 			$$->next = NULL;
3374 			$$->tail = $$;
3375 		}
3376 		;
3377 
3378 af		: /* empty */			{ $$ = 0; }
3379 		| INET				{ $$ = AF_INET; }
3380 		| INET6				{ $$ = AF_INET6; }
3381 		;
3382 
3383 etherproto	: /* empty */				{ $$ = NULL; }
3384 		| PROTO etherproto_item			{ $$ = $2; }
3385 		| PROTO '{' optnl etherproto_list '}'	{ $$ = $4; }
3386 		;
3387 
3388 etherproto_list	: etherproto_item optnl			{ $$ = $1; }
3389 		| etherproto_list comma etherproto_item optnl	{
3390 			$1->tail->next = $3;
3391 			$1->tail = $3;
3392 			$$ = $1;
3393 		}
3394 		;
3395 
3396 etherproto_item	: etherprotoval		{
3397 			u_int16_t	pr;
3398 
3399 			pr = (u_int16_t)$1;
3400 			if (pr == 0) {
3401 				yyerror("proto 0 cannot be used");
3402 				YYERROR;
3403 			}
3404 			$$ = calloc(1, sizeof(struct node_proto));
3405 			if ($$ == NULL)
3406 				err(1, "proto_item: calloc");
3407 			$$->proto = pr;
3408 			$$->next = NULL;
3409 			$$->tail = $$;
3410 		}
3411 		;
3412 
3413 etherprotoval	: NUMBER			{
3414 			if ($1 < 0 || $1 > 65565) {
3415 				yyerror("protocol outside range");
3416 				YYERROR;
3417 			}
3418 		}
3419 		| STRING
3420 		{
3421 			if (!strncmp($1, "0x", 2)) {
3422 				if (sscanf($1, "0x%4x", &$$) != 1) {
3423 					free($1);
3424 					yyerror("invalid EtherType hex");
3425 					YYERROR;
3426 				}
3427 			} else {
3428 				yyerror("Symbolic EtherType not yet supported");
3429 			}
3430 		}
3431 		;
3432 
3433 proto		: /* empty */				{ $$ = NULL; }
3434 		| PROTO proto_item			{ $$ = $2; }
3435 		| PROTO '{' optnl proto_list '}'	{ $$ = $4; }
3436 		;
3437 
3438 proto_list	: proto_item optnl		{ $$ = $1; }
3439 		| proto_list comma proto_item optnl	{
3440 			$1->tail->next = $3;
3441 			$1->tail = $3;
3442 			$$ = $1;
3443 		}
3444 		;
3445 
3446 proto_item	: protoval			{
3447 			u_int8_t	pr;
3448 
3449 			pr = (u_int8_t)$1;
3450 			if (pr == 0) {
3451 				yyerror("proto 0 cannot be used");
3452 				YYERROR;
3453 			}
3454 			$$ = calloc(1, sizeof(struct node_proto));
3455 			if ($$ == NULL)
3456 				err(1, "proto_item: calloc");
3457 			$$->proto = pr;
3458 			$$->next = NULL;
3459 			$$->tail = $$;
3460 		}
3461 		;
3462 
3463 protoval	: STRING			{
3464 			struct protoent	*p;
3465 
3466 			p = getprotobyname($1);
3467 			if (p == NULL) {
3468 				yyerror("unknown protocol %s", $1);
3469 				free($1);
3470 				YYERROR;
3471 			}
3472 			$$ = p->p_proto;
3473 			free($1);
3474 		}
3475 		| NUMBER			{
3476 			if ($1 < 0 || $1 > 255) {
3477 				yyerror("protocol outside range");
3478 				YYERROR;
3479 			}
3480 		}
3481 		;
3482 
3483 l3fromto	: /* empty */			{
3484 			bzero(&$$, sizeof($$));
3485 		}
3486 		| L3 fromto			{
3487 			if ($2.src.host != NULL &&
3488 			    $2.src.host->addr.type != PF_ADDR_ADDRMASK &&
3489 			    $2.src.host->addr.type != PF_ADDR_TABLE) {
3490 				yyerror("from must be an address or table");
3491 				YYERROR;
3492 			}
3493 			if ($2.dst.host != NULL &&
3494 			    $2.dst.host->addr.type != PF_ADDR_ADDRMASK &&
3495 			    $2.dst.host->addr.type != PF_ADDR_TABLE) {
3496 				yyerror("to must be an address or table");
3497 				YYERROR;
3498 			}
3499 			$$ = $2;
3500 		}
3501 		;
3502 etherfromto	: ALL				{
3503 			$$.src = NULL;
3504 			$$.dst = NULL;
3505 		}
3506 		| etherfrom etherto		{
3507 			$$.src = $1.mac;
3508 			$$.dst = $2.mac;
3509 		}
3510 		;
3511 
3512 etherfrom	: /* emtpy */			{
3513 			bzero(&$$, sizeof($$));
3514 		}
3515 		| FROM macspec			{
3516 			$$.mac = $2;
3517 		}
3518 		;
3519 
3520 etherto		: /* empty */			{
3521 			bzero(&$$, sizeof($$));
3522 		}
3523 		| TO macspec			{
3524 			$$.mac = $2;
3525 		}
3526 		;
3527 
3528 mac		: string '/' NUMBER		{
3529 			$$ = node_mac_from_string_masklen($1, $3);
3530 			free($1);
3531 			if ($$ == NULL)
3532 				YYERROR;
3533 		}
3534 		| string			{
3535 			if (strchr($1, '&')) {
3536 				/* mac&mask */
3537 				char *mac = strtok($1, "&");
3538 				char *mask = strtok(NULL, "&");
3539 				$$ = node_mac_from_string_mask(mac, mask);
3540 			} else {
3541 				$$ = node_mac_from_string($1);
3542 			}
3543 			free($1);
3544 			if ($$ == NULL)
3545 				YYERROR;
3546 
3547 		}
3548 xmac		: not mac {
3549 			struct node_mac	*n;
3550 
3551 			for (n = $2; n != NULL; n = n->next)
3552 				n->neg = $1;
3553 			$$ = $2;
3554 		}
3555 		;
3556 macspec		: xmac {
3557 			$$ = $1;
3558 		}
3559 		| '{' optnl mac_list '}'
3560 		{
3561 			$$ = $3;
3562 		}
3563 		;
3564 mac_list	: xmac optnl {
3565 			$$ = $1;
3566 		}
3567 		| mac_list comma xmac {
3568 			if ($3 == NULL)
3569 				$$ = $1;
3570 			else if ($1 == NULL)
3571 				$$ = $3;
3572 			else {
3573 				$1->tail->next = $3;
3574 				$1->tail = $3->tail;
3575 				$$ = $1;
3576 			}
3577 		}
3578 
3579 fromto		: ALL				{
3580 			$$.src.host = NULL;
3581 			$$.src.port = NULL;
3582 			$$.dst.host = NULL;
3583 			$$.dst.port = NULL;
3584 			$$.src_os = NULL;
3585 		}
3586 		| from os to			{
3587 			$$.src = $1;
3588 			$$.src_os = $2;
3589 			$$.dst = $3;
3590 		}
3591 		;
3592 
3593 os		: /* empty */			{ $$ = NULL; }
3594 		| OS xos			{ $$ = $2; }
3595 		| OS '{' optnl os_list '}'	{ $$ = $4; }
3596 		;
3597 
3598 xos		: STRING {
3599 			$$ = calloc(1, sizeof(struct node_os));
3600 			if ($$ == NULL)
3601 				err(1, "os: calloc");
3602 			$$->os = $1;
3603 			$$->tail = $$;
3604 		}
3605 		;
3606 
3607 os_list		: xos optnl 			{ $$ = $1; }
3608 		| os_list comma xos optnl	{
3609 			$1->tail->next = $3;
3610 			$1->tail = $3;
3611 			$$ = $1;
3612 		}
3613 		;
3614 
3615 from		: /* empty */			{
3616 			$$.host = NULL;
3617 			$$.port = NULL;
3618 		}
3619 		| FROM ipportspec		{
3620 			$$ = $2;
3621 		}
3622 		;
3623 
3624 to		: /* empty */			{
3625 			$$.host = NULL;
3626 			$$.port = NULL;
3627 		}
3628 		| TO ipportspec		{
3629 			if (disallow_urpf_failed($2.host, "\"urpf-failed\" is "
3630 			    "not permitted in a destination address"))
3631 				YYERROR;
3632 			$$ = $2;
3633 		}
3634 		;
3635 
3636 ipportspec	: ipspec			{
3637 			$$.host = $1;
3638 			$$.port = NULL;
3639 		}
3640 		| ipspec PORT portspec		{
3641 			$$.host = $1;
3642 			$$.port = $3;
3643 		}
3644 		| PORT portspec			{
3645 			$$.host = NULL;
3646 			$$.port = $2;
3647 		}
3648 		;
3649 
3650 optnl		: '\n' optnl
3651 		|
3652 		;
3653 
3654 ipspec		: ANY				{ $$ = NULL; }
3655 		| xhost				{ $$ = $1; }
3656 		| '{' optnl host_list '}'	{ $$ = $3; }
3657 		;
3658 
3659 toipspec	: TO ipspec			{ $$ = $2; }
3660 		| /* empty */			{ $$ = NULL; }
3661 		;
3662 
3663 host_list	: ipspec optnl			{ $$ = $1; }
3664 		| host_list comma ipspec optnl	{
3665 			if ($1 == NULL) {
3666 				freehostlist($3);
3667 				$$ = $1;
3668 			} else if ($3 == NULL) {
3669 				freehostlist($1);
3670 				$$ = $3;
3671 			} else {
3672 				$1->tail->next = $3;
3673 				$1->tail = $3->tail;
3674 				$$ = $1;
3675 			}
3676 		}
3677 		;
3678 
3679 xhost		: not host			{
3680 			struct node_host	*n;
3681 
3682 			for (n = $2; n != NULL; n = n->next)
3683 				n->not = $1;
3684 			$$ = $2;
3685 		}
3686 		| not NOROUTE			{
3687 			$$ = calloc(1, sizeof(struct node_host));
3688 			if ($$ == NULL)
3689 				err(1, "xhost: calloc");
3690 			$$->addr.type = PF_ADDR_NOROUTE;
3691 			$$->next = NULL;
3692 			$$->not = $1;
3693 			$$->tail = $$;
3694 		}
3695 		| not URPFFAILED		{
3696 			$$ = calloc(1, sizeof(struct node_host));
3697 			if ($$ == NULL)
3698 				err(1, "xhost: calloc");
3699 			$$->addr.type = PF_ADDR_URPFFAILED;
3700 			$$->next = NULL;
3701 			$$->not = $1;
3702 			$$->tail = $$;
3703 		}
3704 		;
3705 
3706 host		: STRING			{
3707 			if (($$ = host($1)) == NULL)	{
3708 				/* error. "any" is handled elsewhere */
3709 				free($1);
3710 				yyerror("could not parse host specification");
3711 				YYERROR;
3712 			}
3713 			free($1);
3714 
3715 		}
3716 		| STRING '-' STRING		{
3717 			struct node_host *b, *e;
3718 
3719 			if ((b = host($1)) == NULL || (e = host($3)) == NULL) {
3720 				free($1);
3721 				free($3);
3722 				yyerror("could not parse host specification");
3723 				YYERROR;
3724 			}
3725 			if (b->af != e->af ||
3726 			    b->addr.type != PF_ADDR_ADDRMASK ||
3727 			    e->addr.type != PF_ADDR_ADDRMASK ||
3728 			    unmask(&b->addr.v.a.mask, b->af) !=
3729 			    (b->af == AF_INET ? 32 : 128) ||
3730 			    unmask(&e->addr.v.a.mask, e->af) !=
3731 			    (e->af == AF_INET ? 32 : 128) ||
3732 			    b->next != NULL || b->not ||
3733 			    e->next != NULL || e->not) {
3734 				free(b);
3735 				free(e);
3736 				free($1);
3737 				free($3);
3738 				yyerror("invalid address range");
3739 				YYERROR;
3740 			}
3741 			memcpy(&b->addr.v.a.mask, &e->addr.v.a.addr,
3742 			    sizeof(b->addr.v.a.mask));
3743 			b->addr.type = PF_ADDR_RANGE;
3744 			$$ = b;
3745 			free(e);
3746 			free($1);
3747 			free($3);
3748 		}
3749 		| STRING '/' NUMBER		{
3750 			char	*buf;
3751 
3752 			if (asprintf(&buf, "%s/%lld", $1, (long long)$3) == -1)
3753 				err(1, "host: asprintf");
3754 			free($1);
3755 			if (($$ = host(buf)) == NULL)	{
3756 				/* error. "any" is handled elsewhere */
3757 				free(buf);
3758 				yyerror("could not parse host specification");
3759 				YYERROR;
3760 			}
3761 			free(buf);
3762 		}
3763 		| NUMBER '/' NUMBER		{
3764 			char	*buf;
3765 
3766 			/* ie. for 10/8 parsing */
3767 #ifdef __FreeBSD__
3768 			if (asprintf(&buf, "%lld/%lld", (long long)$1, (long long)$3) == -1)
3769 #else
3770 			if (asprintf(&buf, "%lld/%lld", $1, $3) == -1)
3771 #endif
3772 				err(1, "host: asprintf");
3773 			if (($$ = host(buf)) == NULL)	{
3774 				/* error. "any" is handled elsewhere */
3775 				free(buf);
3776 				yyerror("could not parse host specification");
3777 				YYERROR;
3778 			}
3779 			free(buf);
3780 		}
3781 		| dynaddr
3782 		| dynaddr '/' NUMBER		{
3783 			struct node_host	*n;
3784 
3785 			if ($3 < 0 || $3 > 128) {
3786 				yyerror("bit number too big");
3787 				YYERROR;
3788 			}
3789 			$$ = $1;
3790 			for (n = $1; n != NULL; n = n->next)
3791 				set_ipmask(n, $3);
3792 		}
3793 		| '<' STRING '>'	{
3794 			if (strlen($2) >= PF_TABLE_NAME_SIZE) {
3795 				yyerror("table name '%s' too long", $2);
3796 				free($2);
3797 				YYERROR;
3798 			}
3799 			$$ = calloc(1, sizeof(struct node_host));
3800 			if ($$ == NULL)
3801 				err(1, "host: calloc");
3802 			$$->addr.type = PF_ADDR_TABLE;
3803 			if (strlcpy($$->addr.v.tblname, $2,
3804 			    sizeof($$->addr.v.tblname)) >=
3805 			    sizeof($$->addr.v.tblname))
3806 				errx(1, "host: strlcpy");
3807 			free($2);
3808 			$$->next = NULL;
3809 			$$->tail = $$;
3810 		}
3811 		;
3812 
3813 number		: NUMBER
3814 		| STRING		{
3815 			u_long	ulval;
3816 
3817 			if (atoul($1, &ulval) == -1) {
3818 				yyerror("%s is not a number", $1);
3819 				free($1);
3820 				YYERROR;
3821 			} else
3822 				$$ = ulval;
3823 			free($1);
3824 		}
3825 		;
3826 
3827 dynaddr		: '(' STRING ')'		{
3828 			int	 flags = 0;
3829 			char	*p, *op;
3830 
3831 			op = $2;
3832 			if (!isalpha(op[0])) {
3833 				yyerror("invalid interface name '%s'", op);
3834 				free(op);
3835 				YYERROR;
3836 			}
3837 			while ((p = strrchr($2, ':')) != NULL) {
3838 				if (!strcmp(p+1, "network"))
3839 					flags |= PFI_AFLAG_NETWORK;
3840 				else if (!strcmp(p+1, "broadcast"))
3841 					flags |= PFI_AFLAG_BROADCAST;
3842 				else if (!strcmp(p+1, "peer"))
3843 					flags |= PFI_AFLAG_PEER;
3844 				else if (!strcmp(p+1, "0"))
3845 					flags |= PFI_AFLAG_NOALIAS;
3846 				else {
3847 					yyerror("interface %s has bad modifier",
3848 					    $2);
3849 					free(op);
3850 					YYERROR;
3851 				}
3852 				*p = '\0';
3853 			}
3854 			if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) {
3855 				free(op);
3856 				yyerror("illegal combination of "
3857 				    "interface modifiers");
3858 				YYERROR;
3859 			}
3860 			$$ = calloc(1, sizeof(struct node_host));
3861 			if ($$ == NULL)
3862 				err(1, "address: calloc");
3863 			$$->af = 0;
3864 			set_ipmask($$, 128);
3865 			$$->addr.type = PF_ADDR_DYNIFTL;
3866 			$$->addr.iflags = flags;
3867 			if (strlcpy($$->addr.v.ifname, $2,
3868 			    sizeof($$->addr.v.ifname)) >=
3869 			    sizeof($$->addr.v.ifname)) {
3870 				free(op);
3871 				free($$);
3872 				yyerror("interface name too long");
3873 				YYERROR;
3874 			}
3875 			free(op);
3876 			$$->next = NULL;
3877 			$$->tail = $$;
3878 		}
3879 		;
3880 
3881 portspec	: port_item			{ $$ = $1; }
3882 		| '{' optnl port_list '}'	{ $$ = $3; }
3883 		;
3884 
3885 port_list	: port_item optnl		{ $$ = $1; }
3886 		| port_list comma port_item optnl	{
3887 			$1->tail->next = $3;
3888 			$1->tail = $3;
3889 			$$ = $1;
3890 		}
3891 		;
3892 
3893 port_item	: portrange			{
3894 			$$ = calloc(1, sizeof(struct node_port));
3895 			if ($$ == NULL)
3896 				err(1, "port_item: calloc");
3897 			$$->port[0] = $1.a;
3898 			$$->port[1] = $1.b;
3899 			if ($1.t)
3900 				$$->op = PF_OP_RRG;
3901 			else
3902 				$$->op = PF_OP_EQ;
3903 			$$->next = NULL;
3904 			$$->tail = $$;
3905 		}
3906 		| unaryop portrange	{
3907 			if ($2.t) {
3908 				yyerror("':' cannot be used with an other "
3909 				    "port operator");
3910 				YYERROR;
3911 			}
3912 			$$ = calloc(1, sizeof(struct node_port));
3913 			if ($$ == NULL)
3914 				err(1, "port_item: calloc");
3915 			$$->port[0] = $2.a;
3916 			$$->port[1] = $2.b;
3917 			$$->op = $1;
3918 			$$->next = NULL;
3919 			$$->tail = $$;
3920 		}
3921 		| portrange PORTBINARY portrange	{
3922 			if ($1.t || $3.t) {
3923 				yyerror("':' cannot be used with an other "
3924 				    "port operator");
3925 				YYERROR;
3926 			}
3927 			$$ = calloc(1, sizeof(struct node_port));
3928 			if ($$ == NULL)
3929 				err(1, "port_item: calloc");
3930 			$$->port[0] = $1.a;
3931 			$$->port[1] = $3.a;
3932 			$$->op = $2;
3933 			$$->next = NULL;
3934 			$$->tail = $$;
3935 		}
3936 		;
3937 
3938 portplain	: numberstring			{
3939 			if (parseport($1, &$$, 0) == -1) {
3940 				free($1);
3941 				YYERROR;
3942 			}
3943 			free($1);
3944 		}
3945 		;
3946 
3947 portrange	: numberstring			{
3948 			if (parseport($1, &$$, PPORT_RANGE) == -1) {
3949 				free($1);
3950 				YYERROR;
3951 			}
3952 			free($1);
3953 		}
3954 		;
3955 
3956 uids		: uid_item			{ $$ = $1; }
3957 		| '{' optnl uid_list '}'	{ $$ = $3; }
3958 		;
3959 
3960 uid_list	: uid_item optnl		{ $$ = $1; }
3961 		| uid_list comma uid_item optnl	{
3962 			$1->tail->next = $3;
3963 			$1->tail = $3;
3964 			$$ = $1;
3965 		}
3966 		;
3967 
3968 uid_item	: uid				{
3969 			$$ = calloc(1, sizeof(struct node_uid));
3970 			if ($$ == NULL)
3971 				err(1, "uid_item: calloc");
3972 			$$->uid[0] = $1;
3973 			$$->uid[1] = $1;
3974 			$$->op = PF_OP_EQ;
3975 			$$->next = NULL;
3976 			$$->tail = $$;
3977 		}
3978 		| unaryop uid			{
3979 			if ($2 == UID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
3980 				yyerror("user unknown requires operator = or "
3981 				    "!=");
3982 				YYERROR;
3983 			}
3984 			$$ = calloc(1, sizeof(struct node_uid));
3985 			if ($$ == NULL)
3986 				err(1, "uid_item: calloc");
3987 			$$->uid[0] = $2;
3988 			$$->uid[1] = $2;
3989 			$$->op = $1;
3990 			$$->next = NULL;
3991 			$$->tail = $$;
3992 		}
3993 		| uid PORTBINARY uid		{
3994 			if ($1 == UID_MAX || $3 == UID_MAX) {
3995 				yyerror("user unknown requires operator = or "
3996 				    "!=");
3997 				YYERROR;
3998 			}
3999 			$$ = calloc(1, sizeof(struct node_uid));
4000 			if ($$ == NULL)
4001 				err(1, "uid_item: calloc");
4002 			$$->uid[0] = $1;
4003 			$$->uid[1] = $3;
4004 			$$->op = $2;
4005 			$$->next = NULL;
4006 			$$->tail = $$;
4007 		}
4008 		;
4009 
4010 uid		: STRING			{
4011 			if (!strcmp($1, "unknown"))
4012 				$$ = UID_MAX;
4013 			else {
4014 				struct passwd	*pw;
4015 
4016 				if ((pw = getpwnam($1)) == NULL) {
4017 					yyerror("unknown user %s", $1);
4018 					free($1);
4019 					YYERROR;
4020 				}
4021 				$$ = pw->pw_uid;
4022 			}
4023 			free($1);
4024 		}
4025 		| NUMBER			{
4026 			if ($1 < 0 || $1 >= UID_MAX) {
4027 				yyerror("illegal uid value %lu", $1);
4028 				YYERROR;
4029 			}
4030 			$$ = $1;
4031 		}
4032 		;
4033 
4034 gids		: gid_item			{ $$ = $1; }
4035 		| '{' optnl gid_list '}'	{ $$ = $3; }
4036 		;
4037 
4038 gid_list	: gid_item optnl		{ $$ = $1; }
4039 		| gid_list comma gid_item optnl	{
4040 			$1->tail->next = $3;
4041 			$1->tail = $3;
4042 			$$ = $1;
4043 		}
4044 		;
4045 
4046 gid_item	: gid				{
4047 			$$ = calloc(1, sizeof(struct node_gid));
4048 			if ($$ == NULL)
4049 				err(1, "gid_item: calloc");
4050 			$$->gid[0] = $1;
4051 			$$->gid[1] = $1;
4052 			$$->op = PF_OP_EQ;
4053 			$$->next = NULL;
4054 			$$->tail = $$;
4055 		}
4056 		| unaryop gid			{
4057 			if ($2 == GID_MAX && $1 != PF_OP_EQ && $1 != PF_OP_NE) {
4058 				yyerror("group unknown requires operator = or "
4059 				    "!=");
4060 				YYERROR;
4061 			}
4062 			$$ = calloc(1, sizeof(struct node_gid));
4063 			if ($$ == NULL)
4064 				err(1, "gid_item: calloc");
4065 			$$->gid[0] = $2;
4066 			$$->gid[1] = $2;
4067 			$$->op = $1;
4068 			$$->next = NULL;
4069 			$$->tail = $$;
4070 		}
4071 		| gid PORTBINARY gid		{
4072 			if ($1 == GID_MAX || $3 == GID_MAX) {
4073 				yyerror("group unknown requires operator = or "
4074 				    "!=");
4075 				YYERROR;
4076 			}
4077 			$$ = calloc(1, sizeof(struct node_gid));
4078 			if ($$ == NULL)
4079 				err(1, "gid_item: calloc");
4080 			$$->gid[0] = $1;
4081 			$$->gid[1] = $3;
4082 			$$->op = $2;
4083 			$$->next = NULL;
4084 			$$->tail = $$;
4085 		}
4086 		;
4087 
4088 gid		: STRING			{
4089 			if (!strcmp($1, "unknown"))
4090 				$$ = GID_MAX;
4091 			else {
4092 				struct group	*grp;
4093 
4094 				if ((grp = getgrnam($1)) == NULL) {
4095 					yyerror("unknown group %s", $1);
4096 					free($1);
4097 					YYERROR;
4098 				}
4099 				$$ = grp->gr_gid;
4100 			}
4101 			free($1);
4102 		}
4103 		| NUMBER			{
4104 			if ($1 < 0 || $1 >= GID_MAX) {
4105 				yyerror("illegal gid value %lu", $1);
4106 				YYERROR;
4107 			}
4108 			$$ = $1;
4109 		}
4110 		;
4111 
4112 flag		: STRING			{
4113 			int	f;
4114 
4115 			if ((f = parse_flags($1)) < 0) {
4116 				yyerror("bad flags %s", $1);
4117 				free($1);
4118 				YYERROR;
4119 			}
4120 			free($1);
4121 			$$.b1 = f;
4122 		}
4123 		;
4124 
4125 flags		: FLAGS flag '/' flag	{ $$.b1 = $2.b1; $$.b2 = $4.b1; }
4126 		| FLAGS '/' flag	{ $$.b1 = 0; $$.b2 = $3.b1; }
4127 		| FLAGS ANY		{ $$.b1 = 0; $$.b2 = 0; }
4128 		;
4129 
4130 icmpspec	: ICMPTYPE icmp_item			{ $$ = $2; }
4131 		| ICMPTYPE '{' optnl icmp_list '}'	{ $$ = $4; }
4132 		| ICMP6TYPE icmp6_item			{ $$ = $2; }
4133 		| ICMP6TYPE '{' optnl icmp6_list '}'	{ $$ = $4; }
4134 		;
4135 
4136 icmp_list	: icmp_item optnl		{ $$ = $1; }
4137 		| icmp_list comma icmp_item optnl {
4138 			$1->tail->next = $3;
4139 			$1->tail = $3;
4140 			$$ = $1;
4141 		}
4142 		;
4143 
4144 icmp6_list	: icmp6_item optnl		{ $$ = $1; }
4145 		| icmp6_list comma icmp6_item optnl {
4146 			$1->tail->next = $3;
4147 			$1->tail = $3;
4148 			$$ = $1;
4149 		}
4150 		;
4151 
4152 icmp_item	: icmptype		{
4153 			$$ = calloc(1, sizeof(struct node_icmp));
4154 			if ($$ == NULL)
4155 				err(1, "icmp_item: calloc");
4156 			$$->type = $1;
4157 			$$->code = 0;
4158 			$$->proto = IPPROTO_ICMP;
4159 			$$->next = NULL;
4160 			$$->tail = $$;
4161 		}
4162 		| icmptype CODE STRING	{
4163 			const struct icmpcodeent	*p;
4164 
4165 			if ((p = geticmpcodebyname($1-1, $3, AF_INET)) == NULL) {
4166 				yyerror("unknown icmp-code %s", $3);
4167 				free($3);
4168 				YYERROR;
4169 			}
4170 
4171 			free($3);
4172 			$$ = calloc(1, sizeof(struct node_icmp));
4173 			if ($$ == NULL)
4174 				err(1, "icmp_item: calloc");
4175 			$$->type = $1;
4176 			$$->code = p->code + 1;
4177 			$$->proto = IPPROTO_ICMP;
4178 			$$->next = NULL;
4179 			$$->tail = $$;
4180 		}
4181 		| icmptype CODE NUMBER	{
4182 			if ($3 < 0 || $3 > 255) {
4183 				yyerror("illegal icmp-code %lu", $3);
4184 				YYERROR;
4185 			}
4186 			$$ = calloc(1, sizeof(struct node_icmp));
4187 			if ($$ == NULL)
4188 				err(1, "icmp_item: calloc");
4189 			$$->type = $1;
4190 			$$->code = $3 + 1;
4191 			$$->proto = IPPROTO_ICMP;
4192 			$$->next = NULL;
4193 			$$->tail = $$;
4194 		}
4195 		;
4196 
4197 icmp6_item	: icmp6type		{
4198 			$$ = calloc(1, sizeof(struct node_icmp));
4199 			if ($$ == NULL)
4200 				err(1, "icmp_item: calloc");
4201 			$$->type = $1;
4202 			$$->code = 0;
4203 			$$->proto = IPPROTO_ICMPV6;
4204 			$$->next = NULL;
4205 			$$->tail = $$;
4206 		}
4207 		| icmp6type CODE STRING	{
4208 			const struct icmpcodeent	*p;
4209 
4210 			if ((p = geticmpcodebyname($1-1, $3, AF_INET6)) == NULL) {
4211 				yyerror("unknown icmp6-code %s", $3);
4212 				free($3);
4213 				YYERROR;
4214 			}
4215 			free($3);
4216 
4217 			$$ = calloc(1, sizeof(struct node_icmp));
4218 			if ($$ == NULL)
4219 				err(1, "icmp_item: calloc");
4220 			$$->type = $1;
4221 			$$->code = p->code + 1;
4222 			$$->proto = IPPROTO_ICMPV6;
4223 			$$->next = NULL;
4224 			$$->tail = $$;
4225 		}
4226 		| icmp6type CODE NUMBER	{
4227 			if ($3 < 0 || $3 > 255) {
4228 				yyerror("illegal icmp-code %lu", $3);
4229 				YYERROR;
4230 			}
4231 			$$ = calloc(1, sizeof(struct node_icmp));
4232 			if ($$ == NULL)
4233 				err(1, "icmp_item: calloc");
4234 			$$->type = $1;
4235 			$$->code = $3 + 1;
4236 			$$->proto = IPPROTO_ICMPV6;
4237 			$$->next = NULL;
4238 			$$->tail = $$;
4239 		}
4240 		;
4241 
4242 icmptype	: STRING			{
4243 			const struct icmptypeent	*p;
4244 
4245 			if ((p = geticmptypebyname($1, AF_INET)) == NULL) {
4246 				yyerror("unknown icmp-type %s", $1);
4247 				free($1);
4248 				YYERROR;
4249 			}
4250 			$$ = p->type + 1;
4251 			free($1);
4252 		}
4253 		| NUMBER			{
4254 			if ($1 < 0 || $1 > 255) {
4255 				yyerror("illegal icmp-type %lu", $1);
4256 				YYERROR;
4257 			}
4258 			$$ = $1 + 1;
4259 		}
4260 		;
4261 
4262 icmp6type	: STRING			{
4263 			const struct icmptypeent	*p;
4264 
4265 			if ((p = geticmptypebyname($1, AF_INET6)) ==
4266 			    NULL) {
4267 				yyerror("unknown icmp6-type %s", $1);
4268 				free($1);
4269 				YYERROR;
4270 			}
4271 			$$ = p->type + 1;
4272 			free($1);
4273 		}
4274 		| NUMBER			{
4275 			if ($1 < 0 || $1 > 255) {
4276 				yyerror("illegal icmp6-type %lu", $1);
4277 				YYERROR;
4278 			}
4279 			$$ = $1 + 1;
4280 		}
4281 		;
4282 
4283 tos	: STRING			{
4284 			int val;
4285 			char *end;
4286 
4287 			if (map_tos($1, &val))
4288 				$$ = val;
4289 			else if ($1[0] == '0' && $1[1] == 'x') {
4290 				errno = 0;
4291 				$$ = strtoul($1, &end, 16);
4292 				if (errno || *end != '\0')
4293 					$$ = 256;
4294 			} else
4295 				$$ = 256;		/* flag bad argument */
4296 			if ($$ < 0 || $$ > 255) {
4297 				yyerror("illegal tos value %s", $1);
4298 				free($1);
4299 				YYERROR;
4300 			}
4301 			free($1);
4302 		}
4303 		| NUMBER			{
4304 			$$ = $1;
4305 			if ($$ < 0 || $$ > 255) {
4306 				yyerror("illegal tos value %lu", $1);
4307 				YYERROR;
4308 			}
4309 		}
4310 		;
4311 
4312 sourcetrack	: SOURCETRACK		{ $$ = PF_SRCTRACK; }
4313 		| SOURCETRACK GLOBAL	{ $$ = PF_SRCTRACK_GLOBAL; }
4314 		| SOURCETRACK RULE	{ $$ = PF_SRCTRACK_RULE; }
4315 		;
4316 
4317 statelock	: IFBOUND {
4318 			$$ = PFRULE_IFBOUND;
4319 		}
4320 		| FLOATING {
4321 			$$ = 0;
4322 		}
4323 		;
4324 
4325 keep		: NO STATE			{
4326 			$$.action = 0;
4327 			$$.options = NULL;
4328 		}
4329 		| KEEP STATE state_opt_spec	{
4330 			$$.action = PF_STATE_NORMAL;
4331 			$$.options = $3;
4332 		}
4333 		| MODULATE STATE state_opt_spec {
4334 			$$.action = PF_STATE_MODULATE;
4335 			$$.options = $3;
4336 		}
4337 		| SYNPROXY STATE state_opt_spec {
4338 			$$.action = PF_STATE_SYNPROXY;
4339 			$$.options = $3;
4340 		}
4341 		;
4342 
4343 flush		: /* empty */			{ $$ = 0; }
4344 		| FLUSH				{ $$ = PF_FLUSH; }
4345 		| FLUSH GLOBAL			{
4346 			$$ = PF_FLUSH | PF_FLUSH_GLOBAL;
4347 		}
4348 		;
4349 
4350 state_opt_spec	: '(' state_opt_list ')'	{ $$ = $2; }
4351 		| /* empty */			{ $$ = NULL; }
4352 		;
4353 
4354 state_opt_list	: state_opt_item		{ $$ = $1; }
4355 		| state_opt_list comma state_opt_item {
4356 			$1->tail->next = $3;
4357 			$1->tail = $3;
4358 			$$ = $1;
4359 		}
4360 		;
4361 
4362 state_opt_item	: MAXIMUM NUMBER		{
4363 			if ($2 < 0 || $2 > UINT_MAX) {
4364 				yyerror("only positive values permitted");
4365 				YYERROR;
4366 			}
4367 			$$ = calloc(1, sizeof(struct node_state_opt));
4368 			if ($$ == NULL)
4369 				err(1, "state_opt_item: calloc");
4370 			$$->type = PF_STATE_OPT_MAX;
4371 			$$->data.max_states = $2;
4372 			$$->next = NULL;
4373 			$$->tail = $$;
4374 		}
4375 		| NOSYNC				{
4376 			$$ = calloc(1, sizeof(struct node_state_opt));
4377 			if ($$ == NULL)
4378 				err(1, "state_opt_item: calloc");
4379 			$$->type = PF_STATE_OPT_NOSYNC;
4380 			$$->next = NULL;
4381 			$$->tail = $$;
4382 		}
4383 		| MAXSRCSTATES NUMBER			{
4384 			if ($2 < 0 || $2 > UINT_MAX) {
4385 				yyerror("only positive values permitted");
4386 				YYERROR;
4387 			}
4388 			$$ = calloc(1, sizeof(struct node_state_opt));
4389 			if ($$ == NULL)
4390 				err(1, "state_opt_item: calloc");
4391 			$$->type = PF_STATE_OPT_MAX_SRC_STATES;
4392 			$$->data.max_src_states = $2;
4393 			$$->next = NULL;
4394 			$$->tail = $$;
4395 		}
4396 		| MAXSRCCONN NUMBER			{
4397 			if ($2 < 0 || $2 > UINT_MAX) {
4398 				yyerror("only positive values permitted");
4399 				YYERROR;
4400 			}
4401 			$$ = calloc(1, sizeof(struct node_state_opt));
4402 			if ($$ == NULL)
4403 				err(1, "state_opt_item: calloc");
4404 			$$->type = PF_STATE_OPT_MAX_SRC_CONN;
4405 			$$->data.max_src_conn = $2;
4406 			$$->next = NULL;
4407 			$$->tail = $$;
4408 		}
4409 		| MAXSRCCONNRATE NUMBER '/' NUMBER	{
4410 			if ($2 < 0 || $2 > UINT_MAX ||
4411 			    $4 < 0 || $4 > UINT_MAX) {
4412 				yyerror("only positive values permitted");
4413 				YYERROR;
4414 			}
4415 			$$ = calloc(1, sizeof(struct node_state_opt));
4416 			if ($$ == NULL)
4417 				err(1, "state_opt_item: calloc");
4418 			$$->type = PF_STATE_OPT_MAX_SRC_CONN_RATE;
4419 			$$->data.max_src_conn_rate.limit = $2;
4420 			$$->data.max_src_conn_rate.seconds = $4;
4421 			$$->next = NULL;
4422 			$$->tail = $$;
4423 		}
4424 		| OVERLOAD '<' STRING '>' flush		{
4425 			if (strlen($3) >= PF_TABLE_NAME_SIZE) {
4426 				yyerror("table name '%s' too long", $3);
4427 				free($3);
4428 				YYERROR;
4429 			}
4430 			$$ = calloc(1, sizeof(struct node_state_opt));
4431 			if ($$ == NULL)
4432 				err(1, "state_opt_item: calloc");
4433 			if (strlcpy($$->data.overload.tblname, $3,
4434 			    PF_TABLE_NAME_SIZE) >= PF_TABLE_NAME_SIZE)
4435 				errx(1, "state_opt_item: strlcpy");
4436 			free($3);
4437 			$$->type = PF_STATE_OPT_OVERLOAD;
4438 			$$->data.overload.flush = $5;
4439 			$$->next = NULL;
4440 			$$->tail = $$;
4441 		}
4442 		| MAXSRCNODES NUMBER			{
4443 			if ($2 < 0 || $2 > UINT_MAX) {
4444 				yyerror("only positive values permitted");
4445 				YYERROR;
4446 			}
4447 			$$ = calloc(1, sizeof(struct node_state_opt));
4448 			if ($$ == NULL)
4449 				err(1, "state_opt_item: calloc");
4450 			$$->type = PF_STATE_OPT_MAX_SRC_NODES;
4451 			$$->data.max_src_nodes = $2;
4452 			$$->next = NULL;
4453 			$$->tail = $$;
4454 		}
4455 		| sourcetrack {
4456 			$$ = calloc(1, sizeof(struct node_state_opt));
4457 			if ($$ == NULL)
4458 				err(1, "state_opt_item: calloc");
4459 			$$->type = PF_STATE_OPT_SRCTRACK;
4460 			$$->data.src_track = $1;
4461 			$$->next = NULL;
4462 			$$->tail = $$;
4463 		}
4464 		| statelock {
4465 			$$ = calloc(1, sizeof(struct node_state_opt));
4466 			if ($$ == NULL)
4467 				err(1, "state_opt_item: calloc");
4468 			$$->type = PF_STATE_OPT_STATELOCK;
4469 			$$->data.statelock = $1;
4470 			$$->next = NULL;
4471 			$$->tail = $$;
4472 		}
4473 		| SLOPPY {
4474 			$$ = calloc(1, sizeof(struct node_state_opt));
4475 			if ($$ == NULL)
4476 				err(1, "state_opt_item: calloc");
4477 			$$->type = PF_STATE_OPT_SLOPPY;
4478 			$$->next = NULL;
4479 			$$->tail = $$;
4480 		}
4481 		| PFLOW {
4482 			$$ = calloc(1, sizeof(struct node_state_opt));
4483 			if ($$ == NULL)
4484 				err(1, "state_opt_item: calloc");
4485 			$$->type = PF_STATE_OPT_PFLOW;
4486 			$$->next = NULL;
4487 			$$->tail = $$;
4488 		}
4489 		| STRING NUMBER			{
4490 			int	i;
4491 
4492 			if ($2 < 0 || $2 > UINT_MAX) {
4493 				yyerror("only positive values permitted");
4494 				YYERROR;
4495 			}
4496 			for (i = 0; pf_timeouts[i].name &&
4497 			    strcmp(pf_timeouts[i].name, $1); ++i)
4498 				;	/* nothing */
4499 			if (!pf_timeouts[i].name) {
4500 				yyerror("illegal timeout name %s", $1);
4501 				free($1);
4502 				YYERROR;
4503 			}
4504 			if (strchr(pf_timeouts[i].name, '.') == NULL) {
4505 				yyerror("illegal state timeout %s", $1);
4506 				free($1);
4507 				YYERROR;
4508 			}
4509 			free($1);
4510 			$$ = calloc(1, sizeof(struct node_state_opt));
4511 			if ($$ == NULL)
4512 				err(1, "state_opt_item: calloc");
4513 			$$->type = PF_STATE_OPT_TIMEOUT;
4514 			$$->data.timeout.number = pf_timeouts[i].timeout;
4515 			$$->data.timeout.seconds = $2;
4516 			$$->next = NULL;
4517 			$$->tail = $$;
4518 		}
4519 		;
4520 
4521 label		: LABEL STRING			{
4522 			$$ = $2;
4523 		}
4524 		;
4525 
4526 etherqname	: QUEUE STRING				{
4527 			$$.qname = $2;
4528 		}
4529 		| QUEUE '(' STRING ')'			{
4530 			$$.qname = $3;
4531 		}
4532 		;
4533 
4534 qname		: QUEUE STRING				{
4535 			$$.qname = $2;
4536 			$$.pqname = NULL;
4537 		}
4538 		| QUEUE '(' STRING ')'			{
4539 			$$.qname = $3;
4540 			$$.pqname = NULL;
4541 		}
4542 		| QUEUE '(' STRING comma STRING ')'	{
4543 			$$.qname = $3;
4544 			$$.pqname = $5;
4545 		}
4546 		;
4547 
4548 no		: /* empty */			{ $$ = 0; }
4549 		| NO				{ $$ = 1; }
4550 		;
4551 
4552 portstar	: numberstring			{
4553 			if (parseport($1, &$$, PPORT_RANGE|PPORT_STAR) == -1) {
4554 				free($1);
4555 				YYERROR;
4556 			}
4557 			free($1);
4558 		}
4559 		;
4560 
4561 redirspec	: host				{ $$ = $1; }
4562 		| '{' optnl redir_host_list '}'	{ $$ = $3; }
4563 		;
4564 
4565 redir_host_list	: host optnl			{ $$ = $1; }
4566 		| redir_host_list comma host optnl {
4567 			$1->tail->next = $3;
4568 			$1->tail = $3->tail;
4569 			$$ = $1;
4570 		}
4571 		;
4572 
4573 redirpool	: /* empty */			{ $$ = NULL; }
4574 		| ARROW redirspec		{
4575 			$$ = calloc(1, sizeof(struct redirection));
4576 			if ($$ == NULL)
4577 				err(1, "redirection: calloc");
4578 			$$->host = $2;
4579 			$$->rport.a = $$->rport.b = $$->rport.t = 0;
4580 		}
4581 		| ARROW redirspec PORT portstar	{
4582 			$$ = calloc(1, sizeof(struct redirection));
4583 			if ($$ == NULL)
4584 				err(1, "redirection: calloc");
4585 			$$->host = $2;
4586 			$$->rport = $4;
4587 		}
4588 		;
4589 
4590 hashkey		: /* empty */
4591 		{
4592 			$$ = calloc(1, sizeof(struct pf_poolhashkey));
4593 			if ($$ == NULL)
4594 				err(1, "hashkey: calloc");
4595 			$$->key32[0] = arc4random();
4596 			$$->key32[1] = arc4random();
4597 			$$->key32[2] = arc4random();
4598 			$$->key32[3] = arc4random();
4599 		}
4600 		| string
4601 		{
4602 			if (!strncmp($1, "0x", 2)) {
4603 				if (strlen($1) != 34) {
4604 					free($1);
4605 					yyerror("hex key must be 128 bits "
4606 						"(32 hex digits) long");
4607 					YYERROR;
4608 				}
4609 				$$ = calloc(1, sizeof(struct pf_poolhashkey));
4610 				if ($$ == NULL)
4611 					err(1, "hashkey: calloc");
4612 
4613 				if (sscanf($1, "0x%8x%8x%8x%8x",
4614 				    &$$->key32[0], &$$->key32[1],
4615 				    &$$->key32[2], &$$->key32[3]) != 4) {
4616 					free($$);
4617 					free($1);
4618 					yyerror("invalid hex key");
4619 					YYERROR;
4620 				}
4621 			} else {
4622 				MD5_CTX	context;
4623 
4624 				$$ = calloc(1, sizeof(struct pf_poolhashkey));
4625 				if ($$ == NULL)
4626 					err(1, "hashkey: calloc");
4627 				MD5Init(&context);
4628 				MD5Update(&context, (unsigned char *)$1,
4629 				    strlen($1));
4630 				MD5Final((unsigned char *)$$, &context);
4631 				HTONL($$->key32[0]);
4632 				HTONL($$->key32[1]);
4633 				HTONL($$->key32[2]);
4634 				HTONL($$->key32[3]);
4635 			}
4636 			free($1);
4637 		}
4638 		;
4639 
4640 pool_opts	:	{ bzero(&pool_opts, sizeof pool_opts); }
4641 		    pool_opts_l
4642 			{ $$ = pool_opts; }
4643 		| /* empty */	{
4644 			bzero(&pool_opts, sizeof pool_opts);
4645 			$$ = pool_opts;
4646 		}
4647 		;
4648 
4649 pool_opts_l	: pool_opts_l pool_opt
4650 		| pool_opt
4651 		;
4652 
4653 pool_opt	: BITMASK	{
4654 			if (pool_opts.type) {
4655 				yyerror("pool type cannot be redefined");
4656 				YYERROR;
4657 			}
4658 			pool_opts.type =  PF_POOL_BITMASK;
4659 		}
4660 		| RANDOM	{
4661 			if (pool_opts.type) {
4662 				yyerror("pool type cannot be redefined");
4663 				YYERROR;
4664 			}
4665 			pool_opts.type = PF_POOL_RANDOM;
4666 		}
4667 		| SOURCEHASH hashkey {
4668 			if (pool_opts.type) {
4669 				yyerror("pool type cannot be redefined");
4670 				YYERROR;
4671 			}
4672 			pool_opts.type = PF_POOL_SRCHASH;
4673 			pool_opts.key = $2;
4674 		}
4675 		| ROUNDROBIN	{
4676 			if (pool_opts.type) {
4677 				yyerror("pool type cannot be redefined");
4678 				YYERROR;
4679 			}
4680 			pool_opts.type = PF_POOL_ROUNDROBIN;
4681 		}
4682 		| STATICPORT	{
4683 			if (pool_opts.staticport) {
4684 				yyerror("static-port cannot be redefined");
4685 				YYERROR;
4686 			}
4687 			pool_opts.staticport = 1;
4688 		}
4689 		| STICKYADDRESS	{
4690 			if (pool_opts.marker & POM_STICKYADDRESS) {
4691 				yyerror("sticky-address cannot be redefined");
4692 				YYERROR;
4693 			}
4694 			pool_opts.marker |= POM_STICKYADDRESS;
4695 			pool_opts.opts |= PF_POOL_STICKYADDR;
4696 		}
4697 		| ENDPI {
4698 			if (pool_opts.marker & POM_ENDPI) {
4699 				yyerror("endpoint-independent cannot be redefined");
4700 				YYERROR;
4701 			}
4702 			pool_opts.marker |= POM_ENDPI;
4703 			pool_opts.opts |= PF_POOL_ENDPI;
4704 		}
4705 		| MAPEPORTSET number '/' number '/' number {
4706 			if (pool_opts.mape.offset) {
4707 				yyerror("map-e-portset cannot be redefined");
4708 				YYERROR;
4709 			}
4710 			if (pool_opts.type) {
4711 				yyerror("map-e-portset cannot be used with "
4712 					"address pools");
4713 				YYERROR;
4714 			}
4715 			if ($2 <= 0 || $2 >= 16) {
4716 				yyerror("MAP-E PSID offset must be 1-15");
4717 				YYERROR;
4718 			}
4719 			if ($4 < 0 || $4 >= 16 || $2 + $4 > 16) {
4720 				yyerror("Invalid MAP-E PSID length");
4721 				YYERROR;
4722 			} else if ($4 == 0) {
4723 				yyerror("PSID Length = 0: this means"
4724 				    " you do not need MAP-E");
4725 				YYERROR;
4726 			}
4727 			if ($6 < 0 || $6 > 65535) {
4728 				yyerror("Invalid MAP-E PSID");
4729 				YYERROR;
4730 			}
4731 			pool_opts.mape.offset = $2;
4732 			pool_opts.mape.psidlen = $4;
4733 			pool_opts.mape.psid = $6;
4734 		}
4735 		;
4736 
4737 redirection	: /* empty */			{ $$ = NULL; }
4738 		| ARROW host			{
4739 			$$ = calloc(1, sizeof(struct redirection));
4740 			if ($$ == NULL)
4741 				err(1, "redirection: calloc");
4742 			$$->host = $2;
4743 			$$->rport.a = $$->rport.b = $$->rport.t = 0;
4744 		}
4745 		| ARROW host PORT portstar	{
4746 			$$ = calloc(1, sizeof(struct redirection));
4747 			if ($$ == NULL)
4748 				err(1, "redirection: calloc");
4749 			$$->host = $2;
4750 			$$->rport = $4;
4751 		}
4752 		;
4753 
4754 natpasslog	: /* empty */	{ $$.b1 = $$.b2 = 0; $$.w2 = 0; }
4755 		| PASS		{ $$.b1 = 1; $$.b2 = 0; $$.w2 = 0; }
4756 		| PASS log	{ $$.b1 = 1; $$.b2 = $2.log; $$.w2 = $2.logif; }
4757 		| log		{ $$.b1 = 0; $$.b2 = $1.log; $$.w2 = $1.logif; }
4758 		;
4759 
4760 nataction	: no NAT natpasslog {
4761 			if ($1 && $3.b1) {
4762 				yyerror("\"pass\" not valid with \"no\"");
4763 				YYERROR;
4764 			}
4765 			if ($1)
4766 				$$.b1 = PF_NONAT;
4767 			else
4768 				$$.b1 = PF_NAT;
4769 			$$.b2 = $3.b1;
4770 			$$.w = $3.b2;
4771 			$$.w2 = $3.w2;
4772 		}
4773 		| no RDR natpasslog {
4774 			if ($1 && $3.b1) {
4775 				yyerror("\"pass\" not valid with \"no\"");
4776 				YYERROR;
4777 			}
4778 			if ($1)
4779 				$$.b1 = PF_NORDR;
4780 			else
4781 				$$.b1 = PF_RDR;
4782 			$$.b2 = $3.b1;
4783 			$$.w = $3.b2;
4784 			$$.w2 = $3.w2;
4785 		}
4786 		;
4787 
4788 natrule		: nataction interface af proto fromto tag tagged rtable
4789 		    redirpool pool_opts
4790 		{
4791 			struct pfctl_rule	r;
4792 			struct node_state_opt	*o;
4793 
4794 			if (check_rulestate(PFCTL_STATE_NAT))
4795 				YYERROR;
4796 
4797 			memset(&r, 0, sizeof(r));
4798 
4799 			r.action = $1.b1;
4800 			r.natpass = $1.b2;
4801 			r.log = $1.w;
4802 			r.logif = $1.w2;
4803 			r.af = $3;
4804 
4805 			if (!r.af) {
4806 				if ($5.src.host && $5.src.host->af &&
4807 				    !$5.src.host->ifindex)
4808 					r.af = $5.src.host->af;
4809 				else if ($5.dst.host && $5.dst.host->af &&
4810 				    !$5.dst.host->ifindex)
4811 					r.af = $5.dst.host->af;
4812 			}
4813 
4814 			if ($6 != NULL)
4815 				if (strlcpy(r.tagname, $6, PF_TAG_NAME_SIZE) >=
4816 				    PF_TAG_NAME_SIZE) {
4817 					yyerror("tag too long, max %u chars",
4818 					    PF_TAG_NAME_SIZE - 1);
4819 					YYERROR;
4820 				}
4821 
4822 			if ($7.name)
4823 				if (strlcpy(r.match_tagname, $7.name,
4824 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
4825 					yyerror("tag too long, max %u chars",
4826 					    PF_TAG_NAME_SIZE - 1);
4827 					YYERROR;
4828 				}
4829 			r.match_tag_not = $7.neg;
4830 			r.rtableid = $8;
4831 
4832 			if (r.action == PF_NONAT || r.action == PF_NORDR) {
4833 				if ($9 != NULL) {
4834 					yyerror("translation rule with 'no' "
4835 					    "does not need '->'");
4836 					YYERROR;
4837 				}
4838 			} else {
4839 				if ($9 == NULL || $9->host == NULL) {
4840 					yyerror("translation rule requires '-> "
4841 					    "address'");
4842 					YYERROR;
4843 				}
4844 				if (!r.af && ! $9->host->ifindex)
4845 					r.af = $9->host->af;
4846 
4847 				remove_invalid_hosts(&$9->host, &r.af);
4848 				if (invalid_redirect($9->host, r.af))
4849 					YYERROR;
4850 				if ($9->host->addr.type == PF_ADDR_DYNIFTL) {
4851 					if (($9->host = gen_dynnode($9->host, r.af)) == NULL)
4852 						err(1, "calloc");
4853 				}
4854 				if (check_netmask($9->host, r.af))
4855 					YYERROR;
4856 
4857 				r.rpool.proxy_port[0] = ntohs($9->rport.a);
4858 
4859 				switch (r.action) {
4860 				case PF_RDR:
4861 					if (!$9->rport.b && $9->rport.t &&
4862 					    $5.dst.port != NULL) {
4863 						r.rpool.proxy_port[1] =
4864 						    ntohs($9->rport.a) +
4865 						    (ntohs(
4866 						    $5.dst.port->port[1]) -
4867 						    ntohs(
4868 						    $5.dst.port->port[0]));
4869 					} else
4870 						r.rpool.proxy_port[1] =
4871 						    ntohs($9->rport.b);
4872 					break;
4873 				case PF_NAT:
4874 					r.rpool.proxy_port[1] =
4875 					    ntohs($9->rport.b);
4876 					if (!r.rpool.proxy_port[0] &&
4877 					    !r.rpool.proxy_port[1]) {
4878 						r.rpool.proxy_port[0] =
4879 						    PF_NAT_PROXY_PORT_LOW;
4880 						r.rpool.proxy_port[1] =
4881 						    PF_NAT_PROXY_PORT_HIGH;
4882 					} else if (!r.rpool.proxy_port[1])
4883 						r.rpool.proxy_port[1] =
4884 						    r.rpool.proxy_port[0];
4885 					break;
4886 				default:
4887 					break;
4888 				}
4889 
4890 				r.rpool.opts = $10.type;
4891 				if ((r.rpool.opts & PF_POOL_TYPEMASK) ==
4892 				    PF_POOL_NONE && ($9->host->next != NULL ||
4893 				    $9->host->addr.type == PF_ADDR_TABLE ||
4894 				    DYNIF_MULTIADDR($9->host->addr)))
4895 					r.rpool.opts = PF_POOL_ROUNDROBIN;
4896 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4897 				    PF_POOL_ROUNDROBIN &&
4898 				    disallow_table($9->host, "tables are only "
4899 				    "supported in round-robin redirection "
4900 				    "pools"))
4901 					YYERROR;
4902 				if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4903 				    PF_POOL_ROUNDROBIN &&
4904 				    disallow_alias($9->host, "interface (%s) "
4905 				    "is only supported in round-robin "
4906 				    "redirection pools"))
4907 					YYERROR;
4908 				if ($9->host->next != NULL) {
4909 					if ((r.rpool.opts & PF_POOL_TYPEMASK) !=
4910 					    PF_POOL_ROUNDROBIN) {
4911 						yyerror("only round-robin "
4912 						    "valid for multiple "
4913 						    "redirection addresses");
4914 						YYERROR;
4915 					}
4916 				}
4917 			}
4918 
4919 			if ($10.key != NULL)
4920 				memcpy(&r.rpool.key, $10.key,
4921 				    sizeof(struct pf_poolhashkey));
4922 
4923 			 if ($10.opts)
4924 				r.rpool.opts |= $10.opts;
4925 
4926 			if ($10.staticport) {
4927 				if (r.action != PF_NAT) {
4928 					yyerror("the 'static-port' option is "
4929 					    "only valid with nat rules");
4930 					YYERROR;
4931 				}
4932 				if (r.rpool.proxy_port[0] !=
4933 				    PF_NAT_PROXY_PORT_LOW &&
4934 				    r.rpool.proxy_port[1] !=
4935 				    PF_NAT_PROXY_PORT_HIGH) {
4936 					yyerror("the 'static-port' option can't"
4937 					    " be used when specifying a port"
4938 					    " range");
4939 					YYERROR;
4940 				}
4941 				r.rpool.proxy_port[0] = 0;
4942 				r.rpool.proxy_port[1] = 0;
4943 			}
4944 
4945 			if ($10.mape.offset) {
4946 				if (r.action != PF_NAT) {
4947 					yyerror("the 'map-e-portset' option is"
4948 					    " only valid with nat rules");
4949 					YYERROR;
4950 				}
4951 				if ($10.staticport) {
4952 					yyerror("the 'map-e-portset' option"
4953 					    " can't be used 'static-port'");
4954 					YYERROR;
4955 				}
4956 				if (r.rpool.proxy_port[0] !=
4957 				    PF_NAT_PROXY_PORT_LOW &&
4958 				    r.rpool.proxy_port[1] !=
4959 				    PF_NAT_PROXY_PORT_HIGH) {
4960 					yyerror("the 'map-e-portset' option"
4961 					    " can't be used when specifying"
4962 					    " a port range");
4963 					YYERROR;
4964 				}
4965 				r.rpool.mape = $10.mape;
4966 			}
4967 
4968 			o = keep_state_defaults;
4969 			while (o) {
4970 				switch (o->type) {
4971 				case PF_STATE_OPT_PFLOW:
4972 					if (r.rule_flag & PFRULE_PFLOW) {
4973 						yyerror("state pflow option: "
4974 						    "multiple definitions");
4975 						YYERROR;
4976 					}
4977 					r.rule_flag |= PFRULE_PFLOW;
4978 					break;
4979 				}
4980 				o = o->next;
4981 			}
4982 
4983 			expand_rule(&r, $2, $9 == NULL ? NULL : $9->host, NULL, $4,
4984 			    $5.src_os, $5.src.host, $5.src.port, $5.dst.host,
4985 			    $5.dst.port, 0, 0, 0, 0, "");
4986 			free($9);
4987 		}
4988 		;
4989 
4990 binatrule	: no BINAT natpasslog interface af proto FROM ipspec toipspec tag
4991 		    tagged rtable redirection
4992 		{
4993 			struct pfctl_rule	binat;
4994 			struct pf_pooladdr	*pa;
4995 
4996 			if (check_rulestate(PFCTL_STATE_NAT))
4997 				YYERROR;
4998 			if (disallow_urpf_failed($9, "\"urpf-failed\" is not "
4999 			    "permitted as a binat destination"))
5000 				YYERROR;
5001 
5002 			memset(&binat, 0, sizeof(binat));
5003 
5004 			if ($1 && $3.b1) {
5005 				yyerror("\"pass\" not valid with \"no\"");
5006 				YYERROR;
5007 			}
5008 			if ($1)
5009 				binat.action = PF_NOBINAT;
5010 			else
5011 				binat.action = PF_BINAT;
5012 			binat.natpass = $3.b1;
5013 			binat.log = $3.b2;
5014 			binat.logif = $3.w2;
5015 			binat.af = $5;
5016 			if (!binat.af && $8 != NULL && $8->af)
5017 				binat.af = $8->af;
5018 			if (!binat.af && $9 != NULL && $9->af)
5019 				binat.af = $9->af;
5020 
5021 			if (!binat.af && $13 != NULL && $13->host)
5022 				binat.af = $13->host->af;
5023 			if (!binat.af) {
5024 				yyerror("address family (inet/inet6) "
5025 				    "undefined");
5026 				YYERROR;
5027 			}
5028 
5029 			if ($4 != NULL) {
5030 				memcpy(binat.ifname, $4->ifname,
5031 				    sizeof(binat.ifname));
5032 				binat.ifnot = $4->not;
5033 				free($4);
5034 			}
5035 
5036 			if ($10 != NULL)
5037 				if (strlcpy(binat.tagname, $10,
5038 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
5039 					yyerror("tag too long, max %u chars",
5040 					    PF_TAG_NAME_SIZE - 1);
5041 					YYERROR;
5042 				}
5043 			if ($11.name)
5044 				if (strlcpy(binat.match_tagname, $11.name,
5045 				    PF_TAG_NAME_SIZE) >= PF_TAG_NAME_SIZE) {
5046 					yyerror("tag too long, max %u chars",
5047 					    PF_TAG_NAME_SIZE - 1);
5048 					YYERROR;
5049 				}
5050 			binat.match_tag_not = $11.neg;
5051 			binat.rtableid = $12;
5052 
5053 			if ($6 != NULL) {
5054 				binat.proto = $6->proto;
5055 				free($6);
5056 			}
5057 
5058 			if ($8 != NULL && disallow_table($8, "invalid use of "
5059 			    "table <%s> as the source address of a binat rule"))
5060 				YYERROR;
5061 			if ($8 != NULL && disallow_alias($8, "invalid use of "
5062 			    "interface (%s) as the source address of a binat "
5063 			    "rule"))
5064 				YYERROR;
5065 			if ($13 != NULL && $13->host != NULL && disallow_table(
5066 			    $13->host, "invalid use of table <%s> as the "
5067 			    "redirect address of a binat rule"))
5068 				YYERROR;
5069 			if ($13 != NULL && $13->host != NULL && disallow_alias(
5070 			    $13->host, "invalid use of interface (%s) as the "
5071 			    "redirect address of a binat rule"))
5072 				YYERROR;
5073 
5074 			if ($8 != NULL) {
5075 				if ($8->next) {
5076 					yyerror("multiple binat ip addresses");
5077 					YYERROR;
5078 				}
5079 				if ($8->addr.type == PF_ADDR_DYNIFTL)
5080 					$8->af = binat.af;
5081 				if ($8->af != binat.af) {
5082 					yyerror("binat ip versions must match");
5083 					YYERROR;
5084 				}
5085 				if ($8->addr.type == PF_ADDR_DYNIFTL) {
5086 					if (($8 = gen_dynnode($8, binat.af)) == NULL)
5087 						err(1, "calloc");
5088 				}
5089 				if (check_netmask($8, binat.af))
5090 					YYERROR;
5091 				memcpy(&binat.src.addr, &$8->addr,
5092 				    sizeof(binat.src.addr));
5093 				free($8);
5094 			}
5095 			if ($9 != NULL) {
5096 				if ($9->next) {
5097 					yyerror("multiple binat ip addresses");
5098 					YYERROR;
5099 				}
5100 				if ($9->af != binat.af && $9->af) {
5101 					yyerror("binat ip versions must match");
5102 					YYERROR;
5103 				}
5104 				if ($9->addr.type == PF_ADDR_DYNIFTL) {
5105 					if (($9 = gen_dynnode($9, binat.af)) == NULL)
5106 						err(1, "calloc");
5107 				}
5108 				if (check_netmask($9, binat.af))
5109 					YYERROR;
5110 				memcpy(&binat.dst.addr, &$9->addr,
5111 				    sizeof(binat.dst.addr));
5112 				binat.dst.neg = $9->not;
5113 				free($9);
5114 			}
5115 
5116 			if (binat.action == PF_NOBINAT) {
5117 				if ($13 != NULL) {
5118 					yyerror("'no binat' rule does not need"
5119 					    " '->'");
5120 					YYERROR;
5121 				}
5122 			} else {
5123 				if ($13 == NULL || $13->host == NULL) {
5124 					yyerror("'binat' rule requires"
5125 					    " '-> address'");
5126 					YYERROR;
5127 				}
5128 
5129 				remove_invalid_hosts(&$13->host, &binat.af);
5130 				if (invalid_redirect($13->host, binat.af))
5131 					YYERROR;
5132 				if ($13->host->next != NULL) {
5133 					yyerror("binat rule must redirect to "
5134 					    "a single address");
5135 					YYERROR;
5136 				}
5137 				if ($13->host->addr.type == PF_ADDR_DYNIFTL) {
5138 					if (($13->host = gen_dynnode($13->host, binat.af)) == NULL)
5139 						err(1, "calloc");
5140 				}
5141 				if (check_netmask($13->host, binat.af))
5142 					YYERROR;
5143 
5144 				if (!PF_AZERO(&binat.src.addr.v.a.mask,
5145 				    binat.af) &&
5146 				    !PF_AEQ(&binat.src.addr.v.a.mask,
5147 				    &$13->host->addr.v.a.mask, binat.af)) {
5148 					yyerror("'binat' source mask and "
5149 					    "redirect mask must be the same");
5150 					YYERROR;
5151 				}
5152 
5153 				TAILQ_INIT(&binat.rpool.list);
5154 				pa = calloc(1, sizeof(struct pf_pooladdr));
5155 				if (pa == NULL)
5156 					err(1, "binat: calloc");
5157 				pa->addr = $13->host->addr;
5158 				pa->ifname[0] = 0;
5159 				TAILQ_INSERT_TAIL(&binat.rpool.list,
5160 				    pa, entries);
5161 
5162 				free($13);
5163 			}
5164 
5165 			pfctl_append_rule(pf, &binat, "");
5166 		}
5167 		;
5168 
5169 tag		: /* empty */		{ $$ = NULL; }
5170 		| TAG STRING		{ $$ = $2; }
5171 		;
5172 
5173 tagged		: /* empty */		{ $$.neg = 0; $$.name = NULL; }
5174 		| not TAGGED string	{ $$.neg = $1; $$.name = $3; }
5175 		;
5176 
5177 rtable		: /* empty */		{ $$ = -1; }
5178 		| RTABLE NUMBER		{
5179 			if ($2 < 0 || $2 > rt_tableid_max()) {
5180 				yyerror("invalid rtable id");
5181 				YYERROR;
5182 			}
5183 			$$ = $2;
5184 		}
5185 		;
5186 
5187 route_host	: STRING			{
5188 			$$ = calloc(1, sizeof(struct node_host));
5189 			if ($$ == NULL)
5190 				err(1, "route_host: calloc");
5191 			if (strlen($1) >= IFNAMSIZ) {
5192 				yyerror("interface name too long");
5193 				YYERROR;
5194 			}
5195 			$$->ifname = strdup($1);
5196 			set_ipmask($$, 128);
5197 			$$->next = NULL;
5198 			$$->tail = $$;
5199 		}
5200 		| '(' STRING host ')'		{
5201 			struct node_host *n;
5202 
5203 			$$ = $3;
5204 			for (n = $3; n != NULL; n = n->next) {
5205 				if (strlen($2) >= IFNAMSIZ) {
5206 					yyerror("interface name too long");
5207 					YYERROR;
5208 				}
5209 				n->ifname = strdup($2);
5210 			}
5211 		}
5212 		;
5213 
5214 route_host_list	: route_host optnl			{ $$ = $1; }
5215 		| route_host_list comma route_host optnl {
5216 			if ($1->af == 0)
5217 				$1->af = $3->af;
5218 			if ($1->af != $3->af) {
5219 				yyerror("all pool addresses must be in the "
5220 				    "same address family");
5221 				YYERROR;
5222 			}
5223 			$1->tail->next = $3;
5224 			$1->tail = $3->tail;
5225 			$$ = $1;
5226 		}
5227 		;
5228 
5229 routespec	: route_host			{ $$ = $1; }
5230 		| '{' optnl route_host_list '}'	{ $$ = $3; }
5231 		;
5232 
5233 route		: /* empty */			{
5234 			$$.host = NULL;
5235 			$$.rt = 0;
5236 			$$.pool_opts = 0;
5237 		}
5238 		| FASTROUTE {
5239 			/* backwards-compat */
5240 			$$.host = NULL;
5241 			$$.rt = 0;
5242 			$$.pool_opts = 0;
5243 		}
5244 		| ROUTETO routespec pool_opts {
5245 			$$.host = $2;
5246 			$$.rt = PF_ROUTETO;
5247 			$$.pool_opts = $3.type | $3.opts;
5248 			if ($3.key != NULL)
5249 				$$.key = $3.key;
5250 		}
5251 		| REPLYTO routespec pool_opts {
5252 			$$.host = $2;
5253 			$$.rt = PF_REPLYTO;
5254 			$$.pool_opts = $3.type | $3.opts;
5255 			if ($3.key != NULL)
5256 				$$.key = $3.key;
5257 		}
5258 		| DUPTO routespec pool_opts {
5259 			$$.host = $2;
5260 			$$.rt = PF_DUPTO;
5261 			$$.pool_opts = $3.type | $3.opts;
5262 			if ($3.key != NULL)
5263 				$$.key = $3.key;
5264 		}
5265 		;
5266 
5267 timeout_spec	: STRING NUMBER
5268 		{
5269 			if (check_rulestate(PFCTL_STATE_OPTION)) {
5270 				free($1);
5271 				YYERROR;
5272 			}
5273 			if ($2 < 0 || $2 > UINT_MAX) {
5274 				yyerror("only positive values permitted");
5275 				YYERROR;
5276 			}
5277 			if (pfctl_apply_timeout(pf, $1, $2, 0) != 0) {
5278 				yyerror("unknown timeout %s", $1);
5279 				free($1);
5280 				YYERROR;
5281 			}
5282 			free($1);
5283 		}
5284 		| INTERVAL NUMBER		{
5285 			if (check_rulestate(PFCTL_STATE_OPTION))
5286 				YYERROR;
5287 			if ($2 < 0 || $2 > UINT_MAX) {
5288 				yyerror("only positive values permitted");
5289 				YYERROR;
5290 			}
5291 			if (pfctl_apply_timeout(pf, "interval", $2, 0) != 0)
5292 				YYERROR;
5293 		}
5294 		;
5295 
5296 timeout_list	: timeout_list comma timeout_spec optnl
5297 		| timeout_spec optnl
5298 		;
5299 
5300 limit_spec	: STRING NUMBER
5301 		{
5302 			if (check_rulestate(PFCTL_STATE_OPTION)) {
5303 				free($1);
5304 				YYERROR;
5305 			}
5306 			if ($2 < 0 || $2 > UINT_MAX) {
5307 				yyerror("only positive values permitted");
5308 				YYERROR;
5309 			}
5310 			if (pfctl_apply_limit(pf, $1, $2) != 0) {
5311 				yyerror("unable to set limit %s %u", $1, $2);
5312 				free($1);
5313 				YYERROR;
5314 			}
5315 			free($1);
5316 		}
5317 		;
5318 
5319 limit_list	: limit_list comma limit_spec optnl
5320 		| limit_spec optnl
5321 		;
5322 
5323 comma		: ','
5324 		| /* empty */
5325 		;
5326 
5327 yesno		: NO			{ $$ = 0; }
5328 		| STRING		{
5329 			if (!strcmp($1, "yes"))
5330 				$$ = 1;
5331 			else {
5332 				yyerror("invalid value '%s', expected 'yes' "
5333 				    "or 'no'", $1);
5334 				free($1);
5335 				YYERROR;
5336 			}
5337 			free($1);
5338 		}
5339 		;
5340 
5341 unaryop		: '='		{ $$ = PF_OP_EQ; }
5342 		| NE			{ $$ = PF_OP_NE; }
5343 		| LE			{ $$ = PF_OP_LE; }
5344 		| '<'		{ $$ = PF_OP_LT; }
5345 		| GE		{ $$ = PF_OP_GE; }
5346 		| '>'		{ $$ = PF_OP_GT; }
5347 		;
5348 
5349 %%
5350 
5351 int
5352 yyerror(const char *fmt, ...)
5353 {
5354 	va_list		 ap;
5355 
5356 	file->errors++;
5357 	va_start(ap, fmt);
5358 	fprintf(stderr, "%s:%d: ", file->name, yylval.lineno);
5359 	vfprintf(stderr, fmt, ap);
5360 	fprintf(stderr, "\n");
5361 	va_end(ap);
5362 	return (0);
5363 }
5364 
5365 int
5366 disallow_table(struct node_host *h, const char *fmt)
5367 {
5368 	for (; h != NULL; h = h->next)
5369 		if (h->addr.type == PF_ADDR_TABLE) {
5370 			yyerror(fmt, h->addr.v.tblname);
5371 			return (1);
5372 		}
5373 	return (0);
5374 }
5375 
5376 int
5377 disallow_urpf_failed(struct node_host *h, const char *fmt)
5378 {
5379 	for (; h != NULL; h = h->next)
5380 		if (h->addr.type == PF_ADDR_URPFFAILED) {
5381 			yyerror(fmt);
5382 			return (1);
5383 		}
5384 	return (0);
5385 }
5386 
5387 int
5388 disallow_alias(struct node_host *h, const char *fmt)
5389 {
5390 	for (; h != NULL; h = h->next)
5391 		if (DYNIF_MULTIADDR(h->addr)) {
5392 			yyerror(fmt, h->addr.v.tblname);
5393 			return (1);
5394 		}
5395 	return (0);
5396 }
5397 
5398 int
5399 rule_consistent(struct pfctl_rule *r, int anchor_call)
5400 {
5401 	int	problems = 0;
5402 
5403 	switch (r->action) {
5404 	case PF_PASS:
5405 	case PF_MATCH:
5406 	case PF_DROP:
5407 	case PF_SCRUB:
5408 	case PF_NOSCRUB:
5409 		problems = filter_consistent(r, anchor_call);
5410 		break;
5411 	case PF_NAT:
5412 	case PF_NONAT:
5413 		problems = nat_consistent(r);
5414 		break;
5415 	case PF_RDR:
5416 	case PF_NORDR:
5417 		problems = rdr_consistent(r);
5418 		break;
5419 	case PF_BINAT:
5420 	case PF_NOBINAT:
5421 	default:
5422 		break;
5423 	}
5424 	return (problems);
5425 }
5426 
5427 int
5428 filter_consistent(struct pfctl_rule *r, int anchor_call)
5429 {
5430 	int	problems = 0;
5431 
5432 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
5433 	    r->proto != IPPROTO_SCTP &&
5434 	    (r->src.port_op || r->dst.port_op)) {
5435 		yyerror("port only applies to tcp/udp/sctp");
5436 		problems++;
5437 	}
5438 	if (r->proto != IPPROTO_ICMP && r->proto != IPPROTO_ICMPV6 &&
5439 	    (r->type || r->code)) {
5440 		yyerror("icmp-type/code only applies to icmp");
5441 		problems++;
5442 	}
5443 	if (!r->af && (r->type || r->code)) {
5444 		yyerror("must indicate address family with icmp-type/code");
5445 		problems++;
5446 	}
5447 	if (r->overload_tblname[0] &&
5448 	    r->max_src_conn == 0 && r->max_src_conn_rate.seconds == 0) {
5449 		yyerror("'overload' requires 'max-src-conn' "
5450 		    "or 'max-src-conn-rate'");
5451 		problems++;
5452 	}
5453 	if ((r->proto == IPPROTO_ICMP && r->af == AF_INET6) ||
5454 	    (r->proto == IPPROTO_ICMPV6 && r->af == AF_INET)) {
5455 		yyerror("proto %s doesn't match address family %s",
5456 		    r->proto == IPPROTO_ICMP ? "icmp" : "icmp6",
5457 		    r->af == AF_INET ? "inet" : "inet6");
5458 		problems++;
5459 	}
5460 	if (r->allow_opts && r->action != PF_PASS) {
5461 		yyerror("allow-opts can only be specified for pass rules");
5462 		problems++;
5463 	}
5464 	if (r->rule_flag & PFRULE_FRAGMENT && (r->src.port_op ||
5465 	    r->dst.port_op || r->flagset || r->type || r->code)) {
5466 		yyerror("fragments can be filtered only on IP header fields");
5467 		problems++;
5468 	}
5469 	if (r->rule_flag & PFRULE_RETURNRST && r->proto != IPPROTO_TCP) {
5470 		yyerror("return-rst can only be applied to TCP rules");
5471 		problems++;
5472 	}
5473 	if (r->max_src_nodes && !(r->rule_flag & PFRULE_RULESRCTRACK)) {
5474 		yyerror("max-src-nodes requires 'source-track rule'");
5475 		problems++;
5476 	}
5477 	if (r->action != PF_PASS && r->keep_state) {
5478 		yyerror("keep state is great, but only for pass rules");
5479 		problems++;
5480 	}
5481 	if (r->rule_flag & PFRULE_STATESLOPPY &&
5482 	    (r->keep_state == PF_STATE_MODULATE ||
5483 	    r->keep_state == PF_STATE_SYNPROXY)) {
5484 		yyerror("sloppy state matching cannot be used with "
5485 		    "synproxy state or modulate state");
5486 		problems++;
5487 	}
5488 	/* match rules rules */
5489 	if (r->action == PF_MATCH) {
5490 		if (r->divert.port) {
5491 			yyerror("divert is not supported on match rules");
5492 			problems++;
5493 		}
5494 		if (r->rt) {
5495 			yyerror("route-to, reply-to, dup-to and fastroute "
5496 			   "must not be used on match rules");
5497 			problems++;
5498 		}
5499 		if (r->naf != r->af) {
5500 			yyerror("af-to is not supported on match rules");
5501 			problems++;
5502 		}
5503 	}
5504 	if (r->rpool.opts & PF_POOL_STICKYADDR && !r->keep_state) {
5505 		yyerror("'sticky-address' requires 'keep state'");
5506 		problems++;
5507 	}
5508 	return (-problems);
5509 }
5510 
5511 int
5512 nat_consistent(struct pfctl_rule *r)
5513 {
5514 	return (0);	/* yeah! */
5515 }
5516 
5517 int
5518 rdr_consistent(struct pfctl_rule *r)
5519 {
5520 	int			 problems = 0;
5521 
5522 	if (r->proto != IPPROTO_TCP && r->proto != IPPROTO_UDP &&
5523 	    r->proto != IPPROTO_SCTP) {
5524 		if (r->src.port_op) {
5525 			yyerror("src port only applies to tcp/udp/sctp");
5526 			problems++;
5527 		}
5528 		if (r->dst.port_op) {
5529 			yyerror("dst port only applies to tcp/udp/sctp");
5530 			problems++;
5531 		}
5532 		if (r->rpool.proxy_port[0]) {
5533 			yyerror("rpool port only applies to tcp/udp/sctp");
5534 			problems++;
5535 		}
5536 	}
5537 	if (r->dst.port_op &&
5538 	    r->dst.port_op != PF_OP_EQ && r->dst.port_op != PF_OP_RRG) {
5539 		yyerror("invalid port operator for rdr destination port");
5540 		problems++;
5541 	}
5542 	return (-problems);
5543 }
5544 
5545 int
5546 process_tabledef(char *name, struct table_opts *opts)
5547 {
5548 	struct pfr_buffer	 ab;
5549 	struct node_tinit	*ti;
5550 	unsigned long		 maxcount;
5551 	size_t			 s = sizeof(maxcount);
5552 
5553 	bzero(&ab, sizeof(ab));
5554 	ab.pfrb_type = PFRB_ADDRS;
5555 	SIMPLEQ_FOREACH(ti, &opts->init_nodes, entries) {
5556 		if (ti->file)
5557 			if (pfr_buf_load(&ab, ti->file, 0, append_addr)) {
5558 				if (errno)
5559 					yyerror("cannot load \"%s\": %s",
5560 					    ti->file, strerror(errno));
5561 				else
5562 					yyerror("file \"%s\" contains bad data",
5563 					    ti->file);
5564 				goto _error;
5565 			}
5566 		if (ti->host)
5567 			if (append_addr_host(&ab, ti->host, 0, 0)) {
5568 				yyerror("cannot create address buffer: %s",
5569 				    strerror(errno));
5570 				goto _error;
5571 			}
5572 	}
5573 	if (pf->opts & PF_OPT_VERBOSE)
5574 		print_tabledef(name, opts->flags, opts->init_addr,
5575 		    &opts->init_nodes);
5576 	if (!(pf->opts & PF_OPT_NOACTION) &&
5577 	    pfctl_define_table(name, opts->flags, opts->init_addr,
5578 	    pf->anchor->name, &ab, pf->anchor->ruleset.tticket)) {
5579 
5580 		if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s,
5581 		    NULL, 0) == -1)
5582 			maxcount = 65535;
5583 
5584 		if (ab.pfrb_size > maxcount)
5585 			yyerror("cannot define table %s: too many elements.\n"
5586 			    "Consider increasing net.pf.request_maxcount.",
5587 			    name);
5588 		else
5589 			yyerror("cannot define table %s: %s", name,
5590 			    pfr_strerror(errno));
5591 
5592 		goto _error;
5593 	}
5594 	pf->tdirty = 1;
5595 	pfr_buf_clear(&ab);
5596 	return (0);
5597 _error:
5598 	pfr_buf_clear(&ab);
5599 	return (-1);
5600 }
5601 
5602 struct keywords {
5603 	const char	*k_name;
5604 	int		 k_val;
5605 };
5606 
5607 /* macro gore, but you should've seen the prior indentation nightmare... */
5608 
5609 #define FREE_LIST(T,r) \
5610 	do { \
5611 		T *p, *node = r; \
5612 		while (node != NULL) { \
5613 			p = node; \
5614 			node = node->next; \
5615 			free(p); \
5616 		} \
5617 	} while (0)
5618 
5619 #define LOOP_THROUGH(T,n,r,C) \
5620 	do { \
5621 		T *n; \
5622 		if (r == NULL) { \
5623 			r = calloc(1, sizeof(T)); \
5624 			if (r == NULL) \
5625 				err(1, "LOOP: calloc"); \
5626 			r->next = NULL; \
5627 		} \
5628 		n = r; \
5629 		while (n != NULL) { \
5630 			do { \
5631 				C; \
5632 			} while (0); \
5633 			n = n->next; \
5634 		} \
5635 	} while (0)
5636 
5637 void
5638 expand_label_str(char *label, size_t len, const char *srch, const char *repl)
5639 {
5640 	char *tmp;
5641 	char *p, *q;
5642 
5643 	if ((tmp = calloc(1, len)) == NULL)
5644 		err(1, "expand_label_str: calloc");
5645 	p = q = label;
5646 	while ((q = strstr(p, srch)) != NULL) {
5647 		*q = '\0';
5648 		if ((strlcat(tmp, p, len) >= len) ||
5649 		    (strlcat(tmp, repl, len) >= len))
5650 			errx(1, "expand_label: label too long");
5651 		q += strlen(srch);
5652 		p = q;
5653 	}
5654 	if (strlcat(tmp, p, len) >= len)
5655 		errx(1, "expand_label: label too long");
5656 	strlcpy(label, tmp, len);	/* always fits */
5657 	free(tmp);
5658 }
5659 
5660 void
5661 expand_label_if(const char *name, char *label, size_t len, const char *ifname)
5662 {
5663 	if (strstr(label, name) != NULL) {
5664 		if (!*ifname)
5665 			expand_label_str(label, len, name, "any");
5666 		else
5667 			expand_label_str(label, len, name, ifname);
5668 	}
5669 }
5670 
5671 void
5672 expand_label_addr(const char *name, char *label, size_t len, sa_family_t af,
5673     struct pf_rule_addr *addr)
5674 {
5675 	char tmp[64], tmp_not[66];
5676 
5677 	if (strstr(label, name) != NULL) {
5678 		switch (addr->addr.type) {
5679 		case PF_ADDR_DYNIFTL:
5680 			snprintf(tmp, sizeof(tmp), "(%s)", addr->addr.v.ifname);
5681 			break;
5682 		case PF_ADDR_TABLE:
5683 			snprintf(tmp, sizeof(tmp), "<%s>", addr->addr.v.tblname);
5684 			break;
5685 		case PF_ADDR_NOROUTE:
5686 			snprintf(tmp, sizeof(tmp), "no-route");
5687 			break;
5688 		case PF_ADDR_URPFFAILED:
5689 			snprintf(tmp, sizeof(tmp), "urpf-failed");
5690 			break;
5691 		case PF_ADDR_ADDRMASK:
5692 			if (!af || (PF_AZERO(&addr->addr.v.a.addr, af) &&
5693 			    PF_AZERO(&addr->addr.v.a.mask, af)))
5694 				snprintf(tmp, sizeof(tmp), "any");
5695 			else {
5696 				char	a[48];
5697 				int	bits;
5698 
5699 				if (inet_ntop(af, &addr->addr.v.a.addr, a,
5700 				    sizeof(a)) == NULL)
5701 					snprintf(tmp, sizeof(tmp), "?");
5702 				else {
5703 					bits = unmask(&addr->addr.v.a.mask, af);
5704 					if ((af == AF_INET && bits < 32) ||
5705 					    (af == AF_INET6 && bits < 128))
5706 						snprintf(tmp, sizeof(tmp),
5707 						    "%s/%d", a, bits);
5708 					else
5709 						snprintf(tmp, sizeof(tmp),
5710 						    "%s", a);
5711 				}
5712 			}
5713 			break;
5714 		default:
5715 			snprintf(tmp, sizeof(tmp), "?");
5716 			break;
5717 		}
5718 
5719 		if (addr->neg) {
5720 			snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp);
5721 			expand_label_str(label, len, name, tmp_not);
5722 		} else
5723 			expand_label_str(label, len, name, tmp);
5724 	}
5725 }
5726 
5727 void
5728 expand_label_port(const char *name, char *label, size_t len,
5729     struct pf_rule_addr *addr)
5730 {
5731 	char	 a1[6], a2[6], op[13] = "";
5732 
5733 	if (strstr(label, name) != NULL) {
5734 		snprintf(a1, sizeof(a1), "%u", ntohs(addr->port[0]));
5735 		snprintf(a2, sizeof(a2), "%u", ntohs(addr->port[1]));
5736 		if (!addr->port_op)
5737 			;
5738 		else if (addr->port_op == PF_OP_IRG)
5739 			snprintf(op, sizeof(op), "%s><%s", a1, a2);
5740 		else if (addr->port_op == PF_OP_XRG)
5741 			snprintf(op, sizeof(op), "%s<>%s", a1, a2);
5742 		else if (addr->port_op == PF_OP_EQ)
5743 			snprintf(op, sizeof(op), "%s", a1);
5744 		else if (addr->port_op == PF_OP_NE)
5745 			snprintf(op, sizeof(op), "!=%s", a1);
5746 		else if (addr->port_op == PF_OP_LT)
5747 			snprintf(op, sizeof(op), "<%s", a1);
5748 		else if (addr->port_op == PF_OP_LE)
5749 			snprintf(op, sizeof(op), "<=%s", a1);
5750 		else if (addr->port_op == PF_OP_GT)
5751 			snprintf(op, sizeof(op), ">%s", a1);
5752 		else if (addr->port_op == PF_OP_GE)
5753 			snprintf(op, sizeof(op), ">=%s", a1);
5754 		expand_label_str(label, len, name, op);
5755 	}
5756 }
5757 
5758 void
5759 expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto)
5760 {
5761 	const char *protoname;
5762 	char n[4];
5763 
5764 	if (strstr(label, name) != NULL) {
5765 		protoname = pfctl_proto2name(proto);
5766 		if (protoname != NULL)
5767 			expand_label_str(label, len, name, protoname);
5768 		else {
5769 			snprintf(n, sizeof(n), "%u", proto);
5770 			expand_label_str(label, len, name, n);
5771 		}
5772 	}
5773 }
5774 
5775 void
5776 expand_label_nr(const char *name, char *label, size_t len,
5777     struct pfctl_rule *r)
5778 {
5779 	char n[11];
5780 
5781 	if (strstr(label, name) != NULL) {
5782 		snprintf(n, sizeof(n), "%u", r->nr);
5783 		expand_label_str(label, len, name, n);
5784 	}
5785 }
5786 
5787 void
5788 expand_label(char *label, size_t len, struct pfctl_rule *r)
5789 {
5790 	expand_label_if("$if", label, len, r->ifname);
5791 	expand_label_addr("$srcaddr", label, len, r->af, &r->src);
5792 	expand_label_addr("$dstaddr", label, len, r->af, &r->dst);
5793 	expand_label_port("$srcport", label, len, &r->src);
5794 	expand_label_port("$dstport", label, len, &r->dst);
5795 	expand_label_proto("$proto", label, len, r->proto);
5796 	expand_label_nr("$nr", label, len, r);
5797 }
5798 
5799 int
5800 expand_altq(struct pf_altq *a, struct node_if *interfaces,
5801     struct node_queue *nqueues, struct node_queue_bw bwspec,
5802     struct node_queue_opt *opts)
5803 {
5804 	struct pf_altq		 pa, pb;
5805 	char			 qname[PF_QNAME_SIZE];
5806 	struct node_queue	*n;
5807 	struct node_queue_bw	 bw;
5808 	int			 errs = 0;
5809 
5810 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5811 		FREE_LIST(struct node_if, interfaces);
5812 		if (nqueues)
5813 			FREE_LIST(struct node_queue, nqueues);
5814 		return (0);
5815 	}
5816 
5817 	LOOP_THROUGH(struct node_if, interface, interfaces,
5818 		memcpy(&pa, a, sizeof(struct pf_altq));
5819 		if (strlcpy(pa.ifname, interface->ifname,
5820 		    sizeof(pa.ifname)) >= sizeof(pa.ifname))
5821 			errx(1, "expand_altq: strlcpy");
5822 
5823 		if (interface->not) {
5824 			yyerror("altq on ! <interface> is not supported");
5825 			errs++;
5826 		} else {
5827 			if (eval_pfaltq(pf, &pa, &bwspec, opts))
5828 				errs++;
5829 			else
5830 				if (pfctl_add_altq(pf, &pa))
5831 					errs++;
5832 
5833 			if (pf->opts & PF_OPT_VERBOSE) {
5834 				print_altq(&pf->paltq->altq, 0,
5835 				    &bwspec, opts);
5836 				if (nqueues && nqueues->tail) {
5837 					printf("queue { ");
5838 					LOOP_THROUGH(struct node_queue, queue,
5839 					    nqueues,
5840 						printf("%s ",
5841 						    queue->queue);
5842 					);
5843 					printf("}");
5844 				}
5845 				printf("\n");
5846 			}
5847 
5848 			if (pa.scheduler == ALTQT_CBQ ||
5849 			    pa.scheduler == ALTQT_HFSC ||
5850 			    pa.scheduler == ALTQT_FAIRQ) {
5851 				/* now create a root queue */
5852 				memset(&pb, 0, sizeof(struct pf_altq));
5853 				if (strlcpy(qname, "root_", sizeof(qname)) >=
5854 				    sizeof(qname))
5855 					errx(1, "expand_altq: strlcpy");
5856 				if (strlcat(qname, interface->ifname,
5857 				    sizeof(qname)) >= sizeof(qname))
5858 					errx(1, "expand_altq: strlcat");
5859 				if (strlcpy(pb.qname, qname,
5860 				    sizeof(pb.qname)) >= sizeof(pb.qname))
5861 					errx(1, "expand_altq: strlcpy");
5862 				if (strlcpy(pb.ifname, interface->ifname,
5863 				    sizeof(pb.ifname)) >= sizeof(pb.ifname))
5864 					errx(1, "expand_altq: strlcpy");
5865 				pb.qlimit = pa.qlimit;
5866 				pb.scheduler = pa.scheduler;
5867 				bw.bw_absolute = pa.ifbandwidth;
5868 				bw.bw_percent = 0;
5869 				if (eval_pfqueue(pf, &pb, &bw, opts))
5870 					errs++;
5871 				else
5872 					if (pfctl_add_altq(pf, &pb))
5873 						errs++;
5874 			}
5875 
5876 			LOOP_THROUGH(struct node_queue, queue, nqueues,
5877 				n = calloc(1, sizeof(struct node_queue));
5878 				if (n == NULL)
5879 					err(1, "expand_altq: calloc");
5880 				if (pa.scheduler == ALTQT_CBQ ||
5881 				    pa.scheduler == ALTQT_HFSC ||
5882 				    pa.scheduler == ALTQT_FAIRQ)
5883 					if (strlcpy(n->parent, qname,
5884 					    sizeof(n->parent)) >=
5885 					    sizeof(n->parent))
5886 						errx(1, "expand_altq: strlcpy");
5887 				if (strlcpy(n->queue, queue->queue,
5888 				    sizeof(n->queue)) >= sizeof(n->queue))
5889 					errx(1, "expand_altq: strlcpy");
5890 				if (strlcpy(n->ifname, interface->ifname,
5891 				    sizeof(n->ifname)) >= sizeof(n->ifname))
5892 					errx(1, "expand_altq: strlcpy");
5893 				n->scheduler = pa.scheduler;
5894 				n->next = NULL;
5895 				n->tail = n;
5896 				if (queues == NULL)
5897 					queues = n;
5898 				else {
5899 					queues->tail->next = n;
5900 					queues->tail = n;
5901 				}
5902 			);
5903 		}
5904 	);
5905 	FREE_LIST(struct node_if, interfaces);
5906 	if (nqueues)
5907 		FREE_LIST(struct node_queue, nqueues);
5908 
5909 	return (errs);
5910 }
5911 
5912 int
5913 expand_queue(struct pf_altq *a, struct node_if *interfaces,
5914     struct node_queue *nqueues, struct node_queue_bw bwspec,
5915     struct node_queue_opt *opts)
5916 {
5917 	struct node_queue	*n, *nq;
5918 	struct pf_altq		 pa;
5919 	u_int8_t		 found = 0;
5920 	u_int8_t		 errs = 0;
5921 
5922 	if ((pf->loadopt & PFCTL_FLAG_ALTQ) == 0) {
5923 		FREE_LIST(struct node_queue, nqueues);
5924 		return (0);
5925 	}
5926 
5927 	if (queues == NULL) {
5928 		yyerror("queue %s has no parent", a->qname);
5929 		FREE_LIST(struct node_queue, nqueues);
5930 		return (1);
5931 	}
5932 
5933 	LOOP_THROUGH(struct node_if, interface, interfaces,
5934 		LOOP_THROUGH(struct node_queue, tqueue, queues,
5935 			if (!strncmp(a->qname, tqueue->queue, PF_QNAME_SIZE) &&
5936 			    (interface->ifname[0] == 0 ||
5937 			    (!interface->not && !strncmp(interface->ifname,
5938 			    tqueue->ifname, IFNAMSIZ)) ||
5939 			    (interface->not && strncmp(interface->ifname,
5940 			    tqueue->ifname, IFNAMSIZ)))) {
5941 				/* found ourself in queues */
5942 				found++;
5943 
5944 				memcpy(&pa, a, sizeof(struct pf_altq));
5945 
5946 				if (pa.scheduler != ALTQT_NONE &&
5947 				    pa.scheduler != tqueue->scheduler) {
5948 					yyerror("exactly one scheduler type "
5949 					    "per interface allowed");
5950 					return (1);
5951 				}
5952 				pa.scheduler = tqueue->scheduler;
5953 
5954 				/* scheduler dependent error checking */
5955 				switch (pa.scheduler) {
5956 				case ALTQT_PRIQ:
5957 					if (nqueues != NULL) {
5958 						yyerror("priq queues cannot "
5959 						    "have child queues");
5960 						return (1);
5961 					}
5962 					if (bwspec.bw_absolute > 0 ||
5963 					    bwspec.bw_percent < 100) {
5964 						yyerror("priq doesn't take "
5965 						    "bandwidth");
5966 						return (1);
5967 					}
5968 					break;
5969 				default:
5970 					break;
5971 				}
5972 
5973 				if (strlcpy(pa.ifname, tqueue->ifname,
5974 				    sizeof(pa.ifname)) >= sizeof(pa.ifname))
5975 					errx(1, "expand_queue: strlcpy");
5976 				if (strlcpy(pa.parent, tqueue->parent,
5977 				    sizeof(pa.parent)) >= sizeof(pa.parent))
5978 					errx(1, "expand_queue: strlcpy");
5979 
5980 				if (eval_pfqueue(pf, &pa, &bwspec, opts))
5981 					errs++;
5982 				else
5983 					if (pfctl_add_altq(pf, &pa))
5984 						errs++;
5985 
5986 				for (nq = nqueues; nq != NULL; nq = nq->next) {
5987 					if (!strcmp(a->qname, nq->queue)) {
5988 						yyerror("queue cannot have "
5989 						    "itself as child");
5990 						errs++;
5991 						continue;
5992 					}
5993 					n = calloc(1,
5994 					    sizeof(struct node_queue));
5995 					if (n == NULL)
5996 						err(1, "expand_queue: calloc");
5997 					if (strlcpy(n->parent, a->qname,
5998 					    sizeof(n->parent)) >=
5999 					    sizeof(n->parent))
6000 						errx(1, "expand_queue strlcpy");
6001 					if (strlcpy(n->queue, nq->queue,
6002 					    sizeof(n->queue)) >=
6003 					    sizeof(n->queue))
6004 						errx(1, "expand_queue strlcpy");
6005 					if (strlcpy(n->ifname, tqueue->ifname,
6006 					    sizeof(n->ifname)) >=
6007 					    sizeof(n->ifname))
6008 						errx(1, "expand_queue strlcpy");
6009 					n->scheduler = tqueue->scheduler;
6010 					n->next = NULL;
6011 					n->tail = n;
6012 					if (queues == NULL)
6013 						queues = n;
6014 					else {
6015 						queues->tail->next = n;
6016 						queues->tail = n;
6017 					}
6018 				}
6019 				if ((pf->opts & PF_OPT_VERBOSE) && (
6020 				    (found == 1 && interface->ifname[0] == 0) ||
6021 				    (found > 0 && interface->ifname[0] != 0))) {
6022 					print_queue(&pf->paltq->altq, 0,
6023 					    &bwspec, interface->ifname[0] != 0,
6024 					    opts);
6025 					if (nqueues && nqueues->tail) {
6026 						printf("{ ");
6027 						LOOP_THROUGH(struct node_queue,
6028 						    queue, nqueues,
6029 							printf("%s ",
6030 							    queue->queue);
6031 						);
6032 						printf("}");
6033 					}
6034 					printf("\n");
6035 				}
6036 			}
6037 		);
6038 	);
6039 
6040 	FREE_LIST(struct node_queue, nqueues);
6041 	FREE_LIST(struct node_if, interfaces);
6042 
6043 	if (!found) {
6044 		yyerror("queue %s has no parent", a->qname);
6045 		errs++;
6046 	}
6047 
6048 	if (errs)
6049 		return (1);
6050 	else
6051 		return (0);
6052 }
6053 
6054 static int
6055 pf_af_to_proto(sa_family_t af)
6056 {
6057 	if (af == AF_INET)
6058 		return (ETHERTYPE_IP);
6059 	if (af == AF_INET6)
6060 		return (ETHERTYPE_IPV6);
6061 
6062 	return (0);
6063 }
6064 
6065 void
6066 expand_eth_rule(struct pfctl_eth_rule *r,
6067     struct node_if *interfaces, struct node_etherproto *protos,
6068     struct node_mac *srcs, struct node_mac *dsts,
6069     struct node_host *ipsrcs, struct node_host *ipdsts,
6070     const char *bridge_to, const char *anchor_call)
6071 {
6072 	char tagname[PF_TAG_NAME_SIZE];
6073 	char match_tagname[PF_TAG_NAME_SIZE];
6074 	char qname[PF_QNAME_SIZE];
6075 
6076 	if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
6077 		errx(1, "expand_eth_rule: tagname");
6078 	if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
6079 	    sizeof(match_tagname))
6080 		errx(1, "expand_eth_rule: match_tagname");
6081 	if (strlcpy(qname, r->qname, sizeof(qname)) >= sizeof(qname))
6082 		errx(1, "expand_eth_rule: qname");
6083 
6084 	LOOP_THROUGH(struct node_if, interface, interfaces,
6085 	LOOP_THROUGH(struct node_etherproto, proto, protos,
6086 	LOOP_THROUGH(struct node_mac, src, srcs,
6087 	LOOP_THROUGH(struct node_mac, dst, dsts,
6088 	LOOP_THROUGH(struct node_host, ipsrc, ipsrcs,
6089 	LOOP_THROUGH(struct node_host, ipdst, ipdsts,
6090 		strlcpy(r->ifname, interface->ifname,
6091 		    sizeof(r->ifname));
6092 		r->ifnot = interface->not;
6093 		r->proto = proto->proto;
6094 		if (!r->proto && ipsrc->af)
6095 			r->proto = pf_af_to_proto(ipsrc->af);
6096 		else if (!r->proto && ipdst->af)
6097 			r->proto = pf_af_to_proto(ipdst->af);
6098 		bcopy(src->mac, r->src.addr, ETHER_ADDR_LEN);
6099 		bcopy(src->mask, r->src.mask, ETHER_ADDR_LEN);
6100 		r->src.neg = src->neg;
6101 		r->src.isset = src->isset;
6102 		r->ipsrc.addr = ipsrc->addr;
6103 		r->ipsrc.neg = ipsrc->not;
6104 		r->ipdst.addr = ipdst->addr;
6105 		r->ipdst.neg = ipdst->not;
6106 		bcopy(dst->mac, r->dst.addr, ETHER_ADDR_LEN);
6107 		bcopy(dst->mask, r->dst.mask, ETHER_ADDR_LEN);
6108 		r->dst.neg = dst->neg;
6109 		r->dst.isset = dst->isset;
6110 		r->nr = pf->eastack[pf->asd]->match++;
6111 
6112 		if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
6113 		    sizeof(r->tagname))
6114 			errx(1, "expand_eth_rule: r->tagname");
6115 		if (strlcpy(r->match_tagname, match_tagname,
6116 		    sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
6117 			errx(1, "expand_eth_rule: r->match_tagname");
6118 		if (strlcpy(r->qname, qname, sizeof(r->qname)) >= sizeof(r->qname))
6119 			errx(1, "expand_eth_rule: r->qname");
6120 
6121 		if (bridge_to)
6122 			strlcpy(r->bridge_to, bridge_to, sizeof(r->bridge_to));
6123 
6124 		pfctl_append_eth_rule(pf, r, anchor_call);
6125 	))))));
6126 
6127 	FREE_LIST(struct node_if, interfaces);
6128 	FREE_LIST(struct node_etherproto, protos);
6129 	FREE_LIST(struct node_mac, srcs);
6130 	FREE_LIST(struct node_mac, dsts);
6131 	FREE_LIST(struct node_host, ipsrcs);
6132 	FREE_LIST(struct node_host, ipdsts);
6133 }
6134 
6135 void
6136 expand_rule(struct pfctl_rule *r,
6137     struct node_if *interfaces, struct node_host *rdr_hosts,
6138     struct node_host *nat_hosts,
6139     struct node_proto *protos, struct node_os *src_oses,
6140     struct node_host *src_hosts, struct node_port *src_ports,
6141     struct node_host *dst_hosts, struct node_port *dst_ports,
6142     struct node_uid *uids, struct node_gid *gids, struct node_if *rcv,
6143     struct node_icmp *icmp_types, const char *anchor_call)
6144 {
6145 	sa_family_t		 af = r->af;
6146 	int			 added = 0, error = 0;
6147 	char			 ifname[IF_NAMESIZE];
6148 	char			 label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
6149 	char			 tagname[PF_TAG_NAME_SIZE];
6150 	char			 match_tagname[PF_TAG_NAME_SIZE];
6151 	struct pf_pooladdr	*pa;
6152 	struct node_host	*h, *osrch, *odsth;
6153 	u_int8_t		 flags, flagset, keep_state;
6154 
6155 	memcpy(label, r->label, sizeof(r->label));
6156 	assert(sizeof(r->label) == sizeof(label));
6157 	if (strlcpy(tagname, r->tagname, sizeof(tagname)) >= sizeof(tagname))
6158 		errx(1, "expand_rule: strlcpy");
6159 	if (strlcpy(match_tagname, r->match_tagname, sizeof(match_tagname)) >=
6160 	    sizeof(match_tagname))
6161 		errx(1, "expand_rule: strlcpy");
6162 	flags = r->flags;
6163 	flagset = r->flagset;
6164 	keep_state = r->keep_state;
6165 
6166 	LOOP_THROUGH(struct node_if, interface, interfaces,
6167 	LOOP_THROUGH(struct node_proto, proto, protos,
6168 	LOOP_THROUGH(struct node_icmp, icmp_type, icmp_types,
6169 	LOOP_THROUGH(struct node_host, src_host, src_hosts,
6170 	LOOP_THROUGH(struct node_host, dst_host, dst_hosts,
6171 	LOOP_THROUGH(struct node_port, src_port, src_ports,
6172 	LOOP_THROUGH(struct node_port, dst_port, dst_ports,
6173 	LOOP_THROUGH(struct node_os, src_os, src_oses,
6174 	LOOP_THROUGH(struct node_uid, uid, uids,
6175 	LOOP_THROUGH(struct node_gid, gid, gids,
6176 
6177 		r->af = af;
6178 		/* for link-local IPv6 address, interface must match up */
6179 		if ((r->af && src_host->af && r->af != src_host->af) ||
6180 		    (r->af && dst_host->af && r->af != dst_host->af) ||
6181 		    (src_host->af && dst_host->af &&
6182 		    src_host->af != dst_host->af) ||
6183 		    (src_host->ifindex && dst_host->ifindex &&
6184 		    src_host->ifindex != dst_host->ifindex) ||
6185 		    (src_host->ifindex && *interface->ifname &&
6186 		    src_host->ifindex != if_nametoindex(interface->ifname)) ||
6187 		    (dst_host->ifindex && *interface->ifname &&
6188 		    dst_host->ifindex != if_nametoindex(interface->ifname)))
6189 			continue;
6190 		if (!r->af && src_host->af)
6191 			r->af = src_host->af;
6192 		else if (!r->af && dst_host->af)
6193 			r->af = dst_host->af;
6194 
6195 		if (*interface->ifname)
6196 			strlcpy(r->ifname, interface->ifname,
6197 			    sizeof(r->ifname));
6198 		else if (if_indextoname(src_host->ifindex, ifname))
6199 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
6200 		else if (if_indextoname(dst_host->ifindex, ifname))
6201 			strlcpy(r->ifname, ifname, sizeof(r->ifname));
6202 		else
6203 			memset(r->ifname, '\0', sizeof(r->ifname));
6204 
6205 		memcpy(r->label, label, sizeof(r->label));
6206 		if (strlcpy(r->tagname, tagname, sizeof(r->tagname)) >=
6207 		    sizeof(r->tagname))
6208 			errx(1, "expand_rule: strlcpy");
6209 		if (strlcpy(r->match_tagname, match_tagname,
6210 		    sizeof(r->match_tagname)) >= sizeof(r->match_tagname))
6211 			errx(1, "expand_rule: strlcpy");
6212 
6213 		osrch = odsth = NULL;
6214 		if (src_host->addr.type == PF_ADDR_DYNIFTL) {
6215 			osrch = src_host;
6216 			if ((src_host = gen_dynnode(src_host, r->af)) == NULL)
6217 				err(1, "expand_rule: calloc");
6218 		}
6219 		if (dst_host->addr.type == PF_ADDR_DYNIFTL) {
6220 			odsth = dst_host;
6221 			if ((dst_host = gen_dynnode(dst_host, r->af)) == NULL)
6222 				err(1, "expand_rule: calloc");
6223 		}
6224 
6225 		error += check_netmask(src_host, r->af);
6226 		error += check_netmask(dst_host, r->af);
6227 
6228 		r->ifnot = interface->not;
6229 		r->proto = proto->proto;
6230 		r->src.addr = src_host->addr;
6231 		r->src.neg = src_host->not;
6232 		r->src.port[0] = src_port->port[0];
6233 		r->src.port[1] = src_port->port[1];
6234 		r->src.port_op = src_port->op;
6235 		r->dst.addr = dst_host->addr;
6236 		r->dst.neg = dst_host->not;
6237 		r->dst.port[0] = dst_port->port[0];
6238 		r->dst.port[1] = dst_port->port[1];
6239 		r->dst.port_op = dst_port->op;
6240 		r->uid.op = uid->op;
6241 		r->uid.uid[0] = uid->uid[0];
6242 		r->uid.uid[1] = uid->uid[1];
6243 		r->gid.op = gid->op;
6244 		r->gid.gid[0] = gid->gid[0];
6245 		r->gid.gid[1] = gid->gid[1];
6246 		if (rcv) {
6247 			strlcpy(r->rcv_ifname, rcv->ifname,
6248 			    sizeof(r->rcv_ifname));
6249 		}
6250 		r->type = icmp_type->type;
6251 		r->code = icmp_type->code;
6252 
6253 		if ((keep_state == PF_STATE_MODULATE ||
6254 		    keep_state == PF_STATE_SYNPROXY) &&
6255 		    r->proto && r->proto != IPPROTO_TCP)
6256 			r->keep_state = PF_STATE_NORMAL;
6257 		else
6258 			r->keep_state = keep_state;
6259 
6260 		if (r->proto && r->proto != IPPROTO_TCP) {
6261 			r->flags = 0;
6262 			r->flagset = 0;
6263 		} else {
6264 			r->flags = flags;
6265 			r->flagset = flagset;
6266 		}
6267 		if (icmp_type->proto && r->proto != icmp_type->proto) {
6268 			yyerror("icmp-type mismatch");
6269 			error++;
6270 		}
6271 
6272 		if (src_os && src_os->os) {
6273 			r->os_fingerprint = pfctl_get_fingerprint(src_os->os);
6274 			if ((pf->opts & PF_OPT_VERBOSE2) &&
6275 			    r->os_fingerprint == PF_OSFP_NOMATCH)
6276 				fprintf(stderr,
6277 				    "warning: unknown '%s' OS fingerprint\n",
6278 				    src_os->os);
6279 		} else {
6280 			r->os_fingerprint = PF_OSFP_ANY;
6281 		}
6282 
6283 		TAILQ_INIT(&r->rdr.list);
6284 		for (h = rdr_hosts; h != NULL; h = h->next) {
6285 			pa = calloc(1, sizeof(struct pf_pooladdr));
6286 			if (pa == NULL)
6287 				err(1, "expand_rule: calloc");
6288 			pa->addr = h->addr;
6289 			if (h->ifname != NULL) {
6290 				if (strlcpy(pa->ifname, h->ifname,
6291 				    sizeof(pa->ifname)) >=
6292 				    sizeof(pa->ifname))
6293 					errx(1, "expand_rule: strlcpy");
6294 			} else
6295 				pa->ifname[0] = 0;
6296 			TAILQ_INSERT_TAIL(&r->rpool.list, pa, entries);
6297 		}
6298 		TAILQ_INIT(&r->nat.list);
6299 		for (h = nat_hosts; h != NULL; h = h->next) {
6300 			pa = calloc(1, sizeof(struct pf_pooladdr));
6301 			if (pa == NULL)
6302 				err(1, "expand_rule: calloc");
6303 			pa->addr = h->addr;
6304 			if (h->ifname != NULL) {
6305 				if (strlcpy(pa->ifname, h->ifname,
6306 				    sizeof(pa->ifname)) >=
6307 				    sizeof(pa->ifname))
6308 					errx(1, "expand_rule: strlcpy");
6309 			} else
6310 				pa->ifname[0] = 0;
6311 			TAILQ_INSERT_TAIL(&r->nat.list, pa, entries);
6312 		}
6313 
6314 		r->nat.proxy_port[0] = PF_NAT_PROXY_PORT_LOW;
6315 		r->nat.proxy_port[1] = PF_NAT_PROXY_PORT_HIGH;
6316 
6317 		if (rule_consistent(r, anchor_call[0]) < 0 || error)
6318 			yyerror("skipping rule due to errors");
6319 		else {
6320 			r->nr = pf->astack[pf->asd]->match++;
6321 			pfctl_append_rule(pf, r, anchor_call);
6322 			added++;
6323 		}
6324 
6325 		if (osrch && src_host->addr.type == PF_ADDR_DYNIFTL) {
6326 			free(src_host);
6327 			src_host = osrch;
6328 		}
6329 		if (odsth && dst_host->addr.type == PF_ADDR_DYNIFTL) {
6330 			free(dst_host);
6331 			dst_host = odsth;
6332 		}
6333 
6334 	))))))))));
6335 
6336 	FREE_LIST(struct node_if, interfaces);
6337 	FREE_LIST(struct node_proto, protos);
6338 	FREE_LIST(struct node_host, src_hosts);
6339 	FREE_LIST(struct node_port, src_ports);
6340 	FREE_LIST(struct node_os, src_oses);
6341 	FREE_LIST(struct node_host, dst_hosts);
6342 	FREE_LIST(struct node_port, dst_ports);
6343 	FREE_LIST(struct node_uid, uids);
6344 	FREE_LIST(struct node_gid, gids);
6345 	FREE_LIST(struct node_icmp, icmp_types);
6346 	FREE_LIST(struct node_host, rdr_hosts);
6347 	FREE_LIST(struct node_host, nat_hosts);
6348 
6349 	if (!added)
6350 		yyerror("rule expands to no valid combination");
6351 }
6352 
6353 int
6354 expand_skip_interface(struct node_if *interfaces)
6355 {
6356 	int	errs = 0;
6357 
6358 	if (!interfaces || (!interfaces->next && !interfaces->not &&
6359 	    !strcmp(interfaces->ifname, "none"))) {
6360 		if (pf->opts & PF_OPT_VERBOSE)
6361 			printf("set skip on none\n");
6362 		errs = pfctl_set_interface_flags(pf, "", PFI_IFLAG_SKIP, 0);
6363 		return (errs);
6364 	}
6365 
6366 	if (pf->opts & PF_OPT_VERBOSE)
6367 		printf("set skip on {");
6368 	LOOP_THROUGH(struct node_if, interface, interfaces,
6369 		if (pf->opts & PF_OPT_VERBOSE)
6370 			printf(" %s", interface->ifname);
6371 		if (interface->not) {
6372 			yyerror("skip on ! <interface> is not supported");
6373 			errs++;
6374 		} else
6375 			errs += pfctl_set_interface_flags(pf,
6376 			    interface->ifname, PFI_IFLAG_SKIP, 1);
6377 	);
6378 	if (pf->opts & PF_OPT_VERBOSE)
6379 		printf(" }\n");
6380 
6381 	FREE_LIST(struct node_if, interfaces);
6382 
6383 	if (errs)
6384 		return (1);
6385 	else
6386 		return (0);
6387 }
6388 
6389 void
6390 freehostlist(struct node_host *h)
6391 {
6392 	FREE_LIST(struct node_host, h);
6393 }
6394 
6395 #undef FREE_LIST
6396 #undef LOOP_THROUGH
6397 
6398 int
6399 check_rulestate(int desired_state)
6400 {
6401 	if (require_order && (rulestate > desired_state)) {
6402 		yyerror("Rules must be in order: options, ethernet, "
6403 		    "normalization, queueing, translation, filtering");
6404 		return (1);
6405 	}
6406 	rulestate = desired_state;
6407 	return (0);
6408 }
6409 
6410 int
6411 kw_cmp(const void *k, const void *e)
6412 {
6413 	return (strcmp(k, ((const struct keywords *)e)->k_name));
6414 }
6415 
6416 int
6417 lookup(char *s)
6418 {
6419 	/* this has to be sorted always */
6420 	static const struct keywords keywords[] = {
6421 		{ "af-to",		AFTO},
6422 		{ "all",		ALL},
6423 		{ "allow-opts",		ALLOWOPTS},
6424 		{ "altq",		ALTQ},
6425 		{ "anchor",		ANCHOR},
6426 		{ "antispoof",		ANTISPOOF},
6427 		{ "any",		ANY},
6428 		{ "bandwidth",		BANDWIDTH},
6429 		{ "binat",		BINAT},
6430 		{ "binat-anchor",	BINATANCHOR},
6431 		{ "bitmask",		BITMASK},
6432 		{ "block",		BLOCK},
6433 		{ "block-policy",	BLOCKPOLICY},
6434 		{ "bridge-to",		BRIDGE_TO},
6435 		{ "buckets",		BUCKETS},
6436 		{ "cbq",		CBQ},
6437 		{ "code",		CODE},
6438 		{ "codelq",		CODEL},
6439 		{ "debug",		DEBUG},
6440 		{ "divert-reply",	DIVERTREPLY},
6441 		{ "divert-to",		DIVERTTO},
6442 		{ "dnpipe",		DNPIPE},
6443 		{ "dnqueue",		DNQUEUE},
6444 		{ "drop",		DROP},
6445 		{ "dup-to",		DUPTO},
6446 		{ "endpoint-independent", ENDPI},
6447 		{ "ether",		ETHER},
6448 		{ "fail-policy",	FAILPOLICY},
6449 		{ "fairq",		FAIRQ},
6450 		{ "fastroute",		FASTROUTE},
6451 		{ "file",		FILENAME},
6452 		{ "fingerprints",	FINGERPRINTS},
6453 		{ "flags",		FLAGS},
6454 		{ "floating",		FLOATING},
6455 		{ "flush",		FLUSH},
6456 		{ "for",		FOR},
6457 		{ "fragment",		FRAGMENT},
6458 		{ "from",		FROM},
6459 		{ "global",		GLOBAL},
6460 		{ "group",		GROUP},
6461 		{ "hfsc",		HFSC},
6462 		{ "hogs",		HOGS},
6463 		{ "hostid",		HOSTID},
6464 		{ "icmp-type",		ICMPTYPE},
6465 		{ "icmp6-type",		ICMP6TYPE},
6466 		{ "if-bound",		IFBOUND},
6467 		{ "in",			IN},
6468 		{ "include",		INCLUDE},
6469 		{ "inet",		INET},
6470 		{ "inet6",		INET6},
6471 		{ "interval",		INTERVAL},
6472 		{ "keep",		KEEP},
6473 		{ "keepcounters",	KEEPCOUNTERS},
6474 		{ "l3",			L3},
6475 		{ "label",		LABEL},
6476 		{ "limit",		LIMIT},
6477 		{ "linkshare",		LINKSHARE},
6478 		{ "load",		LOAD},
6479 		{ "log",		LOG},
6480 		{ "loginterface",	LOGINTERFACE},
6481 		{ "map-e-portset",	MAPEPORTSET},
6482 		{ "match",		MATCH},
6483 		{ "matches",	MATCHES},
6484 		{ "max",		MAXIMUM},
6485 		{ "max-mss",		MAXMSS},
6486 		{ "max-src-conn",	MAXSRCCONN},
6487 		{ "max-src-conn-rate",	MAXSRCCONNRATE},
6488 		{ "max-src-nodes",	MAXSRCNODES},
6489 		{ "max-src-states",	MAXSRCSTATES},
6490 		{ "min-ttl",		MINTTL},
6491 		{ "modulate",		MODULATE},
6492 		{ "nat",		NAT},
6493 		{ "nat-anchor",		NATANCHOR},
6494 		{ "no",			NO},
6495 		{ "no-df",		NODF},
6496 		{ "no-route",		NOROUTE},
6497 		{ "no-sync",		NOSYNC},
6498 		{ "on",			ON},
6499 		{ "optimization",	OPTIMIZATION},
6500 		{ "os",			OS},
6501 		{ "out",		OUT},
6502 		{ "overload",		OVERLOAD},
6503 		{ "pass",		PASS},
6504 		{ "pflow",		PFLOW},
6505 		{ "port",		PORT},
6506 		{ "prio",		PRIO},
6507 		{ "priority",		PRIORITY},
6508 		{ "priq",		PRIQ},
6509 		{ "probability",	PROBABILITY},
6510 		{ "proto",		PROTO},
6511 		{ "qlimit",		QLIMIT},
6512 		{ "queue",		QUEUE},
6513 		{ "quick",		QUICK},
6514 		{ "random",		RANDOM},
6515 		{ "random-id",		RANDOMID},
6516 		{ "rdr",		RDR},
6517 		{ "rdr-anchor",		RDRANCHOR},
6518 		{ "realtime",		REALTIME},
6519 		{ "reassemble",		REASSEMBLE},
6520 		{ "received-on",	RECEIVEDON},
6521 		{ "reply-to",		REPLYTO},
6522 		{ "require-order",	REQUIREORDER},
6523 		{ "return",		RETURN},
6524 		{ "return-icmp",	RETURNICMP},
6525 		{ "return-icmp6",	RETURNICMP6},
6526 		{ "return-rst",		RETURNRST},
6527 		{ "ridentifier",	RIDENTIFIER},
6528 		{ "round-robin",	ROUNDROBIN},
6529 		{ "route",		ROUTE},
6530 		{ "route-to",		ROUTETO},
6531 		{ "rtable",		RTABLE},
6532 		{ "rule",		RULE},
6533 		{ "ruleset-optimization",	RULESET_OPTIMIZATION},
6534 		{ "scrub",		SCRUB},
6535 		{ "set",		SET},
6536 		{ "set-tos",		SETTOS},
6537 		{ "skip",		SKIP},
6538 		{ "sloppy",		SLOPPY},
6539 		{ "source-hash",	SOURCEHASH},
6540 		{ "source-track",	SOURCETRACK},
6541 		{ "state",		STATE},
6542 		{ "state-defaults",	STATEDEFAULTS},
6543 		{ "state-policy",	STATEPOLICY},
6544 		{ "static-port",	STATICPORT},
6545 		{ "sticky-address",	STICKYADDRESS},
6546 		{ "syncookies",         SYNCOOKIES},
6547 		{ "synproxy",		SYNPROXY},
6548 		{ "table",		TABLE},
6549 		{ "tag",		TAG},
6550 		{ "tagged",		TAGGED},
6551 		{ "target",		TARGET},
6552 		{ "tbrsize",		TBRSIZE},
6553 		{ "timeout",		TIMEOUT},
6554 		{ "to",			TO},
6555 		{ "tos",		TOS},
6556 		{ "ttl",		TTL},
6557 		{ "upperlimit",		UPPERLIMIT},
6558 		{ "urpf-failed",	URPFFAILED},
6559 		{ "user",		USER},
6560 	};
6561 	const struct keywords	*p;
6562 
6563 	p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
6564 	    sizeof(keywords[0]), kw_cmp);
6565 
6566 	if (p) {
6567 		if (debug > 1)
6568 			fprintf(stderr, "%s: %d\n", s, p->k_val);
6569 		return (p->k_val);
6570 	} else {
6571 		if (debug > 1)
6572 			fprintf(stderr, "string: %s\n", s);
6573 		return (STRING);
6574 	}
6575 }
6576 
6577 #define MAXPUSHBACK	128
6578 
6579 static char	*parsebuf;
6580 static int	 parseindex;
6581 static char	 pushback_buffer[MAXPUSHBACK];
6582 static int	 pushback_index = 0;
6583 
6584 int
6585 lgetc(int quotec)
6586 {
6587 	int		c, next;
6588 
6589 	if (parsebuf) {
6590 		/* Read character from the parsebuffer instead of input. */
6591 		if (parseindex >= 0) {
6592 			c = parsebuf[parseindex++];
6593 			if (c != '\0')
6594 				return (c);
6595 			parsebuf = NULL;
6596 		} else
6597 			parseindex++;
6598 	}
6599 
6600 	if (pushback_index)
6601 		return (pushback_buffer[--pushback_index]);
6602 
6603 	if (quotec) {
6604 		if ((c = getc(file->stream)) == EOF) {
6605 			yyerror("reached end of file while parsing quoted string");
6606 			if (popfile() == EOF)
6607 				return (EOF);
6608 			return (quotec);
6609 		}
6610 		return (c);
6611 	}
6612 
6613 	while ((c = getc(file->stream)) == '\\') {
6614 		next = getc(file->stream);
6615 		if (next != '\n') {
6616 			c = next;
6617 			break;
6618 		}
6619 		yylval.lineno = file->lineno;
6620 		file->lineno++;
6621 	}
6622 
6623 	while (c == EOF) {
6624 		if (popfile() == EOF)
6625 			return (EOF);
6626 		c = getc(file->stream);
6627 	}
6628 	return (c);
6629 }
6630 
6631 int
6632 lungetc(int c)
6633 {
6634 	if (c == EOF)
6635 		return (EOF);
6636 	if (parsebuf) {
6637 		parseindex--;
6638 		if (parseindex >= 0)
6639 			return (c);
6640 	}
6641 	if (pushback_index < MAXPUSHBACK-1)
6642 		return (pushback_buffer[pushback_index++] = c);
6643 	else
6644 		return (EOF);
6645 }
6646 
6647 int
6648 findeol(void)
6649 {
6650 	int	c;
6651 
6652 	parsebuf = NULL;
6653 
6654 	/* skip to either EOF or the first real EOL */
6655 	while (1) {
6656 		if (pushback_index)
6657 			c = pushback_buffer[--pushback_index];
6658 		else
6659 			c = lgetc(0);
6660 		if (c == '\n') {
6661 			file->lineno++;
6662 			break;
6663 		}
6664 		if (c == EOF)
6665 			break;
6666 	}
6667 	return (ERROR);
6668 }
6669 
6670 int
6671 yylex(void)
6672 {
6673 	char	 buf[8096];
6674 	char	*p, *val;
6675 	int	 quotec, next, c;
6676 	int	 token;
6677 
6678 top:
6679 	p = buf;
6680 	while ((c = lgetc(0)) == ' ' || c == '\t')
6681 		; /* nothing */
6682 
6683 	yylval.lineno = file->lineno;
6684 	if (c == '#')
6685 		while ((c = lgetc(0)) != '\n' && c != EOF)
6686 			; /* nothing */
6687 	if (c == '$' && parsebuf == NULL) {
6688 		while (1) {
6689 			if ((c = lgetc(0)) == EOF)
6690 				return (0);
6691 
6692 			if (p + 1 >= buf + sizeof(buf) - 1) {
6693 				yyerror("string too long");
6694 				return (findeol());
6695 			}
6696 			if (isalnum(c) || c == '_') {
6697 				*p++ = (char)c;
6698 				continue;
6699 			}
6700 			*p = '\0';
6701 			lungetc(c);
6702 			break;
6703 		}
6704 		val = symget(buf);
6705 		if (val == NULL) {
6706 			yyerror("macro '%s' not defined", buf);
6707 			return (findeol());
6708 		}
6709 		parsebuf = val;
6710 		parseindex = 0;
6711 		goto top;
6712 	}
6713 
6714 	switch (c) {
6715 	case '\'':
6716 	case '"':
6717 		quotec = c;
6718 		while (1) {
6719 			if ((c = lgetc(quotec)) == EOF)
6720 				return (0);
6721 			if (c == '\n') {
6722 				file->lineno++;
6723 				continue;
6724 			} else if (c == '\\') {
6725 				if ((next = lgetc(quotec)) == EOF)
6726 					return (0);
6727 				if (next == quotec || c == ' ' || c == '\t')
6728 					c = next;
6729 				else if (next == '\n') {
6730 					file->lineno++;
6731 					continue;
6732 				}
6733 				else
6734 					lungetc(next);
6735 			} else if (c == quotec) {
6736 				*p = '\0';
6737 				break;
6738 			}
6739 			if (p + 1 >= buf + sizeof(buf) - 1) {
6740 				yyerror("string too long");
6741 				return (findeol());
6742 			}
6743 			*p++ = (char)c;
6744 		}
6745 		yylval.v.string = strdup(buf);
6746 		if (yylval.v.string == NULL)
6747 			err(1, "yylex: strdup");
6748 		return (STRING);
6749 		case '!':
6750 			next = lgetc(0);
6751 			if (next == '=')
6752 				return (NE);
6753 			lungetc(next);
6754 		break;
6755 	case '<':
6756 		next = lgetc(0);
6757 		if (next == '>') {
6758 			yylval.v.i = PF_OP_XRG;
6759 			return (PORTBINARY);
6760 		} else if (next == '=')
6761 			return (LE);
6762 		lungetc(next);
6763 		break;
6764 	case '>':
6765 		next = lgetc(0);
6766 		if (next == '<') {
6767 			yylval.v.i = PF_OP_IRG;
6768 			return (PORTBINARY);
6769 		} else if (next == '=')
6770 			return (GE);
6771 		lungetc(next);
6772 		break;
6773 	case '-':
6774 		next = lgetc(0);
6775 		if (next == '>')
6776 			return (ARROW);
6777 		lungetc(next);
6778 		break;
6779 	}
6780 
6781 #define allowed_to_end_number(x) \
6782 	(isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
6783 
6784 	if (c == '-' || isdigit(c)) {
6785 		do {
6786 			*p++ = c;
6787 			if ((unsigned)(p-buf) >= sizeof(buf)) {
6788 				yyerror("string too long");
6789 				return (findeol());
6790 			}
6791 		} while ((c = lgetc(0)) != EOF && isdigit(c));
6792 		lungetc(c);
6793 		if (p == buf + 1 && buf[0] == '-')
6794 			goto nodigits;
6795 		if (c == EOF || allowed_to_end_number(c)) {
6796 			const char *errstr = NULL;
6797 
6798 			*p = '\0';
6799 			yylval.v.number = strtonum(buf, LLONG_MIN,
6800 			    LLONG_MAX, &errstr);
6801 			if (errstr) {
6802 				yyerror("\"%s\" invalid number: %s",
6803 				    buf, errstr);
6804 				return (findeol());
6805 			}
6806 			return (NUMBER);
6807 		} else {
6808 nodigits:
6809 			while (p > buf + 1)
6810 				lungetc(*--p);
6811 			c = *--p;
6812 			if (c == '-')
6813 				return (c);
6814 		}
6815 	}
6816 
6817 #define allowed_in_string(x) \
6818 	(isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
6819 	x != '{' && x != '}' && x != '<' && x != '>' && \
6820 	x != '!' && x != '=' && x != '/' && x != '#' && \
6821 	x != ','))
6822 
6823 	if (isalnum(c) || c == ':' || c == '_') {
6824 		do {
6825 			*p++ = c;
6826 			if ((unsigned)(p-buf) >= sizeof(buf)) {
6827 				yyerror("string too long");
6828 				return (findeol());
6829 			}
6830 		} while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
6831 		lungetc(c);
6832 		*p = '\0';
6833 		if ((token = lookup(buf)) == STRING)
6834 			if ((yylval.v.string = strdup(buf)) == NULL)
6835 				err(1, "yylex: strdup");
6836 		return (token);
6837 	}
6838 	if (c == '\n') {
6839 		yylval.lineno = file->lineno;
6840 		file->lineno++;
6841 	}
6842 	if (c == EOF)
6843 		return (0);
6844 	return (c);
6845 }
6846 
6847 int
6848 check_file_secrecy(int fd, const char *fname)
6849 {
6850 	struct stat	st;
6851 
6852 	if (fstat(fd, &st)) {
6853 		warn("cannot stat %s", fname);
6854 		return (-1);
6855 	}
6856 	if (st.st_uid != 0 && st.st_uid != getuid()) {
6857 		warnx("%s: owner not root or current user", fname);
6858 		return (-1);
6859 	}
6860 	if (st.st_mode & (S_IRWXG | S_IRWXO)) {
6861 		warnx("%s: group/world readable/writeable", fname);
6862 		return (-1);
6863 	}
6864 	return (0);
6865 }
6866 
6867 struct file *
6868 pushfile(const char *name, int secret)
6869 {
6870 	struct file	*nfile;
6871 
6872 	if ((nfile = calloc(1, sizeof(struct file))) == NULL ||
6873 	    (nfile->name = strdup(name)) == NULL) {
6874 		warn("malloc");
6875 		return (NULL);
6876 	}
6877 	if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
6878 		nfile->stream = stdin;
6879 		free(nfile->name);
6880 		if ((nfile->name = strdup("stdin")) == NULL) {
6881 			warn("strdup");
6882 			free(nfile);
6883 			return (NULL);
6884 		}
6885 	} else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
6886 		warn("%s", nfile->name);
6887 		free(nfile->name);
6888 		free(nfile);
6889 		return (NULL);
6890 	} else if (secret &&
6891 	    check_file_secrecy(fileno(nfile->stream), nfile->name)) {
6892 		fclose(nfile->stream);
6893 		free(nfile->name);
6894 		free(nfile);
6895 		return (NULL);
6896 	}
6897 	nfile->lineno = 1;
6898 	TAILQ_INSERT_TAIL(&files, nfile, entry);
6899 	return (nfile);
6900 }
6901 
6902 int
6903 popfile(void)
6904 {
6905 	struct file	*prev;
6906 
6907 	if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
6908 		prev->errors += file->errors;
6909 		TAILQ_REMOVE(&files, file, entry);
6910 		fclose(file->stream);
6911 		free(file->name);
6912 		free(file);
6913 		file = prev;
6914 		return (0);
6915 	}
6916 	return (EOF);
6917 }
6918 
6919 int
6920 parse_config(char *filename, struct pfctl *xpf)
6921 {
6922 	int		 errors = 0;
6923 	struct sym	*sym;
6924 
6925 	pf = xpf;
6926 	errors = 0;
6927 	rulestate = PFCTL_STATE_NONE;
6928 	returnicmpdefault = (ICMP_UNREACH << 8) | ICMP_UNREACH_PORT;
6929 	returnicmp6default =
6930 	    (ICMP6_DST_UNREACH << 8) | ICMP6_DST_UNREACH_NOPORT;
6931 	blockpolicy = PFRULE_DROP;
6932 	failpolicy = PFRULE_DROP;
6933 	require_order = 1;
6934 
6935 	if ((file = pushfile(filename, 0)) == NULL) {
6936 		warn("cannot open the main config file!");
6937 		return (-1);
6938 	}
6939 
6940 	yyparse();
6941 	errors = file->errors;
6942 	popfile();
6943 
6944 	/* Free macros and check which have not been used. */
6945 	while ((sym = TAILQ_FIRST(&symhead))) {
6946 		if ((pf->opts & PF_OPT_VERBOSE2) && !sym->used)
6947 			fprintf(stderr, "warning: macro '%s' not "
6948 			    "used\n", sym->nam);
6949 		free(sym->nam);
6950 		free(sym->val);
6951 		TAILQ_REMOVE(&symhead, sym, entry);
6952 		free(sym);
6953 	}
6954 
6955 	return (errors ? -1 : 0);
6956 }
6957 
6958 int
6959 symset(const char *nam, const char *val, int persist)
6960 {
6961 	struct sym	*sym;
6962 
6963 	for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
6964 	    sym = TAILQ_NEXT(sym, entry))
6965 		;	/* nothing */
6966 
6967 	if (sym != NULL) {
6968 		if (sym->persist == 1)
6969 			return (0);
6970 		else {
6971 			free(sym->nam);
6972 			free(sym->val);
6973 			TAILQ_REMOVE(&symhead, sym, entry);
6974 			free(sym);
6975 		}
6976 	}
6977 	if ((sym = calloc(1, sizeof(*sym))) == NULL)
6978 		return (-1);
6979 
6980 	sym->nam = strdup(nam);
6981 	if (sym->nam == NULL) {
6982 		free(sym);
6983 		return (-1);
6984 	}
6985 	sym->val = strdup(val);
6986 	if (sym->val == NULL) {
6987 		free(sym->nam);
6988 		free(sym);
6989 		return (-1);
6990 	}
6991 	sym->used = 0;
6992 	sym->persist = persist;
6993 	TAILQ_INSERT_TAIL(&symhead, sym, entry);
6994 	return (0);
6995 }
6996 
6997 int
6998 pfctl_cmdline_symset(char *s)
6999 {
7000 	char	*sym, *val;
7001 	int	 ret;
7002 
7003 	if ((val = strrchr(s, '=')) == NULL)
7004 		return (-1);
7005 
7006 	if ((sym = malloc(strlen(s) - strlen(val) + 1)) == NULL)
7007 		err(1, "pfctl_cmdline_symset: malloc");
7008 
7009 	strlcpy(sym, s, strlen(s) - strlen(val) + 1);
7010 
7011 	ret = symset(sym, val + 1, 1);
7012 	free(sym);
7013 
7014 	return (ret);
7015 }
7016 
7017 char *
7018 symget(const char *nam)
7019 {
7020 	struct sym	*sym;
7021 
7022 	TAILQ_FOREACH(sym, &symhead, entry)
7023 		if (strcmp(nam, sym->nam) == 0) {
7024 			sym->used = 1;
7025 			return (sym->val);
7026 		}
7027 	return (NULL);
7028 }
7029 
7030 void
7031 mv_rules(struct pfctl_ruleset *src, struct pfctl_ruleset *dst)
7032 {
7033 	int i;
7034 	struct pfctl_rule *r;
7035 
7036 	for (i = 0; i < PF_RULESET_MAX; ++i) {
7037 		while ((r = TAILQ_FIRST(src->rules[i].active.ptr))
7038 		    != NULL) {
7039 			TAILQ_REMOVE(src->rules[i].active.ptr, r, entries);
7040 			TAILQ_INSERT_TAIL(dst->rules[i].active.ptr, r, entries);
7041 			dst->anchor->match++;
7042 		}
7043 		src->anchor->match = 0;
7044 		while ((r = TAILQ_FIRST(src->rules[i].inactive.ptr))
7045 		    != NULL) {
7046 			TAILQ_REMOVE(src->rules[i].inactive.ptr, r, entries);
7047 			TAILQ_INSERT_TAIL(dst->rules[i].inactive.ptr,
7048 				r, entries);
7049 		}
7050 	}
7051 }
7052 
7053 void
7054 mv_eth_rules(struct pfctl_eth_ruleset *src, struct pfctl_eth_ruleset *dst)
7055 {
7056 	struct pfctl_eth_rule *r;
7057 
7058 	while ((r = TAILQ_FIRST(&src->rules)) != NULL) {
7059 		TAILQ_REMOVE(&src->rules, r, entries);
7060 		TAILQ_INSERT_TAIL(&dst->rules, r, entries);
7061 		dst->anchor->match++;
7062 	}
7063 	src->anchor->match = 0;
7064 }
7065 
7066 void
7067 decide_address_family(struct node_host *n, sa_family_t *af)
7068 {
7069 	if (*af != 0 || n == NULL)
7070 		return;
7071 	*af = n->af;
7072 	while ((n = n->next) != NULL) {
7073 		if (n->af != *af) {
7074 			*af = 0;
7075 			return;
7076 		}
7077 	}
7078 }
7079 
7080 void
7081 remove_invalid_hosts(struct node_host **nh, sa_family_t *af)
7082 {
7083 	struct node_host	*n = *nh, *prev = NULL;
7084 
7085 	while (n != NULL) {
7086 		if (*af && n->af && n->af != *af) {
7087 			/* unlink and free n */
7088 			struct node_host *next = n->next;
7089 
7090 			/* adjust tail pointer */
7091 			if (n == (*nh)->tail)
7092 				(*nh)->tail = prev;
7093 			/* adjust previous node's next pointer */
7094 			if (prev == NULL)
7095 				*nh = next;
7096 			else
7097 				prev->next = next;
7098 			/* free node */
7099 			if (n->ifname != NULL)
7100 				free(n->ifname);
7101 			free(n);
7102 			n = next;
7103 		} else {
7104 			if (n->af && !*af)
7105 				*af = n->af;
7106 			prev = n;
7107 			n = n->next;
7108 		}
7109 	}
7110 }
7111 
7112 int
7113 invalid_redirect(struct node_host *nh, sa_family_t af)
7114 {
7115 	if (!af) {
7116 		struct node_host *n;
7117 
7118 		/* tables and dyniftl are ok without an address family */
7119 		for (n = nh; n != NULL; n = n->next) {
7120 			if (n->addr.type != PF_ADDR_TABLE &&
7121 			    n->addr.type != PF_ADDR_DYNIFTL) {
7122 				yyerror("address family not given and "
7123 				    "translation address expands to multiple "
7124 				    "address families");
7125 				return (1);
7126 			}
7127 		}
7128 	}
7129 	if (nh == NULL) {
7130 		yyerror("no translation address with matching address family "
7131 		    "found.");
7132 		return (1);
7133 	}
7134 	return (0);
7135 }
7136 
7137 int
7138 atoul(char *s, u_long *ulvalp)
7139 {
7140 	u_long	 ulval;
7141 	char	*ep;
7142 
7143 	errno = 0;
7144 	ulval = strtoul(s, &ep, 0);
7145 	if (s[0] == '\0' || *ep != '\0')
7146 		return (-1);
7147 	if (errno == ERANGE && ulval == ULONG_MAX)
7148 		return (-1);
7149 	*ulvalp = ulval;
7150 	return (0);
7151 }
7152 
7153 int
7154 getservice(char *n)
7155 {
7156 	struct servent	*s;
7157 	u_long		 ulval;
7158 
7159 	if (atoul(n, &ulval) == 0) {
7160 		if (ulval > 65535) {
7161 			yyerror("illegal port value %lu", ulval);
7162 			return (-1);
7163 		}
7164 		return (htons(ulval));
7165 	} else {
7166 		s = getservbyname(n, "tcp");
7167 		if (s == NULL)
7168 			s = getservbyname(n, "udp");
7169 		if (s == NULL)
7170 			s = getservbyname(n, "sctp");
7171 		if (s == NULL) {
7172 			yyerror("unknown port %s", n);
7173 			return (-1);
7174 		}
7175 		return (s->s_port);
7176 	}
7177 }
7178 
7179 int
7180 rule_label(struct pfctl_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
7181 {
7182 	for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
7183 		if (s[i] == NULL)
7184 			return (0);
7185 
7186 		if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
7187 		    sizeof(r->label[0])) {
7188 			yyerror("rule label too long (max %d chars)",
7189 			    sizeof(r->label[0])-1);
7190 			return (-1);
7191 		}
7192 	}
7193 	return (0);
7194 }
7195 
7196 int
7197 eth_rule_label(struct pfctl_eth_rule *r, char *s[PF_RULE_MAX_LABEL_COUNT])
7198 {
7199 	for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
7200 		if (s[i] == NULL)
7201 			return (0);
7202 
7203 		if (strlcpy(r->label[i], s[i], sizeof(r->label[0])) >=
7204 		    sizeof(r->label[0])) {
7205 			yyerror("rule label too long (max %d chars)",
7206 			    sizeof(r->label[0])-1);
7207 			return (-1);
7208 		}
7209 	}
7210 	return (0);
7211 }
7212 
7213 u_int16_t
7214 parseicmpspec(char *w, sa_family_t af)
7215 {
7216 	const struct icmpcodeent	*p;
7217 	u_long				 ulval;
7218 	u_int8_t			 icmptype;
7219 
7220 	if (af == AF_INET)
7221 		icmptype = returnicmpdefault >> 8;
7222 	else
7223 		icmptype = returnicmp6default >> 8;
7224 
7225 	if (atoul(w, &ulval) == -1) {
7226 		if ((p = geticmpcodebyname(icmptype, w, af)) == NULL) {
7227 			yyerror("unknown icmp code %s", w);
7228 			return (0);
7229 		}
7230 		ulval = p->code;
7231 	}
7232 	if (ulval > 255) {
7233 		yyerror("invalid icmp code %lu", ulval);
7234 		return (0);
7235 	}
7236 	return (icmptype << 8 | ulval);
7237 }
7238 
7239 int
7240 parseport(char *port, struct range *r, int extensions)
7241 {
7242 	char	*p = strchr(port, ':');
7243 
7244 	if (p == NULL) {
7245 		if ((r->a = getservice(port)) == -1)
7246 			return (-1);
7247 		r->b = 0;
7248 		r->t = PF_OP_NONE;
7249 		return (0);
7250 	}
7251 	if ((extensions & PPORT_STAR) && !strcmp(p+1, "*")) {
7252 		*p = 0;
7253 		if ((r->a = getservice(port)) == -1)
7254 			return (-1);
7255 		r->b = 0;
7256 		r->t = PF_OP_IRG;
7257 		return (0);
7258 	}
7259 	if ((extensions & PPORT_RANGE)) {
7260 		*p++ = 0;
7261 		if ((r->a = getservice(port)) == -1 ||
7262 		    (r->b = getservice(p)) == -1)
7263 			return (-1);
7264 		if (r->a == r->b) {
7265 			r->b = 0;
7266 			r->t = PF_OP_NONE;
7267 		} else
7268 			r->t = PF_OP_RRG;
7269 		return (0);
7270 	}
7271 	return (-1);
7272 }
7273 
7274 int
7275 pfctl_load_anchors(int dev, struct pfctl *pf, struct pfr_buffer *trans)
7276 {
7277 	struct loadanchors	*la;
7278 
7279 	TAILQ_FOREACH(la, &loadanchorshead, entries) {
7280 		if (pf->opts & PF_OPT_VERBOSE)
7281 			fprintf(stderr, "\nLoading anchor %s from %s\n",
7282 			    la->anchorname, la->filename);
7283 		if (pfctl_rules(dev, la->filename, pf->opts, pf->optimize,
7284 		    la->anchorname, trans) == -1)
7285 			return (-1);
7286 	}
7287 
7288 	return (0);
7289 }
7290 
7291 int
7292 kw_casecmp(const void *k, const void *e)
7293 {
7294 	return (strcasecmp(k, ((const struct keywords *)e)->k_name));
7295 }
7296 
7297 int
7298 map_tos(char *s, int *val)
7299 {
7300 	/* DiffServ Codepoints and other TOS mappings */
7301 	const struct keywords	 toswords[] = {
7302 		{ "af11",		IPTOS_DSCP_AF11 },
7303 		{ "af12",		IPTOS_DSCP_AF12 },
7304 		{ "af13",		IPTOS_DSCP_AF13 },
7305 		{ "af21",		IPTOS_DSCP_AF21 },
7306 		{ "af22",		IPTOS_DSCP_AF22 },
7307 		{ "af23",		IPTOS_DSCP_AF23 },
7308 		{ "af31",		IPTOS_DSCP_AF31 },
7309 		{ "af32",		IPTOS_DSCP_AF32 },
7310 		{ "af33",		IPTOS_DSCP_AF33 },
7311 		{ "af41",		IPTOS_DSCP_AF41 },
7312 		{ "af42",		IPTOS_DSCP_AF42 },
7313 		{ "af43",		IPTOS_DSCP_AF43 },
7314 		{ "critical",		IPTOS_PREC_CRITIC_ECP },
7315 		{ "cs0",		IPTOS_DSCP_CS0 },
7316 		{ "cs1",		IPTOS_DSCP_CS1 },
7317 		{ "cs2",		IPTOS_DSCP_CS2 },
7318 		{ "cs3",		IPTOS_DSCP_CS3 },
7319 		{ "cs4",		IPTOS_DSCP_CS4 },
7320 		{ "cs5",		IPTOS_DSCP_CS5 },
7321 		{ "cs6",		IPTOS_DSCP_CS6 },
7322 		{ "cs7",		IPTOS_DSCP_CS7 },
7323 		{ "ef",			IPTOS_DSCP_EF },
7324 		{ "inetcontrol",	IPTOS_PREC_INTERNETCONTROL },
7325 		{ "lowdelay",		IPTOS_LOWDELAY },
7326 		{ "netcontrol",		IPTOS_PREC_NETCONTROL },
7327 		{ "reliability",	IPTOS_RELIABILITY },
7328 		{ "throughput",		IPTOS_THROUGHPUT },
7329 		{ "va",			IPTOS_DSCP_VA }
7330 	};
7331 	const struct keywords	*p;
7332 
7333 	p = bsearch(s, toswords, sizeof(toswords)/sizeof(toswords[0]),
7334 	    sizeof(toswords[0]), kw_casecmp);
7335 
7336 	if (p) {
7337 		*val = p->k_val;
7338 		return (1);
7339 	}
7340 	return (0);
7341 }
7342 
7343 int
7344 rt_tableid_max(void)
7345 {
7346 #ifdef __FreeBSD__
7347 	int fibs;
7348 	size_t l = sizeof(fibs);
7349 
7350         if (sysctlbyname("net.fibs", &fibs, &l, NULL, 0) == -1)
7351 		fibs = 16;	/* XXX RT_MAXFIBS, at least limit it some. */
7352 	/*
7353 	 * As the OpenBSD code only compares > and not >= we need to adjust
7354 	 * here given we only accept values of 0..n and want to avoid #ifdefs
7355 	 * in the grammar.
7356 	 */
7357 	return (fibs - 1);
7358 #else
7359 	return (RT_TABLEID_MAX);
7360 #endif
7361 }
7362 
7363 struct node_mac*
7364 node_mac_from_string(const char *str)
7365 {
7366 	struct node_mac *m;
7367 
7368 	m = calloc(1, sizeof(struct node_mac));
7369 	if (m == NULL)
7370 		err(1, "mac: calloc");
7371 
7372 	if (sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7373 	    &m->mac[0], &m->mac[1], &m->mac[2], &m->mac[3], &m->mac[4],
7374 	    &m->mac[5]) != 6) {
7375 		free(m);
7376 		yyerror("invalid MAC address");
7377 		return (NULL);
7378 	}
7379 
7380 	memset(m->mask, 0xff, ETHER_ADDR_LEN);
7381 	m->isset = true;
7382 	m->next = NULL;
7383 	m->tail = m;
7384 
7385 	return (m);
7386 }
7387 
7388 struct node_mac*
7389 node_mac_from_string_masklen(const char *str, int masklen)
7390 {
7391 	struct node_mac *m;
7392 
7393 	if (masklen < 0 || masklen > (ETHER_ADDR_LEN * 8)) {
7394 		yyerror("invalid MAC mask length");
7395 		return (NULL);
7396 	}
7397 
7398 	m = node_mac_from_string(str);
7399 	if (m == NULL)
7400 		return (NULL);
7401 
7402 	memset(m->mask, 0, ETHER_ADDR_LEN);
7403 	for (int i = 0; i < masklen; i++)
7404 		m->mask[i / 8] |= 1 << (i % 8);
7405 
7406 	return (m);
7407 }
7408 
7409 struct node_mac*
7410 node_mac_from_string_mask(const char *str, const char *mask)
7411 {
7412 	struct node_mac *m;
7413 
7414 	m = node_mac_from_string(str);
7415 	if (m == NULL)
7416 		return (NULL);
7417 
7418 	if (sscanf(mask, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
7419 	    &m->mask[0], &m->mask[1], &m->mask[2], &m->mask[3], &m->mask[4],
7420 	    &m->mask[5]) != 6) {
7421 		free(m);
7422 		yyerror("invalid MAC mask");
7423 		return (NULL);
7424 	}
7425 
7426 	return (m);
7427 }
7428