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