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