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